bcmgenet.c 94 KB

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