2
0

dsa.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <net/dsa.h>
  3. #include <linux/if_bridge.h>
  4. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  5. #include "rtl83xx.h"
  6. extern struct rtl83xx_soc_info soc_info;
  7. static void rtl83xx_init_stats(struct rtl838x_switch_priv *priv)
  8. {
  9. mutex_lock(&priv->reg_mutex);
  10. /* Enable statistics module: all counters plus debug.
  11. * On RTL839x all counters are enabled by default
  12. */
  13. if (priv->family_id == RTL8380_FAMILY_ID)
  14. sw_w32_mask(0, 3, RTL838X_STAT_CTRL);
  15. /* Reset statistics counters */
  16. sw_w32_mask(0, 1, priv->r->stat_rst);
  17. mutex_unlock(&priv->reg_mutex);
  18. }
  19. static void rtl83xx_enable_phy_polling(struct rtl838x_switch_priv *priv)
  20. {
  21. int i;
  22. u64 v = 0;
  23. msleep(1000);
  24. /* Enable all ports with a PHY, including the SFP-ports */
  25. for (i = 0; i < priv->cpu_port; i++) {
  26. if (priv->ports[i].phy)
  27. v |= BIT_ULL(i);
  28. }
  29. pr_info("%s: %16llx\n", __func__, v);
  30. priv->r->set_port_reg_le(v, priv->r->smi_poll_ctrl);
  31. /* PHY update complete, there is no global PHY polling enable bit on the 9300 */
  32. if (priv->family_id == RTL8390_FAMILY_ID)
  33. sw_w32_mask(0, BIT(7), RTL839X_SMI_GLB_CTRL);
  34. else if(priv->family_id == RTL9300_FAMILY_ID)
  35. sw_w32_mask(0, 0x8000, RTL838X_SMI_GLB_CTRL);
  36. }
  37. const struct rtl83xx_mib_desc rtl83xx_mib[] = {
  38. MIB_DESC(2, 0xf8, "ifInOctets"),
  39. MIB_DESC(2, 0xf0, "ifOutOctets"),
  40. MIB_DESC(1, 0xec, "dot1dTpPortInDiscards"),
  41. MIB_DESC(1, 0xe8, "ifInUcastPkts"),
  42. MIB_DESC(1, 0xe4, "ifInMulticastPkts"),
  43. MIB_DESC(1, 0xe0, "ifInBroadcastPkts"),
  44. MIB_DESC(1, 0xdc, "ifOutUcastPkts"),
  45. MIB_DESC(1, 0xd8, "ifOutMulticastPkts"),
  46. MIB_DESC(1, 0xd4, "ifOutBroadcastPkts"),
  47. MIB_DESC(1, 0xd0, "ifOutDiscards"),
  48. MIB_DESC(1, 0xcc, ".3SingleCollisionFrames"),
  49. MIB_DESC(1, 0xc8, ".3MultipleCollisionFrames"),
  50. MIB_DESC(1, 0xc4, ".3DeferredTransmissions"),
  51. MIB_DESC(1, 0xc0, ".3LateCollisions"),
  52. MIB_DESC(1, 0xbc, ".3ExcessiveCollisions"),
  53. MIB_DESC(1, 0xb8, ".3SymbolErrors"),
  54. MIB_DESC(1, 0xb4, ".3ControlInUnknownOpcodes"),
  55. MIB_DESC(1, 0xb0, ".3InPauseFrames"),
  56. MIB_DESC(1, 0xac, ".3OutPauseFrames"),
  57. MIB_DESC(1, 0xa8, "DropEvents"),
  58. MIB_DESC(1, 0xa4, "tx_BroadcastPkts"),
  59. MIB_DESC(1, 0xa0, "tx_MulticastPkts"),
  60. MIB_DESC(1, 0x9c, "CRCAlignErrors"),
  61. MIB_DESC(1, 0x98, "tx_UndersizePkts"),
  62. MIB_DESC(1, 0x94, "rx_UndersizePkts"),
  63. MIB_DESC(1, 0x90, "rx_UndersizedropPkts"),
  64. MIB_DESC(1, 0x8c, "tx_OversizePkts"),
  65. MIB_DESC(1, 0x88, "rx_OversizePkts"),
  66. MIB_DESC(1, 0x84, "Fragments"),
  67. MIB_DESC(1, 0x80, "Jabbers"),
  68. MIB_DESC(1, 0x7c, "Collisions"),
  69. MIB_DESC(1, 0x78, "tx_Pkts64Octets"),
  70. MIB_DESC(1, 0x74, "rx_Pkts64Octets"),
  71. MIB_DESC(1, 0x70, "tx_Pkts65to127Octets"),
  72. MIB_DESC(1, 0x6c, "rx_Pkts65to127Octets"),
  73. MIB_DESC(1, 0x68, "tx_Pkts128to255Octets"),
  74. MIB_DESC(1, 0x64, "rx_Pkts128to255Octets"),
  75. MIB_DESC(1, 0x60, "tx_Pkts256to511Octets"),
  76. MIB_DESC(1, 0x5c, "rx_Pkts256to511Octets"),
  77. MIB_DESC(1, 0x58, "tx_Pkts512to1023Octets"),
  78. MIB_DESC(1, 0x54, "rx_Pkts512to1023Octets"),
  79. MIB_DESC(1, 0x50, "tx_Pkts1024to1518Octets"),
  80. MIB_DESC(1, 0x4c, "rx_StatsPkts1024to1518Octets"),
  81. MIB_DESC(1, 0x48, "tx_Pkts1519toMaxOctets"),
  82. MIB_DESC(1, 0x44, "rx_Pkts1519toMaxOctets"),
  83. MIB_DESC(1, 0x40, "rxMacDiscards")
  84. };
  85. /* DSA callbacks */
  86. static enum dsa_tag_protocol rtl83xx_get_tag_protocol(struct dsa_switch *ds,
  87. int port,
  88. enum dsa_tag_protocol mprot)
  89. {
  90. /* The switch does not tag the frames, instead internally the header
  91. * structure for each packet is tagged accordingly.
  92. */
  93. return DSA_TAG_PROTO_TRAILER;
  94. }
  95. /*
  96. * Initialize all VLANS
  97. */
  98. static void rtl83xx_vlan_setup(struct rtl838x_switch_priv *priv)
  99. {
  100. struct rtl838x_vlan_info info;
  101. int i;
  102. pr_info("In %s\n", __func__);
  103. priv->r->vlan_profile_setup(0);
  104. priv->r->vlan_profile_setup(1);
  105. pr_info("UNKNOWN_MC_PMASK: %016llx\n", priv->r->read_mcast_pmask(UNKNOWN_MC_PMASK));
  106. priv->r->vlan_profile_dump(0);
  107. info.fid = 0; // Default Forwarding ID / MSTI
  108. info.hash_uc_fid = false; // Do not build the L2 lookup hash with FID, but VID
  109. info.hash_mc_fid = false; // Do the same for Multicast packets
  110. info.profile_id = 0; // Use default Vlan Profile 0
  111. info.tagged_ports = 0; // Initially no port members
  112. if (priv->family_id == RTL9310_FAMILY_ID) {
  113. info.if_id = 0;
  114. info.multicast_grp_mask = 0;
  115. info.l2_tunnel_list_id = -1;
  116. }
  117. // Initialize all vlans 0-4095
  118. for (i = 0; i < MAX_VLANS; i ++)
  119. priv->r->vlan_set_tagged(i, &info);
  120. // reset PVIDs; defaults to 1 on reset
  121. for (i = 0; i <= priv->ds->num_ports; i++) {
  122. priv->r->vlan_port_pvid_set(i, PBVLAN_TYPE_INNER, 0);
  123. priv->r->vlan_port_pvid_set(i, PBVLAN_TYPE_OUTER, 0);
  124. priv->r->vlan_port_pvidmode_set(i, PBVLAN_TYPE_INNER, PBVLAN_MODE_UNTAG_AND_PRITAG);
  125. priv->r->vlan_port_pvidmode_set(i, PBVLAN_TYPE_OUTER, PBVLAN_MODE_UNTAG_AND_PRITAG);
  126. }
  127. // Set forwarding action based on inner VLAN tag
  128. for (i = 0; i < priv->cpu_port; i++)
  129. priv->r->vlan_fwd_on_inner(i, true);
  130. }
  131. static void rtl83xx_setup_bpdu_traps(struct rtl838x_switch_priv *priv)
  132. {
  133. int i;
  134. for (i = 0; i < priv->cpu_port; i++)
  135. priv->r->set_receive_management_action(i, BPDU, COPY2CPU);
  136. }
  137. static int rtl83xx_setup(struct dsa_switch *ds)
  138. {
  139. int i;
  140. struct rtl838x_switch_priv *priv = ds->priv;
  141. u64 port_bitmap = BIT_ULL(priv->cpu_port);
  142. pr_debug("%s called\n", __func__);
  143. /* Disable MAC polling the PHY so that we can start configuration */
  144. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  145. for (i = 0; i < ds->num_ports; i++)
  146. priv->ports[i].enable = false;
  147. priv->ports[priv->cpu_port].enable = true;
  148. /* Isolate ports from each other: traffic only CPU <-> port */
  149. /* Setting bit j in register RTL838X_PORT_ISO_CTRL(i) allows
  150. * traffic from source port i to destination port j
  151. */
  152. for (i = 0; i < priv->cpu_port; i++) {
  153. if (priv->ports[i].phy) {
  154. priv->r->set_port_reg_be(BIT_ULL(priv->cpu_port) | BIT_ULL(i),
  155. priv->r->port_iso_ctrl(i));
  156. port_bitmap |= BIT_ULL(i);
  157. }
  158. }
  159. priv->r->set_port_reg_be(port_bitmap, priv->r->port_iso_ctrl(priv->cpu_port));
  160. if (priv->family_id == RTL8380_FAMILY_ID)
  161. rtl838x_print_matrix();
  162. else
  163. rtl839x_print_matrix();
  164. rtl83xx_init_stats(priv);
  165. rtl83xx_vlan_setup(priv);
  166. rtl83xx_setup_bpdu_traps(priv);
  167. ds->configure_vlan_while_not_filtering = true;
  168. priv->r->l2_learning_setup();
  169. /*
  170. * Make sure all frames sent to the switch's MAC are trapped to the CPU-port
  171. * 0: FWD, 1: DROP, 2: TRAP2CPU
  172. */
  173. if (priv->family_id == RTL8380_FAMILY_ID)
  174. sw_w32(0x2, RTL838X_SPCL_TRAP_SWITCH_MAC_CTRL);
  175. else
  176. sw_w32(0x2, RTL839X_SPCL_TRAP_SWITCH_MAC_CTRL);
  177. /* Enable MAC Polling PHY again */
  178. rtl83xx_enable_phy_polling(priv);
  179. pr_debug("Please wait until PHY is settled\n");
  180. msleep(1000);
  181. priv->r->pie_init(priv);
  182. return 0;
  183. }
  184. static int rtl93xx_setup(struct dsa_switch *ds)
  185. {
  186. int i;
  187. struct rtl838x_switch_priv *priv = ds->priv;
  188. u32 port_bitmap = BIT(priv->cpu_port);
  189. pr_info("%s called\n", __func__);
  190. /* Disable MAC polling the PHY so that we can start configuration */
  191. if (priv->family_id == RTL9300_FAMILY_ID)
  192. sw_w32(0, RTL930X_SMI_POLL_CTRL);
  193. if (priv->family_id == RTL9310_FAMILY_ID) {
  194. sw_w32(0, RTL931X_SMI_PORT_POLLING_CTRL);
  195. sw_w32(0, RTL931X_SMI_PORT_POLLING_CTRL + 4);
  196. }
  197. // Disable all ports except CPU port
  198. for (i = 0; i < ds->num_ports; i++)
  199. priv->ports[i].enable = false;
  200. priv->ports[priv->cpu_port].enable = true;
  201. for (i = 0; i < priv->cpu_port; i++) {
  202. if (priv->ports[i].phy) {
  203. priv->r->traffic_set(i, BIT_ULL(priv->cpu_port) | BIT_ULL(i));
  204. port_bitmap |= BIT_ULL(i);
  205. }
  206. }
  207. priv->r->traffic_set(priv->cpu_port, port_bitmap);
  208. rtl930x_print_matrix();
  209. // TODO: Initialize statistics
  210. rtl83xx_vlan_setup(priv);
  211. ds->configure_vlan_while_not_filtering = true;
  212. priv->r->l2_learning_setup();
  213. rtl83xx_enable_phy_polling(priv);
  214. priv->r->pie_init(priv);
  215. priv->r->led_init(priv);
  216. return 0;
  217. }
  218. static int rtl93xx_get_sds(struct phy_device *phydev)
  219. {
  220. struct device *dev = &phydev->mdio.dev;
  221. struct device_node *dn;
  222. u32 sds_num;
  223. if (!dev)
  224. return -1;
  225. if (dev->of_node) {
  226. dn = dev->of_node;
  227. if (of_property_read_u32(dn, "sds", &sds_num))
  228. sds_num = -1;
  229. } else {
  230. dev_err(dev, "No DT node.\n");
  231. return -1;
  232. }
  233. return sds_num;
  234. }
  235. static void rtl83xx_phylink_validate(struct dsa_switch *ds, int port,
  236. unsigned long *supported,
  237. struct phylink_link_state *state)
  238. {
  239. struct rtl838x_switch_priv *priv = ds->priv;
  240. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  241. pr_debug("In %s port %d, state is %d", __func__, port, state->interface);
  242. if (!phy_interface_mode_is_rgmii(state->interface) &&
  243. state->interface != PHY_INTERFACE_MODE_NA &&
  244. state->interface != PHY_INTERFACE_MODE_1000BASEX &&
  245. state->interface != PHY_INTERFACE_MODE_MII &&
  246. state->interface != PHY_INTERFACE_MODE_REVMII &&
  247. state->interface != PHY_INTERFACE_MODE_GMII &&
  248. state->interface != PHY_INTERFACE_MODE_QSGMII &&
  249. state->interface != PHY_INTERFACE_MODE_INTERNAL &&
  250. state->interface != PHY_INTERFACE_MODE_SGMII) {
  251. bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  252. dev_err(ds->dev,
  253. "Unsupported interface: %d for port %d\n",
  254. state->interface, port);
  255. return;
  256. }
  257. /* Allow all the expected bits */
  258. phylink_set(mask, Autoneg);
  259. phylink_set_port_modes(mask);
  260. phylink_set(mask, Pause);
  261. phylink_set(mask, Asym_Pause);
  262. /* With the exclusion of MII and Reverse MII, we support Gigabit,
  263. * including Half duplex
  264. */
  265. if (state->interface != PHY_INTERFACE_MODE_MII &&
  266. state->interface != PHY_INTERFACE_MODE_REVMII) {
  267. phylink_set(mask, 1000baseT_Full);
  268. phylink_set(mask, 1000baseT_Half);
  269. }
  270. /* On both the 8380 and 8382, ports 24-27 are SFP ports */
  271. if (port >= 24 && port <= 27 && priv->family_id == RTL8380_FAMILY_ID)
  272. phylink_set(mask, 1000baseX_Full);
  273. /* On the RTL839x family of SoCs, ports 48 to 51 are SFP ports */
  274. if (port >= 48 && port <= 51 && priv->family_id == RTL8390_FAMILY_ID)
  275. phylink_set(mask, 1000baseX_Full);
  276. phylink_set(mask, 10baseT_Half);
  277. phylink_set(mask, 10baseT_Full);
  278. phylink_set(mask, 100baseT_Half);
  279. phylink_set(mask, 100baseT_Full);
  280. bitmap_and(supported, supported, mask,
  281. __ETHTOOL_LINK_MODE_MASK_NBITS);
  282. bitmap_and(state->advertising, state->advertising, mask,
  283. __ETHTOOL_LINK_MODE_MASK_NBITS);
  284. }
  285. static void rtl93xx_phylink_validate(struct dsa_switch *ds, int port,
  286. unsigned long *supported,
  287. struct phylink_link_state *state)
  288. {
  289. struct rtl838x_switch_priv *priv = ds->priv;
  290. __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  291. pr_debug("In %s port %d, state is %d (%s)", __func__, port, state->interface,
  292. phy_modes(state->interface));
  293. if (!phy_interface_mode_is_rgmii(state->interface) &&
  294. state->interface != PHY_INTERFACE_MODE_NA &&
  295. state->interface != PHY_INTERFACE_MODE_1000BASEX &&
  296. state->interface != PHY_INTERFACE_MODE_MII &&
  297. state->interface != PHY_INTERFACE_MODE_REVMII &&
  298. state->interface != PHY_INTERFACE_MODE_GMII &&
  299. state->interface != PHY_INTERFACE_MODE_QSGMII &&
  300. state->interface != PHY_INTERFACE_MODE_XGMII &&
  301. state->interface != PHY_INTERFACE_MODE_HSGMII &&
  302. state->interface != PHY_INTERFACE_MODE_10GBASER &&
  303. state->interface != PHY_INTERFACE_MODE_10GKR &&
  304. state->interface != PHY_INTERFACE_MODE_USXGMII &&
  305. state->interface != PHY_INTERFACE_MODE_INTERNAL &&
  306. state->interface != PHY_INTERFACE_MODE_SGMII) {
  307. bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
  308. dev_err(ds->dev,
  309. "Unsupported interface: %d for port %d\n",
  310. state->interface, port);
  311. return;
  312. }
  313. /* Allow all the expected bits */
  314. phylink_set(mask, Autoneg);
  315. phylink_set_port_modes(mask);
  316. phylink_set(mask, Pause);
  317. phylink_set(mask, Asym_Pause);
  318. /* With the exclusion of MII and Reverse MII, we support Gigabit,
  319. * including Half duplex
  320. */
  321. if (state->interface != PHY_INTERFACE_MODE_MII &&
  322. state->interface != PHY_INTERFACE_MODE_REVMII) {
  323. phylink_set(mask, 1000baseT_Full);
  324. phylink_set(mask, 1000baseT_Half);
  325. }
  326. // Internal phys of the RTL93xx family provide 10G
  327. if (priv->ports[port].phy_is_integrated
  328. && state->interface == PHY_INTERFACE_MODE_1000BASEX) {
  329. phylink_set(mask, 1000baseX_Full);
  330. } else if (priv->ports[port].phy_is_integrated) {
  331. phylink_set(mask, 1000baseX_Full);
  332. phylink_set(mask, 10000baseKR_Full);
  333. phylink_set(mask, 10000baseSR_Full);
  334. phylink_set(mask, 10000baseCR_Full);
  335. }
  336. if (state->interface == PHY_INTERFACE_MODE_INTERNAL) {
  337. phylink_set(mask, 1000baseX_Full);
  338. phylink_set(mask, 1000baseT_Full);
  339. phylink_set(mask, 10000baseKR_Full);
  340. phylink_set(mask, 10000baseT_Full);
  341. phylink_set(mask, 10000baseSR_Full);
  342. phylink_set(mask, 10000baseCR_Full);
  343. }
  344. if (state->interface == PHY_INTERFACE_MODE_USXGMII)
  345. phylink_set(mask, 10000baseT_Full);
  346. phylink_set(mask, 10baseT_Half);
  347. phylink_set(mask, 10baseT_Full);
  348. phylink_set(mask, 100baseT_Half);
  349. phylink_set(mask, 100baseT_Full);
  350. bitmap_and(supported, supported, mask,
  351. __ETHTOOL_LINK_MODE_MASK_NBITS);
  352. bitmap_and(state->advertising, state->advertising, mask,
  353. __ETHTOOL_LINK_MODE_MASK_NBITS);
  354. pr_debug("%s leaving supported: %*pb", __func__, __ETHTOOL_LINK_MODE_MASK_NBITS, supported);
  355. }
  356. static int rtl83xx_phylink_mac_link_state(struct dsa_switch *ds, int port,
  357. struct phylink_link_state *state)
  358. {
  359. struct rtl838x_switch_priv *priv = ds->priv;
  360. u64 speed;
  361. u64 link;
  362. if (port < 0 || port > priv->cpu_port)
  363. return -EINVAL;
  364. state->link = 0;
  365. link = priv->r->get_port_reg_le(priv->r->mac_link_sts);
  366. if (link & BIT_ULL(port))
  367. state->link = 1;
  368. pr_debug("%s: link state port %d: %llx\n", __func__, port, link & BIT_ULL(port));
  369. state->duplex = 0;
  370. if (priv->r->get_port_reg_le(priv->r->mac_link_dup_sts) & BIT_ULL(port))
  371. state->duplex = 1;
  372. speed = priv->r->get_port_reg_le(priv->r->mac_link_spd_sts(port));
  373. speed >>= (port % 16) << 1;
  374. switch (speed & 0x3) {
  375. case 0:
  376. state->speed = SPEED_10;
  377. break;
  378. case 1:
  379. state->speed = SPEED_100;
  380. break;
  381. case 2:
  382. state->speed = SPEED_1000;
  383. break;
  384. case 3:
  385. if (priv->family_id == RTL9300_FAMILY_ID
  386. && (port == 24 || port == 26)) /* Internal serdes */
  387. state->speed = SPEED_2500;
  388. else
  389. state->speed = SPEED_100; /* Is in fact 500Mbit */
  390. }
  391. state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
  392. if (priv->r->get_port_reg_le(priv->r->mac_rx_pause_sts) & BIT_ULL(port))
  393. state->pause |= MLO_PAUSE_RX;
  394. if (priv->r->get_port_reg_le(priv->r->mac_tx_pause_sts) & BIT_ULL(port))
  395. state->pause |= MLO_PAUSE_TX;
  396. return 1;
  397. }
  398. static int rtl93xx_phylink_mac_link_state(struct dsa_switch *ds, int port,
  399. struct phylink_link_state *state)
  400. {
  401. struct rtl838x_switch_priv *priv = ds->priv;
  402. u64 speed;
  403. u64 link;
  404. u64 media;
  405. if (port < 0 || port > priv->cpu_port)
  406. return -EINVAL;
  407. /*
  408. * On the RTL9300 for at least the RTL8226B PHY, the MAC-side link
  409. * state needs to be read twice in order to read a correct result.
  410. * This would not be necessary for ports connected e.g. to RTL8218D
  411. * PHYs.
  412. */
  413. state->link = 0;
  414. link = priv->r->get_port_reg_le(priv->r->mac_link_sts);
  415. link = priv->r->get_port_reg_le(priv->r->mac_link_sts);
  416. if (link & BIT_ULL(port))
  417. state->link = 1;
  418. if (priv->family_id == RTL9310_FAMILY_ID)
  419. media = priv->r->get_port_reg_le(RTL931X_MAC_LINK_MEDIA_STS);
  420. if (priv->family_id == RTL9300_FAMILY_ID)
  421. media = sw_r32(RTL930X_MAC_LINK_MEDIA_STS);
  422. if (media & BIT_ULL(port))
  423. state->link = 1;
  424. pr_debug("%s: link state port %d: %llx, media %llx\n", __func__, port,
  425. link & BIT_ULL(port), media);
  426. state->duplex = 0;
  427. if (priv->r->get_port_reg_le(priv->r->mac_link_dup_sts) & BIT_ULL(port))
  428. state->duplex = 1;
  429. speed = priv->r->get_port_reg_le(priv->r->mac_link_spd_sts(port));
  430. speed >>= (port % 8) << 2;
  431. switch (speed & 0xf) {
  432. case 0:
  433. state->speed = SPEED_10;
  434. break;
  435. case 1:
  436. state->speed = SPEED_100;
  437. break;
  438. case 2:
  439. case 7:
  440. state->speed = SPEED_1000;
  441. break;
  442. case 4:
  443. state->speed = SPEED_10000;
  444. break;
  445. case 5:
  446. case 8:
  447. state->speed = SPEED_2500;
  448. break;
  449. case 6:
  450. state->speed = SPEED_5000;
  451. break;
  452. default:
  453. pr_err("%s: unknown speed: %d\n", __func__, (u32)speed & 0xf);
  454. }
  455. if (priv->family_id == RTL9310_FAMILY_ID
  456. && (port >= 52 || port <= 55)) { /* Internal serdes */
  457. state->speed = SPEED_10000;
  458. state->link = 1;
  459. state->duplex = 1;
  460. }
  461. pr_debug("%s: speed is: %d %d\n", __func__, (u32)speed & 0xf, state->speed);
  462. state->pause &= (MLO_PAUSE_RX | MLO_PAUSE_TX);
  463. if (priv->r->get_port_reg_le(priv->r->mac_rx_pause_sts) & BIT_ULL(port))
  464. state->pause |= MLO_PAUSE_RX;
  465. if (priv->r->get_port_reg_le(priv->r->mac_tx_pause_sts) & BIT_ULL(port))
  466. state->pause |= MLO_PAUSE_TX;
  467. return 1;
  468. }
  469. static void rtl83xx_config_interface(int port, phy_interface_t interface)
  470. {
  471. u32 old, int_shift, sds_shift;
  472. switch (port) {
  473. case 24:
  474. int_shift = 0;
  475. sds_shift = 5;
  476. break;
  477. case 26:
  478. int_shift = 3;
  479. sds_shift = 0;
  480. break;
  481. default:
  482. return;
  483. }
  484. old = sw_r32(RTL838X_SDS_MODE_SEL);
  485. switch (interface) {
  486. case PHY_INTERFACE_MODE_1000BASEX:
  487. if ((old >> sds_shift & 0x1f) == 4)
  488. return;
  489. sw_w32_mask(0x7 << int_shift, 1 << int_shift, RTL838X_INT_MODE_CTRL);
  490. sw_w32_mask(0x1f << sds_shift, 4 << sds_shift, RTL838X_SDS_MODE_SEL);
  491. break;
  492. case PHY_INTERFACE_MODE_SGMII:
  493. if ((old >> sds_shift & 0x1f) == 2)
  494. return;
  495. sw_w32_mask(0x7 << int_shift, 2 << int_shift, RTL838X_INT_MODE_CTRL);
  496. sw_w32_mask(0x1f << sds_shift, 2 << sds_shift, RTL838X_SDS_MODE_SEL);
  497. break;
  498. default:
  499. return;
  500. }
  501. pr_debug("configured port %d for interface %s\n", port, phy_modes(interface));
  502. }
  503. static void rtl83xx_phylink_mac_config(struct dsa_switch *ds, int port,
  504. unsigned int mode,
  505. const struct phylink_link_state *state)
  506. {
  507. struct rtl838x_switch_priv *priv = ds->priv;
  508. u32 reg;
  509. int speed_bit = priv->family_id == RTL8380_FAMILY_ID ? 4 : 3;
  510. pr_debug("%s port %d, mode %x\n", __func__, port, mode);
  511. if (port == priv->cpu_port) {
  512. /* Set Speed, duplex, flow control
  513. * FORCE_EN | LINK_EN | NWAY_EN | DUP_SEL
  514. * | SPD_SEL = 0b10 | FORCE_FC_EN | PHY_MASTER_SLV_MANUAL_EN
  515. * | MEDIA_SEL
  516. */
  517. if (priv->family_id == RTL8380_FAMILY_ID) {
  518. sw_w32(0x6192F, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  519. /* allow CRC errors on CPU-port */
  520. sw_w32_mask(0, 0x8, RTL838X_MAC_PORT_CTRL(priv->cpu_port));
  521. } else {
  522. sw_w32_mask(0, 3, priv->r->mac_force_mode_ctrl(priv->cpu_port));
  523. }
  524. return;
  525. }
  526. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  527. /* Auto-Negotiation does not work for MAC in RTL8390 */
  528. if (priv->family_id == RTL8380_FAMILY_ID) {
  529. if (mode == MLO_AN_PHY || phylink_autoneg_inband(mode)) {
  530. pr_debug("PHY autonegotiates\n");
  531. reg |= RTL838X_NWAY_EN;
  532. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  533. rtl83xx_config_interface(port, state->interface);
  534. return;
  535. }
  536. }
  537. if (mode != MLO_AN_FIXED)
  538. pr_debug("Fixed state.\n");
  539. /* Clear id_mode_dis bit, and the existing port mode, let
  540. * RGMII_MODE_EN bet set by mac_link_{up,down} */
  541. if (priv->family_id == RTL8380_FAMILY_ID) {
  542. reg &= ~(RTL838X_RX_PAUSE_EN | RTL838X_TX_PAUSE_EN);
  543. if (state->pause & MLO_PAUSE_TXRX_MASK) {
  544. if (state->pause & MLO_PAUSE_TX)
  545. reg |= RTL838X_TX_PAUSE_EN;
  546. reg |= RTL838X_RX_PAUSE_EN;
  547. }
  548. } else if (priv->family_id == RTL8390_FAMILY_ID) {
  549. reg &= ~(RTL839X_RX_PAUSE_EN | RTL839X_TX_PAUSE_EN);
  550. if (state->pause & MLO_PAUSE_TXRX_MASK) {
  551. if (state->pause & MLO_PAUSE_TX)
  552. reg |= RTL839X_TX_PAUSE_EN;
  553. reg |= RTL839X_RX_PAUSE_EN;
  554. }
  555. }
  556. reg &= ~(3 << speed_bit);
  557. switch (state->speed) {
  558. case SPEED_1000:
  559. reg |= 2 << speed_bit;
  560. break;
  561. case SPEED_100:
  562. reg |= 1 << speed_bit;
  563. break;
  564. default:
  565. break; // Ignore, including 10MBit which has a speed value of 0
  566. }
  567. if (priv->family_id == RTL8380_FAMILY_ID) {
  568. reg &= ~(RTL838X_DUPLEX_MODE | RTL838X_FORCE_LINK_EN);
  569. if (state->link)
  570. reg |= RTL838X_FORCE_LINK_EN;
  571. if (state->duplex == RTL838X_DUPLEX_MODE)
  572. reg |= RTL838X_DUPLEX_MODE;
  573. } else if (priv->family_id == RTL8390_FAMILY_ID) {
  574. reg &= ~(RTL839X_DUPLEX_MODE | RTL839X_FORCE_LINK_EN);
  575. if (state->link)
  576. reg |= RTL839X_FORCE_LINK_EN;
  577. if (state->duplex == RTL839X_DUPLEX_MODE)
  578. reg |= RTL839X_DUPLEX_MODE;
  579. }
  580. // LAG members must use DUPLEX and we need to enable the link
  581. if (priv->lagmembers & BIT_ULL(port)) {
  582. switch(priv->family_id) {
  583. case RTL8380_FAMILY_ID:
  584. reg |= (RTL838X_DUPLEX_MODE | RTL838X_FORCE_LINK_EN);
  585. break;
  586. case RTL8390_FAMILY_ID:
  587. reg |= (RTL839X_DUPLEX_MODE | RTL839X_FORCE_LINK_EN);
  588. break;
  589. }
  590. }
  591. // Disable AN
  592. if (priv->family_id == RTL8380_FAMILY_ID)
  593. reg &= ~RTL838X_NWAY_EN;
  594. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  595. }
  596. static void rtl931x_phylink_mac_config(struct dsa_switch *ds, int port,
  597. unsigned int mode,
  598. const struct phylink_link_state *state)
  599. {
  600. struct rtl838x_switch_priv *priv = ds->priv;
  601. int sds_num;
  602. u32 reg, band;
  603. sds_num = priv->ports[port].sds_num;
  604. pr_info("%s: speed %d sds_num %d\n", __func__, state->speed, sds_num);
  605. switch (state->interface) {
  606. case PHY_INTERFACE_MODE_HSGMII:
  607. pr_info("%s setting mode PHY_INTERFACE_MODE_HSGMII\n", __func__);
  608. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_HSGMII);
  609. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_HSGMII);
  610. band = rtl931x_sds_cmu_band_set(sds_num, true, 62, PHY_INTERFACE_MODE_HSGMII);
  611. break;
  612. case PHY_INTERFACE_MODE_1000BASEX:
  613. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_1000BASEX);
  614. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_1000BASEX);
  615. break;
  616. case PHY_INTERFACE_MODE_XGMII:
  617. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_XGMII);
  618. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_XGMII);
  619. break;
  620. case PHY_INTERFACE_MODE_10GBASER:
  621. case PHY_INTERFACE_MODE_10GKR:
  622. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_10GBASER);
  623. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_10GBASER);
  624. break;
  625. case PHY_INTERFACE_MODE_USXGMII:
  626. // Translates to MII_USXGMII_10GSXGMII
  627. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_USXGMII);
  628. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_USXGMII);
  629. break;
  630. case PHY_INTERFACE_MODE_SGMII:
  631. pr_info("%s setting mode PHY_INTERFACE_MODE_SGMII\n", __func__);
  632. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_SGMII);
  633. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_SGMII);
  634. band = rtl931x_sds_cmu_band_set(sds_num, true, 62, PHY_INTERFACE_MODE_SGMII);
  635. break;
  636. case PHY_INTERFACE_MODE_QSGMII:
  637. band = rtl931x_sds_cmu_band_get(sds_num, PHY_INTERFACE_MODE_QSGMII);
  638. rtl931x_sds_init(sds_num, PHY_INTERFACE_MODE_QSGMII);
  639. break;
  640. default:
  641. pr_err("%s: unknown serdes mode: %s\n",
  642. __func__, phy_modes(state->interface));
  643. return;
  644. }
  645. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  646. pr_info("%s reading FORCE_MODE_CTRL: %08x\n", __func__, reg);
  647. reg &= ~(RTL931X_DUPLEX_MODE | RTL931X_FORCE_EN | RTL931X_FORCE_LINK_EN);
  648. reg &= ~(0xf << 12);
  649. reg |= 0x2 << 12; // Set SMI speed to 0x2
  650. reg |= RTL931X_TX_PAUSE_EN | RTL931X_RX_PAUSE_EN;
  651. if (priv->lagmembers & BIT_ULL(port))
  652. reg |= RTL931X_DUPLEX_MODE;
  653. if (state->duplex == DUPLEX_FULL)
  654. reg |= RTL931X_DUPLEX_MODE;
  655. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  656. }
  657. static void rtl93xx_phylink_mac_config(struct dsa_switch *ds, int port,
  658. unsigned int mode,
  659. const struct phylink_link_state *state)
  660. {
  661. struct rtl838x_switch_priv *priv = ds->priv;
  662. int sds_num, sds_mode;
  663. u32 reg;
  664. pr_info("%s port %d, mode %x, phy-mode: %s, speed %d, link %d\n", __func__,
  665. port, mode, phy_modes(state->interface), state->speed, state->link);
  666. // Nothing to be done for the CPU-port
  667. if (port == priv->cpu_port)
  668. return;
  669. if (priv->family_id == RTL9310_FAMILY_ID)
  670. return rtl931x_phylink_mac_config(ds, port, mode, state);
  671. sds_num = priv->ports[port].sds_num;
  672. pr_info("%s SDS is %d\n", __func__, sds_num);
  673. if (sds_num >= 0) {
  674. switch (state->interface) {
  675. case PHY_INTERFACE_MODE_HSGMII:
  676. sds_mode = 0x12;
  677. break;
  678. case PHY_INTERFACE_MODE_1000BASEX:
  679. sds_mode = 0x04;
  680. break;
  681. case PHY_INTERFACE_MODE_XGMII:
  682. sds_mode = 0x10;
  683. break;
  684. case PHY_INTERFACE_MODE_10GBASER:
  685. case PHY_INTERFACE_MODE_10GKR:
  686. sds_mode = 0x1b; // 10G 1000X Auto
  687. break;
  688. case PHY_INTERFACE_MODE_USXGMII:
  689. sds_mode = 0x0d;
  690. break;
  691. default:
  692. pr_err("%s: unknown serdes mode: %s\n",
  693. __func__, phy_modes(state->interface));
  694. return;
  695. }
  696. if (state->interface == PHY_INTERFACE_MODE_10GBASER)
  697. rtl9300_serdes_setup(sds_num, state->interface);
  698. }
  699. reg = sw_r32(priv->r->mac_force_mode_ctrl(port));
  700. reg &= ~(0xf << 3);
  701. switch (state->speed) {
  702. case SPEED_10000:
  703. reg |= 4 << 3;
  704. break;
  705. case SPEED_5000:
  706. reg |= 6 << 3;
  707. break;
  708. case SPEED_2500:
  709. reg |= 5 << 3;
  710. break;
  711. case SPEED_1000:
  712. reg |= 2 << 3;
  713. break;
  714. default:
  715. reg |= 2 << 3;
  716. break;
  717. }
  718. if (state->link)
  719. reg |= RTL930X_FORCE_LINK_EN;
  720. if (priv->lagmembers & BIT_ULL(port))
  721. reg |= RTL930X_DUPLEX_MODE | RTL930X_FORCE_LINK_EN;
  722. if (state->duplex == DUPLEX_FULL)
  723. reg |= RTL930X_DUPLEX_MODE;
  724. if (priv->ports[port].phy_is_integrated)
  725. reg &= ~RTL930X_FORCE_EN; // Clear MAC_FORCE_EN to allow SDS-MAC link
  726. else
  727. reg |= RTL930X_FORCE_EN;
  728. sw_w32(reg, priv->r->mac_force_mode_ctrl(port));
  729. }
  730. static void rtl83xx_phylink_mac_link_down(struct dsa_switch *ds, int port,
  731. unsigned int mode,
  732. phy_interface_t interface)
  733. {
  734. struct rtl838x_switch_priv *priv = ds->priv;
  735. u32 v;
  736. /* Stop TX/RX to port */
  737. sw_w32_mask(0x3, 0, priv->r->mac_port_ctrl(port));
  738. // No longer force link
  739. if (priv->family_id == RTL9300_FAMILY_ID)
  740. v = RTL930X_FORCE_EN | RTL930X_FORCE_LINK_EN;
  741. else if (priv->family_id == RTL9310_FAMILY_ID)
  742. v = RTL931X_FORCE_EN | RTL931X_FORCE_LINK_EN;
  743. sw_w32_mask(v, 0, priv->r->mac_port_ctrl(port));
  744. }
  745. static void rtl93xx_phylink_mac_link_down(struct dsa_switch *ds, int port,
  746. unsigned int mode,
  747. phy_interface_t interface)
  748. {
  749. struct rtl838x_switch_priv *priv = ds->priv;
  750. /* Stop TX/RX to port */
  751. sw_w32_mask(0x3, 0, priv->r->mac_port_ctrl(port));
  752. // No longer force link
  753. sw_w32_mask(3, 0, priv->r->mac_force_mode_ctrl(port));
  754. }
  755. static void rtl83xx_phylink_mac_link_up(struct dsa_switch *ds, int port,
  756. unsigned int mode,
  757. phy_interface_t interface,
  758. struct phy_device *phydev,
  759. int speed, int duplex,
  760. bool tx_pause, bool rx_pause)
  761. {
  762. struct rtl838x_switch_priv *priv = ds->priv;
  763. /* Restart TX/RX to port */
  764. sw_w32_mask(0, 0x3, priv->r->mac_port_ctrl(port));
  765. // TODO: Set speed/duplex/pauses
  766. }
  767. static void rtl93xx_phylink_mac_link_up(struct dsa_switch *ds, int port,
  768. unsigned int mode,
  769. phy_interface_t interface,
  770. struct phy_device *phydev,
  771. int speed, int duplex,
  772. bool tx_pause, bool rx_pause)
  773. {
  774. struct rtl838x_switch_priv *priv = ds->priv;
  775. /* Restart TX/RX to port */
  776. sw_w32_mask(0, 0x3, priv->r->mac_port_ctrl(port));
  777. // TODO: Set speed/duplex/pauses
  778. }
  779. static void rtl83xx_get_strings(struct dsa_switch *ds,
  780. int port, u32 stringset, u8 *data)
  781. {
  782. int i;
  783. if (stringset != ETH_SS_STATS)
  784. return;
  785. for (i = 0; i < ARRAY_SIZE(rtl83xx_mib); i++)
  786. strncpy(data + i * ETH_GSTRING_LEN, rtl83xx_mib[i].name,
  787. ETH_GSTRING_LEN);
  788. }
  789. static void rtl83xx_get_ethtool_stats(struct dsa_switch *ds, int port,
  790. uint64_t *data)
  791. {
  792. struct rtl838x_switch_priv *priv = ds->priv;
  793. const struct rtl83xx_mib_desc *mib;
  794. int i;
  795. u64 h;
  796. for (i = 0; i < ARRAY_SIZE(rtl83xx_mib); i++) {
  797. mib = &rtl83xx_mib[i];
  798. data[i] = sw_r32(priv->r->stat_port_std_mib + (port << 8) + 252 - mib->offset);
  799. if (mib->size == 2) {
  800. h = sw_r32(priv->r->stat_port_std_mib + (port << 8) + 248 - mib->offset);
  801. data[i] |= h << 32;
  802. }
  803. }
  804. }
  805. static int rtl83xx_get_sset_count(struct dsa_switch *ds, int port, int sset)
  806. {
  807. if (sset != ETH_SS_STATS)
  808. return 0;
  809. return ARRAY_SIZE(rtl83xx_mib);
  810. }
  811. static int rtl83xx_mc_group_alloc(struct rtl838x_switch_priv *priv, int port)
  812. {
  813. int mc_group = find_first_zero_bit(priv->mc_group_bm, MAX_MC_GROUPS - 1);
  814. u64 portmask;
  815. if (mc_group >= MAX_MC_GROUPS - 1)
  816. return -1;
  817. if (priv->is_lagmember[port]) {
  818. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  819. return 0;
  820. }
  821. set_bit(mc_group, priv->mc_group_bm);
  822. mc_group++; // We cannot use group 0, as this is used for lookup miss flooding
  823. portmask = BIT_ULL(port) | BIT_ULL(priv->cpu_port);
  824. priv->r->write_mcast_pmask(mc_group, portmask);
  825. return mc_group;
  826. }
  827. static u64 rtl83xx_mc_group_add_port(struct rtl838x_switch_priv *priv, int mc_group, int port)
  828. {
  829. u64 portmask = priv->r->read_mcast_pmask(mc_group);
  830. pr_debug("%s: %d\n", __func__, port);
  831. if (priv->is_lagmember[port]) {
  832. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  833. return portmask;
  834. }
  835. portmask |= BIT_ULL(port);
  836. priv->r->write_mcast_pmask(mc_group, portmask);
  837. return portmask;
  838. }
  839. static u64 rtl83xx_mc_group_del_port(struct rtl838x_switch_priv *priv, int mc_group, int port)
  840. {
  841. u64 portmask = priv->r->read_mcast_pmask(mc_group);
  842. pr_debug("%s: %d\n", __func__, port);
  843. if (priv->is_lagmember[port]) {
  844. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  845. return portmask;
  846. }
  847. priv->r->write_mcast_pmask(mc_group, portmask);
  848. if (portmask == BIT_ULL(priv->cpu_port)) {
  849. portmask &= ~BIT_ULL(priv->cpu_port);
  850. priv->r->write_mcast_pmask(mc_group, portmask);
  851. clear_bit(mc_group, priv->mc_group_bm);
  852. }
  853. return portmask;
  854. }
  855. static void store_mcgroups(struct rtl838x_switch_priv *priv, int port)
  856. {
  857. int mc_group;
  858. for (mc_group = 0; mc_group < MAX_MC_GROUPS; mc_group++) {
  859. u64 portmask = priv->r->read_mcast_pmask(mc_group);
  860. if (portmask & BIT_ULL(port)) {
  861. priv->mc_group_saves[mc_group] = port;
  862. rtl83xx_mc_group_del_port(priv, mc_group, port);
  863. }
  864. }
  865. }
  866. static void load_mcgroups(struct rtl838x_switch_priv *priv, int port)
  867. {
  868. int mc_group;
  869. for (mc_group = 0; mc_group < MAX_MC_GROUPS; mc_group++) {
  870. if (priv->mc_group_saves[mc_group] == port) {
  871. rtl83xx_mc_group_add_port(priv, mc_group, port);
  872. priv->mc_group_saves[mc_group] = -1;
  873. }
  874. }
  875. }
  876. static int rtl83xx_port_enable(struct dsa_switch *ds, int port,
  877. struct phy_device *phydev)
  878. {
  879. struct rtl838x_switch_priv *priv = ds->priv;
  880. u64 v;
  881. pr_debug("%s: %x %d", __func__, (u32) priv, port);
  882. priv->ports[port].enable = true;
  883. /* enable inner tagging on egress, do not keep any tags */
  884. if (priv->family_id == RTL9310_FAMILY_ID)
  885. sw_w32(BIT(4), priv->r->vlan_port_tag_sts_ctrl + (port << 2));
  886. else
  887. sw_w32(1, priv->r->vlan_port_tag_sts_ctrl + (port << 2));
  888. if (dsa_is_cpu_port(ds, port))
  889. return 0;
  890. /* add port to switch mask of CPU_PORT */
  891. priv->r->traffic_enable(priv->cpu_port, port);
  892. load_mcgroups(priv, port);
  893. if (priv->is_lagmember[port]) {
  894. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  895. return 0;
  896. }
  897. /* add all other ports in the same bridge to switch mask of port */
  898. v = priv->r->traffic_get(port);
  899. v |= priv->ports[port].pm;
  900. priv->r->traffic_set(port, v);
  901. // TODO: Figure out if this is necessary
  902. if (priv->family_id == RTL9300_FAMILY_ID) {
  903. sw_w32_mask(0, BIT(port), RTL930X_L2_PORT_SABLK_CTRL);
  904. sw_w32_mask(0, BIT(port), RTL930X_L2_PORT_DABLK_CTRL);
  905. }
  906. if (priv->ports[port].sds_num < 0)
  907. priv->ports[port].sds_num = rtl93xx_get_sds(phydev);
  908. return 0;
  909. }
  910. static void rtl83xx_port_disable(struct dsa_switch *ds, int port)
  911. {
  912. struct rtl838x_switch_priv *priv = ds->priv;
  913. u64 v;
  914. pr_debug("%s %x: %d", __func__, (u32)priv, port);
  915. /* you can only disable user ports */
  916. if (!dsa_is_user_port(ds, port))
  917. return;
  918. // BUG: This does not work on RTL931X
  919. /* remove port from switch mask of CPU_PORT */
  920. priv->r->traffic_disable(priv->cpu_port, port);
  921. store_mcgroups(priv, port);
  922. /* remove all other ports in the same bridge from switch mask of port */
  923. v = priv->r->traffic_get(port);
  924. v &= ~priv->ports[port].pm;
  925. priv->r->traffic_set(port, v);
  926. priv->ports[port].enable = false;
  927. }
  928. static int rtl83xx_set_mac_eee(struct dsa_switch *ds, int port,
  929. struct ethtool_eee *e)
  930. {
  931. struct rtl838x_switch_priv *priv = ds->priv;
  932. if (e->eee_enabled && !priv->eee_enabled) {
  933. pr_info("Globally enabling EEE\n");
  934. priv->r->init_eee(priv, true);
  935. }
  936. priv->r->port_eee_set(priv, port, e->eee_enabled);
  937. if (e->eee_enabled)
  938. pr_info("Enabled EEE for port %d\n", port);
  939. else
  940. pr_info("Disabled EEE for port %d\n", port);
  941. return 0;
  942. }
  943. static int rtl83xx_get_mac_eee(struct dsa_switch *ds, int port,
  944. struct ethtool_eee *e)
  945. {
  946. struct rtl838x_switch_priv *priv = ds->priv;
  947. e->supported = SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full;
  948. priv->r->eee_port_ability(priv, e, port);
  949. e->eee_enabled = priv->ports[port].eee_enabled;
  950. e->eee_active = !!(e->advertised & e->lp_advertised);
  951. return 0;
  952. }
  953. static int rtl93xx_get_mac_eee(struct dsa_switch *ds, int port,
  954. struct ethtool_eee *e)
  955. {
  956. struct rtl838x_switch_priv *priv = ds->priv;
  957. e->supported = SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full
  958. | SUPPORTED_2500baseX_Full;
  959. priv->r->eee_port_ability(priv, e, port);
  960. e->eee_enabled = priv->ports[port].eee_enabled;
  961. e->eee_active = !!(e->advertised & e->lp_advertised);
  962. return 0;
  963. }
  964. static int rtl83xx_set_ageing_time(struct dsa_switch *ds, unsigned int msec)
  965. {
  966. struct rtl838x_switch_priv *priv = ds->priv;
  967. priv->r->set_ageing_time(msec);
  968. return 0;
  969. }
  970. static int rtl83xx_port_bridge_join(struct dsa_switch *ds, int port,
  971. struct net_device *bridge)
  972. {
  973. struct rtl838x_switch_priv *priv = ds->priv;
  974. u64 port_bitmap = BIT_ULL(priv->cpu_port), v;
  975. int i;
  976. pr_debug("%s %x: %d %llx", __func__, (u32)priv, port, port_bitmap);
  977. if (priv->is_lagmember[port]) {
  978. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  979. return 0;
  980. }
  981. mutex_lock(&priv->reg_mutex);
  982. for (i = 0; i < ds->num_ports; i++) {
  983. /* Add this port to the port matrix of the other ports in the
  984. * same bridge. If the port is disabled, port matrix is kept
  985. * and not being setup until the port becomes enabled.
  986. */
  987. if (dsa_is_user_port(ds, i) && !priv->is_lagmember[i] && i != port) {
  988. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  989. continue;
  990. if (priv->ports[i].enable)
  991. priv->r->traffic_enable(i, port);
  992. priv->ports[i].pm |= BIT_ULL(port);
  993. port_bitmap |= BIT_ULL(i);
  994. }
  995. }
  996. load_mcgroups(priv, port);
  997. /* Add all other ports to this port matrix. */
  998. if (priv->ports[port].enable) {
  999. priv->r->traffic_enable(priv->cpu_port, port);
  1000. v = priv->r->traffic_get(port);
  1001. v |= port_bitmap;
  1002. priv->r->traffic_set(port, v);
  1003. }
  1004. priv->ports[port].pm |= port_bitmap;
  1005. mutex_unlock(&priv->reg_mutex);
  1006. return 0;
  1007. }
  1008. static void rtl83xx_port_bridge_leave(struct dsa_switch *ds, int port,
  1009. struct net_device *bridge)
  1010. {
  1011. struct rtl838x_switch_priv *priv = ds->priv;
  1012. u64 port_bitmap = BIT_ULL(priv->cpu_port), v;
  1013. int i;
  1014. pr_debug("%s %x: %d", __func__, (u32)priv, port);
  1015. mutex_lock(&priv->reg_mutex);
  1016. for (i = 0; i < ds->num_ports; i++) {
  1017. /* Remove this port from the port matrix of the other ports
  1018. * in the same bridge. If the port is disabled, port matrix
  1019. * is kept and not being setup until the port becomes enabled.
  1020. * And the other port's port matrix cannot be broken when the
  1021. * other port is still a VLAN-aware port.
  1022. */
  1023. if (dsa_is_user_port(ds, i) && i != port) {
  1024. if (dsa_to_port(ds, i)->bridge_dev != bridge)
  1025. continue;
  1026. if (priv->ports[i].enable)
  1027. priv->r->traffic_disable(i, port);
  1028. priv->ports[i].pm |= BIT_ULL(port);
  1029. port_bitmap &= ~BIT_ULL(i);
  1030. }
  1031. }
  1032. store_mcgroups(priv, port);
  1033. /* Add all other ports to this port matrix. */
  1034. if (priv->ports[port].enable) {
  1035. v = priv->r->traffic_get(port);
  1036. v |= port_bitmap;
  1037. priv->r->traffic_set(port, v);
  1038. }
  1039. priv->ports[port].pm &= ~port_bitmap;
  1040. mutex_unlock(&priv->reg_mutex);
  1041. }
  1042. void rtl83xx_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
  1043. {
  1044. u32 msti = 0;
  1045. u32 port_state[4];
  1046. int index, bit;
  1047. int pos = port;
  1048. struct rtl838x_switch_priv *priv = ds->priv;
  1049. int n = priv->port_width << 1;
  1050. /* Ports above or equal CPU port can never be configured */
  1051. if (port >= priv->cpu_port)
  1052. return;
  1053. mutex_lock(&priv->reg_mutex);
  1054. /* For the RTL839x and following, the bits are left-aligned, 838x and 930x
  1055. * have 64 bit fields, 839x and 931x have 128 bit fields
  1056. */
  1057. if (priv->family_id == RTL8390_FAMILY_ID)
  1058. pos += 12;
  1059. if (priv->family_id == RTL9300_FAMILY_ID)
  1060. pos += 3;
  1061. if (priv->family_id == RTL9310_FAMILY_ID)
  1062. pos += 8;
  1063. index = n - (pos >> 4) - 1;
  1064. bit = (pos << 1) % 32;
  1065. priv->r->stp_get(priv, msti, port_state);
  1066. pr_debug("Current state, port %d: %d\n", port, (port_state[index] >> bit) & 3);
  1067. port_state[index] &= ~(3 << bit);
  1068. switch (state) {
  1069. case BR_STATE_DISABLED: /* 0 */
  1070. port_state[index] |= (0 << bit);
  1071. break;
  1072. case BR_STATE_BLOCKING: /* 4 */
  1073. case BR_STATE_LISTENING: /* 1 */
  1074. port_state[index] |= (1 << bit);
  1075. break;
  1076. case BR_STATE_LEARNING: /* 2 */
  1077. port_state[index] |= (2 << bit);
  1078. break;
  1079. case BR_STATE_FORWARDING: /* 3*/
  1080. port_state[index] |= (3 << bit);
  1081. default:
  1082. break;
  1083. }
  1084. priv->r->stp_set(priv, msti, port_state);
  1085. mutex_unlock(&priv->reg_mutex);
  1086. }
  1087. void rtl83xx_fast_age(struct dsa_switch *ds, int port)
  1088. {
  1089. struct rtl838x_switch_priv *priv = ds->priv;
  1090. int s = priv->family_id == RTL8390_FAMILY_ID ? 2 : 0;
  1091. pr_debug("FAST AGE port %d\n", port);
  1092. mutex_lock(&priv->reg_mutex);
  1093. /* RTL838X_L2_TBL_FLUSH_CTRL register bits, 839x has 1 bit larger
  1094. * port fields:
  1095. * 0-4: Replacing port
  1096. * 5-9: Flushed/replaced port
  1097. * 10-21: FVID
  1098. * 22: Entry types: 1: dynamic, 0: also static
  1099. * 23: Match flush port
  1100. * 24: Match FVID
  1101. * 25: Flush (0) or replace (1) L2 entries
  1102. * 26: Status of action (1: Start, 0: Done)
  1103. */
  1104. sw_w32(1 << (26 + s) | 1 << (23 + s) | port << (5 + (s / 2)), priv->r->l2_tbl_flush_ctrl);
  1105. do { } while (sw_r32(priv->r->l2_tbl_flush_ctrl) & BIT(26 + s));
  1106. mutex_unlock(&priv->reg_mutex);
  1107. }
  1108. void rtl931x_fast_age(struct dsa_switch *ds, int port)
  1109. {
  1110. struct rtl838x_switch_priv *priv = ds->priv;
  1111. pr_info("%s port %d\n", __func__, port);
  1112. mutex_lock(&priv->reg_mutex);
  1113. sw_w32(port << 11, RTL931X_L2_TBL_FLUSH_CTRL + 4);
  1114. sw_w32(BIT(24) | BIT(28), RTL931X_L2_TBL_FLUSH_CTRL);
  1115. do { } while (sw_r32(RTL931X_L2_TBL_FLUSH_CTRL) & BIT (28));
  1116. mutex_unlock(&priv->reg_mutex);
  1117. }
  1118. void rtl930x_fast_age(struct dsa_switch *ds, int port)
  1119. {
  1120. struct rtl838x_switch_priv *priv = ds->priv;
  1121. if (priv->family_id == RTL9310_FAMILY_ID)
  1122. return rtl931x_fast_age(ds, port);
  1123. pr_debug("FAST AGE port %d\n", port);
  1124. mutex_lock(&priv->reg_mutex);
  1125. sw_w32(port << 11, RTL930X_L2_TBL_FLUSH_CTRL + 4);
  1126. sw_w32(BIT(26) | BIT(30), RTL930X_L2_TBL_FLUSH_CTRL);
  1127. do { } while (sw_r32(priv->r->l2_tbl_flush_ctrl) & BIT(30));
  1128. mutex_unlock(&priv->reg_mutex);
  1129. }
  1130. static int rtl83xx_vlan_filtering(struct dsa_switch *ds, int port,
  1131. bool vlan_filtering,
  1132. struct switchdev_trans *trans)
  1133. {
  1134. struct rtl838x_switch_priv *priv = ds->priv;
  1135. pr_debug("%s: port %d\n", __func__, port);
  1136. mutex_lock(&priv->reg_mutex);
  1137. if (vlan_filtering) {
  1138. /* Enable ingress and egress filtering
  1139. * The VLAN_PORT_IGR_FILTER register uses 2 bits for each port to define
  1140. * the filter action:
  1141. * 0: Always Forward
  1142. * 1: Drop packet
  1143. * 2: Trap packet to CPU port
  1144. * The Egress filter used 1 bit per state (0: DISABLED, 1: ENABLED)
  1145. */
  1146. if (port != priv->cpu_port)
  1147. priv->r->set_vlan_igr_filter(port, IGR_DROP);
  1148. priv->r->set_vlan_egr_filter(port, EGR_ENABLE);
  1149. } else {
  1150. /* Disable ingress and egress filtering */
  1151. if (port != priv->cpu_port)
  1152. priv->r->set_vlan_igr_filter(port, IGR_FORWARD);
  1153. priv->r->set_vlan_egr_filter(port, EGR_DISABLE);
  1154. }
  1155. /* Do we need to do something to the CPU-Port, too? */
  1156. mutex_unlock(&priv->reg_mutex);
  1157. return 0;
  1158. }
  1159. static int rtl83xx_vlan_prepare(struct dsa_switch *ds, int port,
  1160. const struct switchdev_obj_port_vlan *vlan)
  1161. {
  1162. struct rtl838x_vlan_info info;
  1163. struct rtl838x_switch_priv *priv = ds->priv;
  1164. priv->r->vlan_tables_read(0, &info);
  1165. pr_debug("VLAN 0: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
  1166. info.tagged_ports, info.untagged_ports, info.profile_id,
  1167. info.hash_mc_fid, info.hash_uc_fid, info.fid);
  1168. priv->r->vlan_tables_read(1, &info);
  1169. pr_debug("VLAN 1: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
  1170. info.tagged_ports, info.untagged_ports, info.profile_id,
  1171. info.hash_mc_fid, info.hash_uc_fid, info.fid);
  1172. priv->r->vlan_set_untagged(1, info.untagged_ports);
  1173. pr_debug("SET: Untagged ports, VLAN %d: %llx\n", 1, info.untagged_ports);
  1174. priv->r->vlan_set_tagged(1, &info);
  1175. pr_debug("SET: Tagged ports, VLAN %d: %llx\n", 1, info.tagged_ports);
  1176. return 0;
  1177. }
  1178. static void rtl83xx_vlan_add(struct dsa_switch *ds, int port,
  1179. const struct switchdev_obj_port_vlan *vlan)
  1180. {
  1181. struct rtl838x_vlan_info info;
  1182. struct rtl838x_switch_priv *priv = ds->priv;
  1183. int v;
  1184. pr_debug("%s port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  1185. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  1186. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  1187. dev_err(priv->dev, "VLAN out of range: %d - %d",
  1188. vlan->vid_begin, vlan->vid_end);
  1189. return;
  1190. }
  1191. mutex_lock(&priv->reg_mutex);
  1192. if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
  1193. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1194. if (!v)
  1195. continue;
  1196. /* Set both inner and outer PVID of the port */
  1197. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_INNER, v);
  1198. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_OUTER, v);
  1199. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_INNER,
  1200. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1201. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_OUTER,
  1202. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1203. priv->ports[port].pvid = vlan->vid_end;
  1204. }
  1205. }
  1206. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1207. /* Get port memberships of this vlan */
  1208. priv->r->vlan_tables_read(v, &info);
  1209. /* new VLAN? */
  1210. if (!info.tagged_ports) {
  1211. info.fid = 0;
  1212. info.hash_mc_fid = false;
  1213. info.hash_uc_fid = false;
  1214. info.profile_id = 0;
  1215. }
  1216. /* sanitize untagged_ports - must be a subset */
  1217. if (info.untagged_ports & ~info.tagged_ports)
  1218. info.untagged_ports = 0;
  1219. info.tagged_ports |= BIT_ULL(port);
  1220. if (vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  1221. info.untagged_ports |= BIT_ULL(port);
  1222. priv->r->vlan_set_untagged(v, info.untagged_ports);
  1223. pr_debug("Untagged ports, VLAN %d: %llx\n", v, info.untagged_ports);
  1224. priv->r->vlan_set_tagged(v, &info);
  1225. pr_debug("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  1226. }
  1227. mutex_unlock(&priv->reg_mutex);
  1228. }
  1229. static int rtl83xx_vlan_del(struct dsa_switch *ds, int port,
  1230. const struct switchdev_obj_port_vlan *vlan)
  1231. {
  1232. struct rtl838x_vlan_info info;
  1233. struct rtl838x_switch_priv *priv = ds->priv;
  1234. int v;
  1235. u16 pvid;
  1236. pr_debug("%s: port %d, vid_end %d, vid_end %d, flags %x\n", __func__,
  1237. port, vlan->vid_begin, vlan->vid_end, vlan->flags);
  1238. if (vlan->vid_begin > 4095 || vlan->vid_end > 4095) {
  1239. dev_err(priv->dev, "VLAN out of range: %d - %d",
  1240. vlan->vid_begin, vlan->vid_end);
  1241. return -ENOTSUPP;
  1242. }
  1243. mutex_lock(&priv->reg_mutex);
  1244. pvid = priv->ports[port].pvid;
  1245. for (v = vlan->vid_begin; v <= vlan->vid_end; v++) {
  1246. /* Reset to default if removing the current PVID */
  1247. if (v == pvid) {
  1248. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_INNER, 0);
  1249. priv->r->vlan_port_pvid_set(port, PBVLAN_TYPE_OUTER, 0);
  1250. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_INNER,
  1251. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1252. priv->r->vlan_port_pvidmode_set(port, PBVLAN_TYPE_OUTER,
  1253. PBVLAN_MODE_UNTAG_AND_PRITAG);
  1254. }
  1255. /* Get port memberships of this vlan */
  1256. priv->r->vlan_tables_read(v, &info);
  1257. /* remove port from both tables */
  1258. info.untagged_ports &= (~BIT_ULL(port));
  1259. info.tagged_ports &= (~BIT_ULL(port));
  1260. priv->r->vlan_set_untagged(v, info.untagged_ports);
  1261. pr_debug("Untagged ports, VLAN %d: %llx\n", v, info.untagged_ports);
  1262. priv->r->vlan_set_tagged(v, &info);
  1263. pr_debug("Tagged ports, VLAN %d: %llx\n", v, info.tagged_ports);
  1264. }
  1265. mutex_unlock(&priv->reg_mutex);
  1266. return 0;
  1267. }
  1268. static void dump_l2_entry(struct rtl838x_l2_entry *e)
  1269. {
  1270. pr_info("MAC: %02x:%02x:%02x:%02x:%02x:%02x vid: %d, rvid: %d, port: %d, valid: %d\n",
  1271. e->mac[0], e->mac[1], e->mac[2], e->mac[3], e->mac[4], e->mac[5],
  1272. e->vid, e->rvid, e->port, e->valid);
  1273. if (e->type != L2_MULTICAST) {
  1274. pr_info("Type: %d, is_static: %d, is_ip_mc: %d, is_ipv6_mc: %d, block_da: %d\n",
  1275. e->type, e->is_static, e->is_ip_mc, e->is_ipv6_mc, e->block_da);
  1276. pr_info(" block_sa: %d, susp: %d, nh: %d, age: %d, is_trunk: %d, trunk: %d\n",
  1277. e->block_sa, e->suspended, e->next_hop, e->age, e->is_trunk, e->trunk);
  1278. }
  1279. if (e->type == L2_MULTICAST)
  1280. pr_info(" L2_MULTICAST mc_portmask_index: %d\n", e->mc_portmask_index);
  1281. if (e->is_ip_mc || e->is_ipv6_mc)
  1282. pr_info(" mc_portmask_index: %d, mc_gip: %d, mc_sip: %d\n",
  1283. e->mc_portmask_index, e->mc_gip, e->mc_sip);
  1284. pr_info(" stack_dev: %d\n", e->stack_dev);
  1285. if (e->next_hop)
  1286. pr_info(" nh_route_id: %d\n", e->nh_route_id);
  1287. }
  1288. static void rtl83xx_setup_l2_uc_entry(struct rtl838x_l2_entry *e, int port, int vid, u64 mac)
  1289. {
  1290. e->is_ip_mc = e->is_ipv6_mc = false;
  1291. e->valid = true;
  1292. e->age = 3;
  1293. e->port = port,
  1294. e->vid = vid;
  1295. u64_to_ether_addr(mac, e->mac);
  1296. }
  1297. static void rtl83xx_setup_l2_mc_entry(struct rtl838x_switch_priv *priv,
  1298. struct rtl838x_l2_entry *e, int vid, u64 mac, int mc_group)
  1299. {
  1300. e->is_ip_mc = e->is_ipv6_mc = false;
  1301. e->valid = true;
  1302. e->mc_portmask_index = mc_group;
  1303. e->type = L2_MULTICAST;
  1304. e->rvid = e->vid = vid;
  1305. pr_debug("%s: vid: %d, rvid: %d\n", __func__, e->vid, e->rvid);
  1306. u64_to_ether_addr(mac, e->mac);
  1307. }
  1308. /*
  1309. * Uses the seed to identify a hash bucket in the L2 using the derived hash key and then loops
  1310. * over the entries in the bucket until either a matching entry is found or an empty slot
  1311. * Returns the filled in rtl838x_l2_entry and the index in the bucket when an entry was found
  1312. * when an empty slot was found and must exist is false, the index of the slot is returned
  1313. * when no slots are available returns -1
  1314. */
  1315. static int rtl83xx_find_l2_hash_entry(struct rtl838x_switch_priv *priv, u64 seed,
  1316. bool must_exist, struct rtl838x_l2_entry *e)
  1317. {
  1318. int i, idx = -1;
  1319. u32 key = priv->r->l2_hash_key(priv, seed);
  1320. u64 entry;
  1321. pr_debug("%s: using key %x, for seed %016llx\n", __func__, key, seed);
  1322. // Loop over all entries in the hash-bucket and over the second block on 93xx SoCs
  1323. for (i = 0; i < priv->l2_bucket_size; i++) {
  1324. entry = priv->r->read_l2_entry_using_hash(key, i, e);
  1325. pr_debug("valid %d, mac %016llx\n", e->valid, ether_addr_to_u64(&e->mac[0]));
  1326. if (must_exist && !e->valid)
  1327. continue;
  1328. if (!e->valid || ((entry & 0x0fffffffffffffffULL) == seed)) {
  1329. idx = i > 3 ? ((key >> 14) & 0xffff) | i >> 1 : ((key << 2) | i) & 0xffff;
  1330. break;
  1331. }
  1332. }
  1333. return idx;
  1334. }
  1335. /*
  1336. * Uses the seed to identify an entry in the CAM by looping over all its entries
  1337. * Returns the filled in rtl838x_l2_entry and the index in the CAM when an entry was found
  1338. * when an empty slot was found the index of the slot is returned
  1339. * when no slots are available returns -1
  1340. */
  1341. static int rtl83xx_find_l2_cam_entry(struct rtl838x_switch_priv *priv, u64 seed,
  1342. bool must_exist, struct rtl838x_l2_entry *e)
  1343. {
  1344. int i, idx = -1;
  1345. u64 entry;
  1346. for (i = 0; i < 64; i++) {
  1347. entry = priv->r->read_cam(i, e);
  1348. if (!must_exist && !e->valid) {
  1349. if (idx < 0) /* First empty entry? */
  1350. idx = i;
  1351. break;
  1352. } else if ((entry & 0x0fffffffffffffffULL) == seed) {
  1353. pr_debug("Found entry in CAM\n");
  1354. idx = i;
  1355. break;
  1356. }
  1357. }
  1358. return idx;
  1359. }
  1360. static int rtl83xx_port_fdb_add(struct dsa_switch *ds, int port,
  1361. const unsigned char *addr, u16 vid)
  1362. {
  1363. struct rtl838x_switch_priv *priv = ds->priv;
  1364. u64 mac = ether_addr_to_u64(addr);
  1365. struct rtl838x_l2_entry e;
  1366. int err = 0, idx;
  1367. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1368. if (priv->is_lagmember[port]) {
  1369. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  1370. return 0;
  1371. }
  1372. mutex_lock(&priv->reg_mutex);
  1373. idx = rtl83xx_find_l2_hash_entry(priv, seed, false, &e);
  1374. // Found an existing or empty entry
  1375. if (idx >= 0) {
  1376. rtl83xx_setup_l2_uc_entry(&e, port, vid, mac);
  1377. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1378. goto out;
  1379. }
  1380. // Hash buckets full, try CAM
  1381. rtl83xx_find_l2_cam_entry(priv, seed, false, &e);
  1382. if (idx >= 0) {
  1383. rtl83xx_setup_l2_uc_entry(&e, port, vid, mac);
  1384. priv->r->write_cam(idx, &e);
  1385. goto out;
  1386. }
  1387. err = -ENOTSUPP;
  1388. out:
  1389. mutex_unlock(&priv->reg_mutex);
  1390. return err;
  1391. }
  1392. static int rtl83xx_port_fdb_del(struct dsa_switch *ds, int port,
  1393. const unsigned char *addr, u16 vid)
  1394. {
  1395. struct rtl838x_switch_priv *priv = ds->priv;
  1396. u64 mac = ether_addr_to_u64(addr);
  1397. struct rtl838x_l2_entry e;
  1398. int err = 0, idx;
  1399. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1400. pr_info("In %s, mac %llx, vid: %d\n", __func__, mac, vid);
  1401. mutex_lock(&priv->reg_mutex);
  1402. idx = rtl83xx_find_l2_hash_entry(priv, seed, true, &e);
  1403. pr_info("Found entry index %d, key %d and bucket %d\n", idx, idx >> 2, idx & 3);
  1404. if (idx >= 0) {
  1405. e.valid = false;
  1406. dump_l2_entry(&e);
  1407. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1408. goto out;
  1409. }
  1410. /* Check CAM for spillover from hash buckets */
  1411. rtl83xx_find_l2_cam_entry(priv, seed, true, &e);
  1412. if (idx >= 0) {
  1413. e.valid = false;
  1414. priv->r->write_cam(idx, &e);
  1415. goto out;
  1416. }
  1417. err = -ENOENT;
  1418. out:
  1419. mutex_unlock(&priv->reg_mutex);
  1420. return err;
  1421. }
  1422. static int rtl83xx_port_fdb_dump(struct dsa_switch *ds, int port,
  1423. dsa_fdb_dump_cb_t *cb, void *data)
  1424. {
  1425. struct rtl838x_l2_entry e;
  1426. struct rtl838x_switch_priv *priv = ds->priv;
  1427. int i;
  1428. u32 fid, pkey;
  1429. u64 mac;
  1430. mutex_lock(&priv->reg_mutex);
  1431. for (i = 0; i < priv->fib_entries; i++) {
  1432. priv->r->read_l2_entry_using_hash(i >> 2, i & 0x3, &e);
  1433. if (!e.valid)
  1434. continue;
  1435. if (e.port == port || e.port == RTL930X_PORT_IGNORE) {
  1436. u64 seed;
  1437. u32 key;
  1438. fid = ((i >> 2) & 0x3ff) | (e.rvid & ~0x3ff);
  1439. mac = ether_addr_to_u64(&e.mac[0]);
  1440. pkey = priv->r->l2_hash_key(priv, priv->r->l2_hash_seed(mac, fid));
  1441. fid = (pkey & 0x3ff) | (fid & ~0x3ff);
  1442. pr_info("-> index %d, key %x, bucket %d, dmac %016llx, fid: %x rvid: %x\n",
  1443. i, i >> 2, i & 0x3, mac, fid, e.rvid);
  1444. dump_l2_entry(&e);
  1445. seed = priv->r->l2_hash_seed(mac, e.rvid);
  1446. key = priv->r->l2_hash_key(priv, seed);
  1447. pr_info("seed: %016llx, key based on rvid: %08x\n", seed, key);
  1448. cb(e.mac, e.vid, e.is_static, data);
  1449. }
  1450. if (e.type == L2_MULTICAST) {
  1451. u64 portmask = priv->r->read_mcast_pmask(e.mc_portmask_index);
  1452. if (portmask & BIT_ULL(port)) {
  1453. dump_l2_entry(&e);
  1454. pr_info(" PM: %016llx\n", portmask);
  1455. }
  1456. }
  1457. }
  1458. for (i = 0; i < 64; i++) {
  1459. priv->r->read_cam(i, &e);
  1460. if (!e.valid)
  1461. continue;
  1462. if (e.port == port)
  1463. cb(e.mac, e.vid, e.is_static, data);
  1464. }
  1465. mutex_unlock(&priv->reg_mutex);
  1466. return 0;
  1467. }
  1468. static int rtl83xx_port_mdb_prepare(struct dsa_switch *ds, int port,
  1469. const struct switchdev_obj_port_mdb *mdb)
  1470. {
  1471. struct rtl838x_switch_priv *priv = ds->priv;
  1472. if (priv->id >= 0x9300)
  1473. return -EOPNOTSUPP;
  1474. return 0;
  1475. }
  1476. static void rtl83xx_port_mdb_add(struct dsa_switch *ds, int port,
  1477. const struct switchdev_obj_port_mdb *mdb)
  1478. {
  1479. struct rtl838x_switch_priv *priv = ds->priv;
  1480. u64 mac = ether_addr_to_u64(mdb->addr);
  1481. struct rtl838x_l2_entry e;
  1482. int err = 0, idx;
  1483. int vid = mdb->vid;
  1484. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1485. int mc_group;
  1486. pr_debug("In %s port %d, mac %llx, vid: %d\n", __func__, port, mac, vid);
  1487. if (priv->is_lagmember[port]) {
  1488. pr_debug("%s: %d is lag slave. ignore\n", __func__, port);
  1489. return;
  1490. }
  1491. mutex_lock(&priv->reg_mutex);
  1492. idx = rtl83xx_find_l2_hash_entry(priv, seed, false, &e);
  1493. // Found an existing or empty entry
  1494. if (idx >= 0) {
  1495. if (e.valid) {
  1496. pr_debug("Found an existing entry %016llx, mc_group %d\n",
  1497. ether_addr_to_u64(e.mac), e.mc_portmask_index);
  1498. rtl83xx_mc_group_add_port(priv, e.mc_portmask_index, port);
  1499. } else {
  1500. pr_debug("New entry for seed %016llx\n", seed);
  1501. mc_group = rtl83xx_mc_group_alloc(priv, port);
  1502. if (mc_group < 0) {
  1503. err = -ENOTSUPP;
  1504. goto out;
  1505. }
  1506. rtl83xx_setup_l2_mc_entry(priv, &e, vid, mac, mc_group);
  1507. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1508. }
  1509. goto out;
  1510. }
  1511. // Hash buckets full, try CAM
  1512. rtl83xx_find_l2_cam_entry(priv, seed, false, &e);
  1513. if (idx >= 0) {
  1514. if (e.valid) {
  1515. pr_debug("Found existing CAM entry %016llx, mc_group %d\n",
  1516. ether_addr_to_u64(e.mac), e.mc_portmask_index);
  1517. rtl83xx_mc_group_add_port(priv, e.mc_portmask_index, port);
  1518. } else {
  1519. pr_debug("New entry\n");
  1520. mc_group = rtl83xx_mc_group_alloc(priv, port);
  1521. if (mc_group < 0) {
  1522. err = -ENOTSUPP;
  1523. goto out;
  1524. }
  1525. rtl83xx_setup_l2_mc_entry(priv, &e, vid, mac, mc_group);
  1526. priv->r->write_cam(idx, &e);
  1527. }
  1528. goto out;
  1529. }
  1530. err = -ENOTSUPP;
  1531. out:
  1532. mutex_unlock(&priv->reg_mutex);
  1533. if (err)
  1534. dev_err(ds->dev, "failed to add MDB entry\n");
  1535. }
  1536. int rtl83xx_port_mdb_del(struct dsa_switch *ds, int port,
  1537. const struct switchdev_obj_port_mdb *mdb)
  1538. {
  1539. struct rtl838x_switch_priv *priv = ds->priv;
  1540. u64 mac = ether_addr_to_u64(mdb->addr);
  1541. struct rtl838x_l2_entry e;
  1542. int err = 0, idx;
  1543. int vid = mdb->vid;
  1544. u64 seed = priv->r->l2_hash_seed(mac, vid);
  1545. u64 portmask;
  1546. pr_debug("In %s, port %d, mac %llx, vid: %d\n", __func__, port, mac, vid);
  1547. if (priv->is_lagmember[port]) {
  1548. pr_info("%s: %d is lag slave. ignore\n", __func__, port);
  1549. return 0;
  1550. }
  1551. mutex_lock(&priv->reg_mutex);
  1552. idx = rtl83xx_find_l2_hash_entry(priv, seed, true, &e);
  1553. pr_debug("Found entry index %d, key %d and bucket %d\n", idx, idx >> 2, idx & 3);
  1554. if (idx >= 0) {
  1555. portmask = rtl83xx_mc_group_del_port(priv, e.mc_portmask_index, port);
  1556. if (!portmask) {
  1557. e.valid = false;
  1558. // dump_l2_entry(&e);
  1559. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  1560. }
  1561. goto out;
  1562. }
  1563. /* Check CAM for spillover from hash buckets */
  1564. rtl83xx_find_l2_cam_entry(priv, seed, true, &e);
  1565. if (idx >= 0) {
  1566. portmask = rtl83xx_mc_group_del_port(priv, e.mc_portmask_index, port);
  1567. if (!portmask) {
  1568. e.valid = false;
  1569. // dump_l2_entry(&e);
  1570. priv->r->write_cam(idx, &e);
  1571. }
  1572. goto out;
  1573. }
  1574. // TODO: Re-enable with a newer kernel: err = -ENOENT;
  1575. out:
  1576. mutex_unlock(&priv->reg_mutex);
  1577. return err;
  1578. }
  1579. static int rtl83xx_port_mirror_add(struct dsa_switch *ds, int port,
  1580. struct dsa_mall_mirror_tc_entry *mirror,
  1581. bool ingress)
  1582. {
  1583. /* We support 4 mirror groups, one destination port per group */
  1584. int group;
  1585. struct rtl838x_switch_priv *priv = ds->priv;
  1586. int ctrl_reg, dpm_reg, spm_reg;
  1587. pr_debug("In %s\n", __func__);
  1588. for (group = 0; group < 4; group++) {
  1589. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  1590. break;
  1591. }
  1592. if (group >= 4) {
  1593. for (group = 0; group < 4; group++) {
  1594. if (priv->mirror_group_ports[group] < 0)
  1595. break;
  1596. }
  1597. }
  1598. if (group >= 4)
  1599. return -ENOSPC;
  1600. ctrl_reg = priv->r->mir_ctrl + group * 4;
  1601. dpm_reg = priv->r->mir_dpm + group * 4 * priv->port_width;
  1602. spm_reg = priv->r->mir_spm + group * 4 * priv->port_width;
  1603. pr_debug("Using group %d\n", group);
  1604. mutex_lock(&priv->reg_mutex);
  1605. if (priv->family_id == RTL8380_FAMILY_ID) {
  1606. /* Enable mirroring to port across VLANs (bit 11) */
  1607. sw_w32(1 << 11 | (mirror->to_local_port << 4) | 1, ctrl_reg);
  1608. } else {
  1609. /* Enable mirroring to destination port */
  1610. sw_w32((mirror->to_local_port << 4) | 1, ctrl_reg);
  1611. }
  1612. if (ingress && (priv->r->get_port_reg_be(spm_reg) & (1ULL << port))) {
  1613. mutex_unlock(&priv->reg_mutex);
  1614. return -EEXIST;
  1615. }
  1616. if ((!ingress) && (priv->r->get_port_reg_be(dpm_reg) & (1ULL << port))) {
  1617. mutex_unlock(&priv->reg_mutex);
  1618. return -EEXIST;
  1619. }
  1620. if (ingress)
  1621. priv->r->mask_port_reg_be(0, 1ULL << port, spm_reg);
  1622. else
  1623. priv->r->mask_port_reg_be(0, 1ULL << port, dpm_reg);
  1624. priv->mirror_group_ports[group] = mirror->to_local_port;
  1625. mutex_unlock(&priv->reg_mutex);
  1626. return 0;
  1627. }
  1628. static void rtl83xx_port_mirror_del(struct dsa_switch *ds, int port,
  1629. struct dsa_mall_mirror_tc_entry *mirror)
  1630. {
  1631. int group = 0;
  1632. struct rtl838x_switch_priv *priv = ds->priv;
  1633. int ctrl_reg, dpm_reg, spm_reg;
  1634. pr_debug("In %s\n", __func__);
  1635. for (group = 0; group < 4; group++) {
  1636. if (priv->mirror_group_ports[group] == mirror->to_local_port)
  1637. break;
  1638. }
  1639. if (group >= 4)
  1640. return;
  1641. ctrl_reg = priv->r->mir_ctrl + group * 4;
  1642. dpm_reg = priv->r->mir_dpm + group * 4 * priv->port_width;
  1643. spm_reg = priv->r->mir_spm + group * 4 * priv->port_width;
  1644. mutex_lock(&priv->reg_mutex);
  1645. if (mirror->ingress) {
  1646. /* Ingress, clear source port matrix */
  1647. priv->r->mask_port_reg_be(1ULL << port, 0, spm_reg);
  1648. } else {
  1649. /* Egress, clear destination port matrix */
  1650. priv->r->mask_port_reg_be(1ULL << port, 0, dpm_reg);
  1651. }
  1652. if (!(sw_r32(spm_reg) || sw_r32(dpm_reg))) {
  1653. priv->mirror_group_ports[group] = -1;
  1654. sw_w32(0, ctrl_reg);
  1655. }
  1656. mutex_unlock(&priv->reg_mutex);
  1657. }
  1658. static int rtl83xx_port_pre_bridge_flags(struct dsa_switch *ds, int port, unsigned long flags, struct netlink_ext_ack *extack)
  1659. {
  1660. struct rtl838x_switch_priv *priv = ds->priv;
  1661. unsigned long features = 0;
  1662. pr_debug("%s: %d %lX\n", __func__, port, flags);
  1663. if (priv->r->enable_learning)
  1664. features |= BR_LEARNING;
  1665. if (priv->r->enable_flood)
  1666. features |= BR_FLOOD;
  1667. if (priv->r->enable_mcast_flood)
  1668. features |= BR_MCAST_FLOOD;
  1669. if (priv->r->enable_bcast_flood)
  1670. features |= BR_BCAST_FLOOD;
  1671. if (flags & ~(features))
  1672. return -EINVAL;
  1673. return 0;
  1674. }
  1675. static int rtl83xx_port_bridge_flags(struct dsa_switch *ds, int port, unsigned long flags, struct netlink_ext_ack *extack)
  1676. {
  1677. struct rtl838x_switch_priv *priv = ds->priv;
  1678. pr_debug("%s: %d %lX\n", __func__, port, flags);
  1679. if (priv->r->enable_learning)
  1680. priv->r->enable_learning(port, !!(flags & BR_LEARNING));
  1681. if (priv->r->enable_flood)
  1682. priv->r->enable_flood(port, !!(flags & BR_FLOOD));
  1683. if (priv->r->enable_mcast_flood)
  1684. priv->r->enable_mcast_flood(port, !!(flags & BR_MCAST_FLOOD));
  1685. if (priv->r->enable_bcast_flood)
  1686. priv->r->enable_bcast_flood(port, !!(flags & BR_BCAST_FLOOD));
  1687. return 0;
  1688. }
  1689. static bool rtl83xx_lag_can_offload(struct dsa_switch *ds,
  1690. struct net_device *lag,
  1691. struct netdev_lag_upper_info *info)
  1692. {
  1693. int id;
  1694. id = dsa_lag_id(ds->dst, lag);
  1695. if (id < 0 || id >= ds->num_lag_ids)
  1696. return false;
  1697. if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  1698. return false;
  1699. }
  1700. if (info->hash_type != NETDEV_LAG_HASH_L2 && info->hash_type != NETDEV_LAG_HASH_L23)
  1701. return false;
  1702. return true;
  1703. }
  1704. static int rtl83xx_port_lag_change(struct dsa_switch *ds, int port)
  1705. {
  1706. struct rtl838x_switch_priv *priv = ds->priv;
  1707. pr_debug("%s: %d\n", __func__, port);
  1708. // Nothing to be done...
  1709. return 0;
  1710. }
  1711. static int rtl83xx_port_lag_join(struct dsa_switch *ds, int port,
  1712. struct net_device *lag,
  1713. struct netdev_lag_upper_info *info)
  1714. {
  1715. struct rtl838x_switch_priv *priv = ds->priv;
  1716. int i, err = 0;
  1717. if (!rtl83xx_lag_can_offload(ds, lag, info))
  1718. return -EOPNOTSUPP;
  1719. mutex_lock(&priv->reg_mutex);
  1720. for (i = 0; i < priv->n_lags; i++) {
  1721. if ((!priv->lag_devs[i]) || (priv->lag_devs[i] == lag))
  1722. break;
  1723. }
  1724. if (port >= priv->cpu_port) {
  1725. err = -EINVAL;
  1726. goto out;
  1727. }
  1728. pr_info("port_lag_join: group %d, port %d\n",i, port);
  1729. if (!priv->lag_devs[i])
  1730. priv->lag_devs[i] = lag;
  1731. if (priv->lag_primary[i]==-1) {
  1732. priv->lag_primary[i]=port;
  1733. } else
  1734. priv->is_lagmember[port] = 1;
  1735. priv->lagmembers |= (1ULL << port);
  1736. pr_debug("lag_members = %llX\n", priv->lagmembers);
  1737. err = rtl83xx_lag_add(priv->ds, i, port, info);
  1738. if (err) {
  1739. err = -EINVAL;
  1740. goto out;
  1741. }
  1742. out:
  1743. mutex_unlock(&priv->reg_mutex);
  1744. return err;
  1745. }
  1746. static int rtl83xx_port_lag_leave(struct dsa_switch *ds, int port,
  1747. struct net_device *lag)
  1748. {
  1749. int i, group = -1, err;
  1750. struct rtl838x_switch_priv *priv = ds->priv;
  1751. mutex_lock(&priv->reg_mutex);
  1752. for (i=0;i<priv->n_lags;i++) {
  1753. if (priv->lags_port_members[i] & BIT_ULL(port)) {
  1754. group = i;
  1755. break;
  1756. }
  1757. }
  1758. if (group == -1) {
  1759. pr_info("port_lag_leave: port %d is not a member\n", port);
  1760. err = -EINVAL;
  1761. goto out;
  1762. }
  1763. if (port >= priv->cpu_port) {
  1764. err = -EINVAL;
  1765. goto out;
  1766. }
  1767. pr_info("port_lag_del: group %d, port %d\n",group, port);
  1768. priv->lagmembers &=~ (1ULL << port);
  1769. priv->lag_primary[i] = -1;
  1770. priv->is_lagmember[port] = 0;
  1771. pr_debug("lag_members = %llX\n", priv->lagmembers);
  1772. err = rtl83xx_lag_del(priv->ds, group, port);
  1773. if (err) {
  1774. err = -EINVAL;
  1775. goto out;
  1776. }
  1777. if (!priv->lags_port_members[i])
  1778. priv->lag_devs[i] = NULL;
  1779. out:
  1780. mutex_unlock(&priv->reg_mutex);
  1781. return 0;
  1782. }
  1783. int dsa_phy_read(struct dsa_switch *ds, int phy_addr, int phy_reg)
  1784. {
  1785. u32 val;
  1786. u32 offset = 0;
  1787. struct rtl838x_switch_priv *priv = ds->priv;
  1788. if (phy_addr >= 24 && phy_addr <= 27
  1789. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  1790. if (phy_addr == 26)
  1791. offset = 0x100;
  1792. val = sw_r32(RTL838X_SDS4_FIB_REG0 + offset + (phy_reg << 2)) & 0xffff;
  1793. return val;
  1794. }
  1795. read_phy(phy_addr, 0, phy_reg, &val);
  1796. return val;
  1797. }
  1798. int dsa_phy_write(struct dsa_switch *ds, int phy_addr, int phy_reg, u16 val)
  1799. {
  1800. u32 offset = 0;
  1801. struct rtl838x_switch_priv *priv = ds->priv;
  1802. if (phy_addr >= 24 && phy_addr <= 27
  1803. && priv->ports[24].phy == PHY_RTL838X_SDS) {
  1804. if (phy_addr == 26)
  1805. offset = 0x100;
  1806. sw_w32(val, RTL838X_SDS4_FIB_REG0 + offset + (phy_reg << 2));
  1807. return 0;
  1808. }
  1809. return write_phy(phy_addr, 0, phy_reg, val);
  1810. }
  1811. const struct dsa_switch_ops rtl83xx_switch_ops = {
  1812. .get_tag_protocol = rtl83xx_get_tag_protocol,
  1813. .setup = rtl83xx_setup,
  1814. .phy_read = dsa_phy_read,
  1815. .phy_write = dsa_phy_write,
  1816. .phylink_validate = rtl83xx_phylink_validate,
  1817. .phylink_mac_link_state = rtl83xx_phylink_mac_link_state,
  1818. .phylink_mac_config = rtl83xx_phylink_mac_config,
  1819. .phylink_mac_link_down = rtl83xx_phylink_mac_link_down,
  1820. .phylink_mac_link_up = rtl83xx_phylink_mac_link_up,
  1821. .get_strings = rtl83xx_get_strings,
  1822. .get_ethtool_stats = rtl83xx_get_ethtool_stats,
  1823. .get_sset_count = rtl83xx_get_sset_count,
  1824. .port_enable = rtl83xx_port_enable,
  1825. .port_disable = rtl83xx_port_disable,
  1826. .get_mac_eee = rtl83xx_get_mac_eee,
  1827. .set_mac_eee = rtl83xx_set_mac_eee,
  1828. .set_ageing_time = rtl83xx_set_ageing_time,
  1829. .port_bridge_join = rtl83xx_port_bridge_join,
  1830. .port_bridge_leave = rtl83xx_port_bridge_leave,
  1831. .port_stp_state_set = rtl83xx_port_stp_state_set,
  1832. .port_fast_age = rtl83xx_fast_age,
  1833. .port_vlan_filtering = rtl83xx_vlan_filtering,
  1834. .port_vlan_prepare = rtl83xx_vlan_prepare,
  1835. .port_vlan_add = rtl83xx_vlan_add,
  1836. .port_vlan_del = rtl83xx_vlan_del,
  1837. .port_fdb_add = rtl83xx_port_fdb_add,
  1838. .port_fdb_del = rtl83xx_port_fdb_del,
  1839. .port_fdb_dump = rtl83xx_port_fdb_dump,
  1840. .port_mdb_prepare = rtl83xx_port_mdb_prepare,
  1841. .port_mdb_add = rtl83xx_port_mdb_add,
  1842. .port_mdb_del = rtl83xx_port_mdb_del,
  1843. .port_mirror_add = rtl83xx_port_mirror_add,
  1844. .port_mirror_del = rtl83xx_port_mirror_del,
  1845. .port_lag_change = rtl83xx_port_lag_change,
  1846. .port_lag_join = rtl83xx_port_lag_join,
  1847. .port_lag_leave = rtl83xx_port_lag_leave,
  1848. .port_pre_bridge_flags = rtl83xx_port_pre_bridge_flags,
  1849. .port_bridge_flags = rtl83xx_port_bridge_flags,
  1850. };
  1851. const struct dsa_switch_ops rtl930x_switch_ops = {
  1852. .get_tag_protocol = rtl83xx_get_tag_protocol,
  1853. .setup = rtl93xx_setup,
  1854. .phy_read = dsa_phy_read,
  1855. .phy_write = dsa_phy_write,
  1856. .phylink_validate = rtl93xx_phylink_validate,
  1857. .phylink_mac_link_state = rtl93xx_phylink_mac_link_state,
  1858. .phylink_mac_config = rtl93xx_phylink_mac_config,
  1859. .phylink_mac_link_down = rtl93xx_phylink_mac_link_down,
  1860. .phylink_mac_link_up = rtl93xx_phylink_mac_link_up,
  1861. .get_strings = rtl83xx_get_strings,
  1862. .get_ethtool_stats = rtl83xx_get_ethtool_stats,
  1863. .get_sset_count = rtl83xx_get_sset_count,
  1864. .port_enable = rtl83xx_port_enable,
  1865. .port_disable = rtl83xx_port_disable,
  1866. .get_mac_eee = rtl93xx_get_mac_eee,
  1867. .set_mac_eee = rtl83xx_set_mac_eee,
  1868. .set_ageing_time = rtl83xx_set_ageing_time,
  1869. .port_bridge_join = rtl83xx_port_bridge_join,
  1870. .port_bridge_leave = rtl83xx_port_bridge_leave,
  1871. .port_stp_state_set = rtl83xx_port_stp_state_set,
  1872. .port_fast_age = rtl930x_fast_age,
  1873. .port_vlan_filtering = rtl83xx_vlan_filtering,
  1874. .port_vlan_prepare = rtl83xx_vlan_prepare,
  1875. .port_vlan_add = rtl83xx_vlan_add,
  1876. .port_vlan_del = rtl83xx_vlan_del,
  1877. .port_fdb_add = rtl83xx_port_fdb_add,
  1878. .port_fdb_del = rtl83xx_port_fdb_del,
  1879. .port_fdb_dump = rtl83xx_port_fdb_dump,
  1880. .port_mdb_prepare = rtl83xx_port_mdb_prepare,
  1881. .port_mdb_add = rtl83xx_port_mdb_add,
  1882. .port_mdb_del = rtl83xx_port_mdb_del,
  1883. .port_lag_change = rtl83xx_port_lag_change,
  1884. .port_lag_join = rtl83xx_port_lag_join,
  1885. .port_lag_leave = rtl83xx_port_lag_leave,
  1886. .port_pre_bridge_flags = rtl83xx_port_pre_bridge_flags,
  1887. .port_bridge_flags = rtl83xx_port_bridge_flags,
  1888. };