gianfar.c 86 KB

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