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