gianfar.c 52 KB

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