gianfar.c 61 KB

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