stmmac_main.c 55 KB

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