gianfar.c 56 KB

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