gianfar.c 60 KB

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