gianfar.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050
  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. * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
  12. *
  13. * Copyright 2002-2009 Freescale Semiconductor, Inc.
  14. * Copyright 2007 MontaVista Software, Inc.
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the
  18. * Free Software Foundation; either version 2 of the License, or (at your
  19. * option) any later version.
  20. *
  21. * Gianfar: AKA Lambda Draconis, "Dragon"
  22. * RA 11 31 24.2
  23. * Dec +69 19 52
  24. * V 3.84
  25. * B-V +1.62
  26. *
  27. * Theory of operation
  28. *
  29. * The driver is initialized through of_device. Configuration information
  30. * is therefore conveyed through an OF-style device tree.
  31. *
  32. * The Gianfar Ethernet Controller uses a ring of buffer
  33. * descriptors. The beginning is indicated by a register
  34. * pointing to the physical address of the start of the ring.
  35. * The end is determined by a "wrap" bit being set in the
  36. * last descriptor of the ring.
  37. *
  38. * When a packet is received, the RXF bit in the
  39. * IEVENT register is set, triggering an interrupt when the
  40. * corresponding bit in the IMASK register is also set (if
  41. * interrupt coalescing is active, then the interrupt may not
  42. * happen immediately, but will wait until either a set number
  43. * of frames or amount of time have passed). In NAPI, the
  44. * interrupt handler will signal there is work to be done, and
  45. * exit. This method will start at the last known empty
  46. * descriptor, and process every subsequent descriptor until there
  47. * are none left with data (NAPI will stop after a set number of
  48. * packets to give time to other tasks, but will eventually
  49. * process all the packets). The data arrives inside a
  50. * pre-allocated skb, and so after the skb is passed up to the
  51. * stack, a new skb must be allocated, and the address field in
  52. * the buffer descriptor must be updated to indicate this new
  53. * skb.
  54. *
  55. * When the kernel requests that a packet be transmitted, the
  56. * driver starts where it left off last time, and points the
  57. * descriptor at the buffer which was passed in. The driver
  58. * then informs the DMA engine that there are packets ready to
  59. * be transmitted. Once the controller is finished transmitting
  60. * the packet, an interrupt may be triggered (under the same
  61. * conditions as for reception, but depending on the TXF bit).
  62. * The driver then cleans up the buffer.
  63. */
  64. #include <linux/kernel.h>
  65. #include <linux/string.h>
  66. #include <linux/errno.h>
  67. #include <linux/unistd.h>
  68. #include <linux/slab.h>
  69. #include <linux/interrupt.h>
  70. #include <linux/init.h>
  71. #include <linux/delay.h>
  72. #include <linux/netdevice.h>
  73. #include <linux/etherdevice.h>
  74. #include <linux/skbuff.h>
  75. #include <linux/if_vlan.h>
  76. #include <linux/spinlock.h>
  77. #include <linux/mm.h>
  78. #include <linux/of_mdio.h>
  79. #include <linux/of_platform.h>
  80. #include <linux/ip.h>
  81. #include <linux/tcp.h>
  82. #include <linux/udp.h>
  83. #include <linux/in.h>
  84. #include <asm/io.h>
  85. #include <asm/irq.h>
  86. #include <asm/uaccess.h>
  87. #include <linux/module.h>
  88. #include <linux/dma-mapping.h>
  89. #include <linux/crc32.h>
  90. #include <linux/mii.h>
  91. #include <linux/phy.h>
  92. #include <linux/phy_fixed.h>
  93. #include <linux/of.h>
  94. #include "gianfar.h"
  95. #include "fsl_pq_mdio.h"
  96. #define TX_TIMEOUT (1*HZ)
  97. #undef BRIEF_GFAR_ERRORS
  98. #undef VERBOSE_GFAR_ERRORS
  99. const char gfar_driver_name[] = "Gianfar Ethernet";
  100. const char gfar_driver_version[] = "1.3";
  101. static int gfar_enet_open(struct net_device *dev);
  102. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
  103. static void gfar_reset_task(struct work_struct *work);
  104. static void gfar_timeout(struct net_device *dev);
  105. static int gfar_close(struct net_device *dev);
  106. struct sk_buff *gfar_new_skb(struct net_device *dev);
  107. static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
  108. struct sk_buff *skb);
  109. static int gfar_set_mac_address(struct net_device *dev);
  110. static int gfar_change_mtu(struct net_device *dev, int new_mtu);
  111. static irqreturn_t gfar_error(int irq, void *dev_id);
  112. static irqreturn_t gfar_transmit(int irq, void *dev_id);
  113. static irqreturn_t gfar_interrupt(int irq, void *dev_id);
  114. static void adjust_link(struct net_device *dev);
  115. static void init_registers(struct net_device *dev);
  116. static int init_phy(struct net_device *dev);
  117. static int gfar_probe(struct of_device *ofdev,
  118. const struct of_device_id *match);
  119. static int gfar_remove(struct of_device *ofdev);
  120. static void free_skb_resources(struct gfar_private *priv);
  121. static void gfar_set_multi(struct net_device *dev);
  122. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
  123. static void gfar_configure_serdes(struct net_device *dev);
  124. static int gfar_poll(struct napi_struct *napi, int budget);
  125. #ifdef CONFIG_NET_POLL_CONTROLLER
  126. static void gfar_netpoll(struct net_device *dev);
  127. #endif
  128. int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
  129. static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
  130. static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
  131. int amount_pull);
  132. static void gfar_vlan_rx_register(struct net_device *netdev,
  133. struct vlan_group *grp);
  134. void gfar_halt(struct net_device *dev);
  135. static void gfar_halt_nodisable(struct net_device *dev);
  136. void gfar_start(struct net_device *dev);
  137. static void gfar_clear_exact_match(struct net_device *dev);
  138. static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr);
  139. static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  140. u16 gfar_select_queue(struct net_device *dev, struct sk_buff *skb);
  141. MODULE_AUTHOR("Freescale Semiconductor, Inc");
  142. MODULE_DESCRIPTION("Gianfar Ethernet Driver");
  143. MODULE_LICENSE("GPL");
  144. static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
  145. dma_addr_t buf)
  146. {
  147. u32 lstatus;
  148. bdp->bufPtr = buf;
  149. lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
  150. if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
  151. lstatus |= BD_LFLAG(RXBD_WRAP);
  152. eieio();
  153. bdp->lstatus = lstatus;
  154. }
  155. static int gfar_init_bds(struct net_device *ndev)
  156. {
  157. struct gfar_private *priv = netdev_priv(ndev);
  158. struct gfar_priv_tx_q *tx_queue = NULL;
  159. struct gfar_priv_rx_q *rx_queue = NULL;
  160. struct txbd8 *txbdp;
  161. struct rxbd8 *rxbdp;
  162. int i, j;
  163. for (i = 0; i < priv->num_tx_queues; i++) {
  164. tx_queue = priv->tx_queue[i];
  165. /* Initialize some variables in our dev structure */
  166. tx_queue->num_txbdfree = tx_queue->tx_ring_size;
  167. tx_queue->dirty_tx = tx_queue->tx_bd_base;
  168. tx_queue->cur_tx = tx_queue->tx_bd_base;
  169. tx_queue->skb_curtx = 0;
  170. tx_queue->skb_dirtytx = 0;
  171. /* Initialize Transmit Descriptor Ring */
  172. txbdp = tx_queue->tx_bd_base;
  173. for (j = 0; j < tx_queue->tx_ring_size; j++) {
  174. txbdp->lstatus = 0;
  175. txbdp->bufPtr = 0;
  176. txbdp++;
  177. }
  178. /* Set the last descriptor in the ring to indicate wrap */
  179. txbdp--;
  180. txbdp->status |= TXBD_WRAP;
  181. }
  182. for (i = 0; i < priv->num_rx_queues; i++) {
  183. rx_queue = priv->rx_queue[i];
  184. rx_queue->cur_rx = rx_queue->rx_bd_base;
  185. rx_queue->skb_currx = 0;
  186. rxbdp = rx_queue->rx_bd_base;
  187. for (j = 0; j < rx_queue->rx_ring_size; j++) {
  188. struct sk_buff *skb = rx_queue->rx_skbuff[j];
  189. if (skb) {
  190. gfar_init_rxbdp(rx_queue, rxbdp,
  191. rxbdp->bufPtr);
  192. } else {
  193. skb = gfar_new_skb(ndev);
  194. if (!skb) {
  195. pr_err("%s: Can't allocate RX buffers\n",
  196. ndev->name);
  197. goto err_rxalloc_fail;
  198. }
  199. rx_queue->rx_skbuff[j] = skb;
  200. gfar_new_rxbdp(rx_queue, rxbdp, skb);
  201. }
  202. rxbdp++;
  203. }
  204. }
  205. return 0;
  206. err_rxalloc_fail:
  207. free_skb_resources(priv);
  208. return -ENOMEM;
  209. }
  210. static int gfar_alloc_skb_resources(struct net_device *ndev)
  211. {
  212. void *vaddr;
  213. dma_addr_t addr;
  214. int i, j, k;
  215. struct gfar_private *priv = netdev_priv(ndev);
  216. struct device *dev = &priv->ofdev->dev;
  217. struct gfar_priv_tx_q *tx_queue = NULL;
  218. struct gfar_priv_rx_q *rx_queue = NULL;
  219. priv->total_tx_ring_size = 0;
  220. for (i = 0; i < priv->num_tx_queues; i++)
  221. priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
  222. priv->total_rx_ring_size = 0;
  223. for (i = 0; i < priv->num_rx_queues; i++)
  224. priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
  225. /* Allocate memory for the buffer descriptors */
  226. vaddr = dma_alloc_coherent(dev,
  227. sizeof(struct txbd8) * priv->total_tx_ring_size +
  228. sizeof(struct rxbd8) * priv->total_rx_ring_size,
  229. &addr, GFP_KERNEL);
  230. if (!vaddr) {
  231. if (netif_msg_ifup(priv))
  232. pr_err("%s: Could not allocate buffer descriptors!\n",
  233. ndev->name);
  234. return -ENOMEM;
  235. }
  236. for (i = 0; i < priv->num_tx_queues; i++) {
  237. tx_queue = priv->tx_queue[i];
  238. tx_queue->tx_bd_base = (struct txbd8 *) vaddr;
  239. tx_queue->tx_bd_dma_base = addr;
  240. tx_queue->dev = ndev;
  241. /* enet DMA only understands physical addresses */
  242. addr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
  243. vaddr += sizeof(struct txbd8) *tx_queue->tx_ring_size;
  244. }
  245. /* Start the rx descriptor ring where the tx ring leaves off */
  246. for (i = 0; i < priv->num_rx_queues; i++) {
  247. rx_queue = priv->rx_queue[i];
  248. rx_queue->rx_bd_base = (struct rxbd8 *) vaddr;
  249. rx_queue->rx_bd_dma_base = addr;
  250. rx_queue->dev = ndev;
  251. addr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
  252. vaddr += sizeof (struct rxbd8) * rx_queue->rx_ring_size;
  253. }
  254. /* Setup the skbuff rings */
  255. for (i = 0; i < priv->num_tx_queues; i++) {
  256. tx_queue = priv->tx_queue[i];
  257. tx_queue->tx_skbuff = kmalloc(sizeof(*tx_queue->tx_skbuff) *
  258. tx_queue->tx_ring_size, GFP_KERNEL);
  259. if (!tx_queue->tx_skbuff) {
  260. if (netif_msg_ifup(priv))
  261. pr_err("%s: Could not allocate tx_skbuff\n",
  262. ndev->name);
  263. goto cleanup;
  264. }
  265. for (k = 0; k < tx_queue->tx_ring_size; k++)
  266. tx_queue->tx_skbuff[k] = NULL;
  267. }
  268. for (i = 0; i < priv->num_rx_queues; i++) {
  269. rx_queue = priv->rx_queue[i];
  270. rx_queue->rx_skbuff = kmalloc(sizeof(*rx_queue->rx_skbuff) *
  271. rx_queue->rx_ring_size, GFP_KERNEL);
  272. if (!rx_queue->rx_skbuff) {
  273. if (netif_msg_ifup(priv))
  274. pr_err("%s: Could not allocate rx_skbuff\n",
  275. ndev->name);
  276. goto cleanup;
  277. }
  278. for (j = 0; j < rx_queue->rx_ring_size; j++)
  279. rx_queue->rx_skbuff[j] = NULL;
  280. }
  281. if (gfar_init_bds(ndev))
  282. goto cleanup;
  283. return 0;
  284. cleanup:
  285. free_skb_resources(priv);
  286. return -ENOMEM;
  287. }
  288. static void gfar_init_tx_rx_base(struct gfar_private *priv)
  289. {
  290. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  291. u32 __iomem *baddr;
  292. int i;
  293. baddr = &regs->tbase0;
  294. for(i = 0; i < priv->num_tx_queues; i++) {
  295. gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
  296. baddr += 2;
  297. }
  298. baddr = &regs->rbase0;
  299. for(i = 0; i < priv->num_rx_queues; i++) {
  300. gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
  301. baddr += 2;
  302. }
  303. }
  304. static void gfar_init_mac(struct net_device *ndev)
  305. {
  306. struct gfar_private *priv = netdev_priv(ndev);
  307. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  308. u32 rctrl = 0;
  309. u32 tctrl = 0;
  310. u32 attrs = 0;
  311. /* write the tx/rx base registers */
  312. gfar_init_tx_rx_base(priv);
  313. /* Configure the coalescing support */
  314. gfar_configure_coalescing(priv, 0xFF, 0xFF);
  315. if (priv->rx_filer_enable) {
  316. rctrl |= RCTRL_FILREN;
  317. /* Program the RIR0 reg with the required distribution */
  318. gfar_write(&regs->rir0, DEFAULT_RIR0);
  319. }
  320. if (priv->rx_csum_enable)
  321. rctrl |= RCTRL_CHECKSUMMING;
  322. if (priv->extended_hash) {
  323. rctrl |= RCTRL_EXTHASH;
  324. gfar_clear_exact_match(ndev);
  325. rctrl |= RCTRL_EMEN;
  326. }
  327. if (priv->padding) {
  328. rctrl &= ~RCTRL_PAL_MASK;
  329. rctrl |= RCTRL_PADDING(priv->padding);
  330. }
  331. /* keep vlan related bits if it's enabled */
  332. if (priv->vlgrp) {
  333. rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
  334. tctrl |= TCTRL_VLINS;
  335. }
  336. /* Init rctrl based on our settings */
  337. gfar_write(&regs->rctrl, rctrl);
  338. if (ndev->features & NETIF_F_IP_CSUM)
  339. tctrl |= TCTRL_INIT_CSUM;
  340. tctrl |= TCTRL_TXSCHED_PRIO;
  341. gfar_write(&regs->tctrl, tctrl);
  342. /* Set the extraction length and index */
  343. attrs = ATTRELI_EL(priv->rx_stash_size) |
  344. ATTRELI_EI(priv->rx_stash_index);
  345. gfar_write(&regs->attreli, attrs);
  346. /* Start with defaults, and add stashing or locking
  347. * depending on the approprate variables */
  348. attrs = ATTR_INIT_SETTINGS;
  349. if (priv->bd_stash_en)
  350. attrs |= ATTR_BDSTASH;
  351. if (priv->rx_stash_size != 0)
  352. attrs |= ATTR_BUFSTASH;
  353. gfar_write(&regs->attr, attrs);
  354. gfar_write(&regs->fifo_tx_thr, priv->fifo_threshold);
  355. gfar_write(&regs->fifo_tx_starve, priv->fifo_starve);
  356. gfar_write(&regs->fifo_tx_starve_shutoff, priv->fifo_starve_off);
  357. }
  358. static const struct net_device_ops gfar_netdev_ops = {
  359. .ndo_open = gfar_enet_open,
  360. .ndo_start_xmit = gfar_start_xmit,
  361. .ndo_stop = gfar_close,
  362. .ndo_change_mtu = gfar_change_mtu,
  363. .ndo_set_multicast_list = gfar_set_multi,
  364. .ndo_tx_timeout = gfar_timeout,
  365. .ndo_do_ioctl = gfar_ioctl,
  366. .ndo_select_queue = gfar_select_queue,
  367. .ndo_vlan_rx_register = gfar_vlan_rx_register,
  368. .ndo_set_mac_address = eth_mac_addr,
  369. .ndo_validate_addr = eth_validate_addr,
  370. #ifdef CONFIG_NET_POLL_CONTROLLER
  371. .ndo_poll_controller = gfar_netpoll,
  372. #endif
  373. };
  374. unsigned int ftp_rqfpr[MAX_FILER_IDX + 1];
  375. unsigned int ftp_rqfcr[MAX_FILER_IDX + 1];
  376. void lock_rx_qs(struct gfar_private *priv)
  377. {
  378. int i = 0x0;
  379. for (i = 0; i < priv->num_rx_queues; i++)
  380. spin_lock(&priv->rx_queue[i]->rxlock);
  381. }
  382. void lock_tx_qs(struct gfar_private *priv)
  383. {
  384. int i = 0x0;
  385. for (i = 0; i < priv->num_tx_queues; i++)
  386. spin_lock(&priv->tx_queue[i]->txlock);
  387. }
  388. void unlock_rx_qs(struct gfar_private *priv)
  389. {
  390. int i = 0x0;
  391. for (i = 0; i < priv->num_rx_queues; i++)
  392. spin_unlock(&priv->rx_queue[i]->rxlock);
  393. }
  394. void unlock_tx_qs(struct gfar_private *priv)
  395. {
  396. int i = 0x0;
  397. for (i = 0; i < priv->num_tx_queues; i++)
  398. spin_unlock(&priv->tx_queue[i]->txlock);
  399. }
  400. /* Returns 1 if incoming frames use an FCB */
  401. static inline int gfar_uses_fcb(struct gfar_private *priv)
  402. {
  403. return priv->vlgrp || priv->rx_csum_enable;
  404. }
  405. u16 gfar_select_queue(struct net_device *dev, struct sk_buff *skb)
  406. {
  407. return skb_get_queue_mapping(skb);
  408. }
  409. static void free_tx_pointers(struct gfar_private *priv)
  410. {
  411. int i = 0;
  412. for (i = 0; i < priv->num_tx_queues; i++)
  413. kfree(priv->tx_queue[i]);
  414. }
  415. static void free_rx_pointers(struct gfar_private *priv)
  416. {
  417. int i = 0;
  418. for (i = 0; i < priv->num_rx_queues; i++)
  419. kfree(priv->rx_queue[i]);
  420. }
  421. static void unmap_group_regs(struct gfar_private *priv)
  422. {
  423. int i = 0;
  424. for (i = 0; i < MAXGROUPS; i++)
  425. if (priv->gfargrp[i].regs)
  426. iounmap(priv->gfargrp[i].regs);
  427. }
  428. static void disable_napi(struct gfar_private *priv)
  429. {
  430. int i = 0;
  431. for (i = 0; i < priv->num_grps; i++)
  432. napi_disable(&priv->gfargrp[i].napi);
  433. }
  434. static void enable_napi(struct gfar_private *priv)
  435. {
  436. int i = 0;
  437. for (i = 0; i < priv->num_grps; i++)
  438. napi_enable(&priv->gfargrp[i].napi);
  439. }
  440. static int gfar_parse_group(struct device_node *np,
  441. struct gfar_private *priv, const char *model)
  442. {
  443. u32 *queue_mask;
  444. u64 addr, size;
  445. addr = of_translate_address(np,
  446. of_get_address(np, 0, &size, NULL));
  447. priv->gfargrp[priv->num_grps].regs = ioremap(addr, size);
  448. if (!priv->gfargrp[priv->num_grps].regs)
  449. return -ENOMEM;
  450. priv->gfargrp[priv->num_grps].interruptTransmit =
  451. irq_of_parse_and_map(np, 0);
  452. /* If we aren't the FEC we have multiple interrupts */
  453. if (model && strcasecmp(model, "FEC")) {
  454. priv->gfargrp[priv->num_grps].interruptReceive =
  455. irq_of_parse_and_map(np, 1);
  456. priv->gfargrp[priv->num_grps].interruptError =
  457. irq_of_parse_and_map(np,2);
  458. if (priv->gfargrp[priv->num_grps].interruptTransmit < 0 ||
  459. priv->gfargrp[priv->num_grps].interruptReceive < 0 ||
  460. priv->gfargrp[priv->num_grps].interruptError < 0) {
  461. return -EINVAL;
  462. }
  463. }
  464. priv->gfargrp[priv->num_grps].grp_id = priv->num_grps;
  465. priv->gfargrp[priv->num_grps].priv = priv;
  466. spin_lock_init(&priv->gfargrp[priv->num_grps].grplock);
  467. if(priv->mode == MQ_MG_MODE) {
  468. queue_mask = (u32 *)of_get_property(np,
  469. "fsl,rx-bit-map", NULL);
  470. priv->gfargrp[priv->num_grps].rx_bit_map =
  471. queue_mask ? *queue_mask :(DEFAULT_MAPPING >> priv->num_grps);
  472. queue_mask = (u32 *)of_get_property(np,
  473. "fsl,tx-bit-map", NULL);
  474. priv->gfargrp[priv->num_grps].tx_bit_map =
  475. queue_mask ? *queue_mask : (DEFAULT_MAPPING >> priv->num_grps);
  476. } else {
  477. priv->gfargrp[priv->num_grps].rx_bit_map = 0xFF;
  478. priv->gfargrp[priv->num_grps].tx_bit_map = 0xFF;
  479. }
  480. priv->num_grps++;
  481. return 0;
  482. }
  483. static int gfar_of_init(struct of_device *ofdev, struct net_device **pdev)
  484. {
  485. const char *model;
  486. const char *ctype;
  487. const void *mac_addr;
  488. int err = 0, i;
  489. struct net_device *dev = NULL;
  490. struct gfar_private *priv = NULL;
  491. struct device_node *np = ofdev->node;
  492. struct device_node *child = NULL;
  493. const u32 *stash;
  494. const u32 *stash_len;
  495. const u32 *stash_idx;
  496. unsigned int num_tx_qs, num_rx_qs;
  497. u32 *tx_queues, *rx_queues;
  498. if (!np || !of_device_is_available(np))
  499. return -ENODEV;
  500. /* parse the num of tx and rx queues */
  501. tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
  502. num_tx_qs = tx_queues ? *tx_queues : 1;
  503. if (num_tx_qs > MAX_TX_QS) {
  504. printk(KERN_ERR "num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
  505. num_tx_qs, MAX_TX_QS);
  506. printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
  507. return -EINVAL;
  508. }
  509. rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
  510. num_rx_qs = rx_queues ? *rx_queues : 1;
  511. if (num_rx_qs > MAX_RX_QS) {
  512. printk(KERN_ERR "num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
  513. num_tx_qs, MAX_TX_QS);
  514. printk(KERN_ERR "Cannot do alloc_etherdev, aborting\n");
  515. return -EINVAL;
  516. }
  517. *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
  518. dev = *pdev;
  519. if (NULL == dev)
  520. return -ENOMEM;
  521. priv = netdev_priv(dev);
  522. priv->node = ofdev->node;
  523. priv->ndev = dev;
  524. dev->num_tx_queues = num_tx_qs;
  525. dev->real_num_tx_queues = num_tx_qs;
  526. priv->num_tx_queues = num_tx_qs;
  527. priv->num_rx_queues = num_rx_qs;
  528. priv->num_grps = 0x0;
  529. model = of_get_property(np, "model", NULL);
  530. for (i = 0; i < MAXGROUPS; i++)
  531. priv->gfargrp[i].regs = NULL;
  532. /* Parse and initialize group specific information */
  533. if (of_device_is_compatible(np, "fsl,etsec2")) {
  534. priv->mode = MQ_MG_MODE;
  535. for_each_child_of_node(np, child) {
  536. err = gfar_parse_group(child, priv, model);
  537. if (err)
  538. goto err_grp_init;
  539. }
  540. } else {
  541. priv->mode = SQ_SG_MODE;
  542. err = gfar_parse_group(np, priv, model);
  543. if(err)
  544. goto err_grp_init;
  545. }
  546. for (i = 0; i < priv->num_tx_queues; i++)
  547. priv->tx_queue[i] = NULL;
  548. for (i = 0; i < priv->num_rx_queues; i++)
  549. priv->rx_queue[i] = NULL;
  550. for (i = 0; i < priv->num_tx_queues; i++) {
  551. priv->tx_queue[i] = (struct gfar_priv_tx_q *)kmalloc(
  552. sizeof (struct gfar_priv_tx_q), GFP_KERNEL);
  553. if (!priv->tx_queue[i]) {
  554. err = -ENOMEM;
  555. goto tx_alloc_failed;
  556. }
  557. priv->tx_queue[i]->tx_skbuff = NULL;
  558. priv->tx_queue[i]->qindex = i;
  559. priv->tx_queue[i]->dev = dev;
  560. spin_lock_init(&(priv->tx_queue[i]->txlock));
  561. }
  562. for (i = 0; i < priv->num_rx_queues; i++) {
  563. priv->rx_queue[i] = (struct gfar_priv_rx_q *)kmalloc(
  564. sizeof (struct gfar_priv_rx_q), GFP_KERNEL);
  565. if (!priv->rx_queue[i]) {
  566. err = -ENOMEM;
  567. goto rx_alloc_failed;
  568. }
  569. priv->rx_queue[i]->rx_skbuff = NULL;
  570. priv->rx_queue[i]->qindex = i;
  571. priv->rx_queue[i]->dev = dev;
  572. spin_lock_init(&(priv->rx_queue[i]->rxlock));
  573. }
  574. stash = of_get_property(np, "bd-stash", NULL);
  575. if (stash) {
  576. priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
  577. priv->bd_stash_en = 1;
  578. }
  579. stash_len = of_get_property(np, "rx-stash-len", NULL);
  580. if (stash_len)
  581. priv->rx_stash_size = *stash_len;
  582. stash_idx = of_get_property(np, "rx-stash-idx", NULL);
  583. if (stash_idx)
  584. priv->rx_stash_index = *stash_idx;
  585. if (stash_len || stash_idx)
  586. priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
  587. mac_addr = of_get_mac_address(np);
  588. if (mac_addr)
  589. memcpy(dev->dev_addr, mac_addr, MAC_ADDR_LEN);
  590. if (model && !strcasecmp(model, "TSEC"))
  591. priv->device_flags =
  592. FSL_GIANFAR_DEV_HAS_GIGABIT |
  593. FSL_GIANFAR_DEV_HAS_COALESCE |
  594. FSL_GIANFAR_DEV_HAS_RMON |
  595. FSL_GIANFAR_DEV_HAS_MULTI_INTR;
  596. if (model && !strcasecmp(model, "eTSEC"))
  597. priv->device_flags =
  598. FSL_GIANFAR_DEV_HAS_GIGABIT |
  599. FSL_GIANFAR_DEV_HAS_COALESCE |
  600. FSL_GIANFAR_DEV_HAS_RMON |
  601. FSL_GIANFAR_DEV_HAS_MULTI_INTR |
  602. FSL_GIANFAR_DEV_HAS_PADDING |
  603. FSL_GIANFAR_DEV_HAS_CSUM |
  604. FSL_GIANFAR_DEV_HAS_VLAN |
  605. FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
  606. FSL_GIANFAR_DEV_HAS_EXTENDED_HASH;
  607. ctype = of_get_property(np, "phy-connection-type", NULL);
  608. /* We only care about rgmii-id. The rest are autodetected */
  609. if (ctype && !strcmp(ctype, "rgmii-id"))
  610. priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
  611. else
  612. priv->interface = PHY_INTERFACE_MODE_MII;
  613. if (of_get_property(np, "fsl,magic-packet", NULL))
  614. priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
  615. priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
  616. /* Find the TBI PHY. If it's not there, we don't support SGMII */
  617. priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
  618. return 0;
  619. rx_alloc_failed:
  620. free_rx_pointers(priv);
  621. tx_alloc_failed:
  622. free_tx_pointers(priv);
  623. err_grp_init:
  624. unmap_group_regs(priv);
  625. free_netdev(dev);
  626. return err;
  627. }
  628. /* Ioctl MII Interface */
  629. static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  630. {
  631. struct gfar_private *priv = netdev_priv(dev);
  632. if (!netif_running(dev))
  633. return -EINVAL;
  634. if (!priv->phydev)
  635. return -ENODEV;
  636. return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
  637. }
  638. static unsigned int reverse_bitmap(unsigned int bit_map, unsigned int max_qs)
  639. {
  640. unsigned int new_bit_map = 0x0;
  641. int mask = 0x1 << (max_qs - 1), i;
  642. for (i = 0; i < max_qs; i++) {
  643. if (bit_map & mask)
  644. new_bit_map = new_bit_map + (1 << i);
  645. mask = mask >> 0x1;
  646. }
  647. return new_bit_map;
  648. }
  649. static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
  650. u32 class)
  651. {
  652. u32 rqfpr = FPR_FILER_MASK;
  653. u32 rqfcr = 0x0;
  654. rqfar--;
  655. rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
  656. ftp_rqfpr[rqfar] = rqfpr;
  657. ftp_rqfcr[rqfar] = rqfcr;
  658. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  659. rqfar--;
  660. rqfcr = RQFCR_CMP_NOMATCH;
  661. ftp_rqfpr[rqfar] = rqfpr;
  662. ftp_rqfcr[rqfar] = rqfcr;
  663. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  664. rqfar--;
  665. rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
  666. rqfpr = class;
  667. ftp_rqfcr[rqfar] = rqfcr;
  668. ftp_rqfpr[rqfar] = rqfpr;
  669. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  670. rqfar--;
  671. rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
  672. rqfpr = class;
  673. ftp_rqfcr[rqfar] = rqfcr;
  674. ftp_rqfpr[rqfar] = rqfpr;
  675. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  676. return rqfar;
  677. }
  678. static void gfar_init_filer_table(struct gfar_private *priv)
  679. {
  680. int i = 0x0;
  681. u32 rqfar = MAX_FILER_IDX;
  682. u32 rqfcr = 0x0;
  683. u32 rqfpr = FPR_FILER_MASK;
  684. /* Default rule */
  685. rqfcr = RQFCR_CMP_MATCH;
  686. ftp_rqfcr[rqfar] = rqfcr;
  687. ftp_rqfpr[rqfar] = rqfpr;
  688. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  689. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
  690. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
  691. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
  692. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
  693. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
  694. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
  695. /* cur_filer_idx indicated the fisrt non-masked rule */
  696. priv->cur_filer_idx = rqfar;
  697. /* Rest are masked rules */
  698. rqfcr = RQFCR_CMP_NOMATCH;
  699. for (i = 0; i < rqfar; i++) {
  700. ftp_rqfcr[i] = rqfcr;
  701. ftp_rqfpr[i] = rqfpr;
  702. gfar_write_filer(priv, i, rqfcr, rqfpr);
  703. }
  704. }
  705. /* Set up the ethernet device structure, private data,
  706. * and anything else we need before we start */
  707. static int gfar_probe(struct of_device *ofdev,
  708. const struct of_device_id *match)
  709. {
  710. u32 tempval;
  711. struct net_device *dev = NULL;
  712. struct gfar_private *priv = NULL;
  713. struct gfar __iomem *regs = NULL;
  714. int err = 0, i, grp_idx = 0;
  715. int len_devname;
  716. u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
  717. u32 isrg = 0;
  718. u32 __iomem *baddr;
  719. err = gfar_of_init(ofdev, &dev);
  720. if (err)
  721. return err;
  722. priv = netdev_priv(dev);
  723. priv->ndev = dev;
  724. priv->ofdev = ofdev;
  725. priv->node = ofdev->node;
  726. SET_NETDEV_DEV(dev, &ofdev->dev);
  727. spin_lock_init(&priv->bflock);
  728. INIT_WORK(&priv->reset_task, gfar_reset_task);
  729. dev_set_drvdata(&ofdev->dev, priv);
  730. regs = priv->gfargrp[0].regs;
  731. /* Stop the DMA engine now, in case it was running before */
  732. /* (The firmware could have used it, and left it running). */
  733. gfar_halt(dev);
  734. /* Reset MAC layer */
  735. gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
  736. /* We need to delay at least 3 TX clocks */
  737. udelay(2);
  738. tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  739. gfar_write(&regs->maccfg1, tempval);
  740. /* Initialize MACCFG2. */
  741. gfar_write(&regs->maccfg2, MACCFG2_INIT_SETTINGS);
  742. /* Initialize ECNTRL */
  743. gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
  744. /* Set the dev->base_addr to the gfar reg region */
  745. dev->base_addr = (unsigned long) regs;
  746. SET_NETDEV_DEV(dev, &ofdev->dev);
  747. /* Fill in the dev structure */
  748. dev->watchdog_timeo = TX_TIMEOUT;
  749. dev->mtu = 1500;
  750. dev->netdev_ops = &gfar_netdev_ops;
  751. dev->ethtool_ops = &gfar_ethtool_ops;
  752. /* Register for napi ...We are registering NAPI for each grp */
  753. for (i = 0; i < priv->num_grps; i++)
  754. netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll, GFAR_DEV_WEIGHT);
  755. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
  756. priv->rx_csum_enable = 1;
  757. dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_HIGHDMA;
  758. } else
  759. priv->rx_csum_enable = 0;
  760. priv->vlgrp = NULL;
  761. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN)
  762. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  763. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
  764. priv->extended_hash = 1;
  765. priv->hash_width = 9;
  766. priv->hash_regs[0] = &regs->igaddr0;
  767. priv->hash_regs[1] = &regs->igaddr1;
  768. priv->hash_regs[2] = &regs->igaddr2;
  769. priv->hash_regs[3] = &regs->igaddr3;
  770. priv->hash_regs[4] = &regs->igaddr4;
  771. priv->hash_regs[5] = &regs->igaddr5;
  772. priv->hash_regs[6] = &regs->igaddr6;
  773. priv->hash_regs[7] = &regs->igaddr7;
  774. priv->hash_regs[8] = &regs->gaddr0;
  775. priv->hash_regs[9] = &regs->gaddr1;
  776. priv->hash_regs[10] = &regs->gaddr2;
  777. priv->hash_regs[11] = &regs->gaddr3;
  778. priv->hash_regs[12] = &regs->gaddr4;
  779. priv->hash_regs[13] = &regs->gaddr5;
  780. priv->hash_regs[14] = &regs->gaddr6;
  781. priv->hash_regs[15] = &regs->gaddr7;
  782. } else {
  783. priv->extended_hash = 0;
  784. priv->hash_width = 8;
  785. priv->hash_regs[0] = &regs->gaddr0;
  786. priv->hash_regs[1] = &regs->gaddr1;
  787. priv->hash_regs[2] = &regs->gaddr2;
  788. priv->hash_regs[3] = &regs->gaddr3;
  789. priv->hash_regs[4] = &regs->gaddr4;
  790. priv->hash_regs[5] = &regs->gaddr5;
  791. priv->hash_regs[6] = &regs->gaddr6;
  792. priv->hash_regs[7] = &regs->gaddr7;
  793. }
  794. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
  795. priv->padding = DEFAULT_PADDING;
  796. else
  797. priv->padding = 0;
  798. if (dev->features & NETIF_F_IP_CSUM)
  799. dev->hard_header_len += GMAC_FCB_LEN;
  800. /* Program the isrg regs only if number of grps > 1 */
  801. if (priv->num_grps > 1) {
  802. baddr = &regs->isrg0;
  803. for (i = 0; i < priv->num_grps; i++) {
  804. isrg |= (priv->gfargrp[i].rx_bit_map << ISRG_SHIFT_RX);
  805. isrg |= (priv->gfargrp[i].tx_bit_map << ISRG_SHIFT_TX);
  806. gfar_write(baddr, isrg);
  807. baddr++;
  808. isrg = 0x0;
  809. }
  810. }
  811. /* Need to reverse the bit maps as bit_map's MSB is q0
  812. * but, for_each_bit parses from right to left, which
  813. * basically reverses the queue numbers */
  814. for (i = 0; i< priv->num_grps; i++) {
  815. priv->gfargrp[i].tx_bit_map = reverse_bitmap(
  816. priv->gfargrp[i].tx_bit_map, MAX_TX_QS);
  817. priv->gfargrp[i].rx_bit_map = reverse_bitmap(
  818. priv->gfargrp[i].rx_bit_map, MAX_RX_QS);
  819. }
  820. /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
  821. * also assign queues to groups */
  822. for (grp_idx = 0; grp_idx < priv->num_grps; grp_idx++) {
  823. priv->gfargrp[grp_idx].num_rx_queues = 0x0;
  824. for_each_bit(i, &priv->gfargrp[grp_idx].rx_bit_map,
  825. priv->num_rx_queues) {
  826. priv->gfargrp[grp_idx].num_rx_queues++;
  827. priv->rx_queue[i]->grp = &priv->gfargrp[grp_idx];
  828. rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
  829. rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
  830. }
  831. priv->gfargrp[grp_idx].num_tx_queues = 0x0;
  832. for_each_bit (i, &priv->gfargrp[grp_idx].tx_bit_map,
  833. priv->num_tx_queues) {
  834. priv->gfargrp[grp_idx].num_tx_queues++;
  835. priv->tx_queue[i]->grp = &priv->gfargrp[grp_idx];
  836. tstat = tstat | (TSTAT_CLEAR_THALT >> i);
  837. tqueue = tqueue | (TQUEUE_EN0 >> i);
  838. }
  839. priv->gfargrp[grp_idx].rstat = rstat;
  840. priv->gfargrp[grp_idx].tstat = tstat;
  841. rstat = tstat =0;
  842. }
  843. gfar_write(&regs->rqueue, rqueue);
  844. gfar_write(&regs->tqueue, tqueue);
  845. priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
  846. /* Initializing some of the rx/tx queue level parameters */
  847. for (i = 0; i < priv->num_tx_queues; i++) {
  848. priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
  849. priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
  850. priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
  851. priv->tx_queue[i]->txic = DEFAULT_TXIC;
  852. }
  853. for (i = 0; i < priv->num_rx_queues; i++) {
  854. priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
  855. priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
  856. priv->rx_queue[i]->rxic = DEFAULT_RXIC;
  857. }
  858. /* enable filer if using multiple RX queues*/
  859. if(priv->num_rx_queues > 1)
  860. priv->rx_filer_enable = 1;
  861. /* Enable most messages by default */
  862. priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  863. /* Carrier starts down, phylib will bring it up */
  864. netif_carrier_off(dev);
  865. err = register_netdev(dev);
  866. if (err) {
  867. printk(KERN_ERR "%s: Cannot register net device, aborting.\n",
  868. dev->name);
  869. goto register_fail;
  870. }
  871. device_init_wakeup(&dev->dev,
  872. priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  873. /* fill out IRQ number and name fields */
  874. len_devname = strlen(dev->name);
  875. for (i = 0; i < priv->num_grps; i++) {
  876. strncpy(&priv->gfargrp[i].int_name_tx[0], dev->name,
  877. len_devname);
  878. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  879. strncpy(&priv->gfargrp[i].int_name_tx[len_devname],
  880. "_g", sizeof("_g"));
  881. priv->gfargrp[i].int_name_tx[
  882. strlen(priv->gfargrp[i].int_name_tx)] = i+48;
  883. strncpy(&priv->gfargrp[i].int_name_tx[strlen(
  884. priv->gfargrp[i].int_name_tx)],
  885. "_tx", sizeof("_tx") + 1);
  886. strncpy(&priv->gfargrp[i].int_name_rx[0], dev->name,
  887. len_devname);
  888. strncpy(&priv->gfargrp[i].int_name_rx[len_devname],
  889. "_g", sizeof("_g"));
  890. priv->gfargrp[i].int_name_rx[
  891. strlen(priv->gfargrp[i].int_name_rx)] = i+48;
  892. strncpy(&priv->gfargrp[i].int_name_rx[strlen(
  893. priv->gfargrp[i].int_name_rx)],
  894. "_rx", sizeof("_rx") + 1);
  895. strncpy(&priv->gfargrp[i].int_name_er[0], dev->name,
  896. len_devname);
  897. strncpy(&priv->gfargrp[i].int_name_er[len_devname],
  898. "_g", sizeof("_g"));
  899. priv->gfargrp[i].int_name_er[strlen(
  900. priv->gfargrp[i].int_name_er)] = i+48;
  901. strncpy(&priv->gfargrp[i].int_name_er[strlen(\
  902. priv->gfargrp[i].int_name_er)],
  903. "_er", sizeof("_er") + 1);
  904. } else
  905. priv->gfargrp[i].int_name_tx[len_devname] = '\0';
  906. }
  907. /* Initialize the filer table */
  908. gfar_init_filer_table(priv);
  909. /* Create all the sysfs files */
  910. gfar_init_sysfs(dev);
  911. /* Print out the device info */
  912. printk(KERN_INFO DEVICE_NAME "%pM\n", dev->name, dev->dev_addr);
  913. /* Even more device info helps when determining which kernel */
  914. /* provided which set of benchmarks. */
  915. printk(KERN_INFO "%s: Running with NAPI enabled\n", dev->name);
  916. for (i = 0; i < priv->num_rx_queues; i++)
  917. printk(KERN_INFO "%s: :RX BD ring size for Q[%d]: %d\n",
  918. dev->name, i, priv->rx_queue[i]->rx_ring_size);
  919. for(i = 0; i < priv->num_tx_queues; i++)
  920. printk(KERN_INFO "%s:TX BD ring size for Q[%d]: %d\n",
  921. dev->name, i, priv->tx_queue[i]->tx_ring_size);
  922. return 0;
  923. register_fail:
  924. unmap_group_regs(priv);
  925. free_tx_pointers(priv);
  926. free_rx_pointers(priv);
  927. if (priv->phy_node)
  928. of_node_put(priv->phy_node);
  929. if (priv->tbi_node)
  930. of_node_put(priv->tbi_node);
  931. free_netdev(dev);
  932. return err;
  933. }
  934. static int gfar_remove(struct of_device *ofdev)
  935. {
  936. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  937. if (priv->phy_node)
  938. of_node_put(priv->phy_node);
  939. if (priv->tbi_node)
  940. of_node_put(priv->tbi_node);
  941. dev_set_drvdata(&ofdev->dev, NULL);
  942. unregister_netdev(priv->ndev);
  943. unmap_group_regs(priv);
  944. free_netdev(priv->ndev);
  945. return 0;
  946. }
  947. #ifdef CONFIG_PM
  948. static int gfar_suspend(struct device *dev)
  949. {
  950. struct gfar_private *priv = dev_get_drvdata(dev);
  951. struct net_device *ndev = priv->ndev;
  952. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  953. unsigned long flags;
  954. u32 tempval;
  955. int magic_packet = priv->wol_en &&
  956. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  957. netif_device_detach(ndev);
  958. if (netif_running(ndev)) {
  959. local_irq_save(flags);
  960. lock_tx_qs(priv);
  961. lock_rx_qs(priv);
  962. gfar_halt_nodisable(ndev);
  963. /* Disable Tx, and Rx if wake-on-LAN is disabled. */
  964. tempval = gfar_read(&regs->maccfg1);
  965. tempval &= ~MACCFG1_TX_EN;
  966. if (!magic_packet)
  967. tempval &= ~MACCFG1_RX_EN;
  968. gfar_write(&regs->maccfg1, tempval);
  969. unlock_rx_qs(priv);
  970. unlock_tx_qs(priv);
  971. local_irq_restore(flags);
  972. disable_napi(priv);
  973. if (magic_packet) {
  974. /* Enable interrupt on Magic Packet */
  975. gfar_write(&regs->imask, IMASK_MAG);
  976. /* Enable Magic Packet mode */
  977. tempval = gfar_read(&regs->maccfg2);
  978. tempval |= MACCFG2_MPEN;
  979. gfar_write(&regs->maccfg2, tempval);
  980. } else {
  981. phy_stop(priv->phydev);
  982. }
  983. }
  984. return 0;
  985. }
  986. static int gfar_resume(struct device *dev)
  987. {
  988. struct gfar_private *priv = dev_get_drvdata(dev);
  989. struct net_device *ndev = priv->ndev;
  990. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  991. unsigned long flags;
  992. u32 tempval;
  993. int magic_packet = priv->wol_en &&
  994. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  995. if (!netif_running(ndev)) {
  996. netif_device_attach(ndev);
  997. return 0;
  998. }
  999. if (!magic_packet && priv->phydev)
  1000. phy_start(priv->phydev);
  1001. /* Disable Magic Packet mode, in case something
  1002. * else woke us up.
  1003. */
  1004. local_irq_save(flags);
  1005. lock_tx_qs(priv);
  1006. lock_rx_qs(priv);
  1007. tempval = gfar_read(&regs->maccfg2);
  1008. tempval &= ~MACCFG2_MPEN;
  1009. gfar_write(&regs->maccfg2, tempval);
  1010. gfar_start(ndev);
  1011. unlock_rx_qs(priv);
  1012. unlock_tx_qs(priv);
  1013. local_irq_restore(flags);
  1014. netif_device_attach(ndev);
  1015. enable_napi(priv);
  1016. return 0;
  1017. }
  1018. static int gfar_restore(struct device *dev)
  1019. {
  1020. struct gfar_private *priv = dev_get_drvdata(dev);
  1021. struct net_device *ndev = priv->ndev;
  1022. if (!netif_running(ndev))
  1023. return 0;
  1024. gfar_init_bds(ndev);
  1025. init_registers(ndev);
  1026. gfar_set_mac_address(ndev);
  1027. gfar_init_mac(ndev);
  1028. gfar_start(ndev);
  1029. priv->oldlink = 0;
  1030. priv->oldspeed = 0;
  1031. priv->oldduplex = -1;
  1032. if (priv->phydev)
  1033. phy_start(priv->phydev);
  1034. netif_device_attach(ndev);
  1035. enable_napi(priv);
  1036. return 0;
  1037. }
  1038. static struct dev_pm_ops gfar_pm_ops = {
  1039. .suspend = gfar_suspend,
  1040. .resume = gfar_resume,
  1041. .freeze = gfar_suspend,
  1042. .thaw = gfar_resume,
  1043. .restore = gfar_restore,
  1044. };
  1045. #define GFAR_PM_OPS (&gfar_pm_ops)
  1046. static int gfar_legacy_suspend(struct of_device *ofdev, pm_message_t state)
  1047. {
  1048. return gfar_suspend(&ofdev->dev);
  1049. }
  1050. static int gfar_legacy_resume(struct of_device *ofdev)
  1051. {
  1052. return gfar_resume(&ofdev->dev);
  1053. }
  1054. #else
  1055. #define GFAR_PM_OPS NULL
  1056. #define gfar_legacy_suspend NULL
  1057. #define gfar_legacy_resume NULL
  1058. #endif
  1059. /* Reads the controller's registers to determine what interface
  1060. * connects it to the PHY.
  1061. */
  1062. static phy_interface_t gfar_get_interface(struct net_device *dev)
  1063. {
  1064. struct gfar_private *priv = netdev_priv(dev);
  1065. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1066. u32 ecntrl;
  1067. ecntrl = gfar_read(&regs->ecntrl);
  1068. if (ecntrl & ECNTRL_SGMII_MODE)
  1069. return PHY_INTERFACE_MODE_SGMII;
  1070. if (ecntrl & ECNTRL_TBI_MODE) {
  1071. if (ecntrl & ECNTRL_REDUCED_MODE)
  1072. return PHY_INTERFACE_MODE_RTBI;
  1073. else
  1074. return PHY_INTERFACE_MODE_TBI;
  1075. }
  1076. if (ecntrl & ECNTRL_REDUCED_MODE) {
  1077. if (ecntrl & ECNTRL_REDUCED_MII_MODE)
  1078. return PHY_INTERFACE_MODE_RMII;
  1079. else {
  1080. phy_interface_t interface = priv->interface;
  1081. /*
  1082. * This isn't autodetected right now, so it must
  1083. * be set by the device tree or platform code.
  1084. */
  1085. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  1086. return PHY_INTERFACE_MODE_RGMII_ID;
  1087. return PHY_INTERFACE_MODE_RGMII;
  1088. }
  1089. }
  1090. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  1091. return PHY_INTERFACE_MODE_GMII;
  1092. return PHY_INTERFACE_MODE_MII;
  1093. }
  1094. /* Initializes driver's PHY state, and attaches to the PHY.
  1095. * Returns 0 on success.
  1096. */
  1097. static int init_phy(struct net_device *dev)
  1098. {
  1099. struct gfar_private *priv = netdev_priv(dev);
  1100. uint gigabit_support =
  1101. priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
  1102. SUPPORTED_1000baseT_Full : 0;
  1103. phy_interface_t interface;
  1104. priv->oldlink = 0;
  1105. priv->oldspeed = 0;
  1106. priv->oldduplex = -1;
  1107. interface = gfar_get_interface(dev);
  1108. priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
  1109. interface);
  1110. if (!priv->phydev)
  1111. priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
  1112. interface);
  1113. if (!priv->phydev) {
  1114. dev_err(&dev->dev, "could not attach to PHY\n");
  1115. return -ENODEV;
  1116. }
  1117. if (interface == PHY_INTERFACE_MODE_SGMII)
  1118. gfar_configure_serdes(dev);
  1119. /* Remove any features not supported by the controller */
  1120. priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
  1121. priv->phydev->advertising = priv->phydev->supported;
  1122. return 0;
  1123. }
  1124. /*
  1125. * Initialize TBI PHY interface for communicating with the
  1126. * SERDES lynx PHY on the chip. We communicate with this PHY
  1127. * through the MDIO bus on each controller, treating it as a
  1128. * "normal" PHY at the address found in the TBIPA register. We assume
  1129. * that the TBIPA register is valid. Either the MDIO bus code will set
  1130. * it to a value that doesn't conflict with other PHYs on the bus, or the
  1131. * value doesn't matter, as there are no other PHYs on the bus.
  1132. */
  1133. static void gfar_configure_serdes(struct net_device *dev)
  1134. {
  1135. struct gfar_private *priv = netdev_priv(dev);
  1136. struct phy_device *tbiphy;
  1137. if (!priv->tbi_node) {
  1138. dev_warn(&dev->dev, "error: SGMII mode requires that the "
  1139. "device tree specify a tbi-handle\n");
  1140. return;
  1141. }
  1142. tbiphy = of_phy_find_device(priv->tbi_node);
  1143. if (!tbiphy) {
  1144. dev_err(&dev->dev, "error: Could not get TBI device\n");
  1145. return;
  1146. }
  1147. /*
  1148. * If the link is already up, we must already be ok, and don't need to
  1149. * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
  1150. * everything for us? Resetting it takes the link down and requires
  1151. * several seconds for it to come back.
  1152. */
  1153. if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
  1154. return;
  1155. /* Single clk mode, mii mode off(for serdes communication) */
  1156. phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
  1157. phy_write(tbiphy, MII_ADVERTISE,
  1158. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  1159. ADVERTISE_1000XPSE_ASYM);
  1160. phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
  1161. BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
  1162. }
  1163. static void init_registers(struct net_device *dev)
  1164. {
  1165. struct gfar_private *priv = netdev_priv(dev);
  1166. struct gfar __iomem *regs = NULL;
  1167. int i = 0;
  1168. for (i = 0; i < priv->num_grps; i++) {
  1169. regs = priv->gfargrp[i].regs;
  1170. /* Clear IEVENT */
  1171. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  1172. /* Initialize IMASK */
  1173. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  1174. }
  1175. regs = priv->gfargrp[0].regs;
  1176. /* Init hash registers to zero */
  1177. gfar_write(&regs->igaddr0, 0);
  1178. gfar_write(&regs->igaddr1, 0);
  1179. gfar_write(&regs->igaddr2, 0);
  1180. gfar_write(&regs->igaddr3, 0);
  1181. gfar_write(&regs->igaddr4, 0);
  1182. gfar_write(&regs->igaddr5, 0);
  1183. gfar_write(&regs->igaddr6, 0);
  1184. gfar_write(&regs->igaddr7, 0);
  1185. gfar_write(&regs->gaddr0, 0);
  1186. gfar_write(&regs->gaddr1, 0);
  1187. gfar_write(&regs->gaddr2, 0);
  1188. gfar_write(&regs->gaddr3, 0);
  1189. gfar_write(&regs->gaddr4, 0);
  1190. gfar_write(&regs->gaddr5, 0);
  1191. gfar_write(&regs->gaddr6, 0);
  1192. gfar_write(&regs->gaddr7, 0);
  1193. /* Zero out the rmon mib registers if it has them */
  1194. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  1195. memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
  1196. /* Mask off the CAM interrupts */
  1197. gfar_write(&regs->rmon.cam1, 0xffffffff);
  1198. gfar_write(&regs->rmon.cam2, 0xffffffff);
  1199. }
  1200. /* Initialize the max receive buffer length */
  1201. gfar_write(&regs->mrblr, priv->rx_buffer_size);
  1202. /* Initialize the Minimum Frame Length Register */
  1203. gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
  1204. }
  1205. /* Halt the receive and transmit queues */
  1206. static void gfar_halt_nodisable(struct net_device *dev)
  1207. {
  1208. struct gfar_private *priv = netdev_priv(dev);
  1209. struct gfar __iomem *regs = NULL;
  1210. u32 tempval;
  1211. int i = 0;
  1212. for (i = 0; i < priv->num_grps; i++) {
  1213. regs = priv->gfargrp[i].regs;
  1214. /* Mask all interrupts */
  1215. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  1216. /* Clear all interrupts */
  1217. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  1218. }
  1219. regs = priv->gfargrp[0].regs;
  1220. /* Stop the DMA, and wait for it to stop */
  1221. tempval = gfar_read(&regs->dmactrl);
  1222. if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
  1223. != (DMACTRL_GRS | DMACTRL_GTS)) {
  1224. tempval |= (DMACTRL_GRS | DMACTRL_GTS);
  1225. gfar_write(&regs->dmactrl, tempval);
  1226. while (!(gfar_read(&regs->ievent) &
  1227. (IEVENT_GRSC | IEVENT_GTSC)))
  1228. cpu_relax();
  1229. }
  1230. }
  1231. /* Halt the receive and transmit queues */
  1232. void gfar_halt(struct net_device *dev)
  1233. {
  1234. struct gfar_private *priv = netdev_priv(dev);
  1235. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1236. u32 tempval;
  1237. gfar_halt_nodisable(dev);
  1238. /* Disable Rx and Tx */
  1239. tempval = gfar_read(&regs->maccfg1);
  1240. tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
  1241. gfar_write(&regs->maccfg1, tempval);
  1242. }
  1243. static void free_grp_irqs(struct gfar_priv_grp *grp)
  1244. {
  1245. free_irq(grp->interruptError, grp);
  1246. free_irq(grp->interruptTransmit, grp);
  1247. free_irq(grp->interruptReceive, grp);
  1248. }
  1249. void stop_gfar(struct net_device *dev)
  1250. {
  1251. struct gfar_private *priv = netdev_priv(dev);
  1252. unsigned long flags;
  1253. int i;
  1254. phy_stop(priv->phydev);
  1255. /* Lock it down */
  1256. local_irq_save(flags);
  1257. lock_tx_qs(priv);
  1258. lock_rx_qs(priv);
  1259. gfar_halt(dev);
  1260. unlock_rx_qs(priv);
  1261. unlock_tx_qs(priv);
  1262. local_irq_restore(flags);
  1263. /* Free the IRQs */
  1264. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1265. for (i = 0; i < priv->num_grps; i++)
  1266. free_grp_irqs(&priv->gfargrp[i]);
  1267. } else {
  1268. for (i = 0; i < priv->num_grps; i++)
  1269. free_irq(priv->gfargrp[i].interruptTransmit,
  1270. &priv->gfargrp[i]);
  1271. }
  1272. free_skb_resources(priv);
  1273. }
  1274. static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
  1275. {
  1276. struct txbd8 *txbdp;
  1277. struct gfar_private *priv = netdev_priv(tx_queue->dev);
  1278. int i, j;
  1279. txbdp = tx_queue->tx_bd_base;
  1280. for (i = 0; i < tx_queue->tx_ring_size; i++) {
  1281. if (!tx_queue->tx_skbuff[i])
  1282. continue;
  1283. dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
  1284. txbdp->length, DMA_TO_DEVICE);
  1285. txbdp->lstatus = 0;
  1286. for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
  1287. j++) {
  1288. txbdp++;
  1289. dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
  1290. txbdp->length, DMA_TO_DEVICE);
  1291. }
  1292. txbdp++;
  1293. dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
  1294. tx_queue->tx_skbuff[i] = NULL;
  1295. }
  1296. kfree(tx_queue->tx_skbuff);
  1297. }
  1298. static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
  1299. {
  1300. struct rxbd8 *rxbdp;
  1301. struct gfar_private *priv = netdev_priv(rx_queue->dev);
  1302. int i;
  1303. rxbdp = rx_queue->rx_bd_base;
  1304. for (i = 0; i < rx_queue->rx_ring_size; i++) {
  1305. if (rx_queue->rx_skbuff[i]) {
  1306. dma_unmap_single(&priv->ofdev->dev,
  1307. rxbdp->bufPtr, priv->rx_buffer_size,
  1308. DMA_FROM_DEVICE);
  1309. dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
  1310. rx_queue->rx_skbuff[i] = NULL;
  1311. }
  1312. rxbdp->lstatus = 0;
  1313. rxbdp->bufPtr = 0;
  1314. rxbdp++;
  1315. }
  1316. kfree(rx_queue->rx_skbuff);
  1317. }
  1318. /* If there are any tx skbs or rx skbs still around, free them.
  1319. * Then free tx_skbuff and rx_skbuff */
  1320. static void free_skb_resources(struct gfar_private *priv)
  1321. {
  1322. struct gfar_priv_tx_q *tx_queue = NULL;
  1323. struct gfar_priv_rx_q *rx_queue = NULL;
  1324. int i;
  1325. /* Go through all the buffer descriptors and free their data buffers */
  1326. for (i = 0; i < priv->num_tx_queues; i++) {
  1327. tx_queue = priv->tx_queue[i];
  1328. if(!tx_queue->tx_skbuff)
  1329. free_skb_tx_queue(tx_queue);
  1330. }
  1331. for (i = 0; i < priv->num_rx_queues; i++) {
  1332. rx_queue = priv->rx_queue[i];
  1333. if(!rx_queue->rx_skbuff)
  1334. free_skb_rx_queue(rx_queue);
  1335. }
  1336. dma_free_coherent(&priv->ofdev->dev,
  1337. sizeof(struct txbd8) * priv->total_tx_ring_size +
  1338. sizeof(struct rxbd8) * priv->total_rx_ring_size,
  1339. priv->tx_queue[0]->tx_bd_base,
  1340. priv->tx_queue[0]->tx_bd_dma_base);
  1341. }
  1342. void gfar_start(struct net_device *dev)
  1343. {
  1344. struct gfar_private *priv = netdev_priv(dev);
  1345. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1346. u32 tempval;
  1347. int i = 0;
  1348. /* Enable Rx and Tx in MACCFG1 */
  1349. tempval = gfar_read(&regs->maccfg1);
  1350. tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  1351. gfar_write(&regs->maccfg1, tempval);
  1352. /* Initialize DMACTRL to have WWR and WOP */
  1353. tempval = gfar_read(&regs->dmactrl);
  1354. tempval |= DMACTRL_INIT_SETTINGS;
  1355. gfar_write(&regs->dmactrl, tempval);
  1356. /* Make sure we aren't stopped */
  1357. tempval = gfar_read(&regs->dmactrl);
  1358. tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
  1359. gfar_write(&regs->dmactrl, tempval);
  1360. for (i = 0; i < priv->num_grps; i++) {
  1361. regs = priv->gfargrp[i].regs;
  1362. /* Clear THLT/RHLT, so that the DMA starts polling now */
  1363. gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
  1364. gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
  1365. /* Unmask the interrupts we look for */
  1366. gfar_write(&regs->imask, IMASK_DEFAULT);
  1367. }
  1368. dev->trans_start = jiffies;
  1369. }
  1370. void gfar_configure_coalescing(struct gfar_private *priv,
  1371. unsigned long tx_mask, unsigned long rx_mask)
  1372. {
  1373. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1374. u32 __iomem *baddr;
  1375. int i = 0;
  1376. /* Backward compatible case ---- even if we enable
  1377. * multiple queues, there's only single reg to program
  1378. */
  1379. gfar_write(&regs->txic, 0);
  1380. if(likely(priv->tx_queue[0]->txcoalescing))
  1381. gfar_write(&regs->txic, priv->tx_queue[0]->txic);
  1382. gfar_write(&regs->rxic, 0);
  1383. if(unlikely(priv->rx_queue[0]->rxcoalescing))
  1384. gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
  1385. if (priv->mode == MQ_MG_MODE) {
  1386. baddr = &regs->txic0;
  1387. for_each_bit (i, &tx_mask, priv->num_tx_queues) {
  1388. if (likely(priv->tx_queue[i]->txcoalescing)) {
  1389. gfar_write(baddr + i, 0);
  1390. gfar_write(baddr + i, priv->tx_queue[i]->txic);
  1391. }
  1392. }
  1393. baddr = &regs->rxic0;
  1394. for_each_bit (i, &rx_mask, priv->num_rx_queues) {
  1395. if (likely(priv->rx_queue[i]->rxcoalescing)) {
  1396. gfar_write(baddr + i, 0);
  1397. gfar_write(baddr + i, priv->rx_queue[i]->rxic);
  1398. }
  1399. }
  1400. }
  1401. }
  1402. static int register_grp_irqs(struct gfar_priv_grp *grp)
  1403. {
  1404. struct gfar_private *priv = grp->priv;
  1405. struct net_device *dev = priv->ndev;
  1406. int err;
  1407. /* If the device has multiple interrupts, register for
  1408. * them. Otherwise, only register for the one */
  1409. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1410. /* Install our interrupt handlers for Error,
  1411. * Transmit, and Receive */
  1412. if ((err = request_irq(grp->interruptError, gfar_error, 0,
  1413. grp->int_name_er,grp)) < 0) {
  1414. if (netif_msg_intr(priv))
  1415. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  1416. dev->name, grp->interruptError);
  1417. goto err_irq_fail;
  1418. }
  1419. if ((err = request_irq(grp->interruptTransmit, gfar_transmit,
  1420. 0, grp->int_name_tx, grp)) < 0) {
  1421. if (netif_msg_intr(priv))
  1422. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  1423. dev->name, grp->interruptTransmit);
  1424. goto tx_irq_fail;
  1425. }
  1426. if ((err = request_irq(grp->interruptReceive, gfar_receive, 0,
  1427. grp->int_name_rx, grp)) < 0) {
  1428. if (netif_msg_intr(priv))
  1429. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  1430. dev->name, grp->interruptReceive);
  1431. goto rx_irq_fail;
  1432. }
  1433. } else {
  1434. if ((err = request_irq(grp->interruptTransmit, gfar_interrupt, 0,
  1435. grp->int_name_tx, grp)) < 0) {
  1436. if (netif_msg_intr(priv))
  1437. printk(KERN_ERR "%s: Can't get IRQ %d\n",
  1438. dev->name, grp->interruptTransmit);
  1439. goto err_irq_fail;
  1440. }
  1441. }
  1442. return 0;
  1443. rx_irq_fail:
  1444. free_irq(grp->interruptTransmit, grp);
  1445. tx_irq_fail:
  1446. free_irq(grp->interruptError, grp);
  1447. err_irq_fail:
  1448. return err;
  1449. }
  1450. /* Bring the controller up and running */
  1451. int startup_gfar(struct net_device *ndev)
  1452. {
  1453. struct gfar_private *priv = netdev_priv(ndev);
  1454. struct gfar __iomem *regs = NULL;
  1455. int err, i, j;
  1456. for (i = 0; i < priv->num_grps; i++) {
  1457. regs= priv->gfargrp[i].regs;
  1458. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  1459. }
  1460. regs= priv->gfargrp[0].regs;
  1461. err = gfar_alloc_skb_resources(ndev);
  1462. if (err)
  1463. return err;
  1464. gfar_init_mac(ndev);
  1465. for (i = 0; i < priv->num_grps; i++) {
  1466. err = register_grp_irqs(&priv->gfargrp[i]);
  1467. if (err) {
  1468. for (j = 0; j < i; j++)
  1469. free_grp_irqs(&priv->gfargrp[j]);
  1470. goto irq_fail;
  1471. }
  1472. }
  1473. /* Start the controller */
  1474. gfar_start(ndev);
  1475. phy_start(priv->phydev);
  1476. gfar_configure_coalescing(priv, 0xFF, 0xFF);
  1477. return 0;
  1478. irq_fail:
  1479. free_skb_resources(priv);
  1480. return err;
  1481. }
  1482. /* Called when something needs to use the ethernet device */
  1483. /* Returns 0 for success. */
  1484. static int gfar_enet_open(struct net_device *dev)
  1485. {
  1486. struct gfar_private *priv = netdev_priv(dev);
  1487. int err;
  1488. enable_napi(priv);
  1489. skb_queue_head_init(&priv->rx_recycle);
  1490. /* Initialize a bunch of registers */
  1491. init_registers(dev);
  1492. gfar_set_mac_address(dev);
  1493. err = init_phy(dev);
  1494. if (err) {
  1495. disable_napi(priv);
  1496. return err;
  1497. }
  1498. err = startup_gfar(dev);
  1499. if (err) {
  1500. disable_napi(priv);
  1501. return err;
  1502. }
  1503. netif_tx_start_all_queues(dev);
  1504. device_set_wakeup_enable(&dev->dev, priv->wol_en);
  1505. return err;
  1506. }
  1507. static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
  1508. {
  1509. struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
  1510. memset(fcb, 0, GMAC_FCB_LEN);
  1511. return fcb;
  1512. }
  1513. static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb)
  1514. {
  1515. u8 flags = 0;
  1516. /* If we're here, it's a IP packet with a TCP or UDP
  1517. * payload. We set it to checksum, using a pseudo-header
  1518. * we provide
  1519. */
  1520. flags = TXFCB_DEFAULT;
  1521. /* Tell the controller what the protocol is */
  1522. /* And provide the already calculated phcs */
  1523. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  1524. flags |= TXFCB_UDP;
  1525. fcb->phcs = udp_hdr(skb)->check;
  1526. } else
  1527. fcb->phcs = tcp_hdr(skb)->check;
  1528. /* l3os is the distance between the start of the
  1529. * frame (skb->data) and the start of the IP hdr.
  1530. * l4os is the distance between the start of the
  1531. * l3 hdr and the l4 hdr */
  1532. fcb->l3os = (u16)(skb_network_offset(skb) - GMAC_FCB_LEN);
  1533. fcb->l4os = skb_network_header_len(skb);
  1534. fcb->flags = flags;
  1535. }
  1536. void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
  1537. {
  1538. fcb->flags |= TXFCB_VLN;
  1539. fcb->vlctl = vlan_tx_tag_get(skb);
  1540. }
  1541. static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
  1542. struct txbd8 *base, int ring_size)
  1543. {
  1544. struct txbd8 *new_bd = bdp + stride;
  1545. return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
  1546. }
  1547. static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
  1548. int ring_size)
  1549. {
  1550. return skip_txbd(bdp, 1, base, ring_size);
  1551. }
  1552. /* This is called by the kernel when a frame is ready for transmission. */
  1553. /* It is pointed to by the dev->hard_start_xmit function pointer */
  1554. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1555. {
  1556. struct gfar_private *priv = netdev_priv(dev);
  1557. struct gfar_priv_tx_q *tx_queue = NULL;
  1558. struct netdev_queue *txq;
  1559. struct gfar __iomem *regs = NULL;
  1560. struct txfcb *fcb = NULL;
  1561. struct txbd8 *txbdp, *txbdp_start, *base;
  1562. u32 lstatus;
  1563. int i, rq = 0;
  1564. u32 bufaddr;
  1565. unsigned long flags;
  1566. unsigned int nr_frags, length;
  1567. rq = skb->queue_mapping;
  1568. tx_queue = priv->tx_queue[rq];
  1569. txq = netdev_get_tx_queue(dev, rq);
  1570. base = tx_queue->tx_bd_base;
  1571. regs = tx_queue->grp->regs;
  1572. /* make space for additional header when fcb is needed */
  1573. if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
  1574. (priv->vlgrp && vlan_tx_tag_present(skb))) &&
  1575. (skb_headroom(skb) < GMAC_FCB_LEN)) {
  1576. struct sk_buff *skb_new;
  1577. skb_new = skb_realloc_headroom(skb, GMAC_FCB_LEN);
  1578. if (!skb_new) {
  1579. dev->stats.tx_errors++;
  1580. kfree_skb(skb);
  1581. return NETDEV_TX_OK;
  1582. }
  1583. kfree_skb(skb);
  1584. skb = skb_new;
  1585. }
  1586. /* total number of fragments in the SKB */
  1587. nr_frags = skb_shinfo(skb)->nr_frags;
  1588. /* check if there is space to queue this packet */
  1589. if ((nr_frags+1) > tx_queue->num_txbdfree) {
  1590. /* no space, stop the queue */
  1591. netif_tx_stop_queue(txq);
  1592. dev->stats.tx_fifo_errors++;
  1593. return NETDEV_TX_BUSY;
  1594. }
  1595. /* Update transmit stats */
  1596. dev->stats.tx_bytes += skb->len;
  1597. txbdp = txbdp_start = tx_queue->cur_tx;
  1598. if (nr_frags == 0) {
  1599. lstatus = txbdp->lstatus | BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1600. } else {
  1601. /* Place the fragment addresses and lengths into the TxBDs */
  1602. for (i = 0; i < nr_frags; i++) {
  1603. /* Point at the next BD, wrapping as needed */
  1604. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1605. length = skb_shinfo(skb)->frags[i].size;
  1606. lstatus = txbdp->lstatus | length |
  1607. BD_LFLAG(TXBD_READY);
  1608. /* Handle the last BD specially */
  1609. if (i == nr_frags - 1)
  1610. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1611. bufaddr = dma_map_page(&priv->ofdev->dev,
  1612. skb_shinfo(skb)->frags[i].page,
  1613. skb_shinfo(skb)->frags[i].page_offset,
  1614. length,
  1615. DMA_TO_DEVICE);
  1616. /* set the TxBD length and buffer pointer */
  1617. txbdp->bufPtr = bufaddr;
  1618. txbdp->lstatus = lstatus;
  1619. }
  1620. lstatus = txbdp_start->lstatus;
  1621. }
  1622. /* Set up checksumming */
  1623. if (CHECKSUM_PARTIAL == skb->ip_summed) {
  1624. fcb = gfar_add_fcb(skb);
  1625. lstatus |= BD_LFLAG(TXBD_TOE);
  1626. gfar_tx_checksum(skb, fcb);
  1627. }
  1628. if (priv->vlgrp && vlan_tx_tag_present(skb)) {
  1629. if (unlikely(NULL == fcb)) {
  1630. fcb = gfar_add_fcb(skb);
  1631. lstatus |= BD_LFLAG(TXBD_TOE);
  1632. }
  1633. gfar_tx_vlan(skb, fcb);
  1634. }
  1635. /* setup the TxBD length and buffer pointer for the first BD */
  1636. tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
  1637. txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
  1638. skb_headlen(skb), DMA_TO_DEVICE);
  1639. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
  1640. /*
  1641. * We can work in parallel with gfar_clean_tx_ring(), except
  1642. * when modifying num_txbdfree. Note that we didn't grab the lock
  1643. * when we were reading the num_txbdfree and checking for available
  1644. * space, that's because outside of this function it can only grow,
  1645. * and once we've got needed space, it cannot suddenly disappear.
  1646. *
  1647. * The lock also protects us from gfar_error(), which can modify
  1648. * regs->tstat and thus retrigger the transfers, which is why we
  1649. * also must grab the lock before setting ready bit for the first
  1650. * to be transmitted BD.
  1651. */
  1652. spin_lock_irqsave(&tx_queue->txlock, flags);
  1653. /*
  1654. * The powerpc-specific eieio() is used, as wmb() has too strong
  1655. * semantics (it requires synchronization between cacheable and
  1656. * uncacheable mappings, which eieio doesn't provide and which we
  1657. * don't need), thus requiring a more expensive sync instruction. At
  1658. * some point, the set of architecture-independent barrier functions
  1659. * should be expanded to include weaker barriers.
  1660. */
  1661. eieio();
  1662. txbdp_start->lstatus = lstatus;
  1663. /* Update the current skb pointer to the next entry we will use
  1664. * (wrapping if necessary) */
  1665. tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
  1666. TX_RING_MOD_MASK(tx_queue->tx_ring_size);
  1667. tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1668. /* reduce TxBD free count */
  1669. tx_queue->num_txbdfree -= (nr_frags + 1);
  1670. dev->trans_start = jiffies;
  1671. /* If the next BD still needs to be cleaned up, then the bds
  1672. are full. We need to tell the kernel to stop sending us stuff. */
  1673. if (!tx_queue->num_txbdfree) {
  1674. netif_tx_stop_queue(txq);
  1675. dev->stats.tx_fifo_errors++;
  1676. }
  1677. /* Tell the DMA to go go go */
  1678. gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
  1679. /* Unlock priv */
  1680. spin_unlock_irqrestore(&tx_queue->txlock, flags);
  1681. return NETDEV_TX_OK;
  1682. }
  1683. /* Stops the kernel queue, and halts the controller */
  1684. static int gfar_close(struct net_device *dev)
  1685. {
  1686. struct gfar_private *priv = netdev_priv(dev);
  1687. disable_napi(priv);
  1688. skb_queue_purge(&priv->rx_recycle);
  1689. cancel_work_sync(&priv->reset_task);
  1690. stop_gfar(dev);
  1691. /* Disconnect from the PHY */
  1692. phy_disconnect(priv->phydev);
  1693. priv->phydev = NULL;
  1694. netif_tx_stop_all_queues(dev);
  1695. return 0;
  1696. }
  1697. /* Changes the mac address if the controller is not running. */
  1698. static int gfar_set_mac_address(struct net_device *dev)
  1699. {
  1700. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  1701. return 0;
  1702. }
  1703. /* Enables and disables VLAN insertion/extraction */
  1704. static void gfar_vlan_rx_register(struct net_device *dev,
  1705. struct vlan_group *grp)
  1706. {
  1707. struct gfar_private *priv = netdev_priv(dev);
  1708. struct gfar __iomem *regs = NULL;
  1709. unsigned long flags;
  1710. u32 tempval;
  1711. regs = priv->gfargrp[0].regs;
  1712. local_irq_save(flags);
  1713. lock_rx_qs(priv);
  1714. priv->vlgrp = grp;
  1715. if (grp) {
  1716. /* Enable VLAN tag insertion */
  1717. tempval = gfar_read(&regs->tctrl);
  1718. tempval |= TCTRL_VLINS;
  1719. gfar_write(&regs->tctrl, tempval);
  1720. /* Enable VLAN tag extraction */
  1721. tempval = gfar_read(&regs->rctrl);
  1722. tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
  1723. gfar_write(&regs->rctrl, tempval);
  1724. } else {
  1725. /* Disable VLAN tag insertion */
  1726. tempval = gfar_read(&regs->tctrl);
  1727. tempval &= ~TCTRL_VLINS;
  1728. gfar_write(&regs->tctrl, tempval);
  1729. /* Disable VLAN tag extraction */
  1730. tempval = gfar_read(&regs->rctrl);
  1731. tempval &= ~RCTRL_VLEX;
  1732. /* If parse is no longer required, then disable parser */
  1733. if (tempval & RCTRL_REQ_PARSER)
  1734. tempval |= RCTRL_PRSDEP_INIT;
  1735. else
  1736. tempval &= ~RCTRL_PRSDEP_INIT;
  1737. gfar_write(&regs->rctrl, tempval);
  1738. }
  1739. gfar_change_mtu(dev, dev->mtu);
  1740. unlock_rx_qs(priv);
  1741. local_irq_restore(flags);
  1742. }
  1743. static int gfar_change_mtu(struct net_device *dev, int new_mtu)
  1744. {
  1745. int tempsize, tempval;
  1746. struct gfar_private *priv = netdev_priv(dev);
  1747. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1748. int oldsize = priv->rx_buffer_size;
  1749. int frame_size = new_mtu + ETH_HLEN;
  1750. if (priv->vlgrp)
  1751. frame_size += VLAN_HLEN;
  1752. if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
  1753. if (netif_msg_drv(priv))
  1754. printk(KERN_ERR "%s: Invalid MTU setting\n",
  1755. dev->name);
  1756. return -EINVAL;
  1757. }
  1758. if (gfar_uses_fcb(priv))
  1759. frame_size += GMAC_FCB_LEN;
  1760. frame_size += priv->padding;
  1761. tempsize =
  1762. (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
  1763. INCREMENTAL_BUFFER_SIZE;
  1764. /* Only stop and start the controller if it isn't already
  1765. * stopped, and we changed something */
  1766. if ((oldsize != tempsize) && (dev->flags & IFF_UP))
  1767. stop_gfar(dev);
  1768. priv->rx_buffer_size = tempsize;
  1769. dev->mtu = new_mtu;
  1770. gfar_write(&regs->mrblr, priv->rx_buffer_size);
  1771. gfar_write(&regs->maxfrm, priv->rx_buffer_size);
  1772. /* If the mtu is larger than the max size for standard
  1773. * ethernet frames (ie, a jumbo frame), then set maccfg2
  1774. * to allow huge frames, and to check the length */
  1775. tempval = gfar_read(&regs->maccfg2);
  1776. if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE)
  1777. tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
  1778. else
  1779. tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
  1780. gfar_write(&regs->maccfg2, tempval);
  1781. if ((oldsize != tempsize) && (dev->flags & IFF_UP))
  1782. startup_gfar(dev);
  1783. return 0;
  1784. }
  1785. /* gfar_reset_task gets scheduled when a packet has not been
  1786. * transmitted after a set amount of time.
  1787. * For now, assume that clearing out all the structures, and
  1788. * starting over will fix the problem.
  1789. */
  1790. static void gfar_reset_task(struct work_struct *work)
  1791. {
  1792. struct gfar_private *priv = container_of(work, struct gfar_private,
  1793. reset_task);
  1794. struct net_device *dev = priv->ndev;
  1795. if (dev->flags & IFF_UP) {
  1796. netif_tx_stop_all_queues(dev);
  1797. stop_gfar(dev);
  1798. startup_gfar(dev);
  1799. netif_tx_start_all_queues(dev);
  1800. }
  1801. netif_tx_schedule_all(dev);
  1802. }
  1803. static void gfar_timeout(struct net_device *dev)
  1804. {
  1805. struct gfar_private *priv = netdev_priv(dev);
  1806. dev->stats.tx_errors++;
  1807. schedule_work(&priv->reset_task);
  1808. }
  1809. /* Interrupt Handler for Transmit complete */
  1810. static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
  1811. {
  1812. struct net_device *dev = tx_queue->dev;
  1813. struct gfar_private *priv = netdev_priv(dev);
  1814. struct gfar_priv_rx_q *rx_queue = NULL;
  1815. struct txbd8 *bdp;
  1816. struct txbd8 *lbdp = NULL;
  1817. struct txbd8 *base = tx_queue->tx_bd_base;
  1818. struct sk_buff *skb;
  1819. int skb_dirtytx;
  1820. int tx_ring_size = tx_queue->tx_ring_size;
  1821. int frags = 0;
  1822. int i;
  1823. int howmany = 0;
  1824. u32 lstatus;
  1825. rx_queue = priv->rx_queue[tx_queue->qindex];
  1826. bdp = tx_queue->dirty_tx;
  1827. skb_dirtytx = tx_queue->skb_dirtytx;
  1828. while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
  1829. unsigned long flags;
  1830. frags = skb_shinfo(skb)->nr_frags;
  1831. lbdp = skip_txbd(bdp, frags, base, tx_ring_size);
  1832. lstatus = lbdp->lstatus;
  1833. /* Only clean completed frames */
  1834. if ((lstatus & BD_LFLAG(TXBD_READY)) &&
  1835. (lstatus & BD_LENGTH_MASK))
  1836. break;
  1837. dma_unmap_single(&priv->ofdev->dev,
  1838. bdp->bufPtr,
  1839. bdp->length,
  1840. DMA_TO_DEVICE);
  1841. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  1842. bdp = next_txbd(bdp, base, tx_ring_size);
  1843. for (i = 0; i < frags; i++) {
  1844. dma_unmap_page(&priv->ofdev->dev,
  1845. bdp->bufPtr,
  1846. bdp->length,
  1847. DMA_TO_DEVICE);
  1848. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  1849. bdp = next_txbd(bdp, base, tx_ring_size);
  1850. }
  1851. /*
  1852. * If there's room in the queue (limit it to rx_buffer_size)
  1853. * we add this skb back into the pool, if it's the right size
  1854. */
  1855. if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
  1856. skb_recycle_check(skb, priv->rx_buffer_size +
  1857. RXBUF_ALIGNMENT))
  1858. __skb_queue_head(&priv->rx_recycle, skb);
  1859. else
  1860. dev_kfree_skb_any(skb);
  1861. tx_queue->tx_skbuff[skb_dirtytx] = NULL;
  1862. skb_dirtytx = (skb_dirtytx + 1) &
  1863. TX_RING_MOD_MASK(tx_ring_size);
  1864. howmany++;
  1865. spin_lock_irqsave(&tx_queue->txlock, flags);
  1866. tx_queue->num_txbdfree += frags + 1;
  1867. spin_unlock_irqrestore(&tx_queue->txlock, flags);
  1868. }
  1869. /* If we freed a buffer, we can restart transmission, if necessary */
  1870. if (__netif_subqueue_stopped(dev, tx_queue->qindex) && tx_queue->num_txbdfree)
  1871. netif_wake_subqueue(dev, tx_queue->qindex);
  1872. /* Update dirty indicators */
  1873. tx_queue->skb_dirtytx = skb_dirtytx;
  1874. tx_queue->dirty_tx = bdp;
  1875. dev->stats.tx_packets += howmany;
  1876. return howmany;
  1877. }
  1878. static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
  1879. {
  1880. unsigned long flags;
  1881. spin_lock_irqsave(&gfargrp->grplock, flags);
  1882. if (napi_schedule_prep(&gfargrp->napi)) {
  1883. gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
  1884. __napi_schedule(&gfargrp->napi);
  1885. } else {
  1886. /*
  1887. * Clear IEVENT, so interrupts aren't called again
  1888. * because of the packets that have already arrived.
  1889. */
  1890. gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
  1891. }
  1892. spin_unlock_irqrestore(&gfargrp->grplock, flags);
  1893. }
  1894. /* Interrupt Handler for Transmit complete */
  1895. static irqreturn_t gfar_transmit(int irq, void *grp_id)
  1896. {
  1897. gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
  1898. return IRQ_HANDLED;
  1899. }
  1900. static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
  1901. struct sk_buff *skb)
  1902. {
  1903. struct net_device *dev = rx_queue->dev;
  1904. struct gfar_private *priv = netdev_priv(dev);
  1905. dma_addr_t buf;
  1906. buf = dma_map_single(&priv->ofdev->dev, skb->data,
  1907. priv->rx_buffer_size, DMA_FROM_DEVICE);
  1908. gfar_init_rxbdp(rx_queue, bdp, buf);
  1909. }
  1910. struct sk_buff * gfar_new_skb(struct net_device *dev)
  1911. {
  1912. unsigned int alignamount;
  1913. struct gfar_private *priv = netdev_priv(dev);
  1914. struct sk_buff *skb = NULL;
  1915. skb = __skb_dequeue(&priv->rx_recycle);
  1916. if (!skb)
  1917. skb = netdev_alloc_skb(dev,
  1918. priv->rx_buffer_size + RXBUF_ALIGNMENT);
  1919. if (!skb)
  1920. return NULL;
  1921. alignamount = RXBUF_ALIGNMENT -
  1922. (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1));
  1923. /* We need the data buffer to be aligned properly. We will reserve
  1924. * as many bytes as needed to align the data properly
  1925. */
  1926. skb_reserve(skb, alignamount);
  1927. return skb;
  1928. }
  1929. static inline void count_errors(unsigned short status, struct net_device *dev)
  1930. {
  1931. struct gfar_private *priv = netdev_priv(dev);
  1932. struct net_device_stats *stats = &dev->stats;
  1933. struct gfar_extra_stats *estats = &priv->extra_stats;
  1934. /* If the packet was truncated, none of the other errors
  1935. * matter */
  1936. if (status & RXBD_TRUNCATED) {
  1937. stats->rx_length_errors++;
  1938. estats->rx_trunc++;
  1939. return;
  1940. }
  1941. /* Count the errors, if there were any */
  1942. if (status & (RXBD_LARGE | RXBD_SHORT)) {
  1943. stats->rx_length_errors++;
  1944. if (status & RXBD_LARGE)
  1945. estats->rx_large++;
  1946. else
  1947. estats->rx_short++;
  1948. }
  1949. if (status & RXBD_NONOCTET) {
  1950. stats->rx_frame_errors++;
  1951. estats->rx_nonoctet++;
  1952. }
  1953. if (status & RXBD_CRCERR) {
  1954. estats->rx_crcerr++;
  1955. stats->rx_crc_errors++;
  1956. }
  1957. if (status & RXBD_OVERRUN) {
  1958. estats->rx_overrun++;
  1959. stats->rx_crc_errors++;
  1960. }
  1961. }
  1962. irqreturn_t gfar_receive(int irq, void *grp_id)
  1963. {
  1964. gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
  1965. return IRQ_HANDLED;
  1966. }
  1967. static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
  1968. {
  1969. /* If valid headers were found, and valid sums
  1970. * were verified, then we tell the kernel that no
  1971. * checksumming is necessary. Otherwise, it is */
  1972. if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
  1973. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1974. else
  1975. skb->ip_summed = CHECKSUM_NONE;
  1976. }
  1977. /* gfar_process_frame() -- handle one incoming packet if skb
  1978. * isn't NULL. */
  1979. static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
  1980. int amount_pull)
  1981. {
  1982. struct gfar_private *priv = netdev_priv(dev);
  1983. struct rxfcb *fcb = NULL;
  1984. int ret;
  1985. /* fcb is at the beginning if exists */
  1986. fcb = (struct rxfcb *)skb->data;
  1987. /* Remove the FCB from the skb */
  1988. skb_set_queue_mapping(skb, fcb->rq);
  1989. /* Remove the padded bytes, if there are any */
  1990. if (amount_pull)
  1991. skb_pull(skb, amount_pull);
  1992. if (priv->rx_csum_enable)
  1993. gfar_rx_checksum(skb, fcb);
  1994. /* Tell the skb what kind of packet this is */
  1995. skb->protocol = eth_type_trans(skb, dev);
  1996. /* Send the packet up the stack */
  1997. if (unlikely(priv->vlgrp && (fcb->flags & RXFCB_VLN)))
  1998. ret = vlan_hwaccel_receive_skb(skb, priv->vlgrp, fcb->vlctl);
  1999. else
  2000. ret = netif_receive_skb(skb);
  2001. if (NET_RX_DROP == ret)
  2002. priv->extra_stats.kernel_dropped++;
  2003. return 0;
  2004. }
  2005. /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
  2006. * until the budget/quota has been reached. Returns the number
  2007. * of frames handled
  2008. */
  2009. int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
  2010. {
  2011. struct net_device *dev = rx_queue->dev;
  2012. struct rxbd8 *bdp, *base;
  2013. struct sk_buff *skb;
  2014. int pkt_len;
  2015. int amount_pull;
  2016. int howmany = 0;
  2017. struct gfar_private *priv = netdev_priv(dev);
  2018. /* Get the first full descriptor */
  2019. bdp = rx_queue->cur_rx;
  2020. base = rx_queue->rx_bd_base;
  2021. amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0) +
  2022. priv->padding;
  2023. while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
  2024. struct sk_buff *newskb;
  2025. rmb();
  2026. /* Add another skb for the future */
  2027. newskb = gfar_new_skb(dev);
  2028. skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
  2029. dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
  2030. priv->rx_buffer_size, DMA_FROM_DEVICE);
  2031. /* We drop the frame if we failed to allocate a new buffer */
  2032. if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
  2033. bdp->status & RXBD_ERR)) {
  2034. count_errors(bdp->status, dev);
  2035. if (unlikely(!newskb))
  2036. newskb = skb;
  2037. else if (skb) {
  2038. /*
  2039. * We need to reset ->data to what it
  2040. * was before gfar_new_skb() re-aligned
  2041. * it to an RXBUF_ALIGNMENT boundary
  2042. * before we put the skb back on the
  2043. * recycle list.
  2044. */
  2045. skb->data = skb->head + NET_SKB_PAD;
  2046. __skb_queue_head(&priv->rx_recycle, skb);
  2047. }
  2048. } else {
  2049. /* Increment the number of packets */
  2050. dev->stats.rx_packets++;
  2051. howmany++;
  2052. if (likely(skb)) {
  2053. pkt_len = bdp->length - ETH_FCS_LEN;
  2054. /* Remove the FCS from the packet length */
  2055. skb_put(skb, pkt_len);
  2056. dev->stats.rx_bytes += pkt_len;
  2057. gfar_process_frame(dev, skb, amount_pull);
  2058. } else {
  2059. if (netif_msg_rx_err(priv))
  2060. printk(KERN_WARNING
  2061. "%s: Missing skb!\n", dev->name);
  2062. dev->stats.rx_dropped++;
  2063. priv->extra_stats.rx_skbmissing++;
  2064. }
  2065. }
  2066. rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
  2067. /* Setup the new bdp */
  2068. gfar_new_rxbdp(rx_queue, bdp, newskb);
  2069. /* Update to the next pointer */
  2070. bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
  2071. /* update to point at the next skb */
  2072. rx_queue->skb_currx =
  2073. (rx_queue->skb_currx + 1) &
  2074. RX_RING_MOD_MASK(rx_queue->rx_ring_size);
  2075. }
  2076. /* Update the current rxbd pointer to be the next one */
  2077. rx_queue->cur_rx = bdp;
  2078. return howmany;
  2079. }
  2080. static int gfar_poll(struct napi_struct *napi, int budget)
  2081. {
  2082. struct gfar_priv_grp *gfargrp = container_of(napi,
  2083. struct gfar_priv_grp, napi);
  2084. struct gfar_private *priv = gfargrp->priv;
  2085. struct gfar __iomem *regs = gfargrp->regs;
  2086. struct gfar_priv_tx_q *tx_queue = NULL;
  2087. struct gfar_priv_rx_q *rx_queue = NULL;
  2088. int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
  2089. int tx_cleaned = 0, i, left_over_budget = budget;
  2090. unsigned long serviced_queues = 0;
  2091. int num_queues = 0;
  2092. num_queues = gfargrp->num_rx_queues;
  2093. budget_per_queue = budget/num_queues;
  2094. /* Clear IEVENT, so interrupts aren't called again
  2095. * because of the packets that have already arrived */
  2096. gfar_write(&regs->ievent, IEVENT_RTX_MASK);
  2097. while (num_queues && left_over_budget) {
  2098. budget_per_queue = left_over_budget/num_queues;
  2099. left_over_budget = 0;
  2100. for_each_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
  2101. if (test_bit(i, &serviced_queues))
  2102. continue;
  2103. rx_queue = priv->rx_queue[i];
  2104. tx_queue = priv->tx_queue[rx_queue->qindex];
  2105. tx_cleaned += gfar_clean_tx_ring(tx_queue);
  2106. rx_cleaned_per_queue = gfar_clean_rx_ring(rx_queue,
  2107. budget_per_queue);
  2108. rx_cleaned += rx_cleaned_per_queue;
  2109. if(rx_cleaned_per_queue < budget_per_queue) {
  2110. left_over_budget = left_over_budget +
  2111. (budget_per_queue - rx_cleaned_per_queue);
  2112. set_bit(i, &serviced_queues);
  2113. num_queues--;
  2114. }
  2115. }
  2116. }
  2117. if (tx_cleaned)
  2118. return budget;
  2119. if (rx_cleaned < budget) {
  2120. napi_complete(napi);
  2121. /* Clear the halt bit in RSTAT */
  2122. gfar_write(&regs->rstat, gfargrp->rstat);
  2123. gfar_write(&regs->imask, IMASK_DEFAULT);
  2124. /* If we are coalescing interrupts, update the timer */
  2125. /* Otherwise, clear it */
  2126. gfar_configure_coalescing(priv,
  2127. gfargrp->rx_bit_map, gfargrp->tx_bit_map);
  2128. }
  2129. return rx_cleaned;
  2130. }
  2131. #ifdef CONFIG_NET_POLL_CONTROLLER
  2132. /*
  2133. * Polling 'interrupt' - used by things like netconsole to send skbs
  2134. * without having to re-enable interrupts. It's not called while
  2135. * the interrupt routine is executing.
  2136. */
  2137. static void gfar_netpoll(struct net_device *dev)
  2138. {
  2139. struct gfar_private *priv = netdev_priv(dev);
  2140. int i = 0;
  2141. /* If the device has multiple interrupts, run tx/rx */
  2142. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2143. for (i = 0; i < priv->num_grps; i++) {
  2144. disable_irq(priv->gfargrp[i].interruptTransmit);
  2145. disable_irq(priv->gfargrp[i].interruptReceive);
  2146. disable_irq(priv->gfargrp[i].interruptError);
  2147. gfar_interrupt(priv->gfargrp[i].interruptTransmit,
  2148. &priv->gfargrp[i]);
  2149. enable_irq(priv->gfargrp[i].interruptError);
  2150. enable_irq(priv->gfargrp[i].interruptReceive);
  2151. enable_irq(priv->gfargrp[i].interruptTransmit);
  2152. }
  2153. } else {
  2154. for (i = 0; i < priv->num_grps; i++) {
  2155. disable_irq(priv->gfargrp[i].interruptTransmit);
  2156. gfar_interrupt(priv->gfargrp[i].interruptTransmit,
  2157. &priv->gfargrp[i]);
  2158. enable_irq(priv->gfargrp[i].interruptTransmit);
  2159. }
  2160. }
  2161. }
  2162. #endif
  2163. /* The interrupt handler for devices with one interrupt */
  2164. static irqreturn_t gfar_interrupt(int irq, void *grp_id)
  2165. {
  2166. struct gfar_priv_grp *gfargrp = grp_id;
  2167. /* Save ievent for future reference */
  2168. u32 events = gfar_read(&gfargrp->regs->ievent);
  2169. /* Check for reception */
  2170. if (events & IEVENT_RX_MASK)
  2171. gfar_receive(irq, grp_id);
  2172. /* Check for transmit completion */
  2173. if (events & IEVENT_TX_MASK)
  2174. gfar_transmit(irq, grp_id);
  2175. /* Check for errors */
  2176. if (events & IEVENT_ERR_MASK)
  2177. gfar_error(irq, grp_id);
  2178. return IRQ_HANDLED;
  2179. }
  2180. /* Called every time the controller might need to be made
  2181. * aware of new link state. The PHY code conveys this
  2182. * information through variables in the phydev structure, and this
  2183. * function converts those variables into the appropriate
  2184. * register values, and can bring down the device if needed.
  2185. */
  2186. static void adjust_link(struct net_device *dev)
  2187. {
  2188. struct gfar_private *priv = netdev_priv(dev);
  2189. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2190. unsigned long flags;
  2191. struct phy_device *phydev = priv->phydev;
  2192. int new_state = 0;
  2193. local_irq_save(flags);
  2194. lock_tx_qs(priv);
  2195. if (phydev->link) {
  2196. u32 tempval = gfar_read(&regs->maccfg2);
  2197. u32 ecntrl = gfar_read(&regs->ecntrl);
  2198. /* Now we make sure that we can be in full duplex mode.
  2199. * If not, we operate in half-duplex mode. */
  2200. if (phydev->duplex != priv->oldduplex) {
  2201. new_state = 1;
  2202. if (!(phydev->duplex))
  2203. tempval &= ~(MACCFG2_FULL_DUPLEX);
  2204. else
  2205. tempval |= MACCFG2_FULL_DUPLEX;
  2206. priv->oldduplex = phydev->duplex;
  2207. }
  2208. if (phydev->speed != priv->oldspeed) {
  2209. new_state = 1;
  2210. switch (phydev->speed) {
  2211. case 1000:
  2212. tempval =
  2213. ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
  2214. ecntrl &= ~(ECNTRL_R100);
  2215. break;
  2216. case 100:
  2217. case 10:
  2218. tempval =
  2219. ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
  2220. /* Reduced mode distinguishes
  2221. * between 10 and 100 */
  2222. if (phydev->speed == SPEED_100)
  2223. ecntrl |= ECNTRL_R100;
  2224. else
  2225. ecntrl &= ~(ECNTRL_R100);
  2226. break;
  2227. default:
  2228. if (netif_msg_link(priv))
  2229. printk(KERN_WARNING
  2230. "%s: Ack! Speed (%d) is not 10/100/1000!\n",
  2231. dev->name, phydev->speed);
  2232. break;
  2233. }
  2234. priv->oldspeed = phydev->speed;
  2235. }
  2236. gfar_write(&regs->maccfg2, tempval);
  2237. gfar_write(&regs->ecntrl, ecntrl);
  2238. if (!priv->oldlink) {
  2239. new_state = 1;
  2240. priv->oldlink = 1;
  2241. }
  2242. } else if (priv->oldlink) {
  2243. new_state = 1;
  2244. priv->oldlink = 0;
  2245. priv->oldspeed = 0;
  2246. priv->oldduplex = -1;
  2247. }
  2248. if (new_state && netif_msg_link(priv))
  2249. phy_print_status(phydev);
  2250. unlock_tx_qs(priv);
  2251. local_irq_restore(flags);
  2252. }
  2253. /* Update the hash table based on the current list of multicast
  2254. * addresses we subscribe to. Also, change the promiscuity of
  2255. * the device based on the flags (this function is called
  2256. * whenever dev->flags is changed */
  2257. static void gfar_set_multi(struct net_device *dev)
  2258. {
  2259. struct dev_mc_list *mc_ptr;
  2260. struct gfar_private *priv = netdev_priv(dev);
  2261. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2262. u32 tempval;
  2263. if (dev->flags & IFF_PROMISC) {
  2264. /* Set RCTRL to PROM */
  2265. tempval = gfar_read(&regs->rctrl);
  2266. tempval |= RCTRL_PROM;
  2267. gfar_write(&regs->rctrl, tempval);
  2268. } else {
  2269. /* Set RCTRL to not PROM */
  2270. tempval = gfar_read(&regs->rctrl);
  2271. tempval &= ~(RCTRL_PROM);
  2272. gfar_write(&regs->rctrl, tempval);
  2273. }
  2274. if (dev->flags & IFF_ALLMULTI) {
  2275. /* Set the hash to rx all multicast frames */
  2276. gfar_write(&regs->igaddr0, 0xffffffff);
  2277. gfar_write(&regs->igaddr1, 0xffffffff);
  2278. gfar_write(&regs->igaddr2, 0xffffffff);
  2279. gfar_write(&regs->igaddr3, 0xffffffff);
  2280. gfar_write(&regs->igaddr4, 0xffffffff);
  2281. gfar_write(&regs->igaddr5, 0xffffffff);
  2282. gfar_write(&regs->igaddr6, 0xffffffff);
  2283. gfar_write(&regs->igaddr7, 0xffffffff);
  2284. gfar_write(&regs->gaddr0, 0xffffffff);
  2285. gfar_write(&regs->gaddr1, 0xffffffff);
  2286. gfar_write(&regs->gaddr2, 0xffffffff);
  2287. gfar_write(&regs->gaddr3, 0xffffffff);
  2288. gfar_write(&regs->gaddr4, 0xffffffff);
  2289. gfar_write(&regs->gaddr5, 0xffffffff);
  2290. gfar_write(&regs->gaddr6, 0xffffffff);
  2291. gfar_write(&regs->gaddr7, 0xffffffff);
  2292. } else {
  2293. int em_num;
  2294. int idx;
  2295. /* zero out the hash */
  2296. gfar_write(&regs->igaddr0, 0x0);
  2297. gfar_write(&regs->igaddr1, 0x0);
  2298. gfar_write(&regs->igaddr2, 0x0);
  2299. gfar_write(&regs->igaddr3, 0x0);
  2300. gfar_write(&regs->igaddr4, 0x0);
  2301. gfar_write(&regs->igaddr5, 0x0);
  2302. gfar_write(&regs->igaddr6, 0x0);
  2303. gfar_write(&regs->igaddr7, 0x0);
  2304. gfar_write(&regs->gaddr0, 0x0);
  2305. gfar_write(&regs->gaddr1, 0x0);
  2306. gfar_write(&regs->gaddr2, 0x0);
  2307. gfar_write(&regs->gaddr3, 0x0);
  2308. gfar_write(&regs->gaddr4, 0x0);
  2309. gfar_write(&regs->gaddr5, 0x0);
  2310. gfar_write(&regs->gaddr6, 0x0);
  2311. gfar_write(&regs->gaddr7, 0x0);
  2312. /* If we have extended hash tables, we need to
  2313. * clear the exact match registers to prepare for
  2314. * setting them */
  2315. if (priv->extended_hash) {
  2316. em_num = GFAR_EM_NUM + 1;
  2317. gfar_clear_exact_match(dev);
  2318. idx = 1;
  2319. } else {
  2320. idx = 0;
  2321. em_num = 0;
  2322. }
  2323. if (dev->mc_count == 0)
  2324. return;
  2325. /* Parse the list, and set the appropriate bits */
  2326. for(mc_ptr = dev->mc_list; mc_ptr; mc_ptr = mc_ptr->next) {
  2327. if (idx < em_num) {
  2328. gfar_set_mac_for_addr(dev, idx,
  2329. mc_ptr->dmi_addr);
  2330. idx++;
  2331. } else
  2332. gfar_set_hash_for_addr(dev, mc_ptr->dmi_addr);
  2333. }
  2334. }
  2335. return;
  2336. }
  2337. /* Clears each of the exact match registers to zero, so they
  2338. * don't interfere with normal reception */
  2339. static void gfar_clear_exact_match(struct net_device *dev)
  2340. {
  2341. int idx;
  2342. u8 zero_arr[MAC_ADDR_LEN] = {0,0,0,0,0,0};
  2343. for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
  2344. gfar_set_mac_for_addr(dev, idx, (u8 *)zero_arr);
  2345. }
  2346. /* Set the appropriate hash bit for the given addr */
  2347. /* The algorithm works like so:
  2348. * 1) Take the Destination Address (ie the multicast address), and
  2349. * do a CRC on it (little endian), and reverse the bits of the
  2350. * result.
  2351. * 2) Use the 8 most significant bits as a hash into a 256-entry
  2352. * table. The table is controlled through 8 32-bit registers:
  2353. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  2354. * gaddr7. This means that the 3 most significant bits in the
  2355. * hash index which gaddr register to use, and the 5 other bits
  2356. * indicate which bit (assuming an IBM numbering scheme, which
  2357. * for PowerPC (tm) is usually the case) in the register holds
  2358. * the entry. */
  2359. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
  2360. {
  2361. u32 tempval;
  2362. struct gfar_private *priv = netdev_priv(dev);
  2363. u32 result = ether_crc(MAC_ADDR_LEN, addr);
  2364. int width = priv->hash_width;
  2365. u8 whichbit = (result >> (32 - width)) & 0x1f;
  2366. u8 whichreg = result >> (32 - width + 5);
  2367. u32 value = (1 << (31-whichbit));
  2368. tempval = gfar_read(priv->hash_regs[whichreg]);
  2369. tempval |= value;
  2370. gfar_write(priv->hash_regs[whichreg], tempval);
  2371. return;
  2372. }
  2373. /* There are multiple MAC Address register pairs on some controllers
  2374. * This function sets the numth pair to a given address
  2375. */
  2376. static void gfar_set_mac_for_addr(struct net_device *dev, int num, u8 *addr)
  2377. {
  2378. struct gfar_private *priv = netdev_priv(dev);
  2379. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2380. int idx;
  2381. char tmpbuf[MAC_ADDR_LEN];
  2382. u32 tempval;
  2383. u32 __iomem *macptr = &regs->macstnaddr1;
  2384. macptr += num*2;
  2385. /* Now copy it into the mac registers backwards, cuz */
  2386. /* little endian is silly */
  2387. for (idx = 0; idx < MAC_ADDR_LEN; idx++)
  2388. tmpbuf[MAC_ADDR_LEN - 1 - idx] = addr[idx];
  2389. gfar_write(macptr, *((u32 *) (tmpbuf)));
  2390. tempval = *((u32 *) (tmpbuf + 4));
  2391. gfar_write(macptr+1, tempval);
  2392. }
  2393. /* GFAR error interrupt handler */
  2394. static irqreturn_t gfar_error(int irq, void *grp_id)
  2395. {
  2396. struct gfar_priv_grp *gfargrp = grp_id;
  2397. struct gfar __iomem *regs = gfargrp->regs;
  2398. struct gfar_private *priv= gfargrp->priv;
  2399. struct net_device *dev = priv->ndev;
  2400. /* Save ievent for future reference */
  2401. u32 events = gfar_read(&regs->ievent);
  2402. /* Clear IEVENT */
  2403. gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
  2404. /* Magic Packet is not an error. */
  2405. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  2406. (events & IEVENT_MAG))
  2407. events &= ~IEVENT_MAG;
  2408. /* Hmm... */
  2409. if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
  2410. printk(KERN_DEBUG "%s: error interrupt (ievent=0x%08x imask=0x%08x)\n",
  2411. dev->name, events, gfar_read(&regs->imask));
  2412. /* Update the error counters */
  2413. if (events & IEVENT_TXE) {
  2414. dev->stats.tx_errors++;
  2415. if (events & IEVENT_LC)
  2416. dev->stats.tx_window_errors++;
  2417. if (events & IEVENT_CRL)
  2418. dev->stats.tx_aborted_errors++;
  2419. if (events & IEVENT_XFUN) {
  2420. unsigned long flags;
  2421. if (netif_msg_tx_err(priv))
  2422. printk(KERN_DEBUG "%s: TX FIFO underrun, "
  2423. "packet dropped.\n", dev->name);
  2424. dev->stats.tx_dropped++;
  2425. priv->extra_stats.tx_underrun++;
  2426. local_irq_save(flags);
  2427. lock_tx_qs(priv);
  2428. /* Reactivate the Tx Queues */
  2429. gfar_write(&regs->tstat, gfargrp->tstat);
  2430. unlock_tx_qs(priv);
  2431. local_irq_restore(flags);
  2432. }
  2433. if (netif_msg_tx_err(priv))
  2434. printk(KERN_DEBUG "%s: Transmit Error\n", dev->name);
  2435. }
  2436. if (events & IEVENT_BSY) {
  2437. dev->stats.rx_errors++;
  2438. priv->extra_stats.rx_bsy++;
  2439. gfar_receive(irq, grp_id);
  2440. if (netif_msg_rx_err(priv))
  2441. printk(KERN_DEBUG "%s: busy error (rstat: %x)\n",
  2442. dev->name, gfar_read(&regs->rstat));
  2443. }
  2444. if (events & IEVENT_BABR) {
  2445. dev->stats.rx_errors++;
  2446. priv->extra_stats.rx_babr++;
  2447. if (netif_msg_rx_err(priv))
  2448. printk(KERN_DEBUG "%s: babbling RX error\n", dev->name);
  2449. }
  2450. if (events & IEVENT_EBERR) {
  2451. priv->extra_stats.eberr++;
  2452. if (netif_msg_rx_err(priv))
  2453. printk(KERN_DEBUG "%s: bus error\n", dev->name);
  2454. }
  2455. if ((events & IEVENT_RXC) && netif_msg_rx_status(priv))
  2456. printk(KERN_DEBUG "%s: control frame\n", dev->name);
  2457. if (events & IEVENT_BABT) {
  2458. priv->extra_stats.tx_babt++;
  2459. if (netif_msg_tx_err(priv))
  2460. printk(KERN_DEBUG "%s: babbling TX error\n", dev->name);
  2461. }
  2462. return IRQ_HANDLED;
  2463. }
  2464. static struct of_device_id gfar_match[] =
  2465. {
  2466. {
  2467. .type = "network",
  2468. .compatible = "gianfar",
  2469. },
  2470. {
  2471. .compatible = "fsl,etsec2",
  2472. },
  2473. {},
  2474. };
  2475. MODULE_DEVICE_TABLE(of, gfar_match);
  2476. /* Structure for a device driver */
  2477. static struct of_platform_driver gfar_driver = {
  2478. .name = "fsl-gianfar",
  2479. .match_table = gfar_match,
  2480. .probe = gfar_probe,
  2481. .remove = gfar_remove,
  2482. .suspend = gfar_legacy_suspend,
  2483. .resume = gfar_legacy_resume,
  2484. .driver.pm = GFAR_PM_OPS,
  2485. };
  2486. static int __init gfar_init(void)
  2487. {
  2488. return of_register_platform_driver(&gfar_driver);
  2489. }
  2490. static void __exit gfar_exit(void)
  2491. {
  2492. of_unregister_platform_driver(&gfar_driver);
  2493. }
  2494. module_init(gfar_init);
  2495. module_exit(gfar_exit);