gianfar.c 84 KB

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