Linux Audio

Check our new training course

Yocto distribution development and maintenance

Need a Yocto distribution for your embedded project?
Loading...
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Handling of a single switch port
   4 *
   5 * Copyright (c) 2017 Savoir-faire Linux Inc.
   6 *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
   7 */
   8
   9#include <linux/if_bridge.h>
  10#include <linux/notifier.h>
  11#include <linux/of_mdio.h>
  12#include <linux/of_net.h>
  13
  14#include "dsa_priv.h"
  15
  16/**
  17 * dsa_port_notify - Notify the switching fabric of changes to a port
  18 * @dp: port on which change occurred
  19 * @e: event, must be of type DSA_NOTIFIER_*
  20 * @v: event-specific value.
  21 *
  22 * Notify all switches in the DSA tree that this port's switch belongs to,
  23 * including this switch itself, of an event. Allows the other switches to
  24 * reconfigure themselves for cross-chip operations. Can also be used to
  25 * reconfigure ports without net_devices (CPU ports, DSA links) whenever
  26 * a user port's state changes.
  27 */
  28static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
  29{
  30	return dsa_tree_notify(dp->ds->dst, e, v);
 
 
 
 
 
  31}
  32
  33int dsa_port_set_state(struct dsa_port *dp, u8 state)
 
  34{
  35	struct dsa_switch *ds = dp->ds;
  36	int port = dp->index;
  37
  38	if (!ds->ops->port_stp_state_set)
  39		return -EOPNOTSUPP;
  40
  41	ds->ops->port_stp_state_set(ds, port, state);
 
  42
  43	if (ds->ops->port_fast_age) {
  44		/* Fast age FDB entries or flush appropriate forwarding database
  45		 * for the given port, if we are moving it from Learning or
  46		 * Forwarding state, to Disabled or Blocking or Listening state.
  47		 */
  48
  49		if ((dp->stp_state == BR_STATE_LEARNING ||
  50		     dp->stp_state == BR_STATE_FORWARDING) &&
  51		    (state == BR_STATE_DISABLED ||
  52		     state == BR_STATE_BLOCKING ||
  53		     state == BR_STATE_LISTENING))
  54			ds->ops->port_fast_age(ds, port);
  55	}
  56
  57	dp->stp_state = state;
  58
  59	return 0;
  60}
  61
  62static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
  63{
  64	int err;
  65
  66	err = dsa_port_set_state(dp, state);
  67	if (err)
  68		pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
  69}
  70
  71int dsa_port_enable_rt(struct dsa_port *dp, struct phy_device *phy)
  72{
  73	struct dsa_switch *ds = dp->ds;
  74	int port = dp->index;
  75	int err;
  76
  77	if (ds->ops->port_enable) {
  78		err = ds->ops->port_enable(ds, port, phy);
  79		if (err)
  80			return err;
  81	}
  82
  83	if (!dp->bridge_dev)
  84		dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
  85
  86	if (dp->pl)
  87		phylink_start(dp->pl);
  88
  89	return 0;
  90}
  91
  92int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
  93{
  94	int err;
  95
  96	rtnl_lock();
  97	err = dsa_port_enable_rt(dp, phy);
  98	rtnl_unlock();
  99
 100	return err;
 101}
 102
 103void dsa_port_disable_rt(struct dsa_port *dp)
 104{
 105	struct dsa_switch *ds = dp->ds;
 106	int port = dp->index;
 107
 108	if (dp->pl)
 109		phylink_stop(dp->pl);
 110
 111	if (!dp->bridge_dev)
 112		dsa_port_set_state_now(dp, BR_STATE_DISABLED);
 113
 114	if (ds->ops->port_disable)
 115		ds->ops->port_disable(ds, port);
 116}
 117
 118void dsa_port_disable(struct dsa_port *dp)
 119{
 120	rtnl_lock();
 121	dsa_port_disable_rt(dp);
 122	rtnl_unlock();
 123}
 124
 125static int dsa_port_inherit_brport_flags(struct dsa_port *dp,
 126					 struct netlink_ext_ack *extack)
 127{
 128	const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
 129				   BR_BCAST_FLOOD;
 130	struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
 131	int flag, err;
 132
 133	for_each_set_bit(flag, &mask, 32) {
 134		struct switchdev_brport_flags flags = {0};
 135
 136		flags.mask = BIT(flag);
 137
 138		if (br_port_flag_is_set(brport_dev, BIT(flag)))
 139			flags.val = BIT(flag);
 140
 141		err = dsa_port_bridge_flags(dp, flags, extack);
 142		if (err && err != -EOPNOTSUPP)
 143			return err;
 144	}
 145
 146	return 0;
 147}
 148
 149static void dsa_port_clear_brport_flags(struct dsa_port *dp)
 150{
 151	const unsigned long val = BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
 152	const unsigned long mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD |
 153				   BR_BCAST_FLOOD;
 154	int flag, err;
 155
 156	for_each_set_bit(flag, &mask, 32) {
 157		struct switchdev_brport_flags flags = {0};
 158
 159		flags.mask = BIT(flag);
 160		flags.val = val & BIT(flag);
 161
 162		err = dsa_port_bridge_flags(dp, flags, NULL);
 163		if (err && err != -EOPNOTSUPP)
 164			dev_err(dp->ds->dev,
 165				"failed to clear bridge port flag %lu: %pe\n",
 166				flags.val, ERR_PTR(err));
 167	}
 168}
 169
 170static int dsa_port_switchdev_sync(struct dsa_port *dp,
 171				   struct netlink_ext_ack *extack)
 172{
 173	struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
 174	struct net_device *br = dp->bridge_dev;
 175	int err;
 176
 177	err = dsa_port_inherit_brport_flags(dp, extack);
 178	if (err)
 179		return err;
 180
 181	err = dsa_port_set_state(dp, br_port_get_stp_state(brport_dev));
 182	if (err && err != -EOPNOTSUPP)
 183		return err;
 184
 185	err = dsa_port_vlan_filtering(dp, br_vlan_enabled(br), extack);
 186	if (err && err != -EOPNOTSUPP)
 187		return err;
 188
 189	err = dsa_port_ageing_time(dp, br_get_ageing_time(br));
 190	if (err && err != -EOPNOTSUPP)
 191		return err;
 192
 193	err = br_mdb_replay(br, brport_dev, dp, true,
 194			    &dsa_slave_switchdev_blocking_notifier, extack);
 195	if (err && err != -EOPNOTSUPP)
 196		return err;
 197
 198	/* Forwarding and termination FDB entries on the port */
 199	err = br_fdb_replay(br, brport_dev, dp, true,
 200			    &dsa_slave_switchdev_notifier);
 201	if (err && err != -EOPNOTSUPP)
 202		return err;
 203
 204	/* Termination FDB entries on the bridge itself */
 205	err = br_fdb_replay(br, br, dp, true, &dsa_slave_switchdev_notifier);
 206	if (err && err != -EOPNOTSUPP)
 207		return err;
 208
 209	err = br_vlan_replay(br, brport_dev, dp, true,
 210			     &dsa_slave_switchdev_blocking_notifier, extack);
 211	if (err && err != -EOPNOTSUPP)
 212		return err;
 213
 214	return 0;
 215}
 216
 217static int dsa_port_switchdev_unsync_objs(struct dsa_port *dp,
 218					  struct net_device *br,
 219					  struct netlink_ext_ack *extack)
 220{
 221	struct net_device *brport_dev = dsa_port_to_bridge_port(dp);
 222	int err;
 223
 224	/* Delete the switchdev objects left on this port */
 225	err = br_mdb_replay(br, brport_dev, dp, false,
 226			    &dsa_slave_switchdev_blocking_notifier, extack);
 227	if (err && err != -EOPNOTSUPP)
 228		return err;
 229
 230	/* Forwarding and termination FDB entries on the port */
 231	err = br_fdb_replay(br, brport_dev, dp, false,
 232			    &dsa_slave_switchdev_notifier);
 233	if (err && err != -EOPNOTSUPP)
 234		return err;
 235
 236	/* Termination FDB entries on the bridge itself */
 237	err = br_fdb_replay(br, br, dp, false, &dsa_slave_switchdev_notifier);
 238	if (err && err != -EOPNOTSUPP)
 239		return err;
 240
 241	err = br_vlan_replay(br, brport_dev, dp, false,
 242			     &dsa_slave_switchdev_blocking_notifier, extack);
 243	if (err && err != -EOPNOTSUPP)
 244		return err;
 245
 246	return 0;
 247}
 248
 249static void dsa_port_switchdev_unsync_attrs(struct dsa_port *dp)
 250{
 251	/* Configure the port for standalone mode (no address learning,
 252	 * flood everything).
 253	 * The bridge only emits SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS events
 254	 * when the user requests it through netlink or sysfs, but not
 255	 * automatically at port join or leave, so we need to handle resetting
 256	 * the brport flags ourselves. But we even prefer it that way, because
 257	 * otherwise, some setups might never get the notification they need,
 258	 * for example, when a port leaves a LAG that offloads the bridge,
 259	 * it becomes standalone, but as far as the bridge is concerned, no
 260	 * port ever left.
 261	 */
 262	dsa_port_clear_brport_flags(dp);
 263
 264	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
 265	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
 266	 */
 267	dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
 268
 269	/* VLAN filtering is handled by dsa_switch_bridge_leave */
 270
 271	/* Ageing time may be global to the switch chip, so don't change it
 272	 * here because we have no good reason (or value) to change it to.
 273	 */
 274}
 275
 276int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br,
 277			 struct netlink_ext_ack *extack)
 278{
 279	struct dsa_notifier_bridge_info info = {
 280		.tree_index = dp->ds->dst->index,
 281		.sw_index = dp->ds->index,
 282		.port = dp->index,
 283		.br = br,
 284	};
 285	int err;
 286
 
 
 
 
 
 287	/* Here the interface is already bridged. Reflect the current
 288	 * configuration so that drivers can program their chips accordingly.
 289	 */
 290	dp->bridge_dev = br;
 291
 292	err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_JOIN, &info);
 293	if (err)
 294		goto out_rollback;
 295
 296	err = dsa_port_switchdev_sync(dp, extack);
 297	if (err)
 298		goto out_rollback_unbridge;
 299
 300	return 0;
 
 
 
 
 301
 302out_rollback_unbridge:
 303	dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
 304out_rollback:
 305	dp->bridge_dev = NULL;
 306	return err;
 307}
 308
 309int dsa_port_pre_bridge_leave(struct dsa_port *dp, struct net_device *br,
 310			      struct netlink_ext_ack *extack)
 311{
 312	return dsa_port_switchdev_unsync_objs(dp, br, extack);
 313}
 314
 315void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
 316{
 317	struct dsa_notifier_bridge_info info = {
 318		.tree_index = dp->ds->dst->index,
 319		.sw_index = dp->ds->index,
 320		.port = dp->index,
 321		.br = br,
 322	};
 323	int err;
 324
 325	/* Here the port is already unbridged. Reflect the current configuration
 326	 * so that drivers can program their chips accordingly.
 327	 */
 328	dp->bridge_dev = NULL;
 329
 330	err = dsa_broadcast(DSA_NOTIFIER_BRIDGE_LEAVE, &info);
 331	if (err)
 332		pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
 333
 334	dsa_port_switchdev_unsync_attrs(dp);
 335}
 336
 337int dsa_port_lag_change(struct dsa_port *dp,
 338			struct netdev_lag_lower_state_info *linfo)
 339{
 340	struct dsa_notifier_lag_info info = {
 341		.sw_index = dp->ds->index,
 342		.port = dp->index,
 343	};
 344	bool tx_enabled;
 345
 346	if (!dp->lag_dev)
 347		return 0;
 348
 349	/* On statically configured aggregates (e.g. loadbalance
 350	 * without LACP) ports will always be tx_enabled, even if the
 351	 * link is down. Thus we require both link_up and tx_enabled
 352	 * in order to include it in the tx set.
 353	 */
 354	tx_enabled = linfo->link_up && linfo->tx_enabled;
 355
 356	if (tx_enabled == dp->lag_tx_enabled)
 357		return 0;
 358
 359	dp->lag_tx_enabled = tx_enabled;
 360
 361	return dsa_port_notify(dp, DSA_NOTIFIER_LAG_CHANGE, &info);
 362}
 363
 364int dsa_port_lag_join(struct dsa_port *dp, struct net_device *lag,
 365		      struct netdev_lag_upper_info *uinfo,
 366		      struct netlink_ext_ack *extack)
 367{
 368	struct dsa_notifier_lag_info info = {
 369		.sw_index = dp->ds->index,
 370		.port = dp->index,
 371		.lag = lag,
 372		.info = uinfo,
 373	};
 374	struct net_device *bridge_dev;
 375	int err;
 376
 377	dsa_lag_map(dp->ds->dst, lag);
 378	dp->lag_dev = lag;
 379
 380	err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_JOIN, &info);
 381	if (err)
 382		goto err_lag_join;
 383
 384	bridge_dev = netdev_master_upper_dev_get(lag);
 385	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
 386		return 0;
 387
 388	err = dsa_port_bridge_join(dp, bridge_dev, extack);
 389	if (err)
 390		goto err_bridge_join;
 391
 392	return 0;
 393
 394err_bridge_join:
 395	dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
 396err_lag_join:
 397	dp->lag_dev = NULL;
 398	dsa_lag_unmap(dp->ds->dst, lag);
 399	return err;
 400}
 401
 402int dsa_port_pre_lag_leave(struct dsa_port *dp, struct net_device *lag,
 403			   struct netlink_ext_ack *extack)
 404{
 405	if (dp->bridge_dev)
 406		return dsa_port_pre_bridge_leave(dp, dp->bridge_dev, extack);
 407
 408	return 0;
 409}
 410
 411void dsa_port_lag_leave(struct dsa_port *dp, struct net_device *lag)
 412{
 413	struct dsa_notifier_lag_info info = {
 414		.sw_index = dp->ds->index,
 415		.port = dp->index,
 416		.lag = lag,
 417	};
 418	int err;
 419
 420	if (!dp->lag_dev)
 421		return;
 422
 423	/* Port might have been part of a LAG that in turn was
 424	 * attached to a bridge.
 425	 */
 426	if (dp->bridge_dev)
 427		dsa_port_bridge_leave(dp, dp->bridge_dev);
 428
 429	dp->lag_tx_enabled = false;
 430	dp->lag_dev = NULL;
 431
 432	err = dsa_port_notify(dp, DSA_NOTIFIER_LAG_LEAVE, &info);
 433	if (err)
 434		pr_err("DSA: failed to notify DSA_NOTIFIER_LAG_LEAVE: %d\n",
 435		       err);
 436
 437	dsa_lag_unmap(dp->ds->dst, lag);
 438}
 439
 440/* Must be called under rcu_read_lock() */
 441static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
 442					      bool vlan_filtering,
 443					      struct netlink_ext_ack *extack)
 444{
 445	struct dsa_switch *ds = dp->ds;
 446	int err, i;
 447
 448	/* VLAN awareness was off, so the question is "can we turn it on".
 449	 * We may have had 8021q uppers, those need to go. Make sure we don't
 450	 * enter an inconsistent state: deny changing the VLAN awareness state
 451	 * as long as we have 8021q uppers.
 452	 */
 453	if (vlan_filtering && dsa_is_user_port(ds, dp->index)) {
 454		struct net_device *upper_dev, *slave = dp->slave;
 455		struct net_device *br = dp->bridge_dev;
 456		struct list_head *iter;
 457
 458		netdev_for_each_upper_dev_rcu(slave, upper_dev, iter) {
 459			struct bridge_vlan_info br_info;
 460			u16 vid;
 461
 462			if (!is_vlan_dev(upper_dev))
 463				continue;
 464
 465			vid = vlan_dev_vlan_id(upper_dev);
 466
 467			/* br_vlan_get_info() returns -EINVAL or -ENOENT if the
 468			 * device, respectively the VID is not found, returning
 469			 * 0 means success, which is a failure for us here.
 470			 */
 471			err = br_vlan_get_info(br, vid, &br_info);
 472			if (err == 0) {
 473				NL_SET_ERR_MSG_MOD(extack,
 474						   "Must first remove VLAN uppers having VIDs also present in bridge");
 475				return false;
 476			}
 477		}
 478	}
 479
 480	if (!ds->vlan_filtering_is_global)
 481		return true;
 482
 483	/* For cases where enabling/disabling VLAN awareness is global to the
 484	 * switch, we need to handle the case where multiple bridges span
 485	 * different ports of the same switch device and one of them has a
 486	 * different setting than what is being requested.
 487	 */
 488	for (i = 0; i < ds->num_ports; i++) {
 489		struct net_device *other_bridge;
 490
 491		other_bridge = dsa_to_port(ds, i)->bridge_dev;
 492		if (!other_bridge)
 493			continue;
 494		/* If it's the same bridge, it also has same
 495		 * vlan_filtering setting => no need to check
 496		 */
 497		if (other_bridge == dp->bridge_dev)
 498			continue;
 499		if (br_vlan_enabled(other_bridge) != vlan_filtering) {
 500			NL_SET_ERR_MSG_MOD(extack,
 501					   "VLAN filtering is a global setting");
 502			return false;
 503		}
 504	}
 505	return true;
 506}
 507
 508int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
 509			    struct netlink_ext_ack *extack)
 510{
 511	struct dsa_switch *ds = dp->ds;
 512	bool apply;
 513	int err;
 514
 
 
 
 
 515	if (!ds->ops->port_vlan_filtering)
 516		return -EOPNOTSUPP;
 517
 518	/* We are called from dsa_slave_switchdev_blocking_event(),
 519	 * which is not under rcu_read_lock(), unlike
 520	 * dsa_slave_switchdev_event().
 521	 */
 522	rcu_read_lock();
 523	apply = dsa_port_can_apply_vlan_filtering(dp, vlan_filtering, extack);
 524	rcu_read_unlock();
 525	if (!apply)
 526		return -EINVAL;
 527
 528	if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
 529		return 0;
 530
 531	err = ds->ops->port_vlan_filtering(ds, dp->index, vlan_filtering,
 532					   extack);
 533	if (err)
 534		return err;
 535
 536	if (ds->vlan_filtering_is_global)
 537		ds->vlan_filtering = vlan_filtering;
 538	else
 539		dp->vlan_filtering = vlan_filtering;
 540
 541	return 0;
 542}
 543
 544/* This enforces legacy behavior for switch drivers which assume they can't
 545 * receive VLAN configuration when enslaved to a bridge with vlan_filtering=0
 546 */
 547bool dsa_port_skip_vlan_configuration(struct dsa_port *dp)
 548{
 549	struct dsa_switch *ds = dp->ds;
 550
 551	if (!dp->bridge_dev)
 552		return false;
 553
 554	return (!ds->configure_vlan_while_not_filtering &&
 555		!br_vlan_enabled(dp->bridge_dev));
 556}
 557
 558int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock)
 559{
 560	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
 561	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
 562	struct dsa_notifier_ageing_time_info info;
 563	int err;
 
 
 564
 565	info.ageing_time = ageing_time;
 566
 567	err = dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
 568	if (err)
 569		return err;
 570
 571	dp->ageing_time = ageing_time;
 572
 573	return 0;
 574}
 575
 576int dsa_port_pre_bridge_flags(const struct dsa_port *dp,
 577			      struct switchdev_brport_flags flags,
 578			      struct netlink_ext_ack *extack)
 579{
 580	struct dsa_switch *ds = dp->ds;
 581
 582	if (!ds->ops->port_pre_bridge_flags)
 
 583		return -EINVAL;
 584
 585	return ds->ops->port_pre_bridge_flags(ds, dp->index, flags, extack);
 586}
 587
 588int dsa_port_bridge_flags(const struct dsa_port *dp,
 589			  struct switchdev_brport_flags flags,
 590			  struct netlink_ext_ack *extack)
 591{
 592	struct dsa_switch *ds = dp->ds;
 
 
 593
 594	if (!ds->ops->port_bridge_flags)
 595		return -EOPNOTSUPP;
 596
 597	return ds->ops->port_bridge_flags(ds, dp->index, flags, extack);
 
 
 
 
 598}
 599
 600int dsa_port_mtu_change(struct dsa_port *dp, int new_mtu,
 601			bool targeted_match)
 602{
 603	struct dsa_notifier_mtu_info info = {
 604		.sw_index = dp->ds->index,
 605		.targeted_match = targeted_match,
 606		.port = dp->index,
 607		.mtu = new_mtu,
 608	};
 609
 610	return dsa_port_notify(dp, DSA_NOTIFIER_MTU, &info);
 
 
 
 611}
 612
 613int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
 614		     u16 vid)
 615{
 616	struct dsa_notifier_fdb_info info = {
 617		.sw_index = dp->ds->index,
 618		.port = dp->index,
 619		.addr = addr,
 620		.vid = vid,
 621	};
 622
 623	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
 624}
 625
 626int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
 627		     u16 vid)
 628{
 629	struct dsa_notifier_fdb_info info = {
 630		.sw_index = dp->ds->index,
 631		.port = dp->index,
 632		.addr = addr,
 633		.vid = vid,
 634
 635	};
 636
 637	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
 638}
 639
 640int dsa_port_host_fdb_add(struct dsa_port *dp, const unsigned char *addr,
 641			  u16 vid)
 642{
 643	struct dsa_notifier_fdb_info info = {
 644		.sw_index = dp->ds->index,
 645		.port = dp->index,
 646		.addr = addr,
 647		.vid = vid,
 648	};
 649	struct dsa_port *cpu_dp = dp->cpu_dp;
 650	int err;
 651
 652	err = dev_uc_add(cpu_dp->master, addr);
 653	if (err)
 654		return err;
 655
 656	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_ADD, &info);
 657}
 658
 659int dsa_port_host_fdb_del(struct dsa_port *dp, const unsigned char *addr,
 660			  u16 vid)
 661{
 662	struct dsa_notifier_fdb_info info = {
 663		.sw_index = dp->ds->index,
 664		.port = dp->index,
 665		.addr = addr,
 666		.vid = vid,
 667	};
 668	struct dsa_port *cpu_dp = dp->cpu_dp;
 669	int err;
 670
 671	err = dev_uc_del(cpu_dp->master, addr);
 672	if (err)
 673		return err;
 674
 675	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_FDB_DEL, &info);
 676}
 677
 678int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
 679{
 680	struct dsa_switch *ds = dp->ds;
 681	int port = dp->index;
 682
 683	if (!ds->ops->port_fdb_dump)
 684		return -EOPNOTSUPP;
 685
 686	return ds->ops->port_fdb_dump(ds, port, cb, data);
 687}
 688
 689int dsa_port_mdb_add(const struct dsa_port *dp,
 690		     const struct switchdev_obj_port_mdb *mdb)
 
 691{
 692	struct dsa_notifier_mdb_info info = {
 693		.sw_index = dp->ds->index,
 694		.port = dp->index,
 
 695		.mdb = mdb,
 696	};
 697
 698	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
 699}
 700
 701int dsa_port_mdb_del(const struct dsa_port *dp,
 702		     const struct switchdev_obj_port_mdb *mdb)
 703{
 704	struct dsa_notifier_mdb_info info = {
 705		.sw_index = dp->ds->index,
 706		.port = dp->index,
 707		.mdb = mdb,
 708	};
 709
 710	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
 711}
 712
 713int dsa_port_host_mdb_add(const struct dsa_port *dp,
 714			  const struct switchdev_obj_port_mdb *mdb)
 715{
 716	struct dsa_notifier_mdb_info info = {
 717		.sw_index = dp->ds->index,
 718		.port = dp->index,
 719		.mdb = mdb,
 720	};
 721	struct dsa_port *cpu_dp = dp->cpu_dp;
 722	int err;
 723
 724	err = dev_mc_add(cpu_dp->master, mdb->addr);
 725	if (err)
 726		return err;
 727
 728	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_ADD, &info);
 729}
 730
 731int dsa_port_host_mdb_del(const struct dsa_port *dp,
 732			  const struct switchdev_obj_port_mdb *mdb)
 733{
 734	struct dsa_notifier_mdb_info info = {
 735		.sw_index = dp->ds->index,
 736		.port = dp->index,
 737		.mdb = mdb,
 738	};
 739	struct dsa_port *cpu_dp = dp->cpu_dp;
 740	int err;
 741
 742	err = dev_mc_del(cpu_dp->master, mdb->addr);
 743	if (err)
 744		return err;
 745
 746	return dsa_port_notify(dp, DSA_NOTIFIER_HOST_MDB_DEL, &info);
 747}
 748
 749int dsa_port_vlan_add(struct dsa_port *dp,
 750		      const struct switchdev_obj_port_vlan *vlan,
 751		      struct netlink_ext_ack *extack)
 752{
 753	struct dsa_notifier_vlan_info info = {
 754		.sw_index = dp->ds->index,
 755		.port = dp->index,
 
 756		.vlan = vlan,
 757		.extack = extack,
 758	};
 759
 760	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
 761}
 762
 763int dsa_port_vlan_del(struct dsa_port *dp,
 764		      const struct switchdev_obj_port_vlan *vlan)
 765{
 766	struct dsa_notifier_vlan_info info = {
 767		.sw_index = dp->ds->index,
 768		.port = dp->index,
 769		.vlan = vlan,
 770	};
 771
 772	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
 773}
 774
 775int dsa_port_mrp_add(const struct dsa_port *dp,
 776		     const struct switchdev_obj_mrp *mrp)
 777{
 778	struct dsa_notifier_mrp_info info = {
 779		.sw_index = dp->ds->index,
 780		.port = dp->index,
 781		.mrp = mrp,
 782	};
 783
 784	return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD, &info);
 785}
 786
 787int dsa_port_mrp_del(const struct dsa_port *dp,
 788		     const struct switchdev_obj_mrp *mrp)
 789{
 790	struct dsa_notifier_mrp_info info = {
 791		.sw_index = dp->ds->index,
 792		.port = dp->index,
 793		.mrp = mrp,
 
 794	};
 
 
 795
 796	return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL, &info);
 797}
 798
 799int dsa_port_mrp_add_ring_role(const struct dsa_port *dp,
 800			       const struct switchdev_obj_ring_role_mrp *mrp)
 801{
 802	struct dsa_notifier_mrp_ring_role_info info = {
 803		.sw_index = dp->ds->index,
 804		.port = dp->index,
 805		.mrp = mrp,
 806	};
 807
 808	return dsa_port_notify(dp, DSA_NOTIFIER_MRP_ADD_RING_ROLE, &info);
 
 809}
 
 810
 811int dsa_port_mrp_del_ring_role(const struct dsa_port *dp,
 812			       const struct switchdev_obj_ring_role_mrp *mrp)
 813{
 814	struct dsa_notifier_mrp_ring_role_info info = {
 815		.sw_index = dp->ds->index,
 816		.port = dp->index,
 817		.mrp = mrp,
 818	};
 819
 820	return dsa_port_notify(dp, DSA_NOTIFIER_MRP_DEL_RING_ROLE, &info);
 821}
 822
 823void dsa_port_set_tag_protocol(struct dsa_port *cpu_dp,
 824			       const struct dsa_device_ops *tag_ops)
 825{
 826	cpu_dp->filter = tag_ops->filter;
 827	cpu_dp->rcv = tag_ops->rcv;
 828	cpu_dp->tag_ops = tag_ops;
 829}
 
 830
 831static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
 832{
 833	struct device_node *phy_dn;
 834	struct phy_device *phydev;
 835
 836	phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
 837	if (!phy_dn)
 838		return NULL;
 839
 840	phydev = of_phy_find_device(phy_dn);
 841	if (!phydev) {
 842		of_node_put(phy_dn);
 843		return ERR_PTR(-EPROBE_DEFER);
 844	}
 845
 846	of_node_put(phy_dn);
 847	return phydev;
 848}
 849
 850static void dsa_port_phylink_validate(struct phylink_config *config,
 851				      unsigned long *supported,
 852				      struct phylink_link_state *state)
 853{
 854	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
 855	struct dsa_switch *ds = dp->ds;
 856
 857	if (!ds->ops->phylink_validate)
 858		return;
 859
 860	ds->ops->phylink_validate(ds, dp->index, supported, state);
 861}
 
 862
 863static void dsa_port_phylink_mac_pcs_get_state(struct phylink_config *config,
 864					       struct phylink_link_state *state)
 865{
 866	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
 867	struct dsa_switch *ds = dp->ds;
 868	int err;
 869
 870	/* Only called for inband modes */
 871	if (!ds->ops->phylink_mac_link_state) {
 872		state->link = 0;
 873		return;
 874	}
 875
 876	err = ds->ops->phylink_mac_link_state(ds, dp->index, state);
 877	if (err < 0) {
 878		dev_err(ds->dev, "p%d: phylink_mac_link_state() failed: %d\n",
 879			dp->index, err);
 880		state->link = 0;
 881	}
 882}
 
 883
 884static void dsa_port_phylink_mac_config(struct phylink_config *config,
 885					unsigned int mode,
 886					const struct phylink_link_state *state)
 887{
 888	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
 889	struct dsa_switch *ds = dp->ds;
 890
 891	if (!ds->ops->phylink_mac_config)
 892		return;
 893
 894	ds->ops->phylink_mac_config(ds, dp->index, mode, state);
 895}
 
 896
 897static void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
 898{
 899	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
 900	struct dsa_switch *ds = dp->ds;
 901
 902	if (!ds->ops->phylink_mac_an_restart)
 903		return;
 904
 905	ds->ops->phylink_mac_an_restart(ds, dp->index);
 906}
 
 907
 908static void dsa_port_phylink_mac_link_down(struct phylink_config *config,
 909					   unsigned int mode,
 910					   phy_interface_t interface)
 911{
 912	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
 913	struct phy_device *phydev = NULL;
 914	struct dsa_switch *ds = dp->ds;
 915
 916	if (dsa_is_user_port(ds, dp->index))
 917		phydev = dp->slave->phydev;
 918
 919	if (!ds->ops->phylink_mac_link_down) {
 920		if (ds->ops->adjust_link && phydev)
 921			ds->ops->adjust_link(ds, dp->index, phydev);
 922		return;
 923	}
 924
 925	ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
 926}
 
 927
 928static void dsa_port_phylink_mac_link_up(struct phylink_config *config,
 929					 struct phy_device *phydev,
 930					 unsigned int mode,
 931					 phy_interface_t interface,
 932					 int speed, int duplex,
 933					 bool tx_pause, bool rx_pause)
 934{
 935	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
 936	struct dsa_switch *ds = dp->ds;
 937
 938	if (!ds->ops->phylink_mac_link_up) {
 939		if (ds->ops->adjust_link && phydev)
 940			ds->ops->adjust_link(ds, dp->index, phydev);
 941		return;
 942	}
 943
 944	ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev,
 945				     speed, duplex, tx_pause, rx_pause);
 946}
 
 947
 948const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
 949	.validate = dsa_port_phylink_validate,
 950	.mac_pcs_get_state = dsa_port_phylink_mac_pcs_get_state,
 951	.mac_config = dsa_port_phylink_mac_config,
 952	.mac_an_restart = dsa_port_phylink_mac_an_restart,
 953	.mac_link_down = dsa_port_phylink_mac_link_down,
 954	.mac_link_up = dsa_port_phylink_mac_link_up,
 955};
 956
 957static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
 958{
 959	struct dsa_switch *ds = dp->ds;
 960	struct phy_device *phydev;
 961	int port = dp->index;
 962	int err = 0;
 963
 964	phydev = dsa_port_get_phy_device(dp);
 965	if (!phydev)
 966		return 0;
 967
 968	if (IS_ERR(phydev))
 969		return PTR_ERR(phydev);
 970
 971	if (enable) {
 972		err = genphy_resume(phydev);
 973		if (err < 0)
 974			goto err_put_dev;
 975
 976		err = genphy_read_status(phydev);
 977		if (err < 0)
 978			goto err_put_dev;
 979	} else {
 980		err = genphy_suspend(phydev);
 981		if (err < 0)
 982			goto err_put_dev;
 983	}
 984
 985	if (ds->ops->adjust_link)
 986		ds->ops->adjust_link(ds, port, phydev);
 987
 988	dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
 989
 990err_put_dev:
 991	put_device(&phydev->mdio.dev);
 992	return err;
 993}
 994
 995static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
 996{
 997	struct device_node *dn = dp->dn;
 998	struct dsa_switch *ds = dp->ds;
 999	struct phy_device *phydev;
1000	int port = dp->index;
1001	phy_interface_t mode;
1002	int err;
1003
1004	err = of_phy_register_fixed_link(dn);
1005	if (err) {
1006		dev_err(ds->dev,
1007			"failed to register the fixed PHY of port %d\n",
1008			port);
1009		return err;
1010	}
1011
1012	phydev = of_phy_find_device(dn);
1013
1014	err = of_get_phy_mode(dn, &mode);
1015	if (err)
1016		mode = PHY_INTERFACE_MODE_NA;
1017	phydev->interface = mode;
1018
1019	genphy_read_status(phydev);
1020
1021	if (ds->ops->adjust_link)
1022		ds->ops->adjust_link(ds, port, phydev);
1023
1024	put_device(&phydev->mdio.dev);
1025
1026	return 0;
1027}
1028
1029static int dsa_port_phylink_register(struct dsa_port *dp)
1030{
1031	struct dsa_switch *ds = dp->ds;
1032	struct device_node *port_dn = dp->dn;
1033	phy_interface_t mode;
1034	int err;
1035
1036	err = of_get_phy_mode(port_dn, &mode);
1037	if (err)
1038		mode = PHY_INTERFACE_MODE_NA;
1039
1040	dp->pl_config.dev = ds->dev;
1041	dp->pl_config.type = PHYLINK_DEV;
1042	dp->pl_config.pcs_poll = ds->pcs_poll;
1043
1044	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
1045				mode, &dsa_port_phylink_mac_ops);
1046	if (IS_ERR(dp->pl)) {
1047		pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
1048		return PTR_ERR(dp->pl);
1049	}
1050
1051	err = phylink_of_phy_connect(dp->pl, port_dn, 0);
1052	if (err && err != -ENODEV) {
1053		pr_err("could not attach to PHY: %d\n", err);
1054		goto err_phy_connect;
1055	}
1056
 
 
 
 
1057	return 0;
1058
1059err_phy_connect:
1060	phylink_destroy(dp->pl);
1061	return err;
1062}
1063
1064int dsa_port_link_register_of(struct dsa_port *dp)
1065{
1066	struct dsa_switch *ds = dp->ds;
1067	struct device_node *phy_np;
1068	int port = dp->index;
1069
1070	if (!ds->ops->adjust_link) {
1071		phy_np = of_parse_phandle(dp->dn, "phy-handle", 0);
1072		if (of_phy_is_fixed_link(dp->dn) || phy_np) {
1073			if (ds->ops->phylink_mac_link_down)
1074				ds->ops->phylink_mac_link_down(ds, port,
1075					MLO_AN_FIXED, PHY_INTERFACE_MODE_NA);
1076			return dsa_port_phylink_register(dp);
1077		}
1078		return 0;
1079	}
1080
1081	dev_warn(ds->dev,
1082		 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
1083
1084	if (of_phy_is_fixed_link(dp->dn))
1085		return dsa_port_fixed_link_register_of(dp);
1086	else
1087		return dsa_port_setup_phy_of(dp, true);
1088}
1089
1090void dsa_port_link_unregister_of(struct dsa_port *dp)
1091{
1092	struct dsa_switch *ds = dp->ds;
1093
1094	if (!ds->ops->adjust_link && dp->pl) {
1095		rtnl_lock();
1096		phylink_disconnect_phy(dp->pl);
1097		rtnl_unlock();
1098		phylink_destroy(dp->pl);
1099		dp->pl = NULL;
1100		return;
1101	}
1102
1103	if (of_phy_is_fixed_link(dp->dn))
1104		of_phy_deregister_fixed_link(dp->dn);
1105	else
1106		dsa_port_setup_phy_of(dp, false);
1107}
1108
1109int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
1110{
1111	struct phy_device *phydev;
1112	int ret = -EOPNOTSUPP;
1113
1114	if (of_phy_is_fixed_link(dp->dn))
1115		return ret;
1116
1117	phydev = dsa_port_get_phy_device(dp);
1118	if (IS_ERR_OR_NULL(phydev))
1119		return ret;
1120
1121	ret = phy_ethtool_get_strings(phydev, data);
1122	put_device(&phydev->mdio.dev);
1123
1124	return ret;
1125}
1126EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
1127
1128int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
1129{
1130	struct phy_device *phydev;
1131	int ret = -EOPNOTSUPP;
1132
1133	if (of_phy_is_fixed_link(dp->dn))
1134		return ret;
1135
1136	phydev = dsa_port_get_phy_device(dp);
1137	if (IS_ERR_OR_NULL(phydev))
1138		return ret;
1139
1140	ret = phy_ethtool_get_stats(phydev, NULL, data);
1141	put_device(&phydev->mdio.dev);
1142
1143	return ret;
1144}
1145EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
1146
1147int dsa_port_get_phy_sset_count(struct dsa_port *dp)
1148{
1149	struct phy_device *phydev;
1150	int ret = -EOPNOTSUPP;
1151
1152	if (of_phy_is_fixed_link(dp->dn))
1153		return ret;
1154
1155	phydev = dsa_port_get_phy_device(dp);
1156	if (IS_ERR_OR_NULL(phydev))
1157		return ret;
1158
1159	ret = phy_ethtool_get_sset_count(phydev);
1160	put_device(&phydev->mdio.dev);
1161
1162	return ret;
1163}
1164EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);
1165
1166int dsa_port_hsr_join(struct dsa_port *dp, struct net_device *hsr)
1167{
1168	struct dsa_notifier_hsr_info info = {
1169		.sw_index = dp->ds->index,
1170		.port = dp->index,
1171		.hsr = hsr,
1172	};
1173	int err;
1174
1175	dp->hsr_dev = hsr;
1176
1177	err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_JOIN, &info);
1178	if (err)
1179		dp->hsr_dev = NULL;
1180
1181	return err;
1182}
1183
1184void dsa_port_hsr_leave(struct dsa_port *dp, struct net_device *hsr)
1185{
1186	struct dsa_notifier_hsr_info info = {
1187		.sw_index = dp->ds->index,
1188		.port = dp->index,
1189		.hsr = hsr,
1190	};
1191	int err;
1192
1193	dp->hsr_dev = NULL;
1194
1195	err = dsa_port_notify(dp, DSA_NOTIFIER_HSR_LEAVE, &info);
1196	if (err)
1197		pr_err("DSA: failed to notify DSA_NOTIFIER_HSR_LEAVE\n");
1198}
v5.4
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Handling of a single switch port
  4 *
  5 * Copyright (c) 2017 Savoir-faire Linux Inc.
  6 *	Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  7 */
  8
  9#include <linux/if_bridge.h>
 10#include <linux/notifier.h>
 11#include <linux/of_mdio.h>
 12#include <linux/of_net.h>
 13
 14#include "dsa_priv.h"
 15
 
 
 
 
 
 
 
 
 
 
 
 
 16static int dsa_port_notify(const struct dsa_port *dp, unsigned long e, void *v)
 17{
 18	struct raw_notifier_head *nh = &dp->ds->dst->nh;
 19	int err;
 20
 21	err = raw_notifier_call_chain(nh, e, v);
 22
 23	return notifier_to_errno(err);
 24}
 25
 26int dsa_port_set_state(struct dsa_port *dp, u8 state,
 27		       struct switchdev_trans *trans)
 28{
 29	struct dsa_switch *ds = dp->ds;
 30	int port = dp->index;
 31
 32	if (switchdev_trans_ph_prepare(trans))
 33		return ds->ops->port_stp_state_set ? 0 : -EOPNOTSUPP;
 34
 35	if (ds->ops->port_stp_state_set)
 36		ds->ops->port_stp_state_set(ds, port, state);
 37
 38	if (ds->ops->port_fast_age) {
 39		/* Fast age FDB entries or flush appropriate forwarding database
 40		 * for the given port, if we are moving it from Learning or
 41		 * Forwarding state, to Disabled or Blocking or Listening state.
 42		 */
 43
 44		if ((dp->stp_state == BR_STATE_LEARNING ||
 45		     dp->stp_state == BR_STATE_FORWARDING) &&
 46		    (state == BR_STATE_DISABLED ||
 47		     state == BR_STATE_BLOCKING ||
 48		     state == BR_STATE_LISTENING))
 49			ds->ops->port_fast_age(ds, port);
 50	}
 51
 52	dp->stp_state = state;
 53
 54	return 0;
 55}
 56
 57static void dsa_port_set_state_now(struct dsa_port *dp, u8 state)
 58{
 59	int err;
 60
 61	err = dsa_port_set_state(dp, state, NULL);
 62	if (err)
 63		pr_err("DSA: failed to set STP state %u (%d)\n", state, err);
 64}
 65
 66int dsa_port_enable(struct dsa_port *dp, struct phy_device *phy)
 67{
 68	struct dsa_switch *ds = dp->ds;
 69	int port = dp->index;
 70	int err;
 71
 72	if (ds->ops->port_enable) {
 73		err = ds->ops->port_enable(ds, port, phy);
 74		if (err)
 75			return err;
 76	}
 77
 78	if (!dp->bridge_dev)
 79		dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
 80
 
 
 
 81	return 0;
 82}
 83
 84void dsa_port_disable(struct dsa_port *dp)
 
 
 
 
 
 
 
 
 
 
 
 85{
 86	struct dsa_switch *ds = dp->ds;
 87	int port = dp->index;
 88
 
 
 
 89	if (!dp->bridge_dev)
 90		dsa_port_set_state_now(dp, BR_STATE_DISABLED);
 91
 92	if (ds->ops->port_disable)
 93		ds->ops->port_disable(ds, port);
 94}
 95
 96int dsa_port_bridge_join(struct dsa_port *dp, struct net_device *br)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 97{
 98	struct dsa_notifier_bridge_info info = {
 
 99		.sw_index = dp->ds->index,
100		.port = dp->index,
101		.br = br,
102	};
103	int err;
104
105	/* Set the flooding mode before joining the port in the switch */
106	err = dsa_port_bridge_flags(dp, BR_FLOOD | BR_MCAST_FLOOD, NULL);
107	if (err)
108		return err;
109
110	/* Here the interface is already bridged. Reflect the current
111	 * configuration so that drivers can program their chips accordingly.
112	 */
113	dp->bridge_dev = br;
114
115	err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_JOIN, &info);
 
 
 
 
 
 
116
117	/* The bridging is rolled back on error */
118	if (err) {
119		dsa_port_bridge_flags(dp, 0, NULL);
120		dp->bridge_dev = NULL;
121	}
122
 
 
 
 
123	return err;
124}
125
 
 
 
 
 
 
126void dsa_port_bridge_leave(struct dsa_port *dp, struct net_device *br)
127{
128	struct dsa_notifier_bridge_info info = {
 
129		.sw_index = dp->ds->index,
130		.port = dp->index,
131		.br = br,
132	};
133	int err;
134
135	/* Here the port is already unbridged. Reflect the current configuration
136	 * so that drivers can program their chips accordingly.
137	 */
138	dp->bridge_dev = NULL;
139
140	err = dsa_port_notify(dp, DSA_NOTIFIER_BRIDGE_LEAVE, &info);
141	if (err)
142		pr_err("DSA: failed to notify DSA_NOTIFIER_BRIDGE_LEAVE\n");
143
144	/* Port is leaving the bridge, disable flooding */
145	dsa_port_bridge_flags(dp, 0, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
147	/* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
148	 * so allow it to be in BR_STATE_FORWARDING to be kept functional
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149	 */
150	dsa_port_set_state_now(dp, BR_STATE_FORWARDING);
 
 
 
 
 
 
 
 
 
 
 
151}
152
 
153static bool dsa_port_can_apply_vlan_filtering(struct dsa_port *dp,
154					      bool vlan_filtering)
 
155{
156	struct dsa_switch *ds = dp->ds;
157	int i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
159	if (!ds->vlan_filtering_is_global)
160		return true;
161
162	/* For cases where enabling/disabling VLAN awareness is global to the
163	 * switch, we need to handle the case where multiple bridges span
164	 * different ports of the same switch device and one of them has a
165	 * different setting than what is being requested.
166	 */
167	for (i = 0; i < ds->num_ports; i++) {
168		struct net_device *other_bridge;
169
170		other_bridge = dsa_to_port(ds, i)->bridge_dev;
171		if (!other_bridge)
172			continue;
173		/* If it's the same bridge, it also has same
174		 * vlan_filtering setting => no need to check
175		 */
176		if (other_bridge == dp->bridge_dev)
177			continue;
178		if (br_vlan_enabled(other_bridge) != vlan_filtering) {
179			dev_err(ds->dev, "VLAN filtering is a global setting\n");
 
180			return false;
181		}
182	}
183	return true;
184}
185
186int dsa_port_vlan_filtering(struct dsa_port *dp, bool vlan_filtering,
187			    struct switchdev_trans *trans)
188{
189	struct dsa_switch *ds = dp->ds;
 
190	int err;
191
192	/* bridge skips -EOPNOTSUPP, so skip the prepare phase */
193	if (switchdev_trans_ph_prepare(trans))
194		return 0;
195
196	if (!ds->ops->port_vlan_filtering)
197		return 0;
198
199	if (!dsa_port_can_apply_vlan_filtering(dp, vlan_filtering))
 
 
 
 
 
 
 
200		return -EINVAL;
201
202	if (dsa_port_is_vlan_filtering(dp) == vlan_filtering)
203		return 0;
204
205	err = ds->ops->port_vlan_filtering(ds, dp->index,
206					   vlan_filtering);
207	if (err)
208		return err;
209
210	if (ds->vlan_filtering_is_global)
211		ds->vlan_filtering = vlan_filtering;
212	else
213		dp->vlan_filtering = vlan_filtering;
 
214	return 0;
215}
216
217int dsa_port_ageing_time(struct dsa_port *dp, clock_t ageing_clock,
218			 struct switchdev_trans *trans)
 
 
 
 
 
 
 
 
 
 
 
 
 
219{
220	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock);
221	unsigned int ageing_time = jiffies_to_msecs(ageing_jiffies);
222	struct dsa_notifier_ageing_time_info info = {
223		.ageing_time = ageing_time,
224		.trans = trans,
225	};
226
227	if (switchdev_trans_ph_prepare(trans))
228		return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
 
 
 
229
230	dp->ageing_time = ageing_time;
231
232	return dsa_port_notify(dp, DSA_NOTIFIER_AGEING_TIME, &info);
233}
234
235int dsa_port_pre_bridge_flags(const struct dsa_port *dp, unsigned long flags,
236			      struct switchdev_trans *trans)
 
