gianfar.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327
  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. /* fill out IRQ number and name fields */
  387. len_devname = strlen(dev->name);
  388. strncpy(&priv->int_name_tx[0], dev->name, len_devname);
  389. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  390. strncpy(&priv->int_name_tx[len_devname],
  391. "_tx", sizeof("_tx") + 1);
  392. strncpy(&priv->int_name_rx[0], dev->name, len_devname);
  393. strncpy(&priv->int_name_rx[len_devname],
  394. "_rx", sizeof("_rx") + 1);
  395. strncpy(&priv->int_name_er[0], dev->name, len_devname);
  396. strncpy(&priv->int_name_er[len_devname],
  397. "_er", sizeof("_er") + 1);
  398. } else
  399. priv->int_name_tx[len_devname] = '\0';
  400. /* Create all the sysfs files */
  401. gfar_init_sysfs(dev);
  402. /* Print out the device info */
  403. printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
  404. /* Even more device info helps when determining which kernel */
  405. /* provided which set of benchmarks. */
  406. printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
  407. printk(KERN_INFO "%s: %d/%d RX/TX BD ring size\n",
  408. dev->name, priv->rx_ring_size, priv->tx_ring_size);
  409. return 0;
  410. register_fail:
  411. iounmap(priv->regs);
  412. regs_fail:
  413. free_netdev(dev);
  414. return err;
  415. }
  416. static int gfar_remove(struct of_device *ofdev)
  417. {
  418. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  419. dev_set_drvdata(&ofdev->dev, NULL);
  420. iounmap(priv->regs);
  421. free_netdev(priv->dev);
  422. return 0;
  423. }
  424. #ifdef CONFIG_PM
  425. static int gfar_suspend(struct of_device *ofdev, pm_message_t state)
  426. {
  427. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  428. struct net_device *dev = priv->dev;
  429. unsigned long flags;
  430. u32 tempval;
  431. int magic_packet = priv->wol_en &&
  432. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  433. netif_device_detach(dev);
  434. if (netif_running(dev)) {
  435. spin_lock_irqsave(&priv->txlock, flags);
  436. spin_lock(&priv->rxlock);
  437. gfar_halt_nodisable(dev);
  438. /* Disable Tx, and Rx if wake-on-LAN is disabled. */
  439. tempval = gfar_read(&priv->regs->maccfg1);
  440. tempval &= ~MACCFG1_TX_EN;
  441. if (!magic_packet)
  442. tempval &= ~MACCFG1_RX_EN;
  443. gfar_write(&priv->regs->maccfg1, tempval);
  444. spin_unlock(&priv->rxlock);
  445. spin_unlock_irqrestore(&priv->txlock, flags);
  446. napi_disable(&priv->napi);
  447. if (magic_packet) {
  448. /* Enable interrupt on Magic Packet */
  449. gfar_write(&priv->regs->imask, IMASK_MAG);
  450. /* Enable Magic Packet mode */
  451. tempval = gfar_read(&priv->regs->maccfg2);
  452. tempval |= MACCFG2_MPEN;
  453. gfar_write(&priv->regs->maccfg2, tempval);
  454. } else {
  455. phy_stop(priv->phydev);
  456. }
  457. }
  458. return 0;
  459. }
  460. static int gfar_resume(struct of_device *ofdev)
  461. {
  462. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  463. struct net_device *dev = priv->dev;
  464. unsigned long flags;
  465. u32 tempval;
  466. int magic_packet = priv->wol_en &&
  467. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  468. if (!netif_running(dev)) {
  469. netif_device_attach(dev);
  470. return 0;
  471. }
  472. if (!magic_packet && priv->phydev)
  473. phy_start(priv->phydev);
  474. /* Disable Magic Packet mode, in case something
  475. * else woke us up.
  476. */
  477. spin_lock_irqsave(&priv->txlock, flags);
  478. spin_lock(&priv->rxlock);
  479. tempval = gfar_read(&priv->regs->maccfg2);
  480. tempval &= ~MACCFG2_MPEN;
  481. gfar_write(&priv->regs->maccfg2, tempval);
  482. gfar_start(dev);
  483. spin_unlock(&priv->rxlock);
  484. spin_unlock_irqrestore(&priv->txlock, flags);
  485. netif_device_attach(dev);
  486. napi_enable(&priv->napi);
  487. return 0;
  488. }
  489. #else
  490. #define gfar_suspend NULL
  491. #define gfar_resume NULL
  492. #endif
  493. /* Reads the controller's registers to determine what interface
  494. * connects it to the PHY.
  495. */
  496. static phy_interface_t gfar_get_interface(struct net_device *dev)
  497. {
  498. struct gfar_private *priv = netdev_priv(dev);
  499. u32 ecntrl = gfar_read(&priv->regs->ecntrl);
  500. if (ecntrl & ECNTRL_SGMII_MODE)
  501. return PHY_INTERFACE_MODE_SGMII;
  502. if (ecntrl & ECNTRL_TBI_MODE) {
  503. if (ecntrl & ECNTRL_REDUCED_MODE)
  504. return PHY_INTERFACE_MODE_RTBI;
  505. else
  506. return PHY_INTERFACE_MODE_TBI;
  507. }
  508. if (ecntrl & ECNTRL_REDUCED_MODE) {
  509. if (ecntrl & ECNTRL_REDUCED_MII_MODE)
  510. return PHY_INTERFACE_MODE_RMII;
  511. else {
  512. phy_interface_t interface = priv->interface;
  513. /*
  514. * This isn't autodetected right now, so it must
  515. * be set by the device tree or platform code.
  516. */
  517. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  518. return PHY_INTERFACE_MODE_RGMII_ID;
  519. return PHY_INTERFACE_MODE_RGMII;
  520. }
  521. }
  522. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  523. return PHY_INTERFACE_MODE_GMII;
  524. return PHY_INTERFACE_MODE_MII;
  525. }
  526. /* Initializes driver's PHY state, and attaches to the PHY.
  527. * Returns 0 on success.
  528. */
  529. static int init_phy(struct net_device *dev)
  530. {
  531. struct gfar_private *priv = netdev_priv(dev);
  532. uint gigabit_support =
  533. priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
  534. SUPPORTED_1000baseT_Full : 0;
  535. struct phy_device *phydev;
  536. phy_interface_t interface;
  537. priv->oldlink = 0;
  538. priv->oldspeed = 0;
  539. priv->oldduplex = -1;
  540. interface = gfar_get_interface(dev);
  541. phydev = phy_connect(dev, priv->phy_bus_id, &adjust_link, 0, interface);
  542. if (interface == PHY_INTERFACE_MODE_SGMII)
  543. gfar_configure_serdes(dev);
  544. if (IS_ERR(phydev)) {
  545. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  546. return PTR_ERR(phydev);
  547. }
  548. /* Remove any features not supported by the controller */
  549. phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
  550. phydev->advertising = phydev->supported;
  551. priv->phydev = phydev;
  552. return 0;
  553. }
  554. /*
  555. * Initialize TBI PHY interface for communicating with the
  556. * SERDES lynx PHY on the chip. We communicate with this PHY
  557. * through the MDIO bus on each controller, treating it as a
  558. * "normal" PHY at the address found in the TBIPA register. We assume
  559. * that the TBIPA register is valid. Either the MDIO bus code will set
  560. * it to a value that doesn't conflict with other PHYs on the bus, or the
  561. * value doesn't matter, as there are no other PHYs on the bus.
  562. */
  563. static void gfar_configure_serdes(struct net_device *dev)
  564. {
  565. struct gfar_private *priv = netdev_priv(dev);
  566. if (!priv->tbiphy) {
  567. printk(KERN_WARNING "SGMII mode requires that the device "
  568. "tree specify a tbi-handle\n");
  569. return;
  570. }
  571. /*
  572. * If the link is already up, we must already be ok, and don't need to
  573. * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
  574. * everything for us? Resetting it takes the link down and requires
  575. * several seconds for it to come back.
  576. */
  577. if (phy_read(priv->tbiphy, MII_BMSR) & BMSR_LSTATUS)
  578. return;
  579. /* Single clk mode, mii mode off(for serdes communication) */
  580. phy_write(priv->tbiphy, MII_TBICON, TBICON_CLK_SELECT);
  581. phy_write(priv->tbiphy, MII_ADVERTISE,
  582. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  583. ADVERTISE_1000XPSE_ASYM);
  584. phy_write(priv->tbiphy, MII_BMCR, BMCR_ANENABLE |
  585. BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
  586. }
  587. static void init_registers(struct net_device *dev)
  588. {
  589. struct gfar_private *priv = netdev_priv(dev);
  590. /* Clear IEVENT */
  591. gfar_write(&priv->regs->ievent, IEVENT_INIT_CLEAR);
  592. /* Initialize IMASK */
  593. gfar_write(&priv->regs->imask, IMASK_INIT_CLEAR);
  594. /* Init hash registers to zero */
  595. gfar_write(&priv->regs->igaddr0, 0);
  596. gfar_write(&priv->regs->igaddr1, 0);
  597. gfar_write(&priv->regs->igaddr2, 0);
  598. gfar_write(&priv->regs->igaddr3, 0);
  599. gfar_write(&priv->regs->igaddr4, 0);
  600. gfar_write(&priv->regs->igaddr5, 0);
  601. gfar_write(&priv->regs->igaddr6, 0);
  602. gfar_write(&priv->regs->igaddr7, 0);
  603. gfar_write(&priv->regs->gaddr0, 0);
  604. gfar_write(&priv->regs->gaddr1, 0);
  605. gfar_write(&priv->regs->gaddr2, 0);
  606. gfar_write(&priv->regs->gaddr3, 0);
  607. gfar_write(&priv->regs->gaddr4, 0);
  608. gfar_write(&priv->regs->gaddr5, 0);
  609. gfar_write(&priv->regs->gaddr6, 0);
  610. gfar_write(&priv->regs->gaddr7, 0);
  611. /* Zero out the rmon mib registers if it has them */
  612. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  613. memset_io(&(priv->regs->rmon), 0, sizeof (struct rmon_mib));
  614. /* Mask off the CAM interrupts */
  615. gfar_write(&priv->regs->rmon.cam1, 0xffffffff);
  616. gfar_write(&priv->regs->rmon.cam2, 0xffffffff);
  617. }
  618. /* Initialize the max receive buffer length */
  619. gfar_write(&priv->regs->mrblr, priv->rx_buffer_size);
  620. /* Initialize the Minimum Frame Length Register */
  621. gfar_write(&priv->regs->minflr, MINFLR_INIT_SETTINGS);
  622. }
  623. /* Halt the receive and transmit queues */
  624. static void gfar_halt_nodisable(struct net_device *dev)
  625. {
  626. struct gfar_private *priv = netdev_priv(dev);
  627. struct gfar __iomem *regs = priv->regs;
  628. u32 tempval;
  629. /* Mask all interrupts */
  630. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  631. /* Clear all interrupts */
  632. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  633. /* Stop the DMA, and wait for it to stop */
  634. tempval = gfar_read(&priv->regs->dmactrl);
  635. if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
  636. != (DMACTRL_GRS | DMACTRL_GTS)) {
  637. tempval |= (DMACTRL_GRS | DMACTRL_GTS);
  638. gfar_write(&priv->regs->dmactrl, tempval);
  639. while (!(gfar_read(&priv->regs->ievent) &
  640. (IEVENT_GRSC | IEVENT_GTSC)))
  641. cpu_relax();
  642. }
  643. }
  644. /* Halt the receive and transmit queues */
  645. void gfar_halt(struct net_device *dev)
  646. {
  647. struct gfar_private *priv = netdev_priv(dev);
  648. struct gfar __iomem *regs = priv->regs;
  649. u32 tempval;
  650. gfar_halt_nodisable(dev);
  651. /* Disable Rx and Tx */
  652. tempval = gfar_read(&regs->maccfg1);
  653. tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
  654. gfar_write(&regs->maccfg1, tempval);
  655. }
  656. void stop_gfar(struct net_device *dev)
  657. {
  658. struct gfar_private *priv = netdev_priv(dev);
  659. struct gfar __iomem *regs = priv->regs;
  660. unsigned long flags;
  661. phy_stop(priv->phydev);
  662. /* Lock it down */
  663. spin_lock_irqsave(&priv->txlock, flags);
  664. spin_lock(&priv->rxlock);
  665. gfar_halt(dev);
  666. spin_unlock(&priv->rxlock);
  667. spin_unlock_irqrestore(&priv->txlock, flags);
  668. /* Free the IRQs */
  669. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  670. free_irq(priv->interruptError, dev);
  671. free_irq(priv->interruptTransmit, dev);
  672. free_irq(priv->interruptReceive, dev);
  673. } else {
  674. free_irq(priv->interruptTransmit, dev);
  675. }
  676. free_skb_resources(priv);
  677. dma_free_coherent(&dev->dev,
  678. sizeof(struct txbd8)*priv->tx_ring_size
  679. + sizeof(struct rxbd8)*priv->rx_ring_size,
  680. priv->tx_bd_base,
  681. gfar_read(&regs->tbase0));
  682. }
  683. /* If there are any tx skbs or rx skbs still around, free them.
  684. * Then free tx_skbuff and rx_skbuff */
  685. static void free_skb_resources(struct gfar_private *priv)
  686. {
  687. struct rxbd8 *rxbdp;
  688. struct txbd8 *txbdp;
  689. int i, j;
  690. /* Go through all the buffer descriptors and free their data buffers */
  691. txbdp = priv->tx_bd_base;
  692. for (i = 0; i < priv->tx_ring_size; i++) {
  693. if (!priv->tx_skbuff[i])
  694. continue;
  695. dma_unmap_single(&priv->dev->dev, txbdp->bufPtr,
  696. txbdp->length, DMA_TO_DEVICE);
  697. txbdp->lstatus = 0;
  698. for (j = 0; j < skb_shinfo(priv->tx_skbuff[i])->nr_frags; j++) {
  699. txbdp++;
  700. dma_unmap_page(&priv->dev->dev, txbdp->bufPtr,
  701. txbdp->length, DMA_TO_DEVICE);
  702. }
  703. txbdp++;
  704. dev_kfree_skb_any(priv->tx_skbuff[i]);
  705. priv->tx_skbuff[i] = NULL;
  706. }
  707. kfree(priv->tx_skbuff);
  708. rxbdp = priv->rx_bd_base;
  709. /* rx_skbuff is not guaranteed to be allocated, so only
  710. * free it and its contents if it is allocated */
  711. if(priv->rx_skbuff != NULL) {
  712. for (i = 0; i < priv->rx_ring_size; i++) {
  713. if (priv->rx_skbuff[i]) {
  714. dma_unmap_single(&priv->dev->dev, rxbdp->bufPtr,
  715. priv->rx_buffer_size,
  716. DMA_FROM_DEVICE);
  717. dev_kfree_skb_any(priv->rx_skbuff[i]);
  718. priv->rx_skbuff[i] = NULL;
  719. }
  720. rxbdp->lstatus = 0;
  721. rxbdp->bufPtr = 0;
  722. rxbdp++;
  723. }
  724. kfree(priv->rx_skbuff);
  725. }
  726. }
  727. void gfar_start(struct net_device *dev)
  728. {
  729. struct gfar_private *priv = netdev_priv(dev);
  730. struct gfar __iomem *regs = priv->regs;
  731. u32 tempval;
  732. /* Enable Rx and Tx in MACCFG1 */
  733. tempval = gfar_read(&regs->maccfg1);
  734. tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  735. gfar_write(&regs->maccfg1, tempval);
  736. /* Initialize DMACTRL to have WWR and WOP */
  737. tempval = gfar_read(&priv->regs->dmactrl);
  738. tempval |= DMACTRL_INIT_SETTINGS;
  739. gfar_write(&priv->regs->dmactrl, tempval);
  740. /* Make sure we aren't stopped */
  741. tempval = gfar_read(&priv->regs->dmactrl);
  742. tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
  743. gfar_write(&priv->regs->dmactrl, tempval);
  744. /* Clear THLT/RHLT, so that the DMA starts polling now */
  745. gfar_write(&regs->tstat, TSTAT_CLEAR_THALT);
  746. gfar_write(&regs->rstat, RSTAT_CLEAR_RHALT);
  747. /* Unmask the interrupts we look for */
  748. gfar_write(&regs->imask, IMASK_DEFAULT);
  749. dev->trans_start = jiffies;
  750. }
  751. /* Bring the controller up and running */
  752. int startup_gfar(struct net_device *dev)
  753. {
  754. struct txbd8 *txbdp;
  755. struct rxbd8 *rxbdp;
  756. dma_addr_t addr = 0;
  757. unsigned long vaddr;
  758. int i;
  759. struct gfar_private *priv = netdev_priv(dev);
  760. struct gfar __iomem *regs = priv->regs;
  761. int err = 0;
  762. u32 rctrl = 0;
  763. u32 attrs = 0;
  764. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  765. /* Allocate memory for the buffer descriptors */
  766. vaddr = (unsigned long) dma_alloc_coherent(&dev->dev,
  767. sizeof (struct txbd8) * priv->tx_ring_size +
  768. sizeof (struct rxbd8) * priv->rx_ring_size,
  769. &addr, GFP_KERNEL);
  770. if (vaddr == 0) {
  771. if (netif_msg_ifup(priv))
  772. printk(KERN_ERR "%s: Could not allocate buffer descriptors!\n",
  773. dev->name);
  774. return -ENOMEM;
  775. }
  776. priv->tx_bd_base = (struct txbd8 *) vaddr;
  777. /* enet DMA only understands physical addresses */
  778. gfar_write(&regs->tbase0, addr);
  779. /* Start the rx descriptor ring where the tx ring leaves off */
  780. addr = addr + sizeof (struct txbd8) * priv->tx_ring_size;
  781. vaddr = vaddr + sizeof (struct txbd8) * priv->tx_ring_size;
  782. priv->rx_bd_base = (struct rxbd8 *) vaddr;
  783. gfar_write(&regs->rbase0, addr);
  784. /* Setup the skbuff rings */
  785. priv->tx_skbuff =
  786. (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
  787. priv->tx_ring_size, GFP_KERNEL);
  788. if (NULL == priv->tx_skbuff) {
  789. if (netif_msg_ifup(priv))
  790. printk(KERN_ERR "%s: Could not allocate tx_skbuff\n",
  791. dev->name);
  792. err = -ENOMEM;
  793. goto tx_skb_fail;
  794. }
  795. for (i = 0; i < priv->tx_ring_size; i++)
  796. priv->tx_skbuff[i] = NULL;
  797. priv->rx_skbuff =
  798. (struct sk_buff **) kmalloc(sizeof (struct sk_buff *) *
  799. priv->rx_ring_size, GFP_KERNEL);
  800. if (NULL == priv->rx_skbuff) {
  801. if (netif_msg_ifup(priv))
  802. printk(KERN_ERR "%s: Could not allocate rx_skbuff\n",
  803. dev->name);
  804. err = -ENOMEM;
  805. goto rx_skb_fail;
  806. }
  807. for (i = 0; i < priv->rx_ring_size; i++)
  808. priv->rx_skbuff[i] = NULL;
  809. /* Initialize some variables in our dev structure */
  810. priv->num_txbdfree = priv->tx_ring_size;
  811. priv->dirty_tx = priv->cur_tx = priv->tx_bd_base;
  812. priv->cur_rx = priv->rx_bd_base;
  813. priv->skb_curtx = priv->skb_dirtytx = 0;
  814. priv->skb_currx = 0;
  815. /* Initialize Transmit Descriptor Ring */
  816. txbdp = priv->tx_bd_base;
  817. for (i = 0; i < priv->tx_ring_size; i++) {
  818. txbdp->lstatus = 0;
  819. txbdp->bufPtr = 0;
  820. txbdp++;
  821. }
  822. /* Set the last descriptor in the ring to indicate wrap */
  823. txbdp--;
  824. txbdp->status |= TXBD_WRAP;
  825. rxbdp = priv->rx_bd_base;
  826. for (i = 0; i < priv->rx_ring_size; i++) {
  827. struct sk_buff *skb;
  828. skb = gfar_new_skb(dev);
  829. if (!skb) {
  830. printk(KERN_ERR "%s: Can't allocate RX buffers\n",
  831. dev->name);
  832. goto err_rxalloc_fail;
  833. }
  834. priv->rx_skbuff[i] = skb;
  835. gfar_new_rxbdp(dev, rxbdp, skb);
  836. rxbdp++;
  837. }
  838. /* Set the last descriptor in the ring to wrap */
  839. rxbdp--;
  840. rxbdp->status |= RXBD_WRAP;
  841. /* If the device has multiple interrupts, register for
  842. * them. Otherwise, only register for the one */
  843. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  844. /* Install our interrupt handlers for Error,
  845. * Transmit, and Receive */
  846. if (request_irq(priv->interruptError, gfar_error,
  847. 0, priv->int_name_er, dev) < 0) {
  848. if (netif_msg_intr(priv))
  849. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  850. dev->name, priv->interruptError);
  851. err = -1;
  852. goto err_irq_fail;
  853. }
  854. if (request_irq(priv->interruptTransmit, gfar_transmit,
  855. 0, priv->int_name_tx, dev) < 0) {
  856. if (netif_msg_intr(priv))
  857. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  858. dev->name, priv->interruptTransmit);
  859. err = -1;
  860. goto tx_irq_fail;
  861. }
  862. if (request_irq(priv->interruptReceive, gfar_receive,
  863. 0, priv->int_name_rx, dev) < 0) {
  864. if (netif_msg_intr(priv))
  865. printk(KERN_ERR "%s: Can't get IRQ %d (receive0)\n",
  866. dev->name, priv->interruptReceive);
  867. err = -1;
  868. goto rx_irq_fail;
  869. }
  870. } else {
  871. if (request_irq(priv->interruptTransmit, gfar_interrupt,
  872. 0, priv->int_name_tx, dev) < 0) {
  873. if (netif_msg_intr(priv))
  874. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  875. dev->name, priv->interruptTransmit);
  876. err = -1;
  877. goto err_irq_fail;
  878. }
  879. }
  880. phy_start(priv->phydev);
  881. /* Configure the coalescing support */
  882. gfar_write(&regs->txic, 0);
  883. if (priv->txcoalescing)
  884. gfar_write(&regs->txic, priv->txic);
  885. gfar_write(&regs->rxic, 0);
  886. if (priv->rxcoalescing)
  887. gfar_write(&regs->rxic, priv->rxic);
  888. if (priv->rx_csum_enable)
  889. rctrl |= RCTRL_CHECKSUMMING;
  890. if (priv->extended_hash) {
  891. rctrl |= RCTRL_EXTHASH;
  892. gfar_clear_exact_match(dev);
  893. rctrl |= RCTRL_EMEN;
  894. }
  895. if (priv->padding) {
  896. rctrl &= ~RCTRL_PAL_MASK;
  897. rctrl |= RCTRL_PADDING(priv->padding);
  898. }
  899. /* Init rctrl based on our settings */
  900. gfar_write(&priv->regs->rctrl, rctrl);
  901. if (dev->features & NETIF_F_IP_CSUM)
  902. gfar_write(&priv->regs->tctrl, TCTRL_INIT_CSUM);
  903. /* Set the extraction length and index */
  904. attrs = ATTRELI_EL(priv->rx_stash_size) |
  905. ATTRELI_EI(priv->rx_stash_index);
  906. gfar_write(&priv->regs->attreli, attrs);
  907. /* Start with defaults, and add stashing or locking
  908. * depending on the approprate variables */
  909. attrs = ATTR_INIT_SETTINGS;
  910. if (priv->bd_stash_en)
  911. attrs |= ATTR_BDSTASH;
  912. if (priv->rx_stash_size != 0)
  913. attrs |= ATTR_BUFSTASH;
  914. gfar_write(&priv->regs->attr, attrs);
  915. gfar_write(&priv->regs->fifo_tx_thr, priv->fifo_threshold);
  916. gfar_write(&priv->regs->fifo_tx_starve, priv->fifo_starve);
  917. gfar_write(&priv->regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
  918. /* Start the controller */
  919. gfar_start(dev);
  920. return 0;
  921. rx_irq_fail:
  922. free_irq(priv->interruptTransmit, dev);
  923. tx_irq_fail:
  924. free_irq(priv->interruptError, dev);
  925. err_irq_fail:
  926. err_rxalloc_fail:
  927. rx_skb_fail:
  928. free_skb_resources(priv);
  929. tx_skb_fail:
  930. dma_free_coherent(&dev->dev,
  931. sizeof(struct txbd8)*priv->tx_ring_size
  932. + sizeof(struct rxbd8)*priv->rx_ring_size,
  933. priv->tx_bd_base,
  934. gfar_read(&regs->tbase0));
  935. return err;
  936. }
  937. /* Called when something needs to use the ethernet device */
  938. /* Returns 0 for success. */
  939. static int gfar_enet_open(struct net_device *dev)
  940. {
  941. struct gfar_private *priv = netdev_priv(dev);
  942. int err;
  943. napi_enable(&priv->napi);
  944. /* Initialize a bunch of registers */
  945. init_registers(dev);
  946. gfar_set_mac_address(dev);
  947. err = init_phy(dev);
  948. if(err) {
  949. napi_disable(&priv->napi);
  950. return err;
  951. }
  952. err = startup_gfar(dev);
  953. if (err) {
  954. napi_disable(&priv->napi);
  955. return err;
  956. }
  957. netif_start_queue(dev);
  958. return err;
  959. }
  960. static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
  961. {
  962. struct txfcb *fcb = (struct txfcb *)skb_push (skb, GMAC_FCB_LEN);
  963. cacheable_memzero(fcb, GMAC_FCB_LEN);
  964. return fcb;
  965. }
  966. static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
  967. {
  968. u8 flags = 0;
  969. /* If we're here, it's a IP packet with a TCP or UDP
  970. * payload. We set it to checksum, using a pseudo-header
  971. * we provide
  972. */
  973. flags = TXFCB_DEFAULT;
  974. /* Tell the controller what the protocol is */
  975. /* And provide the already calculated phcs */
  976. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  977. flags |= TXFCB_UDP;
  978. fcb->phcs = udp_hdr(skb)->check;
  979. } else
  980. fcb->phcs = tcp_hdr(skb)->check;
  981. /* l3os is the distance between the start of the
  982. * frame (skb->data) and the start of the IP hdr.
  983. * l4os is the distance between the start of the
  984. * l3 hdr and the l4 hdr */
  985. fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
  986. fcb->l4os = skb_network_header_len(skb);
  987. fcb->flags = flags;
  988. }
  989. void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
  990. {
  991. fcb->flags |= TXFCB_VLN;
  992. fcb->vlctl = vlan_tx_tag_get(skb);
  993. }
  994. static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
  995. struct txbd8 *base, int ring_size)
  996. {
  997. struct txbd8 *new_bd = bdp + stride;
  998. return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
  999. }
  1000. static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
  1001. int ring_size)
  1002. {
  1003. return skip_txbd(bdp, 1, base, ring_size);
  1004. }
  1005. /* This is called by the kernel when a frame is ready for transmission. */
  1006. /* It is pointed to by the dev->hard_start_xmit function pointer */
  1007. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1008. {
  1009. struct gfar_private *priv = netdev_priv(dev);
  1010. struct txfcb *fcb = NULL;
  1011. struct txbd8 *txbdp, *txbdp_start, *base;
  1012. u32 lstatus;
  1013. int i;
  1014. u32 bufaddr;
  1015. unsigned long flags;
  1016. unsigned int nr_frags, length;
  1017. base = priv->tx_bd_base;
  1018. /* total number of fragments in the SKB */
  1019. nr_frags = skb_shinfo(skb)->nr_frags;
  1020. spin_lock_irqsave(&priv->txlock, flags);
  1021. /* check if there is space to queue this packet */
  1022. if (nr_frags > priv->num_txbdfree) {
  1023. /* no space, stop the queue */
  1024. netif_stop_queue(dev);
  1025. dev->stats.tx_fifo_errors++;
  1026. spin_unlock_irqrestore(&priv->txlock, flags);
  1027. return NETDEV_TX_BUSY;
  1028. }
  1029. /* Update transmit stats */
  1030. dev->stats.tx_bytes += skb->len;
  1031. txbdp = txbdp_start = priv->cur_tx;
  1032. if (nr_frags == 0) {
  1033. lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1034. } else {
  1035. /* Place the fragment addresses and lengths into the TxBDs */
  1036. for (i = 0; i < nr_frags; i++) {
  1037. /* Point at the next BD, wrapping as needed */
  1038. txbdp = next_txbd(txbdp, base, priv->tx_ring_size);
  1039. length = skb_shinfo(skb)->frags[i].size;
  1040. lstatus = txbdp->lstatus | length |
  1041. BD_LFLAG(TXBD_READY);
  1042. /* Handle the last BD specially */
  1043. if (i == nr_frags - 1)
  1044. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1045. bufaddr = dma_map_page(&dev->dev,
  1046. skb_shinfo(skb)->frags[i].page,
  1047. skb_shinfo(skb)->frags[i].page_offset,
  1048. length,
  1049. DMA_TO_DEVICE);
  1050. /* set the TxBD length and buffer pointer */
  1051. txbdp->bufPtr = bufaddr;
  1052. txbdp->lstatus = lstatus;
  1053. }
  1054. lstatus = txbdp_start->lstatus;
  1055. }
  1056. /* Set up checksumming */
  1057. if (CHECKSUM_PARTIAL == skb->ip_summed) {
  1058. fcb = gfar_add_fcb(skb);
  1059. lstatus |= BD_LFLAG(TXBD_TOE);
  1060. gfar_tx_checksum(skb, fcb);
  1061. }
  1062. if (priv->vlgrp && vlan_tx_tag_present(skb)) {
  1063. if (unlikely(NULL == fcb)) {
  1064. fcb = gfar_add_fcb(skb);
  1065. lstatus |= BD_LFLAG(TXBD_TOE);
  1066. }
  1067. gfar_tx_vlan(skb, fcb);
  1068. }
  1069. /* setup the TxBD length and buffer pointer for the first BD */
  1070. priv->tx_skbuff[priv->skb_curtx] = skb;
  1071. txbdp_start->bufPtr = dma_map_single(&dev->dev, skb->data,
  1072. skb_headlen(skb), DMA_TO_DEVICE);
  1073. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
  1074. /*
  1075. * The powerpc-specific eieio() is used, as wmb() has too strong
  1076. * semantics (it requires synchronization between cacheable and
  1077. * uncacheable mappings, which eieio doesn't provide and which we
  1078. * don't need), thus requiring a more expensive sync instruction. At
  1079. * some point, the set of architecture-independent barrier functions
  1080. * should be expanded to include weaker barriers.
  1081. */
  1082. eieio();
  1083. txbdp_start->lstatus = lstatus;
  1084. /* Update the current skb pointer to the next entry we will use
  1085. * (wrapping if necessary) */
  1086. priv->skb_curtx = (priv->skb_curtx + 1) &
  1087. TX_RING_MOD_MASK(priv->tx_ring_size);
  1088. priv->cur_tx = next_txbd(txbdp, base, priv->tx_ring_size);
  1089. /* reduce TxBD free count */
  1090. priv->num_txbdfree -= (nr_frags + 1);
  1091. dev->trans_start = jiffies;
  1092. /* If the next BD still needs to be cleaned up, then the bds
  1093. are full. We need to tell the kernel to stop sending us stuff. */
  1094. if (!priv->num_txbdfree) {
  1095. netif_stop_queue(dev);
  1096. dev->stats.tx_fifo_errors++;
  1097. }
  1098. /* Tell the DMA to go go go */
  1099. gfar_write(&priv->regs->tstat, TSTAT_CLEAR_THALT);
  1100. /* Unlock priv */
  1101. spin_unlock_irqrestore(&priv->txlock, flags);
  1102. return 0;
  1103. }
  1104. /* Stops the kernel queue, and halts the controller */
  1105. static int gfar_close(struct net_device *dev)
  1106. {
  1107. struct gfar_private *priv = netdev_priv(dev);
  1108. napi_disable(&priv->napi);
  1109. cancel_work_sync(&priv->reset_task);
  1110. stop_gfar(dev);
  1111. /* Disconnect from the PHY */
  1112. phy_disconnect(priv->phydev);
  1113. priv->phydev = NULL;
  1114. netif_stop_queue(dev);
  1115. return 0;
  1116. }
  1117. /* Changes the mac address if the controller is not running. */
  1118. static int gfar_set_mac_address(struct net_device *dev)
  1119. {
  1120. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  1121. return 0;
  1122. }
  1123. /* Enables and disables VLAN insertion/extraction */
  1124. static void gfar_vlan_rx_register(struct net_device *dev,
  1125. struct vlan_group *grp)
  1126. {
  1127. struct gfar_private *priv = netdev_priv(dev);
  1128. unsigned long flags;
  1129. struct vlan_group *old_grp;
  1130. u32 tempval;
  1131. spin_lock_irqsave(&priv->rxlock, flags);
  1132. old_grp = priv->vlgrp;
  1133. if (old_grp == grp)
  1134. return;
  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 (netif_rx_schedule_prep(&priv->napi)) {
  1287. gfar_write(&priv->regs->imask, IMASK_RTX_DISABLED);
  1288. __netif_rx_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. netif_rx_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);