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