fec_main.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx.
  4. * Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
  5. *
  6. * Right now, I am very wasteful with the buffers. I allocate memory
  7. * pages and then divide them into 2K frame buffers. This way I know I
  8. * have buffers large enough to hold one frame within one buffer descriptor.
  9. * Once I get this working, I will use 64 or 128 byte CPM buffers, which
  10. * will be much more memory efficient and will easily handle lots of
  11. * small packets.
  12. *
  13. * Much better multiple PHY support by Magnus Damm.
  14. * Copyright (c) 2000 Ericsson Radio Systems AB.
  15. *
  16. * Support for FEC controller of ColdFire processors.
  17. * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com)
  18. *
  19. * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be)
  20. * Copyright (c) 2004-2006 Macq Electronique SA.
  21. *
  22. * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
  23. */
  24. #include <linux/bitops.h>
  25. #include <linux/bpf.h>
  26. #include <linux/bpf_trace.h>
  27. #include <linux/cacheflush.h>
  28. #include <linux/clk.h>
  29. #include <linux/crc32.h>
  30. #include <linux/delay.h>
  31. #include <linux/errno.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/fec.h>
  34. #include <linux/filter.h>
  35. #include <linux/gpio/consumer.h>
  36. #include <linux/icmp.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/in.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/io.h>
  41. #include <linux/ioport.h>
  42. #include <linux/ip.h>
  43. #include <linux/irq.h>
  44. #include <linux/kernel.h>
  45. #include <linux/mdio.h>
  46. #include <linux/mfd/syscon.h>
  47. #include <linux/module.h>
  48. #include <linux/netdevice.h>
  49. #include <linux/of.h>
  50. #include <linux/of_mdio.h>
  51. #include <linux/of_net.h>
  52. #include <linux/phy.h>
  53. #include <linux/pinctrl/consumer.h>
  54. #include <linux/phy_fixed.h>
  55. #include <linux/platform_device.h>
  56. #include <linux/pm_runtime.h>
  57. #include <linux/prefetch.h>
  58. #include <linux/property.h>
  59. #include <linux/ptrace.h>
  60. #include <linux/regmap.h>
  61. #include <linux/regulator/consumer.h>
  62. #include <linux/skbuff.h>
  63. #include <linux/slab.h>
  64. #include <linux/spinlock.h>
  65. #include <linux/string.h>
  66. #include <linux/tcp.h>
  67. #include <linux/udp.h>
  68. #include <linux/workqueue.h>
  69. #include <net/ip.h>
  70. #include <net/page_pool/helpers.h>
  71. #include <net/selftests.h>
  72. #include <net/tso.h>
  73. #include <net/xdp_sock_drv.h>
  74. #include <soc/imx/cpuidle.h>
  75. #include "fec.h"
  76. static void set_multicast_list(struct net_device *ndev);
  77. static void fec_enet_itr_coal_set(struct net_device *ndev);
  78. static int fec_enet_xdp_tx_xmit(struct fec_enet_private *fep,
  79. int cpu, struct xdp_buff *xdp,
  80. u32 dma_sync_len, int queue);
  81. #define DRIVER_NAME "fec"
  82. static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2};
  83. #define FEC_ENET_RSEM_V 0x84
  84. #define FEC_ENET_RSFL_V 16
  85. #define FEC_ENET_RAEM_V 0x8
  86. #define FEC_ENET_RAFL_V 0x8
  87. #define FEC_ENET_OPD_V 0xFFF0
  88. #define FEC_MDIO_PM_TIMEOUT 100 /* ms */
  89. #define FEC_ENET_XDP_PASS 0
  90. #define FEC_ENET_XDP_CONSUMED BIT(0)
  91. #define FEC_ENET_XDP_TX BIT(1)
  92. #define FEC_ENET_XDP_REDIR BIT(2)
  93. struct fec_devinfo {
  94. u32 quirks;
  95. };
  96. static const struct fec_devinfo fec_imx25_info = {
  97. .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR |
  98. FEC_QUIRK_HAS_FRREG | FEC_QUIRK_HAS_MDIO_C45,
  99. };
  100. static const struct fec_devinfo fec_imx27_info = {
  101. .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG |
  102. FEC_QUIRK_HAS_MDIO_C45,
  103. };
  104. static const struct fec_devinfo fec_imx28_info = {
  105. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME |
  106. FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC |
  107. FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII |
  108. FEC_QUIRK_NO_HARD_RESET | FEC_QUIRK_HAS_MDIO_C45,
  109. };
  110. static const struct fec_devinfo fec_imx6q_info = {
  111. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  112. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  113. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 |
  114. FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII |
  115. FEC_QUIRK_HAS_PMQOS | FEC_QUIRK_HAS_MDIO_C45,
  116. };
  117. static const struct fec_devinfo fec_mvf600_info = {
  118. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC |
  119. FEC_QUIRK_HAS_MDIO_C45,
  120. };
  121. static const struct fec_devinfo fec_imx6sx_info = {
  122. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  123. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  124. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
  125. FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
  126. FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
  127. FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
  128. FEC_QUIRK_HAS_MDIO_C45,
  129. };
  130. static const struct fec_devinfo fec_imx6ul_info = {
  131. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  132. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  133. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 |
  134. FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC |
  135. FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII |
  136. FEC_QUIRK_HAS_MDIO_C45,
  137. };
  138. static const struct fec_devinfo fec_imx8mq_info = {
  139. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  140. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  141. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
  142. FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
  143. FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
  144. FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
  145. FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2 |
  146. FEC_QUIRK_HAS_MDIO_C45,
  147. };
  148. static const struct fec_devinfo fec_imx8qm_info = {
  149. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  150. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  151. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
  152. FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
  153. FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE |
  154. FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES |
  155. FEC_QUIRK_DELAYED_CLKS_SUPPORT | FEC_QUIRK_HAS_MDIO_C45 |
  156. FEC_QUIRK_JUMBO_FRAME,
  157. };
  158. static const struct fec_devinfo fec_s32v234_info = {
  159. .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT |
  160. FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM |
  161. FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB |
  162. FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE |
  163. FEC_QUIRK_HAS_MDIO_C45,
  164. };
  165. static struct platform_device_id fec_devtype[] = {
  166. {
  167. /* keep it for coldfire */
  168. .name = DRIVER_NAME,
  169. .driver_data = 0,
  170. }, {
  171. /* sentinel */
  172. }
  173. };
  174. MODULE_DEVICE_TABLE(platform, fec_devtype);
  175. static const struct of_device_id fec_dt_ids[] = {
  176. { .compatible = "fsl,imx25-fec", .data = &fec_imx25_info, },
  177. { .compatible = "fsl,imx27-fec", .data = &fec_imx27_info, },
  178. { .compatible = "fsl,imx28-fec", .data = &fec_imx28_info, },
  179. { .compatible = "fsl,imx6q-fec", .data = &fec_imx6q_info, },
  180. { .compatible = "fsl,mvf600-fec", .data = &fec_mvf600_info, },
  181. { .compatible = "fsl,imx6sx-fec", .data = &fec_imx6sx_info, },
  182. { .compatible = "fsl,imx6ul-fec", .data = &fec_imx6ul_info, },
  183. { .compatible = "fsl,imx8mq-fec", .data = &fec_imx8mq_info, },
  184. { .compatible = "fsl,imx8qm-fec", .data = &fec_imx8qm_info, },
  185. { .compatible = "fsl,s32v234-fec", .data = &fec_s32v234_info, },
  186. { /* sentinel */ }
  187. };
  188. MODULE_DEVICE_TABLE(of, fec_dt_ids);
  189. static unsigned char macaddr[ETH_ALEN];
  190. module_param_array(macaddr, byte, NULL, 0);
  191. MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
  192. #if defined(CONFIG_M5272)
  193. /*
  194. * Some hardware gets it MAC address out of local flash memory.
  195. * if this is non-zero then assume it is the address to get MAC from.
  196. */
  197. #if defined(CONFIG_NETtel)
  198. #define FEC_FLASHMAC 0xf0006006
  199. #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES)
  200. #define FEC_FLASHMAC 0xf0006000
  201. #elif defined(CONFIG_CANCam)
  202. #define FEC_FLASHMAC 0xf0020000
  203. #elif defined (CONFIG_M5272C3)
  204. #define FEC_FLASHMAC (0xffe04000 + 4)
  205. #elif defined(CONFIG_MOD5272)
  206. #define FEC_FLASHMAC 0xffc0406b
  207. #else
  208. #define FEC_FLASHMAC 0
  209. #endif
  210. #endif /* CONFIG_M5272 */
  211. /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets.
  212. *
  213. * 2048 byte skbufs are allocated. However, alignment requirements
  214. * varies between FEC variants. Worst case is 64, so round down by 64.
  215. */
  216. #define MAX_JUMBO_BUF_SIZE (round_down(16384 - FEC_DRV_RESERVE_SPACE - 64, 64))
  217. #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64))
  218. #define PKT_MINBUF_SIZE 64
  219. /* FEC receive acceleration */
  220. #define FEC_RACC_IPDIS BIT(1)
  221. #define FEC_RACC_PRODIS BIT(2)
  222. #define FEC_RACC_SHIFT16 BIT(7)
  223. #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS)
  224. /* MIB Control Register */
  225. #define FEC_MIB_CTRLSTAT_DISABLE BIT(31)
  226. /*
  227. * The 5270/5271/5280/5282/532x RX control register also contains maximum frame
  228. * size bits. Other FEC hardware does not, so we need to take that into
  229. * account when setting it.
  230. */
  231. #ifndef CONFIG_M5272
  232. #define OPT_ARCH_HAS_MAX_FL 1
  233. #else
  234. #define OPT_ARCH_HAS_MAX_FL 0
  235. #endif
  236. /* FEC MII MMFR bits definition */
  237. #define FEC_MMFR_ST (1 << 30)
  238. #define FEC_MMFR_ST_C45 (0)
  239. #define FEC_MMFR_OP_READ (2 << 28)
  240. #define FEC_MMFR_OP_READ_C45 (3 << 28)
  241. #define FEC_MMFR_OP_WRITE (1 << 28)
  242. #define FEC_MMFR_OP_ADDR_WRITE (0)
  243. #define FEC_MMFR_PA(v) ((v & 0x1f) << 23)
  244. #define FEC_MMFR_RA(v) ((v & 0x1f) << 18)
  245. #define FEC_MMFR_TA (2 << 16)
  246. #define FEC_MMFR_DATA(v) (v & 0xffff)
  247. /* FEC ECR bits definition */
  248. #define FEC_ECR_RESET BIT(0)
  249. #define FEC_ECR_ETHEREN BIT(1)
  250. #define FEC_ECR_MAGICEN BIT(2)
  251. #define FEC_ECR_SLEEP BIT(3)
  252. #define FEC_ECR_EN1588 BIT(4)
  253. #define FEC_ECR_SPEED BIT(5)
  254. #define FEC_ECR_BYTESWP BIT(8)
  255. /* FEC RCR bits definition */
  256. #define FEC_RCR_LOOP BIT(0)
  257. #define FEC_RCR_DRT BIT(1)
  258. #define FEC_RCR_MII BIT(2)
  259. #define FEC_RCR_PROMISC BIT(3)
  260. #define FEC_RCR_BC_REJ BIT(4)
  261. #define FEC_RCR_FLOWCTL BIT(5)
  262. #define FEC_RCR_RGMII BIT(6)
  263. #define FEC_RCR_RMII BIT(8)
  264. #define FEC_RCR_10BASET BIT(9)
  265. #define FEC_RCR_NLC BIT(30)
  266. /* TX WMARK bits */
  267. #define FEC_TXWMRK_STRFWD BIT(8)
  268. #define FEC_MII_TIMEOUT 30000 /* us */
  269. /* Transmitter timeout */
  270. #define TX_TIMEOUT (2 * HZ)
  271. #define FEC_PAUSE_FLAG_AUTONEG 0x1
  272. #define FEC_PAUSE_FLAG_ENABLE 0x2
  273. #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0)
  274. #define FEC_WOL_FLAG_ENABLE (0x1 << 1)
  275. #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2)
  276. /* Max number of allowed TCP segments for software TSO */
  277. #define FEC_MAX_TSO_SEGS 100
  278. #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
  279. #define IS_TSO_HEADER(txq, addr) \
  280. ((addr >= txq->tso_hdrs_dma) && \
  281. (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE))
  282. static int mii_cnt;
  283. static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp,
  284. struct bufdesc_prop *bd)
  285. {
  286. return (bdp >= bd->last) ? bd->base
  287. : (struct bufdesc *)(((void *)bdp) + bd->dsize);
  288. }
  289. static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp,
  290. struct bufdesc_prop *bd)
  291. {
  292. return (bdp <= bd->base) ? bd->last
  293. : (struct bufdesc *)(((void *)bdp) - bd->dsize);
  294. }
  295. static int fec_enet_get_bd_index(struct bufdesc *bdp,
  296. struct bufdesc_prop *bd)
  297. {
  298. return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2;
  299. }
  300. static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq)
  301. {
  302. int entries;
  303. entries = (((const char *)txq->dirty_tx -
  304. (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1;
  305. return entries >= 0 ? entries : entries + txq->bd.ring_size;
  306. }
  307. static void swap_buffer(void *bufaddr, int len)
  308. {
  309. int i;
  310. unsigned int *buf = bufaddr;
  311. for (i = 0; i < len; i += 4, buf++)
  312. swab32s(buf);
  313. }
  314. static void fec_dump(struct net_device *ndev)
  315. {
  316. struct fec_enet_private *fep = netdev_priv(ndev);
  317. struct bufdesc *bdp;
  318. struct fec_enet_priv_tx_q *txq;
  319. int index = 0;
  320. netdev_info(ndev, "TX ring dump\n");
  321. pr_info("Nr SC addr len SKB\n");
  322. txq = fep->tx_queue[0];
  323. bdp = txq->bd.base;
  324. do {
  325. pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n",
  326. index,
  327. bdp == txq->bd.cur ? 'S' : ' ',
  328. bdp == txq->dirty_tx ? 'H' : ' ',
  329. fec16_to_cpu(bdp->cbd_sc),
  330. fec32_to_cpu(bdp->cbd_bufaddr),
  331. fec16_to_cpu(bdp->cbd_datlen),
  332. txq->tx_buf[index].buf_p);
  333. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  334. index++;
  335. } while (bdp != txq->bd.base);
  336. }
  337. /*
  338. * Coldfire does not support DMA coherent allocations, and has historically used
  339. * a band-aid with a manual flush in fec_enet_rx_queue.
  340. */
  341. #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
  342. static void *fec_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  343. gfp_t gfp)
  344. {
  345. return dma_alloc_noncoherent(dev, size, handle, DMA_BIDIRECTIONAL, gfp);
  346. }
  347. static void fec_dma_free(struct device *dev, size_t size, void *cpu_addr,
  348. dma_addr_t handle)
  349. {
  350. dma_free_noncoherent(dev, size, cpu_addr, handle, DMA_BIDIRECTIONAL);
  351. }
  352. #else /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */
  353. static void *fec_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  354. gfp_t gfp)
  355. {
  356. return dma_alloc_coherent(dev, size, handle, gfp);
  357. }
  358. static void fec_dma_free(struct device *dev, size_t size, void *cpu_addr,
  359. dma_addr_t handle)
  360. {
  361. dma_free_coherent(dev, size, cpu_addr, handle);
  362. }
  363. #endif /* !CONFIG_COLDFIRE || CONFIG_COLDFIRE_COHERENT_DMA */
  364. struct fec_dma_devres {
  365. size_t size;
  366. void *vaddr;
  367. dma_addr_t dma_handle;
  368. };
  369. static void fec_dmam_release(struct device *dev, void *res)
  370. {
  371. struct fec_dma_devres *this = res;
  372. fec_dma_free(dev, this->size, this->vaddr, this->dma_handle);
  373. }
  374. static void *fec_dmam_alloc(struct device *dev, size_t size, dma_addr_t *handle,
  375. gfp_t gfp)
  376. {
  377. struct fec_dma_devres *dr;
  378. void *vaddr;
  379. dr = devres_alloc(fec_dmam_release, sizeof(*dr), gfp);
  380. if (!dr)
  381. return NULL;
  382. vaddr = fec_dma_alloc(dev, size, handle, gfp);
  383. if (!vaddr) {
  384. devres_free(dr);
  385. return NULL;
  386. }
  387. dr->vaddr = vaddr;
  388. dr->dma_handle = *handle;
  389. dr->size = size;
  390. devres_add(dev, dr);
  391. return vaddr;
  392. }
  393. static inline bool is_ipv4_pkt(struct sk_buff *skb)
  394. {
  395. return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4;
  396. }
  397. static int
  398. fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev)
  399. {
  400. /* Only run for packets requiring a checksum. */
  401. if (skb->ip_summed != CHECKSUM_PARTIAL)
  402. return 0;
  403. if (unlikely(skb_cow_head(skb, 0)))
  404. return -1;
  405. if (is_ipv4_pkt(skb))
  406. ip_hdr(skb)->check = 0;
  407. *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0;
  408. return 0;
  409. }
  410. static int
  411. fec_enet_create_page_pool(struct fec_enet_private *fep,
  412. struct fec_enet_priv_rx_q *rxq)
  413. {
  414. struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog);
  415. struct page_pool_params pp_params = {
  416. .order = fep->pagepool_order,
  417. .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
  418. .pool_size = rxq->bd.ring_size,
  419. .nid = dev_to_node(&fep->pdev->dev),
  420. .dev = &fep->pdev->dev,
  421. .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
  422. .offset = FEC_ENET_XDP_HEADROOM,
  423. .max_len = fep->rx_frame_size,
  424. };
  425. int err;
  426. rxq->page_pool = page_pool_create(&pp_params);
  427. if (IS_ERR(rxq->page_pool)) {
  428. err = PTR_ERR(rxq->page_pool);
  429. rxq->page_pool = NULL;
  430. return err;
  431. }
  432. return 0;
  433. }
  434. static void fec_txq_trigger_xmit(struct fec_enet_private *fep,
  435. struct fec_enet_priv_tx_q *txq)
  436. {
  437. if (!(fep->quirks & FEC_QUIRK_ERR007885) ||
  438. !readl(txq->bd.reg_desc_active) ||
  439. !readl(txq->bd.reg_desc_active) ||
  440. !readl(txq->bd.reg_desc_active) ||
  441. !readl(txq->bd.reg_desc_active))
  442. writel(0, txq->bd.reg_desc_active);
  443. }
  444. static struct bufdesc *
  445. fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq,
  446. struct sk_buff *skb,
  447. struct net_device *ndev)
  448. {
  449. struct fec_enet_private *fep = netdev_priv(ndev);
  450. struct bufdesc *bdp = txq->bd.cur;
  451. struct bufdesc_ex *ebdp;
  452. int nr_frags = skb_shinfo(skb)->nr_frags;
  453. int frag, frag_len;
  454. unsigned short status;
  455. unsigned int estatus = 0;
  456. skb_frag_t *this_frag;
  457. unsigned int index;
  458. void *bufaddr;
  459. dma_addr_t addr;
  460. int i;
  461. for (frag = 0; frag < nr_frags; frag++) {
  462. this_frag = &skb_shinfo(skb)->frags[frag];
  463. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  464. ebdp = (struct bufdesc_ex *)bdp;
  465. status = fec16_to_cpu(bdp->cbd_sc);
  466. status &= ~BD_ENET_TX_STATS;
  467. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  468. frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]);
  469. /* Handle the last BD specially */
  470. if (frag == nr_frags - 1) {
  471. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  472. if (fep->bufdesc_ex) {
  473. estatus |= BD_ENET_TX_INT;
  474. if (unlikely(skb_shinfo(skb)->tx_flags &
  475. SKBTX_HW_TSTAMP && fep->hwts_tx_en))
  476. estatus |= BD_ENET_TX_TS;
  477. }
  478. }
  479. if (fep->bufdesc_ex) {
  480. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  481. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  482. if (skb->ip_summed == CHECKSUM_PARTIAL)
  483. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  484. ebdp->cbd_bdu = 0;
  485. ebdp->cbd_esc = cpu_to_fec32(estatus);
  486. }
  487. bufaddr = skb_frag_address(this_frag);
  488. index = fec_enet_get_bd_index(bdp, &txq->bd);
  489. if (((unsigned long) bufaddr) & fep->tx_align ||
  490. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  491. memcpy(txq->tx_bounce[index], bufaddr, frag_len);
  492. bufaddr = txq->tx_bounce[index];
  493. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  494. swap_buffer(bufaddr, frag_len);
  495. }
  496. addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len,
  497. DMA_TO_DEVICE);
  498. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  499. if (net_ratelimit())
  500. netdev_err(ndev, "Tx DMA memory map failed\n");
  501. goto dma_mapping_error;
  502. }
  503. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  504. bdp->cbd_datlen = cpu_to_fec16(frag_len);
  505. /* Make sure the updates to rest of the descriptor are
  506. * performed before transferring ownership.
  507. */
  508. wmb();
  509. bdp->cbd_sc = cpu_to_fec16(status);
  510. }
  511. return bdp;
  512. dma_mapping_error:
  513. bdp = txq->bd.cur;
  514. for (i = 0; i < frag; i++) {
  515. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  516. dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr),
  517. fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE);
  518. }
  519. return ERR_PTR(-ENOMEM);
  520. }
  521. static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq,
  522. struct sk_buff *skb, struct net_device *ndev)
  523. {
  524. struct fec_enet_private *fep = netdev_priv(ndev);
  525. int nr_frags = skb_shinfo(skb)->nr_frags;
  526. struct bufdesc *bdp, *last_bdp;
  527. void *bufaddr;
  528. dma_addr_t addr;
  529. unsigned short status;
  530. unsigned short buflen;
  531. unsigned int estatus = 0;
  532. unsigned int index;
  533. int entries_free;
  534. entries_free = fec_enet_get_free_txdesc_num(txq);
  535. if (entries_free < MAX_SKB_FRAGS + 1) {
  536. dev_kfree_skb_any(skb);
  537. if (net_ratelimit())
  538. netdev_err(ndev, "NOT enough BD for SG!\n");
  539. return NETDEV_TX_OK;
  540. }
  541. /* Protocol checksum off-load for TCP and UDP. */
  542. if (fec_enet_clear_csum(skb, ndev)) {
  543. dev_kfree_skb_any(skb);
  544. return NETDEV_TX_OK;
  545. }
  546. /* Fill in a Tx ring entry */
  547. bdp = txq->bd.cur;
  548. last_bdp = bdp;
  549. status = fec16_to_cpu(bdp->cbd_sc);
  550. status &= ~BD_ENET_TX_STATS;
  551. /* Set buffer length and buffer pointer */
  552. bufaddr = skb->data;
  553. buflen = skb_headlen(skb);
  554. index = fec_enet_get_bd_index(bdp, &txq->bd);
  555. if (((unsigned long) bufaddr) & fep->tx_align ||
  556. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  557. memcpy(txq->tx_bounce[index], skb->data, buflen);
  558. bufaddr = txq->tx_bounce[index];
  559. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  560. swap_buffer(bufaddr, buflen);
  561. }
  562. /* Push the data cache so the CPM does not get stale memory data. */
  563. addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE);
  564. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  565. dev_kfree_skb_any(skb);
  566. if (net_ratelimit())
  567. netdev_err(ndev, "Tx DMA memory map failed\n");
  568. return NETDEV_TX_OK;
  569. }
  570. if (nr_frags) {
  571. last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev);
  572. if (IS_ERR(last_bdp)) {
  573. dma_unmap_single(&fep->pdev->dev, addr,
  574. buflen, DMA_TO_DEVICE);
  575. dev_kfree_skb_any(skb);
  576. return NETDEV_TX_OK;
  577. }
  578. } else {
  579. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  580. if (fep->bufdesc_ex) {
  581. estatus = BD_ENET_TX_INT;
  582. if (unlikely(skb_shinfo(skb)->tx_flags &
  583. SKBTX_HW_TSTAMP && fep->hwts_tx_en))
  584. estatus |= BD_ENET_TX_TS;
  585. }
  586. }
  587. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  588. bdp->cbd_datlen = cpu_to_fec16(buflen);
  589. if (fep->bufdesc_ex) {
  590. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  591. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  592. fep->hwts_tx_en))
  593. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  594. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  595. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  596. if (skb->ip_summed == CHECKSUM_PARTIAL)
  597. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  598. ebdp->cbd_bdu = 0;
  599. ebdp->cbd_esc = cpu_to_fec32(estatus);
  600. }
  601. index = fec_enet_get_bd_index(last_bdp, &txq->bd);
  602. /* Save skb pointer */
  603. txq->tx_buf[index].buf_p = skb;
  604. /* Make sure the updates to rest of the descriptor are performed before
  605. * transferring ownership.
  606. */
  607. wmb();
  608. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  609. * it's the last BD of the frame, and to put the CRC on the end.
  610. */
  611. status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
  612. bdp->cbd_sc = cpu_to_fec16(status);
  613. /* If this was the last BD in the ring, start at the beginning again. */
  614. bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd);
  615. skb_tx_timestamp(skb);
  616. /* Make sure the update to bdp is performed before txq->bd.cur. */
  617. wmb();
  618. txq->bd.cur = bdp;
  619. /* Trigger transmission start */
  620. fec_txq_trigger_xmit(fep, txq);
  621. return 0;
  622. }
  623. static int
  624. fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
  625. struct net_device *ndev,
  626. struct bufdesc *bdp, int index, char *data,
  627. int size, bool last_tcp, bool is_last)
  628. {
  629. struct fec_enet_private *fep = netdev_priv(ndev);
  630. struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
  631. unsigned short status;
  632. unsigned int estatus = 0;
  633. dma_addr_t addr;
  634. status = fec16_to_cpu(bdp->cbd_sc);
  635. status &= ~BD_ENET_TX_STATS;
  636. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  637. if (((unsigned long) data) & fep->tx_align ||
  638. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  639. memcpy(txq->tx_bounce[index], data, size);
  640. data = txq->tx_bounce[index];
  641. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  642. swap_buffer(data, size);
  643. }
  644. addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
  645. if (dma_mapping_error(&fep->pdev->dev, addr)) {
  646. dev_kfree_skb_any(skb);
  647. if (net_ratelimit())
  648. netdev_err(ndev, "Tx DMA memory map failed\n");
  649. return NETDEV_TX_OK;
  650. }
  651. bdp->cbd_datlen = cpu_to_fec16(size);
  652. bdp->cbd_bufaddr = cpu_to_fec32(addr);
  653. if (fep->bufdesc_ex) {
  654. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  655. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  656. if (skb->ip_summed == CHECKSUM_PARTIAL)
  657. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  658. ebdp->cbd_bdu = 0;
  659. ebdp->cbd_esc = cpu_to_fec32(estatus);
  660. }
  661. /* Handle the last BD specially */
  662. if (last_tcp)
  663. status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC);
  664. if (is_last) {
  665. status |= BD_ENET_TX_INTR;
  666. if (fep->bufdesc_ex)
  667. ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT);
  668. }
  669. bdp->cbd_sc = cpu_to_fec16(status);
  670. return 0;
  671. }
  672. static int
  673. fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
  674. struct sk_buff *skb, struct net_device *ndev,
  675. struct bufdesc *bdp, int index)
  676. {
  677. struct fec_enet_private *fep = netdev_priv(ndev);
  678. int hdr_len = skb_tcp_all_headers(skb);
  679. struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc);
  680. void *bufaddr;
  681. unsigned long dmabuf;
  682. unsigned short status;
  683. unsigned int estatus = 0;
  684. status = fec16_to_cpu(bdp->cbd_sc);
  685. status &= ~BD_ENET_TX_STATS;
  686. status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
  687. bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
  688. dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
  689. if (((unsigned long)bufaddr) & fep->tx_align ||
  690. fep->quirks & FEC_QUIRK_SWAP_FRAME) {
  691. memcpy(txq->tx_bounce[index], skb->data, hdr_len);
  692. bufaddr = txq->tx_bounce[index];
  693. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  694. swap_buffer(bufaddr, hdr_len);
  695. dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
  696. hdr_len, DMA_TO_DEVICE);
  697. if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
  698. dev_kfree_skb_any(skb);
  699. if (net_ratelimit())
  700. netdev_err(ndev, "Tx DMA memory map failed\n");
  701. return NETDEV_TX_OK;
  702. }
  703. }
  704. bdp->cbd_bufaddr = cpu_to_fec32(dmabuf);
  705. bdp->cbd_datlen = cpu_to_fec16(hdr_len);
  706. if (fep->bufdesc_ex) {
  707. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  708. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  709. if (skb->ip_summed == CHECKSUM_PARTIAL)
  710. estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS;
  711. ebdp->cbd_bdu = 0;
  712. ebdp->cbd_esc = cpu_to_fec32(estatus);
  713. }
  714. bdp->cbd_sc = cpu_to_fec16(status);
  715. return 0;
  716. }
  717. static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
  718. struct sk_buff *skb,
  719. struct net_device *ndev)
  720. {
  721. struct fec_enet_private *fep = netdev_priv(ndev);
  722. int hdr_len, total_len, data_left;
  723. struct bufdesc *bdp = txq->bd.cur;
  724. struct bufdesc *tmp_bdp;
  725. struct bufdesc_ex *ebdp;
  726. struct tso_t tso;
  727. unsigned int index = 0;
  728. int ret;
  729. if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) {
  730. dev_kfree_skb_any(skb);
  731. if (net_ratelimit())
  732. netdev_err(ndev, "NOT enough BD for TSO!\n");
  733. return NETDEV_TX_OK;
  734. }
  735. /* Protocol checksum off-load for TCP and UDP. */
  736. if (fec_enet_clear_csum(skb, ndev)) {
  737. dev_kfree_skb_any(skb);
  738. return NETDEV_TX_OK;
  739. }
  740. /* Initialize the TSO handler, and prepare the first payload */
  741. hdr_len = tso_start(skb, &tso);
  742. total_len = skb->len - hdr_len;
  743. while (total_len > 0) {
  744. char *hdr;
  745. index = fec_enet_get_bd_index(bdp, &txq->bd);
  746. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  747. total_len -= data_left;
  748. /* prepare packet headers: MAC + IP + TCP */
  749. hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
  750. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  751. ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index);
  752. if (ret)
  753. goto err_release;
  754. while (data_left > 0) {
  755. int size;
  756. size = min_t(int, tso.size, data_left);
  757. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  758. index = fec_enet_get_bd_index(bdp, &txq->bd);
  759. ret = fec_enet_txq_put_data_tso(txq, skb, ndev,
  760. bdp, index,
  761. tso.data, size,
  762. size == data_left,
  763. total_len == 0);
  764. if (ret)
  765. goto err_release;
  766. data_left -= size;
  767. tso_build_data(skb, &tso, size);
  768. }
  769. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  770. }
  771. /* Save skb pointer */
  772. txq->tx_buf[index].buf_p = skb;
  773. skb_tx_timestamp(skb);
  774. txq->bd.cur = bdp;
  775. /* Trigger transmission start */
  776. fec_txq_trigger_xmit(fep, txq);
  777. return 0;
  778. err_release:
  779. /* Release all used data descriptors for TSO */
  780. tmp_bdp = txq->bd.cur;
  781. while (tmp_bdp != bdp) {
  782. /* Unmap data buffers */
  783. if (tmp_bdp->cbd_bufaddr &&
  784. !IS_TSO_HEADER(txq, fec32_to_cpu(tmp_bdp->cbd_bufaddr)))
  785. dma_unmap_single(&fep->pdev->dev,
  786. fec32_to_cpu(tmp_bdp->cbd_bufaddr),
  787. fec16_to_cpu(tmp_bdp->cbd_datlen),
  788. DMA_TO_DEVICE);
  789. /* Clear standard buffer descriptor fields */
  790. tmp_bdp->cbd_sc = 0;
  791. tmp_bdp->cbd_datlen = 0;
  792. tmp_bdp->cbd_bufaddr = 0;
  793. /* Handle extended descriptor if enabled */
  794. if (fep->bufdesc_ex) {
  795. ebdp = (struct bufdesc_ex *)tmp_bdp;
  796. ebdp->cbd_esc = 0;
  797. }
  798. tmp_bdp = fec_enet_get_nextdesc(tmp_bdp, &txq->bd);
  799. }
  800. dev_kfree_skb_any(skb);
  801. return ret;
  802. }
  803. static netdev_tx_t
  804. fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  805. {
  806. struct fec_enet_private *fep = netdev_priv(ndev);
  807. int entries_free;
  808. unsigned short queue;
  809. struct fec_enet_priv_tx_q *txq;
  810. struct netdev_queue *nq;
  811. int ret;
  812. queue = skb_get_queue_mapping(skb);
  813. txq = fep->tx_queue[queue];
  814. nq = netdev_get_tx_queue(ndev, queue);
  815. if (skb_is_gso(skb))
  816. ret = fec_enet_txq_submit_tso(txq, skb, ndev);
  817. else
  818. ret = fec_enet_txq_submit_skb(txq, skb, ndev);
  819. if (ret)
  820. return ret;
  821. entries_free = fec_enet_get_free_txdesc_num(txq);
  822. if (entries_free <= txq->tx_stop_threshold)
  823. netif_tx_stop_queue(nq);
  824. return NETDEV_TX_OK;
  825. }
  826. /* Init RX & TX buffer descriptors
  827. */
  828. static void fec_enet_bd_init(struct net_device *dev)
  829. {
  830. struct fec_enet_private *fep = netdev_priv(dev);
  831. struct fec_enet_priv_tx_q *txq;
  832. struct fec_enet_priv_rx_q *rxq;
  833. struct bufdesc *bdp;
  834. unsigned int i;
  835. unsigned int q;
  836. for (q = 0; q < fep->num_rx_queues; q++) {
  837. /* Initialize the receive buffer descriptors. */
  838. rxq = fep->rx_queue[q];
  839. bdp = rxq->bd.base;
  840. for (i = 0; i < rxq->bd.ring_size; i++) {
  841. /* Initialize the BD for every fragment in the page. */
  842. if (bdp->cbd_bufaddr)
  843. bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY);
  844. else
  845. bdp->cbd_sc = cpu_to_fec16(0);
  846. if (fep->bufdesc_ex) {
  847. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  848. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  849. }
  850. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  851. }
  852. /* Set the last buffer to wrap */
  853. bdp = fec_enet_get_prevdesc(bdp, &rxq->bd);
  854. bdp->cbd_sc |= cpu_to_fec16(BD_ENET_RX_WRAP);
  855. rxq->bd.cur = rxq->bd.base;
  856. }
  857. for (q = 0; q < fep->num_tx_queues; q++) {
  858. /* ...and the same for transmit */
  859. txq = fep->tx_queue[q];
  860. bdp = txq->bd.base;
  861. txq->bd.cur = bdp;
  862. for (i = 0; i < txq->bd.ring_size; i++) {
  863. struct page *page;
  864. /* Initialize the BD for every fragment in the page. */
  865. bdp->cbd_sc = cpu_to_fec16(0);
  866. switch (txq->tx_buf[i].type) {
  867. case FEC_TXBUF_T_SKB:
  868. if (bdp->cbd_bufaddr &&
  869. !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
  870. dma_unmap_single(&fep->pdev->dev,
  871. fec32_to_cpu(bdp->cbd_bufaddr),
  872. fec16_to_cpu(bdp->cbd_datlen),
  873. DMA_TO_DEVICE);
  874. dev_kfree_skb_any(txq->tx_buf[i].buf_p);
  875. break;
  876. case FEC_TXBUF_T_XDP_NDO:
  877. dma_unmap_single(&fep->pdev->dev,
  878. fec32_to_cpu(bdp->cbd_bufaddr),
  879. fec16_to_cpu(bdp->cbd_datlen),
  880. DMA_TO_DEVICE);
  881. xdp_return_frame(txq->tx_buf[i].buf_p);
  882. break;
  883. case FEC_TXBUF_T_XDP_TX:
  884. page = txq->tx_buf[i].buf_p;
  885. page_pool_put_page(pp_page_to_nmdesc(page)->pp,
  886. page, 0, false);
  887. break;
  888. case FEC_TXBUF_T_XSK_TX:
  889. xsk_buff_free(txq->tx_buf[i].buf_p);
  890. break;
  891. default:
  892. break;
  893. }
  894. txq->tx_buf[i].buf_p = NULL;
  895. /* restore default tx buffer type: FEC_TXBUF_T_SKB */
  896. txq->tx_buf[i].type = FEC_TXBUF_T_SKB;
  897. bdp->cbd_bufaddr = cpu_to_fec32(0);
  898. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  899. }
  900. /* Set the last buffer to wrap */
  901. bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
  902. bdp->cbd_sc |= cpu_to_fec16(BD_ENET_TX_WRAP);
  903. txq->dirty_tx = bdp;
  904. }
  905. }
  906. static void fec_enet_active_rxring(struct net_device *ndev)
  907. {
  908. struct fec_enet_private *fep = netdev_priv(ndev);
  909. int i;
  910. for (i = 0; i < fep->num_rx_queues; i++)
  911. writel(0, fep->rx_queue[i]->bd.reg_desc_active);
  912. }
  913. static void fec_enet_enable_ring(struct net_device *ndev)
  914. {
  915. struct fec_enet_private *fep = netdev_priv(ndev);
  916. struct fec_enet_priv_tx_q *txq;
  917. struct fec_enet_priv_rx_q *rxq;
  918. int i;
  919. for (i = 0; i < fep->num_rx_queues; i++) {
  920. rxq = fep->rx_queue[i];
  921. writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i));
  922. writel(fep->max_buf_size, fep->hwp + FEC_R_BUFF_SIZE(i));
  923. /* enable DMA1/2 */
  924. if (i)
  925. writel(RCMR_MATCHEN | RCMR_CMP(i),
  926. fep->hwp + FEC_RCMR(i));
  927. }
  928. for (i = 0; i < fep->num_tx_queues; i++) {
  929. txq = fep->tx_queue[i];
  930. writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i));
  931. /* enable DMA1/2 */
  932. if (i)
  933. writel(DMA_CLASS_EN | IDLE_SLOPE(i),
  934. fep->hwp + FEC_DMA_CFG(i));
  935. }
  936. }
  937. /* Whack a reset. We should wait for this.
  938. * For i.MX6SX SOC, enet use AXI bus, we use disable MAC
  939. * instead of reset MAC itself.
  940. */
  941. static void fec_ctrl_reset(struct fec_enet_private *fep, bool allow_wol)
  942. {
  943. u32 val;
  944. if (!allow_wol || !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
  945. if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES ||
  946. ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) {
  947. writel(0, fep->hwp + FEC_ECNTRL);
  948. } else {
  949. writel(FEC_ECR_RESET, fep->hwp + FEC_ECNTRL);
  950. udelay(10);
  951. }
  952. } else {
  953. val = readl(fep->hwp + FEC_ECNTRL);
  954. val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
  955. writel(val, fep->hwp + FEC_ECNTRL);
  956. }
  957. }
  958. static void fec_set_hw_mac_addr(struct net_device *ndev)
  959. {
  960. struct fec_enet_private *fep = netdev_priv(ndev);
  961. writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
  962. (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
  963. fep->hwp + FEC_ADDR_LOW);
  964. writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
  965. fep->hwp + FEC_ADDR_HIGH);
  966. }
  967. /*
  968. * This function is called to start or restart the FEC during a link
  969. * change, transmit timeout, or to reconfigure the FEC. The network
  970. * packet processing for this device must be stopped before this call.
  971. */
  972. static void
  973. fec_restart(struct net_device *ndev)
  974. {
  975. struct fec_enet_private *fep = netdev_priv(ndev);
  976. u32 ecntl = FEC_ECR_ETHEREN;
  977. u32 rcntl = FEC_RCR_MII;
  978. if (OPT_ARCH_HAS_MAX_FL)
  979. rcntl |= (fep->netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN) << 16;
  980. if (fep->bufdesc_ex)
  981. fec_ptp_save_state(fep);
  982. fec_ctrl_reset(fep, false);
  983. /*
  984. * enet-mac reset will reset mac address registers too,
  985. * so need to reconfigure it.
  986. */
  987. fec_set_hw_mac_addr(ndev);
  988. /* Clear any outstanding interrupt, except MDIO. */
  989. writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
  990. fec_enet_bd_init(ndev);
  991. fec_enet_enable_ring(ndev);
  992. /* Enable MII mode */
  993. if (fep->full_duplex == DUPLEX_FULL) {
  994. /* FD enable */
  995. writel(0x04, fep->hwp + FEC_X_CNTRL);
  996. } else {
  997. /* No Rcv on Xmit */
  998. rcntl |= FEC_RCR_DRT;
  999. writel(0x0, fep->hwp + FEC_X_CNTRL);
  1000. }
  1001. /* Set MII speed */
  1002. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1003. #if !defined(CONFIG_M5272)
  1004. if (fep->quirks & FEC_QUIRK_HAS_RACC) {
  1005. u32 val = readl(fep->hwp + FEC_RACC);
  1006. /* align IP header */
  1007. val |= FEC_RACC_SHIFT16;
  1008. if (fep->csum_flags & FLAG_RX_CSUM_ENABLED)
  1009. /* set RX checksum */
  1010. val |= FEC_RACC_OPTIONS;
  1011. else
  1012. val &= ~FEC_RACC_OPTIONS;
  1013. writel(val, fep->hwp + FEC_RACC);
  1014. writel(min(fep->rx_frame_size, fep->max_buf_size), fep->hwp + FEC_FTRL);
  1015. }
  1016. #endif
  1017. /*
  1018. * The phy interface and speed need to get configured
  1019. * differently on enet-mac.
  1020. */
  1021. if (fep->quirks & FEC_QUIRK_ENET_MAC) {
  1022. /* Enable flow control and length check */
  1023. rcntl |= FEC_RCR_NLC | FEC_RCR_FLOWCTL;
  1024. /* RGMII, RMII or MII */
  1025. if (phy_interface_mode_is_rgmii(fep->phy_interface))
  1026. rcntl |= FEC_RCR_RGMII;
  1027. else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  1028. rcntl |= FEC_RCR_RMII;
  1029. else
  1030. rcntl &= ~FEC_RCR_RMII;
  1031. /* 1G, 100M or 10M */
  1032. if (ndev->phydev) {
  1033. if (ndev->phydev->speed == SPEED_1000)
  1034. ecntl |= FEC_ECR_SPEED;
  1035. else if (ndev->phydev->speed == SPEED_100)
  1036. rcntl &= ~FEC_RCR_10BASET;
  1037. else
  1038. rcntl |= FEC_RCR_10BASET;
  1039. }
  1040. } else {
  1041. #ifdef FEC_MIIGSK_ENR
  1042. if (fep->quirks & FEC_QUIRK_USE_GASKET) {
  1043. u32 cfgr;
  1044. /* disable the gasket and wait */
  1045. writel(0, fep->hwp + FEC_MIIGSK_ENR);
  1046. while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4)
  1047. udelay(1);
  1048. /*
  1049. * configure the gasket:
  1050. * RMII, 50 MHz, no loopback, no echo
  1051. * MII, 25 MHz, no loopback, no echo
  1052. */
  1053. cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII)
  1054. ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII;
  1055. if (ndev->phydev && ndev->phydev->speed == SPEED_10)
  1056. cfgr |= BM_MIIGSK_CFGR_FRCONT_10M;
  1057. writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR);
  1058. /* re-enable the gasket */
  1059. writel(2, fep->hwp + FEC_MIIGSK_ENR);
  1060. }
  1061. #endif
  1062. }
  1063. #if !defined(CONFIG_M5272)
  1064. /* enable pause frame*/
  1065. if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) ||
  1066. ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) &&
  1067. ndev->phydev && ndev->phydev->pause)) {
  1068. rcntl |= FEC_RCR_FLOWCTL;
  1069. /* set FIFO threshold parameter to reduce overrun */
  1070. writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM);
  1071. writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL);
  1072. writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM);
  1073. writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL);
  1074. /* OPD */
  1075. writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD);
  1076. } else {
  1077. rcntl &= ~FEC_RCR_FLOWCTL;
  1078. }
  1079. #endif /* !defined(CONFIG_M5272) */
  1080. writel(rcntl, fep->hwp + FEC_R_CNTRL);
  1081. /* Setup multicast filter. */
  1082. set_multicast_list(ndev);
  1083. #ifndef CONFIG_M5272
  1084. writel(0, fep->hwp + FEC_HASH_TABLE_HIGH);
  1085. writel(0, fep->hwp + FEC_HASH_TABLE_LOW);
  1086. #endif
  1087. if (fep->quirks & FEC_QUIRK_ENET_MAC) {
  1088. /* enable ENET endian swap */
  1089. ecntl |= FEC_ECR_BYTESWP;
  1090. /* When Jumbo Frame is enabled, the FIFO may not be large enough
  1091. * to hold an entire frame. In such cases, if the MTU exceeds
  1092. * (PKT_MAXBUF_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN), configure
  1093. * the interface to operate in cut-through mode, triggered by
  1094. * the FIFO threshold.
  1095. * Otherwise, enable the ENET store-and-forward mode.
  1096. */
  1097. if ((fep->quirks & FEC_QUIRK_JUMBO_FRAME) &&
  1098. (ndev->mtu > (PKT_MAXBUF_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN)))
  1099. writel(0xF, fep->hwp + FEC_X_WMRK);
  1100. else
  1101. writel(FEC_TXWMRK_STRFWD, fep->hwp + FEC_X_WMRK);
  1102. }
  1103. if (fep->bufdesc_ex)
  1104. ecntl |= FEC_ECR_EN1588;
  1105. if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
  1106. fep->rgmii_txc_dly)
  1107. ecntl |= FEC_ENET_TXC_DLY;
  1108. if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
  1109. fep->rgmii_rxc_dly)
  1110. ecntl |= FEC_ENET_RXC_DLY;
  1111. #ifndef CONFIG_M5272
  1112. /* Enable the MIB statistic event counters */
  1113. writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT);
  1114. #endif
  1115. /* And last, enable the transmit and receive processing */
  1116. writel(ecntl, fep->hwp + FEC_ECNTRL);
  1117. fec_enet_active_rxring(ndev);
  1118. if (fep->bufdesc_ex) {
  1119. fec_ptp_start_cyclecounter(ndev);
  1120. fec_ptp_restore_state(fep);
  1121. }
  1122. /* Enable interrupts we wish to service */
  1123. if (fep->link)
  1124. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  1125. else
  1126. writel(0, fep->hwp + FEC_IMASK);
  1127. /* Init the interrupt coalescing */
  1128. if (fep->quirks & FEC_QUIRK_HAS_COALESCE)
  1129. fec_enet_itr_coal_set(ndev);
  1130. }
  1131. static int fec_enet_ipc_handle_init(struct fec_enet_private *fep)
  1132. {
  1133. if (!(of_machine_is_compatible("fsl,imx8qm") ||
  1134. of_machine_is_compatible("fsl,imx8qp") ||
  1135. of_machine_is_compatible("fsl,imx8qxp") ||
  1136. of_machine_is_compatible("fsl,imx8dx") ||
  1137. of_machine_is_compatible("fsl,imx8dxl")))
  1138. return 0;
  1139. return imx_scu_get_handle(&fep->ipc_handle);
  1140. }
  1141. static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled)
  1142. {
  1143. struct device_node *np = fep->pdev->dev.of_node;
  1144. u32 rsrc_id, val;
  1145. int idx;
  1146. if (!np || !fep->ipc_handle)
  1147. return;
  1148. idx = of_alias_get_id(np, "ethernet");
  1149. if (idx < 0)
  1150. idx = 0;
  1151. rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0;
  1152. val = enabled ? 1 : 0;
  1153. imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val);
  1154. }
  1155. static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled)
  1156. {
  1157. struct fec_platform_data *pdata = fep->pdev->dev.platform_data;
  1158. struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr;
  1159. if (stop_gpr->gpr) {
  1160. if (enabled)
  1161. regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
  1162. BIT(stop_gpr->bit),
  1163. BIT(stop_gpr->bit));
  1164. else
  1165. regmap_update_bits(stop_gpr->gpr, stop_gpr->reg,
  1166. BIT(stop_gpr->bit), 0);
  1167. } else if (pdata && pdata->sleep_mode_enable) {
  1168. pdata->sleep_mode_enable(enabled);
  1169. } else {
  1170. fec_enet_ipg_stop_set(fep, enabled);
  1171. }
  1172. }
  1173. static void fec_irqs_disable(struct net_device *ndev)
  1174. {
  1175. struct fec_enet_private *fep = netdev_priv(ndev);
  1176. writel(0, fep->hwp + FEC_IMASK);
  1177. }
  1178. static void fec_irqs_disable_except_wakeup(struct net_device *ndev)
  1179. {
  1180. struct fec_enet_private *fep = netdev_priv(ndev);
  1181. writel(0, fep->hwp + FEC_IMASK);
  1182. writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK);
  1183. }
  1184. static void
  1185. fec_stop(struct net_device *ndev)
  1186. {
  1187. struct fec_enet_private *fep = netdev_priv(ndev);
  1188. u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & FEC_RCR_RMII;
  1189. u32 val;
  1190. /* We cannot expect a graceful transmit stop without link !!! */
  1191. if (fep->link) {
  1192. writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */
  1193. udelay(10);
  1194. if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA))
  1195. netdev_err(ndev, "Graceful transmit stop did not complete!\n");
  1196. }
  1197. if (fep->bufdesc_ex)
  1198. fec_ptp_save_state(fep);
  1199. fec_ctrl_reset(fep, true);
  1200. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  1201. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  1202. /* We have to keep ENET enabled to have MII interrupt stay working */
  1203. if (fep->quirks & FEC_QUIRK_ENET_MAC &&
  1204. !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
  1205. writel(FEC_ECR_ETHEREN, fep->hwp + FEC_ECNTRL);
  1206. writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
  1207. }
  1208. if (fep->bufdesc_ex) {
  1209. val = readl(fep->hwp + FEC_ECNTRL);
  1210. val |= FEC_ECR_EN1588;
  1211. writel(val, fep->hwp + FEC_ECNTRL);
  1212. fec_ptp_start_cyclecounter(ndev);
  1213. fec_ptp_restore_state(fep);
  1214. }
  1215. }
  1216. static void
  1217. fec_timeout(struct net_device *ndev, unsigned int txqueue)
  1218. {
  1219. struct fec_enet_private *fep = netdev_priv(ndev);
  1220. fec_dump(ndev);
  1221. ndev->stats.tx_errors++;
  1222. schedule_work(&fep->tx_timeout_work);
  1223. }
  1224. static void fec_enet_timeout_work(struct work_struct *work)
  1225. {
  1226. struct fec_enet_private *fep =
  1227. container_of(work, struct fec_enet_private, tx_timeout_work);
  1228. struct net_device *ndev = fep->netdev;
  1229. rtnl_lock();
  1230. if (netif_device_present(ndev) || netif_running(ndev)) {
  1231. napi_disable(&fep->napi);
  1232. netif_tx_lock_bh(ndev);
  1233. fec_restart(ndev);
  1234. netif_tx_wake_all_queues(ndev);
  1235. netif_tx_unlock_bh(ndev);
  1236. napi_enable(&fep->napi);
  1237. }
  1238. rtnl_unlock();
  1239. }
  1240. static void
  1241. fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts,
  1242. struct skb_shared_hwtstamps *hwtstamps)
  1243. {
  1244. unsigned long flags;
  1245. u64 ns;
  1246. spin_lock_irqsave(&fep->tmreg_lock, flags);
  1247. ns = timecounter_cyc2time(&fep->tc, ts);
  1248. spin_unlock_irqrestore(&fep->tmreg_lock, flags);
  1249. memset(hwtstamps, 0, sizeof(*hwtstamps));
  1250. hwtstamps->hwtstamp = ns_to_ktime(ns);
  1251. }
  1252. static bool fec_enet_xsk_xmit(struct fec_enet_private *fep,
  1253. struct xsk_buff_pool *pool,
  1254. u32 queue)
  1255. {
  1256. struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
  1257. struct xdp_desc *xsk_desc = pool->tx_descs;
  1258. int cpu = smp_processor_id();
  1259. int free_bds, budget, batch;
  1260. struct netdev_queue *nq;
  1261. struct bufdesc *bdp;
  1262. dma_addr_t dma;
  1263. u32 estatus;
  1264. u16 status;
  1265. int i, j;
  1266. nq = netdev_get_tx_queue(fep->netdev, queue);
  1267. __netif_tx_lock(nq, cpu);
  1268. txq_trans_cond_update(nq);
  1269. free_bds = fec_enet_get_free_txdesc_num(txq);
  1270. if (!free_bds)
  1271. goto tx_unlock;
  1272. budget = min(free_bds, FEC_XSK_TX_BUDGET_MAX);
  1273. batch = xsk_tx_peek_release_desc_batch(pool, budget);
  1274. if (!batch)
  1275. goto tx_unlock;
  1276. bdp = txq->bd.cur;
  1277. for (i = 0; i < batch; i++) {
  1278. dma = xsk_buff_raw_get_dma(pool, xsk_desc[i].addr);
  1279. xsk_buff_raw_dma_sync_for_device(pool, dma, xsk_desc[i].len);
  1280. j = fec_enet_get_bd_index(bdp, &txq->bd);
  1281. txq->tx_buf[j].type = FEC_TXBUF_T_XSK_XMIT;
  1282. txq->tx_buf[j].buf_p = NULL;
  1283. status = fec16_to_cpu(bdp->cbd_sc);
  1284. status &= ~BD_ENET_TX_STATS;
  1285. status |= BD_ENET_TX_INTR | BD_ENET_TX_LAST;
  1286. bdp->cbd_datlen = cpu_to_fec16(xsk_desc[i].len);
  1287. bdp->cbd_bufaddr = cpu_to_fec32(dma);
  1288. if (fep->bufdesc_ex) {
  1289. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1290. estatus = BD_ENET_TX_INT;
  1291. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  1292. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  1293. ebdp->cbd_bdu = 0;
  1294. ebdp->cbd_esc = cpu_to_fec32(estatus);
  1295. }
  1296. /* Make sure the updates to rest of the descriptor are performed
  1297. * before transferring ownership.
  1298. */
  1299. dma_wmb();
  1300. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  1301. * it's the last BD of the frame, and to put the CRC on the end.
  1302. */
  1303. status |= BD_ENET_TX_READY | BD_ENET_TX_TC;
  1304. bdp->cbd_sc = cpu_to_fec16(status);
  1305. dma_wmb();
  1306. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1307. txq->bd.cur = bdp;
  1308. }
  1309. /* Trigger transmission start */
  1310. fec_txq_trigger_xmit(fep, txq);
  1311. __netif_tx_unlock(nq);
  1312. return batch < budget;
  1313. tx_unlock:
  1314. __netif_tx_unlock(nq);
  1315. return true;
  1316. }
  1317. static int fec_enet_tx_queue(struct fec_enet_private *fep,
  1318. u16 queue, int budget)
  1319. {
  1320. struct netdev_queue *nq = netdev_get_tx_queue(fep->netdev, queue);
  1321. struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
  1322. struct net_device *ndev = fep->netdev;
  1323. struct bufdesc *bdp = txq->dirty_tx;
  1324. int index, frame_len, entries_free;
  1325. struct fec_tx_buffer *tx_buf;
  1326. unsigned short status;
  1327. struct sk_buff *skb;
  1328. struct page *page;
  1329. int xsk_cnt = 0;
  1330. /* get next bdp of dirty_tx */
  1331. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1332. while (bdp != READ_ONCE(txq->bd.cur)) {
  1333. /* Order the load of bd.cur and cbd_sc */
  1334. rmb();
  1335. status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc));
  1336. if (status & BD_ENET_TX_READY)
  1337. break;
  1338. index = fec_enet_get_bd_index(bdp, &txq->bd);
  1339. tx_buf = &txq->tx_buf[index];
  1340. frame_len = fec16_to_cpu(bdp->cbd_datlen);
  1341. switch (tx_buf->type) {
  1342. case FEC_TXBUF_T_SKB:
  1343. if (bdp->cbd_bufaddr &&
  1344. !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr)))
  1345. dma_unmap_single(&fep->pdev->dev,
  1346. fec32_to_cpu(bdp->cbd_bufaddr),
  1347. frame_len, DMA_TO_DEVICE);
  1348. bdp->cbd_bufaddr = cpu_to_fec32(0);
  1349. skb = tx_buf->buf_p;
  1350. if (!skb)
  1351. goto tx_buf_done;
  1352. frame_len = skb->len;
  1353. /* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who
  1354. * are to time stamp the packet, so we still need to check time
  1355. * stamping enabled flag.
  1356. */
  1357. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS &&
  1358. fep->hwts_tx_en) && fep->bufdesc_ex) {
  1359. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1360. struct skb_shared_hwtstamps shhwtstamps;
  1361. fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps);
  1362. skb_tstamp_tx(skb, &shhwtstamps);
  1363. }
  1364. /* Free the sk buffer associated with this last transmit */
  1365. napi_consume_skb(skb, budget);
  1366. break;
  1367. case FEC_TXBUF_T_XDP_NDO:
  1368. /* Tx processing cannot call any XDP (or page pool) APIs if
  1369. * the "budget" is 0. Because NAPI is called with budget of
  1370. * 0 (such as netpoll) indicates we may be in an IRQ context,
  1371. * however, we can't use the page pool from IRQ context.
  1372. */
  1373. if (unlikely(!budget))
  1374. goto out;
  1375. dma_unmap_single(&fep->pdev->dev,
  1376. fec32_to_cpu(bdp->cbd_bufaddr),
  1377. frame_len, DMA_TO_DEVICE);
  1378. bdp->cbd_bufaddr = cpu_to_fec32(0);
  1379. xdp_return_frame_rx_napi(tx_buf->buf_p);
  1380. break;
  1381. case FEC_TXBUF_T_XDP_TX:
  1382. if (unlikely(!budget))
  1383. goto out;
  1384. bdp->cbd_bufaddr = cpu_to_fec32(0);
  1385. page = tx_buf->buf_p;
  1386. /* The dma_sync_size = 0 as XDP_TX has already synced
  1387. * DMA for_device
  1388. */
  1389. page_pool_put_page(pp_page_to_nmdesc(page)->pp, page,
  1390. 0, true);
  1391. break;
  1392. case FEC_TXBUF_T_XSK_XMIT:
  1393. bdp->cbd_bufaddr = cpu_to_fec32(0);
  1394. xsk_cnt++;
  1395. break;
  1396. case FEC_TXBUF_T_XSK_TX:
  1397. bdp->cbd_bufaddr = cpu_to_fec32(0);
  1398. xsk_buff_free(tx_buf->buf_p);
  1399. break;
  1400. default:
  1401. break;
  1402. }
  1403. /* Check for errors. */
  1404. if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC |
  1405. BD_ENET_TX_RL | BD_ENET_TX_UN |
  1406. BD_ENET_TX_CSL)) {
  1407. ndev->stats.tx_errors++;
  1408. if (status & BD_ENET_TX_HB) /* No heartbeat */
  1409. ndev->stats.tx_heartbeat_errors++;
  1410. if (status & BD_ENET_TX_LC) /* Late collision */
  1411. ndev->stats.tx_window_errors++;
  1412. if (status & BD_ENET_TX_RL) /* Retrans limit */
  1413. ndev->stats.tx_aborted_errors++;
  1414. if (status & BD_ENET_TX_UN) /* Underrun */
  1415. ndev->stats.tx_fifo_errors++;
  1416. if (status & BD_ENET_TX_CSL) /* Carrier lost */
  1417. ndev->stats.tx_carrier_errors++;
  1418. } else {
  1419. ndev->stats.tx_packets++;
  1420. ndev->stats.tx_bytes += frame_len;
  1421. }
  1422. /* Deferred means some collisions occurred during transmit,
  1423. * but we eventually sent the packet OK.
  1424. */
  1425. if (status & BD_ENET_TX_DEF)
  1426. ndev->stats.collisions++;
  1427. tx_buf->buf_p = NULL;
  1428. /* restore default tx buffer type: FEC_TXBUF_T_SKB */
  1429. tx_buf->type = FEC_TXBUF_T_SKB;
  1430. tx_buf_done:
  1431. /* Make sure the update to bdp and tx_buf are performed
  1432. * before dirty_tx
  1433. */
  1434. wmb();
  1435. txq->dirty_tx = bdp;
  1436. /* Update pointer to next buffer descriptor to be transmitted */
  1437. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1438. /* Since we have freed up a buffer, the ring is no longer full
  1439. */
  1440. if (netif_tx_queue_stopped(nq)) {
  1441. entries_free = fec_enet_get_free_txdesc_num(txq);
  1442. if (entries_free >= txq->tx_wake_threshold)
  1443. netif_tx_wake_queue(nq);
  1444. }
  1445. }
  1446. out:
  1447. /* ERR006358: Keep the transmitter going */
  1448. if (bdp != txq->bd.cur &&
  1449. readl(txq->bd.reg_desc_active) == 0)
  1450. writel(0, txq->bd.reg_desc_active);
  1451. if (txq->xsk_pool) {
  1452. struct xsk_buff_pool *pool = txq->xsk_pool;
  1453. if (xsk_cnt)
  1454. xsk_tx_completed(pool, xsk_cnt);
  1455. if (xsk_uses_need_wakeup(pool))
  1456. xsk_set_tx_need_wakeup(pool);
  1457. /* If the condition is true, it indicates that there are still
  1458. * packets to be transmitted, so return "budget" to make the
  1459. * NAPI continue polling.
  1460. */
  1461. if (!fec_enet_xsk_xmit(fep, pool, queue))
  1462. return budget;
  1463. }
  1464. return 0;
  1465. }
  1466. static int fec_enet_tx(struct net_device *ndev, int budget)
  1467. {
  1468. struct fec_enet_private *fep = netdev_priv(ndev);
  1469. int i, count = 0;
  1470. /* Make sure that AVB queues are processed first. */
  1471. for (i = fep->num_tx_queues - 1; i >= 0; i--)
  1472. count += fec_enet_tx_queue(fep, i, budget);
  1473. return count;
  1474. }
  1475. static int fec_enet_update_cbd(struct fec_enet_priv_rx_q *rxq,
  1476. struct bufdesc *bdp, int index)
  1477. {
  1478. struct page *new_page;
  1479. dma_addr_t phys_addr;
  1480. new_page = page_pool_dev_alloc_pages(rxq->page_pool);
  1481. if (unlikely(!new_page))
  1482. return -ENOMEM;
  1483. rxq->rx_buf[index].page = new_page;
  1484. phys_addr = page_pool_get_dma_addr(new_page) + FEC_ENET_XDP_HEADROOM;
  1485. bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
  1486. return 0;
  1487. }
  1488. static int fec_enet_update_cbd_zc(struct fec_enet_priv_rx_q *rxq,
  1489. struct bufdesc *bdp, int index)
  1490. {
  1491. struct xdp_buff *new_xdp;
  1492. dma_addr_t phys_addr;
  1493. new_xdp = xsk_buff_alloc(rxq->xsk_pool);
  1494. if (unlikely(!new_xdp))
  1495. return -ENOMEM;
  1496. rxq->rx_buf[index].xdp = new_xdp;
  1497. phys_addr = xsk_buff_xdp_get_dma(new_xdp);
  1498. bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
  1499. return 0;
  1500. }
  1501. static void fec_enet_rx_vlan(const struct net_device *ndev, struct sk_buff *skb)
  1502. {
  1503. if (ndev->features & NETIF_F_HW_VLAN_CTAG_RX) {
  1504. const struct vlan_ethhdr *vlan_header = skb_vlan_eth_hdr(skb);
  1505. const u16 vlan_tag = ntohs(vlan_header->h_vlan_TCI);
  1506. /* Push and remove the vlan tag */
  1507. memmove(skb->data + VLAN_HLEN, skb->data, ETH_ALEN * 2);
  1508. skb_pull(skb, VLAN_HLEN);
  1509. __vlan_hwaccel_put_tag(skb,
  1510. htons(ETH_P_8021Q),
  1511. vlan_tag);
  1512. }
  1513. }
  1514. static int fec_rx_error_check(struct net_device *ndev, u16 status)
  1515. {
  1516. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO |
  1517. BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST |
  1518. BD_ENET_RX_CL)) {
  1519. ndev->stats.rx_errors++;
  1520. if (status & BD_ENET_RX_OV) {
  1521. /* FIFO overrun */
  1522. ndev->stats.rx_fifo_errors++;
  1523. return -EIO;
  1524. }
  1525. if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH |
  1526. BD_ENET_RX_LAST)) {
  1527. /* Frame too long or too short. */
  1528. ndev->stats.rx_length_errors++;
  1529. if ((status & BD_ENET_RX_LAST) && net_ratelimit())
  1530. netdev_err(ndev, "rcv is not +last\n");
  1531. }
  1532. /* CRC Error */
  1533. if (status & BD_ENET_RX_CR)
  1534. ndev->stats.rx_crc_errors++;
  1535. /* Report late collisions as a frame error. */
  1536. if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL))
  1537. ndev->stats.rx_frame_errors++;
  1538. return -EIO;
  1539. }
  1540. return 0;
  1541. }
  1542. static struct sk_buff *fec_build_skb(struct fec_enet_private *fep,
  1543. struct fec_enet_priv_rx_q *rxq,
  1544. struct bufdesc *bdp,
  1545. struct page *page, u32 len)
  1546. {
  1547. struct net_device *ndev = fep->netdev;
  1548. struct bufdesc_ex *ebdp;
  1549. struct sk_buff *skb;
  1550. skb = build_skb(page_address(page),
  1551. PAGE_SIZE << fep->pagepool_order);
  1552. if (unlikely(!skb)) {
  1553. page_pool_recycle_direct(rxq->page_pool, page);
  1554. ndev->stats.rx_dropped++;
  1555. if (net_ratelimit())
  1556. netdev_err(ndev, "build_skb failed\n");
  1557. return NULL;
  1558. }
  1559. skb_reserve(skb, FEC_ENET_XDP_HEADROOM + fep->rx_shift);
  1560. skb_put(skb, len);
  1561. skb_mark_for_recycle(skb);
  1562. /* Get offloads from the enhanced buffer descriptor */
  1563. if (fep->bufdesc_ex) {
  1564. ebdp = (struct bufdesc_ex *)bdp;
  1565. /* If this is a VLAN packet remove the VLAN Tag */
  1566. if (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))
  1567. fec_enet_rx_vlan(ndev, skb);
  1568. /* Get receive timestamp from the skb */
  1569. if (fep->hwts_rx_en)
  1570. fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts),
  1571. skb_hwtstamps(skb));
  1572. if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) {
  1573. if (!(ebdp->cbd_esc &
  1574. cpu_to_fec32(FLAG_RX_CSUM_ERROR)))
  1575. /* don't check it */
  1576. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1577. else
  1578. skb_checksum_none_assert(skb);
  1579. }
  1580. }
  1581. skb->protocol = eth_type_trans(skb, ndev);
  1582. skb_record_rx_queue(skb, rxq->bd.qid);
  1583. return skb;
  1584. }
  1585. /* During a receive, the bd_rx.cur points to the current incoming buffer.
  1586. * When we update through the ring, if the next incoming buffer has
  1587. * not been given to the system, we just set the empty indicator,
  1588. * effectively tossing the packet.
  1589. */
  1590. static int fec_enet_rx_queue(struct fec_enet_private *fep,
  1591. u16 queue, int budget)
  1592. {
  1593. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
  1594. bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME;
  1595. struct net_device *ndev = fep->netdev;
  1596. struct bufdesc *bdp = rxq->bd.cur;
  1597. u32 sub_len = 4 + fep->rx_shift;
  1598. int pkt_received = 0;
  1599. u16 status, pkt_len;
  1600. struct sk_buff *skb;
  1601. struct page *page;
  1602. dma_addr_t dma;
  1603. int index;
  1604. #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
  1605. /*
  1606. * Hacky flush of all caches instead of using the DMA API for the TSO
  1607. * headers.
  1608. */
  1609. flush_cache_all();
  1610. #endif
  1611. /* First, grab all of the stats for the incoming packet.
  1612. * These get messed up if we get called due to a busy condition.
  1613. */
  1614. while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
  1615. if (pkt_received >= budget)
  1616. break;
  1617. pkt_received++;
  1618. writel(FEC_ENET_RXF_GET(queue), fep->hwp + FEC_IEVENT);
  1619. /* Check for errors. */
  1620. status ^= BD_ENET_RX_LAST;
  1621. if (unlikely(fec_rx_error_check(ndev, status)))
  1622. goto rx_processing_done;
  1623. /* Process the incoming frame. */
  1624. ndev->stats.rx_packets++;
  1625. pkt_len = fec16_to_cpu(bdp->cbd_datlen);
  1626. ndev->stats.rx_bytes += pkt_len - fep->rx_shift;
  1627. index = fec_enet_get_bd_index(bdp, &rxq->bd);
  1628. page = rxq->rx_buf[index].page;
  1629. dma = fec32_to_cpu(bdp->cbd_bufaddr);
  1630. if (fec_enet_update_cbd(rxq, bdp, index)) {
  1631. ndev->stats.rx_dropped++;
  1632. goto rx_processing_done;
  1633. }
  1634. dma_sync_single_for_cpu(&fep->pdev->dev, dma, pkt_len,
  1635. DMA_FROM_DEVICE);
  1636. prefetch(page_address(page));
  1637. if (unlikely(need_swap)) {
  1638. u8 *data;
  1639. data = page_address(page) + FEC_ENET_XDP_HEADROOM;
  1640. swap_buffer(data, pkt_len);
  1641. }
  1642. /* The packet length includes FCS, but we don't want to
  1643. * include that when passing upstream as it messes up
  1644. * bridging applications.
  1645. */
  1646. skb = fec_build_skb(fep, rxq, bdp, page, pkt_len - sub_len);
  1647. if (!skb)
  1648. goto rx_processing_done;
  1649. napi_gro_receive(&fep->napi, skb);
  1650. rx_processing_done:
  1651. /* Clear the status flags for this buffer */
  1652. status &= ~BD_ENET_RX_STATS;
  1653. /* Mark the buffer empty */
  1654. status |= BD_ENET_RX_EMPTY;
  1655. if (fep->bufdesc_ex) {
  1656. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1657. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  1658. ebdp->cbd_prot = 0;
  1659. ebdp->cbd_bdu = 0;
  1660. }
  1661. /* Make sure the updates to rest of the descriptor are
  1662. * performed before transferring ownership.
  1663. */
  1664. wmb();
  1665. bdp->cbd_sc = cpu_to_fec16(status);
  1666. /* Update BD pointer to next entry */
  1667. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  1668. /* Doing this here will keep the FEC running while we process
  1669. * incoming frames. On a heavily loaded network, we should be
  1670. * able to keep up at the expense of system resources.
  1671. */
  1672. writel(0, rxq->bd.reg_desc_active);
  1673. }
  1674. rxq->bd.cur = bdp;
  1675. return pkt_received;
  1676. }
  1677. static void fec_xdp_drop(struct fec_enet_priv_rx_q *rxq,
  1678. struct xdp_buff *xdp, u32 sync)
  1679. {
  1680. struct page *page = virt_to_head_page(xdp->data);
  1681. page_pool_put_page(rxq->page_pool, page, sync, true);
  1682. }
  1683. static int
  1684. fec_enet_xdp_get_tx_queue(struct fec_enet_private *fep, int index)
  1685. {
  1686. if (unlikely(index < 0))
  1687. return 0;
  1688. return (index % fep->num_tx_queues);
  1689. }
  1690. static int fec_enet_rx_queue_xdp(struct fec_enet_private *fep, int queue,
  1691. int budget, struct bpf_prog *prog)
  1692. {
  1693. u32 data_start = FEC_ENET_XDP_HEADROOM + fep->rx_shift;
  1694. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
  1695. struct net_device *ndev = fep->netdev;
  1696. struct bufdesc *bdp = rxq->bd.cur;
  1697. u32 sub_len = 4 + fep->rx_shift;
  1698. int cpu = smp_processor_id();
  1699. int pkt_received = 0;
  1700. struct sk_buff *skb;
  1701. u16 status, pkt_len;
  1702. struct xdp_buff xdp;
  1703. int tx_qid = queue;
  1704. struct page *page;
  1705. u32 xdp_res = 0;
  1706. dma_addr_t dma;
  1707. int index, err;
  1708. u32 act, sync;
  1709. #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
  1710. /*
  1711. * Hacky flush of all caches instead of using the DMA API for the TSO
  1712. * headers.
  1713. */
  1714. flush_cache_all();
  1715. #endif
  1716. if (unlikely(tx_qid >= fep->num_tx_queues))
  1717. tx_qid = fec_enet_xdp_get_tx_queue(fep, cpu);
  1718. xdp_init_buff(&xdp, PAGE_SIZE << fep->pagepool_order, &rxq->xdp_rxq);
  1719. while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
  1720. if (pkt_received >= budget)
  1721. break;
  1722. pkt_received++;
  1723. writel(FEC_ENET_RXF_GET(queue), fep->hwp + FEC_IEVENT);
  1724. /* Check for errors. */
  1725. status ^= BD_ENET_RX_LAST;
  1726. if (unlikely(fec_rx_error_check(ndev, status)))
  1727. goto rx_processing_done;
  1728. /* Process the incoming frame. */
  1729. ndev->stats.rx_packets++;
  1730. pkt_len = fec16_to_cpu(bdp->cbd_datlen);
  1731. ndev->stats.rx_bytes += pkt_len - fep->rx_shift;
  1732. index = fec_enet_get_bd_index(bdp, &rxq->bd);
  1733. page = rxq->rx_buf[index].page;
  1734. dma = fec32_to_cpu(bdp->cbd_bufaddr);
  1735. if (fec_enet_update_cbd(rxq, bdp, index)) {
  1736. ndev->stats.rx_dropped++;
  1737. goto rx_processing_done;
  1738. }
  1739. dma_sync_single_for_cpu(&fep->pdev->dev, dma, pkt_len,
  1740. DMA_FROM_DEVICE);
  1741. prefetch(page_address(page));
  1742. xdp_buff_clear_frags_flag(&xdp);
  1743. /* subtract 16bit shift and FCS */
  1744. pkt_len -= sub_len;
  1745. xdp_prepare_buff(&xdp, page_address(page), data_start,
  1746. pkt_len, false);
  1747. act = bpf_prog_run_xdp(prog, &xdp);
  1748. /* Due xdp_adjust_tail and xdp_adjust_head: DMA sync
  1749. * for_device cover max len CPU touch.
  1750. */
  1751. sync = xdp.data_end - xdp.data;
  1752. sync = max(sync, pkt_len);
  1753. switch (act) {
  1754. case XDP_PASS:
  1755. rxq->stats[RX_XDP_PASS]++;
  1756. /* The packet length includes FCS, but we don't want to
  1757. * include that when passing upstream as it messes up
  1758. * bridging applications.
  1759. */
  1760. skb = fec_build_skb(fep, rxq, bdp, page, pkt_len);
  1761. if (!skb)
  1762. trace_xdp_exception(ndev, prog, XDP_PASS);
  1763. else
  1764. napi_gro_receive(&fep->napi, skb);
  1765. break;
  1766. case XDP_REDIRECT:
  1767. rxq->stats[RX_XDP_REDIRECT]++;
  1768. err = xdp_do_redirect(ndev, &xdp, prog);
  1769. if (unlikely(err)) {
  1770. fec_xdp_drop(rxq, &xdp, sync);
  1771. trace_xdp_exception(ndev, prog, XDP_REDIRECT);
  1772. } else {
  1773. xdp_res |= FEC_ENET_XDP_REDIR;
  1774. }
  1775. break;
  1776. case XDP_TX:
  1777. rxq->stats[RX_XDP_TX]++;
  1778. err = fec_enet_xdp_tx_xmit(fep, cpu, &xdp, sync, tx_qid);
  1779. if (unlikely(err)) {
  1780. rxq->stats[RX_XDP_TX_ERRORS]++;
  1781. fec_xdp_drop(rxq, &xdp, sync);
  1782. trace_xdp_exception(ndev, prog, XDP_TX);
  1783. } else {
  1784. xdp_res |= FEC_ENET_XDP_TX;
  1785. }
  1786. break;
  1787. default:
  1788. bpf_warn_invalid_xdp_action(ndev, prog, act);
  1789. fallthrough;
  1790. case XDP_ABORTED:
  1791. trace_xdp_exception(ndev, prog, act);
  1792. /* handle aborts by dropping packet */
  1793. fallthrough;
  1794. case XDP_DROP:
  1795. rxq->stats[RX_XDP_DROP]++;
  1796. fec_xdp_drop(rxq, &xdp, sync);
  1797. break;
  1798. }
  1799. rx_processing_done:
  1800. /* Clear the status flags for this buffer */
  1801. status &= ~BD_ENET_RX_STATS;
  1802. /* Mark the buffer empty */
  1803. status |= BD_ENET_RX_EMPTY;
  1804. if (fep->bufdesc_ex) {
  1805. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1806. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  1807. ebdp->cbd_prot = 0;
  1808. ebdp->cbd_bdu = 0;
  1809. }
  1810. /* Make sure the updates to rest of the descriptor are
  1811. * performed before transferring ownership.
  1812. */
  1813. dma_wmb();
  1814. bdp->cbd_sc = cpu_to_fec16(status);
  1815. /* Update BD pointer to next entry */
  1816. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  1817. /* Doing this here will keep the FEC running while we process
  1818. * incoming frames. On a heavily loaded network, we should be
  1819. * able to keep up at the expense of system resources.
  1820. */
  1821. writel(0, rxq->bd.reg_desc_active);
  1822. }
  1823. rxq->bd.cur = bdp;
  1824. if (xdp_res & FEC_ENET_XDP_REDIR)
  1825. xdp_do_flush();
  1826. if (xdp_res & FEC_ENET_XDP_TX)
  1827. /* Trigger transmission start */
  1828. fec_txq_trigger_xmit(fep, fep->tx_queue[tx_qid]);
  1829. return pkt_received;
  1830. }
  1831. static struct sk_buff *fec_build_skb_zc(struct xdp_buff *xsk,
  1832. struct napi_struct *napi)
  1833. {
  1834. size_t len = xdp_get_buff_len(xsk);
  1835. struct sk_buff *skb;
  1836. skb = napi_alloc_skb(napi, len);
  1837. if (unlikely(!skb)) {
  1838. xsk_buff_free(xsk);
  1839. return NULL;
  1840. }
  1841. skb_put_data(skb, xsk->data, len);
  1842. xsk_buff_free(xsk);
  1843. return skb;
  1844. }
  1845. static int fec_enet_xsk_tx_xmit(struct fec_enet_private *fep,
  1846. struct xdp_buff *xsk, int cpu,
  1847. int queue)
  1848. {
  1849. struct netdev_queue *nq = netdev_get_tx_queue(fep->netdev, queue);
  1850. struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
  1851. u32 offset = xsk->data - xsk->data_hard_start;
  1852. u32 headroom = txq->xsk_pool->headroom;
  1853. u32 len = xsk->data_end - xsk->data;
  1854. u32 index, status, estatus;
  1855. struct bufdesc *bdp;
  1856. dma_addr_t dma;
  1857. __netif_tx_lock(nq, cpu);
  1858. /* Avoid tx timeout as XDP shares the queue with kernel stack */
  1859. txq_trans_cond_update(nq);
  1860. if (!fec_enet_get_free_txdesc_num(txq)) {
  1861. __netif_tx_unlock(nq);
  1862. return -EBUSY;
  1863. }
  1864. /* Fill in a Tx ring entry */
  1865. bdp = txq->bd.cur;
  1866. status = fec16_to_cpu(bdp->cbd_sc);
  1867. status &= ~BD_ENET_TX_STATS;
  1868. index = fec_enet_get_bd_index(bdp, &txq->bd);
  1869. dma = xsk_buff_xdp_get_frame_dma(xsk) + headroom + offset;
  1870. xsk_buff_raw_dma_sync_for_device(txq->xsk_pool, dma, len);
  1871. txq->tx_buf[index].buf_p = xsk;
  1872. txq->tx_buf[index].type = FEC_TXBUF_T_XSK_TX;
  1873. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  1874. if (fep->bufdesc_ex)
  1875. estatus = BD_ENET_TX_INT;
  1876. bdp->cbd_bufaddr = cpu_to_fec32(dma);
  1877. bdp->cbd_datlen = cpu_to_fec16(len);
  1878. if (fep->bufdesc_ex) {
  1879. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1880. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  1881. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  1882. ebdp->cbd_bdu = 0;
  1883. ebdp->cbd_esc = cpu_to_fec32(estatus);
  1884. }
  1885. dma_wmb();
  1886. status |= BD_ENET_TX_READY | BD_ENET_TX_TC;
  1887. bdp->cbd_sc = cpu_to_fec16(status);
  1888. dma_wmb();
  1889. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  1890. txq->bd.cur = bdp;
  1891. __netif_tx_unlock(nq);
  1892. return 0;
  1893. }
  1894. static int fec_enet_rx_queue_xsk(struct fec_enet_private *fep, int queue,
  1895. int budget, struct bpf_prog *prog)
  1896. {
  1897. u32 data_start = FEC_ENET_XDP_HEADROOM + fep->rx_shift;
  1898. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
  1899. struct net_device *ndev = fep->netdev;
  1900. struct bufdesc *bdp = rxq->bd.cur;
  1901. u32 sub_len = 4 + fep->rx_shift;
  1902. int cpu = smp_processor_id();
  1903. bool wakeup_xsk = false;
  1904. struct xdp_buff *xsk;
  1905. int pkt_received = 0;
  1906. struct sk_buff *skb;
  1907. u16 status, pkt_len;
  1908. u32 xdp_res = 0;
  1909. int index, err;
  1910. u32 act;
  1911. #if defined(CONFIG_COLDFIRE) && !defined(CONFIG_COLDFIRE_COHERENT_DMA)
  1912. /*
  1913. * Hacky flush of all caches instead of using the DMA API for the TSO
  1914. * headers.
  1915. */
  1916. flush_cache_all();
  1917. #endif
  1918. while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) {
  1919. if (unlikely(pkt_received >= budget))
  1920. break;
  1921. writel(FEC_ENET_RXF_GET(queue), fep->hwp + FEC_IEVENT);
  1922. index = fec_enet_get_bd_index(bdp, &rxq->bd);
  1923. xsk = rxq->rx_buf[index].xdp;
  1924. if (unlikely(!xsk)) {
  1925. if (fec_enet_update_cbd_zc(rxq, bdp, index))
  1926. break;
  1927. if (fep->bufdesc_ex) {
  1928. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  1929. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  1930. ebdp->cbd_prot = 0;
  1931. ebdp->cbd_bdu = 0;
  1932. }
  1933. dma_wmb();
  1934. status &= ~BD_ENET_RX_STATS;
  1935. status |= BD_ENET_RX_EMPTY;
  1936. bdp->cbd_sc = cpu_to_fec16(status);
  1937. break;
  1938. }
  1939. pkt_received++;
  1940. /* Check for errors. */
  1941. status ^= BD_ENET_RX_LAST;
  1942. if (unlikely(fec_rx_error_check(ndev, status)))
  1943. goto rx_processing_done;
  1944. /* Process the incoming frame. */
  1945. ndev->stats.rx_packets++;
  1946. pkt_len = fec16_to_cpu(bdp->cbd_datlen);
  1947. ndev->stats.rx_bytes += pkt_len - fep->rx_shift;
  1948. if (fec_enet_update_cbd_zc(rxq, bdp, index)) {
  1949. ndev->stats.rx_dropped++;
  1950. goto rx_processing_done;
  1951. }
  1952. pkt_len -= sub_len;
  1953. xsk->data = xsk->data_hard_start + data_start;
  1954. /* Subtract FCS and 16bit shift */
  1955. xsk->data_end = xsk->data + pkt_len;
  1956. xsk->data_meta = xsk->data;
  1957. xsk_buff_dma_sync_for_cpu(xsk);
  1958. /* If the XSK pool is enabled before the bpf program is
  1959. * installed, or the bpf program is uninstalled before
  1960. * the XSK pool is disabled. prog will be NULL and we
  1961. * need to set a default XDP_PASS action.
  1962. */
  1963. if (unlikely(!prog))
  1964. act = XDP_PASS;
  1965. else
  1966. act = bpf_prog_run_xdp(prog, xsk);
  1967. switch (act) {
  1968. case XDP_PASS:
  1969. rxq->stats[RX_XDP_PASS]++;
  1970. skb = fec_build_skb_zc(xsk, &fep->napi);
  1971. if (unlikely(!skb)) {
  1972. ndev->stats.rx_dropped++;
  1973. trace_xdp_exception(ndev, prog, XDP_PASS);
  1974. } else {
  1975. napi_gro_receive(&fep->napi, skb);
  1976. }
  1977. break;
  1978. case XDP_TX:
  1979. rxq->stats[RX_XDP_TX]++;
  1980. err = fec_enet_xsk_tx_xmit(fep, xsk, cpu, queue);
  1981. if (unlikely(err)) {
  1982. rxq->stats[RX_XDP_TX_ERRORS]++;
  1983. xsk_buff_free(xsk);
  1984. trace_xdp_exception(ndev, prog, XDP_TX);
  1985. } else {
  1986. xdp_res |= FEC_ENET_XDP_TX;
  1987. }
  1988. break;
  1989. case XDP_REDIRECT:
  1990. rxq->stats[RX_XDP_REDIRECT]++;
  1991. err = xdp_do_redirect(ndev, xsk, prog);
  1992. if (unlikely(err)) {
  1993. if (err == -ENOBUFS)
  1994. wakeup_xsk = true;
  1995. rxq->stats[RX_XDP_DROP]++;
  1996. xsk_buff_free(xsk);
  1997. trace_xdp_exception(ndev, prog, XDP_REDIRECT);
  1998. } else {
  1999. xdp_res |= FEC_ENET_XDP_REDIR;
  2000. }
  2001. break;
  2002. default:
  2003. bpf_warn_invalid_xdp_action(ndev, prog, act);
  2004. fallthrough;
  2005. case XDP_ABORTED:
  2006. trace_xdp_exception(ndev, prog, act);
  2007. fallthrough;
  2008. case XDP_DROP:
  2009. rxq->stats[RX_XDP_DROP]++;
  2010. xsk_buff_free(xsk);
  2011. break;
  2012. }
  2013. rx_processing_done:
  2014. /* Clear the status flags for this buffer */
  2015. status &= ~BD_ENET_RX_STATS;
  2016. /* Mark the buffer empty */
  2017. status |= BD_ENET_RX_EMPTY;
  2018. if (fep->bufdesc_ex) {
  2019. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  2020. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT);
  2021. ebdp->cbd_prot = 0;
  2022. ebdp->cbd_bdu = 0;
  2023. }
  2024. /* Make sure the updates to rest of the descriptor are
  2025. * performed before transferring ownership.
  2026. */
  2027. dma_wmb();
  2028. bdp->cbd_sc = cpu_to_fec16(status);
  2029. /* Update BD pointer to next entry */
  2030. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  2031. /* Doing this here will keep the FEC running while we process
  2032. * incoming frames. On a heavily loaded network, we should be
  2033. * able to keep up at the expense of system resources.
  2034. */
  2035. writel(0, rxq->bd.reg_desc_active);
  2036. }
  2037. rxq->bd.cur = bdp;
  2038. if (xdp_res & FEC_ENET_XDP_REDIR)
  2039. xdp_do_flush();
  2040. if (xdp_res & FEC_ENET_XDP_TX)
  2041. fec_txq_trigger_xmit(fep, fep->tx_queue[queue]);
  2042. if (rxq->xsk_pool && xsk_uses_need_wakeup(rxq->xsk_pool)) {
  2043. if (wakeup_xsk)
  2044. xsk_set_rx_need_wakeup(rxq->xsk_pool);
  2045. else
  2046. xsk_clear_rx_need_wakeup(rxq->xsk_pool);
  2047. }
  2048. return pkt_received;
  2049. }
  2050. static int fec_enet_rx(struct net_device *ndev, int budget)
  2051. {
  2052. struct fec_enet_private *fep = netdev_priv(ndev);
  2053. struct bpf_prog *prog = READ_ONCE(fep->xdp_prog);
  2054. int i, done = 0;
  2055. /* Make sure that AVB queues are processed first. */
  2056. for (i = fep->num_rx_queues - 1; i >= 0; i--) {
  2057. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
  2058. int batch = budget - done;
  2059. if (rxq->xsk_pool)
  2060. done += fec_enet_rx_queue_xsk(fep, i, batch, prog);
  2061. else if (prog)
  2062. done += fec_enet_rx_queue_xdp(fep, i, batch, prog);
  2063. else
  2064. done += fec_enet_rx_queue(fep, i, batch);
  2065. }
  2066. return done;
  2067. }
  2068. static bool fec_enet_collect_events(struct fec_enet_private *fep)
  2069. {
  2070. uint int_events;
  2071. int_events = readl(fep->hwp + FEC_IEVENT);
  2072. /* Don't clear MDIO events, we poll for those */
  2073. int_events &= ~FEC_ENET_MII;
  2074. writel(int_events, fep->hwp + FEC_IEVENT);
  2075. return int_events != 0;
  2076. }
  2077. static irqreturn_t
  2078. fec_enet_interrupt(int irq, void *dev_id)
  2079. {
  2080. struct net_device *ndev = dev_id;
  2081. struct fec_enet_private *fep = netdev_priv(ndev);
  2082. irqreturn_t ret = IRQ_NONE;
  2083. if (fec_enet_collect_events(fep) && fep->link) {
  2084. ret = IRQ_HANDLED;
  2085. if (napi_schedule_prep(&fep->napi)) {
  2086. /* Disable interrupts */
  2087. writel(0, fep->hwp + FEC_IMASK);
  2088. __napi_schedule(&fep->napi);
  2089. }
  2090. }
  2091. return ret;
  2092. }
  2093. static int fec_enet_rx_napi(struct napi_struct *napi, int budget)
  2094. {
  2095. struct net_device *ndev = napi->dev;
  2096. struct fec_enet_private *fep = netdev_priv(ndev);
  2097. int rx_done = 0, tx_done = 0;
  2098. int max_done;
  2099. do {
  2100. rx_done += fec_enet_rx(ndev, budget - rx_done);
  2101. tx_done += fec_enet_tx(ndev, budget);
  2102. max_done = max(rx_done, tx_done);
  2103. } while ((max_done < budget) && fec_enet_collect_events(fep));
  2104. if (max_done < budget) {
  2105. napi_complete_done(napi, max_done);
  2106. writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
  2107. return max_done;
  2108. }
  2109. return budget;
  2110. }
  2111. /* ------------------------------------------------------------------------- */
  2112. static int fec_get_mac(struct net_device *ndev)
  2113. {
  2114. struct fec_enet_private *fep = netdev_priv(ndev);
  2115. unsigned char *iap, tmpaddr[ETH_ALEN];
  2116. int ret;
  2117. /*
  2118. * try to get mac address in following order:
  2119. *
  2120. * 1) module parameter via kernel command line in form
  2121. * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0
  2122. */
  2123. iap = macaddr;
  2124. /*
  2125. * 2) from device tree data
  2126. */
  2127. if (!is_valid_ether_addr(iap)) {
  2128. struct device_node *np = fep->pdev->dev.of_node;
  2129. if (np) {
  2130. ret = of_get_mac_address(np, tmpaddr);
  2131. if (!ret)
  2132. iap = tmpaddr;
  2133. else if (ret == -EPROBE_DEFER)
  2134. return ret;
  2135. }
  2136. }
  2137. /*
  2138. * 3) from flash or fuse (via platform data)
  2139. */
  2140. if (!is_valid_ether_addr(iap)) {
  2141. #ifdef CONFIG_M5272
  2142. if (FEC_FLASHMAC)
  2143. iap = (unsigned char *)FEC_FLASHMAC;
  2144. #else
  2145. struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev);
  2146. if (pdata)
  2147. iap = (unsigned char *)&pdata->mac;
  2148. #endif
  2149. }
  2150. /*
  2151. * 4) FEC mac registers set by bootloader
  2152. */
  2153. if (!is_valid_ether_addr(iap)) {
  2154. *((__be32 *) &tmpaddr[0]) =
  2155. cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW));
  2156. *((__be16 *) &tmpaddr[4]) =
  2157. cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16);
  2158. iap = &tmpaddr[0];
  2159. }
  2160. /*
  2161. * 5) random mac address
  2162. */
  2163. if (!is_valid_ether_addr(iap)) {
  2164. /* Report it and use a random ethernet address instead */
  2165. dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap);
  2166. eth_hw_addr_random(ndev);
  2167. dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n",
  2168. ndev->dev_addr);
  2169. return 0;
  2170. }
  2171. /* Adjust MAC if using macaddr */
  2172. eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0);
  2173. return 0;
  2174. }
  2175. /* ------------------------------------------------------------------------- */
  2176. /*
  2177. * Phy section
  2178. */
  2179. /* LPI Sleep Ts count base on tx clk (clk_ref).
  2180. * The lpi sleep cnt value = X us / (cycle_ns).
  2181. */
  2182. static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us)
  2183. {
  2184. struct fec_enet_private *fep = netdev_priv(ndev);
  2185. return us * (fep->clk_ref_rate / 1000) / 1000;
  2186. }
  2187. static int fec_enet_eee_mode_set(struct net_device *ndev, u32 lpi_timer,
  2188. bool enable)
  2189. {
  2190. struct fec_enet_private *fep = netdev_priv(ndev);
  2191. unsigned int sleep_cycle, wake_cycle;
  2192. if (enable) {
  2193. sleep_cycle = fec_enet_us_to_tx_cycle(ndev, lpi_timer);
  2194. wake_cycle = sleep_cycle;
  2195. } else {
  2196. sleep_cycle = 0;
  2197. wake_cycle = 0;
  2198. }
  2199. writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP);
  2200. writel(wake_cycle, fep->hwp + FEC_LPI_WAKE);
  2201. return 0;
  2202. }
  2203. static void fec_enet_adjust_link(struct net_device *ndev)
  2204. {
  2205. struct fec_enet_private *fep = netdev_priv(ndev);
  2206. struct phy_device *phy_dev = ndev->phydev;
  2207. int status_change = 0;
  2208. /*
  2209. * If the netdev is down, or is going down, we're not interested
  2210. * in link state events, so just mark our idea of the link as down
  2211. * and ignore the event.
  2212. */
  2213. if (!netif_running(ndev) || !netif_device_present(ndev)) {
  2214. fep->link = 0;
  2215. } else if (phy_dev->link) {
  2216. if (!fep->link) {
  2217. fep->link = phy_dev->link;
  2218. status_change = 1;
  2219. }
  2220. if (fep->full_duplex != phy_dev->duplex) {
  2221. fep->full_duplex = phy_dev->duplex;
  2222. status_change = 1;
  2223. }
  2224. if (phy_dev->speed != fep->speed) {
  2225. fep->speed = phy_dev->speed;
  2226. status_change = 1;
  2227. }
  2228. /* if any of the above changed restart the FEC */
  2229. if (status_change) {
  2230. netif_stop_queue(ndev);
  2231. napi_disable(&fep->napi);
  2232. netif_tx_lock_bh(ndev);
  2233. fec_restart(ndev);
  2234. netif_tx_wake_all_queues(ndev);
  2235. netif_tx_unlock_bh(ndev);
  2236. napi_enable(&fep->napi);
  2237. }
  2238. if (fep->quirks & FEC_QUIRK_HAS_EEE)
  2239. fec_enet_eee_mode_set(ndev,
  2240. phy_dev->eee_cfg.tx_lpi_timer,
  2241. phy_dev->enable_tx_lpi);
  2242. } else {
  2243. if (fep->link) {
  2244. netif_stop_queue(ndev);
  2245. napi_disable(&fep->napi);
  2246. netif_tx_lock_bh(ndev);
  2247. fec_stop(ndev);
  2248. netif_tx_unlock_bh(ndev);
  2249. napi_enable(&fep->napi);
  2250. fep->link = phy_dev->link;
  2251. status_change = 1;
  2252. }
  2253. }
  2254. if (status_change)
  2255. phy_print_status(phy_dev);
  2256. }
  2257. static int fec_enet_mdio_wait(struct fec_enet_private *fep)
  2258. {
  2259. uint ievent;
  2260. int ret;
  2261. ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent,
  2262. ievent & FEC_ENET_MII, 2, 30000);
  2263. if (!ret)
  2264. writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
  2265. return ret;
  2266. }
  2267. static int fec_enet_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum)
  2268. {
  2269. struct fec_enet_private *fep = bus->priv;
  2270. struct device *dev = &fep->pdev->dev;
  2271. int ret = 0, frame_start, frame_addr, frame_op;
  2272. ret = pm_runtime_resume_and_get(dev);
  2273. if (ret < 0)
  2274. return ret;
  2275. /* C22 read */
  2276. frame_op = FEC_MMFR_OP_READ;
  2277. frame_start = FEC_MMFR_ST;
  2278. frame_addr = regnum;
  2279. /* start a read op */
  2280. writel(frame_start | frame_op |
  2281. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
  2282. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  2283. /* wait for end of transfer */
  2284. ret = fec_enet_mdio_wait(fep);
  2285. if (ret) {
  2286. netdev_err(fep->netdev, "MDIO read timeout\n");
  2287. goto out;
  2288. }
  2289. ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  2290. out:
  2291. pm_runtime_put_autosuspend(dev);
  2292. return ret;
  2293. }
  2294. static int fec_enet_mdio_read_c45(struct mii_bus *bus, int mii_id,
  2295. int devad, int regnum)
  2296. {
  2297. struct fec_enet_private *fep = bus->priv;
  2298. struct device *dev = &fep->pdev->dev;
  2299. int ret = 0, frame_start, frame_op;
  2300. ret = pm_runtime_resume_and_get(dev);
  2301. if (ret < 0)
  2302. return ret;
  2303. frame_start = FEC_MMFR_ST_C45;
  2304. /* write address */
  2305. writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
  2306. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
  2307. FEC_MMFR_TA | (regnum & 0xFFFF),
  2308. fep->hwp + FEC_MII_DATA);
  2309. /* wait for end of transfer */
  2310. ret = fec_enet_mdio_wait(fep);
  2311. if (ret) {
  2312. netdev_err(fep->netdev, "MDIO address write timeout\n");
  2313. goto out;
  2314. }
  2315. frame_op = FEC_MMFR_OP_READ_C45;
  2316. /* start a read op */
  2317. writel(frame_start | frame_op |
  2318. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
  2319. FEC_MMFR_TA, fep->hwp + FEC_MII_DATA);
  2320. /* wait for end of transfer */
  2321. ret = fec_enet_mdio_wait(fep);
  2322. if (ret) {
  2323. netdev_err(fep->netdev, "MDIO read timeout\n");
  2324. goto out;
  2325. }
  2326. ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA));
  2327. out:
  2328. pm_runtime_put_autosuspend(dev);
  2329. return ret;
  2330. }
  2331. static int fec_enet_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum,
  2332. u16 value)
  2333. {
  2334. struct fec_enet_private *fep = bus->priv;
  2335. struct device *dev = &fep->pdev->dev;
  2336. int ret, frame_start, frame_addr;
  2337. ret = pm_runtime_resume_and_get(dev);
  2338. if (ret < 0)
  2339. return ret;
  2340. /* C22 write */
  2341. frame_start = FEC_MMFR_ST;
  2342. frame_addr = regnum;
  2343. /* start a write op */
  2344. writel(frame_start | FEC_MMFR_OP_WRITE |
  2345. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) |
  2346. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  2347. fep->hwp + FEC_MII_DATA);
  2348. /* wait for end of transfer */
  2349. ret = fec_enet_mdio_wait(fep);
  2350. if (ret)
  2351. netdev_err(fep->netdev, "MDIO write timeout\n");
  2352. pm_runtime_put_autosuspend(dev);
  2353. return ret;
  2354. }
  2355. static int fec_enet_mdio_write_c45(struct mii_bus *bus, int mii_id,
  2356. int devad, int regnum, u16 value)
  2357. {
  2358. struct fec_enet_private *fep = bus->priv;
  2359. struct device *dev = &fep->pdev->dev;
  2360. int ret, frame_start;
  2361. ret = pm_runtime_resume_and_get(dev);
  2362. if (ret < 0)
  2363. return ret;
  2364. frame_start = FEC_MMFR_ST_C45;
  2365. /* write address */
  2366. writel(frame_start | FEC_MMFR_OP_ADDR_WRITE |
  2367. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
  2368. FEC_MMFR_TA | (regnum & 0xFFFF),
  2369. fep->hwp + FEC_MII_DATA);
  2370. /* wait for end of transfer */
  2371. ret = fec_enet_mdio_wait(fep);
  2372. if (ret) {
  2373. netdev_err(fep->netdev, "MDIO address write timeout\n");
  2374. goto out;
  2375. }
  2376. /* start a write op */
  2377. writel(frame_start | FEC_MMFR_OP_WRITE |
  2378. FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) |
  2379. FEC_MMFR_TA | FEC_MMFR_DATA(value),
  2380. fep->hwp + FEC_MII_DATA);
  2381. /* wait for end of transfer */
  2382. ret = fec_enet_mdio_wait(fep);
  2383. if (ret)
  2384. netdev_err(fep->netdev, "MDIO write timeout\n");
  2385. out:
  2386. pm_runtime_put_autosuspend(dev);
  2387. return ret;
  2388. }
  2389. static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev)
  2390. {
  2391. struct fec_enet_private *fep = netdev_priv(ndev);
  2392. struct phy_device *phy_dev = ndev->phydev;
  2393. if (phy_dev) {
  2394. phy_reset_after_clk_enable(phy_dev);
  2395. } else if (fep->phy_node) {
  2396. /*
  2397. * If the PHY still is not bound to the MAC, but there is
  2398. * OF PHY node and a matching PHY device instance already,
  2399. * use the OF PHY node to obtain the PHY device instance,
  2400. * and then use that PHY device instance when triggering
  2401. * the PHY reset.
  2402. */
  2403. phy_dev = of_phy_find_device(fep->phy_node);
  2404. phy_reset_after_clk_enable(phy_dev);
  2405. if (phy_dev)
  2406. put_device(&phy_dev->mdio.dev);
  2407. }
  2408. }
  2409. static int fec_enet_clk_enable(struct net_device *ndev, bool enable)
  2410. {
  2411. struct fec_enet_private *fep = netdev_priv(ndev);
  2412. int ret;
  2413. if (enable) {
  2414. ret = clk_prepare_enable(fep->clk_enet_out);
  2415. if (ret)
  2416. return ret;
  2417. if (fep->clk_ptp) {
  2418. mutex_lock(&fep->ptp_clk_mutex);
  2419. ret = clk_prepare_enable(fep->clk_ptp);
  2420. if (ret) {
  2421. mutex_unlock(&fep->ptp_clk_mutex);
  2422. goto failed_clk_ptp;
  2423. } else {
  2424. fep->ptp_clk_on = true;
  2425. }
  2426. mutex_unlock(&fep->ptp_clk_mutex);
  2427. }
  2428. ret = clk_prepare_enable(fep->clk_ref);
  2429. if (ret)
  2430. goto failed_clk_ref;
  2431. ret = clk_prepare_enable(fep->clk_2x_txclk);
  2432. if (ret)
  2433. goto failed_clk_2x_txclk;
  2434. fec_enet_phy_reset_after_clk_enable(ndev);
  2435. } else {
  2436. clk_disable_unprepare(fep->clk_enet_out);
  2437. if (fep->clk_ptp) {
  2438. mutex_lock(&fep->ptp_clk_mutex);
  2439. clk_disable_unprepare(fep->clk_ptp);
  2440. fep->ptp_clk_on = false;
  2441. mutex_unlock(&fep->ptp_clk_mutex);
  2442. }
  2443. clk_disable_unprepare(fep->clk_ref);
  2444. clk_disable_unprepare(fep->clk_2x_txclk);
  2445. }
  2446. return 0;
  2447. failed_clk_2x_txclk:
  2448. if (fep->clk_ref)
  2449. clk_disable_unprepare(fep->clk_ref);
  2450. failed_clk_ref:
  2451. if (fep->clk_ptp) {
  2452. mutex_lock(&fep->ptp_clk_mutex);
  2453. clk_disable_unprepare(fep->clk_ptp);
  2454. fep->ptp_clk_on = false;
  2455. mutex_unlock(&fep->ptp_clk_mutex);
  2456. }
  2457. failed_clk_ptp:
  2458. clk_disable_unprepare(fep->clk_enet_out);
  2459. return ret;
  2460. }
  2461. static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep,
  2462. struct device_node *np)
  2463. {
  2464. u32 rgmii_tx_delay, rgmii_rx_delay;
  2465. /* For rgmii tx internal delay, valid values are 0ps and 2000ps */
  2466. if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) {
  2467. if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) {
  2468. dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps");
  2469. return -EINVAL;
  2470. } else if (rgmii_tx_delay == 2000) {
  2471. fep->rgmii_txc_dly = true;
  2472. }
  2473. }
  2474. /* For rgmii rx internal delay, valid values are 0ps and 2000ps */
  2475. if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) {
  2476. if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) {
  2477. dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps");
  2478. return -EINVAL;
  2479. } else if (rgmii_rx_delay == 2000) {
  2480. fep->rgmii_rxc_dly = true;
  2481. }
  2482. }
  2483. return 0;
  2484. }
  2485. static int fec_enet_mii_probe(struct net_device *ndev)
  2486. {
  2487. struct fec_enet_private *fep = netdev_priv(ndev);
  2488. struct phy_device *phy_dev;
  2489. int ret;
  2490. if (fep->phy_node) {
  2491. phy_dev = of_phy_connect(ndev, fep->phy_node,
  2492. &fec_enet_adjust_link, 0,
  2493. fep->phy_interface);
  2494. if (!phy_dev) {
  2495. netdev_err(ndev, "Unable to connect to phy\n");
  2496. return -ENODEV;
  2497. }
  2498. } else {
  2499. /* check for attached phy */
  2500. phy_dev = phy_find_first(fep->mii_bus);
  2501. if (fep->dev_id && phy_dev)
  2502. phy_dev = phy_find_next(fep->mii_bus, phy_dev);
  2503. if (!phy_dev) {
  2504. netdev_info(ndev, "no PHY, assuming direct connection to switch\n");
  2505. phy_dev = fixed_phy_register_100fd();
  2506. if (IS_ERR(phy_dev)) {
  2507. netdev_err(ndev, "could not register fixed PHY\n");
  2508. return PTR_ERR(phy_dev);
  2509. }
  2510. }
  2511. ret = phy_connect_direct(ndev, phy_dev, &fec_enet_adjust_link,
  2512. fep->phy_interface);
  2513. if (ret) {
  2514. if (phy_is_pseudo_fixed_link(phy_dev))
  2515. fixed_phy_unregister(phy_dev);
  2516. netdev_err(ndev, "could not attach to PHY\n");
  2517. return ret;
  2518. }
  2519. }
  2520. /* mask with MAC supported features */
  2521. if (fep->quirks & FEC_QUIRK_HAS_GBIT) {
  2522. phy_set_max_speed(phy_dev, 1000);
  2523. phy_remove_link_mode(phy_dev,
  2524. ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
  2525. phy_support_sym_pause(phy_dev);
  2526. }
  2527. else
  2528. phy_set_max_speed(phy_dev, 100);
  2529. if (fep->quirks & FEC_QUIRK_HAS_EEE)
  2530. phy_support_eee(phy_dev);
  2531. fep->link = 0;
  2532. fep->full_duplex = 0;
  2533. phy_attached_info(phy_dev);
  2534. return 0;
  2535. }
  2536. static int fec_enet_mii_init(struct platform_device *pdev)
  2537. {
  2538. static struct mii_bus *fec0_mii_bus;
  2539. struct net_device *ndev = platform_get_drvdata(pdev);
  2540. struct fec_enet_private *fep = netdev_priv(ndev);
  2541. bool suppress_preamble = false;
  2542. struct phy_device *phydev;
  2543. struct device_node *node;
  2544. int err = -ENXIO;
  2545. u32 mii_speed, holdtime;
  2546. u32 bus_freq;
  2547. /*
  2548. * The i.MX28 dual fec interfaces are not equal.
  2549. * Here are the differences:
  2550. *
  2551. * - fec0 supports MII & RMII modes while fec1 only supports RMII
  2552. * - fec0 acts as the 1588 time master while fec1 is slave
  2553. * - external phys can only be configured by fec0
  2554. *
  2555. * That is to say fec1 can not work independently. It only works
  2556. * when fec0 is working. The reason behind this design is that the
  2557. * second interface is added primarily for Switch mode.
  2558. *
  2559. * Because of the last point above, both phys are attached on fec0
  2560. * mdio interface in board design, and need to be configured by
  2561. * fec0 mii_bus.
  2562. */
  2563. if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) {
  2564. /* fec1 uses fec0 mii_bus */
  2565. if (mii_cnt && fec0_mii_bus) {
  2566. fep->mii_bus = fec0_mii_bus;
  2567. mii_cnt++;
  2568. return 0;
  2569. }
  2570. return -ENOENT;
  2571. }
  2572. bus_freq = 2500000; /* 2.5MHz by default */
  2573. node = of_get_child_by_name(pdev->dev.of_node, "mdio");
  2574. if (node) {
  2575. of_property_read_u32(node, "clock-frequency", &bus_freq);
  2576. suppress_preamble = of_property_read_bool(node,
  2577. "suppress-preamble");
  2578. }
  2579. /*
  2580. * Set MII speed (= clk_get_rate() / 2 * phy_speed)
  2581. *
  2582. * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while
  2583. * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28
  2584. * Reference Manual has an error on this, and gets fixed on i.MX6Q
  2585. * document.
  2586. */
  2587. mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2);
  2588. if (fep->quirks & FEC_QUIRK_ENET_MAC)
  2589. mii_speed--;
  2590. if (mii_speed > 63) {
  2591. dev_err(&pdev->dev,
  2592. "fec clock (%lu) too fast to get right mii speed\n",
  2593. clk_get_rate(fep->clk_ipg));
  2594. err = -EINVAL;
  2595. goto err_out;
  2596. }
  2597. /*
  2598. * The i.MX28 and i.MX6 types have another filed in the MSCR (aka
  2599. * MII_SPEED) register that defines the MDIO output hold time. Earlier
  2600. * versions are RAZ there, so just ignore the difference and write the
  2601. * register always.
  2602. * The minimal hold time according to IEE802.3 (clause 22) is 10 ns.
  2603. * HOLDTIME + 1 is the number of clk cycles the fec is holding the
  2604. * output.
  2605. * The HOLDTIME bitfield takes values between 0 and 7 (inclusive).
  2606. * Given that ceil(clkrate / 5000000) <= 64, the calculation for
  2607. * holdtime cannot result in a value greater than 3.
  2608. */
  2609. holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1;
  2610. fep->phy_speed = mii_speed << 1 | holdtime << 8;
  2611. if (suppress_preamble)
  2612. fep->phy_speed |= BIT(7);
  2613. if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) {
  2614. /* Clear MMFR to avoid to generate MII event by writing MSCR.
  2615. * MII event generation condition:
  2616. * - writing MSCR:
  2617. * - mmfr[31:0]_not_zero & mscr[7:0]_is_zero &
  2618. * mscr_reg_data_in[7:0] != 0
  2619. * - writing MMFR:
  2620. * - mscr[7:0]_not_zero
  2621. */
  2622. writel(0, fep->hwp + FEC_MII_DATA);
  2623. }
  2624. writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
  2625. /* Clear any pending transaction complete indication */
  2626. writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT);
  2627. fep->mii_bus = mdiobus_alloc();
  2628. if (fep->mii_bus == NULL) {
  2629. err = -ENOMEM;
  2630. goto err_out;
  2631. }
  2632. fep->mii_bus->name = "fec_enet_mii_bus";
  2633. fep->mii_bus->read = fec_enet_mdio_read_c22;
  2634. fep->mii_bus->write = fec_enet_mdio_write_c22;
  2635. if (fep->quirks & FEC_QUIRK_HAS_MDIO_C45) {
  2636. fep->mii_bus->read_c45 = fec_enet_mdio_read_c45;
  2637. fep->mii_bus->write_c45 = fec_enet_mdio_write_c45;
  2638. }
  2639. snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  2640. pdev->name, fep->dev_id + 1);
  2641. fep->mii_bus->priv = fep;
  2642. fep->mii_bus->parent = &pdev->dev;
  2643. err = of_mdiobus_register(fep->mii_bus, node);
  2644. if (err)
  2645. goto err_out_free_mdiobus;
  2646. of_node_put(node);
  2647. /* find all the PHY devices on the bus and set mac_managed_pm to true */
  2648. mdiobus_for_each_phy(fep->mii_bus, phydev)
  2649. phydev->mac_managed_pm = true;
  2650. mii_cnt++;
  2651. /* save fec0 mii_bus */
  2652. if (fep->quirks & FEC_QUIRK_SINGLE_MDIO)
  2653. fec0_mii_bus = fep->mii_bus;
  2654. return 0;
  2655. err_out_free_mdiobus:
  2656. mdiobus_free(fep->mii_bus);
  2657. err_out:
  2658. of_node_put(node);
  2659. return err;
  2660. }
  2661. static void fec_enet_mii_remove(struct fec_enet_private *fep)
  2662. {
  2663. if (--mii_cnt == 0) {
  2664. mdiobus_unregister(fep->mii_bus);
  2665. mdiobus_free(fep->mii_bus);
  2666. }
  2667. }
  2668. static void fec_enet_get_drvinfo(struct net_device *ndev,
  2669. struct ethtool_drvinfo *info)
  2670. {
  2671. struct fec_enet_private *fep = netdev_priv(ndev);
  2672. strscpy(info->driver, fep->pdev->dev.driver->name,
  2673. sizeof(info->driver));
  2674. strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info));
  2675. }
  2676. static int fec_enet_get_regs_len(struct net_device *ndev)
  2677. {
  2678. struct fec_enet_private *fep = netdev_priv(ndev);
  2679. struct resource *r;
  2680. int s = 0;
  2681. r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0);
  2682. if (r)
  2683. s = resource_size(r);
  2684. return s;
  2685. }
  2686. /* List of registers that can be safety be read to dump them with ethtool */
  2687. #if !defined(CONFIG_M5272) || defined(CONFIG_COMPILE_TEST)
  2688. static __u32 fec_enet_register_version = 2;
  2689. static u32 fec_enet_register_offset[] = {
  2690. FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
  2691. FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
  2692. FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1,
  2693. FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH,
  2694. FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW,
  2695. FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1,
  2696. FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2,
  2697. FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0,
  2698. FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
  2699. FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2,
  2700. FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1,
  2701. FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME,
  2702. RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
  2703. RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
  2704. RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
  2705. RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
  2706. RMON_T_P_GTE2048, RMON_T_OCTETS,
  2707. IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
  2708. IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
  2709. IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
  2710. RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
  2711. RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
  2712. RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
  2713. RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
  2714. RMON_R_P_GTE2048, RMON_R_OCTETS,
  2715. IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
  2716. IEEE_R_FDXFC, IEEE_R_OCTETS_OK
  2717. };
  2718. /* for i.MX6ul */
  2719. static u32 fec_enet_register_offset_6ul[] = {
  2720. FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0,
  2721. FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL,
  2722. FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_RXIC0,
  2723. FEC_HASH_TABLE_HIGH, FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH,
  2724. FEC_GRP_HASH_TABLE_LOW, FEC_X_WMRK, FEC_R_DES_START_0,
  2725. FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM,
  2726. FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC,
  2727. RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT,
  2728. RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG,
  2729. RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255,
  2730. RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047,
  2731. RMON_T_P_GTE2048, RMON_T_OCTETS,
  2732. IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF,
  2733. IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE,
  2734. IEEE_T_FDXFC, IEEE_T_OCTETS_OK,
  2735. RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN,
  2736. RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB,
  2737. RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255,
  2738. RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047,
  2739. RMON_R_P_GTE2048, RMON_R_OCTETS,
  2740. IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR,
  2741. IEEE_R_FDXFC, IEEE_R_OCTETS_OK
  2742. };
  2743. #else
  2744. static __u32 fec_enet_register_version = 1;
  2745. static u32 fec_enet_register_offset[] = {
  2746. FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0,
  2747. FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0,
  2748. FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED,
  2749. FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL,
  2750. FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH,
  2751. FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0,
  2752. FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0,
  2753. FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0,
  2754. FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2
  2755. };
  2756. #endif
  2757. static void fec_enet_get_regs(struct net_device *ndev,
  2758. struct ethtool_regs *regs, void *regbuf)
  2759. {
  2760. u32 reg_cnt = ARRAY_SIZE(fec_enet_register_offset);
  2761. struct fec_enet_private *fep = netdev_priv(ndev);
  2762. u32 __iomem *theregs = (u32 __iomem *)fep->hwp;
  2763. u32 *reg_list = fec_enet_register_offset;
  2764. struct device *dev = &fep->pdev->dev;
  2765. u32 *buf = (u32 *)regbuf;
  2766. u32 i, off;
  2767. int ret;
  2768. #if !defined(CONFIG_M5272) || defined(CONFIG_COMPILE_TEST)
  2769. if (of_machine_is_compatible("fsl,imx6ul")) {
  2770. reg_list = fec_enet_register_offset_6ul;
  2771. reg_cnt = ARRAY_SIZE(fec_enet_register_offset_6ul);
  2772. }
  2773. #endif
  2774. ret = pm_runtime_resume_and_get(dev);
  2775. if (ret < 0)
  2776. return;
  2777. regs->version = fec_enet_register_version;
  2778. memset(buf, 0, regs->len);
  2779. for (i = 0; i < reg_cnt; i++) {
  2780. off = reg_list[i];
  2781. if ((off == FEC_R_BOUND || off == FEC_R_FSTART) &&
  2782. !(fep->quirks & FEC_QUIRK_HAS_FRREG))
  2783. continue;
  2784. off >>= 2;
  2785. buf[off] = readl(&theregs[off]);
  2786. }
  2787. pm_runtime_put_autosuspend(dev);
  2788. }
  2789. static int fec_enet_get_ts_info(struct net_device *ndev,
  2790. struct kernel_ethtool_ts_info *info)
  2791. {
  2792. struct fec_enet_private *fep = netdev_priv(ndev);
  2793. if (fep->bufdesc_ex) {
  2794. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  2795. SOF_TIMESTAMPING_TX_HARDWARE |
  2796. SOF_TIMESTAMPING_RX_HARDWARE |
  2797. SOF_TIMESTAMPING_RAW_HARDWARE;
  2798. if (fep->ptp_clock)
  2799. info->phc_index = ptp_clock_index(fep->ptp_clock);
  2800. info->tx_types = (1 << HWTSTAMP_TX_OFF) |
  2801. (1 << HWTSTAMP_TX_ON);
  2802. info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) |
  2803. (1 << HWTSTAMP_FILTER_ALL);
  2804. return 0;
  2805. } else {
  2806. return ethtool_op_get_ts_info(ndev, info);
  2807. }
  2808. }
  2809. #if !defined(CONFIG_M5272)
  2810. static void fec_enet_get_pauseparam(struct net_device *ndev,
  2811. struct ethtool_pauseparam *pause)
  2812. {
  2813. struct fec_enet_private *fep = netdev_priv(ndev);
  2814. pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0;
  2815. pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0;
  2816. pause->rx_pause = pause->tx_pause;
  2817. }
  2818. static int fec_enet_set_pauseparam(struct net_device *ndev,
  2819. struct ethtool_pauseparam *pause)
  2820. {
  2821. struct fec_enet_private *fep = netdev_priv(ndev);
  2822. if (!ndev->phydev)
  2823. return -ENODEV;
  2824. if (pause->tx_pause != pause->rx_pause) {
  2825. netdev_info(ndev,
  2826. "hardware only support enable/disable both tx and rx");
  2827. return -EINVAL;
  2828. }
  2829. fep->pause_flag = 0;
  2830. /* tx pause must be same as rx pause */
  2831. fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0;
  2832. fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0;
  2833. phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause,
  2834. pause->autoneg);
  2835. if (pause->autoneg) {
  2836. if (netif_running(ndev))
  2837. fec_stop(ndev);
  2838. phy_start_aneg(ndev->phydev);
  2839. }
  2840. if (netif_running(ndev)) {
  2841. napi_disable(&fep->napi);
  2842. netif_tx_lock_bh(ndev);
  2843. fec_restart(ndev);
  2844. netif_tx_wake_all_queues(ndev);
  2845. netif_tx_unlock_bh(ndev);
  2846. napi_enable(&fep->napi);
  2847. }
  2848. return 0;
  2849. }
  2850. static const struct fec_stat {
  2851. char name[ETH_GSTRING_LEN];
  2852. u16 offset;
  2853. } fec_stats[] = {
  2854. /* RMON TX */
  2855. { "tx_dropped", RMON_T_DROP },
  2856. { "tx_packets", RMON_T_PACKETS },
  2857. { "tx_broadcast", RMON_T_BC_PKT },
  2858. { "tx_multicast", RMON_T_MC_PKT },
  2859. { "tx_crc_errors", RMON_T_CRC_ALIGN },
  2860. { "tx_undersize", RMON_T_UNDERSIZE },
  2861. { "tx_oversize", RMON_T_OVERSIZE },
  2862. { "tx_fragment", RMON_T_FRAG },
  2863. { "tx_jabber", RMON_T_JAB },
  2864. { "tx_collision", RMON_T_COL },
  2865. { "tx_64byte", RMON_T_P64 },
  2866. { "tx_65to127byte", RMON_T_P65TO127 },
  2867. { "tx_128to255byte", RMON_T_P128TO255 },
  2868. { "tx_256to511byte", RMON_T_P256TO511 },
  2869. { "tx_512to1023byte", RMON_T_P512TO1023 },
  2870. { "tx_1024to2047byte", RMON_T_P1024TO2047 },
  2871. { "tx_GTE2048byte", RMON_T_P_GTE2048 },
  2872. { "tx_octets", RMON_T_OCTETS },
  2873. /* IEEE TX */
  2874. { "IEEE_tx_drop", IEEE_T_DROP },
  2875. { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK },
  2876. { "IEEE_tx_1col", IEEE_T_1COL },
  2877. { "IEEE_tx_mcol", IEEE_T_MCOL },
  2878. { "IEEE_tx_def", IEEE_T_DEF },
  2879. { "IEEE_tx_lcol", IEEE_T_LCOL },
  2880. { "IEEE_tx_excol", IEEE_T_EXCOL },
  2881. { "IEEE_tx_macerr", IEEE_T_MACERR },
  2882. { "IEEE_tx_cserr", IEEE_T_CSERR },
  2883. { "IEEE_tx_sqe", IEEE_T_SQE },
  2884. { "IEEE_tx_fdxfc", IEEE_T_FDXFC },
  2885. { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK },
  2886. /* RMON RX */
  2887. { "rx_packets", RMON_R_PACKETS },
  2888. { "rx_broadcast", RMON_R_BC_PKT },
  2889. { "rx_multicast", RMON_R_MC_PKT },
  2890. { "rx_crc_errors", RMON_R_CRC_ALIGN },
  2891. { "rx_undersize", RMON_R_UNDERSIZE },
  2892. { "rx_oversize", RMON_R_OVERSIZE },
  2893. { "rx_fragment", RMON_R_FRAG },
  2894. { "rx_jabber", RMON_R_JAB },
  2895. { "rx_64byte", RMON_R_P64 },
  2896. { "rx_65to127byte", RMON_R_P65TO127 },
  2897. { "rx_128to255byte", RMON_R_P128TO255 },
  2898. { "rx_256to511byte", RMON_R_P256TO511 },
  2899. { "rx_512to1023byte", RMON_R_P512TO1023 },
  2900. { "rx_1024to2047byte", RMON_R_P1024TO2047 },
  2901. { "rx_GTE2048byte", RMON_R_P_GTE2048 },
  2902. { "rx_octets", RMON_R_OCTETS },
  2903. /* IEEE RX */
  2904. { "IEEE_rx_drop", IEEE_R_DROP },
  2905. { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK },
  2906. { "IEEE_rx_crc", IEEE_R_CRC },
  2907. { "IEEE_rx_align", IEEE_R_ALIGN },
  2908. { "IEEE_rx_macerr", IEEE_R_MACERR },
  2909. { "IEEE_rx_fdxfc", IEEE_R_FDXFC },
  2910. { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK },
  2911. };
  2912. #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64))
  2913. static const char *fec_xdp_stat_strs[XDP_STATS_TOTAL] = {
  2914. "rx_xdp_redirect", /* RX_XDP_REDIRECT = 0, */
  2915. "rx_xdp_pass", /* RX_XDP_PASS, */
  2916. "rx_xdp_drop", /* RX_XDP_DROP, */
  2917. "rx_xdp_tx", /* RX_XDP_TX, */
  2918. "rx_xdp_tx_errors", /* RX_XDP_TX_ERRORS, */
  2919. "tx_xdp_xmit", /* TX_XDP_XMIT, */
  2920. "tx_xdp_xmit_errors", /* TX_XDP_XMIT_ERRORS, */
  2921. };
  2922. static void fec_enet_update_ethtool_stats(struct net_device *dev)
  2923. {
  2924. struct fec_enet_private *fep = netdev_priv(dev);
  2925. int i;
  2926. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  2927. fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset);
  2928. }
  2929. static void fec_enet_get_xdp_stats(struct fec_enet_private *fep, u64 *data)
  2930. {
  2931. u64 xdp_stats[XDP_STATS_TOTAL] = { 0 };
  2932. struct fec_enet_priv_rx_q *rxq;
  2933. int i, j;
  2934. for (i = fep->num_rx_queues - 1; i >= 0; i--) {
  2935. rxq = fep->rx_queue[i];
  2936. for (j = 0; j < XDP_STATS_TOTAL; j++)
  2937. xdp_stats[j] += rxq->stats[j];
  2938. }
  2939. memcpy(data, xdp_stats, sizeof(xdp_stats));
  2940. }
  2941. static void fec_enet_page_pool_stats(struct fec_enet_private *fep, u64 *data)
  2942. {
  2943. #ifdef CONFIG_PAGE_POOL_STATS
  2944. struct page_pool_stats stats = {};
  2945. struct fec_enet_priv_rx_q *rxq;
  2946. int i;
  2947. for (i = fep->num_rx_queues - 1; i >= 0; i--) {
  2948. rxq = fep->rx_queue[i];
  2949. if (!rxq->page_pool)
  2950. continue;
  2951. page_pool_get_stats(rxq->page_pool, &stats);
  2952. }
  2953. page_pool_ethtool_stats_get(data, &stats);
  2954. #endif
  2955. }
  2956. static void fec_enet_get_ethtool_stats(struct net_device *dev,
  2957. struct ethtool_stats *stats, u64 *data)
  2958. {
  2959. struct fec_enet_private *fep = netdev_priv(dev);
  2960. if (netif_running(dev))
  2961. fec_enet_update_ethtool_stats(dev);
  2962. memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE);
  2963. data += FEC_STATS_SIZE / sizeof(u64);
  2964. fec_enet_get_xdp_stats(fep, data);
  2965. data += XDP_STATS_TOTAL;
  2966. fec_enet_page_pool_stats(fep, data);
  2967. }
  2968. static void fec_enet_get_strings(struct net_device *netdev,
  2969. u32 stringset, u8 *data)
  2970. {
  2971. int i;
  2972. switch (stringset) {
  2973. case ETH_SS_STATS:
  2974. for (i = 0; i < ARRAY_SIZE(fec_stats); i++) {
  2975. ethtool_puts(&data, fec_stats[i].name);
  2976. }
  2977. for (i = 0; i < ARRAY_SIZE(fec_xdp_stat_strs); i++) {
  2978. ethtool_puts(&data, fec_xdp_stat_strs[i]);
  2979. }
  2980. page_pool_ethtool_stats_get_strings(data);
  2981. break;
  2982. case ETH_SS_TEST:
  2983. net_selftest_get_strings(data);
  2984. break;
  2985. }
  2986. }
  2987. static int fec_enet_get_sset_count(struct net_device *dev, int sset)
  2988. {
  2989. int count;
  2990. switch (sset) {
  2991. case ETH_SS_STATS:
  2992. count = ARRAY_SIZE(fec_stats) + XDP_STATS_TOTAL;
  2993. count += page_pool_ethtool_stats_get_count();
  2994. return count;
  2995. case ETH_SS_TEST:
  2996. return net_selftest_get_count();
  2997. default:
  2998. return -EOPNOTSUPP;
  2999. }
  3000. }
  3001. static void fec_enet_clear_ethtool_stats(struct net_device *dev)
  3002. {
  3003. struct fec_enet_private *fep = netdev_priv(dev);
  3004. struct fec_enet_priv_rx_q *rxq;
  3005. int i, j;
  3006. /* Disable MIB statistics counters */
  3007. writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT);
  3008. for (i = 0; i < ARRAY_SIZE(fec_stats); i++)
  3009. writel(0, fep->hwp + fec_stats[i].offset);
  3010. for (i = fep->num_rx_queues - 1; i >= 0; i--) {
  3011. rxq = fep->rx_queue[i];
  3012. for (j = 0; j < XDP_STATS_TOTAL; j++)
  3013. rxq->stats[j] = 0;
  3014. }
  3015. /* Don't disable MIB statistics counters */
  3016. writel(0, fep->hwp + FEC_MIB_CTRLSTAT);
  3017. }
  3018. #else /* !defined(CONFIG_M5272) */
  3019. #define FEC_STATS_SIZE 0
  3020. static inline void fec_enet_update_ethtool_stats(struct net_device *dev)
  3021. {
  3022. }
  3023. static inline void fec_enet_clear_ethtool_stats(struct net_device *dev)
  3024. {
  3025. }
  3026. #endif /* !defined(CONFIG_M5272) */
  3027. /* ITR clock source is enet system clock (clk_ahb).
  3028. * TCTT unit is cycle_ns * 64 cycle
  3029. * So, the ICTT value = X us / (cycle_ns * 64)
  3030. */
  3031. static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us)
  3032. {
  3033. struct fec_enet_private *fep = netdev_priv(ndev);
  3034. return us * (fep->itr_clk_rate / 64000) / 1000;
  3035. }
  3036. /* Set threshold for interrupt coalescing */
  3037. static void fec_enet_itr_coal_set(struct net_device *ndev)
  3038. {
  3039. struct fec_enet_private *fep = netdev_priv(ndev);
  3040. u32 rx_itr = 0, tx_itr = 0;
  3041. int rx_ictt, tx_ictt;
  3042. rx_ictt = fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr);
  3043. tx_ictt = fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr);
  3044. if (rx_ictt > 0 && fep->rx_pkts_itr > 1) {
  3045. /* Enable with enet system clock as Interrupt Coalescing timer Clock Source */
  3046. rx_itr = FEC_ITR_EN | FEC_ITR_CLK_SEL;
  3047. rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr);
  3048. rx_itr |= FEC_ITR_ICTT(rx_ictt);
  3049. }
  3050. if (tx_ictt > 0 && fep->tx_pkts_itr > 1) {
  3051. /* Enable with enet system clock as Interrupt Coalescing timer Clock Source */
  3052. tx_itr = FEC_ITR_EN | FEC_ITR_CLK_SEL;
  3053. tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr);
  3054. tx_itr |= FEC_ITR_ICTT(tx_ictt);
  3055. }
  3056. writel(tx_itr, fep->hwp + FEC_TXIC0);
  3057. writel(rx_itr, fep->hwp + FEC_RXIC0);
  3058. if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) {
  3059. writel(tx_itr, fep->hwp + FEC_TXIC1);
  3060. writel(rx_itr, fep->hwp + FEC_RXIC1);
  3061. writel(tx_itr, fep->hwp + FEC_TXIC2);
  3062. writel(rx_itr, fep->hwp + FEC_RXIC2);
  3063. }
  3064. }
  3065. static int fec_enet_get_coalesce(struct net_device *ndev,
  3066. struct ethtool_coalesce *ec,
  3067. struct kernel_ethtool_coalesce *kernel_coal,
  3068. struct netlink_ext_ack *extack)
  3069. {
  3070. struct fec_enet_private *fep = netdev_priv(ndev);
  3071. if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
  3072. return -EOPNOTSUPP;
  3073. ec->rx_coalesce_usecs = fep->rx_time_itr;
  3074. ec->rx_max_coalesced_frames = fep->rx_pkts_itr;
  3075. ec->tx_coalesce_usecs = fep->tx_time_itr;
  3076. ec->tx_max_coalesced_frames = fep->tx_pkts_itr;
  3077. return 0;
  3078. }
  3079. static int fec_enet_set_coalesce(struct net_device *ndev,
  3080. struct ethtool_coalesce *ec,
  3081. struct kernel_ethtool_coalesce *kernel_coal,
  3082. struct netlink_ext_ack *extack)
  3083. {
  3084. struct fec_enet_private *fep = netdev_priv(ndev);
  3085. struct device *dev = &fep->pdev->dev;
  3086. unsigned int cycle;
  3087. if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE))
  3088. return -EOPNOTSUPP;
  3089. if (ec->rx_max_coalesced_frames > 255) {
  3090. dev_err(dev, "Rx coalesced frames exceed hardware limitation\n");
  3091. return -EINVAL;
  3092. }
  3093. if (ec->tx_max_coalesced_frames > 255) {
  3094. dev_err(dev, "Tx coalesced frame exceed hardware limitation\n");
  3095. return -EINVAL;
  3096. }
  3097. cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs);
  3098. if (cycle > 0xFFFF) {
  3099. dev_err(dev, "Rx coalesced usec exceed hardware limitation\n");
  3100. return -EINVAL;
  3101. }
  3102. cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs);
  3103. if (cycle > 0xFFFF) {
  3104. dev_err(dev, "Tx coalesced usec exceed hardware limitation\n");
  3105. return -EINVAL;
  3106. }
  3107. fep->rx_time_itr = ec->rx_coalesce_usecs;
  3108. fep->rx_pkts_itr = ec->rx_max_coalesced_frames;
  3109. fep->tx_time_itr = ec->tx_coalesce_usecs;
  3110. fep->tx_pkts_itr = ec->tx_max_coalesced_frames;
  3111. fec_enet_itr_coal_set(ndev);
  3112. return 0;
  3113. }
  3114. static int
  3115. fec_enet_get_eee(struct net_device *ndev, struct ethtool_keee *edata)
  3116. {
  3117. struct fec_enet_private *fep = netdev_priv(ndev);
  3118. if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
  3119. return -EOPNOTSUPP;
  3120. if (!netif_running(ndev))
  3121. return -ENETDOWN;
  3122. return phy_ethtool_get_eee(ndev->phydev, edata);
  3123. }
  3124. static int
  3125. fec_enet_set_eee(struct net_device *ndev, struct ethtool_keee *edata)
  3126. {
  3127. struct fec_enet_private *fep = netdev_priv(ndev);
  3128. if (!(fep->quirks & FEC_QUIRK_HAS_EEE))
  3129. return -EOPNOTSUPP;
  3130. if (!netif_running(ndev))
  3131. return -ENETDOWN;
  3132. return phy_ethtool_set_eee(ndev->phydev, edata);
  3133. }
  3134. static void
  3135. fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  3136. {
  3137. struct fec_enet_private *fep = netdev_priv(ndev);
  3138. if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) {
  3139. wol->supported = WAKE_MAGIC;
  3140. wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0;
  3141. } else {
  3142. wol->supported = wol->wolopts = 0;
  3143. }
  3144. }
  3145. static int
  3146. fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
  3147. {
  3148. struct fec_enet_private *fep = netdev_priv(ndev);
  3149. if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET))
  3150. return -EINVAL;
  3151. if (wol->wolopts & ~WAKE_MAGIC)
  3152. return -EINVAL;
  3153. device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC);
  3154. if (device_may_wakeup(&ndev->dev))
  3155. fep->wol_flag |= FEC_WOL_FLAG_ENABLE;
  3156. else
  3157. fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE);
  3158. return 0;
  3159. }
  3160. static const struct ethtool_ops fec_enet_ethtool_ops = {
  3161. .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
  3162. ETHTOOL_COALESCE_MAX_FRAMES,
  3163. .get_drvinfo = fec_enet_get_drvinfo,
  3164. .get_regs_len = fec_enet_get_regs_len,
  3165. .get_regs = fec_enet_get_regs,
  3166. .nway_reset = phy_ethtool_nway_reset,
  3167. .get_link = ethtool_op_get_link,
  3168. .get_coalesce = fec_enet_get_coalesce,
  3169. .set_coalesce = fec_enet_set_coalesce,
  3170. #ifndef CONFIG_M5272
  3171. .get_pauseparam = fec_enet_get_pauseparam,
  3172. .set_pauseparam = fec_enet_set_pauseparam,
  3173. .get_strings = fec_enet_get_strings,
  3174. .get_ethtool_stats = fec_enet_get_ethtool_stats,
  3175. .get_sset_count = fec_enet_get_sset_count,
  3176. #endif
  3177. .get_ts_info = fec_enet_get_ts_info,
  3178. .get_wol = fec_enet_get_wol,
  3179. .set_wol = fec_enet_set_wol,
  3180. .get_eee = fec_enet_get_eee,
  3181. .set_eee = fec_enet_set_eee,
  3182. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  3183. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  3184. .self_test = net_selftest,
  3185. };
  3186. static int fec_xdp_rxq_info_reg(struct fec_enet_private *fep,
  3187. struct fec_enet_priv_rx_q *rxq)
  3188. {
  3189. struct net_device *ndev = fep->netdev;
  3190. void *allocator;
  3191. int type, err;
  3192. err = xdp_rxq_info_reg(&rxq->xdp_rxq, ndev, rxq->id, 0);
  3193. if (err) {
  3194. netdev_err(ndev, "Failed to register xdp rxq info\n");
  3195. return err;
  3196. }
  3197. allocator = rxq->xsk_pool ? NULL : rxq->page_pool;
  3198. type = rxq->xsk_pool ? MEM_TYPE_XSK_BUFF_POOL : MEM_TYPE_PAGE_POOL;
  3199. err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, type, allocator);
  3200. if (err) {
  3201. netdev_err(ndev, "Failed to register XDP mem model\n");
  3202. xdp_rxq_info_unreg(&rxq->xdp_rxq);
  3203. return err;
  3204. }
  3205. if (rxq->xsk_pool)
  3206. xsk_pool_set_rxq_info(rxq->xsk_pool, &rxq->xdp_rxq);
  3207. return 0;
  3208. }
  3209. static void fec_xdp_rxq_info_unreg(struct fec_enet_priv_rx_q *rxq)
  3210. {
  3211. if (xdp_rxq_info_is_reg(&rxq->xdp_rxq)) {
  3212. xdp_rxq_info_unreg_mem_model(&rxq->xdp_rxq);
  3213. xdp_rxq_info_unreg(&rxq->xdp_rxq);
  3214. }
  3215. }
  3216. static void fec_free_rxq_buffers(struct fec_enet_priv_rx_q *rxq)
  3217. {
  3218. bool xsk = !!rxq->xsk_pool;
  3219. int i;
  3220. for (i = 0; i < rxq->bd.ring_size; i++) {
  3221. union fec_rx_buffer *buf = &rxq->rx_buf[i];
  3222. if (!buf->buf_p)
  3223. continue;
  3224. if (xsk)
  3225. xsk_buff_free(buf->xdp);
  3226. else
  3227. page_pool_put_full_page(rxq->page_pool,
  3228. buf->page, false);
  3229. rxq->rx_buf[i].buf_p = NULL;
  3230. }
  3231. if (!xsk) {
  3232. page_pool_destroy(rxq->page_pool);
  3233. rxq->page_pool = NULL;
  3234. }
  3235. }
  3236. static void fec_enet_free_buffers(struct net_device *ndev)
  3237. {
  3238. struct fec_enet_private *fep = netdev_priv(ndev);
  3239. unsigned int i;
  3240. struct fec_enet_priv_tx_q *txq;
  3241. struct fec_enet_priv_rx_q *rxq;
  3242. struct page *page;
  3243. unsigned int q;
  3244. for (q = 0; q < fep->num_rx_queues; q++) {
  3245. rxq = fep->rx_queue[q];
  3246. fec_xdp_rxq_info_unreg(rxq);
  3247. fec_free_rxq_buffers(rxq);
  3248. for (i = 0; i < XDP_STATS_TOTAL; i++)
  3249. rxq->stats[i] = 0;
  3250. }
  3251. for (q = 0; q < fep->num_tx_queues; q++) {
  3252. txq = fep->tx_queue[q];
  3253. for (i = 0; i < txq->bd.ring_size; i++) {
  3254. kfree(txq->tx_bounce[i]);
  3255. txq->tx_bounce[i] = NULL;
  3256. switch (txq->tx_buf[i].type) {
  3257. case FEC_TXBUF_T_SKB:
  3258. dev_kfree_skb(txq->tx_buf[i].buf_p);
  3259. break;
  3260. case FEC_TXBUF_T_XDP_NDO:
  3261. xdp_return_frame(txq->tx_buf[i].buf_p);
  3262. break;
  3263. case FEC_TXBUF_T_XDP_TX:
  3264. page = txq->tx_buf[i].buf_p;
  3265. page_pool_put_page(pp_page_to_nmdesc(page)->pp,
  3266. page, 0, false);
  3267. break;
  3268. case FEC_TXBUF_T_XSK_TX:
  3269. xsk_buff_free(txq->tx_buf[i].buf_p);
  3270. break;
  3271. default:
  3272. break;
  3273. }
  3274. txq->tx_buf[i].buf_p = NULL;
  3275. txq->tx_buf[i].type = FEC_TXBUF_T_SKB;
  3276. }
  3277. }
  3278. }
  3279. static void fec_enet_free_queue(struct net_device *ndev)
  3280. {
  3281. struct fec_enet_private *fep = netdev_priv(ndev);
  3282. int i;
  3283. struct fec_enet_priv_tx_q *txq;
  3284. for (i = 0; i < fep->num_tx_queues; i++)
  3285. if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) {
  3286. txq = fep->tx_queue[i];
  3287. fec_dma_free(&fep->pdev->dev,
  3288. txq->bd.ring_size * TSO_HEADER_SIZE,
  3289. txq->tso_hdrs, txq->tso_hdrs_dma);
  3290. }
  3291. for (i = 0; i < fep->num_rx_queues; i++)
  3292. kfree(fep->rx_queue[i]);
  3293. for (i = 0; i < fep->num_tx_queues; i++)
  3294. kfree(fep->tx_queue[i]);
  3295. }
  3296. static int fec_enet_alloc_queue(struct net_device *ndev)
  3297. {
  3298. struct fec_enet_private *fep = netdev_priv(ndev);
  3299. int i;
  3300. int ret = 0;
  3301. struct fec_enet_priv_tx_q *txq;
  3302. for (i = 0; i < fep->num_tx_queues; i++) {
  3303. txq = kzalloc_obj(*txq);
  3304. if (!txq) {
  3305. ret = -ENOMEM;
  3306. goto alloc_failed;
  3307. }
  3308. fep->tx_queue[i] = txq;
  3309. txq->bd.ring_size = TX_RING_SIZE;
  3310. fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size;
  3311. txq->tx_stop_threshold = FEC_MAX_SKB_DESCS;
  3312. txq->tx_wake_threshold = FEC_MAX_SKB_DESCS + 2 * MAX_SKB_FRAGS;
  3313. txq->tso_hdrs = fec_dma_alloc(&fep->pdev->dev,
  3314. txq->bd.ring_size * TSO_HEADER_SIZE,
  3315. &txq->tso_hdrs_dma, GFP_KERNEL);
  3316. if (!txq->tso_hdrs) {
  3317. ret = -ENOMEM;
  3318. goto alloc_failed;
  3319. }
  3320. }
  3321. for (i = 0; i < fep->num_rx_queues; i++) {
  3322. fep->rx_queue[i] = kzalloc_obj(*fep->rx_queue[i]);
  3323. if (!fep->rx_queue[i]) {
  3324. ret = -ENOMEM;
  3325. goto alloc_failed;
  3326. }
  3327. fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE;
  3328. fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size;
  3329. }
  3330. return ret;
  3331. alloc_failed:
  3332. fec_enet_free_queue(ndev);
  3333. return ret;
  3334. }
  3335. static int fec_alloc_rxq_buffers_pp(struct fec_enet_private *fep,
  3336. struct fec_enet_priv_rx_q *rxq)
  3337. {
  3338. struct bufdesc *bdp = rxq->bd.base;
  3339. dma_addr_t phys_addr;
  3340. struct page *page;
  3341. int i, err;
  3342. err = fec_enet_create_page_pool(fep, rxq);
  3343. if (err < 0) {
  3344. netdev_err(fep->netdev, "%s failed queue %d (%d)\n",
  3345. __func__, rxq->bd.qid, err);
  3346. return err;
  3347. }
  3348. /* Some platforms require the RX buffer must be 64 bytes alignment.
  3349. * Some platforms require 16 bytes alignment. And some platforms
  3350. * require 4 bytes alignment. But since the page pool have been
  3351. * introduced into the driver, the address of RX buffer is always
  3352. * the page address plus FEC_ENET_XDP_HEADROOM, and
  3353. * FEC_ENET_XDP_HEADROOM is 256 bytes. Therefore, this address can
  3354. * satisfy all platforms. To prevent future modifications to
  3355. * FEC_ENET_XDP_HEADROOM from ignoring this hardware limitation, a
  3356. * BUILD_BUG_ON() test has been added, which ensures that
  3357. * FEC_ENET_XDP_HEADROOM provides the required alignment.
  3358. */
  3359. BUILD_BUG_ON(FEC_ENET_XDP_HEADROOM & 0x3f);
  3360. for (i = 0; i < rxq->bd.ring_size; i++) {
  3361. page = page_pool_dev_alloc_pages(rxq->page_pool);
  3362. if (!page) {
  3363. err = -ENOMEM;
  3364. goto free_rx_buffers;
  3365. }
  3366. phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM;
  3367. bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
  3368. rxq->rx_buf[i].page = page;
  3369. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  3370. }
  3371. return 0;
  3372. free_rx_buffers:
  3373. fec_free_rxq_buffers(rxq);
  3374. return err;
  3375. }
  3376. static int fec_alloc_rxq_buffers_zc(struct fec_enet_private *fep,
  3377. struct fec_enet_priv_rx_q *rxq)
  3378. {
  3379. union fec_rx_buffer *buf = &rxq->rx_buf[0];
  3380. struct bufdesc *bdp = rxq->bd.base;
  3381. dma_addr_t phys_addr;
  3382. int i;
  3383. for (i = 0; i < rxq->bd.ring_size; i++) {
  3384. buf[i].xdp = xsk_buff_alloc(rxq->xsk_pool);
  3385. if (!buf[i].xdp)
  3386. break;
  3387. phys_addr = xsk_buff_xdp_get_dma(buf[i].xdp);
  3388. bdp->cbd_bufaddr = cpu_to_fec32(phys_addr);
  3389. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  3390. }
  3391. for (; i < rxq->bd.ring_size; i++) {
  3392. buf[i].xdp = NULL;
  3393. bdp->cbd_bufaddr = cpu_to_fec32(0);
  3394. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  3395. }
  3396. return 0;
  3397. }
  3398. static int
  3399. fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue)
  3400. {
  3401. struct fec_enet_private *fep = netdev_priv(ndev);
  3402. struct fec_enet_priv_rx_q *rxq;
  3403. int err;
  3404. rxq = fep->rx_queue[queue];
  3405. if (rxq->xsk_pool) {
  3406. /* RX XDP ZC buffer pool may not be populated, e.g.
  3407. * xdpsock TX-only.
  3408. */
  3409. fec_alloc_rxq_buffers_zc(fep, rxq);
  3410. } else {
  3411. err = fec_alloc_rxq_buffers_pp(fep, rxq);
  3412. if (err)
  3413. goto free_buffers;
  3414. }
  3415. err = fec_xdp_rxq_info_reg(fep, rxq);
  3416. if (err)
  3417. goto free_buffers;
  3418. return 0;
  3419. free_buffers:
  3420. fec_enet_free_buffers(ndev);
  3421. return err;
  3422. }
  3423. static int
  3424. fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue)
  3425. {
  3426. struct fec_enet_private *fep = netdev_priv(ndev);
  3427. unsigned int i;
  3428. struct bufdesc *bdp;
  3429. struct fec_enet_priv_tx_q *txq;
  3430. txq = fep->tx_queue[queue];
  3431. bdp = txq->bd.base;
  3432. for (i = 0; i < txq->bd.ring_size; i++) {
  3433. txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL);
  3434. if (!txq->tx_bounce[i])
  3435. goto err_alloc;
  3436. bdp->cbd_sc = cpu_to_fec16(0);
  3437. bdp->cbd_bufaddr = cpu_to_fec32(0);
  3438. if (fep->bufdesc_ex) {
  3439. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  3440. ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT);
  3441. }
  3442. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  3443. }
  3444. /* Set the last buffer to wrap. */
  3445. bdp = fec_enet_get_prevdesc(bdp, &txq->bd);
  3446. bdp->cbd_sc |= cpu_to_fec16(BD_ENET_TX_WRAP);
  3447. return 0;
  3448. err_alloc:
  3449. fec_enet_free_buffers(ndev);
  3450. return -ENOMEM;
  3451. }
  3452. static int fec_enet_alloc_buffers(struct net_device *ndev)
  3453. {
  3454. struct fec_enet_private *fep = netdev_priv(ndev);
  3455. unsigned int i;
  3456. for (i = 0; i < fep->num_rx_queues; i++)
  3457. if (fec_enet_alloc_rxq_buffers(ndev, i))
  3458. return -ENOMEM;
  3459. for (i = 0; i < fep->num_tx_queues; i++)
  3460. if (fec_enet_alloc_txq_buffers(ndev, i))
  3461. return -ENOMEM;
  3462. return 0;
  3463. }
  3464. static int
  3465. fec_enet_open(struct net_device *ndev)
  3466. {
  3467. struct fec_enet_private *fep = netdev_priv(ndev);
  3468. int ret;
  3469. bool reset_again;
  3470. ret = pm_runtime_resume_and_get(&fep->pdev->dev);
  3471. if (ret < 0)
  3472. return ret;
  3473. pinctrl_pm_select_default_state(&fep->pdev->dev);
  3474. ret = fec_enet_clk_enable(ndev, true);
  3475. if (ret)
  3476. goto clk_enable;
  3477. /* During the first fec_enet_open call the PHY isn't probed at this
  3478. * point. Therefore the phy_reset_after_clk_enable() call within
  3479. * fec_enet_clk_enable() fails. As we need this reset in order to be
  3480. * sure the PHY is working correctly we check if we need to reset again
  3481. * later when the PHY is probed
  3482. */
  3483. if (ndev->phydev && ndev->phydev->drv)
  3484. reset_again = false;
  3485. else
  3486. reset_again = true;
  3487. /* I should reset the ring buffers here, but I don't yet know
  3488. * a simple way to do that.
  3489. */
  3490. ret = fec_enet_alloc_buffers(ndev);
  3491. if (ret)
  3492. goto err_enet_alloc;
  3493. /* Init MAC prior to mii bus probe */
  3494. fec_restart(ndev);
  3495. /* Call phy_reset_after_clk_enable() again if it failed during
  3496. * phy_reset_after_clk_enable() before because the PHY wasn't probed.
  3497. */
  3498. if (reset_again)
  3499. fec_enet_phy_reset_after_clk_enable(ndev);
  3500. /* Probe and connect to PHY when open the interface */
  3501. ret = fec_enet_mii_probe(ndev);
  3502. if (ret)
  3503. goto err_enet_mii_probe;
  3504. if (fep->quirks & FEC_QUIRK_ERR006687)
  3505. imx6q_cpuidle_fec_irqs_used();
  3506. if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
  3507. cpu_latency_qos_add_request(&fep->pm_qos_req, 0);
  3508. napi_enable(&fep->napi);
  3509. phy_start(ndev->phydev);
  3510. netif_tx_start_all_queues(ndev);
  3511. device_set_wakeup_enable(&ndev->dev, fep->wol_flag &
  3512. FEC_WOL_FLAG_ENABLE);
  3513. return 0;
  3514. err_enet_mii_probe:
  3515. fec_enet_free_buffers(ndev);
  3516. err_enet_alloc:
  3517. fec_enet_clk_enable(ndev, false);
  3518. clk_enable:
  3519. pm_runtime_put_autosuspend(&fep->pdev->dev);
  3520. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  3521. return ret;
  3522. }
  3523. static int
  3524. fec_enet_close(struct net_device *ndev)
  3525. {
  3526. struct fec_enet_private *fep = netdev_priv(ndev);
  3527. struct phy_device *phy_dev = ndev->phydev;
  3528. phy_stop(phy_dev);
  3529. if (netif_device_present(ndev)) {
  3530. napi_disable(&fep->napi);
  3531. netif_tx_disable(ndev);
  3532. fec_stop(ndev);
  3533. }
  3534. phy_disconnect(phy_dev);
  3535. if (!fep->phy_node && phy_is_pseudo_fixed_link(phy_dev))
  3536. fixed_phy_unregister(phy_dev);
  3537. if (fep->quirks & FEC_QUIRK_ERR006687)
  3538. imx6q_cpuidle_fec_irqs_unused();
  3539. fec_enet_update_ethtool_stats(ndev);
  3540. fec_enet_clk_enable(ndev, false);
  3541. if (fep->quirks & FEC_QUIRK_HAS_PMQOS)
  3542. cpu_latency_qos_remove_request(&fep->pm_qos_req);
  3543. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  3544. pm_runtime_put_autosuspend(&fep->pdev->dev);
  3545. fec_enet_free_buffers(ndev);
  3546. return 0;
  3547. }
  3548. /* Set or clear the multicast filter for this adaptor.
  3549. * Skeleton taken from sunlance driver.
  3550. * The CPM Ethernet implementation allows Multicast as well as individual
  3551. * MAC address filtering. Some of the drivers check to make sure it is
  3552. * a group multicast address, and discard those that are not. I guess I
  3553. * will do the same for now, but just remove the test if you want
  3554. * individual filtering as well (do the upper net layers want or support
  3555. * this kind of feature?).
  3556. */
  3557. #define FEC_HASH_BITS 6 /* #bits in hash */
  3558. static void set_multicast_list(struct net_device *ndev)
  3559. {
  3560. struct fec_enet_private *fep = netdev_priv(ndev);
  3561. struct netdev_hw_addr *ha;
  3562. unsigned int crc, tmp;
  3563. unsigned char hash;
  3564. unsigned int hash_high = 0, hash_low = 0;
  3565. if (ndev->flags & IFF_PROMISC) {
  3566. tmp = readl(fep->hwp + FEC_R_CNTRL);
  3567. tmp |= 0x8;
  3568. writel(tmp, fep->hwp + FEC_R_CNTRL);
  3569. return;
  3570. }
  3571. tmp = readl(fep->hwp + FEC_R_CNTRL);
  3572. tmp &= ~0x8;
  3573. writel(tmp, fep->hwp + FEC_R_CNTRL);
  3574. if (ndev->flags & IFF_ALLMULTI) {
  3575. /* Catch all multicast addresses, so set the
  3576. * filter to all 1's
  3577. */
  3578. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  3579. writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  3580. return;
  3581. }
  3582. /* Add the addresses in hash register */
  3583. netdev_for_each_mc_addr(ha, ndev) {
  3584. /* calculate crc32 value of mac address */
  3585. crc = ether_crc_le(ndev->addr_len, ha->addr);
  3586. /* only upper 6 bits (FEC_HASH_BITS) are used
  3587. * which point to specific bit in the hash registers
  3588. */
  3589. hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f;
  3590. if (hash > 31)
  3591. hash_high |= 1 << (hash - 32);
  3592. else
  3593. hash_low |= 1 << hash;
  3594. }
  3595. writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH);
  3596. writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW);
  3597. }
  3598. /* Set a MAC change in hardware. */
  3599. static int
  3600. fec_set_mac_address(struct net_device *ndev, void *p)
  3601. {
  3602. struct sockaddr *addr = p;
  3603. if (addr) {
  3604. if (!is_valid_ether_addr(addr->sa_data))
  3605. return -EADDRNOTAVAIL;
  3606. eth_hw_addr_set(ndev, addr->sa_data);
  3607. }
  3608. /* Add netif status check here to avoid system hang in below case:
  3609. * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx;
  3610. * After ethx down, fec all clocks are gated off and then register
  3611. * access causes system hang.
  3612. */
  3613. if (!netif_running(ndev))
  3614. return 0;
  3615. fec_set_hw_mac_addr(ndev);
  3616. return 0;
  3617. }
  3618. static inline void fec_enet_set_netdev_features(struct net_device *netdev,
  3619. netdev_features_t features)
  3620. {
  3621. struct fec_enet_private *fep = netdev_priv(netdev);
  3622. netdev_features_t changed = features ^ netdev->features;
  3623. netdev->features = features;
  3624. /* Receive checksum has been changed */
  3625. if (changed & NETIF_F_RXCSUM) {
  3626. if (features & NETIF_F_RXCSUM)
  3627. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  3628. else
  3629. fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED;
  3630. }
  3631. }
  3632. static int fec_set_features(struct net_device *netdev,
  3633. netdev_features_t features)
  3634. {
  3635. struct fec_enet_private *fep = netdev_priv(netdev);
  3636. netdev_features_t changed = features ^ netdev->features;
  3637. if (netif_running(netdev) && changed & NETIF_F_RXCSUM) {
  3638. napi_disable(&fep->napi);
  3639. netif_tx_lock_bh(netdev);
  3640. fec_stop(netdev);
  3641. fec_enet_set_netdev_features(netdev, features);
  3642. fec_restart(netdev);
  3643. netif_tx_wake_all_queues(netdev);
  3644. netif_tx_unlock_bh(netdev);
  3645. napi_enable(&fep->napi);
  3646. } else {
  3647. fec_enet_set_netdev_features(netdev, features);
  3648. }
  3649. return 0;
  3650. }
  3651. static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb,
  3652. struct net_device *sb_dev)
  3653. {
  3654. struct fec_enet_private *fep = netdev_priv(ndev);
  3655. u16 vlan_tag = 0;
  3656. if (!(fep->quirks & FEC_QUIRK_HAS_AVB))
  3657. return netdev_pick_tx(ndev, skb, NULL);
  3658. /* VLAN is present in the payload.*/
  3659. if (eth_type_vlan(skb->protocol)) {
  3660. struct vlan_ethhdr *vhdr = skb_vlan_eth_hdr(skb);
  3661. vlan_tag = ntohs(vhdr->h_vlan_TCI);
  3662. /* VLAN is present in the skb but not yet pushed in the payload.*/
  3663. } else if (skb_vlan_tag_present(skb)) {
  3664. vlan_tag = skb->vlan_tci;
  3665. } else {
  3666. return vlan_tag;
  3667. }
  3668. return fec_enet_vlan_pri_to_queue[vlan_tag >> 13];
  3669. }
  3670. static void fec_free_rxq(struct fec_enet_priv_rx_q *rxq)
  3671. {
  3672. fec_xdp_rxq_info_unreg(rxq);
  3673. fec_free_rxq_buffers(rxq);
  3674. kfree(rxq);
  3675. }
  3676. static struct fec_enet_priv_rx_q *
  3677. fec_alloc_new_rxq_xsk(struct fec_enet_private *fep, int queue,
  3678. struct xsk_buff_pool *pool)
  3679. {
  3680. struct fec_enet_priv_rx_q *old_rxq = fep->rx_queue[queue];
  3681. struct fec_enet_priv_rx_q *rxq;
  3682. union fec_rx_buffer *buf;
  3683. int i;
  3684. rxq = kzalloc_obj(*rxq);
  3685. if (!rxq)
  3686. return NULL;
  3687. /* Copy the BD ring to the new rxq */
  3688. rxq->bd = old_rxq->bd;
  3689. rxq->id = queue;
  3690. rxq->xsk_pool = pool;
  3691. buf = &rxq->rx_buf[0];
  3692. for (i = 0; i < rxq->bd.ring_size; i++) {
  3693. buf[i].xdp = xsk_buff_alloc(pool);
  3694. /* RX XDP ZC buffer pool may not be populated, e.g.
  3695. * xdpsock TX-only.
  3696. */
  3697. if (!buf[i].xdp)
  3698. break;
  3699. }
  3700. if (fec_xdp_rxq_info_reg(fep, rxq))
  3701. goto free_buffers;
  3702. return rxq;
  3703. free_buffers:
  3704. while (--i >= 0)
  3705. xsk_buff_free(buf[i].xdp);
  3706. kfree(rxq);
  3707. return NULL;
  3708. }
  3709. static struct fec_enet_priv_rx_q *
  3710. fec_alloc_new_rxq_pp(struct fec_enet_private *fep, int queue)
  3711. {
  3712. struct fec_enet_priv_rx_q *old_rxq = fep->rx_queue[queue];
  3713. struct fec_enet_priv_rx_q *rxq;
  3714. union fec_rx_buffer *buf;
  3715. int i = 0;
  3716. rxq = kzalloc_obj(*rxq);
  3717. if (!rxq)
  3718. return NULL;
  3719. rxq->bd = old_rxq->bd;
  3720. rxq->id = queue;
  3721. if (fec_enet_create_page_pool(fep, rxq))
  3722. goto free_rxq;
  3723. buf = &rxq->rx_buf[0];
  3724. for (; i < rxq->bd.ring_size; i++) {
  3725. buf[i].page = page_pool_dev_alloc_pages(rxq->page_pool);
  3726. if (!buf[i].page)
  3727. goto free_buffers;
  3728. }
  3729. if (fec_xdp_rxq_info_reg(fep, rxq))
  3730. goto free_buffers;
  3731. return rxq;
  3732. free_buffers:
  3733. while (--i >= 0)
  3734. page_pool_put_full_page(rxq->page_pool,
  3735. buf[i].page, false);
  3736. page_pool_destroy(rxq->page_pool);
  3737. free_rxq:
  3738. kfree(rxq);
  3739. return NULL;
  3740. }
  3741. static void fec_init_rxq_bd_buffers(struct fec_enet_priv_rx_q *rxq, bool xsk)
  3742. {
  3743. union fec_rx_buffer *buf = &rxq->rx_buf[0];
  3744. struct bufdesc *bdp = rxq->bd.base;
  3745. dma_addr_t dma;
  3746. for (int i = 0; i < rxq->bd.ring_size; i++) {
  3747. if (xsk)
  3748. dma = buf[i].xdp ?
  3749. xsk_buff_xdp_get_dma(buf[i].xdp) : 0;
  3750. else
  3751. dma = page_pool_get_dma_addr(buf[i].page) +
  3752. FEC_ENET_XDP_HEADROOM;
  3753. bdp->cbd_bufaddr = cpu_to_fec32(dma);
  3754. bdp = fec_enet_get_nextdesc(bdp, &rxq->bd);
  3755. }
  3756. }
  3757. static int fec_xsk_restart_napi(struct fec_enet_private *fep,
  3758. struct xsk_buff_pool *pool,
  3759. u16 queue)
  3760. {
  3761. struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
  3762. struct net_device *ndev = fep->netdev;
  3763. struct fec_enet_priv_rx_q *rxq;
  3764. int err;
  3765. napi_disable(&fep->napi);
  3766. netif_tx_disable(ndev);
  3767. synchronize_rcu();
  3768. rxq = pool ? fec_alloc_new_rxq_xsk(fep, queue, pool) :
  3769. fec_alloc_new_rxq_pp(fep, queue);
  3770. if (!rxq) {
  3771. err = -ENOMEM;
  3772. goto err_alloc_new_rxq;
  3773. }
  3774. /* Replace the old rxq with the new rxq */
  3775. fec_free_rxq(fep->rx_queue[queue]);
  3776. fep->rx_queue[queue] = rxq;
  3777. fec_init_rxq_bd_buffers(rxq, !!pool);
  3778. txq->xsk_pool = pool;
  3779. fec_restart(ndev);
  3780. napi_enable(&fep->napi);
  3781. netif_tx_start_all_queues(ndev);
  3782. return 0;
  3783. err_alloc_new_rxq:
  3784. napi_enable(&fep->napi);
  3785. netif_tx_start_all_queues(ndev);
  3786. return err;
  3787. }
  3788. static int fec_enable_xsk_pool(struct fec_enet_private *fep,
  3789. struct xsk_buff_pool *pool,
  3790. u16 queue)
  3791. {
  3792. int err;
  3793. err = xsk_pool_dma_map(pool, &fep->pdev->dev, 0);
  3794. if (err) {
  3795. netdev_err(fep->netdev, "Failed to map xsk pool\n");
  3796. return err;
  3797. }
  3798. if (!netif_running(fep->netdev)) {
  3799. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
  3800. struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
  3801. rxq->xsk_pool = pool;
  3802. txq->xsk_pool = pool;
  3803. return 0;
  3804. }
  3805. err = fec_xsk_restart_napi(fep, pool, queue);
  3806. if (err) {
  3807. xsk_pool_dma_unmap(pool, 0);
  3808. return err;
  3809. }
  3810. return 0;
  3811. }
  3812. static int fec_disable_xsk_pool(struct fec_enet_private *fep,
  3813. u16 queue)
  3814. {
  3815. struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
  3816. struct xsk_buff_pool *old_pool = txq->xsk_pool;
  3817. int err;
  3818. if (!netif_running(fep->netdev)) {
  3819. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[queue];
  3820. xsk_pool_dma_unmap(old_pool, 0);
  3821. rxq->xsk_pool = NULL;
  3822. txq->xsk_pool = NULL;
  3823. return 0;
  3824. }
  3825. err = fec_xsk_restart_napi(fep, NULL, queue);
  3826. if (err)
  3827. return err;
  3828. xsk_pool_dma_unmap(old_pool, 0);
  3829. return 0;
  3830. }
  3831. static int fec_setup_xsk_pool(struct fec_enet_private *fep,
  3832. struct xsk_buff_pool *pool,
  3833. u16 queue)
  3834. {
  3835. if (queue >= fep->num_rx_queues || queue >= fep->num_tx_queues)
  3836. return -ERANGE;
  3837. return pool ? fec_enable_xsk_pool(fep, pool, queue) :
  3838. fec_disable_xsk_pool(fep, queue);
  3839. }
  3840. static int fec_enet_bpf(struct net_device *dev, struct netdev_bpf *bpf)
  3841. {
  3842. struct fec_enet_private *fep = netdev_priv(dev);
  3843. bool is_run = netif_running(dev);
  3844. struct bpf_prog *old_prog;
  3845. /* No need to support the SoCs that require to do the frame swap
  3846. * because the performance wouldn't be better than the skb mode.
  3847. */
  3848. if (fep->quirks & FEC_QUIRK_SWAP_FRAME)
  3849. return -EOPNOTSUPP;
  3850. switch (bpf->command) {
  3851. case XDP_SETUP_PROG:
  3852. if (!bpf->prog)
  3853. xdp_features_clear_redirect_target(dev);
  3854. if (is_run) {
  3855. napi_disable(&fep->napi);
  3856. netif_tx_disable(dev);
  3857. }
  3858. old_prog = xchg(&fep->xdp_prog, bpf->prog);
  3859. if (old_prog)
  3860. bpf_prog_put(old_prog);
  3861. fec_restart(dev);
  3862. if (is_run) {
  3863. napi_enable(&fep->napi);
  3864. netif_tx_start_all_queues(dev);
  3865. }
  3866. if (bpf->prog)
  3867. xdp_features_set_redirect_target(dev, false);
  3868. return 0;
  3869. case XDP_SETUP_XSK_POOL:
  3870. return fec_setup_xsk_pool(fep, bpf->xsk.pool,
  3871. bpf->xsk.queue_id);
  3872. default:
  3873. return -EOPNOTSUPP;
  3874. }
  3875. }
  3876. static int fec_enet_txq_xmit_frame(struct fec_enet_private *fep,
  3877. struct fec_enet_priv_tx_q *txq,
  3878. void *frame, u32 dma_sync_len,
  3879. bool ndo_xmit)
  3880. {
  3881. unsigned int index, status, estatus;
  3882. struct bufdesc *bdp;
  3883. dma_addr_t dma_addr;
  3884. int entries_free;
  3885. u16 frame_len;
  3886. entries_free = fec_enet_get_free_txdesc_num(txq);
  3887. if (entries_free < MAX_SKB_FRAGS + 1) {
  3888. netdev_err_once(fep->netdev, "NOT enough BD for SG!\n");
  3889. return -EBUSY;
  3890. }
  3891. /* Fill in a Tx ring entry */
  3892. bdp = txq->bd.cur;
  3893. status = fec16_to_cpu(bdp->cbd_sc);
  3894. status &= ~BD_ENET_TX_STATS;
  3895. index = fec_enet_get_bd_index(bdp, &txq->bd);
  3896. if (ndo_xmit) {
  3897. struct xdp_frame *xdpf = frame;
  3898. dma_addr = dma_map_single(&fep->pdev->dev, xdpf->data,
  3899. xdpf->len, DMA_TO_DEVICE);
  3900. if (dma_mapping_error(&fep->pdev->dev, dma_addr))
  3901. return -ENOMEM;
  3902. frame_len = xdpf->len;
  3903. txq->tx_buf[index].buf_p = xdpf;
  3904. txq->tx_buf[index].type = FEC_TXBUF_T_XDP_NDO;
  3905. } else {
  3906. struct xdp_buff *xdpb = frame;
  3907. struct page *page;
  3908. page = virt_to_page(xdpb->data);
  3909. dma_addr = page_pool_get_dma_addr(page) +
  3910. (xdpb->data - xdpb->data_hard_start);
  3911. dma_sync_single_for_device(&fep->pdev->dev, dma_addr,
  3912. dma_sync_len, DMA_BIDIRECTIONAL);
  3913. frame_len = xdpb->data_end - xdpb->data;
  3914. txq->tx_buf[index].buf_p = page;
  3915. txq->tx_buf[index].type = FEC_TXBUF_T_XDP_TX;
  3916. }
  3917. status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST);
  3918. if (fep->bufdesc_ex)
  3919. estatus = BD_ENET_TX_INT;
  3920. bdp->cbd_bufaddr = cpu_to_fec32(dma_addr);
  3921. bdp->cbd_datlen = cpu_to_fec16(frame_len);
  3922. if (fep->bufdesc_ex) {
  3923. struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp;
  3924. if (fep->quirks & FEC_QUIRK_HAS_AVB)
  3925. estatus |= FEC_TX_BD_FTYPE(txq->bd.qid);
  3926. ebdp->cbd_bdu = 0;
  3927. ebdp->cbd_esc = cpu_to_fec32(estatus);
  3928. }
  3929. /* Make sure the updates to rest of the descriptor are performed before
  3930. * transferring ownership.
  3931. */
  3932. dma_wmb();
  3933. /* Send it on its way. Tell FEC it's ready, interrupt when done,
  3934. * it's the last BD of the frame, and to put the CRC on the end.
  3935. */
  3936. status |= (BD_ENET_TX_READY | BD_ENET_TX_TC);
  3937. bdp->cbd_sc = cpu_to_fec16(status);
  3938. /* If this was the last BD in the ring, start at the beginning again. */
  3939. bdp = fec_enet_get_nextdesc(bdp, &txq->bd);
  3940. /* Make sure the update to bdp are performed before txq->bd.cur. */
  3941. dma_wmb();
  3942. txq->bd.cur = bdp;
  3943. return 0;
  3944. }
  3945. static int fec_enet_xdp_tx_xmit(struct fec_enet_private *fep,
  3946. int cpu, struct xdp_buff *xdp,
  3947. u32 dma_sync_len, int queue)
  3948. {
  3949. struct netdev_queue *nq = netdev_get_tx_queue(fep->netdev, queue);
  3950. struct fec_enet_priv_tx_q *txq = fep->tx_queue[queue];
  3951. int ret;
  3952. __netif_tx_lock(nq, cpu);
  3953. /* Avoid tx timeout as XDP shares the queue with kernel stack */
  3954. txq_trans_cond_update(nq);
  3955. ret = fec_enet_txq_xmit_frame(fep, txq, xdp, dma_sync_len, false);
  3956. __netif_tx_unlock(nq);
  3957. return ret;
  3958. }
  3959. static int fec_enet_xdp_xmit(struct net_device *dev,
  3960. int num_frames,
  3961. struct xdp_frame **frames,
  3962. u32 flags)
  3963. {
  3964. struct fec_enet_private *fep = netdev_priv(dev);
  3965. struct fec_enet_priv_tx_q *txq;
  3966. int cpu = smp_processor_id();
  3967. unsigned int sent_frames = 0;
  3968. struct netdev_queue *nq;
  3969. unsigned int queue;
  3970. int i;
  3971. queue = fec_enet_xdp_get_tx_queue(fep, cpu);
  3972. txq = fep->tx_queue[queue];
  3973. nq = netdev_get_tx_queue(fep->netdev, queue);
  3974. __netif_tx_lock(nq, cpu);
  3975. /* Avoid tx timeout as XDP shares the queue with kernel stack */
  3976. txq_trans_cond_update(nq);
  3977. for (i = 0; i < num_frames; i++) {
  3978. if (fec_enet_txq_xmit_frame(fep, txq, frames[i], 0, true) < 0)
  3979. break;
  3980. sent_frames++;
  3981. }
  3982. if (sent_frames)
  3983. fec_txq_trigger_xmit(fep, txq);
  3984. __netif_tx_unlock(nq);
  3985. return sent_frames;
  3986. }
  3987. static int fec_enet_xsk_wakeup(struct net_device *ndev, u32 queue, u32 flags)
  3988. {
  3989. struct fec_enet_private *fep = netdev_priv(ndev);
  3990. struct fec_enet_priv_rx_q *rxq;
  3991. if (!netif_running(ndev) || !netif_carrier_ok(ndev))
  3992. return -ENETDOWN;
  3993. if (queue >= fep->num_rx_queues || queue >= fep->num_tx_queues)
  3994. return -ERANGE;
  3995. rxq = fep->rx_queue[queue];
  3996. if (!rxq->xsk_pool)
  3997. return -EINVAL;
  3998. if (!napi_if_scheduled_mark_missed(&fep->napi)) {
  3999. if (likely(napi_schedule_prep(&fep->napi)))
  4000. __napi_schedule(&fep->napi);
  4001. }
  4002. return 0;
  4003. }
  4004. static int fec_hwtstamp_get(struct net_device *ndev,
  4005. struct kernel_hwtstamp_config *config)
  4006. {
  4007. struct fec_enet_private *fep = netdev_priv(ndev);
  4008. if (!netif_running(ndev))
  4009. return -EINVAL;
  4010. if (!fep->bufdesc_ex)
  4011. return -EOPNOTSUPP;
  4012. fec_ptp_get(ndev, config);
  4013. return 0;
  4014. }
  4015. static int fec_hwtstamp_set(struct net_device *ndev,
  4016. struct kernel_hwtstamp_config *config,
  4017. struct netlink_ext_ack *extack)
  4018. {
  4019. struct fec_enet_private *fep = netdev_priv(ndev);
  4020. if (!netif_running(ndev))
  4021. return -EINVAL;
  4022. if (!fep->bufdesc_ex)
  4023. return -EOPNOTSUPP;
  4024. return fec_ptp_set(ndev, config, extack);
  4025. }
  4026. static int fec_change_mtu(struct net_device *ndev, int new_mtu)
  4027. {
  4028. struct fec_enet_private *fep = netdev_priv(ndev);
  4029. int order;
  4030. if (netif_running(ndev))
  4031. return -EBUSY;
  4032. order = get_order(new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN
  4033. + FEC_DRV_RESERVE_SPACE);
  4034. fep->rx_frame_size = (PAGE_SIZE << order) - FEC_DRV_RESERVE_SPACE;
  4035. fep->pagepool_order = order;
  4036. WRITE_ONCE(ndev->mtu, new_mtu);
  4037. return 0;
  4038. }
  4039. static const struct net_device_ops fec_netdev_ops = {
  4040. .ndo_open = fec_enet_open,
  4041. .ndo_stop = fec_enet_close,
  4042. .ndo_start_xmit = fec_enet_start_xmit,
  4043. .ndo_select_queue = fec_enet_select_queue,
  4044. .ndo_set_rx_mode = set_multicast_list,
  4045. .ndo_validate_addr = eth_validate_addr,
  4046. .ndo_tx_timeout = fec_timeout,
  4047. .ndo_set_mac_address = fec_set_mac_address,
  4048. .ndo_change_mtu = fec_change_mtu,
  4049. .ndo_eth_ioctl = phy_do_ioctl_running,
  4050. .ndo_set_features = fec_set_features,
  4051. .ndo_bpf = fec_enet_bpf,
  4052. .ndo_xdp_xmit = fec_enet_xdp_xmit,
  4053. .ndo_xsk_wakeup = fec_enet_xsk_wakeup,
  4054. .ndo_hwtstamp_get = fec_hwtstamp_get,
  4055. .ndo_hwtstamp_set = fec_hwtstamp_set,
  4056. };
  4057. static const unsigned short offset_des_active_rxq[] = {
  4058. FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2
  4059. };
  4060. static const unsigned short offset_des_active_txq[] = {
  4061. FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2
  4062. };
  4063. /*
  4064. * XXX: We need to clean up on failure exits here.
  4065. *
  4066. */
  4067. static int fec_enet_init(struct net_device *ndev)
  4068. {
  4069. struct fec_enet_private *fep = netdev_priv(ndev);
  4070. struct bufdesc *cbd_base;
  4071. dma_addr_t bd_dma;
  4072. int bd_size;
  4073. unsigned int i;
  4074. unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) :
  4075. sizeof(struct bufdesc);
  4076. unsigned dsize_log2 = __fls(dsize);
  4077. int ret;
  4078. WARN_ON(dsize != (1 << dsize_log2));
  4079. #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
  4080. fep->tx_align = 0xf;
  4081. #else
  4082. fep->tx_align = 0x3;
  4083. #endif
  4084. fep->rx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
  4085. fep->tx_pkts_itr = FEC_ITR_ICFT_DEFAULT;
  4086. fep->rx_time_itr = FEC_ITR_ICTT_DEFAULT;
  4087. fep->tx_time_itr = FEC_ITR_ICTT_DEFAULT;
  4088. /* Check mask of the streaming and coherent API */
  4089. ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32));
  4090. if (ret < 0) {
  4091. dev_warn(&fep->pdev->dev, "No suitable DMA available\n");
  4092. return ret;
  4093. }
  4094. ret = fec_enet_alloc_queue(ndev);
  4095. if (ret)
  4096. return ret;
  4097. bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize;
  4098. /* Allocate memory for buffer descriptors. */
  4099. cbd_base = fec_dmam_alloc(&fep->pdev->dev, bd_size, &bd_dma,
  4100. GFP_KERNEL);
  4101. if (!cbd_base) {
  4102. ret = -ENOMEM;
  4103. goto free_queue_mem;
  4104. }
  4105. /* Get the Ethernet address */
  4106. ret = fec_get_mac(ndev);
  4107. if (ret)
  4108. goto free_queue_mem;
  4109. /* Set receive and transmit descriptor base. */
  4110. for (i = 0; i < fep->num_rx_queues; i++) {
  4111. struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i];
  4112. unsigned size = dsize * rxq->bd.ring_size;
  4113. rxq->bd.qid = i;
  4114. rxq->bd.base = cbd_base;
  4115. rxq->bd.cur = cbd_base;
  4116. rxq->bd.dma = bd_dma;
  4117. rxq->bd.dsize = dsize;
  4118. rxq->bd.dsize_log2 = dsize_log2;
  4119. rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i];
  4120. bd_dma += size;
  4121. cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
  4122. rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
  4123. }
  4124. for (i = 0; i < fep->num_tx_queues; i++) {
  4125. struct fec_enet_priv_tx_q *txq = fep->tx_queue[i];
  4126. unsigned size = dsize * txq->bd.ring_size;
  4127. txq->bd.qid = i;
  4128. txq->bd.base = cbd_base;
  4129. txq->bd.cur = cbd_base;
  4130. txq->bd.dma = bd_dma;
  4131. txq->bd.dsize = dsize;
  4132. txq->bd.dsize_log2 = dsize_log2;
  4133. txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i];
  4134. bd_dma += size;
  4135. cbd_base = (struct bufdesc *)(((void *)cbd_base) + size);
  4136. txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize);
  4137. }
  4138. /* The FEC Ethernet specific entries in the device structure */
  4139. ndev->watchdog_timeo = TX_TIMEOUT;
  4140. ndev->netdev_ops = &fec_netdev_ops;
  4141. ndev->ethtool_ops = &fec_enet_ethtool_ops;
  4142. writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK);
  4143. netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi);
  4144. if (fep->quirks & FEC_QUIRK_HAS_VLAN)
  4145. /* enable hw VLAN support */
  4146. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  4147. if (fep->quirks & FEC_QUIRK_HAS_CSUM) {
  4148. netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS);
  4149. /* enable hw accelerator */
  4150. ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM
  4151. | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO);
  4152. fep->csum_flags |= FLAG_RX_CSUM_ENABLED;
  4153. }
  4154. if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES)
  4155. fep->tx_align = 0;
  4156. ndev->hw_features = ndev->features;
  4157. if (!(fep->quirks & FEC_QUIRK_SWAP_FRAME))
  4158. ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
  4159. NETDEV_XDP_ACT_REDIRECT |
  4160. NETDEV_XDP_ACT_XSK_ZEROCOPY;
  4161. fec_restart(ndev);
  4162. if (fep->quirks & FEC_QUIRK_MIB_CLEAR)
  4163. fec_enet_clear_ethtool_stats(ndev);
  4164. else
  4165. fec_enet_update_ethtool_stats(ndev);
  4166. return 0;
  4167. free_queue_mem:
  4168. fec_enet_free_queue(ndev);
  4169. return ret;
  4170. }
  4171. static void fec_enet_deinit(struct net_device *ndev)
  4172. {
  4173. struct fec_enet_private *fep = netdev_priv(ndev);
  4174. netif_napi_del(&fep->napi);
  4175. fec_enet_free_queue(ndev);
  4176. }
  4177. #ifdef CONFIG_OF
  4178. static int fec_reset_phy(struct platform_device *pdev)
  4179. {
  4180. struct gpio_desc *phy_reset;
  4181. int msec = 1, phy_post_delay = 0;
  4182. struct device_node *np = pdev->dev.of_node;
  4183. int err;
  4184. if (!np)
  4185. return 0;
  4186. err = of_property_read_u32(np, "phy-reset-duration", &msec);
  4187. /* A sane reset duration should not be longer than 1s */
  4188. if (!err && msec > 1000)
  4189. msec = 1;
  4190. err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay);
  4191. /* valid reset duration should be less than 1s */
  4192. if (!err && phy_post_delay > 1000)
  4193. return -EINVAL;
  4194. phy_reset = devm_gpiod_get_optional(&pdev->dev, "phy-reset",
  4195. GPIOD_OUT_HIGH);
  4196. if (IS_ERR(phy_reset))
  4197. return dev_err_probe(&pdev->dev, PTR_ERR(phy_reset),
  4198. "failed to get phy-reset-gpios\n");
  4199. if (!phy_reset)
  4200. return 0;
  4201. if (msec > 20)
  4202. msleep(msec);
  4203. else
  4204. usleep_range(msec * 1000, msec * 1000 + 1000);
  4205. gpiod_set_value_cansleep(phy_reset, 0);
  4206. if (!phy_post_delay)
  4207. return 0;
  4208. if (phy_post_delay > 20)
  4209. msleep(phy_post_delay);
  4210. else
  4211. usleep_range(phy_post_delay * 1000,
  4212. phy_post_delay * 1000 + 1000);
  4213. return 0;
  4214. }
  4215. #else /* CONFIG_OF */
  4216. static int fec_reset_phy(struct platform_device *pdev)
  4217. {
  4218. /*
  4219. * In case of platform probe, the reset has been done
  4220. * by machine code.
  4221. */
  4222. return 0;
  4223. }
  4224. #endif /* CONFIG_OF */
  4225. static void
  4226. fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx)
  4227. {
  4228. struct device_node *np = pdev->dev.of_node;
  4229. *num_tx = *num_rx = 1;
  4230. if (!np || !of_device_is_available(np))
  4231. return;
  4232. /* parse the num of tx and rx queues */
  4233. of_property_read_u32(np, "fsl,num-tx-queues", num_tx);
  4234. of_property_read_u32(np, "fsl,num-rx-queues", num_rx);
  4235. if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) {
  4236. dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n",
  4237. *num_tx);
  4238. *num_tx = 1;
  4239. return;
  4240. }
  4241. if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) {
  4242. dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n",
  4243. *num_rx);
  4244. *num_rx = 1;
  4245. return;
  4246. }
  4247. }
  4248. static int fec_enet_get_irq_cnt(struct platform_device *pdev)
  4249. {
  4250. int irq_cnt = platform_irq_count(pdev);
  4251. if (irq_cnt > FEC_IRQ_NUM)
  4252. irq_cnt = FEC_IRQ_NUM; /* last for pps */
  4253. else if (irq_cnt == 2)
  4254. irq_cnt = 1; /* last for pps */
  4255. else if (irq_cnt <= 0)
  4256. irq_cnt = 1; /* At least 1 irq is needed */
  4257. return irq_cnt;
  4258. }
  4259. static void fec_enet_get_wakeup_irq(struct platform_device *pdev)
  4260. {
  4261. struct net_device *ndev = platform_get_drvdata(pdev);
  4262. struct fec_enet_private *fep = netdev_priv(ndev);
  4263. if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2)
  4264. fep->wake_irq = fep->irq[2];
  4265. else
  4266. fep->wake_irq = fep->irq[0];
  4267. }
  4268. static int fec_enet_init_stop_mode(struct fec_enet_private *fep,
  4269. struct device_node *np)
  4270. {
  4271. struct device_node *gpr_np;
  4272. u32 out_val[3];
  4273. int ret = 0;
  4274. gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0);
  4275. if (!gpr_np)
  4276. return 0;
  4277. ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val,
  4278. ARRAY_SIZE(out_val));
  4279. if (ret) {
  4280. dev_dbg(&fep->pdev->dev, "no stop mode property\n");
  4281. goto out;
  4282. }
  4283. fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np);
  4284. if (IS_ERR(fep->stop_gpr.gpr)) {
  4285. dev_err(&fep->pdev->dev, "could not find gpr regmap\n");
  4286. ret = PTR_ERR(fep->stop_gpr.gpr);
  4287. fep->stop_gpr.gpr = NULL;
  4288. goto out;
  4289. }
  4290. fep->stop_gpr.reg = out_val[1];
  4291. fep->stop_gpr.bit = out_val[2];
  4292. out:
  4293. of_node_put(gpr_np);
  4294. return ret;
  4295. }
  4296. static int
  4297. fec_probe(struct platform_device *pdev)
  4298. {
  4299. struct fec_enet_private *fep;
  4300. struct fec_platform_data *pdata;
  4301. phy_interface_t interface;
  4302. struct net_device *ndev;
  4303. int i, irq, ret = 0;
  4304. static int dev_id;
  4305. struct device_node *np = pdev->dev.of_node, *phy_node;
  4306. int num_tx_qs;
  4307. int num_rx_qs;
  4308. char irq_name[8];
  4309. int irq_cnt;
  4310. const struct fec_devinfo *dev_info;
  4311. fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs);
  4312. /* Init network device */
  4313. ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) +
  4314. FEC_STATS_SIZE, num_tx_qs, num_rx_qs);
  4315. if (!ndev)
  4316. return -ENOMEM;
  4317. SET_NETDEV_DEV(ndev, &pdev->dev);
  4318. /* setup board info structure */
  4319. fep = netdev_priv(ndev);
  4320. dev_info = device_get_match_data(&pdev->dev);
  4321. if (!dev_info)
  4322. dev_info = (const struct fec_devinfo *)pdev->id_entry->driver_data;
  4323. if (dev_info)
  4324. fep->quirks = dev_info->quirks;
  4325. fep->netdev = ndev;
  4326. fep->num_rx_queues = num_rx_qs;
  4327. fep->num_tx_queues = num_tx_qs;
  4328. /* default enable pause frame auto negotiation */
  4329. if (fep->quirks & FEC_QUIRK_HAS_GBIT)
  4330. fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG;
  4331. /* Select default pin state */
  4332. pinctrl_pm_select_default_state(&pdev->dev);
  4333. fep->hwp = devm_platform_ioremap_resource(pdev, 0);
  4334. if (IS_ERR(fep->hwp)) {
  4335. ret = PTR_ERR(fep->hwp);
  4336. goto failed_ioremap;
  4337. }
  4338. fep->pdev = pdev;
  4339. fep->dev_id = dev_id++;
  4340. platform_set_drvdata(pdev, ndev);
  4341. if ((of_machine_is_compatible("fsl,imx6q") ||
  4342. of_machine_is_compatible("fsl,imx6dl")) &&
  4343. !of_property_read_bool(np, "fsl,err006687-workaround-present"))
  4344. fep->quirks |= FEC_QUIRK_ERR006687;
  4345. ret = fec_enet_ipc_handle_init(fep);
  4346. if (ret)
  4347. goto failed_ipc_init;
  4348. if (of_property_read_bool(np, "fsl,magic-packet"))
  4349. fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET;
  4350. ret = fec_enet_init_stop_mode(fep, np);
  4351. if (ret)
  4352. goto failed_stop_mode;
  4353. phy_node = of_parse_phandle(np, "phy-handle", 0);
  4354. if (!phy_node && of_phy_is_fixed_link(np)) {
  4355. ret = of_phy_register_fixed_link(np);
  4356. if (ret < 0) {
  4357. dev_err(&pdev->dev,
  4358. "broken fixed-link specification\n");
  4359. goto failed_phy;
  4360. }
  4361. phy_node = of_node_get(np);
  4362. }
  4363. fep->phy_node = phy_node;
  4364. ret = of_get_phy_mode(pdev->dev.of_node, &interface);
  4365. if (ret) {
  4366. pdata = dev_get_platdata(&pdev->dev);
  4367. if (pdata)
  4368. fep->phy_interface = pdata->phy;
  4369. else
  4370. fep->phy_interface = PHY_INTERFACE_MODE_MII;
  4371. } else {
  4372. fep->phy_interface = interface;
  4373. }
  4374. ret = fec_enet_parse_rgmii_delay(fep, np);
  4375. if (ret)
  4376. goto failed_rgmii_delay;
  4377. fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  4378. if (IS_ERR(fep->clk_ipg)) {
  4379. ret = PTR_ERR(fep->clk_ipg);
  4380. goto failed_clk;
  4381. }
  4382. fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb");
  4383. if (IS_ERR(fep->clk_ahb)) {
  4384. ret = PTR_ERR(fep->clk_ahb);
  4385. goto failed_clk;
  4386. }
  4387. fep->itr_clk_rate = clk_get_rate(fep->clk_ahb);
  4388. /* enet_out is optional, depends on board */
  4389. fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out");
  4390. if (IS_ERR(fep->clk_enet_out)) {
  4391. ret = PTR_ERR(fep->clk_enet_out);
  4392. goto failed_clk;
  4393. }
  4394. fep->ptp_clk_on = false;
  4395. mutex_init(&fep->ptp_clk_mutex);
  4396. /* clk_ref is optional, depends on board */
  4397. fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref");
  4398. if (IS_ERR(fep->clk_ref)) {
  4399. ret = PTR_ERR(fep->clk_ref);
  4400. goto failed_clk;
  4401. }
  4402. fep->clk_ref_rate = clk_get_rate(fep->clk_ref);
  4403. /* clk_2x_txclk is optional, depends on board */
  4404. if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) {
  4405. fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk");
  4406. if (IS_ERR(fep->clk_2x_txclk))
  4407. fep->clk_2x_txclk = NULL;
  4408. }
  4409. fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX;
  4410. fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp");
  4411. if (IS_ERR(fep->clk_ptp)) {
  4412. fep->clk_ptp = NULL;
  4413. fep->bufdesc_ex = false;
  4414. }
  4415. ret = fec_enet_clk_enable(ndev, true);
  4416. if (ret)
  4417. goto failed_clk;
  4418. ret = clk_prepare_enable(fep->clk_ipg);
  4419. if (ret)
  4420. goto failed_clk_ipg;
  4421. ret = clk_prepare_enable(fep->clk_ahb);
  4422. if (ret)
  4423. goto failed_clk_ahb;
  4424. fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy");
  4425. if (!IS_ERR(fep->reg_phy)) {
  4426. ret = regulator_enable(fep->reg_phy);
  4427. if (ret) {
  4428. dev_err(&pdev->dev,
  4429. "Failed to enable phy regulator: %d\n", ret);
  4430. goto failed_regulator;
  4431. }
  4432. } else {
  4433. if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) {
  4434. ret = -EPROBE_DEFER;
  4435. goto failed_regulator;
  4436. }
  4437. fep->reg_phy = NULL;
  4438. }
  4439. pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT);
  4440. pm_runtime_use_autosuspend(&pdev->dev);
  4441. pm_runtime_get_noresume(&pdev->dev);
  4442. pm_runtime_set_active(&pdev->dev);
  4443. pm_runtime_enable(&pdev->dev);
  4444. ret = fec_reset_phy(pdev);
  4445. if (ret)
  4446. goto failed_reset;
  4447. irq_cnt = fec_enet_get_irq_cnt(pdev);
  4448. if (fep->bufdesc_ex)
  4449. fec_ptp_init(pdev, irq_cnt);
  4450. ret = fec_enet_init(ndev);
  4451. if (ret)
  4452. goto failed_init;
  4453. for (i = 0; i < irq_cnt; i++) {
  4454. snprintf(irq_name, sizeof(irq_name), "int%d", i);
  4455. irq = platform_get_irq_byname_optional(pdev, irq_name);
  4456. if (irq < 0)
  4457. irq = platform_get_irq(pdev, i);
  4458. if (irq < 0) {
  4459. ret = irq;
  4460. goto failed_irq;
  4461. }
  4462. ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt,
  4463. 0, pdev->name, ndev);
  4464. if (ret)
  4465. goto failed_irq;
  4466. fep->irq[i] = irq;
  4467. }
  4468. /* Decide which interrupt line is wakeup capable */
  4469. fec_enet_get_wakeup_irq(pdev);
  4470. ret = fec_enet_mii_init(pdev);
  4471. if (ret)
  4472. goto failed_mii_init;
  4473. /* Carrier starts down, phylib will bring it up */
  4474. netif_carrier_off(ndev);
  4475. fec_enet_clk_enable(ndev, false);
  4476. pinctrl_pm_select_sleep_state(&pdev->dev);
  4477. fep->pagepool_order = 0;
  4478. fep->rx_frame_size = FEC_ENET_RX_FRSIZE;
  4479. if (fep->quirks & FEC_QUIRK_JUMBO_FRAME)
  4480. fep->max_buf_size = MAX_JUMBO_BUF_SIZE;
  4481. else
  4482. fep->max_buf_size = PKT_MAXBUF_SIZE;
  4483. ndev->max_mtu = fep->max_buf_size - VLAN_ETH_HLEN - ETH_FCS_LEN;
  4484. if (fep->quirks & FEC_QUIRK_HAS_RACC)
  4485. fep->rx_shift = 2;
  4486. else
  4487. fep->rx_shift = 0;
  4488. ret = register_netdev(ndev);
  4489. if (ret)
  4490. goto failed_register;
  4491. device_init_wakeup(&ndev->dev, fep->wol_flag &
  4492. FEC_WOL_HAS_MAGIC_PACKET);
  4493. if (fep->bufdesc_ex && fep->ptp_clock)
  4494. netdev_info(ndev, "registered PHC device %d\n", fep->dev_id);
  4495. INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work);
  4496. pm_runtime_put_autosuspend(&pdev->dev);
  4497. return 0;
  4498. failed_register:
  4499. fec_enet_mii_remove(fep);
  4500. failed_mii_init:
  4501. failed_irq:
  4502. fec_enet_deinit(ndev);
  4503. failed_init:
  4504. fec_ptp_stop(pdev);
  4505. failed_reset:
  4506. pm_runtime_put_noidle(&pdev->dev);
  4507. pm_runtime_disable(&pdev->dev);
  4508. if (fep->reg_phy)
  4509. regulator_disable(fep->reg_phy);
  4510. failed_regulator:
  4511. clk_disable_unprepare(fep->clk_ahb);
  4512. failed_clk_ahb:
  4513. clk_disable_unprepare(fep->clk_ipg);
  4514. failed_clk_ipg:
  4515. fec_enet_clk_enable(ndev, false);
  4516. failed_clk:
  4517. failed_rgmii_delay:
  4518. if (of_phy_is_fixed_link(np))
  4519. of_phy_deregister_fixed_link(np);
  4520. of_node_put(phy_node);
  4521. failed_stop_mode:
  4522. failed_ipc_init:
  4523. failed_phy:
  4524. dev_id--;
  4525. failed_ioremap:
  4526. free_netdev(ndev);
  4527. return ret;
  4528. }
  4529. static void
  4530. fec_drv_remove(struct platform_device *pdev)
  4531. {
  4532. struct net_device *ndev = platform_get_drvdata(pdev);
  4533. struct fec_enet_private *fep = netdev_priv(ndev);
  4534. struct device_node *np = pdev->dev.of_node;
  4535. int ret;
  4536. ret = pm_runtime_get_sync(&pdev->dev);
  4537. if (ret < 0)
  4538. dev_err(&pdev->dev,
  4539. "Failed to resume device in remove callback (%pe)\n",
  4540. ERR_PTR(ret));
  4541. cancel_work_sync(&fep->tx_timeout_work);
  4542. fec_ptp_stop(pdev);
  4543. unregister_netdev(ndev);
  4544. fec_enet_mii_remove(fep);
  4545. if (fep->reg_phy)
  4546. regulator_disable(fep->reg_phy);
  4547. if (of_phy_is_fixed_link(np))
  4548. of_phy_deregister_fixed_link(np);
  4549. of_node_put(fep->phy_node);
  4550. /* After pm_runtime_get_sync() failed, the clks are still off, so skip
  4551. * disabling them again.
  4552. */
  4553. if (ret >= 0) {
  4554. clk_disable_unprepare(fep->clk_ahb);
  4555. clk_disable_unprepare(fep->clk_ipg);
  4556. }
  4557. pm_runtime_put_noidle(&pdev->dev);
  4558. pm_runtime_disable(&pdev->dev);
  4559. fec_enet_deinit(ndev);
  4560. free_netdev(ndev);
  4561. }
  4562. static int fec_suspend(struct device *dev)
  4563. {
  4564. struct net_device *ndev = dev_get_drvdata(dev);
  4565. struct fec_enet_private *fep = netdev_priv(ndev);
  4566. int ret;
  4567. rtnl_lock();
  4568. if (netif_running(ndev)) {
  4569. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE)
  4570. fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON;
  4571. phy_stop(ndev->phydev);
  4572. napi_disable(&fep->napi);
  4573. netif_tx_lock_bh(ndev);
  4574. netif_device_detach(ndev);
  4575. netif_tx_unlock_bh(ndev);
  4576. fec_stop(ndev);
  4577. if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
  4578. fec_irqs_disable(ndev);
  4579. pinctrl_pm_select_sleep_state(&fep->pdev->dev);
  4580. } else {
  4581. fec_irqs_disable_except_wakeup(ndev);
  4582. if (fep->wake_irq > 0) {
  4583. disable_irq(fep->wake_irq);
  4584. enable_irq_wake(fep->wake_irq);
  4585. }
  4586. fec_enet_stop_mode(fep, true);
  4587. }
  4588. /* It's safe to disable clocks since interrupts are masked */
  4589. fec_enet_clk_enable(ndev, false);
  4590. fep->rpm_active = !pm_runtime_status_suspended(dev);
  4591. if (fep->rpm_active) {
  4592. ret = pm_runtime_force_suspend(dev);
  4593. if (ret < 0) {
  4594. rtnl_unlock();
  4595. return ret;
  4596. }
  4597. }
  4598. }
  4599. rtnl_unlock();
  4600. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE))
  4601. regulator_disable(fep->reg_phy);
  4602. /* SOC supply clock to phy, when clock is disabled, phy link down
  4603. * SOC control phy regulator, when regulator is disabled, phy link down
  4604. */
  4605. if (fep->clk_enet_out || fep->reg_phy)
  4606. fep->link = 0;
  4607. return 0;
  4608. }
  4609. static int fec_resume(struct device *dev)
  4610. {
  4611. struct net_device *ndev = dev_get_drvdata(dev);
  4612. struct fec_enet_private *fep = netdev_priv(ndev);
  4613. int ret;
  4614. int val;
  4615. if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) {
  4616. ret = regulator_enable(fep->reg_phy);
  4617. if (ret)
  4618. return ret;
  4619. }
  4620. rtnl_lock();
  4621. if (netif_running(ndev)) {
  4622. if (fep->rpm_active)
  4623. pm_runtime_force_resume(dev);
  4624. ret = fec_enet_clk_enable(ndev, true);
  4625. if (ret) {
  4626. rtnl_unlock();
  4627. goto failed_clk;
  4628. }
  4629. if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) {
  4630. fec_enet_stop_mode(fep, false);
  4631. if (fep->wake_irq) {
  4632. disable_irq_wake(fep->wake_irq);
  4633. enable_irq(fep->wake_irq);
  4634. }
  4635. val = readl(fep->hwp + FEC_ECNTRL);
  4636. val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP);
  4637. writel(val, fep->hwp + FEC_ECNTRL);
  4638. fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON;
  4639. } else {
  4640. pinctrl_pm_select_default_state(&fep->pdev->dev);
  4641. }
  4642. fec_restart(ndev);
  4643. netif_tx_lock_bh(ndev);
  4644. netif_device_attach(ndev);
  4645. netif_tx_unlock_bh(ndev);
  4646. napi_enable(&fep->napi);
  4647. phy_init_hw(ndev->phydev);
  4648. phy_start(ndev->phydev);
  4649. }
  4650. rtnl_unlock();
  4651. return 0;
  4652. failed_clk:
  4653. if (fep->reg_phy)
  4654. regulator_disable(fep->reg_phy);
  4655. return ret;
  4656. }
  4657. static int fec_runtime_suspend(struct device *dev)
  4658. {
  4659. struct net_device *ndev = dev_get_drvdata(dev);
  4660. struct fec_enet_private *fep = netdev_priv(ndev);
  4661. clk_disable_unprepare(fep->clk_ahb);
  4662. clk_disable_unprepare(fep->clk_ipg);
  4663. return 0;
  4664. }
  4665. static int fec_runtime_resume(struct device *dev)
  4666. {
  4667. struct net_device *ndev = dev_get_drvdata(dev);
  4668. struct fec_enet_private *fep = netdev_priv(ndev);
  4669. int ret;
  4670. ret = clk_prepare_enable(fep->clk_ahb);
  4671. if (ret)
  4672. return ret;
  4673. ret = clk_prepare_enable(fep->clk_ipg);
  4674. if (ret)
  4675. goto failed_clk_ipg;
  4676. return 0;
  4677. failed_clk_ipg:
  4678. clk_disable_unprepare(fep->clk_ahb);
  4679. return ret;
  4680. }
  4681. static const struct dev_pm_ops fec_pm_ops = {
  4682. SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume)
  4683. RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL)
  4684. };
  4685. static struct platform_driver fec_driver = {
  4686. .driver = {
  4687. .name = DRIVER_NAME,
  4688. .pm = pm_ptr(&fec_pm_ops),
  4689. .of_match_table = fec_dt_ids,
  4690. .suppress_bind_attrs = true,
  4691. },
  4692. .id_table = fec_devtype,
  4693. .probe = fec_probe,
  4694. .remove = fec_drv_remove,
  4695. };
  4696. module_platform_driver(fec_driver);
  4697. MODULE_DESCRIPTION("NXP Fast Ethernet Controller (FEC) driver");
  4698. MODULE_LICENSE("GPL");