gianfar.c 52 KB

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