237{
238	struct dsa_switch *ds = dp->ds;
239
240	if (!ds->ops->port_egress_floods ||
241	    (flags & ~(BR_FLOOD | BR_MCAST_FLOOD)))
242		return -EINVAL;
243
244	return 0;
245}
246
247int dsa_port_bridge_flags(const struct dsa_port *dp, unsigned long flags,
248			  struct switchdev_trans *trans)
 
249{
250	struct dsa_switch *ds = dp->ds;
251	int port = dp->index;
252	int err = 0;
253
254	if (switchdev_trans_ph_prepare(trans))
255		return 0;
256
257	if (ds->ops->port_egress_floods)
258		err = ds->ops->port_egress_floods(ds, port, flags & BR_FLOOD,
259						  flags & BR_MCAST_FLOOD);
260
261	return err;
262}
263
264int dsa_port_mrouter(struct dsa_port *dp, bool mrouter,
265		     struct switchdev_trans *trans)
266{
267	struct dsa_switch *ds = dp->ds;
268	int port = dp->index;
 
 
 
 
269
270	if (switchdev_trans_ph_prepare(trans))
271		return ds->ops->port_egress_floods ? 0 : -EOPNOTSUPP;
272
273	return ds->ops->port_egress_floods(ds, port, true, mrouter);
274}
275
276int dsa_port_fdb_add(struct dsa_port *dp, const unsigned char *addr,
277		     u16 vid)
278{
279	struct dsa_notifier_fdb_info info = {
280		.sw_index = dp->ds->index,
281		.port = dp->index,
282		.addr = addr,
283		.vid = vid,
284	};
285
286	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_ADD, &info);
287}
288
289int dsa_port_fdb_del(struct dsa_port *dp, const unsigned char *addr,
290		     u16 vid)
291{
292	struct dsa_notifier_fdb_info info = {
293		.sw_index = dp->ds->index,
294		.port = dp->index,
295		.addr = addr,
296		.vid = vid,
297
298	};
299
300	return dsa_port_notify(dp, DSA_NOTIFIER_FDB_DEL, &info);
301}
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303int dsa_port_fdb_dump(struct dsa_port *dp, dsa_fdb_dump_cb_t *cb, void *data)
304{
305	struct dsa_switch *ds = dp->ds;
306	int port = dp->index;
307
308	if (!ds->ops->port_fdb_dump)
309		return -EOPNOTSUPP;
310
311	return ds->ops->port_fdb_dump(ds, port, cb, data);
312}
313
314int dsa_port_mdb_add(const struct dsa_port *dp,
315		     const struct switchdev_obj_port_mdb *mdb,
316		     struct switchdev_trans *trans)
317{
318	struct dsa_notifier_mdb_info info = {
319		.sw_index = dp->ds->index,
320		.port = dp->index,
321		.trans = trans,
322		.mdb = mdb,
323	};
324
325	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_ADD, &info);
326}
327
328int dsa_port_mdb_del(const struct dsa_port *dp,
329		     const struct switchdev_obj_port_mdb *mdb)
330{
331	struct dsa_notifier_mdb_info info = {
332		.sw_index = dp->ds->index,
333		.port = dp->index,
334		.mdb = mdb,
335	};
336
337	return dsa_port_notify(dp, DSA_NOTIFIER_MDB_DEL, &info);
338}
339
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340int dsa_port_vlan_add(struct dsa_port *dp,
341		      const struct switchdev_obj_port_vlan *vlan,
342		      struct switchdev_trans *trans)
343{
344	struct dsa_notifier_vlan_info info = {
345		.sw_index = dp->ds->index,
346		.port = dp->index,
347		.trans = trans,
348		.vlan = vlan,
 
349	};
350
351	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_ADD, &info);
352}
353
354int dsa_port_vlan_del(struct dsa_port *dp,
355		      const struct switchdev_obj_port_vlan *vlan)
356{
357	struct dsa_notifier_vlan_info info = {
358		.sw_index = dp->ds->index,
359		.port = dp->index,
360		.vlan = vlan,
361	};
362
363	return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
364}
365
366int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags)
 
 
 
 
 
 
 
 
 
 
 
 
 
