stmmac_main.c 58 KB

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