gianfar.c 86 KB

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