gianfar.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /*
  2. * drivers/net/gianfar.c
  3. *
  4. * Gianfar Ethernet Driver
  5. * This driver is designed for the non-CPM ethernet controllers
  6. * on the 85xx and 83xx family of integrated processors
  7. * Based on 8260_io/fcc_enet.c
  8. *
  9. * Author: Andy Fleming
  10. * Maintainer: Kumar Gala
  11. *
  12. * Copyright (c) 2002-2006 Freescale Semiconductor, Inc.
  13. * Copyright (c) 2007 MontaVista Software, Inc.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * Gianfar: AKA Lambda Draconis, "Dragon"
  21. * RA 11 31 24.2
  22. * Dec +69 19 52
  23. * V 3.84
  24. * B-V +1.62
  25. *
  26. * Theory of operation
  27. *
  28. * The driver is initialized through of_device. Configuration information
  29. * is therefore conveyed through an OF-style device tree.
  30. *
  31. * The Gianfar Ethernet Controller uses a ring of buffer
  32. * descriptors. The beginning is indicated by a register
  33. * pointing to the physical address of the start of the ring.
  34. * The end is determined by a "wrap" bit being set in the
  35. * last descriptor of the ring.
  36. *
  37. * When a packet is received, the RXF bit in the
  38. * IEVENT register is set, triggering an interrupt when the
  39. * corresponding bit in the IMASK register is also set (if
  40. * interrupt coalescing is active, then the interrupt may not
  41. * happen immediately, but will wait until either a set number
  42. * of frames or amount of time have passed). In NAPI, the
  43. * interrupt handler will signal there is work to be done, and
  44. * exit. This method will start at the last known empty
  45. * descriptor, and process every subsequent descriptor until there
  46. * are none left with data (NAPI will stop after a set number of
  47. * packets to give time to other tasks, but will eventually
  48. * process all the packets). The data arrives inside a
  49. * pre-allocated skb, and so after the skb is passed up to the
  50. * stack, a new skb must be allocated, and the address field in
  51. * the buffer descriptor must be updated to indicate this new
  52. * skb.
  53. *
  54. * When the kernel requests that a packet be transmitted, the
  55. * driver starts where it left off last time, and points the
  56. * descriptor at the buffer which was passed in. The driver
  57. * then informs the DMA engine that there are packets ready to
  58. * be transmitted. Once the controller is finished transmitting
  59. * the packet, an interrupt may be triggered (under the same
  60. * conditions as for reception, but depending on the TXF bit).
  61. * The driver then cleans up the buffer.
  62. */
  63. #include <linux/kernel.h>
  64. #include <linux/string.h>
  65. #include <linux/errno.h>
  66. #include <linux/unistd.h>
  67. #include <linux/slab.h>
  68. #include <linux/interrupt.h>
  69. #include <linux/init.h>
  70. #include <linux/delay.h>
  71. #include <linux/netdevice.h>
  72. #include <linux/etherdevice.h>
  73. #include <linux/skbuff.h>
  74. #include <linux/if_vlan.h>
  75. #include <linux/spinlock.h>
  76. #include <linux/mm.h>
  77. #include <linux/of_platform.h>
  78. #include <linux/ip.h>
  79. #include <linux/tcp.h>
  80. #include <linux/udp.h>
  81. #include <linux/in.h>
  82. #include <asm/io.h>
  83. #include <asm/irq.h>
  84. #include <asm/uaccess.h>
  85. #include <linux/module.h>
  86. #include <linux/dma-mapping.h>
  87. #include <linux/crc32.h>
  88. #include <linux/mii.h>
  89. #include <linux/phy.h>
  90. #include <linux/phy_fixed.h>
  91. #include <linux/of.h>
  92. #include "gianfar.h"
  93. #include "gianfar_mii.h"
  94. #define TX_TIMEOUT (1*HZ)
  95. #undef BRIEF_GFAR_ERRORS
  96. #undef VERBOSE_GFAR_ERRORS
  97. const char gfar_driver_name[] = "Gianfar Ethernet";
  98. const char gfar_driver_version[] = "1.3";
  99. static int gfar_enet_open(struct net_device *dev);
  100. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
  101. static void gfar_reset_task(struct work_struct *work);
  102. static void gfar_timeout(struct net_device *dev);
  103. static int gfar_close(struct net_device *dev);
  104. struct sk_buff *gfar_new_skb(struct net_device *dev);
  105. static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
  106. struct sk_buff *skb);
  107. static int gfar_set_mac_address(struct net_device *dev);
  108. static int gfar_change_mtu(struct net_device *dev, int new_mtu);
  109. static irqreturn_t gfar_error(int irq, void *dev_id);
  110. static irqreturn_t gfar_transmit(int irq, void *dev_id);
  111. static irqreturn_t gfar_interrupt(int irq, void *dev_id);
  112. static void adjust_link(struct net_device *dev);
  113. static void init_registers(struct net_device *dev);
  114. static int init_phy(struct net_device *dev);
  115. static int gfar_probe(struct of_device *ofdev,
  116. const struct of_device_id *match);
  117. static int gfar_remove(struct of_device *ofdev);
  118. static void free_skb_resources(struct gfar_private *priv);
  119. static void gfar_set_multi(struct net_device *dev);
  120. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
  121. static void gfar_configure_serdes(struct net_device *dev);
  122. static int gfar_poll(struct napi_struct *napi, int budget);
  123. #ifdef CONFIG_NET_POLL_CONTROLLER
  124. static void gfar_netpoll(struct net_device *dev);
  125. #endif
  126. int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit);
  127. static int gfar_clean_tx_ring(struct net_device *dev);
  128. static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb, int length);
  129. static void gfar_vlan_rx_register(struct net_device *netdev,
  130. struct vlan_group *grp);
  131. void gfar_halt(struct net_device *dev);
  132. static void gfar_halt_nodisable(struct net_device *dev);
  133. void gfar_start(struct net_device *dev);
  134. static void gfar_clear_exact_match(struct net_device *dev);
  135. static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
  136. extern const struct ethtool_ops gfar_ethtool_ops;
  137. MODULE_AUTHOR("Freescale Semiconductor, Inc");
  138. MODULE_DESCRIPTION("Gianfar Ethernet Driver");
  139. MODULE_LICENSE("GPL");
  140. /* Returns 1 if incoming frames use an FCB */
  141. static inline int gfar_uses_fcb(struct gfar_private *priv)
  142. {
  143. return priv->vlgrp || priv->rx_csum_enable;
  144. }
  145. static int gfar_of_init(struct net_device *dev)
  146. {
  147. struct device_node *phy, *mdio;
  148. const unsigned int *id;
  149. const char *model;
  150. const char *ctype;
  151. const void *mac_addr;
  152. const phandle *ph;
  153. u64 addr, size;
  154. int err = 0;
  155. struct gfar_private *priv = netdev_priv(dev);
  156. struct device_node *np = priv->node;
  157. char bus_name[MII_BUS_ID_SIZE];
  158. if (!np || !of_device_is_available(np))
  159. return -ENODEV;
  160. /* get a pointer to the register memory */
  161. addr = of_translate_address(np, of_get_address(np, 0, &size, NULL));
  162. priv->regs = ioremap(addr, size);
  163. if (priv->regs == NULL)
  164. return -ENOMEM;
  165. priv->interruptTransmit = irq_of_parse_and_map(np, 0);
  166. model = of_get_property(np, "model", NULL);
  167. /* If we aren't the FEC we have multiple interrupts */
  168. if (model && strcasecmp(model, "FEC")) {
  169. priv->interruptReceive = irq_of_parse_and_map(np, 1);
  170. priv->interruptError = irq_of_parse_and_map(np, 2);
  171. if (priv->interruptTransmit < 0 ||
  172. priv->interruptReceive < 0 ||
  173. priv->interruptError < 0) {
  174. err = -EINVAL;
  175. goto err_out;
  176. }
  177. }
  178. mac_addr = of_get_mac_address(np);
  179. if (mac_addr)
  180. memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
  181. if (model && !strcasecmp(model, "TSEC"))
  182. priv->device_flags =
  183. FSL_GIANFAR_DEV_HAS_GIGABIT |
  184. FSL_GIANFAR_DEV_HAS_COALESCE |
  185. FSL_GIANFAR_DEV_HAS_RMON |
  186. FSL_GIANFAR_DEV_HAS_MULTI_INTR;
  187. if (model && !strcasecmp(model, "eTSEC"))
  188. priv->device_flags =
  189. FSL_GIANFAR_DEV_HAS_GIGABIT |
  190. FSL_GIANFAR_DEV_HAS_COALESCE |
  191. FSL_GIANFAR_DEV_HAS_RMON |
  192. FSL_GIANFAR_DEV_HAS_MULTI_INTR |
  193. FSL_GIANFAR_DEV_HAS_CSUM |
  194. FSL_GIANFAR_DEV_HAS_VLAN |
  195. FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
  196. FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
  197. ctype = of_get_property(np, "phy-connection-type", NULL);
  198. /* We only care about rgmii-id. The rest are autodetected */
  199. if (ctype && !strcmp(ctype, "rgmii-id"))
  200. priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
  201. else
  202. priv->interface = PHY_INTERFACE_MODE_MII;
  203. if (of_get_property(np, "fsl,magic-packet", NULL))
  204. priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
  205. ph = of_get_property(np, "phy-handle", NULL);
  206. if (ph == NULL) {
  207. u32 *fixed_link;
  208. fixed_link = (u32 *)of_get_property(np, "fixed-link", NULL);
  209. if (!fixed_link) {
  210. err = -ENODEV;
  211. goto err_out;
  212. }
  213. snprintf(priv->phy_bus_id, BUS_ID_SIZE, PHY_ID_FMT, "0",
  214. fixed_link[0]);
  215. } else {
  216. phy = of_find_node_by_phandle(*ph);
  217. if (phy == NULL) {
  218. err = -ENODEV;
  219. goto err_out;
  220. }
  221. mdio = of_get_parent(phy);
  222. id = of_get_property(phy, "reg", NULL);
  223. of_node_put(phy);
  224. of_node_put(mdio);
  225. gfar_mdio_bus_name(bus_name, mdio);
  226. snprintf(priv->phy_bus_id, BUS_ID_SIZE, "%s:%02x",
  227. bus_name, *id);
  228. }
  229. /* Find the TBI PHY. If it's not there, we don't support SGMII */
  230. ph = of_get_property(np, "tbi-handle", NULL);
  231. if (ph) {
  232. struct device_node *tbi = of_find_node_by_phandle(*ph);
  233. struct of_device *ofdev;
  234. struct mii_bus *bus;
  235. if (!tbi)
  236. return 0;
  237. mdio = of_get_parent(tbi);
  238. if (!mdio)
  239. return 0;
  240. ofdev = of_find_device_by_node(mdio);
  241. of_node_put(mdio);
  242. id = of_get_property(tbi, "reg", NULL);
  243. if (!id)
  244. return 0;
  245. of_node_put(tbi);
  246. bus = dev_get_drvdata(&ofdev->dev);
  247. priv->tbiphy = bus->phy_map[*id];
  248. }
  249. return 0;
  250. err_out:
  251. iounmap(priv->regs);
  252. return err;
  253. }
  254. /* Set up the ethernet device structure, private data,
  255. * and anything else we need before we start */
  256. static int gfar_probe(struct of_device *ofdev,
  257. const struct of_device_id *match)
  258. {
  259. u32 tempval;
  260. struct net_device *dev = NULL;
  261. struct gfar_private *priv = NULL;
  262. int err = 0;
  263. DECLARE_MAC_BUF(mac);
  264. /* Create an ethernet device instance */
  265. dev = alloc_etherdev(sizeof (*priv));
  266. if (NULL == dev)
  267. return -ENOMEM;
  268. priv = netdev_priv(dev);
  269. priv->dev = dev;
  270. priv->node = ofdev->node;
  271. err = gfar_of_init(dev);
  272. if (err)
  273. goto regs_fail;
  274. spin_lock_init(&priv->txlock);
  275. spin_lock_init(&priv->rxlock);
  276. spin_lock_init(&priv->bflock);
  277. INIT_WORK(&priv->reset_task, gfar_reset_task);
  278. dev_set_drvdata(&ofdev->dev, priv);
  279. /* Stop the DMA engine now, in case it was running before */
  280. /* (The firmware could have used it, and left it running). */
  281. gfar_halt(dev);
  282. /* Reset MAC layer */
  283. gfar_write(&priv->regs->maccfg1, MACCFG1_SOFT_RESET);
  284. tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  285. gfar_write(&priv->regs->maccfg1, tempval);
  286. /* Initialize MACCFG2. */
  287. gfar_write(&priv->regs->maccfg2, MACCFG2_INIT_SETTINGS);
  288. /* Initialize ECNTRL */
  289. gfar_write(&priv->regs->ecntrl, ECNTRL_INIT_SETTINGS);
  290. /* Set the dev->base_addr to the gfar reg region */
  291. dev->base_addr = (unsigned long) (priv->regs);
  292. SET_NETDEV_DEV(dev, &ofdev->dev);
  293. /* Fill in the dev structure */
  294. dev->open = gfar_enet_open;
  295. dev->hard_start_xmit = gfar_start_xmit;
  296. dev->tx_timeout = gfar_timeout;
  297. dev->watchdog_timeo = TX_TIMEOUT;
  298. netif_napi_add(dev, &priv->napi, gfar_poll, GFAR_DEV_WEIGHT);
  299. #ifdef CONFIG_NET_POLL_CONTROLLER
  300. dev->poll_controller = gfar_netpoll;
  301. #endif
  302. dev->stop = gfar_close;
  303. dev->change_mtu = gfar_change_mtu;
  304. dev->mtu = 1500;
  305. dev->set_multicast_list = gfar_set_multi;
  306. dev->ethtool_ops = &gfar_ethtool_ops;
  307. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
  308. priv->rx_csum_enable = 1;
  309. dev->features |= NETIF_F_IP_CSUM;
  310. } else
  311. priv->rx_csum_enable = 0;
  312. priv->vlgrp = NULL;
  313. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
  314. dev->vlan_rx_register = gfar_vlan_rx_register;
  315. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  316. }
  317. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
  318. priv->extended_hash = 1;
  319. priv->hash_width = 9;
  320. priv->hash_regs[0] = &priv->regs->igaddr0;
  321. priv->hash_regs[1] = &priv->regs->igaddr1;
  322. priv->hash_regs[2] = &priv->regs->igaddr2;
  323. priv->hash_regs[3] = &priv->regs->igaddr3;
  324. priv->hash_regs[4] = &priv->regs->igaddr4;
  325. priv->hash_regs[5] = &priv->regs->igaddr5;
  326. priv->hash_regs[6] = &priv->regs->igaddr6;
  327. priv->hash_regs[7] = &priv->regs->igaddr7;
  328. priv->hash_regs[8] = &priv->regs->gaddr0;
  329. priv->hash_regs[9] = &priv->regs->gaddr1;
  330. priv->hash_regs[10] = &priv->regs->gaddr2;
  331. priv->hash_regs[11] = &priv->regs->gaddr3;
  332. priv->hash_regs[12] = &priv->regs->gaddr4;
  333. priv->hash_regs[13] = &priv->regs->gaddr5;
  334. priv->hash_regs[14] = &priv->regs->gaddr6;
  335. priv->hash_regs[15] = &priv->regs->gaddr7;
  336. } else {
  337. priv->extended_hash = 0;
  338. priv->hash_width = 8;
  339. priv->hash_regs[0] = &priv->regs->gaddr0;
  340. priv->hash_regs[1] = &priv->regs->gaddr1;
  341. priv->hash_regs[2] = &priv->regs->gaddr2;
  342. priv->hash_regs[3] = &priv->regs->gaddr3;
  343. priv->hash_regs[4] = &priv->regs->gaddr4;
  344. priv->hash_regs[5] = &priv->regs->gaddr5;
  345. priv->hash_regs[6] = &priv->regs->gaddr6;
  346. priv->hash_regs[7] = &priv->regs->gaddr7;
  347. }
  348. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
  349. priv->padding = DEFAULT_PADDING;
  350. else
  351. priv->padding = 0;
  352. if (dev->features & NETIF_F_IP_CSUM)
  353. dev->hard_header_len += GMAC_FCB_LEN;
  354. priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
  355. priv->tx_ring_size = DEFAULT_TX_RING_SIZE;
  356. priv->rx_ring_size = DEFAULT_RX_RING_SIZE;
  357. priv->txcoalescing = DEFAULT_TX_COALESCE;
  358. priv->txic = DEFAULT_TXIC;
  359. priv->rxcoalescing = DEFAULT_RX_COALESCE;
  360. priv->rxic = DEFAULT_RXIC;
  361. /* Enable most messages by default */
  362. priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  363. /* Carrier starts down, phylib will bring it up */
  364. netif_carrier_off(dev);
  365. err = register_netdev(dev);
  366. if (err) {
  367. printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
  368. dev->name);
  369. goto register_fail;
  370. }
  371. /* Create all the sysfs files */
  372. gfar_init_sysfs(dev);
  373. /* Print out the device info */
  374. printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
  375. /* Even more device info helps when determining which kernel */
  376. /* provided which set of benchmarks. */
  377. printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
  378. printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
  379. dev->name, priv->rx_ring_size, priv->tx_ring_size);
  380. return 0;
  381. register_fail:
  382. iounmap(priv->regs);
  383. regs_fail:
  384. free_netdev(dev);
  385. return err;
  386. }
  387. static int gfar_remove(struct of_device *ofdev)
  388. {
  389. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  390. dev_set_drvdata(&ofdev->dev, NULL);
  391. iounmap(priv->regs);
  392. free_netdev(priv->dev);
  393. return 0;
  394. }
  395. #ifdef CONFIG_PM
  396. static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
  397. {
  398. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  399. struct net_device *dev = priv->dev;
  400. unsigned long flags;
  401. u32 tempval;
  402. int magic_packet = priv->wol_en &&
  403. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  404. netif_device_detach(dev);
  405. if (netif_running(dev)) {
  406. spin_lock_irqsave(&priv->txlock, flags);
  407. spin_lock(&priv->rxlock);
  408. gfar_halt_nodisable(dev);
  409. /* Disable Tx, and Rx if wake-on-LAN is disabled. */
  410. tempval = gfar_read(&priv->regs->maccfg1);
  411. tempval &= ~MACCFG1_TX_EN;
  412. if (!magic_packet)
  413. tempval &= ~MACCFG1_RX_EN;
  414. gfar_write(&priv->regs->maccfg1, tempval);
  415. spin_unlock(&priv->rxlock);
  416. spin_unlock_irqrestore(&priv->txlock, flags);
  417. napi_disable(&priv->napi);
  418. if (magic_packet) {
  419. /* Enable interrupt on Magic Packet */
  420. gfar_write(&priv->regs->imask, IMASK_MAG);
  421. /* Enable Magic Packet mode */
  422. tempval = gfar_read(&priv->regs->maccfg2);
  423. tempval |= MACCFG2_MPEN;
  424. gfar_write(&priv->regs->maccfg2, tempval);
  425. } else {
  426. phy_stop(priv->phydev);
  427. }
  428. }
  429. return 0;
  430. }
  431. static int gfar_resume(struct of_device *ofdev)
  432. {
  433. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  434. struct net_device *dev = priv->dev;
  435. unsigned long flags;
  436. u32 tempval;
  437. int magic_packet = priv->wol_en &&
  438. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  439. if (!netif_running(dev)) {
  440. netif_device_attach(dev);
  441. return 0;
  442. }
  443. if (!magic_packet && priv->phydev)
  444. phy_start(priv->phydev);
  445. /* Disable Magic Packet mode, in case something
  446. * else woke us up.
  447. */
  448. spin_lock_irqsave(&priv->txlock, flags);
  449. spin_lock(&priv->rxlock);
  450. tempval = gfar_read(&priv->regs->maccfg2);
  451. tempval &= ~MACCFG2_MPEN;
  452. gfar_write(&priv->regs->maccfg2, tempval);
  453. gfar_start(dev);
  454. spin_unlock(&priv->rxlock);
  455. spin_unlock_irqrestore(&priv->txlock, flags);
  456. netif_device_attach(dev);
  457. napi_enable(&priv->napi);
  458. return 0;
  459. }
  460. #else
  461. #define gfar_suspend NULL
  462. #define gfar_resume NULL
  463. #endif
  464. /* Reads the controller's registers to determine what interface
  465. * connects it to the PHY.
  466. */
  467. static phy_interface_t gfar_get_interface(struct net_device *dev)
  468. {
  469. struct gfar_private *priv = netdev_priv(dev);
  470. u32 ecntrl = gfar_read(&priv->regs->ecntrl);
  471. if (ecntrl & ECNTRL_SGMII_MODE)
  472. return PHY_INTERFACE_MODE_SGMII;
  473. if (ecntrl & ECNTRL_TBI_MODE) {
  474. if (ecntrl & ECNTRL_REDUCED_MODE)
  475. return PHY_INTERFACE_MODE_RTBI;
  476. else
  477. return PHY_INTERFACE_MODE_TBI;
  478. }
  479. if (ecntrl & ECNTRL_REDUCED_MODE) {
  480. if (ecntrl & ECNTRL_REDUCED_MII_MODE)
  481. return PHY_INTERFACE_MODE_RMII;
  482. else {
  483. phy_interface_t interface = priv->interface;
  484. /*
  485. * This isn't autodetected right now, so it must
  486. * be set by the device tree or platform code.
  487. */
  488. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  489. return PHY_INTERFACE_MODE_RGMII_ID;
  490. return PHY_INTERFACE_MODE_RGMII;
  491. }
  492. }
  493. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  494. return PHY_INTERFACE_MODE_GMII;
  495. return PHY_INTERFACE_MODE_MII;
  496. }
  497. /* Initializes driver's PHY state, and attaches to the PHY.
  498. * Returns 0 on success.
  499. */
  500. static int init_phy(struct net_device *dev)
  501. {
  502. struct gfar_private *priv = netdev_priv(dev);
  503. uint gigabit_support =
  504. priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
  505. SUPPORTED_1000baseT_Full : 0;
  506. struct phy_device *phydev;
  507. phy_interface_t interface;
  508. priv->oldlink = 0;
  509. priv->oldspeed = 0;
  510. priv->oldduplex = -1;
  511. interface = gfar_get_interface(dev);
  512. phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
  513. if (interface == PHY_INTERFACE_MODE_SGMII)
  514. gfar_configure_serdes(dev);
  515. if (IS_ERR(phydev)) {
  516. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  517. return PTR_ERR(phydev);
  518. }
  519. /* Remove any features not supported by the controller */
  520. phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
  521. phydev->advertising = phydev->supported;
  522. priv->phydev = phydev;
  523. return 0;
  524. }
  525. /*
  526. * Initialize TBI PHY interface for communicating with the
  527. * SERDES lynx PHY on the chip. We communicate with this PHY
  528. * through the MDIO bus on each controller, treating it as a
  529. * "normal" PHY at the address found in the TBIPA register. We assume
  530. * that the TBIPA register is valid. Either the MDIO bus code will set
  531. * it to a value that doesn't conflict with other PHYs on the bus, or the
  532. * value doesn't matter, as there are no other PHYs on the bus.
  533. */
  534. static void gfar_configure_serdes(struct net_device *dev)
  535. {
  536. struct gfar_private *priv = netdev_priv(dev);
  537. if (!priv->tbiphy) {
  538. printk(KERN_WARNING "SGMII mode requires that the device "
  539. "tree specify a tbi-handle\n");
  540. return;
  541. }
  542. /*
  543. * If the link is already up, we must already be ok, and don't need to
  544. * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
  545. * everything for us? Resetting it takes the link down and requires
  546. * several seconds for it to come back.
  547. */
  548. if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
  549. return;
  550. /* Single clk mode, mii mode off(for serdes communication) */
  551. phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
  552. phy_write(priv->tbiphy, MII_ADVERTISE,
  553. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  554. ADVERTISE_1000XPSE_ASYM);
  555. phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
  556. BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
  557. }
  558. static void init_registers(struct net_device *dev)
  559. {
  560. struct gfar_private *priv = netdev_priv(dev);
  561. /* Clear IEVENT */
  562. gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
  563. /* Initialize IMASK */
  564. gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
  565. /* Init hash registers to zero */
  566. gfar_write(&priv->regs->igaddr0, 0);
  567. gfar_write(&priv->regs->igaddr1, 0);
  568. gfar_write(&priv->regs->igaddr2, 0);
  569. gfar_write(&priv->regs->igaddr3, 0);
  570. gfar_write(&priv->regs->igaddr4, 0);
  571. gfar_write(&priv->regs->igaddr5, 0);
  572. gfar_write(&priv->regs->igaddr6, 0);
  573. gfar_write(&priv->regs->igaddr7, 0);
  574. gfar_write(&priv->regs->gaddr0, 0);
  575. gfar_write(&priv->regs->gaddr1, 0);
  576. gfar_write(&priv->regs->gaddr2, 0);
  577. gfar_write(&priv->regs->gaddr3, 0);
  578. gfar_write(&priv->regs->gaddr4, 0);
  579. gfar_write(&priv->regs->gaddr5, 0);
  580. gfar_write(&priv->regs->gaddr6, 0);
  581. gfar_write(&priv->regs->gaddr7, 0);
  582. /* Zero out the rmon mib registers if it has them */
  583. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  584. memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
  585. /* Mask off the CAM interrupts */
  586. gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
  587. gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
  588. }
  589. /* Initialize the max receive buffer length */
  590. gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
  591. /* Initialize the Minimum Frame Length Register */
  592. gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
  593. }
  594. /* Halt the receive and transmit queues */
  595. static void gfar_halt_nodisable(struct net_device *dev)
  596. {
  597. struct gfar_private *priv = netdev_priv(dev);
  598. struct gfar __iomem *regs = priv->regs;
  599. u32 tempval;
  600. /* Mask all interrupts */
  601. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  602. /* Clear all interrupts */
  603. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  604. /* Stop the DMA, and wait for it to stop */
  605. tempval = gfar_read(&priv->regs->dmactrl);
  606. if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
  607. != (DMACTRL_GRS | DMACTRL_GTS)) {
  608. tempval |= (DMACTRL_GRS | DMACTRL_GTS);
  609. gfar_write(&priv->regs->dmactrl, tempval);
  610. while (!(gfar_read(&priv->regs->ievent) &
  611. (IEVENT_GRSC | IEVENT_GTSC)))
  612. cpu_relax();
  613. }
  614. }
  615. /* Halt the receive and transmit queues */
  616. void gfar_halt(struct net_device *dev)
  617. {
  618. struct gfar_private *priv = netdev_priv(dev);
  619. struct gfar __iomem *regs = priv->regs;
  620. u32 tempval;
  621. gfar_halt_nodisable(dev);
  622. /* Disable Rx and Tx */
  623. tempval = gfar_read(&regs->maccfg1);
  624. tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
  625. gfar_write(&regs->maccfg1, tempval);
  626. }
  627. void stop_gfar(struct net_device *dev)
  628. {
  629. struct gfar_private *priv = netdev_priv(dev);
  630. struct gfar __iomem *regs = priv->regs;
  631. unsigned long flags;
  632. phy_stop(priv->phydev);
  633. /* Lock it down */
  634. spin_lock_irqsave(&priv->txlock, flags);
  635. spin_lock(&priv->rxlock);
  636. gfar_halt(dev);
  637. spin_unlock(&priv->rxlock);
  638. spin_unlock_irqrestore(&priv->txlock, flags);
  639. /* Free the IRQs */
  640. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  641. free_irq(priv->interruptError, dev);
  642. free_irq(priv->interruptTransmit, dev);
  643. free_irq(priv->interruptReceive, dev);
  644. } else {
  645. free_irq(priv->interruptTransmit, dev);
  646. }
  647. free_skb_resources(priv);
  648. dma_free_coherent(&dev->dev,
  649. sizeof(struct txbd8)*priv->tx_ring_size
  650. + sizeof(struct rxbd8)*priv->rx_ring_size,
  651. priv->tx_bd_base,
  652. gfar_read(&regs->tbase0));
  653. }
  654. /* If there are any tx skbs or rx skbs still around, free them.
  655. * Then free tx_skbuff and rx_skbuff */
  656. static void free_skb_resources(struct gfar_private *priv)
  657. {
  658. struct rxbd8 *rxbdp;
  659. struct txbd8 *txbdp;
  660. int i;
  661. /* Go through all the buffer descriptors and free their data buffers */
  662. txbdp = priv->tx_bd_base;
  663. for (i = 0; i < priv->tx_ring_size; i++) {
  664. if (priv->tx_skbuff[i]) {
  665. dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
  666. txbdp->length,
  667. DMA_TO_DEVICE);
  668. dev_kfree_skb_any(priv->tx_skbuff[i]);
  669. priv->tx_skbuff[i] = NULL;
  670. }
  671. txbdp++;
  672. }
  673. kfree(priv->tx_skbuff);
  674. rxbdp = priv->rx_bd_base;
  675. /* rx_skbuff is not guaranteed to be allocated, so only
  676. * free it and its contents if it is allocated */
  677. if(priv->rx_skbuff != NULL) {
  678. for (i = 0; i < priv->rx_ring_size; i++) {
  679. if (priv->rx_skbuff[i]) {
  680. dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
  681. priv->rx_buffer_size,
  682. DMA_FROM_DEVICE);
  683. dev_kfree_skb_any(priv->rx_skbuff[i]);
  684. priv->rx_skbuff[i] = NULL;
  685. }
  686. rxbdp->status = 0;
  687. rxbdp->length = 0;
  688. rxbdp->bufPtr = 0;
  689. rxbdp++;
  690. }
  691. kfree(priv->rx_skbuff);
  692. }
  693. }
  694. void gfar_start(struct net_device *dev)
  695. {
  696. struct gfar_private *priv = netdev_priv(dev);
  697. struct gfar __iomem *regs = priv->regs;
  698. u32 tempval;
  699. /* Enable Rx and Tx in MACCFG1 */
  700. tempval = gfar_read(&regs->maccfg1);
  701. tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  702. gfar_write(&regs->maccfg1, tempval);
  703. /* Initialize DMACTRL to have WWR and WOP */
  704. tempval = gfar_read(&priv->regs->dmactrl);
  705. tempval |= DMACTRL_INIT_SETTINGS;
  706. gfar_write(&priv->regs->dmactrl, tempval);
  707. /* Make sure we aren't stopped */
  708. tempval = gfar_read(&priv->regs->dmactrl);
  709. tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
  710. gfar_write(&priv->regs->dmactrl, tempval);
  711. /* Clear THLT/RHLT, so that the DMA starts polling now */
  712. gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
  713. gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
  714. /* Unmask the interrupts we look for */
  715. gfar_write(&regs->imask, IMASK_DEFAULT);
  716. dev->trans_start = jiffies;
  717. }
  718. /* Bring the controller up and running */
  719. int startup_gfar(struct net_device *dev)
  720. {
  721. struct txbd8 *txbdp;
  722. struct rxbd8 *rxbdp;
  723. dma_addr_t addr = 0;
  724. unsigned long vaddr;
  725. int i;
  726. struct gfar_private *priv = netdev_priv(dev);
  727. struct gfar __iomem *regs = priv->regs;
  728. int err = 0;
  729. u32 rctrl = 0;
  730. u32 attrs = 0;
  731. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  732. /* Allocate memory for the buffer descriptors */
  733. vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
  734. sizeof (struct txbd8) * priv->tx_ring_size +
  735. sizeof (struct rxbd8) * priv->rx_ring_size,
  736. &addr, GFP_KERNEL);
  737. if (vaddr == 0) {
  738. if (netif_msg_ifup(priv))
  739. printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
  740. dev->name);
  741. return -ENOMEM;
  742. }
  743. priv->tx_bd_base = (struct txbd8 *) vaddr;
  744. /* enet DMA only understands physical addresses */
  745. gfar_write(&regs->tbase0, addr);
  746. /* Start the rx descriptor ring where the tx ring leaves off */
  747. addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
  748. vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
  749. priv->rx_bd_base = (struct rxbd8 *) vaddr;
  750. gfar_write(&regs->rbase0, addr);
  751. /* Setup the skbuff rings */
  752. priv->tx_skbuff =
  753. (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
  754. priv->tx_ring_size, GFP_KERNEL);
  755. if (NULL == priv->tx_skbuff) {
  756. if (netif_msg_ifup(priv))
  757. printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
  758. dev->name);
  759. err = -ENOMEM;
  760. goto tx_skb_fail;
  761. }
  762. for (i = 0; i < priv->tx_ring_size; i++)
  763. priv->tx_skbuff[i] = NULL;
  764. priv->rx_skbuff =
  765. (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
  766. priv->rx_ring_size, GFP_KERNEL);
  767. if (NULL == priv->rx_skbuff) {
  768. if (netif_msg_ifup(priv))
  769. printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
  770. dev->name);
  771. err = -ENOMEM;
  772. goto rx_skb_fail;
  773. }
  774. for (i = 0; i < priv->rx_ring_size; i++)
  775. priv->rx_skbuff[i] = NULL;
  776. /* Initialize some variables in our dev structure */
  777. priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
  778. priv->cur_rx = priv->rx_bd_base;
  779. priv->skb_curtx = priv->skb_dirtytx = 0;
  780. priv->skb_currx = 0;
  781. /* Initialize Transmit Descriptor Ring */
  782. txbdp = priv->tx_bd_base;
  783. for (i = 0; i < priv->tx_ring_size; i++) {
  784. txbdp->status = 0;
  785. txbdp->length = 0;
  786. txbdp->bufPtr = 0;
  787. txbdp++;
  788. }
  789. /* Set the last descriptor in the ring to indicate wrap */
  790. txbdp--;
  791. txbdp->status |= TXBD_WRAP;
  792. rxbdp = priv->rx_bd_base;
  793. for (i = 0; i < priv->rx_ring_size; i++) {
  794. struct sk_buff *skb;
  795. skb = gfar_new_skb(dev);
  796. if (!skb) {
  797. printk(KERN_ERR "%s: Can't allocate RX buffers\n",
  798. dev->name);
  799. goto err_rxalloc_fail;
  800. }
  801. priv->rx_skbuff[i] = skb;
  802. gfar_new_rxbdp(dev, rxbdp, skb);
  803. rxbdp++;
  804. }
  805. /* Set the last descriptor in the ring to wrap */
  806. rxbdp--;
  807. rxbdp->status |= RXBD_WRAP;
  808. /* If the device has multiple interrupts, register for
  809. * them. Otherwise, only register for the one */
  810. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  811. /* Install our interrupt handlers for Error,
  812. * Transmit, and Receive */
  813. if (request_irq(priv->interruptError, gfar_error,
  814. 0, "enet_error", dev) < 0) {
  815. if (netif_msg_intr(priv))
  816. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  817. dev->name, priv->interruptError);
  818. err = -1;
  819. goto err_irq_fail;
  820. }
  821. if (request_irq(priv->interruptTransmit, gfar_transmit,
  822. 0, "enet_tx", dev) < 0) {
  823. if (netif_msg_intr(priv))
  824. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  825. dev->name, priv->interruptTransmit);
  826. err = -1;
  827. goto tx_irq_fail;
  828. }
  829. if (request_irq(priv->interruptReceive, gfar_receive,
  830. 0, "enet_rx", dev) < 0) {
  831. if (netif_msg_intr(priv))
  832. printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
  833. dev->name, priv->interruptReceive);
  834. err = -1;
  835. goto rx_irq_fail;
  836. }
  837. } else {
  838. if (request_irq(priv->interruptTransmit, gfar_interrupt,
  839. 0, "gfar_interrupt", dev) < 0) {
  840. if (netif_msg_intr(priv))
  841. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  842. dev->name, priv->interruptError);
  843. err = -1;
  844. goto err_irq_fail;
  845. }
  846. }
  847. phy_start(priv->phydev);
  848. /* Configure the coalescing support */
  849. gfar_write(&regs->txic, 0);
  850. if (priv->txcoalescing)
  851. gfar_write(&regs->txic, priv->txic);
  852. gfar_write(&regs->rxic, 0);
  853. if (priv->rxcoalescing)
  854. gfar_write(&regs->rxic, priv->rxic);
  855. if (priv->rx_csum_enable)
  856. rctrl |= RCTRL_CHECKSUMMING;
  857. if (priv->extended_hash) {
  858. rctrl |= RCTRL_EXTHASH;
  859. gfar_clear_exact_match(dev);
  860. rctrl |= RCTRL_EMEN;
  861. }
  862. if (priv->padding) {
  863. rctrl &= ~RCTRL_PAL_MASK;
  864. rctrl |= RCTRL_PADDING(priv->padding);
  865. }
  866. /* Init rctrl based on our settings */
  867. gfar_write(&priv->regs->rctrl, rctrl);
  868. if (dev->features & NETIF_F_IP_CSUM)
  869. gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
  870. /* Set the extraction length and index */
  871. attrs = ATTRELI_EL(priv->rx_stash_size) |
  872. ATTRELI_EI(priv->rx_stash_index);
  873. gfar_write(&priv->regs->attreli, attrs);
  874. /* Start with defaults, and add stashing or locking
  875. * depending on the approprate variables */
  876. attrs = ATTR_INIT_SETTINGS;
  877. if (priv->bd_stash_en)
  878. attrs |= ATTR_BDSTASH;
  879. if (priv->rx_stash_size != 0)
  880. attrs |= ATTR_BUFSTASH;
  881. gfar_write(&priv->regs->attr, attrs);
  882. gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
  883. gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
  884. gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
  885. /* Start the controller */
  886. gfar_start(dev);
  887. return 0;
  888. rx_irq_fail:
  889. free_irq(priv->interruptTransmit, dev);
  890. tx_irq_fail:
  891. free_irq(priv->interruptError, dev);
  892. err_irq_fail:
  893. err_rxalloc_fail:
  894. rx_skb_fail:
  895. free_skb_resources(priv);
  896. tx_skb_fail:
  897. dma_free_coherent(&dev->dev,
  898. sizeof(struct txbd8)*priv->tx_ring_size
  899. + sizeof(struct rxbd8)*priv->rx_ring_size,
  900. priv->tx_bd_base,
  901. gfar_read(&regs->tbase0));
  902. return err;
  903. }
  904. /* Called when something needs to use the ethernet device */
  905. /* Returns 0 for success. */
  906. static int gfar_enet_open(struct net_device *dev)
  907. {
  908. struct gfar_private *priv = netdev_priv(dev);
  909. int err;
  910. napi_enable(&priv->napi);
  911. /* Initialize a bunch of registers */
  912. init_registers(dev);
  913. gfar_set_mac_address(dev);
  914. err = init_phy(dev);
  915. if(err) {
  916. napi_disable(&priv->napi);
  917. return err;
  918. }
  919. err = startup_gfar(dev);
  920. if (err) {
  921. napi_disable(&priv->napi);
  922. return err;
  923. }
  924. netif_start_queue(dev);
  925. return err;
  926. }
  927. static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb, struct txbd8 *bdp)
  928. {
  929. struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
  930. memset(fcb, 0, GMAC_FCB_LEN);
  931. return fcb;
  932. }
  933. static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
  934. {
  935. u8 flags = 0;
  936. /* If we're here, it's a IP packet with a TCP or UDP
  937. * payload. We set it to checksum, using a pseudo-header
  938. * we provide
  939. */
  940. flags = TXFCB_DEFAULT;
  941. /* Tell the controller what the protocol is */
  942. /* And provide the already calculated phcs */
  943. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  944. flags |= TXFCB_UDP;
  945. fcb->phcs = udp_hdr(skb)->check;
  946. } else
  947. fcb->phcs = tcp_hdr(skb)->check;
  948. /* l3os is the distance between the start of the
  949. * frame (skb->data) and the start of the IP hdr.
  950. * l4os is the distance between the start of the
  951. * l3 hdr and the l4 hdr */
  952. fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
  953. fcb->l4os = skb_network_header_len(skb);
  954. fcb->flags = flags;
  955. }
  956. void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
  957. {
  958. fcb->flags |= TXFCB_VLN;
  959. fcb->vlctl = vlan_tx_tag_get(skb);
  960. }
  961. /* This is called by the kernel when a frame is ready for transmission. */
  962. /* It is pointed to by the dev->hard_start_xmit function pointer */
  963. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
  964. {
  965. struct gfar_private *priv = netdev_priv(dev);
  966. struct txfcb *fcb = NULL;
  967. struct txbd8 *txbdp;
  968. u16 status;
  969. unsigned long flags;
  970. /* Update transmit stats */
  971. dev->stats.tx_bytes += skb->len;
  972. /* Lock priv now */
  973. spin_lock_irqsave(&priv->txlock, flags);
  974. /* Point at the first free tx descriptor */
  975. txbdp = priv->cur_tx;
  976. /* Clear all but the WRAP status flags */
  977. status = txbdp->status & TXBD_WRAP;
  978. /* Set up checksumming */
  979. if (CHECKSUM_PARTIAL == skb->ip_summed) {
  980. fcb = gfar_add_fcb(skb, txbdp);
  981. status |= TXBD_TOE;
  982. gfar_tx_checksum(skb, fcb);
  983. }
  984. if (priv->vlgrp && vlan_tx_tag_present(skb)) {
  985. if (unlikely(NULL == fcb)) {
  986. fcb = gfar_add_fcb(skb, txbdp);
  987. status |= TXBD_TOE;
  988. }
  989. gfar_tx_vlan(skb, fcb);
  990. }
  991. /* Set buffer length and pointer */
  992. txbdp->length = skb->len;
  993. txbdp->bufPtr = dma_map_single(&dev->dev, skb->data,
  994. skb->len, DMA_TO_DEVICE);
  995. /* Save the skb pointer so we can free it later */
  996. priv->tx_skbuff[priv->skb_curtx] = skb;
  997. /* Update the current skb pointer (wrapping if this was the last) */
  998. priv->skb_curtx =
  999. (priv->skb_curtx + 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
  1000. /* Flag the BD as interrupt-causing */
  1001. status |= TXBD_INTERRUPT;
  1002. /* Flag the BD as ready to go, last in frame, and */
  1003. /* in need of CRC */
  1004. status |= (TXBD_READY | TXBD_LAST | TXBD_CRC);
  1005. dev->trans_start = jiffies;
  1006. /* The powerpc-specific eieio() is used, as wmb() has too strong
  1007. * semantics (it requires synchronization between cacheable and
  1008. * uncacheable mappings, which eieio doesn't provide and which we
  1009. * don't need), thus requiring a more expensive sync instruction. At
  1010. * some point, the set of architecture-independent barrier functions
  1011. * should be expanded to include weaker barriers.
  1012. */
  1013. eieio();
  1014. txbdp->status = status;
  1015. /* If this was the last BD in the ring, the next one */
  1016. /* is at the beginning of the ring */
  1017. if (txbdp->status & TXBD_WRAP)
  1018. txbdp = priv->tx_bd_base;
  1019. else
  1020. txbdp++;
  1021. /* If the next BD still needs to be cleaned up, then the bds
  1022. are full. We need to tell the kernel to stop sending us stuff. */
  1023. if (txbdp == priv->dirty_tx) {
  1024. netif_stop_queue(dev);
  1025. dev->stats.tx_fifo_errors++;
  1026. }
  1027. /* Update the current txbd to the next one */
  1028. priv->cur_tx = txbdp;
  1029. /* Tell the DMA to go go go */
  1030. gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
  1031. /* Unlock priv */
  1032. spin_unlock_irqrestore(&priv->txlock, flags);
  1033. return 0;
  1034. }
  1035. /* Stops the kernel queue, and halts the controller */
  1036. static int gfar_close(struct net_device *dev)
  1037. {
  1038. struct gfar_private *priv = netdev_priv(dev);
  1039. napi_disable(&priv->napi);
  1040. cancel_work_sync(&priv->reset_task);
  1041. stop_gfar(dev);
  1042. /* Disconnect from the PHY */
  1043. phy_disconnect(priv->phydev);
  1044. priv->phydev = NULL;
  1045. netif_stop_queue(dev);
  1046. return 0;
  1047. }
  1048. /* Changes the mac address if the controller is not running. */
  1049. static int gfar_set_mac_address(struct net_device *dev)
  1050. {
  1051. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  1052. return 0;
  1053. }
  1054. /* Enables and disables VLAN insertion/extraction */
  1055. static void gfar_vlan_rx_register(struct net_device *dev,
  1056. struct vlan_group *grp)
  1057. {
  1058. struct gfar_private *priv = netdev_priv(dev);
  1059. unsigned long flags;
  1060. struct vlan_group *old_grp;
  1061. u32 tempval;
  1062. spin_lock_irqsave(&priv->rxlock, flags);
  1063. old_grp = priv->vlgrp;
  1064. if (old_grp == grp)
  1065. return;
  1066. if (grp) {
  1067. /* Enable VLAN tag insertion */
  1068. tempval = gfar_read(&priv->regs->tctrl);
  1069. tempval |= TCTRL_VLINS;
  1070. gfar_write(&priv->regs->tctrl, tempval);
  1071. /* Enable VLAN tag extraction */
  1072. tempval = gfar_read(&priv->regs->rctrl);
  1073. tempval |= RCTRL_VLEX;
  1074. tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
  1075. gfar_write(&priv->regs->rctrl, tempval);
  1076. } else {
  1077. /* Disable VLAN tag insertion */
  1078. tempval = gfar_read(&priv->regs->tctrl);
  1079. tempval &= ~TCTRL_VLINS;
  1080. gfar_write(&priv->regs->tctrl, tempval);
  1081. /* Disable VLAN tag extraction */
  1082. tempval = gfar_read(&priv->regs->rctrl);
  1083. tempval &= ~RCTRL_VLEX;
  1084. /* If parse is no longer required, then disable parser */
  1085. if (tempval & RCTRL_REQ_PARSER)
  1086. tempval |= RCTRL_PRSDEP_INIT;
  1087. else
  1088. tempval &= ~RCTRL_PRSDEP_INIT;
  1089. gfar_write(&priv->regs->rctrl, tempval);
  1090. }
  1091. gfar_change_mtu(dev, dev->mtu);
  1092. spin_unlock_irqrestore(&priv->rxlock, flags);
  1093. }
  1094. static int gfar_change_mtu(struct net_device *dev, int new_mtu)
  1095. {
  1096. int tempsize, tempval;
  1097. struct gfar_private *priv = netdev_priv(dev);
  1098. int oldsize = priv->rx_buffer_size;
  1099. int frame_size = new_mtu + ETH_HLEN;
  1100. if (priv->vlgrp)
  1101. frame_size += VLAN_HLEN;
  1102. if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
  1103. if (netif_msg_drv(priv))
  1104. printk(KERN_ERR "%s: Invalid MTU setting\n",
  1105. dev->name);
  1106. return -EINVAL;
  1107. }
  1108. if (gfar_uses_fcb(priv))
  1109. frame_size += GMAC_FCB_LEN;
  1110. frame_size += priv->padding;
  1111. tempsize =
  1112. (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
  1113. INCREMENTAL_BUFFER_SIZE;
  1114. /* Only stop and start the controller if it isn't already
  1115. * stopped, and we changed something */
  1116. if ((oldsize != tempsize) && (dev->flags & IFF_UP))
  1117. stop_gfar(dev);
  1118. priv->rx_buffer_size = tempsize;
  1119. dev->mtu = new_mtu;
  1120. gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
  1121. gfar_write(&priv->regs->maxfrm, priv->rx_buffer_size);
  1122. /* If the mtu is larger than the max size for standard
  1123. * ethernet frames (ie, a jumbo frame), then set maccfg2
  1124. * to allow huge frames, and to check the length */
  1125. tempval = gfar_read(&priv->regs->maccfg2);
  1126. if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
  1127. tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
  1128. else
  1129. tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
  1130. gfar_write(&priv->regs->maccfg2, tempval);
  1131. if ((oldsize != tempsize) && (dev->flags & IFF_UP))
  1132. startup_gfar(dev);
  1133. return 0;
  1134. }
  1135. /* gfar_reset_task gets scheduled when a packet has not been
  1136. * transmitted after a set amount of time.
  1137. * For now, assume that clearing out all the structures, and
  1138. * starting over will fix the problem.
  1139. */
  1140. static void gfar_reset_task(struct work_struct *work)
  1141. {
  1142. struct gfar_private *priv = container_of(work, struct gfar_private,
  1143. reset_task);
  1144. struct net_device *dev = priv->dev;
  1145. if (dev->flags & IFF_UP) {
  1146. stop_gfar(dev);
  1147. startup_gfar(dev);
  1148. }
  1149. netif_tx_schedule_all(dev);
  1150. }
  1151. static void gfar_timeout(struct net_device *dev)
  1152. {
  1153. struct gfar_private *priv = netdev_priv(dev);
  1154. dev->stats.tx_errors++;
  1155. schedule_work(&priv->reset_task);
  1156. }
  1157. /* Interrupt Handler for Transmit complete */
  1158. static int gfar_clean_tx_ring(struct net_device *dev)
  1159. {
  1160. struct txbd8 *bdp;
  1161. struct gfar_private *priv = netdev_priv(dev);
  1162. int howmany = 0;
  1163. bdp = priv->dirty_tx;
  1164. while ((bdp->status & TXBD_READY) == 0) {
  1165. /* If dirty_tx and cur_tx are the same, then either the */
  1166. /* ring is empty or full now (it could only be full in the beginning, */
  1167. /* obviously). If it is empty, we are done. */
  1168. if ((bdp == priv->cur_tx) && (netif_queue_stopped(dev) == 0))
  1169. break;
  1170. howmany++;
  1171. /* Deferred means some collisions occurred during transmit, */
  1172. /* but we eventually sent the packet. */
  1173. if (bdp->status & TXBD_DEF)
  1174. dev->stats.collisions++;
  1175. /* Unmap the DMA memory */
  1176. dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
  1177. bdp->length, DMA_TO_DEVICE);
  1178. /* Free the sk buffer associated with this TxBD */
  1179. dev_kfree_skb_irq(priv->tx_skbuff[priv->skb_dirtytx]);
  1180. priv->tx_skbuff[priv->skb_dirtytx] = NULL;
  1181. priv->skb_dirtytx =
  1182. (priv->skb_dirtytx +
  1183. 1) & TX_RING_MOD_MASK(priv->tx_ring_size);
  1184. /* Clean BD length for empty detection */
  1185. bdp->length = 0;
  1186. /* update bdp to point at next bd in the ring (wrapping if necessary) */
  1187. if (bdp->status & TXBD_WRAP)
  1188. bdp = priv->tx_bd_base;
  1189. else
  1190. bdp++;
  1191. /* Move dirty_tx to be the next bd */
  1192. priv->dirty_tx = bdp;
  1193. /* We freed a buffer, so now we can restart transmission */
  1194. if (netif_queue_stopped(dev))
  1195. netif_wake_queue(dev);
  1196. } /* while ((bdp->status & TXBD_READY) == 0) */
  1197. dev->stats.tx_packets += howmany;
  1198. return howmany;
  1199. }
  1200. /* Interrupt Handler for Transmit complete */
  1201. static irqreturn_t gfar_transmit(int irq, void *dev_id)
  1202. {
  1203. struct net_device *dev = (struct net_device *) dev_id;
  1204. struct gfar_private *priv = netdev_priv(dev);
  1205. /* Clear IEVENT */
  1206. gfar_write(&priv->regs->ievent, IEVENT_TX_MASK);
  1207. /* Lock priv */
  1208. spin_lock(&priv->txlock);
  1209. gfar_clean_tx_ring(dev);
  1210. /* If we are coalescing the interrupts, reset the timer */
  1211. /* Otherwise, clear it */
  1212. if (likely(priv->txcoalescing)) {
  1213. gfar_write(&priv->regs->txic, 0);
  1214. gfar_write(&priv->regs->txic, priv->txic);
  1215. }
  1216. spin_unlock(&priv->txlock);
  1217. return IRQ_HANDLED;
  1218. }
  1219. static void gfar_new_rxbdp(struct net_device *dev, struct rxbd8 *bdp,
  1220. struct sk_buff *skb)
  1221. {
  1222. struct gfar_private *priv = netdev_priv(dev);
  1223. u32 * status_len = (u32 *)bdp;
  1224. u16 flags;
  1225. bdp->bufPtr = dma_map_single(&dev->dev, skb->data,
  1226. priv->rx_buffer_size, DMA_FROM_DEVICE);
  1227. flags = RXBD_EMPTY | RXBD_INTERRUPT;
  1228. if (bdp == priv->rx_bd_base + priv->rx_ring_size - 1)
  1229. flags |= RXBD_WRAP;
  1230. eieio();
  1231. *status_len = (u32)flags << 16;
  1232. }
  1233. struct sk_buff * gfar_new_skb(struct net_device *dev)
  1234. {
  1235. unsigned int alignamount;
  1236. struct gfar_private *priv = netdev_priv(dev);
  1237. struct sk_buff *skb = NULL;
  1238. /* We have to allocate the skb, so keep trying till we succeed */
  1239. skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
  1240. if (!skb)
  1241. return NULL;
  1242. alignamount = RXBUF_ALIGNMENT -
  1243. (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
  1244. /* We need the data buffer to be aligned properly. We will reserve
  1245. * as many bytes as needed to align the data properly
  1246. */
  1247. skb_reserve(skb, alignamount);
  1248. return skb;
  1249. }
  1250. static inline void count_errors(unsigned short status, struct net_device *dev)
  1251. {
  1252. struct gfar_private *priv = netdev_priv(dev);
  1253. struct net_device_stats *stats = &dev->stats;
  1254. struct gfar_extra_stats *estats = &priv->extra_stats;
  1255. /* If the packet was truncated, none of the other errors
  1256. * matter */
  1257. if (status & RXBD_TRUNCATED) {
  1258. stats->rx_length_errors++;
  1259. estats->rx_trunc++;
  1260. return;
  1261. }
  1262. /* Count the errors, if there were any */
  1263. if (status & (RXBD_LARGE | RXBD_SHORT)) {
  1264. stats->rx_length_errors++;
  1265. if (status & RXBD_LARGE)
  1266. estats->rx_large++;
  1267. else
  1268. estats->rx_short++;
  1269. }
  1270. if (status & RXBD_NONOCTET) {
  1271. stats->rx_frame_errors++;
  1272. estats->rx_nonoctet++;
  1273. }
  1274. if (status & RXBD_CRCERR) {
  1275. estats->rx_crcerr++;
  1276. stats->rx_crc_errors++;
  1277. }
  1278. if (status & RXBD_OVERRUN) {
  1279. estats->rx_overrun++;
  1280. stats->rx_crc_errors++;
  1281. }
  1282. }
  1283. irqreturn_t gfar_receive(int irq, void *dev_id)
  1284. {
  1285. struct net_device *dev = (struct net_device *) dev_id;
  1286. struct gfar_private *priv = netdev_priv(dev);
  1287. u32 tempval;
  1288. /* support NAPI */
  1289. /* Clear IEVENT, so interrupts aren't called again
  1290. * because of the packets that have already arrived */
  1291. gfar_write(&priv->regs->ievent, IEVENT_RTX_MASK);
  1292. if (netif_rx_schedule_prep(dev, &priv->napi)) {
  1293. tempval = gfar_read(&priv->regs->imask);
  1294. tempval &= IMASK_RTX_DISABLED;
  1295. gfar_write(&priv->regs->imask, tempval);
  1296. __netif_rx_schedule(dev, &priv->napi);
  1297. } else {
  1298. if (netif_msg_rx_err(priv))
  1299. printk(KERN_DEBUG "%s: receive called twice (%x)[%x]\n",
  1300. dev->name, gfar_read(&priv->regs->ievent),
  1301. gfar_read(&priv->regs->imask));
  1302. }
  1303. return IRQ_HANDLED;
  1304. }
  1305. static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
  1306. {
  1307. /* If valid headers were found, and valid sums
  1308. * were verified, then we tell the kernel that no
  1309. * checksumming is necessary. Otherwise, it is */
  1310. if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
  1311. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1312. else
  1313. skb->ip_summed = CHECKSUM_NONE;
  1314. }
  1315. static inline struct rxfcb *gfar_get_fcb(struct sk_buff *skb)
  1316. {
  1317. struct rxfcb *fcb = (struct rxfcb *)skb->data;
  1318. /* Remove the FCB from the skb */
  1319. skb_pull(skb, GMAC_FCB_LEN);
  1320. return fcb;
  1321. }
  1322. /* gfar_process_frame() -- handle one incoming packet if skb
  1323. * isn't NULL. */
  1324. static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
  1325. int length)
  1326. {
  1327. struct gfar_private *priv = netdev_priv(dev);
  1328. struct rxfcb *fcb = NULL;
  1329. if (NULL == skb) {
  1330. if (netif_msg_rx_err(priv))
  1331. printk(KERN_WARNING "%s: Missing skb!!.\n", dev->name);
  1332. dev->stats.rx_dropped++;
  1333. priv->extra_stats.rx_skbmissing++;
  1334. } else {
  1335. int ret;
  1336. /* Prep the skb for the packet */
  1337. skb_put(skb, length);
  1338. /* Grab the FCB if there is one */
  1339. if (gfar_uses_fcb(priv))
  1340. fcb = gfar_get_fcb(skb);
  1341. /* Remove the padded bytes, if there are any */
  1342. if (priv->padding)
  1343. skb_pull(skb, priv->padding);
  1344. if (priv->rx_csum_enable)
  1345. gfar_rx_checksum(skb, fcb);
  1346. /* Tell the skb what kind of packet this is */
  1347. skb->protocol = eth_type_trans(skb, dev);
  1348. /* Send the packet up the stack */
  1349. if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN))) {
  1350. ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp,
  1351. fcb->vlctl);
  1352. } else
  1353. ret = netif_receive_skb(skb);
  1354. if (NET_RX_DROP == ret)
  1355. priv->extra_stats.kernel_dropped++;
  1356. }
  1357. return 0;
  1358. }
  1359. /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
  1360. * until the budget/quota has been reached. Returns the number
  1361. * of frames handled
  1362. */
  1363. int gfar_clean_rx_ring(struct net_device *dev, int rx_work_limit)
  1364. {
  1365. struct rxbd8 *bdp;
  1366. struct sk_buff *skb;
  1367. u16 pkt_len;
  1368. int howmany = 0;
  1369. struct gfar_private *priv = netdev_priv(dev);
  1370. /* Get the first full descriptor */
  1371. bdp = priv->cur_rx;
  1372. while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
  1373. struct sk_buff *newskb;
  1374. rmb();
  1375. /* Add another skb for the future */
  1376. newskb = gfar_new_skb(dev);
  1377. skb = priv->rx_skbuff[priv->skb_currx];
  1378. dma_unmap_single(&priv->dev->dev, bdp->bufPtr,
  1379. priv->rx_buffer_size, DMA_FROM_DEVICE);
  1380. /* We drop the frame if we failed to allocate a new buffer */
  1381. if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
  1382. bdp->status & RXBD_ERR)) {
  1383. count_errors(bdp->status, dev);
  1384. if (unlikely(!newskb))
  1385. newskb = skb;
  1386. if (skb)
  1387. dev_kfree_skb_any(skb);
  1388. } else {
  1389. /* Increment the number of packets */
  1390. dev->stats.rx_packets++;
  1391. howmany++;
  1392. /* Remove the FCS from the packet length */
  1393. pkt_len = bdp->length - 4;
  1394. gfar_process_frame(dev, skb, pkt_len);
  1395. dev->stats.rx_bytes += pkt_len;
  1396. }
  1397. priv->rx_skbuff[priv->skb_currx] = newskb;
  1398. /* Setup the new bdp */
  1399. gfar_new_rxbdp(dev, bdp, newskb);
  1400. /* Update to the next pointer */
  1401. if (bdp->status & RXBD_WRAP)
  1402. bdp = priv->rx_bd_base;
  1403. else
  1404. bdp++;
  1405. /* update to point at the next skb */
  1406. priv->skb_currx =
  1407. (priv->skb_currx + 1) &
  1408. RX_RING_MOD_MASK(priv->rx_ring_size);
  1409. }
  1410. /* Update the current rxbd pointer to be the next one */
  1411. priv->cur_rx = bdp;
  1412. return howmany;
  1413. }
  1414. static int gfar_poll(struct napi_struct *napi, int budget)
  1415. {
  1416. struct gfar_private *priv = container_of(napi, struct gfar_private, napi);
  1417. struct net_device *dev = priv->dev;
  1418. int howmany;
  1419. unsigned long flags;
  1420. /* If we fail to get the lock, don't bother with the TX BDs */
  1421. if (spin_trylock_irqsave(&priv->txlock, flags)) {
  1422. gfar_clean_tx_ring(dev);
  1423. spin_unlock_irqrestore(&priv->txlock, flags);
  1424. }
  1425. howmany = gfar_clean_rx_ring(dev, budget);
  1426. if (howmany < budget) {
  1427. netif_rx_complete(dev, napi);
  1428. /* Clear the halt bit in RSTAT */
  1429. gfar_write(&priv->regs->rstat, RSTAT_CLEAR_RHALT);
  1430. gfar_write(&priv->regs->imask, IMASK_DEFAULT);
  1431. /* If we are coalescing interrupts, update the timer */
  1432. /* Otherwise, clear it */
  1433. if (likely(priv->rxcoalescing)) {
  1434. gfar_write(&priv->regs->rxic, 0);
  1435. gfar_write(&priv->regs->rxic, priv->rxic);
  1436. }
  1437. }
  1438. return howmany;
  1439. }
  1440. #ifdef CONFIG_NET_POLL_CONTROLLER
  1441. /*
  1442. * Polling 'interrupt' - used by things like netconsole to send skbs
  1443. * without having to re-enable interrupts. It's not called while
  1444. * the interrupt routine is executing.
  1445. */
  1446. static void gfar_netpoll(struct net_device *dev)
  1447. {
  1448. struct gfar_private *priv = netdev_priv(dev);
  1449. /* If the device has multiple interrupts, run tx/rx */
  1450. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1451. disable_irq(priv->interruptTransmit);
  1452. disable_irq(priv->interruptReceive);
  1453. disable_irq(priv->interruptError);
  1454. gfar_interrupt(priv->interruptTransmit, dev);
  1455. enable_irq(priv->interruptError);
  1456. enable_irq(priv->interruptReceive);
  1457. enable_irq(priv->interruptTransmit);
  1458. } else {
  1459. disable_irq(priv->interruptTransmit);
  1460. gfar_interrupt(priv->interruptTransmit, dev);
  1461. enable_irq(priv->interruptTransmit);
  1462. }
  1463. }
  1464. #endif
  1465. /* The interrupt handler for devices with one interrupt */
  1466. static irqreturn_t gfar_interrupt(int irq, void *dev_id)
  1467. {
  1468. struct net_device *dev = dev_id;
  1469. struct gfar_private *priv = netdev_priv(dev);
  1470. /* Save ievent for future reference */
  1471. u32 events = gfar_read(&priv->regs->ievent);
  1472. /* Check for reception */
  1473. if (events & IEVENT_RX_MASK)
  1474. gfar_receive(irq, dev_id);
  1475. /* Check for transmit completion */
  1476. if (events & IEVENT_TX_MASK)
  1477. gfar_transmit(irq, dev_id);
  1478. /* Check for errors */
  1479. if (events & IEVENT_ERR_MASK)
  1480. gfar_error(irq, dev_id);
  1481. return IRQ_HANDLED;
  1482. }
  1483. /* Called every time the controller might need to be made
  1484. * aware of new link state. The PHY code conveys this
  1485. * information through variables in the phydev structure, and this
  1486. * function converts those variables into the appropriate
  1487. * register values, and can bring down the device if needed.
  1488. */
  1489. static void adjust_link(struct net_device *dev)
  1490. {
  1491. struct gfar_private *priv = netdev_priv(dev);
  1492. struct gfar __iomem *regs = priv->regs;
  1493. unsigned long flags;
  1494. struct phy_device *phydev = priv->phydev;
  1495. int new_state = 0;
  1496. spin_lock_irqsave(&priv->txlock, flags);
  1497. if (phydev->link) {
  1498. u32 tempval = gfar_read(&regs->maccfg2);
  1499. u32 ecntrl = gfar_read(&regs->ecntrl);
  1500. /* Now we make sure that we can be in full duplex mode.
  1501. * If not, we operate in half-duplex mode. */
  1502. if (phydev->duplex != priv->oldduplex) {
  1503. new_state = 1;
  1504. if (!(phydev->duplex))
  1505. tempval &= ~(MACCFG2_FULL_DUPLEX);
  1506. else
  1507. tempval |= MACCFG2_FULL_DUPLEX;
  1508. priv->oldduplex = phydev->duplex;
  1509. }
  1510. if (phydev->speed != priv->oldspeed) {
  1511. new_state = 1;
  1512. switch (phydev->speed) {
  1513. case 1000:
  1514. tempval =
  1515. ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
  1516. break;
  1517. case 100:
  1518. case 10:
  1519. tempval =
  1520. ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
  1521. /* Reduced mode distinguishes
  1522. * between 10 and 100 */
  1523. if (phydev->speed == SPEED_100)
  1524. ecntrl |= ECNTRL_R100;
  1525. else
  1526. ecntrl &= ~(ECNTRL_R100);
  1527. break;
  1528. default:
  1529. if (netif_msg_link(priv))
  1530. printk(KERN_WARNING
  1531. "%s: Ack! Speed (%d) is not 10/100/1000!\n",
  1532. dev->name, phydev->speed);
  1533. break;
  1534. }
  1535. priv->oldspeed = phydev->speed;
  1536. }
  1537. gfar_write(&regs->maccfg2, tempval);
  1538. gfar_write(&regs->ecntrl, ecntrl);
  1539. if (!priv->oldlink) {
  1540. new_state = 1;
  1541. priv->oldlink = 1;
  1542. }
  1543. } else if (priv->oldlink) {
  1544. new_state = 1;
  1545. priv->oldlink = 0;
  1546. priv->oldspeed = 0;
  1547. priv->oldduplex = -1;
  1548. }
  1549. if (new_state && netif_msg_link(priv))
  1550. phy_print_status(phydev);
  1551. spin_unlock_irqrestore(&priv->txlock, flags);
  1552. }
  1553. /* Update the hash table based on the current list of multicast
  1554. * addresses we subscribe to. Also, change the promiscuity of
  1555. * the device based on the flags (this function is called
  1556. * whenever dev->flags is changed */
  1557. static void gfar_set_multi(struct net_device *dev)
  1558. {
  1559. struct dev_mc_list *mc_ptr;
  1560. struct gfar_private *priv = netdev_priv(dev);
  1561. struct gfar __iomem *regs = priv->regs;
  1562. u32 tempval;
  1563. if(dev->flags & IFF_PROMISC) {
  1564. /* Set RCTRL to PROM */
  1565. tempval = gfar_read(&regs->rctrl);
  1566. tempval |= RCTRL_PROM;
  1567. gfar_write(&regs->rctrl, tempval);
  1568. } else {
  1569. /* Set RCTRL to not PROM */
  1570. tempval = gfar_read(&regs->rctrl);
  1571. tempval &= ~(RCTRL_PROM);
  1572. gfar_write(&regs->rctrl, tempval);
  1573. }
  1574. if(dev->flags & IFF_ALLMULTI) {
  1575. /* Set the hash to rx all multicast frames */
  1576. gfar_write(&regs->igaddr0, 0xffffffff);
  1577. gfar_write(&regs->igaddr1, 0xffffffff);
  1578. gfar_write(&regs->igaddr2, 0xffffffff);
  1579. gfar_write(&regs->igaddr3, 0xffffffff);
  1580. gfar_write(&regs->igaddr4, 0xffffffff);
  1581. gfar_write(&regs->igaddr5, 0xffffffff);
  1582. gfar_write(&regs->igaddr6, 0xffffffff);
  1583. gfar_write(&regs->igaddr7, 0xffffffff);
  1584. gfar_write(&regs->gaddr0, 0xffffffff);
  1585. gfar_write(&regs->gaddr1, 0xffffffff);
  1586. gfar_write(&regs->gaddr2, 0xffffffff);
  1587. gfar_write(&regs->gaddr3, 0xffffffff);
  1588. gfar_write(&regs->gaddr4, 0xffffffff);
  1589. gfar_write(&regs->gaddr5, 0xffffffff);
  1590. gfar_write(&regs->gaddr6, 0xffffffff);
  1591. gfar_write(&regs->gaddr7, 0xffffffff);
  1592. } else {
  1593. int em_num;
  1594. int idx;
  1595. /* zero out the hash */
  1596. gfar_write(&regs->igaddr0, 0x0);
  1597. gfar_write(&regs->igaddr1, 0x0);
  1598. gfar_write(&regs->igaddr2, 0x0);
  1599. gfar_write(&regs->igaddr3, 0x0);
  1600. gfar_write(&regs->igaddr4, 0x0);
  1601. gfar_write(&regs->igaddr5, 0x0);
  1602. gfar_write(&regs->igaddr6, 0x0);
  1603. gfar_write(&regs->igaddr7, 0x0);
  1604. gfar_write(&regs->gaddr0, 0x0);
  1605. gfar_write(&regs->gaddr1, 0x0);
  1606. gfar_write(&regs->gaddr2, 0x0);
  1607. gfar_write(&regs->gaddr3, 0x0);
  1608. gfar_write(&regs->gaddr4, 0x0);
  1609. gfar_write(&regs->gaddr5, 0x0);
  1610. gfar_write(&regs->gaddr6, 0x0);
  1611. gfar_write(&regs->gaddr7, 0x0);
  1612. /* If we have extended hash tables, we need to
  1613. * clear the exact match registers to prepare for
  1614. * setting them */
  1615. if (priv->extended_hash) {
  1616. em_num = GFAR_EM_NUM + 1;
  1617. gfar_clear_exact_match(dev);
  1618. idx = 1;
  1619. } else {
  1620. idx = 0;
  1621. em_num = 0;
  1622. }
  1623. if(dev->mc_count == 0)
  1624. return;
  1625. /* Parse the list, and set the appropriate bits */
  1626. for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
  1627. if (idx < em_num) {
  1628. gfar_set_mac_for_addr(dev, idx,
  1629. mc_ptr->dmi_addr);
  1630. idx++;
  1631. } else
  1632. gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
  1633. }
  1634. }
  1635. return;
  1636. }
  1637. /* Clears each of the exact match registers to zero, so they
  1638. * don't interfere with normal reception */
  1639. static void gfar_clear_exact_match(struct net_device *dev)
  1640. {
  1641. int idx;
  1642. u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
  1643. for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
  1644. gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
  1645. }
  1646. /* Set the appropriate hash bit for the given addr */
  1647. /* The algorithm works like so:
  1648. * 1) Take the Destination Address (ie the multicast address), and
  1649. * do a CRC on it (little endian), and reverse the bits of the
  1650. * result.
  1651. * 2) Use the 8 most significant bits as a hash into a 256-entry
  1652. * table. The table is controlled through 8 32-bit registers:
  1653. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  1654. * gaddr7. This means that the 3 most significant bits in the
  1655. * hash index which gaddr register to use, and the 5 other bits
  1656. * indicate which bit (assuming an IBM numbering scheme, which
  1657. * for PowerPC (tm) is usually the case) in the register holds
  1658. * the entry. */
  1659. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
  1660. {
  1661. u32 tempval;
  1662. struct gfar_private *priv = netdev_priv(dev);
  1663. u32 result = ether_crc(MAC_ADDR_LEN, addr);
  1664. int width = priv->hash_width;
  1665. u8 whichbit = (result >> (32 - width)) & 0x1f;
  1666. u8 whichreg = result >> (32 - width + 5);
  1667. u32 value = (1 << (31-whichbit));
  1668. tempval = gfar_read(priv->hash_regs[whichreg]);
  1669. tempval |= value;
  1670. gfar_write(priv->hash_regs[whichreg], tempval);
  1671. return;
  1672. }
  1673. /* There are multiple MAC Address register pairs on some controllers
  1674. * This function sets the numth pair to a given address
  1675. */
  1676. static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
  1677. {
  1678. struct gfar_private *priv = netdev_priv(dev);
  1679. int idx;
  1680. char tmpbuf[MAC_ADDR_LEN];
  1681. u32 tempval;
  1682. u32 __iomem *macptr = &priv->regs->macstnaddr1;
  1683. macptr += num*2;
  1684. /* Now copy it into the mac registers backwards, cuz */
  1685. /* little endian is silly */
  1686. for (idx = 0; idx < MAC_ADDR_LEN; idx++)
  1687. tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
  1688. gfar_write(macptr, *((u32 *) (tmpbuf)));
  1689. tempval = *((u32 *) (tmpbuf + 4));
  1690. gfar_write(macptr+1, tempval);
  1691. }
  1692. /* GFAR error interrupt handler */
  1693. static irqreturn_t gfar_error(int irq, void *dev_id)
  1694. {
  1695. struct net_device *dev = dev_id;
  1696. struct gfar_private *priv = netdev_priv(dev);
  1697. /* Save ievent for future reference */
  1698. u32 events = gfar_read(&priv->regs->ievent);
  1699. /* Clear IEVENT */
  1700. gfar_write(&priv->regs->ievent, events & IEVENT_ERR_MASK);
  1701. /* Magic Packet is not an error. */
  1702. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  1703. (events & IEVENT_MAG))
  1704. events &= ~IEVENT_MAG;
  1705. /* Hmm... */
  1706. if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
  1707. printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
  1708. dev->name, events, gfar_read(&priv->regs->imask));
  1709. /* Update the error counters */
  1710. if (events & IEVENT_TXE) {
  1711. dev->stats.tx_errors++;
  1712. if (events & IEVENT_LC)
  1713. dev->stats.tx_window_errors++;
  1714. if (events & IEVENT_CRL)
  1715. dev->stats.tx_aborted_errors++;
  1716. if (events & IEVENT_XFUN) {
  1717. if (netif_msg_tx_err(priv))
  1718. printk(KERN_DEBUG "%s: TX FIFO underrun, "
  1719. "packet dropped.\n", dev->name);
  1720. dev->stats.tx_dropped++;
  1721. priv->extra_stats.tx_underrun++;
  1722. /* Reactivate the Tx Queues */
  1723. gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
  1724. }
  1725. if (netif_msg_tx_err(priv))
  1726. printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
  1727. }
  1728. if (events & IEVENT_BSY) {
  1729. dev->stats.rx_errors++;
  1730. priv->extra_stats.rx_bsy++;
  1731. gfar_receive(irq, dev_id);
  1732. if (netif_msg_rx_err(priv))
  1733. printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
  1734. dev->name, gfar_read(&priv->regs->rstat));
  1735. }
  1736. if (events & IEVENT_BABR) {
  1737. dev->stats.rx_errors++;
  1738. priv->extra_stats.rx_babr++;
  1739. if (netif_msg_rx_err(priv))
  1740. printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
  1741. }
  1742. if (events & IEVENT_EBERR) {
  1743. priv->extra_stats.eberr++;
  1744. if (netif_msg_rx_err(priv))
  1745. printk(KERN_DEBUG "%s: bus error\n", dev->name);
  1746. }
  1747. if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
  1748. printk(KERN_DEBUG "%s: control frame\n", dev->name);
  1749. if (events & IEVENT_BABT) {
  1750. priv->extra_stats.tx_babt++;
  1751. if (netif_msg_tx_err(priv))
  1752. printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
  1753. }
  1754. return IRQ_HANDLED;
  1755. }
  1756. /* work with hotplug and coldplug */
  1757. MODULE_ALIAS("platform:fsl-gianfar");
  1758. static struct of_device_id gfar_match[] =
  1759. {
  1760. {
  1761. .type = "network",
  1762. .compatible = "gianfar",
  1763. },
  1764. {},
  1765. };
  1766. /* Structure for a device driver */
  1767. static struct of_platform_driver gfar_driver = {
  1768. .name = "fsl-gianfar",
  1769. .match_table = gfar_match,
  1770. .probe = gfar_probe,
  1771. .remove = gfar_remove,
  1772. .suspend = gfar_suspend,
  1773. .resume = gfar_resume,
  1774. };
  1775. static int __init gfar_init(void)
  1776. {
  1777. int err = gfar_mdio_init();
  1778. if (err)
  1779. return err;
  1780. err = of_register_platform_driver(&gfar_driver);
  1781. if (err)
  1782. gfar_mdio_exit();
  1783. return err;
  1784. }
  1785. static void __exit gfar_exit(void)
  1786. {
  1787. of_unregister_platform_driver(&gfar_driver);
  1788. gfar_mdio_exit();
  1789. }
  1790. module_init(gfar_init);
  1791. module_exit(gfar_exit);