gianfar.c 50 KB

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