gianfar.c 97 KB

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