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