stmmac_main.c 89 KB

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