gianfar.c 53 KB

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