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