1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2020 NovaTech LLC
8 #include <linux/if_bridge.h>
9 #include <linux/of_device.h>
10 #include <linux/netdev_features.h>
11 #include <linux/if_hsr.h>
13 #include "xrs700x_reg.h"
15 #define XRS700X_MIB_INTERVAL msecs_to_jiffies(3000)
17 #define XRS7000X_SUPPORTED_HSR_FEATURES \
18 (NETIF_F_HW_HSR_TAG_INS | NETIF_F_HW_HSR_TAG_RM | \
19 NETIF_F_HW_HSR_FWD | NETIF_F_HW_HSR_DUP)
21 #define XRS7003E_ID 0x100
22 #define XRS7003F_ID 0x101
23 #define XRS7004E_ID 0x200
24 #define XRS7004F_ID 0x201
26 const struct xrs700x_info xrs7003e_info = {XRS7003E_ID, "XRS7003E", 3};
27 EXPORT_SYMBOL(xrs7003e_info);
29 const struct xrs700x_info xrs7003f_info = {XRS7003F_ID, "XRS7003F", 3};
30 EXPORT_SYMBOL(xrs7003f_info);
32 const struct xrs700x_info xrs7004e_info = {XRS7004E_ID, "XRS7004E", 4};
33 EXPORT_SYMBOL(xrs7004e_info);
35 const struct xrs700x_info xrs7004f_info = {XRS7004F_ID, "XRS7004F", 4};
36 EXPORT_SYMBOL(xrs7004f_info);
38 struct xrs700x_regfield {
40 struct regmap_field **rmf;
49 #define XRS700X_MIB_ETHTOOL_ONLY(o, n) {o, n, -1}
50 #define XRS700X_MIB(o, n, m) {o, n, offsetof(struct rtnl_link_stats64, m)}
52 static const struct xrs700x_mib xrs700x_mibs[] = {
53 XRS700X_MIB(XRS_RX_GOOD_OCTETS_L, "rx_good_octets", rx_bytes),
54 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_BAD_OCTETS_L, "rx_bad_octets"),
55 XRS700X_MIB(XRS_RX_UNICAST_L, "rx_unicast", rx_packets),
56 XRS700X_MIB(XRS_RX_BROADCAST_L, "rx_broadcast", rx_packets),
57 XRS700X_MIB(XRS_RX_MULTICAST_L, "rx_multicast", multicast),
58 XRS700X_MIB(XRS_RX_UNDERSIZE_L, "rx_undersize", rx_length_errors),
59 XRS700X_MIB(XRS_RX_FRAGMENTS_L, "rx_fragments", rx_length_errors),
60 XRS700X_MIB(XRS_RX_OVERSIZE_L, "rx_oversize", rx_length_errors),
61 XRS700X_MIB(XRS_RX_JABBER_L, "rx_jabber", rx_length_errors),
62 XRS700X_MIB(XRS_RX_ERR_L, "rx_err", rx_errors),
63 XRS700X_MIB(XRS_RX_CRC_L, "rx_crc", rx_crc_errors),
64 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_64_L, "rx_64"),
65 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_65_127_L, "rx_65_127"),
66 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_128_255_L, "rx_128_255"),
67 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_256_511_L, "rx_256_511"),
68 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_512_1023_L, "rx_512_1023"),
69 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_1024_1536_L, "rx_1024_1536"),
70 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_HSR_PRP_L, "rx_hsr_prp"),
71 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_WRONGLAN_L, "rx_wronglan"),
72 XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_DUPLICATE_L, "rx_duplicate"),
73 XRS700X_MIB(XRS_TX_OCTETS_L, "tx_octets", tx_bytes),
74 XRS700X_MIB(XRS_TX_UNICAST_L, "tx_unicast", tx_packets),
75 XRS700X_MIB(XRS_TX_BROADCAST_L, "tx_broadcast", tx_packets),
76 XRS700X_MIB(XRS_TX_MULTICAST_L, "tx_multicast", tx_packets),
77 XRS700X_MIB_ETHTOOL_ONLY(XRS_TX_HSR_PRP_L, "tx_hsr_prp"),
78 XRS700X_MIB(XRS_PRIQ_DROP_L, "priq_drop", tx_dropped),
79 XRS700X_MIB(XRS_EARLY_DROP_L, "early_drop", tx_dropped),
82 static void xrs700x_get_strings(struct dsa_switch *ds, int port,
83 u32 stringset, u8 *data)
87 if (stringset != ETH_SS_STATS)
90 for (i = 0; i < ARRAY_SIZE(xrs700x_mibs); i++) {
91 strscpy(data, xrs700x_mibs[i].name, ETH_GSTRING_LEN);
92 data += ETH_GSTRING_LEN;
96 static int xrs700x_get_sset_count(struct dsa_switch *ds, int port, int sset)
98 if (sset != ETH_SS_STATS)
101 return ARRAY_SIZE(xrs700x_mibs);
104 static void xrs700x_read_port_counters(struct xrs700x *priv, int port)
106 struct xrs700x_port *p = &priv->ports[port];
107 struct rtnl_link_stats64 stats;
110 memset(&stats, 0, sizeof(stats));
112 mutex_lock(&p->mib_mutex);
114 /* Capture counter values */
115 regmap_write(priv->regmap, XRS_CNT_CTRL(port), 1);
117 for (i = 0; i < ARRAY_SIZE(xrs700x_mibs); i++) {
118 unsigned int high = 0, low = 0, reg;
120 reg = xrs700x_mibs[i].offset + XRS_PORT_OFFSET * port;
121 regmap_read(priv->regmap, reg, &low);
122 regmap_read(priv->regmap, reg + 2, &high);
124 p->mib_data[i] += (high << 16) | low;
126 if (xrs700x_mibs[i].stats64_offset >= 0) {
127 u8 *s = (u8 *)&stats + xrs700x_mibs[i].stats64_offset;
128 *(u64 *)s += p->mib_data[i];
132 /* multicast must be added to rx_packets (which already includes
133 * unicast and broadcast)
135 stats.rx_packets += stats.multicast;
137 u64_stats_update_begin(&p->syncp);
139 u64_stats_update_end(&p->syncp);
141 mutex_unlock(&p->mib_mutex);
144 static void xrs700x_mib_work(struct work_struct *work)
146 struct xrs700x *priv = container_of(work, struct xrs700x,
150 for (i = 0; i < priv->ds->num_ports; i++)
151 xrs700x_read_port_counters(priv, i);
153 schedule_delayed_work(&priv->mib_work, XRS700X_MIB_INTERVAL);
156 static void xrs700x_get_ethtool_stats(struct dsa_switch *ds, int port,
159 struct xrs700x *priv = ds->priv;
160 struct xrs700x_port *p = &priv->ports[port];
162 xrs700x_read_port_counters(priv, port);
164 mutex_lock(&p->mib_mutex);
165 memcpy(data, p->mib_data, sizeof(*data) * ARRAY_SIZE(xrs700x_mibs));
166 mutex_unlock(&p->mib_mutex);
169 static void xrs700x_get_stats64(struct dsa_switch *ds, int port,
170 struct rtnl_link_stats64 *s)
172 struct xrs700x *priv = ds->priv;
173 struct xrs700x_port *p = &priv->ports[port];
177 start = u64_stats_fetch_begin(&p->syncp);
179 } while (u64_stats_fetch_retry(&p->syncp, start));
182 static int xrs700x_setup_regmap_range(struct xrs700x *priv)
184 struct xrs700x_regfield regfields[] = {
186 .rf = REG_FIELD_ID(XRS_PORT_STATE(0), 0, 1,
189 .rmf = &priv->ps_forward
192 .rf = REG_FIELD_ID(XRS_PORT_STATE(0), 2, 3,
195 .rmf = &priv->ps_management
198 .rf = REG_FIELD_ID(XRS_PORT_STATE(0), 4, 9,
201 .rmf = &priv->ps_sel_speed
204 .rf = REG_FIELD_ID(XRS_PORT_STATE(0), 10, 11,
207 .rmf = &priv->ps_cur_speed
212 for (; i < ARRAY_SIZE(regfields); i++) {
213 *regfields[i].rmf = devm_regmap_field_alloc(priv->dev,
216 if (IS_ERR(*regfields[i].rmf))
217 return PTR_ERR(*regfields[i].rmf);
223 static enum dsa_tag_protocol xrs700x_get_tag_protocol(struct dsa_switch *ds,
225 enum dsa_tag_protocol m)
227 return DSA_TAG_PROTO_XRS700X;
230 static int xrs700x_reset(struct dsa_switch *ds)
232 struct xrs700x *priv = ds->priv;
236 ret = regmap_write(priv->regmap, XRS_GENERAL, XRS_GENERAL_RESET);
240 ret = regmap_read_poll_timeout(priv->regmap, XRS_GENERAL,
241 val, !(val & XRS_GENERAL_RESET),
245 dev_err_ratelimited(priv->dev, "error resetting switch: %d\n",
252 static void xrs700x_port_stp_state_set(struct dsa_switch *ds, int port,
255 struct xrs700x *priv = ds->priv;
256 unsigned int bpdus = 1;
260 case BR_STATE_DISABLED:
263 case BR_STATE_BLOCKING:
264 case BR_STATE_LISTENING:
265 val = XRS_PORT_DISABLED;
267 case BR_STATE_LEARNING:
268 val = XRS_PORT_LEARNING;
270 case BR_STATE_FORWARDING:
271 val = XRS_PORT_FORWARDING;
274 dev_err(ds->dev, "invalid STP state: %d\n", state);
278 regmap_fields_write(priv->ps_forward, port, val);
280 /* Enable/disable inbound policy added by xrs700x_port_add_bpdu_ipf()
281 * which allows BPDU forwarding to the CPU port when the front facing
282 * port is in disabled/learning state.
284 regmap_update_bits(priv->regmap, XRS_ETH_ADDR_CFG(port, 0), 1, bpdus);
286 dev_dbg_ratelimited(priv->dev, "%s - port: %d, state: %u, val: 0x%x\n",
287 __func__, port, state, val);
290 /* Add an inbound policy filter which matches the BPDU destination MAC
291 * and forwards to the CPU port. Leave the policy disabled, it will be
294 static int xrs700x_port_add_bpdu_ipf(struct dsa_switch *ds, int port)
296 struct xrs700x *priv = ds->priv;
297 unsigned int val = 0;
301 /* Compare all 48 bits of the destination MAC address. */
302 ret = regmap_write(priv->regmap, XRS_ETH_ADDR_CFG(port, 0), 48 << 2);
306 /* match BPDU destination 01:80:c2:00:00:00 */
307 for (i = 0; i < sizeof(eth_stp_addr); i += 2) {
308 ret = regmap_write(priv->regmap, XRS_ETH_ADDR_0(port, 0) + i,
310 (eth_stp_addr[i + 1] << 8));
315 /* Mirror BPDU to CPU port */
316 for (i = 0; i < ds->num_ports; i++) {
317 if (dsa_is_cpu_port(ds, i))
321 ret = regmap_write(priv->regmap, XRS_ETH_ADDR_FWD_MIRROR(port, 0), val);
325 ret = regmap_write(priv->regmap, XRS_ETH_ADDR_FWD_ALLOW(port, 0), 0);
332 static int xrs700x_port_setup(struct dsa_switch *ds, int port)
334 bool cpu_port = dsa_is_cpu_port(ds, port);
335 struct xrs700x *priv = ds->priv;
336 unsigned int val = 0;
339 xrs700x_port_stp_state_set(ds, port, BR_STATE_DISABLED);
341 /* Disable forwarding to non-CPU ports */
342 for (i = 0; i < ds->num_ports; i++) {
343 if (!dsa_is_cpu_port(ds, i))
347 /* 1 = Disable forwarding to the port */
348 ret = regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port), val);
352 val = cpu_port ? XRS_PORT_MODE_MANAGEMENT : XRS_PORT_MODE_NORMAL;
353 ret = regmap_fields_write(priv->ps_management, port, val);
358 ret = xrs700x_port_add_bpdu_ipf(ds, port);
366 static int xrs700x_setup(struct dsa_switch *ds)
368 struct xrs700x *priv = ds->priv;
371 ret = xrs700x_reset(ds);
375 for (i = 0; i < ds->num_ports; i++) {
376 ret = xrs700x_port_setup(ds, i);
381 schedule_delayed_work(&priv->mib_work, XRS700X_MIB_INTERVAL);
386 static void xrs700x_teardown(struct dsa_switch *ds)
388 struct xrs700x *priv = ds->priv;
390 cancel_delayed_work_sync(&priv->mib_work);
393 static void xrs700x_phylink_validate(struct dsa_switch *ds, int port,
394 unsigned long *supported,
395 struct phylink_link_state *state)
397 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
405 phylink_set(mask, 1000baseT_Full);
408 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
409 dev_err(ds->dev, "Unsupported port: %i\n", port);
413 phylink_set_port_modes(mask);
415 /* The switch only supports full duplex. */
416 phylink_set(mask, 10baseT_Full);
417 phylink_set(mask, 100baseT_Full);
419 bitmap_and(supported, supported, mask,
420 __ETHTOOL_LINK_MODE_MASK_NBITS);
421 bitmap_and(state->advertising, state->advertising, mask,
422 __ETHTOOL_LINK_MODE_MASK_NBITS);
425 static void xrs700x_mac_link_up(struct dsa_switch *ds, int port,
426 unsigned int mode, phy_interface_t interface,
427 struct phy_device *phydev,
428 int speed, int duplex,
429 bool tx_pause, bool rx_pause)
431 struct xrs700x *priv = ds->priv;
436 val = XRS_PORT_SPEED_1000;
439 val = XRS_PORT_SPEED_100;
442 val = XRS_PORT_SPEED_10;
448 regmap_fields_write(priv->ps_sel_speed, port, val);
450 dev_dbg_ratelimited(priv->dev, "%s: port: %d mode: %u speed: %u\n",
451 __func__, port, mode, speed);
454 static int xrs700x_bridge_common(struct dsa_switch *ds, int port,
455 struct net_device *bridge, bool join)
457 unsigned int i, cpu_mask = 0, mask = 0;
458 struct xrs700x *priv = ds->priv;
461 for (i = 0; i < ds->num_ports; i++) {
462 if (dsa_is_cpu_port(ds, i))
467 if (dsa_to_port(ds, i)->bridge_dev == bridge)
473 for (i = 0; i < ds->num_ports; i++) {
474 if (dsa_to_port(ds, i)->bridge_dev != bridge)
477 /* 1 = Disable forwarding to the port */
478 ret = regmap_write(priv->regmap, XRS_PORT_FWD_MASK(i), mask);
484 ret = regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port),
493 static int xrs700x_bridge_join(struct dsa_switch *ds, int port,
494 struct net_device *bridge)
496 return xrs700x_bridge_common(ds, port, bridge, true);
499 static void xrs700x_bridge_leave(struct dsa_switch *ds, int port,
500 struct net_device *bridge)
502 xrs700x_bridge_common(ds, port, bridge, false);
505 static int xrs700x_hsr_join(struct dsa_switch *ds, int port,
506 struct net_device *hsr)
508 unsigned int val = XRS_HSR_CFG_HSR_PRP;
509 struct dsa_port *partner = NULL, *dp;
510 struct xrs700x *priv = ds->priv;
511 struct net_device *slave;
512 int ret, i, hsr_pair[2];
513 enum hsr_version ver;
515 ret = hsr_get_version(hsr, &ver);
519 /* Only ports 1 and 2 can be HSR/PRP redundant ports. */
520 if (port != 1 && port != 2)
524 val |= XRS_HSR_CFG_HSR;
525 else if (ver == PRP_V1)
526 val |= XRS_HSR_CFG_PRP;
530 dsa_hsr_foreach_port(dp, ds, hsr) {
531 if (dp->index != port) {
537 /* We can't enable redundancy on the switch until both
538 * redundant ports have signed up.
543 regmap_fields_write(priv->ps_forward, partner->index,
545 regmap_fields_write(priv->ps_forward, port, XRS_PORT_DISABLED);
547 regmap_write(priv->regmap, XRS_HSR_CFG(partner->index),
548 val | XRS_HSR_CFG_LANID_A);
549 regmap_write(priv->regmap, XRS_HSR_CFG(port),
550 val | XRS_HSR_CFG_LANID_B);
552 /* Clear bits for both redundant ports (HSR only) and the CPU port to
555 val = GENMASK(ds->num_ports - 1, 0);
557 val &= ~BIT(partner->index);
560 val &= ~BIT(dsa_upstream_port(ds, port));
561 regmap_write(priv->regmap, XRS_PORT_FWD_MASK(partner->index), val);
562 regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port), val);
564 regmap_fields_write(priv->ps_forward, partner->index,
565 XRS_PORT_FORWARDING);
566 regmap_fields_write(priv->ps_forward, port, XRS_PORT_FORWARDING);
569 hsr_pair[1] = partner->index;
570 for (i = 0; i < ARRAY_SIZE(hsr_pair); i++) {
571 slave = dsa_to_port(ds, hsr_pair[i])->slave;
572 slave->features |= XRS7000X_SUPPORTED_HSR_FEATURES;
578 static int xrs700x_hsr_leave(struct dsa_switch *ds, int port,
579 struct net_device *hsr)
581 struct dsa_port *partner = NULL, *dp;
582 struct xrs700x *priv = ds->priv;
583 struct net_device *slave;
587 dsa_hsr_foreach_port(dp, ds, hsr) {
588 if (dp->index != port) {
597 regmap_fields_write(priv->ps_forward, partner->index,
599 regmap_fields_write(priv->ps_forward, port, XRS_PORT_DISABLED);
601 regmap_write(priv->regmap, XRS_HSR_CFG(partner->index), 0);
602 regmap_write(priv->regmap, XRS_HSR_CFG(port), 0);
604 /* Clear bit for the CPU port to enable forwarding. */
605 val = GENMASK(ds->num_ports - 1, 0);
606 val &= ~BIT(dsa_upstream_port(ds, port));
607 regmap_write(priv->regmap, XRS_PORT_FWD_MASK(partner->index), val);
608 regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port), val);
610 regmap_fields_write(priv->ps_forward, partner->index,
611 XRS_PORT_FORWARDING);
612 regmap_fields_write(priv->ps_forward, port, XRS_PORT_FORWARDING);
615 hsr_pair[1] = partner->index;
616 for (i = 0; i < ARRAY_SIZE(hsr_pair); i++) {
617 slave = dsa_to_port(ds, hsr_pair[i])->slave;
618 slave->features &= ~XRS7000X_SUPPORTED_HSR_FEATURES;
624 static const struct dsa_switch_ops xrs700x_ops = {
625 .get_tag_protocol = xrs700x_get_tag_protocol,
626 .setup = xrs700x_setup,
627 .teardown = xrs700x_teardown,
628 .port_stp_state_set = xrs700x_port_stp_state_set,
629 .phylink_validate = xrs700x_phylink_validate,
630 .phylink_mac_link_up = xrs700x_mac_link_up,
631 .get_strings = xrs700x_get_strings,
632 .get_sset_count = xrs700x_get_sset_count,
633 .get_ethtool_stats = xrs700x_get_ethtool_stats,
634 .get_stats64 = xrs700x_get_stats64,
635 .port_bridge_join = xrs700x_bridge_join,
636 .port_bridge_leave = xrs700x_bridge_leave,
637 .port_hsr_join = xrs700x_hsr_join,
638 .port_hsr_leave = xrs700x_hsr_leave,
641 static int xrs700x_detect(struct xrs700x *priv)
643 const struct xrs700x_info *info;
647 ret = regmap_read(priv->regmap, XRS_DEV_ID0, &id);
649 dev_err(priv->dev, "error %d while reading switch id.\n",
654 info = of_device_get_match_data(priv->dev);
658 if (info->id == id) {
659 priv->ds->num_ports = info->num_ports;
660 dev_info(priv->dev, "%s detected.\n", info->name);
664 dev_err(priv->dev, "expected switch id 0x%x but found 0x%x.\n",
670 struct xrs700x *xrs700x_switch_alloc(struct device *base, void *devpriv)
672 struct dsa_switch *ds;
673 struct xrs700x *priv;
675 ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
681 priv = devm_kzalloc(base, sizeof(*priv), GFP_KERNEL);
685 INIT_DELAYED_WORK(&priv->mib_work, xrs700x_mib_work);
687 ds->ops = &xrs700x_ops;
692 priv->priv = devpriv;
696 EXPORT_SYMBOL(xrs700x_switch_alloc);
698 static int xrs700x_alloc_port_mib(struct xrs700x *priv, int port)
700 struct xrs700x_port *p = &priv->ports[port];
702 p->mib_data = devm_kcalloc(priv->dev, ARRAY_SIZE(xrs700x_mibs),
703 sizeof(*p->mib_data), GFP_KERNEL);
707 mutex_init(&p->mib_mutex);
708 u64_stats_init(&p->syncp);
713 int xrs700x_switch_register(struct xrs700x *priv)
718 ret = xrs700x_detect(priv);
722 ret = xrs700x_setup_regmap_range(priv);
726 priv->ports = devm_kcalloc(priv->dev, priv->ds->num_ports,
727 sizeof(*priv->ports), GFP_KERNEL);
731 for (i = 0; i < priv->ds->num_ports; i++) {
732 ret = xrs700x_alloc_port_mib(priv, i);
737 return dsa_register_switch(priv->ds);
739 EXPORT_SYMBOL(xrs700x_switch_register);
741 void xrs700x_switch_remove(struct xrs700x *priv)
743 dsa_unregister_switch(priv->ds);
745 EXPORT_SYMBOL(xrs700x_switch_remove);
748 MODULE_DESCRIPTION("Arrow SpeedChips XRS700x DSA driver");
749 MODULE_LICENSE("GPL v2");