stmmac_main.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. /*******************************************************************************
  2. This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
  3. ST Ethernet IPs are built around a Synopsys IP Core.
  4. Copyright(C) 2007-2011 STMicroelectronics Ltd
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms and conditions of the GNU General Public License,
  7. version 2, as published by the Free Software Foundation.
  8. This program is distributed in the hope it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. You should have received a copy of the GNU General Public License along with
  13. this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  15. The full GNU General Public License is included in this distribution in
  16. the file called "COPYING".
  17. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  18. Documentation available at:
  19. http://www.stlinux.com
  20. Support available at:
  21. https://bugzilla.stlinux.com/
  22. *******************************************************************************/
  23. #include <linux/clk.h>
  24. #include <linux/kernel.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/ip.h>
  27. #include <linux/tcp.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/if_ether.h>
  31. #include <linux/crc32.h>
  32. #include <linux/mii.h>
  33. #include <linux/if.h>
  34. #include <linux/if_vlan.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/slab.h>
  37. #include <linux/prefetch.h>
  38. #ifdef CONFIG_STMMAC_DEBUG_FS
  39. #include <linux/debugfs.h>
  40. #include <linux/seq_file.h>
  41. #endif
  42. #include "stmmac.h"
  43. #undef STMMAC_DEBUG
  44. /*#define STMMAC_DEBUG*/
  45. #ifdef STMMAC_DEBUG
  46. #define DBG(nlevel, klevel, fmt, args...) \
  47. ((void)(netif_msg_##nlevel(priv) && \
  48. printk(KERN_##klevel fmt, ## args)))
  49. #else
  50. #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
  51. #endif
  52. #undef STMMAC_RX_DEBUG
  53. /*#define STMMAC_RX_DEBUG*/
  54. #ifdef STMMAC_RX_DEBUG
  55. #define RX_DBG(fmt, args...) printk(fmt, ## args)
  56. #else
  57. #define RX_DBG(fmt, args...) do { } while (0)
  58. #endif
  59. #undef STMMAC_XMIT_DEBUG
  60. /*#define STMMAC_XMIT_DEBUG*/
  61. #ifdef STMMAC_XMIT_DEBUG
  62. #define TX_DBG(fmt, args...) printk(fmt, ## args)
  63. #else
  64. #define TX_DBG(fmt, args...) do { } while (0)
  65. #endif
  66. #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
  67. #define JUMBO_LEN 9000
  68. /* Module parameters */
  69. #define TX_TIMEO 5000 /* default 5 seconds */
  70. static int watchdog = TX_TIMEO;
  71. module_param(watchdog, int, S_IRUGO | S_IWUSR);
  72. MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
  73. static int debug = -1; /* -1: default, 0: no output, 16: all */
  74. module_param(debug, int, S_IRUGO | S_IWUSR);
  75. MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
  76. int phyaddr = -1;
  77. module_param(phyaddr, int, S_IRUGO);
  78. MODULE_PARM_DESC(phyaddr, "Physical device address");
  79. #define DMA_TX_SIZE 256
  80. static int dma_txsize = DMA_TX_SIZE;
  81. module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
  82. MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
  83. #define DMA_RX_SIZE 256
  84. static int dma_rxsize = DMA_RX_SIZE;
  85. module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
  86. MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
  87. static int flow_ctrl = FLOW_OFF;
  88. module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
  89. MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
  90. static int pause = PAUSE_TIME;
  91. module_param(pause, int, S_IRUGO | S_IWUSR);
  92. MODULE_PARM_DESC(pause, "Flow Control Pause Time");
  93. #define TC_DEFAULT 64
  94. static int tc = TC_DEFAULT;
  95. module_param(tc, int, S_IRUGO | S_IWUSR);
  96. MODULE_PARM_DESC(tc, "DMA threshold control value");
  97. #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
  98. static int buf_sz = DMA_BUFFER_SIZE;
  99. module_param(buf_sz, int, S_IRUGO | S_IWUSR);
  100. MODULE_PARM_DESC(buf_sz, "DMA buffer size");
  101. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  102. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  103. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  104. #define STMMAC_DEFAULT_LPI_TIMER 1000
  105. static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  106. module_param(eee_timer, int, S_IRUGO | S_IWUSR);
  107. MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
  108. #define STMMAC_LPI_TIMER(x) (jiffies + msecs_to_jiffies(x))
  109. /* By default the driver will use the ring mode to manage tx and rx descriptors
  110. * but passing this value so user can force to use the chain instead of the ring
  111. */
  112. static unsigned int chain_mode;
  113. module_param(chain_mode, int, S_IRUGO);
  114. MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
  115. static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
  116. #ifdef CONFIG_STMMAC_DEBUG_FS
  117. static int stmmac_init_fs(struct net_device *dev);
  118. static void stmmac_exit_fs(void);
  119. #endif
  120. #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
  121. /**
  122. * stmmac_verify_args - verify the driver parameters.
  123. * Description: it verifies if some wrong parameter is passed to the driver.
  124. * Note that wrong parameters are replaced with the default values.
  125. */
  126. static void stmmac_verify_args(void)
  127. {
  128. if (unlikely(watchdog < 0))
  129. watchdog = TX_TIMEO;
  130. if (unlikely(dma_rxsize < 0))
  131. dma_rxsize = DMA_RX_SIZE;
  132. if (unlikely(dma_txsize < 0))
  133. dma_txsize = DMA_TX_SIZE;
  134. if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
  135. buf_sz = DMA_BUFFER_SIZE;
  136. if (unlikely(flow_ctrl > 1))
  137. flow_ctrl = FLOW_AUTO;
  138. else if (likely(flow_ctrl < 0))
  139. flow_ctrl = FLOW_OFF;
  140. if (unlikely((pause < 0) || (pause > 0xffff)))
  141. pause = PAUSE_TIME;
  142. if (eee_timer < 0)
  143. eee_timer = STMMAC_DEFAULT_LPI_TIMER;
  144. }
  145. static void stmmac_clk_csr_set(struct stmmac_priv *priv)
  146. {
  147. u32 clk_rate;
  148. clk_rate = clk_get_rate(priv->stmmac_clk);
  149. /* Platform provided default clk_csr would be assumed valid
  150. * for all other cases except for the below mentioned ones. */
  151. if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
  152. if (clk_rate < CSR_F_35M)
  153. priv->clk_csr = STMMAC_CSR_20_35M;
  154. else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
  155. priv->clk_csr = STMMAC_CSR_35_60M;
  156. else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
  157. priv->clk_csr = STMMAC_CSR_60_100M;
  158. else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
  159. priv->clk_csr = STMMAC_CSR_100_150M;
  160. else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
  161. priv->clk_csr = STMMAC_CSR_150_250M;
  162. else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
  163. priv->clk_csr = STMMAC_CSR_250_300M;
  164. } /* For values higher than the IEEE 802.3 specified frequency
  165. * we can not estimate the proper divider as it is not known
  166. * the frequency of clk_csr_i. So we do not change the default
  167. * divider. */
  168. }
  169. #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
  170. static void print_pkt(unsigned char *buf, int len)
  171. {
  172. int j;
  173. pr_info("len = %d byte, buf addr: 0x%p", len, buf);
  174. for (j = 0; j < len; j++) {
  175. if ((j % 16) == 0)
  176. pr_info("\n %03x:", j);
  177. pr_info(" %02x", buf[j]);
  178. }
  179. pr_info("\n");
  180. }
  181. #endif
  182. /* minimum number of free TX descriptors required to wake up TX process */
  183. #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
  184. static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
  185. {
  186. return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
  187. }
  188. /* On some ST platforms, some HW system configuraton registers have to be
  189. * set according to the link speed negotiated.
  190. */
  191. static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
  192. {
  193. struct phy_device *phydev = priv->phydev;
  194. if (likely(priv->plat->fix_mac_speed))
  195. priv->plat->fix_mac_speed(priv->plat->bsp_priv,
  196. phydev->speed);
  197. }
  198. static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
  199. {
  200. /* Check and enter in LPI mode */
  201. if ((priv->dirty_tx == priv->cur_tx) &&
  202. (priv->tx_path_in_lpi_mode == false))
  203. priv->hw->mac->set_eee_mode(priv->ioaddr);
  204. }
  205. void stmmac_disable_eee_mode(struct stmmac_priv *priv)
  206. {
  207. /* Exit and disable EEE in case of we are are in LPI state. */
  208. priv->hw->mac->reset_eee_mode(priv->ioaddr);
  209. del_timer_sync(&priv->eee_ctrl_timer);
  210. priv->tx_path_in_lpi_mode = false;
  211. }
  212. /**
  213. * stmmac_eee_ctrl_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_TIMER(eee_timer));
  224. }
  225. /**
  226. * stmmac_eee_init
  227. * @priv: private device pointer
  228. * Description:
  229. * If the EEE support has been enabled while configuring the driver,
  230. * if the GMAC actually supports the EEE (from the HW cap reg) and the
  231. * phy can also manage EEE, so enable the LPI state and start the timer
  232. * to verify if the tx path can enter in LPI state.
  233. */
  234. bool stmmac_eee_init(struct stmmac_priv *priv)
  235. {
  236. bool ret = false;
  237. /* MAC core supports the EEE feature. */
  238. if (priv->dma_cap.eee) {
  239. /* Check if the PHY supports EEE */
  240. if (phy_init_eee(priv->phydev, 1))
  241. goto out;
  242. priv->eee_active = 1;
  243. init_timer(&priv->eee_ctrl_timer);
  244. priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
  245. priv->eee_ctrl_timer.data = (unsigned long)priv;
  246. priv->eee_ctrl_timer.expires = STMMAC_LPI_TIMER(eee_timer);
  247. add_timer(&priv->eee_ctrl_timer);
  248. priv->hw->mac->set_eee_timer(priv->ioaddr,
  249. STMMAC_DEFAULT_LIT_LS_TIMER,
  250. priv->tx_lpi_timer);
  251. pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
  252. ret = true;
  253. }
  254. out:
  255. return ret;
  256. }
  257. static void stmmac_eee_adjust(struct stmmac_priv *priv)
  258. {
  259. /* When the EEE has been already initialised we have to
  260. * modify the PLS bit in the LPI ctrl & status reg according
  261. * to the PHY link status. For this reason.
  262. */
  263. if (priv->eee_enabled)
  264. priv->hw->mac->set_eee_pls(priv->ioaddr, priv->phydev->link);
  265. }
  266. /**
  267. * stmmac_adjust_link
  268. * @dev: net device structure
  269. * Description: it adjusts the link parameters.
  270. */
  271. static void stmmac_adjust_link(struct net_device *dev)
  272. {
  273. struct stmmac_priv *priv = netdev_priv(dev);
  274. struct phy_device *phydev = priv->phydev;
  275. unsigned long flags;
  276. int new_state = 0;
  277. unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
  278. if (phydev == NULL)
  279. return;
  280. DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
  281. phydev->addr, phydev->link);
  282. spin_lock_irqsave(&priv->lock, flags);
  283. if (phydev->link) {
  284. u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
  285. /* Now we make sure that we can be in full duplex mode.
  286. * If not, we operate in half-duplex mode. */
  287. if (phydev->duplex != priv->oldduplex) {
  288. new_state = 1;
  289. if (!(phydev->duplex))
  290. ctrl &= ~priv->hw->link.duplex;
  291. else
  292. ctrl |= priv->hw->link.duplex;
  293. priv->oldduplex = phydev->duplex;
  294. }
  295. /* Flow Control operation */
  296. if (phydev->pause)
  297. priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
  298. fc, pause_time);
  299. if (phydev->speed != priv->speed) {
  300. new_state = 1;
  301. switch (phydev->speed) {
  302. case 1000:
  303. if (likely(priv->plat->has_gmac))
  304. ctrl &= ~priv->hw->link.port;
  305. stmmac_hw_fix_mac_speed(priv);
  306. break;
  307. case 100:
  308. case 10:
  309. if (priv->plat->has_gmac) {
  310. ctrl |= priv->hw->link.port;
  311. if (phydev->speed == SPEED_100) {
  312. ctrl |= priv->hw->link.speed;
  313. } else {
  314. ctrl &= ~(priv->hw->link.speed);
  315. }
  316. } else {
  317. ctrl &= ~priv->hw->link.port;
  318. }
  319. stmmac_hw_fix_mac_speed(priv);
  320. break;
  321. default:
  322. if (netif_msg_link(priv))
  323. pr_warning("%s: Speed (%d) is not 10"
  324. " or 100!\n", dev->name, phydev->speed);
  325. break;
  326. }
  327. priv->speed = phydev->speed;
  328. }
  329. writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
  330. if (!priv->oldlink) {
  331. new_state = 1;
  332. priv->oldlink = 1;
  333. }
  334. } else if (priv->oldlink) {
  335. new_state = 1;
  336. priv->oldlink = 0;
  337. priv->speed = 0;
  338. priv->oldduplex = -1;
  339. }
  340. if (new_state && netif_msg_link(priv))
  341. phy_print_status(phydev);
  342. stmmac_eee_adjust(priv);
  343. spin_unlock_irqrestore(&priv->lock, flags);
  344. DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
  345. }
  346. /**
  347. * stmmac_init_phy - PHY initialization
  348. * @dev: net device structure
  349. * Description: it initializes the driver's PHY state, and attaches the PHY
  350. * to the mac driver.
  351. * Return value:
  352. * 0 on success
  353. */
  354. static int stmmac_init_phy(struct net_device *dev)
  355. {
  356. struct stmmac_priv *priv = netdev_priv(dev);
  357. struct phy_device *phydev;
  358. char phy_id_fmt[MII_BUS_ID_SIZE + 3];
  359. char bus_id[MII_BUS_ID_SIZE];
  360. int interface = priv->plat->interface;
  361. priv->oldlink = 0;
  362. priv->speed = 0;
  363. priv->oldduplex = -1;
  364. if (priv->plat->phy_bus_name)
  365. snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
  366. priv->plat->phy_bus_name, priv->plat->bus_id);
  367. else
  368. snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
  369. priv->plat->bus_id);
  370. snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  371. priv->plat->phy_addr);
  372. pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
  373. phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
  374. if (IS_ERR(phydev)) {
  375. pr_err("%s: Could not attach to PHY\n", dev->name);
  376. return PTR_ERR(phydev);
  377. }
  378. /* Stop Advertising 1000BASE Capability if interface is not GMII */
  379. if ((interface == PHY_INTERFACE_MODE_MII) ||
  380. (interface == PHY_INTERFACE_MODE_RMII))
  381. phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
  382. SUPPORTED_1000baseT_Full);
  383. /*
  384. * Broken HW is sometimes missing the pull-up resistor on the
  385. * MDIO line, which results in reads to non-existent devices returning
  386. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  387. * device as well.
  388. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  389. */
  390. if (phydev->phy_id == 0) {
  391. phy_disconnect(phydev);
  392. return -ENODEV;
  393. }
  394. pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
  395. " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
  396. priv->phydev = phydev;
  397. return 0;
  398. }
  399. /**
  400. * stmmac_display_ring
  401. * @p: pointer to the ring.
  402. * @size: size of the ring.
  403. * Description: display the control/status and buffer descriptors.
  404. */
  405. static void stmmac_display_ring(void *head, int size, int extend_desc)
  406. {
  407. int i;
  408. struct dma_extended_desc *ep = (struct dma_extended_desc *) head;
  409. struct dma_desc *p = (struct dma_desc *) head;
  410. for (i = 0; i < size; i++) {
  411. u64 x;
  412. if (extend_desc) {
  413. x = *(u64 *) ep;
  414. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  415. i, (unsigned int) virt_to_phys(ep),
  416. (unsigned int) x, (unsigned int) (x >> 32),
  417. ep->basic.des2, ep->basic.des3);
  418. ep++;
  419. } else {
  420. x = *(u64 *) p;
  421. pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
  422. i, (unsigned int) virt_to_phys(p),
  423. (unsigned int) x, (unsigned int) (x >> 32),
  424. p->des2, p->des3);
  425. p++;
  426. }
  427. pr_info("\n");
  428. }
  429. }
  430. static void stmmac_display_rings(struct stmmac_priv *priv)
  431. {
  432. unsigned int txsize = priv->dma_tx_size;
  433. unsigned int rxsize = priv->dma_rx_size;
  434. if (priv->extend_desc) {
  435. pr_info("Extended RX descriptor ring:\n");
  436. stmmac_display_ring((void *) priv->dma_erx, rxsize, 1);
  437. pr_info("Extended TX descriptor ring:\n");
  438. stmmac_display_ring((void *) priv->dma_etx, txsize, 1);
  439. } else {
  440. pr_info("RX descriptor ring:\n");
  441. stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
  442. pr_info("TX descriptor ring:\n");
  443. stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
  444. }
  445. }
  446. static int stmmac_set_bfsize(int mtu, int bufsize)
  447. {
  448. int ret = bufsize;
  449. if (mtu >= BUF_SIZE_4KiB)
  450. ret = BUF_SIZE_8KiB;
  451. else if (mtu >= BUF_SIZE_2KiB)
  452. ret = BUF_SIZE_4KiB;
  453. else if (mtu >= DMA_BUFFER_SIZE)
  454. ret = BUF_SIZE_2KiB;
  455. else
  456. ret = DMA_BUFFER_SIZE;
  457. return ret;
  458. }
  459. static void stmmac_clear_descriptors(struct stmmac_priv *priv)
  460. {
  461. int i;
  462. unsigned int txsize = priv->dma_tx_size;
  463. unsigned int rxsize = priv->dma_rx_size;
  464. /* Clear the Rx/Tx descriptors */
  465. for (i = 0; i < rxsize; i++)
  466. if (priv->extend_desc)
  467. priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
  468. priv->use_riwt, priv->mode,
  469. (i == rxsize - 1));
  470. else
  471. priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
  472. priv->use_riwt, priv->mode,
  473. (i == rxsize - 1));
  474. for (i = 0; i < txsize; i++)
  475. if (priv->extend_desc)
  476. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  477. priv->mode,
  478. (i == txsize - 1));
  479. else
  480. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  481. priv->mode,
  482. (i == txsize - 1));
  483. }
  484. static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
  485. int i)
  486. {
  487. struct sk_buff *skb;
  488. skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
  489. GFP_KERNEL);
  490. if (unlikely(skb == NULL)) {
  491. pr_err("%s: Rx init fails; skb is NULL\n", __func__);
  492. return 1;
  493. }
  494. skb_reserve(skb, NET_IP_ALIGN);
  495. priv->rx_skbuff[i] = skb;
  496. priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  497. priv->dma_buf_sz,
  498. DMA_FROM_DEVICE);
  499. p->des2 = priv->rx_skbuff_dma[i];
  500. if ((priv->mode == STMMAC_RING_MODE) &&
  501. (priv->dma_buf_sz == BUF_SIZE_16KiB))
  502. priv->hw->ring->init_desc3(p);
  503. return 0;
  504. }
  505. /**
  506. * init_dma_desc_rings - init the RX/TX descriptor rings
  507. * @dev: net device structure
  508. * Description: this function initializes the DMA RX/TX descriptors
  509. * and allocates the socket buffers. It suppors the chained and ring
  510. * modes.
  511. */
  512. static void init_dma_desc_rings(struct net_device *dev)
  513. {
  514. int i;
  515. struct stmmac_priv *priv = netdev_priv(dev);
  516. unsigned int txsize = priv->dma_tx_size;
  517. unsigned int rxsize = priv->dma_rx_size;
  518. unsigned int bfsize = 0;
  519. /* Set the max buffer size according to the DESC mode
  520. * and the MTU. Note that RING mode allows 16KiB bsize. */
  521. if (priv->mode == STMMAC_RING_MODE)
  522. bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
  523. if (bfsize < BUF_SIZE_16KiB)
  524. bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
  525. DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
  526. txsize, rxsize, bfsize);
  527. if (priv->extend_desc) {
  528. priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
  529. sizeof(struct
  530. dma_extended_desc),
  531. &priv->dma_rx_phy,
  532. GFP_KERNEL);
  533. priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
  534. sizeof(struct
  535. dma_extended_desc),
  536. &priv->dma_tx_phy,
  537. GFP_KERNEL);
  538. if ((!priv->dma_erx) || (!priv->dma_etx))
  539. return;
  540. } else {
  541. priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
  542. sizeof(struct dma_desc),
  543. &priv->dma_rx_phy,
  544. GFP_KERNEL);
  545. priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
  546. sizeof(struct dma_desc),
  547. &priv->dma_tx_phy,
  548. GFP_KERNEL);
  549. if ((!priv->dma_rx) || (!priv->dma_tx))
  550. return;
  551. }
  552. priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
  553. GFP_KERNEL);
  554. priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
  555. GFP_KERNEL);
  556. priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
  557. GFP_KERNEL);
  558. if (netif_msg_drv(priv))
  559. pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
  560. (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
  561. /* RX INITIALIZATION */
  562. DBG(probe, INFO, "stmmac: SKB addresses:\nskb\t\tskb data\tdma data\n");
  563. for (i = 0; i < rxsize; i++) {
  564. struct dma_desc *p;
  565. if (priv->extend_desc)
  566. p = &((priv->dma_erx + i)->basic);
  567. else
  568. p = priv->dma_rx + i;
  569. if (stmmac_init_rx_buffers(priv, p, i))
  570. break;
  571. DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
  572. priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
  573. }
  574. priv->cur_rx = 0;
  575. priv->dirty_rx = (unsigned int)(i - rxsize);
  576. priv->dma_buf_sz = bfsize;
  577. buf_sz = bfsize;
  578. /* Setup the chained descriptor addresses */
  579. if (priv->mode == STMMAC_CHAIN_MODE) {
  580. if (priv->extend_desc) {
  581. priv->hw->chain->init(priv->dma_erx, priv->dma_rx_phy,
  582. rxsize, 1);
  583. priv->hw->chain->init(priv->dma_etx, priv->dma_tx_phy,
  584. txsize, 1);
  585. } else {
  586. priv->hw->chain->init(priv->dma_rx, priv->dma_rx_phy,
  587. rxsize, 0);
  588. priv->hw->chain->init(priv->dma_tx, priv->dma_tx_phy,
  589. txsize, 0);
  590. }
  591. }
  592. /* TX INITIALIZATION */
  593. for (i = 0; i < txsize; i++) {
  594. struct dma_desc *p;
  595. if (priv->extend_desc)
  596. p = &((priv->dma_etx + i)->basic);
  597. else
  598. p = priv->dma_tx + i;
  599. p->des2 = 0;
  600. priv->tx_skbuff[i] = NULL;
  601. }
  602. priv->dirty_tx = 0;
  603. priv->cur_tx = 0;
  604. stmmac_clear_descriptors(priv);
  605. if (netif_msg_hw(priv))
  606. stmmac_display_rings(priv);
  607. }
  608. static void dma_free_rx_skbufs(struct stmmac_priv *priv)
  609. {
  610. int i;
  611. for (i = 0; i < priv->dma_rx_size; i++) {
  612. if (priv->rx_skbuff[i]) {
  613. dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
  614. priv->dma_buf_sz, DMA_FROM_DEVICE);
  615. dev_kfree_skb_any(priv->rx_skbuff[i]);
  616. }
  617. priv->rx_skbuff[i] = NULL;
  618. }
  619. }
  620. static void dma_free_tx_skbufs(struct stmmac_priv *priv)
  621. {
  622. int i;
  623. for (i = 0; i < priv->dma_tx_size; i++) {
  624. if (priv->tx_skbuff[i] != NULL) {
  625. struct dma_desc *p;
  626. if (priv->extend_desc)
  627. p = &((priv->dma_etx + i)->basic);
  628. else
  629. p = priv->dma_tx + i;
  630. if (p->des2)
  631. dma_unmap_single(priv->device, p->des2,
  632. priv->hw->desc->get_tx_len(p),
  633. DMA_TO_DEVICE);
  634. dev_kfree_skb_any(priv->tx_skbuff[i]);
  635. priv->tx_skbuff[i] = NULL;
  636. }
  637. }
  638. }
  639. static void free_dma_desc_resources(struct stmmac_priv *priv)
  640. {
  641. /* Release the DMA TX/RX socket buffers */
  642. dma_free_rx_skbufs(priv);
  643. dma_free_tx_skbufs(priv);
  644. /* Free the region of consistent memory previously allocated for
  645. * the DMA */
  646. if (!priv->extend_desc) {
  647. dma_free_coherent(priv->device,
  648. priv->dma_tx_size * sizeof(struct dma_desc),
  649. priv->dma_tx, priv->dma_tx_phy);
  650. dma_free_coherent(priv->device,
  651. priv->dma_rx_size * sizeof(struct dma_desc),
  652. priv->dma_rx, priv->dma_rx_phy);
  653. } else {
  654. dma_free_coherent(priv->device, priv->dma_tx_size *
  655. sizeof(struct dma_extended_desc),
  656. priv->dma_etx, priv->dma_tx_phy);
  657. dma_free_coherent(priv->device, priv->dma_rx_size *
  658. sizeof(struct dma_extended_desc),
  659. priv->dma_erx, priv->dma_rx_phy);
  660. }
  661. kfree(priv->rx_skbuff_dma);
  662. kfree(priv->rx_skbuff);
  663. kfree(priv->tx_skbuff);
  664. }
  665. /**
  666. * stmmac_dma_operation_mode - HW DMA operation mode
  667. * @priv : pointer to the private device structure.
  668. * Description: it sets the DMA operation mode: tx/rx DMA thresholds
  669. * or Store-And-Forward capability.
  670. */
  671. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  672. {
  673. if (likely(priv->plat->force_sf_dma_mode ||
  674. ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
  675. /*
  676. * In case of GMAC, SF mode can be enabled
  677. * to perform the TX COE in HW. This depends on:
  678. * 1) TX COE if actually supported
  679. * 2) There is no bugged Jumbo frame support
  680. * that needs to not insert csum in the TDES.
  681. */
  682. priv->hw->dma->dma_mode(priv->ioaddr,
  683. SF_DMA_MODE, SF_DMA_MODE);
  684. tc = SF_DMA_MODE;
  685. } else
  686. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  687. }
  688. /**
  689. * stmmac_tx_clean:
  690. * @priv: private data pointer
  691. * Description: it reclaims resources after transmission completes.
  692. */
  693. static void stmmac_tx_clean(struct stmmac_priv *priv)
  694. {
  695. unsigned int txsize = priv->dma_tx_size;
  696. spin_lock(&priv->tx_lock);
  697. priv->xstats.tx_clean++;
  698. while (priv->dirty_tx != priv->cur_tx) {
  699. int last;
  700. unsigned int entry = priv->dirty_tx % txsize;
  701. struct sk_buff *skb = priv->tx_skbuff[entry];
  702. struct dma_desc *p;
  703. if (priv->extend_desc)
  704. p = (struct dma_desc *) (priv->dma_etx + entry);
  705. else
  706. p = priv->dma_tx + entry;
  707. /* Check if the descriptor is owned by the DMA. */
  708. if (priv->hw->desc->get_tx_owner(p))
  709. break;
  710. /* Verify tx error by looking at the last segment. */
  711. last = priv->hw->desc->get_tx_ls(p);
  712. if (likely(last)) {
  713. int tx_error =
  714. priv->hw->desc->tx_status(&priv->dev->stats,
  715. &priv->xstats, p,
  716. priv->ioaddr);
  717. if (likely(tx_error == 0)) {
  718. priv->dev->stats.tx_packets++;
  719. priv->xstats.tx_pkt_n++;
  720. } else
  721. priv->dev->stats.tx_errors++;
  722. }
  723. TX_DBG("%s: curr %d, dirty %d\n", __func__,
  724. priv->cur_tx, priv->dirty_tx);
  725. if (likely(p->des2))
  726. dma_unmap_single(priv->device, p->des2,
  727. priv->hw->desc->get_tx_len(p),
  728. DMA_TO_DEVICE);
  729. if (priv->mode == STMMAC_RING_MODE)
  730. priv->hw->ring->clean_desc3(p);
  731. if (likely(skb != NULL)) {
  732. dev_kfree_skb(skb);
  733. priv->tx_skbuff[entry] = NULL;
  734. }
  735. priv->hw->desc->release_tx_desc(p, priv->mode);
  736. priv->dirty_tx++;
  737. }
  738. if (unlikely(netif_queue_stopped(priv->dev) &&
  739. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
  740. netif_tx_lock(priv->dev);
  741. if (netif_queue_stopped(priv->dev) &&
  742. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
  743. TX_DBG("%s: restart transmit\n", __func__);
  744. netif_wake_queue(priv->dev);
  745. }
  746. netif_tx_unlock(priv->dev);
  747. }
  748. if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
  749. stmmac_enable_eee_mode(priv);
  750. mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
  751. }
  752. spin_unlock(&priv->tx_lock);
  753. }
  754. static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
  755. {
  756. priv->hw->dma->enable_dma_irq(priv->ioaddr);
  757. }
  758. static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
  759. {
  760. priv->hw->dma->disable_dma_irq(priv->ioaddr);
  761. }
  762. /**
  763. * stmmac_tx_err:
  764. * @priv: pointer to the private device structure
  765. * Description: it cleans the descriptors and restarts the transmission
  766. * in case of errors.
  767. */
  768. static void stmmac_tx_err(struct stmmac_priv *priv)
  769. {
  770. int i;
  771. int txsize = priv->dma_tx_size;
  772. netif_stop_queue(priv->dev);
  773. priv->hw->dma->stop_tx(priv->ioaddr);
  774. dma_free_tx_skbufs(priv);
  775. for (i = 0; i < txsize; i++)
  776. if (priv->extend_desc)
  777. priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
  778. priv->mode,
  779. (i == txsize - 1));
  780. else
  781. priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
  782. priv->mode,
  783. (i == txsize - 1));
  784. priv->dirty_tx = 0;
  785. priv->cur_tx = 0;
  786. priv->hw->dma->start_tx(priv->ioaddr);
  787. priv->dev->stats.tx_errors++;
  788. netif_wake_queue(priv->dev);
  789. }
  790. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  791. {
  792. int status;
  793. status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
  794. if (likely((status & handle_rx)) || (status & handle_tx)) {
  795. if (likely(napi_schedule_prep(&priv->napi))) {
  796. stmmac_disable_dma_irq(priv);
  797. __napi_schedule(&priv->napi);
  798. }
  799. }
  800. if (unlikely(status & tx_hard_error_bump_tc)) {
  801. /* Try to bump up the dma threshold on this failure */
  802. if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
  803. tc += 64;
  804. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  805. priv->xstats.threshold = tc;
  806. }
  807. } else if (unlikely(status == tx_hard_error))
  808. stmmac_tx_err(priv);
  809. }
  810. static void stmmac_mmc_setup(struct stmmac_priv *priv)
  811. {
  812. unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
  813. MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
  814. /* Mask MMC irq, counters are managed in SW and registers
  815. * are cleared on each READ eventually. */
  816. dwmac_mmc_intr_all_mask(priv->ioaddr);
  817. if (priv->dma_cap.rmon) {
  818. dwmac_mmc_ctrl(priv->ioaddr, mode);
  819. memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
  820. } else
  821. pr_info(" No MAC Management Counters available\n");
  822. }
  823. static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
  824. {
  825. u32 hwid = priv->hw->synopsys_uid;
  826. /* Only check valid Synopsys Id because old MAC chips
  827. * have no HW registers where get the ID */
  828. if (likely(hwid)) {
  829. u32 uid = ((hwid & 0x0000ff00) >> 8);
  830. u32 synid = (hwid & 0x000000ff);
  831. pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
  832. uid, synid);
  833. return synid;
  834. }
  835. return 0;
  836. }
  837. /**
  838. * stmmac_selec_desc_mode
  839. * @priv : private structure
  840. * Description: select the Enhanced/Alternate or Normal descriptors
  841. */
  842. static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
  843. {
  844. if (priv->plat->enh_desc) {
  845. pr_info(" Enhanced/Alternate descriptors\n");
  846. /* GMAC older than 3.50 has no extended descriptors */
  847. if (priv->synopsys_id >= DWMAC_CORE_3_50) {
  848. pr_info("\tEnabled extended descriptors\n");
  849. priv->extend_desc = 1;
  850. } else
  851. pr_warn("Extended descriptors not supported\n");
  852. priv->hw->desc = &enh_desc_ops;
  853. } else {
  854. pr_info(" Normal descriptors\n");
  855. priv->hw->desc = &ndesc_ops;
  856. }
  857. }
  858. /**
  859. * stmmac_get_hw_features
  860. * @priv : private device pointer
  861. * Description:
  862. * new GMAC chip generations have a new register to indicate the
  863. * presence of the optional feature/functions.
  864. * This can be also used to override the value passed through the
  865. * platform and necessary for old MAC10/100 and GMAC chips.
  866. */
  867. static int stmmac_get_hw_features(struct stmmac_priv *priv)
  868. {
  869. u32 hw_cap = 0;
  870. if (priv->hw->dma->get_hw_feature) {
  871. hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
  872. priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
  873. priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
  874. priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
  875. priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
  876. priv->dma_cap.multi_addr =
  877. (hw_cap & DMA_HW_FEAT_ADDMACADRSEL) >> 5;
  878. priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
  879. priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
  880. priv->dma_cap.pmt_remote_wake_up =
  881. (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
  882. priv->dma_cap.pmt_magic_frame =
  883. (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
  884. /* MMC */
  885. priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
  886. /* IEEE 1588-2002*/
  887. priv->dma_cap.time_stamp =
  888. (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
  889. /* IEEE 1588-2008*/
  890. priv->dma_cap.atime_stamp =
  891. (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
  892. /* 802.3az - Energy-Efficient Ethernet (EEE) */
  893. priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
  894. priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
  895. /* TX and RX csum */
  896. priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
  897. priv->dma_cap.rx_coe_type1 =
  898. (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
  899. priv->dma_cap.rx_coe_type2 =
  900. (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
  901. priv->dma_cap.rxfifo_over_2048 =
  902. (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
  903. /* TX and RX number of channels */
  904. priv->dma_cap.number_rx_channel =
  905. (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
  906. priv->dma_cap.number_tx_channel =
  907. (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
  908. /* Alternate (enhanced) DESC mode*/
  909. priv->dma_cap.enh_desc =
  910. (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
  911. }
  912. return hw_cap;
  913. }
  914. static void stmmac_check_ether_addr(struct stmmac_priv *priv)
  915. {
  916. /* verify if the MAC address is valid, in case of failures it
  917. * generates a random MAC address */
  918. if (!is_valid_ether_addr(priv->dev->dev_addr)) {
  919. priv->hw->mac->get_umac_addr((void __iomem *)
  920. priv->dev->base_addr,
  921. priv->dev->dev_addr, 0);
  922. if (!is_valid_ether_addr(priv->dev->dev_addr))
  923. eth_hw_addr_random(priv->dev);
  924. }
  925. pr_warning("%s: device MAC address %pM\n", priv->dev->name,
  926. priv->dev->dev_addr);
  927. }
  928. static int stmmac_init_dma_engine(struct stmmac_priv *priv)
  929. {
  930. int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
  931. int mixed_burst = 0;
  932. int atds = 0;
  933. /* Some DMA parameters can be passed from the platform;
  934. * in case of these are not passed we keep a default
  935. * (good for all the chips) and init the DMA! */
  936. if (priv->plat->dma_cfg) {
  937. pbl = priv->plat->dma_cfg->pbl;
  938. fixed_burst = priv->plat->dma_cfg->fixed_burst;
  939. mixed_burst = priv->plat->dma_cfg->mixed_burst;
  940. burst_len = priv->plat->dma_cfg->burst_len;
  941. }
  942. if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
  943. atds = 1;
  944. return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
  945. burst_len, priv->dma_tx_phy,
  946. priv->dma_rx_phy, atds);
  947. }
  948. /**
  949. * stmmac_tx_timer:
  950. * @data: data pointer
  951. * Description:
  952. * This is the timer handler to directly invoke the stmmac_tx_clean.
  953. */
  954. static void stmmac_tx_timer(unsigned long data)
  955. {
  956. struct stmmac_priv *priv = (struct stmmac_priv *)data;
  957. stmmac_tx_clean(priv);
  958. }
  959. /**
  960. * stmmac_tx_timer:
  961. * @priv: private data structure
  962. * Description:
  963. * This inits the transmit coalesce parameters: i.e. timer rate,
  964. * timer handler and default threshold used for enabling the
  965. * interrupt on completion bit.
  966. */
  967. static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
  968. {
  969. priv->tx_coal_frames = STMMAC_TX_FRAMES;
  970. priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
  971. init_timer(&priv->txtimer);
  972. priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
  973. priv->txtimer.data = (unsigned long)priv;
  974. priv->txtimer.function = stmmac_tx_timer;
  975. add_timer(&priv->txtimer);
  976. }
  977. /**
  978. * stmmac_open - open entry point of the driver
  979. * @dev : pointer to the device structure.
  980. * Description:
  981. * This function is the open entry point of the driver.
  982. * Return value:
  983. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  984. * file on failure.
  985. */
  986. static int stmmac_open(struct net_device *dev)
  987. {
  988. struct stmmac_priv *priv = netdev_priv(dev);
  989. int ret;
  990. clk_prepare_enable(priv->stmmac_clk);
  991. stmmac_check_ether_addr(priv);
  992. ret = stmmac_init_phy(dev);
  993. if (unlikely(ret)) {
  994. pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
  995. goto open_error;
  996. }
  997. /* Create and initialize the TX/RX descriptors chains. */
  998. priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
  999. priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
  1000. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  1001. init_dma_desc_rings(dev);
  1002. /* DMA initialization and SW reset */
  1003. ret = stmmac_init_dma_engine(priv);
  1004. if (ret < 0) {
  1005. pr_err("%s: DMA initialization failed\n", __func__);
  1006. goto open_error;
  1007. }
  1008. /* Copy the MAC addr into the HW */
  1009. priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
  1010. /* If required, perform hw setup of the bus. */
  1011. if (priv->plat->bus_setup)
  1012. priv->plat->bus_setup(priv->ioaddr);
  1013. /* Initialize the MAC Core */
  1014. priv->hw->mac->core_init(priv->ioaddr);
  1015. /* Request the IRQ lines */
  1016. ret = request_irq(dev->irq, stmmac_interrupt,
  1017. IRQF_SHARED, dev->name, dev);
  1018. if (unlikely(ret < 0)) {
  1019. pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
  1020. __func__, dev->irq, ret);
  1021. goto open_error;
  1022. }
  1023. /* Request the Wake IRQ in case of another line is used for WoL */
  1024. if (priv->wol_irq != dev->irq) {
  1025. ret = request_irq(priv->wol_irq, stmmac_interrupt,
  1026. IRQF_SHARED, dev->name, dev);
  1027. if (unlikely(ret < 0)) {
  1028. pr_err("%s: ERROR: allocating the ext WoL IRQ %d "
  1029. "(error: %d)\n", __func__, priv->wol_irq, ret);
  1030. goto open_error_wolirq;
  1031. }
  1032. }
  1033. /* Request the IRQ lines */
  1034. if (priv->lpi_irq != -ENXIO) {
  1035. ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
  1036. dev->name, dev);
  1037. if (unlikely(ret < 0)) {
  1038. pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
  1039. __func__, priv->lpi_irq, ret);
  1040. goto open_error_lpiirq;
  1041. }
  1042. }
  1043. /* Enable the MAC Rx/Tx */
  1044. stmmac_set_mac(priv->ioaddr, true);
  1045. /* Set the HW DMA mode and the COE */
  1046. stmmac_dma_operation_mode(priv);
  1047. /* Extra statistics */
  1048. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  1049. priv->xstats.threshold = tc;
  1050. stmmac_mmc_setup(priv);
  1051. #ifdef CONFIG_STMMAC_DEBUG_FS
  1052. ret = stmmac_init_fs(dev);
  1053. if (ret < 0)
  1054. pr_warning("%s: failed debugFS registration\n", __func__);
  1055. #endif
  1056. /* Start the ball rolling... */
  1057. DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
  1058. priv->hw->dma->start_tx(priv->ioaddr);
  1059. priv->hw->dma->start_rx(priv->ioaddr);
  1060. /* Dump DMA/MAC registers */
  1061. if (netif_msg_hw(priv)) {
  1062. priv->hw->mac->dump_regs(priv->ioaddr);
  1063. priv->hw->dma->dump_regs(priv->ioaddr);
  1064. }
  1065. if (priv->phydev)
  1066. phy_start(priv->phydev);
  1067. priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS_TIMER;
  1068. priv->eee_enabled = stmmac_eee_init(priv);
  1069. stmmac_init_tx_coalesce(priv);
  1070. if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
  1071. priv->rx_riwt = MAX_DMA_RIWT;
  1072. priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
  1073. }
  1074. napi_enable(&priv->napi);
  1075. netif_start_queue(dev);
  1076. return 0;
  1077. open_error_lpiirq:
  1078. if (priv->wol_irq != dev->irq)
  1079. free_irq(priv->wol_irq, dev);
  1080. open_error_wolirq:
  1081. free_irq(dev->irq, dev);
  1082. open_error:
  1083. if (priv->phydev)
  1084. phy_disconnect(priv->phydev);
  1085. clk_disable_unprepare(priv->stmmac_clk);
  1086. return ret;
  1087. }
  1088. /**
  1089. * stmmac_release - close entry point of the driver
  1090. * @dev : device pointer.
  1091. * Description:
  1092. * This is the stop entry point of the driver.
  1093. */
  1094. static int stmmac_release(struct net_device *dev)
  1095. {
  1096. struct stmmac_priv *priv = netdev_priv(dev);
  1097. if (priv->eee_enabled)
  1098. del_timer_sync(&priv->eee_ctrl_timer);
  1099. /* Stop and disconnect the PHY */
  1100. if (priv->phydev) {
  1101. phy_stop(priv->phydev);
  1102. phy_disconnect(priv->phydev);
  1103. priv->phydev = NULL;
  1104. }
  1105. netif_stop_queue(dev);
  1106. napi_disable(&priv->napi);
  1107. del_timer_sync(&priv->txtimer);
  1108. /* Free the IRQ lines */
  1109. free_irq(dev->irq, dev);
  1110. if (priv->wol_irq != dev->irq)
  1111. free_irq(priv->wol_irq, dev);
  1112. if (priv->lpi_irq != -ENXIO)
  1113. free_irq(priv->lpi_irq, dev);
  1114. /* Stop TX/RX DMA and clear the descriptors */
  1115. priv->hw->dma->stop_tx(priv->ioaddr);
  1116. priv->hw->dma->stop_rx(priv->ioaddr);
  1117. /* Release and free the Rx/Tx resources */
  1118. free_dma_desc_resources(priv);
  1119. /* Disable the MAC Rx/Tx */
  1120. stmmac_set_mac(priv->ioaddr, false);
  1121. netif_carrier_off(dev);
  1122. #ifdef CONFIG_STMMAC_DEBUG_FS
  1123. stmmac_exit_fs();
  1124. #endif
  1125. clk_disable_unprepare(priv->stmmac_clk);
  1126. return 0;
  1127. }
  1128. /**
  1129. * stmmac_xmit:
  1130. * @skb : the socket buffer
  1131. * @dev : device pointer
  1132. * Description : Tx entry point of the driver.
  1133. */
  1134. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  1135. {
  1136. struct stmmac_priv *priv = netdev_priv(dev);
  1137. unsigned int txsize = priv->dma_tx_size;
  1138. unsigned int entry;
  1139. int i, csum_insertion = 0, is_jumbo = 0;
  1140. int nfrags = skb_shinfo(skb)->nr_frags;
  1141. struct dma_desc *desc, *first;
  1142. unsigned int nopaged_len = skb_headlen(skb);
  1143. if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
  1144. if (!netif_queue_stopped(dev)) {
  1145. netif_stop_queue(dev);
  1146. /* This is a hard error, log it. */
  1147. pr_err("%s: BUG! Tx Ring full when queue awake\n",
  1148. __func__);
  1149. }
  1150. return NETDEV_TX_BUSY;
  1151. }
  1152. spin_lock(&priv->tx_lock);
  1153. if (priv->tx_path_in_lpi_mode)
  1154. stmmac_disable_eee_mode(priv);
  1155. entry = priv->cur_tx % txsize;
  1156. #ifdef STMMAC_XMIT_DEBUG
  1157. if ((skb->len > ETH_FRAME_LEN) || nfrags)
  1158. pr_debug("stmmac xmit: [entry %d]\n"
  1159. "\tskb addr %p - len: %d - nopaged_len: %d\n"
  1160. "\tn_frags: %d - ip_summed: %d - %s gso\n"
  1161. "\ttx_count_frames %d\n", entry,
  1162. skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
  1163. !skb_is_gso(skb) ? "isn't" : "is",
  1164. priv->tx_count_frames);
  1165. #endif
  1166. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  1167. if (priv->extend_desc)
  1168. desc = (struct dma_desc *) (priv->dma_etx + entry);
  1169. else
  1170. desc = priv->dma_tx + entry;
  1171. first = desc;
  1172. #ifdef STMMAC_XMIT_DEBUG
  1173. if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
  1174. pr_debug("\tskb len: %d, nopaged_len: %d,\n"
  1175. "\t\tn_frags: %d, ip_summed: %d\n",
  1176. skb->len, nopaged_len, nfrags, skb->ip_summed);
  1177. #endif
  1178. priv->tx_skbuff[entry] = skb;
  1179. /* To program the descriptors according to the size of the frame */
  1180. if (priv->mode == STMMAC_RING_MODE) {
  1181. is_jumbo = priv->hw->ring->is_jumbo_frm(skb->len,
  1182. priv->plat->enh_desc);
  1183. if (unlikely(is_jumbo))
  1184. entry = priv->hw->ring->jumbo_frm(priv, skb,
  1185. csum_insertion);
  1186. } else {
  1187. is_jumbo = priv->hw->chain->is_jumbo_frm(skb->len,
  1188. priv->plat->enh_desc);
  1189. if (unlikely(is_jumbo))
  1190. entry = priv->hw->chain->jumbo_frm(priv, skb,
  1191. csum_insertion);
  1192. }
  1193. if (likely(!is_jumbo)) {
  1194. desc->des2 = dma_map_single(priv->device, skb->data,
  1195. nopaged_len, DMA_TO_DEVICE);
  1196. priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
  1197. csum_insertion, priv->mode);
  1198. } else
  1199. desc = first;
  1200. for (i = 0; i < nfrags; i++) {
  1201. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1202. int len = skb_frag_size(frag);
  1203. entry = (++priv->cur_tx) % txsize;
  1204. if (priv->extend_desc)
  1205. desc = (struct dma_desc *) (priv->dma_etx + entry);
  1206. else
  1207. desc = priv->dma_tx + entry;
  1208. TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
  1209. desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
  1210. DMA_TO_DEVICE);
  1211. priv->tx_skbuff[entry] = NULL;
  1212. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
  1213. priv->mode);
  1214. wmb();
  1215. priv->hw->desc->set_tx_owner(desc);
  1216. wmb();
  1217. }
  1218. /* Finalize the latest segment. */
  1219. priv->hw->desc->close_tx_desc(desc);
  1220. wmb();
  1221. /* According to the coalesce parameter the IC bit for the latest
  1222. * segment could be reset and the timer re-started to invoke the
  1223. * stmmac_tx function. This approach takes care about the fragments.
  1224. */
  1225. priv->tx_count_frames += nfrags + 1;
  1226. if (priv->tx_coal_frames > priv->tx_count_frames) {
  1227. priv->hw->desc->clear_tx_ic(desc);
  1228. priv->xstats.tx_reset_ic_bit++;
  1229. TX_DBG("\t[entry %d]: tx_count_frames %d\n", entry,
  1230. priv->tx_count_frames);
  1231. mod_timer(&priv->txtimer,
  1232. STMMAC_COAL_TIMER(priv->tx_coal_timer));
  1233. } else
  1234. priv->tx_count_frames = 0;
  1235. /* To avoid raise condition */
  1236. priv->hw->desc->set_tx_owner(first);
  1237. wmb();
  1238. priv->cur_tx++;
  1239. #ifdef STMMAC_XMIT_DEBUG
  1240. if (netif_msg_pktdata(priv)) {
  1241. pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
  1242. "first=%p, nfrags=%d\n",
  1243. (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
  1244. entry, first, nfrags);
  1245. if (priv->extend_desc)
  1246. stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
  1247. else
  1248. stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
  1249. pr_info(">>> frame to be transmitted: ");
  1250. print_pkt(skb->data, skb->len);
  1251. }
  1252. #endif
  1253. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  1254. TX_DBG("%s: stop transmitted packets\n", __func__);
  1255. netif_stop_queue(dev);
  1256. }
  1257. dev->stats.tx_bytes += skb->len;
  1258. skb_tx_timestamp(skb);
  1259. priv->hw->dma->enable_dma_transmission(priv->ioaddr);
  1260. spin_unlock(&priv->tx_lock);
  1261. return NETDEV_TX_OK;
  1262. }
  1263. static inline void stmmac_rx_refill(struct stmmac_priv *priv)
  1264. {
  1265. unsigned int rxsize = priv->dma_rx_size;
  1266. int bfsize = priv->dma_buf_sz;
  1267. for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
  1268. unsigned int entry = priv->dirty_rx % rxsize;
  1269. struct dma_desc *p;
  1270. if (priv->extend_desc)
  1271. p = (struct dma_desc *) (priv->dma_erx + entry);
  1272. else
  1273. p = priv->dma_rx + entry;
  1274. if (likely(priv->rx_skbuff[entry] == NULL)) {
  1275. struct sk_buff *skb;
  1276. skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
  1277. if (unlikely(skb == NULL))
  1278. break;
  1279. priv->rx_skbuff[entry] = skb;
  1280. priv->rx_skbuff_dma[entry] =
  1281. dma_map_single(priv->device, skb->data, bfsize,
  1282. DMA_FROM_DEVICE);
  1283. p->des2 = priv->rx_skbuff_dma[entry];
  1284. if (unlikely((priv->mode == STMMAC_RING_MODE) &&
  1285. (priv->plat->has_gmac)))
  1286. priv->hw->ring->refill_desc3(bfsize, p);
  1287. RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
  1288. }
  1289. wmb();
  1290. priv->hw->desc->set_rx_owner(p);
  1291. wmb();
  1292. }
  1293. }
  1294. static int stmmac_rx(struct stmmac_priv *priv, int limit)
  1295. {
  1296. unsigned int rxsize = priv->dma_rx_size;
  1297. unsigned int entry = priv->cur_rx % rxsize;
  1298. unsigned int next_entry;
  1299. unsigned int count = 0;
  1300. #ifdef STMMAC_RX_DEBUG
  1301. if (netif_msg_hw(priv)) {
  1302. pr_debug(">>> stmmac_rx: descriptor ring:\n");
  1303. if (priv->extend_desc)
  1304. stmmac_display_ring((void *) priv->dma_erx, rxsize, 1);
  1305. else
  1306. stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
  1307. }
  1308. #endif
  1309. while (count < limit) {
  1310. int status;
  1311. struct dma_desc *p, *p_next;
  1312. if (priv->extend_desc)
  1313. p = (struct dma_desc *) (priv->dma_erx + entry);
  1314. else
  1315. p = priv->dma_rx + entry ;
  1316. if (priv->hw->desc->get_rx_owner(p))
  1317. break;
  1318. count++;
  1319. next_entry = (++priv->cur_rx) % rxsize;
  1320. if (priv->extend_desc)
  1321. p_next = (struct dma_desc *) (priv->dma_erx +
  1322. next_entry);
  1323. else
  1324. p_next = priv->dma_rx + next_entry;
  1325. prefetch(p_next);
  1326. /* read the status of the incoming frame */
  1327. status = priv->hw->desc->rx_status(&priv->dev->stats,
  1328. &priv->xstats, p);
  1329. if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
  1330. priv->hw->desc->rx_extended_status(&priv->dev->stats,
  1331. &priv->xstats,
  1332. priv->dma_erx +
  1333. entry);
  1334. if (unlikely(status == discard_frame))
  1335. priv->dev->stats.rx_errors++;
  1336. else {
  1337. struct sk_buff *skb;
  1338. int frame_len;
  1339. frame_len = priv->hw->desc->get_rx_frame_len(p,
  1340. priv->plat->rx_coe);
  1341. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  1342. * Type frames (LLC/LLC-SNAP) */
  1343. if (unlikely(status != llc_snap))
  1344. frame_len -= ETH_FCS_LEN;
  1345. #ifdef STMMAC_RX_DEBUG
  1346. if (frame_len > ETH_FRAME_LEN)
  1347. pr_debug("\tRX frame size %d, COE status: %d\n",
  1348. frame_len, status);
  1349. if (netif_msg_hw(priv))
  1350. pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
  1351. p, entry, p->des2);
  1352. #endif
  1353. skb = priv->rx_skbuff[entry];
  1354. if (unlikely(!skb)) {
  1355. pr_err("%s: Inconsistent Rx descriptor chain\n",
  1356. priv->dev->name);
  1357. priv->dev->stats.rx_dropped++;
  1358. break;
  1359. }
  1360. prefetch(skb->data - NET_IP_ALIGN);
  1361. priv->rx_skbuff[entry] = NULL;
  1362. skb_put(skb, frame_len);
  1363. dma_unmap_single(priv->device,
  1364. priv->rx_skbuff_dma[entry],
  1365. priv->dma_buf_sz, DMA_FROM_DEVICE);
  1366. #ifdef STMMAC_RX_DEBUG
  1367. if (netif_msg_pktdata(priv)) {
  1368. pr_info(" frame received (%dbytes)", frame_len);
  1369. print_pkt(skb->data, frame_len);
  1370. }
  1371. #endif
  1372. skb->protocol = eth_type_trans(skb, priv->dev);
  1373. if (unlikely(!priv->plat->rx_coe))
  1374. skb_checksum_none_assert(skb);
  1375. else
  1376. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1377. napi_gro_receive(&priv->napi, skb);
  1378. priv->dev->stats.rx_packets++;
  1379. priv->dev->stats.rx_bytes += frame_len;
  1380. }
  1381. entry = next_entry;
  1382. }
  1383. stmmac_rx_refill(priv);
  1384. priv->xstats.rx_pkt_n += count;
  1385. return count;
  1386. }
  1387. /**
  1388. * stmmac_poll - stmmac poll method (NAPI)
  1389. * @napi : pointer to the napi structure.
  1390. * @budget : maximum number of packets that the current CPU can receive from
  1391. * all interfaces.
  1392. * Description :
  1393. * To look at the incoming frames and clear the tx resources.
  1394. */
  1395. static int stmmac_poll(struct napi_struct *napi, int budget)
  1396. {
  1397. struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
  1398. int work_done = 0;
  1399. priv->xstats.napi_poll++;
  1400. stmmac_tx_clean(priv);
  1401. work_done = stmmac_rx(priv, budget);
  1402. if (work_done < budget) {
  1403. napi_complete(napi);
  1404. stmmac_enable_dma_irq(priv);
  1405. }
  1406. return work_done;
  1407. }
  1408. /**
  1409. * stmmac_tx_timeout
  1410. * @dev : Pointer to net device structure
  1411. * Description: this function is called when a packet transmission fails to
  1412. * complete within a reasonable time. The driver will mark the error in the
  1413. * netdev structure and arrange for the device to be reset to a sane state
  1414. * in order to transmit a new packet.
  1415. */
  1416. static void stmmac_tx_timeout(struct net_device *dev)
  1417. {
  1418. struct stmmac_priv *priv = netdev_priv(dev);
  1419. /* Clear Tx resources and restart transmitting again */
  1420. stmmac_tx_err(priv);
  1421. }
  1422. /* Configuration changes (passed on by ifconfig) */
  1423. static int stmmac_config(struct net_device *dev, struct ifmap *map)
  1424. {
  1425. if (dev->flags & IFF_UP) /* can't act on a running interface */
  1426. return -EBUSY;
  1427. /* Don't allow changing the I/O address */
  1428. if (map->base_addr != dev->base_addr) {
  1429. pr_warning("%s: can't change I/O address\n", dev->name);
  1430. return -EOPNOTSUPP;
  1431. }
  1432. /* Don't allow changing the IRQ */
  1433. if (map->irq != dev->irq) {
  1434. pr_warning("%s: can't change IRQ number %d\n",
  1435. dev->name, dev->irq);
  1436. return -EOPNOTSUPP;
  1437. }
  1438. /* ignore other fields */
  1439. return 0;
  1440. }
  1441. /**
  1442. * stmmac_set_rx_mode - entry point for multicast addressing
  1443. * @dev : pointer to the device structure
  1444. * Description:
  1445. * This function is a driver entry point which gets called by the kernel
  1446. * whenever multicast addresses must be enabled/disabled.
  1447. * Return value:
  1448. * void.
  1449. */
  1450. static void stmmac_set_rx_mode(struct net_device *dev)
  1451. {
  1452. struct stmmac_priv *priv = netdev_priv(dev);
  1453. spin_lock(&priv->lock);
  1454. priv->hw->mac->set_filter(dev, priv->synopsys_id);
  1455. spin_unlock(&priv->lock);
  1456. }
  1457. /**
  1458. * stmmac_change_mtu - entry point to change MTU size for the device.
  1459. * @dev : device pointer.
  1460. * @new_mtu : the new MTU size for the device.
  1461. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1462. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1463. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1464. * Return value:
  1465. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1466. * file on failure.
  1467. */
  1468. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  1469. {
  1470. struct stmmac_priv *priv = netdev_priv(dev);
  1471. int max_mtu;
  1472. if (netif_running(dev)) {
  1473. pr_err("%s: must be stopped to change its MTU\n", dev->name);
  1474. return -EBUSY;
  1475. }
  1476. if (priv->plat->enh_desc)
  1477. max_mtu = JUMBO_LEN;
  1478. else
  1479. max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  1480. if ((new_mtu < 46) || (new_mtu > max_mtu)) {
  1481. pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
  1482. return -EINVAL;
  1483. }
  1484. dev->mtu = new_mtu;
  1485. netdev_update_features(dev);
  1486. return 0;
  1487. }
  1488. static netdev_features_t stmmac_fix_features(struct net_device *dev,
  1489. netdev_features_t features)
  1490. {
  1491. struct stmmac_priv *priv = netdev_priv(dev);
  1492. if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
  1493. features &= ~NETIF_F_RXCSUM;
  1494. else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
  1495. features &= ~NETIF_F_IPV6_CSUM;
  1496. if (!priv->plat->tx_coe)
  1497. features &= ~NETIF_F_ALL_CSUM;
  1498. /* Some GMAC devices have a bugged Jumbo frame support that
  1499. * needs to have the Tx COE disabled for oversized frames
  1500. * (due to limited buffer sizes). In this case we disable
  1501. * the TX csum insertionin the TDES and not use SF. */
  1502. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  1503. features &= ~NETIF_F_ALL_CSUM;
  1504. return features;
  1505. }
  1506. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  1507. {
  1508. struct net_device *dev = (struct net_device *)dev_id;
  1509. struct stmmac_priv *priv = netdev_priv(dev);
  1510. if (unlikely(!dev)) {
  1511. pr_err("%s: invalid dev pointer\n", __func__);
  1512. return IRQ_NONE;
  1513. }
  1514. /* To handle GMAC own interrupts */
  1515. if (priv->plat->has_gmac) {
  1516. int status = priv->hw->mac->host_irq_status((void __iomem *)
  1517. dev->base_addr);
  1518. if (unlikely(status)) {
  1519. if (status & core_mmc_tx_irq)
  1520. priv->xstats.mmc_tx_irq_n++;
  1521. if (status & core_mmc_rx_irq)
  1522. priv->xstats.mmc_rx_irq_n++;
  1523. if (status & core_mmc_rx_csum_offload_irq)
  1524. priv->xstats.mmc_rx_csum_offload_irq_n++;
  1525. if (status & core_irq_receive_pmt_irq)
  1526. priv->xstats.irq_receive_pmt_irq_n++;
  1527. /* For LPI we need to save the tx status */
  1528. if (status & core_irq_tx_path_in_lpi_mode) {
  1529. priv->xstats.irq_tx_path_in_lpi_mode_n++;
  1530. priv->tx_path_in_lpi_mode = true;
  1531. }
  1532. if (status & core_irq_tx_path_exit_lpi_mode) {
  1533. priv->xstats.irq_tx_path_exit_lpi_mode_n++;
  1534. priv->tx_path_in_lpi_mode = false;
  1535. }
  1536. if (status & core_irq_rx_path_in_lpi_mode)
  1537. priv->xstats.irq_rx_path_in_lpi_mode_n++;
  1538. if (status & core_irq_rx_path_exit_lpi_mode)
  1539. priv->xstats.irq_rx_path_exit_lpi_mode_n++;
  1540. }
  1541. }
  1542. /* To handle DMA interrupts */
  1543. stmmac_dma_interrupt(priv);
  1544. return IRQ_HANDLED;
  1545. }
  1546. #ifdef CONFIG_NET_POLL_CONTROLLER
  1547. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1548. * to allow network I/O with interrupts disabled. */
  1549. static void stmmac_poll_controller(struct net_device *dev)
  1550. {
  1551. disable_irq(dev->irq);
  1552. stmmac_interrupt(dev->irq, dev);
  1553. enable_irq(dev->irq);
  1554. }
  1555. #endif
  1556. /**
  1557. * stmmac_ioctl - Entry point for the Ioctl
  1558. * @dev: Device pointer.
  1559. * @rq: An IOCTL specefic structure, that can contain a pointer to
  1560. * a proprietary structure used to pass information to the driver.
  1561. * @cmd: IOCTL command
  1562. * Description:
  1563. * Currently there are no special functionality supported in IOCTL, just the
  1564. * phy_mii_ioctl(...) can be invoked.
  1565. */
  1566. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1567. {
  1568. struct stmmac_priv *priv = netdev_priv(dev);
  1569. int ret;
  1570. if (!netif_running(dev))
  1571. return -EINVAL;
  1572. if (!priv->phydev)
  1573. return -EINVAL;
  1574. ret = phy_mii_ioctl(priv->phydev, rq, cmd);
  1575. return ret;
  1576. }
  1577. #ifdef CONFIG_STMMAC_DEBUG_FS
  1578. static struct dentry *stmmac_fs_dir;
  1579. static struct dentry *stmmac_rings_status;
  1580. static struct dentry *stmmac_dma_cap;
  1581. static void sysfs_display_ring(void *head, int size, int extend_desc,
  1582. struct seq_file *seq)
  1583. {
  1584. int i;
  1585. struct dma_extended_desc *ep = (struct dma_extended_desc *) head;
  1586. struct dma_desc *p = (struct dma_desc *) head;
  1587. for (i = 0; i < size; i++) {
  1588. u64 x;
  1589. if (extend_desc) {
  1590. x = *(u64 *) ep;
  1591. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  1592. i, (unsigned int) virt_to_phys(ep),
  1593. (unsigned int) x, (unsigned int) (x >> 32),
  1594. ep->basic.des2, ep->basic.des3);
  1595. ep++;
  1596. } else {
  1597. x = *(u64 *) p;
  1598. seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
  1599. i, (unsigned int) virt_to_phys(ep),
  1600. (unsigned int) x, (unsigned int) (x >> 32),
  1601. p->des2, p->des3);
  1602. p++;
  1603. }
  1604. seq_printf(seq, "\n");
  1605. }
  1606. }
  1607. static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
  1608. {
  1609. struct net_device *dev = seq->private;
  1610. struct stmmac_priv *priv = netdev_priv(dev);
  1611. unsigned int txsize = priv->dma_tx_size;
  1612. unsigned int rxsize = priv->dma_rx_size;
  1613. if (priv->extend_desc) {
  1614. seq_printf(seq, "Extended RX descriptor ring:\n");
  1615. sysfs_display_ring((void *) priv->dma_erx, rxsize, 1, seq);
  1616. seq_printf(seq, "Extended TX descriptor ring:\n");
  1617. sysfs_display_ring((void *) priv->dma_etx, txsize, 1, seq);
  1618. } else {
  1619. seq_printf(seq, "RX descriptor ring:\n");
  1620. sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
  1621. seq_printf(seq, "TX descriptor ring:\n");
  1622. sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
  1623. }
  1624. return 0;
  1625. }
  1626. static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
  1627. {
  1628. return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
  1629. }
  1630. static const struct file_operations stmmac_rings_status_fops = {
  1631. .owner = THIS_MODULE,
  1632. .open = stmmac_sysfs_ring_open,
  1633. .read = seq_read,
  1634. .llseek = seq_lseek,
  1635. .release = single_release,
  1636. };
  1637. static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
  1638. {
  1639. struct net_device *dev = seq->private;
  1640. struct stmmac_priv *priv = netdev_priv(dev);
  1641. if (!priv->hw_cap_support) {
  1642. seq_printf(seq, "DMA HW features not supported\n");
  1643. return 0;
  1644. }
  1645. seq_printf(seq, "==============================\n");
  1646. seq_printf(seq, "\tDMA HW features\n");
  1647. seq_printf(seq, "==============================\n");
  1648. seq_printf(seq, "\t10/100 Mbps %s\n",
  1649. (priv->dma_cap.mbps_10_100) ? "Y" : "N");
  1650. seq_printf(seq, "\t1000 Mbps %s\n",
  1651. (priv->dma_cap.mbps_1000) ? "Y" : "N");
  1652. seq_printf(seq, "\tHalf duple %s\n",
  1653. (priv->dma_cap.half_duplex) ? "Y" : "N");
  1654. seq_printf(seq, "\tHash Filter: %s\n",
  1655. (priv->dma_cap.hash_filter) ? "Y" : "N");
  1656. seq_printf(seq, "\tMultiple MAC address registers: %s\n",
  1657. (priv->dma_cap.multi_addr) ? "Y" : "N");
  1658. seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
  1659. (priv->dma_cap.pcs) ? "Y" : "N");
  1660. seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
  1661. (priv->dma_cap.sma_mdio) ? "Y" : "N");
  1662. seq_printf(seq, "\tPMT Remote wake up: %s\n",
  1663. (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
  1664. seq_printf(seq, "\tPMT Magic Frame: %s\n",
  1665. (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
  1666. seq_printf(seq, "\tRMON module: %s\n",
  1667. (priv->dma_cap.rmon) ? "Y" : "N");
  1668. seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
  1669. (priv->dma_cap.time_stamp) ? "Y" : "N");
  1670. seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
  1671. (priv->dma_cap.atime_stamp) ? "Y" : "N");
  1672. seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
  1673. (priv->dma_cap.eee) ? "Y" : "N");
  1674. seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
  1675. seq_printf(seq, "\tChecksum Offload in TX: %s\n",
  1676. (priv->dma_cap.tx_coe) ? "Y" : "N");
  1677. seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
  1678. (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
  1679. seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
  1680. (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
  1681. seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
  1682. (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
  1683. seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
  1684. priv->dma_cap.number_rx_channel);
  1685. seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
  1686. priv->dma_cap.number_tx_channel);
  1687. seq_printf(seq, "\tEnhanced descriptors: %s\n",
  1688. (priv->dma_cap.enh_desc) ? "Y" : "N");
  1689. return 0;
  1690. }
  1691. static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
  1692. {
  1693. return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
  1694. }
  1695. static const struct file_operations stmmac_dma_cap_fops = {
  1696. .owner = THIS_MODULE,
  1697. .open = stmmac_sysfs_dma_cap_open,
  1698. .read = seq_read,
  1699. .llseek = seq_lseek,
  1700. .release = single_release,
  1701. };
  1702. static int stmmac_init_fs(struct net_device *dev)
  1703. {
  1704. /* Create debugfs entries */
  1705. stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
  1706. if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
  1707. pr_err("ERROR %s, debugfs create directory failed\n",
  1708. STMMAC_RESOURCE_NAME);
  1709. return -ENOMEM;
  1710. }
  1711. /* Entry to report DMA RX/TX rings */
  1712. stmmac_rings_status = debugfs_create_file("descriptors_status",
  1713. S_IRUGO, stmmac_fs_dir, dev,
  1714. &stmmac_rings_status_fops);
  1715. if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
  1716. pr_info("ERROR creating stmmac ring debugfs file\n");
  1717. debugfs_remove(stmmac_fs_dir);
  1718. return -ENOMEM;
  1719. }
  1720. /* Entry to report the DMA HW features */
  1721. stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
  1722. dev, &stmmac_dma_cap_fops);
  1723. if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
  1724. pr_info("ERROR creating stmmac MMC debugfs file\n");
  1725. debugfs_remove(stmmac_rings_status);
  1726. debugfs_remove(stmmac_fs_dir);
  1727. return -ENOMEM;
  1728. }
  1729. return 0;
  1730. }
  1731. static void stmmac_exit_fs(void)
  1732. {
  1733. debugfs_remove(stmmac_rings_status);
  1734. debugfs_remove(stmmac_dma_cap);
  1735. debugfs_remove(stmmac_fs_dir);
  1736. }
  1737. #endif /* CONFIG_STMMAC_DEBUG_FS */
  1738. static const struct net_device_ops stmmac_netdev_ops = {
  1739. .ndo_open = stmmac_open,
  1740. .ndo_start_xmit = stmmac_xmit,
  1741. .ndo_stop = stmmac_release,
  1742. .ndo_change_mtu = stmmac_change_mtu,
  1743. .ndo_fix_features = stmmac_fix_features,
  1744. .ndo_set_rx_mode = stmmac_set_rx_mode,
  1745. .ndo_tx_timeout = stmmac_tx_timeout,
  1746. .ndo_do_ioctl = stmmac_ioctl,
  1747. .ndo_set_config = stmmac_config,
  1748. #ifdef CONFIG_NET_POLL_CONTROLLER
  1749. .ndo_poll_controller = stmmac_poll_controller,
  1750. #endif
  1751. .ndo_set_mac_address = eth_mac_addr,
  1752. };
  1753. /**
  1754. * stmmac_hw_init - Init the MAC device
  1755. * @priv : pointer to the private device structure.
  1756. * Description: this function detects which MAC device
  1757. * (GMAC/MAC10-100) has to attached, checks the HW capability
  1758. * (if supported) and sets the driver's features (for example
  1759. * to use the ring or chaine mode or support the normal/enh
  1760. * descriptor structure).
  1761. */
  1762. static int stmmac_hw_init(struct stmmac_priv *priv)
  1763. {
  1764. int ret;
  1765. struct mac_device_info *mac;
  1766. /* Identify the MAC HW device */
  1767. if (priv->plat->has_gmac) {
  1768. priv->dev->priv_flags |= IFF_UNICAST_FLT;
  1769. mac = dwmac1000_setup(priv->ioaddr);
  1770. } else {
  1771. mac = dwmac100_setup(priv->ioaddr);
  1772. }
  1773. if (!mac)
  1774. return -ENOMEM;
  1775. priv->hw = mac;
  1776. /* Get and dump the chip ID */
  1777. priv->synopsys_id = stmmac_get_synopsys_id(priv);
  1778. /* To use alternate (extended) or normal descriptor structures */
  1779. stmmac_selec_desc_mode(priv);
  1780. /* To use the chained or ring mode */
  1781. if (chain_mode) {
  1782. priv->hw->chain = &chain_mode_ops;
  1783. pr_info(" Chain mode enabled\n");
  1784. priv->mode = STMMAC_CHAIN_MODE;
  1785. } else {
  1786. priv->hw->ring = &ring_mode_ops;
  1787. pr_info(" Ring mode enabled\n");
  1788. priv->mode = STMMAC_RING_MODE;
  1789. }
  1790. /* Get the HW capability (new GMAC newer than 3.50a) */
  1791. priv->hw_cap_support = stmmac_get_hw_features(priv);
  1792. if (priv->hw_cap_support) {
  1793. pr_info(" DMA HW capability register supported");
  1794. /* We can override some gmac/dma configuration fields: e.g.
  1795. * enh_desc, tx_coe (e.g. that are passed through the
  1796. * platform) with the values from the HW capability
  1797. * register (if supported).
  1798. */
  1799. priv->plat->enh_desc = priv->dma_cap.enh_desc;
  1800. priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
  1801. priv->plat->tx_coe = priv->dma_cap.tx_coe;
  1802. if (priv->dma_cap.rx_coe_type2)
  1803. priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
  1804. else if (priv->dma_cap.rx_coe_type1)
  1805. priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
  1806. } else
  1807. pr_info(" No HW DMA feature register supported");
  1808. /* Enable the IPC (Checksum Offload) and check if the feature has been
  1809. * enabled during the core configuration. */
  1810. ret = priv->hw->mac->rx_ipc(priv->ioaddr);
  1811. if (!ret) {
  1812. pr_warning(" RX IPC Checksum Offload not configured.\n");
  1813. priv->plat->rx_coe = STMMAC_RX_COE_NONE;
  1814. }
  1815. if (priv->plat->rx_coe)
  1816. pr_info(" RX Checksum Offload Engine supported (type %d)\n",
  1817. priv->plat->rx_coe);
  1818. if (priv->plat->tx_coe)
  1819. pr_info(" TX Checksum insertion supported\n");
  1820. if (priv->plat->pmt) {
  1821. pr_info(" Wake-Up On Lan supported\n");
  1822. device_set_wakeup_capable(priv->device, 1);
  1823. }
  1824. return 0;
  1825. }
  1826. /**
  1827. * stmmac_dvr_probe
  1828. * @device: device pointer
  1829. * @plat_dat: platform data pointer
  1830. * @addr: iobase memory address
  1831. * Description: this is the main probe function used to
  1832. * call the alloc_etherdev, allocate the priv structure.
  1833. */
  1834. struct stmmac_priv *stmmac_dvr_probe(struct device *device,
  1835. struct plat_stmmacenet_data *plat_dat,
  1836. void __iomem *addr)
  1837. {
  1838. int ret = 0;
  1839. struct net_device *ndev = NULL;
  1840. struct stmmac_priv *priv;
  1841. ndev = alloc_etherdev(sizeof(struct stmmac_priv));
  1842. if (!ndev)
  1843. return NULL;
  1844. SET_NETDEV_DEV(ndev, device);
  1845. priv = netdev_priv(ndev);
  1846. priv->device = device;
  1847. priv->dev = ndev;
  1848. ether_setup(ndev);
  1849. stmmac_set_ethtool_ops(ndev);
  1850. priv->pause = pause;
  1851. priv->plat = plat_dat;
  1852. priv->ioaddr = addr;
  1853. priv->dev->base_addr = (unsigned long)addr;
  1854. /* Verify driver arguments */
  1855. stmmac_verify_args();
  1856. /* Override with kernel parameters if supplied XXX CRS XXX
  1857. * this needs to have multiple instances */
  1858. if ((phyaddr >= 0) && (phyaddr <= 31))
  1859. priv->plat->phy_addr = phyaddr;
  1860. /* Init MAC and get the capabilities */
  1861. ret = stmmac_hw_init(priv);
  1862. if (ret)
  1863. goto error_free_netdev;
  1864. ndev->netdev_ops = &stmmac_netdev_ops;
  1865. ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1866. NETIF_F_RXCSUM;
  1867. ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
  1868. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1869. #ifdef STMMAC_VLAN_TAG_USED
  1870. /* Both mac100 and gmac support receive VLAN tag detection */
  1871. ndev->features |= NETIF_F_HW_VLAN_RX;
  1872. #endif
  1873. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  1874. if (flow_ctrl)
  1875. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  1876. /* Rx Watchdog is available in the COREs newer than the 3.40.
  1877. * In some case, for example on bugged HW this feature
  1878. * has to be disable and this can be done by passing the
  1879. * riwt_off field from the platform.
  1880. */
  1881. if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
  1882. priv->use_riwt = 1;
  1883. pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
  1884. }
  1885. netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
  1886. spin_lock_init(&priv->lock);
  1887. spin_lock_init(&priv->tx_lock);
  1888. ret = register_netdev(ndev);
  1889. if (ret) {
  1890. pr_err("%s: ERROR %i registering the device\n", __func__, ret);
  1891. goto error_netdev_register;
  1892. }
  1893. priv->stmmac_clk = clk_get(priv->device, STMMAC_RESOURCE_NAME);
  1894. if (IS_ERR(priv->stmmac_clk)) {
  1895. pr_warning("%s: warning: cannot get CSR clock\n", __func__);
  1896. goto error_clk_get;
  1897. }
  1898. /* If a specific clk_csr value is passed from the platform
  1899. * this means that the CSR Clock Range selection cannot be
  1900. * changed at run-time and it is fixed. Viceversa the driver'll try to
  1901. * set the MDC clock dynamically according to the csr actual
  1902. * clock input.
  1903. */
  1904. if (!priv->plat->clk_csr)
  1905. stmmac_clk_csr_set(priv);
  1906. else
  1907. priv->clk_csr = priv->plat->clk_csr;
  1908. /* MDIO bus Registration */
  1909. ret = stmmac_mdio_register(ndev);
  1910. if (ret < 0) {
  1911. pr_debug("%s: MDIO bus (id: %d) registration failed",
  1912. __func__, priv->plat->bus_id);
  1913. goto error_mdio_register;
  1914. }
  1915. return priv;
  1916. error_mdio_register:
  1917. clk_put(priv->stmmac_clk);
  1918. error_clk_get:
  1919. unregister_netdev(ndev);
  1920. error_netdev_register:
  1921. netif_napi_del(&priv->napi);
  1922. error_free_netdev:
  1923. free_netdev(ndev);
  1924. return NULL;
  1925. }
  1926. /**
  1927. * stmmac_dvr_remove
  1928. * @ndev: net device pointer
  1929. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1930. * changes the link status, releases the DMA descriptor rings.
  1931. */
  1932. int stmmac_dvr_remove(struct net_device *ndev)
  1933. {
  1934. struct stmmac_priv *priv = netdev_priv(ndev);
  1935. pr_info("%s:\n\tremoving driver", __func__);
  1936. priv->hw->dma->stop_rx(priv->ioaddr);
  1937. priv->hw->dma->stop_tx(priv->ioaddr);
  1938. stmmac_set_mac(priv->ioaddr, false);
  1939. stmmac_mdio_unregister(ndev);
  1940. netif_carrier_off(ndev);
  1941. unregister_netdev(ndev);
  1942. free_netdev(ndev);
  1943. return 0;
  1944. }
  1945. #ifdef CONFIG_PM
  1946. int stmmac_suspend(struct net_device *ndev)
  1947. {
  1948. struct stmmac_priv *priv = netdev_priv(ndev);
  1949. unsigned long flags;
  1950. if (!ndev || !netif_running(ndev))
  1951. return 0;
  1952. if (priv->phydev)
  1953. phy_stop(priv->phydev);
  1954. spin_lock_irqsave(&priv->lock, flags);
  1955. netif_device_detach(ndev);
  1956. netif_stop_queue(ndev);
  1957. napi_disable(&priv->napi);
  1958. /* Stop TX/RX DMA */
  1959. priv->hw->dma->stop_tx(priv->ioaddr);
  1960. priv->hw->dma->stop_rx(priv->ioaddr);
  1961. stmmac_clear_descriptors(priv);
  1962. /* Enable Power down mode by programming the PMT regs */
  1963. if (device_may_wakeup(priv->device))
  1964. priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
  1965. else {
  1966. stmmac_set_mac(priv->ioaddr, false);
  1967. /* Disable clock in case of PWM is off */
  1968. clk_disable_unprepare(priv->stmmac_clk);
  1969. }
  1970. spin_unlock_irqrestore(&priv->lock, flags);
  1971. return 0;
  1972. }
  1973. int stmmac_resume(struct net_device *ndev)
  1974. {
  1975. struct stmmac_priv *priv = netdev_priv(ndev);
  1976. unsigned long flags;
  1977. if (!netif_running(ndev))
  1978. return 0;
  1979. spin_lock_irqsave(&priv->lock, flags);
  1980. /* Power Down bit, into the PM register, is cleared
  1981. * automatically as soon as a magic packet or a Wake-up frame
  1982. * is received. Anyway, it's better to manually clear
  1983. * this bit because it can generate problems while resuming
  1984. * from another devices (e.g. serial console). */
  1985. if (device_may_wakeup(priv->device))
  1986. priv->hw->mac->pmt(priv->ioaddr, 0);
  1987. else
  1988. /* enable the clk prevously disabled */
  1989. clk_prepare_enable(priv->stmmac_clk);
  1990. netif_device_attach(ndev);
  1991. /* Enable the MAC and DMA */
  1992. stmmac_set_mac(priv->ioaddr, true);
  1993. priv->hw->dma->start_tx(priv->ioaddr);
  1994. priv->hw->dma->start_rx(priv->ioaddr);
  1995. napi_enable(&priv->napi);
  1996. netif_start_queue(ndev);
  1997. spin_unlock_irqrestore(&priv->lock, flags);
  1998. if (priv->phydev)
  1999. phy_start(priv->phydev);
  2000. return 0;
  2001. }
  2002. int stmmac_freeze(struct net_device *ndev)
  2003. {
  2004. if (!ndev || !netif_running(ndev))
  2005. return 0;
  2006. return stmmac_release(ndev);
  2007. }
  2008. int stmmac_restore(struct net_device *ndev)
  2009. {
  2010. if (!ndev || !netif_running(ndev))
  2011. return 0;
  2012. return stmmac_open(ndev);
  2013. }
  2014. #endif /* CONFIG_PM */
  2015. /* Driver can be configured w/ and w/ both PCI and Platf drivers
  2016. * depending on the configuration selected.
  2017. */
  2018. static int __init stmmac_init(void)
  2019. {
  2020. int ret;
  2021. ret = stmmac_register_platform();
  2022. if (ret)
  2023. goto err;
  2024. ret = stmmac_register_pci();
  2025. if (ret)
  2026. goto err_pci;
  2027. return 0;
  2028. err_pci:
  2029. stmmac_unregister_platform();
  2030. err:
  2031. pr_err("stmmac: driver registration failed\n");
  2032. return ret;
  2033. }
  2034. static void __exit stmmac_exit(void)
  2035. {
  2036. stmmac_unregister_platform();
  2037. stmmac_unregister_pci();
  2038. }
  2039. module_init(stmmac_init);
  2040. module_exit(stmmac_exit);
  2041. #ifndef MODULE
  2042. static int __init stmmac_cmdline_opt(char *str)
  2043. {
  2044. char *opt;
  2045. if (!str || !*str)
  2046. return -EINVAL;
  2047. while ((opt = strsep(&str, ",")) != NULL) {
  2048. if (!strncmp(opt, "debug:", 6)) {
  2049. if (kstrtoint(opt + 6, 0, &debug))
  2050. goto err;
  2051. } else if (!strncmp(opt, "phyaddr:", 8)) {
  2052. if (kstrtoint(opt + 8, 0, &phyaddr))
  2053. goto err;
  2054. } else if (!strncmp(opt, "dma_txsize:", 11)) {
  2055. if (kstrtoint(opt + 11, 0, &dma_txsize))
  2056. goto err;
  2057. } else if (!strncmp(opt, "dma_rxsize:", 11)) {
  2058. if (kstrtoint(opt + 11, 0, &dma_rxsize))
  2059. goto err;
  2060. } else if (!strncmp(opt, "buf_sz:", 7)) {
  2061. if (kstrtoint(opt + 7, 0, &buf_sz))
  2062. goto err;
  2063. } else if (!strncmp(opt, "tc:", 3)) {
  2064. if (kstrtoint(opt + 3, 0, &tc))
  2065. goto err;
  2066. } else if (!strncmp(opt, "watchdog:", 9)) {
  2067. if (kstrtoint(opt + 9, 0, &watchdog))
  2068. goto err;
  2069. } else if (!strncmp(opt, "flow_ctrl:", 10)) {
  2070. if (kstrtoint(opt + 10, 0, &flow_ctrl))
  2071. goto err;
  2072. } else if (!strncmp(opt, "pause:", 6)) {
  2073. if (kstrtoint(opt + 6, 0, &pause))
  2074. goto err;
  2075. } else if (!strncmp(opt, "eee_timer:", 10)) {
  2076. if (kstrtoint(opt + 10, 0, &eee_timer))
  2077. goto err;
  2078. } else if (!strncmp(opt, "chain_mode:", 11)) {
  2079. if (kstrtoint(opt + 11, 0, &chain_mode))
  2080. goto err;
  2081. }
  2082. }
  2083. return 0;
  2084. err:
  2085. pr_err("%s: ERROR broken module parameter conversion", __func__);
  2086. return -EINVAL;
  2087. }
  2088. __setup("stmmaceth=", stmmac_cmdline_opt);
  2089. #endif
  2090. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
  2091. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  2092. MODULE_LICENSE("GPL");