2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/netdevice.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/vport.h>
36 #include "mlx5_core.h"
41 /* General purpose, use for short periods of time.
42 * Beware of lock dependencies (preferably, no locks should be acquired
45 static DEFINE_SPINLOCK(lag_lock);
47 static int mlx5_cmd_create_lag(struct mlx5_core_dev *dev, u8 remap_port1,
50 u32 in[MLX5_ST_SZ_DW(create_lag_in)] = {};
51 void *lag_ctx = MLX5_ADDR_OF(create_lag_in, in, ctx);
53 MLX5_SET(create_lag_in, in, opcode, MLX5_CMD_OP_CREATE_LAG);
55 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
56 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
58 return mlx5_cmd_exec_in(dev, create_lag, in);
61 static int mlx5_cmd_modify_lag(struct mlx5_core_dev *dev, u8 remap_port1,
64 u32 in[MLX5_ST_SZ_DW(modify_lag_in)] = {};
65 void *lag_ctx = MLX5_ADDR_OF(modify_lag_in, in, ctx);
67 MLX5_SET(modify_lag_in, in, opcode, MLX5_CMD_OP_MODIFY_LAG);
68 MLX5_SET(modify_lag_in, in, field_select, 0x1);
70 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_1, remap_port1);
71 MLX5_SET(lagc, lag_ctx, tx_remap_affinity_2, remap_port2);
73 return mlx5_cmd_exec_in(dev, modify_lag, in);
76 int mlx5_cmd_create_vport_lag(struct mlx5_core_dev *dev)
78 u32 in[MLX5_ST_SZ_DW(create_vport_lag_in)] = {};
80 MLX5_SET(create_vport_lag_in, in, opcode, MLX5_CMD_OP_CREATE_VPORT_LAG);
82 return mlx5_cmd_exec_in(dev, create_vport_lag, in);
84 EXPORT_SYMBOL(mlx5_cmd_create_vport_lag);
86 int mlx5_cmd_destroy_vport_lag(struct mlx5_core_dev *dev)
88 u32 in[MLX5_ST_SZ_DW(destroy_vport_lag_in)] = {};
90 MLX5_SET(destroy_vport_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_VPORT_LAG);
92 return mlx5_cmd_exec_in(dev, destroy_vport_lag, in);
94 EXPORT_SYMBOL(mlx5_cmd_destroy_vport_lag);
96 int mlx5_lag_dev_get_netdev_idx(struct mlx5_lag *ldev,
97 struct net_device *ndev)
101 for (i = 0; i < MLX5_MAX_PORTS; i++)
102 if (ldev->pf[i].netdev == ndev)
108 static bool __mlx5_lag_is_roce(struct mlx5_lag *ldev)
110 return !!(ldev->flags & MLX5_LAG_FLAG_ROCE);
113 static bool __mlx5_lag_is_sriov(struct mlx5_lag *ldev)
115 return !!(ldev->flags & MLX5_LAG_FLAG_SRIOV);
118 static void mlx5_infer_tx_affinity_mapping(struct lag_tracker *tracker,
119 u8 *port1, u8 *port2)
123 if (!tracker->netdev_state[MLX5_LAG_P1].tx_enabled ||
124 !tracker->netdev_state[MLX5_LAG_P1].link_up) {
129 if (!tracker->netdev_state[MLX5_LAG_P2].tx_enabled ||
130 !tracker->netdev_state[MLX5_LAG_P2].link_up)
134 void mlx5_modify_lag(struct mlx5_lag *ldev,
135 struct lag_tracker *tracker)
137 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
138 u8 v2p_port1, v2p_port2;
141 mlx5_infer_tx_affinity_mapping(tracker, &v2p_port1,
144 if (v2p_port1 != ldev->v2p_map[MLX5_LAG_P1] ||
145 v2p_port2 != ldev->v2p_map[MLX5_LAG_P2]) {
146 ldev->v2p_map[MLX5_LAG_P1] = v2p_port1;
147 ldev->v2p_map[MLX5_LAG_P2] = v2p_port2;
149 mlx5_core_info(dev0, "modify lag map port 1:%d port 2:%d",
150 ldev->v2p_map[MLX5_LAG_P1],
151 ldev->v2p_map[MLX5_LAG_P2]);
153 err = mlx5_cmd_modify_lag(dev0, v2p_port1, v2p_port2);
156 "Failed to modify LAG (%d)\n",
161 static int mlx5_create_lag(struct mlx5_lag *ldev,
162 struct lag_tracker *tracker)
164 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
167 mlx5_infer_tx_affinity_mapping(tracker, &ldev->v2p_map[MLX5_LAG_P1],
168 &ldev->v2p_map[MLX5_LAG_P2]);
170 mlx5_core_info(dev0, "lag map port 1:%d port 2:%d",
171 ldev->v2p_map[MLX5_LAG_P1], ldev->v2p_map[MLX5_LAG_P2]);
173 err = mlx5_cmd_create_lag(dev0, ldev->v2p_map[MLX5_LAG_P1],
174 ldev->v2p_map[MLX5_LAG_P2]);
177 "Failed to create LAG (%d)\n",
182 int mlx5_activate_lag(struct mlx5_lag *ldev,
183 struct lag_tracker *tracker,
186 bool roce_lag = !!(flags & MLX5_LAG_FLAG_ROCE);
187 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
190 err = mlx5_create_lag(ldev, tracker);
194 "Failed to activate RoCE LAG\n");
197 "Failed to activate VF LAG\n"
198 "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
203 ldev->flags |= flags;
207 static int mlx5_deactivate_lag(struct mlx5_lag *ldev)
209 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
210 u32 in[MLX5_ST_SZ_DW(destroy_lag_in)] = {};
211 bool roce_lag = __mlx5_lag_is_roce(ldev);
214 ldev->flags &= ~MLX5_LAG_MODE_FLAGS;
216 MLX5_SET(destroy_lag_in, in, opcode, MLX5_CMD_OP_DESTROY_LAG);
217 err = mlx5_cmd_exec_in(dev0, destroy_lag, in);
221 "Failed to deactivate RoCE LAG; driver restart required\n");
224 "Failed to deactivate VF LAG; driver restart required\n"
225 "Make sure all VFs are unbound prior to VF LAG activation or deactivation\n");
232 static bool mlx5_lag_check_prereq(struct mlx5_lag *ldev)
234 if (!ldev->pf[MLX5_LAG_P1].dev || !ldev->pf[MLX5_LAG_P2].dev)
237 #ifdef CONFIG_MLX5_ESWITCH
238 return mlx5_esw_lag_prereq(ldev->pf[MLX5_LAG_P1].dev,
239 ldev->pf[MLX5_LAG_P2].dev);
241 return (!mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P1].dev) &&
242 !mlx5_sriov_is_enabled(ldev->pf[MLX5_LAG_P2].dev));
246 static void mlx5_lag_add_devices(struct mlx5_lag *ldev)
250 for (i = 0; i < MLX5_MAX_PORTS; i++) {
251 if (!ldev->pf[i].dev)
254 ldev->pf[i].dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
255 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
259 static void mlx5_lag_remove_devices(struct mlx5_lag *ldev)
263 for (i = 0; i < MLX5_MAX_PORTS; i++) {
264 if (!ldev->pf[i].dev)
267 ldev->pf[i].dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
268 mlx5_rescan_drivers_locked(ldev->pf[i].dev);
272 static void mlx5_do_bond(struct mlx5_lag *ldev)
274 struct mlx5_core_dev *dev0 = ldev->pf[MLX5_LAG_P1].dev;
275 struct mlx5_core_dev *dev1 = ldev->pf[MLX5_LAG_P2].dev;
276 struct lag_tracker tracker;
277 bool do_bond, roce_lag;
280 if (!mlx5_lag_is_ready(ldev))
283 tracker = ldev->tracker;
285 do_bond = tracker.is_bonded && mlx5_lag_check_prereq(ldev);
287 if (do_bond && !__mlx5_lag_is_active(ldev)) {
288 roce_lag = !mlx5_sriov_is_enabled(dev0) &&
289 !mlx5_sriov_is_enabled(dev1);
291 #ifdef CONFIG_MLX5_ESWITCH
292 roce_lag = roce_lag &&
293 dev0->priv.eswitch->mode == MLX5_ESWITCH_NONE &&
294 dev1->priv.eswitch->mode == MLX5_ESWITCH_NONE;
298 mlx5_lag_remove_devices(ldev);
300 err = mlx5_activate_lag(ldev, &tracker,
301 roce_lag ? MLX5_LAG_FLAG_ROCE :
302 MLX5_LAG_FLAG_SRIOV);
305 mlx5_lag_add_devices(ldev);
311 dev0->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
312 mlx5_rescan_drivers_locked(dev0);
313 mlx5_nic_vport_enable_roce(dev1);
315 } else if (do_bond && __mlx5_lag_is_active(ldev)) {
316 mlx5_modify_lag(ldev, &tracker);
317 } else if (!do_bond && __mlx5_lag_is_active(ldev)) {
318 roce_lag = __mlx5_lag_is_roce(ldev);
321 dev0->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_IB_ADEV;
322 mlx5_rescan_drivers_locked(dev0);
323 mlx5_nic_vport_disable_roce(dev1);
326 err = mlx5_deactivate_lag(ldev);
331 mlx5_lag_add_devices(ldev);
335 static void mlx5_queue_bond_work(struct mlx5_lag *ldev, unsigned long delay)
337 queue_delayed_work(ldev->wq, &ldev->bond_work, delay);
340 static void mlx5_do_bond_work(struct work_struct *work)
342 struct delayed_work *delayed_work = to_delayed_work(work);
343 struct mlx5_lag *ldev = container_of(delayed_work, struct mlx5_lag,
347 status = mlx5_dev_list_trylock();
350 mlx5_queue_bond_work(ldev, HZ);
355 mlx5_dev_list_unlock();
358 static int mlx5_handle_changeupper_event(struct mlx5_lag *ldev,
359 struct lag_tracker *tracker,
360 struct net_device *ndev,
361 struct netdev_notifier_changeupper_info *info)
363 struct net_device *upper = info->upper_dev, *ndev_tmp;
364 struct netdev_lag_upper_info *lag_upper_info = NULL;
365 bool is_bonded, is_in_lag, mode_supported;
370 if (!netif_is_lag_master(upper))
374 lag_upper_info = info->upper_info;
376 /* The event may still be of interest if the slave does not belong to
377 * us, but is enslaved to a master which has one or more of our netdevs
378 * as slaves (e.g., if a new slave is added to a master that bonds two
379 * of our netdevs, we should unbond).
382 for_each_netdev_in_bond_rcu(upper, ndev_tmp) {
383 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev_tmp);
385 bond_status |= (1 << idx);
391 /* None of this lagdev's netdevs are slaves of this master. */
392 if (!(bond_status & 0x3))
396 tracker->tx_type = lag_upper_info->tx_type;
398 /* Determine bonding status:
399 * A device is considered bonded if both its physical ports are slaves
400 * of the same lag master, and only them.
402 is_in_lag = num_slaves == MLX5_MAX_PORTS && bond_status == 0x3;
404 if (!mlx5_lag_is_ready(ldev) && is_in_lag) {
405 NL_SET_ERR_MSG_MOD(info->info.extack,
406 "Can't activate LAG offload, PF is configured with more than 64 VFs");
410 /* Lag mode must be activebackup or hash. */
411 mode_supported = tracker->tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP ||
412 tracker->tx_type == NETDEV_LAG_TX_TYPE_HASH;
414 if (is_in_lag && !mode_supported)
415 NL_SET_ERR_MSG_MOD(info->info.extack,
416 "Can't activate LAG offload, TX type isn't supported");
418 is_bonded = is_in_lag && mode_supported;
419 if (tracker->is_bonded != is_bonded) {
420 tracker->is_bonded = is_bonded;
427 static int mlx5_handle_changelowerstate_event(struct mlx5_lag *ldev,
428 struct lag_tracker *tracker,
429 struct net_device *ndev,
430 struct netdev_notifier_changelowerstate_info *info)
432 struct netdev_lag_lower_state_info *lag_lower_info;
435 if (!netif_is_lag_port(ndev))
438 idx = mlx5_lag_dev_get_netdev_idx(ldev, ndev);
442 /* This information is used to determine virtual to physical
445 lag_lower_info = info->lower_state_info;
449 tracker->netdev_state[idx] = *lag_lower_info;
454 static int mlx5_lag_netdev_event(struct notifier_block *this,
455 unsigned long event, void *ptr)
457 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
458 struct lag_tracker tracker;
459 struct mlx5_lag *ldev;
462 if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
465 ldev = container_of(this, struct mlx5_lag, nb);
467 if (!mlx5_lag_is_ready(ldev) && event == NETDEV_CHANGELOWERSTATE)
470 tracker = ldev->tracker;
473 case NETDEV_CHANGEUPPER:
474 changed = mlx5_handle_changeupper_event(ldev, &tracker, ndev,
477 case NETDEV_CHANGELOWERSTATE:
478 changed = mlx5_handle_changelowerstate_event(ldev, &tracker,
483 ldev->tracker = tracker;
486 mlx5_queue_bond_work(ldev, 0);
491 static struct mlx5_lag *mlx5_lag_dev_alloc(void)
493 struct mlx5_lag *ldev;
495 ldev = kzalloc(sizeof(*ldev), GFP_KERNEL);
499 ldev->wq = create_singlethread_workqueue("mlx5_lag");
505 INIT_DELAYED_WORK(&ldev->bond_work, mlx5_do_bond_work);
510 static void mlx5_lag_dev_free(struct mlx5_lag *ldev)
512 destroy_workqueue(ldev->wq);
516 static int mlx5_lag_dev_add_pf(struct mlx5_lag *ldev,
517 struct mlx5_core_dev *dev,
518 struct net_device *netdev)
520 unsigned int fn = PCI_FUNC(dev->pdev->devfn);
522 if (fn >= MLX5_MAX_PORTS)
525 spin_lock(&lag_lock);
526 ldev->pf[fn].dev = dev;
527 ldev->pf[fn].netdev = netdev;
528 ldev->tracker.netdev_state[fn].link_up = 0;
529 ldev->tracker.netdev_state[fn].tx_enabled = 0;
531 dev->priv.lag = ldev;
533 spin_unlock(&lag_lock);
538 static void mlx5_lag_dev_remove_pf(struct mlx5_lag *ldev,
539 struct mlx5_core_dev *dev)
543 for (i = 0; i < MLX5_MAX_PORTS; i++)
544 if (ldev->pf[i].dev == dev)
547 if (i == MLX5_MAX_PORTS)
550 spin_lock(&lag_lock);
551 memset(&ldev->pf[i], 0, sizeof(*ldev->pf));
553 dev->priv.lag = NULL;
554 spin_unlock(&lag_lock);
557 /* Must be called with intf_mutex held */
558 void mlx5_lag_add(struct mlx5_core_dev *dev, struct net_device *netdev)
560 struct mlx5_lag *ldev = NULL;
561 struct mlx5_core_dev *tmp_dev;
564 if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
565 !MLX5_CAP_GEN(dev, lag_master) ||
566 MLX5_CAP_GEN(dev, num_lag_ports) != MLX5_MAX_PORTS)
569 tmp_dev = mlx5_get_next_phys_dev(dev);
571 ldev = tmp_dev->priv.lag;
574 ldev = mlx5_lag_dev_alloc();
576 mlx5_core_err(dev, "Failed to alloc lag dev\n");
581 if (mlx5_lag_dev_add_pf(ldev, dev, netdev) < 0)
584 for (i = 0; i < MLX5_MAX_PORTS; i++)
585 if (!ldev->pf[i].dev)
588 if (i >= MLX5_MAX_PORTS)
589 ldev->flags |= MLX5_LAG_FLAG_READY;
591 if (!ldev->nb.notifier_call) {
592 ldev->nb.notifier_call = mlx5_lag_netdev_event;
593 if (register_netdevice_notifier_net(&init_net, &ldev->nb)) {
594 ldev->nb.notifier_call = NULL;
595 mlx5_core_err(dev, "Failed to register LAG netdev notifier\n");
599 err = mlx5_lag_mp_init(ldev);
601 mlx5_core_err(dev, "Failed to init multipath lag err=%d\n",
605 /* Must be called with intf_mutex held */
606 void mlx5_lag_remove(struct mlx5_core_dev *dev)
608 struct mlx5_lag *ldev;
611 ldev = mlx5_lag_dev_get(dev);
615 if (__mlx5_lag_is_active(ldev))
616 mlx5_deactivate_lag(ldev);
618 mlx5_lag_dev_remove_pf(ldev, dev);
620 ldev->flags &= ~MLX5_LAG_FLAG_READY;
622 for (i = 0; i < MLX5_MAX_PORTS; i++)
626 if (i == MLX5_MAX_PORTS) {
627 if (ldev->nb.notifier_call) {
628 unregister_netdevice_notifier_net(&init_net, &ldev->nb);
629 ldev->nb.notifier_call = NULL;
631 mlx5_lag_mp_cleanup(ldev);
632 cancel_delayed_work_sync(&ldev->bond_work);
633 mlx5_lag_dev_free(ldev);
637 bool mlx5_lag_is_roce(struct mlx5_core_dev *dev)
639 struct mlx5_lag *ldev;
642 spin_lock(&lag_lock);
643 ldev = mlx5_lag_dev_get(dev);
644 res = ldev && __mlx5_lag_is_roce(ldev);
645 spin_unlock(&lag_lock);
649 EXPORT_SYMBOL(mlx5_lag_is_roce);
651 bool mlx5_lag_is_active(struct mlx5_core_dev *dev)
653 struct mlx5_lag *ldev;
656 spin_lock(&lag_lock);
657 ldev = mlx5_lag_dev_get(dev);
658 res = ldev && __mlx5_lag_is_active(ldev);
659 spin_unlock(&lag_lock);
663 EXPORT_SYMBOL(mlx5_lag_is_active);
665 bool mlx5_lag_is_sriov(struct mlx5_core_dev *dev)
667 struct mlx5_lag *ldev;
670 spin_lock(&lag_lock);
671 ldev = mlx5_lag_dev_get(dev);
672 res = ldev && __mlx5_lag_is_sriov(ldev);
673 spin_unlock(&lag_lock);
677 EXPORT_SYMBOL(mlx5_lag_is_sriov);
679 void mlx5_lag_update(struct mlx5_core_dev *dev)
681 struct mlx5_lag *ldev;
683 mlx5_dev_list_lock();
684 ldev = mlx5_lag_dev_get(dev);
691 mlx5_dev_list_unlock();
694 struct net_device *mlx5_lag_get_roce_netdev(struct mlx5_core_dev *dev)
696 struct net_device *ndev = NULL;
697 struct mlx5_lag *ldev;
699 spin_lock(&lag_lock);
700 ldev = mlx5_lag_dev_get(dev);
702 if (!(ldev && __mlx5_lag_is_roce(ldev)))
705 if (ldev->tracker.tx_type == NETDEV_LAG_TX_TYPE_ACTIVEBACKUP) {
706 ndev = ldev->tracker.netdev_state[MLX5_LAG_P1].tx_enabled ?
707 ldev->pf[MLX5_LAG_P1].netdev :
708 ldev->pf[MLX5_LAG_P2].netdev;
710 ndev = ldev->pf[MLX5_LAG_P1].netdev;
716 spin_unlock(&lag_lock);
720 EXPORT_SYMBOL(mlx5_lag_get_roce_netdev);
722 u8 mlx5_lag_get_slave_port(struct mlx5_core_dev *dev,
723 struct net_device *slave)
725 struct mlx5_lag *ldev;
728 spin_lock(&lag_lock);
729 ldev = mlx5_lag_dev_get(dev);
730 if (!(ldev && __mlx5_lag_is_roce(ldev)))
733 if (ldev->pf[MLX5_LAG_P1].netdev == slave)
738 port = ldev->v2p_map[port];
741 spin_unlock(&lag_lock);
744 EXPORT_SYMBOL(mlx5_lag_get_slave_port);
746 int mlx5_lag_query_cong_counters(struct mlx5_core_dev *dev,
751 int outlen = MLX5_ST_SZ_BYTES(query_cong_statistics_out);
752 struct mlx5_core_dev *mdev[MLX5_MAX_PORTS];
753 struct mlx5_lag *ldev;
758 out = kvzalloc(outlen, GFP_KERNEL);
762 memset(values, 0, sizeof(*values) * num_counters);
764 spin_lock(&lag_lock);
765 ldev = mlx5_lag_dev_get(dev);
766 if (ldev && __mlx5_lag_is_active(ldev)) {
767 num_ports = MLX5_MAX_PORTS;
768 mdev[MLX5_LAG_P1] = ldev->pf[MLX5_LAG_P1].dev;
769 mdev[MLX5_LAG_P2] = ldev->pf[MLX5_LAG_P2].dev;
772 mdev[MLX5_LAG_P1] = dev;
774 spin_unlock(&lag_lock);
776 for (i = 0; i < num_ports; ++i) {
777 u32 in[MLX5_ST_SZ_DW(query_cong_statistics_in)] = {};
779 MLX5_SET(query_cong_statistics_in, in, opcode,
780 MLX5_CMD_OP_QUERY_CONG_STATISTICS);
781 ret = mlx5_cmd_exec_inout(mdev[i], query_cong_statistics, in,
786 for (j = 0; j < num_counters; ++j)
787 values[j] += be64_to_cpup((__be64 *)(out + offsets[j]));
794 EXPORT_SYMBOL(mlx5_lag_query_cong_counters);