1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
5 #ifndef RTE_PMD_MLX5_COMMON_H_
6 #define RTE_PMD_MLX5_COMMON_H_
11 #include <rte_debug.h>
12 #include <rte_atomic.h>
13 #include <rte_rwlock.h>
15 #include <rte_kvargs.h>
16 #include <rte_devargs.h>
17 #include <rte_bitops.h>
18 #include <rte_lcore.h>
19 #include <rte_spinlock.h>
20 #include <rte_os_shim.h>
23 #include "mlx5_devx_cmds.h"
24 #include "mlx5_common_os.h"
25 #include "mlx5_common_mr.h"
27 /* Reported driver name. */
28 #define MLX5_PCI_DRIVER_NAME "mlx5_pci"
29 #define MLX5_AUXILIARY_DRIVER_NAME "mlx5_auxiliary"
31 /* Bit-field manipulation. */
32 #define BITFIELD_DECLARE(bf, type, size) \
33 type bf[(((size_t)(size) / (sizeof(type) * CHAR_BIT)) + \
34 !!((size_t)(size) % (sizeof(type) * CHAR_BIT)))]
35 #define BITFIELD_DEFINE(bf, type, size) \
36 BITFIELD_DECLARE((bf), type, (size)) = { 0 }
37 #define BITFIELD_SET(bf, b) \
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 (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &= \
42 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))
43 #define BITFIELD_ISSET(bf, b) \
44 !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] & \
45 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
48 * Helper macros to work around __VA_ARGS__ limitations in a C99 compliant
51 #define PMD_DRV_LOG_STRIP(a, b) a
52 #define PMD_DRV_LOG_OPAREN (
53 #define PMD_DRV_LOG_CPAREN )
54 #define PMD_DRV_LOG_COMMA ,
56 /* Return the file name part of a path. */
57 static inline const char *
58 pmd_drv_log_basename(const char *s)
68 #define PMD_DRV_LOG___(level, type, name, ...) \
69 rte_log(RTE_LOG_ ## level, \
72 RTE_FMT_HEAD(__VA_ARGS__,), \
73 RTE_FMT_TAIL(__VA_ARGS__,)))
75 #ifdef RTE_LIBRTE_MLX5_DEBUG
77 #define PMD_DRV_LOG__(level, type, name, ...) \
78 PMD_DRV_LOG___(level, type, name, "%s:%u: %s(): " __VA_ARGS__)
79 #define PMD_DRV_LOG_(level, type, name, s, ...) \
80 PMD_DRV_LOG__(level, type, name,\
81 s "\n" PMD_DRV_LOG_COMMA \
82 pmd_drv_log_basename(__FILE__) PMD_DRV_LOG_COMMA \
83 __LINE__ PMD_DRV_LOG_COMMA \
87 #else /* RTE_LIBRTE_MLX5_DEBUG */
88 #define PMD_DRV_LOG__(level, type, name, ...) \
89 PMD_DRV_LOG___(level, type, name, __VA_ARGS__)
90 #define PMD_DRV_LOG_(level, type, name, s, ...) \
91 PMD_DRV_LOG__(level, type, name, s "\n", __VA_ARGS__)
93 #endif /* RTE_LIBRTE_MLX5_DEBUG */
95 /* claim_zero() does not perform any check when debugging is disabled. */
96 #ifdef RTE_LIBRTE_MLX5_DEBUG
98 #define MLX5_ASSERT(exp) RTE_VERIFY(exp)
99 #define claim_zero(...) MLX5_ASSERT((__VA_ARGS__) == 0)
100 #define claim_nonzero(...) MLX5_ASSERT((__VA_ARGS__) != 0)
102 #else /* RTE_LIBRTE_MLX5_DEBUG */
104 #define MLX5_ASSERT(exp) RTE_ASSERT(exp)
105 #define claim_zero(...) (__VA_ARGS__)
106 #define claim_nonzero(...) (__VA_ARGS__)
108 #endif /* RTE_LIBRTE_MLX5_DEBUG */
110 /* Allocate a buffer on the stack and fill it with a printf format string. */
111 #define MKSTR(name, ...) \
112 int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
113 char name[mkstr_size_##name + 1]; \
115 memset(name, 0, mkstr_size_##name + 1); \
116 snprintf(name, sizeof(name), "" __VA_ARGS__)
119 PCI_VENDOR_ID_MELLANOX = 0x15b3,
123 PCI_DEVICE_ID_MELLANOX_CONNECTX4 = 0x1013,
124 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF = 0x1014,
125 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX = 0x1015,
126 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF = 0x1016,
127 PCI_DEVICE_ID_MELLANOX_CONNECTX5 = 0x1017,
128 PCI_DEVICE_ID_MELLANOX_CONNECTX5VF = 0x1018,
129 PCI_DEVICE_ID_MELLANOX_CONNECTX5EX = 0x1019,
130 PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF = 0x101a,
131 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF = 0xa2d2,
132 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF = 0xa2d3,
133 PCI_DEVICE_ID_MELLANOX_CONNECTX6 = 0x101b,
134 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF = 0x101c,
135 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX = 0x101d,
136 PCI_DEVICE_ID_MELLANOX_CONNECTXVF = 0x101e,
137 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF = 0xa2d6,
138 PCI_DEVICE_ID_MELLANOX_CONNECTX6LX = 0x101f,
139 PCI_DEVICE_ID_MELLANOX_CONNECTX7 = 0x1021,
140 PCI_DEVICE_ID_MELLANOX_CONNECTX7BF = 0Xa2dc,
143 /* Maximum number of simultaneous unicast MAC addresses. */
144 #define MLX5_MAX_UC_MAC_ADDRESSES 128
145 /* Maximum number of simultaneous Multicast MAC addresses. */
146 #define MLX5_MAX_MC_MAC_ADDRESSES 128
147 /* Maximum number of simultaneous MAC addresses. */
148 #define MLX5_MAX_MAC_ADDRESSES \
149 (MLX5_MAX_UC_MAC_ADDRESSES + MLX5_MAX_MC_MAC_ADDRESSES)
151 /* Recognized Infiniband device physical port name types. */
152 enum mlx5_nl_phys_port_name_type {
153 MLX5_PHYS_PORT_NAME_TYPE_NOTSET = 0, /* Not set. */
154 MLX5_PHYS_PORT_NAME_TYPE_LEGACY, /* before kernel ver < 5.0 */
155 MLX5_PHYS_PORT_NAME_TYPE_UPLINK, /* p0, kernel ver >= 5.0 */
156 MLX5_PHYS_PORT_NAME_TYPE_PFVF, /* pf0vf0, kernel ver >= 5.0 */
157 MLX5_PHYS_PORT_NAME_TYPE_PFHPF, /* pf0, kernel ver >= 5.7, HPF rep */
158 MLX5_PHYS_PORT_NAME_TYPE_PFSF, /* pf0sf0, kernel ver >= 5.0 */
159 MLX5_PHYS_PORT_NAME_TYPE_UNKNOWN, /* Unrecognized. */
162 /** Switch information returned by mlx5_nl_switch_info(). */
163 struct mlx5_switch_info {
164 uint32_t master:1; /**< Master device. */
165 uint32_t representor:1; /**< Representor device. */
166 enum mlx5_nl_phys_port_name_type name_type; /** < Port name type. */
167 int32_t ctrl_num; /**< Controller number (valid for c#pf#vf# format). */
168 int32_t pf_num; /**< PF number (valid for pfxvfx format only). */
169 int32_t port_name; /**< Representor port name. */
170 uint64_t switch_id; /**< Switch identifier. */
174 enum mlx5_cqe_status {
175 MLX5_CQE_STATUS_SW_OWN = -1,
176 MLX5_CQE_STATUS_HW_OWN = -2,
177 MLX5_CQE_STATUS_ERR = -3,
181 * Check whether CQE is valid.
186 * Size of completion queue.
193 static __rte_always_inline enum mlx5_cqe_status
194 check_cqe(volatile struct mlx5_cqe *cqe, const uint16_t cqes_n,
197 const uint16_t idx = ci & cqes_n;
198 const uint8_t op_own = cqe->op_own;
199 const uint8_t op_owner = MLX5_CQE_OWNER(op_own);
200 const uint8_t op_code = MLX5_CQE_OPCODE(op_own);
202 if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
203 return MLX5_CQE_STATUS_HW_OWN;
205 if (unlikely(op_code == MLX5_CQE_RESP_ERR ||
206 op_code == MLX5_CQE_REQ_ERR))
207 return MLX5_CQE_STATUS_ERR;
208 return MLX5_CQE_STATUS_SW_OWN;
212 * Get PCI address <DBDF> string from EAL device.
215 * The output address buffer string
217 * The output buffer size
220 * - Negative value and rte_errno is set otherwise.
222 int mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size);
225 * Get PCI address from sysfs of a PCI-related device.
227 * @param[in] dev_path
228 * The sysfs path should not point to the direct plain PCI device.
229 * Instead, the node "/device/" is used to access the real device.
230 * @param[out] pci_addr
231 * Parsed PCI address.
235 * - Negative value and rte_errno is set otherwise.
238 int mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr);
241 * Get kernel network interface name from sysfs IB device path.
243 * @param[in] ibdev_path
244 * The sysfs path to IB device.
246 * Interface name output of size IF_NAMESIZE.
250 * - Negative value and rte_errno is set otherwise.
253 int mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname);
256 int mlx5_auxiliary_get_child_name(const char *dev, const char *node,
257 char *child, size_t size);
261 MLX5_CLASS_ETH = RTE_BIT64(0),
262 MLX5_CLASS_VDPA = RTE_BIT64(1),
263 MLX5_CLASS_REGEX = RTE_BIT64(2),
264 MLX5_CLASS_COMPRESS = RTE_BIT64(3),
265 MLX5_CLASS_CRYPTO = RTE_BIT64(4),
268 #define MLX5_DBR_SIZE RTE_CACHE_LINE_SIZE
270 /* devX creation object */
271 struct mlx5_devx_obj {
272 void *obj; /* The DV object. */
273 int id; /* The object ID. */
276 /* UMR memory buffer used to define 1 entry in indirect mkey. */
284 void mlx5_translate_port_name(const char *port_name_in,
285 struct mlx5_switch_info *port_info_out);
286 void mlx5_glue_constructor(void);
288 void *mlx5_devx_alloc_uar(void *ctx, int mapping);
289 extern uint8_t haswell_broadwell_cpu;
292 void mlx5_common_init(void);
295 * Common Driver Interface
297 * ConnectX common driver supports multiple classes: net, vDPA, regex, crypto
298 * and compress devices. This layer enables creating such multiple classes
299 * on a single device by allowing to bind multiple class-specific device
300 * drivers to attach to the common driver.
302 * ------------ ------------- -------------- ----------------- ------------
303 * | mlx5 net | | mlx5 vdpa | | mlx5 regex | | mlx5 compress | | mlx5 ... |
304 * | driver | | driver | | driver | | driver | | drivers |
305 * ------------ ------------- -------------- ----------------- ------------
312 * ----------- -----------------
314 * | pci dev | | auxiliary dev |
315 * ----------- -----------------
317 * - mlx5 PCI bus driver binds to mlx5 PCI devices defined by PCI ID table
318 * of all related devices.
319 * - mlx5 class driver such as net, vDPA, regex defines its specific
320 * PCI ID table and mlx5 bus driver probes matching class drivers.
321 * - mlx5 common driver is central place that validates supported
322 * class combinations.
323 * - mlx5 common driver hides bus difference by resolving device address
324 * from devargs, locating target RDMA device and probing with it.
328 * Device configuration structure.
330 * Merged configuration from:
332 * - Device capabilities,
333 * - User device parameters disabled features.
335 struct mlx5_common_dev_config {
336 struct mlx5_hca_attr hca_attr; /* HCA attributes. */
337 int dbnc; /* Skip doorbell register write barrier. */
338 unsigned int devx:1; /* Whether devx interface is available or not. */
339 unsigned int sys_mem_en:1; /* The default memory allocator. */
340 unsigned int mr_mempool_reg_en:1;
341 /* Allow/prevent implicit mempool memory registration. */
342 unsigned int mr_ext_memseg_en:1;
343 /* Whether memseg should be extended for MR creation. */
346 struct mlx5_common_device {
347 struct rte_device *dev;
348 TAILQ_ENTRY(mlx5_common_device) next;
349 uint32_t classes_loaded;
350 void *ctx; /* Verbs/DV/DevX context. */
351 void *pd; /* Protection Domain. */
352 uint32_t pdn; /* Protection Domain Number. */
353 struct mlx5_mr_share_cache mr_scache; /* Global shared MR cache. */
354 struct mlx5_common_dev_config config; /* Device configuration. */
358 * Initialization function for the driver called during device probing.
360 typedef int (mlx5_class_driver_probe_t)(struct mlx5_common_device *dev);
363 * Uninitialization function for the driver called during hot-unplugging.
365 typedef int (mlx5_class_driver_remove_t)(struct mlx5_common_device *dev);
367 /** Device already probed can be probed again to check for new ports. */
368 #define MLX5_DRV_PROBE_AGAIN 0x0004
371 * A structure describing a mlx5 common class driver.
373 struct mlx5_class_driver {
374 TAILQ_ENTRY(mlx5_class_driver) next;
375 enum mlx5_class drv_class; /**< Class of this driver. */
376 const char *name; /**< Driver name. */
377 mlx5_class_driver_probe_t *probe; /**< Device probe function. */
378 mlx5_class_driver_remove_t *remove; /**< Device remove function. */
379 const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
380 uint32_t probe_again:1;
381 /**< Device already probed can be probed again to check new device. */
382 uint32_t intr_lsc:1; /**< Supports link state interrupt. */
383 uint32_t intr_rmv:1; /**< Supports device remove interrupt. */
387 * Register a mlx5 device driver.
390 * A pointer to a mlx5_driver structure describing the driver
395 mlx5_class_driver_register(struct mlx5_class_driver *driver);
398 * Test device is a PCI bus device.
404 * - True on device devargs is a PCI bus device.
409 mlx5_dev_is_pci(const struct rte_device *dev);
411 /* mlx5_common_mr.c */
415 mlx5_mr_mb2mr(struct mlx5_common_device *cdev, struct mlx5_mp_id *mp_id,
416 struct mlx5_mr_ctrl *mr_ctrl, struct rte_mbuf *mbuf);
418 /* mlx5_common_os.c */
420 int mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes);
421 int mlx5_os_pd_create(struct mlx5_common_device *cdev);
423 #endif /* RTE_PMD_MLX5_COMMON_H_ */