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