gianfar.c 87 KB

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