File Index
Files and directories
Sign in
FILE

ocelot_net.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/ethernet/mscc/ocelot_net.c C
   1// SPDX-License-Identifier: (GPL-2.0 OR MIT)
   2/* Microsemi Ocelot Switch driver
   3 *
   4 * This contains glue logic between the switchdev driver operations and the
   5 * mscc_ocelot_switch_lib.
   6 *
   7 * Copyright (c) 2017, 2019 Microsemi Corporation
   8 * Copyright 2020-2021 NXP
   9 */
  10
  11#include <linux/dsa/ocelot.h>
  12#include <linux/if_bridge.h>
  13#include <linux/of_net.h>
  14#include <linux/phy/phy.h>
  15#include <net/pkt_cls.h>
  16#include "ocelot.h"
  17#include "ocelot_police.h"
  18#include "ocelot_vcap.h"
  19#include "ocelot_fdma.h"
  20
  21#define OCELOT_MAC_QUIRKS	OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP
  22
  23struct ocelot_dump_ctx {
  24	struct net_device *dev;
  25	struct sk_buff *skb;
  26	struct netlink_callback *cb;
  27	int idx;
  28};
  29
  30static bool ocelot_netdevice_dev_check(const struct net_device *dev);
  31
  32static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
  33{
  34	return devlink_priv(dlp->devlink);
  35}
  36
  37static int devlink_port_to_port(struct devlink_port *dlp)
  38{
  39	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  40
  41	return dlp - ocelot->devlink_ports;
  42}
  43
  44static int ocelot_devlink_sb_pool_get(struct devlink *dl,
  45				      unsigned int sb_index, u16 pool_index,
  46				      struct devlink_sb_pool_info *pool_info)
  47{
  48	struct ocelot *ocelot = devlink_priv(dl);
  49
  50	return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
  51}
  52
  53static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
  54				      u16 pool_index, u32 size,
  55				      enum devlink_sb_threshold_type threshold_type,
  56				      struct netlink_ext_ack *extack)
  57{
  58	struct ocelot *ocelot = devlink_priv(dl);
  59
  60	return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
  61				  threshold_type, extack);
  62}
  63
  64static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
  65					   unsigned int sb_index, u16 pool_index,
  66					   u32 *p_threshold)
  67{
  68	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  69	int port = devlink_port_to_port(dlp);
  70
  71	return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
  72				       p_threshold);
  73}
  74
  75static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
  76					   unsigned int sb_index, u16 pool_index,
  77					   u32 threshold,
  78					   struct netlink_ext_ack *extack)
  79{
  80	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  81	int port = devlink_port_to_port(dlp);
  82
  83	return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
  84				       threshold, extack);
  85}
  86
  87static int
  88ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
  89				   unsigned int sb_index, u16 tc_index,
  90				   enum devlink_sb_pool_type pool_type,
  91				   u16 *p_pool_index, u32 *p_threshold)
  92{
  93	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  94	int port = devlink_port_to_port(dlp);
  95
  96	return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
  97					  pool_type, p_pool_index,
  98					  p_threshold);
  99}
 100
 101static int
 102ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
 103				   unsigned int sb_index, u16 tc_index,
 104				   enum devlink_sb_pool_type pool_type,
 105				   u16 pool_index, u32 threshold,
 106				   struct netlink_ext_ack *extack)
 107{
 108	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
 109	int port = devlink_port_to_port(dlp);
 110
 111	return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
 112					  pool_type, pool_index, threshold,
 113					  extack);
 114}
 115
 116static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
 117					  unsigned int sb_index)
 118{
 119	struct ocelot *ocelot = devlink_priv(dl);
 120
 121	return ocelot_sb_occ_snapshot(ocelot, sb_index);
 122}
 123
 124static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
 125					   unsigned int sb_index)
 126{
 127	struct ocelot *ocelot = devlink_priv(dl);
 128
 129	return ocelot_sb_occ_max_clear(ocelot, sb_index);
 130}
 131
 132static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
 133					       unsigned int sb_index,
 134					       u16 pool_index, u32 *p_cur,
 135					       u32 *p_max)
 136{
 137	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
 138	int port = devlink_port_to_port(dlp);
 139
 140	return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
 141					   p_cur, p_max);
 142}
 143
 144static int
 145ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
 146				       unsigned int sb_index, u16 tc_index,
 147				       enum devlink_sb_pool_type pool_type,
 148				       u32 *p_cur, u32 *p_max)
 149{
 150	struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
 151	int port = devlink_port_to_port(dlp);
 152
 153	return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
 154					      tc_index, pool_type,
 155					      p_cur, p_max);
 156}
 157
 158const struct devlink_ops ocelot_devlink_ops = {
 159	.sb_pool_get			= ocelot_devlink_sb_pool_get,
 160	.sb_pool_set			= ocelot_devlink_sb_pool_set,
 161	.sb_port_pool_get		= ocelot_devlink_sb_port_pool_get,
 162	.sb_port_pool_set		= ocelot_devlink_sb_port_pool_set,
 163	.sb_tc_pool_bind_get		= ocelot_devlink_sb_tc_pool_bind_get,
 164	.sb_tc_pool_bind_set		= ocelot_devlink_sb_tc_pool_bind_set,
 165	.sb_occ_snapshot		= ocelot_devlink_sb_occ_snapshot,
 166	.sb_occ_max_clear		= ocelot_devlink_sb_occ_max_clear,
 167	.sb_occ_port_pool_get		= ocelot_devlink_sb_occ_port_pool_get,
 168	.sb_occ_tc_port_bind_get	= ocelot_devlink_sb_occ_tc_port_bind_get,
 169};
 170
 171int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
 172			     enum devlink_port_flavour flavour)
 173{
 174	struct devlink_port *dlp = &ocelot->devlink_ports[port];
 175	int id_len = sizeof(ocelot->base_mac);
 176	struct devlink *dl = ocelot->devlink;
 177	struct devlink_port_attrs attrs = {};
 178
 179	memset(dlp, 0, sizeof(*dlp));
 180	memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
 181	attrs.switch_id.id_len = id_len;
 182	attrs.phys.port_number = port;
 183	attrs.flavour = flavour;
 184
 185	devlink_port_attrs_set(dlp, &attrs);
 186
 187	return devlink_port_register(dl, dlp, port);
 188}
 189
 190void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
 191{
 192	struct devlink_port *dlp = &ocelot->devlink_ports[port];
 193
 194	devlink_port_unregister(dlp);
 195}
 196
 197int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
 198			       struct flow_cls_offload *f,
 199			       bool ingress)
 200{
 201	struct ocelot *ocelot = priv->port.ocelot;
 202	int port = priv->port.index;
 203
 204	if (!ingress)
 205		return -EOPNOTSUPP;
 206
 207	switch (f->command) {
 208	case FLOW_CLS_REPLACE:
 209		return ocelot_cls_flower_replace(ocelot, port, f, ingress);
 210	case FLOW_CLS_DESTROY:
 211		return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
 212	case FLOW_CLS_STATS:
 213		return ocelot_cls_flower_stats(ocelot, port, f, ingress);
 214	default:
 215		return -EOPNOTSUPP;
 216	}
 217}
 218
 219static int ocelot_setup_tc_cls_matchall_police(struct ocelot_port_private *priv,
 220					       struct tc_cls_matchall_offload *f,
 221					       bool ingress,
 222					       struct netlink_ext_ack *extack)
 223{
 224	struct flow_action_entry *action = &f->rule->action.entries[0];
 225	struct ocelot *ocelot = priv->port.ocelot;
 226	struct ocelot_policer pol = { 0 };
 227	int port = priv->port.index;
 228	int err;
 229
 230	if (!ingress) {
 231		NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
 232		return -EOPNOTSUPP;
 233	}
 234
 235	if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
 236		NL_SET_ERR_MSG_MOD(extack,
 237				   "Only one policer per port is supported");
 238		return -EEXIST;
 239	}
 240
 241	err = ocelot_policer_validate(&f->rule->action, action, extack);
 242	if (err)
 243		return err;
 244
 245	pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
 246	pol.burst = action->police.burst;
 247
 248	err = ocelot_port_policer_add(ocelot, port, &pol);
 249	if (err) {
 250		NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
 251		return err;
 252	}
 253
 254	priv->tc.police_id = f->cookie;
 255	priv->tc.offload_cnt++;
 256
 257	return 0;
 258}
 259
 260static int ocelot_setup_tc_cls_matchall_mirred(struct ocelot_port_private *priv,
 261					       struct tc_cls_matchall_offload *f,
 262					       bool ingress,
 263					       struct netlink_ext_ack *extack)
 264{
 265	struct flow_action *action = &f->rule->action;
 266	struct ocelot *ocelot = priv->port.ocelot;
 267	struct ocelot_port_private *other_priv;
 268	const struct flow_action_entry *a;
 269	int err;
 270
 271	if (f->common.protocol != htons(ETH_P_ALL))
 272		return -EOPNOTSUPP;
 273
 274	if (!flow_action_basic_hw_stats_check(action, extack))
 275		return -EOPNOTSUPP;
 276
 277	a = &action->entries[0];
 278	if (!a->dev)
 279		return -EINVAL;
 280
 281	if (!ocelot_netdevice_dev_check(a->dev)) {
 282		NL_SET_ERR_MSG_MOD(extack,
 283				   "Destination not an ocelot port");
 284		return -EOPNOTSUPP;
 285	}
 286
 287	other_priv = netdev_priv(a->dev);
 288
 289	err = ocelot_port_mirror_add(ocelot, priv->port.index,
 290				     other_priv->port.index, ingress, extack);
 291	if (err)
 292		return err;
 293
 294	if (ingress)
 295		priv->tc.ingress_mirred_id = f->cookie;
 296	else
 297		priv->tc.egress_mirred_id = f->cookie;
 298	priv->tc.offload_cnt++;
 299
 300	return 0;
 301}
 302
 303static int ocelot_del_tc_cls_matchall_police(struct ocelot_port_private *priv,
 304					     struct netlink_ext_ack *extack)
 305{
 306	struct ocelot *ocelot = priv->port.ocelot;
 307	int port = priv->port.index;
 308	int err;
 309
 310	err = ocelot_port_policer_del(ocelot, port);
 311	if (err) {
 312		NL_SET_ERR_MSG_MOD(extack,
 313				   "Could not delete policer");
 314		return err;
 315	}
 316
 317	priv->tc.police_id = 0;
 318	priv->tc.offload_cnt--;
 319
 320	return 0;
 321}
 322
 323static int ocelot_del_tc_cls_matchall_mirred(struct ocelot_port_private *priv,
 324					     bool ingress,
 325					     struct netlink_ext_ack *extack)
 326{
 327	struct ocelot *ocelot = priv->port.ocelot;
 328	int port = priv->port.index;
 329
 330	ocelot_port_mirror_del(ocelot, port, ingress);
 331
 332	if (ingress)
 333		priv->tc.ingress_mirred_id = 0;
 334	else
 335		priv->tc.egress_mirred_id = 0;
 336	priv->tc.offload_cnt--;
 337
 338	return 0;
 339}
 340
 341static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
 342					struct tc_cls_matchall_offload *f,
 343					bool ingress)
 344{
 345	struct netlink_ext_ack *extack = f->common.extack;
 346	struct flow_action_entry *action;
 347
 348	switch (f->command) {
 349	case TC_CLSMATCHALL_REPLACE:
 350		if (!flow_offload_has_one_action(&f->rule->action)) {
 351			NL_SET_ERR_MSG_MOD(extack,
 352					   "Only one action is supported");
 353			return -EOPNOTSUPP;
 354		}
 355
 356		if (priv->tc.block_shared) {
 357			NL_SET_ERR_MSG_MOD(extack,
 358					   "Matchall offloads not supported on shared blocks");
 359			return -EOPNOTSUPP;
 360		}
 361
 362		action = &f->rule->action.entries[0];
 363
 364		switch (action->id) {
 365		case FLOW_ACTION_POLICE:
 366			return ocelot_setup_tc_cls_matchall_police(priv, f,
 367								   ingress,
 368								   extack);
 369			break;
 370		case FLOW_ACTION_MIRRED:
 371			return ocelot_setup_tc_cls_matchall_mirred(priv, f,
 372								   ingress,
 373								   extack);
 374		default:
 375			NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
 376			return -EOPNOTSUPP;
 377		}
 378
 379		break;
 380	case TC_CLSMATCHALL_DESTROY:
 381		action = &f->rule->action.entries[0];
 382
 383		if (f->cookie == priv->tc.police_id)
 384			return ocelot_del_tc_cls_matchall_police(priv, extack);
 385		else if (f->cookie == priv->tc.ingress_mirred_id ||
 386			 f->cookie == priv->tc.egress_mirred_id)
 387			return ocelot_del_tc_cls_matchall_mirred(priv, ingress,
 388								 extack);
 389		else
 390			return -ENOENT;
 391
 392		break;
 393	case TC_CLSMATCHALL_STATS:
 394	default:
 395		return -EOPNOTSUPP;
 396	}
 397}
 398
 399static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
 400				    void *type_data,
 401				    void *cb_priv, bool ingress)
 402{
 403	struct ocelot_port_private *priv = cb_priv;
 404
 405	if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
 406		return -EOPNOTSUPP;
 407
 408	switch (type) {
 409	case TC_SETUP_CLSMATCHALL:
 410		return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
 411	case TC_SETUP_CLSFLOWER:
 412		return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
 413	default:
 414		return -EOPNOTSUPP;
 415	}
 416}
 417
 418static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
 419				       void *type_data,
 420				       void *cb_priv)
 421{
 422	return ocelot_setup_tc_block_cb(type, type_data,
 423					cb_priv, true);
 424}
 425
 426static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
 427				       void *type_data,
 428				       void *cb_priv)
 429{
 430	return ocelot_setup_tc_block_cb(type, type_data,
 431					cb_priv, false);
 432}
 433
 434static LIST_HEAD(ocelot_block_cb_list);
 435
 436static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
 437				 struct flow_block_offload *f)
 438{
 439	struct flow_block_cb *block_cb;
 440	flow_setup_cb_t *cb;
 441
 442	if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
 443		cb = ocelot_setup_tc_block_cb_ig;
 444		priv->tc.block_shared = f->block_shared;
 445	} else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
 446		cb = ocelot_setup_tc_block_cb_eg;
 447	} else {
 448		return -EOPNOTSUPP;
 449	}
 450
 451	f->driver_block_list = &ocelot_block_cb_list;
 452
 453	switch (f->command) {
 454	case FLOW_BLOCK_BIND:
 455		if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
 456			return -EBUSY;
 457
 458		block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
 459		if (IS_ERR(block_cb))
 460			return PTR_ERR(block_cb);
 461
 462		flow_block_cb_add(block_cb, f);
 463		list_add_tail(&block_cb->driver_list, f->driver_block_list);
 464		return 0;
 465	case FLOW_BLOCK_UNBIND:
 466		block_cb = flow_block_cb_lookup(f->block, cb, priv);
 467		if (!block_cb)
 468			return -ENOENT;
 469
 470		flow_block_cb_remove(block_cb, f);
 471		list_del(&block_cb->driver_list);
 472		return 0;
 473	default:
 474		return -EOPNOTSUPP;
 475	}
 476}
 477
 478static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
 479			   void *type_data)
 480{
 481	struct ocelot_port_private *priv = netdev_priv(dev);
 482
 483	switch (type) {
 484	case TC_SETUP_BLOCK:
 485		return ocelot_setup_tc_block(priv, type_data);
 486	default:
 487		return -EOPNOTSUPP;
 488	}
 489	return 0;
 490}
 491
 492static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
 493			       bool untagged)
 494{
 495	struct ocelot_port_private *priv = netdev_priv(dev);
 496	struct ocelot_port *ocelot_port = &priv->port;
 497	struct ocelot *ocelot = ocelot_port->ocelot;
 498	int port = priv->port.index;
 499	int ret;
 500
 501	ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
 502	if (ret)
 503		return ret;
 504
 505	/* Add the port MAC address to with the right VLAN information */
 506	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
 507			  ENTRYTYPE_LOCKED);
 508
 509	return 0;
 510}
 511
 512static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
 513{
 514	struct ocelot_port_private *priv = netdev_priv(dev);
 515	struct ocelot *ocelot = priv->port.ocelot;
 516	int port = priv->port.index;
 517	int ret;
 518
 519	/* 8021q removes VID 0 on module unload for all interfaces
 520	 * with VLAN filtering feature. We need to keep it to receive
 521	 * untagged traffic.
 522	 */
 523	if (vid == OCELOT_STANDALONE_PVID)
 524		return 0;
 525
 526	ret = ocelot_vlan_del(ocelot, port, vid);
 527	if (ret)
 528		return ret;
 529
 530	/* Del the port MAC address to with the right VLAN information */
 531	ocelot_mact_forget(ocelot, dev->dev_addr, vid);
 532
 533	return 0;
 534}
 535
 536static int ocelot_port_open(struct net_device *dev)
 537{
 538	struct ocelot_port_private *priv = netdev_priv(dev);
 539
 540	phylink_start(priv->phylink);
 541
 542	return 0;
 543}
 544
 545static int ocelot_port_stop(struct net_device *dev)
 546{
 547	struct ocelot_port_private *priv = netdev_priv(dev);
 548
 549	phylink_stop(priv->phylink);
 550
 551	return 0;
 552}
 553
 554static bool ocelot_xmit_timestamp(struct ocelot *ocelot, int port,
 555				  struct sk_buff *skb, u32 *rew_op)
 556{
 557	if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
 558		struct sk_buff *clone = NULL;
 559
 560		if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
 561			kfree_skb(skb);
 562			return false;
 563		}
 564
 565		if (clone)
 566			OCELOT_SKB_CB(skb)->clone = clone;
 567
 568		*rew_op = ocelot_ptp_rew_op(skb);
 569	}
 570
 571	return true;
 572}
 573
 574static netdev_tx_t ocelot_port_xmit_fdma(struct sk_buff *skb,
 575					 struct net_device *dev)
 576{
 577	struct ocelot_port_private *priv = netdev_priv(dev);
 578	struct ocelot_port *ocelot_port = &priv->port;
 579	struct ocelot *ocelot = ocelot_port->ocelot;
 580	int port = priv->port.index;
 581	u32 rew_op = 0;
 582
 583	if (!ocelot_xmit_timestamp(ocelot, port, skb, &rew_op))
 584		return NETDEV_TX_OK;
 585
 586	ocelot_fdma_inject_frame(ocelot, port, rew_op, skb, dev);
 587
 588	return NETDEV_TX_OK;
 589}
 590
 591static netdev_tx_t ocelot_port_xmit_inj(struct sk_buff *skb,
 592					struct net_device *dev)
 593{
 594	struct ocelot_port_private *priv = netdev_priv(dev);
 595	struct ocelot_port *ocelot_port = &priv->port;
 596	struct ocelot *ocelot = ocelot_port->ocelot;
 597	int port = priv->port.index;
 598	u32 rew_op = 0;
 599
 600	ocelot_lock_inj_grp(ocelot, 0);
 601
 602	if (!ocelot_can_inject(ocelot, 0)) {
 603		ocelot_unlock_inj_grp(ocelot, 0);
 604		return NETDEV_TX_BUSY;
 605	}
 606
 607	if (!ocelot_xmit_timestamp(ocelot, port, skb, &rew_op)) {
 608		ocelot_unlock_inj_grp(ocelot, 0);
 609		return NETDEV_TX_OK;
 610	}
 611
 612	ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
 613
 614	ocelot_unlock_inj_grp(ocelot, 0);
 615
 616	consume_skb(skb);
 617
 618	return NETDEV_TX_OK;
 619}
 620
 621static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
 622{
 623	if (static_branch_unlikely(&ocelot_fdma_enabled))
 624		return ocelot_port_xmit_fdma(skb, dev);
 625
 626	return ocelot_port_xmit_inj(skb, dev);
 627}
 628
 629enum ocelot_action_type {
 630	OCELOT_MACT_LEARN,
 631	OCELOT_MACT_FORGET,
 632};
 633
 634struct ocelot_mact_work_ctx {
 635	struct work_struct work;
 636	struct ocelot *ocelot;
 637	enum ocelot_action_type type;
 638	union {
 639		/* OCELOT_MACT_LEARN */
 640		struct {
 641			unsigned char addr[ETH_ALEN];
 642			u16 vid;
 643			enum macaccess_entry_type entry_type;
 644			int pgid;
 645		} learn;
 646		/* OCELOT_MACT_FORGET */
 647		struct {
 648			unsigned char addr[ETH_ALEN];
 649			u16 vid;
 650		} forget;
 651	};
 652};
 653
 654#define ocelot_work_to_ctx(x) \
 655	container_of((x), struct ocelot_mact_work_ctx, work)
 656
 657static void ocelot_mact_work(struct work_struct *work)
 658{
 659	struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
 660	struct ocelot *ocelot = w->ocelot;
 661
 662	switch (w->type) {
 663	case OCELOT_MACT_LEARN:
 664		ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
 665				  w->learn.vid, w->learn.entry_type);
 666		break;
 667	case OCELOT_MACT_FORGET:
 668		ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
 669		break;
 670	default:
 671		break;
 672	}
 673
 674	kfree(w);
 675}
 676
 677static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
 678				      const struct ocelot_mact_work_ctx *ctx)
 679{
 680	struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
 681
 682	if (!w)
 683		return -ENOMEM;
 684
 685	w->ocelot = ocelot;
 686	INIT_WORK(&w->work, ocelot_mact_work);
 687	queue_work(ocelot->owq, &w->work);
 688
 689	return 0;
 690}
 691
 692static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
 693{
 694	struct ocelot_port_private *priv = netdev_priv(dev);
 695	struct ocelot_port *ocelot_port = &priv->port;
 696	struct ocelot *ocelot = ocelot_port->ocelot;
 697	struct ocelot_mact_work_ctx w;
 698
 699	ether_addr_copy(w.forget.addr, addr);
 700	w.forget.vid = OCELOT_STANDALONE_PVID;
 701	w.type = OCELOT_MACT_FORGET;
 702
 703	return ocelot_enqueue_mact_action(ocelot, &w);
 704}
 705
 706static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
 707{
 708	struct ocelot_port_private *priv = netdev_priv(dev);
 709	struct ocelot_port *ocelot_port = &priv->port;
 710	struct ocelot *ocelot = ocelot_port->ocelot;
 711	struct ocelot_mact_work_ctx w;
 712
 713	ether_addr_copy(w.learn.addr, addr);
 714	w.learn.vid = OCELOT_STANDALONE_PVID;
 715	w.learn.pgid = PGID_CPU;
 716	w.learn.entry_type = ENTRYTYPE_LOCKED;
 717	w.type = OCELOT_MACT_LEARN;
 718
 719	return ocelot_enqueue_mact_action(ocelot, &w);
 720}
 721
 722static void ocelot_set_rx_mode(struct net_device *dev)
 723{
 724	struct ocelot_port_private *priv = netdev_priv(dev);
 725	struct ocelot *ocelot = priv->port.ocelot;
 726	u32 val;
 727	int i;
 728
 729	/* This doesn't handle promiscuous mode because the bridge core is
 730	 * setting IFF_PROMISC on all slave interfaces and all frames would be
 731	 * forwarded to the CPU port.
 732	 */
 733	val = GENMASK(ocelot->num_phys_ports - 1, 0);
 734	for_each_nonreserved_multicast_dest_pgid(ocelot, i)
 735		ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
 736
 737	__dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
 738}
 739
 740static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
 741{
 742	struct ocelot_port_private *priv = netdev_priv(dev);
 743	struct ocelot_port *ocelot_port = &priv->port;
 744	struct ocelot *ocelot = ocelot_port->ocelot;
 745	const struct sockaddr *addr = p;
 746
 747	/* Learn the new net device MAC address in the mac table. */
 748	ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
 749			  OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
 750	/* Then forget the previous one. */
 751	ocelot_mact_forget(ocelot, dev->dev_addr, OCELOT_STANDALONE_PVID);
 752
 753	eth_hw_addr_set(dev, addr->sa_data);
 754	return 0;
 755}
 756
 757static void ocelot_get_stats64(struct net_device *dev,
 758			       struct rtnl_link_stats64 *stats)
 759{
 760	struct ocelot_port_private *priv = netdev_priv(dev);
 761	struct ocelot *ocelot = priv->port.ocelot;
 762	int port = priv->port.index;
 763
 764	return ocelot_port_get_stats64(ocelot, port, stats);
 765}
 766
 767static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
 768			       struct net_device *dev,
 769			       const unsigned char *addr,
 770			       u16 vid, u16 flags, bool *notified,
 771			       struct netlink_ext_ack *extack)
 772{
 773	struct ocelot_port_private *priv = netdev_priv(dev);
 774	struct ocelot_port *ocelot_port = &priv->port;
 775	struct ocelot *ocelot = ocelot_port->ocelot;
 776	int port = priv->port.index;
 777
 778	return ocelot_fdb_add(ocelot, port, addr, vid, ocelot_port->bridge);
 779}
 780
 781static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
 782			       struct net_device *dev,
 783			       const unsigned char *addr, u16 vid,
 784			       bool *notified, struct netlink_ext_ack *extack)
 785{
 786	struct ocelot_port_private *priv = netdev_priv(dev);
 787	struct ocelot_port *ocelot_port = &priv->port;
 788	struct ocelot *ocelot = ocelot_port->ocelot;
 789	int port = priv->port.index;
 790
 791	return ocelot_fdb_del(ocelot, port, addr, vid, ocelot_port->bridge);
 792}
 793
 794static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
 795				   bool is_static, void *data)
 796{
 797	struct ocelot_dump_ctx *dump = data;
 798	struct ndo_fdb_dump_context *ctx = (void *)dump->cb->ctx;
 799	u32 portid = NETLINK_CB(dump->cb->skb).portid;
 800	u32 seq = dump->cb->nlh->nlmsg_seq;
 801	struct nlmsghdr *nlh;
 802	struct ndmsg *ndm;
 803
 804	if (dump->idx < ctx->fdb_idx)
 805		goto skip;
 806
 807	nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
 808			sizeof(*ndm), NLM_F_MULTI);
 809	if (!nlh)
 810		return -EMSGSIZE;
 811
 812	ndm = nlmsg_data(nlh);
 813	ndm->ndm_family  = AF_BRIDGE;
 814	ndm->ndm_pad1    = 0;
 815	ndm->ndm_pad2    = 0;
 816	ndm->ndm_flags   = NTF_SELF;
 817	ndm->ndm_type    = 0;
 818	ndm->ndm_ifindex = dump->dev->ifindex;
 819	ndm->ndm_state   = is_static ? NUD_NOARP : NUD_REACHABLE;
 820
 821	if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
 822		goto nla_put_failure;
 823
 824	if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
 825		goto nla_put_failure;
 826
 827	nlmsg_end(dump->skb, nlh);
 828
 829skip:
 830	dump->idx++;
 831	return 0;
 832
 833nla_put_failure:
 834	nlmsg_cancel(dump->skb, nlh);
 835	return -EMSGSIZE;
 836}
 837
 838static int ocelot_port_fdb_dump(struct sk_buff *skb,
 839				struct netlink_callback *cb,
 840				struct net_device *dev,
 841				struct net_device *filter_dev, int *idx)
 842{
 843	struct ocelot_port_private *priv = netdev_priv(dev);
 844	struct ocelot *ocelot = priv->port.ocelot;
 845	struct ocelot_dump_ctx dump = {
 846		.dev = dev,
 847		.skb = skb,
 848		.cb = cb,
 849		.idx = *idx,
 850	};
 851	int port = priv->port.index;
 852	int ret;
 853
 854	ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
 855
 856	*idx = dump.idx;
 857
 858	return ret;
 859}
 860
 861static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
 862				  u16 vid)
 863{
 864	return ocelot_vlan_vid_add(dev, vid, false, false);
 865}
 866
 867static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
 868				   u16 vid)
 869{
 870	return ocelot_vlan_vid_del(dev, vid);
 871}
 872
 873static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
 874			     netdev_features_t features)
 875{
 876	u32 val;
 877
 878	/* Filtering */
 879	val = ocelot_read(ocelot, ANA_VLANMASK);
 880	if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
 881		val |= BIT(port);
 882	else
 883		val &= ~BIT(port);
 884	ocelot_write(ocelot, val, ANA_VLANMASK);
 885}
 886
 887static int ocelot_set_features(struct net_device *dev,
 888			       netdev_features_t features)
 889{
 890	netdev_features_t changed = dev->features ^ features;
 891	struct ocelot_port_private *priv = netdev_priv(dev);
 892	struct ocelot *ocelot = priv->port.ocelot;
 893	int port = priv->port.index;
 894
 895	if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
 896	    priv->tc.offload_cnt) {
 897		netdev_err(dev,
 898			   "Cannot disable HW TC offload while offloads active\n");
 899		return -EBUSY;
 900	}
 901
 902	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
 903		ocelot_vlan_mode(ocelot, port, features);
 904
 905	return 0;
 906}
 907
 908static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 909{
 910	return phy_mii_ioctl(dev->phydev, ifr, cmd);
 911}
 912
 913static int ocelot_port_hwtstamp_get(struct net_device *dev,
 914				    struct kernel_hwtstamp_config *cfg)
 915{
 916	struct ocelot_port_private *priv = netdev_priv(dev);
 917	struct ocelot *ocelot = priv->port.ocelot;
 918	int port = priv->port.index;
 919
 920	ocelot_hwstamp_get(ocelot, port, cfg);
 921
 922	return 0;
 923}
 924
 925static int ocelot_port_hwtstamp_set(struct net_device *dev,
 926				    struct kernel_hwtstamp_config *cfg,
 927				    struct netlink_ext_ack *extack)
 928{
 929	struct ocelot_port_private *priv = netdev_priv(dev);
 930	struct ocelot *ocelot = priv->port.ocelot;
 931	int port = priv->port.index;
 932
 933	return ocelot_hwstamp_set(ocelot, port, cfg, extack);
 934}
 935
 936static int ocelot_change_mtu(struct net_device *dev, int new_mtu)
 937{
 938	struct ocelot_port_private *priv = netdev_priv(dev);
 939	struct ocelot_port *ocelot_port = &priv->port;
 940	struct ocelot *ocelot = ocelot_port->ocelot;
 941
 942	ocelot_port_set_maxlen(ocelot, priv->port.index, new_mtu);
 943	WRITE_ONCE(dev->mtu, new_mtu);
 944
 945	return 0;
 946}
 947
 948static const struct net_device_ops ocelot_port_netdev_ops = {
 949	.ndo_open			= ocelot_port_open,
 950	.ndo_stop			= ocelot_port_stop,
 951	.ndo_start_xmit			= ocelot_port_xmit,
 952	.ndo_change_mtu			= ocelot_change_mtu,
 953	.ndo_set_rx_mode		= ocelot_set_rx_mode,
 954	.ndo_set_mac_address		= ocelot_port_set_mac_address,
 955	.ndo_get_stats64		= ocelot_get_stats64,
 956	.ndo_fdb_add			= ocelot_port_fdb_add,
 957	.ndo_fdb_del			= ocelot_port_fdb_del,
 958	.ndo_fdb_dump			= ocelot_port_fdb_dump,
 959	.ndo_vlan_rx_add_vid		= ocelot_vlan_rx_add_vid,
 960	.ndo_vlan_rx_kill_vid		= ocelot_vlan_rx_kill_vid,
 961	.ndo_set_features		= ocelot_set_features,
 962	.ndo_setup_tc			= ocelot_setup_tc,
 963	.ndo_eth_ioctl			= ocelot_ioctl,
 964	.ndo_hwtstamp_get		= ocelot_port_hwtstamp_get,
 965	.ndo_hwtstamp_set		= ocelot_port_hwtstamp_set,
 966};
 967
 968struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
 969{
 970	struct ocelot_port *ocelot_port = ocelot->ports[port];
 971	struct ocelot_port_private *priv;
 972
 973	if (!ocelot_port)
 974		return NULL;
 975
 976	priv = container_of(ocelot_port, struct ocelot_port_private, port);
 977
 978	return priv->dev;
 979}
 980
 981/* Checks if the net_device instance given to us originates from our driver */
 982static bool ocelot_netdevice_dev_check(const struct net_device *dev)
 983{
 984	return dev->netdev_ops == &ocelot_port_netdev_ops;
 985}
 986
 987int ocelot_netdev_to_port(struct net_device *dev)
 988{
 989	struct ocelot_port_private *priv;
 990
 991	if (!dev || !ocelot_netdevice_dev_check(dev))
 992		return -EINVAL;
 993
 994	priv = netdev_priv(dev);
 995
 996	return priv->port.index;
 997}
 998
 999static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
