stmmac_main.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982
  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. #ifdef CONFIG_STMMAC_DEBUG_FS
  39. #include <linux/debugfs.h>
  40. #include <linux/seq_file.h>
  41. #endif /* CONFIG_STMMAC_DEBUG_FS */
  42. #include <linux/net_tstamp.h>
  43. #include "stmmac_ptp.h"
  44. #include "stmmac.h"
  45. #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
  46. #define JUMBO_LEN 9000
  47. /* Module parameters */
  48. #define TX_TIMEO 5000
  49. static int watchdog = TX_TIMEO;
  50. module_param(watchdog, int, S_IRUGO | S_IWUSR);
  51. MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
  52. static int debug = -1;
  53. module_param(debug, int, S_IRUGO | S_IWUSR);
  54. MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
  55. int phyaddr = -1;
  56. module_param(phyaddr, int, S_IRUGO);
  57. MODULE_PARM_DESC(phyaddr, "Physical device address");
  58. #define DMA_TX_SIZE 256
  59. static int dma_txsize = DMA_TX_SIZE;
  60. module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
  61. MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
  62. #define DMA_RX_SIZE 256
  63. static int dma_rxsize = DMA_RX_SIZE;
  64. module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
  65. MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
  66. static int flow_ctrl = FLOW_OFF;
  67. module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
  68. MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
  69. static int pause = PAUSE_TIME;
  70. module_param(pause, int, S_IRUGO | S_IWUSR);
  71. MODULE_PARM_DESC(pause, "Flow Control Pause Time");
  72. #define TC_DEFAULT 64
  73. static int tc = TC_DEFAULT;
  74. module_param(tc, int, S_IRUGO | S_IWUSR);
  75. MODULE_PARM_DESC(tc, "DMA threshold control value");
  76. #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
  77. static int buf_sz = DMA_BUFFER_SIZE;
  78. module_param(buf_sz, int, S_IRUGO | S_IWUSR);
  79. MODULE_PARM_DESC(buf_sz, "DMA buffer size");
  80. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  81. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  82. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  83. #define STMMAC_DEFAULT_LPI_TIMER 1000
  84. static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  85. module_param(eee_timer, int, S_IRUGO | S_IWUSR);
  86. MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
  87. #define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
  88. /* By default the driver will use the ring mode to manage tx and rx descriptors
  89. * but passing this value so user can force to use the chain instead of the ring
  90. */
  91. static unsigned int chain_mode;
  92. module_param(chain_mode, int, S_IRUGO);
  93. MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
  94. static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
  95. #ifdef CONFIG_STMMAC_DEBUG_FS
  96. static int stmmac_init_fs(struct net_device *dev);
  97. static void stmmac_exit_fs(void);
  98. #endif
  99. #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
  100. /**
  101. * stmmac_verify_args - verify the driver parameters.
  102. * Description: it verifies if some wrong parameter is passed to the driver.
  103. * Note that wrong parameters are replaced with the default values.
  104. */
  105. static void stmmac_verify_args(void)
  106. {
  107. if (unlikely(watchdog < 0))
  108. watchdog = TX_TIMEO;
  109. if (unlikely(dma_rxsize < 0))
  110. dma_rxsize = DMA_RX_SIZE;
  111. if (unlikely(dma_txsize < 0))
  112. dma_txsize = DMA_TX_SIZE;
  113. if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
  114. buf_sz = DMA_BUFFER_SIZE;
  115. if (unlikely(flow_ctrl > 1))
  116. flow_ctrl = FLOW_AUTO;
  117. else if (likely(flow_ctrl < 0))
  118. flow_ctrl = FLOW_OFF;
  119. if (unlikely((pause < 0) || (pause > 0xffff)))
  120. pause = PAUSE_TIME;
  121. if (eee_timer < 0)
  122. eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  123. }
  124. /**
  125. * stmmac_clk_csr_set - dynamically set the MDC clock
  126. * @priv: driver private structure
  127. * Description: this is to dynamically set the MDC clock according to the csr
  128. * clock input.
  129. * Note:
  130. * If a specific clk_csr value is passed from the platform
  131. * this means that the CSR Clock Range selection cannot be
  132. * changed at run-time and it is fixed (as reported in the driver
  133. * documentation). Viceversa the driver will try to set the MDC
  134. * clock dynamically according to the actual clock input.
  135. */
  136. static void stmmac_clk_csr_set(struct stmmac_priv *priv)
  137. {
  138. u32 clk_rate;
  139. clk_rate = clk_get_rate(priv->stmmac_clk);
  140. /* Platform provided default clk_csr would be assumed valid
  141. * for all other cases except for the below mentioned ones.
  142. * For values higher than the IEEE 802.3 specified frequency
  143. * we can not estimate the proper divider as it is not known
  144. * the frequency of clk_csr_i. So we do not change the default
  145. * divider.
  146. */
  147. if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
  148. if (clk_rate < CSR_F_35M)
  149. priv->clk_csr = STMMAC_CSR_20_35M;
  150. else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
  151. priv->clk_csr = STMMAC_CSR_35_60M;
  152. else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
  153. priv->clk_csr = STMMAC_CSR_60_100M;
  154. else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
  155. priv->clk_csr = STMMAC_CSR_100_150M;
  156. else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
  157. priv->clk_csr = STMMAC_CSR_150_250M;
  158. else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
  159. priv->clk_csr = STMMAC_CSR_250_300M;
  160. }
  161. }
  162. static void print_pkt(unsigned char *buf, int len)
  163. {
  164. int j;
  165. pr_debug("len = %d byte, buf addr: 0x%p", len, buf);
  166. for (j = 0; j < len; j++) {
  167. if ((j % 16) == 0)
  168. pr_debug("\n %03x:", j);
  169. pr_debug(" %02x", buf[j]);
  170. }
  171. pr_debug("\n");
  172. }
  173. /* minimum number of free TX descriptors required to wake up TX process */
  174. #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
  175. static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
  176. {
  177. return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
  178. }
  179. /**
  180. * stmmac_hw_fix_mac_speed: callback for speed selection
  181. * @priv: driver private structure
  182. * Description: on some platforms (e.g. ST), some HW system configuraton
  183. * registers have to be set according to the link speed negotiated.
  184. */
  185. static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
  186. {
  187. struct phy_device *phydev = priv->phydev;
  188. if (likely(priv->plat->fix_mac_speed))
  189. priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
  190. }
  191. /**
  192. * stmmac_enable_eee_mode: Check and enter in LPI mode
  193. * @priv: driver private structure
  194. * Description: this function is to verify and enter in LPI mode for EEE.
  195. */
  196. static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
  197. {
  198. /* Check and enter in LPI mode */
  199. if ((priv->dirty_tx == priv->cur_tx) &&
  200. (priv->tx_path_in_lpi_mode == false))
  201. priv->hw->mac->set_eee_mode(priv->ioaddr);
  202. }
  203. /**
  204. * stmmac_disable_eee_mode: disable/exit from EEE
  205. * @priv: driver private structure
  206. * Description: this function is to exit and disable EEE in case of
  207. * LPI state is true. This is called by the xmit.
  208. */
  209. void stmmac_disable_eee_mode(struct stmmac_priv *priv)
  210. {
  211. priv->hw->mac->reset_eee_mode(priv->ioaddr);
  212. del_timer_sync(&priv->eee_ctrl_timer);
  213. priv->tx_path_in_lpi_mode = false;
  214. }
  215. /**
  216. * stmmac_eee_ctrl_timer: EEE TX SW timer.
  217. * @arg : data hook
  218. * Description:
  219. * if there is no data transfer and if we are not in LPI state,
  220. * then MAC Transmitter can be moved to LPI state.
  221. */
  222. static void stmmac_eee_ctrl_timer(unsigned long arg)
  223. {
  224. struct stmmac_priv *priv = (struct stmmac_priv *)arg;
  225. stmmac_enable_eee_mode(priv);
  226. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  227. }
  228. /**
  229. * stmmac_eee_init: init EEE
  230. * @priv: driver private structure
  231. * Description:
  232. * If the EEE support has been enabled while configuring the driver,
  233. * if the GMAC actually supports the EEE (from the HW cap reg) and the
  234. * phy can also manage EEE, so enable the LPI state and start the timer
  235. * to verify if the tx path can enter in LPI state.
  236. */
  237. bool stmmac_eee_init(struct stmmac_priv *priv)
  238. {
  239. bool ret = false;
  240. /* Using PCS we cannot dial with the phy registers at this stage
  241. * so we do not support extra feature like EEE.
  242. */
  243. if ((priv->pcs == STMMAC_PCS_RGMII) || (priv->pcs == STMMAC_PCS_TBI) ||
  244. (priv->pcs == STMMAC_PCS_RTBI))
  245. goto out;
  246. /* MAC core supports the EEE feature. */
  247. if (priv->dma_cap.eee) {
  248. /* Check if the PHY supports EEE */
  249. if (phy_init_eee(priv->phydev, 1))
  250. goto out;
  251. if (!priv->eee_active) {
  252. priv->eee_active = 1;
  253. init_timer(&priv->eee_ctrl_timer);
  254. priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
  255. priv->eee_ctrl_timer.data = (unsigned long)priv;
  256. priv->eee_ctrl_timer.expires = STMMAC_LPI_T(eee_timer);
  257. add_timer(&priv->eee_ctrl_timer);
  258. priv->hw->mac->set_eee_timer(priv->ioaddr,
  259. STMMAC_DEFAULT_LIT_LS,
  260. priv->tx_lpi_timer);
  261. } else
  262. /* Set HW EEE according to the speed */
  263. priv->hw->mac->set_eee_pls(priv->ioaddr,
  264. priv->phydev->link);
  265. pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
  266. ret = true;
  267. }
  268. out:
  269. return ret;
  270. }
  271. /* stmmac_get_tx_hwtstamp: get HW TX timestamps
  272. * @priv: driver private structure
  273. * @entry : descriptor index to be used.
  274. * @skb : the socket buffer
  275. * Description :
  276. * This function will read timestamp from the descriptor & pass it to stack.
  277. * and also perform some sanity checks.
  278. */
  279. static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
  280. unsigned int entry, struct sk_buff *skb)
  281. {
  282. struct skb_shared_hwtstamps shhwtstamp;
  283. u64 ns;
  284. void *desc = NULL;
  285. if (!priv->hwts_tx_en)
  286. return;
  287. /* exit if skb doesn't support hw tstamp */
  288. if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
  289. return;
  290. if (priv->adv_ts)
  291. desc = (priv->dma_etx + entry);
  292. else
  293. desc = (priv->dma_tx + entry);
  294. /* check tx tstamp status */
  295. if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
  296. return;
  297. /* get the valid tstamp */
  298. ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
  299. memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  300. shhwtstamp.hwtstamp = ns_to_ktime(ns);
  301. /* pass tstamp to stack */
  302. skb_tstamp_tx(skb, &shhwtstamp);
  303. return;
  304. }
  305. /* stmmac_get_rx_hwtstamp: get HW RX timestamps
  306. * @priv: driver private structure
  307. * @entry : descriptor index to be used.
  308. * @skb : the socket buffer
  309. * Description :
  310. * This function will read received packet's timestamp from the descriptor
  311. * and pass it to stack. It also perform some sanity checks.
  312. */
  313. static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
  314. unsigned int entry, struct sk_buff *skb)
  315. {
  316. struct skb_shared_hwtstamps *shhwtstamp = NULL;
  317. u64 ns;
  318. void *desc = NULL;
  319. if (!priv->hwts_rx_en)
  320. return;
  321. if (priv->adv_ts)
  322. desc = (priv->dma_erx + entry);
  323. else
  324. desc = (priv->dma_rx + entry);
  325. /* exit if rx tstamp is not valid */
  326. if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
  327. return;
  328. /* get valid tstamp */
  329. ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
  330. shhwtstamp = skb_hwtstamps(skb);
  331. memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
  332. shhwtstamp->hwtstamp = ns_to_ktime(ns);
  333. }
  334. /**
  335. * stmmac_hwtstamp_ioctl - control hardware timestamping.
  336. * @dev: device pointer.
  337. * @ifr: An IOCTL specefic structure, that can contain a pointer to
  338. * a proprietary structure used to pass information to the driver.
  339. * Description:
  340. * This function configures the MAC to enable/disable both outgoing(TX)
  341. * and incoming(RX) packets time stamping based on user input.
  342. * Return Value:
  343. * 0 on success and an appropriate -ve integer on failure.
  344. */
  345. static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  346. {
  347. struct stmmac_priv *priv = netdev_priv(dev);
  348. struct hwtstamp_config config;
  349. struct timespec now;
  350. u64 temp = 0;
  351. u32 ptp_v2 = 0;
  352. u32 tstamp_all = 0;
  353. u32 ptp_over_ipv4_udp = 0;
  354. u32 ptp_over_ipv6_udp = 0;
  355. u32 ptp_over_ethernet = 0;
  356. u32 snap_type_sel = 0;
  357. u32 ts_master_en = 0;
  358. u32 ts_event_en = 0;
  359. u32 value = 0;
  360. if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
  361. netdev_alert(priv->dev, "No support for HW time stamping\n");
  362. priv->hwts_tx_en = 0;
  363. priv->hwts_rx_en = 0;
  364. return -EOPNOTSUPP;
  365. }
  366. if (copy_from_user(&config, ifr->ifr_data,
  367. sizeof(struct hwtstamp_config)))
  368. return -EFAULT;
  369. pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
  370. __func__, config.flags, config.tx_type, config.rx_filter);
  371. /* reserved for future extensions */
  372. if (config.flags)
  373. return -EINVAL;
  374. switch (config.tx_type) {
  375. case HWTSTAMP_TX_OFF:
  376. priv->hwts_tx_en = 0;
  377. break;
  378. case HWTSTAMP_TX_ON:
  379. priv->hwts_tx_en = 1;
  380. break;
  381. default:
  382. return -ERANGE;
  383. }
  384. if (priv->adv_ts) {
  385. switch (config.rx_filter) {
  386. case HWTSTAMP_FILTER_NONE:
  387. /* time stamp no incoming packet at all */
  388. config.rx_filter = HWTSTAMP_FILTER_NONE;
  389. break;
  390. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  391. /* PTP v1, UDP, any kind of event packet */
  392. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  393. /* take time stamp for all event messages */
  394. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  395. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  396. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  397. break;
  398. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  399. /* PTP v1, UDP, Sync packet */
  400. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
  401. /* take time stamp for SYNC messages only */
  402. ts_event_en = PTP_TCR_TSEVNTENA;
  403. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  404. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  405. break;
  406. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  407. /* PTP v1, UDP, Delay_req packet */
  408. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
  409. /* take time stamp for Delay_Req messages only */
  410. ts_master_en = PTP_TCR_TSMSTRENA;
  411. ts_event_en = PTP_TCR_TSEVNTENA;
  412. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  413. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  414. break;
  415. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  416. /* PTP v2, UDP, any kind of event packet */
  417. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
  418. ptp_v2 = PTP_TCR_TSVER2ENA;
  419. /* take time stamp for all event messages */
  420. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  421. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  422. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  423. break;
  424. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  425. /* PTP v2, UDP, Sync packet */
  426. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
  427. ptp_v2 = PTP_TCR_TSVER2ENA;
  428. /* take time stamp for SYNC messages only */
  429. ts_event_en = PTP_TCR_TSEVNTENA;
  430. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  431. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  432. break;
  433. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  434. /* PTP v2, UDP, Delay_req packet */
  435. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
  436. ptp_v2 = PTP_TCR_TSVER2ENA;
  437. /* take time stamp for Delay_Req messages only */
  438. ts_master_en = PTP_TCR_TSMSTRENA;
  439. ts_event_en = PTP_TCR_TSEVNTENA;
  440. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  441. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  442. break;
  443. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  444. /* PTP v2/802.AS1 any layer, any kind of event packet */
  445. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  446. ptp_v2 = PTP_TCR_TSVER2ENA;
  447. /* take time stamp for all event messages */
  448. snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
  449. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  450. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  451. ptp_over_ethernet = PTP_TCR_TSIPENA;
  452. break;
  453. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  454. /* PTP v2/802.AS1, any layer, Sync packet */
  455. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
  456. ptp_v2 = PTP_TCR_TSVER2ENA;
  457. /* take time stamp for SYNC messages only */
  458. ts_event_en = PTP_TCR_TSEVNTENA;
  459. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  460. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  461. ptp_over_ethernet = PTP_TCR_TSIPENA;
  462. break;
  463. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  464. /* PTP v2/802.AS1, any layer, Delay_req packet */
  465. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
  466. ptp_v2 = PTP_TCR_TSVER2ENA;
  467. /* take time stamp for Delay_Req messages only */
  468. ts_master_en = PTP_TCR_TSMSTRENA;
  469. ts_event_en = PTP_TCR_TSEVNTENA;
  470. ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
  471. ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
  472. ptp_over_ethernet = PTP_TCR_TSIPENA;
  473. break;
  474. case HWTSTAMP_FILTER_ALL:
  475. /* time stamp any incoming packet */
  476. config.rx_filter = HWTSTAMP_FILTER_ALL;
  477. tstamp_all = PTP_TCR_TSENALL;
  478. break;
  479. default:
  480. return -ERANGE;
  481. }
  482. } else {
  483. switch (config.rx_filter) {
  484. case HWTSTAMP_FILTER_NONE:
  485. config.rx_filter = HWTSTAMP_FILTER_NONE;
  486. break;
  487. default:
  488. /* PTP v1, UDP, any kind of event packet */
  489. config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
  490. break;
  491. }
  492. }
  493. priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
  494. if (!priv->hwts_tx_en && !priv->hwts_rx_en)
  495. priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
  496. else {
  497. value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
  498. tstamp_all | ptp_v2 | ptp_over_ethernet |
  499. ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
  500. ts_master_en | snap_type_sel);
  501. priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
  502. /* program Sub Second Increment reg */
  503. priv->hw->ptp->config_sub_second_increment(priv->ioaddr);
  504. /* calculate default added value:
  505. * formula is :
  506. * addend = (2^32)/freq_div_ratio;
  507. * where, freq_div_ratio = STMMAC_SYSCLOCK/50MHz
  508. * hence, addend = ((2^32) * 50MHz)/STMMAC_SYSCLOCK;
  509. * NOTE: STMMAC_SYSCLOCK should be >= 50MHz to
  510. * achive 20ns accuracy.
  511. *
  512. * 2^x * y == (y << x), hence
  513. * 2^32 * 50000000 ==> (50000000 << 32)
  514. */
  515. temp = (u64) (50000000ULL << 32);
  516. priv->default_addend = div_u64(temp, STMMAC_SYSCLOCK);
  517. priv->hw->ptp->config_addend(priv->ioaddr,
  518. priv->default_addend);
  519. /* initialize system time */
  520. getnstimeofday(&now);
  521. priv->hw->ptp->init_systime(priv->ioaddr, now.tv_sec,
  522. now.tv_nsec);
  523. }
  524. return copy_to_user(ifr->ifr_data, &config,
  525. sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
  526. }
  527. /**
  528. * stmmac_init_ptp: init PTP
  529. * @priv: driver private structure
  530. * Description: this is to verify if the HW supports the PTPv1 or v2.
  531. * This is done by looking at the HW cap. register.
  532. * Also it registers the ptp driver.
  533. */
  534. static int stmmac_init_ptp(struct stmmac_priv *priv)
  535. {
  536. if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
  537. return -EOPNOTSUPP;
  538. if (netif_msg_hw(priv)) {
  539. if (priv->dma_cap.time_stamp) {
  540. pr_debug("IEEE 1588-2002 Time Stamp supported\n");
  541. priv->adv_ts = 0;
  542. }
  543. if (priv->dma_cap.atime_stamp && priv->extend_desc) {
  544. pr_debug
  545. ("IEEE 1588-2008 Advanced Time Stamp supported\n");
  546. priv->adv_ts = 1;
  547. }
  548. }
  549. priv->hw->ptp = &stmmac_ptp;
  550. priv->hwts_tx_en = 0;
  551. priv->hwts_rx_en = 0;
  552. return stmmac_ptp_register(priv);
  553. }
  554. static void stmmac_release_ptp(struct stmmac_priv *priv)
  555. {
  556. stmmac_ptp_unregister(priv);
  557. }
  558. /**
  559. * stmmac_adjust_link
  560. * @dev: net device structure
  561. * Description: it adjusts the link parameters.
  562. */
  563. static void stmmac_adjust_link(struct net_device *dev)
  564. {
  565. struct stmmac_priv *priv = netdev_priv(dev);
  566. struct phy_device *phydev = priv->phydev;
  567. unsigned long flags;
  568. int new_state = 0;
  569. unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
  570. if (phydev == NULL)
  571. return;
  572. spin_lock_irqsave(&priv->lock, flags);
  573. if (phydev->link) {
  574. u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
  575. /* Now we make sure that we can be in full duplex mode.
  576. * If not, we operate in half-duplex mode. */
  577. if (phydev->duplex != priv->oldduplex) {
  578. new_state = 1;
  579. if (!(phydev->duplex))
  580. ctrl &= ~priv->hw->link.duplex;
  581. else
  582. ctrl |= priv->hw->link.duplex;
  583. priv->oldduplex = phydev->duplex;
  584. }
  585. /* Flow Control operation */
  586. if (phydev->pause)
  587. priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
  588. fc, pause_time);
  589. if (phydev->speed != priv->speed) {
  590. new_state = 1;
  591. switch (phydev->speed) {
  592. case 1000:
  593. if (likely(priv->plat->has_gmac))
  594. ctrl &= ~priv->hw->link.port;
  595. stmmac_hw_fix_mac_speed(priv);
  596. break;
  597. case 100:
  598. case 10:
  599. if (priv->plat->has_gmac) {
  600. ctrl |= priv->hw->link.port;
  601. if (phydev->speed == SPEED_100) {
  602. ctrl |= priv->hw->link.speed;
  603. } else {
  604. ctrl &= ~(priv->hw->link.speed);
  605. }
  606. } else {
  607. ctrl &= ~priv->hw->link.port;
  608. }
  609. stmmac_hw_fix_mac_speed(priv);
  610. break;
  611. default:
  612. if (netif_msg_link(priv))
  613. pr_warn("%s: Speed (%d) not 10/100\n",
  614. dev->name, phydev->speed);
  615. break;
  616. }
  617. priv->speed = phydev->speed;
  618. }
  619. writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
  620. if (!priv->oldlink) {
  621. new_state = 1;
  622. priv->oldlink = 1;
  623. }
  624. } else if (priv->oldlink) {
  625. new_state = 1;
  626. priv->oldlink = 0;
  627. priv->speed = 0;
  628. priv->oldduplex = -1;
  629. }
  630. if (new_state && netif_msg_link(priv))
  631. phy_print_status(phydev);
  632. /* At this stage, it could be needed to setup the EEE or adjust some
  633. * MAC related HW registers.
  634. */
  635. priv->eee_enabled = stmmac_eee_init(priv);
  636. spin_unlock_irqrestore(&priv->lock, flags);
  637. }
  638. /**
  639. * stmmac_check_pcs_mode: verify if RGMII/SGMII is supported
  640. * @priv: driver private structure
  641. * Description: this is to verify if the HW supports the PCS.
  642. * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
  643. * configured for the TBI, RTBI, or SGMII PHY interface.
  644. */
  645. static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
  646. {
  647. int interface = priv->plat->interface;
  648. if (priv->dma_cap.pcs) {
  649. if ((interface == PHY_INTERFACE_MODE_RGMII) ||
  650. (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
  651. (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
  652. (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
  653. pr_debug("STMMAC: PCS RGMII support enable\n");
  654. priv->pcs = STMMAC_PCS_RGMII;
  655. } else if (interface == PHY_INTERFACE_MODE_SGMII) {
  656. pr_debug("STMMAC: PCS SGMII support enable\n");
  657. priv->pcs = STMMAC_PCS_SGMII;
  658. }
  659. }
  660. }
  661. /**
  662. * stmmac_init_phy - PHY initialization
  663. * @dev: net device structure
  664. * Description: it initializes the driver's PHY state, and attaches the PHY
  665. * to the mac driver.
  666. * Return value:
  667. * 0 on success
  668. */
  669. static int stmmac_init_phy(struct net_device *dev)
  670. {
  671. struct stmmac_priv *priv = netdev_priv(dev);
  672. struct phy_device *phydev;
  673. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  674. char bus_id[MII_BUS_ID_SIZE];
  675. int interface = priv->plat->interface;
  676. priv->oldlink = 0;
  677. priv->speed = 0;
  678. priv->oldduplex = -1;
  679. if (priv->plat->phy_bus_name)
  680. snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
  681. priv->plat->phy_bus_name, priv->plat->bus_id);
  682. else
  683. snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
  684. priv->plat->bus_id);
  685. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  686. priv->plat->phy_addr);
  687. pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
  688. phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
  689. if (IS_ERR(phydev)) {
  690. pr_err("%s: Could not attach to PHY\n", dev->name);
  691. return PTR_ERR(phydev);
  692. }
  693. /* Stop Advertising 1000BASE Capability if interface is not GMII */
  694. if ((interface == PHY_INTERFACE_MODE_MII) ||
  695. (interface == PHY_INTERFACE_MODE_RMII))
  696. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  697. SUPPORTED_1000baseT_Full);
  698. /*
  699. * Broken HW is sometimes missing the pull-up resistor on the
  700. * MDIO line, which results in reads to non-existent devices returning
  701. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  702. * device as well.
  703. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  704. */
  705. if (phydev->phy_id == 0) {
  706. phy_disconnect(phydev);
  707. return -ENODEV;
  708. }
  709. pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
  710. " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
  711. priv->phydev = phydev;
  712. return 0;
  713. }
  714. /**
  715. * stmmac_display_ring: display ring
  716. * @head: pointer to the head of the ring passed.
  717. * @size: size of the ring.
  718. * @extend_desc: to verify if extended descriptors are used.
  719. * Description: display the control/status and buffer descriptors.
  720. */
  721. static void stmmac_display_ring(void *head, int size, int extend_desc)
  722. {
  723. int i;
  724. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  725. struct dma_desc *p = (struct dma_desc *)head;
  726. for (i = 0; i < size; i++) {
  727. u64 x;
  728. if (extend_desc) {
  729. x = *(u64 *) ep;
  730. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  731. i, (unsigned int)virt_to_phys(ep),
  732. (unsigned int)x, (unsigned int)(x >> 32),
  733. ep->basic.des2, ep->basic.des3);
  734. ep++;
  735. } else {
  736. x = *(u64 *) p;
  737. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
  738. i, (unsigned int)virt_to_phys(p),
  739. (unsigned int)x, (unsigned int)(x >> 32),
  740. p->des2, p->des3);
  741. p++;
  742. }
  743. pr_info("\n");
  744. }
  745. }
  746. static void stmmac_display_rings(struct stmmac_priv *priv)
  747. {
  748. unsigned int txsize = priv->dma_tx_size;
  749. unsigned int rxsize = priv->dma_rx_size;
  750. if (priv->extend_desc) {
  751. pr_info("Extended RX descriptor ring:\n");
  752. stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
  753. pr_info("Extended TX descriptor ring:\n");
  754. stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
  755. } else {
  756. pr_info("RX descriptor ring:\n");
  757. stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
  758. pr_info("TX descriptor ring:\n");
  759. stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
  760. }
  761. }
  762. static int stmmac_set_bfsize(int mtu, int bufsize)
  763. {
  764. int ret = bufsize;
  765. if (mtu >= BUF_SIZE_4KiB)
  766. ret = BUF_SIZE_8KiB;
  767. else if (mtu >= BUF_SIZE_2KiB)
  768. ret = BUF_SIZE_4KiB;
  769. else if (mtu >= DMA_BUFFER_SIZE)
  770. ret = BUF_SIZE_2KiB;
  771. else
  772. ret = DMA_BUFFER_SIZE;
  773. return ret;
  774. }
  775. /**
  776. * stmmac_clear_descriptors: clear descriptors
  777. * @priv: driver private structure
  778. * Description: this function is called to clear the tx and rx descriptors
  779. * in case of both basic and extended descriptors are used.
  780. */
  781. static void stmmac_clear_descriptors(struct stmmac_priv *priv)
  782. {
  783. int i;
  784. unsigned int txsize = priv->dma_tx_size;
  785. unsigned int rxsize = priv->dma_rx_size;
  786. /* Clear the Rx/Tx descriptors */
  787. for (i = 0; i < rxsize; i++)
  788. if (priv->extend_desc)
  789. priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
  790. priv->use_riwt, priv->mode,
  791. (i == rxsize - 1));
  792. else
  793. priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
  794. priv->use_riwt, priv->mode,
  795. (i == rxsize - 1));
  796. for (i = 0; i < txsize; i++)
  797. if (priv->extend_desc)
  798. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  799. priv->mode,
  800. (i == txsize - 1));
  801. else
  802. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  803. priv->mode,
  804. (i == txsize - 1));
  805. }
  806. static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
  807. int i)
  808. {
  809. struct sk_buff *skb;
  810. skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
  811. GFP_KERNEL);
  812. if (!skb) {
  813. pr_err("%s: Rx init fails; skb is NULL\n", __func__);
  814. return -ENOMEM;
  815. }
  816. skb_reserve(skb, NET_IP_ALIGN);
  817. priv->rx_skbuff[i] = skb;
  818. priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  819. priv->dma_buf_sz,
  820. DMA_FROM_DEVICE);
  821. if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
  822. pr_err("%s: DMA mapping error\n", __func__);
  823. dev_kfree_skb_any(skb);
  824. return -EINVAL;
  825. }
  826. p->des2 = priv->rx_skbuff_dma[i];
  827. if ((priv->mode == STMMAC_RING_MODE) &&
  828. (priv->dma_buf_sz == BUF_SIZE_16KiB))
  829. priv->hw->ring->init_desc3(p);
  830. return 0;
  831. }
  832. static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
  833. {
  834. if (priv->rx_skbuff[i]) {
  835. dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
  836. priv->dma_buf_sz, DMA_FROM_DEVICE);
  837. dev_kfree_skb_any(priv->rx_skbuff[i]);
  838. }
  839. priv->rx_skbuff[i] = NULL;
  840. }
  841. /**
  842. * init_dma_desc_rings - init the RX/TX descriptor rings
  843. * @dev: net device structure
  844. * Description: this function initializes the DMA RX/TX descriptors
  845. * and allocates the socket buffers. It suppors the chained and ring
  846. * modes.
  847. */
  848. static int init_dma_desc_rings(struct net_device *dev)
  849. {
  850. int i;
  851. struct stmmac_priv *priv = netdev_priv(dev);
  852. unsigned int txsize = priv->dma_tx_size;
  853. unsigned int rxsize = priv->dma_rx_size;
  854. unsigned int bfsize = 0;
  855. int ret = -ENOMEM;
  856. /* Set the max buffer size according to the DESC mode
  857. * and the MTU. Note that RING mode allows 16KiB bsize.
  858. */
  859. if (priv->mode == STMMAC_RING_MODE)
  860. bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
  861. if (bfsize < BUF_SIZE_16KiB)
  862. bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
  863. if (netif_msg_probe(priv))
  864. pr_debug("%s: txsize %d, rxsize %d, bfsize %d\n", __func__,
  865. txsize, rxsize, bfsize);
  866. if (priv->extend_desc) {
  867. priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
  868. sizeof(struct
  869. dma_extended_desc),
  870. &priv->dma_rx_phy,
  871. GFP_KERNEL);
  872. if (!priv->dma_erx)
  873. goto err_dma;
  874. priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
  875. sizeof(struct
  876. dma_extended_desc),
  877. &priv->dma_tx_phy,
  878. GFP_KERNEL);
  879. if (!priv->dma_etx) {
  880. dma_free_coherent(priv->device, priv->dma_rx_size *
  881. sizeof(struct dma_extended_desc),
  882. priv->dma_erx, priv->dma_rx_phy);
  883. goto err_dma;
  884. }
  885. } else {
  886. priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
  887. sizeof(struct dma_desc),
  888. &priv->dma_rx_phy,
  889. GFP_KERNEL);
  890. if (!priv->dma_rx)
  891. goto err_dma;
  892. priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
  893. sizeof(struct dma_desc),
  894. &priv->dma_tx_phy,
  895. GFP_KERNEL);
  896. if (!priv->dma_tx) {
  897. dma_free_coherent(priv->device, priv->dma_rx_size *
  898. sizeof(struct dma_desc),
  899. priv->dma_rx, priv->dma_rx_phy);
  900. goto err_dma;
  901. }
  902. }
  903. priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
  904. GFP_KERNEL);
  905. if (!priv->rx_skbuff_dma)
  906. goto err_rx_skbuff_dma;
  907. priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
  908. GFP_KERNEL);
  909. if (!priv->rx_skbuff)
  910. goto err_rx_skbuff;
  911. priv->tx_skbuff_dma = kmalloc_array(txsize, sizeof(dma_addr_t),
  912. GFP_KERNEL);
  913. if (!priv->tx_skbuff_dma)
  914. goto err_tx_skbuff_dma;
  915. priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
  916. GFP_KERNEL);
  917. if (!priv->tx_skbuff)
  918. goto err_tx_skbuff;
  919. if (netif_msg_probe(priv)) {
  920. pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
  921. (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
  922. /* RX INITIALIZATION */
  923. pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
  924. }
  925. for (i = 0; i < rxsize; i++) {
  926. struct dma_desc *p;
  927. if (priv->extend_desc)
  928. p = &((priv->dma_erx + i)->basic);
  929. else
  930. p = priv->dma_rx + i;
  931. ret = stmmac_init_rx_buffers(priv, p, i);
  932. if (ret)
  933. goto err_init_rx_buffers;
  934. if (netif_msg_probe(priv))
  935. pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
  936. priv->rx_skbuff[i]->data,
  937. (unsigned int)priv->rx_skbuff_dma[i]);
  938. }
  939. priv->cur_rx = 0;
  940. priv->dirty_rx = (unsigned int)(i - rxsize);
  941. priv->dma_buf_sz = bfsize;
  942. buf_sz = bfsize;
  943. /* Setup the chained descriptor addresses */
  944. if (priv->mode == STMMAC_CHAIN_MODE) {
  945. if (priv->extend_desc) {
  946. priv->hw->chain->init(priv->dma_erx, priv->dma_rx_phy,
  947. rxsize, 1);
  948. priv->hw->chain->init(priv->dma_etx, priv->dma_tx_phy,
  949. txsize, 1);
  950. } else {
  951. priv->hw->chain->init(priv->dma_rx, priv->dma_rx_phy,
  952. rxsize, 0);
  953. priv->hw->chain->init(priv->dma_tx, priv->dma_tx_phy,
  954. txsize, 0);
  955. }
  956. }
  957. /* TX INITIALIZATION */
  958. for (i = 0; i < txsize; i++) {
  959. struct dma_desc *p;
  960. if (priv->extend_desc)
  961. p = &((priv->dma_etx + i)->basic);
  962. else
  963. p = priv->dma_tx + i;
  964. p->des2 = 0;
  965. priv->tx_skbuff_dma[i] = 0;
  966. priv->tx_skbuff[i] = NULL;
  967. }
  968. priv->dirty_tx = 0;
  969. priv->cur_tx = 0;
  970. stmmac_clear_descriptors(priv);
  971. if (netif_msg_hw(priv))
  972. stmmac_display_rings(priv);
  973. return 0;
  974. err_init_rx_buffers:
  975. while (--i >= 0)
  976. stmmac_free_rx_buffers(priv, i);
  977. kfree(priv->tx_skbuff);
  978. err_tx_skbuff:
  979. kfree(priv->tx_skbuff_dma);
  980. err_tx_skbuff_dma:
  981. kfree(priv->rx_skbuff);
  982. err_rx_skbuff:
  983. kfree(priv->rx_skbuff_dma);
  984. err_rx_skbuff_dma:
  985. if (priv->extend_desc) {
  986. dma_free_coherent(priv->device, priv->dma_tx_size *
  987. sizeof(struct dma_extended_desc),
  988. priv->dma_etx, priv->dma_tx_phy);
  989. dma_free_coherent(priv->device, priv->dma_rx_size *
  990. sizeof(struct dma_extended_desc),
  991. priv->dma_erx, priv->dma_rx_phy);
  992. } else {
  993. dma_free_coherent(priv->device,
  994. priv->dma_tx_size * sizeof(struct dma_desc),
  995. priv->dma_tx, priv->dma_tx_phy);
  996. dma_free_coherent(priv->device,
  997. priv->dma_rx_size * sizeof(struct dma_desc),
  998. priv->dma_rx, priv->dma_rx_phy);
  999. }
  1000. err_dma:
  1001. return ret;
  1002. }
  1003. static void dma_free_rx_skbufs(struct stmmac_priv *priv)
  1004. {
  1005. int i;
  1006. for (i = 0; i < priv->dma_rx_size; i++)
  1007. stmmac_free_rx_buffers(priv, i);
  1008. }
  1009. static void dma_free_tx_skbufs(struct stmmac_priv *priv)
  1010. {
  1011. int i;
  1012. for (i = 0; i < priv->dma_tx_size; i++) {
  1013. if (priv->tx_skbuff[i] != NULL) {
  1014. struct dma_desc *p;
  1015. if (priv->extend_desc)
  1016. p = &((priv->dma_etx + i)->basic);
  1017. else
  1018. p = priv->dma_tx + i;
  1019. if (priv->tx_skbuff_dma[i])
  1020. dma_unmap_single(priv->device,
  1021. priv->tx_skbuff_dma[i],
  1022. priv->hw->desc->get_tx_len(p),
  1023. DMA_TO_DEVICE);
  1024. dev_kfree_skb_any(priv->tx_skbuff[i]);
  1025. priv->tx_skbuff[i] = NULL;
  1026. priv->tx_skbuff_dma[i] = 0;
  1027. }
  1028. }
  1029. }
  1030. static void free_dma_desc_resources(struct stmmac_priv *priv)
  1031. {
  1032. /* Release the DMA TX/RX socket buffers */
  1033. dma_free_rx_skbufs(priv);
  1034. dma_free_tx_skbufs(priv);
  1035. /* Free DMA regions of consistent memory previously allocated */
  1036. if (!priv->extend_desc) {
  1037. dma_free_coherent(priv->device,
  1038. priv->dma_tx_size * sizeof(struct dma_desc),
  1039. priv->dma_tx, priv->dma_tx_phy);
  1040. dma_free_coherent(priv->device,
  1041. priv->dma_rx_size * sizeof(struct dma_desc),
  1042. priv->dma_rx, priv->dma_rx_phy);
  1043. } else {
  1044. dma_free_coherent(priv->device, priv->dma_tx_size *
  1045. sizeof(struct dma_extended_desc),
  1046. priv->dma_etx, priv->dma_tx_phy);
  1047. dma_free_coherent(priv->device, priv->dma_rx_size *
  1048. sizeof(struct dma_extended_desc),
  1049. priv->dma_erx, priv->dma_rx_phy);
  1050. }
  1051. kfree(priv->rx_skbuff_dma);
  1052. kfree(priv->rx_skbuff);
  1053. kfree(priv->tx_skbuff_dma);
  1054. kfree(priv->tx_skbuff);
  1055. }
  1056. /**
  1057. * stmmac_dma_operation_mode - HW DMA operation mode
  1058. * @priv: driver private structure
  1059. * Description: it sets the DMA operation mode: tx/rx DMA thresholds
  1060. * or Store-And-Forward capability.
  1061. */
  1062. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  1063. {
  1064. if (priv->plat->force_thresh_dma_mode)
  1065. priv->hw->dma->dma_mode(priv->ioaddr, tc, tc);
  1066. else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
  1067. /*
  1068. * In case of GMAC, SF mode can be enabled
  1069. * to perform the TX COE in HW. This depends on:
  1070. * 1) TX COE if actually supported
  1071. * 2) There is no bugged Jumbo frame support
  1072. * that needs to not insert csum in the TDES.
  1073. */
  1074. priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE);
  1075. tc = SF_DMA_MODE;
  1076. } else
  1077. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  1078. }
  1079. /**
  1080. * stmmac_tx_clean:
  1081. * @priv: driver private structure
  1082. * Description: it reclaims resources after transmission completes.
  1083. */
  1084. static void stmmac_tx_clean(struct stmmac_priv *priv)
  1085. {
  1086. unsigned int txsize = priv->dma_tx_size;
  1087. spin_lock(&priv->tx_lock);
  1088. priv->xstats.tx_clean++;
  1089. while (priv->dirty_tx != priv->cur_tx) {
  1090. int last;
  1091. unsigned int entry = priv->dirty_tx % txsize;
  1092. struct sk_buff *skb = priv->tx_skbuff[entry];
  1093. struct dma_desc *p;
  1094. if (priv->extend_desc)
  1095. p = (struct dma_desc *)(priv->dma_etx + entry);
  1096. else
  1097. p = priv->dma_tx + entry;
  1098. /* Check if the descriptor is owned by the DMA. */
  1099. if (priv->hw->desc->get_tx_owner(p))
  1100. break;
  1101. /* Verify tx error by looking at the last segment. */
  1102. last = priv->hw->desc->get_tx_ls(p);
  1103. if (likely(last)) {
  1104. int tx_error =
  1105. priv->hw->desc->tx_status(&priv->dev->stats,
  1106. &priv->xstats, p,
  1107. priv->ioaddr);
  1108. if (likely(tx_error == 0)) {
  1109. priv->dev->stats.tx_packets++;
  1110. priv->xstats.tx_pkt_n++;
  1111. } else
  1112. priv->dev->stats.tx_errors++;
  1113. stmmac_get_tx_hwtstamp(priv, entry, skb);
  1114. }
  1115. if (netif_msg_tx_done(priv))
  1116. pr_debug("%s: curr %d, dirty %d\n", __func__,
  1117. priv->cur_tx, priv->dirty_tx);
  1118. if (likely(priv->tx_skbuff_dma[entry])) {
  1119. dma_unmap_single(priv->device,
  1120. priv->tx_skbuff_dma[entry],
  1121. priv->hw->desc->get_tx_len(p),
  1122. DMA_TO_DEVICE);
  1123. priv->tx_skbuff_dma[entry] = 0;
  1124. }
  1125. priv->hw->ring->clean_desc3(priv, p);
  1126. if (likely(skb != NULL)) {
  1127. dev_kfree_skb(skb);
  1128. priv->tx_skbuff[entry] = NULL;
  1129. }
  1130. priv->hw->desc->release_tx_desc(p, priv->mode);
  1131. priv->dirty_tx++;
  1132. }
  1133. if (unlikely(netif_queue_stopped(priv->dev) &&
  1134. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
  1135. netif_tx_lock(priv->dev);
  1136. if (netif_queue_stopped(priv->dev) &&
  1137. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
  1138. if (netif_msg_tx_done(priv))
  1139. pr_debug("%s: restart transmit\n", __func__);
  1140. netif_wake_queue(priv->dev);
  1141. }
  1142. netif_tx_unlock(priv->dev);
  1143. }
  1144. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  1145. stmmac_enable_eee_mode(priv);
  1146. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
  1147. }
  1148. spin_unlock(&priv->tx_lock);
  1149. }
  1150. static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
  1151. {
  1152. priv->hw->dma->enable_dma_irq(priv->ioaddr);
  1153. }
  1154. static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
  1155. {
  1156. priv->hw->dma->disable_dma_irq(priv->ioaddr);
  1157. }
  1158. /**
  1159. * stmmac_tx_err: irq tx error mng function
  1160. * @priv: driver private structure
  1161. * Description: it cleans the descriptors and restarts the transmission
  1162. * in case of errors.
  1163. */
  1164. static void stmmac_tx_err(struct stmmac_priv *priv)
  1165. {
  1166. int i;
  1167. int txsize = priv->dma_tx_size;
  1168. netif_stop_queue(priv->dev);
  1169. priv->hw->dma->stop_tx(priv->ioaddr);
  1170. dma_free_tx_skbufs(priv);
  1171. for (i = 0; i < txsize; i++)
  1172. if (priv->extend_desc)
  1173. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  1174. priv->mode,
  1175. (i == txsize - 1));
  1176. else
  1177. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  1178. priv->mode,
  1179. (i == txsize - 1));
  1180. priv->dirty_tx = 0;
  1181. priv->cur_tx = 0;
  1182. priv->hw->dma->start_tx(priv->ioaddr);
  1183. priv->dev->stats.tx_errors++;
  1184. netif_wake_queue(priv->dev);
  1185. }
  1186. /**
  1187. * stmmac_dma_interrupt: DMA ISR
  1188. * @priv: driver private structure
  1189. * Description: this is the DMA ISR. It is called by the main ISR.
  1190. * It calls the dwmac dma routine to understand which type of interrupt
  1191. * happened. In case of there is a Normal interrupt and either TX or RX
  1192. * interrupt happened so the NAPI is scheduled.
  1193. */
  1194. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  1195. {
  1196. int status;
  1197. status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
  1198. if (likely((status & handle_rx)) || (status & handle_tx)) {
  1199. if (likely(napi_schedule_prep(&priv->napi))) {
  1200. stmmac_disable_dma_irq(priv);
  1201. __napi_schedule(&priv->napi);
  1202. }
  1203. }
  1204. if (unlikely(status & tx_hard_error_bump_tc)) {
  1205. /* Try to bump up the dma threshold on this failure */
  1206. if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
  1207. tc += 64;
  1208. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  1209. priv->xstats.threshold = tc;
  1210. }
  1211. } else if (unlikely(status == tx_hard_error))
  1212. stmmac_tx_err(priv);
  1213. }
  1214. /**
  1215. * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
  1216. * @priv: driver private structure
  1217. * Description: this masks the MMC irq, in fact, the counters are managed in SW.
  1218. */
  1219. static void stmmac_mmc_setup(struct stmmac_priv *priv)
  1220. {
  1221. unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
  1222. MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
  1223. dwmac_mmc_intr_all_mask(priv->ioaddr);
  1224. if (priv->dma_cap.rmon) {
  1225. dwmac_mmc_ctrl(priv->ioaddr, mode);
  1226. memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
  1227. } else
  1228. pr_info(" No MAC Management Counters available\n");
  1229. }
  1230. static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
  1231. {
  1232. u32 hwid = priv->hw->synopsys_uid;
  1233. /* Check Synopsys Id (not available on old chips) */
  1234. if (likely(hwid)) {
  1235. u32 uid = ((hwid & 0x0000ff00) >> 8);
  1236. u32 synid = (hwid & 0x000000ff);
  1237. pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
  1238. uid, synid);
  1239. return synid;
  1240. }
  1241. return 0;
  1242. }
  1243. /**
  1244. * stmmac_selec_desc_mode: to select among: normal/alternate/extend descriptors
  1245. * @priv: driver private structure
  1246. * Description: select the Enhanced/Alternate or Normal descriptors.
  1247. * In case of Enhanced/Alternate, it looks at the extended descriptors are
  1248. * supported by the HW cap. register.
  1249. */
  1250. static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
  1251. {
  1252. if (priv->plat->enh_desc) {
  1253. pr_info(" Enhanced/Alternate descriptors\n");
  1254. /* GMAC older than 3.50 has no extended descriptors */
  1255. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  1256. pr_info("\tEnabled extended descriptors\n");
  1257. priv->extend_desc = 1;
  1258. } else
  1259. pr_warn("Extended descriptors not supported\n");
  1260. priv->hw->desc = &enh_desc_ops;
  1261. } else {
  1262. pr_info(" Normal descriptors\n");
  1263. priv->hw->desc = &ndesc_ops;
  1264. }
  1265. }
  1266. /**
  1267. * stmmac_get_hw_features: get MAC capabilities from the HW cap. register.
  1268. * @priv: driver private structure
  1269. * Description:
  1270. * new GMAC chip generations have a new register to indicate the
  1271. * presence of the optional feature/functions.
  1272. * This can be also used to override the value passed through the
  1273. * platform and necessary for old MAC10/100 and GMAC chips.
  1274. */
  1275. static int stmmac_get_hw_features(struct stmmac_priv *priv)
  1276. {
  1277. u32 hw_cap = 0;
  1278. if (priv->hw->dma->get_hw_feature) {
  1279. hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
  1280. priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
  1281. priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
  1282. priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
  1283. priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
  1284. priv->dma_cap.multi_addr = (hw_cap & DMA_HW_FEAT_ADDMAC) >> 5;
  1285. priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
  1286. priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
  1287. priv->dma_cap.pmt_remote_wake_up =
  1288. (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
  1289. priv->dma_cap.pmt_magic_frame =
  1290. (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
  1291. /* MMC */
  1292. priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
  1293. /* IEEE 1588-2002 */
  1294. priv->dma_cap.time_stamp =
  1295. (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
  1296. /* IEEE 1588-2008 */
  1297. priv->dma_cap.atime_stamp =
  1298. (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
  1299. /* 802.3az - Energy-Efficient Ethernet (EEE) */
  1300. priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
  1301. priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
  1302. /* TX and RX csum */
  1303. priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
  1304. priv->dma_cap.rx_coe_type1 =
  1305. (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
  1306. priv->dma_cap.rx_coe_type2 =
  1307. (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
  1308. priv->dma_cap.rxfifo_over_2048 =
  1309. (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
  1310. /* TX and RX number of channels */
  1311. priv->dma_cap.number_rx_channel =
  1312. (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
  1313. priv->dma_cap.number_tx_channel =
  1314. (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
  1315. /* Alternate (enhanced) DESC mode */
  1316. priv->dma_cap.enh_desc = (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
  1317. }
  1318. return hw_cap;
  1319. }
  1320. /**
  1321. * stmmac_check_ether_addr: check if the MAC addr is valid
  1322. * @priv: driver private structure
  1323. * Description:
  1324. * it is to verify if the MAC address is valid, in case of failures it
  1325. * generates a random MAC address
  1326. */
  1327. static void stmmac_check_ether_addr(struct stmmac_priv *priv)
  1328. {
  1329. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  1330. priv->hw->mac->get_umac_addr((void __iomem *)
  1331. priv->dev->base_addr,
  1332. priv->dev->dev_addr, 0);
  1333. if (!is_valid_ether_addr(priv->dev->dev_addr))
  1334. eth_hw_addr_random(priv->dev);
  1335. }
  1336. pr_warn("%s: device MAC address %pM\n", priv->dev->name,
  1337. priv->dev->dev_addr);
  1338. }
  1339. /**
  1340. * stmmac_init_dma_engine: DMA init.
  1341. * @priv: driver private structure
  1342. * Description:
  1343. * It inits the DMA invoking the specific MAC/GMAC callback.
  1344. * Some DMA parameters can be passed from the platform;
  1345. * in case of these are not passed a default is kept for the MAC or GMAC.
  1346. */
  1347. static int stmmac_init_dma_engine(struct stmmac_priv *priv)
  1348. {
  1349. int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
  1350. int mixed_burst = 0;
  1351. int atds = 0;
  1352. if (priv->plat->dma_cfg) {
  1353. pbl = priv->plat->dma_cfg->pbl;
  1354. fixed_burst = priv->plat->dma_cfg->fixed_burst;
  1355. mixed_burst = priv->plat->dma_cfg->mixed_burst;
  1356. burst_len = priv->plat->dma_cfg->burst_len;
  1357. }
  1358. if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
  1359. atds = 1;
  1360. return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
  1361. burst_len, priv->dma_tx_phy,
  1362. priv->dma_rx_phy, atds);
  1363. }
  1364. /**
  1365. * stmmac_tx_timer: mitigation sw timer for tx.
  1366. * @data: data pointer
  1367. * Description:
  1368. * This is the timer handler to directly invoke the stmmac_tx_clean.
  1369. */
  1370. static void stmmac_tx_timer(unsigned long data)
  1371. {
  1372. struct stmmac_priv *priv = (struct stmmac_priv *)data;
  1373. stmmac_tx_clean(priv);
  1374. }
  1375. /**
  1376. * stmmac_init_tx_coalesce: init tx mitigation options.
  1377. * @priv: driver private structure
  1378. * Description:
  1379. * This inits the transmit coalesce parameters: i.e. timer rate,
  1380. * timer handler and default threshold used for enabling the
  1381. * interrupt on completion bit.
  1382. */
  1383. static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
  1384. {
  1385. priv->tx_coal_frames = STMMAC_TX_FRAMES;
  1386. priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
  1387. init_timer(&priv->txtimer);
  1388. priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
  1389. priv->txtimer.data = (unsigned long)priv;
  1390. priv->txtimer.function = stmmac_tx_timer;
  1391. add_timer(&priv->txtimer);
  1392. }
  1393. /**
  1394. * stmmac_open - open entry point of the driver
  1395. * @dev : pointer to the device structure.
  1396. * Description:
  1397. * This function is the open entry point of the driver.
  1398. * Return value:
  1399. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1400. * file on failure.
  1401. */
  1402. static int stmmac_open(struct net_device *dev)
  1403. {
  1404. struct stmmac_priv *priv = netdev_priv(dev);
  1405. int ret;
  1406. clk_prepare_enable(priv->stmmac_clk);
  1407. stmmac_check_ether_addr(priv);
  1408. if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
  1409. priv->pcs != STMMAC_PCS_RTBI) {
  1410. ret = stmmac_init_phy(dev);
  1411. if (ret) {
  1412. pr_err("%s: Cannot attach to PHY (error: %d)\n",
  1413. __func__, ret);
  1414. goto phy_error;
  1415. }
  1416. }
  1417. /* Create and initialize the TX/RX descriptors chains. */
  1418. priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
  1419. priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
  1420. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  1421. ret = init_dma_desc_rings(dev);
  1422. if (ret < 0) {
  1423. pr_err("%s: DMA descriptors initialization failed\n", __func__);
  1424. goto dma_desc_error;
  1425. }
  1426. /* DMA initialization and SW reset */
  1427. ret = stmmac_init_dma_engine(priv);
  1428. if (ret < 0) {
  1429. pr_err("%s: DMA engine initialization failed\n", __func__);
  1430. goto init_error;
  1431. }
  1432. /* Copy the MAC addr into the HW */
  1433. priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
  1434. /* If required, perform hw setup of the bus. */
  1435. if (priv->plat->bus_setup)
  1436. priv->plat->bus_setup(priv->ioaddr);
  1437. /* Initialize the MAC Core */
  1438. priv->hw->mac->core_init(priv->ioaddr);
  1439. /* Request the IRQ lines */
  1440. ret = request_irq(dev->irq, stmmac_interrupt,
  1441. IRQF_SHARED, dev->name, dev);
  1442. if (unlikely(ret < 0)) {
  1443. pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
  1444. __func__, dev->irq, ret);
  1445. goto init_error;
  1446. }
  1447. /* Request the Wake IRQ in case of another line is used for WoL */
  1448. if (priv->wol_irq != dev->irq) {
  1449. ret = request_irq(priv->wol_irq, stmmac_interrupt,
  1450. IRQF_SHARED, dev->name, dev);
  1451. if (unlikely(ret < 0)) {
  1452. pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
  1453. __func__, priv->wol_irq, ret);
  1454. goto wolirq_error;
  1455. }
  1456. }
  1457. /* Request the IRQ lines */
  1458. if (priv->lpi_irq != -ENXIO) {
  1459. ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
  1460. dev->name, dev);
  1461. if (unlikely(ret < 0)) {
  1462. pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  1463. __func__, priv->lpi_irq, ret);
  1464. goto lpiirq_error;
  1465. }
  1466. }
  1467. /* Enable the MAC Rx/Tx */
  1468. stmmac_set_mac(priv->ioaddr, true);
  1469. /* Set the HW DMA mode and the COE */
  1470. stmmac_dma_operation_mode(priv);
  1471. /* Extra statistics */
  1472. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  1473. priv->xstats.threshold = tc;
  1474. stmmac_mmc_setup(priv);
  1475. ret = stmmac_init_ptp(priv);
  1476. if (ret)
  1477. pr_warn("%s: failed PTP initialisation\n", __func__);
  1478. #ifdef CONFIG_STMMAC_DEBUG_FS
  1479. ret = stmmac_init_fs(dev);
  1480. if (ret < 0)
  1481. pr_warn("%s: failed debugFS registration\n", __func__);
  1482. #endif
  1483. /* Start the ball rolling... */
  1484. pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
  1485. priv->hw->dma->start_tx(priv->ioaddr);
  1486. priv->hw->dma->start_rx(priv->ioaddr);
  1487. /* Dump DMA/MAC registers */
  1488. if (netif_msg_hw(priv)) {
  1489. priv->hw->mac->dump_regs(priv->ioaddr);
  1490. priv->hw->dma->dump_regs(priv->ioaddr);
  1491. }
  1492. if (priv->phydev)
  1493. phy_start(priv->phydev);
  1494. priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
  1495. priv->eee_enabled = stmmac_eee_init(priv);
  1496. stmmac_init_tx_coalesce(priv);
  1497. if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
  1498. priv->rx_riwt = MAX_DMA_RIWT;
  1499. priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
  1500. }
  1501. if (priv->pcs && priv->hw->mac->ctrl_ane)
  1502. priv->hw->mac->ctrl_ane(priv->ioaddr, 0);
  1503. napi_enable(&priv->napi);
  1504. netif_start_queue(dev);
  1505. return 0;
  1506. lpiirq_error:
  1507. if (priv->wol_irq != dev->irq)
  1508. free_irq(priv->wol_irq, dev);
  1509. wolirq_error:
  1510. free_irq(dev->irq, dev);
  1511. init_error:
  1512. free_dma_desc_resources(priv);
  1513. dma_desc_error:
  1514. if (priv->phydev)
  1515. phy_disconnect(priv->phydev);
  1516. phy_error:
  1517. clk_disable_unprepare(priv->stmmac_clk);
  1518. return ret;
  1519. }
  1520. /**
  1521. * stmmac_release - close entry point of the driver
  1522. * @dev : device pointer.
  1523. * Description:
  1524. * This is the stop entry point of the driver.
  1525. */
  1526. static int stmmac_release(struct net_device *dev)
  1527. {
  1528. struct stmmac_priv *priv = netdev_priv(dev);
  1529. if (priv->eee_enabled)
  1530. del_timer_sync(&priv->eee_ctrl_timer);
  1531. /* Stop and disconnect the PHY */
  1532. if (priv->phydev) {
  1533. phy_stop(priv->phydev);
  1534. phy_disconnect(priv->phydev);
  1535. priv->phydev = NULL;
  1536. }
  1537. netif_stop_queue(dev);
  1538. napi_disable(&priv->napi);
  1539. del_timer_sync(&priv->txtimer);
  1540. /* Free the IRQ lines */
  1541. free_irq(dev->irq, dev);
  1542. if (priv->wol_irq != dev->irq)
  1543. free_irq(priv->wol_irq, dev);
  1544. if (priv->lpi_irq != -ENXIO)
  1545. free_irq(priv->lpi_irq, dev);
  1546. /* Stop TX/RX DMA and clear the descriptors */
  1547. priv->hw->dma->stop_tx(priv->ioaddr);
  1548. priv->hw->dma->stop_rx(priv->ioaddr);
  1549. /* Release and free the Rx/Tx resources */
  1550. free_dma_desc_resources(priv);
  1551. /* Disable the MAC Rx/Tx */
  1552. stmmac_set_mac(priv->ioaddr, false);
  1553. netif_carrier_off(dev);
  1554. #ifdef CONFIG_STMMAC_DEBUG_FS
  1555. stmmac_exit_fs();
  1556. #endif
  1557. clk_disable_unprepare(priv->stmmac_clk);
  1558. stmmac_release_ptp(priv);
  1559. return 0;
  1560. }
  1561. /**
  1562. * stmmac_xmit: Tx entry point of the driver
  1563. * @skb : the socket buffer
  1564. * @dev : device pointer
  1565. * Description : this is the tx entry point of the driver.
  1566. * It programs the chain or the ring and supports oversized frames
  1567. * and SG feature.
  1568. */
  1569. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  1570. {
  1571. struct stmmac_priv *priv = netdev_priv(dev);
  1572. unsigned int txsize = priv->dma_tx_size;
  1573. unsigned int entry;
  1574. int i, csum_insertion = 0, is_jumbo = 0;
  1575. int nfrags = skb_shinfo(skb)->nr_frags;
  1576. struct dma_desc *desc, *first;
  1577. unsigned int nopaged_len = skb_headlen(skb);
  1578. if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
  1579. if (!netif_queue_stopped(dev)) {
  1580. netif_stop_queue(dev);
  1581. /* This is a hard error, log it. */
  1582. pr_err("%s: Tx Ring full when queue awake\n", __func__);
  1583. }
  1584. return NETDEV_TX_BUSY;
  1585. }
  1586. spin_lock(&priv->tx_lock);
  1587. if (priv->tx_path_in_lpi_mode)
  1588. stmmac_disable_eee_mode(priv);
  1589. entry = priv->cur_tx % txsize;
  1590. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  1591. if (priv->extend_desc)
  1592. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1593. else
  1594. desc = priv->dma_tx + entry;
  1595. first = desc;
  1596. priv->tx_skbuff[entry] = skb;
  1597. /* To program the descriptors according to the size of the frame */
  1598. if (priv->mode == STMMAC_RING_MODE) {
  1599. is_jumbo = priv->hw->ring->is_jumbo_frm(skb->len,
  1600. priv->plat->enh_desc);
  1601. if (unlikely(is_jumbo))
  1602. entry = priv->hw->ring->jumbo_frm(priv, skb,
  1603. csum_insertion);
  1604. } else {
  1605. is_jumbo = priv->hw->chain->is_jumbo_frm(skb->len,
  1606. priv->plat->enh_desc);
  1607. if (unlikely(is_jumbo))
  1608. entry = priv->hw->chain->jumbo_frm(priv, skb,
  1609. csum_insertion);
  1610. }
  1611. if (likely(!is_jumbo)) {
  1612. desc->des2 = dma_map_single(priv->device, skb->data,
  1613. nopaged_len, DMA_TO_DEVICE);
  1614. priv->tx_skbuff_dma[entry] = desc->des2;
  1615. priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
  1616. csum_insertion, priv->mode);
  1617. } else
  1618. desc = first;
  1619. for (i = 0; i < nfrags; i++) {
  1620. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1621. int len = skb_frag_size(frag);
  1622. entry = (++priv->cur_tx) % txsize;
  1623. if (priv->extend_desc)
  1624. desc = (struct dma_desc *)(priv->dma_etx + entry);
  1625. else
  1626. desc = priv->dma_tx + entry;
  1627. desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
  1628. DMA_TO_DEVICE);
  1629. priv->tx_skbuff_dma[entry] = desc->des2;
  1630. priv->tx_skbuff[entry] = NULL;
  1631. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
  1632. priv->mode);
  1633. wmb();
  1634. priv->hw->desc->set_tx_owner(desc);
  1635. wmb();
  1636. }
  1637. /* Finalize the latest segment. */
  1638. priv->hw->desc->close_tx_desc(desc);
  1639. wmb();
  1640. /* According to the coalesce parameter the IC bit for the latest
  1641. * segment could be reset and the timer re-started to invoke the
  1642. * stmmac_tx function. This approach takes care about the fragments.
  1643. */
  1644. priv->tx_count_frames += nfrags + 1;
  1645. if (priv->tx_coal_frames > priv->tx_count_frames) {
  1646. priv->hw->desc->clear_tx_ic(desc);
  1647. priv->xstats.tx_reset_ic_bit++;
  1648. mod_timer(&priv->txtimer,
  1649. STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1650. } else
  1651. priv->tx_count_frames = 0;
  1652. /* To avoid raise condition */
  1653. priv->hw->desc->set_tx_owner(first);
  1654. wmb();
  1655. priv->cur_tx++;
  1656. if (netif_msg_pktdata(priv)) {
  1657. pr_debug("%s: curr %d dirty=%d entry=%d, first=%p, nfrags=%d",
  1658. __func__, (priv->cur_tx % txsize),
  1659. (priv->dirty_tx % txsize), entry, first, nfrags);
  1660. if (priv->extend_desc)
  1661. stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
  1662. else
  1663. stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
  1664. pr_debug(">>> frame to be transmitted: ");
  1665. print_pkt(skb->data, skb->len);
  1666. }
  1667. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  1668. if (netif_msg_hw(priv))
  1669. pr_debug("%s: stop transmitted packets\n", __func__);
  1670. netif_stop_queue(dev);
  1671. }
  1672. dev->stats.tx_bytes += skb->len;
  1673. if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1674. priv->hwts_tx_en)) {
  1675. /* declare that device is doing timestamping */
  1676. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1677. priv->hw->desc->enable_tx_timestamp(first);
  1678. }
  1679. if (!priv->hwts_tx_en)
  1680. skb_tx_timestamp(skb);
  1681. priv->hw->dma->enable_dma_transmission(priv->ioaddr);
  1682. spin_unlock(&priv->tx_lock);
  1683. return NETDEV_TX_OK;
  1684. }
  1685. /**
  1686. * stmmac_rx_refill: refill used skb preallocated buffers
  1687. * @priv: driver private structure
  1688. * Description : this is to reallocate the skb for the reception process
  1689. * that is based on zero-copy.
  1690. */
  1691. static inline void stmmac_rx_refill(struct stmmac_priv *priv)
  1692. {
  1693. unsigned int rxsize = priv->dma_rx_size;
  1694. int bfsize = priv->dma_buf_sz;
  1695. for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
  1696. unsigned int entry = priv->dirty_rx % rxsize;
  1697. struct dma_desc *p;
  1698. if (priv->extend_desc)
  1699. p = (struct dma_desc *)(priv->dma_erx + entry);
  1700. else
  1701. p = priv->dma_rx + entry;
  1702. if (likely(priv->rx_skbuff[entry] == NULL)) {
  1703. struct sk_buff *skb;
  1704. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  1705. if (unlikely(skb == NULL))
  1706. break;
  1707. priv->rx_skbuff[entry] = skb;
  1708. priv->rx_skbuff_dma[entry] =
  1709. dma_map_single(priv->device, skb->data, bfsize,
  1710. DMA_FROM_DEVICE);
  1711. p->des2 = priv->rx_skbuff_dma[entry];
  1712. priv->hw->ring->refill_desc3(priv, p);
  1713. if (netif_msg_rx_status(priv))
  1714. pr_debug("\trefill entry #%d\n", entry);
  1715. }
  1716. wmb();
  1717. priv->hw->desc->set_rx_owner(p);
  1718. wmb();
  1719. }
  1720. }
  1721. /**
  1722. * stmmac_rx_refill: refill used skb preallocated buffers
  1723. * @priv: driver private structure
  1724. * @limit: napi bugget.
  1725. * Description : this the function called by the napi poll method.
  1726. * It gets all the frames inside the ring.
  1727. */
  1728. static int stmmac_rx(struct stmmac_priv *priv, int limit)
  1729. {
  1730. unsigned int rxsize = priv->dma_rx_size;
  1731. unsigned int entry = priv->cur_rx % rxsize;
  1732. unsigned int next_entry;
  1733. unsigned int count = 0;
  1734. int coe = priv->plat->rx_coe;
  1735. if (netif_msg_rx_status(priv)) {
  1736. pr_debug("%s: descriptor ring:\n", __func__);
  1737. if (priv->extend_desc)
  1738. stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
  1739. else
  1740. stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
  1741. }
  1742. while (count < limit) {
  1743. int status;
  1744. struct dma_desc *p;
  1745. if (priv->extend_desc)
  1746. p = (struct dma_desc *)(priv->dma_erx + entry);
  1747. else
  1748. p = priv->dma_rx + entry;
  1749. if (priv->hw->desc->get_rx_owner(p))
  1750. break;
  1751. count++;
  1752. next_entry = (++priv->cur_rx) % rxsize;
  1753. if (priv->extend_desc)
  1754. prefetch(priv->dma_erx + next_entry);
  1755. else
  1756. prefetch(priv->dma_rx + next_entry);
  1757. /* read the status of the incoming frame */
  1758. status = priv->hw->desc->rx_status(&priv->dev->stats,
  1759. &priv->xstats, p);
  1760. if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
  1761. priv->hw->desc->rx_extended_status(&priv->dev->stats,
  1762. &priv->xstats,
  1763. priv->dma_erx +
  1764. entry);
  1765. if (unlikely(status == discard_frame)) {
  1766. priv->dev->stats.rx_errors++;
  1767. if (priv->hwts_rx_en && !priv->extend_desc) {
  1768. /* DESC2 & DESC3 will be overwitten by device
  1769. * with timestamp value, hence reinitialize
  1770. * them in stmmac_rx_refill() function so that
  1771. * device can reuse it.
  1772. */
  1773. priv->rx_skbuff[entry] = NULL;
  1774. dma_unmap_single(priv->device,
  1775. priv->rx_skbuff_dma[entry],
  1776. priv->dma_buf_sz,
  1777. DMA_FROM_DEVICE);
  1778. }
  1779. } else {
  1780. struct sk_buff *skb;
  1781. int frame_len;
  1782. frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
  1783. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  1784. * Type frames (LLC/LLC-SNAP)
  1785. */
  1786. if (unlikely(status != llc_snap))
  1787. frame_len -= ETH_FCS_LEN;
  1788. if (netif_msg_rx_status(priv)) {
  1789. pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
  1790. p, entry, p->des2);
  1791. if (frame_len > ETH_FRAME_LEN)
  1792. pr_debug("\tframe size %d, COE: %d\n",
  1793. frame_len, status);
  1794. }
  1795. skb = priv->rx_skbuff[entry];
  1796. if (unlikely(!skb)) {
  1797. pr_err("%s: Inconsistent Rx descriptor chain\n",
  1798. priv->dev->name);
  1799. priv->dev->stats.rx_dropped++;
  1800. break;
  1801. }
  1802. prefetch(skb->data - NET_IP_ALIGN);
  1803. priv->rx_skbuff[entry] = NULL;
  1804. stmmac_get_rx_hwtstamp(priv, entry, skb);
  1805. skb_put(skb, frame_len);
  1806. dma_unmap_single(priv->device,
  1807. priv->rx_skbuff_dma[entry],
  1808. priv->dma_buf_sz, DMA_FROM_DEVICE);
  1809. if (netif_msg_pktdata(priv)) {
  1810. pr_debug("frame received (%dbytes)", frame_len);
  1811. print_pkt(skb->data, frame_len);
  1812. }
  1813. skb->protocol = eth_type_trans(skb, priv->dev);
  1814. if (unlikely(!coe))
  1815. skb_checksum_none_assert(skb);
  1816. else
  1817. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1818. napi_gro_receive(&priv->napi, skb);
  1819. priv->dev->stats.rx_packets++;
  1820. priv->dev->stats.rx_bytes += frame_len;
  1821. }
  1822. entry = next_entry;
  1823. }
  1824. stmmac_rx_refill(priv);
  1825. priv->xstats.rx_pkt_n += count;
  1826. return count;
  1827. }
  1828. /**
  1829. * stmmac_poll - stmmac poll method (NAPI)
  1830. * @napi : pointer to the napi structure.
  1831. * @budget : maximum number of packets that the current CPU can receive from
  1832. * all interfaces.
  1833. * Description :
  1834. * To look at the incoming frames and clear the tx resources.
  1835. */
  1836. static int stmmac_poll(struct napi_struct *napi, int budget)
  1837. {
  1838. struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
  1839. int work_done = 0;
  1840. priv->xstats.napi_poll++;
  1841. stmmac_tx_clean(priv);
  1842. work_done = stmmac_rx(priv, budget);
  1843. if (work_done < budget) {
  1844. napi_complete(napi);
  1845. stmmac_enable_dma_irq(priv);
  1846. }
  1847. return work_done;
  1848. }
  1849. /**
  1850. * stmmac_tx_timeout
  1851. * @dev : Pointer to net device structure
  1852. * Description: this function is called when a packet transmission fails to
  1853. * complete within a reasonable time. The driver will mark the error in the
  1854. * netdev structure and arrange for the device to be reset to a sane state
  1855. * in order to transmit a new packet.
  1856. */
  1857. static void stmmac_tx_timeout(struct net_device *dev)
  1858. {
  1859. struct stmmac_priv *priv = netdev_priv(dev);
  1860. /* Clear Tx resources and restart transmitting again */
  1861. stmmac_tx_err(priv);
  1862. }
  1863. /* Configuration changes (passed on by ifconfig) */
  1864. static int stmmac_config(struct net_device *dev, struct ifmap *map)
  1865. {
  1866. if (dev->flags & IFF_UP) /* can't act on a running interface */
  1867. return -EBUSY;
  1868. /* Don't allow changing the I/O address */
  1869. if (map->base_addr != dev->base_addr) {
  1870. pr_warn("%s: can't change I/O address\n", dev->name);
  1871. return -EOPNOTSUPP;
  1872. }
  1873. /* Don't allow changing the IRQ */
  1874. if (map->irq != dev->irq) {
  1875. pr_warn("%s: not change IRQ number %d\n", dev->name, dev->irq);
  1876. return -EOPNOTSUPP;
  1877. }
  1878. return 0;
  1879. }
  1880. /**
  1881. * stmmac_set_rx_mode - entry point for multicast addressing
  1882. * @dev : pointer to the device structure
  1883. * Description:
  1884. * This function is a driver entry point which gets called by the kernel
  1885. * whenever multicast addresses must be enabled/disabled.
  1886. * Return value:
  1887. * void.
  1888. */
  1889. static void stmmac_set_rx_mode(struct net_device *dev)
  1890. {
  1891. struct stmmac_priv *priv = netdev_priv(dev);
  1892. spin_lock(&priv->lock);
  1893. priv->hw->mac->set_filter(dev, priv->synopsys_id);
  1894. spin_unlock(&priv->lock);
  1895. }
  1896. /**
  1897. * stmmac_change_mtu - entry point to change MTU size for the device.
  1898. * @dev : device pointer.
  1899. * @new_mtu : the new MTU size for the device.
  1900. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1901. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1902. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1903. * Return value:
  1904. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1905. * file on failure.
  1906. */
  1907. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  1908. {
  1909. struct stmmac_priv *priv = netdev_priv(dev);
  1910. int max_mtu;
  1911. if (netif_running(dev)) {
  1912. pr_err("%s: must be stopped to change its MTU\n", dev->name);
  1913. return -EBUSY;
  1914. }
  1915. if (priv->plat->enh_desc)
  1916. max_mtu = JUMBO_LEN;
  1917. else
  1918. max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  1919. if ((new_mtu < 46) || (new_mtu > max_mtu)) {
  1920. pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
  1921. return -EINVAL;
  1922. }
  1923. dev->mtu = new_mtu;
  1924. netdev_update_features(dev);
  1925. return 0;
  1926. }
  1927. static netdev_features_t stmmac_fix_features(struct net_device *dev,
  1928. netdev_features_t features)
  1929. {
  1930. struct stmmac_priv *priv = netdev_priv(dev);
  1931. if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
  1932. features &= ~NETIF_F_RXCSUM;
  1933. else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
  1934. features &= ~NETIF_F_IPV6_CSUM;
  1935. if (!priv->plat->tx_coe)
  1936. features &= ~NETIF_F_ALL_CSUM;
  1937. /* Some GMAC devices have a bugged Jumbo frame support that
  1938. * needs to have the Tx COE disabled for oversized frames
  1939. * (due to limited buffer sizes). In this case we disable
  1940. * the TX csum insertionin the TDES and not use SF.
  1941. */
  1942. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  1943. features &= ~NETIF_F_ALL_CSUM;
  1944. return features;
  1945. }
  1946. /**
  1947. * stmmac_interrupt - main ISR
  1948. * @irq: interrupt number.
  1949. * @dev_id: to pass the net device pointer.
  1950. * Description: this is the main driver interrupt service routine.
  1951. * It calls the DMA ISR and also the core ISR to manage PMT, MMC, LPI
  1952. * interrupts.
  1953. */
  1954. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  1955. {
  1956. struct net_device *dev = (struct net_device *)dev_id;
  1957. struct stmmac_priv *priv = netdev_priv(dev);
  1958. if (unlikely(!dev)) {
  1959. pr_err("%s: invalid dev pointer\n", __func__);
  1960. return IRQ_NONE;
  1961. }
  1962. /* To handle GMAC own interrupts */
  1963. if (priv->plat->has_gmac) {
  1964. int status = priv->hw->mac->host_irq_status((void __iomem *)
  1965. dev->base_addr,
  1966. &priv->xstats);
  1967. if (unlikely(status)) {
  1968. /* For LPI we need to save the tx status */
  1969. if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
  1970. priv->tx_path_in_lpi_mode = true;
  1971. if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
  1972. priv->tx_path_in_lpi_mode = false;
  1973. }
  1974. }
  1975. /* To handle DMA interrupts */
  1976. stmmac_dma_interrupt(priv);
  1977. return IRQ_HANDLED;
  1978. }
  1979. #ifdef CONFIG_NET_POLL_CONTROLLER
  1980. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1981. * to allow network I/O with interrupts disabled.
  1982. */
  1983. static void stmmac_poll_controller(struct net_device *dev)
  1984. {
  1985. disable_irq(dev->irq);
  1986. stmmac_interrupt(dev->irq, dev);
  1987. enable_irq(dev->irq);
  1988. }
  1989. #endif
  1990. /**
  1991. * stmmac_ioctl - Entry point for the Ioctl
  1992. * @dev: Device pointer.
  1993. * @rq: An IOCTL specefic structure, that can contain a pointer to
  1994. * a proprietary structure used to pass information to the driver.
  1995. * @cmd: IOCTL command
  1996. * Description:
  1997. * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
  1998. */
  1999. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2000. {
  2001. struct stmmac_priv *priv = netdev_priv(dev);
  2002. int ret = -EOPNOTSUPP;
  2003. if (!netif_running(dev))
  2004. return -EINVAL;
  2005. switch (cmd) {
  2006. case SIOCGMIIPHY:
  2007. case SIOCGMIIREG:
  2008. case SIOCSMIIREG:
  2009. if (!priv->phydev)
  2010. return -EINVAL;
  2011. ret = phy_mii_ioctl(priv->phydev, rq, cmd);
  2012. break;
  2013. case SIOCSHWTSTAMP:
  2014. ret = stmmac_hwtstamp_ioctl(dev, rq);
  2015. break;
  2016. default:
  2017. break;
  2018. }
  2019. return ret;
  2020. }
  2021. #ifdef CONFIG_STMMAC_DEBUG_FS
  2022. static struct dentry *stmmac_fs_dir;
  2023. static struct dentry *stmmac_rings_status;
  2024. static struct dentry *stmmac_dma_cap;
  2025. static void sysfs_display_ring(void *head, int size, int extend_desc,
  2026. struct seq_file *seq)
  2027. {
  2028. int i;
  2029. struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
  2030. struct dma_desc *p = (struct dma_desc *)head;
  2031. for (i = 0; i < size; i++) {
  2032. u64 x;
  2033. if (extend_desc) {
  2034. x = *(u64 *) ep;
  2035. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2036. i, (unsigned int)virt_to_phys(ep),
  2037. (unsigned int)x, (unsigned int)(x >> 32),
  2038. ep->basic.des2, ep->basic.des3);
  2039. ep++;
  2040. } else {
  2041. x = *(u64 *) p;
  2042. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  2043. i, (unsigned int)virt_to_phys(ep),
  2044. (unsigned int)x, (unsigned int)(x >> 32),
  2045. p->des2, p->des3);
  2046. p++;
  2047. }
  2048. seq_printf(seq, "\n");
  2049. }
  2050. }
  2051. static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
  2052. {
  2053. struct net_device *dev = seq->private;
  2054. struct stmmac_priv *priv = netdev_priv(dev);
  2055. unsigned int txsize = priv->dma_tx_size;
  2056. unsigned int rxsize = priv->dma_rx_size;
  2057. if (priv->extend_desc) {
  2058. seq_printf(seq, "Extended RX descriptor ring:\n");
  2059. sysfs_display_ring((void *)priv->dma_erx, rxsize, 1, seq);
  2060. seq_printf(seq, "Extended TX descriptor ring:\n");
  2061. sysfs_display_ring((void *)priv->dma_etx, txsize, 1, seq);
  2062. } else {
  2063. seq_printf(seq, "RX descriptor ring:\n");
  2064. sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
  2065. seq_printf(seq, "TX descriptor ring:\n");
  2066. sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
  2067. }
  2068. return 0;
  2069. }
  2070. static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
  2071. {
  2072. return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
  2073. }
  2074. static const struct file_operations stmmac_rings_status_fops = {
  2075. .owner = THIS_MODULE,
  2076. .open = stmmac_sysfs_ring_open,
  2077. .read = seq_read,
  2078. .llseek = seq_lseek,
  2079. .release = single_release,
  2080. };
  2081. static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
  2082. {
  2083. struct net_device *dev = seq->private;
  2084. struct stmmac_priv *priv = netdev_priv(dev);
  2085. if (!priv->hw_cap_support) {
  2086. seq_printf(seq, "DMA HW features not supported\n");
  2087. return 0;
  2088. }
  2089. seq_printf(seq, "==============================\n");
  2090. seq_printf(seq, "\tDMA HW features\n");
  2091. seq_printf(seq, "==============================\n");
  2092. seq_printf(seq, "\t10/100 Mbps %s\n",
  2093. (priv->dma_cap.mbps_10_100) ? "Y" : "N");
  2094. seq_printf(seq, "\t1000 Mbps %s\n",
  2095. (priv->dma_cap.mbps_1000) ? "Y" : "N");
  2096. seq_printf(seq, "\tHalf duple %s\n",
  2097. (priv->dma_cap.half_duplex) ? "Y" : "N");
  2098. seq_printf(seq, "\tHash Filter: %s\n",
  2099. (priv->dma_cap.hash_filter) ? "Y" : "N");
  2100. seq_printf(seq, "\tMultiple MAC address registers: %s\n",
  2101. (priv->dma_cap.multi_addr) ? "Y" : "N");
  2102. seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
  2103. (priv->dma_cap.pcs) ? "Y" : "N");
  2104. seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
  2105. (priv->dma_cap.sma_mdio) ? "Y" : "N");
  2106. seq_printf(seq, "\tPMT Remote wake up: %s\n",
  2107. (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
  2108. seq_printf(seq, "\tPMT Magic Frame: %s\n",
  2109. (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
  2110. seq_printf(seq, "\tRMON module: %s\n",
  2111. (priv->dma_cap.rmon) ? "Y" : "N");
  2112. seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
  2113. (priv->dma_cap.time_stamp) ? "Y" : "N");
  2114. seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
  2115. (priv->dma_cap.atime_stamp) ? "Y" : "N");
  2116. seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
  2117. (priv->dma_cap.eee) ? "Y" : "N");
  2118. seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
  2119. seq_printf(seq, "\tChecksum Offload in TX: %s\n",
  2120. (priv->dma_cap.tx_coe) ? "Y" : "N");
  2121. seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
  2122. (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
  2123. seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
  2124. (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
  2125. seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
  2126. (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
  2127. seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
  2128. priv->dma_cap.number_rx_channel);
  2129. seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
  2130. priv->dma_cap.number_tx_channel);
  2131. seq_printf(seq, "\tEnhanced descriptors: %s\n",
  2132. (priv->dma_cap.enh_desc) ? "Y" : "N");
  2133. return 0;
  2134. }
  2135. static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
  2136. {
  2137. return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
  2138. }
  2139. static const struct file_operations stmmac_dma_cap_fops = {
  2140. .owner = THIS_MODULE,
  2141. .open = stmmac_sysfs_dma_cap_open,
  2142. .read = seq_read,
  2143. .llseek = seq_lseek,
  2144. .release = single_release,
  2145. };
  2146. static int stmmac_init_fs(struct net_device *dev)
  2147. {
  2148. /* Create debugfs entries */
  2149. stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
  2150. if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
  2151. pr_err("ERROR %s, debugfs create directory failed\n",
  2152. STMMAC_RESOURCE_NAME);
  2153. return -ENOMEM;
  2154. }
  2155. /* Entry to report DMA RX/TX rings */
  2156. stmmac_rings_status = debugfs_create_file("descriptors_status",
  2157. S_IRUGO, stmmac_fs_dir, dev,
  2158. &stmmac_rings_status_fops);
  2159. if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
  2160. pr_info("ERROR creating stmmac ring debugfs file\n");
  2161. debugfs_remove(stmmac_fs_dir);
  2162. return -ENOMEM;
  2163. }
  2164. /* Entry to report the DMA HW features */
  2165. stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
  2166. dev, &stmmac_dma_cap_fops);
  2167. if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
  2168. pr_info("ERROR creating stmmac MMC debugfs file\n");
  2169. debugfs_remove(stmmac_rings_status);
  2170. debugfs_remove(stmmac_fs_dir);
  2171. return -ENOMEM;
  2172. }
  2173. return 0;
  2174. }
  2175. static void stmmac_exit_fs(void)
  2176. {
  2177. debugfs_remove(stmmac_rings_status);
  2178. debugfs_remove(stmmac_dma_cap);
  2179. debugfs_remove(stmmac_fs_dir);
  2180. }
  2181. #endif /* CONFIG_STMMAC_DEBUG_FS */
  2182. static const struct net_device_ops stmmac_netdev_ops = {
  2183. .ndo_open = stmmac_open,
  2184. .ndo_start_xmit = stmmac_xmit,
  2185. .ndo_stop = stmmac_release,
  2186. .ndo_change_mtu = stmmac_change_mtu,
  2187. .ndo_fix_features = stmmac_fix_features,
  2188. .ndo_set_rx_mode = stmmac_set_rx_mode,
  2189. .ndo_tx_timeout = stmmac_tx_timeout,
  2190. .ndo_do_ioctl = stmmac_ioctl,
  2191. .ndo_set_config = stmmac_config,
  2192. #ifdef CONFIG_NET_POLL_CONTROLLER
  2193. .ndo_poll_controller = stmmac_poll_controller,
  2194. #endif
  2195. .ndo_set_mac_address = eth_mac_addr,
  2196. };
  2197. /**
  2198. * stmmac_hw_init - Init the MAC device
  2199. * @priv: driver private structure
  2200. * Description: this function detects which MAC device
  2201. * (GMAC/MAC10-100) has to attached, checks the HW capability
  2202. * (if supported) and sets the driver's features (for example
  2203. * to use the ring or chaine mode or support the normal/enh
  2204. * descriptor structure).
  2205. */
  2206. static int stmmac_hw_init(struct stmmac_priv *priv)
  2207. {
  2208. int ret;
  2209. struct mac_device_info *mac;
  2210. /* Identify the MAC HW device */
  2211. if (priv->plat->has_gmac) {
  2212. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  2213. mac = dwmac1000_setup(priv->ioaddr);
  2214. } else {
  2215. mac = dwmac100_setup(priv->ioaddr);
  2216. }
  2217. if (!mac)
  2218. return -ENOMEM;
  2219. priv->hw = mac;
  2220. /* Get and dump the chip ID */
  2221. priv->synopsys_id = stmmac_get_synopsys_id(priv);
  2222. /* To use the chained or ring mode */
  2223. if (chain_mode) {
  2224. priv->hw->chain = &chain_mode_ops;
  2225. pr_info(" Chain mode enabled\n");
  2226. priv->mode = STMMAC_CHAIN_MODE;
  2227. } else {
  2228. priv->hw->ring = &ring_mode_ops;
  2229. pr_info(" Ring mode enabled\n");
  2230. priv->mode = STMMAC_RING_MODE;
  2231. }
  2232. /* Get the HW capability (new GMAC newer than 3.50a) */
  2233. priv->hw_cap_support = stmmac_get_hw_features(priv);
  2234. if (priv->hw_cap_support) {
  2235. pr_info(" DMA HW capability register supported");
  2236. /* We can override some gmac/dma configuration fields: e.g.
  2237. * enh_desc, tx_coe (e.g. that are passed through the
  2238. * platform) with the values from the HW capability
  2239. * register (if supported).
  2240. */
  2241. priv->plat->enh_desc = priv->dma_cap.enh_desc;
  2242. priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
  2243. priv->plat->tx_coe = priv->dma_cap.tx_coe;
  2244. if (priv->dma_cap.rx_coe_type2)
  2245. priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
  2246. else if (priv->dma_cap.rx_coe_type1)
  2247. priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
  2248. } else
  2249. pr_info(" No HW DMA feature register supported");
  2250. /* To use alternate (extended) or normal descriptor structures */
  2251. stmmac_selec_desc_mode(priv);
  2252. ret = priv->hw->mac->rx_ipc(priv->ioaddr);
  2253. if (!ret) {
  2254. pr_warn(" RX IPC Checksum Offload not configured.\n");
  2255. priv->plat->rx_coe = STMMAC_RX_COE_NONE;
  2256. }
  2257. if (priv->plat->rx_coe)
  2258. pr_info(" RX Checksum Offload Engine supported (type %d)\n",
  2259. priv->plat->rx_coe);
  2260. if (priv->plat->tx_coe)
  2261. pr_info(" TX Checksum insertion supported\n");
  2262. if (priv->plat->pmt) {
  2263. pr_info(" Wake-Up On Lan supported\n");
  2264. device_set_wakeup_capable(priv->device, 1);
  2265. }
  2266. return 0;
  2267. }
  2268. /**
  2269. * stmmac_dvr_probe
  2270. * @device: device pointer
  2271. * @plat_dat: platform data pointer
  2272. * @addr: iobase memory address
  2273. * Description: this is the main probe function used to
  2274. * call the alloc_etherdev, allocate the priv structure.
  2275. */
  2276. struct stmmac_priv *stmmac_dvr_probe(struct device *device,
  2277. struct plat_stmmacenet_data *plat_dat,
  2278. void __iomem *addr)
  2279. {
  2280. int ret = 0;
  2281. struct net_device *ndev = NULL;
  2282. struct stmmac_priv *priv;
  2283. ndev = alloc_etherdev(sizeof(struct stmmac_priv));
  2284. if (!ndev)
  2285. return NULL;
  2286. SET_NETDEV_DEV(ndev, device);
  2287. priv = netdev_priv(ndev);
  2288. priv->device = device;
  2289. priv->dev = ndev;
  2290. ether_setup(ndev);
  2291. stmmac_set_ethtool_ops(ndev);
  2292. priv->pause = pause;
  2293. priv->plat = plat_dat;
  2294. priv->ioaddr = addr;
  2295. priv->dev->base_addr = (unsigned long)addr;
  2296. /* Verify driver arguments */
  2297. stmmac_verify_args();
  2298. /* Override with kernel parameters if supplied XXX CRS XXX
  2299. * this needs to have multiple instances
  2300. */
  2301. if ((phyaddr >= 0) && (phyaddr <= 31))
  2302. priv->plat->phy_addr = phyaddr;
  2303. /* Init MAC and get the capabilities */
  2304. ret = stmmac_hw_init(priv);
  2305. if (ret)
  2306. goto error_free_netdev;
  2307. ndev->netdev_ops = &stmmac_netdev_ops;
  2308. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2309. NETIF_F_RXCSUM;
  2310. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  2311. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  2312. #ifdef STMMAC_VLAN_TAG_USED
  2313. /* Both mac100 and gmac support receive VLAN tag detection */
  2314. ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2315. #endif
  2316. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  2317. if (flow_ctrl)
  2318. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  2319. /* Rx Watchdog is available in the COREs newer than the 3.40.
  2320. * In some case, for example on bugged HW this feature
  2321. * has to be disable and this can be done by passing the
  2322. * riwt_off field from the platform.
  2323. */
  2324. if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
  2325. priv->use_riwt = 1;
  2326. pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
  2327. }
  2328. netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
  2329. spin_lock_init(&priv->lock);
  2330. spin_lock_init(&priv->tx_lock);
  2331. ret = register_netdev(ndev);
  2332. if (ret) {
  2333. pr_err("%s: ERROR %i registering the device\n", __func__, ret);
  2334. goto error_netdev_register;
  2335. }
  2336. priv->stmmac_clk = clk_get(priv->device, STMMAC_RESOURCE_NAME);
  2337. if (IS_ERR(priv->stmmac_clk)) {
  2338. pr_warn("%s: warning: cannot get CSR clock\n", __func__);
  2339. goto error_clk_get;
  2340. }
  2341. /* If a specific clk_csr value is passed from the platform
  2342. * this means that the CSR Clock Range selection cannot be
  2343. * changed at run-time and it is fixed. Viceversa the driver'll try to
  2344. * set the MDC clock dynamically according to the csr actual
  2345. * clock input.
  2346. */
  2347. if (!priv->plat->clk_csr)
  2348. stmmac_clk_csr_set(priv);
  2349. else
  2350. priv->clk_csr = priv->plat->clk_csr;
  2351. stmmac_check_pcs_mode(priv);
  2352. if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
  2353. priv->pcs != STMMAC_PCS_RTBI) {
  2354. /* MDIO bus Registration */
  2355. ret = stmmac_mdio_register(ndev);
  2356. if (ret < 0) {
  2357. pr_debug("%s: MDIO bus (id: %d) registration failed",
  2358. __func__, priv->plat->bus_id);
  2359. goto error_mdio_register;
  2360. }
  2361. }
  2362. return priv;
  2363. error_mdio_register:
  2364. clk_put(priv->stmmac_clk);
  2365. error_clk_get:
  2366. unregister_netdev(ndev);
  2367. error_netdev_register:
  2368. netif_napi_del(&priv->napi);
  2369. error_free_netdev:
  2370. free_netdev(ndev);
  2371. return NULL;
  2372. }
  2373. /**
  2374. * stmmac_dvr_remove
  2375. * @ndev: net device pointer
  2376. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  2377. * changes the link status, releases the DMA descriptor rings.
  2378. */
  2379. int stmmac_dvr_remove(struct net_device *ndev)
  2380. {
  2381. struct stmmac_priv *priv = netdev_priv(ndev);
  2382. pr_info("%s:\n\tremoving driver", __func__);
  2383. priv->hw->dma->stop_rx(priv->ioaddr);
  2384. priv->hw->dma->stop_tx(priv->ioaddr);
  2385. stmmac_set_mac(priv->ioaddr, false);
  2386. if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
  2387. priv->pcs != STMMAC_PCS_RTBI)
  2388. stmmac_mdio_unregister(ndev);
  2389. netif_carrier_off(ndev);
  2390. unregister_netdev(ndev);
  2391. free_netdev(ndev);
  2392. return 0;
  2393. }
  2394. #ifdef CONFIG_PM
  2395. int stmmac_suspend(struct net_device *ndev)
  2396. {
  2397. struct stmmac_priv *priv = netdev_priv(ndev);
  2398. unsigned long flags;
  2399. if (!ndev || !netif_running(ndev))
  2400. return 0;
  2401. if (priv->phydev)
  2402. phy_stop(priv->phydev);
  2403. spin_lock_irqsave(&priv->lock, flags);
  2404. netif_device_detach(ndev);
  2405. netif_stop_queue(ndev);
  2406. napi_disable(&priv->napi);
  2407. /* Stop TX/RX DMA */
  2408. priv->hw->dma->stop_tx(priv->ioaddr);
  2409. priv->hw->dma->stop_rx(priv->ioaddr);
  2410. stmmac_clear_descriptors(priv);
  2411. /* Enable Power down mode by programming the PMT regs */
  2412. if (device_may_wakeup(priv->device))
  2413. priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
  2414. else {
  2415. stmmac_set_mac(priv->ioaddr, false);
  2416. /* Disable clock in case of PWM is off */
  2417. clk_disable_unprepare(priv->stmmac_clk);
  2418. }
  2419. spin_unlock_irqrestore(&priv->lock, flags);
  2420. return 0;
  2421. }
  2422. int stmmac_resume(struct net_device *ndev)
  2423. {
  2424. struct stmmac_priv *priv = netdev_priv(ndev);
  2425. unsigned long flags;
  2426. if (!netif_running(ndev))
  2427. return 0;
  2428. spin_lock_irqsave(&priv->lock, flags);
  2429. /* Power Down bit, into the PM register, is cleared
  2430. * automatically as soon as a magic packet or a Wake-up frame
  2431. * is received. Anyway, it's better to manually clear
  2432. * this bit because it can generate problems while resuming
  2433. * from another devices (e.g. serial console).
  2434. */
  2435. if (device_may_wakeup(priv->device))
  2436. priv->hw->mac->pmt(priv->ioaddr, 0);
  2437. else
  2438. /* enable the clk prevously disabled */
  2439. clk_prepare_enable(priv->stmmac_clk);
  2440. netif_device_attach(ndev);
  2441. /* Enable the MAC and DMA */
  2442. stmmac_set_mac(priv->ioaddr, true);
  2443. priv->hw->dma->start_tx(priv->ioaddr);
  2444. priv->hw->dma->start_rx(priv->ioaddr);
  2445. napi_enable(&priv->napi);
  2446. netif_start_queue(ndev);
  2447. spin_unlock_irqrestore(&priv->lock, flags);
  2448. if (priv->phydev)
  2449. phy_start(priv->phydev);
  2450. return 0;
  2451. }
  2452. int stmmac_freeze(struct net_device *ndev)
  2453. {
  2454. if (!ndev || !netif_running(ndev))
  2455. return 0;
  2456. return stmmac_release(ndev);
  2457. }
  2458. int stmmac_restore(struct net_device *ndev)
  2459. {
  2460. if (!ndev || !netif_running(ndev))
  2461. return 0;
  2462. return stmmac_open(ndev);
  2463. }
  2464. #endif /* CONFIG_PM */
  2465. /* Driver can be configured w/ and w/ both PCI and Platf drivers
  2466. * depending on the configuration selected.
  2467. */
  2468. static int __init stmmac_init(void)
  2469. {
  2470. int ret;
  2471. ret = stmmac_register_platform();
  2472. if (ret)
  2473. goto err;
  2474. ret = stmmac_register_pci();
  2475. if (ret)
  2476. goto err_pci;
  2477. return 0;
  2478. err_pci:
  2479. stmmac_unregister_platform();
  2480. err:
  2481. pr_err("stmmac: driver registration failed\n");
  2482. return ret;
  2483. }
  2484. static void __exit stmmac_exit(void)
  2485. {
  2486. stmmac_unregister_platform();
  2487. stmmac_unregister_pci();
  2488. }
  2489. module_init(stmmac_init);
  2490. module_exit(stmmac_exit);
  2491. #ifndef MODULE
  2492. static int __init stmmac_cmdline_opt(char *str)
  2493. {
  2494. char *opt;
  2495. if (!str || !*str)
  2496. return -EINVAL;
  2497. while ((opt = strsep(&str, ",")) != NULL) {
  2498. if (!strncmp(opt, "debug:", 6)) {
  2499. if (kstrtoint(opt + 6, 0, &debug))
  2500. goto err;
  2501. } else if (!strncmp(opt, "phyaddr:", 8)) {
  2502. if (kstrtoint(opt + 8, 0, &phyaddr))
  2503. goto err;
  2504. } else if (!strncmp(opt, "dma_txsize:", 11)) {
  2505. if (kstrtoint(opt + 11, 0, &dma_txsize))
  2506. goto err;
  2507. } else if (!strncmp(opt, "dma_rxsize:", 11)) {
  2508. if (kstrtoint(opt + 11, 0, &dma_rxsize))
  2509. goto err;
  2510. } else if (!strncmp(opt, "buf_sz:", 7)) {
  2511. if (kstrtoint(opt + 7, 0, &buf_sz))
  2512. goto err;
  2513. } else if (!strncmp(opt, "tc:", 3)) {
  2514. if (kstrtoint(opt + 3, 0, &tc))
  2515. goto err;
  2516. } else if (!strncmp(opt, "watchdog:", 9)) {
  2517. if (kstrtoint(opt + 9, 0, &watchdog))
  2518. goto err;
  2519. } else if (!strncmp(opt, "flow_ctrl:", 10)) {
  2520. if (kstrtoint(opt + 10, 0, &flow_ctrl))
  2521. goto err;
  2522. } else if (!strncmp(opt, "pause:", 6)) {
  2523. if (kstrtoint(opt + 6, 0, &pause))
  2524. goto err;
  2525. } else if (!strncmp(opt, "eee_timer:", 10)) {
  2526. if (kstrtoint(opt + 10, 0, &eee_timer))
  2527. goto err;
  2528. } else if (!strncmp(opt, "chain_mode:", 11)) {
  2529. if (kstrtoint(opt + 11, 0, &chain_mode))
  2530. goto err;
  2531. }
  2532. }
  2533. return 0;
  2534. err:
  2535. pr_err("%s: ERROR broken module parameter conversion", __func__);
  2536. return -EINVAL;
  2537. }
  2538. __setup("stmmaceth=", stmmac_cmdline_opt);
  2539. #endif /* MODULE */
  2540. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
  2541. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  2542. MODULE_LICENSE("GPL");