Linux Audio

Check our new training course

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