xref: /linux/drivers/net/ethernet/intel/ice/ice_lag.c (revision ef2233850edc4cc0d5fc6136fcdb004a1ddfa7db)
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_info_event - handle NETDEV_BONDING_INFO event
827  * @lag: LAG info struct
828  * @ptr: opaque data pointer
829  *
830  * ptr is to be cast to (netdev_notifier_bonding_info *)
831  */
ice_lag_info_event(struct ice_lag * lag,void * ptr)832 static void ice_lag_info_event(struct ice_lag *lag, void *ptr)
833 {
834 	struct netdev_notifier_bonding_info *info;
835 	struct netdev_bonding_info *bonding_info;
836 	struct net_device *event_netdev;
837 	const char *lag_netdev_name;
838 
839 	event_netdev = netdev_notifier_info_to_dev(ptr);
840 	info = ptr;
841 	lag_netdev_name = netdev_name(lag->netdev);
842 	bonding_info = &info->bonding_info;
843 
844 	if (event_netdev != lag->netdev || !lag->bonded || !lag->upper_netdev)
845 		return;
846 
847 	if (bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) {
848 		netdev_dbg(lag->netdev, "Bonding event recv, but mode not active/backup\n");
849 		goto lag_out;
850 	}
851 
852 	if (strcmp(bonding_info->slave.slave_name, lag_netdev_name)) {
853 		netdev_dbg(lag->netdev, "Bonding event recv, but secondary info not for us\n");
854 		goto lag_out;
855 	}
856 
857 	if (bonding_info->slave.state)
858 		ice_lag_set_backup(lag);
859 	else
860 		ice_lag_set_primary(lag);
861 
862 lag_out:
863 	ice_display_lag_info(lag);
864 }
865 
866 /**
867  * ice_lag_reclaim_vf_tc - move scheduling nodes back to primary interface
868  * @lag: primary interface lag struct
869  * @src_hw: HW struct current node location
870  * @vsi_num: VSI index in PF space
871  * @tc: traffic class to move
872  */
873 static void
ice_lag_reclaim_vf_tc(struct ice_lag * lag,struct ice_hw * src_hw,u16 vsi_num,u8 tc)874 ice_lag_reclaim_vf_tc(struct ice_lag *lag, struct ice_hw *src_hw, u16 vsi_num,
875 		      u8 tc)
876 {
877 	DEFINE_RAW_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
878 	struct device *dev = ice_pf_to_dev(lag->pf);
879 	u16 numq, valq, num_moved, qbuf_size;
880 	u16 buf_size = __struct_size(buf);
881 	struct ice_aqc_cfg_txqs_buf *qbuf;
882 	struct ice_sched_node *n_prt;
883 	__le32 teid, parent_teid;
884 	struct ice_vsi_ctx *ctx;
885 	struct ice_hw *hw;
886 	u32 tmp_teid;
887 
888 	hw = &lag->pf->hw;
889 	ctx = ice_get_vsi_ctx(hw, vsi_num);
890 	if (!ctx) {
891 		dev_warn(dev, "Unable to locate VSI context for LAG reclaim\n");
892 		return;
893 	}
894 
895 	/* check to see if this VF is enabled on this TC */
896 	if (!ctx->sched.vsi_node[tc])
897 		return;
898 
899 	numq = ctx->num_lan_q_entries[tc];
900 	teid = ctx->sched.vsi_node[tc]->info.node_teid;
901 	tmp_teid = le32_to_cpu(teid);
902 	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
903 
904 	/* if !teid or !numq, then this TC is not active */
905 	if (!tmp_teid || !numq)
906 		return;
907 
908 	/* suspend traffic */
909 	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, true))
910 		dev_dbg(dev, "Problem suspending traffic for LAG node move\n");
911 
912 	/* reconfig queues for new port */
913 	qbuf_size = struct_size(qbuf, queue_info, numq);
914 	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
915 	if (!qbuf) {
916 		dev_warn(dev, "Failure allocating memory for VF queue recfg buffer\n");
917 		goto resume_reclaim;
918 	}
919 
920 	/* add the per queue info for the reconfigure command buffer */
921 	valq = ice_lag_qbuf_recfg(hw, qbuf, vsi_num, numq, tc);
922 	if (!valq) {
923 		dev_dbg(dev, "No valid queues found for LAG reclaim\n");
924 		goto reclaim_none;
925 	}
926 
927 	if (ice_aq_cfg_lan_txq(hw, qbuf, qbuf_size, numq,
928 			       src_hw->port_info->lport, hw->port_info->lport,
929 			       NULL)) {
930 		dev_warn(dev, "Failure to configure queues for LAG failover\n");
931 		goto reclaim_qerr;
932 	}
933 
934 reclaim_none:
935 	kfree(qbuf);
936 
937 	/* find parent in primary tree */
938 	n_prt = ice_lag_get_sched_parent(hw, tc);
939 	if (!n_prt)
940 		goto resume_reclaim;
941 
942 	/* Move node to new parent */
943 	buf->hdr.src_parent_teid = parent_teid;
944 	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
945 	buf->hdr.num_elems = cpu_to_le16(1);
946 	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
947 	buf->teid[0] = teid;
948 
949 	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
950 		dev_warn(dev, "Failure to move VF nodes for LAG reclaim\n");
951 	else
952 		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
953 
954 	goto resume_reclaim;
955 
956 reclaim_qerr:
957 	kfree(qbuf);
958 
959 resume_reclaim:
960 	/* restart traffic */
961 	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, false))
962 		dev_warn(dev, "Problem restarting traffic for LAG node reclaim\n");
963 }
964 
965 /**
966  * ice_lag_reclaim_vf_nodes - When interface leaving bond primary reclaims nodes
967  * @lag: primary interface lag struct
968  * @src_hw: HW struct for current node location
969  */
970 static void
ice_lag_reclaim_vf_nodes(struct ice_lag * lag,struct ice_hw * src_hw)971 ice_lag_reclaim_vf_nodes(struct ice_lag *lag, struct ice_hw *src_hw)
972 {
973 	struct ice_pf *pf;
974 	int i, tc;
975 
976 	if (!lag->primary || !src_hw)
977 		return;
978 
979 	pf = lag->pf;
980 	ice_for_each_vsi(pf, i)
981 		if (pf->vsi[i] && pf->vsi[i]->type == ICE_VSI_VF)
982 			ice_for_each_traffic_class(tc)
983 				ice_lag_reclaim_vf_tc(lag, src_hw, i, tc);
984 }
985 
986 /**
987  * ice_lag_link - handle LAG link event
988  * @lag: LAG info struct
989  */
ice_lag_link(struct ice_lag * lag)990 static void ice_lag_link(struct ice_lag *lag)
991 {
992 	struct ice_pf *pf = lag->pf;
993 
994 	if (lag->bonded)
995 		dev_warn(ice_pf_to_dev(pf), "%s Already part of a bond\n",
996 			 netdev_name(lag->netdev));
997 
998 	lag->bonded = true;
999 	lag->role = ICE_LAG_UNSET;
1000 	netdev_info(lag->netdev, "Shared SR-IOV resources in bond are active\n");
1001 }
1002 
1003 /**
1004  * ice_lag_config_eswitch - configure eswitch to work with LAG
1005  * @lag: lag info struct
1006  * @netdev: active network interface device struct
1007  *
1008  * Updates all port representors in eswitch to use @netdev for Tx.
1009  *
1010  * Configures the netdev to keep dst metadata (also used in representor Tx).
1011  * This is required for an uplink without switchdev mode configured.
1012  */
ice_lag_config_eswitch(struct ice_lag * lag,struct net_device * netdev)1013 static void ice_lag_config_eswitch(struct ice_lag *lag,
1014 				   struct net_device *netdev)
1015 {
1016 	struct ice_repr *repr;
1017 	unsigned long id;
1018 
1019 	xa_for_each(&lag->pf->eswitch.reprs, id, repr)
1020 		repr->dst->u.port_info.lower_dev = netdev;
1021 
1022 	netif_keep_dst(netdev);
1023 }
1024 
1025 /**
1026  * ice_lag_unlink - handle unlink event
1027  * @lag: LAG info struct
1028  */
ice_lag_unlink(struct ice_lag * lag)1029 static void ice_lag_unlink(struct ice_lag *lag)
1030 {
1031 	u8 pri_port, act_port, loc_port;
1032 	struct ice_pf *pf = lag->pf;
1033 
1034 	if (!lag->bonded) {
1035 		netdev_dbg(lag->netdev, "bonding unlink event on non-LAG netdev\n");
1036 		return;
1037 	}
1038 
1039 	if (lag->primary) {
1040 		act_port = lag->active_port;
1041 		pri_port = lag->pf->hw.port_info->lport;
1042 		if (act_port != pri_port && act_port != ICE_LAG_INVALID_PORT)
1043 			ice_lag_move_vf_nodes(lag, act_port, pri_port);
1044 		lag->primary = false;
1045 		lag->active_port = ICE_LAG_INVALID_PORT;
1046 
1047 		/* Config primary's eswitch back to normal operation. */
1048 		ice_lag_config_eswitch(lag, lag->netdev);
1049 	} else {
1050 		struct ice_lag *primary_lag;
1051 
1052 		primary_lag = ice_lag_find_primary(lag);
1053 		if (primary_lag) {
1054 			act_port = primary_lag->active_port;
1055 			pri_port = primary_lag->pf->hw.port_info->lport;
1056 			loc_port = pf->hw.port_info->lport;
1057 			if (act_port == loc_port &&
1058 			    act_port != ICE_LAG_INVALID_PORT) {
1059 				ice_lag_reclaim_vf_nodes(primary_lag,
1060 							 &lag->pf->hw);
1061 				primary_lag->active_port = ICE_LAG_INVALID_PORT;
1062 			}
1063 		}
1064 	}
1065 
1066 	lag->bonded = false;
1067 	lag->role = ICE_LAG_NONE;
1068 	lag->upper_netdev = NULL;
1069 }
1070 
1071 /**
1072  * ice_lag_link_unlink - helper function to call lag_link/unlink
1073  * @lag: lag info struct
1074  * @ptr: opaque pointer data
1075  */
ice_lag_link_unlink(struct ice_lag * lag,void * ptr)1076 static void ice_lag_link_unlink(struct ice_lag *lag, void *ptr)
1077 {
1078 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1079 	struct netdev_notifier_changeupper_info *info = ptr;
1080 
1081 	if (netdev != lag->netdev)
1082 		return;
1083 
1084 	if (info->linking)
1085 		ice_lag_link(lag);
1086 	else
1087 		ice_lag_unlink(lag);
1088 }
1089 
1090 /**
1091  * ice_lag_set_swid - set the SWID on secondary interface
1092  * @primary_swid: primary interface's SWID
1093  * @local_lag: local interfaces LAG struct
1094  * @link: Is this a linking activity
1095  *
1096  * If link is false, then primary_swid should be expected to not be valid
1097  * This function should never be called in interrupt context.
1098  */
1099 static void
ice_lag_set_swid(u16 primary_swid,struct ice_lag * local_lag,bool link)1100 ice_lag_set_swid(u16 primary_swid, struct ice_lag *local_lag,
1101 		 bool link)
1102 {
1103 	struct ice_aqc_alloc_free_res_elem *buf;
1104 	struct ice_aqc_set_port_params *cmd;
1105 	struct ice_aq_desc desc;
1106 	u16 buf_len, swid;
1107 	int status, i;
1108 
1109 	buf_len = struct_size(buf, elem, 1);
1110 	buf = kzalloc(buf_len, GFP_KERNEL);
1111 	if (!buf) {
1112 		dev_err(ice_pf_to_dev(local_lag->pf), "-ENOMEM error setting SWID\n");
1113 		return;
1114 	}
1115 
1116 	buf->num_elems = cpu_to_le16(1);
1117 	buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_SWID);
1118 	/* if unlinnking need to free the shared resource */
1119 	if (!link && local_lag->bond_swid) {
1120 		buf->elem[0].e.sw_resp = cpu_to_le16(local_lag->bond_swid);
1121 		status = ice_aq_alloc_free_res(&local_lag->pf->hw, buf,
1122 					       buf_len, ice_aqc_opc_free_res);
1123 		if (status)
1124 			dev_err(ice_pf_to_dev(local_lag->pf), "Error freeing SWID during LAG unlink\n");
1125 		local_lag->bond_swid = 0;
1126 	}
1127 
1128 	if (link) {
1129 		buf->res_type |=  cpu_to_le16(ICE_LAG_RES_SHARED |
1130 					      ICE_LAG_RES_VALID);
1131 		/* store the primary's SWID in case it leaves bond first */
1132 		local_lag->bond_swid = primary_swid;
1133 		buf->elem[0].e.sw_resp = cpu_to_le16(local_lag->bond_swid);
1134 	} else {
1135 		buf->elem[0].e.sw_resp =
1136 			cpu_to_le16(local_lag->pf->hw.port_info->sw_id);
1137 	}
1138 
1139 	status = ice_aq_alloc_free_res(&local_lag->pf->hw, buf, buf_len,
1140 				       ice_aqc_opc_alloc_res);
1141 	if (status)
1142 		dev_err(ice_pf_to_dev(local_lag->pf), "Error subscribing to SWID 0x%04X\n",
1143 			local_lag->bond_swid);
1144 
1145 	kfree(buf);
1146 
1147 	/* Configure port param SWID to correct value */
1148 	if (link)
1149 		swid = primary_swid;
1150 	else
1151 		swid = local_lag->pf->hw.port_info->sw_id;
1152 
1153 	cmd = &desc.params.set_port_params;
1154 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_set_port_params);
1155 
1156 	cmd->swid = cpu_to_le16(ICE_AQC_PORT_SWID_VALID | swid);
1157 	/* If this is happening in reset context, it is possible that the
1158 	 * primary interface has not finished setting its SWID to SHARED
1159 	 * yet.  Allow retries to account for this timing issue between
1160 	 * interfaces.
1161 	 */
1162 	for (i = 0; i < ICE_LAG_RESET_RETRIES; i++) {
1163 		status = ice_aq_send_cmd(&local_lag->pf->hw, &desc, NULL, 0,
1164 					 NULL);
1165 		if (!status)
1166 			break;
1167 
1168 		usleep_range(1000, 2000);
1169 	}
1170 
1171 	if (status)
1172 		dev_err(ice_pf_to_dev(local_lag->pf), "Error setting SWID in port params %d\n",
1173 			status);
1174 }
1175 
1176 /**
1177  * ice_lag_primary_swid - set/clear the SHARED attrib of primary's SWID
1178  * @lag: primary interface's lag struct
1179  * @link: is this a linking activity
1180  *
1181  * Implement setting primary SWID as shared using 0x020B
1182  */
ice_lag_primary_swid(struct ice_lag * lag,bool link)1183 static void ice_lag_primary_swid(struct ice_lag *lag, bool link)
1184 {
1185 	struct ice_hw *hw;
1186 	u16 swid;
1187 
1188 	hw = &lag->pf->hw;
1189 	swid = hw->port_info->sw_id;
1190 
1191 	if (ice_share_res(hw, ICE_AQC_RES_TYPE_SWID, link, swid))
1192 		dev_warn(ice_pf_to_dev(lag->pf), "Failure to set primary interface shared status\n");
1193 }
1194 
1195 /**
1196  * ice_lag_add_prune_list - Adds event_pf's VSI to primary's prune list
1197  * @lag: lag info struct
1198  * @event_pf: PF struct for VSI we are adding to primary's prune list
1199  */
ice_lag_add_prune_list(struct ice_lag * lag,struct ice_pf * event_pf)1200 static void ice_lag_add_prune_list(struct ice_lag *lag, struct ice_pf *event_pf)
1201 {
1202 	u16 num_vsi, rule_buf_sz, vsi_list_id, event_vsi_num, prim_vsi_idx;
1203 	struct ice_sw_rule_vsi_list *s_rule = NULL;
1204 	struct device *dev;
1205 
1206 	num_vsi = 1;
1207 
1208 	dev = ice_pf_to_dev(lag->pf);
1209 	event_vsi_num = event_pf->vsi[0]->vsi_num;
1210 	prim_vsi_idx = lag->pf->vsi[0]->idx;
1211 
1212 	if (!ice_find_vsi_list_entry(&lag->pf->hw, ICE_SW_LKUP_VLAN,
1213 				     prim_vsi_idx, &vsi_list_id)) {
1214 		dev_warn(dev, "Could not locate prune list when setting up SRIOV LAG\n");
1215 		return;
1216 	}
1217 
1218 	rule_buf_sz = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
1219 	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
1220 	if (!s_rule) {
1221 		dev_warn(dev, "Error allocating space for prune list when configuring SRIOV LAG\n");
1222 		return;
1223 	}
1224 
1225 	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_PRUNE_LIST_SET);
1226 	s_rule->index = cpu_to_le16(vsi_list_id);
1227 	s_rule->number_vsi = cpu_to_le16(num_vsi);
1228 	s_rule->vsi[0] = cpu_to_le16(event_vsi_num);
1229 
1230 	if (ice_aq_sw_rules(&event_pf->hw, s_rule, rule_buf_sz, 1,
1231 			    ice_aqc_opc_update_sw_rules, NULL))
1232 		dev_warn(dev, "Error adding VSI prune list\n");
1233 	kfree(s_rule);
1234 }
1235 
1236 /**
1237  * ice_lag_del_prune_list - Remove secondary's vsi from primary's prune list
1238  * @lag: primary interface's ice_lag struct
1239  * @event_pf: PF struct for unlinking interface
1240  */
ice_lag_del_prune_list(struct ice_lag * lag,struct ice_pf * event_pf)1241 static void ice_lag_del_prune_list(struct ice_lag *lag, struct ice_pf *event_pf)
1242 {
1243 	u16 num_vsi, vsi_num, vsi_idx, rule_buf_sz, vsi_list_id;
1244 	struct ice_sw_rule_vsi_list *s_rule = NULL;
1245 	struct device *dev;
1246 
1247 	num_vsi = 1;
1248 
1249 	dev = ice_pf_to_dev(lag->pf);
1250 	vsi_num = event_pf->vsi[0]->vsi_num;
1251 	vsi_idx = lag->pf->vsi[0]->idx;
1252 
1253 	if (!ice_find_vsi_list_entry(&lag->pf->hw, ICE_SW_LKUP_VLAN,
1254 				     vsi_idx, &vsi_list_id)) {
1255 		dev_warn(dev, "Could not locate prune list when unwinding SRIOV LAG\n");
1256 		return;
1257 	}
1258 
1259 	rule_buf_sz = (u16)ICE_SW_RULE_VSI_LIST_SIZE(s_rule, num_vsi);
1260 	s_rule = kzalloc(rule_buf_sz, GFP_KERNEL);
1261 	if (!s_rule) {
1262 		dev_warn(dev, "Error allocating prune list when unwinding SRIOV LAG\n");
1263 		return;
1264 	}
1265 
1266 	s_rule->hdr.type = cpu_to_le16(ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR);
1267 	s_rule->index = cpu_to_le16(vsi_list_id);
1268 	s_rule->number_vsi = cpu_to_le16(num_vsi);
1269 	s_rule->vsi[0] = cpu_to_le16(vsi_num);
1270 
1271 	if (ice_aq_sw_rules(&event_pf->hw, (struct ice_aqc_sw_rules *)s_rule,
1272 			    rule_buf_sz, 1, ice_aqc_opc_update_sw_rules, NULL))
1273 		dev_warn(dev, "Error clearing VSI prune list\n");
1274 
1275 	kfree(s_rule);
1276 }
1277 
1278 /**
1279  * ice_lag_init_feature_support_flag - Check for package and NVM support for LAG
1280  * @pf: PF struct
1281  */
ice_lag_init_feature_support_flag(struct ice_pf * pf)1282 static void ice_lag_init_feature_support_flag(struct ice_pf *pf)
1283 {
1284 	struct ice_hw_common_caps *caps;
1285 
1286 	caps = &pf->hw.dev_caps.common_cap;
1287 	if (caps->roce_lag)
1288 		ice_set_feature_support(pf, ICE_F_ROCE_LAG);
1289 	else
1290 		ice_clear_feature_support(pf, ICE_F_ROCE_LAG);
1291 
1292 	if (caps->sriov_lag && ice_pkg_has_lport_extract(&pf->hw))
1293 		ice_set_feature_support(pf, ICE_F_SRIOV_LAG);
1294 	else
1295 		ice_clear_feature_support(pf, ICE_F_SRIOV_LAG);
1296 }
1297 
1298 /**
1299  * ice_lag_changeupper_event - handle LAG changeupper event
1300  * @lag: LAG info struct
1301  * @ptr: opaque pointer data
1302  */
ice_lag_changeupper_event(struct ice_lag * lag,void * ptr)1303 static void ice_lag_changeupper_event(struct ice_lag *lag, void *ptr)
1304 {
1305 	struct netdev_notifier_changeupper_info *info;
1306 	struct ice_lag *primary_lag;
1307 	struct net_device *netdev;
1308 
1309 	info = ptr;
1310 	netdev = netdev_notifier_info_to_dev(ptr);
1311 
1312 	/* not for this netdev */
1313 	if (netdev != lag->netdev)
1314 		return;
1315 
1316 	primary_lag = ice_lag_find_primary(lag);
1317 	if (info->linking) {
1318 		lag->upper_netdev = info->upper_dev;
1319 		/* If there is not already a primary interface in the LAG,
1320 		 * then mark this one as primary.
1321 		 */
1322 		if (!primary_lag) {
1323 			lag->primary = true;
1324 			if (!ice_is_switchdev_running(lag->pf))
1325 				return;
1326 
1327 			/* Configure primary's SWID to be shared */
1328 			ice_lag_primary_swid(lag, true);
1329 			primary_lag = lag;
1330 		} else {
1331 			u16 swid;
1332 
1333 			if (!ice_is_switchdev_running(primary_lag->pf))
1334 				return;
1335 
1336 			swid = primary_lag->pf->hw.port_info->sw_id;
1337 			ice_lag_set_swid(swid, lag, true);
1338 			ice_lag_add_prune_list(primary_lag, lag->pf);
1339 			ice_lag_cfg_drop_fltr(lag, true);
1340 		}
1341 		/* add filter for primary control packets */
1342 		ice_lag_cfg_cp_fltr(lag, true);
1343 	} else {
1344 		if (!primary_lag && lag->primary)
1345 			primary_lag = lag;
1346 
1347 		if (!lag->primary) {
1348 			ice_lag_set_swid(0, lag, false);
1349 		} else {
1350 			if (primary_lag && lag->primary) {
1351 				ice_lag_primary_swid(lag, false);
1352 				ice_lag_del_prune_list(primary_lag, lag->pf);
1353 			}
1354 		}
1355 		/* remove filter for control packets */
1356 		ice_lag_cfg_cp_fltr(lag, false);
1357 	}
1358 }
1359 
1360 /**
1361  * ice_lag_monitor_link - monitor interfaces entering/leaving the aggregate
1362  * @lag: lag info struct
1363  * @ptr: opaque data containing notifier event
1364  *
1365  * This function only operates after a primary has been set.
1366  */
ice_lag_monitor_link(struct ice_lag * lag,void * ptr)1367 static void ice_lag_monitor_link(struct ice_lag *lag, void *ptr)
1368 {
1369 	struct netdev_notifier_changeupper_info *info;
1370 	struct ice_hw *prim_hw, *active_hw;
1371 	struct net_device *event_netdev;
1372 	struct ice_pf *pf;
1373 	u8 prim_port;
1374 
1375 	if (!lag->primary)
1376 		return;
1377 
1378 	event_netdev = netdev_notifier_info_to_dev(ptr);
1379 	if (!netif_is_same_ice(lag->pf, event_netdev))
1380 		return;
1381 
1382 	pf = lag->pf;
1383 	prim_hw = &pf->hw;
1384 	prim_port = prim_hw->port_info->lport;
1385 
1386 	info = (struct netdev_notifier_changeupper_info *)ptr;
1387 	if (info->upper_dev != lag->upper_netdev)
1388 		return;
1389 
1390 	if (!info->linking) {
1391 		/* Since there are only two interfaces allowed in SRIOV+LAG, if
1392 		 * one port is leaving, then nodes need to be on primary
1393 		 * interface.
1394 		 */
1395 		if (prim_port != lag->active_port &&
1396 		    lag->active_port != ICE_LAG_INVALID_PORT) {
1397 			active_hw = ice_lag_find_hw_by_lport(lag,
1398 							     lag->active_port);
1399 			ice_lag_reclaim_vf_nodes(lag, active_hw);
1400 			lag->active_port = ICE_LAG_INVALID_PORT;
1401 		}
1402 	}
1403 }
1404 
1405 /**
1406  * ice_lag_monitor_active - main PF keep track of which port is active
1407  * @lag: lag info struct
1408  * @ptr: opaque data containing notifier event
1409  *
1410  * This function is for the primary PF to monitor changes in which port is
1411  * active and handle changes for SRIOV VF functionality
1412  */
ice_lag_monitor_active(struct ice_lag * lag,void * ptr)1413 static void ice_lag_monitor_active(struct ice_lag *lag, void *ptr)
1414 {
1415 	struct net_device *event_netdev, *event_upper;
1416 	struct netdev_notifier_bonding_info *info;
1417 	struct netdev_bonding_info *bonding_info;
1418 	struct ice_netdev_priv *event_np;
1419 	struct ice_pf *pf, *event_pf;
1420 	u8 prim_port, event_port;
1421 
1422 	if (!lag->primary)
1423 		return;
1424 
1425 	pf = lag->pf;
1426 	if (!pf)
1427 		return;
1428 
1429 	event_netdev = netdev_notifier_info_to_dev(ptr);
1430 	rcu_read_lock();
1431 	event_upper = netdev_master_upper_dev_get_rcu(event_netdev);
1432 	rcu_read_unlock();
1433 	if (!netif_is_ice(event_netdev) || event_upper != lag->upper_netdev)
1434 		return;
1435 
1436 	event_np = netdev_priv(event_netdev);
1437 	event_pf = event_np->vsi->back;
1438 	event_port = event_pf->hw.port_info->lport;
1439 	prim_port = pf->hw.port_info->lport;
1440 
1441 	info = (struct netdev_notifier_bonding_info *)ptr;
1442 	bonding_info = &info->bonding_info;
1443 
1444 	if (!bonding_info->slave.state) {
1445 		/* if no port is currently active, then nodes and filters exist
1446 		 * on primary port, check if we need to move them
1447 		 */
1448 		if (lag->active_port == ICE_LAG_INVALID_PORT) {
1449 			if (event_port != prim_port)
1450 				ice_lag_move_vf_nodes(lag, prim_port,
1451 						      event_port);
1452 			lag->active_port = event_port;
1453 			ice_lag_config_eswitch(lag, event_netdev);
1454 			return;
1455 		}
1456 
1457 		/* active port is already set and is current event port */
1458 		if (lag->active_port == event_port)
1459 			return;
1460 		/* new active port */
1461 		ice_lag_move_vf_nodes(lag, lag->active_port, event_port);
1462 		lag->active_port = event_port;
1463 		ice_lag_config_eswitch(lag, event_netdev);
1464 	} else {
1465 		/* port not set as currently active (e.g. new active port
1466 		 * has already claimed the nodes and filters
1467 		 */
1468 		if (lag->active_port != event_port)
1469 			return;
1470 		/* This is the case when neither port is active (both link down)
1471 		 * Link down on the bond - set active port to invalid and move
1472 		 * nodes and filters back to primary if not already there
1473 		 */
1474 		if (event_port != prim_port)
1475 			ice_lag_move_vf_nodes(lag, event_port, prim_port);
1476 		lag->active_port = ICE_LAG_INVALID_PORT;
1477 	}
1478 }
1479 
1480 /**
1481  * ice_lag_chk_comp - evaluate bonded interface for feature support
1482  * @lag: lag info struct
1483  * @ptr: opaque data for netdev event info
1484  */
1485 static bool
ice_lag_chk_comp(struct ice_lag * lag,void * ptr)1486 ice_lag_chk_comp(struct ice_lag *lag, void *ptr)
1487 {
1488 	struct net_device *event_netdev, *event_upper;
1489 	struct netdev_notifier_bonding_info *info;
1490 	struct netdev_bonding_info *bonding_info;
1491 	struct list_head *tmp;
1492 	struct device *dev;
1493 	int count = 0;
1494 
1495 	if (!lag->primary)
1496 		return true;
1497 
1498 	event_netdev = netdev_notifier_info_to_dev(ptr);
1499 	rcu_read_lock();
1500 	event_upper = netdev_master_upper_dev_get_rcu(event_netdev);
1501 	rcu_read_unlock();
1502 	if (event_upper != lag->upper_netdev)
1503 		return true;
1504 
1505 	dev = ice_pf_to_dev(lag->pf);
1506 
1507 	/* only supporting switchdev mode for SRIOV VF LAG.
1508 	 * primary interface has to be in switchdev mode
1509 	 */
1510 	if (!ice_is_switchdev_running(lag->pf)) {
1511 		dev_info(dev, "Primary interface not in switchdev mode - VF LAG disabled\n");
1512 		return false;
1513 	}
1514 
1515 	info = (struct netdev_notifier_bonding_info *)ptr;
1516 	bonding_info = &info->bonding_info;
1517 	lag->bond_mode = bonding_info->master.bond_mode;
1518 	if (lag->bond_mode != BOND_MODE_ACTIVEBACKUP) {
1519 		dev_info(dev, "Bond Mode not ACTIVE-BACKUP - VF LAG disabled\n");
1520 		return false;
1521 	}
1522 
1523 	list_for_each(tmp, lag->netdev_head) {
1524 		struct ice_dcbx_cfg *dcb_cfg, *peer_dcb_cfg;
1525 		struct ice_lag_netdev_list *entry;
1526 		struct ice_netdev_priv *peer_np;
1527 		struct net_device *peer_netdev;
1528 		struct ice_vsi *vsi, *peer_vsi;
1529 		struct ice_pf *peer_pf;
1530 
1531 		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
1532 		peer_netdev = entry->netdev;
1533 		if (!netif_is_ice(peer_netdev)) {
1534 			dev_info(dev, "Found %s non-ice netdev in LAG - VF LAG disabled\n",
1535 				 netdev_name(peer_netdev));
1536 			return false;
1537 		}
1538 
1539 		count++;
1540 		if (count > 2) {
1541 			dev_info(dev, "Found more than two netdevs in LAG - VF LAG disabled\n");
1542 			return false;
1543 		}
1544 
1545 		peer_np = netdev_priv(peer_netdev);
1546 		vsi = ice_get_main_vsi(lag->pf);
1547 		peer_vsi = peer_np->vsi;
1548 		if (lag->pf->pdev->bus != peer_vsi->back->pdev->bus ||
1549 		    lag->pf->pdev->slot != peer_vsi->back->pdev->slot) {
1550 			dev_info(dev, "Found %s on different device in LAG - VF LAG disabled\n",
1551 				 netdev_name(peer_netdev));
1552 			return false;
1553 		}
1554 
1555 		dcb_cfg = &vsi->port_info->qos_cfg.local_dcbx_cfg;
1556 		peer_dcb_cfg = &peer_vsi->port_info->qos_cfg.local_dcbx_cfg;
1557 		if (memcmp(dcb_cfg, peer_dcb_cfg,
1558 			   sizeof(struct ice_dcbx_cfg))) {
1559 			dev_info(dev, "Found %s with different DCB in LAG - VF LAG disabled\n",
1560 				 netdev_name(peer_netdev));
1561 			return false;
1562 		}
1563 
1564 		peer_pf = peer_vsi->back;
1565 		if (test_bit(ICE_FLAG_FW_LLDP_AGENT, peer_pf->flags)) {
1566 			dev_warn(dev, "Found %s with FW LLDP agent active - VF LAG disabled\n",
1567 				 netdev_name(peer_netdev));
1568 			return false;
1569 		}
1570 	}
1571 
1572 	return true;
1573 }
1574 
1575 /**
1576  * ice_lag_unregister - handle netdev unregister events
1577  * @lag: LAG info struct
1578  * @event_netdev: netdev struct for target of notifier event
1579  */
1580 static void
ice_lag_unregister(struct ice_lag * lag,struct net_device * event_netdev)1581 ice_lag_unregister(struct ice_lag *lag, struct net_device *event_netdev)
1582 {
1583 	struct ice_netdev_priv *np;
1584 	struct ice_pf *event_pf;
1585 	struct ice_lag *p_lag;
1586 
1587 	p_lag = ice_lag_find_primary(lag);
1588 	np = netdev_priv(event_netdev);
1589 	event_pf = np->vsi->back;
1590 
1591 	if (p_lag) {
1592 		if (p_lag->active_port != p_lag->pf->hw.port_info->lport &&
1593 		    p_lag->active_port != ICE_LAG_INVALID_PORT) {
1594 			struct ice_hw *active_hw;
1595 
1596 			active_hw = ice_lag_find_hw_by_lport(lag,
1597 							     p_lag->active_port);
1598 			if (active_hw)
1599 				ice_lag_reclaim_vf_nodes(p_lag, active_hw);
1600 			lag->active_port = ICE_LAG_INVALID_PORT;
1601 		}
1602 	}
1603 
1604 	/* primary processing for primary */
1605 	if (lag->primary && lag->netdev == event_netdev)
1606 		ice_lag_primary_swid(lag, false);
1607 
1608 	/* primary processing for secondary */
1609 	if (lag->primary && lag->netdev != event_netdev)
1610 		ice_lag_del_prune_list(lag, event_pf);
1611 
1612 	/* secondary processing for secondary */
1613 	if (!lag->primary && lag->netdev == event_netdev)
1614 		ice_lag_set_swid(0, lag, false);
1615 }
1616 
1617 /**
1618  * ice_lag_monitor_rdma - set and clear rdma functionality
1619  * @lag: pointer to lag struct
1620  * @ptr: opaque data for netdev event info
1621  */
1622 static void
ice_lag_monitor_rdma(struct ice_lag * lag,void * ptr)1623 ice_lag_monitor_rdma(struct ice_lag *lag, void *ptr)
1624 {
1625 	struct netdev_notifier_changeupper_info *info;
1626 	struct net_device *netdev;
1627 
1628 	info = ptr;
1629 	netdev = netdev_notifier_info_to_dev(ptr);
1630 
1631 	if (netdev != lag->netdev)
1632 		return;
1633 
1634 	if (info->linking)
1635 		ice_clear_rdma_cap(lag->pf);
1636 	else
1637 		ice_set_rdma_cap(lag->pf);
1638 }
1639 
1640 /**
1641  * ice_lag_chk_disabled_bond - monitor interfaces entering/leaving disabled bond
1642  * @lag: lag info struct
1643  * @ptr: opaque data containing event
1644  *
1645  * as interfaces enter a bond - determine if the bond is currently
1646  * SRIOV LAG compliant and flag if not.  As interfaces leave the
1647  * bond, reset their compliant status.
1648  */
ice_lag_chk_disabled_bond(struct ice_lag * lag,void * ptr)1649 static void ice_lag_chk_disabled_bond(struct ice_lag *lag, void *ptr)
1650 {
1651 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1652 	struct netdev_notifier_changeupper_info *info = ptr;
1653 	struct ice_lag *prim_lag;
1654 
1655 	if (netdev != lag->netdev)
1656 		return;
1657 
1658 	if (info->linking) {
1659 		prim_lag = ice_lag_find_primary(lag);
1660 		if (prim_lag &&
1661 		    !ice_is_feature_supported(prim_lag->pf, ICE_F_SRIOV_LAG)) {
1662 			ice_clear_feature_support(lag->pf, ICE_F_SRIOV_LAG);
1663 			netdev_info(netdev, "Interface added to non-compliant SRIOV LAG aggregate\n");
1664 		}
1665 	} else {
1666 		ice_lag_init_feature_support_flag(lag->pf);
1667 	}
1668 }
1669 
1670 /**
1671  * ice_lag_disable_sriov_bond - set members of bond as not supporting SRIOV LAG
1672  * @lag: primary interfaces lag struct
1673  */
ice_lag_disable_sriov_bond(struct ice_lag * lag)1674 static void ice_lag_disable_sriov_bond(struct ice_lag *lag)
1675 {
1676 	struct ice_netdev_priv *np;
1677 	struct ice_pf *pf;
1678 
1679 	np = netdev_priv(lag->netdev);
1680 	pf = np->vsi->back;
1681 	ice_clear_feature_support(pf, ICE_F_SRIOV_LAG);
1682 }
1683 
1684 /**
1685  * ice_lag_process_event - process a task assigned to the lag_wq
1686  * @work: pointer to work_struct
1687  */
ice_lag_process_event(struct work_struct * work)1688 static void ice_lag_process_event(struct work_struct *work)
1689 {
1690 	struct netdev_notifier_changeupper_info *info;
1691 	struct ice_lag_work *lag_work;
1692 	struct net_device *netdev;
1693 	struct list_head *tmp, *n;
1694 	struct ice_pf *pf;
1695 
1696 	lag_work = container_of(work, struct ice_lag_work, lag_task);
1697 	pf = lag_work->lag->pf;
1698 
1699 	mutex_lock(&pf->lag_mutex);
1700 	lag_work->lag->netdev_head = &lag_work->netdev_list.node;
1701 
1702 	switch (lag_work->event) {
1703 	case NETDEV_CHANGEUPPER:
1704 		info = &lag_work->info.changeupper_info;
1705 		ice_lag_chk_disabled_bond(lag_work->lag, info);
1706 		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1707 			ice_lag_monitor_link(lag_work->lag, info);
1708 			ice_lag_changeupper_event(lag_work->lag, info);
1709 			ice_lag_link_unlink(lag_work->lag, info);
1710 		}
1711 		ice_lag_monitor_rdma(lag_work->lag, info);
1712 		break;
1713 	case NETDEV_BONDING_INFO:
1714 		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1715 			if (!ice_lag_chk_comp(lag_work->lag,
1716 					      &lag_work->info.bonding_info)) {
1717 				netdev = lag_work->info.bonding_info.info.dev;
1718 				ice_lag_disable_sriov_bond(lag_work->lag);
1719 				ice_lag_unregister(lag_work->lag, netdev);
1720 				goto lag_cleanup;
1721 			}
1722 			ice_lag_monitor_active(lag_work->lag,
1723 					       &lag_work->info.bonding_info);
1724 			ice_lag_cfg_pf_fltrs(lag_work->lag,
1725 					     &lag_work->info.bonding_info);
1726 		}
1727 		ice_lag_info_event(lag_work->lag, &lag_work->info.bonding_info);
1728 		break;
1729 	case NETDEV_UNREGISTER:
1730 		if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG)) {
1731 			netdev = lag_work->info.bonding_info.info.dev;
1732 			if ((netdev == lag_work->lag->netdev ||
1733 			     lag_work->lag->primary) && lag_work->lag->bonded)
1734 				ice_lag_unregister(lag_work->lag, netdev);
1735 		}
1736 		break;
1737 	default:
1738 		break;
1739 	}
1740 
1741 lag_cleanup:
1742 	/* cleanup resources allocated for this work item */
1743 	list_for_each_safe(tmp, n, &lag_work->netdev_list.node) {
1744 		struct ice_lag_netdev_list *entry;
1745 
1746 		entry = list_entry(tmp, struct ice_lag_netdev_list, node);
1747 		list_del(&entry->node);
1748 		kfree(entry);
1749 	}
1750 	lag_work->lag->netdev_head = NULL;
1751 
1752 	mutex_unlock(&pf->lag_mutex);
1753 
1754 	kfree(lag_work);
1755 }
1756 
1757 /**
1758  * ice_lag_event_handler - handle LAG events from netdev
1759  * @notif_blk: notifier block registered by this netdev
1760  * @event: event type
1761  * @ptr: opaque data containing notifier event
1762  */
1763 static int
ice_lag_event_handler(struct notifier_block * notif_blk,unsigned long event,void * ptr)1764 ice_lag_event_handler(struct notifier_block *notif_blk, unsigned long event,
1765 		      void *ptr)
1766 {
1767 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1768 	struct net_device *upper_netdev;
1769 	struct ice_lag_work *lag_work;
1770 	struct ice_lag *lag;
1771 
1772 	if (!netif_is_ice(netdev))
1773 		return NOTIFY_DONE;
1774 
1775 	if (event != NETDEV_CHANGEUPPER && event != NETDEV_BONDING_INFO &&
1776 	    event != NETDEV_UNREGISTER)
1777 		return NOTIFY_DONE;
1778 
1779 	if (!(netdev->priv_flags & IFF_BONDING))
1780 		return NOTIFY_DONE;
1781 
1782 	lag = container_of(notif_blk, struct ice_lag, notif_block);
1783 	if (!lag->netdev)
1784 		return NOTIFY_DONE;
1785 
1786 	if (!net_eq(dev_net(netdev), &init_net))
1787 		return NOTIFY_DONE;
1788 
1789 	/* This memory will be freed at the end of ice_lag_process_event */
1790 	lag_work = kzalloc(sizeof(*lag_work), GFP_KERNEL);
1791 	if (!lag_work)
1792 		return -ENOMEM;
1793 
1794 	lag_work->event_netdev = netdev;
1795 	lag_work->lag = lag;
1796 	lag_work->event = event;
1797 	if (event == NETDEV_CHANGEUPPER) {
1798 		struct netdev_notifier_changeupper_info *info;
1799 
1800 		info = ptr;
1801 		upper_netdev = info->upper_dev;
1802 	} else {
1803 		upper_netdev = netdev_master_upper_dev_get(netdev);
1804 	}
1805 
1806 	INIT_LIST_HEAD(&lag_work->netdev_list.node);
1807 	if (upper_netdev) {
1808 		struct ice_lag_netdev_list *nd_list;
1809 		struct net_device *tmp_nd;
1810 
1811 		rcu_read_lock();
1812 		for_each_netdev_in_bond_rcu(upper_netdev, tmp_nd) {
1813 			nd_list = kzalloc(sizeof(*nd_list), GFP_ATOMIC);
1814 			if (!nd_list)
1815 				break;
1816 
1817 			nd_list->netdev = tmp_nd;
1818 			list_add(&nd_list->node, &lag_work->netdev_list.node);
1819 		}
1820 		rcu_read_unlock();
1821 	}
1822 
1823 	switch (event) {
1824 	case NETDEV_CHANGEUPPER:
1825 		lag_work->info.changeupper_info =
1826 			*((struct netdev_notifier_changeupper_info *)ptr);
1827 		break;
1828 	case NETDEV_BONDING_INFO:
1829 		lag_work->info.bonding_info =
1830 			*((struct netdev_notifier_bonding_info *)ptr);
1831 		break;
1832 	default:
1833 		lag_work->info.notifier_info =
1834 			*((struct netdev_notifier_info *)ptr);
1835 		break;
1836 	}
1837 
1838 	INIT_WORK(&lag_work->lag_task, ice_lag_process_event);
1839 	queue_work(ice_lag_wq, &lag_work->lag_task);
1840 
1841 	return NOTIFY_DONE;
1842 }
1843 
1844 /**
1845  * ice_register_lag_handler - register LAG handler on netdev
1846  * @lag: LAG struct
1847  */
ice_register_lag_handler(struct ice_lag * lag)1848 static int ice_register_lag_handler(struct ice_lag *lag)
1849 {
1850 	struct device *dev = ice_pf_to_dev(lag->pf);
1851 	struct notifier_block *notif_blk;
1852 
1853 	notif_blk = &lag->notif_block;
1854 
1855 	if (!notif_blk->notifier_call) {
1856 		notif_blk->notifier_call = ice_lag_event_handler;
1857 		if (register_netdevice_notifier(notif_blk)) {
1858 			notif_blk->notifier_call = NULL;
1859 			dev_err(dev, "FAIL register LAG event handler!\n");
1860 			return -EINVAL;
1861 		}
1862 		dev_dbg(dev, "LAG event handler registered\n");
1863 	}
1864 	return 0;
1865 }
1866 
1867 /**
1868  * ice_unregister_lag_handler - unregister LAG handler on netdev
1869  * @lag: LAG struct
1870  */
ice_unregister_lag_handler(struct ice_lag * lag)1871 static void ice_unregister_lag_handler(struct ice_lag *lag)
1872 {
1873 	struct device *dev = ice_pf_to_dev(lag->pf);
1874 	struct notifier_block *notif_blk;
1875 
1876 	notif_blk = &lag->notif_block;
1877 	if (notif_blk->notifier_call) {
1878 		unregister_netdevice_notifier(notif_blk);
1879 		dev_dbg(dev, "LAG event handler unregistered\n");
1880 	}
1881 }
1882 
1883 /**
1884  * ice_create_lag_recipe
1885  * @hw: pointer to HW struct
1886  * @rid: pointer to u16 to pass back recipe index
1887  * @base_recipe: recipe to base the new recipe on
1888  * @prio: priority for new recipe
1889  *
1890  * function returns 0 on error
1891  */
ice_create_lag_recipe(struct ice_hw * hw,u16 * rid,const u8 * base_recipe,u8 prio)1892 static int ice_create_lag_recipe(struct ice_hw *hw, u16 *rid,
1893 				 const u8 *base_recipe, u8 prio)
1894 {
1895 	struct ice_aqc_recipe_data_elem *new_rcp;
1896 	int err;
1897 
1898 	err = ice_alloc_recipe(hw, rid);
1899 	if (err)
1900 		return err;
1901 
1902 	new_rcp = kzalloc(ICE_RECIPE_LEN * ICE_MAX_NUM_RECIPES, GFP_KERNEL);
1903 	if (!new_rcp)
1904 		return -ENOMEM;
1905 
1906 	memcpy(new_rcp, base_recipe, ICE_RECIPE_LEN);
1907 	new_rcp->content.act_ctrl_fwd_priority = prio;
1908 	new_rcp->content.rid = *rid | ICE_AQ_RECIPE_ID_IS_ROOT;
1909 	new_rcp->recipe_indx = *rid;
1910 	bitmap_zero((unsigned long *)new_rcp->recipe_bitmap,
1911 		    ICE_MAX_NUM_RECIPES);
1912 	set_bit(*rid, (unsigned long *)new_rcp->recipe_bitmap);
1913 
1914 	err = ice_aq_add_recipe(hw, new_rcp, 1, NULL);
1915 	if (err)
1916 		*rid = 0;
1917 
1918 	kfree(new_rcp);
1919 	return err;
1920 }
1921 
1922 /**
1923  * ice_lag_move_vf_nodes_tc_sync - move a VF's nodes for a tc during reset
1924  * @lag: primary interfaces lag struct
1925  * @dest_hw: HW struct for destination's interface
1926  * @vsi_num: VSI index in PF space
1927  * @tc: traffic class to move
1928  */
1929 static void
ice_lag_move_vf_nodes_tc_sync(struct ice_lag * lag,struct ice_hw * dest_hw,u16 vsi_num,u8 tc)1930 ice_lag_move_vf_nodes_tc_sync(struct ice_lag *lag, struct ice_hw *dest_hw,
1931 			      u16 vsi_num, u8 tc)
1932 {
1933 	DEFINE_RAW_FLEX(struct ice_aqc_move_elem, buf, teid, 1);
1934 	struct device *dev = ice_pf_to_dev(lag->pf);
1935 	u16 numq, valq, num_moved, qbuf_size;
1936 	u16 buf_size = __struct_size(buf);
1937 	struct ice_aqc_cfg_txqs_buf *qbuf;
1938 	struct ice_sched_node *n_prt;
1939 	__le32 teid, parent_teid;
1940 	struct ice_vsi_ctx *ctx;
1941 	struct ice_hw *hw;
1942 	u32 tmp_teid;
1943 
1944 	hw = &lag->pf->hw;
1945 	ctx = ice_get_vsi_ctx(hw, vsi_num);
1946 	if (!ctx) {
1947 		dev_warn(dev, "LAG rebuild failed after reset due to VSI Context failure\n");
1948 		return;
1949 	}
1950 
1951 	if (!ctx->sched.vsi_node[tc])
1952 		return;
1953 
1954 	numq = ctx->num_lan_q_entries[tc];
1955 	teid = ctx->sched.vsi_node[tc]->info.node_teid;
1956 	tmp_teid = le32_to_cpu(teid);
1957 	parent_teid = ctx->sched.vsi_node[tc]->info.parent_teid;
1958 
1959 	if (!tmp_teid || !numq)
1960 		return;
1961 
1962 	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, true))
1963 		dev_dbg(dev, "Problem suspending traffic during reset rebuild\n");
1964 
1965 	/* reconfig queues for new port */
1966 	qbuf_size = struct_size(qbuf, queue_info, numq);
1967 	qbuf = kzalloc(qbuf_size, GFP_KERNEL);
1968 	if (!qbuf) {
1969 		dev_warn(dev, "Failure allocating VF queue recfg buffer for reset rebuild\n");
1970 		goto resume_sync;
1971 	}
1972 
1973 	/* add the per queue info for the reconfigure command buffer */
1974 	valq = ice_lag_qbuf_recfg(hw, qbuf, vsi_num, numq, tc);
1975 	if (!valq) {
1976 		dev_warn(dev, "Failure to reconfig queues for LAG reset rebuild\n");
1977 		goto sync_none;
1978 	}
1979 
1980 	if (ice_aq_cfg_lan_txq(hw, qbuf, qbuf_size, numq, hw->port_info->lport,
1981 			       dest_hw->port_info->lport, NULL)) {
1982 		dev_warn(dev, "Failure to configure queues for LAG reset rebuild\n");
1983 		goto sync_qerr;
1984 	}
1985 
1986 sync_none:
1987 	kfree(qbuf);
1988 
1989 	/* find parent in destination tree */
1990 	n_prt = ice_lag_get_sched_parent(dest_hw, tc);
1991 	if (!n_prt)
1992 		goto resume_sync;
1993 
1994 	/* Move node to new parent */
1995 	buf->hdr.src_parent_teid = parent_teid;
1996 	buf->hdr.dest_parent_teid = n_prt->info.node_teid;
1997 	buf->hdr.num_elems = cpu_to_le16(1);
1998 	buf->hdr.mode = ICE_AQC_MOVE_ELEM_MODE_KEEP_OWN;
1999 	buf->teid[0] = teid;
2000 
2001 	if (ice_aq_move_sched_elems(&lag->pf->hw, buf, buf_size, &num_moved))
2002 		dev_warn(dev, "Failure to move VF nodes for LAG reset rebuild\n");
2003 	else
2004 		ice_sched_update_parent(n_prt, ctx->sched.vsi_node[tc]);
2005 
2006 	goto resume_sync;
2007 
2008 sync_qerr:
2009 	kfree(qbuf);
2010 
2011 resume_sync:
2012 	if (ice_sched_suspend_resume_elems(hw, 1, &tmp_teid, false))
2013 		dev_warn(dev, "Problem restarting traffic for LAG node reset rebuild\n");
2014 }
2015 
2016 /**
2017  * ice_lag_move_vf_nodes_sync - move vf nodes to active interface
2018  * @lag: primary interfaces lag struct
2019  * @dest_hw: lport value for currently active port
2020  *
2021  * This function is used in a reset context, outside of event handling,
2022  * to move the VF nodes to the secondary interface when that interface
2023  * is the active interface during a reset rebuild
2024  */
2025 static void
ice_lag_move_vf_nodes_sync(struct ice_lag * lag,struct ice_hw * dest_hw)2026 ice_lag_move_vf_nodes_sync(struct ice_lag *lag, struct ice_hw *dest_hw)
2027 {
2028 	struct ice_pf *pf;
2029 	int i, tc;
2030 
2031 	if (!lag->primary || !dest_hw)
2032 		return;
2033 
2034 	pf = lag->pf;
2035 	ice_for_each_vsi(pf, i)
2036 		if (pf->vsi[i] && pf->vsi[i]->type == ICE_VSI_VF)
2037 			ice_for_each_traffic_class(tc)
2038 				ice_lag_move_vf_nodes_tc_sync(lag, dest_hw, i,
2039 							      tc);
2040 }
2041 
2042 /**
2043  * ice_init_lag - initialize support for LAG
2044  * @pf: PF struct
2045  *
2046  * Alloc memory for LAG structs and initialize the elements.
2047  * Memory will be freed in ice_deinit_lag
2048  */
ice_init_lag(struct ice_pf * pf)2049 int ice_init_lag(struct ice_pf *pf)
2050 {
2051 	struct device *dev = ice_pf_to_dev(pf);
2052 	struct ice_lag *lag;
2053 	struct ice_vsi *vsi;
2054 	u64 recipe_bits = 0;
2055 	int n, err;
2056 
2057 	ice_lag_init_feature_support_flag(pf);
2058 	if (!ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
2059 		return 0;
2060 
2061 	pf->lag = kzalloc(sizeof(*lag), GFP_KERNEL);
2062 	if (!pf->lag)
2063 		return -ENOMEM;
2064 	lag = pf->lag;
2065 
2066 	vsi = ice_get_main_vsi(pf);
2067 	if (!vsi) {
2068 		dev_err(dev, "couldn't get main vsi, link aggregation init fail\n");
2069 		err = -EIO;
2070 		goto lag_error;
2071 	}
2072 
2073 	lag->pf = pf;
2074 	lag->netdev = vsi->netdev;
2075 	lag->role = ICE_LAG_NONE;
2076 	lag->active_port = ICE_LAG_INVALID_PORT;
2077 	lag->bonded = false;
2078 	lag->upper_netdev = NULL;
2079 	lag->notif_block.notifier_call = NULL;
2080 
2081 	err = ice_register_lag_handler(lag);
2082 	if (err) {
2083 		dev_warn(dev, "INIT LAG: Failed to register event handler\n");
2084 		goto lag_error;
2085 	}
2086 
2087 	err = ice_create_lag_recipe(&pf->hw, &lag->pf_recipe,
2088 				    ice_dflt_vsi_rcp, 1);
2089 	if (err)
2090 		goto lag_error;
2091 
2092 	err = ice_create_lag_recipe(&pf->hw, &lag->lport_recipe,
2093 				    ice_lport_rcp, 3);
2094 	if (err)
2095 		goto free_rcp_res;
2096 
2097 	/* associate recipes to profiles */
2098 	for (n = 0; n < ICE_PROFID_IPV6_GTPU_IPV6_TCP_INNER; n++) {
2099 		err = ice_aq_get_recipe_to_profile(&pf->hw, n,
2100 						   &recipe_bits, NULL);
2101 		if (err)
2102 			continue;
2103 
2104 		if (recipe_bits & BIT(ICE_SW_LKUP_DFLT)) {
2105 			recipe_bits |= BIT(lag->pf_recipe) |
2106 				       BIT(lag->lport_recipe);
2107 			ice_aq_map_recipe_to_profile(&pf->hw, n,
2108 						     recipe_bits, NULL);
2109 		}
2110 	}
2111 
2112 	ice_display_lag_info(lag);
2113 
2114 	dev_dbg(dev, "INIT LAG complete\n");
2115 	return 0;
2116 
2117 free_rcp_res:
2118 	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2119 			&pf->lag->pf_recipe);
2120 lag_error:
2121 	kfree(lag);
2122 	pf->lag = NULL;
2123 	return err;
2124 }
2125 
2126 /**
2127  * ice_deinit_lag - Clean up LAG
2128  * @pf: PF struct
2129  *
2130  * Clean up kernel LAG info and free memory
2131  * This function is meant to only be called on driver remove/shutdown
2132  */
ice_deinit_lag(struct ice_pf * pf)2133 void ice_deinit_lag(struct ice_pf *pf)
2134 {
2135 	struct ice_lag *lag;
2136 
2137 	lag = pf->lag;
2138 
2139 	if (!lag)
2140 		return;
2141 
2142 	if (lag->pf)
2143 		ice_unregister_lag_handler(lag);
2144 
2145 	flush_workqueue(ice_lag_wq);
2146 
2147 	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2148 			&pf->lag->pf_recipe);
2149 	ice_free_hw_res(&pf->hw, ICE_AQC_RES_TYPE_RECIPE, 1,
2150 			&pf->lag->lport_recipe);
2151 
2152 	kfree(lag);
2153 
2154 	pf->lag = NULL;
2155 }
2156 
2157 /**
2158  * ice_lag_rebuild - rebuild lag resources after reset
2159  * @pf: pointer to local pf struct
2160  *
2161  * PF resets are promoted to CORER resets when interface in an aggregate.  This
2162  * means that we need to rebuild the PF resources for the interface.  Since
2163  * this will happen outside the normal event processing, need to acquire the lag
2164  * lock.
2165  *
2166  * This function will also evaluate the VF resources if this is the primary
2167  * interface.
2168  */
ice_lag_rebuild(struct ice_pf * pf)2169 void ice_lag_rebuild(struct ice_pf *pf)
2170 {
2171 	struct ice_lag_netdev_list ndlist;
2172 	struct ice_lag *lag, *prim_lag;
2173 	u8 act_port, loc_port;
2174 
2175 	if (!pf->lag || !pf->lag->bonded)
2176 		return;
2177 
2178 	mutex_lock(&pf->lag_mutex);
2179 
2180 	lag = pf->lag;
2181 	if (lag->primary) {
2182 		prim_lag = lag;
2183 	} else {
2184 		ice_lag_build_netdev_list(lag, &ndlist);
2185 		prim_lag = ice_lag_find_primary(lag);
2186 	}
2187 
2188 	if (!prim_lag) {
2189 		dev_dbg(ice_pf_to_dev(pf), "No primary interface in aggregate, can't rebuild\n");
2190 		goto lag_rebuild_out;
2191 	}
2192 
2193 	act_port = prim_lag->active_port;
2194 	loc_port = lag->pf->hw.port_info->lport;
2195 
2196 	/* configure SWID for this port */
2197 	if (lag->primary) {
2198 		ice_lag_primary_swid(lag, true);
2199 	} else {
2200 		ice_lag_set_swid(prim_lag->pf->hw.port_info->sw_id, lag, true);
2201 		ice_lag_add_prune_list(prim_lag, pf);
2202 		if (act_port == loc_port)
2203 			ice_lag_move_vf_nodes_sync(prim_lag, &pf->hw);
2204 	}
2205 
2206 	ice_lag_cfg_cp_fltr(lag, true);
2207 
2208 	if (lag->pf_rx_rule_id)
2209 		if (ice_lag_cfg_dflt_fltr(lag, true))
2210 			dev_err(ice_pf_to_dev(pf), "Error adding default VSI rule in rebuild\n");
2211 
2212 	ice_clear_rdma_cap(pf);
2213 lag_rebuild_out:
2214 	ice_lag_destroy_netdev_list(lag, &ndlist);
2215 	mutex_unlock(&pf->lag_mutex);
2216 }
2217 
2218 /**
2219  * ice_lag_is_switchdev_running
2220  * @pf: pointer to PF structure
2221  *
2222  * Check if switchdev is running on any of the interfaces connected to lag.
2223  */
ice_lag_is_switchdev_running(struct ice_pf * pf)2224 bool ice_lag_is_switchdev_running(struct ice_pf *pf)
2225 {
2226 	struct ice_lag *lag = pf->lag;
2227 	struct net_device *tmp_nd;
2228 
2229 	if (!ice_is_feature_supported(pf, ICE_F_SRIOV_LAG) || !lag)
2230 		return false;
2231 
2232 	rcu_read_lock();
2233 	for_each_netdev_in_bond_rcu(lag->upper_netdev, tmp_nd) {
2234 		struct ice_netdev_priv *priv = netdev_priv(tmp_nd);
2235 
2236 		if (!netif_is_ice(tmp_nd) || !priv || !priv->vsi ||
2237 		    !priv->vsi->back)
2238 			continue;
2239 
2240 		if (ice_is_switchdev_running(priv->vsi->back)) {
2241 			rcu_read_unlock();
2242 			return true;
2243 		}
2244 	}
2245 	rcu_read_unlock();
2246 
2247 	return false;
2248 }
2249