gianfar.c 86 KB

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