17b4f1e6bSMatan Azrad /* SPDX-License-Identifier: BSD-3-Clause
27b4f1e6bSMatan Azrad * Copyright 2019 Mellanox Technologies, Ltd
37b4f1e6bSMatan Azrad */
47b4f1e6bSMatan Azrad
57b4f1e6bSMatan Azrad #include <unistd.h>
67b4f1e6bSMatan Azrad #include <string.h>
793e30982SMatan Azrad #include <stdio.h>
8*2744cb6eSThomas Monjalon #include <pthread.h>
97b4f1e6bSMatan Azrad
107b4f1e6bSMatan Azrad #include <rte_errno.h>
11262c7ad0SOri Kam #include <rte_mempool.h>
12ad435d32SXueming Li #include <rte_class.h>
13ad435d32SXueming Li #include <rte_malloc.h>
145dfa003dSMichael Baum #include <rte_eal_paging.h>
157b4f1e6bSMatan Azrad
167b4f1e6bSMatan Azrad #include "mlx5_common.h"
17262c7ad0SOri Kam #include "mlx5_common_os.h"
18fc59a1ecSMichael Baum #include "mlx5_common_mp.h"
1925245d5dSShiri Kuzin #include "mlx5_common_log.h"
20a77bedf2SMichael Baum #include "mlx5_common_defs.h"
21ad435d32SXueming Li #include "mlx5_common_private.h"
227b4f1e6bSMatan Azrad
234c204fe5SShiri Kuzin uint8_t haswell_broadwell_cpu;
244c204fe5SShiri Kuzin
25a729d2f0SMichael Baum /* Driver type key for new device global syntax. */
26a729d2f0SMichael Baum #define MLX5_DRIVER_KEY "driver"
27a729d2f0SMichael Baum
289d936f4fSMichael Baum /* Device parameter to get file descriptor for import device. */
299d936f4fSMichael Baum #define MLX5_DEVICE_FD "cmd_fd"
309d936f4fSMichael Baum
319d936f4fSMichael Baum /* Device parameter to get PD number for import Protection Domain. */
329d936f4fSMichael Baum #define MLX5_PD_HANDLE "pd_handle"
339d936f4fSMichael Baum
34a729d2f0SMichael Baum /* Enable extending memsegs when creating a MR. */
35a729d2f0SMichael Baum #define MLX5_MR_EXT_MEMSEG_EN "mr_ext_memseg_en"
36a729d2f0SMichael Baum
37a729d2f0SMichael Baum /* Device parameter to configure implicit registration of mempool memory. */
38a729d2f0SMichael Baum #define MLX5_MR_MEMPOOL_REG_EN "mr_mempool_reg_en"
39a729d2f0SMichael Baum
40a729d2f0SMichael Baum /* The default memory allocator used in PMD. */
41a729d2f0SMichael Baum #define MLX5_SYS_MEM_EN "sys_mem_en"
42a729d2f0SMichael Baum
43a729d2f0SMichael Baum /*
44a729d2f0SMichael Baum * Device parameter to force doorbell register mapping
45a6b9d5a5SMichael Baum * to non-cached region eliminating the extra write memory barrier.
46a6b9d5a5SMichael Baum * Deprecated, ignored (Name changed to sq_db_nc).
47a729d2f0SMichael Baum */
48a729d2f0SMichael Baum #define MLX5_TX_DB_NC "tx_db_nc"
49a729d2f0SMichael Baum
50a6b9d5a5SMichael Baum /*
51a6b9d5a5SMichael Baum * Device parameter to force doorbell register mapping
52a6b9d5a5SMichael Baum * to non-cached region eliminating the extra write memory barrier.
53a6b9d5a5SMichael Baum */
54a6b9d5a5SMichael Baum #define MLX5_SQ_DB_NC "sq_db_nc"
55a6b9d5a5SMichael Baum
564c204fe5SShiri Kuzin /* In case this is an x86_64 intel processor to check if
574c204fe5SShiri Kuzin * we should use relaxed ordering.
584c204fe5SShiri Kuzin */
594c204fe5SShiri Kuzin #ifdef RTE_ARCH_X86_64
604c204fe5SShiri Kuzin /**
614c204fe5SShiri Kuzin * This function returns processor identification and feature information
624c204fe5SShiri Kuzin * into the registers.
634c204fe5SShiri Kuzin *
644c204fe5SShiri Kuzin * @param eax, ebx, ecx, edx
654c204fe5SShiri Kuzin * Pointers to the registers that will hold cpu information.
664c204fe5SShiri Kuzin * @param level
674c204fe5SShiri Kuzin * The main category of information returned.
684c204fe5SShiri Kuzin */
mlx5_cpu_id(unsigned int level,unsigned int * eax,unsigned int * ebx,unsigned int * ecx,unsigned int * edx)694c204fe5SShiri Kuzin static inline void mlx5_cpu_id(unsigned int level,
704c204fe5SShiri Kuzin unsigned int *eax, unsigned int *ebx,
714c204fe5SShiri Kuzin unsigned int *ecx, unsigned int *edx)
724c204fe5SShiri Kuzin {
734c204fe5SShiri Kuzin __asm__("cpuid\n\t"
744c204fe5SShiri Kuzin : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
754c204fe5SShiri Kuzin : "0" (level));
764c204fe5SShiri Kuzin }
774c204fe5SShiri Kuzin #endif
784c204fe5SShiri Kuzin
79eeded204SDavid Marchand RTE_LOG_REGISTER_DEFAULT(mlx5_common_logtype, NOTICE)
8083c99c36SThomas Monjalon
81ad435d32SXueming Li /* Head of list of drivers. */
82ad435d32SXueming Li static TAILQ_HEAD(mlx5_drivers, mlx5_class_driver) drivers_list =
83ad435d32SXueming Li TAILQ_HEAD_INITIALIZER(drivers_list);
84ad435d32SXueming Li
85ad435d32SXueming Li /* Head of devices. */
86ad435d32SXueming Li static TAILQ_HEAD(mlx5_devices, mlx5_common_device) devices_list =
87ad435d32SXueming Li TAILQ_HEAD_INITIALIZER(devices_list);
88dc26c9c2SMichael Baum static pthread_mutex_t devices_list_lock;
89ad435d32SXueming Li
90ad435d32SXueming Li static const struct {
91ad435d32SXueming Li const char *name;
92ad435d32SXueming Li unsigned int drv_class;
93ad435d32SXueming Li } mlx5_classes[] = {
94ad435d32SXueming Li { .name = "vdpa", .drv_class = MLX5_CLASS_VDPA },
95ad435d32SXueming Li { .name = "eth", .drv_class = MLX5_CLASS_ETH },
96ad435d32SXueming Li /* Keep class "net" for backward compatibility. */
97ad435d32SXueming Li { .name = "net", .drv_class = MLX5_CLASS_ETH },
98ad435d32SXueming Li { .name = "regex", .drv_class = MLX5_CLASS_REGEX },
99ad435d32SXueming Li { .name = "compress", .drv_class = MLX5_CLASS_COMPRESS },
100ad435d32SXueming Li { .name = "crypto", .drv_class = MLX5_CLASS_CRYPTO },
101ad435d32SXueming Li };
102ad435d32SXueming Li
103ad435d32SXueming Li static int
class_name_to_value(const char * class_name)104ad435d32SXueming Li class_name_to_value(const char *class_name)
105ad435d32SXueming Li {
106ad435d32SXueming Li unsigned int i;
107ad435d32SXueming Li
108ad435d32SXueming Li for (i = 0; i < RTE_DIM(mlx5_classes); i++) {
109ad435d32SXueming Li if (strcmp(class_name, mlx5_classes[i].name) == 0)
110ad435d32SXueming Li return mlx5_classes[i].drv_class;
111ad435d32SXueming Li }
112ad435d32SXueming Li return -EINVAL;
113ad435d32SXueming Li }
114ad435d32SXueming Li
115ad435d32SXueming Li static struct mlx5_class_driver *
driver_get(uint32_t class)116ad435d32SXueming Li driver_get(uint32_t class)
117ad435d32SXueming Li {
118ad435d32SXueming Li struct mlx5_class_driver *driver;
119ad435d32SXueming Li
120ad435d32SXueming Li TAILQ_FOREACH(driver, &drivers_list, next) {
121ad435d32SXueming Li if ((uint32_t)driver->drv_class == class)
122ad435d32SXueming Li return driver;
123ad435d32SXueming Li }
124ad435d32SXueming Li return NULL;
125ad435d32SXueming Li }
126ad435d32SXueming Li
127a729d2f0SMichael Baum int
mlx5_kvargs_process(struct mlx5_kvargs_ctrl * mkvlist,const char * const keys[],arg_handler_t handler,void * opaque_arg)128a729d2f0SMichael Baum mlx5_kvargs_process(struct mlx5_kvargs_ctrl *mkvlist, const char *const keys[],
129a729d2f0SMichael Baum arg_handler_t handler, void *opaque_arg)
130a729d2f0SMichael Baum {
131a729d2f0SMichael Baum const struct rte_kvargs_pair *pair;
132a729d2f0SMichael Baum uint32_t i, j;
133a729d2f0SMichael Baum
134a729d2f0SMichael Baum MLX5_ASSERT(mkvlist && mkvlist->kvlist);
135a729d2f0SMichael Baum /* Process parameters. */
136a729d2f0SMichael Baum for (i = 0; i < mkvlist->kvlist->count; i++) {
137a729d2f0SMichael Baum pair = &mkvlist->kvlist->pairs[i];
138a729d2f0SMichael Baum for (j = 0; keys[j] != NULL; ++j) {
139a729d2f0SMichael Baum if (strcmp(pair->key, keys[j]) != 0)
140a729d2f0SMichael Baum continue;
141a729d2f0SMichael Baum if ((*handler)(pair->key, pair->value, opaque_arg) < 0)
142a729d2f0SMichael Baum return -1;
143a729d2f0SMichael Baum mkvlist->is_used[i] = true;
144a729d2f0SMichael Baum break;
145a729d2f0SMichael Baum }
146a729d2f0SMichael Baum }
147a729d2f0SMichael Baum return 0;
148a729d2f0SMichael Baum }
149a729d2f0SMichael Baum
150a729d2f0SMichael Baum /**
151a729d2f0SMichael Baum * Prepare a mlx5 kvargs control.
152a729d2f0SMichael Baum *
153a729d2f0SMichael Baum * @param[out] mkvlist
154a729d2f0SMichael Baum * Pointer to mlx5 kvargs control.
155a729d2f0SMichael Baum * @param[in] devargs
156a729d2f0SMichael Baum * The input string containing the key/value associations.
157a729d2f0SMichael Baum *
158a729d2f0SMichael Baum * @return
159a729d2f0SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set.
160a729d2f0SMichael Baum */
161a729d2f0SMichael Baum static int
mlx5_kvargs_prepare(struct mlx5_kvargs_ctrl * mkvlist,const struct rte_devargs * devargs)162a729d2f0SMichael Baum mlx5_kvargs_prepare(struct mlx5_kvargs_ctrl *mkvlist,
163a729d2f0SMichael Baum const struct rte_devargs *devargs)
164a729d2f0SMichael Baum {
165a729d2f0SMichael Baum struct rte_kvargs *kvlist;
166a729d2f0SMichael Baum uint32_t i;
167a729d2f0SMichael Baum
1685fb0c63dSMichael Baum if (mkvlist == NULL)
169a729d2f0SMichael Baum return 0;
1705fb0c63dSMichael Baum MLX5_ASSERT(devargs != NULL && devargs->args != NULL);
171a729d2f0SMichael Baum kvlist = rte_kvargs_parse(devargs->args, NULL);
172a729d2f0SMichael Baum if (kvlist == NULL) {
173a729d2f0SMichael Baum rte_errno = EINVAL;
174a729d2f0SMichael Baum return -rte_errno;
175a729d2f0SMichael Baum }
176a729d2f0SMichael Baum /*
177a729d2f0SMichael Baum * rte_kvargs_parse enable key without value, in mlx5 PMDs we disable
178a729d2f0SMichael Baum * this syntax.
179a729d2f0SMichael Baum */
180a729d2f0SMichael Baum for (i = 0; i < kvlist->count; i++) {
181a729d2f0SMichael Baum const struct rte_kvargs_pair *pair = &kvlist->pairs[i];
182a729d2f0SMichael Baum if (pair->value == NULL || *(pair->value) == '\0') {
183a729d2f0SMichael Baum DRV_LOG(ERR, "Key %s is missing value.", pair->key);
184a729d2f0SMichael Baum rte_kvargs_free(kvlist);
185a729d2f0SMichael Baum rte_errno = EINVAL;
186a729d2f0SMichael Baum return -rte_errno;
187a729d2f0SMichael Baum }
188a729d2f0SMichael Baum }
189a729d2f0SMichael Baum /* Makes sure all devargs used array is false. */
190a729d2f0SMichael Baum memset(mkvlist, 0, sizeof(*mkvlist));
191a729d2f0SMichael Baum mkvlist->kvlist = kvlist;
192a729d2f0SMichael Baum DRV_LOG(DEBUG, "Parse successfully %u devargs.",
193a729d2f0SMichael Baum mkvlist->kvlist->count);
194a729d2f0SMichael Baum return 0;
195a729d2f0SMichael Baum }
196a729d2f0SMichael Baum
197a729d2f0SMichael Baum /**
198a729d2f0SMichael Baum * Release a mlx5 kvargs control.
199a729d2f0SMichael Baum *
200a729d2f0SMichael Baum * @param[out] mkvlist
201a729d2f0SMichael Baum * Pointer to mlx5 kvargs control.
202a729d2f0SMichael Baum */
203a729d2f0SMichael Baum static void
mlx5_kvargs_release(struct mlx5_kvargs_ctrl * mkvlist)204a729d2f0SMichael Baum mlx5_kvargs_release(struct mlx5_kvargs_ctrl *mkvlist)
205a729d2f0SMichael Baum {
206a729d2f0SMichael Baum if (mkvlist == NULL)
207a729d2f0SMichael Baum return;
208a729d2f0SMichael Baum rte_kvargs_free(mkvlist->kvlist);
209a729d2f0SMichael Baum memset(mkvlist, 0, sizeof(*mkvlist));
210a729d2f0SMichael Baum }
211a729d2f0SMichael Baum
212a729d2f0SMichael Baum /**
213a729d2f0SMichael Baum * Validate device arguments list.
214a729d2f0SMichael Baum * It report about the first unknown parameter.
215a729d2f0SMichael Baum *
216a729d2f0SMichael Baum * @param[in] mkvlist
217a729d2f0SMichael Baum * Pointer to mlx5 kvargs control.
218a729d2f0SMichael Baum *
219a729d2f0SMichael Baum * @return
220a729d2f0SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set.
221a729d2f0SMichael Baum */
222a729d2f0SMichael Baum static int
mlx5_kvargs_validate(struct mlx5_kvargs_ctrl * mkvlist)223a729d2f0SMichael Baum mlx5_kvargs_validate(struct mlx5_kvargs_ctrl *mkvlist)
224a729d2f0SMichael Baum {
225a729d2f0SMichael Baum uint32_t i;
226a729d2f0SMichael Baum
227a729d2f0SMichael Baum /* Secondary process should not handle devargs. */
228a729d2f0SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY)
229a729d2f0SMichael Baum return 0;
230a729d2f0SMichael Baum if (mkvlist == NULL)
231a729d2f0SMichael Baum return 0;
232a729d2f0SMichael Baum for (i = 0; i < mkvlist->kvlist->count; i++) {
233a729d2f0SMichael Baum if (mkvlist->is_used[i] == 0) {
234a729d2f0SMichael Baum DRV_LOG(ERR, "Key \"%s\" "
235a729d2f0SMichael Baum "is unknown for the provided classes.",
236a729d2f0SMichael Baum mkvlist->kvlist->pairs[i].key);
237a729d2f0SMichael Baum rte_errno = EINVAL;
238a729d2f0SMichael Baum return -rte_errno;
239a729d2f0SMichael Baum }
240a729d2f0SMichael Baum }
241a729d2f0SMichael Baum return 0;
242a729d2f0SMichael Baum }
243a729d2f0SMichael Baum
24485209924SMichael Baum /**
24585209924SMichael Baum * Verify and store value for devargs.
24685209924SMichael Baum *
24785209924SMichael Baum * @param[in] key
24885209924SMichael Baum * Key argument to verify.
24985209924SMichael Baum * @param[in] val
25085209924SMichael Baum * Value associated with key.
25185209924SMichael Baum * @param opaque
25285209924SMichael Baum * User data.
25385209924SMichael Baum *
25485209924SMichael Baum * @return
25585209924SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set.
25685209924SMichael Baum */
25785209924SMichael Baum static int
mlx5_common_args_check_handler(const char * key,const char * val,void * opaque)25885209924SMichael Baum mlx5_common_args_check_handler(const char *key, const char *val, void *opaque)
25985209924SMichael Baum {
26085209924SMichael Baum struct mlx5_common_dev_config *config = opaque;
26185209924SMichael Baum signed long tmp;
26285209924SMichael Baum
263a729d2f0SMichael Baum if (strcmp(MLX5_DRIVER_KEY, key) == 0 ||
264a729d2f0SMichael Baum strcmp(RTE_DEVARGS_KEY_CLASS, key) == 0)
265a729d2f0SMichael Baum return 0;
26685209924SMichael Baum errno = 0;
26785209924SMichael Baum tmp = strtol(val, NULL, 0);
26885209924SMichael Baum if (errno) {
26985209924SMichael Baum rte_errno = errno;
27085209924SMichael Baum DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val);
27185209924SMichael Baum return -rte_errno;
27285209924SMichael Baum }
273a6b9d5a5SMichael Baum if (strcmp(key, MLX5_TX_DB_NC) == 0)
274a6b9d5a5SMichael Baum DRV_LOG(WARNING,
275a6b9d5a5SMichael Baum "%s: deprecated parameter, converted to queue_db_nc",
276a6b9d5a5SMichael Baum key);
277a6b9d5a5SMichael Baum if (strcmp(key, MLX5_SQ_DB_NC) == 0 ||
278a6b9d5a5SMichael Baum strcmp(key, MLX5_TX_DB_NC) == 0) {
279a6b9d5a5SMichael Baum if (tmp != MLX5_SQ_DB_CACHED &&
280a6b9d5a5SMichael Baum tmp != MLX5_SQ_DB_NCACHED &&
281a6b9d5a5SMichael Baum tmp != MLX5_SQ_DB_HEURISTIC) {
282a6b9d5a5SMichael Baum DRV_LOG(ERR,
283a6b9d5a5SMichael Baum "Invalid Send Queue doorbell mapping parameter.");
28485209924SMichael Baum rte_errno = EINVAL;
28585209924SMichael Baum return -rte_errno;
28685209924SMichael Baum }
28785209924SMichael Baum config->dbnc = tmp;
288a729d2f0SMichael Baum } else if (strcmp(key, MLX5_MR_EXT_MEMSEG_EN) == 0) {
28985209924SMichael Baum config->mr_ext_memseg_en = !!tmp;
290a729d2f0SMichael Baum } else if (strcmp(key, MLX5_MR_MEMPOOL_REG_EN) == 0) {
29185209924SMichael Baum config->mr_mempool_reg_en = !!tmp;
292a729d2f0SMichael Baum } else if (strcmp(key, MLX5_SYS_MEM_EN) == 0) {
29385209924SMichael Baum config->sys_mem_en = !!tmp;
2949d936f4fSMichael Baum } else if (strcmp(key, MLX5_DEVICE_FD) == 0) {
2959d936f4fSMichael Baum config->device_fd = tmp;
2969d936f4fSMichael Baum } else if (strcmp(key, MLX5_PD_HANDLE) == 0) {
2979d936f4fSMichael Baum config->pd_handle = tmp;
29885209924SMichael Baum }
29985209924SMichael Baum return 0;
30085209924SMichael Baum }
30185209924SMichael Baum
30285209924SMichael Baum /**
30385209924SMichael Baum * Parse common device parameters.
30485209924SMichael Baum *
30585209924SMichael Baum * @param devargs
30685209924SMichael Baum * Device arguments structure.
30785209924SMichael Baum * @param config
30885209924SMichael Baum * Pointer to device configuration structure.
30985209924SMichael Baum *
31085209924SMichael Baum * @return
31185209924SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set.
31285209924SMichael Baum */
31385209924SMichael Baum static int
mlx5_common_config_get(struct mlx5_kvargs_ctrl * mkvlist,struct mlx5_common_dev_config * config)314a729d2f0SMichael Baum mlx5_common_config_get(struct mlx5_kvargs_ctrl *mkvlist,
31585209924SMichael Baum struct mlx5_common_dev_config *config)
31685209924SMichael Baum {
317a729d2f0SMichael Baum const char **params = (const char *[]){
318a729d2f0SMichael Baum RTE_DEVARGS_KEY_CLASS,
319a729d2f0SMichael Baum MLX5_DRIVER_KEY,
320a729d2f0SMichael Baum MLX5_TX_DB_NC,
321a6b9d5a5SMichael Baum MLX5_SQ_DB_NC,
322a729d2f0SMichael Baum MLX5_MR_EXT_MEMSEG_EN,
323a729d2f0SMichael Baum MLX5_SYS_MEM_EN,
324a729d2f0SMichael Baum MLX5_MR_MEMPOOL_REG_EN,
3259d936f4fSMichael Baum MLX5_DEVICE_FD,
3269d936f4fSMichael Baum MLX5_PD_HANDLE,
327a729d2f0SMichael Baum NULL,
328a729d2f0SMichael Baum };
32985209924SMichael Baum int ret = 0;
33085209924SMichael Baum
33185209924SMichael Baum /* Set defaults. */
33285209924SMichael Baum config->mr_ext_memseg_en = 1;
33385209924SMichael Baum config->mr_mempool_reg_en = 1;
33485209924SMichael Baum config->sys_mem_en = 0;
33585209924SMichael Baum config->dbnc = MLX5_ARG_UNSET;
3369d936f4fSMichael Baum config->device_fd = MLX5_ARG_UNSET;
3379d936f4fSMichael Baum config->pd_handle = MLX5_ARG_UNSET;
3382c75b9bcSMichael Baum if (mkvlist == NULL)
3392c75b9bcSMichael Baum return 0;
340a729d2f0SMichael Baum /* Process common parameters. */
341a729d2f0SMichael Baum ret = mlx5_kvargs_process(mkvlist, params,
342a729d2f0SMichael Baum mlx5_common_args_check_handler, config);
343a729d2f0SMichael Baum if (ret) {
34485209924SMichael Baum rte_errno = EINVAL;
3459d936f4fSMichael Baum return -rte_errno;
346a729d2f0SMichael Baum }
3479d936f4fSMichael Baum /* Validate user arguments for remote PD and CTX if it is given. */
3489d936f4fSMichael Baum ret = mlx5_os_remote_pd_and_ctx_validate(config);
3499d936f4fSMichael Baum if (ret)
3509d936f4fSMichael Baum return ret;
35185209924SMichael Baum DRV_LOG(DEBUG, "mr_ext_memseg_en is %u.", config->mr_ext_memseg_en);
35285209924SMichael Baum DRV_LOG(DEBUG, "mr_mempool_reg_en is %u.", config->mr_mempool_reg_en);
35385209924SMichael Baum DRV_LOG(DEBUG, "sys_mem_en is %u.", config->sys_mem_en);
354a6b9d5a5SMichael Baum DRV_LOG(DEBUG, "Send Queue doorbell mapping parameter is %d.",
355a6b9d5a5SMichael Baum config->dbnc);
35685209924SMichael Baum return ret;
35785209924SMichael Baum }
35885209924SMichael Baum
359ad435d32SXueming Li static int
devargs_class_handler(__rte_unused const char * key,const char * class_names,void * opaque)360ad435d32SXueming Li devargs_class_handler(__rte_unused const char *key,
361ad435d32SXueming Li const char *class_names, void *opaque)
362ad435d32SXueming Li {
363ad435d32SXueming Li int *ret = opaque;
364ad435d32SXueming Li int class_val;
365ad435d32SXueming Li char *scratch;
366ad435d32SXueming Li char *found;
367ad435d32SXueming Li char *refstr = NULL;
368ad435d32SXueming Li
369ad435d32SXueming Li *ret = 0;
370ad435d32SXueming Li scratch = strdup(class_names);
371ad435d32SXueming Li if (scratch == NULL) {
372ad435d32SXueming Li *ret = -ENOMEM;
373ad435d32SXueming Li return *ret;
374ad435d32SXueming Li }
375ad435d32SXueming Li found = strtok_r(scratch, ":", &refstr);
376ad435d32SXueming Li if (found == NULL)
377ad435d32SXueming Li /* Empty string. */
378ad435d32SXueming Li goto err;
379ad435d32SXueming Li do {
380ad435d32SXueming Li /* Extract each individual class name. Multiple
381ad435d32SXueming Li * classes can be supplied as class=net:regex:foo:bar.
382ad435d32SXueming Li */
383ad435d32SXueming Li class_val = class_name_to_value(found);
384ad435d32SXueming Li /* Check if its a valid class. */
385ad435d32SXueming Li if (class_val < 0) {
386ad435d32SXueming Li *ret = -EINVAL;
387ad435d32SXueming Li goto err;
388ad435d32SXueming Li }
389ad435d32SXueming Li *ret |= class_val;
390ad435d32SXueming Li found = strtok_r(NULL, ":", &refstr);
391ad435d32SXueming Li } while (found != NULL);
392ad435d32SXueming Li err:
393ad435d32SXueming Li free(scratch);
394ad435d32SXueming Li if (*ret < 0)
395ad435d32SXueming Li DRV_LOG(ERR, "Invalid mlx5 class options: %s.\n", class_names);
396ad435d32SXueming Li return *ret;
397ad435d32SXueming Li }
398ad435d32SXueming Li
399ad435d32SXueming Li static int
parse_class_options(const struct rte_devargs * devargs,struct mlx5_kvargs_ctrl * mkvlist)400a729d2f0SMichael Baum parse_class_options(const struct rte_devargs *devargs,
401a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist)
402ad435d32SXueming Li {
403ad435d32SXueming Li int ret = 0;
404ad435d32SXueming Li
4055fb0c63dSMichael Baum if (mkvlist == NULL)
406ad435d32SXueming Li return 0;
4075fb0c63dSMichael Baum MLX5_ASSERT(devargs != NULL);
408ad435d32SXueming Li if (devargs->cls != NULL && devargs->cls->name != NULL)
409ad435d32SXueming Li /* Global syntax, only one class type. */
410ad435d32SXueming Li return class_name_to_value(devargs->cls->name);
411ad435d32SXueming Li /* Legacy devargs support multiple classes. */
412a729d2f0SMichael Baum rte_kvargs_process(mkvlist->kvlist, RTE_DEVARGS_KEY_CLASS,
413ad435d32SXueming Li devargs_class_handler, &ret);
414ad435d32SXueming Li return ret;
415ad435d32SXueming Li }
416ad435d32SXueming Li
417ad435d32SXueming Li static const unsigned int mlx5_class_invalid_combinations[] = {
418ad435d32SXueming Li MLX5_CLASS_ETH | MLX5_CLASS_VDPA,
419ad435d32SXueming Li /* New class combination should be added here. */
420ad435d32SXueming Li };
421ad435d32SXueming Li
422ad435d32SXueming Li static int
is_valid_class_combination(uint32_t user_classes)423ad435d32SXueming Li is_valid_class_combination(uint32_t user_classes)
424ad435d32SXueming Li {
425ad435d32SXueming Li unsigned int i;
426ad435d32SXueming Li
427ad435d32SXueming Li /* Verify if user specified unsupported combination. */
428ad435d32SXueming Li for (i = 0; i < RTE_DIM(mlx5_class_invalid_combinations); i++) {
429ad435d32SXueming Li if ((mlx5_class_invalid_combinations[i] & user_classes) ==
430ad435d32SXueming Li mlx5_class_invalid_combinations[i])
431ad435d32SXueming Li return -EINVAL;
432ad435d32SXueming Li }
433ad435d32SXueming Li /* Not found any invalid class combination. */
434ad435d32SXueming Li return 0;
435ad435d32SXueming Li }
436ad435d32SXueming Li
437ad435d32SXueming Li static bool
mlx5_bus_match(const struct mlx5_class_driver * drv,const struct rte_device * dev)438ad435d32SXueming Li mlx5_bus_match(const struct mlx5_class_driver *drv,
439ad435d32SXueming Li const struct rte_device *dev)
440ad435d32SXueming Li {
441ad435d32SXueming Li if (mlx5_dev_is_pci(dev))
442ad435d32SXueming Li return mlx5_dev_pci_match(drv, dev);
443ad435d32SXueming Li return true;
444ad435d32SXueming Li }
445ad435d32SXueming Li
446ad435d32SXueming Li static struct mlx5_common_device *
to_mlx5_device(const struct rte_device * rte_dev)447ad435d32SXueming Li to_mlx5_device(const struct rte_device *rte_dev)
448ad435d32SXueming Li {
44985209924SMichael Baum struct mlx5_common_device *cdev;
450ad435d32SXueming Li
45185209924SMichael Baum TAILQ_FOREACH(cdev, &devices_list, next) {
45285209924SMichael Baum if (rte_dev == cdev->dev)
45385209924SMichael Baum return cdev;
454ad435d32SXueming Li }
455ad435d32SXueming Li return NULL;
456ad435d32SXueming Li }
457ad435d32SXueming Li
4584d567938SThomas Monjalon int
mlx5_dev_to_pci_str(const struct rte_device * dev,char * addr,size_t size)4594d567938SThomas Monjalon mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size)
4604d567938SThomas Monjalon {
4614d567938SThomas Monjalon struct rte_pci_addr pci_addr = { 0 };
4624d567938SThomas Monjalon int ret;
4634d567938SThomas Monjalon
4644d567938SThomas Monjalon if (mlx5_dev_is_pci(dev)) {
4654d567938SThomas Monjalon /* Input might be <BDF>, format PCI address to <DBDF>. */
4664d567938SThomas Monjalon ret = rte_pci_addr_parse(dev->name, &pci_addr);
4674d567938SThomas Monjalon if (ret != 0)
4684d567938SThomas Monjalon return -ENODEV;
4694d567938SThomas Monjalon rte_pci_device_name(&pci_addr, addr, size);
4704d567938SThomas Monjalon return 0;
4714d567938SThomas Monjalon }
4724d567938SThomas Monjalon #ifdef RTE_EXEC_ENV_LINUX
4734d567938SThomas Monjalon return mlx5_auxiliary_get_pci_str(RTE_DEV_TO_AUXILIARY_CONST(dev),
4744d567938SThomas Monjalon addr, size);
4754d567938SThomas Monjalon #else
4764d567938SThomas Monjalon rte_errno = ENODEV;
4774d567938SThomas Monjalon return -rte_errno;
4784d567938SThomas Monjalon #endif
4794d567938SThomas Monjalon }
4804d567938SThomas Monjalon
481ca1418ceSMichael Baum /**
482fc59a1ecSMichael Baum * Register the mempool for the protection domain.
483fc59a1ecSMichael Baum *
484fc59a1ecSMichael Baum * @param cdev
485fc59a1ecSMichael Baum * Pointer to the mlx5 common device.
486fc59a1ecSMichael Baum * @param mp
487fc59a1ecSMichael Baum * Mempool being registered.
488fc59a1ecSMichael Baum *
489fc59a1ecSMichael Baum * @return
490fc59a1ecSMichael Baum * 0 on success, (-1) on failure and rte_errno is set.
491fc59a1ecSMichael Baum */
492fc59a1ecSMichael Baum static int
mlx5_dev_mempool_register(struct mlx5_common_device * cdev,struct rte_mempool * mp,bool is_extmem)493fc59a1ecSMichael Baum mlx5_dev_mempool_register(struct mlx5_common_device *cdev,
49408ac0358SDmitry Kozlyuk struct rte_mempool *mp, bool is_extmem)
495fc59a1ecSMichael Baum {
49608ac0358SDmitry Kozlyuk return mlx5_mr_mempool_register(cdev, mp, is_extmem);
497fc59a1ecSMichael Baum }
498fc59a1ecSMichael Baum
499fc59a1ecSMichael Baum /**
500fc59a1ecSMichael Baum * Unregister the mempool from the protection domain.
501fc59a1ecSMichael Baum *
502fc59a1ecSMichael Baum * @param cdev
503fc59a1ecSMichael Baum * Pointer to the mlx5 common device.
504fc59a1ecSMichael Baum * @param mp
505fc59a1ecSMichael Baum * Mempool being unregistered.
506fc59a1ecSMichael Baum */
507fc59a1ecSMichael Baum void
mlx5_dev_mempool_unregister(struct mlx5_common_device * cdev,struct rte_mempool * mp)508fc59a1ecSMichael Baum mlx5_dev_mempool_unregister(struct mlx5_common_device *cdev,
509fc59a1ecSMichael Baum struct rte_mempool *mp)
510fc59a1ecSMichael Baum {
51120489176SMichael Baum if (mlx5_mr_mempool_unregister(cdev, mp) < 0)
512fc59a1ecSMichael Baum DRV_LOG(WARNING, "Failed to unregister mempool %s for PD %p: %s",
513fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno));
514fc59a1ecSMichael Baum }
515fc59a1ecSMichael Baum
516fc59a1ecSMichael Baum /**
517fc59a1ecSMichael Baum * rte_mempool_walk() callback to register mempools for the protection domain.
518fc59a1ecSMichael Baum *
519fc59a1ecSMichael Baum * @param mp
520fc59a1ecSMichael Baum * The mempool being walked.
521fc59a1ecSMichael Baum * @param arg
522fc59a1ecSMichael Baum * Pointer to the device shared context.
523fc59a1ecSMichael Baum */
524fc59a1ecSMichael Baum static void
mlx5_dev_mempool_register_cb(struct rte_mempool * mp,void * arg)525fc59a1ecSMichael Baum mlx5_dev_mempool_register_cb(struct rte_mempool *mp, void *arg)
526fc59a1ecSMichael Baum {
527fc59a1ecSMichael Baum struct mlx5_common_device *cdev = arg;
528fc59a1ecSMichael Baum int ret;
529fc59a1ecSMichael Baum
53008ac0358SDmitry Kozlyuk ret = mlx5_dev_mempool_register(cdev, mp, false);
531fc59a1ecSMichael Baum if (ret < 0 && rte_errno != EEXIST)
532fc59a1ecSMichael Baum DRV_LOG(ERR,
533fc59a1ecSMichael Baum "Failed to register existing mempool %s for PD %p: %s",
534fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno));
535fc59a1ecSMichael Baum }
536fc59a1ecSMichael Baum
537fc59a1ecSMichael Baum /**
538fc59a1ecSMichael Baum * rte_mempool_walk() callback to unregister mempools
539fc59a1ecSMichael Baum * from the protection domain.
540fc59a1ecSMichael Baum *
541fc59a1ecSMichael Baum * @param mp
542fc59a1ecSMichael Baum * The mempool being walked.
543fc59a1ecSMichael Baum * @param arg
544fc59a1ecSMichael Baum * Pointer to the device shared context.
545fc59a1ecSMichael Baum */
546fc59a1ecSMichael Baum static void
mlx5_dev_mempool_unregister_cb(struct rte_mempool * mp,void * arg)547fc59a1ecSMichael Baum mlx5_dev_mempool_unregister_cb(struct rte_mempool *mp, void *arg)
548fc59a1ecSMichael Baum {
549fc59a1ecSMichael Baum mlx5_dev_mempool_unregister((struct mlx5_common_device *)arg, mp);
550fc59a1ecSMichael Baum }
551fc59a1ecSMichael Baum
552fc59a1ecSMichael Baum /**
553fc59a1ecSMichael Baum * Mempool life cycle callback for mlx5 common devices.
554fc59a1ecSMichael Baum *
555fc59a1ecSMichael Baum * @param event
556fc59a1ecSMichael Baum * Mempool life cycle event.
557fc59a1ecSMichael Baum * @param mp
558fc59a1ecSMichael Baum * Associated mempool.
559fc59a1ecSMichael Baum * @param arg
560fc59a1ecSMichael Baum * Pointer to a device shared context.
561fc59a1ecSMichael Baum */
562fc59a1ecSMichael Baum static void
mlx5_dev_mempool_event_cb(enum rte_mempool_event event,struct rte_mempool * mp,void * arg)563fc59a1ecSMichael Baum mlx5_dev_mempool_event_cb(enum rte_mempool_event event, struct rte_mempool *mp,
564fc59a1ecSMichael Baum void *arg)
565fc59a1ecSMichael Baum {
566fc59a1ecSMichael Baum struct mlx5_common_device *cdev = arg;
567fc59a1ecSMichael Baum
568fc59a1ecSMichael Baum switch (event) {
569fc59a1ecSMichael Baum case RTE_MEMPOOL_EVENT_READY:
57008ac0358SDmitry Kozlyuk if (mlx5_dev_mempool_register(cdev, mp, false) < 0)
571fc59a1ecSMichael Baum DRV_LOG(ERR,
572fc59a1ecSMichael Baum "Failed to register new mempool %s for PD %p: %s",
573fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno));
574fc59a1ecSMichael Baum break;
575fc59a1ecSMichael Baum case RTE_MEMPOOL_EVENT_DESTROY:
576fc59a1ecSMichael Baum mlx5_dev_mempool_unregister(cdev, mp);
577fc59a1ecSMichael Baum break;
578fc59a1ecSMichael Baum }
579fc59a1ecSMichael Baum }
580fc59a1ecSMichael Baum
581aeca11f8SGregory Etelson /**
582aeca11f8SGregory Etelson * Primary and secondary processes share the `cdev` pointer.
583aeca11f8SGregory Etelson * Callbacks addresses are local in each process.
584aeca11f8SGregory Etelson * Therefore, each process can register private callbacks.
585aeca11f8SGregory Etelson */
586fc59a1ecSMichael Baum int
mlx5_dev_mempool_subscribe(struct mlx5_common_device * cdev)587fc59a1ecSMichael Baum mlx5_dev_mempool_subscribe(struct mlx5_common_device *cdev)
588fc59a1ecSMichael Baum {
589fc59a1ecSMichael Baum int ret = 0;
590fc59a1ecSMichael Baum
591fc59a1ecSMichael Baum if (!cdev->config.mr_mempool_reg_en)
592fc59a1ecSMichael Baum return 0;
593fc59a1ecSMichael Baum rte_rwlock_write_lock(&cdev->mr_scache.mprwlock);
594fc59a1ecSMichael Baum /* Callback for this device may be already registered. */
595fc59a1ecSMichael Baum ret = rte_mempool_event_callback_register(mlx5_dev_mempool_event_cb,
596fc59a1ecSMichael Baum cdev);
597fc59a1ecSMichael Baum /* Register mempools only once for this device. */
598aeca11f8SGregory Etelson if (ret == 0 && rte_eal_process_type() == RTE_PROC_PRIMARY) {
599fc59a1ecSMichael Baum rte_mempool_walk(mlx5_dev_mempool_register_cb, cdev);
600aeca11f8SGregory Etelson goto exit;
601aeca11f8SGregory Etelson }
602aeca11f8SGregory Etelson if (ret != 0 && rte_errno == EEXIST)
603fc59a1ecSMichael Baum ret = 0;
604fc59a1ecSMichael Baum exit:
605fc59a1ecSMichael Baum rte_rwlock_write_unlock(&cdev->mr_scache.mprwlock);
606fc59a1ecSMichael Baum return ret;
607fc59a1ecSMichael Baum }
608fc59a1ecSMichael Baum
609fc59a1ecSMichael Baum static void
mlx5_dev_mempool_unsubscribe(struct mlx5_common_device * cdev)610fc59a1ecSMichael Baum mlx5_dev_mempool_unsubscribe(struct mlx5_common_device *cdev)
611fc59a1ecSMichael Baum {
612fc59a1ecSMichael Baum int ret;
613fc59a1ecSMichael Baum
614aeca11f8SGregory Etelson MLX5_ASSERT(cdev->dev != NULL);
6158ad97e4bSDmitry Kozlyuk if (!cdev->config.mr_mempool_reg_en)
6168ad97e4bSDmitry Kozlyuk return;
617fc59a1ecSMichael Baum /* Stop watching for mempool events and unregister all mempools. */
618fc59a1ecSMichael Baum ret = rte_mempool_event_callback_unregister(mlx5_dev_mempool_event_cb,
619fc59a1ecSMichael Baum cdev);
620fc59a1ecSMichael Baum if (ret == 0)
621fc59a1ecSMichael Baum rte_mempool_walk(mlx5_dev_mempool_unregister_cb, cdev);
622fc59a1ecSMichael Baum }
623fc59a1ecSMichael Baum
624fc59a1ecSMichael Baum /**
6259f1d636fSMichael Baum * Callback for memory event.
6269f1d636fSMichael Baum *
6279f1d636fSMichael Baum * @param event_type
6289f1d636fSMichael Baum * Memory event type.
6299f1d636fSMichael Baum * @param addr
6309f1d636fSMichael Baum * Address of memory.
6319f1d636fSMichael Baum * @param len
6329f1d636fSMichael Baum * Size of memory.
6339f1d636fSMichael Baum */
6349f1d636fSMichael Baum static void
mlx5_mr_mem_event_cb(enum rte_mem_event event_type,const void * addr,size_t len,void * arg __rte_unused)6359f1d636fSMichael Baum mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
6369f1d636fSMichael Baum size_t len, void *arg __rte_unused)
6379f1d636fSMichael Baum {
6389f1d636fSMichael Baum struct mlx5_common_device *cdev;
6399f1d636fSMichael Baum
6409f1d636fSMichael Baum /* Must be called from the primary process. */
6419f1d636fSMichael Baum MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
6429f1d636fSMichael Baum switch (event_type) {
6439f1d636fSMichael Baum case RTE_MEM_EVENT_FREE:
6449f1d636fSMichael Baum pthread_mutex_lock(&devices_list_lock);
6459f1d636fSMichael Baum /* Iterate all the existing mlx5 devices. */
6469f1d636fSMichael Baum TAILQ_FOREACH(cdev, &devices_list, next)
6479f1d636fSMichael Baum mlx5_free_mr_by_addr(&cdev->mr_scache,
6489f1d636fSMichael Baum mlx5_os_get_ctx_device_name
6499f1d636fSMichael Baum (cdev->ctx),
6509f1d636fSMichael Baum addr, len);
6519f1d636fSMichael Baum pthread_mutex_unlock(&devices_list_lock);
6529f1d636fSMichael Baum break;
6539f1d636fSMichael Baum case RTE_MEM_EVENT_ALLOC:
6549f1d636fSMichael Baum default:
6559f1d636fSMichael Baum break;
6569f1d636fSMichael Baum }
6579f1d636fSMichael Baum }
6589f1d636fSMichael Baum
6599f1d636fSMichael Baum /**
660ca1418ceSMichael Baum * Uninitialize all HW global of device context.
661ca1418ceSMichael Baum *
662ca1418ceSMichael Baum * @param cdev
663ca1418ceSMichael Baum * Pointer to mlx5 device structure.
664ca1418ceSMichael Baum *
665ca1418ceSMichael Baum * @return
666ca1418ceSMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set.
667ca1418ceSMichael Baum */
668ca1418ceSMichael Baum static void
mlx5_dev_hw_global_release(struct mlx5_common_device * cdev)669ca1418ceSMichael Baum mlx5_dev_hw_global_release(struct mlx5_common_device *cdev)
670ca1418ceSMichael Baum {
671e35ccf24SMichael Baum if (cdev->pd != NULL) {
6729d936f4fSMichael Baum claim_zero(mlx5_os_pd_release(cdev));
673e35ccf24SMichael Baum cdev->pd = NULL;
674e35ccf24SMichael Baum }
675ca1418ceSMichael Baum if (cdev->ctx != NULL) {
676ca1418ceSMichael Baum claim_zero(mlx5_glue->close_device(cdev->ctx));
677ca1418ceSMichael Baum cdev->ctx = NULL;
678ca1418ceSMichael Baum }
679ca1418ceSMichael Baum }
680ca1418ceSMichael Baum
681ca1418ceSMichael Baum /**
682ca1418ceSMichael Baum * Initialize all HW global of device context.
683ca1418ceSMichael Baum *
684ca1418ceSMichael Baum * @param cdev
685ca1418ceSMichael Baum * Pointer to mlx5 device structure.
686ca1418ceSMichael Baum * @param classes
687ca1418ceSMichael Baum * Chosen classes come from user device arguments.
688ca1418ceSMichael Baum *
689ca1418ceSMichael Baum * @return
690ca1418ceSMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set.
691ca1418ceSMichael Baum */
692ca1418ceSMichael Baum static int
mlx5_dev_hw_global_prepare(struct mlx5_common_device * cdev,uint32_t classes)693ca1418ceSMichael Baum mlx5_dev_hw_global_prepare(struct mlx5_common_device *cdev, uint32_t classes)
694ca1418ceSMichael Baum {
695ca1418ceSMichael Baum int ret;
696ca1418ceSMichael Baum
697ca1418ceSMichael Baum /* Create context device */
698ca1418ceSMichael Baum ret = mlx5_os_open_device(cdev, classes);
699ca1418ceSMichael Baum if (ret < 0)
700ca1418ceSMichael Baum return ret;
7019d936f4fSMichael Baum /*
7029d936f4fSMichael Baum * When CTX is created by Verbs, query HCA attribute is unsupported.
7039d936f4fSMichael Baum * When CTX is imported, we cannot know if it is created by DevX or
7049d936f4fSMichael Baum * Verbs. So, we use query HCA attribute function to check it.
7059d936f4fSMichael Baum */
7069d936f4fSMichael Baum if (cdev->config.devx || cdev->config.device_fd != MLX5_ARG_UNSET) {
707fe46b20cSMichael Baum /* Query HCA attributes. */
7089d936f4fSMichael Baum ret = mlx5_devx_cmd_query_hca_attr(cdev->ctx,
7099d936f4fSMichael Baum &cdev->config.hca_attr);
710fe46b20cSMichael Baum if (ret) {
7119d936f4fSMichael Baum DRV_LOG(ERR, "Unable to read HCA caps in DevX mode.");
712fe46b20cSMichael Baum rte_errno = ENOTSUP;
713fe46b20cSMichael Baum goto error;
714fe46b20cSMichael Baum }
7159d936f4fSMichael Baum cdev->config.devx = 1;
7169d936f4fSMichael Baum }
7179d936f4fSMichael Baum DRV_LOG(DEBUG, "DevX is %ssupported.", cdev->config.devx ? "" : "NOT ");
7189d936f4fSMichael Baum /* Prepare Protection Domain object and extract its pdn. */
7199d936f4fSMichael Baum ret = mlx5_os_pd_prepare(cdev);
7209d936f4fSMichael Baum if (ret)
7219d936f4fSMichael Baum goto error;
722ca1418ceSMichael Baum return 0;
723e35ccf24SMichael Baum error:
724e35ccf24SMichael Baum mlx5_dev_hw_global_release(cdev);
725e35ccf24SMichael Baum return ret;
726ca1418ceSMichael Baum }
727ca1418ceSMichael Baum
728ad435d32SXueming Li static void
mlx5_common_dev_release(struct mlx5_common_device * cdev)72985209924SMichael Baum mlx5_common_dev_release(struct mlx5_common_device *cdev)
730ad435d32SXueming Li {
731dc26c9c2SMichael Baum pthread_mutex_lock(&devices_list_lock);
73285209924SMichael Baum TAILQ_REMOVE(&devices_list, cdev, next);
733dc26c9c2SMichael Baum pthread_mutex_unlock(&devices_list_lock);
7349f1d636fSMichael Baum if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
7359f1d636fSMichael Baum if (TAILQ_EMPTY(&devices_list))
7369f1d636fSMichael Baum rte_mem_event_callback_unregister("MLX5_MEM_EVENT_CB",
7379f1d636fSMichael Baum NULL);
738fc59a1ecSMichael Baum mlx5_dev_mempool_unsubscribe(cdev);
7399f1d636fSMichael Baum mlx5_mr_release_cache(&cdev->mr_scache);
740ca1418ceSMichael Baum mlx5_dev_hw_global_release(cdev);
7419f1d636fSMichael Baum }
74285209924SMichael Baum rte_free(cdev);
74385209924SMichael Baum }
74485209924SMichael Baum
74585209924SMichael Baum static struct mlx5_common_device *
mlx5_common_dev_create(struct rte_device * eal_dev,uint32_t classes,struct mlx5_kvargs_ctrl * mkvlist)746a729d2f0SMichael Baum mlx5_common_dev_create(struct rte_device *eal_dev, uint32_t classes,
747a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist)
74885209924SMichael Baum {
74985209924SMichael Baum struct mlx5_common_device *cdev;
75085209924SMichael Baum int ret;
75185209924SMichael Baum
75285209924SMichael Baum cdev = rte_zmalloc("mlx5_common_device", sizeof(*cdev), 0);
75385209924SMichael Baum if (!cdev) {
75485209924SMichael Baum DRV_LOG(ERR, "Device allocation failure.");
75585209924SMichael Baum rte_errno = ENOMEM;
75685209924SMichael Baum return NULL;
75785209924SMichael Baum }
75885209924SMichael Baum cdev->dev = eal_dev;
75985209924SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY)
76085209924SMichael Baum goto exit;
76185209924SMichael Baum /* Parse device parameters. */
762a729d2f0SMichael Baum ret = mlx5_common_config_get(mkvlist, &cdev->config);
76385209924SMichael Baum if (ret < 0) {
76485209924SMichael Baum DRV_LOG(ERR, "Failed to process device arguments: %s",
76585209924SMichael Baum strerror(rte_errno));
76685209924SMichael Baum rte_free(cdev);
76785209924SMichael Baum return NULL;
76885209924SMichael Baum }
76985209924SMichael Baum mlx5_malloc_mem_select(cdev->config.sys_mem_en);
770ca1418ceSMichael Baum /* Initialize all HW global of device context. */
771ca1418ceSMichael Baum ret = mlx5_dev_hw_global_prepare(cdev, classes);
772ca1418ceSMichael Baum if (ret) {
773ca1418ceSMichael Baum DRV_LOG(ERR, "Failed to initialize device context.");
774ca1418ceSMichael Baum rte_free(cdev);
775ca1418ceSMichael Baum return NULL;
776ca1418ceSMichael Baum }
7779f1d636fSMichael Baum /* Initialize global MR cache resources and update its functions. */
7789f1d636fSMichael Baum ret = mlx5_mr_create_cache(&cdev->mr_scache, eal_dev->numa_node);
7799f1d636fSMichael Baum if (ret) {
7809f1d636fSMichael Baum DRV_LOG(ERR, "Failed to initialize global MR share cache.");
7819f1d636fSMichael Baum mlx5_dev_hw_global_release(cdev);
7829f1d636fSMichael Baum rte_free(cdev);
7839f1d636fSMichael Baum return NULL;
7849f1d636fSMichael Baum }
7859f1d636fSMichael Baum /* Register callback function for global shared MR cache management. */
7869f1d636fSMichael Baum if (TAILQ_EMPTY(&devices_list))
7879f1d636fSMichael Baum rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
7889f1d636fSMichael Baum mlx5_mr_mem_event_cb, NULL);
78985209924SMichael Baum exit:
79085209924SMichael Baum pthread_mutex_lock(&devices_list_lock);
79185209924SMichael Baum TAILQ_INSERT_HEAD(&devices_list, cdev, next);
79285209924SMichael Baum pthread_mutex_unlock(&devices_list_lock);
79385209924SMichael Baum return cdev;
794ad435d32SXueming Li }
795ad435d32SXueming Li
796c089eb93SMichael Baum /**
797c089eb93SMichael Baum * Validate common devargs when probing again.
798c089eb93SMichael Baum *
799c089eb93SMichael Baum * When common device probing again, it cannot change its configurations.
800c089eb93SMichael Baum * If user ask non compatible configurations in devargs, it is error.
801c089eb93SMichael Baum * This function checks the match between:
802c089eb93SMichael Baum * - Common device configurations requested by probe again devargs.
803c089eb93SMichael Baum * - Existing common device configurations.
804c089eb93SMichael Baum *
805c089eb93SMichael Baum * @param cdev
806c089eb93SMichael Baum * Pointer to mlx5 device structure.
807a729d2f0SMichael Baum * @param mkvlist
808a729d2f0SMichael Baum * Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
809c089eb93SMichael Baum *
810c089eb93SMichael Baum * @return
811c089eb93SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set.
812c089eb93SMichael Baum */
813c089eb93SMichael Baum static int
mlx5_common_probe_again_args_validate(struct mlx5_common_device * cdev,struct mlx5_kvargs_ctrl * mkvlist)814a729d2f0SMichael Baum mlx5_common_probe_again_args_validate(struct mlx5_common_device *cdev,
815a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist)
816c089eb93SMichael Baum {
817c089eb93SMichael Baum struct mlx5_common_dev_config *config;
818c089eb93SMichael Baum int ret;
819c089eb93SMichael Baum
820c089eb93SMichael Baum /* Secondary process should not handle devargs. */
821c089eb93SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY)
822c089eb93SMichael Baum return 0;
823c089eb93SMichael Baum /* Probe again doesn't have to generate devargs. */
824a729d2f0SMichael Baum if (mkvlist == NULL)
825c089eb93SMichael Baum return 0;
826c089eb93SMichael Baum config = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
827c089eb93SMichael Baum sizeof(struct mlx5_common_dev_config),
828c089eb93SMichael Baum RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
829c089eb93SMichael Baum if (config == NULL) {
830c089eb93SMichael Baum rte_errno = -ENOMEM;
831c089eb93SMichael Baum return -rte_errno;
832c089eb93SMichael Baum }
833c089eb93SMichael Baum /*
834c089eb93SMichael Baum * Creates a temporary common configure structure according to new
835c089eb93SMichael Baum * devargs attached in probing again.
836c089eb93SMichael Baum */
837a729d2f0SMichael Baum ret = mlx5_common_config_get(mkvlist, config);
838c089eb93SMichael Baum if (ret) {
839c089eb93SMichael Baum DRV_LOG(ERR, "Failed to process device configure: %s",
840c089eb93SMichael Baum strerror(rte_errno));
841c089eb93SMichael Baum mlx5_free(config);
842c089eb93SMichael Baum return ret;
843c089eb93SMichael Baum }
844c089eb93SMichael Baum /*
845c089eb93SMichael Baum * Checks the match between the temporary structure and the existing
846c089eb93SMichael Baum * common device structure.
847c089eb93SMichael Baum */
8489d936f4fSMichael Baum if (cdev->config.mr_ext_memseg_en != config->mr_ext_memseg_en) {
8499d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_MR_EXT_MEMSEG_EN "\" "
850c089eb93SMichael Baum "configuration mismatch for device %s.",
851c089eb93SMichael Baum cdev->dev->name);
852c089eb93SMichael Baum goto error;
853c089eb93SMichael Baum }
8549d936f4fSMichael Baum if (cdev->config.mr_mempool_reg_en != config->mr_mempool_reg_en) {
8559d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_MR_MEMPOOL_REG_EN "\" "
856c089eb93SMichael Baum "configuration mismatch for device %s.",
857c089eb93SMichael Baum cdev->dev->name);
858c089eb93SMichael Baum goto error;
859c089eb93SMichael Baum }
8609d936f4fSMichael Baum if (cdev->config.device_fd != config->device_fd) {
8619d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_DEVICE_FD "\" "
8629d936f4fSMichael Baum "configuration mismatch for device %s.",
863c089eb93SMichael Baum cdev->dev->name);
864c089eb93SMichael Baum goto error;
865c089eb93SMichael Baum }
8669d936f4fSMichael Baum if (cdev->config.pd_handle != config->pd_handle) {
8679d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_PD_HANDLE "\" "
8689d936f4fSMichael Baum "configuration mismatch for device %s.",
8699d936f4fSMichael Baum cdev->dev->name);
8709d936f4fSMichael Baum goto error;
8719d936f4fSMichael Baum }
8729d936f4fSMichael Baum if (cdev->config.sys_mem_en != config->sys_mem_en) {
8739d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_SYS_MEM_EN "\" "
8749d936f4fSMichael Baum "configuration mismatch for device %s.",
8759d936f4fSMichael Baum cdev->dev->name);
8769d936f4fSMichael Baum goto error;
8779d936f4fSMichael Baum }
8789d936f4fSMichael Baum if (cdev->config.dbnc != config->dbnc) {
8799d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_SQ_DB_NC "\" "
8809d936f4fSMichael Baum "configuration mismatch for device %s.",
881c089eb93SMichael Baum cdev->dev->name);
882c089eb93SMichael Baum goto error;
883c089eb93SMichael Baum }
884c089eb93SMichael Baum mlx5_free(config);
885c089eb93SMichael Baum return 0;
886c089eb93SMichael Baum error:
887c089eb93SMichael Baum mlx5_free(config);
888c089eb93SMichael Baum rte_errno = EINVAL;
889c089eb93SMichael Baum return -rte_errno;
890c089eb93SMichael Baum }
891c089eb93SMichael Baum
892ad435d32SXueming Li static int
drivers_remove(struct mlx5_common_device * cdev,uint32_t enabled_classes)89385209924SMichael Baum drivers_remove(struct mlx5_common_device *cdev, uint32_t enabled_classes)
894ad435d32SXueming Li {
895ad435d32SXueming Li struct mlx5_class_driver *driver;
896ad435d32SXueming Li int local_ret = -ENODEV;
897ad435d32SXueming Li unsigned int i = 0;
898ad435d32SXueming Li int ret = 0;
899ad435d32SXueming Li
900ad435d32SXueming Li while (enabled_classes) {
901ad435d32SXueming Li driver = driver_get(RTE_BIT64(i));
902ad435d32SXueming Li if (driver != NULL) {
90385209924SMichael Baum local_ret = driver->remove(cdev);
904ad435d32SXueming Li if (local_ret == 0)
90585209924SMichael Baum cdev->classes_loaded &= ~RTE_BIT64(i);
906ad435d32SXueming Li else if (ret == 0)
907ad435d32SXueming Li ret = local_ret;
908ad435d32SXueming Li }
909ad435d32SXueming Li enabled_classes &= ~RTE_BIT64(i);
910ad435d32SXueming Li i++;
911ad435d32SXueming Li }
912ad435d32SXueming Li if (local_ret != 0 && ret == 0)
913ad435d32SXueming Li ret = local_ret;
914ad435d32SXueming Li return ret;
915ad435d32SXueming Li }
916ad435d32SXueming Li
917ad435d32SXueming Li static int
drivers_probe(struct mlx5_common_device * cdev,uint32_t user_classes,struct mlx5_kvargs_ctrl * mkvlist)918a729d2f0SMichael Baum drivers_probe(struct mlx5_common_device *cdev, uint32_t user_classes,
919a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist)
920ad435d32SXueming Li {
921ad435d32SXueming Li struct mlx5_class_driver *driver;
922ad435d32SXueming Li uint32_t enabled_classes = 0;
923ad435d32SXueming Li bool already_loaded;
924b4a4159dSBing Zhao int ret = -EINVAL;
925ad435d32SXueming Li
926ad435d32SXueming Li TAILQ_FOREACH(driver, &drivers_list, next) {
927ad435d32SXueming Li if ((driver->drv_class & user_classes) == 0)
928ad435d32SXueming Li continue;
92985209924SMichael Baum if (!mlx5_bus_match(driver, cdev->dev))
930ad435d32SXueming Li continue;
93185209924SMichael Baum already_loaded = cdev->classes_loaded & driver->drv_class;
932ad435d32SXueming Li if (already_loaded && driver->probe_again == 0) {
933ad435d32SXueming Li DRV_LOG(ERR, "Device %s is already probed",
93485209924SMichael Baum cdev->dev->name);
935ad435d32SXueming Li ret = -EEXIST;
936ad435d32SXueming Li goto probe_err;
937ad435d32SXueming Li }
938a729d2f0SMichael Baum ret = driver->probe(cdev, mkvlist);
939ad435d32SXueming Li if (ret < 0) {
940ad435d32SXueming Li DRV_LOG(ERR, "Failed to load driver %s",
941ad435d32SXueming Li driver->name);
942ad435d32SXueming Li goto probe_err;
943ad435d32SXueming Li }
944ad435d32SXueming Li enabled_classes |= driver->drv_class;
945ad435d32SXueming Li }
946b4a4159dSBing Zhao if (!ret) {
94785209924SMichael Baum cdev->classes_loaded |= enabled_classes;
948ad435d32SXueming Li return 0;
949b4a4159dSBing Zhao }
950ad435d32SXueming Li probe_err:
9518928997aSMichael Baum /*
9528928997aSMichael Baum * Need to remove only drivers which were not probed before this probe
9538928997aSMichael Baum * instance, but have already been probed before this failure.
954ad435d32SXueming Li */
9558928997aSMichael Baum enabled_classes &= ~cdev->classes_loaded;
95685209924SMichael Baum drivers_remove(cdev, enabled_classes);
957ad435d32SXueming Li return ret;
958ad435d32SXueming Li }
959ad435d32SXueming Li
960ad435d32SXueming Li int
mlx5_common_dev_probe(struct rte_device * eal_dev)961ad435d32SXueming Li mlx5_common_dev_probe(struct rte_device *eal_dev)
962ad435d32SXueming Li {
96385209924SMichael Baum struct mlx5_common_device *cdev;
964a729d2f0SMichael Baum struct mlx5_kvargs_ctrl mkvlist;
965a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist_p = NULL;
966ad435d32SXueming Li uint32_t classes = 0;
967ad435d32SXueming Li bool new_device = false;
968ad435d32SXueming Li int ret;
969ad435d32SXueming Li
970ad435d32SXueming Li DRV_LOG(INFO, "probe device \"%s\".", eal_dev->name);
9715fb0c63dSMichael Baum if (eal_dev->devargs != NULL && eal_dev->devargs->args != NULL)
972a729d2f0SMichael Baum mkvlist_p = &mkvlist;
973a729d2f0SMichael Baum ret = mlx5_kvargs_prepare(mkvlist_p, eal_dev->devargs);
974a729d2f0SMichael Baum if (ret < 0) {
975a729d2f0SMichael Baum DRV_LOG(ERR, "Unsupported device arguments: %s",
976a729d2f0SMichael Baum eal_dev->devargs->args);
977a729d2f0SMichael Baum return ret;
978a729d2f0SMichael Baum }
979a729d2f0SMichael Baum ret = parse_class_options(eal_dev->devargs, mkvlist_p);
980ad435d32SXueming Li if (ret < 0) {
981ad435d32SXueming Li DRV_LOG(ERR, "Unsupported mlx5 class type: %s",
982ad435d32SXueming Li eal_dev->devargs->args);
983a729d2f0SMichael Baum goto class_err;
984ad435d32SXueming Li }
985ad435d32SXueming Li classes = ret;
986ad435d32SXueming Li if (classes == 0)
987ad435d32SXueming Li /* Default to net class. */
988ad435d32SXueming Li classes = MLX5_CLASS_ETH;
989c089eb93SMichael Baum /*
990c089eb93SMichael Baum * MLX5 common driver supports probing again in two scenarios:
991c089eb93SMichael Baum * - Add new driver under existing common device (regardless of the
992c089eb93SMichael Baum * driver's own support in probing again).
993c089eb93SMichael Baum * - Transfer the probing again support of the drivers themselves.
994c089eb93SMichael Baum *
995c089eb93SMichael Baum * In both scenarios it uses in the existing device. here it looks for
996c089eb93SMichael Baum * device that match to rte device, if it exists, the request classes
997c089eb93SMichael Baum * were probed with this device.
998c089eb93SMichael Baum */
99985209924SMichael Baum cdev = to_mlx5_device(eal_dev);
100085209924SMichael Baum if (!cdev) {
1001c089eb93SMichael Baum /* It isn't probing again, creates a new device. */
1002a729d2f0SMichael Baum cdev = mlx5_common_dev_create(eal_dev, classes, mkvlist_p);
1003a729d2f0SMichael Baum if (!cdev) {
1004a729d2f0SMichael Baum ret = -ENOMEM;
1005a729d2f0SMichael Baum goto class_err;
1006a729d2f0SMichael Baum }
1007ad435d32SXueming Li new_device = true;
1008c089eb93SMichael Baum } else {
1009c089eb93SMichael Baum /* It is probing again, validate common devargs match. */
1010a729d2f0SMichael Baum ret = mlx5_common_probe_again_args_validate(cdev, mkvlist_p);
1011c089eb93SMichael Baum if (ret) {
1012c089eb93SMichael Baum DRV_LOG(ERR,
1013c089eb93SMichael Baum "Probe again parameters aren't compatible : %s",
1014c089eb93SMichael Baum strerror(rte_errno));
1015a729d2f0SMichael Baum goto class_err;
1016c089eb93SMichael Baum }
1017288d7c3fSMichael Baum }
1018288d7c3fSMichael Baum /*
1019288d7c3fSMichael Baum * Validate combination here.
1020288d7c3fSMichael Baum * For new device, the classes_loaded field is 0 and it check only
1021288d7c3fSMichael Baum * the classes given as user device arguments.
1022288d7c3fSMichael Baum */
102385209924SMichael Baum ret = is_valid_class_combination(classes | cdev->classes_loaded);
1024ad435d32SXueming Li if (ret != 0) {
1025ad435d32SXueming Li DRV_LOG(ERR, "Unsupported mlx5 classes combination.");
1026288d7c3fSMichael Baum goto class_err;
1027ad435d32SXueming Li }
1028a729d2f0SMichael Baum ret = drivers_probe(cdev, classes, mkvlist_p);
1029ad435d32SXueming Li if (ret)
1030ad435d32SXueming Li goto class_err;
1031a729d2f0SMichael Baum /*
1032a729d2f0SMichael Baum * Validate that all devargs have been used, unused key -> unknown Key.
1033a729d2f0SMichael Baum * When probe again validate is failed, the added drivers aren't removed
1034a729d2f0SMichael Baum * here but when device is released.
1035a729d2f0SMichael Baum */
1036a729d2f0SMichael Baum ret = mlx5_kvargs_validate(mkvlist_p);
1037a729d2f0SMichael Baum if (ret)
1038a729d2f0SMichael Baum goto class_err;
1039a729d2f0SMichael Baum mlx5_kvargs_release(mkvlist_p);
1040ad435d32SXueming Li return 0;
1041ad435d32SXueming Li class_err:
1042a729d2f0SMichael Baum if (new_device) {
1043a729d2f0SMichael Baum /*
1044a729d2f0SMichael Baum * For new device, classes_loaded is always 0 before
1045a729d2f0SMichael Baum * drivers_probe function.
1046a729d2f0SMichael Baum */
1047a729d2f0SMichael Baum if (cdev->classes_loaded)
1048a729d2f0SMichael Baum drivers_remove(cdev, cdev->classes_loaded);
104985209924SMichael Baum mlx5_common_dev_release(cdev);
1050a729d2f0SMichael Baum }
1051a729d2f0SMichael Baum mlx5_kvargs_release(mkvlist_p);
1052ad435d32SXueming Li return ret;
1053ad435d32SXueming Li }
1054ad435d32SXueming Li
1055ad435d32SXueming Li int
mlx5_common_dev_remove(struct rte_device * eal_dev)1056ad435d32SXueming Li mlx5_common_dev_remove(struct rte_device *eal_dev)
1057ad435d32SXueming Li {
105885209924SMichael Baum struct mlx5_common_device *cdev;
1059ad435d32SXueming Li int ret;
1060ad435d32SXueming Li
106185209924SMichael Baum cdev = to_mlx5_device(eal_dev);
106285209924SMichael Baum if (!cdev)
1063ad435d32SXueming Li return -ENODEV;
1064ad435d32SXueming Li /* Matching device found, cleanup and unload drivers. */
106585209924SMichael Baum ret = drivers_remove(cdev, cdev->classes_loaded);
1066dffae63dSMichael Baum if (ret == 0)
106785209924SMichael Baum mlx5_common_dev_release(cdev);
1068ad435d32SXueming Li return ret;
1069ad435d32SXueming Li }
1070ad435d32SXueming Li
1071a5d06c90SMichael Baum /**
1072a5d06c90SMichael Baum * Callback to DMA map external memory to a device.
1073a5d06c90SMichael Baum *
1074a5d06c90SMichael Baum * @param rte_dev
1075a5d06c90SMichael Baum * Pointer to the generic device.
1076a5d06c90SMichael Baum * @param addr
1077a5d06c90SMichael Baum * Starting virtual address of memory to be mapped.
1078a5d06c90SMichael Baum * @param iova
1079a5d06c90SMichael Baum * Starting IOVA address of memory to be mapped.
1080a5d06c90SMichael Baum * @param len
1081a5d06c90SMichael Baum * Length of memory segment being mapped.
1082a5d06c90SMichael Baum *
1083a5d06c90SMichael Baum * @return
1084a5d06c90SMichael Baum * 0 on success, negative value on error.
1085a5d06c90SMichael Baum */
1086ad435d32SXueming Li int
mlx5_common_dev_dma_map(struct rte_device * rte_dev,void * addr,uint64_t iova __rte_unused,size_t len)1087a5d06c90SMichael Baum mlx5_common_dev_dma_map(struct rte_device *rte_dev, void *addr,
1088a5d06c90SMichael Baum uint64_t iova __rte_unused, size_t len)
1089ad435d32SXueming Li {
1090a5d06c90SMichael Baum struct mlx5_common_device *dev;
1091e96d3d02SDmitry Kozlyuk struct mlx5_mr_btree *bt;
1092a5d06c90SMichael Baum struct mlx5_mr *mr;
1093ad435d32SXueming Li
1094a5d06c90SMichael Baum dev = to_mlx5_device(rte_dev);
1095a5d06c90SMichael Baum if (!dev) {
1096a5d06c90SMichael Baum DRV_LOG(WARNING,
1097a5d06c90SMichael Baum "Unable to find matching mlx5 device to device %s",
1098a5d06c90SMichael Baum rte_dev->name);
1099a5d06c90SMichael Baum rte_errno = ENODEV;
1100a5d06c90SMichael Baum return -1;
1101ad435d32SXueming Li }
1102a5d06c90SMichael Baum mr = mlx5_create_mr_ext(dev->pd, (uintptr_t)addr, len,
1103a5d06c90SMichael Baum SOCKET_ID_ANY, dev->mr_scache.reg_mr_cb);
1104a5d06c90SMichael Baum if (!mr) {
1105a5d06c90SMichael Baum DRV_LOG(WARNING, "Device %s unable to DMA map", rte_dev->name);
1106a5d06c90SMichael Baum rte_errno = EINVAL;
1107a5d06c90SMichael Baum return -1;
1108ad435d32SXueming Li }
1109e96d3d02SDmitry Kozlyuk try_insert:
1110a5d06c90SMichael Baum rte_rwlock_write_lock(&dev->mr_scache.rwlock);
1111e96d3d02SDmitry Kozlyuk bt = &dev->mr_scache.cache;
1112e96d3d02SDmitry Kozlyuk if (bt->len == bt->size) {
1113e96d3d02SDmitry Kozlyuk uint32_t size;
1114e96d3d02SDmitry Kozlyuk int ret;
1115e96d3d02SDmitry Kozlyuk
1116e96d3d02SDmitry Kozlyuk size = bt->size + 1;
1117e96d3d02SDmitry Kozlyuk MLX5_ASSERT(size > bt->size);
1118e96d3d02SDmitry Kozlyuk /*
1119e96d3d02SDmitry Kozlyuk * Avoid deadlock (numbers show the sequence of events):
1120e96d3d02SDmitry Kozlyuk * mlx5_mr_create_primary():
1121e96d3d02SDmitry Kozlyuk * 1) take EAL memory lock
1122e96d3d02SDmitry Kozlyuk * 3) take MR lock
1123e96d3d02SDmitry Kozlyuk * this function:
1124e96d3d02SDmitry Kozlyuk * 2) take MR lock
1125e96d3d02SDmitry Kozlyuk * 4) take EAL memory lock while allocating the new cache
1126e96d3d02SDmitry Kozlyuk * Releasing the MR lock before step 4
1127e96d3d02SDmitry Kozlyuk * allows another thread to execute step 3.
1128e96d3d02SDmitry Kozlyuk */
1129e96d3d02SDmitry Kozlyuk rte_rwlock_write_unlock(&dev->mr_scache.rwlock);
1130e96d3d02SDmitry Kozlyuk ret = mlx5_mr_expand_cache(&dev->mr_scache, size,
1131e96d3d02SDmitry Kozlyuk rte_dev->numa_node);
1132e96d3d02SDmitry Kozlyuk if (ret < 0) {
1133e96d3d02SDmitry Kozlyuk mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb);
1134e96d3d02SDmitry Kozlyuk rte_errno = ret;
1135e96d3d02SDmitry Kozlyuk return -1;
1136e96d3d02SDmitry Kozlyuk }
1137e96d3d02SDmitry Kozlyuk goto try_insert;
1138e96d3d02SDmitry Kozlyuk }
1139a5d06c90SMichael Baum LIST_INSERT_HEAD(&dev->mr_scache.mr_list, mr, mr);
1140a5d06c90SMichael Baum /* Insert to the global cache table. */
1141a5d06c90SMichael Baum mlx5_mr_insert_cache(&dev->mr_scache, mr);
1142a5d06c90SMichael Baum rte_rwlock_write_unlock(&dev->mr_scache.rwlock);
1143a5d06c90SMichael Baum return 0;
1144ad435d32SXueming Li }
1145ad435d32SXueming Li
1146a5d06c90SMichael Baum /**
1147a5d06c90SMichael Baum * Callback to DMA unmap external memory to a device.
1148a5d06c90SMichael Baum *
1149a5d06c90SMichael Baum * @param rte_dev
1150a5d06c90SMichael Baum * Pointer to the generic device.
1151a5d06c90SMichael Baum * @param addr
1152a5d06c90SMichael Baum * Starting virtual address of memory to be unmapped.
1153a5d06c90SMichael Baum * @param iova
1154a5d06c90SMichael Baum * Starting IOVA address of memory to be unmapped.
1155a5d06c90SMichael Baum * @param len
1156a5d06c90SMichael Baum * Length of memory segment being unmapped.
1157a5d06c90SMichael Baum *
1158a5d06c90SMichael Baum * @return
1159a5d06c90SMichael Baum * 0 on success, negative value on error.
1160a5d06c90SMichael Baum */
1161ad435d32SXueming Li int
mlx5_common_dev_dma_unmap(struct rte_device * rte_dev,void * addr,uint64_t iova __rte_unused,size_t len __rte_unused)1162a5d06c90SMichael Baum mlx5_common_dev_dma_unmap(struct rte_device *rte_dev, void *addr,
1163a5d06c90SMichael Baum uint64_t iova __rte_unused, size_t len __rte_unused)
1164ad435d32SXueming Li {
1165a5d06c90SMichael Baum struct mlx5_common_device *dev;
1166a5d06c90SMichael Baum struct mr_cache_entry entry;
1167a5d06c90SMichael Baum struct mlx5_mr *mr;
1168ad435d32SXueming Li
1169a5d06c90SMichael Baum dev = to_mlx5_device(rte_dev);
1170a5d06c90SMichael Baum if (!dev) {
1171a5d06c90SMichael Baum DRV_LOG(WARNING,
1172a5d06c90SMichael Baum "Unable to find matching mlx5 device to device %s.",
1173a5d06c90SMichael Baum rte_dev->name);
1174a5d06c90SMichael Baum rte_errno = ENODEV;
1175a5d06c90SMichael Baum return -1;
1176ad435d32SXueming Li }
1177a5d06c90SMichael Baum rte_rwlock_read_lock(&dev->mr_scache.rwlock);
1178a5d06c90SMichael Baum mr = mlx5_mr_lookup_list(&dev->mr_scache, &entry, (uintptr_t)addr);
1179a5d06c90SMichael Baum if (!mr) {
1180a5d06c90SMichael Baum rte_rwlock_read_unlock(&dev->mr_scache.rwlock);
1181a5d06c90SMichael Baum DRV_LOG(WARNING,
1182a5d06c90SMichael Baum "Address 0x%" PRIxPTR " wasn't registered to device %s",
1183a5d06c90SMichael Baum (uintptr_t)addr, rte_dev->name);
1184a5d06c90SMichael Baum rte_errno = EINVAL;
1185a5d06c90SMichael Baum return -1;
1186a5d06c90SMichael Baum }
1187a5d06c90SMichael Baum LIST_REMOVE(mr, mr);
1188a5d06c90SMichael Baum DRV_LOG(DEBUG, "MR(%p) is removed from list.", (void *)mr);
1189a5d06c90SMichael Baum mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb);
1190a5d06c90SMichael Baum mlx5_mr_rebuild_cache(&dev->mr_scache);
1191a5d06c90SMichael Baum /*
1192a5d06c90SMichael Baum * No explicit wmb is needed after updating dev_gen due to
1193a5d06c90SMichael Baum * store-release ordering in unlock that provides the
1194a5d06c90SMichael Baum * implicit barrier at the software visible level.
1195a5d06c90SMichael Baum */
1196a5d06c90SMichael Baum ++dev->mr_scache.dev_gen;
1197a5d06c90SMichael Baum DRV_LOG(DEBUG, "Broadcasting local cache flush, gen=%d.",
1198a5d06c90SMichael Baum dev->mr_scache.dev_gen);
1199a5d06c90SMichael Baum rte_rwlock_read_unlock(&dev->mr_scache.rwlock);
1200a5d06c90SMichael Baum return 0;
1201ad435d32SXueming Li }
1202ad435d32SXueming Li
1203ad435d32SXueming Li void
mlx5_class_driver_register(struct mlx5_class_driver * driver)1204ad435d32SXueming Li mlx5_class_driver_register(struct mlx5_class_driver *driver)
1205ad435d32SXueming Li {
1206ad435d32SXueming Li mlx5_common_driver_on_register_pci(driver);
1207ad435d32SXueming Li TAILQ_INSERT_TAIL(&drivers_list, driver, next);
1208ad435d32SXueming Li }
1209ad435d32SXueming Li
mlx5_common_driver_init(void)1210ad435d32SXueming Li static void mlx5_common_driver_init(void)
1211ad435d32SXueming Li {
1212ad435d32SXueming Li mlx5_common_pci_init();
1213777b72a9SXueming Li #ifdef RTE_EXEC_ENV_LINUX
1214777b72a9SXueming Li mlx5_common_auxiliary_init();
1215777b72a9SXueming Li #endif
1216ad435d32SXueming Li }
1217ad435d32SXueming Li
121882088001SParav Pandit static bool mlx5_common_initialized;
121982088001SParav Pandit
122083c99c36SThomas Monjalon /**
12217be78d02SJosh Soref * One time initialization routine for run-time dependency on glue library
122282088001SParav Pandit * for multiple PMDs. Each mlx5 PMD that depends on mlx5_common module,
122382088001SParav Pandit * must invoke in its constructor.
122483c99c36SThomas Monjalon */
122582088001SParav Pandit void
mlx5_common_init(void)122682088001SParav Pandit mlx5_common_init(void)
122783c99c36SThomas Monjalon {
122882088001SParav Pandit if (mlx5_common_initialized)
122982088001SParav Pandit return;
123082088001SParav Pandit
1231dc26c9c2SMichael Baum pthread_mutex_init(&devices_list_lock, NULL);
123279aa4307SOphir Munk mlx5_glue_constructor();
1233ad435d32SXueming Li mlx5_common_driver_init();
123482088001SParav Pandit mlx5_common_initialized = true;
12357b4f1e6bSMatan Azrad }
12364c204fe5SShiri Kuzin
12374c204fe5SShiri Kuzin /**
12384c204fe5SShiri Kuzin * This function is responsible of initializing the variable
12394c204fe5SShiri Kuzin * haswell_broadwell_cpu by checking if the cpu is intel
12404c204fe5SShiri Kuzin * and reading the data returned from mlx5_cpu_id().
12414c204fe5SShiri Kuzin * since haswell and broadwell cpus don't have improved performance
12424c204fe5SShiri Kuzin * when using relaxed ordering we want to check the cpu type before
12434c204fe5SShiri Kuzin * before deciding whether to enable RO or not.
12444c204fe5SShiri Kuzin * if the cpu is haswell or broadwell the variable will be set to 1
12454c204fe5SShiri Kuzin * otherwise it will be 0.
12464c204fe5SShiri Kuzin */
RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu,LOG)12474c204fe5SShiri Kuzin RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu, LOG)
12484c204fe5SShiri Kuzin {
12494c204fe5SShiri Kuzin #ifdef RTE_ARCH_X86_64
12504c204fe5SShiri Kuzin unsigned int broadwell_models[4] = {0x3d, 0x47, 0x4F, 0x56};
12514c204fe5SShiri Kuzin unsigned int haswell_models[4] = {0x3c, 0x3f, 0x45, 0x46};
12524c204fe5SShiri Kuzin unsigned int i, model, family, brand_id, vendor;
12534c204fe5SShiri Kuzin unsigned int signature_intel_ebx = 0x756e6547;
12544c204fe5SShiri Kuzin unsigned int extended_model;
12554c204fe5SShiri Kuzin unsigned int eax = 0;
12564c204fe5SShiri Kuzin unsigned int ebx = 0;
12574c204fe5SShiri Kuzin unsigned int ecx = 0;
12584c204fe5SShiri Kuzin unsigned int edx = 0;
12594c204fe5SShiri Kuzin int max_level;
12604c204fe5SShiri Kuzin
12614c204fe5SShiri Kuzin mlx5_cpu_id(0, &eax, &ebx, &ecx, &edx);
12624c204fe5SShiri Kuzin vendor = ebx;
12634c204fe5SShiri Kuzin max_level = eax;
12644c204fe5SShiri Kuzin if (max_level < 1) {
12654c204fe5SShiri Kuzin haswell_broadwell_cpu = 0;
12664c204fe5SShiri Kuzin return;
12674c204fe5SShiri Kuzin }
12684c204fe5SShiri Kuzin mlx5_cpu_id(1, &eax, &ebx, &ecx, &edx);
12694c204fe5SShiri Kuzin model = (eax >> 4) & 0x0f;
12704c204fe5SShiri Kuzin family = (eax >> 8) & 0x0f;
12714c204fe5SShiri Kuzin brand_id = ebx & 0xff;
12724c204fe5SShiri Kuzin extended_model = (eax >> 12) & 0xf0;
12734c204fe5SShiri Kuzin /* Check if the processor is Haswell or Broadwell */
12744c204fe5SShiri Kuzin if (vendor == signature_intel_ebx) {
12754c204fe5SShiri Kuzin if (family == 0x06)
12764c204fe5SShiri Kuzin model += extended_model;
12774c204fe5SShiri Kuzin if (brand_id == 0 && family == 0x6) {
12784c204fe5SShiri Kuzin for (i = 0; i < RTE_DIM(broadwell_models); i++)
12794c204fe5SShiri Kuzin if (model == broadwell_models[i]) {
12804c204fe5SShiri Kuzin haswell_broadwell_cpu = 1;
12814c204fe5SShiri Kuzin return;
12824c204fe5SShiri Kuzin }
12834c204fe5SShiri Kuzin for (i = 0; i < RTE_DIM(haswell_models); i++)
12844c204fe5SShiri Kuzin if (model == haswell_models[i]) {
12854c204fe5SShiri Kuzin haswell_broadwell_cpu = 1;
12864c204fe5SShiri Kuzin return;
12874c204fe5SShiri Kuzin }
12884c204fe5SShiri Kuzin }
12894c204fe5SShiri Kuzin }
12904c204fe5SShiri Kuzin #endif
12914c204fe5SShiri Kuzin haswell_broadwell_cpu = 0;
12924c204fe5SShiri Kuzin }
1293262c7ad0SOri Kam
1294262c7ad0SOri Kam /**
12959cc0e99cSViacheslav Ovsiienko * Allocate the User Access Region with DevX on specified device.
1296b4371d3dSMichael Baum * This routine handles the following UAR allocation issues:
12979cc0e99cSViacheslav Ovsiienko *
12985dfa003dSMichael Baum * - Try to allocate the UAR with the most appropriate memory mapping
1299b4371d3dSMichael Baum * type from the ones supported by the host.
1300b4371d3dSMichael Baum *
13015dfa003dSMichael Baum * - Try to allocate the UAR with non-NULL base address OFED 5.0.x and
1302b4371d3dSMichael Baum * Upstream rdma_core before v29 returned the NULL as UAR base address
1303b4371d3dSMichael Baum * if UAR was not the first object in the UAR page.
1304b4371d3dSMichael Baum * It caused the PMD failure and we should try to get another UAR till
1305b4371d3dSMichael Baum * we get the first one with non-NULL base address returned.
1306b4371d3dSMichael Baum *
1307b4371d3dSMichael Baum * @param [in] cdev
1308b4371d3dSMichael Baum * Pointer to mlx5 device structure to perform allocation on its context.
13099cc0e99cSViacheslav Ovsiienko *
13109cc0e99cSViacheslav Ovsiienko * @return
13119cc0e99cSViacheslav Ovsiienko * UAR object pointer on success, NULL otherwise and rte_errno is set.
13129cc0e99cSViacheslav Ovsiienko */
13135dfa003dSMichael Baum static void *
mlx5_devx_alloc_uar(struct mlx5_common_device * cdev)1314b4371d3dSMichael Baum mlx5_devx_alloc_uar(struct mlx5_common_device *cdev)
13159cc0e99cSViacheslav Ovsiienko {
13169cc0e99cSViacheslav Ovsiienko void *uar;
13179cc0e99cSViacheslav Ovsiienko uint32_t retry, uar_mapping;
13189cc0e99cSViacheslav Ovsiienko void *base_addr;
13199cc0e99cSViacheslav Ovsiienko
13209cc0e99cSViacheslav Ovsiienko for (retry = 0; retry < MLX5_ALLOC_UAR_RETRY; ++retry) {
13219cc0e99cSViacheslav Ovsiienko #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
13229cc0e99cSViacheslav Ovsiienko /* Control the mapping type according to the settings. */
1323a6b9d5a5SMichael Baum uar_mapping = (cdev->config.dbnc == MLX5_SQ_DB_NCACHED) ?
1324b4371d3dSMichael Baum MLX5DV_UAR_ALLOC_TYPE_NC : MLX5DV_UAR_ALLOC_TYPE_BF;
13259cc0e99cSViacheslav Ovsiienko #else
13269cc0e99cSViacheslav Ovsiienko /*
13279cc0e99cSViacheslav Ovsiienko * It seems we have no way to control the memory mapping type
13289cc0e99cSViacheslav Ovsiienko * for the UAR, the default "Write-Combining" type is supposed.
13299cc0e99cSViacheslav Ovsiienko */
13309cc0e99cSViacheslav Ovsiienko uar_mapping = 0;
13319cc0e99cSViacheslav Ovsiienko #endif
1332b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
13339cc0e99cSViacheslav Ovsiienko #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
1334b4371d3dSMichael Baum if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_BF) {
1335b4371d3dSMichael Baum /*
1336b4371d3dSMichael Baum * In some environments like virtual machine the
1337b4371d3dSMichael Baum * Write Combining mapped might be not supported and
1338b4371d3dSMichael Baum * UAR allocation fails. We tried "Non-Cached" mapping
1339b4371d3dSMichael Baum * for the case.
1340b4371d3dSMichael Baum */
1341b4371d3dSMichael Baum DRV_LOG(DEBUG, "Failed to allocate DevX UAR (BF)");
1342b4371d3dSMichael Baum uar_mapping = MLX5DV_UAR_ALLOC_TYPE_NC;
1343b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
1344b4371d3dSMichael Baum } else if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_NC) {
13459cc0e99cSViacheslav Ovsiienko /*
13469cc0e99cSViacheslav Ovsiienko * If Verbs/kernel does not support "Non-Cached"
13479cc0e99cSViacheslav Ovsiienko * try the "Write-Combining".
13489cc0e99cSViacheslav Ovsiienko */
13493f0e54feSMichael Baum DRV_LOG(DEBUG, "Failed to allocate DevX UAR (NC)");
13509cc0e99cSViacheslav Ovsiienko uar_mapping = MLX5DV_UAR_ALLOC_TYPE_BF;
1351b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
13529cc0e99cSViacheslav Ovsiienko }
13539cc0e99cSViacheslav Ovsiienko #endif
13549cc0e99cSViacheslav Ovsiienko if (!uar) {
13559cc0e99cSViacheslav Ovsiienko DRV_LOG(ERR, "Failed to allocate DevX UAR (BF/NC)");
13569cc0e99cSViacheslav Ovsiienko rte_errno = ENOMEM;
13579cc0e99cSViacheslav Ovsiienko goto exit;
13589cc0e99cSViacheslav Ovsiienko }
13599cc0e99cSViacheslav Ovsiienko base_addr = mlx5_os_get_devx_uar_base_addr(uar);
13609cc0e99cSViacheslav Ovsiienko if (base_addr)
13619cc0e99cSViacheslav Ovsiienko break;
13629cc0e99cSViacheslav Ovsiienko /*
13639cc0e99cSViacheslav Ovsiienko * The UARs are allocated by rdma_core within the
13649cc0e99cSViacheslav Ovsiienko * IB device context, on context closure all UARs
13659cc0e99cSViacheslav Ovsiienko * will be freed, should be no memory/object leakage.
13669cc0e99cSViacheslav Ovsiienko */
13673f0e54feSMichael Baum DRV_LOG(DEBUG, "Retrying to allocate DevX UAR");
13689cc0e99cSViacheslav Ovsiienko uar = NULL;
13699cc0e99cSViacheslav Ovsiienko }
13709cc0e99cSViacheslav Ovsiienko /* Check whether we finally succeeded with valid UAR allocation. */
13719cc0e99cSViacheslav Ovsiienko if (!uar) {
13729cc0e99cSViacheslav Ovsiienko DRV_LOG(ERR, "Failed to allocate DevX UAR (NULL base)");
13739cc0e99cSViacheslav Ovsiienko rte_errno = ENOMEM;
13749cc0e99cSViacheslav Ovsiienko }
13759cc0e99cSViacheslav Ovsiienko /*
13769cc0e99cSViacheslav Ovsiienko * Return void * instead of struct mlx5dv_devx_uar *
13779cc0e99cSViacheslav Ovsiienko * is for compatibility with older rdma-core library headers.
13789cc0e99cSViacheslav Ovsiienko */
13799cc0e99cSViacheslav Ovsiienko exit:
13809cc0e99cSViacheslav Ovsiienko return uar;
13819cc0e99cSViacheslav Ovsiienko }
1382ad435d32SXueming Li
13835dfa003dSMichael Baum void
mlx5_devx_uar_release(struct mlx5_uar * uar)13845dfa003dSMichael Baum mlx5_devx_uar_release(struct mlx5_uar *uar)
13855dfa003dSMichael Baum {
13865dfa003dSMichael Baum if (uar->obj != NULL)
13875dfa003dSMichael Baum mlx5_glue->devx_free_uar(uar->obj);
13885dfa003dSMichael Baum memset(uar, 0, sizeof(*uar));
13895dfa003dSMichael Baum }
13905dfa003dSMichael Baum
13915dfa003dSMichael Baum int
mlx5_devx_uar_prepare(struct mlx5_common_device * cdev,struct mlx5_uar * uar)13925dfa003dSMichael Baum mlx5_devx_uar_prepare(struct mlx5_common_device *cdev, struct mlx5_uar *uar)
13935dfa003dSMichael Baum {
13945dfa003dSMichael Baum off_t uar_mmap_offset;
13955dfa003dSMichael Baum const size_t page_size = rte_mem_page_size();
13965dfa003dSMichael Baum void *base_addr;
13975dfa003dSMichael Baum void *uar_obj;
13985dfa003dSMichael Baum
13995dfa003dSMichael Baum if (page_size == (size_t)-1) {
14005dfa003dSMichael Baum DRV_LOG(ERR, "Failed to get mem page size");
14015dfa003dSMichael Baum rte_errno = ENOMEM;
14025dfa003dSMichael Baum return -1;
14035dfa003dSMichael Baum }
14045dfa003dSMichael Baum uar_obj = mlx5_devx_alloc_uar(cdev);
14055dfa003dSMichael Baum if (uar_obj == NULL || mlx5_os_get_devx_uar_reg_addr(uar_obj) == NULL) {
14065dfa003dSMichael Baum rte_errno = errno;
14075dfa003dSMichael Baum DRV_LOG(ERR, "Failed to allocate UAR.");
14085dfa003dSMichael Baum return -1;
14095dfa003dSMichael Baum }
14105dfa003dSMichael Baum uar->obj = uar_obj;
14115dfa003dSMichael Baum uar_mmap_offset = mlx5_os_get_devx_uar_mmap_offset(uar_obj);
14125dfa003dSMichael Baum base_addr = mlx5_os_get_devx_uar_base_addr(uar_obj);
14135dfa003dSMichael Baum uar->dbnc = mlx5_db_map_type_get(uar_mmap_offset, page_size);
14145dfa003dSMichael Baum uar->bf_db.db = mlx5_os_get_devx_uar_reg_addr(uar_obj);
14155dfa003dSMichael Baum uar->cq_db.db = RTE_PTR_ADD(base_addr, MLX5_CQ_DOORBELL);
14165dfa003dSMichael Baum #ifndef RTE_ARCH_64
14175dfa003dSMichael Baum rte_spinlock_init(&uar->bf_sl);
14185dfa003dSMichael Baum rte_spinlock_init(&uar->cq_sl);
14195dfa003dSMichael Baum uar->bf_db.sl_p = &uar->bf_sl;
14205dfa003dSMichael Baum uar->cq_db.sl_p = &uar->cq_sl;
14215dfa003dSMichael Baum #endif /* RTE_ARCH_64 */
14225dfa003dSMichael Baum return 0;
14235dfa003dSMichael Baum }
14245dfa003dSMichael Baum
1425ad435d32SXueming Li RTE_PMD_EXPORT_NAME(mlx5_common_driver, __COUNTER__);
1426