Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
   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
  34struct 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
  47enum dsa_standalone_event {
  48	DSA_UC_ADD,
  49	DSA_UC_DEL,
  50	DSA_MC_ADD,
  51	DSA_MC_DEL,
  52};
  53
  54struct 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
  62struct dsa_host_vlan_rx_filtering_ctx {
  63	struct net_device *dev;
  64	const unsigned char *addr;
  65	enum dsa_standalone_event event;
  66};
  67
  68static 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
  75static 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
  82static 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
 143static 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
 166static 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
 174static 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
 196static 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
 216static 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
 236static 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
 256static 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
 276void 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
 297void 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 ***************************************************/
 319static 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
 329static 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
 339void 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 ****************************************************/
 353static int dsa_user_get_iflink(const struct net_device *dev)
 354{
 355	return READ_ONCE(dsa_user_to_conduit(dev)->ifindex);
 356}
 357
 358int 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
 379del_host_addr:
 380	if (dsa_switch_supports_uc_filtering(ds))
 381		dsa_port_standalone_host_fdb_del(dp, addr, 0);
 382out:
 383	return err;
 384}
 385
 386void 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
 399static 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
 421out_del_host_uc:
 422	dsa_user_host_uc_uninstall(dev);
 423out:
 424	return err;
 425}
 426
 427static 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
 438static 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
 447static 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
 465static 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
 471static 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
 500out_change_dev_addr:
 501	eth_hw_addr_set(dev, addr->sa_data);
 502
 503	return 0;
 504}
 505
 506struct dsa_user_dump_ctx {
 507	struct net_device *dev;
 508	struct sk_buff *skb;
 509	struct netlink_callback *cb;
 510	int idx;
 511};
 512
 513static int
 514dsa_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
 548skip:
 549	dump->idx++;
 550	return 0;
 551
 552nla_put_failure:
 553	nlmsg_cancel(dump->skb, nlh);
 554	return -EMSGSIZE;
 555}
 556
 557static int
 558dsa_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
 577static 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
 598static 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() */
 668static int
 669dsa_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
 689static 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 */
 724static 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
 750static 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
 800static 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
 814static 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
 832static 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
 881static 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
 894static 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
 908netdev_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}
 924EXPORT_SYMBOL_GPL(dsa_enqueue_skb);
 925
 926static 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
 964static 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
 972static 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
 983static void
 984dsa_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
 993static 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
