1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DPAA2 Ethernet Switch driver
4  *
5  * Copyright 2014-2016 Freescale Semiconductor Inc.
6  * Copyright 2017-2021 NXP
7  *
8  */
9 
10 #include <linux/module.h>
11 
12 #include <linux/interrupt.h>
13 #include <linux/msi.h>
14 #include <linux/kthread.h>
15 #include <linux/workqueue.h>
16 #include <linux/iommu.h>
17 #include <net/pkt_cls.h>
18 
19 #include <linux/fsl/mc.h>
20 
21 #include "dpaa2-switch.h"
22 
23 /* Minimal supported DPSW version */
24 #define DPSW_MIN_VER_MAJOR		8
25 #define DPSW_MIN_VER_MINOR		9
26 
27 #define DEFAULT_VLAN_ID			1
28 
29 static u16 dpaa2_switch_port_get_fdb_id(struct ethsw_port_priv *port_priv)
30 {
31 	return port_priv->fdb->fdb_id;
32 }
33 
34 static struct dpaa2_switch_fdb *dpaa2_switch_fdb_get_unused(struct ethsw_core *ethsw)
35 {
36 	int i;
37 
38 	for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
39 		if (!ethsw->fdbs[i].in_use)
40 			return &ethsw->fdbs[i];
41 	return NULL;
42 }
43 
44 static struct dpaa2_switch_filter_block *
45 dpaa2_switch_filter_block_get_unused(struct ethsw_core *ethsw)
46 {
47 	int i;
48 
49 	for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
50 		if (!ethsw->filter_blocks[i].in_use)
51 			return &ethsw->filter_blocks[i];
52 	return NULL;
53 }
54 
55 static u16 dpaa2_switch_port_set_fdb(struct ethsw_port_priv *port_priv,
56 				     struct net_device *bridge_dev)
57 {
58 	struct ethsw_port_priv *other_port_priv = NULL;
59 	struct dpaa2_switch_fdb *fdb;
60 	struct net_device *other_dev;
61 	struct list_head *iter;
62 
63 	/* If we leave a bridge (bridge_dev is NULL), find an unused
64 	 * FDB and use that.
65 	 */
66 	if (!bridge_dev) {
67 		fdb = dpaa2_switch_fdb_get_unused(port_priv->ethsw_data);
68 
69 		/* If there is no unused FDB, we must be the last port that
70 		 * leaves the last bridge, all the others are standalone. We
71 		 * can just keep the FDB that we already have.
72 		 */
73 
74 		if (!fdb) {
75 			port_priv->fdb->bridge_dev = NULL;
76 			return 0;
77 		}
78 
79 		port_priv->fdb = fdb;
80 		port_priv->fdb->in_use = true;
81 		port_priv->fdb->bridge_dev = NULL;
82 		return 0;
83 	}
84 
85 	/* The below call to netdev_for_each_lower_dev() demands the RTNL lock
86 	 * being held. Assert on it so that it's easier to catch new code
87 	 * paths that reach this point without the RTNL lock.
88 	 */
89 	ASSERT_RTNL();
90 
91 	/* If part of a bridge, use the FDB of the first dpaa2 switch interface
92 	 * to be present in that bridge
93 	 */
94 	netdev_for_each_lower_dev(bridge_dev, other_dev, iter) {
95 		if (!dpaa2_switch_port_dev_check(other_dev))
96 			continue;
97 
98 		if (other_dev == port_priv->netdev)
99 			continue;
100 
101 		other_port_priv = netdev_priv(other_dev);
102 		break;
103 	}
104 
105 	/* The current port is about to change its FDB to the one used by the
106 	 * first port that joined the bridge.
107 	 */
108 	if (other_port_priv) {
109 		/* The previous FDB is about to become unused, since the
110 		 * interface is no longer standalone.
111 		 */
112 		port_priv->fdb->in_use = false;
113 		port_priv->fdb->bridge_dev = NULL;
114 
115 		/* Get a reference to the new FDB */
116 		port_priv->fdb = other_port_priv->fdb;
117 	}
118 
119 	/* Keep track of the new upper bridge device */
120 	port_priv->fdb->bridge_dev = bridge_dev;
121 
122 	return 0;
123 }
124 
125 static void dpaa2_switch_fdb_get_flood_cfg(struct ethsw_core *ethsw, u16 fdb_id,
126 					   enum dpsw_flood_type type,
127 					   struct dpsw_egress_flood_cfg *cfg)
128 {
129 	int i = 0, j;
130 
131 	memset(cfg, 0, sizeof(*cfg));
132 
133 	/* Add all the DPAA2 switch ports found in the same bridging domain to
134 	 * the egress flooding domain
135 	 */
136 	for (j = 0; j < ethsw->sw_attr.num_ifs; j++) {
137 		if (!ethsw->ports[j])
138 			continue;
139 		if (ethsw->ports[j]->fdb->fdb_id != fdb_id)
140 			continue;
141 
142 		if (type == DPSW_BROADCAST && ethsw->ports[j]->bcast_flood)
143 			cfg->if_id[i++] = ethsw->ports[j]->idx;
144 		else if (type == DPSW_FLOODING && ethsw->ports[j]->ucast_flood)
145 			cfg->if_id[i++] = ethsw->ports[j]->idx;
146 	}
147 
148 	/* Add the CTRL interface to the egress flooding domain */
149 	cfg->if_id[i++] = ethsw->sw_attr.num_ifs;
150 
151 	cfg->fdb_id = fdb_id;
152 	cfg->flood_type = type;
153 	cfg->num_ifs = i;
154 }
155 
156 static int dpaa2_switch_fdb_set_egress_flood(struct ethsw_core *ethsw, u16 fdb_id)
157 {
158 	struct dpsw_egress_flood_cfg flood_cfg;
159 	int err;
160 
161 	/* Setup broadcast flooding domain */
162 	dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_BROADCAST, &flood_cfg);
163 	err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle,
164 				    &flood_cfg);
165 	if (err) {
166 		dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err);
167 		return err;
168 	}
169 
170 	/* Setup unknown flooding domain */
171 	dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_FLOODING, &flood_cfg);
172 	err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle,
173 				    &flood_cfg);
174 	if (err) {
175 		dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err);
176 		return err;
177 	}
178 
179 	return 0;
180 }
181 
182 static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
183 				dma_addr_t iova_addr)
184 {
185 	phys_addr_t phys_addr;
186 
187 	phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
188 
189 	return phys_to_virt(phys_addr);
190 }
191 
192 static int dpaa2_switch_add_vlan(struct ethsw_port_priv *port_priv, u16 vid)
193 {
194 	struct ethsw_core *ethsw = port_priv->ethsw_data;
195 	struct dpsw_vlan_cfg vcfg = {0};
196 	int err;
197 
198 	vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
199 	err = dpsw_vlan_add(ethsw->mc_io, 0,
200 			    ethsw->dpsw_handle, vid, &vcfg);
201 	if (err) {
202 		dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
203 		return err;
204 	}
205 	ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
206 
207 	return 0;
208 }
209 
210 static bool dpaa2_switch_port_is_up(struct ethsw_port_priv *port_priv)
211 {
212 	struct net_device *netdev = port_priv->netdev;
213 	struct dpsw_link_state state;
214 	int err;
215 
216 	err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
217 				     port_priv->ethsw_data->dpsw_handle,
218 				     port_priv->idx, &state);
219 	if (err) {
220 		netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
221 		return true;
222 	}
223 
224 	WARN_ONCE(state.up > 1, "Garbage read into link_state");
225 
226 	return state.up ? true : false;
227 }
228 
229 static int dpaa2_switch_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
230 {
231 	struct ethsw_core *ethsw = port_priv->ethsw_data;
232 	struct net_device *netdev = port_priv->netdev;
233 	struct dpsw_tci_cfg tci_cfg = { 0 };
234 	bool up;
235 	int err, ret;
236 
237 	err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
238 			      port_priv->idx, &tci_cfg);
239 	if (err) {
240 		netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
241 		return err;
242 	}
243 
244 	tci_cfg.vlan_id = pvid;
245 
246 	/* Interface needs to be down to change PVID */
247 	up = dpaa2_switch_port_is_up(port_priv);
248 	if (up) {
249 		err = dpsw_if_disable(ethsw->mc_io, 0,
250 				      ethsw->dpsw_handle,
251 				      port_priv->idx);
252 		if (err) {
253 			netdev_err(netdev, "dpsw_if_disable err %d\n", err);
254 			return err;
255 		}
256 	}
257 
258 	err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
259 			      port_priv->idx, &tci_cfg);
260 	if (err) {
261 		netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
262 		goto set_tci_error;
263 	}
264 
265 	/* Delete previous PVID info and mark the new one */
266 	port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
267 	port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
268 	port_priv->pvid = pvid;
269 
270 set_tci_error:
271 	if (up) {
272 		ret = dpsw_if_enable(ethsw->mc_io, 0,
273 				     ethsw->dpsw_handle,
274 				     port_priv->idx);
275 		if (ret) {
276 			netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
277 			return ret;
278 		}
279 	}
280 
281 	return err;
282 }
283 
284 static int dpaa2_switch_port_add_vlan(struct ethsw_port_priv *port_priv,
285 				      u16 vid, u16 flags)
286 {
287 	struct ethsw_core *ethsw = port_priv->ethsw_data;
288 	struct net_device *netdev = port_priv->netdev;
289 	struct dpsw_vlan_if_cfg vcfg = {0};
290 	int err;
291 
292 	if (port_priv->vlans[vid]) {
293 		netdev_warn(netdev, "VLAN %d already configured\n", vid);
294 		return -EEXIST;
295 	}
296 
297 	/* If hit, this VLAN rule will lead the packet into the FDB table
298 	 * specified in the vlan configuration below
299 	 */
300 	vcfg.num_ifs = 1;
301 	vcfg.if_id[0] = port_priv->idx;
302 	vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
303 	vcfg.options |= DPSW_VLAN_ADD_IF_OPT_FDB_ID;
304 	err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
305 	if (err) {
306 		netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
307 		return err;
308 	}
309 
310 	port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
311 
312 	if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
313 		err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
314 						ethsw->dpsw_handle,
315 						vid, &vcfg);
316 		if (err) {
317 			netdev_err(netdev,
318 				   "dpsw_vlan_add_if_untagged err %d\n", err);
319 			return err;
320 		}
321 		port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
322 	}
323 
324 	if (flags & BRIDGE_VLAN_INFO_PVID) {
325 		err = dpaa2_switch_port_set_pvid(port_priv, vid);
326 		if (err)
327 			return err;
328 	}
329 
330 	return 0;
331 }
332 
333 static enum dpsw_stp_state br_stp_state_to_dpsw(u8 state)
334 {
335 	switch (state) {
336 	case BR_STATE_DISABLED:
337 		return DPSW_STP_STATE_DISABLED;
338 	case BR_STATE_LISTENING:
339 		return DPSW_STP_STATE_LISTENING;
340 	case BR_STATE_LEARNING:
341 		return DPSW_STP_STATE_LEARNING;
342 	case BR_STATE_FORWARDING:
343 		return DPSW_STP_STATE_FORWARDING;
344 	case BR_STATE_BLOCKING:
345 		return DPSW_STP_STATE_BLOCKING;
346 	default:
347 		return DPSW_STP_STATE_DISABLED;
348 	}
349 }
350 
351 static int dpaa2_switch_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
352 {
353 	struct dpsw_stp_cfg stp_cfg = {0};
354 	int err;
355 	u16 vid;
356 
357 	if (!netif_running(port_priv->netdev) || state == port_priv->stp_state)
358 		return 0;	/* Nothing to do */
359 
360 	stp_cfg.state = br_stp_state_to_dpsw(state);
361 	for (vid = 0; vid <= VLAN_VID_MASK; vid++) {
362 		if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
363 			stp_cfg.vlan_id = vid;
364 			err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
365 					      port_priv->ethsw_data->dpsw_handle,
366 					      port_priv->idx, &stp_cfg);
367 			if (err) {
368 				netdev_err(port_priv->netdev,
369 					   "dpsw_if_set_stp err %d\n", err);
370 				return err;
371 			}
372 		}
373 	}
374 
375 	port_priv->stp_state = state;
376 
377 	return 0;
378 }
379 
380 static int dpaa2_switch_dellink(struct ethsw_core *ethsw, u16 vid)
381 {
382 	struct ethsw_port_priv *ppriv_local = NULL;
383 	int i, err;
384 
385 	if (!ethsw->vlans[vid])
386 		return -ENOENT;
387 
388 	err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
389 	if (err) {
390 		dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
391 		return err;
392 	}
393 	ethsw->vlans[vid] = 0;
394 
395 	for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
396 		ppriv_local = ethsw->ports[i];
397 		ppriv_local->vlans[vid] = 0;
398 	}
399 
400 	return 0;
401 }
402 
403 static int dpaa2_switch_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
404 					const unsigned char *addr)
405 {
406 	struct dpsw_fdb_unicast_cfg entry = {0};
407 	u16 fdb_id;
408 	int err;
409 
410 	entry.if_egress = port_priv->idx;
411 	entry.type = DPSW_FDB_ENTRY_STATIC;
412 	ether_addr_copy(entry.mac_addr, addr);
413 
414 	fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
415 	err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
416 				   port_priv->ethsw_data->dpsw_handle,
417 				   fdb_id, &entry);
418 	if (err)
419 		netdev_err(port_priv->netdev,
420 			   "dpsw_fdb_add_unicast err %d\n", err);
421 	return err;
422 }
423 
424 static int dpaa2_switch_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
425 					const unsigned char *addr)
426 {
427 	struct dpsw_fdb_unicast_cfg entry = {0};
428 	u16 fdb_id;
429 	int err;
430 
431 	entry.if_egress = port_priv->idx;
432 	entry.type = DPSW_FDB_ENTRY_STATIC;
433 	ether_addr_copy(entry.mac_addr, addr);
434 
435 	fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
436 	err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
437 				      port_priv->ethsw_data->dpsw_handle,
438 				      fdb_id, &entry);
439 	/* Silently discard error for calling multiple times the del command */
440 	if (err && err != -ENXIO)
441 		netdev_err(port_priv->netdev,
442 			   "dpsw_fdb_remove_unicast err %d\n", err);
443 	return err;
444 }
445 
446 static int dpaa2_switch_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
447 					const unsigned char *addr)
448 {
449 	struct dpsw_fdb_multicast_cfg entry = {0};
450 	u16 fdb_id;
451 	int err;
452 
453 	ether_addr_copy(entry.mac_addr, addr);
454 	entry.type = DPSW_FDB_ENTRY_STATIC;
455 	entry.num_ifs = 1;
456 	entry.if_id[0] = port_priv->idx;
457 
458 	fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
459 	err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
460 				     port_priv->ethsw_data->dpsw_handle,
461 				     fdb_id, &entry);
462 	/* Silently discard error for calling multiple times the add command */
463 	if (err && err != -ENXIO)
464 		netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
465 			   err);
466 	return err;
467 }
468 
469 static int dpaa2_switch_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
470 					const unsigned char *addr)
471 {
472 	struct dpsw_fdb_multicast_cfg entry = {0};
473 	u16 fdb_id;
474 	int err;
475 
476 	ether_addr_copy(entry.mac_addr, addr);
477 	entry.type = DPSW_FDB_ENTRY_STATIC;
478 	entry.num_ifs = 1;
479 	entry.if_id[0] = port_priv->idx;
480 
481 	fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
482 	err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
483 					port_priv->ethsw_data->dpsw_handle,
484 					fdb_id, &entry);
485 	/* Silently discard error for calling multiple times the del command */
486 	if (err && err != -ENAVAIL)
487 		netdev_err(port_priv->netdev,
488 			   "dpsw_fdb_remove_multicast err %d\n", err);
489 	return err;
490 }
491 
492 static void dpaa2_switch_port_get_stats(struct net_device *netdev,
493 					struct rtnl_link_stats64 *stats)
494 {
495 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
496 	u64 tmp;
497 	int err;
498 
499 	err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
500 				  port_priv->ethsw_data->dpsw_handle,
501 				  port_priv->idx,
502 				  DPSW_CNT_ING_FRAME, &stats->rx_packets);
503 	if (err)
504 		goto error;
505 
506 	err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
507 				  port_priv->ethsw_data->dpsw_handle,
508 				  port_priv->idx,
509 				  DPSW_CNT_EGR_FRAME, &stats->tx_packets);
510 	if (err)
511 		goto error;
512 
513 	err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
514 				  port_priv->ethsw_data->dpsw_handle,
515 				  port_priv->idx,
516 				  DPSW_CNT_ING_BYTE, &stats->rx_bytes);
517 	if (err)
518 		goto error;
519 
520 	err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
521 				  port_priv->ethsw_data->dpsw_handle,
522 				  port_priv->idx,
523 				  DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
524 	if (err)
525 		goto error;
526 
527 	err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
528 				  port_priv->ethsw_data->dpsw_handle,
529 				  port_priv->idx,
530 				  DPSW_CNT_ING_FRAME_DISCARD,
531 				  &stats->rx_dropped);
532 	if (err)
533 		goto error;
534 
535 	err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
536 				  port_priv->ethsw_data->dpsw_handle,
537 				  port_priv->idx,
538 				  DPSW_CNT_ING_FLTR_FRAME,
539 				  &tmp);
540 	if (err)
541 		goto error;
542 	stats->rx_dropped += tmp;
543 
544 	err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
545 				  port_priv->ethsw_data->dpsw_handle,
546 				  port_priv->idx,
547 				  DPSW_CNT_EGR_FRAME_DISCARD,
548 				  &stats->tx_dropped);
549 	if (err)
550 		goto error;
551 
552 	return;
553 
554 error:
555 	netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
556 }
557 
558 static bool dpaa2_switch_port_has_offload_stats(const struct net_device *netdev,
559 						int attr_id)
560 {
561 	return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
562 }
563 
564 static int dpaa2_switch_port_get_offload_stats(int attr_id,
565 					       const struct net_device *netdev,
566 					       void *sp)
567 {
568 	switch (attr_id) {
569 	case IFLA_OFFLOAD_XSTATS_CPU_HIT:
570 		dpaa2_switch_port_get_stats((struct net_device *)netdev, sp);
571 		return 0;
572 	}
573 
574 	return -EINVAL;
575 }
576 
577 static int dpaa2_switch_port_change_mtu(struct net_device *netdev, int mtu)
578 {
579 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
580 	int err;
581 
582 	err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
583 					   0,
584 					   port_priv->ethsw_data->dpsw_handle,
585 					   port_priv->idx,
586 					   (u16)ETHSW_L2_MAX_FRM(mtu));
587 	if (err) {
588 		netdev_err(netdev,
589 			   "dpsw_if_set_max_frame_length() err %d\n", err);
590 		return err;
591 	}
592 
593 	netdev->mtu = mtu;
594 	return 0;
595 }
596 
597 static int dpaa2_switch_port_link_state_update(struct net_device *netdev)
598 {
599 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
600 	struct dpsw_link_state state;
601 	int err;
602 
603 	/* Interrupts are received even though no one issued an 'ifconfig up'
604 	 * on the switch interface. Ignore these link state update interrupts
605 	 */
606 	if (!netif_running(netdev))
607 		return 0;
608 
609 	err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
610 				     port_priv->ethsw_data->dpsw_handle,
611 				     port_priv->idx, &state);
612 	if (err) {
613 		netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
614 		return err;
615 	}
616 
617 	WARN_ONCE(state.up > 1, "Garbage read into link_state");
618 
619 	if (state.up != port_priv->link_state) {
620 		if (state.up) {
621 			netif_carrier_on(netdev);
622 			netif_tx_start_all_queues(netdev);
623 		} else {
624 			netif_carrier_off(netdev);
625 			netif_tx_stop_all_queues(netdev);
626 		}
627 		port_priv->link_state = state.up;
628 	}
629 
630 	return 0;
631 }
632 
633 /* Manage all NAPI instances for the control interface.
634  *
635  * We only have one RX queue and one Tx Conf queue for all
636  * switch ports. Therefore, we only need to enable the NAPI instance once, the
637  * first time one of the switch ports runs .dev_open().
638  */
639 
640 static void dpaa2_switch_enable_ctrl_if_napi(struct ethsw_core *ethsw)
641 {
642 	int i;
643 
644 	/* Access to the ethsw->napi_users relies on the RTNL lock */
645 	ASSERT_RTNL();
646 
647 	/* a new interface is using the NAPI instance */
648 	ethsw->napi_users++;
649 
650 	/* if there is already a user of the instance, return */
651 	if (ethsw->napi_users > 1)
652 		return;
653 
654 	for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
655 		napi_enable(&ethsw->fq[i].napi);
656 }
657 
658 static void dpaa2_switch_disable_ctrl_if_napi(struct ethsw_core *ethsw)
659 {
660 	int i;
661 
662 	/* Access to the ethsw->napi_users relies on the RTNL lock */
663 	ASSERT_RTNL();
664 
665 	/* If we are not the last interface using the NAPI, return */
666 	ethsw->napi_users--;
667 	if (ethsw->napi_users)
668 		return;
669 
670 	for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
671 		napi_disable(&ethsw->fq[i].napi);
672 }
673 
674 static int dpaa2_switch_port_open(struct net_device *netdev)
675 {
676 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
677 	struct ethsw_core *ethsw = port_priv->ethsw_data;
678 	int err;
679 
680 	/* Explicitly set carrier off, otherwise
681 	 * netif_carrier_ok() will return true and cause 'ip link show'
682 	 * to report the LOWER_UP flag, even though the link
683 	 * notification wasn't even received.
684 	 */
685 	netif_carrier_off(netdev);
686 
687 	err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
688 			     port_priv->ethsw_data->dpsw_handle,
689 			     port_priv->idx);
690 	if (err) {
691 		netdev_err(netdev, "dpsw_if_enable err %d\n", err);
692 		return err;
693 	}
694 
695 	/* sync carrier state */
696 	err = dpaa2_switch_port_link_state_update(netdev);
697 	if (err) {
698 		netdev_err(netdev,
699 			   "dpaa2_switch_port_link_state_update err %d\n", err);
700 		goto err_carrier_sync;
701 	}
702 
703 	dpaa2_switch_enable_ctrl_if_napi(ethsw);
704 
705 	return 0;
706 
707 err_carrier_sync:
708 	dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
709 			port_priv->ethsw_data->dpsw_handle,
710 			port_priv->idx);
711 	return err;
712 }
713 
714 static int dpaa2_switch_port_stop(struct net_device *netdev)
715 {
716 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
717 	struct ethsw_core *ethsw = port_priv->ethsw_data;
718 	int err;
719 
720 	err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
721 			      port_priv->ethsw_data->dpsw_handle,
722 			      port_priv->idx);
723 	if (err) {
724 		netdev_err(netdev, "dpsw_if_disable err %d\n", err);
725 		return err;
726 	}
727 
728 	dpaa2_switch_disable_ctrl_if_napi(ethsw);
729 
730 	return 0;
731 }
732 
733 static int dpaa2_switch_port_parent_id(struct net_device *dev,
734 				       struct netdev_phys_item_id *ppid)
735 {
736 	struct ethsw_port_priv *port_priv = netdev_priv(dev);
737 
738 	ppid->id_len = 1;
739 	ppid->id[0] = port_priv->ethsw_data->dev_id;
740 
741 	return 0;
742 }
743 
744 static int dpaa2_switch_port_get_phys_name(struct net_device *netdev, char *name,
745 					   size_t len)
746 {
747 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
748 	int err;
749 
750 	err = snprintf(name, len, "p%d", port_priv->idx);
751 	if (err >= len)
752 		return -EINVAL;
753 
754 	return 0;
755 }
756 
757 struct ethsw_dump_ctx {
758 	struct net_device *dev;
759 	struct sk_buff *skb;
760 	struct netlink_callback *cb;
761 	int idx;
762 };
763 
764 static int dpaa2_switch_fdb_dump_nl(struct fdb_dump_entry *entry,
765 				    struct ethsw_dump_ctx *dump)
766 {
767 	int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
768 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
769 	u32 seq = dump->cb->nlh->nlmsg_seq;
770 	struct nlmsghdr *nlh;
771 	struct ndmsg *ndm;
772 
773 	if (dump->idx < dump->cb->args[2])
774 		goto skip;
775 
776 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
777 			sizeof(*ndm), NLM_F_MULTI);
778 	if (!nlh)
779 		return -EMSGSIZE;
780 
781 	ndm = nlmsg_data(nlh);
782 	ndm->ndm_family  = AF_BRIDGE;
783 	ndm->ndm_pad1    = 0;
784 	ndm->ndm_pad2    = 0;
785 	ndm->ndm_flags   = NTF_SELF;
786 	ndm->ndm_type    = 0;
787 	ndm->ndm_ifindex = dump->dev->ifindex;
788 	ndm->ndm_state   = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
789 
790 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
791 		goto nla_put_failure;
792 
793 	nlmsg_end(dump->skb, nlh);
794 
795 skip:
796 	dump->idx++;
797 	return 0;
798 
799 nla_put_failure:
800 	nlmsg_cancel(dump->skb, nlh);
801 	return -EMSGSIZE;
802 }
803 
804 static int dpaa2_switch_port_fdb_valid_entry(struct fdb_dump_entry *entry,
805 					     struct ethsw_port_priv *port_priv)
806 {
807 	int idx = port_priv->idx;
808 	int valid;
809 
810 	if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
811 		valid = entry->if_info == port_priv->idx;
812 	else
813 		valid = entry->if_mask[idx / 8] & BIT(idx % 8);
814 
815 	return valid;
816 }
817 
818 static int dpaa2_switch_fdb_iterate(struct ethsw_port_priv *port_priv,
819 				    dpaa2_switch_fdb_cb_t cb, void *data)
820 {
821 	struct net_device *net_dev = port_priv->netdev;
822 	struct ethsw_core *ethsw = port_priv->ethsw_data;
823 	struct device *dev = net_dev->dev.parent;
824 	struct fdb_dump_entry *fdb_entries;
825 	struct fdb_dump_entry fdb_entry;
826 	dma_addr_t fdb_dump_iova;
827 	u16 num_fdb_entries;
828 	u32 fdb_dump_size;
829 	int err = 0, i;
830 	u8 *dma_mem;
831 	u16 fdb_id;
832 
833 	fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
834 	dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
835 	if (!dma_mem)
836 		return -ENOMEM;
837 
838 	fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
839 				       DMA_FROM_DEVICE);
840 	if (dma_mapping_error(dev, fdb_dump_iova)) {
841 		netdev_err(net_dev, "dma_map_single() failed\n");
842 		err = -ENOMEM;
843 		goto err_map;
844 	}
845 
846 	fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
847 	err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, fdb_id,
848 			    fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
849 	if (err) {
850 		netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
851 		goto err_dump;
852 	}
853 
854 	dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
855 
856 	fdb_entries = (struct fdb_dump_entry *)dma_mem;
857 	for (i = 0; i < num_fdb_entries; i++) {
858 		fdb_entry = fdb_entries[i];
859 
860 		err = cb(port_priv, &fdb_entry, data);
861 		if (err)
862 			goto end;
863 	}
864 
865 end:
866 	kfree(dma_mem);
867 
868 	return 0;
869 
870 err_dump:
871 	dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
872 err_map:
873 	kfree(dma_mem);
874 	return err;
875 }
876 
877 static int dpaa2_switch_fdb_entry_dump(struct ethsw_port_priv *port_priv,
878 				       struct fdb_dump_entry *fdb_entry,
879 				       void *data)
880 {
881 	if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv))
882 		return 0;
883 
884 	return dpaa2_switch_fdb_dump_nl(fdb_entry, data);
885 }
886 
887 static int dpaa2_switch_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
888 				      struct net_device *net_dev,
889 				      struct net_device *filter_dev, int *idx)
890 {
891 	struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
892 	struct ethsw_dump_ctx dump = {
893 		.dev = net_dev,
894 		.skb = skb,
895 		.cb = cb,
896 		.idx = *idx,
897 	};
898 	int err;
899 
900 	err = dpaa2_switch_fdb_iterate(port_priv, dpaa2_switch_fdb_entry_dump, &dump);
901 	*idx = dump.idx;
902 
903 	return err;
904 }
905 
906 static int dpaa2_switch_fdb_entry_fast_age(struct ethsw_port_priv *port_priv,
907 					   struct fdb_dump_entry *fdb_entry,
908 					   void *data __always_unused)
909 {
910 	if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv))
911 		return 0;
912 
913 	if (!(fdb_entry->type & DPSW_FDB_ENTRY_TYPE_DYNAMIC))
914 		return 0;
915 
916 	if (fdb_entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
917 		dpaa2_switch_port_fdb_del_uc(port_priv, fdb_entry->mac_addr);
918 	else
919 		dpaa2_switch_port_fdb_del_mc(port_priv, fdb_entry->mac_addr);
920 
921 	return 0;
922 }
923 
924 static void dpaa2_switch_port_fast_age(struct ethsw_port_priv *port_priv)
925 {
926 	dpaa2_switch_fdb_iterate(port_priv,
927 				 dpaa2_switch_fdb_entry_fast_age, NULL);
928 }
929 
930 static int dpaa2_switch_port_vlan_add(struct net_device *netdev, __be16 proto,
931 				      u16 vid)
932 {
933 	struct switchdev_obj_port_vlan vlan = {
934 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
935 		.vid = vid,
936 		.obj.orig_dev = netdev,
937 		/* This API only allows programming tagged, non-PVID VIDs */
938 		.flags = 0,
939 	};
940 
941 	return dpaa2_switch_port_vlans_add(netdev, &vlan);
942 }
943 
944 static int dpaa2_switch_port_vlan_kill(struct net_device *netdev, __be16 proto,
945 				       u16 vid)
946 {
947 	struct switchdev_obj_port_vlan vlan = {
948 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
949 		.vid = vid,
950 		.obj.orig_dev = netdev,
951 		/* This API only allows programming tagged, non-PVID VIDs */
952 		.flags = 0,
953 	};
954 
955 	return dpaa2_switch_port_vlans_del(netdev, &vlan);
956 }
957 
958 static int dpaa2_switch_port_set_mac_addr(struct ethsw_port_priv *port_priv)
959 {
960 	struct ethsw_core *ethsw = port_priv->ethsw_data;
961 	struct net_device *net_dev = port_priv->netdev;
962 	struct device *dev = net_dev->dev.parent;
963 	u8 mac_addr[ETH_ALEN];
964 	int err;
965 
966 	if (!(ethsw->features & ETHSW_FEATURE_MAC_ADDR))
967 		return 0;
968 
969 	/* Get firmware address, if any */
970 	err = dpsw_if_get_port_mac_addr(ethsw->mc_io, 0, ethsw->dpsw_handle,
971 					port_priv->idx, mac_addr);
972 	if (err) {
973 		dev_err(dev, "dpsw_if_get_port_mac_addr() failed\n");
974 		return err;
975 	}
976 
977 	/* First check if firmware has any address configured by bootloader */
978 	if (!is_zero_ether_addr(mac_addr)) {
979 		memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
980 	} else {
981 		/* No MAC address configured, fill in net_dev->dev_addr
982 		 * with a random one
983 		 */
984 		eth_hw_addr_random(net_dev);
985 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
986 
987 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
988 		 * practical purposes, this will be our "permanent" mac address,
989 		 * at least until the next reboot. This move will also permit
990 		 * register_netdevice() to properly fill up net_dev->perm_addr.
991 		 */
992 		net_dev->addr_assign_type = NET_ADDR_PERM;
993 	}
994 
995 	return 0;
996 }
997 
998 static void dpaa2_switch_free_fd(const struct ethsw_core *ethsw,
999 				 const struct dpaa2_fd *fd)
1000 {
1001 	struct device *dev = ethsw->dev;
1002 	unsigned char *buffer_start;
1003 	struct sk_buff **skbh, *skb;
1004 	dma_addr_t fd_addr;
1005 
1006 	fd_addr = dpaa2_fd_get_addr(fd);
1007 	skbh = dpaa2_iova_to_virt(ethsw->iommu_domain, fd_addr);
1008 
1009 	skb = *skbh;
1010 	buffer_start = (unsigned char *)skbh;
1011 
1012 	dma_unmap_single(dev, fd_addr,
1013 			 skb_tail_pointer(skb) - buffer_start,
1014 			 DMA_TO_DEVICE);
1015 
1016 	/* Move on with skb release */
1017 	dev_kfree_skb(skb);
1018 }
1019 
1020 static int dpaa2_switch_build_single_fd(struct ethsw_core *ethsw,
1021 					struct sk_buff *skb,
1022 					struct dpaa2_fd *fd)
1023 {
1024 	struct device *dev = ethsw->dev;
1025 	struct sk_buff **skbh;
1026 	dma_addr_t addr;
1027 	u8 *buff_start;
1028 	void *hwa;
1029 
1030 	buff_start = PTR_ALIGN(skb->data - DPAA2_SWITCH_TX_DATA_OFFSET -
1031 			       DPAA2_SWITCH_TX_BUF_ALIGN,
1032 			       DPAA2_SWITCH_TX_BUF_ALIGN);
1033 
1034 	/* Clear FAS to have consistent values for TX confirmation. It is
1035 	 * located in the first 8 bytes of the buffer's hardware annotation
1036 	 * area
1037 	 */
1038 	hwa = buff_start + DPAA2_SWITCH_SWA_SIZE;
1039 	memset(hwa, 0, 8);
1040 
1041 	/* Store a backpointer to the skb at the beginning of the buffer
1042 	 * (in the private data area) such that we can release it
1043 	 * on Tx confirm
1044 	 */
1045 	skbh = (struct sk_buff **)buff_start;
1046 	*skbh = skb;
1047 
1048 	addr = dma_map_single(dev, buff_start,
1049 			      skb_tail_pointer(skb) - buff_start,
1050 			      DMA_TO_DEVICE);
1051 	if (unlikely(dma_mapping_error(dev, addr)))
1052 		return -ENOMEM;
1053 
1054 	/* Setup the FD fields */
1055 	memset(fd, 0, sizeof(*fd));
1056 
1057 	dpaa2_fd_set_addr(fd, addr);
1058 	dpaa2_fd_set_offset(fd, (u16)(skb->data - buff_start));
1059 	dpaa2_fd_set_len(fd, skb->len);
1060 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
1061 
1062 	return 0;
1063 }
1064 
1065 static netdev_tx_t dpaa2_switch_port_tx(struct sk_buff *skb,
1066 					struct net_device *net_dev)
1067 {
1068 	struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
1069 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1070 	int retries = DPAA2_SWITCH_SWP_BUSY_RETRIES;
1071 	struct dpaa2_fd fd;
1072 	int err;
1073 
1074 	if (unlikely(skb_headroom(skb) < DPAA2_SWITCH_NEEDED_HEADROOM)) {
1075 		struct sk_buff *ns;
1076 
1077 		ns = skb_realloc_headroom(skb, DPAA2_SWITCH_NEEDED_HEADROOM);
1078 		if (unlikely(!ns)) {
1079 			net_err_ratelimited("%s: Error reallocating skb headroom\n", net_dev->name);
1080 			goto err_free_skb;
1081 		}
1082 		dev_consume_skb_any(skb);
1083 		skb = ns;
1084 	}
1085 
1086 	/* We'll be holding a back-reference to the skb until Tx confirmation */
1087 	skb = skb_unshare(skb, GFP_ATOMIC);
1088 	if (unlikely(!skb)) {
1089 		/* skb_unshare() has already freed the skb */
1090 		net_err_ratelimited("%s: Error copying the socket buffer\n", net_dev->name);
1091 		goto err_exit;
1092 	}
1093 
1094 	/* At this stage, we do not support non-linear skbs so just try to
1095 	 * linearize the skb and if that's not working, just drop the packet.
1096 	 */
1097 	err = skb_linearize(skb);
1098 	if (err) {
1099 		net_err_ratelimited("%s: skb_linearize error (%d)!\n", net_dev->name, err);
1100 		goto err_free_skb;
1101 	}
1102 
1103 	err = dpaa2_switch_build_single_fd(ethsw, skb, &fd);
1104 	if (unlikely(err)) {
1105 		net_err_ratelimited("%s: ethsw_build_*_fd() %d\n", net_dev->name, err);
1106 		goto err_free_skb;
1107 	}
1108 
1109 	do {
1110 		err = dpaa2_io_service_enqueue_qd(NULL,
1111 						  port_priv->tx_qdid,
1112 						  8, 0, &fd);
1113 		retries--;
1114 	} while (err == -EBUSY && retries);
1115 
1116 	if (unlikely(err < 0)) {
1117 		dpaa2_switch_free_fd(ethsw, &fd);
1118 		goto err_exit;
1119 	}
1120 
1121 	return NETDEV_TX_OK;
1122 
1123 err_free_skb:
1124 	dev_kfree_skb(skb);
1125 err_exit:
1126 	return NETDEV_TX_OK;
1127 }
1128 
1129 static int
1130 dpaa2_switch_setup_tc_cls_flower(struct dpaa2_switch_filter_block *filter_block,
1131 				 struct flow_cls_offload *f)
1132 {
1133 	switch (f->command) {
1134 	case FLOW_CLS_REPLACE:
1135 		return dpaa2_switch_cls_flower_replace(filter_block, f);
1136 	case FLOW_CLS_DESTROY:
1137 		return dpaa2_switch_cls_flower_destroy(filter_block, f);
1138 	default:
1139 		return -EOPNOTSUPP;
1140 	}
1141 }
1142 
1143 static int
1144 dpaa2_switch_setup_tc_cls_matchall(struct dpaa2_switch_filter_block *block,
1145 				   struct tc_cls_matchall_offload *f)
1146 {
1147 	switch (f->command) {
1148 	case TC_CLSMATCHALL_REPLACE:
1149 		return dpaa2_switch_cls_matchall_replace(block, f);
1150 	case TC_CLSMATCHALL_DESTROY:
1151 		return dpaa2_switch_cls_matchall_destroy(block, f);
1152 	default:
1153 		return -EOPNOTSUPP;
1154 	}
1155 }
1156 
1157 static int dpaa2_switch_port_setup_tc_block_cb_ig(enum tc_setup_type type,
1158 						  void *type_data,
1159 						  void *cb_priv)
1160 {
1161 	switch (type) {
1162 	case TC_SETUP_CLSFLOWER:
1163 		return dpaa2_switch_setup_tc_cls_flower(cb_priv, type_data);
1164 	case TC_SETUP_CLSMATCHALL:
1165 		return dpaa2_switch_setup_tc_cls_matchall(cb_priv, type_data);
1166 	default:
1167 		return -EOPNOTSUPP;
1168 	}
1169 }
1170 
1171 static LIST_HEAD(dpaa2_switch_block_cb_list);
1172 
1173 static int
1174 dpaa2_switch_port_acl_tbl_bind(struct ethsw_port_priv *port_priv,
1175 			       struct dpaa2_switch_filter_block *block)
1176 {
1177 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1178 	struct net_device *netdev = port_priv->netdev;
1179 	struct dpsw_acl_if_cfg acl_if_cfg;
1180 	int err;
1181 
1182 	if (port_priv->filter_block)
1183 		return -EINVAL;
1184 
1185 	acl_if_cfg.if_id[0] = port_priv->idx;
1186 	acl_if_cfg.num_ifs = 1;
1187 	err = dpsw_acl_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1188 			      block->acl_id, &acl_if_cfg);
1189 	if (err) {
1190 		netdev_err(netdev, "dpsw_acl_add_if err %d\n", err);
1191 		return err;
1192 	}
1193 
1194 	block->ports |= BIT(port_priv->idx);
1195 	port_priv->filter_block = block;
1196 
1197 	return 0;
1198 }
1199 
1200 static int
1201 dpaa2_switch_port_acl_tbl_unbind(struct ethsw_port_priv *port_priv,
1202 				 struct dpaa2_switch_filter_block *block)
1203 {
1204 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1205 	struct net_device *netdev = port_priv->netdev;
1206 	struct dpsw_acl_if_cfg acl_if_cfg;
1207 	int err;
1208 
1209 	if (port_priv->filter_block != block)
1210 		return -EINVAL;
1211 
1212 	acl_if_cfg.if_id[0] = port_priv->idx;
1213 	acl_if_cfg.num_ifs = 1;
1214 	err = dpsw_acl_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1215 				 block->acl_id, &acl_if_cfg);
1216 	if (err) {
1217 		netdev_err(netdev, "dpsw_acl_add_if err %d\n", err);
1218 		return err;
1219 	}
1220 
1221 	block->ports &= ~BIT(port_priv->idx);
1222 	port_priv->filter_block = NULL;
1223 	return 0;
1224 }
1225 
1226 static int dpaa2_switch_port_block_bind(struct ethsw_port_priv *port_priv,
1227 					struct dpaa2_switch_filter_block *block)
1228 {
1229 	struct dpaa2_switch_filter_block *old_block = port_priv->filter_block;
1230 	int err;
1231 
1232 	/* Offload all the mirror entries found in the block on this new port
1233 	 * joining it.
1234 	 */
1235 	err = dpaa2_switch_block_offload_mirror(block, port_priv);
1236 	if (err)
1237 		return err;
1238 
1239 	/* If the port is already bound to this ACL table then do nothing. This
1240 	 * can happen when this port is the first one to join a tc block
1241 	 */
1242 	if (port_priv->filter_block == block)
1243 		return 0;
1244 
1245 	err = dpaa2_switch_port_acl_tbl_unbind(port_priv, old_block);
1246 	if (err)
1247 		return err;
1248 
1249 	/* Mark the previous ACL table as being unused if this was the last
1250 	 * port that was using it.
1251 	 */
1252 	if (old_block->ports == 0)
1253 		old_block->in_use = false;
1254 
1255 	return dpaa2_switch_port_acl_tbl_bind(port_priv, block);
1256 }
1257 
1258 static int
1259 dpaa2_switch_port_block_unbind(struct ethsw_port_priv *port_priv,
1260 			       struct dpaa2_switch_filter_block *block)
1261 {
1262 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1263 	struct dpaa2_switch_filter_block *new_block;
1264 	int err;
1265 
1266 	/* Unoffload all the mirror entries found in the block from the
1267 	 * port leaving it.
1268 	 */
1269 	err = dpaa2_switch_block_unoffload_mirror(block, port_priv);
1270 	if (err)
1271 		return err;
1272 
1273 	/* We are the last port that leaves a block (an ACL table).
1274 	 * We'll continue to use this table.
1275 	 */
1276 	if (block->ports == BIT(port_priv->idx))
1277 		return 0;
1278 
1279 	err = dpaa2_switch_port_acl_tbl_unbind(port_priv, block);
1280 	if (err)
1281 		return err;
1282 
1283 	if (block->ports == 0)
1284 		block->in_use = false;
1285 
1286 	new_block = dpaa2_switch_filter_block_get_unused(ethsw);
1287 	new_block->in_use = true;
1288 	return dpaa2_switch_port_acl_tbl_bind(port_priv, new_block);
1289 }
1290 
1291 static int dpaa2_switch_setup_tc_block_bind(struct net_device *netdev,
1292 					    struct flow_block_offload *f)
1293 {
1294 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1295 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1296 	struct dpaa2_switch_filter_block *filter_block;
1297 	struct flow_block_cb *block_cb;
1298 	bool register_block = false;
1299 	int err;
1300 
1301 	block_cb = flow_block_cb_lookup(f->block,
1302 					dpaa2_switch_port_setup_tc_block_cb_ig,
1303 					ethsw);
1304 
1305 	if (!block_cb) {
1306 		/* If the filter block is not already known, then this port
1307 		 * must be the first to join it. In this case, we can just
1308 		 * continue to use our private table
1309 		 */
1310 		filter_block = port_priv->filter_block;
1311 
1312 		block_cb = flow_block_cb_alloc(dpaa2_switch_port_setup_tc_block_cb_ig,
1313 					       ethsw, filter_block, NULL);
1314 		if (IS_ERR(block_cb))
1315 			return PTR_ERR(block_cb);
1316 
1317 		register_block = true;
1318 	} else {
1319 		filter_block = flow_block_cb_priv(block_cb);
1320 	}
1321 
1322 	flow_block_cb_incref(block_cb);
1323 	err = dpaa2_switch_port_block_bind(port_priv, filter_block);
1324 	if (err)
1325 		goto err_block_bind;
1326 
1327 	if (register_block) {
1328 		flow_block_cb_add(block_cb, f);
1329 		list_add_tail(&block_cb->driver_list,
1330 			      &dpaa2_switch_block_cb_list);
1331 	}
1332 
1333 	return 0;
1334 
1335 err_block_bind:
1336 	if (!flow_block_cb_decref(block_cb))
1337 		flow_block_cb_free(block_cb);
1338 	return err;
1339 }
1340 
1341 static void dpaa2_switch_setup_tc_block_unbind(struct net_device *netdev,
1342 					       struct flow_block_offload *f)
1343 {
1344 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1345 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1346 	struct dpaa2_switch_filter_block *filter_block;
1347 	struct flow_block_cb *block_cb;
1348 	int err;
1349 
1350 	block_cb = flow_block_cb_lookup(f->block,
1351 					dpaa2_switch_port_setup_tc_block_cb_ig,
1352 					ethsw);
1353 	if (!block_cb)
1354 		return;
1355 
1356 	filter_block = flow_block_cb_priv(block_cb);
1357 	err = dpaa2_switch_port_block_unbind(port_priv, filter_block);
1358 	if (!err && !flow_block_cb_decref(block_cb)) {
1359 		flow_block_cb_remove(block_cb, f);
1360 		list_del(&block_cb->driver_list);
1361 	}
1362 }
1363 
1364 static int dpaa2_switch_setup_tc_block(struct net_device *netdev,
1365 				       struct flow_block_offload *f)
1366 {
1367 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1368 		return -EOPNOTSUPP;
1369 
1370 	f->driver_block_list = &dpaa2_switch_block_cb_list;
1371 
1372 	switch (f->command) {
1373 	case FLOW_BLOCK_BIND:
1374 		return dpaa2_switch_setup_tc_block_bind(netdev, f);
1375 	case FLOW_BLOCK_UNBIND:
1376 		dpaa2_switch_setup_tc_block_unbind(netdev, f);
1377 		return 0;
1378 	default:
1379 		return -EOPNOTSUPP;
1380 	}
1381 }
1382 
1383 static int dpaa2_switch_port_setup_tc(struct net_device *netdev,
1384 				      enum tc_setup_type type,
1385 				      void *type_data)
1386 {
1387 	switch (type) {
1388 	case TC_SETUP_BLOCK: {
1389 		return dpaa2_switch_setup_tc_block(netdev, type_data);
1390 	}
1391 	default:
1392 		return -EOPNOTSUPP;
1393 	}
1394 
1395 	return 0;
1396 }
1397 
1398 static const struct net_device_ops dpaa2_switch_port_ops = {
1399 	.ndo_open		= dpaa2_switch_port_open,
1400 	.ndo_stop		= dpaa2_switch_port_stop,
1401 
1402 	.ndo_set_mac_address	= eth_mac_addr,
1403 	.ndo_get_stats64	= dpaa2_switch_port_get_stats,
1404 	.ndo_change_mtu		= dpaa2_switch_port_change_mtu,
1405 	.ndo_has_offload_stats	= dpaa2_switch_port_has_offload_stats,
1406 	.ndo_get_offload_stats	= dpaa2_switch_port_get_offload_stats,
1407 	.ndo_fdb_dump		= dpaa2_switch_port_fdb_dump,
1408 	.ndo_vlan_rx_add_vid	= dpaa2_switch_port_vlan_add,
1409 	.ndo_vlan_rx_kill_vid	= dpaa2_switch_port_vlan_kill,
1410 
1411 	.ndo_start_xmit		= dpaa2_switch_port_tx,
1412 	.ndo_get_port_parent_id	= dpaa2_switch_port_parent_id,
1413 	.ndo_get_phys_port_name = dpaa2_switch_port_get_phys_name,
1414 	.ndo_setup_tc		= dpaa2_switch_port_setup_tc,
1415 };
1416 
1417 bool dpaa2_switch_port_dev_check(const struct net_device *netdev)
1418 {
1419 	return netdev->netdev_ops == &dpaa2_switch_port_ops;
1420 }
1421 
1422 static irqreturn_t dpaa2_switch_irq0_handler_thread(int irq_num, void *arg)
1423 {
1424 	struct device *dev = (struct device *)arg;
1425 	struct ethsw_core *ethsw = dev_get_drvdata(dev);
1426 	struct ethsw_port_priv *port_priv;
1427 	u32 status = ~0;
1428 	int err, if_id;
1429 
1430 	err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
1431 				  DPSW_IRQ_INDEX_IF, &status);
1432 	if (err) {
1433 		dev_err(dev, "Can't get irq status (err %d)\n", err);
1434 		goto out;
1435 	}
1436 
1437 	if_id = (status & 0xFFFF0000) >> 16;
1438 	port_priv = ethsw->ports[if_id];
1439 
1440 	if (status & DPSW_IRQ_EVENT_LINK_CHANGED) {
1441 		dpaa2_switch_port_link_state_update(port_priv->netdev);
1442 		dpaa2_switch_port_set_mac_addr(port_priv);
1443 	}
1444 out:
1445 	err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
1446 				    DPSW_IRQ_INDEX_IF, status);
1447 	if (err)
1448 		dev_err(dev, "Can't clear irq status (err %d)\n", err);
1449 
1450 	return IRQ_HANDLED;
1451 }
1452 
1453 static int dpaa2_switch_setup_irqs(struct fsl_mc_device *sw_dev)
1454 {
1455 	struct device *dev = &sw_dev->dev;
1456 	struct ethsw_core *ethsw = dev_get_drvdata(dev);
1457 	u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED;
1458 	struct fsl_mc_device_irq *irq;
1459 	int err;
1460 
1461 	err = fsl_mc_allocate_irqs(sw_dev);
1462 	if (err) {
1463 		dev_err(dev, "MC irqs allocation failed\n");
1464 		return err;
1465 	}
1466 
1467 	if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
1468 		err = -EINVAL;
1469 		goto free_irq;
1470 	}
1471 
1472 	err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
1473 				  DPSW_IRQ_INDEX_IF, 0);
1474 	if (err) {
1475 		dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
1476 		goto free_irq;
1477 	}
1478 
1479 	irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
1480 
1481 	err = devm_request_threaded_irq(dev, irq->msi_desc->irq,
1482 					NULL,
1483 					dpaa2_switch_irq0_handler_thread,
1484 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
1485 					dev_name(dev), dev);
1486 	if (err) {
1487 		dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
1488 		goto free_irq;
1489 	}
1490 
1491 	err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
1492 				DPSW_IRQ_INDEX_IF, mask);
1493 	if (err) {
1494 		dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
1495 		goto free_devm_irq;
1496 	}
1497 
1498 	err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
1499 				  DPSW_IRQ_INDEX_IF, 1);
1500 	if (err) {
1501 		dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
1502 		goto free_devm_irq;
1503 	}
1504 
1505 	return 0;
1506 
1507 free_devm_irq:
1508 	devm_free_irq(dev, irq->msi_desc->irq, dev);
1509 free_irq:
1510 	fsl_mc_free_irqs(sw_dev);
1511 	return err;
1512 }
1513 
1514 static void dpaa2_switch_teardown_irqs(struct fsl_mc_device *sw_dev)
1515 {
1516 	struct device *dev = &sw_dev->dev;
1517 	struct ethsw_core *ethsw = dev_get_drvdata(dev);
1518 	int err;
1519 
1520 	err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
1521 				  DPSW_IRQ_INDEX_IF, 0);
1522 	if (err)
1523 		dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
1524 
1525 	fsl_mc_free_irqs(sw_dev);
1526 }
1527 
1528 static int dpaa2_switch_port_set_learning(struct ethsw_port_priv *port_priv, bool enable)
1529 {
1530 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1531 	enum dpsw_learning_mode learn_mode;
1532 	int err;
1533 
1534 	if (enable)
1535 		learn_mode = DPSW_LEARNING_MODE_HW;
1536 	else
1537 		learn_mode = DPSW_LEARNING_MODE_DIS;
1538 
1539 	err = dpsw_if_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle,
1540 					port_priv->idx, learn_mode);
1541 	if (err)
1542 		netdev_err(port_priv->netdev, "dpsw_if_set_learning_mode err %d\n", err);
1543 
1544 	if (!enable)
1545 		dpaa2_switch_port_fast_age(port_priv);
1546 
1547 	return err;
1548 }
1549 
1550 static int dpaa2_switch_port_attr_stp_state_set(struct net_device *netdev,
1551 						u8 state)
1552 {
1553 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1554 	int err;
1555 
1556 	err = dpaa2_switch_port_set_stp_state(port_priv, state);
1557 	if (err)
1558 		return err;
1559 
1560 	switch (state) {
1561 	case BR_STATE_DISABLED:
1562 	case BR_STATE_BLOCKING:
1563 	case BR_STATE_LISTENING:
1564 		err = dpaa2_switch_port_set_learning(port_priv, false);
1565 		break;
1566 	case BR_STATE_LEARNING:
1567 	case BR_STATE_FORWARDING:
1568 		err = dpaa2_switch_port_set_learning(port_priv,
1569 						     port_priv->learn_ena);
1570 		break;
1571 	}
1572 
1573 	return err;
1574 }
1575 
1576 static int dpaa2_switch_port_flood(struct ethsw_port_priv *port_priv,
1577 				   struct switchdev_brport_flags flags)
1578 {
1579 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1580 
1581 	if (flags.mask & BR_BCAST_FLOOD)
1582 		port_priv->bcast_flood = !!(flags.val & BR_BCAST_FLOOD);
1583 
1584 	if (flags.mask & BR_FLOOD)
1585 		port_priv->ucast_flood = !!(flags.val & BR_FLOOD);
1586 
1587 	return dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
1588 }
1589 
1590 static int dpaa2_switch_port_pre_bridge_flags(struct net_device *netdev,
1591 					      struct switchdev_brport_flags flags,
1592 					      struct netlink_ext_ack *extack)
1593 {
1594 	if (flags.mask & ~(BR_LEARNING | BR_BCAST_FLOOD | BR_FLOOD |
1595 			   BR_MCAST_FLOOD))
1596 		return -EINVAL;
1597 
1598 	if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD)) {
1599 		bool multicast = !!(flags.val & BR_MCAST_FLOOD);
1600 		bool unicast = !!(flags.val & BR_FLOOD);
1601 
1602 		if (unicast != multicast) {
1603 			NL_SET_ERR_MSG_MOD(extack,
1604 					   "Cannot configure multicast flooding independently of unicast");
1605 			return -EINVAL;
1606 		}
1607 	}
1608 
1609 	return 0;
1610 }
1611 
1612 static int dpaa2_switch_port_bridge_flags(struct net_device *netdev,
1613 					  struct switchdev_brport_flags flags,
1614 					  struct netlink_ext_ack *extack)
1615 {
1616 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1617 	int err;
1618 
1619 	if (flags.mask & BR_LEARNING) {
1620 		bool learn_ena = !!(flags.val & BR_LEARNING);
1621 
1622 		err = dpaa2_switch_port_set_learning(port_priv, learn_ena);
1623 		if (err)
1624 			return err;
1625 		port_priv->learn_ena = learn_ena;
1626 	}
1627 
1628 	if (flags.mask & (BR_BCAST_FLOOD | BR_FLOOD | BR_MCAST_FLOOD)) {
1629 		err = dpaa2_switch_port_flood(port_priv, flags);
1630 		if (err)
1631 			return err;
1632 	}
1633 
1634 	return 0;
1635 }
1636 
1637 static int dpaa2_switch_port_attr_set(struct net_device *netdev, const void *ctx,
1638 				      const struct switchdev_attr *attr,
1639 				      struct netlink_ext_ack *extack)
1640 {
1641 	int err = 0;
1642 
1643 	switch (attr->id) {
1644 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1645 		err = dpaa2_switch_port_attr_stp_state_set(netdev,
1646 							   attr->u.stp_state);
1647 		break;
1648 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1649 		if (!attr->u.vlan_filtering) {
1650 			NL_SET_ERR_MSG_MOD(extack,
1651 					   "The DPAA2 switch does not support VLAN-unaware operation");
1652 			return -EOPNOTSUPP;
1653 		}
1654 		break;
1655 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
1656 		err = dpaa2_switch_port_pre_bridge_flags(netdev, attr->u.brport_flags, extack);
1657 		break;
1658 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
1659 		err = dpaa2_switch_port_bridge_flags(netdev, attr->u.brport_flags, extack);
1660 		break;
1661 	default:
1662 		err = -EOPNOTSUPP;
1663 		break;
1664 	}
1665 
1666 	return err;
1667 }
1668 
1669 int dpaa2_switch_port_vlans_add(struct net_device *netdev,
1670 				const struct switchdev_obj_port_vlan *vlan)
1671 {
1672 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1673 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1674 	struct dpsw_attr *attr = &ethsw->sw_attr;
1675 	int err = 0;
1676 
1677 	/* Make sure that the VLAN is not already configured
1678 	 * on the switch port
1679 	 */
1680 	if (port_priv->vlans[vlan->vid] & ETHSW_VLAN_MEMBER)
1681 		return -EEXIST;
1682 
1683 	/* Check if there is space for a new VLAN */
1684 	err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1685 				  &ethsw->sw_attr);
1686 	if (err) {
1687 		netdev_err(netdev, "dpsw_get_attributes err %d\n", err);
1688 		return err;
1689 	}
1690 	if (attr->max_vlans - attr->num_vlans < 1)
1691 		return -ENOSPC;
1692 
1693 	/* Check if there is space for a new VLAN */
1694 	err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1695 				  &ethsw->sw_attr);
1696 	if (err) {
1697 		netdev_err(netdev, "dpsw_get_attributes err %d\n", err);
1698 		return err;
1699 	}
1700 	if (attr->max_vlans - attr->num_vlans < 1)
1701 		return -ENOSPC;
1702 
1703 	if (!port_priv->ethsw_data->vlans[vlan->vid]) {
1704 		/* this is a new VLAN */
1705 		err = dpaa2_switch_add_vlan(port_priv, vlan->vid);
1706 		if (err)
1707 			return err;
1708 
1709 		port_priv->ethsw_data->vlans[vlan->vid] |= ETHSW_VLAN_GLOBAL;
1710 	}
1711 
1712 	return dpaa2_switch_port_add_vlan(port_priv, vlan->vid, vlan->flags);
1713 }
1714 
1715 static int dpaa2_switch_port_lookup_address(struct net_device *netdev, int is_uc,
1716 					    const unsigned char *addr)
1717 {
1718 	struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
1719 	struct netdev_hw_addr *ha;
1720 
1721 	netif_addr_lock_bh(netdev);
1722 	list_for_each_entry(ha, &list->list, list) {
1723 		if (ether_addr_equal(ha->addr, addr)) {
1724 			netif_addr_unlock_bh(netdev);
1725 			return 1;
1726 		}
1727 	}
1728 	netif_addr_unlock_bh(netdev);
1729 	return 0;
1730 }
1731 
1732 static int dpaa2_switch_port_mdb_add(struct net_device *netdev,
1733 				     const struct switchdev_obj_port_mdb *mdb)
1734 {
1735 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1736 	int err;
1737 
1738 	/* Check if address is already set on this port */
1739 	if (dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr))
1740 		return -EEXIST;
1741 
1742 	err = dpaa2_switch_port_fdb_add_mc(port_priv, mdb->addr);
1743 	if (err)
1744 		return err;
1745 
1746 	err = dev_mc_add(netdev, mdb->addr);
1747 	if (err) {
1748 		netdev_err(netdev, "dev_mc_add err %d\n", err);
1749 		dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr);
1750 	}
1751 
1752 	return err;
1753 }
1754 
1755 static int dpaa2_switch_port_obj_add(struct net_device *netdev,
1756 				     const struct switchdev_obj *obj)
1757 {
1758 	int err;
1759 
1760 	switch (obj->id) {
1761 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1762 		err = dpaa2_switch_port_vlans_add(netdev,
1763 						  SWITCHDEV_OBJ_PORT_VLAN(obj));
1764 		break;
1765 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1766 		err = dpaa2_switch_port_mdb_add(netdev,
1767 						SWITCHDEV_OBJ_PORT_MDB(obj));
1768 		break;
1769 	default:
1770 		err = -EOPNOTSUPP;
1771 		break;
1772 	}
1773 
1774 	return err;
1775 }
1776 
1777 static int dpaa2_switch_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
1778 {
1779 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1780 	struct net_device *netdev = port_priv->netdev;
1781 	struct dpsw_vlan_if_cfg vcfg;
1782 	int i, err;
1783 
1784 	if (!port_priv->vlans[vid])
1785 		return -ENOENT;
1786 
1787 	if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
1788 		/* If we are deleting the PVID of a port, use VLAN 4095 instead
1789 		 * as we are sure that neither the bridge nor the 8021q module
1790 		 * will use it
1791 		 */
1792 		err = dpaa2_switch_port_set_pvid(port_priv, 4095);
1793 		if (err)
1794 			return err;
1795 	}
1796 
1797 	vcfg.num_ifs = 1;
1798 	vcfg.if_id[0] = port_priv->idx;
1799 	if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
1800 		err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
1801 						   ethsw->dpsw_handle,
1802 						   vid, &vcfg);
1803 		if (err) {
1804 			netdev_err(netdev,
1805 				   "dpsw_vlan_remove_if_untagged err %d\n",
1806 				   err);
1807 		}
1808 		port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
1809 	}
1810 
1811 	if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
1812 		err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1813 					  vid, &vcfg);
1814 		if (err) {
1815 			netdev_err(netdev,
1816 				   "dpsw_vlan_remove_if err %d\n", err);
1817 			return err;
1818 		}
1819 		port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
1820 
1821 		/* Delete VLAN from switch if it is no longer configured on
1822 		 * any port
1823 		 */
1824 		for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1825 			if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
1826 				return 0; /* Found a port member in VID */
1827 
1828 		ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
1829 
1830 		err = dpaa2_switch_dellink(ethsw, vid);
1831 		if (err)
1832 			return err;
1833 	}
1834 
1835 	return 0;
1836 }
1837 
1838 int dpaa2_switch_port_vlans_del(struct net_device *netdev,
1839 				const struct switchdev_obj_port_vlan *vlan)
1840 {
1841 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1842 
1843 	if (netif_is_bridge_master(vlan->obj.orig_dev))
1844 		return -EOPNOTSUPP;
1845 
1846 	return dpaa2_switch_port_del_vlan(port_priv, vlan->vid);
1847 }
1848 
1849 static int dpaa2_switch_port_mdb_del(struct net_device *netdev,
1850 				     const struct switchdev_obj_port_mdb *mdb)
1851 {
1852 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1853 	int err;
1854 
1855 	if (!dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr))
1856 		return -ENOENT;
1857 
1858 	err = dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr);
1859 	if (err)
1860 		return err;
1861 
1862 	err = dev_mc_del(netdev, mdb->addr);
1863 	if (err) {
1864 		netdev_err(netdev, "dev_mc_del err %d\n", err);
1865 		return err;
1866 	}
1867 
1868 	return err;
1869 }
1870 
1871 static int dpaa2_switch_port_obj_del(struct net_device *netdev,
1872 				     const struct switchdev_obj *obj)
1873 {
1874 	int err;
1875 
1876 	switch (obj->id) {
1877 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1878 		err = dpaa2_switch_port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
1879 		break;
1880 	case SWITCHDEV_OBJ_ID_PORT_MDB:
1881 		err = dpaa2_switch_port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
1882 		break;
1883 	default:
1884 		err = -EOPNOTSUPP;
1885 		break;
1886 	}
1887 	return err;
1888 }
1889 
1890 static int dpaa2_switch_port_attr_set_event(struct net_device *netdev,
1891 					    struct switchdev_notifier_port_attr_info *ptr)
1892 {
1893 	int err;
1894 
1895 	err = switchdev_handle_port_attr_set(netdev, ptr,
1896 					     dpaa2_switch_port_dev_check,
1897 					     dpaa2_switch_port_attr_set);
1898 	return notifier_from_errno(err);
1899 }
1900 
1901 static struct notifier_block dpaa2_switch_port_switchdev_nb;
1902 static struct notifier_block dpaa2_switch_port_switchdev_blocking_nb;
1903 
1904 static int dpaa2_switch_port_bridge_join(struct net_device *netdev,
1905 					 struct net_device *upper_dev,
1906 					 struct netlink_ext_ack *extack)
1907 {
1908 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1909 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1910 	struct ethsw_port_priv *other_port_priv;
1911 	struct net_device *other_dev;
1912 	struct list_head *iter;
1913 	bool learn_ena;
1914 	int err;
1915 
1916 	netdev_for_each_lower_dev(upper_dev, other_dev, iter) {
1917 		if (!dpaa2_switch_port_dev_check(other_dev))
1918 			continue;
1919 
1920 		other_port_priv = netdev_priv(other_dev);
1921 		if (other_port_priv->ethsw_data != port_priv->ethsw_data) {
1922 			NL_SET_ERR_MSG_MOD(extack,
1923 					   "Interface from a different DPSW is in the bridge already");
1924 			return -EINVAL;
1925 		}
1926 	}
1927 
1928 	/* Delete the previously manually installed VLAN 1 */
1929 	err = dpaa2_switch_port_del_vlan(port_priv, 1);
1930 	if (err)
1931 		return err;
1932 
1933 	dpaa2_switch_port_set_fdb(port_priv, upper_dev);
1934 
1935 	/* Inherit the initial bridge port learning state */
1936 	learn_ena = br_port_flag_is_set(netdev, BR_LEARNING);
1937 	err = dpaa2_switch_port_set_learning(port_priv, learn_ena);
1938 	port_priv->learn_ena = learn_ena;
1939 
1940 	/* Setup the egress flood policy (broadcast, unknown unicast) */
1941 	err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
1942 	if (err)
1943 		goto err_egress_flood;
1944 
1945 	err = switchdev_bridge_port_offload(netdev, netdev, NULL,
1946 					    &dpaa2_switch_port_switchdev_nb,
1947 					    &dpaa2_switch_port_switchdev_blocking_nb,
1948 					    false, extack);
1949 	if (err)
1950 		goto err_switchdev_offload;
1951 
1952 	return 0;
1953 
1954 err_switchdev_offload:
1955 err_egress_flood:
1956 	dpaa2_switch_port_set_fdb(port_priv, NULL);
1957 	return err;
1958 }
1959 
1960 static int dpaa2_switch_port_clear_rxvlan(struct net_device *vdev, int vid, void *arg)
1961 {
1962 	__be16 vlan_proto = htons(ETH_P_8021Q);
1963 
1964 	if (vdev)
1965 		vlan_proto = vlan_dev_vlan_proto(vdev);
1966 
1967 	return dpaa2_switch_port_vlan_kill(arg, vlan_proto, vid);
1968 }
1969 
1970 static int dpaa2_switch_port_restore_rxvlan(struct net_device *vdev, int vid, void *arg)
1971 {
1972 	__be16 vlan_proto = htons(ETH_P_8021Q);
1973 
1974 	if (vdev)
1975 		vlan_proto = vlan_dev_vlan_proto(vdev);
1976 
1977 	return dpaa2_switch_port_vlan_add(arg, vlan_proto, vid);
1978 }
1979 
1980 static void dpaa2_switch_port_pre_bridge_leave(struct net_device *netdev)
1981 {
1982 	switchdev_bridge_port_unoffload(netdev, NULL,
1983 					&dpaa2_switch_port_switchdev_nb,
1984 					&dpaa2_switch_port_switchdev_blocking_nb);
1985 }
1986 
1987 static int dpaa2_switch_port_bridge_leave(struct net_device *netdev)
1988 {
1989 	struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1990 	struct dpaa2_switch_fdb *old_fdb = port_priv->fdb;
1991 	struct ethsw_core *ethsw = port_priv->ethsw_data;
1992 	int err;
1993 
1994 	/* First of all, fast age any learn FDB addresses on this switch port */
1995 	dpaa2_switch_port_fast_age(port_priv);
1996 
1997 	/* Clear all RX VLANs installed through vlan_vid_add() either as VLAN
1998 	 * upper devices or otherwise from the FDB table that we are about to
1999 	 * leave
2000 	 */
2001 	err = vlan_for_each(netdev, dpaa2_switch_port_clear_rxvlan, netdev);
2002 	if (err)
2003 		netdev_err(netdev, "Unable to clear RX VLANs from old FDB table, err (%d)\n", err);
2004 
2005 	dpaa2_switch_port_set_fdb(port_priv, NULL);
2006 
2007 	/* Restore all RX VLANs into the new FDB table that we just joined */
2008 	err = vlan_for_each(netdev, dpaa2_switch_port_restore_rxvlan, netdev);
2009 	if (err)
2010 		netdev_err(netdev, "Unable to restore RX VLANs to the new FDB, err (%d)\n", err);
2011 
2012 	/* Reset the flooding state to denote that this port can send any
2013 	 * packet in standalone mode. With this, we are also ensuring that any
2014 	 * later bridge join will have the flooding flag on.
2015 	 */
2016 	port_priv->bcast_flood = true;
2017 	port_priv->ucast_flood = true;
2018 
2019 	/* Setup the egress flood policy (broadcast, unknown unicast).
2020 	 * When the port is not under a bridge, only the CTRL interface is part
2021 	 * of the flooding domain besides the actual port
2022 	 */
2023 	err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
2024 	if (err)
2025 		return err;
2026 
2027 	/* Recreate the egress flood domain of the FDB that we just left */
2028 	err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id);
2029 	if (err)
2030 		return err;
2031 
2032 	/* No HW learning when not under a bridge */
2033 	err = dpaa2_switch_port_set_learning(port_priv, false);
2034 	if (err)
2035 		return err;
2036 	port_priv->learn_ena = false;
2037 
2038 	/* Add the VLAN 1 as PVID when not under a bridge. We need this since
2039 	 * the dpaa2 switch interfaces are not capable to be VLAN unaware
2040 	 */
2041 	return dpaa2_switch_port_add_vlan(port_priv, DEFAULT_VLAN_ID,
2042 					  BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID);
2043 }
2044 
2045 static int dpaa2_switch_prevent_bridging_with_8021q_upper(struct net_device *netdev)
2046 {
2047 	struct net_device *upper_dev;
2048 	struct list_head *iter;
2049 
2050 	/* RCU read lock not necessary because we have write-side protection
2051 	 * (rtnl_mutex), however a non-rcu iterator does not exist.
2052 	 */
2053 	netdev_for_each_upper_dev_rcu(netdev, upper_dev, iter)
2054 		if (is_vlan_dev(upper_dev))
2055 			return -EOPNOTSUPP;
2056 
2057 	return 0;
2058 }
2059 
2060 static int
2061 dpaa2_switch_prechangeupper_sanity_checks(struct net_device *netdev,
2062 					  struct net_device *upper_dev,
2063 					  struct netlink_ext_ack *extack)
2064 {
2065 	int err;
2066 
2067 	if (!br_vlan_enabled(upper_dev)) {
2068 		NL_SET_ERR_MSG_MOD(extack, "Cannot join a VLAN-unaware bridge");
2069 		return -EOPNOTSUPP;
2070 	}
2071 
2072 	err = dpaa2_switch_prevent_bridging_with_8021q_upper(netdev);
2073 	if (err) {
2074 		NL_SET_ERR_MSG_MOD(extack,
2075 				   "Cannot join a bridge while VLAN uppers are present");
2076 		return 0;
2077 	}
2078 
2079 	return 0;
2080 }
2081 
2082 static int dpaa2_switch_port_netdevice_event(struct notifier_block *nb,
2083 					     unsigned long event, void *ptr)
2084 {
2085 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
2086 	struct netdev_notifier_changeupper_info *info = ptr;
2087 	struct netlink_ext_ack *extack;
2088 	struct net_device *upper_dev;
2089 	int err = 0;
2090 
2091 	if (!dpaa2_switch_port_dev_check(netdev))
2092 		return NOTIFY_DONE;
2093 
2094 	extack = netdev_notifier_info_to_extack(&info->info);
2095 
2096 	switch (event) {
2097 	case NETDEV_PRECHANGEUPPER:
2098 		upper_dev = info->upper_dev;
2099 		if (!netif_is_bridge_master(upper_dev))
2100 			break;
2101 
2102 		err = dpaa2_switch_prechangeupper_sanity_checks(netdev,
2103 								upper_dev,
2104 								extack);
2105 		if (err)
2106 			goto out;
2107 
2108 		if (!info->linking)
2109 			dpaa2_switch_port_pre_bridge_leave(netdev);
2110 
2111 		break;
2112 	case NETDEV_CHANGEUPPER:
2113 		upper_dev = info->upper_dev;
2114 		if (netif_is_bridge_master(upper_dev)) {
2115 			if (info->linking)
2116 				err = dpaa2_switch_port_bridge_join(netdev,
2117 								    upper_dev,
2118 								    extack);
2119 			else
2120 				err = dpaa2_switch_port_bridge_leave(netdev);
2121 		}
2122 		break;
2123 	}
2124 
2125 out:
2126 	return notifier_from_errno(err);
2127 }
2128 
2129 struct ethsw_switchdev_event_work {
2130 	struct work_struct work;
2131 	struct switchdev_notifier_fdb_info fdb_info;
2132 	struct net_device *dev;
2133 	unsigned long event;
2134 };
2135 
2136 static void dpaa2_switch_event_work(struct work_struct *work)
2137 {
2138 	struct ethsw_switchdev_event_work *switchdev_work =
2139 		container_of(work, struct ethsw_switchdev_event_work, work);
2140 	struct net_device *dev = switchdev_work->dev;
2141 	struct switchdev_notifier_fdb_info *fdb_info;
2142 	int err;
2143 
2144 	rtnl_lock();
2145 	fdb_info = &switchdev_work->fdb_info;
2146 
2147 	switch (switchdev_work->event) {
2148 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2149 		if (!fdb_info->added_by_user || fdb_info->is_local)
2150 			break;
2151 		if (is_unicast_ether_addr(fdb_info->addr))
2152 			err = dpaa2_switch_port_fdb_add_uc(netdev_priv(dev),
2153 							   fdb_info->addr);
2154 		else
2155 			err = dpaa2_switch_port_fdb_add_mc(netdev_priv(dev),
2156 							   fdb_info->addr);
2157 		if (err)
2158 			break;
2159 		fdb_info->offloaded = true;
2160 		call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
2161 					 &fdb_info->info, NULL);
2162 		break;
2163 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2164 		if (!fdb_info->added_by_user || fdb_info->is_local)
2165 			break;
2166 		if (is_unicast_ether_addr(fdb_info->addr))
2167 			dpaa2_switch_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
2168 		else
2169 			dpaa2_switch_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
2170 		break;
2171 	}
2172 
2173 	rtnl_unlock();
2174 	kfree(switchdev_work->fdb_info.addr);
2175 	kfree(switchdev_work);
2176 	dev_put(dev);
2177 }
2178 
2179 /* Called under rcu_read_lock() */
2180 static int dpaa2_switch_port_event(struct notifier_block *nb,
2181 				   unsigned long event, void *ptr)
2182 {
2183 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2184 	struct ethsw_port_priv *port_priv = netdev_priv(dev);
2185 	struct ethsw_switchdev_event_work *switchdev_work;
2186 	struct switchdev_notifier_fdb_info *fdb_info = ptr;
2187 	struct ethsw_core *ethsw = port_priv->ethsw_data;
2188 
2189 	if (event == SWITCHDEV_PORT_ATTR_SET)
2190 		return dpaa2_switch_port_attr_set_event(dev, ptr);
2191 
2192 	if (!dpaa2_switch_port_dev_check(dev))
2193 		return NOTIFY_DONE;
2194 
2195 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2196 	if (!switchdev_work)
2197 		return NOTIFY_BAD;
2198 
2199 	INIT_WORK(&switchdev_work->work, dpaa2_switch_event_work);
2200 	switchdev_work->dev = dev;
2201 	switchdev_work->event = event;
2202 
2203 	switch (event) {
2204 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
2205 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
2206 		memcpy(&switchdev_work->fdb_info, ptr,
2207 		       sizeof(switchdev_work->fdb_info));
2208 		switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2209 		if (!switchdev_work->fdb_info.addr)
2210 			goto err_addr_alloc;
2211 
2212 		ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2213 				fdb_info->addr);
2214 
2215 		/* Take a reference on the device to avoid being freed. */
2216 		dev_hold(dev);
2217 		break;
2218 	default:
2219 		kfree(switchdev_work);
2220 		return NOTIFY_DONE;
2221 	}
2222 
2223 	queue_work(ethsw->workqueue, &switchdev_work->work);
2224 
2225 	return NOTIFY_DONE;
2226 
2227 err_addr_alloc:
2228 	kfree(switchdev_work);
2229 	return NOTIFY_BAD;
2230 }
2231 
2232 static int dpaa2_switch_port_obj_event(unsigned long event,
2233 				       struct net_device *netdev,
2234 				       struct switchdev_notifier_port_obj_info *port_obj_info)
2235 {
2236 	int err = -EOPNOTSUPP;
2237 
2238 	if (!dpaa2_switch_port_dev_check(netdev))
2239 		return NOTIFY_DONE;
2240 
2241 	switch (event) {
2242 	case SWITCHDEV_PORT_OBJ_ADD:
2243 		err = dpaa2_switch_port_obj_add(netdev, port_obj_info->obj);
2244 		break;
2245 	case SWITCHDEV_PORT_OBJ_DEL:
2246 		err = dpaa2_switch_port_obj_del(netdev, port_obj_info->obj);
2247 		break;
2248 	}
2249 
2250 	port_obj_info->handled = true;
2251 	return notifier_from_errno(err);
2252 }
2253 
2254 static int dpaa2_switch_port_blocking_event(struct notifier_block *nb,
2255 					    unsigned long event, void *ptr)
2256 {
2257 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2258 
2259 	switch (event) {
2260 	case SWITCHDEV_PORT_OBJ_ADD:
2261 	case SWITCHDEV_PORT_OBJ_DEL:
2262 		return dpaa2_switch_port_obj_event(event, dev, ptr);
2263 	case SWITCHDEV_PORT_ATTR_SET:
2264 		return dpaa2_switch_port_attr_set_event(dev, ptr);
2265 	}
2266 
2267 	return NOTIFY_DONE;
2268 }
2269 
2270 /* Build a linear skb based on a single-buffer frame descriptor */
2271 static struct sk_buff *dpaa2_switch_build_linear_skb(struct ethsw_core *ethsw,
2272 						     const struct dpaa2_fd *fd)
2273 {
2274 	u16 fd_offset = dpaa2_fd_get_offset(fd);
2275 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
2276 	u32 fd_length = dpaa2_fd_get_len(fd);
2277 	struct device *dev = ethsw->dev;
2278 	struct sk_buff *skb = NULL;
2279 	void *fd_vaddr;
2280 
2281 	fd_vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, addr);
2282 	dma_unmap_page(dev, addr, DPAA2_SWITCH_RX_BUF_SIZE,
2283 		       DMA_FROM_DEVICE);
2284 
2285 	skb = build_skb(fd_vaddr, DPAA2_SWITCH_RX_BUF_SIZE +
2286 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
2287 	if (unlikely(!skb)) {
2288 		dev_err(dev, "build_skb() failed\n");
2289 		return NULL;
2290 	}
2291 
2292 	skb_reserve(skb, fd_offset);
2293 	skb_put(skb, fd_length);
2294 
2295 	ethsw->buf_count--;
2296 
2297 	return skb;
2298 }
2299 
2300 static void dpaa2_switch_tx_conf(struct dpaa2_switch_fq *fq,
2301 				 const struct dpaa2_fd *fd)
2302 {
2303 	dpaa2_switch_free_fd(fq->ethsw, fd);
2304 }
2305 
2306 static void dpaa2_switch_rx(struct dpaa2_switch_fq *fq,
2307 			    const struct dpaa2_fd *fd)
2308 {
2309 	struct ethsw_core *ethsw = fq->ethsw;
2310 	struct ethsw_port_priv *port_priv;
2311 	struct net_device *netdev;
2312 	struct vlan_ethhdr *hdr;
2313 	struct sk_buff *skb;
2314 	u16 vlan_tci, vid;
2315 	int if_id, err;
2316 
2317 	/* get switch ingress interface ID */
2318 	if_id = upper_32_bits(dpaa2_fd_get_flc(fd)) & 0x0000FFFF;
2319 
2320 	if (if_id >= ethsw->sw_attr.num_ifs) {
2321 		dev_err(ethsw->dev, "Frame received from unknown interface!\n");
2322 		goto err_free_fd;
2323 	}
2324 	port_priv = ethsw->ports[if_id];
2325 	netdev = port_priv->netdev;
2326 
2327 	/* build the SKB based on the FD received */
2328 	if (dpaa2_fd_get_format(fd) != dpaa2_fd_single) {
2329 		if (net_ratelimit()) {
2330 			netdev_err(netdev, "Received invalid frame format\n");
2331 			goto err_free_fd;
2332 		}
2333 	}
2334 
2335 	skb = dpaa2_switch_build_linear_skb(ethsw, fd);
2336 	if (unlikely(!skb))
2337 		goto err_free_fd;
2338 
2339 	skb_reset_mac_header(skb);
2340 
2341 	/* Remove the VLAN header if the packet that we just received has a vid
2342 	 * equal to the port PVIDs. Since the dpaa2-switch can operate only in
2343 	 * VLAN-aware mode and no alterations are made on the packet when it's
2344 	 * redirected/mirrored to the control interface, we are sure that there
2345 	 * will always be a VLAN header present.
2346 	 */
2347 	hdr = vlan_eth_hdr(skb);
2348 	vid = ntohs(hdr->h_vlan_TCI) & VLAN_VID_MASK;
2349 	if (vid == port_priv->pvid) {
2350 		err = __skb_vlan_pop(skb, &vlan_tci);
2351 		if (err) {
2352 			dev_info(ethsw->dev, "__skb_vlan_pop() returned %d", err);
2353 			goto err_free_fd;
2354 		}
2355 	}
2356 
2357 	skb->dev = netdev;
2358 	skb->protocol = eth_type_trans(skb, skb->dev);
2359 
2360 	/* Setup the offload_fwd_mark only if the port is under a bridge */
2361 	skb->offload_fwd_mark = !!(port_priv->fdb->bridge_dev);
2362 
2363 	netif_receive_skb(skb);
2364 
2365 	return;
2366 
2367 err_free_fd:
2368 	dpaa2_switch_free_fd(ethsw, fd);
2369 }
2370 
2371 static void dpaa2_switch_detect_features(struct ethsw_core *ethsw)
2372 {
2373 	ethsw->features = 0;
2374 
2375 	if (ethsw->major > 8 || (ethsw->major == 8 && ethsw->minor >= 6))
2376 		ethsw->features |= ETHSW_FEATURE_MAC_ADDR;
2377 }
2378 
2379 static int dpaa2_switch_setup_fqs(struct ethsw_core *ethsw)
2380 {
2381 	struct dpsw_ctrl_if_attr ctrl_if_attr;
2382 	struct device *dev = ethsw->dev;
2383 	int i = 0;
2384 	int err;
2385 
2386 	err = dpsw_ctrl_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
2387 					  &ctrl_if_attr);
2388 	if (err) {
2389 		dev_err(dev, "dpsw_ctrl_if_get_attributes() = %d\n", err);
2390 		return err;
2391 	}
2392 
2393 	ethsw->fq[i].fqid = ctrl_if_attr.rx_fqid;
2394 	ethsw->fq[i].ethsw = ethsw;
2395 	ethsw->fq[i++].type = DPSW_QUEUE_RX;
2396 
2397 	ethsw->fq[i].fqid = ctrl_if_attr.tx_err_conf_fqid;
2398 	ethsw->fq[i].ethsw = ethsw;
2399 	ethsw->fq[i++].type = DPSW_QUEUE_TX_ERR_CONF;
2400 
2401 	return 0;
2402 }
2403 
2404 /* Free buffers acquired from the buffer pool or which were meant to
2405  * be released in the pool
2406  */
2407 static void dpaa2_switch_free_bufs(struct ethsw_core *ethsw, u64 *buf_array, int count)
2408 {
2409 	struct device *dev = ethsw->dev;
2410 	void *vaddr;
2411 	int i;
2412 
2413 	for (i = 0; i < count; i++) {
2414 		vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, buf_array[i]);
2415 		dma_unmap_page(dev, buf_array[i], DPAA2_SWITCH_RX_BUF_SIZE,
2416 			       DMA_FROM_DEVICE);
2417 		free_pages((unsigned long)vaddr, 0);
2418 	}
2419 }
2420 
2421 /* Perform a single release command to add buffers
2422  * to the specified buffer pool
2423  */
2424 static int dpaa2_switch_add_bufs(struct ethsw_core *ethsw, u16 bpid)
2425 {
2426 	struct device *dev = ethsw->dev;
2427 	u64 buf_array[BUFS_PER_CMD];
2428 	struct page *page;
2429 	int retries = 0;
2430 	dma_addr_t addr;
2431 	int err;
2432 	int i;
2433 
2434 	for (i = 0; i < BUFS_PER_CMD; i++) {
2435 		/* Allocate one page for each Rx buffer. WRIOP sees
2436 		 * the entire page except for a tailroom reserved for
2437 		 * skb shared info
2438 		 */
2439 		page = dev_alloc_pages(0);
2440 		if (!page) {
2441 			dev_err(dev, "buffer allocation failed\n");
2442 			goto err_alloc;
2443 		}
2444 
2445 		addr = dma_map_page(dev, page, 0, DPAA2_SWITCH_RX_BUF_SIZE,
2446 				    DMA_FROM_DEVICE);
2447 		if (dma_mapping_error(dev, addr)) {
2448 			dev_err(dev, "dma_map_single() failed\n");
2449 			goto err_map;
2450 		}
2451 		buf_array[i] = addr;
2452 	}
2453 
2454 release_bufs:
2455 	/* In case the portal is busy, retry until successful or
2456 	 * max retries hit.
2457 	 */
2458 	while ((err = dpaa2_io_service_release(NULL, bpid,
2459 					       buf_array, i)) == -EBUSY) {
2460 		if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES)
2461 			break;
2462 
2463 		cpu_relax();
2464 	}
2465 
2466 	/* If release command failed, clean up and bail out. */
2467 	if (err) {
2468 		dpaa2_switch_free_bufs(ethsw, buf_array, i);
2469 		return 0;
2470 	}
2471 
2472 	return i;
2473 
2474 err_map:
2475 	__free_pages(page, 0);
2476 err_alloc:
2477 	/* If we managed to allocate at least some buffers,
2478 	 * release them to hardware
2479 	 */
2480 	if (i)
2481 		goto release_bufs;
2482 
2483 	return 0;
2484 }
2485 
2486 static int dpaa2_switch_refill_bp(struct ethsw_core *ethsw)
2487 {
2488 	int *count = &ethsw->buf_count;
2489 	int new_count;
2490 	int err = 0;
2491 
2492 	if (unlikely(*count < DPAA2_ETHSW_REFILL_THRESH)) {
2493 		do {
2494 			new_count = dpaa2_switch_add_bufs(ethsw, ethsw->bpid);
2495 			if (unlikely(!new_count)) {
2496 				/* Out of memory; abort for now, we'll
2497 				 * try later on
2498 				 */
2499 				break;
2500 			}
2501 			*count += new_count;
2502 		} while (*count < DPAA2_ETHSW_NUM_BUFS);
2503 
2504 		if (unlikely(*count < DPAA2_ETHSW_NUM_BUFS))
2505 			err = -ENOMEM;
2506 	}
2507 
2508 	return err;
2509 }
2510 
2511 static int dpaa2_switch_seed_bp(struct ethsw_core *ethsw)
2512 {
2513 	int *count, i;
2514 
2515 	for (i = 0; i < DPAA2_ETHSW_NUM_BUFS; i += BUFS_PER_CMD) {
2516 		count = &ethsw->buf_count;
2517 		*count += dpaa2_switch_add_bufs(ethsw, ethsw->bpid);
2518 
2519 		if (unlikely(*count < BUFS_PER_CMD))
2520 			return -ENOMEM;
2521 	}
2522 
2523 	return 0;
2524 }
2525 
2526 static void dpaa2_switch_drain_bp(struct ethsw_core *ethsw)
2527 {
2528 	u64 buf_array[BUFS_PER_CMD];
2529 	int ret;
2530 
2531 	do {
2532 		ret = dpaa2_io_service_acquire(NULL, ethsw->bpid,
2533 					       buf_array, BUFS_PER_CMD);
2534 		if (ret < 0) {
2535 			dev_err(ethsw->dev,
2536 				"dpaa2_io_service_acquire() = %d\n", ret);
2537 			return;
2538 		}
2539 		dpaa2_switch_free_bufs(ethsw, buf_array, ret);
2540 
2541 	} while (ret);
2542 }
2543 
2544 static int dpaa2_switch_setup_dpbp(struct ethsw_core *ethsw)
2545 {
2546 	struct dpsw_ctrl_if_pools_cfg dpsw_ctrl_if_pools_cfg = { 0 };
2547 	struct device *dev = ethsw->dev;
2548 	struct fsl_mc_device *dpbp_dev;
2549 	struct dpbp_attr dpbp_attrs;
2550 	int err;
2551 
2552 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2553 				     &dpbp_dev);
2554 	if (err) {
2555 		if (err == -ENXIO)
2556 			err = -EPROBE_DEFER;
2557 		else
2558 			dev_err(dev, "DPBP device allocation failed\n");
2559 		return err;
2560 	}
2561 	ethsw->dpbp_dev = dpbp_dev;
2562 
2563 	err = dpbp_open(ethsw->mc_io, 0, dpbp_dev->obj_desc.id,
2564 			&dpbp_dev->mc_handle);
2565 	if (err) {
2566 		dev_err(dev, "dpbp_open() failed\n");
2567 		goto err_open;
2568 	}
2569 
2570 	err = dpbp_reset(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2571 	if (err) {
2572 		dev_err(dev, "dpbp_reset() failed\n");
2573 		goto err_reset;
2574 	}
2575 
2576 	err = dpbp_enable(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2577 	if (err) {
2578 		dev_err(dev, "dpbp_enable() failed\n");
2579 		goto err_enable;
2580 	}
2581 
2582 	err = dpbp_get_attributes(ethsw->mc_io, 0, dpbp_dev->mc_handle,
2583 				  &dpbp_attrs);
2584 	if (err) {
2585 		dev_err(dev, "dpbp_get_attributes() failed\n");
2586 		goto err_get_attr;
2587 	}
2588 
2589 	dpsw_ctrl_if_pools_cfg.num_dpbp = 1;
2590 	dpsw_ctrl_if_pools_cfg.pools[0].dpbp_id = dpbp_attrs.id;
2591 	dpsw_ctrl_if_pools_cfg.pools[0].buffer_size = DPAA2_SWITCH_RX_BUF_SIZE;
2592 	dpsw_ctrl_if_pools_cfg.pools[0].backup_pool = 0;
2593 
2594 	err = dpsw_ctrl_if_set_pools(ethsw->mc_io, 0, ethsw->dpsw_handle,
2595 				     &dpsw_ctrl_if_pools_cfg);
2596 	if (err) {
2597 		dev_err(dev, "dpsw_ctrl_if_set_pools() failed\n");
2598 		goto err_get_attr;
2599 	}
2600 	ethsw->bpid = dpbp_attrs.id;
2601 
2602 	return 0;
2603 
2604 err_get_attr:
2605 	dpbp_disable(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2606 err_enable:
2607 err_reset:
2608 	dpbp_close(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2609 err_open:
2610 	fsl_mc_object_free(dpbp_dev);
2611 	return err;
2612 }
2613 
2614 static void dpaa2_switch_free_dpbp(struct ethsw_core *ethsw)
2615 {
2616 	dpbp_disable(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle);
2617 	dpbp_close(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle);
2618 	fsl_mc_object_free(ethsw->dpbp_dev);
2619 }
2620 
2621 static int dpaa2_switch_alloc_rings(struct ethsw_core *ethsw)
2622 {
2623 	int i;
2624 
2625 	for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) {
2626 		ethsw->fq[i].store =
2627 			dpaa2_io_store_create(DPAA2_SWITCH_STORE_SIZE,
2628 					      ethsw->dev);
2629 		if (!ethsw->fq[i].store) {
2630 			dev_err(ethsw->dev, "dpaa2_io_store_create failed\n");
2631 			while (--i >= 0)
2632 				dpaa2_io_store_destroy(ethsw->fq[i].store);
2633 			return -ENOMEM;
2634 		}
2635 	}
2636 
2637 	return 0;
2638 }
2639 
2640 static void dpaa2_switch_destroy_rings(struct ethsw_core *ethsw)
2641 {
2642 	int i;
2643 
2644 	for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
2645 		dpaa2_io_store_destroy(ethsw->fq[i].store);
2646 }
2647 
2648 static int dpaa2_switch_pull_fq(struct dpaa2_switch_fq *fq)
2649 {
2650 	int err, retries = 0;
2651 
2652 	/* Try to pull from the FQ while the portal is busy and we didn't hit
2653 	 * the maximum number fo retries
2654 	 */
2655 	do {
2656 		err = dpaa2_io_service_pull_fq(NULL, fq->fqid, fq->store);
2657 		cpu_relax();
2658 	} while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES);
2659 
2660 	if (unlikely(err))
2661 		dev_err(fq->ethsw->dev, "dpaa2_io_service_pull err %d", err);
2662 
2663 	return err;
2664 }
2665 
2666 /* Consume all frames pull-dequeued into the store */
2667 static int dpaa2_switch_store_consume(struct dpaa2_switch_fq *fq)
2668 {
2669 	struct ethsw_core *ethsw = fq->ethsw;
2670 	int cleaned = 0, is_last;
2671 	struct dpaa2_dq *dq;
2672 	int retries = 0;
2673 
2674 	do {
2675 		/* Get the next available FD from the store */
2676 		dq = dpaa2_io_store_next(fq->store, &is_last);
2677 		if (unlikely(!dq)) {
2678 			if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) {
2679 				dev_err_once(ethsw->dev,
2680 					     "No valid dequeue response\n");
2681 				return -ETIMEDOUT;
2682 			}
2683 			continue;
2684 		}
2685 
2686 		if (fq->type == DPSW_QUEUE_RX)
2687 			dpaa2_switch_rx(fq, dpaa2_dq_fd(dq));
2688 		else
2689 			dpaa2_switch_tx_conf(fq, dpaa2_dq_fd(dq));
2690 		cleaned++;
2691 
2692 	} while (!is_last);
2693 
2694 	return cleaned;
2695 }
2696 
2697 /* NAPI poll routine */
2698 static int dpaa2_switch_poll(struct napi_struct *napi, int budget)
2699 {
2700 	int err, cleaned = 0, store_cleaned, work_done;
2701 	struct dpaa2_switch_fq *fq;
2702 	int retries = 0;
2703 
2704 	fq = container_of(napi, struct dpaa2_switch_fq, napi);
2705 
2706 	do {
2707 		err = dpaa2_switch_pull_fq(fq);
2708 		if (unlikely(err))
2709 			break;
2710 
2711 		/* Refill pool if appropriate */
2712 		dpaa2_switch_refill_bp(fq->ethsw);
2713 
2714 		store_cleaned = dpaa2_switch_store_consume(fq);
2715 		cleaned += store_cleaned;
2716 
2717 		if (cleaned >= budget) {
2718 			work_done = budget;
2719 			goto out;
2720 		}
2721 
2722 	} while (store_cleaned);
2723 
2724 	/* We didn't consume the entire budget, so finish napi and re-enable
2725 	 * data availability notifications
2726 	 */
2727 	napi_complete_done(napi, cleaned);
2728 	do {
2729 		err = dpaa2_io_service_rearm(NULL, &fq->nctx);
2730 		cpu_relax();
2731 	} while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES);
2732 
2733 	work_done = max(cleaned, 1);
2734 out:
2735 
2736 	return work_done;
2737 }
2738 
2739 static void dpaa2_switch_fqdan_cb(struct dpaa2_io_notification_ctx *nctx)
2740 {
2741 	struct dpaa2_switch_fq *fq;
2742 
2743 	fq = container_of(nctx, struct dpaa2_switch_fq, nctx);
2744 
2745 	napi_schedule(&fq->napi);
2746 }
2747 
2748 static int dpaa2_switch_setup_dpio(struct ethsw_core *ethsw)
2749 {
2750 	struct dpsw_ctrl_if_queue_cfg queue_cfg;
2751 	struct dpaa2_io_notification_ctx *nctx;
2752 	int err, i, j;
2753 
2754 	for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) {
2755 		nctx = &ethsw->fq[i].nctx;
2756 
2757 		/* Register a new software context for the FQID.
2758 		 * By using NULL as the first parameter, we specify that we do
2759 		 * not care on which cpu are interrupts received for this queue
2760 		 */
2761 		nctx->is_cdan = 0;
2762 		nctx->id = ethsw->fq[i].fqid;
2763 		nctx->desired_cpu = DPAA2_IO_ANY_CPU;
2764 		nctx->cb = dpaa2_switch_fqdan_cb;
2765 		err = dpaa2_io_service_register(NULL, nctx, ethsw->dev);
2766 		if (err) {
2767 			err = -EPROBE_DEFER;
2768 			goto err_register;
2769 		}
2770 
2771 		queue_cfg.options = DPSW_CTRL_IF_QUEUE_OPT_DEST |
2772 				    DPSW_CTRL_IF_QUEUE_OPT_USER_CTX;
2773 		queue_cfg.dest_cfg.dest_type = DPSW_CTRL_IF_DEST_DPIO;
2774 		queue_cfg.dest_cfg.dest_id = nctx->dpio_id;
2775 		queue_cfg.dest_cfg.priority = 0;
2776 		queue_cfg.user_ctx = nctx->qman64;
2777 
2778 		err = dpsw_ctrl_if_set_queue(ethsw->mc_io, 0,
2779 					     ethsw->dpsw_handle,
2780 					     ethsw->fq[i].type,
2781 					     &queue_cfg);
2782 		if (err)
2783 			goto err_set_queue;
2784 	}
2785 
2786 	return 0;
2787 
2788 err_set_queue:
2789 	dpaa2_io_service_deregister(NULL, nctx, ethsw->dev);
2790 err_register:
2791 	for (j = 0; j < i; j++)
2792 		dpaa2_io_service_deregister(NULL, &ethsw->fq[j].nctx,
2793 					    ethsw->dev);
2794 
2795 	return err;
2796 }
2797 
2798 static void dpaa2_switch_free_dpio(struct ethsw_core *ethsw)
2799 {
2800 	int i;
2801 
2802 	for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
2803 		dpaa2_io_service_deregister(NULL, &ethsw->fq[i].nctx,
2804 					    ethsw->dev);
2805 }
2806 
2807 static int dpaa2_switch_ctrl_if_setup(struct ethsw_core *ethsw)
2808 {
2809 	int err;
2810 
2811 	/* setup FQs for Rx and Tx Conf */
2812 	err = dpaa2_switch_setup_fqs(ethsw);
2813 	if (err)
2814 		return err;
2815 
2816 	/* setup the buffer pool needed on the Rx path */
2817 	err = dpaa2_switch_setup_dpbp(ethsw);
2818 	if (err)
2819 		return err;
2820 
2821 	err = dpaa2_switch_alloc_rings(ethsw);
2822 	if (err)
2823 		goto err_free_dpbp;
2824 
2825 	err = dpaa2_switch_setup_dpio(ethsw);
2826 	if (err)
2827 		goto err_destroy_rings;
2828 
2829 	err = dpaa2_switch_seed_bp(ethsw);
2830 	if (err)
2831 		goto err_deregister_dpio;
2832 
2833 	err = dpsw_ctrl_if_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
2834 	if (err) {
2835 		dev_err(ethsw->dev, "dpsw_ctrl_if_enable err %d\n", err);
2836 		goto err_drain_dpbp;
2837 	}
2838 
2839 	return 0;
2840 
2841 err_drain_dpbp:
2842 	dpaa2_switch_drain_bp(ethsw);
2843 err_deregister_dpio:
2844 	dpaa2_switch_free_dpio(ethsw);
2845 err_destroy_rings:
2846 	dpaa2_switch_destroy_rings(ethsw);
2847 err_free_dpbp:
2848 	dpaa2_switch_free_dpbp(ethsw);
2849 
2850 	return err;
2851 }
2852 
2853 static int dpaa2_switch_init(struct fsl_mc_device *sw_dev)
2854 {
2855 	struct device *dev = &sw_dev->dev;
2856 	struct ethsw_core *ethsw = dev_get_drvdata(dev);
2857 	struct dpsw_vlan_if_cfg vcfg = {0};
2858 	struct dpsw_tci_cfg tci_cfg = {0};
2859 	struct dpsw_stp_cfg stp_cfg;
2860 	int err;
2861 	u16 i;
2862 
2863 	ethsw->dev_id = sw_dev->obj_desc.id;
2864 
2865 	err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, &ethsw->dpsw_handle);
2866 	if (err) {
2867 		dev_err(dev, "dpsw_open err %d\n", err);
2868 		return err;
2869 	}
2870 
2871 	err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
2872 				  &ethsw->sw_attr);
2873 	if (err) {
2874 		dev_err(dev, "dpsw_get_attributes err %d\n", err);
2875 		goto err_close;
2876 	}
2877 
2878 	err = dpsw_get_api_version(ethsw->mc_io, 0,
2879 				   &ethsw->major,
2880 				   &ethsw->minor);
2881 	if (err) {
2882 		dev_err(dev, "dpsw_get_api_version err %d\n", err);
2883 		goto err_close;
2884 	}
2885 
2886 	/* Minimum supported DPSW version check */
2887 	if (ethsw->major < DPSW_MIN_VER_MAJOR ||
2888 	    (ethsw->major == DPSW_MIN_VER_MAJOR &&
2889 	     ethsw->minor < DPSW_MIN_VER_MINOR)) {
2890 		dev_err(dev, "DPSW version %d:%d not supported. Use firmware 10.28.0 or greater.\n",
2891 			ethsw->major, ethsw->minor);
2892 		err = -EOPNOTSUPP;
2893 		goto err_close;
2894 	}
2895 
2896 	if (!dpaa2_switch_supports_cpu_traffic(ethsw)) {
2897 		err = -EOPNOTSUPP;
2898 		goto err_close;
2899 	}
2900 
2901 	dpaa2_switch_detect_features(ethsw);
2902 
2903 	err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
2904 	if (err) {
2905 		dev_err(dev, "dpsw_reset err %d\n", err);
2906 		goto err_close;
2907 	}
2908 
2909 	stp_cfg.vlan_id = DEFAULT_VLAN_ID;
2910 	stp_cfg.state = DPSW_STP_STATE_FORWARDING;
2911 
2912 	for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
2913 		err = dpsw_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle, i);
2914 		if (err) {
2915 			dev_err(dev, "dpsw_if_disable err %d\n", err);
2916 			goto err_close;
2917 		}
2918 
2919 		err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
2920 				      &stp_cfg);
2921 		if (err) {
2922 			dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
2923 				err, i);
2924 			goto err_close;
2925 		}
2926 
2927 		/* Switch starts with all ports configured to VLAN 1. Need to
2928 		 * remove this setting to allow configuration at bridge join
2929 		 */
2930 		vcfg.num_ifs = 1;
2931 		vcfg.if_id[0] = i;
2932 		err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
2933 						   DEFAULT_VLAN_ID, &vcfg);
2934 		if (err) {
2935 			dev_err(dev, "dpsw_vlan_remove_if_untagged err %d\n",
2936 				err);
2937 			goto err_close;
2938 		}
2939 
2940 		tci_cfg.vlan_id = 4095;
2941 		err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, i, &tci_cfg);
2942 		if (err) {
2943 			dev_err(dev, "dpsw_if_set_tci err %d\n", err);
2944 			goto err_close;
2945 		}
2946 
2947 		err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
2948 					  DEFAULT_VLAN_ID, &vcfg);
2949 		if (err) {
2950 			dev_err(dev, "dpsw_vlan_remove_if err %d\n", err);
2951 			goto err_close;
2952 		}
2953 	}
2954 
2955 	err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, DEFAULT_VLAN_ID);
2956 	if (err) {
2957 		dev_err(dev, "dpsw_vlan_remove err %d\n", err);
2958 		goto err_close;
2959 	}
2960 
2961 	ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered",
2962 						   WQ_MEM_RECLAIM, "ethsw",
2963 						   ethsw->sw_attr.id);
2964 	if (!ethsw->workqueue) {
2965 		err = -ENOMEM;
2966 		goto err_close;
2967 	}
2968 
2969 	err = dpsw_fdb_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, 0);
2970 	if (err)
2971 		goto err_destroy_ordered_workqueue;
2972 
2973 	err = dpaa2_switch_ctrl_if_setup(ethsw);
2974 	if (err)
2975 		goto err_destroy_ordered_workqueue;
2976 
2977 	return 0;
2978 
2979 err_destroy_ordered_workqueue:
2980 	destroy_workqueue(ethsw->workqueue);
2981 
2982 err_close:
2983 	dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
2984 	return err;
2985 }
2986 
2987 /* Add an ACL to redirect frames with specific destination MAC address to
2988  * control interface
2989  */
2990 static int dpaa2_switch_port_trap_mac_addr(struct ethsw_port_priv *port_priv,
2991 					   const char *mac)
2992 {
2993 	struct dpaa2_switch_acl_entry acl_entry = {0};
2994 
2995 	/* Match on the destination MAC address */
2996 	ether_addr_copy(acl_entry.key.match.l2_dest_mac, mac);
2997 	eth_broadcast_addr(acl_entry.key.mask.l2_dest_mac);
2998 
2999 	/* Trap to CPU */
3000 	acl_entry.cfg.precedence = 0;
3001 	acl_entry.cfg.result.action = DPSW_ACL_ACTION_REDIRECT_TO_CTRL_IF;
3002 
3003 	return dpaa2_switch_acl_entry_add(port_priv->filter_block, &acl_entry);
3004 }
3005 
3006 static int dpaa2_switch_port_init(struct ethsw_port_priv *port_priv, u16 port)
3007 {
3008 	const char stpa[ETH_ALEN] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
3009 	struct switchdev_obj_port_vlan vlan = {
3010 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
3011 		.vid = DEFAULT_VLAN_ID,
3012 		.flags = BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID,
3013 	};
3014 	struct net_device *netdev = port_priv->netdev;
3015 	struct ethsw_core *ethsw = port_priv->ethsw_data;
3016 	struct dpaa2_switch_filter_block *filter_block;
3017 	struct dpsw_fdb_cfg fdb_cfg = {0};
3018 	struct dpsw_if_attr dpsw_if_attr;
3019 	struct dpaa2_switch_fdb *fdb;
3020 	struct dpsw_acl_cfg acl_cfg;
3021 	u16 fdb_id, acl_tbl_id;
3022 	int err;
3023 
3024 	/* Get the Tx queue for this specific port */
3025 	err = dpsw_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
3026 				     port_priv->idx, &dpsw_if_attr);
3027 	if (err) {
3028 		netdev_err(netdev, "dpsw_if_get_attributes err %d\n", err);
3029 		return err;
3030 	}
3031 	port_priv->tx_qdid = dpsw_if_attr.qdid;
3032 
3033 	/* Create a FDB table for this particular switch port */
3034 	fdb_cfg.num_fdb_entries = ethsw->sw_attr.max_fdb_entries / ethsw->sw_attr.num_ifs;
3035 	err = dpsw_fdb_add(ethsw->mc_io, 0, ethsw->dpsw_handle,
3036 			   &fdb_id, &fdb_cfg);
3037 	if (err) {
3038 		netdev_err(netdev, "dpsw_fdb_add err %d\n", err);
3039 		return err;
3040 	}
3041 
3042 	/* Find an unused dpaa2_switch_fdb structure and use it */
3043 	fdb = dpaa2_switch_fdb_get_unused(ethsw);
3044 	fdb->fdb_id = fdb_id;
3045 	fdb->in_use = true;
3046 	fdb->bridge_dev = NULL;
3047 	port_priv->fdb = fdb;
3048 
3049 	/* We need to add VLAN 1 as the PVID on this port until it is under a
3050 	 * bridge since the DPAA2 switch is not able to handle the traffic in a
3051 	 * VLAN unaware fashion
3052 	 */
3053 	err = dpaa2_switch_port_vlans_add(netdev, &vlan);
3054 	if (err)
3055 		return err;
3056 
3057 	/* Setup the egress flooding domains (broadcast, unknown unicast */
3058 	err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
3059 	if (err)
3060 		return err;
3061 
3062 	/* Create an ACL table to be used by this switch port */
3063 	acl_cfg.max_entries = DPAA2_ETHSW_PORT_MAX_ACL_ENTRIES;
3064 	err = dpsw_acl_add(ethsw->mc_io, 0, ethsw->dpsw_handle,
3065 			   &acl_tbl_id, &acl_cfg);
3066 	if (err) {
3067 		netdev_err(netdev, "dpsw_acl_add err %d\n", err);
3068 		return err;
3069 	}
3070 
3071 	filter_block = dpaa2_switch_filter_block_get_unused(ethsw);
3072 	filter_block->ethsw = ethsw;
3073 	filter_block->acl_id = acl_tbl_id;
3074 	filter_block->in_use = true;
3075 	filter_block->num_acl_rules = 0;
3076 	INIT_LIST_HEAD(&filter_block->acl_entries);
3077 	INIT_LIST_HEAD(&filter_block->mirror_entries);
3078 
3079 	err = dpaa2_switch_port_acl_tbl_bind(port_priv, filter_block);
3080 	if (err)
3081 		return err;
3082 
3083 	err = dpaa2_switch_port_trap_mac_addr(port_priv, stpa);
3084 	if (err)
3085 		return err;
3086 
3087 	return err;
3088 }
3089 
3090 static void dpaa2_switch_takedown(struct fsl_mc_device *sw_dev)
3091 {
3092 	struct device *dev = &sw_dev->dev;
3093 	struct ethsw_core *ethsw = dev_get_drvdata(dev);
3094 	int err;
3095 
3096 	err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
3097 	if (err)
3098 		dev_warn(dev, "dpsw_close err %d\n", err);
3099 }
3100 
3101 static void dpaa2_switch_ctrl_if_teardown(struct ethsw_core *ethsw)
3102 {
3103 	dpsw_ctrl_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
3104 	dpaa2_switch_free_dpio(ethsw);
3105 	dpaa2_switch_destroy_rings(ethsw);
3106 	dpaa2_switch_drain_bp(ethsw);
3107 	dpaa2_switch_free_dpbp(ethsw);
3108 }
3109 
3110 static int dpaa2_switch_remove(struct fsl_mc_device *sw_dev)
3111 {
3112 	struct ethsw_port_priv *port_priv;
3113 	struct ethsw_core *ethsw;
3114 	struct device *dev;
3115 	int i;
3116 
3117 	dev = &sw_dev->dev;
3118 	ethsw = dev_get_drvdata(dev);
3119 
3120 	dpaa2_switch_ctrl_if_teardown(ethsw);
3121 
3122 	dpaa2_switch_teardown_irqs(sw_dev);
3123 
3124 	dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
3125 
3126 	for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
3127 		port_priv = ethsw->ports[i];
3128 		unregister_netdev(port_priv->netdev);
3129 		free_netdev(port_priv->netdev);
3130 	}
3131 
3132 	kfree(ethsw->fdbs);
3133 	kfree(ethsw->filter_blocks);
3134 	kfree(ethsw->ports);
3135 
3136 	dpaa2_switch_takedown(sw_dev);
3137 
3138 	destroy_workqueue(ethsw->workqueue);
3139 
3140 	fsl_mc_portal_free(ethsw->mc_io);
3141 
3142 	kfree(ethsw);
3143 
3144 	dev_set_drvdata(dev, NULL);
3145 
3146 	return 0;
3147 }
3148 
3149 static int dpaa2_switch_probe_port(struct ethsw_core *ethsw,
3150 				   u16 port_idx)
3151 {
3152 	struct ethsw_port_priv *port_priv;
3153 	struct device *dev = ethsw->dev;
3154 	struct net_device *port_netdev;
3155 	int err;
3156 
3157 	port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
3158 	if (!port_netdev) {
3159 		dev_err(dev, "alloc_etherdev error\n");
3160 		return -ENOMEM;
3161 	}
3162 
3163 	port_priv = netdev_priv(port_netdev);
3164 	port_priv->netdev = port_netdev;
3165 	port_priv->ethsw_data = ethsw;
3166 
3167 	port_priv->idx = port_idx;
3168 	port_priv->stp_state = BR_STATE_FORWARDING;
3169 
3170 	SET_NETDEV_DEV(port_netdev, dev);
3171 	port_netdev->netdev_ops = &dpaa2_switch_port_ops;
3172 	port_netdev->ethtool_ops = &dpaa2_switch_port_ethtool_ops;
3173 
3174 	port_netdev->needed_headroom = DPAA2_SWITCH_NEEDED_HEADROOM;
3175 
3176 	port_priv->bcast_flood = true;
3177 	port_priv->ucast_flood = true;
3178 
3179 	/* Set MTU limits */
3180 	port_netdev->min_mtu = ETH_MIN_MTU;
3181 	port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
3182 
3183 	/* Populate the private port structure so that later calls to
3184 	 * dpaa2_switch_port_init() can use it.
3185 	 */
3186 	ethsw->ports[port_idx] = port_priv;
3187 
3188 	/* The DPAA2 switch's ingress path depends on the VLAN table,
3189 	 * thus we are not able to disable VLAN filtering.
3190 	 */
3191 	port_netdev->features = NETIF_F_HW_VLAN_CTAG_FILTER |
3192 				NETIF_F_HW_VLAN_STAG_FILTER |
3193 				NETIF_F_HW_TC;
3194 
3195 	err = dpaa2_switch_port_init(port_priv, port_idx);
3196 	if (err)
3197 		goto err_port_probe;
3198 
3199 	err = dpaa2_switch_port_set_mac_addr(port_priv);
3200 	if (err)
3201 		goto err_port_probe;
3202 
3203 	err = dpaa2_switch_port_set_learning(port_priv, false);
3204 	if (err)
3205 		goto err_port_probe;
3206 	port_priv->learn_ena = false;
3207 
3208 	return 0;
3209 
3210 err_port_probe:
3211 	free_netdev(port_netdev);
3212 	ethsw->ports[port_idx] = NULL;
3213 
3214 	return err;
3215 }
3216 
3217 static int dpaa2_switch_probe(struct fsl_mc_device *sw_dev)
3218 {
3219 	struct device *dev = &sw_dev->dev;
3220 	struct ethsw_core *ethsw;
3221 	int i, err;
3222 
3223 	/* Allocate switch core*/
3224 	ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
3225 
3226 	if (!ethsw)
3227 		return -ENOMEM;
3228 
3229 	ethsw->dev = dev;
3230 	ethsw->iommu_domain = iommu_get_domain_for_dev(dev);
3231 	dev_set_drvdata(dev, ethsw);
3232 
3233 	err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3234 				     &ethsw->mc_io);
3235 	if (err) {
3236 		if (err == -ENXIO)
3237 			err = -EPROBE_DEFER;
3238 		else
3239 			dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
3240 		goto err_free_drvdata;
3241 	}
3242 
3243 	err = dpaa2_switch_init(sw_dev);
3244 	if (err)
3245 		goto err_free_cmdport;
3246 
3247 	ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
3248 			       GFP_KERNEL);
3249 	if (!(ethsw->ports)) {
3250 		err = -ENOMEM;
3251 		goto err_takedown;
3252 	}
3253 
3254 	ethsw->fdbs = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->fdbs),
3255 			      GFP_KERNEL);
3256 	if (!ethsw->fdbs) {
3257 		err = -ENOMEM;
3258 		goto err_free_ports;
3259 	}
3260 
3261 	ethsw->filter_blocks = kcalloc(ethsw->sw_attr.num_ifs,
3262 				       sizeof(*ethsw->filter_blocks),
3263 				       GFP_KERNEL);
3264 	if (!ethsw->filter_blocks) {
3265 		err = -ENOMEM;
3266 		goto err_free_fdbs;
3267 	}
3268 
3269 	for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
3270 		err = dpaa2_switch_probe_port(ethsw, i);
3271 		if (err)
3272 			goto err_free_netdev;
3273 	}
3274 
3275 	/* Add a NAPI instance for each of the Rx queues. The first port's
3276 	 * net_device will be associated with the instances since we do not have
3277 	 * different queues for each switch ports.
3278 	 */
3279 	for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
3280 		netif_napi_add(ethsw->ports[0]->netdev,
3281 			       &ethsw->fq[i].napi, dpaa2_switch_poll,
3282 			       NAPI_POLL_WEIGHT);
3283 
3284 	err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
3285 	if (err) {
3286 		dev_err(ethsw->dev, "dpsw_enable err %d\n", err);
3287 		goto err_free_netdev;
3288 	}
3289 
3290 	/* Setup IRQs */
3291 	err = dpaa2_switch_setup_irqs(sw_dev);
3292 	if (err)
3293 		goto err_stop;
3294 
3295 	/* By convention, if the mirror port is equal to the number of switch
3296 	 * interfaces, then mirroring of any kind is disabled.
3297 	 */
3298 	ethsw->mirror_port =  ethsw->sw_attr.num_ifs;
3299 
3300 	/* Register the netdev only when the entire setup is done and the
3301 	 * switch port interfaces are ready to receive traffic
3302 	 */
3303 	for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
3304 		err = register_netdev(ethsw->ports[i]->netdev);
3305 		if (err < 0) {
3306 			dev_err(dev, "register_netdev error %d\n", err);
3307 			goto err_unregister_ports;
3308 		}
3309 	}
3310 
3311 	return 0;
3312 
3313 err_unregister_ports:
3314 	for (i--; i >= 0; i--)
3315 		unregister_netdev(ethsw->ports[i]->netdev);
3316 	dpaa2_switch_teardown_irqs(sw_dev);
3317 err_stop:
3318 	dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
3319 err_free_netdev:
3320 	for (i--; i >= 0; i--)
3321 		free_netdev(ethsw->ports[i]->netdev);
3322 	kfree(ethsw->filter_blocks);
3323 err_free_fdbs:
3324 	kfree(ethsw->fdbs);
3325 err_free_ports:
3326 	kfree(ethsw->ports);
3327 
3328 err_takedown:
3329 	dpaa2_switch_takedown(sw_dev);
3330 
3331 err_free_cmdport:
3332 	fsl_mc_portal_free(ethsw->mc_io);
3333 
3334 err_free_drvdata:
3335 	kfree(ethsw);
3336 	dev_set_drvdata(dev, NULL);
3337 
3338 	return err;
3339 }
3340 
3341 static const struct fsl_mc_device_id dpaa2_switch_match_id_table[] = {
3342 	{
3343 		.vendor = FSL_MC_VENDOR_FREESCALE,
3344 		.obj_type = "dpsw",
3345 	},
3346 	{ .vendor = 0x0 }
3347 };
3348 MODULE_DEVICE_TABLE(fslmc, dpaa2_switch_match_id_table);
3349 
3350 static struct fsl_mc_driver dpaa2_switch_drv = {
3351 	.driver = {
3352 		.name = KBUILD_MODNAME,
3353 		.owner = THIS_MODULE,
3354 	},
3355 	.probe = dpaa2_switch_probe,
3356 	.remove = dpaa2_switch_remove,
3357 	.match_id_table = dpaa2_switch_match_id_table
3358 };
3359 
3360 static struct notifier_block dpaa2_switch_port_nb __read_mostly = {
3361 	.notifier_call = dpaa2_switch_port_netdevice_event,
3362 };
3363 
3364 static struct notifier_block dpaa2_switch_port_switchdev_nb = {
3365 	.notifier_call = dpaa2_switch_port_event,
3366 };
3367 
3368 static struct notifier_block dpaa2_switch_port_switchdev_blocking_nb = {
3369 	.notifier_call = dpaa2_switch_port_blocking_event,
3370 };
3371 
3372 static int dpaa2_switch_register_notifiers(void)
3373 {
3374 	int err;
3375 
3376 	err = register_netdevice_notifier(&dpaa2_switch_port_nb);
3377 	if (err) {
3378 		pr_err("dpaa2-switch: failed to register net_device notifier (%d)\n", err);
3379 		return err;
3380 	}
3381 
3382 	err = register_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
3383 	if (err) {
3384 		pr_err("dpaa2-switch: failed to register switchdev notifier (%d)\n", err);
3385 		goto err_switchdev_nb;
3386 	}
3387 
3388 	err = register_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb);
3389 	if (err) {
3390 		pr_err("dpaa2-switch: failed to register switchdev blocking notifier (%d)\n", err);
3391 		goto err_switchdev_blocking_nb;
3392 	}
3393 
3394 	return 0;
3395 
3396 err_switchdev_blocking_nb:
3397 	unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
3398 err_switchdev_nb:
3399 	unregister_netdevice_notifier(&dpaa2_switch_port_nb);
3400 
3401 	return err;
3402 }
3403 
3404 static void dpaa2_switch_unregister_notifiers(void)
3405 {
3406 	int err;
3407 
3408 	err = unregister_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb);
3409 	if (err)
3410 		pr_err("dpaa2-switch: failed to unregister switchdev blocking notifier (%d)\n",
3411 		       err);
3412 
3413 	err = unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
3414 	if (err)
3415 		pr_err("dpaa2-switch: failed to unregister switchdev notifier (%d)\n", err);
3416 
3417 	err = unregister_netdevice_notifier(&dpaa2_switch_port_nb);
3418 	if (err)
3419 		pr_err("dpaa2-switch: failed to unregister net_device notifier (%d)\n", err);
3420 }
3421 
3422 static int __init dpaa2_switch_driver_init(void)
3423 {
3424 	int err;
3425 
3426 	err = fsl_mc_driver_register(&dpaa2_switch_drv);
3427 	if (err)
3428 		return err;
3429 
3430 	err = dpaa2_switch_register_notifiers();
3431 	if (err) {
3432 		fsl_mc_driver_unregister(&dpaa2_switch_drv);
3433 		return err;
3434 	}
3435 
3436 	return 0;
3437 }
3438 
3439 static void __exit dpaa2_switch_driver_exit(void)
3440 {
3441 	dpaa2_switch_unregister_notifiers();
3442 	fsl_mc_driver_unregister(&dpaa2_switch_drv);
3443 }
3444 
3445 module_init(dpaa2_switch_driver_init);
3446 module_exit(dpaa2_switch_driver_exit);
3447 
3448 MODULE_LICENSE("GPL v2");
3449 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");
3450