gianfar.c 52 KB

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