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