gianfar.c 54 KB

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