gianfar.c 86 KB

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