common/mlx5: share Netlink commands
[dpdk.git] / drivers / common / mlx5 / mlx5_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4
5 #ifndef RTE_PMD_MLX5_COMMON_H_
6 #define RTE_PMD_MLX5_COMMON_H_
7
8 #include <assert.h>
9 #include <stdio.h>
10
11 #include <rte_pci.h>
12 #include <rte_atomic.h>
13 #include <rte_log.h>
14 #include <rte_kvargs.h>
15 #include <rte_devargs.h>
16
17 #include "mlx5_prm.h"
18
19
20 /*
21  * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11.
22  * Otherwise there would be a type conflict between stdbool and altivec.
23  */
24 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__)
25 #undef bool
26 /* redefine as in stdbool.h */
27 #define bool _Bool
28 #endif
29
30 /* Bit-field manipulation. */
31 #define BITFIELD_DECLARE(bf, type, size) \
32         type bf[(((size_t)(size) / (sizeof(type) * CHAR_BIT)) + \
33                  !!((size_t)(size) % (sizeof(type) * CHAR_BIT)))]
34 #define BITFIELD_DEFINE(bf, type, size) \
35         BITFIELD_DECLARE((bf), type, (size)) = { 0 }
36 #define BITFIELD_SET(bf, b) \
37         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
38          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] |= \
39                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
40 #define BITFIELD_RESET(bf, b) \
41         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
42          (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &= \
43                 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
44 #define BITFIELD_ISSET(bf, b) \
45         (assert((size_t)(b) < (sizeof(bf) * CHAR_BIT)), \
46          !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] & \
47              ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))))
48
49 /*
50  * Helper macros to work around __VA_ARGS__ limitations in a C99 compliant
51  * manner.
52  */
53 #define PMD_DRV_LOG_STRIP(a, b) a
54 #define PMD_DRV_LOG_OPAREN (
55 #define PMD_DRV_LOG_CPAREN )
56 #define PMD_DRV_LOG_COMMA ,
57
58 /* Return the file name part of a path. */
59 static inline const char *
60 pmd_drv_log_basename(const char *s)
61 {
62         const char *n = s;
63
64         while (*n)
65                 if (*(n++) == '/')
66                         s = n;
67         return s;
68 }
69
70 #define PMD_DRV_LOG___(level, type, name, ...) \
71         rte_log(RTE_LOG_ ## level, \
72                 type, \
73                 RTE_FMT(name ": " \
74                         RTE_FMT_HEAD(__VA_ARGS__,), \
75                 RTE_FMT_TAIL(__VA_ARGS__,)))
76
77 /*
78  * When debugging is enabled (NDEBUG not defined), file, line and function
79  * information replace the driver name (MLX5_DRIVER_NAME) in log messages.
80  */
81 #ifndef NDEBUG
82
83 #define PMD_DRV_LOG__(level, type, name, ...) \
84         PMD_DRV_LOG___(level, type, name, "%s:%u: %s(): " __VA_ARGS__)
85 #define PMD_DRV_LOG_(level, type, name, s, ...) \
86         PMD_DRV_LOG__(level, type, name,\
87                 s "\n" PMD_DRV_LOG_COMMA \
88                 pmd_drv_log_basename(__FILE__) PMD_DRV_LOG_COMMA \
89                 __LINE__ PMD_DRV_LOG_COMMA \
90                 __func__, \
91                 __VA_ARGS__)
92
93 #else /* NDEBUG */
94 #define PMD_DRV_LOG__(level, type, name, ...) \
95         PMD_DRV_LOG___(level, type, name, __VA_ARGS__)
96 #define PMD_DRV_LOG_(level, type, name, s, ...) \
97         PMD_DRV_LOG__(level, type, name, s "\n", __VA_ARGS__)
98
99 #endif /* NDEBUG */
100
101 /* claim_zero() does not perform any check when debugging is disabled. */
102 #ifndef NDEBUG
103
104 #define DEBUG(...) DRV_LOG(DEBUG, __VA_ARGS__)
105 #define claim_zero(...) assert((__VA_ARGS__) == 0)
106 #define claim_nonzero(...) assert((__VA_ARGS__) != 0)
107
108 #else /* NDEBUG */
109
110 #define DEBUG(...) (void)0
111 #define claim_zero(...) (__VA_ARGS__)
112 #define claim_nonzero(...) (__VA_ARGS__)
113
114 #endif /* NDEBUG */
115
116 /* Allocate a buffer on the stack and fill it with a printf format string. */
117 #define MKSTR(name, ...) \
118         int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
119         char name[mkstr_size_##name + 1]; \
120         \
121         snprintf(name, sizeof(name), "" __VA_ARGS__)
122
123 enum {
124         PCI_VENDOR_ID_MELLANOX = 0x15b3,
125 };
126
127 enum {
128         PCI_DEVICE_ID_MELLANOX_CONNECTX4 = 0x1013,
129         PCI_DEVICE_ID_MELLANOX_CONNECTX4VF = 0x1014,
130         PCI_DEVICE_ID_MELLANOX_CONNECTX4LX = 0x1015,
131         PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF = 0x1016,
132         PCI_DEVICE_ID_MELLANOX_CONNECTX5 = 0x1017,
133         PCI_DEVICE_ID_MELLANOX_CONNECTX5VF = 0x1018,
134         PCI_DEVICE_ID_MELLANOX_CONNECTX5EX = 0x1019,
135         PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF = 0x101a,
136         PCI_DEVICE_ID_MELLANOX_CONNECTX5BF = 0xa2d2,
137         PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF = 0xa2d3,
138         PCI_DEVICE_ID_MELLANOX_CONNECTX6 = 0x101b,
139         PCI_DEVICE_ID_MELLANOX_CONNECTX6VF = 0x101c,
140         PCI_DEVICE_ID_MELLANOX_CONNECTX6DX = 0x101d,
141         PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF = 0x101e,
142 };
143
144 /* Maximum number of simultaneous unicast MAC addresses. */
145 #define MLX5_MAX_UC_MAC_ADDRESSES 128
146 /* Maximum number of simultaneous Multicast MAC addresses. */
147 #define MLX5_MAX_MC_MAC_ADDRESSES 128
148 /* Maximum number of simultaneous MAC addresses. */
149 #define MLX5_MAX_MAC_ADDRESSES \
150         (MLX5_MAX_UC_MAC_ADDRESSES + MLX5_MAX_MC_MAC_ADDRESSES)
151
152 /* Recognized Infiniband device physical port name types. */
153 enum mlx5_nl_phys_port_name_type {
154         MLX5_PHYS_PORT_NAME_TYPE_NOTSET = 0, /* Not set. */
155         MLX5_PHYS_PORT_NAME_TYPE_LEGACY, /* before kernel ver < 5.0 */
156         MLX5_PHYS_PORT_NAME_TYPE_UPLINK, /* p0, kernel ver >= 5.0 */
157         MLX5_PHYS_PORT_NAME_TYPE_PFVF, /* pf0vf0, kernel ver >= 5.0 */
158         MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN, /* Unrecognized. */
159 };
160
161 /** Switch information returned by mlx5_nl_switch_info(). */
162 struct mlx5_switch_info {
163         uint32_t master:1; /**< Master device. */
164         uint32_t representor:1; /**< Representor device. */
165         enum mlx5_nl_phys_port_name_type name_type; /** < Port name type. */
166         int32_t pf_num; /**< PF number (valid for pfxvfx format only). */
167         int32_t port_name; /**< Representor port name. */
168         uint64_t switch_id; /**< Switch identifier. */
169 };
170
171 /* CQE status. */
172 enum mlx5_cqe_status {
173         MLX5_CQE_STATUS_SW_OWN = -1,
174         MLX5_CQE_STATUS_HW_OWN = -2,
175         MLX5_CQE_STATUS_ERR = -3,
176 };
177
178 /**
179  * Check whether CQE is valid.
180  *
181  * @param cqe
182  *   Pointer to CQE.
183  * @param cqes_n
184  *   Size of completion queue.
185  * @param ci
186  *   Consumer index.
187  *
188  * @return
189  *   The CQE status.
190  */
191 static __rte_always_inline enum mlx5_cqe_status
192 check_cqe(volatile struct mlx5_cqe *cqe, const uint16_t cqes_n,
193           const uint16_t ci)
194 {
195         const uint16_t idx = ci & cqes_n;
196         const uint8_t op_own = cqe->op_own;
197         const uint8_t op_owner = MLX5_CQE_OWNER(op_own);
198         const uint8_t op_code = MLX5_CQE_OPCODE(op_own);
199
200         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
201                 return MLX5_CQE_STATUS_HW_OWN;
202         rte_cio_rmb();
203         if (unlikely(op_code == MLX5_CQE_RESP_ERR ||
204                      op_code == MLX5_CQE_REQ_ERR))
205                 return MLX5_CQE_STATUS_ERR;
206         return MLX5_CQE_STATUS_SW_OWN;
207 }
208
209 int mlx5_dev_to_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr);
210
211 #define MLX5_CLASS_ARG_NAME "class"
212
213 enum mlx5_class {
214         MLX5_CLASS_NET,
215         MLX5_CLASS_VDPA,
216         MLX5_CLASS_INVALID,
217 };
218
219 enum mlx5_class mlx5_class_get(struct rte_devargs *devargs);
220 void mlx5_translate_port_name(const char *port_name_in,
221                               struct mlx5_switch_info *port_info_out);
222
223 #endif /* RTE_PMD_MLX5_COMMON_H_ */