gianfar.c 53 KB

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