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