gianfar.c 86 KB

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