gianfar.c 86 KB

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