gianfar.c 57 KB

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