common/mlx5: share protection domain object
[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 <stdio.h>
9
10 #include <rte_pci.h>
11 #include <rte_debug.h>
12 #include <rte_atomic.h>
13 #include <rte_rwlock.h>
14 #include <rte_log.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>
21
22 #include "mlx5_prm.h"
23 #include "mlx5_devx_cmds.h"
24 #include "mlx5_common_os.h"
25
26 /* Reported driver name. */
27 #define MLX5_PCI_DRIVER_NAME "mlx5_pci"
28 #define MLX5_AUXILIARY_DRIVER_NAME "mlx5_auxiliary"
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         (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] |= \
38                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))
39 #define BITFIELD_RESET(bf, b) \
40         (void)((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] &= \
41                 ~((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT))))
42 #define BITFIELD_ISSET(bf, b) \
43         !!(((bf)[((b) / (sizeof((bf)[0]) * CHAR_BIT))] & \
44                 ((size_t)1 << ((b) % (sizeof((bf)[0]) * CHAR_BIT)))))
45
46 /*
47  * Helper macros to work around __VA_ARGS__ limitations in a C99 compliant
48  * manner.
49  */
50 #define PMD_DRV_LOG_STRIP(a, b) a
51 #define PMD_DRV_LOG_OPAREN (
52 #define PMD_DRV_LOG_CPAREN )
53 #define PMD_DRV_LOG_COMMA ,
54
55 /* Return the file name part of a path. */
56 static inline const char *
57 pmd_drv_log_basename(const char *s)
58 {
59         const char *n = s;
60
61         while (*n)
62                 if (*(n++) == '/')
63                         s = n;
64         return s;
65 }
66
67 #define PMD_DRV_LOG___(level, type, name, ...) \
68         rte_log(RTE_LOG_ ## level, \
69                 type, \
70                 RTE_FMT(name ": " \
71                         RTE_FMT_HEAD(__VA_ARGS__,), \
72                 RTE_FMT_TAIL(__VA_ARGS__,)))
73
74 #ifdef RTE_LIBRTE_MLX5_DEBUG
75
76 #define PMD_DRV_LOG__(level, type, name, ...) \
77         PMD_DRV_LOG___(level, type, name, "%s:%u: %s(): " __VA_ARGS__)
78 #define PMD_DRV_LOG_(level, type, name, s, ...) \
79         PMD_DRV_LOG__(level, type, name,\
80                 s "\n" PMD_DRV_LOG_COMMA \
81                 pmd_drv_log_basename(__FILE__) PMD_DRV_LOG_COMMA \
82                 __LINE__ PMD_DRV_LOG_COMMA \
83                 __func__, \
84                 __VA_ARGS__)
85
86 #else /* RTE_LIBRTE_MLX5_DEBUG */
87 #define PMD_DRV_LOG__(level, type, name, ...) \
88         PMD_DRV_LOG___(level, type, name, __VA_ARGS__)
89 #define PMD_DRV_LOG_(level, type, name, s, ...) \
90         PMD_DRV_LOG__(level, type, name, s "\n", __VA_ARGS__)
91
92 #endif /* RTE_LIBRTE_MLX5_DEBUG */
93
94 /* claim_zero() does not perform any check when debugging is disabled. */
95 #ifdef RTE_LIBRTE_MLX5_DEBUG
96
97 #define MLX5_ASSERT(exp) RTE_VERIFY(exp)
98 #define claim_zero(...) MLX5_ASSERT((__VA_ARGS__) == 0)
99 #define claim_nonzero(...) MLX5_ASSERT((__VA_ARGS__) != 0)
100
101 #else /* RTE_LIBRTE_MLX5_DEBUG */
102
103 #define MLX5_ASSERT(exp) RTE_ASSERT(exp)
104 #define claim_zero(...) (__VA_ARGS__)
105 #define claim_nonzero(...) (__VA_ARGS__)
106
107 #endif /* RTE_LIBRTE_MLX5_DEBUG */
108
109 /* Allocate a buffer on the stack and fill it with a printf format string. */
110 #define MKSTR(name, ...) \
111         int mkstr_size_##name = snprintf(NULL, 0, "" __VA_ARGS__); \
112         char name[mkstr_size_##name + 1]; \
113         \
114         memset(name, 0, mkstr_size_##name + 1); \
115         snprintf(name, sizeof(name), "" __VA_ARGS__)
116
117 enum {
118         PCI_VENDOR_ID_MELLANOX = 0x15b3,
119 };
120
121 enum {
122         PCI_DEVICE_ID_MELLANOX_CONNECTX4 = 0x1013,
123         PCI_DEVICE_ID_MELLANOX_CONNECTX4VF = 0x1014,
124         PCI_DEVICE_ID_MELLANOX_CONNECTX4LX = 0x1015,
125         PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF = 0x1016,
126         PCI_DEVICE_ID_MELLANOX_CONNECTX5 = 0x1017,
127         PCI_DEVICE_ID_MELLANOX_CONNECTX5VF = 0x1018,
128         PCI_DEVICE_ID_MELLANOX_CONNECTX5EX = 0x1019,
129         PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF = 0x101a,
130         PCI_DEVICE_ID_MELLANOX_CONNECTX5BF = 0xa2d2,
131         PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF = 0xa2d3,
132         PCI_DEVICE_ID_MELLANOX_CONNECTX6 = 0x101b,
133         PCI_DEVICE_ID_MELLANOX_CONNECTX6VF = 0x101c,
134         PCI_DEVICE_ID_MELLANOX_CONNECTX6DX = 0x101d,
135         PCI_DEVICE_ID_MELLANOX_CONNECTXVF = 0x101e,
136         PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF = 0xa2d6,
137         PCI_DEVICE_ID_MELLANOX_CONNECTX6LX = 0x101f,
138         PCI_DEVICE_ID_MELLANOX_CONNECTX7 = 0x1021,
139         PCI_DEVICE_ID_MELLANOX_CONNECTX7BF = 0Xa2dc,
140 };
141
142 /* Maximum number of simultaneous unicast MAC addresses. */
143 #define MLX5_MAX_UC_MAC_ADDRESSES 128
144 /* Maximum number of simultaneous Multicast MAC addresses. */
145 #define MLX5_MAX_MC_MAC_ADDRESSES 128
146 /* Maximum number of simultaneous MAC addresses. */
147 #define MLX5_MAX_MAC_ADDRESSES \
148         (MLX5_MAX_UC_MAC_ADDRESSES + MLX5_MAX_MC_MAC_ADDRESSES)
149
150 /* Recognized Infiniband device physical port name types. */
151 enum mlx5_nl_phys_port_name_type {
152         MLX5_PHYS_PORT_NAME_TYPE_NOTSET = 0, /* Not set. */
153         MLX5_PHYS_PORT_NAME_TYPE_LEGACY, /* before kernel ver < 5.0 */
154         MLX5_PHYS_PORT_NAME_TYPE_UPLINK, /* p0, kernel ver >= 5.0 */
155         MLX5_PHYS_PORT_NAME_TYPE_PFVF, /* pf0vf0, kernel ver >= 5.0 */
156         MLX5_PHYS_PORT_NAME_TYPE_PFHPF, /* pf0, kernel ver >= 5.7, HPF rep */
157         MLX5_PHYS_PORT_NAME_TYPE_PFSF, /* pf0sf0, 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 ctrl_num; /**< Controller number (valid for c#pf#vf# format). */
167         int32_t pf_num; /**< PF number (valid for pfxvfx format only). */
168         int32_t port_name; /**< Representor port name. */
169         uint64_t switch_id; /**< Switch identifier. */
170 };
171
172 /* CQE status. */
173 enum mlx5_cqe_status {
174         MLX5_CQE_STATUS_SW_OWN = -1,
175         MLX5_CQE_STATUS_HW_OWN = -2,
176         MLX5_CQE_STATUS_ERR = -3,
177 };
178
179 /**
180  * Check whether CQE is valid.
181  *
182  * @param cqe
183  *   Pointer to CQE.
184  * @param cqes_n
185  *   Size of completion queue.
186  * @param ci
187  *   Consumer index.
188  *
189  * @return
190  *   The CQE status.
191  */
192 static __rte_always_inline enum mlx5_cqe_status
193 check_cqe(volatile struct mlx5_cqe *cqe, const uint16_t cqes_n,
194           const uint16_t ci)
195 {
196         const uint16_t idx = ci & cqes_n;
197         const uint8_t op_own = cqe->op_own;
198         const uint8_t op_owner = MLX5_CQE_OWNER(op_own);
199         const uint8_t op_code = MLX5_CQE_OPCODE(op_own);
200
201         if (unlikely((op_owner != (!!(idx))) || (op_code == MLX5_CQE_INVALID)))
202                 return MLX5_CQE_STATUS_HW_OWN;
203         rte_io_rmb();
204         if (unlikely(op_code == MLX5_CQE_RESP_ERR ||
205                      op_code == MLX5_CQE_REQ_ERR))
206                 return MLX5_CQE_STATUS_ERR;
207         return MLX5_CQE_STATUS_SW_OWN;
208 }
209
210 /*
211  * Get PCI address <DBDF> string from EAL device.
212  *
213  * @param[out] addr
214  *      The output address buffer string
215  * @param[in] size
216  *      The output buffer size
217  * @return
218  *   - 0 on success.
219  *   - Negative value and rte_errno is set otherwise.
220  */
221 int mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size);
222
223 /*
224  * Get PCI address from sysfs of a PCI-related device.
225  *
226  * @param[in] dev_path
227  *   The sysfs path should not point to the direct plain PCI device.
228  *   Instead, the node "/device/" is used to access the real device.
229  * @param[out] pci_addr
230  *   Parsed PCI address.
231  *
232  * @return
233  *   - 0 on success.
234  *   - Negative value and rte_errno is set otherwise.
235  */
236 __rte_internal
237 int mlx5_get_pci_addr(const char *dev_path, struct rte_pci_addr *pci_addr);
238
239 /*
240  * Get kernel network interface name from sysfs IB device path.
241  *
242  * @param[in] ibdev_path
243  *   The sysfs path to IB device.
244  * @param[out] ifname
245  *   Interface name output of size IF_NAMESIZE.
246  *
247  * @return
248  *   - 0 on success.
249  *   - Negative value and rte_errno is set otherwise.
250  */
251 __rte_internal
252 int mlx5_get_ifname_sysfs(const char *ibdev_path, char *ifname);
253
254 __rte_internal
255 int mlx5_auxiliary_get_child_name(const char *dev, const char *node,
256                                   char *child, size_t size);
257
258 enum mlx5_class {
259         MLX5_CLASS_INVALID,
260         MLX5_CLASS_ETH = RTE_BIT64(0),
261         MLX5_CLASS_VDPA = RTE_BIT64(1),
262         MLX5_CLASS_REGEX = RTE_BIT64(2),
263         MLX5_CLASS_COMPRESS = RTE_BIT64(3),
264         MLX5_CLASS_CRYPTO = RTE_BIT64(4),
265 };
266
267 #define MLX5_DBR_SIZE RTE_CACHE_LINE_SIZE
268
269 /* devX creation object */
270 struct mlx5_devx_obj {
271         void *obj; /* The DV object. */
272         int id; /* The object ID. */
273 };
274
275 /* UMR memory buffer used to define 1 entry in indirect mkey. */
276 struct mlx5_klm {
277         uint32_t byte_count;
278         uint32_t mkey;
279         uint64_t address;
280 };
281
282 __rte_internal
283 void mlx5_translate_port_name(const char *port_name_in,
284                               struct mlx5_switch_info *port_info_out);
285 void mlx5_glue_constructor(void);
286 __rte_internal
287 void *mlx5_devx_alloc_uar(void *ctx, int mapping);
288 extern uint8_t haswell_broadwell_cpu;
289
290 __rte_internal
291 void mlx5_common_init(void);
292
293 /*
294  * Common Driver Interface
295  *
296  * ConnectX common driver supports multiple classes: net, vDPA, regex, crypto
297  * and compress devices. This layer enables creating such multiple classes
298  * on a single device by allowing to bind multiple class-specific device
299  * drivers to attach to the common driver.
300  *
301  * ------------  -------------  --------------  -----------------  ------------
302  * | mlx5 net |  | mlx5 vdpa |  | mlx5 regex |  | mlx5 compress |  | mlx5 ... |
303  * |  driver  |  |  driver   |  |   driver   |  |     driver    |  |  drivers |
304  * ------------  -------------  --------------  -----------------  ------------
305  *                               ||
306  *                        -----------------
307  *                        |     mlx5      |
308  *                        | common driver |
309  *                        -----------------
310  *                          |          |
311  *                 -----------        -----------------
312  *                 |   mlx5  |        |   mlx5        |
313  *                 | pci dev |        | auxiliary dev |
314  *                 -----------        -----------------
315  *
316  * - mlx5 PCI bus driver binds to mlx5 PCI devices defined by PCI ID table
317  *   of all related devices.
318  * - mlx5 class driver such as net, vDPA, regex defines its specific
319  *   PCI ID table and mlx5 bus driver probes matching class drivers.
320  * - mlx5 common driver is central place that validates supported
321  *   class combinations.
322  * - mlx5 common driver hides bus difference by resolving device address
323  *   from devargs, locating target RDMA device and probing with it.
324  */
325
326 /*
327  * Device configuration structure.
328  *
329  * Merged configuration from:
330  *
331  *  - Device capabilities,
332  *  - User device parameters disabled features.
333  */
334 struct mlx5_common_dev_config {
335         int dbnc; /* Skip doorbell register write barrier. */
336         unsigned int devx:1; /* Whether devx interface is available or not. */
337         unsigned int sys_mem_en:1; /* The default memory allocator. */
338         unsigned int mr_mempool_reg_en:1;
339         /* Allow/prevent implicit mempool memory registration. */
340         unsigned int mr_ext_memseg_en:1;
341         /* Whether memseg should be extended for MR creation. */
342 };
343
344 struct mlx5_common_device {
345         struct rte_device *dev;
346         TAILQ_ENTRY(mlx5_common_device) next;
347         uint32_t classes_loaded;
348         void *ctx; /* Verbs/DV/DevX context. */
349         void *pd; /* Protection Domain. */
350         uint32_t pdn; /* Protection Domain Number. */
351         struct mlx5_common_dev_config config; /* Device configuration. */
352 };
353
354 /**
355  * Initialization function for the driver called during device probing.
356  */
357 typedef int (mlx5_class_driver_probe_t)(struct mlx5_common_device *dev);
358
359 /**
360  * Uninitialization function for the driver called during hot-unplugging.
361  */
362 typedef int (mlx5_class_driver_remove_t)(struct mlx5_common_device *dev);
363
364 /**
365  * Driver-specific DMA mapping. After a successful call the device
366  * will be able to read/write from/to this segment.
367  *
368  * @param dev
369  *   Pointer to the device.
370  * @param addr
371  *   Starting virtual address of memory to be mapped.
372  * @param iova
373  *   Starting IOVA address of memory to be mapped.
374  * @param len
375  *   Length of memory segment being mapped.
376  * @return
377  *   - 0 On success.
378  *   - Negative value and rte_errno is set otherwise.
379  */
380 typedef int (mlx5_class_driver_dma_map_t)(struct rte_device *dev, void *addr,
381                                           uint64_t iova, size_t len);
382
383 /**
384  * Driver-specific DMA un-mapping. After a successful call the device
385  * will not be able to read/write from/to this segment.
386  *
387  * @param dev
388  *   Pointer to the device.
389  * @param addr
390  *   Starting virtual address of memory to be unmapped.
391  * @param iova
392  *   Starting IOVA address of memory to be unmapped.
393  * @param len
394  *   Length of memory segment being unmapped.
395  * @return
396  *   - 0 On success.
397  *   - Negative value and rte_errno is set otherwise.
398  */
399 typedef int (mlx5_class_driver_dma_unmap_t)(struct rte_device *dev, void *addr,
400                                             uint64_t iova, size_t len);
401
402 /** Device already probed can be probed again to check for new ports. */
403 #define MLX5_DRV_PROBE_AGAIN 0x0004
404
405 /**
406  * A structure describing a mlx5 common class driver.
407  */
408 struct mlx5_class_driver {
409         TAILQ_ENTRY(mlx5_class_driver) next;
410         enum mlx5_class drv_class;            /**< Class of this driver. */
411         const char *name;                     /**< Driver name. */
412         mlx5_class_driver_probe_t *probe;     /**< Device probe function. */
413         mlx5_class_driver_remove_t *remove;   /**< Device remove function. */
414         mlx5_class_driver_dma_map_t *dma_map; /**< Device DMA map function. */
415         mlx5_class_driver_dma_unmap_t *dma_unmap;
416         /**< Device DMA unmap function. */
417         const struct rte_pci_id *id_table;    /**< ID table, NULL terminated. */
418         uint32_t probe_again:1;
419         /**< Device already probed can be probed again to check new device. */
420         uint32_t intr_lsc:1; /**< Supports link state interrupt. */
421         uint32_t intr_rmv:1; /**< Supports device remove interrupt. */
422 };
423
424 /**
425  * Register a mlx5 device driver.
426  *
427  * @param driver
428  *   A pointer to a mlx5_driver structure describing the driver
429  *   to be registered.
430  */
431 __rte_internal
432 void
433 mlx5_class_driver_register(struct mlx5_class_driver *driver);
434
435 /**
436  * Test device is a PCI bus device.
437  *
438  * @param dev
439  *   Pointer to device.
440  *
441  * @return
442  *   - True on device devargs is a PCI bus device.
443  *   - False otherwise.
444  */
445 __rte_internal
446 bool
447 mlx5_dev_is_pci(const struct rte_device *dev);
448
449 /* mlx5_common_os.c */
450
451 int mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes);
452 int mlx5_os_pd_create(struct mlx5_common_device *cdev);
453
454 #endif /* RTE_PMD_MLX5_COMMON_H_ */