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