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