gianfar.c 58 KB

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