gianfar.c 87 KB

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