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