1000static 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
1014static 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
1026static 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
1038static 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		ethtool_puts(&data, "tx_packets");
1046		ethtool_puts(&data, "tx_bytes");
1047		ethtool_puts(&data, "rx_packets");
1048		ethtool_puts(&data, "rx_bytes");
1049		if (ds->ops->get_strings)
1050			ds->ops->get_strings(ds, dp->index, stringset, data);
1051	} else if (stringset ==  ETH_SS_TEST) {
1052		net_selftest_get_strings(data);
1053	}
1054
1055}
1056
1057static void dsa_user_get_ethtool_stats(struct net_device *dev,
1058				       struct ethtool_stats *stats,
1059				       uint64_t *data)
1060{
1061	struct dsa_port *dp = dsa_user_to_port(dev);
1062	struct dsa_switch *ds = dp->ds;
1063	struct pcpu_sw_netstats *s;
1064	unsigned int start;
1065	int i;
1066
1067	for_each_possible_cpu(i) {
1068		u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
1069
1070		s = per_cpu_ptr(dev->tstats, i);
1071		do {
1072			start = u64_stats_fetch_begin(&s->syncp);
1073			tx_packets = u64_stats_read(&s->tx_packets);
1074			tx_bytes = u64_stats_read(&s->tx_bytes);
1075			rx_packets = u64_stats_read(&s->rx_packets);
1076			rx_bytes = u64_stats_read(&s->rx_bytes);
1077		} while (u64_stats_fetch_retry(&s->syncp, start));
1078		data[0] += tx_packets;
1079		data[1] += tx_bytes;
1080		data[2] += rx_packets;
1081		data[3] += rx_bytes;
1082	}
1083	if (ds->ops->get_ethtool_stats)
1084		ds->ops->get_ethtool_stats(ds, dp->index, data + 4);
1085}
1086
1087static int dsa_user_get_sset_count(struct net_device *dev, int sset)
1088{
1089	struct dsa_port *dp = dsa_user_to_port(dev);
1090	struct dsa_switch *ds = dp->ds;
1091
1092	if (sset == ETH_SS_STATS) {
1093		int count = 0;
1094
1095		if (ds->ops->get_sset_count) {
1096			count = ds->ops->get_sset_count(ds, dp->index, sset);
1097			if (count < 0)
1098				return count;
1099		}
1100
1101		return count + 4;
1102	} else if (sset ==  ETH_SS_TEST) {
1103		return net_selftest_get_count();
1104	}
1105
1106	return -EOPNOTSUPP;
1107}
1108
1109static void dsa_user_get_eth_phy_stats(struct net_device *dev,
1110				       struct ethtool_eth_phy_stats *phy_stats)
1111{
1112	struct dsa_port *dp = dsa_user_to_port(dev);
1113	struct dsa_switch *ds = dp->ds;
1114
1115	if (ds->ops->get_eth_phy_stats)
1116		ds->ops->get_eth_phy_stats(ds, dp->index, phy_stats);
1117}
1118
1119static void dsa_user_get_eth_mac_stats(struct net_device *dev,
1120				       struct ethtool_eth_mac_stats *mac_stats)
1121{
1122	struct dsa_port *dp = dsa_user_to_port(dev);
1123	struct dsa_switch *ds = dp->ds;
1124
1125	if (ds->ops->get_eth_mac_stats)
1126		ds->ops->get_eth_mac_stats(ds, dp->index, mac_stats);
1127}
1128
1129static void
1130dsa_user_get_eth_ctrl_stats(struct net_device *dev,
1131			    struct ethtool_eth_ctrl_stats *ctrl_stats)
1132{
1133	struct dsa_port *dp = dsa_user_to_port(dev);
1134	struct dsa_switch *ds = dp->ds;
1135
1136	if (ds->ops->get_eth_ctrl_stats)
1137		ds->ops->get_eth_ctrl_stats(ds, dp->index, ctrl_stats);
1138}
1139
1140static void
1141dsa_user_get_rmon_stats(struct net_device *dev,
1142			struct ethtool_rmon_stats *rmon_stats,
1143			const struct ethtool_rmon_hist_range **ranges)
1144{
1145	struct dsa_port *dp = dsa_user_to_port(dev);
1146	struct dsa_switch *ds = dp->ds;
1147
1148	if (ds->ops->get_rmon_stats)
1149		ds->ops->get_rmon_stats(ds, dp->index, rmon_stats, ranges);
1150}
1151
1152static void dsa_user_net_selftest(struct net_device *ndev,
1153				  struct ethtool_test *etest, u64 *buf)
1154{
1155	struct dsa_port *dp = dsa_user_to_port(ndev);
1156	struct dsa_switch *ds = dp->ds;
1157
1158	if (ds->ops->self_test) {
1159		ds->ops->self_test(ds, dp->index, etest, buf);
1160		return;
1161	}
1162
1163	net_selftest(ndev, etest, buf);
1164}
1165
1166static int dsa_user_get_mm(struct net_device *dev,
1167			   struct ethtool_mm_state *state)
1168{
1169	struct dsa_port *dp = dsa_user_to_port(dev);
1170	struct dsa_switch *ds = dp->ds;
1171
1172	if (!ds->ops->get_mm)
1173		return -EOPNOTSUPP;
1174
1175	return ds->ops->get_mm(ds, dp->index, state);
1176}
1177
1178static int dsa_user_set_mm(struct net_device *dev, struct ethtool_mm_cfg *cfg,
1179			   struct netlink_ext_ack *extack)
1180{
1181	struct dsa_port *dp = dsa_user_to_port(dev);
1182	struct dsa_switch *ds = dp->ds;
1183
1184	if (!ds->ops->set_mm)
1185		return -EOPNOTSUPP;
1186
1187	return ds->ops->set_mm(ds, dp->index, cfg, extack);
1188}
1189
1190static void dsa_user_get_mm_stats(struct net_device *dev,
1191				  struct ethtool_mm_stats *stats)
1192{
1193	struct dsa_port *dp = dsa_user_to_port(dev);
1194	struct dsa_switch *ds = dp->ds;
1195
1196	if (ds->ops->get_mm_stats)
1197		ds->ops->get_mm_stats(ds, dp->index, stats);
1198}
1199
1200static void dsa_user_get_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1201{
1202	struct dsa_port *dp = dsa_user_to_port(dev);
1203	struct dsa_switch *ds = dp->ds;
1204
1205	phylink_ethtool_get_wol(dp->pl, w);
1206
1207	if (ds->ops->get_wol)
1208		ds->ops->get_wol(ds, dp->index, w);
1209}
1210
1211static int dsa_user_set_wol(struct net_device *dev, struct ethtool_wolinfo *w)
1212{
1213	struct dsa_port *dp = dsa_user_to_port(dev);
1214	struct dsa_switch *ds = dp->ds;
1215	int ret = -EOPNOTSUPP;
1216
1217	phylink_ethtool_set_wol(dp->pl, w);
1218
1219	if (ds->ops->set_wol)
1220		ret = ds->ops->set_wol(ds, dp->index, w);
1221
1222	return ret;
1223}
1224
1225static int dsa_user_set_eee(struct net_device *dev, struct ethtool_keee *e)
1226{
1227	struct dsa_port *dp = dsa_user_to_port(dev);
1228	struct dsa_switch *ds = dp->ds;
1229	int ret;
1230
1231	/* Port's PHY and MAC both need to be EEE capable */
1232	if (!dev->phydev || !dp->pl)
1233		return -ENODEV;
1234
1235	if (!ds->ops->set_mac_eee)
1236		return -EOPNOTSUPP;
1237
1238	ret = ds->ops->set_mac_eee(ds, dp->index, e);
1239	if (ret)
1240		return ret;
1241
1242	return phylink_ethtool_set_eee(dp->pl, e);
1243}
1244
1245static int dsa_user_get_eee(struct net_device *dev, struct ethtool_keee *e)
1246{
1247	struct dsa_port *dp = dsa_user_to_port(dev);
1248	struct dsa_switch *ds = dp->ds;
1249	int ret;
1250
1251	/* Port's PHY and MAC both need to be EEE capable */
1252	if (!dev->phydev || !dp->pl)
1253		return -ENODEV;
1254
1255	if (!ds->ops->get_mac_eee)
1256		return -EOPNOTSUPP;
1257
1258	ret = ds->ops->get_mac_eee(ds, dp->index, e);
1259	if (ret)
1260		return ret;
1261
1262	return phylink_ethtool_get_eee(dp->pl, e);
1263}
1264
1265static int dsa_user_get_link_ksettings(struct net_device *dev,
1266				       struct ethtool_link_ksettings *cmd)
1267{
1268	struct dsa_port *dp = dsa_user_to_port(dev);
1269
1270	return phylink_ethtool_ksettings_get(dp->pl, cmd);
1271}
1272
1273static int dsa_user_set_link_ksettings(struct net_device *dev,
1274				       const struct ethtool_link_ksettings *cmd)
1275{
1276	struct dsa_port *dp = dsa_user_to_port(dev);
1277
1278	return phylink_ethtool_ksettings_set(dp->pl, cmd);
1279}
1280
1281static void dsa_user_get_pause_stats(struct net_device *dev,
1282				     struct ethtool_pause_stats *pause_stats)
1283{
1284	struct dsa_port *dp = dsa_user_to_port(dev);
1285	struct dsa_switch *ds = dp->ds;
1286
1287	if (ds->ops->get_pause_stats)
1288		ds->ops->get_pause_stats(ds, dp->index, pause_stats);
1289}
1290
1291static void dsa_user_get_pauseparam(struct net_device *dev,
1292				    struct ethtool_pauseparam *pause)
1293{
1294	struct dsa_port *dp = dsa_user_to_port(dev);
1295
1296	phylink_ethtool_get_pauseparam(dp->pl, pause);
1297}
1298
1299static int dsa_user_set_pauseparam(struct net_device *dev,
1300				   struct ethtool_pauseparam *pause)
1301{
1302	struct dsa_port *dp = dsa_user_to_port(dev);
1303
1304	return phylink_ethtool_set_pauseparam(dp->pl, pause);
1305}
1306
1307#ifdef CONFIG_NET_POLL_CONTROLLER
1308static int dsa_user_netpoll_setup(struct net_device *dev)
1309{
1310	struct net_device *conduit = dsa_user_to_conduit(dev);
1311	struct dsa_user_priv *p = netdev_priv(dev);
1312	struct netpoll *netpoll;
1313	int err = 0;
1314
1315	netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
1316	if (!netpoll)
1317		return -ENOMEM;
1318
1319	err = __netpoll_setup(netpoll, conduit);
1320	if (err) {
1321		kfree(netpoll);
1322		goto out;
1323	}
1324
1325	p->netpoll = netpoll;
1326out:
1327	return err;
1328}
1329
1330static void dsa_user_netpoll_cleanup(struct net_device *dev)
1331{
1332	struct dsa_user_priv *p = netdev_priv(dev);
1333	struct netpoll *netpoll = p->netpoll;
1334
1335	if (!netpoll)
1336		return;
1337
1338	p->netpoll = NULL;
1339
1340	__netpoll_free(netpoll);
1341}
1342
1343static void dsa_user_poll_controller(struct net_device *dev)
1344{
1345}
1346#endif
1347
1348static struct dsa_mall_tc_entry *
1349dsa_user_mall_tc_entry_find(struct net_device *dev, unsigned long cookie)
1350{
1351	struct dsa_user_priv *p = netdev_priv(dev);
1352	struct dsa_mall_tc_entry *mall_tc_entry;
1353
1354	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list)
1355		if (mall_tc_entry->cookie == cookie)
1356			return mall_tc_entry;
1357
1358	return NULL;
1359}
1360
1361static int
1362dsa_user_add_cls_matchall_mirred(struct net_device *dev,
1363				 struct tc_cls_matchall_offload *cls,
1364				 bool ingress, bool ingress_target)
1365{
1366	struct netlink_ext_ack *extack = cls->common.extack;
1367	struct dsa_port *dp = dsa_user_to_port(dev);
1368	struct dsa_user_priv *p = netdev_priv(dev);
1369	struct dsa_mall_mirror_tc_entry *mirror;
1370	struct dsa_mall_tc_entry *mall_tc_entry;
1371	struct dsa_switch *ds = dp->ds;
1372	struct flow_action_entry *act;
1373	struct dsa_port *to_dp;
1374	int err;
1375
1376	if (cls->common.protocol != htons(ETH_P_ALL)) {
1377		NL_SET_ERR_MSG_MOD(extack,
1378				   "Can only offload \"protocol all\" matchall filter");
1379		return -EOPNOTSUPP;
1380	}
1381
1382	if (!ds->ops->port_mirror_add) {
1383		NL_SET_ERR_MSG_MOD(extack,
1384				   "Switch does not support mirroring operation");
1385		return -EOPNOTSUPP;
1386	}
1387
1388	if (!flow_action_basic_hw_stats_check(&cls->rule->action, extack))
1389		return -EOPNOTSUPP;
1390
1391	act = &cls->rule->action.entries[0];
1392
1393	if (!act->dev)
1394		return -EINVAL;
1395
1396	if (dsa_user_dev_check(act->dev)) {
1397		if (ingress_target) {
1398			/* We can only fulfill this using software assist */
1399			if (cls->common.skip_sw) {
1400				NL_SET_ERR_MSG_MOD(extack,
1401						   "Can only mirred to ingress of DSA user port if filter also runs in software");
1402				return -EOPNOTSUPP;
1403			}
1404			to_dp = dp->cpu_dp;
1405		} else {
1406			to_dp = dsa_user_to_port(act->dev);
1407		}
1408	} else {
1409		/* Handle mirroring to foreign target ports as a mirror towards
1410		 * the CPU. The software tc rule will take the packets from
1411		 * there.
1412		 */
1413		if (cls->common.skip_sw) {
1414			NL_SET_ERR_MSG_MOD(extack,
1415					   "Can only mirred to CPU if filter also runs in software");
1416			return -EOPNOTSUPP;
1417		}
1418		to_dp = dp->cpu_dp;
1419	}
1420
1421	if (dp->ds != to_dp->ds) {
1422		NL_SET_ERR_MSG_MOD(extack,
1423				   "Cross-chip mirroring not implemented");
1424		return -EOPNOTSUPP;
1425	}
1426
1427	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1428	if (!mall_tc_entry)
1429		return -ENOMEM;
1430
1431	mall_tc_entry->cookie = cls->cookie;
1432	mall_tc_entry->type = DSA_PORT_MALL_MIRROR;
1433	mirror = &mall_tc_entry->mirror;
1434	mirror->to_local_port = to_dp->index;
1435	mirror->ingress = ingress;
1436
1437	err = ds->ops->port_mirror_add(ds, dp->index, mirror, ingress, extack);
1438	if (err) {
1439		kfree(mall_tc_entry);
1440		return err;
1441	}
1442
1443	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1444
1445	return err;
1446}
1447
1448static int
1449dsa_user_add_cls_matchall_police(struct net_device *dev,
1450				 struct tc_cls_matchall_offload *cls,
1451				 bool ingress)
1452{
1453	struct netlink_ext_ack *extack = cls->common.extack;
1454	struct dsa_port *dp = dsa_user_to_port(dev);
1455	struct dsa_user_priv *p = netdev_priv(dev);
1456	struct dsa_mall_policer_tc_entry *policer;
1457	struct dsa_mall_tc_entry *mall_tc_entry;
1458	struct dsa_switch *ds = dp->ds;
1459	struct flow_action_entry *act;
1460	int err;
1461
1462	if (!ds->ops->port_policer_add) {
1463		NL_SET_ERR_MSG_MOD(extack,
1464				   "Policing offload not implemented");
1465		return -EOPNOTSUPP;
1466	}
1467
1468	if (!ingress) {
1469		NL_SET_ERR_MSG_MOD(extack,
1470				   "Only supported on ingress qdisc");
1471		return -EOPNOTSUPP;
1472	}
1473
1474	if (!flow_action_basic_hw_stats_check(&cls->rule->action, extack))
1475		return -EOPNOTSUPP;
1476
1477	list_for_each_entry(mall_tc_entry, &p->mall_tc_list, list) {
1478		if (mall_tc_entry->type == DSA_PORT_MALL_POLICER) {
1479			NL_SET_ERR_MSG_MOD(extack,
1480					   "Only one port policer allowed");
1481			return -EEXIST;
1482		}
1483	}
1484
1485	act = &cls->rule->action.entries[0];
1486
1487	mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1488	if (!mall_tc_entry)
1489		return -ENOMEM;
1490
1491	mall_tc_entry->cookie = cls->cookie;
1492	mall_tc_entry->type = DSA_PORT_MALL_POLICER;
1493	policer = &mall_tc_entry->policer;
1494	policer->rate_bytes_per_sec = act->police.rate_bytes_ps;
1495	policer->burst = act->police.burst;
1496
1497	err = ds->ops->port_policer_add(ds, dp->index, policer);
1498	if (err) {
1499		kfree(mall_tc_entry);
1500		return err;
1501	}
1502
1503	list_add_tail(&mall_tc_entry->list, &p->mall_tc_list);
1504
1505	return err;
1506}
1507
1508static int dsa_user_add_cls_matchall(struct net_device *dev,
1509				     struct tc_cls_matchall_offload *cls,
1510				     bool ingress)
1511{
1512	const struct flow_action *action = &cls->rule->action;
1513	struct netlink_ext_ack *extack = cls->common.extack;
1514
1515	if (!flow_offload_has_one_action(action)) {
1516		NL_SET_ERR_MSG_MOD(extack,
1517				   "Cannot offload matchall filter with more than one action");
1518		return -EOPNOTSUPP;
1519	}
1520
1521	switch (action->entries[0].id) {
1522	case FLOW_ACTION_MIRRED:
1523		return dsa_user_add_cls_matchall_mirred(dev, cls, ingress,
1524							false);
1525	case FLOW_ACTION_MIRRED_INGRESS:
1526		return dsa_user_add_cls_matchall_mirred(dev, cls, ingress,
1527							true);
1528	case FLOW_ACTION_POLICE:
1529		return dsa_user_add_cls_matchall_police(dev, cls, ingress);
1530	default:
1531		NL_SET_ERR_MSG_MOD(extack, "Unknown action");
1532		break;
1533	}
1534
1535	return -EOPNOTSUPP;
1536}
1537
1538static void dsa_user_del_cls_matchall(struct net_device *dev,
1539				      struct tc_cls_matchall_offload *cls)
1540{
1541	struct dsa_port *dp = dsa_user_to_port(dev);
1542	struct dsa_mall_tc_entry *mall_tc_entry;
1543	struct dsa_switch *ds = dp->ds;
1544
1545	mall_tc_entry = dsa_user_mall_tc_entry_find(dev, cls->cookie);
1546	if (!mall_tc_entry)
1547		return;
1548
1549	list_del(&mall_tc_entry->list);
1550
1551	switch (mall_tc_entry->type) {
1552	case DSA_PORT_MALL_MIRROR:
1553		if (ds->ops->port_mirror_del)
1554			ds->ops->port_mirror_del(ds, dp->index,
1555						 &mall_tc_entry->mirror);
1556		break;
1557	case DSA_PORT_MALL_POLICER:
1558		if (ds->ops->port_policer_del)
1559			ds->ops->port_policer_del(ds, dp->index);
1560		break;
1561	default:
1562		WARN_ON(1);
1563	}
1564
1565	kfree(mall_tc_entry);
1566}
1567
1568static int dsa_user_setup_tc_cls_matchall(struct net_device *dev,
1569					  struct tc_cls_matchall_offload *cls,
1570					  bool ingress)
1571{
1572	if (cls->common.chain_index)
1573		return -EOPNOTSUPP;
1574
1575	switch (cls->command) {
1576	case TC_CLSMATCHALL_REPLACE:
1577		return dsa_user_add_cls_matchall(dev, cls, ingress);
1578	case TC_CLSMATCHALL_DESTROY:
1579		dsa_user_del_cls_matchall(dev, cls);
1580		return 0;
1581	default:
1582		return -EOPNOTSUPP;
1583	}
1584}
1585
1586static int dsa_user_add_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_add)
1595		return -EOPNOTSUPP;
1596
1597	return ds->ops->cls_flower_add(ds, port, cls, ingress);
1598}
1599
1600static int dsa_user_del_cls_flower(struct net_device *dev,
1601				   struct flow_cls_offload *cls,
1602				   bool ingress)
1603{
1604	struct dsa_port *dp = dsa_user_to_port(dev);
1605	struct dsa_switch *ds = dp->ds;
1606	int port = dp->index;
1607
1608	if (!ds->ops->cls_flower_del)
1609		return -EOPNOTSUPP;
1610
1611	return ds->ops->cls_flower_del(ds, port, cls, ingress);
1612}
1613
1614static int dsa_user_stats_cls_flower(struct net_device *dev,
1615				     struct flow_cls_offload *cls,
1616				     bool ingress)
1617{
1618	struct dsa_port *dp = dsa_user_to_port(dev);
1619	struct dsa_switch *ds = dp->ds;
1620	int port = dp->index;
1621
1622	if (!ds->ops->cls_flower_stats)
1623		return -EOPNOTSUPP;
1624
1625	return ds->ops->cls_flower_stats(ds, port, cls, ingress);
1626}
1627
1628static int dsa_user_setup_tc_cls_flower(struct net_device *dev,
1629					struct flow_cls_offload *cls,
1630					bool ingress)
1631{
1632	switch (cls->command) {
1633	case FLOW_CLS_REPLACE:
1634		return dsa_user_add_cls_flower(dev, cls, ingress);
1635	case FLOW_CLS_DESTROY:
1636		return dsa_user_del_cls_flower(dev, cls, ingress);
1637	case FLOW_CLS_STATS:
1638		return dsa_user_stats_cls_flower(dev, cls, ingress);
1639	default:
1640		return -EOPNOTSUPP;
1641	}
1642}
1643
1644static int dsa_user_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
1645				      void *cb_priv, bool ingress)
1646{
1647	struct net_device *dev = cb_priv;
1648
1649	if (!tc_can_offload(dev))
1650		return -EOPNOTSUPP;
1651
1652	switch (type) {
1653	case TC_SETUP_CLSMATCHALL:
1654		return dsa_user_setup_tc_cls_matchall(dev, type_data, ingress);
1655	case TC_SETUP_CLSFLOWER:
1656		return dsa_user_setup_tc_cls_flower(dev, type_data, ingress);
1657	default:
1658		return -EOPNOTSUPP;
1659	}
1660}
1661
1662static int dsa_user_setup_tc_block_cb_ig(enum tc_setup_type type,
1663					 void *type_data, void *cb_priv)
1664{
1665	return dsa_user_setup_tc_block_cb(type, type_data, cb_priv, true);
1666}
1667
1668static int dsa_user_setup_tc_block_cb_eg(enum tc_setup_type type,
1669					 void *type_data, void *cb_priv)
1670{
1671	return dsa_user_setup_tc_block_cb(type, type_data, cb_priv, false);
1672}
1673
1674static LIST_HEAD(dsa_user_block_cb_list);
1675
1676static int dsa_user_setup_tc_block(struct net_device *dev,
1677				   struct flow_block_offload *f)
1678{
1679	struct flow_block_cb *block_cb;
1680	flow_setup_cb_t *cb;
1681
1682	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1683		cb = dsa_user_setup_tc_block_cb_ig;
1684	else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
1685		cb = dsa_user_setup_tc_block_cb_eg;
1686	else
1687		return -EOPNOTSUPP;
1688
1689	f->driver_block_list = &dsa_user_block_cb_list;
1690
1691	switch (f->command) {
1692	case FLOW_BLOCK_BIND:
1693		if (flow_block_cb_is_busy(cb, dev, &dsa_user_block_cb_list))
1694			return -EBUSY;
1695
1696		block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1697		if (IS_ERR(block_cb))
1698			return PTR_ERR(block_cb);
1699
1700		flow_block_cb_add(block_cb, f);
1701		list_add_tail(&block_cb->driver_list, &dsa_user_block_cb_list);
1702		return 0;
1703	case FLOW_BLOCK_UNBIND:
1704		block_cb = flow_block_cb_lookup(f->block, cb, dev);
1705		if (!block_cb)
1706			return -ENOENT;
1707
1708		flow_block_cb_remove(block_cb, f);
1709		list_del(&block_cb->driver_list);
1710		return 0;
1711	default:
1712		return -EOPNOTSUPP;
1713	}
1714}
1715
1716static int dsa_user_setup_ft_block(struct dsa_switch *ds, int port,
1717				   void *type_data)
1718{
1719	struct net_device *conduit = dsa_port_to_conduit(dsa_to_port(ds, port));
1720
1721	if (!conduit->netdev_ops->ndo_setup_tc)
1722		return -EOPNOTSUPP;
1723
1724	return conduit->netdev_ops->ndo_setup_tc(conduit, TC_SETUP_FT, type_data);
1725}
1726
1727static int dsa_user_setup_tc(struct net_device *dev, enum tc_setup_type type,
1728			     void *type_data)
1729{
1730	struct dsa_port *dp = dsa_user_to_port(dev);
1731	struct dsa_switch *ds = dp->ds;
1732
1733	switch (type) {
1734	case TC_SETUP_BLOCK:
1735		return dsa_user_setup_tc_block(dev, type_data);
1736	case TC_SETUP_FT:
1737		return dsa_user_setup_ft_block(ds, dp->index, type_data);
1738	default:
1739		break;
1740	}
1741
1742	if (!ds->ops->port_setup_tc)
1743		return -EOPNOTSUPP;
1744
1745	return ds->ops->port_setup_tc(ds, dp->index, type, type_data);
1746}
1747
1748static int dsa_user_get_rxnfc(struct net_device *dev,
1749			      struct ethtool_rxnfc *nfc, u32 *rule_locs)
1750{
1751	struct dsa_port *dp = dsa_user_to_port(dev);
1752	struct dsa_switch *ds = dp->ds;
1753
1754	if (!ds->ops->get_rxnfc)
1755		return -EOPNOTSUPP;
1756
1757	return ds->ops->get_rxnfc(ds, dp->index, nfc, rule_locs);
1758}
1759
1760static int dsa_user_set_rxnfc(struct net_device *dev,
1761			      struct ethtool_rxnfc *nfc)
1762{
1763	struct dsa_port *dp = dsa_user_to_port(dev);
1764	struct dsa_switch *ds = dp->ds;
1765
1766	if (!ds->ops->set_rxnfc)
1767		return -EOPNOTSUPP;
1768
1769	return ds->ops->set_rxnfc(ds, dp->index, nfc);
1770}
1771
1772static int dsa_user_get_ts_info(struct net_device *dev,
1773				struct kernel_ethtool_ts_info *ts)
1774{
1775	struct dsa_user_priv *p = netdev_priv(dev);
1776	struct dsa_switch *ds = p->dp->ds;
1777
1778	if (!ds->ops->get_ts_info)
1779		return -EOPNOTSUPP;
1780
1781	return ds->ops->get_ts_info(ds, p->dp->index, ts);
1782}
1783
1784static int dsa_user_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
1785				    u16 vid)
1786{
1787	struct dsa_port *dp = dsa_user_to_port(dev);
1788	struct switchdev_obj_port_vlan vlan = {
1789		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
1790		.vid = vid,
1791		/* This API only allows programming tagged, non-PVID VIDs */
1792		.flags = 0,
1793	};
1794	struct netlink_ext_ack extack = {0};
1795	struct dsa_switch *ds = dp->ds;
1796	struct netdev_hw_addr *ha;
1797	struct dsa_vlan *v;
1798	int ret;
1799
1800	/* User port... */
1801	ret = dsa_port_vlan_add(dp, &vlan, &extack);
1802	if (ret) {
1803		if (extack._msg)
1804			netdev_err(dev, "%s\n", extack._msg);
1805		return ret;
1806	}
1807
1808	/* And CPU port... */
1809	ret = dsa_port_host_vlan_add(dp, &vlan, &extack);
1810	if (ret) {
1811		if (extack._msg)
1812			netdev_err(dev, "CPU port %d: %s\n", dp->cpu_dp->index,
1813				   extack._msg);
1814		return ret;
1815	}
1816
1817	if (!dsa_switch_supports_uc_filtering(ds) &&
1818	    !dsa_switch_supports_mc_filtering(ds))
1819		return 0;
1820
1821	v = kzalloc(sizeof(*v), GFP_KERNEL);
1822	if (!v) {
1823		ret = -ENOMEM;
1824		goto rollback;
1825	}
1826
1827	netif_addr_lock_bh(dev);
1828
1829	v->vid = vid;
1830	list_add_tail(&v->list, &dp->user_vlans);
1831
1832	if (dsa_switch_supports_mc_filtering(ds)) {
1833		netdev_for_each_synced_mc_addr(ha, dev) {
1834			dsa_user_schedule_standalone_work(dev, DSA_MC_ADD,
1835							  ha->addr, vid);
1836		}
1837	}
1838
1839	if (dsa_switch_supports_uc_filtering(ds)) {
1840		netdev_for_each_synced_uc_addr(ha, dev) {
1841			dsa_user_schedule_standalone_work(dev, DSA_UC_ADD,
1842							  ha->addr, vid);
1843		}
1844	}
1845
1846	netif_addr_unlock_bh(dev);
1847
1848	dsa_flush_workqueue();
1849
1850	return 0;
1851
1852rollback:
1853	dsa_port_host_vlan_del(dp, &vlan);
1854	dsa_port_vlan_del(dp, &vlan);
1855
1856	return ret;
1857}
1858
1859static int dsa_user_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
1860				     u16 vid)
1861{
1862	struct dsa_port *dp = dsa_user_to_port(dev);
1863	struct switchdev_obj_port_vlan vlan = {
1864		.vid = vid,
1865		/* This API only allows programming tagged, non-PVID VIDs */
1866		.flags = 0,
1867	};
1868	struct dsa_switch *ds = dp->ds;
1869	struct netdev_hw_addr *ha;
1870	struct dsa_vlan *v;
1871	int err;
1872
1873	err = dsa_port_vlan_del(dp, &vlan);
1874	if (err)
1875		return err;
1876
1877	err = dsa_port_host_vlan_del(dp, &vlan);
1878	if (err)
1879		return err;
1880
1881	if (!dsa_switch_supports_uc_filtering(ds) &&
1882	    !dsa_switch_supports_mc_filtering(ds))
1883		return 0;
1884
1885	netif_addr_lock_bh(dev);
1886
1887	v = dsa_vlan_find(&dp->user_vlans, &vlan);
1888	if (!v) {
1889		netif_addr_unlock_bh(dev);
1890		return -ENOENT;
1891	}
1892
1893	list_del(&v->list);
1894	kfree(v);
1895
1896	if (dsa_switch_supports_mc_filtering(ds)) {
1897		netdev_for_each_synced_mc_addr(ha, dev) {
1898			dsa_user_schedule_standalone_work(dev, DSA_MC_DEL,
1899							  ha->addr, vid);
1900		}
1901	}
1902
1903	if (dsa_switch_supports_uc_filtering(ds)) {
1904		netdev_for_each_synced_uc_addr(ha, dev) {
1905			dsa_user_schedule_standalone_work(dev, DSA_UC_DEL,
1906							  ha->addr, vid);
1907		}
1908	}
1909
1910	netif_addr_unlock_bh(dev);
1911
1912	dsa_flush_workqueue();
1913
1914	return 0;
1915}
1916
1917static int dsa_user_restore_vlan(struct net_device *vdev, int vid, void *arg)
1918{
1919	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1920
1921	return dsa_user_vlan_rx_add_vid(arg, proto, vid);
1922}
1923
1924static int dsa_user_clear_vlan(struct net_device *vdev, int vid, void *arg)
1925{
1926	__be16 proto = vdev ? vlan_dev_vlan_proto(vdev) : htons(ETH_P_8021Q);
1927
1928	return dsa_user_vlan_rx_kill_vid(arg, proto, vid);
1929}
1930
1931/* Keep the VLAN RX filtering list in sync with the hardware only if VLAN
1932 * filtering is enabled. The baseline is that only ports that offload a
1933 * VLAN-aware bridge are VLAN-aware, and standalone ports are VLAN-unaware,
1934 * but there are exceptions for quirky hardware.
1935 *
1936 * If ds->vlan_filtering_is_global = true, then standalone ports which share
1937 * the same switch with other ports that offload a VLAN-aware bridge are also
1938 * inevitably VLAN-aware.
1939 *
1940 * To summarize, a DSA switch port offloads:
1941 *
1942 * - If standalone (this includes software bridge, software LAG):
1943 *     - if ds->needs_standalone_vlan_filtering = true, OR if
1944 *       (ds->vlan_filtering_is_global = true AND there are bridges spanning
1945 *       this switch chip which have vlan_filtering=1)
1946 *         - the 8021q upper VLANs
1947 *     - else (standalone VLAN filtering is not needed, VLAN filtering is not
1948 *       global, or it is, but no port is under a VLAN-aware bridge):
1949 *         - no VLAN (any 8021q upper is a software VLAN)
1950 *
1951 * - If under a vlan_filtering=0 bridge which it offload:
1952 *     - if ds->configure_vlan_while_not_filtering = true (default):
1953 *         - the bridge VLANs. These VLANs are committed to hardware but inactive.
1954 *     - else (deprecated):
1955 *         - no VLAN. The bridge VLANs are not restored when VLAN awareness is
1956 *           enabled, so this behavior is broken and discouraged.
1957 *
1958 * - If under a vlan_filtering=1 bridge which it offload:
1959 *     - the bridge VLANs
1960 *     - the 8021q upper VLANs
1961 */
1962int dsa_user_manage_vlan_filtering(struct net_device *user,
1963				   bool vlan_filtering)
1964{
1965	int err;
1966
1967	if (vlan_filtering) {
1968		user->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1969
1970		err = vlan_for_each(user, dsa_user_restore_vlan, user);
1971		if (err) {
1972			vlan_for_each(user, dsa_user_clear_vlan, user);
1973			user->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1974			return err;
1975		}
1976	} else {
1977		err = vlan_for_each(user, dsa_user_clear_vlan, user);
1978		if (err)
1979			return err;
1980
1981		user->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1982	}
1983
1984	return 0;
1985}
1986
1987struct dsa_hw_port {
1988	struct list_head list;
1989	struct net_device *dev;
1990	int old_mtu;
1991};
1992
1993static int dsa_hw_port_list_set_mtu(struct list_head *hw_port_list, int mtu)
1994{
1995	const struct dsa_hw_port *p;
1996	int err;
1997
1998	list_for_each_entry(p, hw_port_list, list) {
1999		if (p->dev->mtu == mtu)
2000			continue;
2001
2002		err = dev_set_mtu(p->dev, mtu);
2003		if (err)
2004			goto rollback;
2005	}
2006
2007	return 0;
2008
2009rollback:
2010	list_for_each_entry_continue_reverse(p, hw_port_list, list) {
2011		if (p->dev->mtu == p->old_mtu)
2012			continue;
2013
2014		if (dev_set_mtu(p->dev, p->old_mtu))
2015			netdev_err(p->dev, "Failed to restore MTU\n");
2016	}
2017
2018	return err;
2019}
2020
2021static void dsa_hw_port_list_free(struct list_head *hw_port_list)
2022{
2023	struct dsa_hw_port *p, *n;
2024
2025	list_for_each_entry_safe(p, n, hw_port_list, list)
2026		kfree(p);
2027}
2028
2029/* Make the hardware datapath to/from @dev limited to a common MTU */
2030static void dsa_bridge_mtu_normalization(struct dsa_port *dp)
2031{
2032	struct list_head hw_port_list;
2033	struct dsa_switch_tree *dst;
2034	int min_mtu = ETH_MAX_MTU;
2035	struct dsa_port *other_dp;
2036	int err;
2037
2038	if (!dp->ds->mtu_enforcement_ingress)
2039		return;
2040
2041	if (!dp->bridge)
2042		return;
2043
2044	INIT_LIST_HEAD(&hw_port_list);
2045
2046	/* Populate the list of ports that are part of the same bridge
2047	 * as the newly added/modified port
2048	 */
2049	list_for_each_entry(dst, &dsa_tree_list, list) {
2050		list_for_each_entry(other_dp, &dst->ports, list) {
2051			struct dsa_hw_port *hw_port;
2052			struct net_device *user;
2053
2054			if (other_dp->type != DSA_PORT_TYPE_USER)
2055				continue;
2056
2057			if (!dsa_port_bridge_same(dp, other_dp))
2058				continue;
2059
2060			if (!other_dp->ds->mtu_enforcement_ingress)
2061				continue;
2062
2063			user = other_dp->user;
2064
2065			if (min_mtu > user->mtu)
2066				min_mtu = user->mtu;
2067
2068			hw_port = kzalloc(sizeof(*hw_port), GFP_KERNEL);
2069			if (!hw_port)
2070				goto out;
2071
2072			hw_port->dev = user;
2073			hw_port->old_mtu = user->mtu;
2074
2075			list_add(&hw_port->list, &hw_port_list);
2076		}
2077	}
2078
2079	/* Attempt to configure the entire hardware bridge to the newly added
2080	 * interface's MTU first, regardless of whether the intention of the
2081	 * user was to raise or lower it.
2082	 */
2083	err = dsa_hw_port_list_set_mtu(&hw_port_list, dp->user->mtu);
2084	if (!err)
2085		goto out;
2086
2087	/* Clearly that didn't work out so well, so just set the minimum MTU on
2088	 * all hardware bridge ports now. If this fails too, then all ports will
2089	 * still have their old MTU rolled back anyway.
2090	 */
2091	dsa_hw_port_list_set_mtu(&hw_port_list, min_mtu);
2092
2093out:
2094	dsa_hw_port_list_free(&hw_port_list);
2095}
2096
2097int dsa_user_change_mtu(struct net_device *dev, int new_mtu)
2098{
2099	struct net_device *conduit = dsa_user_to_conduit(dev);
2100	struct dsa_port *dp = dsa_user_to_port(dev);
2101	struct dsa_port *cpu_dp = dp->cpu_dp;
2102	struct dsa_switch *ds = dp->ds;
2103	struct dsa_port *other_dp;
2104	int largest_mtu = 0;
2105	int new_conduit_mtu;
2106	int old_conduit_mtu;
2107	int mtu_limit;
2108	int overhead;
2109	int cpu_mtu;
2110	int err;
2111
2112	if (!ds->ops->port_change_mtu)
2113		return -EOPNOTSUPP;
2114
2115	dsa_tree_for_each_user_port(other_dp, ds->dst) {
2116		int user_mtu;
2117
2118		/* During probe, this function will be called for each user
2119		 * device, while not all of them have been allocated. That's
2120		 * ok, it doesn't change what the maximum is, so ignore it.
2121		 */
2122		if (!other_dp->user)
2123			continue;
2124
2125		/* Pretend that we already applied the setting, which we
2126		 * actually haven't (still haven't done all integrity checks)
2127		 */
2128		if (dp == other_dp)
2129			user_mtu = new_mtu;
2130		else
2131			user_mtu = other_dp->user->mtu;
2132
2133		if (largest_mtu < user_mtu)
2134			largest_mtu = user_mtu;
2135	}
2136
2137	overhead = dsa_tag_protocol_overhead(cpu_dp->tag_ops);
2138	mtu_limit = min_t(int, conduit->max_mtu, dev->max_mtu + overhead);
2139	old_conduit_mtu = conduit->mtu;
2140	new_conduit_mtu = largest_mtu + overhead;
2141	if (new_conduit_mtu > mtu_limit)
2142		return -ERANGE;
2143
2144	/* If the conduit MTU isn't over limit, there's no need to check the CPU
2145	 * MTU, since that surely isn't either.
2146	 */
2147	cpu_mtu = largest_mtu;
2148
2149	/* Start applying stuff */
2150	if (new_conduit_mtu != old_conduit_mtu) {
2151		err = dev_set_mtu(conduit, new_conduit_mtu);
2152		if (err < 0)
2153			goto out_conduit_failed;
2154
2155		/* We only need to propagate the MTU of the CPU port to
2156		 * upstream switches, so emit a notifier which updates them.
2157		 */
2158		err = dsa_port_mtu_change(cpu_dp, cpu_mtu);
2159		if (err)
2160			goto out_cpu_failed;
2161	}
2162
2163	err = ds->ops->port_change_mtu(ds, dp->index, new_mtu);
2164	if (err)
2165		goto out_port_failed;
2166
2167	WRITE_ONCE(dev->mtu, new_mtu);
2168
2169	dsa_bridge_mtu_normalization(dp);
2170
2171	return 0;
2172
2173out_port_failed:
2174	if (new_conduit_mtu != old_conduit_mtu)
2175		dsa_port_mtu_change(cpu_dp, old_conduit_mtu - overhead);
2176out_cpu_failed:
2177	if (new_conduit_mtu != old_conduit_mtu)
2178		dev_set_mtu(conduit, old_conduit_mtu);
2179out_conduit_failed:
2180	return err;
2181}
2182
2183static int __maybe_unused
2184dsa_user_dcbnl_set_apptrust(struct net_device *dev, u8 *sel, int nsel)
2185{
2186	struct dsa_port *dp = dsa_user_to_port(dev);
2187	struct dsa_switch *ds = dp->ds;
2188	int port = dp->index;
2189
2190	if (!ds->ops->port_set_apptrust)
2191		return -EOPNOTSUPP;
2192
2193	return ds->ops->port_set_apptrust(ds, port, sel, nsel);
2194}
2195
2196static int __maybe_unused
2197dsa_user_dcbnl_get_apptrust(struct net_device *dev, u8 *sel, int *nsel)
2198{
2199	struct dsa_port *dp = dsa_user_to_port(dev);
2200	struct dsa_switch *ds = dp->ds;
2201	int port = dp->index;
2202
2203	if (!ds->ops->port_get_apptrust)
2204		return -EOPNOTSUPP;
2205
2206	return ds->ops->port_get_apptrust(ds, port, sel, nsel);
2207}
2208
2209static int __maybe_unused
2210dsa_user_dcbnl_set_default_prio(struct net_device *dev, struct dcb_app *app)
2211{
2212	struct dsa_port *dp = dsa_user_to_port(dev);
2213	struct dsa_switch *ds = dp->ds;
2214	unsigned long mask, new_prio;
2215	int err, port = dp->index;
2216
2217	if (!ds->ops->port_set_default_prio)
2218		return -EOPNOTSUPP;
2219
2220	err = dcb_ieee_setapp(dev, app);
2221	if (err)
2222		return err;
2223
2224	mask = dcb_ieee_getapp_mask(dev, app);
2225	new_prio = __fls(mask);
2226
2227	err = ds->ops->port_set_default_prio(ds, port, new_prio);
2228	if (err) {
2229		dcb_ieee_delapp(dev, app);
2230		return err;
2231	}
2232
2233	return 0;
2234}
2235
2236/* Update the DSCP prio entries on all user ports of the switch in case
2237 * the switch supports global DSCP prio instead of per port DSCP prios.
2238 */
2239static int dsa_user_dcbnl_ieee_global_dscp_setdel(struct net_device *dev,
2240						  struct dcb_app *app, bool del)
2241{
2242	int (*setdel)(struct net_device *dev, struct dcb_app *app);
2243	struct dsa_port *dp = dsa_user_to_port(dev);
2244	struct dsa_switch *ds = dp->ds;
2245	struct dsa_port *other_dp;
2246	int err, restore_err;
2247
2248	if (del)
2249		setdel = dcb_ieee_delapp;
2250	else
2251		setdel = dcb_ieee_setapp;
2252
2253	dsa_switch_for_each_user_port(other_dp, ds) {
2254		struct net_device *user = other_dp->user;
2255
2256		if (!user || user == dev)
2257			continue;
2258
2259		err = setdel(user, app);
2260		if (err)
2261			goto err_try_to_restore;
2262	}
2263
2264	return 0;
2265
2266err_try_to_restore:
2267
2268	/* Revert logic to restore previous state of app entries */
2269	if (!del)
2270		setdel = dcb_ieee_delapp;
2271	else
2272		setdel = dcb_ieee_setapp;
2273
2274	dsa_switch_for_each_user_port_continue_reverse(other_dp, ds) {
2275		struct net_device *user = other_dp->user;
2276
2277		if (!user || user == dev)
2278			continue;
2279
2280		restore_err = setdel(user, app);
2281		if (restore_err)
2282			netdev_err(user, "Failed to restore DSCP prio entry configuration\n");
2283	}
2284
2285	return err;
2286}
2287
2288static int __maybe_unused
2289dsa_user_dcbnl_add_dscp_prio(struct net_device *dev, struct dcb_app *app)
2290{
2291	struct dsa_port *dp = dsa_user_to_port(dev);
2292	struct dsa_switch *ds = dp->ds;
2293	unsigned long mask, new_prio;
2294	int err, port = dp->index;
2295	u8 dscp = app->protocol;
2296
2297	if (!ds->ops->port_add_dscp_prio)
2298		return -EOPNOTSUPP;
2299
2300	if (dscp >= 64) {
2301		netdev_err(dev, "DSCP APP entry with protocol value %u is invalid\n",
2302			   dscp);
2303		return -EINVAL;
2304	}
2305
2306	err = dcb_ieee_setapp(dev, app);
2307	if (err)
2308		return err;
2309
2310	mask = dcb_ieee_getapp_mask(dev, app);
2311	new_prio = __fls(mask);
2312
2313	err = ds->ops->port_add_dscp_prio(ds, port, dscp, new_prio);
2314	if (err) {
2315		dcb_ieee_delapp(dev, app);
2316		return err;
2317	}
2318
2319	if (!ds->dscp_prio_mapping_is_global)
2320		return 0;
2321
2322	err = dsa_user_dcbnl_ieee_global_dscp_setdel(dev, app, false);
2323	if (err) {
2324		if (ds->ops->port_del_dscp_prio)
2325			ds->ops->port_del_dscp_prio(ds, port, dscp, new_prio);
2326		dcb_ieee_delapp(dev, app);
2327		return err;
2328	}
2329
2330	return 0;
2331}
2332
2333static int __maybe_unused dsa_user_dcbnl_ieee_setapp(struct net_device *dev,
2334						     struct dcb_app *app)
2335{
2336	switch (app->selector) {
2337	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2338		switch (app->protocol) {
2339		case 0:
2340			return dsa_user_dcbnl_set_default_prio(dev, app);
2341		default:
2342			return -EOPNOTSUPP;
2343		}
2344		break;
2345	case IEEE_8021QAZ_APP_SEL_DSCP:
2346		return dsa_user_dcbnl_add_dscp_prio(dev, app);
2347	default:
2348		return -EOPNOTSUPP;
2349	}
2350}
2351
2352static int __maybe_unused
2353dsa_user_dcbnl_del_default_prio(struct net_device *dev, struct dcb_app *app)
2354{
2355	struct dsa_port *dp = dsa_user_to_port(dev);
2356	struct dsa_switch *ds = dp->ds;
2357	unsigned long mask, new_prio;
2358	int err, port = dp->index;
2359
2360	if (!ds->ops->port_set_default_prio)
2361		return -EOPNOTSUPP;
2362
2363	err = dcb_ieee_delapp(dev, app);
2364	if (err)
2365		return err;
2366
2367	mask = dcb_ieee_getapp_mask(dev, app);
2368	new_prio = mask ? __fls(mask) : 0;
2369
2370	err = ds->ops->port_set_default_prio(ds, port, new_prio);
2371	if (err) {
2372		dcb_ieee_setapp(dev, app);
2373		return err;
2374	}
2375
2376	return 0;
2377}
2378
2379static int __maybe_unused
2380dsa_user_dcbnl_del_dscp_prio(struct net_device *dev, struct dcb_app *app)
2381{
2382	struct dsa_port *dp = dsa_user_to_port(dev);
2383	struct dsa_switch *ds = dp->ds;
2384	int err, port = dp->index;
2385	u8 dscp = app->protocol;
2386
2387	if (!ds->ops->port_del_dscp_prio)
2388		return -EOPNOTSUPP;
2389
2390	err = dcb_ieee_delapp(dev, app);
2391	if (err)
2392		return err;
2393
2394	err = ds->ops->port_del_dscp_prio(ds, port, dscp, app->priority);
2395	if (err) {
2396		dcb_ieee_setapp(dev, app);
2397		return err;
2398	}
2399
2400	if (!ds->dscp_prio_mapping_is_global)
2401		return 0;
2402
2403	err = dsa_user_dcbnl_ieee_global_dscp_setdel(dev, app, true);
2404	if (err) {
2405		if (ds->ops->port_add_dscp_prio)
2406			ds->ops->port_add_dscp_prio(ds, port, dscp,
2407						    app->priority);
2408		dcb_ieee_setapp(dev, app);
2409		return err;
2410	}
2411
2412	return 0;
2413}
2414
2415static int __maybe_unused dsa_user_dcbnl_ieee_delapp(struct net_device *dev,
2416						     struct dcb_app *app)
2417{
2418	switch (app->selector) {
2419	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
2420		switch (app->protocol) {
2421		case 0:
2422			return dsa_user_dcbnl_del_default_prio(dev, app);
2423		default:
2424			return -EOPNOTSUPP;
2425		}
2426		break;
2427	case IEEE_8021QAZ_APP_SEL_DSCP:
2428		return dsa_user_dcbnl_del_dscp_prio(dev, app);
2429	default:
2430		return -EOPNOTSUPP;
2431	}
2432}
2433
2434/* Pre-populate the DCB application priority table with the priorities
2435 * configured during switch setup, which we read from hardware here.
2436 */
2437static int dsa_user_dcbnl_init(struct net_device *dev)
2438{
2439	struct dsa_port *dp = dsa_user_to_port(dev);
2440	struct dsa_switch *ds = dp->ds;
2441	int port = dp->index;
2442	int err;
2443
2444	if (ds->ops->port_get_default_prio) {
2445		int prio = ds->ops->port_get_default_prio(ds, port);
2446		struct dcb_app app = {
2447			.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE,
2448			.protocol = 0,
2449			.priority = prio,
2450		};
2451
2452		if (prio < 0)
2453			return prio;
2454
2455		err = dcb_ieee_setapp(dev, &app);
2456		if (err)
2457			return err;
2458	}
2459
2460	if (ds->ops->port_get_dscp_prio) {
2461		int protocol;
2462
2463		for (protocol = 0; protocol < 64; protocol++) {
2464			struct dcb_app app = {
2465				.selector = IEEE_8021QAZ_APP_SEL_DSCP,
2466				.protocol = protocol,
2467			};
2468			int prio;
2469
2470			prio = ds->ops->port_get_dscp_prio(ds, port, protocol);
2471			if (prio == -EOPNOTSUPP)
2472				continue;
2473			if (prio < 0)
2474				return prio;
2475
2476			app.priority = prio;
2477
2478			err = dcb_ieee_setapp(dev, &app);
2479			if (err)
2480				return err;
2481		}
2482	}
2483
2484	return 0;
2485}
2486
2487static const struct ethtool_ops dsa_user_ethtool_ops = {
2488	.get_drvinfo		= dsa_user_get_drvinfo,
2489	.get_regs_len		= dsa_user_get_regs_len,
2490	.get_regs		= dsa_user_get_regs,
2491	.nway_reset		= dsa_user_nway_reset,
2492	.get_link		= ethtool_op_get_link,
2493	.get_eeprom_len		= dsa_user_get_eeprom_len,
2494	.get_eeprom		= dsa_user_get_eeprom,
2495	.set_eeprom		= dsa_user_set_eeprom,
2496	.get_strings		= dsa_user_get_strings,
2497	.get_ethtool_stats	= dsa_user_get_ethtool_stats,
2498	.get_sset_count		= dsa_user_get_sset_count,
2499	.get_eth_phy_stats	= dsa_user_get_eth_phy_stats,
2500	.get_eth_mac_stats	= dsa_user_get_eth_mac_stats,
2501	.get_eth_ctrl_stats	= dsa_user_get_eth_ctrl_stats,
2502	.get_rmon_stats		= dsa_user_get_rmon_stats,
2503	.set_wol		= dsa_user_set_wol,
2504	.get_wol		= dsa_user_get_wol,
2505	.set_eee		= dsa_user_set_eee,
2506	.get_eee		= dsa_user_get_eee,
2507	.get_link_ksettings	= dsa_user_get_link_ksettings,
2508	.set_link_ksettings	= dsa_user_set_link_ksettings,
2509	.get_pause_stats	= dsa_user_get_pause_stats,
2510	.get_pauseparam		= dsa_user_get_pauseparam,
2511	.set_pauseparam		= dsa_user_set_pauseparam,
2512	.get_rxnfc		= dsa_user_get_rxnfc,
2513	.set_rxnfc		= dsa_user_set_rxnfc,
2514	.get_ts_info		= dsa_user_get_ts_info,
2515	.self_test		= dsa_user_net_selftest,
2516	.get_mm			= dsa_user_get_mm,
2517	.set_mm			= dsa_user_set_mm,
2518	.get_mm_stats		= dsa_user_get_mm_stats,
2519};
2520
2521static const struct dcbnl_rtnl_ops __maybe_unused dsa_user_dcbnl_ops = {
2522	.ieee_setapp		= dsa_user_dcbnl_ieee_setapp,
2523	.ieee_delapp		= dsa_user_dcbnl_ieee_delapp,
2524	.dcbnl_setapptrust	= dsa_user_dcbnl_set_apptrust,
2525	.dcbnl_getapptrust	= dsa_user_dcbnl_get_apptrust,
2526};
2527
2528static void dsa_user_get_stats64(struct net_device *dev,
2529				 struct rtnl_link_stats64 *s)
2530{
2531	struct dsa_port *dp = dsa_user_to_port(dev);
2532	struct dsa_switch *ds = dp->ds;
2533
2534	if (ds->ops->get_stats64)
2535		ds->ops->get_stats64(ds, dp->index, s);
2536	else
2537		dev_get_tstats64(dev, s);
2538}
2539
2540static int dsa_user_fill_forward_path(struct net_device_path_ctx *ctx,
2541				      struct net_device_path *path)
2542{
2543	struct dsa_port *dp = dsa_user_to_port(ctx->dev);
2544	struct net_device *conduit = dsa_port_to_conduit(dp);
2545	struct dsa_port *cpu_dp = dp->cpu_dp;
2546
2547	path->dev = ctx->dev;
2548	path->type = DEV_PATH_DSA;
2549	path->dsa.proto = cpu_dp->tag_ops->proto;
2550	path->dsa.port = dp->index;
2551	ctx->dev = conduit;
2552
2553	return 0;
2554}
2555
2556static const struct net_device_ops dsa_user_netdev_ops = {
2557	.ndo_open		= dsa_user_open,
2558	.ndo_stop		= dsa_user_close,
2559	.ndo_start_xmit		= dsa_user_xmit,
2560	.ndo_change_rx_flags	= dsa_user_change_rx_flags,
2561	.ndo_set_rx_mode	= dsa_user_set_rx_mode,
2562	.ndo_set_mac_address	= dsa_user_set_mac_address,
2563	.ndo_fdb_dump		= dsa_user_fdb_dump,
2564	.ndo_eth_ioctl		= dsa_user_ioctl,
2565	.ndo_get_iflink		= dsa_user_get_iflink,
2566#ifdef CONFIG_NET_POLL_CONTROLLER
2567	.ndo_netpoll_setup	= dsa_user_netpoll_setup,
2568	.ndo_netpoll_cleanup	= dsa_user_netpoll_cleanup,
2569	.ndo_poll_controller	= dsa_user_poll_controller,
2570#endif
2571	.ndo_setup_tc		= dsa_user_setup_tc,
2572	.ndo_get_stats64	= dsa_user_get_stats64,
2573	.ndo_vlan_rx_add_vid	= dsa_user_vlan_rx_add_vid,
2574	.ndo_vlan_rx_kill_vid	= dsa_user_vlan_rx_kill_vid,
2575	.ndo_change_mtu		= dsa_user_change_mtu,
2576	.ndo_fill_forward_path	= dsa_user_fill_forward_path,
2577};
2578
2579static const struct device_type dsa_type = {
2580	.name	= "dsa",
2581};
2582
2583void dsa_port_phylink_mac_change(struct dsa_switch *ds, int port, bool up)
2584{
2585	const struct dsa_port *dp = dsa_to_port(ds, port);
2586
2587	if (dp->pl)
2588		phylink_mac_change(dp->pl, up);
2589}
2590EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_change);
2591
2592static void dsa_user_phylink_fixed_state(struct phylink_config *config,
2593					 struct phylink_link_state *state)
2594{
2595	struct dsa_port *dp = dsa_phylink_to_port(config);
2596	struct dsa_switch *ds = dp->ds;
2597
2598	/* No need to check that this operation is valid, the callback would
2599	 * not be called if it was not.
2600	 */
2601	ds->ops->phylink_fixed_state(ds, dp->index, state);
2602}
2603
2604/* user device setup *******************************************************/
2605static int dsa_user_phy_connect(struct net_device *user_dev, int addr,
2606				u32 flags)
2607{
2608	struct dsa_port *dp = dsa_user_to_port(user_dev);
2609	struct dsa_switch *ds = dp->ds;
2610
2611	user_dev->phydev = mdiobus_get_phy(ds->user_mii_bus, addr);
2612	if (!user_dev->phydev) {
2613		netdev_err(user_dev, "no phy at %d\n", addr);
2614		return -ENODEV;
2615	}
2616
2617	user_dev->phydev->dev_flags |= flags;
2618
2619	return phylink_connect_phy(dp->pl, user_dev->phydev);
2620}
2621
2622static int dsa_user_phy_setup(struct net_device *user_dev)
2623{
2624	struct dsa_port *dp = dsa_user_to_port(user_dev);
2625	struct device_node *port_dn = dp->dn;
2626	struct dsa_switch *ds = dp->ds;
2627	u32 phy_flags = 0;
2628	int ret;
2629
2630	dp->pl_config.dev = &user_dev->dev;
2631	dp->pl_config.type = PHYLINK_NETDEV;
2632
2633	/* The get_fixed_state callback takes precedence over polling the
2634	 * link GPIO in PHYLINK (see phylink_get_fixed_state).  Only set
2635	 * this if the switch provides such a callback.
2636	 */
2637	if (ds->ops->phylink_fixed_state) {
2638		dp->pl_config.get_fixed_state = dsa_user_phylink_fixed_state;
2639		dp->pl_config.poll_fixed_state = true;
2640	}
2641
2642	ret = dsa_port_phylink_create(dp);
2643	if (ret)
2644		return ret;
2645
2646	if (ds->ops->get_phy_flags)
2647		phy_flags = ds->ops->get_phy_flags(ds, dp->index);
2648
2649	ret = phylink_of_phy_connect(dp->pl, port_dn, phy_flags);
2650	if (ret == -ENODEV && ds->user_mii_bus) {
2651		/* We could not connect to a designated PHY or SFP, so try to
2652		 * use the switch internal MDIO bus instead
2653		 */
2654		ret = dsa_user_phy_connect(user_dev, dp->index, phy_flags);
2655	}
2656	if (ret) {
2657		netdev_err(user_dev, "failed to connect to PHY: %pe\n",
2658			   ERR_PTR(ret));
2659		dsa_port_phylink_destroy(dp);
2660	}
2661
2662	return ret;
2663}
2664
2665void dsa_user_setup_tagger(struct net_device *user)
2666{
2667	struct dsa_port *dp = dsa_user_to_port(user);
2668	struct net_device *conduit = dsa_port_to_conduit(dp);
2669	struct dsa_user_priv *p = netdev_priv(user);
2670	const struct dsa_port *cpu_dp = dp->cpu_dp;
2671	const struct dsa_switch *ds = dp->ds;
2672
2673	user->needed_headroom = cpu_dp->tag_ops->needed_headroom;
2674	user->needed_tailroom = cpu_dp->tag_ops->needed_tailroom;
2675	/* Try to save one extra realloc later in the TX path (in the conduit)
2676	 * by also inheriting the conduit's needed headroom and tailroom.
2677	 * The 8021q driver also does this.
2678	 */
2679	user->needed_headroom += conduit->needed_headroom;
2680	user->needed_tailroom += conduit->needed_tailroom;
2681
2682	p->xmit = cpu_dp->tag_ops->xmit;
2683
2684	user->features = conduit->vlan_features | NETIF_F_HW_TC;
2685	user->hw_features |= NETIF_F_HW_TC;
2686	if (user->needed_tailroom)
2687		user->features &= ~(NETIF_F_SG | NETIF_F_FRAGLIST);
2688	if (ds->needs_standalone_vlan_filtering)
2689		user->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2690
2691	user->lltx = true;
2692}
2693
2694int dsa_user_suspend(struct net_device *user_dev)
2695{
2696	struct dsa_port *dp = dsa_user_to_port(user_dev);
2697
2698	if (!netif_running(user_dev))
2699		return 0;
2700
2701	netif_device_detach(user_dev);
2702
2703	rtnl_lock();
2704	phylink_stop(dp->pl);
2705	rtnl_unlock();
2706
2707	return 0;
2708}
2709
2710int dsa_user_resume(struct net_device *user_dev)
2711{
2712	struct dsa_port *dp = dsa_user_to_port(user_dev);
2713
2714	if (!netif_running(user_dev))
2715		return 0;
2716
2717	netif_device_attach(user_dev);
2718
2719	rtnl_lock();
2720	phylink_start(dp->pl);
2721	rtnl_unlock();
2722
2723	return 0;
2724}
2725
2726int dsa_user_create(struct dsa_port *port)
2727{
2728	struct net_device *conduit = dsa_port_to_conduit(port);
2729	struct dsa_switch *ds = port->ds;
2730	struct net_device *user_dev;
2731	struct dsa_user_priv *p;
2732	const char *name;
2733	int assign_type;
2734	int ret;
2735
2736	if (!ds->num_tx_queues)
2737		ds->num_tx_queues = 1;
2738
2739	if (port->name) {
2740		name = port->name;
2741		assign_type = NET_NAME_PREDICTABLE;
2742	} else {
2743		name = "eth%d";
2744		assign_type = NET_NAME_ENUM;
2745	}
2746
2747	user_dev = alloc_netdev_mqs(sizeof(struct dsa_user_priv), name,
2748				    assign_type, ether_setup,
2749				    ds->num_tx_queues, 1);
2750	if (user_dev == NULL)
2751		return -ENOMEM;
2752
2753	user_dev->rtnl_link_ops = &dsa_link_ops;
2754	user_dev->ethtool_ops = &dsa_user_ethtool_ops;
2755#if IS_ENABLED(CONFIG_DCB)
2756	user_dev->dcbnl_ops = &dsa_user_dcbnl_ops;
2757#endif
2758	if (!is_zero_ether_addr(port->mac))
2759		eth_hw_addr_set(user_dev, port->mac);
2760	else
2761		eth_hw_addr_inherit(user_dev, conduit);
2762	user_dev->priv_flags |= IFF_NO_QUEUE;
2763	if (dsa_switch_supports_uc_filtering(ds))
2764		user_dev->priv_flags |= IFF_UNICAST_FLT;
2765	user_dev->netdev_ops = &dsa_user_netdev_ops;
2766	if (ds->ops->port_max_mtu)
2767		user_dev->max_mtu = ds->ops->port_max_mtu(ds, port->index);
2768	SET_NETDEV_DEVTYPE(user_dev, &dsa_type);
2769
2770	SET_NETDEV_DEV(user_dev, port->ds->dev);
2771	SET_NETDEV_DEVLINK_PORT(user_dev, &port->devlink_port);
2772	user_dev->dev.of_node = port->dn;
2773	user_dev->vlan_features = conduit->vlan_features;
2774
2775	p = netdev_priv(user_dev);
2776	user_dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
2777
2778	ret = gro_cells_init(&p->gcells, user_dev);
2779	if (ret)
2780		goto out_free;
2781
2782	p->dp = port;
2783	INIT_LIST_HEAD(&p->mall_tc_list);
2784	port->user = user_dev;
2785	dsa_user_setup_tagger(user_dev);
2786
2787	netif_carrier_off(user_dev);
2788
2789	ret = dsa_user_phy_setup(user_dev);
2790	if (ret) {
2791		netdev_err(user_dev,
2792			   "error %d setting up PHY for tree %d, switch %d, port %d\n",
2793			   ret, ds->dst->index, ds->index, port->index);
2794		goto out_gcells;
2795	}
2796
2797	rtnl_lock();
2798
2799	ret = dsa_user_change_mtu(user_dev, ETH_DATA_LEN);
2800	if (ret && ret != -EOPNOTSUPP)
2801		dev_warn(ds->dev, "nonfatal error %d setting MTU to %d on port %d\n",
2802			 ret, ETH_DATA_LEN, port->index);
2803
2804	ret = register_netdevice(user_dev);
2805	if (ret) {
2806		netdev_err(conduit, "error %d registering interface %s\n",
2807			   ret, user_dev->name);
2808		rtnl_unlock();
2809		goto out_phy;
2810	}
2811
2812	if (IS_ENABLED(CONFIG_DCB)) {
2813		ret = dsa_user_dcbnl_init(user_dev);
2814		if (ret) {
2815			netdev_err(user_dev,
2816				   "failed to initialize DCB: %pe\n",
2817				   ERR_PTR(ret));
2818			rtnl_unlock();
2819			goto out_unregister;
2820		}
2821	}
2822
2823	ret = netdev_upper_dev_link(conduit, user_dev, NULL);
2824
2825	rtnl_unlock();
2826
2827	if (ret)
2828		goto out_unregister;
2829
2830	return 0;
2831
2832out_unregister:
2833	unregister_netdev(user_dev);
2834out_phy:
2835	rtnl_lock();
2836	phylink_disconnect_phy(p->dp->pl);
2837	rtnl_unlock();
2838	dsa_port_phylink_destroy(p->dp);
2839out_gcells:
2840	gro_cells_destroy(&p->gcells);
2841out_free:
2842	free_netdev(user_dev);
2843	port->user = NULL;
2844	return ret;
2845}
2846
2847void dsa_user_destroy(struct net_device *user_dev)
2848{
2849	struct net_device *conduit = dsa_user_to_conduit(user_dev);
2850	struct dsa_port *dp = dsa_user_to_port(user_dev);
2851	struct dsa_user_priv *p = netdev_priv(user_dev);
2852
2853	netif_carrier_off(user_dev);
2854	rtnl_lock();
2855	netdev_upper_dev_unlink(conduit, user_dev);
2856	unregister_netdevice(user_dev);
2857	phylink_disconnect_phy(dp->pl);
2858	rtnl_unlock();
2859
2860	dsa_port_phylink_destroy(dp);
2861	gro_cells_destroy(&p->gcells);
2862	free_netdev(user_dev);
2863}
2864
2865int dsa_user_change_conduit(struct net_device *dev, struct net_device *conduit,
2866			    struct netlink_ext_ack *extack)
2867{
2868	struct net_device *old_conduit = dsa_user_to_conduit(dev);
2869	struct dsa_port *dp = dsa_user_to_port(dev);
2870	struct dsa_switch *ds = dp->ds;
2871	struct net_device *upper;
2872	struct list_head *iter;
2873	int err;
2874
2875	if (conduit == old_conduit)
2876		return 0;
2877
2878	if (!ds->ops->port_change_conduit) {
2879		NL_SET_ERR_MSG_MOD(extack,
2880				   "Driver does not support changing DSA conduit");
2881		return -EOPNOTSUPP;
2882	}
2883
2884	if (!netdev_uses_dsa(conduit)) {
2885		NL_SET_ERR_MSG_MOD(extack,
2886				   "Interface not eligible as DSA conduit");
2887		return -EOPNOTSUPP;
2888	}
2889
2890	netdev_for_each_upper_dev_rcu(conduit, upper, iter) {
2891		if (dsa_user_dev_check(upper))
2892			continue;
2893		if (netif_is_bridge_master(upper))
2894			continue;
2895		NL_SET_ERR_MSG_MOD(extack, "Cannot join conduit with unknown uppers");
2896		return -EOPNOTSUPP;
2897	}
2898
2899	/* Since we allow live-changing the DSA conduit, plus we auto-open the
2900	 * DSA conduit when the user port opens => we need to ensure that the
2901	 * new DSA conduit is open too.
2902	 */
2903	if (dev->flags & IFF_UP) {
2904		err = dev_open(conduit, extack);
2905		if (err)
2906			return err;
2907	}
2908
2909	netdev_upper_dev_unlink(old_conduit, dev);
2910
2911	err = netdev_upper_dev_link(conduit, dev, extack);
2912	if (err)
2913		goto out_revert_old_conduit_unlink;
2914
2915	err = dsa_port_change_conduit(dp, conduit, extack);
2916	if (err)
2917		goto out_revert_conduit_link;
2918
2919	/* Update the MTU of the new CPU port through cross-chip notifiers */
2920	err = dsa_user_change_mtu(dev, dev->mtu);
2921	if (err && err != -EOPNOTSUPP) {
2922		netdev_warn(dev,
2923			    "nonfatal error updating MTU with new conduit: %pe\n",
2924			    ERR_PTR(err));
2925	}
2926
2927	return 0;
2928
2929out_revert_conduit_link:
2930	netdev_upper_dev_unlink(conduit, dev);
2931out_revert_old_conduit_unlink:
2932	netdev_upper_dev_link(old_conduit, dev, NULL);
2933	return err;
2934}
2935
2936bool dsa_user_dev_check(const struct net_device *dev)
2937{
2938	return dev->netdev_ops == &dsa_user_netdev_ops;
2939}
2940EXPORT_SYMBOL_GPL(dsa_user_dev_check);
2941
2942static int dsa_user_changeupper(struct net_device *dev,
2943				struct netdev_notifier_changeupper_info *info)
2944{
2945	struct netlink_ext_ack *extack;
2946	int err = NOTIFY_DONE;
2947	struct dsa_port *dp;
2948
2949	if (!dsa_user_dev_check(dev))
2950		return err;
2951
2952	dp = dsa_user_to_port(dev);
2953	extack = netdev_notifier_info_to_extack(&info->info);
2954
2955	if (netif_is_bridge_master(info->upper_dev)) {
2956		if (info->linking) {
2957			err = dsa_port_bridge_join(dp, info->upper_dev, extack);
2958			if (!err)
2959				dsa_bridge_mtu_normalization(dp);
2960			if (err == -EOPNOTSUPP) {
2961				NL_SET_ERR_MSG_WEAK_MOD(extack,
2962							"Offloading not supported");
2963				err = 0;
2964			}
2965			err = notifier_from_errno(err);
2966		} else {
2967			dsa_port_bridge_leave(dp, info->upper_dev);
2968			err = NOTIFY_OK;
2969		}
2970	} else if (netif_is_lag_master(info->upper_dev)) {
2971		if (info->linking) {
2972			err = dsa_port_lag_join(dp, info->upper_dev,
2973						info->upper_info, extack);
2974			if (err == -EOPNOTSUPP) {
2975				NL_SET_ERR_MSG_WEAK_MOD(extack,
2976							"Offloading not supported");
2977				err = 0;
2978			}
2979			err = notifier_from_errno(err);
2980		} else {
2981			dsa_port_lag_leave(dp, info->upper_dev);
2982			err = NOTIFY_OK;
2983		}
2984	} else if (is_hsr_master(info->upper_dev)) {
2985		if (info->linking) {
2986			err = dsa_port_hsr_join(dp, info->upper_dev, extack);
2987			if (err == -EOPNOTSUPP) {
2988				NL_SET_ERR_MSG_WEAK_MOD(extack,
2989							"Offloading not supported");
2990				err = 0;
2991			}
2992			err = notifier_from_errno(err);
2993		} else {
2994			dsa_port_hsr_leave(dp, info->upper_dev);
2995			err = NOTIFY_OK;
2996		}
2997	}
2998
2999	return err;
3000}
3001
3002static int dsa_user_prechangeupper(struct net_device *dev,
3003				   struct netdev_notifier_changeupper_info *info)
3004{
3005	struct dsa_port *dp;
3006
3007	if (!dsa_user_dev_check(dev))
3008		return NOTIFY_DONE;
3009
3010	dp = dsa_user_to_port(dev);
3011
3012	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
3013		dsa_port_pre_bridge_leave(dp, info->upper_dev);
3014	else if (netif_is_lag_master(info->upper_dev) && !info->linking)
3015		dsa_port_pre_lag_leave(dp, info->upper_dev);
3016	/* dsa_port_pre_hsr_leave is not yet necessary since hsr devices cannot
3017	 * meaningfully placed under a bridge yet
3018	 */
3019
3020	return NOTIFY_DONE;
3021}
3022
3023static int
3024dsa_user_lag_changeupper(struct net_device *dev,
3025			 struct netdev_notifier_changeupper_info *info)
3026{
3027	struct net_device *lower;
3028	struct list_head *iter;
3029	int err = NOTIFY_DONE;
3030	struct dsa_port *dp;
3031
3032	if (!netif_is_lag_master(dev))
3033		return err;
3034
3035	netdev_for_each_lower_dev(dev, lower, iter) {
3036		if (!dsa_user_dev_check(lower))
3037			continue;
3038
3039		dp = dsa_user_to_port(lower);
3040		if (!dp->lag)
3041			/* Software LAG */
3042			continue;
3043
3044		err = dsa_user_changeupper(lower, info);
3045		if (notifier_to_errno(err))
3046			break;
3047	}
3048
3049	return err;
3050}
3051
3052/* Same as dsa_user_lag_changeupper() except that it calls
3053 * dsa_user_prechangeupper()
3054 */
3055static int
3056dsa_user_lag_prechangeupper(struct net_device *dev,
3057			    struct netdev_notifier_changeupper_info *info)
3058{
3059	struct net_device *lower;
3060	struct list_head *iter;
3061	int err = NOTIFY_DONE;
3062	struct dsa_port *dp;
3063
3064	if (!netif_is_lag_master(dev))
3065		return err;
3066
3067	netdev_for_each_lower_dev(dev, lower, iter) {
3068		if (!dsa_user_dev_check(lower))
3069			continue;
3070
3071		dp = dsa_user_to_port(lower);
3072		if (!dp->lag)
3073			/* Software LAG */
3074			continue;
3075
3076		err = dsa_user_prechangeupper(lower, info);
3077		if (notifier_to_errno(err))
3078			break;
3079	}
3080
3081	return err;
3082}
3083
3084static int
3085dsa_prevent_bridging_8021q_upper(struct net_device *dev,
3086				 struct netdev_notifier_changeupper_info *info)
3087{
3088	struct netlink_ext_ack *ext_ack;
3089	struct net_device *user, *br;
3090	struct dsa_port *dp;
3091
3092	ext_ack = netdev_notifier_info_to_extack(&info->info);
3093
3094	if (!is_vlan_dev(dev))
3095		return NOTIFY_DONE;
3096
3097	user = vlan_dev_real_dev(dev);
3098	if (!dsa_user_dev_check(user))
3099		return NOTIFY_DONE;
3100
3101	dp = dsa_user_to_port(user);
3102	br = dsa_port_bridge_dev_get(dp);
3103	if (!br)
3104		return NOTIFY_DONE;
3105
3106	/* Deny enslaving a VLAN device into a VLAN-aware bridge */
3107	if (br_vlan_enabled(br) &&
3108	    netif_is_bridge_master(info->upper_dev) && info->linking) {
3109		NL_SET_ERR_MSG_MOD(ext_ack,
3110				   "Cannot make VLAN device join VLAN-aware bridge");
3111		return notifier_from_errno(-EINVAL);
3112	}
3113
3114	return NOTIFY_DONE;
3115}
3116
3117static int
3118dsa_user_check_8021q_upper(struct net_device *dev,
3119			   struct netdev_notifier_changeupper_info *info)
3120{
3121	struct dsa_port *dp = dsa_user_to_port(dev);
3122	struct net_device *br = dsa_port_bridge_dev_get(dp);
3123	struct bridge_vlan_info br_info;
3124	struct netlink_ext_ack *extack;
3125	int err = NOTIFY_DONE;
3126	u16 vid;
3127
3128	if (!br || !br_vlan_enabled(br))
3129		return NOTIFY_DONE;
3130
3131	extack = netdev_notifier_info_to_extack(&info->info);
3132	vid = vlan_dev_vlan_id(info->upper_dev);
3133
3134	/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
3135	 * device, respectively the VID is not found, returning
3136	 * 0 means success, which is a failure for us here.
3137	 */
3138	err = br_vlan_get_info(br, vid, &br_info);
3139	if (err == 0) {
3140		NL_SET_ERR_MSG_MOD(extack,
3141				   "This VLAN is already configured by the bridge");
3142		return notifier_from_errno(-EBUSY);
3143	}
3144
3145	return NOTIFY_DONE;
3146}
3147
3148static int
3149dsa_user_prechangeupper_sanity_check(struct net_device *dev,
3150				     struct netdev_notifier_changeupper_info *info)
3151{
3152	struct dsa_switch *ds;
3153	struct dsa_port *dp;
3154	int err;
3155
3156	if (!dsa_user_dev_check(dev))
3157		return dsa_prevent_bridging_8021q_upper(dev, info);
3158
3159	dp = dsa_user_to_port(dev);
3160	ds = dp->ds;
3161
3162	if (ds->ops->port_prechangeupper) {
3163		err = ds->ops->port_prechangeupper(ds, dp->index, info);
3164		if (err)
3165			return notifier_from_errno(err);
3166	}
3167
3168	if (is_vlan_dev(info->upper_dev))
3169		return dsa_user_check_8021q_upper(dev, info);
3170
3171	return NOTIFY_DONE;
3172}
3173
3174/* To be eligible as a DSA conduit, a LAG must have all lower interfaces be
3175 * eligible DSA conduits. Additionally, all LAG slaves must be DSA conduits of
3176 * switches in the same switch tree.
3177 */
3178static int dsa_lag_conduit_validate(struct net_device *lag_dev,
3179				    struct netlink_ext_ack *extack)
3180{
3181	struct net_device *lower1, *lower2;
3182	struct list_head *iter1, *iter2;
3183
3184	netdev_for_each_lower_dev(lag_dev, lower1, iter1) {
3185		netdev_for_each_lower_dev(lag_dev, lower2, iter2) {
3186			if (!netdev_uses_dsa(lower1) ||
3187			    !netdev_uses_dsa(lower2)) {
3188				NL_SET_ERR_MSG_MOD(extack,
3189						   "All LAG ports must be eligible as DSA conduits");
3190				return notifier_from_errno(-EINVAL);
3191			}
3192
3193			if (lower1 == lower2)
3194				continue;
3195
3196			if (!dsa_port_tree_same(lower1->dsa_ptr,
3197						lower2->dsa_ptr)) {
3198				NL_SET_ERR_MSG_MOD(extack,
3199						   "LAG contains DSA conduits of disjoint switch trees");
3200				return notifier_from_errno(-EINVAL);
3201			}
3202		}
3203	}
3204
3205	return NOTIFY_DONE;
3206}
3207
3208static int
3209dsa_conduit_prechangeupper_sanity_check(struct net_device *conduit,
3210					struct netdev_notifier_changeupper_info *info)
3211{
3212	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3213
3214	if (!netdev_uses_dsa(conduit))
3215		return NOTIFY_DONE;
3216
3217	if (!info->linking)
3218		return NOTIFY_DONE;
3219
3220	/* Allow DSA switch uppers */
3221	if (dsa_user_dev_check(info->upper_dev))
3222		return NOTIFY_DONE;
3223
3224	/* Allow bridge uppers of DSA conduits, subject to further
3225	 * restrictions in dsa_bridge_prechangelower_sanity_check()
3226	 */
3227	if (netif_is_bridge_master(info->upper_dev))
3228		return NOTIFY_DONE;
3229
3230	/* Allow LAG uppers, subject to further restrictions in
3231	 * dsa_lag_conduit_prechangelower_sanity_check()
3232	 */
3233	if (netif_is_lag_master(info->upper_dev))
3234		return dsa_lag_conduit_validate(info->upper_dev, extack);
3235
3236	NL_SET_ERR_MSG_MOD(extack,
3237			   "DSA conduit cannot join unknown upper interfaces");
3238	return notifier_from_errno(-EBUSY);
3239}
3240
3241static int
3242dsa_lag_conduit_prechangelower_sanity_check(struct net_device *dev,
3243					    struct netdev_notifier_changeupper_info *info)
3244{
3245	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(&info->info);
3246	struct net_device *lag_dev = info->upper_dev;
3247	struct net_device *lower;
3248	struct list_head *iter;
3249
3250	if (!netdev_uses_dsa(lag_dev) || !netif_is_lag_master(lag_dev))
3251		return NOTIFY_DONE;
3252
3253	if (!info->linking)
3254		return NOTIFY_DONE;
3255
3256	if (!netdev_uses_dsa(dev)) {
3257		NL_SET_ERR_MSG(extack,
3258			       "Only DSA conduits can join a LAG DSA conduit");
3259		return notifier_from_errno(-EINVAL);
3260	}
3261
3262	netdev_for_each_lower_dev(lag_dev, lower, iter) {
3263		if (!dsa_port_tree_same(dev->dsa_ptr, lower->dsa_ptr)) {
3264			NL_SET_ERR_MSG(extack,
3265				       "Interface is DSA conduit for a different switch tree than this LAG");
3266			return notifier_from_errno(-EINVAL);
3267		}
3268
3269		break;
3270	}
3271
3272	return NOTIFY_DONE;
3273}
3274
3275/* Don't allow bridging of DSA conduits, since the bridge layer rx_handler
3276 * prevents the DSA fake ethertype handler to be invoked, so we don't get the
3277 * chance to strip off and parse the DSA switch tag protocol header (the bridge
3278 * layer just returns RX_HANDLER_CONSUMED, stopping RX processing for these
3279 * frames).
3280 * The only case where that would not be an issue is when bridging can already
3281 * be offloaded, such as when the DSA conduit is itself a DSA or plain switchdev
3282 * port, and is bridged only with other ports from the same hardware device.
3283 */
3284static int
3285dsa_bridge_prechangelower_sanity_check(struct net_device *new_lower,
3286				       struct netdev_notifier_changeupper_info *info)
3287{
3288	struct net_device *br = info->upper_dev;
3289	struct netlink_ext_ack *extack;
3290	struct net_device *lower;
3291	struct list_head *iter;
3292
3293	if (!netif_is_bridge_master(br))
3294		return NOTIFY_DONE;
3295
3296	if (!info->linking)
3297		return NOTIFY_DONE;
3298
3299	extack = netdev_notifier_info_to_extack(&info->info);
3300
3301	netdev_for_each_lower_dev(br, lower, iter) {
3302		if (!netdev_uses_dsa(new_lower) && !netdev_uses_dsa(lower))
3303			continue;
3304
3305		if (!netdev_port_same_parent_id(lower, new_lower)) {
3306			NL_SET_ERR_MSG(extack,
3307				       "Cannot do software bridging with a DSA conduit");
3308			return notifier_from_errno(-EINVAL);
3309		}
3310	}
3311
3312	return NOTIFY_DONE;
3313}
3314
3315static void dsa_tree_migrate_ports_from_lag_conduit(struct dsa_switch_tree *dst,
3316						    struct net_device *lag_dev)
3317{
3318	struct net_device *new_conduit = dsa_tree_find_first_conduit(dst);
3319	struct dsa_port *dp;
3320	int err;
3321
3322	dsa_tree_for_each_user_port(dp, dst) {
3323		if (dsa_port_to_conduit(dp) != lag_dev)
3324			continue;
3325
3326		err = dsa_user_change_conduit(dp->user, new_conduit, NULL);
3327		if (err) {
3328			netdev_err(dp->user,
3329				   "failed to restore conduit to %s: %pe\n",
3330				   new_conduit->name, ERR_PTR(err));
3331		}
3332	}
3333}
3334
3335static int dsa_conduit_lag_join(struct net_device *conduit,
3336				struct net_device *lag_dev,
3337				struct netdev_lag_upper_info *uinfo,
3338				struct netlink_ext_ack *extack)
3339{
3340	struct dsa_port *cpu_dp = conduit->dsa_ptr;
3341	struct dsa_switch_tree *dst = cpu_dp->dst;
3342	struct dsa_port *dp;
3343	int err;
3344
3345	err = dsa_conduit_lag_setup(lag_dev, cpu_dp, uinfo, extack);
3346	if (err)
3347		return err;
3348
3349	dsa_tree_for_each_user_port(dp, dst) {
3350		if (dsa_port_to_conduit(dp) != conduit)
3351			continue;
3352
3353		err = dsa_user_change_conduit(dp->user, lag_dev, extack);
3354		if (err)
3355			goto restore;
3356	}
3357
3358	return 0;
3359
3360restore:
3361	dsa_tree_for_each_user_port_continue_reverse(dp, dst) {
3362		if (dsa_port_to_conduit(dp) != lag_dev)
3363			continue;
3364
3365		err = dsa_user_change_conduit(dp->user, conduit, NULL);
3366		if (err) {
3367			netdev_err(dp->user,
3368				   "failed to restore conduit to %s: %pe\n",
3369				   conduit->name, ERR_PTR(err));
3370		}
3371	}
3372
3373	dsa_conduit_lag_teardown(lag_dev, conduit->dsa_ptr);
3374
3375	return err;
3376}
3377
3378static void dsa_conduit_lag_leave(struct net_device *conduit,
3379				  struct net_device *lag_dev)
3380{
3381	struct dsa_port *dp, *cpu_dp = lag_dev->dsa_ptr;
3382	struct dsa_switch_tree *dst = cpu_dp->dst;
3383	struct dsa_port *new_cpu_dp = NULL;
3384	struct net_device *lower;
3385	struct list_head *iter;
3386
3387	netdev_for_each_lower_dev(lag_dev, lower, iter) {
3388		if (netdev_uses_dsa(lower)) {
3389			new_cpu_dp = lower->dsa_ptr;
3390			break;
3391		}
3392	}
3393
3394	if (new_cpu_dp) {
3395		/* Update the CPU port of the user ports still under the LAG
3396		 * so that dsa_port_to_conduit() continues to work properly
3397		 */
3398		dsa_tree_for_each_user_port(dp, dst)
3399			if (dsa_port_to_conduit(dp) == lag_dev)
3400				dp->cpu_dp = new_cpu_dp;
3401
3402		/* Update the index of the virtual CPU port to match the lowest
3403		 * physical CPU port
3404		 */
3405		lag_dev->dsa_ptr = new_cpu_dp;
3406		wmb();
3407	} else {
3408		/* If the LAG DSA conduit has no ports left, migrate back all
3409		 * user ports to the first physical CPU port
3410		 */
3411		dsa_tree_migrate_ports_from_lag_conduit(dst, lag_dev);
3412	}
3413
3414	/* This DSA conduit has left its LAG in any case, so let
3415	 * the CPU port leave the hardware LAG as well
3416	 */
3417	dsa_conduit_lag_teardown(lag_dev, conduit->dsa_ptr);
3418}
3419
3420static int dsa_conduit_changeupper(struct net_device *dev,
3421				   struct netdev_notifier_changeupper_info *info)
3422{
3423	struct netlink_ext_ack *extack;
3424	int err = NOTIFY_DONE;
3425
3426	if (!netdev_uses_dsa(dev))
3427		return err;
3428
3429	extack = netdev_notifier_info_to_extack(&info->info);
3430
3431	if (netif_is_lag_master(info->upper_dev)) {
3432		if (info->linking) {
3433			err = dsa_conduit_lag_join(dev, info->upper_dev,
3434						   info->upper_info, extack);
3435			err = notifier_from_errno(err);
3436		} else {
3437			dsa_conduit_lag_leave(dev, info->upper_dev);
3438			err = NOTIFY_OK;
3439		}
3440	}
3441
3442	return err;
3443}
3444
3445static int dsa_user_netdevice_event(struct notifier_block *nb,
3446				    unsigned long event, void *ptr)
3447{
3448	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3449
3450	switch (event) {
3451	case NETDEV_PRECHANGEUPPER: {
3452		struct netdev_notifier_changeupper_info *info = ptr;
3453		int err;
3454
3455		err = dsa_user_prechangeupper_sanity_check(dev, info);
3456		if (notifier_to_errno(err))
3457			return err;
3458
3459		err = dsa_conduit_prechangeupper_sanity_check(dev, info);
3460		if (notifier_to_errno(err))
3461			return err;
3462
3463		err = dsa_lag_conduit_prechangelower_sanity_check(dev, info);
3464		if (notifier_to_errno(err))
3465			return err;
3466
3467		err = dsa_bridge_prechangelower_sanity_check(dev, info);
3468		if (notifier_to_errno(err))
3469			return err;
3470
3471		err = dsa_user_prechangeupper(dev, ptr);
3472		if (notifier_to_errno(err))
3473			return err;
3474
3475		err = dsa_user_lag_prechangeupper(dev, ptr);
3476		if (notifier_to_errno(err))
3477			return err;
3478
3479		break;
3480	}
3481	case NETDEV_CHANGEUPPER: {
3482		int err;
3483
3484		err = dsa_user_changeupper(dev, ptr);
3485		if (notifier_to_errno(err))
3486			return err;
3487
3488		err = dsa_user_lag_changeupper(dev, ptr);
3489		if (notifier_to_errno(err))
3490			return err;
3491
3492		err = dsa_conduit_changeupper(dev, ptr);
3493		if (notifier_to_errno(err))
3494			return err;
3495
3496		break;
3497	}
3498	case NETDEV_CHANGELOWERSTATE: {
3499		struct netdev_notifier_changelowerstate_info *info = ptr;
3500		struct dsa_port *dp;
3501		int err = 0;
3502
3503		if (dsa_user_dev_check(dev)) {
3504			dp = dsa_user_to_port(dev);
3505
3506			err = dsa_port_lag_change(dp, info->lower_state_info);
3507		}
3508
3509		/* Mirror LAG port events on DSA conduits that are in
3510		 * a LAG towards their respective switch CPU ports
3511		 */
3512		if (netdev_uses_dsa(dev)) {
3513			dp = dev->dsa_ptr;
3514
3515			err = dsa_port_lag_change(dp, info->lower_state_info);
3516		}
3517
3518		return notifier_from_errno(err);
3519	}
3520	case NETDEV_CHANGE:
3521	case NETDEV_UP: {
3522		/* Track state of conduit port.
3523		 * DSA driver may require the conduit port (and indirectly
3524		 * the tagger) to be available for some special operation.
3525		 */
3526		if (netdev_uses_dsa(dev)) {
3527			struct dsa_port *cpu_dp = dev->dsa_ptr;
3528			struct dsa_switch_tree *dst = cpu_dp->ds->dst;
3529
3530			/* Track when the conduit port is UP */
3531			dsa_tree_conduit_oper_state_change(dst, dev,
3532							   netif_oper_up(dev));
3533
3534			/* Track when the conduit port is ready and can accept
3535			 * packet.
3536			 * NETDEV_UP event is not enough to flag a port as ready.
3537			 * We also have to wait for linkwatch_do_dev to dev_activate
3538			 * and emit a NETDEV_CHANGE event.
3539			 * We check if a conduit port is ready by checking if the dev
3540			 * have a qdisc assigned and is not noop.
3541			 */
3542			dsa_tree_conduit_admin_state_change(dst, dev,
3543							    !qdisc_tx_is_noop(dev));
3544
3545			return NOTIFY_OK;
3546		}
3547
3548		return NOTIFY_DONE;
3549	}
3550	case NETDEV_GOING_DOWN: {
3551		struct dsa_port *dp, *cpu_dp;
3552		struct dsa_switch_tree *dst;
3553		LIST_HEAD(close_list);
3554
3555		if (!netdev_uses_dsa(dev))
3556			return NOTIFY_DONE;
3557
3558		cpu_dp = dev->dsa_ptr;
3559		dst = cpu_dp->ds->dst;
3560
3561		dsa_tree_conduit_admin_state_change(dst, dev, false);
3562
3563		list_for_each_entry(dp, &dst->ports, list) {
3564			if (!dsa_port_is_user(dp))
3565				continue;
3566
3567			if (dp->cpu_dp != cpu_dp)
3568				continue;
3569
3570			list_add(&dp->user->close_list, &close_list);
3571		}
3572
3573		dev_close_many(&close_list, true);
3574
3575		return NOTIFY_OK;
3576	}
3577	default:
3578		break;
3579	}
3580
3581	return NOTIFY_DONE;
3582}
3583
3584static void
3585dsa_fdb_offload_notify(struct dsa_switchdev_event_work *switchdev_work)
3586{
3587	struct switchdev_notifier_fdb_info info = {};
3588
3589	info.addr = switchdev_work->addr;
3590	info.vid = switchdev_work->vid;
3591	info.offloaded = true;
3592	call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
3593				 switchdev_work->orig_dev, &info.info, NULL);
3594}
3595
3596static void dsa_user_switchdev_event_work(struct work_struct *work)
3597{
3598	struct dsa_switchdev_event_work *switchdev_work =
3599		container_of(work, struct dsa_switchdev_event_work, work);
3600	const unsigned char *addr = switchdev_work->addr;
3601	struct net_device *dev = switchdev_work->dev;
3602	u16 vid = switchdev_work->vid;
3603	struct dsa_switch *ds;
3604	struct dsa_port *dp;
3605	int err;
3606
3607	dp = dsa_user_to_port(dev);
3608	ds = dp->ds;
3609
3610	switch (switchdev_work->event) {
3611	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3612		if (switchdev_work->host_addr)
3613			err = dsa_port_bridge_host_fdb_add(dp, addr, vid);
3614		else if (dp->lag)
3615			err = dsa_port_lag_fdb_add(dp, addr, vid);
3616		else
3617			err = dsa_port_fdb_add(dp, addr, vid);
3618		if (err) {
3619			dev_err(ds->dev,
3620				"port %d failed to add %pM vid %d to fdb: %d\n",
3621				dp->index, addr, vid, err);
3622			break;
3623		}
3624		dsa_fdb_offload_notify(switchdev_work);
3625		break;
3626
3627	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3628		if (switchdev_work->host_addr)
3629			err = dsa_port_bridge_host_fdb_del(dp, addr, vid);
3630		else if (dp->lag)
3631			err = dsa_port_lag_fdb_del(dp, addr, vid);
3632		else
3633			err = dsa_port_fdb_del(dp, addr, vid);
3634		if (err) {
3635			dev_err(ds->dev,
3636				"port %d failed to delete %pM vid %d from fdb: %d\n",
3637				dp->index, addr, vid, err);
3638		}
3639
3640		break;
3641	}
3642
3643	kfree(switchdev_work);
3644}
3645
3646static bool dsa_foreign_dev_check(const struct net_device *dev,
3647				  const struct net_device *foreign_dev)
3648{
3649	const struct dsa_port *dp = dsa_user_to_port(dev);
3650	struct dsa_switch_tree *dst = dp->ds->dst;
3651
3652	if (netif_is_bridge_master(foreign_dev))
3653		return !dsa_tree_offloads_bridge_dev(dst, foreign_dev);
3654
3655	if (netif_is_bridge_port(foreign_dev))
3656		return !dsa_tree_offloads_bridge_port(dst, foreign_dev);
3657
3658	/* Everything else is foreign */
3659	return true;
3660}
3661
3662static int dsa_user_fdb_event(struct net_device *dev,
3663			      struct net_device *orig_dev,
3664			      unsigned long event, const void *ctx,
3665			      const struct switchdev_notifier_fdb_info *fdb_info)
3666{
3667	struct dsa_switchdev_event_work *switchdev_work;
3668	struct dsa_port *dp = dsa_user_to_port(dev);
3669	bool host_addr = fdb_info->is_local;
3670	struct dsa_switch *ds = dp->ds;
3671
3672	if (ctx && ctx != dp)
3673		return 0;
3674
3675	if (!dp->bridge)
3676		return 0;
3677
3678	if (switchdev_fdb_is_dynamically_learned(fdb_info)) {
3679		if (dsa_port_offloads_bridge_port(dp, orig_dev))
3680			return 0;
3681
3682		/* FDB entries learned by the software bridge or by foreign
3683		 * bridge ports should be installed as host addresses only if
3684		 * the driver requests assisted learning.
3685		 */
3686		if (!ds->assisted_learning_on_cpu_port)
3687			return 0;
3688	}
3689
3690	/* Also treat FDB entries on foreign interfaces bridged with us as host
3691	 * addresses.
3692	 */
3693	if (dsa_foreign_dev_check(dev, orig_dev))
3694		host_addr = true;
3695
3696	/* Check early that we're not doing work in vain.
3697	 * Host addresses on LAG ports still require regular FDB ops,
3698	 * since the CPU port isn't in a LAG.
3699	 */
3700	if (dp->lag && !host_addr) {
3701		if (!ds->ops->lag_fdb_add || !ds->ops->lag_fdb_del)
3702			return -EOPNOTSUPP;
3703	} else {
3704		if (!ds->ops->port_fdb_add || !ds->ops->port_fdb_del)
3705			return -EOPNOTSUPP;
3706	}
3707
3708	switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3709	if (!switchdev_work)
3710		return -ENOMEM;
3711
3712	netdev_dbg(dev, "%s FDB entry towards %s, addr %pM vid %d%s\n",
3713		   event == SWITCHDEV_FDB_ADD_TO_DEVICE ? "Adding" : "Deleting",
3714		   orig_dev->name, fdb_info->addr, fdb_info->vid,
3715		   host_addr ? " as host address" : "");
3716
3717	INIT_WORK(&switchdev_work->work, dsa_user_switchdev_event_work);
3718	switchdev_work->event = event;
3719	switchdev_work->dev = dev;
3720	switchdev_work->orig_dev = orig_dev;
3721
3722	ether_addr_copy(switchdev_work->addr, fdb_info->addr);
3723	switchdev_work->vid = fdb_info->vid;
3724	switchdev_work->host_addr = host_addr;
3725
3726	dsa_schedule_work(&switchdev_work->work);
3727
3728	return 0;
3729}
3730
3731/* Called under rcu_read_lock() */
3732static int dsa_user_switchdev_event(struct notifier_block *unused,
3733				    unsigned long event, void *ptr)
3734{
3735	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3736	int err;
3737
3738	switch (event) {
3739	case SWITCHDEV_PORT_ATTR_SET:
3740		err = switchdev_handle_port_attr_set(dev, ptr,
3741						     dsa_user_dev_check,
3742						     dsa_user_port_attr_set);
3743		return notifier_from_errno(err);
3744	case SWITCHDEV_FDB_ADD_TO_DEVICE:
3745	case SWITCHDEV_FDB_DEL_TO_DEVICE:
3746		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
3747							   dsa_user_dev_check,
3748							   dsa_foreign_dev_check,
3749							   dsa_user_fdb_event);
3750		return notifier_from_errno(err);
3751	default:
3752		return NOTIFY_DONE;
3753	}
3754
3755	return NOTIFY_OK;
3756}
3757
3758static int dsa_user_switchdev_blocking_event(struct notifier_block *unused,
3759					     unsigned long event, void *ptr)
3760{
3761	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3762	int err;
3763
3764	switch (event) {
3765	case SWITCHDEV_PORT_OBJ_ADD:
3766		err = switchdev_handle_port_obj_add_foreign(dev, ptr,
3767							    dsa_user_dev_check,
3768							    dsa_foreign_dev_check,
3769							    dsa_user_port_obj_add);
3770		return notifier_from_errno(err);
3771	case SWITCHDEV_PORT_OBJ_DEL:
3772		err = switchdev_handle_port_obj_del_foreign(dev, ptr,
3773							    dsa_user_dev_check,
3774							    dsa_foreign_dev_check,
3775							    dsa_user_port_obj_del);
3776		return notifier_from_errno(err);
3777	case SWITCHDEV_PORT_ATTR_SET:
3778		err = switchdev_handle_port_attr_set(dev, ptr,
3779						     dsa_user_dev_check,
3780						     dsa_user_port_attr_set);
3781		return notifier_from_errno(err);
3782	}
3783
3784	return NOTIFY_DONE;
3785}
3786
3787static struct notifier_block dsa_user_nb __read_mostly = {
3788	.notifier_call  = dsa_user_netdevice_event,
3789};
3790
3791struct notifier_block dsa_user_switchdev_notifier = {
3792	.notifier_call = dsa_user_switchdev_event,
3793};
3794
3795struct notifier_block dsa_user_switchdev_blocking_notifier = {
3796	.notifier_call = dsa_user_switchdev_blocking_event,
3797};
3798
3799int dsa_user_register_notifier(void)
3800{
3801	struct notifier_block *nb;
3802	int err;
3803
3804	err = register_netdevice_notifier(&dsa_user_nb);
3805	if (err)
3806		return err;
3807
3808	err = register_switchdev_notifier(&dsa_user_switchdev_notifier);
3809	if (err)
3810		goto err_switchdev_nb;
3811
3812	nb = &dsa_user_switchdev_blocking_notifier;
3813	err = register_switchdev_blocking_notifier(nb);
3814	if (err)
3815		goto err_switchdev_blocking_nb;
3816
3817	return 0;
3818
3819err_switchdev_blocking_nb:
3820	unregister_switchdev_notifier(&dsa_user_switchdev_notifier);
3821err_switchdev_nb:
3822	unregister_netdevice_notifier(&dsa_user_nb);
3823	return err;
3824}
3825
3826void dsa_user_unregister_notifier(void)
3827{
3828	struct notifier_block *nb;
3829	int err;
3830
3831	nb = &dsa_user_switchdev_blocking_notifier;
3832	err = unregister_switchdev_blocking_notifier(nb);
3833	if (err)
3834		pr_err("DSA: failed to unregister switchdev blocking notifier (%d)\n", err);
3835
3836	err = unregister_switchdev_notifier(&dsa_user_switchdev_notifier);
3837	if (err)
3838		pr_err("DSA: failed to unregister switchdev notifier (%d)\n", err);
3839
3840	err = unregister_netdevice_notifier(&dsa_user_nb);
3841	if (err)
3842		pr_err("DSA: failed to unregister user notifier (%d)\n", err);
3843}