1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019 Mellanox Technologies. */
3
4 #ifndef __MLX5_LAG_H__
5 #define __MLX5_LAG_H__
6
7 #include <linux/debugfs.h>
8
9 #define MLX5_LAG_MAX_HASH_BUCKETS 16
10 #include "mlx5_core.h"
11 #include "mp.h"
12 #include "port_sel.h"
13 #include "mpesw.h"
14
15 enum {
16 MLX5_LAG_P1,
17 MLX5_LAG_P2,
18 };
19
20 enum {
21 MLX5_LAG_FLAG_NDEVS_READY,
22 };
23
24 enum {
25 MLX5_LAG_MODE_FLAG_HASH_BASED,
26 MLX5_LAG_MODE_FLAG_SHARED_FDB,
27 MLX5_LAG_MODE_FLAG_FDB_SEL_MODE_NATIVE,
28 };
29
30 enum mlx5_lag_mode {
31 MLX5_LAG_MODE_NONE,
32 MLX5_LAG_MODE_ROCE,
33 MLX5_LAG_MODE_SRIOV,
34 MLX5_LAG_MODE_MULTIPATH,
35 MLX5_LAG_MODE_MPESW,
36 };
37
38 struct lag_func {
39 struct mlx5_core_dev *dev;
40 struct net_device *netdev;
41 bool has_drop;
42 };
43
44 /* Used for collection of netdev event info. */
45 struct lag_tracker {
46 enum netdev_lag_tx_type tx_type;
47 struct netdev_lag_lower_state_info netdev_state[MLX5_MAX_PORTS];
48 unsigned int is_bonded:1;
49 unsigned int has_inactive:1;
50 enum netdev_lag_hash hash_type;
51 };
52
53 /* LAG data of a ConnectX card.
54 * It serves both its phys functions.
55 */
56 struct mlx5_lag {
57 enum mlx5_lag_mode mode;
58 unsigned long mode_flags;
59 unsigned long state_flags;
60 u8 ports;
61 u8 buckets;
62 int mode_changes_in_progress;
63 u8 v2p_map[MLX5_MAX_PORTS * MLX5_LAG_MAX_HASH_BUCKETS];
64 struct kref ref;
65 struct lag_func pf[MLX5_MAX_PORTS];
66 struct lag_tracker tracker;
67 struct workqueue_struct *wq;
68 struct delayed_work bond_work;
69 struct notifier_block nb;
70 struct lag_mp lag_mp;
71 struct mlx5_lag_port_sel port_sel;
72 /* Protect lag fields/state changes */
73 struct mutex lock;
74 struct lag_mpesw lag_mpesw;
75 };
76
77 static inline struct mlx5_lag *
mlx5_lag_dev(struct mlx5_core_dev * dev)78 mlx5_lag_dev(struct mlx5_core_dev *dev)
79 {
80 return dev->priv.lag;
81 }
82
83 static inline bool
__mlx5_lag_is_active(struct mlx5_lag * ldev)84 __mlx5_lag_is_active(struct mlx5_lag *ldev)
85 {
86 return ldev->mode != MLX5_LAG_MODE_NONE;
87 }
88
89 static inline bool
mlx5_lag_is_ready(struct mlx5_lag * ldev)90 mlx5_lag_is_ready(struct mlx5_lag *ldev)
91 {
92 return test_bit(MLX5_LAG_FLAG_NDEVS_READY, &ldev->state_flags);
93 }
94
95 bool mlx5_lag_check_prereq(struct mlx5_lag *ldev);
96 void mlx5_modify_lag(struct mlx5_lag *ldev,
97 struct lag_tracker *tracker);
98 int mlx5_activate_lag(struct mlx5_lag *ldev,
99 struct lag_tracker *tracker,
100 enum mlx5_lag_mode mode,
101 bool shared_fdb);
102 int mlx5_lag_dev_get_netdev_idx(struct mlx5_lag *ldev,
103 struct net_device *ndev);
104
105 char *mlx5_get_str_port_sel_mode(enum mlx5_lag_mode mode, unsigned long flags);
106 void mlx5_infer_tx_enabled(struct lag_tracker *tracker, u8 num_ports,
107 u8 *ports, int *num_enabled);
108
109 void mlx5_ldev_add_debugfs(struct mlx5_core_dev *dev);
110 void mlx5_ldev_remove_debugfs(struct dentry *dbg);
111 void mlx5_disable_lag(struct mlx5_lag *ldev);
112 void mlx5_lag_remove_devices(struct mlx5_lag *ldev);
113 int mlx5_deactivate_lag(struct mlx5_lag *ldev);
114 void mlx5_lag_add_devices(struct mlx5_lag *ldev);
115
mlx5_lag_is_supported(struct mlx5_core_dev * dev)116 static inline bool mlx5_lag_is_supported(struct mlx5_core_dev *dev)
117 {
118 if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
119 !MLX5_CAP_GEN(dev, lag_master) ||
120 MLX5_CAP_GEN(dev, num_lag_ports) < 2 ||
121 MLX5_CAP_GEN(dev, num_lag_ports) > MLX5_MAX_PORTS)
122 return false;
123 return true;
124 }
125
126 #endif /* __MLX5_LAG_H__ */
127