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