367{
368	struct switchdev_obj_port_vlan vlan = {
369		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
370		.flags = flags,
371		.vid_begin = vid,
372		.vid_end = vid,
373	};
374	struct switchdev_trans trans;
375	int err;
376
377	trans.ph_prepare = true;
378	err = dsa_port_vlan_add(dp, &vlan, &trans);
379	if (err)
380		return err;
 
 
 
 
 
 
 
381
382	trans.ph_prepare = false;
383	return dsa_port_vlan_add(dp, &vlan, &trans);
384}
385EXPORT_SYMBOL(dsa_port_vid_add);
386
387int dsa_port_vid_del(struct dsa_port *dp, u16 vid)
 
388{
389	struct switchdev_obj_port_vlan vlan = {
390		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
391		.vid_begin = vid,
392		.vid_end = vid,
393	};
394
395	return dsa_port_vlan_del(dp, &vlan);
 
 
 
 
 
 
 
 
396}
397EXPORT_SYMBOL(dsa_port_vid_del);
398
399static struct phy_device *dsa_port_get_phy_device(struct dsa_port *dp)
400{
401	struct device_node *phy_dn;
402	struct phy_device *phydev;
403
404	phy_dn = of_parse_phandle(dp->dn, "phy-handle", 0);
405	if (!phy_dn)
406		return NULL;
407
408	phydev = of_phy_find_device(phy_dn);
409	if (!phydev) {
410		of_node_put(phy_dn);
411		return ERR_PTR(-EPROBE_DEFER);
412	}
413
414	of_node_put(phy_dn);
415	return phydev;
416}
417
418void dsa_port_phylink_validate(struct phylink_config *config,
419			       unsigned long *supported,
420			       struct phylink_link_state *state)
421{
422	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
423	struct dsa_switch *ds = dp->ds;
424
425	if (!ds->ops->phylink_validate)
426		return;
427
428	ds->ops->phylink_validate(ds, dp->index, supported, state);
429}
430EXPORT_SYMBOL_GPL(dsa_port_phylink_validate);
431
432int dsa_port_phylink_mac_link_state(struct phylink_config *config,
433				    struct phylink_link_state *state)
434{
435	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
436	struct dsa_switch *ds = dp->ds;
 
437
438	/* Only called for SGMII and 802.3z */
439	if (!ds->ops->phylink_mac_link_state)
440		return -EOPNOTSUPP;
 
 
441
442	return ds->ops->phylink_mac_link_state(ds, dp->index, state);
 
 
 
 
 
443}
444EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_state);
445
446void dsa_port_phylink_mac_config(struct phylink_config *config,
447				 unsigned int mode,
448				 const struct phylink_link_state *state)
449{
450	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
451	struct dsa_switch *ds = dp->ds;
452
453	if (!ds->ops->phylink_mac_config)
454		return;
455
456	ds->ops->phylink_mac_config(ds, dp->index, mode, state);
457}
458EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_config);
459
460void dsa_port_phylink_mac_an_restart(struct phylink_config *config)
461{
462	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
463	struct dsa_switch *ds = dp->ds;
464
465	if (!ds->ops->phylink_mac_an_restart)
466		return;
467
468	ds->ops->phylink_mac_an_restart(ds, dp->index);
469}
470EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_an_restart);
471
472void dsa_port_phylink_mac_link_down(struct phylink_config *config,
473				    unsigned int mode,
474				    phy_interface_t interface)
475{
476	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
477	struct phy_device *phydev = NULL;
478	struct dsa_switch *ds = dp->ds;
479
480	if (dsa_is_user_port(ds, dp->index))
481		phydev = dp->slave->phydev;
482
483	if (!ds->ops->phylink_mac_link_down) {
484		if (ds->ops->adjust_link && phydev)
485			ds->ops->adjust_link(ds, dp->index, phydev);
486		return;
487	}
488
489	ds->ops->phylink_mac_link_down(ds, dp->index, mode, interface);
490}
491EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_down);
492
493void dsa_port_phylink_mac_link_up(struct phylink_config *config,
494				  unsigned int mode,
495				  phy_interface_t interface,
496				  struct phy_device *phydev)
 
 
497{
498	struct dsa_port *dp = container_of(config, struct dsa_port, pl_config);
499	struct dsa_switch *ds = dp->ds;
500
501	if (!ds->ops->phylink_mac_link_up) {
502		if (ds->ops->adjust_link && phydev)
503			ds->ops->adjust_link(ds, dp->index, phydev);
504		return;
505	}
506
507	ds->ops->phylink_mac_link_up(ds, dp->index, mode, interface, phydev);
 
508}
509EXPORT_SYMBOL_GPL(dsa_port_phylink_mac_link_up);
510
511const struct phylink_mac_ops dsa_port_phylink_mac_ops = {
512	.validate = dsa_port_phylink_validate,
513	.mac_link_state = dsa_port_phylink_mac_link_state,
514	.mac_config = dsa_port_phylink_mac_config,
515	.mac_an_restart = dsa_port_phylink_mac_an_restart,
516	.mac_link_down = dsa_port_phylink_mac_link_down,
517	.mac_link_up = dsa_port_phylink_mac_link_up,
518};
519
520static int dsa_port_setup_phy_of(struct dsa_port *dp, bool enable)
521{
522	struct dsa_switch *ds = dp->ds;
523	struct phy_device *phydev;
524	int port = dp->index;
525	int err = 0;
526
527	phydev = dsa_port_get_phy_device(dp);
528	if (!phydev)
529		return 0;
530
531	if (IS_ERR(phydev))
532		return PTR_ERR(phydev);
533
534	if (enable) {
535		err = genphy_resume(phydev);
536		if (err < 0)
537			goto err_put_dev;
538
539		err = genphy_read_status(phydev);
540		if (err < 0)
541			goto err_put_dev;
542	} else {
543		err = genphy_suspend(phydev);
544		if (err < 0)
545			goto err_put_dev;
546	}
547
548	if (ds->ops->adjust_link)
549		ds->ops->adjust_link(ds, port, phydev);
550
551	dev_dbg(ds->dev, "enabled port's phy: %s", phydev_name(phydev));
552
553err_put_dev:
554	put_device(&phydev->mdio.dev);
555	return err;
556}
557
558static int dsa_port_fixed_link_register_of(struct dsa_port *dp)
559{
560	struct device_node *dn = dp->dn;
561	struct dsa_switch *ds = dp->ds;
562	struct phy_device *phydev;
563	int port = dp->index;
564	int mode;
565	int err;
566
567	err = of_phy_register_fixed_link(dn);
568	if (err) {
569		dev_err(ds->dev,
570			"failed to register the fixed PHY of port %d\n",
571			port);
572		return err;
573	}
574
575	phydev = of_phy_find_device(dn);
576
577	mode = of_get_phy_mode(dn);
578	if (mode < 0)
579		mode = PHY_INTERFACE_MODE_NA;
580	phydev->interface = mode;
581
582	genphy_read_status(phydev);
583
584	if (ds->ops->adjust_link)
585		ds->ops->adjust_link(ds, port, phydev);
586
587	put_device(&phydev->mdio.dev);
588
589	return 0;
590}
591
592static int dsa_port_phylink_register(struct dsa_port *dp)
593{
594	struct dsa_switch *ds = dp->ds;
595	struct device_node *port_dn = dp->dn;
596	int mode, err;
 
597
598	mode = of_get_phy_mode(port_dn);
599	if (mode < 0)
600		mode = PHY_INTERFACE_MODE_NA;
601
602	dp->pl_config.dev = ds->dev;
603	dp->pl_config.type = PHYLINK_DEV;
 
604
605	dp->pl = phylink_create(&dp->pl_config, of_fwnode_handle(port_dn),
606				mode, &dsa_port_phylink_mac_ops);
607	if (IS_ERR(dp->pl)) {
608		pr_err("error creating PHYLINK: %ld\n", PTR_ERR(dp->pl));
609		return PTR_ERR(dp->pl);
610	}
611
612	err = phylink_of_phy_connect(dp->pl, port_dn, 0);
613	if (err && err != -ENODEV) {
614		pr_err("could not attach to PHY: %d\n", err);
615		goto err_phy_connect;
616	}
617
618	rtnl_lock();
619	phylink_start(dp->pl);
620	rtnl_unlock();
621
622	return 0;
623
624err_phy_connect:
625	phylink_destroy(dp->pl);
626	return err;
627}
628
629int dsa_port_link_register_of(struct dsa_port *dp)
630{
631	struct dsa_switch *ds = dp->ds;
 
 
632
633	if (!ds->ops->adjust_link)
634		return dsa_port_phylink_register(dp);
 
 
 
 
 
 
 
 
635
636	dev_warn(ds->dev,
637		 "Using legacy PHYLIB callbacks. Please migrate to PHYLINK!\n");
638
639	if (of_phy_is_fixed_link(dp->dn))
640		return dsa_port_fixed_link_register_of(dp);
641	else
642		return dsa_port_setup_phy_of(dp, true);
643}
644
645void dsa_port_link_unregister_of(struct dsa_port *dp)
646{
647	struct dsa_switch *ds = dp->ds;
648
649	if (!ds->ops->adjust_link) {
650		rtnl_lock();
651		phylink_disconnect_phy(dp->pl);
652		rtnl_unlock();
653		phylink_destroy(dp->pl);
 
654		return;
655	}
656
657	if (of_phy_is_fixed_link(dp->dn))
658		of_phy_deregister_fixed_link(dp->dn);
659	else
660		dsa_port_setup_phy_of(dp, false);
661}
662
663int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data)
664{
665	struct phy_device *phydev;
666	int ret = -EOPNOTSUPP;
667
668	if (of_phy_is_fixed_link(dp->dn))
669		return ret;
670
671	phydev = dsa_port_get_phy_device(dp);
672	if (IS_ERR_OR_NULL(phydev))
673		return ret;
674
675	ret = phy_ethtool_get_strings(phydev, data);
676	put_device(&phydev->mdio.dev);
677
678	return ret;
679}
680EXPORT_SYMBOL_GPL(dsa_port_get_phy_strings);
681
682int dsa_port_get_ethtool_phy_stats(struct dsa_port *dp, uint64_t *data)
683{
684	struct phy_device *phydev;
685	int ret = -EOPNOTSUPP;
686
687	if (of_phy_is_fixed_link(dp->dn))
688		return ret;
689
690	phydev = dsa_port_get_phy_device(dp);
691	if (IS_ERR_OR_NULL(phydev))
692		return ret;
693
694	ret = phy_ethtool_get_stats(phydev, NULL, data);
695	put_device(&phydev->mdio.dev);
696
697	return ret;
698}
699EXPORT_SYMBOL_GPL(dsa_port_get_ethtool_phy_stats);
700
701int dsa_port_get_phy_sset_count(struct dsa_port *dp)
702{
703	struct phy_device *phydev;
704	int ret = -EOPNOTSUPP;
705
706	if (of_phy_is_fixed_link(dp->dn))
707		return ret;
708
709	phydev = dsa_port_get_phy_device(dp);
710	if (IS_ERR_OR_NULL(phydev))
711		return ret;
712
713	ret = phy_ethtool_get_sset_count(phydev);
714	put_device(&phydev->mdio.dev);
715
716	return ret;
717}
718EXPORT_SYMBOL_GPL(dsa_port_get_phy_sset_count);