gianfar.c 90 KB

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