xref: /linux/drivers/net/dsa/motorcomm/chip.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Motorcomm YT921x Switch
4  *
5  * Should work on YT9213/YT9214/YT9215/YT9218, but only tested on YT9215+SGMII,
6  * be sure to do your own checks before porting to another chip.
7  *
8  * Copyright (c) 2025 David Yang
9  */
10 
11 #include <linux/dcbnl.h>
12 #include <linux/etherdevice.h>
13 #include <linux/if_bridge.h>
14 #include <linux/if_hsr.h>
15 #include <linux/if_vlan.h>
16 #include <linux/mdio.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_net.h>
21 #include <linux/sort.h>
22 #include <linux/u64_stats_sync.h>
23 
24 #include <net/dsa.h>
25 #include <net/dscp.h>
26 #include <net/ieee8021q.h>
27 
28 #include "chip.h"
29 #include "leds.h"
30 #include "mdio_bus.h"
31 #include "mib.h"
32 #include "pcs.h"
33 #include "smi.h"
34 #include "tc.h"
35 
36 static const struct yt921x_info yt921x_infos[] = {
37 	{
38 		"YT9215SC", YT9215_MAJOR, 1, 0,
39 		GENMASK(4, 0),
40 		BIT(9),
41 		BIT(8) | BIT(9),
42 	},
43 	{
44 		"YT9215S", YT9215_MAJOR, 2, 0,
45 		GENMASK(4, 0),
46 		BIT(9),
47 		BIT(8),
48 	},
49 	{
50 		"YT9215RB", YT9215_MAJOR, 3, 0,
51 		GENMASK(4, 0),
52 		BIT(8) | BIT(9),
53 		0,
54 	},
55 	{
56 		"YT9214NB", YT9215_MAJOR, 3, 2,
57 		BIT(1) | BIT(3),
58 		BIT(9),
59 		BIT(8),
60 	},
61 	{
62 		"YT9213NB", YT9215_MAJOR, 3, 3,
63 		BIT(1) | BIT(3),
64 		BIT(9),
65 		BIT(9),
66 	},
67 	{
68 		"YT9218N", YT9218_MAJOR, 0, 0,
69 		GENMASK(7, 0),
70 		0,
71 		0,
72 	},
73 	{
74 		"YT9218MB", YT9218_MAJOR, 1, 0,
75 		GENMASK(7, 0),
76 		BIT(8) | BIT(9),
77 		BIT(8) | BIT(9),
78 	},
79 	{}
80 };
81 
82 #define YT921X_VID_UNWARE	4095
83 
84 struct yt921x_reg_mdio {
85 	struct mii_bus *bus;
86 	int addr;
87 	/* SWITCH_ID_1 / SWITCH_ID_0 of the device
88 	 *
89 	 * This is a way to multiplex multiple devices on the same MII phyaddr
90 	 * and should be configurable in DT. However, MDIO core simply doesn't
91 	 * allow multiple devices over one reg addr, so this is a fixed value
92 	 * for now until a solution is found.
93 	 *
94 	 * Keep this because we need switchid to form MII regaddrs anyway.
95 	 */
96 	unsigned char switchid;
97 };
98 
99 /* TODO: SPI/I2C */
100 
101 static int yt921x_reg_mdio_read(void *context, u32 reg, u32 *valp)
102 {
103 	struct yt921x_reg_mdio *mdio = context;
104 	struct mii_bus *bus = mdio->bus;
105 	int addr = mdio->addr;
106 	u32 reg_addr;
107 	u32 reg_data;
108 	u32 val;
109 	int res;
110 
111 	/* Hold the mdio bus lock to avoid (un)locking for 4 times */
112 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
113 
114 	reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
115 		   YT921X_SMI_READ;
116 	res = __mdiobus_write(bus, addr, reg_addr, (u16)(reg >> 16));
117 	if (res)
118 		goto end;
119 	res = __mdiobus_write(bus, addr, reg_addr, (u16)reg);
120 	if (res)
121 		goto end;
122 
123 	reg_data = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_DATA |
124 		   YT921X_SMI_READ;
125 	res = __mdiobus_read(bus, addr, reg_data);
126 	if (res < 0)
127 		goto end;
128 	val = (u16)res;
129 	res = __mdiobus_read(bus, addr, reg_data);
130 	if (res < 0)
131 		goto end;
132 	val = (val << 16) | (u16)res;
133 
134 	*valp = val;
135 	res = 0;
136 
137 end:
138 	mutex_unlock(&bus->mdio_lock);
139 	return res;
140 }
141 
142 static int yt921x_reg_mdio_write(void *context, u32 reg, u32 val)
143 {
144 	struct yt921x_reg_mdio *mdio = context;
145 	struct mii_bus *bus = mdio->bus;
146 	int addr = mdio->addr;
147 	u32 reg_addr;
148 	u32 reg_data;
149 	int res;
150 
151 	mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
152 
153 	reg_addr = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_ADDR |
154 		   YT921X_SMI_WRITE;
155 	res = __mdiobus_write(bus, addr, reg_addr, (u16)(reg >> 16));
156 	if (res)
157 		goto end;
158 	res = __mdiobus_write(bus, addr, reg_addr, (u16)reg);
159 	if (res)
160 		goto end;
161 
162 	reg_data = YT921X_SMI_SWITCHID(mdio->switchid) | YT921X_SMI_DATA |
163 		   YT921X_SMI_WRITE;
164 	res = __mdiobus_write(bus, addr, reg_data, (u16)(val >> 16));
165 	if (res)
166 		goto end;
167 	res = __mdiobus_write(bus, addr, reg_data, (u16)val);
168 	if (res)
169 		goto end;
170 
171 	res = 0;
172 
173 end:
174 	mutex_unlock(&bus->mdio_lock);
175 	return res;
176 }
177 
178 static const struct yt921x_reg_ops yt921x_reg_ops_mdio = {
179 	.read = yt921x_reg_mdio_read,
180 	.write = yt921x_reg_mdio_write,
181 };
182 
183 /* TODO: SPI/I2C */
184 
185 static int
186 yt921x_set_eee(struct yt921x_priv *priv, int port, struct ethtool_keee *e)
187 {
188 	/* Poor datasheet for EEE operations; don't ask if you are confused */
189 
190 	bool enable = e->eee_enabled;
191 	u16 new_mask;
192 	int res;
193 
194 	/* Enable / disable global EEE */
195 	new_mask = priv->eee_ports_mask;
196 	new_mask &= ~BIT(port);
197 	new_mask |= !enable ? 0 : BIT(port);
198 
199 	if (!!new_mask != !!priv->eee_ports_mask) {
200 		res = yt921x_reg_toggle_bits(priv, YT921X_PON_STRAP_FUNC,
201 					     YT921X_PON_STRAP_EEE, !!new_mask);
202 		if (res)
203 			return res;
204 		res = yt921x_reg_toggle_bits(priv, YT921X_PON_STRAP_VAL,
205 					     YT921X_PON_STRAP_EEE, !!new_mask);
206 		if (res)
207 			return res;
208 	}
209 
210 	priv->eee_ports_mask = new_mask;
211 
212 	/* Enable / disable port EEE */
213 	res = yt921x_reg_toggle_bits(priv, YT921X_EEE_CTRL,
214 				     YT921X_EEE_CTRL_ENn(port), enable);
215 	if (res)
216 		return res;
217 	res = yt921x_reg_toggle_bits(priv, YT921X_EEEn_VAL(port),
218 				     YT921X_EEE_VAL_DATA, enable);
219 	if (res)
220 		return res;
221 
222 	return 0;
223 }
224 
225 static int
226 yt921x_dsa_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_keee *e)
227 {
228 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
229 	int res;
230 
231 	mutex_lock(&priv->reg_lock);
232 	res = yt921x_set_eee(priv, port, e);
233 	mutex_unlock(&priv->reg_lock);
234 
235 	return res;
236 }
237 
238 static int
239 yt921x_dsa_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
240 {
241 	/* Only serves as packet filter, since the frame size is always set to
242 	 * maximum after reset
243 	 */
244 
245 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
246 	struct dsa_port *dp = dsa_to_port(ds, port);
247 	int frame_size;
248 	int res;
249 
250 	frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN;
251 	if (dsa_port_is_cpu(dp))
252 		frame_size += YT921X_TAG_LEN;
253 
254 	mutex_lock(&priv->reg_lock);
255 	res = yt921x_reg_update_bits(priv, YT921X_MACn_FRAME(port),
256 				     YT921X_MAC_FRAME_SIZE_M,
257 				     YT921X_MAC_FRAME_SIZE(frame_size));
258 	mutex_unlock(&priv->reg_lock);
259 
260 	return res;
261 }
262 
263 static int yt921x_dsa_port_max_mtu(struct dsa_switch *ds, int port)
264 {
265 	/* Only called for user ports, exclude tag len here */
266 	return YT921X_FRAME_SIZE_MAX - ETH_HLEN - ETH_FCS_LEN - YT921X_TAG_LEN;
267 }
268 
269 static int yt921x_lag_hash(struct yt921x_priv *priv, u32 ctrl, bool unique_lag,
270 			   struct netlink_ext_ack *extack)
271 {
272 	u32 val;
273 	int res;
274 
275 	/* Hash Mode is global. Make sure the same Hash Mode is set to all the
276 	 * 2 possible lags.
277 	 * If we are the unique LAG we can set whatever hash mode we want.
278 	 * To change hash mode it's needed to remove all LAG and change the mode
279 	 * with the latest.
280 	 */
281 	if (unique_lag) {
282 		res = yt921x_reg_write(priv, YT921X_LAG_HASH, ctrl);
283 		if (res)
284 			return res;
285 	} else {
286 		res = yt921x_reg_read(priv, YT921X_LAG_HASH, &val);
287 		if (res)
288 			return res;
289 
290 		if (val != ctrl) {
291 			NL_SET_ERR_MSG_MOD(extack,
292 					   "Mismatched Hash Mode across different lags is not supported");
293 			return -EOPNOTSUPP;
294 		}
295 	}
296 
297 	return 0;
298 }
299 
300 static int yt921x_lag_set(struct yt921x_priv *priv, u8 index, u16 ports_mask)
301 {
302 	unsigned long targets_mask = ports_mask;
303 	unsigned int cnt;
304 	u32 ctrl;
305 	int port;
306 	int res;
307 
308 	cnt = 0;
309 	for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
310 		ctrl = YT921X_LAG_MEMBER_PORT(port);
311 		res = yt921x_reg_write(priv, YT921X_LAG_MEMBERnm(index, cnt),
312 				       ctrl);
313 		if (res)
314 			return res;
315 
316 		cnt++;
317 	}
318 
319 	ctrl = YT921X_LAG_GROUP_PORTS(ports_mask) |
320 	       YT921X_LAG_GROUP_MEMBER_NUM(cnt);
321 	return yt921x_reg_write(priv, YT921X_LAG_GROUPn(index), ctrl);
322 }
323 
324 static int
325 yt921x_dsa_port_lag_leave(struct dsa_switch *ds, int port, struct dsa_lag lag)
326 {
327 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
328 	struct dsa_port *dp;
329 	u32 ctrl;
330 	int res;
331 
332 	if (!lag.id)
333 		return -EINVAL;
334 
335 	ctrl = 0;
336 	dsa_lag_foreach_port(dp, ds->dst, &lag)
337 		ctrl |= BIT(dp->index);
338 
339 	mutex_lock(&priv->reg_lock);
340 	res = yt921x_lag_set(priv, lag.id - 1, ctrl);
341 	mutex_unlock(&priv->reg_lock);
342 
343 	return res;
344 }
345 
346 static int
347 yt921x_dsa_port_lag_check(struct dsa_switch *ds, struct dsa_lag lag,
348 			  struct netdev_lag_upper_info *info,
349 			  struct netlink_ext_ack *extack)
350 {
351 	unsigned int members;
352 	struct dsa_port *dp;
353 
354 	if (!lag.id)
355 		return -EINVAL;
356 
357 	members = 0;
358 	dsa_lag_foreach_port(dp, ds->dst, &lag)
359 		/* Includes the port joining the LAG */
360 		members++;
361 
362 	if (members > YT921X_LAG_PORT_NUM) {
363 		NL_SET_ERR_MSG_MOD(extack,
364 				   "Cannot offload more than 4 LAG ports");
365 		return -EOPNOTSUPP;
366 	}
367 
368 	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
369 		NL_SET_ERR_MSG_MOD(extack,
370 				   "Can only offload LAG using hash TX type");
371 		return -EOPNOTSUPP;
372 	}
373 
374 	if (info->hash_type != NETDEV_LAG_HASH_L2 &&
375 	    info->hash_type != NETDEV_LAG_HASH_L23 &&
376 	    info->hash_type != NETDEV_LAG_HASH_L34) {
377 		NL_SET_ERR_MSG_MOD(extack,
378 				   "Can only offload L2 or L2+L3 or L3+L4 TX hash");
379 		return -EOPNOTSUPP;
380 	}
381 
382 	return 0;
383 }
384 
385 static int
386 yt921x_dsa_port_lag_join(struct dsa_switch *ds, int port, struct dsa_lag lag,
387 			 struct netdev_lag_upper_info *info,
388 			 struct netlink_ext_ack *extack)
389 {
390 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
391 	struct dsa_port *dp;
392 	bool unique_lag;
393 	unsigned int i;
394 	u32 ctrl;
395 	int res;
396 
397 	res = yt921x_dsa_port_lag_check(ds, lag, info, extack);
398 	if (res)
399 		return res;
400 
401 	ctrl = 0;
402 	switch (info->hash_type) {
403 	case NETDEV_LAG_HASH_L34:
404 		ctrl |= YT921X_LAG_HASH_IP_DST;
405 		ctrl |= YT921X_LAG_HASH_IP_SRC;
406 		ctrl |= YT921X_LAG_HASH_IP_PROTO;
407 
408 		ctrl |= YT921X_LAG_HASH_L4_DPORT;
409 		ctrl |= YT921X_LAG_HASH_L4_SPORT;
410 		break;
411 	case NETDEV_LAG_HASH_L23:
412 		ctrl |= YT921X_LAG_HASH_MAC_DA;
413 		ctrl |= YT921X_LAG_HASH_MAC_SA;
414 
415 		ctrl |= YT921X_LAG_HASH_IP_DST;
416 		ctrl |= YT921X_LAG_HASH_IP_SRC;
417 		ctrl |= YT921X_LAG_HASH_IP_PROTO;
418 		break;
419 	case NETDEV_LAG_HASH_L2:
420 		ctrl |= YT921X_LAG_HASH_MAC_DA;
421 		ctrl |= YT921X_LAG_HASH_MAC_SA;
422 		break;
423 	default:
424 		return -EOPNOTSUPP;
425 	}
426 
427 	/* Check if we are the unique configured LAG */
428 	unique_lag = true;
429 	dsa_lags_foreach_id(i, ds->dst)
430 		if (i != lag.id && dsa_lag_by_id(ds->dst, i)) {
431 			unique_lag = false;
432 			break;
433 		}
434 
435 	mutex_lock(&priv->reg_lock);
436 	do {
437 		res = yt921x_lag_hash(priv, ctrl, unique_lag, extack);
438 		if (res)
439 			break;
440 
441 		ctrl = 0;
442 		dsa_lag_foreach_port(dp, ds->dst, &lag)
443 			ctrl |= BIT(dp->index);
444 		res = yt921x_lag_set(priv, lag.id - 1, ctrl);
445 	} while (0);
446 	mutex_unlock(&priv->reg_lock);
447 
448 	return res;
449 }
450 
451 static int yt921x_fdb_wait(struct yt921x_priv *priv, u32 *valp)
452 {
453 	struct device *dev = yt921x_priv_to_device(priv);
454 	u32 val = YT921X_FDB_RESULT_DONE;
455 	int res;
456 
457 	res = yt921x_reg_wait(priv, YT921X_FDB_RESULT, YT921X_FDB_RESULT_DONE,
458 			      &val);
459 	if (res) {
460 		dev_err(dev, "FDB probably stuck\n");
461 		return res;
462 	}
463 
464 	*valp = val;
465 	return 0;
466 }
467 
468 static int
469 yt921x_fdb_in01(struct yt921x_priv *priv, const unsigned char *addr,
470 		u16 vid, u32 ctrl1)
471 {
472 	u32 ctrl;
473 	int res;
474 
475 	ctrl = ethaddr_hi4_to_u32(addr);
476 	res = yt921x_reg_write(priv, YT921X_FDB_IN0, ctrl);
477 	if (res)
478 		return res;
479 
480 	ctrl = ctrl1 | YT921X_FDB_IO1_FID(vid) | ethaddr_lo2_to_u32(addr);
481 	return yt921x_reg_write(priv, YT921X_FDB_IN1, ctrl);
482 }
483 
484 static int
485 yt921x_fdb_has(struct yt921x_priv *priv, const unsigned char *addr, u16 vid,
486 	       u16 *indexp)
487 {
488 	u32 ctrl;
489 	u32 val;
490 	int res;
491 
492 	res = yt921x_fdb_in01(priv, addr, vid, 0);
493 	if (res)
494 		return res;
495 
496 	ctrl = 0;
497 	res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
498 	if (res)
499 		return res;
500 
501 	ctrl = YT921X_FDB_OP_OP_GET_ONE | YT921X_FDB_OP_START;
502 	res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
503 	if (res)
504 		return res;
505 
506 	res = yt921x_fdb_wait(priv, &val);
507 	if (res)
508 		return res;
509 	if (val & YT921X_FDB_RESULT_NOTFOUND) {
510 		*indexp = YT921X_FDB_NUM;
511 		return 0;
512 	}
513 
514 	*indexp = FIELD_GET(YT921X_FDB_RESULT_INDEX_M, val);
515 	return 0;
516 }
517 
518 static int
519 yt921x_fdb_read(struct yt921x_priv *priv, unsigned char *addr, u16 *vidp,
520 		u16 *ports_maskp, u16 *indexp, u8 *statusp)
521 {
522 	struct device *dev = yt921x_priv_to_device(priv);
523 	u16 index;
524 	u32 data0;
525 	u32 data1;
526 	u32 data2;
527 	u32 val;
528 	int res;
529 
530 	res = yt921x_fdb_wait(priv, &val);
531 	if (res)
532 		return res;
533 	if (val & YT921X_FDB_RESULT_NOTFOUND) {
534 		*ports_maskp = 0;
535 		return 0;
536 	}
537 	index = FIELD_GET(YT921X_FDB_RESULT_INDEX_M, val);
538 
539 	res = yt921x_reg_read(priv, YT921X_FDB_OUT1, &data1);
540 	if (res)
541 		return res;
542 	if ((data1 & YT921X_FDB_IO1_STATUS_M) ==
543 	    YT921X_FDB_IO1_STATUS_INVALID) {
544 		*ports_maskp = 0;
545 		return 0;
546 	}
547 
548 	res = yt921x_reg_read(priv, YT921X_FDB_OUT0, &data0);
549 	if (res)
550 		return res;
551 	res = yt921x_reg_read(priv, YT921X_FDB_OUT2, &data2);
552 	if (res)
553 		return res;
554 
555 	addr[0] = data0 >> 24;
556 	addr[1] = data0 >> 16;
557 	addr[2] = data0 >> 8;
558 	addr[3] = data0;
559 	addr[4] = data1 >> 8;
560 	addr[5] = data1;
561 	*vidp = FIELD_GET(YT921X_FDB_IO1_FID_M, data1);
562 	*indexp = index;
563 	*ports_maskp = FIELD_GET(YT921X_FDB_IO2_EGR_PORTS_M, data2);
564 	*statusp = FIELD_GET(YT921X_FDB_IO1_STATUS_M, data1);
565 
566 	dev_dbg(dev,
567 		"%s: index 0x%x, mac %02x:%02x:%02x:%02x:%02x:%02x, vid %d, ports 0x%x, status %d\n",
568 		__func__, *indexp, addr[0], addr[1], addr[2], addr[3],
569 		addr[4], addr[5], *vidp, *ports_maskp, *statusp);
570 	return 0;
571 }
572 
573 static int
574 yt921x_fdb_dump(struct yt921x_priv *priv, u16 ports_mask,
575 		dsa_fdb_dump_cb_t *cb, void *data)
576 {
577 	unsigned char addr[ETH_ALEN];
578 	u8 status;
579 	u16 pmask;
580 	u16 index;
581 	u32 ctrl;
582 	u16 vid;
583 	int res;
584 
585 	ctrl = YT921X_FDB_OP_INDEX(0) | YT921X_FDB_OP_MODE_INDEX |
586 	       YT921X_FDB_OP_OP_GET_ONE | YT921X_FDB_OP_START;
587 	res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
588 	if (res)
589 		return res;
590 	res = yt921x_fdb_read(priv, addr, &vid, &pmask, &index, &status);
591 	if (res)
592 		return res;
593 	if ((pmask & ports_mask) && !is_multicast_ether_addr(addr)) {
594 		res = cb(addr, vid,
595 			 status == YT921X_FDB_ENTRY_STATUS_STATIC, data);
596 		if (res)
597 			return res;
598 	}
599 
600 	ctrl = YT921X_FDB_IO2_EGR_PORTS(ports_mask);
601 	res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
602 	if (res)
603 		return res;
604 
605 	index = 0;
606 	do {
607 		ctrl = YT921X_FDB_OP_INDEX(index) | YT921X_FDB_OP_MODE_INDEX |
608 		       YT921X_FDB_OP_NEXT_TYPE_UCAST_PORT |
609 		       YT921X_FDB_OP_OP_GET_NEXT | YT921X_FDB_OP_START;
610 		res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
611 		if (res)
612 			return res;
613 
614 		res = yt921x_fdb_read(priv, addr, &vid, &pmask, &index,
615 				      &status);
616 		if (res)
617 			return res;
618 		if (!pmask)
619 			break;
620 
621 		if ((pmask & ports_mask) && !is_multicast_ether_addr(addr)) {
622 			res = cb(addr, vid,
623 				 status == YT921X_FDB_ENTRY_STATUS_STATIC,
624 				 data);
625 			if (res)
626 				return res;
627 		}
628 
629 		/* Never call GET_NEXT with 4095, otherwise it will hang
630 		 * forever until a reset!
631 		 */
632 	} while (index < YT921X_FDB_NUM - 1);
633 
634 	return 0;
635 }
636 
637 static int
638 yt921x_fdb_flush_raw(struct yt921x_priv *priv, u16 ports_mask, u16 vid,
639 		     bool flush_static)
640 {
641 	u32 ctrl;
642 	u32 val;
643 	int res;
644 
645 	if (vid < 4096) {
646 		ctrl = YT921X_FDB_IO1_FID(vid);
647 		res = yt921x_reg_write(priv, YT921X_FDB_IN1, ctrl);
648 		if (res)
649 			return res;
650 	}
651 
652 	ctrl = YT921X_FDB_IO2_EGR_PORTS(ports_mask);
653 	res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
654 	if (res)
655 		return res;
656 
657 	ctrl = YT921X_FDB_OP_OP_FLUSH | YT921X_FDB_OP_START;
658 	if (vid >= 4096)
659 		ctrl |= YT921X_FDB_OP_FLUSH_PORT;
660 	else
661 		ctrl |= YT921X_FDB_OP_FLUSH_PORT_VID;
662 	if (flush_static)
663 		ctrl |= YT921X_FDB_OP_FLUSH_STATIC;
664 	res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
665 	if (res)
666 		return res;
667 
668 	res = yt921x_fdb_wait(priv, &val);
669 	if (res)
670 		return res;
671 
672 	return 0;
673 }
674 
675 static int
676 yt921x_fdb_flush_port(struct yt921x_priv *priv, int port, bool flush_static)
677 {
678 	return yt921x_fdb_flush_raw(priv, BIT(port), 4096, flush_static);
679 }
680 
681 static int
682 yt921x_fdb_add_index_in12(struct yt921x_priv *priv, u16 index, u32 ctrl1,
683 			  u32 ctrl2)
684 {
685 	u32 ctrl;
686 	u32 val;
687 	int res;
688 
689 	res = yt921x_reg_write(priv, YT921X_FDB_IN1, ctrl1);
690 	if (res)
691 		return res;
692 	res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl2);
693 	if (res)
694 		return res;
695 
696 	ctrl = YT921X_FDB_OP_INDEX(index) | YT921X_FDB_OP_MODE_INDEX |
697 	       YT921X_FDB_OP_OP_ADD | YT921X_FDB_OP_START;
698 	res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
699 	if (res)
700 		return res;
701 
702 	return yt921x_fdb_wait(priv, &val);
703 }
704 
705 static int
706 yt921x_fdb_add(struct yt921x_priv *priv, const unsigned char *addr, u16 vid,
707 	       u16 ports_mask)
708 {
709 	u32 ctrl;
710 	u32 val;
711 	int res;
712 
713 	ctrl = YT921X_FDB_IO1_STATUS_STATIC;
714 	res = yt921x_fdb_in01(priv, addr, vid, ctrl);
715 	if (res)
716 		return res;
717 
718 	ctrl = YT921X_FDB_IO2_EGR_PORTS(ports_mask);
719 	res = yt921x_reg_write(priv, YT921X_FDB_IN2, ctrl);
720 	if (res)
721 		return res;
722 
723 	ctrl = YT921X_FDB_OP_OP_ADD | YT921X_FDB_OP_START;
724 	res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
725 	if (res)
726 		return res;
727 
728 	return yt921x_fdb_wait(priv, &val);
729 }
730 
731 static int
732 yt921x_fdb_leave(struct yt921x_priv *priv, const unsigned char *addr,
733 		 u16 vid, u16 ports_mask)
734 {
735 	u16 index;
736 	u32 ctrl1;
737 	u32 ctrl2;
738 	u32 ctrl;
739 	u32 val2;
740 	u32 val;
741 	int res;
742 
743 	/* Check for presence */
744 	res = yt921x_fdb_has(priv, addr, vid, &index);
745 	if (res)
746 		return res;
747 	if (index >= YT921X_FDB_NUM)
748 		return 0;
749 
750 	/* Check if action required */
751 	res = yt921x_reg_read(priv, YT921X_FDB_OUT2, &val2);
752 	if (res)
753 		return res;
754 
755 	ctrl2 = val2 & ~YT921X_FDB_IO2_EGR_PORTS(ports_mask);
756 	if (ctrl2 == val2)
757 		return 0;
758 	if (!(ctrl2 & YT921X_FDB_IO2_EGR_PORTS_M)) {
759 		ctrl = YT921X_FDB_OP_OP_DEL | YT921X_FDB_OP_START;
760 		res = yt921x_reg_write(priv, YT921X_FDB_OP, ctrl);
761 		if (res)
762 			return res;
763 
764 		return yt921x_fdb_wait(priv, &val);
765 	}
766 
767 	res = yt921x_reg_read(priv, YT921X_FDB_OUT1, &ctrl1);
768 	if (res)
769 		return res;
770 
771 	return yt921x_fdb_add_index_in12(priv, index, ctrl1, ctrl2);
772 }
773 
774 static int
775 yt921x_fdb_join(struct yt921x_priv *priv, const unsigned char *addr, u16 vid,
776 		u16 ports_mask)
777 {
778 	u16 index;
779 	u32 ctrl1;
780 	u32 ctrl2;
781 	u32 val1;
782 	u32 val2;
783 	int res;
784 
785 	/* Check for presence */
786 	res = yt921x_fdb_has(priv, addr, vid, &index);
787 	if (res)
788 		return res;
789 	if (index >= YT921X_FDB_NUM)
790 		return yt921x_fdb_add(priv, addr, vid, ports_mask);
791 
792 	/* Check if action required */
793 	res = yt921x_reg_read(priv, YT921X_FDB_OUT1, &val1);
794 	if (res)
795 		return res;
796 	res = yt921x_reg_read(priv, YT921X_FDB_OUT2, &val2);
797 	if (res)
798 		return res;
799 
800 	ctrl1 = val1 & ~YT921X_FDB_IO1_STATUS_M;
801 	ctrl1 |= YT921X_FDB_IO1_STATUS_STATIC;
802 	ctrl2 = val2 | YT921X_FDB_IO2_EGR_PORTS(ports_mask);
803 	if (ctrl1 == val1 && ctrl2 == val2)
804 		return 0;
805 
806 	return yt921x_fdb_add_index_in12(priv, index, ctrl1, ctrl2);
807 }
808 
809 static int
810 yt921x_dsa_port_fdb_dump(struct dsa_switch *ds, int port,
811 			 dsa_fdb_dump_cb_t *cb, void *data)
812 {
813 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
814 	int res;
815 
816 	mutex_lock(&priv->reg_lock);
817 	/* Hardware FDB is shared for fdb and mdb, "bridge fdb show"
818 	 * only wants to see unicast
819 	 */
820 	res = yt921x_fdb_dump(priv, BIT(port), cb, data);
821 	mutex_unlock(&priv->reg_lock);
822 
823 	return res;
824 }
825 
826 static void yt921x_dsa_port_fast_age(struct dsa_switch *ds, int port)
827 {
828 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
829 	struct device *dev = yt921x_priv_to_device(priv);
830 	int res;
831 
832 	mutex_lock(&priv->reg_lock);
833 	res = yt921x_fdb_flush_port(priv, port, false);
834 	mutex_unlock(&priv->reg_lock);
835 
836 	if (res)
837 		dev_err(dev, "Failed to %s port %d: %i\n", "clear FDB for",
838 			port, res);
839 }
840 
841 static int
842 yt921x_dsa_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
843 {
844 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
845 	u32 ctrl;
846 	int res;
847 
848 	/* AGEING reg is set in 5s step */
849 	ctrl = clamp(msecs / 5000, 1, U16_MAX);
850 
851 	mutex_lock(&priv->reg_lock);
852 	res = yt921x_reg_write(priv, YT921X_AGEING, ctrl);
853 	mutex_unlock(&priv->reg_lock);
854 
855 	return res;
856 }
857 
858 static int
859 yt921x_dsa_port_fdb_del(struct dsa_switch *ds, int port,
860 			const unsigned char *addr, u16 vid, struct dsa_db db)
861 {
862 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
863 	int res;
864 
865 	mutex_lock(&priv->reg_lock);
866 	res = yt921x_fdb_leave(priv, addr, vid, BIT(port));
867 	mutex_unlock(&priv->reg_lock);
868 
869 	return res;
870 }
871 
872 static int
873 yt921x_dsa_port_fdb_add(struct dsa_switch *ds, int port,
874 			const unsigned char *addr, u16 vid, struct dsa_db db)
875 {
876 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
877 	int res;
878 
879 	mutex_lock(&priv->reg_lock);
880 	res = yt921x_fdb_join(priv, addr, vid, BIT(port));
881 	mutex_unlock(&priv->reg_lock);
882 
883 	return res;
884 }
885 
886 static int
887 yt921x_dsa_port_mdb_del(struct dsa_switch *ds, int port,
888 			const struct switchdev_obj_port_mdb *mdb,
889 			struct dsa_db db)
890 {
891 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
892 	const unsigned char *addr = mdb->addr;
893 	u16 vid = mdb->vid;
894 	int res;
895 
896 	mutex_lock(&priv->reg_lock);
897 	res = yt921x_fdb_leave(priv, addr, vid, BIT(port));
898 	mutex_unlock(&priv->reg_lock);
899 
900 	return res;
901 }
902 
903 static int
904 yt921x_dsa_port_mdb_add(struct dsa_switch *ds, int port,
905 			const struct switchdev_obj_port_mdb *mdb,
906 			struct dsa_db db)
907 {
908 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
909 	const unsigned char *addr = mdb->addr;
910 	u16 vid = mdb->vid;
911 	int res;
912 
913 	mutex_lock(&priv->reg_lock);
914 	res = yt921x_fdb_join(priv, addr, vid, BIT(port));
915 	mutex_unlock(&priv->reg_lock);
916 
917 	return res;
918 }
919 
920 static int
921 yt921x_vlan_aware_set(struct yt921x_priv *priv, int port, bool vlan_aware)
922 {
923 	u32 ctrl;
924 
925 	/* Abuse SVLAN for PCP parsing without polluting the FDB - it just works
926 	 * despite YT921X_VLAN_CTRL_SVLAN_EN never being set
927 	 */
928 	if (!vlan_aware)
929 		ctrl = YT921X_PORT_IGR_TPIDn_STAG(0);
930 	else
931 		ctrl = YT921X_PORT_IGR_TPIDn_CTAG(0);
932 	return yt921x_reg_write(priv, YT921X_PORTn_IGR_TPID(port), ctrl);
933 }
934 
935 static int
936 yt921x_port_set_pvid(struct yt921x_priv *priv, int port, u16 vid)
937 {
938 	u32 mask;
939 	u32 ctrl;
940 
941 	mask = YT921X_PORT_VLAN_CTRL_CVID_M;
942 	ctrl = YT921X_PORT_VLAN_CTRL_CVID(vid);
943 	return yt921x_reg_update_bits(priv, YT921X_PORTn_VLAN_CTRL(port),
944 				      mask, ctrl);
945 }
946 
947 static int
948 yt921x_vlan_filtering(struct yt921x_priv *priv, int port, bool vlan_filtering)
949 {
950 	struct dsa_port *dp = dsa_to_port(&priv->ds, port);
951 	struct net_device *bdev;
952 	u16 pvid;
953 	u32 mask;
954 	u32 ctrl;
955 	int res;
956 
957 	bdev = dsa_port_bridge_dev_get(dp);
958 
959 	if (!bdev || !vlan_filtering)
960 		pvid = YT921X_VID_UNWARE;
961 	else
962 		br_vlan_get_pvid(bdev, &pvid);
963 	res = yt921x_port_set_pvid(priv, port, pvid);
964 	if (res)
965 		return res;
966 
967 	mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_TAGGED |
968 	       YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
969 	ctrl = 0;
970 	/* Do not drop tagged frames here; let VLAN_IGR_FILTER do it */
971 	if (vlan_filtering && !pvid)
972 		ctrl |= YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
973 	res = yt921x_reg_update_bits(priv, YT921X_PORTn_VLAN_CTRL1(port),
974 				     mask, ctrl);
975 	if (res)
976 		return res;
977 
978 	res = yt921x_reg_toggle_bits(priv, YT921X_VLAN_IGR_FILTER,
979 				     YT921X_VLAN_IGR_FILTER_PORTn(port),
980 				     vlan_filtering);
981 	if (res)
982 		return res;
983 
984 	res = yt921x_vlan_aware_set(priv, port, vlan_filtering);
985 	if (res)
986 		return res;
987 
988 	return 0;
989 }
990 
991 static int yt921x_vlan_del(struct yt921x_priv *priv, int port, u16 vid)
992 {
993 	u32 masks[2];
994 
995 	masks[0] = YT921X_VLAN_CTRLa_PORTn(port);
996 	masks[1] = YT921X_VLAN_CTRLb_UNTAG_PORTn(port);
997 
998 	return yt921x_reg64_clear_bits(priv, YT921X_VLANn_CTRL(vid), masks);
999 }
1000 
1001 static int
1002 yt921x_vlan_add(struct yt921x_priv *priv, int port, u16 vid, bool untagged)
1003 {
1004 	u32 masks[2];
1005 	u32 ctrls[2];
1006 
1007 	masks[0] = YT921X_VLAN_CTRLa_PORTn(port) |
1008 		   YT921X_VLAN_CTRLa_PORTS(priv->cpu_ports_mask);
1009 	ctrls[0] = masks[0];
1010 
1011 	masks[1] = YT921X_VLAN_CTRLb_UNTAG_PORTn(port);
1012 	ctrls[1] = untagged ? masks[1] : 0;
1013 
1014 	return yt921x_reg64_update_bits(priv, YT921X_VLANn_CTRL(vid),
1015 					masks, ctrls);
1016 }
1017 
1018 static int
1019 yt921x_pvid_clear(struct yt921x_priv *priv, int port)
1020 {
1021 	struct dsa_port *dp = dsa_to_port(&priv->ds, port);
1022 	bool vlan_filtering;
1023 	u32 mask;
1024 	int res;
1025 
1026 	vlan_filtering = dsa_port_is_vlan_filtering(dp);
1027 
1028 	res = yt921x_port_set_pvid(priv, port,
1029 				   vlan_filtering ? 0 : YT921X_VID_UNWARE);
1030 	if (res)
1031 		return res;
1032 
1033 	if (vlan_filtering) {
1034 		mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
1035 		res = yt921x_reg_set_bits(priv, YT921X_PORTn_VLAN_CTRL1(port),
1036 					  mask);
1037 		if (res)
1038 			return res;
1039 	}
1040 
1041 	return 0;
1042 }
1043 
1044 static int
1045 yt921x_pvid_set(struct yt921x_priv *priv, int port, u16 vid)
1046 {
1047 	struct dsa_port *dp = dsa_to_port(&priv->ds, port);
1048 	bool vlan_filtering;
1049 	u32 mask;
1050 	int res;
1051 
1052 	vlan_filtering = dsa_port_is_vlan_filtering(dp);
1053 
1054 	if (vlan_filtering) {
1055 		res = yt921x_port_set_pvid(priv, port, vid);
1056 		if (res)
1057 			return res;
1058 	}
1059 
1060 	mask = YT921X_PORT_VLAN_CTRL1_CVLAN_DROP_UNTAGGED;
1061 	res = yt921x_reg_clear_bits(priv, YT921X_PORTn_VLAN_CTRL1(port), mask);
1062 	if (res)
1063 		return res;
1064 
1065 	return 0;
1066 }
1067 
1068 static int
1069 yt921x_dsa_port_vlan_filtering(struct dsa_switch *ds, int port,
1070 			       bool vlan_filtering,
1071 			       struct netlink_ext_ack *extack)
1072 {
1073 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1074 	int res;
1075 
1076 	if (dsa_is_cpu_port(ds, port))
1077 		return 0;
1078 
1079 	mutex_lock(&priv->reg_lock);
1080 	res = yt921x_vlan_filtering(priv, port, vlan_filtering);
1081 	mutex_unlock(&priv->reg_lock);
1082 
1083 	return res;
1084 }
1085 
1086 static int
1087 yt921x_dsa_port_vlan_del(struct dsa_switch *ds, int port,
1088 			 const struct switchdev_obj_port_vlan *vlan)
1089 {
1090 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1091 	u16 vid = vlan->vid;
1092 	u16 pvid;
1093 	int res;
1094 
1095 	if (dsa_is_cpu_port(ds, port))
1096 		return 0;
1097 
1098 	mutex_lock(&priv->reg_lock);
1099 	do {
1100 		struct dsa_port *dp = dsa_to_port(ds, port);
1101 		struct net_device *bdev;
1102 
1103 		res = yt921x_vlan_del(priv, port, vid);
1104 		if (res)
1105 			break;
1106 
1107 		bdev = dsa_port_bridge_dev_get(dp);
1108 		if (bdev) {
1109 			br_vlan_get_pvid(bdev, &pvid);
1110 			if (pvid == vid)
1111 				res = yt921x_pvid_clear(priv, port);
1112 		}
1113 	} while (0);
1114 	mutex_unlock(&priv->reg_lock);
1115 
1116 	return res;
1117 }
1118 
1119 static int
1120 yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port,
1121 			 const struct switchdev_obj_port_vlan *vlan,
1122 			 struct netlink_ext_ack *extack)
1123 {
1124 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1125 	u16 vid = vlan->vid;
1126 	u16 pvid;
1127 	int res;
1128 
1129 	/* CPU port is supposed to be a member of every VLAN; see
1130 	 * yt921x_vlan_add() and yt921x_port_setup()
1131 	 */
1132 	if (dsa_is_cpu_port(ds, port))
1133 		return 0;
1134 
1135 	mutex_lock(&priv->reg_lock);
1136 	do {
1137 		struct dsa_port *dp = dsa_to_port(ds, port);
1138 		struct net_device *bdev;
1139 
1140 		res = yt921x_vlan_add(priv, port, vid,
1141 				      vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1142 		if (res)
1143 			break;
1144 
1145 		bdev = dsa_port_bridge_dev_get(dp);
1146 		if (bdev) {
1147 			if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1148 				res = yt921x_pvid_set(priv, port, vid);
1149 			} else {
1150 				br_vlan_get_pvid(bdev, &pvid);
1151 				if (pvid == vid)
1152 					res = yt921x_pvid_clear(priv, port);
1153 			}
1154 		}
1155 	} while (0);
1156 	mutex_unlock(&priv->reg_lock);
1157 
1158 	return res;
1159 }
1160 
1161 static int yt921x_userport_standalone(struct yt921x_priv *priv, int port)
1162 {
1163 	u32 mask;
1164 	u32 ctrl;
1165 	int res;
1166 
1167 	ctrl = ~priv->cpu_ports_mask;
1168 	res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl);
1169 	if (res)
1170 		return res;
1171 
1172 	/* Turn off FDB learning to prevent FDB pollution */
1173 	mask = YT921X_PORT_LEARN_DIS;
1174 	res = yt921x_reg_set_bits(priv, YT921X_PORTn_LEARN(port), mask);
1175 	if (res)
1176 		return res;
1177 
1178 	/* Turn off VLAN awareness */
1179 	res = yt921x_vlan_aware_set(priv, port, false);
1180 	if (res)
1181 		return res;
1182 
1183 	/* Unrelated since learning is off and all packets are trapped;
1184 	 * set it anyway
1185 	 */
1186 	res = yt921x_port_set_pvid(priv, port, YT921X_VID_UNWARE);
1187 	if (res)
1188 		return res;
1189 
1190 	return 0;
1191 }
1192 
1193 static int yt921x_userport_bridge(struct yt921x_priv *priv, int port)
1194 {
1195 	u32 mask;
1196 	int res;
1197 
1198 	mask = YT921X_PORT_LEARN_DIS;
1199 	res = yt921x_reg_clear_bits(priv, YT921X_PORTn_LEARN(port), mask);
1200 	if (res)
1201 		return res;
1202 
1203 	return 0;
1204 }
1205 
1206 static int yt921x_isolate(struct yt921x_priv *priv, int port)
1207 {
1208 	u32 mask;
1209 	int res;
1210 
1211 	mask = BIT(port);
1212 	for (int i = 0; i < YT921X_PORT_NUM; i++) {
1213 		if ((BIT(i) & priv->cpu_ports_mask) || i == port)
1214 			continue;
1215 
1216 		res = yt921x_reg_set_bits(priv, YT921X_PORTn_ISOLATION(i),
1217 					  mask);
1218 		if (res)
1219 			return res;
1220 	}
1221 
1222 	return 0;
1223 }
1224 
1225 /* Make sure to include the CPU port in ports_mask, or your bridge will
1226  * not have it.
1227  */
1228 static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask)
1229 {
1230 	unsigned long targets_mask = ports_mask & ~priv->cpu_ports_mask;
1231 	u32 isolated_mask;
1232 	u32 ctrl;
1233 	int port;
1234 	int res;
1235 
1236 	isolated_mask = 0;
1237 	for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
1238 		struct yt921x_port *pp = &priv->ports[port];
1239 
1240 		if (pp->isolated)
1241 			isolated_mask |= BIT(port);
1242 	}
1243 
1244 	/* Block from non-cpu bridge ports ... */
1245 	for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) {
1246 		struct yt921x_port *pp = &priv->ports[port];
1247 
1248 		/* to non-bridge ports */
1249 		ctrl = ~ports_mask;
1250 		/* to isolated ports when isolated */
1251 		if (pp->isolated)
1252 			ctrl |= isolated_mask;
1253 		/* to itself when non-hairpin */
1254 		if (!pp->hairpin)
1255 			ctrl |= BIT(port);
1256 		else
1257 			ctrl &= ~BIT(port);
1258 
1259 		res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
1260 				       ctrl);
1261 		if (res)
1262 			return res;
1263 	}
1264 
1265 	return 0;
1266 }
1267 
1268 static int yt921x_bridge_leave(struct yt921x_priv *priv, int port)
1269 {
1270 	int res;
1271 
1272 	res = yt921x_userport_standalone(priv, port);
1273 	if (res)
1274 		return res;
1275 
1276 	res = yt921x_isolate(priv, port);
1277 	if (res)
1278 		return res;
1279 
1280 	return 0;
1281 }
1282 
1283 static int
1284 yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask)
1285 {
1286 	int res;
1287 
1288 	res = yt921x_userport_bridge(priv, port);
1289 	if (res)
1290 		return res;
1291 
1292 	res = yt921x_bridge(priv, ports_mask);
1293 	if (res)
1294 		return res;
1295 
1296 	return 0;
1297 }
1298 
1299 static int
1300 yt921x_bridge_flags(struct yt921x_priv *priv, int port,
1301 		    struct switchdev_brport_flags flags)
1302 {
1303 	struct yt921x_port *pp = &priv->ports[port];
1304 	bool do_flush;
1305 	u32 mask;
1306 	int res;
1307 
1308 	if (flags.mask & BR_LEARNING) {
1309 		bool learning = flags.val & BR_LEARNING;
1310 
1311 		mask = YT921X_PORT_LEARN_DIS;
1312 		res = yt921x_reg_toggle_bits(priv, YT921X_PORTn_LEARN(port),
1313 					     mask, !learning);
1314 		if (res)
1315 			return res;
1316 	}
1317 
1318 	/* BR_FLOOD, BR_MCAST_FLOOD: see the comment where ACT_UNK_ACTn_TRAP
1319 	 * is set
1320 	 */
1321 
1322 	/* BR_BCAST_FLOOD: we can filter bcast, but cannot trap them */
1323 
1324 	do_flush = false;
1325 	if (flags.mask & BR_HAIRPIN_MODE) {
1326 		pp->hairpin = flags.val & BR_HAIRPIN_MODE;
1327 		do_flush = true;
1328 	}
1329 	if (flags.mask & BR_ISOLATED) {
1330 		pp->isolated = flags.val & BR_ISOLATED;
1331 		do_flush = true;
1332 	}
1333 	if (do_flush) {
1334 		struct dsa_switch *ds = &priv->ds;
1335 		struct dsa_port *dp = dsa_to_port(ds, port);
1336 		struct net_device *bdev;
1337 
1338 		bdev = dsa_port_bridge_dev_get(dp);
1339 		if (bdev) {
1340 			u32 ports_mask;
1341 
1342 			ports_mask = dsa_bridge_ports(ds, bdev);
1343 			ports_mask |= priv->cpu_ports_mask;
1344 			res = yt921x_bridge(priv, ports_mask);
1345 			if (res)
1346 				return res;
1347 		}
1348 	}
1349 
1350 	return 0;
1351 }
1352 
1353 static int
1354 yt921x_dsa_port_pre_bridge_flags(struct dsa_switch *ds, int port,
1355 				 struct switchdev_brport_flags flags,
1356 				 struct netlink_ext_ack *extack)
1357 {
1358 	if (flags.mask & ~(BR_HAIRPIN_MODE | BR_LEARNING | BR_FLOOD |
1359 			   BR_MCAST_FLOOD | BR_ISOLATED))
1360 		return -EINVAL;
1361 	return 0;
1362 }
1363 
1364 static int
1365 yt921x_dsa_port_bridge_flags(struct dsa_switch *ds, int port,
1366 			     struct switchdev_brport_flags flags,
1367 			     struct netlink_ext_ack *extack)
1368 {
1369 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1370 	int res;
1371 
1372 	if (dsa_is_cpu_port(ds, port))
1373 		return 0;
1374 
1375 	mutex_lock(&priv->reg_lock);
1376 	res = yt921x_bridge_flags(priv, port, flags);
1377 	mutex_unlock(&priv->reg_lock);
1378 
1379 	return res;
1380 }
1381 
1382 static void
1383 yt921x_dsa_port_bridge_leave(struct dsa_switch *ds, int port,
1384 			     struct dsa_bridge bridge)
1385 {
1386 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1387 	struct device *dev = yt921x_priv_to_device(priv);
1388 	int res;
1389 
1390 	if (dsa_is_cpu_port(ds, port))
1391 		return;
1392 
1393 	mutex_lock(&priv->reg_lock);
1394 	res = yt921x_bridge_leave(priv, port);
1395 	mutex_unlock(&priv->reg_lock);
1396 
1397 	if (res)
1398 		dev_err(dev, "Failed to %s port %d: %i\n", "unbridge",
1399 			port, res);
1400 }
1401 
1402 static int
1403 yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port,
1404 			    struct dsa_bridge bridge, bool *tx_fwd_offload,
1405 			    struct netlink_ext_ack *extack)
1406 {
1407 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1408 	struct yt921x_port *pp = &priv->ports[port];
1409 	struct net_device *brport_dev;
1410 	struct dsa_port *dp;
1411 	u16 ports_mask;
1412 	int res;
1413 
1414 	if (dsa_is_cpu_port(ds, port))
1415 		return 0;
1416 
1417 	ports_mask = dsa_bridge_ports(ds, bridge.dev);
1418 	ports_mask |= priv->cpu_ports_mask;
1419 
1420 	dp = dsa_to_port(ds, port);
1421 	brport_dev = dsa_port_to_bridge_port(dp);
1422 
1423 	mutex_lock(&priv->reg_lock);
1424 	pp->isolated = br_port_flag_is_set(brport_dev, BR_ISOLATED);
1425 	pp->hairpin = br_port_flag_is_set(brport_dev, BR_HAIRPIN_MODE);
1426 	res = yt921x_bridge_join(priv, port, ports_mask);
1427 	mutex_unlock(&priv->reg_lock);
1428 
1429 	return res;
1430 }
1431 
1432 static int
1433 yt921x_dsa_port_mst_state_set(struct dsa_switch *ds, int port,
1434 			      const struct switchdev_mst_state *st)
1435 {
1436 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1437 	u32 mask;
1438 	u32 ctrl;
1439 	int res;
1440 
1441 	mask = YT921X_STP_PORTn_M(port);
1442 	switch (st->state) {
1443 	case BR_STATE_DISABLED:
1444 		ctrl = YT921X_STP_PORTn_DISABLED(port);
1445 		break;
1446 	case BR_STATE_LISTENING:
1447 	case BR_STATE_LEARNING:
1448 		ctrl = YT921X_STP_PORTn_LEARNING(port);
1449 		break;
1450 	case BR_STATE_FORWARDING:
1451 	default:
1452 		ctrl = YT921X_STP_PORTn_FORWARD(port);
1453 		break;
1454 	case BR_STATE_BLOCKING:
1455 		ctrl = YT921X_STP_PORTn_BLOCKING(port);
1456 		break;
1457 	}
1458 
1459 	mutex_lock(&priv->reg_lock);
1460 	res = yt921x_reg_update_bits(priv, YT921X_STPn(st->msti), mask, ctrl);
1461 	mutex_unlock(&priv->reg_lock);
1462 
1463 	return res;
1464 }
1465 
1466 static int
1467 yt921x_dsa_vlan_msti_set(struct dsa_switch *ds, struct dsa_bridge bridge,
1468 			 const struct switchdev_vlan_msti *msti)
1469 {
1470 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1471 	u32 masks[2];
1472 	u32 ctrls[2];
1473 	int res;
1474 
1475 	if (!msti->vid)
1476 		return -EINVAL;
1477 	if (!msti->msti || msti->msti >= YT921X_MSTI_NUM)
1478 		return -EINVAL;
1479 
1480 	masks[0] = 0;
1481 	ctrls[0] = 0;
1482 	masks[1] = YT921X_VLAN_CTRLb_STP_ID_M;
1483 	ctrls[1] = YT921X_VLAN_CTRLb_STP_ID(msti->msti);
1484 
1485 	mutex_lock(&priv->reg_lock);
1486 	res = yt921x_reg64_update_bits(priv, YT921X_VLANn_CTRL(msti->vid),
1487 				       masks, ctrls);
1488 	mutex_unlock(&priv->reg_lock);
1489 
1490 	return res;
1491 }
1492 
1493 static void
1494 yt921x_dsa_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1495 {
1496 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1497 	struct device *dev = yt921x_priv_to_device(priv);
1498 	struct dsa_port *dp = dsa_to_port(ds, port);
1499 	bool learning;
1500 	u32 mask;
1501 	u32 ctrl;
1502 	int res;
1503 
1504 	mask = YT921X_STP_PORTn_M(port);
1505 	learning = false;
1506 	switch (state) {
1507 	case BR_STATE_DISABLED:
1508 		ctrl = YT921X_STP_PORTn_DISABLED(port);
1509 		break;
1510 	case BR_STATE_LISTENING:
1511 		ctrl = YT921X_STP_PORTn_LEARNING(port);
1512 		break;
1513 	case BR_STATE_LEARNING:
1514 		ctrl = YT921X_STP_PORTn_LEARNING(port);
1515 		learning = dp->learning;
1516 		break;
1517 	case BR_STATE_FORWARDING:
1518 	default:
1519 		ctrl = YT921X_STP_PORTn_FORWARD(port);
1520 		learning = dp->learning;
1521 		break;
1522 	case BR_STATE_BLOCKING:
1523 		ctrl = YT921X_STP_PORTn_BLOCKING(port);
1524 		break;
1525 	}
1526 
1527 	mutex_lock(&priv->reg_lock);
1528 	do {
1529 		res = yt921x_reg_update_bits(priv, YT921X_STPn(0), mask, ctrl);
1530 		if (res)
1531 			break;
1532 
1533 		mask = YT921X_PORT_LEARN_DIS;
1534 		ctrl = !learning ? YT921X_PORT_LEARN_DIS : 0;
1535 		res = yt921x_reg_update_bits(priv, YT921X_PORTn_LEARN(port),
1536 					     mask, ctrl);
1537 	} while (0);
1538 	mutex_unlock(&priv->reg_lock);
1539 
1540 	if (res)
1541 		dev_err(dev, "Failed to %s port %d: %i\n", "set STP state for",
1542 			port, res);
1543 }
1544 
1545 static int __maybe_unused
1546 yt921x_dsa_port_get_default_prio(struct dsa_switch *ds, int port)
1547 {
1548 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1549 	u32 val;
1550 	int res;
1551 
1552 	mutex_lock(&priv->reg_lock);
1553 	res = yt921x_reg_read(priv, YT921X_PORTn_QOS(port), &val);
1554 	mutex_unlock(&priv->reg_lock);
1555 
1556 	if (res)
1557 		return res;
1558 
1559 	return FIELD_GET(YT921X_PORT_QOS_PRIO_M, val);
1560 }
1561 
1562 static int __maybe_unused
1563 yt921x_dsa_port_set_default_prio(struct dsa_switch *ds, int port, u8 prio)
1564 {
1565 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1566 	u32 mask;
1567 	u32 ctrl;
1568 	int res;
1569 
1570 	if (prio >= YT921X_PRIO_NUM)
1571 		return -EINVAL;
1572 
1573 	mutex_lock(&priv->reg_lock);
1574 	mask = YT921X_PORT_QOS_PRIO_M | YT921X_PORT_QOS_PRIO_EN;
1575 	ctrl = YT921X_PORT_QOS_PRIO(prio) | YT921X_PORT_QOS_PRIO_EN;
1576 	res = yt921x_reg_update_bits(priv, YT921X_PORTn_QOS(port), mask, ctrl);
1577 	mutex_unlock(&priv->reg_lock);
1578 
1579 	return res;
1580 }
1581 
1582 static int __maybe_unused appprios_cmp(const void *a, const void *b)
1583 {
1584 	return ((const u8 *)b)[1] - ((const u8 *)a)[1];
1585 }
1586 
1587 static int __maybe_unused
1588 yt921x_dsa_port_get_apptrust(struct dsa_switch *ds, int port, u8 *sel,
1589 			     int *nselp)
1590 {
1591 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1592 	u8 appprios[2][2] = {};
1593 	int nsel;
1594 	u32 val;
1595 	int res;
1596 
1597 	mutex_lock(&priv->reg_lock);
1598 	res = yt921x_reg_read(priv, YT921X_PORTn_PRIO_ORD(port), &val);
1599 	mutex_unlock(&priv->reg_lock);
1600 
1601 	if (res)
1602 		return res;
1603 
1604 	appprios[0][0] = IEEE_8021QAZ_APP_SEL_DSCP;
1605 	appprios[0][1] = (val >> (3 * YT921X_APP_SEL_DSCP)) & 7;
1606 	appprios[1][0] = DCB_APP_SEL_PCP;
1607 	appprios[1][1] = (val >> (3 * YT921X_APP_SEL_CVLAN_PCP)) & 7;
1608 	sort(appprios, ARRAY_SIZE(appprios), sizeof(appprios[0]), appprios_cmp,
1609 	     NULL);
1610 
1611 	nsel = 0;
1612 	for (int i = 0; i < ARRAY_SIZE(appprios) && appprios[i][1]; i++) {
1613 		sel[nsel] = appprios[i][0];
1614 		nsel++;
1615 	}
1616 	*nselp = nsel;
1617 
1618 	return 0;
1619 }
1620 
1621 static int __maybe_unused
1622 yt921x_dsa_port_set_apptrust(struct dsa_switch *ds, int port, const u8 *sel,
1623 			     int nsel)
1624 {
1625 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1626 	struct device *dev = yt921x_priv_to_device(priv);
1627 	u32 ctrl;
1628 	int res;
1629 
1630 	if (nsel > YT921X_APP_SEL_NUM)
1631 		return -EINVAL;
1632 
1633 	ctrl = 0;
1634 	for (int i = 0; i < nsel; i++) {
1635 		switch (sel[i]) {
1636 		case IEEE_8021QAZ_APP_SEL_DSCP:
1637 			ctrl |= YT921X_PORT_PRIO_ORD_APPm(YT921X_APP_SEL_DSCP,
1638 							  7 - i);
1639 			break;
1640 		case DCB_APP_SEL_PCP:
1641 			ctrl |= YT921X_PORT_PRIO_ORD_APPm(YT921X_APP_SEL_CVLAN_PCP,
1642 							  7 - i);
1643 			ctrl |= YT921X_PORT_PRIO_ORD_APPm(YT921X_APP_SEL_SVLAN_PCP,
1644 							  7 - i);
1645 			break;
1646 		default:
1647 			dev_err(dev,
1648 				"Invalid apptrust selector (at %d-th). Supported: dscp, pcp\n",
1649 				i + 1);
1650 			return -EOPNOTSUPP;
1651 		}
1652 	}
1653 
1654 	mutex_lock(&priv->reg_lock);
1655 	res = yt921x_reg_write(priv, YT921X_PORTn_PRIO_ORD(port), ctrl);
1656 	mutex_unlock(&priv->reg_lock);
1657 
1658 	return res;
1659 }
1660 
1661 static int yt921x_port_down(struct yt921x_priv *priv, int port)
1662 {
1663 	const struct yt921x_info *info = priv->info;
1664 	u32 mask;
1665 	int res;
1666 
1667 	mask = YT921X_PORT_CTRL_LINK_AN | YT921X_PORT_RX_MAC_EN |
1668 	       YT921X_PORT_TX_MAC_EN;
1669 	res = yt921x_reg_clear_bits(priv, YT921X_PORTn_CTRL(port), mask);
1670 	if (res)
1671 		return res;
1672 
1673 	if (BIT(port) & info->serdes_mask) {
1674 		mask = YT921X_SERDES_LINK;
1675 		res = yt921x_reg_clear_bits(priv, YT921X_SERDESn(port), mask);
1676 		if (res)
1677 			return res;
1678 	}
1679 	if (BIT(port) & info->xmii_mask) {
1680 		mask = YT921X_XMII_LINK;
1681 		res = yt921x_reg_clear_bits(priv, YT921X_XMIIn(port), mask);
1682 		if (res)
1683 			return res;
1684 	}
1685 
1686 	return 0;
1687 }
1688 
1689 static int
1690 yt921x_port_up(struct yt921x_priv *priv, int port, unsigned int mode,
1691 	       phy_interface_t interface, int speed, int duplex,
1692 	       bool tx_pause, bool rx_pause)
1693 {
1694 	const struct yt921x_info *info = priv->info;
1695 	struct yt921x_port *pp = &priv->ports[port];
1696 	int ps = ethtool_speed_to_yt921x(speed);
1697 	u32 mask;
1698 	u32 ctrl;
1699 	int res;
1700 
1701 	if (ps == YT921X_SPEED_INVALID)
1702 		return -EINVAL;
1703 
1704 	mask = YT921X_PORT_SPEED_M | YT921X_PORT_TX_MAC_EN |
1705 	       YT921X_PORT_RX_MAC_EN | YT921X_PORT_TX_PAUSE |
1706 	       YT921X_PORT_RX_PAUSE | YT921X_PORT_DUPLEX_FULL |
1707 	       YT921X_PORT_CTRL_LINK_AN;
1708 	ctrl = YT921X_PORT_SPEED(ps);
1709 	if (duplex == DUPLEX_FULL)
1710 		ctrl |= YT921X_PORT_DUPLEX_FULL;
1711 	if (tx_pause)
1712 		ctrl |= YT921X_PORT_TX_PAUSE;
1713 	if (rx_pause)
1714 		ctrl |= YT921X_PORT_RX_PAUSE;
1715 	ctrl |= YT921X_PORT_RX_MAC_EN | YT921X_PORT_TX_MAC_EN;
1716 	if (pp->serdes && pp->inband)
1717 		ctrl |= YT921X_PORT_CTRL_LINK_AN;
1718 	res = yt921x_reg_update_bits(priv, YT921X_PORTn_CTRL(port), mask, ctrl);
1719 	if (res)
1720 		return res;
1721 
1722 	if (!(BIT(port) & (info->serdes_mask | info->xmii_mask)))
1723 		return 0;
1724 
1725 	if (pp->serdes) {
1726 		mask = YT921X_SERDES_SPEED_M;
1727 		ctrl = YT921X_SERDES_SPEED(ps);
1728 		mask |= YT921X_SERDES_DUPLEX_FULL;
1729 		if (duplex == DUPLEX_FULL)
1730 			ctrl |= YT921X_SERDES_DUPLEX_FULL;
1731 		mask |= YT921X_SERDES_TX_PAUSE;
1732 		if (tx_pause)
1733 			ctrl |= YT921X_SERDES_TX_PAUSE;
1734 		mask |= YT921X_SERDES_RX_PAUSE;
1735 		if (rx_pause)
1736 			ctrl |= YT921X_SERDES_RX_PAUSE;
1737 		mask |= YT921X_SERDES_LINK;
1738 		if (!pp->inband)
1739 			ctrl |= YT921X_SERDES_LINK;
1740 		res = yt921x_reg_update_bits(priv, YT921X_SERDESn(port),
1741 					     mask, ctrl);
1742 		if (res)
1743 			return res;
1744 	} else {
1745 		mask = YT921X_XMII_LINK;
1746 		res = yt921x_reg_set_bits(priv, YT921X_XMIIn(port), mask);
1747 		if (res)
1748 			return res;
1749 
1750 		ctrl = YT921X_MDIO_POLLING_SPEED(ps);
1751 		if (duplex == DUPLEX_FULL)
1752 			ctrl |= YT921X_MDIO_POLLING_DUPLEX_FULL;
1753 		ctrl |= YT921X_MDIO_POLLING_LINK;
1754 		res = yt921x_reg_write(priv, YT921X_MDIO_POLLINGn(port), ctrl);
1755 		if (res)
1756 			return res;
1757 	}
1758 
1759 	return 0;
1760 }
1761 
1762 static int
1763 yt921x_port_config(struct yt921x_priv *priv, int port, unsigned int mode,
1764 		   phy_interface_t interface)
1765 {
1766 	struct device *dev = yt921x_priv_to_device(priv);
1767 	const struct yt921x_info *info = priv->info;
1768 	struct yt921x_port *pp = &priv->ports[port];
1769 	u32 mask;
1770 	int res;
1771 
1772 	if (BIT(port) & info->internal_mask) {
1773 		if (interface != PHY_INTERFACE_MODE_INTERNAL)
1774 			goto err;
1775 		return 0;
1776 	}
1777 
1778 	switch (interface) {
1779 	/* SERDES */
1780 	case PHY_INTERFACE_MODE_SGMII:
1781 	case PHY_INTERFACE_MODE_100BASEX:
1782 	case PHY_INTERFACE_MODE_1000BASEX:
1783 	case PHY_INTERFACE_MODE_2500BASEX:
1784 		if (!(BIT(port) & info->serdes_mask))
1785 			goto err;
1786 
1787 		mask = YT921X_SERDES_CTRL_PORTn(port);
1788 		res = yt921x_reg_set_bits(priv, YT921X_SERDES_CTRL, mask);
1789 		if (res)
1790 			return res;
1791 
1792 		mask = YT921X_XMII_CTRL_PORTn(port);
1793 		res = yt921x_reg_clear_bits(priv, YT921X_XMII_CTRL, mask);
1794 		if (res)
1795 			return res;
1796 
1797 		/* The order is quite arbitrary - we can't return to a safe
1798 		 * state on IO errors.
1799 		 */
1800 		pp->serdes = true;
1801 
1802 		break;
1803 	/* add XMII support here */
1804 	default:
1805 		goto err;
1806 	}
1807 
1808 	return 0;
1809 
1810 err:
1811 	dev_err(dev, "Wrong mode %d on port %d\n", interface, port);
1812 	return -EINVAL;
1813 }
1814 
1815 static struct phylink_pcs *
1816 yt921x_phylink_mac_select_pcs(struct phylink_config *config,
1817 			      phy_interface_t interface)
1818 {
1819 	struct dsa_port *dp = dsa_phylink_to_port(config);
1820 	struct yt921x_priv *priv = dsa_to_yt921x_priv(dp->ds);
1821 	const struct yt921x_info *info = priv->info;
1822 	int port = dp->index;
1823 
1824 	if (!(BIT(port) & info->serdes_mask))
1825 		return NULL;
1826 
1827 	switch (interface) {
1828 	case PHY_INTERFACE_MODE_SGMII:
1829 	case PHY_INTERFACE_MODE_100BASEX:
1830 	case PHY_INTERFACE_MODE_1000BASEX:
1831 	case PHY_INTERFACE_MODE_2500BASEX:
1832 		return &priv->ports[port].pcs;
1833 	default:
1834 		return NULL;
1835 	}
1836 }
1837 
1838 static void
1839 yt921x_phylink_mac_link_down(struct phylink_config *config, unsigned int mode,
1840 			     phy_interface_t interface)
1841 {
1842 	struct dsa_port *dp = dsa_phylink_to_port(config);
1843 	struct yt921x_priv *priv = dsa_to_yt921x_priv(dp->ds);
1844 	struct yt921x_mib *pm;
1845 	int port = dp->index;
1846 	int res;
1847 
1848 	/* No need to sync; port control block is hold until device remove */
1849 	pm = priv->ports[port].mib;
1850 	if (pm)
1851 		cancel_delayed_work(&pm->work);
1852 
1853 	mutex_lock(&priv->reg_lock);
1854 	res = yt921x_port_down(priv, port);
1855 	mutex_unlock(&priv->reg_lock);
1856 
1857 	if (res)
1858 		dev_err(dp->ds->dev, "Failed to %s port %d: %i\n", "bring down",
1859 			port, res);
1860 }
1861 
1862 static void
1863 yt921x_phylink_mac_link_up(struct phylink_config *config,
1864 			   struct phy_device *phydev, unsigned int mode,
1865 			   phy_interface_t interface, int speed, int duplex,
1866 			   bool tx_pause, bool rx_pause)
1867 {
1868 	struct dsa_port *dp = dsa_phylink_to_port(config);
1869 	struct yt921x_priv *priv = dsa_to_yt921x_priv(dp->ds);
1870 	struct yt921x_mib *pm;
1871 	int port = dp->index;
1872 	int res;
1873 
1874 	mutex_lock(&priv->reg_lock);
1875 	res = yt921x_port_up(priv, port, mode, interface, speed, duplex,
1876 			     tx_pause, rx_pause);
1877 	mutex_unlock(&priv->reg_lock);
1878 
1879 	if (res)
1880 		dev_err(dp->ds->dev, "Failed to %s port %d: %i\n", "bring up",
1881 			port, res);
1882 
1883 	pm = priv->ports[port].mib;
1884 	if (pm)
1885 		schedule_delayed_work(&pm->work, 0);
1886 }
1887 
1888 static void
1889 yt921x_phylink_mac_config(struct phylink_config *config, unsigned int mode,
1890 			  const struct phylink_link_state *state)
1891 {
1892 	struct dsa_port *dp = dsa_phylink_to_port(config);
1893 	struct yt921x_priv *priv = dsa_to_yt921x_priv(dp->ds);
1894 	int port = dp->index;
1895 	int res;
1896 
1897 	mutex_lock(&priv->reg_lock);
1898 	res = yt921x_port_config(priv, port, mode, state->interface);
1899 	mutex_unlock(&priv->reg_lock);
1900 
1901 	if (res)
1902 		dev_err(dp->ds->dev, "Failed to %s port %d: %i\n", "config",
1903 			port, res);
1904 }
1905 
1906 static void
1907 yt921x_dsa_phylink_get_caps(struct dsa_switch *ds, int port,
1908 			    struct phylink_config *config)
1909 {
1910 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
1911 	const struct yt921x_info *info = priv->info;
1912 
1913 	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1914 				   MAC_10 | MAC_100 | MAC_1000;
1915 
1916 	if (info->internal_mask & BIT(port)) {
1917 		/* Port 10 for MCU should probably go here too. But since that
1918 		 * is untested yet, turn it down for the moment by letting it
1919 		 * fall to the default branch.
1920 		 */
1921 		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
1922 			  config->supported_interfaces);
1923 		return;
1924 	}
1925 
1926 	if (BIT(port) & info->serdes_mask) {
1927 		__set_bit(PHY_INTERFACE_MODE_SGMII,
1928 			  config->supported_interfaces);
1929 		/* REVSGMII (SGMII in PHY role) should go here, once
1930 		 * PHY_INTERFACE_MODE_REVSGMII is introduced.
1931 		 */
1932 		__set_bit(PHY_INTERFACE_MODE_100BASEX,
1933 			  config->supported_interfaces);
1934 		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
1935 			  config->supported_interfaces);
1936 		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
1937 			  config->supported_interfaces);
1938 		config->mac_capabilities |= MAC_2500FD;
1939 	}
1940 	if (BIT(port) & info->xmii_mask) {
1941 		/* Not tested. To add support for XMII:
1942 		 *   - Add proper interface modes below
1943 		 *   - Handle them in yt921x_port_config()
1944 		 */
1945 	}
1946 	/* no such port: empty supported_interfaces causes phylink to turn it
1947 	 * down
1948 	 */
1949 }
1950 
1951 static int yt921x_port_setup(struct yt921x_priv *priv, int port)
1952 {
1953 	struct dsa_switch *ds = &priv->ds;
1954 	u32 ctrl;
1955 	int res;
1956 
1957 	res = yt921x_userport_standalone(priv, port);
1958 	if (res)
1959 		return res;
1960 
1961 	/* Clear prio order (even if DCB is not enabled) to avoid unsolicited
1962 	 * priorities
1963 	 */
1964 	res = yt921x_reg_write(priv, YT921X_PORTn_PRIO_ORD(port), 0);
1965 	if (res)
1966 		return res;
1967 
1968 	if (dsa_is_cpu_port(ds, port)) {
1969 		/* Egress of CPU port is supposed to be completely controlled
1970 		 * via tagging, so set to oneway isolated (drop all packets
1971 		 * without tag).
1972 		 */
1973 		ctrl = ~(u32)0;
1974 		res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port),
1975 				       ctrl);
1976 		if (res)
1977 			return res;
1978 
1979 		/* To simplify FDB "isolation" simulation, we also disable
1980 		 * learning on the CPU port, and let software identify packets
1981 		 * towarding CPU (either trapped or a static FDB entry is
1982 		 * matched, no matter which bridge that entry is for), which is
1983 		 * already done by yt921x_userport_standalone(). As a result,
1984 		 * VLAN-awareness becomes unrelated on the CPU port (set to
1985 		 * VLAN-unaware by the way).
1986 		 */
1987 	}
1988 
1989 	return 0;
1990 }
1991 
1992 static enum dsa_tag_protocol
1993 yt921x_dsa_get_tag_protocol(struct dsa_switch *ds, int port,
1994 			    enum dsa_tag_protocol m)
1995 {
1996 	return DSA_TAG_PROTO_YT921X;
1997 }
1998 
1999 static void yt921x_dsa_port_teardown(struct dsa_switch *ds, int port)
2000 {
2001 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
2002 	struct yt921x_port *pp = &priv->ports[port];
2003 	struct yt921x_mib *pm = pp->mib;
2004 
2005 	/* get_stats64() might not be synchronized with port_teardown(), hold
2006 	 * the MIB block till the device teardown
2007 	 */
2008 	if (pm)
2009 		disable_delayed_work_sync(&pm->work);
2010 }
2011 
2012 static int yt921x_dsa_port_setup(struct dsa_switch *ds, int port)
2013 {
2014 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
2015 	struct yt921x_port *pp = &priv->ports[port];
2016 	const struct yt921x_info *info = priv->info;
2017 	struct device *dev = yt921x_priv_to_device(priv);
2018 	struct yt921x_mib *pm = pp->mib;
2019 	int res;
2020 
2021 	if (!pm && (BIT(port) & yt921x_info_ports_mask(info))) {
2022 		pm = devm_kzalloc_obj(dev, *pm);
2023 		if (!pm)
2024 			return -ENOMEM;
2025 		pp->mib = pm;
2026 
2027 		pm->port = pp;
2028 		u64_stats_init(&pm->syncp);
2029 		INIT_DELAYED_WORK(&pm->work, yt921x_mib_poll);
2030 		/* Start disabled, so that the enable below always pairs with
2031 		 * either this disable or the one from a previous teardown
2032 		 */
2033 		disable_delayed_work_sync(&pm->work);
2034 	}
2035 
2036 	mutex_lock(&priv->reg_lock);
2037 	res = yt921x_port_setup(priv, port);
2038 	mutex_unlock(&priv->reg_lock);
2039 	if (res)
2040 		return res;
2041 
2042 	if (pm)
2043 		enable_delayed_work(&pm->work);
2044 
2045 	return 0;
2046 }
2047 
2048 /* Not "port" - DSCP mapping is global */
2049 static int __maybe_unused
2050 yt921x_dsa_port_get_dscp_prio(struct dsa_switch *ds, int port, u8 dscp)
2051 {
2052 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
2053 	u32 val;
2054 	int res;
2055 
2056 	mutex_lock(&priv->reg_lock);
2057 	res = yt921x_reg_read(priv, YT921X_IPM_DSCPn(dscp), &val);
2058 	mutex_unlock(&priv->reg_lock);
2059 
2060 	if (res)
2061 		return res;
2062 
2063 	return FIELD_GET(YT921X_IPM_PRIO_M, val);
2064 }
2065 
2066 static int __maybe_unused
2067 yt921x_dsa_port_del_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, u8 prio)
2068 {
2069 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
2070 	u32 val;
2071 	int res;
2072 
2073 	mutex_lock(&priv->reg_lock);
2074 	/* During a "dcb app replace" command, the new app table entry will be
2075 	 * added first, then the old one will be deleted. But the hardware only
2076 	 * supports one QoS class per DSCP value (duh), so if we blindly delete
2077 	 * the app table entry for this DSCP value, we end up deleting the
2078 	 * entry with the new priority. Avoid that by checking whether user
2079 	 * space wants to delete the priority which is currently configured, or
2080 	 * something else which is no longer current.
2081 	 */
2082 	res = yt921x_reg_read(priv, YT921X_IPM_DSCPn(dscp), &val);
2083 	if (!res && FIELD_GET(YT921X_IPM_PRIO_M, val) == prio)
2084 		res = yt921x_reg_write(priv, YT921X_IPM_DSCPn(dscp),
2085 				       YT921X_IPM_PRIO(IEEE8021Q_TT_BK));
2086 	mutex_unlock(&priv->reg_lock);
2087 
2088 	return res;
2089 }
2090 
2091 static int __maybe_unused
2092 yt921x_dsa_port_add_dscp_prio(struct dsa_switch *ds, int port, u8 dscp, u8 prio)
2093 {
2094 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
2095 	int res;
2096 
2097 	if (prio >= YT921X_PRIO_NUM)
2098 		return -EINVAL;
2099 
2100 	mutex_lock(&priv->reg_lock);
2101 	res = yt921x_reg_write(priv, YT921X_IPM_DSCPn(dscp),
2102 			       YT921X_IPM_PRIO(prio));
2103 	mutex_unlock(&priv->reg_lock);
2104 
2105 	return res;
2106 }
2107 
2108 static int yt921x_edata_wait(struct yt921x_priv *priv, u32 *valp)
2109 {
2110 	u32 val = YT921X_EDATA_DATA_IDLE;
2111 	int res;
2112 
2113 	res = yt921x_reg_wait(priv, YT921X_EDATA_DATA,
2114 			      YT921X_EDATA_DATA_STATUS_M, &val);
2115 	if (res)
2116 		return res;
2117 
2118 	*valp = val;
2119 	return 0;
2120 }
2121 
2122 static int
2123 yt921x_edata_read_cont(struct yt921x_priv *priv, u8 addr, u8 *valp)
2124 {
2125 	u32 ctrl;
2126 	u32 val;
2127 	int res;
2128 
2129 	ctrl = YT921X_EDATA_CTRL_ADDR(addr) | YT921X_EDATA_CTRL_READ;
2130 	res = yt921x_reg_write(priv, YT921X_EDATA_CTRL, ctrl);
2131 	if (res)
2132 		return res;
2133 	res = yt921x_edata_wait(priv, &val);
2134 	if (res)
2135 		return res;
2136 
2137 	*valp = FIELD_GET(YT921X_EDATA_DATA_DATA_M, val);
2138 	return 0;
2139 }
2140 
2141 static int yt921x_edata_read(struct yt921x_priv *priv, u8 addr, u8 *valp)
2142 {
2143 	u32 val;
2144 	int res;
2145 
2146 	res = yt921x_edata_wait(priv, &val);
2147 	if (res)
2148 		return res;
2149 	return yt921x_edata_read_cont(priv, addr, valp);
2150 }
2151 
2152 static void yt921x_dsa_teardown(struct dsa_switch *ds)
2153 {
2154 #if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS)
2155 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
2156 
2157 	yt921x_leds_remove(priv);
2158 #endif
2159 }
2160 
2161 static int yt921x_chip_detect(struct yt921x_priv *priv)
2162 {
2163 	struct device *dev = yt921x_priv_to_device(priv);
2164 	const struct yt921x_info *info;
2165 	u8 extmode;
2166 	u32 chipid;
2167 	u32 major;
2168 	u32 mode;
2169 	u32 val;
2170 	int res;
2171 
2172 	res = yt921x_reg_read(priv, YT921X_CHIP_ID, &chipid);
2173 	if (res)
2174 		return res;
2175 
2176 	major = FIELD_GET(YT921X_CHIP_ID_MAJOR, chipid);
2177 
2178 	for (info = yt921x_infos; info->name; info++)
2179 		if (info->major == major)
2180 			break;
2181 	if (!info->name) {
2182 		dev_err(dev, "Unexpected chipid 0x%x\n", chipid);
2183 		return -ENODEV;
2184 	}
2185 
2186 	res = yt921x_reg_read(priv, YT921X_CHIP_MODE, &mode);
2187 	if (res)
2188 		return res;
2189 	res = yt921x_edata_read(priv, YT921X_EDATA_EXTMODE, &extmode);
2190 	if (res)
2191 		return res;
2192 
2193 	for (; info->name; info++)
2194 		if (info->major == major && info->mode == mode &&
2195 		    info->extmode == extmode)
2196 			break;
2197 	if (!info->name) {
2198 		dev_err(dev,
2199 			"Unsupported chipid 0x%x with chipmode 0x%x 0x%x\n",
2200 			chipid, mode, extmode);
2201 		return -ENODEV;
2202 	}
2203 
2204 	res = yt921x_reg_read(priv, YT921X_SYS_CLK, &val);
2205 	if (res)
2206 		return res;
2207 	switch (FIELD_GET(YT921X_SYS_CLK_SEL_M, val)) {
2208 	case 0:
2209 		priv->cycle_ns = info->major == YT9215_MAJOR ? 8 : 6;
2210 		break;
2211 	case YT921X_SYS_CLK_143M:
2212 		priv->cycle_ns = 7;
2213 		break;
2214 	default:
2215 		priv->cycle_ns = 8;
2216 	}
2217 
2218 	/* Print chipid here since we are interested in lower 16 bits */
2219 	dev_info(dev,
2220 		 "Motorcomm %s ethernet switch, chipid: 0x%x, chipmode: 0x%x 0x%x\n",
2221 		 info->name, chipid, mode, extmode);
2222 
2223 	priv->info = info;
2224 
2225 	return 0;
2226 }
2227 
2228 static int yt921x_chip_reset(struct yt921x_priv *priv)
2229 {
2230 	struct device *dev = yt921x_priv_to_device(priv);
2231 	u16 eth_p_tag;
2232 	u32 val;
2233 	int res;
2234 
2235 	res = yt921x_chip_detect(priv);
2236 	if (res)
2237 		return res;
2238 
2239 	/* Reset */
2240 	res = yt921x_reg_write(priv, YT921X_RST, YT921X_RST_HW);
2241 	if (res)
2242 		return res;
2243 
2244 	/* RST_HW is almost same as GPIO hard reset, so we need this delay. */
2245 	fsleep(YT921X_RST_DELAY_US);
2246 
2247 	val = 0;
2248 	res = yt921x_reg_wait(priv, YT921X_RST, ~0, &val);
2249 	if (res)
2250 		return res;
2251 
2252 	/* Check for tag EtherType; do it after reset in case you messed it up
2253 	 * before.
2254 	 */
2255 	res = yt921x_reg_read(priv, YT921X_CPU_TAG_TPID, &val);
2256 	if (res)
2257 		return res;
2258 	eth_p_tag = FIELD_GET(YT921X_CPU_TAG_TPID_TPID_M, val);
2259 	if (eth_p_tag != ETH_P_YT921X) {
2260 		dev_err(dev, "Tag type 0x%x != 0x%x\n", eth_p_tag,
2261 			ETH_P_YT921X);
2262 		/* Despite being possible, we choose not to set CPU_TAG_TPID,
2263 		 * since there is no way it can be different unless you have the
2264 		 * wrong chip.
2265 		 */
2266 		return -EINVAL;
2267 	}
2268 
2269 	return 0;
2270 }
2271 
2272 static int yt921x_chip_setup_dsa(struct yt921x_priv *priv)
2273 {
2274 	struct dsa_switch *ds = &priv->ds;
2275 	unsigned long cpu_ports_mask;
2276 	u32 ctrls[2];
2277 	u32 ctrl;
2278 	int port;
2279 	int res;
2280 
2281 	/* Enable DSA */
2282 	priv->cpu_ports_mask = dsa_cpu_ports(ds);
2283 
2284 	ctrl = YT921X_EXT_CPU_PORT_TAG_EN | YT921X_EXT_CPU_PORT_PORT_EN |
2285 	       YT921X_EXT_CPU_PORT_PORT(__ffs(priv->cpu_ports_mask));
2286 	res = yt921x_reg_write(priv, YT921X_EXT_CPU_PORT, ctrl);
2287 	if (res)
2288 		return res;
2289 
2290 	/* Setup software switch */
2291 	ctrl = YT921X_CPU_COPY_TO_EXT_CPU;
2292 	res = yt921x_reg_write(priv, YT921X_CPU_COPY, ctrl);
2293 	if (res)
2294 		return res;
2295 
2296 	ctrl = GENMASK(10, 0);
2297 	res = yt921x_reg_write(priv, YT921X_FILTER_UNK_UCAST, ctrl);
2298 	if (res)
2299 		return res;
2300 	res = yt921x_reg_write(priv, YT921X_FILTER_UNK_MCAST, ctrl);
2301 	if (res)
2302 		return res;
2303 
2304 	/* YT921x does not support native DSA port bridging, so we use port
2305 	 * isolation to emulate it. However, be especially careful that port
2306 	 * isolation takes _after_ FDB lookups, i.e. if an FDB entry (from
2307 	 * another bridge) is matched and the destination port (in another
2308 	 * bridge) is blocked, the packet will be dropped instead of flooding to
2309 	 * the "bridged" ports, thus we need to trap and handle those packets by
2310 	 * software.
2311 	 *
2312 	 * If there is no more than one bridge, we might be able to drop them
2313 	 * directly given some conditions are met, but we trap them in all cases
2314 	 * for now.
2315 	 */
2316 	ctrl = 0;
2317 	for (int i = 0; i < YT921X_PORT_NUM; i++)
2318 		ctrl |= YT921X_ACT_UNK_ACTn_TRAP(i);
2319 	/* Except for CPU ports, if any packets are sent via CPU ports without
2320 	 * tag, they should be dropped.
2321 	 */
2322 	cpu_ports_mask = priv->cpu_ports_mask;
2323 	for_each_set_bit(port, &cpu_ports_mask, YT921X_PORT_NUM) {
2324 		ctrl &= ~YT921X_ACT_UNK_ACTn_M(port);
2325 		ctrl |= YT921X_ACT_UNK_ACTn_DROP(port);
2326 	}
2327 	res = yt921x_reg_write(priv, YT921X_ACT_UNK_UCAST, ctrl);
2328 	if (res)
2329 		return res;
2330 	res = yt921x_reg_write(priv, YT921X_ACT_UNK_MCAST, ctrl);
2331 	if (res)
2332 		return res;
2333 
2334 	/* Tagged VID 0 should be treated as untagged, which confuses the
2335 	 * hardware a lot
2336 	 */
2337 	ctrls[0] = YT921X_VLAN_CTRLa_LEARN_DIS | YT921X_VLAN_CTRLa_PORTS_M;
2338 	ctrls[1] = 0;
2339 	res = yt921x_reg64_write(priv, YT921X_VLANn_CTRL(0), ctrls);
2340 	if (res)
2341 		return res;
2342 
2343 	return 0;
2344 }
2345 
2346 static int __maybe_unused yt921x_chip_setup_qos(struct yt921x_priv *priv)
2347 {
2348 	u32 ctrl;
2349 	int res;
2350 
2351 	/* DSCP to internal priorities */
2352 	for (u8 dscp = 0; dscp < DSCP_MAX; dscp++) {
2353 		int prio = ietf_dscp_to_ieee8021q_tt(dscp);
2354 
2355 		if (prio < 0)
2356 			return prio;
2357 
2358 		res = yt921x_reg_write(priv, YT921X_IPM_DSCPn(dscp),
2359 				       YT921X_IPM_PRIO(prio));
2360 		if (res)
2361 			return res;
2362 	}
2363 
2364 	/* 802.1Q QoS to internal priorities */
2365 	for (u8 pcp = 0; pcp < 8; pcp++)
2366 		for (u8 dei = 0; dei < 2; dei++) {
2367 			ctrl = YT921X_IPM_PRIO(pcp);
2368 			if (dei)
2369 				/* "Red" almost means drop, so it's not that
2370 				 * useful. Note that tc police does not support
2371 				 * Three-Color very well
2372 				 */
2373 				ctrl |= YT921X_IPM_COLOR_YELLOW;
2374 
2375 			for (u8 svlan = 0; svlan < 2; svlan++) {
2376 				u32 reg = YT921X_IPM_PCPn(svlan, dei, pcp);
2377 
2378 				res = yt921x_reg_write(priv, reg, ctrl);
2379 				if (res)
2380 					return res;
2381 			}
2382 		}
2383 
2384 	return 0;
2385 }
2386 
2387 static int yt921x_chip_setup(struct yt921x_priv *priv)
2388 {
2389 	u32 ctrl;
2390 	int res;
2391 
2392 	ctrl = YT921X_FUNC_MIB | YT921X_FUNC_ACL | YT921X_FUNC_METER;
2393 	res = yt921x_reg_set_bits(priv, YT921X_FUNC, ctrl);
2394 	if (res)
2395 		return res;
2396 
2397 	res = yt921x_chip_setup_dsa(priv);
2398 	if (res)
2399 		return res;
2400 
2401 	res = yt921x_chip_setup_tc(priv);
2402 	if (res)
2403 		return res;
2404 
2405 	res = yt921x_chip_setup_acl(priv);
2406 	if (res)
2407 		return res;
2408 
2409 #if IS_ENABLED(CONFIG_DCB)
2410 	res = yt921x_chip_setup_qos(priv);
2411 	if (res)
2412 		return res;
2413 #endif
2414 
2415 	/* Miscellaneous */
2416 	res = yt921x_reg_set_bits(priv, YT921X_SENSOR, YT921X_SENSOR_TEMP);
2417 	if (res)
2418 		return res;
2419 
2420 	res = yt921x_reg_clear_bits(priv, YT921X_SERDES_CTRL,
2421 				    YT921X_SERDES_CTRL_TEST);
2422 	if (res)
2423 		return res;
2424 
2425 	return 0;
2426 }
2427 
2428 static int yt921x_dsa_setup(struct dsa_switch *ds)
2429 {
2430 	struct yt921x_priv *priv = dsa_to_yt921x_priv(ds);
2431 	struct device *dev = yt921x_priv_to_device(priv);
2432 	struct device_node *np = dev->of_node;
2433 	struct device_node *child;
2434 	unsigned long mask;
2435 	int port;
2436 	int res;
2437 
2438 	mutex_lock(&priv->reg_lock);
2439 	res = yt921x_chip_reset(priv);
2440 	mutex_unlock(&priv->reg_lock);
2441 
2442 	if (res)
2443 		return res;
2444 
2445 	/* Register the internal mdio bus. Nodes for internal ports should have
2446 	 * proper phy-handle pointing to their PHYs. Not enabling the internal
2447 	 * bus is possible, though pretty wired, if internal ports are not used.
2448 	 */
2449 	child = of_get_child_by_name(np, "mdio");
2450 	if (child) {
2451 		res = yt921x_mbus_int_init(priv, child);
2452 		of_node_put(child);
2453 		if (res)
2454 			return res;
2455 	}
2456 
2457 	/* External mdio bus is optional */
2458 	child = of_get_child_by_name(np, "mdio-external");
2459 	if (child) {
2460 		res = yt921x_mbus_ext_init(priv, child);
2461 		of_node_put(child);
2462 		if (res)
2463 			return res;
2464 
2465 		dev_err(dev, "Untested external mdio bus\n");
2466 		return -ENODEV;
2467 	}
2468 
2469 	mask = priv->info->serdes_mask;
2470 	for_each_set_bit(port, &mask, YT921X_PORT_NUM) {
2471 		struct yt921x_port *pp = &priv->ports[port];
2472 
2473 		pp->pcs.ops = &yt921x_phylink_pcs_ops;
2474 		pp->pcs.poll = true;
2475 
2476 		__set_bit(PHY_INTERFACE_MODE_SGMII,
2477 			  pp->pcs.supported_interfaces);
2478 		__set_bit(PHY_INTERFACE_MODE_100BASEX,
2479 			  pp->pcs.supported_interfaces);
2480 		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
2481 			  pp->pcs.supported_interfaces);
2482 		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
2483 			  pp->pcs.supported_interfaces);
2484 	}
2485 
2486 	mutex_lock(&priv->reg_lock);
2487 	res = yt921x_chip_setup(priv);
2488 	mutex_unlock(&priv->reg_lock);
2489 
2490 	if (res)
2491 		return res;
2492 
2493 #if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS)
2494 	res = yt921x_leds_setup(priv);
2495 	if (res)
2496 		dev_warn(dev, "Failed to setup LEDs: %d\n", res);
2497 #endif
2498 
2499 	return 0;
2500 }
2501 
2502 static const struct phylink_mac_ops yt921x_phylink_mac_ops = {
2503 	.mac_select_pcs	= yt921x_phylink_mac_select_pcs,
2504 	.mac_link_down	= yt921x_phylink_mac_link_down,
2505 	.mac_link_up	= yt921x_phylink_mac_link_up,
2506 	.mac_config	= yt921x_phylink_mac_config,
2507 };
2508 
2509 static const struct dsa_switch_ops yt921x_dsa_switch_ops = {
2510 	/* mib */
2511 	.get_strings		= yt921x_dsa_get_strings,
2512 	.get_ethtool_stats	= yt921x_dsa_get_ethtool_stats,
2513 	.get_sset_count		= yt921x_dsa_get_sset_count,
2514 	.get_eth_mac_stats	= yt921x_dsa_get_eth_mac_stats,
2515 	.get_eth_ctrl_stats	= yt921x_dsa_get_eth_ctrl_stats,
2516 	.get_rmon_stats		= yt921x_dsa_get_rmon_stats,
2517 	.get_stats64		= yt921x_dsa_get_stats64,
2518 	.get_pause_stats	= yt921x_dsa_get_pause_stats,
2519 	/* eee */
2520 	.support_eee		= dsa_supports_eee,
2521 	.set_mac_eee		= yt921x_dsa_set_mac_eee,
2522 	/* mtu */
2523 	.port_change_mtu	= yt921x_dsa_port_change_mtu,
2524 	.port_max_mtu		= yt921x_dsa_port_max_mtu,
2525 	/* rate */
2526 	.port_policer_del	= yt921x_dsa_port_policer_del,
2527 	.port_policer_add	= yt921x_dsa_port_policer_add,
2528 	.port_setup_tc		= yt921x_dsa_port_setup_tc,
2529 	/* acl */
2530 	.cls_flower_del		= yt921x_dsa_cls_flower_del,
2531 	.cls_flower_add		= yt921x_dsa_cls_flower_add,
2532 	/* hsr */
2533 	.port_hsr_leave		= dsa_port_simple_hsr_leave,
2534 	.port_hsr_join		= dsa_port_simple_hsr_join,
2535 	/* mirror */
2536 	.port_mirror_del	= yt921x_dsa_port_mirror_del,
2537 	.port_mirror_add	= yt921x_dsa_port_mirror_add,
2538 	/* lag */
2539 	.port_lag_leave		= yt921x_dsa_port_lag_leave,
2540 	.port_lag_join		= yt921x_dsa_port_lag_join,
2541 	/* fdb */
2542 	.port_fdb_dump		= yt921x_dsa_port_fdb_dump,
2543 	.port_fast_age		= yt921x_dsa_port_fast_age,
2544 	.set_ageing_time	= yt921x_dsa_set_ageing_time,
2545 	.port_fdb_del		= yt921x_dsa_port_fdb_del,
2546 	.port_fdb_add		= yt921x_dsa_port_fdb_add,
2547 	.port_mdb_del		= yt921x_dsa_port_mdb_del,
2548 	.port_mdb_add		= yt921x_dsa_port_mdb_add,
2549 	/* vlan */
2550 	.port_vlan_filtering	= yt921x_dsa_port_vlan_filtering,
2551 	.port_vlan_del		= yt921x_dsa_port_vlan_del,
2552 	.port_vlan_add		= yt921x_dsa_port_vlan_add,
2553 	/* bridge */
2554 	.port_pre_bridge_flags	= yt921x_dsa_port_pre_bridge_flags,
2555 	.port_bridge_flags	= yt921x_dsa_port_bridge_flags,
2556 	.port_bridge_leave	= yt921x_dsa_port_bridge_leave,
2557 	.port_bridge_join	= yt921x_dsa_port_bridge_join,
2558 	/* mst */
2559 	.port_mst_state_set	= yt921x_dsa_port_mst_state_set,
2560 	.vlan_msti_set		= yt921x_dsa_vlan_msti_set,
2561 	.port_stp_state_set	= yt921x_dsa_port_stp_state_set,
2562 #if IS_ENABLED(CONFIG_DCB)
2563 	/* dcb */
2564 	.port_get_default_prio	= yt921x_dsa_port_get_default_prio,
2565 	.port_set_default_prio	= yt921x_dsa_port_set_default_prio,
2566 	.port_get_apptrust	= yt921x_dsa_port_get_apptrust,
2567 	.port_set_apptrust	= yt921x_dsa_port_set_apptrust,
2568 #endif
2569 	/* port */
2570 	.get_tag_protocol	= yt921x_dsa_get_tag_protocol,
2571 	.phylink_get_caps	= yt921x_dsa_phylink_get_caps,
2572 	.port_teardown		= yt921x_dsa_port_teardown,
2573 	.port_setup		= yt921x_dsa_port_setup,
2574 #if IS_ENABLED(CONFIG_DCB)
2575 	/* dscp */
2576 	.port_get_dscp_prio	= yt921x_dsa_port_get_dscp_prio,
2577 	.port_del_dscp_prio	= yt921x_dsa_port_del_dscp_prio,
2578 	.port_add_dscp_prio	= yt921x_dsa_port_add_dscp_prio,
2579 #endif
2580 	/* chip */
2581 	.teardown		= yt921x_dsa_teardown,
2582 	.setup			= yt921x_dsa_setup,
2583 };
2584 
2585 static void yt921x_mdio_shutdown(struct mdio_device *mdiodev)
2586 {
2587 	struct yt921x_priv *priv = mdiodev_get_drvdata(mdiodev);
2588 
2589 	if (!priv)
2590 		return;
2591 
2592 	dsa_switch_shutdown(&priv->ds);
2593 }
2594 
2595 static void yt921x_mdio_remove(struct mdio_device *mdiodev)
2596 {
2597 	struct yt921x_priv *priv = mdiodev_get_drvdata(mdiodev);
2598 
2599 	if (!priv)
2600 		return;
2601 
2602 	dsa_unregister_switch(&priv->ds);
2603 
2604 	for (unsigned int i = 0; i < ARRAY_SIZE(priv->acl_blks); i++) {
2605 		struct yt921x_acl_blk *aclblk = priv->acl_blks[i];
2606 
2607 		if (!aclblk)
2608 			continue;
2609 		for (unsigned int j = 0; j < ARRAY_SIZE(aclblk->rules); j++) {
2610 			struct yt921x_acl_rule *aclrule = aclblk->rules[j];
2611 
2612 			if (!aclrule)
2613 				continue;
2614 			kvfree(aclrule);
2615 		}
2616 		kvfree(aclblk);
2617 	}
2618 	mutex_destroy(&priv->reg_lock);
2619 }
2620 
2621 static int yt921x_mdio_probe(struct mdio_device *mdiodev)
2622 {
2623 	struct device *dev = &mdiodev->dev;
2624 	struct yt921x_reg_mdio *mdio;
2625 	struct yt921x_priv *priv;
2626 	struct dsa_switch *ds;
2627 
2628 	priv = devm_kzalloc_obj(dev, *priv);
2629 	if (!priv)
2630 		return -ENOMEM;
2631 
2632 	mdio = devm_kzalloc_obj(dev, *mdio);
2633 	if (!mdio)
2634 		return -ENOMEM;
2635 
2636 	mdio->bus = mdiodev->bus;
2637 	mdio->addr = mdiodev->addr;
2638 	mdio->switchid = 0;
2639 
2640 	mutex_init(&priv->reg_lock);
2641 
2642 	priv->reg_ops = &yt921x_reg_ops_mdio;
2643 	priv->reg_ctx = mdio;
2644 
2645 	for (size_t i = 0; i < ARRAY_SIZE(priv->ports); i++) {
2646 		struct yt921x_port *pp = &priv->ports[i];
2647 
2648 		pp->index = i;
2649 	}
2650 
2651 	ds = &priv->ds;
2652 	ds->dev = dev;
2653 	ds->assisted_learning_on_cpu_port = true;
2654 	ds->dscp_prio_mapping_is_global = true;
2655 	ds->priv = priv;
2656 	ds->ops = &yt921x_dsa_switch_ops;
2657 	ds->ageing_time_min = 1 * 5000;
2658 	ds->ageing_time_max = U16_MAX * 5000;
2659 	ds->phylink_mac_ops = &yt921x_phylink_mac_ops;
2660 	ds->num_lag_ids = YT921X_LAG_NUM;
2661 	ds->num_ports = YT921X_PORT_NUM;
2662 
2663 	mdiodev_set_drvdata(mdiodev, priv);
2664 
2665 	return dsa_register_switch(ds);
2666 }
2667 
2668 static const struct of_device_id yt921x_of_match[] = {
2669 	{ .compatible = "motorcomm,yt9215" },
2670 	{}
2671 };
2672 MODULE_DEVICE_TABLE(of, yt921x_of_match);
2673 
2674 static struct mdio_driver yt921x_mdio_driver = {
2675 	.probe = yt921x_mdio_probe,
2676 	.remove = yt921x_mdio_remove,
2677 	.shutdown = yt921x_mdio_shutdown,
2678 	.mdiodrv.driver = {
2679 		.name = YT921X_NAME,
2680 		.of_match_table = yt921x_of_match,
2681 	},
2682 };
2683 
2684 mdio_module_driver(yt921x_mdio_driver);
2685 
2686 MODULE_AUTHOR("David Yang <mmyangfl@gmail.com>");
2687 MODULE_DESCRIPTION("Driver for Motorcomm YT921x Switch");
2688 MODULE_LICENSE("GPL");
2689