gianfar.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572
  1. /* drivers/net/ethernet/freescale/gianfar.c
  2. *
  3. * Gianfar Ethernet Driver
  4. * This driver is designed for the non-CPM ethernet controllers
  5. * on the 85xx and 83xx family of integrated processors
  6. * Based on 8260_io/fcc_enet.c
  7. *
  8. * Author: Andy Fleming
  9. * Maintainer: Kumar Gala
  10. * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
  11. *
  12. * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
  13. * Copyright 2007 MontaVista Software, Inc.
  14. *
  15. * This program is free software; you can redistribute it and/or modify it
  16. * under the terms of the GNU General Public License as published by the
  17. * Free Software Foundation; either version 2 of the License, or (at your
  18. * option) any later version.
  19. *
  20. * Gianfar: AKA Lambda Draconis, "Dragon"
  21. * RA 11 31 24.2
  22. * Dec +69 19 52
  23. * V 3.84
  24. * B-V +1.62
  25. *
  26. * Theory of operation
  27. *
  28. * The driver is initialized through of_device. Configuration information
  29. * is therefore conveyed through an OF-style device tree.
  30. *
  31. * The Gianfar Ethernet Controller uses a ring of buffer
  32. * descriptors. The beginning is indicated by a register
  33. * pointing to the physical address of the start of the ring.
  34. * The end is determined by a "wrap" bit being set in the
  35. * last descriptor of the ring.
  36. *
  37. * When a packet is received, the RXF bit in the
  38. * IEVENT register is set, triggering an interrupt when the
  39. * corresponding bit in the IMASK register is also set (if
  40. * interrupt coalescing is active, then the interrupt may not
  41. * happen immediately, but will wait until either a set number
  42. * of frames or amount of time have passed). In NAPI, the
  43. * interrupt handler will signal there is work to be done, and
  44. * exit. This method will start at the last known empty
  45. * descriptor, and process every subsequent descriptor until there
  46. * are none left with data (NAPI will stop after a set number of
  47. * packets to give time to other tasks, but will eventually
  48. * process all the packets). The data arrives inside a
  49. * pre-allocated skb, and so after the skb is passed up to the
  50. * stack, a new skb must be allocated, and the address field in
  51. * the buffer descriptor must be updated to indicate this new
  52. * skb.
  53. *
  54. * When the kernel requests that a packet be transmitted, the
  55. * driver starts where it left off last time, and points the
  56. * descriptor at the buffer which was passed in. The driver
  57. * then informs the DMA engine that there are packets ready to
  58. * be transmitted. Once the controller is finished transmitting
  59. * the packet, an interrupt may be triggered (under the same
  60. * conditions as for reception, but depending on the TXF bit).
  61. * The driver then cleans up the buffer.
  62. */
  63. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  64. #define DEBUG
  65. #include <linux/kernel.h>
  66. #include <linux/string.h>
  67. #include <linux/errno.h>
  68. #include <linux/unistd.h>
  69. #include <linux/slab.h>
  70. #include <linux/interrupt.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_address.h>
  79. #include <linux/of_irq.h>
  80. #include <linux/of_mdio.h>
  81. #include <linux/of_platform.h>
  82. #include <linux/ip.h>
  83. #include <linux/tcp.h>
  84. #include <linux/udp.h>
  85. #include <linux/in.h>
  86. #include <linux/net_tstamp.h>
  87. #include <asm/io.h>
  88. #ifdef CONFIG_PPC
  89. #include <asm/reg.h>
  90. #include <asm/mpc85xx.h>
  91. #endif
  92. #include <asm/irq.h>
  93. #include <asm/uaccess.h>
  94. #include <linux/module.h>
  95. #include <linux/dma-mapping.h>
  96. #include <linux/crc32.h>
  97. #include <linux/mii.h>
  98. #include <linux/phy.h>
  99. #include <linux/phy_fixed.h>
  100. #include <linux/of.h>
  101. #include <linux/of_net.h>
  102. #include <linux/of_address.h>
  103. #include <linux/of_irq.h>
  104. #include "gianfar.h"
  105. #define TX_TIMEOUT (1*HZ)
  106. const char gfar_driver_version[] = "1.3";
  107. static int gfar_enet_open(struct net_device *dev);
  108. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
  109. static void gfar_reset_task(struct work_struct *work);
  110. static void gfar_timeout(struct net_device *dev);
  111. static int gfar_close(struct net_device *dev);
  112. static struct sk_buff *gfar_new_skb(struct net_device *dev,
  113. dma_addr_t *bufaddr);
  114. static int gfar_set_mac_address(struct net_device *dev);
  115. static int gfar_change_mtu(struct net_device *dev, int new_mtu);
  116. static irqreturn_t gfar_error(int irq, void *dev_id);
  117. static irqreturn_t gfar_transmit(int irq, void *dev_id);
  118. static irqreturn_t gfar_interrupt(int irq, void *dev_id);
  119. static void adjust_link(struct net_device *dev);
  120. static noinline void gfar_update_link_state(struct gfar_private *priv);
  121. static int init_phy(struct net_device *dev);
  122. static int gfar_probe(struct platform_device *ofdev);
  123. static int gfar_remove(struct platform_device *ofdev);
  124. static void free_skb_resources(struct gfar_private *priv);
  125. static void gfar_set_multi(struct net_device *dev);
  126. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
  127. static void gfar_configure_serdes(struct net_device *dev);
  128. static int gfar_poll_rx(struct napi_struct *napi, int budget);
  129. static int gfar_poll_tx(struct napi_struct *napi, int budget);
  130. static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
  131. static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
  132. #ifdef CONFIG_NET_POLL_CONTROLLER
  133. static void gfar_netpoll(struct net_device *dev);
  134. #endif
  135. int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
  136. static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
  137. static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
  138. int amount_pull, struct napi_struct *napi);
  139. static void gfar_halt_nodisable(struct gfar_private *priv);
  140. static void gfar_clear_exact_match(struct net_device *dev);
  141. static void gfar_set_mac_for_addr(struct net_device *dev, int num,
  142. const u8 *addr);
  143. static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  144. MODULE_AUTHOR("Freescale Semiconductor, Inc");
  145. MODULE_DESCRIPTION("Gianfar Ethernet Driver");
  146. MODULE_LICENSE("GPL");
  147. static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
  148. dma_addr_t buf)
  149. {
  150. u32 lstatus;
  151. bdp->bufPtr = buf;
  152. lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
  153. if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
  154. lstatus |= BD_LFLAG(RXBD_WRAP);
  155. gfar_wmb();
  156. bdp->lstatus = lstatus;
  157. }
  158. static int gfar_init_bds(struct net_device *ndev)
  159. {
  160. struct gfar_private *priv = netdev_priv(ndev);
  161. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  162. struct gfar_priv_tx_q *tx_queue = NULL;
  163. struct gfar_priv_rx_q *rx_queue = NULL;
  164. struct txbd8 *txbdp;
  165. struct rxbd8 *rxbdp;
  166. u32 __iomem *rfbptr;
  167. int i, j;
  168. dma_addr_t bufaddr;
  169. for (i = 0; i < priv->num_tx_queues; i++) {
  170. tx_queue = priv->tx_queue[i];
  171. /* Initialize some variables in our dev structure */
  172. tx_queue->num_txbdfree = tx_queue->tx_ring_size;
  173. tx_queue->dirty_tx = tx_queue->tx_bd_base;
  174. tx_queue->cur_tx = tx_queue->tx_bd_base;
  175. tx_queue->skb_curtx = 0;
  176. tx_queue->skb_dirtytx = 0;
  177. /* Initialize Transmit Descriptor Ring */
  178. txbdp = tx_queue->tx_bd_base;
  179. for (j = 0; j < tx_queue->tx_ring_size; j++) {
  180. txbdp->lstatus = 0;
  181. txbdp->bufPtr = 0;
  182. txbdp++;
  183. }
  184. /* Set the last descriptor in the ring to indicate wrap */
  185. txbdp--;
  186. txbdp->status |= TXBD_WRAP;
  187. }
  188. rfbptr = &regs->rfbptr0;
  189. for (i = 0; i < priv->num_rx_queues; i++) {
  190. rx_queue = priv->rx_queue[i];
  191. rx_queue->cur_rx = rx_queue->rx_bd_base;
  192. rx_queue->skb_currx = 0;
  193. rxbdp = rx_queue->rx_bd_base;
  194. for (j = 0; j < rx_queue->rx_ring_size; j++) {
  195. struct sk_buff *skb = rx_queue->rx_skbuff[j];
  196. if (skb) {
  197. bufaddr = rxbdp->bufPtr;
  198. } else {
  199. skb = gfar_new_skb(ndev, &bufaddr);
  200. if (!skb) {
  201. netdev_err(ndev, "Can't allocate RX buffers\n");
  202. return -ENOMEM;
  203. }
  204. rx_queue->rx_skbuff[j] = skb;
  205. }
  206. gfar_init_rxbdp(rx_queue, rxbdp, bufaddr);
  207. rxbdp++;
  208. }
  209. rx_queue->rfbptr = rfbptr;
  210. rfbptr += 2;
  211. }
  212. return 0;
  213. }
  214. static int gfar_alloc_skb_resources(struct net_device *ndev)
  215. {
  216. void *vaddr;
  217. dma_addr_t addr;
  218. int i, j, k;
  219. struct gfar_private *priv = netdev_priv(ndev);
  220. struct device *dev = priv->dev;
  221. struct gfar_priv_tx_q *tx_queue = NULL;
  222. struct gfar_priv_rx_q *rx_queue = NULL;
  223. priv->total_tx_ring_size = 0;
  224. for (i = 0; i < priv->num_tx_queues; i++)
  225. priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
  226. priv->total_rx_ring_size = 0;
  227. for (i = 0; i < priv->num_rx_queues; i++)
  228. priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
  229. /* Allocate memory for the buffer descriptors */
  230. vaddr = dma_alloc_coherent(dev,
  231. (priv->total_tx_ring_size *
  232. sizeof(struct txbd8)) +
  233. (priv->total_rx_ring_size *
  234. sizeof(struct rxbd8)),
  235. &addr, GFP_KERNEL);
  236. if (!vaddr)
  237. return -ENOMEM;
  238. for (i = 0; i < priv->num_tx_queues; i++) {
  239. tx_queue = priv->tx_queue[i];
  240. tx_queue->tx_bd_base = vaddr;
  241. tx_queue->tx_bd_dma_base = addr;
  242. tx_queue->dev = ndev;
  243. /* enet DMA only understands physical addresses */
  244. addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
  245. vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
  246. }
  247. /* Start the rx descriptor ring where the tx ring leaves off */
  248. for (i = 0; i < priv->num_rx_queues; i++) {
  249. rx_queue = priv->rx_queue[i];
  250. rx_queue->rx_bd_base = vaddr;
  251. rx_queue->rx_bd_dma_base = addr;
  252. rx_queue->dev = ndev;
  253. addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
  254. vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
  255. }
  256. /* Setup the skbuff rings */
  257. for (i = 0; i < priv->num_tx_queues; i++) {
  258. tx_queue = priv->tx_queue[i];
  259. tx_queue->tx_skbuff =
  260. kmalloc_array(tx_queue->tx_ring_size,
  261. sizeof(*tx_queue->tx_skbuff),
  262. GFP_KERNEL);
  263. if (!tx_queue->tx_skbuff)
  264. goto cleanup;
  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 =
  271. kmalloc_array(rx_queue->rx_ring_size,
  272. sizeof(*rx_queue->rx_skbuff),
  273. GFP_KERNEL);
  274. if (!rx_queue->rx_skbuff)
  275. goto cleanup;
  276. for (j = 0; j < rx_queue->rx_ring_size; j++)
  277. rx_queue->rx_skbuff[j] = NULL;
  278. }
  279. if (gfar_init_bds(ndev))
  280. goto cleanup;
  281. return 0;
  282. cleanup:
  283. free_skb_resources(priv);
  284. return -ENOMEM;
  285. }
  286. static void gfar_init_tx_rx_base(struct gfar_private *priv)
  287. {
  288. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  289. u32 __iomem *baddr;
  290. int i;
  291. baddr = &regs->tbase0;
  292. for (i = 0; i < priv->num_tx_queues; i++) {
  293. gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
  294. baddr += 2;
  295. }
  296. baddr = &regs->rbase0;
  297. for (i = 0; i < priv->num_rx_queues; i++) {
  298. gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
  299. baddr += 2;
  300. }
  301. }
  302. static void gfar_init_rqprm(struct gfar_private *priv)
  303. {
  304. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  305. u32 __iomem *baddr;
  306. int i;
  307. baddr = &regs->rqprm0;
  308. for (i = 0; i < priv->num_rx_queues; i++) {
  309. gfar_write(baddr, priv->rx_queue[i]->rx_ring_size |
  310. (DEFAULT_RX_LFC_THR << FBTHR_SHIFT));
  311. baddr++;
  312. }
  313. }
  314. static void gfar_rx_buff_size_config(struct gfar_private *priv)
  315. {
  316. int frame_size = priv->ndev->mtu + ETH_HLEN + ETH_FCS_LEN;
  317. /* set this when rx hw offload (TOE) functions are being used */
  318. priv->uses_rxfcb = 0;
  319. if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
  320. priv->uses_rxfcb = 1;
  321. if (priv->hwts_rx_en)
  322. priv->uses_rxfcb = 1;
  323. if (priv->uses_rxfcb)
  324. frame_size += GMAC_FCB_LEN;
  325. frame_size += priv->padding;
  326. frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
  327. INCREMENTAL_BUFFER_SIZE;
  328. priv->rx_buffer_size = frame_size;
  329. }
  330. static void gfar_mac_rx_config(struct gfar_private *priv)
  331. {
  332. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  333. u32 rctrl = 0;
  334. if (priv->rx_filer_enable) {
  335. rctrl |= RCTRL_FILREN;
  336. /* Program the RIR0 reg with the required distribution */
  337. if (priv->poll_mode == GFAR_SQ_POLLING)
  338. gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
  339. else /* GFAR_MQ_POLLING */
  340. gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
  341. }
  342. /* Restore PROMISC mode */
  343. if (priv->ndev->flags & IFF_PROMISC)
  344. rctrl |= RCTRL_PROM;
  345. if (priv->ndev->features & NETIF_F_RXCSUM)
  346. rctrl |= RCTRL_CHECKSUMMING;
  347. if (priv->extended_hash)
  348. rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
  349. if (priv->padding) {
  350. rctrl &= ~RCTRL_PAL_MASK;
  351. rctrl |= RCTRL_PADDING(priv->padding);
  352. }
  353. /* Enable HW time stamping if requested from user space */
  354. if (priv->hwts_rx_en)
  355. rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
  356. if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
  357. rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
  358. /* Clear the LFC bit */
  359. gfar_write(&regs->rctrl, rctrl);
  360. /* Init flow control threshold values */
  361. gfar_init_rqprm(priv);
  362. gfar_write(&regs->ptv, DEFAULT_LFC_PTVVAL);
  363. rctrl |= RCTRL_LFC;
  364. /* Init rctrl based on our settings */
  365. gfar_write(&regs->rctrl, rctrl);
  366. }
  367. static void gfar_mac_tx_config(struct gfar_private *priv)
  368. {
  369. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  370. u32 tctrl = 0;
  371. if (priv->ndev->features & NETIF_F_IP_CSUM)
  372. tctrl |= TCTRL_INIT_CSUM;
  373. if (priv->prio_sched_en)
  374. tctrl |= TCTRL_TXSCHED_PRIO;
  375. else {
  376. tctrl |= TCTRL_TXSCHED_WRRS;
  377. gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
  378. gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
  379. }
  380. if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
  381. tctrl |= TCTRL_VLINS;
  382. gfar_write(&regs->tctrl, tctrl);
  383. }
  384. static void gfar_configure_coalescing(struct gfar_private *priv,
  385. unsigned long tx_mask, unsigned long rx_mask)
  386. {
  387. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  388. u32 __iomem *baddr;
  389. if (priv->mode == MQ_MG_MODE) {
  390. int i = 0;
  391. baddr = &regs->txic0;
  392. for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
  393. gfar_write(baddr + i, 0);
  394. if (likely(priv->tx_queue[i]->txcoalescing))
  395. gfar_write(baddr + i, priv->tx_queue[i]->txic);
  396. }
  397. baddr = &regs->rxic0;
  398. for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
  399. gfar_write(baddr + i, 0);
  400. if (likely(priv->rx_queue[i]->rxcoalescing))
  401. gfar_write(baddr + i, priv->rx_queue[i]->rxic);
  402. }
  403. } else {
  404. /* Backward compatible case -- even if we enable
  405. * multiple queues, there's only single reg to program
  406. */
  407. gfar_write(&regs->txic, 0);
  408. if (likely(priv->tx_queue[0]->txcoalescing))
  409. gfar_write(&regs->txic, priv->tx_queue[0]->txic);
  410. gfar_write(&regs->rxic, 0);
  411. if (unlikely(priv->rx_queue[0]->rxcoalescing))
  412. gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
  413. }
  414. }
  415. void gfar_configure_coalescing_all(struct gfar_private *priv)
  416. {
  417. gfar_configure_coalescing(priv, 0xFF, 0xFF);
  418. }
  419. static struct net_device_stats *gfar_get_stats(struct net_device *dev)
  420. {
  421. struct gfar_private *priv = netdev_priv(dev);
  422. unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
  423. unsigned long tx_packets = 0, tx_bytes = 0;
  424. int i;
  425. for (i = 0; i < priv->num_rx_queues; i++) {
  426. rx_packets += priv->rx_queue[i]->stats.rx_packets;
  427. rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
  428. rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
  429. }
  430. dev->stats.rx_packets = rx_packets;
  431. dev->stats.rx_bytes = rx_bytes;
  432. dev->stats.rx_dropped = rx_dropped;
  433. for (i = 0; i < priv->num_tx_queues; i++) {
  434. tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
  435. tx_packets += priv->tx_queue[i]->stats.tx_packets;
  436. }
  437. dev->stats.tx_bytes = tx_bytes;
  438. dev->stats.tx_packets = tx_packets;
  439. return &dev->stats;
  440. }
  441. static const struct net_device_ops gfar_netdev_ops = {
  442. .ndo_open = gfar_enet_open,
  443. .ndo_start_xmit = gfar_start_xmit,
  444. .ndo_stop = gfar_close,
  445. .ndo_change_mtu = gfar_change_mtu,
  446. .ndo_set_features = gfar_set_features,
  447. .ndo_set_rx_mode = gfar_set_multi,
  448. .ndo_tx_timeout = gfar_timeout,
  449. .ndo_do_ioctl = gfar_ioctl,
  450. .ndo_get_stats = gfar_get_stats,
  451. .ndo_set_mac_address = eth_mac_addr,
  452. .ndo_validate_addr = eth_validate_addr,
  453. #ifdef CONFIG_NET_POLL_CONTROLLER
  454. .ndo_poll_controller = gfar_netpoll,
  455. #endif
  456. };
  457. static void gfar_ints_disable(struct gfar_private *priv)
  458. {
  459. int i;
  460. for (i = 0; i < priv->num_grps; i++) {
  461. struct gfar __iomem *regs = priv->gfargrp[i].regs;
  462. /* Clear IEVENT */
  463. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  464. /* Initialize IMASK */
  465. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  466. }
  467. }
  468. static void gfar_ints_enable(struct gfar_private *priv)
  469. {
  470. int i;
  471. for (i = 0; i < priv->num_grps; i++) {
  472. struct gfar __iomem *regs = priv->gfargrp[i].regs;
  473. /* Unmask the interrupts we look for */
  474. gfar_write(&regs->imask, IMASK_DEFAULT);
  475. }
  476. }
  477. static void lock_tx_qs(struct gfar_private *priv)
  478. {
  479. int i;
  480. for (i = 0; i < priv->num_tx_queues; i++)
  481. spin_lock(&priv->tx_queue[i]->txlock);
  482. }
  483. static void unlock_tx_qs(struct gfar_private *priv)
  484. {
  485. int i;
  486. for (i = 0; i < priv->num_tx_queues; i++)
  487. spin_unlock(&priv->tx_queue[i]->txlock);
  488. }
  489. static int gfar_alloc_tx_queues(struct gfar_private *priv)
  490. {
  491. int i;
  492. for (i = 0; i < priv->num_tx_queues; i++) {
  493. priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
  494. GFP_KERNEL);
  495. if (!priv->tx_queue[i])
  496. return -ENOMEM;
  497. priv->tx_queue[i]->tx_skbuff = NULL;
  498. priv->tx_queue[i]->qindex = i;
  499. priv->tx_queue[i]->dev = priv->ndev;
  500. spin_lock_init(&(priv->tx_queue[i]->txlock));
  501. }
  502. return 0;
  503. }
  504. static int gfar_alloc_rx_queues(struct gfar_private *priv)
  505. {
  506. int i;
  507. for (i = 0; i < priv->num_rx_queues; i++) {
  508. priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
  509. GFP_KERNEL);
  510. if (!priv->rx_queue[i])
  511. return -ENOMEM;
  512. priv->rx_queue[i]->rx_skbuff = NULL;
  513. priv->rx_queue[i]->qindex = i;
  514. priv->rx_queue[i]->dev = priv->ndev;
  515. }
  516. return 0;
  517. }
  518. static void gfar_free_tx_queues(struct gfar_private *priv)
  519. {
  520. int i;
  521. for (i = 0; i < priv->num_tx_queues; i++)
  522. kfree(priv->tx_queue[i]);
  523. }
  524. static void gfar_free_rx_queues(struct gfar_private *priv)
  525. {
  526. int i;
  527. for (i = 0; i < priv->num_rx_queues; i++)
  528. kfree(priv->rx_queue[i]);
  529. }
  530. static void unmap_group_regs(struct gfar_private *priv)
  531. {
  532. int i;
  533. for (i = 0; i < MAXGROUPS; i++)
  534. if (priv->gfargrp[i].regs)
  535. iounmap(priv->gfargrp[i].regs);
  536. }
  537. static void free_gfar_dev(struct gfar_private *priv)
  538. {
  539. int i, j;
  540. for (i = 0; i < priv->num_grps; i++)
  541. for (j = 0; j < GFAR_NUM_IRQS; j++) {
  542. kfree(priv->gfargrp[i].irqinfo[j]);
  543. priv->gfargrp[i].irqinfo[j] = NULL;
  544. }
  545. free_netdev(priv->ndev);
  546. }
  547. static void disable_napi(struct gfar_private *priv)
  548. {
  549. int i;
  550. for (i = 0; i < priv->num_grps; i++) {
  551. napi_disable(&priv->gfargrp[i].napi_rx);
  552. napi_disable(&priv->gfargrp[i].napi_tx);
  553. }
  554. }
  555. static void enable_napi(struct gfar_private *priv)
  556. {
  557. int i;
  558. for (i = 0; i < priv->num_grps; i++) {
  559. napi_enable(&priv->gfargrp[i].napi_rx);
  560. napi_enable(&priv->gfargrp[i].napi_tx);
  561. }
  562. }
  563. static int gfar_parse_group(struct device_node *np,
  564. struct gfar_private *priv, const char *model)
  565. {
  566. struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
  567. int i;
  568. for (i = 0; i < GFAR_NUM_IRQS; i++) {
  569. grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
  570. GFP_KERNEL);
  571. if (!grp->irqinfo[i])
  572. return -ENOMEM;
  573. }
  574. grp->regs = of_iomap(np, 0);
  575. if (!grp->regs)
  576. return -ENOMEM;
  577. gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
  578. /* If we aren't the FEC we have multiple interrupts */
  579. if (model && strcasecmp(model, "FEC")) {
  580. gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
  581. gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
  582. if (gfar_irq(grp, TX)->irq == NO_IRQ ||
  583. gfar_irq(grp, RX)->irq == NO_IRQ ||
  584. gfar_irq(grp, ER)->irq == NO_IRQ)
  585. return -EINVAL;
  586. }
  587. grp->priv = priv;
  588. spin_lock_init(&grp->grplock);
  589. if (priv->mode == MQ_MG_MODE) {
  590. u32 *rxq_mask, *txq_mask;
  591. rxq_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
  592. txq_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
  593. if (priv->poll_mode == GFAR_SQ_POLLING) {
  594. /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
  595. grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
  596. grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
  597. } else { /* GFAR_MQ_POLLING */
  598. grp->rx_bit_map = rxq_mask ?
  599. *rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
  600. grp->tx_bit_map = txq_mask ?
  601. *txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
  602. }
  603. } else {
  604. grp->rx_bit_map = 0xFF;
  605. grp->tx_bit_map = 0xFF;
  606. }
  607. /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
  608. * right to left, so we need to revert the 8 bits to get the q index
  609. */
  610. grp->rx_bit_map = bitrev8(grp->rx_bit_map);
  611. grp->tx_bit_map = bitrev8(grp->tx_bit_map);
  612. /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
  613. * also assign queues to groups
  614. */
  615. for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
  616. if (!grp->rx_queue)
  617. grp->rx_queue = priv->rx_queue[i];
  618. grp->num_rx_queues++;
  619. grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
  620. priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
  621. priv->rx_queue[i]->grp = grp;
  622. }
  623. for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
  624. if (!grp->tx_queue)
  625. grp->tx_queue = priv->tx_queue[i];
  626. grp->num_tx_queues++;
  627. grp->tstat |= (TSTAT_CLEAR_THALT >> i);
  628. priv->tqueue |= (TQUEUE_EN0 >> i);
  629. priv->tx_queue[i]->grp = grp;
  630. }
  631. priv->num_grps++;
  632. return 0;
  633. }
  634. static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
  635. {
  636. const char *model;
  637. const char *ctype;
  638. const void *mac_addr;
  639. int err = 0, i;
  640. struct net_device *dev = NULL;
  641. struct gfar_private *priv = NULL;
  642. struct device_node *np = ofdev->dev.of_node;
  643. struct device_node *child = NULL;
  644. const u32 *stash;
  645. const u32 *stash_len;
  646. const u32 *stash_idx;
  647. unsigned int num_tx_qs, num_rx_qs;
  648. u32 *tx_queues, *rx_queues;
  649. unsigned short mode, poll_mode;
  650. if (!np)
  651. return -ENODEV;
  652. if (of_device_is_compatible(np, "fsl,etsec2")) {
  653. mode = MQ_MG_MODE;
  654. poll_mode = GFAR_SQ_POLLING;
  655. } else {
  656. mode = SQ_SG_MODE;
  657. poll_mode = GFAR_SQ_POLLING;
  658. }
  659. /* parse the num of HW tx and rx queues */
  660. tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
  661. rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
  662. if (mode == SQ_SG_MODE) {
  663. num_tx_qs = 1;
  664. num_rx_qs = 1;
  665. } else { /* MQ_MG_MODE */
  666. /* get the actual number of supported groups */
  667. unsigned int num_grps = of_get_available_child_count(np);
  668. if (num_grps == 0 || num_grps > MAXGROUPS) {
  669. dev_err(&ofdev->dev, "Invalid # of int groups(%d)\n",
  670. num_grps);
  671. pr_err("Cannot do alloc_etherdev, aborting\n");
  672. return -EINVAL;
  673. }
  674. if (poll_mode == GFAR_SQ_POLLING) {
  675. num_tx_qs = num_grps; /* one txq per int group */
  676. num_rx_qs = num_grps; /* one rxq per int group */
  677. } else { /* GFAR_MQ_POLLING */
  678. num_tx_qs = tx_queues ? *tx_queues : 1;
  679. num_rx_qs = rx_queues ? *rx_queues : 1;
  680. }
  681. }
  682. if (num_tx_qs > MAX_TX_QS) {
  683. pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
  684. num_tx_qs, MAX_TX_QS);
  685. pr_err("Cannot do alloc_etherdev, aborting\n");
  686. return -EINVAL;
  687. }
  688. if (num_rx_qs > MAX_RX_QS) {
  689. pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
  690. num_rx_qs, MAX_RX_QS);
  691. pr_err("Cannot do alloc_etherdev, aborting\n");
  692. return -EINVAL;
  693. }
  694. *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
  695. dev = *pdev;
  696. if (NULL == dev)
  697. return -ENOMEM;
  698. priv = netdev_priv(dev);
  699. priv->ndev = dev;
  700. priv->mode = mode;
  701. priv->poll_mode = poll_mode;
  702. priv->num_tx_queues = num_tx_qs;
  703. netif_set_real_num_rx_queues(dev, num_rx_qs);
  704. priv->num_rx_queues = num_rx_qs;
  705. err = gfar_alloc_tx_queues(priv);
  706. if (err)
  707. goto tx_alloc_failed;
  708. err = gfar_alloc_rx_queues(priv);
  709. if (err)
  710. goto rx_alloc_failed;
  711. /* Init Rx queue filer rule set linked list */
  712. INIT_LIST_HEAD(&priv->rx_list.list);
  713. priv->rx_list.count = 0;
  714. mutex_init(&priv->rx_queue_access);
  715. model = of_get_property(np, "model", NULL);
  716. for (i = 0; i < MAXGROUPS; i++)
  717. priv->gfargrp[i].regs = NULL;
  718. /* Parse and initialize group specific information */
  719. if (priv->mode == MQ_MG_MODE) {
  720. for_each_child_of_node(np, child) {
  721. err = gfar_parse_group(child, priv, model);
  722. if (err)
  723. goto err_grp_init;
  724. }
  725. } else { /* SQ_SG_MODE */
  726. err = gfar_parse_group(np, priv, model);
  727. if (err)
  728. goto err_grp_init;
  729. }
  730. stash = of_get_property(np, "bd-stash", NULL);
  731. if (stash) {
  732. priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
  733. priv->bd_stash_en = 1;
  734. }
  735. stash_len = of_get_property(np, "rx-stash-len", NULL);
  736. if (stash_len)
  737. priv->rx_stash_size = *stash_len;
  738. stash_idx = of_get_property(np, "rx-stash-idx", NULL);
  739. if (stash_idx)
  740. priv->rx_stash_index = *stash_idx;
  741. if (stash_len || stash_idx)
  742. priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
  743. mac_addr = of_get_mac_address(np);
  744. if (mac_addr)
  745. memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
  746. if (model && !strcasecmp(model, "TSEC"))
  747. priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
  748. FSL_GIANFAR_DEV_HAS_COALESCE |
  749. FSL_GIANFAR_DEV_HAS_RMON |
  750. FSL_GIANFAR_DEV_HAS_MULTI_INTR;
  751. if (model && !strcasecmp(model, "eTSEC"))
  752. priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
  753. FSL_GIANFAR_DEV_HAS_COALESCE |
  754. FSL_GIANFAR_DEV_HAS_RMON |
  755. FSL_GIANFAR_DEV_HAS_MULTI_INTR |
  756. FSL_GIANFAR_DEV_HAS_CSUM |
  757. FSL_GIANFAR_DEV_HAS_VLAN |
  758. FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
  759. FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
  760. FSL_GIANFAR_DEV_HAS_TIMER;
  761. ctype = of_get_property(np, "phy-connection-type", NULL);
  762. /* We only care about rgmii-id. The rest are autodetected */
  763. if (ctype && !strcmp(ctype, "rgmii-id"))
  764. priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
  765. else
  766. priv->interface = PHY_INTERFACE_MODE_MII;
  767. if (of_get_property(np, "fsl,magic-packet", NULL))
  768. priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
  769. priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
  770. /* In the case of a fixed PHY, the DT node associated
  771. * to the PHY is the Ethernet MAC DT node.
  772. */
  773. if (!priv->phy_node && of_phy_is_fixed_link(np)) {
  774. err = of_phy_register_fixed_link(np);
  775. if (err)
  776. goto err_grp_init;
  777. priv->phy_node = of_node_get(np);
  778. }
  779. /* Find the TBI PHY. If it's not there, we don't support SGMII */
  780. priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
  781. return 0;
  782. err_grp_init:
  783. unmap_group_regs(priv);
  784. rx_alloc_failed:
  785. gfar_free_rx_queues(priv);
  786. tx_alloc_failed:
  787. gfar_free_tx_queues(priv);
  788. free_gfar_dev(priv);
  789. return err;
  790. }
  791. static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
  792. {
  793. struct hwtstamp_config config;
  794. struct gfar_private *priv = netdev_priv(netdev);
  795. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  796. return -EFAULT;
  797. /* reserved for future extensions */
  798. if (config.flags)
  799. return -EINVAL;
  800. switch (config.tx_type) {
  801. case HWTSTAMP_TX_OFF:
  802. priv->hwts_tx_en = 0;
  803. break;
  804. case HWTSTAMP_TX_ON:
  805. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
  806. return -ERANGE;
  807. priv->hwts_tx_en = 1;
  808. break;
  809. default:
  810. return -ERANGE;
  811. }
  812. switch (config.rx_filter) {
  813. case HWTSTAMP_FILTER_NONE:
  814. if (priv->hwts_rx_en) {
  815. priv->hwts_rx_en = 0;
  816. reset_gfar(netdev);
  817. }
  818. break;
  819. default:
  820. if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
  821. return -ERANGE;
  822. if (!priv->hwts_rx_en) {
  823. priv->hwts_rx_en = 1;
  824. reset_gfar(netdev);
  825. }
  826. config.rx_filter = HWTSTAMP_FILTER_ALL;
  827. break;
  828. }
  829. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  830. -EFAULT : 0;
  831. }
  832. static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
  833. {
  834. struct hwtstamp_config config;
  835. struct gfar_private *priv = netdev_priv(netdev);
  836. config.flags = 0;
  837. config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
  838. config.rx_filter = (priv->hwts_rx_en ?
  839. HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
  840. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  841. -EFAULT : 0;
  842. }
  843. static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  844. {
  845. struct gfar_private *priv = netdev_priv(dev);
  846. if (!netif_running(dev))
  847. return -EINVAL;
  848. if (cmd == SIOCSHWTSTAMP)
  849. return gfar_hwtstamp_set(dev, rq);
  850. if (cmd == SIOCGHWTSTAMP)
  851. return gfar_hwtstamp_get(dev, rq);
  852. if (!priv->phydev)
  853. return -ENODEV;
  854. return phy_mii_ioctl(priv->phydev, rq, cmd);
  855. }
  856. static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
  857. u32 class)
  858. {
  859. u32 rqfpr = FPR_FILER_MASK;
  860. u32 rqfcr = 0x0;
  861. rqfar--;
  862. rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
  863. priv->ftp_rqfpr[rqfar] = rqfpr;
  864. priv->ftp_rqfcr[rqfar] = rqfcr;
  865. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  866. rqfar--;
  867. rqfcr = RQFCR_CMP_NOMATCH;
  868. priv->ftp_rqfpr[rqfar] = rqfpr;
  869. priv->ftp_rqfcr[rqfar] = rqfcr;
  870. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  871. rqfar--;
  872. rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
  873. rqfpr = class;
  874. priv->ftp_rqfcr[rqfar] = rqfcr;
  875. priv->ftp_rqfpr[rqfar] = rqfpr;
  876. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  877. rqfar--;
  878. rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
  879. rqfpr = class;
  880. priv->ftp_rqfcr[rqfar] = rqfcr;
  881. priv->ftp_rqfpr[rqfar] = rqfpr;
  882. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  883. return rqfar;
  884. }
  885. static void gfar_init_filer_table(struct gfar_private *priv)
  886. {
  887. int i = 0x0;
  888. u32 rqfar = MAX_FILER_IDX;
  889. u32 rqfcr = 0x0;
  890. u32 rqfpr = FPR_FILER_MASK;
  891. /* Default rule */
  892. rqfcr = RQFCR_CMP_MATCH;
  893. priv->ftp_rqfcr[rqfar] = rqfcr;
  894. priv->ftp_rqfpr[rqfar] = rqfpr;
  895. gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
  896. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
  897. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
  898. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
  899. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
  900. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
  901. rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
  902. /* cur_filer_idx indicated the first non-masked rule */
  903. priv->cur_filer_idx = rqfar;
  904. /* Rest are masked rules */
  905. rqfcr = RQFCR_CMP_NOMATCH;
  906. for (i = 0; i < rqfar; i++) {
  907. priv->ftp_rqfcr[i] = rqfcr;
  908. priv->ftp_rqfpr[i] = rqfpr;
  909. gfar_write_filer(priv, i, rqfcr, rqfpr);
  910. }
  911. }
  912. #ifdef CONFIG_PPC
  913. static void __gfar_detect_errata_83xx(struct gfar_private *priv)
  914. {
  915. unsigned int pvr = mfspr(SPRN_PVR);
  916. unsigned int svr = mfspr(SPRN_SVR);
  917. unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
  918. unsigned int rev = svr & 0xffff;
  919. /* MPC8313 Rev 2.0 and higher; All MPC837x */
  920. if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
  921. (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
  922. priv->errata |= GFAR_ERRATA_74;
  923. /* MPC8313 and MPC837x all rev */
  924. if ((pvr == 0x80850010 && mod == 0x80b0) ||
  925. (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
  926. priv->errata |= GFAR_ERRATA_76;
  927. /* MPC8313 Rev < 2.0 */
  928. if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
  929. priv->errata |= GFAR_ERRATA_12;
  930. }
  931. static void __gfar_detect_errata_85xx(struct gfar_private *priv)
  932. {
  933. unsigned int svr = mfspr(SPRN_SVR);
  934. if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
  935. priv->errata |= GFAR_ERRATA_12;
  936. if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
  937. ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
  938. priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
  939. }
  940. #endif
  941. static void gfar_detect_errata(struct gfar_private *priv)
  942. {
  943. struct device *dev = &priv->ofdev->dev;
  944. /* no plans to fix */
  945. priv->errata |= GFAR_ERRATA_A002;
  946. #ifdef CONFIG_PPC
  947. if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
  948. __gfar_detect_errata_85xx(priv);
  949. else /* non-mpc85xx parts, i.e. e300 core based */
  950. __gfar_detect_errata_83xx(priv);
  951. #endif
  952. if (priv->errata)
  953. dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
  954. priv->errata);
  955. }
  956. void gfar_mac_reset(struct gfar_private *priv)
  957. {
  958. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  959. u32 tempval;
  960. /* Reset MAC layer */
  961. gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
  962. /* We need to delay at least 3 TX clocks */
  963. udelay(3);
  964. /* the soft reset bit is not self-resetting, so we need to
  965. * clear it before resuming normal operation
  966. */
  967. gfar_write(&regs->maccfg1, 0);
  968. udelay(3);
  969. /* Compute rx_buff_size based on config flags */
  970. gfar_rx_buff_size_config(priv);
  971. /* Initialize the max receive frame/buffer lengths */
  972. gfar_write(&regs->maxfrm, priv->rx_buffer_size);
  973. gfar_write(&regs->mrblr, priv->rx_buffer_size);
  974. /* Initialize the Minimum Frame Length Register */
  975. gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
  976. /* Initialize MACCFG2. */
  977. tempval = MACCFG2_INIT_SETTINGS;
  978. /* If the mtu is larger than the max size for standard
  979. * ethernet frames (ie, a jumbo frame), then set maccfg2
  980. * to allow huge frames, and to check the length
  981. */
  982. if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
  983. gfar_has_errata(priv, GFAR_ERRATA_74))
  984. tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
  985. gfar_write(&regs->maccfg2, tempval);
  986. /* Clear mac addr hash registers */
  987. gfar_write(&regs->igaddr0, 0);
  988. gfar_write(&regs->igaddr1, 0);
  989. gfar_write(&regs->igaddr2, 0);
  990. gfar_write(&regs->igaddr3, 0);
  991. gfar_write(&regs->igaddr4, 0);
  992. gfar_write(&regs->igaddr5, 0);
  993. gfar_write(&regs->igaddr6, 0);
  994. gfar_write(&regs->igaddr7, 0);
  995. gfar_write(&regs->gaddr0, 0);
  996. gfar_write(&regs->gaddr1, 0);
  997. gfar_write(&regs->gaddr2, 0);
  998. gfar_write(&regs->gaddr3, 0);
  999. gfar_write(&regs->gaddr4, 0);
  1000. gfar_write(&regs->gaddr5, 0);
  1001. gfar_write(&regs->gaddr6, 0);
  1002. gfar_write(&regs->gaddr7, 0);
  1003. if (priv->extended_hash)
  1004. gfar_clear_exact_match(priv->ndev);
  1005. gfar_mac_rx_config(priv);
  1006. gfar_mac_tx_config(priv);
  1007. gfar_set_mac_address(priv->ndev);
  1008. gfar_set_multi(priv->ndev);
  1009. /* clear ievent and imask before configuring coalescing */
  1010. gfar_ints_disable(priv);
  1011. /* Configure the coalescing support */
  1012. gfar_configure_coalescing_all(priv);
  1013. }
  1014. static void gfar_hw_init(struct gfar_private *priv)
  1015. {
  1016. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1017. u32 attrs;
  1018. /* Stop the DMA engine now, in case it was running before
  1019. * (The firmware could have used it, and left it running).
  1020. */
  1021. gfar_halt(priv);
  1022. gfar_mac_reset(priv);
  1023. /* Zero out the rmon mib registers if it has them */
  1024. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  1025. memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
  1026. /* Mask off the CAM interrupts */
  1027. gfar_write(&regs->rmon.cam1, 0xffffffff);
  1028. gfar_write(&regs->rmon.cam2, 0xffffffff);
  1029. }
  1030. /* Initialize ECNTRL */
  1031. gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
  1032. /* Set the extraction length and index */
  1033. attrs = ATTRELI_EL(priv->rx_stash_size) |
  1034. ATTRELI_EI(priv->rx_stash_index);
  1035. gfar_write(&regs->attreli, attrs);
  1036. /* Start with defaults, and add stashing
  1037. * depending on driver parameters
  1038. */
  1039. attrs = ATTR_INIT_SETTINGS;
  1040. if (priv->bd_stash_en)
  1041. attrs |= ATTR_BDSTASH;
  1042. if (priv->rx_stash_size != 0)
  1043. attrs |= ATTR_BUFSTASH;
  1044. gfar_write(&regs->attr, attrs);
  1045. /* FIFO configs */
  1046. gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
  1047. gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
  1048. gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
  1049. /* Program the interrupt steering regs, only for MG devices */
  1050. if (priv->num_grps > 1)
  1051. gfar_write_isrg(priv);
  1052. }
  1053. static void gfar_init_addr_hash_table(struct gfar_private *priv)
  1054. {
  1055. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1056. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
  1057. priv->extended_hash = 1;
  1058. priv->hash_width = 9;
  1059. priv->hash_regs[0] = &regs->igaddr0;
  1060. priv->hash_regs[1] = &regs->igaddr1;
  1061. priv->hash_regs[2] = &regs->igaddr2;
  1062. priv->hash_regs[3] = &regs->igaddr3;
  1063. priv->hash_regs[4] = &regs->igaddr4;
  1064. priv->hash_regs[5] = &regs->igaddr5;
  1065. priv->hash_regs[6] = &regs->igaddr6;
  1066. priv->hash_regs[7] = &regs->igaddr7;
  1067. priv->hash_regs[8] = &regs->gaddr0;
  1068. priv->hash_regs[9] = &regs->gaddr1;
  1069. priv->hash_regs[10] = &regs->gaddr2;
  1070. priv->hash_regs[11] = &regs->gaddr3;
  1071. priv->hash_regs[12] = &regs->gaddr4;
  1072. priv->hash_regs[13] = &regs->gaddr5;
  1073. priv->hash_regs[14] = &regs->gaddr6;
  1074. priv->hash_regs[15] = &regs->gaddr7;
  1075. } else {
  1076. priv->extended_hash = 0;
  1077. priv->hash_width = 8;
  1078. priv->hash_regs[0] = &regs->gaddr0;
  1079. priv->hash_regs[1] = &regs->gaddr1;
  1080. priv->hash_regs[2] = &regs->gaddr2;
  1081. priv->hash_regs[3] = &regs->gaddr3;
  1082. priv->hash_regs[4] = &regs->gaddr4;
  1083. priv->hash_regs[5] = &regs->gaddr5;
  1084. priv->hash_regs[6] = &regs->gaddr6;
  1085. priv->hash_regs[7] = &regs->gaddr7;
  1086. }
  1087. }
  1088. /* Set up the ethernet device structure, private data,
  1089. * and anything else we need before we start
  1090. */
  1091. static int gfar_probe(struct platform_device *ofdev)
  1092. {
  1093. struct net_device *dev = NULL;
  1094. struct gfar_private *priv = NULL;
  1095. int err = 0, i;
  1096. err = gfar_of_init(ofdev, &dev);
  1097. if (err)
  1098. return err;
  1099. priv = netdev_priv(dev);
  1100. priv->ndev = dev;
  1101. priv->ofdev = ofdev;
  1102. priv->dev = &ofdev->dev;
  1103. SET_NETDEV_DEV(dev, &ofdev->dev);
  1104. spin_lock_init(&priv->bflock);
  1105. INIT_WORK(&priv->reset_task, gfar_reset_task);
  1106. platform_set_drvdata(ofdev, priv);
  1107. gfar_detect_errata(priv);
  1108. /* Set the dev->base_addr to the gfar reg region */
  1109. dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
  1110. /* Fill in the dev structure */
  1111. dev->watchdog_timeo = TX_TIMEOUT;
  1112. dev->mtu = 1500;
  1113. dev->netdev_ops = &gfar_netdev_ops;
  1114. dev->ethtool_ops = &gfar_ethtool_ops;
  1115. /* Register for napi ...We are registering NAPI for each grp */
  1116. for (i = 0; i < priv->num_grps; i++) {
  1117. if (priv->poll_mode == GFAR_SQ_POLLING) {
  1118. netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
  1119. gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
  1120. netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
  1121. gfar_poll_tx_sq, 2);
  1122. } else {
  1123. netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
  1124. gfar_poll_rx, GFAR_DEV_WEIGHT);
  1125. netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
  1126. gfar_poll_tx, 2);
  1127. }
  1128. }
  1129. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
  1130. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  1131. NETIF_F_RXCSUM;
  1132. dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
  1133. NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
  1134. }
  1135. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
  1136. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
  1137. NETIF_F_HW_VLAN_CTAG_RX;
  1138. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  1139. }
  1140. gfar_init_addr_hash_table(priv);
  1141. /* Insert receive time stamps into padding alignment bytes */
  1142. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
  1143. priv->padding = 8;
  1144. if (dev->features & NETIF_F_IP_CSUM ||
  1145. priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
  1146. dev->needed_headroom = GMAC_FCB_LEN;
  1147. priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
  1148. /* Initializing some of the rx/tx queue level parameters */
  1149. for (i = 0; i < priv->num_tx_queues; i++) {
  1150. priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
  1151. priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
  1152. priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
  1153. priv->tx_queue[i]->txic = DEFAULT_TXIC;
  1154. }
  1155. for (i = 0; i < priv->num_rx_queues; i++) {
  1156. priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
  1157. priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
  1158. priv->rx_queue[i]->rxic = DEFAULT_RXIC;
  1159. }
  1160. /* always enable rx filer */
  1161. priv->rx_filer_enable = 1;
  1162. /* Enable most messages by default */
  1163. priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  1164. /* use pritority h/w tx queue scheduling for single queue devices */
  1165. if (priv->num_tx_queues == 1)
  1166. priv->prio_sched_en = 1;
  1167. set_bit(GFAR_DOWN, &priv->state);
  1168. gfar_hw_init(priv);
  1169. /* Carrier starts down, phylib will bring it up */
  1170. netif_carrier_off(dev);
  1171. err = register_netdev(dev);
  1172. if (err) {
  1173. pr_err("%s: Cannot register net device, aborting\n", dev->name);
  1174. goto register_fail;
  1175. }
  1176. device_init_wakeup(&dev->dev,
  1177. priv->device_flags &
  1178. FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  1179. /* fill out IRQ number and name fields */
  1180. for (i = 0; i < priv->num_grps; i++) {
  1181. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  1182. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1183. sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
  1184. dev->name, "_g", '0' + i, "_tx");
  1185. sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
  1186. dev->name, "_g", '0' + i, "_rx");
  1187. sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
  1188. dev->name, "_g", '0' + i, "_er");
  1189. } else
  1190. strcpy(gfar_irq(grp, TX)->name, dev->name);
  1191. }
  1192. /* Initialize the filer table */
  1193. gfar_init_filer_table(priv);
  1194. /* Print out the device info */
  1195. netdev_info(dev, "mac: %pM\n", dev->dev_addr);
  1196. /* Even more device info helps when determining which kernel
  1197. * provided which set of benchmarks.
  1198. */
  1199. netdev_info(dev, "Running with NAPI enabled\n");
  1200. for (i = 0; i < priv->num_rx_queues; i++)
  1201. netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
  1202. i, priv->rx_queue[i]->rx_ring_size);
  1203. for (i = 0; i < priv->num_tx_queues; i++)
  1204. netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
  1205. i, priv->tx_queue[i]->tx_ring_size);
  1206. return 0;
  1207. register_fail:
  1208. unmap_group_regs(priv);
  1209. gfar_free_rx_queues(priv);
  1210. gfar_free_tx_queues(priv);
  1211. of_node_put(priv->phy_node);
  1212. of_node_put(priv->tbi_node);
  1213. free_gfar_dev(priv);
  1214. return err;
  1215. }
  1216. static int gfar_remove(struct platform_device *ofdev)
  1217. {
  1218. struct gfar_private *priv = platform_get_drvdata(ofdev);
  1219. of_node_put(priv->phy_node);
  1220. of_node_put(priv->tbi_node);
  1221. unregister_netdev(priv->ndev);
  1222. unmap_group_regs(priv);
  1223. gfar_free_rx_queues(priv);
  1224. gfar_free_tx_queues(priv);
  1225. free_gfar_dev(priv);
  1226. return 0;
  1227. }
  1228. #ifdef CONFIG_PM
  1229. static int gfar_suspend(struct device *dev)
  1230. {
  1231. struct gfar_private *priv = dev_get_drvdata(dev);
  1232. struct net_device *ndev = priv->ndev;
  1233. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1234. unsigned long flags;
  1235. u32 tempval;
  1236. int magic_packet = priv->wol_en &&
  1237. (priv->device_flags &
  1238. FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  1239. netif_device_detach(ndev);
  1240. if (netif_running(ndev)) {
  1241. local_irq_save(flags);
  1242. lock_tx_qs(priv);
  1243. gfar_halt_nodisable(priv);
  1244. /* Disable Tx, and Rx if wake-on-LAN is disabled. */
  1245. tempval = gfar_read(&regs->maccfg1);
  1246. tempval &= ~MACCFG1_TX_EN;
  1247. if (!magic_packet)
  1248. tempval &= ~MACCFG1_RX_EN;
  1249. gfar_write(&regs->maccfg1, tempval);
  1250. unlock_tx_qs(priv);
  1251. local_irq_restore(flags);
  1252. disable_napi(priv);
  1253. if (magic_packet) {
  1254. /* Enable interrupt on Magic Packet */
  1255. gfar_write(&regs->imask, IMASK_MAG);
  1256. /* Enable Magic Packet mode */
  1257. tempval = gfar_read(&regs->maccfg2);
  1258. tempval |= MACCFG2_MPEN;
  1259. gfar_write(&regs->maccfg2, tempval);
  1260. } else {
  1261. phy_stop(priv->phydev);
  1262. }
  1263. }
  1264. return 0;
  1265. }
  1266. static int gfar_resume(struct device *dev)
  1267. {
  1268. struct gfar_private *priv = dev_get_drvdata(dev);
  1269. struct net_device *ndev = priv->ndev;
  1270. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1271. unsigned long flags;
  1272. u32 tempval;
  1273. int magic_packet = priv->wol_en &&
  1274. (priv->device_flags &
  1275. FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  1276. if (!netif_running(ndev)) {
  1277. netif_device_attach(ndev);
  1278. return 0;
  1279. }
  1280. if (!magic_packet && priv->phydev)
  1281. phy_start(priv->phydev);
  1282. /* Disable Magic Packet mode, in case something
  1283. * else woke us up.
  1284. */
  1285. local_irq_save(flags);
  1286. lock_tx_qs(priv);
  1287. tempval = gfar_read(&regs->maccfg2);
  1288. tempval &= ~MACCFG2_MPEN;
  1289. gfar_write(&regs->maccfg2, tempval);
  1290. gfar_start(priv);
  1291. unlock_tx_qs(priv);
  1292. local_irq_restore(flags);
  1293. netif_device_attach(ndev);
  1294. enable_napi(priv);
  1295. return 0;
  1296. }
  1297. static int gfar_restore(struct device *dev)
  1298. {
  1299. struct gfar_private *priv = dev_get_drvdata(dev);
  1300. struct net_device *ndev = priv->ndev;
  1301. if (!netif_running(ndev)) {
  1302. netif_device_attach(ndev);
  1303. return 0;
  1304. }
  1305. if (gfar_init_bds(ndev)) {
  1306. free_skb_resources(priv);
  1307. return -ENOMEM;
  1308. }
  1309. gfar_mac_reset(priv);
  1310. gfar_init_tx_rx_base(priv);
  1311. gfar_start(priv);
  1312. priv->oldlink = 0;
  1313. priv->oldspeed = 0;
  1314. priv->oldduplex = -1;
  1315. if (priv->phydev)
  1316. phy_start(priv->phydev);
  1317. netif_device_attach(ndev);
  1318. enable_napi(priv);
  1319. return 0;
  1320. }
  1321. static struct dev_pm_ops gfar_pm_ops = {
  1322. .suspend = gfar_suspend,
  1323. .resume = gfar_resume,
  1324. .freeze = gfar_suspend,
  1325. .thaw = gfar_resume,
  1326. .restore = gfar_restore,
  1327. };
  1328. #define GFAR_PM_OPS (&gfar_pm_ops)
  1329. #else
  1330. #define GFAR_PM_OPS NULL
  1331. #endif
  1332. /* Reads the controller's registers to determine what interface
  1333. * connects it to the PHY.
  1334. */
  1335. static phy_interface_t gfar_get_interface(struct net_device *dev)
  1336. {
  1337. struct gfar_private *priv = netdev_priv(dev);
  1338. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1339. u32 ecntrl;
  1340. ecntrl = gfar_read(&regs->ecntrl);
  1341. if (ecntrl & ECNTRL_SGMII_MODE)
  1342. return PHY_INTERFACE_MODE_SGMII;
  1343. if (ecntrl & ECNTRL_TBI_MODE) {
  1344. if (ecntrl & ECNTRL_REDUCED_MODE)
  1345. return PHY_INTERFACE_MODE_RTBI;
  1346. else
  1347. return PHY_INTERFACE_MODE_TBI;
  1348. }
  1349. if (ecntrl & ECNTRL_REDUCED_MODE) {
  1350. if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
  1351. return PHY_INTERFACE_MODE_RMII;
  1352. }
  1353. else {
  1354. phy_interface_t interface = priv->interface;
  1355. /* This isn't autodetected right now, so it must
  1356. * be set by the device tree or platform code.
  1357. */
  1358. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  1359. return PHY_INTERFACE_MODE_RGMII_ID;
  1360. return PHY_INTERFACE_MODE_RGMII;
  1361. }
  1362. }
  1363. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  1364. return PHY_INTERFACE_MODE_GMII;
  1365. return PHY_INTERFACE_MODE_MII;
  1366. }
  1367. /* Initializes driver's PHY state, and attaches to the PHY.
  1368. * Returns 0 on success.
  1369. */
  1370. static int init_phy(struct net_device *dev)
  1371. {
  1372. struct gfar_private *priv = netdev_priv(dev);
  1373. uint gigabit_support =
  1374. priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
  1375. GFAR_SUPPORTED_GBIT : 0;
  1376. phy_interface_t interface;
  1377. priv->oldlink = 0;
  1378. priv->oldspeed = 0;
  1379. priv->oldduplex = -1;
  1380. interface = gfar_get_interface(dev);
  1381. priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
  1382. interface);
  1383. if (!priv->phydev) {
  1384. dev_err(&dev->dev, "could not attach to PHY\n");
  1385. return -ENODEV;
  1386. }
  1387. if (interface == PHY_INTERFACE_MODE_SGMII)
  1388. gfar_configure_serdes(dev);
  1389. /* Remove any features not supported by the controller */
  1390. priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
  1391. priv->phydev->advertising = priv->phydev->supported;
  1392. /* Add support for flow control, but don't advertise it by default */
  1393. priv->phydev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
  1394. return 0;
  1395. }
  1396. /* Initialize TBI PHY interface for communicating with the
  1397. * SERDES lynx PHY on the chip. We communicate with this PHY
  1398. * through the MDIO bus on each controller, treating it as a
  1399. * "normal" PHY at the address found in the TBIPA register. We assume
  1400. * that the TBIPA register is valid. Either the MDIO bus code will set
  1401. * it to a value that doesn't conflict with other PHYs on the bus, or the
  1402. * value doesn't matter, as there are no other PHYs on the bus.
  1403. */
  1404. static void gfar_configure_serdes(struct net_device *dev)
  1405. {
  1406. struct gfar_private *priv = netdev_priv(dev);
  1407. struct phy_device *tbiphy;
  1408. if (!priv->tbi_node) {
  1409. dev_warn(&dev->dev, "error: SGMII mode requires that the "
  1410. "device tree specify a tbi-handle\n");
  1411. return;
  1412. }
  1413. tbiphy = of_phy_find_device(priv->tbi_node);
  1414. if (!tbiphy) {
  1415. dev_err(&dev->dev, "error: Could not get TBI device\n");
  1416. return;
  1417. }
  1418. /* If the link is already up, we must already be ok, and don't need to
  1419. * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
  1420. * everything for us? Resetting it takes the link down and requires
  1421. * several seconds for it to come back.
  1422. */
  1423. if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
  1424. return;
  1425. /* Single clk mode, mii mode off(for serdes communication) */
  1426. phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
  1427. phy_write(tbiphy, MII_ADVERTISE,
  1428. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  1429. ADVERTISE_1000XPSE_ASYM);
  1430. phy_write(tbiphy, MII_BMCR,
  1431. BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
  1432. BMCR_SPEED1000);
  1433. }
  1434. static int __gfar_is_rx_idle(struct gfar_private *priv)
  1435. {
  1436. u32 res;
  1437. /* Normaly TSEC should not hang on GRS commands, so we should
  1438. * actually wait for IEVENT_GRSC flag.
  1439. */
  1440. if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
  1441. return 0;
  1442. /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
  1443. * the same as bits 23-30, the eTSEC Rx is assumed to be idle
  1444. * and the Rx can be safely reset.
  1445. */
  1446. res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
  1447. res &= 0x7f807f80;
  1448. if ((res & 0xffff) == (res >> 16))
  1449. return 1;
  1450. return 0;
  1451. }
  1452. /* Halt the receive and transmit queues */
  1453. static void gfar_halt_nodisable(struct gfar_private *priv)
  1454. {
  1455. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1456. u32 tempval;
  1457. unsigned int timeout;
  1458. int stopped;
  1459. gfar_ints_disable(priv);
  1460. if (gfar_is_dma_stopped(priv))
  1461. return;
  1462. /* Stop the DMA, and wait for it to stop */
  1463. tempval = gfar_read(&regs->dmactrl);
  1464. tempval |= (DMACTRL_GRS | DMACTRL_GTS);
  1465. gfar_write(&regs->dmactrl, tempval);
  1466. retry:
  1467. timeout = 1000;
  1468. while (!(stopped = gfar_is_dma_stopped(priv)) && timeout) {
  1469. cpu_relax();
  1470. timeout--;
  1471. }
  1472. if (!timeout)
  1473. stopped = gfar_is_dma_stopped(priv);
  1474. if (!stopped && !gfar_is_rx_dma_stopped(priv) &&
  1475. !__gfar_is_rx_idle(priv))
  1476. goto retry;
  1477. }
  1478. /* Halt the receive and transmit queues */
  1479. void gfar_halt(struct gfar_private *priv)
  1480. {
  1481. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1482. u32 tempval;
  1483. /* Dissable the Rx/Tx hw queues */
  1484. gfar_write(&regs->rqueue, 0);
  1485. gfar_write(&regs->tqueue, 0);
  1486. mdelay(10);
  1487. gfar_halt_nodisable(priv);
  1488. /* Disable Rx/Tx DMA */
  1489. tempval = gfar_read(&regs->maccfg1);
  1490. tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
  1491. gfar_write(&regs->maccfg1, tempval);
  1492. }
  1493. void stop_gfar(struct net_device *dev)
  1494. {
  1495. struct gfar_private *priv = netdev_priv(dev);
  1496. netif_tx_stop_all_queues(dev);
  1497. smp_mb__before_atomic();
  1498. set_bit(GFAR_DOWN, &priv->state);
  1499. smp_mb__after_atomic();
  1500. disable_napi(priv);
  1501. /* disable ints and gracefully shut down Rx/Tx DMA */
  1502. gfar_halt(priv);
  1503. phy_stop(priv->phydev);
  1504. free_skb_resources(priv);
  1505. }
  1506. static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
  1507. {
  1508. struct txbd8 *txbdp;
  1509. struct gfar_private *priv = netdev_priv(tx_queue->dev);
  1510. int i, j;
  1511. txbdp = tx_queue->tx_bd_base;
  1512. for (i = 0; i < tx_queue->tx_ring_size; i++) {
  1513. if (!tx_queue->tx_skbuff[i])
  1514. continue;
  1515. dma_unmap_single(priv->dev, txbdp->bufPtr,
  1516. txbdp->length, DMA_TO_DEVICE);
  1517. txbdp->lstatus = 0;
  1518. for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
  1519. j++) {
  1520. txbdp++;
  1521. dma_unmap_page(priv->dev, txbdp->bufPtr,
  1522. txbdp->length, DMA_TO_DEVICE);
  1523. }
  1524. txbdp++;
  1525. dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
  1526. tx_queue->tx_skbuff[i] = NULL;
  1527. }
  1528. kfree(tx_queue->tx_skbuff);
  1529. tx_queue->tx_skbuff = NULL;
  1530. }
  1531. static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
  1532. {
  1533. struct rxbd8 *rxbdp;
  1534. struct gfar_private *priv = netdev_priv(rx_queue->dev);
  1535. int i;
  1536. rxbdp = rx_queue->rx_bd_base;
  1537. for (i = 0; i < rx_queue->rx_ring_size; i++) {
  1538. if (rx_queue->rx_skbuff[i]) {
  1539. dma_unmap_single(priv->dev, rxbdp->bufPtr,
  1540. priv->rx_buffer_size,
  1541. DMA_FROM_DEVICE);
  1542. dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
  1543. rx_queue->rx_skbuff[i] = NULL;
  1544. }
  1545. rxbdp->lstatus = 0;
  1546. rxbdp->bufPtr = 0;
  1547. rxbdp++;
  1548. }
  1549. kfree(rx_queue->rx_skbuff);
  1550. rx_queue->rx_skbuff = NULL;
  1551. }
  1552. /* If there are any tx skbs or rx skbs still around, free them.
  1553. * Then free tx_skbuff and rx_skbuff
  1554. */
  1555. static void free_skb_resources(struct gfar_private *priv)
  1556. {
  1557. struct gfar_priv_tx_q *tx_queue = NULL;
  1558. struct gfar_priv_rx_q *rx_queue = NULL;
  1559. int i;
  1560. /* Go through all the buffer descriptors and free their data buffers */
  1561. for (i = 0; i < priv->num_tx_queues; i++) {
  1562. struct netdev_queue *txq;
  1563. tx_queue = priv->tx_queue[i];
  1564. txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
  1565. if (tx_queue->tx_skbuff)
  1566. free_skb_tx_queue(tx_queue);
  1567. netdev_tx_reset_queue(txq);
  1568. }
  1569. for (i = 0; i < priv->num_rx_queues; i++) {
  1570. rx_queue = priv->rx_queue[i];
  1571. if (rx_queue->rx_skbuff)
  1572. free_skb_rx_queue(rx_queue);
  1573. }
  1574. dma_free_coherent(priv->dev,
  1575. sizeof(struct txbd8) * priv->total_tx_ring_size +
  1576. sizeof(struct rxbd8) * priv->total_rx_ring_size,
  1577. priv->tx_queue[0]->tx_bd_base,
  1578. priv->tx_queue[0]->tx_bd_dma_base);
  1579. }
  1580. void gfar_start(struct gfar_private *priv)
  1581. {
  1582. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1583. u32 tempval;
  1584. int i = 0;
  1585. /* Enable Rx/Tx hw queues */
  1586. gfar_write(&regs->rqueue, priv->rqueue);
  1587. gfar_write(&regs->tqueue, priv->tqueue);
  1588. /* Initialize DMACTRL to have WWR and WOP */
  1589. tempval = gfar_read(&regs->dmactrl);
  1590. tempval |= DMACTRL_INIT_SETTINGS;
  1591. gfar_write(&regs->dmactrl, tempval);
  1592. /* Make sure we aren't stopped */
  1593. tempval = gfar_read(&regs->dmactrl);
  1594. tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
  1595. gfar_write(&regs->dmactrl, tempval);
  1596. for (i = 0; i < priv->num_grps; i++) {
  1597. regs = priv->gfargrp[i].regs;
  1598. /* Clear THLT/RHLT, so that the DMA starts polling now */
  1599. gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
  1600. gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
  1601. }
  1602. /* Enable Rx/Tx DMA */
  1603. tempval = gfar_read(&regs->maccfg1);
  1604. tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  1605. gfar_write(&regs->maccfg1, tempval);
  1606. gfar_ints_enable(priv);
  1607. priv->ndev->trans_start = jiffies; /* prevent tx timeout */
  1608. }
  1609. static void free_grp_irqs(struct gfar_priv_grp *grp)
  1610. {
  1611. free_irq(gfar_irq(grp, TX)->irq, grp);
  1612. free_irq(gfar_irq(grp, RX)->irq, grp);
  1613. free_irq(gfar_irq(grp, ER)->irq, grp);
  1614. }
  1615. static int register_grp_irqs(struct gfar_priv_grp *grp)
  1616. {
  1617. struct gfar_private *priv = grp->priv;
  1618. struct net_device *dev = priv->ndev;
  1619. int err;
  1620. /* If the device has multiple interrupts, register for
  1621. * them. Otherwise, only register for the one
  1622. */
  1623. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1624. /* Install our interrupt handlers for Error,
  1625. * Transmit, and Receive
  1626. */
  1627. err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
  1628. gfar_irq(grp, ER)->name, grp);
  1629. if (err < 0) {
  1630. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1631. gfar_irq(grp, ER)->irq);
  1632. goto err_irq_fail;
  1633. }
  1634. err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
  1635. gfar_irq(grp, TX)->name, grp);
  1636. if (err < 0) {
  1637. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1638. gfar_irq(grp, TX)->irq);
  1639. goto tx_irq_fail;
  1640. }
  1641. err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
  1642. gfar_irq(grp, RX)->name, grp);
  1643. if (err < 0) {
  1644. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1645. gfar_irq(grp, RX)->irq);
  1646. goto rx_irq_fail;
  1647. }
  1648. } else {
  1649. err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
  1650. gfar_irq(grp, TX)->name, grp);
  1651. if (err < 0) {
  1652. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1653. gfar_irq(grp, TX)->irq);
  1654. goto err_irq_fail;
  1655. }
  1656. }
  1657. return 0;
  1658. rx_irq_fail:
  1659. free_irq(gfar_irq(grp, TX)->irq, grp);
  1660. tx_irq_fail:
  1661. free_irq(gfar_irq(grp, ER)->irq, grp);
  1662. err_irq_fail:
  1663. return err;
  1664. }
  1665. static void gfar_free_irq(struct gfar_private *priv)
  1666. {
  1667. int i;
  1668. /* Free the IRQs */
  1669. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1670. for (i = 0; i < priv->num_grps; i++)
  1671. free_grp_irqs(&priv->gfargrp[i]);
  1672. } else {
  1673. for (i = 0; i < priv->num_grps; i++)
  1674. free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
  1675. &priv->gfargrp[i]);
  1676. }
  1677. }
  1678. static int gfar_request_irq(struct gfar_private *priv)
  1679. {
  1680. int err, i, j;
  1681. for (i = 0; i < priv->num_grps; i++) {
  1682. err = register_grp_irqs(&priv->gfargrp[i]);
  1683. if (err) {
  1684. for (j = 0; j < i; j++)
  1685. free_grp_irqs(&priv->gfargrp[j]);
  1686. return err;
  1687. }
  1688. }
  1689. return 0;
  1690. }
  1691. /* Bring the controller up and running */
  1692. int startup_gfar(struct net_device *ndev)
  1693. {
  1694. struct gfar_private *priv = netdev_priv(ndev);
  1695. int err;
  1696. gfar_mac_reset(priv);
  1697. err = gfar_alloc_skb_resources(ndev);
  1698. if (err)
  1699. return err;
  1700. gfar_init_tx_rx_base(priv);
  1701. smp_mb__before_atomic();
  1702. clear_bit(GFAR_DOWN, &priv->state);
  1703. smp_mb__after_atomic();
  1704. /* Start Rx/Tx DMA and enable the interrupts */
  1705. gfar_start(priv);
  1706. phy_start(priv->phydev);
  1707. enable_napi(priv);
  1708. netif_tx_wake_all_queues(ndev);
  1709. return 0;
  1710. }
  1711. /* Called when something needs to use the ethernet device
  1712. * Returns 0 for success.
  1713. */
  1714. static int gfar_enet_open(struct net_device *dev)
  1715. {
  1716. struct gfar_private *priv = netdev_priv(dev);
  1717. int err;
  1718. err = init_phy(dev);
  1719. if (err)
  1720. return err;
  1721. err = gfar_request_irq(priv);
  1722. if (err)
  1723. return err;
  1724. err = startup_gfar(dev);
  1725. if (err)
  1726. return err;
  1727. device_set_wakeup_enable(&dev->dev, priv->wol_en);
  1728. return err;
  1729. }
  1730. static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
  1731. {
  1732. struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
  1733. memset(fcb, 0, GMAC_FCB_LEN);
  1734. return fcb;
  1735. }
  1736. static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
  1737. int fcb_length)
  1738. {
  1739. /* If we're here, it's a IP packet with a TCP or UDP
  1740. * payload. We set it to checksum, using a pseudo-header
  1741. * we provide
  1742. */
  1743. u8 flags = TXFCB_DEFAULT;
  1744. /* Tell the controller what the protocol is
  1745. * And provide the already calculated phcs
  1746. */
  1747. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  1748. flags |= TXFCB_UDP;
  1749. fcb->phcs = udp_hdr(skb)->check;
  1750. } else
  1751. fcb->phcs = tcp_hdr(skb)->check;
  1752. /* l3os is the distance between the start of the
  1753. * frame (skb->data) and the start of the IP hdr.
  1754. * l4os is the distance between the start of the
  1755. * l3 hdr and the l4 hdr
  1756. */
  1757. fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
  1758. fcb->l4os = skb_network_header_len(skb);
  1759. fcb->flags = flags;
  1760. }
  1761. void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
  1762. {
  1763. fcb->flags |= TXFCB_VLN;
  1764. fcb->vlctl = skb_vlan_tag_get(skb);
  1765. }
  1766. static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
  1767. struct txbd8 *base, int ring_size)
  1768. {
  1769. struct txbd8 *new_bd = bdp + stride;
  1770. return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
  1771. }
  1772. static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
  1773. int ring_size)
  1774. {
  1775. return skip_txbd(bdp, 1, base, ring_size);
  1776. }
  1777. /* eTSEC12: csum generation not supported for some fcb offsets */
  1778. static inline bool gfar_csum_errata_12(struct gfar_private *priv,
  1779. unsigned long fcb_addr)
  1780. {
  1781. return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
  1782. (fcb_addr % 0x20) > 0x18);
  1783. }
  1784. /* eTSEC76: csum generation for frames larger than 2500 may
  1785. * cause excess delays before start of transmission
  1786. */
  1787. static inline bool gfar_csum_errata_76(struct gfar_private *priv,
  1788. unsigned int len)
  1789. {
  1790. return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
  1791. (len > 2500));
  1792. }
  1793. /* This is called by the kernel when a frame is ready for transmission.
  1794. * It is pointed to by the dev->hard_start_xmit function pointer
  1795. */
  1796. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1797. {
  1798. struct gfar_private *priv = netdev_priv(dev);
  1799. struct gfar_priv_tx_q *tx_queue = NULL;
  1800. struct netdev_queue *txq;
  1801. struct gfar __iomem *regs = NULL;
  1802. struct txfcb *fcb = NULL;
  1803. struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
  1804. u32 lstatus;
  1805. int i, rq = 0;
  1806. int do_tstamp, do_csum, do_vlan;
  1807. u32 bufaddr;
  1808. unsigned long flags;
  1809. unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
  1810. rq = skb->queue_mapping;
  1811. tx_queue = priv->tx_queue[rq];
  1812. txq = netdev_get_tx_queue(dev, rq);
  1813. base = tx_queue->tx_bd_base;
  1814. regs = tx_queue->grp->regs;
  1815. do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
  1816. do_vlan = skb_vlan_tag_present(skb);
  1817. do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
  1818. priv->hwts_tx_en;
  1819. if (do_csum || do_vlan)
  1820. fcb_len = GMAC_FCB_LEN;
  1821. /* check if time stamp should be generated */
  1822. if (unlikely(do_tstamp))
  1823. fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  1824. /* make space for additional header when fcb is needed */
  1825. if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
  1826. struct sk_buff *skb_new;
  1827. skb_new = skb_realloc_headroom(skb, fcb_len);
  1828. if (!skb_new) {
  1829. dev->stats.tx_errors++;
  1830. dev_kfree_skb_any(skb);
  1831. return NETDEV_TX_OK;
  1832. }
  1833. if (skb->sk)
  1834. skb_set_owner_w(skb_new, skb->sk);
  1835. dev_consume_skb_any(skb);
  1836. skb = skb_new;
  1837. }
  1838. /* total number of fragments in the SKB */
  1839. nr_frags = skb_shinfo(skb)->nr_frags;
  1840. /* calculate the required number of TxBDs for this skb */
  1841. if (unlikely(do_tstamp))
  1842. nr_txbds = nr_frags + 2;
  1843. else
  1844. nr_txbds = nr_frags + 1;
  1845. /* check if there is space to queue this packet */
  1846. if (nr_txbds > tx_queue->num_txbdfree) {
  1847. /* no space, stop the queue */
  1848. netif_tx_stop_queue(txq);
  1849. dev->stats.tx_fifo_errors++;
  1850. return NETDEV_TX_BUSY;
  1851. }
  1852. /* Update transmit stats */
  1853. bytes_sent = skb->len;
  1854. tx_queue->stats.tx_bytes += bytes_sent;
  1855. /* keep Tx bytes on wire for BQL accounting */
  1856. GFAR_CB(skb)->bytes_sent = bytes_sent;
  1857. tx_queue->stats.tx_packets++;
  1858. txbdp = txbdp_start = tx_queue->cur_tx;
  1859. lstatus = txbdp->lstatus;
  1860. /* Time stamp insertion requires one additional TxBD */
  1861. if (unlikely(do_tstamp))
  1862. txbdp_tstamp = txbdp = next_txbd(txbdp, base,
  1863. tx_queue->tx_ring_size);
  1864. if (nr_frags == 0) {
  1865. if (unlikely(do_tstamp))
  1866. txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
  1867. TXBD_INTERRUPT);
  1868. else
  1869. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1870. } else {
  1871. /* Place the fragment addresses and lengths into the TxBDs */
  1872. for (i = 0; i < nr_frags; i++) {
  1873. unsigned int frag_len;
  1874. /* Point at the next BD, wrapping as needed */
  1875. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1876. frag_len = skb_shinfo(skb)->frags[i].size;
  1877. lstatus = txbdp->lstatus | frag_len |
  1878. BD_LFLAG(TXBD_READY);
  1879. /* Handle the last BD specially */
  1880. if (i == nr_frags - 1)
  1881. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1882. bufaddr = skb_frag_dma_map(priv->dev,
  1883. &skb_shinfo(skb)->frags[i],
  1884. 0,
  1885. frag_len,
  1886. DMA_TO_DEVICE);
  1887. if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
  1888. goto dma_map_err;
  1889. /* set the TxBD length and buffer pointer */
  1890. txbdp->bufPtr = bufaddr;
  1891. txbdp->lstatus = lstatus;
  1892. }
  1893. lstatus = txbdp_start->lstatus;
  1894. }
  1895. /* Add TxPAL between FCB and frame if required */
  1896. if (unlikely(do_tstamp)) {
  1897. skb_push(skb, GMAC_TXPAL_LEN);
  1898. memset(skb->data, 0, GMAC_TXPAL_LEN);
  1899. }
  1900. /* Add TxFCB if required */
  1901. if (fcb_len) {
  1902. fcb = gfar_add_fcb(skb);
  1903. lstatus |= BD_LFLAG(TXBD_TOE);
  1904. }
  1905. /* Set up checksumming */
  1906. if (do_csum) {
  1907. gfar_tx_checksum(skb, fcb, fcb_len);
  1908. if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
  1909. unlikely(gfar_csum_errata_76(priv, skb->len))) {
  1910. __skb_pull(skb, GMAC_FCB_LEN);
  1911. skb_checksum_help(skb);
  1912. if (do_vlan || do_tstamp) {
  1913. /* put back a new fcb for vlan/tstamp TOE */
  1914. fcb = gfar_add_fcb(skb);
  1915. } else {
  1916. /* Tx TOE not used */
  1917. lstatus &= ~(BD_LFLAG(TXBD_TOE));
  1918. fcb = NULL;
  1919. }
  1920. }
  1921. }
  1922. if (do_vlan)
  1923. gfar_tx_vlan(skb, fcb);
  1924. /* Setup tx hardware time stamping if requested */
  1925. if (unlikely(do_tstamp)) {
  1926. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1927. fcb->ptp = 1;
  1928. }
  1929. bufaddr = dma_map_single(priv->dev, skb->data, skb_headlen(skb),
  1930. DMA_TO_DEVICE);
  1931. if (unlikely(dma_mapping_error(priv->dev, bufaddr)))
  1932. goto dma_map_err;
  1933. txbdp_start->bufPtr = bufaddr;
  1934. /* If time stamping is requested one additional TxBD must be set up. The
  1935. * first TxBD points to the FCB and must have a data length of
  1936. * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
  1937. * the full frame length.
  1938. */
  1939. if (unlikely(do_tstamp)) {
  1940. txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
  1941. txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
  1942. (skb_headlen(skb) - fcb_len);
  1943. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
  1944. } else {
  1945. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
  1946. }
  1947. netdev_tx_sent_queue(txq, bytes_sent);
  1948. /* We can work in parallel with gfar_clean_tx_ring(), except
  1949. * when modifying num_txbdfree. Note that we didn't grab the lock
  1950. * when we were reading the num_txbdfree and checking for available
  1951. * space, that's because outside of this function it can only grow,
  1952. * and once we've got needed space, it cannot suddenly disappear.
  1953. *
  1954. * The lock also protects us from gfar_error(), which can modify
  1955. * regs->tstat and thus retrigger the transfers, which is why we
  1956. * also must grab the lock before setting ready bit for the first
  1957. * to be transmitted BD.
  1958. */
  1959. spin_lock_irqsave(&tx_queue->txlock, flags);
  1960. gfar_wmb();
  1961. txbdp_start->lstatus = lstatus;
  1962. gfar_wmb(); /* force lstatus write before tx_skbuff */
  1963. tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
  1964. /* Update the current skb pointer to the next entry we will use
  1965. * (wrapping if necessary)
  1966. */
  1967. tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
  1968. TX_RING_MOD_MASK(tx_queue->tx_ring_size);
  1969. tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1970. /* reduce TxBD free count */
  1971. tx_queue->num_txbdfree -= (nr_txbds);
  1972. /* If the next BD still needs to be cleaned up, then the bds
  1973. * are full. We need to tell the kernel to stop sending us stuff.
  1974. */
  1975. if (!tx_queue->num_txbdfree) {
  1976. netif_tx_stop_queue(txq);
  1977. dev->stats.tx_fifo_errors++;
  1978. }
  1979. /* Tell the DMA to go go go */
  1980. gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
  1981. /* Unlock priv */
  1982. spin_unlock_irqrestore(&tx_queue->txlock, flags);
  1983. return NETDEV_TX_OK;
  1984. dma_map_err:
  1985. txbdp = next_txbd(txbdp_start, base, tx_queue->tx_ring_size);
  1986. if (do_tstamp)
  1987. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1988. for (i = 0; i < nr_frags; i++) {
  1989. lstatus = txbdp->lstatus;
  1990. if (!(lstatus & BD_LFLAG(TXBD_READY)))
  1991. break;
  1992. txbdp->lstatus = lstatus & ~BD_LFLAG(TXBD_READY);
  1993. bufaddr = txbdp->bufPtr;
  1994. dma_unmap_page(priv->dev, bufaddr, txbdp->length,
  1995. DMA_TO_DEVICE);
  1996. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1997. }
  1998. gfar_wmb();
  1999. dev_kfree_skb_any(skb);
  2000. return NETDEV_TX_OK;
  2001. }
  2002. /* Stops the kernel queue, and halts the controller */
  2003. static int gfar_close(struct net_device *dev)
  2004. {
  2005. struct gfar_private *priv = netdev_priv(dev);
  2006. cancel_work_sync(&priv->reset_task);
  2007. stop_gfar(dev);
  2008. /* Disconnect from the PHY */
  2009. phy_disconnect(priv->phydev);
  2010. priv->phydev = NULL;
  2011. gfar_free_irq(priv);
  2012. return 0;
  2013. }
  2014. /* Changes the mac address if the controller is not running. */
  2015. static int gfar_set_mac_address(struct net_device *dev)
  2016. {
  2017. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  2018. return 0;
  2019. }
  2020. static int gfar_change_mtu(struct net_device *dev, int new_mtu)
  2021. {
  2022. struct gfar_private *priv = netdev_priv(dev);
  2023. int frame_size = new_mtu + ETH_HLEN;
  2024. if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
  2025. netif_err(priv, drv, dev, "Invalid MTU setting\n");
  2026. return -EINVAL;
  2027. }
  2028. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  2029. cpu_relax();
  2030. if (dev->flags & IFF_UP)
  2031. stop_gfar(dev);
  2032. dev->mtu = new_mtu;
  2033. if (dev->flags & IFF_UP)
  2034. startup_gfar(dev);
  2035. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  2036. return 0;
  2037. }
  2038. void reset_gfar(struct net_device *ndev)
  2039. {
  2040. struct gfar_private *priv = netdev_priv(ndev);
  2041. while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
  2042. cpu_relax();
  2043. stop_gfar(ndev);
  2044. startup_gfar(ndev);
  2045. clear_bit_unlock(GFAR_RESETTING, &priv->state);
  2046. }
  2047. /* gfar_reset_task gets scheduled when a packet has not been
  2048. * transmitted after a set amount of time.
  2049. * For now, assume that clearing out all the structures, and
  2050. * starting over will fix the problem.
  2051. */
  2052. static void gfar_reset_task(struct work_struct *work)
  2053. {
  2054. struct gfar_private *priv = container_of(work, struct gfar_private,
  2055. reset_task);
  2056. reset_gfar(priv->ndev);
  2057. }
  2058. static void gfar_timeout(struct net_device *dev)
  2059. {
  2060. struct gfar_private *priv = netdev_priv(dev);
  2061. dev->stats.tx_errors++;
  2062. schedule_work(&priv->reset_task);
  2063. }
  2064. static void gfar_align_skb(struct sk_buff *skb)
  2065. {
  2066. /* We need the data buffer to be aligned properly. We will reserve
  2067. * as many bytes as needed to align the data properly
  2068. */
  2069. skb_reserve(skb, RXBUF_ALIGNMENT -
  2070. (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
  2071. }
  2072. /* Interrupt Handler for Transmit complete */
  2073. static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
  2074. {
  2075. struct net_device *dev = tx_queue->dev;
  2076. struct netdev_queue *txq;
  2077. struct gfar_private *priv = netdev_priv(dev);
  2078. struct txbd8 *bdp, *next = NULL;
  2079. struct txbd8 *lbdp = NULL;
  2080. struct txbd8 *base = tx_queue->tx_bd_base;
  2081. struct sk_buff *skb;
  2082. int skb_dirtytx;
  2083. int tx_ring_size = tx_queue->tx_ring_size;
  2084. int frags = 0, nr_txbds = 0;
  2085. int i;
  2086. int howmany = 0;
  2087. int tqi = tx_queue->qindex;
  2088. unsigned int bytes_sent = 0;
  2089. u32 lstatus;
  2090. size_t buflen;
  2091. txq = netdev_get_tx_queue(dev, tqi);
  2092. bdp = tx_queue->dirty_tx;
  2093. skb_dirtytx = tx_queue->skb_dirtytx;
  2094. while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
  2095. unsigned long flags;
  2096. frags = skb_shinfo(skb)->nr_frags;
  2097. /* When time stamping, one additional TxBD must be freed.
  2098. * Also, we need to dma_unmap_single() the TxPAL.
  2099. */
  2100. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  2101. nr_txbds = frags + 2;
  2102. else
  2103. nr_txbds = frags + 1;
  2104. lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
  2105. lstatus = lbdp->lstatus;
  2106. /* Only clean completed frames */
  2107. if ((lstatus & BD_LFLAG(TXBD_READY)) &&
  2108. (lstatus & BD_LENGTH_MASK))
  2109. break;
  2110. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
  2111. next = next_txbd(bdp, base, tx_ring_size);
  2112. buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  2113. } else
  2114. buflen = bdp->length;
  2115. dma_unmap_single(priv->dev, bdp->bufPtr,
  2116. buflen, DMA_TO_DEVICE);
  2117. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
  2118. struct skb_shared_hwtstamps shhwtstamps;
  2119. u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
  2120. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  2121. shhwtstamps.hwtstamp = ns_to_ktime(*ns);
  2122. skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
  2123. skb_tstamp_tx(skb, &shhwtstamps);
  2124. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  2125. bdp = next;
  2126. }
  2127. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  2128. bdp = next_txbd(bdp, base, tx_ring_size);
  2129. for (i = 0; i < frags; i++) {
  2130. dma_unmap_page(priv->dev, bdp->bufPtr,
  2131. bdp->length, DMA_TO_DEVICE);
  2132. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  2133. bdp = next_txbd(bdp, base, tx_ring_size);
  2134. }
  2135. bytes_sent += GFAR_CB(skb)->bytes_sent;
  2136. dev_kfree_skb_any(skb);
  2137. tx_queue->tx_skbuff[skb_dirtytx] = NULL;
  2138. skb_dirtytx = (skb_dirtytx + 1) &
  2139. TX_RING_MOD_MASK(tx_ring_size);
  2140. howmany++;
  2141. spin_lock_irqsave(&tx_queue->txlock, flags);
  2142. tx_queue->num_txbdfree += nr_txbds;
  2143. spin_unlock_irqrestore(&tx_queue->txlock, flags);
  2144. }
  2145. /* If we freed a buffer, we can restart transmission, if necessary */
  2146. if (tx_queue->num_txbdfree &&
  2147. netif_tx_queue_stopped(txq) &&
  2148. !(test_bit(GFAR_DOWN, &priv->state)))
  2149. netif_wake_subqueue(priv->ndev, tqi);
  2150. /* Update dirty indicators */
  2151. tx_queue->skb_dirtytx = skb_dirtytx;
  2152. tx_queue->dirty_tx = bdp;
  2153. netdev_tx_completed_queue(txq, howmany, bytes_sent);
  2154. }
  2155. static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
  2156. {
  2157. struct gfar_private *priv = netdev_priv(dev);
  2158. struct sk_buff *skb;
  2159. skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
  2160. if (!skb)
  2161. return NULL;
  2162. gfar_align_skb(skb);
  2163. return skb;
  2164. }
  2165. static struct sk_buff *gfar_new_skb(struct net_device *dev, dma_addr_t *bufaddr)
  2166. {
  2167. struct gfar_private *priv = netdev_priv(dev);
  2168. struct sk_buff *skb;
  2169. dma_addr_t addr;
  2170. skb = gfar_alloc_skb(dev);
  2171. if (!skb)
  2172. return NULL;
  2173. addr = dma_map_single(priv->dev, skb->data,
  2174. priv->rx_buffer_size, DMA_FROM_DEVICE);
  2175. if (unlikely(dma_mapping_error(priv->dev, addr))) {
  2176. dev_kfree_skb_any(skb);
  2177. return NULL;
  2178. }
  2179. *bufaddr = addr;
  2180. return skb;
  2181. }
  2182. static inline void count_errors(unsigned short status, struct net_device *dev)
  2183. {
  2184. struct gfar_private *priv = netdev_priv(dev);
  2185. struct net_device_stats *stats = &dev->stats;
  2186. struct gfar_extra_stats *estats = &priv->extra_stats;
  2187. /* If the packet was truncated, none of the other errors matter */
  2188. if (status & RXBD_TRUNCATED) {
  2189. stats->rx_length_errors++;
  2190. atomic64_inc(&estats->rx_trunc);
  2191. return;
  2192. }
  2193. /* Count the errors, if there were any */
  2194. if (status & (RXBD_LARGE | RXBD_SHORT)) {
  2195. stats->rx_length_errors++;
  2196. if (status & RXBD_LARGE)
  2197. atomic64_inc(&estats->rx_large);
  2198. else
  2199. atomic64_inc(&estats->rx_short);
  2200. }
  2201. if (status & RXBD_NONOCTET) {
  2202. stats->rx_frame_errors++;
  2203. atomic64_inc(&estats->rx_nonoctet);
  2204. }
  2205. if (status & RXBD_CRCERR) {
  2206. atomic64_inc(&estats->rx_crcerr);
  2207. stats->rx_crc_errors++;
  2208. }
  2209. if (status & RXBD_OVERRUN) {
  2210. atomic64_inc(&estats->rx_overrun);
  2211. stats->rx_crc_errors++;
  2212. }
  2213. }
  2214. irqreturn_t gfar_receive(int irq, void *grp_id)
  2215. {
  2216. struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
  2217. unsigned long flags;
  2218. u32 imask;
  2219. if (likely(napi_schedule_prep(&grp->napi_rx))) {
  2220. spin_lock_irqsave(&grp->grplock, flags);
  2221. imask = gfar_read(&grp->regs->imask);
  2222. imask &= IMASK_RX_DISABLED;
  2223. gfar_write(&grp->regs->imask, imask);
  2224. spin_unlock_irqrestore(&grp->grplock, flags);
  2225. __napi_schedule(&grp->napi_rx);
  2226. } else {
  2227. /* Clear IEVENT, so interrupts aren't called again
  2228. * because of the packets that have already arrived.
  2229. */
  2230. gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
  2231. }
  2232. return IRQ_HANDLED;
  2233. }
  2234. /* Interrupt Handler for Transmit complete */
  2235. static irqreturn_t gfar_transmit(int irq, void *grp_id)
  2236. {
  2237. struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
  2238. unsigned long flags;
  2239. u32 imask;
  2240. if (likely(napi_schedule_prep(&grp->napi_tx))) {
  2241. spin_lock_irqsave(&grp->grplock, flags);
  2242. imask = gfar_read(&grp->regs->imask);
  2243. imask &= IMASK_TX_DISABLED;
  2244. gfar_write(&grp->regs->imask, imask);
  2245. spin_unlock_irqrestore(&grp->grplock, flags);
  2246. __napi_schedule(&grp->napi_tx);
  2247. } else {
  2248. /* Clear IEVENT, so interrupts aren't called again
  2249. * because of the packets that have already arrived.
  2250. */
  2251. gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
  2252. }
  2253. return IRQ_HANDLED;
  2254. }
  2255. static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
  2256. {
  2257. /* If valid headers were found, and valid sums
  2258. * were verified, then we tell the kernel that no
  2259. * checksumming is necessary. Otherwise, it is [FIXME]
  2260. */
  2261. if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
  2262. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2263. else
  2264. skb_checksum_none_assert(skb);
  2265. }
  2266. /* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
  2267. static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
  2268. int amount_pull, struct napi_struct *napi)
  2269. {
  2270. struct gfar_private *priv = netdev_priv(dev);
  2271. struct rxfcb *fcb = NULL;
  2272. /* fcb is at the beginning if exists */
  2273. fcb = (struct rxfcb *)skb->data;
  2274. /* Remove the FCB from the skb
  2275. * Remove the padded bytes, if there are any
  2276. */
  2277. if (amount_pull) {
  2278. skb_record_rx_queue(skb, fcb->rq);
  2279. skb_pull(skb, amount_pull);
  2280. }
  2281. /* Get receive timestamp from the skb */
  2282. if (priv->hwts_rx_en) {
  2283. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  2284. u64 *ns = (u64 *) skb->data;
  2285. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  2286. shhwtstamps->hwtstamp = ns_to_ktime(*ns);
  2287. }
  2288. if (priv->padding)
  2289. skb_pull(skb, priv->padding);
  2290. if (dev->features & NETIF_F_RXCSUM)
  2291. gfar_rx_checksum(skb, fcb);
  2292. /* Tell the skb what kind of packet this is */
  2293. skb->protocol = eth_type_trans(skb, dev);
  2294. /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
  2295. * Even if vlan rx accel is disabled, on some chips
  2296. * RXFCB_VLN is pseudo randomly set.
  2297. */
  2298. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
  2299. fcb->flags & RXFCB_VLN)
  2300. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
  2301. /* Send the packet up the stack */
  2302. napi_gro_receive(napi, skb);
  2303. }
  2304. /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
  2305. * until the budget/quota has been reached. Returns the number
  2306. * of frames handled
  2307. */
  2308. int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
  2309. {
  2310. struct net_device *dev = rx_queue->dev;
  2311. struct rxbd8 *bdp, *base;
  2312. struct sk_buff *skb;
  2313. int pkt_len;
  2314. int amount_pull;
  2315. int howmany = 0;
  2316. struct gfar_private *priv = netdev_priv(dev);
  2317. /* Get the first full descriptor */
  2318. bdp = rx_queue->cur_rx;
  2319. base = rx_queue->rx_bd_base;
  2320. amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
  2321. while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
  2322. struct sk_buff *newskb;
  2323. dma_addr_t bufaddr;
  2324. rmb();
  2325. /* Add another skb for the future */
  2326. newskb = gfar_new_skb(dev, &bufaddr);
  2327. skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
  2328. dma_unmap_single(priv->dev, bdp->bufPtr,
  2329. priv->rx_buffer_size, DMA_FROM_DEVICE);
  2330. if (unlikely(!(bdp->status & RXBD_ERR) &&
  2331. bdp->length > priv->rx_buffer_size))
  2332. bdp->status = RXBD_LARGE;
  2333. /* We drop the frame if we failed to allocate a new buffer */
  2334. if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
  2335. bdp->status & RXBD_ERR)) {
  2336. count_errors(bdp->status, dev);
  2337. if (unlikely(!newskb)) {
  2338. newskb = skb;
  2339. bufaddr = bdp->bufPtr;
  2340. } else if (skb)
  2341. dev_kfree_skb(skb);
  2342. } else {
  2343. /* Increment the number of packets */
  2344. rx_queue->stats.rx_packets++;
  2345. howmany++;
  2346. if (likely(skb)) {
  2347. pkt_len = bdp->length - ETH_FCS_LEN;
  2348. /* Remove the FCS from the packet length */
  2349. skb_put(skb, pkt_len);
  2350. rx_queue->stats.rx_bytes += pkt_len;
  2351. skb_record_rx_queue(skb, rx_queue->qindex);
  2352. gfar_process_frame(dev, skb, amount_pull,
  2353. &rx_queue->grp->napi_rx);
  2354. } else {
  2355. netif_warn(priv, rx_err, dev, "Missing skb!\n");
  2356. rx_queue->stats.rx_dropped++;
  2357. atomic64_inc(&priv->extra_stats.rx_skbmissing);
  2358. }
  2359. }
  2360. rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
  2361. /* Setup the new bdp */
  2362. gfar_init_rxbdp(rx_queue, bdp, bufaddr);
  2363. /* Update Last Free RxBD pointer for LFC */
  2364. if (unlikely(rx_queue->rfbptr && priv->tx_actual_en))
  2365. gfar_write(rx_queue->rfbptr, (u32)bdp);
  2366. /* Update to the next pointer */
  2367. bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
  2368. /* update to point at the next skb */
  2369. rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
  2370. RX_RING_MOD_MASK(rx_queue->rx_ring_size);
  2371. }
  2372. /* Update the current rxbd pointer to be the next one */
  2373. rx_queue->cur_rx = bdp;
  2374. return howmany;
  2375. }
  2376. static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
  2377. {
  2378. struct gfar_priv_grp *gfargrp =
  2379. container_of(napi, struct gfar_priv_grp, napi_rx);
  2380. struct gfar __iomem *regs = gfargrp->regs;
  2381. struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
  2382. int work_done = 0;
  2383. /* Clear IEVENT, so interrupts aren't called again
  2384. * because of the packets that have already arrived
  2385. */
  2386. gfar_write(&regs->ievent, IEVENT_RX_MASK);
  2387. work_done = gfar_clean_rx_ring(rx_queue, budget);
  2388. if (work_done < budget) {
  2389. u32 imask;
  2390. napi_complete(napi);
  2391. /* Clear the halt bit in RSTAT */
  2392. gfar_write(&regs->rstat, gfargrp->rstat);
  2393. spin_lock_irq(&gfargrp->grplock);
  2394. imask = gfar_read(&regs->imask);
  2395. imask |= IMASK_RX_DEFAULT;
  2396. gfar_write(&regs->imask, imask);
  2397. spin_unlock_irq(&gfargrp->grplock);
  2398. }
  2399. return work_done;
  2400. }
  2401. static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
  2402. {
  2403. struct gfar_priv_grp *gfargrp =
  2404. container_of(napi, struct gfar_priv_grp, napi_tx);
  2405. struct gfar __iomem *regs = gfargrp->regs;
  2406. struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
  2407. u32 imask;
  2408. /* Clear IEVENT, so interrupts aren't called again
  2409. * because of the packets that have already arrived
  2410. */
  2411. gfar_write(&regs->ievent, IEVENT_TX_MASK);
  2412. /* run Tx cleanup to completion */
  2413. if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
  2414. gfar_clean_tx_ring(tx_queue);
  2415. napi_complete(napi);
  2416. spin_lock_irq(&gfargrp->grplock);
  2417. imask = gfar_read(&regs->imask);
  2418. imask |= IMASK_TX_DEFAULT;
  2419. gfar_write(&regs->imask, imask);
  2420. spin_unlock_irq(&gfargrp->grplock);
  2421. return 0;
  2422. }
  2423. static int gfar_poll_rx(struct napi_struct *napi, int budget)
  2424. {
  2425. struct gfar_priv_grp *gfargrp =
  2426. container_of(napi, struct gfar_priv_grp, napi_rx);
  2427. struct gfar_private *priv = gfargrp->priv;
  2428. struct gfar __iomem *regs = gfargrp->regs;
  2429. struct gfar_priv_rx_q *rx_queue = NULL;
  2430. int work_done = 0, work_done_per_q = 0;
  2431. int i, budget_per_q = 0;
  2432. unsigned long rstat_rxf;
  2433. int num_act_queues;
  2434. /* Clear IEVENT, so interrupts aren't called again
  2435. * because of the packets that have already arrived
  2436. */
  2437. gfar_write(&regs->ievent, IEVENT_RX_MASK);
  2438. rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
  2439. num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
  2440. if (num_act_queues)
  2441. budget_per_q = budget/num_act_queues;
  2442. for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
  2443. /* skip queue if not active */
  2444. if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
  2445. continue;
  2446. rx_queue = priv->rx_queue[i];
  2447. work_done_per_q =
  2448. gfar_clean_rx_ring(rx_queue, budget_per_q);
  2449. work_done += work_done_per_q;
  2450. /* finished processing this queue */
  2451. if (work_done_per_q < budget_per_q) {
  2452. /* clear active queue hw indication */
  2453. gfar_write(&regs->rstat,
  2454. RSTAT_CLEAR_RXF0 >> i);
  2455. num_act_queues--;
  2456. if (!num_act_queues)
  2457. break;
  2458. }
  2459. }
  2460. if (!num_act_queues) {
  2461. u32 imask;
  2462. napi_complete(napi);
  2463. /* Clear the halt bit in RSTAT */
  2464. gfar_write(&regs->rstat, gfargrp->rstat);
  2465. spin_lock_irq(&gfargrp->grplock);
  2466. imask = gfar_read(&regs->imask);
  2467. imask |= IMASK_RX_DEFAULT;
  2468. gfar_write(&regs->imask, imask);
  2469. spin_unlock_irq(&gfargrp->grplock);
  2470. }
  2471. return work_done;
  2472. }
  2473. static int gfar_poll_tx(struct napi_struct *napi, int budget)
  2474. {
  2475. struct gfar_priv_grp *gfargrp =
  2476. container_of(napi, struct gfar_priv_grp, napi_tx);
  2477. struct gfar_private *priv = gfargrp->priv;
  2478. struct gfar __iomem *regs = gfargrp->regs;
  2479. struct gfar_priv_tx_q *tx_queue = NULL;
  2480. int has_tx_work = 0;
  2481. int i;
  2482. /* Clear IEVENT, so interrupts aren't called again
  2483. * because of the packets that have already arrived
  2484. */
  2485. gfar_write(&regs->ievent, IEVENT_TX_MASK);
  2486. for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
  2487. tx_queue = priv->tx_queue[i];
  2488. /* run Tx cleanup to completion */
  2489. if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
  2490. gfar_clean_tx_ring(tx_queue);
  2491. has_tx_work = 1;
  2492. }
  2493. }
  2494. if (!has_tx_work) {
  2495. u32 imask;
  2496. napi_complete(napi);
  2497. spin_lock_irq(&gfargrp->grplock);
  2498. imask = gfar_read(&regs->imask);
  2499. imask |= IMASK_TX_DEFAULT;
  2500. gfar_write(&regs->imask, imask);
  2501. spin_unlock_irq(&gfargrp->grplock);
  2502. }
  2503. return 0;
  2504. }
  2505. #ifdef CONFIG_NET_POLL_CONTROLLER
  2506. /* Polling 'interrupt' - used by things like netconsole to send skbs
  2507. * without having to re-enable interrupts. It's not called while
  2508. * the interrupt routine is executing.
  2509. */
  2510. static void gfar_netpoll(struct net_device *dev)
  2511. {
  2512. struct gfar_private *priv = netdev_priv(dev);
  2513. int i;
  2514. /* If the device has multiple interrupts, run tx/rx */
  2515. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2516. for (i = 0; i < priv->num_grps; i++) {
  2517. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  2518. disable_irq(gfar_irq(grp, TX)->irq);
  2519. disable_irq(gfar_irq(grp, RX)->irq);
  2520. disable_irq(gfar_irq(grp, ER)->irq);
  2521. gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
  2522. enable_irq(gfar_irq(grp, ER)->irq);
  2523. enable_irq(gfar_irq(grp, RX)->irq);
  2524. enable_irq(gfar_irq(grp, TX)->irq);
  2525. }
  2526. } else {
  2527. for (i = 0; i < priv->num_grps; i++) {
  2528. struct gfar_priv_grp *grp = &priv->gfargrp[i];
  2529. disable_irq(gfar_irq(grp, TX)->irq);
  2530. gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
  2531. enable_irq(gfar_irq(grp, TX)->irq);
  2532. }
  2533. }
  2534. }
  2535. #endif
  2536. /* The interrupt handler for devices with one interrupt */
  2537. static irqreturn_t gfar_interrupt(int irq, void *grp_id)
  2538. {
  2539. struct gfar_priv_grp *gfargrp = grp_id;
  2540. /* Save ievent for future reference */
  2541. u32 events = gfar_read(&gfargrp->regs->ievent);
  2542. /* Check for reception */
  2543. if (events & IEVENT_RX_MASK)
  2544. gfar_receive(irq, grp_id);
  2545. /* Check for transmit completion */
  2546. if (events & IEVENT_TX_MASK)
  2547. gfar_transmit(irq, grp_id);
  2548. /* Check for errors */
  2549. if (events & IEVENT_ERR_MASK)
  2550. gfar_error(irq, grp_id);
  2551. return IRQ_HANDLED;
  2552. }
  2553. /* Called every time the controller might need to be made
  2554. * aware of new link state. The PHY code conveys this
  2555. * information through variables in the phydev structure, and this
  2556. * function converts those variables into the appropriate
  2557. * register values, and can bring down the device if needed.
  2558. */
  2559. static void adjust_link(struct net_device *dev)
  2560. {
  2561. struct gfar_private *priv = netdev_priv(dev);
  2562. struct phy_device *phydev = priv->phydev;
  2563. if (unlikely(phydev->link != priv->oldlink ||
  2564. phydev->duplex != priv->oldduplex ||
  2565. phydev->speed != priv->oldspeed))
  2566. gfar_update_link_state(priv);
  2567. }
  2568. /* Update the hash table based on the current list of multicast
  2569. * addresses we subscribe to. Also, change the promiscuity of
  2570. * the device based on the flags (this function is called
  2571. * whenever dev->flags is changed
  2572. */
  2573. static void gfar_set_multi(struct net_device *dev)
  2574. {
  2575. struct netdev_hw_addr *ha;
  2576. struct gfar_private *priv = netdev_priv(dev);
  2577. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2578. u32 tempval;
  2579. if (dev->flags & IFF_PROMISC) {
  2580. /* Set RCTRL to PROM */
  2581. tempval = gfar_read(&regs->rctrl);
  2582. tempval |= RCTRL_PROM;
  2583. gfar_write(&regs->rctrl, tempval);
  2584. } else {
  2585. /* Set RCTRL to not PROM */
  2586. tempval = gfar_read(&regs->rctrl);
  2587. tempval &= ~(RCTRL_PROM);
  2588. gfar_write(&regs->rctrl, tempval);
  2589. }
  2590. if (dev->flags & IFF_ALLMULTI) {
  2591. /* Set the hash to rx all multicast frames */
  2592. gfar_write(&regs->igaddr0, 0xffffffff);
  2593. gfar_write(&regs->igaddr1, 0xffffffff);
  2594. gfar_write(&regs->igaddr2, 0xffffffff);
  2595. gfar_write(&regs->igaddr3, 0xffffffff);
  2596. gfar_write(&regs->igaddr4, 0xffffffff);
  2597. gfar_write(&regs->igaddr5, 0xffffffff);
  2598. gfar_write(&regs->igaddr6, 0xffffffff);
  2599. gfar_write(&regs->igaddr7, 0xffffffff);
  2600. gfar_write(&regs->gaddr0, 0xffffffff);
  2601. gfar_write(&regs->gaddr1, 0xffffffff);
  2602. gfar_write(&regs->gaddr2, 0xffffffff);
  2603. gfar_write(&regs->gaddr3, 0xffffffff);
  2604. gfar_write(&regs->gaddr4, 0xffffffff);
  2605. gfar_write(&regs->gaddr5, 0xffffffff);
  2606. gfar_write(&regs->gaddr6, 0xffffffff);
  2607. gfar_write(&regs->gaddr7, 0xffffffff);
  2608. } else {
  2609. int em_num;
  2610. int idx;
  2611. /* zero out the hash */
  2612. gfar_write(&regs->igaddr0, 0x0);
  2613. gfar_write(&regs->igaddr1, 0x0);
  2614. gfar_write(&regs->igaddr2, 0x0);
  2615. gfar_write(&regs->igaddr3, 0x0);
  2616. gfar_write(&regs->igaddr4, 0x0);
  2617. gfar_write(&regs->igaddr5, 0x0);
  2618. gfar_write(&regs->igaddr6, 0x0);
  2619. gfar_write(&regs->igaddr7, 0x0);
  2620. gfar_write(&regs->gaddr0, 0x0);
  2621. gfar_write(&regs->gaddr1, 0x0);
  2622. gfar_write(&regs->gaddr2, 0x0);
  2623. gfar_write(&regs->gaddr3, 0x0);
  2624. gfar_write(&regs->gaddr4, 0x0);
  2625. gfar_write(&regs->gaddr5, 0x0);
  2626. gfar_write(&regs->gaddr6, 0x0);
  2627. gfar_write(&regs->gaddr7, 0x0);
  2628. /* If we have extended hash tables, we need to
  2629. * clear the exact match registers to prepare for
  2630. * setting them
  2631. */
  2632. if (priv->extended_hash) {
  2633. em_num = GFAR_EM_NUM + 1;
  2634. gfar_clear_exact_match(dev);
  2635. idx = 1;
  2636. } else {
  2637. idx = 0;
  2638. em_num = 0;
  2639. }
  2640. if (netdev_mc_empty(dev))
  2641. return;
  2642. /* Parse the list, and set the appropriate bits */
  2643. netdev_for_each_mc_addr(ha, dev) {
  2644. if (idx < em_num) {
  2645. gfar_set_mac_for_addr(dev, idx, ha->addr);
  2646. idx++;
  2647. } else
  2648. gfar_set_hash_for_addr(dev, ha->addr);
  2649. }
  2650. }
  2651. }
  2652. /* Clears each of the exact match registers to zero, so they
  2653. * don't interfere with normal reception
  2654. */
  2655. static void gfar_clear_exact_match(struct net_device *dev)
  2656. {
  2657. int idx;
  2658. static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  2659. for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
  2660. gfar_set_mac_for_addr(dev, idx, zero_arr);
  2661. }
  2662. /* Set the appropriate hash bit for the given addr */
  2663. /* The algorithm works like so:
  2664. * 1) Take the Destination Address (ie the multicast address), and
  2665. * do a CRC on it (little endian), and reverse the bits of the
  2666. * result.
  2667. * 2) Use the 8 most significant bits as a hash into a 256-entry
  2668. * table. The table is controlled through 8 32-bit registers:
  2669. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  2670. * gaddr7. This means that the 3 most significant bits in the
  2671. * hash index which gaddr register to use, and the 5 other bits
  2672. * indicate which bit (assuming an IBM numbering scheme, which
  2673. * for PowerPC (tm) is usually the case) in the register holds
  2674. * the entry.
  2675. */
  2676. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
  2677. {
  2678. u32 tempval;
  2679. struct gfar_private *priv = netdev_priv(dev);
  2680. u32 result = ether_crc(ETH_ALEN, addr);
  2681. int width = priv->hash_width;
  2682. u8 whichbit = (result >> (32 - width)) & 0x1f;
  2683. u8 whichreg = result >> (32 - width + 5);
  2684. u32 value = (1 << (31-whichbit));
  2685. tempval = gfar_read(priv->hash_regs[whichreg]);
  2686. tempval |= value;
  2687. gfar_write(priv->hash_regs[whichreg], tempval);
  2688. }
  2689. /* There are multiple MAC Address register pairs on some controllers
  2690. * This function sets the numth pair to a given address
  2691. */
  2692. static void gfar_set_mac_for_addr(struct net_device *dev, int num,
  2693. const u8 *addr)
  2694. {
  2695. struct gfar_private *priv = netdev_priv(dev);
  2696. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2697. u32 tempval;
  2698. u32 __iomem *macptr = &regs->macstnaddr1;
  2699. macptr += num*2;
  2700. /* For a station address of 0x12345678ABCD in transmission
  2701. * order (BE), MACnADDR1 is set to 0xCDAB7856 and
  2702. * MACnADDR2 is set to 0x34120000.
  2703. */
  2704. tempval = (addr[5] << 24) | (addr[4] << 16) |
  2705. (addr[3] << 8) | addr[2];
  2706. gfar_write(macptr, tempval);
  2707. tempval = (addr[1] << 24) | (addr[0] << 16);
  2708. gfar_write(macptr+1, tempval);
  2709. }
  2710. /* GFAR error interrupt handler */
  2711. static irqreturn_t gfar_error(int irq, void *grp_id)
  2712. {
  2713. struct gfar_priv_grp *gfargrp = grp_id;
  2714. struct gfar __iomem *regs = gfargrp->regs;
  2715. struct gfar_private *priv= gfargrp->priv;
  2716. struct net_device *dev = priv->ndev;
  2717. /* Save ievent for future reference */
  2718. u32 events = gfar_read(&regs->ievent);
  2719. /* Clear IEVENT */
  2720. gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
  2721. /* Magic Packet is not an error. */
  2722. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  2723. (events & IEVENT_MAG))
  2724. events &= ~IEVENT_MAG;
  2725. /* Hmm... */
  2726. if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
  2727. netdev_dbg(dev,
  2728. "error interrupt (ievent=0x%08x imask=0x%08x)\n",
  2729. events, gfar_read(&regs->imask));
  2730. /* Update the error counters */
  2731. if (events & IEVENT_TXE) {
  2732. dev->stats.tx_errors++;
  2733. if (events & IEVENT_LC)
  2734. dev->stats.tx_window_errors++;
  2735. if (events & IEVENT_CRL)
  2736. dev->stats.tx_aborted_errors++;
  2737. if (events & IEVENT_XFUN) {
  2738. unsigned long flags;
  2739. netif_dbg(priv, tx_err, dev,
  2740. "TX FIFO underrun, packet dropped\n");
  2741. dev->stats.tx_dropped++;
  2742. atomic64_inc(&priv->extra_stats.tx_underrun);
  2743. local_irq_save(flags);
  2744. lock_tx_qs(priv);
  2745. /* Reactivate the Tx Queues */
  2746. gfar_write(&regs->tstat, gfargrp->tstat);
  2747. unlock_tx_qs(priv);
  2748. local_irq_restore(flags);
  2749. }
  2750. netif_dbg(priv, tx_err, dev, "Transmit Error\n");
  2751. }
  2752. if (events & IEVENT_BSY) {
  2753. dev->stats.rx_errors++;
  2754. atomic64_inc(&priv->extra_stats.rx_bsy);
  2755. gfar_receive(irq, grp_id);
  2756. netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
  2757. gfar_read(&regs->rstat));
  2758. }
  2759. if (events & IEVENT_BABR) {
  2760. dev->stats.rx_errors++;
  2761. atomic64_inc(&priv->extra_stats.rx_babr);
  2762. netif_dbg(priv, rx_err, dev, "babbling RX error\n");
  2763. }
  2764. if (events & IEVENT_EBERR) {
  2765. atomic64_inc(&priv->extra_stats.eberr);
  2766. netif_dbg(priv, rx_err, dev, "bus error\n");
  2767. }
  2768. if (events & IEVENT_RXC)
  2769. netif_dbg(priv, rx_status, dev, "control frame\n");
  2770. if (events & IEVENT_BABT) {
  2771. atomic64_inc(&priv->extra_stats.tx_babt);
  2772. netif_dbg(priv, tx_err, dev, "babbling TX error\n");
  2773. }
  2774. return IRQ_HANDLED;
  2775. }
  2776. static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
  2777. {
  2778. struct phy_device *phydev = priv->phydev;
  2779. u32 val = 0;
  2780. if (!phydev->duplex)
  2781. return val;
  2782. if (!priv->pause_aneg_en) {
  2783. if (priv->tx_pause_en)
  2784. val |= MACCFG1_TX_FLOW;
  2785. if (priv->rx_pause_en)
  2786. val |= MACCFG1_RX_FLOW;
  2787. } else {
  2788. u16 lcl_adv, rmt_adv;
  2789. u8 flowctrl;
  2790. /* get link partner capabilities */
  2791. rmt_adv = 0;
  2792. if (phydev->pause)
  2793. rmt_adv = LPA_PAUSE_CAP;
  2794. if (phydev->asym_pause)
  2795. rmt_adv |= LPA_PAUSE_ASYM;
  2796. lcl_adv = 0;
  2797. if (phydev->advertising & ADVERTISED_Pause)
  2798. lcl_adv |= ADVERTISE_PAUSE_CAP;
  2799. if (phydev->advertising & ADVERTISED_Asym_Pause)
  2800. lcl_adv |= ADVERTISE_PAUSE_ASYM;
  2801. flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
  2802. if (flowctrl & FLOW_CTRL_TX)
  2803. val |= MACCFG1_TX_FLOW;
  2804. if (flowctrl & FLOW_CTRL_RX)
  2805. val |= MACCFG1_RX_FLOW;
  2806. }
  2807. return val;
  2808. }
  2809. static noinline void gfar_update_link_state(struct gfar_private *priv)
  2810. {
  2811. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2812. struct phy_device *phydev = priv->phydev;
  2813. struct gfar_priv_rx_q *rx_queue = NULL;
  2814. int i;
  2815. struct rxbd8 *bdp;
  2816. if (unlikely(test_bit(GFAR_RESETTING, &priv->state)))
  2817. return;
  2818. if (phydev->link) {
  2819. u32 tempval1 = gfar_read(&regs->maccfg1);
  2820. u32 tempval = gfar_read(&regs->maccfg2);
  2821. u32 ecntrl = gfar_read(&regs->ecntrl);
  2822. u32 tx_flow_oldval = (tempval & MACCFG1_TX_FLOW);
  2823. if (phydev->duplex != priv->oldduplex) {
  2824. if (!(phydev->duplex))
  2825. tempval &= ~(MACCFG2_FULL_DUPLEX);
  2826. else
  2827. tempval |= MACCFG2_FULL_DUPLEX;
  2828. priv->oldduplex = phydev->duplex;
  2829. }
  2830. if (phydev->speed != priv->oldspeed) {
  2831. switch (phydev->speed) {
  2832. case 1000:
  2833. tempval =
  2834. ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
  2835. ecntrl &= ~(ECNTRL_R100);
  2836. break;
  2837. case 100:
  2838. case 10:
  2839. tempval =
  2840. ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
  2841. /* Reduced mode distinguishes
  2842. * between 10 and 100
  2843. */
  2844. if (phydev->speed == SPEED_100)
  2845. ecntrl |= ECNTRL_R100;
  2846. else
  2847. ecntrl &= ~(ECNTRL_R100);
  2848. break;
  2849. default:
  2850. netif_warn(priv, link, priv->ndev,
  2851. "Ack! Speed (%d) is not 10/100/1000!\n",
  2852. phydev->speed);
  2853. break;
  2854. }
  2855. priv->oldspeed = phydev->speed;
  2856. }
  2857. tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  2858. tempval1 |= gfar_get_flowctrl_cfg(priv);
  2859. /* Turn last free buffer recording on */
  2860. if ((tempval1 & MACCFG1_TX_FLOW) && !tx_flow_oldval) {
  2861. for (i = 0; i < priv->num_rx_queues; i++) {
  2862. rx_queue = priv->rx_queue[i];
  2863. bdp = rx_queue->cur_rx;
  2864. /* skip to previous bd */
  2865. bdp = skip_bd(bdp, rx_queue->rx_ring_size - 1,
  2866. rx_queue->rx_bd_base,
  2867. rx_queue->rx_ring_size);
  2868. if (rx_queue->rfbptr)
  2869. gfar_write(rx_queue->rfbptr, (u32)bdp);
  2870. }
  2871. priv->tx_actual_en = 1;
  2872. }
  2873. if (unlikely(!(tempval1 & MACCFG1_TX_FLOW) && tx_flow_oldval))
  2874. priv->tx_actual_en = 0;
  2875. gfar_write(&regs->maccfg1, tempval1);
  2876. gfar_write(&regs->maccfg2, tempval);
  2877. gfar_write(&regs->ecntrl, ecntrl);
  2878. if (!priv->oldlink)
  2879. priv->oldlink = 1;
  2880. } else if (priv->oldlink) {
  2881. priv->oldlink = 0;
  2882. priv->oldspeed = 0;
  2883. priv->oldduplex = -1;
  2884. }
  2885. if (netif_msg_link(priv))
  2886. phy_print_status(phydev);
  2887. }
  2888. static struct of_device_id gfar_match[] =
  2889. {
  2890. {
  2891. .type = "network",
  2892. .compatible = "gianfar",
  2893. },
  2894. {
  2895. .compatible = "fsl,etsec2",
  2896. },
  2897. {},
  2898. };
  2899. MODULE_DEVICE_TABLE(of, gfar_match);
  2900. /* Structure for a device driver */
  2901. static struct platform_driver gfar_driver = {
  2902. .driver = {
  2903. .name = "fsl-gianfar",
  2904. .pm = GFAR_PM_OPS,
  2905. .of_match_table = gfar_match,
  2906. },
  2907. .probe = gfar_probe,
  2908. .remove = gfar_remove,
  2909. };
  2910. module_platform_driver(gfar_driver);