gianfar.c 60 KB

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