1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/dsa/user.c - user device handling
4  * Copyright (c) 2008-2009 Marvell Semiconductor
5  */
6 
7 #include <linux/list.h>
8 #include <linux/etherdevice.h>
9 #include <linux/netdevice.h>
10 #include <linux/phy.h>
11 #include <linux/phy_fixed.h>
12 #include <linux/phylink.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
15 #include <linux/mdio.h>
16 #include <net/rtnetlink.h>
17 #include <net/pkt_cls.h>
18 #include <net/selftests.h>
19 #include <net/tc_act/tc_mirred.h>
20 #include <linux/if_bridge.h>
21 #include <linux/if_hsr.h>
22 #include <net/dcbnl.h>
23 #include <linux/netpoll.h>
24 #include <linux/string.h>
25 
26 #include "conduit.h"
27 #include "dsa.h"
28 #include "netlink.h"
29 #include "port.h"
30 #include "switch.h"
31 #include "tag.h"
32 #include "user.h"
33 
34 struct dsa_switchdev_event_work {
35 	struct net_device *dev;
36 	struct net_device *orig_dev;
37 	struct work_struct work;
38 	unsigned long event;
39 	/* Specific for SWITCHDEV_FDB_ADD_TO_DEVICE and
40 	 * SWITCHDEV_FDB_DEL_TO_DEVICE
41 	 */
42 	unsigned char addr[ETH_ALEN];
43 	u16 vid;
44 	bool host_addr;
45 };
46 
47 enum dsa_standalone_event {
48 	DSA_UC_ADD,
49 	DSA_UC_DEL,
50 	DSA_MC_ADD,
51 	DSA_MC_DEL,
52 };
53 
54 struct dsa_standalone_event_work {
55 	struct work_struct work;
56 	struct net_device *dev;
57 	enum dsa_standalone_event event;
58 	unsigned char addr[ETH_ALEN];
59 	u16 vid;
60 };
61 
62 struct dsa_host_vlan_rx_filtering_ctx {
63 	struct net_device *dev;
64 	const unsigned char *addr;
65 	enum dsa_standalone_event event;
66 };
67 
dsa_switch_supports_uc_filtering(struct dsa_switch * ds)68 static bool dsa_switch_supports_uc_filtering(struct dsa_switch *ds)
69 {
70 	return ds->ops->port_fdb_add && ds->ops->port_fdb_del &&
71 	       ds->fdb_isolation && !ds->vlan_filtering_is_global &&
72 	       !ds->needs_standalone_vlan_filtering;
73 }
74 
dsa_switch_supports_mc_filtering(struct dsa_switch * ds)75 static bool dsa_switch_supports_mc_filtering(struct dsa_switch *ds)
76 {
77 	return ds->ops->port_mdb_add && ds->ops->port_mdb_del &&
78 	       ds->fdb_isolation && !ds->vlan_filtering_is_global &&
79 	       !ds->needs_standalone_vlan_filtering;
80 }
81 
dsa_user_standalone_event_work(struct work_struct * work)82 static void dsa_user_standalone_event_work(struct work_struct *work)
83 {
84 	struct dsa_standalone_event_work *standalone_work =
85 		container_of(work, struct dsa_standalone_event_work, work);
86 	const unsigned char *addr = standalone_work->addr;
87 	struct net_device *dev = standalone_work->dev;
88 	struct dsa_port *dp = dsa_user_to_port(dev);
89 	struct switchdev_obj_port_mdb mdb;
90 	struct dsa_switch *ds = dp->ds;
91 	u16 vid = standalone_work->vid;
92 	int err;
93 
94 	switch (standalone_work->event) {
95 	case DSA_UC_ADD:
96 		err = dsa_port_standalone_host_fdb_add(dp, addr, vid);
97 		if (err) {
98 			dev_err(ds->dev,
99 				"port %d failed to add %pM vid %d to fdb: %d\n",
100 				dp->index, addr, vid, err);
101 			break;
102 		}
103 		break;
104 
105 	case DSA_UC_DEL:
106 		err = dsa_port_standalone_host_fdb_del(dp, addr, vid);
107 		if (err) {
108 			dev_err(ds->dev,
109 				"port %d failed to delete %pM vid %d from fdb: %d\n",
110 				dp->index, addr, vid, err);
111 		}
112 
113 		break;
114 	case DSA_MC_ADD:
115 		ether_addr_copy(mdb.addr, addr);
116 		mdb.vid = vid;
117 
118 		err = dsa_port_standalone_host_mdb_add(dp, &mdb);
119 		if (err) {
120 			dev_err(ds->dev,
121 				"port %d failed to add %pM vid %d to mdb: %d\n",
122 				dp->index, addr, vid, err);
123 			break;
124 		}
125 		break;
126 	case DSA_MC_DEL:
127 		ether_addr_copy(mdb.addr, addr);
128 		mdb.vid = vid;
129 
130 		err = dsa_port_standalone_host_mdb_del(dp, &mdb);
131 		if (err) {
132 			dev_err(ds->dev,
133 				"port %d failed to delete %pM vid %d from mdb: %d\n",
134 				dp->index, addr, vid, err);
135 		}
136 
137 		break;
138 	}
139 
140 	kfree(standalone_work);
141 }
142 
dsa_user_schedule_standalone_work(struct net_device * dev,enum dsa_standalone_event event,const unsigned char * addr,u16 vid)143 static int dsa_user_schedule_standalone_work(struct net_device *dev,
144 					     enum dsa_standalone_event event,
145 					     const unsigned char *addr,
146 					     u16 vid)
147 {
148 	struct dsa_standalone_event_work *standalone_work;
149 
150 	standalone_work = kzalloc(sizeof(*standalone_work), GFP_ATOMIC);
151 	if (!standalone_work)
152 		return -ENOMEM;
153 
154 	INIT_WORK(&standalone_work->work, dsa_user_standalone_event_work);
155 	standalone_work->event = event;
156 	standalone_work->dev = dev;
157 
158 	ether_addr_copy(standalone_work->addr, addr);
159 	standalone_work->vid = vid;
160 
161 	dsa_schedule_work(&standalone_work->work);
162 
163 	return 0;
164 }
165 
dsa_user_host_vlan_rx_filtering(void * arg,int vid)166 static int dsa_user_host_vlan_rx_filtering(void *arg, int vid)
167 {
168 	struct dsa_host_vlan_rx_filtering_ctx *ctx = arg;
169 
170 	return dsa_user_schedule_standalone_work(ctx->dev, ctx->event,
171 						  ctx->addr, vid);
172 }
173 
dsa_user_vlan_for_each(struct net_device * dev,int (* cb)(void * arg,int vid),void * arg)174 static int dsa_user_vlan_for_each(struct net_device *dev,
175 				  int (*cb)(void *arg, int vid), void *arg)
176 {
177 	struct dsa_port *dp = dsa_user_to_port(dev);
178 	struct dsa_vlan *v;
179 	int err;
180 
181 	lockdep_assert_held(&dev->addr_list_lock);
182 
183 	err = cb(arg, 0);
184 	if (err)
185 		return err;
186 
187 	list_for_each_entry(v, &dp->user_vlans, list) {
188 		err = cb(arg, v->vid);
189 		if (err)
190 			return err;
191 	}
192 
193 	return 0;
194 }
195 
dsa_user_sync_uc(struct net_device * dev,const unsigned char * addr)196 static int dsa_user_sync_uc(struct net_device *dev,
197 			    const unsigned char *addr)
198 {
199 	struct net_device *conduit = dsa_user_to_conduit(dev);
200 	struct dsa_port *dp = dsa_user_to_port(dev);
201 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
202 		.dev = dev,
203 		.addr = addr,
204 		.event = DSA_UC_ADD,
205 	};
206 
207 	dev_uc_add(conduit, addr);
208 
209 	if (!dsa_switch_supports_uc_filtering(dp->ds))
210 		return 0;
211 
212 	return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
213 				      &ctx);
214 }
215 
dsa_user_unsync_uc(struct net_device * dev,const unsigned char * addr)216 static int dsa_user_unsync_uc(struct net_device *dev,
217 			      const unsigned char *addr)
218 {
219 	struct net_device *conduit = dsa_user_to_conduit(dev);
220 	struct dsa_port *dp = dsa_user_to_port(dev);
221 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
222 		.dev = dev,
223 		.addr = addr,
224 		.event = DSA_UC_DEL,
225 	};
226 
227 	dev_uc_del(conduit, addr);
228 
229 	if (!dsa_switch_supports_uc_filtering(dp->ds))
230 		return 0;
231 
232 	return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
233 				      &ctx);
234 }
235 
dsa_user_sync_mc(struct net_device * dev,const unsigned char * addr)236 static int dsa_user_sync_mc(struct net_device *dev,
237 			    const unsigned char *addr)
238 {
239 	struct net_device *conduit = dsa_user_to_conduit(dev);
240 	struct dsa_port *dp = dsa_user_to_port(dev);
241 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
242 		.dev = dev,
243 		.addr = addr,
244 		.event = DSA_MC_ADD,
245 	};
246 
247 	dev_mc_add(conduit, addr);
248 
249 	if (!dsa_switch_supports_mc_filtering(dp->ds))
250 		return 0;
251 
252 	return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
253 				      &ctx);
254 }
255 
dsa_user_unsync_mc(struct net_device * dev,const unsigned char * addr)256 static int dsa_user_unsync_mc(struct net_device *dev,
257 			      const unsigned char *addr)
258 {
259 	struct net_device *conduit = dsa_user_to_conduit(dev);
260 	struct dsa_port *dp = dsa_user_to_port(dev);
261 	struct dsa_host_vlan_rx_filtering_ctx ctx = {
262 		.dev = dev,
263 		.addr = addr,
264 		.event = DSA_MC_DEL,
265 	};
266 
267 	dev_mc_del(conduit, addr);
268 
269 	if (!dsa_switch_supports_mc_filtering(dp->ds))
270 		return 0;
271 
272 	return dsa_user_vlan_for_each(dev, dsa_user_host_vlan_rx_filtering,
273 				      &ctx);
274 }
275 
dsa_user_sync_ha(struct net_device * dev)276 void dsa_user_sync_ha(struct net_device *dev)
277 {
278 	struct dsa_port *dp = dsa_user_to_port(dev);
279 	struct dsa_switch *ds = dp->ds;
280 	struct netdev_hw_addr *ha;
281 
282 	netif_addr_lock_bh(dev);
283 
284 	netdev_for_each_synced_mc_addr(ha, dev)
285 		dsa_user_sync_mc(dev, ha->addr);
286 
287 	netdev_for_each_synced_uc_addr(ha, dev)
288 		dsa_user_sync_uc(dev, ha->addr);
289 
290 	netif_addr_unlock_bh(dev);
291 
292 	if (dsa_switch_supports_uc_filtering(ds) ||
293 	    dsa_switch_supports_mc_filtering(ds))
294 		dsa_flush_workqueue();
295 }
296 
dsa_user_unsync_ha(struct net_device * dev)297 void dsa_user_unsync_ha(struct net_device *dev)
298 {
299 	struct dsa_port *dp = dsa_user_to_port(dev);
300 	struct dsa_switch *ds = dp->ds;
301 	struct netdev_hw_addr *ha;
302 
303 	netif_addr_lock_bh(dev);
304 
305 	netdev_for_each_synced_uc_addr(ha, dev)
306 		dsa_user_unsync_uc(dev, ha->addr);
307 
308 	netdev_for_each_synced_mc_addr(ha, dev)
309 		dsa_user_unsync_mc(dev, ha->addr);
310 
311 	netif_addr_unlock_bh(dev);
312 
313 	if (dsa_switch_supports_uc_filtering(ds) ||
314 	    dsa_switch_supports_mc_filtering(ds))
315 		dsa_flush_workqueue();
316 }
317 
318 /* user mii_bus handling ***************************************************/
dsa_user_phy_read(struct mii_bus * bus,int addr,int reg)319 static int dsa_user_phy_read(struct mii_bus *bus, int addr, int reg)
320 {
321 	struct dsa_switch *ds = bus->priv;
322 
323 	if (ds->phys_mii_mask & (1 << addr))
324 		return ds->ops->phy_read(ds, addr, reg);
325 
326 	return 0xffff;
327 }
328 
dsa_user_phy_write(struct mii_bus * bus,int addr,int reg,u16 val)329 static int dsa_user_phy_write(struct mii_bus *bus, int addr, int reg, u16 val)
330 {
331 	struct dsa_switch *ds = bus->priv;
332 
333 	if (ds->phys_mii_mask & (1 << addr))
334 		return ds->ops->phy_write(ds, addr, reg, val);
335 
336 	return 0;
337 }
338 
dsa_user_mii_bus_init(struct dsa_switch * ds)339 void dsa_user_mii_bus_init(struct dsa_switch *ds)
340 {
341 	ds->user_mii_bus->priv = (void *)ds;
342 	ds->user_mii_bus->name = "dsa user smi";
343 	ds->user_mii_bus->read = dsa_user_phy_read;
344 	ds->user_mii_bus->write = dsa_user_phy_write;
345 	snprintf(ds->user_mii_bus->id, MII_BUS_ID_SIZE, "dsa-%d.%d",
346 		 ds->dst->index, ds->index);
347 	ds->user_mii_bus->parent = ds->dev;
348 	ds->user_mii_bus->phy_mask = ~ds->phys_mii_mask;
349 }
350 
351 
352 /* user device handling ****************************************************/
dsa_user_get_iflink(const struct net_device * dev)353 static int dsa_user_get_iflink(const struct net_device *dev)
354 {
355 	return READ_ONCE(dsa_user_to_conduit(dev)->ifindex);
356 }
357 
dsa_user_host_uc_install(struct net_device * dev,const u8 * addr)358 int dsa_user_host_uc_install(struct net_device *dev, const u8 *addr)
359 {
360 	struct net_device *conduit = dsa_user_to_conduit(dev);
361 	struct dsa_port *dp = dsa_user_to_port(dev);
362 	struct dsa_switch *ds = dp->ds;
363 	int err;
364 
365 	if (dsa_switch_supports_uc_filtering(ds)) {
366 		err = dsa_port_standalone_host_fdb_add(dp, addr, 0);
367 		if (err)
368 			goto out;
369 	}
370 
371 	if (!ether_addr_equal(addr, conduit->dev_addr)) {
372 		err = dev_uc_add(conduit, addr);
373 		if (err < 0)
374 			goto del_host_addr;
375 	}
376 
377 	return 0;
378 
379 del_host_addr:
380 	if (dsa_switch_supports_uc_filtering(ds))
381 		dsa_port_standalone_host_fdb_del(dp, addr, 0);
382 out:
383 	return err;
384 }
385 
dsa_user_host_uc_uninstall(struct net_device * dev)386 void dsa_user_host_uc_uninstall(struct net_device *dev)
387 {
388 	struct net_device *conduit = dsa_user_to_conduit(dev);
389 	struct dsa_port *dp = dsa_user_to_port(dev);
390 	struct dsa_switch *ds = dp->ds;
391 
392 	if (!ether_addr_equal(dev->dev_addr, conduit->dev_addr))
393 		dev_uc_del(conduit, dev->dev_addr);
394 
395 	if (dsa_switch_supports_uc_filtering(ds))
396 		dsa_port_standalone_host_fdb_del(dp, dev->dev_addr, 0);
397 }
398 
dsa_user_open(struct net_device * dev)399 static int dsa_user_open(struct net_device *dev)
400 {
401 	struct net_device *conduit = dsa_user_to_conduit(dev);
402 	struct dsa_port *dp = dsa_user_to_port(dev);
403 	int err;
404 
405 	err = dev_open(conduit, NULL);
406 	if (err < 0) {
407 		netdev_err(dev, "failed to open conduit %s\n", conduit->name);
408 		goto out;
409 	}
410 
411 	err = dsa_user_host_uc_install(dev, dev->dev_addr);
412 	if (err)
413 		goto out;
414 
415 	err = dsa_port_enable_rt(dp, dev->phydev);
416 	if (err)
417 		goto out_del_host_uc;
418 
419 	return 0;
420 
421 out_del_host_uc:
422 	dsa_user_host_uc_uninstall(dev);
423 out:
424 	return err;
425 }
426 
dsa_user_close(struct net_device * dev)427 static int dsa_user_close(struct net_device *dev)
428 {
429 	struct dsa_port *dp = dsa_user_to_port(dev);
430 
431 	dsa_port_disable_rt(dp);
432 
433 	dsa_user_host_uc_uninstall(dev);
434 
435 	return 0;
436 }
437 
dsa_user_manage_host_flood(struct net_device * dev)438 static void dsa_user_manage_host_flood(struct net_device *dev)
439 {
440 	bool mc = dev->flags & (IFF_PROMISC | IFF_ALLMULTI);
441 	struct dsa_port *dp = dsa_user_to_port(dev);
442 	bool uc = dev->flags & IFF_PROMISC;
443 
444 	dsa_port_set_host_flood(dp, uc, mc);
445 }
446 
dsa_user_change_rx_flags(struct net_device * dev,int change)447 static void dsa_user_change_rx_flags(struct net_device *dev, int change)
448 {
449 	struct net_device *conduit = dsa_user_to_conduit(dev);
450 	struct dsa_port *dp = dsa_user_to_port(dev);
451 	struct dsa_switch *ds = dp->ds;
452 
453 	if (change & IFF_ALLMULTI)
454 		dev_set_allmulti(conduit,
455 				 dev->flags & IFF_ALLMULTI ? 1 : -1);
456 	if (change & IFF_PROMISC)
457 		dev_set_promiscuity(conduit,
458 				    dev->flags & IFF_PROMISC ? 1 : -1);
459 
460 	if (dsa_switch_supports_uc_filtering(ds) &&
461 	    dsa_switch_supports_mc_filtering(ds))
462 		dsa_user_manage_host_flood(dev);
463 }
464 
dsa_user_set_rx_mode(struct net_device * dev)465 static void dsa_user_set_rx_mode(struct net_device *dev)
466 {
467 	__dev_mc_sync(dev, dsa_user_sync_mc, dsa_user_unsync_mc);
468 	__dev_uc_sync(dev, dsa_user_sync_uc, dsa_user_unsync_uc);
469 }
470 
dsa_user_set_mac_address(struct net_device * dev,void * a)471 static int dsa_user_set_mac_address(struct net_device *dev, void *a)
472 {
473 	struct dsa_port *dp = dsa_user_to_port(dev);
474 	struct dsa_switch *ds = dp->ds;
475 	struct sockaddr *addr = a;
476 	int err;
477 
478 	if (!is_valid_ether_addr(addr->sa_data))
479 		return -EADDRNOTAVAIL;
480 
481 	if (ds->ops->port_set_mac_address) {
482 		err = ds->ops->port_set_mac_address(ds, dp->index,
483 						    addr->sa_data);
484 		if (err)
485 			return err;
486 	}
487 
488 	/* If the port is down, the address isn't synced yet to hardware or
489 	 * to the DSA conduit, so there is nothing to change.
490 	 */
491 	if (!(dev->flags & IFF_UP))
492 		goto out_change_dev_addr;
493 
494 	err = dsa_user_host_uc_install(dev, addr->sa_data);
495 	if (err)
496 		return err;
497 
498 	dsa_user_host_uc_uninstall(dev);
499 
500 out_change_dev_addr:
501 	eth_hw_addr_set(dev, addr->sa_data);
502 
503 	return 0;
504 }
505 
506 struct dsa_user_dump_ctx {
507 	struct net_device *dev;
508 	struct sk_buff *skb;
509 	struct netlink_callback *cb;
510 	int idx;
511 };
512 
513 static int
dsa_user_port_fdb_do_dump(const unsigned char * addr,u16 vid,bool is_static,void * data)514 dsa_user_port_fdb_do_dump(const unsigned char *addr, u16 vid,
515 			  bool is_static, void *data)
516 {
517 	struct dsa_user_dump_ctx *dump = data;
518 	u32 portid = NETLINK_CB(dump->cb->skb).portid;
519 	u32 seq = dump->cb->nlh->nlmsg_seq;
520 	struct nlmsghdr *nlh;
521 	struct ndmsg *ndm;
522 
523 	if (dump->idx < dump->cb->args[2])
524 		goto skip;
525 
526 	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
527 			sizeof(*ndm), NLM_F_MULTI);
528 	if (!nlh)
529 		return -EMSGSIZE;
530 
531 	ndm = nlmsg_data(nlh);
532 	ndm->ndm_family  = AF_BRIDGE;
533 	ndm->ndm_pad1    = 0;
534 	ndm->ndm_pad2    = 0;
535 	ndm->ndm_flags   = NTF_SELF;
536 	ndm->ndm_type    = 0;
537 	ndm->ndm_ifindex = dump->dev->ifindex;
538 	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
539 
540 	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
541 		goto nla_put_failure;
542 
543 	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
544 		goto nla_put_failure;
545 
546 	nlmsg_end(dump->skb, nlh);
547 
548 skip:
549 	dump->idx++;
550 	return 0;
551 
552 nla_put_failure:
553 	nlmsg_cancel(dump->skb, nlh);
554 	return -EMSGSIZE;
555 }
556 
557 static int
dsa_user_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,struct net_device * filter_dev,int * idx)558 dsa_user_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
559 		  struct net_device *dev, struct net_device *filter_dev,
560 		  int *idx)
561 {
562 	struct dsa_port *dp = dsa_user_to_port(dev);
563 	struct dsa_user_dump_ctx dump = {
564 		.dev = dev,
565 		.skb = skb,
566 		.cb = cb,
567 		.idx = *idx,
568 	};
569 	int err;
570 
571 	err = dsa_port_fdb_dump(dp, dsa_user_port_fdb_do_dump, &dump);
572 	*idx = dump.idx;
573 
574 	return err;
575 }
576 
dsa_user_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)577 static int dsa_user_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
578 {
579 	struct dsa_user_priv *p = netdev_priv(dev);
580 	struct dsa_switch *ds = p->dp->ds;
581 	int port = p->dp->index;
582 
583 	/* Pass through to switch driver if it supports timestamping */
584 	switch (cmd) {
585 	case SIOCGHWTSTAMP:
586 		if (ds->ops->port_hwtstamp_get)
587 			return ds->ops->port_hwtstamp_get(ds, port, ifr);
588 		break;
589 	case SIOCSHWTSTAMP:
590 		if (ds->ops->port_hwtstamp_set)
591 			return ds->ops->port_hwtstamp_set(ds, port, ifr);
592 		break;
593 	}
594 
595 	return phylink_mii_ioctl(p->dp->pl, ifr, cmd);
596 }
597 
dsa_user_port_attr_set(struct net_device * dev,const void * ctx,const struct switchdev_attr * attr,struct netlink_ext_ack * extack)598 static int dsa_user_port_attr_set(struct net_device *dev, const void *ctx,
599 				  const struct switchdev_attr *attr,
600 				  struct netlink_ext_ack *extack)
601 {
602 	struct dsa_port *dp = dsa_user_to_port(dev);
603 	int ret;
604 
605 	if (ctx && ctx != dp)
606 		return 0;
607 
608 	switch (attr->id) {
609 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
610 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
611 			return -EOPNOTSUPP;
612 
613 		ret = dsa_port_set_state(dp, attr->u.stp_state, true);
614 		break;
615 	case SWITCHDEV_ATTR_ID_PORT_MST_STATE:
616 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
617 			return -EOPNOTSUPP;
618 
619 		ret = dsa_port_set_mst_state(dp, &attr->u.mst_state, extack);
620 		break;
621 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
622 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
623 			return -EOPNOTSUPP;
624 
625 		ret = dsa_port_vlan_filtering(dp, attr->u.vlan_filtering,
626 					      extack);
627 		break;
628 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
629 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
630 			return -EOPNOTSUPP;
631 
632 		ret = dsa_port_ageing_time(dp, attr->u.ageing_time);
633 		break;
634 	case SWITCHDEV_ATTR_ID_BRIDGE_MST:
635 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
636 			return -EOPNOTSUPP;
637 
638 		ret = dsa_port_mst_enable(dp, attr->u.mst, extack);
639 		break;
640 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
641 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
642 			return -EOPNOTSUPP;
643 
644 		ret = dsa_port_pre_bridge_flags(dp, attr->u.brport_flags,
645 						extack);
646 		break;
647 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
648 		if (!dsa_port_offloads_bridge_port(dp, attr->orig_dev))
649 			return -EOPNOTSUPP;
650 
651 		ret = dsa_port_bridge_flags(dp, attr->u.brport_flags, extack);
652 		break;
653 	case SWITCHDEV_ATTR_ID_VLAN_MSTI:
654 		if (!dsa_port_offloads_bridge_dev(dp, attr->orig_dev))
655 			return -EOPNOTSUPP;
656 
657 		ret = dsa_port_vlan_msti(dp, &attr->u.vlan_msti);
658 		break;
659 	default:
660 		ret = -EOPNOTSUPP;
661 		break;
662 	}
663 
664 	return ret;
665 }
666 
667 /* Must be called under rcu_read_lock() */
668 static int
dsa_user_vlan_check_for_8021q_uppers(struct net_device * user,const struct switchdev_obj_port_vlan * vlan)669 dsa_user_vlan_check_for_8021q_uppers(struct net_device *user,
670 				     const struct switchdev_obj_port_vlan *vlan)
671 {
672 	struct net_device *upper_dev;
673 	struct list_head *iter;
674 
675 	netdev_for_each_upper_dev_rcu(user, upper_dev, iter) {
676 		u16 vid;
677 
678 		if (!is_vlan_dev(upper_dev))
679 			continue;
680 
681 		vid = vlan_dev_vlan_id(upper_dev);
682 		if (vid == vlan->vid)
683 			return -EBUSY;
684 	}
685 
686 	return 0;
687 }
688 
dsa_user_vlan_add(struct net_device * dev,const struct switchdev_obj * obj,struct netlink_ext_ack * extack)689 static int dsa_user_vlan_add(struct net_device *dev,
690 			     const struct switchdev_obj *obj,
691 			     struct netlink_ext_ack *extack)
692 {
693 	struct dsa_port *dp = dsa_user_to_port(dev);
694 	struct switchdev_obj_port_vlan *vlan;
695 	int err;
696 
697 	if (dsa_port_skip_vlan_configuration(dp)) {
698 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
699 		return 0;
700 	}
701 
702 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
703 
704 	/* Deny adding a bridge VLAN when there is already an 802.1Q upper with
705 	 * the same VID.
706 	 */
707 	if (br_vlan_enabled(dsa_port_bridge_dev_get(dp))) {
708 		rcu_read_lock();
709 		err = dsa_user_vlan_check_for_8021q_uppers(dev, vlan);
710 		rcu_read_unlock();
711 		if (err) {
712 			NL_SET_ERR_MSG_MOD(extack,
713 					   "Port already has a VLAN upper with this VID");
714 			return err;
715 		}
716 	}
717 
718 	return dsa_port_vlan_add(dp, vlan, extack);
719 }
720 
721 /* Offload a VLAN installed on the bridge or on a foreign interface by
722  * installing it as a VLAN towards the CPU port.
723  */
dsa_user_host_vlan_add(struct net_device * dev,const struct switchdev_obj * obj,struct netlink_ext_ack * extack)724 static int dsa_user_host_vlan_add(struct net_device *dev,
725 				  const struct switchdev_obj *obj,
726 				  struct netlink_ext_ack *extack)
727 {
728 	struct dsa_port *dp = dsa_user_to_port(dev);
729 	struct switchdev_obj_port_vlan vlan;
730 
731 	/* Do nothing if this is a software bridge */
732 	if (!dp->bridge)
733 		return -EOPNOTSUPP;
734 
735 	if (dsa_port_skip_vlan_configuration(dp)) {
736 		NL_SET_ERR_MSG_MOD(extack, "skipping configuration of VLAN");
737 		return 0;
738 	}
739 
740 	vlan = *SWITCHDEV_OBJ_PORT_VLAN(obj);
741 
742 	/* Even though drivers often handle CPU membership in special ways,
743 	 * it doesn't make sense to program a PVID, so clear this flag.
744 	 */
745 	vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
746 
747 	return dsa_port_host_vlan_add(dp, &vlan, extack);
748 }
749 
dsa_user_port_obj_add(struct net_device * dev,const void * ctx,const struct switchdev_obj * obj,struct netlink_ext_ack * extack)750 static int dsa_user_port_obj_add(struct net_device *dev, const void *ctx,
751 				 const struct switchdev_obj *obj,
752 				 struct netlink_ext_ack *extack)
753 {
754 	struct dsa_port *dp = dsa_user_to_port(dev);
755 	int err;
756 
757 	if (ctx && ctx != dp)
758 		return 0;
759 
760 	switch (obj->id) {
761 	case SWITCHDEV_OBJ_ID_PORT_MDB:
762 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
763 			return -EOPNOTSUPP;
764 
765 		err = dsa_port_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
766 		break;
767 	case SWITCHDEV_OBJ_ID_HOST_MDB:
768 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
769 			return -EOPNOTSUPP;
770 
771 		err = dsa_port_bridge_host_mdb_add(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
772 		break;
773 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
774 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
775 			err = dsa_user_vlan_add(dev, obj, extack);
776 		else
777 			err = dsa_user_host_vlan_add(dev, obj, extack);
778 		break;
779 	case SWITCHDEV_OBJ_ID_MRP:
780 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
781 			return -EOPNOTSUPP;
782 
783 		err = dsa_port_mrp_add(dp, SWITCHDEV_OBJ_MRP(obj));
784 		break;
785 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
786 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
787 			return -EOPNOTSUPP;
788 
789 		err = dsa_port_mrp_add_ring_role(dp,
790 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
791 		break;
792 	default:
793 		err = -EOPNOTSUPP;
794 		break;
795 	}
796 
797 	return err;
798 }
799 
dsa_user_vlan_del(struct net_device * dev,const struct switchdev_obj * obj)800 static int dsa_user_vlan_del(struct net_device *dev,
801 			     const struct switchdev_obj *obj)
802 {
803 	struct dsa_port *dp = dsa_user_to_port(dev);
804 	struct switchdev_obj_port_vlan *vlan;
805 
806 	if (dsa_port_skip_vlan_configuration(dp))
807 		return 0;
808 
809 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
810 
811 	return dsa_port_vlan_del(dp, vlan);
812 }
813 
dsa_user_host_vlan_del(struct net_device * dev,const struct switchdev_obj * obj)814 static int dsa_user_host_vlan_del(struct net_device *dev,
815 				  const struct switchdev_obj *obj)
816 {
817 	struct dsa_port *dp = dsa_user_to_port(dev);
818 	struct switchdev_obj_port_vlan *vlan;
819 
820 	/* Do nothing if this is a software bridge */
821 	if (!dp->bridge)
822 		return -EOPNOTSUPP;
823 
824 	if (dsa_port_skip_vlan_configuration(dp))
825 		return 0;
826 
827 	vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
828 
829 	return dsa_port_host_vlan_del(dp, vlan);
830 }
831 
dsa_user_port_obj_del(struct net_device * dev,const void * ctx,const struct switchdev_obj * obj)832 static int dsa_user_port_obj_del(struct net_device *dev, const void *ctx,
833 				 const struct switchdev_obj *obj)
834 {
835 	struct dsa_port *dp = dsa_user_to_port(dev);
836 	int err;
837 
838 	if (ctx && ctx != dp)
839 		return 0;
840 
841 	switch (obj->id) {
842 	case SWITCHDEV_OBJ_ID_PORT_MDB:
843 		if (!dsa_port_offloads_bridge_port(dp, obj->orig_dev))
844 			return -EOPNOTSUPP;
845 
846 		err = dsa_port_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
847 		break;
848 	case SWITCHDEV_OBJ_ID_HOST_MDB:
849 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
850 			return -EOPNOTSUPP;
851 
852 		err = dsa_port_bridge_host_mdb_del(dp, SWITCHDEV_OBJ_PORT_MDB(obj));
853 		break;
854 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
855 		if (dsa_port_offloads_bridge_port(dp, obj->orig_dev))
856 			err = dsa_user_vlan_del(dev, obj);
857 		else
858 			err = dsa_user_host_vlan_del(dev, obj);
859 		break;
860 	case SWITCHDEV_OBJ_ID_MRP:
861 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
862 			return -EOPNOTSUPP;
863 
864 		err = dsa_port_mrp_del(dp, SWITCHDEV_OBJ_MRP(obj));
865 		break;
866 	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
867 		if (!dsa_port_offloads_bridge_dev(dp, obj->orig_dev))
868 			return -EOPNOTSUPP;
869 
870 		err = dsa_port_mrp_del_ring_role(dp,
871 						 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
872 		break;
873 	default:
874 		err = -EOPNOTSUPP;
875 		break;
876 	}
877 
878 	return err;
879 }
880 
dsa_user_netpoll_send_skb(struct net_device * dev,struct sk_buff * skb)881 static netdev_tx_t dsa_user_netpoll_send_skb(struct net_device *dev,
882 					     struct sk_buff *skb)
883 {
884 #ifdef CONFIG_NET_POLL_CONTROLLER
885 	struct dsa_user_priv *p = netdev_priv(dev);
886 
887 	return netpoll_send_skb(p->netpoll, skb);
888 #else
889 	BUG();
890 	return NETDEV_TX_OK;
891 #endif
892 }
893 
dsa_skb_tx_timestamp(struct dsa_user_priv * p,struct sk_buff * skb)894 static void dsa_skb_tx_timestamp(struct dsa_user_priv *p,
895 				 struct sk_buff *skb)
896 {
897 	struct dsa_switch *ds = p->dp->ds;
898 
899 	if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
900 		return;
901 
902 	if (!ds->ops->port_txtstamp)
903 		return;
904 
905 	ds->ops->port_txtstamp(ds, p->dp->index, skb);
906 }
907 
dsa_enqueue_skb(struct sk_buff * skb,struct net_device * dev)908 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev)
909 {
910 	/* SKB for netpoll still need to be mangled with the protocol-specific
911 	 * tag to be successfully transmitted
912 	 */
913 	if (unlikely(netpoll_tx_running(dev)))
914 		return dsa_user_netpoll_send_skb(dev, skb);
915 
916 	/* Queue the SKB for transmission on the parent interface, but
917 	 * do not modify its EtherType
918 	 */
919 	skb->dev = dsa_user_to_conduit(dev);
920 	dev_queue_xmit(skb);
921 
922 	return NETDEV_TX_OK;
923 }
924 EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
925 
dsa_user_xmit(struct sk_buff * skb,struct net_device * dev)926 static netdev_tx_t dsa_user_xmit(struct sk_buff *skb, struct net_device *dev)
927 {
928 	struct dsa_user_priv *p = netdev_priv(dev);
929 	struct sk_buff *nskb;
930 
931 	dev_sw_netstats_tx_add(dev, 1, skb->len);
932 
933 	memset(skb->cb, 0, sizeof(skb->cb));
934 
935 	/* Handle tx timestamp if any */
936 	dsa_skb_tx_timestamp(p, skb);
937 
938 	if (skb_ensure_writable_head_tail(skb, dev)) {
939 		dev_kfree_skb_any(skb);
940 		return NETDEV_TX_OK;
941 	}
942 
943 	/* needed_tailroom should still be 'warm' in the cache line from
944 	 * skb_ensure_writable_head_tail(), which has also ensured that
945 	 * padding is safe.
946 	 */
947 	if (dev->needed_tailroom)
948 		eth_skb_pad(skb);
949 
950 	/* Transmit function may have to reallocate the original SKB,
951 	 * in which case it must have freed it. Only free it here on error.
952 	 */
953 	nskb = p->xmit(skb, dev);
954 	if (!nskb) {
955 		kfree_skb(skb);
956 		return NETDEV_TX_OK;
957 	}
958 
959 	return dsa_enqueue_skb(nskb, dev);
960 }
961 
962 /* ethtool operations *******************************************************/
963 
dsa_user_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)964 static void dsa_user_get_drvinfo(struct net_device *dev,
965 				 struct ethtool_drvinfo *drvinfo)
966 {
967 	strscpy(drvinfo->driver, "dsa", sizeof(drvinfo->driver));
968 	strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
969 	strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
970 }
971 
dsa_user_get_regs_len(struct net_device * dev)972 static int dsa_user_get_regs_len(struct net_device *dev)
973 {
974 	struct dsa_port *dp = dsa_user_to_port(dev);
975 	struct dsa_switch *ds = dp->ds;
976 
977 	if (ds->ops->get_regs_len)
978 		return ds->ops->get_regs_len(ds, dp->index);
979 
980 	return -EOPNOTSUPP;
981 }
982 
983 static void
dsa_user_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * _p)984 dsa_user_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *_p)
985 {
986 	struct dsa_port *dp = dsa_user_to_port(dev);
987 	struct dsa_switch *ds = dp->ds;
988 
989 	if (ds->ops->get_regs)
990 		ds->ops->get_regs(ds, dp->index, regs, _p);
991 }
992 
dsa_user_nway_reset(struct net_device * dev)993 static int dsa_user_nway_reset(struct net_device *dev)
994 {
995 	struct dsa_port *dp = dsa_user_to_port(dev);
996 
997 	return phylink_ethtool_nway_reset(dp->pl);
998 }
999 
dsa_user_get_eeprom_len(struct net_device * dev)1000 static int dsa_user_get_eeprom_len(struct net_device *dev)
1001 {
1002 	struct dsa_port *dp = dsa_user_to_port(dev);
1003 	struct dsa_switch *ds = dp->ds;
1004 
1005 	if (ds->cd && ds->cd->eeprom_len)
1006 		return ds->cd->eeprom_len;
1007 
1008 	if (ds->ops->get_eeprom_len)
1009 		return ds->ops->get_eeprom_len(ds);
1010 
1011 	return 0;
1012 }
1013 
dsa_user_get_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)1014 static int dsa_user_get_eeprom(struct net_device *dev,
1015 			       struct ethtool_eeprom *eeprom, u8 *data)
1016 {
1017 	struct dsa_port *dp = dsa_user_to_port(dev);
1018 	struct dsa_switch *ds = dp->ds;
1019 
1020 	if (ds->ops->get_eeprom)
1021 		return ds->ops->get_eeprom(ds, eeprom, data);
1022 
1023 	return -EOPNOTSUPP;
1024 }
1025 
dsa_user_set_eeprom(struct net_device * dev,struct ethtool_eeprom * eeprom,u8 * data)1026 static int dsa_user_set_eeprom(struct net_device *dev,
1027 			       struct ethtool_eeprom *eeprom, u8 *data)
1028 {
1029 	struct dsa_port *dp = dsa_user_to_port(dev);
1030 	struct dsa_switch *ds = dp->ds;
1031 
1032 	if (ds->ops->set_eeprom)
1033 		return ds->ops->set_eeprom(ds, eeprom, data);
1034 
1035 	return -EOPNOTSUPP;
1036 }
1037 
dsa_user_get_strings(struct net_device * dev,uint32_t stringset,uint8_t * data)1038 static void dsa_user_get_strings(struct net_device *dev,
1039 				 uint32_t stringset, uint8_t *data)
1040 {
1041 	struct dsa_port *dp = dsa_user_to_port(dev);
1042 	struct dsa_switch *ds = dp->ds;
1043 
1044 	if (stringset == ETH_SS_STATS) {
1045 		int len = ETH_GSTRING_LEN;
1046 
1047 		strscpy_pad(data, "tx_packets", len);
1048 		strscpy_pad(data + len, "tx_bytes", len);
1049 		strscpy_pad(data + 2 * len, "rx_packets", len);
1050 		strscpy_pad(data + 3 * len, "rx_bytes", len);
1051 		if (ds->ops->get_strings)
1052 			ds->ops->get_strings(ds, dp->index, stringset,
1053 					     data + 4 * len);
1054 	} else if (stringset ==  ETH_SS_TEST) {
1055 		net_selftest_get_strings(data);
1056 	}
1057 
1058 }
1059 
dsa_user_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,uint64_t * data)1060 static void dsa_user_get_ethtool_stats(struct net_device *dev,
1061 				       struct ethtool_stats *stats,
1062 				       uint64_t *data)
1063 {
1064 	struct dsa_port *dp = dsa_user_to_port(dev);
1065 	struct dsa_switch *ds = dp->ds;
1066 	struct pcpu_sw_netstats *s;
1067 	unsigned int start;
1068 	int i;
1069 
1070 	for_each_possible_cpu(i) {
1071 		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1072 
1073 		s = per_cpu_ptr(dev->tstats, i);
1074 		do {
1075 			start = u64_stats_fetch_begin(&s->syncp);
1076 			tx_packets = u64_stats_read(&s->tx_packets);
1077 			tx_bytes = u64_stats_read(&s->tx_bytes);
1078 			rx_packets = u64_stats_read(&s->rx_packets);
1079 			rx_bytes = u64_stats_read(&s->rx_bytes);
1080 		} while (u64_stats_fetch_retry(&s->syncp, start));
1081 		data[0] += tx_packets;
1082 		data[1] += tx_bytes;
1083 		data[2] += rx_packets;
1084 		data[3] += rx_bytes;
1085 	}
1086 	if (ds->ops->get_ethtool_stats)
1087 		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
1088 }
1089 
dsa_user_get_sset_count(struct net_device * dev,int sset)1090 static int dsa_user_get_sset_count(struct net_device *dev, int sset)
1091 {
1092 	struct dsa_port *dp = dsa_user_to_port(dev);
1093 	struct dsa_switch *ds = dp->ds;
1094 
1095 	if (sset == ETH_SS_STATS) {
1096 		int count = 0;
1097 
1098 		if (ds->ops->get_sset_count) {
1099 			count = ds->ops->get_sset_count(ds, dp->index, sset);
1100 			if (count < 0)
1101 				return count;
1102 		}
1103 
1104 		return count + 4;
1105 	} else if (sset ==  ETH_SS_TEST) {
1106 		return net_selftest_get_count();
1107 	}
1108 
1109 	return -EOPNOTSUPP;
1110 }
1111 
dsa_user_get_eth_phy_stats(struct net_device * dev,struct ethtool_eth_phy_stats * phy_stats)1112 static void dsa_user_get_eth_phy_stats(struct net_device *dev,
1113 				       struct ethtool_eth_phy_stats *phy_stats)
1114 {
1115 	struct dsa_port *dp = dsa_user_to_port(dev);
1116 	struct dsa_switch *ds = dp->ds;
1117 
1118 	if (ds->ops->get_eth_phy_stats)
1119 		ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1120 }
1121 
dsa_user_get_eth_mac_stats(struct net_device * dev,struct ethtool_eth_mac_stats * mac_stats)1122 static void dsa_user_get_eth_mac_stats(struct net_device *dev,
1123 				       struct ethtool_eth_mac_stats *mac_stats)
1124 {
1125 	struct dsa_port *dp = dsa_user_to_port(dev);
1126 	struct dsa_switch *ds = dp->ds;
1127 
1128 	if (ds->ops->get_eth_mac_stats)
1129 		ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1130 }
1131 
1132 static void
dsa_user_get_eth_ctrl_stats(struct net_device * dev,struct ethtool_eth_ctrl_stats * ctrl_stats)1133 dsa_user_get_eth_ctrl_stats(struct net_device *dev,
1134 			    struct ethtool_eth_ctrl_stats *ctrl_stats)
1135 {
1136 	struct dsa_port *dp = dsa_user_to_port(dev);
1137 	struct dsa_switch *ds = dp->ds;
1138 
1139 	if (ds->ops->get_eth_ctrl_stats)
1140 		ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1141 }
1142 
1143 static void
dsa_user_get_rmon_stats(struct net_device * dev,struct ethtool_rmon_stats * rmon_stats,const struct ethtool_rmon_hist_range ** ranges)1144 dsa_user_get_rmon_stats(struct net_device *dev,
1145 			struct ethtool_rmon_stats *rmon_stats,
1146 			const struct ethtool_rmon_hist_range **ranges)
1147 {
1148 	struct dsa_port *dp = dsa_user_to_port(dev);
1149 	struct dsa_switch *ds = dp->ds;
1150 
1151 	if (ds->ops->get_rmon_stats)
1152 		ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges);
1153 }
1154 
dsa_user_net_selftest(struct net_device * ndev,struct ethtool_test * etest,u64 * buf)1155 static void dsa_user_net_selftest(struct net_device *ndev,
1156 				  struct ethtool_test *etest, u64 *buf)
1157 {
1158 	struct dsa_port *dp = dsa_user_to_port(ndev);
1159 	struct dsa_switch *ds = dp->ds;
1160 
1161 	if (ds->ops->self_test) {
1162 		ds->ops->self_test(ds, dp->index, etest, buf);
1163 		return;
1164 	}
1165 
1166 	net_selftest(ndev, etest, buf);
1167 }
1168 
dsa_user_get_mm(struct net_device * dev,struct ethtool_mm_state * state)1169 static int dsa_user_get_mm(struct net_device *dev,
1170 			   struct ethtool_mm_state *state)
1171 {
1172 	struct dsa_port *dp = dsa_user_to_port(dev);
1173 	struct dsa_switch *ds = dp->ds;
1174 
1175 	if (!ds->ops->get_mm)
1176 		return -EOPNOTSUPP;
1177 
1178 	return ds->ops->get_mm(ds, dp->index, state);
1179 }
1180 
dsa_user_set_mm(struct net_device * dev,struct ethtool_mm_cfg * cfg,struct netlink_ext_ack * extack)1181 static int dsa_user_set_mm(struct net_device *dev, struct ethtool_mm_cfg *cfg,
1182 			   struct netlink_ext_ack *extack)
1183 {
1184 	struct dsa_port *dp = dsa_user_to_port(dev);
1185 	struct dsa_switch *ds = dp->ds;
1186 
1187 	if (!ds->ops->set_mm)
1188 		return -EOPNOTSUPP;
1189 
1190 	return ds->ops->set_mm(ds, dp->index, cfg, extack);
1191 }
1192 
dsa_user_get_mm_stats(struct net_device * dev,struct ethtool_mm_stats * stats)1193 static void dsa_user_get_mm_stats(struct net_device *dev,
1194 				  struct ethtool_mm_stats *stats)
1195 {
1196 	struct dsa_port *dp = dsa_user_to_port(dev);
1197 	struct dsa_switch *ds = dp->ds;
1198 
1199 	if (ds->ops->get_mm_stats)
1200 		ds->ops->get_mm_stats(ds, dp->index, stats);
1201 }
1202 
dsa_user_get_wol(struct net_device * dev,struct ethtool_wolinfo * w)1203 static void dsa_user_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1204 {
1205 	struct dsa_port *dp = dsa_user_to_port(dev);
1206 	struct dsa_switch *ds = dp->ds;
1207 
1208 	phylink_ethtool_get_wol(dp->pl, w);
1209 
1210 	if (ds->ops->get_wol)
1211 		ds->ops->get_wol(ds, dp->index, w);
1212 }
1213 
dsa_user_set_wol(struct net_device * dev,struct ethtool_wolinfo * w)1214 static int dsa_user_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1215 {
1216 	struct dsa_port *dp = dsa_user_to_port(dev);
1217 	struct dsa_switch *ds = dp->ds;
1218 	int ret = -EOPNOTSUPP;
1219 
1220 	phylink_ethtool_set_wol(dp->pl, w);
1221 
1222 	if (ds->ops->set_wol)
1223 		ret = ds->ops->set_wol(ds, dp->index, w);
1224 
1225 	return ret;
1226 }
1227 
dsa_user_set_eee(struct net_device * dev,struct ethtool_keee * e)1228 static int dsa_user_set_eee(struct net_device *dev, struct ethtool_keee *e)
1229 {
1230 	struct dsa_port *dp = dsa_user_to_port(dev);
1231 	struct dsa_switch *ds = dp->ds;
1232 	int ret;
1233 
1234 	/* Check whether the switch supports EEE */
1235 	if (ds->ops->support_eee && !ds->ops->support_eee(ds, dp->index))
1236 		return -EOPNOTSUPP;
1237 
1238 	/* Port's PHY and MAC both need to be EEE capable */
1239 	if (!dev->phydev || !dp->pl)
1240 		return -ENODEV;
1241 
1242 	if (!ds->ops->set_mac_eee)
1243 		return -EOPNOTSUPP;
1244 
1245 	ret = ds->ops->set_mac_eee(ds, dp->index, e);
1246 	if (ret)
1247 		return ret;
1248 
1249 	return phylink_ethtool_set_eee(dp->pl, e);
1250 }
1251 
dsa_user_get_eee(struct net_device * dev,struct ethtool_keee * e)1252 static int dsa_user_get_eee(struct net_device *dev, struct ethtool_keee *e)
1253 {
1254 	struct dsa_port *dp = dsa_user_to_port(dev);
1255 	struct dsa_switch *ds = dp->ds;
1256 	int ret;
1257 
1258 	/* Check whether the switch supports EEE */
1259 	if (ds->ops->support_eee && !ds->ops->support_eee(ds, dp->index))
1260 		return -EOPNOTSUPP;
1261 
1262 	/* Port's PHY and MAC both need to be EEE capable */
1263 	if (!dev->phydev || !dp->pl)
1264 		return -ENODEV;
1265 
1266 	if (!ds->ops->get_mac_eee)
1267 		return -EOPNOTSUPP;
1268 
1269 	ret = ds->ops->get_mac_eee(ds, dp->index, e);
1270 	if (ret)
1271 		return ret;
1272 
1273 	return phylink_ethtool_get_eee(dp->pl, e);
1274 }
1275 
dsa_user_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)1276 static int dsa_user_get_link_ksettings(struct net_device *dev,
1277 				       struct ethtool_link_ksettings *cmd)
1278 {
1279 	struct dsa_port *dp = dsa_user_to_port(dev);
1280 
1281 	return phylink_ethtool_ksettings_get(dp->pl, cmd);
1282 }
1283 
dsa_user_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)1284 static int dsa_user_set_link_ksettings(struct net_device *dev,
1285 				       const struct ethtool_link_ksettings *cmd)
1286 {
1287 	struct dsa_port *dp = dsa_user_to_port(dev);
1288 
1289 	return phylink_ethtool_ksettings_set(dp->pl, cmd);
1290 }
1291 
dsa_user_get_pause_stats(struct net_device * dev,struct ethtool_pause_stats * pause_stats)1292 static void dsa_user_get_pause_stats(struct net_device *dev,
1293 				     struct ethtool_pause_stats *pause_stats)
1294 {
1295 	struct dsa_port *dp = dsa_user_to_port(dev);
1296 	struct dsa_switch *ds = dp->ds;
1297 
1298 	if (ds->ops->get_pause_stats)
1299 		ds->ops->get_pause_stats(ds, dp->index, pause_stats);
1300 }
1301 
dsa_user_get_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)1302 static void dsa_user_get_pauseparam(struct net_device *dev,
1303 				    struct ethtool_pauseparam *pause)
1304 {
1305 	struct dsa_port *dp = dsa_user_to_port(dev);
1306 
1307 	phylink_ethtool_get_pauseparam(dp->pl, pause);
1308 }
1309 
dsa_user_set_pauseparam(struct net_device * dev,struct ethtool_pauseparam * pause)1310 static int dsa_user_set_pauseparam(struct net_device *dev,
1311 				   struct ethtool_pauseparam *pause)
1312 {
1313 	struct dsa_port *dp = dsa_user_to_port(dev);
1314 
1315 	return phylink_ethtool_set_pauseparam(dp->pl, pause);
1316 }
1317 
1318 #ifdef CONFIG_NET_POLL_CONTROLLER
dsa_user_netpoll_setup(struct net_device * dev,struct netpoll_info * ni)1319 static int dsa_user_netpoll_setup(struct net_device *dev,
1320 				  struct netpoll_info *ni)
1321 {
1322 	struct net_device *conduit = dsa_user_to_conduit(dev);
1323 	struct dsa_user_priv *p = netdev_priv(dev);
1324 	struct netpoll *netpoll;
1325 	int err = 0;
1326 
1327 	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1328 	if (!netpoll)
1329 		return -ENOMEM;
1330 
1331 	err = __netpoll_setup(netpoll, conduit);
1332 	if (err) {
1333 		kfree(netpoll);
1334 		goto out;
1335 	}
1336 
1337 	p->netpoll = netpoll;
1338 out:
1339 	return err;
1340 }
1341 
dsa_user_netpoll_cleanup(struct net_device * dev)1342 static void dsa_user_netpoll_cleanup(struct net_device *dev)
1343 {
1344 	struct dsa_user_priv *p = netdev_priv(dev);
1345 	struct netpoll *netpoll = p->netpoll;
1346 
1347 	if (!netpoll)
1348 		return;
1349 
1350 	p->netpoll = NULL;
1351 
1352 	__netpoll_free(netpoll);
1353 }
1354 
dsa_user_poll_controller(struct net_device * dev)1355 static void dsa_user_poll_controller(struct net_device *dev)
1356 {
1357 }
1358 #endif
1359 
1360 static struct dsa_mall_tc_entry *
dsa_user_mall_tc_entry_find(struct net_device * dev,unsigned long cookie)1361 dsa_user_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1362 {
1363 	struct dsa_user_priv *p = netdev_priv(dev);
1364 	struct dsa_mall_tc_entry *mall_tc_entry;
1365 
1366 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1367 		if (mall_tc_entry->cookie == cookie)
1368 			return mall_tc_entry;
1369 
1370 	return NULL;
1371 }
1372 
1373 static int
dsa_user_add_cls_matchall_mirred(struct net_device * dev,struct tc_cls_matchall_offload * cls,bool ingress)1374 dsa_user_add_cls_matchall_mirred(struct net_device *dev,
1375 				 struct tc_cls_matchall_offload *cls,
1376 				 bool ingress)
1377 {
1378 	struct netlink_ext_ack *extack = cls->common.extack;
1379 	struct dsa_port *dp = dsa_user_to_port(dev);
1380 	struct dsa_user_priv *p = netdev_priv(dev);
1381 	struct dsa_mall_mirror_tc_entry *mirror;
1382 	struct dsa_mall_tc_entry *mall_tc_entry;
1383 	struct dsa_switch *ds = dp->ds;
1384 	struct flow_action_entry *act;
1385 	struct dsa_port *to_dp;
1386 	int err;
1387 
1388 	if (!ds->ops->port_mirror_add)
1389 		return -EOPNOTSUPP;
1390 
1391 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1392 					      cls->common.extack))
1393 		return -EOPNOTSUPP;
1394 
1395 	act = &cls->rule->action.entries[0];
1396 
1397 	if (!act->dev)
1398 		return -EINVAL;
1399 
1400 	if (!dsa_user_dev_check(act->dev))
1401 		return -EOPNOTSUPP;
1402 
1403 	to_dp = dsa_user_to_port(act->dev);
1404 
1405 	if (dp->ds != to_dp->ds) {
1406 		NL_SET_ERR_MSG_MOD(extack,
1407 				   "Cross-chip mirroring not implemented");
1408 		return -EOPNOTSUPP;
1409 	}
1410 
1411 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1412 	if (!mall_tc_entry)
1413 		return -ENOMEM;
1414 
1415 	mall_tc_entry->cookie = cls->cookie;
1416 	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1417 	mirror = &mall_tc_entry->mirror;
1418 	mirror->to_local_port = to_dp->index;
1419 	mirror->ingress = ingress;
1420 
1421 	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1422 	if (err) {
1423 		kfree(mall_tc_entry);
1424 		return err;
1425 	}
1426 
1427 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1428 
1429 	return err;
1430 }
1431 
1432 static int
dsa_user_add_cls_matchall_police(struct net_device * dev,struct tc_cls_matchall_offload * cls,bool ingress)1433 dsa_user_add_cls_matchall_police(struct net_device *dev,
1434 				 struct tc_cls_matchall_offload *cls,
1435 				 bool ingress)
1436 {
1437 	struct netlink_ext_ack *extack = cls->common.extack;
1438 	struct dsa_port *dp = dsa_user_to_port(dev);
1439 	struct dsa_user_priv *p = netdev_priv(dev);
1440 	struct dsa_mall_policer_tc_entry *policer;
1441 	struct dsa_mall_tc_entry *mall_tc_entry;
1442 	struct dsa_switch *ds = dp->ds;
1443 	struct flow_action_entry *act;
1444 	int err;
1445 
1446 	if (!ds->ops->port_policer_add) {
1447 		NL_SET_ERR_MSG_MOD(extack,
1448 				   "Policing offload not implemented");
1449 		return -EOPNOTSUPP;
1450 	}
1451 
1452 	if (!ingress) {
1453 		NL_SET_ERR_MSG_MOD(extack,
1454 				   "Only supported on ingress qdisc");
1455 		return -EOPNOTSUPP;
1456 	}
1457 
1458 	if (!flow_action_basic_hw_stats_check(&cls->rule->action,
1459 					      cls->common.extack))
1460 		return -EOPNOTSUPP;
1461 
1462 	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1463 		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1464 			NL_SET_ERR_MSG_MOD(extack,
1465 					   "Only one port policer allowed");
1466 			return -EEXIST;
1467 		}
1468 	}
1469 
1470 	act = &cls->rule->action.entries[0];
1471 
1472 	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1473 	if (!mall_tc_entry)
1474 		return -ENOMEM;
1475 
1476 	mall_tc_entry->cookie = cls->cookie;
1477 	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1478 	policer = &mall_tc_entry->policer;
1479 	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1480 	policer->burst = act->police.burst;
1481 
1482 	err = ds->ops->port_policer_add(ds, dp->index, policer);
1483 	if (err) {
1484 		kfree(mall_tc_entry);
1485 		return err;
1486 	}
1487 
1488 	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1489 
1490 	return err;
1491 }
1492 
dsa_user_add_cls_matchall(struct net_device * dev,struct tc_cls_matchall_offload * cls,bool ingress)1493 static int dsa_user_add_cls_matchall(struct net_device *dev,
1494 				     struct tc_cls_matchall_offload *cls,
1495 				     bool ingress)
1496 {
1497 	int err = -EOPNOTSUPP;
1498 
1499 	if (cls->common.protocol == htons(ETH_P_ALL) &&
1500 	    flow_offload_has_one_action(&cls->rule->action) &&
1501 	    cls->rule->action.entries[0].id == FLOW_ACTION_MIRRED)
1502 		err = dsa_user_add_cls_matchall_mirred(dev, cls, ingress);
1503 	else if (flow_offload_has_one_action(&cls->rule->action) &&
1504 		 cls->rule->action.entries[0].id == FLOW_ACTION_POLICE)
1505 		err = dsa_user_add_cls_matchall_police(dev, cls, ingress);
1506 
1507 	return err;
1508 }
1509 
dsa_user_del_cls_matchall(struct net_device * dev,struct tc_cls_matchall_offload * cls)1510 static void dsa_user_del_cls_matchall(struct net_device *dev,
1511 				      struct tc_cls_matchall_offload *cls)
1512 {
1513 	struct dsa_port *dp = dsa_user_to_port(dev);
1514 	struct dsa_mall_tc_entry *mall_tc_entry;
1515 	struct dsa_switch *ds = dp->ds;
1516 
1517 	mall_tc_entry = dsa_user_mall_tc_entry_find(dev, cls->cookie);
1518 	if (!mall_tc_entry)
1519 		return;
1520 
1521 	list_del(&mall_tc_entry->list);
1522 
1523 	switch (mall_tc_entry->type) {
1524 	case DSA_PORT_MALL_MIRROR:
1525 		if (ds->ops->port_mirror_del)
1526 			ds->ops->port_mirror_del(ds, dp->index,
1527 						 &mall_tc_entry->mirror);
1528 		break;
1529 	case DSA_PORT_MALL_POLICER:
1530 		if (ds->ops->port_policer_del)
1531 			ds->ops->port_policer_del(ds, dp->index);
1532 		break;
1533 	default:
1534 		WARN_ON(1);
1535 	}
1536 
1537 	kfree(mall_tc_entry);
1538 }
1539 
dsa_user_setup_tc_cls_matchall(struct net_device * dev,struct tc_cls_matchall_offload * cls,bool ingress)1540 static int dsa_user_setup_tc_cls_matchall(struct net_device *dev,
1541 					  struct tc_cls_matchall_offload *cls,
1542 					  bool ingress)
1543 {
1544 	if (cls->common.chain_index)
1545 		return -EOPNOTSUPP;
1546 
1547 	switch (cls->command) {
1548 	case TC_CLSMATCHALL_REPLACE:
1549 		return dsa_user_add_cls_matchall(dev, cls, ingress);
1550 	case TC_CLSMATCHALL_DESTROY:
1551 		dsa_user_del_cls_matchall(dev, cls);
1552 		return 0;
1553 	default:
1554 		return -EOPNOTSUPP;
1555 	}
1556 }
1557 
dsa_user_add_cls_flower(struct net_device * dev,struct flow_cls_offload * cls,bool ingress)1558 static int dsa_user_add_cls_flower(struct net_device *dev,
1559 				   struct flow_cls_offload *cls,
1560 				   bool ingress)
1561 {
1562 	struct dsa_port *dp = dsa_user_to_port(dev);
1563 	struct dsa_switch *ds = dp->ds;
1564 	int port = dp->index;
1565 
1566 	if (!ds->ops->cls_flower_add)
1567 		return -EOPNOTSUPP;
1568 
1569 	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1570 }
1571 
dsa_user_del_cls_flower(struct net_device * dev,struct flow_cls_offload * cls,bool ingress)1572 static int dsa_user_del_cls_flower(struct net_device *dev,
1573 				   struct flow_cls_offload *cls,
1574 				   bool ingress)
1575 {
1576 	struct dsa_port *dp = dsa_user_to_port(dev);
1577 	struct dsa_switch *ds = dp->ds;
1578 	int port = dp->index;
1579 
1580 	if (!ds->ops->cls_flower_del)
1581 		return -EOPNOTSUPP;
1582 
1583 	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1584 }
1585 
dsa_user_stats_cls_flower(struct net_device * dev,struct flow_cls_offload * cls,bool ingress)1586 static int dsa_user_stats_cls_flower(struct net_device *dev,
1587 				     struct flow_cls_offload *cls,
1588 				     bool ingress)
1589 {
1590 	struct dsa_port *dp = dsa_user_to_port(dev);
1591 	struct dsa_switch *ds = dp->ds;
1592 	int port = dp->index;
1593 
1594 	if (!ds->ops->cls_flower_stats)
1595 		return -EOPNOTSUPP;
1596 
1597 	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1598 }
1599 
dsa_user_setup_tc_cls_flower(struct net_device * dev,struct flow_cls_offload * cls,bool ingress)1600 static int dsa_user_setup_tc_cls_flower(struct net_device *dev,
1601 					struct flow_cls_offload *cls,
1602 					bool ingress)
1603 {
1604 	switch (cls->command) {
1605 	case FLOW_CLS_REPLACE:
1606 		return dsa_user_add_cls_flower(dev, cls, ingress);
1607 	case FLOW_CLS_DESTROY:
1608 		return dsa_user_del_cls_flower(dev, cls, ingress);
1609 	case FLOW_CLS_STATS:
1610 		return dsa_user_stats_cls_flower(dev, cls, ingress);
1611 	default:
1612 		return -EOPNOTSUPP;
1613 	}
1614 }
1615 
dsa_user_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv,bool ingress)1616 static int dsa_user_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1617 				      void *cb_priv, bool ingress)
1618 {
1619 	struct net_device *dev = cb_priv;
1620 
1621 	if (!tc_can_offload(dev))
1622 		return -EOPNOTSUPP;
1623 
1624 	switch (type) {
1625 	case TC_SETUP_CLSMATCHALL:
1626 		return dsa_user_setup_tc_cls_matchall(dev, type_data, ingress);
1627 	case TC_SETUP_CLSFLOWER:
1628 		return dsa_user_setup_tc_cls_flower(dev, type_data, ingress);
1629 	default:
1630 		return -EOPNOTSUPP;
1631 	}
1632 }
1633 
dsa_user_setup_tc_block_cb_ig(enum tc_setup_type type,void * type_data,void * cb_priv)1634 static int dsa_user_setup_tc_block_cb_ig(enum tc_setup_type type,
1635 					 void *type_data, void *cb_priv)
1636 {
1637 	return dsa_user_setup_tc_block_cb(type, type_data, cb_priv, true);
1638 }
1639 
dsa_user_setup_tc_block_cb_eg(enum tc_setup_type type,void * type_data,void * cb_priv)1640 static int dsa_user_setup_tc_block_cb_eg(enum tc_setup_type type,
1641 					 void *type_data, void *cb_priv)
1642 {
1643 	return dsa_user_setup_tc_block_cb(type, type_data, cb_priv, false);
1644 }
1645 
1646 static LIST_HEAD(dsa_user_block_cb_list);
1647 
dsa_user_setup_tc_block(struct net_device * dev,struct flow_block_offload * f)1648 static int dsa_user_setup_tc_block(struct net_device *dev,
1649 				   struct flow_block_offload *f)
1650 {
1651 	struct flow_block_cb *block_cb;
1652 	flow_setup_cb_t *cb;
1653 
1654 	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1655 		cb = dsa_user_setup_tc_block_cb_ig;
1656 	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1657 		cb = dsa_user_setup_tc_block_cb_eg;
1658 	else
1659 		return -EOPNOTSUPP;
1660 
1661 	f->driver_block_list = &dsa_user_block_cb_list;
1662 
1663 	switch (f->command) {
1664 	case FLOW_BLOCK_BIND:
1665 		if (flow_block_cb_is_busy(cb, dev, &dsa_user_block_cb_list))
1666 			return -EBUSY;
1667 
1668 		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1669 		if (IS_ERR(block_cb))
1670 			return PTR_ERR(block_cb);
1671 
1672 		flow_block_cb_add(block_cb, f);
1673 		list_add_tail(&block_cb->driver_list, &dsa_user_block_cb_list);
1674 		return 0;
1675 	case FLOW_BLOCK_UNBIND:
1676 		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1677 		if (!block_cb)
1678 			return -ENOENT;
1679 
1680 		flow_block_cb_remove(block_cb, f);
1681 		list_del(&block_cb->driver_list);
1682 		return 0;
1683 	default:
1684 		return -EOPNOTSUPP;
1685 	}
1686 }
1687 
dsa_user_setup_ft_block(struct dsa_switch * ds,int port,void * type_data)1688 static int dsa_user_setup_ft_block(struct dsa_switch *ds, int port,
1689 				   void *type_data)
1690 {
1691 	struct net_device *conduit = dsa_port_to_conduit(dsa_to_port(ds, port));
1692 
1693 	if (!conduit->netdev_ops->ndo_setup_tc)
1694 		return -EOPNOTSUPP;
1695 
1696 	return conduit->netdev_ops->ndo_setup_tc(conduit, TC_SETUP_FT, type_data);
1697 }
1698 
dsa_user_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)1699 static int dsa_user_setup_tc(struct net_device *dev, enum tc_setup_type type,
1700 			     void *type_data)
1701 {
1702 	struct dsa_port *dp = dsa_user_to_port(dev);
1703 	struct dsa_switch *ds = dp->ds;
1704 
1705 	switch (type) {
1706 	case TC_SETUP_BLOCK:
1707 		return dsa_user_setup_tc_block(dev, type_data);
1708 	case TC_SETUP_FT:
1709 		return dsa_user_setup_ft_block(ds, dp->index, type_data);
1710 	default:
1711 		break;
1712 	}
1713 
1714 	if (!ds->ops->port_setup_tc)
1715 		return -EOPNOTSUPP;
1716 
1717 	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1718 }
1719 
dsa_user_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc,u32 * rule_locs)1720 static int dsa_user_get_rxnfc(struct net_device *dev,
1721 			      struct ethtool_rxnfc *nfc, u32 *rule_locs)
1722 {
1723 	struct dsa_port *dp = dsa_user_to_port(dev);
1724 	struct dsa_switch *ds = dp->ds;
1725 
1726 	if (!ds->ops->get_rxnfc)
1727 		return -EOPNOTSUPP;
1728 
1729 	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1730 }
1731 
dsa_user_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * nfc)1732 static int dsa_user_set_rxnfc(struct net_device *dev,
1733 			      struct ethtool_rxnfc *nfc)
1734 {
1735 	struct dsa_port *dp = dsa_user_to_port(dev);
1736 	struct dsa_switch *ds = dp->ds;
1737 
1738 	if (!ds->ops->set_rxnfc)
1739 		return -EOPNOTSUPP;
1740 
1741 	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1742 }
1743 
dsa_user_get_ts_info(struct net_device * dev,struct kernel_ethtool_ts_info * ts)1744 static int dsa_user_get_ts_info(struct net_device *dev,
1745 				struct kernel_ethtool_ts_info *ts)
1746 {
1747 	struct dsa_user_priv *p = netdev_priv(dev);
1748 	struct dsa_switch *ds = p->dp->ds;
1749 
1750 	if (!ds->ops->get_ts_info)
1751 		return -EOPNOTSUPP;
1752 
1753 	return ds->ops->get_ts_info(ds, p->dp->index, ts);
1754 }
1755 
dsa_user_vlan_rx_add_vid(struct net_device * dev,__be16 proto,u16 vid)1756 static int dsa_user_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1757 				    u16 vid)
1758 {
1759 	struct dsa_port *dp = dsa_user_to_port(dev);
1760 	struct switchdev_obj_port_vlan vlan = {
1761 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1762 		.vid = vid,
1763 		/* This API only allows programming tagged, non-PVID VIDs */
1764 		.flags = 0,
1765 	};
1766 	struct netlink_ext_ack extack = {0};
1767 	struct dsa_switch *ds = dp->ds;
1768 	struct netdev_hw_addr *ha;
1769 	struct dsa_vlan *v;
1770 	int ret;
1771 
1772 	/* User port... */
1773 	ret = dsa_port_vlan_add(dp, &vlan, &extack);
1774 	if (ret) {
1775 		if (extack._msg)
1776 			netdev_err(dev, "%s\n", extack._msg);
1777 		return ret;
1778 	}
1779 
1780 	/* And CPU port... */
1781 	ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1782 	if (ret) {
1783 		if (extack._msg)
1784 			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1785 				   extack._msg);
1786 		return ret;
1787 	}
1788 
1789 	if (!dsa_switch_supports_uc_filtering(ds) &&
1790 	    !dsa_switch_supports_mc_filtering(ds))
1791 		return 0;
1792 
1793 	v = kzalloc(sizeof(*v), GFP_KERNEL);
1794 	if (!v) {
1795 		ret = -ENOMEM;
1796 		goto rollback;
1797 	}
1798 
1799 	netif_addr_lock_bh(dev);
1800 
1801 	v->vid = vid;
1802 	list_add_tail(&v->list, &dp->user_vlans);
1803 
1804 	if (dsa_switch_supports_mc_filtering(ds)) {
1805 		netdev_for_each_synced_mc_addr(ha, dev) {
1806 			dsa_user_schedule_standalone_work(dev, DSA_MC_ADD,
1807 							  ha->addr, vid);
1808 		}
1809 	}
1810 
1811 	if (dsa_switch_supports_uc_filtering(ds)) {
1812 		netdev_for_each_synced_uc_addr(ha, dev) {
1813 			dsa_user_schedule_standalone_work(dev, DSA_UC_ADD,
1814 							  ha->addr, vid);
1815 		}
1816 	}
1817 
1818 	netif_addr_unlock_bh(dev);
1819 
1820 	dsa_flush_workqueue();
1821 
1822 	return 0;
1823 
1824 rollback:
1825 	dsa_port_host_vlan_del(dp, &vlan);
1826 	dsa_port_vlan_del(dp, &vlan);
1827 
1828 	return ret;
1829 }
1830 
dsa_user_vlan_rx_kill_vid(struct net_device * dev,__be16 proto,u16 vid)1831 static int dsa_user_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1832 				     u16 vid)
1833 {
1834 	struct dsa_port *dp = dsa_user_to_port(dev);
1835 	struct switchdev_obj_port_vlan vlan = {
1836 		.vid = vid,
1837 		/* This API only allows programming tagged, non-PVID VIDs */
1838 		.flags = 0,
1839 	};
1840 	struct dsa_switch *ds = dp->ds;
1841 	struct netdev_hw_addr *ha;
1842 	struct dsa_vlan *v;
1843 	int err;
1844 
1845 	err = dsa_port_vlan_del(dp, &vlan);
1846 	if (err)
1847 		return err;
1848 
1849 	err = dsa_port_host_vlan_del(dp, &vlan);
1850 	if (err)
1851 		return err;
1852 
1853 	if (!dsa_switch_supports_uc_filtering(ds) &&
1854 	    !dsa_switch_supports_mc_filtering(ds))
1855 		return 0;
1856 
1857 	netif_addr_lock_bh(dev);
1858 
1859 	v = dsa_vlan_find(&dp->user_vlans, &vlan);
1860 	if (!v) {
1861 		netif_addr_unlock_bh(dev);
1862 		return -ENOENT;
1863 	}
1864 
1865 	list_del(&v->list);
1866 	kfree(v);
1867 
1868 	if (dsa_switch_supports_mc_filtering(ds)) {
1869 		netdev_for_each_synced_mc_addr(ha, dev) {
1870 			dsa_user_schedule_standalone_work(dev, DSA_MC_DEL,
1871 							  ha->addr, vid);
1872 		}
1873 	}
1874 
1875 	if (dsa_switch_supports_uc_filtering(ds)) {
1876 		netdev_for_each_synced_uc_addr(ha, dev) {
1877 			dsa_user_schedule_standalone_work(dev, DSA_UC_DEL,
1878 							  ha->addr, vid);
1879 		}
1880 	}
1881 
1882 	netif_addr_unlock_bh(dev);
1883 
1884 	dsa_flush_workqueue();
1885 
1886 	return 0;
1887 }
1888 
dsa_user_restore_vlan(struct net_device * vdev,int vid,void * arg)1889 static int dsa_user_restore_vlan(struct net_device *vdev, int vid, void *arg)
1890 {
1891 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1892 
1893 	return dsa_user_vlan_rx_add_vid(arg, proto, vid);
1894 }
1895 
dsa_user_clear_vlan(struct net_device * vdev,int vid,void * arg)1896 static int dsa_user_clear_vlan(struct net_device *vdev, int vid, void *arg)
1897 {
1898 	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1899 
1900 	return dsa_user_vlan_rx_kill_vid(arg, proto, vid);
1901 }
1902 
1903 /* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1904  * filtering is enabled. The baseline is that only ports that offload a
1905  * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1906  * but there are exceptions for quirky hardware.
1907  *
1908  * If ds->vlan_filtering_is_global = true, then standalone ports which share
1909  * the same switch with other ports that offload a VLAN-aware bridge are also
1910  * inevitably VLAN-aware.
1911  *
1912  * To summarize, a DSA switch port offloads:
1913  *
1914  * - If standalone (this includes software bridge, software LAG):
1915  *     - if ds->needs_standalone_vlan_filtering = true, OR if
1916  *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
1917  *       this switch chip which have vlan_filtering=1)
1918  *         - the 8021q upper VLANs
1919  *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
1920  *       global, or it is, but no port is under a VLAN-aware bridge):
1921  *         - no VLAN (any 8021q upper is a software VLAN)
1922  *
1923  * - If under a vlan_filtering=0 bridge which it offload:
1924  *     - if ds->configure_vlan_while_not_filtering = true (default):
1925  *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
1926  *     - else (deprecated):
1927  *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1928  *           enabled, so this behavior is broken and discouraged.
1929  *
1930  * - If under a vlan_filtering=1 bridge which it offload:
1931  *     - the bridge VLANs
1932  *     - the 8021q upper VLANs
1933  */
dsa_user_manage_vlan_filtering(struct net_device * user,bool vlan_filtering)1934 int dsa_user_manage_vlan_filtering(struct net_device *user,
1935 				   bool vlan_filtering)
1936 {
1937 	int err;
1938 
1939 	if (vlan_filtering) {
1940 		user->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1941 
1942 		err = vlan_for_each(user, dsa_user_restore_vlan, user);
1943 		if (err) {
1944 			vlan_for_each(user, dsa_user_clear_vlan, user);
1945 			user->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1946 			return err;
1947 		}
1948 	} else {
1949 		err = vlan_for_each(user, dsa_user_clear_vlan, user);
1950 		if (err)
1951 			return err;
1952 
1953 		user->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1954 	}
1955 
1956 	return 0;
1957 }
1958 
1959 struct dsa_hw_port {
1960 	struct list_head list;
1961 	struct net_device *dev;
1962 	int old_mtu;
1963 };
1964 
dsa_hw_port_list_set_mtu(struct list_head * hw_port_list,int mtu)1965 static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1966 {
1967 	const struct dsa_hw_port *p;
1968 	int err;
1969 
1970 	list_for_each_entry(p, hw_port_list, list) {
1971 		if (p->dev->mtu == mtu)
1972 			continue;
1973 
1974 		err = dev_set_mtu(p->dev, mtu);
1975 		if (err)
1976 			goto rollback;
1977 	}
1978 
1979 	return 0;
1980 
1981 rollback:
1982 	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
1983 		if (p->dev->mtu == p->old_mtu)
1984 			continue;
1985 
1986 		if (dev_set_mtu(p->dev, p->old_mtu))
1987 			netdev_err(p->dev, "Failed to restore MTU\n");
1988 	}
1989 
1990 	return err;
1991 }
1992 
dsa_hw_port_list_free(struct list_head * hw_port_list)1993 static void dsa_hw_port_list_free(struct list_head *hw_port_list)
1994 {
1995 	struct dsa_hw_port *p, *n;
1996 
1997 	list_for_each_entry_safe(p, n, hw_port_list, list)
1998 		kfree(p);
1999 }
2000 
2001 /* Make the hardware datapath to/from @dev limited to a common MTU */
dsa_bridge_mtu_normalization(struct dsa_port * dp)2002 static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
2003 {
2004 	struct list_head hw_port_list;
2005 	struct dsa_switch_tree *dst;
2006 	int min_mtu = ETH_MAX_MTU;
2007 	struct dsa_port *other_dp;
2008 	int err;
2009 
2010 	if (!dp->ds->mtu_enforcement_ingress)
2011 		return;
2012 
2013 	if (!dp->bridge)
2014 		return;
2015 
2016 	INIT_LIST_HEAD(&hw_port_list);
2017 
2018 	/* Populate the list of ports that are part of the same bridge
2019 	 * as the newly added/modified port
2020 	 */
2021 	list_for_each_entry(dst, &dsa_tree_list, list) {
2022 		list_for_each_entry(other_dp, &dst->ports, list) {
2023 			struct dsa_hw_port *hw_port;
2024 			struct net_device *user;
2025 
2026 			if (other_dp->type != DSA_PORT_TYPE_USER)
2027 				continue;
2028 
2029 			if (!dsa_port_bridge_same(dp, other_dp))
2030 				continue;
2031 
2032 			if (!other_dp->ds->mtu_enforcement_ingress)
2033 				continue;
2034 
2035 			user = other_dp->user;
2036 
2037 			if (min_mtu > user->mtu)
2038 				min_mtu = user->mtu;
2039 
2040 			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
2041 			if (!hw_port)
2042 				goto out;
2043 
2044 			hw_port->dev = user;
2045 			hw_port->old_mtu = user->mtu;
2046 
2047 			list_add(&hw_port->list, &hw_port_list);
2048 		}
2049 	}
2050 
2051 	/* Attempt to configure the entire hardware bridge to the newly added
2052 	 * interface's MTU first, regardless of whether the intention of the
2053 	 * user was to raise or lower it.
2054 	 */
2055 	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->user->mtu);
2056 	if (!err)
2057 		goto out;
2058 
2059 	/* Clearly that didn't work out so well, so just set the minimum MTU on
2060 	 * all hardware bridge ports now. If this fails too, then all ports will
2061 	 * still have their old MTU rolled back anyway.
2062 	 */
2063 	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
2064 
2065 out:
2066 	dsa_hw_port_list_free(&hw_port_list);
2067 }
2068 
dsa_user_change_mtu(struct net_device * dev,int new_mtu)2069 int dsa_user_change_mtu(struct net_device *dev, int new_mtu)
2070 {
2071 	struct net_device *conduit = dsa_user_to_conduit(dev);
2072 	struct dsa_port *dp = dsa_user_to_port(dev);
2073 	struct dsa_port *cpu_dp = dp->cpu_dp;
2074 	struct dsa_switch *ds = dp->ds;
2075 	struct dsa_port *other_dp;
2076 	int largest_mtu = 0;
2077 	int new_conduit_mtu;
2078 	int old_conduit_mtu;
2079 	int mtu_limit;
2080 	int overhead;
2081 	int cpu_mtu;
2082 	int err;
2083 
2084 	if (!ds->ops->port_change_mtu)
2085 		return -EOPNOTSUPP;
2086 
2087 	dsa_tree_for_each_user_port(other_dp, ds->dst) {
2088 		int user_mtu;
2089 
2090 		/* During probe, this function will be called for each user
2091 		 * device, while not all of them have been allocated. That's
2092 		 * ok, it doesn't change what the maximum is, so ignore it.
2093 		 */
2094 		if (!other_dp->user)
2095 			continue;
2096 
2097 		/* Pretend that we already applied the setting, which we
2098 		 * actually haven't (still haven't done all integrity checks)
2099 		 */
2100 		if (dp == other_dp)
2101 			user_mtu = new_mtu;
2102 		else
2103 			user_mtu = other_dp->user->mtu;
2104 
2105 		if (largest_mtu < user_mtu)
2106 			largest_mtu = user_mtu;
2107 	}
2108 
2109 	overhead = dsa_tag_protocol_overhead(cpu_dp->tag_ops);
2110 	mtu_limit = min_t(int, conduit->max_mtu, dev->max_mtu + overhead);
2111 	old_conduit_mtu = conduit->mtu;
2112 	new_conduit_mtu = largest_mtu + overhead;
2113 	if (new_conduit_mtu > mtu_limit)
2114 		return -ERANGE;
2115 
2116 	/* If the conduit MTU isn't over limit, there's no need to check the CPU
2117 	 * MTU, since that surely isn't either.
2118 	 */
2119 	cpu_mtu = largest_mtu;
2120 
2121 	/* Start applying stuff */
2122 	if (new_conduit_mtu != old_conduit_mtu) {
2123 		err = dev_set_mtu(conduit, new_conduit_mtu);
2124 		if (err < 0)
2125 			goto out_conduit_failed;
2126 
2127 		/* We only need to propagate the MTU of the CPU port to
2128 		 * upstream switches, so emit a notifier which updates them.
2129 		 */
2130 		err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
2131 		if (err)
2132 			goto out_cpu_failed;
2133 	}
2134 
2135 	err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
2136 	if (err)
2137 		goto out_port_failed;
2138 
2139 	WRITE_ONCE(dev->mtu, new_mtu);
2140 
2141 	dsa_bridge_mtu_normalization(dp);
2142 
2143 	return 0;
2144 
2145 out_port_failed:
2146 	if (new_conduit_mtu != old_conduit_mtu)
2147 		dsa_port_mtu_change(cpu_dp, old_conduit_mtu - overhead);
2148 out_cpu_failed:
2149 	if (new_conduit_mtu != old_conduit_mtu)
2150 		dev_set_mtu(conduit, old_conduit_mtu);
2151 out_conduit_failed:
2152 	return err;
2153 }
2154 
2155 static int __maybe_unused
dsa_user_dcbnl_set_apptrust(struct net_device * dev,u8 * sel,int nsel)2156 dsa_user_dcbnl_set_apptrust(struct net_device *dev, u8 *sel, int nsel)
2157 {
2158 	struct dsa_port *dp = dsa_user_to_port(dev);
2159 	struct dsa_switch *ds = dp->ds;
2160 	int port = dp->index;
2161 
2162 	if (!ds->ops->port_set_apptrust)
2163 		return -EOPNOTSUPP;
2164 
2165 	return ds->ops->port_set_apptrust(ds, port, sel, nsel);
2166 }
2167 
2168 static int __maybe_unused
dsa_user_dcbnl_get_apptrust(struct net_device * dev,u8 * sel,int * nsel)2169 dsa_user_dcbnl_get_apptrust(struct net_device *dev, u8 *sel, int *nsel)
2170 {
2171 	struct dsa_port *dp = dsa_user_to_port(dev);
2172 	struct dsa_switch *ds = dp->ds;
2173 	int port = dp->index;
2174 
2175 	if (!ds->ops->port_get_apptrust)
2176 		return -EOPNOTSUPP;
2177 
2178 	return ds->ops->port_get_apptrust(ds, port, sel, nsel);
2179 }
2180 
2181 static int __maybe_unused
dsa_user_dcbnl_set_default_prio(struct net_device * dev,struct dcb_app * app)2182 dsa_user_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
2183 {
2184 	struct dsa_port *dp = dsa_user_to_port(dev);
2185 	struct dsa_switch *ds = dp->ds;
2186 	unsigned long mask, new_prio;
2187 	int err, port = dp->index;
2188 
2189 	if (!ds->ops->port_set_default_prio)
2190 		return -EOPNOTSUPP;
2191 
2192 	err = dcb_ieee_setapp(dev, app);
2193 	if (err)
2194 		return err;
2195 
2196 	mask = dcb_ieee_getapp_mask(dev, app);
2197 	new_prio = __fls(mask);
2198 
2199 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
2200 	if (err) {
2201 		dcb_ieee_delapp(dev, app);
2202 		return err;
2203 	}
2204 
2205 	return 0;
2206 }
2207 
2208 /* Update the DSCP prio entries on all user ports of the switch in case
2209  * the switch supports global DSCP prio instead of per port DSCP prios.
2210  */
dsa_user_dcbnl_ieee_global_dscp_setdel(struct net_device * dev,struct dcb_app * app,bool del)2211 static int dsa_user_dcbnl_ieee_global_dscp_setdel(struct net_device *dev,
2212 						  struct dcb_app *app, bool del)
2213 {
2214 	int (*setdel)(struct net_device *dev, struct dcb_app *app);
2215 	struct dsa_port *dp = dsa_user_to_port(dev);
2216 	struct dsa_switch *ds = dp->ds;
2217 	struct dsa_port *other_dp;
2218 	int err, restore_err;
2219 
2220 	if (del)
2221 		setdel = dcb_ieee_delapp;
2222 	else
2223 		setdel = dcb_ieee_setapp;
2224 
2225 	dsa_switch_for_each_user_port(other_dp, ds) {
2226 		struct net_device *user = other_dp->user;
2227 
2228 		if (!user || user == dev)
2229 			continue;
2230 
2231 		err = setdel(user, app);
2232 		if (err)
2233 			goto err_try_to_restore;
2234 	}
2235 
2236 	return 0;
2237 
2238 err_try_to_restore:
2239 
2240 	/* Revert logic to restore previous state of app entries */
2241 	if (!del)
2242 		setdel = dcb_ieee_delapp;
2243 	else
2244 		setdel = dcb_ieee_setapp;
2245 
2246 	dsa_switch_for_each_user_port_continue_reverse(other_dp, ds) {
2247 		struct net_device *user = other_dp->user;
2248 
2249 		if (!user || user == dev)
2250 			continue;
2251 
2252 		restore_err = setdel(user, app);
2253 		if (restore_err)
2254 			netdev_err(user, "Failed to restore DSCP prio entry configuration\n");
2255 	}
2256 
2257 	return err;
2258 }
2259 
2260 static int __maybe_unused
dsa_user_dcbnl_add_dscp_prio(struct net_device * dev,struct dcb_app * app)2261 dsa_user_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
2262 {
2263 	struct dsa_port *dp = dsa_user_to_port(dev);
2264 	struct dsa_switch *ds = dp->ds;
2265 	unsigned long mask, new_prio;
2266 	int err, port = dp->index;
2267 	u8 dscp = app->protocol;
2268 
2269 	if (!ds->ops->port_add_dscp_prio)
2270 		return -EOPNOTSUPP;
2271 
2272 	if (dscp >= 64) {
2273 		netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
2274 			   dscp);
2275 		return -EINVAL;
2276 	}
2277 
2278 	err = dcb_ieee_setapp(dev, app);
2279 	if (err)
2280 		return err;
2281 
2282 	mask = dcb_ieee_getapp_mask(dev, app);
2283 	new_prio = __fls(mask);
2284 
2285 	err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
2286 	if (err) {
2287 		dcb_ieee_delapp(dev, app);
2288 		return err;
2289 	}
2290 
2291 	if (!ds->dscp_prio_mapping_is_global)
2292 		return 0;
2293 
2294 	err = dsa_user_dcbnl_ieee_global_dscp_setdel(dev, app, false);
2295 	if (err) {
2296 		if (ds->ops->port_del_dscp_prio)
2297 			ds->ops->port_del_dscp_prio(ds, port, dscp, new_prio);
2298 		dcb_ieee_delapp(dev, app);
2299 		return err;
2300 	}
2301 
2302 	return 0;
2303 }
2304 
dsa_user_dcbnl_ieee_setapp(struct net_device * dev,struct dcb_app * app)2305 static int __maybe_unused dsa_user_dcbnl_ieee_setapp(struct net_device *dev,
2306 						     struct dcb_app *app)
2307 {
2308 	switch (app->selector) {
2309 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2310 		switch (app->protocol) {
2311 		case 0:
2312 			return dsa_user_dcbnl_set_default_prio(dev, app);
2313 		default:
2314 			return -EOPNOTSUPP;
2315 		}
2316 		break;
2317 	case IEEE_8021QAZ_APP_SEL_DSCP:
2318 		return dsa_user_dcbnl_add_dscp_prio(dev, app);
2319 	default:
2320 		return -EOPNOTSUPP;
2321 	}
2322 }
2323 
2324 static int __maybe_unused
dsa_user_dcbnl_del_default_prio(struct net_device * dev,struct dcb_app * app)2325 dsa_user_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
2326 {
2327 	struct dsa_port *dp = dsa_user_to_port(dev);
2328 	struct dsa_switch *ds = dp->ds;
2329 	unsigned long mask, new_prio;
2330 	int err, port = dp->index;
2331 
2332 	if (!ds->ops->port_set_default_prio)
2333 		return -EOPNOTSUPP;
2334 
2335 	err = dcb_ieee_delapp(dev, app);
2336 	if (err)
2337 		return err;
2338 
2339 	mask = dcb_ieee_getapp_mask(dev, app);
2340 	new_prio = mask ? __fls(mask) : 0;
2341 
2342 	err = ds->ops->port_set_default_prio(ds, port, new_prio);
2343 	if (err) {
2344 		dcb_ieee_setapp(dev, app);
2345 		return err;
2346 	}
2347 
2348 	return 0;
2349 }
2350 
2351 static int __maybe_unused
dsa_user_dcbnl_del_dscp_prio(struct net_device * dev,struct dcb_app * app)2352 dsa_user_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
2353 {
2354 	struct dsa_port *dp = dsa_user_to_port(dev);
2355 	struct dsa_switch *ds = dp->ds;
2356 	int err, port = dp->index;
2357 	u8 dscp = app->protocol;
2358 
2359 	if (!ds->ops->port_del_dscp_prio)
2360 		return -EOPNOTSUPP;
2361 
2362 	err = dcb_ieee_delapp(dev, app);
2363 	if (err)
2364 		return err;
2365 
2366 	err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2367 	if (err) {
2368 		dcb_ieee_setapp(dev, app);
2369 		return err;
2370 	}
2371 
2372 	if (!ds->dscp_prio_mapping_is_global)
2373 		return 0;
2374 
2375 	err = dsa_user_dcbnl_ieee_global_dscp_setdel(dev, app, true);
2376 	if (err) {
2377 		if (ds->ops->port_add_dscp_prio)
2378 			ds->ops->port_add_dscp_prio(ds, port, dscp,
2379 						    app->priority);
2380 		dcb_ieee_setapp(dev, app);
2381 		return err;
2382 	}
2383 
2384 	return 0;
2385 }
2386 
dsa_user_dcbnl_ieee_delapp(struct net_device * dev,struct dcb_app * app)2387 static int __maybe_unused dsa_user_dcbnl_ieee_delapp(struct net_device *dev,
2388 						     struct dcb_app *app)
2389 {
2390 	switch (app->selector) {
2391 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2392 		switch (app->protocol) {
2393 		case 0:
2394 			return dsa_user_dcbnl_del_default_prio(dev, app);
2395 		default:
2396 			return -EOPNOTSUPP;
2397 		}
2398 		break;
2399 	case IEEE_8021QAZ_APP_SEL_DSCP:
2400 		return dsa_user_dcbnl_del_dscp_prio(dev, app);
2401 	default:
2402 		return -EOPNOTSUPP;
2403 	}
2404 }
2405 
2406 /* Pre-populate the DCB application priority table with the priorities
2407  * configured during switch setup, which we read from hardware here.
2408  */
dsa_user_dcbnl_init(struct net_device * dev)2409 static int dsa_user_dcbnl_init(struct net_device *dev)
2410 {
2411 	struct dsa_port *dp = dsa_user_to_port(dev);
2412 	struct dsa_switch *ds = dp->ds;
2413 	int port = dp->index;
2414 	int err;
2415 
2416 	if (ds->ops->port_get_default_prio) {
2417 		int prio = ds->ops->port_get_default_prio(ds, port);
2418 		struct dcb_app app = {
2419 			.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2420 			.protocol = 0,
2421 			.priority = prio,
2422 		};
2423 
2424 		if (prio < 0)
2425 			return prio;
2426 
2427 		err = dcb_ieee_setapp(dev, &app);
2428 		if (err)
2429 			return err;
2430 	}
2431 
2432 	if (ds->ops->port_get_dscp_prio) {
2433 		int protocol;
2434 
2435 		for (protocol = 0; protocol < 64; protocol++) {
2436 			struct dcb_app app = {
2437 				.selector = IEEE_8021QAZ_APP_SEL_DSCP,
2438 				.protocol = protocol,
2439 			};
2440 			int prio;
2441 
2442 			prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2443 			if (prio == -EOPNOTSUPP)
2444 				continue;
2445 			if (prio < 0)
2446 				return prio;
2447 
2448 			app.priority = prio;
2449 
2450 			err = dcb_ieee_setapp(dev, &app);
2451 			if (err)
2452 				return err;
2453 		}
2454 	}
2455 
2456 	return 0;
2457 }
2458 
2459 static const struct ethtool_ops dsa_user_ethtool_ops = {
2460 	.get_drvinfo		= dsa_user_get_drvinfo,
2461 	.get_regs_len		= dsa_user_get_regs_len,
2462 	.get_regs		= dsa_user_get_regs,
2463 	.nway_reset		= dsa_user_nway_reset,
2464 	.get_link		= ethtool_op_get_link,
2465 	.get_eeprom_len		= dsa_user_get_eeprom_len,
2466 	.get_eeprom		= dsa_user_get_eeprom,
2467 	.set_eeprom		= dsa_user_set_eeprom,
2468 	.get_strings		= dsa_user_get_strings,
2469 	.get_ethtool_stats	= dsa_user_get_ethtool_stats,
2470 	.get_sset_count		= dsa_user_get_sset_count,
2471 	.get_eth_phy_stats	= dsa_user_get_eth_phy_stats,
2472 	.get_eth_mac_stats	= dsa_user_get_eth_mac_stats,
2473 	.get_eth_ctrl_stats	= dsa_user_get_eth_ctrl_stats,
2474 	.get_rmon_stats		= dsa_user_get_rmon_stats,
2475 	.set_wol		= dsa_user_set_wol,
2476 	.get_wol		= dsa_user_get_wol,
2477 	.set_eee		= dsa_user_set_eee,
2478 	.get_eee		= dsa_user_get_eee,
2479 	.get_link_ksettings	= dsa_user_get_link_ksettings,
2480 	.set_link_ksettings	= dsa_user_set_link_ksettings,
2481 	.get_pause_stats	= dsa_user_get_pause_stats,
2482 	.get_pauseparam		= dsa_user_get_pauseparam,
2483 	.set_pauseparam		= dsa_user_set_pauseparam,
2484 	.get_rxnfc		= dsa_user_get_rxnfc,
2485 	.set_rxnfc		= dsa_user_set_rxnfc,
2486 	.get_ts_info		= dsa_user_get_ts_info,
2487 	.self_test		= dsa_user_net_selftest,
2488 	.get_mm			= dsa_user_get_mm,
2489 	.set_mm			= dsa_user_set_mm,
2490 	.get_mm_stats		= dsa_user_get_mm_stats,
2491 };
2492 
2493 static const struct dcbnl_rtnl_ops __maybe_unused dsa_user_dcbnl_ops = {
2494 	.ieee_setapp		= dsa_user_dcbnl_ieee_setapp,
2495 	.ieee_delapp		= dsa_user_dcbnl_ieee_delapp,
2496 	.dcbnl_setapptrust	= dsa_user_dcbnl_set_apptrust,
2497 	.dcbnl_getapptrust	= dsa_user_dcbnl_get_apptrust,
2498 };
2499 
dsa_user_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * s)2500 static void dsa_user_get_stats64(struct net_device *dev,
2501 				 struct rtnl_link_stats64 *s)
2502 {
2503 	struct dsa_port *dp = dsa_user_to_port(dev);
2504 	struct dsa_switch *ds = dp->ds;
2505 
2506 	if (ds->ops->get_stats64)
2507 		ds->ops->get_stats64(ds, dp->index, s);
2508 	else
2509 		dev_get_tstats64(dev, s);
2510 }
2511 
dsa_user_fill_forward_path(struct net_device_path_ctx * ctx,struct net_device_path * path)2512 static int dsa_user_fill_forward_path(struct net_device_path_ctx *ctx,
2513 				      struct net_device_path *path)
2514 {
2515 	struct dsa_port *dp = dsa_user_to_port(ctx->dev);
2516 	struct net_device *conduit = dsa_port_to_conduit(dp);
2517 	struct dsa_port *cpu_dp = dp->cpu_dp;
2518 
2519 	path->dev = ctx->dev;
2520 	path->type = DEV_PATH_DSA;
2521 	path->dsa.proto = cpu_dp->tag_ops->proto;
2522 	path->dsa.port = dp->index;
2523 	ctx->dev = conduit;
2524 
2525 	return 0;
2526 }
2527 
2528 static const struct net_device_ops dsa_user_netdev_ops = {
2529 	.ndo_open		= dsa_user_open,
2530 	.ndo_stop		= dsa_user_close,
2531 	.ndo_start_xmit		= dsa_user_xmit,
2532 	.ndo_change_rx_flags	= dsa_user_change_rx_flags,
2533 	.ndo_set_rx_mode	= dsa_user_set_rx_mode,
2534 	.ndo_set_mac_address	= dsa_user_set_mac_address,
2535 	.ndo_fdb_dump		= dsa_user_fdb_dump,
2536 	.ndo_eth_ioctl		= dsa_user_ioctl,
2537 	.ndo_get_iflink		= dsa_user_get_iflink,
2538 #ifdef CONFIG_NET_POLL_CONTROLLER
2539 	.ndo_netpoll_setup	= dsa_user_netpoll_setup,
2540 	.ndo_netpoll_cleanup	= dsa_user_netpoll_cleanup,
2541 	.ndo_poll_controller	= dsa_user_poll_controller,
2542 #endif
2543 	.ndo_setup_tc		= dsa_user_setup_tc,
2544 	.ndo_get_stats64	= dsa_user_get_stats64,
2545 	.ndo_vlan_rx_add_vid	= dsa_user_vlan_rx_add_vid,
2546 	.ndo_vlan_rx_kill_vid	= dsa_user_vlan_rx_kill_vid,
2547 	.ndo_change_mtu		= dsa_user_change_mtu,
2548 	.ndo_fill_forward_path	= dsa_user_fill_forward_path,
2549 };
2550 
2551 static const struct device_type dsa_type = {
2552 	.name	= "dsa",
2553 };
2554 
dsa_port_phylink_mac_change(struct dsa_switch * ds,int port,bool up)2555 void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2556 {
2557 	const struct dsa_port *dp = dsa_to_port(ds, port);
2558 
2559 	if (dp->pl)
2560 		phylink_mac_change(dp->pl, up);
2561 }
2562 EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2563 
dsa_user_phylink_fixed_state(struct phylink_config * config,struct phylink_link_state * state)2564 static void dsa_user_phylink_fixed_state(struct phylink_config *config,
2565 					 struct phylink_link_state *state)
2566 {
2567 	struct dsa_port *dp = dsa_phylink_to_port(config);
2568 	struct dsa_switch *ds = dp->ds;
2569 
2570 	/* No need to check that this operation is valid, the callback would
2571 	 * not be called if it was not.
2572 	 */
2573 	ds->ops->phylink_fixed_state(ds, dp->index, state);
2574 }
2575 
2576 /* user device setup *******************************************************/
dsa_user_phy_connect(struct net_device * user_dev,int addr,u32 flags)2577 static int dsa_user_phy_connect(struct net_device *user_dev, int addr,
2578 				u32 flags)
2579 {
2580 	struct dsa_port *dp = dsa_user_to_port(user_dev);
2581 	struct dsa_switch *ds = dp->ds;
2582 
2583 	user_dev->phydev = mdiobus_get_phy(ds->user_mii_bus, addr);
2584 	if (!user_dev->phydev) {
2585 		netdev_err(user_dev, "no phy at %d\n", addr);
2586 		return -ENODEV;
2587 	}
2588 
2589 	user_dev->phydev->dev_flags |= flags;
2590 
2591 	return phylink_connect_phy(dp->pl, user_dev->phydev);
2592 }
2593 
dsa_user_phy_setup(struct net_device * user_dev)2594 static int dsa_user_phy_setup(struct net_device *user_dev)
2595 {
2596 	struct dsa_port *dp = dsa_user_to_port(user_dev);
2597 	struct device_node *port_dn = dp->dn;
2598 	struct dsa_switch *ds = dp->ds;
2599 	u32 phy_flags = 0;
2600 	int ret;
2601 
2602 	dp->pl_config.dev = &user_dev->dev;
2603 	dp->pl_config.type = PHYLINK_NETDEV;
2604 
2605 	/* The get_fixed_state callback takes precedence over polling the
2606 	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
2607 	 * this if the switch provides such a callback.
2608 	 */
2609 	if (ds->ops->phylink_fixed_state) {
2610 		dp->pl_config.get_fixed_state = dsa_user_phylink_fixed_state;
2611 		dp->pl_config.poll_fixed_state = true;
2612 	}
2613 
2614 	ret = dsa_port_phylink_create(dp);
2615 	if (ret)
2616 		return ret;
2617 
2618 	if (ds->ops->get_phy_flags)
2619 		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2620 
2621 	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2622 	if (ret == -ENODEV && ds->user_mii_bus) {
2623 		/* We could not connect to a designated PHY or SFP, so try to
2624 		 * use the switch internal MDIO bus instead
2625 		 */
2626 		ret = dsa_user_phy_connect(user_dev, dp->index, phy_flags);
2627 	}
2628 	if (ret) {
2629 		netdev_err(user_dev, "failed to connect to PHY: %pe\n",
2630 			   ERR_PTR(ret));
2631 		dsa_port_phylink_destroy(dp);
2632 	}
2633 
2634 	return ret;
2635 }
2636 
dsa_user_setup_tagger(struct net_device * user)2637 void dsa_user_setup_tagger(struct net_device *user)
2638 {
2639 	struct dsa_port *dp = dsa_user_to_port(user);
2640 	struct net_device *conduit = dsa_port_to_conduit(dp);
2641 	struct dsa_user_priv *p = netdev_priv(user);
2642 	const struct dsa_port *cpu_dp = dp->cpu_dp;
2643 	const struct dsa_switch *ds = dp->ds;
2644 
2645 	user->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2646 	user->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2647 	/* Try to save one extra realloc later in the TX path (in the conduit)
2648 	 * by also inheriting the conduit's needed headroom and tailroom.
2649 	 * The 8021q driver also does this.
2650 	 */
2651 	user->needed_headroom += conduit->needed_headroom;
2652 	user->needed_tailroom += conduit->needed_tailroom;
2653 
2654 	p->xmit = cpu_dp->tag_ops->xmit;
2655 
2656 	user->features = conduit->vlan_features | NETIF_F_HW_TC;
2657 	user->hw_features |= NETIF_F_HW_TC;
2658 	if (user->needed_tailroom)
2659 		user->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2660 	if (ds->needs_standalone_vlan_filtering)
2661 		user->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2662 
2663 	user->lltx = true;
2664 }
2665 
dsa_user_suspend(struct net_device * user_dev)2666 int dsa_user_suspend(struct net_device *user_dev)
2667 {
2668 	struct dsa_port *dp = dsa_user_to_port(user_dev);
2669 
2670 	if (!netif_running(user_dev))
2671 		return 0;
2672 
2673 	netif_device_detach(user_dev);
2674 
2675 	rtnl_lock();
2676 	phylink_stop(dp->pl);
2677 	rtnl_unlock();
2678 
2679 	return 0;
2680 }
2681 
dsa_user_resume(struct net_device * user_dev)2682 int dsa_user_resume(struct net_device *user_dev)
2683 {
2684 	struct dsa_port *dp = dsa_user_to_port(user_dev);
2685 
2686 	if (!netif_running(user_dev))
2687 		return 0;
2688 
2689 	netif_device_attach(user_dev);
2690 
2691 	rtnl_lock();
2692 	phylink_start(dp->pl);
2693 	rtnl_unlock();
2694 
2695 	return 0;
2696 }
2697 
dsa_user_create(struct dsa_port * port)2698 int dsa_user_create(struct dsa_port *port)
2699 {
2700 	struct net_device *conduit = dsa_port_to_conduit(port);
2701 	struct dsa_switch *ds = port->ds;
2702 	struct net_device *user_dev;
2703 	struct dsa_user_priv *p;
2704 	const char *name;
2705 	int assign_type;
2706 	int ret;
2707 
2708 	if (!ds->num_tx_queues)
2709 		ds->num_tx_queues = 1;
2710 
2711 	if (port->name) {
2712 		name = port->name;
2713 		assign_type = NET_NAME_PREDICTABLE;
2714 	} else {
2715 		name = "eth%d";
2716 		assign_type = NET_NAME_ENUM;
2717 	}
2718 
2719 	user_dev = alloc_netdev_mqs(sizeof(struct dsa_user_priv), name,
2720 				    assign_type, ether_setup,
2721 				    ds->num_tx_queues, 1);
2722 	if (user_dev == NULL)
2723 		return -ENOMEM;
2724 
2725 	user_dev->rtnl_link_ops = &dsa_link_ops;
2726 	user_dev->ethtool_ops = &dsa_user_ethtool_ops;
2727 #if IS_ENABLED(CONFIG_DCB)
2728 	user_dev->dcbnl_ops = &dsa_user_dcbnl_ops;
2729 #endif
2730 	if (!is_zero_ether_addr(port->mac))
2731 		eth_hw_addr_set(user_dev, port->mac);
2732 	else
2733 		eth_hw_addr_inherit(user_dev, conduit);
2734 	user_dev->priv_flags |= IFF_NO_QUEUE;
2735 	if (dsa_switch_supports_uc_filtering(ds))
2736 		user_dev->priv_flags |= IFF_UNICAST_FLT;
2737 	user_dev->netdev_ops = &dsa_user_netdev_ops;
2738 	if (ds->ops->port_max_mtu)
2739 		user_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2740 	SET_NETDEV_DEVTYPE(user_dev, &dsa_type);
2741 
2742 	SET_NETDEV_DEV(user_dev, port->ds->dev);
2743 	SET_NETDEV_DEVLINK_PORT(user_dev, &port->devlink_port);
2744 	user_dev->dev.of_node = port->dn;
2745 	user_dev->vlan_features = conduit->vlan_features;
2746 
2747 	p = netdev_priv(user_dev);
2748 	user_dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
2749 
2750 	ret = gro_cells_init(&p->gcells, user_dev);
2751 	if (ret)
2752 		goto out_free;
2753 
2754 	p->dp = port;
2755 	INIT_LIST_HEAD(&p->mall_tc_list);
2756 	port->user = user_dev;
2757 	dsa_user_setup_tagger(user_dev);
2758 
2759 	netif_carrier_off(user_dev);
2760 
2761 	ret = dsa_user_phy_setup(user_dev);
2762 	if (ret) {
2763 		netdev_err(user_dev,
2764 			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2765 			   ret, ds->dst->index, ds->index, port->index);
2766 		goto out_gcells;
2767 	}
2768 
2769 	rtnl_lock();
2770 
2771 	ret = dsa_user_change_mtu(user_dev, ETH_DATA_LEN);
2772 	if (ret && ret != -EOPNOTSUPP)
2773 		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2774 			 ret, ETH_DATA_LEN, port->index);
2775 
2776 	ret = register_netdevice(user_dev);
2777 	if (ret) {
2778 		netdev_err(conduit, "error %d registering interface %s\n",
2779 			   ret, user_dev->name);
2780 		rtnl_unlock();
2781 		goto out_phy;
2782 	}
2783 
2784 	if (IS_ENABLED(CONFIG_DCB)) {
2785 		ret = dsa_user_dcbnl_init(user_dev);
2786 		if (ret) {
2787 			netdev_err(user_dev,
2788 				   "failed to initialize DCB: %pe\n",
2789 				   ERR_PTR(ret));
2790 			rtnl_unlock();
2791 			goto out_unregister;
2792 		}
2793 	}
2794 
2795 	ret = netdev_upper_dev_link(conduit, user_dev, NULL);
2796 
2797 	rtnl_unlock();
2798 
2799 	if (ret)
2800 		goto out_unregister;
2801 
2802 	return 0;
2803 
2804 out_unregister:
2805 	unregister_netdev(user_dev);
2806 out_phy:
2807 	rtnl_lock();
2808 	phylink_disconnect_phy(p->dp->pl);
2809 	rtnl_unlock();
2810 	dsa_port_phylink_destroy(p->dp);
2811 out_gcells:
2812 	gro_cells_destroy(&p->gcells);
2813 out_free:
2814 	free_netdev(user_dev);
2815 	port->user = NULL;
2816 	return ret;
2817 }
2818 
dsa_user_destroy(struct net_device * user_dev)2819 void dsa_user_destroy(struct net_device *user_dev)
2820 {
2821 	struct net_device *conduit = dsa_user_to_conduit(user_dev);
2822 	struct dsa_port *dp = dsa_user_to_port(user_dev);
2823 	struct dsa_user_priv *p = netdev_priv(user_dev);
2824 
2825 	netif_carrier_off(user_dev);
2826 	rtnl_lock();
2827 	netdev_upper_dev_unlink(conduit, user_dev);
2828 	unregister_netdevice(user_dev);
2829 	phylink_disconnect_phy(dp->pl);
2830 	rtnl_unlock();
2831 
2832 	dsa_port_phylink_destroy(dp);
2833 	gro_cells_destroy(&p->gcells);
2834 	free_netdev(user_dev);
2835 }
2836 
dsa_user_change_conduit(struct net_device * dev,struct net_device * conduit,struct netlink_ext_ack * extack)2837 int dsa_user_change_conduit(struct net_device *dev, struct net_device *conduit,
2838 			    struct netlink_ext_ack *extack)
2839 {
2840 	struct net_device *old_conduit = dsa_user_to_conduit(dev);
2841 	struct dsa_port *dp = dsa_user_to_port(dev);
2842 	struct dsa_switch *ds = dp->ds;
2843 	struct net_device *upper;
2844 	struct list_head *iter;
2845 	int err;
2846 
2847 	if (conduit == old_conduit)
2848 		return 0;
2849 
2850 	if (!ds->ops->port_change_conduit) {
2851 		NL_SET_ERR_MSG_MOD(extack,
2852 				   "Driver does not support changing DSA conduit");
2853 		return -EOPNOTSUPP;
2854 	}
2855 
2856 	if (!netdev_uses_dsa(conduit)) {
2857 		NL_SET_ERR_MSG_MOD(extack,
2858 				   "Interface not eligible as DSA conduit");
2859 		return -EOPNOTSUPP;
2860 	}
2861 
2862 	netdev_for_each_upper_dev_rcu(conduit, upper, iter) {
2863 		if (dsa_user_dev_check(upper))
2864 			continue;
2865 		if (netif_is_bridge_master(upper))
2866 			continue;
2867 		NL_SET_ERR_MSG_MOD(extack, "Cannot join conduit with unknown uppers");
2868 		return -EOPNOTSUPP;
2869 	}
2870 
2871 	/* Since we allow live-changing the DSA conduit, plus we auto-open the
2872 	 * DSA conduit when the user port opens => we need to ensure that the
2873 	 * new DSA conduit is open too.
2874 	 */
2875 	if (dev->flags & IFF_UP) {
2876 		err = dev_open(conduit, extack);
2877 		if (err)
2878 			return err;
2879 	}
2880 
2881 	netdev_upper_dev_unlink(old_conduit, dev);
2882 
2883 	err = netdev_upper_dev_link(conduit, dev, extack);
2884 	if (err)
2885 		goto out_revert_old_conduit_unlink;
2886 
2887 	err = dsa_port_change_conduit(dp, conduit, extack);
2888 	if (err)
2889 		goto out_revert_conduit_link;
2890 
2891 	/* Update the MTU of the new CPU port through cross-chip notifiers */
2892 	err = dsa_user_change_mtu(dev, dev->mtu);
2893 	if (err && err != -EOPNOTSUPP) {
2894 		netdev_warn(dev,
2895 			    "nonfatal error updating MTU with new conduit: %pe\n",
2896 			    ERR_PTR(err));
2897 	}
2898 
2899 	return 0;
2900 
2901 out_revert_conduit_link:
2902 	netdev_upper_dev_unlink(conduit, dev);
2903 out_revert_old_conduit_unlink:
2904 	netdev_upper_dev_link(old_conduit, dev, NULL);
2905 	return err;
2906 }
2907 
dsa_user_dev_check(const struct net_device * dev)2908 bool dsa_user_dev_check(const struct net_device *dev)
2909 {
2910 	return dev->netdev_ops == &dsa_user_netdev_ops;
2911 }
2912 EXPORT_SYMBOL_GPL(dsa_user_dev_check);
2913 
dsa_user_changeupper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)2914 static int dsa_user_changeupper(struct net_device *dev,
2915 				struct netdev_notifier_changeupper_info *info)
2916 {
2917 	struct netlink_ext_ack *extack;
2918 	int err = NOTIFY_DONE;
2919 	struct dsa_port *dp;
2920 
2921 	if (!dsa_user_dev_check(dev))
2922 		return err;
2923 
2924 	dp = dsa_user_to_port(dev);
2925 	extack = netdev_notifier_info_to_extack(&info->info);
2926 
2927 	if (netif_is_bridge_master(info->upper_dev)) {
2928 		if (info->linking) {
2929 			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2930 			if (!err)
2931 				dsa_bridge_mtu_normalization(dp);
2932 			if (err == -EOPNOTSUPP) {
2933 				NL_SET_ERR_MSG_WEAK_MOD(extack,
2934 							"Offloading not supported");
2935 				err = 0;
2936 			}
2937 			err = notifier_from_errno(err);
2938 		} else {
2939 			dsa_port_bridge_leave(dp, info->upper_dev);
2940 			err = NOTIFY_OK;
2941 		}
2942 	} else if (netif_is_lag_master(info->upper_dev)) {
2943 		if (info->linking) {
2944 			err = dsa_port_lag_join(dp, info->upper_dev,
2945 						info->upper_info, extack);
2946 			if (err == -EOPNOTSUPP) {
2947 				NL_SET_ERR_MSG_WEAK_MOD(extack,
2948 							"Offloading not supported");
2949 				err = 0;
2950 			}
2951 			err = notifier_from_errno(err);
2952 		} else {
2953 			dsa_port_lag_leave(dp, info->upper_dev);
2954 			err = NOTIFY_OK;
2955 		}
2956 	} else if (is_hsr_master(info->upper_dev)) {
2957 		if (info->linking) {
2958 			err = dsa_port_hsr_join(dp, info->upper_dev, extack);
2959 			if (err == -EOPNOTSUPP) {
2960 				NL_SET_ERR_MSG_WEAK_MOD(extack,
2961 							"Offloading not supported");
2962 				err = 0;
2963 			}
2964 			err = notifier_from_errno(err);
2965 		} else {
2966 			dsa_port_hsr_leave(dp, info->upper_dev);
2967 			err = NOTIFY_OK;
2968 		}
2969 	}
2970 
2971 	return err;
2972 }
2973 
dsa_user_prechangeupper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)2974 static int dsa_user_prechangeupper(struct net_device *dev,
2975 				   struct netdev_notifier_changeupper_info *info)
2976 {
2977 	struct dsa_port *dp;
2978 
2979 	if (!dsa_user_dev_check(dev))
2980 		return NOTIFY_DONE;
2981 
2982 	dp = dsa_user_to_port(dev);
2983 
2984 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
2985 		dsa_port_pre_bridge_leave(dp, info->upper_dev);
2986 	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
2987 		dsa_port_pre_lag_leave(dp, info->upper_dev);
2988 	/* dsa_port_pre_hsr_leave is not yet necessary since hsr devices cannot
2989 	 * meaningfully placed under a bridge yet
2990 	 */
2991 
2992 	return NOTIFY_DONE;
2993 }
2994 
2995 static int
dsa_user_lag_changeupper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)2996 dsa_user_lag_changeupper(struct net_device *dev,
2997 			 struct netdev_notifier_changeupper_info *info)
2998 {
2999 	struct net_device *lower;
3000 	struct list_head *iter;
3001 	int err = NOTIFY_DONE;
3002 	struct dsa_port *dp;
3003 
3004 	if (!netif_is_lag_master(dev))
3005 		return err;
3006 
3007 	netdev_for_each_lower_dev(dev, lower, iter) {
3008 		if (!dsa_user_dev_check(lower))
3009 			continue;
3010 
3011 		dp = dsa_user_to_port(lower);
3012 		if (!dp->lag)
3013 			/* Software LAG */
3014 			continue;
3015 
3016 		err = dsa_user_changeupper(lower, info);
3017 		if (notifier_to_errno(err))
3018 			break;
3019 	}
3020 
3021 	return err;
3022 }
3023 
3024 /* Same as dsa_user_lag_changeupper() except that it calls
3025  * dsa_user_prechangeupper()
3026  */
3027 static int
dsa_user_lag_prechangeupper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)3028 dsa_user_lag_prechangeupper(struct net_device *dev,
3029 			    struct netdev_notifier_changeupper_info *info)
3030 {
3031 	struct net_device *lower;
3032 	struct list_head *iter;
3033 	int err = NOTIFY_DONE;
3034 	struct dsa_port *dp;
3035 
3036 	if (!netif_is_lag_master(dev))
3037 		return err;
3038 
3039 	netdev_for_each_lower_dev(dev, lower, iter) {
3040 		if (!dsa_user_dev_check(lower))
3041 			continue;
3042 
3043 		dp = dsa_user_to_port(lower);
3044 		if (!dp->lag)
3045 			/* Software LAG */
3046 			continue;
3047 
3048 		err = dsa_user_prechangeupper(lower, info);
3049 		if (notifier_to_errno(err))
3050 			break;
3051 	}
3052 
3053 	return err;
3054 }
3055 
3056 static int
dsa_prevent_bridging_8021q_upper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)3057 dsa_prevent_bridging_8021q_upper(struct net_device *dev,
3058 				 struct netdev_notifier_changeupper_info *info)
3059 {
3060 	struct netlink_ext_ack *ext_ack;
3061 	struct net_device *user, *br;
3062 	struct dsa_port *dp;
3063 
3064 	ext_ack = netdev_notifier_info_to_extack(&info->info);
3065 
3066 	if (!is_vlan_dev(dev))
3067 		return NOTIFY_DONE;
3068 
3069 	user = vlan_dev_real_dev(dev);
3070 	if (!dsa_user_dev_check(user))
3071 		return NOTIFY_DONE;
3072 
3073 	dp = dsa_user_to_port(user);
3074 	br = dsa_port_bridge_dev_get(dp);
3075 	if (!br)
3076 		return NOTIFY_DONE;
3077 
3078 	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
3079 	if (br_vlan_enabled(br) &&
3080 	    netif_is_bridge_master(info->upper_dev) && info->linking) {
3081 		NL_SET_ERR_MSG_MOD(ext_ack,
3082 				   "Cannot make VLAN device join VLAN-aware bridge");
3083 		return notifier_from_errno(-EINVAL);
3084 	}
3085 
3086 	return NOTIFY_DONE;
3087 }
3088 
3089 static int
dsa_user_check_8021q_upper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)3090 dsa_user_check_8021q_upper(struct net_device *dev,
3091 			   struct netdev_notifier_changeupper_info *info)
3092 {
3093 	struct dsa_port *dp = dsa_user_to_port(dev);
3094 	struct net_device *br = dsa_port_bridge_dev_get(dp);
3095 	struct bridge_vlan_info br_info;
3096 	struct netlink_ext_ack *extack;
3097 	int err = NOTIFY_DONE;
3098 	u16 vid;
3099 
3100 	if (!br || !br_vlan_enabled(br))
3101 		return NOTIFY_DONE;
3102 
3103 	extack = netdev_notifier_info_to_extack(&info->info);
3104 	vid = vlan_dev_vlan_id(info->upper_dev);
3105 
3106 	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
3107 	 * device, respectively the VID is not found, returning
3108 	 * 0 means success, which is a failure for us here.
3109 	 */
3110 	err = br_vlan_get_info(br, vid, &br_info);
3111 	if (err == 0) {
3112 		NL_SET_ERR_MSG_MOD(extack,
3113 				   "This VLAN is already configured by the bridge");
3114 		return notifier_from_errno(-EBUSY);
3115 	}
3116 
3117 	return NOTIFY_DONE;
3118 }
3119 
3120 static int
dsa_user_prechangeupper_sanity_check(struct net_device * dev,struct netdev_notifier_changeupper_info * info)3121 dsa_user_prechangeupper_sanity_check(struct net_device *dev,
3122 				     struct netdev_notifier_changeupper_info *info)
3123 {
3124 	struct dsa_switch *ds;
3125 	struct dsa_port *dp;
3126 	int err;
3127 
3128 	if (!dsa_user_dev_check(dev))
3129 		return dsa_prevent_bridging_8021q_upper(dev, info);
3130 
3131 	dp = dsa_user_to_port(dev);
3132 	ds = dp->ds;
3133 
3134 	if (ds->ops->port_prechangeupper) {
3135 		err = ds->ops->port_prechangeupper(ds, dp->index, info);
3136 		if (err)
3137 			return notifier_from_errno(err);
3138 	}
3139 
3140 	if (is_vlan_dev(info->upper_dev))
3141 		return dsa_user_check_8021q_upper(dev, info);
3142 
3143 	return NOTIFY_DONE;
3144 }
3145 
3146 /* To be eligible as a DSA conduit, a LAG must have all lower interfaces be
3147  * eligible DSA conduits. Additionally, all LAG slaves must be DSA conduits of
3148  * switches in the same switch tree.
3149  */
dsa_lag_conduit_validate(struct net_device * lag_dev,struct netlink_ext_ack * extack)3150 static int dsa_lag_conduit_validate(struct net_device *lag_dev,
3151 				    struct netlink_ext_ack *extack)
3152 {
3153 	struct net_device *lower1, *lower2;
3154 	struct list_head *iter1, *iter2;
3155 
3156 	netdev_for_each_lower_dev(lag_dev, lower1, iter1) {
3157 		netdev_for_each_lower_dev(lag_dev, lower2, iter2) {
3158 			if (!netdev_uses_dsa(lower1) ||
3159 			    !netdev_uses_dsa(lower2)) {
3160 				NL_SET_ERR_MSG_MOD(extack,
3161 						   "All LAG ports must be eligible as DSA conduits");
3162 				return notifier_from_errno(-EINVAL);
3163 			}
3164 
3165 			if (lower1 == lower2)
3166 				continue;
3167 
3168 			if (!dsa_port_tree_same(lower1->dsa_ptr,
3169 						lower2->dsa_ptr)) {
3170 				NL_SET_ERR_MSG_MOD(extack,
3171 						   "LAG contains DSA conduits of disjoint switch trees");
3172 				return notifier_from_errno(-EINVAL);
3173 			}
3174 		}
3175 	}
3176 
3177 	return NOTIFY_DONE;
3178 }
3179 
3180 static int
dsa_conduit_prechangeupper_sanity_check(struct net_device * conduit,struct netdev_notifier_changeupper_info * info)3181 dsa_conduit_prechangeupper_sanity_check(struct net_device *conduit,
3182 					struct netdev_notifier_changeupper_info *info)
3183 {
3184 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3185 
3186 	if (!netdev_uses_dsa(conduit))
3187 		return NOTIFY_DONE;
3188 
3189 	if (!info->linking)
3190 		return NOTIFY_DONE;
3191 
3192 	/* Allow DSA switch uppers */
3193 	if (dsa_user_dev_check(info->upper_dev))
3194 		return NOTIFY_DONE;
3195 
3196 	/* Allow bridge uppers of DSA conduits, subject to further
3197 	 * restrictions in dsa_bridge_prechangelower_sanity_check()
3198 	 */
3199 	if (netif_is_bridge_master(info->upper_dev))
3200 		return NOTIFY_DONE;
3201 
3202 	/* Allow LAG uppers, subject to further restrictions in
3203 	 * dsa_lag_conduit_prechangelower_sanity_check()
3204 	 */
3205 	if (netif_is_lag_master(info->upper_dev))
3206 		return dsa_lag_conduit_validate(info->upper_dev, extack);
3207 
3208 	NL_SET_ERR_MSG_MOD(extack,
3209 			   "DSA conduit cannot join unknown upper interfaces");
3210 	return notifier_from_errno(-EBUSY);
3211 }
3212 
3213 static int
dsa_lag_conduit_prechangelower_sanity_check(struct net_device * dev,struct netdev_notifier_changeupper_info * info)3214 dsa_lag_conduit_prechangelower_sanity_check(struct net_device *dev,
3215 					    struct netdev_notifier_changeupper_info *info)
3216 {
3217 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3218 	struct net_device *lag_dev = info->upper_dev;
3219 	struct net_device *lower;
3220 	struct list_head *iter;
3221 
3222 	if (!netdev_uses_dsa(lag_dev) || !netif_is_lag_master(lag_dev))
3223 		return NOTIFY_DONE;
3224 
3225 	if (!info->linking)
3226 		return NOTIFY_DONE;
3227 
3228 	if (!netdev_uses_dsa(dev)) {
3229 		NL_SET_ERR_MSG(extack,
3230 			       "Only DSA conduits can join a LAG DSA conduit");
3231 		return notifier_from_errno(-EINVAL);
3232 	}
3233 
3234 	netdev_for_each_lower_dev(lag_dev, lower, iter) {
3235 		if (!dsa_port_tree_same(dev->dsa_ptr, lower->dsa_ptr)) {
3236 			NL_SET_ERR_MSG(extack,
3237 				       "Interface is DSA conduit for a different switch tree than this LAG");
3238 			return notifier_from_errno(-EINVAL);
3239 		}
3240 
3241 		break;
3242 	}
3243 
3244 	return NOTIFY_DONE;
3245 }
3246 
3247 /* Don't allow bridging of DSA conduits, since the bridge layer rx_handler
3248  * prevents the DSA fake ethertype handler to be invoked, so we don't get the
3249  * chance to strip off and parse the DSA switch tag protocol header (the bridge
3250  * layer just returns RX_HANDLER_CONSUMED, stopping RX processing for these
3251  * frames).
3252  * The only case where that would not be an issue is when bridging can already
3253  * be offloaded, such as when the DSA conduit is itself a DSA or plain switchdev
3254  * port, and is bridged only with other ports from the same hardware device.
3255  */
3256 static int
dsa_bridge_prechangelower_sanity_check(struct net_device * new_lower,struct netdev_notifier_changeupper_info * info)3257 dsa_bridge_prechangelower_sanity_check(struct net_device *new_lower,
3258 				       struct netdev_notifier_changeupper_info *info)
3259 {
3260 	struct net_device *br = info->upper_dev;
3261 	struct netlink_ext_ack *extack;
3262 	struct net_device *lower;
3263 	struct list_head *iter;
3264 
3265 	if (!netif_is_bridge_master(br))
3266 		return NOTIFY_DONE;
3267 
3268 	if (!info->linking)
3269 		return NOTIFY_DONE;
3270 
3271 	extack = netdev_notifier_info_to_extack(&info->info);
3272 
3273 	netdev_for_each_lower_dev(br, lower, iter) {
3274 		if (!netdev_uses_dsa(new_lower) && !netdev_uses_dsa(lower))
3275 			continue;
3276 
3277 		if (!netdev_port_same_parent_id(lower, new_lower)) {
3278 			NL_SET_ERR_MSG(extack,
3279 				       "Cannot do software bridging with a DSA conduit");
3280 			return notifier_from_errno(-EINVAL);
3281 		}
3282 	}
3283 
3284 	return NOTIFY_DONE;
3285 }
3286 
dsa_tree_migrate_ports_from_lag_conduit(struct dsa_switch_tree * dst,struct net_device * lag_dev)3287 static void dsa_tree_migrate_ports_from_lag_conduit(struct dsa_switch_tree *dst,
3288 						    struct net_device *lag_dev)
3289 {
3290 	struct net_device *new_conduit = dsa_tree_find_first_conduit(dst);
3291 	struct dsa_port *dp;
3292 	int err;
3293 
3294 	dsa_tree_for_each_user_port(dp, dst) {
3295 		if (dsa_port_to_conduit(dp) != lag_dev)
3296 			continue;
3297 
3298 		err = dsa_user_change_conduit(dp->user, new_conduit, NULL);
3299 		if (err) {
3300 			netdev_err(dp->user,
3301 				   "failed to restore conduit to %s: %pe\n",
3302 				   new_conduit->name, ERR_PTR(err));
3303 		}
3304 	}
3305 }
3306 
dsa_conduit_lag_join(struct net_device * conduit,struct net_device * lag_dev,struct netdev_lag_upper_info * uinfo,struct netlink_ext_ack * extack)3307 static int dsa_conduit_lag_join(struct net_device *conduit,
3308 				struct net_device *lag_dev,
3309 				struct netdev_lag_upper_info *uinfo,
3310 				struct netlink_ext_ack *extack)
3311 {
3312 	struct dsa_port *cpu_dp = conduit->dsa_ptr;
3313 	struct dsa_switch_tree *dst = cpu_dp->dst;
3314 	struct dsa_port *dp;
3315 	int err;
3316 
3317 	err = dsa_conduit_lag_setup(lag_dev, cpu_dp, uinfo, extack);
3318 	if (err)
3319 		return err;
3320 
3321 	dsa_tree_for_each_user_port(dp, dst) {
3322 		if (dsa_port_to_conduit(dp) != conduit)
3323 			continue;
3324 
3325 		err = dsa_user_change_conduit(dp->user, lag_dev, extack);
3326 		if (err)
3327 			goto restore;
3328 	}
3329 
3330 	return 0;
3331 
3332 restore:
3333 	dsa_tree_for_each_user_port_continue_reverse(dp, dst) {
3334 		if (dsa_port_to_conduit(dp) != lag_dev)
3335 			continue;
3336 
3337 		err = dsa_user_change_conduit(dp->user, conduit, NULL);
3338 		if (err) {
3339 			netdev_err(dp->user,
3340 				   "failed to restore conduit to %s: %pe\n",
3341 				   conduit->name, ERR_PTR(err));
3342 		}
3343 	}
3344 
3345 	dsa_conduit_lag_teardown(lag_dev, conduit->dsa_ptr);
3346 
3347 	return err;
3348 }
3349 
dsa_conduit_lag_leave(struct net_device * conduit,struct net_device * lag_dev)3350 static void dsa_conduit_lag_leave(struct net_device *conduit,
3351 				  struct net_device *lag_dev)
3352 {
3353 	struct dsa_port *dp, *cpu_dp = lag_dev->dsa_ptr;
3354 	struct dsa_switch_tree *dst = cpu_dp->dst;
3355 	struct dsa_port *new_cpu_dp = NULL;
3356 	struct net_device *lower;
3357 	struct list_head *iter;
3358 
3359 	netdev_for_each_lower_dev(lag_dev, lower, iter) {
3360 		if (netdev_uses_dsa(lower)) {
3361 			new_cpu_dp = lower->dsa_ptr;
3362 			break;
3363 		}
3364 	}
3365 
3366 	if (new_cpu_dp) {
3367 		/* Update the CPU port of the user ports still under the LAG
3368 		 * so that dsa_port_to_conduit() continues to work properly
3369 		 */
3370 		dsa_tree_for_each_user_port(dp, dst)
3371 			if (dsa_port_to_conduit(dp) == lag_dev)
3372 				dp->cpu_dp = new_cpu_dp;
3373 
3374 		/* Update the index of the virtual CPU port to match the lowest
3375 		 * physical CPU port
3376 		 */
3377 		lag_dev->dsa_ptr = new_cpu_dp;
3378 		wmb();
3379 	} else {
3380 		/* If the LAG DSA conduit has no ports left, migrate back all
3381 		 * user ports to the first physical CPU port
3382 		 */
3383 		dsa_tree_migrate_ports_from_lag_conduit(dst, lag_dev);
3384 	}
3385 
3386 	/* This DSA conduit has left its LAG in any case, so let
3387 	 * the CPU port leave the hardware LAG as well
3388 	 */
3389 	dsa_conduit_lag_teardown(lag_dev, conduit->dsa_ptr);
3390 }
3391 
dsa_conduit_changeupper(struct net_device * dev,struct netdev_notifier_changeupper_info * info)3392 static int dsa_conduit_changeupper(struct net_device *dev,
3393 				   struct netdev_notifier_changeupper_info *info)
3394 {
3395 	struct netlink_ext_ack *extack;
3396 	int err = NOTIFY_DONE;
3397 
3398 	if (!netdev_uses_dsa(dev))
3399 		return err;
3400 
3401 	extack = netdev_notifier_info_to_extack(&info->info);
3402 
3403 	if (netif_is_lag_master(info->upper_dev)) {
3404 		if (info->linking) {
3405 			err = dsa_conduit_lag_join(dev, info->upper_dev,
3406 						   info->upper_info, extack);
3407 			err = notifier_from_errno(err);
3408 		} else {
3409 			dsa_conduit_lag_leave(dev, info->upper_dev);
3410 			err = NOTIFY_OK;
3411 		}
3412 	}
3413 
3414 	return err;
3415 }
3416 
dsa_user_netdevice_event(struct notifier_block * nb,unsigned long event,void * ptr)3417 static int dsa_user_netdevice_event(struct notifier_block *nb,
3418 				    unsigned long event, void *ptr)
3419 {
3420 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3421 
3422 	switch (event) {
3423 	case NETDEV_PRECHANGEUPPER: {
3424 		struct netdev_notifier_changeupper_info *info = ptr;
3425 		int err;
3426 
3427 		err = dsa_user_prechangeupper_sanity_check(dev, info);
3428 		if (notifier_to_errno(err))
3429 			return err;
3430 
3431 		err = dsa_conduit_prechangeupper_sanity_check(dev, info);
3432 		if (notifier_to_errno(err))
3433 			return err;
3434 
3435 		err = dsa_lag_conduit_prechangelower_sanity_check(dev, info);
3436 		if (notifier_to_errno(err))
3437 			return err;
3438 
3439 		err = dsa_bridge_prechangelower_sanity_check(dev, info);
3440 		if (notifier_to_errno(err))
3441 			return err;
3442 
3443 		err = dsa_user_prechangeupper(dev, ptr);
3444 		if (notifier_to_errno(err))
3445 			return err;
3446 
3447 		err = dsa_user_lag_prechangeupper(dev, ptr);
3448 		if (notifier_to_errno(err))
3449 			return err;
3450 
3451 		break;
3452 	}
3453 	case NETDEV_CHANGEUPPER: {
3454 		int err;
3455 
3456 		err = dsa_user_changeupper(dev, ptr);
3457 		if (notifier_to_errno(err))
3458 			return err;
3459 
3460 		err = dsa_user_lag_changeupper(dev, ptr);
3461 		if (notifier_to_errno(err))
3462 			return err;
3463 
3464 		err = dsa_conduit_changeupper(dev, ptr);
3465 		if (notifier_to_errno(err))
3466 			return err;
3467 
3468 		break;
3469 	}
3470 	case NETDEV_CHANGELOWERSTATE: {
3471 		struct netdev_notifier_changelowerstate_info *info = ptr;
3472 		struct dsa_port *dp;
3473 		int err = 0;
3474 
3475 		if (dsa_user_dev_check(dev)) {
3476 			dp = dsa_user_to_port(dev);
3477 
3478 			err = dsa_port_lag_change(dp, info->lower_state_info);
3479 		}
3480 
3481 		/* Mirror LAG port events on DSA conduits that are in
3482 		 * a LAG towards their respective switch CPU ports
3483 		 */
3484 		if (netdev_uses_dsa(dev)) {
3485 			dp = dev->dsa_ptr;
3486 
3487 			err = dsa_port_lag_change(dp, info->lower_state_info);
3488 		}
3489 
3490 		return notifier_from_errno(err);
3491 	}
3492 	case NETDEV_CHANGE:
3493 	case NETDEV_UP: {
3494 		/* Track state of conduit port.
3495 		 * DSA driver may require the conduit port (and indirectly
3496 		 * the tagger) to be available for some special operation.
3497 		 */
3498 		if (netdev_uses_dsa(dev)) {
3499 			struct dsa_port *cpu_dp = dev->dsa_ptr;
3500 			struct dsa_switch_tree *dst = cpu_dp->ds->dst;
3501 
3502 			/* Track when the conduit port is UP */
3503 			dsa_tree_conduit_oper_state_change(dst, dev,
3504 							   netif_oper_up(dev));
3505 
3506 			/* Track when the conduit port is ready and can accept
3507 			 * packet.
3508 			 * NETDEV_UP event is not enough to flag a port as ready.
3509 			 * We also have to wait for linkwatch_do_dev to dev_activate
3510 			 * and emit a NETDEV_CHANGE event.
3511 			 * We check if a conduit port is ready by checking if the dev
3512 			 * have a qdisc assigned and is not noop.
3513 			 */
3514 			dsa_tree_conduit_admin_state_change(dst, dev,
3515 							    !qdisc_tx_is_noop(dev));
3516 
3517 			return NOTIFY_OK;
3518 		}
3519 
3520 		return NOTIFY_DONE;
3521 	}
3522 	case NETDEV_GOING_DOWN: {
3523 		struct dsa_port *dp, *cpu_dp;
3524 		struct dsa_switch_tree *dst;
3525 		LIST_HEAD(close_list);
3526 
3527 		if (!netdev_uses_dsa(dev))
3528 			return NOTIFY_DONE;
3529 
3530 		cpu_dp = dev->dsa_ptr;
3531 		dst = cpu_dp->ds->dst;
3532 
3533 		dsa_tree_conduit_admin_state_change(dst, dev, false);
3534 
3535 		list_for_each_entry(dp, &dst->ports, list) {
3536 			if (!dsa_port_is_user(dp))
3537 				continue;
3538 
3539 			if (dp->cpu_dp != cpu_dp)
3540 				continue;
3541 
3542 			list_add(&dp->user->close_list, &close_list);
3543 		}
3544 
3545 		dev_close_many(&close_list, true);
3546 
3547 		return NOTIFY_OK;
3548 	}
3549 	default:
3550 		break;
3551 	}
3552 
3553 	return NOTIFY_DONE;
3554 }
3555 
3556 static void
dsa_fdb_offload_notify(struct dsa_switchdev_event_work * switchdev_work)3557 dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
3558 {
3559 	struct switchdev_notifier_fdb_info info = {};
3560 
3561 	info.addr = switchdev_work->addr;
3562 	info.vid = switchdev_work->vid;
3563 	info.offloaded = true;
3564 	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
3565 				 switchdev_work->orig_dev, &info.info, NULL);
3566 }
3567 
dsa_user_switchdev_event_work(struct work_struct * work)3568 static void dsa_user_switchdev_event_work(struct work_struct *work)
3569 {
3570 	struct dsa_switchdev_event_work *switchdev_work =
3571 		container_of(work, struct dsa_switchdev_event_work, work);
3572 	const unsigned char *addr = switchdev_work->addr;
3573 	struct net_device *dev = switchdev_work->dev;
3574 	u16 vid = switchdev_work->vid;
3575 	struct dsa_switch *ds;
3576 	struct dsa_port *dp;
3577 	int err;
3578 
3579 	dp = dsa_user_to_port(dev);
3580 	ds = dp->ds;
3581 
3582 	switch (switchdev_work->event) {
3583 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3584 		if (switchdev_work->host_addr)
3585 			err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
3586 		else if (dp->lag)
3587 			err = dsa_port_lag_fdb_add(dp, addr, vid);
3588 		else
3589 			err = dsa_port_fdb_add(dp, addr, vid);
3590 		if (err) {
3591 			dev_err(ds->dev,
3592 				"port %d failed to add %pM vid %d to fdb: %d\n",
3593 				dp->index, addr, vid, err);
3594 			break;
3595 		}
3596 		dsa_fdb_offload_notify(switchdev_work);
3597 		break;
3598 
3599 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3600 		if (switchdev_work->host_addr)
3601 			err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
3602 		else if (dp->lag)
3603 			err = dsa_port_lag_fdb_del(dp, addr, vid);
3604 		else
3605 			err = dsa_port_fdb_del(dp, addr, vid);
3606 		if (err) {
3607 			dev_err(ds->dev,
3608 				"port %d failed to delete %pM vid %d from fdb: %d\n",
3609 				dp->index, addr, vid, err);
3610 		}
3611 
3612 		break;
3613 	}
3614 
3615 	kfree(switchdev_work);
3616 }
3617 
dsa_foreign_dev_check(const struct net_device * dev,const struct net_device * foreign_dev)3618 static bool dsa_foreign_dev_check(const struct net_device *dev,
3619 				  const struct net_device *foreign_dev)
3620 {
3621 	const struct dsa_port *dp = dsa_user_to_port(dev);
3622 	struct dsa_switch_tree *dst = dp->ds->dst;
3623 
3624 	if (netif_is_bridge_master(foreign_dev))
3625 		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
3626 
3627 	if (netif_is_bridge_port(foreign_dev))
3628 		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
3629 
3630 	/* Everything else is foreign */
3631 	return true;
3632 }
3633 
dsa_user_fdb_event(struct net_device * dev,struct net_device * orig_dev,unsigned long event,const void * ctx,const struct switchdev_notifier_fdb_info * fdb_info)3634 static int dsa_user_fdb_event(struct net_device *dev,
3635 			      struct net_device *orig_dev,
3636 			      unsigned long event, const void *ctx,
3637 			      const struct switchdev_notifier_fdb_info *fdb_info)
3638 {
3639 	struct dsa_switchdev_event_work *switchdev_work;
3640 	struct dsa_port *dp = dsa_user_to_port(dev);
3641 	bool host_addr = fdb_info->is_local;
3642 	struct dsa_switch *ds = dp->ds;
3643 
3644 	if (ctx && ctx != dp)
3645 		return 0;
3646 
3647 	if (!dp->bridge)
3648 		return 0;
3649 
3650 	if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
3651 		if (dsa_port_offloads_bridge_port(dp, orig_dev))
3652 			return 0;
3653 
3654 		/* FDB entries learned by the software bridge or by foreign
3655 		 * bridge ports should be installed as host addresses only if
3656 		 * the driver requests assisted learning.
3657 		 */
3658 		if (!ds->assisted_learning_on_cpu_port)
3659 			return 0;
3660 	}
3661 
3662 	/* Also treat FDB entries on foreign interfaces bridged with us as host
3663 	 * addresses.
3664 	 */
3665 	if (dsa_foreign_dev_check(dev, orig_dev))
3666 		host_addr = true;
3667 
3668 	/* Check early that we're not doing work in vain.
3669 	 * Host addresses on LAG ports still require regular FDB ops,
3670 	 * since the CPU port isn't in a LAG.
3671 	 */
3672 	if (dp->lag && !host_addr) {
3673 		if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
3674 			return -EOPNOTSUPP;
3675 	} else {
3676 		if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
3677 			return -EOPNOTSUPP;
3678 	}
3679 
3680 	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3681 	if (!switchdev_work)
3682 		return -ENOMEM;
3683 
3684 	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
3685 		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
3686 		   orig_dev->name, fdb_info->addr, fdb_info->vid,
3687 		   host_addr ? " as host address" : "");
3688 
3689 	INIT_WORK(&switchdev_work->work, dsa_user_switchdev_event_work);
3690 	switchdev_work->event = event;
3691 	switchdev_work->dev = dev;
3692 	switchdev_work->orig_dev = orig_dev;
3693 
3694 	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
3695 	switchdev_work->vid = fdb_info->vid;
3696 	switchdev_work->host_addr = host_addr;
3697 
3698 	dsa_schedule_work(&switchdev_work->work);
3699 
3700 	return 0;
3701 }
3702 
3703 /* Called under rcu_read_lock() */
dsa_user_switchdev_event(struct notifier_block * unused,unsigned long event,void * ptr)3704 static int dsa_user_switchdev_event(struct notifier_block *unused,
3705 				    unsigned long event, void *ptr)
3706 {
3707 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3708 	int err;
3709 
3710 	switch (event) {
3711 	case SWITCHDEV_PORT_ATTR_SET:
3712 		err = switchdev_handle_port_attr_set(dev, ptr,
3713 						     dsa_user_dev_check,
3714 						     dsa_user_port_attr_set);
3715 		return notifier_from_errno(err);
3716 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3717 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3718 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
3719 							   dsa_user_dev_check,
3720 							   dsa_foreign_dev_check,
3721 							   dsa_user_fdb_event);
3722 		return notifier_from_errno(err);
3723 	default:
3724 		return NOTIFY_DONE;
3725 	}
3726 
3727 	return NOTIFY_OK;
3728 }
3729 
dsa_user_switchdev_blocking_event(struct notifier_block * unused,unsigned long event,void * ptr)3730 static int dsa_user_switchdev_blocking_event(struct notifier_block *unused,
3731 					     unsigned long event, void *ptr)
3732 {
3733 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3734 	int err;
3735 
3736 	switch (event) {
3737 	case SWITCHDEV_PORT_OBJ_ADD:
3738 		err = switchdev_handle_port_obj_add_foreign(dev, ptr,
3739 							    dsa_user_dev_check,
3740 							    dsa_foreign_dev_check,
3741 							    dsa_user_port_obj_add);
3742 		return notifier_from_errno(err);
3743 	case SWITCHDEV_PORT_OBJ_DEL:
3744 		err = switchdev_handle_port_obj_del_foreign(dev, ptr,
3745 							    dsa_user_dev_check,
3746 							    dsa_foreign_dev_check,
3747 							    dsa_user_port_obj_del);
3748 		return notifier_from_errno(err);
3749 	case SWITCHDEV_PORT_ATTR_SET:
3750 		err = switchdev_handle_port_attr_set(dev, ptr,
3751 						     dsa_user_dev_check,
3752 						     dsa_user_port_attr_set);
3753 		return notifier_from_errno(err);
3754 	}
3755 
3756 	return NOTIFY_DONE;
3757 }
3758 
3759 static struct notifier_block dsa_user_nb __read_mostly = {
3760 	.notifier_call  = dsa_user_netdevice_event,
3761 };
3762 
3763 struct notifier_block dsa_user_switchdev_notifier = {
3764 	.notifier_call = dsa_user_switchdev_event,
3765 };
3766 
3767 struct notifier_block dsa_user_switchdev_blocking_notifier = {
3768 	.notifier_call = dsa_user_switchdev_blocking_event,
3769 };
3770 
dsa_user_register_notifier(void)3771 int dsa_user_register_notifier(void)
3772 {
3773 	struct notifier_block *nb;
3774 	int err;
3775 
3776 	err = register_netdevice_notifier(&dsa_user_nb);
3777 	if (err)
3778 		return err;
3779 
3780 	err = register_switchdev_notifier(&dsa_user_switchdev_notifier);
3781 	if (err)
3782 		goto err_switchdev_nb;
3783 
3784 	nb = &dsa_user_switchdev_blocking_notifier;
3785 	err = register_switchdev_blocking_notifier(nb);
3786 	if (err)
3787 		goto err_switchdev_blocking_nb;
3788 
3789 	return 0;
3790 
3791 err_switchdev_blocking_nb:
3792 	unregister_switchdev_notifier(&dsa_user_switchdev_notifier);
3793 err_switchdev_nb:
3794 	unregister_netdevice_notifier(&dsa_user_nb);
3795 	return err;
3796 }
3797 
dsa_user_unregister_notifier(void)3798 void dsa_user_unregister_notifier(void)
3799 {
3800 	struct notifier_block *nb;
3801 	int err;
3802 
3803 	nb = &dsa_user_switchdev_blocking_notifier;
3804 	err = unregister_switchdev_blocking_notifier(nb);
3805 	if (err)
3806 		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3807 
3808 	err = unregister_switchdev_notifier(&dsa_user_switchdev_notifier);
3809 	if (err)
3810 		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3811 
3812 	err = unregister_netdevice_notifier(&dsa_user_nb);
3813 	if (err)
3814 		pr_err("DSA: failed to unregister user notifier (%d)\n", err);
3815 }
3816