gianfar.c 81 KB

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