gianfar.c 86 KB

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