gianfar.c 58 KB

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