gianfar.c 55 KB

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