bcmgenet.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742
  1. /*
  2. * Broadcom GENET (Gigabit Ethernet) controller driver
  3. *
  4. * Copyright (c) 2014-2017 Broadcom
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) "bcmgenet: " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <linux/fcntl.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/string.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/pm.h>
  25. #include <linux/clk.h>
  26. #include <linux/of.h>
  27. #include <linux/of_address.h>
  28. #include <linux/of_irq.h>
  29. #include <linux/of_net.h>
  30. #include <linux/of_platform.h>
  31. #include <net/arp.h>
  32. #include <linux/mii.h>
  33. #include <linux/ethtool.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/inetdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/in.h>
  39. #include <linux/ip.h>
  40. #include <linux/ipv6.h>
  41. #include <linux/phy.h>
  42. #include <linux/platform_data/bcmgenet.h>
  43. #include <asm/unaligned.h>
  44. #include "bcmgenet.h"
  45. /* Maximum number of hardware queues, downsized if needed */
  46. #define GENET_MAX_MQ_CNT 4
  47. /* Default highest priority queue for multi queue support */
  48. #define GENET_Q0_PRIORITY 0
  49. #define GENET_Q16_RX_BD_CNT \
  50. (TOTAL_DESC - priv->hw_params->rx_queues * priv->hw_params->rx_bds_per_q)
  51. #define GENET_Q16_TX_BD_CNT \
  52. (TOTAL_DESC - priv->hw_params->tx_queues * priv->hw_params->tx_bds_per_q)
  53. #define RX_BUF_LENGTH 2048
  54. #define SKB_ALIGNMENT 32
  55. /* Tx/Rx DMA register offset, skip 256 descriptors */
  56. #define WORDS_PER_BD(p) (p->hw_params->words_per_bd)
  57. #define DMA_DESC_SIZE (WORDS_PER_BD(priv) * sizeof(u32))
  58. #define GENET_TDMA_REG_OFF (priv->hw_params->tdma_offset + \
  59. TOTAL_DESC * DMA_DESC_SIZE)
  60. #define GENET_RDMA_REG_OFF (priv->hw_params->rdma_offset + \
  61. TOTAL_DESC * DMA_DESC_SIZE)
  62. static inline void bcmgenet_writel(u32 value, void __iomem *offset)
  63. {
  64. /* MIPS chips strapped for BE will automagically configure the
  65. * peripheral registers for CPU-native byte order.
  66. */
  67. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  68. __raw_writel(value, offset);
  69. else
  70. writel_relaxed(value, offset);
  71. }
  72. static inline u32 bcmgenet_readl(void __iomem *offset)
  73. {
  74. if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  75. return __raw_readl(offset);
  76. else
  77. return readl_relaxed(offset);
  78. }
  79. static inline void dmadesc_set_length_status(struct bcmgenet_priv *priv,
  80. void __iomem *d, u32 value)
  81. {
  82. bcmgenet_writel(value, d + DMA_DESC_LENGTH_STATUS);
  83. }
  84. static inline u32 dmadesc_get_length_status(struct bcmgenet_priv *priv,
  85. void __iomem *d)
  86. {
  87. return bcmgenet_readl(d + DMA_DESC_LENGTH_STATUS);
  88. }
  89. static inline void dmadesc_set_addr(struct bcmgenet_priv *priv,
  90. void __iomem *d,
  91. dma_addr_t addr)
  92. {
  93. bcmgenet_writel(lower_32_bits(addr), d + DMA_DESC_ADDRESS_LO);
  94. /* Register writes to GISB bus can take couple hundred nanoseconds
  95. * and are done for each packet, save these expensive writes unless
  96. * the platform is explicitly configured for 64-bits/LPAE.
  97. */
  98. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  99. if (priv->hw_params->flags & GENET_HAS_40BITS)
  100. bcmgenet_writel(upper_32_bits(addr), d + DMA_DESC_ADDRESS_HI);
  101. #endif
  102. }
  103. /* Combined address + length/status setter */
  104. static inline void dmadesc_set(struct bcmgenet_priv *priv,
  105. void __iomem *d, dma_addr_t addr, u32 val)
  106. {
  107. dmadesc_set_addr(priv, d, addr);
  108. dmadesc_set_length_status(priv, d, val);
  109. }
  110. static inline dma_addr_t dmadesc_get_addr(struct bcmgenet_priv *priv,
  111. void __iomem *d)
  112. {
  113. dma_addr_t addr;
  114. addr = bcmgenet_readl(d + DMA_DESC_ADDRESS_LO);
  115. /* Register writes to GISB bus can take couple hundred nanoseconds
  116. * and are done for each packet, save these expensive writes unless
  117. * the platform is explicitly configured for 64-bits/LPAE.
  118. */
  119. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  120. if (priv->hw_params->flags & GENET_HAS_40BITS)
  121. addr |= (u64)bcmgenet_readl(d + DMA_DESC_ADDRESS_HI) << 32;
  122. #endif
  123. return addr;
  124. }
  125. #define GENET_VER_FMT "%1d.%1d EPHY: 0x%04x"
  126. #define GENET_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  127. NETIF_MSG_LINK)
  128. static inline u32 bcmgenet_rbuf_ctrl_get(struct bcmgenet_priv *priv)
  129. {
  130. if (GENET_IS_V1(priv))
  131. return bcmgenet_rbuf_readl(priv, RBUF_FLUSH_CTRL_V1);
  132. else
  133. return bcmgenet_sys_readl(priv, SYS_RBUF_FLUSH_CTRL);
  134. }
  135. static inline void bcmgenet_rbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
  136. {
  137. if (GENET_IS_V1(priv))
  138. bcmgenet_rbuf_writel(priv, val, RBUF_FLUSH_CTRL_V1);
  139. else
  140. bcmgenet_sys_writel(priv, val, SYS_RBUF_FLUSH_CTRL);
  141. }
  142. /* These macros are defined to deal with register map change
  143. * between GENET1.1 and GENET2. Only those currently being used
  144. * by driver are defined.
  145. */
  146. static inline u32 bcmgenet_tbuf_ctrl_get(struct bcmgenet_priv *priv)
  147. {
  148. if (GENET_IS_V1(priv))
  149. return bcmgenet_rbuf_readl(priv, TBUF_CTRL_V1);
  150. else
  151. return bcmgenet_readl(priv->base +
  152. priv->hw_params->tbuf_offset + TBUF_CTRL);
  153. }
  154. static inline void bcmgenet_tbuf_ctrl_set(struct bcmgenet_priv *priv, u32 val)
  155. {
  156. if (GENET_IS_V1(priv))
  157. bcmgenet_rbuf_writel(priv, val, TBUF_CTRL_V1);
  158. else
  159. bcmgenet_writel(val, priv->base +
  160. priv->hw_params->tbuf_offset + TBUF_CTRL);
  161. }
  162. static inline u32 bcmgenet_bp_mc_get(struct bcmgenet_priv *priv)
  163. {
  164. if (GENET_IS_V1(priv))
  165. return bcmgenet_rbuf_readl(priv, TBUF_BP_MC_V1);
  166. else
  167. return bcmgenet_readl(priv->base +
  168. priv->hw_params->tbuf_offset + TBUF_BP_MC);
  169. }
  170. static inline void bcmgenet_bp_mc_set(struct bcmgenet_priv *priv, u32 val)
  171. {
  172. if (GENET_IS_V1(priv))
  173. bcmgenet_rbuf_writel(priv, val, TBUF_BP_MC_V1);
  174. else
  175. bcmgenet_writel(val, priv->base +
  176. priv->hw_params->tbuf_offset + TBUF_BP_MC);
  177. }
  178. /* RX/TX DMA register accessors */
  179. enum dma_reg {
  180. DMA_RING_CFG = 0,
  181. DMA_CTRL,
  182. DMA_STATUS,
  183. DMA_SCB_BURST_SIZE,
  184. DMA_ARB_CTRL,
  185. DMA_PRIORITY_0,
  186. DMA_PRIORITY_1,
  187. DMA_PRIORITY_2,
  188. DMA_INDEX2RING_0,
  189. DMA_INDEX2RING_1,
  190. DMA_INDEX2RING_2,
  191. DMA_INDEX2RING_3,
  192. DMA_INDEX2RING_4,
  193. DMA_INDEX2RING_5,
  194. DMA_INDEX2RING_6,
  195. DMA_INDEX2RING_7,
  196. DMA_RING0_TIMEOUT,
  197. DMA_RING1_TIMEOUT,
  198. DMA_RING2_TIMEOUT,
  199. DMA_RING3_TIMEOUT,
  200. DMA_RING4_TIMEOUT,
  201. DMA_RING5_TIMEOUT,
  202. DMA_RING6_TIMEOUT,
  203. DMA_RING7_TIMEOUT,
  204. DMA_RING8_TIMEOUT,
  205. DMA_RING9_TIMEOUT,
  206. DMA_RING10_TIMEOUT,
  207. DMA_RING11_TIMEOUT,
  208. DMA_RING12_TIMEOUT,
  209. DMA_RING13_TIMEOUT,
  210. DMA_RING14_TIMEOUT,
  211. DMA_RING15_TIMEOUT,
  212. DMA_RING16_TIMEOUT,
  213. };
  214. static const u8 bcmgenet_dma_regs_v3plus[] = {
  215. [DMA_RING_CFG] = 0x00,
  216. [DMA_CTRL] = 0x04,
  217. [DMA_STATUS] = 0x08,
  218. [DMA_SCB_BURST_SIZE] = 0x0C,
  219. [DMA_ARB_CTRL] = 0x2C,
  220. [DMA_PRIORITY_0] = 0x30,
  221. [DMA_PRIORITY_1] = 0x34,
  222. [DMA_PRIORITY_2] = 0x38,
  223. [DMA_RING0_TIMEOUT] = 0x2C,
  224. [DMA_RING1_TIMEOUT] = 0x30,
  225. [DMA_RING2_TIMEOUT] = 0x34,
  226. [DMA_RING3_TIMEOUT] = 0x38,
  227. [DMA_RING4_TIMEOUT] = 0x3c,
  228. [DMA_RING5_TIMEOUT] = 0x40,
  229. [DMA_RING6_TIMEOUT] = 0x44,
  230. [DMA_RING7_TIMEOUT] = 0x48,
  231. [DMA_RING8_TIMEOUT] = 0x4c,
  232. [DMA_RING9_TIMEOUT] = 0x50,
  233. [DMA_RING10_TIMEOUT] = 0x54,
  234. [DMA_RING11_TIMEOUT] = 0x58,
  235. [DMA_RING12_TIMEOUT] = 0x5c,
  236. [DMA_RING13_TIMEOUT] = 0x60,
  237. [DMA_RING14_TIMEOUT] = 0x64,
  238. [DMA_RING15_TIMEOUT] = 0x68,
  239. [DMA_RING16_TIMEOUT] = 0x6C,
  240. [DMA_INDEX2RING_0] = 0x70,
  241. [DMA_INDEX2RING_1] = 0x74,
  242. [DMA_INDEX2RING_2] = 0x78,
  243. [DMA_INDEX2RING_3] = 0x7C,
  244. [DMA_INDEX2RING_4] = 0x80,
  245. [DMA_INDEX2RING_5] = 0x84,
  246. [DMA_INDEX2RING_6] = 0x88,
  247. [DMA_INDEX2RING_7] = 0x8C,
  248. };
  249. static const u8 bcmgenet_dma_regs_v2[] = {
  250. [DMA_RING_CFG] = 0x00,
  251. [DMA_CTRL] = 0x04,
  252. [DMA_STATUS] = 0x08,
  253. [DMA_SCB_BURST_SIZE] = 0x0C,
  254. [DMA_ARB_CTRL] = 0x30,
  255. [DMA_PRIORITY_0] = 0x34,
  256. [DMA_PRIORITY_1] = 0x38,
  257. [DMA_PRIORITY_2] = 0x3C,
  258. [DMA_RING0_TIMEOUT] = 0x2C,
  259. [DMA_RING1_TIMEOUT] = 0x30,
  260. [DMA_RING2_TIMEOUT] = 0x34,
  261. [DMA_RING3_TIMEOUT] = 0x38,
  262. [DMA_RING4_TIMEOUT] = 0x3c,
  263. [DMA_RING5_TIMEOUT] = 0x40,
  264. [DMA_RING6_TIMEOUT] = 0x44,
  265. [DMA_RING7_TIMEOUT] = 0x48,
  266. [DMA_RING8_TIMEOUT] = 0x4c,
  267. [DMA_RING9_TIMEOUT] = 0x50,
  268. [DMA_RING10_TIMEOUT] = 0x54,
  269. [DMA_RING11_TIMEOUT] = 0x58,
  270. [DMA_RING12_TIMEOUT] = 0x5c,
  271. [DMA_RING13_TIMEOUT] = 0x60,
  272. [DMA_RING14_TIMEOUT] = 0x64,
  273. [DMA_RING15_TIMEOUT] = 0x68,
  274. [DMA_RING16_TIMEOUT] = 0x6C,
  275. };
  276. static const u8 bcmgenet_dma_regs_v1[] = {
  277. [DMA_CTRL] = 0x00,
  278. [DMA_STATUS] = 0x04,
  279. [DMA_SCB_BURST_SIZE] = 0x0C,
  280. [DMA_ARB_CTRL] = 0x30,
  281. [DMA_PRIORITY_0] = 0x34,
  282. [DMA_PRIORITY_1] = 0x38,
  283. [DMA_PRIORITY_2] = 0x3C,
  284. [DMA_RING0_TIMEOUT] = 0x2C,
  285. [DMA_RING1_TIMEOUT] = 0x30,
  286. [DMA_RING2_TIMEOUT] = 0x34,
  287. [DMA_RING3_TIMEOUT] = 0x38,
  288. [DMA_RING4_TIMEOUT] = 0x3c,
  289. [DMA_RING5_TIMEOUT] = 0x40,
  290. [DMA_RING6_TIMEOUT] = 0x44,
  291. [DMA_RING7_TIMEOUT] = 0x48,
  292. [DMA_RING8_TIMEOUT] = 0x4c,
  293. [DMA_RING9_TIMEOUT] = 0x50,
  294. [DMA_RING10_TIMEOUT] = 0x54,
  295. [DMA_RING11_TIMEOUT] = 0x58,
  296. [DMA_RING12_TIMEOUT] = 0x5c,
  297. [DMA_RING13_TIMEOUT] = 0x60,
  298. [DMA_RING14_TIMEOUT] = 0x64,
  299. [DMA_RING15_TIMEOUT] = 0x68,
  300. [DMA_RING16_TIMEOUT] = 0x6C,
  301. };
  302. /* Set at runtime once bcmgenet version is known */
  303. static const u8 *bcmgenet_dma_regs;
  304. static inline struct bcmgenet_priv *dev_to_priv(struct device *dev)
  305. {
  306. return netdev_priv(dev_get_drvdata(dev));
  307. }
  308. static inline u32 bcmgenet_tdma_readl(struct bcmgenet_priv *priv,
  309. enum dma_reg r)
  310. {
  311. return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
  312. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  313. }
  314. static inline void bcmgenet_tdma_writel(struct bcmgenet_priv *priv,
  315. u32 val, enum dma_reg r)
  316. {
  317. bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
  318. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  319. }
  320. static inline u32 bcmgenet_rdma_readl(struct bcmgenet_priv *priv,
  321. enum dma_reg r)
  322. {
  323. return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
  324. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  325. }
  326. static inline void bcmgenet_rdma_writel(struct bcmgenet_priv *priv,
  327. u32 val, enum dma_reg r)
  328. {
  329. bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
  330. DMA_RINGS_SIZE + bcmgenet_dma_regs[r]);
  331. }
  332. /* RDMA/TDMA ring registers and accessors
  333. * we merge the common fields and just prefix with T/D the registers
  334. * having different meaning depending on the direction
  335. */
  336. enum dma_ring_reg {
  337. TDMA_READ_PTR = 0,
  338. RDMA_WRITE_PTR = TDMA_READ_PTR,
  339. TDMA_READ_PTR_HI,
  340. RDMA_WRITE_PTR_HI = TDMA_READ_PTR_HI,
  341. TDMA_CONS_INDEX,
  342. RDMA_PROD_INDEX = TDMA_CONS_INDEX,
  343. TDMA_PROD_INDEX,
  344. RDMA_CONS_INDEX = TDMA_PROD_INDEX,
  345. DMA_RING_BUF_SIZE,
  346. DMA_START_ADDR,
  347. DMA_START_ADDR_HI,
  348. DMA_END_ADDR,
  349. DMA_END_ADDR_HI,
  350. DMA_MBUF_DONE_THRESH,
  351. TDMA_FLOW_PERIOD,
  352. RDMA_XON_XOFF_THRESH = TDMA_FLOW_PERIOD,
  353. TDMA_WRITE_PTR,
  354. RDMA_READ_PTR = TDMA_WRITE_PTR,
  355. TDMA_WRITE_PTR_HI,
  356. RDMA_READ_PTR_HI = TDMA_WRITE_PTR_HI
  357. };
  358. /* GENET v4 supports 40-bits pointer addressing
  359. * for obvious reasons the LO and HI word parts
  360. * are contiguous, but this offsets the other
  361. * registers.
  362. */
  363. static const u8 genet_dma_ring_regs_v4[] = {
  364. [TDMA_READ_PTR] = 0x00,
  365. [TDMA_READ_PTR_HI] = 0x04,
  366. [TDMA_CONS_INDEX] = 0x08,
  367. [TDMA_PROD_INDEX] = 0x0C,
  368. [DMA_RING_BUF_SIZE] = 0x10,
  369. [DMA_START_ADDR] = 0x14,
  370. [DMA_START_ADDR_HI] = 0x18,
  371. [DMA_END_ADDR] = 0x1C,
  372. [DMA_END_ADDR_HI] = 0x20,
  373. [DMA_MBUF_DONE_THRESH] = 0x24,
  374. [TDMA_FLOW_PERIOD] = 0x28,
  375. [TDMA_WRITE_PTR] = 0x2C,
  376. [TDMA_WRITE_PTR_HI] = 0x30,
  377. };
  378. static const u8 genet_dma_ring_regs_v123[] = {
  379. [TDMA_READ_PTR] = 0x00,
  380. [TDMA_CONS_INDEX] = 0x04,
  381. [TDMA_PROD_INDEX] = 0x08,
  382. [DMA_RING_BUF_SIZE] = 0x0C,
  383. [DMA_START_ADDR] = 0x10,
  384. [DMA_END_ADDR] = 0x14,
  385. [DMA_MBUF_DONE_THRESH] = 0x18,
  386. [TDMA_FLOW_PERIOD] = 0x1C,
  387. [TDMA_WRITE_PTR] = 0x20,
  388. };
  389. /* Set at runtime once GENET version is known */
  390. static const u8 *genet_dma_ring_regs;
  391. static inline u32 bcmgenet_tdma_ring_readl(struct bcmgenet_priv *priv,
  392. unsigned int ring,
  393. enum dma_ring_reg r)
  394. {
  395. return bcmgenet_readl(priv->base + GENET_TDMA_REG_OFF +
  396. (DMA_RING_SIZE * ring) +
  397. genet_dma_ring_regs[r]);
  398. }
  399. static inline void bcmgenet_tdma_ring_writel(struct bcmgenet_priv *priv,
  400. unsigned int ring, u32 val,
  401. enum dma_ring_reg r)
  402. {
  403. bcmgenet_writel(val, priv->base + GENET_TDMA_REG_OFF +
  404. (DMA_RING_SIZE * ring) +
  405. genet_dma_ring_regs[r]);
  406. }
  407. static inline u32 bcmgenet_rdma_ring_readl(struct bcmgenet_priv *priv,
  408. unsigned int ring,
  409. enum dma_ring_reg r)
  410. {
  411. return bcmgenet_readl(priv->base + GENET_RDMA_REG_OFF +
  412. (DMA_RING_SIZE * ring) +
  413. genet_dma_ring_regs[r]);
  414. }
  415. static inline void bcmgenet_rdma_ring_writel(struct bcmgenet_priv *priv,
  416. unsigned int ring, u32 val,
  417. enum dma_ring_reg r)
  418. {
  419. bcmgenet_writel(val, priv->base + GENET_RDMA_REG_OFF +
  420. (DMA_RING_SIZE * ring) +
  421. genet_dma_ring_regs[r]);
  422. }
  423. static int bcmgenet_begin(struct net_device *dev)
  424. {
  425. struct bcmgenet_priv *priv = netdev_priv(dev);
  426. /* Turn on the clock */
  427. return clk_prepare_enable(priv->clk);
  428. }
  429. static void bcmgenet_complete(struct net_device *dev)
  430. {
  431. struct bcmgenet_priv *priv = netdev_priv(dev);
  432. /* Turn off the clock */
  433. clk_disable_unprepare(priv->clk);
  434. }
  435. static int bcmgenet_get_link_ksettings(struct net_device *dev,
  436. struct ethtool_link_ksettings *cmd)
  437. {
  438. if (!netif_running(dev))
  439. return -EINVAL;
  440. if (!dev->phydev)
  441. return -ENODEV;
  442. phy_ethtool_ksettings_get(dev->phydev, cmd);
  443. return 0;
  444. }
  445. static int bcmgenet_set_link_ksettings(struct net_device *dev,
  446. const struct ethtool_link_ksettings *cmd)
  447. {
  448. if (!netif_running(dev))
  449. return -EINVAL;
  450. if (!dev->phydev)
  451. return -ENODEV;
  452. return phy_ethtool_ksettings_set(dev->phydev, cmd);
  453. }
  454. static int bcmgenet_set_rx_csum(struct net_device *dev,
  455. netdev_features_t wanted)
  456. {
  457. struct bcmgenet_priv *priv = netdev_priv(dev);
  458. u32 rbuf_chk_ctrl;
  459. bool rx_csum_en;
  460. rx_csum_en = !!(wanted & NETIF_F_RXCSUM);
  461. rbuf_chk_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CHK_CTRL);
  462. /* enable rx checksumming */
  463. if (rx_csum_en)
  464. rbuf_chk_ctrl |= RBUF_RXCHK_EN;
  465. else
  466. rbuf_chk_ctrl &= ~RBUF_RXCHK_EN;
  467. priv->desc_rxchk_en = rx_csum_en;
  468. /* If UniMAC forwards CRC, we need to skip over it to get
  469. * a valid CHK bit to be set in the per-packet status word
  470. */
  471. if (rx_csum_en && priv->crc_fwd_en)
  472. rbuf_chk_ctrl |= RBUF_SKIP_FCS;
  473. else
  474. rbuf_chk_ctrl &= ~RBUF_SKIP_FCS;
  475. bcmgenet_rbuf_writel(priv, rbuf_chk_ctrl, RBUF_CHK_CTRL);
  476. return 0;
  477. }
  478. static int bcmgenet_set_tx_csum(struct net_device *dev,
  479. netdev_features_t wanted)
  480. {
  481. struct bcmgenet_priv *priv = netdev_priv(dev);
  482. bool desc_64b_en;
  483. u32 tbuf_ctrl, rbuf_ctrl;
  484. tbuf_ctrl = bcmgenet_tbuf_ctrl_get(priv);
  485. rbuf_ctrl = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
  486. desc_64b_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
  487. /* enable 64 bytes descriptor in both directions (RBUF and TBUF) */
  488. if (desc_64b_en) {
  489. tbuf_ctrl |= RBUF_64B_EN;
  490. rbuf_ctrl |= RBUF_64B_EN;
  491. } else {
  492. tbuf_ctrl &= ~RBUF_64B_EN;
  493. rbuf_ctrl &= ~RBUF_64B_EN;
  494. }
  495. priv->desc_64b_en = desc_64b_en;
  496. bcmgenet_tbuf_ctrl_set(priv, tbuf_ctrl);
  497. bcmgenet_rbuf_writel(priv, rbuf_ctrl, RBUF_CTRL);
  498. return 0;
  499. }
  500. static int bcmgenet_set_features(struct net_device *dev,
  501. netdev_features_t features)
  502. {
  503. netdev_features_t changed = features ^ dev->features;
  504. netdev_features_t wanted = dev->wanted_features;
  505. int ret = 0;
  506. if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
  507. ret = bcmgenet_set_tx_csum(dev, wanted);
  508. if (changed & (NETIF_F_RXCSUM))
  509. ret = bcmgenet_set_rx_csum(dev, wanted);
  510. return ret;
  511. }
  512. static u32 bcmgenet_get_msglevel(struct net_device *dev)
  513. {
  514. struct bcmgenet_priv *priv = netdev_priv(dev);
  515. return priv->msg_enable;
  516. }
  517. static void bcmgenet_set_msglevel(struct net_device *dev, u32 level)
  518. {
  519. struct bcmgenet_priv *priv = netdev_priv(dev);
  520. priv->msg_enable = level;
  521. }
  522. static int bcmgenet_get_coalesce(struct net_device *dev,
  523. struct ethtool_coalesce *ec)
  524. {
  525. struct bcmgenet_priv *priv = netdev_priv(dev);
  526. struct bcmgenet_rx_ring *ring;
  527. unsigned int i;
  528. ec->tx_max_coalesced_frames =
  529. bcmgenet_tdma_ring_readl(priv, DESC_INDEX,
  530. DMA_MBUF_DONE_THRESH);
  531. ec->rx_max_coalesced_frames =
  532. bcmgenet_rdma_ring_readl(priv, DESC_INDEX,
  533. DMA_MBUF_DONE_THRESH);
  534. ec->rx_coalesce_usecs =
  535. bcmgenet_rdma_readl(priv, DMA_RING16_TIMEOUT) * 8192 / 1000;
  536. for (i = 0; i < priv->hw_params->rx_queues; i++) {
  537. ring = &priv->rx_rings[i];
  538. ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
  539. }
  540. ring = &priv->rx_rings[DESC_INDEX];
  541. ec->use_adaptive_rx_coalesce |= ring->dim.use_dim;
  542. return 0;
  543. }
  544. static void bcmgenet_set_rx_coalesce(struct bcmgenet_rx_ring *ring,
  545. u32 usecs, u32 pkts)
  546. {
  547. struct bcmgenet_priv *priv = ring->priv;
  548. unsigned int i = ring->index;
  549. u32 reg;
  550. bcmgenet_rdma_ring_writel(priv, i, pkts, DMA_MBUF_DONE_THRESH);
  551. reg = bcmgenet_rdma_readl(priv, DMA_RING0_TIMEOUT + i);
  552. reg &= ~DMA_TIMEOUT_MASK;
  553. reg |= DIV_ROUND_UP(usecs * 1000, 8192);
  554. bcmgenet_rdma_writel(priv, reg, DMA_RING0_TIMEOUT + i);
  555. }
  556. static void bcmgenet_set_ring_rx_coalesce(struct bcmgenet_rx_ring *ring,
  557. struct ethtool_coalesce *ec)
  558. {
  559. struct net_dim_cq_moder moder;
  560. u32 usecs, pkts;
  561. ring->rx_coalesce_usecs = ec->rx_coalesce_usecs;
  562. ring->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  563. usecs = ring->rx_coalesce_usecs;
  564. pkts = ring->rx_max_coalesced_frames;
  565. if (ec->use_adaptive_rx_coalesce && !ring->dim.use_dim) {
  566. moder = net_dim_get_def_rx_moderation(ring->dim.dim.mode);
  567. usecs = moder.usec;
  568. pkts = moder.pkts;
  569. }
  570. ring->dim.use_dim = ec->use_adaptive_rx_coalesce;
  571. bcmgenet_set_rx_coalesce(ring, usecs, pkts);
  572. }
  573. static int bcmgenet_set_coalesce(struct net_device *dev,
  574. struct ethtool_coalesce *ec)
  575. {
  576. struct bcmgenet_priv *priv = netdev_priv(dev);
  577. unsigned int i;
  578. /* Base system clock is 125Mhz, DMA timeout is this reference clock
  579. * divided by 1024, which yields roughly 8.192us, our maximum value
  580. * has to fit in the DMA_TIMEOUT_MASK (16 bits)
  581. */
  582. if (ec->tx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
  583. ec->tx_max_coalesced_frames == 0 ||
  584. ec->rx_max_coalesced_frames > DMA_INTR_THRESHOLD_MASK ||
  585. ec->rx_coalesce_usecs > (DMA_TIMEOUT_MASK * 8) + 1)
  586. return -EINVAL;
  587. if (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0)
  588. return -EINVAL;
  589. /* GENET TDMA hardware does not support a configurable timeout, but will
  590. * always generate an interrupt either after MBDONE packets have been
  591. * transmitted, or when the ring is empty.
  592. */
  593. if (ec->tx_coalesce_usecs || ec->tx_coalesce_usecs_high ||
  594. ec->tx_coalesce_usecs_irq || ec->tx_coalesce_usecs_low ||
  595. ec->use_adaptive_tx_coalesce)
  596. return -EOPNOTSUPP;
  597. /* Program all TX queues with the same values, as there is no
  598. * ethtool knob to do coalescing on a per-queue basis
  599. */
  600. for (i = 0; i < priv->hw_params->tx_queues; i++)
  601. bcmgenet_tdma_ring_writel(priv, i,
  602. ec->tx_max_coalesced_frames,
  603. DMA_MBUF_DONE_THRESH);
  604. bcmgenet_tdma_ring_writel(priv, DESC_INDEX,
  605. ec->tx_max_coalesced_frames,
  606. DMA_MBUF_DONE_THRESH);
  607. for (i = 0; i < priv->hw_params->rx_queues; i++)
  608. bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[i], ec);
  609. bcmgenet_set_ring_rx_coalesce(&priv->rx_rings[DESC_INDEX], ec);
  610. return 0;
  611. }
  612. /* standard ethtool support functions. */
  613. enum bcmgenet_stat_type {
  614. BCMGENET_STAT_NETDEV = -1,
  615. BCMGENET_STAT_MIB_RX,
  616. BCMGENET_STAT_MIB_TX,
  617. BCMGENET_STAT_RUNT,
  618. BCMGENET_STAT_MISC,
  619. BCMGENET_STAT_SOFT,
  620. };
  621. struct bcmgenet_stats {
  622. char stat_string[ETH_GSTRING_LEN];
  623. int stat_sizeof;
  624. int stat_offset;
  625. enum bcmgenet_stat_type type;
  626. /* reg offset from UMAC base for misc counters */
  627. u16 reg_offset;
  628. };
  629. #define STAT_NETDEV(m) { \
  630. .stat_string = __stringify(m), \
  631. .stat_sizeof = sizeof(((struct net_device_stats *)0)->m), \
  632. .stat_offset = offsetof(struct net_device_stats, m), \
  633. .type = BCMGENET_STAT_NETDEV, \
  634. }
  635. #define STAT_GENET_MIB(str, m, _type) { \
  636. .stat_string = str, \
  637. .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
  638. .stat_offset = offsetof(struct bcmgenet_priv, m), \
  639. .type = _type, \
  640. }
  641. #define STAT_GENET_MIB_RX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_RX)
  642. #define STAT_GENET_MIB_TX(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_MIB_TX)
  643. #define STAT_GENET_RUNT(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_RUNT)
  644. #define STAT_GENET_SOFT_MIB(str, m) STAT_GENET_MIB(str, m, BCMGENET_STAT_SOFT)
  645. #define STAT_GENET_MISC(str, m, offset) { \
  646. .stat_string = str, \
  647. .stat_sizeof = sizeof(((struct bcmgenet_priv *)0)->m), \
  648. .stat_offset = offsetof(struct bcmgenet_priv, m), \
  649. .type = BCMGENET_STAT_MISC, \
  650. .reg_offset = offset, \
  651. }
  652. #define STAT_GENET_Q(num) \
  653. STAT_GENET_SOFT_MIB("txq" __stringify(num) "_packets", \
  654. tx_rings[num].packets), \
  655. STAT_GENET_SOFT_MIB("txq" __stringify(num) "_bytes", \
  656. tx_rings[num].bytes), \
  657. STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_bytes", \
  658. rx_rings[num].bytes), \
  659. STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_packets", \
  660. rx_rings[num].packets), \
  661. STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_errors", \
  662. rx_rings[num].errors), \
  663. STAT_GENET_SOFT_MIB("rxq" __stringify(num) "_dropped", \
  664. rx_rings[num].dropped)
  665. /* There is a 0xC gap between the end of RX and beginning of TX stats and then
  666. * between the end of TX stats and the beginning of the RX RUNT
  667. */
  668. #define BCMGENET_STAT_OFFSET 0xc
  669. /* Hardware counters must be kept in sync because the order/offset
  670. * is important here (order in structure declaration = order in hardware)
  671. */
  672. static const struct bcmgenet_stats bcmgenet_gstrings_stats[] = {
  673. /* general stats */
  674. STAT_NETDEV(rx_packets),
  675. STAT_NETDEV(tx_packets),
  676. STAT_NETDEV(rx_bytes),
  677. STAT_NETDEV(tx_bytes),
  678. STAT_NETDEV(rx_errors),
  679. STAT_NETDEV(tx_errors),
  680. STAT_NETDEV(rx_dropped),
  681. STAT_NETDEV(tx_dropped),
  682. STAT_NETDEV(multicast),
  683. /* UniMAC RSV counters */
  684. STAT_GENET_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
  685. STAT_GENET_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
  686. STAT_GENET_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
  687. STAT_GENET_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
  688. STAT_GENET_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
  689. STAT_GENET_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
  690. STAT_GENET_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
  691. STAT_GENET_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
  692. STAT_GENET_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
  693. STAT_GENET_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
  694. STAT_GENET_MIB_RX("rx_pkts", mib.rx.pkt),
  695. STAT_GENET_MIB_RX("rx_bytes", mib.rx.bytes),
  696. STAT_GENET_MIB_RX("rx_multicast", mib.rx.mca),
  697. STAT_GENET_MIB_RX("rx_broadcast", mib.rx.bca),
  698. STAT_GENET_MIB_RX("rx_fcs", mib.rx.fcs),
  699. STAT_GENET_MIB_RX("rx_control", mib.rx.cf),
  700. STAT_GENET_MIB_RX("rx_pause", mib.rx.pf),
  701. STAT_GENET_MIB_RX("rx_unknown", mib.rx.uo),
  702. STAT_GENET_MIB_RX("rx_align", mib.rx.aln),
  703. STAT_GENET_MIB_RX("rx_outrange", mib.rx.flr),
  704. STAT_GENET_MIB_RX("rx_code", mib.rx.cde),
  705. STAT_GENET_MIB_RX("rx_carrier", mib.rx.fcr),
  706. STAT_GENET_MIB_RX("rx_oversize", mib.rx.ovr),
  707. STAT_GENET_MIB_RX("rx_jabber", mib.rx.jbr),
  708. STAT_GENET_MIB_RX("rx_mtu_err", mib.rx.mtue),
  709. STAT_GENET_MIB_RX("rx_good_pkts", mib.rx.pok),
  710. STAT_GENET_MIB_RX("rx_unicast", mib.rx.uc),
  711. STAT_GENET_MIB_RX("rx_ppp", mib.rx.ppp),
  712. STAT_GENET_MIB_RX("rx_crc", mib.rx.rcrc),
  713. /* UniMAC TSV counters */
  714. STAT_GENET_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
  715. STAT_GENET_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
  716. STAT_GENET_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
  717. STAT_GENET_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
  718. STAT_GENET_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
  719. STAT_GENET_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
  720. STAT_GENET_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
  721. STAT_GENET_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
  722. STAT_GENET_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
  723. STAT_GENET_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
  724. STAT_GENET_MIB_TX("tx_pkts", mib.tx.pkts),
  725. STAT_GENET_MIB_TX("tx_multicast", mib.tx.mca),
  726. STAT_GENET_MIB_TX("tx_broadcast", mib.tx.bca),
  727. STAT_GENET_MIB_TX("tx_pause", mib.tx.pf),
  728. STAT_GENET_MIB_TX("tx_control", mib.tx.cf),
  729. STAT_GENET_MIB_TX("tx_fcs_err", mib.tx.fcs),
  730. STAT_GENET_MIB_TX("tx_oversize", mib.tx.ovr),
  731. STAT_GENET_MIB_TX("tx_defer", mib.tx.drf),
  732. STAT_GENET_MIB_TX("tx_excess_defer", mib.tx.edf),
  733. STAT_GENET_MIB_TX("tx_single_col", mib.tx.scl),
  734. STAT_GENET_MIB_TX("tx_multi_col", mib.tx.mcl),
  735. STAT_GENET_MIB_TX("tx_late_col", mib.tx.lcl),
  736. STAT_GENET_MIB_TX("tx_excess_col", mib.tx.ecl),
  737. STAT_GENET_MIB_TX("tx_frags", mib.tx.frg),
  738. STAT_GENET_MIB_TX("tx_total_col", mib.tx.ncl),
  739. STAT_GENET_MIB_TX("tx_jabber", mib.tx.jbr),
  740. STAT_GENET_MIB_TX("tx_bytes", mib.tx.bytes),
  741. STAT_GENET_MIB_TX("tx_good_pkts", mib.tx.pok),
  742. STAT_GENET_MIB_TX("tx_unicast", mib.tx.uc),
  743. /* UniMAC RUNT counters */
  744. STAT_GENET_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
  745. STAT_GENET_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
  746. STAT_GENET_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
  747. STAT_GENET_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
  748. /* Misc UniMAC counters */
  749. STAT_GENET_MISC("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt,
  750. UMAC_RBUF_OVFL_CNT_V1),
  751. STAT_GENET_MISC("rbuf_err_cnt", mib.rbuf_err_cnt,
  752. UMAC_RBUF_ERR_CNT_V1),
  753. STAT_GENET_MISC("mdf_err_cnt", mib.mdf_err_cnt, UMAC_MDF_ERR_CNT),
  754. STAT_GENET_SOFT_MIB("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
  755. STAT_GENET_SOFT_MIB("rx_dma_failed", mib.rx_dma_failed),
  756. STAT_GENET_SOFT_MIB("tx_dma_failed", mib.tx_dma_failed),
  757. /* Per TX queues */
  758. STAT_GENET_Q(0),
  759. STAT_GENET_Q(1),
  760. STAT_GENET_Q(2),
  761. STAT_GENET_Q(3),
  762. STAT_GENET_Q(16),
  763. };
  764. #define BCMGENET_STATS_LEN ARRAY_SIZE(bcmgenet_gstrings_stats)
  765. static void bcmgenet_get_drvinfo(struct net_device *dev,
  766. struct ethtool_drvinfo *info)
  767. {
  768. strlcpy(info->driver, "bcmgenet", sizeof(info->driver));
  769. strlcpy(info->version, "v2.0", sizeof(info->version));
  770. }
  771. static int bcmgenet_get_sset_count(struct net_device *dev, int string_set)
  772. {
  773. switch (string_set) {
  774. case ETH_SS_STATS:
  775. return BCMGENET_STATS_LEN;
  776. default:
  777. return -EOPNOTSUPP;
  778. }
  779. }
  780. static void bcmgenet_get_strings(struct net_device *dev, u32 stringset,
  781. u8 *data)
  782. {
  783. int i;
  784. switch (stringset) {
  785. case ETH_SS_STATS:
  786. for (i = 0; i < BCMGENET_STATS_LEN; i++) {
  787. memcpy(data + i * ETH_GSTRING_LEN,
  788. bcmgenet_gstrings_stats[i].stat_string,
  789. ETH_GSTRING_LEN);
  790. }
  791. break;
  792. }
  793. }
  794. static u32 bcmgenet_update_stat_misc(struct bcmgenet_priv *priv, u16 offset)
  795. {
  796. u16 new_offset;
  797. u32 val;
  798. switch (offset) {
  799. case UMAC_RBUF_OVFL_CNT_V1:
  800. if (GENET_IS_V2(priv))
  801. new_offset = RBUF_OVFL_CNT_V2;
  802. else
  803. new_offset = RBUF_OVFL_CNT_V3PLUS;
  804. val = bcmgenet_rbuf_readl(priv, new_offset);
  805. /* clear if overflowed */
  806. if (val == ~0)
  807. bcmgenet_rbuf_writel(priv, 0, new_offset);
  808. break;
  809. case UMAC_RBUF_ERR_CNT_V1:
  810. if (GENET_IS_V2(priv))
  811. new_offset = RBUF_ERR_CNT_V2;
  812. else
  813. new_offset = RBUF_ERR_CNT_V3PLUS;
  814. val = bcmgenet_rbuf_readl(priv, new_offset);
  815. /* clear if overflowed */
  816. if (val == ~0)
  817. bcmgenet_rbuf_writel(priv, 0, new_offset);
  818. break;
  819. default:
  820. val = bcmgenet_umac_readl(priv, offset);
  821. /* clear if overflowed */
  822. if (val == ~0)
  823. bcmgenet_umac_writel(priv, 0, offset);
  824. break;
  825. }
  826. return val;
  827. }
  828. static void bcmgenet_update_mib_counters(struct bcmgenet_priv *priv)
  829. {
  830. int i, j = 0;
  831. for (i = 0; i < BCMGENET_STATS_LEN; i++) {
  832. const struct bcmgenet_stats *s;
  833. u8 offset = 0;
  834. u32 val = 0;
  835. char *p;
  836. s = &bcmgenet_gstrings_stats[i];
  837. switch (s->type) {
  838. case BCMGENET_STAT_NETDEV:
  839. case BCMGENET_STAT_SOFT:
  840. continue;
  841. case BCMGENET_STAT_RUNT:
  842. offset += BCMGENET_STAT_OFFSET;
  843. /* fall through */
  844. case BCMGENET_STAT_MIB_TX:
  845. offset += BCMGENET_STAT_OFFSET;
  846. /* fall through */
  847. case BCMGENET_STAT_MIB_RX:
  848. val = bcmgenet_umac_readl(priv,
  849. UMAC_MIB_START + j + offset);
  850. offset = 0; /* Reset Offset */
  851. break;
  852. case BCMGENET_STAT_MISC:
  853. if (GENET_IS_V1(priv)) {
  854. val = bcmgenet_umac_readl(priv, s->reg_offset);
  855. /* clear if overflowed */
  856. if (val == ~0)
  857. bcmgenet_umac_writel(priv, 0,
  858. s->reg_offset);
  859. } else {
  860. val = bcmgenet_update_stat_misc(priv,
  861. s->reg_offset);
  862. }
  863. break;
  864. }
  865. j += s->stat_sizeof;
  866. p = (char *)priv + s->stat_offset;
  867. *(u32 *)p = val;
  868. }
  869. }
  870. static void bcmgenet_get_ethtool_stats(struct net_device *dev,
  871. struct ethtool_stats *stats,
  872. u64 *data)
  873. {
  874. struct bcmgenet_priv *priv = netdev_priv(dev);
  875. int i;
  876. if (netif_running(dev))
  877. bcmgenet_update_mib_counters(priv);
  878. for (i = 0; i < BCMGENET_STATS_LEN; i++) {
  879. const struct bcmgenet_stats *s;
  880. char *p;
  881. s = &bcmgenet_gstrings_stats[i];
  882. if (s->type == BCMGENET_STAT_NETDEV)
  883. p = (char *)&dev->stats;
  884. else
  885. p = (char *)priv;
  886. p += s->stat_offset;
  887. if (sizeof(unsigned long) != sizeof(u32) &&
  888. s->stat_sizeof == sizeof(unsigned long))
  889. data[i] = *(unsigned long *)p;
  890. else
  891. data[i] = *(u32 *)p;
  892. }
  893. }
  894. static void bcmgenet_eee_enable_set(struct net_device *dev, bool enable)
  895. {
  896. struct bcmgenet_priv *priv = netdev_priv(dev);
  897. u32 off = priv->hw_params->tbuf_offset + TBUF_ENERGY_CTRL;
  898. u32 reg;
  899. if (enable && !priv->clk_eee_enabled) {
  900. clk_prepare_enable(priv->clk_eee);
  901. priv->clk_eee_enabled = true;
  902. }
  903. reg = bcmgenet_umac_readl(priv, UMAC_EEE_CTRL);
  904. if (enable)
  905. reg |= EEE_EN;
  906. else
  907. reg &= ~EEE_EN;
  908. bcmgenet_umac_writel(priv, reg, UMAC_EEE_CTRL);
  909. /* Enable EEE and switch to a 27Mhz clock automatically */
  910. reg = bcmgenet_readl(priv->base + off);
  911. if (enable)
  912. reg |= TBUF_EEE_EN | TBUF_PM_EN;
  913. else
  914. reg &= ~(TBUF_EEE_EN | TBUF_PM_EN);
  915. bcmgenet_writel(reg, priv->base + off);
  916. /* Do the same for thing for RBUF */
  917. reg = bcmgenet_rbuf_readl(priv, RBUF_ENERGY_CTRL);
  918. if (enable)
  919. reg |= RBUF_EEE_EN | RBUF_PM_EN;
  920. else
  921. reg &= ~(RBUF_EEE_EN | RBUF_PM_EN);
  922. bcmgenet_rbuf_writel(priv, reg, RBUF_ENERGY_CTRL);
  923. if (!enable && priv->clk_eee_enabled) {
  924. clk_disable_unprepare(priv->clk_eee);
  925. priv->clk_eee_enabled = false;
  926. }
  927. priv->eee.eee_enabled = enable;
  928. priv->eee.eee_active = enable;
  929. }
  930. static int bcmgenet_get_eee(struct net_device *dev, struct ethtool_eee *e)
  931. {
  932. struct bcmgenet_priv *priv = netdev_priv(dev);
  933. struct ethtool_eee *p = &priv->eee;
  934. if (GENET_IS_V1(priv))
  935. return -EOPNOTSUPP;
  936. if (!dev->phydev)
  937. return -ENODEV;
  938. e->eee_enabled = p->eee_enabled;
  939. e->eee_active = p->eee_active;
  940. e->tx_lpi_timer = bcmgenet_umac_readl(priv, UMAC_EEE_LPI_TIMER);
  941. return phy_ethtool_get_eee(dev->phydev, e);
  942. }
  943. static int bcmgenet_set_eee(struct net_device *dev, struct ethtool_eee *e)
  944. {
  945. struct bcmgenet_priv *priv = netdev_priv(dev);
  946. struct ethtool_eee *p = &priv->eee;
  947. int ret = 0;
  948. if (GENET_IS_V1(priv))
  949. return -EOPNOTSUPP;
  950. if (!dev->phydev)
  951. return -ENODEV;
  952. p->eee_enabled = e->eee_enabled;
  953. if (!p->eee_enabled) {
  954. bcmgenet_eee_enable_set(dev, false);
  955. } else {
  956. ret = phy_init_eee(dev->phydev, 0);
  957. if (ret) {
  958. netif_err(priv, hw, dev, "EEE initialization failed\n");
  959. return ret;
  960. }
  961. bcmgenet_umac_writel(priv, e->tx_lpi_timer, UMAC_EEE_LPI_TIMER);
  962. bcmgenet_eee_enable_set(dev, true);
  963. }
  964. return phy_ethtool_set_eee(dev->phydev, e);
  965. }
  966. /* standard ethtool support functions. */
  967. static const struct ethtool_ops bcmgenet_ethtool_ops = {
  968. .begin = bcmgenet_begin,
  969. .complete = bcmgenet_complete,
  970. .get_strings = bcmgenet_get_strings,
  971. .get_sset_count = bcmgenet_get_sset_count,
  972. .get_ethtool_stats = bcmgenet_get_ethtool_stats,
  973. .get_drvinfo = bcmgenet_get_drvinfo,
  974. .get_link = ethtool_op_get_link,
  975. .get_msglevel = bcmgenet_get_msglevel,
  976. .set_msglevel = bcmgenet_set_msglevel,
  977. .get_wol = bcmgenet_get_wol,
  978. .set_wol = bcmgenet_set_wol,
  979. .get_eee = bcmgenet_get_eee,
  980. .set_eee = bcmgenet_set_eee,
  981. .nway_reset = phy_ethtool_nway_reset,
  982. .get_coalesce = bcmgenet_get_coalesce,
  983. .set_coalesce = bcmgenet_set_coalesce,
  984. .get_link_ksettings = bcmgenet_get_link_ksettings,
  985. .set_link_ksettings = bcmgenet_set_link_ksettings,
  986. };
  987. /* Power down the unimac, based on mode. */
  988. static int bcmgenet_power_down(struct bcmgenet_priv *priv,
  989. enum bcmgenet_power_mode mode)
  990. {
  991. int ret = 0;
  992. u32 reg;
  993. switch (mode) {
  994. case GENET_POWER_CABLE_SENSE:
  995. phy_detach(priv->dev->phydev);
  996. break;
  997. case GENET_POWER_WOL_MAGIC:
  998. ret = bcmgenet_wol_power_down_cfg(priv, mode);
  999. break;
  1000. case GENET_POWER_PASSIVE:
  1001. /* Power down LED */
  1002. if (priv->hw_params->flags & GENET_HAS_EXT) {
  1003. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  1004. if (GENET_IS_V5(priv))
  1005. reg |= EXT_PWR_DOWN_PHY_EN |
  1006. EXT_PWR_DOWN_PHY_RD |
  1007. EXT_PWR_DOWN_PHY_SD |
  1008. EXT_PWR_DOWN_PHY_RX |
  1009. EXT_PWR_DOWN_PHY_TX |
  1010. EXT_IDDQ_GLBL_PWR;
  1011. else
  1012. reg |= EXT_PWR_DOWN_PHY;
  1013. reg |= (EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
  1014. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  1015. bcmgenet_phy_power_set(priv->dev, false);
  1016. }
  1017. break;
  1018. default:
  1019. break;
  1020. }
  1021. return ret;
  1022. }
  1023. static void bcmgenet_power_up(struct bcmgenet_priv *priv,
  1024. enum bcmgenet_power_mode mode)
  1025. {
  1026. u32 reg;
  1027. if (!(priv->hw_params->flags & GENET_HAS_EXT))
  1028. return;
  1029. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  1030. switch (mode) {
  1031. case GENET_POWER_PASSIVE:
  1032. reg &= ~(EXT_PWR_DOWN_DLL | EXT_PWR_DOWN_BIAS);
  1033. if (GENET_IS_V5(priv)) {
  1034. reg &= ~(EXT_PWR_DOWN_PHY_EN |
  1035. EXT_PWR_DOWN_PHY_RD |
  1036. EXT_PWR_DOWN_PHY_SD |
  1037. EXT_PWR_DOWN_PHY_RX |
  1038. EXT_PWR_DOWN_PHY_TX |
  1039. EXT_IDDQ_GLBL_PWR);
  1040. reg |= EXT_PHY_RESET;
  1041. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  1042. mdelay(1);
  1043. reg &= ~EXT_PHY_RESET;
  1044. } else {
  1045. reg &= ~EXT_PWR_DOWN_PHY;
  1046. reg |= EXT_PWR_DN_EN_LD;
  1047. }
  1048. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  1049. bcmgenet_phy_power_set(priv->dev, true);
  1050. break;
  1051. case GENET_POWER_CABLE_SENSE:
  1052. /* enable APD */
  1053. if (!GENET_IS_V5(priv)) {
  1054. reg |= EXT_PWR_DN_EN_LD;
  1055. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  1056. }
  1057. break;
  1058. case GENET_POWER_WOL_MAGIC:
  1059. bcmgenet_wol_power_up_cfg(priv, mode);
  1060. return;
  1061. default:
  1062. break;
  1063. }
  1064. }
  1065. /* ioctl handle special commands that are not present in ethtool. */
  1066. static int bcmgenet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1067. {
  1068. if (!netif_running(dev))
  1069. return -EINVAL;
  1070. if (!dev->phydev)
  1071. return -ENODEV;
  1072. return phy_mii_ioctl(dev->phydev, rq, cmd);
  1073. }
  1074. static struct enet_cb *bcmgenet_get_txcb(struct bcmgenet_priv *priv,
  1075. struct bcmgenet_tx_ring *ring)
  1076. {
  1077. struct enet_cb *tx_cb_ptr;
  1078. tx_cb_ptr = ring->cbs;
  1079. tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
  1080. /* Advancing local write pointer */
  1081. if (ring->write_ptr == ring->end_ptr)
  1082. ring->write_ptr = ring->cb_ptr;
  1083. else
  1084. ring->write_ptr++;
  1085. return tx_cb_ptr;
  1086. }
  1087. static struct enet_cb *bcmgenet_put_txcb(struct bcmgenet_priv *priv,
  1088. struct bcmgenet_tx_ring *ring)
  1089. {
  1090. struct enet_cb *tx_cb_ptr;
  1091. tx_cb_ptr = ring->cbs;
  1092. tx_cb_ptr += ring->write_ptr - ring->cb_ptr;
  1093. /* Rewinding local write pointer */
  1094. if (ring->write_ptr == ring->cb_ptr)
  1095. ring->write_ptr = ring->end_ptr;
  1096. else
  1097. ring->write_ptr--;
  1098. return tx_cb_ptr;
  1099. }
  1100. static inline void bcmgenet_rx_ring16_int_disable(struct bcmgenet_rx_ring *ring)
  1101. {
  1102. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
  1103. INTRL2_CPU_MASK_SET);
  1104. }
  1105. static inline void bcmgenet_rx_ring16_int_enable(struct bcmgenet_rx_ring *ring)
  1106. {
  1107. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_RXDMA_DONE,
  1108. INTRL2_CPU_MASK_CLEAR);
  1109. }
  1110. static inline void bcmgenet_rx_ring_int_disable(struct bcmgenet_rx_ring *ring)
  1111. {
  1112. bcmgenet_intrl2_1_writel(ring->priv,
  1113. 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
  1114. INTRL2_CPU_MASK_SET);
  1115. }
  1116. static inline void bcmgenet_rx_ring_int_enable(struct bcmgenet_rx_ring *ring)
  1117. {
  1118. bcmgenet_intrl2_1_writel(ring->priv,
  1119. 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index),
  1120. INTRL2_CPU_MASK_CLEAR);
  1121. }
  1122. static inline void bcmgenet_tx_ring16_int_disable(struct bcmgenet_tx_ring *ring)
  1123. {
  1124. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
  1125. INTRL2_CPU_MASK_SET);
  1126. }
  1127. static inline void bcmgenet_tx_ring16_int_enable(struct bcmgenet_tx_ring *ring)
  1128. {
  1129. bcmgenet_intrl2_0_writel(ring->priv, UMAC_IRQ_TXDMA_DONE,
  1130. INTRL2_CPU_MASK_CLEAR);
  1131. }
  1132. static inline void bcmgenet_tx_ring_int_enable(struct bcmgenet_tx_ring *ring)
  1133. {
  1134. bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
  1135. INTRL2_CPU_MASK_CLEAR);
  1136. }
  1137. static inline void bcmgenet_tx_ring_int_disable(struct bcmgenet_tx_ring *ring)
  1138. {
  1139. bcmgenet_intrl2_1_writel(ring->priv, 1 << ring->index,
  1140. INTRL2_CPU_MASK_SET);
  1141. }
  1142. /* Simple helper to free a transmit control block's resources
  1143. * Returns an skb when the last transmit control block associated with the
  1144. * skb is freed. The skb should be freed by the caller if necessary.
  1145. */
  1146. static struct sk_buff *bcmgenet_free_tx_cb(struct device *dev,
  1147. struct enet_cb *cb)
  1148. {
  1149. struct sk_buff *skb;
  1150. skb = cb->skb;
  1151. if (skb) {
  1152. cb->skb = NULL;
  1153. if (cb == GENET_CB(skb)->first_cb)
  1154. dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
  1155. dma_unmap_len(cb, dma_len),
  1156. DMA_TO_DEVICE);
  1157. else
  1158. dma_unmap_page(dev, dma_unmap_addr(cb, dma_addr),
  1159. dma_unmap_len(cb, dma_len),
  1160. DMA_TO_DEVICE);
  1161. dma_unmap_addr_set(cb, dma_addr, 0);
  1162. if (cb == GENET_CB(skb)->last_cb)
  1163. return skb;
  1164. } else if (dma_unmap_addr(cb, dma_addr)) {
  1165. dma_unmap_page(dev,
  1166. dma_unmap_addr(cb, dma_addr),
  1167. dma_unmap_len(cb, dma_len),
  1168. DMA_TO_DEVICE);
  1169. dma_unmap_addr_set(cb, dma_addr, 0);
  1170. }
  1171. return NULL;
  1172. }
  1173. /* Simple helper to free a receive control block's resources */
  1174. static struct sk_buff *bcmgenet_free_rx_cb(struct device *dev,
  1175. struct enet_cb *cb)
  1176. {
  1177. struct sk_buff *skb;
  1178. skb = cb->skb;
  1179. cb->skb = NULL;
  1180. if (dma_unmap_addr(cb, dma_addr)) {
  1181. dma_unmap_single(dev, dma_unmap_addr(cb, dma_addr),
  1182. dma_unmap_len(cb, dma_len), DMA_FROM_DEVICE);
  1183. dma_unmap_addr_set(cb, dma_addr, 0);
  1184. }
  1185. return skb;
  1186. }
  1187. /* Unlocked version of the reclaim routine */
  1188. static unsigned int __bcmgenet_tx_reclaim(struct net_device *dev,
  1189. struct bcmgenet_tx_ring *ring)
  1190. {
  1191. struct bcmgenet_priv *priv = netdev_priv(dev);
  1192. unsigned int txbds_processed = 0;
  1193. unsigned int bytes_compl = 0;
  1194. unsigned int pkts_compl = 0;
  1195. unsigned int txbds_ready;
  1196. unsigned int c_index;
  1197. struct sk_buff *skb;
  1198. /* Clear status before servicing to reduce spurious interrupts */
  1199. if (ring->index == DESC_INDEX)
  1200. bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_TXDMA_DONE,
  1201. INTRL2_CPU_CLEAR);
  1202. else
  1203. bcmgenet_intrl2_1_writel(priv, (1 << ring->index),
  1204. INTRL2_CPU_CLEAR);
  1205. /* Compute how many buffers are transmitted since last xmit call */
  1206. c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX)
  1207. & DMA_C_INDEX_MASK;
  1208. txbds_ready = (c_index - ring->c_index) & DMA_C_INDEX_MASK;
  1209. netif_dbg(priv, tx_done, dev,
  1210. "%s ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
  1211. __func__, ring->index, ring->c_index, c_index, txbds_ready);
  1212. /* Reclaim transmitted buffers */
  1213. while (txbds_processed < txbds_ready) {
  1214. skb = bcmgenet_free_tx_cb(&priv->pdev->dev,
  1215. &priv->tx_cbs[ring->clean_ptr]);
  1216. if (skb) {
  1217. pkts_compl++;
  1218. bytes_compl += GENET_CB(skb)->bytes_sent;
  1219. dev_consume_skb_any(skb);
  1220. }
  1221. txbds_processed++;
  1222. if (likely(ring->clean_ptr < ring->end_ptr))
  1223. ring->clean_ptr++;
  1224. else
  1225. ring->clean_ptr = ring->cb_ptr;
  1226. }
  1227. ring->free_bds += txbds_processed;
  1228. ring->c_index = c_index;
  1229. ring->packets += pkts_compl;
  1230. ring->bytes += bytes_compl;
  1231. netdev_tx_completed_queue(netdev_get_tx_queue(dev, ring->queue),
  1232. pkts_compl, bytes_compl);
  1233. return txbds_processed;
  1234. }
  1235. static unsigned int bcmgenet_tx_reclaim(struct net_device *dev,
  1236. struct bcmgenet_tx_ring *ring)
  1237. {
  1238. unsigned int released;
  1239. spin_lock_bh(&ring->lock);
  1240. released = __bcmgenet_tx_reclaim(dev, ring);
  1241. spin_unlock_bh(&ring->lock);
  1242. return released;
  1243. }
  1244. static int bcmgenet_tx_poll(struct napi_struct *napi, int budget)
  1245. {
  1246. struct bcmgenet_tx_ring *ring =
  1247. container_of(napi, struct bcmgenet_tx_ring, napi);
  1248. unsigned int work_done = 0;
  1249. struct netdev_queue *txq;
  1250. spin_lock(&ring->lock);
  1251. work_done = __bcmgenet_tx_reclaim(ring->priv->dev, ring);
  1252. if (ring->free_bds > (MAX_SKB_FRAGS + 1)) {
  1253. txq = netdev_get_tx_queue(ring->priv->dev, ring->queue);
  1254. netif_tx_wake_queue(txq);
  1255. }
  1256. spin_unlock(&ring->lock);
  1257. if (work_done == 0) {
  1258. napi_complete(napi);
  1259. ring->int_enable(ring);
  1260. return 0;
  1261. }
  1262. return budget;
  1263. }
  1264. static void bcmgenet_tx_reclaim_all(struct net_device *dev)
  1265. {
  1266. struct bcmgenet_priv *priv = netdev_priv(dev);
  1267. int i;
  1268. if (netif_is_multiqueue(dev)) {
  1269. for (i = 0; i < priv->hw_params->tx_queues; i++)
  1270. bcmgenet_tx_reclaim(dev, &priv->tx_rings[i]);
  1271. }
  1272. bcmgenet_tx_reclaim(dev, &priv->tx_rings[DESC_INDEX]);
  1273. }
  1274. /* Reallocate the SKB to put enough headroom in front of it and insert
  1275. * the transmit checksum offsets in the descriptors
  1276. */
  1277. static struct sk_buff *bcmgenet_put_tx_csum(struct net_device *dev,
  1278. struct sk_buff *skb)
  1279. {
  1280. struct status_64 *status = NULL;
  1281. struct sk_buff *new_skb;
  1282. u16 offset;
  1283. u8 ip_proto;
  1284. __be16 ip_ver;
  1285. u32 tx_csum_info;
  1286. if (unlikely(skb_headroom(skb) < sizeof(*status))) {
  1287. /* If 64 byte status block enabled, must make sure skb has
  1288. * enough headroom for us to insert 64B status block.
  1289. */
  1290. new_skb = skb_realloc_headroom(skb, sizeof(*status));
  1291. dev_kfree_skb(skb);
  1292. if (!new_skb) {
  1293. dev->stats.tx_dropped++;
  1294. return NULL;
  1295. }
  1296. skb = new_skb;
  1297. }
  1298. skb_push(skb, sizeof(*status));
  1299. status = (struct status_64 *)skb->data;
  1300. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1301. ip_ver = skb->protocol;
  1302. switch (ip_ver) {
  1303. case htons(ETH_P_IP):
  1304. ip_proto = ip_hdr(skb)->protocol;
  1305. break;
  1306. case htons(ETH_P_IPV6):
  1307. ip_proto = ipv6_hdr(skb)->nexthdr;
  1308. break;
  1309. default:
  1310. return skb;
  1311. }
  1312. offset = skb_checksum_start_offset(skb) - sizeof(*status);
  1313. tx_csum_info = (offset << STATUS_TX_CSUM_START_SHIFT) |
  1314. (offset + skb->csum_offset);
  1315. /* Set the length valid bit for TCP and UDP and just set
  1316. * the special UDP flag for IPv4, else just set to 0.
  1317. */
  1318. if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
  1319. tx_csum_info |= STATUS_TX_CSUM_LV;
  1320. if (ip_proto == IPPROTO_UDP &&
  1321. ip_ver == htons(ETH_P_IP))
  1322. tx_csum_info |= STATUS_TX_CSUM_PROTO_UDP;
  1323. } else {
  1324. tx_csum_info = 0;
  1325. }
  1326. status->tx_csum_info = tx_csum_info;
  1327. }
  1328. return skb;
  1329. }
  1330. static netdev_tx_t bcmgenet_xmit(struct sk_buff *skb, struct net_device *dev)
  1331. {
  1332. struct bcmgenet_priv *priv = netdev_priv(dev);
  1333. struct device *kdev = &priv->pdev->dev;
  1334. struct bcmgenet_tx_ring *ring = NULL;
  1335. struct enet_cb *tx_cb_ptr;
  1336. struct netdev_queue *txq;
  1337. int nr_frags, index;
  1338. dma_addr_t mapping;
  1339. unsigned int size;
  1340. skb_frag_t *frag;
  1341. u32 len_stat;
  1342. int ret;
  1343. int i;
  1344. index = skb_get_queue_mapping(skb);
  1345. /* Mapping strategy:
  1346. * queue_mapping = 0, unclassified, packet xmited through ring16
  1347. * queue_mapping = 1, goes to ring 0. (highest priority queue
  1348. * queue_mapping = 2, goes to ring 1.
  1349. * queue_mapping = 3, goes to ring 2.
  1350. * queue_mapping = 4, goes to ring 3.
  1351. */
  1352. if (index == 0)
  1353. index = DESC_INDEX;
  1354. else
  1355. index -= 1;
  1356. ring = &priv->tx_rings[index];
  1357. txq = netdev_get_tx_queue(dev, ring->queue);
  1358. nr_frags = skb_shinfo(skb)->nr_frags;
  1359. spin_lock(&ring->lock);
  1360. if (ring->free_bds <= (nr_frags + 1)) {
  1361. if (!netif_tx_queue_stopped(txq)) {
  1362. netif_tx_stop_queue(txq);
  1363. netdev_err(dev,
  1364. "%s: tx ring %d full when queue %d awake\n",
  1365. __func__, index, ring->queue);
  1366. }
  1367. ret = NETDEV_TX_BUSY;
  1368. goto out;
  1369. }
  1370. if (skb_padto(skb, ETH_ZLEN)) {
  1371. ret = NETDEV_TX_OK;
  1372. goto out;
  1373. }
  1374. /* Retain how many bytes will be sent on the wire, without TSB inserted
  1375. * by transmit checksum offload
  1376. */
  1377. GENET_CB(skb)->bytes_sent = skb->len;
  1378. /* set the SKB transmit checksum */
  1379. if (priv->desc_64b_en) {
  1380. skb = bcmgenet_put_tx_csum(dev, skb);
  1381. if (!skb) {
  1382. ret = NETDEV_TX_OK;
  1383. goto out;
  1384. }
  1385. }
  1386. for (i = 0; i <= nr_frags; i++) {
  1387. tx_cb_ptr = bcmgenet_get_txcb(priv, ring);
  1388. BUG_ON(!tx_cb_ptr);
  1389. if (!i) {
  1390. /* Transmit single SKB or head of fragment list */
  1391. GENET_CB(skb)->first_cb = tx_cb_ptr;
  1392. size = skb_headlen(skb);
  1393. mapping = dma_map_single(kdev, skb->data, size,
  1394. DMA_TO_DEVICE);
  1395. } else {
  1396. /* xmit fragment */
  1397. frag = &skb_shinfo(skb)->frags[i - 1];
  1398. size = skb_frag_size(frag);
  1399. mapping = skb_frag_dma_map(kdev, frag, 0, size,
  1400. DMA_TO_DEVICE);
  1401. }
  1402. ret = dma_mapping_error(kdev, mapping);
  1403. if (ret) {
  1404. priv->mib.tx_dma_failed++;
  1405. netif_err(priv, tx_err, dev, "Tx DMA map failed\n");
  1406. ret = NETDEV_TX_OK;
  1407. goto out_unmap_frags;
  1408. }
  1409. dma_unmap_addr_set(tx_cb_ptr, dma_addr, mapping);
  1410. dma_unmap_len_set(tx_cb_ptr, dma_len, size);
  1411. tx_cb_ptr->skb = skb;
  1412. len_stat = (size << DMA_BUFLENGTH_SHIFT) |
  1413. (priv->hw_params->qtag_mask << DMA_TX_QTAG_SHIFT);
  1414. if (!i) {
  1415. len_stat |= DMA_TX_APPEND_CRC | DMA_SOP;
  1416. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1417. len_stat |= DMA_TX_DO_CSUM;
  1418. }
  1419. if (i == nr_frags)
  1420. len_stat |= DMA_EOP;
  1421. dmadesc_set(priv, tx_cb_ptr->bd_addr, mapping, len_stat);
  1422. }
  1423. GENET_CB(skb)->last_cb = tx_cb_ptr;
  1424. skb_tx_timestamp(skb);
  1425. /* Decrement total BD count and advance our write pointer */
  1426. ring->free_bds -= nr_frags + 1;
  1427. ring->prod_index += nr_frags + 1;
  1428. ring->prod_index &= DMA_P_INDEX_MASK;
  1429. netdev_tx_sent_queue(txq, GENET_CB(skb)->bytes_sent);
  1430. if (ring->free_bds <= (MAX_SKB_FRAGS + 1))
  1431. netif_tx_stop_queue(txq);
  1432. if (!skb->xmit_more || netif_xmit_stopped(txq))
  1433. /* Packets are ready, update producer index */
  1434. bcmgenet_tdma_ring_writel(priv, ring->index,
  1435. ring->prod_index, TDMA_PROD_INDEX);
  1436. out:
  1437. spin_unlock(&ring->lock);
  1438. return ret;
  1439. out_unmap_frags:
  1440. /* Back up for failed control block mapping */
  1441. bcmgenet_put_txcb(priv, ring);
  1442. /* Unmap successfully mapped control blocks */
  1443. while (i-- > 0) {
  1444. tx_cb_ptr = bcmgenet_put_txcb(priv, ring);
  1445. bcmgenet_free_tx_cb(kdev, tx_cb_ptr);
  1446. }
  1447. dev_kfree_skb(skb);
  1448. goto out;
  1449. }
  1450. static struct sk_buff *bcmgenet_rx_refill(struct bcmgenet_priv *priv,
  1451. struct enet_cb *cb)
  1452. {
  1453. struct device *kdev = &priv->pdev->dev;
  1454. struct sk_buff *skb;
  1455. struct sk_buff *rx_skb;
  1456. dma_addr_t mapping;
  1457. /* Allocate a new Rx skb */
  1458. skb = netdev_alloc_skb(priv->dev, priv->rx_buf_len + SKB_ALIGNMENT);
  1459. if (!skb) {
  1460. priv->mib.alloc_rx_buff_failed++;
  1461. netif_err(priv, rx_err, priv->dev,
  1462. "%s: Rx skb allocation failed\n", __func__);
  1463. return NULL;
  1464. }
  1465. /* DMA-map the new Rx skb */
  1466. mapping = dma_map_single(kdev, skb->data, priv->rx_buf_len,
  1467. DMA_FROM_DEVICE);
  1468. if (dma_mapping_error(kdev, mapping)) {
  1469. priv->mib.rx_dma_failed++;
  1470. dev_kfree_skb_any(skb);
  1471. netif_err(priv, rx_err, priv->dev,
  1472. "%s: Rx skb DMA mapping failed\n", __func__);
  1473. return NULL;
  1474. }
  1475. /* Grab the current Rx skb from the ring and DMA-unmap it */
  1476. rx_skb = bcmgenet_free_rx_cb(kdev, cb);
  1477. /* Put the new Rx skb on the ring */
  1478. cb->skb = skb;
  1479. dma_unmap_addr_set(cb, dma_addr, mapping);
  1480. dma_unmap_len_set(cb, dma_len, priv->rx_buf_len);
  1481. dmadesc_set_addr(priv, cb->bd_addr, mapping);
  1482. /* Return the current Rx skb to caller */
  1483. return rx_skb;
  1484. }
  1485. /* bcmgenet_desc_rx - descriptor based rx process.
  1486. * this could be called from bottom half, or from NAPI polling method.
  1487. */
  1488. static unsigned int bcmgenet_desc_rx(struct bcmgenet_rx_ring *ring,
  1489. unsigned int budget)
  1490. {
  1491. struct bcmgenet_priv *priv = ring->priv;
  1492. struct net_device *dev = priv->dev;
  1493. struct enet_cb *cb;
  1494. struct sk_buff *skb;
  1495. u32 dma_length_status;
  1496. unsigned long dma_flag;
  1497. int len;
  1498. unsigned int rxpktprocessed = 0, rxpkttoprocess;
  1499. unsigned int bytes_processed = 0;
  1500. unsigned int p_index, mask;
  1501. unsigned int discards;
  1502. unsigned int chksum_ok = 0;
  1503. /* Clear status before servicing to reduce spurious interrupts */
  1504. if (ring->index == DESC_INDEX) {
  1505. bcmgenet_intrl2_0_writel(priv, UMAC_IRQ_RXDMA_DONE,
  1506. INTRL2_CPU_CLEAR);
  1507. } else {
  1508. mask = 1 << (UMAC_IRQ1_RX_INTR_SHIFT + ring->index);
  1509. bcmgenet_intrl2_1_writel(priv,
  1510. mask,
  1511. INTRL2_CPU_CLEAR);
  1512. }
  1513. p_index = bcmgenet_rdma_ring_readl(priv, ring->index, RDMA_PROD_INDEX);
  1514. discards = (p_index >> DMA_P_INDEX_DISCARD_CNT_SHIFT) &
  1515. DMA_P_INDEX_DISCARD_CNT_MASK;
  1516. if (discards > ring->old_discards) {
  1517. discards = discards - ring->old_discards;
  1518. ring->errors += discards;
  1519. ring->old_discards += discards;
  1520. /* Clear HW register when we reach 75% of maximum 0xFFFF */
  1521. if (ring->old_discards >= 0xC000) {
  1522. ring->old_discards = 0;
  1523. bcmgenet_rdma_ring_writel(priv, ring->index, 0,
  1524. RDMA_PROD_INDEX);
  1525. }
  1526. }
  1527. p_index &= DMA_P_INDEX_MASK;
  1528. rxpkttoprocess = (p_index - ring->c_index) & DMA_C_INDEX_MASK;
  1529. netif_dbg(priv, rx_status, dev,
  1530. "RDMA: rxpkttoprocess=%d\n", rxpkttoprocess);
  1531. while ((rxpktprocessed < rxpkttoprocess) &&
  1532. (rxpktprocessed < budget)) {
  1533. cb = &priv->rx_cbs[ring->read_ptr];
  1534. skb = bcmgenet_rx_refill(priv, cb);
  1535. if (unlikely(!skb)) {
  1536. ring->dropped++;
  1537. goto next;
  1538. }
  1539. if (!priv->desc_64b_en) {
  1540. dma_length_status =
  1541. dmadesc_get_length_status(priv, cb->bd_addr);
  1542. } else {
  1543. struct status_64 *status;
  1544. status = (struct status_64 *)skb->data;
  1545. dma_length_status = status->length_status;
  1546. }
  1547. /* DMA flags and length are still valid no matter how
  1548. * we got the Receive Status Vector (64B RSB or register)
  1549. */
  1550. dma_flag = dma_length_status & 0xffff;
  1551. len = dma_length_status >> DMA_BUFLENGTH_SHIFT;
  1552. netif_dbg(priv, rx_status, dev,
  1553. "%s:p_ind=%d c_ind=%d read_ptr=%d len_stat=0x%08x\n",
  1554. __func__, p_index, ring->c_index,
  1555. ring->read_ptr, dma_length_status);
  1556. if (unlikely(!(dma_flag & DMA_EOP) || !(dma_flag & DMA_SOP))) {
  1557. netif_err(priv, rx_status, dev,
  1558. "dropping fragmented packet!\n");
  1559. ring->errors++;
  1560. dev_kfree_skb_any(skb);
  1561. goto next;
  1562. }
  1563. /* report errors */
  1564. if (unlikely(dma_flag & (DMA_RX_CRC_ERROR |
  1565. DMA_RX_OV |
  1566. DMA_RX_NO |
  1567. DMA_RX_LG |
  1568. DMA_RX_RXER))) {
  1569. netif_err(priv, rx_status, dev, "dma_flag=0x%x\n",
  1570. (unsigned int)dma_flag);
  1571. if (dma_flag & DMA_RX_CRC_ERROR)
  1572. dev->stats.rx_crc_errors++;
  1573. if (dma_flag & DMA_RX_OV)
  1574. dev->stats.rx_over_errors++;
  1575. if (dma_flag & DMA_RX_NO)
  1576. dev->stats.rx_frame_errors++;
  1577. if (dma_flag & DMA_RX_LG)
  1578. dev->stats.rx_length_errors++;
  1579. dev->stats.rx_errors++;
  1580. dev_kfree_skb_any(skb);
  1581. goto next;
  1582. } /* error packet */
  1583. chksum_ok = (dma_flag & priv->dma_rx_chk_bit) &&
  1584. priv->desc_rxchk_en;
  1585. skb_put(skb, len);
  1586. if (priv->desc_64b_en) {
  1587. skb_pull(skb, 64);
  1588. len -= 64;
  1589. }
  1590. if (likely(chksum_ok))
  1591. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1592. /* remove hardware 2bytes added for IP alignment */
  1593. skb_pull(skb, 2);
  1594. len -= 2;
  1595. if (priv->crc_fwd_en) {
  1596. skb_trim(skb, len - ETH_FCS_LEN);
  1597. len -= ETH_FCS_LEN;
  1598. }
  1599. bytes_processed += len;
  1600. /*Finish setting up the received SKB and send it to the kernel*/
  1601. skb->protocol = eth_type_trans(skb, priv->dev);
  1602. ring->packets++;
  1603. ring->bytes += len;
  1604. if (dma_flag & DMA_RX_MULT)
  1605. dev->stats.multicast++;
  1606. /* Notify kernel */
  1607. napi_gro_receive(&ring->napi, skb);
  1608. netif_dbg(priv, rx_status, dev, "pushed up to kernel\n");
  1609. next:
  1610. rxpktprocessed++;
  1611. if (likely(ring->read_ptr < ring->end_ptr))
  1612. ring->read_ptr++;
  1613. else
  1614. ring->read_ptr = ring->cb_ptr;
  1615. ring->c_index = (ring->c_index + 1) & DMA_C_INDEX_MASK;
  1616. bcmgenet_rdma_ring_writel(priv, ring->index, ring->c_index, RDMA_CONS_INDEX);
  1617. }
  1618. ring->dim.bytes = bytes_processed;
  1619. ring->dim.packets = rxpktprocessed;
  1620. return rxpktprocessed;
  1621. }
  1622. /* Rx NAPI polling method */
  1623. static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
  1624. {
  1625. struct bcmgenet_rx_ring *ring = container_of(napi,
  1626. struct bcmgenet_rx_ring, napi);
  1627. struct net_dim_sample dim_sample;
  1628. unsigned int work_done;
  1629. work_done = bcmgenet_desc_rx(ring, budget);
  1630. if (work_done < budget) {
  1631. napi_complete_done(napi, work_done);
  1632. ring->int_enable(ring);
  1633. }
  1634. if (ring->dim.use_dim) {
  1635. net_dim_sample(ring->dim.event_ctr, ring->dim.packets,
  1636. ring->dim.bytes, &dim_sample);
  1637. net_dim(&ring->dim.dim, dim_sample);
  1638. }
  1639. return work_done;
  1640. }
  1641. static void bcmgenet_dim_work(struct work_struct *work)
  1642. {
  1643. struct net_dim *dim = container_of(work, struct net_dim, work);
  1644. struct bcmgenet_net_dim *ndim =
  1645. container_of(dim, struct bcmgenet_net_dim, dim);
  1646. struct bcmgenet_rx_ring *ring =
  1647. container_of(ndim, struct bcmgenet_rx_ring, dim);
  1648. struct net_dim_cq_moder cur_profile =
  1649. net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
  1650. bcmgenet_set_rx_coalesce(ring, cur_profile.usec, cur_profile.pkts);
  1651. dim->state = NET_DIM_START_MEASURE;
  1652. }
  1653. /* Assign skb to RX DMA descriptor. */
  1654. static int bcmgenet_alloc_rx_buffers(struct bcmgenet_priv *priv,
  1655. struct bcmgenet_rx_ring *ring)
  1656. {
  1657. struct enet_cb *cb;
  1658. struct sk_buff *skb;
  1659. int i;
  1660. netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
  1661. /* loop here for each buffer needing assign */
  1662. for (i = 0; i < ring->size; i++) {
  1663. cb = ring->cbs + i;
  1664. skb = bcmgenet_rx_refill(priv, cb);
  1665. if (skb)
  1666. dev_consume_skb_any(skb);
  1667. if (!cb->skb)
  1668. return -ENOMEM;
  1669. }
  1670. return 0;
  1671. }
  1672. static void bcmgenet_free_rx_buffers(struct bcmgenet_priv *priv)
  1673. {
  1674. struct sk_buff *skb;
  1675. struct enet_cb *cb;
  1676. int i;
  1677. for (i = 0; i < priv->num_rx_bds; i++) {
  1678. cb = &priv->rx_cbs[i];
  1679. skb = bcmgenet_free_rx_cb(&priv->pdev->dev, cb);
  1680. if (skb)
  1681. dev_consume_skb_any(skb);
  1682. }
  1683. }
  1684. static void umac_enable_set(struct bcmgenet_priv *priv, u32 mask, bool enable)
  1685. {
  1686. u32 reg;
  1687. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  1688. if (enable)
  1689. reg |= mask;
  1690. else
  1691. reg &= ~mask;
  1692. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  1693. /* UniMAC stops on a packet boundary, wait for a full-size packet
  1694. * to be processed
  1695. */
  1696. if (enable == 0)
  1697. usleep_range(1000, 2000);
  1698. }
  1699. static void reset_umac(struct bcmgenet_priv *priv)
  1700. {
  1701. /* 7358a0/7552a0: bad default in RBUF_FLUSH_CTRL.umac_sw_rst */
  1702. bcmgenet_rbuf_ctrl_set(priv, 0);
  1703. udelay(10);
  1704. /* disable MAC while updating its registers */
  1705. bcmgenet_umac_writel(priv, 0, UMAC_CMD);
  1706. /* issue soft reset with (rg)mii loopback to ensure a stable rxclk */
  1707. bcmgenet_umac_writel(priv, CMD_SW_RESET | CMD_LCL_LOOP_EN, UMAC_CMD);
  1708. }
  1709. static void bcmgenet_intr_disable(struct bcmgenet_priv *priv)
  1710. {
  1711. /* Mask all interrupts.*/
  1712. bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
  1713. bcmgenet_intrl2_0_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
  1714. bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_MASK_SET);
  1715. bcmgenet_intrl2_1_writel(priv, 0xFFFFFFFF, INTRL2_CPU_CLEAR);
  1716. }
  1717. static void bcmgenet_link_intr_enable(struct bcmgenet_priv *priv)
  1718. {
  1719. u32 int0_enable = 0;
  1720. /* Monitor cable plug/unplugged event for internal PHY, external PHY
  1721. * and MoCA PHY
  1722. */
  1723. if (priv->internal_phy) {
  1724. int0_enable |= UMAC_IRQ_LINK_EVENT;
  1725. if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
  1726. int0_enable |= UMAC_IRQ_PHY_DET_R;
  1727. } else if (priv->ext_phy) {
  1728. int0_enable |= UMAC_IRQ_LINK_EVENT;
  1729. } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
  1730. if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
  1731. int0_enable |= UMAC_IRQ_LINK_EVENT;
  1732. }
  1733. bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
  1734. }
  1735. static void init_umac(struct bcmgenet_priv *priv)
  1736. {
  1737. struct device *kdev = &priv->pdev->dev;
  1738. u32 reg;
  1739. u32 int0_enable = 0;
  1740. dev_dbg(&priv->pdev->dev, "bcmgenet: init_umac\n");
  1741. reset_umac(priv);
  1742. /* clear tx/rx counter */
  1743. bcmgenet_umac_writel(priv,
  1744. MIB_RESET_RX | MIB_RESET_TX | MIB_RESET_RUNT,
  1745. UMAC_MIB_CTRL);
  1746. bcmgenet_umac_writel(priv, 0, UMAC_MIB_CTRL);
  1747. bcmgenet_umac_writel(priv, ENET_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  1748. /* init rx registers, enable ip header optimization */
  1749. reg = bcmgenet_rbuf_readl(priv, RBUF_CTRL);
  1750. reg |= RBUF_ALIGN_2B;
  1751. bcmgenet_rbuf_writel(priv, reg, RBUF_CTRL);
  1752. if (!GENET_IS_V1(priv) && !GENET_IS_V2(priv))
  1753. bcmgenet_rbuf_writel(priv, 1, RBUF_TBUF_SIZE_CTRL);
  1754. bcmgenet_intr_disable(priv);
  1755. /* Configure backpressure vectors for MoCA */
  1756. if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
  1757. reg = bcmgenet_bp_mc_get(priv);
  1758. reg |= BIT(priv->hw_params->bp_in_en_shift);
  1759. /* bp_mask: back pressure mask */
  1760. if (netif_is_multiqueue(priv->dev))
  1761. reg |= priv->hw_params->bp_in_mask;
  1762. else
  1763. reg &= ~priv->hw_params->bp_in_mask;
  1764. bcmgenet_bp_mc_set(priv, reg);
  1765. }
  1766. /* Enable MDIO interrupts on GENET v3+ */
  1767. if (priv->hw_params->flags & GENET_HAS_MDIO_INTR)
  1768. int0_enable |= (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR);
  1769. bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
  1770. dev_dbg(kdev, "done init umac\n");
  1771. }
  1772. static void bcmgenet_init_dim(struct bcmgenet_rx_ring *ring,
  1773. void (*cb)(struct work_struct *work))
  1774. {
  1775. struct bcmgenet_net_dim *dim = &ring->dim;
  1776. INIT_WORK(&dim->dim.work, cb);
  1777. dim->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  1778. dim->event_ctr = 0;
  1779. dim->packets = 0;
  1780. dim->bytes = 0;
  1781. }
  1782. static void bcmgenet_init_rx_coalesce(struct bcmgenet_rx_ring *ring)
  1783. {
  1784. struct bcmgenet_net_dim *dim = &ring->dim;
  1785. struct net_dim_cq_moder moder;
  1786. u32 usecs, pkts;
  1787. usecs = ring->rx_coalesce_usecs;
  1788. pkts = ring->rx_max_coalesced_frames;
  1789. /* If DIM was enabled, re-apply default parameters */
  1790. if (dim->use_dim) {
  1791. moder = net_dim_get_def_rx_moderation(dim->dim.mode);
  1792. usecs = moder.usec;
  1793. pkts = moder.pkts;
  1794. }
  1795. bcmgenet_set_rx_coalesce(ring, usecs, pkts);
  1796. }
  1797. /* Initialize a Tx ring along with corresponding hardware registers */
  1798. static void bcmgenet_init_tx_ring(struct bcmgenet_priv *priv,
  1799. unsigned int index, unsigned int size,
  1800. unsigned int start_ptr, unsigned int end_ptr)
  1801. {
  1802. struct bcmgenet_tx_ring *ring = &priv->tx_rings[index];
  1803. u32 words_per_bd = WORDS_PER_BD(priv);
  1804. u32 flow_period_val = 0;
  1805. spin_lock_init(&ring->lock);
  1806. ring->priv = priv;
  1807. ring->index = index;
  1808. if (index == DESC_INDEX) {
  1809. ring->queue = 0;
  1810. ring->int_enable = bcmgenet_tx_ring16_int_enable;
  1811. ring->int_disable = bcmgenet_tx_ring16_int_disable;
  1812. } else {
  1813. ring->queue = index + 1;
  1814. ring->int_enable = bcmgenet_tx_ring_int_enable;
  1815. ring->int_disable = bcmgenet_tx_ring_int_disable;
  1816. }
  1817. ring->cbs = priv->tx_cbs + start_ptr;
  1818. ring->size = size;
  1819. ring->clean_ptr = start_ptr;
  1820. ring->c_index = 0;
  1821. ring->free_bds = size;
  1822. ring->write_ptr = start_ptr;
  1823. ring->cb_ptr = start_ptr;
  1824. ring->end_ptr = end_ptr - 1;
  1825. ring->prod_index = 0;
  1826. /* Set flow period for ring != 16 */
  1827. if (index != DESC_INDEX)
  1828. flow_period_val = ENET_MAX_MTU_SIZE << 16;
  1829. bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_PROD_INDEX);
  1830. bcmgenet_tdma_ring_writel(priv, index, 0, TDMA_CONS_INDEX);
  1831. bcmgenet_tdma_ring_writel(priv, index, 1, DMA_MBUF_DONE_THRESH);
  1832. /* Disable rate control for now */
  1833. bcmgenet_tdma_ring_writel(priv, index, flow_period_val,
  1834. TDMA_FLOW_PERIOD);
  1835. bcmgenet_tdma_ring_writel(priv, index,
  1836. ((size << DMA_RING_SIZE_SHIFT) |
  1837. RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
  1838. /* Set start and end address, read and write pointers */
  1839. bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1840. DMA_START_ADDR);
  1841. bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1842. TDMA_READ_PTR);
  1843. bcmgenet_tdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1844. TDMA_WRITE_PTR);
  1845. bcmgenet_tdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
  1846. DMA_END_ADDR);
  1847. /* Initialize Tx NAPI */
  1848. netif_tx_napi_add(priv->dev, &ring->napi, bcmgenet_tx_poll,
  1849. NAPI_POLL_WEIGHT);
  1850. }
  1851. /* Initialize a RDMA ring */
  1852. static int bcmgenet_init_rx_ring(struct bcmgenet_priv *priv,
  1853. unsigned int index, unsigned int size,
  1854. unsigned int start_ptr, unsigned int end_ptr)
  1855. {
  1856. struct bcmgenet_rx_ring *ring = &priv->rx_rings[index];
  1857. u32 words_per_bd = WORDS_PER_BD(priv);
  1858. int ret;
  1859. ring->priv = priv;
  1860. ring->index = index;
  1861. if (index == DESC_INDEX) {
  1862. ring->int_enable = bcmgenet_rx_ring16_int_enable;
  1863. ring->int_disable = bcmgenet_rx_ring16_int_disable;
  1864. } else {
  1865. ring->int_enable = bcmgenet_rx_ring_int_enable;
  1866. ring->int_disable = bcmgenet_rx_ring_int_disable;
  1867. }
  1868. ring->cbs = priv->rx_cbs + start_ptr;
  1869. ring->size = size;
  1870. ring->c_index = 0;
  1871. ring->read_ptr = start_ptr;
  1872. ring->cb_ptr = start_ptr;
  1873. ring->end_ptr = end_ptr - 1;
  1874. ret = bcmgenet_alloc_rx_buffers(priv, ring);
  1875. if (ret)
  1876. return ret;
  1877. bcmgenet_init_dim(ring, bcmgenet_dim_work);
  1878. bcmgenet_init_rx_coalesce(ring);
  1879. /* Initialize Rx NAPI */
  1880. netif_napi_add(priv->dev, &ring->napi, bcmgenet_rx_poll,
  1881. NAPI_POLL_WEIGHT);
  1882. bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_PROD_INDEX);
  1883. bcmgenet_rdma_ring_writel(priv, index, 0, RDMA_CONS_INDEX);
  1884. bcmgenet_rdma_ring_writel(priv, index,
  1885. ((size << DMA_RING_SIZE_SHIFT) |
  1886. RX_BUF_LENGTH), DMA_RING_BUF_SIZE);
  1887. bcmgenet_rdma_ring_writel(priv, index,
  1888. (DMA_FC_THRESH_LO <<
  1889. DMA_XOFF_THRESHOLD_SHIFT) |
  1890. DMA_FC_THRESH_HI, RDMA_XON_XOFF_THRESH);
  1891. /* Set start and end address, read and write pointers */
  1892. bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1893. DMA_START_ADDR);
  1894. bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1895. RDMA_READ_PTR);
  1896. bcmgenet_rdma_ring_writel(priv, index, start_ptr * words_per_bd,
  1897. RDMA_WRITE_PTR);
  1898. bcmgenet_rdma_ring_writel(priv, index, end_ptr * words_per_bd - 1,
  1899. DMA_END_ADDR);
  1900. return ret;
  1901. }
  1902. static void bcmgenet_enable_tx_napi(struct bcmgenet_priv *priv)
  1903. {
  1904. unsigned int i;
  1905. struct bcmgenet_tx_ring *ring;
  1906. for (i = 0; i < priv->hw_params->tx_queues; ++i) {
  1907. ring = &priv->tx_rings[i];
  1908. napi_enable(&ring->napi);
  1909. ring->int_enable(ring);
  1910. }
  1911. ring = &priv->tx_rings[DESC_INDEX];
  1912. napi_enable(&ring->napi);
  1913. ring->int_enable(ring);
  1914. }
  1915. static void bcmgenet_disable_tx_napi(struct bcmgenet_priv *priv)
  1916. {
  1917. unsigned int i;
  1918. struct bcmgenet_tx_ring *ring;
  1919. for (i = 0; i < priv->hw_params->tx_queues; ++i) {
  1920. ring = &priv->tx_rings[i];
  1921. napi_disable(&ring->napi);
  1922. }
  1923. ring = &priv->tx_rings[DESC_INDEX];
  1924. napi_disable(&ring->napi);
  1925. }
  1926. static void bcmgenet_fini_tx_napi(struct bcmgenet_priv *priv)
  1927. {
  1928. unsigned int i;
  1929. struct bcmgenet_tx_ring *ring;
  1930. for (i = 0; i < priv->hw_params->tx_queues; ++i) {
  1931. ring = &priv->tx_rings[i];
  1932. netif_napi_del(&ring->napi);
  1933. }
  1934. ring = &priv->tx_rings[DESC_INDEX];
  1935. netif_napi_del(&ring->napi);
  1936. }
  1937. /* Initialize Tx queues
  1938. *
  1939. * Queues 0-3 are priority-based, each one has 32 descriptors,
  1940. * with queue 0 being the highest priority queue.
  1941. *
  1942. * Queue 16 is the default Tx queue with
  1943. * GENET_Q16_TX_BD_CNT = 256 - 4 * 32 = 128 descriptors.
  1944. *
  1945. * The transmit control block pool is then partitioned as follows:
  1946. * - Tx queue 0 uses tx_cbs[0..31]
  1947. * - Tx queue 1 uses tx_cbs[32..63]
  1948. * - Tx queue 2 uses tx_cbs[64..95]
  1949. * - Tx queue 3 uses tx_cbs[96..127]
  1950. * - Tx queue 16 uses tx_cbs[128..255]
  1951. */
  1952. static void bcmgenet_init_tx_queues(struct net_device *dev)
  1953. {
  1954. struct bcmgenet_priv *priv = netdev_priv(dev);
  1955. u32 i, dma_enable;
  1956. u32 dma_ctrl, ring_cfg;
  1957. u32 dma_priority[3] = {0, 0, 0};
  1958. dma_ctrl = bcmgenet_tdma_readl(priv, DMA_CTRL);
  1959. dma_enable = dma_ctrl & DMA_EN;
  1960. dma_ctrl &= ~DMA_EN;
  1961. bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
  1962. dma_ctrl = 0;
  1963. ring_cfg = 0;
  1964. /* Enable strict priority arbiter mode */
  1965. bcmgenet_tdma_writel(priv, DMA_ARBITER_SP, DMA_ARB_CTRL);
  1966. /* Initialize Tx priority queues */
  1967. for (i = 0; i < priv->hw_params->tx_queues; i++) {
  1968. bcmgenet_init_tx_ring(priv, i, priv->hw_params->tx_bds_per_q,
  1969. i * priv->hw_params->tx_bds_per_q,
  1970. (i + 1) * priv->hw_params->tx_bds_per_q);
  1971. ring_cfg |= (1 << i);
  1972. dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
  1973. dma_priority[DMA_PRIO_REG_INDEX(i)] |=
  1974. ((GENET_Q0_PRIORITY + i) << DMA_PRIO_REG_SHIFT(i));
  1975. }
  1976. /* Initialize Tx default queue 16 */
  1977. bcmgenet_init_tx_ring(priv, DESC_INDEX, GENET_Q16_TX_BD_CNT,
  1978. priv->hw_params->tx_queues *
  1979. priv->hw_params->tx_bds_per_q,
  1980. TOTAL_DESC);
  1981. ring_cfg |= (1 << DESC_INDEX);
  1982. dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
  1983. dma_priority[DMA_PRIO_REG_INDEX(DESC_INDEX)] |=
  1984. ((GENET_Q0_PRIORITY + priv->hw_params->tx_queues) <<
  1985. DMA_PRIO_REG_SHIFT(DESC_INDEX));
  1986. /* Set Tx queue priorities */
  1987. bcmgenet_tdma_writel(priv, dma_priority[0], DMA_PRIORITY_0);
  1988. bcmgenet_tdma_writel(priv, dma_priority[1], DMA_PRIORITY_1);
  1989. bcmgenet_tdma_writel(priv, dma_priority[2], DMA_PRIORITY_2);
  1990. /* Enable Tx queues */
  1991. bcmgenet_tdma_writel(priv, ring_cfg, DMA_RING_CFG);
  1992. /* Enable Tx DMA */
  1993. if (dma_enable)
  1994. dma_ctrl |= DMA_EN;
  1995. bcmgenet_tdma_writel(priv, dma_ctrl, DMA_CTRL);
  1996. }
  1997. static void bcmgenet_enable_rx_napi(struct bcmgenet_priv *priv)
  1998. {
  1999. unsigned int i;
  2000. struct bcmgenet_rx_ring *ring;
  2001. for (i = 0; i < priv->hw_params->rx_queues; ++i) {
  2002. ring = &priv->rx_rings[i];
  2003. napi_enable(&ring->napi);
  2004. ring->int_enable(ring);
  2005. }
  2006. ring = &priv->rx_rings[DESC_INDEX];
  2007. napi_enable(&ring->napi);
  2008. ring->int_enable(ring);
  2009. }
  2010. static void bcmgenet_disable_rx_napi(struct bcmgenet_priv *priv)
  2011. {
  2012. unsigned int i;
  2013. struct bcmgenet_rx_ring *ring;
  2014. for (i = 0; i < priv->hw_params->rx_queues; ++i) {
  2015. ring = &priv->rx_rings[i];
  2016. napi_disable(&ring->napi);
  2017. cancel_work_sync(&ring->dim.dim.work);
  2018. }
  2019. ring = &priv->rx_rings[DESC_INDEX];
  2020. napi_disable(&ring->napi);
  2021. cancel_work_sync(&ring->dim.dim.work);
  2022. }
  2023. static void bcmgenet_fini_rx_napi(struct bcmgenet_priv *priv)
  2024. {
  2025. unsigned int i;
  2026. struct bcmgenet_rx_ring *ring;
  2027. for (i = 0; i < priv->hw_params->rx_queues; ++i) {
  2028. ring = &priv->rx_rings[i];
  2029. netif_napi_del(&ring->napi);
  2030. }
  2031. ring = &priv->rx_rings[DESC_INDEX];
  2032. netif_napi_del(&ring->napi);
  2033. }
  2034. /* Initialize Rx queues
  2035. *
  2036. * Queues 0-15 are priority queues. Hardware Filtering Block (HFB) can be
  2037. * used to direct traffic to these queues.
  2038. *
  2039. * Queue 16 is the default Rx queue with GENET_Q16_RX_BD_CNT descriptors.
  2040. */
  2041. static int bcmgenet_init_rx_queues(struct net_device *dev)
  2042. {
  2043. struct bcmgenet_priv *priv = netdev_priv(dev);
  2044. u32 i;
  2045. u32 dma_enable;
  2046. u32 dma_ctrl;
  2047. u32 ring_cfg;
  2048. int ret;
  2049. dma_ctrl = bcmgenet_rdma_readl(priv, DMA_CTRL);
  2050. dma_enable = dma_ctrl & DMA_EN;
  2051. dma_ctrl &= ~DMA_EN;
  2052. bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
  2053. dma_ctrl = 0;
  2054. ring_cfg = 0;
  2055. /* Initialize Rx priority queues */
  2056. for (i = 0; i < priv->hw_params->rx_queues; i++) {
  2057. ret = bcmgenet_init_rx_ring(priv, i,
  2058. priv->hw_params->rx_bds_per_q,
  2059. i * priv->hw_params->rx_bds_per_q,
  2060. (i + 1) *
  2061. priv->hw_params->rx_bds_per_q);
  2062. if (ret)
  2063. return ret;
  2064. ring_cfg |= (1 << i);
  2065. dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
  2066. }
  2067. /* Initialize Rx default queue 16 */
  2068. ret = bcmgenet_init_rx_ring(priv, DESC_INDEX, GENET_Q16_RX_BD_CNT,
  2069. priv->hw_params->rx_queues *
  2070. priv->hw_params->rx_bds_per_q,
  2071. TOTAL_DESC);
  2072. if (ret)
  2073. return ret;
  2074. ring_cfg |= (1 << DESC_INDEX);
  2075. dma_ctrl |= (1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT));
  2076. /* Enable rings */
  2077. bcmgenet_rdma_writel(priv, ring_cfg, DMA_RING_CFG);
  2078. /* Configure ring as descriptor ring and re-enable DMA if enabled */
  2079. if (dma_enable)
  2080. dma_ctrl |= DMA_EN;
  2081. bcmgenet_rdma_writel(priv, dma_ctrl, DMA_CTRL);
  2082. return 0;
  2083. }
  2084. static int bcmgenet_dma_teardown(struct bcmgenet_priv *priv)
  2085. {
  2086. int ret = 0;
  2087. int timeout = 0;
  2088. u32 reg;
  2089. u32 dma_ctrl;
  2090. int i;
  2091. /* Disable TDMA to stop add more frames in TX DMA */
  2092. reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
  2093. reg &= ~DMA_EN;
  2094. bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
  2095. /* Check TDMA status register to confirm TDMA is disabled */
  2096. while (timeout++ < DMA_TIMEOUT_VAL) {
  2097. reg = bcmgenet_tdma_readl(priv, DMA_STATUS);
  2098. if (reg & DMA_DISABLED)
  2099. break;
  2100. udelay(1);
  2101. }
  2102. if (timeout == DMA_TIMEOUT_VAL) {
  2103. netdev_warn(priv->dev, "Timed out while disabling TX DMA\n");
  2104. ret = -ETIMEDOUT;
  2105. }
  2106. /* Wait 10ms for packet drain in both tx and rx dma */
  2107. usleep_range(10000, 20000);
  2108. /* Disable RDMA */
  2109. reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
  2110. reg &= ~DMA_EN;
  2111. bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
  2112. timeout = 0;
  2113. /* Check RDMA status register to confirm RDMA is disabled */
  2114. while (timeout++ < DMA_TIMEOUT_VAL) {
  2115. reg = bcmgenet_rdma_readl(priv, DMA_STATUS);
  2116. if (reg & DMA_DISABLED)
  2117. break;
  2118. udelay(1);
  2119. }
  2120. if (timeout == DMA_TIMEOUT_VAL) {
  2121. netdev_warn(priv->dev, "Timed out while disabling RX DMA\n");
  2122. ret = -ETIMEDOUT;
  2123. }
  2124. dma_ctrl = 0;
  2125. for (i = 0; i < priv->hw_params->rx_queues; i++)
  2126. dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
  2127. reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
  2128. reg &= ~dma_ctrl;
  2129. bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
  2130. dma_ctrl = 0;
  2131. for (i = 0; i < priv->hw_params->tx_queues; i++)
  2132. dma_ctrl |= (1 << (i + DMA_RING_BUF_EN_SHIFT));
  2133. reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
  2134. reg &= ~dma_ctrl;
  2135. bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
  2136. return ret;
  2137. }
  2138. static void bcmgenet_fini_dma(struct bcmgenet_priv *priv)
  2139. {
  2140. struct netdev_queue *txq;
  2141. struct sk_buff *skb;
  2142. struct enet_cb *cb;
  2143. int i;
  2144. bcmgenet_fini_rx_napi(priv);
  2145. bcmgenet_fini_tx_napi(priv);
  2146. for (i = 0; i < priv->num_tx_bds; i++) {
  2147. cb = priv->tx_cbs + i;
  2148. skb = bcmgenet_free_tx_cb(&priv->pdev->dev, cb);
  2149. if (skb)
  2150. dev_kfree_skb(skb);
  2151. }
  2152. for (i = 0; i < priv->hw_params->tx_queues; i++) {
  2153. txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[i].queue);
  2154. netdev_tx_reset_queue(txq);
  2155. }
  2156. txq = netdev_get_tx_queue(priv->dev, priv->tx_rings[DESC_INDEX].queue);
  2157. netdev_tx_reset_queue(txq);
  2158. bcmgenet_free_rx_buffers(priv);
  2159. kfree(priv->rx_cbs);
  2160. kfree(priv->tx_cbs);
  2161. }
  2162. /* init_edma: Initialize DMA control register */
  2163. static int bcmgenet_init_dma(struct bcmgenet_priv *priv)
  2164. {
  2165. int ret;
  2166. unsigned int i;
  2167. struct enet_cb *cb;
  2168. netif_dbg(priv, hw, priv->dev, "%s\n", __func__);
  2169. /* Initialize common Rx ring structures */
  2170. priv->rx_bds = priv->base + priv->hw_params->rdma_offset;
  2171. priv->num_rx_bds = TOTAL_DESC;
  2172. priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct enet_cb),
  2173. GFP_KERNEL);
  2174. if (!priv->rx_cbs)
  2175. return -ENOMEM;
  2176. for (i = 0; i < priv->num_rx_bds; i++) {
  2177. cb = priv->rx_cbs + i;
  2178. cb->bd_addr = priv->rx_bds + i * DMA_DESC_SIZE;
  2179. }
  2180. /* Initialize common TX ring structures */
  2181. priv->tx_bds = priv->base + priv->hw_params->tdma_offset;
  2182. priv->num_tx_bds = TOTAL_DESC;
  2183. priv->tx_cbs = kcalloc(priv->num_tx_bds, sizeof(struct enet_cb),
  2184. GFP_KERNEL);
  2185. if (!priv->tx_cbs) {
  2186. kfree(priv->rx_cbs);
  2187. return -ENOMEM;
  2188. }
  2189. for (i = 0; i < priv->num_tx_bds; i++) {
  2190. cb = priv->tx_cbs + i;
  2191. cb->bd_addr = priv->tx_bds + i * DMA_DESC_SIZE;
  2192. }
  2193. /* Init rDma */
  2194. bcmgenet_rdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
  2195. /* Initialize Rx queues */
  2196. ret = bcmgenet_init_rx_queues(priv->dev);
  2197. if (ret) {
  2198. netdev_err(priv->dev, "failed to initialize Rx queues\n");
  2199. bcmgenet_free_rx_buffers(priv);
  2200. kfree(priv->rx_cbs);
  2201. kfree(priv->tx_cbs);
  2202. return ret;
  2203. }
  2204. /* Init tDma */
  2205. bcmgenet_tdma_writel(priv, DMA_MAX_BURST_LENGTH, DMA_SCB_BURST_SIZE);
  2206. /* Initialize Tx queues */
  2207. bcmgenet_init_tx_queues(priv->dev);
  2208. return 0;
  2209. }
  2210. /* Interrupt bottom half */
  2211. static void bcmgenet_irq_task(struct work_struct *work)
  2212. {
  2213. unsigned int status;
  2214. struct bcmgenet_priv *priv = container_of(
  2215. work, struct bcmgenet_priv, bcmgenet_irq_work);
  2216. netif_dbg(priv, intr, priv->dev, "%s\n", __func__);
  2217. spin_lock_irq(&priv->lock);
  2218. status = priv->irq0_stat;
  2219. priv->irq0_stat = 0;
  2220. spin_unlock_irq(&priv->lock);
  2221. if (status & UMAC_IRQ_PHY_DET_R &&
  2222. priv->dev->phydev->autoneg != AUTONEG_ENABLE) {
  2223. phy_init_hw(priv->dev->phydev);
  2224. genphy_config_aneg(priv->dev->phydev);
  2225. }
  2226. /* Link UP/DOWN event */
  2227. if (status & UMAC_IRQ_LINK_EVENT)
  2228. phy_mac_interrupt(priv->dev->phydev);
  2229. }
  2230. /* bcmgenet_isr1: handle Rx and Tx priority queues */
  2231. static irqreturn_t bcmgenet_isr1(int irq, void *dev_id)
  2232. {
  2233. struct bcmgenet_priv *priv = dev_id;
  2234. struct bcmgenet_rx_ring *rx_ring;
  2235. struct bcmgenet_tx_ring *tx_ring;
  2236. unsigned int index, status;
  2237. /* Read irq status */
  2238. status = bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_STAT) &
  2239. ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
  2240. /* clear interrupts */
  2241. bcmgenet_intrl2_1_writel(priv, status, INTRL2_CPU_CLEAR);
  2242. netif_dbg(priv, intr, priv->dev,
  2243. "%s: IRQ=0x%x\n", __func__, status);
  2244. /* Check Rx priority queue interrupts */
  2245. for (index = 0; index < priv->hw_params->rx_queues; index++) {
  2246. if (!(status & BIT(UMAC_IRQ1_RX_INTR_SHIFT + index)))
  2247. continue;
  2248. rx_ring = &priv->rx_rings[index];
  2249. rx_ring->dim.event_ctr++;
  2250. if (likely(napi_schedule_prep(&rx_ring->napi))) {
  2251. rx_ring->int_disable(rx_ring);
  2252. __napi_schedule_irqoff(&rx_ring->napi);
  2253. }
  2254. }
  2255. /* Check Tx priority queue interrupts */
  2256. for (index = 0; index < priv->hw_params->tx_queues; index++) {
  2257. if (!(status & BIT(index)))
  2258. continue;
  2259. tx_ring = &priv->tx_rings[index];
  2260. if (likely(napi_schedule_prep(&tx_ring->napi))) {
  2261. tx_ring->int_disable(tx_ring);
  2262. __napi_schedule_irqoff(&tx_ring->napi);
  2263. }
  2264. }
  2265. return IRQ_HANDLED;
  2266. }
  2267. /* bcmgenet_isr0: handle Rx and Tx default queues + other stuff */
  2268. static irqreturn_t bcmgenet_isr0(int irq, void *dev_id)
  2269. {
  2270. struct bcmgenet_priv *priv = dev_id;
  2271. struct bcmgenet_rx_ring *rx_ring;
  2272. struct bcmgenet_tx_ring *tx_ring;
  2273. unsigned int status;
  2274. unsigned long flags;
  2275. /* Read irq status */
  2276. status = bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_STAT) &
  2277. ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
  2278. /* clear interrupts */
  2279. bcmgenet_intrl2_0_writel(priv, status, INTRL2_CPU_CLEAR);
  2280. netif_dbg(priv, intr, priv->dev,
  2281. "IRQ=0x%x\n", status);
  2282. if (status & UMAC_IRQ_RXDMA_DONE) {
  2283. rx_ring = &priv->rx_rings[DESC_INDEX];
  2284. rx_ring->dim.event_ctr++;
  2285. if (likely(napi_schedule_prep(&rx_ring->napi))) {
  2286. rx_ring->int_disable(rx_ring);
  2287. __napi_schedule_irqoff(&rx_ring->napi);
  2288. }
  2289. }
  2290. if (status & UMAC_IRQ_TXDMA_DONE) {
  2291. tx_ring = &priv->tx_rings[DESC_INDEX];
  2292. if (likely(napi_schedule_prep(&tx_ring->napi))) {
  2293. tx_ring->int_disable(tx_ring);
  2294. __napi_schedule_irqoff(&tx_ring->napi);
  2295. }
  2296. }
  2297. if ((priv->hw_params->flags & GENET_HAS_MDIO_INTR) &&
  2298. status & (UMAC_IRQ_MDIO_DONE | UMAC_IRQ_MDIO_ERROR)) {
  2299. wake_up(&priv->wq);
  2300. }
  2301. /* all other interested interrupts handled in bottom half */
  2302. status &= (UMAC_IRQ_LINK_EVENT | UMAC_IRQ_PHY_DET_R);
  2303. if (status) {
  2304. /* Save irq status for bottom-half processing. */
  2305. spin_lock_irqsave(&priv->lock, flags);
  2306. priv->irq0_stat |= status;
  2307. spin_unlock_irqrestore(&priv->lock, flags);
  2308. schedule_work(&priv->bcmgenet_irq_work);
  2309. }
  2310. return IRQ_HANDLED;
  2311. }
  2312. static irqreturn_t bcmgenet_wol_isr(int irq, void *dev_id)
  2313. {
  2314. struct bcmgenet_priv *priv = dev_id;
  2315. pm_wakeup_event(&priv->pdev->dev, 0);
  2316. return IRQ_HANDLED;
  2317. }
  2318. #ifdef CONFIG_NET_POLL_CONTROLLER
  2319. static void bcmgenet_poll_controller(struct net_device *dev)
  2320. {
  2321. struct bcmgenet_priv *priv = netdev_priv(dev);
  2322. /* Invoke the main RX/TX interrupt handler */
  2323. disable_irq(priv->irq0);
  2324. bcmgenet_isr0(priv->irq0, priv);
  2325. enable_irq(priv->irq0);
  2326. /* And the interrupt handler for RX/TX priority queues */
  2327. disable_irq(priv->irq1);
  2328. bcmgenet_isr1(priv->irq1, priv);
  2329. enable_irq(priv->irq1);
  2330. }
  2331. #endif
  2332. static void bcmgenet_umac_reset(struct bcmgenet_priv *priv)
  2333. {
  2334. u32 reg;
  2335. reg = bcmgenet_rbuf_ctrl_get(priv);
  2336. reg |= BIT(1);
  2337. bcmgenet_rbuf_ctrl_set(priv, reg);
  2338. udelay(10);
  2339. reg &= ~BIT(1);
  2340. bcmgenet_rbuf_ctrl_set(priv, reg);
  2341. udelay(10);
  2342. }
  2343. static void bcmgenet_set_hw_addr(struct bcmgenet_priv *priv,
  2344. unsigned char *addr)
  2345. {
  2346. bcmgenet_umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
  2347. (addr[2] << 8) | addr[3], UMAC_MAC0);
  2348. bcmgenet_umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
  2349. }
  2350. /* Returns a reusable dma control register value */
  2351. static u32 bcmgenet_dma_disable(struct bcmgenet_priv *priv)
  2352. {
  2353. u32 reg;
  2354. u32 dma_ctrl;
  2355. /* disable DMA */
  2356. dma_ctrl = 1 << (DESC_INDEX + DMA_RING_BUF_EN_SHIFT) | DMA_EN;
  2357. reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
  2358. reg &= ~dma_ctrl;
  2359. bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
  2360. reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
  2361. reg &= ~dma_ctrl;
  2362. bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
  2363. bcmgenet_umac_writel(priv, 1, UMAC_TX_FLUSH);
  2364. udelay(10);
  2365. bcmgenet_umac_writel(priv, 0, UMAC_TX_FLUSH);
  2366. return dma_ctrl;
  2367. }
  2368. static void bcmgenet_enable_dma(struct bcmgenet_priv *priv, u32 dma_ctrl)
  2369. {
  2370. u32 reg;
  2371. reg = bcmgenet_rdma_readl(priv, DMA_CTRL);
  2372. reg |= dma_ctrl;
  2373. bcmgenet_rdma_writel(priv, reg, DMA_CTRL);
  2374. reg = bcmgenet_tdma_readl(priv, DMA_CTRL);
  2375. reg |= dma_ctrl;
  2376. bcmgenet_tdma_writel(priv, reg, DMA_CTRL);
  2377. }
  2378. /* bcmgenet_hfb_clear
  2379. *
  2380. * Clear Hardware Filter Block and disable all filtering.
  2381. */
  2382. static void bcmgenet_hfb_clear(struct bcmgenet_priv *priv)
  2383. {
  2384. u32 i;
  2385. bcmgenet_hfb_reg_writel(priv, 0x0, HFB_CTRL);
  2386. bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS);
  2387. bcmgenet_hfb_reg_writel(priv, 0x0, HFB_FLT_ENABLE_V3PLUS + 4);
  2388. for (i = DMA_INDEX2RING_0; i <= DMA_INDEX2RING_7; i++)
  2389. bcmgenet_rdma_writel(priv, 0x0, i);
  2390. for (i = 0; i < (priv->hw_params->hfb_filter_cnt / 4); i++)
  2391. bcmgenet_hfb_reg_writel(priv, 0x0,
  2392. HFB_FLT_LEN_V3PLUS + i * sizeof(u32));
  2393. for (i = 0; i < priv->hw_params->hfb_filter_cnt *
  2394. priv->hw_params->hfb_filter_size; i++)
  2395. bcmgenet_hfb_writel(priv, 0x0, i * sizeof(u32));
  2396. }
  2397. static void bcmgenet_hfb_init(struct bcmgenet_priv *priv)
  2398. {
  2399. if (GENET_IS_V1(priv) || GENET_IS_V2(priv))
  2400. return;
  2401. bcmgenet_hfb_clear(priv);
  2402. }
  2403. static void bcmgenet_netif_start(struct net_device *dev)
  2404. {
  2405. struct bcmgenet_priv *priv = netdev_priv(dev);
  2406. /* Start the network engine */
  2407. bcmgenet_enable_rx_napi(priv);
  2408. umac_enable_set(priv, CMD_TX_EN | CMD_RX_EN, true);
  2409. bcmgenet_enable_tx_napi(priv);
  2410. /* Monitor link interrupts now */
  2411. bcmgenet_link_intr_enable(priv);
  2412. phy_start(dev->phydev);
  2413. }
  2414. static int bcmgenet_open(struct net_device *dev)
  2415. {
  2416. struct bcmgenet_priv *priv = netdev_priv(dev);
  2417. unsigned long dma_ctrl;
  2418. u32 reg;
  2419. int ret;
  2420. netif_dbg(priv, ifup, dev, "bcmgenet_open\n");
  2421. /* Turn on the clock */
  2422. clk_prepare_enable(priv->clk);
  2423. /* If this is an internal GPHY, power it back on now, before UniMAC is
  2424. * brought out of reset as absolutely no UniMAC activity is allowed
  2425. */
  2426. if (priv->internal_phy)
  2427. bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
  2428. /* take MAC out of reset */
  2429. bcmgenet_umac_reset(priv);
  2430. init_umac(priv);
  2431. /* Make sure we reflect the value of CRC_CMD_FWD */
  2432. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  2433. priv->crc_fwd_en = !!(reg & CMD_CRC_FWD);
  2434. bcmgenet_set_hw_addr(priv, dev->dev_addr);
  2435. if (priv->internal_phy) {
  2436. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  2437. reg |= EXT_ENERGY_DET_MASK;
  2438. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  2439. }
  2440. /* Disable RX/TX DMA and flush TX queues */
  2441. dma_ctrl = bcmgenet_dma_disable(priv);
  2442. /* Reinitialize TDMA and RDMA and SW housekeeping */
  2443. ret = bcmgenet_init_dma(priv);
  2444. if (ret) {
  2445. netdev_err(dev, "failed to initialize DMA\n");
  2446. goto err_clk_disable;
  2447. }
  2448. /* Always enable ring 16 - descriptor ring */
  2449. bcmgenet_enable_dma(priv, dma_ctrl);
  2450. /* HFB init */
  2451. bcmgenet_hfb_init(priv);
  2452. ret = request_irq(priv->irq0, bcmgenet_isr0, IRQF_SHARED,
  2453. dev->name, priv);
  2454. if (ret < 0) {
  2455. netdev_err(dev, "can't request IRQ %d\n", priv->irq0);
  2456. goto err_fini_dma;
  2457. }
  2458. ret = request_irq(priv->irq1, bcmgenet_isr1, IRQF_SHARED,
  2459. dev->name, priv);
  2460. if (ret < 0) {
  2461. netdev_err(dev, "can't request IRQ %d\n", priv->irq1);
  2462. goto err_irq0;
  2463. }
  2464. ret = bcmgenet_mii_probe(dev);
  2465. if (ret) {
  2466. netdev_err(dev, "failed to connect to PHY\n");
  2467. goto err_irq1;
  2468. }
  2469. bcmgenet_netif_start(dev);
  2470. netif_tx_start_all_queues(dev);
  2471. return 0;
  2472. err_irq1:
  2473. free_irq(priv->irq1, priv);
  2474. err_irq0:
  2475. free_irq(priv->irq0, priv);
  2476. err_fini_dma:
  2477. bcmgenet_dma_teardown(priv);
  2478. bcmgenet_fini_dma(priv);
  2479. err_clk_disable:
  2480. if (priv->internal_phy)
  2481. bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
  2482. clk_disable_unprepare(priv->clk);
  2483. return ret;
  2484. }
  2485. static void bcmgenet_netif_stop(struct net_device *dev)
  2486. {
  2487. struct bcmgenet_priv *priv = netdev_priv(dev);
  2488. bcmgenet_disable_tx_napi(priv);
  2489. netif_tx_disable(dev);
  2490. /* Disable MAC receive */
  2491. umac_enable_set(priv, CMD_RX_EN, false);
  2492. bcmgenet_dma_teardown(priv);
  2493. /* Disable MAC transmit. TX DMA disabled must be done before this */
  2494. umac_enable_set(priv, CMD_TX_EN, false);
  2495. phy_stop(dev->phydev);
  2496. bcmgenet_disable_rx_napi(priv);
  2497. bcmgenet_intr_disable(priv);
  2498. /* Wait for pending work items to complete. Since interrupts are
  2499. * disabled no new work will be scheduled.
  2500. */
  2501. cancel_work_sync(&priv->bcmgenet_irq_work);
  2502. priv->old_link = -1;
  2503. priv->old_speed = -1;
  2504. priv->old_duplex = -1;
  2505. priv->old_pause = -1;
  2506. /* tx reclaim */
  2507. bcmgenet_tx_reclaim_all(dev);
  2508. bcmgenet_fini_dma(priv);
  2509. }
  2510. static int bcmgenet_close(struct net_device *dev)
  2511. {
  2512. struct bcmgenet_priv *priv = netdev_priv(dev);
  2513. int ret = 0;
  2514. netif_dbg(priv, ifdown, dev, "bcmgenet_close\n");
  2515. bcmgenet_netif_stop(dev);
  2516. /* Really kill the PHY state machine and disconnect from it */
  2517. phy_disconnect(dev->phydev);
  2518. free_irq(priv->irq0, priv);
  2519. free_irq(priv->irq1, priv);
  2520. if (priv->internal_phy)
  2521. ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
  2522. clk_disable_unprepare(priv->clk);
  2523. return ret;
  2524. }
  2525. static void bcmgenet_dump_tx_queue(struct bcmgenet_tx_ring *ring)
  2526. {
  2527. struct bcmgenet_priv *priv = ring->priv;
  2528. u32 p_index, c_index, intsts, intmsk;
  2529. struct netdev_queue *txq;
  2530. unsigned int free_bds;
  2531. bool txq_stopped;
  2532. if (!netif_msg_tx_err(priv))
  2533. return;
  2534. txq = netdev_get_tx_queue(priv->dev, ring->queue);
  2535. spin_lock(&ring->lock);
  2536. if (ring->index == DESC_INDEX) {
  2537. intsts = ~bcmgenet_intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
  2538. intmsk = UMAC_IRQ_TXDMA_DONE | UMAC_IRQ_TXDMA_MBDONE;
  2539. } else {
  2540. intsts = ~bcmgenet_intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
  2541. intmsk = 1 << ring->index;
  2542. }
  2543. c_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_CONS_INDEX);
  2544. p_index = bcmgenet_tdma_ring_readl(priv, ring->index, TDMA_PROD_INDEX);
  2545. txq_stopped = netif_tx_queue_stopped(txq);
  2546. free_bds = ring->free_bds;
  2547. spin_unlock(&ring->lock);
  2548. netif_err(priv, tx_err, priv->dev, "Ring %d queue %d status summary\n"
  2549. "TX queue status: %s, interrupts: %s\n"
  2550. "(sw)free_bds: %d (sw)size: %d\n"
  2551. "(sw)p_index: %d (hw)p_index: %d\n"
  2552. "(sw)c_index: %d (hw)c_index: %d\n"
  2553. "(sw)clean_p: %d (sw)write_p: %d\n"
  2554. "(sw)cb_ptr: %d (sw)end_ptr: %d\n",
  2555. ring->index, ring->queue,
  2556. txq_stopped ? "stopped" : "active",
  2557. intsts & intmsk ? "enabled" : "disabled",
  2558. free_bds, ring->size,
  2559. ring->prod_index, p_index & DMA_P_INDEX_MASK,
  2560. ring->c_index, c_index & DMA_C_INDEX_MASK,
  2561. ring->clean_ptr, ring->write_ptr,
  2562. ring->cb_ptr, ring->end_ptr);
  2563. }
  2564. static void bcmgenet_timeout(struct net_device *dev)
  2565. {
  2566. struct bcmgenet_priv *priv = netdev_priv(dev);
  2567. u32 int0_enable = 0;
  2568. u32 int1_enable = 0;
  2569. unsigned int q;
  2570. netif_dbg(priv, tx_err, dev, "bcmgenet_timeout\n");
  2571. for (q = 0; q < priv->hw_params->tx_queues; q++)
  2572. bcmgenet_dump_tx_queue(&priv->tx_rings[q]);
  2573. bcmgenet_dump_tx_queue(&priv->tx_rings[DESC_INDEX]);
  2574. bcmgenet_tx_reclaim_all(dev);
  2575. for (q = 0; q < priv->hw_params->tx_queues; q++)
  2576. int1_enable |= (1 << q);
  2577. int0_enable = UMAC_IRQ_TXDMA_DONE;
  2578. /* Re-enable TX interrupts if disabled */
  2579. bcmgenet_intrl2_0_writel(priv, int0_enable, INTRL2_CPU_MASK_CLEAR);
  2580. bcmgenet_intrl2_1_writel(priv, int1_enable, INTRL2_CPU_MASK_CLEAR);
  2581. netif_trans_update(dev);
  2582. dev->stats.tx_errors++;
  2583. netif_tx_wake_all_queues(dev);
  2584. }
  2585. #define MAX_MDF_FILTER 17
  2586. static inline void bcmgenet_set_mdf_addr(struct bcmgenet_priv *priv,
  2587. unsigned char *addr,
  2588. int *i)
  2589. {
  2590. bcmgenet_umac_writel(priv, addr[0] << 8 | addr[1],
  2591. UMAC_MDF_ADDR + (*i * 4));
  2592. bcmgenet_umac_writel(priv, addr[2] << 24 | addr[3] << 16 |
  2593. addr[4] << 8 | addr[5],
  2594. UMAC_MDF_ADDR + ((*i + 1) * 4));
  2595. *i += 2;
  2596. }
  2597. static void bcmgenet_set_rx_mode(struct net_device *dev)
  2598. {
  2599. struct bcmgenet_priv *priv = netdev_priv(dev);
  2600. struct netdev_hw_addr *ha;
  2601. int i, nfilter;
  2602. u32 reg;
  2603. netif_dbg(priv, hw, dev, "%s: %08X\n", __func__, dev->flags);
  2604. /* Number of filters needed */
  2605. nfilter = netdev_uc_count(dev) + netdev_mc_count(dev) + 2;
  2606. /*
  2607. * Turn on promicuous mode for three scenarios
  2608. * 1. IFF_PROMISC flag is set
  2609. * 2. IFF_ALLMULTI flag is set
  2610. * 3. The number of filters needed exceeds the number filters
  2611. * supported by the hardware.
  2612. */
  2613. reg = bcmgenet_umac_readl(priv, UMAC_CMD);
  2614. if ((dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) ||
  2615. (nfilter > MAX_MDF_FILTER)) {
  2616. reg |= CMD_PROMISC;
  2617. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  2618. bcmgenet_umac_writel(priv, 0, UMAC_MDF_CTRL);
  2619. return;
  2620. } else {
  2621. reg &= ~CMD_PROMISC;
  2622. bcmgenet_umac_writel(priv, reg, UMAC_CMD);
  2623. }
  2624. /* update MDF filter */
  2625. i = 0;
  2626. /* Broadcast */
  2627. bcmgenet_set_mdf_addr(priv, dev->broadcast, &i);
  2628. /* my own address.*/
  2629. bcmgenet_set_mdf_addr(priv, dev->dev_addr, &i);
  2630. /* Unicast */
  2631. netdev_for_each_uc_addr(ha, dev)
  2632. bcmgenet_set_mdf_addr(priv, ha->addr, &i);
  2633. /* Multicast */
  2634. netdev_for_each_mc_addr(ha, dev)
  2635. bcmgenet_set_mdf_addr(priv, ha->addr, &i);
  2636. /* Enable filters */
  2637. reg = GENMASK(MAX_MDF_FILTER - 1, MAX_MDF_FILTER - nfilter);
  2638. bcmgenet_umac_writel(priv, reg, UMAC_MDF_CTRL);
  2639. }
  2640. /* Set the hardware MAC address. */
  2641. static int bcmgenet_set_mac_addr(struct net_device *dev, void *p)
  2642. {
  2643. struct sockaddr *addr = p;
  2644. /* Setting the MAC address at the hardware level is not possible
  2645. * without disabling the UniMAC RX/TX enable bits.
  2646. */
  2647. if (netif_running(dev))
  2648. return -EBUSY;
  2649. ether_addr_copy(dev->dev_addr, addr->sa_data);
  2650. return 0;
  2651. }
  2652. static struct net_device_stats *bcmgenet_get_stats(struct net_device *dev)
  2653. {
  2654. struct bcmgenet_priv *priv = netdev_priv(dev);
  2655. unsigned long tx_bytes = 0, tx_packets = 0;
  2656. unsigned long rx_bytes = 0, rx_packets = 0;
  2657. unsigned long rx_errors = 0, rx_dropped = 0;
  2658. struct bcmgenet_tx_ring *tx_ring;
  2659. struct bcmgenet_rx_ring *rx_ring;
  2660. unsigned int q;
  2661. for (q = 0; q < priv->hw_params->tx_queues; q++) {
  2662. tx_ring = &priv->tx_rings[q];
  2663. tx_bytes += tx_ring->bytes;
  2664. tx_packets += tx_ring->packets;
  2665. }
  2666. tx_ring = &priv->tx_rings[DESC_INDEX];
  2667. tx_bytes += tx_ring->bytes;
  2668. tx_packets += tx_ring->packets;
  2669. for (q = 0; q < priv->hw_params->rx_queues; q++) {
  2670. rx_ring = &priv->rx_rings[q];
  2671. rx_bytes += rx_ring->bytes;
  2672. rx_packets += rx_ring->packets;
  2673. rx_errors += rx_ring->errors;
  2674. rx_dropped += rx_ring->dropped;
  2675. }
  2676. rx_ring = &priv->rx_rings[DESC_INDEX];
  2677. rx_bytes += rx_ring->bytes;
  2678. rx_packets += rx_ring->packets;
  2679. rx_errors += rx_ring->errors;
  2680. rx_dropped += rx_ring->dropped;
  2681. dev->stats.tx_bytes = tx_bytes;
  2682. dev->stats.tx_packets = tx_packets;
  2683. dev->stats.rx_bytes = rx_bytes;
  2684. dev->stats.rx_packets = rx_packets;
  2685. dev->stats.rx_errors = rx_errors;
  2686. dev->stats.rx_missed_errors = rx_errors;
  2687. return &dev->stats;
  2688. }
  2689. static const struct net_device_ops bcmgenet_netdev_ops = {
  2690. .ndo_open = bcmgenet_open,
  2691. .ndo_stop = bcmgenet_close,
  2692. .ndo_start_xmit = bcmgenet_xmit,
  2693. .ndo_tx_timeout = bcmgenet_timeout,
  2694. .ndo_set_rx_mode = bcmgenet_set_rx_mode,
  2695. .ndo_set_mac_address = bcmgenet_set_mac_addr,
  2696. .ndo_do_ioctl = bcmgenet_ioctl,
  2697. .ndo_set_features = bcmgenet_set_features,
  2698. #ifdef CONFIG_NET_POLL_CONTROLLER
  2699. .ndo_poll_controller = bcmgenet_poll_controller,
  2700. #endif
  2701. .ndo_get_stats = bcmgenet_get_stats,
  2702. };
  2703. /* Array of GENET hardware parameters/characteristics */
  2704. static struct bcmgenet_hw_params bcmgenet_hw_params[] = {
  2705. [GENET_V1] = {
  2706. .tx_queues = 0,
  2707. .tx_bds_per_q = 0,
  2708. .rx_queues = 0,
  2709. .rx_bds_per_q = 0,
  2710. .bp_in_en_shift = 16,
  2711. .bp_in_mask = 0xffff,
  2712. .hfb_filter_cnt = 16,
  2713. .qtag_mask = 0x1F,
  2714. .hfb_offset = 0x1000,
  2715. .rdma_offset = 0x2000,
  2716. .tdma_offset = 0x3000,
  2717. .words_per_bd = 2,
  2718. },
  2719. [GENET_V2] = {
  2720. .tx_queues = 4,
  2721. .tx_bds_per_q = 32,
  2722. .rx_queues = 0,
  2723. .rx_bds_per_q = 0,
  2724. .bp_in_en_shift = 16,
  2725. .bp_in_mask = 0xffff,
  2726. .hfb_filter_cnt = 16,
  2727. .qtag_mask = 0x1F,
  2728. .tbuf_offset = 0x0600,
  2729. .hfb_offset = 0x1000,
  2730. .hfb_reg_offset = 0x2000,
  2731. .rdma_offset = 0x3000,
  2732. .tdma_offset = 0x4000,
  2733. .words_per_bd = 2,
  2734. .flags = GENET_HAS_EXT,
  2735. },
  2736. [GENET_V3] = {
  2737. .tx_queues = 4,
  2738. .tx_bds_per_q = 32,
  2739. .rx_queues = 0,
  2740. .rx_bds_per_q = 0,
  2741. .bp_in_en_shift = 17,
  2742. .bp_in_mask = 0x1ffff,
  2743. .hfb_filter_cnt = 48,
  2744. .hfb_filter_size = 128,
  2745. .qtag_mask = 0x3F,
  2746. .tbuf_offset = 0x0600,
  2747. .hfb_offset = 0x8000,
  2748. .hfb_reg_offset = 0xfc00,
  2749. .rdma_offset = 0x10000,
  2750. .tdma_offset = 0x11000,
  2751. .words_per_bd = 2,
  2752. .flags = GENET_HAS_EXT | GENET_HAS_MDIO_INTR |
  2753. GENET_HAS_MOCA_LINK_DET,
  2754. },
  2755. [GENET_V4] = {
  2756. .tx_queues = 4,
  2757. .tx_bds_per_q = 32,
  2758. .rx_queues = 0,
  2759. .rx_bds_per_q = 0,
  2760. .bp_in_en_shift = 17,
  2761. .bp_in_mask = 0x1ffff,
  2762. .hfb_filter_cnt = 48,
  2763. .hfb_filter_size = 128,
  2764. .qtag_mask = 0x3F,
  2765. .tbuf_offset = 0x0600,
  2766. .hfb_offset = 0x8000,
  2767. .hfb_reg_offset = 0xfc00,
  2768. .rdma_offset = 0x2000,
  2769. .tdma_offset = 0x4000,
  2770. .words_per_bd = 3,
  2771. .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
  2772. GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
  2773. },
  2774. [GENET_V5] = {
  2775. .tx_queues = 4,
  2776. .tx_bds_per_q = 32,
  2777. .rx_queues = 0,
  2778. .rx_bds_per_q = 0,
  2779. .bp_in_en_shift = 17,
  2780. .bp_in_mask = 0x1ffff,
  2781. .hfb_filter_cnt = 48,
  2782. .hfb_filter_size = 128,
  2783. .qtag_mask = 0x3F,
  2784. .tbuf_offset = 0x0600,
  2785. .hfb_offset = 0x8000,
  2786. .hfb_reg_offset = 0xfc00,
  2787. .rdma_offset = 0x2000,
  2788. .tdma_offset = 0x4000,
  2789. .words_per_bd = 3,
  2790. .flags = GENET_HAS_40BITS | GENET_HAS_EXT |
  2791. GENET_HAS_MDIO_INTR | GENET_HAS_MOCA_LINK_DET,
  2792. },
  2793. };
  2794. /* Infer hardware parameters from the detected GENET version */
  2795. static void bcmgenet_set_hw_params(struct bcmgenet_priv *priv)
  2796. {
  2797. struct bcmgenet_hw_params *params;
  2798. u32 reg;
  2799. u8 major;
  2800. u16 gphy_rev;
  2801. if (GENET_IS_V5(priv) || GENET_IS_V4(priv)) {
  2802. bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
  2803. genet_dma_ring_regs = genet_dma_ring_regs_v4;
  2804. priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
  2805. } else if (GENET_IS_V3(priv)) {
  2806. bcmgenet_dma_regs = bcmgenet_dma_regs_v3plus;
  2807. genet_dma_ring_regs = genet_dma_ring_regs_v123;
  2808. priv->dma_rx_chk_bit = DMA_RX_CHK_V3PLUS;
  2809. } else if (GENET_IS_V2(priv)) {
  2810. bcmgenet_dma_regs = bcmgenet_dma_regs_v2;
  2811. genet_dma_ring_regs = genet_dma_ring_regs_v123;
  2812. priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
  2813. } else if (GENET_IS_V1(priv)) {
  2814. bcmgenet_dma_regs = bcmgenet_dma_regs_v1;
  2815. genet_dma_ring_regs = genet_dma_ring_regs_v123;
  2816. priv->dma_rx_chk_bit = DMA_RX_CHK_V12;
  2817. }
  2818. /* enum genet_version starts at 1 */
  2819. priv->hw_params = &bcmgenet_hw_params[priv->version];
  2820. params = priv->hw_params;
  2821. /* Read GENET HW version */
  2822. reg = bcmgenet_sys_readl(priv, SYS_REV_CTRL);
  2823. major = (reg >> 24 & 0x0f);
  2824. if (major == 6)
  2825. major = 5;
  2826. else if (major == 5)
  2827. major = 4;
  2828. else if (major == 0)
  2829. major = 1;
  2830. if (major != priv->version) {
  2831. dev_err(&priv->pdev->dev,
  2832. "GENET version mismatch, got: %d, configured for: %d\n",
  2833. major, priv->version);
  2834. }
  2835. /* Print the GENET core version */
  2836. dev_info(&priv->pdev->dev, "GENET " GENET_VER_FMT,
  2837. major, (reg >> 16) & 0x0f, reg & 0xffff);
  2838. /* Store the integrated PHY revision for the MDIO probing function
  2839. * to pass this information to the PHY driver. The PHY driver expects
  2840. * to find the PHY major revision in bits 15:8 while the GENET register
  2841. * stores that information in bits 7:0, account for that.
  2842. *
  2843. * On newer chips, starting with PHY revision G0, a new scheme is
  2844. * deployed similar to the Starfighter 2 switch with GPHY major
  2845. * revision in bits 15:8 and patch level in bits 7:0. Major revision 0
  2846. * is reserved as well as special value 0x01ff, we have a small
  2847. * heuristic to check for the new GPHY revision and re-arrange things
  2848. * so the GPHY driver is happy.
  2849. */
  2850. gphy_rev = reg & 0xffff;
  2851. if (GENET_IS_V5(priv)) {
  2852. /* The EPHY revision should come from the MDIO registers of
  2853. * the PHY not from GENET.
  2854. */
  2855. if (gphy_rev != 0) {
  2856. pr_warn("GENET is reporting EPHY revision: 0x%04x\n",
  2857. gphy_rev);
  2858. }
  2859. /* This is reserved so should require special treatment */
  2860. } else if (gphy_rev == 0 || gphy_rev == 0x01ff) {
  2861. pr_warn("Invalid GPHY revision detected: 0x%04x\n", gphy_rev);
  2862. return;
  2863. /* This is the good old scheme, just GPHY major, no minor nor patch */
  2864. } else if ((gphy_rev & 0xf0) != 0) {
  2865. priv->gphy_rev = gphy_rev << 8;
  2866. /* This is the new scheme, GPHY major rolls over with 0x10 = rev G0 */
  2867. } else if ((gphy_rev & 0xff00) != 0) {
  2868. priv->gphy_rev = gphy_rev;
  2869. }
  2870. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  2871. if (!(params->flags & GENET_HAS_40BITS))
  2872. pr_warn("GENET does not support 40-bits PA\n");
  2873. #endif
  2874. pr_debug("Configuration for version: %d\n"
  2875. "TXq: %1d, TXqBDs: %1d, RXq: %1d, RXqBDs: %1d\n"
  2876. "BP << en: %2d, BP msk: 0x%05x\n"
  2877. "HFB count: %2d, QTAQ msk: 0x%05x\n"
  2878. "TBUF: 0x%04x, HFB: 0x%04x, HFBreg: 0x%04x\n"
  2879. "RDMA: 0x%05x, TDMA: 0x%05x\n"
  2880. "Words/BD: %d\n",
  2881. priv->version,
  2882. params->tx_queues, params->tx_bds_per_q,
  2883. params->rx_queues, params->rx_bds_per_q,
  2884. params->bp_in_en_shift, params->bp_in_mask,
  2885. params->hfb_filter_cnt, params->qtag_mask,
  2886. params->tbuf_offset, params->hfb_offset,
  2887. params->hfb_reg_offset,
  2888. params->rdma_offset, params->tdma_offset,
  2889. params->words_per_bd);
  2890. }
  2891. static const struct of_device_id bcmgenet_match[] = {
  2892. { .compatible = "brcm,genet-v1", .data = (void *)GENET_V1 },
  2893. { .compatible = "brcm,genet-v2", .data = (void *)GENET_V2 },
  2894. { .compatible = "brcm,genet-v3", .data = (void *)GENET_V3 },
  2895. { .compatible = "brcm,genet-v4", .data = (void *)GENET_V4 },
  2896. { .compatible = "brcm,genet-v5", .data = (void *)GENET_V5 },
  2897. { },
  2898. };
  2899. MODULE_DEVICE_TABLE(of, bcmgenet_match);
  2900. static int bcmgenet_probe(struct platform_device *pdev)
  2901. {
  2902. struct bcmgenet_platform_data *pd = pdev->dev.platform_data;
  2903. struct device_node *dn = pdev->dev.of_node;
  2904. const struct of_device_id *of_id = NULL;
  2905. struct bcmgenet_priv *priv;
  2906. struct net_device *dev;
  2907. const void *macaddr;
  2908. struct resource *r;
  2909. unsigned int i;
  2910. int err = -EIO;
  2911. const char *phy_mode_str;
  2912. /* Up to GENET_MAX_MQ_CNT + 1 TX queues and RX queues */
  2913. dev = alloc_etherdev_mqs(sizeof(*priv), GENET_MAX_MQ_CNT + 1,
  2914. GENET_MAX_MQ_CNT + 1);
  2915. if (!dev) {
  2916. dev_err(&pdev->dev, "can't allocate net device\n");
  2917. return -ENOMEM;
  2918. }
  2919. if (dn) {
  2920. of_id = of_match_node(bcmgenet_match, dn);
  2921. if (!of_id)
  2922. return -EINVAL;
  2923. }
  2924. priv = netdev_priv(dev);
  2925. priv->irq0 = platform_get_irq(pdev, 0);
  2926. priv->irq1 = platform_get_irq(pdev, 1);
  2927. priv->wol_irq = platform_get_irq(pdev, 2);
  2928. if (!priv->irq0 || !priv->irq1) {
  2929. dev_err(&pdev->dev, "can't find IRQs\n");
  2930. err = -EINVAL;
  2931. goto err;
  2932. }
  2933. if (dn) {
  2934. macaddr = of_get_mac_address(dn);
  2935. if (!macaddr) {
  2936. dev_err(&pdev->dev, "can't find MAC address\n");
  2937. err = -EINVAL;
  2938. goto err;
  2939. }
  2940. } else {
  2941. macaddr = pd->mac_address;
  2942. }
  2943. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2944. priv->base = devm_ioremap_resource(&pdev->dev, r);
  2945. if (IS_ERR(priv->base)) {
  2946. err = PTR_ERR(priv->base);
  2947. goto err;
  2948. }
  2949. spin_lock_init(&priv->lock);
  2950. SET_NETDEV_DEV(dev, &pdev->dev);
  2951. dev_set_drvdata(&pdev->dev, dev);
  2952. ether_addr_copy(dev->dev_addr, macaddr);
  2953. dev->watchdog_timeo = 2 * HZ;
  2954. dev->ethtool_ops = &bcmgenet_ethtool_ops;
  2955. dev->netdev_ops = &bcmgenet_netdev_ops;
  2956. priv->msg_enable = netif_msg_init(-1, GENET_MSG_DEFAULT);
  2957. /* Set hardware features */
  2958. dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM |
  2959. NETIF_F_IPV6_CSUM | NETIF_F_RXCSUM;
  2960. /* Request the WOL interrupt and advertise suspend if available */
  2961. priv->wol_irq_disabled = true;
  2962. err = devm_request_irq(&pdev->dev, priv->wol_irq, bcmgenet_wol_isr, 0,
  2963. dev->name, priv);
  2964. if (!err)
  2965. device_set_wakeup_capable(&pdev->dev, 1);
  2966. /* Set the needed headroom to account for any possible
  2967. * features enabling/disabling at runtime
  2968. */
  2969. dev->needed_headroom += 64;
  2970. netdev_boot_setup_check(dev);
  2971. priv->dev = dev;
  2972. priv->pdev = pdev;
  2973. if (of_id)
  2974. priv->version = (enum bcmgenet_version)of_id->data;
  2975. else
  2976. priv->version = pd->genet_version;
  2977. priv->clk = devm_clk_get(&priv->pdev->dev, "enet");
  2978. if (IS_ERR(priv->clk)) {
  2979. dev_warn(&priv->pdev->dev, "failed to get enet clock\n");
  2980. priv->clk = NULL;
  2981. }
  2982. clk_prepare_enable(priv->clk);
  2983. bcmgenet_set_hw_params(priv);
  2984. /* Mii wait queue */
  2985. init_waitqueue_head(&priv->wq);
  2986. /* Always use RX_BUF_LENGTH (2KB) buffer for all chips */
  2987. priv->rx_buf_len = RX_BUF_LENGTH;
  2988. INIT_WORK(&priv->bcmgenet_irq_work, bcmgenet_irq_task);
  2989. priv->clk_wol = devm_clk_get(&priv->pdev->dev, "enet-wol");
  2990. if (IS_ERR(priv->clk_wol)) {
  2991. dev_warn(&priv->pdev->dev, "failed to get enet-wol clock\n");
  2992. priv->clk_wol = NULL;
  2993. }
  2994. priv->clk_eee = devm_clk_get(&priv->pdev->dev, "enet-eee");
  2995. if (IS_ERR(priv->clk_eee)) {
  2996. dev_warn(&priv->pdev->dev, "failed to get enet-eee clock\n");
  2997. priv->clk_eee = NULL;
  2998. }
  2999. /* If this is an internal GPHY, power it on now, before UniMAC is
  3000. * brought out of reset as absolutely no UniMAC activity is allowed
  3001. */
  3002. if (dn && !of_property_read_string(dn, "phy-mode", &phy_mode_str) &&
  3003. !strcasecmp(phy_mode_str, "internal"))
  3004. bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
  3005. reset_umac(priv);
  3006. err = bcmgenet_mii_init(dev);
  3007. if (err)
  3008. goto err_clk_disable;
  3009. /* setup number of real queues + 1 (GENET_V1 has 0 hardware queues
  3010. * just the ring 16 descriptor based TX
  3011. */
  3012. netif_set_real_num_tx_queues(priv->dev, priv->hw_params->tx_queues + 1);
  3013. netif_set_real_num_rx_queues(priv->dev, priv->hw_params->rx_queues + 1);
  3014. /* Set default coalescing parameters */
  3015. for (i = 0; i < priv->hw_params->rx_queues; i++)
  3016. priv->rx_rings[i].rx_max_coalesced_frames = 1;
  3017. priv->rx_rings[DESC_INDEX].rx_max_coalesced_frames = 1;
  3018. /* libphy will determine the link state */
  3019. netif_carrier_off(dev);
  3020. /* Turn off the main clock, WOL clock is handled separately */
  3021. clk_disable_unprepare(priv->clk);
  3022. err = register_netdev(dev);
  3023. if (err)
  3024. goto err;
  3025. return err;
  3026. err_clk_disable:
  3027. clk_disable_unprepare(priv->clk);
  3028. err:
  3029. free_netdev(dev);
  3030. return err;
  3031. }
  3032. static int bcmgenet_remove(struct platform_device *pdev)
  3033. {
  3034. struct bcmgenet_priv *priv = dev_to_priv(&pdev->dev);
  3035. dev_set_drvdata(&pdev->dev, NULL);
  3036. unregister_netdev(priv->dev);
  3037. bcmgenet_mii_exit(priv->dev);
  3038. free_netdev(priv->dev);
  3039. return 0;
  3040. }
  3041. #ifdef CONFIG_PM_SLEEP
  3042. static int bcmgenet_suspend(struct device *d)
  3043. {
  3044. struct net_device *dev = dev_get_drvdata(d);
  3045. struct bcmgenet_priv *priv = netdev_priv(dev);
  3046. int ret = 0;
  3047. if (!netif_running(dev))
  3048. return 0;
  3049. netif_device_detach(dev);
  3050. bcmgenet_netif_stop(dev);
  3051. if (!device_may_wakeup(d))
  3052. phy_suspend(dev->phydev);
  3053. /* Prepare the device for Wake-on-LAN and switch to the slow clock */
  3054. if (device_may_wakeup(d) && priv->wolopts) {
  3055. ret = bcmgenet_power_down(priv, GENET_POWER_WOL_MAGIC);
  3056. clk_prepare_enable(priv->clk_wol);
  3057. } else if (priv->internal_phy) {
  3058. ret = bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
  3059. }
  3060. /* Turn off the clocks */
  3061. clk_disable_unprepare(priv->clk);
  3062. return ret;
  3063. }
  3064. static int bcmgenet_resume(struct device *d)
  3065. {
  3066. struct net_device *dev = dev_get_drvdata(d);
  3067. struct bcmgenet_priv *priv = netdev_priv(dev);
  3068. unsigned long dma_ctrl;
  3069. int ret;
  3070. u32 reg;
  3071. if (!netif_running(dev))
  3072. return 0;
  3073. /* Turn on the clock */
  3074. ret = clk_prepare_enable(priv->clk);
  3075. if (ret)
  3076. return ret;
  3077. /* If this is an internal GPHY, power it back on now, before UniMAC is
  3078. * brought out of reset as absolutely no UniMAC activity is allowed
  3079. */
  3080. if (priv->internal_phy)
  3081. bcmgenet_power_up(priv, GENET_POWER_PASSIVE);
  3082. bcmgenet_umac_reset(priv);
  3083. init_umac(priv);
  3084. /* From WOL-enabled suspend, switch to regular clock */
  3085. if (priv->wolopts)
  3086. clk_disable_unprepare(priv->clk_wol);
  3087. phy_init_hw(dev->phydev);
  3088. /* Speed settings must be restored */
  3089. genphy_config_aneg(dev->phydev);
  3090. bcmgenet_mii_config(priv->dev, false);
  3091. bcmgenet_set_hw_addr(priv, dev->dev_addr);
  3092. if (priv->internal_phy) {
  3093. reg = bcmgenet_ext_readl(priv, EXT_EXT_PWR_MGMT);
  3094. reg |= EXT_ENERGY_DET_MASK;
  3095. bcmgenet_ext_writel(priv, reg, EXT_EXT_PWR_MGMT);
  3096. }
  3097. if (priv->wolopts)
  3098. bcmgenet_power_up(priv, GENET_POWER_WOL_MAGIC);
  3099. /* Disable RX/TX DMA and flush TX queues */
  3100. dma_ctrl = bcmgenet_dma_disable(priv);
  3101. /* Reinitialize TDMA and RDMA and SW housekeeping */
  3102. ret = bcmgenet_init_dma(priv);
  3103. if (ret) {
  3104. netdev_err(dev, "failed to initialize DMA\n");
  3105. goto out_clk_disable;
  3106. }
  3107. /* Always enable ring 16 - descriptor ring */
  3108. bcmgenet_enable_dma(priv, dma_ctrl);
  3109. if (!device_may_wakeup(d))
  3110. phy_resume(dev->phydev);
  3111. if (priv->eee.eee_enabled)
  3112. bcmgenet_eee_enable_set(dev, true);
  3113. bcmgenet_netif_start(dev);
  3114. netif_device_attach(dev);
  3115. return 0;
  3116. out_clk_disable:
  3117. if (priv->internal_phy)
  3118. bcmgenet_power_down(priv, GENET_POWER_PASSIVE);
  3119. clk_disable_unprepare(priv->clk);
  3120. return ret;
  3121. }
  3122. #endif /* CONFIG_PM_SLEEP */
  3123. static SIMPLE_DEV_PM_OPS(bcmgenet_pm_ops, bcmgenet_suspend, bcmgenet_resume);
  3124. static struct platform_driver bcmgenet_driver = {
  3125. .probe = bcmgenet_probe,
  3126. .remove = bcmgenet_remove,
  3127. .driver = {
  3128. .name = "bcmgenet",
  3129. .of_match_table = bcmgenet_match,
  3130. .pm = &bcmgenet_pm_ops,
  3131. },
  3132. };
  3133. module_platform_driver(bcmgenet_driver);
  3134. MODULE_AUTHOR("Broadcom Corporation");
  3135. MODULE_DESCRIPTION("Broadcom GENET Ethernet controller driver");
  3136. MODULE_ALIAS("platform:bcmgenet");
  3137. MODULE_LICENSE("GPL");