gianfar.c 53 KB

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