1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot Switch driver
3  *
4  * This contains glue logic between the switchdev driver operations and the
5  * mscc_ocelot_switch_lib.
6  *
7  * Copyright (c) 2017, 2019 Microsemi Corporation
8  * Copyright 2020-2021 NXP
9  */
10 
11 #include <linux/dsa/ocelot.h>
12 #include <linux/if_bridge.h>
13 #include <linux/of_net.h>
14 #include <linux/phy/phy.h>
15 #include <net/pkt_cls.h>
16 #include "ocelot.h"
17 #include "ocelot_vcap.h"
18 #include "ocelot_fdma.h"
19 
20 #define OCELOT_MAC_QUIRKS	OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP
21 
22 static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
23 {
24 	return devlink_priv(dlp->devlink);
25 }
26 
27 static int devlink_port_to_port(struct devlink_port *dlp)
28 {
29 	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
30 
31 	return dlp - ocelot->devlink_ports;
32 }
33 
34 static int ocelot_devlink_sb_pool_get(struct devlink *dl,
35 				      unsigned int sb_index, u16 pool_index,
36 				      struct devlink_sb_pool_info *pool_info)
37 {
38 	struct ocelot *ocelot = devlink_priv(dl);
39 
40 	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
41 }
42 
43 static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
44 				      u16 pool_index, u32 size,
45 				      enum devlink_sb_threshold_type threshold_type,
46 				      struct netlink_ext_ack *extack)
47 {
48 	struct ocelot *ocelot = devlink_priv(dl);
49 
50 	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
51 				  threshold_type, extack);
52 }
53 
54 static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
55 					   unsigned int sb_index, u16 pool_index,
56 					   u32 *p_threshold)
57 {
58 	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
59 	int port = devlink_port_to_port(dlp);
60 
61 	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
62 				       p_threshold);
63 }
64 
65 static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
66 					   unsigned int sb_index, u16 pool_index,
67 					   u32 threshold,
68 					   struct netlink_ext_ack *extack)
69 {
70 	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
71 	int port = devlink_port_to_port(dlp);
72 
73 	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
74 				       threshold, extack);
75 }
76 
77 static int
78 ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
79 				   unsigned int sb_index, u16 tc_index,
80 				   enum devlink_sb_pool_type pool_type,
81 				   u16 *p_pool_index, u32 *p_threshold)
82 {
83 	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
84 	int port = devlink_port_to_port(dlp);
85 
86 	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
87 					  pool_type, p_pool_index,
88 					  p_threshold);
89 }
90 
91 static int
92 ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
93 				   unsigned int sb_index, u16 tc_index,
94 				   enum devlink_sb_pool_type pool_type,
95 				   u16 pool_index, u32 threshold,
96 				   struct netlink_ext_ack *extack)
97 {
98 	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
99 	int port = devlink_port_to_port(dlp);
100 
101 	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
102 					  pool_type, pool_index, threshold,
103 					  extack);
104 }
105 
106 static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
107 					  unsigned int sb_index)
108 {
109 	struct ocelot *ocelot = devlink_priv(dl);
110 
111 	return ocelot_sb_occ_snapshot(ocelot, sb_index);
112 }
113 
114 static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
115 					   unsigned int sb_index)
116 {
117 	struct ocelot *ocelot = devlink_priv(dl);
118 
119 	return ocelot_sb_occ_max_clear(ocelot, sb_index);
120 }
121 
122 static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
123 					       unsigned int sb_index,
124 					       u16 pool_index, u32 *p_cur,
125 					       u32 *p_max)
126 {
127 	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
128 	int port = devlink_port_to_port(dlp);
129 
130 	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
131 					   p_cur, p_max);
132 }
133 
134 static int
135 ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
136 				       unsigned int sb_index, u16 tc_index,
137 				       enum devlink_sb_pool_type pool_type,
138 				       u32 *p_cur, u32 *p_max)
139 {
140 	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
141 	int port = devlink_port_to_port(dlp);
142 
143 	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
144 					      tc_index, pool_type,
145 					      p_cur, p_max);
146 }
147 
148 const struct devlink_ops ocelot_devlink_ops = {
149 	.sb_pool_get			= ocelot_devlink_sb_pool_get,
150 	.sb_pool_set			= ocelot_devlink_sb_pool_set,
151 	.sb_port_pool_get		= ocelot_devlink_sb_port_pool_get,
152 	.sb_port_pool_set		= ocelot_devlink_sb_port_pool_set,
153 	.sb_tc_pool_bind_get		= ocelot_devlink_sb_tc_pool_bind_get,
154 	.sb_tc_pool_bind_set		= ocelot_devlink_sb_tc_pool_bind_set,
155 	.sb_occ_snapshot		= ocelot_devlink_sb_occ_snapshot,
156 	.sb_occ_max_clear		= ocelot_devlink_sb_occ_max_clear,
157 	.sb_occ_port_pool_get		= ocelot_devlink_sb_occ_port_pool_get,
158 	.sb_occ_tc_port_bind_get	= ocelot_devlink_sb_occ_tc_port_bind_get,
159 };
160 
161 int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
162 			     enum devlink_port_flavour flavour)
163 {
164 	struct devlink_port *dlp = &ocelot->devlink_ports[port];
165 	int id_len = sizeof(ocelot->base_mac);
166 	struct devlink *dl = ocelot->devlink;
167 	struct devlink_port_attrs attrs = {};
168 
169 	memset(dlp, 0, sizeof(*dlp));
170 	memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
171 	attrs.switch_id.id_len = id_len;
172 	attrs.phys.port_number = port;
173 	attrs.flavour = flavour;
174 
175 	devlink_port_attrs_set(dlp, &attrs);
176 
177 	return devlink_port_register(dl, dlp, port);
178 }
179 
180 void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
181 {
182 	struct devlink_port *dlp = &ocelot->devlink_ports[port];
183 
184 	devlink_port_unregister(dlp);
185 }
186 
187 static struct devlink_port *ocelot_get_devlink_port(struct net_device *dev)
188 {
189 	struct ocelot_port_private *priv = netdev_priv(dev);
190 	struct ocelot *ocelot = priv->port.ocelot;
191 	int port = priv->chip_port;
192 
193 	return &ocelot->devlink_ports[port];
194 }
195 
196 int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
197 			       struct flow_cls_offload *f,
198 			       bool ingress)
199 {
200 	struct ocelot *ocelot = priv->port.ocelot;
201 	int port = priv->chip_port;
202 
203 	if (!ingress)
204 		return -EOPNOTSUPP;
205 
206 	switch (f->command) {
207 	case FLOW_CLS_REPLACE:
208 		return ocelot_cls_flower_replace(ocelot, port, f, ingress);
209 	case FLOW_CLS_DESTROY:
210 		return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
211 	case FLOW_CLS_STATS:
212 		return ocelot_cls_flower_stats(ocelot, port, f, ingress);
213 	default:
214 		return -EOPNOTSUPP;
215 	}
216 }
217 
218 static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
219 					struct tc_cls_matchall_offload *f,
220 					bool ingress)
221 {
222 	struct netlink_ext_ack *extack = f->common.extack;
223 	struct ocelot *ocelot = priv->port.ocelot;
224 	struct ocelot_policer pol = { 0 };
225 	struct flow_action_entry *action;
226 	int port = priv->chip_port;
227 	int err;
228 
229 	if (!ingress) {
230 		NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
231 		return -EOPNOTSUPP;
232 	}
233 
234 	switch (f->command) {
235 	case TC_CLSMATCHALL_REPLACE:
236 		if (!flow_offload_has_one_action(&f->rule->action)) {
237 			NL_SET_ERR_MSG_MOD(extack,
238 					   "Only one action is supported");
239 			return -EOPNOTSUPP;
240 		}
241 
242 		if (priv->tc.block_shared) {
243 			NL_SET_ERR_MSG_MOD(extack,
244 					   "Rate limit is not supported on shared blocks");
245 			return -EOPNOTSUPP;
246 		}
247 
248 		action = &f->rule->action.entries[0];
249 
250 		if (action->id != FLOW_ACTION_POLICE) {
251 			NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
252 			return -EOPNOTSUPP;
253 		}
254 
255 		if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
256 			NL_SET_ERR_MSG_MOD(extack,
257 					   "Only one policer per port is supported");
258 			return -EEXIST;
259 		}
260 
261 		if (action->police.rate_pkt_ps) {
262 			NL_SET_ERR_MSG_MOD(extack,
263 					   "QoS offload not support packets per second");
264 			return -EOPNOTSUPP;
265 		}
266 
267 		pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
268 		pol.burst = action->police.burst;
269 
270 		err = ocelot_port_policer_add(ocelot, port, &pol);
271 		if (err) {
272 			NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
273 			return err;
274 		}
275 
276 		priv->tc.police_id = f->cookie;
277 		priv->tc.offload_cnt++;
278 		return 0;
279 	case TC_CLSMATCHALL_DESTROY:
280 		if (priv->tc.police_id != f->cookie)
281 			return -ENOENT;
282 
283 		err = ocelot_port_policer_del(ocelot, port);
284 		if (err) {
285 			NL_SET_ERR_MSG_MOD(extack,
286 					   "Could not delete policer");
287 			return err;
288 		}
289 		priv->tc.police_id = 0;
290 		priv->tc.offload_cnt--;
291 		return 0;
292 	case TC_CLSMATCHALL_STATS:
293 	default:
294 		return -EOPNOTSUPP;
295 	}
296 }
297 
298 static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
299 				    void *type_data,
300 				    void *cb_priv, bool ingress)
301 {
302 	struct ocelot_port_private *priv = cb_priv;
303 
304 	if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
305 		return -EOPNOTSUPP;
306 
307 	switch (type) {
308 	case TC_SETUP_CLSMATCHALL:
309 		return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
310 	case TC_SETUP_CLSFLOWER:
311 		return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
312 	default:
313 		return -EOPNOTSUPP;
314 	}
315 }
316 
317 static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
318 				       void *type_data,
319 				       void *cb_priv)
320 {
321 	return ocelot_setup_tc_block_cb(type, type_data,
322 					cb_priv, true);
323 }
324 
325 static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
326 				       void *type_data,
327 				       void *cb_priv)
328 {
329 	return ocelot_setup_tc_block_cb(type, type_data,
330 					cb_priv, false);
331 }
332 
333 static LIST_HEAD(ocelot_block_cb_list);
334 
335 static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
336 				 struct flow_block_offload *f)
337 {
338 	struct flow_block_cb *block_cb;
339 	flow_setup_cb_t *cb;
340 
341 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
342 		cb = ocelot_setup_tc_block_cb_ig;
343 		priv->tc.block_shared = f->block_shared;
344 	} else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
345 		cb = ocelot_setup_tc_block_cb_eg;
346 	} else {
347 		return -EOPNOTSUPP;
348 	}
349 
350 	f->driver_block_list = &ocelot_block_cb_list;
351 
352 	switch (f->command) {
353 	case FLOW_BLOCK_BIND:
354 		if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
355 			return -EBUSY;
356 
357 		block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
358 		if (IS_ERR(block_cb))
359 			return PTR_ERR(block_cb);
360 
361 		flow_block_cb_add(block_cb, f);
362 		list_add_tail(&block_cb->driver_list, f->driver_block_list);
363 		return 0;
364 	case FLOW_BLOCK_UNBIND:
365 		block_cb = flow_block_cb_lookup(f->block, cb, priv);
366 		if (!block_cb)
367 			return -ENOENT;
368 
369 		flow_block_cb_remove(block_cb, f);
370 		list_del(&block_cb->driver_list);
371 		return 0;
372 	default:
373 		return -EOPNOTSUPP;
374 	}
375 }
376 
377 static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
378 			   void *type_data)
379 {
380 	struct ocelot_port_private *priv = netdev_priv(dev);
381 
382 	switch (type) {
383 	case TC_SETUP_BLOCK:
384 		return ocelot_setup_tc_block(priv, type_data);
385 	default:
386 		return -EOPNOTSUPP;
387 	}
388 	return 0;
389 }
390 
391 static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
392 			       bool untagged)
393 {
394 	struct ocelot_port_private *priv = netdev_priv(dev);
395 	struct ocelot_port *ocelot_port = &priv->port;
396 	struct ocelot *ocelot = ocelot_port->ocelot;
397 	int port = priv->chip_port;
398 	int ret;
399 
400 	ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
401 	if (ret)
402 		return ret;
403 
404 	/* Add the port MAC address to with the right VLAN information */
405 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
406 			  ENTRYTYPE_LOCKED);
407 
408 	return 0;
409 }
410 
411 static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
412 {
413 	struct ocelot_port_private *priv = netdev_priv(dev);
414 	struct ocelot *ocelot = priv->port.ocelot;
415 	int port = priv->chip_port;
416 	int ret;
417 
418 	/* 8021q removes VID 0 on module unload for all interfaces
419 	 * with VLAN filtering feature. We need to keep it to receive
420 	 * untagged traffic.
421 	 */
422 	if (vid == OCELOT_VLAN_UNAWARE_PVID)
423 		return 0;
424 
425 	ret = ocelot_vlan_del(ocelot, port, vid);
426 	if (ret)
427 		return ret;
428 
429 	/* Del the port MAC address to with the right VLAN information */
430 	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
431 
432 	return 0;
433 }
434 
435 static int ocelot_port_open(struct net_device *dev)
436 {
437 	struct ocelot_port_private *priv = netdev_priv(dev);
438 
439 	phylink_start(priv->phylink);
440 
441 	return 0;
442 }
443 
444 static int ocelot_port_stop(struct net_device *dev)
445 {
446 	struct ocelot_port_private *priv = netdev_priv(dev);
447 
448 	phylink_stop(priv->phylink);
449 
450 	return 0;
451 }
452 
453 static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
454 {
455 	struct ocelot_port_private *priv = netdev_priv(dev);
456 	struct ocelot_port *ocelot_port = &priv->port;
457 	struct ocelot *ocelot = ocelot_port->ocelot;
458 	int port = priv->chip_port;
459 	u32 rew_op = 0;
460 
461 	if (!static_branch_unlikely(&ocelot_fdma_enabled) &&
462 	    !ocelot_can_inject(ocelot, 0))
463 		return NETDEV_TX_BUSY;
464 
465 	/* Check if timestamping is needed */
466 	if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
467 		struct sk_buff *clone = NULL;
468 
469 		if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
470 			kfree_skb(skb);
471 			return NETDEV_TX_OK;
472 		}
473 
474 		if (clone)
475 			OCELOT_SKB_CB(skb)->clone = clone;
476 
477 		rew_op = ocelot_ptp_rew_op(skb);
478 	}
479 
480 	if (static_branch_unlikely(&ocelot_fdma_enabled)) {
481 		ocelot_fdma_inject_frame(ocelot, port, rew_op, skb, dev);
482 	} else {
483 		ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
484 
485 		consume_skb(skb);
486 	}
487 
488 	return NETDEV_TX_OK;
489 }
490 
491 enum ocelot_action_type {
492 	OCELOT_MACT_LEARN,
493 	OCELOT_MACT_FORGET,
494 };
495 
496 struct ocelot_mact_work_ctx {
497 	struct work_struct work;
498 	struct ocelot *ocelot;
499 	enum ocelot_action_type type;
500 	union {
501 		/* OCELOT_MACT_LEARN */
502 		struct {
503 			unsigned char addr[ETH_ALEN];
504 			u16 vid;
505 			enum macaccess_entry_type entry_type;
506 			int pgid;
507 		} learn;
508 		/* OCELOT_MACT_FORGET */
509 		struct {
510 			unsigned char addr[ETH_ALEN];
511 			u16 vid;
512 		} forget;
513 	};
514 };
515 
516 #define ocelot_work_to_ctx(x) \
517 	container_of((x), struct ocelot_mact_work_ctx, work)
518 
519 static void ocelot_mact_work(struct work_struct *work)
520 {
521 	struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
522 	struct ocelot *ocelot = w->ocelot;
523 
524 	switch (w->type) {
525 	case OCELOT_MACT_LEARN:
526 		ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
527 				  w->learn.vid, w->learn.entry_type);
528 		break;
529 	case OCELOT_MACT_FORGET:
530 		ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
531 		break;
532 	default:
533 		break;
534 	}
535 
536 	kfree(w);
537 }
538 
539 static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
540 				      const struct ocelot_mact_work_ctx *ctx)
541 {
542 	struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
543 
544 	if (!w)
545 		return -ENOMEM;
546 
547 	w->ocelot = ocelot;
548 	INIT_WORK(&w->work, ocelot_mact_work);
549 	queue_work(ocelot->owq, &w->work);
550 
551 	return 0;
552 }
553 
554 static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
555 {
556 	struct ocelot_port_private *priv = netdev_priv(dev);
557 	struct ocelot_port *ocelot_port = &priv->port;
558 	struct ocelot *ocelot = ocelot_port->ocelot;
559 	struct ocelot_mact_work_ctx w;
560 
561 	ether_addr_copy(w.forget.addr, addr);
562 	w.forget.vid = OCELOT_VLAN_UNAWARE_PVID;
563 	w.type = OCELOT_MACT_FORGET;
564 
565 	return ocelot_enqueue_mact_action(ocelot, &w);
566 }
567 
568 static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
569 {
570 	struct ocelot_port_private *priv = netdev_priv(dev);
571 	struct ocelot_port *ocelot_port = &priv->port;
572 	struct ocelot *ocelot = ocelot_port->ocelot;
573 	struct ocelot_mact_work_ctx w;
574 
575 	ether_addr_copy(w.learn.addr, addr);
576 	w.learn.vid = OCELOT_VLAN_UNAWARE_PVID;
577 	w.learn.pgid = PGID_CPU;
578 	w.learn.entry_type = ENTRYTYPE_LOCKED;
579 	w.type = OCELOT_MACT_LEARN;
580 
581 	return ocelot_enqueue_mact_action(ocelot, &w);
582 }
583 
584 static void ocelot_set_rx_mode(struct net_device *dev)
585 {
586 	struct ocelot_port_private *priv = netdev_priv(dev);
587 	struct ocelot *ocelot = priv->port.ocelot;
588 	u32 val;
589 	int i;
590 
591 	/* This doesn't handle promiscuous mode because the bridge core is
592 	 * setting IFF_PROMISC on all slave interfaces and all frames would be
593 	 * forwarded to the CPU port.
594 	 */
595 	val = GENMASK(ocelot->num_phys_ports - 1, 0);
596 	for_each_nonreserved_multicast_dest_pgid(ocelot, i)
597 		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
598 
599 	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
600 }
601 
602 static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
603 {
604 	struct ocelot_port_private *priv = netdev_priv(dev);
605 	struct ocelot_port *ocelot_port = &priv->port;
606 	struct ocelot *ocelot = ocelot_port->ocelot;
607 	const struct sockaddr *addr = p;
608 
609 	/* Learn the new net device MAC address in the mac table. */
610 	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
611 			  OCELOT_VLAN_UNAWARE_PVID, ENTRYTYPE_LOCKED);
612 	/* Then forget the previous one. */
613 	ocelot_mact_forget(ocelot, dev->dev_addr, OCELOT_VLAN_UNAWARE_PVID);
614 
615 	eth_hw_addr_set(dev, addr->sa_data);
616 	return 0;
617 }
618 
619 static void ocelot_get_stats64(struct net_device *dev,
620 			       struct rtnl_link_stats64 *stats)
621 {
622 	struct ocelot_port_private *priv = netdev_priv(dev);
623 	struct ocelot *ocelot = priv->port.ocelot;
624 	int port = priv->chip_port;
625 
626 	/* Configure the port to read the stats from */
627 	ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
628 		     SYS_STAT_CFG);
629 
630 	/* Get Rx stats */
631 	stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
632 	stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
633 			    ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
634 			    ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
635 			    ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
636 			    ocelot_read(ocelot, SYS_COUNT_RX_64) +
637 			    ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
638 			    ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
639 			    ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
640 			    ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
641 			    ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
642 	stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
643 	stats->rx_dropped = dev->stats.rx_dropped;
644 
645 	/* Get Tx stats */
646 	stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
647 	stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
648 			    ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
649 			    ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
650 			    ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
651 			    ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
652 			    ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
653 	stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
654 			    ocelot_read(ocelot, SYS_COUNT_TX_AGING);
655 	stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
656 }
657 
658 static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
659 			       struct net_device *dev,
660 			       const unsigned char *addr,
661 			       u16 vid, u16 flags,
662 			       struct netlink_ext_ack *extack)
663 {
664 	struct ocelot_port_private *priv = netdev_priv(dev);
665 	struct ocelot *ocelot = priv->port.ocelot;
666 	int port = priv->chip_port;
667 
668 	return ocelot_fdb_add(ocelot, port, addr, vid);
669 }
670 
671 static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
672 			       struct net_device *dev,
673 			       const unsigned char *addr, u16 vid)
674 {
675 	struct ocelot_port_private *priv = netdev_priv(dev);
676 	struct ocelot *ocelot = priv->port.ocelot;
677 	int port = priv->chip_port;
678 
679 	return ocelot_fdb_del(ocelot, port, addr, vid);
680 }
681 
682 static int ocelot_port_fdb_dump(struct sk_buff *skb,
683 				struct netlink_callback *cb,
684 				struct net_device *dev,
685 				struct net_device *filter_dev, int *idx)
686 {
687 	struct ocelot_port_private *priv = netdev_priv(dev);
688 	struct ocelot *ocelot = priv->port.ocelot;
689 	struct ocelot_dump_ctx dump = {
690 		.dev = dev,
691 		.skb = skb,
692 		.cb = cb,
693 		.idx = *idx,
694 	};
695 	int port = priv->chip_port;
696 	int ret;
697 
698 	ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
699 
700 	*idx = dump.idx;
701 
702 	return ret;
703 }
704 
705 static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
706 				  u16 vid)
707 {
708 	return ocelot_vlan_vid_add(dev, vid, false, false);
709 }
710 
711 static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
712 				   u16 vid)
713 {
714 	return ocelot_vlan_vid_del(dev, vid);
715 }
716 
717 static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
718 			     netdev_features_t features)
719 {
720 	u32 val;
721 
722 	/* Filtering */
723 	val = ocelot_read(ocelot, ANA_VLANMASK);
724 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
725 		val |= BIT(port);
726 	else
727 		val &= ~BIT(port);
728 	ocelot_write(ocelot, val, ANA_VLANMASK);
729 }
730 
731 static int ocelot_set_features(struct net_device *dev,
732 			       netdev_features_t features)
733 {
734 	netdev_features_t changed = dev->features ^ features;
735 	struct ocelot_port_private *priv = netdev_priv(dev);
736 	struct ocelot *ocelot = priv->port.ocelot;
737 	int port = priv->chip_port;
738 
739 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
740 	    priv->tc.offload_cnt) {
741 		netdev_err(dev,
742 			   "Cannot disable HW TC offload while offloads active\n");
743 		return -EBUSY;
744 	}
745 
746 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
747 		ocelot_vlan_mode(ocelot, port, features);
748 
749 	return 0;
750 }
751 
752 static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
753 {
754 	struct ocelot_port_private *priv = netdev_priv(dev);
755 	struct ocelot *ocelot = priv->port.ocelot;
756 	int port = priv->chip_port;
757 
758 	/* If the attached PHY device isn't capable of timestamping operations,
759 	 * use our own (when possible).
760 	 */
761 	if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
762 		switch (cmd) {
763 		case SIOCSHWTSTAMP:
764 			return ocelot_hwstamp_set(ocelot, port, ifr);
765 		case SIOCGHWTSTAMP:
766 			return ocelot_hwstamp_get(ocelot, port, ifr);
767 		}
768 	}
769 
770 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
771 }
772 
773 static int ocelot_change_mtu(struct net_device *dev, int new_mtu)
774 {
775 	struct ocelot_port_private *priv = netdev_priv(dev);
776 	struct ocelot_port *ocelot_port = &priv->port;
777 	struct ocelot *ocelot = ocelot_port->ocelot;
778 
779 	ocelot_port_set_maxlen(ocelot, priv->chip_port, new_mtu);
780 	WRITE_ONCE(dev->mtu, new_mtu);
781 
782 	return 0;
783 }
784 
785 static const struct net_device_ops ocelot_port_netdev_ops = {
786 	.ndo_open			= ocelot_port_open,
787 	.ndo_stop			= ocelot_port_stop,
788 	.ndo_start_xmit			= ocelot_port_xmit,
789 	.ndo_change_mtu			= ocelot_change_mtu,
790 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
791 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
792 	.ndo_get_stats64		= ocelot_get_stats64,
793 	.ndo_fdb_add			= ocelot_port_fdb_add,
794 	.ndo_fdb_del			= ocelot_port_fdb_del,
795 	.ndo_fdb_dump			= ocelot_port_fdb_dump,
796 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
797 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
798 	.ndo_set_features		= ocelot_set_features,
799 	.ndo_setup_tc			= ocelot_setup_tc,
800 	.ndo_eth_ioctl			= ocelot_ioctl,
801 	.ndo_get_devlink_port		= ocelot_get_devlink_port,
802 };
803 
804 struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
805 {
806 	struct ocelot_port *ocelot_port = ocelot->ports[port];
807 	struct ocelot_port_private *priv;
808 
809 	if (!ocelot_port)
810 		return NULL;
811 
812 	priv = container_of(ocelot_port, struct ocelot_port_private, port);
813 
814 	return priv->dev;
815 }
816 
817 /* Checks if the net_device instance given to us originates from our driver */
818 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
819 {
820 	return dev->netdev_ops == &ocelot_port_netdev_ops;
821 }
822 
823 int ocelot_netdev_to_port(struct net_device *dev)
824 {
825 	struct ocelot_port_private *priv;
826 
827 	if (!dev || !ocelot_netdevice_dev_check(dev))
828 		return -EINVAL;
829 
830 	priv = netdev_priv(dev);
831 
832 	return priv->chip_port;
833 }
834 
835 static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
836 				    u8 *data)
837 {
838 	struct ocelot_port_private *priv = netdev_priv(netdev);
839 	struct ocelot *ocelot = priv->port.ocelot;
840 	int port = priv->chip_port;
841 
842 	ocelot_get_strings(ocelot, port, sset, data);
843 }
844 
845 static void ocelot_port_get_ethtool_stats(struct net_device *dev,
846 					  struct ethtool_stats *stats,
847 					  u64 *data)
848 {
849 	struct ocelot_port_private *priv = netdev_priv(dev);
850 	struct ocelot *ocelot = priv->port.ocelot;
851 	int port = priv->chip_port;
852 
853 	ocelot_get_ethtool_stats(ocelot, port, data);
854 }
855 
856 static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
857 {
858 	struct ocelot_port_private *priv = netdev_priv(dev);
859 	struct ocelot *ocelot = priv->port.ocelot;
860 	int port = priv->chip_port;
861 
862 	return ocelot_get_sset_count(ocelot, port, sset);
863 }
864 
865 static int ocelot_port_get_ts_info(struct net_device *dev,
866 				   struct ethtool_ts_info *info)
867 {
868 	struct ocelot_port_private *priv = netdev_priv(dev);
869 	struct ocelot *ocelot = priv->port.ocelot;
870 	int port = priv->chip_port;
871 
872 	if (!ocelot->ptp)
873 		return ethtool_op_get_ts_info(dev, info);
874 
875 	return ocelot_get_ts_info(ocelot, port, info);
876 }
877 
878 static const struct ethtool_ops ocelot_ethtool_ops = {
879 	.get_strings		= ocelot_port_get_strings,
880 	.get_ethtool_stats	= ocelot_port_get_ethtool_stats,
881 	.get_sset_count		= ocelot_port_get_sset_count,
882 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
883 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
884 	.get_ts_info		= ocelot_port_get_ts_info,
885 };
886 
887 static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
888 					   u8 state)
889 {
890 	ocelot_bridge_stp_state_set(ocelot, port, state);
891 }
892 
893 static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
894 					unsigned long ageing_clock_t)
895 {
896 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
897 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
898 
899 	ocelot_set_ageing_time(ocelot, ageing_time);
900 }
901 
902 static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
903 {
904 	u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
905 			    ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
906 			    ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
907 	u32 val = 0;
908 
909 	if (mc)
910 		val = cpu_fwd_mcast;
911 
912 	ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
913 		       ANA_PORT_CPU_FWD_CFG, port);
914 }
915 
916 static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
917 				const struct switchdev_attr *attr,
918 				struct netlink_ext_ack *extack)
919 {
920 	struct ocelot_port_private *priv = netdev_priv(dev);
921 	struct ocelot *ocelot = priv->port.ocelot;
922 	int port = priv->chip_port;
923 	int err = 0;
924 
925 	if (ctx && ctx != priv)
926 		return 0;
927 
928 	switch (attr->id) {
929 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
930 		ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
931 		break;
932 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
933 		ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
934 		break;
935 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
936 		ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering,
937 					   extack);
938 		break;
939 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
940 		ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
941 		break;
942 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
943 		err = ocelot_port_pre_bridge_flags(ocelot, port,
944 						   attr->u.brport_flags);
945 		break;
946 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
947 		ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
948 		break;
949 	default:
950 		err = -EOPNOTSUPP;
951 		break;
952 	}
953 
954 	return err;
955 }
956 
957 static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
958 				   bool untagged, struct netlink_ext_ack *extack)
959 {
960 	struct ocelot_port_private *priv = netdev_priv(dev);
961 	struct ocelot_port *ocelot_port = &priv->port;
962 	struct ocelot *ocelot = ocelot_port->ocelot;
963 	int port = priv->chip_port;
964 
965 	return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged, extack);
966 }
967 
968 static int ocelot_port_obj_add_vlan(struct net_device *dev,
969 				    const struct switchdev_obj_port_vlan *vlan,
970 				    struct netlink_ext_ack *extack)
971 {
972 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
973 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
974 	int ret;
975 
976 	ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged, extack);
977 	if (ret)
978 		return ret;
979 
980 	return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
981 }
982 
983 static int ocelot_port_obj_add_mdb(struct net_device *dev,
984 				   const struct switchdev_obj_port_mdb *mdb)
985 {
986 	struct ocelot_port_private *priv = netdev_priv(dev);
987 	struct ocelot_port *ocelot_port = &priv->port;
988 	struct ocelot *ocelot = ocelot_port->ocelot;
989 	int port = priv->chip_port;
990 
991 	return ocelot_port_mdb_add(ocelot, port, mdb);
992 }
993 
994 static int ocelot_port_obj_del_mdb(struct net_device *dev,
995 				   const struct switchdev_obj_port_mdb *mdb)
996 {
997 	struct ocelot_port_private *priv = netdev_priv(dev);
998 	struct ocelot_port *ocelot_port = &priv->port;
999 	struct ocelot *ocelot = ocelot_port->ocelot;
1000 	int port = priv->chip_port;
1001 
1002 	return ocelot_port_mdb_del(ocelot, port, mdb);
1003 }
1004 
1005 static int ocelot_port_obj_mrp_add(struct net_device *dev,
1006 				   const struct switchdev_obj_mrp *mrp)
1007 {
1008 	struct ocelot_port_private *priv = netdev_priv(dev);
1009 	struct ocelot_port *ocelot_port = &priv->port;
1010 	struct ocelot *ocelot = ocelot_port->ocelot;
1011 	int port = priv->chip_port;
1012 
1013 	return ocelot_mrp_add(ocelot, port, mrp);
1014 }
1015 
1016 static int ocelot_port_obj_mrp_del(struct net_device *dev,
1017 				   const struct switchdev_obj_mrp *mrp)
1018 {
1019 	struct ocelot_port_private *priv = netdev_priv(dev);
1020 	struct ocelot_port *ocelot_port = &priv->port;
1021 	struct ocelot *ocelot = ocelot_port->ocelot;
1022 	int port = priv->chip_port;
1023 
1024 	return ocelot_mrp_del(ocelot, port, mrp);
1025 }
1026 
1027 static int
1028 ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1029 				  const struct switchdev_obj_ring_role_mrp *mrp)
1030 {
1031 	struct ocelot_port_private *priv = netdev_priv(dev);
1032 	struct ocelot_port *ocelot_port = &priv->port;
1033 	struct ocelot *ocelot = ocelot_port->ocelot;
1034 	int port = priv->chip_port;
1035 
1036 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1037 }
1038 
1039 static int
1040 ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1041 				  const struct switchdev_obj_ring_role_mrp *mrp)
1042 {
1043 	struct ocelot_port_private *priv = netdev_priv(dev);
1044 	struct ocelot_port *ocelot_port = &priv->port;
1045 	struct ocelot *ocelot = ocelot_port->ocelot;
1046 	int port = priv->chip_port;
1047 
1048 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1049 }
1050 
1051 static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
1052 			       const struct switchdev_obj *obj,
1053 			       struct netlink_ext_ack *extack)
1054 {
1055 	struct ocelot_port_private *priv = netdev_priv(dev);
1056 	int ret = 0;
1057 
1058 	if (ctx && ctx != priv)
1059 		return 0;
1060 
1061 	switch (obj->id) {
1062 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1063 		ret = ocelot_port_obj_add_vlan(dev,
1064 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
1065 					       extack);
1066 		break;
1067 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1068 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1069 		break;
1070 	case SWITCHDEV_OBJ_ID_MRP:
1071 		ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1072 		break;
1073 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1074 		ret = ocelot_port_obj_mrp_add_ring_role(dev,
1075 							SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1076 		break;
1077 	default:
1078 		return -EOPNOTSUPP;
1079 	}
1080 
1081 	return ret;
1082 }
1083 
1084 static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
1085 			       const struct switchdev_obj *obj)
1086 {
1087 	struct ocelot_port_private *priv = netdev_priv(dev);
1088 	int ret = 0;
1089 
1090 	if (ctx && ctx != priv)
1091 		return 0;
1092 
1093 	switch (obj->id) {
1094 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1095 		ret = ocelot_vlan_vid_del(dev,
1096 					  SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
1097 		break;
1098 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1099 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1100 		break;
1101 	case SWITCHDEV_OBJ_ID_MRP:
1102 		ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1103 		break;
1104 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1105 		ret = ocelot_port_obj_mrp_del_ring_role(dev,
1106 							SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1107 		break;
1108 	default:
1109 		return -EOPNOTSUPP;
1110 	}
1111 
1112 	return ret;
1113 }
1114 
1115 static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1116 					struct net_device *brport_dev)
1117 {
1118 	struct switchdev_brport_flags flags = {0};
1119 	int flag;
1120 
1121 	flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1122 
1123 	for_each_set_bit(flag, &flags.mask, 32)
1124 		if (br_port_flag_is_set(brport_dev, BIT(flag)))
1125 			flags.val |= BIT(flag);
1126 
1127 	ocelot_port_bridge_flags(ocelot, port, flags);
1128 }
1129 
1130 static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1131 {
1132 	struct switchdev_brport_flags flags;
1133 
1134 	flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1135 	flags.val = flags.mask & ~BR_LEARNING;
1136 
1137 	ocelot_port_bridge_flags(ocelot, port, flags);
1138 }
1139 
1140 static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1141 				 struct net_device *brport_dev,
1142 				 struct net_device *bridge_dev,
1143 				 struct netlink_ext_ack *extack)
1144 {
1145 	clock_t ageing_time;
1146 	u8 stp_state;
1147 
1148 	ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1149 
1150 	stp_state = br_port_get_stp_state(brport_dev);
1151 	ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1152 
1153 	ageing_time = br_get_ageing_time(bridge_dev);
1154 	ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1155 
1156 	return ocelot_port_vlan_filtering(ocelot, port,
1157 					  br_vlan_enabled(bridge_dev),
1158 					  extack);
1159 }
1160 
1161 static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1162 {
1163 	int err;
1164 
1165 	err = ocelot_port_vlan_filtering(ocelot, port, false, NULL);
1166 	if (err)
1167 		return err;
1168 
1169 	ocelot_clear_brport_flags(ocelot, port);
1170 
1171 	ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1172 
1173 	return 0;
1174 }
1175 
1176 static int ocelot_netdevice_bridge_join(struct net_device *dev,
1177 					struct net_device *brport_dev,
1178 					struct net_device *bridge,
1179 					struct netlink_ext_ack *extack)
1180 {
1181 	struct ocelot_port_private *priv = netdev_priv(dev);
1182 	struct ocelot_port *ocelot_port = &priv->port;
1183 	struct ocelot *ocelot = ocelot_port->ocelot;
1184 	int port = priv->chip_port;
1185 	int err;
1186 
1187 	ocelot_port_bridge_join(ocelot, port, bridge);
1188 
1189 	err = switchdev_bridge_port_offload(brport_dev, dev, priv,
1190 					    &ocelot_switchdev_nb,
1191 					    &ocelot_switchdev_blocking_nb,
1192 					    false, extack);
1193 	if (err)
1194 		goto err_switchdev_offload;
1195 
1196 	err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
1197 	if (err)
1198 		goto err_switchdev_sync;
1199 
1200 	return 0;
1201 
1202 err_switchdev_sync:
1203 	switchdev_bridge_port_unoffload(brport_dev, priv,
1204 					&ocelot_switchdev_nb,
1205 					&ocelot_switchdev_blocking_nb);
1206 err_switchdev_offload:
1207 	ocelot_port_bridge_leave(ocelot, port, bridge);
1208 	return err;
1209 }
1210 
1211 static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev,
1212 					      struct net_device *brport_dev)
1213 {
1214 	struct ocelot_port_private *priv = netdev_priv(dev);
1215 
1216 	switchdev_bridge_port_unoffload(brport_dev, priv,
1217 					&ocelot_switchdev_nb,
1218 					&ocelot_switchdev_blocking_nb);
1219 }
1220 
1221 static int ocelot_netdevice_bridge_leave(struct net_device *dev,
1222 					 struct net_device *brport_dev,
1223 					 struct net_device *bridge)
1224 {
1225 	struct ocelot_port_private *priv = netdev_priv(dev);
1226 	struct ocelot_port *ocelot_port = &priv->port;
1227 	struct ocelot *ocelot = ocelot_port->ocelot;
1228 	int port = priv->chip_port;
1229 	int err;
1230 
1231 	err = ocelot_switchdev_unsync(ocelot, port);
1232 	if (err)
1233 		return err;
1234 
1235 	ocelot_port_bridge_leave(ocelot, port, bridge);
1236 
1237 	return 0;
1238 }
1239 
1240 static int ocelot_netdevice_lag_join(struct net_device *dev,
1241 				     struct net_device *bond,
1242 				     struct netdev_lag_upper_info *info,
1243 				     struct netlink_ext_ack *extack)
1244 {
1245 	struct ocelot_port_private *priv = netdev_priv(dev);
1246 	struct ocelot_port *ocelot_port = &priv->port;
1247 	struct ocelot *ocelot = ocelot_port->ocelot;
1248 	struct net_device *bridge_dev;
1249 	int port = priv->chip_port;
1250 	int err;
1251 
1252 	err = ocelot_port_lag_join(ocelot, port, bond, info);
1253 	if (err == -EOPNOTSUPP) {
1254 		NL_SET_ERR_MSG_MOD(extack, "Offloading not supported");
1255 		return 0;
1256 	}
1257 
1258 	bridge_dev = netdev_master_upper_dev_get(bond);
1259 	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1260 		return 0;
1261 
1262 	err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
1263 	if (err)
1264 		goto err_bridge_join;
1265 
1266 	return 0;
1267 
1268 err_bridge_join:
1269 	ocelot_port_lag_leave(ocelot, port, bond);
1270 	return err;
1271 }
1272 
1273 static void ocelot_netdevice_pre_lag_leave(struct net_device *dev,
1274 					   struct net_device *bond)
1275 {
1276 	struct net_device *bridge_dev;
1277 
1278 	bridge_dev = netdev_master_upper_dev_get(bond);
1279 	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1280 		return;
1281 
1282 	ocelot_netdevice_pre_bridge_leave(dev, bond);
1283 }
1284 
1285 static int ocelot_netdevice_lag_leave(struct net_device *dev,
1286 				      struct net_device *bond)
1287 {
1288 	struct ocelot_port_private *priv = netdev_priv(dev);
1289 	struct ocelot_port *ocelot_port = &priv->port;
1290 	struct ocelot *ocelot = ocelot_port->ocelot;
1291 	struct net_device *bridge_dev;
1292 	int port = priv->chip_port;
1293 
1294 	ocelot_port_lag_leave(ocelot, port, bond);
1295 
1296 	bridge_dev = netdev_master_upper_dev_get(bond);
1297 	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1298 		return 0;
1299 
1300 	return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
1301 }
1302 
1303 static int ocelot_netdevice_changeupper(struct net_device *dev,
1304 					struct net_device *brport_dev,
1305 					struct netdev_notifier_changeupper_info *info)
1306 {
1307 	struct netlink_ext_ack *extack;
1308 	int err = 0;
1309 
1310 	extack = netdev_notifier_info_to_extack(&info->info);
1311 
1312 	if (netif_is_bridge_master(info->upper_dev)) {
1313 		if (info->linking)
1314 			err = ocelot_netdevice_bridge_join(dev, brport_dev,
1315 							   info->upper_dev,
1316 							   extack);
1317 		else
1318 			err = ocelot_netdevice_bridge_leave(dev, brport_dev,
1319 							    info->upper_dev);
1320 	}
1321 	if (netif_is_lag_master(info->upper_dev)) {
1322 		if (info->linking)
1323 			err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1324 							info->upper_info, extack);
1325 		else
1326 			ocelot_netdevice_lag_leave(dev, info->upper_dev);
1327 	}
1328 
1329 	return notifier_from_errno(err);
1330 }
1331 
1332 /* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1333  * events for the lower physical ports of the LAG.
1334  * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1335  * In case the LAG joined a bridge, notify that we are offloading it and can do
1336  * forwarding in hardware towards it.
1337  */
1338 static int
1339 ocelot_netdevice_lag_changeupper(struct net_device *dev,
1340 				 struct netdev_notifier_changeupper_info *info)
1341 {
1342 	struct net_device *lower;
1343 	struct list_head *iter;
1344 	int err = NOTIFY_DONE;
1345 
1346 	netdev_for_each_lower_dev(dev, lower, iter) {
1347 		struct ocelot_port_private *priv = netdev_priv(lower);
1348 		struct ocelot_port *ocelot_port = &priv->port;
1349 
1350 		if (ocelot_port->bond != dev)
1351 			return NOTIFY_OK;
1352 
1353 		err = ocelot_netdevice_changeupper(lower, dev, info);
1354 		if (err)
1355 			return notifier_from_errno(err);
1356 	}
1357 
1358 	return NOTIFY_DONE;
1359 }
1360 
1361 static int
1362 ocelot_netdevice_prechangeupper(struct net_device *dev,
1363 				struct net_device *brport_dev,
1364 				struct netdev_notifier_changeupper_info *info)
1365 {
1366 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
1367 		ocelot_netdevice_pre_bridge_leave(dev, brport_dev);
1368 
1369 	if (netif_is_lag_master(info->upper_dev) && !info->linking)
1370 		ocelot_netdevice_pre_lag_leave(dev, info->upper_dev);
1371 
1372 	return NOTIFY_DONE;
1373 }
1374 
1375 static int
1376 ocelot_netdevice_lag_prechangeupper(struct net_device *dev,
1377 				    struct netdev_notifier_changeupper_info *info)
1378 {
1379 	struct net_device *lower;
1380 	struct list_head *iter;
1381 	int err = NOTIFY_DONE;
1382 
1383 	netdev_for_each_lower_dev(dev, lower, iter) {
1384 		struct ocelot_port_private *priv = netdev_priv(lower);
1385 		struct ocelot_port *ocelot_port = &priv->port;
1386 
1387 		if (ocelot_port->bond != dev)
1388 			return NOTIFY_OK;
1389 
1390 		err = ocelot_netdevice_prechangeupper(dev, lower, info);
1391 		if (err)
1392 			return err;
1393 	}
1394 
1395 	return NOTIFY_DONE;
1396 }
1397 
1398 static int
1399 ocelot_netdevice_changelowerstate(struct net_device *dev,
1400 				  struct netdev_lag_lower_state_info *info)
1401 {
1402 	struct ocelot_port_private *priv = netdev_priv(dev);
1403 	bool is_active = info->link_up && info->tx_enabled;
1404 	struct ocelot_port *ocelot_port = &priv->port;
1405 	struct ocelot *ocelot = ocelot_port->ocelot;
1406 	int port = priv->chip_port;
1407 
1408 	if (!ocelot_port->bond)
1409 		return NOTIFY_DONE;
1410 
1411 	if (ocelot_port->lag_tx_active == is_active)
1412 		return NOTIFY_DONE;
1413 
1414 	ocelot_port_lag_change(ocelot, port, is_active);
1415 
1416 	return NOTIFY_OK;
1417 }
1418 
1419 static int ocelot_netdevice_event(struct notifier_block *unused,
1420 				  unsigned long event, void *ptr)
1421 {
1422 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1423 
1424 	switch (event) {
1425 	case NETDEV_PRECHANGEUPPER: {
1426 		struct netdev_notifier_changeupper_info *info = ptr;
1427 
1428 		if (ocelot_netdevice_dev_check(dev))
1429 			return ocelot_netdevice_prechangeupper(dev, dev, info);
1430 
1431 		if (netif_is_lag_master(dev))
1432 			return ocelot_netdevice_lag_prechangeupper(dev, info);
1433 
1434 		break;
1435 	}
1436 	case NETDEV_CHANGEUPPER: {
1437 		struct netdev_notifier_changeupper_info *info = ptr;
1438 
1439 		if (ocelot_netdevice_dev_check(dev))
1440 			return ocelot_netdevice_changeupper(dev, dev, info);
1441 
1442 		if (netif_is_lag_master(dev))
1443 			return ocelot_netdevice_lag_changeupper(dev, info);
1444 
1445 		break;
1446 	}
1447 	case NETDEV_CHANGELOWERSTATE: {
1448 		struct netdev_notifier_changelowerstate_info *info = ptr;
1449 
1450 		if (!ocelot_netdevice_dev_check(dev))
1451 			break;
1452 
1453 		return ocelot_netdevice_changelowerstate(dev,
1454 							 info->lower_state_info);
1455 	}
1456 	default:
1457 		break;
1458 	}
1459 
1460 	return NOTIFY_DONE;
1461 }
1462 
1463 struct notifier_block ocelot_netdevice_nb __read_mostly = {
1464 	.notifier_call = ocelot_netdevice_event,
1465 };
1466 
1467 static int ocelot_switchdev_event(struct notifier_block *unused,
1468 				  unsigned long event, void *ptr)
1469 {
1470 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1471 	int err;
1472 
1473 	switch (event) {
1474 	case SWITCHDEV_PORT_ATTR_SET:
1475 		err = switchdev_handle_port_attr_set(dev, ptr,
1476 						     ocelot_netdevice_dev_check,
1477 						     ocelot_port_attr_set);
1478 		return notifier_from_errno(err);
1479 	}
1480 
1481 	return NOTIFY_DONE;
1482 }
1483 
1484 struct notifier_block ocelot_switchdev_nb __read_mostly = {
1485 	.notifier_call = ocelot_switchdev_event,
1486 };
1487 
1488 static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1489 					   unsigned long event, void *ptr)
1490 {
1491 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1492 	int err;
1493 
1494 	switch (event) {
1495 		/* Blocking events. */
1496 	case SWITCHDEV_PORT_OBJ_ADD:
1497 		err = switchdev_handle_port_obj_add(dev, ptr,
1498 						    ocelot_netdevice_dev_check,
1499 						    ocelot_port_obj_add);
1500 		return notifier_from_errno(err);
1501 	case SWITCHDEV_PORT_OBJ_DEL:
1502 		err = switchdev_handle_port_obj_del(dev, ptr,
1503 						    ocelot_netdevice_dev_check,
1504 						    ocelot_port_obj_del);
1505 		return notifier_from_errno(err);
1506 	case SWITCHDEV_PORT_ATTR_SET:
1507 		err = switchdev_handle_port_attr_set(dev, ptr,
1508 						     ocelot_netdevice_dev_check,
1509 						     ocelot_port_attr_set);
1510 		return notifier_from_errno(err);
1511 	}
1512 
1513 	return NOTIFY_DONE;
1514 }
1515 
1516 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1517 	.notifier_call = ocelot_switchdev_blocking_event,
1518 };
1519 
1520 static void vsc7514_phylink_mac_config(struct phylink_config *config,
1521 				       unsigned int link_an_mode,
1522 				       const struct phylink_link_state *state)
1523 {
1524 	struct net_device *ndev = to_net_dev(config->dev);
1525 	struct ocelot_port_private *priv = netdev_priv(ndev);
1526 	struct ocelot_port *ocelot_port = &priv->port;
1527 
1528 	/* Disable HDX fast control */
1529 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
1530 			   DEV_PORT_MISC);
1531 
1532 	/* SGMII only for now */
1533 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
1534 			   PCS1G_MODE_CFG);
1535 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
1536 
1537 	/* Enable PCS */
1538 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
1539 
1540 	/* No aneg on SGMII */
1541 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
1542 
1543 	/* No loopback */
1544 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
1545 }
1546 
1547 static void vsc7514_phylink_mac_link_down(struct phylink_config *config,
1548 					  unsigned int link_an_mode,
1549 					  phy_interface_t interface)
1550 {
1551 	struct net_device *ndev = to_net_dev(config->dev);
1552 	struct ocelot_port_private *priv = netdev_priv(ndev);
1553 	struct ocelot *ocelot = priv->port.ocelot;
1554 	int port = priv->chip_port;
1555 
1556 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1557 				     OCELOT_MAC_QUIRKS);
1558 }
1559 
1560 static void vsc7514_phylink_mac_link_up(struct phylink_config *config,
1561 					struct phy_device *phydev,
1562 					unsigned int link_an_mode,
1563 					phy_interface_t interface,
1564 					int speed, int duplex,
1565 					bool tx_pause, bool rx_pause)
1566 {
1567 	struct net_device *ndev = to_net_dev(config->dev);
1568 	struct ocelot_port_private *priv = netdev_priv(ndev);
1569 	struct ocelot *ocelot = priv->port.ocelot;
1570 	int port = priv->chip_port;
1571 
1572 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1573 				   interface, speed, duplex,
1574 				   tx_pause, rx_pause, OCELOT_MAC_QUIRKS);
1575 }
1576 
1577 static const struct phylink_mac_ops ocelot_phylink_ops = {
1578 	.validate		= phylink_generic_validate,
1579 	.mac_config		= vsc7514_phylink_mac_config,
1580 	.mac_link_down		= vsc7514_phylink_mac_link_down,
1581 	.mac_link_up		= vsc7514_phylink_mac_link_up,
1582 };
1583 
1584 static int ocelot_port_phylink_create(struct ocelot *ocelot, int port,
1585 				      struct device_node *portnp)
1586 {
1587 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1588 	struct ocelot_port_private *priv;
1589 	struct device *dev = ocelot->dev;
1590 	phy_interface_t phy_mode;
1591 	struct phylink *phylink;
1592 	int err;
1593 
1594 	of_get_phy_mode(portnp, &phy_mode);
1595 	/* DT bindings of internal PHY ports are broken and don't
1596 	 * specify a phy-mode
1597 	 */
1598 	if (phy_mode == PHY_INTERFACE_MODE_NA)
1599 		phy_mode = PHY_INTERFACE_MODE_INTERNAL;
1600 
1601 	if (phy_mode != PHY_INTERFACE_MODE_SGMII &&
1602 	    phy_mode != PHY_INTERFACE_MODE_QSGMII &&
1603 	    phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1604 		dev_err(dev, "unsupported phy mode %s for port %d\n",
1605 			phy_modes(phy_mode), port);
1606 		return -EINVAL;
1607 	}
1608 
1609 	/* Ensure clock signals and speed are set on all QSGMII links */
1610 	if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
1611 		ocelot_port_rmwl(ocelot_port, 0,
1612 				 DEV_CLOCK_CFG_MAC_TX_RST |
1613 				 DEV_CLOCK_CFG_MAC_RX_RST,
1614 				 DEV_CLOCK_CFG);
1615 
1616 	ocelot_port->phy_mode = phy_mode;
1617 
1618 	if (phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1619 		struct phy *serdes = of_phy_get(portnp, NULL);
1620 
1621 		if (IS_ERR(serdes)) {
1622 			err = PTR_ERR(serdes);
1623 			dev_err_probe(dev, err,
1624 				      "missing SerDes phys for port %d\n",
1625 				      port);
1626 			return err;
1627 		}
1628 
1629 		err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, phy_mode);
1630 		of_phy_put(serdes);
1631 		if (err) {
1632 			dev_err(dev, "Could not SerDes mode on port %d: %pe\n",
1633 				port, ERR_PTR(err));
1634 			return err;
1635 		}
1636 	}
1637 
1638 	priv = container_of(ocelot_port, struct ocelot_port_private, port);
1639 
1640 	priv->phylink_config.dev = &priv->dev->dev;
1641 	priv->phylink_config.type = PHYLINK_NETDEV;
1642 	priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1643 		MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
1644 
1645 	__set_bit(ocelot_port->phy_mode,
1646 		  priv->phylink_config.supported_interfaces);
1647 
1648 	phylink = phylink_create(&priv->phylink_config,
1649 				 of_fwnode_handle(portnp),
1650 				 phy_mode, &ocelot_phylink_ops);
1651 	if (IS_ERR(phylink)) {
1652 		err = PTR_ERR(phylink);
1653 		dev_err(dev, "Could not create phylink (%pe)\n", phylink);
1654 		return err;
1655 	}
1656 
1657 	priv->phylink = phylink;
1658 
1659 	err = phylink_of_phy_connect(phylink, portnp, 0);
1660 	if (err) {
1661 		dev_err(dev, "Could not connect to PHY: %pe\n", ERR_PTR(err));
1662 		phylink_destroy(phylink);
1663 		priv->phylink = NULL;
1664 		return err;
1665 	}
1666 
1667 	return 0;
1668 }
1669 
1670 int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
1671 		      struct device_node *portnp)
1672 {
1673 	struct ocelot_port_private *priv;
1674 	struct ocelot_port *ocelot_port;
1675 	struct net_device *dev;
1676 	int err;
1677 
1678 	dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1679 	if (!dev)
1680 		return -ENOMEM;
1681 	SET_NETDEV_DEV(dev, ocelot->dev);
1682 	priv = netdev_priv(dev);
1683 	priv->dev = dev;
1684 	priv->chip_port = port;
1685 	ocelot_port = &priv->port;
1686 	ocelot_port->ocelot = ocelot;
1687 	ocelot_port->target = target;
1688 	ocelot->ports[port] = ocelot_port;
1689 
1690 	dev->netdev_ops = &ocelot_port_netdev_ops;
1691 	dev->ethtool_ops = &ocelot_ethtool_ops;
1692 	dev->max_mtu = OCELOT_JUMBO_MTU;
1693 
1694 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1695 		NETIF_F_HW_TC;
1696 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1697 
1698 	err = of_get_ethdev_address(portnp, dev);
1699 	if (err)
1700 		eth_hw_addr_gen(dev, ocelot->base_mac, port);
1701 
1702 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1703 			  OCELOT_VLAN_UNAWARE_PVID, ENTRYTYPE_LOCKED);
1704 
1705 	ocelot_init_port(ocelot, port);
1706 
1707 	err = ocelot_port_phylink_create(ocelot, port, portnp);
1708 	if (err)
1709 		goto out;
1710 
1711 	if (ocelot->fdma)
1712 		ocelot_fdma_netdev_init(ocelot, dev);
1713 
1714 	err = register_netdev(dev);
1715 	if (err) {
1716 		dev_err(ocelot->dev, "register_netdev failed\n");
1717 		goto out_fdma_deinit;
1718 	}
1719 
1720 	return 0;
1721 
1722 out_fdma_deinit:
1723 	if (ocelot->fdma)
1724 		ocelot_fdma_netdev_deinit(ocelot, dev);
1725 out:
1726 	ocelot->ports[port] = NULL;
1727 	free_netdev(dev);
1728 
1729 	return err;
1730 }
1731 
1732 void ocelot_release_port(struct ocelot_port *ocelot_port)
1733 {
1734 	struct ocelot_port_private *priv = container_of(ocelot_port,
1735 						struct ocelot_port_private,
1736 						port);
1737 	struct ocelot *ocelot = ocelot_port->ocelot;
1738 	struct ocelot_fdma *fdma = ocelot->fdma;
1739 
1740 	unregister_netdev(priv->dev);
1741 
1742 	if (fdma)
1743 		ocelot_fdma_netdev_deinit(ocelot, priv->dev);
1744 
1745 	if (priv->phylink) {
1746 		rtnl_lock();
1747 		phylink_disconnect_phy(priv->phylink);
1748 		rtnl_unlock();
1749 
1750 		phylink_destroy(priv->phylink);
1751 	}
1752 
1753 	free_netdev(priv->dev);
1754 }
1755