gianfar.c 52 KB

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