net: Add depends on OF_NET for LiteX's LiteETH
[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/eswitch.h>
36 #include <linux/mlx5/vport.h>
37 #include "lib/devcom.h"
38 #include "mlx5_core.h"
39 #include "eswitch.h"
40 #include "lag.h"
41 #include "lag_mp.h"
42
43 /* General purpose, use for short periods of time.
44  * Beware of lock dependencies (preferably, no locks should be acquired
45  * under it).
46  */
47 static DEFINE_SPINLOCK(lag_lock);
48
49 static int mlx5_cmd_create_lag(struct mlx5_core_dev *dev, u8 remap_port1,
50                                u8 remap_port2, bool shared_fdb)
51 {
52         u32 in[MLX5_ST_SZ_DW(create_lag_in)] = {};
53         void *lag_ctx = MLX5_ADDR_OF(create_lag_in, in, ctx);
54
55         MLX5_SET(create_lag_in, in, opcode, MLX5_CMD_OP_CREATE_LAG);
56
57         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
58         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
59         MLX5_SET(lagc, lag_ctx, fdb_selection_mode, shared_fdb);
60
61         return mlx5_cmd_exec_in(dev, create_lag, in);
62 }
63
64 static int mlx5_cmd_modify_lag(struct mlx5_core_dev *dev, u8 remap_port1,
65                                u8 remap_port2)
66 {
67         u32 in[MLX5_ST_SZ_DW(modify_lag_in)] = {};
68         void *lag_ctx = MLX5_ADDR_OF(modify_lag_in, in, ctx);
69
70         MLX5_SET(modify_lag_in, in, opcode, MLX5_CMD_OP_MODIFY_LAG);
71         MLX5_SET(modify_lag_in, in, field_select, 0x1);
72
73         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
74         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
75
76         return mlx5_cmd_exec_in(dev, modify_lag, in);
77 }
78
79 int mlx5_cmd_create_vport_lag(struct mlx5_core_dev *dev)
80 {
81         u32 in[MLX5_ST_SZ_DW(create_vport_lag_in)] = {};
82
83         MLX5_SET(create_vport_lag_in, in, opcode, MLX5_CMD_OP_CREATE_VPORT_LAG);
84
85         return mlx5_cmd_exec_in(dev, create_vport_lag, in);
86 }
87 EXPORT_SYMBOL(mlx5_cmd_create_vport_lag);
88
89 int mlx5_cmd_destroy_vport_lag(struct mlx5_core_dev *dev)
90 {
91         u32 in[MLX5_ST_SZ_DW(destroy_vport_lag_in)] = {};
92
93         MLX5_SET(destroy_vport_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_VPORT_LAG);
94
95         return mlx5_cmd_exec_in(dev, destroy_vport_lag, in);
96 }
97 EXPORT_SYMBOL(mlx5_cmd_destroy_vport_lag);
98
99 static int mlx5_lag_netdev_event(struct notifier_block *this,
100                                  unsigned long event, void *ptr);
101 static void mlx5_do_bond_work(struct work_struct *work);
102
103 static void mlx5_ldev_free(struct kref *ref)
104 {
105         struct mlx5_lag *ldev = container_of(ref, struct mlx5_lag, ref);
106
107         if (ldev->nb.notifier_call)
108                 unregister_netdevice_notifier_net(&init_net, &ldev->nb);
109         mlx5_lag_mp_cleanup(ldev);
110         cancel_delayed_work_sync(&ldev->bond_work);
111         destroy_workqueue(ldev->wq);
112         kfree(ldev);
113 }
114
115 static void mlx5_ldev_put(struct mlx5_lag *ldev)
116 {
117         kref_put(&ldev->ref, mlx5_ldev_free);
118 }
119
120 static void mlx5_ldev_get(struct mlx5_lag *ldev)
121 {
122         kref_get(&ldev->ref);
123 }
124
125 static struct mlx5_lag *mlx5_lag_dev_alloc(struct mlx5_core_dev *dev)
126 {
127         struct mlx5_lag *ldev;
128         int err;
129
130         ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
131         if (!ldev)
132                 return NULL;
133
134         ldev->wq = create_singlethread_workqueue("mlx5_lag");
135         if (!ldev->wq) {
136                 kfree(ldev);
137                 return NULL;
138         }
139
140         kref_init(&ldev->ref);
141         INIT_DELAYED_WORK(&ldev->bond_work, mlx5_do_bond_work);
142
143         ldev->nb.notifier_call = mlx5_lag_netdev_event;
144         if (register_netdevice_notifier_net(&init_net, &ldev->nb)) {
145                 ldev->nb.notifier_call = NULL;
146                 mlx5_core_err(dev, "Failed to register LAG netdev notifier\n");
147         }
148
149         err = mlx5_lag_mp_init(ldev);
150         if (err)
151                 mlx5_core_err(dev, "Failed to init multipath lag err=%d\n",
152                               err);
153
154         return ldev;
155 }
156
157 int mlx5_lag_dev_get_netdev_idx(struct mlx5_lag *ldev,
158                                 struct net_device *ndev)
159 {
160         int i;
161
162         for (i = 0; i < MLX5_MAX_PORTS; i++)
163                 if (ldev->pf[i].netdev == ndev)
164                         return i;
165
166         return -ENOENT;
167 }
168
169 static bool __mlx5_lag_is_roce(struct mlx5_lag *ldev)
170 {
171         return !!(ldev->flags & MLX5_LAG_FLAG_ROCE);
172 }
173
174 static bool __mlx5_lag_is_sriov(struct mlx5_lag *ldev)
175 {
176         return !!(ldev->flags & MLX5_LAG_FLAG_SRIOV);
177 }
178
179 static void mlx5_infer_tx_affinity_mapping(struct lag_tracker *tracker,
180                                            u8 *port1, u8 *port2)
181 {
182         bool p1en;
183         bool p2en;
184
185         p1en = tracker->netdev_state[MLX5_LAG_P1].tx_enabled &&
186                tracker->netdev_state[MLX5_LAG_P1].link_up;
187
188         p2en = tracker->netdev_state[MLX5_LAG_P2].tx_enabled &&
189                tracker->netdev_state[MLX5_LAG_P2].link_up;
190
191         *port1 = 1;
192         *port2 = 2;
193         if ((!p1en && !p2en) || (p1en && p2en))
194                 return;
195
196         if (p1en)
197                 *port2 = 1;
198         else
199                 *port1 = 2;
200 }
201
202 void mlx5_modify_lag(struct mlx5_lag *ldev,
203                      struct lag_tracker *tracker)
204 {
205         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
206         u8 v2p_port1, v2p_port2;
207         int err;
208
209         mlx5_infer_tx_affinity_mapping(tracker, &v2p_port1,
210                                        &v2p_port2);
211
212         if (v2p_port1 != ldev->v2p_map[MLX5_LAG_P1] ||
213             v2p_port2 != ldev->v2p_map[MLX5_LAG_P2]) {
214                 ldev->v2p_map[MLX5_LAG_P1] = v2p_port1;
215                 ldev->v2p_map[MLX5_LAG_P2] = v2p_port2;
216
217                 mlx5_core_info(dev0, "modify lag map port 1:%d port 2:%d",
218                                ldev->v2p_map[MLX5_LAG_P1],
219                                ldev->v2p_map[MLX5_LAG_P2]);
220
221                 err = mlx5_cmd_modify_lag(dev0, v2p_port1, v2p_port2);
222                 if (err)
223                         mlx5_core_err(dev0,
224                                       "Failed to modify LAG (%d)\n",
225                                       err);
226         }
227 }
228
229 static int mlx5_create_lag(struct mlx5_lag *ldev,
230                            struct lag_tracker *tracker,
231                            bool shared_fdb)
232 {
233         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
234         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
235         u32 in[MLX5_ST_SZ_DW(destroy_lag_in)] = {};
236         int err;
237
238         mlx5_infer_tx_affinity_mapping(tracker, &ldev->v2p_map[MLX5_LAG_P1],
239                                        &ldev->v2p_map[MLX5_LAG_P2]);
240
241         mlx5_core_info(dev0, "lag map port 1:%d port 2:%d shared_fdb:%d",
242                        ldev->v2p_map[MLX5_LAG_P1], ldev->v2p_map[MLX5_LAG_P2],
243                        shared_fdb);
244
245         err = mlx5_cmd_create_lag(dev0, ldev->v2p_map[MLX5_LAG_P1],
246                                   ldev->v2p_map[MLX5_LAG_P2], shared_fdb);
247         if (err) {
248                 mlx5_core_err(dev0,
249                               "Failed to create LAG (%d)\n",
250                               err);
251                 return err;
252         }
253
254         if (shared_fdb) {
255                 err = mlx5_eswitch_offloads_config_single_fdb(dev0->priv.eswitch,
256                                                               dev1->priv.eswitch);
257                 if (err)
258                         mlx5_core_err(dev0, "Can't enable single FDB mode\n");
259                 else
260                         mlx5_core_info(dev0, "Operation mode is single FDB\n");
261         }
262
263         if (err) {
264                 MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
265                 if (mlx5_cmd_exec_in(dev0, destroy_lag, in))
266                         mlx5_core_err(dev0,
267                                       "Failed to deactivate RoCE LAG; driver restart required\n");
268         }
269
270         return err;
271 }
272
273 int mlx5_activate_lag(struct mlx5_lag *ldev,
274                       struct lag_tracker *tracker,
275                       u8 flags,
276                       bool shared_fdb)
277 {
278         bool roce_lag = !!(flags & MLX5_LAG_FLAG_ROCE);
279         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
280         int err;
281
282         err = mlx5_create_lag(ldev, tracker, shared_fdb);
283         if (err) {
284                 if (roce_lag) {
285                         mlx5_core_err(dev0,
286                                       "Failed to activate RoCE LAG\n");
287                 } else {
288                         mlx5_core_err(dev0,
289                                       "Failed to activate VF LAG\n"
290                                       "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
291                 }
292                 return err;
293         }
294
295         ldev->flags |= flags;
296         ldev->shared_fdb = shared_fdb;
297         return 0;
298 }
299
300 static int mlx5_deactivate_lag(struct mlx5_lag *ldev)
301 {
302         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
303         u32 in[MLX5_ST_SZ_DW(destroy_lag_in)] = {};
304         bool roce_lag = __mlx5_lag_is_roce(ldev);
305         int err;
306
307         ldev->flags &= ~MLX5_LAG_MODE_FLAGS;
308
309         if (ldev->shared_fdb) {
310                 mlx5_eswitch_offloads_destroy_single_fdb(ldev->pf[MLX5_LAG_P1].dev->priv.eswitch,
311                                                          ldev->pf[MLX5_LAG_P2].dev->priv.eswitch);
312                 ldev->shared_fdb = false;
313         }
314
315         MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
316         err = mlx5_cmd_exec_in(dev0, destroy_lag, in);
317         if (err) {
318                 if (roce_lag) {
319                         mlx5_core_err(dev0,
320                                       "Failed to deactivate RoCE LAG; driver restart required\n");
321                 } else {
322                         mlx5_core_err(dev0,
323                                       "Failed to deactivate VF LAG; driver restart required\n"
324                                       "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
325                 }
326         }
327
328         return err;
329 }
330
331 static bool mlx5_lag_check_prereq(struct mlx5_lag *ldev)
332 {
333         if (!ldev->pf[MLX5_LAG_P1].dev || !ldev->pf[MLX5_LAG_P2].dev)
334                 return false;
335
336 #ifdef CONFIG_MLX5_ESWITCH
337         return mlx5_esw_lag_prereq(ldev->pf[MLX5_LAG_P1].dev,
338                                    ldev->pf[MLX5_LAG_P2].dev);
339 #else
340         return (!mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P1].dev) &&
341                 !mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P2].dev));
342 #endif
343 }
344
345 static void mlx5_lag_add_devices(struct mlx5_lag *ldev)
346 {
347         int i;
348
349         for (i = 0; i < MLX5_MAX_PORTS; i++) {
350                 if (!ldev->pf[i].dev)
351                         continue;
352
353                 if (ldev->pf[i].dev->priv.flags &
354                     MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
355                         continue;
356
357                 ldev->pf[i].dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
358                 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
359         }
360 }
361
362 static void mlx5_lag_remove_devices(struct mlx5_lag *ldev)
363 {
364         int i;
365
366         for (i = 0; i < MLX5_MAX_PORTS; i++) {
367                 if (!ldev->pf[i].dev)
368                         continue;
369
370                 if (ldev->pf[i].dev->priv.flags &
371                     MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
372                         continue;
373
374                 ldev->pf[i].dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
375                 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
376         }
377 }
378
379 static void mlx5_disable_lag(struct mlx5_lag *ldev)
380 {
381         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
382         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
383         bool shared_fdb = ldev->shared_fdb;
384         bool roce_lag;
385         int err;
386
387         roce_lag = __mlx5_lag_is_roce(ldev);
388
389         if (shared_fdb) {
390                 mlx5_lag_remove_devices(ldev);
391         } else if (roce_lag) {
392                 if (!(dev0->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)) {
393                         dev0->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
394                         mlx5_rescan_drivers_locked(dev0);
395                 }
396                 mlx5_nic_vport_disable_roce(dev1);
397         }
398
399         err = mlx5_deactivate_lag(ldev);
400         if (err)
401                 return;
402
403         if (shared_fdb || roce_lag)
404                 mlx5_lag_add_devices(ldev);
405
406         if (shared_fdb) {
407                 if (!(dev0->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV))
408                         mlx5_eswitch_reload_reps(dev0->priv.eswitch);
409                 if (!(dev1->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV))
410                         mlx5_eswitch_reload_reps(dev1->priv.eswitch);
411         }
412 }
413
414 static bool mlx5_shared_fdb_supported(struct mlx5_lag *ldev)
415 {
416         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
417         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
418
419         if (is_mdev_switchdev_mode(dev0) &&
420             is_mdev_switchdev_mode(dev1) &&
421             mlx5_eswitch_vport_match_metadata_enabled(dev0->priv.eswitch) &&
422             mlx5_eswitch_vport_match_metadata_enabled(dev1->priv.eswitch) &&
423             mlx5_devcom_is_paired(dev0->priv.devcom,
424                                   MLX5_DEVCOM_ESW_OFFLOADS) &&
425             MLX5_CAP_GEN(dev1, lag_native_fdb_selection) &&
426             MLX5_CAP_ESW(dev1, root_ft_on_other_esw) &&
427             MLX5_CAP_ESW(dev0, esw_shared_ingress_acl))
428                 return true;
429
430         return false;
431 }
432
433 static void mlx5_do_bond(struct mlx5_lag *ldev)
434 {
435         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
436         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
437         struct lag_tracker tracker;
438         bool do_bond, roce_lag;
439         int err;
440
441         if (!mlx5_lag_is_ready(ldev)) {
442                 do_bond = false;
443         } else {
444                 tracker = ldev->tracker;
445
446                 do_bond = tracker.is_bonded && mlx5_lag_check_prereq(ldev);
447         }
448
449         if (do_bond && !__mlx5_lag_is_active(ldev)) {
450                 bool shared_fdb = mlx5_shared_fdb_supported(ldev);
451
452                 roce_lag = !mlx5_sriov_is_enabled(dev0) &&
453                            !mlx5_sriov_is_enabled(dev1);
454
455 #ifdef CONFIG_MLX5_ESWITCH
456                 roce_lag = roce_lag &&
457                            dev0->priv.eswitch->mode == MLX5_ESWITCH_NONE &&
458                            dev1->priv.eswitch->mode == MLX5_ESWITCH_NONE;
459 #endif
460
461                 if (shared_fdb || roce_lag)
462                         mlx5_lag_remove_devices(ldev);
463
464                 err = mlx5_activate_lag(ldev, &tracker,
465                                         roce_lag ? MLX5_LAG_FLAG_ROCE :
466                                                    MLX5_LAG_FLAG_SRIOV,
467                                         shared_fdb);
468                 if (err) {
469                         if (shared_fdb || roce_lag)
470                                 mlx5_lag_add_devices(ldev);
471
472                         return;
473                 } else if (roce_lag) {
474                         dev0->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
475                         mlx5_rescan_drivers_locked(dev0);
476                         mlx5_nic_vport_enable_roce(dev1);
477                 } else if (shared_fdb) {
478                         dev0->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
479                         mlx5_rescan_drivers_locked(dev0);
480
481                         err = mlx5_eswitch_reload_reps(dev0->priv.eswitch);
482                         if (!err)
483                                 err = mlx5_eswitch_reload_reps(dev1->priv.eswitch);
484
485                         if (err) {
486                                 dev0->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
487                                 mlx5_rescan_drivers_locked(dev0);
488                                 mlx5_deactivate_lag(ldev);
489                                 mlx5_lag_add_devices(ldev);
490                                 mlx5_eswitch_reload_reps(dev0->priv.eswitch);
491                                 mlx5_eswitch_reload_reps(dev1->priv.eswitch);
492                                 mlx5_core_err(dev0, "Failed to enable lag\n");
493                                 return;
494                         }
495                 }
496         } else if (do_bond && __mlx5_lag_is_active(ldev)) {
497                 mlx5_modify_lag(ldev, &tracker);
498         } else if (!do_bond && __mlx5_lag_is_active(ldev)) {
499                 mlx5_disable_lag(ldev);
500         }
501 }
502
503 static void mlx5_queue_bond_work(struct mlx5_lag *ldev, unsigned long delay)
504 {
505         queue_delayed_work(ldev->wq, &ldev->bond_work, delay);
506 }
507
508 static void mlx5_lag_lock_eswitches(struct mlx5_core_dev *dev0,
509                                     struct mlx5_core_dev *dev1)
510 {
511         if (dev0)
512                 mlx5_esw_lock(dev0->priv.eswitch);
513         if (dev1)
514                 mlx5_esw_lock(dev1->priv.eswitch);
515 }
516
517 static void mlx5_lag_unlock_eswitches(struct mlx5_core_dev *dev0,
518                                       struct mlx5_core_dev *dev1)
519 {
520         if (dev1)
521                 mlx5_esw_unlock(dev1->priv.eswitch);
522         if (dev0)
523                 mlx5_esw_unlock(dev0->priv.eswitch);
524 }
525
526 static void mlx5_do_bond_work(struct work_struct *work)
527 {
528         struct delayed_work *delayed_work = to_delayed_work(work);
529         struct mlx5_lag *ldev = container_of(delayed_work, struct mlx5_lag,
530                                              bond_work);
531         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
532         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
533         int status;
534
535         status = mlx5_dev_list_trylock();
536         if (!status) {
537                 mlx5_queue_bond_work(ldev, HZ);
538                 return;
539         }
540
541         if (ldev->mode_changes_in_progress) {
542                 mlx5_dev_list_unlock();
543                 mlx5_queue_bond_work(ldev, HZ);
544                 return;
545         }
546
547         mlx5_lag_lock_eswitches(dev0, dev1);
548         mlx5_do_bond(ldev);
549         mlx5_lag_unlock_eswitches(dev0, dev1);
550         mlx5_dev_list_unlock();
551 }
552
553 static int mlx5_handle_changeupper_event(struct mlx5_lag *ldev,
554                                          struct lag_tracker *tracker,
555                                          struct net_device *ndev,
556                                          struct netdev_notifier_changeupper_info *info)
557 {
558         struct net_device *upper = info->upper_dev, *ndev_tmp;
559         struct netdev_lag_upper_info *lag_upper_info = NULL;
560         bool is_bonded, is_in_lag, mode_supported;
561         int bond_status = 0;
562         int num_slaves = 0;
563         int idx;
564
565         if (!netif_is_lag_master(upper))
566                 return 0;
567
568         if (info->linking)
569                 lag_upper_info = info->upper_info;
570
571         /* The event may still be of interest if the slave does not belong to
572          * us, but is enslaved to a master which has one or more of our netdevs
573          * as slaves (e.g., if a new slave is added to a master that bonds two
574          * of our netdevs, we should unbond).
575          */
576         rcu_read_lock();
577         for_each_netdev_in_bond_rcu(upper, ndev_tmp) {
578                 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
579                 if (idx >= 0)
580                         bond_status |= (1 << idx);
581
582                 num_slaves++;
583         }
584         rcu_read_unlock();
585
586         /* None of this lagdev's netdevs are slaves of this master. */
587         if (!(bond_status & 0x3))
588                 return 0;
589
590         if (lag_upper_info)
591                 tracker->tx_type = lag_upper_info->tx_type;
592
593         /* Determine bonding status:
594          * A device is considered bonded if both its physical ports are slaves
595          * of the same lag master, and only them.
596          */
597         is_in_lag = num_slaves == MLX5_MAX_PORTS && bond_status == 0x3;
598
599         if (!mlx5_lag_is_ready(ldev) && is_in_lag) {
600                 NL_SET_ERR_MSG_MOD(info->info.extack,
601                                    "Can't activate LAG offload, PF is configured with more than 64 VFs");
602                 return 0;
603         }
604
605         /* Lag mode must be activebackup or hash. */
606         mode_supported = tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP ||
607                          tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH;
608
609         if (is_in_lag && !mode_supported)
610                 NL_SET_ERR_MSG_MOD(info->info.extack,
611                                    "Can't activate LAG offload, TX type isn't supported");
612
613         is_bonded = is_in_lag && mode_supported;
614         if (tracker->is_bonded != is_bonded) {
615                 tracker->is_bonded = is_bonded;
616                 return 1;
617         }
618
619         return 0;
620 }
621
622 static int mlx5_handle_changelowerstate_event(struct mlx5_lag *ldev,
623                                               struct lag_tracker *tracker,
624                                               struct net_device *ndev,
625                                               struct netdev_notifier_changelowerstate_info *info)
626 {
627         struct netdev_lag_lower_state_info *lag_lower_info;
628         int idx;
629
630         if (!netif_is_lag_port(ndev))
631                 return 0;
632
633         idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev);
634         if (idx < 0)
635                 return 0;
636
637         /* This information is used to determine virtual to physical
638          * port mapping.
639          */
640         lag_lower_info = info->lower_state_info;
641         if (!lag_lower_info)
642                 return 0;
643
644         tracker->netdev_state[idx] = *lag_lower_info;
645
646         return 1;
647 }
648
649 static int mlx5_lag_netdev_event(struct notifier_block *this,
650                                  unsigned long event, void *ptr)
651 {
652         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
653         struct lag_tracker tracker;
654         struct mlx5_lag *ldev;
655         int changed = 0;
656
657         if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
658                 return NOTIFY_DONE;
659
660         ldev    = container_of(this, struct mlx5_lag, nb);
661
662         if (!mlx5_lag_is_ready(ldev) && event == NETDEV_CHANGELOWERSTATE)
663                 return NOTIFY_DONE;
664
665         tracker = ldev->tracker;
666
667         switch (event) {
668         case NETDEV_CHANGEUPPER:
669                 changed = mlx5_handle_changeupper_event(ldev, &tracker, ndev,
670                                                         ptr);
671                 break;
672         case NETDEV_CHANGELOWERSTATE:
673                 changed = mlx5_handle_changelowerstate_event(ldev, &tracker,
674                                                              ndev, ptr);
675                 break;
676         }
677
678         ldev->tracker = tracker;
679
680         if (changed)
681                 mlx5_queue_bond_work(ldev, 0);
682
683         return NOTIFY_DONE;
684 }
685
686 static void mlx5_ldev_add_netdev(struct mlx5_lag *ldev,
687                                  struct mlx5_core_dev *dev,
688                                  struct net_device *netdev)
689 {
690         unsigned int fn = PCI_FUNC(dev->pdev->devfn);
691
692         if (fn >= MLX5_MAX_PORTS)
693                 return;
694
695         spin_lock(&lag_lock);
696         ldev->pf[fn].netdev = netdev;
697         ldev->tracker.netdev_state[fn].link_up = 0;
698         ldev->tracker.netdev_state[fn].tx_enabled = 0;
699         spin_unlock(&lag_lock);
700 }
701
702 static void mlx5_ldev_remove_netdev(struct mlx5_lag *ldev,
703                                     struct net_device *netdev)
704 {
705         int i;
706
707         spin_lock(&lag_lock);
708         for (i = 0; i < MLX5_MAX_PORTS; i++) {
709                 if (ldev->pf[i].netdev == netdev) {
710                         ldev->pf[i].netdev = NULL;
711                         break;
712                 }
713         }
714         spin_unlock(&lag_lock);
715 }
716
717 static void mlx5_ldev_add_mdev(struct mlx5_lag *ldev,
718                                struct mlx5_core_dev *dev)
719 {
720         unsigned int fn = PCI_FUNC(dev->pdev->devfn);
721
722         if (fn >= MLX5_MAX_PORTS)
723                 return;
724
725         ldev->pf[fn].dev = dev;
726         dev->priv.lag = ldev;
727 }
728
729 /* Must be called with intf_mutex held */
730 static void mlx5_ldev_remove_mdev(struct mlx5_lag *ldev,
731                                   struct mlx5_core_dev *dev)
732 {
733         int i;
734
735         for (i = 0; i < MLX5_MAX_PORTS; i++)
736                 if (ldev->pf[i].dev == dev)
737                         break;
738
739         if (i == MLX5_MAX_PORTS)
740                 return;
741
742         ldev->pf[i].dev = NULL;
743         dev->priv.lag = NULL;
744 }
745
746 /* Must be called with intf_mutex held */
747 static int __mlx5_lag_dev_add_mdev(struct mlx5_core_dev *dev)
748 {
749         struct mlx5_lag *ldev = NULL;
750         struct mlx5_core_dev *tmp_dev;
751
752         if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
753             !MLX5_CAP_GEN(dev, lag_master) ||
754             MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS)
755                 return 0;
756
757         tmp_dev = mlx5_get_next_phys_dev(dev);
758         if (tmp_dev)
759                 ldev = tmp_dev->priv.lag;
760
761         if (!ldev) {
762                 ldev = mlx5_lag_dev_alloc(dev);
763                 if (!ldev) {
764                         mlx5_core_err(dev, "Failed to alloc lag dev\n");
765                         return 0;
766                 }
767         } else {
768                 if (ldev->mode_changes_in_progress)
769                         return -EAGAIN;
770                 mlx5_ldev_get(ldev);
771         }
772
773         mlx5_ldev_add_mdev(ldev, dev);
774
775         return 0;
776 }
777
778 void mlx5_lag_remove_mdev(struct mlx5_core_dev *dev)
779 {
780         struct mlx5_lag *ldev;
781
782         ldev = mlx5_lag_dev(dev);
783         if (!ldev)
784                 return;
785
786 recheck:
787         mlx5_dev_list_lock();
788         if (ldev->mode_changes_in_progress) {
789                 mlx5_dev_list_unlock();
790                 msleep(100);
791                 goto recheck;
792         }
793         mlx5_ldev_remove_mdev(ldev, dev);
794         mlx5_dev_list_unlock();
795         mlx5_ldev_put(ldev);
796 }
797
798 void mlx5_lag_add_mdev(struct mlx5_core_dev *dev)
799 {
800         int err;
801
802 recheck:
803         mlx5_dev_list_lock();
804         err = __mlx5_lag_dev_add_mdev(dev);
805         if (err) {
806                 mlx5_dev_list_unlock();
807                 msleep(100);
808                 goto recheck;
809         }
810         mlx5_dev_list_unlock();
811 }
812
813 /* Must be called with intf_mutex held */
814 void mlx5_lag_remove_netdev(struct mlx5_core_dev *dev,
815                             struct net_device *netdev)
816 {
817         struct mlx5_lag *ldev;
818
819         ldev = mlx5_lag_dev(dev);
820         if (!ldev)
821                 return;
822
823         mlx5_ldev_remove_netdev(ldev, netdev);
824         ldev->flags &= ~MLX5_LAG_FLAG_READY;
825
826         if (__mlx5_lag_is_active(ldev))
827                 mlx5_queue_bond_work(ldev, 0);
828 }
829
830 /* Must be called with intf_mutex held */
831 void mlx5_lag_add_netdev(struct mlx5_core_dev *dev,
832                          struct net_device *netdev)
833 {
834         struct mlx5_lag *ldev;
835         int i;
836
837         ldev = mlx5_lag_dev(dev);
838         if (!ldev)
839                 return;
840
841         mlx5_ldev_add_netdev(ldev, dev, netdev);
842
843         for (i = 0; i < MLX5_MAX_PORTS; i++)
844                 if (!ldev->pf[i].dev)
845                         break;
846
847         if (i >= MLX5_MAX_PORTS)
848                 ldev->flags |= MLX5_LAG_FLAG_READY;
849         mlx5_queue_bond_work(ldev, 0);
850 }
851
852 bool mlx5_lag_is_roce(struct mlx5_core_dev *dev)
853 {
854         struct mlx5_lag *ldev;
855         bool res;
856
857         spin_lock(&lag_lock);
858         ldev = mlx5_lag_dev(dev);
859         res  = ldev && __mlx5_lag_is_roce(ldev);
860         spin_unlock(&lag_lock);
861
862         return res;
863 }
864 EXPORT_SYMBOL(mlx5_lag_is_roce);
865
866 bool mlx5_lag_is_active(struct mlx5_core_dev *dev)
867 {
868         struct mlx5_lag *ldev;
869         bool res;
870
871         spin_lock(&lag_lock);
872         ldev = mlx5_lag_dev(dev);
873         res  = ldev && __mlx5_lag_is_active(ldev);
874         spin_unlock(&lag_lock);
875
876         return res;
877 }
878 EXPORT_SYMBOL(mlx5_lag_is_active);
879
880 bool mlx5_lag_is_master(struct mlx5_core_dev *dev)
881 {
882         struct mlx5_lag *ldev;
883         bool res;
884
885         spin_lock(&lag_lock);
886         ldev = mlx5_lag_dev(dev);
887         res = ldev && __mlx5_lag_is_active(ldev) &&
888                 dev == ldev->pf[MLX5_LAG_P1].dev;
889         spin_unlock(&lag_lock);
890
891         return res;
892 }
893 EXPORT_SYMBOL(mlx5_lag_is_master);
894
895 bool mlx5_lag_is_sriov(struct mlx5_core_dev *dev)
896 {
897         struct mlx5_lag *ldev;
898         bool res;
899
900         spin_lock(&lag_lock);
901         ldev = mlx5_lag_dev(dev);
902         res  = ldev && __mlx5_lag_is_sriov(ldev);
903         spin_unlock(&lag_lock);
904
905         return res;
906 }
907 EXPORT_SYMBOL(mlx5_lag_is_sriov);
908
909 bool mlx5_lag_is_shared_fdb(struct mlx5_core_dev *dev)
910 {
911         struct mlx5_lag *ldev;
912         bool res;
913
914         spin_lock(&lag_lock);
915         ldev = mlx5_lag_dev(dev);
916         res = ldev && __mlx5_lag_is_sriov(ldev) && ldev->shared_fdb;
917         spin_unlock(&lag_lock);
918
919         return res;
920 }
921 EXPORT_SYMBOL(mlx5_lag_is_shared_fdb);
922
923 void mlx5_lag_disable_change(struct mlx5_core_dev *dev)
924 {
925         struct mlx5_core_dev *dev0;
926         struct mlx5_core_dev *dev1;
927         struct mlx5_lag *ldev;
928
929         mlx5_dev_list_lock();
930
931         ldev = mlx5_lag_dev(dev);
932         dev0 = ldev->pf[MLX5_LAG_P1].dev;
933         dev1 = ldev->pf[MLX5_LAG_P2].dev;
934
935         ldev->mode_changes_in_progress++;
936         if (__mlx5_lag_is_active(ldev)) {
937                 mlx5_lag_lock_eswitches(dev0, dev1);
938                 mlx5_disable_lag(ldev);
939                 mlx5_lag_unlock_eswitches(dev0, dev1);
940         }
941         mlx5_dev_list_unlock();
942 }
943
944 void mlx5_lag_enable_change(struct mlx5_core_dev *dev)
945 {
946         struct mlx5_lag *ldev;
947
948         mlx5_dev_list_lock();
949         ldev = mlx5_lag_dev(dev);
950         ldev->mode_changes_in_progress--;
951         mlx5_dev_list_unlock();
952         mlx5_queue_bond_work(ldev, 0);
953 }
954
955 struct net_device *mlx5_lag_get_roce_netdev(struct mlx5_core_dev *dev)
956 {
957         struct net_device *ndev = NULL;
958         struct mlx5_lag *ldev;
959
960         spin_lock(&lag_lock);
961         ldev = mlx5_lag_dev(dev);
962
963         if (!(ldev && __mlx5_lag_is_roce(ldev)))
964                 goto unlock;
965
966         if (ldev->tracker.tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
967                 ndev = ldev->tracker.netdev_state[MLX5_LAG_P1].tx_enabled ?
968                        ldev->pf[MLX5_LAG_P1].netdev :
969                        ldev->pf[MLX5_LAG_P2].netdev;
970         } else {
971                 ndev = ldev->pf[MLX5_LAG_P1].netdev;
972         }
973         if (ndev)
974                 dev_hold(ndev);
975
976 unlock:
977         spin_unlock(&lag_lock);
978
979         return ndev;
980 }
981 EXPORT_SYMBOL(mlx5_lag_get_roce_netdev);
982
983 u8 mlx5_lag_get_slave_port(struct mlx5_core_dev *dev,
984                            struct net_device *slave)
985 {
986         struct mlx5_lag *ldev;
987         u8 port = 0;
988
989         spin_lock(&lag_lock);
990         ldev = mlx5_lag_dev(dev);
991         if (!(ldev && __mlx5_lag_is_roce(ldev)))
992                 goto unlock;
993
994         if (ldev->pf[MLX5_LAG_P1].netdev == slave)
995                 port = MLX5_LAG_P1;
996         else
997                 port = MLX5_LAG_P2;
998
999         port = ldev->v2p_map[port];
1000
1001 unlock:
1002         spin_unlock(&lag_lock);
1003         return port;
1004 }
1005 EXPORT_SYMBOL(mlx5_lag_get_slave_port);
1006
1007 struct mlx5_core_dev *mlx5_lag_get_peer_mdev(struct mlx5_core_dev *dev)
1008 {
1009         struct mlx5_core_dev *peer_dev = NULL;
1010         struct mlx5_lag *ldev;
1011
1012         spin_lock(&lag_lock);
1013         ldev = mlx5_lag_dev(dev);
1014         if (!ldev)
1015                 goto unlock;
1016
1017         peer_dev = ldev->pf[MLX5_LAG_P1].dev == dev ?
1018                            ldev->pf[MLX5_LAG_P2].dev :
1019                            ldev->pf[MLX5_LAG_P1].dev;
1020
1021 unlock:
1022         spin_unlock(&lag_lock);
1023         return peer_dev;
1024 }
1025 EXPORT_SYMBOL(mlx5_lag_get_peer_mdev);
1026
1027 int mlx5_lag_query_cong_counters(struct mlx5_core_dev *dev,
1028                                  u64 *values,
1029                                  int num_counters,
1030                                  size_t *offsets)
1031 {
1032         int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
1033         struct mlx5_core_dev *mdev[MLX5_MAX_PORTS];
1034         struct mlx5_lag *ldev;
1035         int num_ports;
1036         int ret, i, j;
1037         void *out;
1038
1039         out = kvzalloc(outlen, GFP_KERNEL);
1040         if (!out)
1041                 return -ENOMEM;
1042
1043         memset(values, 0, sizeof(*values) * num_counters);
1044
1045         spin_lock(&lag_lock);
1046         ldev = mlx5_lag_dev(dev);
1047         if (ldev && __mlx5_lag_is_active(ldev)) {
1048                 num_ports = MLX5_MAX_PORTS;
1049                 mdev[MLX5_LAG_P1] = ldev->pf[MLX5_LAG_P1].dev;
1050                 mdev[MLX5_LAG_P2] = ldev->pf[MLX5_LAG_P2].dev;
1051         } else {
1052                 num_ports = 1;
1053                 mdev[MLX5_LAG_P1] = dev;
1054         }
1055         spin_unlock(&lag_lock);
1056
1057         for (i = 0; i < num_ports; ++i) {
1058                 u32 in[MLX5_ST_SZ_DW(query_cong_statistics_in)] = {};
1059
1060                 MLX5_SET(query_cong_statistics_in, in, opcode,
1061                          MLX5_CMD_OP_QUERY_CONG_STATISTICS);
1062                 ret = mlx5_cmd_exec_inout(mdev[i], query_cong_statistics, in,
1063                                           out);
1064                 if (ret)
1065                         goto free;
1066
1067                 for (j = 0; j < num_counters; ++j)
1068                         values[j] += be64_to_cpup((__be64 *)(out + offsets[j]));
1069         }
1070
1071 free:
1072         kvfree(out);
1073         return ret;
1074 }
1075 EXPORT_SYMBOL(mlx5_lag_query_cong_counters);