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