gianfar.c 84 KB

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