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->port.index;
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->port.index;
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->port.index;
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->port.index,
292 				     other_priv->port.index, 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->port.index;
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->port.index;
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->port.index;
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->port.index;
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->port.index;
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->port.index;
728 	u64 *s;
729 
730 	spin_lock(&ocelot->stats_lock);
731 
732 	s = &ocelot->stats[port * OCELOT_NUM_STATS];
733 
734 	/* Get Rx stats */
735 	stats->rx_bytes = s[OCELOT_STAT_RX_OCTETS];
736 	stats->rx_packets = s[OCELOT_STAT_RX_SHORTS] +
737 			    s[OCELOT_STAT_RX_FRAGMENTS] +
738 			    s[OCELOT_STAT_RX_JABBERS] +
739 			    s[OCELOT_STAT_RX_LONGS] +
740 			    s[OCELOT_STAT_RX_64] +
741 			    s[OCELOT_STAT_RX_65_127] +
742 			    s[OCELOT_STAT_RX_128_255] +
743 			    s[OCELOT_STAT_RX_256_511] +
744 			    s[OCELOT_STAT_RX_512_1023] +
745 			    s[OCELOT_STAT_RX_1024_1526] +
746 			    s[OCELOT_STAT_RX_1527_MAX];
747 	stats->multicast = s[OCELOT_STAT_RX_MULTICAST];
748 	stats->rx_dropped = dev->stats.rx_dropped;
749 
750 	/* Get Tx stats */
751 	stats->tx_bytes = s[OCELOT_STAT_TX_OCTETS];
752 	stats->tx_packets = s[OCELOT_STAT_TX_64] +
753 			    s[OCELOT_STAT_TX_65_127] +
754 			    s[OCELOT_STAT_TX_128_255] +
755 			    s[OCELOT_STAT_TX_256_511] +
756 			    s[OCELOT_STAT_TX_512_1023] +
757 			    s[OCELOT_STAT_TX_1024_1526] +
758 			    s[OCELOT_STAT_TX_1527_MAX];
759 	stats->tx_dropped = s[OCELOT_STAT_TX_DROPS] +
760 			    s[OCELOT_STAT_TX_AGED];
761 	stats->collisions = s[OCELOT_STAT_TX_COLLISION];
762 
763 	spin_unlock(&ocelot->stats_lock);
764 }
765 
766 static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
767 			       struct net_device *dev,
768 			       const unsigned char *addr,
769 			       u16 vid, u16 flags,
770 			       struct netlink_ext_ack *extack)
771 {
772 	struct ocelot_port_private *priv = netdev_priv(dev);
773 	struct ocelot_port *ocelot_port = &priv->port;
774 	struct ocelot *ocelot = ocelot_port->ocelot;
775 	int port = priv->port.index;
776 
777 	return ocelot_fdb_add(ocelot, port, addr, vid, ocelot_port->bridge);
778 }
779 
780 static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
781 			       struct net_device *dev,
782 			       const unsigned char *addr, u16 vid,
783 			       struct netlink_ext_ack *extack)
784 {
785 	struct ocelot_port_private *priv = netdev_priv(dev);
786 	struct ocelot_port *ocelot_port = &priv->port;
787 	struct ocelot *ocelot = ocelot_port->ocelot;
788 	int port = priv->port.index;
789 
790 	return ocelot_fdb_del(ocelot, port, addr, vid, ocelot_port->bridge);
791 }
792 
793 static int ocelot_port_fdb_dump(struct sk_buff *skb,
794 				struct netlink_callback *cb,
795 				struct net_device *dev,
796 				struct net_device *filter_dev, int *idx)
797 {
798 	struct ocelot_port_private *priv = netdev_priv(dev);
799 	struct ocelot *ocelot = priv->port.ocelot;
800 	struct ocelot_dump_ctx dump = {
801 		.dev = dev,
802 		.skb = skb,
803 		.cb = cb,
804 		.idx = *idx,
805 	};
806 	int port = priv->port.index;
807 	int ret;
808 
809 	ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
810 
811 	*idx = dump.idx;
812 
813 	return ret;
814 }
815 
816 static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
817 				  u16 vid)
818 {
819 	return ocelot_vlan_vid_add(dev, vid, false, false);
820 }
821 
822 static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
823 				   u16 vid)
824 {
825 	return ocelot_vlan_vid_del(dev, vid);
826 }
827 
828 static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
829 			     netdev_features_t features)
830 {
831 	u32 val;
832 
833 	/* Filtering */
834 	val = ocelot_read(ocelot, ANA_VLANMASK);
835 	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
836 		val |= BIT(port);
837 	else
838 		val &= ~BIT(port);
839 	ocelot_write(ocelot, val, ANA_VLANMASK);
840 }
841 
842 static int ocelot_set_features(struct net_device *dev,
843 			       netdev_features_t features)
844 {
845 	netdev_features_t changed = dev->features ^ features;
846 	struct ocelot_port_private *priv = netdev_priv(dev);
847 	struct ocelot *ocelot = priv->port.ocelot;
848 	int port = priv->port.index;
849 
850 	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
851 	    priv->tc.offload_cnt) {
852 		netdev_err(dev,
853 			   "Cannot disable HW TC offload while offloads active\n");
854 		return -EBUSY;
855 	}
856 
857 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
858 		ocelot_vlan_mode(ocelot, port, features);
859 
860 	return 0;
861 }
862 
863 static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
864 {
865 	struct ocelot_port_private *priv = netdev_priv(dev);
866 	struct ocelot *ocelot = priv->port.ocelot;
867 	int port = priv->port.index;
868 
869 	/* If the attached PHY device isn't capable of timestamping operations,
870 	 * use our own (when possible).
871 	 */
872 	if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
873 		switch (cmd) {
874 		case SIOCSHWTSTAMP:
875 			return ocelot_hwstamp_set(ocelot, port, ifr);
876 		case SIOCGHWTSTAMP:
877 			return ocelot_hwstamp_get(ocelot, port, ifr);
878 		}
879 	}
880 
881 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
882 }
883 
884 static int ocelot_change_mtu(struct net_device *dev, int new_mtu)
885 {
886 	struct ocelot_port_private *priv = netdev_priv(dev);
887 	struct ocelot_port *ocelot_port = &priv->port;
888 	struct ocelot *ocelot = ocelot_port->ocelot;
889 
890 	ocelot_port_set_maxlen(ocelot, priv->port.index, new_mtu);
891 	WRITE_ONCE(dev->mtu, new_mtu);
892 
893 	return 0;
894 }
895 
896 static const struct net_device_ops ocelot_port_netdev_ops = {
897 	.ndo_open			= ocelot_port_open,
898 	.ndo_stop			= ocelot_port_stop,
899 	.ndo_start_xmit			= ocelot_port_xmit,
900 	.ndo_change_mtu			= ocelot_change_mtu,
901 	.ndo_set_rx_mode		= ocelot_set_rx_mode,
902 	.ndo_set_mac_address		= ocelot_port_set_mac_address,
903 	.ndo_get_stats64		= ocelot_get_stats64,
904 	.ndo_fdb_add			= ocelot_port_fdb_add,
905 	.ndo_fdb_del			= ocelot_port_fdb_del,
906 	.ndo_fdb_dump			= ocelot_port_fdb_dump,
907 	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
908 	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
909 	.ndo_set_features		= ocelot_set_features,
910 	.ndo_setup_tc			= ocelot_setup_tc,
911 	.ndo_eth_ioctl			= ocelot_ioctl,
912 	.ndo_get_devlink_port		= ocelot_get_devlink_port,
913 };
914 
915 struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
916 {
917 	struct ocelot_port *ocelot_port = ocelot->ports[port];
918 	struct ocelot_port_private *priv;
919 
920 	if (!ocelot_port)
921 		return NULL;
922 
923 	priv = container_of(ocelot_port, struct ocelot_port_private, port);
924 
925 	return priv->dev;
926 }
927 
928 /* Checks if the net_device instance given to us originates from our driver */
929 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
930 {
931 	return dev->netdev_ops == &ocelot_port_netdev_ops;
932 }
933 
934 int ocelot_netdev_to_port(struct net_device *dev)
935 {
936 	struct ocelot_port_private *priv;
937 
938 	if (!dev || !ocelot_netdevice_dev_check(dev))
939 		return -EINVAL;
940 
941 	priv = netdev_priv(dev);
942 
943 	return priv->port.index;
944 }
945 
946 static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
947 				    u8 *data)
948 {
949 	struct ocelot_port_private *priv = netdev_priv(netdev);
950 	struct ocelot *ocelot = priv->port.ocelot;
951 	int port = priv->port.index;
952 
953 	ocelot_get_strings(ocelot, port, sset, data);
954 }
955 
956 static void ocelot_port_get_ethtool_stats(struct net_device *dev,
957 					  struct ethtool_stats *stats,
958 					  u64 *data)
959 {
960 	struct ocelot_port_private *priv = netdev_priv(dev);
961 	struct ocelot *ocelot = priv->port.ocelot;
962 	int port = priv->port.index;
963 
964 	ocelot_get_ethtool_stats(ocelot, port, data);
965 }
966 
967 static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
968 {
969 	struct ocelot_port_private *priv = netdev_priv(dev);
970 	struct ocelot *ocelot = priv->port.ocelot;
971 	int port = priv->port.index;
972 
973 	return ocelot_get_sset_count(ocelot, port, sset);
974 }
975 
976 static int ocelot_port_get_ts_info(struct net_device *dev,
977 				   struct ethtool_ts_info *info)
978 {
979 	struct ocelot_port_private *priv = netdev_priv(dev);
980 	struct ocelot *ocelot = priv->port.ocelot;
981 	int port = priv->port.index;
982 
983 	if (!ocelot->ptp)
984 		return ethtool_op_get_ts_info(dev, info);
985 
986 	return ocelot_get_ts_info(ocelot, port, info);
987 }
988 
989 static const struct ethtool_ops ocelot_ethtool_ops = {
990 	.get_strings		= ocelot_port_get_strings,
991 	.get_ethtool_stats	= ocelot_port_get_ethtool_stats,
992 	.get_sset_count		= ocelot_port_get_sset_count,
993 	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
994 	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
995 	.get_ts_info		= ocelot_port_get_ts_info,
996 };
997 
998 static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
999 					   u8 state)
1000 {
1001 	ocelot_bridge_stp_state_set(ocelot, port, state);
1002 }
1003 
1004 static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
1005 					unsigned long ageing_clock_t)
1006 {
1007 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1008 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
1009 
1010 	ocelot_set_ageing_time(ocelot, ageing_time);
1011 }
1012 
1013 static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
1014 {
1015 	u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1016 			    ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1017 			    ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1018 	u32 val = 0;
1019 
1020 	if (mc)
1021 		val = cpu_fwd_mcast;
1022 
1023 	ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
1024 		       ANA_PORT_CPU_FWD_CFG, port);
1025 }
1026 
1027 static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
1028 				const struct switchdev_attr *attr,
1029 				struct netlink_ext_ack *extack)
1030 {
1031 	struct ocelot_port_private *priv = netdev_priv(dev);
1032 	struct ocelot *ocelot = priv->port.ocelot;
1033 	int port = priv->port.index;
1034 	int err = 0;
1035 
1036 	if (ctx && ctx != priv)
1037 		return 0;
1038 
1039 	switch (attr->id) {
1040 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1041 		ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
1042 		break;
1043 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1044 		ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
1045 		break;
1046 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1047 		ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering,
1048 					   extack);
1049 		break;
1050 	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1051 		ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
1052 		break;
1053 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
1054 		err = ocelot_port_pre_bridge_flags(ocelot, port,
1055 						   attr->u.brport_flags);
1056 		break;
1057 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
1058 		ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
1059 		break;
1060 	default:
1061 		err = -EOPNOTSUPP;
1062 		break;
1063 	}
1064 
1065 	return err;
1066 }
1067 
1068 static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
1069 				   bool untagged, struct netlink_ext_ack *extack)
1070 {
1071 	struct ocelot_port_private *priv = netdev_priv(dev);
1072 	struct ocelot_port *ocelot_port = &priv->port;
1073 	struct ocelot *ocelot = ocelot_port->ocelot;
1074 	int port = priv->port.index;
1075 
1076 	return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged, extack);
1077 }
1078 
1079 static int ocelot_port_obj_add_vlan(struct net_device *dev,
1080 				    const struct switchdev_obj_port_vlan *vlan,
1081 				    struct netlink_ext_ack *extack)
1082 {
1083 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1084 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1085 	int ret;
1086 
1087 	ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged, extack);
1088 	if (ret)
1089 		return ret;
1090 
1091 	return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
1092 }
1093 
1094 static int ocelot_port_obj_add_mdb(struct net_device *dev,
1095 				   const struct switchdev_obj_port_mdb *mdb)
1096 {
1097 	struct ocelot_port_private *priv = netdev_priv(dev);
1098 	struct ocelot_port *ocelot_port = &priv->port;
1099 	struct ocelot *ocelot = ocelot_port->ocelot;
1100 	int port = priv->port.index;
1101 
1102 	return ocelot_port_mdb_add(ocelot, port, mdb, ocelot_port->bridge);
1103 }
1104 
1105 static int ocelot_port_obj_del_mdb(struct net_device *dev,
1106 				   const struct switchdev_obj_port_mdb *mdb)
1107 {
1108 	struct ocelot_port_private *priv = netdev_priv(dev);
1109 	struct ocelot_port *ocelot_port = &priv->port;
1110 	struct ocelot *ocelot = ocelot_port->ocelot;
1111 	int port = priv->port.index;
1112 
1113 	return ocelot_port_mdb_del(ocelot, port, mdb, ocelot_port->bridge);
1114 }
1115 
1116 static int ocelot_port_obj_mrp_add(struct net_device *dev,
1117 				   const struct switchdev_obj_mrp *mrp)
1118 {
1119 	struct ocelot_port_private *priv = netdev_priv(dev);
1120 	struct ocelot_port *ocelot_port = &priv->port;
1121 	struct ocelot *ocelot = ocelot_port->ocelot;
1122 	int port = priv->port.index;
1123 
1124 	return ocelot_mrp_add(ocelot, port, mrp);
1125 }
1126 
1127 static int ocelot_port_obj_mrp_del(struct net_device *dev,
1128 				   const struct switchdev_obj_mrp *mrp)
1129 {
1130 	struct ocelot_port_private *priv = netdev_priv(dev);
1131 	struct ocelot_port *ocelot_port = &priv->port;
1132 	struct ocelot *ocelot = ocelot_port->ocelot;
1133 	int port = priv->port.index;
1134 
1135 	return ocelot_mrp_del(ocelot, port, mrp);
1136 }
1137 
1138 static int
1139 ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1140 				  const struct switchdev_obj_ring_role_mrp *mrp)
1141 {
1142 	struct ocelot_port_private *priv = netdev_priv(dev);
1143 	struct ocelot_port *ocelot_port = &priv->port;
1144 	struct ocelot *ocelot = ocelot_port->ocelot;
1145 	int port = priv->port.index;
1146 
1147 	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1148 }
1149 
1150 static int
1151 ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1152 				  const struct switchdev_obj_ring_role_mrp *mrp)
1153 {
1154 	struct ocelot_port_private *priv = netdev_priv(dev);
1155 	struct ocelot_port *ocelot_port = &priv->port;
1156 	struct ocelot *ocelot = ocelot_port->ocelot;
1157 	int port = priv->port.index;
1158 
1159 	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1160 }
1161 
1162 static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
1163 			       const struct switchdev_obj *obj,
1164 			       struct netlink_ext_ack *extack)
1165 {
1166 	struct ocelot_port_private *priv = netdev_priv(dev);
1167 	int ret = 0;
1168 
1169 	if (ctx && ctx != priv)
1170 		return 0;
1171 
1172 	switch (obj->id) {
1173 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1174 		ret = ocelot_port_obj_add_vlan(dev,
1175 					       SWITCHDEV_OBJ_PORT_VLAN(obj),
1176 					       extack);
1177 		break;
1178 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1179 		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1180 		break;
1181 	case SWITCHDEV_OBJ_ID_MRP:
1182 		ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1183 		break;
1184 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1185 		ret = ocelot_port_obj_mrp_add_ring_role(dev,
1186 							SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1187 		break;
1188 	default:
1189 		return -EOPNOTSUPP;
1190 	}
1191 
1192 	return ret;
1193 }
1194 
1195 static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
1196 			       const struct switchdev_obj *obj)
1197 {
1198 	struct ocelot_port_private *priv = netdev_priv(dev);
1199 	int ret = 0;
1200 
1201 	if (ctx && ctx != priv)
1202 		return 0;
1203 
1204 	switch (obj->id) {
1205 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1206 		ret = ocelot_vlan_vid_del(dev,
1207 					  SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
1208 		break;
1209 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1210 		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1211 		break;
1212 	case SWITCHDEV_OBJ_ID_MRP:
1213 		ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1214 		break;
1215 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1216 		ret = ocelot_port_obj_mrp_del_ring_role(dev,
1217 							SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1218 		break;
1219 	default:
1220 		return -EOPNOTSUPP;
1221 	}
1222 
1223 	return ret;
1224 }
1225 
1226 static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1227 					struct net_device *brport_dev)
1228 {
1229 	struct switchdev_brport_flags flags = {0};
1230 	int flag;
1231 
1232 	flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1233 
1234 	for_each_set_bit(flag, &flags.mask, 32)
1235 		if (br_port_flag_is_set(brport_dev, BIT(flag)))
1236 			flags.val |= BIT(flag);
1237 
1238 	ocelot_port_bridge_flags(ocelot, port, flags);
1239 }
1240 
1241 static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1242 {
1243 	struct switchdev_brport_flags flags;
1244 
1245 	flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1246 	flags.val = flags.mask & ~BR_LEARNING;
1247 
1248 	ocelot_port_bridge_flags(ocelot, port, flags);
1249 }
1250 
1251 static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1252 				 struct net_device *brport_dev,
1253 				 struct net_device *bridge_dev,
1254 				 struct netlink_ext_ack *extack)
1255 {
1256 	clock_t ageing_time;
1257 	u8 stp_state;
1258 
1259 	ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1260 
1261 	stp_state = br_port_get_stp_state(brport_dev);
1262 	ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1263 
1264 	ageing_time = br_get_ageing_time(bridge_dev);
1265 	ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1266 
1267 	return ocelot_port_vlan_filtering(ocelot, port,
1268 					  br_vlan_enabled(bridge_dev),
1269 					  extack);
1270 }
1271 
1272 static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1273 {
1274 	int err;
1275 
1276 	err = ocelot_port_vlan_filtering(ocelot, port, false, NULL);
1277 	if (err)
1278 		return err;
1279 
1280 	ocelot_clear_brport_flags(ocelot, port);
1281 
1282 	ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1283 
1284 	return 0;
1285 }
1286 
1287 static int ocelot_bridge_num_get(struct ocelot *ocelot,
1288 				 const struct net_device *bridge_dev)
1289 {
1290 	int bridge_num = ocelot_bridge_num_find(ocelot, bridge_dev);
1291 
1292 	if (bridge_num < 0) {
1293 		/* First port that offloads this bridge */
1294 		bridge_num = find_first_zero_bit(&ocelot->bridges,
1295 						 ocelot->num_phys_ports);
1296 
1297 		set_bit(bridge_num, &ocelot->bridges);
1298 	}
1299 
1300 	return bridge_num;
1301 }
1302 
1303 static void ocelot_bridge_num_put(struct ocelot *ocelot,
1304 				  const struct net_device *bridge_dev,
1305 				  int bridge_num)
1306 {
1307 	/* Check if the bridge is still in use, otherwise it is time
1308 	 * to clean it up so we can reuse this bridge_num later.
1309 	 */
1310 	if (!ocelot_bridge_num_find(ocelot, bridge_dev))
1311 		clear_bit(bridge_num, &ocelot->bridges);
1312 }
1313 
1314 static int ocelot_netdevice_bridge_join(struct net_device *dev,
1315 					struct net_device *brport_dev,
1316 					struct net_device *bridge,
1317 					struct netlink_ext_ack *extack)
1318 {
1319 	struct ocelot_port_private *priv = netdev_priv(dev);
1320 	struct ocelot_port *ocelot_port = &priv->port;
1321 	struct ocelot *ocelot = ocelot_port->ocelot;
1322 	int port = priv->port.index;
1323 	int bridge_num, err;
1324 
1325 	bridge_num = ocelot_bridge_num_get(ocelot, bridge);
1326 
1327 	err = ocelot_port_bridge_join(ocelot, port, bridge, bridge_num,
1328 				      extack);
1329 	if (err)
1330 		goto err_join;
1331 
1332 	err = switchdev_bridge_port_offload(brport_dev, dev, priv,
1333 					    &ocelot_switchdev_nb,
1334 					    &ocelot_switchdev_blocking_nb,
1335 					    false, extack);
1336 	if (err)
1337 		goto err_switchdev_offload;
1338 
1339 	err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
1340 	if (err)
1341 		goto err_switchdev_sync;
1342 
1343 	return 0;
1344 
1345 err_switchdev_sync:
1346 	switchdev_bridge_port_unoffload(brport_dev, priv,
1347 					&ocelot_switchdev_nb,
1348 					&ocelot_switchdev_blocking_nb);
1349 err_switchdev_offload:
1350 	ocelot_port_bridge_leave(ocelot, port, bridge);
1351 err_join:
1352 	ocelot_bridge_num_put(ocelot, bridge, bridge_num);
1353 	return err;
1354 }
1355 
1356 static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev,
1357 					      struct net_device *brport_dev)
1358 {
1359 	struct ocelot_port_private *priv = netdev_priv(dev);
1360 
1361 	switchdev_bridge_port_unoffload(brport_dev, priv,
1362 					&ocelot_switchdev_nb,
1363 					&ocelot_switchdev_blocking_nb);
1364 }
1365 
1366 static int ocelot_netdevice_bridge_leave(struct net_device *dev,
1367 					 struct net_device *brport_dev,
1368 					 struct net_device *bridge)
1369 {
1370 	struct ocelot_port_private *priv = netdev_priv(dev);
1371 	struct ocelot_port *ocelot_port = &priv->port;
1372 	struct ocelot *ocelot = ocelot_port->ocelot;
1373 	int bridge_num = ocelot_port->bridge_num;
1374 	int port = priv->port.index;
1375 	int err;
1376 
1377 	err = ocelot_switchdev_unsync(ocelot, port);
1378 	if (err)
1379 		return err;
1380 
1381 	ocelot_port_bridge_leave(ocelot, port, bridge);
1382 	ocelot_bridge_num_put(ocelot, bridge, bridge_num);
1383 
1384 	return 0;
1385 }
1386 
1387 static int ocelot_netdevice_lag_join(struct net_device *dev,
1388 				     struct net_device *bond,
1389 				     struct netdev_lag_upper_info *info,
1390 				     struct netlink_ext_ack *extack)
1391 {
1392 	struct ocelot_port_private *priv = netdev_priv(dev);
1393 	struct ocelot_port *ocelot_port = &priv->port;
1394 	struct ocelot *ocelot = ocelot_port->ocelot;
1395 	struct net_device *bridge_dev;
1396 	int port = priv->port.index;
1397 	int err;
1398 
1399 	err = ocelot_port_lag_join(ocelot, port, bond, info);
1400 	if (err == -EOPNOTSUPP) {
1401 		NL_SET_ERR_MSG_MOD(extack, "Offloading not supported");
1402 		return 0;
1403 	}
1404 
1405 	bridge_dev = netdev_master_upper_dev_get(bond);
1406 	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1407 		return 0;
1408 
1409 	err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
1410 	if (err)
1411 		goto err_bridge_join;
1412 
1413 	return 0;
1414 
1415 err_bridge_join:
1416 	ocelot_port_lag_leave(ocelot, port, bond);
1417 	return err;
1418 }
1419 
1420 static void ocelot_netdevice_pre_lag_leave(struct net_device *dev,
1421 					   struct net_device *bond)
1422 {
1423 	struct net_device *bridge_dev;
1424 
1425 	bridge_dev = netdev_master_upper_dev_get(bond);
1426 	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1427 		return;
1428 
1429 	ocelot_netdevice_pre_bridge_leave(dev, bond);
1430 }
1431 
1432 static int ocelot_netdevice_lag_leave(struct net_device *dev,
1433 				      struct net_device *bond)
1434 {
1435 	struct ocelot_port_private *priv = netdev_priv(dev);
1436 	struct ocelot_port *ocelot_port = &priv->port;
1437 	struct ocelot *ocelot = ocelot_port->ocelot;
1438 	struct net_device *bridge_dev;
1439 	int port = priv->port.index;
1440 
1441 	ocelot_port_lag_leave(ocelot, port, bond);
1442 
1443 	bridge_dev = netdev_master_upper_dev_get(bond);
1444 	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1445 		return 0;
1446 
1447 	return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
1448 }
1449 
1450 static int ocelot_netdevice_changeupper(struct net_device *dev,
1451 					struct net_device *brport_dev,
1452 					struct netdev_notifier_changeupper_info *info)
1453 {
1454 	struct netlink_ext_ack *extack;
1455 	int err = 0;
1456 
1457 	extack = netdev_notifier_info_to_extack(&info->info);
1458 
1459 	if (netif_is_bridge_master(info->upper_dev)) {
1460 		if (info->linking)
1461 			err = ocelot_netdevice_bridge_join(dev, brport_dev,
1462 							   info->upper_dev,
1463 							   extack);
1464 		else
1465 			err = ocelot_netdevice_bridge_leave(dev, brport_dev,
1466 							    info->upper_dev);
1467 	}
1468 	if (netif_is_lag_master(info->upper_dev)) {
1469 		if (info->linking)
1470 			err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1471 							info->upper_info, extack);
1472 		else
1473 			ocelot_netdevice_lag_leave(dev, info->upper_dev);
1474 	}
1475 
1476 	return notifier_from_errno(err);
1477 }
1478 
1479 /* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1480  * events for the lower physical ports of the LAG.
1481  * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1482  * In case the LAG joined a bridge, notify that we are offloading it and can do
1483  * forwarding in hardware towards it.
1484  */
1485 static int
1486 ocelot_netdevice_lag_changeupper(struct net_device *dev,
1487 				 struct netdev_notifier_changeupper_info *info)
1488 {
1489 	struct net_device *lower;
1490 	struct list_head *iter;
1491 	int err = NOTIFY_DONE;
1492 
1493 	netdev_for_each_lower_dev(dev, lower, iter) {
1494 		struct ocelot_port_private *priv = netdev_priv(lower);
1495 		struct ocelot_port *ocelot_port = &priv->port;
1496 
1497 		if (ocelot_port->bond != dev)
1498 			return NOTIFY_OK;
1499 
1500 		err = ocelot_netdevice_changeupper(lower, dev, info);
1501 		if (err)
1502 			return notifier_from_errno(err);
1503 	}
1504 
1505 	return NOTIFY_DONE;
1506 }
1507 
1508 static int
1509 ocelot_netdevice_prechangeupper(struct net_device *dev,
1510 				struct net_device *brport_dev,
1511 				struct netdev_notifier_changeupper_info *info)
1512 {
1513 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
1514 		ocelot_netdevice_pre_bridge_leave(dev, brport_dev);
1515 
1516 	if (netif_is_lag_master(info->upper_dev) && !info->linking)
1517 		ocelot_netdevice_pre_lag_leave(dev, info->upper_dev);
1518 
1519 	return NOTIFY_DONE;
1520 }
1521 
1522 static int
1523 ocelot_netdevice_lag_prechangeupper(struct net_device *dev,
1524 				    struct netdev_notifier_changeupper_info *info)
1525 {
1526 	struct net_device *lower;
1527 	struct list_head *iter;
1528 	int err = NOTIFY_DONE;
1529 
1530 	netdev_for_each_lower_dev(dev, lower, iter) {
1531 		struct ocelot_port_private *priv = netdev_priv(lower);
1532 		struct ocelot_port *ocelot_port = &priv->port;
1533 
1534 		if (ocelot_port->bond != dev)
1535 			return NOTIFY_OK;
1536 
1537 		err = ocelot_netdevice_prechangeupper(dev, lower, info);
1538 		if (err)
1539 			return err;
1540 	}
1541 
1542 	return NOTIFY_DONE;
1543 }
1544 
1545 static int
1546 ocelot_netdevice_changelowerstate(struct net_device *dev,
1547 				  struct netdev_lag_lower_state_info *info)
1548 {
1549 	struct ocelot_port_private *priv = netdev_priv(dev);
1550 	bool is_active = info->link_up && info->tx_enabled;
1551 	struct ocelot_port *ocelot_port = &priv->port;
1552 	struct ocelot *ocelot = ocelot_port->ocelot;
1553 	int port = priv->port.index;
1554 
1555 	if (!ocelot_port->bond)
1556 		return NOTIFY_DONE;
1557 
1558 	if (ocelot_port->lag_tx_active == is_active)
1559 		return NOTIFY_DONE;
1560 
1561 	ocelot_port_lag_change(ocelot, port, is_active);
1562 
1563 	return NOTIFY_OK;
1564 }
1565 
1566 static int ocelot_netdevice_event(struct notifier_block *unused,
1567 				  unsigned long event, void *ptr)
1568 {
1569 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1570 
1571 	switch (event) {
1572 	case NETDEV_PRECHANGEUPPER: {
1573 		struct netdev_notifier_changeupper_info *info = ptr;
1574 
1575 		if (ocelot_netdevice_dev_check(dev))
1576 			return ocelot_netdevice_prechangeupper(dev, dev, info);
1577 
1578 		if (netif_is_lag_master(dev))
1579 			return ocelot_netdevice_lag_prechangeupper(dev, info);
1580 
1581 		break;
1582 	}
1583 	case NETDEV_CHANGEUPPER: {
1584 		struct netdev_notifier_changeupper_info *info = ptr;
1585 
1586 		if (ocelot_netdevice_dev_check(dev))
1587 			return ocelot_netdevice_changeupper(dev, dev, info);
1588 
1589 		if (netif_is_lag_master(dev))
1590 			return ocelot_netdevice_lag_changeupper(dev, info);
1591 
1592 		break;
1593 	}
1594 	case NETDEV_CHANGELOWERSTATE: {
1595 		struct netdev_notifier_changelowerstate_info *info = ptr;
1596 
1597 		if (!ocelot_netdevice_dev_check(dev))
1598 			break;
1599 
1600 		return ocelot_netdevice_changelowerstate(dev,
1601 							 info->lower_state_info);
1602 	}
1603 	default:
1604 		break;
1605 	}
1606 
1607 	return NOTIFY_DONE;
1608 }
1609 
1610 struct notifier_block ocelot_netdevice_nb __read_mostly = {
1611 	.notifier_call = ocelot_netdevice_event,
1612 };
1613 
1614 static int ocelot_switchdev_event(struct notifier_block *unused,
1615 				  unsigned long event, void *ptr)
1616 {
1617 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1618 	int err;
1619 
1620 	switch (event) {
1621 	case SWITCHDEV_PORT_ATTR_SET:
1622 		err = switchdev_handle_port_attr_set(dev, ptr,
1623 						     ocelot_netdevice_dev_check,
1624 						     ocelot_port_attr_set);
1625 		return notifier_from_errno(err);
1626 	}
1627 
1628 	return NOTIFY_DONE;
1629 }
1630 
1631 struct notifier_block ocelot_switchdev_nb __read_mostly = {
1632 	.notifier_call = ocelot_switchdev_event,
1633 };
1634 
1635 static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1636 					   unsigned long event, void *ptr)
1637 {
1638 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1639 	int err;
1640 
1641 	switch (event) {
1642 		/* Blocking events. */
1643 	case SWITCHDEV_PORT_OBJ_ADD:
1644 		err = switchdev_handle_port_obj_add(dev, ptr,
1645 						    ocelot_netdevice_dev_check,
1646 						    ocelot_port_obj_add);
1647 		return notifier_from_errno(err);
1648 	case SWITCHDEV_PORT_OBJ_DEL:
1649 		err = switchdev_handle_port_obj_del(dev, ptr,
1650 						    ocelot_netdevice_dev_check,
1651 						    ocelot_port_obj_del);
1652 		return notifier_from_errno(err);
1653 	case SWITCHDEV_PORT_ATTR_SET:
1654 		err = switchdev_handle_port_attr_set(dev, ptr,
1655 						     ocelot_netdevice_dev_check,
1656 						     ocelot_port_attr_set);
1657 		return notifier_from_errno(err);
1658 	}
1659 
1660 	return NOTIFY_DONE;
1661 }
1662 
1663 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1664 	.notifier_call = ocelot_switchdev_blocking_event,
1665 };
1666 
1667 static void vsc7514_phylink_mac_config(struct phylink_config *config,
1668 				       unsigned int link_an_mode,
1669 				       const struct phylink_link_state *state)
1670 {
1671 	struct net_device *ndev = to_net_dev(config->dev);
1672 	struct ocelot_port_private *priv = netdev_priv(ndev);
1673 	struct ocelot_port *ocelot_port = &priv->port;
1674 
1675 	/* Disable HDX fast control */
1676 	ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
1677 			   DEV_PORT_MISC);
1678 
1679 	/* SGMII only for now */
1680 	ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
1681 			   PCS1G_MODE_CFG);
1682 	ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
1683 
1684 	/* Enable PCS */
1685 	ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
1686 
1687 	/* No aneg on SGMII */
1688 	ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
1689 
1690 	/* No loopback */
1691 	ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
1692 }
1693 
1694 static void vsc7514_phylink_mac_link_down(struct phylink_config *config,
1695 					  unsigned int link_an_mode,
1696 					  phy_interface_t interface)
1697 {
1698 	struct net_device *ndev = to_net_dev(config->dev);
1699 	struct ocelot_port_private *priv = netdev_priv(ndev);
1700 	struct ocelot *ocelot = priv->port.ocelot;
1701 	int port = priv->port.index;
1702 
1703 	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1704 				     OCELOT_MAC_QUIRKS);
1705 }
1706 
1707 static void vsc7514_phylink_mac_link_up(struct phylink_config *config,
1708 					struct phy_device *phydev,
1709 					unsigned int link_an_mode,
1710 					phy_interface_t interface,
1711 					int speed, int duplex,
1712 					bool tx_pause, bool rx_pause)
1713 {
1714 	struct net_device *ndev = to_net_dev(config->dev);
1715 	struct ocelot_port_private *priv = netdev_priv(ndev);
1716 	struct ocelot *ocelot = priv->port.ocelot;
1717 	int port = priv->port.index;
1718 
1719 	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1720 				   interface, speed, duplex,
1721 				   tx_pause, rx_pause, OCELOT_MAC_QUIRKS);
1722 }
1723 
1724 static const struct phylink_mac_ops ocelot_phylink_ops = {
1725 	.validate		= phylink_generic_validate,
1726 	.mac_config		= vsc7514_phylink_mac_config,
1727 	.mac_link_down		= vsc7514_phylink_mac_link_down,
1728 	.mac_link_up		= vsc7514_phylink_mac_link_up,
1729 };
1730 
1731 static int ocelot_port_phylink_create(struct ocelot *ocelot, int port,
1732 				      struct device_node *portnp)
1733 {
1734 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1735 	struct ocelot_port_private *priv;
1736 	struct device *dev = ocelot->dev;
1737 	phy_interface_t phy_mode;
1738 	struct phylink *phylink;
1739 	int err;
1740 
1741 	of_get_phy_mode(portnp, &phy_mode);
1742 	/* DT bindings of internal PHY ports are broken and don't
1743 	 * specify a phy-mode
1744 	 */
1745 	if (phy_mode == PHY_INTERFACE_MODE_NA)
1746 		phy_mode = PHY_INTERFACE_MODE_INTERNAL;
1747 
1748 	if (phy_mode != PHY_INTERFACE_MODE_SGMII &&
1749 	    phy_mode != PHY_INTERFACE_MODE_QSGMII &&
1750 	    phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1751 		dev_err(dev, "unsupported phy mode %s for port %d\n",
1752 			phy_modes(phy_mode), port);
1753 		return -EINVAL;
1754 	}
1755 
1756 	/* Ensure clock signals and speed are set on all QSGMII links */
1757 	if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
1758 		ocelot_port_rmwl(ocelot_port, 0,
1759 				 DEV_CLOCK_CFG_MAC_TX_RST |
1760 				 DEV_CLOCK_CFG_MAC_RX_RST,
1761 				 DEV_CLOCK_CFG);
1762 
1763 	ocelot_port->phy_mode = phy_mode;
1764 
1765 	if (phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1766 		struct phy *serdes = of_phy_get(portnp, NULL);
1767 
1768 		if (IS_ERR(serdes)) {
1769 			err = PTR_ERR(serdes);
1770 			dev_err_probe(dev, err,
1771 				      "missing SerDes phys for port %d\n",
1772 				      port);
1773 			return err;
1774 		}
1775 
1776 		err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, phy_mode);
1777 		of_phy_put(serdes);
1778 		if (err) {
1779 			dev_err(dev, "Could not SerDes mode on port %d: %pe\n",
1780 				port, ERR_PTR(err));
1781 			return err;
1782 		}
1783 	}
1784 
1785 	priv = container_of(ocelot_port, struct ocelot_port_private, port);
1786 
1787 	priv->phylink_config.dev = &priv->dev->dev;
1788 	priv->phylink_config.type = PHYLINK_NETDEV;
1789 	priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1790 		MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
1791 
1792 	__set_bit(ocelot_port->phy_mode,
1793 		  priv->phylink_config.supported_interfaces);
1794 
1795 	phylink = phylink_create(&priv->phylink_config,
1796 				 of_fwnode_handle(portnp),
1797 				 phy_mode, &ocelot_phylink_ops);
1798 	if (IS_ERR(phylink)) {
1799 		err = PTR_ERR(phylink);
1800 		dev_err(dev, "Could not create phylink (%pe)\n", phylink);
1801 		return err;
1802 	}
1803 
1804 	priv->phylink = phylink;
1805 
1806 	err = phylink_of_phy_connect(phylink, portnp, 0);
1807 	if (err) {
1808 		dev_err(dev, "Could not connect to PHY: %pe\n", ERR_PTR(err));
1809 		phylink_destroy(phylink);
1810 		priv->phylink = NULL;
1811 		return err;
1812 	}
1813 
1814 	return 0;
1815 }
1816 
1817 int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
1818 		      struct device_node *portnp)
1819 {
1820 	struct ocelot_port_private *priv;
1821 	struct ocelot_port *ocelot_port;
1822 	struct net_device *dev;
1823 	int err;
1824 
1825 	dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1826 	if (!dev)
1827 		return -ENOMEM;
1828 	SET_NETDEV_DEV(dev, ocelot->dev);
1829 	priv = netdev_priv(dev);
1830 	priv->dev = dev;
1831 	ocelot_port = &priv->port;
1832 	ocelot_port->ocelot = ocelot;
1833 	ocelot_port->index = port;
1834 	ocelot_port->target = target;
1835 	ocelot->ports[port] = ocelot_port;
1836 
1837 	dev->netdev_ops = &ocelot_port_netdev_ops;
1838 	dev->ethtool_ops = &ocelot_ethtool_ops;
1839 	dev->max_mtu = OCELOT_JUMBO_MTU;
1840 
1841 	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1842 		NETIF_F_HW_TC;
1843 	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1844 
1845 	err = of_get_ethdev_address(portnp, dev);
1846 	if (err)
1847 		eth_hw_addr_gen(dev, ocelot->base_mac, port);
1848 
1849 	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1850 			  OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
1851 
1852 	ocelot_init_port(ocelot, port);
1853 
1854 	err = ocelot_port_phylink_create(ocelot, port, portnp);
1855 	if (err)
1856 		goto out;
1857 
1858 	if (ocelot->fdma)
1859 		ocelot_fdma_netdev_init(ocelot, dev);
1860 
1861 	err = register_netdev(dev);
1862 	if (err) {
1863 		dev_err(ocelot->dev, "register_netdev failed\n");
1864 		goto out_fdma_deinit;
1865 	}
1866 
1867 	return 0;
1868 
1869 out_fdma_deinit:
1870 	if (ocelot->fdma)
1871 		ocelot_fdma_netdev_deinit(ocelot, dev);
1872 out:
1873 	ocelot->ports[port] = NULL;
1874 	free_netdev(dev);
1875 
1876 	return err;
1877 }
1878 
1879 void ocelot_release_port(struct ocelot_port *ocelot_port)
1880 {
1881 	struct ocelot_port_private *priv = container_of(ocelot_port,
1882 						struct ocelot_port_private,
1883 						port);
1884 	struct ocelot *ocelot = ocelot_port->ocelot;
1885 	struct ocelot_fdma *fdma = ocelot->fdma;
1886 
1887 	unregister_netdev(priv->dev);
1888 
1889 	if (fdma)
1890 		ocelot_fdma_netdev_deinit(ocelot, priv->dev);
1891 
1892 	if (priv->phylink) {
1893 		rtnl_lock();
1894 		phylink_disconnect_phy(priv->phylink);
1895 		rtnl_unlock();
1896 
1897 		phylink_destroy(priv->phylink);
1898 	}
1899 
1900 	free_netdev(priv->dev);
1901 }
1902