gianfar.c 55 KB

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