1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/etherdevice.h>
5 #include <linux/if_bridge.h>
6 #include <asm/mach-rtl838x/mach-rtl83xx.h>
10 extern struct rtl83xx_soc_info soc_info
;
12 static void rtl83xx_init_stats(struct rtl838x_switch_priv
*priv
)
14 mutex_lock(&priv
->reg_mutex
);
16 /* Enable statistics module: all counters plus debug.
17 * On RTL839x all counters are enabled by default
19 if (priv
->family_id
== RTL8380_FAMILY_ID
)
20 sw_w32_mask(0, 3, RTL838X_STAT_CTRL
);
22 /* Reset statistics counters */
23 sw_w32_mask(0, 1, priv
->r
->stat_rst
);
25 mutex_unlock(&priv
->reg_mutex
);
28 static void rtl83xx_enable_phy_polling(struct rtl838x_switch_priv
*priv
)
33 /* Enable all ports with a PHY, including the SFP-ports */
34 for (int i
= 0; i
< priv
->cpu_port
; i
++) {
35 if (priv
->ports
[i
].phy
)
39 pr_info("%s: %16llx\n", __func__
, v
);
40 priv
->r
->set_port_reg_le(v
, priv
->r
->smi_poll_ctrl
);
42 /* PHY update complete, there is no global PHY polling enable bit on the 9300 */
43 if (priv
->family_id
== RTL8390_FAMILY_ID
)
44 sw_w32_mask(0, BIT(7), RTL839X_SMI_GLB_CTRL
);
45 else if(priv
->family_id
== RTL9300_FAMILY_ID
)
46 sw_w32_mask(0, 0x8000, RTL838X_SMI_GLB_CTRL
);
49 const struct rtl83xx_mib_desc rtl83xx_mib
[] = {
50 MIB_DESC(2, 0xf8, "ifInOctets"),
51 MIB_DESC(2, 0xf0, "ifOutOctets"),
52 MIB_DESC(1, 0xec, "dot1dTpPortInDiscards"),
53 MIB_DESC(1, 0xe8, "ifInUcastPkts"),
54 MIB_DESC(1, 0xe4, "ifInMulticastPkts"),
55 MIB_DESC(1, 0xe0, "ifInBroadcastPkts"),
56 MIB_DESC(1, 0xdc, "ifOutUcastPkts"),
57 MIB_DESC(1, 0xd8, "ifOutMulticastPkts"),
58 MIB_DESC(1, 0xd4, "ifOutBroadcastPkts"),
59 MIB_DESC(1, 0xd0, "ifOutDiscards"),
60 MIB_DESC(1, 0xcc, ".3SingleCollisionFrames"),
61 MIB_DESC(1, 0xc8, ".3MultipleCollisionFrames"),
62 MIB_DESC(1, 0xc4, ".3DeferredTransmissions"),
63 MIB_DESC(1, 0xc0, ".3LateCollisions"),
64 MIB_DESC(1, 0xbc, ".3ExcessiveCollisions"),
65 MIB_DESC(1, 0xb8, ".3SymbolErrors"),
66 MIB_DESC(1, 0xb4, ".3ControlInUnknownOpcodes"),
67 MIB_DESC(1, 0xb0, ".3InPauseFrames"),
68 MIB_DESC(1, 0xac, ".3OutPauseFrames"),
69 MIB_DESC(1, 0xa8, "DropEvents"),
70 MIB_DESC(1, 0xa4, "tx_BroadcastPkts"),
71 MIB_DESC(1, 0xa0, "tx_MulticastPkts"),
72 MIB_DESC(1, 0x9c, "CRCAlignErrors"),
73 MIB_DESC(1, 0x98, "tx_UndersizePkts"),
74 MIB_DESC(1, 0x94, "rx_UndersizePkts"),
75 MIB_DESC(1, 0x90, "rx_UndersizedropPkts"),
76 MIB_DESC(1, 0x8c, "tx_OversizePkts"),
77 MIB_DESC(1, 0x88, "rx_OversizePkts"),
78 MIB_DESC(1, 0x84, "Fragments"),
79 MIB_DESC(1, 0x80, "Jabbers"),
80 MIB_DESC(1, 0x7c, "Collisions"),
81 MIB_DESC(1, 0x78, "tx_Pkts64Octets"),
82 MIB_DESC(1, 0x74, "rx_Pkts64Octets"),
83 MIB_DESC(1, 0x70, "tx_Pkts65to127Octets"),
84 MIB_DESC(1, 0x6c, "rx_Pkts65to127Octets"),
85 MIB_DESC(1, 0x68, "tx_Pkts128to255Octets"),
86 MIB_DESC(1, 0x64, "rx_Pkts128to255Octets"),
87 MIB_DESC(1, 0x60, "tx_Pkts256to511Octets"),
88 MIB_DESC(1, 0x5c, "rx_Pkts256to511Octets"),
89 MIB_DESC(1, 0x58, "tx_Pkts512to1023Octets"),
90 MIB_DESC(1, 0x54, "rx_Pkts512to1023Octets"),
91 MIB_DESC(1, 0x50, "tx_Pkts1024to1518Octets"),
92 MIB_DESC(1, 0x4c, "rx_StatsPkts1024to1518Octets"),
93 MIB_DESC(1, 0x48, "tx_Pkts1519toMaxOctets"),
94 MIB_DESC(1, 0x44, "rx_Pkts1519toMaxOctets"),
95 MIB_DESC(1, 0x40, "rxMacDiscards")
102 static enum dsa_tag_protocol
rtl83xx_get_tag_protocol(struct dsa_switch
*ds
,
104 enum dsa_tag_protocol mprot
)
106 /* The switch does not tag the frames, instead internally the header
107 * structure for each packet is tagged accordingly.
109 return DSA_TAG_PROTO_TRAILER
;
112 /* Initialize all VLANS */
113 static void rtl83xx_vlan_setup(struct rtl838x_switch_priv
*priv
)
115 struct rtl838x_vlan_info info
;
117 pr_info("In %s\n", __func__
);
119 priv
->r
->vlan_profile_setup(0);
120 priv
->r
->vlan_profile_setup(1);
121 pr_info("UNKNOWN_MC_PMASK: %016llx\n", priv
->r
->read_mcast_pmask(UNKNOWN_MC_PMASK
));
122 priv
->r
->vlan_profile_dump(0);
124 info
.fid
= 0; /* Default Forwarding ID / MSTI */
125 info
.hash_uc_fid
= false; /* Do not build the L2 lookup hash with FID, but VID */
126 info
.hash_mc_fid
= false; /* Do the same for Multicast packets */
127 info
.profile_id
= 0; /* Use default Vlan Profile 0 */
128 info
.tagged_ports
= 0; /* Initially no port members */
129 if (priv
->family_id
== RTL9310_FAMILY_ID
) {
131 info
.multicast_grp_mask
= 0;
132 info
.l2_tunnel_list_id
= -1;
135 /* Initialize all vlans 0-4095 */
136 for (int i
= 0; i
< MAX_VLANS
; i
++)
137 priv
->r
->vlan_set_tagged(i
, &info
);
139 /* reset PVIDs; defaults to 1 on reset */
140 for (int i
= 0; i
<= priv
->cpu_port
; i
++) {
141 priv
->r
->vlan_port_pvid_set(i
, PBVLAN_TYPE_INNER
, 1);
142 priv
->r
->vlan_port_pvid_set(i
, PBVLAN_TYPE_OUTER
, 1);
143 priv
->r
->vlan_port_pvidmode_set(i
, PBVLAN_TYPE_INNER
, PBVLAN_MODE_UNTAG_AND_PRITAG
);
144 priv
->r
->vlan_port_pvidmode_set(i
, PBVLAN_TYPE_OUTER
, PBVLAN_MODE_UNTAG_AND_PRITAG
);
147 /* Set forwarding action based on inner VLAN tag */
148 for (int i
= 0; i
< priv
->cpu_port
; i
++)
149 priv
->r
->vlan_fwd_on_inner(i
, true);
152 static void rtl83xx_setup_bpdu_traps(struct rtl838x_switch_priv
*priv
)
154 for (int i
= 0; i
< priv
->cpu_port
; i
++)
155 priv
->r
->set_receive_management_action(i
, BPDU
, TRAP2CPU
);
158 static void rtl83xx_setup_lldp_traps(struct rtl838x_switch_priv
*priv
)
160 for (int i
= 0; i
< priv
->cpu_port
; i
++)
161 priv
->r
->set_receive_management_action(i
, LLDP
, TRAP2CPU
);
164 static void rtl83xx_port_set_salrn(struct rtl838x_switch_priv
*priv
,
165 int port
, bool enable
)
167 int shift
= SALRN_PORT_SHIFT(port
);
168 int val
= enable
? SALRN_MODE_HARDWARE
: SALRN_MODE_DISABLED
;
170 sw_w32_mask(SALRN_MODE_MASK
<< shift
, val
<< shift
,
171 priv
->r
->l2_port_new_salrn(port
));
174 static int rtl83xx_setup(struct dsa_switch
*ds
)
176 struct rtl838x_switch_priv
*priv
= ds
->priv
;
178 pr_debug("%s called\n", __func__
);
180 /* Disable MAC polling the PHY so that we can start configuration */
181 priv
->r
->set_port_reg_le(0ULL, priv
->r
->smi_poll_ctrl
);
183 for (int i
= 0; i
< ds
->num_ports
; i
++)
184 priv
->ports
[i
].enable
= false;
185 priv
->ports
[priv
->cpu_port
].enable
= true;
187 /* Configure ports so they are disabled by default, but once enabled
188 * they will work in isolated mode (only traffic between port and CPU).
190 for (int i
= 0; i
< priv
->cpu_port
; i
++) {
191 if (priv
->ports
[i
].phy
) {
192 priv
->ports
[i
].pm
= BIT_ULL(priv
->cpu_port
);
193 priv
->r
->traffic_set(i
, BIT_ULL(i
));
196 priv
->r
->traffic_set(priv
->cpu_port
, BIT_ULL(priv
->cpu_port
));
198 /* For standalone ports, forward packets even if a static fdb
199 * entry for the source address exists on another port.
201 if (priv
->r
->set_static_move_action
) {
202 for (int i
= 0; i
<= priv
->cpu_port
; i
++)
203 priv
->r
->set_static_move_action(i
, true);
206 if (priv
->family_id
== RTL8380_FAMILY_ID
)
207 rtl838x_print_matrix();
209 rtl839x_print_matrix();
211 rtl83xx_init_stats(priv
);
213 rtl83xx_vlan_setup(priv
);
215 rtl83xx_setup_bpdu_traps(priv
);
216 rtl83xx_setup_lldp_traps(priv
);
218 ds
->configure_vlan_while_not_filtering
= true;
220 priv
->r
->l2_learning_setup();
222 rtl83xx_port_set_salrn(priv
, priv
->cpu_port
, false);
223 ds
->assisted_learning_on_cpu_port
= true;
225 /* Make sure all frames sent to the switch's MAC are trapped to the CPU-port
226 * 0: FWD, 1: DROP, 2: TRAP2CPU
228 if (priv
->family_id
== RTL8380_FAMILY_ID
)
229 sw_w32(0x2, RTL838X_SPCL_TRAP_SWITCH_MAC_CTRL
);
231 sw_w32(0x2, RTL839X_SPCL_TRAP_SWITCH_MAC_CTRL
);
233 /* Enable MAC Polling PHY again */
234 rtl83xx_enable_phy_polling(priv
);
235 pr_debug("Please wait until PHY is settled\n");
237 priv
->r
->pie_init(priv
);
242 static int rtl93xx_setup(struct dsa_switch
*ds
)
244 struct rtl838x_switch_priv
*priv
= ds
->priv
;
246 pr_info("%s called\n", __func__
);
248 /* Disable MAC polling the PHY so that we can start configuration */
249 if (priv
->family_id
== RTL9300_FAMILY_ID
)
250 sw_w32(0, RTL930X_SMI_POLL_CTRL
);
252 if (priv
->family_id
== RTL9310_FAMILY_ID
) {
253 sw_w32(0, RTL931X_SMI_PORT_POLLING_CTRL
);
254 sw_w32(0, RTL931X_SMI_PORT_POLLING_CTRL
+ 4);
257 /* Disable all ports except CPU port */
258 for (int i
= 0; i
< ds
->num_ports
; i
++)
259 priv
->ports
[i
].enable
= false;
260 priv
->ports
[priv
->cpu_port
].enable
= true;
262 /* Configure ports so they are disabled by default, but once enabled
263 * they will work in isolated mode (only traffic between port and CPU).
265 for (int i
= 0; i
< priv
->cpu_port
; i
++) {
266 if (priv
->ports
[i
].phy
) {
267 priv
->ports
[i
].pm
= BIT_ULL(priv
->cpu_port
);
268 priv
->r
->traffic_set(i
, BIT_ULL(i
));
271 priv
->r
->traffic_set(priv
->cpu_port
, BIT_ULL(priv
->cpu_port
));
273 rtl930x_print_matrix();
275 /* TODO: Initialize statistics */
277 rtl83xx_vlan_setup(priv
);
279 ds
->configure_vlan_while_not_filtering
= true;
281 priv
->r
->l2_learning_setup();
283 rtl83xx_port_set_salrn(priv
, priv
->cpu_port
, false);
284 ds
->assisted_learning_on_cpu_port
= true;
286 rtl83xx_enable_phy_polling(priv
);
288 priv
->r
->pie_init(priv
);
290 priv
->r
->led_init(priv
);
295 static int rtl93xx_get_sds(struct phy_device
*phydev
)
297 struct device
*dev
= &phydev
->mdio
.dev
;
298 struct device_node
*dn
;
305 if (of_property_read_u32(dn
, "sds", &sds_num
))
308 dev_err(dev
, "No DT node.\n");
315 static void rtl83xx_phylink_validate(struct dsa_switch
*ds
, int port
,
316 unsigned long *supported
,
317 struct phylink_link_state
*state
)
319 struct rtl838x_switch_priv
*priv
= ds
->priv
;
320 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
322 pr_debug("In %s port %d, state is %d", __func__
, port
, state
->interface
);
324 if (!phy_interface_mode_is_rgmii(state
->interface
) &&
325 state
->interface
!= PHY_INTERFACE_MODE_NA
&&
326 state
->interface
!= PHY_INTERFACE_MODE_1000BASEX
&&
327 state
->interface
!= PHY_INTERFACE_MODE_MII
&&
328 state
->interface
!= PHY_INTERFACE_MODE_REVMII
&&
329 state
->interface
!= PHY_INTERFACE_MODE_GMII
&&
330 state
->interface
!= PHY_INTERFACE_MODE_QSGMII
&&
331 state
->interface
!= PHY_INTERFACE_MODE_INTERNAL
&&
332 state
->interface
!= PHY_INTERFACE_MODE_SGMII
) {
333 bitmap_zero(supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
335 "Unsupported interface: %d for port %d\n",
336 state
->interface
, port
);
340 /* Allow all the expected bits */
341 phylink_set(mask
, Autoneg
);
342 phylink_set_port_modes(mask
);
343 phylink_set(mask
, Pause
);
344 phylink_set(mask
, Asym_Pause
);
346 /* With the exclusion of MII and Reverse MII, we support Gigabit,
347 * including Half duplex
349 if (state
->interface
!= PHY_INTERFACE_MODE_MII
&&
350 state
->interface
!= PHY_INTERFACE_MODE_REVMII
) {
351 phylink_set(mask
, 1000baseT_Full
);
352 phylink_set(mask
, 1000baseT_Half
);
355 /* On both the 8380 and 8382, ports 24-27 are SFP ports */
356 if (port
>= 24 && port
<= 27 && priv
->family_id
== RTL8380_FAMILY_ID
)
357 phylink_set(mask
, 1000baseX_Full
);
359 /* On the RTL839x family of SoCs, ports 48 to 51 are SFP ports */
360 if (port
>= 48 && port
<= 51 && priv
->family_id
== RTL8390_FAMILY_ID
)
361 phylink_set(mask
, 1000baseX_Full
);
363 phylink_set(mask
, 10baseT_Half
);
364 phylink_set(mask
, 10baseT_Full
);
365 phylink_set(mask
, 100baseT_Half
);
366 phylink_set(mask
, 100baseT_Full
);
368 bitmap_and(supported
, supported
, mask
,
369 __ETHTOOL_LINK_MODE_MASK_NBITS
);
370 bitmap_and(state
->advertising
, state
->advertising
, mask
,
371 __ETHTOOL_LINK_MODE_MASK_NBITS
);
374 static void rtl93xx_phylink_validate(struct dsa_switch
*ds
, int port
,
375 unsigned long *supported
,
376 struct phylink_link_state
*state
)
378 struct rtl838x_switch_priv
*priv
= ds
->priv
;
379 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
381 pr_debug("In %s port %d, state is %d (%s)", __func__
, port
, state
->interface
,
382 phy_modes(state
->interface
));
384 if (!phy_interface_mode_is_rgmii(state
->interface
) &&
385 state
->interface
!= PHY_INTERFACE_MODE_NA
&&
386 state
->interface
!= PHY_INTERFACE_MODE_1000BASEX
&&
387 state
->interface
!= PHY_INTERFACE_MODE_MII
&&
388 state
->interface
!= PHY_INTERFACE_MODE_REVMII
&&
389 state
->interface
!= PHY_INTERFACE_MODE_GMII
&&
390 state
->interface
!= PHY_INTERFACE_MODE_QSGMII
&&
391 state
->interface
!= PHY_INTERFACE_MODE_XGMII
&&
392 state
->interface
!= PHY_INTERFACE_MODE_HSGMII
&&
393 state
->interface
!= PHY_INTERFACE_MODE_10GBASER
&&
394 state
->interface
!= PHY_INTERFACE_MODE_10GKR
&&
395 state
->interface
!= PHY_INTERFACE_MODE_USXGMII
&&
396 state
->interface
!= PHY_INTERFACE_MODE_INTERNAL
&&
397 state
->interface
!= PHY_INTERFACE_MODE_SGMII
) {
398 bitmap_zero(supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
400 "Unsupported interface: %d for port %d\n",
401 state
->interface
, port
);
405 /* Allow all the expected bits */
406 phylink_set(mask
, Autoneg
);
407 phylink_set_port_modes(mask
);
408 phylink_set(mask
, Pause
);
409 phylink_set(mask
, Asym_Pause
);
411 /* With the exclusion of MII and Reverse MII, we support Gigabit,
412 * including Half duplex
414 if (state
->interface
!= PHY_INTERFACE_MODE_MII
&&
415 state
->interface
!= PHY_INTERFACE_MODE_REVMII
) {
416 phylink_set(mask
, 1000baseT_Full
);
417 phylink_set(mask
, 1000baseT_Half
);
420 /* Internal phys of the RTL93xx family provide 10G */
421 if (priv
->ports
[port
].phy_is_integrated
&&
422 state
->interface
== PHY_INTERFACE_MODE_1000BASEX
) {
423 phylink_set(mask
, 1000baseX_Full
);
424 } else if (priv
->ports
[port
].phy_is_integrated
) {
425 phylink_set(mask
, 1000baseX_Full
);
426 phylink_set(mask
, 10000baseKR_Full
);
427 phylink_set(mask
, 10000baseSR_Full
);
428 phylink_set(mask
, 10000baseCR_Full
);
430 if (state
->interface
== PHY_INTERFACE_MODE_INTERNAL
) {
431 phylink_set(mask
, 1000baseX_Full
);
432 phylink_set(mask
, 1000baseT_Full
);
433 phylink_set(mask
, 10000baseKR_Full
);
434 phylink_set(mask
, 10000baseT_Full
);
435 phylink_set(mask
, 10000baseSR_Full
);
436 phylink_set(mask
, 10000baseCR_Full
);
439 if (state
->interface
== PHY_INTERFACE_MODE_USXGMII
) {
440 phylink_set(mask
, 2500baseT_Full
);
441 phylink_set(mask
, 5000baseT_Full
);
442 phylink_set(mask
, 10000baseT_Full
);
445 phylink_set(mask
, 10baseT_Half
);
446 phylink_set(mask
, 10baseT_Full
);
447 phylink_set(mask
, 100baseT_Half
);
448 phylink_set(mask
, 100baseT_Full
);
450 bitmap_and(supported
, supported
, mask
,
451 __ETHTOOL_LINK_MODE_MASK_NBITS
);
452 bitmap_and(state
->advertising
, state
->advertising
, mask
,
453 __ETHTOOL_LINK_MODE_MASK_NBITS
);
454 pr_debug("%s leaving supported: %*pb", __func__
, __ETHTOOL_LINK_MODE_MASK_NBITS
, supported
);
457 static int rtl83xx_phylink_mac_link_state(struct dsa_switch
*ds
, int port
,
458 struct phylink_link_state
*state
)
460 struct rtl838x_switch_priv
*priv
= ds
->priv
;
464 if (port
< 0 || port
> priv
->cpu_port
)
468 link
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_sts
);
469 if (link
& BIT_ULL(port
))
471 pr_debug("%s: link state port %d: %llx\n", __func__
, port
, link
& BIT_ULL(port
));
474 if (priv
->r
->get_port_reg_le(priv
->r
->mac_link_dup_sts
) & BIT_ULL(port
))
477 speed
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_spd_sts(port
));
478 speed
>>= (port
% 16) << 1;
479 switch (speed
& 0x3) {
481 state
->speed
= SPEED_10
;
484 state
->speed
= SPEED_100
;
487 state
->speed
= SPEED_1000
;
490 if (priv
->family_id
== RTL9300_FAMILY_ID
491 && (port
== 24 || port
== 26)) /* Internal serdes */
492 state
->speed
= SPEED_2500
;
494 state
->speed
= SPEED_100
; /* Is in fact 500Mbit */
497 state
->pause
&= (MLO_PAUSE_RX
| MLO_PAUSE_TX
);
498 if (priv
->r
->get_port_reg_le(priv
->r
->mac_rx_pause_sts
) & BIT_ULL(port
))
499 state
->pause
|= MLO_PAUSE_RX
;
500 if (priv
->r
->get_port_reg_le(priv
->r
->mac_tx_pause_sts
) & BIT_ULL(port
))
501 state
->pause
|= MLO_PAUSE_TX
;
506 static int rtl93xx_phylink_mac_link_state(struct dsa_switch
*ds
, int port
,
507 struct phylink_link_state
*state
)
509 struct rtl838x_switch_priv
*priv
= ds
->priv
;
514 if (port
< 0 || port
> priv
->cpu_port
)
517 /* On the RTL9300 for at least the RTL8226B PHY, the MAC-side link
518 * state needs to be read twice in order to read a correct result.
519 * This would not be necessary for ports connected e.g. to RTL8218D
523 link
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_sts
);
524 link
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_sts
);
525 if (link
& BIT_ULL(port
))
528 if (priv
->family_id
== RTL9310_FAMILY_ID
)
529 media
= priv
->r
->get_port_reg_le(RTL931X_MAC_LINK_MEDIA_STS
);
531 if (priv
->family_id
== RTL9300_FAMILY_ID
)
532 media
= sw_r32(RTL930X_MAC_LINK_MEDIA_STS
);
534 if (media
& BIT_ULL(port
))
537 pr_debug("%s: link state port %d: %llx, media %llx\n", __func__
, port
,
538 link
& BIT_ULL(port
), media
);
541 if (priv
->r
->get_port_reg_le(priv
->r
->mac_link_dup_sts
) & BIT_ULL(port
))
544 speed
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_spd_sts(port
));
545 speed
>>= (port
% 8) << 2;
546 switch (speed
& 0xf) {
548 state
->speed
= SPEED_10
;
551 state
->speed
= SPEED_100
;
555 state
->speed
= SPEED_1000
;
558 state
->speed
= SPEED_10000
;
562 state
->speed
= SPEED_2500
;
565 state
->speed
= SPEED_5000
;
568 pr_err("%s: unknown speed: %d\n", __func__
, (u32
)speed
& 0xf);
571 if (priv
->family_id
== RTL9310_FAMILY_ID
572 && (port
>= 52 && port
<= 55)) { /* Internal serdes */
573 state
->speed
= SPEED_10000
;
578 pr_debug("%s: speed is: %d %d\n", __func__
, (u32
)speed
& 0xf, state
->speed
);
579 state
->pause
&= (MLO_PAUSE_RX
| MLO_PAUSE_TX
);
580 if (priv
->r
->get_port_reg_le(priv
->r
->mac_rx_pause_sts
) & BIT_ULL(port
))
581 state
->pause
|= MLO_PAUSE_RX
;
582 if (priv
->r
->get_port_reg_le(priv
->r
->mac_tx_pause_sts
) & BIT_ULL(port
))
583 state
->pause
|= MLO_PAUSE_TX
;
588 static void rtl83xx_config_interface(int port
, phy_interface_t interface
)
590 u32 old
, int_shift
, sds_shift
;
605 old
= sw_r32(RTL838X_SDS_MODE_SEL
);
607 case PHY_INTERFACE_MODE_1000BASEX
:
608 if ((old
>> sds_shift
& 0x1f) == 4)
610 sw_w32_mask(0x7 << int_shift
, 1 << int_shift
, RTL838X_INT_MODE_CTRL
);
611 sw_w32_mask(0x1f << sds_shift
, 4 << sds_shift
, RTL838X_SDS_MODE_SEL
);
613 case PHY_INTERFACE_MODE_SGMII
:
614 if ((old
>> sds_shift
& 0x1f) == 2)
616 sw_w32_mask(0x7 << int_shift
, 2 << int_shift
, RTL838X_INT_MODE_CTRL
);
617 sw_w32_mask(0x1f << sds_shift
, 2 << sds_shift
, RTL838X_SDS_MODE_SEL
);
622 pr_debug("configured port %d for interface %s\n", port
, phy_modes(interface
));
625 static void rtl83xx_phylink_mac_config(struct dsa_switch
*ds
, int port
,
627 const struct phylink_link_state
*state
)
629 struct rtl838x_switch_priv
*priv
= ds
->priv
;
631 int speed_bit
= priv
->family_id
== RTL8380_FAMILY_ID
? 4 : 3;
633 pr_debug("%s port %d, mode %x\n", __func__
, port
, mode
);
635 if (port
== priv
->cpu_port
) {
636 /* Set Speed, duplex, flow control
637 * FORCE_EN | LINK_EN | NWAY_EN | DUP_SEL
638 * | SPD_SEL = 0b10 | FORCE_FC_EN | PHY_MASTER_SLV_MANUAL_EN
641 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
642 sw_w32(0x6192F, priv
->r
->mac_force_mode_ctrl(priv
->cpu_port
));
643 /* allow CRC errors on CPU-port */
644 sw_w32_mask(0, 0x8, RTL838X_MAC_PORT_CTRL(priv
->cpu_port
));
646 sw_w32_mask(0, 3, priv
->r
->mac_force_mode_ctrl(priv
->cpu_port
));
651 reg
= sw_r32(priv
->r
->mac_force_mode_ctrl(port
));
652 /* Auto-Negotiation does not work for MAC in RTL8390 */
653 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
654 if (mode
== MLO_AN_PHY
|| phylink_autoneg_inband(mode
)) {
655 pr_debug("PHY autonegotiates\n");
656 reg
|= RTL838X_NWAY_EN
;
657 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
658 rtl83xx_config_interface(port
, state
->interface
);
663 if (mode
!= MLO_AN_FIXED
)
664 pr_debug("Fixed state.\n");
666 /* Clear id_mode_dis bit, and the existing port mode, let
667 * RGMII_MODE_EN bet set by mac_link_{up,down} */
668 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
669 reg
&= ~(RTL838X_RX_PAUSE_EN
| RTL838X_TX_PAUSE_EN
);
670 if (state
->pause
& MLO_PAUSE_TXRX_MASK
) {
671 if (state
->pause
& MLO_PAUSE_TX
)
672 reg
|= RTL838X_TX_PAUSE_EN
;
673 reg
|= RTL838X_RX_PAUSE_EN
;
675 } else if (priv
->family_id
== RTL8390_FAMILY_ID
) {
676 reg
&= ~(RTL839X_RX_PAUSE_EN
| RTL839X_TX_PAUSE_EN
);
677 if (state
->pause
& MLO_PAUSE_TXRX_MASK
) {
678 if (state
->pause
& MLO_PAUSE_TX
)
679 reg
|= RTL839X_TX_PAUSE_EN
;
680 reg
|= RTL839X_RX_PAUSE_EN
;
685 reg
&= ~(3 << speed_bit
);
686 switch (state
->speed
) {
688 reg
|= 2 << speed_bit
;
691 reg
|= 1 << speed_bit
;
694 break; /* Ignore, including 10MBit which has a speed value of 0 */
697 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
698 reg
&= ~(RTL838X_DUPLEX_MODE
| RTL838X_FORCE_LINK_EN
);
700 reg
|= RTL838X_FORCE_LINK_EN
;
701 if (state
->duplex
== RTL838X_DUPLEX_MODE
)
702 reg
|= RTL838X_DUPLEX_MODE
;
703 } else if (priv
->family_id
== RTL8390_FAMILY_ID
) {
704 reg
&= ~(RTL839X_DUPLEX_MODE
| RTL839X_FORCE_LINK_EN
);
706 reg
|= RTL839X_FORCE_LINK_EN
;
707 if (state
->duplex
== RTL839X_DUPLEX_MODE
)
708 reg
|= RTL839X_DUPLEX_MODE
;
711 /* LAG members must use DUPLEX and we need to enable the link */
712 if (priv
->lagmembers
& BIT_ULL(port
)) {
713 switch(priv
->family_id
) {
714 case RTL8380_FAMILY_ID
:
715 reg
|= (RTL838X_DUPLEX_MODE
| RTL838X_FORCE_LINK_EN
);
717 case RTL8390_FAMILY_ID
:
718 reg
|= (RTL839X_DUPLEX_MODE
| RTL839X_FORCE_LINK_EN
);
724 if (priv
->family_id
== RTL8380_FAMILY_ID
)
725 reg
&= ~RTL838X_NWAY_EN
;
726 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
729 static void rtl931x_phylink_mac_config(struct dsa_switch
*ds
, int port
,
731 const struct phylink_link_state
*state
)
733 struct rtl838x_switch_priv
*priv
= ds
->priv
;
737 sds_num
= priv
->ports
[port
].sds_num
;
738 pr_info("%s: speed %d sds_num %d\n", __func__
, state
->speed
, sds_num
);
740 switch (state
->interface
) {
741 case PHY_INTERFACE_MODE_HSGMII
:
742 pr_info("%s setting mode PHY_INTERFACE_MODE_HSGMII\n", __func__
);
743 band
= rtl931x_sds_cmu_band_get(sds_num
, PHY_INTERFACE_MODE_HSGMII
);
744 rtl931x_sds_init(sds_num
, PHY_INTERFACE_MODE_HSGMII
);
745 band
= rtl931x_sds_cmu_band_set(sds_num
, true, 62, PHY_INTERFACE_MODE_HSGMII
);
747 case PHY_INTERFACE_MODE_1000BASEX
:
748 band
= rtl931x_sds_cmu_band_get(sds_num
, PHY_INTERFACE_MODE_1000BASEX
);
749 rtl931x_sds_init(sds_num
, PHY_INTERFACE_MODE_1000BASEX
);
751 case PHY_INTERFACE_MODE_XGMII
:
752 band
= rtl931x_sds_cmu_band_get(sds_num
, PHY_INTERFACE_MODE_XGMII
);
753 rtl931x_sds_init(sds_num
, PHY_INTERFACE_MODE_XGMII
);
755 case PHY_INTERFACE_MODE_10GBASER
:
756 case PHY_INTERFACE_MODE_10GKR
:
757 band
= rtl931x_sds_cmu_band_get(sds_num
, PHY_INTERFACE_MODE_10GBASER
);
758 rtl931x_sds_init(sds_num
, PHY_INTERFACE_MODE_10GBASER
);
760 case PHY_INTERFACE_MODE_USXGMII
:
761 /* Translates to MII_USXGMII_10GSXGMII */
762 band
= rtl931x_sds_cmu_band_get(sds_num
, PHY_INTERFACE_MODE_USXGMII
);
763 rtl931x_sds_init(sds_num
, PHY_INTERFACE_MODE_USXGMII
);
765 case PHY_INTERFACE_MODE_SGMII
:
766 pr_info("%s setting mode PHY_INTERFACE_MODE_SGMII\n", __func__
);
767 band
= rtl931x_sds_cmu_band_get(sds_num
, PHY_INTERFACE_MODE_SGMII
);
768 rtl931x_sds_init(sds_num
, PHY_INTERFACE_MODE_SGMII
);
769 band
= rtl931x_sds_cmu_band_set(sds_num
, true, 62, PHY_INTERFACE_MODE_SGMII
);
771 case PHY_INTERFACE_MODE_QSGMII
:
772 band
= rtl931x_sds_cmu_band_get(sds_num
, PHY_INTERFACE_MODE_QSGMII
);
773 rtl931x_sds_init(sds_num
, PHY_INTERFACE_MODE_QSGMII
);
776 pr_err("%s: unknown serdes mode: %s\n",
777 __func__
, phy_modes(state
->interface
));
781 reg
= sw_r32(priv
->r
->mac_force_mode_ctrl(port
));
782 pr_info("%s reading FORCE_MODE_CTRL: %08x\n", __func__
, reg
);
784 reg
&= ~(RTL931X_DUPLEX_MODE
| RTL931X_FORCE_EN
| RTL931X_FORCE_LINK_EN
);
787 reg
|= 0x2 << 12; /* Set SMI speed to 0x2 */
789 reg
|= RTL931X_TX_PAUSE_EN
| RTL931X_RX_PAUSE_EN
;
791 if (priv
->lagmembers
& BIT_ULL(port
))
792 reg
|= RTL931X_DUPLEX_MODE
;
794 if (state
->duplex
== DUPLEX_FULL
)
795 reg
|= RTL931X_DUPLEX_MODE
;
797 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
801 static void rtl93xx_phylink_mac_config(struct dsa_switch
*ds
, int port
,
803 const struct phylink_link_state
*state
)
805 struct rtl838x_switch_priv
*priv
= ds
->priv
;
809 pr_info("%s port %d, mode %x, phy-mode: %s, speed %d, link %d\n", __func__
,
810 port
, mode
, phy_modes(state
->interface
), state
->speed
, state
->link
);
812 /* Nothing to be done for the CPU-port */
813 if (port
== priv
->cpu_port
)
816 if (priv
->family_id
== RTL9310_FAMILY_ID
)
817 return rtl931x_phylink_mac_config(ds
, port
, mode
, state
);
819 sds_num
= priv
->ports
[port
].sds_num
;
820 pr_info("%s SDS is %d\n", __func__
, sds_num
);
822 (state
->interface
== PHY_INTERFACE_MODE_1000BASEX
||
823 state
->interface
== PHY_INTERFACE_MODE_10GBASER
))
824 rtl9300_serdes_setup(port
, sds_num
, state
->interface
);
826 reg
= sw_r32(priv
->r
->mac_force_mode_ctrl(port
));
829 switch (state
->speed
) {
846 /* Also covers 10M */
851 reg
|= RTL930X_FORCE_LINK_EN
;
853 if (priv
->lagmembers
& BIT_ULL(port
))
854 reg
|= RTL930X_DUPLEX_MODE
| RTL930X_FORCE_LINK_EN
;
856 if (state
->duplex
== DUPLEX_FULL
)
857 reg
|= RTL930X_DUPLEX_MODE
;
859 reg
&= ~RTL930X_DUPLEX_MODE
; /* Clear duplex bit otherwise */
861 if (priv
->ports
[port
].phy_is_integrated
)
862 reg
&= ~RTL930X_FORCE_EN
; /* Clear MAC_FORCE_EN to allow SDS-MAC link */
864 reg
|= RTL930X_FORCE_EN
;
866 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
869 static void rtl83xx_phylink_mac_link_down(struct dsa_switch
*ds
, int port
,
871 phy_interface_t interface
)
873 struct rtl838x_switch_priv
*priv
= ds
->priv
;
875 /* Stop TX/RX to port */
876 sw_w32_mask(0x3, 0, priv
->r
->mac_port_ctrl(port
));
878 /* No longer force link */
879 sw_w32_mask(0x3, 0, priv
->r
->mac_force_mode_ctrl(port
));
882 static void rtl93xx_phylink_mac_link_down(struct dsa_switch
*ds
, int port
,
884 phy_interface_t interface
)
886 struct rtl838x_switch_priv
*priv
= ds
->priv
;
889 /* Stop TX/RX to port */
890 sw_w32_mask(0x3, 0, priv
->r
->mac_port_ctrl(port
));
892 /* No longer force link */
893 if (priv
->family_id
== RTL9300_FAMILY_ID
)
894 v
= RTL930X_FORCE_EN
| RTL930X_FORCE_LINK_EN
;
895 else if (priv
->family_id
== RTL9310_FAMILY_ID
)
896 v
= RTL931X_FORCE_EN
| RTL931X_FORCE_LINK_EN
;
897 sw_w32_mask(v
, 0, priv
->r
->mac_force_mode_ctrl(port
));
900 static void rtl83xx_phylink_mac_link_up(struct dsa_switch
*ds
, int port
,
902 phy_interface_t interface
,
903 struct phy_device
*phydev
,
904 int speed
, int duplex
,
905 bool tx_pause
, bool rx_pause
)
907 struct rtl838x_switch_priv
*priv
= ds
->priv
;
908 /* Restart TX/RX to port */
909 sw_w32_mask(0, 0x3, priv
->r
->mac_port_ctrl(port
));
910 /* TODO: Set speed/duplex/pauses */
913 static void rtl93xx_phylink_mac_link_up(struct dsa_switch
*ds
, int port
,
915 phy_interface_t interface
,
916 struct phy_device
*phydev
,
917 int speed
, int duplex
,
918 bool tx_pause
, bool rx_pause
)
920 struct rtl838x_switch_priv
*priv
= ds
->priv
;
922 /* Restart TX/RX to port */
923 sw_w32_mask(0, 0x3, priv
->r
->mac_port_ctrl(port
));
924 /* TODO: Set speed/duplex/pauses */
927 static void rtl83xx_get_strings(struct dsa_switch
*ds
,
928 int port
, u32 stringset
, u8
*data
)
930 if (stringset
!= ETH_SS_STATS
)
933 for (int i
= 0; i
< ARRAY_SIZE(rtl83xx_mib
); i
++)
934 ethtool_puts(&data
, rtl83xx_mib
[i
].name
);
937 static void rtl83xx_get_ethtool_stats(struct dsa_switch
*ds
, int port
,
940 struct rtl838x_switch_priv
*priv
= ds
->priv
;
941 const struct rtl83xx_mib_desc
*mib
;
944 for (int i
= 0; i
< ARRAY_SIZE(rtl83xx_mib
); i
++) {
945 mib
= &rtl83xx_mib
[i
];
947 data
[i
] = sw_r32(priv
->r
->stat_port_std_mib
+ (port
<< 8) + 252 - mib
->offset
);
948 if (mib
->size
== 2) {
949 h
= sw_r32(priv
->r
->stat_port_std_mib
+ (port
<< 8) + 248 - mib
->offset
);
955 static int rtl83xx_get_sset_count(struct dsa_switch
*ds
, int port
, int sset
)
957 if (sset
!= ETH_SS_STATS
)
960 return ARRAY_SIZE(rtl83xx_mib
);
963 static int rtl83xx_mc_group_alloc(struct rtl838x_switch_priv
*priv
, int port
)
965 int mc_group
= find_first_zero_bit(priv
->mc_group_bm
, MAX_MC_GROUPS
- 1);
968 if (mc_group
>= MAX_MC_GROUPS
- 1)
971 set_bit(mc_group
, priv
->mc_group_bm
);
972 portmask
= BIT_ULL(port
);
973 priv
->r
->write_mcast_pmask(mc_group
, portmask
);
978 static u64
rtl83xx_mc_group_add_port(struct rtl838x_switch_priv
*priv
, int mc_group
, int port
)
980 u64 portmask
= priv
->r
->read_mcast_pmask(mc_group
);
982 pr_debug("%s: %d\n", __func__
, port
);
984 portmask
|= BIT_ULL(port
);
985 priv
->r
->write_mcast_pmask(mc_group
, portmask
);
990 static u64
rtl83xx_mc_group_del_port(struct rtl838x_switch_priv
*priv
, int mc_group
, int port
)
992 u64 portmask
= priv
->r
->read_mcast_pmask(mc_group
);
994 pr_debug("%s: %d\n", __func__
, port
);
996 portmask
&= ~BIT_ULL(port
);
997 priv
->r
->write_mcast_pmask(mc_group
, portmask
);
999 clear_bit(mc_group
, priv
->mc_group_bm
);
1004 static int rtl83xx_port_enable(struct dsa_switch
*ds
, int port
,
1005 struct phy_device
*phydev
)
1007 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1010 pr_debug("%s: %x %d", __func__
, (u32
) priv
, port
);
1011 priv
->ports
[port
].enable
= true;
1013 /* enable inner tagging on egress, do not keep any tags */
1014 priv
->r
->vlan_port_keep_tag_set(port
, 0, 1);
1016 if (dsa_is_cpu_port(ds
, port
))
1019 /* add port to switch mask of CPU_PORT */
1020 priv
->r
->traffic_enable(priv
->cpu_port
, port
);
1022 if (priv
->is_lagmember
[port
]) {
1023 pr_debug("%s: %d is lag slave. ignore\n", __func__
, port
);
1027 /* add all other ports in the same bridge to switch mask of port */
1028 v
= priv
->r
->traffic_get(port
);
1029 v
|= priv
->ports
[port
].pm
;
1030 priv
->r
->traffic_set(port
, v
);
1032 /* TODO: Figure out if this is necessary */
1033 if (priv
->family_id
== RTL9300_FAMILY_ID
) {
1034 sw_w32_mask(0, BIT(port
), RTL930X_L2_PORT_SABLK_CTRL
);
1035 sw_w32_mask(0, BIT(port
), RTL930X_L2_PORT_DABLK_CTRL
);
1038 if (priv
->ports
[port
].sds_num
< 0)
1039 priv
->ports
[port
].sds_num
= rtl93xx_get_sds(phydev
);
1044 static void rtl83xx_port_disable(struct dsa_switch
*ds
, int port
)
1046 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1049 pr_debug("%s %x: %d", __func__
, (u32
)priv
, port
);
1050 /* you can only disable user ports */
1051 if (!dsa_is_user_port(ds
, port
))
1054 /* BUG: This does not work on RTL931X */
1055 /* remove port from switch mask of CPU_PORT */
1056 priv
->r
->traffic_disable(priv
->cpu_port
, port
);
1058 /* remove all other ports in the same bridge from switch mask of port */
1059 v
= priv
->r
->traffic_get(port
);
1060 v
&= ~priv
->ports
[port
].pm
;
1061 priv
->r
->traffic_set(port
, v
);
1063 priv
->ports
[port
].enable
= false;
1066 static int rtl83xx_set_mac_eee(struct dsa_switch
*ds
, int port
,
1067 struct ethtool_eee
*e
)
1069 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1071 if (e
->eee_enabled
&& !priv
->eee_enabled
) {
1072 pr_info("Globally enabling EEE\n");
1073 priv
->r
->init_eee(priv
, true);
1076 priv
->r
->port_eee_set(priv
, port
, e
->eee_enabled
);
1079 pr_info("Enabled EEE for port %d\n", port
);
1081 pr_info("Disabled EEE for port %d\n", port
);
1086 static int rtl83xx_get_mac_eee(struct dsa_switch
*ds
, int port
,
1087 struct ethtool_eee
*e
)
1089 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1091 e
->supported
= SUPPORTED_100baseT_Full
| SUPPORTED_1000baseT_Full
;
1093 priv
->r
->eee_port_ability(priv
, e
, port
);
1095 e
->eee_enabled
= priv
->ports
[port
].eee_enabled
;
1097 e
->eee_active
= !!(e
->advertised
& e
->lp_advertised
);
1102 static int rtl93xx_get_mac_eee(struct dsa_switch
*ds
, int port
,
1103 struct ethtool_eee
*e
)
1105 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1107 e
->supported
= SUPPORTED_100baseT_Full
|
1108 SUPPORTED_1000baseT_Full
|
1109 SUPPORTED_2500baseX_Full
;
1111 priv
->r
->eee_port_ability(priv
, e
, port
);
1113 e
->eee_enabled
= priv
->ports
[port
].eee_enabled
;
1115 e
->eee_active
= !!(e
->advertised
& e
->lp_advertised
);
1120 static int rtl83xx_set_ageing_time(struct dsa_switch
*ds
, unsigned int msec
)
1122 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1124 priv
->r
->set_ageing_time(msec
);
1129 static int rtl83xx_port_bridge_join(struct dsa_switch
*ds
, int port
,
1130 struct net_device
*bridge
)
1132 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1133 u64 port_bitmap
= BIT_ULL(priv
->cpu_port
), v
;
1135 pr_debug("%s %x: %d %llx", __func__
, (u32
)priv
, port
, port_bitmap
);
1137 if (priv
->is_lagmember
[port
]) {
1138 pr_debug("%s: %d is lag slave. ignore\n", __func__
, port
);
1142 mutex_lock(&priv
->reg_mutex
);
1143 for (int i
= 0; i
< ds
->num_ports
; i
++) {
1144 /* Add this port to the port matrix of the other ports in the
1145 * same bridge. If the port is disabled, port matrix is kept
1146 * and not being setup until the port becomes enabled.
1148 if (dsa_is_user_port(ds
, i
) && !priv
->is_lagmember
[i
] && i
!= port
) {
1149 if (dsa_to_port(ds
, i
)->bridge_dev
!= bridge
)
1151 if (priv
->ports
[i
].enable
)
1152 priv
->r
->traffic_enable(i
, port
);
1154 priv
->ports
[i
].pm
|= BIT_ULL(port
);
1155 port_bitmap
|= BIT_ULL(i
);
1159 /* Add all other ports to this port matrix. */
1160 if (priv
->ports
[port
].enable
) {
1161 priv
->r
->traffic_enable(priv
->cpu_port
, port
);
1162 v
= priv
->r
->traffic_get(port
);
1164 priv
->r
->traffic_set(port
, v
);
1166 priv
->ports
[port
].pm
|= port_bitmap
;
1168 if (priv
->r
->set_static_move_action
)
1169 priv
->r
->set_static_move_action(port
, false);
1171 mutex_unlock(&priv
->reg_mutex
);
1176 static void rtl83xx_port_bridge_leave(struct dsa_switch
*ds
, int port
,
1177 struct net_device
*bridge
)
1179 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1180 u64 port_bitmap
= 0, v
;
1182 pr_debug("%s %x: %d", __func__
, (u32
)priv
, port
);
1183 mutex_lock(&priv
->reg_mutex
);
1184 for (int i
= 0; i
< ds
->num_ports
; i
++) {
1185 /* Remove this port from the port matrix of the other ports
1186 * in the same bridge. If the port is disabled, port matrix
1187 * is kept and not being setup until the port becomes enabled.
1188 * And the other port's port matrix cannot be broken when the
1189 * other port is still a VLAN-aware port.
1191 if (dsa_is_user_port(ds
, i
) && i
!= port
) {
1192 if (dsa_to_port(ds
, i
)->bridge_dev
!= bridge
)
1194 if (priv
->ports
[i
].enable
)
1195 priv
->r
->traffic_disable(i
, port
);
1197 priv
->ports
[i
].pm
&= ~BIT_ULL(port
);
1198 port_bitmap
|= BIT_ULL(i
);
1202 /* Remove all other ports from this port matrix. */
1203 if (priv
->ports
[port
].enable
) {
1204 v
= priv
->r
->traffic_get(port
);
1206 priv
->r
->traffic_set(port
, v
);
1208 priv
->ports
[port
].pm
&= ~port_bitmap
;
1210 if (priv
->r
->set_static_move_action
)
1211 priv
->r
->set_static_move_action(port
, true);
1213 mutex_unlock(&priv
->reg_mutex
);
1216 void rtl83xx_port_stp_state_set(struct dsa_switch
*ds
, int port
, u8 state
)
1222 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1223 int n
= priv
->port_width
<< 1;
1225 /* Ports above or equal CPU port can never be configured */
1226 if (port
>= priv
->cpu_port
)
1229 mutex_lock(&priv
->reg_mutex
);
1231 /* For the RTL839x and following, the bits are left-aligned, 838x and 930x
1232 * have 64 bit fields, 839x and 931x have 128 bit fields
1234 if (priv
->family_id
== RTL8390_FAMILY_ID
)
1236 if (priv
->family_id
== RTL9300_FAMILY_ID
)
1238 if (priv
->family_id
== RTL9310_FAMILY_ID
)
1241 index
= n
- (pos
>> 4) - 1;
1242 bit
= (pos
<< 1) % 32;
1244 priv
->r
->stp_get(priv
, msti
, port_state
);
1246 pr_debug("Current state, port %d: %d\n", port
, (port_state
[index
] >> bit
) & 3);
1247 port_state
[index
] &= ~(3 << bit
);
1250 case BR_STATE_DISABLED
: /* 0 */
1251 port_state
[index
] |= (0 << bit
);
1253 case BR_STATE_BLOCKING
: /* 4 */
1254 case BR_STATE_LISTENING
: /* 1 */
1255 port_state
[index
] |= (1 << bit
);
1257 case BR_STATE_LEARNING
: /* 2 */
1258 port_state
[index
] |= (2 << bit
);
1260 case BR_STATE_FORWARDING
: /* 3 */
1261 port_state
[index
] |= (3 << bit
);
1266 priv
->r
->stp_set(priv
, msti
, port_state
);
1268 mutex_unlock(&priv
->reg_mutex
);
1271 void rtl83xx_fast_age(struct dsa_switch
*ds
, int port
)
1273 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1274 int s
= priv
->family_id
== RTL8390_FAMILY_ID
? 2 : 0;
1276 pr_debug("FAST AGE port %d\n", port
);
1277 mutex_lock(&priv
->reg_mutex
);
1278 /* RTL838X_L2_TBL_FLUSH_CTRL register bits, 839x has 1 bit larger
1280 * 0-4: Replacing port
1281 * 5-9: Flushed/replaced port
1283 * 22: Entry types: 1: dynamic, 0: also static
1284 * 23: Match flush port
1286 * 25: Flush (0) or replace (1) L2 entries
1287 * 26: Status of action (1: Start, 0: Done)
1289 sw_w32(1 << (26 + s
) | 1 << (23 + s
) | port
<< (5 + (s
/ 2)), priv
->r
->l2_tbl_flush_ctrl
);
1291 do { } while (sw_r32(priv
->r
->l2_tbl_flush_ctrl
) & BIT(26 + s
));
1293 mutex_unlock(&priv
->reg_mutex
);
1296 void rtl931x_fast_age(struct dsa_switch
*ds
, int port
)
1298 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1300 pr_info("%s port %d\n", __func__
, port
);
1301 mutex_lock(&priv
->reg_mutex
);
1302 sw_w32(port
<< 11, RTL931X_L2_TBL_FLUSH_CTRL
+ 4);
1304 sw_w32(BIT(24) | BIT(28), RTL931X_L2_TBL_FLUSH_CTRL
);
1306 do { } while (sw_r32(RTL931X_L2_TBL_FLUSH_CTRL
) & BIT (28));
1308 mutex_unlock(&priv
->reg_mutex
);
1311 void rtl930x_fast_age(struct dsa_switch
*ds
, int port
)
1313 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1315 if (priv
->family_id
== RTL9310_FAMILY_ID
)
1316 return rtl931x_fast_age(ds
, port
);
1318 pr_debug("FAST AGE port %d\n", port
);
1319 mutex_lock(&priv
->reg_mutex
);
1320 sw_w32(port
<< 11, RTL930X_L2_TBL_FLUSH_CTRL
+ 4);
1322 sw_w32(BIT(26) | BIT(30), RTL930X_L2_TBL_FLUSH_CTRL
);
1324 do { } while (sw_r32(priv
->r
->l2_tbl_flush_ctrl
) & BIT(30));
1326 mutex_unlock(&priv
->reg_mutex
);
1329 static int rtl83xx_vlan_filtering(struct dsa_switch
*ds
, int port
,
1330 bool vlan_filtering
,
1331 struct netlink_ext_ack
*extack
)
1333 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1335 pr_debug("%s: port %d\n", __func__
, port
);
1336 mutex_lock(&priv
->reg_mutex
);
1338 if (vlan_filtering
) {
1339 /* Enable ingress and egress filtering
1340 * The VLAN_PORT_IGR_FILTER register uses 2 bits for each port to define
1341 * the filter action:
1344 * 2: Trap packet to CPU port
1345 * The Egress filter used 1 bit per state (0: DISABLED, 1: ENABLED)
1347 if (port
!= priv
->cpu_port
) {
1348 priv
->r
->set_vlan_igr_filter(port
, IGR_DROP
);
1349 priv
->r
->set_vlan_egr_filter(port
, EGR_ENABLE
);
1352 priv
->r
->set_vlan_igr_filter(port
, IGR_TRAP
);
1353 priv
->r
->set_vlan_egr_filter(port
, EGR_DISABLE
);
1357 /* Disable ingress and egress filtering */
1358 if (port
!= priv
->cpu_port
)
1359 priv
->r
->set_vlan_igr_filter(port
, IGR_FORWARD
);
1361 priv
->r
->set_vlan_egr_filter(port
, EGR_DISABLE
);
1364 /* Do we need to do something to the CPU-Port, too? */
1365 mutex_unlock(&priv
->reg_mutex
);
1370 static int rtl83xx_vlan_prepare(struct dsa_switch
*ds
, int port
,
1371 const struct switchdev_obj_port_vlan
*vlan
)
1373 struct rtl838x_vlan_info info
;
1374 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1376 priv
->r
->vlan_tables_read(0, &info
);
1378 pr_debug("VLAN 0: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
1379 info
.tagged_ports
, info
.untagged_ports
, info
.profile_id
,
1380 info
.hash_mc_fid
, info
.hash_uc_fid
, info
.fid
);
1382 priv
->r
->vlan_tables_read(1, &info
);
1383 pr_debug("VLAN 1: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
1384 info
.tagged_ports
, info
.untagged_ports
, info
.profile_id
,
1385 info
.hash_mc_fid
, info
.hash_uc_fid
, info
.fid
);
1386 priv
->r
->vlan_set_untagged(1, info
.untagged_ports
);
1387 pr_debug("SET: Untagged ports, VLAN %d: %llx\n", 1, info
.untagged_ports
);
1389 priv
->r
->vlan_set_tagged(1, &info
);
1390 pr_debug("SET: Tagged ports, VLAN %d: %llx\n", 1, info
.tagged_ports
);
1395 static void rtl83xx_vlan_set_pvid(struct rtl838x_switch_priv
*priv
,
1398 /* Set both inner and outer PVID of the port */
1399 priv
->r
->vlan_port_pvid_set(port
, PBVLAN_TYPE_INNER
, pvid
);
1400 priv
->r
->vlan_port_pvid_set(port
, PBVLAN_TYPE_OUTER
, pvid
);
1401 priv
->r
->vlan_port_pvidmode_set(port
, PBVLAN_TYPE_INNER
,
1402 PBVLAN_MODE_UNTAG_AND_PRITAG
);
1403 priv
->r
->vlan_port_pvidmode_set(port
, PBVLAN_TYPE_OUTER
,
1404 PBVLAN_MODE_UNTAG_AND_PRITAG
);
1406 priv
->ports
[port
].pvid
= pvid
;
1409 static int rtl83xx_vlan_add(struct dsa_switch
*ds
, int port
,
1410 const struct switchdev_obj_port_vlan
*vlan
,
1411 struct netlink_ext_ack
*extack
)
1413 struct rtl838x_vlan_info info
;
1414 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1417 pr_debug("%s port %d, vid %d, flags %x\n",
1418 __func__
, port
, vlan
->vid
, vlan
->flags
);
1420 if(!vlan
->vid
) return 0;
1422 if (vlan
->vid
> 4095) {
1423 dev_err(priv
->dev
, "VLAN out of range: %d", vlan
->vid
);
1427 err
= rtl83xx_vlan_prepare(ds
, port
, vlan
);
1431 mutex_lock(&priv
->reg_mutex
);
1433 if (vlan
->flags
& BRIDGE_VLAN_INFO_PVID
)
1434 rtl83xx_vlan_set_pvid(priv
, port
, vlan
->vid
);
1435 else if (priv
->ports
[port
].pvid
== vlan
->vid
)
1436 rtl83xx_vlan_set_pvid(priv
, port
, 0);
1438 /* Get port memberships of this vlan */
1439 priv
->r
->vlan_tables_read(vlan
->vid
, &info
);
1442 if (!info
.tagged_ports
) {
1444 info
.hash_mc_fid
= false;
1445 info
.hash_uc_fid
= false;
1446 info
.profile_id
= 0;
1449 /* sanitize untagged_ports - must be a subset */
1450 if (info
.untagged_ports
& ~info
.tagged_ports
)
1451 info
.untagged_ports
= 0;
1453 info
.tagged_ports
|= BIT_ULL(port
);
1454 if (vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
1455 info
.untagged_ports
|= BIT_ULL(port
);
1457 info
.untagged_ports
&= ~BIT_ULL(port
);
1459 priv
->r
->vlan_set_untagged(vlan
->vid
, info
.untagged_ports
);
1460 pr_debug("Untagged ports, VLAN %d: %llx\n", vlan
->vid
, info
.untagged_ports
);
1462 priv
->r
->vlan_set_tagged(vlan
->vid
, &info
);
1463 pr_debug("Tagged ports, VLAN %d: %llx\n", vlan
->vid
, info
.tagged_ports
);
1465 mutex_unlock(&priv
->reg_mutex
);
1470 static int rtl83xx_vlan_del(struct dsa_switch
*ds
, int port
,
1471 const struct switchdev_obj_port_vlan
*vlan
)
1473 struct rtl838x_vlan_info info
;
1474 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1477 pr_debug("%s: port %d, vid %d, flags %x\n",
1478 __func__
, port
, vlan
->vid
, vlan
->flags
);
1480 if (vlan
->vid
> 4095) {
1481 dev_err(priv
->dev
, "VLAN out of range: %d", vlan
->vid
);
1485 mutex_lock(&priv
->reg_mutex
);
1486 pvid
= priv
->ports
[port
].pvid
;
1488 /* Reset to default if removing the current PVID */
1489 if (vlan
->vid
== pvid
) {
1490 rtl83xx_vlan_set_pvid(priv
, port
, 0);
1492 /* Get port memberships of this vlan */
1493 priv
->r
->vlan_tables_read(vlan
->vid
, &info
);
1495 /* remove port from both tables */
1496 info
.untagged_ports
&= (~BIT_ULL(port
));
1497 info
.tagged_ports
&= (~BIT_ULL(port
));
1499 priv
->r
->vlan_set_untagged(vlan
->vid
, info
.untagged_ports
);
1500 pr_debug("Untagged ports, VLAN %d: %llx\n", vlan
->vid
, info
.untagged_ports
);
1502 priv
->r
->vlan_set_tagged(vlan
->vid
, &info
);
1503 pr_debug("Tagged ports, VLAN %d: %llx\n", vlan
->vid
, info
.tagged_ports
);
1505 mutex_unlock(&priv
->reg_mutex
);
1510 static void rtl83xx_setup_l2_uc_entry(struct rtl838x_l2_entry
*e
, int port
, int vid
, u64 mac
)
1512 memset(e
, 0, sizeof(*e
));
1514 e
->type
= L2_UNICAST
;
1518 e
->is_static
= true;
1522 e
->rvid
= e
->vid
= vid
;
1523 e
->is_ip_mc
= e
->is_ipv6_mc
= false;
1524 u64_to_ether_addr(mac
, e
->mac
);
1527 static void rtl83xx_setup_l2_mc_entry(struct rtl838x_l2_entry
*e
, int vid
, u64 mac
, int mc_group
)
1529 memset(e
, 0, sizeof(*e
));
1531 e
->type
= L2_MULTICAST
;
1534 e
->mc_portmask_index
= mc_group
;
1536 e
->rvid
= e
->vid
= vid
;
1537 e
->is_ip_mc
= e
->is_ipv6_mc
= false;
1538 u64_to_ether_addr(mac
, e
->mac
);
1541 /* Uses the seed to identify a hash bucket in the L2 using the derived hash key and then loops
1542 * over the entries in the bucket until either a matching entry is found or an empty slot
1543 * Returns the filled in rtl838x_l2_entry and the index in the bucket when an entry was found
1544 * when an empty slot was found and must exist is false, the index of the slot is returned
1545 * when no slots are available returns -1
1547 static int rtl83xx_find_l2_hash_entry(struct rtl838x_switch_priv
*priv
, u64 seed
,
1548 bool must_exist
, struct rtl838x_l2_entry
*e
)
1551 u32 key
= priv
->r
->l2_hash_key(priv
, seed
);
1554 pr_debug("%s: using key %x, for seed %016llx\n", __func__
, key
, seed
);
1555 /* Loop over all entries in the hash-bucket and over the second block on 93xx SoCs */
1556 for (int i
= 0; i
< priv
->l2_bucket_size
; i
++) {
1557 entry
= priv
->r
->read_l2_entry_using_hash(key
, i
, e
);
1558 pr_debug("valid %d, mac %016llx\n", e
->valid
, ether_addr_to_u64(&e
->mac
[0]));
1559 if (must_exist
&& !e
->valid
)
1561 if (!e
->valid
|| ((entry
& 0x0fffffffffffffffULL
) == seed
)) {
1562 idx
= i
> 3 ? ((key
>> 14) & 0xffff) | i
>> 1 : ((key
<< 2) | i
) & 0xffff;
1570 /* Uses the seed to identify an entry in the CAM by looping over all its entries
1571 * Returns the filled in rtl838x_l2_entry and the index in the CAM when an entry was found
1572 * when an empty slot was found the index of the slot is returned
1573 * when no slots are available returns -1
1575 static int rtl83xx_find_l2_cam_entry(struct rtl838x_switch_priv
*priv
, u64 seed
,
1576 bool must_exist
, struct rtl838x_l2_entry
*e
)
1581 for (int i
= 0; i
< 64; i
++) {
1582 entry
= priv
->r
->read_cam(i
, e
);
1583 if (!must_exist
&& !e
->valid
) {
1584 if (idx
< 0) /* First empty entry? */
1587 } else if ((entry
& 0x0fffffffffffffffULL
) == seed
) {
1588 pr_debug("Found entry in CAM\n");
1597 static int rtl83xx_port_fdb_add(struct dsa_switch
*ds
, int port
,
1598 const unsigned char *addr
, u16 vid
)
1600 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1601 u64 mac
= ether_addr_to_u64(addr
);
1602 struct rtl838x_l2_entry e
;
1604 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1606 if (priv
->is_lagmember
[port
]) {
1607 pr_debug("%s: %d is lag slave. ignore\n", __func__
, port
);
1611 mutex_lock(&priv
->reg_mutex
);
1613 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, false, &e
);
1615 /* Found an existing or empty entry */
1617 rtl83xx_setup_l2_uc_entry(&e
, port
, vid
, mac
);
1618 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1622 /* Hash buckets full, try CAM */
1623 idx
= rtl83xx_find_l2_cam_entry(priv
, seed
, false, &e
);
1626 rtl83xx_setup_l2_uc_entry(&e
, port
, vid
, mac
);
1627 priv
->r
->write_cam(idx
, &e
);
1634 mutex_unlock(&priv
->reg_mutex
);
1639 static int rtl83xx_port_fdb_del(struct dsa_switch
*ds
, int port
,
1640 const unsigned char *addr
, u16 vid
)
1642 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1643 u64 mac
= ether_addr_to_u64(addr
);
1644 struct rtl838x_l2_entry e
;
1646 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1648 pr_debug("In %s, mac %llx, vid: %d\n", __func__
, mac
, vid
);
1649 mutex_lock(&priv
->reg_mutex
);
1651 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, true, &e
);
1654 pr_debug("Found entry index %d, key %d and bucket %d\n", idx
, idx
>> 2, idx
& 3);
1656 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1660 /* Check CAM for spillover from hash buckets */
1661 idx
= rtl83xx_find_l2_cam_entry(priv
, seed
, true, &e
);
1665 priv
->r
->write_cam(idx
, &e
);
1671 mutex_unlock(&priv
->reg_mutex
);
1676 static int rtl83xx_port_fdb_dump(struct dsa_switch
*ds
, int port
,
1677 dsa_fdb_dump_cb_t
*cb
, void *data
)
1679 struct rtl838x_l2_entry e
;
1680 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1682 mutex_lock(&priv
->reg_mutex
);
1684 for (int i
= 0; i
< priv
->fib_entries
; i
++) {
1685 priv
->r
->read_l2_entry_using_hash(i
>> 2, i
& 0x3, &e
);
1690 if (e
.port
== port
|| e
.port
== RTL930X_PORT_IGNORE
)
1691 cb(e
.mac
, e
.vid
, e
.is_static
, data
);
1693 if (!((i
+ 1) % 64))
1697 for (int i
= 0; i
< 64; i
++) {
1698 priv
->r
->read_cam(i
, &e
);
1704 cb(e
.mac
, e
.vid
, e
.is_static
, data
);
1707 mutex_unlock(&priv
->reg_mutex
);
1712 static int rtl83xx_port_mdb_add(struct dsa_switch
*ds
, int port
,
1713 const struct switchdev_obj_port_mdb
*mdb
)
1715 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1716 u64 mac
= ether_addr_to_u64(mdb
->addr
);
1717 struct rtl838x_l2_entry e
;
1720 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1723 if (priv
->id
>= 0x9300)
1726 pr_debug("In %s port %d, mac %llx, vid: %d\n", __func__
, port
, mac
, vid
);
1728 if (priv
->is_lagmember
[port
]) {
1729 pr_debug("%s: %d is lag slave. ignore\n", __func__
, port
);
1733 mutex_lock(&priv
->reg_mutex
);
1735 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, false, &e
);
1737 /* Found an existing or empty entry */
1740 pr_debug("Found an existing entry %016llx, mc_group %d\n",
1741 ether_addr_to_u64(e
.mac
), e
.mc_portmask_index
);
1742 rtl83xx_mc_group_add_port(priv
, e
.mc_portmask_index
, port
);
1744 pr_debug("New entry for seed %016llx\n", seed
);
1745 mc_group
= rtl83xx_mc_group_alloc(priv
, port
);
1750 rtl83xx_setup_l2_mc_entry(&e
, vid
, mac
, mc_group
);
1751 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1756 /* Hash buckets full, try CAM */
1757 idx
= rtl83xx_find_l2_cam_entry(priv
, seed
, false, &e
);
1761 pr_debug("Found existing CAM entry %016llx, mc_group %d\n",
1762 ether_addr_to_u64(e
.mac
), e
.mc_portmask_index
);
1763 rtl83xx_mc_group_add_port(priv
, e
.mc_portmask_index
, port
);
1765 pr_debug("New entry\n");
1766 mc_group
= rtl83xx_mc_group_alloc(priv
, port
);
1771 rtl83xx_setup_l2_mc_entry(&e
, vid
, mac
, mc_group
);
1772 priv
->r
->write_cam(idx
, &e
);
1780 mutex_unlock(&priv
->reg_mutex
);
1782 dev_err(ds
->dev
, "failed to add MDB entry\n");
1787 int rtl83xx_port_mdb_del(struct dsa_switch
*ds
, int port
,
1788 const struct switchdev_obj_port_mdb
*mdb
)
1790 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1791 u64 mac
= ether_addr_to_u64(mdb
->addr
);
1792 struct rtl838x_l2_entry e
;
1795 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1798 pr_debug("In %s, port %d, mac %llx, vid: %d\n", __func__
, port
, mac
, vid
);
1800 if (priv
->is_lagmember
[port
]) {
1801 pr_info("%s: %d is lag slave. ignore\n", __func__
, port
);
1805 mutex_lock(&priv
->reg_mutex
);
1807 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, true, &e
);
1810 pr_debug("Found entry index %d, key %d and bucket %d\n", idx
, idx
>> 2, idx
& 3);
1811 portmask
= rtl83xx_mc_group_del_port(priv
, e
.mc_portmask_index
, port
);
1814 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1819 /* Check CAM for spillover from hash buckets */
1820 idx
= rtl83xx_find_l2_cam_entry(priv
, seed
, true, &e
);
1823 portmask
= rtl83xx_mc_group_del_port(priv
, e
.mc_portmask_index
, port
);
1826 priv
->r
->write_cam(idx
, &e
);
1830 /* TODO: Re-enable with a newer kernel: err = -ENOENT; */
1833 mutex_unlock(&priv
->reg_mutex
);
1838 static int rtl83xx_port_mirror_add(struct dsa_switch
*ds
, int port
,
1839 struct dsa_mall_mirror_tc_entry
*mirror
,
1842 /* We support 4 mirror groups, one destination port per group */
1844 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1845 int ctrl_reg
, dpm_reg
, spm_reg
;
1847 pr_debug("In %s\n", __func__
);
1849 for (group
= 0; group
< 4; group
++) {
1850 if (priv
->mirror_group_ports
[group
] == mirror
->to_local_port
)
1854 for (group
= 0; group
< 4; group
++) {
1855 if (priv
->mirror_group_ports
[group
] < 0)
1863 ctrl_reg
= priv
->r
->mir_ctrl
+ group
* 4;
1864 dpm_reg
= priv
->r
->mir_dpm
+ group
* 4 * priv
->port_width
;
1865 spm_reg
= priv
->r
->mir_spm
+ group
* 4 * priv
->port_width
;
1867 pr_debug("Using group %d\n", group
);
1868 mutex_lock(&priv
->reg_mutex
);
1870 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
1871 /* Enable mirroring to port across VLANs (bit 11) */
1872 sw_w32(1 << 11 | (mirror
->to_local_port
<< 4) | 1, ctrl_reg
);
1874 /* Enable mirroring to destination port */
1875 sw_w32((mirror
->to_local_port
<< 4) | 1, ctrl_reg
);
1878 if (ingress
&& (priv
->r
->get_port_reg_be(spm_reg
) & (1ULL << port
))) {
1879 mutex_unlock(&priv
->reg_mutex
);
1882 if ((!ingress
) && (priv
->r
->get_port_reg_be(dpm_reg
) & (1ULL << port
))) {
1883 mutex_unlock(&priv
->reg_mutex
);
1888 priv
->r
->mask_port_reg_be(0, 1ULL << port
, spm_reg
);
1890 priv
->r
->mask_port_reg_be(0, 1ULL << port
, dpm_reg
);
1892 priv
->mirror_group_ports
[group
] = mirror
->to_local_port
;
1893 mutex_unlock(&priv
->reg_mutex
);
1898 static void rtl83xx_port_mirror_del(struct dsa_switch
*ds
, int port
,
1899 struct dsa_mall_mirror_tc_entry
*mirror
)
1902 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1903 int ctrl_reg
, dpm_reg
, spm_reg
;
1905 pr_debug("In %s\n", __func__
);
1906 for (group
= 0; group
< 4; group
++) {
1907 if (priv
->mirror_group_ports
[group
] == mirror
->to_local_port
)
1913 ctrl_reg
= priv
->r
->mir_ctrl
+ group
* 4;
1914 dpm_reg
= priv
->r
->mir_dpm
+ group
* 4 * priv
->port_width
;
1915 spm_reg
= priv
->r
->mir_spm
+ group
* 4 * priv
->port_width
;
1917 mutex_lock(&priv
->reg_mutex
);
1918 if (mirror
->ingress
) {
1919 /* Ingress, clear source port matrix */
1920 priv
->r
->mask_port_reg_be(1ULL << port
, 0, spm_reg
);
1922 /* Egress, clear destination port matrix */
1923 priv
->r
->mask_port_reg_be(1ULL << port
, 0, dpm_reg
);
1926 if (!(sw_r32(spm_reg
) || sw_r32(dpm_reg
))) {
1927 priv
->mirror_group_ports
[group
] = -1;
1928 sw_w32(0, ctrl_reg
);
1931 mutex_unlock(&priv
->reg_mutex
);
1934 static int rtl83xx_port_pre_bridge_flags(struct dsa_switch
*ds
, int port
, struct switchdev_brport_flags flags
, struct netlink_ext_ack
*extack
)
1936 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1937 unsigned long features
= 0;
1938 pr_debug("%s: %d %lX\n", __func__
, port
, flags
.val
);
1939 if (priv
->r
->enable_learning
)
1940 features
|= BR_LEARNING
;
1941 if (priv
->r
->enable_flood
)
1942 features
|= BR_FLOOD
;
1943 if (priv
->r
->enable_mcast_flood
)
1944 features
|= BR_MCAST_FLOOD
;
1945 if (priv
->r
->enable_bcast_flood
)
1946 features
|= BR_BCAST_FLOOD
;
1947 if (flags
.mask
& ~(features
))
1953 static int rtl83xx_port_bridge_flags(struct dsa_switch
*ds
, int port
, struct switchdev_brport_flags flags
, struct netlink_ext_ack
*extack
)
1955 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1957 pr_debug("%s: %d %lX\n", __func__
, port
, flags
.val
);
1958 if (priv
->r
->enable_learning
&& (flags
.mask
& BR_LEARNING
))
1959 priv
->r
->enable_learning(port
, !!(flags
.val
& BR_LEARNING
));
1961 if (priv
->r
->enable_flood
&& (flags
.mask
& BR_FLOOD
))
1962 priv
->r
->enable_flood(port
, !!(flags
.val
& BR_FLOOD
));
1964 if (priv
->r
->enable_mcast_flood
&& (flags
.mask
& BR_MCAST_FLOOD
))
1965 priv
->r
->enable_mcast_flood(port
, !!(flags
.val
& BR_MCAST_FLOOD
));
1967 if (priv
->r
->enable_bcast_flood
&& (flags
.mask
& BR_BCAST_FLOOD
))
1968 priv
->r
->enable_bcast_flood(port
, !!(flags
.val
& BR_BCAST_FLOOD
));
1973 static bool rtl83xx_lag_can_offload(struct dsa_switch
*ds
,
1974 struct net_device
*lag
,
1975 struct netdev_lag_upper_info
*info
)
1979 id
= dsa_lag_id(ds
->dst
, lag
);
1980 if (id
< 0 || id
>= ds
->num_lag_ids
)
1983 if (info
->tx_type
!= NETDEV_LAG_TX_TYPE_HASH
) {
1986 if (info
->hash_type
!= NETDEV_LAG_HASH_L2
&& info
->hash_type
!= NETDEV_LAG_HASH_L23
)
1992 static int rtl83xx_port_lag_change(struct dsa_switch
*ds
, int port
)
1994 pr_debug("%s: %d\n", __func__
, port
);
1995 /* Nothing to be done... */
2000 static int rtl83xx_port_lag_join(struct dsa_switch
*ds
, int port
,
2001 struct net_device
*lag
,
2002 struct netdev_lag_upper_info
*info
)
2004 struct rtl838x_switch_priv
*priv
= ds
->priv
;
2007 if (!rtl83xx_lag_can_offload(ds
, lag
, info
))
2010 mutex_lock(&priv
->reg_mutex
);
2012 for (i
= 0; i
< priv
->n_lags
; i
++) {
2013 if ((!priv
->lag_devs
[i
]) || (priv
->lag_devs
[i
] == lag
))
2016 if (port
>= priv
->cpu_port
) {
2020 pr_info("port_lag_join: group %d, port %d\n",i
, port
);
2021 if (!priv
->lag_devs
[i
])
2022 priv
->lag_devs
[i
] = lag
;
2024 if (priv
->lag_primary
[i
] == -1) {
2025 priv
->lag_primary
[i
] = port
;
2027 priv
->is_lagmember
[port
] = 1;
2029 priv
->lagmembers
|= (1ULL << port
);
2031 pr_debug("lag_members = %llX\n", priv
->lagmembers
);
2032 err
= rtl83xx_lag_add(priv
->ds
, i
, port
, info
);
2039 mutex_unlock(&priv
->reg_mutex
);
2044 static int rtl83xx_port_lag_leave(struct dsa_switch
*ds
, int port
,
2045 struct net_device
*lag
)
2047 int i
, group
= -1, err
;
2048 struct rtl838x_switch_priv
*priv
= ds
->priv
;
2050 mutex_lock(&priv
->reg_mutex
);
2051 for (i
= 0; i
< priv
->n_lags
; i
++) {
2052 if (priv
->lags_port_members
[i
] & BIT_ULL(port
)) {
2059 pr_info("port_lag_leave: port %d is not a member\n", port
);
2064 if (port
>= priv
->cpu_port
) {
2068 pr_info("port_lag_del: group %d, port %d\n",group
, port
);
2069 priv
->lagmembers
&=~ (1ULL << port
);
2070 priv
->lag_primary
[i
] = -1;
2071 priv
->is_lagmember
[port
] = 0;
2072 pr_debug("lag_members = %llX\n", priv
->lagmembers
);
2073 err
= rtl83xx_lag_del(priv
->ds
, group
, port
);
2078 if (!priv
->lags_port_members
[i
])
2079 priv
->lag_devs
[i
] = NULL
;
2082 mutex_unlock(&priv
->reg_mutex
);
2086 int dsa_phy_read(struct dsa_switch
*ds
, int phy_addr
, int phy_reg
)
2090 struct rtl838x_switch_priv
*priv
= ds
->priv
;
2092 if ((phy_addr
>= 24) &&
2094 (priv
->ports
[24].phy
== PHY_RTL838X_SDS
)) {
2097 val
= sw_r32(RTL838X_SDS4_FIB_REG0
+ offset
+ (phy_reg
<< 2)) & 0xffff;
2101 read_phy(phy_addr
, 0, phy_reg
, &val
);
2105 int dsa_phy_write(struct dsa_switch
*ds
, int phy_addr
, int phy_reg
, u16 val
)
2108 struct rtl838x_switch_priv
*priv
= ds
->priv
;
2110 if ((phy_addr
>= 24) &&
2112 (priv
->ports
[24].phy
== PHY_RTL838X_SDS
)) {
2115 sw_w32(val
, RTL838X_SDS4_FIB_REG0
+ offset
+ (phy_reg
<< 2));
2118 return write_phy(phy_addr
, 0, phy_reg
, val
);
2121 const struct dsa_switch_ops rtl83xx_switch_ops
= {
2122 .get_tag_protocol
= rtl83xx_get_tag_protocol
,
2123 .setup
= rtl83xx_setup
,
2125 .phy_read
= dsa_phy_read
,
2126 .phy_write
= dsa_phy_write
,
2128 .phylink_validate
= rtl83xx_phylink_validate
,
2129 .phylink_mac_link_state
= rtl83xx_phylink_mac_link_state
,
2130 .phylink_mac_config
= rtl83xx_phylink_mac_config
,
2131 .phylink_mac_link_down
= rtl83xx_phylink_mac_link_down
,
2132 .phylink_mac_link_up
= rtl83xx_phylink_mac_link_up
,
2134 .get_strings
= rtl83xx_get_strings
,
2135 .get_ethtool_stats
= rtl83xx_get_ethtool_stats
,
2136 .get_sset_count
= rtl83xx_get_sset_count
,
2138 .port_enable
= rtl83xx_port_enable
,
2139 .port_disable
= rtl83xx_port_disable
,
2141 .get_mac_eee
= rtl83xx_get_mac_eee
,
2142 .set_mac_eee
= rtl83xx_set_mac_eee
,
2144 .set_ageing_time
= rtl83xx_set_ageing_time
,
2145 .port_bridge_join
= rtl83xx_port_bridge_join
,
2146 .port_bridge_leave
= rtl83xx_port_bridge_leave
,
2147 .port_stp_state_set
= rtl83xx_port_stp_state_set
,
2148 .port_fast_age
= rtl83xx_fast_age
,
2150 .port_vlan_filtering
= rtl83xx_vlan_filtering
,
2151 .port_vlan_add
= rtl83xx_vlan_add
,
2152 .port_vlan_del
= rtl83xx_vlan_del
,
2154 .port_fdb_add
= rtl83xx_port_fdb_add
,
2155 .port_fdb_del
= rtl83xx_port_fdb_del
,
2156 .port_fdb_dump
= rtl83xx_port_fdb_dump
,
2158 .port_mdb_add
= rtl83xx_port_mdb_add
,
2159 .port_mdb_del
= rtl83xx_port_mdb_del
,
2161 .port_mirror_add
= rtl83xx_port_mirror_add
,
2162 .port_mirror_del
= rtl83xx_port_mirror_del
,
2164 .port_lag_change
= rtl83xx_port_lag_change
,
2165 .port_lag_join
= rtl83xx_port_lag_join
,
2166 .port_lag_leave
= rtl83xx_port_lag_leave
,
2168 .port_pre_bridge_flags
= rtl83xx_port_pre_bridge_flags
,
2169 .port_bridge_flags
= rtl83xx_port_bridge_flags
,
2172 const struct dsa_switch_ops rtl930x_switch_ops
= {
2173 .get_tag_protocol
= rtl83xx_get_tag_protocol
,
2174 .setup
= rtl93xx_setup
,
2176 .phy_read
= dsa_phy_read
,
2177 .phy_write
= dsa_phy_write
,
2179 .phylink_validate
= rtl93xx_phylink_validate
,
2180 .phylink_mac_link_state
= rtl93xx_phylink_mac_link_state
,
2181 .phylink_mac_config
= rtl93xx_phylink_mac_config
,
2182 .phylink_mac_link_down
= rtl93xx_phylink_mac_link_down
,
2183 .phylink_mac_link_up
= rtl93xx_phylink_mac_link_up
,
2185 .get_strings
= rtl83xx_get_strings
,
2186 .get_ethtool_stats
= rtl83xx_get_ethtool_stats
,
2187 .get_sset_count
= rtl83xx_get_sset_count
,
2189 .port_enable
= rtl83xx_port_enable
,
2190 .port_disable
= rtl83xx_port_disable
,
2192 .get_mac_eee
= rtl93xx_get_mac_eee
,
2193 .set_mac_eee
= rtl83xx_set_mac_eee
,
2195 .set_ageing_time
= rtl83xx_set_ageing_time
,
2196 .port_bridge_join
= rtl83xx_port_bridge_join
,
2197 .port_bridge_leave
= rtl83xx_port_bridge_leave
,
2198 .port_stp_state_set
= rtl83xx_port_stp_state_set
,
2199 .port_fast_age
= rtl930x_fast_age
,
2201 .port_vlan_filtering
= rtl83xx_vlan_filtering
,
2202 .port_vlan_add
= rtl83xx_vlan_add
,
2203 .port_vlan_del
= rtl83xx_vlan_del
,
2205 .port_fdb_add
= rtl83xx_port_fdb_add
,
2206 .port_fdb_del
= rtl83xx_port_fdb_del
,
2207 .port_fdb_dump
= rtl83xx_port_fdb_dump
,
2209 .port_mdb_add
= rtl83xx_port_mdb_add
,
2210 .port_mdb_del
= rtl83xx_port_mdb_del
,
2212 .port_lag_change
= rtl83xx_port_lag_change
,
2213 .port_lag_join
= rtl83xx_port_lag_join
,
2214 .port_lag_leave
= rtl83xx_port_lag_leave
,
2216 .port_pre_bridge_flags
= rtl83xx_port_pre_bridge_flags
,
2217 .port_bridge_flags
= rtl83xx_port_bridge_flags
,