gianfar.c 56 KB

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