gianfar.c 53 KB

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