gianfar.c 60 KB

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