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