bcmgenet.c 95 KB

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