macb_main.c 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Cadence MACB/GEM Ethernet Controller driver
  4. *
  5. * Copyright (C) 2004-2006 Atmel Corporation
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/circ_buf.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/clk.h>
  11. #include <linux/crc32.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/firmware/xlnx-zynqmp.h>
  15. #include <linux/inetdevice.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/ip.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/of.h>
  26. #include <linux/of_mdio.h>
  27. #include <linux/of_net.h>
  28. #include <linux/phy/phy.h>
  29. #include <linux/phylink.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/ptp_classify.h>
  33. #include <linux/reset.h>
  34. #include <linux/slab.h>
  35. #include <linux/tcp.h>
  36. #include <linux/types.h>
  37. #include <linux/udp.h>
  38. #include <linux/gcd.h>
  39. #include <net/pkt_sched.h>
  40. #include "macb.h"
  41. /* This structure is only used for MACB on SiFive FU540 devices */
  42. struct sifive_fu540_macb_mgmt {
  43. void __iomem *reg;
  44. unsigned long rate;
  45. struct clk_hw hw;
  46. };
  47. #define MACB_RX_BUFFER_SIZE 128
  48. #define RX_BUFFER_MULTIPLE 64 /* bytes */
  49. #define DEFAULT_RX_RING_SIZE 512 /* must be power of 2 */
  50. #define MIN_RX_RING_SIZE 64
  51. #define MAX_RX_RING_SIZE 8192
  52. #define DEFAULT_TX_RING_SIZE 512 /* must be power of 2 */
  53. #define MIN_TX_RING_SIZE 64
  54. #define MAX_TX_RING_SIZE 4096
  55. /* level of occupied TX descriptors under which we wake up TX process */
  56. #define MACB_TX_WAKEUP_THRESH(bp) (3 * (bp)->tx_ring_size / 4)
  57. #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(ISR_ROVR))
  58. #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
  59. | MACB_BIT(ISR_RLE) \
  60. | MACB_BIT(TXERR))
  61. #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP) \
  62. | MACB_BIT(TXUBR))
  63. /* Max length of transmit frame must be a multiple of 8 bytes */
  64. #define MACB_TX_LEN_ALIGN 8
  65. #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1) & ~((unsigned int)(MACB_TX_LEN_ALIGN - 1)))
  66. /* Limit maximum TX length as per Cadence TSO errata. This is to avoid a
  67. * false amba_error in TX path from the DMA assuming there is not enough
  68. * space in the SRAM (16KB) even when there is.
  69. */
  70. #define GEM_MAX_TX_LEN (unsigned int)(0x3FC0)
  71. #define GEM_MTU_MIN_SIZE ETH_MIN_MTU
  72. #define MACB_NETIF_LSO NETIF_F_TSO
  73. #define MACB_WOL_ENABLED BIT(0)
  74. #define HS_SPEED_10000M 4
  75. #define MACB_SERDES_RATE_10G 1
  76. /* Graceful stop timeouts in us. We should allow up to
  77. * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
  78. */
  79. #define MACB_HALT_TIMEOUT 14000
  80. #define MACB_PM_TIMEOUT 100 /* ms */
  81. #define MACB_MDIO_TIMEOUT 1000000 /* in usecs */
  82. /* DMA buffer descriptor might be different size
  83. * depends on hardware configuration:
  84. *
  85. * 1. dma address width 32 bits:
  86. * word 1: 32 bit address of Data Buffer
  87. * word 2: control
  88. *
  89. * 2. dma address width 64 bits:
  90. * word 1: 32 bit address of Data Buffer
  91. * word 2: control
  92. * word 3: upper 32 bit address of Data Buffer
  93. * word 4: unused
  94. *
  95. * 3. dma address width 32 bits with hardware timestamping:
  96. * word 1: 32 bit address of Data Buffer
  97. * word 2: control
  98. * word 3: timestamp word 1
  99. * word 4: timestamp word 2
  100. *
  101. * 4. dma address width 64 bits with hardware timestamping:
  102. * word 1: 32 bit address of Data Buffer
  103. * word 2: control
  104. * word 3: upper 32 bit address of Data Buffer
  105. * word 4: unused
  106. * word 5: timestamp word 1
  107. * word 6: timestamp word 2
  108. */
  109. static unsigned int macb_dma_desc_get_size(struct macb *bp)
  110. {
  111. unsigned int desc_size = sizeof(struct macb_dma_desc);
  112. if (macb_dma64(bp))
  113. desc_size += sizeof(struct macb_dma_desc_64);
  114. if (macb_dma_ptp(bp))
  115. desc_size += sizeof(struct macb_dma_desc_ptp);
  116. return desc_size;
  117. }
  118. static unsigned int macb_adj_dma_desc_idx(struct macb *bp, unsigned int desc_idx)
  119. {
  120. return desc_idx * (1 + macb_dma64(bp) + macb_dma_ptp(bp));
  121. }
  122. static struct macb_dma_desc_64 *macb_64b_desc(struct macb *bp, struct macb_dma_desc *desc)
  123. {
  124. return (struct macb_dma_desc_64 *)((void *)desc
  125. + sizeof(struct macb_dma_desc));
  126. }
  127. /* Ring buffer accessors */
  128. static unsigned int macb_tx_ring_wrap(struct macb *bp, unsigned int index)
  129. {
  130. return index & (bp->tx_ring_size - 1);
  131. }
  132. static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
  133. unsigned int index)
  134. {
  135. index = macb_tx_ring_wrap(queue->bp, index);
  136. index = macb_adj_dma_desc_idx(queue->bp, index);
  137. return &queue->tx_ring[index];
  138. }
  139. static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
  140. unsigned int index)
  141. {
  142. return &queue->tx_skb[macb_tx_ring_wrap(queue->bp, index)];
  143. }
  144. static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
  145. {
  146. dma_addr_t offset;
  147. offset = macb_tx_ring_wrap(queue->bp, index) *
  148. macb_dma_desc_get_size(queue->bp);
  149. return queue->tx_ring_dma + offset;
  150. }
  151. static unsigned int macb_rx_ring_wrap(struct macb *bp, unsigned int index)
  152. {
  153. return index & (bp->rx_ring_size - 1);
  154. }
  155. static struct macb_dma_desc *macb_rx_desc(struct macb_queue *queue, unsigned int index)
  156. {
  157. index = macb_rx_ring_wrap(queue->bp, index);
  158. index = macb_adj_dma_desc_idx(queue->bp, index);
  159. return &queue->rx_ring[index];
  160. }
  161. static void *macb_rx_buffer(struct macb_queue *queue, unsigned int index)
  162. {
  163. return queue->rx_buffers + queue->bp->rx_buffer_size *
  164. macb_rx_ring_wrap(queue->bp, index);
  165. }
  166. /* I/O accessors */
  167. static u32 hw_readl_native(struct macb *bp, int offset)
  168. {
  169. return __raw_readl(bp->regs + offset);
  170. }
  171. static void hw_writel_native(struct macb *bp, int offset, u32 value)
  172. {
  173. __raw_writel(value, bp->regs + offset);
  174. }
  175. static u32 hw_readl(struct macb *bp, int offset)
  176. {
  177. return readl_relaxed(bp->regs + offset);
  178. }
  179. static void hw_writel(struct macb *bp, int offset, u32 value)
  180. {
  181. writel_relaxed(value, bp->regs + offset);
  182. }
  183. /* Find the CPU endianness by using the loopback bit of NCR register. When the
  184. * CPU is in big endian we need to program swapped mode for management
  185. * descriptor access.
  186. */
  187. static bool hw_is_native_io(void __iomem *addr)
  188. {
  189. u32 value = MACB_BIT(LLB);
  190. __raw_writel(value, addr + MACB_NCR);
  191. value = __raw_readl(addr + MACB_NCR);
  192. /* Write 0 back to disable everything */
  193. __raw_writel(0, addr + MACB_NCR);
  194. return value == MACB_BIT(LLB);
  195. }
  196. static bool hw_is_gem(void __iomem *addr, bool native_io)
  197. {
  198. u32 id;
  199. if (native_io)
  200. id = __raw_readl(addr + MACB_MID);
  201. else
  202. id = readl_relaxed(addr + MACB_MID);
  203. return MACB_BFEXT(IDNUM, id) >= 0x2;
  204. }
  205. static void macb_set_hwaddr(struct macb *bp)
  206. {
  207. u32 bottom;
  208. u16 top;
  209. bottom = get_unaligned_le32(bp->dev->dev_addr);
  210. macb_or_gem_writel(bp, SA1B, bottom);
  211. top = get_unaligned_le16(bp->dev->dev_addr + 4);
  212. macb_or_gem_writel(bp, SA1T, top);
  213. if (gem_has_ptp(bp)) {
  214. gem_writel(bp, RXPTPUNI, bottom);
  215. gem_writel(bp, TXPTPUNI, bottom);
  216. }
  217. /* Clear unused address register sets */
  218. macb_or_gem_writel(bp, SA2B, 0);
  219. macb_or_gem_writel(bp, SA2T, 0);
  220. macb_or_gem_writel(bp, SA3B, 0);
  221. macb_or_gem_writel(bp, SA3T, 0);
  222. macb_or_gem_writel(bp, SA4B, 0);
  223. macb_or_gem_writel(bp, SA4T, 0);
  224. }
  225. static void macb_get_hwaddr(struct macb *bp)
  226. {
  227. u32 bottom;
  228. u16 top;
  229. u8 addr[6];
  230. int i;
  231. /* Check all 4 address register for valid address */
  232. for (i = 0; i < 4; i++) {
  233. bottom = macb_or_gem_readl(bp, SA1B + i * 8);
  234. top = macb_or_gem_readl(bp, SA1T + i * 8);
  235. addr[0] = bottom & 0xff;
  236. addr[1] = (bottom >> 8) & 0xff;
  237. addr[2] = (bottom >> 16) & 0xff;
  238. addr[3] = (bottom >> 24) & 0xff;
  239. addr[4] = top & 0xff;
  240. addr[5] = (top >> 8) & 0xff;
  241. if (is_valid_ether_addr(addr)) {
  242. eth_hw_addr_set(bp->dev, addr);
  243. return;
  244. }
  245. }
  246. dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
  247. eth_hw_addr_random(bp->dev);
  248. }
  249. static int macb_mdio_wait_for_idle(struct macb *bp)
  250. {
  251. u32 val;
  252. return readx_poll_timeout(MACB_READ_NSR, bp, val, val & MACB_BIT(IDLE),
  253. 1, MACB_MDIO_TIMEOUT);
  254. }
  255. static int macb_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum)
  256. {
  257. struct macb *bp = bus->priv;
  258. int status;
  259. status = pm_runtime_resume_and_get(&bp->pdev->dev);
  260. if (status < 0)
  261. goto mdio_pm_exit;
  262. status = macb_mdio_wait_for_idle(bp);
  263. if (status < 0)
  264. goto mdio_read_exit;
  265. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
  266. | MACB_BF(RW, MACB_MAN_C22_READ)
  267. | MACB_BF(PHYA, mii_id)
  268. | MACB_BF(REGA, regnum)
  269. | MACB_BF(CODE, MACB_MAN_C22_CODE)));
  270. status = macb_mdio_wait_for_idle(bp);
  271. if (status < 0)
  272. goto mdio_read_exit;
  273. status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  274. mdio_read_exit:
  275. pm_runtime_put_autosuspend(&bp->pdev->dev);
  276. mdio_pm_exit:
  277. return status;
  278. }
  279. static int macb_mdio_read_c45(struct mii_bus *bus, int mii_id, int devad,
  280. int regnum)
  281. {
  282. struct macb *bp = bus->priv;
  283. int status;
  284. status = pm_runtime_get_sync(&bp->pdev->dev);
  285. if (status < 0) {
  286. pm_runtime_put_noidle(&bp->pdev->dev);
  287. goto mdio_pm_exit;
  288. }
  289. status = macb_mdio_wait_for_idle(bp);
  290. if (status < 0)
  291. goto mdio_read_exit;
  292. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  293. | MACB_BF(RW, MACB_MAN_C45_ADDR)
  294. | MACB_BF(PHYA, mii_id)
  295. | MACB_BF(REGA, devad & 0x1F)
  296. | MACB_BF(DATA, regnum & 0xFFFF)
  297. | MACB_BF(CODE, MACB_MAN_C45_CODE)));
  298. status = macb_mdio_wait_for_idle(bp);
  299. if (status < 0)
  300. goto mdio_read_exit;
  301. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  302. | MACB_BF(RW, MACB_MAN_C45_READ)
  303. | MACB_BF(PHYA, mii_id)
  304. | MACB_BF(REGA, devad & 0x1F)
  305. | MACB_BF(CODE, MACB_MAN_C45_CODE)));
  306. status = macb_mdio_wait_for_idle(bp);
  307. if (status < 0)
  308. goto mdio_read_exit;
  309. status = MACB_BFEXT(DATA, macb_readl(bp, MAN));
  310. mdio_read_exit:
  311. pm_runtime_put_autosuspend(&bp->pdev->dev);
  312. mdio_pm_exit:
  313. return status;
  314. }
  315. static int macb_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum,
  316. u16 value)
  317. {
  318. struct macb *bp = bus->priv;
  319. int status;
  320. status = pm_runtime_resume_and_get(&bp->pdev->dev);
  321. if (status < 0)
  322. goto mdio_pm_exit;
  323. status = macb_mdio_wait_for_idle(bp);
  324. if (status < 0)
  325. goto mdio_write_exit;
  326. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C22_SOF)
  327. | MACB_BF(RW, MACB_MAN_C22_WRITE)
  328. | MACB_BF(PHYA, mii_id)
  329. | MACB_BF(REGA, regnum)
  330. | MACB_BF(CODE, MACB_MAN_C22_CODE)
  331. | MACB_BF(DATA, value)));
  332. status = macb_mdio_wait_for_idle(bp);
  333. if (status < 0)
  334. goto mdio_write_exit;
  335. mdio_write_exit:
  336. pm_runtime_put_autosuspend(&bp->pdev->dev);
  337. mdio_pm_exit:
  338. return status;
  339. }
  340. static int macb_mdio_write_c45(struct mii_bus *bus, int mii_id,
  341. int devad, int regnum,
  342. u16 value)
  343. {
  344. struct macb *bp = bus->priv;
  345. int status;
  346. status = pm_runtime_get_sync(&bp->pdev->dev);
  347. if (status < 0) {
  348. pm_runtime_put_noidle(&bp->pdev->dev);
  349. goto mdio_pm_exit;
  350. }
  351. status = macb_mdio_wait_for_idle(bp);
  352. if (status < 0)
  353. goto mdio_write_exit;
  354. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  355. | MACB_BF(RW, MACB_MAN_C45_ADDR)
  356. | MACB_BF(PHYA, mii_id)
  357. | MACB_BF(REGA, devad & 0x1F)
  358. | MACB_BF(DATA, regnum & 0xFFFF)
  359. | MACB_BF(CODE, MACB_MAN_C45_CODE)));
  360. status = macb_mdio_wait_for_idle(bp);
  361. if (status < 0)
  362. goto mdio_write_exit;
  363. macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_C45_SOF)
  364. | MACB_BF(RW, MACB_MAN_C45_WRITE)
  365. | MACB_BF(PHYA, mii_id)
  366. | MACB_BF(REGA, devad & 0x1F)
  367. | MACB_BF(CODE, MACB_MAN_C45_CODE)
  368. | MACB_BF(DATA, value)));
  369. status = macb_mdio_wait_for_idle(bp);
  370. if (status < 0)
  371. goto mdio_write_exit;
  372. mdio_write_exit:
  373. pm_runtime_put_autosuspend(&bp->pdev->dev);
  374. mdio_pm_exit:
  375. return status;
  376. }
  377. static void macb_init_buffers(struct macb *bp)
  378. {
  379. struct macb_queue *queue;
  380. unsigned int q;
  381. /* Single register for all queues' high 32 bits. */
  382. if (macb_dma64(bp)) {
  383. macb_writel(bp, RBQPH,
  384. upper_32_bits(bp->queues[0].rx_ring_dma));
  385. macb_writel(bp, TBQPH,
  386. upper_32_bits(bp->queues[0].tx_ring_dma));
  387. }
  388. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  389. queue_writel(queue, RBQP, lower_32_bits(queue->rx_ring_dma));
  390. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  391. }
  392. }
  393. /**
  394. * macb_set_tx_clk() - Set a clock to a new frequency
  395. * @bp: pointer to struct macb
  396. * @speed: New frequency in Hz
  397. */
  398. static void macb_set_tx_clk(struct macb *bp, int speed)
  399. {
  400. long ferr, rate, rate_rounded;
  401. if (!bp->tx_clk || (bp->caps & MACB_CAPS_CLK_HW_CHG))
  402. return;
  403. /* In case of MII the PHY is the clock master */
  404. if (bp->phy_interface == PHY_INTERFACE_MODE_MII)
  405. return;
  406. rate = rgmii_clock(speed);
  407. if (rate < 0)
  408. return;
  409. rate_rounded = clk_round_rate(bp->tx_clk, rate);
  410. if (rate_rounded < 0)
  411. return;
  412. /* RGMII allows 50 ppm frequency error. Test and warn if this limit
  413. * is not satisfied.
  414. */
  415. ferr = abs(rate_rounded - rate);
  416. ferr = DIV_ROUND_UP(ferr, rate / 100000);
  417. if (ferr > 5)
  418. netdev_warn(bp->dev,
  419. "unable to generate target frequency: %ld Hz\n",
  420. rate);
  421. if (clk_set_rate(bp->tx_clk, rate_rounded))
  422. netdev_err(bp->dev, "adjusting tx_clk failed.\n");
  423. }
  424. static void macb_usx_pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
  425. phy_interface_t interface, int speed,
  426. int duplex)
  427. {
  428. struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
  429. u32 config;
  430. config = gem_readl(bp, USX_CONTROL);
  431. config = GEM_BFINS(SERDES_RATE, MACB_SERDES_RATE_10G, config);
  432. config = GEM_BFINS(USX_CTRL_SPEED, HS_SPEED_10000M, config);
  433. config &= ~(GEM_BIT(TX_SCR_BYPASS) | GEM_BIT(RX_SCR_BYPASS));
  434. config |= GEM_BIT(TX_EN);
  435. gem_writel(bp, USX_CONTROL, config);
  436. }
  437. static void macb_usx_pcs_get_state(struct phylink_pcs *pcs,
  438. unsigned int neg_mode,
  439. struct phylink_link_state *state)
  440. {
  441. struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
  442. u32 val;
  443. state->speed = SPEED_10000;
  444. state->duplex = 1;
  445. state->an_complete = 1;
  446. val = gem_readl(bp, USX_STATUS);
  447. state->link = !!(val & GEM_BIT(USX_BLOCK_LOCK));
  448. val = gem_readl(bp, NCFGR);
  449. if (val & GEM_BIT(PAE))
  450. state->pause = MLO_PAUSE_RX;
  451. }
  452. static int macb_usx_pcs_config(struct phylink_pcs *pcs,
  453. unsigned int neg_mode,
  454. phy_interface_t interface,
  455. const unsigned long *advertising,
  456. bool permit_pause_to_mac)
  457. {
  458. struct macb *bp = container_of(pcs, struct macb, phylink_usx_pcs);
  459. gem_writel(bp, USX_CONTROL, gem_readl(bp, USX_CONTROL) |
  460. GEM_BIT(SIGNAL_OK));
  461. return 0;
  462. }
  463. static void macb_pcs_get_state(struct phylink_pcs *pcs, unsigned int neg_mode,
  464. struct phylink_link_state *state)
  465. {
  466. state->link = 0;
  467. }
  468. static void macb_pcs_an_restart(struct phylink_pcs *pcs)
  469. {
  470. /* Not supported */
  471. }
  472. static int macb_pcs_config(struct phylink_pcs *pcs,
  473. unsigned int neg_mode,
  474. phy_interface_t interface,
  475. const unsigned long *advertising,
  476. bool permit_pause_to_mac)
  477. {
  478. return 0;
  479. }
  480. static const struct phylink_pcs_ops macb_phylink_usx_pcs_ops = {
  481. .pcs_get_state = macb_usx_pcs_get_state,
  482. .pcs_config = macb_usx_pcs_config,
  483. .pcs_link_up = macb_usx_pcs_link_up,
  484. };
  485. static const struct phylink_pcs_ops macb_phylink_pcs_ops = {
  486. .pcs_get_state = macb_pcs_get_state,
  487. .pcs_an_restart = macb_pcs_an_restart,
  488. .pcs_config = macb_pcs_config,
  489. };
  490. static void macb_mac_config(struct phylink_config *config, unsigned int mode,
  491. const struct phylink_link_state *state)
  492. {
  493. struct net_device *ndev = to_net_dev(config->dev);
  494. struct macb *bp = netdev_priv(ndev);
  495. unsigned long flags;
  496. u32 old_ctrl, ctrl;
  497. u32 old_ncr, ncr;
  498. spin_lock_irqsave(&bp->lock, flags);
  499. old_ctrl = ctrl = macb_or_gem_readl(bp, NCFGR);
  500. old_ncr = ncr = macb_or_gem_readl(bp, NCR);
  501. if (bp->caps & MACB_CAPS_MACB_IS_EMAC) {
  502. if (state->interface == PHY_INTERFACE_MODE_RMII)
  503. ctrl |= MACB_BIT(RM9200_RMII);
  504. } else if (macb_is_gem(bp)) {
  505. ctrl &= ~(GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL));
  506. ncr &= ~GEM_BIT(ENABLE_HS_MAC);
  507. if (state->interface == PHY_INTERFACE_MODE_SGMII) {
  508. ctrl |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  509. } else if (state->interface == PHY_INTERFACE_MODE_10GBASER) {
  510. ctrl |= GEM_BIT(PCSSEL);
  511. ncr |= GEM_BIT(ENABLE_HS_MAC);
  512. } else if (bp->caps & MACB_CAPS_MIIONRGMII &&
  513. bp->phy_interface == PHY_INTERFACE_MODE_MII) {
  514. ncr |= MACB_BIT(MIIONRGMII);
  515. }
  516. }
  517. /* Apply the new configuration, if any */
  518. if (old_ctrl ^ ctrl)
  519. macb_or_gem_writel(bp, NCFGR, ctrl);
  520. if (old_ncr ^ ncr)
  521. macb_or_gem_writel(bp, NCR, ncr);
  522. /* Disable AN for SGMII fixed link configuration, enable otherwise.
  523. * Must be written after PCSSEL is set in NCFGR,
  524. * otherwise writes will not take effect.
  525. */
  526. if (macb_is_gem(bp) && state->interface == PHY_INTERFACE_MODE_SGMII) {
  527. u32 pcsctrl, old_pcsctrl;
  528. old_pcsctrl = gem_readl(bp, PCSCNTRL);
  529. if (mode == MLO_AN_FIXED)
  530. pcsctrl = old_pcsctrl & ~GEM_BIT(PCSAUTONEG);
  531. else
  532. pcsctrl = old_pcsctrl | GEM_BIT(PCSAUTONEG);
  533. if (old_pcsctrl != pcsctrl)
  534. gem_writel(bp, PCSCNTRL, pcsctrl);
  535. }
  536. spin_unlock_irqrestore(&bp->lock, flags);
  537. }
  538. static void macb_mac_link_down(struct phylink_config *config, unsigned int mode,
  539. phy_interface_t interface)
  540. {
  541. struct net_device *ndev = to_net_dev(config->dev);
  542. struct macb *bp = netdev_priv(ndev);
  543. struct macb_queue *queue;
  544. unsigned int q;
  545. u32 ctrl;
  546. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
  547. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  548. queue_writel(queue, IDR,
  549. bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
  550. /* Disable Rx and Tx */
  551. ctrl = macb_readl(bp, NCR) & ~(MACB_BIT(RE) | MACB_BIT(TE));
  552. macb_writel(bp, NCR, ctrl);
  553. netif_tx_stop_all_queues(ndev);
  554. }
  555. /* Use juggling algorithm to left rotate tx ring and tx skb array */
  556. static void gem_shuffle_tx_one_ring(struct macb_queue *queue)
  557. {
  558. unsigned int head, tail, count, ring_size, desc_size;
  559. struct macb_tx_skb tx_skb, *skb_curr, *skb_next;
  560. struct macb_dma_desc *desc_curr, *desc_next;
  561. unsigned int i, cycles, shift, curr, next;
  562. struct macb *bp = queue->bp;
  563. unsigned char desc[24];
  564. unsigned long flags;
  565. desc_size = macb_dma_desc_get_size(bp);
  566. if (WARN_ON_ONCE(desc_size > ARRAY_SIZE(desc)))
  567. return;
  568. spin_lock_irqsave(&queue->tx_ptr_lock, flags);
  569. head = queue->tx_head;
  570. tail = queue->tx_tail;
  571. ring_size = bp->tx_ring_size;
  572. count = CIRC_CNT(head, tail, ring_size);
  573. if (!(tail % ring_size))
  574. goto unlock;
  575. if (!count) {
  576. queue->tx_head = 0;
  577. queue->tx_tail = 0;
  578. goto unlock;
  579. }
  580. shift = tail % ring_size;
  581. cycles = gcd(ring_size, shift);
  582. for (i = 0; i < cycles; i++) {
  583. memcpy(&desc, macb_tx_desc(queue, i), desc_size);
  584. memcpy(&tx_skb, macb_tx_skb(queue, i),
  585. sizeof(struct macb_tx_skb));
  586. curr = i;
  587. next = (curr + shift) % ring_size;
  588. while (next != i) {
  589. desc_curr = macb_tx_desc(queue, curr);
  590. desc_next = macb_tx_desc(queue, next);
  591. memcpy(desc_curr, desc_next, desc_size);
  592. if (next == ring_size - 1)
  593. desc_curr->ctrl &= ~MACB_BIT(TX_WRAP);
  594. if (curr == ring_size - 1)
  595. desc_curr->ctrl |= MACB_BIT(TX_WRAP);
  596. skb_curr = macb_tx_skb(queue, curr);
  597. skb_next = macb_tx_skb(queue, next);
  598. memcpy(skb_curr, skb_next, sizeof(struct macb_tx_skb));
  599. curr = next;
  600. next = (curr + shift) % ring_size;
  601. }
  602. desc_curr = macb_tx_desc(queue, curr);
  603. memcpy(desc_curr, &desc, desc_size);
  604. if (i == ring_size - 1)
  605. desc_curr->ctrl &= ~MACB_BIT(TX_WRAP);
  606. if (curr == ring_size - 1)
  607. desc_curr->ctrl |= MACB_BIT(TX_WRAP);
  608. memcpy(macb_tx_skb(queue, curr), &tx_skb,
  609. sizeof(struct macb_tx_skb));
  610. }
  611. queue->tx_head = count;
  612. queue->tx_tail = 0;
  613. /* Make descriptor updates visible to hardware */
  614. wmb();
  615. unlock:
  616. spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
  617. }
  618. /* Rotate the queue so that the tail is at index 0 */
  619. static void gem_shuffle_tx_rings(struct macb *bp)
  620. {
  621. struct macb_queue *queue;
  622. int q;
  623. for (q = 0, queue = bp->queues; q < bp->num_queues; q++, queue++)
  624. gem_shuffle_tx_one_ring(queue);
  625. }
  626. static void macb_mac_link_up(struct phylink_config *config,
  627. struct phy_device *phy,
  628. unsigned int mode, phy_interface_t interface,
  629. int speed, int duplex,
  630. bool tx_pause, bool rx_pause)
  631. {
  632. struct net_device *ndev = to_net_dev(config->dev);
  633. struct macb *bp = netdev_priv(ndev);
  634. struct macb_queue *queue;
  635. unsigned long flags;
  636. unsigned int q;
  637. u32 ctrl;
  638. spin_lock_irqsave(&bp->lock, flags);
  639. ctrl = macb_or_gem_readl(bp, NCFGR);
  640. ctrl &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  641. if (speed == SPEED_100)
  642. ctrl |= MACB_BIT(SPD);
  643. if (duplex)
  644. ctrl |= MACB_BIT(FD);
  645. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
  646. ctrl &= ~MACB_BIT(PAE);
  647. if (macb_is_gem(bp)) {
  648. ctrl &= ~GEM_BIT(GBE);
  649. if (speed == SPEED_1000)
  650. ctrl |= GEM_BIT(GBE);
  651. }
  652. if (rx_pause)
  653. ctrl |= MACB_BIT(PAE);
  654. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  655. queue_writel(queue, IER,
  656. bp->rx_intr_mask | MACB_TX_INT_FLAGS | MACB_BIT(HRESP));
  657. }
  658. }
  659. macb_or_gem_writel(bp, NCFGR, ctrl);
  660. if (bp->phy_interface == PHY_INTERFACE_MODE_10GBASER)
  661. gem_writel(bp, HS_MAC_CONFIG, GEM_BFINS(HS_MAC_SPEED, HS_SPEED_10000M,
  662. gem_readl(bp, HS_MAC_CONFIG)));
  663. spin_unlock_irqrestore(&bp->lock, flags);
  664. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
  665. macb_set_tx_clk(bp, speed);
  666. gem_shuffle_tx_rings(bp);
  667. }
  668. /* Enable Rx and Tx; Enable PTP unicast */
  669. ctrl = macb_readl(bp, NCR);
  670. if (gem_has_ptp(bp))
  671. ctrl |= MACB_BIT(PTPUNI);
  672. macb_writel(bp, NCR, ctrl | MACB_BIT(RE) | MACB_BIT(TE));
  673. netif_tx_wake_all_queues(ndev);
  674. }
  675. static struct phylink_pcs *macb_mac_select_pcs(struct phylink_config *config,
  676. phy_interface_t interface)
  677. {
  678. struct net_device *ndev = to_net_dev(config->dev);
  679. struct macb *bp = netdev_priv(ndev);
  680. if (interface == PHY_INTERFACE_MODE_10GBASER)
  681. return &bp->phylink_usx_pcs;
  682. else if (interface == PHY_INTERFACE_MODE_SGMII)
  683. return &bp->phylink_sgmii_pcs;
  684. else
  685. return NULL;
  686. }
  687. static const struct phylink_mac_ops macb_phylink_ops = {
  688. .mac_select_pcs = macb_mac_select_pcs,
  689. .mac_config = macb_mac_config,
  690. .mac_link_down = macb_mac_link_down,
  691. .mac_link_up = macb_mac_link_up,
  692. };
  693. static bool macb_phy_handle_exists(struct device_node *dn)
  694. {
  695. dn = of_parse_phandle(dn, "phy-handle", 0);
  696. of_node_put(dn);
  697. return dn != NULL;
  698. }
  699. static int macb_phylink_connect(struct macb *bp)
  700. {
  701. struct device_node *dn = bp->pdev->dev.of_node;
  702. struct net_device *dev = bp->dev;
  703. struct phy_device *phydev;
  704. int ret;
  705. if (dn)
  706. ret = phylink_of_phy_connect(bp->phylink, dn, 0);
  707. if (!dn || (ret && !macb_phy_handle_exists(dn))) {
  708. phydev = phy_find_first(bp->mii_bus);
  709. if (!phydev) {
  710. netdev_err(dev, "no PHY found\n");
  711. return -ENXIO;
  712. }
  713. /* attach the mac to the phy */
  714. ret = phylink_connect_phy(bp->phylink, phydev);
  715. }
  716. if (ret) {
  717. netdev_err(dev, "Could not attach PHY (%d)\n", ret);
  718. return ret;
  719. }
  720. phylink_start(bp->phylink);
  721. return 0;
  722. }
  723. static void macb_get_pcs_fixed_state(struct phylink_config *config,
  724. struct phylink_link_state *state)
  725. {
  726. struct net_device *ndev = to_net_dev(config->dev);
  727. struct macb *bp = netdev_priv(ndev);
  728. state->link = (macb_readl(bp, NSR) & MACB_BIT(NSR_LINK)) != 0;
  729. }
  730. /* based on au1000_eth. c*/
  731. static int macb_mii_probe(struct net_device *dev)
  732. {
  733. struct macb *bp = netdev_priv(dev);
  734. bp->phylink_sgmii_pcs.ops = &macb_phylink_pcs_ops;
  735. bp->phylink_usx_pcs.ops = &macb_phylink_usx_pcs_ops;
  736. bp->phylink_config.dev = &dev->dev;
  737. bp->phylink_config.type = PHYLINK_NETDEV;
  738. bp->phylink_config.mac_managed_pm = true;
  739. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  740. bp->phylink_config.poll_fixed_state = true;
  741. bp->phylink_config.get_fixed_state = macb_get_pcs_fixed_state;
  742. }
  743. bp->phylink_config.mac_capabilities = MAC_ASYM_PAUSE |
  744. MAC_10 | MAC_100;
  745. __set_bit(PHY_INTERFACE_MODE_MII,
  746. bp->phylink_config.supported_interfaces);
  747. __set_bit(PHY_INTERFACE_MODE_RMII,
  748. bp->phylink_config.supported_interfaces);
  749. /* Determine what modes are supported */
  750. if (macb_is_gem(bp) && (bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)) {
  751. bp->phylink_config.mac_capabilities |= MAC_1000FD;
  752. if (!(bp->caps & MACB_CAPS_NO_GIGABIT_HALF))
  753. bp->phylink_config.mac_capabilities |= MAC_1000HD;
  754. __set_bit(PHY_INTERFACE_MODE_GMII,
  755. bp->phylink_config.supported_interfaces);
  756. phy_interface_set_rgmii(bp->phylink_config.supported_interfaces);
  757. if (bp->caps & MACB_CAPS_PCS)
  758. __set_bit(PHY_INTERFACE_MODE_SGMII,
  759. bp->phylink_config.supported_interfaces);
  760. if (bp->caps & MACB_CAPS_HIGH_SPEED) {
  761. __set_bit(PHY_INTERFACE_MODE_10GBASER,
  762. bp->phylink_config.supported_interfaces);
  763. bp->phylink_config.mac_capabilities |= MAC_10000FD;
  764. }
  765. }
  766. bp->phylink = phylink_create(&bp->phylink_config, bp->pdev->dev.fwnode,
  767. bp->phy_interface, &macb_phylink_ops);
  768. if (IS_ERR(bp->phylink)) {
  769. netdev_err(dev, "Could not create a phylink instance (%ld)\n",
  770. PTR_ERR(bp->phylink));
  771. return PTR_ERR(bp->phylink);
  772. }
  773. return 0;
  774. }
  775. static int macb_mdiobus_register(struct macb *bp, struct device_node *mdio_np)
  776. {
  777. struct device_node *child, *np = bp->pdev->dev.of_node;
  778. /* If we have a child named mdio, probe it instead of looking for PHYs
  779. * directly under the MAC node
  780. */
  781. if (mdio_np)
  782. return of_mdiobus_register(bp->mii_bus, mdio_np);
  783. /* Only create the PHY from the device tree if at least one PHY is
  784. * described. Otherwise scan the entire MDIO bus. We do this to support
  785. * old device tree that did not follow the best practices and did not
  786. * describe their network PHYs.
  787. */
  788. for_each_available_child_of_node(np, child)
  789. if (of_mdiobus_child_is_phy(child)) {
  790. /* The loop increments the child refcount,
  791. * decrement it before returning.
  792. */
  793. of_node_put(child);
  794. return of_mdiobus_register(bp->mii_bus, np);
  795. }
  796. return mdiobus_register(bp->mii_bus);
  797. }
  798. static int macb_mii_init(struct macb *bp)
  799. {
  800. struct device_node *mdio_np, *np = bp->pdev->dev.of_node;
  801. int err = -ENXIO;
  802. /* With fixed-link, we don't need to register the MDIO bus,
  803. * except if we have a child named "mdio" in the device tree.
  804. * In that case, some devices may be attached to the MACB's MDIO bus.
  805. */
  806. mdio_np = of_get_child_by_name(np, "mdio");
  807. if (!mdio_np && of_phy_is_fixed_link(np))
  808. return macb_mii_probe(bp->dev);
  809. /* Enable management port */
  810. macb_writel(bp, NCR, MACB_BIT(MPE));
  811. bp->mii_bus = mdiobus_alloc();
  812. if (!bp->mii_bus) {
  813. err = -ENOMEM;
  814. goto err_out;
  815. }
  816. bp->mii_bus->name = "MACB_mii_bus";
  817. bp->mii_bus->read = &macb_mdio_read_c22;
  818. bp->mii_bus->write = &macb_mdio_write_c22;
  819. bp->mii_bus->read_c45 = &macb_mdio_read_c45;
  820. bp->mii_bus->write_c45 = &macb_mdio_write_c45;
  821. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  822. bp->pdev->name, bp->pdev->id);
  823. bp->mii_bus->priv = bp;
  824. bp->mii_bus->parent = &bp->pdev->dev;
  825. dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
  826. err = macb_mdiobus_register(bp, mdio_np);
  827. if (err)
  828. goto err_out_free_mdiobus;
  829. err = macb_mii_probe(bp->dev);
  830. if (err)
  831. goto err_out_unregister_bus;
  832. return 0;
  833. err_out_unregister_bus:
  834. mdiobus_unregister(bp->mii_bus);
  835. err_out_free_mdiobus:
  836. mdiobus_free(bp->mii_bus);
  837. err_out:
  838. of_node_put(mdio_np);
  839. return err;
  840. }
  841. static void macb_update_stats(struct macb *bp)
  842. {
  843. u64 *p = &bp->hw_stats.macb.rx_pause_frames;
  844. u64 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
  845. int offset = MACB_PFR;
  846. WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
  847. for (; p < end; p++, offset += 4)
  848. *p += bp->macb_reg_readl(bp, offset);
  849. }
  850. static int macb_halt_tx(struct macb *bp)
  851. {
  852. u32 status;
  853. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
  854. /* Poll TSR until TGO is cleared or timeout. */
  855. return read_poll_timeout_atomic(macb_readl, status,
  856. !(status & MACB_BIT(TGO)),
  857. 250, MACB_HALT_TIMEOUT, false,
  858. bp, TSR);
  859. }
  860. static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb, int budget)
  861. {
  862. if (tx_skb->mapping) {
  863. if (tx_skb->mapped_as_page)
  864. dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
  865. tx_skb->size, DMA_TO_DEVICE);
  866. else
  867. dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
  868. tx_skb->size, DMA_TO_DEVICE);
  869. tx_skb->mapping = 0;
  870. }
  871. if (tx_skb->skb) {
  872. dev_consume_skb_any(tx_skb->skb);
  873. tx_skb->skb = NULL;
  874. }
  875. }
  876. static void macb_set_addr(struct macb *bp, struct macb_dma_desc *desc, dma_addr_t addr)
  877. {
  878. if (macb_dma64(bp)) {
  879. struct macb_dma_desc_64 *desc_64;
  880. desc_64 = macb_64b_desc(bp, desc);
  881. desc_64->addrh = upper_32_bits(addr);
  882. /* The low bits of RX address contain the RX_USED bit, clearing
  883. * of which allows packet RX. Make sure the high bits are also
  884. * visible to HW at that point.
  885. */
  886. dma_wmb();
  887. }
  888. desc->addr = lower_32_bits(addr);
  889. }
  890. static dma_addr_t macb_get_addr(struct macb *bp, struct macb_dma_desc *desc)
  891. {
  892. dma_addr_t addr = 0;
  893. if (macb_dma64(bp)) {
  894. struct macb_dma_desc_64 *desc_64;
  895. desc_64 = macb_64b_desc(bp, desc);
  896. addr = ((u64)(desc_64->addrh) << 32);
  897. }
  898. addr |= MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
  899. if (macb_dma_ptp(bp))
  900. addr &= ~GEM_BIT(DMA_RXVALID);
  901. return addr;
  902. }
  903. static void macb_tx_error_task(struct work_struct *work)
  904. {
  905. struct macb_queue *queue = container_of(work, struct macb_queue,
  906. tx_error_task);
  907. bool halt_timeout = false;
  908. struct macb *bp = queue->bp;
  909. u32 queue_index;
  910. u32 packets = 0;
  911. u32 bytes = 0;
  912. struct macb_tx_skb *tx_skb;
  913. struct macb_dma_desc *desc;
  914. struct sk_buff *skb;
  915. unsigned int tail;
  916. unsigned long flags;
  917. queue_index = queue - bp->queues;
  918. netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
  919. queue_index, queue->tx_tail, queue->tx_head);
  920. /* Prevent the queue NAPI TX poll from running, as it calls
  921. * macb_tx_complete(), which in turn may call netif_wake_subqueue().
  922. * As explained below, we have to halt the transmission before updating
  923. * TBQP registers so we call netif_tx_stop_all_queues() to notify the
  924. * network engine about the macb/gem being halted.
  925. */
  926. napi_disable(&queue->napi_tx);
  927. spin_lock_irqsave(&bp->lock, flags);
  928. /* Make sure nobody is trying to queue up new packets */
  929. netif_tx_stop_all_queues(bp->dev);
  930. /* Stop transmission now
  931. * (in case we have just queued new packets)
  932. * macb/gem must be halted to write TBQP register
  933. */
  934. if (macb_halt_tx(bp)) {
  935. netdev_err(bp->dev, "BUG: halt tx timed out\n");
  936. macb_writel(bp, NCR, macb_readl(bp, NCR) & (~MACB_BIT(TE)));
  937. halt_timeout = true;
  938. }
  939. /* Treat frames in TX queue including the ones that caused the error.
  940. * Free transmit buffers in upper layer.
  941. */
  942. for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
  943. u32 ctrl;
  944. desc = macb_tx_desc(queue, tail);
  945. ctrl = desc->ctrl;
  946. tx_skb = macb_tx_skb(queue, tail);
  947. skb = tx_skb->skb;
  948. if (ctrl & MACB_BIT(TX_USED)) {
  949. /* skb is set for the last buffer of the frame */
  950. while (!skb) {
  951. macb_tx_unmap(bp, tx_skb, 0);
  952. tail++;
  953. tx_skb = macb_tx_skb(queue, tail);
  954. skb = tx_skb->skb;
  955. }
  956. /* ctrl still refers to the first buffer descriptor
  957. * since it's the only one written back by the hardware
  958. */
  959. if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
  960. netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
  961. macb_tx_ring_wrap(bp, tail),
  962. skb->data);
  963. bp->dev->stats.tx_packets++;
  964. queue->stats.tx_packets++;
  965. packets++;
  966. bp->dev->stats.tx_bytes += skb->len;
  967. queue->stats.tx_bytes += skb->len;
  968. bytes += skb->len;
  969. }
  970. } else {
  971. /* "Buffers exhausted mid-frame" errors may only happen
  972. * if the driver is buggy, so complain loudly about
  973. * those. Statistics are updated by hardware.
  974. */
  975. if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
  976. netdev_err(bp->dev,
  977. "BUG: TX buffers exhausted mid-frame\n");
  978. desc->ctrl = ctrl | MACB_BIT(TX_USED);
  979. }
  980. macb_tx_unmap(bp, tx_skb, 0);
  981. }
  982. netdev_tx_completed_queue(netdev_get_tx_queue(bp->dev, queue_index),
  983. packets, bytes);
  984. /* Set end of TX queue */
  985. desc = macb_tx_desc(queue, 0);
  986. macb_set_addr(bp, desc, 0);
  987. desc->ctrl = MACB_BIT(TX_USED);
  988. /* Make descriptor updates visible to hardware */
  989. wmb();
  990. /* Reinitialize the TX desc queue */
  991. queue_writel(queue, TBQP, lower_32_bits(queue->tx_ring_dma));
  992. /* Make TX ring reflect state of hardware */
  993. queue->tx_head = 0;
  994. queue->tx_tail = 0;
  995. /* Housework before enabling TX IRQ */
  996. macb_writel(bp, TSR, macb_readl(bp, TSR));
  997. queue_writel(queue, IER, MACB_TX_INT_FLAGS);
  998. if (halt_timeout)
  999. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TE));
  1000. /* Now we are ready to start transmission again */
  1001. netif_tx_start_all_queues(bp->dev);
  1002. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1003. spin_unlock_irqrestore(&bp->lock, flags);
  1004. napi_enable(&queue->napi_tx);
  1005. }
  1006. static bool ptp_one_step_sync(struct sk_buff *skb)
  1007. {
  1008. struct ptp_header *hdr;
  1009. unsigned int ptp_class;
  1010. u8 msgtype;
  1011. /* No need to parse packet if PTP TS is not involved */
  1012. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)))
  1013. goto not_oss;
  1014. /* Identify and return whether PTP one step sync is being processed */
  1015. ptp_class = ptp_classify_raw(skb);
  1016. if (ptp_class == PTP_CLASS_NONE)
  1017. goto not_oss;
  1018. hdr = ptp_parse_header(skb, ptp_class);
  1019. if (!hdr)
  1020. goto not_oss;
  1021. if (hdr->flag_field[0] & PTP_FLAG_TWOSTEP)
  1022. goto not_oss;
  1023. msgtype = ptp_get_msgtype(hdr, ptp_class);
  1024. if (msgtype == PTP_MSGTYPE_SYNC)
  1025. return true;
  1026. not_oss:
  1027. return false;
  1028. }
  1029. static int macb_tx_complete(struct macb_queue *queue, int budget)
  1030. {
  1031. struct macb *bp = queue->bp;
  1032. u16 queue_index = queue - bp->queues;
  1033. unsigned long flags;
  1034. unsigned int tail;
  1035. unsigned int head;
  1036. int packets = 0;
  1037. u32 bytes = 0;
  1038. spin_lock_irqsave(&queue->tx_ptr_lock, flags);
  1039. head = queue->tx_head;
  1040. for (tail = queue->tx_tail; tail != head && packets < budget; tail++) {
  1041. struct macb_tx_skb *tx_skb;
  1042. struct sk_buff *skb;
  1043. struct macb_dma_desc *desc;
  1044. u32 ctrl;
  1045. desc = macb_tx_desc(queue, tail);
  1046. /* Make hw descriptor updates visible to CPU */
  1047. rmb();
  1048. ctrl = desc->ctrl;
  1049. /* TX_USED bit is only set by hardware on the very first buffer
  1050. * descriptor of the transmitted frame.
  1051. */
  1052. if (!(ctrl & MACB_BIT(TX_USED)))
  1053. break;
  1054. /* Process all buffers of the current transmitted frame */
  1055. for (;; tail++) {
  1056. tx_skb = macb_tx_skb(queue, tail);
  1057. skb = tx_skb->skb;
  1058. /* First, update TX stats if needed */
  1059. if (skb) {
  1060. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1061. !ptp_one_step_sync(skb))
  1062. gem_ptp_do_txstamp(bp, skb, desc);
  1063. netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
  1064. macb_tx_ring_wrap(bp, tail),
  1065. skb->data);
  1066. bp->dev->stats.tx_packets++;
  1067. queue->stats.tx_packets++;
  1068. bp->dev->stats.tx_bytes += skb->len;
  1069. queue->stats.tx_bytes += skb->len;
  1070. packets++;
  1071. bytes += skb->len;
  1072. }
  1073. /* Now we can safely release resources */
  1074. macb_tx_unmap(bp, tx_skb, budget);
  1075. /* skb is set only for the last buffer of the frame.
  1076. * WARNING: at this point skb has been freed by
  1077. * macb_tx_unmap().
  1078. */
  1079. if (skb)
  1080. break;
  1081. }
  1082. }
  1083. netdev_tx_completed_queue(netdev_get_tx_queue(bp->dev, queue_index),
  1084. packets, bytes);
  1085. queue->tx_tail = tail;
  1086. if (__netif_subqueue_stopped(bp->dev, queue_index) &&
  1087. CIRC_CNT(queue->tx_head, queue->tx_tail,
  1088. bp->tx_ring_size) <= MACB_TX_WAKEUP_THRESH(bp))
  1089. netif_wake_subqueue(bp->dev, queue_index);
  1090. spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
  1091. return packets;
  1092. }
  1093. static void gem_rx_refill(struct macb_queue *queue)
  1094. {
  1095. unsigned int entry;
  1096. struct sk_buff *skb;
  1097. dma_addr_t paddr;
  1098. struct macb *bp = queue->bp;
  1099. struct macb_dma_desc *desc;
  1100. while (CIRC_SPACE(queue->rx_prepared_head, queue->rx_tail,
  1101. bp->rx_ring_size) > 0) {
  1102. entry = macb_rx_ring_wrap(bp, queue->rx_prepared_head);
  1103. /* Make hw descriptor updates visible to CPU */
  1104. rmb();
  1105. desc = macb_rx_desc(queue, entry);
  1106. if (!queue->rx_skbuff[entry]) {
  1107. /* allocate sk_buff for this free entry in ring */
  1108. skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
  1109. if (unlikely(!skb)) {
  1110. netdev_err(bp->dev,
  1111. "Unable to allocate sk_buff\n");
  1112. break;
  1113. }
  1114. /* now fill corresponding descriptor entry */
  1115. paddr = dma_map_single(&bp->pdev->dev, skb->data,
  1116. bp->rx_buffer_size,
  1117. DMA_FROM_DEVICE);
  1118. if (dma_mapping_error(&bp->pdev->dev, paddr)) {
  1119. dev_kfree_skb(skb);
  1120. break;
  1121. }
  1122. queue->rx_skbuff[entry] = skb;
  1123. if (entry == bp->rx_ring_size - 1)
  1124. paddr |= MACB_BIT(RX_WRAP);
  1125. desc->ctrl = 0;
  1126. /* Setting addr clears RX_USED and allows reception,
  1127. * make sure ctrl is cleared first to avoid a race.
  1128. */
  1129. dma_wmb();
  1130. macb_set_addr(bp, desc, paddr);
  1131. /* Properly align Ethernet header.
  1132. *
  1133. * Hardware can add dummy bytes if asked using the RBOF
  1134. * field inside the NCFGR register. That feature isn't
  1135. * available if hardware is RSC capable.
  1136. *
  1137. * We cannot fallback to doing the 2-byte shift before
  1138. * DMA mapping because the address field does not allow
  1139. * setting the low 2/3 bits.
  1140. * It is 3 bits if HW_DMA_CAP_PTP, else 2 bits.
  1141. */
  1142. if (!(bp->caps & MACB_CAPS_RSC))
  1143. skb_reserve(skb, NET_IP_ALIGN);
  1144. } else {
  1145. desc->ctrl = 0;
  1146. dma_wmb();
  1147. desc->addr &= ~MACB_BIT(RX_USED);
  1148. }
  1149. queue->rx_prepared_head++;
  1150. }
  1151. /* Make descriptor updates visible to hardware */
  1152. wmb();
  1153. netdev_vdbg(bp->dev, "rx ring: queue: %p, prepared head %d, tail %d\n",
  1154. queue, queue->rx_prepared_head, queue->rx_tail);
  1155. }
  1156. /* Mark DMA descriptors from begin up to and not including end as unused */
  1157. static void discard_partial_frame(struct macb_queue *queue, unsigned int begin,
  1158. unsigned int end)
  1159. {
  1160. unsigned int frag;
  1161. for (frag = begin; frag != end; frag++) {
  1162. struct macb_dma_desc *desc = macb_rx_desc(queue, frag);
  1163. desc->addr &= ~MACB_BIT(RX_USED);
  1164. }
  1165. /* Make descriptor updates visible to hardware */
  1166. wmb();
  1167. /* When this happens, the hardware stats registers for
  1168. * whatever caused this is updated, so we don't have to record
  1169. * anything.
  1170. */
  1171. }
  1172. static int gem_rx(struct macb_queue *queue, struct napi_struct *napi,
  1173. int budget)
  1174. {
  1175. struct macb *bp = queue->bp;
  1176. unsigned int len;
  1177. unsigned int entry;
  1178. struct sk_buff *skb;
  1179. struct macb_dma_desc *desc;
  1180. int count = 0;
  1181. while (count < budget) {
  1182. u32 ctrl;
  1183. dma_addr_t addr;
  1184. bool rxused;
  1185. entry = macb_rx_ring_wrap(bp, queue->rx_tail);
  1186. desc = macb_rx_desc(queue, entry);
  1187. /* Make hw descriptor updates visible to CPU */
  1188. rmb();
  1189. rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
  1190. addr = macb_get_addr(bp, desc);
  1191. if (!rxused)
  1192. break;
  1193. /* Ensure ctrl is at least as up-to-date as rxused */
  1194. dma_rmb();
  1195. ctrl = desc->ctrl;
  1196. queue->rx_tail++;
  1197. count++;
  1198. if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
  1199. netdev_err(bp->dev,
  1200. "not whole frame pointed by descriptor\n");
  1201. bp->dev->stats.rx_dropped++;
  1202. queue->stats.rx_dropped++;
  1203. break;
  1204. }
  1205. skb = queue->rx_skbuff[entry];
  1206. if (unlikely(!skb)) {
  1207. netdev_err(bp->dev,
  1208. "inconsistent Rx descriptor chain\n");
  1209. bp->dev->stats.rx_dropped++;
  1210. queue->stats.rx_dropped++;
  1211. break;
  1212. }
  1213. /* now everything is ready for receiving packet */
  1214. queue->rx_skbuff[entry] = NULL;
  1215. len = ctrl & bp->rx_frm_len_mask;
  1216. netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
  1217. skb_put(skb, len);
  1218. dma_unmap_single(&bp->pdev->dev, addr,
  1219. bp->rx_buffer_size, DMA_FROM_DEVICE);
  1220. skb->protocol = eth_type_trans(skb, bp->dev);
  1221. skb_checksum_none_assert(skb);
  1222. if (bp->dev->features & NETIF_F_RXCSUM &&
  1223. !(bp->dev->flags & IFF_PROMISC) &&
  1224. GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
  1225. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1226. bp->dev->stats.rx_packets++;
  1227. queue->stats.rx_packets++;
  1228. bp->dev->stats.rx_bytes += skb->len;
  1229. queue->stats.rx_bytes += skb->len;
  1230. gem_ptp_do_rxstamp(bp, skb, desc);
  1231. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1232. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  1233. skb->len, skb->csum);
  1234. print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
  1235. skb_mac_header(skb), 16, true);
  1236. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
  1237. skb->data, 32, true);
  1238. #endif
  1239. napi_gro_receive(napi, skb);
  1240. }
  1241. gem_rx_refill(queue);
  1242. return count;
  1243. }
  1244. static int macb_rx_frame(struct macb_queue *queue, struct napi_struct *napi,
  1245. unsigned int first_frag, unsigned int last_frag)
  1246. {
  1247. unsigned int len;
  1248. unsigned int frag;
  1249. unsigned int offset;
  1250. struct sk_buff *skb;
  1251. struct macb_dma_desc *desc;
  1252. struct macb *bp = queue->bp;
  1253. desc = macb_rx_desc(queue, last_frag);
  1254. len = desc->ctrl & bp->rx_frm_len_mask;
  1255. netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
  1256. macb_rx_ring_wrap(bp, first_frag),
  1257. macb_rx_ring_wrap(bp, last_frag), len);
  1258. /* The ethernet header starts NET_IP_ALIGN bytes into the
  1259. * first buffer. Since the header is 14 bytes, this makes the
  1260. * payload word-aligned.
  1261. *
  1262. * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
  1263. * the two padding bytes into the skb so that we avoid hitting
  1264. * the slowpath in memcpy(), and pull them off afterwards.
  1265. */
  1266. skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
  1267. if (!skb) {
  1268. bp->dev->stats.rx_dropped++;
  1269. for (frag = first_frag; ; frag++) {
  1270. desc = macb_rx_desc(queue, frag);
  1271. desc->addr &= ~MACB_BIT(RX_USED);
  1272. if (frag == last_frag)
  1273. break;
  1274. }
  1275. /* Make descriptor updates visible to hardware */
  1276. wmb();
  1277. return 1;
  1278. }
  1279. offset = 0;
  1280. len += NET_IP_ALIGN;
  1281. skb_checksum_none_assert(skb);
  1282. skb_put(skb, len);
  1283. for (frag = first_frag; ; frag++) {
  1284. unsigned int frag_len = bp->rx_buffer_size;
  1285. if (offset + frag_len > len) {
  1286. if (unlikely(frag != last_frag)) {
  1287. dev_kfree_skb_any(skb);
  1288. return -1;
  1289. }
  1290. frag_len = len - offset;
  1291. }
  1292. skb_copy_to_linear_data_offset(skb, offset,
  1293. macb_rx_buffer(queue, frag),
  1294. frag_len);
  1295. offset += bp->rx_buffer_size;
  1296. desc = macb_rx_desc(queue, frag);
  1297. desc->addr &= ~MACB_BIT(RX_USED);
  1298. if (frag == last_frag)
  1299. break;
  1300. }
  1301. /* Make descriptor updates visible to hardware */
  1302. wmb();
  1303. __skb_pull(skb, NET_IP_ALIGN);
  1304. skb->protocol = eth_type_trans(skb, bp->dev);
  1305. bp->dev->stats.rx_packets++;
  1306. bp->dev->stats.rx_bytes += skb->len;
  1307. netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
  1308. skb->len, skb->csum);
  1309. napi_gro_receive(napi, skb);
  1310. return 0;
  1311. }
  1312. static inline void macb_init_rx_ring(struct macb_queue *queue)
  1313. {
  1314. struct macb *bp = queue->bp;
  1315. dma_addr_t addr;
  1316. struct macb_dma_desc *desc = NULL;
  1317. int i;
  1318. addr = queue->rx_buffers_dma;
  1319. for (i = 0; i < bp->rx_ring_size; i++) {
  1320. desc = macb_rx_desc(queue, i);
  1321. macb_set_addr(bp, desc, addr);
  1322. desc->ctrl = 0;
  1323. addr += bp->rx_buffer_size;
  1324. }
  1325. desc->addr |= MACB_BIT(RX_WRAP);
  1326. queue->rx_tail = 0;
  1327. }
  1328. static int macb_rx(struct macb_queue *queue, struct napi_struct *napi,
  1329. int budget)
  1330. {
  1331. struct macb *bp = queue->bp;
  1332. bool reset_rx_queue = false;
  1333. int received = 0;
  1334. unsigned int tail;
  1335. int first_frag = -1;
  1336. for (tail = queue->rx_tail; budget > 0; tail++) {
  1337. struct macb_dma_desc *desc = macb_rx_desc(queue, tail);
  1338. u32 ctrl;
  1339. /* Make hw descriptor updates visible to CPU */
  1340. rmb();
  1341. if (!(desc->addr & MACB_BIT(RX_USED)))
  1342. break;
  1343. /* Ensure ctrl is at least as up-to-date as addr */
  1344. dma_rmb();
  1345. ctrl = desc->ctrl;
  1346. if (ctrl & MACB_BIT(RX_SOF)) {
  1347. if (first_frag != -1)
  1348. discard_partial_frame(queue, first_frag, tail);
  1349. first_frag = tail;
  1350. }
  1351. if (ctrl & MACB_BIT(RX_EOF)) {
  1352. int dropped;
  1353. if (unlikely(first_frag == -1)) {
  1354. reset_rx_queue = true;
  1355. continue;
  1356. }
  1357. dropped = macb_rx_frame(queue, napi, first_frag, tail);
  1358. first_frag = -1;
  1359. if (unlikely(dropped < 0)) {
  1360. reset_rx_queue = true;
  1361. continue;
  1362. }
  1363. if (!dropped) {
  1364. received++;
  1365. budget--;
  1366. }
  1367. }
  1368. }
  1369. if (unlikely(reset_rx_queue)) {
  1370. unsigned long flags;
  1371. u32 ctrl;
  1372. netdev_err(bp->dev, "RX queue corruption: reset it\n");
  1373. spin_lock_irqsave(&bp->lock, flags);
  1374. ctrl = macb_readl(bp, NCR);
  1375. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1376. macb_init_rx_ring(queue);
  1377. queue_writel(queue, RBQP, queue->rx_ring_dma);
  1378. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1379. spin_unlock_irqrestore(&bp->lock, flags);
  1380. return received;
  1381. }
  1382. if (first_frag != -1)
  1383. queue->rx_tail = first_frag;
  1384. else
  1385. queue->rx_tail = tail;
  1386. return received;
  1387. }
  1388. static bool macb_rx_pending(struct macb_queue *queue)
  1389. {
  1390. struct macb *bp = queue->bp;
  1391. unsigned int entry;
  1392. struct macb_dma_desc *desc;
  1393. entry = macb_rx_ring_wrap(bp, queue->rx_tail);
  1394. desc = macb_rx_desc(queue, entry);
  1395. /* Make hw descriptor updates visible to CPU */
  1396. rmb();
  1397. return (desc->addr & MACB_BIT(RX_USED)) != 0;
  1398. }
  1399. static int macb_rx_poll(struct napi_struct *napi, int budget)
  1400. {
  1401. struct macb_queue *queue = container_of(napi, struct macb_queue, napi_rx);
  1402. struct macb *bp = queue->bp;
  1403. int work_done;
  1404. work_done = bp->macbgem_ops.mog_rx(queue, napi, budget);
  1405. netdev_vdbg(bp->dev, "RX poll: queue = %u, work_done = %d, budget = %d\n",
  1406. (unsigned int)(queue - bp->queues), work_done, budget);
  1407. if (work_done < budget && napi_complete_done(napi, work_done)) {
  1408. queue_writel(queue, IER, bp->rx_intr_mask);
  1409. /* Packet completions only seem to propagate to raise
  1410. * interrupts when interrupts are enabled at the time, so if
  1411. * packets were received while interrupts were disabled,
  1412. * they will not cause another interrupt to be generated when
  1413. * interrupts are re-enabled.
  1414. * Check for this case here to avoid losing a wakeup. This can
  1415. * potentially race with the interrupt handler doing the same
  1416. * actions if an interrupt is raised just after enabling them,
  1417. * but this should be harmless.
  1418. */
  1419. if (macb_rx_pending(queue)) {
  1420. queue_writel(queue, IDR, bp->rx_intr_mask);
  1421. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1422. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1423. netdev_vdbg(bp->dev, "poll: packets pending, reschedule\n");
  1424. napi_schedule(napi);
  1425. }
  1426. }
  1427. /* TODO: Handle errors */
  1428. return work_done;
  1429. }
  1430. static void macb_tx_restart(struct macb_queue *queue)
  1431. {
  1432. struct macb *bp = queue->bp;
  1433. unsigned int head_idx, tbqp;
  1434. unsigned long flags;
  1435. spin_lock_irqsave(&queue->tx_ptr_lock, flags);
  1436. if (queue->tx_head == queue->tx_tail)
  1437. goto out_tx_ptr_unlock;
  1438. tbqp = queue_readl(queue, TBQP) / macb_dma_desc_get_size(bp);
  1439. tbqp = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, tbqp));
  1440. head_idx = macb_adj_dma_desc_idx(bp, macb_tx_ring_wrap(bp, queue->tx_head));
  1441. if (tbqp == head_idx)
  1442. goto out_tx_ptr_unlock;
  1443. spin_lock(&bp->lock);
  1444. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1445. spin_unlock(&bp->lock);
  1446. out_tx_ptr_unlock:
  1447. spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
  1448. }
  1449. static bool macb_tx_complete_pending(struct macb_queue *queue)
  1450. {
  1451. bool retval = false;
  1452. unsigned long flags;
  1453. spin_lock_irqsave(&queue->tx_ptr_lock, flags);
  1454. if (queue->tx_head != queue->tx_tail) {
  1455. /* Make hw descriptor updates visible to CPU */
  1456. rmb();
  1457. if (macb_tx_desc(queue, queue->tx_tail)->ctrl & MACB_BIT(TX_USED))
  1458. retval = true;
  1459. }
  1460. spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
  1461. return retval;
  1462. }
  1463. static int macb_tx_poll(struct napi_struct *napi, int budget)
  1464. {
  1465. struct macb_queue *queue = container_of(napi, struct macb_queue, napi_tx);
  1466. struct macb *bp = queue->bp;
  1467. int work_done;
  1468. work_done = macb_tx_complete(queue, budget);
  1469. rmb(); // ensure txubr_pending is up to date
  1470. if (queue->txubr_pending) {
  1471. queue->txubr_pending = false;
  1472. netdev_vdbg(bp->dev, "poll: tx restart\n");
  1473. macb_tx_restart(queue);
  1474. }
  1475. netdev_vdbg(bp->dev, "TX poll: queue = %u, work_done = %d, budget = %d\n",
  1476. (unsigned int)(queue - bp->queues), work_done, budget);
  1477. if (work_done < budget && napi_complete_done(napi, work_done)) {
  1478. queue_writel(queue, IER, MACB_BIT(TCOMP));
  1479. /* Packet completions only seem to propagate to raise
  1480. * interrupts when interrupts are enabled at the time, so if
  1481. * packets were sent while interrupts were disabled,
  1482. * they will not cause another interrupt to be generated when
  1483. * interrupts are re-enabled.
  1484. * Check for this case here to avoid losing a wakeup. This can
  1485. * potentially race with the interrupt handler doing the same
  1486. * actions if an interrupt is raised just after enabling them,
  1487. * but this should be harmless.
  1488. */
  1489. if (macb_tx_complete_pending(queue)) {
  1490. queue_writel(queue, IDR, MACB_BIT(TCOMP));
  1491. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1492. queue_writel(queue, ISR, MACB_BIT(TCOMP));
  1493. netdev_vdbg(bp->dev, "TX poll: packets pending, reschedule\n");
  1494. napi_schedule(napi);
  1495. }
  1496. }
  1497. return work_done;
  1498. }
  1499. static void macb_hresp_error_task(struct work_struct *work)
  1500. {
  1501. struct macb *bp = from_work(bp, work, hresp_err_bh_work);
  1502. struct net_device *dev = bp->dev;
  1503. struct macb_queue *queue;
  1504. unsigned int q;
  1505. u32 ctrl;
  1506. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  1507. queue_writel(queue, IDR, bp->rx_intr_mask |
  1508. MACB_TX_INT_FLAGS |
  1509. MACB_BIT(HRESP));
  1510. }
  1511. ctrl = macb_readl(bp, NCR);
  1512. ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
  1513. macb_writel(bp, NCR, ctrl);
  1514. netif_tx_stop_all_queues(dev);
  1515. netif_carrier_off(dev);
  1516. bp->macbgem_ops.mog_init_rings(bp);
  1517. /* Initialize TX and RX buffers */
  1518. macb_init_buffers(bp);
  1519. /* Enable interrupts */
  1520. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1521. queue_writel(queue, IER,
  1522. bp->rx_intr_mask |
  1523. MACB_TX_INT_FLAGS |
  1524. MACB_BIT(HRESP));
  1525. ctrl |= MACB_BIT(RE) | MACB_BIT(TE);
  1526. macb_writel(bp, NCR, ctrl);
  1527. netif_carrier_on(dev);
  1528. netif_tx_start_all_queues(dev);
  1529. }
  1530. static irqreturn_t macb_wol_interrupt(int irq, void *dev_id)
  1531. {
  1532. struct macb_queue *queue = dev_id;
  1533. struct macb *bp = queue->bp;
  1534. u32 status;
  1535. status = queue_readl(queue, ISR);
  1536. if (unlikely(!status))
  1537. return IRQ_NONE;
  1538. spin_lock(&bp->lock);
  1539. if (status & MACB_BIT(WOL)) {
  1540. queue_writel(queue, IDR, MACB_BIT(WOL));
  1541. macb_writel(bp, WOL, 0);
  1542. netdev_vdbg(bp->dev, "MACB WoL: queue = %u, isr = 0x%08lx\n",
  1543. (unsigned int)(queue - bp->queues),
  1544. (unsigned long)status);
  1545. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1546. queue_writel(queue, ISR, MACB_BIT(WOL));
  1547. pm_wakeup_event(&bp->pdev->dev, 0);
  1548. }
  1549. spin_unlock(&bp->lock);
  1550. return IRQ_HANDLED;
  1551. }
  1552. static irqreturn_t gem_wol_interrupt(int irq, void *dev_id)
  1553. {
  1554. struct macb_queue *queue = dev_id;
  1555. struct macb *bp = queue->bp;
  1556. u32 status;
  1557. status = queue_readl(queue, ISR);
  1558. if (unlikely(!status))
  1559. return IRQ_NONE;
  1560. spin_lock(&bp->lock);
  1561. if (status & GEM_BIT(WOL)) {
  1562. queue_writel(queue, IDR, GEM_BIT(WOL));
  1563. gem_writel(bp, WOL, 0);
  1564. netdev_vdbg(bp->dev, "GEM WoL: queue = %u, isr = 0x%08lx\n",
  1565. (unsigned int)(queue - bp->queues),
  1566. (unsigned long)status);
  1567. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1568. queue_writel(queue, ISR, GEM_BIT(WOL));
  1569. pm_wakeup_event(&bp->pdev->dev, 0);
  1570. }
  1571. spin_unlock(&bp->lock);
  1572. return IRQ_HANDLED;
  1573. }
  1574. static irqreturn_t macb_interrupt(int irq, void *dev_id)
  1575. {
  1576. struct macb_queue *queue = dev_id;
  1577. struct macb *bp = queue->bp;
  1578. struct net_device *dev = bp->dev;
  1579. u32 status, ctrl;
  1580. status = queue_readl(queue, ISR);
  1581. if (unlikely(!status))
  1582. return IRQ_NONE;
  1583. spin_lock(&bp->lock);
  1584. while (status) {
  1585. /* close possible race with dev_close */
  1586. if (unlikely(!netif_running(dev))) {
  1587. queue_writel(queue, IDR, -1);
  1588. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1589. queue_writel(queue, ISR, -1);
  1590. break;
  1591. }
  1592. netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
  1593. (unsigned int)(queue - bp->queues),
  1594. (unsigned long)status);
  1595. if (status & bp->rx_intr_mask) {
  1596. /* There's no point taking any more interrupts
  1597. * until we have processed the buffers. The
  1598. * scheduling call may fail if the poll routine
  1599. * is already scheduled, so disable interrupts
  1600. * now.
  1601. */
  1602. queue_writel(queue, IDR, bp->rx_intr_mask);
  1603. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1604. queue_writel(queue, ISR, MACB_BIT(RCOMP));
  1605. if (napi_schedule_prep(&queue->napi_rx)) {
  1606. netdev_vdbg(bp->dev, "scheduling RX softirq\n");
  1607. __napi_schedule(&queue->napi_rx);
  1608. }
  1609. }
  1610. if (status & (MACB_BIT(TCOMP) |
  1611. MACB_BIT(TXUBR))) {
  1612. queue_writel(queue, IDR, MACB_BIT(TCOMP));
  1613. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1614. queue_writel(queue, ISR, MACB_BIT(TCOMP) |
  1615. MACB_BIT(TXUBR));
  1616. if (status & MACB_BIT(TXUBR)) {
  1617. queue->txubr_pending = true;
  1618. wmb(); // ensure softirq can see update
  1619. }
  1620. if (napi_schedule_prep(&queue->napi_tx)) {
  1621. netdev_vdbg(bp->dev, "scheduling TX softirq\n");
  1622. __napi_schedule(&queue->napi_tx);
  1623. }
  1624. }
  1625. if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
  1626. queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
  1627. schedule_work(&queue->tx_error_task);
  1628. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1629. queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
  1630. break;
  1631. }
  1632. /* Link change detection isn't possible with RMII, so we'll
  1633. * add that if/when we get our hands on a full-blown MII PHY.
  1634. */
  1635. /* There is a hardware issue under heavy load where DMA can
  1636. * stop, this causes endless "used buffer descriptor read"
  1637. * interrupts but it can be cleared by re-enabling RX. See
  1638. * the at91rm9200 manual, section 41.3.1 or the Zynq manual
  1639. * section 16.7.4 for details. RXUBR is only enabled for
  1640. * these two versions.
  1641. */
  1642. if (status & MACB_BIT(RXUBR)) {
  1643. ctrl = macb_readl(bp, NCR);
  1644. macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
  1645. wmb();
  1646. macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
  1647. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1648. queue_writel(queue, ISR, MACB_BIT(RXUBR));
  1649. }
  1650. if (status & MACB_BIT(ISR_ROVR)) {
  1651. /* We missed at least one packet */
  1652. spin_lock(&bp->stats_lock);
  1653. if (macb_is_gem(bp))
  1654. bp->hw_stats.gem.rx_overruns++;
  1655. else
  1656. bp->hw_stats.macb.rx_overruns++;
  1657. spin_unlock(&bp->stats_lock);
  1658. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1659. queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
  1660. }
  1661. if (status & MACB_BIT(HRESP)) {
  1662. queue_work(system_bh_wq, &bp->hresp_err_bh_work);
  1663. netdev_err(dev, "DMA bus error: HRESP not OK\n");
  1664. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  1665. queue_writel(queue, ISR, MACB_BIT(HRESP));
  1666. }
  1667. status = queue_readl(queue, ISR);
  1668. }
  1669. spin_unlock(&bp->lock);
  1670. return IRQ_HANDLED;
  1671. }
  1672. #ifdef CONFIG_NET_POLL_CONTROLLER
  1673. /* Polling receive - used by netconsole and other diagnostic tools
  1674. * to allow network i/o with interrupts disabled.
  1675. */
  1676. static void macb_poll_controller(struct net_device *dev)
  1677. {
  1678. struct macb *bp = netdev_priv(dev);
  1679. struct macb_queue *queue;
  1680. unsigned long flags;
  1681. unsigned int q;
  1682. local_irq_save(flags);
  1683. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  1684. macb_interrupt(dev->irq, queue);
  1685. local_irq_restore(flags);
  1686. }
  1687. #endif
  1688. static unsigned int macb_tx_map(struct macb *bp,
  1689. struct macb_queue *queue,
  1690. struct sk_buff *skb,
  1691. unsigned int hdrlen)
  1692. {
  1693. unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
  1694. unsigned int len, i, tx_head = queue->tx_head;
  1695. u32 ctrl, lso_ctrl = 0, seq_ctrl = 0;
  1696. unsigned int eof = 1, mss_mfs = 0;
  1697. struct macb_tx_skb *tx_skb = NULL;
  1698. struct macb_dma_desc *desc;
  1699. unsigned int offset, size;
  1700. dma_addr_t mapping;
  1701. /* LSO */
  1702. if (skb_shinfo(skb)->gso_size != 0) {
  1703. if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  1704. /* UDP - UFO */
  1705. lso_ctrl = MACB_LSO_UFO_ENABLE;
  1706. else
  1707. /* TCP - TSO */
  1708. lso_ctrl = MACB_LSO_TSO_ENABLE;
  1709. }
  1710. /* First, map non-paged data */
  1711. len = skb_headlen(skb);
  1712. /* first buffer length */
  1713. size = hdrlen;
  1714. offset = 0;
  1715. while (len) {
  1716. tx_skb = macb_tx_skb(queue, tx_head);
  1717. mapping = dma_map_single(&bp->pdev->dev,
  1718. skb->data + offset,
  1719. size, DMA_TO_DEVICE);
  1720. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1721. goto dma_error;
  1722. /* Save info to properly release resources */
  1723. tx_skb->skb = NULL;
  1724. tx_skb->mapping = mapping;
  1725. tx_skb->size = size;
  1726. tx_skb->mapped_as_page = false;
  1727. len -= size;
  1728. offset += size;
  1729. tx_head++;
  1730. size = umin(len, bp->max_tx_length);
  1731. }
  1732. /* Then, map paged data from fragments */
  1733. for (f = 0; f < nr_frags; f++) {
  1734. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1735. len = skb_frag_size(frag);
  1736. offset = 0;
  1737. while (len) {
  1738. size = umin(len, bp->max_tx_length);
  1739. tx_skb = macb_tx_skb(queue, tx_head);
  1740. mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
  1741. offset, size, DMA_TO_DEVICE);
  1742. if (dma_mapping_error(&bp->pdev->dev, mapping))
  1743. goto dma_error;
  1744. /* Save info to properly release resources */
  1745. tx_skb->skb = NULL;
  1746. tx_skb->mapping = mapping;
  1747. tx_skb->size = size;
  1748. tx_skb->mapped_as_page = true;
  1749. len -= size;
  1750. offset += size;
  1751. tx_head++;
  1752. }
  1753. }
  1754. /* Should never happen */
  1755. if (unlikely(!tx_skb)) {
  1756. netdev_err(bp->dev, "BUG! empty skb!\n");
  1757. return 0;
  1758. }
  1759. /* This is the last buffer of the frame: save socket buffer */
  1760. tx_skb->skb = skb;
  1761. /* Update TX ring: update buffer descriptors in reverse order
  1762. * to avoid race condition
  1763. */
  1764. /* Set 'TX_USED' bit in buffer descriptor at tx_head position
  1765. * to set the end of TX queue
  1766. */
  1767. i = tx_head;
  1768. ctrl = MACB_BIT(TX_USED);
  1769. desc = macb_tx_desc(queue, i);
  1770. desc->ctrl = ctrl;
  1771. if (lso_ctrl) {
  1772. if (lso_ctrl == MACB_LSO_UFO_ENABLE)
  1773. /* include header and FCS in value given to h/w */
  1774. mss_mfs = skb_shinfo(skb)->gso_size +
  1775. skb_transport_offset(skb) +
  1776. ETH_FCS_LEN;
  1777. else /* TSO */ {
  1778. mss_mfs = skb_shinfo(skb)->gso_size;
  1779. /* TCP Sequence Number Source Select
  1780. * can be set only for TSO
  1781. */
  1782. seq_ctrl = 0;
  1783. }
  1784. }
  1785. do {
  1786. i--;
  1787. tx_skb = macb_tx_skb(queue, i);
  1788. desc = macb_tx_desc(queue, i);
  1789. ctrl = (u32)tx_skb->size;
  1790. if (eof) {
  1791. ctrl |= MACB_BIT(TX_LAST);
  1792. eof = 0;
  1793. }
  1794. if (unlikely(macb_tx_ring_wrap(bp, i) == bp->tx_ring_size - 1))
  1795. ctrl |= MACB_BIT(TX_WRAP);
  1796. /* First descriptor is header descriptor */
  1797. if (i == queue->tx_head) {
  1798. ctrl |= MACB_BF(TX_LSO, lso_ctrl);
  1799. ctrl |= MACB_BF(TX_TCP_SEQ_SRC, seq_ctrl);
  1800. if ((bp->dev->features & NETIF_F_HW_CSUM) &&
  1801. skb->ip_summed != CHECKSUM_PARTIAL && !lso_ctrl &&
  1802. !ptp_one_step_sync(skb))
  1803. ctrl |= MACB_BIT(TX_NOCRC);
  1804. } else
  1805. /* Only set MSS/MFS on payload descriptors
  1806. * (second or later descriptor)
  1807. */
  1808. ctrl |= MACB_BF(MSS_MFS, mss_mfs);
  1809. /* Set TX buffer descriptor */
  1810. macb_set_addr(bp, desc, tx_skb->mapping);
  1811. /* desc->addr must be visible to hardware before clearing
  1812. * 'TX_USED' bit in desc->ctrl.
  1813. */
  1814. wmb();
  1815. desc->ctrl = ctrl;
  1816. } while (i != queue->tx_head);
  1817. queue->tx_head = tx_head;
  1818. return 0;
  1819. dma_error:
  1820. netdev_err(bp->dev, "TX DMA map failed\n");
  1821. for (i = queue->tx_head; i != tx_head; i++) {
  1822. tx_skb = macb_tx_skb(queue, i);
  1823. macb_tx_unmap(bp, tx_skb, 0);
  1824. }
  1825. return -ENOMEM;
  1826. }
  1827. static netdev_features_t macb_features_check(struct sk_buff *skb,
  1828. struct net_device *dev,
  1829. netdev_features_t features)
  1830. {
  1831. unsigned int nr_frags, f;
  1832. unsigned int hdrlen;
  1833. /* Validate LSO compatibility */
  1834. /* there is only one buffer or protocol is not UDP */
  1835. if (!skb_is_nonlinear(skb) || (ip_hdr(skb)->protocol != IPPROTO_UDP))
  1836. return features;
  1837. /* length of header */
  1838. hdrlen = skb_transport_offset(skb);
  1839. /* For UFO only:
  1840. * When software supplies two or more payload buffers all payload buffers
  1841. * apart from the last must be a multiple of 8 bytes in size.
  1842. */
  1843. if (!IS_ALIGNED(skb_headlen(skb) - hdrlen, MACB_TX_LEN_ALIGN))
  1844. return features & ~MACB_NETIF_LSO;
  1845. nr_frags = skb_shinfo(skb)->nr_frags;
  1846. /* No need to check last fragment */
  1847. nr_frags--;
  1848. for (f = 0; f < nr_frags; f++) {
  1849. const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
  1850. if (!IS_ALIGNED(skb_frag_size(frag), MACB_TX_LEN_ALIGN))
  1851. return features & ~MACB_NETIF_LSO;
  1852. }
  1853. return features;
  1854. }
  1855. static inline int macb_clear_csum(struct sk_buff *skb)
  1856. {
  1857. /* no change for packets without checksum offloading */
  1858. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1859. return 0;
  1860. /* make sure we can modify the header */
  1861. if (unlikely(skb_cow_head(skb, 0)))
  1862. return -1;
  1863. /* initialize checksum field
  1864. * This is required - at least for Zynq, which otherwise calculates
  1865. * wrong UDP header checksums for UDP packets with UDP data len <=2
  1866. */
  1867. *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
  1868. return 0;
  1869. }
  1870. static int macb_pad_and_fcs(struct sk_buff **skb, struct net_device *ndev)
  1871. {
  1872. bool cloned = skb_cloned(*skb) || skb_header_cloned(*skb) ||
  1873. skb_is_nonlinear(*skb);
  1874. int padlen = ETH_ZLEN - (*skb)->len;
  1875. int tailroom = skb_tailroom(*skb);
  1876. struct sk_buff *nskb;
  1877. u32 fcs;
  1878. if (!(ndev->features & NETIF_F_HW_CSUM) ||
  1879. !((*skb)->ip_summed != CHECKSUM_PARTIAL) ||
  1880. skb_shinfo(*skb)->gso_size || ptp_one_step_sync(*skb))
  1881. return 0;
  1882. if (padlen <= 0) {
  1883. /* FCS could be appeded to tailroom. */
  1884. if (tailroom >= ETH_FCS_LEN)
  1885. goto add_fcs;
  1886. /* No room for FCS, need to reallocate skb. */
  1887. else
  1888. padlen = ETH_FCS_LEN;
  1889. } else {
  1890. /* Add room for FCS. */
  1891. padlen += ETH_FCS_LEN;
  1892. }
  1893. if (cloned || tailroom < padlen) {
  1894. nskb = skb_copy_expand(*skb, 0, padlen, GFP_ATOMIC);
  1895. if (!nskb)
  1896. return -ENOMEM;
  1897. dev_consume_skb_any(*skb);
  1898. *skb = nskb;
  1899. }
  1900. if (padlen > ETH_FCS_LEN)
  1901. skb_put_zero(*skb, padlen - ETH_FCS_LEN);
  1902. add_fcs:
  1903. /* set FCS to packet */
  1904. fcs = crc32_le(~0, (*skb)->data, (*skb)->len);
  1905. fcs = ~fcs;
  1906. skb_put_u8(*skb, fcs & 0xff);
  1907. skb_put_u8(*skb, (fcs >> 8) & 0xff);
  1908. skb_put_u8(*skb, (fcs >> 16) & 0xff);
  1909. skb_put_u8(*skb, (fcs >> 24) & 0xff);
  1910. return 0;
  1911. }
  1912. static netdev_tx_t macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1913. {
  1914. u16 queue_index = skb_get_queue_mapping(skb);
  1915. struct macb *bp = netdev_priv(dev);
  1916. struct macb_queue *queue = &bp->queues[queue_index];
  1917. unsigned int desc_cnt, nr_frags, frag_size, f;
  1918. unsigned int hdrlen;
  1919. unsigned long flags;
  1920. bool is_lso;
  1921. netdev_tx_t ret = NETDEV_TX_OK;
  1922. if (macb_clear_csum(skb)) {
  1923. dev_kfree_skb_any(skb);
  1924. return ret;
  1925. }
  1926. if (macb_pad_and_fcs(&skb, dev)) {
  1927. dev_kfree_skb_any(skb);
  1928. return ret;
  1929. }
  1930. if (macb_dma_ptp(bp) &&
  1931. (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
  1932. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1933. is_lso = (skb_shinfo(skb)->gso_size != 0);
  1934. if (is_lso) {
  1935. /* length of headers */
  1936. if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  1937. /* only queue eth + ip headers separately for UDP */
  1938. hdrlen = skb_transport_offset(skb);
  1939. else
  1940. hdrlen = skb_tcp_all_headers(skb);
  1941. if (skb_headlen(skb) < hdrlen) {
  1942. netdev_err(bp->dev, "Error - LSO headers fragmented!!!\n");
  1943. /* if this is required, would need to copy to single buffer */
  1944. return NETDEV_TX_BUSY;
  1945. }
  1946. } else
  1947. hdrlen = umin(skb_headlen(skb), bp->max_tx_length);
  1948. #if defined(DEBUG) && defined(VERBOSE_DEBUG)
  1949. netdev_vdbg(bp->dev,
  1950. "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
  1951. queue_index, skb->len, skb->head, skb->data,
  1952. skb_tail_pointer(skb), skb_end_pointer(skb));
  1953. print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
  1954. skb->data, 16, true);
  1955. #endif
  1956. /* Count how many TX buffer descriptors are needed to send this
  1957. * socket buffer: skb fragments of jumbo frames may need to be
  1958. * split into many buffer descriptors.
  1959. */
  1960. if (is_lso && (skb_headlen(skb) > hdrlen))
  1961. /* extra header descriptor if also payload in first buffer */
  1962. desc_cnt = DIV_ROUND_UP((skb_headlen(skb) - hdrlen), bp->max_tx_length) + 1;
  1963. else
  1964. desc_cnt = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
  1965. nr_frags = skb_shinfo(skb)->nr_frags;
  1966. for (f = 0; f < nr_frags; f++) {
  1967. frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
  1968. desc_cnt += DIV_ROUND_UP(frag_size, bp->max_tx_length);
  1969. }
  1970. spin_lock_irqsave(&queue->tx_ptr_lock, flags);
  1971. /* This is a hard error, log it. */
  1972. if (CIRC_SPACE(queue->tx_head, queue->tx_tail,
  1973. bp->tx_ring_size) < desc_cnt) {
  1974. netif_stop_subqueue(dev, queue_index);
  1975. netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
  1976. queue->tx_head, queue->tx_tail);
  1977. ret = NETDEV_TX_BUSY;
  1978. goto unlock;
  1979. }
  1980. /* Map socket buffer for DMA transfer */
  1981. if (macb_tx_map(bp, queue, skb, hdrlen)) {
  1982. dev_kfree_skb_any(skb);
  1983. goto unlock;
  1984. }
  1985. /* Make newly initialized descriptor visible to hardware */
  1986. wmb();
  1987. skb_tx_timestamp(skb);
  1988. netdev_tx_sent_queue(netdev_get_tx_queue(bp->dev, queue_index),
  1989. skb->len);
  1990. spin_lock(&bp->lock);
  1991. macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
  1992. spin_unlock(&bp->lock);
  1993. if (CIRC_SPACE(queue->tx_head, queue->tx_tail, bp->tx_ring_size) < 1)
  1994. netif_stop_subqueue(dev, queue_index);
  1995. unlock:
  1996. spin_unlock_irqrestore(&queue->tx_ptr_lock, flags);
  1997. return ret;
  1998. }
  1999. static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
  2000. {
  2001. if (!macb_is_gem(bp)) {
  2002. bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
  2003. } else {
  2004. bp->rx_buffer_size = size;
  2005. if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
  2006. netdev_dbg(bp->dev,
  2007. "RX buffer must be multiple of %d bytes, expanding\n",
  2008. RX_BUFFER_MULTIPLE);
  2009. bp->rx_buffer_size =
  2010. roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
  2011. }
  2012. }
  2013. netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%zu]\n",
  2014. bp->dev->mtu, bp->rx_buffer_size);
  2015. }
  2016. static void gem_free_rx_buffers(struct macb *bp)
  2017. {
  2018. struct sk_buff *skb;
  2019. struct macb_dma_desc *desc;
  2020. struct macb_queue *queue;
  2021. dma_addr_t addr;
  2022. unsigned int q;
  2023. int i;
  2024. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2025. if (!queue->rx_skbuff)
  2026. continue;
  2027. for (i = 0; i < bp->rx_ring_size; i++) {
  2028. skb = queue->rx_skbuff[i];
  2029. if (!skb)
  2030. continue;
  2031. desc = macb_rx_desc(queue, i);
  2032. addr = macb_get_addr(bp, desc);
  2033. dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
  2034. DMA_FROM_DEVICE);
  2035. dev_kfree_skb_any(skb);
  2036. skb = NULL;
  2037. }
  2038. kfree(queue->rx_skbuff);
  2039. queue->rx_skbuff = NULL;
  2040. }
  2041. }
  2042. static void macb_free_rx_buffers(struct macb *bp)
  2043. {
  2044. struct macb_queue *queue = &bp->queues[0];
  2045. if (queue->rx_buffers) {
  2046. dma_free_coherent(&bp->pdev->dev,
  2047. bp->rx_ring_size * bp->rx_buffer_size,
  2048. queue->rx_buffers, queue->rx_buffers_dma);
  2049. queue->rx_buffers = NULL;
  2050. }
  2051. }
  2052. static unsigned int macb_tx_ring_size_per_queue(struct macb *bp)
  2053. {
  2054. return macb_dma_desc_get_size(bp) * bp->tx_ring_size + bp->tx_bd_rd_prefetch;
  2055. }
  2056. static unsigned int macb_rx_ring_size_per_queue(struct macb *bp)
  2057. {
  2058. return macb_dma_desc_get_size(bp) * bp->rx_ring_size + bp->rx_bd_rd_prefetch;
  2059. }
  2060. static void macb_free_consistent(struct macb *bp)
  2061. {
  2062. struct device *dev = &bp->pdev->dev;
  2063. struct macb_queue *queue;
  2064. unsigned int q;
  2065. size_t size;
  2066. if (bp->rx_ring_tieoff) {
  2067. dma_free_coherent(dev, macb_dma_desc_get_size(bp),
  2068. bp->rx_ring_tieoff, bp->rx_ring_tieoff_dma);
  2069. bp->rx_ring_tieoff = NULL;
  2070. }
  2071. bp->macbgem_ops.mog_free_rx_buffers(bp);
  2072. size = bp->num_queues * macb_tx_ring_size_per_queue(bp);
  2073. dma_free_coherent(dev, size, bp->queues[0].tx_ring, bp->queues[0].tx_ring_dma);
  2074. size = bp->num_queues * macb_rx_ring_size_per_queue(bp);
  2075. dma_free_coherent(dev, size, bp->queues[0].rx_ring, bp->queues[0].rx_ring_dma);
  2076. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2077. kfree(queue->tx_skb);
  2078. queue->tx_skb = NULL;
  2079. queue->tx_ring = NULL;
  2080. queue->rx_ring = NULL;
  2081. }
  2082. }
  2083. static int gem_alloc_rx_buffers(struct macb *bp)
  2084. {
  2085. struct macb_queue *queue;
  2086. unsigned int q;
  2087. int size;
  2088. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2089. size = bp->rx_ring_size * sizeof(struct sk_buff *);
  2090. queue->rx_skbuff = kzalloc(size, GFP_KERNEL);
  2091. if (!queue->rx_skbuff)
  2092. return -ENOMEM;
  2093. else
  2094. netdev_dbg(bp->dev,
  2095. "Allocated %d RX struct sk_buff entries at %p\n",
  2096. bp->rx_ring_size, queue->rx_skbuff);
  2097. }
  2098. return 0;
  2099. }
  2100. static int macb_alloc_rx_buffers(struct macb *bp)
  2101. {
  2102. struct macb_queue *queue = &bp->queues[0];
  2103. int size;
  2104. size = bp->rx_ring_size * bp->rx_buffer_size;
  2105. queue->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
  2106. &queue->rx_buffers_dma, GFP_KERNEL);
  2107. if (!queue->rx_buffers)
  2108. return -ENOMEM;
  2109. netdev_dbg(bp->dev,
  2110. "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
  2111. size, (unsigned long)queue->rx_buffers_dma, queue->rx_buffers);
  2112. return 0;
  2113. }
  2114. static int macb_alloc_consistent(struct macb *bp)
  2115. {
  2116. struct device *dev = &bp->pdev->dev;
  2117. dma_addr_t tx_dma, rx_dma;
  2118. struct macb_queue *queue;
  2119. unsigned int q;
  2120. void *tx, *rx;
  2121. size_t size;
  2122. /*
  2123. * Upper 32-bits of Tx/Rx DMA descriptor for each queues much match!
  2124. * We cannot enforce this guarantee, the best we can do is do a single
  2125. * allocation and hope it will land into alloc_pages() that guarantees
  2126. * natural alignment of physical addresses.
  2127. */
  2128. size = bp->num_queues * macb_tx_ring_size_per_queue(bp);
  2129. tx = dma_alloc_coherent(dev, size, &tx_dma, GFP_KERNEL);
  2130. if (!tx || upper_32_bits(tx_dma) != upper_32_bits(tx_dma + size - 1))
  2131. goto out_err;
  2132. netdev_dbg(bp->dev, "Allocated %zu bytes for %u TX rings at %08lx (mapped %p)\n",
  2133. size, bp->num_queues, (unsigned long)tx_dma, tx);
  2134. size = bp->num_queues * macb_rx_ring_size_per_queue(bp);
  2135. rx = dma_alloc_coherent(dev, size, &rx_dma, GFP_KERNEL);
  2136. if (!rx || upper_32_bits(rx_dma) != upper_32_bits(rx_dma + size - 1))
  2137. goto out_err;
  2138. netdev_dbg(bp->dev, "Allocated %zu bytes for %u RX rings at %08lx (mapped %p)\n",
  2139. size, bp->num_queues, (unsigned long)rx_dma, rx);
  2140. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2141. queue->tx_ring = tx + macb_tx_ring_size_per_queue(bp) * q;
  2142. queue->tx_ring_dma = tx_dma + macb_tx_ring_size_per_queue(bp) * q;
  2143. queue->rx_ring = rx + macb_rx_ring_size_per_queue(bp) * q;
  2144. queue->rx_ring_dma = rx_dma + macb_rx_ring_size_per_queue(bp) * q;
  2145. size = bp->tx_ring_size * sizeof(struct macb_tx_skb);
  2146. queue->tx_skb = kmalloc(size, GFP_KERNEL);
  2147. if (!queue->tx_skb)
  2148. goto out_err;
  2149. }
  2150. if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
  2151. goto out_err;
  2152. /* Required for tie off descriptor for PM cases */
  2153. if (!(bp->caps & MACB_CAPS_QUEUE_DISABLE)) {
  2154. bp->rx_ring_tieoff = dma_alloc_coherent(&bp->pdev->dev,
  2155. macb_dma_desc_get_size(bp),
  2156. &bp->rx_ring_tieoff_dma,
  2157. GFP_KERNEL);
  2158. if (!bp->rx_ring_tieoff)
  2159. goto out_err;
  2160. }
  2161. return 0;
  2162. out_err:
  2163. macb_free_consistent(bp);
  2164. return -ENOMEM;
  2165. }
  2166. static void macb_init_tieoff(struct macb *bp)
  2167. {
  2168. struct macb_dma_desc *desc = bp->rx_ring_tieoff;
  2169. if (bp->caps & MACB_CAPS_QUEUE_DISABLE)
  2170. return;
  2171. /* Setup a wrapping descriptor with no free slots
  2172. * (WRAP and USED) to tie off/disable unused RX queues.
  2173. */
  2174. macb_set_addr(bp, desc, MACB_BIT(RX_WRAP) | MACB_BIT(RX_USED));
  2175. desc->ctrl = 0;
  2176. }
  2177. static void gem_init_rx_ring(struct macb_queue *queue)
  2178. {
  2179. queue->rx_tail = 0;
  2180. queue->rx_prepared_head = 0;
  2181. gem_rx_refill(queue);
  2182. }
  2183. static void gem_init_rings(struct macb *bp)
  2184. {
  2185. struct macb_queue *queue;
  2186. struct macb_dma_desc *desc = NULL;
  2187. unsigned int q;
  2188. int i;
  2189. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2190. for (i = 0; i < bp->tx_ring_size; i++) {
  2191. desc = macb_tx_desc(queue, i);
  2192. macb_set_addr(bp, desc, 0);
  2193. desc->ctrl = MACB_BIT(TX_USED);
  2194. }
  2195. desc->ctrl |= MACB_BIT(TX_WRAP);
  2196. queue->tx_head = 0;
  2197. queue->tx_tail = 0;
  2198. gem_init_rx_ring(queue);
  2199. }
  2200. macb_init_tieoff(bp);
  2201. }
  2202. static void macb_init_rings(struct macb *bp)
  2203. {
  2204. int i;
  2205. struct macb_dma_desc *desc = NULL;
  2206. macb_init_rx_ring(&bp->queues[0]);
  2207. for (i = 0; i < bp->tx_ring_size; i++) {
  2208. desc = macb_tx_desc(&bp->queues[0], i);
  2209. macb_set_addr(bp, desc, 0);
  2210. desc->ctrl = MACB_BIT(TX_USED);
  2211. }
  2212. bp->queues[0].tx_head = 0;
  2213. bp->queues[0].tx_tail = 0;
  2214. desc->ctrl |= MACB_BIT(TX_WRAP);
  2215. macb_init_tieoff(bp);
  2216. }
  2217. static void macb_reset_hw(struct macb *bp)
  2218. {
  2219. struct macb_queue *queue;
  2220. unsigned int q;
  2221. u32 ctrl = macb_readl(bp, NCR);
  2222. /* Disable RX and TX (XXX: Should we halt the transmission
  2223. * more gracefully?)
  2224. */
  2225. ctrl &= ~(MACB_BIT(RE) | MACB_BIT(TE));
  2226. /* Clear the stats registers (XXX: Update stats first?) */
  2227. ctrl |= MACB_BIT(CLRSTAT);
  2228. macb_writel(bp, NCR, ctrl);
  2229. /* Clear all status flags */
  2230. macb_writel(bp, TSR, -1);
  2231. macb_writel(bp, RSR, -1);
  2232. /* Disable RX partial store and forward and reset watermark value */
  2233. gem_writel(bp, PBUFRXCUT, 0);
  2234. /* Disable all interrupts */
  2235. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2236. queue_writel(queue, IDR, -1);
  2237. queue_readl(queue, ISR);
  2238. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  2239. queue_writel(queue, ISR, -1);
  2240. }
  2241. }
  2242. static u32 gem_mdc_clk_div(struct macb *bp)
  2243. {
  2244. u32 config;
  2245. unsigned long pclk_hz = clk_get_rate(bp->pclk);
  2246. if (pclk_hz <= 20000000)
  2247. config = GEM_BF(CLK, GEM_CLK_DIV8);
  2248. else if (pclk_hz <= 40000000)
  2249. config = GEM_BF(CLK, GEM_CLK_DIV16);
  2250. else if (pclk_hz <= 80000000)
  2251. config = GEM_BF(CLK, GEM_CLK_DIV32);
  2252. else if (pclk_hz <= 120000000)
  2253. config = GEM_BF(CLK, GEM_CLK_DIV48);
  2254. else if (pclk_hz <= 160000000)
  2255. config = GEM_BF(CLK, GEM_CLK_DIV64);
  2256. else if (pclk_hz <= 240000000)
  2257. config = GEM_BF(CLK, GEM_CLK_DIV96);
  2258. else if (pclk_hz <= 320000000)
  2259. config = GEM_BF(CLK, GEM_CLK_DIV128);
  2260. else
  2261. config = GEM_BF(CLK, GEM_CLK_DIV224);
  2262. return config;
  2263. }
  2264. static u32 macb_mdc_clk_div(struct macb *bp)
  2265. {
  2266. u32 config;
  2267. unsigned long pclk_hz;
  2268. if (macb_is_gem(bp))
  2269. return gem_mdc_clk_div(bp);
  2270. pclk_hz = clk_get_rate(bp->pclk);
  2271. if (pclk_hz <= 20000000)
  2272. config = MACB_BF(CLK, MACB_CLK_DIV8);
  2273. else if (pclk_hz <= 40000000)
  2274. config = MACB_BF(CLK, MACB_CLK_DIV16);
  2275. else if (pclk_hz <= 80000000)
  2276. config = MACB_BF(CLK, MACB_CLK_DIV32);
  2277. else
  2278. config = MACB_BF(CLK, MACB_CLK_DIV64);
  2279. return config;
  2280. }
  2281. /* Get the DMA bus width field of the network configuration register that we
  2282. * should program. We find the width from decoding the design configuration
  2283. * register to find the maximum supported data bus width.
  2284. */
  2285. static u32 macb_dbw(struct macb *bp)
  2286. {
  2287. if (!macb_is_gem(bp))
  2288. return 0;
  2289. switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
  2290. case 4:
  2291. return GEM_BF(DBW, GEM_DBW128);
  2292. case 2:
  2293. return GEM_BF(DBW, GEM_DBW64);
  2294. case 1:
  2295. default:
  2296. return GEM_BF(DBW, GEM_DBW32);
  2297. }
  2298. }
  2299. /* Configure the receive DMA engine
  2300. * - use the correct receive buffer size
  2301. * - set best burst length for DMA operations
  2302. * (if not supported by FIFO, it will fallback to default)
  2303. * - set both rx/tx packet buffers to full memory size
  2304. * These are configurable parameters for GEM.
  2305. */
  2306. static void macb_configure_dma(struct macb *bp)
  2307. {
  2308. struct macb_queue *queue;
  2309. u32 buffer_size;
  2310. unsigned int q;
  2311. u32 dmacfg;
  2312. buffer_size = bp->rx_buffer_size / RX_BUFFER_MULTIPLE;
  2313. if (macb_is_gem(bp)) {
  2314. dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
  2315. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2316. if (q)
  2317. queue_writel(queue, RBQS, buffer_size);
  2318. else
  2319. dmacfg |= GEM_BF(RXBS, buffer_size);
  2320. }
  2321. if (bp->dma_burst_length)
  2322. dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
  2323. dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
  2324. dmacfg &= ~GEM_BIT(ENDIA_PKT);
  2325. if (bp->native_io)
  2326. dmacfg &= ~GEM_BIT(ENDIA_DESC);
  2327. else
  2328. dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
  2329. if (bp->dev->features & NETIF_F_HW_CSUM)
  2330. dmacfg |= GEM_BIT(TXCOEN);
  2331. else
  2332. dmacfg &= ~GEM_BIT(TXCOEN);
  2333. dmacfg &= ~GEM_BIT(ADDR64);
  2334. if (macb_dma64(bp))
  2335. dmacfg |= GEM_BIT(ADDR64);
  2336. if (macb_dma_ptp(bp))
  2337. dmacfg |= GEM_BIT(RXEXT) | GEM_BIT(TXEXT);
  2338. netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
  2339. dmacfg);
  2340. gem_writel(bp, DMACFG, dmacfg);
  2341. }
  2342. }
  2343. static void macb_init_hw(struct macb *bp)
  2344. {
  2345. u32 config;
  2346. macb_reset_hw(bp);
  2347. macb_set_hwaddr(bp);
  2348. config = macb_mdc_clk_div(bp);
  2349. /* Make eth data aligned.
  2350. * If RSC capable, that offset is ignored by HW.
  2351. */
  2352. if (!(bp->caps & MACB_CAPS_RSC))
  2353. config |= MACB_BF(RBOF, NET_IP_ALIGN);
  2354. config |= MACB_BIT(DRFCS); /* Discard Rx FCS */
  2355. if (bp->caps & MACB_CAPS_JUMBO)
  2356. config |= MACB_BIT(JFRAME); /* Enable jumbo frames */
  2357. else
  2358. config |= MACB_BIT(BIG); /* Receive oversized frames */
  2359. if (bp->dev->flags & IFF_PROMISC)
  2360. config |= MACB_BIT(CAF); /* Copy All Frames */
  2361. else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
  2362. config |= GEM_BIT(RXCOEN);
  2363. if (!(bp->dev->flags & IFF_BROADCAST))
  2364. config |= MACB_BIT(NBC); /* No BroadCast */
  2365. config |= macb_dbw(bp);
  2366. macb_writel(bp, NCFGR, config);
  2367. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  2368. gem_writel(bp, JML, bp->jumbo_max_len);
  2369. bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
  2370. if (bp->caps & MACB_CAPS_JUMBO)
  2371. bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
  2372. macb_configure_dma(bp);
  2373. /* Enable RX partial store and forward and set watermark */
  2374. if (bp->rx_watermark)
  2375. gem_writel(bp, PBUFRXCUT, (bp->rx_watermark | GEM_BIT(ENCUTTHRU)));
  2376. }
  2377. /* The hash address register is 64 bits long and takes up two
  2378. * locations in the memory map. The least significant bits are stored
  2379. * in EMAC_HSL and the most significant bits in EMAC_HSH.
  2380. *
  2381. * The unicast hash enable and the multicast hash enable bits in the
  2382. * network configuration register enable the reception of hash matched
  2383. * frames. The destination address is reduced to a 6 bit index into
  2384. * the 64 bit hash register using the following hash function. The
  2385. * hash function is an exclusive or of every sixth bit of the
  2386. * destination address.
  2387. *
  2388. * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
  2389. * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
  2390. * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
  2391. * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
  2392. * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
  2393. * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
  2394. *
  2395. * da[0] represents the least significant bit of the first byte
  2396. * received, that is, the multicast/unicast indicator, and da[47]
  2397. * represents the most significant bit of the last byte received. If
  2398. * the hash index, hi[n], points to a bit that is set in the hash
  2399. * register then the frame will be matched according to whether the
  2400. * frame is multicast or unicast. A multicast match will be signalled
  2401. * if the multicast hash enable bit is set, da[0] is 1 and the hash
  2402. * index points to a bit set in the hash register. A unicast match
  2403. * will be signalled if the unicast hash enable bit is set, da[0] is 0
  2404. * and the hash index points to a bit set in the hash register. To
  2405. * receive all multicast frames, the hash register should be set with
  2406. * all ones and the multicast hash enable bit should be set in the
  2407. * network configuration register.
  2408. */
  2409. static inline int hash_bit_value(int bitnr, __u8 *addr)
  2410. {
  2411. if (addr[bitnr / 8] & (1 << (bitnr % 8)))
  2412. return 1;
  2413. return 0;
  2414. }
  2415. /* Return the hash index value for the specified address. */
  2416. static int hash_get_index(__u8 *addr)
  2417. {
  2418. int i, j, bitval;
  2419. int hash_index = 0;
  2420. for (j = 0; j < 6; j++) {
  2421. for (i = 0, bitval = 0; i < 8; i++)
  2422. bitval ^= hash_bit_value(i * 6 + j, addr);
  2423. hash_index |= (bitval << j);
  2424. }
  2425. return hash_index;
  2426. }
  2427. /* Add multicast addresses to the internal multicast-hash table. */
  2428. static void macb_sethashtable(struct net_device *dev)
  2429. {
  2430. struct netdev_hw_addr *ha;
  2431. unsigned long mc_filter[2];
  2432. unsigned int bitnr;
  2433. struct macb *bp = netdev_priv(dev);
  2434. mc_filter[0] = 0;
  2435. mc_filter[1] = 0;
  2436. netdev_for_each_mc_addr(ha, dev) {
  2437. bitnr = hash_get_index(ha->addr);
  2438. mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
  2439. }
  2440. macb_or_gem_writel(bp, HRB, mc_filter[0]);
  2441. macb_or_gem_writel(bp, HRT, mc_filter[1]);
  2442. }
  2443. /* Enable/Disable promiscuous and multicast modes. */
  2444. static void macb_set_rx_mode(struct net_device *dev)
  2445. {
  2446. unsigned long cfg;
  2447. struct macb *bp = netdev_priv(dev);
  2448. cfg = macb_readl(bp, NCFGR);
  2449. if (dev->flags & IFF_PROMISC) {
  2450. /* Enable promiscuous mode */
  2451. cfg |= MACB_BIT(CAF);
  2452. /* Disable RX checksum offload */
  2453. if (macb_is_gem(bp))
  2454. cfg &= ~GEM_BIT(RXCOEN);
  2455. } else {
  2456. /* Disable promiscuous mode */
  2457. cfg &= ~MACB_BIT(CAF);
  2458. /* Enable RX checksum offload only if requested */
  2459. if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
  2460. cfg |= GEM_BIT(RXCOEN);
  2461. }
  2462. if (dev->flags & IFF_ALLMULTI) {
  2463. /* Enable all multicast mode */
  2464. macb_or_gem_writel(bp, HRB, -1);
  2465. macb_or_gem_writel(bp, HRT, -1);
  2466. cfg |= MACB_BIT(NCFGR_MTI);
  2467. } else if (!netdev_mc_empty(dev)) {
  2468. /* Enable specific multicasts */
  2469. macb_sethashtable(dev);
  2470. cfg |= MACB_BIT(NCFGR_MTI);
  2471. } else if (dev->flags & (~IFF_ALLMULTI)) {
  2472. /* Disable all multicast mode */
  2473. macb_or_gem_writel(bp, HRB, 0);
  2474. macb_or_gem_writel(bp, HRT, 0);
  2475. cfg &= ~MACB_BIT(NCFGR_MTI);
  2476. }
  2477. macb_writel(bp, NCFGR, cfg);
  2478. }
  2479. static int macb_open(struct net_device *dev)
  2480. {
  2481. size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
  2482. struct macb *bp = netdev_priv(dev);
  2483. struct macb_queue *queue;
  2484. unsigned int q;
  2485. int err;
  2486. netdev_dbg(bp->dev, "open\n");
  2487. err = pm_runtime_resume_and_get(&bp->pdev->dev);
  2488. if (err < 0)
  2489. return err;
  2490. /* RX buffers initialization */
  2491. macb_init_rx_buffer_size(bp, bufsz);
  2492. err = macb_alloc_consistent(bp);
  2493. if (err) {
  2494. netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
  2495. err);
  2496. goto pm_exit;
  2497. }
  2498. bp->macbgem_ops.mog_init_rings(bp);
  2499. macb_init_buffers(bp);
  2500. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2501. napi_enable(&queue->napi_rx);
  2502. napi_enable(&queue->napi_tx);
  2503. }
  2504. macb_init_hw(bp);
  2505. err = phy_set_mode_ext(bp->phy, PHY_MODE_ETHERNET, bp->phy_interface);
  2506. if (err)
  2507. goto reset_hw;
  2508. err = phy_power_on(bp->phy);
  2509. if (err)
  2510. goto reset_hw;
  2511. err = macb_phylink_connect(bp);
  2512. if (err)
  2513. goto phy_off;
  2514. netif_tx_start_all_queues(dev);
  2515. if (bp->ptp_info)
  2516. bp->ptp_info->ptp_init(dev);
  2517. return 0;
  2518. phy_off:
  2519. phy_power_off(bp->phy);
  2520. reset_hw:
  2521. macb_reset_hw(bp);
  2522. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2523. napi_disable(&queue->napi_rx);
  2524. napi_disable(&queue->napi_tx);
  2525. }
  2526. macb_free_consistent(bp);
  2527. pm_exit:
  2528. pm_runtime_put_sync(&bp->pdev->dev);
  2529. return err;
  2530. }
  2531. static int macb_close(struct net_device *dev)
  2532. {
  2533. struct macb *bp = netdev_priv(dev);
  2534. struct macb_queue *queue;
  2535. unsigned long flags;
  2536. unsigned int q;
  2537. netif_tx_stop_all_queues(dev);
  2538. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2539. napi_disable(&queue->napi_rx);
  2540. napi_disable(&queue->napi_tx);
  2541. netdev_tx_reset_queue(netdev_get_tx_queue(dev, q));
  2542. }
  2543. phylink_stop(bp->phylink);
  2544. phylink_disconnect_phy(bp->phylink);
  2545. phy_power_off(bp->phy);
  2546. spin_lock_irqsave(&bp->lock, flags);
  2547. macb_reset_hw(bp);
  2548. netif_carrier_off(dev);
  2549. spin_unlock_irqrestore(&bp->lock, flags);
  2550. macb_free_consistent(bp);
  2551. if (bp->ptp_info)
  2552. bp->ptp_info->ptp_remove(dev);
  2553. pm_runtime_put(&bp->pdev->dev);
  2554. return 0;
  2555. }
  2556. static int macb_change_mtu(struct net_device *dev, int new_mtu)
  2557. {
  2558. if (netif_running(dev))
  2559. return -EBUSY;
  2560. WRITE_ONCE(dev->mtu, new_mtu);
  2561. return 0;
  2562. }
  2563. static int macb_set_mac_addr(struct net_device *dev, void *addr)
  2564. {
  2565. int err;
  2566. err = eth_mac_addr(dev, addr);
  2567. if (err < 0)
  2568. return err;
  2569. macb_set_hwaddr(netdev_priv(dev));
  2570. return 0;
  2571. }
  2572. static void gem_update_stats(struct macb *bp)
  2573. {
  2574. struct macb_queue *queue;
  2575. unsigned int i, q, idx;
  2576. unsigned long *stat;
  2577. u64 *p = &bp->hw_stats.gem.tx_octets;
  2578. for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
  2579. u32 offset = gem_statistics[i].offset;
  2580. u64 val = bp->macb_reg_readl(bp, offset);
  2581. bp->ethtool_stats[i] += val;
  2582. *p += val;
  2583. if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
  2584. /* Add GEM_OCTTXH, GEM_OCTRXH */
  2585. val = bp->macb_reg_readl(bp, offset + 4);
  2586. bp->ethtool_stats[i] += ((u64)val) << 32;
  2587. *p += ((u64)val) << 32;
  2588. }
  2589. }
  2590. idx = GEM_STATS_LEN;
  2591. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
  2592. for (i = 0, stat = &queue->stats.first; i < QUEUE_STATS_LEN; ++i, ++stat)
  2593. bp->ethtool_stats[idx++] = *stat;
  2594. }
  2595. static void gem_get_stats(struct macb *bp, struct rtnl_link_stats64 *nstat)
  2596. {
  2597. struct gem_stats *hwstat = &bp->hw_stats.gem;
  2598. spin_lock_irq(&bp->stats_lock);
  2599. if (netif_running(bp->dev))
  2600. gem_update_stats(bp);
  2601. nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
  2602. hwstat->rx_alignment_errors +
  2603. hwstat->rx_resource_errors +
  2604. hwstat->rx_overruns +
  2605. hwstat->rx_oversize_frames +
  2606. hwstat->rx_jabbers +
  2607. hwstat->rx_undersized_frames +
  2608. hwstat->rx_length_field_frame_errors);
  2609. nstat->tx_errors = (hwstat->tx_late_collisions +
  2610. hwstat->tx_excessive_collisions +
  2611. hwstat->tx_underrun +
  2612. hwstat->tx_carrier_sense_errors);
  2613. nstat->multicast = hwstat->rx_multicast_frames;
  2614. nstat->collisions = (hwstat->tx_single_collision_frames +
  2615. hwstat->tx_multiple_collision_frames +
  2616. hwstat->tx_excessive_collisions);
  2617. nstat->rx_length_errors = (hwstat->rx_oversize_frames +
  2618. hwstat->rx_jabbers +
  2619. hwstat->rx_undersized_frames +
  2620. hwstat->rx_length_field_frame_errors);
  2621. nstat->rx_over_errors = hwstat->rx_resource_errors;
  2622. nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
  2623. nstat->rx_frame_errors = hwstat->rx_alignment_errors;
  2624. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2625. nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
  2626. nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
  2627. nstat->tx_fifo_errors = hwstat->tx_underrun;
  2628. spin_unlock_irq(&bp->stats_lock);
  2629. }
  2630. static void gem_get_ethtool_stats(struct net_device *dev,
  2631. struct ethtool_stats *stats, u64 *data)
  2632. {
  2633. struct macb *bp = netdev_priv(dev);
  2634. spin_lock_irq(&bp->stats_lock);
  2635. gem_update_stats(bp);
  2636. memcpy(data, &bp->ethtool_stats, sizeof(u64)
  2637. * (GEM_STATS_LEN + QUEUE_STATS_LEN * bp->num_queues));
  2638. spin_unlock_irq(&bp->stats_lock);
  2639. }
  2640. static int gem_get_sset_count(struct net_device *dev, int sset)
  2641. {
  2642. struct macb *bp = netdev_priv(dev);
  2643. switch (sset) {
  2644. case ETH_SS_STATS:
  2645. return GEM_STATS_LEN + bp->num_queues * QUEUE_STATS_LEN;
  2646. default:
  2647. return -EOPNOTSUPP;
  2648. }
  2649. }
  2650. static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
  2651. {
  2652. char stat_string[ETH_GSTRING_LEN];
  2653. struct macb *bp = netdev_priv(dev);
  2654. struct macb_queue *queue;
  2655. unsigned int i;
  2656. unsigned int q;
  2657. switch (sset) {
  2658. case ETH_SS_STATS:
  2659. for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
  2660. memcpy(p, gem_statistics[i].stat_string,
  2661. ETH_GSTRING_LEN);
  2662. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  2663. for (i = 0; i < QUEUE_STATS_LEN; i++, p += ETH_GSTRING_LEN) {
  2664. snprintf(stat_string, ETH_GSTRING_LEN, "q%d_%s",
  2665. q, queue_statistics[i].stat_string);
  2666. memcpy(p, stat_string, ETH_GSTRING_LEN);
  2667. }
  2668. }
  2669. break;
  2670. }
  2671. }
  2672. static void macb_get_stats(struct net_device *dev,
  2673. struct rtnl_link_stats64 *nstat)
  2674. {
  2675. struct macb *bp = netdev_priv(dev);
  2676. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2677. netdev_stats_to_stats64(nstat, &bp->dev->stats);
  2678. if (macb_is_gem(bp)) {
  2679. gem_get_stats(bp, nstat);
  2680. return;
  2681. }
  2682. /* read stats from hardware */
  2683. spin_lock_irq(&bp->stats_lock);
  2684. macb_update_stats(bp);
  2685. /* Convert HW stats into netdevice stats */
  2686. nstat->rx_errors = (hwstat->rx_fcs_errors +
  2687. hwstat->rx_align_errors +
  2688. hwstat->rx_resource_errors +
  2689. hwstat->rx_overruns +
  2690. hwstat->rx_oversize_pkts +
  2691. hwstat->rx_jabbers +
  2692. hwstat->rx_undersize_pkts +
  2693. hwstat->rx_length_mismatch);
  2694. nstat->tx_errors = (hwstat->tx_late_cols +
  2695. hwstat->tx_excessive_cols +
  2696. hwstat->tx_underruns +
  2697. hwstat->tx_carrier_errors +
  2698. hwstat->sqe_test_errors);
  2699. nstat->collisions = (hwstat->tx_single_cols +
  2700. hwstat->tx_multiple_cols +
  2701. hwstat->tx_excessive_cols);
  2702. nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
  2703. hwstat->rx_jabbers +
  2704. hwstat->rx_undersize_pkts +
  2705. hwstat->rx_length_mismatch);
  2706. nstat->rx_over_errors = hwstat->rx_resource_errors +
  2707. hwstat->rx_overruns;
  2708. nstat->rx_crc_errors = hwstat->rx_fcs_errors;
  2709. nstat->rx_frame_errors = hwstat->rx_align_errors;
  2710. nstat->rx_fifo_errors = hwstat->rx_overruns;
  2711. /* XXX: What does "missed" mean? */
  2712. nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
  2713. nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
  2714. nstat->tx_fifo_errors = hwstat->tx_underruns;
  2715. /* Don't know about heartbeat or window errors... */
  2716. spin_unlock_irq(&bp->stats_lock);
  2717. }
  2718. static void macb_get_pause_stats(struct net_device *dev,
  2719. struct ethtool_pause_stats *pause_stats)
  2720. {
  2721. struct macb *bp = netdev_priv(dev);
  2722. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2723. spin_lock_irq(&bp->stats_lock);
  2724. macb_update_stats(bp);
  2725. pause_stats->tx_pause_frames = hwstat->tx_pause_frames;
  2726. pause_stats->rx_pause_frames = hwstat->rx_pause_frames;
  2727. spin_unlock_irq(&bp->stats_lock);
  2728. }
  2729. static void gem_get_pause_stats(struct net_device *dev,
  2730. struct ethtool_pause_stats *pause_stats)
  2731. {
  2732. struct macb *bp = netdev_priv(dev);
  2733. struct gem_stats *hwstat = &bp->hw_stats.gem;
  2734. spin_lock_irq(&bp->stats_lock);
  2735. gem_update_stats(bp);
  2736. pause_stats->tx_pause_frames = hwstat->tx_pause_frames;
  2737. pause_stats->rx_pause_frames = hwstat->rx_pause_frames;
  2738. spin_unlock_irq(&bp->stats_lock);
  2739. }
  2740. static void macb_get_eth_mac_stats(struct net_device *dev,
  2741. struct ethtool_eth_mac_stats *mac_stats)
  2742. {
  2743. struct macb *bp = netdev_priv(dev);
  2744. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2745. spin_lock_irq(&bp->stats_lock);
  2746. macb_update_stats(bp);
  2747. mac_stats->FramesTransmittedOK = hwstat->tx_ok;
  2748. mac_stats->SingleCollisionFrames = hwstat->tx_single_cols;
  2749. mac_stats->MultipleCollisionFrames = hwstat->tx_multiple_cols;
  2750. mac_stats->FramesReceivedOK = hwstat->rx_ok;
  2751. mac_stats->FrameCheckSequenceErrors = hwstat->rx_fcs_errors;
  2752. mac_stats->AlignmentErrors = hwstat->rx_align_errors;
  2753. mac_stats->FramesWithDeferredXmissions = hwstat->tx_deferred;
  2754. mac_stats->LateCollisions = hwstat->tx_late_cols;
  2755. mac_stats->FramesAbortedDueToXSColls = hwstat->tx_excessive_cols;
  2756. mac_stats->FramesLostDueToIntMACXmitError = hwstat->tx_underruns;
  2757. mac_stats->CarrierSenseErrors = hwstat->tx_carrier_errors;
  2758. mac_stats->FramesLostDueToIntMACRcvError = hwstat->rx_overruns;
  2759. mac_stats->InRangeLengthErrors = hwstat->rx_length_mismatch;
  2760. mac_stats->FrameTooLongErrors = hwstat->rx_oversize_pkts;
  2761. spin_unlock_irq(&bp->stats_lock);
  2762. }
  2763. static void gem_get_eth_mac_stats(struct net_device *dev,
  2764. struct ethtool_eth_mac_stats *mac_stats)
  2765. {
  2766. struct macb *bp = netdev_priv(dev);
  2767. struct gem_stats *hwstat = &bp->hw_stats.gem;
  2768. spin_lock_irq(&bp->stats_lock);
  2769. gem_update_stats(bp);
  2770. mac_stats->FramesTransmittedOK = hwstat->tx_frames;
  2771. mac_stats->SingleCollisionFrames = hwstat->tx_single_collision_frames;
  2772. mac_stats->MultipleCollisionFrames =
  2773. hwstat->tx_multiple_collision_frames;
  2774. mac_stats->FramesReceivedOK = hwstat->rx_frames;
  2775. mac_stats->FrameCheckSequenceErrors =
  2776. hwstat->rx_frame_check_sequence_errors;
  2777. mac_stats->AlignmentErrors = hwstat->rx_alignment_errors;
  2778. mac_stats->OctetsTransmittedOK = hwstat->tx_octets;
  2779. mac_stats->FramesWithDeferredXmissions = hwstat->tx_deferred_frames;
  2780. mac_stats->LateCollisions = hwstat->tx_late_collisions;
  2781. mac_stats->FramesAbortedDueToXSColls = hwstat->tx_excessive_collisions;
  2782. mac_stats->FramesLostDueToIntMACXmitError = hwstat->tx_underrun;
  2783. mac_stats->CarrierSenseErrors = hwstat->tx_carrier_sense_errors;
  2784. mac_stats->OctetsReceivedOK = hwstat->rx_octets;
  2785. mac_stats->MulticastFramesXmittedOK = hwstat->tx_multicast_frames;
  2786. mac_stats->BroadcastFramesXmittedOK = hwstat->tx_broadcast_frames;
  2787. mac_stats->MulticastFramesReceivedOK = hwstat->rx_multicast_frames;
  2788. mac_stats->BroadcastFramesReceivedOK = hwstat->rx_broadcast_frames;
  2789. mac_stats->InRangeLengthErrors = hwstat->rx_length_field_frame_errors;
  2790. mac_stats->FrameTooLongErrors = hwstat->rx_oversize_frames;
  2791. spin_unlock_irq(&bp->stats_lock);
  2792. }
  2793. /* TODO: Report SQE test errors when added to phy_stats */
  2794. static void macb_get_eth_phy_stats(struct net_device *dev,
  2795. struct ethtool_eth_phy_stats *phy_stats)
  2796. {
  2797. struct macb *bp = netdev_priv(dev);
  2798. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2799. spin_lock_irq(&bp->stats_lock);
  2800. macb_update_stats(bp);
  2801. phy_stats->SymbolErrorDuringCarrier = hwstat->rx_symbol_errors;
  2802. spin_unlock_irq(&bp->stats_lock);
  2803. }
  2804. static void gem_get_eth_phy_stats(struct net_device *dev,
  2805. struct ethtool_eth_phy_stats *phy_stats)
  2806. {
  2807. struct macb *bp = netdev_priv(dev);
  2808. struct gem_stats *hwstat = &bp->hw_stats.gem;
  2809. spin_lock_irq(&bp->stats_lock);
  2810. gem_update_stats(bp);
  2811. phy_stats->SymbolErrorDuringCarrier = hwstat->rx_symbol_errors;
  2812. spin_unlock_irq(&bp->stats_lock);
  2813. }
  2814. static void macb_get_rmon_stats(struct net_device *dev,
  2815. struct ethtool_rmon_stats *rmon_stats,
  2816. const struct ethtool_rmon_hist_range **ranges)
  2817. {
  2818. struct macb *bp = netdev_priv(dev);
  2819. struct macb_stats *hwstat = &bp->hw_stats.macb;
  2820. spin_lock_irq(&bp->stats_lock);
  2821. macb_update_stats(bp);
  2822. rmon_stats->undersize_pkts = hwstat->rx_undersize_pkts;
  2823. rmon_stats->oversize_pkts = hwstat->rx_oversize_pkts;
  2824. rmon_stats->jabbers = hwstat->rx_jabbers;
  2825. spin_unlock_irq(&bp->stats_lock);
  2826. }
  2827. static const struct ethtool_rmon_hist_range gem_rmon_ranges[] = {
  2828. { 64, 64 },
  2829. { 65, 127 },
  2830. { 128, 255 },
  2831. { 256, 511 },
  2832. { 512, 1023 },
  2833. { 1024, 1518 },
  2834. { 1519, 16384 },
  2835. { },
  2836. };
  2837. static void gem_get_rmon_stats(struct net_device *dev,
  2838. struct ethtool_rmon_stats *rmon_stats,
  2839. const struct ethtool_rmon_hist_range **ranges)
  2840. {
  2841. struct macb *bp = netdev_priv(dev);
  2842. struct gem_stats *hwstat = &bp->hw_stats.gem;
  2843. spin_lock_irq(&bp->stats_lock);
  2844. gem_update_stats(bp);
  2845. rmon_stats->undersize_pkts = hwstat->rx_undersized_frames;
  2846. rmon_stats->oversize_pkts = hwstat->rx_oversize_frames;
  2847. rmon_stats->jabbers = hwstat->rx_jabbers;
  2848. rmon_stats->hist[0] = hwstat->rx_64_byte_frames;
  2849. rmon_stats->hist[1] = hwstat->rx_65_127_byte_frames;
  2850. rmon_stats->hist[2] = hwstat->rx_128_255_byte_frames;
  2851. rmon_stats->hist[3] = hwstat->rx_256_511_byte_frames;
  2852. rmon_stats->hist[4] = hwstat->rx_512_1023_byte_frames;
  2853. rmon_stats->hist[5] = hwstat->rx_1024_1518_byte_frames;
  2854. rmon_stats->hist[6] = hwstat->rx_greater_than_1518_byte_frames;
  2855. rmon_stats->hist_tx[0] = hwstat->tx_64_byte_frames;
  2856. rmon_stats->hist_tx[1] = hwstat->tx_65_127_byte_frames;
  2857. rmon_stats->hist_tx[2] = hwstat->tx_128_255_byte_frames;
  2858. rmon_stats->hist_tx[3] = hwstat->tx_256_511_byte_frames;
  2859. rmon_stats->hist_tx[4] = hwstat->tx_512_1023_byte_frames;
  2860. rmon_stats->hist_tx[5] = hwstat->tx_1024_1518_byte_frames;
  2861. rmon_stats->hist_tx[6] = hwstat->tx_greater_than_1518_byte_frames;
  2862. spin_unlock_irq(&bp->stats_lock);
  2863. *ranges = gem_rmon_ranges;
  2864. }
  2865. static int macb_get_regs_len(struct net_device *netdev)
  2866. {
  2867. return MACB_GREGS_NBR * sizeof(u32);
  2868. }
  2869. static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  2870. void *p)
  2871. {
  2872. struct macb *bp = netdev_priv(dev);
  2873. unsigned int tail, head;
  2874. u32 *regs_buff = p;
  2875. regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
  2876. | MACB_GREGS_VERSION;
  2877. tail = macb_tx_ring_wrap(bp, bp->queues[0].tx_tail);
  2878. head = macb_tx_ring_wrap(bp, bp->queues[0].tx_head);
  2879. regs_buff[0] = macb_readl(bp, NCR);
  2880. regs_buff[1] = macb_or_gem_readl(bp, NCFGR);
  2881. regs_buff[2] = macb_readl(bp, NSR);
  2882. regs_buff[3] = macb_readl(bp, TSR);
  2883. regs_buff[4] = macb_readl(bp, RBQP);
  2884. regs_buff[5] = macb_readl(bp, TBQP);
  2885. regs_buff[6] = macb_readl(bp, RSR);
  2886. regs_buff[7] = macb_readl(bp, IMR);
  2887. regs_buff[8] = tail;
  2888. regs_buff[9] = head;
  2889. regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
  2890. regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
  2891. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  2892. regs_buff[12] = macb_or_gem_readl(bp, USRIO);
  2893. if (macb_is_gem(bp))
  2894. regs_buff[13] = gem_readl(bp, DMACFG);
  2895. }
  2896. static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2897. {
  2898. struct macb *bp = netdev_priv(netdev);
  2899. phylink_ethtool_get_wol(bp->phylink, wol);
  2900. wol->supported |= (WAKE_MAGIC | WAKE_ARP);
  2901. /* Add macb wolopts to phy wolopts */
  2902. wol->wolopts |= bp->wolopts;
  2903. }
  2904. static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2905. {
  2906. struct macb *bp = netdev_priv(netdev);
  2907. int ret;
  2908. /* Pass the order to phylink layer */
  2909. ret = phylink_ethtool_set_wol(bp->phylink, wol);
  2910. /* Don't manage WoL on MAC, if PHY set_wol() fails */
  2911. if (ret && ret != -EOPNOTSUPP)
  2912. return ret;
  2913. bp->wolopts = (wol->wolopts & WAKE_MAGIC) ? WAKE_MAGIC : 0;
  2914. bp->wolopts |= (wol->wolopts & WAKE_ARP) ? WAKE_ARP : 0;
  2915. bp->wol = (wol->wolopts) ? MACB_WOL_ENABLED : 0;
  2916. device_set_wakeup_enable(&bp->pdev->dev, bp->wol);
  2917. return 0;
  2918. }
  2919. static int macb_get_link_ksettings(struct net_device *netdev,
  2920. struct ethtool_link_ksettings *kset)
  2921. {
  2922. struct macb *bp = netdev_priv(netdev);
  2923. return phylink_ethtool_ksettings_get(bp->phylink, kset);
  2924. }
  2925. static int macb_set_link_ksettings(struct net_device *netdev,
  2926. const struct ethtool_link_ksettings *kset)
  2927. {
  2928. struct macb *bp = netdev_priv(netdev);
  2929. return phylink_ethtool_ksettings_set(bp->phylink, kset);
  2930. }
  2931. static void macb_get_ringparam(struct net_device *netdev,
  2932. struct ethtool_ringparam *ring,
  2933. struct kernel_ethtool_ringparam *kernel_ring,
  2934. struct netlink_ext_ack *extack)
  2935. {
  2936. struct macb *bp = netdev_priv(netdev);
  2937. ring->rx_max_pending = MAX_RX_RING_SIZE;
  2938. ring->tx_max_pending = MAX_TX_RING_SIZE;
  2939. ring->rx_pending = bp->rx_ring_size;
  2940. ring->tx_pending = bp->tx_ring_size;
  2941. }
  2942. static int macb_set_ringparam(struct net_device *netdev,
  2943. struct ethtool_ringparam *ring,
  2944. struct kernel_ethtool_ringparam *kernel_ring,
  2945. struct netlink_ext_ack *extack)
  2946. {
  2947. struct macb *bp = netdev_priv(netdev);
  2948. u32 new_rx_size, new_tx_size;
  2949. unsigned int reset = 0;
  2950. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2951. return -EINVAL;
  2952. new_rx_size = clamp_t(u32, ring->rx_pending,
  2953. MIN_RX_RING_SIZE, MAX_RX_RING_SIZE);
  2954. new_rx_size = roundup_pow_of_two(new_rx_size);
  2955. new_tx_size = clamp_t(u32, ring->tx_pending,
  2956. MIN_TX_RING_SIZE, MAX_TX_RING_SIZE);
  2957. new_tx_size = roundup_pow_of_two(new_tx_size);
  2958. if ((new_tx_size == bp->tx_ring_size) &&
  2959. (new_rx_size == bp->rx_ring_size)) {
  2960. /* nothing to do */
  2961. return 0;
  2962. }
  2963. if (netif_running(bp->dev)) {
  2964. reset = 1;
  2965. macb_close(bp->dev);
  2966. }
  2967. bp->rx_ring_size = new_rx_size;
  2968. bp->tx_ring_size = new_tx_size;
  2969. if (reset)
  2970. macb_open(bp->dev);
  2971. return 0;
  2972. }
  2973. #ifdef CONFIG_MACB_USE_HWSTAMP
  2974. static unsigned int gem_get_tsu_rate(struct macb *bp)
  2975. {
  2976. struct clk *tsu_clk;
  2977. unsigned int tsu_rate;
  2978. tsu_clk = devm_clk_get(&bp->pdev->dev, "tsu_clk");
  2979. if (!IS_ERR(tsu_clk))
  2980. tsu_rate = clk_get_rate(tsu_clk);
  2981. /* try pclk instead */
  2982. else if (!IS_ERR(bp->pclk)) {
  2983. tsu_clk = bp->pclk;
  2984. tsu_rate = clk_get_rate(tsu_clk);
  2985. } else
  2986. return -ENOTSUPP;
  2987. return tsu_rate;
  2988. }
  2989. static s32 gem_get_ptp_max_adj(void)
  2990. {
  2991. return 64000000;
  2992. }
  2993. static int gem_get_ts_info(struct net_device *dev,
  2994. struct kernel_ethtool_ts_info *info)
  2995. {
  2996. struct macb *bp = netdev_priv(dev);
  2997. if (!macb_dma_ptp(bp)) {
  2998. ethtool_op_get_ts_info(dev, info);
  2999. return 0;
  3000. }
  3001. info->so_timestamping =
  3002. SOF_TIMESTAMPING_TX_SOFTWARE |
  3003. SOF_TIMESTAMPING_TX_HARDWARE |
  3004. SOF_TIMESTAMPING_RX_HARDWARE |
  3005. SOF_TIMESTAMPING_RAW_HARDWARE;
  3006. info->tx_types =
  3007. (1 << HWTSTAMP_TX_ONESTEP_SYNC) |
  3008. (1 << HWTSTAMP_TX_OFF) |
  3009. (1 << HWTSTAMP_TX_ON);
  3010. info->rx_filters =
  3011. (1 << HWTSTAMP_FILTER_NONE) |
  3012. (1 << HWTSTAMP_FILTER_ALL);
  3013. if (bp->ptp_clock)
  3014. info->phc_index = ptp_clock_index(bp->ptp_clock);
  3015. return 0;
  3016. }
  3017. static struct macb_ptp_info gem_ptp_info = {
  3018. .ptp_init = gem_ptp_init,
  3019. .ptp_remove = gem_ptp_remove,
  3020. .get_ptp_max_adj = gem_get_ptp_max_adj,
  3021. .get_tsu_rate = gem_get_tsu_rate,
  3022. .get_ts_info = gem_get_ts_info,
  3023. .get_hwtst = gem_get_hwtst,
  3024. .set_hwtst = gem_set_hwtst,
  3025. };
  3026. #endif
  3027. static int macb_get_ts_info(struct net_device *netdev,
  3028. struct kernel_ethtool_ts_info *info)
  3029. {
  3030. struct macb *bp = netdev_priv(netdev);
  3031. if (bp->ptp_info)
  3032. return bp->ptp_info->get_ts_info(netdev, info);
  3033. return ethtool_op_get_ts_info(netdev, info);
  3034. }
  3035. static void gem_enable_flow_filters(struct macb *bp, bool enable)
  3036. {
  3037. struct net_device *netdev = bp->dev;
  3038. struct ethtool_rx_fs_item *item;
  3039. u32 t2_scr;
  3040. int num_t2_scr;
  3041. if (!(netdev->features & NETIF_F_NTUPLE))
  3042. return;
  3043. num_t2_scr = GEM_BFEXT(T2SCR, gem_readl(bp, DCFG8));
  3044. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  3045. struct ethtool_rx_flow_spec *fs = &item->fs;
  3046. struct ethtool_tcpip4_spec *tp4sp_m;
  3047. if (fs->location >= num_t2_scr)
  3048. continue;
  3049. t2_scr = gem_readl_n(bp, SCRT2, fs->location);
  3050. /* enable/disable screener regs for the flow entry */
  3051. t2_scr = GEM_BFINS(ETHTEN, enable, t2_scr);
  3052. /* only enable fields with no masking */
  3053. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  3054. if (enable && (tp4sp_m->ip4src == 0xFFFFFFFF))
  3055. t2_scr = GEM_BFINS(CMPAEN, 1, t2_scr);
  3056. else
  3057. t2_scr = GEM_BFINS(CMPAEN, 0, t2_scr);
  3058. if (enable && (tp4sp_m->ip4dst == 0xFFFFFFFF))
  3059. t2_scr = GEM_BFINS(CMPBEN, 1, t2_scr);
  3060. else
  3061. t2_scr = GEM_BFINS(CMPBEN, 0, t2_scr);
  3062. if (enable && ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)))
  3063. t2_scr = GEM_BFINS(CMPCEN, 1, t2_scr);
  3064. else
  3065. t2_scr = GEM_BFINS(CMPCEN, 0, t2_scr);
  3066. gem_writel_n(bp, SCRT2, fs->location, t2_scr);
  3067. }
  3068. }
  3069. static void gem_prog_cmp_regs(struct macb *bp, struct ethtool_rx_flow_spec *fs)
  3070. {
  3071. struct ethtool_tcpip4_spec *tp4sp_v, *tp4sp_m;
  3072. uint16_t index = fs->location;
  3073. u32 w0, w1, t2_scr;
  3074. bool cmp_a = false;
  3075. bool cmp_b = false;
  3076. bool cmp_c = false;
  3077. if (!macb_is_gem(bp))
  3078. return;
  3079. tp4sp_v = &(fs->h_u.tcp_ip4_spec);
  3080. tp4sp_m = &(fs->m_u.tcp_ip4_spec);
  3081. /* ignore field if any masking set */
  3082. if (tp4sp_m->ip4src == 0xFFFFFFFF) {
  3083. /* 1st compare reg - IP source address */
  3084. w0 = 0;
  3085. w1 = 0;
  3086. w0 = tp4sp_v->ip4src;
  3087. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  3088. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  3089. w1 = GEM_BFINS(T2OFST, ETYPE_SRCIP_OFFSET, w1);
  3090. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w0);
  3091. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4SRC_CMP(index)), w1);
  3092. cmp_a = true;
  3093. }
  3094. /* ignore field if any masking set */
  3095. if (tp4sp_m->ip4dst == 0xFFFFFFFF) {
  3096. /* 2nd compare reg - IP destination address */
  3097. w0 = 0;
  3098. w1 = 0;
  3099. w0 = tp4sp_v->ip4dst;
  3100. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  3101. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_ETYPE, w1);
  3102. w1 = GEM_BFINS(T2OFST, ETYPE_DSTIP_OFFSET, w1);
  3103. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_IP4DST_CMP(index)), w0);
  3104. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_IP4DST_CMP(index)), w1);
  3105. cmp_b = true;
  3106. }
  3107. /* ignore both port fields if masking set in both */
  3108. if ((tp4sp_m->psrc == 0xFFFF) || (tp4sp_m->pdst == 0xFFFF)) {
  3109. /* 3rd compare reg - source port, destination port */
  3110. w0 = 0;
  3111. w1 = 0;
  3112. w1 = GEM_BFINS(T2CMPOFST, GEM_T2COMPOFST_IPHDR, w1);
  3113. if (tp4sp_m->psrc == tp4sp_m->pdst) {
  3114. w0 = GEM_BFINS(T2MASK, tp4sp_v->psrc, w0);
  3115. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  3116. w1 = GEM_BFINS(T2DISMSK, 1, w1); /* 32-bit compare */
  3117. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  3118. } else {
  3119. /* only one port definition */
  3120. w1 = GEM_BFINS(T2DISMSK, 0, w1); /* 16-bit compare */
  3121. w0 = GEM_BFINS(T2MASK, 0xFFFF, w0);
  3122. if (tp4sp_m->psrc == 0xFFFF) { /* src port */
  3123. w0 = GEM_BFINS(T2CMP, tp4sp_v->psrc, w0);
  3124. w1 = GEM_BFINS(T2OFST, IPHDR_SRCPORT_OFFSET, w1);
  3125. } else { /* dst port */
  3126. w0 = GEM_BFINS(T2CMP, tp4sp_v->pdst, w0);
  3127. w1 = GEM_BFINS(T2OFST, IPHDR_DSTPORT_OFFSET, w1);
  3128. }
  3129. }
  3130. gem_writel_n(bp, T2CMPW0, T2CMP_OFST(GEM_PORT_CMP(index)), w0);
  3131. gem_writel_n(bp, T2CMPW1, T2CMP_OFST(GEM_PORT_CMP(index)), w1);
  3132. cmp_c = true;
  3133. }
  3134. t2_scr = 0;
  3135. t2_scr = GEM_BFINS(QUEUE, (fs->ring_cookie) & 0xFF, t2_scr);
  3136. t2_scr = GEM_BFINS(ETHT2IDX, SCRT2_ETHT, t2_scr);
  3137. if (cmp_a)
  3138. t2_scr = GEM_BFINS(CMPA, GEM_IP4SRC_CMP(index), t2_scr);
  3139. if (cmp_b)
  3140. t2_scr = GEM_BFINS(CMPB, GEM_IP4DST_CMP(index), t2_scr);
  3141. if (cmp_c)
  3142. t2_scr = GEM_BFINS(CMPC, GEM_PORT_CMP(index), t2_scr);
  3143. gem_writel_n(bp, SCRT2, index, t2_scr);
  3144. }
  3145. static int gem_add_flow_filter(struct net_device *netdev,
  3146. struct ethtool_rxnfc *cmd)
  3147. {
  3148. struct macb *bp = netdev_priv(netdev);
  3149. struct ethtool_rx_flow_spec *fs = &cmd->fs;
  3150. struct ethtool_rx_fs_item *item, *newfs;
  3151. unsigned long flags;
  3152. int ret = -EINVAL;
  3153. bool added = false;
  3154. newfs = kmalloc_obj(*newfs);
  3155. if (newfs == NULL)
  3156. return -ENOMEM;
  3157. memcpy(&newfs->fs, fs, sizeof(newfs->fs));
  3158. netdev_dbg(netdev,
  3159. "Adding flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  3160. fs->flow_type, (int)fs->ring_cookie, fs->location,
  3161. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  3162. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  3163. be16_to_cpu(fs->h_u.tcp_ip4_spec.psrc),
  3164. be16_to_cpu(fs->h_u.tcp_ip4_spec.pdst));
  3165. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  3166. /* find correct place to add in list */
  3167. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  3168. if (item->fs.location > newfs->fs.location) {
  3169. list_add_tail(&newfs->list, &item->list);
  3170. added = true;
  3171. break;
  3172. } else if (item->fs.location == fs->location) {
  3173. netdev_err(netdev, "Rule not added: location %d not free!\n",
  3174. fs->location);
  3175. ret = -EBUSY;
  3176. goto err;
  3177. }
  3178. }
  3179. if (!added)
  3180. list_add_tail(&newfs->list, &bp->rx_fs_list.list);
  3181. gem_prog_cmp_regs(bp, fs);
  3182. bp->rx_fs_list.count++;
  3183. /* enable filtering if NTUPLE on */
  3184. gem_enable_flow_filters(bp, 1);
  3185. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  3186. return 0;
  3187. err:
  3188. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  3189. kfree(newfs);
  3190. return ret;
  3191. }
  3192. static int gem_del_flow_filter(struct net_device *netdev,
  3193. struct ethtool_rxnfc *cmd)
  3194. {
  3195. struct macb *bp = netdev_priv(netdev);
  3196. struct ethtool_rx_fs_item *item;
  3197. struct ethtool_rx_flow_spec *fs;
  3198. unsigned long flags;
  3199. spin_lock_irqsave(&bp->rx_fs_lock, flags);
  3200. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  3201. if (item->fs.location == cmd->fs.location) {
  3202. /* disable screener regs for the flow entry */
  3203. fs = &(item->fs);
  3204. netdev_dbg(netdev,
  3205. "Deleting flow filter entry,type=%u,queue=%u,loc=%u,src=%08X,dst=%08X,ps=%u,pd=%u\n",
  3206. fs->flow_type, (int)fs->ring_cookie, fs->location,
  3207. htonl(fs->h_u.tcp_ip4_spec.ip4src),
  3208. htonl(fs->h_u.tcp_ip4_spec.ip4dst),
  3209. be16_to_cpu(fs->h_u.tcp_ip4_spec.psrc),
  3210. be16_to_cpu(fs->h_u.tcp_ip4_spec.pdst));
  3211. gem_writel_n(bp, SCRT2, fs->location, 0);
  3212. list_del(&item->list);
  3213. bp->rx_fs_list.count--;
  3214. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  3215. kfree(item);
  3216. return 0;
  3217. }
  3218. }
  3219. spin_unlock_irqrestore(&bp->rx_fs_lock, flags);
  3220. return -EINVAL;
  3221. }
  3222. static int gem_get_flow_entry(struct net_device *netdev,
  3223. struct ethtool_rxnfc *cmd)
  3224. {
  3225. struct macb *bp = netdev_priv(netdev);
  3226. struct ethtool_rx_fs_item *item;
  3227. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  3228. if (item->fs.location == cmd->fs.location) {
  3229. memcpy(&cmd->fs, &item->fs, sizeof(cmd->fs));
  3230. return 0;
  3231. }
  3232. }
  3233. return -EINVAL;
  3234. }
  3235. static int gem_get_all_flow_entries(struct net_device *netdev,
  3236. struct ethtool_rxnfc *cmd, u32 *rule_locs)
  3237. {
  3238. struct macb *bp = netdev_priv(netdev);
  3239. struct ethtool_rx_fs_item *item;
  3240. uint32_t cnt = 0;
  3241. list_for_each_entry(item, &bp->rx_fs_list.list, list) {
  3242. if (cnt == cmd->rule_cnt)
  3243. return -EMSGSIZE;
  3244. rule_locs[cnt] = item->fs.location;
  3245. cnt++;
  3246. }
  3247. cmd->data = bp->max_tuples;
  3248. cmd->rule_cnt = cnt;
  3249. return 0;
  3250. }
  3251. static u32 gem_get_rx_ring_count(struct net_device *netdev)
  3252. {
  3253. struct macb *bp = netdev_priv(netdev);
  3254. return bp->num_queues;
  3255. }
  3256. static int gem_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  3257. u32 *rule_locs)
  3258. {
  3259. struct macb *bp = netdev_priv(netdev);
  3260. int ret = 0;
  3261. switch (cmd->cmd) {
  3262. case ETHTOOL_GRXCLSRLCNT:
  3263. cmd->rule_cnt = bp->rx_fs_list.count;
  3264. break;
  3265. case ETHTOOL_GRXCLSRULE:
  3266. ret = gem_get_flow_entry(netdev, cmd);
  3267. break;
  3268. case ETHTOOL_GRXCLSRLALL:
  3269. ret = gem_get_all_flow_entries(netdev, cmd, rule_locs);
  3270. break;
  3271. default:
  3272. netdev_err(netdev,
  3273. "Command parameter %d is not supported\n", cmd->cmd);
  3274. ret = -EOPNOTSUPP;
  3275. }
  3276. return ret;
  3277. }
  3278. static int gem_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  3279. {
  3280. struct macb *bp = netdev_priv(netdev);
  3281. int ret;
  3282. if (!(netdev->hw_features & NETIF_F_NTUPLE))
  3283. return -EOPNOTSUPP;
  3284. switch (cmd->cmd) {
  3285. case ETHTOOL_SRXCLSRLINS:
  3286. if ((cmd->fs.location >= bp->max_tuples)
  3287. || (cmd->fs.ring_cookie >= bp->num_queues)) {
  3288. ret = -EINVAL;
  3289. break;
  3290. }
  3291. ret = gem_add_flow_filter(netdev, cmd);
  3292. break;
  3293. case ETHTOOL_SRXCLSRLDEL:
  3294. ret = gem_del_flow_filter(netdev, cmd);
  3295. break;
  3296. default:
  3297. netdev_err(netdev,
  3298. "Command parameter %d is not supported\n", cmd->cmd);
  3299. ret = -EOPNOTSUPP;
  3300. }
  3301. return ret;
  3302. }
  3303. static const struct ethtool_ops macb_ethtool_ops = {
  3304. .get_regs_len = macb_get_regs_len,
  3305. .get_regs = macb_get_regs,
  3306. .get_link = ethtool_op_get_link,
  3307. .get_ts_info = ethtool_op_get_ts_info,
  3308. .get_pause_stats = macb_get_pause_stats,
  3309. .get_eth_mac_stats = macb_get_eth_mac_stats,
  3310. .get_eth_phy_stats = macb_get_eth_phy_stats,
  3311. .get_rmon_stats = macb_get_rmon_stats,
  3312. .get_wol = macb_get_wol,
  3313. .set_wol = macb_set_wol,
  3314. .get_link_ksettings = macb_get_link_ksettings,
  3315. .set_link_ksettings = macb_set_link_ksettings,
  3316. .get_ringparam = macb_get_ringparam,
  3317. .set_ringparam = macb_set_ringparam,
  3318. };
  3319. static const struct ethtool_ops gem_ethtool_ops = {
  3320. .get_regs_len = macb_get_regs_len,
  3321. .get_regs = macb_get_regs,
  3322. .get_wol = macb_get_wol,
  3323. .set_wol = macb_set_wol,
  3324. .get_link = ethtool_op_get_link,
  3325. .get_ts_info = macb_get_ts_info,
  3326. .get_ethtool_stats = gem_get_ethtool_stats,
  3327. .get_strings = gem_get_ethtool_strings,
  3328. .get_sset_count = gem_get_sset_count,
  3329. .get_pause_stats = gem_get_pause_stats,
  3330. .get_eth_mac_stats = gem_get_eth_mac_stats,
  3331. .get_eth_phy_stats = gem_get_eth_phy_stats,
  3332. .get_rmon_stats = gem_get_rmon_stats,
  3333. .get_link_ksettings = macb_get_link_ksettings,
  3334. .set_link_ksettings = macb_set_link_ksettings,
  3335. .get_ringparam = macb_get_ringparam,
  3336. .set_ringparam = macb_set_ringparam,
  3337. .get_rxnfc = gem_get_rxnfc,
  3338. .set_rxnfc = gem_set_rxnfc,
  3339. .get_rx_ring_count = gem_get_rx_ring_count,
  3340. };
  3341. static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  3342. {
  3343. struct macb *bp = netdev_priv(dev);
  3344. if (!netif_running(dev))
  3345. return -EINVAL;
  3346. return phylink_mii_ioctl(bp->phylink, rq, cmd);
  3347. }
  3348. static int macb_hwtstamp_get(struct net_device *dev,
  3349. struct kernel_hwtstamp_config *cfg)
  3350. {
  3351. struct macb *bp = netdev_priv(dev);
  3352. if (!netif_running(dev))
  3353. return -EINVAL;
  3354. if (!bp->ptp_info)
  3355. return -EOPNOTSUPP;
  3356. return bp->ptp_info->get_hwtst(dev, cfg);
  3357. }
  3358. static int macb_hwtstamp_set(struct net_device *dev,
  3359. struct kernel_hwtstamp_config *cfg,
  3360. struct netlink_ext_ack *extack)
  3361. {
  3362. struct macb *bp = netdev_priv(dev);
  3363. if (!netif_running(dev))
  3364. return -EINVAL;
  3365. if (!bp->ptp_info)
  3366. return -EOPNOTSUPP;
  3367. return bp->ptp_info->set_hwtst(dev, cfg, extack);
  3368. }
  3369. static inline void macb_set_txcsum_feature(struct macb *bp,
  3370. netdev_features_t features)
  3371. {
  3372. u32 val;
  3373. if (!macb_is_gem(bp))
  3374. return;
  3375. val = gem_readl(bp, DMACFG);
  3376. if (features & NETIF_F_HW_CSUM)
  3377. val |= GEM_BIT(TXCOEN);
  3378. else
  3379. val &= ~GEM_BIT(TXCOEN);
  3380. gem_writel(bp, DMACFG, val);
  3381. }
  3382. static inline void macb_set_rxcsum_feature(struct macb *bp,
  3383. netdev_features_t features)
  3384. {
  3385. struct net_device *netdev = bp->dev;
  3386. u32 val;
  3387. if (!macb_is_gem(bp))
  3388. return;
  3389. val = gem_readl(bp, NCFGR);
  3390. if ((features & NETIF_F_RXCSUM) && !(netdev->flags & IFF_PROMISC))
  3391. val |= GEM_BIT(RXCOEN);
  3392. else
  3393. val &= ~GEM_BIT(RXCOEN);
  3394. gem_writel(bp, NCFGR, val);
  3395. }
  3396. static inline void macb_set_rxflow_feature(struct macb *bp,
  3397. netdev_features_t features)
  3398. {
  3399. if (!macb_is_gem(bp))
  3400. return;
  3401. gem_enable_flow_filters(bp, !!(features & NETIF_F_NTUPLE));
  3402. }
  3403. static int macb_set_features(struct net_device *netdev,
  3404. netdev_features_t features)
  3405. {
  3406. struct macb *bp = netdev_priv(netdev);
  3407. netdev_features_t changed = features ^ netdev->features;
  3408. /* TX checksum offload */
  3409. if (changed & NETIF_F_HW_CSUM)
  3410. macb_set_txcsum_feature(bp, features);
  3411. /* RX checksum offload */
  3412. if (changed & NETIF_F_RXCSUM)
  3413. macb_set_rxcsum_feature(bp, features);
  3414. /* RX Flow Filters */
  3415. if (changed & NETIF_F_NTUPLE)
  3416. macb_set_rxflow_feature(bp, features);
  3417. return 0;
  3418. }
  3419. static void macb_restore_features(struct macb *bp)
  3420. {
  3421. struct net_device *netdev = bp->dev;
  3422. netdev_features_t features = netdev->features;
  3423. struct ethtool_rx_fs_item *item;
  3424. /* TX checksum offload */
  3425. macb_set_txcsum_feature(bp, features);
  3426. /* RX checksum offload */
  3427. macb_set_rxcsum_feature(bp, features);
  3428. /* RX Flow Filters */
  3429. list_for_each_entry(item, &bp->rx_fs_list.list, list)
  3430. gem_prog_cmp_regs(bp, &item->fs);
  3431. macb_set_rxflow_feature(bp, features);
  3432. }
  3433. static int macb_taprio_setup_replace(struct net_device *ndev,
  3434. struct tc_taprio_qopt_offload *conf)
  3435. {
  3436. u64 total_on_time = 0, start_time_sec = 0, start_time = conf->base_time;
  3437. u32 configured_queues = 0, speed = 0, start_time_nsec;
  3438. struct macb_queue_enst_config *enst_queue;
  3439. struct tc_taprio_sched_entry *entry;
  3440. struct macb *bp = netdev_priv(ndev);
  3441. struct ethtool_link_ksettings kset;
  3442. struct macb_queue *queue;
  3443. u32 queue_mask;
  3444. u8 queue_id;
  3445. size_t i;
  3446. int err;
  3447. if (conf->num_entries > bp->num_queues) {
  3448. netdev_err(ndev, "Too many TAPRIO entries: %zu > %d queues\n",
  3449. conf->num_entries, bp->num_queues);
  3450. return -EINVAL;
  3451. }
  3452. if (conf->base_time < 0) {
  3453. netdev_err(ndev, "Invalid base_time: must be 0 or positive, got %lld\n",
  3454. conf->base_time);
  3455. return -ERANGE;
  3456. }
  3457. /* Get the current link speed */
  3458. err = phylink_ethtool_ksettings_get(bp->phylink, &kset);
  3459. if (unlikely(err)) {
  3460. netdev_err(ndev, "Failed to get link settings: %d\n", err);
  3461. return err;
  3462. }
  3463. speed = kset.base.speed;
  3464. if (unlikely(speed <= 0)) {
  3465. netdev_err(ndev, "Invalid speed: %d\n", speed);
  3466. return -EINVAL;
  3467. }
  3468. enst_queue = kcalloc(conf->num_entries, sizeof(*enst_queue), GFP_KERNEL);
  3469. if (unlikely(!enst_queue))
  3470. return -ENOMEM;
  3471. /* Pre-validate all entries before making any hardware changes */
  3472. for (i = 0; i < conf->num_entries; i++) {
  3473. entry = &conf->entries[i];
  3474. if (entry->command != TC_TAPRIO_CMD_SET_GATES) {
  3475. netdev_err(ndev, "Entry %zu: unsupported command %d\n",
  3476. i, entry->command);
  3477. err = -EOPNOTSUPP;
  3478. goto cleanup;
  3479. }
  3480. /* Validate gate_mask: must be nonzero, single queue, and within range */
  3481. if (!is_power_of_2(entry->gate_mask)) {
  3482. netdev_err(ndev, "Entry %zu: gate_mask 0x%x is not a power of 2 (only one queue per entry allowed)\n",
  3483. i, entry->gate_mask);
  3484. err = -EINVAL;
  3485. goto cleanup;
  3486. }
  3487. /* gate_mask must not select queues outside the valid queues */
  3488. queue_id = order_base_2(entry->gate_mask);
  3489. if (queue_id >= bp->num_queues) {
  3490. netdev_err(ndev, "Entry %zu: gate_mask 0x%x exceeds queue range (max_queues=%d)\n",
  3491. i, entry->gate_mask, bp->num_queues);
  3492. err = -EINVAL;
  3493. goto cleanup;
  3494. }
  3495. /* Check for start time limits */
  3496. start_time_sec = start_time;
  3497. start_time_nsec = do_div(start_time_sec, NSEC_PER_SEC);
  3498. if (start_time_sec > GENMASK(GEM_START_TIME_SEC_SIZE - 1, 0)) {
  3499. netdev_err(ndev, "Entry %zu: Start time %llu s exceeds hardware limit\n",
  3500. i, start_time_sec);
  3501. err = -ERANGE;
  3502. goto cleanup;
  3503. }
  3504. /* Check for on time limit */
  3505. if (entry->interval > enst_max_hw_interval(speed)) {
  3506. netdev_err(ndev, "Entry %zu: interval %u ns exceeds hardware limit %llu ns\n",
  3507. i, entry->interval, enst_max_hw_interval(speed));
  3508. err = -ERANGE;
  3509. goto cleanup;
  3510. }
  3511. /* Check for off time limit*/
  3512. if ((conf->cycle_time - entry->interval) > enst_max_hw_interval(speed)) {
  3513. netdev_err(ndev, "Entry %zu: off_time %llu ns exceeds hardware limit %llu ns\n",
  3514. i, conf->cycle_time - entry->interval,
  3515. enst_max_hw_interval(speed));
  3516. err = -ERANGE;
  3517. goto cleanup;
  3518. }
  3519. enst_queue[i].queue_id = queue_id;
  3520. enst_queue[i].start_time_mask =
  3521. (start_time_sec << GEM_START_TIME_SEC_OFFSET) |
  3522. start_time_nsec;
  3523. enst_queue[i].on_time_bytes =
  3524. enst_ns_to_hw_units(entry->interval, speed);
  3525. enst_queue[i].off_time_bytes =
  3526. enst_ns_to_hw_units(conf->cycle_time - entry->interval, speed);
  3527. configured_queues |= entry->gate_mask;
  3528. total_on_time += entry->interval;
  3529. start_time += entry->interval;
  3530. }
  3531. /* Check total interval doesn't exceed cycle time */
  3532. if (total_on_time > conf->cycle_time) {
  3533. netdev_err(ndev, "Total ON %llu ns exceeds cycle time %llu ns\n",
  3534. total_on_time, conf->cycle_time);
  3535. err = -EINVAL;
  3536. goto cleanup;
  3537. }
  3538. netdev_dbg(ndev, "TAPRIO setup: %zu entries, base_time=%lld ns, cycle_time=%llu ns\n",
  3539. conf->num_entries, conf->base_time, conf->cycle_time);
  3540. /* All validations passed - proceed with hardware configuration */
  3541. scoped_guard(spinlock_irqsave, &bp->lock) {
  3542. /* Disable ENST queues if running before configuring */
  3543. queue_mask = BIT_U32(bp->num_queues) - 1;
  3544. gem_writel(bp, ENST_CONTROL,
  3545. queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET);
  3546. for (i = 0; i < conf->num_entries; i++) {
  3547. queue = &bp->queues[enst_queue[i].queue_id];
  3548. /* Configure queue timing registers */
  3549. queue_writel(queue, ENST_START_TIME,
  3550. enst_queue[i].start_time_mask);
  3551. queue_writel(queue, ENST_ON_TIME,
  3552. enst_queue[i].on_time_bytes);
  3553. queue_writel(queue, ENST_OFF_TIME,
  3554. enst_queue[i].off_time_bytes);
  3555. }
  3556. /* Enable ENST for all configured queues in one write */
  3557. gem_writel(bp, ENST_CONTROL, configured_queues);
  3558. }
  3559. netdev_info(ndev, "TAPRIO configuration completed successfully: %zu entries, %d queues configured\n",
  3560. conf->num_entries, hweight32(configured_queues));
  3561. cleanup:
  3562. kfree(enst_queue);
  3563. return err;
  3564. }
  3565. static void macb_taprio_destroy(struct net_device *ndev)
  3566. {
  3567. struct macb *bp = netdev_priv(ndev);
  3568. struct macb_queue *queue;
  3569. u32 queue_mask;
  3570. unsigned int q;
  3571. netdev_reset_tc(ndev);
  3572. queue_mask = BIT_U32(bp->num_queues) - 1;
  3573. scoped_guard(spinlock_irqsave, &bp->lock) {
  3574. /* Single disable command for all queues */
  3575. gem_writel(bp, ENST_CONTROL,
  3576. queue_mask << GEM_ENST_DISABLE_QUEUE_OFFSET);
  3577. /* Clear all queue ENST registers in batch */
  3578. for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
  3579. queue_writel(queue, ENST_START_TIME, 0);
  3580. queue_writel(queue, ENST_ON_TIME, 0);
  3581. queue_writel(queue, ENST_OFF_TIME, 0);
  3582. }
  3583. }
  3584. netdev_info(ndev, "TAPRIO destroy: All gates disabled\n");
  3585. }
  3586. static int macb_setup_taprio(struct net_device *ndev,
  3587. struct tc_taprio_qopt_offload *taprio)
  3588. {
  3589. struct macb *bp = netdev_priv(ndev);
  3590. int err = 0;
  3591. if (unlikely(!(ndev->hw_features & NETIF_F_HW_TC)))
  3592. return -EOPNOTSUPP;
  3593. /* Check if Device is in runtime suspend */
  3594. if (unlikely(pm_runtime_suspended(&bp->pdev->dev))) {
  3595. netdev_err(ndev, "Device is in runtime suspend\n");
  3596. return -EOPNOTSUPP;
  3597. }
  3598. switch (taprio->cmd) {
  3599. case TAPRIO_CMD_REPLACE:
  3600. err = macb_taprio_setup_replace(ndev, taprio);
  3601. break;
  3602. case TAPRIO_CMD_DESTROY:
  3603. macb_taprio_destroy(ndev);
  3604. break;
  3605. default:
  3606. err = -EOPNOTSUPP;
  3607. }
  3608. return err;
  3609. }
  3610. static int macb_setup_tc(struct net_device *dev, enum tc_setup_type type,
  3611. void *type_data)
  3612. {
  3613. if (!dev || !type_data)
  3614. return -EINVAL;
  3615. switch (type) {
  3616. case TC_SETUP_QDISC_TAPRIO:
  3617. return macb_setup_taprio(dev, type_data);
  3618. default:
  3619. return -EOPNOTSUPP;
  3620. }
  3621. }
  3622. static const struct net_device_ops macb_netdev_ops = {
  3623. .ndo_open = macb_open,
  3624. .ndo_stop = macb_close,
  3625. .ndo_start_xmit = macb_start_xmit,
  3626. .ndo_set_rx_mode = macb_set_rx_mode,
  3627. .ndo_get_stats64 = macb_get_stats,
  3628. .ndo_eth_ioctl = macb_ioctl,
  3629. .ndo_validate_addr = eth_validate_addr,
  3630. .ndo_change_mtu = macb_change_mtu,
  3631. .ndo_set_mac_address = macb_set_mac_addr,
  3632. #ifdef CONFIG_NET_POLL_CONTROLLER
  3633. .ndo_poll_controller = macb_poll_controller,
  3634. #endif
  3635. .ndo_set_features = macb_set_features,
  3636. .ndo_features_check = macb_features_check,
  3637. .ndo_hwtstamp_set = macb_hwtstamp_set,
  3638. .ndo_hwtstamp_get = macb_hwtstamp_get,
  3639. .ndo_setup_tc = macb_setup_tc,
  3640. };
  3641. /* Configure peripheral capabilities according to device tree
  3642. * and integration options used
  3643. */
  3644. static void macb_configure_caps(struct macb *bp,
  3645. const struct macb_config *dt_conf)
  3646. {
  3647. struct device_node *np = bp->pdev->dev.of_node;
  3648. bool refclk_ext;
  3649. u32 dcfg;
  3650. refclk_ext = of_property_read_bool(np, "cdns,refclk-ext");
  3651. if (dt_conf)
  3652. bp->caps = dt_conf->caps;
  3653. if (hw_is_gem(bp->regs, bp->native_io)) {
  3654. bp->caps |= MACB_CAPS_MACB_IS_GEM;
  3655. dcfg = gem_readl(bp, DCFG1);
  3656. if (GEM_BFEXT(IRQCOR, dcfg) == 0)
  3657. bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
  3658. if (GEM_BFEXT(NO_PCS, dcfg) == 0)
  3659. bp->caps |= MACB_CAPS_PCS;
  3660. dcfg = gem_readl(bp, DCFG12);
  3661. if (GEM_BFEXT(HIGH_SPEED, dcfg) == 1)
  3662. bp->caps |= MACB_CAPS_HIGH_SPEED;
  3663. dcfg = gem_readl(bp, DCFG2);
  3664. if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
  3665. bp->caps |= MACB_CAPS_FIFO_MODE;
  3666. if (GEM_BFEXT(PBUF_RSC, gem_readl(bp, DCFG6)))
  3667. bp->caps |= MACB_CAPS_RSC;
  3668. if (gem_has_ptp(bp)) {
  3669. if (!GEM_BFEXT(TSU, gem_readl(bp, DCFG5)))
  3670. dev_err(&bp->pdev->dev,
  3671. "GEM doesn't support hardware ptp.\n");
  3672. else {
  3673. #ifdef CONFIG_MACB_USE_HWSTAMP
  3674. bp->caps |= MACB_CAPS_DMA_PTP;
  3675. bp->ptp_info = &gem_ptp_info;
  3676. #endif
  3677. }
  3678. }
  3679. }
  3680. if (refclk_ext)
  3681. bp->caps |= MACB_CAPS_USRIO_HAS_CLKEN;
  3682. dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
  3683. }
  3684. static int macb_probe_queues(struct device *dev, void __iomem *mem, bool native_io)
  3685. {
  3686. /* BIT(0) is never set but queue 0 always exists. */
  3687. unsigned int queue_mask = 0x1;
  3688. /* Use hw_is_gem() as MACB_CAPS_MACB_IS_GEM is not yet positioned. */
  3689. if (hw_is_gem(mem, native_io)) {
  3690. if (native_io)
  3691. queue_mask |= __raw_readl(mem + GEM_DCFG6) & 0xFF;
  3692. else
  3693. queue_mask |= readl_relaxed(mem + GEM_DCFG6) & 0xFF;
  3694. if (fls(queue_mask) != ffz(queue_mask)) {
  3695. dev_err(dev, "queue mask %#x has a hole\n", queue_mask);
  3696. return -EINVAL;
  3697. }
  3698. }
  3699. return hweight32(queue_mask);
  3700. }
  3701. static void macb_clks_disable(struct clk *pclk, struct clk *hclk, struct clk *tx_clk,
  3702. struct clk *rx_clk, struct clk *tsu_clk)
  3703. {
  3704. struct clk_bulk_data clks[] = {
  3705. { .clk = tsu_clk, },
  3706. { .clk = rx_clk, },
  3707. { .clk = pclk, },
  3708. { .clk = hclk, },
  3709. { .clk = tx_clk },
  3710. };
  3711. clk_bulk_disable_unprepare(ARRAY_SIZE(clks), clks);
  3712. }
  3713. static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
  3714. struct clk **hclk, struct clk **tx_clk,
  3715. struct clk **rx_clk, struct clk **tsu_clk)
  3716. {
  3717. struct macb_platform_data *pdata;
  3718. int err;
  3719. pdata = dev_get_platdata(&pdev->dev);
  3720. if (pdata) {
  3721. *pclk = pdata->pclk;
  3722. *hclk = pdata->hclk;
  3723. } else {
  3724. *pclk = devm_clk_get(&pdev->dev, "pclk");
  3725. *hclk = devm_clk_get(&pdev->dev, "hclk");
  3726. }
  3727. if (IS_ERR_OR_NULL(*pclk))
  3728. return dev_err_probe(&pdev->dev,
  3729. IS_ERR(*pclk) ? PTR_ERR(*pclk) : -ENODEV,
  3730. "failed to get pclk\n");
  3731. if (IS_ERR_OR_NULL(*hclk))
  3732. return dev_err_probe(&pdev->dev,
  3733. IS_ERR(*hclk) ? PTR_ERR(*hclk) : -ENODEV,
  3734. "failed to get hclk\n");
  3735. *tx_clk = devm_clk_get_optional(&pdev->dev, "tx_clk");
  3736. if (IS_ERR(*tx_clk))
  3737. return PTR_ERR(*tx_clk);
  3738. *rx_clk = devm_clk_get_optional(&pdev->dev, "rx_clk");
  3739. if (IS_ERR(*rx_clk))
  3740. return PTR_ERR(*rx_clk);
  3741. *tsu_clk = devm_clk_get_optional(&pdev->dev, "tsu_clk");
  3742. if (IS_ERR(*tsu_clk))
  3743. return PTR_ERR(*tsu_clk);
  3744. err = clk_prepare_enable(*pclk);
  3745. if (err) {
  3746. dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
  3747. return err;
  3748. }
  3749. err = clk_prepare_enable(*hclk);
  3750. if (err) {
  3751. dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
  3752. goto err_disable_pclk;
  3753. }
  3754. err = clk_prepare_enable(*tx_clk);
  3755. if (err) {
  3756. dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
  3757. goto err_disable_hclk;
  3758. }
  3759. err = clk_prepare_enable(*rx_clk);
  3760. if (err) {
  3761. dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
  3762. goto err_disable_txclk;
  3763. }
  3764. err = clk_prepare_enable(*tsu_clk);
  3765. if (err) {
  3766. dev_err(&pdev->dev, "failed to enable tsu_clk (%d)\n", err);
  3767. goto err_disable_rxclk;
  3768. }
  3769. return 0;
  3770. err_disable_rxclk:
  3771. clk_disable_unprepare(*rx_clk);
  3772. err_disable_txclk:
  3773. clk_disable_unprepare(*tx_clk);
  3774. err_disable_hclk:
  3775. clk_disable_unprepare(*hclk);
  3776. err_disable_pclk:
  3777. clk_disable_unprepare(*pclk);
  3778. return err;
  3779. }
  3780. static int macb_init(struct platform_device *pdev)
  3781. {
  3782. struct net_device *dev = platform_get_drvdata(pdev);
  3783. unsigned int hw_q, q;
  3784. struct macb *bp = netdev_priv(dev);
  3785. struct macb_queue *queue;
  3786. int err;
  3787. u32 val, reg;
  3788. bp->tx_ring_size = DEFAULT_TX_RING_SIZE;
  3789. bp->rx_ring_size = DEFAULT_RX_RING_SIZE;
  3790. /* set the queue register mapping once for all: queue0 has a special
  3791. * register mapping but we don't want to test the queue index then
  3792. * compute the corresponding register offset at run time.
  3793. */
  3794. for (hw_q = 0, q = 0; hw_q < bp->num_queues; ++hw_q) {
  3795. queue = &bp->queues[q];
  3796. queue->bp = bp;
  3797. spin_lock_init(&queue->tx_ptr_lock);
  3798. netif_napi_add(dev, &queue->napi_rx, macb_rx_poll);
  3799. netif_napi_add(dev, &queue->napi_tx, macb_tx_poll);
  3800. if (hw_q) {
  3801. queue->ISR = GEM_ISR(hw_q - 1);
  3802. queue->IER = GEM_IER(hw_q - 1);
  3803. queue->IDR = GEM_IDR(hw_q - 1);
  3804. queue->IMR = GEM_IMR(hw_q - 1);
  3805. queue->TBQP = GEM_TBQP(hw_q - 1);
  3806. queue->RBQP = GEM_RBQP(hw_q - 1);
  3807. queue->RBQS = GEM_RBQS(hw_q - 1);
  3808. } else {
  3809. /* queue0 uses legacy registers */
  3810. queue->ISR = MACB_ISR;
  3811. queue->IER = MACB_IER;
  3812. queue->IDR = MACB_IDR;
  3813. queue->IMR = MACB_IMR;
  3814. queue->TBQP = MACB_TBQP;
  3815. queue->RBQP = MACB_RBQP;
  3816. }
  3817. queue->ENST_START_TIME = GEM_ENST_START_TIME(hw_q);
  3818. queue->ENST_ON_TIME = GEM_ENST_ON_TIME(hw_q);
  3819. queue->ENST_OFF_TIME = GEM_ENST_OFF_TIME(hw_q);
  3820. /* get irq: here we use the linux queue index, not the hardware
  3821. * queue index. the queue irq definitions in the device tree
  3822. * must remove the optional gaps that could exist in the
  3823. * hardware queue mask.
  3824. */
  3825. queue->irq = platform_get_irq(pdev, q);
  3826. err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
  3827. IRQF_SHARED, dev->name, queue);
  3828. if (err) {
  3829. dev_err(&pdev->dev,
  3830. "Unable to request IRQ %d (error %d)\n",
  3831. queue->irq, err);
  3832. return err;
  3833. }
  3834. INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
  3835. q++;
  3836. }
  3837. dev->netdev_ops = &macb_netdev_ops;
  3838. /* setup appropriated routines according to adapter type */
  3839. if (macb_is_gem(bp)) {
  3840. bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
  3841. bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
  3842. bp->macbgem_ops.mog_init_rings = gem_init_rings;
  3843. bp->macbgem_ops.mog_rx = gem_rx;
  3844. dev->ethtool_ops = &gem_ethtool_ops;
  3845. } else {
  3846. bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
  3847. bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
  3848. bp->macbgem_ops.mog_init_rings = macb_init_rings;
  3849. bp->macbgem_ops.mog_rx = macb_rx;
  3850. dev->ethtool_ops = &macb_ethtool_ops;
  3851. }
  3852. netdev_sw_irq_coalesce_default_on(dev);
  3853. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  3854. /* Set features */
  3855. dev->hw_features = NETIF_F_SG;
  3856. /* Check LSO capability; runtime detection can be overridden by a cap
  3857. * flag if the hardware is known to be buggy
  3858. */
  3859. if (!(bp->caps & MACB_CAPS_NO_LSO) &&
  3860. GEM_BFEXT(PBUF_LSO, gem_readl(bp, DCFG6)))
  3861. dev->hw_features |= MACB_NETIF_LSO;
  3862. /* Checksum offload is only available on gem with packet buffer */
  3863. if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
  3864. dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  3865. if (bp->caps & MACB_CAPS_SG_DISABLED)
  3866. dev->hw_features &= ~NETIF_F_SG;
  3867. /* Enable HW_TC if hardware supports QBV */
  3868. if (bp->caps & MACB_CAPS_QBV)
  3869. dev->hw_features |= NETIF_F_HW_TC;
  3870. dev->features = dev->hw_features;
  3871. /* Check RX Flow Filters support.
  3872. * Max Rx flows set by availability of screeners & compare regs:
  3873. * each 4-tuple define requires 1 T2 screener reg + 3 compare regs
  3874. */
  3875. reg = gem_readl(bp, DCFG8);
  3876. bp->max_tuples = umin((GEM_BFEXT(SCR2CMP, reg) / 3),
  3877. GEM_BFEXT(T2SCR, reg));
  3878. INIT_LIST_HEAD(&bp->rx_fs_list.list);
  3879. if (bp->max_tuples > 0) {
  3880. /* also needs one ethtype match to check IPv4 */
  3881. if (GEM_BFEXT(SCR2ETH, reg) > 0) {
  3882. /* program this reg now */
  3883. reg = 0;
  3884. reg = GEM_BFINS(ETHTCMP, (uint16_t)ETH_P_IP, reg);
  3885. gem_writel_n(bp, ETHT, SCRT2_ETHT, reg);
  3886. /* Filtering is supported in hw but don't enable it in kernel now */
  3887. dev->hw_features |= NETIF_F_NTUPLE;
  3888. /* init Rx flow definitions */
  3889. bp->rx_fs_list.count = 0;
  3890. spin_lock_init(&bp->rx_fs_lock);
  3891. } else
  3892. bp->max_tuples = 0;
  3893. }
  3894. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
  3895. val = 0;
  3896. if (phy_interface_mode_is_rgmii(bp->phy_interface))
  3897. val = bp->usrio->rgmii;
  3898. else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
  3899. (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  3900. val = bp->usrio->rmii;
  3901. else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
  3902. val = bp->usrio->mii;
  3903. if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
  3904. val |= bp->usrio->refclk;
  3905. macb_or_gem_writel(bp, USRIO, val);
  3906. }
  3907. /* Set MII management clock divider */
  3908. val = macb_mdc_clk_div(bp);
  3909. val |= macb_dbw(bp);
  3910. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
  3911. val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
  3912. macb_writel(bp, NCFGR, val);
  3913. return 0;
  3914. }
  3915. static const struct macb_usrio_config macb_default_usrio = {
  3916. .mii = MACB_BIT(MII),
  3917. .rmii = MACB_BIT(RMII),
  3918. .rgmii = GEM_BIT(RGMII),
  3919. .refclk = MACB_BIT(CLKEN),
  3920. };
  3921. #if defined(CONFIG_OF)
  3922. /* 1518 rounded up */
  3923. #define AT91ETHER_MAX_RBUFF_SZ 0x600
  3924. /* max number of receive buffers */
  3925. #define AT91ETHER_MAX_RX_DESCR 9
  3926. static struct sifive_fu540_macb_mgmt *mgmt;
  3927. static int at91ether_alloc_coherent(struct macb *lp)
  3928. {
  3929. struct macb_queue *q = &lp->queues[0];
  3930. q->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
  3931. (AT91ETHER_MAX_RX_DESCR *
  3932. macb_dma_desc_get_size(lp)),
  3933. &q->rx_ring_dma, GFP_KERNEL);
  3934. if (!q->rx_ring)
  3935. return -ENOMEM;
  3936. q->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
  3937. AT91ETHER_MAX_RX_DESCR *
  3938. AT91ETHER_MAX_RBUFF_SZ,
  3939. &q->rx_buffers_dma, GFP_KERNEL);
  3940. if (!q->rx_buffers) {
  3941. dma_free_coherent(&lp->pdev->dev,
  3942. AT91ETHER_MAX_RX_DESCR *
  3943. macb_dma_desc_get_size(lp),
  3944. q->rx_ring, q->rx_ring_dma);
  3945. q->rx_ring = NULL;
  3946. return -ENOMEM;
  3947. }
  3948. return 0;
  3949. }
  3950. static void at91ether_free_coherent(struct macb *lp)
  3951. {
  3952. struct macb_queue *q = &lp->queues[0];
  3953. if (q->rx_ring) {
  3954. dma_free_coherent(&lp->pdev->dev,
  3955. AT91ETHER_MAX_RX_DESCR *
  3956. macb_dma_desc_get_size(lp),
  3957. q->rx_ring, q->rx_ring_dma);
  3958. q->rx_ring = NULL;
  3959. }
  3960. if (q->rx_buffers) {
  3961. dma_free_coherent(&lp->pdev->dev,
  3962. AT91ETHER_MAX_RX_DESCR *
  3963. AT91ETHER_MAX_RBUFF_SZ,
  3964. q->rx_buffers, q->rx_buffers_dma);
  3965. q->rx_buffers = NULL;
  3966. }
  3967. }
  3968. /* Initialize and start the Receiver and Transmit subsystems */
  3969. static int at91ether_start(struct macb *lp)
  3970. {
  3971. struct macb_queue *q = &lp->queues[0];
  3972. struct macb_dma_desc *desc;
  3973. dma_addr_t addr;
  3974. u32 ctl;
  3975. int i, ret;
  3976. ret = at91ether_alloc_coherent(lp);
  3977. if (ret)
  3978. return ret;
  3979. addr = q->rx_buffers_dma;
  3980. for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
  3981. desc = macb_rx_desc(q, i);
  3982. macb_set_addr(lp, desc, addr);
  3983. desc->ctrl = 0;
  3984. addr += AT91ETHER_MAX_RBUFF_SZ;
  3985. }
  3986. /* Set the Wrap bit on the last descriptor */
  3987. desc->addr |= MACB_BIT(RX_WRAP);
  3988. /* Reset buffer index */
  3989. q->rx_tail = 0;
  3990. /* Program address of descriptor list in Rx Buffer Queue register */
  3991. macb_writel(lp, RBQP, q->rx_ring_dma);
  3992. /* Enable Receive and Transmit */
  3993. ctl = macb_readl(lp, NCR);
  3994. macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
  3995. /* Enable MAC interrupts */
  3996. macb_writel(lp, IER, MACB_BIT(RCOMP) |
  3997. MACB_BIT(RXUBR) |
  3998. MACB_BIT(ISR_TUND) |
  3999. MACB_BIT(ISR_RLE) |
  4000. MACB_BIT(TCOMP) |
  4001. MACB_BIT(ISR_ROVR) |
  4002. MACB_BIT(HRESP));
  4003. return 0;
  4004. }
  4005. static void at91ether_stop(struct macb *lp)
  4006. {
  4007. u32 ctl;
  4008. /* Disable MAC interrupts */
  4009. macb_writel(lp, IDR, MACB_BIT(RCOMP) |
  4010. MACB_BIT(RXUBR) |
  4011. MACB_BIT(ISR_TUND) |
  4012. MACB_BIT(ISR_RLE) |
  4013. MACB_BIT(TCOMP) |
  4014. MACB_BIT(ISR_ROVR) |
  4015. MACB_BIT(HRESP));
  4016. /* Disable Receiver and Transmitter */
  4017. ctl = macb_readl(lp, NCR);
  4018. macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  4019. /* Free resources. */
  4020. at91ether_free_coherent(lp);
  4021. }
  4022. /* Open the ethernet interface */
  4023. static int at91ether_open(struct net_device *dev)
  4024. {
  4025. struct macb *lp = netdev_priv(dev);
  4026. u32 ctl;
  4027. int ret;
  4028. ret = pm_runtime_resume_and_get(&lp->pdev->dev);
  4029. if (ret < 0)
  4030. return ret;
  4031. /* Clear internal statistics */
  4032. ctl = macb_readl(lp, NCR);
  4033. macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
  4034. macb_set_hwaddr(lp);
  4035. ret = at91ether_start(lp);
  4036. if (ret)
  4037. goto pm_exit;
  4038. ret = macb_phylink_connect(lp);
  4039. if (ret)
  4040. goto stop;
  4041. netif_start_queue(dev);
  4042. return 0;
  4043. stop:
  4044. at91ether_stop(lp);
  4045. pm_exit:
  4046. pm_runtime_put_sync(&lp->pdev->dev);
  4047. return ret;
  4048. }
  4049. /* Close the interface */
  4050. static int at91ether_close(struct net_device *dev)
  4051. {
  4052. struct macb *lp = netdev_priv(dev);
  4053. netif_stop_queue(dev);
  4054. phylink_stop(lp->phylink);
  4055. phylink_disconnect_phy(lp->phylink);
  4056. at91ether_stop(lp);
  4057. pm_runtime_put(&lp->pdev->dev);
  4058. return 0;
  4059. }
  4060. /* Transmit packet */
  4061. static netdev_tx_t at91ether_start_xmit(struct sk_buff *skb,
  4062. struct net_device *dev)
  4063. {
  4064. struct macb *lp = netdev_priv(dev);
  4065. if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
  4066. int desc = 0;
  4067. netif_stop_queue(dev);
  4068. /* Store packet information (to free when Tx completed) */
  4069. lp->rm9200_txq[desc].skb = skb;
  4070. lp->rm9200_txq[desc].size = skb->len;
  4071. lp->rm9200_txq[desc].mapping = dma_map_single(&lp->pdev->dev, skb->data,
  4072. skb->len, DMA_TO_DEVICE);
  4073. if (dma_mapping_error(&lp->pdev->dev, lp->rm9200_txq[desc].mapping)) {
  4074. dev_kfree_skb_any(skb);
  4075. dev->stats.tx_dropped++;
  4076. netdev_err(dev, "%s: DMA mapping error\n", __func__);
  4077. return NETDEV_TX_OK;
  4078. }
  4079. /* Set address of the data in the Transmit Address register */
  4080. macb_writel(lp, TAR, lp->rm9200_txq[desc].mapping);
  4081. /* Set length of the packet in the Transmit Control register */
  4082. macb_writel(lp, TCR, skb->len);
  4083. } else {
  4084. netdev_err(dev, "%s called, but device is busy!\n", __func__);
  4085. return NETDEV_TX_BUSY;
  4086. }
  4087. return NETDEV_TX_OK;
  4088. }
  4089. /* Extract received frame from buffer descriptors and sent to upper layers.
  4090. * (Called from interrupt context)
  4091. */
  4092. static void at91ether_rx(struct net_device *dev)
  4093. {
  4094. struct macb *lp = netdev_priv(dev);
  4095. struct macb_queue *q = &lp->queues[0];
  4096. struct macb_dma_desc *desc;
  4097. unsigned char *p_recv;
  4098. struct sk_buff *skb;
  4099. unsigned int pktlen;
  4100. desc = macb_rx_desc(q, q->rx_tail);
  4101. while (desc->addr & MACB_BIT(RX_USED)) {
  4102. p_recv = q->rx_buffers + q->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
  4103. pktlen = MACB_BF(RX_FRMLEN, desc->ctrl);
  4104. skb = netdev_alloc_skb(dev, pktlen + 2);
  4105. if (skb) {
  4106. skb_reserve(skb, 2);
  4107. skb_put_data(skb, p_recv, pktlen);
  4108. skb->protocol = eth_type_trans(skb, dev);
  4109. dev->stats.rx_packets++;
  4110. dev->stats.rx_bytes += pktlen;
  4111. netif_rx(skb);
  4112. } else {
  4113. dev->stats.rx_dropped++;
  4114. }
  4115. if (desc->ctrl & MACB_BIT(RX_MHASH_MATCH))
  4116. dev->stats.multicast++;
  4117. /* reset ownership bit */
  4118. desc->addr &= ~MACB_BIT(RX_USED);
  4119. /* wrap after last buffer */
  4120. if (q->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
  4121. q->rx_tail = 0;
  4122. else
  4123. q->rx_tail++;
  4124. desc = macb_rx_desc(q, q->rx_tail);
  4125. }
  4126. }
  4127. /* MAC interrupt handler */
  4128. static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
  4129. {
  4130. struct net_device *dev = dev_id;
  4131. struct macb *lp = netdev_priv(dev);
  4132. u32 intstatus, ctl;
  4133. unsigned int desc;
  4134. /* MAC Interrupt Status register indicates what interrupts are pending.
  4135. * It is automatically cleared once read.
  4136. */
  4137. intstatus = macb_readl(lp, ISR);
  4138. /* Receive complete */
  4139. if (intstatus & MACB_BIT(RCOMP))
  4140. at91ether_rx(dev);
  4141. /* Transmit complete */
  4142. if (intstatus & MACB_BIT(TCOMP)) {
  4143. /* The TCOM bit is set even if the transmission failed */
  4144. if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
  4145. dev->stats.tx_errors++;
  4146. desc = 0;
  4147. if (lp->rm9200_txq[desc].skb) {
  4148. dev_consume_skb_irq(lp->rm9200_txq[desc].skb);
  4149. lp->rm9200_txq[desc].skb = NULL;
  4150. dma_unmap_single(&lp->pdev->dev, lp->rm9200_txq[desc].mapping,
  4151. lp->rm9200_txq[desc].size, DMA_TO_DEVICE);
  4152. dev->stats.tx_packets++;
  4153. dev->stats.tx_bytes += lp->rm9200_txq[desc].size;
  4154. }
  4155. netif_wake_queue(dev);
  4156. }
  4157. /* Work-around for EMAC Errata section 41.3.1 */
  4158. if (intstatus & MACB_BIT(RXUBR)) {
  4159. ctl = macb_readl(lp, NCR);
  4160. macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
  4161. wmb();
  4162. macb_writel(lp, NCR, ctl | MACB_BIT(RE));
  4163. }
  4164. if (intstatus & MACB_BIT(ISR_ROVR))
  4165. netdev_err(dev, "ROVR error\n");
  4166. return IRQ_HANDLED;
  4167. }
  4168. #ifdef CONFIG_NET_POLL_CONTROLLER
  4169. static void at91ether_poll_controller(struct net_device *dev)
  4170. {
  4171. unsigned long flags;
  4172. local_irq_save(flags);
  4173. at91ether_interrupt(dev->irq, dev);
  4174. local_irq_restore(flags);
  4175. }
  4176. #endif
  4177. static const struct net_device_ops at91ether_netdev_ops = {
  4178. .ndo_open = at91ether_open,
  4179. .ndo_stop = at91ether_close,
  4180. .ndo_start_xmit = at91ether_start_xmit,
  4181. .ndo_get_stats64 = macb_get_stats,
  4182. .ndo_set_rx_mode = macb_set_rx_mode,
  4183. .ndo_set_mac_address = eth_mac_addr,
  4184. .ndo_eth_ioctl = macb_ioctl,
  4185. .ndo_validate_addr = eth_validate_addr,
  4186. #ifdef CONFIG_NET_POLL_CONTROLLER
  4187. .ndo_poll_controller = at91ether_poll_controller,
  4188. #endif
  4189. .ndo_hwtstamp_set = macb_hwtstamp_set,
  4190. .ndo_hwtstamp_get = macb_hwtstamp_get,
  4191. };
  4192. static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
  4193. struct clk **hclk, struct clk **tx_clk,
  4194. struct clk **rx_clk, struct clk **tsu_clk)
  4195. {
  4196. int err;
  4197. *hclk = NULL;
  4198. *tx_clk = NULL;
  4199. *rx_clk = NULL;
  4200. *tsu_clk = NULL;
  4201. *pclk = devm_clk_get(&pdev->dev, "ether_clk");
  4202. if (IS_ERR(*pclk))
  4203. return PTR_ERR(*pclk);
  4204. err = clk_prepare_enable(*pclk);
  4205. if (err) {
  4206. dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
  4207. return err;
  4208. }
  4209. return 0;
  4210. }
  4211. static int at91ether_init(struct platform_device *pdev)
  4212. {
  4213. struct net_device *dev = platform_get_drvdata(pdev);
  4214. struct macb *bp = netdev_priv(dev);
  4215. int err;
  4216. bp->queues[0].bp = bp;
  4217. dev->netdev_ops = &at91ether_netdev_ops;
  4218. dev->ethtool_ops = &macb_ethtool_ops;
  4219. err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
  4220. 0, dev->name, dev);
  4221. if (err)
  4222. return err;
  4223. macb_writel(bp, NCR, 0);
  4224. macb_writel(bp, NCFGR, MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG));
  4225. return 0;
  4226. }
  4227. static unsigned long fu540_macb_tx_recalc_rate(struct clk_hw *hw,
  4228. unsigned long parent_rate)
  4229. {
  4230. return mgmt->rate;
  4231. }
  4232. static int fu540_macb_tx_determine_rate(struct clk_hw *hw,
  4233. struct clk_rate_request *req)
  4234. {
  4235. if (WARN_ON(req->rate < 2500000))
  4236. req->rate = 2500000;
  4237. else if (req->rate == 2500000)
  4238. req->rate = 2500000;
  4239. else if (WARN_ON(req->rate < 13750000))
  4240. req->rate = 2500000;
  4241. else if (WARN_ON(req->rate < 25000000))
  4242. req->rate = 25000000;
  4243. else if (req->rate == 25000000)
  4244. req->rate = 25000000;
  4245. else if (WARN_ON(req->rate < 75000000))
  4246. req->rate = 25000000;
  4247. else if (WARN_ON(req->rate < 125000000))
  4248. req->rate = 125000000;
  4249. else if (req->rate == 125000000)
  4250. req->rate = 125000000;
  4251. else if (WARN_ON(req->rate > 125000000))
  4252. req->rate = 125000000;
  4253. else
  4254. req->rate = 125000000;
  4255. return 0;
  4256. }
  4257. static int fu540_macb_tx_set_rate(struct clk_hw *hw, unsigned long rate,
  4258. unsigned long parent_rate)
  4259. {
  4260. struct clk_rate_request req;
  4261. int ret;
  4262. clk_hw_init_rate_request(hw, &req, rate);
  4263. ret = fu540_macb_tx_determine_rate(hw, &req);
  4264. if (ret != 0)
  4265. return ret;
  4266. if (req.rate != 125000000)
  4267. iowrite32(1, mgmt->reg);
  4268. else
  4269. iowrite32(0, mgmt->reg);
  4270. mgmt->rate = rate;
  4271. return 0;
  4272. }
  4273. static const struct clk_ops fu540_c000_ops = {
  4274. .recalc_rate = fu540_macb_tx_recalc_rate,
  4275. .determine_rate = fu540_macb_tx_determine_rate,
  4276. .set_rate = fu540_macb_tx_set_rate,
  4277. };
  4278. static int fu540_c000_clk_init(struct platform_device *pdev, struct clk **pclk,
  4279. struct clk **hclk, struct clk **tx_clk,
  4280. struct clk **rx_clk, struct clk **tsu_clk)
  4281. {
  4282. struct clk_init_data init;
  4283. int err = 0;
  4284. err = macb_clk_init(pdev, pclk, hclk, tx_clk, rx_clk, tsu_clk);
  4285. if (err)
  4286. return err;
  4287. mgmt = devm_kzalloc(&pdev->dev, sizeof(*mgmt), GFP_KERNEL);
  4288. if (!mgmt) {
  4289. err = -ENOMEM;
  4290. goto err_disable_clks;
  4291. }
  4292. init.name = "sifive-gemgxl-mgmt";
  4293. init.ops = &fu540_c000_ops;
  4294. init.flags = 0;
  4295. init.num_parents = 0;
  4296. mgmt->rate = 0;
  4297. mgmt->hw.init = &init;
  4298. *tx_clk = devm_clk_register(&pdev->dev, &mgmt->hw);
  4299. if (IS_ERR(*tx_clk)) {
  4300. err = PTR_ERR(*tx_clk);
  4301. goto err_disable_clks;
  4302. }
  4303. err = clk_prepare_enable(*tx_clk);
  4304. if (err) {
  4305. dev_err(&pdev->dev, "failed to enable tx_clk (%u)\n", err);
  4306. *tx_clk = NULL;
  4307. goto err_disable_clks;
  4308. } else {
  4309. dev_info(&pdev->dev, "Registered clk switch '%s'\n", init.name);
  4310. }
  4311. return 0;
  4312. err_disable_clks:
  4313. macb_clks_disable(*pclk, *hclk, *tx_clk, *rx_clk, *tsu_clk);
  4314. return err;
  4315. }
  4316. static int fu540_c000_init(struct platform_device *pdev)
  4317. {
  4318. mgmt->reg = devm_platform_ioremap_resource(pdev, 1);
  4319. if (IS_ERR(mgmt->reg))
  4320. return PTR_ERR(mgmt->reg);
  4321. return macb_init(pdev);
  4322. }
  4323. static int init_reset_optional(struct platform_device *pdev)
  4324. {
  4325. struct net_device *dev = platform_get_drvdata(pdev);
  4326. struct macb *bp = netdev_priv(dev);
  4327. int ret;
  4328. if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII) {
  4329. /* Ensure PHY device used in SGMII mode is ready */
  4330. bp->phy = devm_phy_optional_get(&pdev->dev, NULL);
  4331. if (IS_ERR(bp->phy))
  4332. return dev_err_probe(&pdev->dev, PTR_ERR(bp->phy),
  4333. "failed to get SGMII PHY\n");
  4334. ret = phy_init(bp->phy);
  4335. if (ret)
  4336. return dev_err_probe(&pdev->dev, ret,
  4337. "failed to init SGMII PHY\n");
  4338. ret = zynqmp_pm_is_function_supported(PM_IOCTL, IOCTL_SET_GEM_CONFIG);
  4339. if (!ret) {
  4340. u32 pm_info[2];
  4341. ret = of_property_read_u32_array(pdev->dev.of_node, "power-domains",
  4342. pm_info, ARRAY_SIZE(pm_info));
  4343. if (ret) {
  4344. dev_err(&pdev->dev, "Failed to read power management information\n");
  4345. goto err_out_phy_exit;
  4346. }
  4347. ret = zynqmp_pm_set_gem_config(pm_info[1], GEM_CONFIG_FIXED, 0);
  4348. if (ret)
  4349. goto err_out_phy_exit;
  4350. ret = zynqmp_pm_set_gem_config(pm_info[1], GEM_CONFIG_SGMII_MODE, 1);
  4351. if (ret)
  4352. goto err_out_phy_exit;
  4353. }
  4354. }
  4355. /* Fully reset controller at hardware level if mapped in device tree */
  4356. ret = device_reset_optional(&pdev->dev);
  4357. if (ret) {
  4358. phy_exit(bp->phy);
  4359. return dev_err_probe(&pdev->dev, ret, "failed to reset controller");
  4360. }
  4361. ret = macb_init(pdev);
  4362. err_out_phy_exit:
  4363. if (ret)
  4364. phy_exit(bp->phy);
  4365. return ret;
  4366. }
  4367. static int eyeq5_init(struct platform_device *pdev)
  4368. {
  4369. struct net_device *netdev = platform_get_drvdata(pdev);
  4370. struct macb *bp = netdev_priv(netdev);
  4371. struct device *dev = &pdev->dev;
  4372. int ret;
  4373. bp->phy = devm_phy_get(dev, NULL);
  4374. if (IS_ERR(bp->phy))
  4375. return dev_err_probe(dev, PTR_ERR(bp->phy),
  4376. "failed to get PHY\n");
  4377. ret = phy_init(bp->phy);
  4378. if (ret)
  4379. return dev_err_probe(dev, ret, "failed to init PHY\n");
  4380. ret = macb_init(pdev);
  4381. if (ret)
  4382. phy_exit(bp->phy);
  4383. return ret;
  4384. }
  4385. static const struct macb_usrio_config sama7g5_usrio = {
  4386. .mii = 0,
  4387. .rmii = 1,
  4388. .rgmii = 2,
  4389. .refclk = BIT(2),
  4390. .hdfctlen = BIT(6),
  4391. };
  4392. static const struct macb_config fu540_c000_config = {
  4393. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
  4394. MACB_CAPS_GEM_HAS_PTP,
  4395. .dma_burst_length = 16,
  4396. .clk_init = fu540_c000_clk_init,
  4397. .init = fu540_c000_init,
  4398. .jumbo_max_len = 10240,
  4399. .usrio = &macb_default_usrio,
  4400. };
  4401. static const struct macb_config at91sam9260_config = {
  4402. .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  4403. .clk_init = macb_clk_init,
  4404. .init = macb_init,
  4405. .usrio = &macb_default_usrio,
  4406. };
  4407. static const struct macb_config sama5d3macb_config = {
  4408. .caps = MACB_CAPS_SG_DISABLED |
  4409. MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  4410. .clk_init = macb_clk_init,
  4411. .init = macb_init,
  4412. .usrio = &macb_default_usrio,
  4413. };
  4414. static const struct macb_config pc302gem_config = {
  4415. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
  4416. .dma_burst_length = 16,
  4417. .clk_init = macb_clk_init,
  4418. .init = macb_init,
  4419. .usrio = &macb_default_usrio,
  4420. };
  4421. static const struct macb_config sama5d2_config = {
  4422. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
  4423. .dma_burst_length = 16,
  4424. .clk_init = macb_clk_init,
  4425. .init = macb_init,
  4426. .jumbo_max_len = 10240,
  4427. .usrio = &macb_default_usrio,
  4428. };
  4429. static const struct macb_config sama5d29_config = {
  4430. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_GEM_HAS_PTP,
  4431. .dma_burst_length = 16,
  4432. .clk_init = macb_clk_init,
  4433. .init = macb_init,
  4434. .usrio = &macb_default_usrio,
  4435. };
  4436. static const struct macb_config sama5d3_config = {
  4437. .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  4438. MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII | MACB_CAPS_JUMBO,
  4439. .dma_burst_length = 16,
  4440. .clk_init = macb_clk_init,
  4441. .init = macb_init,
  4442. .jumbo_max_len = 10240,
  4443. .usrio = &macb_default_usrio,
  4444. };
  4445. static const struct macb_config sama5d4_config = {
  4446. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
  4447. .dma_burst_length = 4,
  4448. .clk_init = macb_clk_init,
  4449. .init = macb_init,
  4450. .usrio = &macb_default_usrio,
  4451. };
  4452. static const struct macb_config emac_config = {
  4453. .caps = MACB_CAPS_NEEDS_RSTONUBR | MACB_CAPS_MACB_IS_EMAC,
  4454. .clk_init = at91ether_clk_init,
  4455. .init = at91ether_init,
  4456. .usrio = &macb_default_usrio,
  4457. };
  4458. static const struct macb_config np4_config = {
  4459. .caps = MACB_CAPS_USRIO_DISABLED,
  4460. .clk_init = macb_clk_init,
  4461. .init = macb_init,
  4462. .usrio = &macb_default_usrio,
  4463. };
  4464. static const struct macb_config zynqmp_config = {
  4465. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  4466. MACB_CAPS_JUMBO |
  4467. MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH,
  4468. .dma_burst_length = 16,
  4469. .clk_init = macb_clk_init,
  4470. .init = init_reset_optional,
  4471. .jumbo_max_len = 10240,
  4472. .usrio = &macb_default_usrio,
  4473. };
  4474. static const struct macb_config zynq_config = {
  4475. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF |
  4476. MACB_CAPS_NEEDS_RSTONUBR,
  4477. .dma_burst_length = 16,
  4478. .clk_init = macb_clk_init,
  4479. .init = macb_init,
  4480. .usrio = &macb_default_usrio,
  4481. };
  4482. static const struct macb_config mpfs_config = {
  4483. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  4484. MACB_CAPS_JUMBO |
  4485. MACB_CAPS_GEM_HAS_PTP,
  4486. .dma_burst_length = 16,
  4487. .clk_init = macb_clk_init,
  4488. .init = init_reset_optional,
  4489. .usrio = &macb_default_usrio,
  4490. .max_tx_length = 4040, /* Cadence Erratum 1686 */
  4491. .jumbo_max_len = 4040,
  4492. };
  4493. static const struct macb_config sama7g5_gem_config = {
  4494. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
  4495. MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
  4496. MACB_CAPS_MIIONRGMII | MACB_CAPS_GEM_HAS_PTP,
  4497. .dma_burst_length = 16,
  4498. .clk_init = macb_clk_init,
  4499. .init = macb_init,
  4500. .usrio = &sama7g5_usrio,
  4501. };
  4502. static const struct macb_config sama7g5_emac_config = {
  4503. .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII |
  4504. MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_MIIONRGMII |
  4505. MACB_CAPS_GEM_HAS_PTP,
  4506. .dma_burst_length = 16,
  4507. .clk_init = macb_clk_init,
  4508. .init = macb_init,
  4509. .usrio = &sama7g5_usrio,
  4510. };
  4511. static const struct macb_config versal_config = {
  4512. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
  4513. MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_BD_RD_PREFETCH |
  4514. MACB_CAPS_NEED_TSUCLK | MACB_CAPS_QUEUE_DISABLE |
  4515. MACB_CAPS_QBV,
  4516. .dma_burst_length = 16,
  4517. .clk_init = macb_clk_init,
  4518. .init = init_reset_optional,
  4519. .jumbo_max_len = 10240,
  4520. .usrio = &macb_default_usrio,
  4521. };
  4522. static const struct macb_config eyeq5_config = {
  4523. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO |
  4524. MACB_CAPS_GEM_HAS_PTP | MACB_CAPS_QUEUE_DISABLE |
  4525. MACB_CAPS_NO_LSO,
  4526. .dma_burst_length = 16,
  4527. .clk_init = macb_clk_init,
  4528. .init = eyeq5_init,
  4529. .jumbo_max_len = 10240,
  4530. .usrio = &macb_default_usrio,
  4531. };
  4532. static const struct macb_config raspberrypi_rp1_config = {
  4533. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_CLK_HW_CHG |
  4534. MACB_CAPS_JUMBO |
  4535. MACB_CAPS_GEM_HAS_PTP,
  4536. .dma_burst_length = 16,
  4537. .clk_init = macb_clk_init,
  4538. .init = macb_init,
  4539. .usrio = &macb_default_usrio,
  4540. .jumbo_max_len = 10240,
  4541. };
  4542. static const struct of_device_id macb_dt_ids[] = {
  4543. { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
  4544. { .compatible = "cdns,macb" },
  4545. { .compatible = "cdns,np4-macb", .data = &np4_config },
  4546. { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
  4547. { .compatible = "cdns,gem", .data = &pc302gem_config },
  4548. { .compatible = "cdns,sam9x60-macb", .data = &at91sam9260_config },
  4549. { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
  4550. { .compatible = "atmel,sama5d29-gem", .data = &sama5d29_config },
  4551. { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
  4552. { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
  4553. { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
  4554. { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
  4555. { .compatible = "cdns,emac", .data = &emac_config },
  4556. { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config}, /* deprecated */
  4557. { .compatible = "cdns,zynq-gem", .data = &zynq_config }, /* deprecated */
  4558. { .compatible = "sifive,fu540-c000-gem", .data = &fu540_c000_config },
  4559. { .compatible = "microchip,mpfs-macb", .data = &mpfs_config },
  4560. { .compatible = "microchip,sama7g5-gem", .data = &sama7g5_gem_config },
  4561. { .compatible = "microchip,sama7g5-emac", .data = &sama7g5_emac_config },
  4562. { .compatible = "mobileye,eyeq5-gem", .data = &eyeq5_config },
  4563. { .compatible = "raspberrypi,rp1-gem", .data = &raspberrypi_rp1_config },
  4564. { .compatible = "xlnx,zynqmp-gem", .data = &zynqmp_config},
  4565. { .compatible = "xlnx,zynq-gem", .data = &zynq_config },
  4566. { .compatible = "xlnx,versal-gem", .data = &versal_config},
  4567. { /* sentinel */ }
  4568. };
  4569. MODULE_DEVICE_TABLE(of, macb_dt_ids);
  4570. #endif /* CONFIG_OF */
  4571. static const struct macb_config default_gem_config = {
  4572. .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE |
  4573. MACB_CAPS_JUMBO |
  4574. MACB_CAPS_GEM_HAS_PTP,
  4575. .dma_burst_length = 16,
  4576. .clk_init = macb_clk_init,
  4577. .init = macb_init,
  4578. .usrio = &macb_default_usrio,
  4579. .jumbo_max_len = 10240,
  4580. };
  4581. static int macb_probe(struct platform_device *pdev)
  4582. {
  4583. struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
  4584. struct device_node *np = pdev->dev.of_node;
  4585. const struct macb_config *macb_config;
  4586. struct clk *tsu_clk = NULL;
  4587. phy_interface_t interface;
  4588. struct net_device *dev;
  4589. struct resource *regs;
  4590. u32 wtrmrk_rst_val;
  4591. void __iomem *mem;
  4592. struct macb *bp;
  4593. int num_queues;
  4594. bool native_io;
  4595. int err, val;
  4596. mem = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
  4597. if (IS_ERR(mem))
  4598. return PTR_ERR(mem);
  4599. macb_config = of_device_get_match_data(&pdev->dev);
  4600. if (!macb_config)
  4601. macb_config = &default_gem_config;
  4602. err = macb_config->clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk, &tsu_clk);
  4603. if (err)
  4604. return err;
  4605. pm_runtime_set_autosuspend_delay(&pdev->dev, MACB_PM_TIMEOUT);
  4606. pm_runtime_use_autosuspend(&pdev->dev);
  4607. pm_runtime_get_noresume(&pdev->dev);
  4608. pm_runtime_set_active(&pdev->dev);
  4609. pm_runtime_enable(&pdev->dev);
  4610. native_io = hw_is_native_io(mem);
  4611. num_queues = macb_probe_queues(&pdev->dev, mem, native_io);
  4612. if (num_queues < 0) {
  4613. err = num_queues;
  4614. goto err_disable_clocks;
  4615. }
  4616. dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
  4617. if (!dev) {
  4618. err = -ENOMEM;
  4619. goto err_disable_clocks;
  4620. }
  4621. dev->base_addr = regs->start;
  4622. SET_NETDEV_DEV(dev, &pdev->dev);
  4623. bp = netdev_priv(dev);
  4624. bp->pdev = pdev;
  4625. bp->dev = dev;
  4626. bp->regs = mem;
  4627. bp->native_io = native_io;
  4628. if (native_io) {
  4629. bp->macb_reg_readl = hw_readl_native;
  4630. bp->macb_reg_writel = hw_writel_native;
  4631. } else {
  4632. bp->macb_reg_readl = hw_readl;
  4633. bp->macb_reg_writel = hw_writel;
  4634. }
  4635. bp->num_queues = num_queues;
  4636. bp->dma_burst_length = macb_config->dma_burst_length;
  4637. bp->pclk = pclk;
  4638. bp->hclk = hclk;
  4639. bp->tx_clk = tx_clk;
  4640. bp->rx_clk = rx_clk;
  4641. bp->tsu_clk = tsu_clk;
  4642. bp->jumbo_max_len = macb_config->jumbo_max_len;
  4643. if (!hw_is_gem(bp->regs, bp->native_io))
  4644. bp->max_tx_length = MACB_MAX_TX_LEN;
  4645. else if (macb_config->max_tx_length)
  4646. bp->max_tx_length = macb_config->max_tx_length;
  4647. else
  4648. bp->max_tx_length = GEM_MAX_TX_LEN;
  4649. bp->wol = 0;
  4650. device_set_wakeup_capable(&pdev->dev, 1);
  4651. bp->usrio = macb_config->usrio;
  4652. /* By default we set to partial store and forward mode for zynqmp.
  4653. * Disable if not set in devicetree.
  4654. */
  4655. if (GEM_BFEXT(PBUF_CUTTHRU, gem_readl(bp, DCFG6))) {
  4656. err = of_property_read_u32(bp->pdev->dev.of_node,
  4657. "cdns,rx-watermark",
  4658. &bp->rx_watermark);
  4659. if (!err) {
  4660. /* Disable partial store and forward in case of error or
  4661. * invalid watermark value
  4662. */
  4663. wtrmrk_rst_val = (1 << (GEM_BFEXT(RX_PBUF_ADDR, gem_readl(bp, DCFG2)))) - 1;
  4664. if (bp->rx_watermark > wtrmrk_rst_val || !bp->rx_watermark) {
  4665. dev_info(&bp->pdev->dev, "Invalid watermark value\n");
  4666. bp->rx_watermark = 0;
  4667. }
  4668. }
  4669. }
  4670. spin_lock_init(&bp->lock);
  4671. spin_lock_init(&bp->stats_lock);
  4672. /* setup capabilities */
  4673. macb_configure_caps(bp, macb_config);
  4674. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  4675. if (GEM_BFEXT(DAW64, gem_readl(bp, DCFG6))) {
  4676. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
  4677. if (err) {
  4678. dev_err(&pdev->dev, "failed to set DMA mask\n");
  4679. goto err_out_free_netdev;
  4680. }
  4681. bp->caps |= MACB_CAPS_DMA_64B;
  4682. }
  4683. #endif
  4684. platform_set_drvdata(pdev, dev);
  4685. dev->irq = platform_get_irq(pdev, 0);
  4686. if (dev->irq < 0) {
  4687. err = dev->irq;
  4688. goto err_out_free_netdev;
  4689. }
  4690. /* MTU range: 68 - 1518 or 10240 */
  4691. dev->min_mtu = GEM_MTU_MIN_SIZE;
  4692. if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
  4693. dev->max_mtu = bp->jumbo_max_len - ETH_HLEN - ETH_FCS_LEN;
  4694. else
  4695. dev->max_mtu = 1536 - ETH_HLEN - ETH_FCS_LEN;
  4696. if (bp->caps & MACB_CAPS_BD_RD_PREFETCH) {
  4697. val = GEM_BFEXT(RXBD_RDBUFF, gem_readl(bp, DCFG10));
  4698. if (val)
  4699. bp->rx_bd_rd_prefetch = (2 << (val - 1)) *
  4700. macb_dma_desc_get_size(bp);
  4701. val = GEM_BFEXT(TXBD_RDBUFF, gem_readl(bp, DCFG10));
  4702. if (val)
  4703. bp->tx_bd_rd_prefetch = (2 << (val - 1)) *
  4704. macb_dma_desc_get_size(bp);
  4705. }
  4706. bp->rx_intr_mask = MACB_RX_INT_FLAGS;
  4707. if (bp->caps & MACB_CAPS_NEEDS_RSTONUBR)
  4708. bp->rx_intr_mask |= MACB_BIT(RXUBR);
  4709. err = of_get_ethdev_address(np, bp->dev);
  4710. if (err == -EPROBE_DEFER)
  4711. goto err_out_free_netdev;
  4712. else if (err)
  4713. macb_get_hwaddr(bp);
  4714. err = of_get_phy_mode(np, &interface);
  4715. if (err)
  4716. /* not found in DT, MII by default */
  4717. bp->phy_interface = PHY_INTERFACE_MODE_MII;
  4718. else
  4719. bp->phy_interface = interface;
  4720. /* IP specific init */
  4721. err = macb_config->init(pdev);
  4722. if (err)
  4723. goto err_out_free_netdev;
  4724. err = macb_mii_init(bp);
  4725. if (err)
  4726. goto err_out_phy_exit;
  4727. netif_carrier_off(dev);
  4728. err = register_netdev(dev);
  4729. if (err) {
  4730. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  4731. goto err_out_unregister_mdio;
  4732. }
  4733. INIT_WORK(&bp->hresp_err_bh_work, macb_hresp_error_task);
  4734. netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
  4735. macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
  4736. dev->base_addr, dev->irq, dev->dev_addr);
  4737. pm_runtime_put_autosuspend(&bp->pdev->dev);
  4738. return 0;
  4739. err_out_unregister_mdio:
  4740. mdiobus_unregister(bp->mii_bus);
  4741. mdiobus_free(bp->mii_bus);
  4742. err_out_phy_exit:
  4743. phy_exit(bp->phy);
  4744. err_out_free_netdev:
  4745. free_netdev(dev);
  4746. err_disable_clocks:
  4747. macb_clks_disable(pclk, hclk, tx_clk, rx_clk, tsu_clk);
  4748. pm_runtime_disable(&pdev->dev);
  4749. pm_runtime_set_suspended(&pdev->dev);
  4750. pm_runtime_dont_use_autosuspend(&pdev->dev);
  4751. return err;
  4752. }
  4753. static void macb_remove(struct platform_device *pdev)
  4754. {
  4755. struct net_device *dev;
  4756. struct macb *bp;
  4757. dev = platform_get_drvdata(pdev);
  4758. if (dev) {
  4759. bp = netdev_priv(dev);
  4760. unregister_netdev(dev);
  4761. phy_exit(bp->phy);
  4762. mdiobus_unregister(bp->mii_bus);
  4763. mdiobus_free(bp->mii_bus);
  4764. device_set_wakeup_enable(&bp->pdev->dev, 0);
  4765. cancel_work_sync(&bp->hresp_err_bh_work);
  4766. pm_runtime_disable(&pdev->dev);
  4767. pm_runtime_dont_use_autosuspend(&pdev->dev);
  4768. pm_runtime_set_suspended(&pdev->dev);
  4769. phylink_destroy(bp->phylink);
  4770. free_netdev(dev);
  4771. }
  4772. }
  4773. static int __maybe_unused macb_suspend(struct device *dev)
  4774. {
  4775. struct net_device *netdev = dev_get_drvdata(dev);
  4776. struct macb *bp = netdev_priv(netdev);
  4777. struct in_ifaddr *ifa = NULL;
  4778. struct macb_queue *queue;
  4779. struct in_device *idev;
  4780. unsigned long flags;
  4781. u32 tmp, ifa_local;
  4782. unsigned int q;
  4783. int err;
  4784. if (!device_may_wakeup(&bp->dev->dev))
  4785. phy_exit(bp->phy);
  4786. if (!netif_running(netdev))
  4787. return 0;
  4788. if (bp->wol & MACB_WOL_ENABLED) {
  4789. if (bp->wolopts & WAKE_ARP) {
  4790. /* Check for IP address in WOL ARP mode */
  4791. rcu_read_lock();
  4792. idev = __in_dev_get_rcu(bp->dev);
  4793. if (idev)
  4794. ifa = rcu_dereference(idev->ifa_list);
  4795. if (!ifa) {
  4796. rcu_read_unlock();
  4797. netdev_err(netdev, "IP address not assigned as required by WoL walk ARP\n");
  4798. return -EOPNOTSUPP;
  4799. }
  4800. ifa_local = be32_to_cpu(ifa->ifa_local);
  4801. rcu_read_unlock();
  4802. }
  4803. spin_lock_irqsave(&bp->lock, flags);
  4804. /* Disable Tx and Rx engines before disabling the queues,
  4805. * this is mandatory as per the IP spec sheet
  4806. */
  4807. tmp = macb_readl(bp, NCR);
  4808. macb_writel(bp, NCR, tmp & ~(MACB_BIT(TE) | MACB_BIT(RE)));
  4809. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  4810. if (!(bp->caps & MACB_CAPS_QUEUE_DISABLE))
  4811. macb_writel(bp, RBQPH,
  4812. upper_32_bits(bp->rx_ring_tieoff_dma));
  4813. #endif
  4814. for (q = 0, queue = bp->queues; q < bp->num_queues;
  4815. ++q, ++queue) {
  4816. /* Disable RX queues */
  4817. if (bp->caps & MACB_CAPS_QUEUE_DISABLE) {
  4818. queue_writel(queue, RBQP, MACB_BIT(QUEUE_DISABLE));
  4819. } else {
  4820. /* Tie off RX queues */
  4821. queue_writel(queue, RBQP,
  4822. lower_32_bits(bp->rx_ring_tieoff_dma));
  4823. }
  4824. /* Disable all interrupts */
  4825. queue_writel(queue, IDR, -1);
  4826. queue_readl(queue, ISR);
  4827. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  4828. queue_writel(queue, ISR, -1);
  4829. }
  4830. /* Enable Receive engine */
  4831. macb_writel(bp, NCR, tmp | MACB_BIT(RE));
  4832. /* Flush all status bits */
  4833. macb_writel(bp, TSR, -1);
  4834. macb_writel(bp, RSR, -1);
  4835. tmp = (bp->wolopts & WAKE_MAGIC) ? MACB_BIT(MAG) : 0;
  4836. if (bp->wolopts & WAKE_ARP) {
  4837. tmp |= MACB_BIT(ARP);
  4838. /* write IP address into register */
  4839. tmp |= MACB_BFEXT(IP, ifa_local);
  4840. }
  4841. spin_unlock_irqrestore(&bp->lock, flags);
  4842. /* Change interrupt handler and
  4843. * Enable WoL IRQ on queue 0
  4844. */
  4845. devm_free_irq(dev, bp->queues[0].irq, bp->queues);
  4846. if (macb_is_gem(bp)) {
  4847. err = devm_request_irq(dev, bp->queues[0].irq, gem_wol_interrupt,
  4848. IRQF_SHARED, netdev->name, bp->queues);
  4849. if (err) {
  4850. dev_err(dev,
  4851. "Unable to request IRQ %d (error %d)\n",
  4852. bp->queues[0].irq, err);
  4853. return err;
  4854. }
  4855. spin_lock_irqsave(&bp->lock, flags);
  4856. queue_writel(bp->queues, IER, GEM_BIT(WOL));
  4857. gem_writel(bp, WOL, tmp);
  4858. spin_unlock_irqrestore(&bp->lock, flags);
  4859. } else {
  4860. err = devm_request_irq(dev, bp->queues[0].irq, macb_wol_interrupt,
  4861. IRQF_SHARED, netdev->name, bp->queues);
  4862. if (err) {
  4863. dev_err(dev,
  4864. "Unable to request IRQ %d (error %d)\n",
  4865. bp->queues[0].irq, err);
  4866. return err;
  4867. }
  4868. spin_lock_irqsave(&bp->lock, flags);
  4869. queue_writel(bp->queues, IER, MACB_BIT(WOL));
  4870. macb_writel(bp, WOL, tmp);
  4871. spin_unlock_irqrestore(&bp->lock, flags);
  4872. }
  4873. enable_irq_wake(bp->queues[0].irq);
  4874. }
  4875. netif_device_detach(netdev);
  4876. for (q = 0, queue = bp->queues; q < bp->num_queues;
  4877. ++q, ++queue) {
  4878. napi_disable(&queue->napi_rx);
  4879. napi_disable(&queue->napi_tx);
  4880. }
  4881. if (!(bp->wol & MACB_WOL_ENABLED)) {
  4882. rtnl_lock();
  4883. phylink_stop(bp->phylink);
  4884. rtnl_unlock();
  4885. spin_lock_irqsave(&bp->lock, flags);
  4886. macb_reset_hw(bp);
  4887. spin_unlock_irqrestore(&bp->lock, flags);
  4888. }
  4889. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  4890. bp->pm_data.usrio = macb_or_gem_readl(bp, USRIO);
  4891. if (netdev->hw_features & NETIF_F_NTUPLE)
  4892. bp->pm_data.scrt2 = gem_readl_n(bp, ETHT, SCRT2_ETHT);
  4893. if (bp->ptp_info)
  4894. bp->ptp_info->ptp_remove(netdev);
  4895. if (!device_may_wakeup(dev))
  4896. pm_runtime_force_suspend(dev);
  4897. return 0;
  4898. }
  4899. static int __maybe_unused macb_resume(struct device *dev)
  4900. {
  4901. struct net_device *netdev = dev_get_drvdata(dev);
  4902. struct macb *bp = netdev_priv(netdev);
  4903. struct macb_queue *queue;
  4904. unsigned long flags;
  4905. unsigned int q;
  4906. int err;
  4907. if (!device_may_wakeup(&bp->dev->dev))
  4908. phy_init(bp->phy);
  4909. if (!netif_running(netdev))
  4910. return 0;
  4911. if (!device_may_wakeup(dev))
  4912. pm_runtime_force_resume(dev);
  4913. if (bp->wol & MACB_WOL_ENABLED) {
  4914. spin_lock_irqsave(&bp->lock, flags);
  4915. /* Disable WoL */
  4916. if (macb_is_gem(bp)) {
  4917. queue_writel(bp->queues, IDR, GEM_BIT(WOL));
  4918. gem_writel(bp, WOL, 0);
  4919. } else {
  4920. queue_writel(bp->queues, IDR, MACB_BIT(WOL));
  4921. macb_writel(bp, WOL, 0);
  4922. }
  4923. /* Clear ISR on queue 0 */
  4924. queue_readl(bp->queues, ISR);
  4925. if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
  4926. queue_writel(bp->queues, ISR, -1);
  4927. spin_unlock_irqrestore(&bp->lock, flags);
  4928. /* Replace interrupt handler on queue 0 */
  4929. devm_free_irq(dev, bp->queues[0].irq, bp->queues);
  4930. err = devm_request_irq(dev, bp->queues[0].irq, macb_interrupt,
  4931. IRQF_SHARED, netdev->name, bp->queues);
  4932. if (err) {
  4933. dev_err(dev,
  4934. "Unable to request IRQ %d (error %d)\n",
  4935. bp->queues[0].irq, err);
  4936. return err;
  4937. }
  4938. disable_irq_wake(bp->queues[0].irq);
  4939. /* Now make sure we disable phy before moving
  4940. * to common restore path
  4941. */
  4942. rtnl_lock();
  4943. phylink_stop(bp->phylink);
  4944. rtnl_unlock();
  4945. }
  4946. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC))
  4947. macb_init_buffers(bp);
  4948. for (q = 0, queue = bp->queues; q < bp->num_queues;
  4949. ++q, ++queue) {
  4950. if (!(bp->caps & MACB_CAPS_MACB_IS_EMAC)) {
  4951. if (macb_is_gem(bp))
  4952. gem_init_rx_ring(queue);
  4953. else
  4954. macb_init_rx_ring(queue);
  4955. }
  4956. napi_enable(&queue->napi_rx);
  4957. napi_enable(&queue->napi_tx);
  4958. }
  4959. if (netdev->hw_features & NETIF_F_NTUPLE)
  4960. gem_writel_n(bp, ETHT, SCRT2_ETHT, bp->pm_data.scrt2);
  4961. if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
  4962. macb_or_gem_writel(bp, USRIO, bp->pm_data.usrio);
  4963. macb_writel(bp, NCR, MACB_BIT(MPE));
  4964. macb_init_hw(bp);
  4965. macb_set_rx_mode(netdev);
  4966. macb_restore_features(bp);
  4967. rtnl_lock();
  4968. phylink_start(bp->phylink);
  4969. rtnl_unlock();
  4970. netif_device_attach(netdev);
  4971. if (bp->ptp_info)
  4972. bp->ptp_info->ptp_init(netdev);
  4973. return 0;
  4974. }
  4975. static int __maybe_unused macb_runtime_suspend(struct device *dev)
  4976. {
  4977. struct net_device *netdev = dev_get_drvdata(dev);
  4978. struct macb *bp = netdev_priv(netdev);
  4979. if (!(device_may_wakeup(dev)))
  4980. macb_clks_disable(bp->pclk, bp->hclk, bp->tx_clk, bp->rx_clk, bp->tsu_clk);
  4981. else if (!(bp->caps & MACB_CAPS_NEED_TSUCLK))
  4982. macb_clks_disable(NULL, NULL, NULL, NULL, bp->tsu_clk);
  4983. return 0;
  4984. }
  4985. static int __maybe_unused macb_runtime_resume(struct device *dev)
  4986. {
  4987. struct net_device *netdev = dev_get_drvdata(dev);
  4988. struct macb *bp = netdev_priv(netdev);
  4989. if (!(device_may_wakeup(dev))) {
  4990. clk_prepare_enable(bp->pclk);
  4991. clk_prepare_enable(bp->hclk);
  4992. clk_prepare_enable(bp->tx_clk);
  4993. clk_prepare_enable(bp->rx_clk);
  4994. clk_prepare_enable(bp->tsu_clk);
  4995. } else if (!(bp->caps & MACB_CAPS_NEED_TSUCLK)) {
  4996. clk_prepare_enable(bp->tsu_clk);
  4997. }
  4998. return 0;
  4999. }
  5000. static void macb_shutdown(struct platform_device *pdev)
  5001. {
  5002. struct net_device *netdev = platform_get_drvdata(pdev);
  5003. rtnl_lock();
  5004. if (netif_running(netdev))
  5005. dev_close(netdev);
  5006. netif_device_detach(netdev);
  5007. rtnl_unlock();
  5008. }
  5009. static const struct dev_pm_ops macb_pm_ops = {
  5010. SET_SYSTEM_SLEEP_PM_OPS(macb_suspend, macb_resume)
  5011. SET_RUNTIME_PM_OPS(macb_runtime_suspend, macb_runtime_resume, NULL)
  5012. };
  5013. static struct platform_driver macb_driver = {
  5014. .probe = macb_probe,
  5015. .remove = macb_remove,
  5016. .driver = {
  5017. .name = "macb",
  5018. .of_match_table = of_match_ptr(macb_dt_ids),
  5019. .pm = &macb_pm_ops,
  5020. },
  5021. .shutdown = macb_shutdown,
  5022. };
  5023. module_platform_driver(macb_driver);
  5024. MODULE_LICENSE("GPL");
  5025. MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
  5026. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  5027. MODULE_ALIAS("platform:macb");