stmmac_main.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701
  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 = 0;
  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 = 0;
  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 sec_inc = 0;
  486. u32 value = 0;
  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. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  853. SUPPORTED_1000baseT_Full);
  854. /*
  855. * Half-duplex mode not supported with multiqueue
  856. * half-duplex can only works with single queue
  857. */
  858. if (tx_cnt > 1)
  859. phydev->supported &= ~(SUPPORTED_1000baseT_Half |
  860. SUPPORTED_100baseT_Half |
  861. SUPPORTED_10baseT_Half);
  862. /*
  863. * Broken HW is sometimes missing the pull-up resistor on the
  864. * MDIO line, which results in reads to non-existent devices returning
  865. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  866. * device as well.
  867. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  868. */
  869. if (!priv->plat->phy_node && phydev->phy_id == 0) {
  870. phy_disconnect(phydev);
  871. return -ENODEV;
  872. }
  873. /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
  874. * subsequent PHY polling, make sure we force a link transition if
  875. * we have a UP/DOWN/UP transition
  876. */
  877. if (phydev->is_pseudo_fixed_link)
  878. phydev->irq = PHY_POLL;
  879. phy_attached_info(phydev);
  880. return 0;
  881. }
  882. static void stmmac_display_rx_rings(struct stmmac_priv *priv)
  883. {
  884. u32 rx_cnt = priv->plat->rx_queues_to_use;
  885. void *head_rx;
  886. u32 queue;
  887. /* Display RX rings */
  888. for (queue = 0; queue < rx_cnt; queue++) {
  889. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  890. pr_info("\tRX Queue %u rings\n", queue);
  891. if (priv->extend_desc)
  892. head_rx = (void *)rx_q->dma_erx;
  893. else
  894. head_rx = (void *)rx_q->dma_rx;
  895. /* Display RX ring */
  896. stmmac_display_ring(priv, head_rx, DMA_RX_SIZE, true);
  897. }
  898. }
  899. static void stmmac_display_tx_rings(struct stmmac_priv *priv)
  900. {
  901. u32 tx_cnt = priv->plat->tx_queues_to_use;
  902. void *head_tx;
  903. u32 queue;
  904. /* Display TX rings */
  905. for (queue = 0; queue < tx_cnt; queue++) {
  906. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  907. pr_info("\tTX Queue %d rings\n", queue);
  908. if (priv->extend_desc)
  909. head_tx = (void *)tx_q->dma_etx;
  910. else
  911. head_tx = (void *)tx_q->dma_tx;
  912. stmmac_display_ring(priv, head_tx, DMA_TX_SIZE, false);
  913. }
  914. }
  915. static void stmmac_display_rings(struct stmmac_priv *priv)
  916. {
  917. /* Display RX ring */
  918. stmmac_display_rx_rings(priv);
  919. /* Display TX ring */
  920. stmmac_display_tx_rings(priv);
  921. }
  922. static int stmmac_set_bfsize(int mtu, int bufsize)
  923. {
  924. int ret = bufsize;
  925. if (mtu >= BUF_SIZE_4KiB)
  926. ret = BUF_SIZE_8KiB;
  927. else if (mtu >= BUF_SIZE_2KiB)
  928. ret = BUF_SIZE_4KiB;
  929. else if (mtu > DEFAULT_BUFSIZE)
  930. ret = BUF_SIZE_2KiB;
  931. else
  932. ret = DEFAULT_BUFSIZE;
  933. return ret;
  934. }
  935. /**
  936. * stmmac_clear_rx_descriptors - clear RX descriptors
  937. * @priv: driver private structure
  938. * @queue: RX queue index
  939. * Description: this function is called to clear the RX descriptors
  940. * in case of both basic and extended descriptors are used.
  941. */
  942. static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
  943. {
  944. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  945. int i;
  946. /* Clear the RX descriptors */
  947. for (i = 0; i < DMA_RX_SIZE; i++)
  948. if (priv->extend_desc)
  949. stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
  950. priv->use_riwt, priv->mode,
  951. (i == DMA_RX_SIZE - 1),
  952. priv->dma_buf_sz);
  953. else
  954. stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
  955. priv->use_riwt, priv->mode,
  956. (i == DMA_RX_SIZE - 1),
  957. priv->dma_buf_sz);
  958. }
  959. /**
  960. * stmmac_clear_tx_descriptors - clear tx descriptors
  961. * @priv: driver private structure
  962. * @queue: TX queue index.
  963. * Description: this function is called to clear the TX descriptors
  964. * in case of both basic and extended descriptors are used.
  965. */
  966. static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
  967. {
  968. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  969. int i;
  970. /* Clear the TX descriptors */
  971. for (i = 0; i < DMA_TX_SIZE; i++)
  972. if (priv->extend_desc)
  973. stmmac_init_tx_desc(priv, &tx_q->dma_etx[i].basic,
  974. priv->mode, (i == DMA_TX_SIZE - 1));
  975. else
  976. stmmac_init_tx_desc(priv, &tx_q->dma_tx[i],
  977. priv->mode, (i == DMA_TX_SIZE - 1));
  978. }
  979. /**
  980. * stmmac_clear_descriptors - clear descriptors
  981. * @priv: driver private structure
  982. * Description: this function is called to clear the TX and RX descriptors
  983. * in case of both basic and extended descriptors are used.
  984. */
  985. static void stmmac_clear_descriptors(struct stmmac_priv *priv)
  986. {
  987. u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
  988. u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
  989. u32 queue;
  990. /* Clear the RX descriptors */
  991. for (queue = 0; queue < rx_queue_cnt; queue++)
  992. stmmac_clear_rx_descriptors(priv, queue);
  993. /* Clear the TX descriptors */
  994. for (queue = 0; queue < tx_queue_cnt; queue++)
  995. stmmac_clear_tx_descriptors(priv, queue);
  996. }
  997. /**
  998. * stmmac_init_rx_buffers - init the RX descriptor buffer.
  999. * @priv: driver private structure
  1000. * @p: descriptor pointer
  1001. * @i: descriptor index
  1002. * @flags: gfp flag
  1003. * @queue: RX queue index
  1004. * Description: this function is called to allocate a receive buffer, perform
  1005. * the DMA mapping and init the descriptor.
  1006. */
  1007. static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
  1008. int i, gfp_t flags, u32 queue)
  1009. {
  1010. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1011. struct sk_buff *skb;
  1012. skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
  1013. if (!skb) {
  1014. netdev_err(priv->dev,
  1015. "%s: Rx init fails; skb is NULL\n", __func__);
  1016. return -ENOMEM;
  1017. }
  1018. rx_q->rx_skbuff[i] = skb;
  1019. rx_q->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  1020. priv->dma_buf_sz,
  1021. DMA_FROM_DEVICE);
  1022. if (dma_mapping_error(priv->device, rx_q->rx_skbuff_dma[i])) {
  1023. netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
  1024. dev_kfree_skb_any(skb);
  1025. return -EINVAL;
  1026. }
  1027. stmmac_set_desc_addr(priv, p, rx_q->rx_skbuff_dma[i]);
  1028. if (priv->dma_buf_sz == BUF_SIZE_16KiB)
  1029. stmmac_init_desc3(priv, p);
  1030. return 0;
  1031. }
  1032. /**
  1033. * stmmac_free_rx_buffer - free RX dma buffers
  1034. * @priv: private structure
  1035. * @queue: RX queue index
  1036. * @i: buffer index.
  1037. */
  1038. static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
  1039. {
  1040. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1041. if (rx_q->rx_skbuff[i]) {
  1042. dma_unmap_single(priv->device, rx_q->rx_skbuff_dma[i],
  1043. priv->dma_buf_sz, DMA_FROM_DEVICE);
  1044. dev_kfree_skb_any(rx_q->rx_skbuff[i]);
  1045. }
  1046. rx_q->rx_skbuff[i] = NULL;
  1047. }
  1048. /**
  1049. * stmmac_free_tx_buffer - free RX dma buffers
  1050. * @priv: private structure
  1051. * @queue: RX queue index
  1052. * @i: buffer index.
  1053. */
  1054. static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
  1055. {
  1056. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1057. if (tx_q->tx_skbuff_dma[i].buf) {
  1058. if (tx_q->tx_skbuff_dma[i].map_as_page)
  1059. dma_unmap_page(priv->device,
  1060. tx_q->tx_skbuff_dma[i].buf,
  1061. tx_q->tx_skbuff_dma[i].len,
  1062. DMA_TO_DEVICE);
  1063. else
  1064. dma_unmap_single(priv->device,
  1065. tx_q->tx_skbuff_dma[i].buf,
  1066. tx_q->tx_skbuff_dma[i].len,
  1067. DMA_TO_DEVICE);
  1068. }
  1069. if (tx_q->tx_skbuff[i]) {
  1070. dev_kfree_skb_any(tx_q->tx_skbuff[i]);
  1071. tx_q->tx_skbuff[i] = NULL;
  1072. tx_q->tx_skbuff_dma[i].buf = 0;
  1073. tx_q->tx_skbuff_dma[i].map_as_page = false;
  1074. }
  1075. }
  1076. /**
  1077. * init_dma_rx_desc_rings - init the RX descriptor rings
  1078. * @dev: net device structure
  1079. * @flags: gfp flag.
  1080. * Description: this function initializes the DMA RX descriptors
  1081. * and allocates the socket buffers. It supports the chained and ring
  1082. * modes.
  1083. */
  1084. static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
  1085. {
  1086. struct stmmac_priv *priv = netdev_priv(dev);
  1087. u32 rx_count = priv->plat->rx_queues_to_use;
  1088. int ret = -ENOMEM;
  1089. int bfsize = 0;
  1090. int queue;
  1091. int i;
  1092. bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
  1093. if (bfsize < 0)
  1094. bfsize = 0;
  1095. if (bfsize < BUF_SIZE_16KiB)
  1096. bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
  1097. priv->dma_buf_sz = bfsize;
  1098. /* RX INITIALIZATION */
  1099. netif_dbg(priv, probe, priv->dev,
  1100. "SKB addresses:\nskb\t\tskb data\tdma data\n");
  1101. for (queue = 0; queue < rx_count; queue++) {
  1102. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1103. netif_dbg(priv, probe, priv->dev,
  1104. "(%s) dma_rx_phy=0x%08x\n", __func__,
  1105. (u32)rx_q->dma_rx_phy);
  1106. for (i = 0; i < DMA_RX_SIZE; i++) {
  1107. struct dma_desc *p;
  1108. if (priv->extend_desc)
  1109. p = &((rx_q->dma_erx + i)->basic);
  1110. else
  1111. p = rx_q->dma_rx + i;
  1112. ret = stmmac_init_rx_buffers(priv, p, i, flags,
  1113. queue);
  1114. if (ret)
  1115. goto err_init_rx_buffers;
  1116. netif_dbg(priv, probe, priv->dev, "[%p]\t[%p]\t[%x]\n",
  1117. rx_q->rx_skbuff[i], rx_q->rx_skbuff[i]->data,
  1118. (unsigned int)rx_q->rx_skbuff_dma[i]);
  1119. }
  1120. rx_q->cur_rx = 0;
  1121. rx_q->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
  1122. stmmac_clear_rx_descriptors(priv, queue);
  1123. /* Setup the chained descriptor addresses */
  1124. if (priv->mode == STMMAC_CHAIN_MODE) {
  1125. if (priv->extend_desc)
  1126. stmmac_mode_init(priv, rx_q->dma_erx,
  1127. rx_q->dma_rx_phy, DMA_RX_SIZE, 1);
  1128. else
  1129. stmmac_mode_init(priv, rx_q->dma_rx,
  1130. rx_q->dma_rx_phy, DMA_RX_SIZE, 0);
  1131. }
  1132. }
  1133. buf_sz = bfsize;
  1134. return 0;
  1135. err_init_rx_buffers:
  1136. while (queue >= 0) {
  1137. while (--i >= 0)
  1138. stmmac_free_rx_buffer(priv, queue, i);
  1139. if (queue == 0)
  1140. break;
  1141. i = DMA_RX_SIZE;
  1142. queue--;
  1143. }
  1144. return ret;
  1145. }
  1146. /**
  1147. * init_dma_tx_desc_rings - init the TX descriptor rings
  1148. * @dev: net device structure.
  1149. * Description: this function initializes the DMA TX descriptors
  1150. * and allocates the socket buffers. It supports the chained and ring
  1151. * modes.
  1152. */
  1153. static int init_dma_tx_desc_rings(struct net_device *dev)
  1154. {
  1155. struct stmmac_priv *priv = netdev_priv(dev);
  1156. u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
  1157. u32 queue;
  1158. int i;
  1159. for (queue = 0; queue < tx_queue_cnt; queue++) {
  1160. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1161. netif_dbg(priv, probe, priv->dev,
  1162. "(%s) dma_tx_phy=0x%08x\n", __func__,
  1163. (u32)tx_q->dma_tx_phy);
  1164. /* Setup the chained descriptor addresses */
  1165. if (priv->mode == STMMAC_CHAIN_MODE) {
  1166. if (priv->extend_desc)
  1167. stmmac_mode_init(priv, tx_q->dma_etx,
  1168. tx_q->dma_tx_phy, DMA_TX_SIZE, 1);
  1169. else
  1170. stmmac_mode_init(priv, tx_q->dma_tx,
  1171. tx_q->dma_tx_phy, DMA_TX_SIZE, 0);
  1172. }
  1173. for (i = 0; i < DMA_TX_SIZE; i++) {
  1174. struct dma_desc *p;
  1175. if (priv->extend_desc)
  1176. p = &((tx_q->dma_etx + i)->basic);
  1177. else
  1178. p = tx_q->dma_tx + i;
  1179. stmmac_clear_desc(priv, p);
  1180. tx_q->tx_skbuff_dma[i].buf = 0;
  1181. tx_q->tx_skbuff_dma[i].map_as_page = false;
  1182. tx_q->tx_skbuff_dma[i].len = 0;
  1183. tx_q->tx_skbuff_dma[i].last_segment = false;
  1184. tx_q->tx_skbuff[i] = NULL;
  1185. }
  1186. tx_q->dirty_tx = 0;
  1187. tx_q->cur_tx = 0;
  1188. tx_q->mss = 0;
  1189. netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
  1190. }
  1191. return 0;
  1192. }
  1193. /**
  1194. * init_dma_desc_rings - init the RX/TX descriptor rings
  1195. * @dev: net device structure
  1196. * @flags: gfp flag.
  1197. * Description: this function initializes the DMA RX/TX descriptors
  1198. * and allocates the socket buffers. It supports the chained and ring
  1199. * modes.
  1200. */
  1201. static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
  1202. {
  1203. struct stmmac_priv *priv = netdev_priv(dev);
  1204. int ret;
  1205. ret = init_dma_rx_desc_rings(dev, flags);
  1206. if (ret)
  1207. return ret;
  1208. ret = init_dma_tx_desc_rings(dev);
  1209. stmmac_clear_descriptors(priv);
  1210. if (netif_msg_hw(priv))
  1211. stmmac_display_rings(priv);
  1212. return ret;
  1213. }
  1214. /**
  1215. * dma_free_rx_skbufs - free RX dma buffers
  1216. * @priv: private structure
  1217. * @queue: RX queue index
  1218. */
  1219. static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
  1220. {
  1221. int i;
  1222. for (i = 0; i < DMA_RX_SIZE; i++)
  1223. stmmac_free_rx_buffer(priv, queue, i);
  1224. }
  1225. /**
  1226. * dma_free_tx_skbufs - free TX dma buffers
  1227. * @priv: private structure
  1228. * @queue: TX queue index
  1229. */
  1230. static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
  1231. {
  1232. int i;
  1233. for (i = 0; i < DMA_TX_SIZE; i++)
  1234. stmmac_free_tx_buffer(priv, queue, i);
  1235. }
  1236. /**
  1237. * free_dma_rx_desc_resources - free RX dma desc resources
  1238. * @priv: private structure
  1239. */
  1240. static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
  1241. {
  1242. u32 rx_count = priv->plat->rx_queues_to_use;
  1243. u32 queue;
  1244. /* Free RX queue resources */
  1245. for (queue = 0; queue < rx_count; queue++) {
  1246. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1247. /* Release the DMA RX socket buffers */
  1248. dma_free_rx_skbufs(priv, queue);
  1249. /* Free DMA regions of consistent memory previously allocated */
  1250. if (!priv->extend_desc)
  1251. dma_free_coherent(priv->device,
  1252. DMA_RX_SIZE * sizeof(struct dma_desc),
  1253. rx_q->dma_rx, rx_q->dma_rx_phy);
  1254. else
  1255. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1256. sizeof(struct dma_extended_desc),
  1257. rx_q->dma_erx, rx_q->dma_rx_phy);
  1258. kfree(rx_q->rx_skbuff_dma);
  1259. kfree(rx_q->rx_skbuff);
  1260. }
  1261. }
  1262. /**
  1263. * free_dma_tx_desc_resources - free TX dma desc resources
  1264. * @priv: private structure
  1265. */
  1266. static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
  1267. {
  1268. u32 tx_count = priv->plat->tx_queues_to_use;
  1269. u32 queue;
  1270. /* Free TX queue resources */
  1271. for (queue = 0; queue < tx_count; queue++) {
  1272. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1273. /* Release the DMA TX socket buffers */
  1274. dma_free_tx_skbufs(priv, queue);
  1275. /* Free DMA regions of consistent memory previously allocated */
  1276. if (!priv->extend_desc)
  1277. dma_free_coherent(priv->device,
  1278. DMA_TX_SIZE * sizeof(struct dma_desc),
  1279. tx_q->dma_tx, tx_q->dma_tx_phy);
  1280. else
  1281. dma_free_coherent(priv->device, DMA_TX_SIZE *
  1282. sizeof(struct dma_extended_desc),
  1283. tx_q->dma_etx, tx_q->dma_tx_phy);
  1284. kfree(tx_q->tx_skbuff_dma);
  1285. kfree(tx_q->tx_skbuff);
  1286. }
  1287. }
  1288. /**
  1289. * alloc_dma_rx_desc_resources - alloc RX resources.
  1290. * @priv: private structure
  1291. * Description: according to which descriptor can be used (extend or basic)
  1292. * this function allocates the resources for TX and RX paths. In case of
  1293. * reception, for example, it pre-allocated the RX socket buffer in order to
  1294. * allow zero-copy mechanism.
  1295. */
  1296. static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
  1297. {
  1298. u32 rx_count = priv->plat->rx_queues_to_use;
  1299. int ret = -ENOMEM;
  1300. u32 queue;
  1301. /* RX queues buffers and DMA */
  1302. for (queue = 0; queue < rx_count; queue++) {
  1303. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  1304. rx_q->queue_index = queue;
  1305. rx_q->priv_data = priv;
  1306. rx_q->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE,
  1307. sizeof(dma_addr_t),
  1308. GFP_KERNEL);
  1309. if (!rx_q->rx_skbuff_dma)
  1310. goto err_dma;
  1311. rx_q->rx_skbuff = kmalloc_array(DMA_RX_SIZE,
  1312. sizeof(struct sk_buff *),
  1313. GFP_KERNEL);
  1314. if (!rx_q->rx_skbuff)
  1315. goto err_dma;
  1316. if (priv->extend_desc) {
  1317. rx_q->dma_erx = dma_zalloc_coherent(priv->device,
  1318. DMA_RX_SIZE *
  1319. sizeof(struct
  1320. dma_extended_desc),
  1321. &rx_q->dma_rx_phy,
  1322. GFP_KERNEL);
  1323. if (!rx_q->dma_erx)
  1324. goto err_dma;
  1325. } else {
  1326. rx_q->dma_rx = dma_zalloc_coherent(priv->device,
  1327. DMA_RX_SIZE *
  1328. sizeof(struct
  1329. dma_desc),
  1330. &rx_q->dma_rx_phy,
  1331. GFP_KERNEL);
  1332. if (!rx_q->dma_rx)
  1333. goto err_dma;
  1334. }
  1335. }
  1336. return 0;
  1337. err_dma:
  1338. free_dma_rx_desc_resources(priv);
  1339. return ret;
  1340. }
  1341. /**
  1342. * alloc_dma_tx_desc_resources - alloc TX resources.
  1343. * @priv: private structure
  1344. * Description: according to which descriptor can be used (extend or basic)
  1345. * this function allocates the resources for TX and RX paths. In case of
  1346. * reception, for example, it pre-allocated the RX socket buffer in order to
  1347. * allow zero-copy mechanism.
  1348. */
  1349. static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
  1350. {
  1351. u32 tx_count = priv->plat->tx_queues_to_use;
  1352. int ret = -ENOMEM;
  1353. u32 queue;
  1354. /* TX queues buffers and DMA */
  1355. for (queue = 0; queue < tx_count; queue++) {
  1356. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1357. tx_q->queue_index = queue;
  1358. tx_q->priv_data = priv;
  1359. tx_q->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
  1360. sizeof(*tx_q->tx_skbuff_dma),
  1361. GFP_KERNEL);
  1362. if (!tx_q->tx_skbuff_dma)
  1363. goto err_dma;
  1364. tx_q->tx_skbuff = kmalloc_array(DMA_TX_SIZE,
  1365. sizeof(struct sk_buff *),
  1366. GFP_KERNEL);
  1367. if (!tx_q->tx_skbuff)
  1368. goto err_dma;
  1369. if (priv->extend_desc) {
  1370. tx_q->dma_etx = dma_zalloc_coherent(priv->device,
  1371. DMA_TX_SIZE *
  1372. sizeof(struct
  1373. dma_extended_desc),
  1374. &tx_q->dma_tx_phy,
  1375. GFP_KERNEL);
  1376. if (!tx_q->dma_etx)
  1377. goto err_dma;
  1378. } else {
  1379. tx_q->dma_tx = dma_zalloc_coherent(priv->device,
  1380. DMA_TX_SIZE *
  1381. sizeof(struct
  1382. dma_desc),
  1383. &tx_q->dma_tx_phy,
  1384. GFP_KERNEL);
  1385. if (!tx_q->dma_tx)
  1386. goto err_dma;
  1387. }
  1388. }
  1389. return 0;
  1390. err_dma:
  1391. free_dma_tx_desc_resources(priv);
  1392. return ret;
  1393. }
  1394. /**
  1395. * alloc_dma_desc_resources - alloc TX/RX resources.
  1396. * @priv: private structure
  1397. * Description: according to which descriptor can be used (extend or basic)
  1398. * this function allocates the resources for TX and RX paths. In case of
  1399. * reception, for example, it pre-allocated the RX socket buffer in order to
  1400. * allow zero-copy mechanism.
  1401. */
  1402. static int alloc_dma_desc_resources(struct stmmac_priv *priv)
  1403. {
  1404. /* RX Allocation */
  1405. int ret = alloc_dma_rx_desc_resources(priv);
  1406. if (ret)
  1407. return ret;
  1408. ret = alloc_dma_tx_desc_resources(priv);
  1409. return ret;
  1410. }
  1411. /**
  1412. * free_dma_desc_resources - free dma desc resources
  1413. * @priv: private structure
  1414. */
  1415. static void free_dma_desc_resources(struct stmmac_priv *priv)
  1416. {
  1417. /* Release the DMA RX socket buffers */
  1418. free_dma_rx_desc_resources(priv);
  1419. /* Release the DMA TX socket buffers */
  1420. free_dma_tx_desc_resources(priv);
  1421. }
  1422. /**
  1423. * stmmac_mac_enable_rx_queues - Enable MAC rx queues
  1424. * @priv: driver private structure
  1425. * Description: It is used for enabling the rx queues in the MAC
  1426. */
  1427. static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
  1428. {
  1429. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  1430. int queue;
  1431. u8 mode;
  1432. for (queue = 0; queue < rx_queues_count; queue++) {
  1433. mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
  1434. stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
  1435. }
  1436. }
  1437. /**
  1438. * stmmac_start_rx_dma - start RX DMA channel
  1439. * @priv: driver private structure
  1440. * @chan: RX channel index
  1441. * Description:
  1442. * This starts a RX DMA channel
  1443. */
  1444. static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
  1445. {
  1446. netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
  1447. stmmac_start_rx(priv, priv->ioaddr, chan);
  1448. }
  1449. /**
  1450. * stmmac_start_tx_dma - start TX DMA channel
  1451. * @priv: driver private structure
  1452. * @chan: TX channel index
  1453. * Description:
  1454. * This starts a TX DMA channel
  1455. */
  1456. static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
  1457. {
  1458. netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
  1459. stmmac_start_tx(priv, priv->ioaddr, chan);
  1460. }
  1461. /**
  1462. * stmmac_stop_rx_dma - stop RX DMA channel
  1463. * @priv: driver private structure
  1464. * @chan: RX channel index
  1465. * Description:
  1466. * This stops a RX DMA channel
  1467. */
  1468. static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
  1469. {
  1470. netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
  1471. stmmac_stop_rx(priv, priv->ioaddr, chan);
  1472. }
  1473. /**
  1474. * stmmac_stop_tx_dma - stop TX DMA channel
  1475. * @priv: driver private structure
  1476. * @chan: TX channel index
  1477. * Description:
  1478. * This stops a TX DMA channel
  1479. */
  1480. static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
  1481. {
  1482. netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
  1483. stmmac_stop_tx(priv, priv->ioaddr, chan);
  1484. }
  1485. /**
  1486. * stmmac_start_all_dma - start all RX and TX DMA channels
  1487. * @priv: driver private structure
  1488. * Description:
  1489. * This starts all the RX and TX DMA channels
  1490. */
  1491. static void stmmac_start_all_dma(struct stmmac_priv *priv)
  1492. {
  1493. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1494. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1495. u32 chan = 0;
  1496. for (chan = 0; chan < rx_channels_count; chan++)
  1497. stmmac_start_rx_dma(priv, chan);
  1498. for (chan = 0; chan < tx_channels_count; chan++)
  1499. stmmac_start_tx_dma(priv, chan);
  1500. }
  1501. /**
  1502. * stmmac_stop_all_dma - stop all RX and TX DMA channels
  1503. * @priv: driver private structure
  1504. * Description:
  1505. * This stops the RX and TX DMA channels
  1506. */
  1507. static void stmmac_stop_all_dma(struct stmmac_priv *priv)
  1508. {
  1509. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1510. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1511. u32 chan = 0;
  1512. for (chan = 0; chan < rx_channels_count; chan++)
  1513. stmmac_stop_rx_dma(priv, chan);
  1514. for (chan = 0; chan < tx_channels_count; chan++)
  1515. stmmac_stop_tx_dma(priv, chan);
  1516. }
  1517. /**
  1518. * stmmac_dma_operation_mode - HW DMA operation mode
  1519. * @priv: driver private structure
  1520. * Description: it is used for configuring the DMA operation mode register in
  1521. * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
  1522. */
  1523. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  1524. {
  1525. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1526. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1527. int rxfifosz = priv->plat->rx_fifo_size;
  1528. int txfifosz = priv->plat->tx_fifo_size;
  1529. u32 txmode = 0;
  1530. u32 rxmode = 0;
  1531. u32 chan = 0;
  1532. u8 qmode = 0;
  1533. if (rxfifosz == 0)
  1534. rxfifosz = priv->dma_cap.rx_fifo_size;
  1535. if (txfifosz == 0)
  1536. txfifosz = priv->dma_cap.tx_fifo_size;
  1537. /* Adjust for real per queue fifo size */
  1538. rxfifosz /= rx_channels_count;
  1539. txfifosz /= tx_channels_count;
  1540. if (priv->plat->force_thresh_dma_mode) {
  1541. txmode = tc;
  1542. rxmode = tc;
  1543. } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
  1544. /*
  1545. * In case of GMAC, SF mode can be enabled
  1546. * to perform the TX COE in HW. This depends on:
  1547. * 1) TX COE if actually supported
  1548. * 2) There is no bugged Jumbo frame support
  1549. * that needs to not insert csum in the TDES.
  1550. */
  1551. txmode = SF_DMA_MODE;
  1552. rxmode = SF_DMA_MODE;
  1553. priv->xstats.threshold = SF_DMA_MODE;
  1554. } else {
  1555. txmode = tc;
  1556. rxmode = SF_DMA_MODE;
  1557. }
  1558. /* configure all channels */
  1559. for (chan = 0; chan < rx_channels_count; chan++) {
  1560. qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
  1561. stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
  1562. rxfifosz, qmode);
  1563. stmmac_set_dma_bfsize(priv, priv->ioaddr, priv->dma_buf_sz,
  1564. chan);
  1565. }
  1566. for (chan = 0; chan < tx_channels_count; chan++) {
  1567. qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
  1568. stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
  1569. txfifosz, qmode);
  1570. }
  1571. }
  1572. /**
  1573. * stmmac_tx_clean - to manage the transmission completion
  1574. * @priv: driver private structure
  1575. * @queue: TX queue index
  1576. * Description: it reclaims the transmit resources after transmission completes.
  1577. */
  1578. static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
  1579. {
  1580. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1581. unsigned int bytes_compl = 0, pkts_compl = 0;
  1582. unsigned int entry, count = 0;
  1583. __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
  1584. priv->xstats.tx_clean++;
  1585. entry = tx_q->dirty_tx;
  1586. while ((entry != tx_q->cur_tx) && (count < budget)) {
  1587. struct sk_buff *skb = tx_q->tx_skbuff[entry];
  1588. struct dma_desc *p;
  1589. int status;
  1590. if (priv->extend_desc)
  1591. p = (struct dma_desc *)(tx_q->dma_etx + entry);
  1592. else
  1593. p = tx_q->dma_tx + entry;
  1594. status = stmmac_tx_status(priv, &priv->dev->stats,
  1595. &priv->xstats, p, priv->ioaddr);
  1596. /* Check if the descriptor is owned by the DMA */
  1597. if (unlikely(status & tx_dma_own))
  1598. break;
  1599. count++;
  1600. /* Make sure descriptor fields are read after reading
  1601. * the own bit.
  1602. */
  1603. dma_rmb();
  1604. /* Just consider the last segment and ...*/
  1605. if (likely(!(status & tx_not_ls))) {
  1606. /* ... verify the status error condition */
  1607. if (unlikely(status & tx_err)) {
  1608. priv->dev->stats.tx_errors++;
  1609. } else {
  1610. priv->dev->stats.tx_packets++;
  1611. priv->xstats.tx_pkt_n++;
  1612. }
  1613. stmmac_get_tx_hwtstamp(priv, p, skb);
  1614. }
  1615. if (likely(tx_q->tx_skbuff_dma[entry].buf)) {
  1616. if (tx_q->tx_skbuff_dma[entry].map_as_page)
  1617. dma_unmap_page(priv->device,
  1618. tx_q->tx_skbuff_dma[entry].buf,
  1619. tx_q->tx_skbuff_dma[entry].len,
  1620. DMA_TO_DEVICE);
  1621. else
  1622. dma_unmap_single(priv->device,
  1623. tx_q->tx_skbuff_dma[entry].buf,
  1624. tx_q->tx_skbuff_dma[entry].len,
  1625. DMA_TO_DEVICE);
  1626. tx_q->tx_skbuff_dma[entry].buf = 0;
  1627. tx_q->tx_skbuff_dma[entry].len = 0;
  1628. tx_q->tx_skbuff_dma[entry].map_as_page = false;
  1629. }
  1630. stmmac_clean_desc3(priv, tx_q, p);
  1631. tx_q->tx_skbuff_dma[entry].last_segment = false;
  1632. tx_q->tx_skbuff_dma[entry].is_jumbo = false;
  1633. if (likely(skb != NULL)) {
  1634. pkts_compl++;
  1635. bytes_compl += skb->len;
  1636. dev_consume_skb_any(skb);
  1637. tx_q->tx_skbuff[entry] = NULL;
  1638. }
  1639. stmmac_release_tx_desc(priv, p, priv->mode);
  1640. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1641. }
  1642. tx_q->dirty_tx = entry;
  1643. netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
  1644. pkts_compl, bytes_compl);
  1645. if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
  1646. queue))) &&
  1647. stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH) {
  1648. netif_dbg(priv, tx_done, priv->dev,
  1649. "%s: restart transmit\n", __func__);
  1650. netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
  1651. }
  1652. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  1653. stmmac_enable_eee_mode(priv);
  1654. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  1655. }
  1656. __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
  1657. return count;
  1658. }
  1659. /**
  1660. * stmmac_tx_err - to manage the tx error
  1661. * @priv: driver private structure
  1662. * @chan: channel index
  1663. * Description: it cleans the descriptors and restarts the transmission
  1664. * in case of transmission errors.
  1665. */
  1666. static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
  1667. {
  1668. struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
  1669. int i;
  1670. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
  1671. stmmac_stop_tx_dma(priv, chan);
  1672. dma_free_tx_skbufs(priv, chan);
  1673. for (i = 0; i < DMA_TX_SIZE; i++)
  1674. if (priv->extend_desc)
  1675. stmmac_init_tx_desc(priv, &tx_q->dma_etx[i].basic,
  1676. priv->mode, (i == DMA_TX_SIZE - 1));
  1677. else
  1678. stmmac_init_tx_desc(priv, &tx_q->dma_tx[i],
  1679. priv->mode, (i == DMA_TX_SIZE - 1));
  1680. tx_q->dirty_tx = 0;
  1681. tx_q->cur_tx = 0;
  1682. tx_q->mss = 0;
  1683. netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
  1684. stmmac_start_tx_dma(priv, chan);
  1685. priv->dev->stats.tx_errors++;
  1686. netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
  1687. }
  1688. /**
  1689. * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
  1690. * @priv: driver private structure
  1691. * @txmode: TX operating mode
  1692. * @rxmode: RX operating mode
  1693. * @chan: channel index
  1694. * Description: it is used for configuring of the DMA operation mode in
  1695. * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
  1696. * mode.
  1697. */
  1698. static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
  1699. u32 rxmode, u32 chan)
  1700. {
  1701. u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
  1702. u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
  1703. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1704. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1705. int rxfifosz = priv->plat->rx_fifo_size;
  1706. int txfifosz = priv->plat->tx_fifo_size;
  1707. if (rxfifosz == 0)
  1708. rxfifosz = priv->dma_cap.rx_fifo_size;
  1709. if (txfifosz == 0)
  1710. txfifosz = priv->dma_cap.tx_fifo_size;
  1711. /* Adjust for real per queue fifo size */
  1712. rxfifosz /= rx_channels_count;
  1713. txfifosz /= tx_channels_count;
  1714. stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
  1715. stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
  1716. }
  1717. static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
  1718. {
  1719. int ret;
  1720. ret = stmmac_safety_feat_irq_status(priv, priv->dev,
  1721. priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
  1722. if (ret && (ret != -EINVAL)) {
  1723. stmmac_global_err(priv);
  1724. return true;
  1725. }
  1726. return false;
  1727. }
  1728. static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan)
  1729. {
  1730. int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
  1731. &priv->xstats, chan);
  1732. struct stmmac_channel *ch = &priv->channel[chan];
  1733. bool needs_work = false;
  1734. if ((status & handle_rx) && ch->has_rx) {
  1735. needs_work = true;
  1736. } else {
  1737. status &= ~handle_rx;
  1738. }
  1739. if ((status & handle_tx) && ch->has_tx) {
  1740. needs_work = true;
  1741. } else {
  1742. status &= ~handle_tx;
  1743. }
  1744. if (needs_work && napi_schedule_prep(&ch->napi)) {
  1745. stmmac_disable_dma_irq(priv, priv->ioaddr, chan);
  1746. __napi_schedule(&ch->napi);
  1747. }
  1748. return status;
  1749. }
  1750. /**
  1751. * stmmac_dma_interrupt - DMA ISR
  1752. * @priv: driver private structure
  1753. * Description: this is the DMA ISR. It is called by the main ISR.
  1754. * It calls the dwmac dma routine and schedule poll method in case of some
  1755. * work can be done.
  1756. */
  1757. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  1758. {
  1759. u32 tx_channel_count = priv->plat->tx_queues_to_use;
  1760. u32 rx_channel_count = priv->plat->rx_queues_to_use;
  1761. u32 channels_to_check = tx_channel_count > rx_channel_count ?
  1762. tx_channel_count : rx_channel_count;
  1763. u32 chan;
  1764. int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
  1765. /* Make sure we never check beyond our status buffer. */
  1766. if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
  1767. channels_to_check = ARRAY_SIZE(status);
  1768. for (chan = 0; chan < channels_to_check; chan++)
  1769. status[chan] = stmmac_napi_check(priv, chan);
  1770. for (chan = 0; chan < tx_channel_count; chan++) {
  1771. if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
  1772. /* Try to bump up the dma threshold on this failure */
  1773. if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
  1774. (tc <= 256)) {
  1775. tc += 64;
  1776. if (priv->plat->force_thresh_dma_mode)
  1777. stmmac_set_dma_operation_mode(priv,
  1778. tc,
  1779. tc,
  1780. chan);
  1781. else
  1782. stmmac_set_dma_operation_mode(priv,
  1783. tc,
  1784. SF_DMA_MODE,
  1785. chan);
  1786. priv->xstats.threshold = tc;
  1787. }
  1788. } else if (unlikely(status[chan] == tx_hard_error)) {
  1789. stmmac_tx_err(priv, chan);
  1790. }
  1791. }
  1792. }
  1793. /**
  1794. * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
  1795. * @priv: driver private structure
  1796. * Description: this masks the MMC irq, in fact, the counters are managed in SW.
  1797. */
  1798. static void stmmac_mmc_setup(struct stmmac_priv *priv)
  1799. {
  1800. unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
  1801. MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
  1802. dwmac_mmc_intr_all_mask(priv->mmcaddr);
  1803. if (priv->dma_cap.rmon) {
  1804. dwmac_mmc_ctrl(priv->mmcaddr, mode);
  1805. memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
  1806. } else
  1807. netdev_info(priv->dev, "No MAC Management Counters available\n");
  1808. }
  1809. /**
  1810. * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
  1811. * @priv: driver private structure
  1812. * Description:
  1813. * new GMAC chip generations have a new register to indicate the
  1814. * presence of the optional feature/functions.
  1815. * This can be also used to override the value passed through the
  1816. * platform and necessary for old MAC10/100 and GMAC chips.
  1817. */
  1818. static int stmmac_get_hw_features(struct stmmac_priv *priv)
  1819. {
  1820. return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
  1821. }
  1822. /**
  1823. * stmmac_check_ether_addr - check if the MAC addr is valid
  1824. * @priv: driver private structure
  1825. * Description:
  1826. * it is to verify if the MAC address is valid, in case of failures it
  1827. * generates a random MAC address
  1828. */
  1829. static void stmmac_check_ether_addr(struct stmmac_priv *priv)
  1830. {
  1831. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  1832. stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0);
  1833. if (!is_valid_ether_addr(priv->dev->dev_addr))
  1834. eth_hw_addr_random(priv->dev);
  1835. netdev_info(priv->dev, "device MAC address %pM\n",
  1836. priv->dev->dev_addr);
  1837. }
  1838. }
  1839. /**
  1840. * stmmac_init_dma_engine - DMA init.
  1841. * @priv: driver private structure
  1842. * Description:
  1843. * It inits the DMA invoking the specific MAC/GMAC callback.
  1844. * Some DMA parameters can be passed from the platform;
  1845. * in case of these are not passed a default is kept for the MAC or GMAC.
  1846. */
  1847. static int stmmac_init_dma_engine(struct stmmac_priv *priv)
  1848. {
  1849. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1850. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1851. u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
  1852. struct stmmac_rx_queue *rx_q;
  1853. struct stmmac_tx_queue *tx_q;
  1854. u32 chan = 0;
  1855. int atds = 0;
  1856. int ret = 0;
  1857. if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
  1858. dev_err(priv->device, "Invalid DMA configuration\n");
  1859. return -EINVAL;
  1860. }
  1861. if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
  1862. atds = 1;
  1863. ret = stmmac_reset(priv, priv->ioaddr);
  1864. if (ret) {
  1865. dev_err(priv->device, "Failed to reset the dma\n");
  1866. return ret;
  1867. }
  1868. /* DMA Configuration */
  1869. stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
  1870. if (priv->plat->axi)
  1871. stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
  1872. /* DMA CSR Channel configuration */
  1873. for (chan = 0; chan < dma_csr_ch; chan++)
  1874. stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
  1875. /* DMA RX Channel Configuration */
  1876. for (chan = 0; chan < rx_channels_count; chan++) {
  1877. rx_q = &priv->rx_queue[chan];
  1878. stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
  1879. rx_q->dma_rx_phy, chan);
  1880. rx_q->rx_tail_addr = rx_q->dma_rx_phy +
  1881. (DMA_RX_SIZE * sizeof(struct dma_desc));
  1882. stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
  1883. rx_q->rx_tail_addr, chan);
  1884. }
  1885. /* DMA TX Channel Configuration */
  1886. for (chan = 0; chan < tx_channels_count; chan++) {
  1887. tx_q = &priv->tx_queue[chan];
  1888. stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
  1889. tx_q->dma_tx_phy, chan);
  1890. tx_q->tx_tail_addr = tx_q->dma_tx_phy;
  1891. stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
  1892. tx_q->tx_tail_addr, chan);
  1893. }
  1894. return ret;
  1895. }
  1896. static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
  1897. {
  1898. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  1899. mod_timer(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1900. }
  1901. /**
  1902. * stmmac_tx_timer - mitigation sw timer for tx.
  1903. * @data: data pointer
  1904. * Description:
  1905. * This is the timer handler to directly invoke the stmmac_tx_clean.
  1906. */
  1907. static void stmmac_tx_timer(struct timer_list *t)
  1908. {
  1909. struct stmmac_tx_queue *tx_q = from_timer(tx_q, t, txtimer);
  1910. struct stmmac_priv *priv = tx_q->priv_data;
  1911. struct stmmac_channel *ch;
  1912. ch = &priv->channel[tx_q->queue_index];
  1913. if (likely(napi_schedule_prep(&ch->napi)))
  1914. __napi_schedule(&ch->napi);
  1915. }
  1916. /**
  1917. * stmmac_init_tx_coalesce - init tx mitigation options.
  1918. * @priv: driver private structure
  1919. * Description:
  1920. * This inits the transmit coalesce parameters: i.e. timer rate,
  1921. * timer handler and default threshold used for enabling the
  1922. * interrupt on completion bit.
  1923. */
  1924. static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
  1925. {
  1926. u32 tx_channel_count = priv->plat->tx_queues_to_use;
  1927. u32 chan;
  1928. priv->tx_coal_frames = STMMAC_TX_FRAMES;
  1929. priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
  1930. for (chan = 0; chan < tx_channel_count; chan++) {
  1931. struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
  1932. timer_setup(&tx_q->txtimer, stmmac_tx_timer, 0);
  1933. }
  1934. }
  1935. static void stmmac_set_rings_length(struct stmmac_priv *priv)
  1936. {
  1937. u32 rx_channels_count = priv->plat->rx_queues_to_use;
  1938. u32 tx_channels_count = priv->plat->tx_queues_to_use;
  1939. u32 chan;
  1940. /* set TX ring length */
  1941. for (chan = 0; chan < tx_channels_count; chan++)
  1942. stmmac_set_tx_ring_len(priv, priv->ioaddr,
  1943. (DMA_TX_SIZE - 1), chan);
  1944. /* set RX ring length */
  1945. for (chan = 0; chan < rx_channels_count; chan++)
  1946. stmmac_set_rx_ring_len(priv, priv->ioaddr,
  1947. (DMA_RX_SIZE - 1), chan);
  1948. }
  1949. /**
  1950. * stmmac_set_tx_queue_weight - Set TX queue weight
  1951. * @priv: driver private structure
  1952. * Description: It is used for setting TX queues weight
  1953. */
  1954. static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
  1955. {
  1956. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  1957. u32 weight;
  1958. u32 queue;
  1959. for (queue = 0; queue < tx_queues_count; queue++) {
  1960. weight = priv->plat->tx_queues_cfg[queue].weight;
  1961. stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
  1962. }
  1963. }
  1964. /**
  1965. * stmmac_configure_cbs - Configure CBS in TX queue
  1966. * @priv: driver private structure
  1967. * Description: It is used for configuring CBS in AVB TX queues
  1968. */
  1969. static void stmmac_configure_cbs(struct stmmac_priv *priv)
  1970. {
  1971. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  1972. u32 mode_to_use;
  1973. u32 queue;
  1974. /* queue 0 is reserved for legacy traffic */
  1975. for (queue = 1; queue < tx_queues_count; queue++) {
  1976. mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
  1977. if (mode_to_use == MTL_QUEUE_DCB)
  1978. continue;
  1979. stmmac_config_cbs(priv, priv->hw,
  1980. priv->plat->tx_queues_cfg[queue].send_slope,
  1981. priv->plat->tx_queues_cfg[queue].idle_slope,
  1982. priv->plat->tx_queues_cfg[queue].high_credit,
  1983. priv->plat->tx_queues_cfg[queue].low_credit,
  1984. queue);
  1985. }
  1986. }
  1987. /**
  1988. * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
  1989. * @priv: driver private structure
  1990. * Description: It is used for mapping RX queues to RX dma channels
  1991. */
  1992. static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
  1993. {
  1994. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  1995. u32 queue;
  1996. u32 chan;
  1997. for (queue = 0; queue < rx_queues_count; queue++) {
  1998. chan = priv->plat->rx_queues_cfg[queue].chan;
  1999. stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
  2000. }
  2001. }
  2002. /**
  2003. * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
  2004. * @priv: driver private structure
  2005. * Description: It is used for configuring the RX Queue Priority
  2006. */
  2007. static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
  2008. {
  2009. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  2010. u32 queue;
  2011. u32 prio;
  2012. for (queue = 0; queue < rx_queues_count; queue++) {
  2013. if (!priv->plat->rx_queues_cfg[queue].use_prio)
  2014. continue;
  2015. prio = priv->plat->rx_queues_cfg[queue].prio;
  2016. stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
  2017. }
  2018. }
  2019. /**
  2020. * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
  2021. * @priv: driver private structure
  2022. * Description: It is used for configuring the TX Queue Priority
  2023. */
  2024. static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
  2025. {
  2026. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  2027. u32 queue;
  2028. u32 prio;
  2029. for (queue = 0; queue < tx_queues_count; queue++) {
  2030. if (!priv->plat->tx_queues_cfg[queue].use_prio)
  2031. continue;
  2032. prio = priv->plat->tx_queues_cfg[queue].prio;
  2033. stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
  2034. }
  2035. }
  2036. /**
  2037. * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
  2038. * @priv: driver private structure
  2039. * Description: It is used for configuring the RX queue routing
  2040. */
  2041. static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
  2042. {
  2043. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  2044. u32 queue;
  2045. u8 packet;
  2046. for (queue = 0; queue < rx_queues_count; queue++) {
  2047. /* no specific packet type routing specified for the queue */
  2048. if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
  2049. continue;
  2050. packet = priv->plat->rx_queues_cfg[queue].pkt_route;
  2051. stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
  2052. }
  2053. }
  2054. /**
  2055. * stmmac_mtl_configuration - Configure MTL
  2056. * @priv: driver private structure
  2057. * Description: It is used for configurring MTL
  2058. */
  2059. static void stmmac_mtl_configuration(struct stmmac_priv *priv)
  2060. {
  2061. u32 rx_queues_count = priv->plat->rx_queues_to_use;
  2062. u32 tx_queues_count = priv->plat->tx_queues_to_use;
  2063. if (tx_queues_count > 1)
  2064. stmmac_set_tx_queue_weight(priv);
  2065. /* Configure MTL RX algorithms */
  2066. if (rx_queues_count > 1)
  2067. stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
  2068. priv->plat->rx_sched_algorithm);
  2069. /* Configure MTL TX algorithms */
  2070. if (tx_queues_count > 1)
  2071. stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
  2072. priv->plat->tx_sched_algorithm);
  2073. /* Configure CBS in AVB TX queues */
  2074. if (tx_queues_count > 1)
  2075. stmmac_configure_cbs(priv);
  2076. /* Map RX MTL to DMA channels */
  2077. stmmac_rx_queue_dma_chan_map(priv);
  2078. /* Enable MAC RX Queues */
  2079. stmmac_mac_enable_rx_queues(priv);
  2080. /* Set RX priorities */
  2081. if (rx_queues_count > 1)
  2082. stmmac_mac_config_rx_queues_prio(priv);
  2083. /* Set TX priorities */
  2084. if (tx_queues_count > 1)
  2085. stmmac_mac_config_tx_queues_prio(priv);
  2086. /* Set RX routing */
  2087. if (rx_queues_count > 1)
  2088. stmmac_mac_config_rx_queues_routing(priv);
  2089. }
  2090. static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
  2091. {
  2092. if (priv->dma_cap.asp) {
  2093. netdev_info(priv->dev, "Enabling Safety Features\n");
  2094. stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp);
  2095. } else {
  2096. netdev_info(priv->dev, "No Safety Features support found\n");
  2097. }
  2098. }
  2099. /**
  2100. * stmmac_hw_setup - setup mac in a usable state.
  2101. * @dev : pointer to the device structure.
  2102. * Description:
  2103. * this is the main function to setup the HW in a usable state because the
  2104. * dma engine is reset, the core registers are configured (e.g. AXI,
  2105. * Checksum features, timers). The DMA is ready to start receiving and
  2106. * transmitting.
  2107. * Return value:
  2108. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  2109. * file on failure.
  2110. */
  2111. static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
  2112. {
  2113. struct stmmac_priv *priv = netdev_priv(dev);
  2114. u32 rx_cnt = priv->plat->rx_queues_to_use;
  2115. u32 tx_cnt = priv->plat->tx_queues_to_use;
  2116. u32 chan;
  2117. int ret;
  2118. /* DMA initialization and SW reset */
  2119. ret = stmmac_init_dma_engine(priv);
  2120. if (ret < 0) {
  2121. netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
  2122. __func__);
  2123. return ret;
  2124. }
  2125. /* Copy the MAC addr into the HW */
  2126. stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
  2127. /* PS and related bits will be programmed according to the speed */
  2128. if (priv->hw->pcs) {
  2129. int speed = priv->plat->mac_port_sel_speed;
  2130. if ((speed == SPEED_10) || (speed == SPEED_100) ||
  2131. (speed == SPEED_1000)) {
  2132. priv->hw->ps = speed;
  2133. } else {
  2134. dev_warn(priv->device, "invalid port speed\n");
  2135. priv->hw->ps = 0;
  2136. }
  2137. }
  2138. /* Initialize the MAC Core */
  2139. stmmac_core_init(priv, priv->hw, dev);
  2140. /* Initialize MTL*/
  2141. stmmac_mtl_configuration(priv);
  2142. /* Initialize Safety Features */
  2143. stmmac_safety_feat_configuration(priv);
  2144. ret = stmmac_rx_ipc(priv, priv->hw);
  2145. if (!ret) {
  2146. netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
  2147. priv->plat->rx_coe = STMMAC_RX_COE_NONE;
  2148. priv->hw->rx_csum = 0;
  2149. }
  2150. /* Enable the MAC Rx/Tx */
  2151. stmmac_mac_set(priv, priv->ioaddr, true);
  2152. /* Set the HW DMA mode and the COE */
  2153. stmmac_dma_operation_mode(priv);
  2154. stmmac_mmc_setup(priv);
  2155. if (init_ptp) {
  2156. ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
  2157. if (ret < 0)
  2158. netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret);
  2159. ret = stmmac_init_ptp(priv);
  2160. if (ret == -EOPNOTSUPP)
  2161. netdev_warn(priv->dev, "PTP not supported by HW\n");
  2162. else if (ret)
  2163. netdev_warn(priv->dev, "PTP init failed\n");
  2164. }
  2165. priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
  2166. if (priv->use_riwt) {
  2167. ret = stmmac_rx_watchdog(priv, priv->ioaddr, MAX_DMA_RIWT, rx_cnt);
  2168. if (!ret)
  2169. priv->rx_riwt = MAX_DMA_RIWT;
  2170. }
  2171. if (priv->hw->pcs)
  2172. stmmac_pcs_ctrl_ane(priv, priv->hw, 1, priv->hw->ps, 0);
  2173. /* set TX and RX rings length */
  2174. stmmac_set_rings_length(priv);
  2175. /* Enable TSO */
  2176. if (priv->tso) {
  2177. for (chan = 0; chan < tx_cnt; chan++)
  2178. stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
  2179. }
  2180. /* Start the ball rolling... */
  2181. stmmac_start_all_dma(priv);
  2182. return 0;
  2183. }
  2184. static void stmmac_hw_teardown(struct net_device *dev)
  2185. {
  2186. struct stmmac_priv *priv = netdev_priv(dev);
  2187. clk_disable_unprepare(priv->plat->clk_ptp_ref);
  2188. }
  2189. /**
  2190. * stmmac_open - open entry point of the driver
  2191. * @dev : pointer to the device structure.
  2192. * Description:
  2193. * This function is the open entry point of the driver.
  2194. * Return value:
  2195. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  2196. * file on failure.
  2197. */
  2198. static int stmmac_open(struct net_device *dev)
  2199. {
  2200. struct stmmac_priv *priv = netdev_priv(dev);
  2201. u32 chan;
  2202. int ret;
  2203. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  2204. priv->hw->pcs != STMMAC_PCS_TBI &&
  2205. priv->hw->pcs != STMMAC_PCS_RTBI) {
  2206. ret = stmmac_init_phy(dev);
  2207. if (ret) {
  2208. netdev_err(priv->dev,
  2209. "%s: Cannot attach to PHY (error: %d)\n",
  2210. __func__, ret);
  2211. return ret;
  2212. }
  2213. }
  2214. /* Extra statistics */
  2215. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  2216. priv->xstats.threshold = tc;
  2217. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  2218. priv->rx_copybreak = STMMAC_RX_COPYBREAK;
  2219. ret = alloc_dma_desc_resources(priv);
  2220. if (ret < 0) {
  2221. netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
  2222. __func__);
  2223. goto dma_desc_error;
  2224. }
  2225. ret = init_dma_desc_rings(dev, GFP_KERNEL);
  2226. if (ret < 0) {
  2227. netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
  2228. __func__);
  2229. goto init_error;
  2230. }
  2231. ret = stmmac_hw_setup(dev, true);
  2232. if (ret < 0) {
  2233. netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
  2234. goto init_error;
  2235. }
  2236. stmmac_init_tx_coalesce(priv);
  2237. if (dev->phydev)
  2238. phy_start(dev->phydev);
  2239. /* Request the IRQ lines */
  2240. ret = request_irq(dev->irq, stmmac_interrupt,
  2241. IRQF_SHARED, dev->name, dev);
  2242. if (unlikely(ret < 0)) {
  2243. netdev_err(priv->dev,
  2244. "%s: ERROR: allocating the IRQ %d (error: %d)\n",
  2245. __func__, dev->irq, ret);
  2246. goto irq_error;
  2247. }
  2248. /* Request the Wake IRQ in case of another line is used for WoL */
  2249. if (priv->wol_irq != dev->irq) {
  2250. ret = request_irq(priv->wol_irq, stmmac_interrupt,
  2251. IRQF_SHARED, dev->name, dev);
  2252. if (unlikely(ret < 0)) {
  2253. netdev_err(priv->dev,
  2254. "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
  2255. __func__, priv->wol_irq, ret);
  2256. goto wolirq_error;
  2257. }
  2258. }
  2259. /* Request the IRQ lines */
  2260. if (priv->lpi_irq > 0) {
  2261. ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
  2262. dev->name, dev);
  2263. if (unlikely(ret < 0)) {
  2264. netdev_err(priv->dev,
  2265. "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  2266. __func__, priv->lpi_irq, ret);
  2267. goto lpiirq_error;
  2268. }
  2269. }
  2270. stmmac_enable_all_queues(priv);
  2271. stmmac_start_all_queues(priv);
  2272. return 0;
  2273. lpiirq_error:
  2274. if (priv->wol_irq != dev->irq)
  2275. free_irq(priv->wol_irq, dev);
  2276. wolirq_error:
  2277. free_irq(dev->irq, dev);
  2278. irq_error:
  2279. if (dev->phydev)
  2280. phy_stop(dev->phydev);
  2281. for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
  2282. del_timer_sync(&priv->tx_queue[chan].txtimer);
  2283. stmmac_hw_teardown(dev);
  2284. init_error:
  2285. free_dma_desc_resources(priv);
  2286. dma_desc_error:
  2287. if (dev->phydev)
  2288. phy_disconnect(dev->phydev);
  2289. return ret;
  2290. }
  2291. /**
  2292. * stmmac_release - close entry point of the driver
  2293. * @dev : device pointer.
  2294. * Description:
  2295. * This is the stop entry point of the driver.
  2296. */
  2297. static int stmmac_release(struct net_device *dev)
  2298. {
  2299. struct stmmac_priv *priv = netdev_priv(dev);
  2300. u32 chan;
  2301. if (priv->eee_enabled)
  2302. del_timer_sync(&priv->eee_ctrl_timer);
  2303. /* Stop and disconnect the PHY */
  2304. if (dev->phydev) {
  2305. phy_stop(dev->phydev);
  2306. phy_disconnect(dev->phydev);
  2307. }
  2308. stmmac_stop_all_queues(priv);
  2309. stmmac_disable_all_queues(priv);
  2310. for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
  2311. del_timer_sync(&priv->tx_queue[chan].txtimer);
  2312. /* Free the IRQ lines */
  2313. free_irq(dev->irq, dev);
  2314. if (priv->wol_irq != dev->irq)
  2315. free_irq(priv->wol_irq, dev);
  2316. if (priv->lpi_irq > 0)
  2317. free_irq(priv->lpi_irq, dev);
  2318. /* Stop TX/RX DMA and clear the descriptors */
  2319. stmmac_stop_all_dma(priv);
  2320. /* Release and free the Rx/Tx resources */
  2321. free_dma_desc_resources(priv);
  2322. /* Disable the MAC Rx/Tx */
  2323. stmmac_mac_set(priv, priv->ioaddr, false);
  2324. netif_carrier_off(dev);
  2325. stmmac_release_ptp(priv);
  2326. return 0;
  2327. }
  2328. /**
  2329. * stmmac_tso_allocator - close entry point of the driver
  2330. * @priv: driver private structure
  2331. * @des: buffer start address
  2332. * @total_len: total length to fill in descriptors
  2333. * @last_segmant: condition for the last descriptor
  2334. * @queue: TX queue index
  2335. * Description:
  2336. * This function fills descriptor and request new descriptors according to
  2337. * buffer length to fill
  2338. */
  2339. static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
  2340. int total_len, bool last_segment, u32 queue)
  2341. {
  2342. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  2343. struct dma_desc *desc;
  2344. u32 buff_size;
  2345. int tmp_len;
  2346. tmp_len = total_len;
  2347. while (tmp_len > 0) {
  2348. tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
  2349. WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
  2350. desc = tx_q->dma_tx + tx_q->cur_tx;
  2351. desc->des0 = cpu_to_le32(des + (total_len - tmp_len));
  2352. buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
  2353. TSO_MAX_BUFF_SIZE : tmp_len;
  2354. stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
  2355. 0, 1,
  2356. (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
  2357. 0, 0);
  2358. tmp_len -= TSO_MAX_BUFF_SIZE;
  2359. }
  2360. }
  2361. /**
  2362. * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
  2363. * @skb : the socket buffer
  2364. * @dev : device pointer
  2365. * Description: this is the transmit function that is called on TSO frames
  2366. * (support available on GMAC4 and newer chips).
  2367. * Diagram below show the ring programming in case of TSO frames:
  2368. *
  2369. * First Descriptor
  2370. * --------
  2371. * | DES0 |---> buffer1 = L2/L3/L4 header
  2372. * | DES1 |---> TCP Payload (can continue on next descr...)
  2373. * | DES2 |---> buffer 1 and 2 len
  2374. * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
  2375. * --------
  2376. * |
  2377. * ...
  2378. * |
  2379. * --------
  2380. * | DES0 | --| Split TCP Payload on Buffers 1 and 2
  2381. * | DES1 | --|
  2382. * | DES2 | --> buffer 1 and 2 len
  2383. * | DES3 |
  2384. * --------
  2385. *
  2386. * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
  2387. */
  2388. static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
  2389. {
  2390. struct dma_desc *desc, *first, *mss_desc = NULL;
  2391. struct stmmac_priv *priv = netdev_priv(dev);
  2392. int nfrags = skb_shinfo(skb)->nr_frags;
  2393. u32 queue = skb_get_queue_mapping(skb);
  2394. unsigned int first_entry, des;
  2395. struct stmmac_tx_queue *tx_q;
  2396. int tmp_pay_len = 0;
  2397. u32 pay_len, mss;
  2398. u8 proto_hdr_len;
  2399. int i;
  2400. tx_q = &priv->tx_queue[queue];
  2401. /* Compute header lengths */
  2402. proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  2403. /* Desc availability based on threshold should be enough safe */
  2404. if (unlikely(stmmac_tx_avail(priv, queue) <
  2405. (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
  2406. if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
  2407. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
  2408. queue));
  2409. /* This is a hard error, log it. */
  2410. netdev_err(priv->dev,
  2411. "%s: Tx Ring full when queue awake\n",
  2412. __func__);
  2413. }
  2414. return NETDEV_TX_BUSY;
  2415. }
  2416. pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
  2417. mss = skb_shinfo(skb)->gso_size;
  2418. /* set new MSS value if needed */
  2419. if (mss != tx_q->mss) {
  2420. mss_desc = tx_q->dma_tx + tx_q->cur_tx;
  2421. stmmac_set_mss(priv, mss_desc, mss);
  2422. tx_q->mss = mss;
  2423. tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
  2424. WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
  2425. }
  2426. if (netif_msg_tx_queued(priv)) {
  2427. pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
  2428. __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
  2429. pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
  2430. skb->data_len);
  2431. }
  2432. first_entry = tx_q->cur_tx;
  2433. WARN_ON(tx_q->tx_skbuff[first_entry]);
  2434. desc = tx_q->dma_tx + first_entry;
  2435. first = desc;
  2436. /* first descriptor: fill Headers on Buf1 */
  2437. des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
  2438. DMA_TO_DEVICE);
  2439. if (dma_mapping_error(priv->device, des))
  2440. goto dma_map_err;
  2441. tx_q->tx_skbuff_dma[first_entry].buf = des;
  2442. tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
  2443. first->des0 = cpu_to_le32(des);
  2444. /* Fill start of payload in buff2 of first descriptor */
  2445. if (pay_len)
  2446. first->des1 = cpu_to_le32(des + proto_hdr_len);
  2447. /* If needed take extra descriptors to fill the remaining payload */
  2448. tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
  2449. stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
  2450. /* Prepare fragments */
  2451. for (i = 0; i < nfrags; i++) {
  2452. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2453. des = skb_frag_dma_map(priv->device, frag, 0,
  2454. skb_frag_size(frag),
  2455. DMA_TO_DEVICE);
  2456. if (dma_mapping_error(priv->device, des))
  2457. goto dma_map_err;
  2458. stmmac_tso_allocator(priv, des, skb_frag_size(frag),
  2459. (i == nfrags - 1), queue);
  2460. tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
  2461. tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
  2462. tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
  2463. }
  2464. tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
  2465. /* Only the last descriptor gets to point to the skb. */
  2466. tx_q->tx_skbuff[tx_q->cur_tx] = skb;
  2467. /* We've used all descriptors we need for this skb, however,
  2468. * advance cur_tx so that it references a fresh descriptor.
  2469. * ndo_start_xmit will fill this descriptor the next time it's
  2470. * called and stmmac_tx_clean may clean up to this descriptor.
  2471. */
  2472. tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
  2473. if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
  2474. netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
  2475. __func__);
  2476. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
  2477. }
  2478. dev->stats.tx_bytes += skb->len;
  2479. priv->xstats.tx_tso_frames++;
  2480. priv->xstats.tx_tso_nfrags += nfrags;
  2481. /* Manage tx mitigation */
  2482. tx_q->tx_count_frames += nfrags + 1;
  2483. if (likely(priv->tx_coal_frames > tx_q->tx_count_frames) &&
  2484. !(priv->synopsys_id >= DWMAC_CORE_4_00 &&
  2485. (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  2486. priv->hwts_tx_en)) {
  2487. stmmac_tx_timer_arm(priv, queue);
  2488. } else {
  2489. tx_q->tx_count_frames = 0;
  2490. stmmac_set_tx_ic(priv, desc);
  2491. priv->xstats.tx_set_ic_bit++;
  2492. }
  2493. skb_tx_timestamp(skb);
  2494. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  2495. priv->hwts_tx_en)) {
  2496. /* declare that device is doing timestamping */
  2497. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2498. stmmac_enable_tx_timestamp(priv, first);
  2499. }
  2500. /* Complete the first descriptor before granting the DMA */
  2501. stmmac_prepare_tso_tx_desc(priv, first, 1,
  2502. proto_hdr_len,
  2503. pay_len,
  2504. 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
  2505. tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
  2506. /* If context desc is used to change MSS */
  2507. if (mss_desc) {
  2508. /* Make sure that first descriptor has been completely
  2509. * written, including its own bit. This is because MSS is
  2510. * actually before first descriptor, so we need to make
  2511. * sure that MSS's own bit is the last thing written.
  2512. */
  2513. dma_wmb();
  2514. stmmac_set_tx_owner(priv, mss_desc);
  2515. }
  2516. /* The own bit must be the latest setting done when prepare the
  2517. * descriptor and then barrier is needed to make sure that
  2518. * all is coherent before granting the DMA engine.
  2519. */
  2520. wmb();
  2521. if (netif_msg_pktdata(priv)) {
  2522. pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
  2523. __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
  2524. tx_q->cur_tx, first, nfrags);
  2525. stmmac_display_ring(priv, (void *)tx_q->dma_tx, DMA_TX_SIZE, 0);
  2526. pr_info(">>> frame to be transmitted: ");
  2527. print_pkt(skb->data, skb_headlen(skb));
  2528. }
  2529. netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
  2530. tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * sizeof(*desc));
  2531. stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
  2532. return NETDEV_TX_OK;
  2533. dma_map_err:
  2534. dev_err(priv->device, "Tx dma map failed\n");
  2535. dev_kfree_skb(skb);
  2536. priv->dev->stats.tx_dropped++;
  2537. return NETDEV_TX_OK;
  2538. }
  2539. /**
  2540. * stmmac_xmit - Tx entry point of the driver
  2541. * @skb : the socket buffer
  2542. * @dev : device pointer
  2543. * Description : this is the tx entry point of the driver.
  2544. * It programs the chain or the ring and supports oversized frames
  2545. * and SG feature.
  2546. */
  2547. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  2548. {
  2549. struct stmmac_priv *priv = netdev_priv(dev);
  2550. unsigned int nopaged_len = skb_headlen(skb);
  2551. int i, csum_insertion = 0, is_jumbo = 0;
  2552. u32 queue = skb_get_queue_mapping(skb);
  2553. int nfrags = skb_shinfo(skb)->nr_frags;
  2554. int entry;
  2555. unsigned int first_entry;
  2556. struct dma_desc *desc, *first;
  2557. struct stmmac_tx_queue *tx_q;
  2558. unsigned int enh_desc;
  2559. unsigned int des;
  2560. tx_q = &priv->tx_queue[queue];
  2561. if (priv->tx_path_in_lpi_mode)
  2562. stmmac_disable_eee_mode(priv);
  2563. /* Manage oversized TCP frames for GMAC4 device */
  2564. if (skb_is_gso(skb) && priv->tso) {
  2565. if (skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
  2566. return stmmac_tso_xmit(skb, dev);
  2567. }
  2568. if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
  2569. if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
  2570. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
  2571. queue));
  2572. /* This is a hard error, log it. */
  2573. netdev_err(priv->dev,
  2574. "%s: Tx Ring full when queue awake\n",
  2575. __func__);
  2576. }
  2577. return NETDEV_TX_BUSY;
  2578. }
  2579. entry = tx_q->cur_tx;
  2580. first_entry = entry;
  2581. WARN_ON(tx_q->tx_skbuff[first_entry]);
  2582. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  2583. if (likely(priv->extend_desc))
  2584. desc = (struct dma_desc *)(tx_q->dma_etx + entry);
  2585. else
  2586. desc = tx_q->dma_tx + entry;
  2587. first = desc;
  2588. enh_desc = priv->plat->enh_desc;
  2589. /* To program the descriptors according to the size of the frame */
  2590. if (enh_desc)
  2591. is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
  2592. if (unlikely(is_jumbo)) {
  2593. entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
  2594. if (unlikely(entry < 0) && (entry != -EINVAL))
  2595. goto dma_map_err;
  2596. }
  2597. for (i = 0; i < nfrags; i++) {
  2598. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2599. int len = skb_frag_size(frag);
  2600. bool last_segment = (i == (nfrags - 1));
  2601. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  2602. WARN_ON(tx_q->tx_skbuff[entry]);
  2603. if (likely(priv->extend_desc))
  2604. desc = (struct dma_desc *)(tx_q->dma_etx + entry);
  2605. else
  2606. desc = tx_q->dma_tx + entry;
  2607. des = skb_frag_dma_map(priv->device, frag, 0, len,
  2608. DMA_TO_DEVICE);
  2609. if (dma_mapping_error(priv->device, des))
  2610. goto dma_map_err; /* should reuse desc w/o issues */
  2611. tx_q->tx_skbuff_dma[entry].buf = des;
  2612. stmmac_set_desc_addr(priv, desc, des);
  2613. tx_q->tx_skbuff_dma[entry].map_as_page = true;
  2614. tx_q->tx_skbuff_dma[entry].len = len;
  2615. tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
  2616. /* Prepare the descriptor and set the own bit too */
  2617. stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
  2618. priv->mode, 1, last_segment, skb->len);
  2619. }
  2620. /* Only the last descriptor gets to point to the skb. */
  2621. tx_q->tx_skbuff[entry] = skb;
  2622. /* We've used all descriptors we need for this skb, however,
  2623. * advance cur_tx so that it references a fresh descriptor.
  2624. * ndo_start_xmit will fill this descriptor the next time it's
  2625. * called and stmmac_tx_clean may clean up to this descriptor.
  2626. */
  2627. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  2628. tx_q->cur_tx = entry;
  2629. if (netif_msg_pktdata(priv)) {
  2630. void *tx_head;
  2631. netdev_dbg(priv->dev,
  2632. "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
  2633. __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
  2634. entry, first, nfrags);
  2635. if (priv->extend_desc)
  2636. tx_head = (void *)tx_q->dma_etx;
  2637. else
  2638. tx_head = (void *)tx_q->dma_tx;
  2639. stmmac_display_ring(priv, tx_head, DMA_TX_SIZE, false);
  2640. netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
  2641. print_pkt(skb->data, skb->len);
  2642. }
  2643. if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
  2644. netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
  2645. __func__);
  2646. netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
  2647. }
  2648. dev->stats.tx_bytes += skb->len;
  2649. /* According to the coalesce parameter the IC bit for the latest
  2650. * segment is reset and the timer re-started to clean the tx status.
  2651. * This approach takes care about the fragments: desc is the first
  2652. * element in case of no SG.
  2653. */
  2654. tx_q->tx_count_frames += nfrags + 1;
  2655. if (likely(priv->tx_coal_frames > tx_q->tx_count_frames) &&
  2656. !(priv->synopsys_id >= DWMAC_CORE_4_00 &&
  2657. (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  2658. priv->hwts_tx_en)) {
  2659. stmmac_tx_timer_arm(priv, queue);
  2660. } else {
  2661. tx_q->tx_count_frames = 0;
  2662. stmmac_set_tx_ic(priv, desc);
  2663. priv->xstats.tx_set_ic_bit++;
  2664. }
  2665. skb_tx_timestamp(skb);
  2666. /* Ready to fill the first descriptor and set the OWN bit w/o any
  2667. * problems because all the descriptors are actually ready to be
  2668. * passed to the DMA engine.
  2669. */
  2670. if (likely(!is_jumbo)) {
  2671. bool last_segment = (nfrags == 0);
  2672. des = dma_map_single(priv->device, skb->data,
  2673. nopaged_len, DMA_TO_DEVICE);
  2674. if (dma_mapping_error(priv->device, des))
  2675. goto dma_map_err;
  2676. tx_q->tx_skbuff_dma[first_entry].buf = des;
  2677. stmmac_set_desc_addr(priv, first, des);
  2678. tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
  2679. tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
  2680. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  2681. priv->hwts_tx_en)) {
  2682. /* declare that device is doing timestamping */
  2683. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  2684. stmmac_enable_tx_timestamp(priv, first);
  2685. }
  2686. /* Prepare the first descriptor setting the OWN bit too */
  2687. stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
  2688. csum_insertion, priv->mode, 1, last_segment,
  2689. skb->len);
  2690. } else {
  2691. stmmac_set_tx_owner(priv, first);
  2692. }
  2693. /* The own bit must be the latest setting done when prepare the
  2694. * descriptor and then barrier is needed to make sure that
  2695. * all is coherent before granting the DMA engine.
  2696. */
  2697. wmb();
  2698. netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
  2699. stmmac_enable_dma_transmission(priv, priv->ioaddr);
  2700. tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * sizeof(*desc));
  2701. stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
  2702. return NETDEV_TX_OK;
  2703. dma_map_err:
  2704. netdev_err(priv->dev, "Tx DMA map failed\n");
  2705. dev_kfree_skb(skb);
  2706. priv->dev->stats.tx_dropped++;
  2707. return NETDEV_TX_OK;
  2708. }
  2709. static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
  2710. {
  2711. struct vlan_ethhdr *veth;
  2712. __be16 vlan_proto;
  2713. u16 vlanid;
  2714. veth = (struct vlan_ethhdr *)skb->data;
  2715. vlan_proto = veth->h_vlan_proto;
  2716. if ((vlan_proto == htons(ETH_P_8021Q) &&
  2717. dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
  2718. (vlan_proto == htons(ETH_P_8021AD) &&
  2719. dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
  2720. /* pop the vlan tag */
  2721. vlanid = ntohs(veth->h_vlan_TCI);
  2722. memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
  2723. skb_pull(skb, VLAN_HLEN);
  2724. __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
  2725. }
  2726. }
  2727. static inline int stmmac_rx_threshold_count(struct stmmac_rx_queue *rx_q)
  2728. {
  2729. if (rx_q->rx_zeroc_thresh < STMMAC_RX_THRESH)
  2730. return 0;
  2731. return 1;
  2732. }
  2733. /**
  2734. * stmmac_rx_refill - refill used skb preallocated buffers
  2735. * @priv: driver private structure
  2736. * @queue: RX queue index
  2737. * Description : this is to reallocate the skb for the reception process
  2738. * that is based on zero-copy.
  2739. */
  2740. static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
  2741. {
  2742. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  2743. int dirty = stmmac_rx_dirty(priv, queue);
  2744. unsigned int entry = rx_q->dirty_rx;
  2745. int bfsize = priv->dma_buf_sz;
  2746. while (dirty-- > 0) {
  2747. struct dma_desc *p;
  2748. if (priv->extend_desc)
  2749. p = (struct dma_desc *)(rx_q->dma_erx + entry);
  2750. else
  2751. p = rx_q->dma_rx + entry;
  2752. if (likely(!rx_q->rx_skbuff[entry])) {
  2753. struct sk_buff *skb;
  2754. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  2755. if (unlikely(!skb)) {
  2756. /* so for a while no zero-copy! */
  2757. rx_q->rx_zeroc_thresh = STMMAC_RX_THRESH;
  2758. if (unlikely(net_ratelimit()))
  2759. dev_err(priv->device,
  2760. "fail to alloc skb entry %d\n",
  2761. entry);
  2762. break;
  2763. }
  2764. rx_q->rx_skbuff[entry] = skb;
  2765. rx_q->rx_skbuff_dma[entry] =
  2766. dma_map_single(priv->device, skb->data, bfsize,
  2767. DMA_FROM_DEVICE);
  2768. if (dma_mapping_error(priv->device,
  2769. rx_q->rx_skbuff_dma[entry])) {
  2770. netdev_err(priv->dev, "Rx DMA map failed\n");
  2771. dev_kfree_skb(skb);
  2772. break;
  2773. }
  2774. stmmac_set_desc_addr(priv, p, rx_q->rx_skbuff_dma[entry]);
  2775. stmmac_refill_desc3(priv, rx_q, p);
  2776. if (rx_q->rx_zeroc_thresh > 0)
  2777. rx_q->rx_zeroc_thresh--;
  2778. netif_dbg(priv, rx_status, priv->dev,
  2779. "refill entry #%d\n", entry);
  2780. }
  2781. dma_wmb();
  2782. stmmac_set_rx_owner(priv, p, priv->use_riwt);
  2783. dma_wmb();
  2784. entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
  2785. }
  2786. rx_q->dirty_rx = entry;
  2787. stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
  2788. }
  2789. /**
  2790. * stmmac_rx - manage the receive process
  2791. * @priv: driver private structure
  2792. * @limit: napi bugget
  2793. * @queue: RX queue index.
  2794. * Description : this the function called by the napi poll method.
  2795. * It gets all the frames inside the ring.
  2796. */
  2797. static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
  2798. {
  2799. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  2800. struct stmmac_channel *ch = &priv->channel[queue];
  2801. unsigned int next_entry = rx_q->cur_rx;
  2802. int coe = priv->hw->rx_csum;
  2803. unsigned int count = 0;
  2804. bool xmac;
  2805. xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
  2806. if (netif_msg_rx_status(priv)) {
  2807. void *rx_head;
  2808. netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
  2809. if (priv->extend_desc)
  2810. rx_head = (void *)rx_q->dma_erx;
  2811. else
  2812. rx_head = (void *)rx_q->dma_rx;
  2813. stmmac_display_ring(priv, rx_head, DMA_RX_SIZE, true);
  2814. }
  2815. while (count < limit) {
  2816. int entry, status;
  2817. struct dma_desc *p;
  2818. struct dma_desc *np;
  2819. entry = next_entry;
  2820. if (priv->extend_desc)
  2821. p = (struct dma_desc *)(rx_q->dma_erx + entry);
  2822. else
  2823. p = rx_q->dma_rx + entry;
  2824. /* read the status of the incoming frame */
  2825. status = stmmac_rx_status(priv, &priv->dev->stats,
  2826. &priv->xstats, p);
  2827. /* check if managed by the DMA otherwise go ahead */
  2828. if (unlikely(status & dma_own))
  2829. break;
  2830. count++;
  2831. rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, DMA_RX_SIZE);
  2832. next_entry = rx_q->cur_rx;
  2833. if (priv->extend_desc)
  2834. np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
  2835. else
  2836. np = rx_q->dma_rx + next_entry;
  2837. prefetch(np);
  2838. if (priv->extend_desc)
  2839. stmmac_rx_extended_status(priv, &priv->dev->stats,
  2840. &priv->xstats, rx_q->dma_erx + entry);
  2841. if (unlikely(status == discard_frame)) {
  2842. priv->dev->stats.rx_errors++;
  2843. if (priv->hwts_rx_en && !priv->extend_desc) {
  2844. /* DESC2 & DESC3 will be overwritten by device
  2845. * with timestamp value, hence reinitialize
  2846. * them in stmmac_rx_refill() function so that
  2847. * device can reuse it.
  2848. */
  2849. dev_kfree_skb_any(rx_q->rx_skbuff[entry]);
  2850. rx_q->rx_skbuff[entry] = NULL;
  2851. dma_unmap_single(priv->device,
  2852. rx_q->rx_skbuff_dma[entry],
  2853. priv->dma_buf_sz,
  2854. DMA_FROM_DEVICE);
  2855. }
  2856. } else {
  2857. struct sk_buff *skb;
  2858. int frame_len;
  2859. unsigned int des;
  2860. stmmac_get_desc_addr(priv, p, &des);
  2861. frame_len = stmmac_get_rx_frame_len(priv, p, coe);
  2862. /* If frame length is greater than skb buffer size
  2863. * (preallocated during init) then the packet is
  2864. * ignored
  2865. */
  2866. if (frame_len > priv->dma_buf_sz) {
  2867. if (net_ratelimit())
  2868. netdev_err(priv->dev,
  2869. "len %d larger than size (%d)\n",
  2870. frame_len, priv->dma_buf_sz);
  2871. priv->dev->stats.rx_length_errors++;
  2872. continue;
  2873. }
  2874. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  2875. * Type frames (LLC/LLC-SNAP)
  2876. *
  2877. * llc_snap is never checked in GMAC >= 4, so this ACS
  2878. * feature is always disabled and packets need to be
  2879. * stripped manually.
  2880. */
  2881. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
  2882. unlikely(status != llc_snap))
  2883. frame_len -= ETH_FCS_LEN;
  2884. if (netif_msg_rx_status(priv)) {
  2885. netdev_dbg(priv->dev, "\tdesc: %p [entry %d] buff=0x%x\n",
  2886. p, entry, des);
  2887. netdev_dbg(priv->dev, "frame size %d, COE: %d\n",
  2888. frame_len, status);
  2889. }
  2890. /* The zero-copy is always used for all the sizes
  2891. * in case of GMAC4 because it needs
  2892. * to refill the used descriptors, always.
  2893. */
  2894. if (unlikely(!xmac &&
  2895. ((frame_len < priv->rx_copybreak) ||
  2896. stmmac_rx_threshold_count(rx_q)))) {
  2897. skb = netdev_alloc_skb_ip_align(priv->dev,
  2898. frame_len);
  2899. if (unlikely(!skb)) {
  2900. if (net_ratelimit())
  2901. dev_warn(priv->device,
  2902. "packet dropped\n");
  2903. priv->dev->stats.rx_dropped++;
  2904. continue;
  2905. }
  2906. dma_sync_single_for_cpu(priv->device,
  2907. rx_q->rx_skbuff_dma
  2908. [entry], frame_len,
  2909. DMA_FROM_DEVICE);
  2910. skb_copy_to_linear_data(skb,
  2911. rx_q->
  2912. rx_skbuff[entry]->data,
  2913. frame_len);
  2914. skb_put(skb, frame_len);
  2915. dma_sync_single_for_device(priv->device,
  2916. rx_q->rx_skbuff_dma
  2917. [entry], frame_len,
  2918. DMA_FROM_DEVICE);
  2919. } else {
  2920. skb = rx_q->rx_skbuff[entry];
  2921. if (unlikely(!skb)) {
  2922. if (net_ratelimit())
  2923. netdev_err(priv->dev,
  2924. "%s: Inconsistent Rx chain\n",
  2925. priv->dev->name);
  2926. priv->dev->stats.rx_dropped++;
  2927. continue;
  2928. }
  2929. prefetch(skb->data - NET_IP_ALIGN);
  2930. rx_q->rx_skbuff[entry] = NULL;
  2931. rx_q->rx_zeroc_thresh++;
  2932. skb_put(skb, frame_len);
  2933. dma_unmap_single(priv->device,
  2934. rx_q->rx_skbuff_dma[entry],
  2935. priv->dma_buf_sz,
  2936. DMA_FROM_DEVICE);
  2937. }
  2938. if (netif_msg_pktdata(priv)) {
  2939. netdev_dbg(priv->dev, "frame received (%dbytes)",
  2940. frame_len);
  2941. print_pkt(skb->data, frame_len);
  2942. }
  2943. stmmac_get_rx_hwtstamp(priv, p, np, skb);
  2944. stmmac_rx_vlan(priv->dev, skb);
  2945. skb->protocol = eth_type_trans(skb, priv->dev);
  2946. if (unlikely(!coe))
  2947. skb_checksum_none_assert(skb);
  2948. else
  2949. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2950. napi_gro_receive(&ch->napi, skb);
  2951. priv->dev->stats.rx_packets++;
  2952. priv->dev->stats.rx_bytes += frame_len;
  2953. }
  2954. }
  2955. stmmac_rx_refill(priv, queue);
  2956. priv->xstats.rx_pkt_n += count;
  2957. return count;
  2958. }
  2959. /**
  2960. * stmmac_poll - stmmac poll method (NAPI)
  2961. * @napi : pointer to the napi structure.
  2962. * @budget : maximum number of packets that the current CPU can receive from
  2963. * all interfaces.
  2964. * Description :
  2965. * To look at the incoming frames and clear the tx resources.
  2966. */
  2967. static int stmmac_napi_poll(struct napi_struct *napi, int budget)
  2968. {
  2969. struct stmmac_channel *ch =
  2970. container_of(napi, struct stmmac_channel, napi);
  2971. struct stmmac_priv *priv = ch->priv_data;
  2972. int work_done, rx_done = 0, tx_done = 0;
  2973. u32 chan = ch->index;
  2974. priv->xstats.napi_poll++;
  2975. if (ch->has_tx)
  2976. tx_done = stmmac_tx_clean(priv, budget, chan);
  2977. if (ch->has_rx)
  2978. rx_done = stmmac_rx(priv, budget, chan);
  2979. work_done = max(rx_done, tx_done);
  2980. work_done = min(work_done, budget);
  2981. if (work_done < budget && napi_complete_done(napi, work_done)) {
  2982. int stat;
  2983. stmmac_enable_dma_irq(priv, priv->ioaddr, chan);
  2984. stat = stmmac_dma_interrupt_status(priv, priv->ioaddr,
  2985. &priv->xstats, chan);
  2986. if (stat && napi_reschedule(napi))
  2987. stmmac_disable_dma_irq(priv, priv->ioaddr, chan);
  2988. }
  2989. return work_done;
  2990. }
  2991. /**
  2992. * stmmac_tx_timeout
  2993. * @dev : Pointer to net device structure
  2994. * Description: this function is called when a packet transmission fails to
  2995. * complete within a reasonable time. The driver will mark the error in the
  2996. * netdev structure and arrange for the device to be reset to a sane state
  2997. * in order to transmit a new packet.
  2998. */
  2999. static void stmmac_tx_timeout(struct net_device *dev)
  3000. {
  3001. struct stmmac_priv *priv = netdev_priv(dev);
  3002. stmmac_global_err(priv);
  3003. }
  3004. /**
  3005. * stmmac_set_rx_mode - entry point for multicast addressing
  3006. * @dev : pointer to the device structure
  3007. * Description:
  3008. * This function is a driver entry point which gets called by the kernel
  3009. * whenever multicast addresses must be enabled/disabled.
  3010. * Return value:
  3011. * void.
  3012. */
  3013. static void stmmac_set_rx_mode(struct net_device *dev)
  3014. {
  3015. struct stmmac_priv *priv = netdev_priv(dev);
  3016. stmmac_set_filter(priv, priv->hw, dev);
  3017. }
  3018. /**
  3019. * stmmac_change_mtu - entry point to change MTU size for the device.
  3020. * @dev : device pointer.
  3021. * @new_mtu : the new MTU size for the device.
  3022. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  3023. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  3024. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  3025. * Return value:
  3026. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  3027. * file on failure.
  3028. */
  3029. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  3030. {
  3031. struct stmmac_priv *priv = netdev_priv(dev);
  3032. if (netif_running(dev)) {
  3033. netdev_err(priv->dev, "must be stopped to change its MTU\n");
  3034. return -EBUSY;
  3035. }
  3036. dev->mtu = new_mtu;
  3037. netdev_update_features(dev);
  3038. return 0;
  3039. }
  3040. static netdev_features_t stmmac_fix_features(struct net_device *dev,
  3041. netdev_features_t features)
  3042. {
  3043. struct stmmac_priv *priv = netdev_priv(dev);
  3044. if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
  3045. features &= ~NETIF_F_RXCSUM;
  3046. if (!priv->plat->tx_coe)
  3047. features &= ~NETIF_F_CSUM_MASK;
  3048. /* Some GMAC devices have a bugged Jumbo frame support that
  3049. * needs to have the Tx COE disabled for oversized frames
  3050. * (due to limited buffer sizes). In this case we disable
  3051. * the TX csum insertion in the TDES and not use SF.
  3052. */
  3053. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  3054. features &= ~NETIF_F_CSUM_MASK;
  3055. /* Disable tso if asked by ethtool */
  3056. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  3057. if (features & NETIF_F_TSO)
  3058. priv->tso = true;
  3059. else
  3060. priv->tso = false;
  3061. }
  3062. return features;
  3063. }
  3064. static int stmmac_set_features(struct net_device *netdev,
  3065. netdev_features_t features)
  3066. {
  3067. struct stmmac_priv *priv = netdev_priv(netdev);
  3068. /* Keep the COE Type in case of csum is supporting */
  3069. if (features & NETIF_F_RXCSUM)
  3070. priv->hw->rx_csum = priv->plat->rx_coe;
  3071. else
  3072. priv->hw->rx_csum = 0;
  3073. /* No check needed because rx_coe has been set before and it will be
  3074. * fixed in case of issue.
  3075. */
  3076. stmmac_rx_ipc(priv, priv->hw);
  3077. return 0;
  3078. }
  3079. /**
  3080. * stmmac_interrupt - main ISR
  3081. * @irq: interrupt number.
  3082. * @dev_id: to pass the net device pointer.
  3083. * Description: this is the main driver interrupt service routine.
  3084. * It can call:
  3085. * o DMA service routine (to manage incoming frame reception and transmission
  3086. * status)
  3087. * o Core interrupts to manage: remote wake-up, management counter, LPI
  3088. * interrupts.
  3089. */
  3090. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  3091. {
  3092. struct net_device *dev = (struct net_device *)dev_id;
  3093. struct stmmac_priv *priv = netdev_priv(dev);
  3094. u32 rx_cnt = priv->plat->rx_queues_to_use;
  3095. u32 tx_cnt = priv->plat->tx_queues_to_use;
  3096. u32 queues_count;
  3097. u32 queue;
  3098. bool xmac;
  3099. xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
  3100. queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
  3101. if (priv->irq_wake)
  3102. pm_wakeup_event(priv->device, 0);
  3103. if (unlikely(!dev)) {
  3104. netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
  3105. return IRQ_NONE;
  3106. }
  3107. /* Check if adapter is up */
  3108. if (test_bit(STMMAC_DOWN, &priv->state))
  3109. return IRQ_HANDLED;
  3110. /* Check if a fatal error happened */
  3111. if (stmmac_safety_feat_interrupt(priv))
  3112. return IRQ_HANDLED;
  3113. /* To handle GMAC own interrupts */
  3114. if ((priv->plat->has_gmac) || xmac) {
  3115. int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
  3116. int mtl_status;
  3117. if (unlikely(status)) {
  3118. /* For LPI we need to save the tx status */
  3119. if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
  3120. priv->tx_path_in_lpi_mode = true;
  3121. if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
  3122. priv->tx_path_in_lpi_mode = false;
  3123. }
  3124. for (queue = 0; queue < queues_count; queue++) {
  3125. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  3126. mtl_status = stmmac_host_mtl_irq_status(priv, priv->hw,
  3127. queue);
  3128. if (mtl_status != -EINVAL)
  3129. status |= mtl_status;
  3130. if (status & CORE_IRQ_MTL_RX_OVERFLOW)
  3131. stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
  3132. rx_q->rx_tail_addr,
  3133. queue);
  3134. }
  3135. /* PCS link status */
  3136. if (priv->hw->pcs) {
  3137. if (priv->xstats.pcs_link)
  3138. netif_carrier_on(dev);
  3139. else
  3140. netif_carrier_off(dev);
  3141. }
  3142. }
  3143. /* To handle DMA interrupts */
  3144. stmmac_dma_interrupt(priv);
  3145. return IRQ_HANDLED;
  3146. }
  3147. #ifdef CONFIG_NET_POLL_CONTROLLER
  3148. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  3149. * to allow network I/O with interrupts disabled.
  3150. */
  3151. static void stmmac_poll_controller(struct net_device *dev)
  3152. {
  3153. disable_irq(dev->irq);
  3154. stmmac_interrupt(dev->irq, dev);
  3155. enable_irq(dev->irq);
  3156. }
  3157. #endif
  3158. /**
  3159. * stmmac_ioctl - Entry point for the Ioctl
  3160. * @dev: Device pointer.
  3161. * @rq: An IOCTL specefic structure, that can contain a pointer to
  3162. * a proprietary structure used to pass information to the driver.
  3163. * @cmd: IOCTL command
  3164. * Description:
  3165. * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
  3166. */
  3167. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  3168. {
  3169. int ret = -EOPNOTSUPP;
  3170. if (!netif_running(dev))
  3171. return -EINVAL;
  3172. switch (cmd) {
  3173. case SIOCGMIIPHY:
  3174. case SIOCGMIIREG:
  3175. case SIOCSMIIREG:
  3176. if (!dev->phydev)
  3177. return -EINVAL;
  3178. ret = phy_mii_ioctl(dev->phydev, rq, cmd);
  3179. break;
  3180. case SIOCSHWTSTAMP:
  3181. ret = stmmac_hwtstamp_ioctl(dev, rq);
  3182. break;
  3183. default:
  3184. break;
  3185. }
  3186. return ret;
  3187. }
  3188. static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  3189. void *cb_priv)
  3190. {
  3191. struct stmmac_priv *priv = cb_priv;
  3192. int ret = -EOPNOTSUPP;
  3193. stmmac_disable_all_queues(priv);
  3194. switch (type) {
  3195. case TC_SETUP_CLSU32:
  3196. if (tc_cls_can_offload_and_chain0(priv->dev, type_data))
  3197. ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
  3198. break;
  3199. default:
  3200. break;
  3201. }
  3202. stmmac_enable_all_queues(priv);
  3203. return ret;
  3204. }
  3205. static int stmmac_setup_tc_block(struct stmmac_priv *priv,
  3206. struct tc_block_offload *f)
  3207. {
  3208. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  3209. return -EOPNOTSUPP;
  3210. switch (f->command) {
  3211. case TC_BLOCK_BIND:
  3212. return tcf_block_cb_register(f->block, stmmac_setup_tc_block_cb,
  3213. priv, priv, f->extack);
  3214. case TC_BLOCK_UNBIND:
  3215. tcf_block_cb_unregister(f->block, stmmac_setup_tc_block_cb, priv);
  3216. return 0;
  3217. default:
  3218. return -EOPNOTSUPP;
  3219. }
  3220. }
  3221. static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
  3222. void *type_data)
  3223. {
  3224. struct stmmac_priv *priv = netdev_priv(ndev);
  3225. switch (type) {
  3226. case TC_SETUP_BLOCK:
  3227. return stmmac_setup_tc_block(priv, type_data);
  3228. case TC_SETUP_QDISC_CBS:
  3229. return stmmac_tc_setup_cbs(priv, priv, type_data);
  3230. default:
  3231. return -EOPNOTSUPP;
  3232. }
  3233. }
  3234. static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
  3235. struct net_device *sb_dev,
  3236. select_queue_fallback_t fallback)
  3237. {
  3238. if (skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
  3239. /*
  3240. * There is no way to determine the number of TSO
  3241. * capable Queues. Let's use always the Queue 0
  3242. * because if TSO is supported then at least this
  3243. * one will be capable.
  3244. */
  3245. return 0;
  3246. }
  3247. return fallback(dev, skb, NULL) % dev->real_num_tx_queues;
  3248. }
  3249. static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
  3250. {
  3251. struct stmmac_priv *priv = netdev_priv(ndev);
  3252. int ret = 0;
  3253. ret = eth_mac_addr(ndev, addr);
  3254. if (ret)
  3255. return ret;
  3256. stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
  3257. return ret;
  3258. }
  3259. #ifdef CONFIG_DEBUG_FS
  3260. static struct dentry *stmmac_fs_dir;
  3261. static void sysfs_display_ring(void *head, int size, int extend_desc,
  3262. struct seq_file *seq)
  3263. {
  3264. int i;
  3265. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  3266. struct dma_desc *p = (struct dma_desc *)head;
  3267. for (i = 0; i < size; i++) {
  3268. if (extend_desc) {
  3269. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  3270. i, (unsigned int)virt_to_phys(ep),
  3271. le32_to_cpu(ep->basic.des0),
  3272. le32_to_cpu(ep->basic.des1),
  3273. le32_to_cpu(ep->basic.des2),
  3274. le32_to_cpu(ep->basic.des3));
  3275. ep++;
  3276. } else {
  3277. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  3278. i, (unsigned int)virt_to_phys(p),
  3279. le32_to_cpu(p->des0), le32_to_cpu(p->des1),
  3280. le32_to_cpu(p->des2), le32_to_cpu(p->des3));
  3281. p++;
  3282. }
  3283. seq_printf(seq, "\n");
  3284. }
  3285. }
  3286. static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
  3287. {
  3288. struct net_device *dev = seq->private;
  3289. struct stmmac_priv *priv = netdev_priv(dev);
  3290. u32 rx_count = priv->plat->rx_queues_to_use;
  3291. u32 tx_count = priv->plat->tx_queues_to_use;
  3292. u32 queue;
  3293. if ((dev->flags & IFF_UP) == 0)
  3294. return 0;
  3295. for (queue = 0; queue < rx_count; queue++) {
  3296. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  3297. seq_printf(seq, "RX Queue %d:\n", queue);
  3298. if (priv->extend_desc) {
  3299. seq_printf(seq, "Extended descriptor ring:\n");
  3300. sysfs_display_ring((void *)rx_q->dma_erx,
  3301. DMA_RX_SIZE, 1, seq);
  3302. } else {
  3303. seq_printf(seq, "Descriptor ring:\n");
  3304. sysfs_display_ring((void *)rx_q->dma_rx,
  3305. DMA_RX_SIZE, 0, seq);
  3306. }
  3307. }
  3308. for (queue = 0; queue < tx_count; queue++) {
  3309. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  3310. seq_printf(seq, "TX Queue %d:\n", queue);
  3311. if (priv->extend_desc) {
  3312. seq_printf(seq, "Extended descriptor ring:\n");
  3313. sysfs_display_ring((void *)tx_q->dma_etx,
  3314. DMA_TX_SIZE, 1, seq);
  3315. } else {
  3316. seq_printf(seq, "Descriptor ring:\n");
  3317. sysfs_display_ring((void *)tx_q->dma_tx,
  3318. DMA_TX_SIZE, 0, seq);
  3319. }
  3320. }
  3321. return 0;
  3322. }
  3323. static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
  3324. {
  3325. return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
  3326. }
  3327. /* Debugfs files, should appear in /sys/kernel/debug/stmmaceth/eth0 */
  3328. static const struct file_operations stmmac_rings_status_fops = {
  3329. .owner = THIS_MODULE,
  3330. .open = stmmac_sysfs_ring_open,
  3331. .read = seq_read,
  3332. .llseek = seq_lseek,
  3333. .release = single_release,
  3334. };
  3335. static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
  3336. {
  3337. struct net_device *dev = seq->private;
  3338. struct stmmac_priv *priv = netdev_priv(dev);
  3339. if (!priv->hw_cap_support) {
  3340. seq_printf(seq, "DMA HW features not supported\n");
  3341. return 0;
  3342. }
  3343. seq_printf(seq, "==============================\n");
  3344. seq_printf(seq, "\tDMA HW features\n");
  3345. seq_printf(seq, "==============================\n");
  3346. seq_printf(seq, "\t10/100 Mbps: %s\n",
  3347. (priv->dma_cap.mbps_10_100) ? "Y" : "N");
  3348. seq_printf(seq, "\t1000 Mbps: %s\n",
  3349. (priv->dma_cap.mbps_1000) ? "Y" : "N");
  3350. seq_printf(seq, "\tHalf duplex: %s\n",
  3351. (priv->dma_cap.half_duplex) ? "Y" : "N");
  3352. seq_printf(seq, "\tHash Filter: %s\n",
  3353. (priv->dma_cap.hash_filter) ? "Y" : "N");
  3354. seq_printf(seq, "\tMultiple MAC address registers: %s\n",
  3355. (priv->dma_cap.multi_addr) ? "Y" : "N");
  3356. seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
  3357. (priv->dma_cap.pcs) ? "Y" : "N");
  3358. seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
  3359. (priv->dma_cap.sma_mdio) ? "Y" : "N");
  3360. seq_printf(seq, "\tPMT Remote wake up: %s\n",
  3361. (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
  3362. seq_printf(seq, "\tPMT Magic Frame: %s\n",
  3363. (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
  3364. seq_printf(seq, "\tRMON module: %s\n",
  3365. (priv->dma_cap.rmon) ? "Y" : "N");
  3366. seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
  3367. (priv->dma_cap.time_stamp) ? "Y" : "N");
  3368. seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
  3369. (priv->dma_cap.atime_stamp) ? "Y" : "N");
  3370. seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
  3371. (priv->dma_cap.eee) ? "Y" : "N");
  3372. seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
  3373. seq_printf(seq, "\tChecksum Offload in TX: %s\n",
  3374. (priv->dma_cap.tx_coe) ? "Y" : "N");
  3375. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  3376. seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
  3377. (priv->dma_cap.rx_coe) ? "Y" : "N");
  3378. } else {
  3379. seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
  3380. (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
  3381. seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
  3382. (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
  3383. }
  3384. seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
  3385. (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
  3386. seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
  3387. priv->dma_cap.number_rx_channel);
  3388. seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
  3389. priv->dma_cap.number_tx_channel);
  3390. seq_printf(seq, "\tEnhanced descriptors: %s\n",
  3391. (priv->dma_cap.enh_desc) ? "Y" : "N");
  3392. return 0;
  3393. }
  3394. static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
  3395. {
  3396. return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
  3397. }
  3398. static const struct file_operations stmmac_dma_cap_fops = {
  3399. .owner = THIS_MODULE,
  3400. .open = stmmac_sysfs_dma_cap_open,
  3401. .read = seq_read,
  3402. .llseek = seq_lseek,
  3403. .release = single_release,
  3404. };
  3405. static int stmmac_init_fs(struct net_device *dev)
  3406. {
  3407. struct stmmac_priv *priv = netdev_priv(dev);
  3408. /* Create per netdev entries */
  3409. priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
  3410. if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
  3411. netdev_err(priv->dev, "ERROR failed to create debugfs directory\n");
  3412. return -ENOMEM;
  3413. }
  3414. /* Entry to report DMA RX/TX rings */
  3415. priv->dbgfs_rings_status =
  3416. debugfs_create_file("descriptors_status", 0444,
  3417. priv->dbgfs_dir, dev,
  3418. &stmmac_rings_status_fops);
  3419. if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
  3420. netdev_err(priv->dev, "ERROR creating stmmac ring debugfs file\n");
  3421. debugfs_remove_recursive(priv->dbgfs_dir);
  3422. return -ENOMEM;
  3423. }
  3424. /* Entry to report the DMA HW features */
  3425. priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", 0444,
  3426. priv->dbgfs_dir,
  3427. dev, &stmmac_dma_cap_fops);
  3428. if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
  3429. netdev_err(priv->dev, "ERROR creating stmmac MMC debugfs file\n");
  3430. debugfs_remove_recursive(priv->dbgfs_dir);
  3431. return -ENOMEM;
  3432. }
  3433. return 0;
  3434. }
  3435. static void stmmac_exit_fs(struct net_device *dev)
  3436. {
  3437. struct stmmac_priv *priv = netdev_priv(dev);
  3438. debugfs_remove_recursive(priv->dbgfs_dir);
  3439. }
  3440. #endif /* CONFIG_DEBUG_FS */
  3441. static const struct net_device_ops stmmac_netdev_ops = {
  3442. .ndo_open = stmmac_open,
  3443. .ndo_start_xmit = stmmac_xmit,
  3444. .ndo_stop = stmmac_release,
  3445. .ndo_change_mtu = stmmac_change_mtu,
  3446. .ndo_fix_features = stmmac_fix_features,
  3447. .ndo_set_features = stmmac_set_features,
  3448. .ndo_set_rx_mode = stmmac_set_rx_mode,
  3449. .ndo_tx_timeout = stmmac_tx_timeout,
  3450. .ndo_do_ioctl = stmmac_ioctl,
  3451. .ndo_setup_tc = stmmac_setup_tc,
  3452. .ndo_select_queue = stmmac_select_queue,
  3453. #ifdef CONFIG_NET_POLL_CONTROLLER
  3454. .ndo_poll_controller = stmmac_poll_controller,
  3455. #endif
  3456. .ndo_set_mac_address = stmmac_set_mac_address,
  3457. };
  3458. static void stmmac_reset_subtask(struct stmmac_priv *priv)
  3459. {
  3460. if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
  3461. return;
  3462. if (test_bit(STMMAC_DOWN, &priv->state))
  3463. return;
  3464. netdev_err(priv->dev, "Reset adapter.\n");
  3465. rtnl_lock();
  3466. netif_trans_update(priv->dev);
  3467. while (test_and_set_bit(STMMAC_RESETING, &priv->state))
  3468. usleep_range(1000, 2000);
  3469. set_bit(STMMAC_DOWN, &priv->state);
  3470. dev_close(priv->dev);
  3471. dev_open(priv->dev);
  3472. clear_bit(STMMAC_DOWN, &priv->state);
  3473. clear_bit(STMMAC_RESETING, &priv->state);
  3474. rtnl_unlock();
  3475. }
  3476. static void stmmac_service_task(struct work_struct *work)
  3477. {
  3478. struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
  3479. service_task);
  3480. stmmac_reset_subtask(priv);
  3481. clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
  3482. }
  3483. /**
  3484. * stmmac_hw_init - Init the MAC device
  3485. * @priv: driver private structure
  3486. * Description: this function is to configure the MAC device according to
  3487. * some platform parameters or the HW capability register. It prepares the
  3488. * driver to use either ring or chain modes and to setup either enhanced or
  3489. * normal descriptors.
  3490. */
  3491. static int stmmac_hw_init(struct stmmac_priv *priv)
  3492. {
  3493. int ret;
  3494. /* dwmac-sun8i only work in chain mode */
  3495. if (priv->plat->has_sun8i)
  3496. chain_mode = 1;
  3497. priv->chain_mode = chain_mode;
  3498. /* Initialize HW Interface */
  3499. ret = stmmac_hwif_init(priv);
  3500. if (ret)
  3501. return ret;
  3502. /* Get the HW capability (new GMAC newer than 3.50a) */
  3503. priv->hw_cap_support = stmmac_get_hw_features(priv);
  3504. if (priv->hw_cap_support) {
  3505. dev_info(priv->device, "DMA HW capability register supported\n");
  3506. /* We can override some gmac/dma configuration fields: e.g.
  3507. * enh_desc, tx_coe (e.g. that are passed through the
  3508. * platform) with the values from the HW capability
  3509. * register (if supported).
  3510. */
  3511. priv->plat->enh_desc = priv->dma_cap.enh_desc;
  3512. priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
  3513. priv->hw->pmt = priv->plat->pmt;
  3514. /* TXCOE doesn't work in thresh DMA mode */
  3515. if (priv->plat->force_thresh_dma_mode)
  3516. priv->plat->tx_coe = 0;
  3517. else
  3518. priv->plat->tx_coe = priv->dma_cap.tx_coe;
  3519. /* In case of GMAC4 rx_coe is from HW cap register. */
  3520. priv->plat->rx_coe = priv->dma_cap.rx_coe;
  3521. if (priv->dma_cap.rx_coe_type2)
  3522. priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
  3523. else if (priv->dma_cap.rx_coe_type1)
  3524. priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
  3525. } else {
  3526. dev_info(priv->device, "No HW DMA feature register supported\n");
  3527. }
  3528. if (priv->plat->rx_coe) {
  3529. priv->hw->rx_csum = priv->plat->rx_coe;
  3530. dev_info(priv->device, "RX Checksum Offload Engine supported\n");
  3531. if (priv->synopsys_id < DWMAC_CORE_4_00)
  3532. dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
  3533. }
  3534. if (priv->plat->tx_coe)
  3535. dev_info(priv->device, "TX Checksum insertion supported\n");
  3536. if (priv->plat->pmt) {
  3537. dev_info(priv->device, "Wake-Up On Lan supported\n");
  3538. device_set_wakeup_capable(priv->device, 1);
  3539. }
  3540. if (priv->dma_cap.tsoen)
  3541. dev_info(priv->device, "TSO supported\n");
  3542. /* Run HW quirks, if any */
  3543. if (priv->hwif_quirks) {
  3544. ret = priv->hwif_quirks(priv);
  3545. if (ret)
  3546. return ret;
  3547. }
  3548. /* Rx Watchdog is available in the COREs newer than the 3.40.
  3549. * In some case, for example on bugged HW this feature
  3550. * has to be disable and this can be done by passing the
  3551. * riwt_off field from the platform.
  3552. */
  3553. if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
  3554. (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
  3555. priv->use_riwt = 1;
  3556. dev_info(priv->device,
  3557. "Enable RX Mitigation via HW Watchdog Timer\n");
  3558. }
  3559. return 0;
  3560. }
  3561. /**
  3562. * stmmac_dvr_probe
  3563. * @device: device pointer
  3564. * @plat_dat: platform data pointer
  3565. * @res: stmmac resource pointer
  3566. * Description: this is the main probe function used to
  3567. * call the alloc_etherdev, allocate the priv structure.
  3568. * Return:
  3569. * returns 0 on success, otherwise errno.
  3570. */
  3571. int stmmac_dvr_probe(struct device *device,
  3572. struct plat_stmmacenet_data *plat_dat,
  3573. struct stmmac_resources *res)
  3574. {
  3575. struct net_device *ndev = NULL;
  3576. struct stmmac_priv *priv;
  3577. u32 queue, maxq;
  3578. int ret = 0;
  3579. ndev = alloc_etherdev_mqs(sizeof(struct stmmac_priv),
  3580. MTL_MAX_TX_QUEUES,
  3581. MTL_MAX_RX_QUEUES);
  3582. if (!ndev)
  3583. return -ENOMEM;
  3584. SET_NETDEV_DEV(ndev, device);
  3585. priv = netdev_priv(ndev);
  3586. priv->device = device;
  3587. priv->dev = ndev;
  3588. stmmac_set_ethtool_ops(ndev);
  3589. priv->pause = pause;
  3590. priv->plat = plat_dat;
  3591. priv->ioaddr = res->addr;
  3592. priv->dev->base_addr = (unsigned long)res->addr;
  3593. priv->dev->irq = res->irq;
  3594. priv->wol_irq = res->wol_irq;
  3595. priv->lpi_irq = res->lpi_irq;
  3596. if (res->mac)
  3597. memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
  3598. dev_set_drvdata(device, priv->dev);
  3599. /* Verify driver arguments */
  3600. stmmac_verify_args();
  3601. /* Allocate workqueue */
  3602. priv->wq = create_singlethread_workqueue("stmmac_wq");
  3603. if (!priv->wq) {
  3604. dev_err(priv->device, "failed to create workqueue\n");
  3605. ret = -ENOMEM;
  3606. goto error_wq;
  3607. }
  3608. INIT_WORK(&priv->service_task, stmmac_service_task);
  3609. /* Override with kernel parameters if supplied XXX CRS XXX
  3610. * this needs to have multiple instances
  3611. */
  3612. if ((phyaddr >= 0) && (phyaddr <= 31))
  3613. priv->plat->phy_addr = phyaddr;
  3614. if (priv->plat->stmmac_rst) {
  3615. ret = reset_control_assert(priv->plat->stmmac_rst);
  3616. reset_control_deassert(priv->plat->stmmac_rst);
  3617. /* Some reset controllers have only reset callback instead of
  3618. * assert + deassert callbacks pair.
  3619. */
  3620. if (ret == -ENOTSUPP)
  3621. reset_control_reset(priv->plat->stmmac_rst);
  3622. }
  3623. /* Init MAC and get the capabilities */
  3624. ret = stmmac_hw_init(priv);
  3625. if (ret)
  3626. goto error_hw_init;
  3627. stmmac_check_ether_addr(priv);
  3628. /* Configure real RX and TX queues */
  3629. netif_set_real_num_rx_queues(ndev, priv->plat->rx_queues_to_use);
  3630. netif_set_real_num_tx_queues(ndev, priv->plat->tx_queues_to_use);
  3631. ndev->netdev_ops = &stmmac_netdev_ops;
  3632. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  3633. NETIF_F_RXCSUM;
  3634. ret = stmmac_tc_init(priv, priv);
  3635. if (!ret) {
  3636. ndev->hw_features |= NETIF_F_HW_TC;
  3637. }
  3638. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  3639. ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  3640. priv->tso = true;
  3641. dev_info(priv->device, "TSO feature enabled\n");
  3642. }
  3643. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  3644. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  3645. #ifdef STMMAC_VLAN_TAG_USED
  3646. /* Both mac100 and gmac support receive VLAN tag detection */
  3647. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
  3648. #endif
  3649. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  3650. /* MTU range: 46 - hw-specific max */
  3651. ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
  3652. if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
  3653. ndev->max_mtu = JUMBO_LEN;
  3654. else if (priv->plat->has_xgmac)
  3655. ndev->max_mtu = XGMAC_JUMBO_LEN;
  3656. else
  3657. ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  3658. /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
  3659. * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
  3660. */
  3661. if ((priv->plat->maxmtu < ndev->max_mtu) &&
  3662. (priv->plat->maxmtu >= ndev->min_mtu))
  3663. ndev->max_mtu = priv->plat->maxmtu;
  3664. else if (priv->plat->maxmtu < ndev->min_mtu)
  3665. dev_warn(priv->device,
  3666. "%s: warning: maxmtu having invalid value (%d)\n",
  3667. __func__, priv->plat->maxmtu);
  3668. if (flow_ctrl)
  3669. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  3670. /* Setup channels NAPI */
  3671. maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
  3672. for (queue = 0; queue < maxq; queue++) {
  3673. struct stmmac_channel *ch = &priv->channel[queue];
  3674. ch->priv_data = priv;
  3675. ch->index = queue;
  3676. if (queue < priv->plat->rx_queues_to_use)
  3677. ch->has_rx = true;
  3678. if (queue < priv->plat->tx_queues_to_use)
  3679. ch->has_tx = true;
  3680. netif_napi_add(ndev, &ch->napi, stmmac_napi_poll,
  3681. NAPI_POLL_WEIGHT);
  3682. }
  3683. mutex_init(&priv->lock);
  3684. /* If a specific clk_csr value is passed from the platform
  3685. * this means that the CSR Clock Range selection cannot be
  3686. * changed at run-time and it is fixed. Viceversa the driver'll try to
  3687. * set the MDC clock dynamically according to the csr actual
  3688. * clock input.
  3689. */
  3690. if (!priv->plat->clk_csr)
  3691. stmmac_clk_csr_set(priv);
  3692. else
  3693. priv->clk_csr = priv->plat->clk_csr;
  3694. stmmac_check_pcs_mode(priv);
  3695. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  3696. priv->hw->pcs != STMMAC_PCS_TBI &&
  3697. priv->hw->pcs != STMMAC_PCS_RTBI) {
  3698. /* MDIO bus Registration */
  3699. ret = stmmac_mdio_register(ndev);
  3700. if (ret < 0) {
  3701. dev_err(priv->device,
  3702. "%s: MDIO bus (id: %d) registration failed",
  3703. __func__, priv->plat->bus_id);
  3704. goto error_mdio_register;
  3705. }
  3706. }
  3707. ret = register_netdev(ndev);
  3708. if (ret) {
  3709. dev_err(priv->device, "%s: ERROR %i registering the device\n",
  3710. __func__, ret);
  3711. goto error_netdev_register;
  3712. }
  3713. #ifdef CONFIG_DEBUG_FS
  3714. ret = stmmac_init_fs(ndev);
  3715. if (ret < 0)
  3716. netdev_warn(priv->dev, "%s: failed debugFS registration\n",
  3717. __func__);
  3718. #endif
  3719. return ret;
  3720. error_netdev_register:
  3721. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  3722. priv->hw->pcs != STMMAC_PCS_TBI &&
  3723. priv->hw->pcs != STMMAC_PCS_RTBI)
  3724. stmmac_mdio_unregister(ndev);
  3725. error_mdio_register:
  3726. for (queue = 0; queue < maxq; queue++) {
  3727. struct stmmac_channel *ch = &priv->channel[queue];
  3728. netif_napi_del(&ch->napi);
  3729. }
  3730. error_hw_init:
  3731. destroy_workqueue(priv->wq);
  3732. error_wq:
  3733. free_netdev(ndev);
  3734. return ret;
  3735. }
  3736. EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
  3737. /**
  3738. * stmmac_dvr_remove
  3739. * @dev: device pointer
  3740. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  3741. * changes the link status, releases the DMA descriptor rings.
  3742. */
  3743. int stmmac_dvr_remove(struct device *dev)
  3744. {
  3745. struct net_device *ndev = dev_get_drvdata(dev);
  3746. struct stmmac_priv *priv = netdev_priv(ndev);
  3747. netdev_info(priv->dev, "%s: removing driver", __func__);
  3748. #ifdef CONFIG_DEBUG_FS
  3749. stmmac_exit_fs(ndev);
  3750. #endif
  3751. stmmac_stop_all_dma(priv);
  3752. stmmac_mac_set(priv, priv->ioaddr, false);
  3753. netif_carrier_off(ndev);
  3754. unregister_netdev(ndev);
  3755. if (priv->plat->stmmac_rst)
  3756. reset_control_assert(priv->plat->stmmac_rst);
  3757. clk_disable_unprepare(priv->plat->pclk);
  3758. clk_disable_unprepare(priv->plat->stmmac_clk);
  3759. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  3760. priv->hw->pcs != STMMAC_PCS_TBI &&
  3761. priv->hw->pcs != STMMAC_PCS_RTBI)
  3762. stmmac_mdio_unregister(ndev);
  3763. destroy_workqueue(priv->wq);
  3764. mutex_destroy(&priv->lock);
  3765. free_netdev(ndev);
  3766. return 0;
  3767. }
  3768. EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
  3769. /**
  3770. * stmmac_suspend - suspend callback
  3771. * @dev: device pointer
  3772. * Description: this is the function to suspend the device and it is called
  3773. * by the platform driver to stop the network queue, release the resources,
  3774. * program the PMT register (for WoL), clean and release driver resources.
  3775. */
  3776. int stmmac_suspend(struct device *dev)
  3777. {
  3778. struct net_device *ndev = dev_get_drvdata(dev);
  3779. struct stmmac_priv *priv = netdev_priv(ndev);
  3780. if (!ndev || !netif_running(ndev))
  3781. return 0;
  3782. if (ndev->phydev)
  3783. phy_stop(ndev->phydev);
  3784. mutex_lock(&priv->lock);
  3785. netif_device_detach(ndev);
  3786. stmmac_stop_all_queues(priv);
  3787. stmmac_disable_all_queues(priv);
  3788. /* Stop TX/RX DMA */
  3789. stmmac_stop_all_dma(priv);
  3790. /* Enable Power down mode by programming the PMT regs */
  3791. if (device_may_wakeup(priv->device)) {
  3792. stmmac_pmt(priv, priv->hw, priv->wolopts);
  3793. priv->irq_wake = 1;
  3794. } else {
  3795. stmmac_mac_set(priv, priv->ioaddr, false);
  3796. pinctrl_pm_select_sleep_state(priv->device);
  3797. /* Disable clock in case of PWM is off */
  3798. clk_disable(priv->plat->pclk);
  3799. clk_disable(priv->plat->stmmac_clk);
  3800. }
  3801. mutex_unlock(&priv->lock);
  3802. priv->oldlink = false;
  3803. priv->speed = SPEED_UNKNOWN;
  3804. priv->oldduplex = DUPLEX_UNKNOWN;
  3805. return 0;
  3806. }
  3807. EXPORT_SYMBOL_GPL(stmmac_suspend);
  3808. /**
  3809. * stmmac_reset_queues_param - reset queue parameters
  3810. * @dev: device pointer
  3811. */
  3812. static void stmmac_reset_queues_param(struct stmmac_priv *priv)
  3813. {
  3814. u32 rx_cnt = priv->plat->rx_queues_to_use;
  3815. u32 tx_cnt = priv->plat->tx_queues_to_use;
  3816. u32 queue;
  3817. for (queue = 0; queue < rx_cnt; queue++) {
  3818. struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
  3819. rx_q->cur_rx = 0;
  3820. rx_q->dirty_rx = 0;
  3821. }
  3822. for (queue = 0; queue < tx_cnt; queue++) {
  3823. struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
  3824. tx_q->cur_tx = 0;
  3825. tx_q->dirty_tx = 0;
  3826. tx_q->mss = 0;
  3827. }
  3828. }
  3829. /**
  3830. * stmmac_resume - resume callback
  3831. * @dev: device pointer
  3832. * Description: when resume this function is invoked to setup the DMA and CORE
  3833. * in a usable state.
  3834. */
  3835. int stmmac_resume(struct device *dev)
  3836. {
  3837. struct net_device *ndev = dev_get_drvdata(dev);
  3838. struct stmmac_priv *priv = netdev_priv(ndev);
  3839. if (!netif_running(ndev))
  3840. return 0;
  3841. /* Power Down bit, into the PM register, is cleared
  3842. * automatically as soon as a magic packet or a Wake-up frame
  3843. * is received. Anyway, it's better to manually clear
  3844. * this bit because it can generate problems while resuming
  3845. * from another devices (e.g. serial console).
  3846. */
  3847. if (device_may_wakeup(priv->device)) {
  3848. mutex_lock(&priv->lock);
  3849. stmmac_pmt(priv, priv->hw, 0);
  3850. mutex_unlock(&priv->lock);
  3851. priv->irq_wake = 0;
  3852. } else {
  3853. pinctrl_pm_select_default_state(priv->device);
  3854. /* enable the clk previously disabled */
  3855. clk_enable(priv->plat->stmmac_clk);
  3856. clk_enable(priv->plat->pclk);
  3857. /* reset the phy so that it's ready */
  3858. if (priv->mii)
  3859. stmmac_mdio_reset(priv->mii);
  3860. }
  3861. netif_device_attach(ndev);
  3862. mutex_lock(&priv->lock);
  3863. stmmac_reset_queues_param(priv);
  3864. stmmac_clear_descriptors(priv);
  3865. stmmac_hw_setup(ndev, false);
  3866. stmmac_init_tx_coalesce(priv);
  3867. stmmac_set_rx_mode(ndev);
  3868. stmmac_enable_all_queues(priv);
  3869. stmmac_start_all_queues(priv);
  3870. mutex_unlock(&priv->lock);
  3871. if (ndev->phydev)
  3872. phy_start(ndev->phydev);
  3873. return 0;
  3874. }
  3875. EXPORT_SYMBOL_GPL(stmmac_resume);
  3876. #ifndef MODULE
  3877. static int __init stmmac_cmdline_opt(char *str)
  3878. {
  3879. char *opt;
  3880. if (!str || !*str)
  3881. return -EINVAL;
  3882. while ((opt = strsep(&str, ",")) != NULL) {
  3883. if (!strncmp(opt, "debug:", 6)) {
  3884. if (kstrtoint(opt + 6, 0, &debug))
  3885. goto err;
  3886. } else if (!strncmp(opt, "phyaddr:", 8)) {
  3887. if (kstrtoint(opt + 8, 0, &phyaddr))
  3888. goto err;
  3889. } else if (!strncmp(opt, "buf_sz:", 7)) {
  3890. if (kstrtoint(opt + 7, 0, &buf_sz))
  3891. goto err;
  3892. } else if (!strncmp(opt, "tc:", 3)) {
  3893. if (kstrtoint(opt + 3, 0, &tc))
  3894. goto err;
  3895. } else if (!strncmp(opt, "watchdog:", 9)) {
  3896. if (kstrtoint(opt + 9, 0, &watchdog))
  3897. goto err;
  3898. } else if (!strncmp(opt, "flow_ctrl:", 10)) {
  3899. if (kstrtoint(opt + 10, 0, &flow_ctrl))
  3900. goto err;
  3901. } else if (!strncmp(opt, "pause:", 6)) {
  3902. if (kstrtoint(opt + 6, 0, &pause))
  3903. goto err;
  3904. } else if (!strncmp(opt, "eee_timer:", 10)) {
  3905. if (kstrtoint(opt + 10, 0, &eee_timer))
  3906. goto err;
  3907. } else if (!strncmp(opt, "chain_mode:", 11)) {
  3908. if (kstrtoint(opt + 11, 0, &chain_mode))
  3909. goto err;
  3910. }
  3911. }
  3912. return 0;
  3913. err:
  3914. pr_err("%s: ERROR broken module parameter conversion", __func__);
  3915. return -EINVAL;
  3916. }
  3917. __setup("stmmaceth=", stmmac_cmdline_opt);
  3918. #endif /* MODULE */
  3919. static int __init stmmac_init(void)
  3920. {
  3921. #ifdef CONFIG_DEBUG_FS
  3922. /* Create debugfs main directory if it doesn't exist yet */
  3923. if (!stmmac_fs_dir) {
  3924. stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
  3925. if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
  3926. pr_err("ERROR %s, debugfs create directory failed\n",
  3927. STMMAC_RESOURCE_NAME);
  3928. return -ENOMEM;
  3929. }
  3930. }
  3931. #endif
  3932. return 0;
  3933. }
  3934. static void __exit stmmac_exit(void)
  3935. {
  3936. #ifdef CONFIG_DEBUG_FS
  3937. debugfs_remove_recursive(stmmac_fs_dir);
  3938. #endif
  3939. }
  3940. module_init(stmmac_init)
  3941. module_exit(stmmac_exit)
  3942. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
  3943. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  3944. MODULE_LICENSE("GPL");