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