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_init_stats(struct rtl838x_switch_priv
*priv
)
15 mutex_lock(&priv
->reg_mutex
);
17 /* Enable statistics module: all counters plus debug.
18 * On RTL839x all counters are enabled by default
20 if (priv
->family_id
== RTL8380_FAMILY_ID
)
21 sw_w32_mask(0, 3, RTL838X_STAT_CTRL
);
23 /* Reset statistics counters */
24 sw_w32_mask(0, 1, priv
->r
->stat_rst
);
26 mutex_unlock(&priv
->reg_mutex
);
29 static void rtl83xx_enable_phy_polling(struct rtl838x_switch_priv
*priv
)
35 /* Enable all ports with a PHY, including the SFP-ports */
36 for (i
= 0; i
< priv
->cpu_port
; i
++) {
37 if (priv
->ports
[i
].phy
)
41 pr_debug("%s: %16llx\n", __func__
, v
);
42 priv
->r
->set_port_reg_le(v
, priv
->r
->smi_poll_ctrl
);
44 /* PHY update complete, there is no global PHY polling enable bit on the 9300 */
45 if (priv
->family_id
== RTL8390_FAMILY_ID
)
46 sw_w32_mask(0, BIT(7), RTL839X_SMI_GLB_CTRL
);
47 else if(priv
->family_id
== RTL9300_FAMILY_ID
)
48 sw_w32_mask(0, 0x8000, RTL838X_SMI_GLB_CTRL
);
51 const struct rtl83xx_mib_desc rtl83xx_mib
[] = {
52 MIB_DESC(2, 0xf8, "ifInOctets"),
53 MIB_DESC(2, 0xf0, "ifOutOctets"),
54 MIB_DESC(1, 0xec, "dot1dTpPortInDiscards"),
55 MIB_DESC(1, 0xe8, "ifInUcastPkts"),
56 MIB_DESC(1, 0xe4, "ifInMulticastPkts"),
57 MIB_DESC(1, 0xe0, "ifInBroadcastPkts"),
58 MIB_DESC(1, 0xdc, "ifOutUcastPkts"),
59 MIB_DESC(1, 0xd8, "ifOutMulticastPkts"),
60 MIB_DESC(1, 0xd4, "ifOutBroadcastPkts"),
61 MIB_DESC(1, 0xd0, "ifOutDiscards"),
62 MIB_DESC(1, 0xcc, ".3SingleCollisionFrames"),
63 MIB_DESC(1, 0xc8, ".3MultipleCollisionFrames"),
64 MIB_DESC(1, 0xc4, ".3DeferredTransmissions"),
65 MIB_DESC(1, 0xc0, ".3LateCollisions"),
66 MIB_DESC(1, 0xbc, ".3ExcessiveCollisions"),
67 MIB_DESC(1, 0xb8, ".3SymbolErrors"),
68 MIB_DESC(1, 0xb4, ".3ControlInUnknownOpcodes"),
69 MIB_DESC(1, 0xb0, ".3InPauseFrames"),
70 MIB_DESC(1, 0xac, ".3OutPauseFrames"),
71 MIB_DESC(1, 0xa8, "DropEvents"),
72 MIB_DESC(1, 0xa4, "tx_BroadcastPkts"),
73 MIB_DESC(1, 0xa0, "tx_MulticastPkts"),
74 MIB_DESC(1, 0x9c, "CRCAlignErrors"),
75 MIB_DESC(1, 0x98, "tx_UndersizePkts"),
76 MIB_DESC(1, 0x94, "rx_UndersizePkts"),
77 MIB_DESC(1, 0x90, "rx_UndersizedropPkts"),
78 MIB_DESC(1, 0x8c, "tx_OversizePkts"),
79 MIB_DESC(1, 0x88, "rx_OversizePkts"),
80 MIB_DESC(1, 0x84, "Fragments"),
81 MIB_DESC(1, 0x80, "Jabbers"),
82 MIB_DESC(1, 0x7c, "Collisions"),
83 MIB_DESC(1, 0x78, "tx_Pkts64Octets"),
84 MIB_DESC(1, 0x74, "rx_Pkts64Octets"),
85 MIB_DESC(1, 0x70, "tx_Pkts65to127Octets"),
86 MIB_DESC(1, 0x6c, "rx_Pkts65to127Octets"),
87 MIB_DESC(1, 0x68, "tx_Pkts128to255Octets"),
88 MIB_DESC(1, 0x64, "rx_Pkts128to255Octets"),
89 MIB_DESC(1, 0x60, "tx_Pkts256to511Octets"),
90 MIB_DESC(1, 0x5c, "rx_Pkts256to511Octets"),
91 MIB_DESC(1, 0x58, "tx_Pkts512to1023Octets"),
92 MIB_DESC(1, 0x54, "rx_Pkts512to1023Octets"),
93 MIB_DESC(1, 0x50, "tx_Pkts1024to1518Octets"),
94 MIB_DESC(1, 0x4c, "rx_StatsPkts1024to1518Octets"),
95 MIB_DESC(1, 0x48, "tx_Pkts1519toMaxOctets"),
96 MIB_DESC(1, 0x44, "rx_Pkts1519toMaxOctets"),
97 MIB_DESC(1, 0x40, "rxMacDiscards")
104 static enum dsa_tag_protocol
rtl83xx_get_tag_protocol(struct dsa_switch
*ds
,
106 enum dsa_tag_protocol mprot
)
108 /* The switch does not tag the frames, instead internally the header
109 * structure for each packet is tagged accordingly.
111 return DSA_TAG_PROTO_TRAILER
;
115 * Initialize all VLANS
117 static void rtl83xx_vlan_setup(struct rtl838x_switch_priv
*priv
)
119 struct rtl838x_vlan_info info
;
122 pr_info("In %s\n", __func__
);
124 priv
->r
->vlan_profile_setup(0);
125 priv
->r
->vlan_profile_setup(1);
126 pr_info("UNKNOWN_MC_PMASK: %016llx\n", priv
->r
->read_mcast_pmask(UNKNOWN_MC_PMASK
));
127 priv
->r
->vlan_profile_dump(0);
129 info
.fid
= 0; // Default Forwarding ID / MSTI
130 info
.hash_uc_fid
= false; // Do not build the L2 lookup hash with FID, but VID
131 info
.hash_mc_fid
= false; // Do the same for Multicast packets
132 info
.profile_id
= 0; // Use default Vlan Profile 0
133 info
.tagged_ports
= 0; // Initially no port members
135 // Initialize all vlans 0-4095
136 for (i
= 0; i
< MAX_VLANS
; i
++)
137 priv
->r
->vlan_set_tagged(i
, &info
);
139 // reset PVIDs; defaults to 1 on reset
140 for (i
= 0; i
<= priv
->ds
->num_ports
; i
++)
141 sw_w32(0, priv
->r
->vlan_port_pb
+ (i
<< 2));
143 // Set forwarding action based on inner VLAN tag
144 for (i
= 0; i
< priv
->cpu_port
; i
++)
145 priv
->r
->vlan_fwd_on_inner(i
, true);
148 static int rtl83xx_setup(struct dsa_switch
*ds
)
151 struct rtl838x_switch_priv
*priv
= ds
->priv
;
152 u64 port_bitmap
= BIT_ULL(priv
->cpu_port
);
154 pr_debug("%s called\n", __func__
);
156 /* Disable MAC polling the PHY so that we can start configuration */
157 priv
->r
->set_port_reg_le(0ULL, priv
->r
->smi_poll_ctrl
);
159 for (i
= 0; i
< ds
->num_ports
; i
++)
160 priv
->ports
[i
].enable
= false;
161 priv
->ports
[priv
->cpu_port
].enable
= true;
163 /* Isolate ports from each other: traffic only CPU <-> port */
164 /* Setting bit j in register RTL838X_PORT_ISO_CTRL(i) allows
165 * traffic from source port i to destination port j
167 for (i
= 0; i
< priv
->cpu_port
; i
++) {
168 if (priv
->ports
[i
].phy
) {
169 priv
->r
->set_port_reg_be(BIT_ULL(priv
->cpu_port
) | BIT_ULL(i
),
170 priv
->r
->port_iso_ctrl(i
));
171 port_bitmap
|= BIT_ULL(i
);
174 priv
->r
->set_port_reg_be(port_bitmap
, priv
->r
->port_iso_ctrl(priv
->cpu_port
));
176 if (priv
->family_id
== RTL8380_FAMILY_ID
)
177 rtl838x_print_matrix();
179 rtl839x_print_matrix();
181 rtl83xx_init_stats(priv
);
183 rtl83xx_vlan_setup(priv
);
185 ds
->configure_vlan_while_not_filtering
= true;
187 priv
->r
->l2_learning_setup();
189 /* Enable MAC Polling PHY again */
190 rtl83xx_enable_phy_polling(priv
);
191 pr_debug("Please wait until PHY is settled\n");
193 priv
->r
->pie_init(priv
);
198 static int rtl930x_setup(struct dsa_switch
*ds
)
201 struct rtl838x_switch_priv
*priv
= ds
->priv
;
202 u32 port_bitmap
= BIT(priv
->cpu_port
);
204 pr_info("%s called\n", __func__
);
206 // Enable CSTI STP mode
207 // sw_w32(1, RTL930X_ST_CTRL);
209 /* Disable MAC polling the PHY so that we can start configuration */
210 sw_w32(0, RTL930X_SMI_POLL_CTRL
);
212 // Disable all ports except CPU port
213 for (i
= 0; i
< ds
->num_ports
; i
++)
214 priv
->ports
[i
].enable
= false;
215 priv
->ports
[priv
->cpu_port
].enable
= true;
217 for (i
= 0; i
< priv
->cpu_port
; i
++) {
218 if (priv
->ports
[i
].phy
) {
219 priv
->r
->traffic_set(i
, BIT_ULL(priv
->cpu_port
) | BIT_ULL(i
));
220 port_bitmap
|= BIT_ULL(i
);
223 priv
->r
->traffic_set(priv
->cpu_port
, port_bitmap
);
225 rtl930x_print_matrix();
227 // TODO: Initialize statistics
229 rtl83xx_vlan_setup(priv
);
231 ds
->configure_vlan_while_not_filtering
= true;
233 priv
->r
->l2_learning_setup();
235 rtl83xx_enable_phy_polling(priv
);
237 priv
->r
->pie_init(priv
);
242 static int rtl93xx_get_sds(struct phy_device
*phydev
)
244 struct device
*dev
= &phydev
->mdio
.dev
;
245 struct device_node
*dn
;
252 if (of_property_read_u32(dn
, "sds", &sds_num
))
255 dev_err(dev
, "No DT node.\n");
262 static void rtl83xx_phylink_validate(struct dsa_switch
*ds
, int port
,
263 unsigned long *supported
,
264 struct phylink_link_state
*state
)
266 struct rtl838x_switch_priv
*priv
= ds
->priv
;
267 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
269 pr_debug("In %s port %d, state is %d", __func__
, port
, state
->interface
);
271 if (!phy_interface_mode_is_rgmii(state
->interface
) &&
272 state
->interface
!= PHY_INTERFACE_MODE_NA
&&
273 state
->interface
!= PHY_INTERFACE_MODE_1000BASEX
&&
274 state
->interface
!= PHY_INTERFACE_MODE_MII
&&
275 state
->interface
!= PHY_INTERFACE_MODE_REVMII
&&
276 state
->interface
!= PHY_INTERFACE_MODE_GMII
&&
277 state
->interface
!= PHY_INTERFACE_MODE_QSGMII
&&
278 state
->interface
!= PHY_INTERFACE_MODE_INTERNAL
&&
279 state
->interface
!= PHY_INTERFACE_MODE_SGMII
) {
280 bitmap_zero(supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
282 "Unsupported interface: %d for port %d\n",
283 state
->interface
, port
);
287 /* Allow all the expected bits */
288 phylink_set(mask
, Autoneg
);
289 phylink_set_port_modes(mask
);
290 phylink_set(mask
, Pause
);
291 phylink_set(mask
, Asym_Pause
);
293 /* With the exclusion of MII and Reverse MII, we support Gigabit,
294 * including Half duplex
296 if (state
->interface
!= PHY_INTERFACE_MODE_MII
&&
297 state
->interface
!= PHY_INTERFACE_MODE_REVMII
) {
298 phylink_set(mask
, 1000baseT_Full
);
299 phylink_set(mask
, 1000baseT_Half
);
302 /* On both the 8380 and 8382, ports 24-27 are SFP ports */
303 if (port
>= 24 && port
<= 27 && priv
->family_id
== RTL8380_FAMILY_ID
)
304 phylink_set(mask
, 1000baseX_Full
);
306 /* On the RTL839x family of SoCs, ports 48 to 51 are SFP ports */
307 if (port
>= 48 && port
<= 51 && priv
->family_id
== RTL8390_FAMILY_ID
)
308 phylink_set(mask
, 1000baseX_Full
);
310 phylink_set(mask
, 10baseT_Half
);
311 phylink_set(mask
, 10baseT_Full
);
312 phylink_set(mask
, 100baseT_Half
);
313 phylink_set(mask
, 100baseT_Full
);
315 bitmap_and(supported
, supported
, mask
,
316 __ETHTOOL_LINK_MODE_MASK_NBITS
);
317 bitmap_and(state
->advertising
, state
->advertising
, mask
,
318 __ETHTOOL_LINK_MODE_MASK_NBITS
);
321 static void rtl93xx_phylink_validate(struct dsa_switch
*ds
, int port
,
322 unsigned long *supported
,
323 struct phylink_link_state
*state
)
325 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask
) = { 0, };
327 pr_debug("In %s port %d, state is %d (%s)", __func__
, port
, state
->interface
,
328 phy_modes(state
->interface
));
330 if (!phy_interface_mode_is_rgmii(state
->interface
) &&
331 state
->interface
!= PHY_INTERFACE_MODE_NA
&&
332 state
->interface
!= PHY_INTERFACE_MODE_1000BASEX
&&
333 state
->interface
!= PHY_INTERFACE_MODE_MII
&&
334 state
->interface
!= PHY_INTERFACE_MODE_REVMII
&&
335 state
->interface
!= PHY_INTERFACE_MODE_GMII
&&
336 state
->interface
!= PHY_INTERFACE_MODE_QSGMII
&&
337 state
->interface
!= PHY_INTERFACE_MODE_XGMII
&&
338 state
->interface
!= PHY_INTERFACE_MODE_HSGMII
&&
339 state
->interface
!= PHY_INTERFACE_MODE_10GKR
&&
340 state
->interface
!= PHY_INTERFACE_MODE_USXGMII
&&
341 state
->interface
!= PHY_INTERFACE_MODE_INTERNAL
&&
342 state
->interface
!= PHY_INTERFACE_MODE_SGMII
) {
343 bitmap_zero(supported
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
345 "Unsupported interface: %d for port %d\n",
346 state
->interface
, port
);
350 /* Allow all the expected bits */
351 phylink_set(mask
, Autoneg
);
352 phylink_set_port_modes(mask
);
353 phylink_set(mask
, Pause
);
354 phylink_set(mask
, Asym_Pause
);
356 /* With the exclusion of MII and Reverse MII, we support Gigabit,
357 * including Half duplex
359 if (state
->interface
!= PHY_INTERFACE_MODE_MII
&&
360 state
->interface
!= PHY_INTERFACE_MODE_REVMII
) {
361 phylink_set(mask
, 1000baseT_Full
);
362 phylink_set(mask
, 1000baseT_Half
);
365 /* On the RTL9300 family of SoCs, ports 26 to 27 may be SFP ports TODO: take out of .dts */
366 if (port
>= 26 && port
<= 27)
367 phylink_set(mask
, 1000baseX_Full
);
368 if (port
>= 26 && port
<= 27)
369 phylink_set(mask
, 10000baseKR_Full
);
371 phylink_set(mask
, 10baseT_Half
);
372 phylink_set(mask
, 10baseT_Full
);
373 phylink_set(mask
, 100baseT_Half
);
374 phylink_set(mask
, 100baseT_Full
);
376 bitmap_and(supported
, supported
, mask
,
377 __ETHTOOL_LINK_MODE_MASK_NBITS
);
378 bitmap_and(state
->advertising
, state
->advertising
, mask
,
379 __ETHTOOL_LINK_MODE_MASK_NBITS
);
382 static int rtl83xx_phylink_mac_link_state(struct dsa_switch
*ds
, int port
,
383 struct phylink_link_state
*state
)
385 struct rtl838x_switch_priv
*priv
= ds
->priv
;
389 if (port
< 0 || port
> priv
->cpu_port
)
393 link
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_sts
);
394 if (link
& BIT_ULL(port
))
396 pr_debug("%s: link state port %d: %llx\n", __func__
, port
, link
& BIT_ULL(port
));
399 if (priv
->r
->get_port_reg_le(priv
->r
->mac_link_dup_sts
) & BIT_ULL(port
))
402 speed
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_spd_sts(port
));
403 speed
>>= (port
% 16) << 1;
404 switch (speed
& 0x3) {
406 state
->speed
= SPEED_10
;
409 state
->speed
= SPEED_100
;
412 state
->speed
= SPEED_1000
;
415 if (priv
->family_id
== RTL9300_FAMILY_ID
416 && (port
== 24 || port
== 26)) /* Internal serdes */
417 state
->speed
= SPEED_2500
;
419 state
->speed
= SPEED_100
; /* Is in fact 500Mbit */
422 state
->pause
&= (MLO_PAUSE_RX
| MLO_PAUSE_TX
);
423 if (priv
->r
->get_port_reg_le(priv
->r
->mac_rx_pause_sts
) & BIT_ULL(port
))
424 state
->pause
|= MLO_PAUSE_RX
;
425 if (priv
->r
->get_port_reg_le(priv
->r
->mac_tx_pause_sts
) & BIT_ULL(port
))
426 state
->pause
|= MLO_PAUSE_TX
;
430 static int rtl93xx_phylink_mac_link_state(struct dsa_switch
*ds
, int port
,
431 struct phylink_link_state
*state
)
433 struct rtl838x_switch_priv
*priv
= ds
->priv
;
437 if (port
< 0 || port
> priv
->cpu_port
)
441 * On the RTL9300 for at least the RTL8226B PHY, the MAC-side link
442 * state needs to be read twice in order to read a correct result.
443 * This would not be necessary for ports connected e.g. to RTL8218D
447 link
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_sts
);
448 link
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_sts
);
449 if (link
& BIT_ULL(port
))
451 pr_debug("%s: link state port %d: %llx, media %08x\n", __func__
, port
,
452 link
& BIT_ULL(port
), sw_r32(RTL930X_MAC_LINK_MEDIA_STS
));
455 if (priv
->r
->get_port_reg_le(priv
->r
->mac_link_dup_sts
) & BIT_ULL(port
))
458 speed
= priv
->r
->get_port_reg_le(priv
->r
->mac_link_spd_sts(port
));
459 speed
>>= (port
% 8) << 2;
460 switch (speed
& 0xf) {
462 state
->speed
= SPEED_10
;
465 state
->speed
= SPEED_100
;
469 state
->speed
= SPEED_1000
;
472 state
->speed
= SPEED_10000
;
476 state
->speed
= SPEED_2500
;
479 state
->speed
= SPEED_5000
;
482 pr_err("%s: unknown speed: %d\n", __func__
, (u32
)speed
& 0xf);
485 pr_debug("%s: speed is: %d %d\n", __func__
, (u32
)speed
& 0xf, state
->speed
);
486 state
->pause
&= (MLO_PAUSE_RX
| MLO_PAUSE_TX
);
487 if (priv
->r
->get_port_reg_le(priv
->r
->mac_rx_pause_sts
) & BIT_ULL(port
))
488 state
->pause
|= MLO_PAUSE_RX
;
489 if (priv
->r
->get_port_reg_le(priv
->r
->mac_tx_pause_sts
) & BIT_ULL(port
))
490 state
->pause
|= MLO_PAUSE_TX
;
494 static void rtl83xx_config_interface(int port
, phy_interface_t interface
)
496 u32 old
, int_shift
, sds_shift
;
511 old
= sw_r32(RTL838X_SDS_MODE_SEL
);
513 case PHY_INTERFACE_MODE_1000BASEX
:
514 if ((old
>> sds_shift
& 0x1f) == 4)
516 sw_w32_mask(0x7 << int_shift
, 1 << int_shift
, RTL838X_INT_MODE_CTRL
);
517 sw_w32_mask(0x1f << sds_shift
, 4 << sds_shift
, RTL838X_SDS_MODE_SEL
);
519 case PHY_INTERFACE_MODE_SGMII
:
520 if ((old
>> sds_shift
& 0x1f) == 2)
522 sw_w32_mask(0x7 << int_shift
, 2 << int_shift
, RTL838X_INT_MODE_CTRL
);
523 sw_w32_mask(0x1f << sds_shift
, 2 << sds_shift
, RTL838X_SDS_MODE_SEL
);
528 pr_debug("configured port %d for interface %s\n", port
, phy_modes(interface
));
531 static void rtl83xx_phylink_mac_config(struct dsa_switch
*ds
, int port
,
533 const struct phylink_link_state
*state
)
535 struct rtl838x_switch_priv
*priv
= ds
->priv
;
537 int speed_bit
= priv
->family_id
== RTL8380_FAMILY_ID
? 4 : 3;
539 pr_debug("%s port %d, mode %x\n", __func__
, port
, mode
);
541 if (port
== priv
->cpu_port
) {
542 /* Set Speed, duplex, flow control
543 * FORCE_EN | LINK_EN | NWAY_EN | DUP_SEL
544 * | SPD_SEL = 0b10 | FORCE_FC_EN | PHY_MASTER_SLV_MANUAL_EN
547 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
548 sw_w32(0x6192F, priv
->r
->mac_force_mode_ctrl(priv
->cpu_port
));
549 /* allow CRC errors on CPU-port */
550 sw_w32_mask(0, 0x8, RTL838X_MAC_PORT_CTRL(priv
->cpu_port
));
552 sw_w32_mask(0, 3, priv
->r
->mac_force_mode_ctrl(priv
->cpu_port
));
557 reg
= sw_r32(priv
->r
->mac_force_mode_ctrl(port
));
558 /* Auto-Negotiation does not work for MAC in RTL8390 */
559 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
560 if (mode
== MLO_AN_PHY
|| phylink_autoneg_inband(mode
)) {
561 pr_debug("PHY autonegotiates\n");
563 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
564 rtl83xx_config_interface(port
, state
->interface
);
569 if (mode
!= MLO_AN_FIXED
)
570 pr_debug("Fixed state.\n");
572 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
573 /* Clear id_mode_dis bit, and the existing port mode, let
574 * RGMII_MODE_EN bet set by mac_link_{up,down}
576 reg
&= ~(RX_PAUSE_EN
| TX_PAUSE_EN
);
578 if (state
->pause
& MLO_PAUSE_TXRX_MASK
) {
579 if (state
->pause
& MLO_PAUSE_TX
)
585 reg
&= ~(3 << speed_bit
);
586 switch (state
->speed
) {
588 reg
|= 2 << speed_bit
;
591 reg
|= 1 << speed_bit
;
595 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
596 reg
&= ~(DUPLEX_FULL
| FORCE_LINK_EN
);
598 reg
|= FORCE_LINK_EN
;
599 if (state
->duplex
== DUPLEX_FULL
)
604 if (priv
->family_id
== RTL8380_FAMILY_ID
)
606 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
609 static void rtl93xx_phylink_mac_config(struct dsa_switch
*ds
, int port
,
611 const struct phylink_link_state
*state
)
613 struct rtl838x_switch_priv
*priv
= ds
->priv
;
614 int sds_num
, sds_mode
;
617 pr_info("%s port %d, mode %x, phy-mode: %s, speed %d, link %d\n", __func__
,
618 port
, mode
, phy_modes(state
->interface
), state
->speed
, state
->link
);
620 // Nothing to be done for the CPU-port
621 if (port
== priv
->cpu_port
)
624 reg
= sw_r32(priv
->r
->mac_force_mode_ctrl(port
));
627 sds_num
= priv
->ports
[port
].sds_num
;
628 pr_info("%s SDS is %d\n", __func__
, sds_num
);
630 switch (state
->interface
) {
631 case PHY_INTERFACE_MODE_HSGMII
:
634 case PHY_INTERFACE_MODE_1000BASEX
:
635 sds_mode
= 0x1b; // 10G 1000X Auto
637 case PHY_INTERFACE_MODE_XGMII
:
640 case PHY_INTERFACE_MODE_10GKR
:
642 // We need to use media sel for fibre media:
645 case PHY_INTERFACE_MODE_USXGMII
:
649 pr_err("%s: unknown serdes mode: %s\n",
650 __func__
, phy_modes(state
->interface
));
653 rtl9300_sds_rst(sds_num
, sds_mode
);
656 switch (state
->speed
) {
675 reg
|= FORCE_LINK_EN
;
677 if (state
->duplex
== DUPLEX_FULL
)
680 reg
|= 1; // Force Link up
681 sw_w32(reg
, priv
->r
->mac_force_mode_ctrl(port
));
684 static void rtl83xx_phylink_mac_link_down(struct dsa_switch
*ds
, int port
,
686 phy_interface_t interface
)
688 struct rtl838x_switch_priv
*priv
= ds
->priv
;
689 /* Stop TX/RX to port */
690 sw_w32_mask(0x3, 0, priv
->r
->mac_port_ctrl(port
));
693 static void rtl93xx_phylink_mac_link_down(struct dsa_switch
*ds
, int port
,
695 phy_interface_t interface
)
697 struct rtl838x_switch_priv
*priv
= ds
->priv
;
698 /* Stop TX/RX to port */
699 sw_w32_mask(0x3, 0, priv
->r
->mac_port_ctrl(port
));
701 // No longer force link
702 sw_w32_mask(3, 0, priv
->r
->mac_force_mode_ctrl(port
));
705 static void rtl83xx_phylink_mac_link_up(struct dsa_switch
*ds
, int port
,
707 phy_interface_t interface
,
708 struct phy_device
*phydev
,
709 int speed
, int duplex
,
710 bool tx_pause
, bool rx_pause
)
712 struct rtl838x_switch_priv
*priv
= ds
->priv
;
713 /* Restart TX/RX to port */
714 sw_w32_mask(0, 0x3, priv
->r
->mac_port_ctrl(port
));
715 // TODO: Set speed/duplex/pauses
718 static void rtl93xx_phylink_mac_link_up(struct dsa_switch
*ds
, int port
,
720 phy_interface_t interface
,
721 struct phy_device
*phydev
,
722 int speed
, int duplex
,
723 bool tx_pause
, bool rx_pause
)
725 struct rtl838x_switch_priv
*priv
= ds
->priv
;
727 /* Restart TX/RX to port */
728 sw_w32_mask(0, 0x3, priv
->r
->mac_port_ctrl(port
));
729 // TODO: Set speed/duplex/pauses
732 static void rtl83xx_get_strings(struct dsa_switch
*ds
,
733 int port
, u32 stringset
, u8
*data
)
737 if (stringset
!= ETH_SS_STATS
)
740 for (i
= 0; i
< ARRAY_SIZE(rtl83xx_mib
); i
++)
741 strncpy(data
+ i
* ETH_GSTRING_LEN
, rtl83xx_mib
[i
].name
,
745 static void rtl83xx_get_ethtool_stats(struct dsa_switch
*ds
, int port
,
748 struct rtl838x_switch_priv
*priv
= ds
->priv
;
749 const struct rtl83xx_mib_desc
*mib
;
753 for (i
= 0; i
< ARRAY_SIZE(rtl83xx_mib
); i
++) {
754 mib
= &rtl83xx_mib
[i
];
756 data
[i
] = sw_r32(priv
->r
->stat_port_std_mib
+ (port
<< 8) + 252 - mib
->offset
);
757 if (mib
->size
== 2) {
758 h
= sw_r32(priv
->r
->stat_port_std_mib
+ (port
<< 8) + 248 - mib
->offset
);
764 static int rtl83xx_get_sset_count(struct dsa_switch
*ds
, int port
, int sset
)
766 if (sset
!= ETH_SS_STATS
)
769 return ARRAY_SIZE(rtl83xx_mib
);
772 static int rtl83xx_port_enable(struct dsa_switch
*ds
, int port
,
773 struct phy_device
*phydev
)
775 struct rtl838x_switch_priv
*priv
= ds
->priv
;
778 pr_debug("%s: %x %d", __func__
, (u32
) priv
, port
);
779 priv
->ports
[port
].enable
= true;
781 /* enable inner tagging on egress, do not keep any tags */
782 if (priv
->family_id
== RTL9310_FAMILY_ID
)
783 sw_w32(BIT(4), priv
->r
->vlan_port_tag_sts_ctrl
+ (port
<< 2));
785 sw_w32(1, priv
->r
->vlan_port_tag_sts_ctrl
+ (port
<< 2));
787 if (dsa_is_cpu_port(ds
, port
))
790 /* add port to switch mask of CPU_PORT */
791 priv
->r
->traffic_enable(priv
->cpu_port
, port
);
793 /* add all other ports in the same bridge to switch mask of port */
794 v
= priv
->r
->traffic_get(port
);
795 v
|= priv
->ports
[port
].pm
;
796 priv
->r
->traffic_set(port
, v
);
798 // TODO: Figure out if this is necessary
799 if (priv
->family_id
== RTL9300_FAMILY_ID
) {
800 sw_w32_mask(0, BIT(port
), RTL930X_L2_PORT_SABLK_CTRL
);
801 sw_w32_mask(0, BIT(port
), RTL930X_L2_PORT_DABLK_CTRL
);
804 priv
->ports
[port
].sds_num
= rtl93xx_get_sds(phydev
);
809 static void rtl83xx_port_disable(struct dsa_switch
*ds
, int port
)
811 struct rtl838x_switch_priv
*priv
= ds
->priv
;
814 pr_debug("%s %x: %d", __func__
, (u32
)priv
, port
);
815 /* you can only disable user ports */
816 if (!dsa_is_user_port(ds
, port
))
819 // BUG: This does not work on RTL931X
820 /* remove port from switch mask of CPU_PORT */
821 priv
->r
->traffic_disable(priv
->cpu_port
, port
);
823 /* remove all other ports in the same bridge from switch mask of port */
824 v
= priv
->r
->traffic_get(port
);
825 v
&= ~priv
->ports
[port
].pm
;
826 priv
->r
->traffic_set(port
, v
);
828 priv
->ports
[port
].enable
= false;
831 static int rtl83xx_set_mac_eee(struct dsa_switch
*ds
, int port
,
832 struct ethtool_eee
*e
)
834 struct rtl838x_switch_priv
*priv
= ds
->priv
;
836 if (e
->eee_enabled
&& !priv
->eee_enabled
) {
837 pr_info("Globally enabling EEE\n");
838 priv
->r
->init_eee(priv
, true);
841 priv
->r
->port_eee_set(priv
, port
, e
->eee_enabled
);
844 pr_info("Enabled EEE for port %d\n", port
);
846 pr_info("Disabled EEE for port %d\n", port
);
850 static int rtl83xx_get_mac_eee(struct dsa_switch
*ds
, int port
,
851 struct ethtool_eee
*e
)
853 struct rtl838x_switch_priv
*priv
= ds
->priv
;
855 e
->supported
= SUPPORTED_100baseT_Full
| SUPPORTED_1000baseT_Full
;
857 priv
->r
->eee_port_ability(priv
, e
, port
);
859 e
->eee_enabled
= priv
->ports
[port
].eee_enabled
;
861 e
->eee_active
= !!(e
->advertised
& e
->lp_advertised
);
866 static int rtl93xx_get_mac_eee(struct dsa_switch
*ds
, int port
,
867 struct ethtool_eee
*e
)
869 struct rtl838x_switch_priv
*priv
= ds
->priv
;
871 e
->supported
= SUPPORTED_100baseT_Full
| SUPPORTED_1000baseT_Full
872 | SUPPORTED_2500baseX_Full
;
874 priv
->r
->eee_port_ability(priv
, e
, port
);
876 e
->eee_enabled
= priv
->ports
[port
].eee_enabled
;
878 e
->eee_active
= !!(e
->advertised
& e
->lp_advertised
);
884 * Set Switch L2 Aging time, t is time in milliseconds
885 * t = 0: aging is disabled
887 static int rtl83xx_set_l2aging(struct dsa_switch
*ds
, u32 t
)
889 struct rtl838x_switch_priv
*priv
= ds
->priv
;
890 int t_max
= priv
->family_id
== RTL8380_FAMILY_ID
? 0x7fffff : 0x1FFFFF;
892 /* Convert time in mseconds to internal value */
893 if (t
> 0x10000000) { /* Set to maximum */
896 if (priv
->family_id
== RTL8380_FAMILY_ID
)
897 t
= ((t
* 625) / 1000 + 127) / 128;
901 sw_w32(t
, priv
->r
->l2_ctrl_1
);
905 static int rtl83xx_port_bridge_join(struct dsa_switch
*ds
, int port
,
906 struct net_device
*bridge
)
908 struct rtl838x_switch_priv
*priv
= ds
->priv
;
909 u64 port_bitmap
= BIT_ULL(priv
->cpu_port
), v
;
912 pr_debug("%s %x: %d %llx", __func__
, (u32
)priv
, port
, port_bitmap
);
913 mutex_lock(&priv
->reg_mutex
);
914 for (i
= 0; i
< ds
->num_ports
; i
++) {
915 /* Add this port to the port matrix of the other ports in the
916 * same bridge. If the port is disabled, port matrix is kept
917 * and not being setup until the port becomes enabled.
919 if (dsa_is_user_port(ds
, i
) && i
!= port
) {
920 if (dsa_to_port(ds
, i
)->bridge_dev
!= bridge
)
922 if (priv
->ports
[i
].enable
)
923 priv
->r
->traffic_enable(i
, port
);
925 priv
->ports
[i
].pm
|= BIT_ULL(port
);
926 port_bitmap
|= BIT_ULL(i
);
930 /* Add all other ports to this port matrix. */
931 if (priv
->ports
[port
].enable
) {
932 priv
->r
->traffic_enable(priv
->cpu_port
, port
);
933 v
= priv
->r
->traffic_get(port
);
935 priv
->r
->traffic_set(port
, v
);
937 priv
->ports
[port
].pm
|= port_bitmap
;
938 mutex_unlock(&priv
->reg_mutex
);
943 static void rtl83xx_port_bridge_leave(struct dsa_switch
*ds
, int port
,
944 struct net_device
*bridge
)
946 struct rtl838x_switch_priv
*priv
= ds
->priv
;
947 u64 port_bitmap
= BIT_ULL(priv
->cpu_port
), v
;
950 pr_debug("%s %x: %d", __func__
, (u32
)priv
, port
);
951 mutex_lock(&priv
->reg_mutex
);
952 for (i
= 0; i
< ds
->num_ports
; i
++) {
953 /* Remove this port from the port matrix of the other ports
954 * in the same bridge. If the port is disabled, port matrix
955 * is kept and not being setup until the port becomes enabled.
956 * And the other port's port matrix cannot be broken when the
957 * other port is still a VLAN-aware port.
959 if (dsa_is_user_port(ds
, i
) && i
!= port
) {
960 if (dsa_to_port(ds
, i
)->bridge_dev
!= bridge
)
962 if (priv
->ports
[i
].enable
)
963 priv
->r
->traffic_disable(i
, port
);
965 priv
->ports
[i
].pm
|= BIT_ULL(port
);
966 port_bitmap
&= ~BIT_ULL(i
);
970 /* Add all other ports to this port matrix. */
971 if (priv
->ports
[port
].enable
) {
972 v
= priv
->r
->traffic_get(port
);
974 priv
->r
->traffic_set(port
, v
);
976 priv
->ports
[port
].pm
&= ~port_bitmap
;
978 mutex_unlock(&priv
->reg_mutex
);
981 void rtl83xx_port_stp_state_set(struct dsa_switch
*ds
, int port
, u8 state
)
987 struct rtl838x_switch_priv
*priv
= ds
->priv
;
988 int n
= priv
->port_width
<< 1;
990 /* Ports above or equal CPU port can never be configured */
991 if (port
>= priv
->cpu_port
)
994 mutex_lock(&priv
->reg_mutex
);
996 /* For the RTL839x and following, the bits are left-aligned, 838x and 930x
997 * have 64 bit fields, 839x and 931x have 128 bit fields
999 if (priv
->family_id
== RTL8390_FAMILY_ID
)
1001 if (priv
->family_id
== RTL9300_FAMILY_ID
)
1003 if (priv
->family_id
== RTL9310_FAMILY_ID
)
1006 index
= n
- (pos
>> 4) - 1;
1007 bit
= (pos
<< 1) % 32;
1009 priv
->r
->stp_get(priv
, msti
, port_state
);
1011 pr_debug("Current state, port %d: %d\n", port
, (port_state
[index
] >> bit
) & 3);
1012 port_state
[index
] &= ~(3 << bit
);
1015 case BR_STATE_DISABLED
: /* 0 */
1016 port_state
[index
] |= (0 << bit
);
1018 case BR_STATE_BLOCKING
: /* 4 */
1019 case BR_STATE_LISTENING
: /* 1 */
1020 port_state
[index
] |= (1 << bit
);
1022 case BR_STATE_LEARNING
: /* 2 */
1023 port_state
[index
] |= (2 << bit
);
1025 case BR_STATE_FORWARDING
: /* 3*/
1026 port_state
[index
] |= (3 << bit
);
1031 priv
->r
->stp_set(priv
, msti
, port_state
);
1033 mutex_unlock(&priv
->reg_mutex
);
1036 void rtl83xx_fast_age(struct dsa_switch
*ds
, int port
)
1038 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1039 int s
= priv
->family_id
== RTL8390_FAMILY_ID
? 2 : 0;
1041 pr_debug("FAST AGE port %d\n", port
);
1042 mutex_lock(&priv
->reg_mutex
);
1043 /* RTL838X_L2_TBL_FLUSH_CTRL register bits, 839x has 1 bit larger
1045 * 0-4: Replacing port
1046 * 5-9: Flushed/replaced port
1048 * 22: Entry types: 1: dynamic, 0: also static
1049 * 23: Match flush port
1051 * 25: Flush (0) or replace (1) L2 entries
1052 * 26: Status of action (1: Start, 0: Done)
1054 sw_w32(1 << (26 + s
) | 1 << (23 + s
) | port
<< (5 + (s
/ 2)), priv
->r
->l2_tbl_flush_ctrl
);
1056 do { } while (sw_r32(priv
->r
->l2_tbl_flush_ctrl
) & BIT(26 + s
));
1058 mutex_unlock(&priv
->reg_mutex
);
1061 void rtl930x_fast_age(struct dsa_switch
*ds
, int port
)
1063 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1065 pr_debug("FAST AGE port %d\n", port
);
1066 mutex_lock(&priv
->reg_mutex
);
1067 sw_w32(port
<< 11, RTL930X_L2_TBL_FLUSH_CTRL
+ 4);
1069 sw_w32(BIT(26) | BIT(30), RTL930X_L2_TBL_FLUSH_CTRL
);
1071 do { } while (sw_r32(priv
->r
->l2_tbl_flush_ctrl
) & BIT(30));
1073 mutex_unlock(&priv
->reg_mutex
);
1076 static int rtl83xx_vlan_filtering(struct dsa_switch
*ds
, int port
,
1077 bool vlan_filtering
,
1078 struct switchdev_trans
*trans
)
1080 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1082 pr_debug("%s: port %d\n", __func__
, port
);
1083 mutex_lock(&priv
->reg_mutex
);
1085 if (vlan_filtering
) {
1086 /* Enable ingress and egress filtering
1087 * The VLAN_PORT_IGR_FILTER register uses 2 bits for each port to define
1088 * the filter action:
1091 * 2: Trap packet to CPU port
1092 * The Egress filter used 1 bit per state (0: DISABLED, 1: ENABLED)
1094 if (port
!= priv
->cpu_port
)
1095 sw_w32_mask(0b10 << ((port
% 16) << 1), 0b01 << ((port
% 16) << 1),
1096 priv
->r
->vlan_port_igr_filter
+ ((port
>> 4) << 2));
1097 sw_w32_mask(0, BIT(port
% 32), priv
->r
->vlan_port_egr_filter
+ ((port
>> 5) << 2));
1099 /* Disable ingress and egress filtering */
1100 if (port
!= priv
->cpu_port
)
1101 sw_w32_mask(0b11 << ((port
% 16) << 1), 0,
1102 priv
->r
->vlan_port_igr_filter
+ ((port
>> 4) << 2));
1103 sw_w32_mask(BIT(port
% 32), 0, priv
->r
->vlan_port_egr_filter
+ ((port
>> 5) << 2));
1106 /* Do we need to do something to the CPU-Port, too? */
1107 mutex_unlock(&priv
->reg_mutex
);
1112 static int rtl83xx_vlan_prepare(struct dsa_switch
*ds
, int port
,
1113 const struct switchdev_obj_port_vlan
*vlan
)
1115 struct rtl838x_vlan_info info
;
1116 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1118 priv
->r
->vlan_tables_read(0, &info
);
1120 pr_debug("VLAN 0: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
1121 info
.tagged_ports
, info
.untagged_ports
, info
.profile_id
,
1122 info
.hash_mc_fid
, info
.hash_uc_fid
, info
.fid
);
1124 priv
->r
->vlan_tables_read(1, &info
);
1125 pr_debug("VLAN 1: Tagged ports %llx, untag %llx, profile %d, MC# %d, UC# %d, FID %x\n",
1126 info
.tagged_ports
, info
.untagged_ports
, info
.profile_id
,
1127 info
.hash_mc_fid
, info
.hash_uc_fid
, info
.fid
);
1128 priv
->r
->vlan_set_untagged(1, info
.untagged_ports
);
1129 pr_debug("SET: Untagged ports, VLAN %d: %llx\n", 1, info
.untagged_ports
);
1131 priv
->r
->vlan_set_tagged(1, &info
);
1132 pr_debug("SET: Tagged ports, VLAN %d: %llx\n", 1, info
.tagged_ports
);
1134 mutex_unlock(&priv
->reg_mutex
);
1138 static void rtl83xx_vlan_add(struct dsa_switch
*ds
, int port
,
1139 const struct switchdev_obj_port_vlan
*vlan
)
1141 struct rtl838x_vlan_info info
;
1142 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1145 pr_debug("%s port %d, vid_end %d, vid_end %d, flags %x\n", __func__
,
1146 port
, vlan
->vid_begin
, vlan
->vid_end
, vlan
->flags
);
1148 if (vlan
->vid_begin
> 4095 || vlan
->vid_end
> 4095) {
1149 dev_err(priv
->dev
, "VLAN out of range: %d - %d",
1150 vlan
->vid_begin
, vlan
->vid_end
);
1154 mutex_lock(&priv
->reg_mutex
);
1156 if (vlan
->flags
& BRIDGE_VLAN_INFO_PVID
) {
1157 for (v
= vlan
->vid_begin
; v
<= vlan
->vid_end
; v
++) {
1160 /* Set both inner and outer PVID of the port */
1161 sw_w32((v
<< 16) | v
<< 2, priv
->r
->vlan_port_pb
+ (port
<< 2));
1162 priv
->ports
[port
].pvid
= vlan
->vid_end
;
1166 for (v
= vlan
->vid_begin
; v
<= vlan
->vid_end
; v
++) {
1167 /* Get port memberships of this vlan */
1168 priv
->r
->vlan_tables_read(v
, &info
);
1171 if (!info
.tagged_ports
) {
1173 info
.hash_mc_fid
= false;
1174 info
.hash_uc_fid
= false;
1175 info
.profile_id
= 0;
1178 /* sanitize untagged_ports - must be a subset */
1179 if (info
.untagged_ports
& ~info
.tagged_ports
)
1180 info
.untagged_ports
= 0;
1182 info
.tagged_ports
|= BIT_ULL(port
);
1183 if (vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
)
1184 info
.untagged_ports
|= BIT_ULL(port
);
1186 priv
->r
->vlan_set_untagged(v
, info
.untagged_ports
);
1187 pr_debug("Untagged ports, VLAN %d: %llx\n", v
, info
.untagged_ports
);
1189 priv
->r
->vlan_set_tagged(v
, &info
);
1190 pr_debug("Tagged ports, VLAN %d: %llx\n", v
, info
.tagged_ports
);
1193 mutex_unlock(&priv
->reg_mutex
);
1196 static int rtl83xx_vlan_del(struct dsa_switch
*ds
, int port
,
1197 const struct switchdev_obj_port_vlan
*vlan
)
1199 struct rtl838x_vlan_info info
;
1200 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1204 pr_debug("%s: port %d, vid_end %d, vid_end %d, flags %x\n", __func__
,
1205 port
, vlan
->vid_begin
, vlan
->vid_end
, vlan
->flags
);
1207 if (vlan
->vid_begin
> 4095 || vlan
->vid_end
> 4095) {
1208 dev_err(priv
->dev
, "VLAN out of range: %d - %d",
1209 vlan
->vid_begin
, vlan
->vid_end
);
1213 mutex_lock(&priv
->reg_mutex
);
1214 pvid
= priv
->ports
[port
].pvid
;
1216 for (v
= vlan
->vid_begin
; v
<= vlan
->vid_end
; v
++) {
1217 /* Reset to default if removing the current PVID */
1219 sw_w32(0, priv
->r
->vlan_port_pb
+ (port
<< 2));
1221 /* Get port memberships of this vlan */
1222 priv
->r
->vlan_tables_read(v
, &info
);
1224 /* remove port from both tables */
1225 info
.untagged_ports
&= (~BIT_ULL(port
));
1226 info
.tagged_ports
&= (~BIT_ULL(port
));
1228 priv
->r
->vlan_set_untagged(v
, info
.untagged_ports
);
1229 pr_debug("Untagged ports, VLAN %d: %llx\n", v
, info
.untagged_ports
);
1231 priv
->r
->vlan_set_tagged(v
, &info
);
1232 pr_debug("Tagged ports, VLAN %d: %llx\n", v
, info
.tagged_ports
);
1234 mutex_unlock(&priv
->reg_mutex
);
1239 static void dump_l2_entry(struct rtl838x_l2_entry
*e
)
1241 pr_info("MAC: %02x:%02x:%02x:%02x:%02x:%02x vid: %d, rvid: %d, port: %d, valid: %d\n",
1242 e
->mac
[0], e
->mac
[1], e
->mac
[2], e
->mac
[3], e
->mac
[4], e
->mac
[5],
1243 e
->vid
, e
->rvid
, e
->port
, e
->valid
);
1245 if (e
->type
!= L2_MULTICAST
) {
1246 pr_info("Type: %d, is_static: %d, is_ip_mc: %d, is_ipv6_mc: %d, block_da: %d\n",
1247 e
->type
, e
->is_static
, e
->is_ip_mc
, e
->is_ipv6_mc
, e
->block_da
);
1248 pr_info(" block_sa: %d, susp: %d, nh: %d, age: %d, is_trunk: %d, trunk: %d\n",
1249 e
->block_sa
, e
->suspended
, e
->next_hop
, e
->age
, e
->is_trunk
, e
->trunk
);
1251 if (e
->type
== L2_MULTICAST
)
1252 pr_info(" L2_MULTICAST mc_portmask_index: %d\n", e
->mc_portmask_index
);
1253 if (e
->is_ip_mc
|| e
->is_ipv6_mc
)
1254 pr_info(" mc_portmask_index: %d, mc_gip: %d, mc_sip: %d\n",
1255 e
->mc_portmask_index
, e
->mc_gip
, e
->mc_sip
);
1256 pr_info(" stack_dev: %d\n", e
->stack_dev
);
1258 pr_info(" nh_route_id: %d\n", e
->nh_route_id
);
1261 static void rtl83xx_setup_l2_uc_entry(struct rtl838x_l2_entry
*e
, int port
, int vid
, u64 mac
)
1263 e
->is_ip_mc
= e
->is_ipv6_mc
= false;
1268 u64_to_ether_addr(mac
, e
->mac
);
1271 static void rtl83xx_setup_l2_mc_entry(struct rtl838x_switch_priv
*priv
,
1272 struct rtl838x_l2_entry
*e
, int vid
, u64 mac
, int mc_group
)
1274 e
->is_ip_mc
= e
->is_ipv6_mc
= false;
1276 e
->mc_portmask_index
= mc_group
;
1277 e
->type
= L2_MULTICAST
;
1278 e
->rvid
= e
->vid
= vid
;
1279 pr_debug("%s: vid: %d, rvid: %d\n", __func__
, e
->vid
, e
->rvid
);
1280 u64_to_ether_addr(mac
, e
->mac
);
1284 * Uses the seed to identify a hash bucket in the L2 using the derived hash key and then loops
1285 * over the entries in the bucket until either a matching entry is found or an empty slot
1286 * Returns the filled in rtl838x_l2_entry and the index in the bucket when an entry was found
1287 * when an empty slot was found and must exist is false, the index of the slot is returned
1288 * when no slots are available returns -1
1290 static int rtl83xx_find_l2_hash_entry(struct rtl838x_switch_priv
*priv
, u64 seed
,
1291 bool must_exist
, struct rtl838x_l2_entry
*e
)
1294 u32 key
= priv
->r
->l2_hash_key(priv
, seed
);
1297 pr_debug("%s: using key %x, for seed %016llx\n", __func__
, key
, seed
);
1298 // Loop over all entries in the hash-bucket and over the second block on 93xx SoCs
1299 for (i
= 0; i
< priv
->l2_bucket_size
; i
++) {
1300 entry
= priv
->r
->read_l2_entry_using_hash(key
, i
, e
);
1301 pr_debug("valid %d, mac %016llx\n", e
->valid
, ether_addr_to_u64(&e
->mac
[0]));
1302 if (must_exist
&& !e
->valid
)
1304 if (!e
->valid
|| ((entry
& 0x0fffffffffffffffULL
) == seed
)) {
1305 idx
= i
> 3 ? ((key
>> 14) & 0xffff) | i
>> 1 : ((key
<< 2) | i
) & 0xffff;
1314 * Uses the seed to identify an entry in the CAM by looping over all its entries
1315 * Returns the filled in rtl838x_l2_entry and the index in the CAM when an entry was found
1316 * when an empty slot was found the index of the slot is returned
1317 * when no slots are available returns -1
1319 static int rtl83xx_find_l2_cam_entry(struct rtl838x_switch_priv
*priv
, u64 seed
,
1320 bool must_exist
, struct rtl838x_l2_entry
*e
)
1325 for (i
= 0; i
< 64; i
++) {
1326 entry
= priv
->r
->read_cam(i
, e
);
1327 if (!must_exist
&& !e
->valid
) {
1328 if (idx
< 0) /* First empty entry? */
1331 } else if ((entry
& 0x0fffffffffffffffULL
) == seed
) {
1332 pr_debug("Found entry in CAM\n");
1340 static int rtl83xx_port_fdb_add(struct dsa_switch
*ds
, int port
,
1341 const unsigned char *addr
, u16 vid
)
1343 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1344 u64 mac
= ether_addr_to_u64(addr
);
1345 struct rtl838x_l2_entry e
;
1347 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1349 mutex_lock(&priv
->reg_mutex
);
1351 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, false, &e
);
1353 // Found an existing or empty entry
1355 rtl83xx_setup_l2_uc_entry(&e
, port
, vid
, mac
);
1356 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1360 // Hash buckets full, try CAM
1361 rtl83xx_find_l2_cam_entry(priv
, seed
, false, &e
);
1364 rtl83xx_setup_l2_uc_entry(&e
, port
, vid
, mac
);
1365 priv
->r
->write_cam(idx
, &e
);
1371 mutex_unlock(&priv
->reg_mutex
);
1375 static int rtl83xx_port_fdb_del(struct dsa_switch
*ds
, int port
,
1376 const unsigned char *addr
, u16 vid
)
1378 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1379 u64 mac
= ether_addr_to_u64(addr
);
1380 struct rtl838x_l2_entry e
;
1382 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1384 pr_info("In %s, mac %llx, vid: %d\n", __func__
, mac
, vid
);
1385 mutex_lock(&priv
->reg_mutex
);
1387 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, true, &e
);
1389 pr_info("Found entry index %d, key %d and bucket %d\n", idx
, idx
>> 2, idx
& 3);
1393 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1397 /* Check CAM for spillover from hash buckets */
1398 rtl83xx_find_l2_cam_entry(priv
, seed
, true, &e
);
1402 priv
->r
->write_cam(idx
, &e
);
1407 mutex_unlock(&priv
->reg_mutex
);
1411 static int rtl83xx_port_fdb_dump(struct dsa_switch
*ds
, int port
,
1412 dsa_fdb_dump_cb_t
*cb
, void *data
)
1414 struct rtl838x_l2_entry e
;
1415 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1420 mutex_lock(&priv
->reg_mutex
);
1422 for (i
= 0; i
< priv
->fib_entries
; i
++) {
1423 priv
->r
->read_l2_entry_using_hash(i
>> 2, i
& 0x3, &e
);
1428 if (e
.port
== port
|| e
.port
== RTL930X_PORT_IGNORE
) {
1432 fid
= ((i
>> 2) & 0x3ff) | (e
.rvid
& ~0x3ff);
1433 mac
= ether_addr_to_u64(&e
.mac
[0]);
1434 pkey
= priv
->r
->l2_hash_key(priv
, priv
->r
->l2_hash_seed(mac
, fid
));
1435 fid
= (pkey
& 0x3ff) | (fid
& ~0x3ff);
1436 pr_info("-> index %d, key %x, bucket %d, dmac %016llx, fid: %x rvid: %x\n",
1437 i
, i
>> 2, i
& 0x3, mac
, fid
, e
.rvid
);
1439 seed
= priv
->r
->l2_hash_seed(mac
, e
.rvid
);
1440 key
= priv
->r
->l2_hash_key(priv
, seed
);
1441 pr_info("seed: %016llx, key based on rvid: %08x\n", seed
, key
);
1442 cb(e
.mac
, e
.vid
, e
.is_static
, data
);
1444 if (e
.type
== L2_MULTICAST
) {
1445 u64 portmask
= priv
->r
->read_mcast_pmask(e
.mc_portmask_index
);
1447 if (portmask
& BIT_ULL(port
)) {
1449 pr_info(" PM: %016llx\n", portmask
);
1454 for (i
= 0; i
< 64; i
++) {
1455 priv
->r
->read_cam(i
, &e
);
1461 cb(e
.mac
, e
.vid
, e
.is_static
, data
);
1464 mutex_unlock(&priv
->reg_mutex
);
1468 static int rtl83xx_port_mdb_prepare(struct dsa_switch
*ds
, int port
,
1469 const struct switchdev_obj_port_mdb
*mdb
)
1471 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1473 if (priv
->id
>= 0x9300)
1479 static int rtl83xx_mc_group_alloc(struct rtl838x_switch_priv
*priv
, int port
)
1481 int mc_group
= find_first_zero_bit(priv
->mc_group_bm
, MAX_MC_GROUPS
- 1);
1484 if (mc_group
>= MAX_MC_GROUPS
- 1)
1487 pr_debug("Using MC group %d\n", mc_group
);
1488 set_bit(mc_group
, priv
->mc_group_bm
);
1489 mc_group
++; // We cannot use group 0, as this is used for lookup miss flooding
1490 portmask
= BIT_ULL(port
);
1491 priv
->r
->write_mcast_pmask(mc_group
, portmask
);
1496 static u64
rtl83xx_mc_group_add_port(struct rtl838x_switch_priv
*priv
, int mc_group
, int port
)
1498 u64 portmask
= priv
->r
->read_mcast_pmask(mc_group
);
1500 portmask
|= BIT_ULL(port
);
1501 priv
->r
->write_mcast_pmask(mc_group
, portmask
);
1506 static u64
rtl83xx_mc_group_del_port(struct rtl838x_switch_priv
*priv
, int mc_group
, int port
)
1508 u64 portmask
= priv
->r
->read_mcast_pmask(mc_group
);
1510 portmask
&= ~BIT_ULL(port
);
1511 priv
->r
->write_mcast_pmask(mc_group
, portmask
);
1513 clear_bit(mc_group
, priv
->mc_group_bm
);
1518 static void rtl83xx_port_mdb_add(struct dsa_switch
*ds
, int port
,
1519 const struct switchdev_obj_port_mdb
*mdb
)
1521 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1522 u64 mac
= ether_addr_to_u64(mdb
->addr
);
1523 struct rtl838x_l2_entry e
;
1526 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1529 pr_debug("In %s port %d, mac %llx, vid: %d\n", __func__
, port
, mac
, vid
);
1530 mutex_lock(&priv
->reg_mutex
);
1532 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, false, &e
);
1534 // Found an existing or empty entry
1537 pr_debug("Found an existing entry %016llx, mc_group %d\n",
1538 ether_addr_to_u64(e
.mac
), e
.mc_portmask_index
);
1539 rtl83xx_mc_group_add_port(priv
, e
.mc_portmask_index
, port
);
1541 pr_debug("New entry for seed %016llx\n", seed
);
1542 mc_group
= rtl83xx_mc_group_alloc(priv
, port
);
1547 rtl83xx_setup_l2_mc_entry(priv
, &e
, vid
, mac
, mc_group
);
1548 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1553 // Hash buckets full, try CAM
1554 rtl83xx_find_l2_cam_entry(priv
, seed
, false, &e
);
1558 pr_debug("Found existing CAM entry %016llx, mc_group %d\n",
1559 ether_addr_to_u64(e
.mac
), e
.mc_portmask_index
);
1560 rtl83xx_mc_group_add_port(priv
, e
.mc_portmask_index
, port
);
1562 pr_debug("New entry\n");
1563 mc_group
= rtl83xx_mc_group_alloc(priv
, port
);
1568 rtl83xx_setup_l2_mc_entry(priv
, &e
, vid
, mac
, mc_group
);
1569 priv
->r
->write_cam(idx
, &e
);
1576 mutex_unlock(&priv
->reg_mutex
);
1578 dev_err(ds
->dev
, "failed to add MDB entry\n");
1581 int rtl83xx_port_mdb_del(struct dsa_switch
*ds
, int port
,
1582 const struct switchdev_obj_port_mdb
*mdb
)
1584 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1585 u64 mac
= ether_addr_to_u64(mdb
->addr
);
1586 struct rtl838x_l2_entry e
;
1589 u64 seed
= priv
->r
->l2_hash_seed(mac
, vid
);
1592 pr_debug("In %s, port %d, mac %llx, vid: %d\n", __func__
, port
, mac
, vid
);
1593 mutex_lock(&priv
->reg_mutex
);
1595 idx
= rtl83xx_find_l2_hash_entry(priv
, seed
, true, &e
);
1597 pr_debug("Found entry index %d, key %d and bucket %d\n", idx
, idx
>> 2, idx
& 3);
1599 portmask
= rtl83xx_mc_group_del_port(priv
, e
.mc_portmask_index
, port
);
1602 // dump_l2_entry(&e);
1603 priv
->r
->write_l2_entry_using_hash(idx
>> 2, idx
& 0x3, &e
);
1608 /* Check CAM for spillover from hash buckets */
1609 rtl83xx_find_l2_cam_entry(priv
, seed
, true, &e
);
1612 portmask
= rtl83xx_mc_group_del_port(priv
, e
.mc_portmask_index
, port
);
1615 // dump_l2_entry(&e);
1616 priv
->r
->write_cam(idx
, &e
);
1620 // TODO: Re-enable with a newer kernel: err = -ENOENT;
1622 mutex_unlock(&priv
->reg_mutex
);
1626 static int rtl83xx_port_mirror_add(struct dsa_switch
*ds
, int port
,
1627 struct dsa_mall_mirror_tc_entry
*mirror
,
1630 /* We support 4 mirror groups, one destination port per group */
1632 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1633 int ctrl_reg
, dpm_reg
, spm_reg
;
1635 pr_debug("In %s\n", __func__
);
1637 for (group
= 0; group
< 4; group
++) {
1638 if (priv
->mirror_group_ports
[group
] == mirror
->to_local_port
)
1642 for (group
= 0; group
< 4; group
++) {
1643 if (priv
->mirror_group_ports
[group
] < 0)
1651 ctrl_reg
= priv
->r
->mir_ctrl
+ group
* 4;
1652 dpm_reg
= priv
->r
->mir_dpm
+ group
* 4 * priv
->port_width
;
1653 spm_reg
= priv
->r
->mir_spm
+ group
* 4 * priv
->port_width
;
1655 pr_debug("Using group %d\n", group
);
1656 mutex_lock(&priv
->reg_mutex
);
1658 if (priv
->family_id
== RTL8380_FAMILY_ID
) {
1659 /* Enable mirroring to port across VLANs (bit 11) */
1660 sw_w32(1 << 11 | (mirror
->to_local_port
<< 4) | 1, ctrl_reg
);
1662 /* Enable mirroring to destination port */
1663 sw_w32((mirror
->to_local_port
<< 4) | 1, ctrl_reg
);
1666 if (ingress
&& (priv
->r
->get_port_reg_be(spm_reg
) & (1ULL << port
))) {
1667 mutex_unlock(&priv
->reg_mutex
);
1670 if ((!ingress
) && (priv
->r
->get_port_reg_be(dpm_reg
) & (1ULL << port
))) {
1671 mutex_unlock(&priv
->reg_mutex
);
1676 priv
->r
->mask_port_reg_be(0, 1ULL << port
, spm_reg
);
1678 priv
->r
->mask_port_reg_be(0, 1ULL << port
, dpm_reg
);
1680 priv
->mirror_group_ports
[group
] = mirror
->to_local_port
;
1681 mutex_unlock(&priv
->reg_mutex
);
1685 static void rtl83xx_port_mirror_del(struct dsa_switch
*ds
, int port
,
1686 struct dsa_mall_mirror_tc_entry
*mirror
)
1689 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1690 int ctrl_reg
, dpm_reg
, spm_reg
;
1692 pr_debug("In %s\n", __func__
);
1693 for (group
= 0; group
< 4; group
++) {
1694 if (priv
->mirror_group_ports
[group
] == mirror
->to_local_port
)
1700 ctrl_reg
= priv
->r
->mir_ctrl
+ group
* 4;
1701 dpm_reg
= priv
->r
->mir_dpm
+ group
* 4 * priv
->port_width
;
1702 spm_reg
= priv
->r
->mir_spm
+ group
* 4 * priv
->port_width
;
1704 mutex_lock(&priv
->reg_mutex
);
1705 if (mirror
->ingress
) {
1706 /* Ingress, clear source port matrix */
1707 priv
->r
->mask_port_reg_be(1ULL << port
, 0, spm_reg
);
1709 /* Egress, clear destination port matrix */
1710 priv
->r
->mask_port_reg_be(1ULL << port
, 0, dpm_reg
);
1713 if (!(sw_r32(spm_reg
) || sw_r32(dpm_reg
))) {
1714 priv
->mirror_group_ports
[group
] = -1;
1715 sw_w32(0, ctrl_reg
);
1718 mutex_unlock(&priv
->reg_mutex
);
1721 int dsa_phy_read(struct dsa_switch
*ds
, int phy_addr
, int phy_reg
)
1725 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1727 if (phy_addr
>= 24 && phy_addr
<= 27
1728 && priv
->ports
[24].phy
== PHY_RTL838X_SDS
) {
1731 val
= sw_r32(RTL838X_SDS4_FIB_REG0
+ offset
+ (phy_reg
<< 2)) & 0xffff;
1735 read_phy(phy_addr
, 0, phy_reg
, &val
);
1739 int dsa_phy_write(struct dsa_switch
*ds
, int phy_addr
, int phy_reg
, u16 val
)
1742 struct rtl838x_switch_priv
*priv
= ds
->priv
;
1744 if (phy_addr
>= 24 && phy_addr
<= 27
1745 && priv
->ports
[24].phy
== PHY_RTL838X_SDS
) {
1748 sw_w32(val
, RTL838X_SDS4_FIB_REG0
+ offset
+ (phy_reg
<< 2));
1751 return write_phy(phy_addr
, 0, phy_reg
, val
);
1754 const struct dsa_switch_ops rtl83xx_switch_ops
= {
1755 .get_tag_protocol
= rtl83xx_get_tag_protocol
,
1756 .setup
= rtl83xx_setup
,
1758 .phy_read
= dsa_phy_read
,
1759 .phy_write
= dsa_phy_write
,
1761 .phylink_validate
= rtl83xx_phylink_validate
,
1762 .phylink_mac_link_state
= rtl83xx_phylink_mac_link_state
,
1763 .phylink_mac_config
= rtl83xx_phylink_mac_config
,
1764 .phylink_mac_link_down
= rtl83xx_phylink_mac_link_down
,
1765 .phylink_mac_link_up
= rtl83xx_phylink_mac_link_up
,
1767 .get_strings
= rtl83xx_get_strings
,
1768 .get_ethtool_stats
= rtl83xx_get_ethtool_stats
,
1769 .get_sset_count
= rtl83xx_get_sset_count
,
1771 .port_enable
= rtl83xx_port_enable
,
1772 .port_disable
= rtl83xx_port_disable
,
1774 .get_mac_eee
= rtl83xx_get_mac_eee
,
1775 .set_mac_eee
= rtl83xx_set_mac_eee
,
1777 .set_ageing_time
= rtl83xx_set_l2aging
,
1778 .port_bridge_join
= rtl83xx_port_bridge_join
,
1779 .port_bridge_leave
= rtl83xx_port_bridge_leave
,
1780 .port_stp_state_set
= rtl83xx_port_stp_state_set
,
1781 .port_fast_age
= rtl83xx_fast_age
,
1783 .port_vlan_filtering
= rtl83xx_vlan_filtering
,
1784 .port_vlan_prepare
= rtl83xx_vlan_prepare
,
1785 .port_vlan_add
= rtl83xx_vlan_add
,
1786 .port_vlan_del
= rtl83xx_vlan_del
,
1788 .port_fdb_add
= rtl83xx_port_fdb_add
,
1789 .port_fdb_del
= rtl83xx_port_fdb_del
,
1790 .port_fdb_dump
= rtl83xx_port_fdb_dump
,
1792 .port_mdb_prepare
= rtl83xx_port_mdb_prepare
,
1793 .port_mdb_add
= rtl83xx_port_mdb_add
,
1794 .port_mdb_del
= rtl83xx_port_mdb_del
,
1796 .port_mirror_add
= rtl83xx_port_mirror_add
,
1797 .port_mirror_del
= rtl83xx_port_mirror_del
,
1800 const struct dsa_switch_ops rtl930x_switch_ops
= {
1801 .get_tag_protocol
= rtl83xx_get_tag_protocol
,
1802 .setup
= rtl930x_setup
,
1804 .phy_read
= dsa_phy_read
,
1805 .phy_write
= dsa_phy_write
,
1807 .phylink_validate
= rtl93xx_phylink_validate
,
1808 .phylink_mac_link_state
= rtl93xx_phylink_mac_link_state
,
1809 .phylink_mac_config
= rtl93xx_phylink_mac_config
,
1810 .phylink_mac_link_down
= rtl93xx_phylink_mac_link_down
,
1811 .phylink_mac_link_up
= rtl93xx_phylink_mac_link_up
,
1813 .get_strings
= rtl83xx_get_strings
,
1814 .get_ethtool_stats
= rtl83xx_get_ethtool_stats
,
1815 .get_sset_count
= rtl83xx_get_sset_count
,
1817 .port_enable
= rtl83xx_port_enable
,
1818 .port_disable
= rtl83xx_port_disable
,
1820 .get_mac_eee
= rtl93xx_get_mac_eee
,
1821 .set_mac_eee
= rtl83xx_set_mac_eee
,
1823 .set_ageing_time
= rtl83xx_set_l2aging
,
1824 .port_bridge_join
= rtl83xx_port_bridge_join
,
1825 .port_bridge_leave
= rtl83xx_port_bridge_leave
,
1826 .port_stp_state_set
= rtl83xx_port_stp_state_set
,
1827 .port_fast_age
= rtl930x_fast_age
,
1829 .port_vlan_filtering
= rtl83xx_vlan_filtering
,
1830 .port_vlan_prepare
= rtl83xx_vlan_prepare
,
1831 .port_vlan_add
= rtl83xx_vlan_add
,
1832 .port_vlan_del
= rtl83xx_vlan_del
,
1834 .port_fdb_add
= rtl83xx_port_fdb_add
,
1835 .port_fdb_del
= rtl83xx_port_fdb_del
,
1836 .port_fdb_dump
= rtl83xx_port_fdb_dump
,
1838 .port_mdb_prepare
= rtl83xx_port_mdb_prepare
,
1839 .port_mdb_add
= rtl83xx_port_mdb_add
,
1840 .port_mdb_del
= rtl83xx_port_mdb_del
,