stmmac_main.c 128 KB

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