stmmac_main.c 58 KB

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