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