Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) 2018-2021, Intel Corporation. */
   3
   4/* Link Aggregation code */
   5
   6#include "ice.h"
   7#include "ice_lib.h"
   8#include "ice_lag.h"
   9
  10#define ICE_LAG_RES_SHARED	BIT(14)
  11#define ICE_LAG_RES_VALID	BIT(15)
  12
  13#define LACP_TRAIN_PKT_LEN		16
  14static const u8 lacp_train_pkt[LACP_TRAIN_PKT_LEN] = { 0, 0, 0, 0, 0, 0,
  15						       0, 0, 0, 0, 0, 0,
  16						       0x88, 0x09, 0, 0 };
  17
  18#define ICE_RECIPE_LEN			64
  19static const u8 ice_dflt_vsi_rcp[ICE_RECIPE_LEN] = {
  20	0x05, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21	0x85, 0, 0x01, 0, 0, 0, 0xff, 0xff, 0x08, 0, 0, 0, 0, 0, 0, 0,
  22	0, 0, 0, 0, 0, 0, 0x30 };
  23static const u8 ice_lport_rcp[ICE_RECIPE_LEN] = {
  24	0x05, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25	0x85, 0, 0x16, 0, 0, 0, 0xff, 0xff, 0x07, 0, 0, 0, 0, 0, 0, 0,
  26	0, 0, 0, 0, 0, 0, 0x30 };
  27
  28/**
  29 * ice_lag_set_primary - set PF LAG state as Primary
  30 * @lag: LAG info struct
  31 */
  32static void ice_lag_set_primary(struct ice_lag *lag)
  33{
  34	struct ice_pf *pf = lag->pf;
  35
  36	if (!pf)
  37		return;
  38
  39	if (lag->role != ICE_LAG_UNSET && lag->role != ICE_LAG_BACKUP) {
  40		dev_warn(ice_pf_to_dev(pf), "%s: Attempt to be Primary, but incompatible state.\n",
  41			 netdev_name(lag->netdev));
  42		return;
  43	}
  44
  45	lag->role = ICE_LAG_PRIMARY;
  46}
  47
  48/**
  49 * ice_lag_set_backup - set PF LAG state to Backup
  50 * @lag: LAG info struct
  51 */
  52static void ice_lag_set_backup(struct ice_lag *lag)
  53{
  54	struct ice_pf *pf = lag->pf;
  55
  56	if (!pf)
  57		return;
  58
  59	if (lag->role != ICE_LAG_UNSET && lag->role != ICE_LAG_PRIMARY) {
  60		dev_dbg(ice_pf_to_dev(pf), "%s: Attempt to be Backup, but incompatible state\n",
  61			netdev_name(lag->netdev));
  62		return;
  63	}
  64
  65	lag->role = ICE_LAG_BACKUP;
  66}
  67
  68/**
  69 * netif_is_same_ice - determine if netdev is on the same ice NIC as local PF
  70 * @pf: local PF struct
  71 * @netdev: netdev we are evaluating
  72 */
  73static bool netif_is_same_ice(struct ice_pf *pf, struct net_device *netdev)
  74{
  75	struct ice_netdev_priv *np;
  76	struct ice_pf *test_pf;
  77	struct ice_vsi *vsi;
  78
  79	if (!netif_is_ice(netdev))
  80		return false;
  81
  82	np = netdev_priv(netdev);
  83	if (!np)
  84		return false;
  85
  86	vsi = np->vsi;
  87	if (!vsi)
  88		return false;
  89
  90	test_pf = vsi->back;
  91	if (!test_pf)
  92		return false;
  93
  94	if (pf->pdev->bus != test_pf->pdev->bus ||
  95	    pf->pdev->slot != test_pf->pdev->slot)
  96		return false;
  97
  98	return true;
  99}
 100
 101/**
 102 * ice_netdev_to_lag - return pointer to associated lag struct from netdev
 103 * @netdev: pointer to net_device struct to query
 104 */
 105static struct ice_lag *ice_netdev_to_lag(struct net_device *netdev)
 106{
 107	struct ice_netdev_priv *np;
 108	struct ice_vsi *vsi;
 109
 110	if (!netif_is_ice(netdev))
 111		return NULL;
 112
 113	np = netdev_priv(netdev);
 114	if (!np)
 115		return NULL;
 116
 117	vsi = np->vsi;
 118	if (!vsi)
 119		return NULL;
 120
 121	return vsi->back->lag;
 122}
 123
 124/**
 125 * ice_lag_find_hw_by_lport - return an hw struct from bond members lport
 126 * @lag: lag struct
 127 * @lport: lport value to search for
 128 */
 129static struct ice_hw *
 130ice_lag_find_hw_by_lport(struct ice_lag *lag, u8 lport)
 131{
 132	struct ice_lag_netdev_list *entry;
 133	struct net_device *tmp_netdev;
 134	struct ice_netdev_priv *np;
 135	struct ice_hw *hw;
 136
 137	list_for_each_entry(entry, lag->netdev_head, node) {
 138		tmp_netdev = entry->netdev;
 139		if (!tmp_netdev || !netif_is_ice(tmp_netdev))
 140			continue;
 141
 142		np = netdev_priv(tmp_netdev);
 143		if (!np || !np->vsi)
 144			continue;
 145
 146		hw = &np->vsi->back->hw;
 147		if (hw->port_info->lport == lport)
 148			return hw;
 149	}
 150
 151	return NULL;
 152}
 153
 154/**
 155 * ice_pkg_has_lport_extract - check if lport extraction supported
 156 * @hw: HW struct
 157 */
 158static bool ice_pkg_has_lport_extract(struct ice_hw *hw)
 159{
 160	int i;
 161
 162	for (i = 0; i < hw->blk[ICE_BLK_SW].es.count; i++) {
 163		u16 offset;
 164		u8 fv_prot;
 165
 166		ice_find_prot_off(hw, ICE_BLK_SW, ICE_SW_DEFAULT_PROFILE, i,
 167				  &fv_prot, &offset);
 168		if (fv_prot == ICE_FV_PROT_MDID &&
 169		    offset == ICE_LP_EXT_BUF_OFFSET)
 170			return true;
 171	}
 172	return false;
 173}
 174
 175/**
 176 * ice_lag_find_primary - returns pointer to primary interfaces lag struct
 177 * @lag: local interfaces lag struct
 178 */
 179static struct ice_lag *ice_lag_find_primary(struct ice_lag *lag)
 180{
 181	struct ice_lag *primary_lag = NULL;
 182	struct list_head *tmp;
 183
 184	list_for_each(tmp, lag->netdev_head) {
 185		struct ice_lag_netdev_list *entry;
 186		struct ice_lag *tmp_lag;
 187
 188		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
 189		tmp_lag = ice_netdev_to_lag(entry->netdev);
 190		if (tmp_lag && tmp_lag->primary) {
 191			primary_lag = tmp_lag;
 192			break;
 193		}
 194	}
 195
 196	return primary_lag;
 197}
 198
 199/**
 200 * ice_lag_cfg_fltr - Add/Remove rule for LAG
 201 * @lag: lag struct for local interface
 202 * @act: rule action
 203 * @recipe_id: recipe id for the new rule
 204 * @rule_idx: pointer to rule index
 205 * @direction: ICE_FLTR_RX or ICE_FLTR_TX
 206 * @add: boolean on whether we are adding filters
 207 */
 208static int
 209ice_lag_cfg_fltr(struct ice_lag *lag, u32 act, u16 recipe_id, u16 *rule_idx,
 210		 u8 direction, bool add)
 211{
 212	struct ice_sw_rule_lkup_rx_tx *s_rule;
 213	u16 s_rule_sz, vsi_num;
 214	struct ice_hw *hw;
 215	u8 *eth_hdr;
 216	u32 opc;
 217	int err;
 218
 219	hw = &lag->pf->hw;
 220	vsi_num = ice_get_hw_vsi_num(hw, 0);
 221
 222	s_rule_sz = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule);
 223	s_rule = kzalloc(s_rule_sz, GFP_KERNEL);
 224	if (!s_rule) {
 225		dev_err(ice_pf_to_dev(lag->pf), "error allocating rule for LAG\n");
 226		return -ENOMEM;
 227	}
 228
 229	if (add) {
 230		eth_hdr = s_rule->hdr_data;
 231		ice_fill_eth_hdr(eth_hdr);
 232
 233		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M, vsi_num);
 234
 
 235		s_rule->recipe_id = cpu_to_le16(recipe_id);
 236		if (direction == ICE_FLTR_RX) {
 237			s_rule->hdr.type =
 238				cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
 239			s_rule->src = cpu_to_le16(hw->port_info->lport);
 240		} else {
 241			s_rule->hdr.type =
 242				cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
 243			s_rule->src = cpu_to_le16(vsi_num);
 244		}
 245		s_rule->act = cpu_to_le32(act);
 246		s_rule->hdr_len = cpu_to_le16(DUMMY_ETH_HDR_LEN);
 247		opc = ice_aqc_opc_add_sw_rules;
 248	} else {
 249		s_rule->index = cpu_to_le16(*rule_idx);
 250		opc = ice_aqc_opc_remove_sw_rules;
 251	}
 252
 253	err = ice_aq_sw_rules(&lag->pf->hw, s_rule, s_rule_sz, 1, opc, NULL);
 254	if (err)
 255		goto dflt_fltr_free;
 256
 257	if (add)
 258		*rule_idx = le16_to_cpu(s_rule->index);
 259	else
 260		*rule_idx = 0;
 261
 262dflt_fltr_free:
 263	kfree(s_rule);
 264	return err;
 265}
 266
 267/**
 268 * ice_lag_cfg_dflt_fltr - Add/Remove default VSI rule for LAG
 269 * @lag: lag struct for local interface
 270 * @add: boolean on whether to add filter
 271 */
 272static int
 273ice_lag_cfg_dflt_fltr(struct ice_lag *lag, bool add)
 274{
 275	u32 act = ICE_SINGLE_ACT_VSI_FORWARDING |
 276		ICE_SINGLE_ACT_VALID_BIT | ICE_SINGLE_ACT_LAN_ENABLE;
 277	int err;
 278
 279	err = ice_lag_cfg_fltr(lag, act, lag->pf_recipe, &lag->pf_rx_rule_id,
 280			       ICE_FLTR_RX, add);
 281	if (err)
 282		goto err_rx;
 283
 284	act = ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_VALID_BIT |
 285	      ICE_SINGLE_ACT_LB_ENABLE;
 286	err = ice_lag_cfg_fltr(lag, act, lag->pf_recipe, &lag->pf_tx_rule_id,
 287			       ICE_FLTR_TX, add);
 288	if (err)
 289		goto err_tx;
 290
 291	return 0;
 292
 293err_tx:
 294	ice_lag_cfg_fltr(lag, act, lag->pf_recipe, &lag->pf_rx_rule_id,
 295			 ICE_FLTR_RX, !add);
 296err_rx:
 297	return err;
 298}
 299
 300/**
 301 * ice_lag_cfg_drop_fltr - Add/Remove lport drop rule
 302 * @lag: lag struct for local interface
 303 * @add: boolean on whether to add filter
 304 */
 305static int
 306ice_lag_cfg_drop_fltr(struct ice_lag *lag, bool add)
 307{
 308	u32 act = ICE_SINGLE_ACT_VSI_FORWARDING |
 309		  ICE_SINGLE_ACT_VALID_BIT |
 310		  ICE_SINGLE_ACT_DROP;
 311
 312	return ice_lag_cfg_fltr(lag, act, lag->lport_recipe,
 313				&lag->lport_rule_idx, ICE_FLTR_RX, add);
 314}
 315
 316/**
 317 * ice_lag_cfg_pf_fltrs - set filters up for new active port
 318 * @lag: local interfaces lag struct
 319 * @ptr: opaque data containing notifier event
 320 */
 321static void
 322ice_lag_cfg_pf_fltrs(struct ice_lag *lag, void *ptr)
 323{
 324	struct netdev_notifier_bonding_info *info;
 325	struct netdev_bonding_info *bonding_info;
 326	struct net_device *event_netdev;
 327	struct device *dev;
 328
 329	event_netdev = netdev_notifier_info_to_dev(ptr);
 330	/* not for this netdev */
 331	if (event_netdev != lag->netdev)
 332		return;
 333
 334	info = (struct netdev_notifier_bonding_info *)ptr;
 335	bonding_info = &info->bonding_info;
 336	dev = ice_pf_to_dev(lag->pf);
 337
 338	/* interface not active - remove old default VSI rule */
 339	if (bonding_info->slave.state && lag->pf_rx_rule_id) {
 340		if (ice_lag_cfg_dflt_fltr(lag, false))
 341			dev_err(dev, "Error removing old default VSI filter\n");
 342		if (ice_lag_cfg_drop_fltr(lag, true))
 343			dev_err(dev, "Error adding new drop filter\n");
 344		return;
 345	}
 346
 347	/* interface becoming active - add new default VSI rule */
 348	if (!bonding_info->slave.state && !lag->pf_rx_rule_id) {
 349		if (ice_lag_cfg_dflt_fltr(lag, true))
 350			dev_err(dev, "Error adding new default VSI filter\n");
 351		if (lag->lport_rule_idx && ice_lag_cfg_drop_fltr(lag, false))
 352			dev_err(dev, "Error removing old drop filter\n");
 353	}
 354}
 355
 356/**
 357 * ice_display_lag_info - print LAG info
 358 * @lag: LAG info struct
 359 */
 360static void ice_display_lag_info(struct ice_lag *lag)
 361{
 362	const char *name, *upper, *role, *bonded, *primary;
 363	struct device *dev = &lag->pf->pdev->dev;
 364
 365	name = lag->netdev ? netdev_name(lag->netdev) : "unset";
 366	upper = lag->upper_netdev ? netdev_name(lag->upper_netdev) : "unset";
 367	primary = lag->primary ? "TRUE" : "FALSE";
 368	bonded = lag->bonded ? "BONDED" : "UNBONDED";
 369
 370	switch (lag->role) {
 371	case ICE_LAG_NONE:
 372		role = "NONE";
 373		break;
 374	case ICE_LAG_PRIMARY:
 375		role = "PRIMARY";
 376		break;
 377	case ICE_LAG_BACKUP:
 378		role = "BACKUP";
 379		break;
 380	case ICE_LAG_UNSET:
 381		role = "UNSET";
 382		break;
 383	default:
 384		role = "ERROR";
 385	}
 386
 387	dev_dbg(dev, "%s %s, upper:%s, role:%s, primary:%s\n", name, bonded,
 388		upper, role, primary);
 389}
 390
 391/**
 392 * ice_lag_qbuf_recfg - generate a buffer of queues for a reconfigure command
 393 * @hw: HW struct that contains the queue contexts
 394 * @qbuf: pointer to buffer to populate
 395 * @vsi_num: index of the VSI in PF space
 396 * @numq: number of queues to search for
 397 * @tc: traffic class that contains the queues
 398 *
 399 * function returns the number of valid queues in buffer
 400 */
 401static u16
 402ice_lag_qbuf_recfg(struct ice_hw *hw, struct ice_aqc_cfg_txqs_buf *qbuf,
 403		   u16 vsi_num, u16 numq, u8 tc)
 404{
 405	struct ice_q_ctx *q_ctx;
 406	u16 qid, count = 0;
 407	struct ice_pf *pf;
 408	int i;
 409
 410	pf = hw->back;
 411	for (i = 0; i < numq; i++) {
 412		q_ctx = ice_get_lan_q_ctx(hw, vsi_num, tc, i);
 413		if (!q_ctx) {
 414			dev_dbg(ice_hw_to_dev(hw), "%s queue %d NO Q CONTEXT\n",
 415				__func__, i);
 416			continue;
 417		}
 418		if (q_ctx->q_teid == ICE_INVAL_TEID) {
 419			dev_dbg(ice_hw_to_dev(hw), "%s queue %d INVAL TEID\n",
 420				__func__, i);
 421			continue;
 422		}
 423		if (q_ctx->q_handle == ICE_INVAL_Q_HANDLE) {
 424			dev_dbg(ice_hw_to_dev(hw), "%s queue %d INVAL Q HANDLE\n",
 425				__func__, i);
 426			continue;
 427		}
 428
 429		qid = pf->vsi[vsi_num]->txq_map[q_ctx->q_handle];
 430		qbuf->queue_info[count].q_handle = cpu_to_le16(qid);
 431		qbuf->queue_info[count].tc = tc;
 432		qbuf->queue_info[count].q_teid = cpu_to_le32(q_ctx->q_teid);
 433		count++;
 434	}
 435
 436	return count;
 437}
 438
 439/**
 440 * ice_lag_get_sched_parent - locate or create a sched node parent
 441 * @hw: HW struct for getting parent in
 442 * @tc: traffic class on parent/node
 443 */
 444static struct ice_sched_node *
 445ice_lag_get_sched_parent(struct ice_hw *hw, u8 tc)
 446{
 447	struct ice_sched_node *tc_node, *aggnode, *parent = NULL;
 448	u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
 449	struct ice_port_info *pi = hw->port_info;
 450	struct device *dev;
 451	u8 aggl, vsil;
 452	int n;
 453
 454	dev = ice_hw_to_dev(hw);
 455
 456	tc_node = ice_sched_get_tc_node(pi, tc);
 457	if (!tc_node) {
 458		dev_warn(dev, "Failure to find TC node for LAG move\n");
 459		return parent;
 460	}
 461
 462	aggnode = ice_sched_get_agg_node(pi, tc_node, ICE_DFLT_AGG_ID);
 463	if (!aggnode) {
 464		dev_warn(dev, "Failure to find aggregate node for LAG move\n");
 465		return parent;
 466	}
 467
 468	aggl = ice_sched_get_agg_layer(hw);
 469	vsil = ice_sched_get_vsi_layer(hw);
 470
 471	for (n = aggl + 1; n < vsil; n++)
 472		num_nodes[n] = 1;
 473
 474	for (n = 0; n < aggnode->num_children; n++) {
 475		parent = ice_sched_get_free_vsi_parent(hw, aggnode->children[n],
 476						       num_nodes);
 477		if (parent)
 478			return parent;
 479	}
 480
 481	/* if free parent not found - add one */
 482	parent = aggnode;
 483	for (n = aggl + 1; n < vsil; n++) {
 484		u16 num_nodes_added;
 485		u32 first_teid;
 486		int err;
 487
 488		err = ice_sched_add_nodes_to_layer(pi, tc_node, parent, n,
 489						   num_nodes[n], &first_teid,
 490						   &num_nodes_added);
 491		if (err || num_nodes[n] != num_nodes_added)
 492			return NULL;
 493
 494		if (num_nodes_added)
 495			parent = ice_sched_find_node_by_teid(tc_node,
 496							     first_teid);
 497		else
 498			parent = parent->children[0];
 499		if (!parent) {
 500			dev_warn(dev, "Failure to add new parent for LAG move\n");
 501			return parent;
 502		}
 503	}
 504
 505	return parent;
 506}
 507
 508/**
 509 * ice_lag_move_vf_node_tc - move scheduling nodes for one VF on one TC
 510 * @lag: lag info struct
 511 * @oldport: lport of previous nodes location
 512 * @newport: lport of destination nodes location
 513 * @vsi_num: array index of VSI in PF space
 514 * @tc: traffic class to move
 515 */
 516static void
 517ice_lag_move_vf_node_tc(struct ice_lag *lag, u8 oldport, u8 newport,
 518			u16 vsi_num, u8 tc)
 519{
 520	DEFINE_RAW_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
 521	struct device *dev = ice_pf_to_dev(lag->pf);
 522	u16 numq, valq, num_moved, qbuf_size;
 523	u16 buf_size = __struct_size(buf);
 524	struct ice_aqc_cfg_txqs_buf *qbuf;
 525	struct ice_sched_node *n_prt;
 526	struct ice_hw *new_hw = NULL;
 527	__le32 teid, parent_teid;
 528	struct ice_vsi_ctx *ctx;
 529	u32 tmp_teid;
 530
 531	ctx = ice_get_vsi_ctx(&lag->pf->hw, vsi_num);
 532	if (!ctx) {
 533		dev_warn(dev, "Unable to locate VSI context for LAG failover\n");
 534		return;
 535	}
 536
 537	/* check to see if this VF is enabled on this TC */
 538	if (!ctx->sched.vsi_node[tc])
 539		return;
 540
 541	/* locate HW struct for destination port */
 542	new_hw = ice_lag_find_hw_by_lport(lag, newport);
 543	if (!new_hw) {
 544		dev_warn(dev, "Unable to locate HW struct for LAG node destination\n");
 545		return;
 546	}
 547
 548	numq = ctx->num_lan_q_entries[tc];
 549	teid = ctx->sched.vsi_node[tc]->info.node_teid;
 550	tmp_teid = le32_to_cpu(teid);
 551	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
 552	/* if no teid assigned or numq == 0, then this TC is not active */
 553	if (!tmp_teid || !numq)
 554		return;
 555
 556	/* suspend VSI subtree for Traffic Class "tc" on
 557	 * this VF's VSI
 558	 */
 559	if (ice_sched_suspend_resume_elems(&lag->pf->hw, 1, &tmp_teid, true))
 560		dev_dbg(dev, "Problem suspending traffic for LAG node move\n");
 561
 562	/* reconfigure all VF's queues on this Traffic Class
 563	 * to new port
 564	 */
 565	qbuf_size = struct_size(qbuf, queue_info, numq);
 566	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
 567	if (!qbuf) {
 568		dev_warn(dev, "Failure allocating memory for VF queue recfg buffer\n");
 569		goto resume_traffic;
 570	}
 571
 572	/* add the per queue info for the reconfigure command buffer */
 573	valq = ice_lag_qbuf_recfg(&lag->pf->hw, qbuf, vsi_num, numq, tc);
 574	if (!valq) {
 575		dev_dbg(dev, "No valid queues found for LAG failover\n");
 576		goto qbuf_none;
 577	}
 578
 579	if (ice_aq_cfg_lan_txq(&lag->pf->hw, qbuf, qbuf_size, valq, oldport,
 580			       newport, NULL)) {
 581		dev_warn(dev, "Failure to configure queues for LAG failover\n");
 582		goto qbuf_err;
 583	}
 584
 585qbuf_none:
 586	kfree(qbuf);
 587
 588	/* find new parent in destination port's tree for VF VSI node on this
 589	 * Traffic Class
 590	 */
 591	n_prt = ice_lag_get_sched_parent(new_hw, tc);
 592	if (!n_prt)
 593		goto resume_traffic;
 594
 595	/* Move Vf's VSI node for this TC to newport's scheduler tree */
 596	buf->hdr.src_parent_teid = parent_teid;
 597	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
 598	buf->hdr.num_elems = cpu_to_le16(1);
 599	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
 600	buf->teid[0] = teid;
 601
 602	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
 603		dev_warn(dev, "Failure to move VF nodes for failover\n");
 604	else
 605		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
 606
 607	goto resume_traffic;
 608
 609qbuf_err:
 610	kfree(qbuf);
 611
 612resume_traffic:
 613	/* restart traffic for VSI node */
 614	if (ice_sched_suspend_resume_elems(&lag->pf->hw, 1, &tmp_teid, false))
 615		dev_dbg(dev, "Problem restarting traffic for LAG node move\n");
 616}
 617
 618/**
 619 * ice_lag_build_netdev_list - populate the lag struct's netdev list
 620 * @lag: local lag struct
 621 * @ndlist: pointer to netdev list to populate
 622 */
 623static void ice_lag_build_netdev_list(struct ice_lag *lag,
 624				      struct ice_lag_netdev_list *ndlist)
 625{
 626	struct ice_lag_netdev_list *nl;
 627	struct net_device *tmp_nd;
 628
 629	INIT_LIST_HEAD(&ndlist->node);
 630	rcu_read_lock();
 631	for_each_netdev_in_bond_rcu(lag->upper_netdev, tmp_nd) {
 632		nl = kzalloc(sizeof(*nl), GFP_ATOMIC);
 633		if (!nl)
 634			break;
 635
 636		nl->netdev = tmp_nd;
 637		list_add(&nl->node, &ndlist->node);
 638	}
 639	rcu_read_unlock();
 640	lag->netdev_head = &ndlist->node;
 641}
 642
 643/**
 644 * ice_lag_destroy_netdev_list - free lag struct's netdev list
 645 * @lag: pointer to local lag struct
 646 * @ndlist: pointer to lag struct netdev list
 647 */
 648static void ice_lag_destroy_netdev_list(struct ice_lag *lag,
 649					struct ice_lag_netdev_list *ndlist)
 650{
 651	struct ice_lag_netdev_list *entry, *n;
 652
 653	rcu_read_lock();
 654	list_for_each_entry_safe(entry, n, &ndlist->node, node) {
 655		list_del(&entry->node);
 656		kfree(entry);
 657	}
 658	rcu_read_unlock();
 659	lag->netdev_head = NULL;
 660}
 661
 662/**
 663 * ice_lag_move_single_vf_nodes - Move Tx scheduling nodes for single VF
 664 * @lag: primary interface LAG struct
 665 * @oldport: lport of previous interface
 666 * @newport: lport of destination interface
 667 * @vsi_num: SW index of VF's VSI
 668 */
 669static void
 670ice_lag_move_single_vf_nodes(struct ice_lag *lag, u8 oldport, u8 newport,
 671			     u16 vsi_num)
 672{
 673	u8 tc;
 674
 675	ice_for_each_traffic_class(tc)
 676		ice_lag_move_vf_node_tc(lag, oldport, newport, vsi_num, tc);
 677}
 678
 679/**
 680 * ice_lag_move_new_vf_nodes - Move Tx scheduling nodes for a VF if required
 681 * @vf: the VF to move Tx nodes for
 682 *
 683 * Called just after configuring new VF queues. Check whether the VF Tx
 684 * scheduling nodes need to be updated to fail over to the active port. If so,
 685 * move them now.
 686 */
 687void ice_lag_move_new_vf_nodes(struct ice_vf *vf)
 688{
 689	struct ice_lag_netdev_list ndlist;
 690	u8 pri_port, act_port;
 691	struct ice_lag *lag;
 692	struct ice_vsi *vsi;
 693	struct ice_pf *pf;
 694
 695	vsi = ice_get_vf_vsi(vf);
 696
 697	if (WARN_ON(!vsi))
 698		return;
 699
 700	if (WARN_ON(vsi->type != ICE_VSI_VF))
 701		return;
 702
 703	pf = vf->pf;
 704	lag = pf->lag;
 705
 706	mutex_lock(&pf->lag_mutex);
 707	if (!lag->bonded)
 708		goto new_vf_unlock;
 709
 710	pri_port = pf->hw.port_info->lport;
 711	act_port = lag->active_port;
 712
 713	if (lag->upper_netdev)
 714		ice_lag_build_netdev_list(lag, &ndlist);
 715
 716	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG) &&
 717	    lag->bonded && lag->primary && pri_port != act_port &&
 718	    !list_empty(lag->netdev_head))
 719		ice_lag_move_single_vf_nodes(lag, pri_port, act_port, vsi->idx);
 720
 721	ice_lag_destroy_netdev_list(lag, &ndlist);
 722
 723new_vf_unlock:
 724	mutex_unlock(&pf->lag_mutex);
 725}
 726
 727/**
 728 * ice_lag_move_vf_nodes - move Tx scheduling nodes for all VFs to new port
 729 * @lag: lag info struct
 730 * @oldport: lport of previous interface
 731 * @newport: lport of destination interface
 732 */
 733static void ice_lag_move_vf_nodes(struct ice_lag *lag, u8 oldport, u8 newport)
 734{
 735	struct ice_pf *pf;
 736	int i;
 737
 738	if (!lag->primary)
 739		return;
 740
 741	pf = lag->pf;
 742	ice_for_each_vsi(pf, i)
 743		if (pf->vsi[i] && pf->vsi[i]->type == ICE_VSI_VF)
 
 744			ice_lag_move_single_vf_nodes(lag, oldport, newport, i);
 745}
 746
 747/**
 748 * ice_lag_move_vf_nodes_cfg - move vf nodes outside LAG netdev event context
 749 * @lag: local lag struct
 750 * @src_prt: lport value for source port
 751 * @dst_prt: lport value for destination port
 752 *
 753 * This function is used to move nodes during an out-of-netdev-event situation,
 754 * primarily when the driver needs to reconfigure or recreate resources.
 755 *
 756 * Must be called while holding the lag_mutex to avoid lag events from
 757 * processing while out-of-sync moves are happening.  Also, paired moves,
 758 * such as used in a reset flow, should both be called under the same mutex
 759 * lock to avoid changes between start of reset and end of reset.
 760 */
 761void ice_lag_move_vf_nodes_cfg(struct ice_lag *lag, u8 src_prt, u8 dst_prt)
 762{
 763	struct ice_lag_netdev_list ndlist;
 764
 765	ice_lag_build_netdev_list(lag, &ndlist);
 766	ice_lag_move_vf_nodes(lag, src_prt, dst_prt);
 767	ice_lag_destroy_netdev_list(lag, &ndlist);
 768}
 769
 770#define ICE_LAG_SRIOV_CP_RECIPE		10
 771#define ICE_LAG_SRIOV_TRAIN_PKT_LEN	16
 772
 773/**
 774 * ice_lag_cfg_cp_fltr - configure filter for control packets
 775 * @lag: local interface's lag struct
 776 * @add: add or remove rule
 777 */
 778static void
 779ice_lag_cfg_cp_fltr(struct ice_lag *lag, bool add)
 780{
 781	struct ice_sw_rule_lkup_rx_tx *s_rule = NULL;
 782	struct ice_vsi *vsi;
 783	u16 buf_len, opc;
 784
 785	vsi = lag->pf->vsi[0];
 786
 787	buf_len = ICE_SW_RULE_RX_TX_HDR_SIZE(s_rule,
 788					     ICE_LAG_SRIOV_TRAIN_PKT_LEN);
 789	s_rule = kzalloc(buf_len, GFP_KERNEL);
 790	if (!s_rule) {
 791		netdev_warn(lag->netdev, "-ENOMEM error configuring CP filter\n");
 792		return;
 793	}
 794
 795	if (add) {
 796		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
 797		s_rule->recipe_id = cpu_to_le16(ICE_LAG_SRIOV_CP_RECIPE);
 798		s_rule->src = cpu_to_le16(vsi->port_info->lport);
 799		s_rule->act = cpu_to_le32(ICE_FWD_TO_VSI |
 800					  ICE_SINGLE_ACT_LAN_ENABLE |
 801					  ICE_SINGLE_ACT_VALID_BIT |
 802					  FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M, vsi->vsi_num));
 803		s_rule->hdr_len = cpu_to_le16(ICE_LAG_SRIOV_TRAIN_PKT_LEN);
 804		memcpy(s_rule->hdr_data, lacp_train_pkt, LACP_TRAIN_PKT_LEN);
 805		opc = ice_aqc_opc_add_sw_rules;
 806	} else {
 807		opc = ice_aqc_opc_remove_sw_rules;
 808		s_rule->index = cpu_to_le16(lag->cp_rule_idx);
 809	}
 810	if (ice_aq_sw_rules(&lag->pf->hw, s_rule, buf_len, 1, opc, NULL)) {
 811		netdev_warn(lag->netdev, "Error %s CP rule for fail-over\n",
 812			    add ? "ADDING" : "REMOVING");
 813		goto cp_free;
 814	}
 815
 816	if (add)
 817		lag->cp_rule_idx = le16_to_cpu(s_rule->index);
 818	else
 819		lag->cp_rule_idx = 0;
 820
 821cp_free:
 822	kfree(s_rule);
 823}
 824
 825/**
 826 * ice_lag_info_event - handle NETDEV_BONDING_INFO event
 827 * @lag: LAG info struct
 828 * @ptr: opaque data pointer
 829 *
 830 * ptr is to be cast to (netdev_notifier_bonding_info *)
 831 */
 832static void ice_lag_info_event(struct ice_lag *lag, void *ptr)
 833{
 834	struct netdev_notifier_bonding_info *info;
 835	struct netdev_bonding_info *bonding_info;
 836	struct net_device *event_netdev;
 837	const char *lag_netdev_name;
 838
 839	event_netdev = netdev_notifier_info_to_dev(ptr);
 840	info = ptr;
 841	lag_netdev_name = netdev_name(lag->netdev);
 842	bonding_info = &info->bonding_info;
 843
 844	if (event_netdev != lag->netdev || !lag->bonded || !lag->upper_netdev)
 845		return;
 846
 847	if (bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) {
 848		netdev_dbg(lag->netdev, "Bonding event recv, but mode not active/backup\n");
 849		goto lag_out;
 850	}
 851
 852	if (strcmp(bonding_info->slave.slave_name, lag_netdev_name)) {
 853		netdev_dbg(lag->netdev, "Bonding event recv, but secondary info not for us\n");
 854		goto lag_out;
 855	}
 856
 857	if (bonding_info->slave.state)
 858		ice_lag_set_backup(lag);
 859	else
 860		ice_lag_set_primary(lag);
 861
 862lag_out:
 863	ice_display_lag_info(lag);
 864}
 865
 866/**
 867 * ice_lag_reclaim_vf_tc - move scheduling nodes back to primary interface
 868 * @lag: primary interface lag struct
 869 * @src_hw: HW struct current node location
 870 * @vsi_num: VSI index in PF space
 871 * @tc: traffic class to move
 872 */
 873static void
 874ice_lag_reclaim_vf_tc(struct ice_lag *lag, struct ice_hw *src_hw, u16 vsi_num,
 875		      u8 tc)
 876{
 877	DEFINE_RAW_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
 878	struct device *dev = ice_pf_to_dev(lag->pf);
 879	u16 numq, valq, num_moved, qbuf_size;
 880	u16 buf_size = __struct_size(buf);
 881	struct ice_aqc_cfg_txqs_buf *qbuf;
 882	struct ice_sched_node *n_prt;
 883	__le32 teid, parent_teid;
 884	struct ice_vsi_ctx *ctx;
 885	struct ice_hw *hw;
 886	u32 tmp_teid;
 887
 888	hw = &lag->pf->hw;
 889	ctx = ice_get_vsi_ctx(hw, vsi_num);
 890	if (!ctx) {
 891		dev_warn(dev, "Unable to locate VSI context for LAG reclaim\n");
 892		return;
 893	}
 894
 895	/* check to see if this VF is enabled on this TC */
 896	if (!ctx->sched.vsi_node[tc])
 897		return;
 898
 899	numq = ctx->num_lan_q_entries[tc];
 900	teid = ctx->sched.vsi_node[tc]->info.node_teid;
 901	tmp_teid = le32_to_cpu(teid);
 902	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
 903
 904	/* if !teid or !numq, then this TC is not active */
 905	if (!tmp_teid || !numq)
 906		return;
 907
 908	/* suspend traffic */
 909	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, true))
 910		dev_dbg(dev, "Problem suspending traffic for LAG node move\n");
 911
 912	/* reconfig queues for new port */
 913	qbuf_size = struct_size(qbuf, queue_info, numq);
 914	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
 915	if (!qbuf) {
 916		dev_warn(dev, "Failure allocating memory for VF queue recfg buffer\n");
 917		goto resume_reclaim;
 918	}
 919
 920	/* add the per queue info for the reconfigure command buffer */
 921	valq = ice_lag_qbuf_recfg(hw, qbuf, vsi_num, numq, tc);
 922	if (!valq) {
 923		dev_dbg(dev, "No valid queues found for LAG reclaim\n");
 924		goto reclaim_none;
 925	}
 926
 927	if (ice_aq_cfg_lan_txq(hw, qbuf, qbuf_size, numq,
 928			       src_hw->port_info->lport, hw->port_info->lport,
 929			       NULL)) {
 930		dev_warn(dev, "Failure to configure queues for LAG failover\n");
 931		goto reclaim_qerr;
 932	}
 933
 934reclaim_none:
 935	kfree(qbuf);
 936
 937	/* find parent in primary tree */
 938	n_prt = ice_lag_get_sched_parent(hw, tc);
 939	if (!n_prt)
 940		goto resume_reclaim;
 941
 942	/* Move node to new parent */
 943	buf->hdr.src_parent_teid = parent_teid;
 944	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
 945	buf->hdr.num_elems = cpu_to_le16(1);
 946	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
 947	buf->teid[0] = teid;
 948
 949	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
 950		dev_warn(dev, "Failure to move VF nodes for LAG reclaim\n");
 951	else
 952		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
 953
 954	goto resume_reclaim;
 955
 956reclaim_qerr:
 957	kfree(qbuf);
 958
 959resume_reclaim:
 960	/* restart traffic */
 961	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, false))
 962		dev_warn(dev, "Problem restarting traffic for LAG node reclaim\n");
 963}
 964
 965/**
 966 * ice_lag_reclaim_vf_nodes - When interface leaving bond primary reclaims nodes
 967 * @lag: primary interface lag struct
 968 * @src_hw: HW struct for current node location
 969 */
 970static void
 971ice_lag_reclaim_vf_nodes(struct ice_lag *lag, struct ice_hw *src_hw)
 972{
 973	struct ice_pf *pf;
 974	int i, tc;
 975
 976	if (!lag->primary || !src_hw)
 977		return;
 978
 979	pf = lag->pf;
 980	ice_for_each_vsi(pf, i)
 981		if (pf->vsi[i] && pf->vsi[i]->type == ICE_VSI_VF)
 
 982			ice_for_each_traffic_class(tc)
 983				ice_lag_reclaim_vf_tc(lag, src_hw, i, tc);
 984}
 985
 986/**
 987 * ice_lag_link - handle LAG link event
 988 * @lag: LAG info struct
 989 */
 990static void ice_lag_link(struct ice_lag *lag)
 991{
 992	struct ice_pf *pf = lag->pf;
 993
 994	if (lag->bonded)
 995		dev_warn(ice_pf_to_dev(pf), "%s Already part of a bond\n",
 996			 netdev_name(lag->netdev));
 997
 998	lag->bonded = true;
 999	lag->role = ICE_LAG_UNSET;
1000	netdev_info(lag->netdev, "Shared SR-IOV resources in bond are active\n");
1001}
1002
1003/**
1004 * ice_lag_unlink - handle unlink event
1005 * @lag: LAG info struct
1006 */
1007static void ice_lag_unlink(struct ice_lag *lag)
1008{
1009	u8 pri_port, act_port, loc_port;
1010	struct ice_pf *pf = lag->pf;
1011
1012	if (!lag->bonded) {
1013		netdev_dbg(lag->netdev, "bonding unlink event on non-LAG netdev\n");
1014		return;
1015	}
1016
1017	if (lag->primary) {
1018		act_port = lag->active_port;
1019		pri_port = lag->pf->hw.port_info->lport;
1020		if (act_port != pri_port && act_port != ICE_LAG_INVALID_PORT)
1021			ice_lag_move_vf_nodes(lag, act_port, pri_port);
1022		lag->primary = false;
1023		lag->active_port = ICE_LAG_INVALID_PORT;
1024	} else {
1025		struct ice_lag *primary_lag;
1026
1027		primary_lag = ice_lag_find_primary(lag);
1028		if (primary_lag) {
1029			act_port = primary_lag->active_port;
1030			pri_port = primary_lag->pf->hw.port_info->lport;
1031			loc_port = pf->hw.port_info->lport;
1032			if (act_port == loc_port &&
1033			    act_port != ICE_LAG_INVALID_PORT) {
1034				ice_lag_reclaim_vf_nodes(primary_lag,
1035							 &lag->pf->hw);
1036				primary_lag->active_port = ICE_LAG_INVALID_PORT;
1037			}
1038		}
1039	}
1040
1041	lag->bonded = false;
1042	lag->role = ICE_LAG_NONE;
1043	lag->upper_netdev = NULL;
1044}
1045
1046/**
1047 * ice_lag_link_unlink - helper function to call lag_link/unlink
1048 * @lag: lag info struct
1049 * @ptr: opaque pointer data
1050 */
1051static void ice_lag_link_unlink(struct ice_lag *lag, void *ptr)
1052{
1053	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1054	struct netdev_notifier_changeupper_info *info = ptr;
1055
1056	if (netdev != lag->netdev)
1057		return;
1058
1059	if (info->linking)
1060		ice_lag_link(lag);
1061	else
1062		ice_lag_unlink(lag);
1063}
1064
1065/**
1066 * ice_lag_set_swid - set the SWID on secondary interface
1067 * @primary_swid: primary interface's SWID
1068 * @local_lag: local interfaces LAG struct
1069 * @link: Is this a linking activity
1070 *
1071 * If link is false, then primary_swid should be expected to not be valid
1072 * This function should never be called in interrupt context.
1073 */
1074static void
1075ice_lag_set_swid(u16 primary_swid, struct ice_lag *local_lag,
1076		 bool link)
1077{
1078	struct ice_aqc_alloc_free_res_elem *buf;
1079	struct ice_aqc_set_port_params *cmd;
1080	struct ice_aq_desc desc;
1081	u16 buf_len, swid;
1082	int status, i;
1083
1084	buf_len = struct_size(buf, elem, 1);
1085	buf = kzalloc(buf_len, GFP_KERNEL);
1086	if (!buf) {
1087		dev_err(ice_pf_to_dev(local_lag->pf), "-ENOMEM error setting SWID\n");
1088		return;
1089	}
1090
1091	buf->num_elems = cpu_to_le16(1);
1092	buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_SWID);
1093	/* if unlinnking need to free the shared resource */
1094	if (!link && local_lag->bond_swid) {
1095		buf->elem[0].e.sw_resp = cpu_to_le16(local_lag->bond_swid);
1096		status = ice_aq_alloc_free_res(&local_lag->pf->hw, buf,
1097					       buf_len, ice_aqc_opc_free_res);
1098		if (status)
1099			dev_err(ice_pf_to_dev(local_lag->pf), "Error freeing SWID during LAG unlink\n");
1100		local_lag->bond_swid = 0;
1101	}
1102
1103	if (link) {
1104		buf->res_type |=  cpu_to_le16(ICE_LAG_RES_SHARED |
1105					      ICE_LAG_RES_VALID);
1106		/* store the primary's SWID in case it leaves bond first */
1107		local_lag->bond_swid = primary_swid;
1108		buf->elem[0].e.sw_resp = cpu_to_le16(local_lag->bond_swid);
1109	} else {
1110		buf->elem[0].e.sw_resp =
1111			cpu_to_le16(local_lag->pf->hw.port_info->sw_id);
1112	}
1113
1114	status = ice_aq_alloc_free_res(&local_lag->pf->hw, buf, buf_len,
1115				       ice_aqc_opc_alloc_res);
1116	if (status)
1117		dev_err(ice_pf_to_dev(local_lag->pf), "Error subscribing to SWID 0x%04X\n",
1118			local_lag->bond_swid);
1119
1120	kfree(buf);
1121
1122	/* Configure port param SWID to correct value */
1123	if (link)
1124		swid = primary_swid;
1125	else
1126		swid = local_lag->pf->hw.port_info->sw_id;
1127
1128	cmd = &desc.params.set_port_params;
1129	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_params);
1130
1131	cmd->swid = cpu_to_le16(ICE_AQC_PORT_SWID_VALID | swid);
1132	/* If this is happening in reset context, it is possible that the
1133	 * primary interface has not finished setting its SWID to SHARED
1134	 * yet.  Allow retries to account for this timing issue between
1135	 * interfaces.
1136	 */
1137	for (i = 0; i < ICE_LAG_RESET_RETRIES; i++) {
1138		status = ice_aq_send_cmd(&local_lag->pf->hw, &desc, NULL, 0,
1139					 NULL);
1140		if (!status)
1141			break;
1142
1143		usleep_range(1000, 2000);
1144	}
1145
1146	if (status)
1147		dev_err(ice_pf_to_dev(local_lag->pf), "Error setting SWID in port params %d\n",
1148			status);
1149}
1150
1151/**
1152 * ice_lag_primary_swid - set/clear the SHARED attrib of primary's SWID
1153 * @lag: primary interface's lag struct
1154 * @link: is this a linking activity
1155 *
1156 * Implement setting primary SWID as shared using 0x020B
1157 */
1158static void ice_lag_primary_swid(struct ice_lag *lag, bool link)
1159{
1160	struct ice_hw *hw;
1161	u16 swid;
1162
1163	hw = &lag->pf->hw;
1164	swid = hw->port_info->sw_id;
1165
1166	if (ice_share_res(hw, ICE_AQC_RES_TYPE_SWID, link, swid))
1167		dev_warn(ice_pf_to_dev(lag->pf), "Failure to set primary interface shared status\n");
1168}
1169
1170/**
1171 * ice_lag_add_prune_list - Adds event_pf's VSI to primary's prune list
1172 * @lag: lag info struct
1173 * @event_pf: PF struct for VSI we are adding to primary's prune list
1174 */
1175static void ice_lag_add_prune_list(struct ice_lag *lag, struct ice_pf *event_pf)
1176{
1177	u16 num_vsi, rule_buf_sz, vsi_list_id, event_vsi_num, prim_vsi_idx;
1178	struct ice_sw_rule_vsi_list *s_rule = NULL;
1179	struct device *dev;
1180
1181	num_vsi = 1;
1182
1183	dev = ice_pf_to_dev(lag->pf);
1184	event_vsi_num = event_pf->vsi[0]->vsi_num;
1185	prim_vsi_idx = lag->pf->vsi[0]->idx;
1186
1187	if (!ice_find_vsi_list_entry(&lag->pf->hw, ICE_SW_LKUP_VLAN,
1188				     prim_vsi_idx, &vsi_list_id)) {
1189		dev_warn(dev, "Could not locate prune list when setting up SRIOV LAG\n");
1190		return;
1191	}
1192
1193	rule_buf_sz = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
1194	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
1195	if (!s_rule) {
1196		dev_warn(dev, "Error allocating space for prune list when configuring SRIOV LAG\n");
1197		return;
1198	}
1199
1200	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_PRUNE_LIST_SET);
1201	s_rule->index = cpu_to_le16(vsi_list_id);
1202	s_rule->number_vsi = cpu_to_le16(num_vsi);
1203	s_rule->vsi[0] = cpu_to_le16(event_vsi_num);
1204
1205	if (ice_aq_sw_rules(&event_pf->hw, s_rule, rule_buf_sz, 1,
1206			    ice_aqc_opc_update_sw_rules, NULL))
1207		dev_warn(dev, "Error adding VSI prune list\n");
1208	kfree(s_rule);
1209}
1210
1211/**
1212 * ice_lag_del_prune_list - Remove secondary's vsi from primary's prune list
1213 * @lag: primary interface's ice_lag struct
1214 * @event_pf: PF struct for unlinking interface
1215 */
1216static void ice_lag_del_prune_list(struct ice_lag *lag, struct ice_pf *event_pf)
1217{
1218	u16 num_vsi, vsi_num, vsi_idx, rule_buf_sz, vsi_list_id;
1219	struct ice_sw_rule_vsi_list *s_rule = NULL;
1220	struct device *dev;
1221
1222	num_vsi = 1;
1223
1224	dev = ice_pf_to_dev(lag->pf);
1225	vsi_num = event_pf->vsi[0]->vsi_num;
1226	vsi_idx = lag->pf->vsi[0]->idx;
1227
1228	if (!ice_find_vsi_list_entry(&lag->pf->hw, ICE_SW_LKUP_VLAN,
1229				     vsi_idx, &vsi_list_id)) {
1230		dev_warn(dev, "Could not locate prune list when unwinding SRIOV LAG\n");
1231		return;
1232	}
1233
1234	rule_buf_sz = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
1235	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
1236	if (!s_rule) {
1237		dev_warn(dev, "Error allocating prune list when unwinding SRIOV LAG\n");
1238		return;
1239	}
1240
1241	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR);
1242	s_rule->index = cpu_to_le16(vsi_list_id);
1243	s_rule->number_vsi = cpu_to_le16(num_vsi);
1244	s_rule->vsi[0] = cpu_to_le16(vsi_num);
1245
1246	if (ice_aq_sw_rules(&event_pf->hw, (struct ice_aqc_sw_rules *)s_rule,
1247			    rule_buf_sz, 1, ice_aqc_opc_update_sw_rules, NULL))
1248		dev_warn(dev, "Error clearing VSI prune list\n");
1249
1250	kfree(s_rule);
1251}
1252
1253/**
1254 * ice_lag_init_feature_support_flag - Check for package and NVM support for LAG
1255 * @pf: PF struct
1256 */
1257static void ice_lag_init_feature_support_flag(struct ice_pf *pf)
1258{
1259	struct ice_hw_common_caps *caps;
1260
1261	caps = &pf->hw.dev_caps.common_cap;
1262	if (caps->roce_lag)
1263		ice_set_feature_support(pf, ICE_F_ROCE_LAG);
1264	else
1265		ice_clear_feature_support(pf, ICE_F_ROCE_LAG);
1266
1267	if (caps->sriov_lag && ice_pkg_has_lport_extract(&pf->hw))
1268		ice_set_feature_support(pf, ICE_F_SRIOV_LAG);
1269	else
1270		ice_clear_feature_support(pf, ICE_F_SRIOV_LAG);
1271}
1272
1273/**
1274 * ice_lag_changeupper_event - handle LAG changeupper event
1275 * @lag: LAG info struct
1276 * @ptr: opaque pointer data
1277 */
1278static void ice_lag_changeupper_event(struct ice_lag *lag, void *ptr)
1279{
1280	struct netdev_notifier_changeupper_info *info;
1281	struct ice_lag *primary_lag;
1282	struct net_device *netdev;
1283
1284	info = ptr;
1285	netdev = netdev_notifier_info_to_dev(ptr);
1286
1287	/* not for this netdev */
1288	if (netdev != lag->netdev)
1289		return;
1290
1291	primary_lag = ice_lag_find_primary(lag);
1292	if (info->linking) {
1293		lag->upper_netdev = info->upper_dev;
1294		/* If there is not already a primary interface in the LAG,
1295		 * then mark this one as primary.
1296		 */
1297		if (!primary_lag) {
1298			lag->primary = true;
1299			/* Configure primary's SWID to be shared */
1300			ice_lag_primary_swid(lag, true);
1301			primary_lag = lag;
1302		} else {
1303			u16 swid;
1304
1305			swid = primary_lag->pf->hw.port_info->sw_id;
1306			ice_lag_set_swid(swid, lag, true);
1307			ice_lag_add_prune_list(primary_lag, lag->pf);
1308			ice_lag_cfg_drop_fltr(lag, true);
1309		}
1310		/* add filter for primary control packets */
1311		ice_lag_cfg_cp_fltr(lag, true);
1312	} else {
1313		if (!primary_lag && lag->primary)
1314			primary_lag = lag;
1315
1316		if (!lag->primary) {
1317			ice_lag_set_swid(0, lag, false);
1318		} else {
1319			if (primary_lag && lag->primary) {
1320				ice_lag_primary_swid(lag, false);
1321				ice_lag_del_prune_list(primary_lag, lag->pf);
1322			}
1323		}
1324		/* remove filter for control packets */
1325		ice_lag_cfg_cp_fltr(lag, false);
1326	}
1327}
1328
1329/**
1330 * ice_lag_monitor_link - monitor interfaces entering/leaving the aggregate
1331 * @lag: lag info struct
1332 * @ptr: opaque data containing notifier event
1333 *
1334 * This function only operates after a primary has been set.
1335 */
1336static void ice_lag_monitor_link(struct ice_lag *lag, void *ptr)
1337{
1338	struct netdev_notifier_changeupper_info *info;
1339	struct ice_hw *prim_hw, *active_hw;
1340	struct net_device *event_netdev;
1341	struct ice_pf *pf;
1342	u8 prim_port;
1343
1344	if (!lag->primary)
1345		return;
1346
1347	event_netdev = netdev_notifier_info_to_dev(ptr);
1348	if (!netif_is_same_ice(lag->pf, event_netdev))
1349		return;
1350
1351	pf = lag->pf;
1352	prim_hw = &pf->hw;
1353	prim_port = prim_hw->port_info->lport;
1354
1355	info = (struct netdev_notifier_changeupper_info *)ptr;
1356	if (info->upper_dev != lag->upper_netdev)
1357		return;
1358
1359	if (!info->linking) {
1360		/* Since there are only two interfaces allowed in SRIOV+LAG, if
1361		 * one port is leaving, then nodes need to be on primary
1362		 * interface.
1363		 */
1364		if (prim_port != lag->active_port &&
1365		    lag->active_port != ICE_LAG_INVALID_PORT) {
1366			active_hw = ice_lag_find_hw_by_lport(lag,
1367							     lag->active_port);
1368			ice_lag_reclaim_vf_nodes(lag, active_hw);
1369			lag->active_port = ICE_LAG_INVALID_PORT;
1370		}
1371	}
1372}
1373
1374/**
1375 * ice_lag_monitor_active - main PF keep track of which port is active
1376 * @lag: lag info struct
1377 * @ptr: opaque data containing notifier event
1378 *
1379 * This function is for the primary PF to monitor changes in which port is
1380 * active and handle changes for SRIOV VF functionality
1381 */
1382static void ice_lag_monitor_active(struct ice_lag *lag, void *ptr)
1383{
1384	struct net_device *event_netdev, *event_upper;
1385	struct netdev_notifier_bonding_info *info;
1386	struct netdev_bonding_info *bonding_info;
1387	struct ice_netdev_priv *event_np;
1388	struct ice_pf *pf, *event_pf;
1389	u8 prim_port, event_port;
1390
1391	if (!lag->primary)
1392		return;
1393
1394	pf = lag->pf;
1395	if (!pf)
1396		return;
1397
1398	event_netdev = netdev_notifier_info_to_dev(ptr);
1399	rcu_read_lock();
1400	event_upper = netdev_master_upper_dev_get_rcu(event_netdev);
1401	rcu_read_unlock();
1402	if (!netif_is_ice(event_netdev) || event_upper != lag->upper_netdev)
1403		return;
1404
1405	event_np = netdev_priv(event_netdev);
1406	event_pf = event_np->vsi->back;
1407	event_port = event_pf->hw.port_info->lport;
1408	prim_port = pf->hw.port_info->lport;
1409
1410	info = (struct netdev_notifier_bonding_info *)ptr;
1411	bonding_info = &info->bonding_info;
1412
1413	if (!bonding_info->slave.state) {
1414		/* if no port is currently active, then nodes and filters exist
1415		 * on primary port, check if we need to move them
1416		 */
1417		if (lag->active_port == ICE_LAG_INVALID_PORT) {
1418			if (event_port != prim_port)
1419				ice_lag_move_vf_nodes(lag, prim_port,
1420						      event_port);
1421			lag->active_port = event_port;
1422			return;
1423		}
1424
1425		/* active port is already set and is current event port */
1426		if (lag->active_port == event_port)
1427			return;
1428		/* new active port */
1429		ice_lag_move_vf_nodes(lag, lag->active_port, event_port);
1430		lag->active_port = event_port;
1431	} else {
1432		/* port not set as currently active (e.g. new active port
1433		 * has already claimed the nodes and filters
1434		 */
1435		if (lag->active_port != event_port)
1436			return;
1437		/* This is the case when neither port is active (both link down)
1438		 * Link down on the bond - set active port to invalid and move
1439		 * nodes and filters back to primary if not already there
1440		 */
1441		if (event_port != prim_port)
1442			ice_lag_move_vf_nodes(lag, event_port, prim_port);
1443		lag->active_port = ICE_LAG_INVALID_PORT;
1444	}
1445}
1446
1447/**
1448 * ice_lag_chk_comp - evaluate bonded interface for feature support
1449 * @lag: lag info struct
1450 * @ptr: opaque data for netdev event info
1451 */
1452static bool
1453ice_lag_chk_comp(struct ice_lag *lag, void *ptr)
1454{
1455	struct net_device *event_netdev, *event_upper;
1456	struct netdev_notifier_bonding_info *info;
1457	struct netdev_bonding_info *bonding_info;
1458	struct list_head *tmp;
1459	struct device *dev;
1460	int count = 0;
1461
1462	if (!lag->primary)
1463		return true;
1464
1465	event_netdev = netdev_notifier_info_to_dev(ptr);
1466	rcu_read_lock();
1467	event_upper = netdev_master_upper_dev_get_rcu(event_netdev);
1468	rcu_read_unlock();
1469	if (event_upper != lag->upper_netdev)
1470		return true;
1471
1472	dev = ice_pf_to_dev(lag->pf);
1473
1474	/* only supporting switchdev mode for SRIOV VF LAG.
1475	 * primary interface has to be in switchdev mode
1476	 */
1477	if (!ice_is_switchdev_running(lag->pf)) {
1478		dev_info(dev, "Primary interface not in switchdev mode - VF LAG disabled\n");
1479		return false;
1480	}
1481
1482	info = (struct netdev_notifier_bonding_info *)ptr;
1483	bonding_info = &info->bonding_info;
1484	lag->bond_mode = bonding_info->master.bond_mode;
1485	if (lag->bond_mode != BOND_MODE_ACTIVEBACKUP) {
1486		dev_info(dev, "Bond Mode not ACTIVE-BACKUP - VF LAG disabled\n");
1487		return false;
1488	}
1489
1490	list_for_each(tmp, lag->netdev_head) {
1491		struct ice_dcbx_cfg *dcb_cfg, *peer_dcb_cfg;
1492		struct ice_lag_netdev_list *entry;
1493		struct ice_netdev_priv *peer_np;
1494		struct net_device *peer_netdev;
1495		struct ice_vsi *vsi, *peer_vsi;
1496		struct ice_pf *peer_pf;
1497
1498		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
1499		peer_netdev = entry->netdev;
1500		if (!netif_is_ice(peer_netdev)) {
1501			dev_info(dev, "Found %s non-ice netdev in LAG - VF LAG disabled\n",
1502				 netdev_name(peer_netdev));
1503			return false;
1504		}
1505
1506		count++;
1507		if (count > 2) {
1508			dev_info(dev, "Found more than two netdevs in LAG - VF LAG disabled\n");
1509			return false;
1510		}
1511
1512		peer_np = netdev_priv(peer_netdev);
1513		vsi = ice_get_main_vsi(lag->pf);
1514		peer_vsi = peer_np->vsi;
1515		if (lag->pf->pdev->bus != peer_vsi->back->pdev->bus ||
1516		    lag->pf->pdev->slot != peer_vsi->back->pdev->slot) {
1517			dev_info(dev, "Found %s on different device in LAG - VF LAG disabled\n",
1518				 netdev_name(peer_netdev));
1519			return false;
1520		}
1521
1522		dcb_cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
1523		peer_dcb_cfg = &peer_vsi->port_info->qos_cfg.local_dcbx_cfg;
1524		if (memcmp(dcb_cfg, peer_dcb_cfg,
1525			   sizeof(struct ice_dcbx_cfg))) {
1526			dev_info(dev, "Found %s with different DCB in LAG - VF LAG disabled\n",
1527				 netdev_name(peer_netdev));
1528			return false;
1529		}
1530
1531		peer_pf = peer_vsi->back;
1532		if (test_bit(ICE_FLAG_FW_LLDP_AGENT, peer_pf->flags)) {
1533			dev_warn(dev, "Found %s with FW LLDP agent active - VF LAG disabled\n",
1534				 netdev_name(peer_netdev));
1535			return false;
1536		}
1537	}
1538
1539	return true;
1540}
1541
1542/**
1543 * ice_lag_unregister - handle netdev unregister events
1544 * @lag: LAG info struct
1545 * @event_netdev: netdev struct for target of notifier event
1546 */
1547static void
1548ice_lag_unregister(struct ice_lag *lag, struct net_device *event_netdev)
1549{
1550	struct ice_netdev_priv *np;
1551	struct ice_pf *event_pf;
1552	struct ice_lag *p_lag;
1553
1554	p_lag = ice_lag_find_primary(lag);
1555	np = netdev_priv(event_netdev);
1556	event_pf = np->vsi->back;
1557
1558	if (p_lag) {
1559		if (p_lag->active_port != p_lag->pf->hw.port_info->lport &&
1560		    p_lag->active_port != ICE_LAG_INVALID_PORT) {
1561			struct ice_hw *active_hw;
1562
1563			active_hw = ice_lag_find_hw_by_lport(lag,
1564							     p_lag->active_port);
1565			if (active_hw)
1566				ice_lag_reclaim_vf_nodes(p_lag, active_hw);
1567			lag->active_port = ICE_LAG_INVALID_PORT;
1568		}
1569	}
1570
1571	/* primary processing for primary */
1572	if (lag->primary && lag->netdev == event_netdev)
1573		ice_lag_primary_swid(lag, false);
1574
1575	/* primary processing for secondary */
1576	if (lag->primary && lag->netdev != event_netdev)
1577		ice_lag_del_prune_list(lag, event_pf);
1578
1579	/* secondary processing for secondary */
1580	if (!lag->primary && lag->netdev == event_netdev)
1581		ice_lag_set_swid(0, lag, false);
1582}
1583
1584/**
1585 * ice_lag_monitor_rdma - set and clear rdma functionality
1586 * @lag: pointer to lag struct
1587 * @ptr: opaque data for netdev event info
1588 */
1589static void
1590ice_lag_monitor_rdma(struct ice_lag *lag, void *ptr)
1591{
1592	struct netdev_notifier_changeupper_info *info;
1593	struct net_device *netdev;
1594
1595	info = ptr;
1596	netdev = netdev_notifier_info_to_dev(ptr);
1597
1598	if (netdev != lag->netdev)
1599		return;
1600
1601	if (info->linking)
1602		ice_clear_rdma_cap(lag->pf);
1603	else
1604		ice_set_rdma_cap(lag->pf);
1605}
1606
1607/**
1608 * ice_lag_chk_disabled_bond - monitor interfaces entering/leaving disabled bond
1609 * @lag: lag info struct
1610 * @ptr: opaque data containing event
1611 *
1612 * as interfaces enter a bond - determine if the bond is currently
1613 * SRIOV LAG compliant and flag if not.  As interfaces leave the
1614 * bond, reset their compliant status.
1615 */
1616static void ice_lag_chk_disabled_bond(struct ice_lag *lag, void *ptr)
1617{
1618	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1619	struct netdev_notifier_changeupper_info *info = ptr;
1620	struct ice_lag *prim_lag;
1621
1622	if (netdev != lag->netdev)
1623		return;
1624
1625	if (info->linking) {
1626		prim_lag = ice_lag_find_primary(lag);
1627		if (prim_lag &&
1628		    !ice_is_feature_supported(prim_lag->pf, ICE_F_SRIOV_LAG)) {
1629			ice_clear_feature_support(lag->pf, ICE_F_SRIOV_LAG);
1630			netdev_info(netdev, "Interface added to non-compliant SRIOV LAG aggregate\n");
1631		}
1632	} else {
1633		ice_lag_init_feature_support_flag(lag->pf);
1634	}
1635}
1636
1637/**
1638 * ice_lag_disable_sriov_bond - set members of bond as not supporting SRIOV LAG
1639 * @lag: primary interfaces lag struct
1640 */
1641static void ice_lag_disable_sriov_bond(struct ice_lag *lag)
1642{
1643	struct ice_netdev_priv *np;
1644	struct ice_pf *pf;
1645
1646	np = netdev_priv(lag->netdev);
1647	pf = np->vsi->back;
1648	ice_clear_feature_support(pf, ICE_F_SRIOV_LAG);
1649}
1650
1651/**
1652 * ice_lag_process_event - process a task assigned to the lag_wq
1653 * @work: pointer to work_struct
1654 */
1655static void ice_lag_process_event(struct work_struct *work)
1656{
1657	struct netdev_notifier_changeupper_info *info;
1658	struct ice_lag_work *lag_work;
1659	struct net_device *netdev;
1660	struct list_head *tmp, *n;
1661	struct ice_pf *pf;
1662
1663	lag_work = container_of(work, struct ice_lag_work, lag_task);
1664	pf = lag_work->lag->pf;
1665
1666	mutex_lock(&pf->lag_mutex);
1667	lag_work->lag->netdev_head = &lag_work->netdev_list.node;
1668
1669	switch (lag_work->event) {
1670	case NETDEV_CHANGEUPPER:
1671		info = &lag_work->info.changeupper_info;
1672		ice_lag_chk_disabled_bond(lag_work->lag, info);
1673		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1674			ice_lag_monitor_link(lag_work->lag, info);
1675			ice_lag_changeupper_event(lag_work->lag, info);
1676			ice_lag_link_unlink(lag_work->lag, info);
1677		}
1678		ice_lag_monitor_rdma(lag_work->lag, info);
1679		break;
1680	case NETDEV_BONDING_INFO:
1681		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1682			if (!ice_lag_chk_comp(lag_work->lag,
1683					      &lag_work->info.bonding_info)) {
1684				netdev = lag_work->info.bonding_info.info.dev;
1685				ice_lag_disable_sriov_bond(lag_work->lag);
1686				ice_lag_unregister(lag_work->lag, netdev);
1687				goto lag_cleanup;
1688			}
1689			ice_lag_monitor_active(lag_work->lag,
1690					       &lag_work->info.bonding_info);
1691			ice_lag_cfg_pf_fltrs(lag_work->lag,
1692					     &lag_work->info.bonding_info);
1693		}
1694		ice_lag_info_event(lag_work->lag, &lag_work->info.bonding_info);
1695		break;
1696	case NETDEV_UNREGISTER:
1697		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1698			netdev = lag_work->info.bonding_info.info.dev;
1699			if ((netdev == lag_work->lag->netdev ||
1700			     lag_work->lag->primary) && lag_work->lag->bonded)
1701				ice_lag_unregister(lag_work->lag, netdev);
1702		}
1703		break;
1704	default:
1705		break;
1706	}
1707
1708lag_cleanup:
1709	/* cleanup resources allocated for this work item */
1710	list_for_each_safe(tmp, n, &lag_work->netdev_list.node) {
1711		struct ice_lag_netdev_list *entry;
1712
1713		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
1714		list_del(&entry->node);
1715		kfree(entry);
1716	}
1717	lag_work->lag->netdev_head = NULL;
1718
1719	mutex_unlock(&pf->lag_mutex);
1720
1721	kfree(lag_work);
1722}
1723
1724/**
1725 * ice_lag_event_handler - handle LAG events from netdev
1726 * @notif_blk: notifier block registered by this netdev
1727 * @event: event type
1728 * @ptr: opaque data containing notifier event
1729 */
1730static int
1731ice_lag_event_handler(struct notifier_block *notif_blk, unsigned long event,
1732		      void *ptr)
1733{
1734	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1735	struct net_device *upper_netdev;
1736	struct ice_lag_work *lag_work;
1737	struct ice_lag *lag;
1738
1739	if (!netif_is_ice(netdev))
1740		return NOTIFY_DONE;
1741
1742	if (event != NETDEV_CHANGEUPPER && event != NETDEV_BONDING_INFO &&
1743	    event != NETDEV_UNREGISTER)
1744		return NOTIFY_DONE;
1745
1746	if (!(netdev->priv_flags & IFF_BONDING))
1747		return NOTIFY_DONE;
1748
1749	lag = container_of(notif_blk, struct ice_lag, notif_block);
1750	if (!lag->netdev)
1751		return NOTIFY_DONE;
1752
1753	if (!net_eq(dev_net(netdev), &init_net))
1754		return NOTIFY_DONE;
1755
1756	/* This memory will be freed at the end of ice_lag_process_event */
1757	lag_work = kzalloc(sizeof(*lag_work), GFP_KERNEL);
1758	if (!lag_work)
1759		return -ENOMEM;
1760
1761	lag_work->event_netdev = netdev;
1762	lag_work->lag = lag;
1763	lag_work->event = event;
1764	if (event == NETDEV_CHANGEUPPER) {
1765		struct netdev_notifier_changeupper_info *info;
1766
1767		info = ptr;
1768		upper_netdev = info->upper_dev;
1769	} else {
1770		upper_netdev = netdev_master_upper_dev_get(netdev);
1771	}
1772
1773	INIT_LIST_HEAD(&lag_work->netdev_list.node);
1774	if (upper_netdev) {
1775		struct ice_lag_netdev_list *nd_list;
1776		struct net_device *tmp_nd;
1777
1778		rcu_read_lock();
1779		for_each_netdev_in_bond_rcu(upper_netdev, tmp_nd) {
1780			nd_list = kzalloc(sizeof(*nd_list), GFP_ATOMIC);
1781			if (!nd_list)
1782				break;
1783
1784			nd_list->netdev = tmp_nd;
1785			list_add(&nd_list->node, &lag_work->netdev_list.node);
1786		}
1787		rcu_read_unlock();
1788	}
1789
1790	switch (event) {
1791	case NETDEV_CHANGEUPPER:
1792		lag_work->info.changeupper_info =
1793			*((struct netdev_notifier_changeupper_info *)ptr);
1794		break;
1795	case NETDEV_BONDING_INFO:
1796		lag_work->info.bonding_info =
1797			*((struct netdev_notifier_bonding_info *)ptr);
1798		break;
1799	default:
1800		lag_work->info.notifier_info =
1801			*((struct netdev_notifier_info *)ptr);
1802		break;
1803	}
1804
1805	INIT_WORK(&lag_work->lag_task, ice_lag_process_event);
1806	queue_work(ice_lag_wq, &lag_work->lag_task);
1807
1808	return NOTIFY_DONE;
1809}
1810
1811/**
1812 * ice_register_lag_handler - register LAG handler on netdev
1813 * @lag: LAG struct
1814 */
1815static int ice_register_lag_handler(struct ice_lag *lag)
1816{
1817	struct device *dev = ice_pf_to_dev(lag->pf);
1818	struct notifier_block *notif_blk;
1819
1820	notif_blk = &lag->notif_block;
1821
1822	if (!notif_blk->notifier_call) {
1823		notif_blk->notifier_call = ice_lag_event_handler;
1824		if (register_netdevice_notifier(notif_blk)) {
1825			notif_blk->notifier_call = NULL;
1826			dev_err(dev, "FAIL register LAG event handler!\n");
1827			return -EINVAL;
1828		}
1829		dev_dbg(dev, "LAG event handler registered\n");
1830	}
1831	return 0;
1832}
1833
1834/**
1835 * ice_unregister_lag_handler - unregister LAG handler on netdev
1836 * @lag: LAG struct
1837 */
1838static void ice_unregister_lag_handler(struct ice_lag *lag)
1839{
1840	struct device *dev = ice_pf_to_dev(lag->pf);
1841	struct notifier_block *notif_blk;
1842
1843	notif_blk = &lag->notif_block;
1844	if (notif_blk->notifier_call) {
1845		unregister_netdevice_notifier(notif_blk);
1846		dev_dbg(dev, "LAG event handler unregistered\n");
1847	}
1848}
1849
1850/**
1851 * ice_create_lag_recipe
1852 * @hw: pointer to HW struct
1853 * @rid: pointer to u16 to pass back recipe index
1854 * @base_recipe: recipe to base the new recipe on
1855 * @prio: priority for new recipe
1856 *
1857 * function returns 0 on error
1858 */
1859static int ice_create_lag_recipe(struct ice_hw *hw, u16 *rid,
1860				 const u8 *base_recipe, u8 prio)
1861{
1862	struct ice_aqc_recipe_data_elem *new_rcp;
1863	int err;
1864
1865	err = ice_alloc_recipe(hw, rid);
1866	if (err)
1867		return err;
1868
1869	new_rcp = kzalloc(ICE_RECIPE_LEN * ICE_MAX_NUM_RECIPES, GFP_KERNEL);
1870	if (!new_rcp)
1871		return -ENOMEM;
1872
1873	memcpy(new_rcp, base_recipe, ICE_RECIPE_LEN);
1874	new_rcp->content.act_ctrl_fwd_priority = prio;
1875	new_rcp->content.rid = *rid | ICE_AQ_RECIPE_ID_IS_ROOT;
1876	new_rcp->recipe_indx = *rid;
1877	bitmap_zero((unsigned long *)new_rcp->recipe_bitmap,
1878		    ICE_MAX_NUM_RECIPES);
1879	set_bit(*rid, (unsigned long *)new_rcp->recipe_bitmap);
1880
1881	err = ice_aq_add_recipe(hw, new_rcp, 1, NULL);
1882	if (err)
1883		*rid = 0;
1884
1885	kfree(new_rcp);
1886	return err;
1887}
1888
1889/**
1890 * ice_lag_move_vf_nodes_tc_sync - move a VF's nodes for a tc during reset
1891 * @lag: primary interfaces lag struct
1892 * @dest_hw: HW struct for destination's interface
1893 * @vsi_num: VSI index in PF space
1894 * @tc: traffic class to move
1895 */
1896static void
1897ice_lag_move_vf_nodes_tc_sync(struct ice_lag *lag, struct ice_hw *dest_hw,
1898			      u16 vsi_num, u8 tc)
1899{
1900	DEFINE_RAW_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
1901	struct device *dev = ice_pf_to_dev(lag->pf);
1902	u16 numq, valq, num_moved, qbuf_size;
1903	u16 buf_size = __struct_size(buf);
1904	struct ice_aqc_cfg_txqs_buf *qbuf;
1905	struct ice_sched_node *n_prt;
1906	__le32 teid, parent_teid;
1907	struct ice_vsi_ctx *ctx;
1908	struct ice_hw *hw;
1909	u32 tmp_teid;
1910
1911	hw = &lag->pf->hw;
1912	ctx = ice_get_vsi_ctx(hw, vsi_num);
1913	if (!ctx) {
1914		dev_warn(dev, "LAG rebuild failed after reset due to VSI Context failure\n");
1915		return;
1916	}
1917
1918	if (!ctx->sched.vsi_node[tc])
1919		return;
1920
1921	numq = ctx->num_lan_q_entries[tc];
1922	teid = ctx->sched.vsi_node[tc]->info.node_teid;
1923	tmp_teid = le32_to_cpu(teid);
1924	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
1925
1926	if (!tmp_teid || !numq)
1927		return;
1928
1929	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, true))
1930		dev_dbg(dev, "Problem suspending traffic during reset rebuild\n");
1931
1932	/* reconfig queues for new port */
1933	qbuf_size = struct_size(qbuf, queue_info, numq);
1934	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
1935	if (!qbuf) {
1936		dev_warn(dev, "Failure allocating VF queue recfg buffer for reset rebuild\n");
1937		goto resume_sync;
1938	}
1939
1940	/* add the per queue info for the reconfigure command buffer */
1941	valq = ice_lag_qbuf_recfg(hw, qbuf, vsi_num, numq, tc);
1942	if (!valq) {
1943		dev_warn(dev, "Failure to reconfig queues for LAG reset rebuild\n");
1944		goto sync_none;
1945	}
1946
1947	if (ice_aq_cfg_lan_txq(hw, qbuf, qbuf_size, numq, hw->port_info->lport,
1948			       dest_hw->port_info->lport, NULL)) {
1949		dev_warn(dev, "Failure to configure queues for LAG reset rebuild\n");
1950		goto sync_qerr;
1951	}
1952
1953sync_none:
1954	kfree(qbuf);
1955
1956	/* find parent in destination tree */
1957	n_prt = ice_lag_get_sched_parent(dest_hw, tc);
1958	if (!n_prt)
1959		goto resume_sync;
1960
1961	/* Move node to new parent */
1962	buf->hdr.src_parent_teid = parent_teid;
1963	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
1964	buf->hdr.num_elems = cpu_to_le16(1);
1965	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
1966	buf->teid[0] = teid;
1967
1968	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
1969		dev_warn(dev, "Failure to move VF nodes for LAG reset rebuild\n");
1970	else
1971		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
1972
1973	goto resume_sync;
1974
1975sync_qerr:
1976	kfree(qbuf);
1977
1978resume_sync:
1979	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, false))
1980		dev_warn(dev, "Problem restarting traffic for LAG node reset rebuild\n");
1981}
1982
1983/**
1984 * ice_lag_move_vf_nodes_sync - move vf nodes to active interface
1985 * @lag: primary interfaces lag struct
1986 * @dest_hw: lport value for currently active port
1987 *
1988 * This function is used in a reset context, outside of event handling,
1989 * to move the VF nodes to the secondary interface when that interface
1990 * is the active interface during a reset rebuild
1991 */
1992static void
1993ice_lag_move_vf_nodes_sync(struct ice_lag *lag, struct ice_hw *dest_hw)
1994{
1995	struct ice_pf *pf;
1996	int i, tc;
1997
1998	if (!lag->primary || !dest_hw)
1999		return;
2000
2001	pf = lag->pf;
2002	ice_for_each_vsi(pf, i)
2003		if (pf->vsi[i] && pf->vsi[i]->type == ICE_VSI_VF)
 
2004			ice_for_each_traffic_class(tc)
2005				ice_lag_move_vf_nodes_tc_sync(lag, dest_hw, i,
2006							      tc);
2007}
2008
2009/**
2010 * ice_init_lag - initialize support for LAG
2011 * @pf: PF struct
2012 *
2013 * Alloc memory for LAG structs and initialize the elements.
2014 * Memory will be freed in ice_deinit_lag
2015 */
2016int ice_init_lag(struct ice_pf *pf)
2017{
2018	struct device *dev = ice_pf_to_dev(pf);
2019	struct ice_lag *lag;
2020	struct ice_vsi *vsi;
2021	u64 recipe_bits = 0;
2022	int n, err;
2023
2024	ice_lag_init_feature_support_flag(pf);
2025	if (!ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
2026		return 0;
2027
2028	pf->lag = kzalloc(sizeof(*lag), GFP_KERNEL);
2029	if (!pf->lag)
2030		return -ENOMEM;
2031	lag = pf->lag;
2032
2033	vsi = ice_get_main_vsi(pf);
2034	if (!vsi) {
2035		dev_err(dev, "couldn't get main vsi, link aggregation init fail\n");
2036		err = -EIO;
2037		goto lag_error;
2038	}
2039
2040	lag->pf = pf;
2041	lag->netdev = vsi->netdev;
2042	lag->role = ICE_LAG_NONE;
2043	lag->active_port = ICE_LAG_INVALID_PORT;
2044	lag->bonded = false;
2045	lag->upper_netdev = NULL;
2046	lag->notif_block.notifier_call = NULL;
2047
2048	err = ice_register_lag_handler(lag);
2049	if (err) {
2050		dev_warn(dev, "INIT LAG: Failed to register event handler\n");
2051		goto lag_error;
2052	}
2053
2054	err = ice_create_lag_recipe(&pf->hw, &lag->pf_recipe,
2055				    ice_dflt_vsi_rcp, 1);
2056	if (err)
2057		goto lag_error;
2058
2059	err = ice_create_lag_recipe(&pf->hw, &lag->lport_recipe,
2060				    ice_lport_rcp, 3);
2061	if (err)
2062		goto free_rcp_res;
2063
2064	/* associate recipes to profiles */
2065	for (n = 0; n < ICE_PROFID_IPV6_GTPU_IPV6_TCP_INNER; n++) {
2066		err = ice_aq_get_recipe_to_profile(&pf->hw, n,
2067						   &recipe_bits, NULL);
2068		if (err)
2069			continue;
2070
2071		if (recipe_bits & BIT(ICE_SW_LKUP_DFLT)) {
2072			recipe_bits |= BIT(lag->pf_recipe) |
2073				       BIT(lag->lport_recipe);
2074			ice_aq_map_recipe_to_profile(&pf->hw, n,
2075						     recipe_bits, NULL);
2076		}
2077	}
2078
2079	ice_display_lag_info(lag);
2080
2081	dev_dbg(dev, "INIT LAG complete\n");
2082	return 0;
2083
2084free_rcp_res:
2085	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2086			&pf->lag->pf_recipe);
2087lag_error:
2088	kfree(lag);
2089	pf->lag = NULL;
2090	return err;
2091}
2092
2093/**
2094 * ice_deinit_lag - Clean up LAG
2095 * @pf: PF struct
2096 *
2097 * Clean up kernel LAG info and free memory
2098 * This function is meant to only be called on driver remove/shutdown
2099 */
2100void ice_deinit_lag(struct ice_pf *pf)
2101{
2102	struct ice_lag *lag;
2103
2104	lag = pf->lag;
2105
2106	if (!lag)
2107		return;
2108
2109	if (lag->pf)
2110		ice_unregister_lag_handler(lag);
2111
2112	flush_workqueue(ice_lag_wq);
2113
2114	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2115			&pf->lag->pf_recipe);
2116	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2117			&pf->lag->lport_recipe);
2118
2119	kfree(lag);
2120
2121	pf->lag = NULL;
2122}
2123
2124/**
2125 * ice_lag_rebuild - rebuild lag resources after reset
2126 * @pf: pointer to local pf struct
2127 *
2128 * PF resets are promoted to CORER resets when interface in an aggregate.  This
2129 * means that we need to rebuild the PF resources for the interface.  Since
2130 * this will happen outside the normal event processing, need to acquire the lag
2131 * lock.
2132 *
2133 * This function will also evaluate the VF resources if this is the primary
2134 * interface.
2135 */
2136void ice_lag_rebuild(struct ice_pf *pf)
2137{
2138	struct ice_lag_netdev_list ndlist;
2139	struct ice_lag *lag, *prim_lag;
2140	u8 act_port, loc_port;
2141
2142	if (!pf->lag || !pf->lag->bonded)
2143		return;
2144
2145	mutex_lock(&pf->lag_mutex);
2146
2147	lag = pf->lag;
2148	if (lag->primary) {
2149		prim_lag = lag;
2150	} else {
2151		ice_lag_build_netdev_list(lag, &ndlist);
2152		prim_lag = ice_lag_find_primary(lag);
2153	}
2154
2155	if (!prim_lag) {
2156		dev_dbg(ice_pf_to_dev(pf), "No primary interface in aggregate, can't rebuild\n");
2157		goto lag_rebuild_out;
2158	}
2159
2160	act_port = prim_lag->active_port;
2161	loc_port = lag->pf->hw.port_info->lport;
2162
2163	/* configure SWID for this port */
2164	if (lag->primary) {
2165		ice_lag_primary_swid(lag, true);
2166	} else {
2167		ice_lag_set_swid(prim_lag->pf->hw.port_info->sw_id, lag, true);
2168		ice_lag_add_prune_list(prim_lag, pf);
2169		if (act_port == loc_port)
2170			ice_lag_move_vf_nodes_sync(prim_lag, &pf->hw);
2171	}
2172
2173	ice_lag_cfg_cp_fltr(lag, true);
2174
2175	if (lag->pf_rx_rule_id)
2176		if (ice_lag_cfg_dflt_fltr(lag, true))
2177			dev_err(ice_pf_to_dev(pf), "Error adding default VSI rule in rebuild\n");
2178
2179	ice_clear_rdma_cap(pf);
2180lag_rebuild_out:
2181	ice_lag_destroy_netdev_list(lag, &ndlist);
2182	mutex_unlock(&pf->lag_mutex);
2183}
2184
2185/**
2186 * ice_lag_is_switchdev_running
2187 * @pf: pointer to PF structure
2188 *
2189 * Check if switchdev is running on any of the interfaces connected to lag.
2190 */
2191bool ice_lag_is_switchdev_running(struct ice_pf *pf)
2192{
2193	struct ice_lag *lag = pf->lag;
2194	struct net_device *tmp_nd;
2195
2196	if (!ice_is_feature_supported(pf, ICE_F_SRIOV_LAG) || !lag)
2197		return false;
2198
2199	rcu_read_lock();
2200	for_each_netdev_in_bond_rcu(lag->upper_netdev, tmp_nd) {
2201		struct ice_netdev_priv *priv = netdev_priv(tmp_nd);
2202
2203		if (!netif_is_ice(tmp_nd) || !priv || !priv->vsi ||
2204		    !priv->vsi->back)
2205			continue;
2206
2207		if (ice_is_switchdev_running(priv->vsi->back)) {
2208			rcu_read_unlock();
2209			return true;
2210		}
2211	}
2212	rcu_read_unlock();
2213
2214	return false;
2215}
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (C) 2018-2021, Intel Corporation. */
   3
   4/* Link Aggregation code */
   5
   6#include "ice.h"
   7#include "ice_lib.h"
   8#include "ice_lag.h"
   9
  10#define ICE_LAG_RES_SHARED	BIT(14)
  11#define ICE_LAG_RES_VALID	BIT(15)
  12
  13#define LACP_TRAIN_PKT_LEN		16
  14static const u8 lacp_train_pkt[LACP_TRAIN_PKT_LEN] = { 0, 0, 0, 0, 0, 0,
  15						       0, 0, 0, 0, 0, 0,
  16						       0x88, 0x09, 0, 0 };
  17
  18#define ICE_RECIPE_LEN			64
  19static const u8 ice_dflt_vsi_rcp[ICE_RECIPE_LEN] = {
  20	0x05, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21	0x85, 0, 0x01, 0, 0, 0, 0xff, 0xff, 0x08, 0, 0, 0, 0, 0, 0, 0,
  22	0, 0, 0, 0, 0, 0, 0x30 };
  23static const u8 ice_lport_rcp[ICE_RECIPE_LEN] = {
  24	0x05, 0, 0, 0, 0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25	0x85, 0, 0x16, 0, 0, 0, 0xff, 0xff, 0x07, 0, 0, 0, 0, 0, 0, 0,
  26	0, 0, 0, 0, 0, 0, 0x30 };
  27
  28/**
  29 * ice_lag_set_primary - set PF LAG state as Primary
  30 * @lag: LAG info struct
  31 */
  32static void ice_lag_set_primary(struct ice_lag *lag)
  33{
  34	struct ice_pf *pf = lag->pf;
  35
  36	if (!pf)
  37		return;
  38
  39	if (lag->role != ICE_LAG_UNSET && lag->role != ICE_LAG_BACKUP) {
  40		dev_warn(ice_pf_to_dev(pf), "%s: Attempt to be Primary, but incompatible state.\n",
  41			 netdev_name(lag->netdev));
  42		return;
  43	}
  44
  45	lag->role = ICE_LAG_PRIMARY;
  46}
  47
  48/**
  49 * ice_lag_set_backup - set PF LAG state to Backup
  50 * @lag: LAG info struct
  51 */
  52static void ice_lag_set_backup(struct ice_lag *lag)
  53{
  54	struct ice_pf *pf = lag->pf;
  55
  56	if (!pf)
  57		return;
  58
  59	if (lag->role != ICE_LAG_UNSET && lag->role != ICE_LAG_PRIMARY) {
  60		dev_dbg(ice_pf_to_dev(pf), "%s: Attempt to be Backup, but incompatible state\n",
  61			netdev_name(lag->netdev));
  62		return;
  63	}
  64
  65	lag->role = ICE_LAG_BACKUP;
  66}
  67
  68/**
  69 * netif_is_same_ice - determine if netdev is on the same ice NIC as local PF
  70 * @pf: local PF struct
  71 * @netdev: netdev we are evaluating
  72 */
  73static bool netif_is_same_ice(struct ice_pf *pf, struct net_device *netdev)
  74{
  75	struct ice_netdev_priv *np;
  76	struct ice_pf *test_pf;
  77	struct ice_vsi *vsi;
  78
  79	if (!netif_is_ice(netdev))
  80		return false;
  81
  82	np = netdev_priv(netdev);
  83	if (!np)
  84		return false;
  85
  86	vsi = np->vsi;
  87	if (!vsi)
  88		return false;
  89
  90	test_pf = vsi->back;
  91	if (!test_pf)
  92		return false;
  93
  94	if (pf->pdev->bus != test_pf->pdev->bus ||
  95	    pf->pdev->slot != test_pf->pdev->slot)
  96		return false;
  97
  98	return true;
  99}
 100
 101/**
 102 * ice_netdev_to_lag - return pointer to associated lag struct from netdev
 103 * @netdev: pointer to net_device struct to query
 104 */
 105static struct ice_lag *ice_netdev_to_lag(struct net_device *netdev)
 106{
 107	struct ice_netdev_priv *np;
 108	struct ice_vsi *vsi;
 109
 110	if (!netif_is_ice(netdev))
 111		return NULL;
 112
 113	np = netdev_priv(netdev);
 114	if (!np)
 115		return NULL;
 116
 117	vsi = np->vsi;
 118	if (!vsi)
 119		return NULL;
 120
 121	return vsi->back->lag;
 122}
 123
 124/**
 125 * ice_lag_find_hw_by_lport - return an hw struct from bond members lport
 126 * @lag: lag struct
 127 * @lport: lport value to search for
 128 */
 129static struct ice_hw *
 130ice_lag_find_hw_by_lport(struct ice_lag *lag, u8 lport)
 131{
 132	struct ice_lag_netdev_list *entry;
 133	struct net_device *tmp_netdev;
 134	struct ice_netdev_priv *np;
 135	struct ice_hw *hw;
 136
 137	list_for_each_entry(entry, lag->netdev_head, node) {
 138		tmp_netdev = entry->netdev;
 139		if (!tmp_netdev || !netif_is_ice(tmp_netdev))
 140			continue;
 141
 142		np = netdev_priv(tmp_netdev);
 143		if (!np || !np->vsi)
 144			continue;
 145
 146		hw = &np->vsi->back->hw;
 147		if (hw->port_info->lport == lport)
 148			return hw;
 149	}
 150
 151	return NULL;
 152}
 153
 154/**
 155 * ice_pkg_has_lport_extract - check if lport extraction supported
 156 * @hw: HW struct
 157 */
 158static bool ice_pkg_has_lport_extract(struct ice_hw *hw)
 159{
 160	int i;
 161
 162	for (i = 0; i < hw->blk[ICE_BLK_SW].es.count; i++) {
 163		u16 offset;
 164		u8 fv_prot;
 165
 166		ice_find_prot_off(hw, ICE_BLK_SW, ICE_SW_DEFAULT_PROFILE, i,
 167				  &fv_prot, &offset);
 168		if (fv_prot == ICE_FV_PROT_MDID &&
 169		    offset == ICE_LP_EXT_BUF_OFFSET)
 170			return true;
 171	}
 172	return false;
 173}
 174
 175/**
 176 * ice_lag_find_primary - returns pointer to primary interfaces lag struct
 177 * @lag: local interfaces lag struct
 178 */
 179static struct ice_lag *ice_lag_find_primary(struct ice_lag *lag)
 180{
 181	struct ice_lag *primary_lag = NULL;
 182	struct list_head *tmp;
 183
 184	list_for_each(tmp, lag->netdev_head) {
 185		struct ice_lag_netdev_list *entry;
 186		struct ice_lag *tmp_lag;
 187
 188		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
 189		tmp_lag = ice_netdev_to_lag(entry->netdev);
 190		if (tmp_lag && tmp_lag->primary) {
 191			primary_lag = tmp_lag;
 192			break;
 193		}
 194	}
 195
 196	return primary_lag;
 197}
 198
 199/**
 200 * ice_lag_cfg_fltr - Add/Remove rule for LAG
 201 * @lag: lag struct for local interface
 202 * @act: rule action
 203 * @recipe_id: recipe id for the new rule
 204 * @rule_idx: pointer to rule index
 
 205 * @add: boolean on whether we are adding filters
 206 */
 207static int
 208ice_lag_cfg_fltr(struct ice_lag *lag, u32 act, u16 recipe_id, u16 *rule_idx,
 209		 bool add)
 210{
 211	struct ice_sw_rule_lkup_rx_tx *s_rule;
 212	u16 s_rule_sz, vsi_num;
 213	struct ice_hw *hw;
 214	u8 *eth_hdr;
 215	u32 opc;
 216	int err;
 217
 218	hw = &lag->pf->hw;
 219	vsi_num = ice_get_hw_vsi_num(hw, 0);
 220
 221	s_rule_sz = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE(s_rule);
 222	s_rule = kzalloc(s_rule_sz, GFP_KERNEL);
 223	if (!s_rule) {
 224		dev_err(ice_pf_to_dev(lag->pf), "error allocating rule for LAG\n");
 225		return -ENOMEM;
 226	}
 227
 228	if (add) {
 229		eth_hdr = s_rule->hdr_data;
 230		ice_fill_eth_hdr(eth_hdr);
 231
 232		act |= FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M, vsi_num);
 233
 234		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
 235		s_rule->recipe_id = cpu_to_le16(recipe_id);
 236		s_rule->src = cpu_to_le16(hw->port_info->lport);
 
 
 
 
 
 
 
 
 237		s_rule->act = cpu_to_le32(act);
 238		s_rule->hdr_len = cpu_to_le16(DUMMY_ETH_HDR_LEN);
 239		opc = ice_aqc_opc_add_sw_rules;
 240	} else {
 241		s_rule->index = cpu_to_le16(*rule_idx);
 242		opc = ice_aqc_opc_remove_sw_rules;
 243	}
 244
 245	err = ice_aq_sw_rules(&lag->pf->hw, s_rule, s_rule_sz, 1, opc, NULL);
 246	if (err)
 247		goto dflt_fltr_free;
 248
 249	if (add)
 250		*rule_idx = le16_to_cpu(s_rule->index);
 251	else
 252		*rule_idx = 0;
 253
 254dflt_fltr_free:
 255	kfree(s_rule);
 256	return err;
 257}
 258
 259/**
 260 * ice_lag_cfg_dflt_fltr - Add/Remove default VSI rule for LAG
 261 * @lag: lag struct for local interface
 262 * @add: boolean on whether to add filter
 263 */
 264static int
 265ice_lag_cfg_dflt_fltr(struct ice_lag *lag, bool add)
 266{
 267	u32 act = ICE_SINGLE_ACT_VSI_FORWARDING |
 268		ICE_SINGLE_ACT_VALID_BIT | ICE_SINGLE_ACT_LAN_ENABLE;
 
 
 
 
 
 
 269
 270	return ice_lag_cfg_fltr(lag, act, lag->pf_recipe,
 271				&lag->pf_rule_id, add);
 
 
 
 
 
 
 
 
 
 
 
 
 272}
 273
 274/**
 275 * ice_lag_cfg_drop_fltr - Add/Remove lport drop rule
 276 * @lag: lag struct for local interface
 277 * @add: boolean on whether to add filter
 278 */
 279static int
 280ice_lag_cfg_drop_fltr(struct ice_lag *lag, bool add)
 281{
 282	u32 act = ICE_SINGLE_ACT_VSI_FORWARDING |
 283		  ICE_SINGLE_ACT_VALID_BIT |
 284		  ICE_SINGLE_ACT_DROP;
 285
 286	return ice_lag_cfg_fltr(lag, act, lag->lport_recipe,
 287				&lag->lport_rule_idx, add);
 288}
 289
 290/**
 291 * ice_lag_cfg_pf_fltrs - set filters up for new active port
 292 * @lag: local interfaces lag struct
 293 * @ptr: opaque data containing notifier event
 294 */
 295static void
 296ice_lag_cfg_pf_fltrs(struct ice_lag *lag, void *ptr)
 297{
 298	struct netdev_notifier_bonding_info *info;
 299	struct netdev_bonding_info *bonding_info;
 300	struct net_device *event_netdev;
 301	struct device *dev;
 302
 303	event_netdev = netdev_notifier_info_to_dev(ptr);
 304	/* not for this netdev */
 305	if (event_netdev != lag->netdev)
 306		return;
 307
 308	info = (struct netdev_notifier_bonding_info *)ptr;
 309	bonding_info = &info->bonding_info;
 310	dev = ice_pf_to_dev(lag->pf);
 311
 312	/* interface not active - remove old default VSI rule */
 313	if (bonding_info->slave.state && lag->pf_rule_id) {
 314		if (ice_lag_cfg_dflt_fltr(lag, false))
 315			dev_err(dev, "Error removing old default VSI filter\n");
 316		if (ice_lag_cfg_drop_fltr(lag, true))
 317			dev_err(dev, "Error adding new drop filter\n");
 318		return;
 319	}
 320
 321	/* interface becoming active - add new default VSI rule */
 322	if (!bonding_info->slave.state && !lag->pf_rule_id) {
 323		if (ice_lag_cfg_dflt_fltr(lag, true))
 324			dev_err(dev, "Error adding new default VSI filter\n");
 325		if (lag->lport_rule_idx && ice_lag_cfg_drop_fltr(lag, false))
 326			dev_err(dev, "Error removing old drop filter\n");
 327	}
 328}
 329
 330/**
 331 * ice_display_lag_info - print LAG info
 332 * @lag: LAG info struct
 333 */
 334static void ice_display_lag_info(struct ice_lag *lag)
 335{
 336	const char *name, *upper, *role, *bonded, *primary;
 337	struct device *dev = &lag->pf->pdev->dev;
 338
 339	name = lag->netdev ? netdev_name(lag->netdev) : "unset";
 340	upper = lag->upper_netdev ? netdev_name(lag->upper_netdev) : "unset";
 341	primary = lag->primary ? "TRUE" : "FALSE";
 342	bonded = lag->bonded ? "BONDED" : "UNBONDED";
 343
 344	switch (lag->role) {
 345	case ICE_LAG_NONE:
 346		role = "NONE";
 347		break;
 348	case ICE_LAG_PRIMARY:
 349		role = "PRIMARY";
 350		break;
 351	case ICE_LAG_BACKUP:
 352		role = "BACKUP";
 353		break;
 354	case ICE_LAG_UNSET:
 355		role = "UNSET";
 356		break;
 357	default:
 358		role = "ERROR";
 359	}
 360
 361	dev_dbg(dev, "%s %s, upper:%s, role:%s, primary:%s\n", name, bonded,
 362		upper, role, primary);
 363}
 364
 365/**
 366 * ice_lag_qbuf_recfg - generate a buffer of queues for a reconfigure command
 367 * @hw: HW struct that contains the queue contexts
 368 * @qbuf: pointer to buffer to populate
 369 * @vsi_num: index of the VSI in PF space
 370 * @numq: number of queues to search for
 371 * @tc: traffic class that contains the queues
 372 *
 373 * function returns the number of valid queues in buffer
 374 */
 375static u16
 376ice_lag_qbuf_recfg(struct ice_hw *hw, struct ice_aqc_cfg_txqs_buf *qbuf,
 377		   u16 vsi_num, u16 numq, u8 tc)
 378{
 379	struct ice_q_ctx *q_ctx;
 380	u16 qid, count = 0;
 381	struct ice_pf *pf;
 382	int i;
 383
 384	pf = hw->back;
 385	for (i = 0; i < numq; i++) {
 386		q_ctx = ice_get_lan_q_ctx(hw, vsi_num, tc, i);
 387		if (!q_ctx) {
 388			dev_dbg(ice_hw_to_dev(hw), "%s queue %d NO Q CONTEXT\n",
 389				__func__, i);
 390			continue;
 391		}
 392		if (q_ctx->q_teid == ICE_INVAL_TEID) {
 393			dev_dbg(ice_hw_to_dev(hw), "%s queue %d INVAL TEID\n",
 394				__func__, i);
 395			continue;
 396		}
 397		if (q_ctx->q_handle == ICE_INVAL_Q_HANDLE) {
 398			dev_dbg(ice_hw_to_dev(hw), "%s queue %d INVAL Q HANDLE\n",
 399				__func__, i);
 400			continue;
 401		}
 402
 403		qid = pf->vsi[vsi_num]->txq_map[q_ctx->q_handle];
 404		qbuf->queue_info[count].q_handle = cpu_to_le16(qid);
 405		qbuf->queue_info[count].tc = tc;
 406		qbuf->queue_info[count].q_teid = cpu_to_le32(q_ctx->q_teid);
 407		count++;
 408	}
 409
 410	return count;
 411}
 412
 413/**
 414 * ice_lag_get_sched_parent - locate or create a sched node parent
 415 * @hw: HW struct for getting parent in
 416 * @tc: traffic class on parent/node
 417 */
 418static struct ice_sched_node *
 419ice_lag_get_sched_parent(struct ice_hw *hw, u8 tc)
 420{
 421	struct ice_sched_node *tc_node, *aggnode, *parent = NULL;
 422	u16 num_nodes[ICE_AQC_TOPO_MAX_LEVEL_NUM] = { 0 };
 423	struct ice_port_info *pi = hw->port_info;
 424	struct device *dev;
 425	u8 aggl, vsil;
 426	int n;
 427
 428	dev = ice_hw_to_dev(hw);
 429
 430	tc_node = ice_sched_get_tc_node(pi, tc);
 431	if (!tc_node) {
 432		dev_warn(dev, "Failure to find TC node for LAG move\n");
 433		return parent;
 434	}
 435
 436	aggnode = ice_sched_get_agg_node(pi, tc_node, ICE_DFLT_AGG_ID);
 437	if (!aggnode) {
 438		dev_warn(dev, "Failure to find aggregate node for LAG move\n");
 439		return parent;
 440	}
 441
 442	aggl = ice_sched_get_agg_layer(hw);
 443	vsil = ice_sched_get_vsi_layer(hw);
 444
 445	for (n = aggl + 1; n < vsil; n++)
 446		num_nodes[n] = 1;
 447
 448	for (n = 0; n < aggnode->num_children; n++) {
 449		parent = ice_sched_get_free_vsi_parent(hw, aggnode->children[n],
 450						       num_nodes);
 451		if (parent)
 452			return parent;
 453	}
 454
 455	/* if free parent not found - add one */
 456	parent = aggnode;
 457	for (n = aggl + 1; n < vsil; n++) {
 458		u16 num_nodes_added;
 459		u32 first_teid;
 460		int err;
 461
 462		err = ice_sched_add_nodes_to_layer(pi, tc_node, parent, n,
 463						   num_nodes[n], &first_teid,
 464						   &num_nodes_added);
 465		if (err || num_nodes[n] != num_nodes_added)
 466			return NULL;
 467
 468		if (num_nodes_added)
 469			parent = ice_sched_find_node_by_teid(tc_node,
 470							     first_teid);
 471		else
 472			parent = parent->children[0];
 473		if (!parent) {
 474			dev_warn(dev, "Failure to add new parent for LAG move\n");
 475			return parent;
 476		}
 477	}
 478
 479	return parent;
 480}
 481
 482/**
 483 * ice_lag_move_vf_node_tc - move scheduling nodes for one VF on one TC
 484 * @lag: lag info struct
 485 * @oldport: lport of previous nodes location
 486 * @newport: lport of destination nodes location
 487 * @vsi_num: array index of VSI in PF space
 488 * @tc: traffic class to move
 489 */
 490static void
 491ice_lag_move_vf_node_tc(struct ice_lag *lag, u8 oldport, u8 newport,
 492			u16 vsi_num, u8 tc)
 493{
 494	DEFINE_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
 495	struct device *dev = ice_pf_to_dev(lag->pf);
 496	u16 numq, valq, num_moved, qbuf_size;
 497	u16 buf_size = __struct_size(buf);
 498	struct ice_aqc_cfg_txqs_buf *qbuf;
 499	struct ice_sched_node *n_prt;
 500	struct ice_hw *new_hw = NULL;
 501	__le32 teid, parent_teid;
 502	struct ice_vsi_ctx *ctx;
 503	u32 tmp_teid;
 504
 505	ctx = ice_get_vsi_ctx(&lag->pf->hw, vsi_num);
 506	if (!ctx) {
 507		dev_warn(dev, "Unable to locate VSI context for LAG failover\n");
 508		return;
 509	}
 510
 511	/* check to see if this VF is enabled on this TC */
 512	if (!ctx->sched.vsi_node[tc])
 513		return;
 514
 515	/* locate HW struct for destination port */
 516	new_hw = ice_lag_find_hw_by_lport(lag, newport);
 517	if (!new_hw) {
 518		dev_warn(dev, "Unable to locate HW struct for LAG node destination\n");
 519		return;
 520	}
 521
 522	numq = ctx->num_lan_q_entries[tc];
 523	teid = ctx->sched.vsi_node[tc]->info.node_teid;
 524	tmp_teid = le32_to_cpu(teid);
 525	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
 526	/* if no teid assigned or numq == 0, then this TC is not active */
 527	if (!tmp_teid || !numq)
 528		return;
 529
 530	/* suspend VSI subtree for Traffic Class "tc" on
 531	 * this VF's VSI
 532	 */
 533	if (ice_sched_suspend_resume_elems(&lag->pf->hw, 1, &tmp_teid, true))
 534		dev_dbg(dev, "Problem suspending traffic for LAG node move\n");
 535
 536	/* reconfigure all VF's queues on this Traffic Class
 537	 * to new port
 538	 */
 539	qbuf_size = struct_size(qbuf, queue_info, numq);
 540	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
 541	if (!qbuf) {
 542		dev_warn(dev, "Failure allocating memory for VF queue recfg buffer\n");
 543		goto resume_traffic;
 544	}
 545
 546	/* add the per queue info for the reconfigure command buffer */
 547	valq = ice_lag_qbuf_recfg(&lag->pf->hw, qbuf, vsi_num, numq, tc);
 548	if (!valq) {
 549		dev_dbg(dev, "No valid queues found for LAG failover\n");
 550		goto qbuf_none;
 551	}
 552
 553	if (ice_aq_cfg_lan_txq(&lag->pf->hw, qbuf, qbuf_size, valq, oldport,
 554			       newport, NULL)) {
 555		dev_warn(dev, "Failure to configure queues for LAG failover\n");
 556		goto qbuf_err;
 557	}
 558
 559qbuf_none:
 560	kfree(qbuf);
 561
 562	/* find new parent in destination port's tree for VF VSI node on this
 563	 * Traffic Class
 564	 */
 565	n_prt = ice_lag_get_sched_parent(new_hw, tc);
 566	if (!n_prt)
 567		goto resume_traffic;
 568
 569	/* Move Vf's VSI node for this TC to newport's scheduler tree */
 570	buf->hdr.src_parent_teid = parent_teid;
 571	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
 572	buf->hdr.num_elems = cpu_to_le16(1);
 573	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
 574	buf->teid[0] = teid;
 575
 576	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
 577		dev_warn(dev, "Failure to move VF nodes for failover\n");
 578	else
 579		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
 580
 581	goto resume_traffic;
 582
 583qbuf_err:
 584	kfree(qbuf);
 585
 586resume_traffic:
 587	/* restart traffic for VSI node */
 588	if (ice_sched_suspend_resume_elems(&lag->pf->hw, 1, &tmp_teid, false))
 589		dev_dbg(dev, "Problem restarting traffic for LAG node move\n");
 590}
 591
 592/**
 593 * ice_lag_build_netdev_list - populate the lag struct's netdev list
 594 * @lag: local lag struct
 595 * @ndlist: pointer to netdev list to populate
 596 */
 597static void ice_lag_build_netdev_list(struct ice_lag *lag,
 598				      struct ice_lag_netdev_list *ndlist)
 599{
 600	struct ice_lag_netdev_list *nl;
 601	struct net_device *tmp_nd;
 602
 603	INIT_LIST_HEAD(&ndlist->node);
 604	rcu_read_lock();
 605	for_each_netdev_in_bond_rcu(lag->upper_netdev, tmp_nd) {
 606		nl = kzalloc(sizeof(*nl), GFP_ATOMIC);
 607		if (!nl)
 608			break;
 609
 610		nl->netdev = tmp_nd;
 611		list_add(&nl->node, &ndlist->node);
 612	}
 613	rcu_read_unlock();
 614	lag->netdev_head = &ndlist->node;
 615}
 616
 617/**
 618 * ice_lag_destroy_netdev_list - free lag struct's netdev list
 619 * @lag: pointer to local lag struct
 620 * @ndlist: pointer to lag struct netdev list
 621 */
 622static void ice_lag_destroy_netdev_list(struct ice_lag *lag,
 623					struct ice_lag_netdev_list *ndlist)
 624{
 625	struct ice_lag_netdev_list *entry, *n;
 626
 627	rcu_read_lock();
 628	list_for_each_entry_safe(entry, n, &ndlist->node, node) {
 629		list_del(&entry->node);
 630		kfree(entry);
 631	}
 632	rcu_read_unlock();
 633	lag->netdev_head = NULL;
 634}
 635
 636/**
 637 * ice_lag_move_single_vf_nodes - Move Tx scheduling nodes for single VF
 638 * @lag: primary interface LAG struct
 639 * @oldport: lport of previous interface
 640 * @newport: lport of destination interface
 641 * @vsi_num: SW index of VF's VSI
 642 */
 643static void
 644ice_lag_move_single_vf_nodes(struct ice_lag *lag, u8 oldport, u8 newport,
 645			     u16 vsi_num)
 646{
 647	u8 tc;
 648
 649	ice_for_each_traffic_class(tc)
 650		ice_lag_move_vf_node_tc(lag, oldport, newport, vsi_num, tc);
 651}
 652
 653/**
 654 * ice_lag_move_new_vf_nodes - Move Tx scheduling nodes for a VF if required
 655 * @vf: the VF to move Tx nodes for
 656 *
 657 * Called just after configuring new VF queues. Check whether the VF Tx
 658 * scheduling nodes need to be updated to fail over to the active port. If so,
 659 * move them now.
 660 */
 661void ice_lag_move_new_vf_nodes(struct ice_vf *vf)
 662{
 663	struct ice_lag_netdev_list ndlist;
 664	u8 pri_port, act_port;
 665	struct ice_lag *lag;
 666	struct ice_vsi *vsi;
 667	struct ice_pf *pf;
 668
 669	vsi = ice_get_vf_vsi(vf);
 670
 671	if (WARN_ON(!vsi))
 672		return;
 673
 674	if (WARN_ON(vsi->type != ICE_VSI_VF))
 675		return;
 676
 677	pf = vf->pf;
 678	lag = pf->lag;
 679
 680	mutex_lock(&pf->lag_mutex);
 681	if (!lag->bonded)
 682		goto new_vf_unlock;
 683
 684	pri_port = pf->hw.port_info->lport;
 685	act_port = lag->active_port;
 686
 687	if (lag->upper_netdev)
 688		ice_lag_build_netdev_list(lag, &ndlist);
 689
 690	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG) &&
 691	    lag->bonded && lag->primary && pri_port != act_port &&
 692	    !list_empty(lag->netdev_head))
 693		ice_lag_move_single_vf_nodes(lag, pri_port, act_port, vsi->idx);
 694
 695	ice_lag_destroy_netdev_list(lag, &ndlist);
 696
 697new_vf_unlock:
 698	mutex_unlock(&pf->lag_mutex);
 699}
 700
 701/**
 702 * ice_lag_move_vf_nodes - move Tx scheduling nodes for all VFs to new port
 703 * @lag: lag info struct
 704 * @oldport: lport of previous interface
 705 * @newport: lport of destination interface
 706 */
 707static void ice_lag_move_vf_nodes(struct ice_lag *lag, u8 oldport, u8 newport)
 708{
 709	struct ice_pf *pf;
 710	int i;
 711
 712	if (!lag->primary)
 713		return;
 714
 715	pf = lag->pf;
 716	ice_for_each_vsi(pf, i)
 717		if (pf->vsi[i] && (pf->vsi[i]->type == ICE_VSI_VF ||
 718				   pf->vsi[i]->type == ICE_VSI_SWITCHDEV_CTRL))
 719			ice_lag_move_single_vf_nodes(lag, oldport, newport, i);
 720}
 721
 722/**
 723 * ice_lag_move_vf_nodes_cfg - move vf nodes outside LAG netdev event context
 724 * @lag: local lag struct
 725 * @src_prt: lport value for source port
 726 * @dst_prt: lport value for destination port
 727 *
 728 * This function is used to move nodes during an out-of-netdev-event situation,
 729 * primarily when the driver needs to reconfigure or recreate resources.
 730 *
 731 * Must be called while holding the lag_mutex to avoid lag events from
 732 * processing while out-of-sync moves are happening.  Also, paired moves,
 733 * such as used in a reset flow, should both be called under the same mutex
 734 * lock to avoid changes between start of reset and end of reset.
 735 */
 736void ice_lag_move_vf_nodes_cfg(struct ice_lag *lag, u8 src_prt, u8 dst_prt)
 737{
 738	struct ice_lag_netdev_list ndlist;
 739
 740	ice_lag_build_netdev_list(lag, &ndlist);
 741	ice_lag_move_vf_nodes(lag, src_prt, dst_prt);
 742	ice_lag_destroy_netdev_list(lag, &ndlist);
 743}
 744
 745#define ICE_LAG_SRIOV_CP_RECIPE		10
 746#define ICE_LAG_SRIOV_TRAIN_PKT_LEN	16
 747
 748/**
 749 * ice_lag_cfg_cp_fltr - configure filter for control packets
 750 * @lag: local interface's lag struct
 751 * @add: add or remove rule
 752 */
 753static void
 754ice_lag_cfg_cp_fltr(struct ice_lag *lag, bool add)
 755{
 756	struct ice_sw_rule_lkup_rx_tx *s_rule = NULL;
 757	struct ice_vsi *vsi;
 758	u16 buf_len, opc;
 759
 760	vsi = lag->pf->vsi[0];
 761
 762	buf_len = ICE_SW_RULE_RX_TX_HDR_SIZE(s_rule,
 763					     ICE_LAG_SRIOV_TRAIN_PKT_LEN);
 764	s_rule = kzalloc(buf_len, GFP_KERNEL);
 765	if (!s_rule) {
 766		netdev_warn(lag->netdev, "-ENOMEM error configuring CP filter\n");
 767		return;
 768	}
 769
 770	if (add) {
 771		s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX);
 772		s_rule->recipe_id = cpu_to_le16(ICE_LAG_SRIOV_CP_RECIPE);
 773		s_rule->src = cpu_to_le16(vsi->port_info->lport);
 774		s_rule->act = cpu_to_le32(ICE_FWD_TO_VSI |
 775					  ICE_SINGLE_ACT_LAN_ENABLE |
 776					  ICE_SINGLE_ACT_VALID_BIT |
 777					  FIELD_PREP(ICE_SINGLE_ACT_VSI_ID_M, vsi->vsi_num));
 778		s_rule->hdr_len = cpu_to_le16(ICE_LAG_SRIOV_TRAIN_PKT_LEN);
 779		memcpy(s_rule->hdr_data, lacp_train_pkt, LACP_TRAIN_PKT_LEN);
 780		opc = ice_aqc_opc_add_sw_rules;
 781	} else {
 782		opc = ice_aqc_opc_remove_sw_rules;
 783		s_rule->index = cpu_to_le16(lag->cp_rule_idx);
 784	}
 785	if (ice_aq_sw_rules(&lag->pf->hw, s_rule, buf_len, 1, opc, NULL)) {
 786		netdev_warn(lag->netdev, "Error %s CP rule for fail-over\n",
 787			    add ? "ADDING" : "REMOVING");
 788		goto cp_free;
 789	}
 790
 791	if (add)
 792		lag->cp_rule_idx = le16_to_cpu(s_rule->index);
 793	else
 794		lag->cp_rule_idx = 0;
 795
 796cp_free:
 797	kfree(s_rule);
 798}
 799
 800/**
 801 * ice_lag_info_event - handle NETDEV_BONDING_INFO event
 802 * @lag: LAG info struct
 803 * @ptr: opaque data pointer
 804 *
 805 * ptr is to be cast to (netdev_notifier_bonding_info *)
 806 */
 807static void ice_lag_info_event(struct ice_lag *lag, void *ptr)
 808{
 809	struct netdev_notifier_bonding_info *info;
 810	struct netdev_bonding_info *bonding_info;
 811	struct net_device *event_netdev;
 812	const char *lag_netdev_name;
 813
 814	event_netdev = netdev_notifier_info_to_dev(ptr);
 815	info = ptr;
 816	lag_netdev_name = netdev_name(lag->netdev);
 817	bonding_info = &info->bonding_info;
 818
 819	if (event_netdev != lag->netdev || !lag->bonded || !lag->upper_netdev)
 820		return;
 821
 822	if (bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) {
 823		netdev_dbg(lag->netdev, "Bonding event recv, but mode not active/backup\n");
 824		goto lag_out;
 825	}
 826
 827	if (strcmp(bonding_info->slave.slave_name, lag_netdev_name)) {
 828		netdev_dbg(lag->netdev, "Bonding event recv, but secondary info not for us\n");
 829		goto lag_out;
 830	}
 831
 832	if (bonding_info->slave.state)
 833		ice_lag_set_backup(lag);
 834	else
 835		ice_lag_set_primary(lag);
 836
 837lag_out:
 838	ice_display_lag_info(lag);
 839}
 840
 841/**
 842 * ice_lag_reclaim_vf_tc - move scheduling nodes back to primary interface
 843 * @lag: primary interface lag struct
 844 * @src_hw: HW struct current node location
 845 * @vsi_num: VSI index in PF space
 846 * @tc: traffic class to move
 847 */
 848static void
 849ice_lag_reclaim_vf_tc(struct ice_lag *lag, struct ice_hw *src_hw, u16 vsi_num,
 850		      u8 tc)
 851{
 852	DEFINE_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
 853	struct device *dev = ice_pf_to_dev(lag->pf);
 854	u16 numq, valq, num_moved, qbuf_size;
 855	u16 buf_size = __struct_size(buf);
 856	struct ice_aqc_cfg_txqs_buf *qbuf;
 857	struct ice_sched_node *n_prt;
 858	__le32 teid, parent_teid;
 859	struct ice_vsi_ctx *ctx;
 860	struct ice_hw *hw;
 861	u32 tmp_teid;
 862
 863	hw = &lag->pf->hw;
 864	ctx = ice_get_vsi_ctx(hw, vsi_num);
 865	if (!ctx) {
 866		dev_warn(dev, "Unable to locate VSI context for LAG reclaim\n");
 867		return;
 868	}
 869
 870	/* check to see if this VF is enabled on this TC */
 871	if (!ctx->sched.vsi_node[tc])
 872		return;
 873
 874	numq = ctx->num_lan_q_entries[tc];
 875	teid = ctx->sched.vsi_node[tc]->info.node_teid;
 876	tmp_teid = le32_to_cpu(teid);
 877	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
 878
 879	/* if !teid or !numq, then this TC is not active */
 880	if (!tmp_teid || !numq)
 881		return;
 882
 883	/* suspend traffic */
 884	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, true))
 885		dev_dbg(dev, "Problem suspending traffic for LAG node move\n");
 886
 887	/* reconfig queues for new port */
 888	qbuf_size = struct_size(qbuf, queue_info, numq);
 889	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
 890	if (!qbuf) {
 891		dev_warn(dev, "Failure allocating memory for VF queue recfg buffer\n");
 892		goto resume_reclaim;
 893	}
 894
 895	/* add the per queue info for the reconfigure command buffer */
 896	valq = ice_lag_qbuf_recfg(hw, qbuf, vsi_num, numq, tc);
 897	if (!valq) {
 898		dev_dbg(dev, "No valid queues found for LAG reclaim\n");
 899		goto reclaim_none;
 900	}
 901
 902	if (ice_aq_cfg_lan_txq(hw, qbuf, qbuf_size, numq,
 903			       src_hw->port_info->lport, hw->port_info->lport,
 904			       NULL)) {
 905		dev_warn(dev, "Failure to configure queues for LAG failover\n");
 906		goto reclaim_qerr;
 907	}
 908
 909reclaim_none:
 910	kfree(qbuf);
 911
 912	/* find parent in primary tree */
 913	n_prt = ice_lag_get_sched_parent(hw, tc);
 914	if (!n_prt)
 915		goto resume_reclaim;
 916
 917	/* Move node to new parent */
 918	buf->hdr.src_parent_teid = parent_teid;
 919	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
 920	buf->hdr.num_elems = cpu_to_le16(1);
 921	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
 922	buf->teid[0] = teid;
 923
 924	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
 925		dev_warn(dev, "Failure to move VF nodes for LAG reclaim\n");
 926	else
 927		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
 928
 929	goto resume_reclaim;
 930
 931reclaim_qerr:
 932	kfree(qbuf);
 933
 934resume_reclaim:
 935	/* restart traffic */
 936	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, false))
 937		dev_warn(dev, "Problem restarting traffic for LAG node reclaim\n");
 938}
 939
 940/**
 941 * ice_lag_reclaim_vf_nodes - When interface leaving bond primary reclaims nodes
 942 * @lag: primary interface lag struct
 943 * @src_hw: HW struct for current node location
 944 */
 945static void
 946ice_lag_reclaim_vf_nodes(struct ice_lag *lag, struct ice_hw *src_hw)
 947{
 948	struct ice_pf *pf;
 949	int i, tc;
 950
 951	if (!lag->primary || !src_hw)
 952		return;
 953
 954	pf = lag->pf;
 955	ice_for_each_vsi(pf, i)
 956		if (pf->vsi[i] && (pf->vsi[i]->type == ICE_VSI_VF ||
 957				   pf->vsi[i]->type == ICE_VSI_SWITCHDEV_CTRL))
 958			ice_for_each_traffic_class(tc)
 959				ice_lag_reclaim_vf_tc(lag, src_hw, i, tc);
 960}
 961
 962/**
 963 * ice_lag_link - handle LAG link event
 964 * @lag: LAG info struct
 965 */
 966static void ice_lag_link(struct ice_lag *lag)
 967{
 968	struct ice_pf *pf = lag->pf;
 969
 970	if (lag->bonded)
 971		dev_warn(ice_pf_to_dev(pf), "%s Already part of a bond\n",
 972			 netdev_name(lag->netdev));
 973
 974	lag->bonded = true;
 975	lag->role = ICE_LAG_UNSET;
 976	netdev_info(lag->netdev, "Shared SR-IOV resources in bond are active\n");
 977}
 978
 979/**
 980 * ice_lag_unlink - handle unlink event
 981 * @lag: LAG info struct
 982 */
 983static void ice_lag_unlink(struct ice_lag *lag)
 984{
 985	u8 pri_port, act_port, loc_port;
 986	struct ice_pf *pf = lag->pf;
 987
 988	if (!lag->bonded) {
 989		netdev_dbg(lag->netdev, "bonding unlink event on non-LAG netdev\n");
 990		return;
 991	}
 992
 993	if (lag->primary) {
 994		act_port = lag->active_port;
 995		pri_port = lag->pf->hw.port_info->lport;
 996		if (act_port != pri_port && act_port != ICE_LAG_INVALID_PORT)
 997			ice_lag_move_vf_nodes(lag, act_port, pri_port);
 998		lag->primary = false;
 999		lag->active_port = ICE_LAG_INVALID_PORT;
1000	} else {
1001		struct ice_lag *primary_lag;
1002
1003		primary_lag = ice_lag_find_primary(lag);
1004		if (primary_lag) {
1005			act_port = primary_lag->active_port;
1006			pri_port = primary_lag->pf->hw.port_info->lport;
1007			loc_port = pf->hw.port_info->lport;
1008			if (act_port == loc_port &&
1009			    act_port != ICE_LAG_INVALID_PORT) {
1010				ice_lag_reclaim_vf_nodes(primary_lag,
1011							 &lag->pf->hw);
1012				primary_lag->active_port = ICE_LAG_INVALID_PORT;
1013			}
1014		}
1015	}
1016
1017	lag->bonded = false;
1018	lag->role = ICE_LAG_NONE;
1019	lag->upper_netdev = NULL;
1020}
1021
1022/**
1023 * ice_lag_link_unlink - helper function to call lag_link/unlink
1024 * @lag: lag info struct
1025 * @ptr: opaque pointer data
1026 */
1027static void ice_lag_link_unlink(struct ice_lag *lag, void *ptr)
1028{
1029	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1030	struct netdev_notifier_changeupper_info *info = ptr;
1031
1032	if (netdev != lag->netdev)
1033		return;
1034
1035	if (info->linking)
1036		ice_lag_link(lag);
1037	else
1038		ice_lag_unlink(lag);
1039}
1040
1041/**
1042 * ice_lag_set_swid - set the SWID on secondary interface
1043 * @primary_swid: primary interface's SWID
1044 * @local_lag: local interfaces LAG struct
1045 * @link: Is this a linking activity
1046 *
1047 * If link is false, then primary_swid should be expected to not be valid
1048 * This function should never be called in interrupt context.
1049 */
1050static void
1051ice_lag_set_swid(u16 primary_swid, struct ice_lag *local_lag,
1052		 bool link)
1053{
1054	struct ice_aqc_alloc_free_res_elem *buf;
1055	struct ice_aqc_set_port_params *cmd;
1056	struct ice_aq_desc desc;
1057	u16 buf_len, swid;
1058	int status, i;
1059
1060	buf_len = struct_size(buf, elem, 1);
1061	buf = kzalloc(buf_len, GFP_KERNEL);
1062	if (!buf) {
1063		dev_err(ice_pf_to_dev(local_lag->pf), "-ENOMEM error setting SWID\n");
1064		return;
1065	}
1066
1067	buf->num_elems = cpu_to_le16(1);
1068	buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_SWID);
1069	/* if unlinnking need to free the shared resource */
1070	if (!link && local_lag->bond_swid) {
1071		buf->elem[0].e.sw_resp = cpu_to_le16(local_lag->bond_swid);
1072		status = ice_aq_alloc_free_res(&local_lag->pf->hw, buf,
1073					       buf_len, ice_aqc_opc_free_res);
1074		if (status)
1075			dev_err(ice_pf_to_dev(local_lag->pf), "Error freeing SWID during LAG unlink\n");
1076		local_lag->bond_swid = 0;
1077	}
1078
1079	if (link) {
1080		buf->res_type |=  cpu_to_le16(ICE_LAG_RES_SHARED |
1081					      ICE_LAG_RES_VALID);
1082		/* store the primary's SWID in case it leaves bond first */
1083		local_lag->bond_swid = primary_swid;
1084		buf->elem[0].e.sw_resp = cpu_to_le16(local_lag->bond_swid);
1085	} else {
1086		buf->elem[0].e.sw_resp =
1087			cpu_to_le16(local_lag->pf->hw.port_info->sw_id);
1088	}
1089
1090	status = ice_aq_alloc_free_res(&local_lag->pf->hw, buf, buf_len,
1091				       ice_aqc_opc_alloc_res);
1092	if (status)
1093		dev_err(ice_pf_to_dev(local_lag->pf), "Error subscribing to SWID 0x%04X\n",
1094			local_lag->bond_swid);
1095
1096	kfree(buf);
1097
1098	/* Configure port param SWID to correct value */
1099	if (link)
1100		swid = primary_swid;
1101	else
1102		swid = local_lag->pf->hw.port_info->sw_id;
1103
1104	cmd = &desc.params.set_port_params;
1105	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_params);
1106
1107	cmd->swid = cpu_to_le16(ICE_AQC_PORT_SWID_VALID | swid);
1108	/* If this is happening in reset context, it is possible that the
1109	 * primary interface has not finished setting its SWID to SHARED
1110	 * yet.  Allow retries to account for this timing issue between
1111	 * interfaces.
1112	 */
1113	for (i = 0; i < ICE_LAG_RESET_RETRIES; i++) {
1114		status = ice_aq_send_cmd(&local_lag->pf->hw, &desc, NULL, 0,
1115					 NULL);
1116		if (!status)
1117			break;
1118
1119		usleep_range(1000, 2000);
1120	}
1121
1122	if (status)
1123		dev_err(ice_pf_to_dev(local_lag->pf), "Error setting SWID in port params %d\n",
1124			status);
1125}
1126
1127/**
1128 * ice_lag_primary_swid - set/clear the SHARED attrib of primary's SWID
1129 * @lag: primary interface's lag struct
1130 * @link: is this a linking activity
1131 *
1132 * Implement setting primary SWID as shared using 0x020B
1133 */
1134static void ice_lag_primary_swid(struct ice_lag *lag, bool link)
1135{
1136	struct ice_hw *hw;
1137	u16 swid;
1138
1139	hw = &lag->pf->hw;
1140	swid = hw->port_info->sw_id;
1141
1142	if (ice_share_res(hw, ICE_AQC_RES_TYPE_SWID, link, swid))
1143		dev_warn(ice_pf_to_dev(lag->pf), "Failure to set primary interface shared status\n");
1144}
1145
1146/**
1147 * ice_lag_add_prune_list - Adds event_pf's VSI to primary's prune list
1148 * @lag: lag info struct
1149 * @event_pf: PF struct for VSI we are adding to primary's prune list
1150 */
1151static void ice_lag_add_prune_list(struct ice_lag *lag, struct ice_pf *event_pf)
1152{
1153	u16 num_vsi, rule_buf_sz, vsi_list_id, event_vsi_num, prim_vsi_idx;
1154	struct ice_sw_rule_vsi_list *s_rule = NULL;
1155	struct device *dev;
1156
1157	num_vsi = 1;
1158
1159	dev = ice_pf_to_dev(lag->pf);
1160	event_vsi_num = event_pf->vsi[0]->vsi_num;
1161	prim_vsi_idx = lag->pf->vsi[0]->idx;
1162
1163	if (!ice_find_vsi_list_entry(&lag->pf->hw, ICE_SW_LKUP_VLAN,
1164				     prim_vsi_idx, &vsi_list_id)) {
1165		dev_warn(dev, "Could not locate prune list when setting up SRIOV LAG\n");
1166		return;
1167	}
1168
1169	rule_buf_sz = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
1170	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
1171	if (!s_rule) {
1172		dev_warn(dev, "Error allocating space for prune list when configuring SRIOV LAG\n");
1173		return;
1174	}
1175
1176	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_PRUNE_LIST_SET);
1177	s_rule->index = cpu_to_le16(vsi_list_id);
1178	s_rule->number_vsi = cpu_to_le16(num_vsi);
1179	s_rule->vsi[0] = cpu_to_le16(event_vsi_num);
1180
1181	if (ice_aq_sw_rules(&event_pf->hw, s_rule, rule_buf_sz, 1,
1182			    ice_aqc_opc_update_sw_rules, NULL))
1183		dev_warn(dev, "Error adding VSI prune list\n");
1184	kfree(s_rule);
1185}
1186
1187/**
1188 * ice_lag_del_prune_list - Remove secondary's vsi from primary's prune list
1189 * @lag: primary interface's ice_lag struct
1190 * @event_pf: PF struct for unlinking interface
1191 */
1192static void ice_lag_del_prune_list(struct ice_lag *lag, struct ice_pf *event_pf)
1193{
1194	u16 num_vsi, vsi_num, vsi_idx, rule_buf_sz, vsi_list_id;
1195	struct ice_sw_rule_vsi_list *s_rule = NULL;
1196	struct device *dev;
1197
1198	num_vsi = 1;
1199
1200	dev = ice_pf_to_dev(lag->pf);
1201	vsi_num = event_pf->vsi[0]->vsi_num;
1202	vsi_idx = lag->pf->vsi[0]->idx;
1203
1204	if (!ice_find_vsi_list_entry(&lag->pf->hw, ICE_SW_LKUP_VLAN,
1205				     vsi_idx, &vsi_list_id)) {
1206		dev_warn(dev, "Could not locate prune list when unwinding SRIOV LAG\n");
1207		return;
1208	}
1209
1210	rule_buf_sz = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
1211	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
1212	if (!s_rule) {
1213		dev_warn(dev, "Error allocating prune list when unwinding SRIOV LAG\n");
1214		return;
1215	}
1216
1217	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR);
1218	s_rule->index = cpu_to_le16(vsi_list_id);
1219	s_rule->number_vsi = cpu_to_le16(num_vsi);
1220	s_rule->vsi[0] = cpu_to_le16(vsi_num);
1221
1222	if (ice_aq_sw_rules(&event_pf->hw, (struct ice_aqc_sw_rules *)s_rule,
1223			    rule_buf_sz, 1, ice_aqc_opc_update_sw_rules, NULL))
1224		dev_warn(dev, "Error clearing VSI prune list\n");
1225
1226	kfree(s_rule);
1227}
1228
1229/**
1230 * ice_lag_init_feature_support_flag - Check for package and NVM support for LAG
1231 * @pf: PF struct
1232 */
1233static void ice_lag_init_feature_support_flag(struct ice_pf *pf)
1234{
1235	struct ice_hw_common_caps *caps;
1236
1237	caps = &pf->hw.dev_caps.common_cap;
1238	if (caps->roce_lag)
1239		ice_set_feature_support(pf, ICE_F_ROCE_LAG);
1240	else
1241		ice_clear_feature_support(pf, ICE_F_ROCE_LAG);
1242
1243	if (caps->sriov_lag && ice_pkg_has_lport_extract(&pf->hw))
1244		ice_set_feature_support(pf, ICE_F_SRIOV_LAG);
1245	else
1246		ice_clear_feature_support(pf, ICE_F_SRIOV_LAG);
1247}
1248
1249/**
1250 * ice_lag_changeupper_event - handle LAG changeupper event
1251 * @lag: LAG info struct
1252 * @ptr: opaque pointer data
1253 */
1254static void ice_lag_changeupper_event(struct ice_lag *lag, void *ptr)
1255{
1256	struct netdev_notifier_changeupper_info *info;
1257	struct ice_lag *primary_lag;
1258	struct net_device *netdev;
1259
1260	info = ptr;
1261	netdev = netdev_notifier_info_to_dev(ptr);
1262
1263	/* not for this netdev */
1264	if (netdev != lag->netdev)
1265		return;
1266
1267	primary_lag = ice_lag_find_primary(lag);
1268	if (info->linking) {
1269		lag->upper_netdev = info->upper_dev;
1270		/* If there is not already a primary interface in the LAG,
1271		 * then mark this one as primary.
1272		 */
1273		if (!primary_lag) {
1274			lag->primary = true;
1275			/* Configure primary's SWID to be shared */
1276			ice_lag_primary_swid(lag, true);
1277			primary_lag = lag;
1278		} else {
1279			u16 swid;
1280
1281			swid = primary_lag->pf->hw.port_info->sw_id;
1282			ice_lag_set_swid(swid, lag, true);
1283			ice_lag_add_prune_list(primary_lag, lag->pf);
1284			ice_lag_cfg_drop_fltr(lag, true);
1285		}
1286		/* add filter for primary control packets */
1287		ice_lag_cfg_cp_fltr(lag, true);
1288	} else {
1289		if (!primary_lag && lag->primary)
1290			primary_lag = lag;
1291
1292		if (!lag->primary) {
1293			ice_lag_set_swid(0, lag, false);
1294		} else {
1295			if (primary_lag && lag->primary) {
1296				ice_lag_primary_swid(lag, false);
1297				ice_lag_del_prune_list(primary_lag, lag->pf);
1298			}
1299		}
1300		/* remove filter for control packets */
1301		ice_lag_cfg_cp_fltr(lag, false);
1302	}
1303}
1304
1305/**
1306 * ice_lag_monitor_link - monitor interfaces entering/leaving the aggregate
1307 * @lag: lag info struct
1308 * @ptr: opaque data containing notifier event
1309 *
1310 * This function only operates after a primary has been set.
1311 */
1312static void ice_lag_monitor_link(struct ice_lag *lag, void *ptr)
1313{
1314	struct netdev_notifier_changeupper_info *info;
1315	struct ice_hw *prim_hw, *active_hw;
1316	struct net_device *event_netdev;
1317	struct ice_pf *pf;
1318	u8 prim_port;
1319
1320	if (!lag->primary)
1321		return;
1322
1323	event_netdev = netdev_notifier_info_to_dev(ptr);
1324	if (!netif_is_same_ice(lag->pf, event_netdev))
1325		return;
1326
1327	pf = lag->pf;
1328	prim_hw = &pf->hw;
1329	prim_port = prim_hw->port_info->lport;
1330
1331	info = (struct netdev_notifier_changeupper_info *)ptr;
1332	if (info->upper_dev != lag->upper_netdev)
1333		return;
1334
1335	if (!info->linking) {
1336		/* Since there are only two interfaces allowed in SRIOV+LAG, if
1337		 * one port is leaving, then nodes need to be on primary
1338		 * interface.
1339		 */
1340		if (prim_port != lag->active_port &&
1341		    lag->active_port != ICE_LAG_INVALID_PORT) {
1342			active_hw = ice_lag_find_hw_by_lport(lag,
1343							     lag->active_port);
1344			ice_lag_reclaim_vf_nodes(lag, active_hw);
1345			lag->active_port = ICE_LAG_INVALID_PORT;
1346		}
1347	}
1348}
1349
1350/**
1351 * ice_lag_monitor_active - main PF keep track of which port is active
1352 * @lag: lag info struct
1353 * @ptr: opaque data containing notifier event
1354 *
1355 * This function is for the primary PF to monitor changes in which port is
1356 * active and handle changes for SRIOV VF functionality
1357 */
1358static void ice_lag_monitor_active(struct ice_lag *lag, void *ptr)
1359{
1360	struct net_device *event_netdev, *event_upper;
1361	struct netdev_notifier_bonding_info *info;
1362	struct netdev_bonding_info *bonding_info;
1363	struct ice_netdev_priv *event_np;
1364	struct ice_pf *pf, *event_pf;
1365	u8 prim_port, event_port;
1366
1367	if (!lag->primary)
1368		return;
1369
1370	pf = lag->pf;
1371	if (!pf)
1372		return;
1373
1374	event_netdev = netdev_notifier_info_to_dev(ptr);
1375	rcu_read_lock();
1376	event_upper = netdev_master_upper_dev_get_rcu(event_netdev);
1377	rcu_read_unlock();
1378	if (!netif_is_ice(event_netdev) || event_upper != lag->upper_netdev)
1379		return;
1380
1381	event_np = netdev_priv(event_netdev);
1382	event_pf = event_np->vsi->back;
1383	event_port = event_pf->hw.port_info->lport;
1384	prim_port = pf->hw.port_info->lport;
1385
1386	info = (struct netdev_notifier_bonding_info *)ptr;
1387	bonding_info = &info->bonding_info;
1388
1389	if (!bonding_info->slave.state) {
1390		/* if no port is currently active, then nodes and filters exist
1391		 * on primary port, check if we need to move them
1392		 */
1393		if (lag->active_port == ICE_LAG_INVALID_PORT) {
1394			if (event_port != prim_port)
1395				ice_lag_move_vf_nodes(lag, prim_port,
1396						      event_port);
1397			lag->active_port = event_port;
1398			return;
1399		}
1400
1401		/* active port is already set and is current event port */
1402		if (lag->active_port == event_port)
1403			return;
1404		/* new active port */
1405		ice_lag_move_vf_nodes(lag, lag->active_port, event_port);
1406		lag->active_port = event_port;
1407	} else {
1408		/* port not set as currently active (e.g. new active port
1409		 * has already claimed the nodes and filters
1410		 */
1411		if (lag->active_port != event_port)
1412			return;
1413		/* This is the case when neither port is active (both link down)
1414		 * Link down on the bond - set active port to invalid and move
1415		 * nodes and filters back to primary if not already there
1416		 */
1417		if (event_port != prim_port)
1418			ice_lag_move_vf_nodes(lag, event_port, prim_port);
1419		lag->active_port = ICE_LAG_INVALID_PORT;
1420	}
1421}
1422
1423/**
1424 * ice_lag_chk_comp - evaluate bonded interface for feature support
1425 * @lag: lag info struct
1426 * @ptr: opaque data for netdev event info
1427 */
1428static bool
1429ice_lag_chk_comp(struct ice_lag *lag, void *ptr)
1430{
1431	struct net_device *event_netdev, *event_upper;
1432	struct netdev_notifier_bonding_info *info;
1433	struct netdev_bonding_info *bonding_info;
1434	struct list_head *tmp;
1435	struct device *dev;
1436	int count = 0;
1437
1438	if (!lag->primary)
1439		return true;
1440
1441	event_netdev = netdev_notifier_info_to_dev(ptr);
1442	rcu_read_lock();
1443	event_upper = netdev_master_upper_dev_get_rcu(event_netdev);
1444	rcu_read_unlock();
1445	if (event_upper != lag->upper_netdev)
1446		return true;
1447
1448	dev = ice_pf_to_dev(lag->pf);
1449
1450	/* only supporting switchdev mode for SRIOV VF LAG.
1451	 * primary interface has to be in switchdev mode
1452	 */
1453	if (!ice_is_switchdev_running(lag->pf)) {
1454		dev_info(dev, "Primary interface not in switchdev mode - VF LAG disabled\n");
1455		return false;
1456	}
1457
1458	info = (struct netdev_notifier_bonding_info *)ptr;
1459	bonding_info = &info->bonding_info;
1460	lag->bond_mode = bonding_info->master.bond_mode;
1461	if (lag->bond_mode != BOND_MODE_ACTIVEBACKUP) {
1462		dev_info(dev, "Bond Mode not ACTIVE-BACKUP - VF LAG disabled\n");
1463		return false;
1464	}
1465
1466	list_for_each(tmp, lag->netdev_head) {
1467		struct ice_dcbx_cfg *dcb_cfg, *peer_dcb_cfg;
1468		struct ice_lag_netdev_list *entry;
1469		struct ice_netdev_priv *peer_np;
1470		struct net_device *peer_netdev;
1471		struct ice_vsi *vsi, *peer_vsi;
1472		struct ice_pf *peer_pf;
1473
1474		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
1475		peer_netdev = entry->netdev;
1476		if (!netif_is_ice(peer_netdev)) {
1477			dev_info(dev, "Found %s non-ice netdev in LAG - VF LAG disabled\n",
1478				 netdev_name(peer_netdev));
1479			return false;
1480		}
1481
1482		count++;
1483		if (count > 2) {
1484			dev_info(dev, "Found more than two netdevs in LAG - VF LAG disabled\n");
1485			return false;
1486		}
1487
1488		peer_np = netdev_priv(peer_netdev);
1489		vsi = ice_get_main_vsi(lag->pf);
1490		peer_vsi = peer_np->vsi;
1491		if (lag->pf->pdev->bus != peer_vsi->back->pdev->bus ||
1492		    lag->pf->pdev->slot != peer_vsi->back->pdev->slot) {
1493			dev_info(dev, "Found %s on different device in LAG - VF LAG disabled\n",
1494				 netdev_name(peer_netdev));
1495			return false;
1496		}
1497
1498		dcb_cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
1499		peer_dcb_cfg = &peer_vsi->port_info->qos_cfg.local_dcbx_cfg;
1500		if (memcmp(dcb_cfg, peer_dcb_cfg,
1501			   sizeof(struct ice_dcbx_cfg))) {
1502			dev_info(dev, "Found %s with different DCB in LAG - VF LAG disabled\n",
1503				 netdev_name(peer_netdev));
1504			return false;
1505		}
1506
1507		peer_pf = peer_vsi->back;
1508		if (test_bit(ICE_FLAG_FW_LLDP_AGENT, peer_pf->flags)) {
1509			dev_warn(dev, "Found %s with FW LLDP agent active - VF LAG disabled\n",
1510				 netdev_name(peer_netdev));
1511			return false;
1512		}
1513	}
1514
1515	return true;
1516}
1517
1518/**
1519 * ice_lag_unregister - handle netdev unregister events
1520 * @lag: LAG info struct
1521 * @event_netdev: netdev struct for target of notifier event
1522 */
1523static void
1524ice_lag_unregister(struct ice_lag *lag, struct net_device *event_netdev)
1525{
1526	struct ice_netdev_priv *np;
1527	struct ice_pf *event_pf;
1528	struct ice_lag *p_lag;
1529
1530	p_lag = ice_lag_find_primary(lag);
1531	np = netdev_priv(event_netdev);
1532	event_pf = np->vsi->back;
1533
1534	if (p_lag) {
1535		if (p_lag->active_port != p_lag->pf->hw.port_info->lport &&
1536		    p_lag->active_port != ICE_LAG_INVALID_PORT) {
1537			struct ice_hw *active_hw;
1538
1539			active_hw = ice_lag_find_hw_by_lport(lag,
1540							     p_lag->active_port);
1541			if (active_hw)
1542				ice_lag_reclaim_vf_nodes(p_lag, active_hw);
1543			lag->active_port = ICE_LAG_INVALID_PORT;
1544		}
1545	}
1546
1547	/* primary processing for primary */
1548	if (lag->primary && lag->netdev == event_netdev)
1549		ice_lag_primary_swid(lag, false);
1550
1551	/* primary processing for secondary */
1552	if (lag->primary && lag->netdev != event_netdev)
1553		ice_lag_del_prune_list(lag, event_pf);
1554
1555	/* secondary processing for secondary */
1556	if (!lag->primary && lag->netdev == event_netdev)
1557		ice_lag_set_swid(0, lag, false);
1558}
1559
1560/**
1561 * ice_lag_monitor_rdma - set and clear rdma functionality
1562 * @lag: pointer to lag struct
1563 * @ptr: opaque data for netdev event info
1564 */
1565static void
1566ice_lag_monitor_rdma(struct ice_lag *lag, void *ptr)
1567{
1568	struct netdev_notifier_changeupper_info *info;
1569	struct net_device *netdev;
1570
1571	info = ptr;
1572	netdev = netdev_notifier_info_to_dev(ptr);
1573
1574	if (netdev != lag->netdev)
1575		return;
1576
1577	if (info->linking)
1578		ice_clear_rdma_cap(lag->pf);
1579	else
1580		ice_set_rdma_cap(lag->pf);
1581}
1582
1583/**
1584 * ice_lag_chk_disabled_bond - monitor interfaces entering/leaving disabled bond
1585 * @lag: lag info struct
1586 * @ptr: opaque data containing event
1587 *
1588 * as interfaces enter a bond - determine if the bond is currently
1589 * SRIOV LAG compliant and flag if not.  As interfaces leave the
1590 * bond, reset their compliant status.
1591 */
1592static void ice_lag_chk_disabled_bond(struct ice_lag *lag, void *ptr)
1593{
1594	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1595	struct netdev_notifier_changeupper_info *info = ptr;
1596	struct ice_lag *prim_lag;
1597
1598	if (netdev != lag->netdev)
1599		return;
1600
1601	if (info->linking) {
1602		prim_lag = ice_lag_find_primary(lag);
1603		if (prim_lag &&
1604		    !ice_is_feature_supported(prim_lag->pf, ICE_F_SRIOV_LAG)) {
1605			ice_clear_feature_support(lag->pf, ICE_F_SRIOV_LAG);
1606			netdev_info(netdev, "Interface added to non-compliant SRIOV LAG aggregate\n");
1607		}
1608	} else {
1609		ice_lag_init_feature_support_flag(lag->pf);
1610	}
1611}
1612
1613/**
1614 * ice_lag_disable_sriov_bond - set members of bond as not supporting SRIOV LAG
1615 * @lag: primary interfaces lag struct
1616 */
1617static void ice_lag_disable_sriov_bond(struct ice_lag *lag)
1618{
1619	struct ice_netdev_priv *np;
1620	struct ice_pf *pf;
1621
1622	np = netdev_priv(lag->netdev);
1623	pf = np->vsi->back;
1624	ice_clear_feature_support(pf, ICE_F_SRIOV_LAG);
1625}
1626
1627/**
1628 * ice_lag_process_event - process a task assigned to the lag_wq
1629 * @work: pointer to work_struct
1630 */
1631static void ice_lag_process_event(struct work_struct *work)
1632{
1633	struct netdev_notifier_changeupper_info *info;
1634	struct ice_lag_work *lag_work;
1635	struct net_device *netdev;
1636	struct list_head *tmp, *n;
1637	struct ice_pf *pf;
1638
1639	lag_work = container_of(work, struct ice_lag_work, lag_task);
1640	pf = lag_work->lag->pf;
1641
1642	mutex_lock(&pf->lag_mutex);
1643	lag_work->lag->netdev_head = &lag_work->netdev_list.node;
1644
1645	switch (lag_work->event) {
1646	case NETDEV_CHANGEUPPER:
1647		info = &lag_work->info.changeupper_info;
1648		ice_lag_chk_disabled_bond(lag_work->lag, info);
1649		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1650			ice_lag_monitor_link(lag_work->lag, info);
1651			ice_lag_changeupper_event(lag_work->lag, info);
1652			ice_lag_link_unlink(lag_work->lag, info);
1653		}
1654		ice_lag_monitor_rdma(lag_work->lag, info);
1655		break;
1656	case NETDEV_BONDING_INFO:
1657		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1658			if (!ice_lag_chk_comp(lag_work->lag,
1659					      &lag_work->info.bonding_info)) {
1660				netdev = lag_work->info.bonding_info.info.dev;
1661				ice_lag_disable_sriov_bond(lag_work->lag);
1662				ice_lag_unregister(lag_work->lag, netdev);
1663				goto lag_cleanup;
1664			}
1665			ice_lag_monitor_active(lag_work->lag,
1666					       &lag_work->info.bonding_info);
1667			ice_lag_cfg_pf_fltrs(lag_work->lag,
1668					     &lag_work->info.bonding_info);
1669		}
1670		ice_lag_info_event(lag_work->lag, &lag_work->info.bonding_info);
1671		break;
1672	case NETDEV_UNREGISTER:
1673		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1674			netdev = lag_work->info.bonding_info.info.dev;
1675			if ((netdev == lag_work->lag->netdev ||
1676			     lag_work->lag->primary) && lag_work->lag->bonded)
1677				ice_lag_unregister(lag_work->lag, netdev);
1678		}
1679		break;
1680	default:
1681		break;
1682	}
1683
1684lag_cleanup:
1685	/* cleanup resources allocated for this work item */
1686	list_for_each_safe(tmp, n, &lag_work->netdev_list.node) {
1687		struct ice_lag_netdev_list *entry;
1688
1689		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
1690		list_del(&entry->node);
1691		kfree(entry);
1692	}
1693	lag_work->lag->netdev_head = NULL;
1694
1695	mutex_unlock(&pf->lag_mutex);
1696
1697	kfree(lag_work);
1698}
1699
1700/**
1701 * ice_lag_event_handler - handle LAG events from netdev
1702 * @notif_blk: notifier block registered by this netdev
1703 * @event: event type
1704 * @ptr: opaque data containing notifier event
1705 */
1706static int
1707ice_lag_event_handler(struct notifier_block *notif_blk, unsigned long event,
1708		      void *ptr)
1709{
1710	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1711	struct net_device *upper_netdev;
1712	struct ice_lag_work *lag_work;
1713	struct ice_lag *lag;
1714
1715	if (!netif_is_ice(netdev))
1716		return NOTIFY_DONE;
1717
1718	if (event != NETDEV_CHANGEUPPER && event != NETDEV_BONDING_INFO &&
1719	    event != NETDEV_UNREGISTER)
1720		return NOTIFY_DONE;
1721
1722	if (!(netdev->priv_flags & IFF_BONDING))
1723		return NOTIFY_DONE;
1724
1725	lag = container_of(notif_blk, struct ice_lag, notif_block);
1726	if (!lag->netdev)
1727		return NOTIFY_DONE;
1728
1729	if (!net_eq(dev_net(netdev), &init_net))
1730		return NOTIFY_DONE;
1731
1732	/* This memory will be freed at the end of ice_lag_process_event */
1733	lag_work = kzalloc(sizeof(*lag_work), GFP_KERNEL);
1734	if (!lag_work)
1735		return -ENOMEM;
1736
1737	lag_work->event_netdev = netdev;
1738	lag_work->lag = lag;
1739	lag_work->event = event;
1740	if (event == NETDEV_CHANGEUPPER) {
1741		struct netdev_notifier_changeupper_info *info;
1742
1743		info = ptr;
1744		upper_netdev = info->upper_dev;
1745	} else {
1746		upper_netdev = netdev_master_upper_dev_get(netdev);
1747	}
1748
1749	INIT_LIST_HEAD(&lag_work->netdev_list.node);
1750	if (upper_netdev) {
1751		struct ice_lag_netdev_list *nd_list;
1752		struct net_device *tmp_nd;
1753
1754		rcu_read_lock();
1755		for_each_netdev_in_bond_rcu(upper_netdev, tmp_nd) {
1756			nd_list = kzalloc(sizeof(*nd_list), GFP_ATOMIC);
1757			if (!nd_list)
1758				break;
1759
1760			nd_list->netdev = tmp_nd;
1761			list_add(&nd_list->node, &lag_work->netdev_list.node);
1762		}
1763		rcu_read_unlock();
1764	}
1765
1766	switch (event) {
1767	case NETDEV_CHANGEUPPER:
1768		lag_work->info.changeupper_info =
1769			*((struct netdev_notifier_changeupper_info *)ptr);
1770		break;
1771	case NETDEV_BONDING_INFO:
1772		lag_work->info.bonding_info =
1773			*((struct netdev_notifier_bonding_info *)ptr);
1774		break;
1775	default:
1776		lag_work->info.notifier_info =
1777			*((struct netdev_notifier_info *)ptr);
1778		break;
1779	}
1780
1781	INIT_WORK(&lag_work->lag_task, ice_lag_process_event);
1782	queue_work(ice_lag_wq, &lag_work->lag_task);
1783
1784	return NOTIFY_DONE;
1785}
1786
1787/**
1788 * ice_register_lag_handler - register LAG handler on netdev
1789 * @lag: LAG struct
1790 */
1791static int ice_register_lag_handler(struct ice_lag *lag)
1792{
1793	struct device *dev = ice_pf_to_dev(lag->pf);
1794	struct notifier_block *notif_blk;
1795
1796	notif_blk = &lag->notif_block;
1797
1798	if (!notif_blk->notifier_call) {
1799		notif_blk->notifier_call = ice_lag_event_handler;
1800		if (register_netdevice_notifier(notif_blk)) {
1801			notif_blk->notifier_call = NULL;
1802			dev_err(dev, "FAIL register LAG event handler!\n");
1803			return -EINVAL;
1804		}
1805		dev_dbg(dev, "LAG event handler registered\n");
1806	}
1807	return 0;
1808}
1809
1810/**
1811 * ice_unregister_lag_handler - unregister LAG handler on netdev
1812 * @lag: LAG struct
1813 */
1814static void ice_unregister_lag_handler(struct ice_lag *lag)
1815{
1816	struct device *dev = ice_pf_to_dev(lag->pf);
1817	struct notifier_block *notif_blk;
1818
1819	notif_blk = &lag->notif_block;
1820	if (notif_blk->notifier_call) {
1821		unregister_netdevice_notifier(notif_blk);
1822		dev_dbg(dev, "LAG event handler unregistered\n");
1823	}
1824}
1825
1826/**
1827 * ice_create_lag_recipe
1828 * @hw: pointer to HW struct
1829 * @rid: pointer to u16 to pass back recipe index
1830 * @base_recipe: recipe to base the new recipe on
1831 * @prio: priority for new recipe
1832 *
1833 * function returns 0 on error
1834 */
1835static int ice_create_lag_recipe(struct ice_hw *hw, u16 *rid,
1836				 const u8 *base_recipe, u8 prio)
1837{
1838	struct ice_aqc_recipe_data_elem *new_rcp;
1839	int err;
1840
1841	err = ice_alloc_recipe(hw, rid);
1842	if (err)
1843		return err;
1844
1845	new_rcp = kzalloc(ICE_RECIPE_LEN * ICE_MAX_NUM_RECIPES, GFP_KERNEL);
1846	if (!new_rcp)
1847		return -ENOMEM;
1848
1849	memcpy(new_rcp, base_recipe, ICE_RECIPE_LEN);
1850	new_rcp->content.act_ctrl_fwd_priority = prio;
1851	new_rcp->content.rid = *rid | ICE_AQ_RECIPE_ID_IS_ROOT;
1852	new_rcp->recipe_indx = *rid;
1853	bitmap_zero((unsigned long *)new_rcp->recipe_bitmap,
1854		    ICE_MAX_NUM_RECIPES);
1855	set_bit(*rid, (unsigned long *)new_rcp->recipe_bitmap);
1856
1857	err = ice_aq_add_recipe(hw, new_rcp, 1, NULL);
1858	if (err)
1859		*rid = 0;
1860
1861	kfree(new_rcp);
1862	return err;
1863}
1864
1865/**
1866 * ice_lag_move_vf_nodes_tc_sync - move a VF's nodes for a tc during reset
1867 * @lag: primary interfaces lag struct
1868 * @dest_hw: HW struct for destination's interface
1869 * @vsi_num: VSI index in PF space
1870 * @tc: traffic class to move
1871 */
1872static void
1873ice_lag_move_vf_nodes_tc_sync(struct ice_lag *lag, struct ice_hw *dest_hw,
1874			      u16 vsi_num, u8 tc)
1875{
1876	DEFINE_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
1877	struct device *dev = ice_pf_to_dev(lag->pf);
1878	u16 numq, valq, num_moved, qbuf_size;
1879	u16 buf_size = __struct_size(buf);
1880	struct ice_aqc_cfg_txqs_buf *qbuf;
1881	struct ice_sched_node *n_prt;
1882	__le32 teid, parent_teid;
1883	struct ice_vsi_ctx *ctx;
1884	struct ice_hw *hw;
1885	u32 tmp_teid;
1886
1887	hw = &lag->pf->hw;
1888	ctx = ice_get_vsi_ctx(hw, vsi_num);
1889	if (!ctx) {
1890		dev_warn(dev, "LAG rebuild failed after reset due to VSI Context failure\n");
1891		return;
1892	}
1893
1894	if (!ctx->sched.vsi_node[tc])
1895		return;
1896
1897	numq = ctx->num_lan_q_entries[tc];
1898	teid = ctx->sched.vsi_node[tc]->info.node_teid;
1899	tmp_teid = le32_to_cpu(teid);
1900	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
1901
1902	if (!tmp_teid || !numq)
1903		return;
1904
1905	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, true))
1906		dev_dbg(dev, "Problem suspending traffic during reset rebuild\n");
1907
1908	/* reconfig queues for new port */
1909	qbuf_size = struct_size(qbuf, queue_info, numq);
1910	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
1911	if (!qbuf) {
1912		dev_warn(dev, "Failure allocating VF queue recfg buffer for reset rebuild\n");
1913		goto resume_sync;
1914	}
1915
1916	/* add the per queue info for the reconfigure command buffer */
1917	valq = ice_lag_qbuf_recfg(hw, qbuf, vsi_num, numq, tc);
1918	if (!valq) {
1919		dev_warn(dev, "Failure to reconfig queues for LAG reset rebuild\n");
1920		goto sync_none;
1921	}
1922
1923	if (ice_aq_cfg_lan_txq(hw, qbuf, qbuf_size, numq, hw->port_info->lport,
1924			       dest_hw->port_info->lport, NULL)) {
1925		dev_warn(dev, "Failure to configure queues for LAG reset rebuild\n");
1926		goto sync_qerr;
1927	}
1928
1929sync_none:
1930	kfree(qbuf);
1931
1932	/* find parent in destination tree */
1933	n_prt = ice_lag_get_sched_parent(dest_hw, tc);
1934	if (!n_prt)
1935		goto resume_sync;
1936
1937	/* Move node to new parent */
1938	buf->hdr.src_parent_teid = parent_teid;
1939	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
1940	buf->hdr.num_elems = cpu_to_le16(1);
1941	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
1942	buf->teid[0] = teid;
1943
1944	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
1945		dev_warn(dev, "Failure to move VF nodes for LAG reset rebuild\n");
1946	else
1947		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
1948
1949	goto resume_sync;
1950
1951sync_qerr:
1952	kfree(qbuf);
1953
1954resume_sync:
1955	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, false))
1956		dev_warn(dev, "Problem restarting traffic for LAG node reset rebuild\n");
1957}
1958
1959/**
1960 * ice_lag_move_vf_nodes_sync - move vf nodes to active interface
1961 * @lag: primary interfaces lag struct
1962 * @dest_hw: lport value for currently active port
1963 *
1964 * This function is used in a reset context, outside of event handling,
1965 * to move the VF nodes to the secondary interface when that interface
1966 * is the active interface during a reset rebuild
1967 */
1968static void
1969ice_lag_move_vf_nodes_sync(struct ice_lag *lag, struct ice_hw *dest_hw)
1970{
1971	struct ice_pf *pf;
1972	int i, tc;
1973
1974	if (!lag->primary || !dest_hw)
1975		return;
1976
1977	pf = lag->pf;
1978	ice_for_each_vsi(pf, i)
1979		if (pf->vsi[i] && (pf->vsi[i]->type == ICE_VSI_VF ||
1980				   pf->vsi[i]->type == ICE_VSI_SWITCHDEV_CTRL))
1981			ice_for_each_traffic_class(tc)
1982				ice_lag_move_vf_nodes_tc_sync(lag, dest_hw, i,
1983							      tc);
1984}
1985
1986/**
1987 * ice_init_lag - initialize support for LAG
1988 * @pf: PF struct
1989 *
1990 * Alloc memory for LAG structs and initialize the elements.
1991 * Memory will be freed in ice_deinit_lag
1992 */
1993int ice_init_lag(struct ice_pf *pf)
1994{
1995	struct device *dev = ice_pf_to_dev(pf);
1996	struct ice_lag *lag;
1997	struct ice_vsi *vsi;
1998	u64 recipe_bits = 0;
1999	int n, err;
2000
2001	ice_lag_init_feature_support_flag(pf);
2002	if (!ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
2003		return 0;
2004
2005	pf->lag = kzalloc(sizeof(*lag), GFP_KERNEL);
2006	if (!pf->lag)
2007		return -ENOMEM;
2008	lag = pf->lag;
2009
2010	vsi = ice_get_main_vsi(pf);
2011	if (!vsi) {
2012		dev_err(dev, "couldn't get main vsi, link aggregation init fail\n");
2013		err = -EIO;
2014		goto lag_error;
2015	}
2016
2017	lag->pf = pf;
2018	lag->netdev = vsi->netdev;
2019	lag->role = ICE_LAG_NONE;
2020	lag->active_port = ICE_LAG_INVALID_PORT;
2021	lag->bonded = false;
2022	lag->upper_netdev = NULL;
2023	lag->notif_block.notifier_call = NULL;
2024
2025	err = ice_register_lag_handler(lag);
2026	if (err) {
2027		dev_warn(dev, "INIT LAG: Failed to register event handler\n");
2028		goto lag_error;
2029	}
2030
2031	err = ice_create_lag_recipe(&pf->hw, &lag->pf_recipe,
2032				    ice_dflt_vsi_rcp, 1);
2033	if (err)
2034		goto lag_error;
2035
2036	err = ice_create_lag_recipe(&pf->hw, &lag->lport_recipe,
2037				    ice_lport_rcp, 3);
2038	if (err)
2039		goto free_rcp_res;
2040
2041	/* associate recipes to profiles */
2042	for (n = 0; n < ICE_PROFID_IPV6_GTPU_IPV6_TCP_INNER; n++) {
2043		err = ice_aq_get_recipe_to_profile(&pf->hw, n,
2044						   (u8 *)&recipe_bits, NULL);
2045		if (err)
2046			continue;
2047
2048		if (recipe_bits & BIT(ICE_SW_LKUP_DFLT)) {
2049			recipe_bits |= BIT(lag->pf_recipe) |
2050				       BIT(lag->lport_recipe);
2051			ice_aq_map_recipe_to_profile(&pf->hw, n,
2052						     (u8 *)&recipe_bits, NULL);
2053		}
2054	}
2055
2056	ice_display_lag_info(lag);
2057
2058	dev_dbg(dev, "INIT LAG complete\n");
2059	return 0;
2060
2061free_rcp_res:
2062	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2063			&pf->lag->pf_recipe);
2064lag_error:
2065	kfree(lag);
2066	pf->lag = NULL;
2067	return err;
2068}
2069
2070/**
2071 * ice_deinit_lag - Clean up LAG
2072 * @pf: PF struct
2073 *
2074 * Clean up kernel LAG info and free memory
2075 * This function is meant to only be called on driver remove/shutdown
2076 */
2077void ice_deinit_lag(struct ice_pf *pf)
2078{
2079	struct ice_lag *lag;
2080
2081	lag = pf->lag;
2082
2083	if (!lag)
2084		return;
2085
2086	if (lag->pf)
2087		ice_unregister_lag_handler(lag);
2088
2089	flush_workqueue(ice_lag_wq);
2090
2091	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2092			&pf->lag->pf_recipe);
2093	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2094			&pf->lag->lport_recipe);
2095
2096	kfree(lag);
2097
2098	pf->lag = NULL;
2099}
2100
2101/**
2102 * ice_lag_rebuild - rebuild lag resources after reset
2103 * @pf: pointer to local pf struct
2104 *
2105 * PF resets are promoted to CORER resets when interface in an aggregate.  This
2106 * means that we need to rebuild the PF resources for the interface.  Since
2107 * this will happen outside the normal event processing, need to acquire the lag
2108 * lock.
2109 *
2110 * This function will also evaluate the VF resources if this is the primary
2111 * interface.
2112 */
2113void ice_lag_rebuild(struct ice_pf *pf)
2114{
2115	struct ice_lag_netdev_list ndlist;
2116	struct ice_lag *lag, *prim_lag;
2117	u8 act_port, loc_port;
2118
2119	if (!pf->lag || !pf->lag->bonded)
2120		return;
2121
2122	mutex_lock(&pf->lag_mutex);
2123
2124	lag = pf->lag;
2125	if (lag->primary) {
2126		prim_lag = lag;
2127	} else {
2128		ice_lag_build_netdev_list(lag, &ndlist);
2129		prim_lag = ice_lag_find_primary(lag);
2130	}
2131
2132	if (!prim_lag) {
2133		dev_dbg(ice_pf_to_dev(pf), "No primary interface in aggregate, can't rebuild\n");
2134		goto lag_rebuild_out;
2135	}
2136
2137	act_port = prim_lag->active_port;
2138	loc_port = lag->pf->hw.port_info->lport;
2139
2140	/* configure SWID for this port */
2141	if (lag->primary) {
2142		ice_lag_primary_swid(lag, true);
2143	} else {
2144		ice_lag_set_swid(prim_lag->pf->hw.port_info->sw_id, lag, true);
2145		ice_lag_add_prune_list(prim_lag, pf);
2146		if (act_port == loc_port)
2147			ice_lag_move_vf_nodes_sync(prim_lag, &pf->hw);
2148	}
2149
2150	ice_lag_cfg_cp_fltr(lag, true);
2151
2152	if (lag->pf_rule_id)
2153		if (ice_lag_cfg_dflt_fltr(lag, true))
2154			dev_err(ice_pf_to_dev(pf), "Error adding default VSI rule in rebuild\n");
2155
2156	ice_clear_rdma_cap(pf);
2157lag_rebuild_out:
2158	ice_lag_destroy_netdev_list(lag, &ndlist);
2159	mutex_unlock(&pf->lag_mutex);
2160}
2161
2162/**
2163 * ice_lag_is_switchdev_running
2164 * @pf: pointer to PF structure
2165 *
2166 * Check if switchdev is running on any of the interfaces connected to lag.
2167 */
2168bool ice_lag_is_switchdev_running(struct ice_pf *pf)
2169{
2170	struct ice_lag *lag = pf->lag;
2171	struct net_device *tmp_nd;
2172
2173	if (!ice_is_feature_supported(pf, ICE_F_SRIOV_LAG) || !lag)
2174		return false;
2175
2176	rcu_read_lock();
2177	for_each_netdev_in_bond_rcu(lag->upper_netdev, tmp_nd) {
2178		struct ice_netdev_priv *priv = netdev_priv(tmp_nd);
2179
2180		if (!netif_is_ice(tmp_nd) || !priv || !priv->vsi ||
2181		    !priv->vsi->back)
2182			continue;
2183
2184		if (ice_is_switchdev_running(priv->vsi->back)) {
2185			rcu_read_unlock();
2186			return true;
2187		}
2188	}
2189	rcu_read_unlock();
2190
2191	return false;
2192}