stmmac_main.c 125 KB

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