ARM: 9148/1: handle CONFIG_CPU_ENDIAN_BE32 in arch/arm/kernel/head.S
[platform/kernel/linux-rpi.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         mlx5_lag_mp_reset(ldev);
309
310         if (ldev->shared_fdb) {
311                 mlx5_eswitch_offloads_destroy_single_fdb(ldev->pf[MLX5_LAG_P1].dev->priv.eswitch,
312                                                          ldev->pf[MLX5_LAG_P2].dev->priv.eswitch);
313                 ldev->shared_fdb = false;
314         }
315
316         MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
317         err = mlx5_cmd_exec_in(dev0, destroy_lag, in);
318         if (err) {
319                 if (roce_lag) {
320                         mlx5_core_err(dev0,
321                                       "Failed to deactivate RoCE LAG; driver restart required\n");
322                 } else {
323                         mlx5_core_err(dev0,
324                                       "Failed to deactivate VF LAG; driver restart required\n"
325                                       "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
326                 }
327         }
328
329         return err;
330 }
331
332 static bool mlx5_lag_check_prereq(struct mlx5_lag *ldev)
333 {
334         if (!ldev->pf[MLX5_LAG_P1].dev || !ldev->pf[MLX5_LAG_P2].dev)
335                 return false;
336
337 #ifdef CONFIG_MLX5_ESWITCH
338         return mlx5_esw_lag_prereq(ldev->pf[MLX5_LAG_P1].dev,
339                                    ldev->pf[MLX5_LAG_P2].dev);
340 #else
341         return (!mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P1].dev) &&
342                 !mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P2].dev));
343 #endif
344 }
345
346 static void mlx5_lag_add_devices(struct mlx5_lag *ldev)
347 {
348         int i;
349
350         for (i = 0; i < MLX5_MAX_PORTS; i++) {
351                 if (!ldev->pf[i].dev)
352                         continue;
353
354                 if (ldev->pf[i].dev->priv.flags &
355                     MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
356                         continue;
357
358                 ldev->pf[i].dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
359                 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
360         }
361 }
362
363 static void mlx5_lag_remove_devices(struct mlx5_lag *ldev)
364 {
365         int i;
366
367         for (i = 0; i < MLX5_MAX_PORTS; i++) {
368                 if (!ldev->pf[i].dev)
369                         continue;
370
371                 if (ldev->pf[i].dev->priv.flags &
372                     MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
373                         continue;
374
375                 ldev->pf[i].dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
376                 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
377         }
378 }
379
380 static void mlx5_disable_lag(struct mlx5_lag *ldev)
381 {
382         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
383         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
384         bool shared_fdb = ldev->shared_fdb;
385         bool roce_lag;
386         int err;
387
388         roce_lag = __mlx5_lag_is_roce(ldev);
389
390         if (shared_fdb) {
391                 mlx5_lag_remove_devices(ldev);
392         } else if (roce_lag) {
393                 if (!(dev0->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)) {
394                         dev0->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
395                         mlx5_rescan_drivers_locked(dev0);
396                 }
397                 mlx5_nic_vport_disable_roce(dev1);
398         }
399
400         err = mlx5_deactivate_lag(ldev);
401         if (err)
402                 return;
403
404         if (shared_fdb || roce_lag)
405                 mlx5_lag_add_devices(ldev);
406
407         if (shared_fdb) {
408                 if (!(dev0->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV))
409                         mlx5_eswitch_reload_reps(dev0->priv.eswitch);
410                 if (!(dev1->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV))
411                         mlx5_eswitch_reload_reps(dev1->priv.eswitch);
412         }
413 }
414
415 static bool mlx5_shared_fdb_supported(struct mlx5_lag *ldev)
416 {
417         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
418         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
419
420         if (is_mdev_switchdev_mode(dev0) &&
421             is_mdev_switchdev_mode(dev1) &&
422             mlx5_eswitch_vport_match_metadata_enabled(dev0->priv.eswitch) &&
423             mlx5_eswitch_vport_match_metadata_enabled(dev1->priv.eswitch) &&
424             mlx5_devcom_is_paired(dev0->priv.devcom,
425                                   MLX5_DEVCOM_ESW_OFFLOADS) &&
426             MLX5_CAP_GEN(dev1, lag_native_fdb_selection) &&
427             MLX5_CAP_ESW(dev1, root_ft_on_other_esw) &&
428             MLX5_CAP_ESW(dev0, esw_shared_ingress_acl))
429                 return true;
430
431         return false;
432 }
433
434 static void mlx5_do_bond(struct mlx5_lag *ldev)
435 {
436         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
437         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
438         struct lag_tracker tracker;
439         bool do_bond, roce_lag;
440         int err;
441
442         if (!mlx5_lag_is_ready(ldev)) {
443                 do_bond = false;
444         } else {
445                 tracker = ldev->tracker;
446
447                 do_bond = tracker.is_bonded && mlx5_lag_check_prereq(ldev);
448         }
449
450         if (do_bond && !__mlx5_lag_is_active(ldev)) {
451                 bool shared_fdb = mlx5_shared_fdb_supported(ldev);
452
453                 roce_lag = !mlx5_sriov_is_enabled(dev0) &&
454                            !mlx5_sriov_is_enabled(dev1);
455
456 #ifdef CONFIG_MLX5_ESWITCH
457                 roce_lag = roce_lag &&
458                            dev0->priv.eswitch->mode == MLX5_ESWITCH_NONE &&
459                            dev1->priv.eswitch->mode == MLX5_ESWITCH_NONE;
460 #endif
461
462                 if (shared_fdb || roce_lag)
463                         mlx5_lag_remove_devices(ldev);
464
465                 err = mlx5_activate_lag(ldev, &tracker,
466                                         roce_lag ? MLX5_LAG_FLAG_ROCE :
467                                                    MLX5_LAG_FLAG_SRIOV,
468                                         shared_fdb);
469                 if (err) {
470                         if (shared_fdb || roce_lag)
471                                 mlx5_lag_add_devices(ldev);
472
473                         return;
474                 } else if (roce_lag) {
475                         dev0->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
476                         mlx5_rescan_drivers_locked(dev0);
477                         mlx5_nic_vport_enable_roce(dev1);
478                 } else if (shared_fdb) {
479                         dev0->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
480                         mlx5_rescan_drivers_locked(dev0);
481
482                         err = mlx5_eswitch_reload_reps(dev0->priv.eswitch);
483                         if (!err)
484                                 err = mlx5_eswitch_reload_reps(dev1->priv.eswitch);
485
486                         if (err) {
487                                 dev0->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
488                                 mlx5_rescan_drivers_locked(dev0);
489                                 mlx5_deactivate_lag(ldev);
490                                 mlx5_lag_add_devices(ldev);
491                                 mlx5_eswitch_reload_reps(dev0->priv.eswitch);
492                                 mlx5_eswitch_reload_reps(dev1->priv.eswitch);
493                                 mlx5_core_err(dev0, "Failed to enable lag\n");
494                                 return;
495                         }
496                 }
497         } else if (do_bond && __mlx5_lag_is_active(ldev)) {
498                 mlx5_modify_lag(ldev, &tracker);
499         } else if (!do_bond && __mlx5_lag_is_active(ldev)) {
500                 mlx5_disable_lag(ldev);
501         }
502 }
503
504 static void mlx5_queue_bond_work(struct mlx5_lag *ldev, unsigned long delay)
505 {
506         queue_delayed_work(ldev->wq, &ldev->bond_work, delay);
507 }
508
509 static void mlx5_lag_lock_eswitches(struct mlx5_core_dev *dev0,
510                                     struct mlx5_core_dev *dev1)
511 {
512         if (dev0)
513                 mlx5_esw_lock(dev0->priv.eswitch);
514         if (dev1)
515                 mlx5_esw_lock(dev1->priv.eswitch);
516 }
517
518 static void mlx5_lag_unlock_eswitches(struct mlx5_core_dev *dev0,
519                                       struct mlx5_core_dev *dev1)
520 {
521         if (dev1)
522                 mlx5_esw_unlock(dev1->priv.eswitch);
523         if (dev0)
524                 mlx5_esw_unlock(dev0->priv.eswitch);
525 }
526
527 static void mlx5_do_bond_work(struct work_struct *work)
528 {
529         struct delayed_work *delayed_work = to_delayed_work(work);
530         struct mlx5_lag *ldev = container_of(delayed_work, struct mlx5_lag,
531                                              bond_work);
532         struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
533         struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
534         int status;
535
536         status = mlx5_dev_list_trylock();
537         if (!status) {
538                 mlx5_queue_bond_work(ldev, HZ);
539                 return;
540         }
541
542         if (ldev->mode_changes_in_progress) {
543                 mlx5_dev_list_unlock();
544                 mlx5_queue_bond_work(ldev, HZ);
545                 return;
546         }
547
548         mlx5_lag_lock_eswitches(dev0, dev1);
549         mlx5_do_bond(ldev);
550         mlx5_lag_unlock_eswitches(dev0, dev1);
551         mlx5_dev_list_unlock();
552 }
553
554 static int mlx5_handle_changeupper_event(struct mlx5_lag *ldev,
555                                          struct lag_tracker *tracker,
556                                          struct net_device *ndev,
557                                          struct netdev_notifier_changeupper_info *info)
558 {
559         struct net_device *upper = info->upper_dev, *ndev_tmp;
560         struct netdev_lag_upper_info *lag_upper_info = NULL;
561         bool is_bonded, is_in_lag, mode_supported;
562         int bond_status = 0;
563         int num_slaves = 0;
564         int idx;
565
566         if (!netif_is_lag_master(upper))
567                 return 0;
568
569         if (info->linking)
570                 lag_upper_info = info->upper_info;
571
572         /* The event may still be of interest if the slave does not belong to
573          * us, but is enslaved to a master which has one or more of our netdevs
574          * as slaves (e.g., if a new slave is added to a master that bonds two
575          * of our netdevs, we should unbond).
576          */
577         rcu_read_lock();
578         for_each_netdev_in_bond_rcu(upper, ndev_tmp) {
579                 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
580                 if (idx >= 0)
581                         bond_status |= (1 << idx);
582
583                 num_slaves++;
584         }
585         rcu_read_unlock();
586
587         /* None of this lagdev's netdevs are slaves of this master. */
588         if (!(bond_status & 0x3))
589                 return 0;
590
591         if (lag_upper_info)
592                 tracker->tx_type = lag_upper_info->tx_type;
593
594         /* Determine bonding status:
595          * A device is considered bonded if both its physical ports are slaves
596          * of the same lag master, and only them.
597          */
598         is_in_lag = num_slaves == MLX5_MAX_PORTS && bond_status == 0x3;
599
600         if (!mlx5_lag_is_ready(ldev) && is_in_lag) {
601                 NL_SET_ERR_MSG_MOD(info->info.extack,
602                                    "Can't activate LAG offload, PF is configured with more than 64 VFs");
603                 return 0;
604         }
605
606         /* Lag mode must be activebackup or hash. */
607         mode_supported = tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP ||
608                          tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH;
609
610         if (is_in_lag && !mode_supported)
611                 NL_SET_ERR_MSG_MOD(info->info.extack,
612                                    "Can't activate LAG offload, TX type isn't supported");
613
614         is_bonded = is_in_lag && mode_supported;
615         if (tracker->is_bonded != is_bonded) {
616                 tracker->is_bonded = is_bonded;
617                 return 1;
618         }
619
620         return 0;
621 }
622
623 static int mlx5_handle_changelowerstate_event(struct mlx5_lag *ldev,
624                                               struct lag_tracker *tracker,
625                                               struct net_device *ndev,
626                                               struct netdev_notifier_changelowerstate_info *info)
627 {
628         struct netdev_lag_lower_state_info *lag_lower_info;
629         int idx;
630
631         if (!netif_is_lag_port(ndev))
632                 return 0;
633
634         idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev);
635         if (idx < 0)
636                 return 0;
637
638         /* This information is used to determine virtual to physical
639          * port mapping.
640          */
641         lag_lower_info = info->lower_state_info;
642         if (!lag_lower_info)
643                 return 0;
644
645         tracker->netdev_state[idx] = *lag_lower_info;
646
647         return 1;
648 }
649
650 static int mlx5_lag_netdev_event(struct notifier_block *this,
651                                  unsigned long event, void *ptr)
652 {
653         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
654         struct lag_tracker tracker;
655         struct mlx5_lag *ldev;
656         int changed = 0;
657
658         if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
659                 return NOTIFY_DONE;
660
661         ldev    = container_of(this, struct mlx5_lag, nb);
662
663         if (!mlx5_lag_is_ready(ldev) && event == NETDEV_CHANGELOWERSTATE)
664                 return NOTIFY_DONE;
665
666         tracker = ldev->tracker;
667
668         switch (event) {
669         case NETDEV_CHANGEUPPER:
670                 changed = mlx5_handle_changeupper_event(ldev, &tracker, ndev,
671                                                         ptr);
672                 break;
673         case NETDEV_CHANGELOWERSTATE:
674                 changed = mlx5_handle_changelowerstate_event(ldev, &tracker,
675                                                              ndev, ptr);
676                 break;
677         }
678
679         ldev->tracker = tracker;
680
681         if (changed)
682                 mlx5_queue_bond_work(ldev, 0);
683
684         return NOTIFY_DONE;
685 }
686
687 static void mlx5_ldev_add_netdev(struct mlx5_lag *ldev,
688                                  struct mlx5_core_dev *dev,
689                                  struct net_device *netdev)
690 {
691         unsigned int fn = PCI_FUNC(dev->pdev->devfn);
692
693         if (fn >= MLX5_MAX_PORTS)
694                 return;
695
696         spin_lock(&lag_lock);
697         ldev->pf[fn].netdev = netdev;
698         ldev->tracker.netdev_state[fn].link_up = 0;
699         ldev->tracker.netdev_state[fn].tx_enabled = 0;
700         spin_unlock(&lag_lock);
701 }
702
703 static void mlx5_ldev_remove_netdev(struct mlx5_lag *ldev,
704                                     struct net_device *netdev)
705 {
706         int i;
707
708         spin_lock(&lag_lock);
709         for (i = 0; i < MLX5_MAX_PORTS; i++) {
710                 if (ldev->pf[i].netdev == netdev) {
711                         ldev->pf[i].netdev = NULL;
712                         break;
713                 }
714         }
715         spin_unlock(&lag_lock);
716 }
717
718 static void mlx5_ldev_add_mdev(struct mlx5_lag *ldev,
719                                struct mlx5_core_dev *dev)
720 {
721         unsigned int fn = PCI_FUNC(dev->pdev->devfn);
722
723         if (fn >= MLX5_MAX_PORTS)
724                 return;
725
726         ldev->pf[fn].dev = dev;
727         dev->priv.lag = ldev;
728 }
729
730 /* Must be called with intf_mutex held */
731 static void mlx5_ldev_remove_mdev(struct mlx5_lag *ldev,
732                                   struct mlx5_core_dev *dev)
733 {
734         int i;
735
736         for (i = 0; i < MLX5_MAX_PORTS; i++)
737                 if (ldev->pf[i].dev == dev)
738                         break;
739
740         if (i == MLX5_MAX_PORTS)
741                 return;
742
743         ldev->pf[i].dev = NULL;
744         dev->priv.lag = NULL;
745 }
746
747 /* Must be called with intf_mutex held */
748 static int __mlx5_lag_dev_add_mdev(struct mlx5_core_dev *dev)
749 {
750         struct mlx5_lag *ldev = NULL;
751         struct mlx5_core_dev *tmp_dev;
752
753         if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
754             !MLX5_CAP_GEN(dev, lag_master) ||
755             MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS)
756                 return 0;
757
758         tmp_dev = mlx5_get_next_phys_dev(dev);
759         if (tmp_dev)
760                 ldev = tmp_dev->priv.lag;
761
762         if (!ldev) {
763                 ldev = mlx5_lag_dev_alloc(dev);
764                 if (!ldev) {
765                         mlx5_core_err(dev, "Failed to alloc lag dev\n");
766                         return 0;
767                 }
768         } else {
769                 if (ldev->mode_changes_in_progress)
770                         return -EAGAIN;
771                 mlx5_ldev_get(ldev);
772         }
773
774         mlx5_ldev_add_mdev(ldev, dev);
775
776         return 0;
777 }
778
779 void mlx5_lag_remove_mdev(struct mlx5_core_dev *dev)
780 {
781         struct mlx5_lag *ldev;
782
783         ldev = mlx5_lag_dev(dev);
784         if (!ldev)
785                 return;
786
787 recheck:
788         mlx5_dev_list_lock();
789         if (ldev->mode_changes_in_progress) {
790                 mlx5_dev_list_unlock();
791                 msleep(100);
792                 goto recheck;
793         }
794         mlx5_ldev_remove_mdev(ldev, dev);
795         mlx5_dev_list_unlock();
796         mlx5_ldev_put(ldev);
797 }
798
799 void mlx5_lag_add_mdev(struct mlx5_core_dev *dev)
800 {
801         int err;
802
803 recheck:
804         mlx5_dev_list_lock();
805         err = __mlx5_lag_dev_add_mdev(dev);
806         if (err) {
807                 mlx5_dev_list_unlock();
808                 msleep(100);
809                 goto recheck;
810         }
811         mlx5_dev_list_unlock();
812 }
813
814 /* Must be called with intf_mutex held */
815 void mlx5_lag_remove_netdev(struct mlx5_core_dev *dev,
816                             struct net_device *netdev)
817 {
818         struct mlx5_lag *ldev;
819
820         ldev = mlx5_lag_dev(dev);
821         if (!ldev)
822                 return;
823
824         mlx5_ldev_remove_netdev(ldev, netdev);
825         ldev->flags &= ~MLX5_LAG_FLAG_READY;
826
827         if (__mlx5_lag_is_active(ldev))
828                 mlx5_queue_bond_work(ldev, 0);
829 }
830
831 /* Must be called with intf_mutex held */
832 void mlx5_lag_add_netdev(struct mlx5_core_dev *dev,
833                          struct net_device *netdev)
834 {
835         struct mlx5_lag *ldev;
836         int i;
837
838         ldev = mlx5_lag_dev(dev);
839         if (!ldev)
840                 return;
841
842         mlx5_ldev_add_netdev(ldev, dev, netdev);
843
844         for (i = 0; i < MLX5_MAX_PORTS; i++)
845                 if (!ldev->pf[i].dev)
846                         break;
847
848         if (i >= MLX5_MAX_PORTS)
849                 ldev->flags |= MLX5_LAG_FLAG_READY;
850         mlx5_queue_bond_work(ldev, 0);
851 }
852
853 bool mlx5_lag_is_roce(struct mlx5_core_dev *dev)
854 {
855         struct mlx5_lag *ldev;
856         bool res;
857
858         spin_lock(&lag_lock);
859         ldev = mlx5_lag_dev(dev);
860         res  = ldev && __mlx5_lag_is_roce(ldev);
861         spin_unlock(&lag_lock);
862
863         return res;
864 }
865 EXPORT_SYMBOL(mlx5_lag_is_roce);
866
867 bool mlx5_lag_is_active(struct mlx5_core_dev *dev)
868 {
869         struct mlx5_lag *ldev;
870         bool res;
871
872         spin_lock(&lag_lock);
873         ldev = mlx5_lag_dev(dev);
874         res  = ldev && __mlx5_lag_is_active(ldev);
875         spin_unlock(&lag_lock);
876
877         return res;
878 }
879 EXPORT_SYMBOL(mlx5_lag_is_active);
880
881 bool mlx5_lag_is_master(struct mlx5_core_dev *dev)
882 {
883         struct mlx5_lag *ldev;
884         bool res;
885
886         spin_lock(&lag_lock);
887         ldev = mlx5_lag_dev(dev);
888         res = ldev && __mlx5_lag_is_active(ldev) &&
889                 dev == ldev->pf[MLX5_LAG_P1].dev;
890         spin_unlock(&lag_lock);
891
892         return res;
893 }
894 EXPORT_SYMBOL(mlx5_lag_is_master);
895
896 bool mlx5_lag_is_sriov(struct mlx5_core_dev *dev)
897 {
898         struct mlx5_lag *ldev;
899         bool res;
900
901         spin_lock(&lag_lock);
902         ldev = mlx5_lag_dev(dev);
903         res  = ldev && __mlx5_lag_is_sriov(ldev);
904         spin_unlock(&lag_lock);
905
906         return res;
907 }
908 EXPORT_SYMBOL(mlx5_lag_is_sriov);
909
910 bool mlx5_lag_is_shared_fdb(struct mlx5_core_dev *dev)
911 {
912         struct mlx5_lag *ldev;
913         bool res;
914
915         spin_lock(&lag_lock);
916         ldev = mlx5_lag_dev(dev);
917         res = ldev && __mlx5_lag_is_sriov(ldev) && ldev->shared_fdb;
918         spin_unlock(&lag_lock);
919
920         return res;
921 }
922 EXPORT_SYMBOL(mlx5_lag_is_shared_fdb);
923
924 void mlx5_lag_disable_change(struct mlx5_core_dev *dev)
925 {
926         struct mlx5_core_dev *dev0;
927         struct mlx5_core_dev *dev1;
928         struct mlx5_lag *ldev;
929
930         mlx5_dev_list_lock();
931
932         ldev = mlx5_lag_dev(dev);
933         dev0 = ldev->pf[MLX5_LAG_P1].dev;
934         dev1 = ldev->pf[MLX5_LAG_P2].dev;
935
936         ldev->mode_changes_in_progress++;
937         if (__mlx5_lag_is_active(ldev)) {
938                 mlx5_lag_lock_eswitches(dev0, dev1);
939                 mlx5_disable_lag(ldev);
940                 mlx5_lag_unlock_eswitches(dev0, dev1);
941         }
942         mlx5_dev_list_unlock();
943 }
944
945 void mlx5_lag_enable_change(struct mlx5_core_dev *dev)
946 {
947         struct mlx5_lag *ldev;
948
949         mlx5_dev_list_lock();
950         ldev = mlx5_lag_dev(dev);
951         ldev->mode_changes_in_progress--;
952         mlx5_dev_list_unlock();
953         mlx5_queue_bond_work(ldev, 0);
954 }
955
956 struct net_device *mlx5_lag_get_roce_netdev(struct mlx5_core_dev *dev)
957 {
958         struct net_device *ndev = NULL;
959         struct mlx5_lag *ldev;
960
961         spin_lock(&lag_lock);
962         ldev = mlx5_lag_dev(dev);
963
964         if (!(ldev && __mlx5_lag_is_roce(ldev)))
965                 goto unlock;
966
967         if (ldev->tracker.tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
968                 ndev = ldev->tracker.netdev_state[MLX5_LAG_P1].tx_enabled ?
969                        ldev->pf[MLX5_LAG_P1].netdev :
970                        ldev->pf[MLX5_LAG_P2].netdev;
971         } else {
972                 ndev = ldev->pf[MLX5_LAG_P1].netdev;
973         }
974         if (ndev)
975                 dev_hold(ndev);
976
977 unlock:
978         spin_unlock(&lag_lock);
979
980         return ndev;
981 }
982 EXPORT_SYMBOL(mlx5_lag_get_roce_netdev);
983
984 u8 mlx5_lag_get_slave_port(struct mlx5_core_dev *dev,
985                            struct net_device *slave)
986 {
987         struct mlx5_lag *ldev;
988         u8 port = 0;
989
990         spin_lock(&lag_lock);
991         ldev = mlx5_lag_dev(dev);
992         if (!(ldev && __mlx5_lag_is_roce(ldev)))
993                 goto unlock;
994
995         if (ldev->pf[MLX5_LAG_P1].netdev == slave)
996                 port = MLX5_LAG_P1;
997         else
998                 port = MLX5_LAG_P2;
999
1000         port = ldev->v2p_map[port];
1001
1002 unlock:
1003         spin_unlock(&lag_lock);
1004         return port;
1005 }
1006 EXPORT_SYMBOL(mlx5_lag_get_slave_port);
1007
1008 struct mlx5_core_dev *mlx5_lag_get_peer_mdev(struct mlx5_core_dev *dev)
1009 {
1010         struct mlx5_core_dev *peer_dev = NULL;
1011         struct mlx5_lag *ldev;
1012
1013         spin_lock(&lag_lock);
1014         ldev = mlx5_lag_dev(dev);
1015         if (!ldev)
1016                 goto unlock;
1017
1018         peer_dev = ldev->pf[MLX5_LAG_P1].dev == dev ?
1019                            ldev->pf[MLX5_LAG_P2].dev :
1020                            ldev->pf[MLX5_LAG_P1].dev;
1021
1022 unlock:
1023         spin_unlock(&lag_lock);
1024         return peer_dev;
1025 }
1026 EXPORT_SYMBOL(mlx5_lag_get_peer_mdev);
1027
1028 int mlx5_lag_query_cong_counters(struct mlx5_core_dev *dev,
1029                                  u64 *values,
1030                                  int num_counters,
1031                                  size_t *offsets)
1032 {
1033         int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
1034         struct mlx5_core_dev *mdev[MLX5_MAX_PORTS];
1035         struct mlx5_lag *ldev;
1036         int num_ports;
1037         int ret, i, j;
1038         void *out;
1039
1040         out = kvzalloc(outlen, GFP_KERNEL);
1041         if (!out)
1042                 return -ENOMEM;
1043
1044         memset(values, 0, sizeof(*values) * num_counters);
1045
1046         spin_lock(&lag_lock);
1047         ldev = mlx5_lag_dev(dev);
1048         if (ldev && __mlx5_lag_is_active(ldev)) {
1049                 num_ports = MLX5_MAX_PORTS;
1050                 mdev[MLX5_LAG_P1] = ldev->pf[MLX5_LAG_P1].dev;
1051                 mdev[MLX5_LAG_P2] = ldev->pf[MLX5_LAG_P2].dev;
1052         } else {
1053                 num_ports = 1;
1054                 mdev[MLX5_LAG_P1] = dev;
1055         }
1056         spin_unlock(&lag_lock);
1057
1058         for (i = 0; i < num_ports; ++i) {
1059                 u32 in[MLX5_ST_SZ_DW(query_cong_statistics_in)] = {};
1060
1061                 MLX5_SET(query_cong_statistics_in, in, opcode,
1062                          MLX5_CMD_OP_QUERY_CONG_STATISTICS);
1063                 ret = mlx5_cmd_exec_inout(mdev[i], query_cong_statistics, in,
1064                                           out);
1065                 if (ret)
1066                         goto free;
1067
1068                 for (j = 0; j < num_counters; ++j)
1069                         values[j] += be64_to_cpup((__be64 *)(out + offsets[j]));
1070         }
1071
1072 free:
1073         kvfree(out);
1074         return ret;
1075 }
1076 EXPORT_SYMBOL(mlx5_lag_query_cong_counters);