1 // SPDX-License-Identifier: GPL-2.0-only
4 #include <linux/if_bridge.h>
6 #include <asm/mach-rtl838x/mach-rtl83xx.h>
10 extern struct rtl83xx_soc_info soc_info
;
13 static void rtl83xx_print_matrix(void)
15 unsigned volatile int *ptr8
;
19 if (soc_info
.family
== RTL8380_FAMILY_ID
) {
20 ptr8
= RTL838X_SW_BASE
+ RTL838X_PORT_ISO_CTRL(0);
21 for (i
= 0; i
< 28; i
+= 8)
22 pr_debug("> %8x %8x %8x %8x %8x %8x %8x %8x\n",
23 ptr8
[i
+ 0], ptr8
[i
+ 1], ptr8
[i
+ 2], ptr8
[i
+ 3],
24 ptr8
[i
+ 4], ptr8
[i
+ 5], ptr8
[i
+ 6], ptr8
[i
+ 7]);
25 pr_debug("CPU_PORT> %8x\n", ptr8
[28]);
27 ptr9
= RTL838X_SW_BASE
+ RTL839X_PORT_ISO_CTRL(0);
28 for (i
= 0; i
< 52; i
+= 4)
29 pr_debug("> %16llx %16llx %16llx %16llx\n",
30 ptr9
[i
+ 0], ptr9
[i
+ 1], ptr9
[i
+ 2], ptr9
[i
+ 3]);
31 pr_debug("CPU_PORT> %16llx\n", ptr9
[52]);
36 static void rtl83xx_init_stats(struct rtl838x_switch_priv
*priv
)
38 mutex_lock(&priv
->reg_mutex
);
40 /* Enable statistics module: all counters plus debug.
41 * On RTL839x all counters are enabled by default
43 if (priv
->family_id
== RTL8380_FAMILY_ID
)
44 sw_w32_mask(0, 3, RTL838X_STAT_CTRL
);
46 /* Reset statistics counters */
47 sw_w32_mask(0, 1, priv
->r
->stat_rst
);
49 mutex_unlock(&priv
->reg_mutex
);
52 static void rtl83xx_write_cam(int idx
, u32
*r
)
54 u32 cmd
= BIT(16) /* Execute cmd */
56 | BIT(13) /* Table type 0b01 */
59 sw_w32(r
[0], RTL838X_TBL_ACCESS_L2_DATA(0));
60 sw_w32(r
[1], RTL838X_TBL_ACCESS_L2_DATA(1));
61 sw_w32(r
[2], RTL838X_TBL_ACCESS_L2_DATA(2));
63 sw_w32(cmd
, RTL838X_TBL_ACCESS_L2_CTRL
);
64 do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL
) & BIT(16));
67 static u64
rtl83xx_hash_key(struct rtl838x_switch_priv
*priv
, u64 mac
, u32 vid
)
69 if (priv
->family_id
== RTL8380_FAMILY_ID
)
70 return rtl838x_hash(priv
, mac
<< 12 | vid
);
72 return rtl839x_hash(priv
, mac
<< 12 | vid
);
75 static void rtl83xx_write_hash(int idx
, u32
*r
)
77 u32 cmd
= BIT(16) /* Execute cmd */
79 | 0 << 13 /* Table type 0b00 */
82 sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(0));
83 sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(1));
84 sw_w32(0, RTL838X_TBL_ACCESS_L2_DATA(2));
85 sw_w32(cmd
, RTL838X_TBL_ACCESS_L2_CTRL
);
86 do { } while (sw_r32(RTL838X_TBL_ACCESS_L2_CTRL
) & BIT(16));
89 static void rtl83xx_enable_phy_polling(struct rtl838x_switch_priv
*priv
)
95 /* Enable all ports with a PHY, including the SFP-ports */
96 for (i
= 0; i
< priv
->cpu_port
; i
++) {
97 if (priv
->ports
[i
].phy
)
101 pr_debug("%s: %16llx\n", __func__
, v
);
102 priv
->r
->set_port_reg_le(v
, priv
->r
->smi_poll_ctrl
);
104 /* PHY update complete */
105 if (priv
->family_id
== RTL8390_FAMILY_ID
)
106 sw_w32_mask(0, BIT(7), RTL839X_SMI_GLB_CTRL
);
108 sw_w32_mask(0, 0x8000, RTL838X_SMI_GLB_CTRL
);
111 const struct rtl83xx_mib_desc rtl83xx_mib
[] = {
112 MIB_DESC(2, 0xf8, "ifInOctets"),
113 MIB_DESC(2, 0xf0, "ifOutOctets"),
114 MIB_DESC(1, 0xec, "dot1dTpPortInDiscards"),
115 MIB_DESC(1, 0xe8, "ifInUcastPkts"),
116 MIB_DESC(1, 0xe4, "ifInMulticastPkts"),
117 MIB_DESC(1, 0xe0, "ifInBroadcastPkts"),
118 MIB_DESC(1, 0xdc, "ifOutUcastPkts"),
119 MIB_DESC(1, 0xd8, "ifOutMulticastPkts"),
120 MIB_DESC(1, 0xd4, "ifOutBroadcastPkts"),
121 MIB_DESC(1, 0xd0, "ifOutDiscards"),
122 MIB_DESC(1, 0xcc, ".3SingleCollisionFrames"),
123 MIB_DESC(1, 0xc8, ".3MultipleCollisionFrames"),
124 MIB_DESC(1, 0xc4, ".3DeferredTransmissions"),
125 MIB_DESC(1, 0xc0, ".3LateCollisions"),
126 MIB_DESC(1, 0xbc, ".3ExcessiveCollisions"),
127 MIB_DESC(1, 0xb8, ".3SymbolErrors"),
128 MIB_DESC(1, 0xb4, ".3ControlInUnknownOpcodes"),
129 MIB_DESC(1, 0xb0, ".3InPauseFrames"),
130 MIB_DESC(1, 0xac, ".3OutPauseFrames"),
131 MIB_DESC(1, 0xa8, "DropEvents"),
132 MIB_DESC(1, 0xa4, "tx_BroadcastPkts"),
133 MIB_DESC(1, 0xa0, "tx_MulticastPkts"),
134 MIB_DESC(1, 0x9c, "CRCAlignErrors"),
135 MIB_DESC(1, 0x98, "tx_UndersizePkts"),
136 MIB_DESC(1, 0x94, "rx_UndersizePkts"),
137 MIB_DESC(1, 0x90, "rx_UndersizedropPkts"),
138 MIB_DESC(1, 0x8c, "tx_OversizePkts"),
139 MIB_DESC(1, 0x88, "rx_OversizePkts"),
140 MIB_DESC(1, 0x84, "Fragments"),
141 MIB_DESC(1, 0x80, "Jabbers"),
142 MIB_DESC(1, 0x7c, "Collisions"),
143 MIB_DESC(1, 0x78, "tx_Pkts64Octets"),
144 MIB_DESC(1, 0x74, "rx_Pkts64Octets"),
145 MIB_DESC(1, 0x70, "tx_Pkts65to127Octets"),
146 MIB_DESC(1, 0x6c, "rx_Pkts65to127Octets"),
147 MIB_DESC(1, 0x68, "tx_Pkts128to255Octets"),
148 MIB_DESC(1, 0x64, "rx_Pkts128to255Octets"),
149 MIB_DESC(1, 0x60, "tx_Pkts256to511Octets"),
150 MIB_DESC(1, 0x5c, "rx_Pkts256to511Octets"),
151 MIB_DESC(1, 0x58, "tx_Pkts512to1023Octets"),
152 MIB_DESC(1, 0x54, "rx_Pkts512to1023Octets"),
153 MIB_DESC(1, 0x50, "tx_Pkts1024to1518Octets"),
154 MIB_DESC(1, 0x4c, "rx_StatsPkts1024to1518Octets"),
155 MIB_DESC(1, 0x48, "tx_Pkts1519toMaxOctets"),
156 MIB_DESC(1, 0x44, "rx_Pkts1519toMaxOctets"),
157 MIB_DESC(1, 0x40, "rxMacDiscards")
164 static enum dsa_tag_protocol
rtl83xx_get_tag_protocol(struct dsa_switch
*ds
, int port
)
166 /* The switch does not tag the frames, instead internally the header
167 * structure for each packet is tagged accordingly.
169 return DSA_TAG_PROTO_TRAILER
;
172 static int rtl83xx_setup(struct dsa_switch
*ds
)
175 struct rtl838x_switch_priv
*priv
= ds
->priv
;
176 u64 port_bitmap
= BIT_ULL(priv
->cpu_port
);
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 (i
= 0; i
< ds
->num_ports
; i
++)
184 priv
->ports
[i
].enable
= false;
185 priv
->ports
[priv
->cpu_port
].enable
= true;
187 /* Isolate ports from each other: traffic only CPU <-> port */
188 /* Setting bit j in register RTL838X_PORT_ISO_CTRL(i) allows
189 * traffic from source port i to destination port j
191 for (i
= 0; i
< priv
->cpu_port
; i
++) {
192 if (priv
->ports
[i
].phy
) {
193 priv
->r
->set_port_reg_be(BIT_ULL(priv
->cpu_port
) | BIT(i
),
194 priv
->r
->port_iso_ctrl(i
));
195 port_bitmap
|= BIT_ULL(i
);
198 priv
->r
->set_port_reg_be(port_bitmap
, priv
->r
->port_iso_ctrl(priv
->cpu_port
));
200 rtl83xx_print_matrix();
202 rtl83xx_init_stats(priv
);
204 ds
->configure_vlan_while_not_filtering
= true;
206 /* Enable MAC Polling PHY again */
207 rtl83xx_enable_phy_polling(priv
);
208 pr_debug("Please wait until PHY is settled\n");
213 static void rtl83xx_phylink_validate(struct dsa_switch
*ds
, int port
,
214 unsigned long *supported
,
215 struct phylink_link_state
*state
)
217 struct rtl838x_switch_priv
*priv
= ds
->priv
;
218 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
220 pr_debug("In %s port %d", __func__
, port
);
222 if (!phy_interface_mode_is_rgmii(state
->interface
) &&
223 state
->interface
!= PHY_INTERFACE_MODE_1000BASEX
&&
224 state
->interface
!= PHY_INTERFACE_MODE_MII
&&
225 state
->interface
!= PHY_INTERFACE_MODE_REVMII
&&
226 state
->interface
!= PHY_INTERFACE_MODE_GMII
&&
227 state
->interface
!= PHY_INTERFACE_MODE_QSGMII
&&
228 state
->interface
!= PHY_INTERFACE_MODE_INTERNAL
&&
229 state
->interface
!= PHY_INTERFACE_MODE_SGMII
) {
230 bitmap_zero(supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
232 "Unsupported interface: %d for port %d\n",
233 state
->interface
, port
);
237 /* Allow all the expected bits */
238 phylink_set(mask
, Autoneg
);
239 phylink_set_port_modes(mask
);
240 phylink_set(mask
, Pause
);
241 phylink_set(mask
, Asym_Pause
);
243 /* With the exclusion of MII and Reverse MII, we support Gigabit,
244 * including Half duplex
246 if (state
->interface
!= PHY_INTERFACE_MODE_MII
&&
247 state
->interface
!= PHY_INTERFACE_MODE_REVMII
) {
248 phylink_set(mask
, 1000baseT_Full
);
249 phylink_set(mask
, 1000baseT_Half
);
252 /* On both the 8380 and 8382, ports 24-27 are SFP ports */
253 if (port
>= 24 && port
<= 27 && priv
->family_id
== RTL8380_FAMILY_ID
)
254 phylink_set(mask
, 1000baseX_Full
);
256 phylink_set(mask
, 10baseT_Half
);
257 phylink_set(mask
, 10baseT_Full
);
258 phylink_set(mask
, 100baseT_Half
);
259 phylink_set(mask
, 100baseT_Full
);
261 bitmap_and(supported
, supported
, mask
,
262 __ETHTOOL_LINK_MODE_MASK_NBITS
);
263 bitmap_and(state
->advertising
, state
->advertising
, mask
,
264 __ETHTOOL_LINK_MODE_MASK_NBITS
);
267 static int rtl83xx_phylink_mac_link_state(struct dsa_switch
*ds
, int port
,
268 struct phylink_link_state
*state
)
270 struct rtl838x_switch_priv
*priv
= ds
->priv
;
273 if (port
< 0 || port
> priv
->cpu_port
)
277 if (priv
->r
->get_port_reg_le(priv
->r
->mac_link_sts
) & BIT_ULL(port
))
280 if (priv
->r
->get_port_reg_le(priv
->r
->mac_link_dup_sts
) & BIT_ULL(port
))
283 speed
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_spd_sts(port
));
284 speed
>>= (port
% 16) << 1;
285 switch (speed
& 0x3) {
287 state
->speed
= SPEED_10
;
290 state
->speed
= SPEED_100
;
293 state
->speed
= SPEED_1000
;
296 if (port
== 24 || port
== 26) /* Internal serdes */
297 state
->speed
= SPEED_2500
;
299 state
->speed
= SPEED_100
; /* Is in fact 500Mbit */
302 state
->pause
&= (MLO_PAUSE_RX
| MLO_PAUSE_TX
);
303 if (priv
->r
->get_port_reg_le(priv
->r
->mac_rx_pause_sts
) & BIT_ULL(port
))
304 state
->pause
|= MLO_PAUSE_RX
;
305 if (priv
->r
->get_port_reg_le(priv
->r
->mac_tx_pause_sts
) & BIT_ULL(port
))
306 state
->pause
|= MLO_PAUSE_TX
;
310 static void rtl83xx_phylink_mac_config(struct dsa_switch
*ds
, int port
,
312 const struct phylink_link_state
*state
)
314 struct rtl838x_switch_priv
*priv
= ds
->priv
;
316 int speed_bit
= priv
->family_id
== RTL8380_FAMILY_ID
? 4 : 3;
318 pr_debug("%s port %d, mode %x\n", __func__
, port
, mode
);
320 if (port
== priv
->cpu_port
) {
321 /* Set Speed, duplex, flow control
322 * FORCE_EN | LINK_EN | NWAY_EN | DUP_SEL
323 * | SPD_SEL = 0b10 | FORCE_FC_EN | PHY_MASTER_SLV_MANUAL_EN
326 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
327 sw_w32(0x6192F, priv
->r
->mac_force_mode_ctrl(priv
->cpu_port
));
328 /* allow CRC errors on CPU-port */
329 sw_w32_mask(0, 0x8, RTL838X_MAC_PORT_CTRL(priv
->cpu_port
));
331 sw_w32_mask(0, 3, priv
->r
->mac_force_mode_ctrl(priv
->cpu_port
));
336 reg
= sw_r32(priv
->r
->mac_force_mode_ctrl(port
));
337 /* Auto-Negotiation does not work for MAC in RTL8390 */
338 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
339 if (mode
== MLO_AN_PHY
) {
340 pr_debug("PHY autonegotiates\n");
342 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
347 if (mode
!= MLO_AN_FIXED
)
348 pr_debug("Fixed state.\n");
350 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
351 /* Clear id_mode_dis bit, and the existing port mode, let
352 * RGMII_MODE_EN bet set by mac_link_{up,down}
354 reg
&= ~(RX_PAUSE_EN
| TX_PAUSE_EN
);
356 if (state
->pause
& MLO_PAUSE_TXRX_MASK
) {
357 if (state
->pause
& MLO_PAUSE_TX
)
363 reg
&= ~(3 << speed_bit
);
364 switch (state
->speed
) {
366 reg
|= 2 << speed_bit
;
369 reg
|= 1 << speed_bit
;
373 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
374 reg
&= ~(DUPLEX_FULL
| FORCE_LINK_EN
);
376 reg
|= FORCE_LINK_EN
;
377 if (state
->duplex
== DUPLEX_FULL
)
382 if (priv
->family_id
== RTL8380_FAMILY_ID
)
384 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
387 static void rtl83xx_phylink_mac_link_down(struct dsa_switch
*ds
, int port
,
389 phy_interface_t interface
)
391 struct rtl838x_switch_priv
*priv
= ds
->priv
;
392 /* Stop TX/RX to port */
393 sw_w32_mask(0x3, 0, priv
->r
->mac_port_ctrl(port
));
396 static void rtl83xx_phylink_mac_link_up(struct dsa_switch
*ds
, int port
,
398 phy_interface_t interface
,
399 struct phy_device
*phydev
)
401 struct rtl838x_switch_priv
*priv
= ds
->priv
;
402 /* Restart TX/RX to port */
403 sw_w32_mask(0, 0x3, priv
->r
->mac_port_ctrl(port
));
406 static void rtl83xx_get_strings(struct dsa_switch
*ds
,
407 int port
, u32 stringset
, u8
*data
)
411 if (stringset
!= ETH_SS_STATS
)
414 for (i
= 0; i
< ARRAY_SIZE(rtl83xx_mib
); i
++)
415 strncpy(data
+ i
* ETH_GSTRING_LEN
, rtl83xx_mib
[i
].name
,
419 static void rtl83xx_get_ethtool_stats(struct dsa_switch
*ds
, int port
,
422 struct rtl838x_switch_priv
*priv
= ds
->priv
;
423 const struct rtl83xx_mib_desc
*mib
;
427 for (i
= 0; i
< ARRAY_SIZE(rtl83xx_mib
); i
++) {
428 mib
= &rtl83xx_mib
[i
];
430 data
[i
] = sw_r32(priv
->r
->stat_port_std_mib(port
) + 252 - mib
->offset
);
431 if (mib
->size
== 2) {
432 high
= sw_r32(priv
->r
->stat_port_std_mib(port
) + 252 - mib
->offset
- 4);
433 data
[i
] |= high
<< 32;
438 static int rtl83xx_get_sset_count(struct dsa_switch
*ds
, int port
, int sset
)
440 if (sset
!= ETH_SS_STATS
)
443 return ARRAY_SIZE(rtl83xx_mib
);
446 static int rtl83xx_port_enable(struct dsa_switch
*ds
, int port
,
447 struct phy_device
*phydev
)
449 struct rtl838x_switch_priv
*priv
= ds
->priv
;
451 pr_debug("%s: %x %d", __func__
, (u32
) priv
, port
);
452 priv
->ports
[port
].enable
= true;
454 /* enable inner tagging on egress, do not keep any tags */
455 sw_w32(1, priv
->r
->vlan_port_tag_sts_ctrl(port
));
457 if (dsa_is_cpu_port(ds
, port
))
460 /* add port to switch mask of CPU_PORT */
461 priv
->r
->mask_port_reg_be(0ULL, BIT_ULL(port
), priv
->r
->port_iso_ctrl(priv
->cpu_port
));
463 /* add all other ports in the same bridge to switch mask of port */
464 priv
->r
->mask_port_reg_be(0ULL, priv
->ports
[port
].pm
, priv
->r
->port_iso_ctrl(port
));
469 static void rtl83xx_port_disable(struct dsa_switch
*ds
, int port
)
471 struct rtl838x_switch_priv
*priv
= ds
->priv
;
473 pr_debug("%s %x: %d", __func__
, (u32
)priv
, port
);
474 /* you can only disable user ports */
475 if (!dsa_is_user_port(ds
, port
))
478 /* remove port from switch mask of CPU_PORT */
479 priv
->r
->mask_port_reg_be(BIT_ULL(port
), 0, priv
->r
->port_iso_ctrl(priv
->cpu_port
));
481 /* remove all other ports in the same bridge from switch mask of port */
482 priv
->r
->mask_port_reg_be(priv
->ports
[port
].pm
, 0LL, priv
->r
->port_iso_ctrl(port
));
484 priv
->ports
[port
].enable
= false;
487 static int rtl83xx_get_mac_eee(struct dsa_switch
*ds
, int port
,
488 struct ethtool_eee
*e
)
490 struct rtl838x_switch_priv
*priv
= ds
->priv
;
492 pr_debug("%s: port %d", __func__
, port
);
493 e
->supported
= SUPPORTED_100baseT_Full
| SUPPORTED_1000baseT_Full
;
494 if (sw_r32(priv
->r
->mac_force_mode_ctrl(port
)) & BIT(9))
495 e
->advertised
|= ADVERTISED_100baseT_Full
;
497 if (sw_r32(priv
->r
->mac_force_mode_ctrl(port
)) & BIT(10))
498 e
->advertised
|= ADVERTISED_1000baseT_Full
;
500 e
->eee_enabled
= priv
->ports
[port
].eee_enabled
;
501 pr_debug("enabled: %d, active %x\n", e
->eee_enabled
, e
->advertised
);
503 if (sw_r32(RTL838X_MAC_EEE_ABLTY
) & BIT(port
)) {
504 e
->lp_advertised
= ADVERTISED_100baseT_Full
;
505 e
->lp_advertised
|= ADVERTISED_1000baseT_Full
;
508 e
->eee_active
= !!(e
->advertised
& e
->lp_advertised
);
509 pr_debug("active: %d, lp %x\n", e
->eee_active
, e
->lp_advertised
);
514 static int rtl83xx_set_mac_eee(struct dsa_switch
*ds
, int port
,
515 struct ethtool_eee
*e
)
517 struct rtl838x_switch_priv
*priv
= ds
->priv
;
519 pr_debug("%s: port %d", __func__
, port
);
520 if (e
->eee_enabled
) {
521 pr_debug("Globally enabling EEE\n");
522 sw_w32_mask(0x4, 0, RTL838X_SMI_GLB_CTRL
);
524 if (e
->eee_enabled
) {
525 pr_debug("Enabling EEE for MAC %d\n", port
);
526 sw_w32_mask(0, 3 << 9, priv
->r
->mac_force_mode_ctrl(port
));
527 sw_w32_mask(0, BIT(port
), RTL838X_EEE_PORT_TX_EN
);
528 sw_w32_mask(0, BIT(port
), RTL838X_EEE_PORT_RX_EN
);
529 priv
->ports
[port
].eee_enabled
= true;
530 e
->eee_enabled
= true;
532 pr_debug("Disabling EEE for MAC %d\n", port
);
533 sw_w32_mask(3 << 9, 0, priv
->r
->mac_force_mode_ctrl(port
));
534 sw_w32_mask(BIT(port
), 0, RTL838X_EEE_PORT_TX_EN
);
535 sw_w32_mask(BIT(port
), 0, RTL838X_EEE_PORT_RX_EN
);
536 priv
->ports
[port
].eee_enabled
= false;
537 e
->eee_enabled
= false;
543 * Set Switch L2 Aging time, t is time in milliseconds
544 * t = 0: aging is disabled
546 static int rtl83xx_set_l2aging(struct dsa_switch
*ds
, u32 t
)
548 struct rtl838x_switch_priv
*priv
= ds
->priv
;
549 int t_max
= priv
->family_id
== RTL8380_FAMILY_ID
? 0x7fffff : 0x1FFFFF;
551 /* Convert time in mseconds to internal value */
552 if (t
> 0x10000000) { /* Set to maximum */
555 if (priv
->family_id
== RTL8380_FAMILY_ID
)
556 t
= ((t
* 625) / 1000 + 127) / 128;
560 sw_w32(t
, priv
->r
->l2_ctrl_1
);
564 static int rtl83xx_port_bridge_join(struct dsa_switch
*ds
, int port
,
565 struct net_device
*bridge
)
567 struct rtl838x_switch_priv
*priv
= ds
->priv
;
568 u64 port_bitmap
= BIT_ULL(priv
->cpu_port
);
571 pr_debug("%s %x: %d %llx", __func__
, (u32
)priv
, port
, port_bitmap
);
572 mutex_lock(&priv
->reg_mutex
);
573 for (i
= 0; i
< ds
->num_ports
; i
++) {
574 /* Add this port to the port matrix of the other ports in the
575 * same bridge. If the port is disabled, port matrix is kept
576 * and not being setup until the port becomes enabled.
578 if (dsa_is_user_port(ds
, i
) && i
!= port
) {
579 if (dsa_to_port(ds
, i
)->bridge_dev
!= bridge
)
581 if (priv
->ports
[i
].enable
)
582 priv
->r
->mask_port_reg_be(0, BIT_ULL(port
),
583 priv
->r
->port_iso_ctrl(i
));
584 priv
->ports
[i
].pm
|= BIT_ULL(port
);
586 port_bitmap
|= BIT_ULL(i
);
590 /* Add all other ports to this port matrix. */
591 if (priv
->ports
[port
].enable
) {
592 priv
->r
->mask_port_reg_be(0, BIT_ULL(port
),
593 priv
->r
->port_iso_ctrl(priv
->cpu_port
));
594 priv
->r
->mask_port_reg_be(0, port_bitmap
,
595 priv
->r
->port_iso_ctrl(port
));
597 priv
->ports
[port
].pm
|= port_bitmap
;
598 mutex_unlock(&priv
->reg_mutex
);
603 static void rtl83xx_port_bridge_leave(struct dsa_switch
*ds
, int port
,
604 struct net_device
*bridge
)
606 struct rtl838x_switch_priv
*priv
= ds
->priv
;
607 u64 port_bitmap
= BIT_ULL(priv
->cpu_port
);
610 pr_debug("%s %x: %d", __func__
, (u32
)priv
, port
);
611 mutex_lock(&priv
->reg_mutex
);
612 for (i
= 0; i
< ds
->num_ports
; i
++) {
613 /* Remove this port from the port matrix of the other ports
614 * in the same bridge. If the port is disabled, port matrix
615 * is kept and not being setup until the port becomes enabled.
616 * And the other port's port matrix cannot be broken when the
617 * other port is still a VLAN-aware port.
619 if (dsa_is_user_port(ds
, i
) && i
!= port
) {
620 if (dsa_to_port(ds
, i
)->bridge_dev
!= bridge
)
622 if (priv
->ports
[i
].enable
)
623 priv
->r
->mask_port_reg_be(BIT_ULL(port
), 0,
624 priv
->r
->port_iso_ctrl(i
));
625 priv
->ports
[i
].pm
&= ~BIT_ULL(port
);
627 port_bitmap
&= ~BIT_ULL(i
);
631 /* Add all other ports to this port matrix. */
632 if (priv
->ports
[port
].enable
)
633 priv
->r
->mask_port_reg_be(0, port_bitmap
, priv
->r
->port_iso_ctrl(port
));
634 priv
->ports
[port
].pm
&= ~port_bitmap
;
636 mutex_unlock(&priv
->reg_mutex
);
639 static void rtl83xx_port_stp_state_set(struct dsa_switch
*ds
, int port
,
646 struct rtl838x_switch_priv
*priv
= ds
->priv
;
647 int n
= priv
->family_id
== RTL8380_FAMILY_ID
? 2 : 4;
649 pr_debug("%s: port %d state %2x\n", __func__
, port
, state
);
651 /* CPU PORT can only be configured on RTL838x */
652 if (port
>= priv
->cpu_port
|| port
> 51)
655 mutex_lock(&priv
->reg_mutex
);
657 /* For the RTL839x, the bits are left-aligned in the 128 bit field */
658 if (priv
->family_id
== RTL8390_FAMILY_ID
)
661 index
= n
- (pos
>> 4) - 1;
662 bit
= (pos
<< 1) % 32;
664 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
665 cmd
= BIT(15) /* Execute cmd */
667 | 2 << 12 /* Table type 0b10 */
670 cmd
= BIT(16) /* Execute cmd */
672 | 5 << 12 /* Table type 0b101 */
675 priv
->r
->exec_tbl0_cmd(cmd
);
677 for (i
= 0; i
< n
; i
++)
678 port_state
[i
] = sw_r32(priv
->r
->tbl_access_data_0(i
));
680 pr_debug("Current state, port %d: %d\n", port
, (port_state
[index
] >> bit
) & 3);
681 port_state
[index
] &= ~(3 << bit
);
684 case BR_STATE_DISABLED
: /* 0 */
685 port_state
[index
] |= (0 << bit
);
687 case BR_STATE_BLOCKING
: /* 4 */
688 case BR_STATE_LISTENING
: /* 1 */
689 port_state
[index
] |= (1 << bit
);
691 case BR_STATE_LEARNING
: /* 2 */
692 port_state
[index
] |= (2 << bit
);
694 case BR_STATE_FORWARDING
: /* 3*/
695 port_state
[index
] |= (3 << bit
);
700 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
701 cmd
= BIT(15) /* Execute cmd */
702 | 0 << 14 /* Write */
703 | 2 << 12 /* Table type 0b10 */
706 cmd
= 1 << 16 /* Execute cmd */
707 | BIT(15) /* Write */
708 | 5 << 12 /* Table type 0b101 */
711 for (i
= 0; i
< n
; i
++)
712 sw_w32(port_state
[i
], priv
->r
->tbl_access_data_0(i
));
713 priv
->r
->exec_tbl0_cmd(cmd
);
715 mutex_unlock(&priv
->reg_mutex
);
718 static void rtl83xx_fast_age(struct dsa_switch
*ds
, int port
)
720 struct rtl838x_switch_priv
*priv
= ds
->priv
;
721 int s
= priv
->family_id
== RTL8390_FAMILY_ID
? 2 : 0;
723 pr_debug("FAST AGE port %d\n", port
);
724 mutex_lock(&priv
->reg_mutex
);
725 /* RTL838X_L2_TBL_FLUSH_CTRL register bits, 839x has 1 bit larger
727 * 0-4: Replacing port
728 * 5-9: Flushed/replaced port
730 * 22: Entry types: 1: dynamic, 0: also static
731 * 23: Match flush port
733 * 25: Flush (0) or replace (1) L2 entries
734 * 26: Status of action (1: Start, 0: Done)
736 sw_w32(1 << (26 + s
) | 1 << (23 + s
) | port
<< (5 + (s
/ 2)), priv
->r
->l2_tbl_flush_ctrl
);
738 do { } while (sw_r32(priv
->r
->l2_tbl_flush_ctrl
) & (1 << (26 + s
)));
740 mutex_unlock(&priv
->reg_mutex
);
743 static int rtl83xx_vlan_filtering(struct dsa_switch
*ds
, int port
,
746 struct rtl838x_switch_priv
*priv
= ds
->priv
;
748 pr_debug("%s: port %d\n", __func__
, port
);
749 mutex_lock(&priv
->reg_mutex
);
751 if (vlan_filtering
) {
752 /* Enable ingress and egress filtering */
753 if (port
!= priv
->cpu_port
)
754 sw_w32_mask(0b10 << ((port
% 16) << 1), 0b01 << ((port
% 16) << 1),
755 priv
->r
->vlan_port_igr_filter(port
));
756 sw_w32_mask(0, 1 << (port
% 32), priv
->r
->vlan_port_egr_filter(port
));
758 /* Disable ingress and egress filtering */
759 if (port
!= priv
->cpu_port
)
760 sw_w32_mask(0b11 << ((port
% 16) << 1), 0,
761 priv
->r
->vlan_port_igr_filter(port
));
762 sw_w32_mask(1 << (port
% 32), 0, priv
->r
->vlan_port_egr_filter(port
));
765 /* Do we need to do something to the CPU-Port, too? */
766 mutex_unlock(&priv
->reg_mutex
);
771 static int rtl83xx_vlan_prepare(struct dsa_switch
*ds
, int port
,
772 const struct switchdev_obj_port_vlan
*vlan
)
774 struct rtl838x_vlan_info info
;
775 struct rtl838x_switch_priv
*priv
= ds
->priv
;
777 pr_debug("%s: port %d\n", __func__
, port
);
779 mutex_lock(&priv
->reg_mutex
);
781 if (priv
->family_id
== RTL8380_FAMILY_ID
)
782 rtl838x_vlan_profile_dump(0);
784 rtl839x_vlan_profile_dump(0);
786 priv
->r
->vlan_tables_read(0, &info
);
788 pr_debug("Tagged ports %llx, untag %llx, prof %x, MC# %d, UC# %d, FID %x\n",
789 info
.tagged_ports
, info
.untagged_ports
, info
.profile_id
,
790 info
.hash_mc_fid
, info
.hash_uc_fid
, info
.fid
);
792 mutex_unlock(&priv
->reg_mutex
);
796 static void rtl83xx_vlan_add(struct dsa_switch
*ds
, int port
,
797 const struct switchdev_obj_port_vlan
*vlan
)
799 struct rtl838x_vlan_info info
= {};
800 struct rtl838x_switch_priv
*priv
= ds
->priv
;
803 pr_debug("%s port %d, vid_end %d, vid_end %d, flags %x\n", __func__
,
804 port
, vlan
->vid_begin
, vlan
->vid_end
, vlan
->flags
);
806 if (vlan
->vid_begin
> 4095 || vlan
->vid_end
> 4095) {
807 dev_err(priv
->dev
, "VLAN out of range: %d - %d",
808 vlan
->vid_begin
, vlan
->vid_end
);
812 mutex_lock(&priv
->reg_mutex
);
814 if (vlan
->flags
& BRIDGE_VLAN_INFO_PVID
) {
815 /* Set both inner and outer PVID of the port */
816 sw_w32((vlan
->vid_end
<< 16) | vlan
->vid_end
<< 2, priv
->r
->vlan_port_pb(port
));
817 priv
->ports
[port
].pvid
= vlan
->vid_end
;
820 for (v
= vlan
->vid_begin
; v
<= vlan
->vid_end
; v
++) {
821 /* Get port memberships of this vlan */
822 priv
->r
->vlan_tables_read(v
, &info
);
825 if (!info
.tagged_ports
) {
827 info
.hash_mc_fid
= false;
828 info
.hash_uc_fid
= false;
832 /* sanitize untagged_ports - must be a subset */
833 if (info
.untagged_ports
& ~info
.tagged_ports
)
834 info
.untagged_ports
= 0;
836 info
.tagged_ports
|= BIT_ULL(port
);
837 if (vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
838 info
.untagged_ports
|= BIT_ULL(port
);
840 priv
->r
->vlan_set_untagged(v
, info
.untagged_ports
);
841 pr_debug("Untagged ports, VLAN %d: %llx\n", v
, info
.untagged_ports
);
843 priv
->r
->vlan_set_tagged(v
, &info
);
844 pr_debug("Tagged ports, VLAN %d: %llx\n", v
, info
.tagged_ports
);
847 mutex_unlock(&priv
->reg_mutex
);
850 static int rtl83xx_vlan_del(struct dsa_switch
*ds
, int port
,
851 const struct switchdev_obj_port_vlan
*vlan
)
853 struct rtl838x_vlan_info info
;
854 struct rtl838x_switch_priv
*priv
= ds
->priv
;
858 pr_debug("%s: port %d, vid_end %d, vid_end %d, flags %x\n", __func__
,
859 port
, vlan
->vid_begin
, vlan
->vid_end
, vlan
->flags
);
861 if (vlan
->vid_begin
> 4095 || vlan
->vid_end
> 4095) {
862 dev_err(priv
->dev
, "VLAN out of range: %d - %d",
863 vlan
->vid_begin
, vlan
->vid_end
);
867 mutex_lock(&priv
->reg_mutex
);
868 pvid
= priv
->ports
[port
].pvid
;
870 for (v
= vlan
->vid_begin
; v
<= vlan
->vid_end
; v
++) {
871 /* Reset to default if removing the current PVID */
873 sw_w32(0, priv
->r
->vlan_port_pb(port
));
875 /* Get port memberships of this vlan */
876 priv
->r
->vlan_tables_read(v
, &info
);
878 /* remove port from both tables */
879 info
.untagged_ports
&= (~BIT_ULL(port
));
880 /* always leave vid 1 */
882 info
.tagged_ports
&= (~BIT_ULL(port
));
884 priv
->r
->vlan_set_untagged(v
, info
.untagged_ports
);
885 pr_debug("Untagged ports, VLAN %d: %llx\n", v
, info
.untagged_ports
);
887 priv
->r
->vlan_set_tagged(v
, &info
);
888 pr_debug("Tagged ports, VLAN %d: %llx\n", v
, info
.tagged_ports
);
890 mutex_unlock(&priv
->reg_mutex
);
895 static int rtl83xx_port_fdb_add(struct dsa_switch
*ds
, int port
,
896 const unsigned char *addr
, u16 vid
)
898 struct rtl838x_switch_priv
*priv
= ds
->priv
;
899 u64 mac
= ether_addr_to_u64(addr
);
900 u32 key
= rtl83xx_hash_key(priv
, mac
, vid
);
901 struct rtl838x_l2_entry e
;
904 int idx
= -1, err
= 0, i
;
906 mutex_lock(&priv
->reg_mutex
);
907 for (i
= 0; i
< 4; i
++) {
908 entry
= priv
->r
->read_l2_entry_using_hash(key
, i
, &e
);
910 idx
= (key
<< 2) | i
;
913 if ((entry
& 0x0fffffffffffffffULL
) == ((mac
<< 12) | vid
)) {
914 idx
= (key
<< 2) | i
;
919 r
[0] = 3 << 17 | port
<< 12; // Aging and port
922 r
[2] = (mac
& 0xffff) << 12; /* rvid = 0 */
923 rtl83xx_write_hash(idx
, r
);
927 /* Hash buckets full, try CAM */
928 for (i
= 0; i
< 64; i
++) {
929 entry
= priv
->r
->read_cam(i
, &e
);
931 if (idx
< 0) /* First empty entry? */
934 } else if ((entry
& 0x0fffffffffffffffULL
) == ((mac
<< 12) | vid
)) {
935 pr_debug("Found entry in CAM\n");
941 r
[0] = 3 << 17 | port
<< 12; // Aging
944 r
[2] = (mac
& 0xffff) << 12; /* rvid = 0 */
945 rtl83xx_write_cam(idx
, r
);
950 mutex_unlock(&priv
->reg_mutex
);
954 static int rtl83xx_port_fdb_del(struct dsa_switch
*ds
, int port
,
955 const unsigned char *addr
, u16 vid
)
957 struct rtl838x_switch_priv
*priv
= ds
->priv
;
958 u64 mac
= ether_addr_to_u64(addr
);
959 u32 key
= rtl83xx_hash_key(priv
, mac
, vid
);
960 struct rtl838x_l2_entry e
;
963 int idx
= -1, err
= 0, i
;
965 pr_debug("In %s, mac %llx, vid: %d, key: %x\n", __func__
, mac
, vid
, key
);
966 mutex_lock(&priv
->reg_mutex
);
967 for (i
= 0; i
< 4; i
++) {
968 entry
= priv
->r
->read_l2_entry_using_hash(key
, i
, &e
);
971 if ((entry
& 0x0fffffffffffffffULL
) == ((mac
<< 12) | vid
)) {
972 idx
= (key
<< 2) | i
;
978 r
[0] = r
[1] = r
[2] = 0;
979 rtl83xx_write_hash(idx
, r
);
983 /* Check CAM for spillover from hash buckets */
984 for (i
= 0; i
< 64; i
++) {
985 entry
= priv
->r
->read_cam(i
, &e
);
986 if ((entry
& 0x0fffffffffffffffULL
) == ((mac
<< 12) | vid
)) {
992 r
[0] = r
[1] = r
[2] = 0;
993 rtl83xx_write_cam(idx
, r
);
998 mutex_unlock(&priv
->reg_mutex
);
1002 static int rtl83xx_port_fdb_dump(struct dsa_switch
*ds
, int port
,
1003 dsa_fdb_dump_cb_t
*cb
, void *data
)
1005 struct rtl838x_l2_entry e
;
1006 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1012 mutex_lock(&priv
->reg_mutex
);
1014 for (i
= 0; i
< priv
->fib_entries
; i
++) {
1015 priv
->r
->read_l2_entry_using_hash(i
>> 2, i
& 0x3, &e
);
1020 if (e
.port
== port
) {
1021 fid
= (i
& 0x3ff) | (e
.rvid
& ~0x3ff);
1022 mac
= ether_addr_to_u64(&e
.mac
[0]);
1023 pkey
= rtl838x_hash(priv
, mac
<< 12 | fid
);
1024 fid
= (pkey
& 0x3ff) | (fid
& ~0x3ff);
1025 pr_debug("-> mac %016llx, fid: %d\n", mac
, fid
);
1026 cb(e
.mac
, e
.vid
, e
.is_static
, data
);
1030 for (i
= 0; i
< 64; i
++) {
1031 priv
->r
->read_cam(i
, &e
);
1037 cb(e
.mac
, e
.vid
, e
.is_static
, data
);
1040 mutex_unlock(&priv
->reg_mutex
);
1044 static int rtl83xx_port_mirror_add(struct dsa_switch
*ds
, int port
,
1045 struct dsa_mall_mirror_tc_entry
*mirror
,
1048 /* We support 4 mirror groups, one destination port per group */
1050 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1052 pr_debug("In %s\n", __func__
);
1054 for (group
= 0; group
< 4; group
++) {
1055 if (priv
->mirror_group_ports
[group
] == mirror
->to_local_port
)
1059 for (group
= 0; group
< 4; group
++) {
1060 if (priv
->mirror_group_ports
[group
] < 0)
1068 pr_debug("Using group %d\n", group
);
1069 mutex_lock(&priv
->reg_mutex
);
1071 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
1072 /* Enable mirroring to port across VLANs (bit 11) */
1073 sw_w32(1 << 11 | (mirror
->to_local_port
<< 4) | 1, RTL838X_MIR_CTRL(group
));
1075 /* Enable mirroring to destination port */
1076 sw_w32((mirror
->to_local_port
<< 4) | 1, RTL839X_MIR_CTRL(group
));
1079 if (ingress
&& (priv
->r
->get_port_reg_be(priv
->r
->mir_spm(group
)) & (1ULL << port
))) {
1080 mutex_unlock(&priv
->reg_mutex
);
1083 if ((!ingress
) && (priv
->r
->get_port_reg_be(priv
->r
->mir_dpm(group
)) & (1ULL << port
))) {
1084 mutex_unlock(&priv
->reg_mutex
);
1089 priv
->r
->mask_port_reg_be(0, 1ULL << port
, priv
->r
->mir_spm(group
));
1091 priv
->r
->mask_port_reg_be(0, 1ULL << port
, priv
->r
->mir_dpm(group
));
1093 priv
->mirror_group_ports
[group
] = mirror
->to_local_port
;
1094 mutex_unlock(&priv
->reg_mutex
);
1098 static void rtl83xx_port_mirror_del(struct dsa_switch
*ds
, int port
,
1099 struct dsa_mall_mirror_tc_entry
*mirror
)
1102 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1104 pr_debug("In %s\n", __func__
);
1105 for (group
= 0; group
< 4; group
++) {
1106 if (priv
->mirror_group_ports
[group
] == mirror
->to_local_port
)
1112 mutex_lock(&priv
->reg_mutex
);
1113 if (mirror
->ingress
) {
1114 /* Ingress, clear source port matrix */
1115 priv
->r
->mask_port_reg_be(1ULL << port
, 0, priv
->r
->mir_spm(group
));
1117 /* Egress, clear destination port matrix */
1118 priv
->r
->mask_port_reg_be(1ULL << port
, 0, priv
->r
->mir_dpm(group
));
1121 if (!(sw_r32(priv
->r
->mir_spm(group
)) || sw_r32(priv
->r
->mir_dpm(group
)))) {
1122 priv
->mirror_group_ports
[group
] = -1;
1123 sw_w32(0, priv
->r
->mir_ctrl(group
));
1126 mutex_unlock(&priv
->reg_mutex
);
1129 const struct dsa_switch_ops rtl83xx_switch_ops
= {
1130 .get_tag_protocol
= rtl83xx_get_tag_protocol
,
1131 .setup
= rtl83xx_setup
,
1133 .phy_read
= rtl83xx_dsa_phy_read
,
1134 .phy_write
= rtl83xx_dsa_phy_write
,
1136 .phylink_validate
= rtl83xx_phylink_validate
,
1137 .phylink_mac_link_state
= rtl83xx_phylink_mac_link_state
,
1138 .phylink_mac_config
= rtl83xx_phylink_mac_config
,
1139 .phylink_mac_link_down
= rtl83xx_phylink_mac_link_down
,
1140 .phylink_mac_link_up
= rtl83xx_phylink_mac_link_up
,
1142 .get_strings
= rtl83xx_get_strings
,
1143 .get_ethtool_stats
= rtl83xx_get_ethtool_stats
,
1144 .get_sset_count
= rtl83xx_get_sset_count
,
1146 .port_enable
= rtl83xx_port_enable
,
1147 .port_disable
= rtl83xx_port_disable
,
1149 .get_mac_eee
= rtl83xx_get_mac_eee
,
1150 .set_mac_eee
= rtl83xx_set_mac_eee
,
1152 .set_ageing_time
= rtl83xx_set_l2aging
,
1153 .port_bridge_join
= rtl83xx_port_bridge_join
,
1154 .port_bridge_leave
= rtl83xx_port_bridge_leave
,
1155 .port_stp_state_set
= rtl83xx_port_stp_state_set
,
1156 .port_fast_age
= rtl83xx_fast_age
,
1158 .port_vlan_filtering
= rtl83xx_vlan_filtering
,
1159 .port_vlan_prepare
= rtl83xx_vlan_prepare
,
1160 .port_vlan_add
= rtl83xx_vlan_add
,
1161 .port_vlan_del
= rtl83xx_vlan_del
,
1163 .port_fdb_add
= rtl83xx_port_fdb_add
,
1164 .port_fdb_del
= rtl83xx_port_fdb_del
,
1165 .port_fdb_dump
= rtl83xx_port_fdb_dump
,
1167 .port_mirror_add
= rtl83xx_port_mirror_add
,
1168 .port_mirror_del
= rtl83xx_port_mirror_del
,