gianfar.c 55 KB

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