gianfar.c 63 KB

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