gianfar.c 57 KB

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