gianfar.c 50 KB

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