stmmac_main.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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-2009 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/module.h>
  24. #include <linux/init.h>
  25. #include <linux/kernel.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/ip.h>
  30. #include <linux/tcp.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/crc32.h>
  35. #include <linux/mii.h>
  36. #include <linux/phy.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/slab.h>
  40. #include <linux/prefetch.h>
  41. #include "stmmac.h"
  42. #define STMMAC_RESOURCE_NAME "stmmaceth"
  43. #define PHY_RESOURCE_NAME "stmmacphy"
  44. #undef STMMAC_DEBUG
  45. /*#define STMMAC_DEBUG*/
  46. #ifdef STMMAC_DEBUG
  47. #define DBG(nlevel, klevel, fmt, args...) \
  48. ((void)(netif_msg_##nlevel(priv) && \
  49. printk(KERN_##klevel fmt, ## args)))
  50. #else
  51. #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
  52. #endif
  53. #undef STMMAC_RX_DEBUG
  54. /*#define STMMAC_RX_DEBUG*/
  55. #ifdef STMMAC_RX_DEBUG
  56. #define RX_DBG(fmt, args...) printk(fmt, ## args)
  57. #else
  58. #define RX_DBG(fmt, args...) do { } while (0)
  59. #endif
  60. #undef STMMAC_XMIT_DEBUG
  61. /*#define STMMAC_XMIT_DEBUG*/
  62. #ifdef STMMAC_TX_DEBUG
  63. #define TX_DBG(fmt, args...) printk(fmt, ## args)
  64. #else
  65. #define TX_DBG(fmt, args...) do { } while (0)
  66. #endif
  67. #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
  68. #define JUMBO_LEN 9000
  69. /* Module parameters */
  70. #define TX_TIMEO 5000 /* default 5 seconds */
  71. static int watchdog = TX_TIMEO;
  72. module_param(watchdog, int, S_IRUGO | S_IWUSR);
  73. MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
  74. static int debug = -1; /* -1: default, 0: no output, 16: all */
  75. module_param(debug, int, S_IRUGO | S_IWUSR);
  76. MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
  77. static int phyaddr = -1;
  78. module_param(phyaddr, int, S_IRUGO);
  79. MODULE_PARM_DESC(phyaddr, "Physical device address");
  80. #define DMA_TX_SIZE 256
  81. static int dma_txsize = DMA_TX_SIZE;
  82. module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
  83. MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
  84. #define DMA_RX_SIZE 256
  85. static int dma_rxsize = DMA_RX_SIZE;
  86. module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
  87. MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
  88. static int flow_ctrl = FLOW_OFF;
  89. module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
  90. MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
  91. static int pause = PAUSE_TIME;
  92. module_param(pause, int, S_IRUGO | S_IWUSR);
  93. MODULE_PARM_DESC(pause, "Flow Control Pause Time");
  94. #define TC_DEFAULT 64
  95. static int tc = TC_DEFAULT;
  96. module_param(tc, int, S_IRUGO | S_IWUSR);
  97. MODULE_PARM_DESC(tc, "DMA threshold control value");
  98. /* Pay attention to tune this parameter; take care of both
  99. * hardware capability and network stabitily/performance impact.
  100. * Many tests showed that ~4ms latency seems to be good enough. */
  101. #ifdef CONFIG_STMMAC_TIMER
  102. #define DEFAULT_PERIODIC_RATE 256
  103. static int tmrate = DEFAULT_PERIODIC_RATE;
  104. module_param(tmrate, int, S_IRUGO | S_IWUSR);
  105. MODULE_PARM_DESC(tmrate, "External timer freq. (default: 256Hz)");
  106. #endif
  107. #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
  108. static int buf_sz = DMA_BUFFER_SIZE;
  109. module_param(buf_sz, int, S_IRUGO | S_IWUSR);
  110. MODULE_PARM_DESC(buf_sz, "DMA buffer size");
  111. static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
  112. NETIF_MSG_LINK | NETIF_MSG_IFUP |
  113. NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
  114. static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
  115. /**
  116. * stmmac_verify_args - verify the driver parameters.
  117. * Description: it verifies if some wrong parameter is passed to the driver.
  118. * Note that wrong parameters are replaced with the default values.
  119. */
  120. static void stmmac_verify_args(void)
  121. {
  122. if (unlikely(watchdog < 0))
  123. watchdog = TX_TIMEO;
  124. if (unlikely(dma_rxsize < 0))
  125. dma_rxsize = DMA_RX_SIZE;
  126. if (unlikely(dma_txsize < 0))
  127. dma_txsize = DMA_TX_SIZE;
  128. if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
  129. buf_sz = DMA_BUFFER_SIZE;
  130. if (unlikely(flow_ctrl > 1))
  131. flow_ctrl = FLOW_AUTO;
  132. else if (likely(flow_ctrl < 0))
  133. flow_ctrl = FLOW_OFF;
  134. if (unlikely((pause < 0) || (pause > 0xffff)))
  135. pause = PAUSE_TIME;
  136. }
  137. #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
  138. static void print_pkt(unsigned char *buf, int len)
  139. {
  140. int j;
  141. pr_info("len = %d byte, buf addr: 0x%p", len, buf);
  142. for (j = 0; j < len; j++) {
  143. if ((j % 16) == 0)
  144. pr_info("\n %03x:", j);
  145. pr_info(" %02x", buf[j]);
  146. }
  147. pr_info("\n");
  148. }
  149. #endif
  150. /* minimum number of free TX descriptors required to wake up TX process */
  151. #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
  152. static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
  153. {
  154. return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
  155. }
  156. /* On some ST platforms, some HW system configuraton registers have to be
  157. * set according to the link speed negotiated.
  158. */
  159. static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
  160. {
  161. struct phy_device *phydev = priv->phydev;
  162. if (likely(priv->plat->fix_mac_speed))
  163. priv->plat->fix_mac_speed(priv->plat->bsp_priv,
  164. phydev->speed);
  165. }
  166. /**
  167. * stmmac_adjust_link
  168. * @dev: net device structure
  169. * Description: it adjusts the link parameters.
  170. */
  171. static void stmmac_adjust_link(struct net_device *dev)
  172. {
  173. struct stmmac_priv *priv = netdev_priv(dev);
  174. struct phy_device *phydev = priv->phydev;
  175. unsigned long flags;
  176. int new_state = 0;
  177. unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
  178. if (phydev == NULL)
  179. return;
  180. DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
  181. phydev->addr, phydev->link);
  182. spin_lock_irqsave(&priv->lock, flags);
  183. if (phydev->link) {
  184. u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
  185. /* Now we make sure that we can be in full duplex mode.
  186. * If not, we operate in half-duplex mode. */
  187. if (phydev->duplex != priv->oldduplex) {
  188. new_state = 1;
  189. if (!(phydev->duplex))
  190. ctrl &= ~priv->hw->link.duplex;
  191. else
  192. ctrl |= priv->hw->link.duplex;
  193. priv->oldduplex = phydev->duplex;
  194. }
  195. /* Flow Control operation */
  196. if (phydev->pause)
  197. priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
  198. fc, pause_time);
  199. if (phydev->speed != priv->speed) {
  200. new_state = 1;
  201. switch (phydev->speed) {
  202. case 1000:
  203. if (likely(priv->plat->has_gmac))
  204. ctrl &= ~priv->hw->link.port;
  205. stmmac_hw_fix_mac_speed(priv);
  206. break;
  207. case 100:
  208. case 10:
  209. if (priv->plat->has_gmac) {
  210. ctrl |= priv->hw->link.port;
  211. if (phydev->speed == SPEED_100) {
  212. ctrl |= priv->hw->link.speed;
  213. } else {
  214. ctrl &= ~(priv->hw->link.speed);
  215. }
  216. } else {
  217. ctrl &= ~priv->hw->link.port;
  218. }
  219. stmmac_hw_fix_mac_speed(priv);
  220. break;
  221. default:
  222. if (netif_msg_link(priv))
  223. pr_warning("%s: Speed (%d) is not 10"
  224. " or 100!\n", dev->name, phydev->speed);
  225. break;
  226. }
  227. priv->speed = phydev->speed;
  228. }
  229. writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
  230. if (!priv->oldlink) {
  231. new_state = 1;
  232. priv->oldlink = 1;
  233. }
  234. } else if (priv->oldlink) {
  235. new_state = 1;
  236. priv->oldlink = 0;
  237. priv->speed = 0;
  238. priv->oldduplex = -1;
  239. }
  240. if (new_state && netif_msg_link(priv))
  241. phy_print_status(phydev);
  242. spin_unlock_irqrestore(&priv->lock, flags);
  243. DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
  244. }
  245. /**
  246. * stmmac_init_phy - PHY initialization
  247. * @dev: net device structure
  248. * Description: it initializes the driver's PHY state, and attaches the PHY
  249. * to the mac driver.
  250. * Return value:
  251. * 0 on success
  252. */
  253. static int stmmac_init_phy(struct net_device *dev)
  254. {
  255. struct stmmac_priv *priv = netdev_priv(dev);
  256. struct phy_device *phydev;
  257. char phy_id[MII_BUS_ID_SIZE + 3];
  258. char bus_id[MII_BUS_ID_SIZE];
  259. priv->oldlink = 0;
  260. priv->speed = 0;
  261. priv->oldduplex = -1;
  262. if (priv->phy_addr == -1) {
  263. /* We don't have a PHY, so do nothing */
  264. return 0;
  265. }
  266. snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
  267. snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
  268. priv->phy_addr);
  269. pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
  270. phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
  271. priv->phy_interface);
  272. if (IS_ERR(phydev)) {
  273. pr_err("%s: Could not attach to PHY\n", dev->name);
  274. return PTR_ERR(phydev);
  275. }
  276. /*
  277. * Broken HW is sometimes missing the pull-up resistor on the
  278. * MDIO line, which results in reads to non-existent devices returning
  279. * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
  280. * device as well.
  281. * Note: phydev->phy_id is the result of reading the UID PHY registers.
  282. */
  283. if (phydev->phy_id == 0) {
  284. phy_disconnect(phydev);
  285. return -ENODEV;
  286. }
  287. pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
  288. " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
  289. priv->phydev = phydev;
  290. return 0;
  291. }
  292. static inline void stmmac_enable_mac(void __iomem *ioaddr)
  293. {
  294. u32 value = readl(ioaddr + MAC_CTRL_REG);
  295. value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
  296. writel(value, ioaddr + MAC_CTRL_REG);
  297. }
  298. static inline void stmmac_disable_mac(void __iomem *ioaddr)
  299. {
  300. u32 value = readl(ioaddr + MAC_CTRL_REG);
  301. value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
  302. writel(value, ioaddr + MAC_CTRL_REG);
  303. }
  304. /**
  305. * display_ring
  306. * @p: pointer to the ring.
  307. * @size: size of the ring.
  308. * Description: display all the descriptors within the ring.
  309. */
  310. static void display_ring(struct dma_desc *p, int size)
  311. {
  312. struct tmp_s {
  313. u64 a;
  314. unsigned int b;
  315. unsigned int c;
  316. };
  317. int i;
  318. for (i = 0; i < size; i++) {
  319. struct tmp_s *x = (struct tmp_s *)(p + i);
  320. pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
  321. i, (unsigned int)virt_to_phys(&p[i]),
  322. (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
  323. x->b, x->c);
  324. pr_info("\n");
  325. }
  326. }
  327. /**
  328. * init_dma_desc_rings - init the RX/TX descriptor rings
  329. * @dev: net device structure
  330. * Description: this function initializes the DMA RX/TX descriptors
  331. * and allocates the socket buffers.
  332. */
  333. static void init_dma_desc_rings(struct net_device *dev)
  334. {
  335. int i;
  336. struct stmmac_priv *priv = netdev_priv(dev);
  337. struct sk_buff *skb;
  338. unsigned int txsize = priv->dma_tx_size;
  339. unsigned int rxsize = priv->dma_rx_size;
  340. unsigned int bfsize = priv->dma_buf_sz;
  341. int buff2_needed = 0, dis_ic = 0;
  342. /* Set the Buffer size according to the MTU;
  343. * indeed, in case of jumbo we need to bump-up the buffer sizes.
  344. */
  345. if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
  346. bfsize = BUF_SIZE_16KiB;
  347. else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
  348. bfsize = BUF_SIZE_8KiB;
  349. else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
  350. bfsize = BUF_SIZE_4KiB;
  351. else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
  352. bfsize = BUF_SIZE_2KiB;
  353. else
  354. bfsize = DMA_BUFFER_SIZE;
  355. #ifdef CONFIG_STMMAC_TIMER
  356. /* Disable interrupts on completion for the reception if timer is on */
  357. if (likely(priv->tm->enable))
  358. dis_ic = 1;
  359. #endif
  360. /* If the MTU exceeds 8k so use the second buffer in the chain */
  361. if (bfsize >= BUF_SIZE_8KiB)
  362. buff2_needed = 1;
  363. DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
  364. txsize, rxsize, bfsize);
  365. priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
  366. priv->rx_skbuff =
  367. kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
  368. priv->dma_rx =
  369. (struct dma_desc *)dma_alloc_coherent(priv->device,
  370. rxsize *
  371. sizeof(struct dma_desc),
  372. &priv->dma_rx_phy,
  373. GFP_KERNEL);
  374. priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
  375. GFP_KERNEL);
  376. priv->dma_tx =
  377. (struct dma_desc *)dma_alloc_coherent(priv->device,
  378. txsize *
  379. sizeof(struct dma_desc),
  380. &priv->dma_tx_phy,
  381. GFP_KERNEL);
  382. if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
  383. pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
  384. return;
  385. }
  386. DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
  387. "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
  388. dev->name, priv->dma_rx, priv->dma_tx,
  389. (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
  390. /* RX INITIALIZATION */
  391. DBG(probe, INFO, "stmmac: SKB addresses:\n"
  392. "skb\t\tskb data\tdma data\n");
  393. for (i = 0; i < rxsize; i++) {
  394. struct dma_desc *p = priv->dma_rx + i;
  395. skb = netdev_alloc_skb_ip_align(dev, bfsize);
  396. if (unlikely(skb == NULL)) {
  397. pr_err("%s: Rx init fails; skb is NULL\n", __func__);
  398. break;
  399. }
  400. priv->rx_skbuff[i] = skb;
  401. priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
  402. bfsize, DMA_FROM_DEVICE);
  403. p->des2 = priv->rx_skbuff_dma[i];
  404. if (unlikely(buff2_needed))
  405. p->des3 = p->des2 + BUF_SIZE_8KiB;
  406. DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
  407. priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
  408. }
  409. priv->cur_rx = 0;
  410. priv->dirty_rx = (unsigned int)(i - rxsize);
  411. priv->dma_buf_sz = bfsize;
  412. buf_sz = bfsize;
  413. /* TX INITIALIZATION */
  414. for (i = 0; i < txsize; i++) {
  415. priv->tx_skbuff[i] = NULL;
  416. priv->dma_tx[i].des2 = 0;
  417. }
  418. priv->dirty_tx = 0;
  419. priv->cur_tx = 0;
  420. /* Clear the Rx/Tx descriptors */
  421. priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
  422. priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
  423. if (netif_msg_hw(priv)) {
  424. pr_info("RX descriptor ring:\n");
  425. display_ring(priv->dma_rx, rxsize);
  426. pr_info("TX descriptor ring:\n");
  427. display_ring(priv->dma_tx, txsize);
  428. }
  429. }
  430. static void dma_free_rx_skbufs(struct stmmac_priv *priv)
  431. {
  432. int i;
  433. for (i = 0; i < priv->dma_rx_size; i++) {
  434. if (priv->rx_skbuff[i]) {
  435. dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
  436. priv->dma_buf_sz, DMA_FROM_DEVICE);
  437. dev_kfree_skb_any(priv->rx_skbuff[i]);
  438. }
  439. priv->rx_skbuff[i] = NULL;
  440. }
  441. }
  442. static void dma_free_tx_skbufs(struct stmmac_priv *priv)
  443. {
  444. int i;
  445. for (i = 0; i < priv->dma_tx_size; i++) {
  446. if (priv->tx_skbuff[i] != NULL) {
  447. struct dma_desc *p = priv->dma_tx + i;
  448. if (p->des2)
  449. dma_unmap_single(priv->device, p->des2,
  450. priv->hw->desc->get_tx_len(p),
  451. DMA_TO_DEVICE);
  452. dev_kfree_skb_any(priv->tx_skbuff[i]);
  453. priv->tx_skbuff[i] = NULL;
  454. }
  455. }
  456. }
  457. static void free_dma_desc_resources(struct stmmac_priv *priv)
  458. {
  459. /* Release the DMA TX/RX socket buffers */
  460. dma_free_rx_skbufs(priv);
  461. dma_free_tx_skbufs(priv);
  462. /* Free the region of consistent memory previously allocated for
  463. * the DMA */
  464. dma_free_coherent(priv->device,
  465. priv->dma_tx_size * sizeof(struct dma_desc),
  466. priv->dma_tx, priv->dma_tx_phy);
  467. dma_free_coherent(priv->device,
  468. priv->dma_rx_size * sizeof(struct dma_desc),
  469. priv->dma_rx, priv->dma_rx_phy);
  470. kfree(priv->rx_skbuff_dma);
  471. kfree(priv->rx_skbuff);
  472. kfree(priv->tx_skbuff);
  473. }
  474. /**
  475. * stmmac_dma_operation_mode - HW DMA operation mode
  476. * @priv : pointer to the private device structure.
  477. * Description: it sets the DMA operation mode: tx/rx DMA thresholds
  478. * or Store-And-Forward capability.
  479. */
  480. static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
  481. {
  482. if (likely((priv->plat->tx_coe) && (!priv->no_csum_insertion))) {
  483. /* In case of GMAC, SF mode has to be enabled
  484. * to perform the TX COE. This depends on:
  485. * 1) TX COE if actually supported
  486. * 2) There is no bugged Jumbo frame support
  487. * that needs to not insert csum in the TDES.
  488. */
  489. priv->hw->dma->dma_mode(priv->ioaddr,
  490. SF_DMA_MODE, SF_DMA_MODE);
  491. tc = SF_DMA_MODE;
  492. } else
  493. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  494. }
  495. /**
  496. * stmmac_tx:
  497. * @priv: private driver structure
  498. * Description: it reclaims resources after transmission completes.
  499. */
  500. static void stmmac_tx(struct stmmac_priv *priv)
  501. {
  502. unsigned int txsize = priv->dma_tx_size;
  503. while (priv->dirty_tx != priv->cur_tx) {
  504. int last;
  505. unsigned int entry = priv->dirty_tx % txsize;
  506. struct sk_buff *skb = priv->tx_skbuff[entry];
  507. struct dma_desc *p = priv->dma_tx + entry;
  508. /* Check if the descriptor is owned by the DMA. */
  509. if (priv->hw->desc->get_tx_owner(p))
  510. break;
  511. /* Verify tx error by looking at the last segment */
  512. last = priv->hw->desc->get_tx_ls(p);
  513. if (likely(last)) {
  514. int tx_error =
  515. priv->hw->desc->tx_status(&priv->dev->stats,
  516. &priv->xstats, p,
  517. priv->ioaddr);
  518. if (likely(tx_error == 0)) {
  519. priv->dev->stats.tx_packets++;
  520. priv->xstats.tx_pkt_n++;
  521. } else
  522. priv->dev->stats.tx_errors++;
  523. }
  524. TX_DBG("%s: curr %d, dirty %d\n", __func__,
  525. priv->cur_tx, priv->dirty_tx);
  526. if (likely(p->des2))
  527. dma_unmap_single(priv->device, p->des2,
  528. priv->hw->desc->get_tx_len(p),
  529. DMA_TO_DEVICE);
  530. if (unlikely(p->des3))
  531. p->des3 = 0;
  532. if (likely(skb != NULL)) {
  533. /*
  534. * If there's room in the queue (limit it to size)
  535. * we add this skb back into the pool,
  536. * if it's the right size.
  537. */
  538. if ((skb_queue_len(&priv->rx_recycle) <
  539. priv->dma_rx_size) &&
  540. skb_recycle_check(skb, priv->dma_buf_sz))
  541. __skb_queue_head(&priv->rx_recycle, skb);
  542. else
  543. dev_kfree_skb(skb);
  544. priv->tx_skbuff[entry] = NULL;
  545. }
  546. priv->hw->desc->release_tx_desc(p);
  547. entry = (++priv->dirty_tx) % txsize;
  548. }
  549. if (unlikely(netif_queue_stopped(priv->dev) &&
  550. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
  551. netif_tx_lock(priv->dev);
  552. if (netif_queue_stopped(priv->dev) &&
  553. stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
  554. TX_DBG("%s: restart transmit\n", __func__);
  555. netif_wake_queue(priv->dev);
  556. }
  557. netif_tx_unlock(priv->dev);
  558. }
  559. }
  560. static inline void stmmac_enable_irq(struct stmmac_priv *priv)
  561. {
  562. #ifdef CONFIG_STMMAC_TIMER
  563. if (likely(priv->tm->enable))
  564. priv->tm->timer_start(tmrate);
  565. else
  566. #endif
  567. priv->hw->dma->enable_dma_irq(priv->ioaddr);
  568. }
  569. static inline void stmmac_disable_irq(struct stmmac_priv *priv)
  570. {
  571. #ifdef CONFIG_STMMAC_TIMER
  572. if (likely(priv->tm->enable))
  573. priv->tm->timer_stop();
  574. else
  575. #endif
  576. priv->hw->dma->disable_dma_irq(priv->ioaddr);
  577. }
  578. static int stmmac_has_work(struct stmmac_priv *priv)
  579. {
  580. unsigned int has_work = 0;
  581. int rxret, tx_work = 0;
  582. rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
  583. (priv->cur_rx % priv->dma_rx_size));
  584. if (priv->dirty_tx != priv->cur_tx)
  585. tx_work = 1;
  586. if (likely(!rxret || tx_work))
  587. has_work = 1;
  588. return has_work;
  589. }
  590. static inline void _stmmac_schedule(struct stmmac_priv *priv)
  591. {
  592. if (likely(stmmac_has_work(priv))) {
  593. stmmac_disable_irq(priv);
  594. napi_schedule(&priv->napi);
  595. }
  596. }
  597. #ifdef CONFIG_STMMAC_TIMER
  598. void stmmac_schedule(struct net_device *dev)
  599. {
  600. struct stmmac_priv *priv = netdev_priv(dev);
  601. priv->xstats.sched_timer_n++;
  602. _stmmac_schedule(priv);
  603. }
  604. static void stmmac_no_timer_started(unsigned int x)
  605. {;
  606. };
  607. static void stmmac_no_timer_stopped(void)
  608. {;
  609. };
  610. #endif
  611. /**
  612. * stmmac_tx_err:
  613. * @priv: pointer to the private device structure
  614. * Description: it cleans the descriptors and restarts the transmission
  615. * in case of errors.
  616. */
  617. static void stmmac_tx_err(struct stmmac_priv *priv)
  618. {
  619. netif_stop_queue(priv->dev);
  620. priv->hw->dma->stop_tx(priv->ioaddr);
  621. dma_free_tx_skbufs(priv);
  622. priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
  623. priv->dirty_tx = 0;
  624. priv->cur_tx = 0;
  625. priv->hw->dma->start_tx(priv->ioaddr);
  626. priv->dev->stats.tx_errors++;
  627. netif_wake_queue(priv->dev);
  628. }
  629. static void stmmac_dma_interrupt(struct stmmac_priv *priv)
  630. {
  631. int status;
  632. status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
  633. if (likely(status == handle_tx_rx))
  634. _stmmac_schedule(priv);
  635. else if (unlikely(status == tx_hard_error_bump_tc)) {
  636. /* Try to bump up the dma threshold on this failure */
  637. if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
  638. tc += 64;
  639. priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
  640. priv->xstats.threshold = tc;
  641. }
  642. } else if (unlikely(status == tx_hard_error))
  643. stmmac_tx_err(priv);
  644. }
  645. /**
  646. * stmmac_open - open entry point of the driver
  647. * @dev : pointer to the device structure.
  648. * Description:
  649. * This function is the open entry point of the driver.
  650. * Return value:
  651. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  652. * file on failure.
  653. */
  654. static int stmmac_open(struct net_device *dev)
  655. {
  656. struct stmmac_priv *priv = netdev_priv(dev);
  657. int ret;
  658. /* Check that the MAC address is valid. If its not, refuse
  659. * to bring the device up. The user must specify an
  660. * address using the following linux command:
  661. * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
  662. if (!is_valid_ether_addr(dev->dev_addr)) {
  663. random_ether_addr(dev->dev_addr);
  664. pr_warning("%s: generated random MAC address %pM\n", dev->name,
  665. dev->dev_addr);
  666. }
  667. stmmac_verify_args();
  668. #ifdef CONFIG_STMMAC_TIMER
  669. priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
  670. if (unlikely(priv->tm == NULL)) {
  671. pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
  672. return -ENOMEM;
  673. }
  674. priv->tm->freq = tmrate;
  675. /* Test if the external timer can be actually used.
  676. * In case of failure continue without timer. */
  677. if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
  678. pr_warning("stmmaceth: cannot attach the external timer.\n");
  679. priv->tm->freq = 0;
  680. priv->tm->timer_start = stmmac_no_timer_started;
  681. priv->tm->timer_stop = stmmac_no_timer_stopped;
  682. } else
  683. priv->tm->enable = 1;
  684. #endif
  685. ret = stmmac_init_phy(dev);
  686. if (unlikely(ret)) {
  687. pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
  688. goto open_error;
  689. }
  690. /* Create and initialize the TX/RX descriptors chains. */
  691. priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
  692. priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
  693. priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
  694. init_dma_desc_rings(dev);
  695. /* DMA initialization and SW reset */
  696. ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
  697. priv->dma_tx_phy, priv->dma_rx_phy);
  698. if (ret < 0) {
  699. pr_err("%s: DMA initialization failed\n", __func__);
  700. goto open_error;
  701. }
  702. /* Copy the MAC addr into the HW */
  703. priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
  704. /* If required, perform hw setup of the bus. */
  705. if (priv->plat->bus_setup)
  706. priv->plat->bus_setup(priv->ioaddr);
  707. /* Initialize the MAC Core */
  708. priv->hw->mac->core_init(priv->ioaddr);
  709. priv->rx_coe = priv->hw->mac->rx_coe(priv->ioaddr);
  710. if (priv->rx_coe)
  711. pr_info("stmmac: Rx Checksum Offload Engine supported\n");
  712. if (priv->plat->tx_coe)
  713. pr_info("\tTX Checksum insertion supported\n");
  714. netdev_update_features(dev);
  715. /* Initialise the MMC (if present) to disable all interrupts. */
  716. writel(0xffffffff, priv->ioaddr + MMC_HIGH_INTR_MASK);
  717. writel(0xffffffff, priv->ioaddr + MMC_LOW_INTR_MASK);
  718. /* Request the IRQ lines */
  719. ret = request_irq(dev->irq, stmmac_interrupt,
  720. IRQF_SHARED, dev->name, dev);
  721. if (unlikely(ret < 0)) {
  722. pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
  723. __func__, dev->irq, ret);
  724. goto open_error;
  725. }
  726. /* Enable the MAC Rx/Tx */
  727. stmmac_enable_mac(priv->ioaddr);
  728. /* Set the HW DMA mode and the COE */
  729. stmmac_dma_operation_mode(priv);
  730. /* Extra statistics */
  731. memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
  732. priv->xstats.threshold = tc;
  733. /* Start the ball rolling... */
  734. DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
  735. priv->hw->dma->start_tx(priv->ioaddr);
  736. priv->hw->dma->start_rx(priv->ioaddr);
  737. #ifdef CONFIG_STMMAC_TIMER
  738. priv->tm->timer_start(tmrate);
  739. #endif
  740. /* Dump DMA/MAC registers */
  741. if (netif_msg_hw(priv)) {
  742. priv->hw->mac->dump_regs(priv->ioaddr);
  743. priv->hw->dma->dump_regs(priv->ioaddr);
  744. }
  745. if (priv->phydev)
  746. phy_start(priv->phydev);
  747. napi_enable(&priv->napi);
  748. skb_queue_head_init(&priv->rx_recycle);
  749. netif_start_queue(dev);
  750. return 0;
  751. open_error:
  752. #ifdef CONFIG_STMMAC_TIMER
  753. kfree(priv->tm);
  754. #endif
  755. if (priv->phydev)
  756. phy_disconnect(priv->phydev);
  757. return ret;
  758. }
  759. /**
  760. * stmmac_release - close entry point of the driver
  761. * @dev : device pointer.
  762. * Description:
  763. * This is the stop entry point of the driver.
  764. */
  765. static int stmmac_release(struct net_device *dev)
  766. {
  767. struct stmmac_priv *priv = netdev_priv(dev);
  768. /* Stop and disconnect the PHY */
  769. if (priv->phydev) {
  770. phy_stop(priv->phydev);
  771. phy_disconnect(priv->phydev);
  772. priv->phydev = NULL;
  773. }
  774. netif_stop_queue(dev);
  775. #ifdef CONFIG_STMMAC_TIMER
  776. /* Stop and release the timer */
  777. stmmac_close_ext_timer();
  778. if (priv->tm != NULL)
  779. kfree(priv->tm);
  780. #endif
  781. napi_disable(&priv->napi);
  782. skb_queue_purge(&priv->rx_recycle);
  783. /* Free the IRQ lines */
  784. free_irq(dev->irq, dev);
  785. /* Stop TX/RX DMA and clear the descriptors */
  786. priv->hw->dma->stop_tx(priv->ioaddr);
  787. priv->hw->dma->stop_rx(priv->ioaddr);
  788. /* Release and free the Rx/Tx resources */
  789. free_dma_desc_resources(priv);
  790. /* Disable the MAC Rx/Tx */
  791. stmmac_disable_mac(priv->ioaddr);
  792. netif_carrier_off(dev);
  793. return 0;
  794. }
  795. static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
  796. struct net_device *dev,
  797. int csum_insertion)
  798. {
  799. struct stmmac_priv *priv = netdev_priv(dev);
  800. unsigned int nopaged_len = skb_headlen(skb);
  801. unsigned int txsize = priv->dma_tx_size;
  802. unsigned int entry = priv->cur_tx % txsize;
  803. struct dma_desc *desc = priv->dma_tx + entry;
  804. if (nopaged_len > BUF_SIZE_8KiB) {
  805. int buf2_size = nopaged_len - BUF_SIZE_8KiB;
  806. desc->des2 = dma_map_single(priv->device, skb->data,
  807. BUF_SIZE_8KiB, DMA_TO_DEVICE);
  808. desc->des3 = desc->des2 + BUF_SIZE_4KiB;
  809. priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
  810. csum_insertion);
  811. entry = (++priv->cur_tx) % txsize;
  812. desc = priv->dma_tx + entry;
  813. desc->des2 = dma_map_single(priv->device,
  814. skb->data + BUF_SIZE_8KiB,
  815. buf2_size, DMA_TO_DEVICE);
  816. desc->des3 = desc->des2 + BUF_SIZE_4KiB;
  817. priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
  818. csum_insertion);
  819. priv->hw->desc->set_tx_owner(desc);
  820. priv->tx_skbuff[entry] = NULL;
  821. } else {
  822. desc->des2 = dma_map_single(priv->device, skb->data,
  823. nopaged_len, DMA_TO_DEVICE);
  824. desc->des3 = desc->des2 + BUF_SIZE_4KiB;
  825. priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
  826. csum_insertion);
  827. }
  828. return entry;
  829. }
  830. /**
  831. * stmmac_xmit:
  832. * @skb : the socket buffer
  833. * @dev : device pointer
  834. * Description : Tx entry point of the driver.
  835. */
  836. static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
  837. {
  838. struct stmmac_priv *priv = netdev_priv(dev);
  839. unsigned int txsize = priv->dma_tx_size;
  840. unsigned int entry;
  841. int i, csum_insertion = 0;
  842. int nfrags = skb_shinfo(skb)->nr_frags;
  843. struct dma_desc *desc, *first;
  844. if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
  845. if (!netif_queue_stopped(dev)) {
  846. netif_stop_queue(dev);
  847. /* This is a hard error, log it. */
  848. pr_err("%s: BUG! Tx Ring full when queue awake\n",
  849. __func__);
  850. }
  851. return NETDEV_TX_BUSY;
  852. }
  853. entry = priv->cur_tx % txsize;
  854. #ifdef STMMAC_XMIT_DEBUG
  855. if ((skb->len > ETH_FRAME_LEN) || nfrags)
  856. pr_info("stmmac xmit:\n"
  857. "\tskb addr %p - len: %d - nopaged_len: %d\n"
  858. "\tn_frags: %d - ip_summed: %d - %s gso\n",
  859. skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
  860. !skb_is_gso(skb) ? "isn't" : "is");
  861. #endif
  862. csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
  863. desc = priv->dma_tx + entry;
  864. first = desc;
  865. #ifdef STMMAC_XMIT_DEBUG
  866. if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
  867. pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
  868. "\t\tn_frags: %d, ip_summed: %d\n",
  869. skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
  870. #endif
  871. priv->tx_skbuff[entry] = skb;
  872. if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
  873. entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
  874. desc = priv->dma_tx + entry;
  875. } else {
  876. unsigned int nopaged_len = skb_headlen(skb);
  877. desc->des2 = dma_map_single(priv->device, skb->data,
  878. nopaged_len, DMA_TO_DEVICE);
  879. priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
  880. csum_insertion);
  881. }
  882. for (i = 0; i < nfrags; i++) {
  883. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  884. int len = frag->size;
  885. entry = (++priv->cur_tx) % txsize;
  886. desc = priv->dma_tx + entry;
  887. TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
  888. desc->des2 = dma_map_page(priv->device, frag->page,
  889. frag->page_offset,
  890. len, DMA_TO_DEVICE);
  891. priv->tx_skbuff[entry] = NULL;
  892. priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
  893. priv->hw->desc->set_tx_owner(desc);
  894. }
  895. /* Interrupt on completition only for the latest segment */
  896. priv->hw->desc->close_tx_desc(desc);
  897. #ifdef CONFIG_STMMAC_TIMER
  898. /* Clean IC while using timer */
  899. if (likely(priv->tm->enable))
  900. priv->hw->desc->clear_tx_ic(desc);
  901. #endif
  902. /* To avoid raise condition */
  903. priv->hw->desc->set_tx_owner(first);
  904. priv->cur_tx++;
  905. #ifdef STMMAC_XMIT_DEBUG
  906. if (netif_msg_pktdata(priv)) {
  907. pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
  908. "first=%p, nfrags=%d\n",
  909. (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
  910. entry, first, nfrags);
  911. display_ring(priv->dma_tx, txsize);
  912. pr_info(">>> frame to be transmitted: ");
  913. print_pkt(skb->data, skb->len);
  914. }
  915. #endif
  916. if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
  917. TX_DBG("%s: stop transmitted packets\n", __func__);
  918. netif_stop_queue(dev);
  919. }
  920. dev->stats.tx_bytes += skb->len;
  921. priv->hw->dma->enable_dma_transmission(priv->ioaddr);
  922. return NETDEV_TX_OK;
  923. }
  924. static inline void stmmac_rx_refill(struct stmmac_priv *priv)
  925. {
  926. unsigned int rxsize = priv->dma_rx_size;
  927. int bfsize = priv->dma_buf_sz;
  928. struct dma_desc *p = priv->dma_rx;
  929. for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
  930. unsigned int entry = priv->dirty_rx % rxsize;
  931. if (likely(priv->rx_skbuff[entry] == NULL)) {
  932. struct sk_buff *skb;
  933. skb = __skb_dequeue(&priv->rx_recycle);
  934. if (skb == NULL)
  935. skb = netdev_alloc_skb_ip_align(priv->dev,
  936. bfsize);
  937. if (unlikely(skb == NULL))
  938. break;
  939. priv->rx_skbuff[entry] = skb;
  940. priv->rx_skbuff_dma[entry] =
  941. dma_map_single(priv->device, skb->data, bfsize,
  942. DMA_FROM_DEVICE);
  943. (p + entry)->des2 = priv->rx_skbuff_dma[entry];
  944. if (unlikely(priv->plat->has_gmac)) {
  945. if (bfsize >= BUF_SIZE_8KiB)
  946. (p + entry)->des3 =
  947. (p + entry)->des2 + BUF_SIZE_8KiB;
  948. }
  949. RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
  950. }
  951. priv->hw->desc->set_rx_owner(p + entry);
  952. }
  953. }
  954. static int stmmac_rx(struct stmmac_priv *priv, int limit)
  955. {
  956. unsigned int rxsize = priv->dma_rx_size;
  957. unsigned int entry = priv->cur_rx % rxsize;
  958. unsigned int next_entry;
  959. unsigned int count = 0;
  960. struct dma_desc *p = priv->dma_rx + entry;
  961. struct dma_desc *p_next;
  962. #ifdef STMMAC_RX_DEBUG
  963. if (netif_msg_hw(priv)) {
  964. pr_debug(">>> stmmac_rx: descriptor ring:\n");
  965. display_ring(priv->dma_rx, rxsize);
  966. }
  967. #endif
  968. count = 0;
  969. while (!priv->hw->desc->get_rx_owner(p)) {
  970. int status;
  971. if (count >= limit)
  972. break;
  973. count++;
  974. next_entry = (++priv->cur_rx) % rxsize;
  975. p_next = priv->dma_rx + next_entry;
  976. prefetch(p_next);
  977. /* read the status of the incoming frame */
  978. status = (priv->hw->desc->rx_status(&priv->dev->stats,
  979. &priv->xstats, p));
  980. if (unlikely(status == discard_frame))
  981. priv->dev->stats.rx_errors++;
  982. else {
  983. struct sk_buff *skb;
  984. int frame_len;
  985. frame_len = priv->hw->desc->get_rx_frame_len(p);
  986. /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
  987. * Type frames (LLC/LLC-SNAP) */
  988. if (unlikely(status != llc_snap))
  989. frame_len -= ETH_FCS_LEN;
  990. #ifdef STMMAC_RX_DEBUG
  991. if (frame_len > ETH_FRAME_LEN)
  992. pr_debug("\tRX frame size %d, COE status: %d\n",
  993. frame_len, status);
  994. if (netif_msg_hw(priv))
  995. pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
  996. p, entry, p->des2);
  997. #endif
  998. skb = priv->rx_skbuff[entry];
  999. if (unlikely(!skb)) {
  1000. pr_err("%s: Inconsistent Rx descriptor chain\n",
  1001. priv->dev->name);
  1002. priv->dev->stats.rx_dropped++;
  1003. break;
  1004. }
  1005. prefetch(skb->data - NET_IP_ALIGN);
  1006. priv->rx_skbuff[entry] = NULL;
  1007. skb_put(skb, frame_len);
  1008. dma_unmap_single(priv->device,
  1009. priv->rx_skbuff_dma[entry],
  1010. priv->dma_buf_sz, DMA_FROM_DEVICE);
  1011. #ifdef STMMAC_RX_DEBUG
  1012. if (netif_msg_pktdata(priv)) {
  1013. pr_info(" frame received (%dbytes)", frame_len);
  1014. print_pkt(skb->data, frame_len);
  1015. }
  1016. #endif
  1017. skb->protocol = eth_type_trans(skb, priv->dev);
  1018. if (unlikely(status == csum_none)) {
  1019. /* always for the old mac 10/100 */
  1020. skb_checksum_none_assert(skb);
  1021. netif_receive_skb(skb);
  1022. } else {
  1023. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1024. napi_gro_receive(&priv->napi, skb);
  1025. }
  1026. priv->dev->stats.rx_packets++;
  1027. priv->dev->stats.rx_bytes += frame_len;
  1028. }
  1029. entry = next_entry;
  1030. p = p_next; /* use prefetched values */
  1031. }
  1032. stmmac_rx_refill(priv);
  1033. priv->xstats.rx_pkt_n += count;
  1034. return count;
  1035. }
  1036. /**
  1037. * stmmac_poll - stmmac poll method (NAPI)
  1038. * @napi : pointer to the napi structure.
  1039. * @budget : maximum number of packets that the current CPU can receive from
  1040. * all interfaces.
  1041. * Description :
  1042. * This function implements the the reception process.
  1043. * Also it runs the TX completion thread
  1044. */
  1045. static int stmmac_poll(struct napi_struct *napi, int budget)
  1046. {
  1047. struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
  1048. int work_done = 0;
  1049. priv->xstats.poll_n++;
  1050. stmmac_tx(priv);
  1051. work_done = stmmac_rx(priv, budget);
  1052. if (work_done < budget) {
  1053. napi_complete(napi);
  1054. stmmac_enable_irq(priv);
  1055. }
  1056. return work_done;
  1057. }
  1058. /**
  1059. * stmmac_tx_timeout
  1060. * @dev : Pointer to net device structure
  1061. * Description: this function is called when a packet transmission fails to
  1062. * complete within a reasonable tmrate. The driver will mark the error in the
  1063. * netdev structure and arrange for the device to be reset to a sane state
  1064. * in order to transmit a new packet.
  1065. */
  1066. static void stmmac_tx_timeout(struct net_device *dev)
  1067. {
  1068. struct stmmac_priv *priv = netdev_priv(dev);
  1069. /* Clear Tx resources and restart transmitting again */
  1070. stmmac_tx_err(priv);
  1071. }
  1072. /* Configuration changes (passed on by ifconfig) */
  1073. static int stmmac_config(struct net_device *dev, struct ifmap *map)
  1074. {
  1075. if (dev->flags & IFF_UP) /* can't act on a running interface */
  1076. return -EBUSY;
  1077. /* Don't allow changing the I/O address */
  1078. if (map->base_addr != dev->base_addr) {
  1079. pr_warning("%s: can't change I/O address\n", dev->name);
  1080. return -EOPNOTSUPP;
  1081. }
  1082. /* Don't allow changing the IRQ */
  1083. if (map->irq != dev->irq) {
  1084. pr_warning("%s: can't change IRQ number %d\n",
  1085. dev->name, dev->irq);
  1086. return -EOPNOTSUPP;
  1087. }
  1088. /* ignore other fields */
  1089. return 0;
  1090. }
  1091. /**
  1092. * stmmac_multicast_list - entry point for multicast addressing
  1093. * @dev : pointer to the device structure
  1094. * Description:
  1095. * This function is a driver entry point which gets called by the kernel
  1096. * whenever multicast addresses must be enabled/disabled.
  1097. * Return value:
  1098. * void.
  1099. */
  1100. static void stmmac_multicast_list(struct net_device *dev)
  1101. {
  1102. struct stmmac_priv *priv = netdev_priv(dev);
  1103. spin_lock(&priv->lock);
  1104. priv->hw->mac->set_filter(dev);
  1105. spin_unlock(&priv->lock);
  1106. }
  1107. /**
  1108. * stmmac_change_mtu - entry point to change MTU size for the device.
  1109. * @dev : device pointer.
  1110. * @new_mtu : the new MTU size for the device.
  1111. * Description: the Maximum Transfer Unit (MTU) is used by the network layer
  1112. * to drive packet transmission. Ethernet has an MTU of 1500 octets
  1113. * (ETH_DATA_LEN). This value can be changed with ifconfig.
  1114. * Return value:
  1115. * 0 on success and an appropriate (-)ve integer as defined in errno.h
  1116. * file on failure.
  1117. */
  1118. static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
  1119. {
  1120. struct stmmac_priv *priv = netdev_priv(dev);
  1121. int max_mtu;
  1122. if (netif_running(dev)) {
  1123. pr_err("%s: must be stopped to change its MTU\n", dev->name);
  1124. return -EBUSY;
  1125. }
  1126. if (priv->plat->has_gmac)
  1127. max_mtu = JUMBO_LEN;
  1128. else
  1129. max_mtu = ETH_DATA_LEN;
  1130. if ((new_mtu < 46) || (new_mtu > max_mtu)) {
  1131. pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
  1132. return -EINVAL;
  1133. }
  1134. dev->mtu = new_mtu;
  1135. netdev_update_features(dev);
  1136. return 0;
  1137. }
  1138. static u32 stmmac_fix_features(struct net_device *dev, u32 features)
  1139. {
  1140. struct stmmac_priv *priv = netdev_priv(dev);
  1141. if (!priv->rx_coe)
  1142. features &= ~NETIF_F_RXCSUM;
  1143. if (!priv->plat->tx_coe)
  1144. features &= ~NETIF_F_ALL_CSUM;
  1145. /* Some GMAC devices have a bugged Jumbo frame support that
  1146. * needs to have the Tx COE disabled for oversized frames
  1147. * (due to limited buffer sizes). In this case we disable
  1148. * the TX csum insertionin the TDES and not use SF. */
  1149. if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
  1150. features &= ~NETIF_F_ALL_CSUM;
  1151. return features;
  1152. }
  1153. static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
  1154. {
  1155. struct net_device *dev = (struct net_device *)dev_id;
  1156. struct stmmac_priv *priv = netdev_priv(dev);
  1157. if (unlikely(!dev)) {
  1158. pr_err("%s: invalid dev pointer\n", __func__);
  1159. return IRQ_NONE;
  1160. }
  1161. if (priv->plat->has_gmac)
  1162. /* To handle GMAC own interrupts */
  1163. priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
  1164. stmmac_dma_interrupt(priv);
  1165. return IRQ_HANDLED;
  1166. }
  1167. #ifdef CONFIG_NET_POLL_CONTROLLER
  1168. /* Polling receive - used by NETCONSOLE and other diagnostic tools
  1169. * to allow network I/O with interrupts disabled. */
  1170. static void stmmac_poll_controller(struct net_device *dev)
  1171. {
  1172. disable_irq(dev->irq);
  1173. stmmac_interrupt(dev->irq, dev);
  1174. enable_irq(dev->irq);
  1175. }
  1176. #endif
  1177. /**
  1178. * stmmac_ioctl - Entry point for the Ioctl
  1179. * @dev: Device pointer.
  1180. * @rq: An IOCTL specefic structure, that can contain a pointer to
  1181. * a proprietary structure used to pass information to the driver.
  1182. * @cmd: IOCTL command
  1183. * Description:
  1184. * Currently there are no special functionality supported in IOCTL, just the
  1185. * phy_mii_ioctl(...) can be invoked.
  1186. */
  1187. static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1188. {
  1189. struct stmmac_priv *priv = netdev_priv(dev);
  1190. int ret;
  1191. if (!netif_running(dev))
  1192. return -EINVAL;
  1193. if (!priv->phydev)
  1194. return -EINVAL;
  1195. spin_lock(&priv->lock);
  1196. ret = phy_mii_ioctl(priv->phydev, rq, cmd);
  1197. spin_unlock(&priv->lock);
  1198. return ret;
  1199. }
  1200. #ifdef STMMAC_VLAN_TAG_USED
  1201. static void stmmac_vlan_rx_register(struct net_device *dev,
  1202. struct vlan_group *grp)
  1203. {
  1204. struct stmmac_priv *priv = netdev_priv(dev);
  1205. DBG(probe, INFO, "%s: Setting vlgrp to %p\n", dev->name, grp);
  1206. spin_lock(&priv->lock);
  1207. priv->vlgrp = grp;
  1208. spin_unlock(&priv->lock);
  1209. }
  1210. #endif
  1211. static const struct net_device_ops stmmac_netdev_ops = {
  1212. .ndo_open = stmmac_open,
  1213. .ndo_start_xmit = stmmac_xmit,
  1214. .ndo_stop = stmmac_release,
  1215. .ndo_change_mtu = stmmac_change_mtu,
  1216. .ndo_fix_features = stmmac_fix_features,
  1217. .ndo_set_multicast_list = stmmac_multicast_list,
  1218. .ndo_tx_timeout = stmmac_tx_timeout,
  1219. .ndo_do_ioctl = stmmac_ioctl,
  1220. .ndo_set_config = stmmac_config,
  1221. #ifdef STMMAC_VLAN_TAG_USED
  1222. .ndo_vlan_rx_register = stmmac_vlan_rx_register,
  1223. #endif
  1224. #ifdef CONFIG_NET_POLL_CONTROLLER
  1225. .ndo_poll_controller = stmmac_poll_controller,
  1226. #endif
  1227. .ndo_set_mac_address = eth_mac_addr,
  1228. };
  1229. /**
  1230. * stmmac_probe - Initialization of the adapter .
  1231. * @dev : device pointer
  1232. * Description: The function initializes the network device structure for
  1233. * the STMMAC driver. It also calls the low level routines
  1234. * in order to init the HW (i.e. the DMA engine)
  1235. */
  1236. static int stmmac_probe(struct net_device *dev)
  1237. {
  1238. int ret = 0;
  1239. struct stmmac_priv *priv = netdev_priv(dev);
  1240. ether_setup(dev);
  1241. dev->netdev_ops = &stmmac_netdev_ops;
  1242. stmmac_set_ethtool_ops(dev);
  1243. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1244. dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
  1245. dev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1246. #ifdef STMMAC_VLAN_TAG_USED
  1247. /* Both mac100 and gmac support receive VLAN tag detection */
  1248. dev->features |= NETIF_F_HW_VLAN_RX;
  1249. #endif
  1250. priv->msg_enable = netif_msg_init(debug, default_msg_level);
  1251. if (flow_ctrl)
  1252. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  1253. priv->pause = pause;
  1254. netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
  1255. /* Get the MAC address */
  1256. priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
  1257. dev->dev_addr, 0);
  1258. if (!is_valid_ether_addr(dev->dev_addr))
  1259. pr_warning("\tno valid MAC address;"
  1260. "please, use ifconfig or nwhwconfig!\n");
  1261. spin_lock_init(&priv->lock);
  1262. ret = register_netdev(dev);
  1263. if (ret) {
  1264. pr_err("%s: ERROR %i registering the device\n",
  1265. __func__, ret);
  1266. return -ENODEV;
  1267. }
  1268. DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
  1269. dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
  1270. (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
  1271. return ret;
  1272. }
  1273. /**
  1274. * stmmac_mac_device_setup
  1275. * @dev : device pointer
  1276. * Description: select and initialise the mac device (mac100 or Gmac).
  1277. */
  1278. static int stmmac_mac_device_setup(struct net_device *dev)
  1279. {
  1280. struct stmmac_priv *priv = netdev_priv(dev);
  1281. struct mac_device_info *device;
  1282. if (priv->plat->has_gmac)
  1283. device = dwmac1000_setup(priv->ioaddr);
  1284. else
  1285. device = dwmac100_setup(priv->ioaddr);
  1286. if (!device)
  1287. return -ENOMEM;
  1288. if (priv->plat->enh_desc) {
  1289. device->desc = &enh_desc_ops;
  1290. pr_info("\tEnhanced descriptor structure\n");
  1291. } else
  1292. device->desc = &ndesc_ops;
  1293. priv->hw = device;
  1294. if (device_can_wakeup(priv->device)) {
  1295. priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
  1296. enable_irq_wake(dev->irq);
  1297. }
  1298. return 0;
  1299. }
  1300. static int stmmacphy_dvr_probe(struct platform_device *pdev)
  1301. {
  1302. struct plat_stmmacphy_data *plat_dat = pdev->dev.platform_data;
  1303. pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
  1304. plat_dat->bus_id);
  1305. return 0;
  1306. }
  1307. static int stmmacphy_dvr_remove(struct platform_device *pdev)
  1308. {
  1309. return 0;
  1310. }
  1311. static struct platform_driver stmmacphy_driver = {
  1312. .driver = {
  1313. .name = PHY_RESOURCE_NAME,
  1314. },
  1315. .probe = stmmacphy_dvr_probe,
  1316. .remove = stmmacphy_dvr_remove,
  1317. };
  1318. /**
  1319. * stmmac_associate_phy
  1320. * @dev: pointer to device structure
  1321. * @data: points to the private structure.
  1322. * Description: Scans through all the PHYs we have registered and checks if
  1323. * any are associated with our MAC. If so, then just fill in
  1324. * the blanks in our local context structure
  1325. */
  1326. static int stmmac_associate_phy(struct device *dev, void *data)
  1327. {
  1328. struct stmmac_priv *priv = (struct stmmac_priv *)data;
  1329. struct plat_stmmacphy_data *plat_dat = dev->platform_data;
  1330. DBG(probe, DEBUG, "%s: checking phy for bus %d\n", __func__,
  1331. plat_dat->bus_id);
  1332. /* Check that this phy is for the MAC being initialised */
  1333. if (priv->plat->bus_id != plat_dat->bus_id)
  1334. return 0;
  1335. /* OK, this PHY is connected to the MAC.
  1336. Go ahead and get the parameters */
  1337. DBG(probe, DEBUG, "%s: OK. Found PHY config\n", __func__);
  1338. priv->phy_irq =
  1339. platform_get_irq_byname(to_platform_device(dev), "phyirq");
  1340. DBG(probe, DEBUG, "%s: PHY irq on bus %d is %d\n", __func__,
  1341. plat_dat->bus_id, priv->phy_irq);
  1342. /* Override with kernel parameters if supplied XXX CRS XXX
  1343. * this needs to have multiple instances */
  1344. if ((phyaddr >= 0) && (phyaddr <= 31))
  1345. plat_dat->phy_addr = phyaddr;
  1346. priv->phy_addr = plat_dat->phy_addr;
  1347. priv->phy_mask = plat_dat->phy_mask;
  1348. priv->phy_interface = plat_dat->interface;
  1349. priv->phy_reset = plat_dat->phy_reset;
  1350. DBG(probe, DEBUG, "%s: exiting\n", __func__);
  1351. return 1; /* forces exit of driver_for_each_device() */
  1352. }
  1353. /**
  1354. * stmmac_dvr_probe
  1355. * @pdev: platform device pointer
  1356. * Description: the driver is initialized through platform_device.
  1357. */
  1358. static int stmmac_dvr_probe(struct platform_device *pdev)
  1359. {
  1360. int ret = 0;
  1361. struct resource *res;
  1362. void __iomem *addr = NULL;
  1363. struct net_device *ndev = NULL;
  1364. struct stmmac_priv *priv = NULL;
  1365. struct plat_stmmacenet_data *plat_dat;
  1366. pr_info("STMMAC driver:\n\tplatform registration... ");
  1367. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1368. if (!res)
  1369. return -ENODEV;
  1370. pr_info("\tdone!\n");
  1371. if (!request_mem_region(res->start, resource_size(res),
  1372. pdev->name)) {
  1373. pr_err("%s: ERROR: memory allocation failed"
  1374. "cannot get the I/O addr 0x%x\n",
  1375. __func__, (unsigned int)res->start);
  1376. return -EBUSY;
  1377. }
  1378. addr = ioremap(res->start, resource_size(res));
  1379. if (!addr) {
  1380. pr_err("%s: ERROR: memory mapping failed\n", __func__);
  1381. ret = -ENOMEM;
  1382. goto out_release_region;
  1383. }
  1384. ndev = alloc_etherdev(sizeof(struct stmmac_priv));
  1385. if (!ndev) {
  1386. pr_err("%s: ERROR: allocating the device\n", __func__);
  1387. ret = -ENOMEM;
  1388. goto out_unmap;
  1389. }
  1390. SET_NETDEV_DEV(ndev, &pdev->dev);
  1391. /* Get the MAC information */
  1392. ndev->irq = platform_get_irq_byname(pdev, "macirq");
  1393. if (ndev->irq == -ENXIO) {
  1394. pr_err("%s: ERROR: MAC IRQ configuration "
  1395. "information not found\n", __func__);
  1396. ret = -ENXIO;
  1397. goto out_free_ndev;
  1398. }
  1399. priv = netdev_priv(ndev);
  1400. priv->device = &(pdev->dev);
  1401. priv->dev = ndev;
  1402. plat_dat = pdev->dev.platform_data;
  1403. priv->plat = plat_dat;
  1404. priv->ioaddr = addr;
  1405. /* PMT module is not integrated in all the MAC devices. */
  1406. if (plat_dat->pmt) {
  1407. pr_info("\tPMT module supported\n");
  1408. device_set_wakeup_capable(&pdev->dev, 1);
  1409. }
  1410. platform_set_drvdata(pdev, ndev);
  1411. /* Set the I/O base addr */
  1412. ndev->base_addr = (unsigned long)addr;
  1413. /* Custom initialisation */
  1414. if (priv->plat->init) {
  1415. ret = priv->plat->init(pdev);
  1416. if (unlikely(ret))
  1417. goto out_free_ndev;
  1418. }
  1419. /* MAC HW revice detection */
  1420. ret = stmmac_mac_device_setup(ndev);
  1421. if (ret < 0)
  1422. goto out_plat_exit;
  1423. /* Network Device Registration */
  1424. ret = stmmac_probe(ndev);
  1425. if (ret < 0)
  1426. goto out_plat_exit;
  1427. /* associate a PHY - it is provided by another platform bus */
  1428. if (!driver_for_each_device
  1429. (&(stmmacphy_driver.driver), NULL, (void *)priv,
  1430. stmmac_associate_phy)) {
  1431. pr_err("No PHY device is associated with this MAC!\n");
  1432. ret = -ENODEV;
  1433. goto out_unregister;
  1434. }
  1435. pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
  1436. "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
  1437. pdev->id, ndev->irq, addr);
  1438. /* MDIO bus Registration */
  1439. pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
  1440. ret = stmmac_mdio_register(ndev);
  1441. if (ret < 0)
  1442. goto out_unregister;
  1443. pr_debug("registered!\n");
  1444. return 0;
  1445. out_unregister:
  1446. unregister_netdev(ndev);
  1447. out_plat_exit:
  1448. if (priv->plat->exit)
  1449. priv->plat->exit(pdev);
  1450. out_free_ndev:
  1451. free_netdev(ndev);
  1452. platform_set_drvdata(pdev, NULL);
  1453. out_unmap:
  1454. iounmap(addr);
  1455. out_release_region:
  1456. release_mem_region(res->start, resource_size(res));
  1457. return ret;
  1458. }
  1459. /**
  1460. * stmmac_dvr_remove
  1461. * @pdev: platform device pointer
  1462. * Description: this function resets the TX/RX processes, disables the MAC RX/TX
  1463. * changes the link status, releases the DMA descriptor rings,
  1464. * unregisters the MDIO bus and unmaps the allocated memory.
  1465. */
  1466. static int stmmac_dvr_remove(struct platform_device *pdev)
  1467. {
  1468. struct net_device *ndev = platform_get_drvdata(pdev);
  1469. struct stmmac_priv *priv = netdev_priv(ndev);
  1470. struct resource *res;
  1471. pr_info("%s:\n\tremoving driver", __func__);
  1472. priv->hw->dma->stop_rx(priv->ioaddr);
  1473. priv->hw->dma->stop_tx(priv->ioaddr);
  1474. stmmac_disable_mac(priv->ioaddr);
  1475. netif_carrier_off(ndev);
  1476. stmmac_mdio_unregister(ndev);
  1477. if (priv->plat->exit)
  1478. priv->plat->exit(pdev);
  1479. platform_set_drvdata(pdev, NULL);
  1480. unregister_netdev(ndev);
  1481. iounmap((void *)priv->ioaddr);
  1482. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1483. release_mem_region(res->start, resource_size(res));
  1484. free_netdev(ndev);
  1485. return 0;
  1486. }
  1487. #ifdef CONFIG_PM
  1488. static int stmmac_suspend(struct device *dev)
  1489. {
  1490. struct net_device *ndev = dev_get_drvdata(dev);
  1491. struct stmmac_priv *priv = netdev_priv(ndev);
  1492. int dis_ic = 0;
  1493. if (!ndev || !netif_running(ndev))
  1494. return 0;
  1495. spin_lock(&priv->lock);
  1496. netif_device_detach(ndev);
  1497. netif_stop_queue(ndev);
  1498. if (priv->phydev)
  1499. phy_stop(priv->phydev);
  1500. #ifdef CONFIG_STMMAC_TIMER
  1501. priv->tm->timer_stop();
  1502. if (likely(priv->tm->enable))
  1503. dis_ic = 1;
  1504. #endif
  1505. napi_disable(&priv->napi);
  1506. /* Stop TX/RX DMA */
  1507. priv->hw->dma->stop_tx(priv->ioaddr);
  1508. priv->hw->dma->stop_rx(priv->ioaddr);
  1509. /* Clear the Rx/Tx descriptors */
  1510. priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
  1511. dis_ic);
  1512. priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
  1513. /* Enable Power down mode by programming the PMT regs */
  1514. if (device_may_wakeup(priv->device))
  1515. priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
  1516. else
  1517. stmmac_disable_mac(priv->ioaddr);
  1518. spin_unlock(&priv->lock);
  1519. return 0;
  1520. }
  1521. static int stmmac_resume(struct device *dev)
  1522. {
  1523. struct net_device *ndev = dev_get_drvdata(dev);
  1524. struct stmmac_priv *priv = netdev_priv(ndev);
  1525. if (!netif_running(ndev))
  1526. return 0;
  1527. spin_lock(&priv->lock);
  1528. /* Power Down bit, into the PM register, is cleared
  1529. * automatically as soon as a magic packet or a Wake-up frame
  1530. * is received. Anyway, it's better to manually clear
  1531. * this bit because it can generate problems while resuming
  1532. * from another devices (e.g. serial console). */
  1533. if (device_may_wakeup(priv->device))
  1534. priv->hw->mac->pmt(priv->ioaddr, 0);
  1535. netif_device_attach(ndev);
  1536. /* Enable the MAC and DMA */
  1537. stmmac_enable_mac(priv->ioaddr);
  1538. priv->hw->dma->start_tx(priv->ioaddr);
  1539. priv->hw->dma->start_rx(priv->ioaddr);
  1540. #ifdef CONFIG_STMMAC_TIMER
  1541. if (likely(priv->tm->enable))
  1542. priv->tm->timer_start(tmrate);
  1543. #endif
  1544. napi_enable(&priv->napi);
  1545. if (priv->phydev)
  1546. phy_start(priv->phydev);
  1547. netif_start_queue(ndev);
  1548. spin_unlock(&priv->lock);
  1549. return 0;
  1550. }
  1551. static int stmmac_freeze(struct device *dev)
  1552. {
  1553. struct net_device *ndev = dev_get_drvdata(dev);
  1554. if (!ndev || !netif_running(ndev))
  1555. return 0;
  1556. return stmmac_release(ndev);
  1557. }
  1558. static int stmmac_restore(struct device *dev)
  1559. {
  1560. struct net_device *ndev = dev_get_drvdata(dev);
  1561. if (!ndev || !netif_running(ndev))
  1562. return 0;
  1563. return stmmac_open(ndev);
  1564. }
  1565. static const struct dev_pm_ops stmmac_pm_ops = {
  1566. .suspend = stmmac_suspend,
  1567. .resume = stmmac_resume,
  1568. .freeze = stmmac_freeze,
  1569. .thaw = stmmac_restore,
  1570. .restore = stmmac_restore,
  1571. };
  1572. #else
  1573. static const struct dev_pm_ops stmmac_pm_ops;
  1574. #endif /* CONFIG_PM */
  1575. static struct platform_driver stmmac_driver = {
  1576. .probe = stmmac_dvr_probe,
  1577. .remove = stmmac_dvr_remove,
  1578. .driver = {
  1579. .name = STMMAC_RESOURCE_NAME,
  1580. .owner = THIS_MODULE,
  1581. .pm = &stmmac_pm_ops,
  1582. },
  1583. };
  1584. /**
  1585. * stmmac_init_module - Entry point for the driver
  1586. * Description: This function is the entry point for the driver.
  1587. */
  1588. static int __init stmmac_init_module(void)
  1589. {
  1590. int ret;
  1591. if (platform_driver_register(&stmmacphy_driver)) {
  1592. pr_err("No PHY devices registered!\n");
  1593. return -ENODEV;
  1594. }
  1595. ret = platform_driver_register(&stmmac_driver);
  1596. return ret;
  1597. }
  1598. /**
  1599. * stmmac_cleanup_module - Cleanup routine for the driver
  1600. * Description: This function is the cleanup routine for the driver.
  1601. */
  1602. static void __exit stmmac_cleanup_module(void)
  1603. {
  1604. platform_driver_unregister(&stmmacphy_driver);
  1605. platform_driver_unregister(&stmmac_driver);
  1606. }
  1607. #ifndef MODULE
  1608. static int __init stmmac_cmdline_opt(char *str)
  1609. {
  1610. char *opt;
  1611. if (!str || !*str)
  1612. return -EINVAL;
  1613. while ((opt = strsep(&str, ",")) != NULL) {
  1614. if (!strncmp(opt, "debug:", 6))
  1615. strict_strtoul(opt + 6, 0, (unsigned long *)&debug);
  1616. else if (!strncmp(opt, "phyaddr:", 8))
  1617. strict_strtoul(opt + 8, 0, (unsigned long *)&phyaddr);
  1618. else if (!strncmp(opt, "dma_txsize:", 11))
  1619. strict_strtoul(opt + 11, 0,
  1620. (unsigned long *)&dma_txsize);
  1621. else if (!strncmp(opt, "dma_rxsize:", 11))
  1622. strict_strtoul(opt + 11, 0,
  1623. (unsigned long *)&dma_rxsize);
  1624. else if (!strncmp(opt, "buf_sz:", 7))
  1625. strict_strtoul(opt + 7, 0, (unsigned long *)&buf_sz);
  1626. else if (!strncmp(opt, "tc:", 3))
  1627. strict_strtoul(opt + 3, 0, (unsigned long *)&tc);
  1628. else if (!strncmp(opt, "watchdog:", 9))
  1629. strict_strtoul(opt + 9, 0, (unsigned long *)&watchdog);
  1630. else if (!strncmp(opt, "flow_ctrl:", 10))
  1631. strict_strtoul(opt + 10, 0,
  1632. (unsigned long *)&flow_ctrl);
  1633. else if (!strncmp(opt, "pause:", 6))
  1634. strict_strtoul(opt + 6, 0, (unsigned long *)&pause);
  1635. #ifdef CONFIG_STMMAC_TIMER
  1636. else if (!strncmp(opt, "tmrate:", 7))
  1637. strict_strtoul(opt + 7, 0, (unsigned long *)&tmrate);
  1638. #endif
  1639. }
  1640. return 0;
  1641. }
  1642. __setup("stmmaceth=", stmmac_cmdline_opt);
  1643. #endif
  1644. module_init(stmmac_init_module);
  1645. module_exit(stmmac_cleanup_module);
  1646. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
  1647. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  1648. MODULE_LICENSE("GPL");