gianfar.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308
  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);
  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. int len_devname;
  812. u32 rstat = 0, tstat = 0, rqueue = 0, tqueue = 0;
  813. u32 isrg = 0;
  814. u32 __iomem *baddr;
  815. err = gfar_of_init(ofdev, &dev);
  816. if (err)
  817. return err;
  818. priv = netdev_priv(dev);
  819. priv->ndev = dev;
  820. priv->ofdev = ofdev;
  821. priv->node = ofdev->dev.of_node;
  822. SET_NETDEV_DEV(dev, &ofdev->dev);
  823. spin_lock_init(&priv->bflock);
  824. INIT_WORK(&priv->reset_task, gfar_reset_task);
  825. dev_set_drvdata(&ofdev->dev, priv);
  826. regs = priv->gfargrp[0].regs;
  827. gfar_detect_errata(priv);
  828. /* Stop the DMA engine now, in case it was running before */
  829. /* (The firmware could have used it, and left it running). */
  830. gfar_halt(dev);
  831. /* Reset MAC layer */
  832. gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
  833. /* We need to delay at least 3 TX clocks */
  834. udelay(2);
  835. tempval = (MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
  836. gfar_write(&regs->maccfg1, tempval);
  837. /* Initialize MACCFG2. */
  838. tempval = MACCFG2_INIT_SETTINGS;
  839. if (gfar_has_errata(priv, GFAR_ERRATA_74))
  840. tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
  841. gfar_write(&regs->maccfg2, tempval);
  842. /* Initialize ECNTRL */
  843. gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
  844. /* Set the dev->base_addr to the gfar reg region */
  845. dev->base_addr = (unsigned long) regs;
  846. SET_NETDEV_DEV(dev, &ofdev->dev);
  847. /* Fill in the dev structure */
  848. dev->watchdog_timeo = TX_TIMEOUT;
  849. dev->mtu = 1500;
  850. dev->netdev_ops = &gfar_netdev_ops;
  851. dev->ethtool_ops = &gfar_ethtool_ops;
  852. /* Register for napi ...We are registering NAPI for each grp */
  853. for (i = 0; i < priv->num_grps; i++)
  854. netif_napi_add(dev, &priv->gfargrp[i].napi, gfar_poll, GFAR_DEV_WEIGHT);
  855. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
  856. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
  857. NETIF_F_RXCSUM;
  858. dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
  859. NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
  860. }
  861. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
  862. dev->hw_features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  863. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  864. }
  865. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
  866. priv->extended_hash = 1;
  867. priv->hash_width = 9;
  868. priv->hash_regs[0] = &regs->igaddr0;
  869. priv->hash_regs[1] = &regs->igaddr1;
  870. priv->hash_regs[2] = &regs->igaddr2;
  871. priv->hash_regs[3] = &regs->igaddr3;
  872. priv->hash_regs[4] = &regs->igaddr4;
  873. priv->hash_regs[5] = &regs->igaddr5;
  874. priv->hash_regs[6] = &regs->igaddr6;
  875. priv->hash_regs[7] = &regs->igaddr7;
  876. priv->hash_regs[8] = &regs->gaddr0;
  877. priv->hash_regs[9] = &regs->gaddr1;
  878. priv->hash_regs[10] = &regs->gaddr2;
  879. priv->hash_regs[11] = &regs->gaddr3;
  880. priv->hash_regs[12] = &regs->gaddr4;
  881. priv->hash_regs[13] = &regs->gaddr5;
  882. priv->hash_regs[14] = &regs->gaddr6;
  883. priv->hash_regs[15] = &regs->gaddr7;
  884. } else {
  885. priv->extended_hash = 0;
  886. priv->hash_width = 8;
  887. priv->hash_regs[0] = &regs->gaddr0;
  888. priv->hash_regs[1] = &regs->gaddr1;
  889. priv->hash_regs[2] = &regs->gaddr2;
  890. priv->hash_regs[3] = &regs->gaddr3;
  891. priv->hash_regs[4] = &regs->gaddr4;
  892. priv->hash_regs[5] = &regs->gaddr5;
  893. priv->hash_regs[6] = &regs->gaddr6;
  894. priv->hash_regs[7] = &regs->gaddr7;
  895. }
  896. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_PADDING)
  897. priv->padding = DEFAULT_PADDING;
  898. else
  899. priv->padding = 0;
  900. if (dev->features & NETIF_F_IP_CSUM ||
  901. priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
  902. dev->hard_header_len += GMAC_FCB_LEN;
  903. /* Program the isrg regs only if number of grps > 1 */
  904. if (priv->num_grps > 1) {
  905. baddr = &regs->isrg0;
  906. for (i = 0; i < priv->num_grps; i++) {
  907. isrg |= (priv->gfargrp[i].rx_bit_map << ISRG_SHIFT_RX);
  908. isrg |= (priv->gfargrp[i].tx_bit_map << ISRG_SHIFT_TX);
  909. gfar_write(baddr, isrg);
  910. baddr++;
  911. isrg = 0x0;
  912. }
  913. }
  914. /* Need to reverse the bit maps as bit_map's MSB is q0
  915. * but, for_each_set_bit parses from right to left, which
  916. * basically reverses the queue numbers */
  917. for (i = 0; i< priv->num_grps; i++) {
  918. priv->gfargrp[i].tx_bit_map = reverse_bitmap(
  919. priv->gfargrp[i].tx_bit_map, MAX_TX_QS);
  920. priv->gfargrp[i].rx_bit_map = reverse_bitmap(
  921. priv->gfargrp[i].rx_bit_map, MAX_RX_QS);
  922. }
  923. /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
  924. * also assign queues to groups */
  925. for (grp_idx = 0; grp_idx < priv->num_grps; grp_idx++) {
  926. priv->gfargrp[grp_idx].num_rx_queues = 0x0;
  927. for_each_set_bit(i, &priv->gfargrp[grp_idx].rx_bit_map,
  928. priv->num_rx_queues) {
  929. priv->gfargrp[grp_idx].num_rx_queues++;
  930. priv->rx_queue[i]->grp = &priv->gfargrp[grp_idx];
  931. rstat = rstat | (RSTAT_CLEAR_RHALT >> i);
  932. rqueue = rqueue | ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
  933. }
  934. priv->gfargrp[grp_idx].num_tx_queues = 0x0;
  935. for_each_set_bit(i, &priv->gfargrp[grp_idx].tx_bit_map,
  936. priv->num_tx_queues) {
  937. priv->gfargrp[grp_idx].num_tx_queues++;
  938. priv->tx_queue[i]->grp = &priv->gfargrp[grp_idx];
  939. tstat = tstat | (TSTAT_CLEAR_THALT >> i);
  940. tqueue = tqueue | (TQUEUE_EN0 >> i);
  941. }
  942. priv->gfargrp[grp_idx].rstat = rstat;
  943. priv->gfargrp[grp_idx].tstat = tstat;
  944. rstat = tstat =0;
  945. }
  946. gfar_write(&regs->rqueue, rqueue);
  947. gfar_write(&regs->tqueue, tqueue);
  948. priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
  949. /* Initializing some of the rx/tx queue level parameters */
  950. for (i = 0; i < priv->num_tx_queues; i++) {
  951. priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
  952. priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
  953. priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
  954. priv->tx_queue[i]->txic = DEFAULT_TXIC;
  955. }
  956. for (i = 0; i < priv->num_rx_queues; i++) {
  957. priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
  958. priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
  959. priv->rx_queue[i]->rxic = DEFAULT_RXIC;
  960. }
  961. /* always enable rx filer*/
  962. priv->rx_filer_enable = 1;
  963. /* Enable most messages by default */
  964. priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
  965. /* Carrier starts down, phylib will bring it up */
  966. netif_carrier_off(dev);
  967. err = register_netdev(dev);
  968. if (err) {
  969. pr_err("%s: Cannot register net device, aborting\n", dev->name);
  970. goto register_fail;
  971. }
  972. device_init_wakeup(&dev->dev,
  973. priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  974. /* fill out IRQ number and name fields */
  975. len_devname = strlen(dev->name);
  976. for (i = 0; i < priv->num_grps; i++) {
  977. strncpy(&priv->gfargrp[i].int_name_tx[0], dev->name,
  978. len_devname);
  979. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  980. strncpy(&priv->gfargrp[i].int_name_tx[len_devname],
  981. "_g", sizeof("_g"));
  982. priv->gfargrp[i].int_name_tx[
  983. strlen(priv->gfargrp[i].int_name_tx)] = i+48;
  984. strncpy(&priv->gfargrp[i].int_name_tx[strlen(
  985. priv->gfargrp[i].int_name_tx)],
  986. "_tx", sizeof("_tx") + 1);
  987. strncpy(&priv->gfargrp[i].int_name_rx[0], dev->name,
  988. len_devname);
  989. strncpy(&priv->gfargrp[i].int_name_rx[len_devname],
  990. "_g", sizeof("_g"));
  991. priv->gfargrp[i].int_name_rx[
  992. strlen(priv->gfargrp[i].int_name_rx)] = i+48;
  993. strncpy(&priv->gfargrp[i].int_name_rx[strlen(
  994. priv->gfargrp[i].int_name_rx)],
  995. "_rx", sizeof("_rx") + 1);
  996. strncpy(&priv->gfargrp[i].int_name_er[0], dev->name,
  997. len_devname);
  998. strncpy(&priv->gfargrp[i].int_name_er[len_devname],
  999. "_g", sizeof("_g"));
  1000. priv->gfargrp[i].int_name_er[strlen(
  1001. priv->gfargrp[i].int_name_er)] = i+48;
  1002. strncpy(&priv->gfargrp[i].int_name_er[strlen(\
  1003. priv->gfargrp[i].int_name_er)],
  1004. "_er", sizeof("_er") + 1);
  1005. } else
  1006. priv->gfargrp[i].int_name_tx[len_devname] = '\0';
  1007. }
  1008. /* Initialize the filer table */
  1009. gfar_init_filer_table(priv);
  1010. /* Create all the sysfs files */
  1011. gfar_init_sysfs(dev);
  1012. /* Print out the device info */
  1013. netdev_info(dev, "mac: %pM\n", dev->dev_addr);
  1014. /* Even more device info helps when determining which kernel */
  1015. /* provided which set of benchmarks. */
  1016. netdev_info(dev, "Running with NAPI enabled\n");
  1017. for (i = 0; i < priv->num_rx_queues; i++)
  1018. netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
  1019. i, priv->rx_queue[i]->rx_ring_size);
  1020. for(i = 0; i < priv->num_tx_queues; i++)
  1021. netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
  1022. i, priv->tx_queue[i]->tx_ring_size);
  1023. return 0;
  1024. register_fail:
  1025. unmap_group_regs(priv);
  1026. free_tx_pointers(priv);
  1027. free_rx_pointers(priv);
  1028. if (priv->phy_node)
  1029. of_node_put(priv->phy_node);
  1030. if (priv->tbi_node)
  1031. of_node_put(priv->tbi_node);
  1032. free_netdev(dev);
  1033. return err;
  1034. }
  1035. static int gfar_remove(struct platform_device *ofdev)
  1036. {
  1037. struct gfar_private *priv = dev_get_drvdata(&ofdev->dev);
  1038. if (priv->phy_node)
  1039. of_node_put(priv->phy_node);
  1040. if (priv->tbi_node)
  1041. of_node_put(priv->tbi_node);
  1042. dev_set_drvdata(&ofdev->dev, NULL);
  1043. unregister_netdev(priv->ndev);
  1044. unmap_group_regs(priv);
  1045. free_netdev(priv->ndev);
  1046. return 0;
  1047. }
  1048. #ifdef CONFIG_PM
  1049. static int gfar_suspend(struct device *dev)
  1050. {
  1051. struct gfar_private *priv = dev_get_drvdata(dev);
  1052. struct net_device *ndev = priv->ndev;
  1053. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1054. unsigned long flags;
  1055. u32 tempval;
  1056. int magic_packet = priv->wol_en &&
  1057. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  1058. netif_device_detach(ndev);
  1059. if (netif_running(ndev)) {
  1060. local_irq_save(flags);
  1061. lock_tx_qs(priv);
  1062. lock_rx_qs(priv);
  1063. gfar_halt_nodisable(ndev);
  1064. /* Disable Tx, and Rx if wake-on-LAN is disabled. */
  1065. tempval = gfar_read(&regs->maccfg1);
  1066. tempval &= ~MACCFG1_TX_EN;
  1067. if (!magic_packet)
  1068. tempval &= ~MACCFG1_RX_EN;
  1069. gfar_write(&regs->maccfg1, tempval);
  1070. unlock_rx_qs(priv);
  1071. unlock_tx_qs(priv);
  1072. local_irq_restore(flags);
  1073. disable_napi(priv);
  1074. if (magic_packet) {
  1075. /* Enable interrupt on Magic Packet */
  1076. gfar_write(&regs->imask, IMASK_MAG);
  1077. /* Enable Magic Packet mode */
  1078. tempval = gfar_read(&regs->maccfg2);
  1079. tempval |= MACCFG2_MPEN;
  1080. gfar_write(&regs->maccfg2, tempval);
  1081. } else {
  1082. phy_stop(priv->phydev);
  1083. }
  1084. }
  1085. return 0;
  1086. }
  1087. static int gfar_resume(struct device *dev)
  1088. {
  1089. struct gfar_private *priv = dev_get_drvdata(dev);
  1090. struct net_device *ndev = priv->ndev;
  1091. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1092. unsigned long flags;
  1093. u32 tempval;
  1094. int magic_packet = priv->wol_en &&
  1095. (priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
  1096. if (!netif_running(ndev)) {
  1097. netif_device_attach(ndev);
  1098. return 0;
  1099. }
  1100. if (!magic_packet && priv->phydev)
  1101. phy_start(priv->phydev);
  1102. /* Disable Magic Packet mode, in case something
  1103. * else woke us up.
  1104. */
  1105. local_irq_save(flags);
  1106. lock_tx_qs(priv);
  1107. lock_rx_qs(priv);
  1108. tempval = gfar_read(&regs->maccfg2);
  1109. tempval &= ~MACCFG2_MPEN;
  1110. gfar_write(&regs->maccfg2, tempval);
  1111. gfar_start(ndev);
  1112. unlock_rx_qs(priv);
  1113. unlock_tx_qs(priv);
  1114. local_irq_restore(flags);
  1115. netif_device_attach(ndev);
  1116. enable_napi(priv);
  1117. return 0;
  1118. }
  1119. static int gfar_restore(struct device *dev)
  1120. {
  1121. struct gfar_private *priv = dev_get_drvdata(dev);
  1122. struct net_device *ndev = priv->ndev;
  1123. if (!netif_running(ndev))
  1124. return 0;
  1125. gfar_init_bds(ndev);
  1126. init_registers(ndev);
  1127. gfar_set_mac_address(ndev);
  1128. gfar_init_mac(ndev);
  1129. gfar_start(ndev);
  1130. priv->oldlink = 0;
  1131. priv->oldspeed = 0;
  1132. priv->oldduplex = -1;
  1133. if (priv->phydev)
  1134. phy_start(priv->phydev);
  1135. netif_device_attach(ndev);
  1136. enable_napi(priv);
  1137. return 0;
  1138. }
  1139. static struct dev_pm_ops gfar_pm_ops = {
  1140. .suspend = gfar_suspend,
  1141. .resume = gfar_resume,
  1142. .freeze = gfar_suspend,
  1143. .thaw = gfar_resume,
  1144. .restore = gfar_restore,
  1145. };
  1146. #define GFAR_PM_OPS (&gfar_pm_ops)
  1147. #else
  1148. #define GFAR_PM_OPS NULL
  1149. #endif
  1150. /* Reads the controller's registers to determine what interface
  1151. * connects it to the PHY.
  1152. */
  1153. static phy_interface_t gfar_get_interface(struct net_device *dev)
  1154. {
  1155. struct gfar_private *priv = netdev_priv(dev);
  1156. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1157. u32 ecntrl;
  1158. ecntrl = gfar_read(&regs->ecntrl);
  1159. if (ecntrl & ECNTRL_SGMII_MODE)
  1160. return PHY_INTERFACE_MODE_SGMII;
  1161. if (ecntrl & ECNTRL_TBI_MODE) {
  1162. if (ecntrl & ECNTRL_REDUCED_MODE)
  1163. return PHY_INTERFACE_MODE_RTBI;
  1164. else
  1165. return PHY_INTERFACE_MODE_TBI;
  1166. }
  1167. if (ecntrl & ECNTRL_REDUCED_MODE) {
  1168. if (ecntrl & ECNTRL_REDUCED_MII_MODE)
  1169. return PHY_INTERFACE_MODE_RMII;
  1170. else {
  1171. phy_interface_t interface = priv->interface;
  1172. /*
  1173. * This isn't autodetected right now, so it must
  1174. * be set by the device tree or platform code.
  1175. */
  1176. if (interface == PHY_INTERFACE_MODE_RGMII_ID)
  1177. return PHY_INTERFACE_MODE_RGMII_ID;
  1178. return PHY_INTERFACE_MODE_RGMII;
  1179. }
  1180. }
  1181. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
  1182. return PHY_INTERFACE_MODE_GMII;
  1183. return PHY_INTERFACE_MODE_MII;
  1184. }
  1185. /* Initializes driver's PHY state, and attaches to the PHY.
  1186. * Returns 0 on success.
  1187. */
  1188. static int init_phy(struct net_device *dev)
  1189. {
  1190. struct gfar_private *priv = netdev_priv(dev);
  1191. uint gigabit_support =
  1192. priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
  1193. SUPPORTED_1000baseT_Full : 0;
  1194. phy_interface_t interface;
  1195. priv->oldlink = 0;
  1196. priv->oldspeed = 0;
  1197. priv->oldduplex = -1;
  1198. interface = gfar_get_interface(dev);
  1199. priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
  1200. interface);
  1201. if (!priv->phydev)
  1202. priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
  1203. interface);
  1204. if (!priv->phydev) {
  1205. dev_err(&dev->dev, "could not attach to PHY\n");
  1206. return -ENODEV;
  1207. }
  1208. if (interface == PHY_INTERFACE_MODE_SGMII)
  1209. gfar_configure_serdes(dev);
  1210. /* Remove any features not supported by the controller */
  1211. priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
  1212. priv->phydev->advertising = priv->phydev->supported;
  1213. return 0;
  1214. }
  1215. /*
  1216. * Initialize TBI PHY interface for communicating with the
  1217. * SERDES lynx PHY on the chip. We communicate with this PHY
  1218. * through the MDIO bus on each controller, treating it as a
  1219. * "normal" PHY at the address found in the TBIPA register. We assume
  1220. * that the TBIPA register is valid. Either the MDIO bus code will set
  1221. * it to a value that doesn't conflict with other PHYs on the bus, or the
  1222. * value doesn't matter, as there are no other PHYs on the bus.
  1223. */
  1224. static void gfar_configure_serdes(struct net_device *dev)
  1225. {
  1226. struct gfar_private *priv = netdev_priv(dev);
  1227. struct phy_device *tbiphy;
  1228. if (!priv->tbi_node) {
  1229. dev_warn(&dev->dev, "error: SGMII mode requires that the "
  1230. "device tree specify a tbi-handle\n");
  1231. return;
  1232. }
  1233. tbiphy = of_phy_find_device(priv->tbi_node);
  1234. if (!tbiphy) {
  1235. dev_err(&dev->dev, "error: Could not get TBI device\n");
  1236. return;
  1237. }
  1238. /*
  1239. * If the link is already up, we must already be ok, and don't need to
  1240. * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
  1241. * everything for us? Resetting it takes the link down and requires
  1242. * several seconds for it to come back.
  1243. */
  1244. if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
  1245. return;
  1246. /* Single clk mode, mii mode off(for serdes communication) */
  1247. phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
  1248. phy_write(tbiphy, MII_ADVERTISE,
  1249. ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
  1250. ADVERTISE_1000XPSE_ASYM);
  1251. phy_write(tbiphy, MII_BMCR, BMCR_ANENABLE |
  1252. BMCR_ANRESTART | BMCR_FULLDPLX | BMCR_SPEED1000);
  1253. }
  1254. static void init_registers(struct net_device *dev)
  1255. {
  1256. struct gfar_private *priv = netdev_priv(dev);
  1257. struct gfar __iomem *regs = NULL;
  1258. int i = 0;
  1259. for (i = 0; i < priv->num_grps; i++) {
  1260. regs = priv->gfargrp[i].regs;
  1261. /* Clear IEVENT */
  1262. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  1263. /* Initialize IMASK */
  1264. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  1265. }
  1266. regs = priv->gfargrp[0].regs;
  1267. /* Init hash registers to zero */
  1268. gfar_write(&regs->igaddr0, 0);
  1269. gfar_write(&regs->igaddr1, 0);
  1270. gfar_write(&regs->igaddr2, 0);
  1271. gfar_write(&regs->igaddr3, 0);
  1272. gfar_write(&regs->igaddr4, 0);
  1273. gfar_write(&regs->igaddr5, 0);
  1274. gfar_write(&regs->igaddr6, 0);
  1275. gfar_write(&regs->igaddr7, 0);
  1276. gfar_write(&regs->gaddr0, 0);
  1277. gfar_write(&regs->gaddr1, 0);
  1278. gfar_write(&regs->gaddr2, 0);
  1279. gfar_write(&regs->gaddr3, 0);
  1280. gfar_write(&regs->gaddr4, 0);
  1281. gfar_write(&regs->gaddr5, 0);
  1282. gfar_write(&regs->gaddr6, 0);
  1283. gfar_write(&regs->gaddr7, 0);
  1284. /* Zero out the rmon mib registers if it has them */
  1285. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
  1286. memset_io(&(regs->rmon), 0, sizeof (struct rmon_mib));
  1287. /* Mask off the CAM interrupts */
  1288. gfar_write(&regs->rmon.cam1, 0xffffffff);
  1289. gfar_write(&regs->rmon.cam2, 0xffffffff);
  1290. }
  1291. /* Initialize the max receive buffer length */
  1292. gfar_write(&regs->mrblr, priv->rx_buffer_size);
  1293. /* Initialize the Minimum Frame Length Register */
  1294. gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
  1295. }
  1296. static int __gfar_is_rx_idle(struct gfar_private *priv)
  1297. {
  1298. u32 res;
  1299. /*
  1300. * Normaly TSEC should not hang on GRS commands, so we should
  1301. * actually wait for IEVENT_GRSC flag.
  1302. */
  1303. if (likely(!gfar_has_errata(priv, GFAR_ERRATA_A002)))
  1304. return 0;
  1305. /*
  1306. * Read the eTSEC register at offset 0xD1C. If bits 7-14 are
  1307. * the same as bits 23-30, the eTSEC Rx is assumed to be idle
  1308. * and the Rx can be safely reset.
  1309. */
  1310. res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
  1311. res &= 0x7f807f80;
  1312. if ((res & 0xffff) == (res >> 16))
  1313. return 1;
  1314. return 0;
  1315. }
  1316. /* Halt the receive and transmit queues */
  1317. static void gfar_halt_nodisable(struct net_device *dev)
  1318. {
  1319. struct gfar_private *priv = netdev_priv(dev);
  1320. struct gfar __iomem *regs = NULL;
  1321. u32 tempval;
  1322. int i = 0;
  1323. for (i = 0; i < priv->num_grps; i++) {
  1324. regs = priv->gfargrp[i].regs;
  1325. /* Mask all interrupts */
  1326. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  1327. /* Clear all interrupts */
  1328. gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
  1329. }
  1330. regs = priv->gfargrp[0].regs;
  1331. /* Stop the DMA, and wait for it to stop */
  1332. tempval = gfar_read(&regs->dmactrl);
  1333. if ((tempval & (DMACTRL_GRS | DMACTRL_GTS))
  1334. != (DMACTRL_GRS | DMACTRL_GTS)) {
  1335. int ret;
  1336. tempval |= (DMACTRL_GRS | DMACTRL_GTS);
  1337. gfar_write(&regs->dmactrl, tempval);
  1338. do {
  1339. ret = spin_event_timeout(((gfar_read(&regs->ievent) &
  1340. (IEVENT_GRSC | IEVENT_GTSC)) ==
  1341. (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
  1342. if (!ret && !(gfar_read(&regs->ievent) & IEVENT_GRSC))
  1343. ret = __gfar_is_rx_idle(priv);
  1344. } while (!ret);
  1345. }
  1346. }
  1347. /* Halt the receive and transmit queues */
  1348. void gfar_halt(struct net_device *dev)
  1349. {
  1350. struct gfar_private *priv = netdev_priv(dev);
  1351. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1352. u32 tempval;
  1353. gfar_halt_nodisable(dev);
  1354. /* Disable Rx and Tx */
  1355. tempval = gfar_read(&regs->maccfg1);
  1356. tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
  1357. gfar_write(&regs->maccfg1, tempval);
  1358. }
  1359. static void free_grp_irqs(struct gfar_priv_grp *grp)
  1360. {
  1361. free_irq(grp->interruptError, grp);
  1362. free_irq(grp->interruptTransmit, grp);
  1363. free_irq(grp->interruptReceive, grp);
  1364. }
  1365. void stop_gfar(struct net_device *dev)
  1366. {
  1367. struct gfar_private *priv = netdev_priv(dev);
  1368. unsigned long flags;
  1369. int i;
  1370. phy_stop(priv->phydev);
  1371. /* Lock it down */
  1372. local_irq_save(flags);
  1373. lock_tx_qs(priv);
  1374. lock_rx_qs(priv);
  1375. gfar_halt(dev);
  1376. unlock_rx_qs(priv);
  1377. unlock_tx_qs(priv);
  1378. local_irq_restore(flags);
  1379. /* Free the IRQs */
  1380. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1381. for (i = 0; i < priv->num_grps; i++)
  1382. free_grp_irqs(&priv->gfargrp[i]);
  1383. } else {
  1384. for (i = 0; i < priv->num_grps; i++)
  1385. free_irq(priv->gfargrp[i].interruptTransmit,
  1386. &priv->gfargrp[i]);
  1387. }
  1388. free_skb_resources(priv);
  1389. }
  1390. static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
  1391. {
  1392. struct txbd8 *txbdp;
  1393. struct gfar_private *priv = netdev_priv(tx_queue->dev);
  1394. int i, j;
  1395. txbdp = tx_queue->tx_bd_base;
  1396. for (i = 0; i < tx_queue->tx_ring_size; i++) {
  1397. if (!tx_queue->tx_skbuff[i])
  1398. continue;
  1399. dma_unmap_single(&priv->ofdev->dev, txbdp->bufPtr,
  1400. txbdp->length, DMA_TO_DEVICE);
  1401. txbdp->lstatus = 0;
  1402. for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
  1403. j++) {
  1404. txbdp++;
  1405. dma_unmap_page(&priv->ofdev->dev, txbdp->bufPtr,
  1406. txbdp->length, DMA_TO_DEVICE);
  1407. }
  1408. txbdp++;
  1409. dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
  1410. tx_queue->tx_skbuff[i] = NULL;
  1411. }
  1412. kfree(tx_queue->tx_skbuff);
  1413. }
  1414. static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
  1415. {
  1416. struct rxbd8 *rxbdp;
  1417. struct gfar_private *priv = netdev_priv(rx_queue->dev);
  1418. int i;
  1419. rxbdp = rx_queue->rx_bd_base;
  1420. for (i = 0; i < rx_queue->rx_ring_size; i++) {
  1421. if (rx_queue->rx_skbuff[i]) {
  1422. dma_unmap_single(&priv->ofdev->dev,
  1423. rxbdp->bufPtr, priv->rx_buffer_size,
  1424. DMA_FROM_DEVICE);
  1425. dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
  1426. rx_queue->rx_skbuff[i] = NULL;
  1427. }
  1428. rxbdp->lstatus = 0;
  1429. rxbdp->bufPtr = 0;
  1430. rxbdp++;
  1431. }
  1432. kfree(rx_queue->rx_skbuff);
  1433. }
  1434. /* If there are any tx skbs or rx skbs still around, free them.
  1435. * Then free tx_skbuff and rx_skbuff */
  1436. static void free_skb_resources(struct gfar_private *priv)
  1437. {
  1438. struct gfar_priv_tx_q *tx_queue = NULL;
  1439. struct gfar_priv_rx_q *rx_queue = NULL;
  1440. int i;
  1441. /* Go through all the buffer descriptors and free their data buffers */
  1442. for (i = 0; i < priv->num_tx_queues; i++) {
  1443. struct netdev_queue *txq;
  1444. tx_queue = priv->tx_queue[i];
  1445. txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
  1446. if(tx_queue->tx_skbuff)
  1447. free_skb_tx_queue(tx_queue);
  1448. netdev_tx_reset_queue(txq);
  1449. }
  1450. for (i = 0; i < priv->num_rx_queues; i++) {
  1451. rx_queue = priv->rx_queue[i];
  1452. if(rx_queue->rx_skbuff)
  1453. free_skb_rx_queue(rx_queue);
  1454. }
  1455. dma_free_coherent(&priv->ofdev->dev,
  1456. sizeof(struct txbd8) * priv->total_tx_ring_size +
  1457. sizeof(struct rxbd8) * priv->total_rx_ring_size,
  1458. priv->tx_queue[0]->tx_bd_base,
  1459. priv->tx_queue[0]->tx_bd_dma_base);
  1460. skb_queue_purge(&priv->rx_recycle);
  1461. }
  1462. void gfar_start(struct net_device *dev)
  1463. {
  1464. struct gfar_private *priv = netdev_priv(dev);
  1465. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1466. u32 tempval;
  1467. int i = 0;
  1468. /* Enable Rx and Tx in MACCFG1 */
  1469. tempval = gfar_read(&regs->maccfg1);
  1470. tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
  1471. gfar_write(&regs->maccfg1, tempval);
  1472. /* Initialize DMACTRL to have WWR and WOP */
  1473. tempval = gfar_read(&regs->dmactrl);
  1474. tempval |= DMACTRL_INIT_SETTINGS;
  1475. gfar_write(&regs->dmactrl, tempval);
  1476. /* Make sure we aren't stopped */
  1477. tempval = gfar_read(&regs->dmactrl);
  1478. tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
  1479. gfar_write(&regs->dmactrl, tempval);
  1480. for (i = 0; i < priv->num_grps; i++) {
  1481. regs = priv->gfargrp[i].regs;
  1482. /* Clear THLT/RHLT, so that the DMA starts polling now */
  1483. gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
  1484. gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
  1485. /* Unmask the interrupts we look for */
  1486. gfar_write(&regs->imask, IMASK_DEFAULT);
  1487. }
  1488. dev->trans_start = jiffies; /* prevent tx timeout */
  1489. }
  1490. void gfar_configure_coalescing(struct gfar_private *priv,
  1491. unsigned long tx_mask, unsigned long rx_mask)
  1492. {
  1493. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1494. u32 __iomem *baddr;
  1495. int i = 0;
  1496. /* Backward compatible case ---- even if we enable
  1497. * multiple queues, there's only single reg to program
  1498. */
  1499. gfar_write(&regs->txic, 0);
  1500. if(likely(priv->tx_queue[0]->txcoalescing))
  1501. gfar_write(&regs->txic, priv->tx_queue[0]->txic);
  1502. gfar_write(&regs->rxic, 0);
  1503. if(unlikely(priv->rx_queue[0]->rxcoalescing))
  1504. gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
  1505. if (priv->mode == MQ_MG_MODE) {
  1506. baddr = &regs->txic0;
  1507. for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
  1508. if (likely(priv->tx_queue[i]->txcoalescing)) {
  1509. gfar_write(baddr + i, 0);
  1510. gfar_write(baddr + i, priv->tx_queue[i]->txic);
  1511. }
  1512. }
  1513. baddr = &regs->rxic0;
  1514. for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
  1515. if (likely(priv->rx_queue[i]->rxcoalescing)) {
  1516. gfar_write(baddr + i, 0);
  1517. gfar_write(baddr + i, priv->rx_queue[i]->rxic);
  1518. }
  1519. }
  1520. }
  1521. }
  1522. static int register_grp_irqs(struct gfar_priv_grp *grp)
  1523. {
  1524. struct gfar_private *priv = grp->priv;
  1525. struct net_device *dev = priv->ndev;
  1526. int err;
  1527. /* If the device has multiple interrupts, register for
  1528. * them. Otherwise, only register for the one */
  1529. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  1530. /* Install our interrupt handlers for Error,
  1531. * Transmit, and Receive */
  1532. if ((err = request_irq(grp->interruptError, gfar_error, 0,
  1533. grp->int_name_er,grp)) < 0) {
  1534. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1535. grp->interruptError);
  1536. goto err_irq_fail;
  1537. }
  1538. if ((err = request_irq(grp->interruptTransmit, gfar_transmit,
  1539. 0, grp->int_name_tx, grp)) < 0) {
  1540. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1541. grp->interruptTransmit);
  1542. goto tx_irq_fail;
  1543. }
  1544. if ((err = request_irq(grp->interruptReceive, gfar_receive, 0,
  1545. grp->int_name_rx, grp)) < 0) {
  1546. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1547. grp->interruptReceive);
  1548. goto rx_irq_fail;
  1549. }
  1550. } else {
  1551. if ((err = request_irq(grp->interruptTransmit, gfar_interrupt, 0,
  1552. grp->int_name_tx, grp)) < 0) {
  1553. netif_err(priv, intr, dev, "Can't get IRQ %d\n",
  1554. grp->interruptTransmit);
  1555. goto err_irq_fail;
  1556. }
  1557. }
  1558. return 0;
  1559. rx_irq_fail:
  1560. free_irq(grp->interruptTransmit, grp);
  1561. tx_irq_fail:
  1562. free_irq(grp->interruptError, grp);
  1563. err_irq_fail:
  1564. return err;
  1565. }
  1566. /* Bring the controller up and running */
  1567. int startup_gfar(struct net_device *ndev)
  1568. {
  1569. struct gfar_private *priv = netdev_priv(ndev);
  1570. struct gfar __iomem *regs = NULL;
  1571. int err, i, j;
  1572. for (i = 0; i < priv->num_grps; i++) {
  1573. regs= priv->gfargrp[i].regs;
  1574. gfar_write(&regs->imask, IMASK_INIT_CLEAR);
  1575. }
  1576. regs= priv->gfargrp[0].regs;
  1577. err = gfar_alloc_skb_resources(ndev);
  1578. if (err)
  1579. return err;
  1580. gfar_init_mac(ndev);
  1581. for (i = 0; i < priv->num_grps; i++) {
  1582. err = register_grp_irqs(&priv->gfargrp[i]);
  1583. if (err) {
  1584. for (j = 0; j < i; j++)
  1585. free_grp_irqs(&priv->gfargrp[j]);
  1586. goto irq_fail;
  1587. }
  1588. }
  1589. /* Start the controller */
  1590. gfar_start(ndev);
  1591. phy_start(priv->phydev);
  1592. gfar_configure_coalescing(priv, 0xFF, 0xFF);
  1593. return 0;
  1594. irq_fail:
  1595. free_skb_resources(priv);
  1596. return err;
  1597. }
  1598. /* Called when something needs to use the ethernet device */
  1599. /* Returns 0 for success. */
  1600. static int gfar_enet_open(struct net_device *dev)
  1601. {
  1602. struct gfar_private *priv = netdev_priv(dev);
  1603. int err;
  1604. enable_napi(priv);
  1605. skb_queue_head_init(&priv->rx_recycle);
  1606. /* Initialize a bunch of registers */
  1607. init_registers(dev);
  1608. gfar_set_mac_address(dev);
  1609. err = init_phy(dev);
  1610. if (err) {
  1611. disable_napi(priv);
  1612. return err;
  1613. }
  1614. err = startup_gfar(dev);
  1615. if (err) {
  1616. disable_napi(priv);
  1617. return err;
  1618. }
  1619. netif_tx_start_all_queues(dev);
  1620. device_set_wakeup_enable(&dev->dev, priv->wol_en);
  1621. return err;
  1622. }
  1623. static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
  1624. {
  1625. struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
  1626. memset(fcb, 0, GMAC_FCB_LEN);
  1627. return fcb;
  1628. }
  1629. static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
  1630. int fcb_length)
  1631. {
  1632. u8 flags = 0;
  1633. /* If we're here, it's a IP packet with a TCP or UDP
  1634. * payload. We set it to checksum, using a pseudo-header
  1635. * we provide
  1636. */
  1637. flags = TXFCB_DEFAULT;
  1638. /* Tell the controller what the protocol is */
  1639. /* And provide the already calculated phcs */
  1640. if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
  1641. flags |= TXFCB_UDP;
  1642. fcb->phcs = udp_hdr(skb)->check;
  1643. } else
  1644. fcb->phcs = tcp_hdr(skb)->check;
  1645. /* l3os is the distance between the start of the
  1646. * frame (skb->data) and the start of the IP hdr.
  1647. * l4os is the distance between the start of the
  1648. * l3 hdr and the l4 hdr */
  1649. fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
  1650. fcb->l4os = skb_network_header_len(skb);
  1651. fcb->flags = flags;
  1652. }
  1653. void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
  1654. {
  1655. fcb->flags |= TXFCB_VLN;
  1656. fcb->vlctl = vlan_tx_tag_get(skb);
  1657. }
  1658. static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
  1659. struct txbd8 *base, int ring_size)
  1660. {
  1661. struct txbd8 *new_bd = bdp + stride;
  1662. return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
  1663. }
  1664. static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
  1665. int ring_size)
  1666. {
  1667. return skip_txbd(bdp, 1, base, ring_size);
  1668. }
  1669. /* This is called by the kernel when a frame is ready for transmission. */
  1670. /* It is pointed to by the dev->hard_start_xmit function pointer */
  1671. static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1672. {
  1673. struct gfar_private *priv = netdev_priv(dev);
  1674. struct gfar_priv_tx_q *tx_queue = NULL;
  1675. struct netdev_queue *txq;
  1676. struct gfar __iomem *regs = NULL;
  1677. struct txfcb *fcb = NULL;
  1678. struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
  1679. u32 lstatus;
  1680. int i, rq = 0, do_tstamp = 0;
  1681. u32 bufaddr;
  1682. unsigned long flags;
  1683. unsigned int nr_frags, nr_txbds, length, fcb_length = GMAC_FCB_LEN;
  1684. /*
  1685. * TOE=1 frames larger than 2500 bytes may see excess delays
  1686. * before start of transmission.
  1687. */
  1688. if (unlikely(gfar_has_errata(priv, GFAR_ERRATA_76) &&
  1689. skb->ip_summed == CHECKSUM_PARTIAL &&
  1690. skb->len > 2500)) {
  1691. int ret;
  1692. ret = skb_checksum_help(skb);
  1693. if (ret)
  1694. return ret;
  1695. }
  1696. rq = skb->queue_mapping;
  1697. tx_queue = priv->tx_queue[rq];
  1698. txq = netdev_get_tx_queue(dev, rq);
  1699. base = tx_queue->tx_bd_base;
  1700. regs = tx_queue->grp->regs;
  1701. /* check if time stamp should be generated */
  1702. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
  1703. priv->hwts_tx_en)) {
  1704. do_tstamp = 1;
  1705. fcb_length = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  1706. }
  1707. /* make space for additional header when fcb is needed */
  1708. if (((skb->ip_summed == CHECKSUM_PARTIAL) ||
  1709. vlan_tx_tag_present(skb) ||
  1710. unlikely(do_tstamp)) &&
  1711. (skb_headroom(skb) < fcb_length)) {
  1712. struct sk_buff *skb_new;
  1713. skb_new = skb_realloc_headroom(skb, fcb_length);
  1714. if (!skb_new) {
  1715. dev->stats.tx_errors++;
  1716. kfree_skb(skb);
  1717. return NETDEV_TX_OK;
  1718. }
  1719. /* Steal sock reference for processing TX time stamps */
  1720. swap(skb_new->sk, skb->sk);
  1721. swap(skb_new->destructor, skb->destructor);
  1722. kfree_skb(skb);
  1723. skb = skb_new;
  1724. }
  1725. /* total number of fragments in the SKB */
  1726. nr_frags = skb_shinfo(skb)->nr_frags;
  1727. /* calculate the required number of TxBDs for this skb */
  1728. if (unlikely(do_tstamp))
  1729. nr_txbds = nr_frags + 2;
  1730. else
  1731. nr_txbds = nr_frags + 1;
  1732. /* check if there is space to queue this packet */
  1733. if (nr_txbds > tx_queue->num_txbdfree) {
  1734. /* no space, stop the queue */
  1735. netif_tx_stop_queue(txq);
  1736. dev->stats.tx_fifo_errors++;
  1737. return NETDEV_TX_BUSY;
  1738. }
  1739. /* Update transmit stats */
  1740. tx_queue->stats.tx_bytes += skb->len;
  1741. tx_queue->stats.tx_packets++;
  1742. txbdp = txbdp_start = tx_queue->cur_tx;
  1743. lstatus = txbdp->lstatus;
  1744. /* Time stamp insertion requires one additional TxBD */
  1745. if (unlikely(do_tstamp))
  1746. txbdp_tstamp = txbdp = next_txbd(txbdp, base,
  1747. tx_queue->tx_ring_size);
  1748. if (nr_frags == 0) {
  1749. if (unlikely(do_tstamp))
  1750. txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
  1751. TXBD_INTERRUPT);
  1752. else
  1753. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1754. } else {
  1755. /* Place the fragment addresses and lengths into the TxBDs */
  1756. for (i = 0; i < nr_frags; i++) {
  1757. /* Point at the next BD, wrapping as needed */
  1758. txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1759. length = skb_shinfo(skb)->frags[i].size;
  1760. lstatus = txbdp->lstatus | length |
  1761. BD_LFLAG(TXBD_READY);
  1762. /* Handle the last BD specially */
  1763. if (i == nr_frags - 1)
  1764. lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
  1765. bufaddr = skb_frag_dma_map(&priv->ofdev->dev,
  1766. &skb_shinfo(skb)->frags[i],
  1767. 0,
  1768. length,
  1769. DMA_TO_DEVICE);
  1770. /* set the TxBD length and buffer pointer */
  1771. txbdp->bufPtr = bufaddr;
  1772. txbdp->lstatus = lstatus;
  1773. }
  1774. lstatus = txbdp_start->lstatus;
  1775. }
  1776. /* Add TxPAL between FCB and frame if required */
  1777. if (unlikely(do_tstamp)) {
  1778. skb_push(skb, GMAC_TXPAL_LEN);
  1779. memset(skb->data, 0, GMAC_TXPAL_LEN);
  1780. }
  1781. /* Set up checksumming */
  1782. if (CHECKSUM_PARTIAL == skb->ip_summed) {
  1783. fcb = gfar_add_fcb(skb);
  1784. /* as specified by errata */
  1785. if (unlikely(gfar_has_errata(priv, GFAR_ERRATA_12)
  1786. && ((unsigned long)fcb % 0x20) > 0x18)) {
  1787. __skb_pull(skb, GMAC_FCB_LEN);
  1788. skb_checksum_help(skb);
  1789. } else {
  1790. lstatus |= BD_LFLAG(TXBD_TOE);
  1791. gfar_tx_checksum(skb, fcb, fcb_length);
  1792. }
  1793. }
  1794. if (vlan_tx_tag_present(skb)) {
  1795. if (unlikely(NULL == fcb)) {
  1796. fcb = gfar_add_fcb(skb);
  1797. lstatus |= BD_LFLAG(TXBD_TOE);
  1798. }
  1799. gfar_tx_vlan(skb, fcb);
  1800. }
  1801. /* Setup tx hardware time stamping if requested */
  1802. if (unlikely(do_tstamp)) {
  1803. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  1804. if (fcb == NULL)
  1805. fcb = gfar_add_fcb(skb);
  1806. fcb->ptp = 1;
  1807. lstatus |= BD_LFLAG(TXBD_TOE);
  1808. }
  1809. txbdp_start->bufPtr = dma_map_single(&priv->ofdev->dev, skb->data,
  1810. skb_headlen(skb), DMA_TO_DEVICE);
  1811. /*
  1812. * If time stamping is requested one additional TxBD must be set up. The
  1813. * first TxBD points to the FCB and must have a data length of
  1814. * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
  1815. * the full frame length.
  1816. */
  1817. if (unlikely(do_tstamp)) {
  1818. txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_length;
  1819. txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
  1820. (skb_headlen(skb) - fcb_length);
  1821. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
  1822. } else {
  1823. lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
  1824. }
  1825. netdev_tx_sent_queue(txq, skb->len);
  1826. /*
  1827. * We can work in parallel with gfar_clean_tx_ring(), except
  1828. * when modifying num_txbdfree. Note that we didn't grab the lock
  1829. * when we were reading the num_txbdfree and checking for available
  1830. * space, that's because outside of this function it can only grow,
  1831. * and once we've got needed space, it cannot suddenly disappear.
  1832. *
  1833. * The lock also protects us from gfar_error(), which can modify
  1834. * regs->tstat and thus retrigger the transfers, which is why we
  1835. * also must grab the lock before setting ready bit for the first
  1836. * to be transmitted BD.
  1837. */
  1838. spin_lock_irqsave(&tx_queue->txlock, flags);
  1839. /*
  1840. * The powerpc-specific eieio() is used, as wmb() has too strong
  1841. * semantics (it requires synchronization between cacheable and
  1842. * uncacheable mappings, which eieio doesn't provide and which we
  1843. * don't need), thus requiring a more expensive sync instruction. At
  1844. * some point, the set of architecture-independent barrier functions
  1845. * should be expanded to include weaker barriers.
  1846. */
  1847. eieio();
  1848. txbdp_start->lstatus = lstatus;
  1849. eieio(); /* force lstatus write before tx_skbuff */
  1850. tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
  1851. /* Update the current skb pointer to the next entry we will use
  1852. * (wrapping if necessary) */
  1853. tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
  1854. TX_RING_MOD_MASK(tx_queue->tx_ring_size);
  1855. tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
  1856. /* reduce TxBD free count */
  1857. tx_queue->num_txbdfree -= (nr_txbds);
  1858. /* If the next BD still needs to be cleaned up, then the bds
  1859. are full. We need to tell the kernel to stop sending us stuff. */
  1860. if (!tx_queue->num_txbdfree) {
  1861. netif_tx_stop_queue(txq);
  1862. dev->stats.tx_fifo_errors++;
  1863. }
  1864. /* Tell the DMA to go go go */
  1865. gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
  1866. /* Unlock priv */
  1867. spin_unlock_irqrestore(&tx_queue->txlock, flags);
  1868. return NETDEV_TX_OK;
  1869. }
  1870. /* Stops the kernel queue, and halts the controller */
  1871. static int gfar_close(struct net_device *dev)
  1872. {
  1873. struct gfar_private *priv = netdev_priv(dev);
  1874. disable_napi(priv);
  1875. cancel_work_sync(&priv->reset_task);
  1876. stop_gfar(dev);
  1877. /* Disconnect from the PHY */
  1878. phy_disconnect(priv->phydev);
  1879. priv->phydev = NULL;
  1880. netif_tx_stop_all_queues(dev);
  1881. return 0;
  1882. }
  1883. /* Changes the mac address if the controller is not running. */
  1884. static int gfar_set_mac_address(struct net_device *dev)
  1885. {
  1886. gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
  1887. return 0;
  1888. }
  1889. /* Check if rx parser should be activated */
  1890. void gfar_check_rx_parser_mode(struct gfar_private *priv)
  1891. {
  1892. struct gfar __iomem *regs;
  1893. u32 tempval;
  1894. regs = priv->gfargrp[0].regs;
  1895. tempval = gfar_read(&regs->rctrl);
  1896. /* If parse is no longer required, then disable parser */
  1897. if (tempval & RCTRL_REQ_PARSER)
  1898. tempval |= RCTRL_PRSDEP_INIT;
  1899. else
  1900. tempval &= ~RCTRL_PRSDEP_INIT;
  1901. gfar_write(&regs->rctrl, tempval);
  1902. }
  1903. /* Enables and disables VLAN insertion/extraction */
  1904. void gfar_vlan_mode(struct net_device *dev, netdev_features_t features)
  1905. {
  1906. struct gfar_private *priv = netdev_priv(dev);
  1907. struct gfar __iomem *regs = NULL;
  1908. unsigned long flags;
  1909. u32 tempval;
  1910. regs = priv->gfargrp[0].regs;
  1911. local_irq_save(flags);
  1912. lock_rx_qs(priv);
  1913. if (features & NETIF_F_HW_VLAN_TX) {
  1914. /* Enable VLAN tag insertion */
  1915. tempval = gfar_read(&regs->tctrl);
  1916. tempval |= TCTRL_VLINS;
  1917. gfar_write(&regs->tctrl, tempval);
  1918. } else {
  1919. /* Disable VLAN tag insertion */
  1920. tempval = gfar_read(&regs->tctrl);
  1921. tempval &= ~TCTRL_VLINS;
  1922. gfar_write(&regs->tctrl, tempval);
  1923. }
  1924. if (features & NETIF_F_HW_VLAN_RX) {
  1925. /* Enable VLAN tag extraction */
  1926. tempval = gfar_read(&regs->rctrl);
  1927. tempval |= (RCTRL_VLEX | RCTRL_PRSDEP_INIT);
  1928. gfar_write(&regs->rctrl, tempval);
  1929. } else {
  1930. /* Disable VLAN tag extraction */
  1931. tempval = gfar_read(&regs->rctrl);
  1932. tempval &= ~RCTRL_VLEX;
  1933. gfar_write(&regs->rctrl, tempval);
  1934. gfar_check_rx_parser_mode(priv);
  1935. }
  1936. gfar_change_mtu(dev, dev->mtu);
  1937. unlock_rx_qs(priv);
  1938. local_irq_restore(flags);
  1939. }
  1940. static int gfar_change_mtu(struct net_device *dev, int new_mtu)
  1941. {
  1942. int tempsize, tempval;
  1943. struct gfar_private *priv = netdev_priv(dev);
  1944. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  1945. int oldsize = priv->rx_buffer_size;
  1946. int frame_size = new_mtu + ETH_HLEN;
  1947. if (gfar_is_vlan_on(priv))
  1948. frame_size += VLAN_HLEN;
  1949. if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
  1950. netif_err(priv, drv, dev, "Invalid MTU setting\n");
  1951. return -EINVAL;
  1952. }
  1953. if (gfar_uses_fcb(priv))
  1954. frame_size += GMAC_FCB_LEN;
  1955. frame_size += priv->padding;
  1956. tempsize =
  1957. (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
  1958. INCREMENTAL_BUFFER_SIZE;
  1959. /* Only stop and start the controller if it isn't already
  1960. * stopped, and we changed something */
  1961. if ((oldsize != tempsize) && (dev->flags & IFF_UP))
  1962. stop_gfar(dev);
  1963. priv->rx_buffer_size = tempsize;
  1964. dev->mtu = new_mtu;
  1965. gfar_write(&regs->mrblr, priv->rx_buffer_size);
  1966. gfar_write(&regs->maxfrm, priv->rx_buffer_size);
  1967. /* If the mtu is larger than the max size for standard
  1968. * ethernet frames (ie, a jumbo frame), then set maccfg2
  1969. * to allow huge frames, and to check the length */
  1970. tempval = gfar_read(&regs->maccfg2);
  1971. if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
  1972. gfar_has_errata(priv, GFAR_ERRATA_74))
  1973. tempval |= (MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
  1974. else
  1975. tempval &= ~(MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK);
  1976. gfar_write(&regs->maccfg2, tempval);
  1977. if ((oldsize != tempsize) && (dev->flags & IFF_UP))
  1978. startup_gfar(dev);
  1979. return 0;
  1980. }
  1981. /* gfar_reset_task gets scheduled when a packet has not been
  1982. * transmitted after a set amount of time.
  1983. * For now, assume that clearing out all the structures, and
  1984. * starting over will fix the problem.
  1985. */
  1986. static void gfar_reset_task(struct work_struct *work)
  1987. {
  1988. struct gfar_private *priv = container_of(work, struct gfar_private,
  1989. reset_task);
  1990. struct net_device *dev = priv->ndev;
  1991. if (dev->flags & IFF_UP) {
  1992. netif_tx_stop_all_queues(dev);
  1993. stop_gfar(dev);
  1994. startup_gfar(dev);
  1995. netif_tx_start_all_queues(dev);
  1996. }
  1997. netif_tx_schedule_all(dev);
  1998. }
  1999. static void gfar_timeout(struct net_device *dev)
  2000. {
  2001. struct gfar_private *priv = netdev_priv(dev);
  2002. dev->stats.tx_errors++;
  2003. schedule_work(&priv->reset_task);
  2004. }
  2005. static void gfar_align_skb(struct sk_buff *skb)
  2006. {
  2007. /* We need the data buffer to be aligned properly. We will reserve
  2008. * as many bytes as needed to align the data properly
  2009. */
  2010. skb_reserve(skb, RXBUF_ALIGNMENT -
  2011. (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
  2012. }
  2013. /* Interrupt Handler for Transmit complete */
  2014. static int gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
  2015. {
  2016. struct net_device *dev = tx_queue->dev;
  2017. struct netdev_queue *txq;
  2018. struct gfar_private *priv = netdev_priv(dev);
  2019. struct gfar_priv_rx_q *rx_queue = NULL;
  2020. struct txbd8 *bdp, *next = NULL;
  2021. struct txbd8 *lbdp = NULL;
  2022. struct txbd8 *base = tx_queue->tx_bd_base;
  2023. struct sk_buff *skb;
  2024. int skb_dirtytx;
  2025. int tx_ring_size = tx_queue->tx_ring_size;
  2026. int frags = 0, nr_txbds = 0;
  2027. int i;
  2028. int howmany = 0;
  2029. int tqi = tx_queue->qindex;
  2030. unsigned int bytes_sent = 0;
  2031. u32 lstatus;
  2032. size_t buflen;
  2033. rx_queue = priv->rx_queue[tqi];
  2034. txq = netdev_get_tx_queue(dev, tqi);
  2035. bdp = tx_queue->dirty_tx;
  2036. skb_dirtytx = tx_queue->skb_dirtytx;
  2037. while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
  2038. unsigned long flags;
  2039. frags = skb_shinfo(skb)->nr_frags;
  2040. /*
  2041. * When time stamping, one additional TxBD must be freed.
  2042. * Also, we need to dma_unmap_single() the TxPAL.
  2043. */
  2044. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  2045. nr_txbds = frags + 2;
  2046. else
  2047. nr_txbds = frags + 1;
  2048. lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
  2049. lstatus = lbdp->lstatus;
  2050. /* Only clean completed frames */
  2051. if ((lstatus & BD_LFLAG(TXBD_READY)) &&
  2052. (lstatus & BD_LENGTH_MASK))
  2053. break;
  2054. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
  2055. next = next_txbd(bdp, base, tx_ring_size);
  2056. buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
  2057. } else
  2058. buflen = bdp->length;
  2059. dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
  2060. buflen, DMA_TO_DEVICE);
  2061. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
  2062. struct skb_shared_hwtstamps shhwtstamps;
  2063. u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
  2064. memset(&shhwtstamps, 0, sizeof(shhwtstamps));
  2065. shhwtstamps.hwtstamp = ns_to_ktime(*ns);
  2066. skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
  2067. skb_tstamp_tx(skb, &shhwtstamps);
  2068. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  2069. bdp = next;
  2070. }
  2071. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  2072. bdp = next_txbd(bdp, base, tx_ring_size);
  2073. for (i = 0; i < frags; i++) {
  2074. dma_unmap_page(&priv->ofdev->dev,
  2075. bdp->bufPtr,
  2076. bdp->length,
  2077. DMA_TO_DEVICE);
  2078. bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
  2079. bdp = next_txbd(bdp, base, tx_ring_size);
  2080. }
  2081. bytes_sent += skb->len;
  2082. /*
  2083. * If there's room in the queue (limit it to rx_buffer_size)
  2084. * we add this skb back into the pool, if it's the right size
  2085. */
  2086. if (skb_queue_len(&priv->rx_recycle) < rx_queue->rx_ring_size &&
  2087. skb_recycle_check(skb, priv->rx_buffer_size +
  2088. RXBUF_ALIGNMENT)) {
  2089. gfar_align_skb(skb);
  2090. skb_queue_head(&priv->rx_recycle, skb);
  2091. } else
  2092. dev_kfree_skb_any(skb);
  2093. tx_queue->tx_skbuff[skb_dirtytx] = NULL;
  2094. skb_dirtytx = (skb_dirtytx + 1) &
  2095. TX_RING_MOD_MASK(tx_ring_size);
  2096. howmany++;
  2097. spin_lock_irqsave(&tx_queue->txlock, flags);
  2098. tx_queue->num_txbdfree += nr_txbds;
  2099. spin_unlock_irqrestore(&tx_queue->txlock, flags);
  2100. }
  2101. /* If we freed a buffer, we can restart transmission, if necessary */
  2102. if (netif_tx_queue_stopped(txq) && tx_queue->num_txbdfree)
  2103. netif_wake_subqueue(dev, tqi);
  2104. /* Update dirty indicators */
  2105. tx_queue->skb_dirtytx = skb_dirtytx;
  2106. tx_queue->dirty_tx = bdp;
  2107. netdev_tx_completed_queue(txq, howmany, bytes_sent);
  2108. return howmany;
  2109. }
  2110. static void gfar_schedule_cleanup(struct gfar_priv_grp *gfargrp)
  2111. {
  2112. unsigned long flags;
  2113. spin_lock_irqsave(&gfargrp->grplock, flags);
  2114. if (napi_schedule_prep(&gfargrp->napi)) {
  2115. gfar_write(&gfargrp->regs->imask, IMASK_RTX_DISABLED);
  2116. __napi_schedule(&gfargrp->napi);
  2117. } else {
  2118. /*
  2119. * Clear IEVENT, so interrupts aren't called again
  2120. * because of the packets that have already arrived.
  2121. */
  2122. gfar_write(&gfargrp->regs->ievent, IEVENT_RTX_MASK);
  2123. }
  2124. spin_unlock_irqrestore(&gfargrp->grplock, flags);
  2125. }
  2126. /* Interrupt Handler for Transmit complete */
  2127. static irqreturn_t gfar_transmit(int irq, void *grp_id)
  2128. {
  2129. gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
  2130. return IRQ_HANDLED;
  2131. }
  2132. static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
  2133. struct sk_buff *skb)
  2134. {
  2135. struct net_device *dev = rx_queue->dev;
  2136. struct gfar_private *priv = netdev_priv(dev);
  2137. dma_addr_t buf;
  2138. buf = dma_map_single(&priv->ofdev->dev, skb->data,
  2139. priv->rx_buffer_size, DMA_FROM_DEVICE);
  2140. gfar_init_rxbdp(rx_queue, bdp, buf);
  2141. }
  2142. static struct sk_buff * gfar_alloc_skb(struct net_device *dev)
  2143. {
  2144. struct gfar_private *priv = netdev_priv(dev);
  2145. struct sk_buff *skb = NULL;
  2146. skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
  2147. if (!skb)
  2148. return NULL;
  2149. gfar_align_skb(skb);
  2150. return skb;
  2151. }
  2152. struct sk_buff * gfar_new_skb(struct net_device *dev)
  2153. {
  2154. struct gfar_private *priv = netdev_priv(dev);
  2155. struct sk_buff *skb = NULL;
  2156. skb = skb_dequeue(&priv->rx_recycle);
  2157. if (!skb)
  2158. skb = gfar_alloc_skb(dev);
  2159. return skb;
  2160. }
  2161. static inline void count_errors(unsigned short status, struct net_device *dev)
  2162. {
  2163. struct gfar_private *priv = netdev_priv(dev);
  2164. struct net_device_stats *stats = &dev->stats;
  2165. struct gfar_extra_stats *estats = &priv->extra_stats;
  2166. /* If the packet was truncated, none of the other errors
  2167. * matter */
  2168. if (status & RXBD_TRUNCATED) {
  2169. stats->rx_length_errors++;
  2170. estats->rx_trunc++;
  2171. return;
  2172. }
  2173. /* Count the errors, if there were any */
  2174. if (status & (RXBD_LARGE | RXBD_SHORT)) {
  2175. stats->rx_length_errors++;
  2176. if (status & RXBD_LARGE)
  2177. estats->rx_large++;
  2178. else
  2179. estats->rx_short++;
  2180. }
  2181. if (status & RXBD_NONOCTET) {
  2182. stats->rx_frame_errors++;
  2183. estats->rx_nonoctet++;
  2184. }
  2185. if (status & RXBD_CRCERR) {
  2186. estats->rx_crcerr++;
  2187. stats->rx_crc_errors++;
  2188. }
  2189. if (status & RXBD_OVERRUN) {
  2190. estats->rx_overrun++;
  2191. stats->rx_crc_errors++;
  2192. }
  2193. }
  2194. irqreturn_t gfar_receive(int irq, void *grp_id)
  2195. {
  2196. gfar_schedule_cleanup((struct gfar_priv_grp *)grp_id);
  2197. return IRQ_HANDLED;
  2198. }
  2199. static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
  2200. {
  2201. /* If valid headers were found, and valid sums
  2202. * were verified, then we tell the kernel that no
  2203. * checksumming is necessary. Otherwise, it is */
  2204. if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
  2205. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2206. else
  2207. skb_checksum_none_assert(skb);
  2208. }
  2209. /* gfar_process_frame() -- handle one incoming packet if skb
  2210. * isn't NULL. */
  2211. static int gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
  2212. int amount_pull)
  2213. {
  2214. struct gfar_private *priv = netdev_priv(dev);
  2215. struct rxfcb *fcb = NULL;
  2216. int ret;
  2217. /* fcb is at the beginning if exists */
  2218. fcb = (struct rxfcb *)skb->data;
  2219. /* Remove the FCB from the skb */
  2220. /* Remove the padded bytes, if there are any */
  2221. if (amount_pull) {
  2222. skb_record_rx_queue(skb, fcb->rq);
  2223. skb_pull(skb, amount_pull);
  2224. }
  2225. /* Get receive timestamp from the skb */
  2226. if (priv->hwts_rx_en) {
  2227. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  2228. u64 *ns = (u64 *) skb->data;
  2229. memset(shhwtstamps, 0, sizeof(*shhwtstamps));
  2230. shhwtstamps->hwtstamp = ns_to_ktime(*ns);
  2231. }
  2232. if (priv->padding)
  2233. skb_pull(skb, priv->padding);
  2234. if (dev->features & NETIF_F_RXCSUM)
  2235. gfar_rx_checksum(skb, fcb);
  2236. /* Tell the skb what kind of packet this is */
  2237. skb->protocol = eth_type_trans(skb, dev);
  2238. /*
  2239. * There's need to check for NETIF_F_HW_VLAN_RX here.
  2240. * Even if vlan rx accel is disabled, on some chips
  2241. * RXFCB_VLN is pseudo randomly set.
  2242. */
  2243. if (dev->features & NETIF_F_HW_VLAN_RX &&
  2244. fcb->flags & RXFCB_VLN)
  2245. __vlan_hwaccel_put_tag(skb, fcb->vlctl);
  2246. /* Send the packet up the stack */
  2247. ret = netif_receive_skb(skb);
  2248. if (NET_RX_DROP == ret)
  2249. priv->extra_stats.kernel_dropped++;
  2250. return 0;
  2251. }
  2252. /* gfar_clean_rx_ring() -- Processes each frame in the rx ring
  2253. * until the budget/quota has been reached. Returns the number
  2254. * of frames handled
  2255. */
  2256. int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
  2257. {
  2258. struct net_device *dev = rx_queue->dev;
  2259. struct rxbd8 *bdp, *base;
  2260. struct sk_buff *skb;
  2261. int pkt_len;
  2262. int amount_pull;
  2263. int howmany = 0;
  2264. struct gfar_private *priv = netdev_priv(dev);
  2265. /* Get the first full descriptor */
  2266. bdp = rx_queue->cur_rx;
  2267. base = rx_queue->rx_bd_base;
  2268. amount_pull = (gfar_uses_fcb(priv) ? GMAC_FCB_LEN : 0);
  2269. while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
  2270. struct sk_buff *newskb;
  2271. rmb();
  2272. /* Add another skb for the future */
  2273. newskb = gfar_new_skb(dev);
  2274. skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
  2275. dma_unmap_single(&priv->ofdev->dev, bdp->bufPtr,
  2276. priv->rx_buffer_size, DMA_FROM_DEVICE);
  2277. if (unlikely(!(bdp->status & RXBD_ERR) &&
  2278. bdp->length > priv->rx_buffer_size))
  2279. bdp->status = RXBD_LARGE;
  2280. /* We drop the frame if we failed to allocate a new buffer */
  2281. if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
  2282. bdp->status & RXBD_ERR)) {
  2283. count_errors(bdp->status, dev);
  2284. if (unlikely(!newskb))
  2285. newskb = skb;
  2286. else if (skb)
  2287. skb_queue_head(&priv->rx_recycle, skb);
  2288. } else {
  2289. /* Increment the number of packets */
  2290. rx_queue->stats.rx_packets++;
  2291. howmany++;
  2292. if (likely(skb)) {
  2293. pkt_len = bdp->length - ETH_FCS_LEN;
  2294. /* Remove the FCS from the packet length */
  2295. skb_put(skb, pkt_len);
  2296. rx_queue->stats.rx_bytes += pkt_len;
  2297. skb_record_rx_queue(skb, rx_queue->qindex);
  2298. gfar_process_frame(dev, skb, amount_pull);
  2299. } else {
  2300. netif_warn(priv, rx_err, dev, "Missing skb!\n");
  2301. rx_queue->stats.rx_dropped++;
  2302. priv->extra_stats.rx_skbmissing++;
  2303. }
  2304. }
  2305. rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
  2306. /* Setup the new bdp */
  2307. gfar_new_rxbdp(rx_queue, bdp, newskb);
  2308. /* Update to the next pointer */
  2309. bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
  2310. /* update to point at the next skb */
  2311. rx_queue->skb_currx =
  2312. (rx_queue->skb_currx + 1) &
  2313. RX_RING_MOD_MASK(rx_queue->rx_ring_size);
  2314. }
  2315. /* Update the current rxbd pointer to be the next one */
  2316. rx_queue->cur_rx = bdp;
  2317. return howmany;
  2318. }
  2319. static int gfar_poll(struct napi_struct *napi, int budget)
  2320. {
  2321. struct gfar_priv_grp *gfargrp = container_of(napi,
  2322. struct gfar_priv_grp, napi);
  2323. struct gfar_private *priv = gfargrp->priv;
  2324. struct gfar __iomem *regs = gfargrp->regs;
  2325. struct gfar_priv_tx_q *tx_queue = NULL;
  2326. struct gfar_priv_rx_q *rx_queue = NULL;
  2327. int rx_cleaned = 0, budget_per_queue = 0, rx_cleaned_per_queue = 0;
  2328. int tx_cleaned = 0, i, left_over_budget = budget;
  2329. unsigned long serviced_queues = 0;
  2330. int num_queues = 0;
  2331. num_queues = gfargrp->num_rx_queues;
  2332. budget_per_queue = budget/num_queues;
  2333. /* Clear IEVENT, so interrupts aren't called again
  2334. * because of the packets that have already arrived */
  2335. gfar_write(&regs->ievent, IEVENT_RTX_MASK);
  2336. while (num_queues && left_over_budget) {
  2337. budget_per_queue = left_over_budget/num_queues;
  2338. left_over_budget = 0;
  2339. for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
  2340. if (test_bit(i, &serviced_queues))
  2341. continue;
  2342. rx_queue = priv->rx_queue[i];
  2343. tx_queue = priv->tx_queue[rx_queue->qindex];
  2344. tx_cleaned += gfar_clean_tx_ring(tx_queue);
  2345. rx_cleaned_per_queue = gfar_clean_rx_ring(rx_queue,
  2346. budget_per_queue);
  2347. rx_cleaned += rx_cleaned_per_queue;
  2348. if(rx_cleaned_per_queue < budget_per_queue) {
  2349. left_over_budget = left_over_budget +
  2350. (budget_per_queue - rx_cleaned_per_queue);
  2351. set_bit(i, &serviced_queues);
  2352. num_queues--;
  2353. }
  2354. }
  2355. }
  2356. if (tx_cleaned)
  2357. return budget;
  2358. if (rx_cleaned < budget) {
  2359. napi_complete(napi);
  2360. /* Clear the halt bit in RSTAT */
  2361. gfar_write(&regs->rstat, gfargrp->rstat);
  2362. gfar_write(&regs->imask, IMASK_DEFAULT);
  2363. /* If we are coalescing interrupts, update the timer */
  2364. /* Otherwise, clear it */
  2365. gfar_configure_coalescing(priv,
  2366. gfargrp->rx_bit_map, gfargrp->tx_bit_map);
  2367. }
  2368. return rx_cleaned;
  2369. }
  2370. #ifdef CONFIG_NET_POLL_CONTROLLER
  2371. /*
  2372. * Polling 'interrupt' - used by things like netconsole to send skbs
  2373. * without having to re-enable interrupts. It's not called while
  2374. * the interrupt routine is executing.
  2375. */
  2376. static void gfar_netpoll(struct net_device *dev)
  2377. {
  2378. struct gfar_private *priv = netdev_priv(dev);
  2379. int i = 0;
  2380. /* If the device has multiple interrupts, run tx/rx */
  2381. if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
  2382. for (i = 0; i < priv->num_grps; i++) {
  2383. disable_irq(priv->gfargrp[i].interruptTransmit);
  2384. disable_irq(priv->gfargrp[i].interruptReceive);
  2385. disable_irq(priv->gfargrp[i].interruptError);
  2386. gfar_interrupt(priv->gfargrp[i].interruptTransmit,
  2387. &priv->gfargrp[i]);
  2388. enable_irq(priv->gfargrp[i].interruptError);
  2389. enable_irq(priv->gfargrp[i].interruptReceive);
  2390. enable_irq(priv->gfargrp[i].interruptTransmit);
  2391. }
  2392. } else {
  2393. for (i = 0; i < priv->num_grps; i++) {
  2394. disable_irq(priv->gfargrp[i].interruptTransmit);
  2395. gfar_interrupt(priv->gfargrp[i].interruptTransmit,
  2396. &priv->gfargrp[i]);
  2397. enable_irq(priv->gfargrp[i].interruptTransmit);
  2398. }
  2399. }
  2400. }
  2401. #endif
  2402. /* The interrupt handler for devices with one interrupt */
  2403. static irqreturn_t gfar_interrupt(int irq, void *grp_id)
  2404. {
  2405. struct gfar_priv_grp *gfargrp = grp_id;
  2406. /* Save ievent for future reference */
  2407. u32 events = gfar_read(&gfargrp->regs->ievent);
  2408. /* Check for reception */
  2409. if (events & IEVENT_RX_MASK)
  2410. gfar_receive(irq, grp_id);
  2411. /* Check for transmit completion */
  2412. if (events & IEVENT_TX_MASK)
  2413. gfar_transmit(irq, grp_id);
  2414. /* Check for errors */
  2415. if (events & IEVENT_ERR_MASK)
  2416. gfar_error(irq, grp_id);
  2417. return IRQ_HANDLED;
  2418. }
  2419. /* Called every time the controller might need to be made
  2420. * aware of new link state. The PHY code conveys this
  2421. * information through variables in the phydev structure, and this
  2422. * function converts those variables into the appropriate
  2423. * register values, and can bring down the device if needed.
  2424. */
  2425. static void adjust_link(struct net_device *dev)
  2426. {
  2427. struct gfar_private *priv = netdev_priv(dev);
  2428. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2429. unsigned long flags;
  2430. struct phy_device *phydev = priv->phydev;
  2431. int new_state = 0;
  2432. local_irq_save(flags);
  2433. lock_tx_qs(priv);
  2434. if (phydev->link) {
  2435. u32 tempval = gfar_read(&regs->maccfg2);
  2436. u32 ecntrl = gfar_read(&regs->ecntrl);
  2437. /* Now we make sure that we can be in full duplex mode.
  2438. * If not, we operate in half-duplex mode. */
  2439. if (phydev->duplex != priv->oldduplex) {
  2440. new_state = 1;
  2441. if (!(phydev->duplex))
  2442. tempval &= ~(MACCFG2_FULL_DUPLEX);
  2443. else
  2444. tempval |= MACCFG2_FULL_DUPLEX;
  2445. priv->oldduplex = phydev->duplex;
  2446. }
  2447. if (phydev->speed != priv->oldspeed) {
  2448. new_state = 1;
  2449. switch (phydev->speed) {
  2450. case 1000:
  2451. tempval =
  2452. ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
  2453. ecntrl &= ~(ECNTRL_R100);
  2454. break;
  2455. case 100:
  2456. case 10:
  2457. tempval =
  2458. ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
  2459. /* Reduced mode distinguishes
  2460. * between 10 and 100 */
  2461. if (phydev->speed == SPEED_100)
  2462. ecntrl |= ECNTRL_R100;
  2463. else
  2464. ecntrl &= ~(ECNTRL_R100);
  2465. break;
  2466. default:
  2467. netif_warn(priv, link, dev,
  2468. "Ack! Speed (%d) is not 10/100/1000!\n",
  2469. phydev->speed);
  2470. break;
  2471. }
  2472. priv->oldspeed = phydev->speed;
  2473. }
  2474. gfar_write(&regs->maccfg2, tempval);
  2475. gfar_write(&regs->ecntrl, ecntrl);
  2476. if (!priv->oldlink) {
  2477. new_state = 1;
  2478. priv->oldlink = 1;
  2479. }
  2480. } else if (priv->oldlink) {
  2481. new_state = 1;
  2482. priv->oldlink = 0;
  2483. priv->oldspeed = 0;
  2484. priv->oldduplex = -1;
  2485. }
  2486. if (new_state && netif_msg_link(priv))
  2487. phy_print_status(phydev);
  2488. unlock_tx_qs(priv);
  2489. local_irq_restore(flags);
  2490. }
  2491. /* Update the hash table based on the current list of multicast
  2492. * addresses we subscribe to. Also, change the promiscuity of
  2493. * the device based on the flags (this function is called
  2494. * whenever dev->flags is changed */
  2495. static void gfar_set_multi(struct net_device *dev)
  2496. {
  2497. struct netdev_hw_addr *ha;
  2498. struct gfar_private *priv = netdev_priv(dev);
  2499. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2500. u32 tempval;
  2501. if (dev->flags & IFF_PROMISC) {
  2502. /* Set RCTRL to PROM */
  2503. tempval = gfar_read(&regs->rctrl);
  2504. tempval |= RCTRL_PROM;
  2505. gfar_write(&regs->rctrl, tempval);
  2506. } else {
  2507. /* Set RCTRL to not PROM */
  2508. tempval = gfar_read(&regs->rctrl);
  2509. tempval &= ~(RCTRL_PROM);
  2510. gfar_write(&regs->rctrl, tempval);
  2511. }
  2512. if (dev->flags & IFF_ALLMULTI) {
  2513. /* Set the hash to rx all multicast frames */
  2514. gfar_write(&regs->igaddr0, 0xffffffff);
  2515. gfar_write(&regs->igaddr1, 0xffffffff);
  2516. gfar_write(&regs->igaddr2, 0xffffffff);
  2517. gfar_write(&regs->igaddr3, 0xffffffff);
  2518. gfar_write(&regs->igaddr4, 0xffffffff);
  2519. gfar_write(&regs->igaddr5, 0xffffffff);
  2520. gfar_write(&regs->igaddr6, 0xffffffff);
  2521. gfar_write(&regs->igaddr7, 0xffffffff);
  2522. gfar_write(&regs->gaddr0, 0xffffffff);
  2523. gfar_write(&regs->gaddr1, 0xffffffff);
  2524. gfar_write(&regs->gaddr2, 0xffffffff);
  2525. gfar_write(&regs->gaddr3, 0xffffffff);
  2526. gfar_write(&regs->gaddr4, 0xffffffff);
  2527. gfar_write(&regs->gaddr5, 0xffffffff);
  2528. gfar_write(&regs->gaddr6, 0xffffffff);
  2529. gfar_write(&regs->gaddr7, 0xffffffff);
  2530. } else {
  2531. int em_num;
  2532. int idx;
  2533. /* zero out the hash */
  2534. gfar_write(&regs->igaddr0, 0x0);
  2535. gfar_write(&regs->igaddr1, 0x0);
  2536. gfar_write(&regs->igaddr2, 0x0);
  2537. gfar_write(&regs->igaddr3, 0x0);
  2538. gfar_write(&regs->igaddr4, 0x0);
  2539. gfar_write(&regs->igaddr5, 0x0);
  2540. gfar_write(&regs->igaddr6, 0x0);
  2541. gfar_write(&regs->igaddr7, 0x0);
  2542. gfar_write(&regs->gaddr0, 0x0);
  2543. gfar_write(&regs->gaddr1, 0x0);
  2544. gfar_write(&regs->gaddr2, 0x0);
  2545. gfar_write(&regs->gaddr3, 0x0);
  2546. gfar_write(&regs->gaddr4, 0x0);
  2547. gfar_write(&regs->gaddr5, 0x0);
  2548. gfar_write(&regs->gaddr6, 0x0);
  2549. gfar_write(&regs->gaddr7, 0x0);
  2550. /* If we have extended hash tables, we need to
  2551. * clear the exact match registers to prepare for
  2552. * setting them */
  2553. if (priv->extended_hash) {
  2554. em_num = GFAR_EM_NUM + 1;
  2555. gfar_clear_exact_match(dev);
  2556. idx = 1;
  2557. } else {
  2558. idx = 0;
  2559. em_num = 0;
  2560. }
  2561. if (netdev_mc_empty(dev))
  2562. return;
  2563. /* Parse the list, and set the appropriate bits */
  2564. netdev_for_each_mc_addr(ha, dev) {
  2565. if (idx < em_num) {
  2566. gfar_set_mac_for_addr(dev, idx, ha->addr);
  2567. idx++;
  2568. } else
  2569. gfar_set_hash_for_addr(dev, ha->addr);
  2570. }
  2571. }
  2572. }
  2573. /* Clears each of the exact match registers to zero, so they
  2574. * don't interfere with normal reception */
  2575. static void gfar_clear_exact_match(struct net_device *dev)
  2576. {
  2577. int idx;
  2578. static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  2579. for(idx = 1;idx < GFAR_EM_NUM + 1;idx++)
  2580. gfar_set_mac_for_addr(dev, idx, zero_arr);
  2581. }
  2582. /* Set the appropriate hash bit for the given addr */
  2583. /* The algorithm works like so:
  2584. * 1) Take the Destination Address (ie the multicast address), and
  2585. * do a CRC on it (little endian), and reverse the bits of the
  2586. * result.
  2587. * 2) Use the 8 most significant bits as a hash into a 256-entry
  2588. * table. The table is controlled through 8 32-bit registers:
  2589. * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
  2590. * gaddr7. This means that the 3 most significant bits in the
  2591. * hash index which gaddr register to use, and the 5 other bits
  2592. * indicate which bit (assuming an IBM numbering scheme, which
  2593. * for PowerPC (tm) is usually the case) in the register holds
  2594. * the entry. */
  2595. static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
  2596. {
  2597. u32 tempval;
  2598. struct gfar_private *priv = netdev_priv(dev);
  2599. u32 result = ether_crc(ETH_ALEN, addr);
  2600. int width = priv->hash_width;
  2601. u8 whichbit = (result >> (32 - width)) & 0x1f;
  2602. u8 whichreg = result >> (32 - width + 5);
  2603. u32 value = (1 << (31-whichbit));
  2604. tempval = gfar_read(priv->hash_regs[whichreg]);
  2605. tempval |= value;
  2606. gfar_write(priv->hash_regs[whichreg], tempval);
  2607. }
  2608. /* There are multiple MAC Address register pairs on some controllers
  2609. * This function sets the numth pair to a given address
  2610. */
  2611. static void gfar_set_mac_for_addr(struct net_device *dev, int num,
  2612. const u8 *addr)
  2613. {
  2614. struct gfar_private *priv = netdev_priv(dev);
  2615. struct gfar __iomem *regs = priv->gfargrp[0].regs;
  2616. int idx;
  2617. char tmpbuf[ETH_ALEN];
  2618. u32 tempval;
  2619. u32 __iomem *macptr = &regs->macstnaddr1;
  2620. macptr += num*2;
  2621. /* Now copy it into the mac registers backwards, cuz */
  2622. /* little endian is silly */
  2623. for (idx = 0; idx < ETH_ALEN; idx++)
  2624. tmpbuf[ETH_ALEN - 1 - idx] = addr[idx];
  2625. gfar_write(macptr, *((u32 *) (tmpbuf)));
  2626. tempval = *((u32 *) (tmpbuf + 4));
  2627. gfar_write(macptr+1, tempval);
  2628. }
  2629. /* GFAR error interrupt handler */
  2630. static irqreturn_t gfar_error(int irq, void *grp_id)
  2631. {
  2632. struct gfar_priv_grp *gfargrp = grp_id;
  2633. struct gfar __iomem *regs = gfargrp->regs;
  2634. struct gfar_private *priv= gfargrp->priv;
  2635. struct net_device *dev = priv->ndev;
  2636. /* Save ievent for future reference */
  2637. u32 events = gfar_read(&regs->ievent);
  2638. /* Clear IEVENT */
  2639. gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
  2640. /* Magic Packet is not an error. */
  2641. if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
  2642. (events & IEVENT_MAG))
  2643. events &= ~IEVENT_MAG;
  2644. /* Hmm... */
  2645. if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
  2646. netdev_dbg(dev, "error interrupt (ievent=0x%08x imask=0x%08x)\n",
  2647. events, gfar_read(&regs->imask));
  2648. /* Update the error counters */
  2649. if (events & IEVENT_TXE) {
  2650. dev->stats.tx_errors++;
  2651. if (events & IEVENT_LC)
  2652. dev->stats.tx_window_errors++;
  2653. if (events & IEVENT_CRL)
  2654. dev->stats.tx_aborted_errors++;
  2655. if (events & IEVENT_XFUN) {
  2656. unsigned long flags;
  2657. netif_dbg(priv, tx_err, dev,
  2658. "TX FIFO underrun, packet dropped\n");
  2659. dev->stats.tx_dropped++;
  2660. priv->extra_stats.tx_underrun++;
  2661. local_irq_save(flags);
  2662. lock_tx_qs(priv);
  2663. /* Reactivate the Tx Queues */
  2664. gfar_write(&regs->tstat, gfargrp->tstat);
  2665. unlock_tx_qs(priv);
  2666. local_irq_restore(flags);
  2667. }
  2668. netif_dbg(priv, tx_err, dev, "Transmit Error\n");
  2669. }
  2670. if (events & IEVENT_BSY) {
  2671. dev->stats.rx_errors++;
  2672. priv->extra_stats.rx_bsy++;
  2673. gfar_receive(irq, grp_id);
  2674. netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
  2675. gfar_read(&regs->rstat));
  2676. }
  2677. if (events & IEVENT_BABR) {
  2678. dev->stats.rx_errors++;
  2679. priv->extra_stats.rx_babr++;
  2680. netif_dbg(priv, rx_err, dev, "babbling RX error\n");
  2681. }
  2682. if (events & IEVENT_EBERR) {
  2683. priv->extra_stats.eberr++;
  2684. netif_dbg(priv, rx_err, dev, "bus error\n");
  2685. }
  2686. if (events & IEVENT_RXC)
  2687. netif_dbg(priv, rx_status, dev, "control frame\n");
  2688. if (events & IEVENT_BABT) {
  2689. priv->extra_stats.tx_babt++;
  2690. netif_dbg(priv, tx_err, dev, "babbling TX error\n");
  2691. }
  2692. return IRQ_HANDLED;
  2693. }
  2694. static struct of_device_id gfar_match[] =
  2695. {
  2696. {
  2697. .type = "network",
  2698. .compatible = "gianfar",
  2699. },
  2700. {
  2701. .compatible = "fsl,etsec2",
  2702. },
  2703. {},
  2704. };
  2705. MODULE_DEVICE_TABLE(of, gfar_match);
  2706. /* Structure for a device driver */
  2707. static struct platform_driver gfar_driver = {
  2708. .driver = {
  2709. .name = "fsl-gianfar",
  2710. .owner = THIS_MODULE,
  2711. .pm = GFAR_PM_OPS,
  2712. .of_match_table = gfar_match,
  2713. },
  2714. .probe = gfar_probe,
  2715. .remove = gfar_remove,
  2716. };
  2717. module_platform_driver(gfar_driver);