gianfar.c 55 KB

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