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