gianfar.c 57 KB

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