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