bcmgenet.c 90 KB

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