gianfar.c 57 KB

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