Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2019 Mellanox Technologies, Ltd
3 : : */
4 : :
5 : : #include <unistd.h>
6 : : #include <string.h>
7 : : #include <stdio.h>
8 : : #include <pthread.h>
9 : :
10 : : #include <eal_export.h>
11 : : #include <rte_errno.h>
12 : : #include <rte_mempool.h>
13 : : #include <rte_class.h>
14 : : #include <rte_malloc.h>
15 : : #include <rte_eal_paging.h>
16 : :
17 : : #include "mlx5_common.h"
18 : : #include "mlx5_common_os.h"
19 : : #include "mlx5_common_mp.h"
20 : : #include "mlx5_common_log.h"
21 : : #include "mlx5_common_defs.h"
22 : : #include "mlx5_common_private.h"
23 : :
24 : : RTE_EXPORT_INTERNAL_SYMBOL(haswell_broadwell_cpu)
25 : : uint8_t haswell_broadwell_cpu;
26 : :
27 : : /* Driver type key for new device global syntax. */
28 : : #define MLX5_DRIVER_KEY "driver"
29 : :
30 : : /* Device parameter to get file descriptor for import device. */
31 : : #define MLX5_DEVICE_FD "cmd_fd"
32 : :
33 : : /* Device parameter to get PD number for import Protection Domain. */
34 : : #define MLX5_PD_HANDLE "pd_handle"
35 : :
36 : : /* Enable extending memsegs when creating a MR. */
37 : : #define MLX5_MR_EXT_MEMSEG_EN "mr_ext_memseg_en"
38 : :
39 : : /* Device parameter to configure implicit registration of mempool memory. */
40 : : #define MLX5_MR_MEMPOOL_REG_EN "mr_mempool_reg_en"
41 : :
42 : : /* The default memory allocator used in PMD. */
43 : : #define MLX5_SYS_MEM_EN "sys_mem_en"
44 : :
45 : : /* Probe optimization in PMD. */
46 : : #define MLX5_PROBE_OPT "probe_opt_en"
47 : :
48 : : /*
49 : : * Device parameter to force doorbell register mapping
50 : : * to non-cached region eliminating the extra write memory barrier.
51 : : * Deprecated, ignored (Name changed to sq_db_nc).
52 : : */
53 : : #define MLX5_TX_DB_NC "tx_db_nc"
54 : :
55 : : /*
56 : : * Device parameter to force doorbell register mapping
57 : : * to non-cached region eliminating the extra write memory barrier.
58 : : */
59 : : #define MLX5_SQ_DB_NC "sq_db_nc"
60 : :
61 : : /* In case this is an x86_64 intel processor to check if
62 : : * we should use relaxed ordering.
63 : : */
64 : : #ifdef RTE_ARCH_X86_64
65 : : /**
66 : : * This function returns processor identification and feature information
67 : : * into the registers.
68 : : *
69 : : * @param eax, ebx, ecx, edx
70 : : * Pointers to the registers that will hold cpu information.
71 : : * @param level
72 : : * The main category of information returned.
73 : : */
74 : : static inline void mlx5_cpu_id(unsigned int level,
75 : : unsigned int *eax, unsigned int *ebx,
76 : : unsigned int *ecx, unsigned int *edx)
77 : : {
78 : : #ifdef RTE_TOOLCHAIN_MSVC
79 : : int data[4];
80 : : __cpuid(data, level);
81 : : *eax = data[0];
82 : : *ebx = data[1];
83 : : *ecx = data[2];
84 : : *edx = data[3];
85 : : #else
86 : 508 : __asm__("cpuid\n\t"
87 : : : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
88 : : : "0" (level));
89 : : #endif
90 : : }
91 : : #endif
92 : :
93 [ - + ]: 254 : RTE_LOG_REGISTER_DEFAULT(mlx5_common_logtype, NOTICE)
94 : :
95 : : /* Head of list of drivers. */
96 : : static TAILQ_HEAD(mlx5_drivers, mlx5_class_driver) drivers_list =
97 : : TAILQ_HEAD_INITIALIZER(drivers_list);
98 : :
99 : : /* Head of devices. */
100 : : static TAILQ_HEAD(mlx5_devices, mlx5_common_device) devices_list =
101 : : TAILQ_HEAD_INITIALIZER(devices_list);
102 : : static pthread_mutex_t devices_list_lock;
103 : :
104 : : static const struct {
105 : : const char *name;
106 : : unsigned int drv_class;
107 : : } mlx5_classes[] = {
108 : : { .name = "vdpa", .drv_class = MLX5_CLASS_VDPA },
109 : : { .name = "eth", .drv_class = MLX5_CLASS_ETH },
110 : : /* Keep class "net" for backward compatibility. */
111 : : { .name = "net", .drv_class = MLX5_CLASS_ETH },
112 : : { .name = "regex", .drv_class = MLX5_CLASS_REGEX },
113 : : { .name = "compress", .drv_class = MLX5_CLASS_COMPRESS },
114 : : { .name = "crypto", .drv_class = MLX5_CLASS_CRYPTO },
115 : : };
116 : :
117 : : static int
118 : 0 : class_name_to_value(const char *class_name)
119 : : {
120 : : unsigned int i;
121 : :
122 [ # # ]: 0 : for (i = 0; i < RTE_DIM(mlx5_classes); i++) {
123 [ # # ]: 0 : if (strcmp(class_name, mlx5_classes[i].name) == 0)
124 : 0 : return mlx5_classes[i].drv_class;
125 : : }
126 : : return -EINVAL;
127 : : }
128 : :
129 : : static struct mlx5_class_driver *
130 : : driver_get(uint32_t class)
131 : : {
132 : : struct mlx5_class_driver *driver;
133 : :
134 [ # # ]: 0 : TAILQ_FOREACH(driver, &drivers_list, next) {
135 [ # # ]: 0 : if ((uint32_t)driver->drv_class == class)
136 : : return driver;
137 : : }
138 : : return NULL;
139 : : }
140 : :
141 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_kvargs_process)
142 : : int
143 : 0 : mlx5_kvargs_process(struct mlx5_kvargs_ctrl *mkvlist, const char *const keys[],
144 : : arg_handler_t handler, void *opaque_arg)
145 : : {
146 : : const struct rte_kvargs_pair *pair;
147 : : uint32_t i, j;
148 : :
149 : : MLX5_ASSERT(mkvlist && mkvlist->kvlist);
150 : : /* Process parameters. */
151 [ # # ]: 0 : for (i = 0; i < mkvlist->kvlist->count; i++) {
152 : : pair = &mkvlist->kvlist->pairs[i];
153 [ # # ]: 0 : for (j = 0; keys[j] != NULL; ++j) {
154 [ # # ]: 0 : if (strcmp(pair->key, keys[j]) != 0)
155 : : continue;
156 [ # # ]: 0 : if ((*handler)(pair->key, pair->value, opaque_arg) < 0)
157 : : return -1;
158 : 0 : mkvlist->is_used[i] = true;
159 : 0 : break;
160 : : }
161 : : }
162 : : return 0;
163 : : }
164 : :
165 : : /**
166 : : * Prepare a mlx5 kvargs control.
167 : : *
168 : : * @param[out] mkvlist
169 : : * Pointer to mlx5 kvargs control.
170 : : * @param[in] devargs
171 : : * The input string containing the key/value associations.
172 : : *
173 : : * @return
174 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
175 : : */
176 : : static int
177 : 0 : mlx5_kvargs_prepare(struct mlx5_kvargs_ctrl *mkvlist,
178 : : const struct rte_devargs *devargs)
179 : : {
180 : : struct rte_kvargs *kvlist;
181 : : uint32_t i;
182 : :
183 [ # # ]: 0 : if (mkvlist == NULL)
184 : : return 0;
185 : : MLX5_ASSERT(devargs != NULL && devargs->args != NULL);
186 : 0 : kvlist = rte_kvargs_parse(devargs->args, NULL);
187 [ # # ]: 0 : if (kvlist == NULL) {
188 : 0 : rte_errno = EINVAL;
189 : 0 : return -rte_errno;
190 : : }
191 : : /*
192 : : * rte_kvargs_parse enable key without value, in mlx5 PMDs we disable
193 : : * this syntax.
194 : : */
195 [ # # ]: 0 : for (i = 0; i < kvlist->count; i++) {
196 : : const struct rte_kvargs_pair *pair = &kvlist->pairs[i];
197 [ # # # # ]: 0 : if (pair->value == NULL || *(pair->value) == '\0') {
198 : 0 : DRV_LOG(ERR, "Key %s is missing value.", pair->key);
199 : 0 : rte_kvargs_free(kvlist);
200 : 0 : rte_errno = EINVAL;
201 : 0 : return -rte_errno;
202 : : }
203 : : }
204 : : /* Makes sure all devargs used array is false. */
205 : : memset(mkvlist, 0, sizeof(*mkvlist));
206 : 0 : mkvlist->kvlist = kvlist;
207 : 0 : DRV_LOG(DEBUG, "Parse successfully %u devargs.",
208 : : mkvlist->kvlist->count);
209 : 0 : return 0;
210 : : }
211 : :
212 : : /**
213 : : * Release a mlx5 kvargs control.
214 : : *
215 : : * @param[out] mkvlist
216 : : * Pointer to mlx5 kvargs control.
217 : : */
218 : : static void
219 : 0 : mlx5_kvargs_release(struct mlx5_kvargs_ctrl *mkvlist)
220 : : {
221 [ # # ]: 0 : if (mkvlist == NULL)
222 : : return;
223 : 0 : rte_kvargs_free(mkvlist->kvlist);
224 : : memset(mkvlist, 0, sizeof(*mkvlist));
225 : : }
226 : :
227 : : /**
228 : : * Validate device arguments list.
229 : : * It report about the first unknown parameter.
230 : : *
231 : : * @param[in] mkvlist
232 : : * Pointer to mlx5 kvargs control.
233 : : *
234 : : * @return
235 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
236 : : */
237 : : static int
238 : 0 : mlx5_kvargs_validate(struct mlx5_kvargs_ctrl *mkvlist)
239 : : {
240 : : uint32_t i;
241 : :
242 : : /* Secondary process should not handle devargs. */
243 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
244 : : return 0;
245 [ # # ]: 0 : if (mkvlist == NULL)
246 : : return 0;
247 [ # # ]: 0 : for (i = 0; i < mkvlist->kvlist->count; i++) {
248 [ # # ]: 0 : if (mkvlist->is_used[i] == 0) {
249 : 0 : DRV_LOG(ERR, "Key \"%s\" "
250 : : "is unknown for the provided classes.",
251 : : mkvlist->kvlist->pairs[i].key);
252 : 0 : rte_errno = EINVAL;
253 : 0 : return -rte_errno;
254 : : }
255 : : }
256 : : return 0;
257 : : }
258 : :
259 : : /**
260 : : * Verify and store value for devargs.
261 : : *
262 : : * @param[in] key
263 : : * Key argument to verify.
264 : : * @param[in] val
265 : : * Value associated with key.
266 : : * @param opaque
267 : : * User data.
268 : : *
269 : : * @return
270 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
271 : : */
272 : : static int
273 : 0 : mlx5_common_args_check_handler(const char *key, const char *val, void *opaque)
274 : : {
275 : : struct mlx5_common_dev_config *config = opaque;
276 : : signed long tmp;
277 : :
278 [ # # ]: 0 : if (strcmp(MLX5_DRIVER_KEY, key) == 0 ||
279 [ # # ]: 0 : strcmp(RTE_DEVARGS_KEY_CLASS, key) == 0)
280 : : return 0;
281 : 0 : errno = 0;
282 : 0 : tmp = strtol(val, NULL, 0);
283 [ # # ]: 0 : if (errno) {
284 : 0 : rte_errno = errno;
285 : 0 : DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val);
286 : 0 : return -rte_errno;
287 : : }
288 [ # # ]: 0 : if (strcmp(key, MLX5_TX_DB_NC) == 0)
289 : 0 : DRV_LOG(WARNING,
290 : : "%s: deprecated parameter, converted to queue_db_nc",
291 : : key);
292 [ # # ]: 0 : if (strcmp(key, MLX5_SQ_DB_NC) == 0 ||
293 [ # # ]: 0 : strcmp(key, MLX5_TX_DB_NC) == 0) {
294 : 0 : if (tmp != MLX5_SQ_DB_CACHED &&
295 [ # # ]: 0 : tmp != MLX5_SQ_DB_NCACHED &&
296 : : tmp != MLX5_SQ_DB_HEURISTIC) {
297 : 0 : DRV_LOG(ERR,
298 : : "Invalid Send Queue doorbell mapping parameter.");
299 : 0 : rte_errno = EINVAL;
300 : 0 : return -rte_errno;
301 : : }
302 : 0 : config->dbnc = tmp;
303 [ # # ]: 0 : } else if (strcmp(key, MLX5_MR_EXT_MEMSEG_EN) == 0) {
304 : 0 : config->mr_ext_memseg_en = !!tmp;
305 [ # # ]: 0 : } else if (strcmp(key, MLX5_MR_MEMPOOL_REG_EN) == 0) {
306 : 0 : config->mr_mempool_reg_en = !!tmp;
307 [ # # ]: 0 : } else if (strcmp(key, MLX5_SYS_MEM_EN) == 0) {
308 : 0 : config->sys_mem_en = !!tmp;
309 [ # # ]: 0 : } else if (strcmp(key, MLX5_DEVICE_FD) == 0) {
310 : 0 : config->device_fd = tmp;
311 [ # # ]: 0 : } else if (strcmp(key, MLX5_PD_HANDLE) == 0) {
312 : 0 : config->pd_handle = tmp;
313 [ # # ]: 0 : } else if (strcmp(key, MLX5_PROBE_OPT) == 0) {
314 : 0 : config->probe_opt = !!tmp;
315 : : }
316 : : return 0;
317 : : }
318 : :
319 : : /**
320 : : * Parse common device parameters.
321 : : *
322 : : * @param devargs
323 : : * Device arguments structure.
324 : : * @param config
325 : : * Pointer to device configuration structure.
326 : : *
327 : : * @return
328 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
329 : : */
330 : : static int
331 : 0 : mlx5_common_config_get(struct mlx5_kvargs_ctrl *mkvlist,
332 : : struct mlx5_common_dev_config *config)
333 : : {
334 : 0 : const char **params = (const char *[]){
335 : : RTE_DEVARGS_KEY_CLASS,
336 : : MLX5_DRIVER_KEY,
337 : : MLX5_TX_DB_NC,
338 : : MLX5_SQ_DB_NC,
339 : : MLX5_MR_EXT_MEMSEG_EN,
340 : : MLX5_SYS_MEM_EN,
341 : : MLX5_MR_MEMPOOL_REG_EN,
342 : : MLX5_DEVICE_FD,
343 : : MLX5_PD_HANDLE,
344 : : MLX5_PROBE_OPT,
345 : : NULL,
346 : : };
347 : : int ret = 0;
348 : :
349 : : /* Set defaults. */
350 : 0 : config->mr_ext_memseg_en = 1;
351 : 0 : config->mr_mempool_reg_en = 1;
352 : 0 : config->sys_mem_en = 0;
353 : 0 : config->probe_opt = 0;
354 : 0 : config->dbnc = MLX5_ARG_UNSET;
355 : 0 : config->device_fd = MLX5_ARG_UNSET;
356 : 0 : config->pd_handle = MLX5_ARG_UNSET;
357 [ # # ]: 0 : if (mkvlist == NULL)
358 : : return 0;
359 : : /* Process common parameters. */
360 : 0 : ret = mlx5_kvargs_process(mkvlist, params,
361 : : mlx5_common_args_check_handler, config);
362 [ # # ]: 0 : if (ret) {
363 : 0 : rte_errno = EINVAL;
364 : 0 : return -rte_errno;
365 : : }
366 : : /* Validate user arguments for remote PD and CTX if it is given. */
367 : 0 : ret = mlx5_os_remote_pd_and_ctx_validate(config);
368 [ # # ]: 0 : if (ret)
369 : : return ret;
370 : 0 : DRV_LOG(DEBUG, "mr_ext_memseg_en is %u.", config->mr_ext_memseg_en);
371 : 0 : DRV_LOG(DEBUG, "mr_mempool_reg_en is %u.", config->mr_mempool_reg_en);
372 : 0 : DRV_LOG(DEBUG, "sys_mem_en is %u.", config->sys_mem_en);
373 : 0 : DRV_LOG(DEBUG, "probe_opt_en is %u.", config->probe_opt);
374 : 0 : DRV_LOG(DEBUG, "Send Queue doorbell mapping parameter is %d.",
375 : : config->dbnc);
376 : 0 : return ret;
377 : : }
378 : :
379 : : static int
380 : 0 : devargs_class_handler(__rte_unused const char *key,
381 : : const char *class_names, void *opaque)
382 : : {
383 : : int *ret = opaque;
384 : : int class_val;
385 : : char *scratch;
386 : : char *found;
387 : 0 : char *refstr = NULL;
388 : :
389 : 0 : *ret = 0;
390 : 0 : scratch = strdup(class_names);
391 [ # # ]: 0 : if (scratch == NULL) {
392 : 0 : *ret = -ENOMEM;
393 : 0 : return *ret;
394 : : }
395 : 0 : found = strtok_r(scratch, ":", &refstr);
396 [ # # ]: 0 : if (found == NULL)
397 : : /* Empty string. */
398 : 0 : goto err;
399 : : do {
400 : : /* Extract each individual class name. Multiple
401 : : * classes can be supplied as class=net:regex:foo:bar.
402 : : */
403 : 0 : class_val = class_name_to_value(found);
404 : : /* Check if its a valid class. */
405 [ # # ]: 0 : if (class_val < 0) {
406 : 0 : *ret = -EINVAL;
407 : 0 : goto err;
408 : : }
409 : 0 : *ret |= class_val;
410 : 0 : found = strtok_r(NULL, ":", &refstr);
411 [ # # ]: 0 : } while (found != NULL);
412 : 0 : err:
413 : 0 : free(scratch);
414 [ # # ]: 0 : if (*ret < 0)
415 : 0 : DRV_LOG(ERR, "Invalid mlx5 class options: %s.\n", class_names);
416 : 0 : return *ret;
417 : : }
418 : :
419 : : static int
420 : 0 : parse_class_options(const struct rte_devargs *devargs,
421 : : struct mlx5_kvargs_ctrl *mkvlist)
422 : : {
423 : 0 : int ret = 0;
424 : :
425 [ # # ]: 0 : if (mkvlist == NULL)
426 : : return 0;
427 : : MLX5_ASSERT(devargs != NULL);
428 [ # # # # ]: 0 : if (devargs->cls != NULL && devargs->cls->name != NULL)
429 : : /* Global syntax, only one class type. */
430 : 0 : return class_name_to_value(devargs->cls->name);
431 : : /* Legacy devargs support multiple classes. */
432 : 0 : rte_kvargs_process(mkvlist->kvlist, RTE_DEVARGS_KEY_CLASS,
433 : : devargs_class_handler, &ret);
434 : 0 : return ret;
435 : : }
436 : :
437 : : static const unsigned int mlx5_class_invalid_combinations[] = {
438 : : MLX5_CLASS_ETH | MLX5_CLASS_VDPA,
439 : : /* New class combination should be added here. */
440 : : };
441 : :
442 : : static int
443 : : is_valid_class_combination(uint32_t user_classes)
444 : : {
445 : : unsigned int i;
446 : :
447 : : /* Verify if user specified unsupported combination. */
448 : : for (i = 0; i < RTE_DIM(mlx5_class_invalid_combinations); i++) {
449 : 0 : if ((mlx5_class_invalid_combinations[i] & user_classes) ==
450 : : mlx5_class_invalid_combinations[i])
451 : : return -EINVAL;
452 : : }
453 : : /* Not found any invalid class combination. */
454 : : return 0;
455 : : }
456 : :
457 : : static bool
458 : 0 : mlx5_bus_match(const struct mlx5_class_driver *drv,
459 : : const struct rte_device *dev)
460 : : {
461 [ # # ]: 0 : if (mlx5_dev_is_pci(dev))
462 : 0 : return mlx5_dev_pci_match(drv, dev);
463 : : return true;
464 : : }
465 : :
466 : : static struct mlx5_common_device *
467 : : to_mlx5_device(const struct rte_device *rte_dev)
468 : : {
469 : : struct mlx5_common_device *cdev;
470 : :
471 [ # # # # : 0 : TAILQ_FOREACH(cdev, &devices_list, next) {
# # # # ]
472 [ # # # # : 0 : if (rte_dev == cdev->dev)
# # # # ]
473 : : return cdev;
474 : : }
475 : : return NULL;
476 : : }
477 : :
478 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_dev_to_pci_str)
479 : : int
480 : 0 : mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size)
481 : : {
482 : 0 : struct rte_pci_addr pci_addr = { 0 };
483 : : int ret;
484 : :
485 [ # # ]: 0 : if (mlx5_dev_is_pci(dev)) {
486 : : /* Input might be <BDF>, format PCI address to <DBDF>. */
487 : 0 : ret = rte_pci_addr_parse(dev->name, &pci_addr);
488 [ # # ]: 0 : if (ret != 0)
489 : : return -ENODEV;
490 : 0 : rte_pci_device_name(&pci_addr, addr, size);
491 : 0 : return 0;
492 : : }
493 : : #ifdef RTE_EXEC_ENV_LINUX
494 : 0 : return mlx5_auxiliary_get_pci_str(RTE_DEV_TO_AUXILIARY_CONST(dev),
495 : : addr, size);
496 : : #else
497 : : rte_errno = ENODEV;
498 : : return -rte_errno;
499 : : #endif
500 : : }
501 : :
502 : : /**
503 : : * Register the mempool for the protection domain.
504 : : *
505 : : * @param cdev
506 : : * Pointer to the mlx5 common device.
507 : : * @param mp
508 : : * Mempool being registered.
509 : : *
510 : : * @return
511 : : * 0 on success, (-1) on failure and rte_errno is set.
512 : : */
513 : : static int
514 : : mlx5_dev_mempool_register(struct mlx5_common_device *cdev,
515 : : struct rte_mempool *mp, bool is_extmem)
516 : : {
517 : 0 : return mlx5_mr_mempool_register(cdev, mp, is_extmem);
518 : : }
519 : :
520 : : /**
521 : : * Unregister the mempool from the protection domain.
522 : : *
523 : : * @param cdev
524 : : * Pointer to the mlx5 common device.
525 : : * @param mp
526 : : * Mempool being unregistered.
527 : : */
528 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_dev_mempool_unregister)
529 : : void
530 : 0 : mlx5_dev_mempool_unregister(struct mlx5_common_device *cdev,
531 : : struct rte_mempool *mp)
532 : : {
533 [ # # ]: 0 : if (mlx5_mr_mempool_unregister(cdev, mp) < 0)
534 : 0 : DRV_LOG(WARNING, "Failed to unregister mempool %s for PD %p: %s",
535 : : mp->name, cdev->pd, rte_strerror(rte_errno));
536 : 0 : }
537 : :
538 : : /**
539 : : * rte_mempool_walk() callback to register mempools for the protection domain.
540 : : *
541 : : * @param mp
542 : : * The mempool being walked.
543 : : * @param arg
544 : : * Pointer to the device shared context.
545 : : */
546 : : static void
547 : 0 : mlx5_dev_mempool_register_cb(struct rte_mempool *mp, void *arg)
548 : : {
549 : : struct mlx5_common_device *cdev = arg;
550 : : int ret;
551 : :
552 : : ret = mlx5_dev_mempool_register(cdev, mp, false);
553 [ # # # # ]: 0 : if (ret < 0 && rte_errno != EEXIST)
554 : 0 : DRV_LOG(ERR,
555 : : "Failed to register existing mempool %s for PD %p: %s",
556 : : mp->name, cdev->pd, rte_strerror(rte_errno));
557 : 0 : }
558 : :
559 : : /**
560 : : * rte_mempool_walk() callback to unregister mempools
561 : : * from the protection domain.
562 : : *
563 : : * @param mp
564 : : * The mempool being walked.
565 : : * @param arg
566 : : * Pointer to the device shared context.
567 : : */
568 : : static void
569 : 0 : mlx5_dev_mempool_unregister_cb(struct rte_mempool *mp, void *arg)
570 : : {
571 : 0 : mlx5_dev_mempool_unregister((struct mlx5_common_device *)arg, mp);
572 : 0 : }
573 : :
574 : : /**
575 : : * Mempool life cycle callback for mlx5 common devices.
576 : : *
577 : : * @param event
578 : : * Mempool life cycle event.
579 : : * @param mp
580 : : * Associated mempool.
581 : : * @param arg
582 : : * Pointer to a device shared context.
583 : : */
584 : : static void
585 : 0 : mlx5_dev_mempool_event_cb(enum rte_mempool_event event, struct rte_mempool *mp,
586 : : void *arg)
587 : : {
588 : : struct mlx5_common_device *cdev = arg;
589 : :
590 [ # # # ]: 0 : switch (event) {
591 : : case RTE_MEMPOOL_EVENT_READY:
592 [ # # ]: 0 : if (mlx5_dev_mempool_register(cdev, mp, false) < 0)
593 : 0 : DRV_LOG(ERR,
594 : : "Failed to register new mempool %s for PD %p: %s",
595 : : mp->name, cdev->pd, rte_strerror(rte_errno));
596 : : break;
597 : 0 : case RTE_MEMPOOL_EVENT_DESTROY:
598 : 0 : mlx5_dev_mempool_unregister(cdev, mp);
599 : 0 : break;
600 : : }
601 : 0 : }
602 : :
603 : : /**
604 : : * Primary and secondary processes share the `cdev` pointer.
605 : : * Callbacks addresses are local in each process.
606 : : * Therefore, each process can register private callbacks.
607 : : */
608 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_dev_mempool_subscribe)
609 : : int
610 : 0 : mlx5_dev_mempool_subscribe(struct mlx5_common_device *cdev)
611 : : {
612 : : int ret = 0;
613 : :
614 [ # # ]: 0 : if (!cdev->config.mr_mempool_reg_en)
615 : : return 0;
616 : 0 : rte_rwlock_write_lock(&cdev->mr_scache.mprwlock);
617 : : /* Callback for this device may be already registered. */
618 : 0 : ret = rte_mempool_event_callback_register(mlx5_dev_mempool_event_cb,
619 : : cdev);
620 : : /* Register mempools only once for this device. */
621 [ # # # # ]: 0 : if (ret == 0 && rte_eal_process_type() == RTE_PROC_PRIMARY) {
622 : 0 : rte_mempool_walk(mlx5_dev_mempool_register_cb, cdev);
623 : 0 : goto exit;
624 : : }
625 [ # # # # ]: 0 : if (ret != 0 && rte_errno == EEXIST)
626 : : ret = 0;
627 : 0 : exit:
628 : : rte_rwlock_write_unlock(&cdev->mr_scache.mprwlock);
629 : 0 : return ret;
630 : : }
631 : :
632 : : static void
633 : 0 : mlx5_dev_mempool_unsubscribe(struct mlx5_common_device *cdev)
634 : : {
635 : : int ret;
636 : :
637 : : MLX5_ASSERT(cdev->dev != NULL);
638 [ # # ]: 0 : if (!cdev->config.mr_mempool_reg_en)
639 : : return;
640 : : /* Stop watching for mempool events and unregister all mempools. */
641 : 0 : ret = rte_mempool_event_callback_unregister(mlx5_dev_mempool_event_cb,
642 : : cdev);
643 [ # # ]: 0 : if (ret == 0)
644 : 0 : rte_mempool_walk(mlx5_dev_mempool_unregister_cb, cdev);
645 : : }
646 : :
647 : : /**
648 : : * Callback for memory event.
649 : : *
650 : : * @param event_type
651 : : * Memory event type.
652 : : * @param addr
653 : : * Address of memory.
654 : : * @param len
655 : : * Size of memory.
656 : : */
657 : : static void
658 : 0 : mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
659 : : size_t len, void *arg __rte_unused)
660 : : {
661 : : struct mlx5_common_device *cdev;
662 : :
663 : : /* Must be called from the primary process. */
664 : : MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
665 [ # # ]: 0 : switch (event_type) {
666 : 0 : case RTE_MEM_EVENT_FREE:
667 : 0 : pthread_mutex_lock(&devices_list_lock);
668 : : /* Iterate all the existing mlx5 devices. */
669 [ # # ]: 0 : TAILQ_FOREACH(cdev, &devices_list, next)
670 [ # # ]: 0 : mlx5_free_mr_by_addr(&cdev->mr_scache,
671 : : mlx5_os_get_ctx_device_name
672 : : (cdev->ctx),
673 : : addr, len);
674 : 0 : pthread_mutex_unlock(&devices_list_lock);
675 : 0 : break;
676 : : case RTE_MEM_EVENT_ALLOC:
677 : : default:
678 : : break;
679 : : }
680 : 0 : }
681 : :
682 : : /**
683 : : * Uninitialize all HW global of device context.
684 : : *
685 : : * @param cdev
686 : : * Pointer to mlx5 device structure.
687 : : *
688 : : * @return
689 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
690 : : */
691 : : static void
692 : 0 : mlx5_dev_hw_global_release(struct mlx5_common_device *cdev)
693 : : {
694 [ # # ]: 0 : if (cdev->pd != NULL) {
695 : 0 : claim_zero(mlx5_os_pd_release(cdev));
696 : 0 : cdev->pd = NULL;
697 : : }
698 [ # # ]: 0 : if (cdev->ctx != NULL) {
699 : 0 : claim_zero(mlx5_glue->close_device(cdev->ctx));
700 : 0 : cdev->ctx = NULL;
701 : : }
702 : 0 : }
703 : :
704 : : /**
705 : : * Initialize all HW global of device context.
706 : : *
707 : : * @param cdev
708 : : * Pointer to mlx5 device structure.
709 : : * @param classes
710 : : * Chosen classes come from user device arguments.
711 : : *
712 : : * @return
713 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
714 : : */
715 : : static int
716 : 0 : mlx5_dev_hw_global_prepare(struct mlx5_common_device *cdev, uint32_t classes)
717 : : {
718 : : int ret;
719 : :
720 : : /* Create context device */
721 : 0 : ret = mlx5_os_open_device(cdev, classes);
722 [ # # ]: 0 : if (ret < 0)
723 : : return ret;
724 : : /*
725 : : * When CTX is created by Verbs, query HCA attribute is unsupported.
726 : : * When CTX is imported, we cannot know if it is created by DevX or
727 : : * Verbs. So, we use query HCA attribute function to check it.
728 : : */
729 [ # # # # ]: 0 : if (cdev->config.devx || cdev->config.device_fd != MLX5_ARG_UNSET) {
730 : : /* Query HCA attributes. */
731 : 0 : ret = mlx5_devx_cmd_query_hca_attr(cdev->ctx,
732 : : &cdev->config.hca_attr);
733 [ # # ]: 0 : if (ret) {
734 : 0 : DRV_LOG(ERR, "Unable to read HCA caps in DevX mode.");
735 : 0 : rte_errno = ENOTSUP;
736 : 0 : goto error;
737 : : }
738 : 0 : cdev->config.devx = 1;
739 : : }
740 [ # # ]: 0 : DRV_LOG(DEBUG, "DevX is %ssupported.", cdev->config.devx ? "" : "NOT ");
741 : : /* Prepare Protection Domain object and extract its pdn. */
742 : 0 : ret = mlx5_os_pd_prepare(cdev);
743 [ # # ]: 0 : if (ret)
744 : 0 : goto error;
745 : : return 0;
746 : 0 : error:
747 : 0 : mlx5_dev_hw_global_release(cdev);
748 : 0 : return ret;
749 : : }
750 : :
751 : : static void
752 : 0 : mlx5_common_dev_release(struct mlx5_common_device *cdev)
753 : : {
754 : 0 : pthread_mutex_lock(&devices_list_lock);
755 [ # # ]: 0 : TAILQ_REMOVE(&devices_list, cdev, next);
756 : 0 : pthread_mutex_unlock(&devices_list_lock);
757 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
758 [ # # ]: 0 : if (TAILQ_EMPTY(&devices_list))
759 : 0 : rte_mem_event_callback_unregister("MLX5_MEM_EVENT_CB",
760 : : NULL);
761 [ # # ]: 0 : if (cdev->dev_info.port_info != NULL) {
762 : 0 : mlx5_free(cdev->dev_info.port_info);
763 : 0 : cdev->dev_info.port_info = NULL;
764 : : }
765 : 0 : cdev->dev_info.port_num = 0;
766 : 0 : mlx5_dev_mempool_unsubscribe(cdev);
767 : 0 : mlx5_mr_release_cache(&cdev->mr_scache);
768 : 0 : mlx5_dev_hw_global_release(cdev);
769 : : }
770 : 0 : rte_free(cdev);
771 : 0 : }
772 : :
773 : : static struct mlx5_common_device *
774 : 0 : mlx5_common_dev_create(struct rte_device *eal_dev, uint32_t classes,
775 : : struct mlx5_kvargs_ctrl *mkvlist)
776 : : {
777 : : struct mlx5_common_device *cdev;
778 : : int ret;
779 : :
780 : 0 : cdev = rte_zmalloc("mlx5_common_device", sizeof(*cdev), 0);
781 [ # # ]: 0 : if (!cdev) {
782 : 0 : DRV_LOG(ERR, "Device allocation failure.");
783 : 0 : rte_errno = ENOMEM;
784 : 0 : return NULL;
785 : : }
786 : 0 : cdev->dev = eal_dev;
787 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
788 : 0 : goto exit;
789 : : /* Parse device parameters. */
790 : 0 : ret = mlx5_common_config_get(mkvlist, &cdev->config);
791 [ # # ]: 0 : if (ret < 0) {
792 : 0 : DRV_LOG(ERR, "Failed to process device arguments: %s",
793 : : strerror(rte_errno));
794 : 0 : rte_free(cdev);
795 : 0 : return NULL;
796 : : }
797 : 0 : mlx5_malloc_mem_select(cdev->config.sys_mem_en);
798 : : /* Initialize all HW global of device context. */
799 : 0 : ret = mlx5_dev_hw_global_prepare(cdev, classes);
800 [ # # ]: 0 : if (ret) {
801 : 0 : DRV_LOG(ERR, "Failed to initialize device context.");
802 : 0 : rte_free(cdev);
803 : 0 : return NULL;
804 : : }
805 : : /* Initialize global MR cache resources and update its functions. */
806 : 0 : ret = mlx5_mr_create_cache(&cdev->mr_scache, eal_dev->numa_node);
807 [ # # ]: 0 : if (ret) {
808 : 0 : DRV_LOG(ERR, "Failed to initialize global MR share cache.");
809 : 0 : mlx5_dev_hw_global_release(cdev);
810 : 0 : rte_free(cdev);
811 : 0 : return NULL;
812 : : }
813 : : /* Register callback function for global shared MR cache management. */
814 [ # # ]: 0 : if (TAILQ_EMPTY(&devices_list))
815 : 0 : rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
816 : : mlx5_mr_mem_event_cb, NULL);
817 : 0 : cdev->dev_info.probe_opt = cdev->config.probe_opt;
818 : 0 : exit:
819 : 0 : pthread_mutex_lock(&devices_list_lock);
820 [ # # ]: 0 : TAILQ_INSERT_HEAD(&devices_list, cdev, next);
821 : 0 : pthread_mutex_unlock(&devices_list_lock);
822 : 0 : return cdev;
823 : : }
824 : :
825 : : /**
826 : : * Validate common devargs when probing again.
827 : : *
828 : : * When common device probing again, it cannot change its configurations.
829 : : * If user ask non compatible configurations in devargs, it is error.
830 : : * This function checks the match between:
831 : : * - Common device configurations requested by probe again devargs.
832 : : * - Existing common device configurations.
833 : : *
834 : : * @param cdev
835 : : * Pointer to mlx5 device structure.
836 : : * @param mkvlist
837 : : * Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
838 : : *
839 : : * @return
840 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
841 : : */
842 : : static int
843 : 0 : mlx5_common_probe_again_args_validate(struct mlx5_common_device *cdev,
844 : : struct mlx5_kvargs_ctrl *mkvlist)
845 : : {
846 : : struct mlx5_common_dev_config *config;
847 : : int ret;
848 : :
849 : : /* Secondary process should not handle devargs. */
850 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
851 : : return 0;
852 : : /* Probe again doesn't have to generate devargs. */
853 [ # # ]: 0 : if (mkvlist == NULL)
854 : : return 0;
855 : 0 : config = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
856 : : sizeof(struct mlx5_common_dev_config),
857 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
858 [ # # ]: 0 : if (config == NULL) {
859 : 0 : rte_errno = -ENOMEM;
860 : 0 : return -rte_errno;
861 : : }
862 : : /*
863 : : * Creates a temporary common configure structure according to new
864 : : * devargs attached in probing again.
865 : : */
866 : 0 : ret = mlx5_common_config_get(mkvlist, config);
867 [ # # ]: 0 : if (ret) {
868 : 0 : DRV_LOG(ERR, "Failed to process device configure: %s",
869 : : strerror(rte_errno));
870 : 0 : mlx5_free(config);
871 : 0 : return ret;
872 : : }
873 : : /*
874 : : * Checks the match between the temporary structure and the existing
875 : : * common device structure.
876 : : */
877 [ # # ]: 0 : if (cdev->config.mr_ext_memseg_en != config->mr_ext_memseg_en) {
878 : 0 : DRV_LOG(ERR, "\"" MLX5_MR_EXT_MEMSEG_EN "\" "
879 : : "configuration mismatch for device %s.",
880 : : cdev->dev->name);
881 : 0 : goto error;
882 : : }
883 [ # # ]: 0 : if (cdev->config.mr_mempool_reg_en != config->mr_mempool_reg_en) {
884 : 0 : DRV_LOG(ERR, "\"" MLX5_MR_MEMPOOL_REG_EN "\" "
885 : : "configuration mismatch for device %s.",
886 : : cdev->dev->name);
887 : 0 : goto error;
888 : : }
889 [ # # ]: 0 : if (cdev->config.device_fd != config->device_fd) {
890 : 0 : DRV_LOG(ERR, "\"" MLX5_DEVICE_FD "\" "
891 : : "configuration mismatch for device %s.",
892 : : cdev->dev->name);
893 : 0 : goto error;
894 : : }
895 [ # # ]: 0 : if (cdev->config.pd_handle != config->pd_handle) {
896 : 0 : DRV_LOG(ERR, "\"" MLX5_PD_HANDLE "\" "
897 : : "configuration mismatch for device %s.",
898 : : cdev->dev->name);
899 : 0 : goto error;
900 : : }
901 [ # # ]: 0 : if (cdev->config.sys_mem_en != config->sys_mem_en) {
902 : 0 : DRV_LOG(ERR, "\"" MLX5_SYS_MEM_EN "\" "
903 : : "configuration mismatch for device %s.",
904 : : cdev->dev->name);
905 : 0 : goto error;
906 : : }
907 [ # # ]: 0 : if (cdev->config.probe_opt != config->probe_opt) {
908 : 0 : DRV_LOG(ERR, "\"" MLX5_PROBE_OPT"\" "
909 : : "configuration mismatch for device %s.",
910 : : cdev->dev->name);
911 : 0 : goto error;
912 : : }
913 [ # # ]: 0 : if (cdev->config.dbnc != config->dbnc) {
914 : 0 : DRV_LOG(ERR, "\"" MLX5_SQ_DB_NC "\" "
915 : : "configuration mismatch for device %s.",
916 : : cdev->dev->name);
917 : 0 : goto error;
918 : : }
919 : 0 : mlx5_free(config);
920 : 0 : return 0;
921 : 0 : error:
922 : 0 : mlx5_free(config);
923 : 0 : rte_errno = EINVAL;
924 : 0 : return -rte_errno;
925 : : }
926 : :
927 : : static int
928 : 0 : drivers_remove(struct mlx5_common_device *cdev, uint32_t enabled_classes)
929 : : {
930 : : struct mlx5_class_driver *driver;
931 : : int local_ret = -ENODEV;
932 : : unsigned int i = 0;
933 : : int ret = 0;
934 : :
935 [ # # ]: 0 : while (enabled_classes) {
936 : 0 : driver = driver_get(RTE_BIT64(i));
937 [ # # ]: 0 : if (driver != NULL) {
938 : 0 : local_ret = driver->remove(cdev);
939 [ # # ]: 0 : if (local_ret == 0)
940 : 0 : cdev->classes_loaded &= ~RTE_BIT64(i);
941 [ # # ]: 0 : else if (ret == 0)
942 : : ret = local_ret;
943 : : }
944 : 0 : enabled_classes &= ~RTE_BIT64(i);
945 : 0 : i++;
946 : : }
947 [ # # ]: 0 : if (local_ret != 0 && ret == 0)
948 : : ret = local_ret;
949 : 0 : return ret;
950 : : }
951 : :
952 : : static int
953 : 0 : drivers_probe(struct mlx5_common_device *cdev, uint32_t user_classes,
954 : : struct mlx5_kvargs_ctrl *mkvlist)
955 : : {
956 : : struct mlx5_class_driver *driver;
957 : : uint32_t enabled_classes = 0;
958 : : bool already_loaded;
959 : : int ret = -EINVAL;
960 : :
961 [ # # ]: 0 : TAILQ_FOREACH(driver, &drivers_list, next) {
962 [ # # ]: 0 : if ((driver->drv_class & user_classes) == 0)
963 : 0 : continue;
964 [ # # ]: 0 : if (!mlx5_bus_match(driver, cdev->dev))
965 : 0 : continue;
966 : 0 : already_loaded = cdev->classes_loaded & driver->drv_class;
967 [ # # # # ]: 0 : if (already_loaded && driver->probe_again == 0) {
968 : 0 : DRV_LOG(ERR, "Device %s is already probed",
969 : : cdev->dev->name);
970 : : ret = -EEXIST;
971 : 0 : goto probe_err;
972 : : }
973 : 0 : ret = driver->probe(cdev, mkvlist);
974 [ # # ]: 0 : if (ret < 0) {
975 : 0 : DRV_LOG(ERR, "Failed to load driver %s",
976 : : driver->name);
977 : 0 : goto probe_err;
978 : : }
979 : 0 : enabled_classes |= driver->drv_class;
980 : : }
981 [ # # ]: 0 : if (!ret) {
982 : 0 : cdev->classes_loaded |= enabled_classes;
983 : 0 : return 0;
984 : : }
985 : 0 : probe_err:
986 : : /*
987 : : * Need to remove only drivers which were not probed before this probe
988 : : * instance, but have already been probed before this failure.
989 : : */
990 : 0 : enabled_classes &= ~cdev->classes_loaded;
991 : 0 : drivers_remove(cdev, enabled_classes);
992 : 0 : return ret;
993 : : }
994 : :
995 : : int
996 : 0 : mlx5_common_dev_probe(struct rte_device *eal_dev)
997 : : {
998 : : struct mlx5_common_device *cdev;
999 : : struct mlx5_kvargs_ctrl mkvlist;
1000 : : struct mlx5_kvargs_ctrl *mkvlist_p = NULL;
1001 : : uint32_t classes = 0;
1002 : : bool new_device = false;
1003 : : int ret;
1004 : :
1005 : 0 : DRV_LOG(INFO, "probe device \"%s\".", eal_dev->name);
1006 [ # # # # ]: 0 : if (eal_dev->devargs != NULL && eal_dev->devargs->args != NULL)
1007 : : mkvlist_p = &mkvlist;
1008 : 0 : ret = mlx5_kvargs_prepare(mkvlist_p, eal_dev->devargs);
1009 [ # # ]: 0 : if (ret < 0) {
1010 : 0 : DRV_LOG(ERR, "Unsupported device arguments: %s",
1011 : : eal_dev->devargs->args);
1012 : 0 : return ret;
1013 : : }
1014 : 0 : ret = parse_class_options(eal_dev->devargs, mkvlist_p);
1015 [ # # ]: 0 : if (ret < 0) {
1016 : 0 : DRV_LOG(ERR, "Unsupported mlx5 class type: %s",
1017 : : eal_dev->devargs->args);
1018 : 0 : goto class_err;
1019 : : }
1020 : 0 : classes = ret;
1021 : : if (classes == 0)
1022 : : /* Default to net class. */
1023 : : classes = MLX5_CLASS_ETH;
1024 : : /*
1025 : : * MLX5 common driver supports probing again in two scenarios:
1026 : : * - Add new driver under existing common device (regardless of the
1027 : : * driver's own support in probing again).
1028 : : * - Transfer the probing again support of the drivers themselves.
1029 : : *
1030 : : * In both scenarios it uses in the existing device. here it looks for
1031 : : * device that match to rte device, if it exists, the request classes
1032 : : * were probed with this device.
1033 : : */
1034 : : cdev = to_mlx5_device(eal_dev);
1035 [ # # ]: 0 : if (!cdev) {
1036 : : /* It isn't probing again, creates a new device. */
1037 : 0 : cdev = mlx5_common_dev_create(eal_dev, classes, mkvlist_p);
1038 [ # # ]: 0 : if (!cdev) {
1039 : : ret = -ENOMEM;
1040 : 0 : goto class_err;
1041 : : }
1042 : : new_device = true;
1043 : : } else {
1044 : : /* It is probing again, validate common devargs match. */
1045 : 0 : ret = mlx5_common_probe_again_args_validate(cdev, mkvlist_p);
1046 [ # # ]: 0 : if (ret) {
1047 : 0 : DRV_LOG(ERR,
1048 : : "Probe again parameters aren't compatible : %s",
1049 : : strerror(rte_errno));
1050 : 0 : goto class_err;
1051 : : }
1052 : : }
1053 : : /*
1054 : : * Validate combination here.
1055 : : * For new device, the classes_loaded field is 0 and it check only
1056 : : * the classes given as user device arguments.
1057 : : */
1058 [ # # ]: 0 : ret = is_valid_class_combination(classes | cdev->classes_loaded);
1059 [ # # ]: 0 : if (ret != 0) {
1060 : 0 : DRV_LOG(ERR, "Unsupported mlx5 classes combination.");
1061 : 0 : goto class_err;
1062 : : }
1063 : 0 : ret = drivers_probe(cdev, classes, mkvlist_p);
1064 [ # # ]: 0 : if (ret)
1065 : 0 : goto class_err;
1066 : : /*
1067 : : * Validate that all devargs have been used, unused key -> unknown Key.
1068 : : * When probe again validate is failed, the added drivers aren't removed
1069 : : * here but when device is released.
1070 : : */
1071 : 0 : ret = mlx5_kvargs_validate(mkvlist_p);
1072 [ # # ]: 0 : if (ret)
1073 : 0 : goto class_err;
1074 : 0 : mlx5_kvargs_release(mkvlist_p);
1075 : 0 : return 0;
1076 : 0 : class_err:
1077 [ # # ]: 0 : if (new_device) {
1078 : : /*
1079 : : * For new device, classes_loaded is always 0 before
1080 : : * drivers_probe function.
1081 : : */
1082 [ # # ]: 0 : if (cdev->classes_loaded)
1083 : 0 : drivers_remove(cdev, cdev->classes_loaded);
1084 : 0 : mlx5_common_dev_release(cdev);
1085 : : }
1086 : 0 : mlx5_kvargs_release(mkvlist_p);
1087 : 0 : return ret;
1088 : : }
1089 : :
1090 : : int
1091 : 0 : mlx5_common_dev_remove(struct rte_device *eal_dev)
1092 : : {
1093 : : struct mlx5_common_device *cdev;
1094 : : int ret;
1095 : :
1096 : : cdev = to_mlx5_device(eal_dev);
1097 [ # # ]: 0 : if (!cdev)
1098 : : return -ENODEV;
1099 : : /* Matching device found, cleanup and unload drivers. */
1100 : 0 : ret = drivers_remove(cdev, cdev->classes_loaded);
1101 [ # # ]: 0 : if (ret == 0)
1102 : 0 : mlx5_common_dev_release(cdev);
1103 : : return ret;
1104 : : }
1105 : :
1106 : : /**
1107 : : * Callback to DMA map external memory to a device.
1108 : : *
1109 : : * @param rte_dev
1110 : : * Pointer to the generic device.
1111 : : * @param addr
1112 : : * Starting virtual address of memory to be mapped.
1113 : : * @param iova
1114 : : * Starting IOVA address of memory to be mapped.
1115 : : * @param len
1116 : : * Length of memory segment being mapped.
1117 : : *
1118 : : * @return
1119 : : * 0 on success, negative value on error.
1120 : : */
1121 : : int
1122 : 0 : mlx5_common_dev_dma_map(struct rte_device *rte_dev, void *addr,
1123 : : uint64_t iova __rte_unused, size_t len)
1124 : : {
1125 : : struct mlx5_common_device *dev;
1126 : : struct mlx5_mr_btree *bt;
1127 : : struct mlx5_mr *mr;
1128 : :
1129 : : dev = to_mlx5_device(rte_dev);
1130 [ # # ]: 0 : if (!dev) {
1131 : 0 : DRV_LOG(WARNING,
1132 : : "Unable to find matching mlx5 device to device %s",
1133 : : rte_dev->name);
1134 : 0 : rte_errno = ENODEV;
1135 : 0 : return -1;
1136 : : }
1137 : 0 : mr = mlx5_create_mr_ext(dev->pd, (uintptr_t)addr, len,
1138 : : SOCKET_ID_ANY, dev->mr_scache.reg_mr_cb);
1139 [ # # ]: 0 : if (!mr) {
1140 : 0 : DRV_LOG(WARNING, "Device %s unable to DMA map", rte_dev->name);
1141 : 0 : rte_errno = EINVAL;
1142 : 0 : return -1;
1143 : : }
1144 : 0 : try_insert:
1145 : 0 : rte_rwlock_write_lock(&dev->mr_scache.rwlock);
1146 : : bt = &dev->mr_scache.cache;
1147 [ # # ]: 0 : if (bt->len == bt->size) {
1148 : : uint32_t size;
1149 : : int ret;
1150 : :
1151 : 0 : size = bt->size + 1;
1152 : : MLX5_ASSERT(size > bt->size);
1153 : : /*
1154 : : * Avoid deadlock (numbers show the sequence of events):
1155 : : * mlx5_mr_create_primary():
1156 : : * 1) take EAL memory lock
1157 : : * 3) take MR lock
1158 : : * this function:
1159 : : * 2) take MR lock
1160 : : * 4) take EAL memory lock while allocating the new cache
1161 : : * Releasing the MR lock before step 4
1162 : : * allows another thread to execute step 3.
1163 : : */
1164 : : rte_rwlock_write_unlock(&dev->mr_scache.rwlock);
1165 : 0 : ret = mlx5_mr_expand_cache(&dev->mr_scache, size,
1166 : : rte_dev->numa_node);
1167 [ # # ]: 0 : if (ret < 0) {
1168 : 0 : mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb);
1169 : 0 : rte_errno = ret;
1170 : 0 : return -1;
1171 : : }
1172 : 0 : goto try_insert;
1173 : : }
1174 [ # # ]: 0 : LIST_INSERT_HEAD(&dev->mr_scache.mr_list, mr, mr);
1175 : : /* Insert to the global cache table. */
1176 : 0 : mlx5_mr_insert_cache(&dev->mr_scache, mr);
1177 : : rte_rwlock_write_unlock(&dev->mr_scache.rwlock);
1178 : 0 : return 0;
1179 : : }
1180 : :
1181 : : /**
1182 : : * Callback to DMA unmap external memory to a device.
1183 : : *
1184 : : * @param rte_dev
1185 : : * Pointer to the generic device.
1186 : : * @param addr
1187 : : * Starting virtual address of memory to be unmapped.
1188 : : * @param iova
1189 : : * Starting IOVA address of memory to be unmapped.
1190 : : * @param len
1191 : : * Length of memory segment being unmapped.
1192 : : *
1193 : : * @return
1194 : : * 0 on success, negative value on error.
1195 : : */
1196 : : int
1197 : 0 : mlx5_common_dev_dma_unmap(struct rte_device *rte_dev, void *addr,
1198 : : uint64_t iova __rte_unused, size_t len __rte_unused)
1199 : : {
1200 : : struct mlx5_common_device *dev;
1201 : : struct mr_cache_entry entry;
1202 : : struct mlx5_mr *mr;
1203 : :
1204 : : dev = to_mlx5_device(rte_dev);
1205 [ # # ]: 0 : if (!dev) {
1206 : 0 : DRV_LOG(WARNING,
1207 : : "Unable to find matching mlx5 device to device %s.",
1208 : : rte_dev->name);
1209 : 0 : rte_errno = ENODEV;
1210 : 0 : return -1;
1211 : : }
1212 : 0 : rte_rwlock_read_lock(&dev->mr_scache.rwlock);
1213 : 0 : mr = mlx5_mr_lookup_list(&dev->mr_scache, &entry, (uintptr_t)addr);
1214 [ # # ]: 0 : if (!mr) {
1215 : : rte_rwlock_read_unlock(&dev->mr_scache.rwlock);
1216 : 0 : DRV_LOG(WARNING,
1217 : : "Address 0x%" PRIxPTR " wasn't registered to device %s",
1218 : : (uintptr_t)addr, rte_dev->name);
1219 : 0 : rte_errno = EINVAL;
1220 : 0 : return -1;
1221 : : }
1222 [ # # ]: 0 : LIST_REMOVE(mr, mr);
1223 : 0 : DRV_LOG(DEBUG, "MR(%p) is removed from list.", (void *)mr);
1224 : 0 : mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb);
1225 : 0 : mlx5_mr_rebuild_cache(&dev->mr_scache);
1226 : : /*
1227 : : * No explicit wmb is needed after updating dev_gen due to
1228 : : * store-release ordering in unlock that provides the
1229 : : * implicit barrier at the software visible level.
1230 : : */
1231 : 0 : ++dev->mr_scache.dev_gen;
1232 : 0 : DRV_LOG(DEBUG, "Broadcasting local cache flush, gen=%d.",
1233 : : dev->mr_scache.dev_gen);
1234 : : rte_rwlock_read_unlock(&dev->mr_scache.rwlock);
1235 : 0 : return 0;
1236 : : }
1237 : :
1238 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_class_driver_register)
1239 : : void
1240 : 1270 : mlx5_class_driver_register(struct mlx5_class_driver *driver)
1241 : : {
1242 : 1270 : mlx5_common_driver_on_register_pci(driver);
1243 : 1270 : TAILQ_INSERT_TAIL(&drivers_list, driver, next);
1244 : 1270 : }
1245 : :
1246 : : static void mlx5_common_driver_init(void)
1247 : : {
1248 : 254 : mlx5_common_pci_init();
1249 : : #ifdef RTE_EXEC_ENV_LINUX
1250 : 254 : mlx5_common_auxiliary_init();
1251 : : #endif
1252 : : }
1253 : :
1254 : : static bool mlx5_common_initialized;
1255 : :
1256 : : /**
1257 : : * One time initialization routine for run-time dependency on glue library
1258 : : * for multiple PMDs. Each mlx5 PMD that depends on mlx5_common module,
1259 : : * must invoke in its constructor.
1260 : : */
1261 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_common_init)
1262 : : void
1263 : 1270 : mlx5_common_init(void)
1264 : : {
1265 [ + + ]: 1270 : if (mlx5_common_initialized)
1266 : : return;
1267 : :
1268 : 254 : pthread_mutex_init(&devices_list_lock, NULL);
1269 : 254 : mlx5_glue_constructor();
1270 : : mlx5_common_driver_init();
1271 : 254 : mlx5_common_initialized = true;
1272 : : }
1273 : :
1274 : : /**
1275 : : * This function is responsible of initializing the variable
1276 : : * haswell_broadwell_cpu by checking if the cpu is intel
1277 : : * and reading the data returned from mlx5_cpu_id().
1278 : : * since haswell and broadwell cpus don't have improved performance
1279 : : * when using relaxed ordering we want to check the cpu type before
1280 : : * before deciding whether to enable RO or not.
1281 : : * if the cpu is haswell or broadwell the variable will be set to 1
1282 : : * otherwise it will be 0.
1283 : : */
1284 : 254 : RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu, LOG)
1285 : : {
1286 : : #ifdef RTE_ARCH_X86_64
1287 : 254 : unsigned int broadwell_models[4] = {0x3d, 0x47, 0x4F, 0x56};
1288 : 254 : unsigned int haswell_models[4] = {0x3c, 0x3f, 0x45, 0x46};
1289 : : unsigned int i, model, family, brand_id, vendor;
1290 : : unsigned int signature_intel_ebx = 0x756e6547;
1291 : : unsigned int extended_model;
1292 : : unsigned int eax = 0;
1293 : : unsigned int ebx = 0;
1294 : : unsigned int ecx = 0;
1295 : : unsigned int edx = 0;
1296 : : int max_level;
1297 : :
1298 : : mlx5_cpu_id(0, &eax, &ebx, &ecx, &edx);
1299 : : vendor = ebx;
1300 : 254 : max_level = eax;
1301 [ - + ]: 254 : if (max_level < 1) {
1302 : 0 : haswell_broadwell_cpu = 0;
1303 : 0 : return;
1304 : : }
1305 : : mlx5_cpu_id(1, &eax, &ebx, &ecx, &edx);
1306 : 254 : model = (eax >> 4) & 0x0f;
1307 : 254 : family = (eax >> 8) & 0x0f;
1308 : 254 : brand_id = ebx & 0xff;
1309 : 254 : extended_model = (eax >> 12) & 0xf0;
1310 : : /* Check if the processor is Haswell or Broadwell */
1311 [ + - ]: 254 : if (vendor == signature_intel_ebx) {
1312 [ + - ]: 254 : if (family == 0x06)
1313 : 254 : model += extended_model;
1314 [ + - ]: 254 : if (brand_id == 0 && family == 0x6) {
1315 [ + + ]: 1270 : for (i = 0; i < RTE_DIM(broadwell_models); i++)
1316 [ - + ]: 1016 : if (model == broadwell_models[i]) {
1317 : 0 : haswell_broadwell_cpu = 1;
1318 : 0 : return;
1319 : : }
1320 [ + + ]: 1270 : for (i = 0; i < RTE_DIM(haswell_models); i++)
1321 [ - + ]: 1016 : if (model == haswell_models[i]) {
1322 : 0 : haswell_broadwell_cpu = 1;
1323 : 0 : return;
1324 : : }
1325 : : }
1326 : : }
1327 : : #endif
1328 : 254 : haswell_broadwell_cpu = 0;
1329 : : }
1330 : :
1331 : : /**
1332 : : * Allocate the User Access Region with DevX on specified device.
1333 : : * This routine handles the following UAR allocation issues:
1334 : : *
1335 : : * - Try to allocate the UAR with the most appropriate memory mapping
1336 : : * type from the ones supported by the host.
1337 : : *
1338 : : * - Try to allocate the UAR with non-NULL base address OFED 5.0.x and
1339 : : * Upstream rdma_core before v29 returned the NULL as UAR base address
1340 : : * if UAR was not the first object in the UAR page.
1341 : : * It caused the PMD failure and we should try to get another UAR till
1342 : : * we get the first one with non-NULL base address returned.
1343 : : *
1344 : : * @param [in] cdev
1345 : : * Pointer to mlx5 device structure to perform allocation on its context.
1346 : : *
1347 : : * @return
1348 : : * UAR object pointer on success, NULL otherwise and rte_errno is set.
1349 : : */
1350 : : static void *
1351 : 0 : mlx5_devx_alloc_uar(struct mlx5_common_device *cdev)
1352 : : {
1353 : : void *uar;
1354 : : uint32_t retry, uar_mapping;
1355 : : void *base_addr;
1356 : :
1357 [ # # ]: 0 : for (retry = 0; retry < MLX5_ALLOC_UAR_RETRY; ++retry) {
1358 : : #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
1359 : : /* Control the mapping type according to the settings. */
1360 : 0 : uar_mapping = (cdev->config.dbnc == MLX5_SQ_DB_NCACHED) ?
1361 : 0 : MLX5DV_UAR_ALLOC_TYPE_NC : MLX5DV_UAR_ALLOC_TYPE_BF;
1362 : : #else
1363 : : /*
1364 : : * It seems we have no way to control the memory mapping type
1365 : : * for the UAR, the default "Write-Combining" type is supposed.
1366 : : */
1367 : : uar_mapping = 0;
1368 : : #endif
1369 : 0 : uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
1370 : : #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
1371 [ # # ]: 0 : if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_BF) {
1372 : : /*
1373 : : * In some environments like virtual machine the
1374 : : * Write Combining mapped might be not supported and
1375 : : * UAR allocation fails. We tried "Non-Cached" mapping
1376 : : * for the case.
1377 : : */
1378 : 0 : DRV_LOG(DEBUG, "Failed to allocate DevX UAR (BF)");
1379 : : uar_mapping = MLX5DV_UAR_ALLOC_TYPE_NC;
1380 : 0 : uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
1381 [ # # ]: 0 : } else if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_NC) {
1382 : : /*
1383 : : * If Verbs/kernel does not support "Non-Cached"
1384 : : * try the "Write-Combining".
1385 : : */
1386 : 0 : DRV_LOG(DEBUG, "Failed to allocate DevX UAR (NC)");
1387 : : uar_mapping = MLX5DV_UAR_ALLOC_TYPE_BF;
1388 : 0 : uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
1389 : : }
1390 : : #endif
1391 [ # # ]: 0 : if (!uar) {
1392 : 0 : DRV_LOG(ERR, "Failed to allocate DevX UAR (BF/NC)");
1393 : 0 : rte_errno = ENOMEM;
1394 : 0 : goto exit;
1395 : : }
1396 : : base_addr = mlx5_os_get_devx_uar_base_addr(uar);
1397 [ # # ]: 0 : if (base_addr)
1398 : : break;
1399 : : /*
1400 : : * The UARs are allocated by rdma_core within the
1401 : : * IB device context, on context closure all UARs
1402 : : * will be freed, should be no memory/object leakage.
1403 : : */
1404 : 0 : DRV_LOG(DEBUG, "Retrying to allocate DevX UAR");
1405 : : uar = NULL;
1406 : : }
1407 : : /* Check whether we finally succeeded with valid UAR allocation. */
1408 [ # # ]: 0 : if (!uar) {
1409 : 0 : DRV_LOG(ERR, "Failed to allocate DevX UAR (NULL base)");
1410 : 0 : rte_errno = ENOMEM;
1411 : : }
1412 : : /*
1413 : : * Return void * instead of struct mlx5dv_devx_uar *
1414 : : * is for compatibility with older rdma-core library headers.
1415 : : */
1416 : 0 : exit:
1417 : 0 : return uar;
1418 : : }
1419 : :
1420 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_devx_uar_release)
1421 : : void
1422 : 0 : mlx5_devx_uar_release(struct mlx5_uar *uar)
1423 : : {
1424 [ # # ]: 0 : if (uar->obj != NULL)
1425 : 0 : mlx5_glue->devx_free_uar(uar->obj);
1426 : : memset(uar, 0, sizeof(*uar));
1427 : 0 : }
1428 : :
1429 : : RTE_EXPORT_INTERNAL_SYMBOL(mlx5_devx_uar_prepare)
1430 : : int
1431 : 0 : mlx5_devx_uar_prepare(struct mlx5_common_device *cdev, struct mlx5_uar *uar)
1432 : : {
1433 : : off_t uar_mmap_offset;
1434 : 0 : const size_t page_size = rte_mem_page_size();
1435 : : void *base_addr;
1436 : : void *uar_obj;
1437 : :
1438 [ # # ]: 0 : if (page_size == (size_t)-1) {
1439 : 0 : DRV_LOG(ERR, "Failed to get mem page size");
1440 : 0 : rte_errno = ENOMEM;
1441 : 0 : return -1;
1442 : : }
1443 : 0 : uar_obj = mlx5_devx_alloc_uar(cdev);
1444 [ # # # # ]: 0 : if (uar_obj == NULL || mlx5_os_get_devx_uar_reg_addr(uar_obj) == NULL) {
1445 : 0 : rte_errno = errno;
1446 : 0 : DRV_LOG(ERR, "Failed to allocate UAR.");
1447 : 0 : return -1;
1448 : : }
1449 [ # # ]: 0 : uar->obj = uar_obj;
1450 : : uar_mmap_offset = mlx5_os_get_devx_uar_mmap_offset(uar_obj);
1451 : : base_addr = mlx5_os_get_devx_uar_base_addr(uar_obj);
1452 : 0 : uar->dbnc = mlx5_db_map_type_get(uar_mmap_offset, page_size);
1453 : 0 : uar->bf_db.db = mlx5_os_get_devx_uar_reg_addr(uar_obj);
1454 : 0 : uar->cq_db.db = RTE_PTR_ADD(base_addr, MLX5_CQ_DOORBELL);
1455 : : #ifndef RTE_ARCH_64
1456 : : rte_spinlock_init(&uar->bf_sl);
1457 : : rte_spinlock_init(&uar->cq_sl);
1458 : : uar->bf_db.sl_p = &uar->bf_sl;
1459 : : uar->cq_db.sl_p = &uar->cq_sl;
1460 : : #endif /* RTE_ARCH_64 */
1461 : 0 : return 0;
1462 : : }
1463 : :
1464 : : RTE_PMD_EXPORT_NAME(mlx5_common_driver);
|