gianfar.c 79 KB

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