bcmsysport.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Broadcom BCM7xxx System Port Ethernet MAC driver
  4. *
  5. * Copyright (C) 2014 Broadcom Corporation
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/dsa/brcm.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/of_net.h>
  18. #include <linux/of_mdio.h>
  19. #include <linux/phy.h>
  20. #include <linux/phy_fixed.h>
  21. #include <net/dsa.h>
  22. #include <linux/clk.h>
  23. #include <net/ip.h>
  24. #include <net/ipv6.h>
  25. #include "bcmsysport.h"
  26. /* I/O accessors register helpers */
  27. #define BCM_SYSPORT_IO_MACRO(name, offset) \
  28. static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off) \
  29. { \
  30. u32 reg = readl_relaxed(priv->base + offset + off); \
  31. return reg; \
  32. } \
  33. static inline void name##_writel(struct bcm_sysport_priv *priv, \
  34. u32 val, u32 off) \
  35. { \
  36. writel_relaxed(val, priv->base + offset + off); \
  37. } \
  38. BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
  39. BCM_SYSPORT_IO_MACRO(intrl2_1, SYS_PORT_INTRL2_1_OFFSET);
  40. BCM_SYSPORT_IO_MACRO(umac, SYS_PORT_UMAC_OFFSET);
  41. BCM_SYSPORT_IO_MACRO(gib, SYS_PORT_GIB_OFFSET);
  42. BCM_SYSPORT_IO_MACRO(tdma, SYS_PORT_TDMA_OFFSET);
  43. BCM_SYSPORT_IO_MACRO(rxchk, SYS_PORT_RXCHK_OFFSET);
  44. BCM_SYSPORT_IO_MACRO(txchk, SYS_PORT_TXCHK_OFFSET);
  45. BCM_SYSPORT_IO_MACRO(rbuf, SYS_PORT_RBUF_OFFSET);
  46. BCM_SYSPORT_IO_MACRO(tbuf, SYS_PORT_TBUF_OFFSET);
  47. BCM_SYSPORT_IO_MACRO(topctrl, SYS_PORT_TOPCTRL_OFFSET);
  48. /* On SYSTEMPORT Lite, any register after RDMA_STATUS has the exact
  49. * same layout, except it has been moved by 4 bytes up, *sigh*
  50. */
  51. static inline u32 rdma_readl(struct bcm_sysport_priv *priv, u32 off)
  52. {
  53. if (priv->is_lite && off >= RDMA_STATUS)
  54. off += 4;
  55. return readl_relaxed(priv->base + SYS_PORT_RDMA_OFFSET + off);
  56. }
  57. static inline void rdma_writel(struct bcm_sysport_priv *priv, u32 val, u32 off)
  58. {
  59. if (priv->is_lite && off >= RDMA_STATUS)
  60. off += 4;
  61. writel_relaxed(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
  62. }
  63. static inline u32 tdma_control_bit(struct bcm_sysport_priv *priv, u32 bit)
  64. {
  65. if (!priv->is_lite) {
  66. return BIT(bit);
  67. } else {
  68. if (bit >= ACB_ALGO)
  69. return BIT(bit + 1);
  70. else
  71. return BIT(bit);
  72. }
  73. }
  74. /* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
  75. * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
  76. */
  77. #define BCM_SYSPORT_INTR_L2(which) \
  78. static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
  79. u32 mask) \
  80. { \
  81. priv->irq##which##_mask &= ~(mask); \
  82. intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
  83. } \
  84. static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
  85. u32 mask) \
  86. { \
  87. intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
  88. priv->irq##which##_mask |= (mask); \
  89. } \
  90. BCM_SYSPORT_INTR_L2(0)
  91. BCM_SYSPORT_INTR_L2(1)
  92. /* Register accesses to GISB/RBUS registers are expensive (few hundred
  93. * nanoseconds), so keep the check for 64-bits explicit here to save
  94. * one register write per-packet on 32-bits platforms.
  95. */
  96. static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
  97. void __iomem *d,
  98. dma_addr_t addr)
  99. {
  100. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  101. writel_relaxed(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
  102. d + DESC_ADDR_HI_STATUS_LEN);
  103. #endif
  104. writel_relaxed(lower_32_bits(addr), d + DESC_ADDR_LO);
  105. }
  106. /* Ethtool operations */
  107. static void bcm_sysport_set_rx_csum(struct net_device *dev,
  108. netdev_features_t wanted)
  109. {
  110. struct bcm_sysport_priv *priv = netdev_priv(dev);
  111. u32 reg;
  112. priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
  113. reg = rxchk_readl(priv, RXCHK_CONTROL);
  114. /* Clear L2 header checks, which would prevent BPDUs
  115. * from being received.
  116. */
  117. reg &= ~RXCHK_L2_HDR_DIS;
  118. if (priv->rx_chk_en)
  119. reg |= RXCHK_EN;
  120. else
  121. reg &= ~RXCHK_EN;
  122. /* If UniMAC forwards CRC, we need to skip over it to get
  123. * a valid CHK bit to be set in the per-packet status word
  124. */
  125. if (priv->rx_chk_en && priv->crc_fwd)
  126. reg |= RXCHK_SKIP_FCS;
  127. else
  128. reg &= ~RXCHK_SKIP_FCS;
  129. /* If Broadcom tags are enabled (e.g: using a switch), make
  130. * sure we tell the RXCHK hardware to expect a 4-bytes Broadcom
  131. * tag after the Ethernet MAC Source Address.
  132. */
  133. if (netdev_uses_dsa(dev))
  134. reg |= RXCHK_BRCM_TAG_EN;
  135. else
  136. reg &= ~RXCHK_BRCM_TAG_EN;
  137. rxchk_writel(priv, reg, RXCHK_CONTROL);
  138. }
  139. static void bcm_sysport_set_tx_csum(struct net_device *dev,
  140. netdev_features_t wanted)
  141. {
  142. struct bcm_sysport_priv *priv = netdev_priv(dev);
  143. u32 reg;
  144. /* Hardware transmit checksum requires us to enable the Transmit status
  145. * block prepended to the packet contents
  146. */
  147. priv->tsb_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  148. NETIF_F_HW_VLAN_CTAG_TX));
  149. reg = tdma_readl(priv, TDMA_CONTROL);
  150. if (priv->tsb_en)
  151. reg |= tdma_control_bit(priv, TSB_EN);
  152. else
  153. reg &= ~tdma_control_bit(priv, TSB_EN);
  154. /* Indicating that software inserts Broadcom tags is needed for the TX
  155. * checksum to be computed correctly when using VLAN HW acceleration,
  156. * else it has no effect, so it can always be turned on.
  157. */
  158. if (netdev_uses_dsa(dev))
  159. reg |= tdma_control_bit(priv, SW_BRCM_TAG);
  160. else
  161. reg &= ~tdma_control_bit(priv, SW_BRCM_TAG);
  162. tdma_writel(priv, reg, TDMA_CONTROL);
  163. /* Default TPID is ETH_P_8021AD, change to ETH_P_8021Q */
  164. if (wanted & NETIF_F_HW_VLAN_CTAG_TX)
  165. tdma_writel(priv, ETH_P_8021Q, TDMA_TPID);
  166. }
  167. static int bcm_sysport_set_features(struct net_device *dev,
  168. netdev_features_t features)
  169. {
  170. struct bcm_sysport_priv *priv = netdev_priv(dev);
  171. int ret;
  172. ret = clk_prepare_enable(priv->clk);
  173. if (ret)
  174. return ret;
  175. /* Read CRC forward */
  176. if (!priv->is_lite)
  177. priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
  178. else
  179. priv->crc_fwd = !((gib_readl(priv, GIB_CONTROL) &
  180. GIB_FCS_STRIP) >> GIB_FCS_STRIP_SHIFT);
  181. bcm_sysport_set_rx_csum(dev, features);
  182. bcm_sysport_set_tx_csum(dev, features);
  183. clk_disable_unprepare(priv->clk);
  184. return 0;
  185. }
  186. /* Hardware counters must be kept in sync because the order/offset
  187. * is important here (order in structure declaration = order in hardware)
  188. */
  189. static const struct bcm_sysport_stats bcm_sysport_gstrings_stats[] = {
  190. /* general stats */
  191. STAT_NETDEV64(rx_packets),
  192. STAT_NETDEV64(tx_packets),
  193. STAT_NETDEV64(rx_bytes),
  194. STAT_NETDEV64(tx_bytes),
  195. STAT_NETDEV(rx_errors),
  196. STAT_NETDEV(tx_errors),
  197. STAT_NETDEV(rx_dropped),
  198. STAT_NETDEV(tx_dropped),
  199. STAT_NETDEV(multicast),
  200. /* UniMAC RSV counters */
  201. STAT_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
  202. STAT_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
  203. STAT_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
  204. STAT_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
  205. STAT_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
  206. STAT_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
  207. STAT_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
  208. STAT_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
  209. STAT_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
  210. STAT_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
  211. STAT_MIB_RX("rx_pkts", mib.rx.pkt),
  212. STAT_MIB_RX("rx_bytes", mib.rx.bytes),
  213. STAT_MIB_RX("rx_multicast", mib.rx.mca),
  214. STAT_MIB_RX("rx_broadcast", mib.rx.bca),
  215. STAT_MIB_RX("rx_fcs", mib.rx.fcs),
  216. STAT_MIB_RX("rx_control", mib.rx.cf),
  217. STAT_MIB_RX("rx_pause", mib.rx.pf),
  218. STAT_MIB_RX("rx_unknown", mib.rx.uo),
  219. STAT_MIB_RX("rx_align", mib.rx.aln),
  220. STAT_MIB_RX("rx_outrange", mib.rx.flr),
  221. STAT_MIB_RX("rx_code", mib.rx.cde),
  222. STAT_MIB_RX("rx_carrier", mib.rx.fcr),
  223. STAT_MIB_RX("rx_oversize", mib.rx.ovr),
  224. STAT_MIB_RX("rx_jabber", mib.rx.jbr),
  225. STAT_MIB_RX("rx_mtu_err", mib.rx.mtue),
  226. STAT_MIB_RX("rx_good_pkts", mib.rx.pok),
  227. STAT_MIB_RX("rx_unicast", mib.rx.uc),
  228. STAT_MIB_RX("rx_ppp", mib.rx.ppp),
  229. STAT_MIB_RX("rx_crc", mib.rx.rcrc),
  230. /* UniMAC TSV counters */
  231. STAT_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
  232. STAT_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
  233. STAT_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
  234. STAT_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
  235. STAT_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
  236. STAT_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
  237. STAT_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
  238. STAT_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
  239. STAT_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
  240. STAT_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
  241. STAT_MIB_TX("tx_pkts", mib.tx.pkts),
  242. STAT_MIB_TX("tx_multicast", mib.tx.mca),
  243. STAT_MIB_TX("tx_broadcast", mib.tx.bca),
  244. STAT_MIB_TX("tx_pause", mib.tx.pf),
  245. STAT_MIB_TX("tx_control", mib.tx.cf),
  246. STAT_MIB_TX("tx_fcs_err", mib.tx.fcs),
  247. STAT_MIB_TX("tx_oversize", mib.tx.ovr),
  248. STAT_MIB_TX("tx_defer", mib.tx.drf),
  249. STAT_MIB_TX("tx_excess_defer", mib.tx.edf),
  250. STAT_MIB_TX("tx_single_col", mib.tx.scl),
  251. STAT_MIB_TX("tx_multi_col", mib.tx.mcl),
  252. STAT_MIB_TX("tx_late_col", mib.tx.lcl),
  253. STAT_MIB_TX("tx_excess_col", mib.tx.ecl),
  254. STAT_MIB_TX("tx_frags", mib.tx.frg),
  255. STAT_MIB_TX("tx_total_col", mib.tx.ncl),
  256. STAT_MIB_TX("tx_jabber", mib.tx.jbr),
  257. STAT_MIB_TX("tx_bytes", mib.tx.bytes),
  258. STAT_MIB_TX("tx_good_pkts", mib.tx.pok),
  259. STAT_MIB_TX("tx_unicast", mib.tx.uc),
  260. /* UniMAC RUNT counters */
  261. STAT_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
  262. STAT_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
  263. STAT_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
  264. STAT_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
  265. /* RXCHK misc statistics */
  266. STAT_RXCHK("rxchk_bad_csum", mib.rxchk_bad_csum, RXCHK_BAD_CSUM_CNTR),
  267. STAT_RXCHK("rxchk_other_pkt_disc", mib.rxchk_other_pkt_disc,
  268. RXCHK_OTHER_DISC_CNTR),
  269. /* RBUF misc statistics */
  270. STAT_RBUF("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, RBUF_OVFL_DISC_CNTR),
  271. STAT_RBUF("rbuf_err_cnt", mib.rbuf_err_cnt, RBUF_ERR_PKT_CNTR),
  272. /* RDMA misc statistics */
  273. STAT_RDMA("rdma_ovflow_cnt", mib.rdma_ovflow_cnt, RDMA_OVFL_DISC_CNTR),
  274. STAT_MIB_SOFT("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
  275. STAT_MIB_SOFT("rx_dma_failed", mib.rx_dma_failed),
  276. STAT_MIB_SOFT("tx_dma_failed", mib.tx_dma_failed),
  277. STAT_MIB_SOFT("tx_realloc_tsb", mib.tx_realloc_tsb),
  278. STAT_MIB_SOFT("tx_realloc_tsb_failed", mib.tx_realloc_tsb_failed),
  279. /* Per TX-queue statistics are dynamically appended */
  280. };
  281. #define BCM_SYSPORT_STATS_LEN ARRAY_SIZE(bcm_sysport_gstrings_stats)
  282. static void bcm_sysport_get_drvinfo(struct net_device *dev,
  283. struct ethtool_drvinfo *info)
  284. {
  285. strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  286. strscpy(info->bus_info, "platform", sizeof(info->bus_info));
  287. }
  288. static u32 bcm_sysport_get_msglvl(struct net_device *dev)
  289. {
  290. struct bcm_sysport_priv *priv = netdev_priv(dev);
  291. return priv->msg_enable;
  292. }
  293. static void bcm_sysport_set_msglvl(struct net_device *dev, u32 enable)
  294. {
  295. struct bcm_sysport_priv *priv = netdev_priv(dev);
  296. priv->msg_enable = enable;
  297. }
  298. static inline bool bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type)
  299. {
  300. switch (type) {
  301. case BCM_SYSPORT_STAT_NETDEV:
  302. case BCM_SYSPORT_STAT_NETDEV64:
  303. case BCM_SYSPORT_STAT_RXCHK:
  304. case BCM_SYSPORT_STAT_RBUF:
  305. case BCM_SYSPORT_STAT_RDMA:
  306. case BCM_SYSPORT_STAT_SOFT:
  307. return true;
  308. default:
  309. return false;
  310. }
  311. }
  312. static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
  313. {
  314. struct bcm_sysport_priv *priv = netdev_priv(dev);
  315. const struct bcm_sysport_stats *s;
  316. unsigned int i, j;
  317. switch (string_set) {
  318. case ETH_SS_STATS:
  319. for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  320. s = &bcm_sysport_gstrings_stats[i];
  321. if (priv->is_lite &&
  322. !bcm_sysport_lite_stat_valid(s->type))
  323. continue;
  324. j++;
  325. }
  326. /* Include per-queue statistics */
  327. return j + dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
  328. default:
  329. return -EOPNOTSUPP;
  330. }
  331. }
  332. static void bcm_sysport_get_strings(struct net_device *dev,
  333. u32 stringset, u8 *data)
  334. {
  335. struct bcm_sysport_priv *priv = netdev_priv(dev);
  336. const struct bcm_sysport_stats *s;
  337. char buf[128];
  338. int i, j;
  339. switch (stringset) {
  340. case ETH_SS_STATS:
  341. for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  342. s = &bcm_sysport_gstrings_stats[i];
  343. if (priv->is_lite &&
  344. !bcm_sysport_lite_stat_valid(s->type))
  345. continue;
  346. memcpy(data + j * ETH_GSTRING_LEN, s->stat_string,
  347. ETH_GSTRING_LEN);
  348. j++;
  349. }
  350. for (i = 0; i < dev->num_tx_queues; i++) {
  351. snprintf(buf, sizeof(buf), "txq%d_packets", i);
  352. memcpy(data + j * ETH_GSTRING_LEN, buf,
  353. ETH_GSTRING_LEN);
  354. j++;
  355. snprintf(buf, sizeof(buf), "txq%d_bytes", i);
  356. memcpy(data + j * ETH_GSTRING_LEN, buf,
  357. ETH_GSTRING_LEN);
  358. j++;
  359. }
  360. break;
  361. default:
  362. break;
  363. }
  364. }
  365. static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv *priv)
  366. {
  367. int i, j = 0;
  368. for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  369. const struct bcm_sysport_stats *s;
  370. u8 offset = 0;
  371. u32 val = 0;
  372. char *p;
  373. s = &bcm_sysport_gstrings_stats[i];
  374. switch (s->type) {
  375. case BCM_SYSPORT_STAT_NETDEV:
  376. case BCM_SYSPORT_STAT_NETDEV64:
  377. case BCM_SYSPORT_STAT_SOFT:
  378. continue;
  379. case BCM_SYSPORT_STAT_MIB_RX:
  380. case BCM_SYSPORT_STAT_MIB_TX:
  381. case BCM_SYSPORT_STAT_RUNT:
  382. if (priv->is_lite)
  383. continue;
  384. if (s->type != BCM_SYSPORT_STAT_MIB_RX)
  385. offset = UMAC_MIB_STAT_OFFSET;
  386. val = umac_readl(priv, UMAC_MIB_START + j + offset);
  387. break;
  388. case BCM_SYSPORT_STAT_RXCHK:
  389. val = rxchk_readl(priv, s->reg_offset);
  390. if (val == ~0)
  391. rxchk_writel(priv, 0, s->reg_offset);
  392. break;
  393. case BCM_SYSPORT_STAT_RBUF:
  394. val = rbuf_readl(priv, s->reg_offset);
  395. if (val == ~0)
  396. rbuf_writel(priv, 0, s->reg_offset);
  397. break;
  398. case BCM_SYSPORT_STAT_RDMA:
  399. if (!priv->is_lite)
  400. continue;
  401. val = rdma_readl(priv, s->reg_offset);
  402. if (val == ~0)
  403. rdma_writel(priv, 0, s->reg_offset);
  404. break;
  405. }
  406. j += s->stat_sizeof;
  407. p = (char *)priv + s->stat_offset;
  408. *(u32 *)p = val;
  409. }
  410. netif_dbg(priv, hw, priv->netdev, "updated MIB counters\n");
  411. }
  412. static void bcm_sysport_update_tx_stats(struct bcm_sysport_priv *priv,
  413. u64 *tx_bytes, u64 *tx_packets)
  414. {
  415. struct bcm_sysport_tx_ring *ring;
  416. u64 bytes = 0, packets = 0;
  417. unsigned int start;
  418. unsigned int q;
  419. for (q = 0; q < priv->netdev->num_tx_queues; q++) {
  420. ring = &priv->tx_rings[q];
  421. do {
  422. start = u64_stats_fetch_begin(&priv->syncp);
  423. bytes = ring->bytes;
  424. packets = ring->packets;
  425. } while (u64_stats_fetch_retry(&priv->syncp, start));
  426. *tx_bytes += bytes;
  427. *tx_packets += packets;
  428. }
  429. }
  430. static void bcm_sysport_get_stats(struct net_device *dev,
  431. struct ethtool_stats *stats, u64 *data)
  432. {
  433. struct bcm_sysport_priv *priv = netdev_priv(dev);
  434. struct bcm_sysport_stats64 *stats64 = &priv->stats64;
  435. struct u64_stats_sync *syncp = &priv->syncp;
  436. struct bcm_sysport_tx_ring *ring;
  437. u64 tx_bytes = 0, tx_packets = 0;
  438. unsigned int start;
  439. int i, j;
  440. if (netif_running(dev)) {
  441. bcm_sysport_update_mib_counters(priv);
  442. bcm_sysport_update_tx_stats(priv, &tx_bytes, &tx_packets);
  443. stats64->tx_bytes = tx_bytes;
  444. stats64->tx_packets = tx_packets;
  445. }
  446. for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  447. const struct bcm_sysport_stats *s;
  448. char *p;
  449. s = &bcm_sysport_gstrings_stats[i];
  450. if (s->type == BCM_SYSPORT_STAT_NETDEV)
  451. p = (char *)&dev->stats;
  452. else if (s->type == BCM_SYSPORT_STAT_NETDEV64)
  453. p = (char *)stats64;
  454. else
  455. p = (char *)priv;
  456. if (priv->is_lite && !bcm_sysport_lite_stat_valid(s->type))
  457. continue;
  458. p += s->stat_offset;
  459. if (s->stat_sizeof == sizeof(u64) &&
  460. s->type == BCM_SYSPORT_STAT_NETDEV64) {
  461. do {
  462. start = u64_stats_fetch_begin(syncp);
  463. data[i] = *(u64 *)p;
  464. } while (u64_stats_fetch_retry(syncp, start));
  465. } else
  466. data[i] = *(u32 *)p;
  467. j++;
  468. }
  469. /* For SYSTEMPORT Lite since we have holes in our statistics, j would
  470. * be equal to BCM_SYSPORT_STATS_LEN at the end of the loop, but it
  471. * needs to point to how many total statistics we have minus the
  472. * number of per TX queue statistics
  473. */
  474. j = bcm_sysport_get_sset_count(dev, ETH_SS_STATS) -
  475. dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
  476. for (i = 0; i < dev->num_tx_queues; i++) {
  477. ring = &priv->tx_rings[i];
  478. data[j] = ring->packets;
  479. j++;
  480. data[j] = ring->bytes;
  481. j++;
  482. }
  483. }
  484. static void bcm_sysport_get_wol(struct net_device *dev,
  485. struct ethtool_wolinfo *wol)
  486. {
  487. struct bcm_sysport_priv *priv = netdev_priv(dev);
  488. wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
  489. wol->wolopts = priv->wolopts;
  490. if (!(priv->wolopts & WAKE_MAGICSECURE))
  491. return;
  492. memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
  493. }
  494. static int bcm_sysport_set_wol(struct net_device *dev,
  495. struct ethtool_wolinfo *wol)
  496. {
  497. struct bcm_sysport_priv *priv = netdev_priv(dev);
  498. struct device *kdev = &priv->pdev->dev;
  499. u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
  500. if (!device_can_wakeup(kdev))
  501. return -ENOTSUPP;
  502. if (wol->wolopts & ~supported)
  503. return -EINVAL;
  504. if (wol->wolopts & WAKE_MAGICSECURE)
  505. memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
  506. /* Flag the device and relevant IRQ as wakeup capable */
  507. if (wol->wolopts) {
  508. device_set_wakeup_enable(kdev, 1);
  509. if (priv->wol_irq_disabled)
  510. enable_irq_wake(priv->wol_irq);
  511. priv->wol_irq_disabled = 0;
  512. } else {
  513. device_set_wakeup_enable(kdev, 0);
  514. /* Avoid unbalanced disable_irq_wake calls */
  515. if (!priv->wol_irq_disabled)
  516. disable_irq_wake(priv->wol_irq);
  517. priv->wol_irq_disabled = 1;
  518. }
  519. priv->wolopts = wol->wolopts;
  520. return 0;
  521. }
  522. static void bcm_sysport_set_rx_coalesce(struct bcm_sysport_priv *priv,
  523. u32 usecs, u32 pkts)
  524. {
  525. u32 reg;
  526. reg = rdma_readl(priv, RDMA_MBDONE_INTR);
  527. reg &= ~(RDMA_INTR_THRESH_MASK |
  528. RDMA_TIMEOUT_MASK << RDMA_TIMEOUT_SHIFT);
  529. reg |= pkts;
  530. reg |= DIV_ROUND_UP(usecs * 1000, 8192) << RDMA_TIMEOUT_SHIFT;
  531. rdma_writel(priv, reg, RDMA_MBDONE_INTR);
  532. }
  533. static void bcm_sysport_set_tx_coalesce(struct bcm_sysport_tx_ring *ring,
  534. struct ethtool_coalesce *ec)
  535. {
  536. struct bcm_sysport_priv *priv = ring->priv;
  537. u32 reg;
  538. reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(ring->index));
  539. reg &= ~(RING_INTR_THRESH_MASK |
  540. RING_TIMEOUT_MASK << RING_TIMEOUT_SHIFT);
  541. reg |= ec->tx_max_coalesced_frames;
  542. reg |= DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000, 8192) <<
  543. RING_TIMEOUT_SHIFT;
  544. tdma_writel(priv, reg, TDMA_DESC_RING_INTR_CONTROL(ring->index));
  545. }
  546. static int bcm_sysport_get_coalesce(struct net_device *dev,
  547. struct ethtool_coalesce *ec,
  548. struct kernel_ethtool_coalesce *kernel_coal,
  549. struct netlink_ext_ack *extack)
  550. {
  551. struct bcm_sysport_priv *priv = netdev_priv(dev);
  552. u32 reg;
  553. reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(0));
  554. ec->tx_coalesce_usecs = (reg >> RING_TIMEOUT_SHIFT) * 8192 / 1000;
  555. ec->tx_max_coalesced_frames = reg & RING_INTR_THRESH_MASK;
  556. reg = rdma_readl(priv, RDMA_MBDONE_INTR);
  557. ec->rx_coalesce_usecs = (reg >> RDMA_TIMEOUT_SHIFT) * 8192 / 1000;
  558. ec->rx_max_coalesced_frames = reg & RDMA_INTR_THRESH_MASK;
  559. ec->use_adaptive_rx_coalesce = priv->dim.use_dim;
  560. return 0;
  561. }
  562. static int bcm_sysport_set_coalesce(struct net_device *dev,
  563. struct ethtool_coalesce *ec,
  564. struct kernel_ethtool_coalesce *kernel_coal,
  565. struct netlink_ext_ack *extack)
  566. {
  567. struct bcm_sysport_priv *priv = netdev_priv(dev);
  568. struct dim_cq_moder moder;
  569. u32 usecs, pkts;
  570. unsigned int i;
  571. /* Base system clock is 125Mhz, DMA timeout is this reference clock
  572. * divided by 1024, which yield roughly 8.192 us, our maximum value has
  573. * to fit in the RING_TIMEOUT_MASK (16 bits).
  574. */
  575. if (ec->tx_max_coalesced_frames > RING_INTR_THRESH_MASK ||
  576. ec->tx_coalesce_usecs > (RING_TIMEOUT_MASK * 8) + 1 ||
  577. ec->rx_max_coalesced_frames > RDMA_INTR_THRESH_MASK ||
  578. ec->rx_coalesce_usecs > (RDMA_TIMEOUT_MASK * 8) + 1)
  579. return -EINVAL;
  580. if ((ec->tx_coalesce_usecs == 0 && ec->tx_max_coalesced_frames == 0) ||
  581. (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0))
  582. return -EINVAL;
  583. for (i = 0; i < dev->num_tx_queues; i++)
  584. bcm_sysport_set_tx_coalesce(&priv->tx_rings[i], ec);
  585. priv->rx_coalesce_usecs = ec->rx_coalesce_usecs;
  586. priv->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  587. usecs = priv->rx_coalesce_usecs;
  588. pkts = priv->rx_max_coalesced_frames;
  589. if (ec->use_adaptive_rx_coalesce && !priv->dim.use_dim) {
  590. moder = net_dim_get_def_rx_moderation(priv->dim.dim.mode);
  591. usecs = moder.usec;
  592. pkts = moder.pkts;
  593. }
  594. priv->dim.use_dim = ec->use_adaptive_rx_coalesce;
  595. /* Apply desired coalescing parameters */
  596. bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
  597. return 0;
  598. }
  599. static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
  600. {
  601. dev_consume_skb_any(cb->skb);
  602. cb->skb = NULL;
  603. dma_unmap_addr_set(cb, dma_addr, 0);
  604. }
  605. static struct sk_buff *bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
  606. struct bcm_sysport_cb *cb)
  607. {
  608. struct device *kdev = &priv->pdev->dev;
  609. struct net_device *ndev = priv->netdev;
  610. struct sk_buff *skb, *rx_skb;
  611. dma_addr_t mapping;
  612. /* Allocate a new SKB for a new packet */
  613. skb = __netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH,
  614. GFP_ATOMIC | __GFP_NOWARN);
  615. if (!skb) {
  616. priv->mib.alloc_rx_buff_failed++;
  617. netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
  618. return NULL;
  619. }
  620. mapping = dma_map_single(kdev, skb->data,
  621. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  622. if (dma_mapping_error(kdev, mapping)) {
  623. priv->mib.rx_dma_failed++;
  624. dev_kfree_skb_any(skb);
  625. netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
  626. return NULL;
  627. }
  628. /* Grab the current SKB on the ring */
  629. rx_skb = cb->skb;
  630. if (likely(rx_skb))
  631. dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
  632. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  633. /* Put the new SKB on the ring */
  634. cb->skb = skb;
  635. dma_unmap_addr_set(cb, dma_addr, mapping);
  636. dma_desc_set_addr(priv, cb->bd_addr, mapping);
  637. netif_dbg(priv, rx_status, ndev, "RX refill\n");
  638. /* Return the current SKB to the caller */
  639. return rx_skb;
  640. }
  641. static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
  642. {
  643. struct bcm_sysport_cb *cb;
  644. struct sk_buff *skb;
  645. unsigned int i;
  646. for (i = 0; i < priv->num_rx_bds; i++) {
  647. cb = &priv->rx_cbs[i];
  648. skb = bcm_sysport_rx_refill(priv, cb);
  649. dev_kfree_skb(skb);
  650. if (!cb->skb)
  651. return -ENOMEM;
  652. }
  653. return 0;
  654. }
  655. /* Poll the hardware for up to budget packets to process */
  656. static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
  657. unsigned int budget)
  658. {
  659. struct bcm_sysport_stats64 *stats64 = &priv->stats64;
  660. struct net_device *ndev = priv->netdev;
  661. unsigned int processed = 0, to_process;
  662. unsigned int processed_bytes = 0;
  663. struct bcm_sysport_cb *cb;
  664. struct sk_buff *skb;
  665. unsigned int p_index;
  666. u16 len, status;
  667. struct bcm_rsb *rsb;
  668. /* Clear status before servicing to reduce spurious interrupts */
  669. intrl2_0_writel(priv, INTRL2_0_RDMA_MBDONE, INTRL2_CPU_CLEAR);
  670. /* Determine how much we should process since last call, SYSTEMPORT Lite
  671. * groups the producer and consumer indexes into the same 32-bit
  672. * which we access using RDMA_CONS_INDEX
  673. */
  674. if (!priv->is_lite)
  675. p_index = rdma_readl(priv, RDMA_PROD_INDEX);
  676. else
  677. p_index = rdma_readl(priv, RDMA_CONS_INDEX);
  678. p_index &= RDMA_PROD_INDEX_MASK;
  679. to_process = (p_index - priv->rx_c_index) & RDMA_CONS_INDEX_MASK;
  680. netif_dbg(priv, rx_status, ndev,
  681. "p_index=%d rx_c_index=%d to_process=%d\n",
  682. p_index, priv->rx_c_index, to_process);
  683. while ((processed < to_process) && (processed < budget)) {
  684. cb = &priv->rx_cbs[priv->rx_read_ptr];
  685. skb = bcm_sysport_rx_refill(priv, cb);
  686. /* We do not have a backing SKB, so we do not a corresponding
  687. * DMA mapping for this incoming packet since
  688. * bcm_sysport_rx_refill always either has both skb and mapping
  689. * or none.
  690. */
  691. if (unlikely(!skb)) {
  692. netif_err(priv, rx_err, ndev, "out of memory!\n");
  693. ndev->stats.rx_dropped++;
  694. ndev->stats.rx_errors++;
  695. goto next;
  696. }
  697. /* Extract the Receive Status Block prepended */
  698. rsb = (struct bcm_rsb *)skb->data;
  699. len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
  700. status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
  701. DESC_STATUS_MASK;
  702. netif_dbg(priv, rx_status, ndev,
  703. "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
  704. p_index, priv->rx_c_index, priv->rx_read_ptr,
  705. len, status);
  706. if (unlikely(len > RX_BUF_LENGTH)) {
  707. netif_err(priv, rx_status, ndev, "oversized packet\n");
  708. ndev->stats.rx_length_errors++;
  709. ndev->stats.rx_errors++;
  710. dev_kfree_skb_any(skb);
  711. goto next;
  712. }
  713. if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
  714. netif_err(priv, rx_status, ndev, "fragmented packet!\n");
  715. ndev->stats.rx_dropped++;
  716. ndev->stats.rx_errors++;
  717. dev_kfree_skb_any(skb);
  718. goto next;
  719. }
  720. if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
  721. netif_err(priv, rx_err, ndev, "error packet\n");
  722. if (status & RX_STATUS_OVFLOW)
  723. ndev->stats.rx_over_errors++;
  724. ndev->stats.rx_dropped++;
  725. ndev->stats.rx_errors++;
  726. dev_kfree_skb_any(skb);
  727. goto next;
  728. }
  729. skb_put(skb, len);
  730. /* Hardware validated our checksum */
  731. if (likely(status & DESC_L4_CSUM))
  732. skb->ip_summed = CHECKSUM_UNNECESSARY;
  733. /* Hardware pre-pends packets with 2bytes before Ethernet
  734. * header plus we have the Receive Status Block, strip off all
  735. * of this from the SKB.
  736. */
  737. skb_pull(skb, sizeof(*rsb) + 2);
  738. len -= (sizeof(*rsb) + 2);
  739. processed_bytes += len;
  740. /* UniMAC may forward CRC */
  741. if (priv->crc_fwd) {
  742. skb_trim(skb, len - ETH_FCS_LEN);
  743. len -= ETH_FCS_LEN;
  744. }
  745. skb->protocol = eth_type_trans(skb, ndev);
  746. ndev->stats.rx_packets++;
  747. ndev->stats.rx_bytes += len;
  748. u64_stats_update_begin(&priv->syncp);
  749. stats64->rx_packets++;
  750. stats64->rx_bytes += len;
  751. u64_stats_update_end(&priv->syncp);
  752. napi_gro_receive(&priv->napi, skb);
  753. next:
  754. processed++;
  755. priv->rx_read_ptr++;
  756. if (priv->rx_read_ptr == priv->num_rx_bds)
  757. priv->rx_read_ptr = 0;
  758. }
  759. priv->dim.packets = processed;
  760. priv->dim.bytes = processed_bytes;
  761. return processed;
  762. }
  763. static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_tx_ring *ring,
  764. struct bcm_sysport_cb *cb,
  765. unsigned int *bytes_compl,
  766. unsigned int *pkts_compl)
  767. {
  768. struct bcm_sysport_priv *priv = ring->priv;
  769. struct device *kdev = &priv->pdev->dev;
  770. if (cb->skb) {
  771. *bytes_compl += cb->skb->len;
  772. dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
  773. dma_unmap_len(cb, dma_len),
  774. DMA_TO_DEVICE);
  775. (*pkts_compl)++;
  776. bcm_sysport_free_cb(cb);
  777. /* SKB fragment */
  778. } else if (dma_unmap_addr(cb, dma_addr)) {
  779. *bytes_compl += dma_unmap_len(cb, dma_len);
  780. dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
  781. dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
  782. dma_unmap_addr_set(cb, dma_addr, 0);
  783. }
  784. }
  785. /* Reclaim queued SKBs for transmission completion, lockless version */
  786. static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
  787. struct bcm_sysport_tx_ring *ring)
  788. {
  789. unsigned int pkts_compl = 0, bytes_compl = 0;
  790. struct net_device *ndev = priv->netdev;
  791. unsigned int txbds_processed = 0;
  792. struct bcm_sysport_cb *cb;
  793. unsigned int txbds_ready;
  794. unsigned int c_index;
  795. u32 hw_ind;
  796. /* Clear status before servicing to reduce spurious interrupts */
  797. if (!ring->priv->is_lite)
  798. intrl2_1_writel(ring->priv, BIT(ring->index), INTRL2_CPU_CLEAR);
  799. else
  800. intrl2_0_writel(ring->priv, BIT(ring->index +
  801. INTRL2_0_TDMA_MBDONE_SHIFT), INTRL2_CPU_CLEAR);
  802. /* Compute how many descriptors have been processed since last call */
  803. hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
  804. c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
  805. txbds_ready = (c_index - ring->c_index) & RING_CONS_INDEX_MASK;
  806. netif_dbg(priv, tx_done, ndev,
  807. "ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
  808. ring->index, ring->c_index, c_index, txbds_ready);
  809. while (txbds_processed < txbds_ready) {
  810. cb = &ring->cbs[ring->clean_index];
  811. bcm_sysport_tx_reclaim_one(ring, cb, &bytes_compl, &pkts_compl);
  812. ring->desc_count++;
  813. txbds_processed++;
  814. if (likely(ring->clean_index < ring->size - 1))
  815. ring->clean_index++;
  816. else
  817. ring->clean_index = 0;
  818. }
  819. u64_stats_update_begin(&priv->syncp);
  820. ring->packets += pkts_compl;
  821. ring->bytes += bytes_compl;
  822. u64_stats_update_end(&priv->syncp);
  823. ring->c_index = c_index;
  824. netif_dbg(priv, tx_done, ndev,
  825. "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
  826. ring->index, ring->c_index, pkts_compl, bytes_compl);
  827. return pkts_compl;
  828. }
  829. /* Locked version of the per-ring TX reclaim routine */
  830. static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
  831. struct bcm_sysport_tx_ring *ring)
  832. {
  833. struct netdev_queue *txq;
  834. unsigned int released;
  835. unsigned long flags;
  836. txq = netdev_get_tx_queue(priv->netdev, ring->index);
  837. spin_lock_irqsave(&ring->lock, flags);
  838. released = __bcm_sysport_tx_reclaim(priv, ring);
  839. if (released)
  840. netif_tx_wake_queue(txq);
  841. spin_unlock_irqrestore(&ring->lock, flags);
  842. return released;
  843. }
  844. /* Locked version of the per-ring TX reclaim, but does not wake the queue */
  845. static void bcm_sysport_tx_clean(struct bcm_sysport_priv *priv,
  846. struct bcm_sysport_tx_ring *ring)
  847. {
  848. unsigned long flags;
  849. spin_lock_irqsave(&ring->lock, flags);
  850. __bcm_sysport_tx_reclaim(priv, ring);
  851. spin_unlock_irqrestore(&ring->lock, flags);
  852. }
  853. static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
  854. {
  855. struct bcm_sysport_tx_ring *ring =
  856. container_of(napi, struct bcm_sysport_tx_ring, napi);
  857. unsigned int work_done = 0;
  858. work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
  859. if (work_done == 0) {
  860. napi_complete(napi);
  861. /* re-enable TX interrupt */
  862. if (!ring->priv->is_lite)
  863. intrl2_1_mask_clear(ring->priv, BIT(ring->index));
  864. else
  865. intrl2_0_mask_clear(ring->priv, BIT(ring->index +
  866. INTRL2_0_TDMA_MBDONE_SHIFT));
  867. return 0;
  868. }
  869. return budget;
  870. }
  871. static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
  872. {
  873. unsigned int q;
  874. for (q = 0; q < priv->netdev->num_tx_queues; q++)
  875. bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
  876. }
  877. static int bcm_sysport_poll(struct napi_struct *napi, int budget)
  878. {
  879. struct bcm_sysport_priv *priv =
  880. container_of(napi, struct bcm_sysport_priv, napi);
  881. struct dim_sample dim_sample = {};
  882. unsigned int work_done = 0;
  883. work_done = bcm_sysport_desc_rx(priv, budget);
  884. priv->rx_c_index += work_done;
  885. priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
  886. /* SYSTEMPORT Lite groups the producer/consumer index, producer is
  887. * maintained by HW, but writes to it will be ignore while RDMA
  888. * is active
  889. */
  890. if (!priv->is_lite)
  891. rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
  892. else
  893. rdma_writel(priv, priv->rx_c_index << 16, RDMA_CONS_INDEX);
  894. if (work_done < budget) {
  895. napi_complete_done(napi, work_done);
  896. /* re-enable RX interrupts */
  897. intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
  898. }
  899. if (priv->dim.use_dim) {
  900. dim_update_sample(priv->dim.event_ctr, priv->dim.packets,
  901. priv->dim.bytes, &dim_sample);
  902. net_dim(&priv->dim.dim, dim_sample);
  903. }
  904. return work_done;
  905. }
  906. static void mpd_enable_set(struct bcm_sysport_priv *priv, bool enable)
  907. {
  908. u32 reg, bit;
  909. reg = umac_readl(priv, UMAC_MPD_CTRL);
  910. if (enable)
  911. reg |= MPD_EN;
  912. else
  913. reg &= ~MPD_EN;
  914. umac_writel(priv, reg, UMAC_MPD_CTRL);
  915. if (priv->is_lite)
  916. bit = RBUF_ACPI_EN_LITE;
  917. else
  918. bit = RBUF_ACPI_EN;
  919. reg = rbuf_readl(priv, RBUF_CONTROL);
  920. if (enable)
  921. reg |= bit;
  922. else
  923. reg &= ~bit;
  924. rbuf_writel(priv, reg, RBUF_CONTROL);
  925. }
  926. static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
  927. {
  928. unsigned int index;
  929. u32 reg;
  930. /* Disable RXCHK, active filters and Broadcom tag matching */
  931. reg = rxchk_readl(priv, RXCHK_CONTROL);
  932. reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
  933. RXCHK_BRCM_TAG_MATCH_SHIFT | RXCHK_EN | RXCHK_BRCM_TAG_EN);
  934. rxchk_writel(priv, reg, RXCHK_CONTROL);
  935. /* Make sure we restore correct CID index in case HW lost
  936. * its context during deep idle state
  937. */
  938. for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
  939. rxchk_writel(priv, priv->filters_loc[index] <<
  940. RXCHK_BRCM_TAG_CID_SHIFT, RXCHK_BRCM_TAG(index));
  941. rxchk_writel(priv, 0xff00ffff, RXCHK_BRCM_TAG_MASK(index));
  942. }
  943. /* Clear the MagicPacket detection logic */
  944. mpd_enable_set(priv, false);
  945. reg = intrl2_0_readl(priv, INTRL2_CPU_STATUS);
  946. if (reg & INTRL2_0_MPD)
  947. netdev_info(priv->netdev, "Wake-on-LAN (MPD) interrupt!\n");
  948. if (reg & INTRL2_0_BRCM_MATCH_TAG) {
  949. reg = rxchk_readl(priv, RXCHK_BRCM_TAG_MATCH_STATUS) &
  950. RXCHK_BRCM_TAG_MATCH_MASK;
  951. netdev_info(priv->netdev,
  952. "Wake-on-LAN (filters 0x%02x) interrupt!\n", reg);
  953. }
  954. netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
  955. }
  956. static void bcm_sysport_dim_work(struct work_struct *work)
  957. {
  958. struct dim *dim = container_of(work, struct dim, work);
  959. struct bcm_sysport_net_dim *ndim =
  960. container_of(dim, struct bcm_sysport_net_dim, dim);
  961. struct bcm_sysport_priv *priv =
  962. container_of(ndim, struct bcm_sysport_priv, dim);
  963. struct dim_cq_moder cur_profile = net_dim_get_rx_moderation(dim->mode,
  964. dim->profile_ix);
  965. bcm_sysport_set_rx_coalesce(priv, cur_profile.usec, cur_profile.pkts);
  966. dim->state = DIM_START_MEASURE;
  967. }
  968. /* RX and misc interrupt routine */
  969. static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
  970. {
  971. struct net_device *dev = dev_id;
  972. struct bcm_sysport_priv *priv = netdev_priv(dev);
  973. struct bcm_sysport_tx_ring *txr;
  974. unsigned int ring, ring_bit;
  975. priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
  976. ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
  977. intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  978. if (unlikely(priv->irq0_stat == 0)) {
  979. netdev_warn(priv->netdev, "spurious RX interrupt\n");
  980. return IRQ_NONE;
  981. }
  982. if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
  983. priv->dim.event_ctr++;
  984. if (likely(napi_schedule_prep(&priv->napi))) {
  985. /* disable RX interrupts */
  986. intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
  987. __napi_schedule_irqoff(&priv->napi);
  988. }
  989. }
  990. /* TX ring is full, perform a full reclaim since we do not know
  991. * which one would trigger this interrupt
  992. */
  993. if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
  994. bcm_sysport_tx_reclaim_all(priv);
  995. if (!priv->is_lite)
  996. goto out;
  997. for (ring = 0; ring < dev->num_tx_queues; ring++) {
  998. ring_bit = BIT(ring + INTRL2_0_TDMA_MBDONE_SHIFT);
  999. if (!(priv->irq0_stat & ring_bit))
  1000. continue;
  1001. txr = &priv->tx_rings[ring];
  1002. if (likely(napi_schedule_prep(&txr->napi))) {
  1003. intrl2_0_mask_set(priv, ring_bit);
  1004. __napi_schedule(&txr->napi);
  1005. }
  1006. }
  1007. out:
  1008. return IRQ_HANDLED;
  1009. }
  1010. /* TX interrupt service routine */
  1011. static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
  1012. {
  1013. struct net_device *dev = dev_id;
  1014. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1015. struct bcm_sysport_tx_ring *txr;
  1016. unsigned int ring;
  1017. priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
  1018. ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
  1019. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1020. if (unlikely(priv->irq1_stat == 0)) {
  1021. netdev_warn(priv->netdev, "spurious TX interrupt\n");
  1022. return IRQ_NONE;
  1023. }
  1024. for (ring = 0; ring < dev->num_tx_queues; ring++) {
  1025. if (!(priv->irq1_stat & BIT(ring)))
  1026. continue;
  1027. txr = &priv->tx_rings[ring];
  1028. if (likely(napi_schedule_prep(&txr->napi))) {
  1029. intrl2_1_mask_set(priv, BIT(ring));
  1030. __napi_schedule_irqoff(&txr->napi);
  1031. }
  1032. }
  1033. return IRQ_HANDLED;
  1034. }
  1035. static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
  1036. {
  1037. struct bcm_sysport_priv *priv = dev_id;
  1038. pm_wakeup_event(&priv->pdev->dev, 0);
  1039. return IRQ_HANDLED;
  1040. }
  1041. #ifdef CONFIG_NET_POLL_CONTROLLER
  1042. static void bcm_sysport_poll_controller(struct net_device *dev)
  1043. {
  1044. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1045. disable_irq(priv->irq0);
  1046. bcm_sysport_rx_isr(priv->irq0, priv);
  1047. enable_irq(priv->irq0);
  1048. if (!priv->is_lite) {
  1049. disable_irq(priv->irq1);
  1050. bcm_sysport_tx_isr(priv->irq1, priv);
  1051. enable_irq(priv->irq1);
  1052. }
  1053. }
  1054. #endif
  1055. static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
  1056. struct net_device *dev)
  1057. {
  1058. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1059. struct sk_buff *nskb;
  1060. struct bcm_tsb *tsb;
  1061. u32 csum_info;
  1062. u8 ip_proto;
  1063. u16 csum_start;
  1064. __be16 ip_ver;
  1065. /* Re-allocate SKB if needed */
  1066. if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
  1067. nskb = skb_realloc_headroom(skb, sizeof(*tsb));
  1068. if (!nskb) {
  1069. dev_kfree_skb_any(skb);
  1070. priv->mib.tx_realloc_tsb_failed++;
  1071. dev->stats.tx_errors++;
  1072. dev->stats.tx_dropped++;
  1073. return NULL;
  1074. }
  1075. dev_consume_skb_any(skb);
  1076. skb = nskb;
  1077. priv->mib.tx_realloc_tsb++;
  1078. }
  1079. tsb = skb_push(skb, sizeof(*tsb));
  1080. /* Zero-out TSB by default */
  1081. memset(tsb, 0, sizeof(*tsb));
  1082. if (skb_vlan_tag_present(skb)) {
  1083. tsb->pcp_dei_vid = skb_vlan_tag_get_prio(skb) & PCP_DEI_MASK;
  1084. tsb->pcp_dei_vid |= (u32)skb_vlan_tag_get_id(skb) << VID_SHIFT;
  1085. }
  1086. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1087. ip_ver = skb->protocol;
  1088. switch (ip_ver) {
  1089. case htons(ETH_P_IP):
  1090. ip_proto = ip_hdr(skb)->protocol;
  1091. break;
  1092. case htons(ETH_P_IPV6):
  1093. ip_proto = ipv6_hdr(skb)->nexthdr;
  1094. break;
  1095. default:
  1096. return skb;
  1097. }
  1098. /* Get the checksum offset and the L4 (transport) offset */
  1099. csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
  1100. /* Account for the HW inserted VLAN tag */
  1101. if (skb_vlan_tag_present(skb))
  1102. csum_start += VLAN_HLEN;
  1103. csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
  1104. csum_info |= (csum_start << L4_PTR_SHIFT);
  1105. if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
  1106. csum_info |= L4_LENGTH_VALID;
  1107. if (ip_proto == IPPROTO_UDP &&
  1108. ip_ver == htons(ETH_P_IP))
  1109. csum_info |= L4_UDP;
  1110. } else {
  1111. csum_info = 0;
  1112. }
  1113. tsb->l4_ptr_dest_map = csum_info;
  1114. }
  1115. return skb;
  1116. }
  1117. static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
  1118. struct net_device *dev)
  1119. {
  1120. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1121. struct device *kdev = &priv->pdev->dev;
  1122. struct bcm_sysport_tx_ring *ring;
  1123. unsigned long flags, desc_flags;
  1124. struct bcm_sysport_cb *cb;
  1125. struct netdev_queue *txq;
  1126. u32 len_status, addr_lo;
  1127. unsigned int skb_len;
  1128. dma_addr_t mapping;
  1129. u16 queue;
  1130. int ret;
  1131. queue = skb_get_queue_mapping(skb);
  1132. txq = netdev_get_tx_queue(dev, queue);
  1133. ring = &priv->tx_rings[queue];
  1134. /* lock against tx reclaim in BH context and TX ring full interrupt */
  1135. spin_lock_irqsave(&ring->lock, flags);
  1136. if (unlikely(ring->desc_count == 0)) {
  1137. netif_tx_stop_queue(txq);
  1138. netdev_err(dev, "queue %d awake and ring full!\n", queue);
  1139. ret = NETDEV_TX_BUSY;
  1140. goto out;
  1141. }
  1142. /* Insert TSB and checksum infos */
  1143. if (priv->tsb_en) {
  1144. skb = bcm_sysport_insert_tsb(skb, dev);
  1145. if (!skb) {
  1146. ret = NETDEV_TX_OK;
  1147. goto out;
  1148. }
  1149. }
  1150. skb_len = skb->len;
  1151. mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
  1152. if (dma_mapping_error(kdev, mapping)) {
  1153. priv->mib.tx_dma_failed++;
  1154. netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
  1155. skb->data, skb_len);
  1156. ret = NETDEV_TX_OK;
  1157. dev_kfree_skb_any(skb);
  1158. goto out;
  1159. }
  1160. /* Remember the SKB for future freeing */
  1161. cb = &ring->cbs[ring->curr_desc];
  1162. cb->skb = skb;
  1163. dma_unmap_addr_set(cb, dma_addr, mapping);
  1164. dma_unmap_len_set(cb, dma_len, skb_len);
  1165. addr_lo = lower_32_bits(mapping);
  1166. len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
  1167. len_status |= (skb_len << DESC_LEN_SHIFT);
  1168. len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
  1169. DESC_STATUS_SHIFT;
  1170. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1171. len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
  1172. if (skb_vlan_tag_present(skb))
  1173. len_status |= (TX_STATUS_VLAN_VID_TSB << DESC_STATUS_SHIFT);
  1174. ring->curr_desc++;
  1175. if (ring->curr_desc == ring->size)
  1176. ring->curr_desc = 0;
  1177. ring->desc_count--;
  1178. /* Ports are latched, so write upper address first */
  1179. spin_lock_irqsave(&priv->desc_lock, desc_flags);
  1180. tdma_writel(priv, len_status, TDMA_WRITE_PORT_HI(ring->index));
  1181. tdma_writel(priv, addr_lo, TDMA_WRITE_PORT_LO(ring->index));
  1182. spin_unlock_irqrestore(&priv->desc_lock, desc_flags);
  1183. /* Check ring space and update SW control flow */
  1184. if (ring->desc_count == 0)
  1185. netif_tx_stop_queue(txq);
  1186. netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
  1187. ring->index, ring->desc_count, ring->curr_desc);
  1188. ret = NETDEV_TX_OK;
  1189. out:
  1190. spin_unlock_irqrestore(&ring->lock, flags);
  1191. return ret;
  1192. }
  1193. static void bcm_sysport_tx_timeout(struct net_device *dev, unsigned int txqueue)
  1194. {
  1195. netdev_warn(dev, "transmit timeout!\n");
  1196. netif_trans_update(dev);
  1197. dev->stats.tx_errors++;
  1198. netif_tx_wake_all_queues(dev);
  1199. }
  1200. /* phylib adjust link callback */
  1201. static void bcm_sysport_adj_link(struct net_device *dev)
  1202. {
  1203. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1204. struct phy_device *phydev = dev->phydev;
  1205. unsigned int changed = 0;
  1206. u32 cmd_bits = 0, reg;
  1207. if (priv->old_link != phydev->link) {
  1208. changed = 1;
  1209. priv->old_link = phydev->link;
  1210. }
  1211. if (priv->old_duplex != phydev->duplex) {
  1212. changed = 1;
  1213. priv->old_duplex = phydev->duplex;
  1214. }
  1215. if (priv->is_lite)
  1216. goto out;
  1217. switch (phydev->speed) {
  1218. case SPEED_2500:
  1219. cmd_bits = CMD_SPEED_2500;
  1220. break;
  1221. case SPEED_1000:
  1222. cmd_bits = CMD_SPEED_1000;
  1223. break;
  1224. case SPEED_100:
  1225. cmd_bits = CMD_SPEED_100;
  1226. break;
  1227. case SPEED_10:
  1228. cmd_bits = CMD_SPEED_10;
  1229. break;
  1230. default:
  1231. break;
  1232. }
  1233. cmd_bits <<= CMD_SPEED_SHIFT;
  1234. if (phydev->duplex == DUPLEX_HALF)
  1235. cmd_bits |= CMD_HD_EN;
  1236. if (priv->old_pause != phydev->pause) {
  1237. changed = 1;
  1238. priv->old_pause = phydev->pause;
  1239. }
  1240. if (!phydev->pause)
  1241. cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
  1242. if (!changed)
  1243. return;
  1244. if (phydev->link) {
  1245. reg = umac_readl(priv, UMAC_CMD);
  1246. reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
  1247. CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
  1248. CMD_TX_PAUSE_IGNORE);
  1249. reg |= cmd_bits;
  1250. umac_writel(priv, reg, UMAC_CMD);
  1251. }
  1252. out:
  1253. if (changed)
  1254. phy_print_status(phydev);
  1255. }
  1256. static void bcm_sysport_init_dim(struct bcm_sysport_priv *priv,
  1257. void (*cb)(struct work_struct *work))
  1258. {
  1259. struct bcm_sysport_net_dim *dim = &priv->dim;
  1260. INIT_WORK(&dim->dim.work, cb);
  1261. dim->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  1262. dim->event_ctr = 0;
  1263. dim->packets = 0;
  1264. dim->bytes = 0;
  1265. }
  1266. static void bcm_sysport_init_rx_coalesce(struct bcm_sysport_priv *priv)
  1267. {
  1268. struct bcm_sysport_net_dim *dim = &priv->dim;
  1269. struct dim_cq_moder moder;
  1270. u32 usecs, pkts;
  1271. usecs = priv->rx_coalesce_usecs;
  1272. pkts = priv->rx_max_coalesced_frames;
  1273. /* If DIM was enabled, re-apply default parameters */
  1274. if (dim->use_dim) {
  1275. moder = net_dim_get_def_rx_moderation(dim->dim.mode);
  1276. usecs = moder.usec;
  1277. pkts = moder.pkts;
  1278. }
  1279. bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
  1280. }
  1281. static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
  1282. unsigned int index)
  1283. {
  1284. struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
  1285. size_t size;
  1286. u32 reg;
  1287. /* Simple descriptors partitioning for now */
  1288. size = 256;
  1289. ring->cbs = kcalloc(size, sizeof(struct bcm_sysport_cb), GFP_KERNEL);
  1290. if (!ring->cbs) {
  1291. netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
  1292. return -ENOMEM;
  1293. }
  1294. /* Initialize SW view of the ring */
  1295. spin_lock_init(&ring->lock);
  1296. ring->priv = priv;
  1297. netif_napi_add_tx(priv->netdev, &ring->napi, bcm_sysport_tx_poll);
  1298. ring->index = index;
  1299. ring->size = size;
  1300. ring->clean_index = 0;
  1301. ring->alloc_size = ring->size;
  1302. ring->desc_count = ring->size;
  1303. ring->curr_desc = 0;
  1304. /* Initialize HW ring */
  1305. tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
  1306. tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
  1307. tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
  1308. tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
  1309. /* Configure QID and port mapping */
  1310. reg = tdma_readl(priv, TDMA_DESC_RING_MAPPING(index));
  1311. reg &= ~(RING_QID_MASK | RING_PORT_ID_MASK << RING_PORT_ID_SHIFT);
  1312. if (ring->inspect) {
  1313. reg |= ring->switch_queue & RING_QID_MASK;
  1314. reg |= ring->switch_port << RING_PORT_ID_SHIFT;
  1315. } else {
  1316. reg |= RING_IGNORE_STATUS;
  1317. }
  1318. tdma_writel(priv, reg, TDMA_DESC_RING_MAPPING(index));
  1319. reg = 0;
  1320. /* Adjust the packet size calculations if SYSTEMPORT is responsible
  1321. * for HW insertion of VLAN tags
  1322. */
  1323. if (priv->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
  1324. reg = VLAN_HLEN << RING_PKT_SIZE_ADJ_SHIFT;
  1325. tdma_writel(priv, reg, TDMA_DESC_RING_PCP_DEI_VID(index));
  1326. /* Enable ACB algorithm 2 */
  1327. reg = tdma_readl(priv, TDMA_CONTROL);
  1328. reg |= tdma_control_bit(priv, ACB_ALGO);
  1329. tdma_writel(priv, reg, TDMA_CONTROL);
  1330. /* Do not use tdma_control_bit() here because TSB_SWAP1 collides
  1331. * with the original definition of ACB_ALGO
  1332. */
  1333. reg = tdma_readl(priv, TDMA_CONTROL);
  1334. if (priv->is_lite)
  1335. reg &= ~BIT(TSB_SWAP1);
  1336. /* Set a correct TSB format based on host endian */
  1337. if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1338. reg |= tdma_control_bit(priv, TSB_SWAP0);
  1339. else
  1340. reg &= ~tdma_control_bit(priv, TSB_SWAP0);
  1341. tdma_writel(priv, reg, TDMA_CONTROL);
  1342. /* Program the number of descriptors as MAX_THRESHOLD and half of
  1343. * its size for the hysteresis trigger
  1344. */
  1345. tdma_writel(priv, ring->size |
  1346. 1 << RING_HYST_THRESH_SHIFT,
  1347. TDMA_DESC_RING_MAX_HYST(index));
  1348. /* Enable the ring queue in the arbiter */
  1349. reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
  1350. reg |= (1 << index);
  1351. tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
  1352. napi_enable(&ring->napi);
  1353. netif_dbg(priv, hw, priv->netdev,
  1354. "TDMA cfg, size=%d, switch q=%d,port=%d\n",
  1355. ring->size, ring->switch_queue,
  1356. ring->switch_port);
  1357. return 0;
  1358. }
  1359. static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
  1360. unsigned int index)
  1361. {
  1362. struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
  1363. u32 reg;
  1364. /* Caller should stop the TDMA engine */
  1365. reg = tdma_readl(priv, TDMA_STATUS);
  1366. if (!(reg & TDMA_DISABLED))
  1367. netdev_warn(priv->netdev, "TDMA not stopped!\n");
  1368. /* ring->cbs is the last part in bcm_sysport_init_tx_ring which could
  1369. * fail, so by checking this pointer we know whether the TX ring was
  1370. * fully initialized or not.
  1371. */
  1372. if (!ring->cbs)
  1373. return;
  1374. napi_disable(&ring->napi);
  1375. netif_napi_del(&ring->napi);
  1376. bcm_sysport_tx_clean(priv, ring);
  1377. kfree(ring->cbs);
  1378. ring->cbs = NULL;
  1379. ring->size = 0;
  1380. ring->alloc_size = 0;
  1381. netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
  1382. }
  1383. /* RDMA helper */
  1384. static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
  1385. unsigned int enable)
  1386. {
  1387. unsigned int timeout = 1000;
  1388. u32 reg;
  1389. reg = rdma_readl(priv, RDMA_CONTROL);
  1390. if (enable)
  1391. reg |= RDMA_EN;
  1392. else
  1393. reg &= ~RDMA_EN;
  1394. rdma_writel(priv, reg, RDMA_CONTROL);
  1395. /* Poll for RMDA disabling completion */
  1396. do {
  1397. reg = rdma_readl(priv, RDMA_STATUS);
  1398. if (!!(reg & RDMA_DISABLED) == !enable)
  1399. return 0;
  1400. usleep_range(1000, 2000);
  1401. } while (timeout-- > 0);
  1402. netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
  1403. return -ETIMEDOUT;
  1404. }
  1405. /* TDMA helper */
  1406. static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
  1407. unsigned int enable)
  1408. {
  1409. unsigned int timeout = 1000;
  1410. u32 reg;
  1411. reg = tdma_readl(priv, TDMA_CONTROL);
  1412. if (enable)
  1413. reg |= tdma_control_bit(priv, TDMA_EN);
  1414. else
  1415. reg &= ~tdma_control_bit(priv, TDMA_EN);
  1416. tdma_writel(priv, reg, TDMA_CONTROL);
  1417. /* Poll for TMDA disabling completion */
  1418. do {
  1419. reg = tdma_readl(priv, TDMA_STATUS);
  1420. if (!!(reg & TDMA_DISABLED) == !enable)
  1421. return 0;
  1422. usleep_range(1000, 2000);
  1423. } while (timeout-- > 0);
  1424. netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
  1425. return -ETIMEDOUT;
  1426. }
  1427. static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
  1428. {
  1429. struct bcm_sysport_cb *cb;
  1430. u32 reg;
  1431. int ret;
  1432. int i;
  1433. /* Initialize SW view of the RX ring */
  1434. priv->num_rx_bds = priv->num_rx_desc_words / WORDS_PER_DESC;
  1435. priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
  1436. priv->rx_c_index = 0;
  1437. priv->rx_read_ptr = 0;
  1438. priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct bcm_sysport_cb),
  1439. GFP_KERNEL);
  1440. if (!priv->rx_cbs) {
  1441. netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
  1442. return -ENOMEM;
  1443. }
  1444. for (i = 0; i < priv->num_rx_bds; i++) {
  1445. cb = priv->rx_cbs + i;
  1446. cb->bd_addr = priv->rx_bds + i * DESC_SIZE;
  1447. }
  1448. ret = bcm_sysport_alloc_rx_bufs(priv);
  1449. if (ret) {
  1450. netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
  1451. return ret;
  1452. }
  1453. /* Initialize HW, ensure RDMA is disabled */
  1454. reg = rdma_readl(priv, RDMA_STATUS);
  1455. if (!(reg & RDMA_DISABLED))
  1456. rdma_enable_set(priv, 0);
  1457. rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
  1458. rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
  1459. rdma_writel(priv, 0, RDMA_PROD_INDEX);
  1460. rdma_writel(priv, 0, RDMA_CONS_INDEX);
  1461. rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
  1462. RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
  1463. /* Operate the queue in ring mode */
  1464. rdma_writel(priv, 0, RDMA_START_ADDR_HI);
  1465. rdma_writel(priv, 0, RDMA_START_ADDR_LO);
  1466. rdma_writel(priv, 0, RDMA_END_ADDR_HI);
  1467. rdma_writel(priv, priv->num_rx_desc_words - 1, RDMA_END_ADDR_LO);
  1468. netif_dbg(priv, hw, priv->netdev,
  1469. "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
  1470. priv->num_rx_bds, priv->rx_bds);
  1471. return 0;
  1472. }
  1473. static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
  1474. {
  1475. struct bcm_sysport_cb *cb;
  1476. unsigned int i;
  1477. u32 reg;
  1478. /* Caller should ensure RDMA is disabled */
  1479. reg = rdma_readl(priv, RDMA_STATUS);
  1480. if (!(reg & RDMA_DISABLED))
  1481. netdev_warn(priv->netdev, "RDMA not stopped!\n");
  1482. for (i = 0; i < priv->num_rx_bds; i++) {
  1483. cb = &priv->rx_cbs[i];
  1484. if (dma_unmap_addr(cb, dma_addr))
  1485. dma_unmap_single(&priv->pdev->dev,
  1486. dma_unmap_addr(cb, dma_addr),
  1487. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  1488. bcm_sysport_free_cb(cb);
  1489. }
  1490. kfree(priv->rx_cbs);
  1491. priv->rx_cbs = NULL;
  1492. netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
  1493. }
  1494. static void bcm_sysport_set_rx_mode(struct net_device *dev)
  1495. {
  1496. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1497. u32 reg;
  1498. if (priv->is_lite)
  1499. return;
  1500. reg = umac_readl(priv, UMAC_CMD);
  1501. if (dev->flags & IFF_PROMISC)
  1502. reg |= CMD_PROMISC;
  1503. else
  1504. reg &= ~CMD_PROMISC;
  1505. umac_writel(priv, reg, UMAC_CMD);
  1506. /* No support for ALLMULTI */
  1507. if (dev->flags & IFF_ALLMULTI)
  1508. return;
  1509. }
  1510. static inline void umac_enable_set(struct bcm_sysport_priv *priv,
  1511. u32 mask, unsigned int enable)
  1512. {
  1513. u32 reg;
  1514. if (!priv->is_lite) {
  1515. reg = umac_readl(priv, UMAC_CMD);
  1516. if (enable)
  1517. reg |= mask;
  1518. else
  1519. reg &= ~mask;
  1520. umac_writel(priv, reg, UMAC_CMD);
  1521. } else {
  1522. reg = gib_readl(priv, GIB_CONTROL);
  1523. if (enable)
  1524. reg |= mask;
  1525. else
  1526. reg &= ~mask;
  1527. gib_writel(priv, reg, GIB_CONTROL);
  1528. }
  1529. /* UniMAC stops on a packet boundary, wait for a full-sized packet
  1530. * to be processed (1 msec).
  1531. */
  1532. if (enable == 0)
  1533. usleep_range(1000, 2000);
  1534. }
  1535. static inline void umac_reset(struct bcm_sysport_priv *priv)
  1536. {
  1537. u32 reg;
  1538. if (priv->is_lite)
  1539. return;
  1540. reg = umac_readl(priv, UMAC_CMD);
  1541. reg |= CMD_SW_RESET;
  1542. umac_writel(priv, reg, UMAC_CMD);
  1543. udelay(10);
  1544. reg = umac_readl(priv, UMAC_CMD);
  1545. reg &= ~CMD_SW_RESET;
  1546. umac_writel(priv, reg, UMAC_CMD);
  1547. }
  1548. static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
  1549. const unsigned char *addr)
  1550. {
  1551. u32 mac0 = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
  1552. addr[3];
  1553. u32 mac1 = (addr[4] << 8) | addr[5];
  1554. if (!priv->is_lite) {
  1555. umac_writel(priv, mac0, UMAC_MAC0);
  1556. umac_writel(priv, mac1, UMAC_MAC1);
  1557. } else {
  1558. gib_writel(priv, mac0, GIB_MAC0);
  1559. gib_writel(priv, mac1, GIB_MAC1);
  1560. }
  1561. }
  1562. static void topctrl_flush(struct bcm_sysport_priv *priv)
  1563. {
  1564. topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
  1565. topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
  1566. mdelay(1);
  1567. topctrl_writel(priv, 0, RX_FLUSH_CNTL);
  1568. topctrl_writel(priv, 0, TX_FLUSH_CNTL);
  1569. }
  1570. static int bcm_sysport_change_mac(struct net_device *dev, void *p)
  1571. {
  1572. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1573. struct sockaddr *addr = p;
  1574. if (!is_valid_ether_addr(addr->sa_data))
  1575. return -EINVAL;
  1576. eth_hw_addr_set(dev, addr->sa_data);
  1577. /* interface is disabled, changes to MAC will be reflected on next
  1578. * open call
  1579. */
  1580. if (!netif_running(dev))
  1581. return 0;
  1582. umac_set_hw_addr(priv, dev->dev_addr);
  1583. return 0;
  1584. }
  1585. static void bcm_sysport_get_stats64(struct net_device *dev,
  1586. struct rtnl_link_stats64 *stats)
  1587. {
  1588. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1589. struct bcm_sysport_stats64 *stats64 = &priv->stats64;
  1590. unsigned int start;
  1591. netdev_stats_to_stats64(stats, &dev->stats);
  1592. bcm_sysport_update_tx_stats(priv, &stats->tx_bytes,
  1593. &stats->tx_packets);
  1594. do {
  1595. start = u64_stats_fetch_begin(&priv->syncp);
  1596. stats->rx_packets = stats64->rx_packets;
  1597. stats->rx_bytes = stats64->rx_bytes;
  1598. } while (u64_stats_fetch_retry(&priv->syncp, start));
  1599. }
  1600. static void bcm_sysport_netif_start(struct net_device *dev)
  1601. {
  1602. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1603. /* Enable NAPI */
  1604. bcm_sysport_init_dim(priv, bcm_sysport_dim_work);
  1605. bcm_sysport_init_rx_coalesce(priv);
  1606. napi_enable(&priv->napi);
  1607. /* Enable RX interrupt and TX ring full interrupt */
  1608. intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
  1609. phy_start(dev->phydev);
  1610. /* Enable TX interrupts for the TXQs */
  1611. if (!priv->is_lite)
  1612. intrl2_1_mask_clear(priv, 0xffffffff);
  1613. else
  1614. intrl2_0_mask_clear(priv, INTRL2_0_TDMA_MBDONE_MASK);
  1615. }
  1616. static void rbuf_init(struct bcm_sysport_priv *priv)
  1617. {
  1618. u32 reg;
  1619. reg = rbuf_readl(priv, RBUF_CONTROL);
  1620. reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
  1621. /* Set a correct RSB format on SYSTEMPORT Lite */
  1622. if (priv->is_lite)
  1623. reg &= ~RBUF_RSB_SWAP1;
  1624. /* Set a correct RSB format based on host endian */
  1625. if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1626. reg |= RBUF_RSB_SWAP0;
  1627. else
  1628. reg &= ~RBUF_RSB_SWAP0;
  1629. rbuf_writel(priv, reg, RBUF_CONTROL);
  1630. }
  1631. static inline void bcm_sysport_mask_all_intrs(struct bcm_sysport_priv *priv)
  1632. {
  1633. intrl2_0_mask_set(priv, 0xffffffff);
  1634. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1635. if (!priv->is_lite) {
  1636. intrl2_1_mask_set(priv, 0xffffffff);
  1637. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1638. }
  1639. }
  1640. static inline void gib_set_pad_extension(struct bcm_sysport_priv *priv)
  1641. {
  1642. u32 reg;
  1643. reg = gib_readl(priv, GIB_CONTROL);
  1644. /* Include Broadcom tag in pad extension and fix up IPG_LENGTH */
  1645. if (netdev_uses_dsa(priv->netdev)) {
  1646. reg &= ~(GIB_PAD_EXTENSION_MASK << GIB_PAD_EXTENSION_SHIFT);
  1647. reg |= ENET_BRCM_TAG_LEN << GIB_PAD_EXTENSION_SHIFT;
  1648. }
  1649. reg &= ~(GIB_IPG_LEN_MASK << GIB_IPG_LEN_SHIFT);
  1650. reg |= 12 << GIB_IPG_LEN_SHIFT;
  1651. gib_writel(priv, reg, GIB_CONTROL);
  1652. }
  1653. static int bcm_sysport_open(struct net_device *dev)
  1654. {
  1655. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1656. struct phy_device *phydev;
  1657. unsigned int i;
  1658. int ret;
  1659. ret = clk_prepare_enable(priv->clk);
  1660. if (ret) {
  1661. netdev_err(dev, "could not enable priv clock\n");
  1662. return ret;
  1663. }
  1664. /* Reset UniMAC */
  1665. umac_reset(priv);
  1666. /* Flush TX and RX FIFOs at TOPCTRL level */
  1667. topctrl_flush(priv);
  1668. /* Disable the UniMAC RX/TX */
  1669. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
  1670. /* Enable RBUF 2bytes alignment and Receive Status Block */
  1671. rbuf_init(priv);
  1672. /* Set maximum frame length */
  1673. if (!priv->is_lite)
  1674. umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  1675. else
  1676. gib_set_pad_extension(priv);
  1677. /* Apply features again in case we changed them while interface was
  1678. * down
  1679. */
  1680. bcm_sysport_set_features(dev, dev->features);
  1681. /* Set MAC address */
  1682. umac_set_hw_addr(priv, dev->dev_addr);
  1683. phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
  1684. 0, priv->phy_interface);
  1685. if (!phydev) {
  1686. netdev_err(dev, "could not attach to PHY\n");
  1687. ret = -ENODEV;
  1688. goto out_clk_disable;
  1689. }
  1690. /* Indicate that the MAC is responsible for PHY PM */
  1691. phydev->mac_managed_pm = true;
  1692. /* Reset house keeping link status */
  1693. priv->old_duplex = -1;
  1694. priv->old_link = -1;
  1695. priv->old_pause = -1;
  1696. /* mask all interrupts and request them */
  1697. bcm_sysport_mask_all_intrs(priv);
  1698. ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
  1699. if (ret) {
  1700. netdev_err(dev, "failed to request RX interrupt\n");
  1701. goto out_phy_disconnect;
  1702. }
  1703. if (!priv->is_lite) {
  1704. ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0,
  1705. dev->name, dev);
  1706. if (ret) {
  1707. netdev_err(dev, "failed to request TX interrupt\n");
  1708. goto out_free_irq0;
  1709. }
  1710. }
  1711. /* Initialize both hardware and software ring */
  1712. spin_lock_init(&priv->desc_lock);
  1713. for (i = 0; i < dev->num_tx_queues; i++) {
  1714. ret = bcm_sysport_init_tx_ring(priv, i);
  1715. if (ret) {
  1716. netdev_err(dev, "failed to initialize TX ring %d\n",
  1717. i);
  1718. goto out_free_tx_ring;
  1719. }
  1720. }
  1721. /* Initialize linked-list */
  1722. tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
  1723. /* Initialize RX ring */
  1724. ret = bcm_sysport_init_rx_ring(priv);
  1725. if (ret) {
  1726. netdev_err(dev, "failed to initialize RX ring\n");
  1727. goto out_free_rx_ring;
  1728. }
  1729. /* Turn on RDMA */
  1730. ret = rdma_enable_set(priv, 1);
  1731. if (ret)
  1732. goto out_free_rx_ring;
  1733. /* Turn on TDMA */
  1734. ret = tdma_enable_set(priv, 1);
  1735. if (ret)
  1736. goto out_clear_rx_int;
  1737. /* Turn on UniMAC TX/RX */
  1738. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
  1739. bcm_sysport_netif_start(dev);
  1740. netif_tx_start_all_queues(dev);
  1741. return 0;
  1742. out_clear_rx_int:
  1743. intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
  1744. out_free_rx_ring:
  1745. bcm_sysport_fini_rx_ring(priv);
  1746. out_free_tx_ring:
  1747. for (i = 0; i < dev->num_tx_queues; i++)
  1748. bcm_sysport_fini_tx_ring(priv, i);
  1749. if (!priv->is_lite)
  1750. free_irq(priv->irq1, dev);
  1751. out_free_irq0:
  1752. free_irq(priv->irq0, dev);
  1753. out_phy_disconnect:
  1754. phy_disconnect(phydev);
  1755. out_clk_disable:
  1756. clk_disable_unprepare(priv->clk);
  1757. return ret;
  1758. }
  1759. static void bcm_sysport_netif_stop(struct net_device *dev)
  1760. {
  1761. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1762. /* stop all software from updating hardware */
  1763. netif_tx_disable(dev);
  1764. napi_disable(&priv->napi);
  1765. cancel_work_sync(&priv->dim.dim.work);
  1766. phy_stop(dev->phydev);
  1767. /* mask all interrupts */
  1768. bcm_sysport_mask_all_intrs(priv);
  1769. }
  1770. static int bcm_sysport_stop(struct net_device *dev)
  1771. {
  1772. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1773. unsigned int i;
  1774. int ret;
  1775. bcm_sysport_netif_stop(dev);
  1776. /* Disable UniMAC RX */
  1777. umac_enable_set(priv, CMD_RX_EN, 0);
  1778. ret = tdma_enable_set(priv, 0);
  1779. if (ret) {
  1780. netdev_err(dev, "timeout disabling RDMA\n");
  1781. return ret;
  1782. }
  1783. /* Wait for a maximum packet size to be drained */
  1784. usleep_range(2000, 3000);
  1785. ret = rdma_enable_set(priv, 0);
  1786. if (ret) {
  1787. netdev_err(dev, "timeout disabling TDMA\n");
  1788. return ret;
  1789. }
  1790. /* Disable UniMAC TX */
  1791. umac_enable_set(priv, CMD_TX_EN, 0);
  1792. /* Free RX/TX rings SW structures */
  1793. for (i = 0; i < dev->num_tx_queues; i++)
  1794. bcm_sysport_fini_tx_ring(priv, i);
  1795. bcm_sysport_fini_rx_ring(priv);
  1796. free_irq(priv->irq0, dev);
  1797. if (!priv->is_lite)
  1798. free_irq(priv->irq1, dev);
  1799. /* Disconnect from PHY */
  1800. phy_disconnect(dev->phydev);
  1801. clk_disable_unprepare(priv->clk);
  1802. return 0;
  1803. }
  1804. static int bcm_sysport_rule_find(struct bcm_sysport_priv *priv,
  1805. u64 location)
  1806. {
  1807. unsigned int index;
  1808. u32 reg;
  1809. for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
  1810. reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
  1811. reg >>= RXCHK_BRCM_TAG_CID_SHIFT;
  1812. reg &= RXCHK_BRCM_TAG_CID_MASK;
  1813. if (reg == location)
  1814. return index;
  1815. }
  1816. return -EINVAL;
  1817. }
  1818. static int bcm_sysport_rule_get(struct bcm_sysport_priv *priv,
  1819. struct ethtool_rxnfc *nfc)
  1820. {
  1821. int index;
  1822. /* This is not a rule that we know about */
  1823. index = bcm_sysport_rule_find(priv, nfc->fs.location);
  1824. if (index < 0)
  1825. return -EOPNOTSUPP;
  1826. nfc->fs.ring_cookie = RX_CLS_FLOW_WAKE;
  1827. return 0;
  1828. }
  1829. static int bcm_sysport_rule_set(struct bcm_sysport_priv *priv,
  1830. struct ethtool_rxnfc *nfc)
  1831. {
  1832. unsigned int index;
  1833. u32 reg;
  1834. /* We cannot match locations greater than what the classification ID
  1835. * permits (256 entries)
  1836. */
  1837. if (nfc->fs.location > RXCHK_BRCM_TAG_CID_MASK)
  1838. return -E2BIG;
  1839. /* We cannot support flows that are not destined for a wake-up */
  1840. if (nfc->fs.ring_cookie != RX_CLS_FLOW_WAKE)
  1841. return -EOPNOTSUPP;
  1842. index = find_first_zero_bit(priv->filters, RXCHK_BRCM_TAG_MAX);
  1843. if (index >= RXCHK_BRCM_TAG_MAX)
  1844. /* All filters are already in use, we cannot match more rules */
  1845. return -ENOSPC;
  1846. /* Location is the classification ID, and index is the position
  1847. * within one of our 8 possible filters to be programmed
  1848. */
  1849. reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
  1850. reg &= ~(RXCHK_BRCM_TAG_CID_MASK << RXCHK_BRCM_TAG_CID_SHIFT);
  1851. reg |= nfc->fs.location << RXCHK_BRCM_TAG_CID_SHIFT;
  1852. rxchk_writel(priv, reg, RXCHK_BRCM_TAG(index));
  1853. rxchk_writel(priv, 0xff00ffff, RXCHK_BRCM_TAG_MASK(index));
  1854. priv->filters_loc[index] = nfc->fs.location;
  1855. set_bit(index, priv->filters);
  1856. return 0;
  1857. }
  1858. static int bcm_sysport_rule_del(struct bcm_sysport_priv *priv,
  1859. u64 location)
  1860. {
  1861. int index;
  1862. /* This is not a rule that we know about */
  1863. index = bcm_sysport_rule_find(priv, location);
  1864. if (index < 0)
  1865. return -EOPNOTSUPP;
  1866. /* No need to disable this filter if it was enabled, this will
  1867. * be taken care of during suspend time by bcm_sysport_suspend_to_wol
  1868. */
  1869. clear_bit(index, priv->filters);
  1870. priv->filters_loc[index] = 0;
  1871. return 0;
  1872. }
  1873. static int bcm_sysport_get_rxnfc(struct net_device *dev,
  1874. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  1875. {
  1876. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1877. int ret = -EOPNOTSUPP;
  1878. switch (nfc->cmd) {
  1879. case ETHTOOL_GRXCLSRULE:
  1880. ret = bcm_sysport_rule_get(priv, nfc);
  1881. break;
  1882. default:
  1883. break;
  1884. }
  1885. return ret;
  1886. }
  1887. static int bcm_sysport_set_rxnfc(struct net_device *dev,
  1888. struct ethtool_rxnfc *nfc)
  1889. {
  1890. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1891. int ret = -EOPNOTSUPP;
  1892. switch (nfc->cmd) {
  1893. case ETHTOOL_SRXCLSRLINS:
  1894. ret = bcm_sysport_rule_set(priv, nfc);
  1895. break;
  1896. case ETHTOOL_SRXCLSRLDEL:
  1897. ret = bcm_sysport_rule_del(priv, nfc->fs.location);
  1898. break;
  1899. default:
  1900. break;
  1901. }
  1902. return ret;
  1903. }
  1904. static const struct ethtool_ops bcm_sysport_ethtool_ops = {
  1905. .supported_coalesce_params = ETHTOOL_COALESCE_USECS |
  1906. ETHTOOL_COALESCE_MAX_FRAMES |
  1907. ETHTOOL_COALESCE_USE_ADAPTIVE_RX,
  1908. .get_drvinfo = bcm_sysport_get_drvinfo,
  1909. .get_msglevel = bcm_sysport_get_msglvl,
  1910. .set_msglevel = bcm_sysport_set_msglvl,
  1911. .get_link = ethtool_op_get_link,
  1912. .get_strings = bcm_sysport_get_strings,
  1913. .get_ethtool_stats = bcm_sysport_get_stats,
  1914. .get_sset_count = bcm_sysport_get_sset_count,
  1915. .get_wol = bcm_sysport_get_wol,
  1916. .set_wol = bcm_sysport_set_wol,
  1917. .get_coalesce = bcm_sysport_get_coalesce,
  1918. .set_coalesce = bcm_sysport_set_coalesce,
  1919. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1920. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1921. .get_rxnfc = bcm_sysport_get_rxnfc,
  1922. .set_rxnfc = bcm_sysport_set_rxnfc,
  1923. };
  1924. static u16 bcm_sysport_select_queue(struct net_device *dev, struct sk_buff *skb,
  1925. struct net_device *sb_dev)
  1926. {
  1927. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1928. u16 queue = skb_get_queue_mapping(skb);
  1929. struct bcm_sysport_tx_ring *tx_ring;
  1930. unsigned int q, port;
  1931. if (!netdev_uses_dsa(dev))
  1932. return netdev_pick_tx(dev, skb, NULL);
  1933. /* DSA tagging layer will have configured the correct queue */
  1934. q = BRCM_TAG_GET_QUEUE(queue);
  1935. port = BRCM_TAG_GET_PORT(queue);
  1936. tx_ring = priv->ring_map[q + port * priv->per_port_num_tx_queues];
  1937. if (unlikely(!tx_ring))
  1938. return netdev_pick_tx(dev, skb, NULL);
  1939. return tx_ring->index;
  1940. }
  1941. static const struct net_device_ops bcm_sysport_netdev_ops = {
  1942. .ndo_start_xmit = bcm_sysport_xmit,
  1943. .ndo_tx_timeout = bcm_sysport_tx_timeout,
  1944. .ndo_open = bcm_sysport_open,
  1945. .ndo_stop = bcm_sysport_stop,
  1946. .ndo_set_features = bcm_sysport_set_features,
  1947. .ndo_set_rx_mode = bcm_sysport_set_rx_mode,
  1948. .ndo_set_mac_address = bcm_sysport_change_mac,
  1949. #ifdef CONFIG_NET_POLL_CONTROLLER
  1950. .ndo_poll_controller = bcm_sysport_poll_controller,
  1951. #endif
  1952. .ndo_get_stats64 = bcm_sysport_get_stats64,
  1953. .ndo_select_queue = bcm_sysport_select_queue,
  1954. };
  1955. static int bcm_sysport_map_queues(struct net_device *dev,
  1956. struct net_device *slave_dev)
  1957. {
  1958. struct dsa_port *dp = dsa_port_from_netdev(slave_dev);
  1959. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1960. struct bcm_sysport_tx_ring *ring;
  1961. unsigned int num_tx_queues;
  1962. unsigned int q, qp, port;
  1963. /* We can't be setting up queue inspection for non directly attached
  1964. * switches
  1965. */
  1966. if (dp->ds->index)
  1967. return 0;
  1968. port = dp->index;
  1969. /* On SYSTEMPORT Lite we have twice as less queues, so we cannot do a
  1970. * 1:1 mapping, we can only do a 2:1 mapping. By reducing the number of
  1971. * per-port (slave_dev) network devices queue, we achieve just that.
  1972. * This need to happen now before any slave network device is used such
  1973. * it accurately reflects the number of real TX queues.
  1974. */
  1975. if (priv->is_lite)
  1976. netif_set_real_num_tx_queues(slave_dev,
  1977. slave_dev->num_tx_queues / 2);
  1978. num_tx_queues = slave_dev->real_num_tx_queues;
  1979. if (priv->per_port_num_tx_queues &&
  1980. priv->per_port_num_tx_queues != num_tx_queues)
  1981. netdev_warn(slave_dev, "asymmetric number of per-port queues\n");
  1982. priv->per_port_num_tx_queues = num_tx_queues;
  1983. for (q = 0, qp = 0; q < dev->num_tx_queues && qp < num_tx_queues;
  1984. q++) {
  1985. ring = &priv->tx_rings[q];
  1986. if (ring->inspect)
  1987. continue;
  1988. /* Just remember the mapping actual programming done
  1989. * during bcm_sysport_init_tx_ring
  1990. */
  1991. ring->switch_queue = qp;
  1992. ring->switch_port = port;
  1993. ring->inspect = true;
  1994. priv->ring_map[qp + port * num_tx_queues] = ring;
  1995. qp++;
  1996. }
  1997. return 0;
  1998. }
  1999. static int bcm_sysport_unmap_queues(struct net_device *dev,
  2000. struct net_device *slave_dev)
  2001. {
  2002. struct dsa_port *dp = dsa_port_from_netdev(slave_dev);
  2003. struct bcm_sysport_priv *priv = netdev_priv(dev);
  2004. struct bcm_sysport_tx_ring *ring;
  2005. unsigned int num_tx_queues;
  2006. unsigned int q, qp, port;
  2007. port = dp->index;
  2008. num_tx_queues = slave_dev->real_num_tx_queues;
  2009. for (q = 0; q < dev->num_tx_queues; q++) {
  2010. ring = &priv->tx_rings[q];
  2011. if (ring->switch_port != port)
  2012. continue;
  2013. if (!ring->inspect)
  2014. continue;
  2015. ring->inspect = false;
  2016. qp = ring->switch_queue;
  2017. priv->ring_map[qp + port * num_tx_queues] = NULL;
  2018. }
  2019. return 0;
  2020. }
  2021. static int bcm_sysport_netdevice_event(struct notifier_block *nb,
  2022. unsigned long event, void *ptr)
  2023. {
  2024. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2025. struct netdev_notifier_changeupper_info *info = ptr;
  2026. struct bcm_sysport_priv *priv;
  2027. int ret = 0;
  2028. priv = container_of(nb, struct bcm_sysport_priv, netdev_notifier);
  2029. if (priv->netdev != dev)
  2030. return NOTIFY_DONE;
  2031. switch (event) {
  2032. case NETDEV_CHANGEUPPER:
  2033. if (dev->netdev_ops != &bcm_sysport_netdev_ops)
  2034. return NOTIFY_DONE;
  2035. if (!dsa_user_dev_check(info->upper_dev))
  2036. return NOTIFY_DONE;
  2037. if (info->linking)
  2038. ret = bcm_sysport_map_queues(dev, info->upper_dev);
  2039. else
  2040. ret = bcm_sysport_unmap_queues(dev, info->upper_dev);
  2041. break;
  2042. }
  2043. return notifier_from_errno(ret);
  2044. }
  2045. #define REV_FMT "v%2x.%02x"
  2046. static const struct bcm_sysport_hw_params bcm_sysport_params[] = {
  2047. [SYSTEMPORT] = {
  2048. .is_lite = false,
  2049. .num_rx_desc_words = SP_NUM_HW_RX_DESC_WORDS,
  2050. },
  2051. [SYSTEMPORT_LITE] = {
  2052. .is_lite = true,
  2053. .num_rx_desc_words = SP_LT_NUM_HW_RX_DESC_WORDS,
  2054. },
  2055. };
  2056. static const struct of_device_id bcm_sysport_of_match[] = {
  2057. { .compatible = "brcm,systemportlite-v1.00",
  2058. .data = &bcm_sysport_params[SYSTEMPORT_LITE] },
  2059. { .compatible = "brcm,systemport-v1.00",
  2060. .data = &bcm_sysport_params[SYSTEMPORT] },
  2061. { .compatible = "brcm,systemport",
  2062. .data = &bcm_sysport_params[SYSTEMPORT] },
  2063. { /* sentinel */ }
  2064. };
  2065. MODULE_DEVICE_TABLE(of, bcm_sysport_of_match);
  2066. static int bcm_sysport_probe(struct platform_device *pdev)
  2067. {
  2068. const struct bcm_sysport_hw_params *params;
  2069. const struct of_device_id *of_id = NULL;
  2070. struct bcm_sysport_priv *priv;
  2071. struct device_node *dn;
  2072. struct net_device *dev;
  2073. u32 txq, rxq;
  2074. int ret;
  2075. dn = pdev->dev.of_node;
  2076. of_id = of_match_node(bcm_sysport_of_match, dn);
  2077. if (!of_id || !of_id->data)
  2078. return -EINVAL;
  2079. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40));
  2080. if (ret)
  2081. ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2082. if (ret) {
  2083. dev_err(&pdev->dev, "unable to set DMA mask: %d\n", ret);
  2084. return ret;
  2085. }
  2086. /* Fairly quickly we need to know the type of adapter we have */
  2087. params = of_id->data;
  2088. /* Read the Transmit/Receive Queue properties */
  2089. if (of_property_read_u32(dn, "systemport,num-txq", &txq))
  2090. txq = TDMA_NUM_RINGS;
  2091. if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
  2092. rxq = 1;
  2093. /* Sanity check the number of transmit queues */
  2094. if (!txq || txq > TDMA_NUM_RINGS)
  2095. return -EINVAL;
  2096. dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
  2097. if (!dev)
  2098. return -ENOMEM;
  2099. /* Initialize private members */
  2100. priv = netdev_priv(dev);
  2101. priv->clk = devm_clk_get_optional(&pdev->dev, "sw_sysport");
  2102. if (IS_ERR(priv->clk)) {
  2103. ret = PTR_ERR(priv->clk);
  2104. goto err_free_netdev;
  2105. }
  2106. /* Allocate number of TX rings */
  2107. priv->tx_rings = devm_kcalloc(&pdev->dev, txq,
  2108. sizeof(struct bcm_sysport_tx_ring),
  2109. GFP_KERNEL);
  2110. if (!priv->tx_rings) {
  2111. ret = -ENOMEM;
  2112. goto err_free_netdev;
  2113. }
  2114. priv->is_lite = params->is_lite;
  2115. priv->num_rx_desc_words = params->num_rx_desc_words;
  2116. priv->irq0 = platform_get_irq(pdev, 0);
  2117. if (!priv->is_lite) {
  2118. priv->irq1 = platform_get_irq(pdev, 1);
  2119. priv->wol_irq = platform_get_irq_optional(pdev, 2);
  2120. } else {
  2121. priv->wol_irq = platform_get_irq_optional(pdev, 1);
  2122. }
  2123. if (priv->irq0 <= 0 || (priv->irq1 <= 0 && !priv->is_lite)) {
  2124. ret = -EINVAL;
  2125. goto err_free_netdev;
  2126. }
  2127. priv->base = devm_platform_ioremap_resource(pdev, 0);
  2128. if (IS_ERR(priv->base)) {
  2129. ret = PTR_ERR(priv->base);
  2130. goto err_free_netdev;
  2131. }
  2132. priv->netdev = dev;
  2133. priv->pdev = pdev;
  2134. ret = of_get_phy_mode(dn, &priv->phy_interface);
  2135. /* Default to GMII interface mode */
  2136. if (ret)
  2137. priv->phy_interface = PHY_INTERFACE_MODE_GMII;
  2138. /* In the case of a fixed PHY, the DT node associated
  2139. * to the PHY is the Ethernet MAC DT node.
  2140. */
  2141. if (of_phy_is_fixed_link(dn)) {
  2142. ret = of_phy_register_fixed_link(dn);
  2143. if (ret) {
  2144. dev_err(&pdev->dev, "failed to register fixed PHY\n");
  2145. goto err_free_netdev;
  2146. }
  2147. priv->phy_dn = dn;
  2148. }
  2149. /* Initialize netdevice members */
  2150. ret = of_get_ethdev_address(dn, dev);
  2151. if (ret) {
  2152. dev_warn(&pdev->dev, "using random Ethernet MAC\n");
  2153. eth_hw_addr_random(dev);
  2154. }
  2155. SET_NETDEV_DEV(dev, &pdev->dev);
  2156. dev_set_drvdata(&pdev->dev, dev);
  2157. dev->ethtool_ops = &bcm_sysport_ethtool_ops;
  2158. dev->netdev_ops = &bcm_sysport_netdev_ops;
  2159. netif_napi_add(dev, &priv->napi, bcm_sysport_poll);
  2160. dev->features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
  2161. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2162. NETIF_F_HW_VLAN_CTAG_TX;
  2163. dev->hw_features |= dev->features;
  2164. dev->vlan_features |= dev->features;
  2165. dev->max_mtu = UMAC_MAX_MTU_SIZE;
  2166. /* Request the WOL interrupt and advertise suspend if available */
  2167. priv->wol_irq_disabled = 1;
  2168. ret = devm_request_irq(&pdev->dev, priv->wol_irq,
  2169. bcm_sysport_wol_isr, 0, dev->name, priv);
  2170. if (!ret)
  2171. device_set_wakeup_capable(&pdev->dev, 1);
  2172. priv->wol_clk = devm_clk_get_optional(&pdev->dev, "sw_sysportwol");
  2173. if (IS_ERR(priv->wol_clk)) {
  2174. ret = PTR_ERR(priv->wol_clk);
  2175. goto err_deregister_fixed_link;
  2176. }
  2177. /* Set the needed headroom once and for all */
  2178. BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
  2179. dev->needed_headroom += sizeof(struct bcm_tsb);
  2180. /* libphy will adjust the link state accordingly */
  2181. netif_carrier_off(dev);
  2182. priv->rx_max_coalesced_frames = 1;
  2183. u64_stats_init(&priv->syncp);
  2184. priv->netdev_notifier.notifier_call = bcm_sysport_netdevice_event;
  2185. ret = register_netdevice_notifier(&priv->netdev_notifier);
  2186. if (ret) {
  2187. dev_err(&pdev->dev, "failed to register DSA notifier\n");
  2188. goto err_deregister_fixed_link;
  2189. }
  2190. ret = register_netdev(dev);
  2191. if (ret) {
  2192. dev_err(&pdev->dev, "failed to register net_device\n");
  2193. goto err_deregister_notifier;
  2194. }
  2195. ret = clk_prepare_enable(priv->clk);
  2196. if (ret) {
  2197. dev_err(&pdev->dev, "could not enable priv clock\n");
  2198. goto err_deregister_netdev;
  2199. }
  2200. priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
  2201. dev_info(&pdev->dev,
  2202. "Broadcom SYSTEMPORT%s " REV_FMT
  2203. " (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
  2204. priv->is_lite ? " Lite" : "",
  2205. (priv->rev >> 8) & 0xff, priv->rev & 0xff,
  2206. priv->irq0, priv->irq1, txq, rxq);
  2207. clk_disable_unprepare(priv->clk);
  2208. return 0;
  2209. err_deregister_netdev:
  2210. unregister_netdev(dev);
  2211. err_deregister_notifier:
  2212. unregister_netdevice_notifier(&priv->netdev_notifier);
  2213. err_deregister_fixed_link:
  2214. if (of_phy_is_fixed_link(dn))
  2215. of_phy_deregister_fixed_link(dn);
  2216. err_free_netdev:
  2217. free_netdev(dev);
  2218. return ret;
  2219. }
  2220. static void bcm_sysport_remove(struct platform_device *pdev)
  2221. {
  2222. struct net_device *dev = dev_get_drvdata(&pdev->dev);
  2223. struct bcm_sysport_priv *priv = netdev_priv(dev);
  2224. struct device_node *dn = pdev->dev.of_node;
  2225. /* Not much to do, ndo_close has been called
  2226. * and we use managed allocations
  2227. */
  2228. unregister_netdevice_notifier(&priv->netdev_notifier);
  2229. unregister_netdev(dev);
  2230. if (of_phy_is_fixed_link(dn))
  2231. of_phy_deregister_fixed_link(dn);
  2232. free_netdev(dev);
  2233. dev_set_drvdata(&pdev->dev, NULL);
  2234. }
  2235. static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
  2236. {
  2237. struct net_device *ndev = priv->netdev;
  2238. unsigned int timeout = 1000;
  2239. unsigned int index, i = 0;
  2240. u32 reg;
  2241. reg = umac_readl(priv, UMAC_MPD_CTRL);
  2242. if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE))
  2243. reg |= MPD_EN;
  2244. reg &= ~PSW_EN;
  2245. if (priv->wolopts & WAKE_MAGICSECURE) {
  2246. /* Program the SecureOn password */
  2247. umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
  2248. UMAC_PSW_MS);
  2249. umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
  2250. UMAC_PSW_LS);
  2251. reg |= PSW_EN;
  2252. }
  2253. umac_writel(priv, reg, UMAC_MPD_CTRL);
  2254. if (priv->wolopts & WAKE_FILTER) {
  2255. /* Turn on ACPI matching to steal packets from RBUF */
  2256. reg = rbuf_readl(priv, RBUF_CONTROL);
  2257. if (priv->is_lite)
  2258. reg |= RBUF_ACPI_EN_LITE;
  2259. else
  2260. reg |= RBUF_ACPI_EN;
  2261. rbuf_writel(priv, reg, RBUF_CONTROL);
  2262. /* Enable RXCHK, active filters and Broadcom tag matching */
  2263. reg = rxchk_readl(priv, RXCHK_CONTROL);
  2264. reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
  2265. RXCHK_BRCM_TAG_MATCH_SHIFT);
  2266. for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
  2267. reg |= BIT(RXCHK_BRCM_TAG_MATCH_SHIFT + i);
  2268. i++;
  2269. }
  2270. reg |= RXCHK_EN | RXCHK_BRCM_TAG_EN;
  2271. rxchk_writel(priv, reg, RXCHK_CONTROL);
  2272. }
  2273. /* Make sure RBUF entered WoL mode as result */
  2274. do {
  2275. reg = rbuf_readl(priv, RBUF_STATUS);
  2276. if (reg & RBUF_WOL_MODE)
  2277. break;
  2278. udelay(10);
  2279. } while (timeout-- > 0);
  2280. /* Do not leave the UniMAC RBUF matching only MPD packets */
  2281. if (!timeout) {
  2282. mpd_enable_set(priv, false);
  2283. netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
  2284. return -ETIMEDOUT;
  2285. }
  2286. /* UniMAC receive needs to be turned on */
  2287. umac_enable_set(priv, CMD_RX_EN, 1);
  2288. netif_dbg(priv, wol, ndev, "entered WOL mode\n");
  2289. return 0;
  2290. }
  2291. static int __maybe_unused bcm_sysport_suspend(struct device *d)
  2292. {
  2293. struct net_device *dev = dev_get_drvdata(d);
  2294. struct bcm_sysport_priv *priv = netdev_priv(dev);
  2295. unsigned int i;
  2296. int ret = 0;
  2297. u32 reg;
  2298. if (!netif_running(dev))
  2299. return 0;
  2300. netif_device_detach(dev);
  2301. bcm_sysport_netif_stop(dev);
  2302. phy_suspend(dev->phydev);
  2303. /* Disable UniMAC RX */
  2304. umac_enable_set(priv, CMD_RX_EN, 0);
  2305. ret = rdma_enable_set(priv, 0);
  2306. if (ret) {
  2307. netdev_err(dev, "RDMA timeout!\n");
  2308. return ret;
  2309. }
  2310. /* Disable RXCHK if enabled */
  2311. if (priv->rx_chk_en) {
  2312. reg = rxchk_readl(priv, RXCHK_CONTROL);
  2313. reg &= ~RXCHK_EN;
  2314. rxchk_writel(priv, reg, RXCHK_CONTROL);
  2315. }
  2316. /* Flush RX pipe */
  2317. if (!priv->wolopts)
  2318. topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
  2319. ret = tdma_enable_set(priv, 0);
  2320. if (ret) {
  2321. netdev_err(dev, "TDMA timeout!\n");
  2322. return ret;
  2323. }
  2324. /* Wait for a packet boundary */
  2325. usleep_range(2000, 3000);
  2326. umac_enable_set(priv, CMD_TX_EN, 0);
  2327. topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
  2328. /* Free RX/TX rings SW structures */
  2329. for (i = 0; i < dev->num_tx_queues; i++)
  2330. bcm_sysport_fini_tx_ring(priv, i);
  2331. bcm_sysport_fini_rx_ring(priv);
  2332. /* Get prepared for Wake-on-LAN */
  2333. if (device_may_wakeup(d) && priv->wolopts) {
  2334. clk_prepare_enable(priv->wol_clk);
  2335. ret = bcm_sysport_suspend_to_wol(priv);
  2336. }
  2337. clk_disable_unprepare(priv->clk);
  2338. return ret;
  2339. }
  2340. static int __maybe_unused bcm_sysport_resume(struct device *d)
  2341. {
  2342. struct net_device *dev = dev_get_drvdata(d);
  2343. struct bcm_sysport_priv *priv = netdev_priv(dev);
  2344. unsigned int i;
  2345. int ret;
  2346. if (!netif_running(dev))
  2347. return 0;
  2348. ret = clk_prepare_enable(priv->clk);
  2349. if (ret) {
  2350. netdev_err(dev, "could not enable priv clock\n");
  2351. return ret;
  2352. }
  2353. if (priv->wolopts)
  2354. clk_disable_unprepare(priv->wol_clk);
  2355. umac_reset(priv);
  2356. /* Disable the UniMAC RX/TX */
  2357. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
  2358. /* We may have been suspended and never received a WOL event that
  2359. * would turn off MPD detection, take care of that now
  2360. */
  2361. bcm_sysport_resume_from_wol(priv);
  2362. /* Initialize both hardware and software ring */
  2363. for (i = 0; i < dev->num_tx_queues; i++) {
  2364. ret = bcm_sysport_init_tx_ring(priv, i);
  2365. if (ret) {
  2366. netdev_err(dev, "failed to initialize TX ring %d\n",
  2367. i);
  2368. goto out_free_tx_rings;
  2369. }
  2370. }
  2371. /* Initialize linked-list */
  2372. tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
  2373. /* Initialize RX ring */
  2374. ret = bcm_sysport_init_rx_ring(priv);
  2375. if (ret) {
  2376. netdev_err(dev, "failed to initialize RX ring\n");
  2377. goto out_free_rx_ring;
  2378. }
  2379. /* RX pipe enable */
  2380. topctrl_writel(priv, 0, RX_FLUSH_CNTL);
  2381. ret = rdma_enable_set(priv, 1);
  2382. if (ret) {
  2383. netdev_err(dev, "failed to enable RDMA\n");
  2384. goto out_free_rx_ring;
  2385. }
  2386. /* Restore enabled features */
  2387. bcm_sysport_set_features(dev, dev->features);
  2388. rbuf_init(priv);
  2389. /* Set maximum frame length */
  2390. if (!priv->is_lite)
  2391. umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  2392. else
  2393. gib_set_pad_extension(priv);
  2394. /* Set MAC address */
  2395. umac_set_hw_addr(priv, dev->dev_addr);
  2396. umac_enable_set(priv, CMD_RX_EN, 1);
  2397. /* TX pipe enable */
  2398. topctrl_writel(priv, 0, TX_FLUSH_CNTL);
  2399. umac_enable_set(priv, CMD_TX_EN, 1);
  2400. ret = tdma_enable_set(priv, 1);
  2401. if (ret) {
  2402. netdev_err(dev, "TDMA timeout!\n");
  2403. goto out_free_rx_ring;
  2404. }
  2405. phy_resume(dev->phydev);
  2406. bcm_sysport_netif_start(dev);
  2407. netif_device_attach(dev);
  2408. return 0;
  2409. out_free_rx_ring:
  2410. bcm_sysport_fini_rx_ring(priv);
  2411. out_free_tx_rings:
  2412. for (i = 0; i < dev->num_tx_queues; i++)
  2413. bcm_sysport_fini_tx_ring(priv, i);
  2414. clk_disable_unprepare(priv->clk);
  2415. return ret;
  2416. }
  2417. static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
  2418. bcm_sysport_suspend, bcm_sysport_resume);
  2419. static struct platform_driver bcm_sysport_driver = {
  2420. .probe = bcm_sysport_probe,
  2421. .remove_new = bcm_sysport_remove,
  2422. .driver = {
  2423. .name = "brcm-systemport",
  2424. .of_match_table = bcm_sysport_of_match,
  2425. .pm = &bcm_sysport_pm_ops,
  2426. },
  2427. };
  2428. module_platform_driver(bcm_sysport_driver);
  2429. MODULE_AUTHOR("Broadcom Corporation");
  2430. MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
  2431. MODULE_ALIAS("platform:brcm-systemport");
  2432. MODULE_LICENSE("GPL");