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