gianfar.c 89 KB

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