gianfar.c 52 KB

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