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