gianfar.c 50 KB

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