stmmac_main.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588
  1. /*******************************************************************************
  2. This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
  3. ST Ethernet IPs are built around a Synopsys IP Core.
  4. Copyright(C) 2007-2011 STMicroelectronics Ltd
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms and conditions of the GNU General Public License,
  7. version 2, as published by the Free Software Foundation.
  8. This program is distributed in the hope it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. The full GNU General Public License is included in this distribution in
  13. the file called "COPYING".
  14. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  15. Documentation available at:
  16. http://www.stlinux.com
  17. Support available at:
  18. https://bugzilla.stlinux.com/
  19. *******************************************************************************/
  20. #include <linux/clk.h>
  21. #include <linux/kernel.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ip.h>
  24. #include <linux/tcp.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/crc32.h>
  29. #include <linux/mii.h>
  30. #include <linux/if.h>
  31. #include <linux/if_vlan.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/slab.h>
  34. #include <linux/prefetch.h>
  35. #include <linux/pinctrl/consumer.h>
  36. #ifdef CONFIG_DEBUG_FS
  37. #include <linux/debugfs.h>
  38. #include <linux/seq_file.h>
  39. #endif /* CONFIG_DEBUG_FS */
  40. #include <linux/net_tstamp.h>
  41. #include "stmmac_ptp.h"
  42. #include "stmmac.h"
  43. #include <linux/reset.h>
  44. #include <linux/of_mdio.h>
  45. #include "dwmac1000.h"
  46. #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
  47. #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
  48. /* Module parameters */
  49. #define TX_TIMEO 5000
  50. static int watchdog = TX_TIMEO;
  51. module_param(watchdog, int, S_IRUGO | S_IWUSR);
  52. MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
  53. static int debug = -1;
  54. module_param(debug, int, S_IRUGO | S_IWUSR);
  55. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  56. static int phyaddr = -1;
  57. module_param(phyaddr, int, S_IRUGO);
  58. MODULE_PARM_DESC(phyaddr, "Physical device address");
  59. #define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
  60. #define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
  61. static int flow_ctrl = FLOW_OFF;
  62. module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
  63. MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
  64. static int pause = PAUSE_TIME;
  65. module_param(pause, int, S_IRUGO | S_IWUSR);
  66. MODULE_PARM_DESC(pause, "Flow Control Pause Time");
  67. #define TC_DEFAULT 64
  68. static int tc = TC_DEFAULT;
  69. module_param(tc, int, S_IRUGO | S_IWUSR);
  70. MODULE_PARM_DESC(tc, "DMA threshold control value");
  71. #define DEFAULT_BUFSIZE 1536
  72. static int buf_sz = DEFAULT_BUFSIZE;
  73. module_param(buf_sz, int, S_IRUGO | S_IWUSR);
  74. MODULE_PARM_DESC(buf_sz, "DMA buffer size");
  75. #define STMMAC_RX_COPYBREAK 256
  76. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  77. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  78. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  79. #define STMMAC_DEFAULT_LPI_TIMER 1000
  80. static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  81. module_param(eee_timer, int, S_IRUGO | S_IWUSR);
  82. MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
  83. #define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
  84. /* By default the driver will use the ring mode to manage tx and rx descriptors,
  85. * but allow user to force to use the chain instead of the ring
  86. */
  87. static unsigned int chain_mode;
  88. module_param(chain_mode, int, S_IRUGO);
  89. MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
  90. static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
  91. #ifdef CONFIG_DEBUG_FS
  92. static int stmmac_init_fs(struct net_device *dev);
  93. static void stmmac_exit_fs(struct net_device *dev);
  94. #endif
  95. #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
  96. /**
  97. * stmmac_verify_args - verify the driver parameters.
  98. * Description: it checks the driver parameters and set a default in case of
  99. * errors.
  100. */
  101. static void stmmac_verify_args(void)
  102. {
  103. if (unlikely(watchdog < 0))
  104. watchdog = TX_TIMEO;
  105. if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
  106. buf_sz = DEFAULT_BUFSIZE;
  107. if (unlikely(flow_ctrl > 1))
  108. flow_ctrl = FLOW_AUTO;
  109. else if (likely(flow_ctrl < 0))
  110. flow_ctrl = FLOW_OFF;
  111. if (unlikely((pause < 0) || (pause > 0xffff)))
  112. pause = PAUSE_TIME;
  113. if (eee_timer < 0)
  114. eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  115. }
  116. /**
  117. * stmmac_clk_csr_set - dynamically set the MDC clock
  118. * @priv: driver private structure
  119. * Description: this is to dynamically set the MDC clock according to the csr
  120. * clock input.
  121. * Note:
  122. * If a specific clk_csr value is passed from the platform
  123. * this means that the CSR Clock Range selection cannot be
  124. * changed at run-time and it is fixed (as reported in the driver
  125. * documentation). Viceversa the driver will try to set the MDC
  126. * clock dynamically according to the actual clock input.
  127. */
  128. static void stmmac_clk_csr_set(struct stmmac_priv *priv)
  129. {
  130. u32 clk_rate;
  131. clk_rate = clk_get_rate(priv->plat->stmmac_clk);
  132. /* Platform provided default clk_csr would be assumed valid
  133. * for all other cases except for the below mentioned ones.
  134. * For values higher than the IEEE 802.3 specified frequency
  135. * we can not estimate the proper divider as it is not known
  136. * the frequency of clk_csr_i. So we do not change the default
  137. * divider.
  138. */
  139. if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
  140. if (clk_rate < CSR_F_35M)
  141. priv->clk_csr = STMMAC_CSR_20_35M;
  142. else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
  143. priv->clk_csr = STMMAC_CSR_35_60M;
  144. else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
  145. priv->clk_csr = STMMAC_CSR_60_100M;
  146. else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
  147. priv->clk_csr = STMMAC_CSR_100_150M;
  148. else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
  149. priv->clk_csr = STMMAC_CSR_150_250M;
  150. else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
  151. priv->clk_csr = STMMAC_CSR_250_300M;
  152. }
  153. }
  154. static void print_pkt(unsigned char *buf, int len)
  155. {
  156. pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
  157. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
  158. }
  159. static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
  160. {
  161. u32 avail;
  162. if (priv->dirty_tx > priv->cur_tx)
  163. avail = priv->dirty_tx - priv->cur_tx - 1;
  164. else
  165. avail = DMA_TX_SIZE - priv->cur_tx + priv->dirty_tx - 1;
  166. return avail;
  167. }
  168. static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv)
  169. {
  170. u32 dirty;
  171. if (priv->dirty_rx <= priv->cur_rx)
  172. dirty = priv->cur_rx - priv->dirty_rx;
  173. else
  174. dirty = DMA_RX_SIZE - priv->dirty_rx + priv->cur_rx;
  175. return dirty;
  176. }
  177. /**
  178. * stmmac_hw_fix_mac_speed - callback for speed selection
  179. * @priv: driver private structure
  180. * Description: on some platforms (e.g. ST), some HW system configuration
  181. * registers have to be set according to the link speed negotiated.
  182. */
  183. static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
  184. {
  185. struct net_device *ndev = priv->dev;
  186. struct phy_device *phydev = ndev->phydev;
  187. if (likely(priv->plat->fix_mac_speed))
  188. priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
  189. }
  190. /**
  191. * stmmac_enable_eee_mode - check and enter in LPI mode
  192. * @priv: driver private structure
  193. * Description: this function is to verify and enter in LPI mode in case of
  194. * EEE.
  195. */
  196. static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
  197. {
  198. /* Check and enter in LPI mode */
  199. if ((priv->dirty_tx == priv->cur_tx) &&
  200. (priv->tx_path_in_lpi_mode == false))
  201. priv->hw->mac->set_eee_mode(priv->hw,
  202. priv->plat->en_tx_lpi_clockgating);
  203. }
  204. /**
  205. * stmmac_disable_eee_mode - disable and exit from LPI mode
  206. * @priv: driver private structure
  207. * Description: this function is to exit and disable EEE in case of
  208. * LPI state is true. This is called by the xmit.
  209. */
  210. void stmmac_disable_eee_mode(struct stmmac_priv *priv)
  211. {
  212. priv->hw->mac->reset_eee_mode(priv->hw);
  213. del_timer_sync(&priv->eee_ctrl_timer);
  214. priv->tx_path_in_lpi_mode = false;
  215. }
  216. /**
  217. * stmmac_eee_ctrl_timer - EEE TX SW timer.
  218. * @arg : data hook
  219. * Description:
  220. * if there is no data transfer and if we are not in LPI state,
  221. * then MAC Transmitter can be moved to LPI state.
  222. */
  223. static void stmmac_eee_ctrl_timer(unsigned long arg)
  224. {
  225. struct stmmac_priv *priv = (struct stmmac_priv *)arg;
  226. stmmac_enable_eee_mode(priv);
  227. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  228. }
  229. /**
  230. * stmmac_eee_init - init EEE
  231. * @priv: driver private structure
  232. * Description:
  233. * if the GMAC supports the EEE (from the HW cap reg) and the phy device
  234. * can also manage EEE, this function enable the LPI state and start related
  235. * timer.
  236. */
  237. bool stmmac_eee_init(struct stmmac_priv *priv)
  238. {
  239. struct net_device *ndev = priv->dev;
  240. unsigned long flags;
  241. bool ret = false;
  242. /* Using PCS we cannot dial with the phy registers at this stage
  243. * so we do not support extra feature like EEE.
  244. */
  245. if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
  246. (priv->hw->pcs == STMMAC_PCS_TBI) ||
  247. (priv->hw->pcs == STMMAC_PCS_RTBI))
  248. goto out;
  249. /* MAC core supports the EEE feature. */
  250. if (priv->dma_cap.eee) {
  251. int tx_lpi_timer = priv->tx_lpi_timer;
  252. /* Check if the PHY supports EEE */
  253. if (phy_init_eee(ndev->phydev, 1)) {
  254. /* To manage at run-time if the EEE cannot be supported
  255. * anymore (for example because the lp caps have been
  256. * changed).
  257. * In that case the driver disable own timers.
  258. */
  259. spin_lock_irqsave(&priv->lock, flags);
  260. if (priv->eee_active) {
  261. netdev_dbg(priv->dev, "disable EEE\n");
  262. del_timer_sync(&priv->eee_ctrl_timer);
  263. priv->hw->mac->set_eee_timer(priv->hw, 0,
  264. tx_lpi_timer);
  265. }
  266. priv->eee_active = 0;
  267. spin_unlock_irqrestore(&priv->lock, flags);
  268. goto out;
  269. }
  270. /* Activate the EEE and start timers */
  271. spin_lock_irqsave(&priv->lock, flags);
  272. if (!priv->eee_active) {
  273. priv->eee_active = 1;
  274. setup_timer(&priv->eee_ctrl_timer,
  275. stmmac_eee_ctrl_timer,
  276. (unsigned long)priv);
  277. mod_timer(&priv->eee_ctrl_timer,
  278. STMMAC_LPI_T(eee_timer));
  279. priv->hw->mac->set_eee_timer(priv->hw,
  280. STMMAC_DEFAULT_LIT_LS,
  281. tx_lpi_timer);
  282. }
  283. /* Set HW EEE according to the speed */
  284. priv->hw->mac->set_eee_pls(priv->hw, ndev->phydev->link);
  285. ret = true;
  286. spin_unlock_irqrestore(&priv->lock, flags);
  287. netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
  288. }
  289. out:
  290. return ret;
  291. }
  292. /* stmmac_get_tx_hwtstamp - get HW TX timestamps
  293. * @priv: driver private structure
  294. * @p : descriptor pointer
  295. * @skb : the socket buffer
  296. * Description :
  297. * This function will read timestamp from the descriptor & pass it to stack.
  298. * and also perform some sanity checks.
  299. */
  300. static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
  301. struct dma_desc *p, struct sk_buff *skb)
  302. {
  303. struct skb_shared_hwtstamps shhwtstamp;
  304. u64 ns;
  305. if (!priv->hwts_tx_en)
  306. return;
  307. /* exit if skb doesn't support hw tstamp */
  308. if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
  309. return;
  310. /* check tx tstamp status */
  311. if (!priv->hw->desc->get_tx_timestamp_status(p)) {
  312. /* get the valid tstamp */
  313. ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
  314. memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  315. shhwtstamp.hwtstamp = ns_to_ktime(ns);
  316. netdev_info(priv->dev, "get valid TX hw timestamp %llu\n", ns);
  317. /* pass tstamp to stack */
  318. skb_tstamp_tx(skb, &shhwtstamp);
  319. }
  320. return;
  321. }
  322. /* stmmac_get_rx_hwtstamp - get HW RX timestamps
  323. * @priv: driver private structure
  324. * @p : descriptor pointer
  325. * @np : next descriptor pointer
  326. * @skb : the socket buffer
  327. * Description :
  328. * This function will read received packet's timestamp from the descriptor
  329. * and pass it to stack. It also perform some sanity checks.
  330. */
  331. static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
  332. struct dma_desc *np, struct sk_buff *skb)
  333. {
  334. struct skb_shared_hwtstamps *shhwtstamp = NULL;
  335. u64 ns;
  336. if (!priv->hwts_rx_en)
  337. return;
  338. /* Check if timestamp is available */
  339. if (!priv->hw->desc->get_rx_timestamp_status(p, priv->adv_ts)) {
  340. /* For GMAC4, the valid timestamp is from CTX next desc. */
  341. if (priv->plat->has_gmac4)
  342. ns = priv->hw->desc->get_timestamp(np, priv->adv_ts);
  343. else
  344. ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
  345. netdev_info(priv->dev, "get valid RX hw timestamp %llu\n", ns);
  346. shhwtstamp = skb_hwtstamps(skb);
  347. memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  348. shhwtstamp->hwtstamp = ns_to_ktime(ns);
  349. } else {
  350. netdev_err(priv->dev, "cannot get RX hw timestamp\n");
  351. }
  352. }
  353. /**
  354. * stmmac_hwtstamp_ioctl - control hardware timestamping.
  355. * @dev: device pointer.
  356. * @ifr: An IOCTL specific structure, that can contain a pointer to
  357. * a proprietary structure used to pass information to the driver.
  358. * Description:
  359. * This function configures the MAC to enable/disable both outgoing(TX)
  360. * and incoming(RX) packets time stamping based on user input.
  361. * Return Value:
  362. * 0 on success and an appropriate -ve integer on failure.
  363. */
  364. static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  365. {
  366. struct stmmac_priv *priv = netdev_priv(dev);
  367. struct hwtstamp_config config;
  368. struct timespec64 now;
  369. u64 temp = 0;
  370. u32 ptp_v2 = 0;
  371. u32 tstamp_all = 0;
  372. u32 ptp_over_ipv4_udp = 0;
  373. u32 ptp_over_ipv6_udp = 0;
  374. u32 ptp_over_ethernet = 0;
  375. u32 snap_type_sel = 0;
  376. u32 ts_master_en = 0;
  377. u32 ts_event_en = 0;
  378. u32 value = 0;
  379. u32 sec_inc;
  380. if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
  381. netdev_alert(priv->dev, "No support for HW time stamping\n");
  382. priv->hwts_tx_en = 0;
  383. priv->hwts_rx_en = 0;
  384. return -EOPNOTSUPP;
  385. }
  386. if (copy_from_user(&config, ifr->ifr_data,
  387. sizeof(struct hwtstamp_config)))
  388. return -EFAULT;
  389. netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
  390. __func__, config.flags, config.tx_type, config.rx_filter);
  391. /* reserved for future extensions */
  392. if (config.flags)
  393. return -EINVAL;
  394. if (config.tx_type != HWTSTAMP_TX_OFF &&
  395. config.tx_type != HWTSTAMP_TX_ON)
  396. return -ERANGE;
  397. if (priv->adv_ts) {
  398. switch (config.rx_filter) {
  399. case HWTSTAMP_FILTER_NONE:
  400. /* time stamp no incoming packet at all */
  401. config.rx_filter = HWTSTAMP_FILTER_NONE;
  402. break;
  403. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  404. /* PTP v1, UDP, any kind of event packet */
  405. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  406. /* take time stamp for all event messages */
  407. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  408. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  409. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  410. break;
  411. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  412. /* PTP v1, UDP, Sync packet */
  413. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  414. /* take time stamp for SYNC messages only */
  415. ts_event_en = PTP_TCR_TSEVNTENA;
  416. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  417. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  418. break;
  419. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  420. /* PTP v1, UDP, Delay_req packet */
  421. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  422. /* take time stamp for Delay_Req messages only */
  423. ts_master_en = PTP_TCR_TSMSTRENA;
  424. ts_event_en = PTP_TCR_TSEVNTENA;
  425. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  426. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  427. break;
  428. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  429. /* PTP v2, UDP, any kind of event packet */
  430. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  431. ptp_v2 = PTP_TCR_TSVER2ENA;
  432. /* take time stamp for all event messages */
  433. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  434. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  435. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  436. break;
  437. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  438. /* PTP v2, UDP, Sync packet */
  439. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  440. ptp_v2 = PTP_TCR_TSVER2ENA;
  441. /* take time stamp for SYNC messages only */
  442. ts_event_en = PTP_TCR_TSEVNTENA;
  443. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  444. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  445. break;
  446. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  447. /* PTP v2, UDP, Delay_req packet */
  448. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  449. ptp_v2 = PTP_TCR_TSVER2ENA;
  450. /* take time stamp for Delay_Req messages only */
  451. ts_master_en = PTP_TCR_TSMSTRENA;
  452. ts_event_en = PTP_TCR_TSEVNTENA;
  453. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  454. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  455. break;
  456. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  457. /* PTP v2/802.AS1 any layer, any kind of event packet */
  458. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  459. ptp_v2 = PTP_TCR_TSVER2ENA;
  460. /* take time stamp for all event messages */
  461. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  462. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  463. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  464. ptp_over_ethernet = PTP_TCR_TSIPENA;
  465. break;
  466. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  467. /* PTP v2/802.AS1, any layer, Sync packet */
  468. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  469. ptp_v2 = PTP_TCR_TSVER2ENA;
  470. /* take time stamp for SYNC messages only */
  471. ts_event_en = PTP_TCR_TSEVNTENA;
  472. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  473. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  474. ptp_over_ethernet = PTP_TCR_TSIPENA;
  475. break;
  476. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  477. /* PTP v2/802.AS1, any layer, Delay_req packet */
  478. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  479. ptp_v2 = PTP_TCR_TSVER2ENA;
  480. /* take time stamp for Delay_Req messages only */
  481. ts_master_en = PTP_TCR_TSMSTRENA;
  482. ts_event_en = PTP_TCR_TSEVNTENA;
  483. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  484. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  485. ptp_over_ethernet = PTP_TCR_TSIPENA;
  486. break;
  487. case HWTSTAMP_FILTER_ALL:
  488. /* time stamp any incoming packet */
  489. config.rx_filter = HWTSTAMP_FILTER_ALL;
  490. tstamp_all = PTP_TCR_TSENALL;
  491. break;
  492. default:
  493. return -ERANGE;
  494. }
  495. } else {
  496. switch (config.rx_filter) {
  497. case HWTSTAMP_FILTER_NONE:
  498. config.rx_filter = HWTSTAMP_FILTER_NONE;
  499. break;
  500. default:
  501. /* PTP v1, UDP, any kind of event packet */
  502. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  503. break;
  504. }
  505. }
  506. priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
  507. priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
  508. if (!priv->hwts_tx_en && !priv->hwts_rx_en)
  509. priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, 0);
  510. else {
  511. value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
  512. tstamp_all | ptp_v2 | ptp_over_ethernet |
  513. ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
  514. ts_master_en | snap_type_sel);
  515. priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, value);
  516. /* program Sub Second Increment reg */
  517. sec_inc = priv->hw->ptp->config_sub_second_increment(
  518. priv->ptpaddr, priv->plat->clk_ptp_rate,
  519. priv->plat->has_gmac4);
  520. temp = div_u64(1000000000ULL, sec_inc);
  521. /* calculate default added value:
  522. * formula is :
  523. * addend = (2^32)/freq_div_ratio;
  524. * where, freq_div_ratio = 1e9ns/sec_inc
  525. */
  526. temp = (u64)(temp << 32);
  527. priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
  528. priv->hw->ptp->config_addend(priv->ptpaddr,
  529. priv->default_addend);
  530. /* initialize system time */
  531. ktime_get_real_ts64(&now);
  532. /* lower 32 bits of tv_sec are safe until y2106 */
  533. priv->hw->ptp->init_systime(priv->ptpaddr, (u32)now.tv_sec,
  534. now.tv_nsec);
  535. }
  536. return copy_to_user(ifr->ifr_data, &config,
  537. sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
  538. }
  539. /**
  540. * stmmac_init_ptp - init PTP
  541. * @priv: driver private structure
  542. * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
  543. * This is done by looking at the HW cap. register.
  544. * This function also registers the ptp driver.
  545. */
  546. static int stmmac_init_ptp(struct stmmac_priv *priv)
  547. {
  548. if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
  549. return -EOPNOTSUPP;
  550. priv->adv_ts = 0;
  551. /* Check if adv_ts can be enabled for dwmac 4.x core */
  552. if (priv->plat->has_gmac4 && priv->dma_cap.atime_stamp)
  553. priv->adv_ts = 1;
  554. /* Dwmac 3.x core with extend_desc can support adv_ts */
  555. else if (priv->extend_desc && priv->dma_cap.atime_stamp)
  556. priv->adv_ts = 1;
  557. if (priv->dma_cap.time_stamp)
  558. netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
  559. if (priv->adv_ts)
  560. netdev_info(priv->dev,
  561. "IEEE 1588-2008 Advanced Timestamp supported\n");
  562. priv->hw->ptp = &stmmac_ptp;
  563. priv->hwts_tx_en = 0;
  564. priv->hwts_rx_en = 0;
  565. stmmac_ptp_register(priv);
  566. return 0;
  567. }
  568. static void stmmac_release_ptp(struct stmmac_priv *priv)
  569. {
  570. if (priv->plat->clk_ptp_ref)
  571. clk_disable_unprepare(priv->plat->clk_ptp_ref);
  572. stmmac_ptp_unregister(priv);
  573. }
  574. /**
  575. * stmmac_adjust_link - adjusts the link parameters
  576. * @dev: net device structure
  577. * Description: this is the helper called by the physical abstraction layer
  578. * drivers to communicate the phy link status. According the speed and duplex
  579. * this driver can invoke registered glue-logic as well.
  580. * It also invoke the eee initialization because it could happen when switch
  581. * on different networks (that are eee capable).
  582. */
  583. static void stmmac_adjust_link(struct net_device *dev)
  584. {
  585. struct stmmac_priv *priv = netdev_priv(dev);
  586. struct phy_device *phydev = dev->phydev;
  587. unsigned long flags;
  588. int new_state = 0;
  589. unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
  590. if (!phydev)
  591. return;
  592. spin_lock_irqsave(&priv->lock, flags);
  593. if (phydev->link) {
  594. u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
  595. /* Now we make sure that we can be in full duplex mode.
  596. * If not, we operate in half-duplex mode. */
  597. if (phydev->duplex != priv->oldduplex) {
  598. new_state = 1;
  599. if (!(phydev->duplex))
  600. ctrl &= ~priv->hw->link.duplex;
  601. else
  602. ctrl |= priv->hw->link.duplex;
  603. priv->oldduplex = phydev->duplex;
  604. }
  605. /* Flow Control operation */
  606. if (phydev->pause)
  607. priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
  608. fc, pause_time);
  609. if (phydev->speed != priv->speed) {
  610. new_state = 1;
  611. switch (phydev->speed) {
  612. case 1000:
  613. if (priv->plat->has_gmac ||
  614. priv->plat->has_gmac4)
  615. ctrl &= ~priv->hw->link.port;
  616. break;
  617. case 100:
  618. if (priv->plat->has_gmac ||
  619. priv->plat->has_gmac4) {
  620. ctrl |= priv->hw->link.port;
  621. ctrl |= priv->hw->link.speed;
  622. } else {
  623. ctrl &= ~priv->hw->link.port;
  624. }
  625. break;
  626. case 10:
  627. if (priv->plat->has_gmac ||
  628. priv->plat->has_gmac4) {
  629. ctrl |= priv->hw->link.port;
  630. ctrl &= ~(priv->hw->link.speed);
  631. } else {
  632. ctrl &= ~priv->hw->link.port;
  633. }
  634. break;
  635. default:
  636. netif_warn(priv, link, priv->dev,
  637. "broken speed: %d\n", phydev->speed);
  638. phydev->speed = SPEED_UNKNOWN;
  639. break;
  640. }
  641. if (phydev->speed != SPEED_UNKNOWN)
  642. stmmac_hw_fix_mac_speed(priv);
  643. priv->speed = phydev->speed;
  644. }
  645. writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
  646. if (!priv->oldlink) {
  647. new_state = 1;
  648. priv->oldlink = 1;
  649. }
  650. } else if (priv->oldlink) {
  651. new_state = 1;
  652. priv->oldlink = 0;
  653. priv->speed = SPEED_UNKNOWN;
  654. priv->oldduplex = DUPLEX_UNKNOWN;
  655. }
  656. if (new_state && netif_msg_link(priv))
  657. phy_print_status(phydev);
  658. spin_unlock_irqrestore(&priv->lock, flags);
  659. if (phydev->is_pseudo_fixed_link)
  660. /* Stop PHY layer to call the hook to adjust the link in case
  661. * of a switch is attached to the stmmac driver.
  662. */
  663. phydev->irq = PHY_IGNORE_INTERRUPT;
  664. else
  665. /* At this stage, init the EEE if supported.
  666. * Never called in case of fixed_link.
  667. */
  668. priv->eee_enabled = stmmac_eee_init(priv);
  669. }
  670. /**
  671. * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
  672. * @priv: driver private structure
  673. * Description: this is to verify if the HW supports the PCS.
  674. * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
  675. * configured for the TBI, RTBI, or SGMII PHY interface.
  676. */
  677. static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
  678. {
  679. int interface = priv->plat->interface;
  680. if (priv->dma_cap.pcs) {
  681. if ((interface == PHY_INTERFACE_MODE_RGMII) ||
  682. (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
  683. (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
  684. (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
  685. netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
  686. priv->hw->pcs = STMMAC_PCS_RGMII;
  687. } else if (interface == PHY_INTERFACE_MODE_SGMII) {
  688. netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
  689. priv->hw->pcs = STMMAC_PCS_SGMII;
  690. }
  691. }
  692. }
  693. /**
  694. * stmmac_init_phy - PHY initialization
  695. * @dev: net device structure
  696. * Description: it initializes the driver's PHY state, and attaches the PHY
  697. * to the mac driver.
  698. * Return value:
  699. * 0 on success
  700. */
  701. static int stmmac_init_phy(struct net_device *dev)
  702. {
  703. struct stmmac_priv *priv = netdev_priv(dev);
  704. struct phy_device *phydev;
  705. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  706. char bus_id[MII_BUS_ID_SIZE];
  707. int interface = priv->plat->interface;
  708. int max_speed = priv->plat->max_speed;
  709. priv->oldlink = 0;
  710. priv->speed = SPEED_UNKNOWN;
  711. priv->oldduplex = DUPLEX_UNKNOWN;
  712. if (priv->plat->phy_node) {
  713. phydev = of_phy_connect(dev, priv->plat->phy_node,
  714. &stmmac_adjust_link, 0, interface);
  715. } else {
  716. snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
  717. priv->plat->bus_id);
  718. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  719. priv->plat->phy_addr);
  720. netdev_dbg(priv->dev, "%s: trying to attach to %s\n", __func__,
  721. phy_id_fmt);
  722. phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
  723. interface);
  724. }
  725. if (IS_ERR_OR_NULL(phydev)) {
  726. netdev_err(priv->dev, "Could not attach to PHY\n");
  727. if (!phydev)
  728. return -ENODEV;
  729. return PTR_ERR(phydev);
  730. }
  731. /* Stop Advertising 1000BASE Capability if interface is not GMII */
  732. if ((interface == PHY_INTERFACE_MODE_MII) ||
  733. (interface == PHY_INTERFACE_MODE_RMII) ||
  734. (max_speed < 1000 && max_speed > 0))
  735. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  736. SUPPORTED_1000baseT_Full);
  737. /*
  738. * Broken HW is sometimes missing the pull-up resistor on the
  739. * MDIO line, which results in reads to non-existent devices returning
  740. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  741. * device as well.
  742. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  743. */
  744. if (!priv->plat->phy_node && phydev->phy_id == 0) {
  745. phy_disconnect(phydev);
  746. return -ENODEV;
  747. }
  748. /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
  749. * subsequent PHY polling, make sure we force a link transition if
  750. * we have a UP/DOWN/UP transition
  751. */
  752. if (phydev->is_pseudo_fixed_link)
  753. phydev->irq = PHY_POLL;
  754. phy_attached_info(phydev);
  755. return 0;
  756. }
  757. static void stmmac_display_rings(struct stmmac_priv *priv)
  758. {
  759. void *head_rx, *head_tx;
  760. if (priv->extend_desc) {
  761. head_rx = (void *)priv->dma_erx;
  762. head_tx = (void *)priv->dma_etx;
  763. } else {
  764. head_rx = (void *)priv->dma_rx;
  765. head_tx = (void *)priv->dma_tx;
  766. }
  767. /* Display Rx ring */
  768. priv->hw->desc->display_ring(head_rx, DMA_RX_SIZE, true);
  769. /* Display Tx ring */
  770. priv->hw->desc->display_ring(head_tx, DMA_TX_SIZE, false);
  771. }
  772. static int stmmac_set_bfsize(int mtu, int bufsize)
  773. {
  774. int ret = bufsize;
  775. if (mtu >= BUF_SIZE_4KiB)
  776. ret = BUF_SIZE_8KiB;
  777. else if (mtu >= BUF_SIZE_2KiB)
  778. ret = BUF_SIZE_4KiB;
  779. else if (mtu > DEFAULT_BUFSIZE)
  780. ret = BUF_SIZE_2KiB;
  781. else
  782. ret = DEFAULT_BUFSIZE;
  783. return ret;
  784. }
  785. /**
  786. * stmmac_clear_descriptors - clear descriptors
  787. * @priv: driver private structure
  788. * Description: this function is called to clear the tx and rx descriptors
  789. * in case of both basic and extended descriptors are used.
  790. */
  791. static void stmmac_clear_descriptors(struct stmmac_priv *priv)
  792. {
  793. int i;
  794. /* Clear the Rx/Tx descriptors */
  795. for (i = 0; i < DMA_RX_SIZE; i++)
  796. if (priv->extend_desc)
  797. priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
  798. priv->use_riwt, priv->mode,
  799. (i == DMA_RX_SIZE - 1));
  800. else
  801. priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
  802. priv->use_riwt, priv->mode,
  803. (i == DMA_RX_SIZE - 1));
  804. for (i = 0; i < DMA_TX_SIZE; i++)
  805. if (priv->extend_desc)
  806. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  807. priv->mode,
  808. (i == DMA_TX_SIZE - 1));
  809. else
  810. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  811. priv->mode,
  812. (i == DMA_TX_SIZE - 1));
  813. }
  814. /**
  815. * stmmac_init_rx_buffers - init the RX descriptor buffer.
  816. * @priv: driver private structure
  817. * @p: descriptor pointer
  818. * @i: descriptor index
  819. * @flags: gfp flag.
  820. * Description: this function is called to allocate a receive buffer, perform
  821. * the DMA mapping and init the descriptor.
  822. */
  823. static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
  824. int i, gfp_t flags)
  825. {
  826. struct sk_buff *skb;
  827. skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
  828. if (!skb) {
  829. netdev_err(priv->dev,
  830. "%s: Rx init fails; skb is NULL\n", __func__);
  831. return -ENOMEM;
  832. }
  833. priv->rx_skbuff[i] = skb;
  834. priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  835. priv->dma_buf_sz,
  836. DMA_FROM_DEVICE);
  837. if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
  838. netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
  839. dev_kfree_skb_any(skb);
  840. return -EINVAL;
  841. }
  842. if (priv->synopsys_id >= DWMAC_CORE_4_00)
  843. p->des0 = cpu_to_le32(priv->rx_skbuff_dma[i]);
  844. else
  845. p->des2 = cpu_to_le32(priv->rx_skbuff_dma[i]);
  846. if ((priv->hw->mode->init_desc3) &&
  847. (priv->dma_buf_sz == BUF_SIZE_16KiB))
  848. priv->hw->mode->init_desc3(p);
  849. return 0;
  850. }
  851. static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
  852. {
  853. if (priv->rx_skbuff[i]) {
  854. dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
  855. priv->dma_buf_sz, DMA_FROM_DEVICE);
  856. dev_kfree_skb_any(priv->rx_skbuff[i]);
  857. }
  858. priv->rx_skbuff[i] = NULL;
  859. }
  860. /**
  861. * init_dma_desc_rings - init the RX/TX descriptor rings
  862. * @dev: net device structure
  863. * @flags: gfp flag.
  864. * Description: this function initializes the DMA RX/TX descriptors
  865. * and allocates the socket buffers. It supports the chained and ring
  866. * modes.
  867. */
  868. static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
  869. {
  870. int i;
  871. struct stmmac_priv *priv = netdev_priv(dev);
  872. unsigned int bfsize = 0;
  873. int ret = -ENOMEM;
  874. if (priv->hw->mode->set_16kib_bfsize)
  875. bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
  876. if (bfsize < BUF_SIZE_16KiB)
  877. bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
  878. priv->dma_buf_sz = bfsize;
  879. netif_dbg(priv, probe, priv->dev,
  880. "(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n",
  881. __func__, (u32)priv->dma_rx_phy, (u32)priv->dma_tx_phy);
  882. /* RX INITIALIZATION */
  883. netif_dbg(priv, probe, priv->dev,
  884. "SKB addresses:\nskb\t\tskb data\tdma data\n");
  885. for (i = 0; i < DMA_RX_SIZE; i++) {
  886. struct dma_desc *p;
  887. if (priv->extend_desc)
  888. p = &((priv->dma_erx + i)->basic);
  889. else
  890. p = priv->dma_rx + i;
  891. ret = stmmac_init_rx_buffers(priv, p, i, flags);
  892. if (ret)
  893. goto err_init_rx_buffers;
  894. netif_dbg(priv, probe, priv->dev, "[%p]\t[%p]\t[%x]\n",
  895. priv->rx_skbuff[i], priv->rx_skbuff[i]->data,
  896. (unsigned int)priv->rx_skbuff_dma[i]);
  897. }
  898. priv->cur_rx = 0;
  899. priv->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
  900. buf_sz = bfsize;
  901. /* Setup the chained descriptor addresses */
  902. if (priv->mode == STMMAC_CHAIN_MODE) {
  903. if (priv->extend_desc) {
  904. priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
  905. DMA_RX_SIZE, 1);
  906. priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
  907. DMA_TX_SIZE, 1);
  908. } else {
  909. priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
  910. DMA_RX_SIZE, 0);
  911. priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
  912. DMA_TX_SIZE, 0);
  913. }
  914. }
  915. /* TX INITIALIZATION */
  916. for (i = 0; i < DMA_TX_SIZE; i++) {
  917. struct dma_desc *p;
  918. if (priv->extend_desc)
  919. p = &((priv->dma_etx + i)->basic);
  920. else
  921. p = priv->dma_tx + i;
  922. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  923. p->des0 = 0;
  924. p->des1 = 0;
  925. p->des2 = 0;
  926. p->des3 = 0;
  927. } else {
  928. p->des2 = 0;
  929. }
  930. priv->tx_skbuff_dma[i].buf = 0;
  931. priv->tx_skbuff_dma[i].map_as_page = false;
  932. priv->tx_skbuff_dma[i].len = 0;
  933. priv->tx_skbuff_dma[i].last_segment = false;
  934. priv->tx_skbuff[i] = NULL;
  935. }
  936. priv->dirty_tx = 0;
  937. priv->cur_tx = 0;
  938. netdev_reset_queue(priv->dev);
  939. stmmac_clear_descriptors(priv);
  940. if (netif_msg_hw(priv))
  941. stmmac_display_rings(priv);
  942. return 0;
  943. err_init_rx_buffers:
  944. while (--i >= 0)
  945. stmmac_free_rx_buffers(priv, i);
  946. return ret;
  947. }
  948. static void dma_free_rx_skbufs(struct stmmac_priv *priv)
  949. {
  950. int i;
  951. for (i = 0; i < DMA_RX_SIZE; i++)
  952. stmmac_free_rx_buffers(priv, i);
  953. }
  954. static void dma_free_tx_skbufs(struct stmmac_priv *priv)
  955. {
  956. int i;
  957. for (i = 0; i < DMA_TX_SIZE; i++) {
  958. if (priv->tx_skbuff_dma[i].buf) {
  959. if (priv->tx_skbuff_dma[i].map_as_page)
  960. dma_unmap_page(priv->device,
  961. priv->tx_skbuff_dma[i].buf,
  962. priv->tx_skbuff_dma[i].len,
  963. DMA_TO_DEVICE);
  964. else
  965. dma_unmap_single(priv->device,
  966. priv->tx_skbuff_dma[i].buf,
  967. priv->tx_skbuff_dma[i].len,
  968. DMA_TO_DEVICE);
  969. }
  970. if (priv->tx_skbuff[i]) {
  971. dev_kfree_skb_any(priv->tx_skbuff[i]);
  972. priv->tx_skbuff[i] = NULL;
  973. priv->tx_skbuff_dma[i].buf = 0;
  974. priv->tx_skbuff_dma[i].map_as_page = false;
  975. }
  976. }
  977. }
  978. /**
  979. * alloc_dma_desc_resources - alloc TX/RX resources.
  980. * @priv: private structure
  981. * Description: according to which descriptor can be used (extend or basic)
  982. * this function allocates the resources for TX and RX paths. In case of
  983. * reception, for example, it pre-allocated the RX socket buffer in order to
  984. * allow zero-copy mechanism.
  985. */
  986. static int alloc_dma_desc_resources(struct stmmac_priv *priv)
  987. {
  988. int ret = -ENOMEM;
  989. priv->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE, sizeof(dma_addr_t),
  990. GFP_KERNEL);
  991. if (!priv->rx_skbuff_dma)
  992. return -ENOMEM;
  993. priv->rx_skbuff = kmalloc_array(DMA_RX_SIZE, sizeof(struct sk_buff *),
  994. GFP_KERNEL);
  995. if (!priv->rx_skbuff)
  996. goto err_rx_skbuff;
  997. priv->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
  998. sizeof(*priv->tx_skbuff_dma),
  999. GFP_KERNEL);
  1000. if (!priv->tx_skbuff_dma)
  1001. goto err_tx_skbuff_dma;
  1002. priv->tx_skbuff = kmalloc_array(DMA_TX_SIZE, sizeof(struct sk_buff *),
  1003. GFP_KERNEL);
  1004. if (!priv->tx_skbuff)
  1005. goto err_tx_skbuff;
  1006. if (priv->extend_desc) {
  1007. priv->dma_erx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
  1008. sizeof(struct
  1009. dma_extended_desc),
  1010. &priv->dma_rx_phy,
  1011. GFP_KERNEL);
  1012. if (!priv->dma_erx)
  1013. goto err_dma;
  1014. priv->dma_etx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
  1015. sizeof(struct
  1016. dma_extended_desc),
  1017. &priv->dma_tx_phy,
  1018. GFP_KERNEL);
  1019. if (!priv->dma_etx) {
  1020. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1021. sizeof(struct dma_extended_desc),
  1022. priv->dma_erx, priv->dma_rx_phy);
  1023. goto err_dma;
  1024. }
  1025. } else {
  1026. priv->dma_rx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
  1027. sizeof(struct dma_desc),
  1028. &priv->dma_rx_phy,
  1029. GFP_KERNEL);
  1030. if (!priv->dma_rx)
  1031. goto err_dma;
  1032. priv->dma_tx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
  1033. sizeof(struct dma_desc),
  1034. &priv->dma_tx_phy,
  1035. GFP_KERNEL);
  1036. if (!priv->dma_tx) {
  1037. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1038. sizeof(struct dma_desc),
  1039. priv->dma_rx, priv->dma_rx_phy);
  1040. goto err_dma;
  1041. }
  1042. }
  1043. return 0;
  1044. err_dma:
  1045. kfree(priv->tx_skbuff);
  1046. err_tx_skbuff:
  1047. kfree(priv->tx_skbuff_dma);
  1048. err_tx_skbuff_dma:
  1049. kfree(priv->rx_skbuff);
  1050. err_rx_skbuff:
  1051. kfree(priv->rx_skbuff_dma);
  1052. return ret;
  1053. }
  1054. static void free_dma_desc_resources(struct stmmac_priv *priv)
  1055. {
  1056. /* Release the DMA TX/RX socket buffers */
  1057. dma_free_rx_skbufs(priv);
  1058. dma_free_tx_skbufs(priv);
  1059. /* Free DMA regions of consistent memory previously allocated */
  1060. if (!priv->extend_desc) {
  1061. dma_free_coherent(priv->device,
  1062. DMA_TX_SIZE * sizeof(struct dma_desc),
  1063. priv->dma_tx, priv->dma_tx_phy);
  1064. dma_free_coherent(priv->device,
  1065. DMA_RX_SIZE * sizeof(struct dma_desc),
  1066. priv->dma_rx, priv->dma_rx_phy);
  1067. } else {
  1068. dma_free_coherent(priv->device, DMA_TX_SIZE *
  1069. sizeof(struct dma_extended_desc),
  1070. priv->dma_etx, priv->dma_tx_phy);
  1071. dma_free_coherent(priv->device, DMA_RX_SIZE *
  1072. sizeof(struct dma_extended_desc),
  1073. priv->dma_erx, priv->dma_rx_phy);
  1074. }
  1075. kfree(priv->rx_skbuff_dma);
  1076. kfree(priv->rx_skbuff);
  1077. kfree(priv->tx_skbuff_dma);
  1078. kfree(priv->tx_skbuff);
  1079. }
  1080. /**
  1081. * stmmac_mac_enable_rx_queues - Enable MAC rx queues
  1082. * @priv: driver private structure
  1083. * Description: It is used for enabling the rx queues in the MAC
  1084. */
  1085. static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
  1086. {
  1087. int rx_count = priv->dma_cap.number_rx_queues;
  1088. int queue = 0;
  1089. /* If GMAC does not have multiple queues, then this is not necessary*/
  1090. if (rx_count == 1)
  1091. return;
  1092. /**
  1093. * If the core is synthesized with multiple rx queues / multiple
  1094. * dma channels, then rx queues will be disabled by default.
  1095. * For now only rx queue 0 is enabled.
  1096. */
  1097. priv->hw->mac->rx_queue_enable(priv->hw, queue);
  1098. }
  1099. /**
  1100. * stmmac_dma_operation_mode - HW DMA operation mode
  1101. * @priv: driver private structure
  1102. * Description: it is used for configuring the DMA operation mode register in
  1103. * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
  1104. */
  1105. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  1106. {
  1107. int rxfifosz = priv->plat->rx_fifo_size;
  1108. if (priv->plat->force_thresh_dma_mode)
  1109. priv->hw->dma->dma_mode(priv->ioaddr, tc, tc, rxfifosz);
  1110. else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
  1111. /*
  1112. * In case of GMAC, SF mode can be enabled
  1113. * to perform the TX COE in HW. This depends on:
  1114. * 1) TX COE if actually supported
  1115. * 2) There is no bugged Jumbo frame support
  1116. * that needs to not insert csum in the TDES.
  1117. */
  1118. priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE,
  1119. rxfifosz);
  1120. priv->xstats.threshold = SF_DMA_MODE;
  1121. } else
  1122. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE,
  1123. rxfifosz);
  1124. }
  1125. /**
  1126. * stmmac_tx_clean - to manage the transmission completion
  1127. * @priv: driver private structure
  1128. * Description: it reclaims the transmit resources after transmission completes.
  1129. */
  1130. static void stmmac_tx_clean(struct stmmac_priv *priv)
  1131. {
  1132. unsigned int bytes_compl = 0, pkts_compl = 0;
  1133. unsigned int entry = priv->dirty_tx;
  1134. netif_tx_lock(priv->dev);
  1135. priv->xstats.tx_clean++;
  1136. while (entry != priv->cur_tx) {
  1137. struct sk_buff *skb = priv->tx_skbuff[entry];
  1138. struct dma_desc *p;
  1139. int status;
  1140. if (priv->extend_desc)
  1141. p = (struct dma_desc *)(priv->dma_etx + entry);
  1142. else
  1143. p = priv->dma_tx + entry;
  1144. status = priv->hw->desc->tx_status(&priv->dev->stats,
  1145. &priv->xstats, p,
  1146. priv->ioaddr);
  1147. /* Check if the descriptor is owned by the DMA */
  1148. if (unlikely(status & tx_dma_own))
  1149. break;
  1150. /* Just consider the last segment and ...*/
  1151. if (likely(!(status & tx_not_ls))) {
  1152. /* ... verify the status error condition */
  1153. if (unlikely(status & tx_err)) {
  1154. priv->dev->stats.tx_errors++;
  1155. } else {
  1156. priv->dev->stats.tx_packets++;
  1157. priv->xstats.tx_pkt_n++;
  1158. }
  1159. stmmac_get_tx_hwtstamp(priv, p, skb);
  1160. }
  1161. if (likely(priv->tx_skbuff_dma[entry].buf)) {
  1162. if (priv->tx_skbuff_dma[entry].map_as_page)
  1163. dma_unmap_page(priv->device,
  1164. priv->tx_skbuff_dma[entry].buf,
  1165. priv->tx_skbuff_dma[entry].len,
  1166. DMA_TO_DEVICE);
  1167. else
  1168. dma_unmap_single(priv->device,
  1169. priv->tx_skbuff_dma[entry].buf,
  1170. priv->tx_skbuff_dma[entry].len,
  1171. DMA_TO_DEVICE);
  1172. priv->tx_skbuff_dma[entry].buf = 0;
  1173. priv->tx_skbuff_dma[entry].len = 0;
  1174. priv->tx_skbuff_dma[entry].map_as_page = false;
  1175. }
  1176. if (priv->hw->mode->clean_desc3)
  1177. priv->hw->mode->clean_desc3(priv, p);
  1178. priv->tx_skbuff_dma[entry].last_segment = false;
  1179. priv->tx_skbuff_dma[entry].is_jumbo = false;
  1180. if (likely(skb != NULL)) {
  1181. pkts_compl++;
  1182. bytes_compl += skb->len;
  1183. dev_consume_skb_any(skb);
  1184. priv->tx_skbuff[entry] = NULL;
  1185. }
  1186. priv->hw->desc->release_tx_desc(p, priv->mode);
  1187. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1188. }
  1189. priv->dirty_tx = entry;
  1190. netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
  1191. if (unlikely(netif_queue_stopped(priv->dev) &&
  1192. stmmac_tx_avail(priv) > STMMAC_TX_THRESH)) {
  1193. netif_dbg(priv, tx_done, priv->dev,
  1194. "%s: restart transmit\n", __func__);
  1195. netif_wake_queue(priv->dev);
  1196. }
  1197. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  1198. stmmac_enable_eee_mode(priv);
  1199. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  1200. }
  1201. netif_tx_unlock(priv->dev);
  1202. }
  1203. static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
  1204. {
  1205. priv->hw->dma->enable_dma_irq(priv->ioaddr);
  1206. }
  1207. static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
  1208. {
  1209. priv->hw->dma->disable_dma_irq(priv->ioaddr);
  1210. }
  1211. /**
  1212. * stmmac_tx_err - to manage the tx error
  1213. * @priv: driver private structure
  1214. * Description: it cleans the descriptors and restarts the transmission
  1215. * in case of transmission errors.
  1216. */
  1217. static void stmmac_tx_err(struct stmmac_priv *priv)
  1218. {
  1219. int i;
  1220. netif_stop_queue(priv->dev);
  1221. priv->hw->dma->stop_tx(priv->ioaddr);
  1222. dma_free_tx_skbufs(priv);
  1223. for (i = 0; i < DMA_TX_SIZE; i++)
  1224. if (priv->extend_desc)
  1225. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  1226. priv->mode,
  1227. (i == DMA_TX_SIZE - 1));
  1228. else
  1229. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  1230. priv->mode,
  1231. (i == DMA_TX_SIZE - 1));
  1232. priv->dirty_tx = 0;
  1233. priv->cur_tx = 0;
  1234. netdev_reset_queue(priv->dev);
  1235. priv->hw->dma->start_tx(priv->ioaddr);
  1236. priv->dev->stats.tx_errors++;
  1237. netif_wake_queue(priv->dev);
  1238. }
  1239. /**
  1240. * stmmac_dma_interrupt - DMA ISR
  1241. * @priv: driver private structure
  1242. * Description: this is the DMA ISR. It is called by the main ISR.
  1243. * It calls the dwmac dma routine and schedule poll method in case of some
  1244. * work can be done.
  1245. */
  1246. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  1247. {
  1248. int status;
  1249. int rxfifosz = priv->plat->rx_fifo_size;
  1250. status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
  1251. if (likely((status & handle_rx)) || (status & handle_tx)) {
  1252. if (likely(napi_schedule_prep(&priv->napi))) {
  1253. stmmac_disable_dma_irq(priv);
  1254. __napi_schedule(&priv->napi);
  1255. }
  1256. }
  1257. if (unlikely(status & tx_hard_error_bump_tc)) {
  1258. /* Try to bump up the dma threshold on this failure */
  1259. if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
  1260. (tc <= 256)) {
  1261. tc += 64;
  1262. if (priv->plat->force_thresh_dma_mode)
  1263. priv->hw->dma->dma_mode(priv->ioaddr, tc, tc,
  1264. rxfifosz);
  1265. else
  1266. priv->hw->dma->dma_mode(priv->ioaddr, tc,
  1267. SF_DMA_MODE, rxfifosz);
  1268. priv->xstats.threshold = tc;
  1269. }
  1270. } else if (unlikely(status == tx_hard_error))
  1271. stmmac_tx_err(priv);
  1272. }
  1273. /**
  1274. * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
  1275. * @priv: driver private structure
  1276. * Description: this masks the MMC irq, in fact, the counters are managed in SW.
  1277. */
  1278. static void stmmac_mmc_setup(struct stmmac_priv *priv)
  1279. {
  1280. unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
  1281. MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
  1282. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  1283. priv->ptpaddr = priv->ioaddr + PTP_GMAC4_OFFSET;
  1284. priv->mmcaddr = priv->ioaddr + MMC_GMAC4_OFFSET;
  1285. } else {
  1286. priv->ptpaddr = priv->ioaddr + PTP_GMAC3_X_OFFSET;
  1287. priv->mmcaddr = priv->ioaddr + MMC_GMAC3_X_OFFSET;
  1288. }
  1289. dwmac_mmc_intr_all_mask(priv->mmcaddr);
  1290. if (priv->dma_cap.rmon) {
  1291. dwmac_mmc_ctrl(priv->mmcaddr, mode);
  1292. memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
  1293. } else
  1294. netdev_info(priv->dev, "No MAC Management Counters available\n");
  1295. }
  1296. /**
  1297. * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
  1298. * @priv: driver private structure
  1299. * Description: select the Enhanced/Alternate or Normal descriptors.
  1300. * In case of Enhanced/Alternate, it checks if the extended descriptors are
  1301. * supported by the HW capability register.
  1302. */
  1303. static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
  1304. {
  1305. if (priv->plat->enh_desc) {
  1306. dev_info(priv->device, "Enhanced/Alternate descriptors\n");
  1307. /* GMAC older than 3.50 has no extended descriptors */
  1308. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  1309. dev_info(priv->device, "Enabled extended descriptors\n");
  1310. priv->extend_desc = 1;
  1311. } else
  1312. dev_warn(priv->device, "Extended descriptors not supported\n");
  1313. priv->hw->desc = &enh_desc_ops;
  1314. } else {
  1315. dev_info(priv->device, "Normal descriptors\n");
  1316. priv->hw->desc = &ndesc_ops;
  1317. }
  1318. }
  1319. /**
  1320. * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
  1321. * @priv: driver private structure
  1322. * Description:
  1323. * new GMAC chip generations have a new register to indicate the
  1324. * presence of the optional feature/functions.
  1325. * This can be also used to override the value passed through the
  1326. * platform and necessary for old MAC10/100 and GMAC chips.
  1327. */
  1328. static int stmmac_get_hw_features(struct stmmac_priv *priv)
  1329. {
  1330. u32 ret = 0;
  1331. if (priv->hw->dma->get_hw_feature) {
  1332. priv->hw->dma->get_hw_feature(priv->ioaddr,
  1333. &priv->dma_cap);
  1334. ret = 1;
  1335. }
  1336. return ret;
  1337. }
  1338. /**
  1339. * stmmac_check_ether_addr - check if the MAC addr is valid
  1340. * @priv: driver private structure
  1341. * Description:
  1342. * it is to verify if the MAC address is valid, in case of failures it
  1343. * generates a random MAC address
  1344. */
  1345. static void stmmac_check_ether_addr(struct stmmac_priv *priv)
  1346. {
  1347. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  1348. priv->hw->mac->get_umac_addr(priv->hw,
  1349. priv->dev->dev_addr, 0);
  1350. if (!is_valid_ether_addr(priv->dev->dev_addr))
  1351. eth_hw_addr_random(priv->dev);
  1352. netdev_info(priv->dev, "device MAC address %pM\n",
  1353. priv->dev->dev_addr);
  1354. }
  1355. }
  1356. /**
  1357. * stmmac_init_dma_engine - DMA init.
  1358. * @priv: driver private structure
  1359. * Description:
  1360. * It inits the DMA invoking the specific MAC/GMAC callback.
  1361. * Some DMA parameters can be passed from the platform;
  1362. * in case of these are not passed a default is kept for the MAC or GMAC.
  1363. */
  1364. static int stmmac_init_dma_engine(struct stmmac_priv *priv)
  1365. {
  1366. int atds = 0;
  1367. int ret = 0;
  1368. if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
  1369. dev_err(priv->device, "Invalid DMA configuration\n");
  1370. return -EINVAL;
  1371. }
  1372. if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
  1373. atds = 1;
  1374. ret = priv->hw->dma->reset(priv->ioaddr);
  1375. if (ret) {
  1376. dev_err(priv->device, "Failed to reset the dma\n");
  1377. return ret;
  1378. }
  1379. priv->hw->dma->init(priv->ioaddr, priv->plat->dma_cfg,
  1380. priv->dma_tx_phy, priv->dma_rx_phy, atds);
  1381. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  1382. priv->rx_tail_addr = priv->dma_rx_phy +
  1383. (DMA_RX_SIZE * sizeof(struct dma_desc));
  1384. priv->hw->dma->set_rx_tail_ptr(priv->ioaddr, priv->rx_tail_addr,
  1385. STMMAC_CHAN0);
  1386. priv->tx_tail_addr = priv->dma_tx_phy +
  1387. (DMA_TX_SIZE * sizeof(struct dma_desc));
  1388. priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
  1389. STMMAC_CHAN0);
  1390. }
  1391. if (priv->plat->axi && priv->hw->dma->axi)
  1392. priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
  1393. return ret;
  1394. }
  1395. /**
  1396. * stmmac_tx_timer - mitigation sw timer for tx.
  1397. * @data: data pointer
  1398. * Description:
  1399. * This is the timer handler to directly invoke the stmmac_tx_clean.
  1400. */
  1401. static void stmmac_tx_timer(unsigned long data)
  1402. {
  1403. struct stmmac_priv *priv = (struct stmmac_priv *)data;
  1404. stmmac_tx_clean(priv);
  1405. }
  1406. /**
  1407. * stmmac_init_tx_coalesce - init tx mitigation options.
  1408. * @priv: driver private structure
  1409. * Description:
  1410. * This inits the transmit coalesce parameters: i.e. timer rate,
  1411. * timer handler and default threshold used for enabling the
  1412. * interrupt on completion bit.
  1413. */
  1414. static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
  1415. {
  1416. priv->tx_coal_frames = STMMAC_TX_FRAMES;
  1417. priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
  1418. init_timer(&priv->txtimer);
  1419. priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
  1420. priv->txtimer.data = (unsigned long)priv;
  1421. priv->txtimer.function = stmmac_tx_timer;
  1422. add_timer(&priv->txtimer);
  1423. }
  1424. /**
  1425. * stmmac_hw_setup - setup mac in a usable state.
  1426. * @dev : pointer to the device structure.
  1427. * Description:
  1428. * this is the main function to setup the HW in a usable state because the
  1429. * dma engine is reset, the core registers are configured (e.g. AXI,
  1430. * Checksum features, timers). The DMA is ready to start receiving and
  1431. * transmitting.
  1432. * Return value:
  1433. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1434. * file on failure.
  1435. */
  1436. static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
  1437. {
  1438. struct stmmac_priv *priv = netdev_priv(dev);
  1439. int ret;
  1440. /* DMA initialization and SW reset */
  1441. ret = stmmac_init_dma_engine(priv);
  1442. if (ret < 0) {
  1443. netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
  1444. __func__);
  1445. return ret;
  1446. }
  1447. /* Copy the MAC addr into the HW */
  1448. priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
  1449. /* PS and related bits will be programmed according to the speed */
  1450. if (priv->hw->pcs) {
  1451. int speed = priv->plat->mac_port_sel_speed;
  1452. if ((speed == SPEED_10) || (speed == SPEED_100) ||
  1453. (speed == SPEED_1000)) {
  1454. priv->hw->ps = speed;
  1455. } else {
  1456. dev_warn(priv->device, "invalid port speed\n");
  1457. priv->hw->ps = 0;
  1458. }
  1459. }
  1460. /* Initialize the MAC Core */
  1461. priv->hw->mac->core_init(priv->hw, dev->mtu);
  1462. /* Initialize MAC RX Queues */
  1463. if (priv->hw->mac->rx_queue_enable)
  1464. stmmac_mac_enable_rx_queues(priv);
  1465. ret = priv->hw->mac->rx_ipc(priv->hw);
  1466. if (!ret) {
  1467. netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
  1468. priv->plat->rx_coe = STMMAC_RX_COE_NONE;
  1469. priv->hw->rx_csum = 0;
  1470. }
  1471. /* Enable the MAC Rx/Tx */
  1472. if (priv->synopsys_id >= DWMAC_CORE_4_00)
  1473. stmmac_dwmac4_set_mac(priv->ioaddr, true);
  1474. else
  1475. stmmac_set_mac(priv->ioaddr, true);
  1476. /* Set the HW DMA mode and the COE */
  1477. stmmac_dma_operation_mode(priv);
  1478. stmmac_mmc_setup(priv);
  1479. if (init_ptp) {
  1480. ret = stmmac_init_ptp(priv);
  1481. if (ret == -EOPNOTSUPP)
  1482. netdev_warn(priv->dev, "PTP not supported by HW\n");
  1483. else if (ret)
  1484. netdev_warn(priv->dev, "PTP init failed\n");
  1485. }
  1486. #ifdef CONFIG_DEBUG_FS
  1487. ret = stmmac_init_fs(dev);
  1488. if (ret < 0)
  1489. netdev_warn(priv->dev, "%s: failed debugFS registration\n",
  1490. __func__);
  1491. #endif
  1492. /* Start the ball rolling... */
  1493. netdev_dbg(priv->dev, "DMA RX/TX processes started...\n");
  1494. priv->hw->dma->start_tx(priv->ioaddr);
  1495. priv->hw->dma->start_rx(priv->ioaddr);
  1496. priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
  1497. if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
  1498. priv->rx_riwt = MAX_DMA_RIWT;
  1499. priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
  1500. }
  1501. if (priv->hw->pcs && priv->hw->mac->pcs_ctrl_ane)
  1502. priv->hw->mac->pcs_ctrl_ane(priv->hw, 1, priv->hw->ps, 0);
  1503. /* set TX ring length */
  1504. if (priv->hw->dma->set_tx_ring_len)
  1505. priv->hw->dma->set_tx_ring_len(priv->ioaddr,
  1506. (DMA_TX_SIZE - 1));
  1507. /* set RX ring length */
  1508. if (priv->hw->dma->set_rx_ring_len)
  1509. priv->hw->dma->set_rx_ring_len(priv->ioaddr,
  1510. (DMA_RX_SIZE - 1));
  1511. /* Enable TSO */
  1512. if (priv->tso)
  1513. priv->hw->dma->enable_tso(priv->ioaddr, 1, STMMAC_CHAN0);
  1514. return 0;
  1515. }
  1516. /**
  1517. * stmmac_open - open entry point of the driver
  1518. * @dev : pointer to the device structure.
  1519. * Description:
  1520. * This function is the open entry point of the driver.
  1521. * Return value:
  1522. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1523. * file on failure.
  1524. */
  1525. static int stmmac_open(struct net_device *dev)
  1526. {
  1527. struct stmmac_priv *priv = netdev_priv(dev);
  1528. int ret;
  1529. stmmac_check_ether_addr(priv);
  1530. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  1531. priv->hw->pcs != STMMAC_PCS_TBI &&
  1532. priv->hw->pcs != STMMAC_PCS_RTBI) {
  1533. ret = stmmac_init_phy(dev);
  1534. if (ret) {
  1535. netdev_err(priv->dev,
  1536. "%s: Cannot attach to PHY (error: %d)\n",
  1537. __func__, ret);
  1538. return ret;
  1539. }
  1540. }
  1541. /* Extra statistics */
  1542. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  1543. priv->xstats.threshold = tc;
  1544. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  1545. priv->rx_copybreak = STMMAC_RX_COPYBREAK;
  1546. ret = alloc_dma_desc_resources(priv);
  1547. if (ret < 0) {
  1548. netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
  1549. __func__);
  1550. goto dma_desc_error;
  1551. }
  1552. ret = init_dma_desc_rings(dev, GFP_KERNEL);
  1553. if (ret < 0) {
  1554. netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
  1555. __func__);
  1556. goto init_error;
  1557. }
  1558. ret = stmmac_hw_setup(dev, true);
  1559. if (ret < 0) {
  1560. netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
  1561. goto init_error;
  1562. }
  1563. stmmac_init_tx_coalesce(priv);
  1564. if (dev->phydev)
  1565. phy_start(dev->phydev);
  1566. /* Request the IRQ lines */
  1567. ret = request_irq(dev->irq, stmmac_interrupt,
  1568. IRQF_SHARED, dev->name, dev);
  1569. if (unlikely(ret < 0)) {
  1570. netdev_err(priv->dev,
  1571. "%s: ERROR: allocating the IRQ %d (error: %d)\n",
  1572. __func__, dev->irq, ret);
  1573. goto init_error;
  1574. }
  1575. /* Request the Wake IRQ in case of another line is used for WoL */
  1576. if (priv->wol_irq != dev->irq) {
  1577. ret = request_irq(priv->wol_irq, stmmac_interrupt,
  1578. IRQF_SHARED, dev->name, dev);
  1579. if (unlikely(ret < 0)) {
  1580. netdev_err(priv->dev,
  1581. "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
  1582. __func__, priv->wol_irq, ret);
  1583. goto wolirq_error;
  1584. }
  1585. }
  1586. /* Request the IRQ lines */
  1587. if (priv->lpi_irq > 0) {
  1588. ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
  1589. dev->name, dev);
  1590. if (unlikely(ret < 0)) {
  1591. netdev_err(priv->dev,
  1592. "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  1593. __func__, priv->lpi_irq, ret);
  1594. goto lpiirq_error;
  1595. }
  1596. }
  1597. napi_enable(&priv->napi);
  1598. netif_start_queue(dev);
  1599. return 0;
  1600. lpiirq_error:
  1601. if (priv->wol_irq != dev->irq)
  1602. free_irq(priv->wol_irq, dev);
  1603. wolirq_error:
  1604. free_irq(dev->irq, dev);
  1605. init_error:
  1606. free_dma_desc_resources(priv);
  1607. dma_desc_error:
  1608. if (dev->phydev)
  1609. phy_disconnect(dev->phydev);
  1610. return ret;
  1611. }
  1612. /**
  1613. * stmmac_release - close entry point of the driver
  1614. * @dev : device pointer.
  1615. * Description:
  1616. * This is the stop entry point of the driver.
  1617. */
  1618. static int stmmac_release(struct net_device *dev)
  1619. {
  1620. struct stmmac_priv *priv = netdev_priv(dev);
  1621. if (priv->eee_enabled)
  1622. del_timer_sync(&priv->eee_ctrl_timer);
  1623. /* Stop and disconnect the PHY */
  1624. if (dev->phydev) {
  1625. phy_stop(dev->phydev);
  1626. phy_disconnect(dev->phydev);
  1627. }
  1628. netif_stop_queue(dev);
  1629. napi_disable(&priv->napi);
  1630. del_timer_sync(&priv->txtimer);
  1631. /* Free the IRQ lines */
  1632. free_irq(dev->irq, dev);
  1633. if (priv->wol_irq != dev->irq)
  1634. free_irq(priv->wol_irq, dev);
  1635. if (priv->lpi_irq > 0)
  1636. free_irq(priv->lpi_irq, dev);
  1637. /* Stop TX/RX DMA and clear the descriptors */
  1638. priv->hw->dma->stop_tx(priv->ioaddr);
  1639. priv->hw->dma->stop_rx(priv->ioaddr);
  1640. /* Release and free the Rx/Tx resources */
  1641. free_dma_desc_resources(priv);
  1642. /* Disable the MAC Rx/Tx */
  1643. stmmac_set_mac(priv->ioaddr, false);
  1644. netif_carrier_off(dev);
  1645. #ifdef CONFIG_DEBUG_FS
  1646. stmmac_exit_fs(dev);
  1647. #endif
  1648. stmmac_release_ptp(priv);
  1649. return 0;
  1650. }
  1651. /**
  1652. * stmmac_tso_allocator - close entry point of the driver
  1653. * @priv: driver private structure
  1654. * @des: buffer start address
  1655. * @total_len: total length to fill in descriptors
  1656. * @last_segmant: condition for the last descriptor
  1657. * Description:
  1658. * This function fills descriptor and request new descriptors according to
  1659. * buffer length to fill
  1660. */
  1661. static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
  1662. int total_len, bool last_segment)
  1663. {
  1664. struct dma_desc *desc;
  1665. int tmp_len;
  1666. u32 buff_size;
  1667. tmp_len = total_len;
  1668. while (tmp_len > 0) {
  1669. priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
  1670. desc = priv->dma_tx + priv->cur_tx;
  1671. desc->des0 = cpu_to_le32(des + (total_len - tmp_len));
  1672. buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
  1673. TSO_MAX_BUFF_SIZE : tmp_len;
  1674. priv->hw->desc->prepare_tso_tx_desc(desc, 0, buff_size,
  1675. 0, 1,
  1676. (last_segment) && (buff_size < TSO_MAX_BUFF_SIZE),
  1677. 0, 0);
  1678. tmp_len -= TSO_MAX_BUFF_SIZE;
  1679. }
  1680. }
  1681. /**
  1682. * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
  1683. * @skb : the socket buffer
  1684. * @dev : device pointer
  1685. * Description: this is the transmit function that is called on TSO frames
  1686. * (support available on GMAC4 and newer chips).
  1687. * Diagram below show the ring programming in case of TSO frames:
  1688. *
  1689. * First Descriptor
  1690. * --------
  1691. * | DES0 |---> buffer1 = L2/L3/L4 header
  1692. * | DES1 |---> TCP Payload (can continue on next descr...)
  1693. * | DES2 |---> buffer 1 and 2 len
  1694. * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
  1695. * --------
  1696. * |
  1697. * ...
  1698. * |
  1699. * --------
  1700. * | DES0 | --| Split TCP Payload on Buffers 1 and 2
  1701. * | DES1 | --|
  1702. * | DES2 | --> buffer 1 and 2 len
  1703. * | DES3 |
  1704. * --------
  1705. *
  1706. * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
  1707. */
  1708. static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
  1709. {
  1710. u32 pay_len, mss;
  1711. int tmp_pay_len = 0;
  1712. struct stmmac_priv *priv = netdev_priv(dev);
  1713. int nfrags = skb_shinfo(skb)->nr_frags;
  1714. unsigned int first_entry, des;
  1715. struct dma_desc *desc, *first, *mss_desc = NULL;
  1716. u8 proto_hdr_len;
  1717. int i;
  1718. /* Compute header lengths */
  1719. proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1720. /* Desc availability based on threshold should be enough safe */
  1721. if (unlikely(stmmac_tx_avail(priv) <
  1722. (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
  1723. if (!netif_queue_stopped(dev)) {
  1724. netif_stop_queue(dev);
  1725. /* This is a hard error, log it. */
  1726. netdev_err(priv->dev,
  1727. "%s: Tx Ring full when queue awake\n",
  1728. __func__);
  1729. }
  1730. return NETDEV_TX_BUSY;
  1731. }
  1732. pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
  1733. mss = skb_shinfo(skb)->gso_size;
  1734. /* set new MSS value if needed */
  1735. if (mss != priv->mss) {
  1736. mss_desc = priv->dma_tx + priv->cur_tx;
  1737. priv->hw->desc->set_mss(mss_desc, mss);
  1738. priv->mss = mss;
  1739. priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
  1740. }
  1741. if (netif_msg_tx_queued(priv)) {
  1742. pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
  1743. __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
  1744. pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
  1745. skb->data_len);
  1746. }
  1747. first_entry = priv->cur_tx;
  1748. desc = priv->dma_tx + first_entry;
  1749. first = desc;
  1750. /* first descriptor: fill Headers on Buf1 */
  1751. des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
  1752. DMA_TO_DEVICE);
  1753. if (dma_mapping_error(priv->device, des))
  1754. goto dma_map_err;
  1755. priv->tx_skbuff_dma[first_entry].buf = des;
  1756. priv->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
  1757. priv->tx_skbuff[first_entry] = skb;
  1758. first->des0 = cpu_to_le32(des);
  1759. /* Fill start of payload in buff2 of first descriptor */
  1760. if (pay_len)
  1761. first->des1 = cpu_to_le32(des + proto_hdr_len);
  1762. /* If needed take extra descriptors to fill the remaining payload */
  1763. tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
  1764. stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0));
  1765. /* Prepare fragments */
  1766. for (i = 0; i < nfrags; i++) {
  1767. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1768. des = skb_frag_dma_map(priv->device, frag, 0,
  1769. skb_frag_size(frag),
  1770. DMA_TO_DEVICE);
  1771. stmmac_tso_allocator(priv, des, skb_frag_size(frag),
  1772. (i == nfrags - 1));
  1773. priv->tx_skbuff_dma[priv->cur_tx].buf = des;
  1774. priv->tx_skbuff_dma[priv->cur_tx].len = skb_frag_size(frag);
  1775. priv->tx_skbuff[priv->cur_tx] = NULL;
  1776. priv->tx_skbuff_dma[priv->cur_tx].map_as_page = true;
  1777. }
  1778. priv->tx_skbuff_dma[priv->cur_tx].last_segment = true;
  1779. priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
  1780. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  1781. netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
  1782. __func__);
  1783. netif_stop_queue(dev);
  1784. }
  1785. dev->stats.tx_bytes += skb->len;
  1786. priv->xstats.tx_tso_frames++;
  1787. priv->xstats.tx_tso_nfrags += nfrags;
  1788. /* Manage tx mitigation */
  1789. priv->tx_count_frames += nfrags + 1;
  1790. if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
  1791. mod_timer(&priv->txtimer,
  1792. STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1793. } else {
  1794. priv->tx_count_frames = 0;
  1795. priv->hw->desc->set_tx_ic(desc);
  1796. priv->xstats.tx_set_ic_bit++;
  1797. }
  1798. if (!priv->hwts_tx_en)
  1799. skb_tx_timestamp(skb);
  1800. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1801. priv->hwts_tx_en)) {
  1802. /* declare that device is doing timestamping */
  1803. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1804. priv->hw->desc->enable_tx_timestamp(first);
  1805. }
  1806. /* Complete the first descriptor before granting the DMA */
  1807. priv->hw->desc->prepare_tso_tx_desc(first, 1,
  1808. proto_hdr_len,
  1809. pay_len,
  1810. 1, priv->tx_skbuff_dma[first_entry].last_segment,
  1811. tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
  1812. /* If context desc is used to change MSS */
  1813. if (mss_desc)
  1814. priv->hw->desc->set_tx_owner(mss_desc);
  1815. /* The own bit must be the latest setting done when prepare the
  1816. * descriptor and then barrier is needed to make sure that
  1817. * all is coherent before granting the DMA engine.
  1818. */
  1819. dma_wmb();
  1820. if (netif_msg_pktdata(priv)) {
  1821. pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
  1822. __func__, priv->cur_tx, priv->dirty_tx, first_entry,
  1823. priv->cur_tx, first, nfrags);
  1824. priv->hw->desc->display_ring((void *)priv->dma_tx, DMA_TX_SIZE,
  1825. 0);
  1826. pr_info(">>> frame to be transmitted: ");
  1827. print_pkt(skb->data, skb_headlen(skb));
  1828. }
  1829. netdev_sent_queue(dev, skb->len);
  1830. priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
  1831. STMMAC_CHAN0);
  1832. return NETDEV_TX_OK;
  1833. dma_map_err:
  1834. dev_err(priv->device, "Tx dma map failed\n");
  1835. dev_kfree_skb(skb);
  1836. priv->dev->stats.tx_dropped++;
  1837. return NETDEV_TX_OK;
  1838. }
  1839. /**
  1840. * stmmac_xmit - Tx entry point of the driver
  1841. * @skb : the socket buffer
  1842. * @dev : device pointer
  1843. * Description : this is the tx entry point of the driver.
  1844. * It programs the chain or the ring and supports oversized frames
  1845. * and SG feature.
  1846. */
  1847. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  1848. {
  1849. struct stmmac_priv *priv = netdev_priv(dev);
  1850. unsigned int nopaged_len = skb_headlen(skb);
  1851. int i, csum_insertion = 0, is_jumbo = 0;
  1852. int nfrags = skb_shinfo(skb)->nr_frags;
  1853. unsigned int entry, first_entry;
  1854. struct dma_desc *desc, *first;
  1855. unsigned int enh_desc;
  1856. unsigned int des;
  1857. /* Manage oversized TCP frames for GMAC4 device */
  1858. if (skb_is_gso(skb) && priv->tso) {
  1859. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  1860. return stmmac_tso_xmit(skb, dev);
  1861. }
  1862. if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
  1863. if (!netif_queue_stopped(dev)) {
  1864. netif_stop_queue(dev);
  1865. /* This is a hard error, log it. */
  1866. netdev_err(priv->dev,
  1867. "%s: Tx Ring full when queue awake\n",
  1868. __func__);
  1869. }
  1870. return NETDEV_TX_BUSY;
  1871. }
  1872. if (priv->tx_path_in_lpi_mode)
  1873. stmmac_disable_eee_mode(priv);
  1874. entry = priv->cur_tx;
  1875. first_entry = entry;
  1876. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  1877. if (likely(priv->extend_desc))
  1878. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1879. else
  1880. desc = priv->dma_tx + entry;
  1881. first = desc;
  1882. priv->tx_skbuff[first_entry] = skb;
  1883. enh_desc = priv->plat->enh_desc;
  1884. /* To program the descriptors according to the size of the frame */
  1885. if (enh_desc)
  1886. is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
  1887. if (unlikely(is_jumbo) && likely(priv->synopsys_id <
  1888. DWMAC_CORE_4_00)) {
  1889. entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
  1890. if (unlikely(entry < 0))
  1891. goto dma_map_err;
  1892. }
  1893. for (i = 0; i < nfrags; i++) {
  1894. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1895. int len = skb_frag_size(frag);
  1896. bool last_segment = (i == (nfrags - 1));
  1897. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1898. if (likely(priv->extend_desc))
  1899. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1900. else
  1901. desc = priv->dma_tx + entry;
  1902. des = skb_frag_dma_map(priv->device, frag, 0, len,
  1903. DMA_TO_DEVICE);
  1904. if (dma_mapping_error(priv->device, des))
  1905. goto dma_map_err; /* should reuse desc w/o issues */
  1906. priv->tx_skbuff[entry] = NULL;
  1907. priv->tx_skbuff_dma[entry].buf = des;
  1908. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
  1909. desc->des0 = cpu_to_le32(des);
  1910. else
  1911. desc->des2 = cpu_to_le32(des);
  1912. priv->tx_skbuff_dma[entry].map_as_page = true;
  1913. priv->tx_skbuff_dma[entry].len = len;
  1914. priv->tx_skbuff_dma[entry].last_segment = last_segment;
  1915. /* Prepare the descriptor and set the own bit too */
  1916. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
  1917. priv->mode, 1, last_segment);
  1918. }
  1919. entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
  1920. priv->cur_tx = entry;
  1921. if (netif_msg_pktdata(priv)) {
  1922. void *tx_head;
  1923. netdev_dbg(priv->dev,
  1924. "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
  1925. __func__, priv->cur_tx, priv->dirty_tx, first_entry,
  1926. entry, first, nfrags);
  1927. if (priv->extend_desc)
  1928. tx_head = (void *)priv->dma_etx;
  1929. else
  1930. tx_head = (void *)priv->dma_tx;
  1931. priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
  1932. netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
  1933. print_pkt(skb->data, skb->len);
  1934. }
  1935. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  1936. netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
  1937. __func__);
  1938. netif_stop_queue(dev);
  1939. }
  1940. dev->stats.tx_bytes += skb->len;
  1941. /* According to the coalesce parameter the IC bit for the latest
  1942. * segment is reset and the timer re-started to clean the tx status.
  1943. * This approach takes care about the fragments: desc is the first
  1944. * element in case of no SG.
  1945. */
  1946. priv->tx_count_frames += nfrags + 1;
  1947. if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
  1948. mod_timer(&priv->txtimer,
  1949. STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1950. } else {
  1951. priv->tx_count_frames = 0;
  1952. priv->hw->desc->set_tx_ic(desc);
  1953. priv->xstats.tx_set_ic_bit++;
  1954. }
  1955. if (!priv->hwts_tx_en)
  1956. skb_tx_timestamp(skb);
  1957. /* Ready to fill the first descriptor and set the OWN bit w/o any
  1958. * problems because all the descriptors are actually ready to be
  1959. * passed to the DMA engine.
  1960. */
  1961. if (likely(!is_jumbo)) {
  1962. bool last_segment = (nfrags == 0);
  1963. des = dma_map_single(priv->device, skb->data,
  1964. nopaged_len, DMA_TO_DEVICE);
  1965. if (dma_mapping_error(priv->device, des))
  1966. goto dma_map_err;
  1967. priv->tx_skbuff_dma[first_entry].buf = des;
  1968. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
  1969. first->des0 = cpu_to_le32(des);
  1970. else
  1971. first->des2 = cpu_to_le32(des);
  1972. priv->tx_skbuff_dma[first_entry].len = nopaged_len;
  1973. priv->tx_skbuff_dma[first_entry].last_segment = last_segment;
  1974. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1975. priv->hwts_tx_en)) {
  1976. /* declare that device is doing timestamping */
  1977. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1978. priv->hw->desc->enable_tx_timestamp(first);
  1979. }
  1980. /* Prepare the first descriptor setting the OWN bit too */
  1981. priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
  1982. csum_insertion, priv->mode, 1,
  1983. last_segment);
  1984. /* The own bit must be the latest setting done when prepare the
  1985. * descriptor and then barrier is needed to make sure that
  1986. * all is coherent before granting the DMA engine.
  1987. */
  1988. dma_wmb();
  1989. }
  1990. netdev_sent_queue(dev, skb->len);
  1991. if (priv->synopsys_id < DWMAC_CORE_4_00)
  1992. priv->hw->dma->enable_dma_transmission(priv->ioaddr);
  1993. else
  1994. priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
  1995. STMMAC_CHAN0);
  1996. return NETDEV_TX_OK;
  1997. dma_map_err:
  1998. netdev_err(priv->dev, "Tx DMA map failed\n");
  1999. dev_kfree_skb(skb);
  2000. priv->dev->stats.tx_dropped++;
  2001. return NETDEV_TX_OK;
  2002. }
  2003. static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
  2004. {
  2005. struct ethhdr *ehdr;
  2006. u16 vlanid;
  2007. if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
  2008. NETIF_F_HW_VLAN_CTAG_RX &&
  2009. !__vlan_get_tag(skb, &vlanid)) {
  2010. /* pop the vlan tag */
  2011. ehdr = (struct ethhdr *)skb->data;
  2012. memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
  2013. skb_pull(skb, VLAN_HLEN);
  2014. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
  2015. }
  2016. }
  2017. static inline int stmmac_rx_threshold_count(struct stmmac_priv *priv)
  2018. {
  2019. if (priv->rx_zeroc_thresh < STMMAC_RX_THRESH)
  2020. return 0;
  2021. return 1;
  2022. }
  2023. /**
  2024. * stmmac_rx_refill - refill used skb preallocated buffers
  2025. * @priv: driver private structure
  2026. * Description : this is to reallocate the skb for the reception process
  2027. * that is based on zero-copy.
  2028. */
  2029. static inline void stmmac_rx_refill(struct stmmac_priv *priv)
  2030. {
  2031. int bfsize = priv->dma_buf_sz;
  2032. unsigned int entry = priv->dirty_rx;
  2033. int dirty = stmmac_rx_dirty(priv);
  2034. while (dirty-- > 0) {
  2035. struct dma_desc *p;
  2036. if (priv->extend_desc)
  2037. p = (struct dma_desc *)(priv->dma_erx + entry);
  2038. else
  2039. p = priv->dma_rx + entry;
  2040. if (likely(priv->rx_skbuff[entry] == NULL)) {
  2041. struct sk_buff *skb;
  2042. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  2043. if (unlikely(!skb)) {
  2044. /* so for a while no zero-copy! */
  2045. priv->rx_zeroc_thresh = STMMAC_RX_THRESH;
  2046. if (unlikely(net_ratelimit()))
  2047. dev_err(priv->device,
  2048. "fail to alloc skb entry %d\n",
  2049. entry);
  2050. break;
  2051. }
  2052. priv->rx_skbuff[entry] = skb;
  2053. priv->rx_skbuff_dma[entry] =
  2054. dma_map_single(priv->device, skb->data, bfsize,
  2055. DMA_FROM_DEVICE);
  2056. if (dma_mapping_error(priv->device,
  2057. priv->rx_skbuff_dma[entry])) {
  2058. netdev_err(priv->dev, "Rx DMA map failed\n");
  2059. dev_kfree_skb(skb);
  2060. break;
  2061. }
  2062. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
  2063. p->des0 = cpu_to_le32(priv->rx_skbuff_dma[entry]);
  2064. p->des1 = 0;
  2065. } else {
  2066. p->des2 = cpu_to_le32(priv->rx_skbuff_dma[entry]);
  2067. }
  2068. if (priv->hw->mode->refill_desc3)
  2069. priv->hw->mode->refill_desc3(priv, p);
  2070. if (priv->rx_zeroc_thresh > 0)
  2071. priv->rx_zeroc_thresh--;
  2072. netif_dbg(priv, rx_status, priv->dev,
  2073. "refill entry #%d\n", entry);
  2074. }
  2075. dma_wmb();
  2076. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
  2077. priv->hw->desc->init_rx_desc(p, priv->use_riwt, 0, 0);
  2078. else
  2079. priv->hw->desc->set_rx_owner(p);
  2080. dma_wmb();
  2081. entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
  2082. }
  2083. priv->dirty_rx = entry;
  2084. }
  2085. /**
  2086. * stmmac_rx - manage the receive process
  2087. * @priv: driver private structure
  2088. * @limit: napi bugget.
  2089. * Description : this the function called by the napi poll method.
  2090. * It gets all the frames inside the ring.
  2091. */
  2092. static int stmmac_rx(struct stmmac_priv *priv, int limit)
  2093. {
  2094. unsigned int entry = priv->cur_rx;
  2095. unsigned int next_entry;
  2096. unsigned int count = 0;
  2097. int coe = priv->hw->rx_csum;
  2098. if (netif_msg_rx_status(priv)) {
  2099. void *rx_head;
  2100. netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
  2101. if (priv->extend_desc)
  2102. rx_head = (void *)priv->dma_erx;
  2103. else
  2104. rx_head = (void *)priv->dma_rx;
  2105. priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
  2106. }
  2107. while (count < limit) {
  2108. int status;
  2109. struct dma_desc *p;
  2110. struct dma_desc *np;
  2111. if (priv->extend_desc)
  2112. p = (struct dma_desc *)(priv->dma_erx + entry);
  2113. else
  2114. p = priv->dma_rx + entry;
  2115. /* read the status of the incoming frame */
  2116. status = priv->hw->desc->rx_status(&priv->dev->stats,
  2117. &priv->xstats, p);
  2118. /* check if managed by the DMA otherwise go ahead */
  2119. if (unlikely(status & dma_own))
  2120. break;
  2121. count++;
  2122. priv->cur_rx = STMMAC_GET_ENTRY(priv->cur_rx, DMA_RX_SIZE);
  2123. next_entry = priv->cur_rx;
  2124. if (priv->extend_desc)
  2125. np = (struct dma_desc *)(priv->dma_erx + next_entry);
  2126. else
  2127. np = priv->dma_rx + next_entry;
  2128. prefetch(np);
  2129. if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
  2130. priv->hw->desc->rx_extended_status(&priv->dev->stats,
  2131. &priv->xstats,
  2132. priv->dma_erx +
  2133. entry);
  2134. if (unlikely(status == discard_frame)) {
  2135. priv->dev->stats.rx_errors++;
  2136. if (priv->hwts_rx_en && !priv->extend_desc) {
  2137. /* DESC2 & DESC3 will be overwritten by device
  2138. * with timestamp value, hence reinitialize
  2139. * them in stmmac_rx_refill() function so that
  2140. * device can reuse it.
  2141. */
  2142. priv->rx_skbuff[entry] = NULL;
  2143. dma_unmap_single(priv->device,
  2144. priv->rx_skbuff_dma[entry],
  2145. priv->dma_buf_sz,
  2146. DMA_FROM_DEVICE);
  2147. }
  2148. } else {
  2149. struct sk_buff *skb;
  2150. int frame_len;
  2151. unsigned int des;
  2152. if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
  2153. des = le32_to_cpu(p->des0);
  2154. else
  2155. des = le32_to_cpu(p->des2);
  2156. frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
  2157. /* If frame length is greater than skb buffer size
  2158. * (preallocated during init) then the packet is
  2159. * ignored
  2160. */
  2161. if (frame_len > priv->dma_buf_sz) {
  2162. netdev_err(priv->dev,
  2163. "len %d larger than size (%d)\n",
  2164. frame_len, priv->dma_buf_sz);
  2165. priv->dev->stats.rx_length_errors++;
  2166. break;
  2167. }
  2168. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  2169. * Type frames (LLC/LLC-SNAP)
  2170. */
  2171. if (unlikely(status != llc_snap))
  2172. frame_len -= ETH_FCS_LEN;
  2173. if (netif_msg_rx_status(priv)) {
  2174. netdev_dbg(priv->dev, "\tdesc: %p [entry %d] buff=0x%x\n",
  2175. p, entry, des);
  2176. if (frame_len > ETH_FRAME_LEN)
  2177. netdev_dbg(priv->dev, "frame size %d, COE: %d\n",
  2178. frame_len, status);
  2179. }
  2180. /* The zero-copy is always used for all the sizes
  2181. * in case of GMAC4 because it needs
  2182. * to refill the used descriptors, always.
  2183. */
  2184. if (unlikely(!priv->plat->has_gmac4 &&
  2185. ((frame_len < priv->rx_copybreak) ||
  2186. stmmac_rx_threshold_count(priv)))) {
  2187. skb = netdev_alloc_skb_ip_align(priv->dev,
  2188. frame_len);
  2189. if (unlikely(!skb)) {
  2190. if (net_ratelimit())
  2191. dev_warn(priv->device,
  2192. "packet dropped\n");
  2193. priv->dev->stats.rx_dropped++;
  2194. break;
  2195. }
  2196. dma_sync_single_for_cpu(priv->device,
  2197. priv->rx_skbuff_dma
  2198. [entry], frame_len,
  2199. DMA_FROM_DEVICE);
  2200. skb_copy_to_linear_data(skb,
  2201. priv->
  2202. rx_skbuff[entry]->data,
  2203. frame_len);
  2204. skb_put(skb, frame_len);
  2205. dma_sync_single_for_device(priv->device,
  2206. priv->rx_skbuff_dma
  2207. [entry], frame_len,
  2208. DMA_FROM_DEVICE);
  2209. } else {
  2210. skb = priv->rx_skbuff[entry];
  2211. if (unlikely(!skb)) {
  2212. netdev_err(priv->dev,
  2213. "%s: Inconsistent Rx chain\n",
  2214. priv->dev->name);
  2215. priv->dev->stats.rx_dropped++;
  2216. break;
  2217. }
  2218. prefetch(skb->data - NET_IP_ALIGN);
  2219. priv->rx_skbuff[entry] = NULL;
  2220. priv->rx_zeroc_thresh++;
  2221. skb_put(skb, frame_len);
  2222. dma_unmap_single(priv->device,
  2223. priv->rx_skbuff_dma[entry],
  2224. priv->dma_buf_sz,
  2225. DMA_FROM_DEVICE);
  2226. }
  2227. if (netif_msg_pktdata(priv)) {
  2228. netdev_dbg(priv->dev, "frame received (%dbytes)",
  2229. frame_len);
  2230. print_pkt(skb->data, frame_len);
  2231. }
  2232. stmmac_get_rx_hwtstamp(priv, p, np, skb);
  2233. stmmac_rx_vlan(priv->dev, skb);
  2234. skb->protocol = eth_type_trans(skb, priv->dev);
  2235. if (unlikely(!coe))
  2236. skb_checksum_none_assert(skb);
  2237. else
  2238. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2239. napi_gro_receive(&priv->napi, skb);
  2240. priv->dev->stats.rx_packets++;
  2241. priv->dev->stats.rx_bytes += frame_len;
  2242. }
  2243. entry = next_entry;
  2244. }
  2245. stmmac_rx_refill(priv);
  2246. priv->xstats.rx_pkt_n += count;
  2247. return count;
  2248. }
  2249. /**
  2250. * stmmac_poll - stmmac poll method (NAPI)
  2251. * @napi : pointer to the napi structure.
  2252. * @budget : maximum number of packets that the current CPU can receive from
  2253. * all interfaces.
  2254. * Description :
  2255. * To look at the incoming frames and clear the tx resources.
  2256. */
  2257. static int stmmac_poll(struct napi_struct *napi, int budget)
  2258. {
  2259. struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
  2260. int work_done = 0;
  2261. priv->xstats.napi_poll++;
  2262. stmmac_tx_clean(priv);
  2263. work_done = stmmac_rx(priv, budget);
  2264. if (work_done < budget) {
  2265. napi_complete_done(napi, work_done);
  2266. stmmac_enable_dma_irq(priv);
  2267. }
  2268. return work_done;
  2269. }
  2270. /**
  2271. * stmmac_tx_timeout
  2272. * @dev : Pointer to net device structure
  2273. * Description: this function is called when a packet transmission fails to
  2274. * complete within a reasonable time. The driver will mark the error in the
  2275. * netdev structure and arrange for the device to be reset to a sane state
  2276. * in order to transmit a new packet.
  2277. */
  2278. static void stmmac_tx_timeout(struct net_device *dev)
  2279. {
  2280. struct stmmac_priv *priv = netdev_priv(dev);
  2281. /* Clear Tx resources and restart transmitting again */
  2282. stmmac_tx_err(priv);
  2283. }
  2284. /**
  2285. * stmmac_set_rx_mode - entry point for multicast addressing
  2286. * @dev : pointer to the device structure
  2287. * Description:
  2288. * This function is a driver entry point which gets called by the kernel
  2289. * whenever multicast addresses must be enabled/disabled.
  2290. * Return value:
  2291. * void.
  2292. */
  2293. static void stmmac_set_rx_mode(struct net_device *dev)
  2294. {
  2295. struct stmmac_priv *priv = netdev_priv(dev);
  2296. priv->hw->mac->set_filter(priv->hw, dev);
  2297. }
  2298. /**
  2299. * stmmac_change_mtu - entry point to change MTU size for the device.
  2300. * @dev : device pointer.
  2301. * @new_mtu : the new MTU size for the device.
  2302. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  2303. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  2304. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  2305. * Return value:
  2306. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  2307. * file on failure.
  2308. */
  2309. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  2310. {
  2311. struct stmmac_priv *priv = netdev_priv(dev);
  2312. if (netif_running(dev)) {
  2313. netdev_err(priv->dev, "must be stopped to change its MTU\n");
  2314. return -EBUSY;
  2315. }
  2316. dev->mtu = new_mtu;
  2317. netdev_update_features(dev);
  2318. return 0;
  2319. }
  2320. static netdev_features_t stmmac_fix_features(struct net_device *dev,
  2321. netdev_features_t features)
  2322. {
  2323. struct stmmac_priv *priv = netdev_priv(dev);
  2324. if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
  2325. features &= ~NETIF_F_RXCSUM;
  2326. if (!priv->plat->tx_coe)
  2327. features &= ~NETIF_F_CSUM_MASK;
  2328. /* Some GMAC devices have a bugged Jumbo frame support that
  2329. * needs to have the Tx COE disabled for oversized frames
  2330. * (due to limited buffer sizes). In this case we disable
  2331. * the TX csum insertion in the TDES and not use SF.
  2332. */
  2333. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  2334. features &= ~NETIF_F_CSUM_MASK;
  2335. /* Disable tso if asked by ethtool */
  2336. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  2337. if (features & NETIF_F_TSO)
  2338. priv->tso = true;
  2339. else
  2340. priv->tso = false;
  2341. }
  2342. return features;
  2343. }
  2344. static int stmmac_set_features(struct net_device *netdev,
  2345. netdev_features_t features)
  2346. {
  2347. struct stmmac_priv *priv = netdev_priv(netdev);
  2348. /* Keep the COE Type in case of csum is supporting */
  2349. if (features & NETIF_F_RXCSUM)
  2350. priv->hw->rx_csum = priv->plat->rx_coe;
  2351. else
  2352. priv->hw->rx_csum = 0;
  2353. /* No check needed because rx_coe has been set before and it will be
  2354. * fixed in case of issue.
  2355. */
  2356. priv->hw->mac->rx_ipc(priv->hw);
  2357. return 0;
  2358. }
  2359. /**
  2360. * stmmac_interrupt - main ISR
  2361. * @irq: interrupt number.
  2362. * @dev_id: to pass the net device pointer.
  2363. * Description: this is the main driver interrupt service routine.
  2364. * It can call:
  2365. * o DMA service routine (to manage incoming frame reception and transmission
  2366. * status)
  2367. * o Core interrupts to manage: remote wake-up, management counter, LPI
  2368. * interrupts.
  2369. */
  2370. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  2371. {
  2372. struct net_device *dev = (struct net_device *)dev_id;
  2373. struct stmmac_priv *priv = netdev_priv(dev);
  2374. if (priv->irq_wake)
  2375. pm_wakeup_event(priv->device, 0);
  2376. if (unlikely(!dev)) {
  2377. netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
  2378. return IRQ_NONE;
  2379. }
  2380. /* To handle GMAC own interrupts */
  2381. if ((priv->plat->has_gmac) || (priv->plat->has_gmac4)) {
  2382. int status = priv->hw->mac->host_irq_status(priv->hw,
  2383. &priv->xstats);
  2384. if (unlikely(status)) {
  2385. /* For LPI we need to save the tx status */
  2386. if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
  2387. priv->tx_path_in_lpi_mode = true;
  2388. if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
  2389. priv->tx_path_in_lpi_mode = false;
  2390. if (status & CORE_IRQ_MTL_RX_OVERFLOW && priv->hw->dma->set_rx_tail_ptr)
  2391. priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
  2392. priv->rx_tail_addr,
  2393. STMMAC_CHAN0);
  2394. }
  2395. /* PCS link status */
  2396. if (priv->hw->pcs) {
  2397. if (priv->xstats.pcs_link)
  2398. netif_carrier_on(dev);
  2399. else
  2400. netif_carrier_off(dev);
  2401. }
  2402. }
  2403. /* To handle DMA interrupts */
  2404. stmmac_dma_interrupt(priv);
  2405. return IRQ_HANDLED;
  2406. }
  2407. #ifdef CONFIG_NET_POLL_CONTROLLER
  2408. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  2409. * to allow network I/O with interrupts disabled.
  2410. */
  2411. static void stmmac_poll_controller(struct net_device *dev)
  2412. {
  2413. disable_irq(dev->irq);
  2414. stmmac_interrupt(dev->irq, dev);
  2415. enable_irq(dev->irq);
  2416. }
  2417. #endif
  2418. /**
  2419. * stmmac_ioctl - Entry point for the Ioctl
  2420. * @dev: Device pointer.
  2421. * @rq: An IOCTL specefic structure, that can contain a pointer to
  2422. * a proprietary structure used to pass information to the driver.
  2423. * @cmd: IOCTL command
  2424. * Description:
  2425. * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
  2426. */
  2427. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2428. {
  2429. int ret = -EOPNOTSUPP;
  2430. if (!netif_running(dev))
  2431. return -EINVAL;
  2432. switch (cmd) {
  2433. case SIOCGMIIPHY:
  2434. case SIOCGMIIREG:
  2435. case SIOCSMIIREG:
  2436. if (!dev->phydev)
  2437. return -EINVAL;
  2438. ret = phy_mii_ioctl(dev->phydev, rq, cmd);
  2439. break;
  2440. case SIOCSHWTSTAMP:
  2441. ret = stmmac_hwtstamp_ioctl(dev, rq);
  2442. break;
  2443. default:
  2444. break;
  2445. }
  2446. return ret;
  2447. }
  2448. #ifdef CONFIG_DEBUG_FS
  2449. static struct dentry *stmmac_fs_dir;
  2450. static void sysfs_display_ring(void *head, int size, int extend_desc,
  2451. struct seq_file *seq)
  2452. {
  2453. int i;
  2454. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  2455. struct dma_desc *p = (struct dma_desc *)head;
  2456. for (i = 0; i < size; i++) {
  2457. if (extend_desc) {
  2458. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2459. i, (unsigned int)virt_to_phys(ep),
  2460. le32_to_cpu(ep->basic.des0),
  2461. le32_to_cpu(ep->basic.des1),
  2462. le32_to_cpu(ep->basic.des2),
  2463. le32_to_cpu(ep->basic.des3));
  2464. ep++;
  2465. } else {
  2466. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2467. i, (unsigned int)virt_to_phys(ep),
  2468. le32_to_cpu(p->des0), le32_to_cpu(p->des1),
  2469. le32_to_cpu(p->des2), le32_to_cpu(p->des3));
  2470. p++;
  2471. }
  2472. seq_printf(seq, "\n");
  2473. }
  2474. }
  2475. static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
  2476. {
  2477. struct net_device *dev = seq->private;
  2478. struct stmmac_priv *priv = netdev_priv(dev);
  2479. if (priv->extend_desc) {
  2480. seq_printf(seq, "Extended RX descriptor ring:\n");
  2481. sysfs_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1, seq);
  2482. seq_printf(seq, "Extended TX descriptor ring:\n");
  2483. sysfs_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1, seq);
  2484. } else {
  2485. seq_printf(seq, "RX descriptor ring:\n");
  2486. sysfs_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0, seq);
  2487. seq_printf(seq, "TX descriptor ring:\n");
  2488. sysfs_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0, seq);
  2489. }
  2490. return 0;
  2491. }
  2492. static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
  2493. {
  2494. return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
  2495. }
  2496. /* Debugfs files, should appear in /sys/kernel/debug/stmmaceth/eth0 */
  2497. static const struct file_operations stmmac_rings_status_fops = {
  2498. .owner = THIS_MODULE,
  2499. .open = stmmac_sysfs_ring_open,
  2500. .read = seq_read,
  2501. .llseek = seq_lseek,
  2502. .release = single_release,
  2503. };
  2504. static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
  2505. {
  2506. struct net_device *dev = seq->private;
  2507. struct stmmac_priv *priv = netdev_priv(dev);
  2508. if (!priv->hw_cap_support) {
  2509. seq_printf(seq, "DMA HW features not supported\n");
  2510. return 0;
  2511. }
  2512. seq_printf(seq, "==============================\n");
  2513. seq_printf(seq, "\tDMA HW features\n");
  2514. seq_printf(seq, "==============================\n");
  2515. seq_printf(seq, "\t10/100 Mbps: %s\n",
  2516. (priv->dma_cap.mbps_10_100) ? "Y" : "N");
  2517. seq_printf(seq, "\t1000 Mbps: %s\n",
  2518. (priv->dma_cap.mbps_1000) ? "Y" : "N");
  2519. seq_printf(seq, "\tHalf duplex: %s\n",
  2520. (priv->dma_cap.half_duplex) ? "Y" : "N");
  2521. seq_printf(seq, "\tHash Filter: %s\n",
  2522. (priv->dma_cap.hash_filter) ? "Y" : "N");
  2523. seq_printf(seq, "\tMultiple MAC address registers: %s\n",
  2524. (priv->dma_cap.multi_addr) ? "Y" : "N");
  2525. seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
  2526. (priv->dma_cap.pcs) ? "Y" : "N");
  2527. seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
  2528. (priv->dma_cap.sma_mdio) ? "Y" : "N");
  2529. seq_printf(seq, "\tPMT Remote wake up: %s\n",
  2530. (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
  2531. seq_printf(seq, "\tPMT Magic Frame: %s\n",
  2532. (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
  2533. seq_printf(seq, "\tRMON module: %s\n",
  2534. (priv->dma_cap.rmon) ? "Y" : "N");
  2535. seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
  2536. (priv->dma_cap.time_stamp) ? "Y" : "N");
  2537. seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
  2538. (priv->dma_cap.atime_stamp) ? "Y" : "N");
  2539. seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
  2540. (priv->dma_cap.eee) ? "Y" : "N");
  2541. seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
  2542. seq_printf(seq, "\tChecksum Offload in TX: %s\n",
  2543. (priv->dma_cap.tx_coe) ? "Y" : "N");
  2544. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  2545. seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
  2546. (priv->dma_cap.rx_coe) ? "Y" : "N");
  2547. } else {
  2548. seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
  2549. (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
  2550. seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
  2551. (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
  2552. }
  2553. seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
  2554. (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
  2555. seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
  2556. priv->dma_cap.number_rx_channel);
  2557. seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
  2558. priv->dma_cap.number_tx_channel);
  2559. seq_printf(seq, "\tEnhanced descriptors: %s\n",
  2560. (priv->dma_cap.enh_desc) ? "Y" : "N");
  2561. return 0;
  2562. }
  2563. static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
  2564. {
  2565. return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
  2566. }
  2567. static const struct file_operations stmmac_dma_cap_fops = {
  2568. .owner = THIS_MODULE,
  2569. .open = stmmac_sysfs_dma_cap_open,
  2570. .read = seq_read,
  2571. .llseek = seq_lseek,
  2572. .release = single_release,
  2573. };
  2574. static int stmmac_init_fs(struct net_device *dev)
  2575. {
  2576. struct stmmac_priv *priv = netdev_priv(dev);
  2577. /* Create per netdev entries */
  2578. priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
  2579. if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
  2580. netdev_err(priv->dev, "ERROR failed to create debugfs directory\n");
  2581. return -ENOMEM;
  2582. }
  2583. /* Entry to report DMA RX/TX rings */
  2584. priv->dbgfs_rings_status =
  2585. debugfs_create_file("descriptors_status", S_IRUGO,
  2586. priv->dbgfs_dir, dev,
  2587. &stmmac_rings_status_fops);
  2588. if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
  2589. netdev_err(priv->dev, "ERROR creating stmmac ring debugfs file\n");
  2590. debugfs_remove_recursive(priv->dbgfs_dir);
  2591. return -ENOMEM;
  2592. }
  2593. /* Entry to report the DMA HW features */
  2594. priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
  2595. priv->dbgfs_dir,
  2596. dev, &stmmac_dma_cap_fops);
  2597. if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
  2598. netdev_err(priv->dev, "ERROR creating stmmac MMC debugfs file\n");
  2599. debugfs_remove_recursive(priv->dbgfs_dir);
  2600. return -ENOMEM;
  2601. }
  2602. return 0;
  2603. }
  2604. static void stmmac_exit_fs(struct net_device *dev)
  2605. {
  2606. struct stmmac_priv *priv = netdev_priv(dev);
  2607. debugfs_remove_recursive(priv->dbgfs_dir);
  2608. }
  2609. #endif /* CONFIG_DEBUG_FS */
  2610. static const struct net_device_ops stmmac_netdev_ops = {
  2611. .ndo_open = stmmac_open,
  2612. .ndo_start_xmit = stmmac_xmit,
  2613. .ndo_stop = stmmac_release,
  2614. .ndo_change_mtu = stmmac_change_mtu,
  2615. .ndo_fix_features = stmmac_fix_features,
  2616. .ndo_set_features = stmmac_set_features,
  2617. .ndo_set_rx_mode = stmmac_set_rx_mode,
  2618. .ndo_tx_timeout = stmmac_tx_timeout,
  2619. .ndo_do_ioctl = stmmac_ioctl,
  2620. #ifdef CONFIG_NET_POLL_CONTROLLER
  2621. .ndo_poll_controller = stmmac_poll_controller,
  2622. #endif
  2623. .ndo_set_mac_address = eth_mac_addr,
  2624. };
  2625. /**
  2626. * stmmac_hw_init - Init the MAC device
  2627. * @priv: driver private structure
  2628. * Description: this function is to configure the MAC device according to
  2629. * some platform parameters or the HW capability register. It prepares the
  2630. * driver to use either ring or chain modes and to setup either enhanced or
  2631. * normal descriptors.
  2632. */
  2633. static int stmmac_hw_init(struct stmmac_priv *priv)
  2634. {
  2635. struct mac_device_info *mac;
  2636. /* Identify the MAC HW device */
  2637. if (priv->plat->has_gmac) {
  2638. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  2639. mac = dwmac1000_setup(priv->ioaddr,
  2640. priv->plat->multicast_filter_bins,
  2641. priv->plat->unicast_filter_entries,
  2642. &priv->synopsys_id);
  2643. } else if (priv->plat->has_gmac4) {
  2644. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  2645. mac = dwmac4_setup(priv->ioaddr,
  2646. priv->plat->multicast_filter_bins,
  2647. priv->plat->unicast_filter_entries,
  2648. &priv->synopsys_id);
  2649. } else {
  2650. mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
  2651. }
  2652. if (!mac)
  2653. return -ENOMEM;
  2654. priv->hw = mac;
  2655. /* To use the chained or ring mode */
  2656. if (priv->synopsys_id >= DWMAC_CORE_4_00) {
  2657. priv->hw->mode = &dwmac4_ring_mode_ops;
  2658. } else {
  2659. if (chain_mode) {
  2660. priv->hw->mode = &chain_mode_ops;
  2661. dev_info(priv->device, "Chain mode enabled\n");
  2662. priv->mode = STMMAC_CHAIN_MODE;
  2663. } else {
  2664. priv->hw->mode = &ring_mode_ops;
  2665. dev_info(priv->device, "Ring mode enabled\n");
  2666. priv->mode = STMMAC_RING_MODE;
  2667. }
  2668. }
  2669. /* Get the HW capability (new GMAC newer than 3.50a) */
  2670. priv->hw_cap_support = stmmac_get_hw_features(priv);
  2671. if (priv->hw_cap_support) {
  2672. dev_info(priv->device, "DMA HW capability register supported\n");
  2673. /* We can override some gmac/dma configuration fields: e.g.
  2674. * enh_desc, tx_coe (e.g. that are passed through the
  2675. * platform) with the values from the HW capability
  2676. * register (if supported).
  2677. */
  2678. priv->plat->enh_desc = priv->dma_cap.enh_desc;
  2679. priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
  2680. priv->hw->pmt = priv->plat->pmt;
  2681. /* TXCOE doesn't work in thresh DMA mode */
  2682. if (priv->plat->force_thresh_dma_mode)
  2683. priv->plat->tx_coe = 0;
  2684. else
  2685. priv->plat->tx_coe = priv->dma_cap.tx_coe;
  2686. /* In case of GMAC4 rx_coe is from HW cap register. */
  2687. priv->plat->rx_coe = priv->dma_cap.rx_coe;
  2688. if (priv->dma_cap.rx_coe_type2)
  2689. priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
  2690. else if (priv->dma_cap.rx_coe_type1)
  2691. priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
  2692. } else {
  2693. dev_info(priv->device, "No HW DMA feature register supported\n");
  2694. }
  2695. /* To use alternate (extended), normal or GMAC4 descriptor structures */
  2696. if (priv->synopsys_id >= DWMAC_CORE_4_00)
  2697. priv->hw->desc = &dwmac4_desc_ops;
  2698. else
  2699. stmmac_selec_desc_mode(priv);
  2700. if (priv->plat->rx_coe) {
  2701. priv->hw->rx_csum = priv->plat->rx_coe;
  2702. dev_info(priv->device, "RX Checksum Offload Engine supported\n");
  2703. if (priv->synopsys_id < DWMAC_CORE_4_00)
  2704. dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
  2705. }
  2706. if (priv->plat->tx_coe)
  2707. dev_info(priv->device, "TX Checksum insertion supported\n");
  2708. if (priv->plat->pmt) {
  2709. dev_info(priv->device, "Wake-Up On Lan supported\n");
  2710. device_set_wakeup_capable(priv->device, 1);
  2711. }
  2712. if (priv->dma_cap.tsoen)
  2713. dev_info(priv->device, "TSO supported\n");
  2714. return 0;
  2715. }
  2716. /**
  2717. * stmmac_dvr_probe
  2718. * @device: device pointer
  2719. * @plat_dat: platform data pointer
  2720. * @res: stmmac resource pointer
  2721. * Description: this is the main probe function used to
  2722. * call the alloc_etherdev, allocate the priv structure.
  2723. * Return:
  2724. * returns 0 on success, otherwise errno.
  2725. */
  2726. int stmmac_dvr_probe(struct device *device,
  2727. struct plat_stmmacenet_data *plat_dat,
  2728. struct stmmac_resources *res)
  2729. {
  2730. int ret = 0;
  2731. struct net_device *ndev = NULL;
  2732. struct stmmac_priv *priv;
  2733. ndev = alloc_etherdev(sizeof(struct stmmac_priv));
  2734. if (!ndev)
  2735. return -ENOMEM;
  2736. SET_NETDEV_DEV(ndev, device);
  2737. priv = netdev_priv(ndev);
  2738. priv->device = device;
  2739. priv->dev = ndev;
  2740. stmmac_set_ethtool_ops(ndev);
  2741. priv->pause = pause;
  2742. priv->plat = plat_dat;
  2743. priv->ioaddr = res->addr;
  2744. priv->dev->base_addr = (unsigned long)res->addr;
  2745. priv->dev->irq = res->irq;
  2746. priv->wol_irq = res->wol_irq;
  2747. priv->lpi_irq = res->lpi_irq;
  2748. if (res->mac)
  2749. memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
  2750. dev_set_drvdata(device, priv->dev);
  2751. /* Verify driver arguments */
  2752. stmmac_verify_args();
  2753. /* Override with kernel parameters if supplied XXX CRS XXX
  2754. * this needs to have multiple instances
  2755. */
  2756. if ((phyaddr >= 0) && (phyaddr <= 31))
  2757. priv->plat->phy_addr = phyaddr;
  2758. if (priv->plat->stmmac_rst)
  2759. reset_control_deassert(priv->plat->stmmac_rst);
  2760. /* Init MAC and get the capabilities */
  2761. ret = stmmac_hw_init(priv);
  2762. if (ret)
  2763. goto error_hw_init;
  2764. ndev->netdev_ops = &stmmac_netdev_ops;
  2765. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2766. NETIF_F_RXCSUM;
  2767. if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
  2768. ndev->hw_features |= NETIF_F_TSO;
  2769. priv->tso = true;
  2770. dev_info(priv->device, "TSO feature enabled\n");
  2771. }
  2772. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  2773. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  2774. #ifdef STMMAC_VLAN_TAG_USED
  2775. /* Both mac100 and gmac support receive VLAN tag detection */
  2776. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2777. #endif
  2778. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  2779. /* MTU range: 46 - hw-specific max */
  2780. ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
  2781. if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
  2782. ndev->max_mtu = JUMBO_LEN;
  2783. else
  2784. ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  2785. /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
  2786. * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
  2787. */
  2788. if ((priv->plat->maxmtu < ndev->max_mtu) &&
  2789. (priv->plat->maxmtu >= ndev->min_mtu))
  2790. ndev->max_mtu = priv->plat->maxmtu;
  2791. else if (priv->plat->maxmtu < ndev->min_mtu)
  2792. dev_warn(priv->device,
  2793. "%s: warning: maxmtu having invalid value (%d)\n",
  2794. __func__, priv->plat->maxmtu);
  2795. if (flow_ctrl)
  2796. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  2797. /* Rx Watchdog is available in the COREs newer than the 3.40.
  2798. * In some case, for example on bugged HW this feature
  2799. * has to be disable and this can be done by passing the
  2800. * riwt_off field from the platform.
  2801. */
  2802. if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
  2803. priv->use_riwt = 1;
  2804. dev_info(priv->device,
  2805. "Enable RX Mitigation via HW Watchdog Timer\n");
  2806. }
  2807. netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
  2808. spin_lock_init(&priv->lock);
  2809. /* If a specific clk_csr value is passed from the platform
  2810. * this means that the CSR Clock Range selection cannot be
  2811. * changed at run-time and it is fixed. Viceversa the driver'll try to
  2812. * set the MDC clock dynamically according to the csr actual
  2813. * clock input.
  2814. */
  2815. if (!priv->plat->clk_csr)
  2816. stmmac_clk_csr_set(priv);
  2817. else
  2818. priv->clk_csr = priv->plat->clk_csr;
  2819. stmmac_check_pcs_mode(priv);
  2820. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  2821. priv->hw->pcs != STMMAC_PCS_TBI &&
  2822. priv->hw->pcs != STMMAC_PCS_RTBI) {
  2823. /* MDIO bus Registration */
  2824. ret = stmmac_mdio_register(ndev);
  2825. if (ret < 0) {
  2826. dev_err(priv->device,
  2827. "%s: MDIO bus (id: %d) registration failed",
  2828. __func__, priv->plat->bus_id);
  2829. goto error_mdio_register;
  2830. }
  2831. }
  2832. ret = register_netdev(ndev);
  2833. if (ret) {
  2834. dev_err(priv->device, "%s: ERROR %i registering the device\n",
  2835. __func__, ret);
  2836. goto error_netdev_register;
  2837. }
  2838. return ret;
  2839. error_netdev_register:
  2840. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  2841. priv->hw->pcs != STMMAC_PCS_TBI &&
  2842. priv->hw->pcs != STMMAC_PCS_RTBI)
  2843. stmmac_mdio_unregister(ndev);
  2844. error_mdio_register:
  2845. netif_napi_del(&priv->napi);
  2846. error_hw_init:
  2847. free_netdev(ndev);
  2848. return ret;
  2849. }
  2850. EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
  2851. /**
  2852. * stmmac_dvr_remove
  2853. * @dev: device pointer
  2854. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  2855. * changes the link status, releases the DMA descriptor rings.
  2856. */
  2857. int stmmac_dvr_remove(struct device *dev)
  2858. {
  2859. struct net_device *ndev = dev_get_drvdata(dev);
  2860. struct stmmac_priv *priv = netdev_priv(ndev);
  2861. netdev_info(priv->dev, "%s: removing driver", __func__);
  2862. priv->hw->dma->stop_rx(priv->ioaddr);
  2863. priv->hw->dma->stop_tx(priv->ioaddr);
  2864. stmmac_set_mac(priv->ioaddr, false);
  2865. netif_carrier_off(ndev);
  2866. unregister_netdev(ndev);
  2867. if (priv->plat->stmmac_rst)
  2868. reset_control_assert(priv->plat->stmmac_rst);
  2869. clk_disable_unprepare(priv->plat->pclk);
  2870. clk_disable_unprepare(priv->plat->stmmac_clk);
  2871. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  2872. priv->hw->pcs != STMMAC_PCS_TBI &&
  2873. priv->hw->pcs != STMMAC_PCS_RTBI)
  2874. stmmac_mdio_unregister(ndev);
  2875. free_netdev(ndev);
  2876. return 0;
  2877. }
  2878. EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
  2879. /**
  2880. * stmmac_suspend - suspend callback
  2881. * @dev: device pointer
  2882. * Description: this is the function to suspend the device and it is called
  2883. * by the platform driver to stop the network queue, release the resources,
  2884. * program the PMT register (for WoL), clean and release driver resources.
  2885. */
  2886. int stmmac_suspend(struct device *dev)
  2887. {
  2888. struct net_device *ndev = dev_get_drvdata(dev);
  2889. struct stmmac_priv *priv = netdev_priv(ndev);
  2890. unsigned long flags;
  2891. if (!ndev || !netif_running(ndev))
  2892. return 0;
  2893. if (ndev->phydev)
  2894. phy_stop(ndev->phydev);
  2895. spin_lock_irqsave(&priv->lock, flags);
  2896. netif_device_detach(ndev);
  2897. netif_stop_queue(ndev);
  2898. napi_disable(&priv->napi);
  2899. /* Stop TX/RX DMA */
  2900. priv->hw->dma->stop_tx(priv->ioaddr);
  2901. priv->hw->dma->stop_rx(priv->ioaddr);
  2902. /* Enable Power down mode by programming the PMT regs */
  2903. if (device_may_wakeup(priv->device)) {
  2904. priv->hw->mac->pmt(priv->hw, priv->wolopts);
  2905. priv->irq_wake = 1;
  2906. } else {
  2907. stmmac_set_mac(priv->ioaddr, false);
  2908. pinctrl_pm_select_sleep_state(priv->device);
  2909. /* Disable clock in case of PWM is off */
  2910. clk_disable(priv->plat->pclk);
  2911. clk_disable(priv->plat->stmmac_clk);
  2912. }
  2913. spin_unlock_irqrestore(&priv->lock, flags);
  2914. priv->oldlink = 0;
  2915. priv->speed = SPEED_UNKNOWN;
  2916. priv->oldduplex = DUPLEX_UNKNOWN;
  2917. return 0;
  2918. }
  2919. EXPORT_SYMBOL_GPL(stmmac_suspend);
  2920. /**
  2921. * stmmac_resume - resume callback
  2922. * @dev: device pointer
  2923. * Description: when resume this function is invoked to setup the DMA and CORE
  2924. * in a usable state.
  2925. */
  2926. int stmmac_resume(struct device *dev)
  2927. {
  2928. struct net_device *ndev = dev_get_drvdata(dev);
  2929. struct stmmac_priv *priv = netdev_priv(ndev);
  2930. unsigned long flags;
  2931. if (!netif_running(ndev))
  2932. return 0;
  2933. /* Power Down bit, into the PM register, is cleared
  2934. * automatically as soon as a magic packet or a Wake-up frame
  2935. * is received. Anyway, it's better to manually clear
  2936. * this bit because it can generate problems while resuming
  2937. * from another devices (e.g. serial console).
  2938. */
  2939. if (device_may_wakeup(priv->device)) {
  2940. spin_lock_irqsave(&priv->lock, flags);
  2941. priv->hw->mac->pmt(priv->hw, 0);
  2942. spin_unlock_irqrestore(&priv->lock, flags);
  2943. priv->irq_wake = 0;
  2944. } else {
  2945. pinctrl_pm_select_default_state(priv->device);
  2946. /* enable the clk previously disabled */
  2947. clk_enable(priv->plat->stmmac_clk);
  2948. clk_enable(priv->plat->pclk);
  2949. /* reset the phy so that it's ready */
  2950. if (priv->mii)
  2951. stmmac_mdio_reset(priv->mii);
  2952. }
  2953. netif_device_attach(ndev);
  2954. spin_lock_irqsave(&priv->lock, flags);
  2955. priv->cur_rx = 0;
  2956. priv->dirty_rx = 0;
  2957. priv->dirty_tx = 0;
  2958. priv->cur_tx = 0;
  2959. /* reset private mss value to force mss context settings at
  2960. * next tso xmit (only used for gmac4).
  2961. */
  2962. priv->mss = 0;
  2963. stmmac_clear_descriptors(priv);
  2964. stmmac_hw_setup(ndev, false);
  2965. stmmac_init_tx_coalesce(priv);
  2966. stmmac_set_rx_mode(ndev);
  2967. napi_enable(&priv->napi);
  2968. netif_start_queue(ndev);
  2969. spin_unlock_irqrestore(&priv->lock, flags);
  2970. if (ndev->phydev)
  2971. phy_start(ndev->phydev);
  2972. return 0;
  2973. }
  2974. EXPORT_SYMBOL_GPL(stmmac_resume);
  2975. #ifndef MODULE
  2976. static int __init stmmac_cmdline_opt(char *str)
  2977. {
  2978. char *opt;
  2979. if (!str || !*str)
  2980. return -EINVAL;
  2981. while ((opt = strsep(&str, ",")) != NULL) {
  2982. if (!strncmp(opt, "debug:", 6)) {
  2983. if (kstrtoint(opt + 6, 0, &debug))
  2984. goto err;
  2985. } else if (!strncmp(opt, "phyaddr:", 8)) {
  2986. if (kstrtoint(opt + 8, 0, &phyaddr))
  2987. goto err;
  2988. } else if (!strncmp(opt, "buf_sz:", 7)) {
  2989. if (kstrtoint(opt + 7, 0, &buf_sz))
  2990. goto err;
  2991. } else if (!strncmp(opt, "tc:", 3)) {
  2992. if (kstrtoint(opt + 3, 0, &tc))
  2993. goto err;
  2994. } else if (!strncmp(opt, "watchdog:", 9)) {
  2995. if (kstrtoint(opt + 9, 0, &watchdog))
  2996. goto err;
  2997. } else if (!strncmp(opt, "flow_ctrl:", 10)) {
  2998. if (kstrtoint(opt + 10, 0, &flow_ctrl))
  2999. goto err;
  3000. } else if (!strncmp(opt, "pause:", 6)) {
  3001. if (kstrtoint(opt + 6, 0, &pause))
  3002. goto err;
  3003. } else if (!strncmp(opt, "eee_timer:", 10)) {
  3004. if (kstrtoint(opt + 10, 0, &eee_timer))
  3005. goto err;
  3006. } else if (!strncmp(opt, "chain_mode:", 11)) {
  3007. if (kstrtoint(opt + 11, 0, &chain_mode))
  3008. goto err;
  3009. }
  3010. }
  3011. return 0;
  3012. err:
  3013. pr_err("%s: ERROR broken module parameter conversion", __func__);
  3014. return -EINVAL;
  3015. }
  3016. __setup("stmmaceth=", stmmac_cmdline_opt);
  3017. #endif /* MODULE */
  3018. static int __init stmmac_init(void)
  3019. {
  3020. #ifdef CONFIG_DEBUG_FS
  3021. /* Create debugfs main directory if it doesn't exist yet */
  3022. if (!stmmac_fs_dir) {
  3023. stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
  3024. if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
  3025. pr_err("ERROR %s, debugfs create directory failed\n",
  3026. STMMAC_RESOURCE_NAME);
  3027. return -ENOMEM;
  3028. }
  3029. }
  3030. #endif
  3031. return 0;
  3032. }
  3033. static void __exit stmmac_exit(void)
  3034. {
  3035. #ifdef CONFIG_DEBUG_FS
  3036. debugfs_remove_recursive(stmmac_fs_dir);
  3037. #endif
  3038. }
  3039. module_init(stmmac_init)
  3040. module_exit(stmmac_exit)
  3041. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
  3042. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  3043. MODULE_LICENSE("GPL");