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