gianfar.c 53 KB

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