OSDN Git Service

b91eabc09fbc187460750f6bc10465ae1b3be944
[tomoyo/tomoyo-test1.git] / drivers / net / ethernet / mellanox / mlx5 / core / lag.c
1 /*
2  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/netdevice.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/vport.h>
36 #include "mlx5_core.h"
37 #include "eswitch.h"
38 #include "lag.h"
39 #include "lag_mp.h"
40
41 /* General purpose, use for short periods of time.
42  * Beware of lock dependencies (preferably, no locks should be acquired
43  * under it).
44  */
45 static DEFINE_MUTEX(lag_mutex);
46
47 static int mlx5_cmd_create_lag(struct mlx5_core_dev *dev, u8 remap_port1,
48                                u8 remap_port2)
49 {
50         u32   in[MLX5_ST_SZ_DW(create_lag_in)]   = {0};
51         u32   out[MLX5_ST_SZ_DW(create_lag_out)] = {0};
52         void *lag_ctx = MLX5_ADDR_OF(create_lag_in, in, ctx);
53
54         MLX5_SET(create_lag_in, in, opcode, MLX5_CMD_OP_CREATE_LAG);
55
56         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
57         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
58
59         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
60 }
61
62 static int mlx5_cmd_modify_lag(struct mlx5_core_dev *dev, u8 remap_port1,
63                                u8 remap_port2)
64 {
65         u32   in[MLX5_ST_SZ_DW(modify_lag_in)]   = {0};
66         u32   out[MLX5_ST_SZ_DW(modify_lag_out)] = {0};
67         void *lag_ctx = MLX5_ADDR_OF(modify_lag_in, in, ctx);
68
69         MLX5_SET(modify_lag_in, in, opcode, MLX5_CMD_OP_MODIFY_LAG);
70         MLX5_SET(modify_lag_in, in, field_select, 0x1);
71
72         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
73         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
74
75         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
76 }
77
78 static int mlx5_cmd_destroy_lag(struct mlx5_core_dev *dev)
79 {
80         u32  in[MLX5_ST_SZ_DW(destroy_lag_in)]  = {0};
81         u32 out[MLX5_ST_SZ_DW(destroy_lag_out)] = {0};
82
83         MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
84
85         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
86 }
87
88 int mlx5_cmd_create_vport_lag(struct mlx5_core_dev *dev)
89 {
90         u32  in[MLX5_ST_SZ_DW(create_vport_lag_in)]  = {0};
91         u32 out[MLX5_ST_SZ_DW(create_vport_lag_out)] = {0};
92
93         MLX5_SET(create_vport_lag_in, in, opcode, MLX5_CMD_OP_CREATE_VPORT_LAG);
94
95         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
96 }
97 EXPORT_SYMBOL(mlx5_cmd_create_vport_lag);
98
99 int mlx5_cmd_destroy_vport_lag(struct mlx5_core_dev *dev)
100 {
101         u32  in[MLX5_ST_SZ_DW(destroy_vport_lag_in)]  = {0};
102         u32 out[MLX5_ST_SZ_DW(destroy_vport_lag_out)] = {0};
103
104         MLX5_SET(destroy_vport_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_VPORT_LAG);
105
106         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
107 }
108 EXPORT_SYMBOL(mlx5_cmd_destroy_vport_lag);
109
110 static int mlx5_cmd_query_cong_counter(struct mlx5_core_dev *dev,
111                                        bool reset, void *out, int out_size)
112 {
113         u32 in[MLX5_ST_SZ_DW(query_cong_statistics_in)] = { };
114
115         MLX5_SET(query_cong_statistics_in, in, opcode,
116                  MLX5_CMD_OP_QUERY_CONG_STATISTICS);
117         MLX5_SET(query_cong_statistics_in, in, clear, reset);
118         return mlx5_cmd_exec(dev, in, sizeof(in), out, out_size);
119 }
120
121 int mlx5_lag_dev_get_netdev_idx(struct mlx5_lag *ldev,
122                                 struct net_device *ndev)
123 {
124         int i;
125
126         for (i = 0; i < MLX5_MAX_PORTS; i++)
127                 if (ldev->pf[i].netdev == ndev)
128                         return i;
129
130         return -1;
131 }
132
133 static bool __mlx5_lag_is_roce(struct mlx5_lag *ldev)
134 {
135         return !!(ldev->flags & MLX5_LAG_FLAG_ROCE);
136 }
137
138 static bool __mlx5_lag_is_sriov(struct mlx5_lag *ldev)
139 {
140         return !!(ldev->flags & MLX5_LAG_FLAG_SRIOV);
141 }
142
143 static void mlx5_infer_tx_affinity_mapping(struct lag_tracker *tracker,
144                                            u8 *port1, u8 *port2)
145 {
146         *port1 = 1;
147         *port2 = 2;
148         if (!tracker->netdev_state[MLX5_LAG_P1].tx_enabled ||
149             !tracker->netdev_state[MLX5_LAG_P1].link_up) {
150                 *port1 = 2;
151                 return;
152         }
153
154         if (!tracker->netdev_state[MLX5_LAG_P2].tx_enabled ||
155             !tracker->netdev_state[MLX5_LAG_P2].link_up)
156                 *port2 = 1;
157 }
158
159 void mlx5_modify_lag(struct mlx5_lag *ldev,
160                      struct lag_tracker *tracker)
161 {
162         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
163         u8 v2p_port1, v2p_port2;
164         int err;
165
166         mlx5_infer_tx_affinity_mapping(tracker, &v2p_port1,
167                                        &v2p_port2);
168
169         if (v2p_port1 != ldev->v2p_map[MLX5_LAG_P1] ||
170             v2p_port2 != ldev->v2p_map[MLX5_LAG_P2]) {
171                 ldev->v2p_map[MLX5_LAG_P1] = v2p_port1;
172                 ldev->v2p_map[MLX5_LAG_P2] = v2p_port2;
173
174                 mlx5_core_info(dev0, "modify lag map port 1:%d port 2:%d",
175                                ldev->v2p_map[MLX5_LAG_P1],
176                                ldev->v2p_map[MLX5_LAG_P2]);
177
178                 err = mlx5_cmd_modify_lag(dev0, v2p_port1, v2p_port2);
179                 if (err)
180                         mlx5_core_err(dev0,
181                                       "Failed to modify LAG (%d)\n",
182                                       err);
183         }
184 }
185
186 static int mlx5_create_lag(struct mlx5_lag *ldev,
187                            struct lag_tracker *tracker)
188 {
189         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
190         int err;
191
192         mlx5_infer_tx_affinity_mapping(tracker, &ldev->v2p_map[MLX5_LAG_P1],
193                                        &ldev->v2p_map[MLX5_LAG_P2]);
194
195         mlx5_core_info(dev0, "lag map port 1:%d port 2:%d",
196                        ldev->v2p_map[MLX5_LAG_P1], ldev->v2p_map[MLX5_LAG_P2]);
197
198         err = mlx5_cmd_create_lag(dev0, ldev->v2p_map[MLX5_LAG_P1],
199                                   ldev->v2p_map[MLX5_LAG_P2]);
200         if (err)
201                 mlx5_core_err(dev0,
202                               "Failed to create LAG (%d)\n",
203                               err);
204         return err;
205 }
206
207 int mlx5_activate_lag(struct mlx5_lag *ldev,
208                       struct lag_tracker *tracker,
209                       u8 flags)
210 {
211         bool roce_lag = !!(flags & MLX5_LAG_FLAG_ROCE);
212         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
213         int err;
214
215         err = mlx5_create_lag(ldev, tracker);
216         if (err) {
217                 if (roce_lag) {
218                         mlx5_core_err(dev0,
219                                       "Failed to activate RoCE LAG\n");
220                 } else {
221                         mlx5_core_err(dev0,
222                                       "Failed to activate VF LAG\n"
223                                       "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
224                 }
225                 return err;
226         }
227
228         ldev->flags |= flags;
229         return 0;
230 }
231
232 static int mlx5_deactivate_lag(struct mlx5_lag *ldev)
233 {
234         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
235         bool roce_lag = __mlx5_lag_is_roce(ldev);
236         int err;
237
238         ldev->flags &= ~MLX5_LAG_MODE_FLAGS;
239
240         err = mlx5_cmd_destroy_lag(dev0);
241         if (err) {
242                 if (roce_lag) {
243                         mlx5_core_err(dev0,
244                                       "Failed to deactivate RoCE LAG; driver restart required\n");
245                 } else {
246                         mlx5_core_err(dev0,
247                                       "Failed to deactivate VF LAG; driver restart required\n"
248                                       "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
249                 }
250         }
251
252         return err;
253 }
254
255 static bool mlx5_lag_check_prereq(struct mlx5_lag *ldev)
256 {
257         if (!ldev->pf[MLX5_LAG_P1].dev || !ldev->pf[MLX5_LAG_P2].dev)
258                 return false;
259
260 #ifdef CONFIG_MLX5_ESWITCH
261         return mlx5_esw_lag_prereq(ldev->pf[MLX5_LAG_P1].dev,
262                                    ldev->pf[MLX5_LAG_P2].dev);
263 #else
264         return (!mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P1].dev) &&
265                 !mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P2].dev));
266 #endif
267 }
268
269 static void mlx5_lag_add_ib_devices(struct mlx5_lag *ldev)
270 {
271         int i;
272
273         for (i = 0; i < MLX5_MAX_PORTS; i++)
274                 if (ldev->pf[i].dev)
275                         mlx5_add_dev_by_protocol(ldev->pf[i].dev,
276                                                  MLX5_INTERFACE_PROTOCOL_IB);
277 }
278
279 static void mlx5_lag_remove_ib_devices(struct mlx5_lag *ldev)
280 {
281         int i;
282
283         for (i = 0; i < MLX5_MAX_PORTS; i++)
284                 if (ldev->pf[i].dev)
285                         mlx5_remove_dev_by_protocol(ldev->pf[i].dev,
286                                                     MLX5_INTERFACE_PROTOCOL_IB);
287 }
288
289 static void mlx5_do_bond(struct mlx5_lag *ldev)
290 {
291         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
292         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
293         struct lag_tracker tracker;
294         bool do_bond, roce_lag;
295         int err;
296
297         if (!dev0 || !dev1)
298                 return;
299
300         mutex_lock(&lag_mutex);
301         tracker = ldev->tracker;
302         mutex_unlock(&lag_mutex);
303
304         do_bond = tracker.is_bonded && mlx5_lag_check_prereq(ldev);
305
306         if (do_bond && !__mlx5_lag_is_active(ldev)) {
307                 roce_lag = !mlx5_sriov_is_enabled(dev0) &&
308                            !mlx5_sriov_is_enabled(dev1);
309
310 #ifdef CONFIG_MLX5_ESWITCH
311                 roce_lag &= dev0->priv.eswitch->mode == MLX5_ESWITCH_NONE &&
312                             dev1->priv.eswitch->mode == MLX5_ESWITCH_NONE;
313 #endif
314
315                 if (roce_lag)
316                         mlx5_lag_remove_ib_devices(ldev);
317
318                 err = mlx5_activate_lag(ldev, &tracker,
319                                         roce_lag ? MLX5_LAG_FLAG_ROCE :
320                                         MLX5_LAG_FLAG_SRIOV);
321                 if (err) {
322                         if (roce_lag)
323                                 mlx5_lag_add_ib_devices(ldev);
324
325                         return;
326                 }
327
328                 if (roce_lag) {
329                         mlx5_add_dev_by_protocol(dev0, MLX5_INTERFACE_PROTOCOL_IB);
330                         mlx5_nic_vport_enable_roce(dev1);
331                 }
332         } else if (do_bond && __mlx5_lag_is_active(ldev)) {
333                 mlx5_modify_lag(ldev, &tracker);
334         } else if (!do_bond && __mlx5_lag_is_active(ldev)) {
335                 roce_lag = __mlx5_lag_is_roce(ldev);
336
337                 if (roce_lag) {
338                         mlx5_remove_dev_by_protocol(dev0, MLX5_INTERFACE_PROTOCOL_IB);
339                         mlx5_nic_vport_disable_roce(dev1);
340                 }
341
342                 err = mlx5_deactivate_lag(ldev);
343                 if (err)
344                         return;
345
346                 if (roce_lag)
347                         mlx5_lag_add_ib_devices(ldev);
348         }
349 }
350
351 static void mlx5_queue_bond_work(struct mlx5_lag *ldev, unsigned long delay)
352 {
353         queue_delayed_work(ldev->wq, &ldev->bond_work, delay);
354 }
355
356 static void mlx5_do_bond_work(struct work_struct *work)
357 {
358         struct delayed_work *delayed_work = to_delayed_work(work);
359         struct mlx5_lag *ldev = container_of(delayed_work, struct mlx5_lag,
360                                              bond_work);
361         int status;
362
363         status = mlx5_dev_list_trylock();
364         if (!status) {
365                 /* 1 sec delay. */
366                 mlx5_queue_bond_work(ldev, HZ);
367                 return;
368         }
369
370         mlx5_do_bond(ldev);
371         mlx5_dev_list_unlock();
372 }
373
374 static int mlx5_handle_changeupper_event(struct mlx5_lag *ldev,
375                                          struct lag_tracker *tracker,
376                                          struct net_device *ndev,
377                                          struct netdev_notifier_changeupper_info *info)
378 {
379         struct net_device *upper = info->upper_dev, *ndev_tmp;
380         struct netdev_lag_upper_info *lag_upper_info = NULL;
381         bool is_bonded;
382         int bond_status = 0;
383         int num_slaves = 0;
384         int idx;
385
386         if (!netif_is_lag_master(upper))
387                 return 0;
388
389         if (info->linking)
390                 lag_upper_info = info->upper_info;
391
392         /* The event may still be of interest if the slave does not belong to
393          * us, but is enslaved to a master which has one or more of our netdevs
394          * as slaves (e.g., if a new slave is added to a master that bonds two
395          * of our netdevs, we should unbond).
396          */
397         rcu_read_lock();
398         for_each_netdev_in_bond_rcu(upper, ndev_tmp) {
399                 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
400                 if (idx > -1)
401                         bond_status |= (1 << idx);
402
403                 num_slaves++;
404         }
405         rcu_read_unlock();
406
407         /* None of this lagdev's netdevs are slaves of this master. */
408         if (!(bond_status & 0x3))
409                 return 0;
410
411         if (lag_upper_info)
412                 tracker->tx_type = lag_upper_info->tx_type;
413
414         /* Determine bonding status:
415          * A device is considered bonded if both its physical ports are slaves
416          * of the same lag master, and only them.
417          * Lag mode must be activebackup or hash.
418          */
419         is_bonded = (num_slaves == MLX5_MAX_PORTS) &&
420                     (bond_status == 0x3) &&
421                     ((tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) ||
422                      (tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH));
423
424         if (tracker->is_bonded != is_bonded) {
425                 tracker->is_bonded = is_bonded;
426                 return 1;
427         }
428
429         return 0;
430 }
431
432 static int mlx5_handle_changelowerstate_event(struct mlx5_lag *ldev,
433                                               struct lag_tracker *tracker,
434                                               struct net_device *ndev,
435                                               struct netdev_notifier_changelowerstate_info *info)
436 {
437         struct netdev_lag_lower_state_info *lag_lower_info;
438         int idx;
439
440         if (!netif_is_lag_port(ndev))
441                 return 0;
442
443         idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev);
444         if (idx == -1)
445                 return 0;
446
447         /* This information is used to determine virtual to physical
448          * port mapping.
449          */
450         lag_lower_info = info->lower_state_info;
451         if (!lag_lower_info)
452                 return 0;
453
454         tracker->netdev_state[idx] = *lag_lower_info;
455
456         return 1;
457 }
458
459 static int mlx5_lag_netdev_event(struct notifier_block *this,
460                                  unsigned long event, void *ptr)
461 {
462         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
463         struct lag_tracker tracker;
464         struct mlx5_lag *ldev;
465         int changed = 0;
466
467         if (!net_eq(dev_net(ndev), &init_net))
468                 return NOTIFY_DONE;
469
470         if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
471                 return NOTIFY_DONE;
472
473         ldev    = container_of(this, struct mlx5_lag, nb);
474         tracker = ldev->tracker;
475
476         switch (event) {
477         case NETDEV_CHANGEUPPER:
478                 changed = mlx5_handle_changeupper_event(ldev, &tracker, ndev,
479                                                         ptr);
480                 break;
481         case NETDEV_CHANGELOWERSTATE:
482                 changed = mlx5_handle_changelowerstate_event(ldev, &tracker,
483                                                              ndev, ptr);
484                 break;
485         }
486
487         mutex_lock(&lag_mutex);
488         ldev->tracker = tracker;
489         mutex_unlock(&lag_mutex);
490
491         if (changed)
492                 mlx5_queue_bond_work(ldev, 0);
493
494         return NOTIFY_DONE;
495 }
496
497 static struct mlx5_lag *mlx5_lag_dev_alloc(void)
498 {
499         struct mlx5_lag *ldev;
500
501         ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
502         if (!ldev)
503                 return NULL;
504
505         ldev->wq = create_singlethread_workqueue("mlx5_lag");
506         if (!ldev->wq) {
507                 kfree(ldev);
508                 return NULL;
509         }
510
511         INIT_DELAYED_WORK(&ldev->bond_work, mlx5_do_bond_work);
512
513         return ldev;
514 }
515
516 static void mlx5_lag_dev_free(struct mlx5_lag *ldev)
517 {
518         destroy_workqueue(ldev->wq);
519         kfree(ldev);
520 }
521
522 static void mlx5_lag_dev_add_pf(struct mlx5_lag *ldev,
523                                 struct mlx5_core_dev *dev,
524                                 struct net_device *netdev)
525 {
526         unsigned int fn = PCI_FUNC(dev->pdev->devfn);
527
528         if (fn >= MLX5_MAX_PORTS)
529                 return;
530
531         mutex_lock(&lag_mutex);
532         ldev->pf[fn].dev    = dev;
533         ldev->pf[fn].netdev = netdev;
534         ldev->tracker.netdev_state[fn].link_up = 0;
535         ldev->tracker.netdev_state[fn].tx_enabled = 0;
536
537         dev->priv.lag = ldev;
538
539         mutex_unlock(&lag_mutex);
540 }
541
542 static void mlx5_lag_dev_remove_pf(struct mlx5_lag *ldev,
543                                    struct mlx5_core_dev *dev)
544 {
545         int i;
546
547         for (i = 0; i < MLX5_MAX_PORTS; i++)
548                 if (ldev->pf[i].dev == dev)
549                         break;
550
551         if (i == MLX5_MAX_PORTS)
552                 return;
553
554         mutex_lock(&lag_mutex);
555         memset(&ldev->pf[i], 0, sizeof(*ldev->pf));
556
557         dev->priv.lag = NULL;
558         mutex_unlock(&lag_mutex);
559 }
560
561 /* Must be called with intf_mutex held */
562 void mlx5_lag_add(struct mlx5_core_dev *dev, struct net_device *netdev)
563 {
564         struct mlx5_lag *ldev = NULL;
565         struct mlx5_core_dev *tmp_dev;
566         int err;
567
568         if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
569             !MLX5_CAP_GEN(dev, lag_master) ||
570             (MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS))
571                 return;
572
573         tmp_dev = mlx5_get_next_phys_dev(dev);
574         if (tmp_dev)
575                 ldev = tmp_dev->priv.lag;
576
577         if (!ldev) {
578                 ldev = mlx5_lag_dev_alloc();
579                 if (!ldev) {
580                         mlx5_core_err(dev, "Failed to alloc lag dev\n");
581                         return;
582                 }
583         }
584
585         mlx5_lag_dev_add_pf(ldev, dev, netdev);
586
587         if (!ldev->nb.notifier_call) {
588                 ldev->nb.notifier_call = mlx5_lag_netdev_event;
589                 if (register_netdevice_notifier_dev_net(netdev, &ldev->nb,
590                                                         &ldev->nn)) {
591                         ldev->nb.notifier_call = NULL;
592                         mlx5_core_err(dev, "Failed to register LAG netdev notifier\n");
593                 }
594         }
595
596         err = mlx5_lag_mp_init(ldev);
597         if (err)
598                 mlx5_core_err(dev, "Failed to init multipath lag err=%d\n",
599                               err);
600 }
601
602 /* Must be called with intf_mutex held */
603 void mlx5_lag_remove(struct mlx5_core_dev *dev, struct net_device *netdev)
604 {
605         struct mlx5_lag *ldev;
606         int i;
607
608         ldev = mlx5_lag_dev_get(dev);
609         if (!ldev)
610                 return;
611
612         if (__mlx5_lag_is_active(ldev))
613                 mlx5_deactivate_lag(ldev);
614
615         mlx5_lag_dev_remove_pf(ldev, dev);
616
617         for (i = 0; i < MLX5_MAX_PORTS; i++)
618                 if (ldev->pf[i].dev)
619                         break;
620
621         if (i == MLX5_MAX_PORTS) {
622                 if (ldev->nb.notifier_call)
623                         unregister_netdevice_notifier_dev_net(netdev, &ldev->nb,
624                                                               &ldev->nn);
625                 mlx5_lag_mp_cleanup(ldev);
626                 cancel_delayed_work_sync(&ldev->bond_work);
627                 mlx5_lag_dev_free(ldev);
628         }
629 }
630
631 bool mlx5_lag_is_roce(struct mlx5_core_dev *dev)
632 {
633         struct mlx5_lag *ldev;
634         bool res;
635
636         mutex_lock(&lag_mutex);
637         ldev = mlx5_lag_dev_get(dev);
638         res  = ldev && __mlx5_lag_is_roce(ldev);
639         mutex_unlock(&lag_mutex);
640
641         return res;
642 }
643 EXPORT_SYMBOL(mlx5_lag_is_roce);
644
645 bool mlx5_lag_is_active(struct mlx5_core_dev *dev)
646 {
647         struct mlx5_lag *ldev;
648         bool res;
649
650         mutex_lock(&lag_mutex);
651         ldev = mlx5_lag_dev_get(dev);
652         res  = ldev && __mlx5_lag_is_active(ldev);
653         mutex_unlock(&lag_mutex);
654
655         return res;
656 }
657 EXPORT_SYMBOL(mlx5_lag_is_active);
658
659 bool mlx5_lag_is_sriov(struct mlx5_core_dev *dev)
660 {
661         struct mlx5_lag *ldev;
662         bool res;
663
664         mutex_lock(&lag_mutex);
665         ldev = mlx5_lag_dev_get(dev);
666         res  = ldev && __mlx5_lag_is_sriov(ldev);
667         mutex_unlock(&lag_mutex);
668
669         return res;
670 }
671 EXPORT_SYMBOL(mlx5_lag_is_sriov);
672
673 void mlx5_lag_update(struct mlx5_core_dev *dev)
674 {
675         struct mlx5_lag *ldev;
676
677         mlx5_dev_list_lock();
678         ldev = mlx5_lag_dev_get(dev);
679         if (!ldev)
680                 goto unlock;
681
682         mlx5_do_bond(ldev);
683
684 unlock:
685         mlx5_dev_list_unlock();
686 }
687
688 struct net_device *mlx5_lag_get_roce_netdev(struct mlx5_core_dev *dev)
689 {
690         struct net_device *ndev = NULL;
691         struct mlx5_lag *ldev;
692
693         mutex_lock(&lag_mutex);
694         ldev = mlx5_lag_dev_get(dev);
695
696         if (!(ldev && __mlx5_lag_is_roce(ldev)))
697                 goto unlock;
698
699         if (ldev->tracker.tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
700                 ndev = ldev->tracker.netdev_state[MLX5_LAG_P1].tx_enabled ?
701                        ldev->pf[MLX5_LAG_P1].netdev :
702                        ldev->pf[MLX5_LAG_P2].netdev;
703         } else {
704                 ndev = ldev->pf[MLX5_LAG_P1].netdev;
705         }
706         if (ndev)
707                 dev_hold(ndev);
708
709 unlock:
710         mutex_unlock(&lag_mutex);
711
712         return ndev;
713 }
714 EXPORT_SYMBOL(mlx5_lag_get_roce_netdev);
715
716 bool mlx5_lag_intf_add(struct mlx5_interface *intf, struct mlx5_priv *priv)
717 {
718         struct mlx5_core_dev *dev = container_of(priv, struct mlx5_core_dev,
719                                                  priv);
720         struct mlx5_lag *ldev;
721
722         if (intf->protocol != MLX5_INTERFACE_PROTOCOL_IB)
723                 return true;
724
725         ldev = mlx5_lag_dev_get(dev);
726         if (!ldev || !__mlx5_lag_is_roce(ldev) ||
727             ldev->pf[MLX5_LAG_P1].dev == dev)
728                 return true;
729
730         /* If bonded, we do not add an IB device for PF1. */
731         return false;
732 }
733
734 int mlx5_lag_query_cong_counters(struct mlx5_core_dev *dev,
735                                  u64 *values,
736                                  int num_counters,
737                                  size_t *offsets)
738 {
739         int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
740         struct mlx5_core_dev *mdev[MLX5_MAX_PORTS];
741         struct mlx5_lag *ldev;
742         int num_ports;
743         int ret, i, j;
744         void *out;
745
746         out = kvzalloc(outlen, GFP_KERNEL);
747         if (!out)
748                 return -ENOMEM;
749
750         memset(values, 0, sizeof(*values) * num_counters);
751
752         mutex_lock(&lag_mutex);
753         ldev = mlx5_lag_dev_get(dev);
754         if (ldev && __mlx5_lag_is_roce(ldev)) {
755                 num_ports = MLX5_MAX_PORTS;
756                 mdev[MLX5_LAG_P1] = ldev->pf[MLX5_LAG_P1].dev;
757                 mdev[MLX5_LAG_P2] = ldev->pf[MLX5_LAG_P2].dev;
758         } else {
759                 num_ports = 1;
760                 mdev[MLX5_LAG_P1] = dev;
761         }
762
763         for (i = 0; i < num_ports; ++i) {
764                 ret = mlx5_cmd_query_cong_counter(mdev[i], false, out, outlen);
765                 if (ret)
766                         goto unlock;
767
768                 for (j = 0; j < num_counters; ++j)
769                         values[j] += be64_to_cpup((__be64 *)(out + offsets[j]));
770         }
771
772 unlock:
773         mutex_unlock(&lag_mutex);
774         kvfree(out);
775         return ret;
776 }
777 EXPORT_SYMBOL(mlx5_lag_query_cong_counters);