stmmac_main.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671
  1. /*******************************************************************************
  2. This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
  3. ST Ethernet IPs are built around a Synopsys IP Core.
  4. Copyright(C) 2007-2011 STMicroelectronics Ltd
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms and conditions of the GNU General Public License,
  7. version 2, as published by the Free Software Foundation.
  8. This program is distributed in the hope it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. The full GNU General Public License is included in this distribution in
  13. the file called "COPYING".
  14. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  15. Documentation available at:
  16. http://www.stlinux.com
  17. Support available at:
  18. https://bugzilla.stlinux.com/
  19. *******************************************************************************/
  20. #include <linux/clk.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ip.h>
  24. #include <linux/tcp.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/crc32.h>
  29. #include <linux/mii.h>
  30. #include <linux/if.h>
  31. #include <linux/if_vlan.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/slab.h>
  34. #include <linux/prefetch.h>
  35. #include <linux/pinctrl/consumer.h>
  36. #ifdef CONFIG_DEBUG_FS
  37. #include <linux/debugfs.h>
  38. #include <linux/seq_file.h>
  39. #endif /* CONFIG_DEBUG_FS */
  40. #include <linux/net_tstamp.h>
  41. #include <net/pkt_cls.h>
  42. #include "stmmac_ptp.h"
  43. #include "stmmac.h"
  44. #include <linux/reset.h>
  45. #include <linux/of_mdio.h>
  46. #include "dwmac1000.h"
  47. #include "dwxgmac2.h"
  48. #include "hwif.h"
  49. #define STMMAC_ALIGN(x) __ALIGN_KERNEL(x, SMP_CACHE_BYTES)
  50. #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
  51. /* Module parameters */
  52. #define TX_TIMEO 5000
  53. static int watchdog = TX_TIMEO;
  54. module_param(watchdog, int, 0644);
  55. MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
  56. static int debug = -1;
  57. module_param(debug, int, 0644);
  58. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  59. static int phyaddr = -1;
  60. module_param(phyaddr, int, 0444);
  61. MODULE_PARM_DESC(phyaddr, "Physical device address");
  62. #define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
  63. #define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
  64. static int flow_ctrl = FLOW_OFF;
  65. module_param(flow_ctrl, int, 0644);
  66. MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
  67. static int pause = PAUSE_TIME;
  68. module_param(pause, int, 0644);
  69. MODULE_PARM_DESC(pause, "Flow Control Pause Time");
  70. #define TC_DEFAULT 64
  71. static int tc = TC_DEFAULT;
  72. module_param(tc, int, 0644);
  73. MODULE_PARM_DESC(tc, "DMA threshold control value");
  74. #define DEFAULT_BUFSIZE 1536
  75. static int buf_sz = DEFAULT_BUFSIZE;
  76. module_param(buf_sz, int, 0644);
  77. MODULE_PARM_DESC(buf_sz, "DMA buffer size");
  78. #define STMMAC_RX_COPYBREAK 256
  79. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  80. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  81. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  82. #define STMMAC_DEFAULT_LPI_TIMER 1000
  83. static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  84. module_param(eee_timer, int, 0644);
  85. MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
  86. #define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
  87. /* By default the driver will use the ring mode to manage tx and rx descriptors,
  88. * but allow user to force to use the chain instead of the ring
  89. */
  90. static unsigned int chain_mode;
  91. module_param(chain_mode, int, 0444);
  92. MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
  93. static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
  94. #ifdef CONFIG_DEBUG_FS
  95. static int stmmac_init_fs(struct net_device *dev);
  96. static void stmmac_exit_fs(struct net_device *dev);
  97. #endif
  98. #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
  99. /**
  100. * stmmac_verify_args - verify the driver parameters.
  101. * Description: it checks the driver parameters and set a default in case of
  102. * errors.
  103. */
  104. static void stmmac_verify_args(void)
  105. {
  106. if (unlikely(watchdog < 0))
  107. watchdog = TX_TIMEO;
  108. if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
  109. buf_sz = DEFAULT_BUFSIZE;
  110. if (unlikely(flow_ctrl > 1))
  111. flow_ctrl = FLOW_AUTO;
  112. else if (likely(flow_ctrl < 0))
  113. flow_ctrl = FLOW_OFF;
  114. if (unlikely((pause < 0) || (pause > 0xffff)))
  115. pause = PAUSE_TIME;
  116. if (eee_timer < 0)
  117. eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  118. }
  119. /**
  120. * stmmac_disable_all_queues - Disable all queues
  121. * @priv: driver private structure
  122. */
  123. static void stmmac_disable_all_queues(struct stmmac_priv *priv)
  124. {
  125. u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
  126. u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
  127. u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
  128. u32 queue;
  129. for (queue = 0; queue < maxq; queue++) {
  130. struct stmmac_channel *ch = &priv->channel[queue];
  131. napi_disable(&ch->napi);
  132. }
  133. }
  134. /**
  135. * stmmac_enable_all_queues - Enable all queues
  136. * @priv: driver private structure
  137. */
  138. static void stmmac_enable_all_queues(struct stmmac_priv *priv)
  139. {
  140. u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
  141. u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
  142. u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
  143. u32 queue;
  144. for (queue = 0; queue < maxq; queue++) {
  145. struct stmmac_channel *ch = &priv->channel[queue];
  146. napi_enable(&ch->napi);
  147. }
  148. }
  149. /**
  150. * stmmac_stop_all_queues - Stop all queues
  151. * @priv: driver private structure
  152. */
  153. static void stmmac_stop_all_queues(struct stmmac_priv *priv)
  154. {
  155. u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
  156. u32 queue;
  157. for (queue = 0; queue < tx_queues_cnt; queue++)
  158. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
  159. }
  160. /**
  161. * stmmac_start_all_queues - Start all queues
  162. * @priv: driver private structure
  163. */
  164. static void stmmac_start_all_queues(struct stmmac_priv *priv)
  165. {
  166. u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
  167. u32 queue;
  168. for (queue = 0; queue < tx_queues_cnt; queue++)
  169. netif_tx_start_queue(netdev_get_tx_queue(priv->dev, queue));
  170. }
  171. static void stmmac_service_event_schedule(struct stmmac_priv *priv)
  172. {
  173. if (!test_bit(STMMAC_DOWN, &priv->state) &&
  174. !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
  175. queue_work(priv->wq, &priv->service_task);
  176. }
  177. static void stmmac_global_err(struct stmmac_priv *priv)
  178. {
  179. netif_carrier_off(priv->dev);
  180. set_bit(STMMAC_RESET_REQUESTED, &priv->state);
  181. stmmac_service_event_schedule(priv);
  182. }
  183. /**
  184. * stmmac_clk_csr_set - dynamically set the MDC clock
  185. * @priv: driver private structure
  186. * Description: this is to dynamically set the MDC clock according to the csr
  187. * clock input.
  188. * Note:
  189. * If a specific clk_csr value is passed from the platform
  190. * this means that the CSR Clock Range selection cannot be
  191. * changed at run-time and it is fixed (as reported in the driver
  192. * documentation). Viceversa the driver will try to set the MDC
  193. * clock dynamically according to the actual clock input.
  194. */
  195. static void stmmac_clk_csr_set(struct stmmac_priv *priv)
  196. {
  197. u32 clk_rate;
  198. clk_rate = clk_get_rate(priv->plat->stmmac_clk);
  199. /* Platform provided default clk_csr would be assumed valid
  200. * for all other cases except for the below mentioned ones.
  201. * For values higher than the IEEE 802.3 specified frequency
  202. * we can not estimate the proper divider as it is not known
  203. * the frequency of clk_csr_i. So we do not change the default
  204. * divider.
  205. */
  206. if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
  207. if (clk_rate < CSR_F_35M)
  208. priv->clk_csr = STMMAC_CSR_20_35M;
  209. else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
  210. priv->clk_csr = STMMAC_CSR_35_60M;
  211. else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
  212. priv->clk_csr = STMMAC_CSR_60_100M;
  213. else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
  214. priv->clk_csr = STMMAC_CSR_100_150M;
  215. else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
  216. priv->clk_csr = STMMAC_CSR_150_250M;
  217. else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
  218. priv->clk_csr = STMMAC_CSR_250_300M;
  219. }
  220. if (priv->plat->has_sun8i) {
  221. if (clk_rate > 160000000)
  222. priv->clk_csr = 0x03;
  223. else if (clk_rate > 80000000)
  224. priv->clk_csr = 0x02;
  225. else if (clk_rate > 40000000)
  226. priv->clk_csr = 0x01;
  227. else
  228. priv->clk_csr = 0;
  229. }
  230. if (priv->plat->has_xgmac) {
  231. if (clk_rate > 400000000)
  232. priv->clk_csr = 0x5;
  233. else if (clk_rate > 350000000)
  234. priv->clk_csr = 0x4;
  235. else if (clk_rate > 300000000)
  236. priv->clk_csr = 0x3;
  237. else if (clk_rate > 250000000)
  238. priv->clk_csr = 0x2;
  239. else if (clk_rate > 150000000)
  240. priv->clk_csr = 0x1;
  241. else
  242. priv->clk_csr = 0x0;
  243. }
  244. }
  245. static void print_pkt(unsigned char *buf, int len)
  246. {
  247. pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
  248. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
  249. }
  250. static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
  251. {
  252. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  253. u32 avail;
  254. if (tx_q->dirty_tx > tx_q->cur_tx)
  255. avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
  256. else
  257. avail = DMA_TX_SIZE - tx_q->cur_tx + tx_q->dirty_tx - 1;
  258. return avail;
  259. }
  260. /**
  261. * stmmac_rx_dirty - Get RX queue dirty
  262. * @priv: driver private structure
  263. * @queue: RX queue index
  264. */
  265. static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
  266. {
  267. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  268. u32 dirty;
  269. if (rx_q->dirty_rx <= rx_q->cur_rx)
  270. dirty = rx_q->cur_rx - rx_q->dirty_rx;
  271. else
  272. dirty = DMA_RX_SIZE - rx_q->dirty_rx + rx_q->cur_rx;
  273. return dirty;
  274. }
  275. /**
  276. * stmmac_hw_fix_mac_speed - callback for speed selection
  277. * @priv: driver private structure
  278. * Description: on some platforms (e.g. ST), some HW system configuration
  279. * registers have to be set according to the link speed negotiated.
  280. */
  281. static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
  282. {
  283. struct net_device *ndev = priv->dev;
  284. struct phy_device *phydev = ndev->phydev;
  285. if (likely(priv->plat->fix_mac_speed))
  286. priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
  287. }
  288. /**
  289. * stmmac_enable_eee_mode - check and enter in LPI mode
  290. * @priv: driver private structure
  291. * Description: this function is to verify and enter in LPI mode in case of
  292. * EEE.
  293. */
  294. static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
  295. {
  296. u32 tx_cnt = priv->plat->tx_queues_to_use;
  297. u32 queue;
  298. /* check if all TX queues have the work finished */
  299. for (queue = 0; queue < tx_cnt; queue++) {
  300. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  301. if (tx_q->dirty_tx != tx_q->cur_tx)
  302. return; /* still unfinished work */
  303. }
  304. /* Check and enter in LPI mode */
  305. if (!priv->tx_path_in_lpi_mode)
  306. stmmac_set_eee_mode(priv, priv->hw,
  307. priv->plat->en_tx_lpi_clockgating);
  308. }
  309. /**
  310. * stmmac_disable_eee_mode - disable and exit from LPI mode
  311. * @priv: driver private structure
  312. * Description: this function is to exit and disable EEE in case of
  313. * LPI state is true. This is called by the xmit.
  314. */
  315. void stmmac_disable_eee_mode(struct stmmac_priv *priv)
  316. {
  317. stmmac_reset_eee_mode(priv, priv->hw);
  318. del_timer_sync(&priv->eee_ctrl_timer);
  319. priv->tx_path_in_lpi_mode = false;
  320. }
  321. /**
  322. * stmmac_eee_ctrl_timer - EEE TX SW timer.
  323. * @arg : data hook
  324. * Description:
  325. * if there is no data transfer and if we are not in LPI state,
  326. * then MAC Transmitter can be moved to LPI state.
  327. */
  328. static void stmmac_eee_ctrl_timer(struct timer_list *t)
  329. {
  330. struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
  331. stmmac_enable_eee_mode(priv);
  332. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  333. }
  334. /**
  335. * stmmac_eee_init - init EEE
  336. * @priv: driver private structure
  337. * Description:
  338. * if the GMAC supports the EEE (from the HW cap reg) and the phy device
  339. * can also manage EEE, this function enable the LPI state and start related
  340. * timer.
  341. */
  342. bool stmmac_eee_init(struct stmmac_priv *priv)
  343. {
  344. struct net_device *ndev = priv->dev;
  345. int interface = priv->plat->interface;
  346. bool ret = false;
  347. if ((interface != PHY_INTERFACE_MODE_MII) &&
  348. (interface != PHY_INTERFACE_MODE_GMII) &&
  349. !phy_interface_mode_is_rgmii(interface))
  350. goto out;
  351. /* Using PCS we cannot dial with the phy registers at this stage
  352. * so we do not support extra feature like EEE.
  353. */
  354. if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
  355. (priv->hw->pcs == STMMAC_PCS_TBI) ||
  356. (priv->hw->pcs == STMMAC_PCS_RTBI))
  357. goto out;
  358. /* MAC core supports the EEE feature. */
  359. if (priv->dma_cap.eee) {
  360. int tx_lpi_timer = priv->tx_lpi_timer;
  361. /* Check if the PHY supports EEE */
  362. if (phy_init_eee(ndev->phydev, 1)) {
  363. /* To manage at run-time if the EEE cannot be supported
  364. * anymore (for example because the lp caps have been
  365. * changed).
  366. * In that case the driver disable own timers.
  367. */
  368. mutex_lock(&priv->lock);
  369. if (priv->eee_active) {
  370. netdev_dbg(priv->dev, "disable EEE\n");
  371. del_timer_sync(&priv->eee_ctrl_timer);
  372. stmmac_set_eee_timer(priv, priv->hw, 0,
  373. tx_lpi_timer);
  374. }
  375. priv->eee_active = 0;
  376. mutex_unlock(&priv->lock);
  377. goto out;
  378. }
  379. /* Activate the EEE and start timers */
  380. mutex_lock(&priv->lock);
  381. if (!priv->eee_active) {
  382. priv->eee_active = 1;
  383. timer_setup(&priv->eee_ctrl_timer,
  384. stmmac_eee_ctrl_timer, 0);
  385. mod_timer(&priv->eee_ctrl_timer,
  386. STMMAC_LPI_T(eee_timer));
  387. stmmac_set_eee_timer(priv, priv->hw,
  388. STMMAC_DEFAULT_LIT_LS, tx_lpi_timer);
  389. }
  390. /* Set HW EEE according to the speed */
  391. stmmac_set_eee_pls(priv, priv->hw, ndev->phydev->link);
  392. ret = true;
  393. mutex_unlock(&priv->lock);
  394. netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
  395. }
  396. out:
  397. return ret;
  398. }
  399. /* stmmac_get_tx_hwtstamp - get HW TX timestamps
  400. * @priv: driver private structure
  401. * @p : descriptor pointer
  402. * @skb : the socket buffer
  403. * Description :
  404. * This function will read timestamp from the descriptor & pass it to stack.
  405. * and also perform some sanity checks.
  406. */
  407. static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
  408. struct dma_desc *p, struct sk_buff *skb)
  409. {
  410. struct skb_shared_hwtstamps shhwtstamp;
  411. u64 ns;
  412. if (!priv->hwts_tx_en)
  413. return;
  414. /* exit if skb doesn't support hw tstamp */
  415. if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
  416. return;
  417. /* check tx tstamp status */
  418. if (stmmac_get_tx_timestamp_status(priv, p)) {
  419. /* get the valid tstamp */
  420. stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
  421. memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  422. shhwtstamp.hwtstamp = ns_to_ktime(ns);
  423. netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
  424. /* pass tstamp to stack */
  425. skb_tstamp_tx(skb, &shhwtstamp);
  426. }
  427. return;
  428. }
  429. /* stmmac_get_rx_hwtstamp - get HW RX timestamps
  430. * @priv: driver private structure
  431. * @p : descriptor pointer
  432. * @np : next descriptor pointer
  433. * @skb : the socket buffer
  434. * Description :
  435. * This function will read received packet's timestamp from the descriptor
  436. * and pass it to stack. It also perform some sanity checks.
  437. */
  438. static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
  439. struct dma_desc *np, struct sk_buff *skb)
  440. {
  441. struct skb_shared_hwtstamps *shhwtstamp = NULL;
  442. struct dma_desc *desc = p;
  443. u64 ns;
  444. if (!priv->hwts_rx_en)
  445. return;
  446. /* For GMAC4, the valid timestamp is from CTX next desc. */
  447. if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
  448. desc = np;
  449. /* Check if timestamp is available */
  450. if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
  451. stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
  452. netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
  453. shhwtstamp = skb_hwtstamps(skb);
  454. memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  455. shhwtstamp->hwtstamp = ns_to_ktime(ns);
  456. } else {
  457. netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
  458. }
  459. }
  460. /**
  461. * stmmac_hwtstamp_ioctl - control hardware timestamping.
  462. * @dev: device pointer.
  463. * @ifr: An IOCTL specific structure, that can contain a pointer to
  464. * a proprietary structure used to pass information to the driver.
  465. * Description:
  466. * This function configures the MAC to enable/disable both outgoing(TX)
  467. * and incoming(RX) packets time stamping based on user input.
  468. * Return Value:
  469. * 0 on success and an appropriate -ve integer on failure.
  470. */
  471. static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  472. {
  473. struct stmmac_priv *priv = netdev_priv(dev);
  474. struct hwtstamp_config config;
  475. struct timespec64 now;
  476. u64 temp = 0;
  477. u32 ptp_v2 = 0;
  478. u32 tstamp_all = 0;
  479. u32 ptp_over_ipv4_udp = 0;
  480. u32 ptp_over_ipv6_udp = 0;
  481. u32 ptp_over_ethernet = 0;
  482. u32 snap_type_sel = 0;
  483. u32 ts_master_en = 0;
  484. u32 ts_event_en = 0;
  485. u32 value = 0;
  486. u32 sec_inc;
  487. bool xmac;
  488. xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
  489. if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
  490. netdev_alert(priv->dev, "No support for HW time stamping\n");
  491. priv->hwts_tx_en = 0;
  492. priv->hwts_rx_en = 0;
  493. return -EOPNOTSUPP;
  494. }
  495. if (copy_from_user(&config, ifr->ifr_data,
  496. sizeof(struct hwtstamp_config)))
  497. return -EFAULT;
  498. netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
  499. __func__, config.flags, config.tx_type, config.rx_filter);
  500. /* reserved for future extensions */
  501. if (config.flags)
  502. return -EINVAL;
  503. if (config.tx_type != HWTSTAMP_TX_OFF &&
  504. config.tx_type != HWTSTAMP_TX_ON)
  505. return -ERANGE;
  506. if (priv->adv_ts) {
  507. switch (config.rx_filter) {
  508. case HWTSTAMP_FILTER_NONE:
  509. /* time stamp no incoming packet at all */
  510. config.rx_filter = HWTSTAMP_FILTER_NONE;
  511. break;
  512. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  513. /* PTP v1, UDP, any kind of event packet */
  514. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  515. /* take time stamp for all event messages */
  516. if (xmac)
  517. snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
  518. else
  519. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  520. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  521. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  522. break;
  523. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  524. /* PTP v1, UDP, Sync packet */
  525. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  526. /* take time stamp for SYNC messages only */
  527. ts_event_en = PTP_TCR_TSEVNTENA;
  528. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  529. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  530. break;
  531. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  532. /* PTP v1, UDP, Delay_req packet */
  533. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  534. /* take time stamp for Delay_Req messages only */
  535. ts_master_en = PTP_TCR_TSMSTRENA;
  536. ts_event_en = PTP_TCR_TSEVNTENA;
  537. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  538. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  539. break;
  540. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  541. /* PTP v2, UDP, any kind of event packet */
  542. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  543. ptp_v2 = PTP_TCR_TSVER2ENA;
  544. /* take time stamp for all event messages */
  545. if (xmac)
  546. snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
  547. else
  548. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  549. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  550. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  551. break;
  552. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  553. /* PTP v2, UDP, Sync packet */
  554. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  555. ptp_v2 = PTP_TCR_TSVER2ENA;
  556. /* take time stamp for SYNC messages only */
  557. ts_event_en = PTP_TCR_TSEVNTENA;
  558. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  559. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  560. break;
  561. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  562. /* PTP v2, UDP, Delay_req packet */
  563. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  564. ptp_v2 = PTP_TCR_TSVER2ENA;
  565. /* take time stamp for Delay_Req messages only */
  566. ts_master_en = PTP_TCR_TSMSTRENA;
  567. ts_event_en = PTP_TCR_TSEVNTENA;
  568. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  569. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  570. break;
  571. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  572. /* PTP v2/802.AS1 any layer, any kind of event packet */
  573. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  574. ptp_v2 = PTP_TCR_TSVER2ENA;
  575. /* take time stamp for all event messages */
  576. if (xmac)
  577. snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
  578. else
  579. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  580. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  581. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  582. ptp_over_ethernet = PTP_TCR_TSIPENA;
  583. break;
  584. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  585. /* PTP v2/802.AS1, any layer, Sync packet */
  586. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  587. ptp_v2 = PTP_TCR_TSVER2ENA;
  588. /* take time stamp for SYNC messages only */
  589. ts_event_en = PTP_TCR_TSEVNTENA;
  590. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  591. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  592. ptp_over_ethernet = PTP_TCR_TSIPENA;
  593. break;
  594. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  595. /* PTP v2/802.AS1, any layer, Delay_req packet */
  596. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  597. ptp_v2 = PTP_TCR_TSVER2ENA;
  598. /* take time stamp for Delay_Req messages only */
  599. ts_master_en = PTP_TCR_TSMSTRENA;
  600. ts_event_en = PTP_TCR_TSEVNTENA;
  601. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  602. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  603. ptp_over_ethernet = PTP_TCR_TSIPENA;
  604. break;
  605. case HWTSTAMP_FILTER_NTP_ALL:
  606. case HWTSTAMP_FILTER_ALL:
  607. /* time stamp any incoming packet */
  608. config.rx_filter = HWTSTAMP_FILTER_ALL;
  609. tstamp_all = PTP_TCR_TSENALL;
  610. break;
  611. default:
  612. return -ERANGE;
  613. }
  614. } else {
  615. switch (config.rx_filter) {
  616. case HWTSTAMP_FILTER_NONE:
  617. config.rx_filter = HWTSTAMP_FILTER_NONE;
  618. break;
  619. default:
  620. /* PTP v1, UDP, any kind of event packet */
  621. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  622. break;
  623. }
  624. }
  625. priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
  626. priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
  627. if (!priv->hwts_tx_en && !priv->hwts_rx_en)
  628. stmmac_config_hw_tstamping(priv, priv->ptpaddr, 0);
  629. else {
  630. value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
  631. tstamp_all | ptp_v2 | ptp_over_ethernet |
  632. ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
  633. ts_master_en | snap_type_sel);
  634. stmmac_config_hw_tstamping(priv, priv->ptpaddr, value);
  635. /* program Sub Second Increment reg */
  636. stmmac_config_sub_second_increment(priv,
  637. priv->ptpaddr, priv->plat->clk_ptp_rate,
  638. xmac, &sec_inc);
  639. temp = div_u64(1000000000ULL, sec_inc);
  640. /* Store sub second increment and flags for later use */
  641. priv->sub_second_inc = sec_inc;
  642. priv->systime_flags = value;
  643. /* calculate default added value:
  644. * formula is :
  645. * addend = (2^32)/freq_div_ratio;
  646. * where, freq_div_ratio = 1e9ns/sec_inc
  647. */
  648. temp = (u64)(temp << 32);
  649. priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
  650. stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
  651. /* initialize system time */
  652. ktime_get_real_ts64(&now);
  653. /* lower 32 bits of tv_sec are safe until y2106 */
  654. stmmac_init_systime(priv, priv->ptpaddr,
  655. (u32)now.tv_sec, now.tv_nsec);
  656. }
  657. return copy_to_user(ifr->ifr_data, &config,
  658. sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
  659. }
  660. /**
  661. * stmmac_init_ptp - init PTP
  662. * @priv: driver private structure
  663. * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
  664. * This is done by looking at the HW cap. register.
  665. * This function also registers the ptp driver.
  666. */
  667. static int stmmac_init_ptp(struct stmmac_priv *priv)
  668. {
  669. bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
  670. if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
  671. return -EOPNOTSUPP;
  672. priv->adv_ts = 0;
  673. /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
  674. if (xmac && priv->dma_cap.atime_stamp)
  675. priv->adv_ts = 1;
  676. /* Dwmac 3.x core with extend_desc can support adv_ts */
  677. else if (priv->extend_desc && priv->dma_cap.atime_stamp)
  678. priv->adv_ts = 1;
  679. if (priv->dma_cap.time_stamp)
  680. netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
  681. if (priv->adv_ts)
  682. netdev_info(priv->dev,
  683. "IEEE 1588-2008 Advanced Timestamp supported\n");
  684. priv->hwts_tx_en = 0;
  685. priv->hwts_rx_en = 0;
  686. stmmac_ptp_register(priv);
  687. return 0;
  688. }
  689. static void stmmac_release_ptp(struct stmmac_priv *priv)
  690. {
  691. if (priv->plat->clk_ptp_ref)
  692. clk_disable_unprepare(priv->plat->clk_ptp_ref);
  693. stmmac_ptp_unregister(priv);
  694. }
  695. /**
  696. * stmmac_mac_flow_ctrl - Configure flow control in all queues
  697. * @priv: driver private structure
  698. * Description: It is used for configuring the flow control in all queues
  699. */
  700. static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
  701. {
  702. u32 tx_cnt = priv->plat->tx_queues_to_use;
  703. stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
  704. priv->pause, tx_cnt);
  705. }
  706. /**
  707. * stmmac_adjust_link - adjusts the link parameters
  708. * @dev: net device structure
  709. * Description: this is the helper called by the physical abstraction layer
  710. * drivers to communicate the phy link status. According the speed and duplex
  711. * this driver can invoke registered glue-logic as well.
  712. * It also invoke the eee initialization because it could happen when switch
  713. * on different networks (that are eee capable).
  714. */
  715. static void stmmac_adjust_link(struct net_device *dev)
  716. {
  717. struct stmmac_priv *priv = netdev_priv(dev);
  718. struct phy_device *phydev = dev->phydev;
  719. bool new_state = false;
  720. if (!phydev)
  721. return;
  722. mutex_lock(&priv->lock);
  723. if (phydev->link) {
  724. u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
  725. /* Now we make sure that we can be in full duplex mode.
  726. * If not, we operate in half-duplex mode. */
  727. if (phydev->duplex != priv->oldduplex) {
  728. new_state = true;
  729. if (!phydev->duplex)
  730. ctrl &= ~priv->hw->link.duplex;
  731. else
  732. ctrl |= priv->hw->link.duplex;
  733. priv->oldduplex = phydev->duplex;
  734. }
  735. /* Flow Control operation */
  736. if (phydev->pause)
  737. stmmac_mac_flow_ctrl(priv, phydev->duplex);
  738. if (phydev->speed != priv->speed) {
  739. new_state = true;
  740. ctrl &= ~priv->hw->link.speed_mask;
  741. switch (phydev->speed) {
  742. case SPEED_1000:
  743. ctrl |= priv->hw->link.speed1000;
  744. break;
  745. case SPEED_100:
  746. ctrl |= priv->hw->link.speed100;
  747. break;
  748. case SPEED_10:
  749. ctrl |= priv->hw->link.speed10;
  750. break;
  751. default:
  752. netif_warn(priv, link, priv->dev,
  753. "broken speed: %d\n", phydev->speed);
  754. phydev->speed = SPEED_UNKNOWN;
  755. break;
  756. }
  757. if (phydev->speed != SPEED_UNKNOWN)
  758. stmmac_hw_fix_mac_speed(priv);
  759. priv->speed = phydev->speed;
  760. }
  761. writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
  762. if (!priv->oldlink) {
  763. new_state = true;
  764. priv->oldlink = true;
  765. }
  766. } else if (priv->oldlink) {
  767. new_state = true;
  768. priv->oldlink = false;
  769. priv->speed = SPEED_UNKNOWN;
  770. priv->oldduplex = DUPLEX_UNKNOWN;
  771. }
  772. if (new_state && netif_msg_link(priv))
  773. phy_print_status(phydev);
  774. mutex_unlock(&priv->lock);
  775. if (phydev->is_pseudo_fixed_link)
  776. /* Stop PHY layer to call the hook to adjust the link in case
  777. * of a switch is attached to the stmmac driver.
  778. */
  779. phydev->irq = PHY_IGNORE_INTERRUPT;
  780. else
  781. /* At this stage, init the EEE if supported.
  782. * Never called in case of fixed_link.
  783. */
  784. priv->eee_enabled = stmmac_eee_init(priv);
  785. }
  786. /**
  787. * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
  788. * @priv: driver private structure
  789. * Description: this is to verify if the HW supports the PCS.
  790. * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
  791. * configured for the TBI, RTBI, or SGMII PHY interface.
  792. */
  793. static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
  794. {
  795. int interface = priv->plat->interface;
  796. if (priv->dma_cap.pcs) {
  797. if ((interface == PHY_INTERFACE_MODE_RGMII) ||
  798. (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
  799. (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
  800. (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
  801. netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
  802. priv->hw->pcs = STMMAC_PCS_RGMII;
  803. } else if (interface == PHY_INTERFACE_MODE_SGMII) {
  804. netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
  805. priv->hw->pcs = STMMAC_PCS_SGMII;
  806. }
  807. }
  808. }
  809. /**
  810. * stmmac_init_phy - PHY initialization
  811. * @dev: net device structure
  812. * Description: it initializes the driver's PHY state, and attaches the PHY
  813. * to the mac driver.
  814. * Return value:
  815. * 0 on success
  816. */
  817. static int stmmac_init_phy(struct net_device *dev)
  818. {
  819. struct stmmac_priv *priv = netdev_priv(dev);
  820. u32 tx_cnt = priv->plat->tx_queues_to_use;
  821. struct phy_device *phydev;
  822. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  823. char bus_id[MII_BUS_ID_SIZE];
  824. int interface = priv->plat->interface;
  825. int max_speed = priv->plat->max_speed;
  826. priv->oldlink = false;
  827. priv->speed = SPEED_UNKNOWN;
  828. priv->oldduplex = DUPLEX_UNKNOWN;
  829. if (priv->plat->phy_node) {
  830. phydev = of_phy_connect(dev, priv->plat->phy_node,
  831. &stmmac_adjust_link, 0, interface);
  832. } else {
  833. snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
  834. priv->plat->bus_id);
  835. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  836. priv->plat->phy_addr);
  837. netdev_dbg(priv->dev, "%s: trying to attach to %s\n", __func__,
  838. phy_id_fmt);
  839. phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
  840. interface);
  841. }
  842. if (IS_ERR_OR_NULL(phydev)) {
  843. netdev_err(priv->dev, "Could not attach to PHY\n");
  844. if (!phydev)
  845. return -ENODEV;
  846. return PTR_ERR(phydev);
  847. }
  848. /* Stop Advertising 1000BASE Capability if interface is not GMII */
  849. if ((interface == PHY_INTERFACE_MODE_MII) ||
  850. (interface == PHY_INTERFACE_MODE_RMII) ||
  851. (max_speed < 1000 && max_speed > 0))
  852. phy_set_max_speed(phydev, SPEED_100);
  853. /*
  854. * Half-duplex mode not supported with multiqueue
  855. * half-duplex can only works with single queue
  856. */
  857. if (tx_cnt > 1) {
  858. phy_remove_link_mode(phydev,
  859. ETHTOOL_LINK_MODE_10baseT_Half_BIT);
  860. phy_remove_link_mode(phydev,
  861. ETHTOOL_LINK_MODE_100baseT_Half_BIT);
  862. phy_remove_link_mode(phydev,
  863. ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
  864. }
  865. /*
  866. * Broken HW is sometimes missing the pull-up resistor on the
  867. * MDIO line, which results in reads to non-existent devices returning
  868. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  869. * device as well.
  870. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  871. */
  872. if (!priv->plat->phy_node && phydev->phy_id == 0) {
  873. phy_disconnect(phydev);
  874. return -ENODEV;
  875. }
  876. /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
  877. * subsequent PHY polling, make sure we force a link transition if
  878. * we have a UP/DOWN/UP transition
  879. */
  880. if (phydev->is_pseudo_fixed_link)
  881. phydev->irq = PHY_POLL;
  882. phy_attached_info(phydev);
  883. return 0;
  884. }
  885. static void stmmac_display_rx_rings(struct stmmac_priv *priv)
  886. {
  887. u32 rx_cnt = priv->plat->rx_queues_to_use;
  888. void *head_rx;
  889. u32 queue;
  890. /* Display RX rings */
  891. for (queue = 0; queue < rx_cnt; queue++) {
  892. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  893. pr_info("\tRX Queue %u rings\n", queue);
  894. if (priv->extend_desc)
  895. head_rx = (void *)rx_q->dma_erx;
  896. else
  897. head_rx = (void *)rx_q->dma_rx;
  898. /* Display RX ring */
  899. stmmac_display_ring(priv, head_rx, DMA_RX_SIZE, true);
  900. }
  901. }
  902. static void stmmac_display_tx_rings(struct stmmac_priv *priv)
  903. {
  904. u32 tx_cnt = priv->plat->tx_queues_to_use;
  905. void *head_tx;
  906. u32 queue;
  907. /* Display TX rings */
  908. for (queue = 0; queue < tx_cnt; queue++) {
  909. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  910. pr_info("\tTX Queue %d rings\n", queue);
  911. if (priv->extend_desc)
  912. head_tx = (void *)tx_q->dma_etx;
  913. else
  914. head_tx = (void *)tx_q->dma_tx;
  915. stmmac_display_ring(priv, head_tx, DMA_TX_SIZE, false);
  916. }
  917. }
  918. static void stmmac_display_rings(struct stmmac_priv *priv)
  919. {
  920. /* Display RX ring */
  921. stmmac_display_rx_rings(priv);
  922. /* Display TX ring */
  923. stmmac_display_tx_rings(priv);
  924. }
  925. static int stmmac_set_bfsize(int mtu, int bufsize)
  926. {
  927. int ret = bufsize;
  928. if (mtu >= BUF_SIZE_4KiB)
  929. ret = BUF_SIZE_8KiB;
  930. else if (mtu >= BUF_SIZE_2KiB)
  931. ret = BUF_SIZE_4KiB;
  932. else if (mtu > DEFAULT_BUFSIZE)
  933. ret = BUF_SIZE_2KiB;
  934. else
  935. ret = DEFAULT_BUFSIZE;
  936. return ret;
  937. }
  938. /**
  939. * stmmac_clear_rx_descriptors - clear RX descriptors
  940. * @priv: driver private structure
  941. * @queue: RX queue index
  942. * Description: this function is called to clear the RX descriptors
  943. * in case of both basic and extended descriptors are used.
  944. */
  945. static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
  946. {
  947. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  948. int i;
  949. /* Clear the RX descriptors */
  950. for (i = 0; i < DMA_RX_SIZE; i++)
  951. if (priv->extend_desc)
  952. stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
  953. priv->use_riwt, priv->mode,
  954. (i == DMA_RX_SIZE - 1));
  955. else
  956. stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
  957. priv->use_riwt, priv->mode,
  958. (i == DMA_RX_SIZE - 1));
  959. }
  960. /**
  961. * stmmac_clear_tx_descriptors - clear tx descriptors
  962. * @priv: driver private structure
  963. * @queue: TX queue index.
  964. * Description: this function is called to clear the TX descriptors
  965. * in case of both basic and extended descriptors are used.
  966. */
  967. static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
  968. {
  969. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  970. int i;
  971. /* Clear the TX descriptors */
  972. for (i = 0; i < DMA_TX_SIZE; i++)
  973. if (priv->extend_desc)
  974. stmmac_init_tx_desc(priv, &tx_q->dma_etx[i].basic,
  975. priv->mode, (i == DMA_TX_SIZE - 1));
  976. else
  977. stmmac_init_tx_desc(priv, &tx_q->dma_tx[i],
  978. priv->mode, (i == DMA_TX_SIZE - 1));
  979. }
  980. /**
  981. * stmmac_clear_descriptors - clear descriptors
  982. * @priv: driver private structure
  983. * Description: this function is called to clear the TX and RX descriptors
  984. * in case of both basic and extended descriptors are used.
  985. */
  986. static void stmmac_clear_descriptors(struct stmmac_priv *priv)
  987. {
  988. u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
  989. u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
  990. u32 queue;
  991. /* Clear the RX descriptors */
  992. for (queue = 0; queue < rx_queue_cnt; queue++)
  993. stmmac_clear_rx_descriptors(priv, queue);
  994. /* Clear the TX descriptors */
  995. for (queue = 0; queue < tx_queue_cnt; queue++)
  996. stmmac_clear_tx_descriptors(priv, queue);
  997. }
  998. /**
  999. * stmmac_init_rx_buffers - init the RX descriptor buffer.
  1000. * @priv: driver private structure
  1001. * @p: descriptor pointer
  1002. * @i: descriptor index
  1003. * @flags: gfp flag
  1004. * @queue: RX queue index
  1005. * Description: this function is called to allocate a receive buffer, perform
  1006. * the DMA mapping and init the descriptor.
  1007. */
  1008. static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
  1009. int i, gfp_t flags, u32 queue)
  1010. {
  1011. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1012. struct sk_buff *skb;
  1013. skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
  1014. if (!skb) {
  1015. netdev_err(priv->dev,
  1016. "%s: Rx init fails; skb is NULL\n", __func__);
  1017. return -ENOMEM;
  1018. }
  1019. rx_q->rx_skbuff[i] = skb;
  1020. rx_q->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  1021. priv->dma_buf_sz,
  1022. DMA_FROM_DEVICE);
  1023. if (dma_mapping_error(priv->device, rx_q->rx_skbuff_dma[i])) {
  1024. netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
  1025. dev_kfree_skb_any(skb);
  1026. return -EINVAL;
  1027. }
  1028. stmmac_set_desc_addr(priv, p, rx_q->rx_skbuff_dma[i]);
  1029. if (priv->dma_buf_sz == BUF_SIZE_16KiB)
  1030. stmmac_init_desc3(priv, p);
  1031. return 0;
  1032. }
  1033. /**
  1034. * stmmac_free_rx_buffer - free RX dma buffers
  1035. * @priv: private structure
  1036. * @queue: RX queue index
  1037. * @i: buffer index.
  1038. */
  1039. static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
  1040. {
  1041. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1042. if (rx_q->rx_skbuff[i]) {
  1043. dma_unmap_single(priv->device, rx_q->rx_skbuff_dma[i],
  1044. priv->dma_buf_sz, DMA_FROM_DEVICE);
  1045. dev_kfree_skb_any(rx_q->rx_skbuff[i]);
  1046. }
  1047. rx_q->rx_skbuff[i] = NULL;
  1048. }
  1049. /**
  1050. * stmmac_free_tx_buffer - free RX dma buffers
  1051. * @priv: private structure
  1052. * @queue: RX queue index
  1053. * @i: buffer index.
  1054. */
  1055. static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
  1056. {
  1057. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1058. if (tx_q->tx_skbuff_dma[i].buf) {
  1059. if (tx_q->tx_skbuff_dma[i].map_as_page)
  1060. dma_unmap_page(priv->device,
  1061. tx_q->tx_skbuff_dma[i].buf,
  1062. tx_q->tx_skbuff_dma[i].len,
  1063. DMA_TO_DEVICE);
  1064. else
  1065. dma_unmap_single(priv->device,
  1066. tx_q->tx_skbuff_dma[i].buf,
  1067. tx_q->tx_skbuff_dma[i].len,
  1068. DMA_TO_DEVICE);
  1069. }
  1070. if (tx_q->tx_skbuff[i]) {
  1071. dev_kfree_skb_any(tx_q->tx_skbuff[i]);
  1072. tx_q->tx_skbuff[i] = NULL;
  1073. tx_q->tx_skbuff_dma[i].buf = 0;
  1074. tx_q->tx_skbuff_dma[i].map_as_page = false;
  1075. }
  1076. }
  1077. /**
  1078. * init_dma_rx_desc_rings - init the RX descriptor rings
  1079. * @dev: net device structure
  1080. * @flags: gfp flag.
  1081. * Description: this function initializes the DMA RX descriptors
  1082. * and allocates the socket buffers. It supports the chained and ring
  1083. * modes.
  1084. */
  1085. static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
  1086. {
  1087. struct stmmac_priv *priv = netdev_priv(dev);
  1088. u32 rx_count = priv->plat->rx_queues_to_use;
  1089. int ret = -ENOMEM;
  1090. int bfsize = 0;
  1091. int queue;
  1092. int i;
  1093. bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
  1094. if (bfsize < 0)
  1095. bfsize = 0;
  1096. if (bfsize < BUF_SIZE_16KiB)
  1097. bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
  1098. priv->dma_buf_sz = bfsize;
  1099. /* RX INITIALIZATION */
  1100. netif_dbg(priv, probe, priv->dev,
  1101. "SKB addresses:\nskb\t\tskb data\tdma data\n");
  1102. for (queue = 0; queue < rx_count; queue++) {
  1103. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1104. netif_dbg(priv, probe, priv->dev,
  1105. "(%s) dma_rx_phy=0x%08x\n", __func__,
  1106. (u32)rx_q->dma_rx_phy);
  1107. for (i = 0; i < DMA_RX_SIZE; i++) {
  1108. struct dma_desc *p;
  1109. if (priv->extend_desc)
  1110. p = &((rx_q->dma_erx + i)->basic);
  1111. else
  1112. p = rx_q->dma_rx + i;
  1113. ret = stmmac_init_rx_buffers(priv, p, i, flags,
  1114. queue);
  1115. if (ret)
  1116. goto err_init_rx_buffers;
  1117. netif_dbg(priv, probe, priv->dev, "[%p]\t[%p]\t[%x]\n",
  1118. rx_q->rx_skbuff[i], rx_q->rx_skbuff[i]->data,
  1119. (unsigned int)rx_q->rx_skbuff_dma[i]);
  1120. }
  1121. rx_q->cur_rx = 0;
  1122. rx_q->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
  1123. stmmac_clear_rx_descriptors(priv, queue);
  1124. /* Setup the chained descriptor addresses */
  1125. if (priv->mode == STMMAC_CHAIN_MODE) {
  1126. if (priv->extend_desc)
  1127. stmmac_mode_init(priv, rx_q->dma_erx,
  1128. rx_q->dma_rx_phy, DMA_RX_SIZE, 1);
  1129. else
  1130. stmmac_mode_init(priv, rx_q->dma_rx,
  1131. rx_q->dma_rx_phy, DMA_RX_SIZE, 0);
  1132. }
  1133. }
  1134. buf_sz = bfsize;
  1135. return 0;
  1136. err_init_rx_buffers:
  1137. while (queue >= 0) {
  1138. while (--i >= 0)
  1139. stmmac_free_rx_buffer(priv, queue, i);
  1140. if (queue == 0)
  1141. break;
  1142. i = DMA_RX_SIZE;
  1143. queue--;
  1144. }
  1145. return ret;
  1146. }
  1147. /**
  1148. * init_dma_tx_desc_rings - init the TX descriptor rings
  1149. * @dev: net device structure.
  1150. * Description: this function initializes the DMA TX descriptors
  1151. * and allocates the socket buffers. It supports the chained and ring
  1152. * modes.
  1153. */
  1154. static int init_dma_tx_desc_rings(struct net_device *dev)
  1155. {
  1156. struct stmmac_priv *priv = netdev_priv(dev);
  1157. u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
  1158. u32 queue;
  1159. int i;
  1160. for (queue = 0; queue < tx_queue_cnt; queue++) {
  1161. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1162. netif_dbg(priv, probe, priv->dev,
  1163. "(%s) dma_tx_phy=0x%08x\n", __func__,
  1164. (u32)tx_q->dma_tx_phy);
  1165. /* Setup the chained descriptor addresses */
  1166. if (priv->mode == STMMAC_CHAIN_MODE) {
  1167. if (priv->extend_desc)
  1168. stmmac_mode_init(priv, tx_q->dma_etx,
  1169. tx_q->dma_tx_phy, DMA_TX_SIZE, 1);
  1170. else
  1171. stmmac_mode_init(priv, tx_q->dma_tx,
  1172. tx_q->dma_tx_phy, DMA_TX_SIZE, 0);
  1173. }
  1174. for (i = 0; i < DMA_TX_SIZE; i++) {
  1175. struct dma_desc *p;
  1176. if (priv->extend_desc)
  1177. p = &((tx_q->dma_etx + i)->basic);
  1178. else
  1179. p = tx_q->dma_tx + i;
  1180. stmmac_clear_desc(priv, p);
  1181. tx_q->tx_skbuff_dma[i].buf = 0;
  1182. tx_q->tx_skbuff_dma[i].map_as_page = false;
  1183. tx_q->tx_skbuff_dma[i].len = 0;
  1184. tx_q->tx_skbuff_dma[i].last_segment = false;
  1185. tx_q->tx_skbuff[i] = NULL;
  1186. }
  1187. tx_q->dirty_tx = 0;
  1188. tx_q->cur_tx = 0;
  1189. tx_q->mss = 0;
  1190. netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
  1191. }
  1192. return 0;
  1193. }
  1194. /**
  1195. * init_dma_desc_rings - init the RX/TX descriptor rings
  1196. * @dev: net device structure
  1197. * @flags: gfp flag.
  1198. * Description: this function initializes the DMA RX/TX descriptors
  1199. * and allocates the socket buffers. It supports the chained and ring
  1200. * modes.
  1201. */
  1202. static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
  1203. {
  1204. struct stmmac_priv *priv = netdev_priv(dev);
  1205. int ret;
  1206. ret = init_dma_rx_desc_rings(dev, flags);
  1207. if (ret)
  1208. return ret;
  1209. ret = init_dma_tx_desc_rings(dev);
  1210. stmmac_clear_descriptors(priv);
  1211. if (netif_msg_hw(priv))
  1212. stmmac_display_rings(priv);
  1213. return ret;
  1214. }
  1215. /**
  1216. * dma_free_rx_skbufs - free RX dma buffers
  1217. * @priv: private structure
  1218. * @queue: RX queue index
  1219. */
  1220. static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
  1221. {
  1222. int i;
  1223. for (i = 0; i < DMA_RX_SIZE; i++)
  1224. stmmac_free_rx_buffer(priv, queue, i);
  1225. }
  1226. /**
  1227. * dma_free_tx_skbufs - free TX dma buffers
  1228. * @priv: private structure
  1229. * @queue: TX queue index
  1230. */
  1231. static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
  1232. {
  1233. int i;
  1234. for (i = 0; i < DMA_TX_SIZE; i++)
  1235. stmmac_free_tx_buffer(priv, queue, i);
  1236. }
  1237. /**
  1238. * free_dma_rx_desc_resources - free RX dma desc resources
  1239. * @priv: private structure
  1240. */
  1241. static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
  1242. {
  1243. u32 rx_count = priv->plat->rx_queues_to_use;
  1244. u32 queue;
  1245. /* Free RX queue resources */
  1246. for (queue = 0; queue < rx_count; queue++) {
  1247. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1248. /* Release the DMA RX socket buffers */
  1249. dma_free_rx_skbufs(priv, queue);
  1250. /* Free DMA regions of consistent memory previously allocated */
  1251. if (!priv->extend_desc)
  1252. dma_free_coherent(priv->device,
  1253. DMA_RX_SIZE * sizeof(struct dma_desc),
  1254. rx_q->dma_rx, rx_q->dma_rx_phy);
  1255. else
  1256. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1257. sizeof(struct dma_extended_desc),
  1258. rx_q->dma_erx, rx_q->dma_rx_phy);
  1259. kfree(rx_q->rx_skbuff_dma);
  1260. kfree(rx_q->rx_skbuff);
  1261. }
  1262. }
  1263. /**
  1264. * free_dma_tx_desc_resources - free TX dma desc resources
  1265. * @priv: private structure
  1266. */
  1267. static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
  1268. {
  1269. u32 tx_count = priv->plat->tx_queues_to_use;
  1270. u32 queue;
  1271. /* Free TX queue resources */
  1272. for (queue = 0; queue < tx_count; queue++) {
  1273. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1274. /* Release the DMA TX socket buffers */
  1275. dma_free_tx_skbufs(priv, queue);
  1276. /* Free DMA regions of consistent memory previously allocated */
  1277. if (!priv->extend_desc)
  1278. dma_free_coherent(priv->device,
  1279. DMA_TX_SIZE * sizeof(struct dma_desc),
  1280. tx_q->dma_tx, tx_q->dma_tx_phy);
  1281. else
  1282. dma_free_coherent(priv->device, DMA_TX_SIZE *
  1283. sizeof(struct dma_extended_desc),
  1284. tx_q->dma_etx, tx_q->dma_tx_phy);
  1285. kfree(tx_q->tx_skbuff_dma);
  1286. kfree(tx_q->tx_skbuff);
  1287. }
  1288. }
  1289. /**
  1290. * alloc_dma_rx_desc_resources - alloc RX resources.
  1291. * @priv: private structure
  1292. * Description: according to which descriptor can be used (extend or basic)
  1293. * this function allocates the resources for TX and RX paths. In case of
  1294. * reception, for example, it pre-allocated the RX socket buffer in order to
  1295. * allow zero-copy mechanism.
  1296. */
  1297. static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
  1298. {
  1299. u32 rx_count = priv->plat->rx_queues_to_use;
  1300. int ret = -ENOMEM;
  1301. u32 queue;
  1302. /* RX queues buffers and DMA */
  1303. for (queue = 0; queue < rx_count; queue++) {
  1304. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1305. rx_q->queue_index = queue;
  1306. rx_q->priv_data = priv;
  1307. rx_q->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE,
  1308. sizeof(dma_addr_t),
  1309. GFP_KERNEL);
  1310. if (!rx_q->rx_skbuff_dma)
  1311. goto err_dma;
  1312. rx_q->rx_skbuff = kmalloc_array(DMA_RX_SIZE,
  1313. sizeof(struct sk_buff *),
  1314. GFP_KERNEL);
  1315. if (!rx_q->rx_skbuff)
  1316. goto err_dma;
  1317. if (priv->extend_desc) {
  1318. rx_q->dma_erx = dma_zalloc_coherent(priv->device,
  1319. DMA_RX_SIZE *
  1320. sizeof(struct
  1321. dma_extended_desc),
  1322. &rx_q->dma_rx_phy,
  1323. GFP_KERNEL);
  1324. if (!rx_q->dma_erx)
  1325. goto err_dma;
  1326. } else {
  1327. rx_q->dma_rx = dma_zalloc_coherent(priv->device,
  1328. DMA_RX_SIZE *
  1329. sizeof(struct
  1330. dma_desc),
  1331. &rx_q->dma_rx_phy,
  1332. GFP_KERNEL);
  1333. if (!rx_q->dma_rx)
  1334. goto err_dma;
  1335. }
  1336. }
  1337. return 0;
  1338. err_dma:
  1339. free_dma_rx_desc_resources(priv);
  1340. return ret;
  1341. }
  1342. /**
  1343. * alloc_dma_tx_desc_resources - alloc TX resources.
  1344. * @priv: private structure
  1345. * Description: according to which descriptor can be used (extend or basic)
  1346. * this function allocates the resources for TX and RX paths. In case of
  1347. * reception, for example, it pre-allocated the RX socket buffer in order to
  1348. * allow zero-copy mechanism.
  1349. */
  1350. static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
  1351. {
  1352. u32 tx_count = priv->plat->tx_queues_to_use;
  1353. int ret = -ENOMEM;
  1354. u32 queue;
  1355. /* TX queues buffers and DMA */
  1356. for (queue = 0; queue < tx_count; queue++) {
  1357. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1358. tx_q->queue_index = queue;
  1359. tx_q->priv_data = priv;
  1360. tx_q->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
  1361. sizeof(*tx_q->tx_skbuff_dma),
  1362. GFP_KERNEL);
  1363. if (!tx_q->tx_skbuff_dma)
  1364. goto err_dma;
  1365. tx_q->tx_skbuff = kmalloc_array(DMA_TX_SIZE,
  1366. sizeof(struct sk_buff *),
  1367. GFP_KERNEL);
  1368. if (!tx_q->tx_skbuff)
  1369. goto err_dma;
  1370. if (priv->extend_desc) {
  1371. tx_q->dma_etx = dma_zalloc_coherent(priv->device,
  1372. DMA_TX_SIZE *
  1373. sizeof(struct
  1374. dma_extended_desc),
  1375. &tx_q->dma_tx_phy,
  1376. GFP_KERNEL);
  1377. if (!tx_q->dma_etx)
  1378. goto err_dma;
  1379. } else {
  1380. tx_q->dma_tx = dma_zalloc_coherent(priv->device,
  1381. DMA_TX_SIZE *
  1382. sizeof(struct
  1383. dma_desc),
  1384. &tx_q->dma_tx_phy,
  1385. GFP_KERNEL);
  1386. if (!tx_q->dma_tx)
  1387. goto err_dma;
  1388. }
  1389. }
  1390. return 0;
  1391. err_dma:
  1392. free_dma_tx_desc_resources(priv);
  1393. return ret;
  1394. }
  1395. /**
  1396. * alloc_dma_desc_resources - alloc TX/RX resources.
  1397. * @priv: private structure
  1398. * Description: according to which descriptor can be used (extend or basic)
  1399. * this function allocates the resources for TX and RX paths. In case of
  1400. * reception, for example, it pre-allocated the RX socket buffer in order to
  1401. * allow zero-copy mechanism.
  1402. */
  1403. static int alloc_dma_desc_resources(struct stmmac_priv *priv)
  1404. {
  1405. /* RX Allocation */
  1406. int ret = alloc_dma_rx_desc_resources(priv);
  1407. if (ret)
  1408. return ret;
  1409. ret = alloc_dma_tx_desc_resources(priv);
  1410. return ret;
  1411. }
  1412. /**
  1413. * free_dma_desc_resources - free dma desc resources
  1414. * @priv: private structure
  1415. */
  1416. static void free_dma_desc_resources(struct stmmac_priv *priv)
  1417. {
  1418. /* Release the DMA RX socket buffers */
  1419. free_dma_rx_desc_resources(priv);
  1420. /* Release the DMA TX socket buffers */
  1421. free_dma_tx_desc_resources(priv);
  1422. }
  1423. /**
  1424. * stmmac_mac_enable_rx_queues - Enable MAC rx queues
  1425. * @priv: driver private structure
  1426. * Description: It is used for enabling the rx queues in the MAC
  1427. */
  1428. static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
  1429. {
  1430. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  1431. int queue;
  1432. u8 mode;
  1433. for (queue = 0; queue < rx_queues_count; queue++) {
  1434. mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
  1435. stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
  1436. }
  1437. }
  1438. /**
  1439. * stmmac_start_rx_dma - start RX DMA channel
  1440. * @priv: driver private structure
  1441. * @chan: RX channel index
  1442. * Description:
  1443. * This starts a RX DMA channel
  1444. */
  1445. static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
  1446. {
  1447. netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
  1448. stmmac_start_rx(priv, priv->ioaddr, chan);
  1449. }
  1450. /**
  1451. * stmmac_start_tx_dma - start TX DMA channel
  1452. * @priv: driver private structure
  1453. * @chan: TX channel index
  1454. * Description:
  1455. * This starts a TX DMA channel
  1456. */
  1457. static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
  1458. {
  1459. netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
  1460. stmmac_start_tx(priv, priv->ioaddr, chan);
  1461. }
  1462. /**
  1463. * stmmac_stop_rx_dma - stop RX DMA channel
  1464. * @priv: driver private structure
  1465. * @chan: RX channel index
  1466. * Description:
  1467. * This stops a RX DMA channel
  1468. */
  1469. static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
  1470. {
  1471. netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
  1472. stmmac_stop_rx(priv, priv->ioaddr, chan);
  1473. }
  1474. /**
  1475. * stmmac_stop_tx_dma - stop TX DMA channel
  1476. * @priv: driver private structure
  1477. * @chan: TX channel index
  1478. * Description:
  1479. * This stops a TX DMA channel
  1480. */
  1481. static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
  1482. {
  1483. netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
  1484. stmmac_stop_tx(priv, priv->ioaddr, chan);
  1485. }
  1486. /**
  1487. * stmmac_start_all_dma - start all RX and TX DMA channels
  1488. * @priv: driver private structure
  1489. * Description:
  1490. * This starts all the RX and TX DMA channels
  1491. */
  1492. static void stmmac_start_all_dma(struct stmmac_priv *priv)
  1493. {
  1494. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1495. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1496. u32 chan = 0;
  1497. for (chan = 0; chan < rx_channels_count; chan++)
  1498. stmmac_start_rx_dma(priv, chan);
  1499. for (chan = 0; chan < tx_channels_count; chan++)
  1500. stmmac_start_tx_dma(priv, chan);
  1501. }
  1502. /**
  1503. * stmmac_stop_all_dma - stop all RX and TX DMA channels
  1504. * @priv: driver private structure
  1505. * Description:
  1506. * This stops the RX and TX DMA channels
  1507. */
  1508. static void stmmac_stop_all_dma(struct stmmac_priv *priv)
  1509. {
  1510. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1511. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1512. u32 chan = 0;
  1513. for (chan = 0; chan < rx_channels_count; chan++)
  1514. stmmac_stop_rx_dma(priv, chan);
  1515. for (chan = 0; chan < tx_channels_count; chan++)
  1516. stmmac_stop_tx_dma(priv, chan);
  1517. }
  1518. /**
  1519. * stmmac_dma_operation_mode - HW DMA operation mode
  1520. * @priv: driver private structure
  1521. * Description: it is used for configuring the DMA operation mode register in
  1522. * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
  1523. */
  1524. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  1525. {
  1526. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1527. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1528. int rxfifosz = priv->plat->rx_fifo_size;
  1529. int txfifosz = priv->plat->tx_fifo_size;
  1530. u32 txmode = 0;
  1531. u32 rxmode = 0;
  1532. u32 chan = 0;
  1533. u8 qmode = 0;
  1534. if (rxfifosz == 0)
  1535. rxfifosz = priv->dma_cap.rx_fifo_size;
  1536. if (txfifosz == 0)
  1537. txfifosz = priv->dma_cap.tx_fifo_size;
  1538. /* Adjust for real per queue fifo size */
  1539. rxfifosz /= rx_channels_count;
  1540. txfifosz /= tx_channels_count;
  1541. if (priv->plat->force_thresh_dma_mode) {
  1542. txmode = tc;
  1543. rxmode = tc;
  1544. } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
  1545. /*
  1546. * In case of GMAC, SF mode can be enabled
  1547. * to perform the TX COE in HW. This depends on:
  1548. * 1) TX COE if actually supported
  1549. * 2) There is no bugged Jumbo frame support
  1550. * that needs to not insert csum in the TDES.
  1551. */
  1552. txmode = SF_DMA_MODE;
  1553. rxmode = SF_DMA_MODE;
  1554. priv->xstats.threshold = SF_DMA_MODE;
  1555. } else {
  1556. txmode = tc;
  1557. rxmode = SF_DMA_MODE;
  1558. }
  1559. /* configure all channels */
  1560. for (chan = 0; chan < rx_channels_count; chan++) {
  1561. qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
  1562. stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
  1563. rxfifosz, qmode);
  1564. stmmac_set_dma_bfsize(priv, priv->ioaddr, priv->dma_buf_sz,
  1565. chan);
  1566. }
  1567. for (chan = 0; chan < tx_channels_count; chan++) {
  1568. qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
  1569. stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
  1570. txfifosz, qmode);
  1571. }
  1572. }
  1573. /**
  1574. * stmmac_tx_clean - to manage the transmission completion
  1575. * @priv: driver private structure
  1576. * @queue: TX queue index
  1577. * Description: it reclaims the transmit resources after transmission completes.
  1578. */
  1579. static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
  1580. {
  1581. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1582. unsigned int bytes_compl = 0, pkts_compl = 0;
  1583. unsigned int entry, count = 0;
  1584. __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
  1585. priv->xstats.tx_clean++;
  1586. entry = tx_q->dirty_tx;
  1587. while ((entry != tx_q->cur_tx) && (count < budget)) {
  1588. struct sk_buff *skb = tx_q->tx_skbuff[entry];
  1589. struct dma_desc *p;
  1590. int status;
  1591. if (priv->extend_desc)
  1592. p = (struct dma_desc *)(tx_q->dma_etx + entry);
  1593. else
  1594. p = tx_q->dma_tx + entry;
  1595. status = stmmac_tx_status(priv, &priv->dev->stats,
  1596. &priv->xstats, p, priv->ioaddr);
  1597. /* Check if the descriptor is owned by the DMA */
  1598. if (unlikely(status & tx_dma_own))
  1599. break;
  1600. count++;
  1601. /* Make sure descriptor fields are read after reading
  1602. * the own bit.
  1603. */
  1604. dma_rmb();
  1605. /* Just consider the last segment and ...*/
  1606. if (likely(!(status & tx_not_ls))) {
  1607. /* ... verify the status error condition */
  1608. if (unlikely(status & tx_err)) {
  1609. priv->dev->stats.tx_errors++;
  1610. } else {
  1611. priv->dev->stats.tx_packets++;
  1612. priv->xstats.tx_pkt_n++;
  1613. }
  1614. stmmac_get_tx_hwtstamp(priv, p, skb);
  1615. }
  1616. if (likely(tx_q->tx_skbuff_dma[entry].buf)) {
  1617. if (tx_q->tx_skbuff_dma[entry].map_as_page)
  1618. dma_unmap_page(priv->device,
  1619. tx_q->tx_skbuff_dma[entry].buf,
  1620. tx_q->tx_skbuff_dma[entry].len,
  1621. DMA_TO_DEVICE);
  1622. else
  1623. dma_unmap_single(priv->device,
  1624. tx_q->tx_skbuff_dma[entry].buf,
  1625. tx_q->tx_skbuff_dma[entry].len,
  1626. DMA_TO_DEVICE);
  1627. tx_q->tx_skbuff_dma[entry].buf = 0;
  1628. tx_q->tx_skbuff_dma[entry].len = 0;
  1629. tx_q->tx_skbuff_dma[entry].map_as_page = false;
  1630. }
  1631. stmmac_clean_desc3(priv, tx_q, p);
  1632. tx_q->tx_skbuff_dma[entry].last_segment = false;
  1633. tx_q->tx_skbuff_dma[entry].is_jumbo = false;
  1634. if (likely(skb != NULL)) {
  1635. pkts_compl++;
  1636. bytes_compl += skb->len;
  1637. dev_consume_skb_any(skb);
  1638. tx_q->tx_skbuff[entry] = NULL;
  1639. }
  1640. stmmac_release_tx_desc(priv, p, priv->mode);
  1641. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1642. }
  1643. tx_q->dirty_tx = entry;
  1644. netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
  1645. pkts_compl, bytes_compl);
  1646. if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
  1647. queue))) &&
  1648. stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH) {
  1649. netif_dbg(priv, tx_done, priv->dev,
  1650. "%s: restart transmit\n", __func__);
  1651. netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
  1652. }
  1653. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  1654. stmmac_enable_eee_mode(priv);
  1655. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  1656. }
  1657. __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
  1658. return count;
  1659. }
  1660. /**
  1661. * stmmac_tx_err - to manage the tx error
  1662. * @priv: driver private structure
  1663. * @chan: channel index
  1664. * Description: it cleans the descriptors and restarts the transmission
  1665. * in case of transmission errors.
  1666. */
  1667. static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
  1668. {
  1669. struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
  1670. int i;
  1671. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
  1672. stmmac_stop_tx_dma(priv, chan);
  1673. dma_free_tx_skbufs(priv, chan);
  1674. for (i = 0; i < DMA_TX_SIZE; i++)
  1675. if (priv->extend_desc)
  1676. stmmac_init_tx_desc(priv, &tx_q->dma_etx[i].basic,
  1677. priv->mode, (i == DMA_TX_SIZE - 1));
  1678. else
  1679. stmmac_init_tx_desc(priv, &tx_q->dma_tx[i],
  1680. priv->mode, (i == DMA_TX_SIZE - 1));
  1681. tx_q->dirty_tx = 0;
  1682. tx_q->cur_tx = 0;
  1683. tx_q->mss = 0;
  1684. netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
  1685. stmmac_start_tx_dma(priv, chan);
  1686. priv->dev->stats.tx_errors++;
  1687. netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
  1688. }
  1689. /**
  1690. * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
  1691. * @priv: driver private structure
  1692. * @txmode: TX operating mode
  1693. * @rxmode: RX operating mode
  1694. * @chan: channel index
  1695. * Description: it is used for configuring of the DMA operation mode in
  1696. * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
  1697. * mode.
  1698. */
  1699. static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
  1700. u32 rxmode, u32 chan)
  1701. {
  1702. u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
  1703. u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
  1704. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1705. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1706. int rxfifosz = priv->plat->rx_fifo_size;
  1707. int txfifosz = priv->plat->tx_fifo_size;
  1708. if (rxfifosz == 0)
  1709. rxfifosz = priv->dma_cap.rx_fifo_size;
  1710. if (txfifosz == 0)
  1711. txfifosz = priv->dma_cap.tx_fifo_size;
  1712. /* Adjust for real per queue fifo size */
  1713. rxfifosz /= rx_channels_count;
  1714. txfifosz /= tx_channels_count;
  1715. stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
  1716. stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
  1717. }
  1718. static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
  1719. {
  1720. int ret;
  1721. ret = stmmac_safety_feat_irq_status(priv, priv->dev,
  1722. priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
  1723. if (ret && (ret != -EINVAL)) {
  1724. stmmac_global_err(priv);
  1725. return true;
  1726. }
  1727. return false;
  1728. }
  1729. static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan)
  1730. {
  1731. int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
  1732. &priv->xstats, chan);
  1733. struct stmmac_channel *ch = &priv->channel[chan];
  1734. bool needs_work = false;
  1735. if ((status & handle_rx) && ch->has_rx) {
  1736. needs_work = true;
  1737. } else {
  1738. status &= ~handle_rx;
  1739. }
  1740. if ((status & handle_tx) && ch->has_tx) {
  1741. needs_work = true;
  1742. } else {
  1743. status &= ~handle_tx;
  1744. }
  1745. if (needs_work && napi_schedule_prep(&ch->napi)) {
  1746. stmmac_disable_dma_irq(priv, priv->ioaddr, chan);
  1747. __napi_schedule(&ch->napi);
  1748. }
  1749. return status;
  1750. }
  1751. /**
  1752. * stmmac_dma_interrupt - DMA ISR
  1753. * @priv: driver private structure
  1754. * Description: this is the DMA ISR. It is called by the main ISR.
  1755. * It calls the dwmac dma routine and schedule poll method in case of some
  1756. * work can be done.
  1757. */
  1758. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  1759. {
  1760. u32 tx_channel_count = priv->plat->tx_queues_to_use;
  1761. u32 rx_channel_count = priv->plat->rx_queues_to_use;
  1762. u32 channels_to_check = tx_channel_count > rx_channel_count ?
  1763. tx_channel_count : rx_channel_count;
  1764. u32 chan;
  1765. int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
  1766. /* Make sure we never check beyond our status buffer. */
  1767. if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
  1768. channels_to_check = ARRAY_SIZE(status);
  1769. for (chan = 0; chan < channels_to_check; chan++)
  1770. status[chan] = stmmac_napi_check(priv, chan);
  1771. for (chan = 0; chan < tx_channel_count; chan++) {
  1772. if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
  1773. /* Try to bump up the dma threshold on this failure */
  1774. if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
  1775. (tc <= 256)) {
  1776. tc += 64;
  1777. if (priv->plat->force_thresh_dma_mode)
  1778. stmmac_set_dma_operation_mode(priv,
  1779. tc,
  1780. tc,
  1781. chan);
  1782. else
  1783. stmmac_set_dma_operation_mode(priv,
  1784. tc,
  1785. SF_DMA_MODE,
  1786. chan);
  1787. priv->xstats.threshold = tc;
  1788. }
  1789. } else if (unlikely(status[chan] == tx_hard_error)) {
  1790. stmmac_tx_err(priv, chan);
  1791. }
  1792. }
  1793. }
  1794. /**
  1795. * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
  1796. * @priv: driver private structure
  1797. * Description: this masks the MMC irq, in fact, the counters are managed in SW.
  1798. */
  1799. static void stmmac_mmc_setup(struct stmmac_priv *priv)
  1800. {
  1801. unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
  1802. MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
  1803. dwmac_mmc_intr_all_mask(priv->mmcaddr);
  1804. if (priv->dma_cap.rmon) {
  1805. dwmac_mmc_ctrl(priv->mmcaddr, mode);
  1806. memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
  1807. } else
  1808. netdev_info(priv->dev, "No MAC Management Counters available\n");
  1809. }
  1810. /**
  1811. * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
  1812. * @priv: driver private structure
  1813. * Description:
  1814. * new GMAC chip generations have a new register to indicate the
  1815. * presence of the optional feature/functions.
  1816. * This can be also used to override the value passed through the
  1817. * platform and necessary for old MAC10/100 and GMAC chips.
  1818. */
  1819. static int stmmac_get_hw_features(struct stmmac_priv *priv)
  1820. {
  1821. return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
  1822. }
  1823. /**
  1824. * stmmac_check_ether_addr - check if the MAC addr is valid
  1825. * @priv: driver private structure
  1826. * Description:
  1827. * it is to verify if the MAC address is valid, in case of failures it
  1828. * generates a random MAC address
  1829. */
  1830. static void stmmac_check_ether_addr(struct stmmac_priv *priv)
  1831. {
  1832. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  1833. stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0);
  1834. if (!is_valid_ether_addr(priv->dev->dev_addr))
  1835. eth_hw_addr_random(priv->dev);
  1836. netdev_info(priv->dev, "device MAC address %pM\n",
  1837. priv->dev->dev_addr);
  1838. }
  1839. }
  1840. /**
  1841. * stmmac_init_dma_engine - DMA init.
  1842. * @priv: driver private structure
  1843. * Description:
  1844. * It inits the DMA invoking the specific MAC/GMAC callback.
  1845. * Some DMA parameters can be passed from the platform;
  1846. * in case of these are not passed a default is kept for the MAC or GMAC.
  1847. */
  1848. static int stmmac_init_dma_engine(struct stmmac_priv *priv)
  1849. {
  1850. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1851. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1852. u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
  1853. struct stmmac_rx_queue *rx_q;
  1854. struct stmmac_tx_queue *tx_q;
  1855. u32 chan = 0;
  1856. int atds = 0;
  1857. int ret = 0;
  1858. if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
  1859. dev_err(priv->device, "Invalid DMA configuration\n");
  1860. return -EINVAL;
  1861. }
  1862. if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
  1863. atds = 1;
  1864. ret = stmmac_reset(priv, priv->ioaddr);
  1865. if (ret) {
  1866. dev_err(priv->device, "Failed to reset the dma\n");
  1867. return ret;
  1868. }
  1869. /* DMA Configuration */
  1870. stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
  1871. if (priv->plat->axi)
  1872. stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
  1873. /* DMA RX Channel Configuration */
  1874. for (chan = 0; chan < rx_channels_count; chan++) {
  1875. rx_q = &priv->rx_queue[chan];
  1876. stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
  1877. rx_q->dma_rx_phy, chan);
  1878. rx_q->rx_tail_addr = rx_q->dma_rx_phy +
  1879. (DMA_RX_SIZE * sizeof(struct dma_desc));
  1880. stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
  1881. rx_q->rx_tail_addr, chan);
  1882. }
  1883. /* DMA TX Channel Configuration */
  1884. for (chan = 0; chan < tx_channels_count; chan++) {
  1885. tx_q = &priv->tx_queue[chan];
  1886. stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
  1887. tx_q->dma_tx_phy, chan);
  1888. tx_q->tx_tail_addr = tx_q->dma_tx_phy;
  1889. stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
  1890. tx_q->tx_tail_addr, chan);
  1891. }
  1892. /* DMA CSR Channel configuration */
  1893. for (chan = 0; chan < dma_csr_ch; chan++)
  1894. stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
  1895. return ret;
  1896. }
  1897. static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
  1898. {
  1899. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1900. mod_timer(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1901. }
  1902. /**
  1903. * stmmac_tx_timer - mitigation sw timer for tx.
  1904. * @data: data pointer
  1905. * Description:
  1906. * This is the timer handler to directly invoke the stmmac_tx_clean.
  1907. */
  1908. static void stmmac_tx_timer(struct timer_list *t)
  1909. {
  1910. struct stmmac_tx_queue *tx_q = from_timer(tx_q, t, txtimer);
  1911. struct stmmac_priv *priv = tx_q->priv_data;
  1912. struct stmmac_channel *ch;
  1913. ch = &priv->channel[tx_q->queue_index];
  1914. if (likely(napi_schedule_prep(&ch->napi)))
  1915. __napi_schedule(&ch->napi);
  1916. }
  1917. /**
  1918. * stmmac_init_tx_coalesce - init tx mitigation options.
  1919. * @priv: driver private structure
  1920. * Description:
  1921. * This inits the transmit coalesce parameters: i.e. timer rate,
  1922. * timer handler and default threshold used for enabling the
  1923. * interrupt on completion bit.
  1924. */
  1925. static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
  1926. {
  1927. u32 tx_channel_count = priv->plat->tx_queues_to_use;
  1928. u32 chan;
  1929. priv->tx_coal_frames = STMMAC_TX_FRAMES;
  1930. priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
  1931. for (chan = 0; chan < tx_channel_count; chan++) {
  1932. struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
  1933. timer_setup(&tx_q->txtimer, stmmac_tx_timer, 0);
  1934. }
  1935. }
  1936. static void stmmac_set_rings_length(struct stmmac_priv *priv)
  1937. {
  1938. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1939. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1940. u32 chan;
  1941. /* set TX ring length */
  1942. for (chan = 0; chan < tx_channels_count; chan++)
  1943. stmmac_set_tx_ring_len(priv, priv->ioaddr,
  1944. (DMA_TX_SIZE - 1), chan);
  1945. /* set RX ring length */
  1946. for (chan = 0; chan < rx_channels_count; chan++)
  1947. stmmac_set_rx_ring_len(priv, priv->ioaddr,
  1948. (DMA_RX_SIZE - 1), chan);
  1949. }
  1950. /**
  1951. * stmmac_set_tx_queue_weight - Set TX queue weight
  1952. * @priv: driver private structure
  1953. * Description: It is used for setting TX queues weight
  1954. */
  1955. static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
  1956. {
  1957. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  1958. u32 weight;
  1959. u32 queue;
  1960. for (queue = 0; queue < tx_queues_count; queue++) {
  1961. weight = priv->plat->tx_queues_cfg[queue].weight;
  1962. stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
  1963. }
  1964. }
  1965. /**
  1966. * stmmac_configure_cbs - Configure CBS in TX queue
  1967. * @priv: driver private structure
  1968. * Description: It is used for configuring CBS in AVB TX queues
  1969. */
  1970. static void stmmac_configure_cbs(struct stmmac_priv *priv)
  1971. {
  1972. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  1973. u32 mode_to_use;
  1974. u32 queue;
  1975. /* queue 0 is reserved for legacy traffic */
  1976. for (queue = 1; queue < tx_queues_count; queue++) {
  1977. mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
  1978. if (mode_to_use == MTL_QUEUE_DCB)
  1979. continue;
  1980. stmmac_config_cbs(priv, priv->hw,
  1981. priv->plat->tx_queues_cfg[queue].send_slope,
  1982. priv->plat->tx_queues_cfg[queue].idle_slope,
  1983. priv->plat->tx_queues_cfg[queue].high_credit,
  1984. priv->plat->tx_queues_cfg[queue].low_credit,
  1985. queue);
  1986. }
  1987. }
  1988. /**
  1989. * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
  1990. * @priv: driver private structure
  1991. * Description: It is used for mapping RX queues to RX dma channels
  1992. */
  1993. static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
  1994. {
  1995. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  1996. u32 queue;
  1997. u32 chan;
  1998. for (queue = 0; queue < rx_queues_count; queue++) {
  1999. chan = priv->plat->rx_queues_cfg[queue].chan;
  2000. stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
  2001. }
  2002. }
  2003. /**
  2004. * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
  2005. * @priv: driver private structure
  2006. * Description: It is used for configuring the RX Queue Priority
  2007. */
  2008. static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
  2009. {
  2010. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  2011. u32 queue;
  2012. u32 prio;
  2013. for (queue = 0; queue < rx_queues_count; queue++) {
  2014. if (!priv->plat->rx_queues_cfg[queue].use_prio)
  2015. continue;
  2016. prio = priv->plat->rx_queues_cfg[queue].prio;
  2017. stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
  2018. }
  2019. }
  2020. /**
  2021. * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
  2022. * @priv: driver private structure
  2023. * Description: It is used for configuring the TX Queue Priority
  2024. */
  2025. static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
  2026. {
  2027. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  2028. u32 queue;
  2029. u32 prio;
  2030. for (queue = 0; queue < tx_queues_count; queue++) {
  2031. if (!priv->plat->tx_queues_cfg[queue].use_prio)
  2032. continue;
  2033. prio = priv->plat->tx_queues_cfg[queue].prio;
  2034. stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
  2035. }
  2036. }
  2037. /**
  2038. * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
  2039. * @priv: driver private structure
  2040. * Description: It is used for configuring the RX queue routing
  2041. */
  2042. static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
  2043. {
  2044. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  2045. u32 queue;
  2046. u8 packet;
  2047. for (queue = 0; queue < rx_queues_count; queue++) {
  2048. /* no specific packet type routing specified for the queue */
  2049. if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
  2050. continue;
  2051. packet = priv->plat->rx_queues_cfg[queue].pkt_route;
  2052. stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
  2053. }
  2054. }
  2055. /**
  2056. * stmmac_mtl_configuration - Configure MTL
  2057. * @priv: driver private structure
  2058. * Description: It is used for configurring MTL
  2059. */
  2060. static void stmmac_mtl_configuration(struct stmmac_priv *priv)
  2061. {
  2062. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  2063. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  2064. if (tx_queues_count > 1)
  2065. stmmac_set_tx_queue_weight(priv);
  2066. /* Configure MTL RX algorithms */
  2067. if (rx_queues_count > 1)
  2068. stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
  2069. priv->plat->rx_sched_algorithm);
  2070. /* Configure MTL TX algorithms */
  2071. if (tx_queues_count > 1)
  2072. stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
  2073. priv->plat->tx_sched_algorithm);
  2074. /* Configure CBS in AVB TX queues */
  2075. if (tx_queues_count > 1)
  2076. stmmac_configure_cbs(priv);
  2077. /* Map RX MTL to DMA channels */
  2078. stmmac_rx_queue_dma_chan_map(priv);
  2079. /* Enable MAC RX Queues */
  2080. stmmac_mac_enable_rx_queues(priv);
  2081. /* Set RX priorities */
  2082. if (rx_queues_count > 1)
  2083. stmmac_mac_config_rx_queues_prio(priv);
  2084. /* Set TX priorities */
  2085. if (tx_queues_count > 1)
  2086. stmmac_mac_config_tx_queues_prio(priv);
  2087. /* Set RX routing */
  2088. if (rx_queues_count > 1)
  2089. stmmac_mac_config_rx_queues_routing(priv);
  2090. }
  2091. static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
  2092. {
  2093. if (priv->dma_cap.asp) {
  2094. netdev_info(priv->dev, "Enabling Safety Features\n");
  2095. stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp);
  2096. } else {
  2097. netdev_info(priv->dev, "No Safety Features support found\n");
  2098. }
  2099. }
  2100. /**
  2101. * stmmac_hw_setup - setup mac in a usable state.
  2102. * @dev : pointer to the device structure.
  2103. * Description:
  2104. * this is the main function to setup the HW in a usable state because the
  2105. * dma engine is reset, the core registers are configured (e.g. AXI,
  2106. * Checksum features, timers). The DMA is ready to start receiving and
  2107. * transmitting.
  2108. * Return value:
  2109. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  2110. * file on failure.
  2111. */
  2112. static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
  2113. {
  2114. struct stmmac_priv *priv = netdev_priv(dev);
  2115. u32 rx_cnt = priv->plat->rx_queues_to_use;
  2116. u32 tx_cnt = priv->plat->tx_queues_to_use;
  2117. u32 chan;
  2118. int ret;
  2119. /* DMA initialization and SW reset */
  2120. ret = stmmac_init_dma_engine(priv);
  2121. if (ret < 0) {
  2122. netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
  2123. __func__);
  2124. return ret;
  2125. }
  2126. /* Copy the MAC addr into the HW */
  2127. stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
  2128. /* PS and related bits will be programmed according to the speed */
  2129. if (priv->hw->pcs) {
  2130. int speed = priv->plat->mac_port_sel_speed;
  2131. if ((speed == SPEED_10) || (speed == SPEED_100) ||
  2132. (speed == SPEED_1000)) {
  2133. priv->hw->ps = speed;
  2134. } else {
  2135. dev_warn(priv->device, "invalid port speed\n");
  2136. priv->hw->ps = 0;
  2137. }
  2138. }
  2139. /* Initialize the MAC Core */
  2140. stmmac_core_init(priv, priv->hw, dev);
  2141. /* Initialize MTL*/
  2142. stmmac_mtl_configuration(priv);
  2143. /* Initialize Safety Features */
  2144. stmmac_safety_feat_configuration(priv);
  2145. ret = stmmac_rx_ipc(priv, priv->hw);
  2146. if (!ret) {
  2147. netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
  2148. priv->plat->rx_coe = STMMAC_RX_COE_NONE;
  2149. priv->hw->rx_csum = 0;
  2150. }
  2151. /* Enable the MAC Rx/Tx */
  2152. stmmac_mac_set(priv, priv->ioaddr, true);
  2153. /* Set the HW DMA mode and the COE */
  2154. stmmac_dma_operation_mode(priv);
  2155. stmmac_mmc_setup(priv);
  2156. if (init_ptp) {
  2157. ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
  2158. if (ret < 0)
  2159. netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret);
  2160. ret = stmmac_init_ptp(priv);
  2161. if (ret == -EOPNOTSUPP)
  2162. netdev_warn(priv->dev, "PTP not supported by HW\n");
  2163. else if (ret)
  2164. netdev_warn(priv->dev, "PTP init failed\n");
  2165. }
  2166. priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
  2167. if (priv->use_riwt) {
  2168. ret = stmmac_rx_watchdog(priv, priv->ioaddr, MAX_DMA_RIWT, rx_cnt);
  2169. if (!ret)
  2170. priv->rx_riwt = MAX_DMA_RIWT;
  2171. }
  2172. if (priv->hw->pcs)
  2173. stmmac_pcs_ctrl_ane(priv, priv->hw, 1, priv->hw->ps, 0);
  2174. /* set TX and RX rings length */
  2175. stmmac_set_rings_length(priv);
  2176. /* Enable TSO */
  2177. if (priv->tso) {
  2178. for (chan = 0; chan < tx_cnt; chan++)
  2179. stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
  2180. }
  2181. /* Start the ball rolling... */
  2182. stmmac_start_all_dma(priv);
  2183. return 0;
  2184. }
  2185. static void stmmac_hw_teardown(struct net_device *dev)
  2186. {
  2187. struct stmmac_priv *priv = netdev_priv(dev);
  2188. clk_disable_unprepare(priv->plat->clk_ptp_ref);
  2189. }
  2190. /**
  2191. * stmmac_open - open entry point of the driver
  2192. * @dev : pointer to the device structure.
  2193. * Description:
  2194. * This function is the open entry point of the driver.
  2195. * Return value:
  2196. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  2197. * file on failure.
  2198. */
  2199. static int stmmac_open(struct net_device *dev)
  2200. {
  2201. struct stmmac_priv *priv = netdev_priv(dev);
  2202. u32 chan;
  2203. int ret;
  2204. stmmac_check_ether_addr(priv);
  2205. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  2206. priv->hw->pcs != STMMAC_PCS_TBI &&
  2207. priv->hw->pcs != STMMAC_PCS_RTBI) {
  2208. ret = stmmac_init_phy(dev);
  2209. if (ret) {
  2210. netdev_err(priv->dev,
  2211. "%s: Cannot attach to PHY (error: %d)\n",
  2212. __func__, ret);
  2213. return ret;
  2214. }
  2215. }
  2216. /* Extra statistics */
  2217. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  2218. priv->xstats.threshold = tc;
  2219. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  2220. priv->rx_copybreak = STMMAC_RX_COPYBREAK;
  2221. ret = alloc_dma_desc_resources(priv);
  2222. if (ret < 0) {
  2223. netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
  2224. __func__);
  2225. goto dma_desc_error;
  2226. }
  2227. ret = init_dma_desc_rings(dev, GFP_KERNEL);
  2228. if (ret < 0) {
  2229. netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
  2230. __func__);
  2231. goto init_error;
  2232. }
  2233. ret = stmmac_hw_setup(dev, true);
  2234. if (ret < 0) {
  2235. netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
  2236. goto init_error;
  2237. }
  2238. stmmac_init_tx_coalesce(priv);
  2239. if (dev->phydev)
  2240. phy_start(dev->phydev);
  2241. /* Request the IRQ lines */
  2242. ret = request_irq(dev->irq, stmmac_interrupt,
  2243. IRQF_SHARED, dev->name, dev);
  2244. if (unlikely(ret < 0)) {
  2245. netdev_err(priv->dev,
  2246. "%s: ERROR: allocating the IRQ %d (error: %d)\n",
  2247. __func__, dev->irq, ret);
  2248. goto irq_error;
  2249. }
  2250. /* Request the Wake IRQ in case of another line is used for WoL */
  2251. if (priv->wol_irq != dev->irq) {
  2252. ret = request_irq(priv->wol_irq, stmmac_interrupt,
  2253. IRQF_SHARED, dev->name, dev);
  2254. if (unlikely(ret < 0)) {
  2255. netdev_err(priv->dev,
  2256. "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
  2257. __func__, priv->wol_irq, ret);
  2258. goto wolirq_error;
  2259. }
  2260. }
  2261. /* Request the IRQ lines */
  2262. if (priv->lpi_irq > 0) {
  2263. ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
  2264. dev->name, dev);
  2265. if (unlikely(ret < 0)) {
  2266. netdev_err(priv->dev,
  2267. "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  2268. __func__, priv->lpi_irq, ret);
  2269. goto lpiirq_error;
  2270. }
  2271. }
  2272. stmmac_enable_all_queues(priv);
  2273. stmmac_start_all_queues(priv);
  2274. return 0;
  2275. lpiirq_error:
  2276. if (priv->wol_irq != dev->irq)
  2277. free_irq(priv->wol_irq, dev);
  2278. wolirq_error:
  2279. free_irq(dev->irq, dev);
  2280. irq_error:
  2281. if (dev->phydev)
  2282. phy_stop(dev->phydev);
  2283. for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
  2284. del_timer_sync(&priv->tx_queue[chan].txtimer);
  2285. stmmac_hw_teardown(dev);
  2286. init_error:
  2287. free_dma_desc_resources(priv);
  2288. dma_desc_error:
  2289. if (dev->phydev)
  2290. phy_disconnect(dev->phydev);
  2291. return ret;
  2292. }
  2293. /**
  2294. * stmmac_release - close entry point of the driver
  2295. * @dev : device pointer.
  2296. * Description:
  2297. * This is the stop entry point of the driver.
  2298. */
  2299. static int stmmac_release(struct net_device *dev)
  2300. {
  2301. struct stmmac_priv *priv = netdev_priv(dev);
  2302. u32 chan;
  2303. if (priv->eee_enabled)
  2304. del_timer_sync(&priv->eee_ctrl_timer);
  2305. /* Stop and disconnect the PHY */
  2306. if (dev->phydev) {
  2307. phy_stop(dev->phydev);
  2308. phy_disconnect(dev->phydev);
  2309. }
  2310. stmmac_stop_all_queues(priv);
  2311. stmmac_disable_all_queues(priv);
  2312. for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
  2313. del_timer_sync(&priv->tx_queue[chan].txtimer);
  2314. /* Free the IRQ lines */
  2315. free_irq(dev->irq, dev);
  2316. if (priv->wol_irq != dev->irq)
  2317. free_irq(priv->wol_irq, dev);
  2318. if (priv->lpi_irq > 0)
  2319. free_irq(priv->lpi_irq, dev);
  2320. /* Stop TX/RX DMA and clear the descriptors */
  2321. stmmac_stop_all_dma(priv);
  2322. /* Release and free the Rx/Tx resources */
  2323. free_dma_desc_resources(priv);
  2324. /* Disable the MAC Rx/Tx */
  2325. stmmac_mac_set(priv, priv->ioaddr, false);
  2326. netif_carrier_off(dev);
  2327. stmmac_release_ptp(priv);
  2328. return 0;
  2329. }
  2330. /**
  2331. * stmmac_tso_allocator - close entry point of the driver
  2332. * @priv: driver private structure
  2333. * @des: buffer start address
  2334. * @total_len: total length to fill in descriptors
  2335. * @last_segmant: condition for the last descriptor
  2336. * @queue: TX queue index
  2337. * Description:
  2338. * This function fills descriptor and request new descriptors according to
  2339. * buffer length to fill
  2340. */
  2341. static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
  2342. int total_len, bool last_segment, u32 queue)
  2343. {
  2344. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  2345. struct dma_desc *desc;
  2346. u32 buff_size;
  2347. int tmp_len;
  2348. tmp_len = total_len;
  2349. while (tmp_len > 0) {
  2350. tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
  2351. WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
  2352. desc = tx_q->dma_tx + tx_q->cur_tx;
  2353. desc->des0 = cpu_to_le32(des + (total_len - tmp_len));
  2354. buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
  2355. TSO_MAX_BUFF_SIZE : tmp_len;
  2356. stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
  2357. 0, 1,
  2358. (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
  2359. 0, 0);
  2360. tmp_len -= TSO_MAX_BUFF_SIZE;
  2361. }
  2362. }
  2363. /**
  2364. * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
  2365. * @skb : the socket buffer
  2366. * @dev : device pointer
  2367. * Description: this is the transmit function that is called on TSO frames
  2368. * (support available on GMAC4 and newer chips).
  2369. * Diagram below show the ring programming in case of TSO frames:
  2370. *
  2371. * First Descriptor
  2372. * --------
  2373. * | DES0 |---> buffer1 = L2/L3/L4 header
  2374. * | DES1 |---> TCP Payload (can continue on next descr...)
  2375. * | DES2 |---> buffer 1 and 2 len
  2376. * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
  2377. * --------
  2378. * |
  2379. * ...
  2380. * |
  2381. * --------
  2382. * | DES0 | --| Split TCP Payload on Buffers 1 and 2
  2383. * | DES1 | --|
  2384. * | DES2 | --> buffer 1 and 2 len
  2385. * | DES3 |
  2386. * --------
  2387. *
  2388. * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
  2389. */
  2390. static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
  2391. {
  2392. struct dma_desc *desc, *first, *mss_desc = NULL;
  2393. struct stmmac_priv *priv = netdev_priv(dev);
  2394. int nfrags = skb_shinfo(skb)->nr_frags;
  2395. u32 queue = skb_get_queue_mapping(skb);
  2396. unsigned int first_entry, des;
  2397. struct stmmac_tx_queue *tx_q;
  2398. int tmp_pay_len = 0;
  2399. u32 pay_len, mss;
  2400. u8 proto_hdr_len;
  2401. int i;
  2402. tx_q = &priv->tx_queue[queue];
  2403. /* Compute header lengths */
  2404. proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  2405. /* Desc availability based on threshold should be enough safe */
  2406. if (unlikely(stmmac_tx_avail(priv, queue) <
  2407. (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
  2408. if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
  2409. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
  2410. queue));
  2411. /* This is a hard error, log it. */
  2412. netdev_err(priv->dev,
  2413. "%s: Tx Ring full when queue awake\n",
  2414. __func__);
  2415. }
  2416. return NETDEV_TX_BUSY;
  2417. }
  2418. pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
  2419. mss = skb_shinfo(skb)->gso_size;
  2420. /* set new MSS value if needed */
  2421. if (mss != tx_q->mss) {
  2422. mss_desc = tx_q->dma_tx + tx_q->cur_tx;
  2423. stmmac_set_mss(priv, mss_desc, mss);
  2424. tx_q->mss = mss;
  2425. tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
  2426. WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
  2427. }
  2428. if (netif_msg_tx_queued(priv)) {
  2429. pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
  2430. __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
  2431. pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
  2432. skb->data_len);
  2433. }
  2434. first_entry = tx_q->cur_tx;
  2435. WARN_ON(tx_q->tx_skbuff[first_entry]);
  2436. desc = tx_q->dma_tx + first_entry;
  2437. first = desc;
  2438. /* first descriptor: fill Headers on Buf1 */
  2439. des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
  2440. DMA_TO_DEVICE);
  2441. if (dma_mapping_error(priv->device, des))
  2442. goto dma_map_err;
  2443. tx_q->tx_skbuff_dma[first_entry].buf = des;
  2444. tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
  2445. first->des0 = cpu_to_le32(des);
  2446. /* Fill start of payload in buff2 of first descriptor */
  2447. if (pay_len)
  2448. first->des1 = cpu_to_le32(des + proto_hdr_len);
  2449. /* If needed take extra descriptors to fill the remaining payload */
  2450. tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
  2451. stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
  2452. /* Prepare fragments */
  2453. for (i = 0; i < nfrags; i++) {
  2454. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2455. des = skb_frag_dma_map(priv->device, frag, 0,
  2456. skb_frag_size(frag),
  2457. DMA_TO_DEVICE);
  2458. if (dma_mapping_error(priv->device, des))
  2459. goto dma_map_err;
  2460. stmmac_tso_allocator(priv, des, skb_frag_size(frag),
  2461. (i == nfrags - 1), queue);
  2462. tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
  2463. tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
  2464. tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
  2465. }
  2466. tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
  2467. /* Only the last descriptor gets to point to the skb. */
  2468. tx_q->tx_skbuff[tx_q->cur_tx] = skb;
  2469. /* We've used all descriptors we need for this skb, however,
  2470. * advance cur_tx so that it references a fresh descriptor.
  2471. * ndo_start_xmit will fill this descriptor the next time it's
  2472. * called and stmmac_tx_clean may clean up to this descriptor.
  2473. */
  2474. tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
  2475. if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
  2476. netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
  2477. __func__);
  2478. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
  2479. }
  2480. dev->stats.tx_bytes += skb->len;
  2481. priv->xstats.tx_tso_frames++;
  2482. priv->xstats.tx_tso_nfrags += nfrags;
  2483. /* Manage tx mitigation */
  2484. tx_q->tx_count_frames += nfrags + 1;
  2485. if (priv->tx_coal_frames <= tx_q->tx_count_frames) {
  2486. stmmac_set_tx_ic(priv, desc);
  2487. priv->xstats.tx_set_ic_bit++;
  2488. tx_q->tx_count_frames = 0;
  2489. } else {
  2490. stmmac_tx_timer_arm(priv, queue);
  2491. }
  2492. skb_tx_timestamp(skb);
  2493. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  2494. priv->hwts_tx_en)) {
  2495. /* declare that device is doing timestamping */
  2496. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2497. stmmac_enable_tx_timestamp(priv, first);
  2498. }
  2499. /* Complete the first descriptor before granting the DMA */
  2500. stmmac_prepare_tso_tx_desc(priv, first, 1,
  2501. proto_hdr_len,
  2502. pay_len,
  2503. 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
  2504. tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
  2505. /* If context desc is used to change MSS */
  2506. if (mss_desc) {
  2507. /* Make sure that first descriptor has been completely
  2508. * written, including its own bit. This is because MSS is
  2509. * actually before first descriptor, so we need to make
  2510. * sure that MSS's own bit is the last thing written.
  2511. */
  2512. dma_wmb();
  2513. stmmac_set_tx_owner(priv, mss_desc);
  2514. }
  2515. /* The own bit must be the latest setting done when prepare the
  2516. * descriptor and then barrier is needed to make sure that
  2517. * all is coherent before granting the DMA engine.
  2518. */
  2519. wmb();
  2520. if (netif_msg_pktdata(priv)) {
  2521. pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
  2522. __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
  2523. tx_q->cur_tx, first, nfrags);
  2524. stmmac_display_ring(priv, (void *)tx_q->dma_tx, DMA_TX_SIZE, 0);
  2525. pr_info(">>> frame to be transmitted: ");
  2526. print_pkt(skb->data, skb_headlen(skb));
  2527. }
  2528. netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
  2529. tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * sizeof(*desc));
  2530. stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
  2531. return NETDEV_TX_OK;
  2532. dma_map_err:
  2533. dev_err(priv->device, "Tx dma map failed\n");
  2534. dev_kfree_skb(skb);
  2535. priv->dev->stats.tx_dropped++;
  2536. return NETDEV_TX_OK;
  2537. }
  2538. /**
  2539. * stmmac_xmit - Tx entry point of the driver
  2540. * @skb : the socket buffer
  2541. * @dev : device pointer
  2542. * Description : this is the tx entry point of the driver.
  2543. * It programs the chain or the ring and supports oversized frames
  2544. * and SG feature.
  2545. */
  2546. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  2547. {
  2548. struct stmmac_priv *priv = netdev_priv(dev);
  2549. unsigned int nopaged_len = skb_headlen(skb);
  2550. int i, csum_insertion = 0, is_jumbo = 0;
  2551. u32 queue = skb_get_queue_mapping(skb);
  2552. int nfrags = skb_shinfo(skb)->nr_frags;
  2553. int entry;
  2554. unsigned int first_entry;
  2555. struct dma_desc *desc, *first;
  2556. struct stmmac_tx_queue *tx_q;
  2557. unsigned int enh_desc;
  2558. unsigned int des;
  2559. tx_q = &priv->tx_queue[queue];
  2560. /* Manage oversized TCP frames for GMAC4 device */
  2561. if (skb_is_gso(skb) && priv->tso) {
  2562. if (skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
  2563. return stmmac_tso_xmit(skb, dev);
  2564. }
  2565. if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
  2566. if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
  2567. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
  2568. queue));
  2569. /* This is a hard error, log it. */
  2570. netdev_err(priv->dev,
  2571. "%s: Tx Ring full when queue awake\n",
  2572. __func__);
  2573. }
  2574. return NETDEV_TX_BUSY;
  2575. }
  2576. if (priv->tx_path_in_lpi_mode)
  2577. stmmac_disable_eee_mode(priv);
  2578. entry = tx_q->cur_tx;
  2579. first_entry = entry;
  2580. WARN_ON(tx_q->tx_skbuff[first_entry]);
  2581. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  2582. if (likely(priv->extend_desc))
  2583. desc = (struct dma_desc *)(tx_q->dma_etx + entry);
  2584. else
  2585. desc = tx_q->dma_tx + entry;
  2586. first = desc;
  2587. enh_desc = priv->plat->enh_desc;
  2588. /* To program the descriptors according to the size of the frame */
  2589. if (enh_desc)
  2590. is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
  2591. if (unlikely(is_jumbo)) {
  2592. entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
  2593. if (unlikely(entry < 0) && (entry != -EINVAL))
  2594. goto dma_map_err;
  2595. }
  2596. for (i = 0; i < nfrags; i++) {
  2597. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2598. int len = skb_frag_size(frag);
  2599. bool last_segment = (i == (nfrags - 1));
  2600. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  2601. WARN_ON(tx_q->tx_skbuff[entry]);
  2602. if (likely(priv->extend_desc))
  2603. desc = (struct dma_desc *)(tx_q->dma_etx + entry);
  2604. else
  2605. desc = tx_q->dma_tx + entry;
  2606. des = skb_frag_dma_map(priv->device, frag, 0, len,
  2607. DMA_TO_DEVICE);
  2608. if (dma_mapping_error(priv->device, des))
  2609. goto dma_map_err; /* should reuse desc w/o issues */
  2610. tx_q->tx_skbuff_dma[entry].buf = des;
  2611. stmmac_set_desc_addr(priv, desc, des);
  2612. tx_q->tx_skbuff_dma[entry].map_as_page = true;
  2613. tx_q->tx_skbuff_dma[entry].len = len;
  2614. tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
  2615. /* Prepare the descriptor and set the own bit too */
  2616. stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
  2617. priv->mode, 1, last_segment, skb->len);
  2618. }
  2619. /* Only the last descriptor gets to point to the skb. */
  2620. tx_q->tx_skbuff[entry] = skb;
  2621. /* We've used all descriptors we need for this skb, however,
  2622. * advance cur_tx so that it references a fresh descriptor.
  2623. * ndo_start_xmit will fill this descriptor the next time it's
  2624. * called and stmmac_tx_clean may clean up to this descriptor.
  2625. */
  2626. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  2627. tx_q->cur_tx = entry;
  2628. if (netif_msg_pktdata(priv)) {
  2629. void *tx_head;
  2630. netdev_dbg(priv->dev,
  2631. "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
  2632. __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
  2633. entry, first, nfrags);
  2634. if (priv->extend_desc)
  2635. tx_head = (void *)tx_q->dma_etx;
  2636. else
  2637. tx_head = (void *)tx_q->dma_tx;
  2638. stmmac_display_ring(priv, tx_head, DMA_TX_SIZE, false);
  2639. netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
  2640. print_pkt(skb->data, skb->len);
  2641. }
  2642. if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
  2643. netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
  2644. __func__);
  2645. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
  2646. }
  2647. dev->stats.tx_bytes += skb->len;
  2648. /* According to the coalesce parameter the IC bit for the latest
  2649. * segment is reset and the timer re-started to clean the tx status.
  2650. * This approach takes care about the fragments: desc is the first
  2651. * element in case of no SG.
  2652. */
  2653. tx_q->tx_count_frames += nfrags + 1;
  2654. if (priv->tx_coal_frames <= tx_q->tx_count_frames) {
  2655. stmmac_set_tx_ic(priv, desc);
  2656. priv->xstats.tx_set_ic_bit++;
  2657. tx_q->tx_count_frames = 0;
  2658. } else {
  2659. stmmac_tx_timer_arm(priv, queue);
  2660. }
  2661. skb_tx_timestamp(skb);
  2662. /* Ready to fill the first descriptor and set the OWN bit w/o any
  2663. * problems because all the descriptors are actually ready to be
  2664. * passed to the DMA engine.
  2665. */
  2666. if (likely(!is_jumbo)) {
  2667. bool last_segment = (nfrags == 0);
  2668. des = dma_map_single(priv->device, skb->data,
  2669. nopaged_len, DMA_TO_DEVICE);
  2670. if (dma_mapping_error(priv->device, des))
  2671. goto dma_map_err;
  2672. tx_q->tx_skbuff_dma[first_entry].buf = des;
  2673. stmmac_set_desc_addr(priv, first, des);
  2674. tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
  2675. tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
  2676. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  2677. priv->hwts_tx_en)) {
  2678. /* declare that device is doing timestamping */
  2679. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2680. stmmac_enable_tx_timestamp(priv, first);
  2681. }
  2682. /* Prepare the first descriptor setting the OWN bit too */
  2683. stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
  2684. csum_insertion, priv->mode, 1, last_segment,
  2685. skb->len);
  2686. /* The own bit must be the latest setting done when prepare the
  2687. * descriptor and then barrier is needed to make sure that
  2688. * all is coherent before granting the DMA engine.
  2689. */
  2690. wmb();
  2691. }
  2692. netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
  2693. stmmac_enable_dma_transmission(priv, priv->ioaddr);
  2694. tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * sizeof(*desc));
  2695. stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
  2696. return NETDEV_TX_OK;
  2697. dma_map_err:
  2698. netdev_err(priv->dev, "Tx DMA map failed\n");
  2699. dev_kfree_skb(skb);
  2700. priv->dev->stats.tx_dropped++;
  2701. return NETDEV_TX_OK;
  2702. }
  2703. static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
  2704. {
  2705. struct vlan_ethhdr *veth;
  2706. __be16 vlan_proto;
  2707. u16 vlanid;
  2708. veth = (struct vlan_ethhdr *)skb->data;
  2709. vlan_proto = veth->h_vlan_proto;
  2710. if ((vlan_proto == htons(ETH_P_8021Q) &&
  2711. dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
  2712. (vlan_proto == htons(ETH_P_8021AD) &&
  2713. dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
  2714. /* pop the vlan tag */
  2715. vlanid = ntohs(veth->h_vlan_TCI);
  2716. memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
  2717. skb_pull(skb, VLAN_HLEN);
  2718. __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
  2719. }
  2720. }
  2721. static inline int stmmac_rx_threshold_count(struct stmmac_rx_queue *rx_q)
  2722. {
  2723. if (rx_q->rx_zeroc_thresh < STMMAC_RX_THRESH)
  2724. return 0;
  2725. return 1;
  2726. }
  2727. /**
  2728. * stmmac_rx_refill - refill used skb preallocated buffers
  2729. * @priv: driver private structure
  2730. * @queue: RX queue index
  2731. * Description : this is to reallocate the skb for the reception process
  2732. * that is based on zero-copy.
  2733. */
  2734. static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
  2735. {
  2736. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  2737. int dirty = stmmac_rx_dirty(priv, queue);
  2738. unsigned int entry = rx_q->dirty_rx;
  2739. int bfsize = priv->dma_buf_sz;
  2740. while (dirty-- > 0) {
  2741. struct dma_desc *p;
  2742. if (priv->extend_desc)
  2743. p = (struct dma_desc *)(rx_q->dma_erx + entry);
  2744. else
  2745. p = rx_q->dma_rx + entry;
  2746. if (likely(!rx_q->rx_skbuff[entry])) {
  2747. struct sk_buff *skb;
  2748. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  2749. if (unlikely(!skb)) {
  2750. /* so for a while no zero-copy! */
  2751. rx_q->rx_zeroc_thresh = STMMAC_RX_THRESH;
  2752. if (unlikely(net_ratelimit()))
  2753. dev_err(priv->device,
  2754. "fail to alloc skb entry %d\n",
  2755. entry);
  2756. break;
  2757. }
  2758. rx_q->rx_skbuff[entry] = skb;
  2759. rx_q->rx_skbuff_dma[entry] =
  2760. dma_map_single(priv->device, skb->data, bfsize,
  2761. DMA_FROM_DEVICE);
  2762. if (dma_mapping_error(priv->device,
  2763. rx_q->rx_skbuff_dma[entry])) {
  2764. netdev_err(priv->dev, "Rx DMA map failed\n");
  2765. dev_kfree_skb(skb);
  2766. break;
  2767. }
  2768. stmmac_set_desc_addr(priv, p, rx_q->rx_skbuff_dma[entry]);
  2769. stmmac_refill_desc3(priv, rx_q, p);
  2770. if (rx_q->rx_zeroc_thresh > 0)
  2771. rx_q->rx_zeroc_thresh--;
  2772. netif_dbg(priv, rx_status, priv->dev,
  2773. "refill entry #%d\n", entry);
  2774. }
  2775. dma_wmb();
  2776. stmmac_set_rx_owner(priv, p, priv->use_riwt);
  2777. dma_wmb();
  2778. entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
  2779. }
  2780. rx_q->dirty_rx = entry;
  2781. }
  2782. /**
  2783. * stmmac_rx - manage the receive process
  2784. * @priv: driver private structure
  2785. * @limit: napi bugget
  2786. * @queue: RX queue index.
  2787. * Description : this the function called by the napi poll method.
  2788. * It gets all the frames inside the ring.
  2789. */
  2790. static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
  2791. {
  2792. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  2793. struct stmmac_channel *ch = &priv->channel[queue];
  2794. unsigned int entry = rx_q->cur_rx;
  2795. int coe = priv->hw->rx_csum;
  2796. unsigned int next_entry;
  2797. unsigned int count = 0;
  2798. bool xmac;
  2799. xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
  2800. if (netif_msg_rx_status(priv)) {
  2801. void *rx_head;
  2802. netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
  2803. if (priv->extend_desc)
  2804. rx_head = (void *)rx_q->dma_erx;
  2805. else
  2806. rx_head = (void *)rx_q->dma_rx;
  2807. stmmac_display_ring(priv, rx_head, DMA_RX_SIZE, true);
  2808. }
  2809. while (count < limit) {
  2810. int status;
  2811. struct dma_desc *p;
  2812. struct dma_desc *np;
  2813. if (priv->extend_desc)
  2814. p = (struct dma_desc *)(rx_q->dma_erx + entry);
  2815. else
  2816. p = rx_q->dma_rx + entry;
  2817. /* read the status of the incoming frame */
  2818. status = stmmac_rx_status(priv, &priv->dev->stats,
  2819. &priv->xstats, p);
  2820. /* check if managed by the DMA otherwise go ahead */
  2821. if (unlikely(status & dma_own))
  2822. break;
  2823. count++;
  2824. rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, DMA_RX_SIZE);
  2825. next_entry = rx_q->cur_rx;
  2826. if (priv->extend_desc)
  2827. np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
  2828. else
  2829. np = rx_q->dma_rx + next_entry;
  2830. prefetch(np);
  2831. if (priv->extend_desc)
  2832. stmmac_rx_extended_status(priv, &priv->dev->stats,
  2833. &priv->xstats, rx_q->dma_erx + entry);
  2834. if (unlikely(status == discard_frame)) {
  2835. priv->dev->stats.rx_errors++;
  2836. if (priv->hwts_rx_en && !priv->extend_desc) {
  2837. /* DESC2 & DESC3 will be overwritten by device
  2838. * with timestamp value, hence reinitialize
  2839. * them in stmmac_rx_refill() function so that
  2840. * device can reuse it.
  2841. */
  2842. dev_kfree_skb_any(rx_q->rx_skbuff[entry]);
  2843. rx_q->rx_skbuff[entry] = NULL;
  2844. dma_unmap_single(priv->device,
  2845. rx_q->rx_skbuff_dma[entry],
  2846. priv->dma_buf_sz,
  2847. DMA_FROM_DEVICE);
  2848. }
  2849. } else {
  2850. struct sk_buff *skb;
  2851. int frame_len;
  2852. unsigned int des;
  2853. stmmac_get_desc_addr(priv, p, &des);
  2854. frame_len = stmmac_get_rx_frame_len(priv, p, coe);
  2855. /* If frame length is greater than skb buffer size
  2856. * (preallocated during init) then the packet is
  2857. * ignored
  2858. */
  2859. if (frame_len > priv->dma_buf_sz) {
  2860. netdev_err(priv->dev,
  2861. "len %d larger than size (%d)\n",
  2862. frame_len, priv->dma_buf_sz);
  2863. priv->dev->stats.rx_length_errors++;
  2864. break;
  2865. }
  2866. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  2867. * Type frames (LLC/LLC-SNAP)
  2868. *
  2869. * llc_snap is never checked in GMAC >= 4, so this ACS
  2870. * feature is always disabled and packets need to be
  2871. * stripped manually.
  2872. */
  2873. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
  2874. unlikely(status != llc_snap))
  2875. frame_len -= ETH_FCS_LEN;
  2876. if (netif_msg_rx_status(priv)) {
  2877. netdev_dbg(priv->dev, "\tdesc: %p [entry %d] buff=0x%x\n",
  2878. p, entry, des);
  2879. netdev_dbg(priv->dev, "frame size %d, COE: %d\n",
  2880. frame_len, status);
  2881. }
  2882. /* The zero-copy is always used for all the sizes
  2883. * in case of GMAC4 because it needs
  2884. * to refill the used descriptors, always.
  2885. */
  2886. if (unlikely(!xmac &&
  2887. ((frame_len < priv->rx_copybreak) ||
  2888. stmmac_rx_threshold_count(rx_q)))) {
  2889. skb = netdev_alloc_skb_ip_align(priv->dev,
  2890. frame_len);
  2891. if (unlikely(!skb)) {
  2892. if (net_ratelimit())
  2893. dev_warn(priv->device,
  2894. "packet dropped\n");
  2895. priv->dev->stats.rx_dropped++;
  2896. break;
  2897. }
  2898. dma_sync_single_for_cpu(priv->device,
  2899. rx_q->rx_skbuff_dma
  2900. [entry], frame_len,
  2901. DMA_FROM_DEVICE);
  2902. skb_copy_to_linear_data(skb,
  2903. rx_q->
  2904. rx_skbuff[entry]->data,
  2905. frame_len);
  2906. skb_put(skb, frame_len);
  2907. dma_sync_single_for_device(priv->device,
  2908. rx_q->rx_skbuff_dma
  2909. [entry], frame_len,
  2910. DMA_FROM_DEVICE);
  2911. } else {
  2912. skb = rx_q->rx_skbuff[entry];
  2913. if (unlikely(!skb)) {
  2914. netdev_err(priv->dev,
  2915. "%s: Inconsistent Rx chain\n",
  2916. priv->dev->name);
  2917. priv->dev->stats.rx_dropped++;
  2918. break;
  2919. }
  2920. prefetch(skb->data - NET_IP_ALIGN);
  2921. rx_q->rx_skbuff[entry] = NULL;
  2922. rx_q->rx_zeroc_thresh++;
  2923. skb_put(skb, frame_len);
  2924. dma_unmap_single(priv->device,
  2925. rx_q->rx_skbuff_dma[entry],
  2926. priv->dma_buf_sz,
  2927. DMA_FROM_DEVICE);
  2928. }
  2929. if (netif_msg_pktdata(priv)) {
  2930. netdev_dbg(priv->dev, "frame received (%dbytes)",
  2931. frame_len);
  2932. print_pkt(skb->data, frame_len);
  2933. }
  2934. stmmac_get_rx_hwtstamp(priv, p, np, skb);
  2935. stmmac_rx_vlan(priv->dev, skb);
  2936. skb->protocol = eth_type_trans(skb, priv->dev);
  2937. if (unlikely(!coe))
  2938. skb_checksum_none_assert(skb);
  2939. else
  2940. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2941. napi_gro_receive(&ch->napi, skb);
  2942. priv->dev->stats.rx_packets++;
  2943. priv->dev->stats.rx_bytes += frame_len;
  2944. }
  2945. entry = next_entry;
  2946. }
  2947. stmmac_rx_refill(priv, queue);
  2948. priv->xstats.rx_pkt_n += count;
  2949. return count;
  2950. }
  2951. /**
  2952. * stmmac_poll - stmmac poll method (NAPI)
  2953. * @napi : pointer to the napi structure.
  2954. * @budget : maximum number of packets that the current CPU can receive from
  2955. * all interfaces.
  2956. * Description :
  2957. * To look at the incoming frames and clear the tx resources.
  2958. */
  2959. static int stmmac_napi_poll(struct napi_struct *napi, int budget)
  2960. {
  2961. struct stmmac_channel *ch =
  2962. container_of(napi, struct stmmac_channel, napi);
  2963. struct stmmac_priv *priv = ch->priv_data;
  2964. int work_done = 0, work_rem = budget;
  2965. u32 chan = ch->index;
  2966. priv->xstats.napi_poll++;
  2967. if (ch->has_tx) {
  2968. int done = stmmac_tx_clean(priv, work_rem, chan);
  2969. work_done += done;
  2970. work_rem -= done;
  2971. }
  2972. if (ch->has_rx) {
  2973. int done = stmmac_rx(priv, work_rem, chan);
  2974. work_done += done;
  2975. work_rem -= done;
  2976. }
  2977. if (work_done < budget && napi_complete_done(napi, work_done))
  2978. stmmac_enable_dma_irq(priv, priv->ioaddr, chan);
  2979. return work_done;
  2980. }
  2981. /**
  2982. * stmmac_tx_timeout
  2983. * @dev : Pointer to net device structure
  2984. * Description: this function is called when a packet transmission fails to
  2985. * complete within a reasonable time. The driver will mark the error in the
  2986. * netdev structure and arrange for the device to be reset to a sane state
  2987. * in order to transmit a new packet.
  2988. */
  2989. static void stmmac_tx_timeout(struct net_device *dev)
  2990. {
  2991. struct stmmac_priv *priv = netdev_priv(dev);
  2992. stmmac_global_err(priv);
  2993. }
  2994. /**
  2995. * stmmac_set_rx_mode - entry point for multicast addressing
  2996. * @dev : pointer to the device structure
  2997. * Description:
  2998. * This function is a driver entry point which gets called by the kernel
  2999. * whenever multicast addresses must be enabled/disabled.
  3000. * Return value:
  3001. * void.
  3002. */
  3003. static void stmmac_set_rx_mode(struct net_device *dev)
  3004. {
  3005. struct stmmac_priv *priv = netdev_priv(dev);
  3006. stmmac_set_filter(priv, priv->hw, dev);
  3007. }
  3008. /**
  3009. * stmmac_change_mtu - entry point to change MTU size for the device.
  3010. * @dev : device pointer.
  3011. * @new_mtu : the new MTU size for the device.
  3012. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  3013. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  3014. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  3015. * Return value:
  3016. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  3017. * file on failure.
  3018. */
  3019. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  3020. {
  3021. struct stmmac_priv *priv = netdev_priv(dev);
  3022. if (netif_running(dev)) {
  3023. netdev_err(priv->dev, "must be stopped to change its MTU\n");
  3024. return -EBUSY;
  3025. }
  3026. dev->mtu = new_mtu;
  3027. netdev_update_features(dev);
  3028. return 0;
  3029. }
  3030. static netdev_features_t stmmac_fix_features(struct net_device *dev,
  3031. netdev_features_t features)
  3032. {
  3033. struct stmmac_priv *priv = netdev_priv(dev);
  3034. if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
  3035. features &= ~NETIF_F_RXCSUM;
  3036. if (!priv->plat->tx_coe)
  3037. features &= ~NETIF_F_CSUM_MASK;
  3038. /* Some GMAC devices have a bugged Jumbo frame support that
  3039. * needs to have the Tx COE disabled for oversized frames
  3040. * (due to limited buffer sizes). In this case we disable
  3041. * the TX csum insertion in the TDES and not use SF.
  3042. */
  3043. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  3044. features &= ~NETIF_F_CSUM_MASK;
  3045. /* Disable tso if asked by ethtool */
  3046. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  3047. if (features & NETIF_F_TSO)
  3048. priv->tso = true;
  3049. else
  3050. priv->tso = false;
  3051. }
  3052. return features;
  3053. }
  3054. static int stmmac_set_features(struct net_device *netdev,
  3055. netdev_features_t features)
  3056. {
  3057. struct stmmac_priv *priv = netdev_priv(netdev);
  3058. /* Keep the COE Type in case of csum is supporting */
  3059. if (features & NETIF_F_RXCSUM)
  3060. priv->hw->rx_csum = priv->plat->rx_coe;
  3061. else
  3062. priv->hw->rx_csum = 0;
  3063. /* No check needed because rx_coe has been set before and it will be
  3064. * fixed in case of issue.
  3065. */
  3066. stmmac_rx_ipc(priv, priv->hw);
  3067. return 0;
  3068. }
  3069. /**
  3070. * stmmac_interrupt - main ISR
  3071. * @irq: interrupt number.
  3072. * @dev_id: to pass the net device pointer.
  3073. * Description: this is the main driver interrupt service routine.
  3074. * It can call:
  3075. * o DMA service routine (to manage incoming frame reception and transmission
  3076. * status)
  3077. * o Core interrupts to manage: remote wake-up, management counter, LPI
  3078. * interrupts.
  3079. */
  3080. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  3081. {
  3082. struct net_device *dev = (struct net_device *)dev_id;
  3083. struct stmmac_priv *priv = netdev_priv(dev);
  3084. u32 rx_cnt = priv->plat->rx_queues_to_use;
  3085. u32 tx_cnt = priv->plat->tx_queues_to_use;
  3086. u32 queues_count;
  3087. u32 queue;
  3088. bool xmac;
  3089. xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
  3090. queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
  3091. if (priv->irq_wake)
  3092. pm_wakeup_event(priv->device, 0);
  3093. if (unlikely(!dev)) {
  3094. netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
  3095. return IRQ_NONE;
  3096. }
  3097. /* Check if adapter is up */
  3098. if (test_bit(STMMAC_DOWN, &priv->state))
  3099. return IRQ_HANDLED;
  3100. /* Check if a fatal error happened */
  3101. if (stmmac_safety_feat_interrupt(priv))
  3102. return IRQ_HANDLED;
  3103. /* To handle GMAC own interrupts */
  3104. if ((priv->plat->has_gmac) || xmac) {
  3105. int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
  3106. int mtl_status;
  3107. if (unlikely(status)) {
  3108. /* For LPI we need to save the tx status */
  3109. if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
  3110. priv->tx_path_in_lpi_mode = true;
  3111. if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
  3112. priv->tx_path_in_lpi_mode = false;
  3113. }
  3114. for (queue = 0; queue < queues_count; queue++) {
  3115. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  3116. mtl_status = stmmac_host_mtl_irq_status(priv, priv->hw,
  3117. queue);
  3118. if (mtl_status != -EINVAL)
  3119. status |= mtl_status;
  3120. if (status & CORE_IRQ_MTL_RX_OVERFLOW)
  3121. stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
  3122. rx_q->rx_tail_addr,
  3123. queue);
  3124. }
  3125. /* PCS link status */
  3126. if (priv->hw->pcs) {
  3127. if (priv->xstats.pcs_link)
  3128. netif_carrier_on(dev);
  3129. else
  3130. netif_carrier_off(dev);
  3131. }
  3132. }
  3133. /* To handle DMA interrupts */
  3134. stmmac_dma_interrupt(priv);
  3135. return IRQ_HANDLED;
  3136. }
  3137. #ifdef CONFIG_NET_POLL_CONTROLLER
  3138. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  3139. * to allow network I/O with interrupts disabled.
  3140. */
  3141. static void stmmac_poll_controller(struct net_device *dev)
  3142. {
  3143. disable_irq(dev->irq);
  3144. stmmac_interrupt(dev->irq, dev);
  3145. enable_irq(dev->irq);
  3146. }
  3147. #endif
  3148. /**
  3149. * stmmac_ioctl - Entry point for the Ioctl
  3150. * @dev: Device pointer.
  3151. * @rq: An IOCTL specefic structure, that can contain a pointer to
  3152. * a proprietary structure used to pass information to the driver.
  3153. * @cmd: IOCTL command
  3154. * Description:
  3155. * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
  3156. */
  3157. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  3158. {
  3159. int ret = -EOPNOTSUPP;
  3160. if (!netif_running(dev))
  3161. return -EINVAL;
  3162. switch (cmd) {
  3163. case SIOCGMIIPHY:
  3164. case SIOCGMIIREG:
  3165. case SIOCSMIIREG:
  3166. if (!dev->phydev)
  3167. return -EINVAL;
  3168. ret = phy_mii_ioctl(dev->phydev, rq, cmd);
  3169. break;
  3170. case SIOCSHWTSTAMP:
  3171. ret = stmmac_hwtstamp_ioctl(dev, rq);
  3172. break;
  3173. default:
  3174. break;
  3175. }
  3176. return ret;
  3177. }
  3178. static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  3179. void *cb_priv)
  3180. {
  3181. struct stmmac_priv *priv = cb_priv;
  3182. int ret = -EOPNOTSUPP;
  3183. stmmac_disable_all_queues(priv);
  3184. switch (type) {
  3185. case TC_SETUP_CLSU32:
  3186. if (tc_cls_can_offload_and_chain0(priv->dev, type_data))
  3187. ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
  3188. break;
  3189. default:
  3190. break;
  3191. }
  3192. stmmac_enable_all_queues(priv);
  3193. return ret;
  3194. }
  3195. static int stmmac_setup_tc_block(struct stmmac_priv *priv,
  3196. struct tc_block_offload *f)
  3197. {
  3198. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  3199. return -EOPNOTSUPP;
  3200. switch (f->command) {
  3201. case TC_BLOCK_BIND:
  3202. return tcf_block_cb_register(f->block, stmmac_setup_tc_block_cb,
  3203. priv, priv, f->extack);
  3204. case TC_BLOCK_UNBIND:
  3205. tcf_block_cb_unregister(f->block, stmmac_setup_tc_block_cb, priv);
  3206. return 0;
  3207. default:
  3208. return -EOPNOTSUPP;
  3209. }
  3210. }
  3211. static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
  3212. void *type_data)
  3213. {
  3214. struct stmmac_priv *priv = netdev_priv(ndev);
  3215. switch (type) {
  3216. case TC_SETUP_BLOCK:
  3217. return stmmac_setup_tc_block(priv, type_data);
  3218. case TC_SETUP_QDISC_CBS:
  3219. return stmmac_tc_setup_cbs(priv, priv, type_data);
  3220. default:
  3221. return -EOPNOTSUPP;
  3222. }
  3223. }
  3224. static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
  3225. {
  3226. struct stmmac_priv *priv = netdev_priv(ndev);
  3227. int ret = 0;
  3228. ret = eth_mac_addr(ndev, addr);
  3229. if (ret)
  3230. return ret;
  3231. stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
  3232. return ret;
  3233. }
  3234. #ifdef CONFIG_DEBUG_FS
  3235. static struct dentry *stmmac_fs_dir;
  3236. static void sysfs_display_ring(void *head, int size, int extend_desc,
  3237. struct seq_file *seq)
  3238. {
  3239. int i;
  3240. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  3241. struct dma_desc *p = (struct dma_desc *)head;
  3242. for (i = 0; i < size; i++) {
  3243. if (extend_desc) {
  3244. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  3245. i, (unsigned int)virt_to_phys(ep),
  3246. le32_to_cpu(ep->basic.des0),
  3247. le32_to_cpu(ep->basic.des1),
  3248. le32_to_cpu(ep->basic.des2),
  3249. le32_to_cpu(ep->basic.des3));
  3250. ep++;
  3251. } else {
  3252. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  3253. i, (unsigned int)virt_to_phys(p),
  3254. le32_to_cpu(p->des0), le32_to_cpu(p->des1),
  3255. le32_to_cpu(p->des2), le32_to_cpu(p->des3));
  3256. p++;
  3257. }
  3258. seq_printf(seq, "\n");
  3259. }
  3260. }
  3261. static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
  3262. {
  3263. struct net_device *dev = seq->private;
  3264. struct stmmac_priv *priv = netdev_priv(dev);
  3265. u32 rx_count = priv->plat->rx_queues_to_use;
  3266. u32 tx_count = priv->plat->tx_queues_to_use;
  3267. u32 queue;
  3268. if ((dev->flags & IFF_UP) == 0)
  3269. return 0;
  3270. for (queue = 0; queue < rx_count; queue++) {
  3271. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  3272. seq_printf(seq, "RX Queue %d:\n", queue);
  3273. if (priv->extend_desc) {
  3274. seq_printf(seq, "Extended descriptor ring:\n");
  3275. sysfs_display_ring((void *)rx_q->dma_erx,
  3276. DMA_RX_SIZE, 1, seq);
  3277. } else {
  3278. seq_printf(seq, "Descriptor ring:\n");
  3279. sysfs_display_ring((void *)rx_q->dma_rx,
  3280. DMA_RX_SIZE, 0, seq);
  3281. }
  3282. }
  3283. for (queue = 0; queue < tx_count; queue++) {
  3284. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  3285. seq_printf(seq, "TX Queue %d:\n", queue);
  3286. if (priv->extend_desc) {
  3287. seq_printf(seq, "Extended descriptor ring:\n");
  3288. sysfs_display_ring((void *)tx_q->dma_etx,
  3289. DMA_TX_SIZE, 1, seq);
  3290. } else {
  3291. seq_printf(seq, "Descriptor ring:\n");
  3292. sysfs_display_ring((void *)tx_q->dma_tx,
  3293. DMA_TX_SIZE, 0, seq);
  3294. }
  3295. }
  3296. return 0;
  3297. }
  3298. static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
  3299. {
  3300. return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
  3301. }
  3302. /* Debugfs files, should appear in /sys/kernel/debug/stmmaceth/eth0 */
  3303. static const struct file_operations stmmac_rings_status_fops = {
  3304. .owner = THIS_MODULE,
  3305. .open = stmmac_sysfs_ring_open,
  3306. .read = seq_read,
  3307. .llseek = seq_lseek,
  3308. .release = single_release,
  3309. };
  3310. static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
  3311. {
  3312. struct net_device *dev = seq->private;
  3313. struct stmmac_priv *priv = netdev_priv(dev);
  3314. if (!priv->hw_cap_support) {
  3315. seq_printf(seq, "DMA HW features not supported\n");
  3316. return 0;
  3317. }
  3318. seq_printf(seq, "==============================\n");
  3319. seq_printf(seq, "\tDMA HW features\n");
  3320. seq_printf(seq, "==============================\n");
  3321. seq_printf(seq, "\t10/100 Mbps: %s\n",
  3322. (priv->dma_cap.mbps_10_100) ? "Y" : "N");
  3323. seq_printf(seq, "\t1000 Mbps: %s\n",
  3324. (priv->dma_cap.mbps_1000) ? "Y" : "N");
  3325. seq_printf(seq, "\tHalf duplex: %s\n",
  3326. (priv->dma_cap.half_duplex) ? "Y" : "N");
  3327. seq_printf(seq, "\tHash Filter: %s\n",
  3328. (priv->dma_cap.hash_filter) ? "Y" : "N");
  3329. seq_printf(seq, "\tMultiple MAC address registers: %s\n",
  3330. (priv->dma_cap.multi_addr) ? "Y" : "N");
  3331. seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
  3332. (priv->dma_cap.pcs) ? "Y" : "N");
  3333. seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
  3334. (priv->dma_cap.sma_mdio) ? "Y" : "N");
  3335. seq_printf(seq, "\tPMT Remote wake up: %s\n",
  3336. (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
  3337. seq_printf(seq, "\tPMT Magic Frame: %s\n",
  3338. (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
  3339. seq_printf(seq, "\tRMON module: %s\n",
  3340. (priv->dma_cap.rmon) ? "Y" : "N");
  3341. seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
  3342. (priv->dma_cap.time_stamp) ? "Y" : "N");
  3343. seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
  3344. (priv->dma_cap.atime_stamp) ? "Y" : "N");
  3345. seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
  3346. (priv->dma_cap.eee) ? "Y" : "N");
  3347. seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
  3348. seq_printf(seq, "\tChecksum Offload in TX: %s\n",
  3349. (priv->dma_cap.tx_coe) ? "Y" : "N");
  3350. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  3351. seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
  3352. (priv->dma_cap.rx_coe) ? "Y" : "N");
  3353. } else {
  3354. seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
  3355. (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
  3356. seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
  3357. (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
  3358. }
  3359. seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
  3360. (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
  3361. seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
  3362. priv->dma_cap.number_rx_channel);
  3363. seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
  3364. priv->dma_cap.number_tx_channel);
  3365. seq_printf(seq, "\tEnhanced descriptors: %s\n",
  3366. (priv->dma_cap.enh_desc) ? "Y" : "N");
  3367. return 0;
  3368. }
  3369. static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
  3370. {
  3371. return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
  3372. }
  3373. static const struct file_operations stmmac_dma_cap_fops = {
  3374. .owner = THIS_MODULE,
  3375. .open = stmmac_sysfs_dma_cap_open,
  3376. .read = seq_read,
  3377. .llseek = seq_lseek,
  3378. .release = single_release,
  3379. };
  3380. static int stmmac_init_fs(struct net_device *dev)
  3381. {
  3382. struct stmmac_priv *priv = netdev_priv(dev);
  3383. /* Create per netdev entries */
  3384. priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
  3385. if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
  3386. netdev_err(priv->dev, "ERROR failed to create debugfs directory\n");
  3387. return -ENOMEM;
  3388. }
  3389. /* Entry to report DMA RX/TX rings */
  3390. priv->dbgfs_rings_status =
  3391. debugfs_create_file("descriptors_status", 0444,
  3392. priv->dbgfs_dir, dev,
  3393. &stmmac_rings_status_fops);
  3394. if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
  3395. netdev_err(priv->dev, "ERROR creating stmmac ring debugfs file\n");
  3396. debugfs_remove_recursive(priv->dbgfs_dir);
  3397. return -ENOMEM;
  3398. }
  3399. /* Entry to report the DMA HW features */
  3400. priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", 0444,
  3401. priv->dbgfs_dir,
  3402. dev, &stmmac_dma_cap_fops);
  3403. if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
  3404. netdev_err(priv->dev, "ERROR creating stmmac MMC debugfs file\n");
  3405. debugfs_remove_recursive(priv->dbgfs_dir);
  3406. return -ENOMEM;
  3407. }
  3408. return 0;
  3409. }
  3410. static void stmmac_exit_fs(struct net_device *dev)
  3411. {
  3412. struct stmmac_priv *priv = netdev_priv(dev);
  3413. debugfs_remove_recursive(priv->dbgfs_dir);
  3414. }
  3415. #endif /* CONFIG_DEBUG_FS */
  3416. static const struct net_device_ops stmmac_netdev_ops = {
  3417. .ndo_open = stmmac_open,
  3418. .ndo_start_xmit = stmmac_xmit,
  3419. .ndo_stop = stmmac_release,
  3420. .ndo_change_mtu = stmmac_change_mtu,
  3421. .ndo_fix_features = stmmac_fix_features,
  3422. .ndo_set_features = stmmac_set_features,
  3423. .ndo_set_rx_mode = stmmac_set_rx_mode,
  3424. .ndo_tx_timeout = stmmac_tx_timeout,
  3425. .ndo_do_ioctl = stmmac_ioctl,
  3426. .ndo_setup_tc = stmmac_setup_tc,
  3427. #ifdef CONFIG_NET_POLL_CONTROLLER
  3428. .ndo_poll_controller = stmmac_poll_controller,
  3429. #endif
  3430. .ndo_set_mac_address = stmmac_set_mac_address,
  3431. };
  3432. static void stmmac_reset_subtask(struct stmmac_priv *priv)
  3433. {
  3434. if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
  3435. return;
  3436. if (test_bit(STMMAC_DOWN, &priv->state))
  3437. return;
  3438. netdev_err(priv->dev, "Reset adapter.\n");
  3439. rtnl_lock();
  3440. netif_trans_update(priv->dev);
  3441. while (test_and_set_bit(STMMAC_RESETING, &priv->state))
  3442. usleep_range(1000, 2000);
  3443. set_bit(STMMAC_DOWN, &priv->state);
  3444. dev_close(priv->dev);
  3445. dev_open(priv->dev);
  3446. clear_bit(STMMAC_DOWN, &priv->state);
  3447. clear_bit(STMMAC_RESETING, &priv->state);
  3448. rtnl_unlock();
  3449. }
  3450. static void stmmac_service_task(struct work_struct *work)
  3451. {
  3452. struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
  3453. service_task);
  3454. stmmac_reset_subtask(priv);
  3455. clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
  3456. }
  3457. /**
  3458. * stmmac_hw_init - Init the MAC device
  3459. * @priv: driver private structure
  3460. * Description: this function is to configure the MAC device according to
  3461. * some platform parameters or the HW capability register. It prepares the
  3462. * driver to use either ring or chain modes and to setup either enhanced or
  3463. * normal descriptors.
  3464. */
  3465. static int stmmac_hw_init(struct stmmac_priv *priv)
  3466. {
  3467. int ret;
  3468. /* dwmac-sun8i only work in chain mode */
  3469. if (priv->plat->has_sun8i)
  3470. chain_mode = 1;
  3471. priv->chain_mode = chain_mode;
  3472. /* Initialize HW Interface */
  3473. ret = stmmac_hwif_init(priv);
  3474. if (ret)
  3475. return ret;
  3476. /* Get the HW capability (new GMAC newer than 3.50a) */
  3477. priv->hw_cap_support = stmmac_get_hw_features(priv);
  3478. if (priv->hw_cap_support) {
  3479. dev_info(priv->device, "DMA HW capability register supported\n");
  3480. /* We can override some gmac/dma configuration fields: e.g.
  3481. * enh_desc, tx_coe (e.g. that are passed through the
  3482. * platform) with the values from the HW capability
  3483. * register (if supported).
  3484. */
  3485. priv->plat->enh_desc = priv->dma_cap.enh_desc;
  3486. priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
  3487. priv->hw->pmt = priv->plat->pmt;
  3488. /* TXCOE doesn't work in thresh DMA mode */
  3489. if (priv->plat->force_thresh_dma_mode)
  3490. priv->plat->tx_coe = 0;
  3491. else
  3492. priv->plat->tx_coe = priv->dma_cap.tx_coe;
  3493. /* In case of GMAC4 rx_coe is from HW cap register. */
  3494. priv->plat->rx_coe = priv->dma_cap.rx_coe;
  3495. if (priv->dma_cap.rx_coe_type2)
  3496. priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
  3497. else if (priv->dma_cap.rx_coe_type1)
  3498. priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
  3499. } else {
  3500. dev_info(priv->device, "No HW DMA feature register supported\n");
  3501. }
  3502. if (priv->plat->rx_coe) {
  3503. priv->hw->rx_csum = priv->plat->rx_coe;
  3504. dev_info(priv->device, "RX Checksum Offload Engine supported\n");
  3505. if (priv->synopsys_id < DWMAC_CORE_4_00)
  3506. dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
  3507. }
  3508. if (priv->plat->tx_coe)
  3509. dev_info(priv->device, "TX Checksum insertion supported\n");
  3510. if (priv->plat->pmt) {
  3511. dev_info(priv->device, "Wake-Up On Lan supported\n");
  3512. device_set_wakeup_capable(priv->device, 1);
  3513. }
  3514. if (priv->dma_cap.tsoen)
  3515. dev_info(priv->device, "TSO supported\n");
  3516. /* Run HW quirks, if any */
  3517. if (priv->hwif_quirks) {
  3518. ret = priv->hwif_quirks(priv);
  3519. if (ret)
  3520. return ret;
  3521. }
  3522. return 0;
  3523. }
  3524. /**
  3525. * stmmac_dvr_probe
  3526. * @device: device pointer
  3527. * @plat_dat: platform data pointer
  3528. * @res: stmmac resource pointer
  3529. * Description: this is the main probe function used to
  3530. * call the alloc_etherdev, allocate the priv structure.
  3531. * Return:
  3532. * returns 0 on success, otherwise errno.
  3533. */
  3534. int stmmac_dvr_probe(struct device *device,
  3535. struct plat_stmmacenet_data *plat_dat,
  3536. struct stmmac_resources *res)
  3537. {
  3538. struct net_device *ndev = NULL;
  3539. struct stmmac_priv *priv;
  3540. u32 queue, maxq;
  3541. int ret = 0;
  3542. ndev = alloc_etherdev_mqs(sizeof(struct stmmac_priv),
  3543. MTL_MAX_TX_QUEUES,
  3544. MTL_MAX_RX_QUEUES);
  3545. if (!ndev)
  3546. return -ENOMEM;
  3547. SET_NETDEV_DEV(ndev, device);
  3548. priv = netdev_priv(ndev);
  3549. priv->device = device;
  3550. priv->dev = ndev;
  3551. stmmac_set_ethtool_ops(ndev);
  3552. priv->pause = pause;
  3553. priv->plat = plat_dat;
  3554. priv->ioaddr = res->addr;
  3555. priv->dev->base_addr = (unsigned long)res->addr;
  3556. priv->dev->irq = res->irq;
  3557. priv->wol_irq = res->wol_irq;
  3558. priv->lpi_irq = res->lpi_irq;
  3559. if (res->mac)
  3560. memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
  3561. dev_set_drvdata(device, priv->dev);
  3562. /* Verify driver arguments */
  3563. stmmac_verify_args();
  3564. /* Allocate workqueue */
  3565. priv->wq = create_singlethread_workqueue("stmmac_wq");
  3566. if (!priv->wq) {
  3567. dev_err(priv->device, "failed to create workqueue\n");
  3568. goto error_wq;
  3569. }
  3570. INIT_WORK(&priv->service_task, stmmac_service_task);
  3571. /* Override with kernel parameters if supplied XXX CRS XXX
  3572. * this needs to have multiple instances
  3573. */
  3574. if ((phyaddr >= 0) && (phyaddr <= 31))
  3575. priv->plat->phy_addr = phyaddr;
  3576. if (priv->plat->stmmac_rst) {
  3577. ret = reset_control_assert(priv->plat->stmmac_rst);
  3578. reset_control_deassert(priv->plat->stmmac_rst);
  3579. /* Some reset controllers have only reset callback instead of
  3580. * assert + deassert callbacks pair.
  3581. */
  3582. if (ret == -ENOTSUPP)
  3583. reset_control_reset(priv->plat->stmmac_rst);
  3584. }
  3585. /* Init MAC and get the capabilities */
  3586. ret = stmmac_hw_init(priv);
  3587. if (ret)
  3588. goto error_hw_init;
  3589. /* Configure real RX and TX queues */
  3590. netif_set_real_num_rx_queues(ndev, priv->plat->rx_queues_to_use);
  3591. netif_set_real_num_tx_queues(ndev, priv->plat->tx_queues_to_use);
  3592. ndev->netdev_ops = &stmmac_netdev_ops;
  3593. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  3594. NETIF_F_RXCSUM;
  3595. ret = stmmac_tc_init(priv, priv);
  3596. if (!ret) {
  3597. ndev->hw_features |= NETIF_F_HW_TC;
  3598. }
  3599. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  3600. ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  3601. priv->tso = true;
  3602. dev_info(priv->device, "TSO feature enabled\n");
  3603. }
  3604. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  3605. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  3606. #ifdef STMMAC_VLAN_TAG_USED
  3607. /* Both mac100 and gmac support receive VLAN tag detection */
  3608. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
  3609. #endif
  3610. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  3611. /* MTU range: 46 - hw-specific max */
  3612. ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
  3613. if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
  3614. ndev->max_mtu = JUMBO_LEN;
  3615. else if (priv->plat->has_xgmac)
  3616. ndev->max_mtu = XGMAC_JUMBO_LEN;
  3617. else
  3618. ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  3619. /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
  3620. * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
  3621. */
  3622. if ((priv->plat->maxmtu < ndev->max_mtu) &&
  3623. (priv->plat->maxmtu >= ndev->min_mtu))
  3624. ndev->max_mtu = priv->plat->maxmtu;
  3625. else if (priv->plat->maxmtu < ndev->min_mtu)
  3626. dev_warn(priv->device,
  3627. "%s: warning: maxmtu having invalid value (%d)\n",
  3628. __func__, priv->plat->maxmtu);
  3629. if (flow_ctrl)
  3630. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  3631. /* Rx Watchdog is available in the COREs newer than the 3.40.
  3632. * In some case, for example on bugged HW this feature
  3633. * has to be disable and this can be done by passing the
  3634. * riwt_off field from the platform.
  3635. */
  3636. if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
  3637. (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
  3638. priv->use_riwt = 1;
  3639. dev_info(priv->device,
  3640. "Enable RX Mitigation via HW Watchdog Timer\n");
  3641. }
  3642. /* Setup channels NAPI */
  3643. maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
  3644. for (queue = 0; queue < maxq; queue++) {
  3645. struct stmmac_channel *ch = &priv->channel[queue];
  3646. ch->priv_data = priv;
  3647. ch->index = queue;
  3648. if (queue < priv->plat->rx_queues_to_use)
  3649. ch->has_rx = true;
  3650. if (queue < priv->plat->tx_queues_to_use)
  3651. ch->has_tx = true;
  3652. netif_napi_add(ndev, &ch->napi, stmmac_napi_poll,
  3653. NAPI_POLL_WEIGHT);
  3654. }
  3655. mutex_init(&priv->lock);
  3656. /* If a specific clk_csr value is passed from the platform
  3657. * this means that the CSR Clock Range selection cannot be
  3658. * changed at run-time and it is fixed. Viceversa the driver'll try to
  3659. * set the MDC clock dynamically according to the csr actual
  3660. * clock input.
  3661. */
  3662. if (!priv->plat->clk_csr)
  3663. stmmac_clk_csr_set(priv);
  3664. else
  3665. priv->clk_csr = priv->plat->clk_csr;
  3666. stmmac_check_pcs_mode(priv);
  3667. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  3668. priv->hw->pcs != STMMAC_PCS_TBI &&
  3669. priv->hw->pcs != STMMAC_PCS_RTBI) {
  3670. /* MDIO bus Registration */
  3671. ret = stmmac_mdio_register(ndev);
  3672. if (ret < 0) {
  3673. dev_err(priv->device,
  3674. "%s: MDIO bus (id: %d) registration failed",
  3675. __func__, priv->plat->bus_id);
  3676. goto error_mdio_register;
  3677. }
  3678. }
  3679. ret = register_netdev(ndev);
  3680. if (ret) {
  3681. dev_err(priv->device, "%s: ERROR %i registering the device\n",
  3682. __func__, ret);
  3683. goto error_netdev_register;
  3684. }
  3685. #ifdef CONFIG_DEBUG_FS
  3686. ret = stmmac_init_fs(ndev);
  3687. if (ret < 0)
  3688. netdev_warn(priv->dev, "%s: failed debugFS registration\n",
  3689. __func__);
  3690. #endif
  3691. return ret;
  3692. error_netdev_register:
  3693. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  3694. priv->hw->pcs != STMMAC_PCS_TBI &&
  3695. priv->hw->pcs != STMMAC_PCS_RTBI)
  3696. stmmac_mdio_unregister(ndev);
  3697. error_mdio_register:
  3698. for (queue = 0; queue < maxq; queue++) {
  3699. struct stmmac_channel *ch = &priv->channel[queue];
  3700. netif_napi_del(&ch->napi);
  3701. }
  3702. error_hw_init:
  3703. destroy_workqueue(priv->wq);
  3704. error_wq:
  3705. free_netdev(ndev);
  3706. return ret;
  3707. }
  3708. EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
  3709. /**
  3710. * stmmac_dvr_remove
  3711. * @dev: device pointer
  3712. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  3713. * changes the link status, releases the DMA descriptor rings.
  3714. */
  3715. int stmmac_dvr_remove(struct device *dev)
  3716. {
  3717. struct net_device *ndev = dev_get_drvdata(dev);
  3718. struct stmmac_priv *priv = netdev_priv(ndev);
  3719. netdev_info(priv->dev, "%s: removing driver", __func__);
  3720. #ifdef CONFIG_DEBUG_FS
  3721. stmmac_exit_fs(ndev);
  3722. #endif
  3723. stmmac_stop_all_dma(priv);
  3724. stmmac_mac_set(priv, priv->ioaddr, false);
  3725. netif_carrier_off(ndev);
  3726. unregister_netdev(ndev);
  3727. if (priv->plat->stmmac_rst)
  3728. reset_control_assert(priv->plat->stmmac_rst);
  3729. clk_disable_unprepare(priv->plat->pclk);
  3730. clk_disable_unprepare(priv->plat->stmmac_clk);
  3731. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  3732. priv->hw->pcs != STMMAC_PCS_TBI &&
  3733. priv->hw->pcs != STMMAC_PCS_RTBI)
  3734. stmmac_mdio_unregister(ndev);
  3735. destroy_workqueue(priv->wq);
  3736. mutex_destroy(&priv->lock);
  3737. free_netdev(ndev);
  3738. return 0;
  3739. }
  3740. EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
  3741. /**
  3742. * stmmac_suspend - suspend callback
  3743. * @dev: device pointer
  3744. * Description: this is the function to suspend the device and it is called
  3745. * by the platform driver to stop the network queue, release the resources,
  3746. * program the PMT register (for WoL), clean and release driver resources.
  3747. */
  3748. int stmmac_suspend(struct device *dev)
  3749. {
  3750. struct net_device *ndev = dev_get_drvdata(dev);
  3751. struct stmmac_priv *priv = netdev_priv(ndev);
  3752. if (!ndev || !netif_running(ndev))
  3753. return 0;
  3754. if (ndev->phydev)
  3755. phy_stop(ndev->phydev);
  3756. mutex_lock(&priv->lock);
  3757. netif_device_detach(ndev);
  3758. stmmac_stop_all_queues(priv);
  3759. stmmac_disable_all_queues(priv);
  3760. /* Stop TX/RX DMA */
  3761. stmmac_stop_all_dma(priv);
  3762. /* Enable Power down mode by programming the PMT regs */
  3763. if (device_may_wakeup(priv->device)) {
  3764. stmmac_pmt(priv, priv->hw, priv->wolopts);
  3765. priv->irq_wake = 1;
  3766. } else {
  3767. stmmac_mac_set(priv, priv->ioaddr, false);
  3768. pinctrl_pm_select_sleep_state(priv->device);
  3769. /* Disable clock in case of PWM is off */
  3770. clk_disable(priv->plat->pclk);
  3771. clk_disable(priv->plat->stmmac_clk);
  3772. }
  3773. mutex_unlock(&priv->lock);
  3774. priv->oldlink = false;
  3775. priv->speed = SPEED_UNKNOWN;
  3776. priv->oldduplex = DUPLEX_UNKNOWN;
  3777. return 0;
  3778. }
  3779. EXPORT_SYMBOL_GPL(stmmac_suspend);
  3780. /**
  3781. * stmmac_reset_queues_param - reset queue parameters
  3782. * @dev: device pointer
  3783. */
  3784. static void stmmac_reset_queues_param(struct stmmac_priv *priv)
  3785. {
  3786. u32 rx_cnt = priv->plat->rx_queues_to_use;
  3787. u32 tx_cnt = priv->plat->tx_queues_to_use;
  3788. u32 queue;
  3789. for (queue = 0; queue < rx_cnt; queue++) {
  3790. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  3791. rx_q->cur_rx = 0;
  3792. rx_q->dirty_rx = 0;
  3793. }
  3794. for (queue = 0; queue < tx_cnt; queue++) {
  3795. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  3796. tx_q->cur_tx = 0;
  3797. tx_q->dirty_tx = 0;
  3798. tx_q->mss = 0;
  3799. }
  3800. }
  3801. /**
  3802. * stmmac_resume - resume callback
  3803. * @dev: device pointer
  3804. * Description: when resume this function is invoked to setup the DMA and CORE
  3805. * in a usable state.
  3806. */
  3807. int stmmac_resume(struct device *dev)
  3808. {
  3809. struct net_device *ndev = dev_get_drvdata(dev);
  3810. struct stmmac_priv *priv = netdev_priv(ndev);
  3811. if (!netif_running(ndev))
  3812. return 0;
  3813. /* Power Down bit, into the PM register, is cleared
  3814. * automatically as soon as a magic packet or a Wake-up frame
  3815. * is received. Anyway, it's better to manually clear
  3816. * this bit because it can generate problems while resuming
  3817. * from another devices (e.g. serial console).
  3818. */
  3819. if (device_may_wakeup(priv->device)) {
  3820. mutex_lock(&priv->lock);
  3821. stmmac_pmt(priv, priv->hw, 0);
  3822. mutex_unlock(&priv->lock);
  3823. priv->irq_wake = 0;
  3824. } else {
  3825. pinctrl_pm_select_default_state(priv->device);
  3826. /* enable the clk previously disabled */
  3827. clk_enable(priv->plat->stmmac_clk);
  3828. clk_enable(priv->plat->pclk);
  3829. /* reset the phy so that it's ready */
  3830. if (priv->mii)
  3831. stmmac_mdio_reset(priv->mii);
  3832. }
  3833. netif_device_attach(ndev);
  3834. mutex_lock(&priv->lock);
  3835. stmmac_reset_queues_param(priv);
  3836. stmmac_clear_descriptors(priv);
  3837. stmmac_hw_setup(ndev, false);
  3838. stmmac_init_tx_coalesce(priv);
  3839. stmmac_set_rx_mode(ndev);
  3840. stmmac_enable_all_queues(priv);
  3841. stmmac_start_all_queues(priv);
  3842. mutex_unlock(&priv->lock);
  3843. if (ndev->phydev)
  3844. phy_start(ndev->phydev);
  3845. return 0;
  3846. }
  3847. EXPORT_SYMBOL_GPL(stmmac_resume);
  3848. #ifndef MODULE
  3849. static int __init stmmac_cmdline_opt(char *str)
  3850. {
  3851. char *opt;
  3852. if (!str || !*str)
  3853. return -EINVAL;
  3854. while ((opt = strsep(&str, ",")) != NULL) {
  3855. if (!strncmp(opt, "debug:", 6)) {
  3856. if (kstrtoint(opt + 6, 0, &debug))
  3857. goto err;
  3858. } else if (!strncmp(opt, "phyaddr:", 8)) {
  3859. if (kstrtoint(opt + 8, 0, &phyaddr))
  3860. goto err;
  3861. } else if (!strncmp(opt, "buf_sz:", 7)) {
  3862. if (kstrtoint(opt + 7, 0, &buf_sz))
  3863. goto err;
  3864. } else if (!strncmp(opt, "tc:", 3)) {
  3865. if (kstrtoint(opt + 3, 0, &tc))
  3866. goto err;
  3867. } else if (!strncmp(opt, "watchdog:", 9)) {
  3868. if (kstrtoint(opt + 9, 0, &watchdog))
  3869. goto err;
  3870. } else if (!strncmp(opt, "flow_ctrl:", 10)) {
  3871. if (kstrtoint(opt + 10, 0, &flow_ctrl))
  3872. goto err;
  3873. } else if (!strncmp(opt, "pause:", 6)) {
  3874. if (kstrtoint(opt + 6, 0, &pause))
  3875. goto err;
  3876. } else if (!strncmp(opt, "eee_timer:", 10)) {
  3877. if (kstrtoint(opt + 10, 0, &eee_timer))
  3878. goto err;
  3879. } else if (!strncmp(opt, "chain_mode:", 11)) {
  3880. if (kstrtoint(opt + 11, 0, &chain_mode))
  3881. goto err;
  3882. }
  3883. }
  3884. return 0;
  3885. err:
  3886. pr_err("%s: ERROR broken module parameter conversion", __func__);
  3887. return -EINVAL;
  3888. }
  3889. __setup("stmmaceth=", stmmac_cmdline_opt);
  3890. #endif /* MODULE */
  3891. static int __init stmmac_init(void)
  3892. {
  3893. #ifdef CONFIG_DEBUG_FS
  3894. /* Create debugfs main directory if it doesn't exist yet */
  3895. if (!stmmac_fs_dir) {
  3896. stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
  3897. if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
  3898. pr_err("ERROR %s, debugfs create directory failed\n",
  3899. STMMAC_RESOURCE_NAME);
  3900. return -ENOMEM;
  3901. }
  3902. }
  3903. #endif
  3904. return 0;
  3905. }
  3906. static void __exit stmmac_exit(void)
  3907. {
  3908. #ifdef CONFIG_DEBUG_FS
  3909. debugfs_remove_recursive(stmmac_fs_dir);
  3910. #endif
  3911. }
  3912. module_init(stmmac_init)
  3913. module_exit(stmmac_exit)
  3914. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
  3915. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  3916. MODULE_LICENSE("GPL");