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