gianfar.c 88 KB

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