bcmgenet.c 95 KB

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