gianfar.c 60 KB

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