stmmac_main.c 100 KB

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