gianfar.c 52 KB

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