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