gianfar.c 90 KB

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