stmmac_main.c 53 KB

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