xref: /linux/drivers/net/dsa/xrs700x/xrs700x.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020 NovaTech LLC
4  * George McCollister <george.mccollister@gmail.com>
5  */
6 
7 #include <net/dsa.h>
8 #include <linux/etherdevice.h>
9 #include <linux/if_bridge.h>
10 #include <linux/of.h>
11 #include <linux/netdev_features.h>
12 #include <linux/if_hsr.h>
13 #include "xrs700x.h"
14 #include "xrs700x_reg.h"
15 
16 #define XRS700X_MIB_INTERVAL msecs_to_jiffies(3000)
17 
18 #define XRS7000X_SUPPORTED_HSR_FEATURES \
19 	(NETIF_F_HW_HSR_TAG_INS | NETIF_F_HW_HSR_TAG_RM | \
20 	 NETIF_F_HW_HSR_FWD | NETIF_F_HW_HSR_DUP)
21 
22 #define XRS7003E_ID	0x100
23 #define XRS7003F_ID	0x101
24 #define XRS7004E_ID	0x200
25 #define XRS7004F_ID	0x201
26 
27 const struct xrs700x_info xrs7003e_info = {XRS7003E_ID, "XRS7003E", 3};
28 EXPORT_SYMBOL(xrs7003e_info);
29 
30 const struct xrs700x_info xrs7003f_info = {XRS7003F_ID, "XRS7003F", 3};
31 EXPORT_SYMBOL(xrs7003f_info);
32 
33 const struct xrs700x_info xrs7004e_info = {XRS7004E_ID, "XRS7004E", 4};
34 EXPORT_SYMBOL(xrs7004e_info);
35 
36 const struct xrs700x_info xrs7004f_info = {XRS7004F_ID, "XRS7004F", 4};
37 EXPORT_SYMBOL(xrs7004f_info);
38 
39 struct xrs700x_regfield {
40 	struct reg_field rf;
41 	struct regmap_field **rmf;
42 };
43 
44 struct xrs700x_mib {
45 	unsigned int offset;
46 	const char *name;
47 	int stats64_offset;
48 };
49 
50 #define XRS700X_MIB_ETHTOOL_ONLY(o, n) {o, n, -1}
51 #define XRS700X_MIB(o, n, m) {o, n, offsetof(struct rtnl_link_stats64, m)}
52 
53 static const struct xrs700x_mib xrs700x_mibs[] = {
54 	XRS700X_MIB(XRS_RX_GOOD_OCTETS_L, "rx_good_octets", rx_bytes),
55 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_BAD_OCTETS_L, "rx_bad_octets"),
56 	XRS700X_MIB(XRS_RX_UNICAST_L, "rx_unicast", rx_packets),
57 	XRS700X_MIB(XRS_RX_BROADCAST_L, "rx_broadcast", rx_packets),
58 	XRS700X_MIB(XRS_RX_MULTICAST_L, "rx_multicast", multicast),
59 	XRS700X_MIB(XRS_RX_UNDERSIZE_L, "rx_undersize", rx_length_errors),
60 	XRS700X_MIB(XRS_RX_FRAGMENTS_L, "rx_fragments", rx_length_errors),
61 	XRS700X_MIB(XRS_RX_OVERSIZE_L, "rx_oversize", rx_length_errors),
62 	XRS700X_MIB(XRS_RX_JABBER_L, "rx_jabber", rx_length_errors),
63 	XRS700X_MIB(XRS_RX_ERR_L, "rx_err", rx_errors),
64 	XRS700X_MIB(XRS_RX_CRC_L, "rx_crc", rx_crc_errors),
65 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_64_L, "rx_64"),
66 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_65_127_L, "rx_65_127"),
67 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_128_255_L, "rx_128_255"),
68 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_256_511_L, "rx_256_511"),
69 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_512_1023_L, "rx_512_1023"),
70 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_1024_1536_L, "rx_1024_1536"),
71 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_HSR_PRP_L, "rx_hsr_prp"),
72 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_WRONGLAN_L, "rx_wronglan"),
73 	XRS700X_MIB_ETHTOOL_ONLY(XRS_RX_DUPLICATE_L, "rx_duplicate"),
74 	XRS700X_MIB(XRS_TX_OCTETS_L, "tx_octets", tx_bytes),
75 	XRS700X_MIB(XRS_TX_UNICAST_L, "tx_unicast", tx_packets),
76 	XRS700X_MIB(XRS_TX_BROADCAST_L, "tx_broadcast", tx_packets),
77 	XRS700X_MIB(XRS_TX_MULTICAST_L, "tx_multicast", tx_packets),
78 	XRS700X_MIB_ETHTOOL_ONLY(XRS_TX_HSR_PRP_L, "tx_hsr_prp"),
79 	XRS700X_MIB(XRS_PRIQ_DROP_L, "priq_drop", tx_dropped),
80 	XRS700X_MIB(XRS_EARLY_DROP_L, "early_drop", tx_dropped),
81 };
82 
83 static const u8 eth_hsrsup_addr[ETH_ALEN] = {
84 	0x01, 0x15, 0x4e, 0x00, 0x01, 0x00};
85 
86 static void xrs700x_get_strings(struct dsa_switch *ds, int port,
87 				u32 stringset, u8 *data)
88 {
89 	int i;
90 
91 	if (stringset != ETH_SS_STATS)
92 		return;
93 
94 	for (i = 0; i < ARRAY_SIZE(xrs700x_mibs); i++)
95 		ethtool_puts(&data, xrs700x_mibs[i].name);
96 }
97 
98 static int xrs700x_get_sset_count(struct dsa_switch *ds, int port, int sset)
99 {
100 	if (sset != ETH_SS_STATS)
101 		return -EOPNOTSUPP;
102 
103 	return ARRAY_SIZE(xrs700x_mibs);
104 }
105 
106 static void xrs700x_read_port_counters(struct xrs700x *priv, int port)
107 {
108 	struct xrs700x_port *p = &priv->ports[port];
109 	struct rtnl_link_stats64 stats;
110 	unsigned long flags;
111 	int i;
112 
113 	memset(&stats, 0, sizeof(stats));
114 
115 	mutex_lock(&p->mib_mutex);
116 
117 	/* Capture counter values */
118 	regmap_write(priv->regmap, XRS_CNT_CTRL(port), 1);
119 
120 	for (i = 0; i < ARRAY_SIZE(xrs700x_mibs); i++) {
121 		unsigned int high = 0, low = 0, reg;
122 
123 		reg = xrs700x_mibs[i].offset + XRS_PORT_OFFSET * port;
124 		regmap_read(priv->regmap, reg, &low);
125 		regmap_read(priv->regmap, reg + 2, &high);
126 
127 		p->mib_data[i] += (high << 16) | low;
128 
129 		if (xrs700x_mibs[i].stats64_offset >= 0) {
130 			u8 *s = (u8 *)&stats + xrs700x_mibs[i].stats64_offset;
131 			*(u64 *)s += p->mib_data[i];
132 		}
133 	}
134 
135 	/* multicast must be added to rx_packets (which already includes
136 	 * unicast and broadcast)
137 	 */
138 	stats.rx_packets += stats.multicast;
139 
140 	flags = u64_stats_update_begin_irqsave(&p->syncp);
141 	p->stats64 = stats;
142 	u64_stats_update_end_irqrestore(&p->syncp, flags);
143 
144 	mutex_unlock(&p->mib_mutex);
145 }
146 
147 static void xrs700x_mib_work(struct work_struct *work)
148 {
149 	struct xrs700x *priv = container_of(work, struct xrs700x,
150 					    mib_work.work);
151 	int i;
152 
153 	for (i = 0; i < priv->ds->num_ports; i++)
154 		xrs700x_read_port_counters(priv, i);
155 
156 	schedule_delayed_work(&priv->mib_work, XRS700X_MIB_INTERVAL);
157 }
158 
159 static void xrs700x_get_ethtool_stats(struct dsa_switch *ds, int port,
160 				      u64 *data)
161 {
162 	struct xrs700x *priv = ds->priv;
163 	struct xrs700x_port *p = &priv->ports[port];
164 
165 	xrs700x_read_port_counters(priv, port);
166 
167 	mutex_lock(&p->mib_mutex);
168 	memcpy(data, p->mib_data, sizeof(*data) * ARRAY_SIZE(xrs700x_mibs));
169 	mutex_unlock(&p->mib_mutex);
170 }
171 
172 static void xrs700x_get_stats64(struct dsa_switch *ds, int port,
173 				struct rtnl_link_stats64 *s)
174 {
175 	struct xrs700x *priv = ds->priv;
176 	struct xrs700x_port *p = &priv->ports[port];
177 	unsigned int start;
178 
179 	do {
180 		start = u64_stats_fetch_begin(&p->syncp);
181 		*s = p->stats64;
182 	} while (u64_stats_fetch_retry(&p->syncp, start));
183 }
184 
185 static int xrs700x_setup_regmap_range(struct xrs700x *priv)
186 {
187 	struct xrs700x_regfield regfields[] = {
188 		{
189 			.rf = REG_FIELD_ID(XRS_PORT_STATE(0), 0, 1,
190 					   priv->ds->num_ports,
191 					   XRS_PORT_OFFSET),
192 			.rmf = &priv->ps_forward
193 		},
194 		{
195 			.rf = REG_FIELD_ID(XRS_PORT_STATE(0), 2, 3,
196 					   priv->ds->num_ports,
197 					   XRS_PORT_OFFSET),
198 			.rmf = &priv->ps_management
199 		},
200 		{
201 			.rf = REG_FIELD_ID(XRS_PORT_STATE(0), 4, 9,
202 					   priv->ds->num_ports,
203 					   XRS_PORT_OFFSET),
204 			.rmf = &priv->ps_sel_speed
205 		},
206 		{
207 			.rf = REG_FIELD_ID(XRS_PORT_STATE(0), 10, 11,
208 					   priv->ds->num_ports,
209 					   XRS_PORT_OFFSET),
210 			.rmf = &priv->ps_cur_speed
211 		}
212 	};
213 	int i = 0;
214 
215 	for (; i < ARRAY_SIZE(regfields); i++) {
216 		*regfields[i].rmf = devm_regmap_field_alloc(priv->dev,
217 							    priv->regmap,
218 							    regfields[i].rf);
219 		if (IS_ERR(*regfields[i].rmf))
220 			return PTR_ERR(*regfields[i].rmf);
221 	}
222 
223 	return 0;
224 }
225 
226 static enum dsa_tag_protocol xrs700x_get_tag_protocol(struct dsa_switch *ds,
227 						      int port,
228 						      enum dsa_tag_protocol m)
229 {
230 	return DSA_TAG_PROTO_XRS700X;
231 }
232 
233 static int xrs700x_reset(struct dsa_switch *ds)
234 {
235 	struct xrs700x *priv = ds->priv;
236 	unsigned int val;
237 	int ret;
238 
239 	ret = regmap_write(priv->regmap, XRS_GENERAL, XRS_GENERAL_RESET);
240 	if (ret)
241 		goto error;
242 
243 	ret = regmap_read_poll_timeout(priv->regmap, XRS_GENERAL,
244 				       val, !(val & XRS_GENERAL_RESET),
245 				       10, 1000);
246 error:
247 	if (ret) {
248 		dev_err_ratelimited(priv->dev, "error resetting switch: %d\n",
249 				    ret);
250 	}
251 
252 	return ret;
253 }
254 
255 static void xrs700x_port_stp_state_set(struct dsa_switch *ds, int port,
256 				       u8 state)
257 {
258 	struct xrs700x *priv = ds->priv;
259 	unsigned int bpdus = 1;
260 	unsigned int val;
261 
262 	switch (state) {
263 	case BR_STATE_DISABLED:
264 		bpdus = 0;
265 		fallthrough;
266 	case BR_STATE_BLOCKING:
267 	case BR_STATE_LISTENING:
268 		val = XRS_PORT_DISABLED;
269 		break;
270 	case BR_STATE_LEARNING:
271 		val = XRS_PORT_LEARNING;
272 		break;
273 	case BR_STATE_FORWARDING:
274 		val = XRS_PORT_FORWARDING;
275 		break;
276 	default:
277 		dev_err(ds->dev, "invalid STP state: %d\n", state);
278 		return;
279 	}
280 
281 	regmap_fields_write(priv->ps_forward, port, val);
282 
283 	/* Enable/disable inbound policy added by xrs700x_port_add_bpdu_ipf()
284 	 * which allows BPDU forwarding to the CPU port when the front facing
285 	 * port is in disabled/learning state.
286 	 */
287 	regmap_update_bits(priv->regmap, XRS_ETH_ADDR_CFG(port, 0), 1, bpdus);
288 
289 	dev_dbg_ratelimited(priv->dev, "%s - port: %d, state: %u, val: 0x%x\n",
290 			    __func__, port, state, val);
291 }
292 
293 /* Add an inbound policy filter which matches the BPDU destination MAC
294  * and forwards to the CPU port. Leave the policy disabled, it will be
295  * enabled as needed.
296  */
297 static int xrs700x_port_add_bpdu_ipf(struct dsa_switch *ds, int port)
298 {
299 	struct xrs700x *priv = ds->priv;
300 	unsigned int val = 0;
301 	int i = 0;
302 	int ret;
303 
304 	/* Compare all 48 bits of the destination MAC address. */
305 	ret = regmap_write(priv->regmap, XRS_ETH_ADDR_CFG(port, 0), 48 << 2);
306 	if (ret)
307 		return ret;
308 
309 	/* match BPDU destination 01:80:c2:00:00:00 */
310 	for (i = 0; i < sizeof(eth_stp_addr); i += 2) {
311 		ret = regmap_write(priv->regmap, XRS_ETH_ADDR_0(port, 0) + i,
312 				   eth_stp_addr[i] |
313 				   (eth_stp_addr[i + 1] << 8));
314 		if (ret)
315 			return ret;
316 	}
317 
318 	/* Mirror BPDU to CPU port */
319 	for (i = 0; i < ds->num_ports; i++) {
320 		if (dsa_is_cpu_port(ds, i))
321 			val |= BIT(i);
322 	}
323 
324 	ret = regmap_write(priv->regmap, XRS_ETH_ADDR_FWD_MIRROR(port, 0), val);
325 	if (ret)
326 		return ret;
327 
328 	ret = regmap_write(priv->regmap, XRS_ETH_ADDR_FWD_ALLOW(port, 0), 0);
329 	if (ret)
330 		return ret;
331 
332 	return 0;
333 }
334 
335 /* Add an inbound policy filter which matches the HSR/PRP supervision MAC
336  * range and forwards to the CPU port without discarding duplicates.
337  * This is required to correctly populate the HSR/PRP node_table.
338  * Leave the policy disabled, it will be enabled as needed.
339  */
340 static int xrs700x_port_add_hsrsup_ipf(struct dsa_switch *ds, int port,
341 				       int fwdport)
342 {
343 	struct xrs700x *priv = ds->priv;
344 	unsigned int val = 0;
345 	int i = 0;
346 	int ret;
347 
348 	/* Compare 40 bits of the destination MAC address. */
349 	ret = regmap_write(priv->regmap, XRS_ETH_ADDR_CFG(port, 1), 40 << 2);
350 	if (ret)
351 		return ret;
352 
353 	/* match HSR/PRP supervision destination 01:15:4e:00:01:XX */
354 	for (i = 0; i < sizeof(eth_hsrsup_addr); i += 2) {
355 		ret = regmap_write(priv->regmap, XRS_ETH_ADDR_0(port, 1) + i,
356 				   eth_hsrsup_addr[i] |
357 				   (eth_hsrsup_addr[i + 1] << 8));
358 		if (ret)
359 			return ret;
360 	}
361 
362 	/* Mirror HSR/PRP supervision to CPU port */
363 	for (i = 0; i < ds->num_ports; i++) {
364 		if (dsa_is_cpu_port(ds, i))
365 			val |= BIT(i);
366 	}
367 
368 	ret = regmap_write(priv->regmap, XRS_ETH_ADDR_FWD_MIRROR(port, 1), val);
369 	if (ret)
370 		return ret;
371 
372 	if (fwdport >= 0)
373 		val |= BIT(fwdport);
374 
375 	/* Allow must be set prevent duplicate discard */
376 	ret = regmap_write(priv->regmap, XRS_ETH_ADDR_FWD_ALLOW(port, 1), val);
377 	if (ret)
378 		return ret;
379 
380 	return 0;
381 }
382 
383 static int xrs700x_port_setup(struct dsa_switch *ds, int port)
384 {
385 	bool cpu_port = dsa_is_cpu_port(ds, port);
386 	struct xrs700x *priv = ds->priv;
387 	unsigned int val = 0;
388 	int ret, i;
389 
390 	xrs700x_port_stp_state_set(ds, port, BR_STATE_DISABLED);
391 
392 	/* Disable forwarding to non-CPU ports */
393 	for (i = 0; i < ds->num_ports; i++) {
394 		if (!dsa_is_cpu_port(ds, i))
395 			val |= BIT(i);
396 	}
397 
398 	/* 1 = Disable forwarding to the port */
399 	ret = regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port), val);
400 	if (ret)
401 		return ret;
402 
403 	val = cpu_port ? XRS_PORT_MODE_MANAGEMENT : XRS_PORT_MODE_NORMAL;
404 	ret = regmap_fields_write(priv->ps_management, port, val);
405 	if (ret)
406 		return ret;
407 
408 	if (!cpu_port) {
409 		ret = xrs700x_port_add_bpdu_ipf(ds, port);
410 		if (ret)
411 			return ret;
412 	}
413 
414 	return 0;
415 }
416 
417 static int xrs700x_setup(struct dsa_switch *ds)
418 {
419 	struct xrs700x *priv = ds->priv;
420 	int ret, i;
421 
422 	ret = xrs700x_reset(ds);
423 	if (ret)
424 		return ret;
425 
426 	for (i = 0; i < ds->num_ports; i++) {
427 		ret = xrs700x_port_setup(ds, i);
428 		if (ret)
429 			return ret;
430 	}
431 
432 	schedule_delayed_work(&priv->mib_work, XRS700X_MIB_INTERVAL);
433 
434 	return 0;
435 }
436 
437 static void xrs700x_teardown(struct dsa_switch *ds)
438 {
439 	struct xrs700x *priv = ds->priv;
440 
441 	cancel_delayed_work_sync(&priv->mib_work);
442 }
443 
444 static void xrs700x_phylink_get_caps(struct dsa_switch *ds, int port,
445 				     struct phylink_config *config)
446 {
447 	switch (port) {
448 	case 0:
449 		__set_bit(PHY_INTERFACE_MODE_RMII,
450 			  config->supported_interfaces);
451 		config->mac_capabilities = MAC_10FD | MAC_100FD;
452 		break;
453 
454 	case 1:
455 	case 2:
456 	case 3:
457 		phy_interface_set_rgmii(config->supported_interfaces);
458 		config->mac_capabilities = MAC_10FD | MAC_100FD | MAC_1000FD;
459 		break;
460 
461 	default:
462 		dev_err(ds->dev, "Unsupported port: %i\n", port);
463 		break;
464 	}
465 }
466 
467 static void xrs700x_mac_config(struct phylink_config *config, unsigned int mode,
468 			       const struct phylink_link_state *state)
469 {
470 }
471 
472 static void xrs700x_mac_link_down(struct phylink_config *config,
473 				  unsigned int mode, phy_interface_t interface)
474 {
475 }
476 
477 static void xrs700x_mac_link_up(struct phylink_config *config,
478 				struct phy_device *phydev,
479 				unsigned int mode, phy_interface_t interface,
480 				int speed, int duplex,
481 				bool tx_pause, bool rx_pause)
482 {
483 	struct dsa_port *dp = dsa_phylink_to_port(config);
484 	struct xrs700x *priv = dp->ds->priv;
485 	int port = dp->index;
486 	unsigned int val;
487 
488 	switch (speed) {
489 	case SPEED_1000:
490 		val = XRS_PORT_SPEED_1000;
491 		break;
492 	case SPEED_100:
493 		val = XRS_PORT_SPEED_100;
494 		break;
495 	case SPEED_10:
496 		val = XRS_PORT_SPEED_10;
497 		break;
498 	default:
499 		return;
500 	}
501 
502 	regmap_fields_write(priv->ps_sel_speed, port, val);
503 
504 	dev_dbg_ratelimited(priv->dev, "%s: port: %d mode: %u speed: %u\n",
505 			    __func__, port, mode, speed);
506 }
507 
508 static int xrs700x_bridge_common(struct dsa_switch *ds, int port,
509 				 struct dsa_bridge bridge, bool join)
510 {
511 	unsigned int i, cpu_mask = 0, mask = 0;
512 	struct xrs700x *priv = ds->priv;
513 	int ret;
514 
515 	for (i = 0; i < ds->num_ports; i++) {
516 		if (dsa_is_cpu_port(ds, i))
517 			continue;
518 
519 		cpu_mask |= BIT(i);
520 
521 		if (dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
522 			continue;
523 
524 		mask |= BIT(i);
525 	}
526 
527 	for (i = 0; i < ds->num_ports; i++) {
528 		if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
529 			continue;
530 
531 		/* 1 = Disable forwarding to the port */
532 		ret = regmap_write(priv->regmap, XRS_PORT_FWD_MASK(i), mask);
533 		if (ret)
534 			return ret;
535 	}
536 
537 	if (!join) {
538 		ret = regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port),
539 				   cpu_mask);
540 		if (ret)
541 			return ret;
542 	}
543 
544 	return 0;
545 }
546 
547 static int xrs700x_bridge_join(struct dsa_switch *ds, int port,
548 			       struct dsa_bridge bridge, bool *tx_fwd_offload,
549 			       struct netlink_ext_ack *extack)
550 {
551 	return xrs700x_bridge_common(ds, port, bridge, true);
552 }
553 
554 static void xrs700x_bridge_leave(struct dsa_switch *ds, int port,
555 				 struct dsa_bridge bridge)
556 {
557 	xrs700x_bridge_common(ds, port, bridge, false);
558 }
559 
560 static int xrs700x_hsr_join(struct dsa_switch *ds, int port,
561 			    struct net_device *hsr,
562 			    struct netlink_ext_ack *extack)
563 {
564 	unsigned int val = XRS_HSR_CFG_HSR_PRP;
565 	struct dsa_port *partner = NULL, *dp;
566 	struct xrs700x *priv = ds->priv;
567 	struct net_device *user;
568 	int ret, i, hsr_pair[2];
569 	enum hsr_version ver;
570 	bool fwd = false;
571 
572 	ret = hsr_get_version(hsr, &ver);
573 	if (ret)
574 		return ret;
575 
576 	if (port != 1 && port != 2) {
577 		NL_SET_ERR_MSG_MOD(extack,
578 				   "Only ports 1 and 2 can offload HSR/PRP");
579 		return -EOPNOTSUPP;
580 	}
581 
582 	if (ver == HSR_V1) {
583 		val |= XRS_HSR_CFG_HSR;
584 	} else if (ver == PRP_V1) {
585 		val |= XRS_HSR_CFG_PRP;
586 	} else {
587 		NL_SET_ERR_MSG_MOD(extack,
588 				   "Only HSR v1 and PRP v1 can be offloaded");
589 		return -EOPNOTSUPP;
590 	}
591 
592 	dsa_hsr_foreach_port(dp, ds, hsr) {
593 		if (dp->index != port) {
594 			partner = dp;
595 			break;
596 		}
597 	}
598 
599 	/* We can't enable redundancy on the switch until both
600 	 * redundant ports have signed up.
601 	 */
602 	if (!partner)
603 		return 0;
604 
605 	regmap_fields_write(priv->ps_forward, partner->index,
606 			    XRS_PORT_DISABLED);
607 	regmap_fields_write(priv->ps_forward, port, XRS_PORT_DISABLED);
608 
609 	regmap_write(priv->regmap, XRS_HSR_CFG(partner->index),
610 		     val | XRS_HSR_CFG_LANID_A);
611 	regmap_write(priv->regmap, XRS_HSR_CFG(port),
612 		     val | XRS_HSR_CFG_LANID_B);
613 
614 	/* Clear bits for both redundant ports (HSR only) and the CPU port to
615 	 * enable forwarding.
616 	 */
617 	val = GENMASK(ds->num_ports - 1, 0);
618 	if (ver == HSR_V1) {
619 		val &= ~BIT(partner->index);
620 		val &= ~BIT(port);
621 		fwd = true;
622 	}
623 	val &= ~BIT(dsa_upstream_port(ds, port));
624 	regmap_write(priv->regmap, XRS_PORT_FWD_MASK(partner->index), val);
625 	regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port), val);
626 
627 	regmap_fields_write(priv->ps_forward, partner->index,
628 			    XRS_PORT_FORWARDING);
629 	regmap_fields_write(priv->ps_forward, port, XRS_PORT_FORWARDING);
630 
631 	/* Enable inbound policy which allows HSR/PRP supervision forwarding
632 	 * to the CPU port without discarding duplicates. Continue to
633 	 * forward to redundant ports when in HSR mode while discarding
634 	 * duplicates.
635 	 */
636 	ret = xrs700x_port_add_hsrsup_ipf(ds, partner->index, fwd ? port : -1);
637 	if (ret)
638 		return ret;
639 
640 	ret = xrs700x_port_add_hsrsup_ipf(ds, port, fwd ? partner->index : -1);
641 	if (ret)
642 		return ret;
643 
644 	regmap_update_bits(priv->regmap,
645 			   XRS_ETH_ADDR_CFG(partner->index, 1), 1, 1);
646 	regmap_update_bits(priv->regmap, XRS_ETH_ADDR_CFG(port, 1), 1, 1);
647 
648 	hsr_pair[0] = port;
649 	hsr_pair[1] = partner->index;
650 	for (i = 0; i < ARRAY_SIZE(hsr_pair); i++) {
651 		user = dsa_to_port(ds, hsr_pair[i])->user;
652 		user->features |= XRS7000X_SUPPORTED_HSR_FEATURES;
653 	}
654 
655 	return 0;
656 }
657 
658 static int xrs700x_hsr_leave(struct dsa_switch *ds, int port,
659 			     struct net_device *hsr)
660 {
661 	struct dsa_port *partner = NULL, *dp;
662 	struct xrs700x *priv = ds->priv;
663 	struct net_device *user;
664 	int i, hsr_pair[2];
665 	unsigned int val;
666 
667 	dsa_hsr_foreach_port(dp, ds, hsr) {
668 		if (dp->index != port) {
669 			partner = dp;
670 			break;
671 		}
672 	}
673 
674 	if (!partner)
675 		return 0;
676 
677 	regmap_fields_write(priv->ps_forward, partner->index,
678 			    XRS_PORT_DISABLED);
679 	regmap_fields_write(priv->ps_forward, port, XRS_PORT_DISABLED);
680 
681 	regmap_write(priv->regmap, XRS_HSR_CFG(partner->index), 0);
682 	regmap_write(priv->regmap, XRS_HSR_CFG(port), 0);
683 
684 	/* Clear bit for the CPU port to enable forwarding. */
685 	val = GENMASK(ds->num_ports - 1, 0);
686 	val &= ~BIT(dsa_upstream_port(ds, port));
687 	regmap_write(priv->regmap, XRS_PORT_FWD_MASK(partner->index), val);
688 	regmap_write(priv->regmap, XRS_PORT_FWD_MASK(port), val);
689 
690 	regmap_fields_write(priv->ps_forward, partner->index,
691 			    XRS_PORT_FORWARDING);
692 	regmap_fields_write(priv->ps_forward, port, XRS_PORT_FORWARDING);
693 
694 	/* Disable inbound policy added by xrs700x_port_add_hsrsup_ipf()
695 	 * which allows HSR/PRP supervision forwarding to the CPU port without
696 	 * discarding duplicates.
697 	 */
698 	regmap_update_bits(priv->regmap,
699 			   XRS_ETH_ADDR_CFG(partner->index, 1), 1, 0);
700 	regmap_update_bits(priv->regmap, XRS_ETH_ADDR_CFG(port, 1), 1, 0);
701 
702 	hsr_pair[0] = port;
703 	hsr_pair[1] = partner->index;
704 	for (i = 0; i < ARRAY_SIZE(hsr_pair); i++) {
705 		user = dsa_to_port(ds, hsr_pair[i])->user;
706 		user->features &= ~XRS7000X_SUPPORTED_HSR_FEATURES;
707 	}
708 
709 	return 0;
710 }
711 
712 static const struct phylink_mac_ops xrs700x_phylink_mac_ops = {
713 	.mac_config		= xrs700x_mac_config,
714 	.mac_link_down		= xrs700x_mac_link_down,
715 	.mac_link_up		= xrs700x_mac_link_up,
716 };
717 
718 static const struct dsa_switch_ops xrs700x_ops = {
719 	.get_tag_protocol	= xrs700x_get_tag_protocol,
720 	.setup			= xrs700x_setup,
721 	.teardown		= xrs700x_teardown,
722 	.port_stp_state_set	= xrs700x_port_stp_state_set,
723 	.phylink_get_caps	= xrs700x_phylink_get_caps,
724 	.get_strings		= xrs700x_get_strings,
725 	.get_sset_count		= xrs700x_get_sset_count,
726 	.get_ethtool_stats	= xrs700x_get_ethtool_stats,
727 	.get_stats64		= xrs700x_get_stats64,
728 	.port_bridge_join	= xrs700x_bridge_join,
729 	.port_bridge_leave	= xrs700x_bridge_leave,
730 	.port_hsr_join		= xrs700x_hsr_join,
731 	.port_hsr_leave		= xrs700x_hsr_leave,
732 };
733 
734 static int xrs700x_detect(struct xrs700x *priv)
735 {
736 	const struct xrs700x_info *info;
737 	unsigned int id;
738 	int ret;
739 
740 	ret = regmap_read(priv->regmap, XRS_DEV_ID0, &id);
741 	if (ret) {
742 		dev_err(priv->dev, "error %d while reading switch id.\n",
743 			ret);
744 		return ret;
745 	}
746 
747 	info = of_device_get_match_data(priv->dev);
748 	if (!info)
749 		return -EINVAL;
750 
751 	if (info->id == id) {
752 		priv->ds->num_ports = info->num_ports;
753 		dev_info(priv->dev, "%s detected.\n", info->name);
754 		return 0;
755 	}
756 
757 	dev_err(priv->dev, "expected switch id 0x%x but found 0x%x.\n",
758 		info->id, id);
759 
760 	return -ENODEV;
761 }
762 
763 struct xrs700x *xrs700x_switch_alloc(struct device *base, void *devpriv)
764 {
765 	struct dsa_switch *ds;
766 	struct xrs700x *priv;
767 
768 	ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
769 	if (!ds)
770 		return NULL;
771 
772 	ds->dev = base;
773 
774 	priv = devm_kzalloc(base, sizeof(*priv), GFP_KERNEL);
775 	if (!priv)
776 		return NULL;
777 
778 	INIT_DELAYED_WORK(&priv->mib_work, xrs700x_mib_work);
779 
780 	ds->ops = &xrs700x_ops;
781 	ds->phylink_mac_ops = &xrs700x_phylink_mac_ops;
782 	ds->priv = priv;
783 	priv->dev = base;
784 
785 	priv->ds = ds;
786 	priv->priv = devpriv;
787 
788 	return priv;
789 }
790 EXPORT_SYMBOL(xrs700x_switch_alloc);
791 
792 static int xrs700x_alloc_port_mib(struct xrs700x *priv, int port)
793 {
794 	struct xrs700x_port *p = &priv->ports[port];
795 
796 	p->mib_data = devm_kcalloc(priv->dev, ARRAY_SIZE(xrs700x_mibs),
797 				   sizeof(*p->mib_data), GFP_KERNEL);
798 	if (!p->mib_data)
799 		return -ENOMEM;
800 
801 	mutex_init(&p->mib_mutex);
802 	u64_stats_init(&p->syncp);
803 
804 	return 0;
805 }
806 
807 int xrs700x_switch_register(struct xrs700x *priv)
808 {
809 	int ret;
810 	int i;
811 
812 	ret = xrs700x_detect(priv);
813 	if (ret)
814 		return ret;
815 
816 	ret = xrs700x_setup_regmap_range(priv);
817 	if (ret)
818 		return ret;
819 
820 	priv->ports = devm_kcalloc(priv->dev, priv->ds->num_ports,
821 				   sizeof(*priv->ports), GFP_KERNEL);
822 	if (!priv->ports)
823 		return -ENOMEM;
824 
825 	for (i = 0; i < priv->ds->num_ports; i++) {
826 		ret = xrs700x_alloc_port_mib(priv, i);
827 		if (ret)
828 			return ret;
829 	}
830 
831 	return dsa_register_switch(priv->ds);
832 }
833 EXPORT_SYMBOL(xrs700x_switch_register);
834 
835 void xrs700x_switch_remove(struct xrs700x *priv)
836 {
837 	dsa_unregister_switch(priv->ds);
838 }
839 EXPORT_SYMBOL(xrs700x_switch_remove);
840 
841 void xrs700x_switch_shutdown(struct xrs700x *priv)
842 {
843 	dsa_switch_shutdown(priv->ds);
844 }
845 EXPORT_SYMBOL(xrs700x_switch_shutdown);
846 
847 MODULE_AUTHOR("George McCollister <george.mccollister@gmail.com>");
848 MODULE_DESCRIPTION("Arrow SpeedChips XRS700x DSA driver");
849 MODULE_LICENSE("GPL v2");
850