gianfar.c 97 KB

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