1000				    u8 *data)
1001{
1002	struct ocelot_port_private *priv = netdev_priv(netdev);
1003	struct ocelot *ocelot = priv->port.ocelot;
1004	int port = priv->port.index;
1005
1006	ocelot_get_strings(ocelot, port, sset, data);
1007}
1008
1009static void ocelot_port_get_ethtool_stats(struct net_device *dev,
1010					  struct ethtool_stats *stats,
1011					  u64 *data)
1012{
1013	struct ocelot_port_private *priv = netdev_priv(dev);
1014	struct ocelot *ocelot = priv->port.ocelot;
1015	int port = priv->port.index;
1016
1017	ocelot_get_ethtool_stats(ocelot, port, data);
1018}
1019
1020static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
1021{
1022	struct ocelot_port_private *priv = netdev_priv(dev);
1023	struct ocelot *ocelot = priv->port.ocelot;
1024	int port = priv->port.index;
1025
1026	return ocelot_get_sset_count(ocelot, port, sset);
1027}
1028
1029static int ocelot_port_get_ts_info(struct net_device *dev,
1030				   struct kernel_ethtool_ts_info *info)
1031{
1032	struct ocelot_port_private *priv = netdev_priv(dev);
1033	struct ocelot *ocelot = priv->port.ocelot;
1034	int port = priv->port.index;
1035
1036	if (!ocelot->ptp)
1037		return ethtool_op_get_ts_info(dev, info);
1038
1039	return ocelot_get_ts_info(ocelot, port, info);
1040}
1041
1042static void ocelot_port_ts_stats(struct net_device *dev,
1043				 struct ethtool_ts_stats *ts_stats)
1044{
1045	struct ocelot_port_private *priv = netdev_priv(dev);
1046	struct ocelot *ocelot = priv->port.ocelot;
1047	int port = priv->port.index;
1048
1049	ocelot_port_get_ts_stats(ocelot, port, ts_stats);
1050}
1051
1052static const struct ethtool_ops ocelot_ethtool_ops = {
1053	.get_strings		= ocelot_port_get_strings,
1054	.get_ethtool_stats	= ocelot_port_get_ethtool_stats,
1055	.get_sset_count		= ocelot_port_get_sset_count,
1056	.get_link_ksettings	= phy_ethtool_get_link_ksettings,
1057	.set_link_ksettings	= phy_ethtool_set_link_ksettings,
1058	.get_ts_info		= ocelot_port_get_ts_info,
1059	.get_ts_stats		= ocelot_port_ts_stats,
1060};
1061
1062static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
1063					   u8 state)
1064{
1065	ocelot_bridge_stp_state_set(ocelot, port, state);
1066}
1067
1068static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
1069					unsigned long ageing_clock_t)
1070{
1071	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1072	u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
1073
1074	ocelot_set_ageing_time(ocelot, ageing_time);
1075}
1076
1077static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
1078{
1079	u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1080			    ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1081			    ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1082	u32 val = 0;
1083
1084	if (mc)
1085		val = cpu_fwd_mcast;
1086
1087	ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
1088		       ANA_PORT_CPU_FWD_CFG, port);
1089}
1090
1091static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
1092				const struct switchdev_attr *attr,
1093				struct netlink_ext_ack *extack)
1094{
1095	struct ocelot_port_private *priv = netdev_priv(dev);
1096	struct ocelot *ocelot = priv->port.ocelot;
1097	int port = priv->port.index;
1098	int err = 0;
1099
1100	if (ctx && ctx != priv)
1101		return 0;
1102
1103	switch (attr->id) {
1104	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1105		ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
1106		break;
1107	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1108		ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
1109		break;
1110	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1111		ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering,
1112					   extack);
1113		break;
1114	case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1115		ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
1116		break;
1117	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
1118		err = ocelot_port_pre_bridge_flags(ocelot, port,
1119						   attr->u.brport_flags);
1120		break;
1121	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
1122		ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
1123		break;
1124	default:
1125		err = -EOPNOTSUPP;
1126		break;
1127	}
1128
1129	return err;
1130}
1131
1132static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
1133				   bool untagged, struct netlink_ext_ack *extack)
1134{
1135	struct ocelot_port_private *priv = netdev_priv(dev);
1136	struct ocelot_port *ocelot_port = &priv->port;
1137	struct ocelot *ocelot = ocelot_port->ocelot;
1138	int port = priv->port.index;
1139
1140	return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged, extack);
1141}
1142
1143static int ocelot_port_obj_add_vlan(struct net_device *dev,
1144				    const struct switchdev_obj_port_vlan *vlan,
1145				    struct netlink_ext_ack *extack)
1146{
1147	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1148	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1149	int ret;
1150
1151	ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged, extack);
1152	if (ret)
1153		return ret;
1154
1155	return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
1156}
1157
1158static int ocelot_port_obj_add_mdb(struct net_device *dev,
1159				   const struct switchdev_obj_port_mdb *mdb)
1160{
1161	struct ocelot_port_private *priv = netdev_priv(dev);
1162	struct ocelot_port *ocelot_port = &priv->port;
1163	struct ocelot *ocelot = ocelot_port->ocelot;
1164	int port = priv->port.index;
1165
1166	return ocelot_port_mdb_add(ocelot, port, mdb, ocelot_port->bridge);
1167}
1168
1169static int ocelot_port_obj_del_mdb(struct net_device *dev,
1170				   const struct switchdev_obj_port_mdb *mdb)
1171{
1172	struct ocelot_port_private *priv = netdev_priv(dev);
1173	struct ocelot_port *ocelot_port = &priv->port;
1174	struct ocelot *ocelot = ocelot_port->ocelot;
1175	int port = priv->port.index;
1176
1177	return ocelot_port_mdb_del(ocelot, port, mdb, ocelot_port->bridge);
1178}
1179
1180static int ocelot_port_obj_mrp_add(struct net_device *dev,
1181				   const struct switchdev_obj_mrp *mrp)
1182{
1183	struct ocelot_port_private *priv = netdev_priv(dev);
1184	struct ocelot_port *ocelot_port = &priv->port;
1185	struct ocelot *ocelot = ocelot_port->ocelot;
1186	int port = priv->port.index;
1187
1188	return ocelot_mrp_add(ocelot, port, mrp);
1189}
1190
1191static int ocelot_port_obj_mrp_del(struct net_device *dev,
1192				   const struct switchdev_obj_mrp *mrp)
1193{
1194	struct ocelot_port_private *priv = netdev_priv(dev);
1195	struct ocelot_port *ocelot_port = &priv->port;
1196	struct ocelot *ocelot = ocelot_port->ocelot;
1197	int port = priv->port.index;
1198
1199	return ocelot_mrp_del(ocelot, port, mrp);
1200}
1201
1202static int
1203ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1204				  const struct switchdev_obj_ring_role_mrp *mrp)
1205{
1206	struct ocelot_port_private *priv = netdev_priv(dev);
1207	struct ocelot_port *ocelot_port = &priv->port;
1208	struct ocelot *ocelot = ocelot_port->ocelot;
1209	int port = priv->port.index;
1210
1211	return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1212}
1213
1214static int
1215ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1216				  const struct switchdev_obj_ring_role_mrp *mrp)
1217{
1218	struct ocelot_port_private *priv = netdev_priv(dev);
1219	struct ocelot_port *ocelot_port = &priv->port;
1220	struct ocelot *ocelot = ocelot_port->ocelot;
1221	int port = priv->port.index;
1222
1223	return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1224}
1225
1226static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
1227			       const struct switchdev_obj *obj,
1228			       struct netlink_ext_ack *extack)
1229{
1230	struct ocelot_port_private *priv = netdev_priv(dev);
1231	int ret = 0;
1232
1233	if (ctx && ctx != priv)
1234		return 0;
1235
1236	switch (obj->id) {
1237	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1238		ret = ocelot_port_obj_add_vlan(dev,
1239					       SWITCHDEV_OBJ_PORT_VLAN(obj),
1240					       extack);
1241		break;
1242	case SWITCHDEV_OBJ_ID_PORT_MDB:
1243		ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1244		break;
1245	case SWITCHDEV_OBJ_ID_MRP:
1246		ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1247		break;
1248	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1249		ret = ocelot_port_obj_mrp_add_ring_role(dev,
1250							SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1251		break;
1252	default:
1253		return -EOPNOTSUPP;
1254	}
1255
1256	return ret;
1257}
1258
1259static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
1260			       const struct switchdev_obj *obj)
1261{
1262	struct ocelot_port_private *priv = netdev_priv(dev);
1263	int ret = 0;
1264
1265	if (ctx && ctx != priv)
1266		return 0;
1267
1268	switch (obj->id) {
1269	case SWITCHDEV_OBJ_ID_PORT_VLAN:
1270		ret = ocelot_vlan_vid_del(dev,
1271					  SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
1272		break;
1273	case SWITCHDEV_OBJ_ID_PORT_MDB:
1274		ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1275		break;
1276	case SWITCHDEV_OBJ_ID_MRP:
1277		ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1278		break;
1279	case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1280		ret = ocelot_port_obj_mrp_del_ring_role(dev,
1281							SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1282		break;
1283	default:
1284		return -EOPNOTSUPP;
1285	}
1286
1287	return ret;
1288}
1289
1290static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1291					struct net_device *brport_dev)
1292{
1293	struct switchdev_brport_flags flags = {0};
1294	int flag;
1295
1296	flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1297
1298	for_each_set_bit(flag, &flags.mask, 32)
1299		if (br_port_flag_is_set(brport_dev, BIT(flag)))
1300			flags.val |= BIT(flag);
1301
1302	ocelot_port_bridge_flags(ocelot, port, flags);
1303}
1304
1305static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1306{
1307	struct switchdev_brport_flags flags;
1308
1309	flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1310	flags.val = flags.mask & ~BR_LEARNING;
1311
1312	ocelot_port_bridge_flags(ocelot, port, flags);
1313}
1314
1315static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1316				 struct net_device *brport_dev,
1317				 struct net_device *bridge_dev,
1318				 struct netlink_ext_ack *extack)
1319{
1320	clock_t ageing_time;
1321	u8 stp_state;
1322
1323	ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1324
1325	stp_state = br_port_get_stp_state(brport_dev);
1326	ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1327
1328	ageing_time = br_get_ageing_time(bridge_dev);
1329	ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1330
1331	return ocelot_port_vlan_filtering(ocelot, port,
1332					  br_vlan_enabled(bridge_dev),
1333					  extack);
1334}
1335
1336static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1337{
1338	int err;
1339
1340	err = ocelot_port_vlan_filtering(ocelot, port, false, NULL);
1341	if (err)
1342		return err;
1343
1344	ocelot_clear_brport_flags(ocelot, port);
1345
1346	ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1347
1348	return 0;
1349}
1350
1351static int ocelot_bridge_num_get(struct ocelot *ocelot,
1352				 const struct net_device *bridge_dev)
1353{
1354	int bridge_num = ocelot_bridge_num_find(ocelot, bridge_dev);
1355
1356	if (bridge_num < 0) {
1357		/* First port that offloads this bridge */
1358		bridge_num = find_first_zero_bit(&ocelot->bridges,
1359						 ocelot->num_phys_ports);
1360
1361		set_bit(bridge_num, &ocelot->bridges);
1362	}
1363
1364	return bridge_num;
1365}
1366
1367static void ocelot_bridge_num_put(struct ocelot *ocelot,
1368				  const struct net_device *bridge_dev,
1369				  int bridge_num)
1370{
1371	/* Check if the bridge is still in use, otherwise it is time
1372	 * to clean it up so we can reuse this bridge_num later.
1373	 */
1374	if (!ocelot_bridge_num_find(ocelot, bridge_dev))
1375		clear_bit(bridge_num, &ocelot->bridges);
1376}
1377
1378static int ocelot_netdevice_bridge_join(struct net_device *dev,
1379					struct net_device *brport_dev,
1380					struct net_device *bridge,
1381					struct netlink_ext_ack *extack)
1382{
1383	struct ocelot_port_private *priv = netdev_priv(dev);
1384	struct ocelot_port *ocelot_port = &priv->port;
1385	struct ocelot *ocelot = ocelot_port->ocelot;
1386	int port = priv->port.index;
1387	int bridge_num, err;
1388
1389	bridge_num = ocelot_bridge_num_get(ocelot, bridge);
1390
1391	err = ocelot_port_bridge_join(ocelot, port, bridge, bridge_num,
1392				      extack);
1393	if (err)
1394		goto err_join;
1395
1396	err = switchdev_bridge_port_offload(brport_dev, dev, priv,
1397					    &ocelot_switchdev_nb,
1398					    &ocelot_switchdev_blocking_nb,
1399					    false, extack);
1400	if (err)
1401		goto err_switchdev_offload;
1402
1403	err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
1404	if (err)
1405		goto err_switchdev_sync;
1406
1407	return 0;
1408
1409err_switchdev_sync:
1410	switchdev_bridge_port_unoffload(brport_dev, priv,
1411					&ocelot_switchdev_nb,
1412					&ocelot_switchdev_blocking_nb);
1413err_switchdev_offload:
1414	ocelot_port_bridge_leave(ocelot, port, bridge);
1415err_join:
1416	ocelot_bridge_num_put(ocelot, bridge, bridge_num);
1417	return err;
1418}
1419
1420static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev,
1421					      struct net_device *brport_dev)
1422{
1423	struct ocelot_port_private *priv = netdev_priv(dev);
1424
1425	switchdev_bridge_port_unoffload(brport_dev, priv,
1426					&ocelot_switchdev_nb,
1427					&ocelot_switchdev_blocking_nb);
1428}
1429
1430static int ocelot_netdevice_bridge_leave(struct net_device *dev,
1431					 struct net_device *brport_dev,
1432					 struct net_device *bridge)
1433{
1434	struct ocelot_port_private *priv = netdev_priv(dev);
1435	struct ocelot_port *ocelot_port = &priv->port;
1436	struct ocelot *ocelot = ocelot_port->ocelot;
1437	int bridge_num = ocelot_port->bridge_num;
1438	int port = priv->port.index;
1439	int err;
1440
1441	err = ocelot_switchdev_unsync(ocelot, port);
1442	if (err)
1443		return err;
1444
1445	ocelot_port_bridge_leave(ocelot, port, bridge);
1446	ocelot_bridge_num_put(ocelot, bridge, bridge_num);
1447
1448	return 0;
1449}
1450
1451static int ocelot_netdevice_lag_join(struct net_device *dev,
1452				     struct net_device *bond,
1453				     struct netdev_lag_upper_info *info,
1454				     struct netlink_ext_ack *extack)
1455{
1456	struct ocelot_port_private *priv = netdev_priv(dev);
1457	struct ocelot_port *ocelot_port = &priv->port;
1458	struct ocelot *ocelot = ocelot_port->ocelot;
1459	struct net_device *bridge_dev;
1460	int port = priv->port.index;
1461	int err;
1462
1463	err = ocelot_port_lag_join(ocelot, port, bond, info, extack);
1464	if (err == -EOPNOTSUPP)
1465		/* Offloading not supported, fall back to software LAG */
1466		return 0;
1467
1468	bridge_dev = netdev_master_upper_dev_get(bond);
1469	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1470		return 0;
1471
1472	err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
1473	if (err)
1474		goto err_bridge_join;
1475
1476	return 0;
1477
1478err_bridge_join:
1479	ocelot_port_lag_leave(ocelot, port, bond);
1480	return err;
1481}
1482
1483static void ocelot_netdevice_pre_lag_leave(struct net_device *dev,
1484					   struct net_device *bond)
1485{
1486	struct net_device *bridge_dev;
1487
1488	bridge_dev = netdev_master_upper_dev_get(bond);
1489	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1490		return;
1491
1492	ocelot_netdevice_pre_bridge_leave(dev, bond);
1493}
1494
1495static int ocelot_netdevice_lag_leave(struct net_device *dev,
1496				      struct net_device *bond)
1497{
1498	struct ocelot_port_private *priv = netdev_priv(dev);
1499	struct ocelot_port *ocelot_port = &priv->port;
1500	struct ocelot *ocelot = ocelot_port->ocelot;
1501	struct net_device *bridge_dev;
1502	int port = priv->port.index;
1503
1504	ocelot_port_lag_leave(ocelot, port, bond);
1505
1506	bridge_dev = netdev_master_upper_dev_get(bond);
1507	if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1508		return 0;
1509
1510	return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
1511}
1512
1513static int ocelot_netdevice_changeupper(struct net_device *dev,
1514					struct net_device *brport_dev,
1515					struct netdev_notifier_changeupper_info *info)
1516{
1517	struct netlink_ext_ack *extack;
1518	int err = 0;
1519
1520	extack = netdev_notifier_info_to_extack(&info->info);
1521
1522	if (netif_is_bridge_master(info->upper_dev)) {
1523		if (info->linking)
1524			err = ocelot_netdevice_bridge_join(dev, brport_dev,
1525							   info->upper_dev,
1526							   extack);
1527		else
1528			err = ocelot_netdevice_bridge_leave(dev, brport_dev,
1529							    info->upper_dev);
1530	}
1531	if (netif_is_lag_master(info->upper_dev)) {
1532		if (info->linking)
1533			err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1534							info->upper_info, extack);
1535		else
1536			ocelot_netdevice_lag_leave(dev, info->upper_dev);
1537	}
1538
1539	return notifier_from_errno(err);
1540}
1541
1542/* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1543 * events for the lower physical ports of the LAG.
1544 * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1545 * In case the LAG joined a bridge, notify that we are offloading it and can do
1546 * forwarding in hardware towards it.
1547 */
1548static int
1549ocelot_netdevice_lag_changeupper(struct net_device *dev,
1550				 struct netdev_notifier_changeupper_info *info)
1551{
1552	struct net_device *lower;
1553	struct list_head *iter;
1554	int err = NOTIFY_DONE;
1555
1556	netdev_for_each_lower_dev(dev, lower, iter) {
1557		struct ocelot_port_private *priv = netdev_priv(lower);
1558		struct ocelot_port *ocelot_port = &priv->port;
1559
1560		if (ocelot_port->bond != dev)
1561			return NOTIFY_OK;
1562
1563		err = ocelot_netdevice_changeupper(lower, dev, info);
1564		if (err)
1565			return notifier_from_errno(err);
1566	}
1567
1568	return NOTIFY_DONE;
1569}
1570
1571static int
1572ocelot_netdevice_prechangeupper(struct net_device *dev,
1573				struct net_device *brport_dev,
1574				struct netdev_notifier_changeupper_info *info)
1575{
1576	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
1577		ocelot_netdevice_pre_bridge_leave(dev, brport_dev);
1578
1579	if (netif_is_lag_master(info->upper_dev) && !info->linking)
1580		ocelot_netdevice_pre_lag_leave(dev, info->upper_dev);
1581
1582	return NOTIFY_DONE;
1583}
1584
1585static int
1586ocelot_netdevice_lag_prechangeupper(struct net_device *dev,
1587				    struct netdev_notifier_changeupper_info *info)
1588{
1589	struct net_device *lower;
1590	struct list_head *iter;
1591	int err = NOTIFY_DONE;
1592
1593	netdev_for_each_lower_dev(dev, lower, iter) {
1594		struct ocelot_port_private *priv = netdev_priv(lower);
1595		struct ocelot_port *ocelot_port = &priv->port;
1596
1597		if (ocelot_port->bond != dev)
1598			return NOTIFY_OK;
1599
1600		err = ocelot_netdevice_prechangeupper(dev, lower, info);
1601		if (err)
1602			return err;
1603	}
1604
1605	return NOTIFY_DONE;
1606}
1607
1608static int
1609ocelot_netdevice_changelowerstate(struct net_device *dev,
1610				  struct netdev_lag_lower_state_info *info)
1611{
1612	struct ocelot_port_private *priv = netdev_priv(dev);
1613	bool is_active = info->link_up && info->tx_enabled;
1614	struct ocelot_port *ocelot_port = &priv->port;
1615	struct ocelot *ocelot = ocelot_port->ocelot;
1616	int port = priv->port.index;
1617
1618	if (!ocelot_port->bond)
1619		return NOTIFY_DONE;
1620
1621	if (ocelot_port->lag_tx_active == is_active)
1622		return NOTIFY_DONE;
1623
1624	ocelot_port_lag_change(ocelot, port, is_active);
1625
1626	return NOTIFY_OK;
1627}
1628
1629static int ocelot_netdevice_event(struct notifier_block *unused,
1630				  unsigned long event, void *ptr)
1631{
1632	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1633
1634	switch (event) {
1635	case NETDEV_PRECHANGEUPPER: {
1636		struct netdev_notifier_changeupper_info *info = ptr;
1637
1638		if (ocelot_netdevice_dev_check(dev))
1639			return ocelot_netdevice_prechangeupper(dev, dev, info);
1640
1641		if (netif_is_lag_master(dev))
1642			return ocelot_netdevice_lag_prechangeupper(dev, info);
1643
1644		break;
1645	}
1646	case NETDEV_CHANGEUPPER: {
1647		struct netdev_notifier_changeupper_info *info = ptr;
1648
1649		if (ocelot_netdevice_dev_check(dev))
1650			return ocelot_netdevice_changeupper(dev, dev, info);
1651
1652		if (netif_is_lag_master(dev))
1653			return ocelot_netdevice_lag_changeupper(dev, info);
1654
1655		break;
1656	}
1657	case NETDEV_CHANGELOWERSTATE: {
1658		struct netdev_notifier_changelowerstate_info *info = ptr;
1659
1660		if (!ocelot_netdevice_dev_check(dev))
1661			break;
1662
1663		return ocelot_netdevice_changelowerstate(dev,
1664							 info->lower_state_info);
1665	}
1666	default:
1667		break;
1668	}
1669
1670	return NOTIFY_DONE;
1671}
1672
1673struct notifier_block ocelot_netdevice_nb __read_mostly = {
1674	.notifier_call = ocelot_netdevice_event,
1675};
1676
1677static int ocelot_switchdev_event(struct notifier_block *unused,
1678				  unsigned long event, void *ptr)
1679{
1680	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1681	int err;
1682
1683	switch (event) {
1684	case SWITCHDEV_PORT_ATTR_SET:
1685		err = switchdev_handle_port_attr_set(dev, ptr,
1686						     ocelot_netdevice_dev_check,
1687						     ocelot_port_attr_set);
1688		return notifier_from_errno(err);
1689	}
1690
1691	return NOTIFY_DONE;
1692}
1693
1694struct notifier_block ocelot_switchdev_nb __read_mostly = {
1695	.notifier_call = ocelot_switchdev_event,
1696};
1697
1698static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1699					   unsigned long event, void *ptr)
1700{
1701	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1702	int err;
1703
1704	switch (event) {
1705		/* Blocking events. */
1706	case SWITCHDEV_PORT_OBJ_ADD:
1707		err = switchdev_handle_port_obj_add(dev, ptr,
1708						    ocelot_netdevice_dev_check,
1709						    ocelot_port_obj_add);
1710		return notifier_from_errno(err);
1711	case SWITCHDEV_PORT_OBJ_DEL:
1712		err = switchdev_handle_port_obj_del(dev, ptr,
1713						    ocelot_netdevice_dev_check,
1714						    ocelot_port_obj_del);
1715		return notifier_from_errno(err);
1716	case SWITCHDEV_PORT_ATTR_SET:
1717		err = switchdev_handle_port_attr_set(dev, ptr,
1718						     ocelot_netdevice_dev_check,
1719						     ocelot_port_attr_set);
1720		return notifier_from_errno(err);
1721	}
1722
1723	return NOTIFY_DONE;
1724}
1725
1726struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1727	.notifier_call = ocelot_switchdev_blocking_event,
1728};
1729
1730static void vsc7514_phylink_mac_config(struct phylink_config *config,
1731				       unsigned int link_an_mode,
1732				       const struct phylink_link_state *state)
1733{
1734	struct net_device *ndev = to_net_dev(config->dev);
1735	struct ocelot_port_private *priv = netdev_priv(ndev);
1736	struct ocelot *ocelot = priv->port.ocelot;
1737	int port = priv->port.index;
1738
1739	ocelot_phylink_mac_config(ocelot, port, link_an_mode, state);
1740}
1741
1742static void vsc7514_phylink_mac_link_down(struct phylink_config *config,
1743					  unsigned int link_an_mode,
1744					  phy_interface_t interface)
1745{
1746	struct net_device *ndev = to_net_dev(config->dev);
1747	struct ocelot_port_private *priv = netdev_priv(ndev);
1748	struct ocelot *ocelot = priv->port.ocelot;
1749	int port = priv->port.index;
1750
1751	ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1752				     OCELOT_MAC_QUIRKS);
1753}
1754
1755static void vsc7514_phylink_mac_link_up(struct phylink_config *config,
1756					struct phy_device *phydev,
1757					unsigned int link_an_mode,
1758					phy_interface_t interface,
1759					int speed, int duplex,
1760					bool tx_pause, bool rx_pause)
1761{
1762	struct net_device *ndev = to_net_dev(config->dev);
1763	struct ocelot_port_private *priv = netdev_priv(ndev);
1764	struct ocelot *ocelot = priv->port.ocelot;
1765	int port = priv->port.index;
1766
1767	ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1768				   interface, speed, duplex,
1769				   tx_pause, rx_pause, OCELOT_MAC_QUIRKS);
1770}
1771
1772static const struct phylink_mac_ops ocelot_phylink_ops = {
1773	.mac_config		= vsc7514_phylink_mac_config,
1774	.mac_link_down		= vsc7514_phylink_mac_link_down,
1775	.mac_link_up		= vsc7514_phylink_mac_link_up,
1776};
1777
1778static int ocelot_port_phylink_create(struct ocelot *ocelot, int port,
1779				      struct device_node *portnp)
1780{
1781	struct ocelot_port *ocelot_port = ocelot->ports[port];
1782	struct ocelot_port_private *priv;
1783	struct device *dev = ocelot->dev;
1784	phy_interface_t phy_mode;
1785	struct phylink *phylink;
1786	int err;
1787
1788	of_get_phy_mode(portnp, &phy_mode);
1789	/* DT bindings of internal PHY ports are broken and don't
1790	 * specify a phy-mode
1791	 */
1792	if (phy_mode == PHY_INTERFACE_MODE_NA)
1793		phy_mode = PHY_INTERFACE_MODE_INTERNAL;
1794
1795	if (phy_mode != PHY_INTERFACE_MODE_SGMII &&
1796	    phy_mode != PHY_INTERFACE_MODE_QSGMII &&
1797	    phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1798		dev_err(dev, "unsupported phy mode %s for port %d\n",
1799			phy_modes(phy_mode), port);
1800		return -EINVAL;
1801	}
1802
1803	ocelot_port->phy_mode = phy_mode;
1804
1805	err = ocelot_port_configure_serdes(ocelot, port, portnp);
1806	if (err)
1807		return err;
1808
1809	priv = container_of(ocelot_port, struct ocelot_port_private, port);
1810
1811	priv->phylink_config.dev = &priv->dev->dev;
1812	priv->phylink_config.type = PHYLINK_NETDEV;
1813	priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1814		MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
1815
1816	__set_bit(ocelot_port->phy_mode,
1817		  priv->phylink_config.supported_interfaces);
1818
1819	phylink = phylink_create(&priv->phylink_config,
1820				 of_fwnode_handle(portnp),
1821				 phy_mode, &ocelot_phylink_ops);
1822	if (IS_ERR(phylink)) {
1823		err = PTR_ERR(phylink);
1824		dev_err(dev, "Could not create phylink (%pe)\n", phylink);
1825		return err;
1826	}
1827
1828	priv->phylink = phylink;
1829
1830	err = phylink_of_phy_connect(phylink, portnp, 0);
1831	if (err) {
1832		dev_err(dev, "Could not connect to PHY: %pe\n", ERR_PTR(err));
1833		phylink_destroy(phylink);
1834		priv->phylink = NULL;
1835		return err;
1836	}
1837
1838	return 0;
1839}
1840
1841int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
1842		      struct device_node *portnp)
1843{
1844	struct ocelot_port_private *priv;
1845	struct ocelot_port *ocelot_port;
1846	struct net_device *dev;
1847	int err;
1848
1849	dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1850	if (!dev)
1851		return -ENOMEM;
1852	SET_NETDEV_DEV(dev, ocelot->dev);
1853	priv = netdev_priv(dev);
1854	priv->dev = dev;
1855	ocelot_port = &priv->port;
1856	ocelot_port->ocelot = ocelot;
1857	ocelot_port->index = port;
1858	ocelot_port->target = target;
1859	ocelot->ports[port] = ocelot_port;
1860
1861	dev->netdev_ops = &ocelot_port_netdev_ops;
1862	dev->ethtool_ops = &ocelot_ethtool_ops;
1863	dev->max_mtu = OCELOT_JUMBO_MTU;
1864
1865	dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1866		NETIF_F_HW_TC;
1867	dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1868
1869	err = of_get_ethdev_address(portnp, dev);
1870	if (err)
1871		eth_hw_addr_gen(dev, ocelot->base_mac, port);
1872
1873	ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1874			  OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
1875
1876	ocelot_init_port(ocelot, port);
1877
1878	err = ocelot_port_phylink_create(ocelot, port, portnp);
1879	if (err)
1880		goto out;
1881
1882	if (ocelot->fdma)
1883		ocelot_fdma_netdev_init(ocelot, dev);
1884
1885	SET_NETDEV_DEVLINK_PORT(dev, &ocelot->devlink_ports[port]);
1886	err = register_netdev(dev);
1887	if (err) {
1888		dev_err(ocelot->dev, "register_netdev failed\n");
1889		goto out_fdma_deinit;
1890	}
1891
1892	return 0;
1893
1894out_fdma_deinit:
1895	if (ocelot->fdma)
1896		ocelot_fdma_netdev_deinit(ocelot, dev);
1897out:
1898	ocelot->ports[port] = NULL;
1899	free_netdev(dev);
1900
1901	return err;
1902}
1903
1904void ocelot_release_port(struct ocelot_port *ocelot_port)
1905{
1906	struct ocelot_port_private *priv = container_of(ocelot_port,
1907						struct ocelot_port_private,
1908						port);
1909	struct ocelot *ocelot = ocelot_port->ocelot;
1910	struct ocelot_fdma *fdma = ocelot->fdma;
1911
1912	unregister_netdev(priv->dev);
1913
1914	if (fdma)
1915		ocelot_fdma_netdev_deinit(ocelot, priv->dev);
1916
1917	if (priv->phylink) {
1918		rtnl_lock();
1919		phylink_disconnect_phy(priv->phylink);
1920		rtnl_unlock();
1921
1922		phylink_destroy(priv->phylink);
1923	}
1924
1925	free_netdev(priv->dev);
1926}
~/ Read-only file preview.