gianfar.c 56 KB

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