]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/mellanox/mlx5/core/lag.c
3cb570ac2b80cd6428b6c95e147ea2775c8f19af
[karo-tx-linux.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
38 enum {
39         MLX5_LAG_FLAG_BONDED = 1 << 0,
40 };
41
42 struct lag_func {
43         struct mlx5_core_dev *dev;
44         struct net_device    *netdev;
45 };
46
47 /* Used for collection of netdev event info. */
48 struct lag_tracker {
49         enum   netdev_lag_tx_type           tx_type;
50         struct netdev_lag_lower_state_info  netdev_state[MLX5_MAX_PORTS];
51         bool is_bonded;
52 };
53
54 /* LAG data of a ConnectX card.
55  * It serves both its phys functions.
56  */
57 struct mlx5_lag {
58         u8                        flags;
59         u8                        v2p_map[MLX5_MAX_PORTS];
60         struct lag_func           pf[MLX5_MAX_PORTS];
61         struct lag_tracker        tracker;
62         struct delayed_work       bond_work;
63         struct notifier_block     nb;
64 };
65
66 /* General purpose, use for short periods of time.
67  * Beware of lock dependencies (preferably, no locks should be acquired
68  * under it).
69  */
70 static DEFINE_MUTEX(lag_mutex);
71
72 static int mlx5_cmd_create_lag(struct mlx5_core_dev *dev, u8 remap_port1,
73                                u8 remap_port2)
74 {
75         u32   in[MLX5_ST_SZ_DW(create_lag_in)]   = {0};
76         u32   out[MLX5_ST_SZ_DW(create_lag_out)] = {0};
77         void *lag_ctx = MLX5_ADDR_OF(create_lag_in, in, ctx);
78
79         MLX5_SET(create_lag_in, in, opcode, MLX5_CMD_OP_CREATE_LAG);
80
81         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
82         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
83
84         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
85 }
86
87 static int mlx5_cmd_modify_lag(struct mlx5_core_dev *dev, u8 remap_port1,
88                                u8 remap_port2)
89 {
90         u32   in[MLX5_ST_SZ_DW(modify_lag_in)]   = {0};
91         u32   out[MLX5_ST_SZ_DW(modify_lag_out)] = {0};
92         void *lag_ctx = MLX5_ADDR_OF(modify_lag_in, in, ctx);
93
94         MLX5_SET(modify_lag_in, in, opcode, MLX5_CMD_OP_MODIFY_LAG);
95         MLX5_SET(modify_lag_in, in, field_select, 0x1);
96
97         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
98         MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
99
100         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
101 }
102
103 static int mlx5_cmd_destroy_lag(struct mlx5_core_dev *dev)
104 {
105         u32  in[MLX5_ST_SZ_DW(destroy_lag_in)]  = {0};
106         u32 out[MLX5_ST_SZ_DW(destroy_lag_out)] = {0};
107
108         MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
109
110         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
111 }
112
113 static struct mlx5_lag *mlx5_lag_dev_get(struct mlx5_core_dev *dev)
114 {
115         return dev->priv.lag;
116 }
117
118 static int mlx5_lag_dev_get_netdev_idx(struct mlx5_lag *ldev,
119                                        struct net_device *ndev)
120 {
121         int i;
122
123         for (i = 0; i < MLX5_MAX_PORTS; i++)
124                 if (ldev->pf[i].netdev == ndev)
125                         return i;
126
127         return -1;
128 }
129
130 static bool mlx5_lag_is_bonded(struct mlx5_lag *ldev)
131 {
132         return !!(ldev->flags & MLX5_LAG_FLAG_BONDED);
133 }
134
135 static void mlx5_infer_tx_affinity_mapping(struct lag_tracker *tracker,
136                                            u8 *port1, u8 *port2)
137 {
138         if (tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
139                 if (tracker->netdev_state[0].tx_enabled) {
140                         *port1 = 1;
141                         *port2 = 1;
142                 } else {
143                         *port1 = 2;
144                         *port2 = 2;
145                 }
146         } else {
147                 *port1 = 1;
148                 *port2 = 2;
149                 if (!tracker->netdev_state[0].link_up)
150                         *port1 = 2;
151                 else if (!tracker->netdev_state[1].link_up)
152                         *port2 = 1;
153         }
154 }
155
156 static void mlx5_activate_lag(struct mlx5_lag *ldev,
157                               struct lag_tracker *tracker)
158 {
159         struct mlx5_core_dev *dev0 = ldev->pf[0].dev;
160         int err;
161
162         ldev->flags |= MLX5_LAG_FLAG_BONDED;
163
164         mlx5_infer_tx_affinity_mapping(tracker, &ldev->v2p_map[0],
165                                        &ldev->v2p_map[1]);
166
167         err = mlx5_cmd_create_lag(dev0, ldev->v2p_map[0], ldev->v2p_map[1]);
168         if (err)
169                 mlx5_core_err(dev0,
170                               "Failed to create LAG (%d)\n",
171                               err);
172 }
173
174 static void mlx5_deactivate_lag(struct mlx5_lag *ldev)
175 {
176         struct mlx5_core_dev *dev0 = ldev->pf[0].dev;
177         int err;
178
179         ldev->flags &= ~MLX5_LAG_FLAG_BONDED;
180
181         err = mlx5_cmd_destroy_lag(dev0);
182         if (err)
183                 mlx5_core_err(dev0,
184                               "Failed to destroy LAG (%d)\n",
185                               err);
186 }
187
188 static void mlx5_do_bond(struct mlx5_lag *ldev)
189 {
190         struct mlx5_core_dev *dev0 = ldev->pf[0].dev;
191         struct mlx5_core_dev *dev1 = ldev->pf[1].dev;
192         struct lag_tracker tracker;
193         u8 v2p_port1, v2p_port2;
194         int i, err;
195
196         if (!dev0 || !dev1)
197                 return;
198
199         mutex_lock(&lag_mutex);
200         tracker = ldev->tracker;
201         mutex_unlock(&lag_mutex);
202
203         if (tracker.is_bonded && !mlx5_lag_is_bonded(ldev)) {
204                 if (mlx5_sriov_is_enabled(dev0) ||
205                     mlx5_sriov_is_enabled(dev1)) {
206                         mlx5_core_warn(dev0, "LAG is not supported with SRIOV");
207                         return;
208                 }
209
210                 for (i = 0; i < MLX5_MAX_PORTS; i++)
211                         mlx5_remove_dev_by_protocol(ldev->pf[i].dev,
212                                                     MLX5_INTERFACE_PROTOCOL_IB);
213
214                 mlx5_activate_lag(ldev, &tracker);
215
216                 mlx5_add_dev_by_protocol(dev0, MLX5_INTERFACE_PROTOCOL_IB);
217                 mlx5_nic_vport_enable_roce(dev1);
218         } else if (tracker.is_bonded && mlx5_lag_is_bonded(ldev)) {
219                 mlx5_infer_tx_affinity_mapping(&tracker, &v2p_port1,
220                                                &v2p_port2);
221
222                 if ((v2p_port1 != ldev->v2p_map[0]) ||
223                     (v2p_port2 != ldev->v2p_map[1])) {
224                         ldev->v2p_map[0] = v2p_port1;
225                         ldev->v2p_map[1] = v2p_port2;
226
227                         err = mlx5_cmd_modify_lag(dev0, v2p_port1, v2p_port2);
228                         if (err)
229                                 mlx5_core_err(dev0,
230                                               "Failed to modify LAG (%d)\n",
231                                               err);
232                 }
233         } else if (!tracker.is_bonded && mlx5_lag_is_bonded(ldev)) {
234                 mlx5_remove_dev_by_protocol(dev0, MLX5_INTERFACE_PROTOCOL_IB);
235                 mlx5_nic_vport_disable_roce(dev1);
236
237                 mlx5_deactivate_lag(ldev);
238
239                 for (i = 0; i < MLX5_MAX_PORTS; i++)
240                         if (ldev->pf[i].dev)
241                                 mlx5_add_dev_by_protocol(ldev->pf[i].dev,
242                                                          MLX5_INTERFACE_PROTOCOL_IB);
243         }
244 }
245
246 static void mlx5_queue_bond_work(struct mlx5_lag *ldev, unsigned long delay)
247 {
248         schedule_delayed_work(&ldev->bond_work, delay);
249 }
250
251 static void mlx5_do_bond_work(struct work_struct *work)
252 {
253         struct delayed_work *delayed_work = to_delayed_work(work);
254         struct mlx5_lag *ldev = container_of(delayed_work, struct mlx5_lag,
255                                              bond_work);
256         int status;
257
258         status = mutex_trylock(&mlx5_intf_mutex);
259         if (!status) {
260                 /* 1 sec delay. */
261                 mlx5_queue_bond_work(ldev, HZ);
262                 return;
263         }
264
265         mlx5_do_bond(ldev);
266         mutex_unlock(&mlx5_intf_mutex);
267 }
268
269 static int mlx5_handle_changeupper_event(struct mlx5_lag *ldev,
270                                          struct lag_tracker *tracker,
271                                          struct net_device *ndev,
272                                          struct netdev_notifier_changeupper_info *info)
273 {
274         struct net_device *upper = info->upper_dev, *ndev_tmp;
275         struct netdev_lag_upper_info *lag_upper_info;
276         bool is_bonded;
277         int bond_status = 0;
278         int num_slaves = 0;
279         int idx;
280
281         if (!netif_is_lag_master(upper))
282                 return 0;
283
284         lag_upper_info = info->upper_info;
285
286         /* The event may still be of interest if the slave does not belong to
287          * us, but is enslaved to a master which has one or more of our netdevs
288          * as slaves (e.g., if a new slave is added to a master that bonds two
289          * of our netdevs, we should unbond).
290          */
291         rcu_read_lock();
292         for_each_netdev_in_bond_rcu(upper, ndev_tmp) {
293                 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
294                 if (idx > -1)
295                         bond_status |= (1 << idx);
296
297                 num_slaves++;
298         }
299         rcu_read_unlock();
300
301         /* None of this lagdev's netdevs are slaves of this master. */
302         if (!(bond_status & 0x3))
303                 return 0;
304
305         if (lag_upper_info)
306                 tracker->tx_type = lag_upper_info->tx_type;
307
308         /* Determine bonding status:
309          * A device is considered bonded if both its physical ports are slaves
310          * of the same lag master, and only them.
311          * Lag mode must be activebackup or hash.
312          */
313         is_bonded = (num_slaves == MLX5_MAX_PORTS) &&
314                     (bond_status == 0x3) &&
315                     ((tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) ||
316                      (tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH));
317
318         if (tracker->is_bonded != is_bonded) {
319                 tracker->is_bonded = is_bonded;
320                 return 1;
321         }
322
323         return 0;
324 }
325
326 static int mlx5_handle_changelowerstate_event(struct mlx5_lag *ldev,
327                                               struct lag_tracker *tracker,
328                                               struct net_device *ndev,
329                                               struct netdev_notifier_changelowerstate_info *info)
330 {
331         struct netdev_lag_lower_state_info *lag_lower_info;
332         int idx;
333
334         if (!netif_is_lag_port(ndev))
335                 return 0;
336
337         idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev);
338         if (idx == -1)
339                 return 0;
340
341         /* This information is used to determine virtual to physical
342          * port mapping.
343          */
344         lag_lower_info = info->lower_state_info;
345         if (!lag_lower_info)
346                 return 0;
347
348         tracker->netdev_state[idx] = *lag_lower_info;
349
350         return 1;
351 }
352
353 static int mlx5_lag_netdev_event(struct notifier_block *this,
354                                  unsigned long event, void *ptr)
355 {
356         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
357         struct lag_tracker tracker;
358         struct mlx5_lag *ldev;
359         int changed = 0;
360
361         if (!net_eq(dev_net(ndev), &init_net))
362                 return NOTIFY_DONE;
363
364         if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
365                 return NOTIFY_DONE;
366
367         ldev    = container_of(this, struct mlx5_lag, nb);
368         tracker = ldev->tracker;
369
370         switch (event) {
371         case NETDEV_CHANGEUPPER:
372                 changed = mlx5_handle_changeupper_event(ldev, &tracker, ndev,
373                                                         ptr);
374                 break;
375         case NETDEV_CHANGELOWERSTATE:
376                 changed = mlx5_handle_changelowerstate_event(ldev, &tracker,
377                                                              ndev, ptr);
378                 break;
379         }
380
381         mutex_lock(&lag_mutex);
382         ldev->tracker = tracker;
383         mutex_unlock(&lag_mutex);
384
385         if (changed)
386                 mlx5_queue_bond_work(ldev, 0);
387
388         return NOTIFY_DONE;
389 }
390
391 static struct mlx5_lag *mlx5_lag_dev_alloc(void)
392 {
393         struct mlx5_lag *ldev;
394
395         ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
396         if (!ldev)
397                 return NULL;
398
399         INIT_DELAYED_WORK(&ldev->bond_work, mlx5_do_bond_work);
400
401         return ldev;
402 }
403
404 static void mlx5_lag_dev_free(struct mlx5_lag *ldev)
405 {
406         kfree(ldev);
407 }
408
409 static void mlx5_lag_dev_add_pf(struct mlx5_lag *ldev,
410                                 struct mlx5_core_dev *dev,
411                                 struct net_device *netdev)
412 {
413         unsigned int fn = PCI_FUNC(dev->pdev->devfn);
414
415         if (fn >= MLX5_MAX_PORTS)
416                 return;
417
418         mutex_lock(&lag_mutex);
419         ldev->pf[fn].dev    = dev;
420         ldev->pf[fn].netdev = netdev;
421         ldev->tracker.netdev_state[fn].link_up = 0;
422         ldev->tracker.netdev_state[fn].tx_enabled = 0;
423
424         dev->priv.lag = ldev;
425         mutex_unlock(&lag_mutex);
426 }
427
428 static void mlx5_lag_dev_remove_pf(struct mlx5_lag *ldev,
429                                    struct mlx5_core_dev *dev)
430 {
431         int i;
432
433         for (i = 0; i < MLX5_MAX_PORTS; i++)
434                 if (ldev->pf[i].dev == dev)
435                         break;
436
437         if (i == MLX5_MAX_PORTS)
438                 return;
439
440         mutex_lock(&lag_mutex);
441         memset(&ldev->pf[i], 0, sizeof(*ldev->pf));
442
443         dev->priv.lag = NULL;
444         mutex_unlock(&lag_mutex);
445 }
446
447 static u16 mlx5_gen_pci_id(struct mlx5_core_dev *dev)
448 {
449         return (u16)((dev->pdev->bus->number << 8) |
450                      PCI_SLOT(dev->pdev->devfn));
451 }
452
453 /* Must be called with intf_mutex held */
454 void mlx5_lag_add(struct mlx5_core_dev *dev, struct net_device *netdev)
455 {
456         struct mlx5_lag *ldev = NULL;
457         struct mlx5_core_dev *tmp_dev;
458         struct mlx5_priv *priv;
459         u16 pci_id;
460
461         if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
462             !MLX5_CAP_GEN(dev, lag_master) ||
463             (MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS))
464                 return;
465
466         pci_id = mlx5_gen_pci_id(dev);
467
468         mlx5_core_for_each_priv(priv) {
469                 tmp_dev = container_of(priv, struct mlx5_core_dev, priv);
470                 if ((dev != tmp_dev) &&
471                     (mlx5_gen_pci_id(tmp_dev) == pci_id)) {
472                         ldev = tmp_dev->priv.lag;
473                         break;
474                 }
475         }
476
477         if (!ldev) {
478                 ldev = mlx5_lag_dev_alloc();
479                 if (!ldev) {
480                         mlx5_core_err(dev, "Failed to alloc lag dev\n");
481                         return;
482                 }
483         }
484
485         mlx5_lag_dev_add_pf(ldev, dev, netdev);
486
487         if (!ldev->nb.notifier_call) {
488                 ldev->nb.notifier_call = mlx5_lag_netdev_event;
489                 if (register_netdevice_notifier(&ldev->nb)) {
490                         ldev->nb.notifier_call = NULL;
491                         mlx5_core_err(dev, "Failed to register LAG netdev notifier\n");
492                 }
493         }
494 }
495
496 /* Must be called with intf_mutex held */
497 void mlx5_lag_remove(struct mlx5_core_dev *dev)
498 {
499         struct mlx5_lag *ldev;
500         int i;
501
502         ldev = mlx5_lag_dev_get(dev);
503         if (!ldev)
504                 return;
505
506         if (mlx5_lag_is_bonded(ldev))
507                 mlx5_deactivate_lag(ldev);
508
509         mlx5_lag_dev_remove_pf(ldev, dev);
510
511         for (i = 0; i < MLX5_MAX_PORTS; i++)
512                 if (ldev->pf[i].dev)
513                         break;
514
515         if (i == MLX5_MAX_PORTS) {
516                 if (ldev->nb.notifier_call)
517                         unregister_netdevice_notifier(&ldev->nb);
518                 cancel_delayed_work_sync(&ldev->bond_work);
519                 mlx5_lag_dev_free(ldev);
520         }
521 }
522
523 bool mlx5_lag_is_active(struct mlx5_core_dev *dev)
524 {
525         struct mlx5_lag *ldev;
526         bool res;
527
528         mutex_lock(&lag_mutex);
529         ldev = mlx5_lag_dev_get(dev);
530         res  = ldev && mlx5_lag_is_bonded(ldev);
531         mutex_unlock(&lag_mutex);
532
533         return res;
534 }
535 EXPORT_SYMBOL(mlx5_lag_is_active);
536
537 struct net_device *mlx5_lag_get_roce_netdev(struct mlx5_core_dev *dev)
538 {
539         struct net_device *ndev = NULL;
540         struct mlx5_lag *ldev;
541
542         mutex_lock(&lag_mutex);
543         ldev = mlx5_lag_dev_get(dev);
544
545         if (!(ldev && mlx5_lag_is_bonded(ldev)))
546                 goto unlock;
547
548         if (ldev->tracker.tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
549                 ndev = ldev->tracker.netdev_state[0].tx_enabled ?
550                        ldev->pf[0].netdev : ldev->pf[1].netdev;
551         } else {
552                 ndev = ldev->pf[0].netdev;
553         }
554         if (ndev)
555                 dev_hold(ndev);
556
557 unlock:
558         mutex_unlock(&lag_mutex);
559
560         return ndev;
561 }
562 EXPORT_SYMBOL(mlx5_lag_get_roce_netdev);
563