1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
10 #include <rte_mempool.h>
11 #include <rte_class.h>
12 #include <rte_malloc.h>
13 #include <rte_eal_paging.h>
15 #include "mlx5_common.h"
16 #include "mlx5_common_os.h"
17 #include "mlx5_common_mp.h"
18 #include "mlx5_common_log.h"
19 #include "mlx5_common_defs.h"
20 #include "mlx5_common_private.h"
22 uint8_t haswell_broadwell_cpu;
24 /* In case this is an x86_64 intel processor to check if
25 * we should use relaxed ordering.
27 #ifdef RTE_ARCH_X86_64
29 * This function returns processor identification and feature information
32 * @param eax, ebx, ecx, edx
33 * Pointers to the registers that will hold cpu information.
35 * The main category of information returned.
37 static inline void mlx5_cpu_id(unsigned int level,
38 unsigned int *eax, unsigned int *ebx,
39 unsigned int *ecx, unsigned int *edx)
42 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
47 RTE_LOG_REGISTER_DEFAULT(mlx5_common_logtype, NOTICE)
49 /* Head of list of drivers. */
50 static TAILQ_HEAD(mlx5_drivers, mlx5_class_driver) drivers_list =
51 TAILQ_HEAD_INITIALIZER(drivers_list);
53 /* Head of devices. */
54 static TAILQ_HEAD(mlx5_devices, mlx5_common_device) devices_list =
55 TAILQ_HEAD_INITIALIZER(devices_list);
56 static pthread_mutex_t devices_list_lock;
60 unsigned int drv_class;
62 { .name = "vdpa", .drv_class = MLX5_CLASS_VDPA },
63 { .name = "eth", .drv_class = MLX5_CLASS_ETH },
64 /* Keep class "net" for backward compatibility. */
65 { .name = "net", .drv_class = MLX5_CLASS_ETH },
66 { .name = "regex", .drv_class = MLX5_CLASS_REGEX },
67 { .name = "compress", .drv_class = MLX5_CLASS_COMPRESS },
68 { .name = "crypto", .drv_class = MLX5_CLASS_CRYPTO },
72 class_name_to_value(const char *class_name)
76 for (i = 0; i < RTE_DIM(mlx5_classes); i++) {
77 if (strcmp(class_name, mlx5_classes[i].name) == 0)
78 return mlx5_classes[i].drv_class;
83 static struct mlx5_class_driver *
84 driver_get(uint32_t class)
86 struct mlx5_class_driver *driver;
88 TAILQ_FOREACH(driver, &drivers_list, next) {
89 if ((uint32_t)driver->drv_class == class)
96 * Verify and store value for devargs.
99 * Key argument to verify.
101 * Value associated with key.
106 * 0 on success, a negative errno value otherwise and rte_errno is set.
109 mlx5_common_args_check_handler(const char *key, const char *val, void *opaque)
111 struct mlx5_common_dev_config *config = opaque;
114 if (val == NULL || *val == '\0') {
115 DRV_LOG(ERR, "Key %s is missing value.", key);
120 tmp = strtol(val, NULL, 0);
123 DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val);
126 if (strcmp(key, "tx_db_nc") == 0) {
127 if (tmp != MLX5_TXDB_CACHED &&
128 tmp != MLX5_TXDB_NCACHED &&
129 tmp != MLX5_TXDB_HEURISTIC) {
130 DRV_LOG(ERR, "Invalid Tx doorbell mapping parameter.");
135 } else if (strcmp(key, "mr_ext_memseg_en") == 0) {
136 config->mr_ext_memseg_en = !!tmp;
137 } else if (strcmp(key, "mr_mempool_reg_en") == 0) {
138 config->mr_mempool_reg_en = !!tmp;
139 } else if (strcmp(key, "sys_mem_en") == 0) {
140 config->sys_mem_en = !!tmp;
146 * Parse common device parameters.
149 * Device arguments structure.
151 * Pointer to device configuration structure.
154 * 0 on success, a negative errno value otherwise and rte_errno is set.
157 mlx5_common_config_get(struct rte_devargs *devargs,
158 struct mlx5_common_dev_config *config)
160 struct rte_kvargs *kvlist;
164 config->mr_ext_memseg_en = 1;
165 config->mr_mempool_reg_en = 1;
166 config->sys_mem_en = 0;
167 config->dbnc = MLX5_ARG_UNSET;
170 kvlist = rte_kvargs_parse(devargs->args, NULL);
171 if (kvlist == NULL) {
175 ret = rte_kvargs_process(kvlist, NULL, mlx5_common_args_check_handler,
179 rte_kvargs_free(kvlist);
180 DRV_LOG(DEBUG, "mr_ext_memseg_en is %u.", config->mr_ext_memseg_en);
181 DRV_LOG(DEBUG, "mr_mempool_reg_en is %u.", config->mr_mempool_reg_en);
182 DRV_LOG(DEBUG, "sys_mem_en is %u.", config->sys_mem_en);
183 DRV_LOG(DEBUG, "Tx doorbell mapping parameter is %d.", config->dbnc);
188 devargs_class_handler(__rte_unused const char *key,
189 const char *class_names, void *opaque)
198 scratch = strdup(class_names);
199 if (scratch == NULL) {
203 found = strtok_r(scratch, ":", &refstr);
208 /* Extract each individual class name. Multiple
209 * classes can be supplied as class=net:regex:foo:bar.
211 class_val = class_name_to_value(found);
212 /* Check if its a valid class. */
218 found = strtok_r(NULL, ":", &refstr);
219 } while (found != NULL);
223 DRV_LOG(ERR, "Invalid mlx5 class options: %s.\n", class_names);
228 parse_class_options(const struct rte_devargs *devargs)
230 struct rte_kvargs *kvlist;
235 if (devargs->cls != NULL && devargs->cls->name != NULL)
236 /* Global syntax, only one class type. */
237 return class_name_to_value(devargs->cls->name);
238 /* Legacy devargs support multiple classes. */
239 kvlist = rte_kvargs_parse(devargs->args, NULL);
242 rte_kvargs_process(kvlist, RTE_DEVARGS_KEY_CLASS,
243 devargs_class_handler, &ret);
244 rte_kvargs_free(kvlist);
248 static const unsigned int mlx5_class_invalid_combinations[] = {
249 MLX5_CLASS_ETH | MLX5_CLASS_VDPA,
250 /* New class combination should be added here. */
254 is_valid_class_combination(uint32_t user_classes)
258 /* Verify if user specified unsupported combination. */
259 for (i = 0; i < RTE_DIM(mlx5_class_invalid_combinations); i++) {
260 if ((mlx5_class_invalid_combinations[i] & user_classes) ==
261 mlx5_class_invalid_combinations[i])
264 /* Not found any invalid class combination. */
269 mlx5_bus_match(const struct mlx5_class_driver *drv,
270 const struct rte_device *dev)
272 if (mlx5_dev_is_pci(dev))
273 return mlx5_dev_pci_match(drv, dev);
277 static struct mlx5_common_device *
278 to_mlx5_device(const struct rte_device *rte_dev)
280 struct mlx5_common_device *cdev;
282 TAILQ_FOREACH(cdev, &devices_list, next) {
283 if (rte_dev == cdev->dev)
290 mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size)
292 struct rte_pci_addr pci_addr = { 0 };
295 if (mlx5_dev_is_pci(dev)) {
296 /* Input might be <BDF>, format PCI address to <DBDF>. */
297 ret = rte_pci_addr_parse(dev->name, &pci_addr);
300 rte_pci_device_name(&pci_addr, addr, size);
303 #ifdef RTE_EXEC_ENV_LINUX
304 return mlx5_auxiliary_get_pci_str(RTE_DEV_TO_AUXILIARY_CONST(dev),
313 * Register the mempool for the protection domain.
316 * Pointer to the mlx5 common device.
318 * Mempool being registered.
321 * 0 on success, (-1) on failure and rte_errno is set.
324 mlx5_dev_mempool_register(struct mlx5_common_device *cdev,
325 struct rte_mempool *mp, bool is_extmem)
327 return mlx5_mr_mempool_register(cdev, mp, is_extmem);
331 * Unregister the mempool from the protection domain.
334 * Pointer to the mlx5 common device.
336 * Mempool being unregistered.
339 mlx5_dev_mempool_unregister(struct mlx5_common_device *cdev,
340 struct rte_mempool *mp)
342 if (mlx5_mr_mempool_unregister(cdev, mp) < 0)
343 DRV_LOG(WARNING, "Failed to unregister mempool %s for PD %p: %s",
344 mp->name, cdev->pd, rte_strerror(rte_errno));
348 * rte_mempool_walk() callback to register mempools for the protection domain.
351 * The mempool being walked.
353 * Pointer to the device shared context.
356 mlx5_dev_mempool_register_cb(struct rte_mempool *mp, void *arg)
358 struct mlx5_common_device *cdev = arg;
361 ret = mlx5_dev_mempool_register(cdev, mp, false);
362 if (ret < 0 && rte_errno != EEXIST)
364 "Failed to register existing mempool %s for PD %p: %s",
365 mp->name, cdev->pd, rte_strerror(rte_errno));
369 * rte_mempool_walk() callback to unregister mempools
370 * from the protection domain.
373 * The mempool being walked.
375 * Pointer to the device shared context.
378 mlx5_dev_mempool_unregister_cb(struct rte_mempool *mp, void *arg)
380 mlx5_dev_mempool_unregister((struct mlx5_common_device *)arg, mp);
384 * Mempool life cycle callback for mlx5 common devices.
387 * Mempool life cycle event.
389 * Associated mempool.
391 * Pointer to a device shared context.
394 mlx5_dev_mempool_event_cb(enum rte_mempool_event event, struct rte_mempool *mp,
397 struct mlx5_common_device *cdev = arg;
400 case RTE_MEMPOOL_EVENT_READY:
401 if (mlx5_dev_mempool_register(cdev, mp, false) < 0)
403 "Failed to register new mempool %s for PD %p: %s",
404 mp->name, cdev->pd, rte_strerror(rte_errno));
406 case RTE_MEMPOOL_EVENT_DESTROY:
407 mlx5_dev_mempool_unregister(cdev, mp);
413 mlx5_dev_mempool_subscribe(struct mlx5_common_device *cdev)
417 if (!cdev->config.mr_mempool_reg_en)
419 rte_rwlock_write_lock(&cdev->mr_scache.mprwlock);
420 if (cdev->mr_scache.mp_cb_registered)
422 /* Callback for this device may be already registered. */
423 ret = rte_mempool_event_callback_register(mlx5_dev_mempool_event_cb,
425 if (ret != 0 && rte_errno != EEXIST)
427 /* Register mempools only once for this device. */
429 rte_mempool_walk(mlx5_dev_mempool_register_cb, cdev);
431 cdev->mr_scache.mp_cb_registered = 1;
433 rte_rwlock_write_unlock(&cdev->mr_scache.mprwlock);
438 mlx5_dev_mempool_unsubscribe(struct mlx5_common_device *cdev)
442 if (!cdev->mr_scache.mp_cb_registered ||
443 !cdev->config.mr_mempool_reg_en)
445 /* Stop watching for mempool events and unregister all mempools. */
446 ret = rte_mempool_event_callback_unregister(mlx5_dev_mempool_event_cb,
449 rte_mempool_walk(mlx5_dev_mempool_unregister_cb, cdev);
453 * Callback for memory event.
463 mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
464 size_t len, void *arg __rte_unused)
466 struct mlx5_common_device *cdev;
468 /* Must be called from the primary process. */
469 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
470 switch (event_type) {
471 case RTE_MEM_EVENT_FREE:
472 pthread_mutex_lock(&devices_list_lock);
473 /* Iterate all the existing mlx5 devices. */
474 TAILQ_FOREACH(cdev, &devices_list, next)
475 mlx5_free_mr_by_addr(&cdev->mr_scache,
476 mlx5_os_get_ctx_device_name
479 pthread_mutex_unlock(&devices_list_lock);
481 case RTE_MEM_EVENT_ALLOC:
488 * Uninitialize all HW global of device context.
491 * Pointer to mlx5 device structure.
494 * 0 on success, a negative errno value otherwise and rte_errno is set.
497 mlx5_dev_hw_global_release(struct mlx5_common_device *cdev)
499 if (cdev->pd != NULL) {
500 claim_zero(mlx5_os_dealloc_pd(cdev->pd));
503 if (cdev->ctx != NULL) {
504 claim_zero(mlx5_glue->close_device(cdev->ctx));
510 * Initialize all HW global of device context.
513 * Pointer to mlx5 device structure.
515 * Chosen classes come from user device arguments.
518 * 0 on success, a negative errno value otherwise and rte_errno is set.
521 mlx5_dev_hw_global_prepare(struct mlx5_common_device *cdev, uint32_t classes)
525 /* Create context device */
526 ret = mlx5_os_open_device(cdev, classes);
529 /* Allocate Protection Domain object and extract its pdn. */
530 ret = mlx5_os_pd_create(cdev);
533 /* All actions taken below are relevant only when DevX is supported */
534 if (cdev->config.devx == 0)
536 /* Query HCA attributes. */
537 ret = mlx5_devx_cmd_query_hca_attr(cdev->ctx, &cdev->config.hca_attr);
539 DRV_LOG(ERR, "Unable to read HCA capabilities.");
545 mlx5_dev_hw_global_release(cdev);
550 mlx5_common_dev_release(struct mlx5_common_device *cdev)
552 pthread_mutex_lock(&devices_list_lock);
553 TAILQ_REMOVE(&devices_list, cdev, next);
554 pthread_mutex_unlock(&devices_list_lock);
555 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
556 if (TAILQ_EMPTY(&devices_list))
557 rte_mem_event_callback_unregister("MLX5_MEM_EVENT_CB",
559 mlx5_dev_mempool_unsubscribe(cdev);
560 mlx5_mr_release_cache(&cdev->mr_scache);
561 mlx5_dev_hw_global_release(cdev);
566 static struct mlx5_common_device *
567 mlx5_common_dev_create(struct rte_device *eal_dev, uint32_t classes)
569 struct mlx5_common_device *cdev;
572 cdev = rte_zmalloc("mlx5_common_device", sizeof(*cdev), 0);
574 DRV_LOG(ERR, "Device allocation failure.");
579 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
581 /* Parse device parameters. */
582 ret = mlx5_common_config_get(eal_dev->devargs, &cdev->config);
584 DRV_LOG(ERR, "Failed to process device arguments: %s",
585 strerror(rte_errno));
589 mlx5_malloc_mem_select(cdev->config.sys_mem_en);
590 /* Initialize all HW global of device context. */
591 ret = mlx5_dev_hw_global_prepare(cdev, classes);
593 DRV_LOG(ERR, "Failed to initialize device context.");
597 /* Initialize global MR cache resources and update its functions. */
598 ret = mlx5_mr_create_cache(&cdev->mr_scache, eal_dev->numa_node);
600 DRV_LOG(ERR, "Failed to initialize global MR share cache.");
601 mlx5_dev_hw_global_release(cdev);
605 /* Register callback function for global shared MR cache management. */
606 if (TAILQ_EMPTY(&devices_list))
607 rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
608 mlx5_mr_mem_event_cb, NULL);
610 pthread_mutex_lock(&devices_list_lock);
611 TAILQ_INSERT_HEAD(&devices_list, cdev, next);
612 pthread_mutex_unlock(&devices_list_lock);
617 * Validate common devargs when probing again.
619 * When common device probing again, it cannot change its configurations.
620 * If user ask non compatible configurations in devargs, it is error.
621 * This function checks the match between:
622 * - Common device configurations requested by probe again devargs.
623 * - Existing common device configurations.
626 * Pointer to mlx5 device structure.
629 * 0 on success, a negative errno value otherwise and rte_errno is set.
632 mlx5_common_probe_again_args_validate(struct mlx5_common_device *cdev)
634 struct mlx5_common_dev_config *config;
637 /* Secondary process should not handle devargs. */
638 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
640 /* Probe again doesn't have to generate devargs. */
641 if (cdev->dev->devargs == NULL)
643 config = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
644 sizeof(struct mlx5_common_dev_config),
645 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
646 if (config == NULL) {
651 * Creates a temporary common configure structure according to new
652 * devargs attached in probing again.
654 ret = mlx5_common_config_get(cdev->dev->devargs, config);
656 DRV_LOG(ERR, "Failed to process device configure: %s",
657 strerror(rte_errno));
662 * Checks the match between the temporary structure and the existing
663 * common device structure.
665 if (cdev->config.mr_ext_memseg_en ^ config->mr_ext_memseg_en) {
666 DRV_LOG(ERR, "\"mr_ext_memseg_en\" "
667 "configuration mismatch for device %s.",
671 if (cdev->config.mr_mempool_reg_en ^ config->mr_mempool_reg_en) {
672 DRV_LOG(ERR, "\"mr_mempool_reg_en\" "
673 "configuration mismatch for device %s.",
677 if (cdev->config.sys_mem_en ^ config->sys_mem_en) {
679 "\"sys_mem_en\" configuration mismatch for device %s.",
683 if (cdev->config.dbnc ^ config->dbnc) {
684 DRV_LOG(ERR, "\"dbnc\" configuration mismatch for device %s.",
697 drivers_remove(struct mlx5_common_device *cdev, uint32_t enabled_classes)
699 struct mlx5_class_driver *driver;
700 int local_ret = -ENODEV;
704 while (enabled_classes) {
705 driver = driver_get(RTE_BIT64(i));
706 if (driver != NULL) {
707 local_ret = driver->remove(cdev);
709 cdev->classes_loaded &= ~RTE_BIT64(i);
713 enabled_classes &= ~RTE_BIT64(i);
716 if (local_ret != 0 && ret == 0)
722 drivers_probe(struct mlx5_common_device *cdev, uint32_t user_classes)
724 struct mlx5_class_driver *driver;
725 uint32_t enabled_classes = 0;
729 TAILQ_FOREACH(driver, &drivers_list, next) {
730 if ((driver->drv_class & user_classes) == 0)
732 if (!mlx5_bus_match(driver, cdev->dev))
734 already_loaded = cdev->classes_loaded & driver->drv_class;
735 if (already_loaded && driver->probe_again == 0) {
736 DRV_LOG(ERR, "Device %s is already probed",
741 ret = driver->probe(cdev);
743 DRV_LOG(ERR, "Failed to load driver %s",
747 enabled_classes |= driver->drv_class;
750 cdev->classes_loaded |= enabled_classes;
755 * Need to remove only drivers which were not probed before this probe
756 * instance, but have already been probed before this failure.
758 enabled_classes &= ~cdev->classes_loaded;
759 drivers_remove(cdev, enabled_classes);
764 mlx5_common_dev_probe(struct rte_device *eal_dev)
766 struct mlx5_common_device *cdev;
767 uint32_t classes = 0;
768 bool new_device = false;
771 DRV_LOG(INFO, "probe device \"%s\".", eal_dev->name);
772 ret = parse_class_options(eal_dev->devargs);
774 DRV_LOG(ERR, "Unsupported mlx5 class type: %s",
775 eal_dev->devargs->args);
780 /* Default to net class. */
781 classes = MLX5_CLASS_ETH;
783 * MLX5 common driver supports probing again in two scenarios:
784 * - Add new driver under existing common device (regardless of the
785 * driver's own support in probing again).
786 * - Transfer the probing again support of the drivers themselves.
788 * In both scenarios it uses in the existing device. here it looks for
789 * device that match to rte device, if it exists, the request classes
790 * were probed with this device.
792 cdev = to_mlx5_device(eal_dev);
794 /* It isn't probing again, creates a new device. */
795 cdev = mlx5_common_dev_create(eal_dev, classes);
800 /* It is probing again, validate common devargs match. */
801 ret = mlx5_common_probe_again_args_validate(cdev);
804 "Probe again parameters aren't compatible : %s",
805 strerror(rte_errno));
810 * Validate combination here.
811 * For new device, the classes_loaded field is 0 and it check only
812 * the classes given as user device arguments.
814 ret = is_valid_class_combination(classes | cdev->classes_loaded);
816 DRV_LOG(ERR, "Unsupported mlx5 classes combination.");
819 ret = drivers_probe(cdev, classes);
825 mlx5_common_dev_release(cdev);
830 mlx5_common_dev_remove(struct rte_device *eal_dev)
832 struct mlx5_common_device *cdev;
835 cdev = to_mlx5_device(eal_dev);
838 /* Matching device found, cleanup and unload drivers. */
839 ret = drivers_remove(cdev, cdev->classes_loaded);
841 mlx5_common_dev_release(cdev);
846 * Callback to DMA map external memory to a device.
849 * Pointer to the generic device.
851 * Starting virtual address of memory to be mapped.
853 * Starting IOVA address of memory to be mapped.
855 * Length of memory segment being mapped.
858 * 0 on success, negative value on error.
861 mlx5_common_dev_dma_map(struct rte_device *rte_dev, void *addr,
862 uint64_t iova __rte_unused, size_t len)
864 struct mlx5_common_device *dev;
867 dev = to_mlx5_device(rte_dev);
870 "Unable to find matching mlx5 device to device %s",
875 mr = mlx5_create_mr_ext(dev->pd, (uintptr_t)addr, len,
876 SOCKET_ID_ANY, dev->mr_scache.reg_mr_cb);
878 DRV_LOG(WARNING, "Device %s unable to DMA map", rte_dev->name);
882 rte_rwlock_write_lock(&dev->mr_scache.rwlock);
883 LIST_INSERT_HEAD(&dev->mr_scache.mr_list, mr, mr);
884 /* Insert to the global cache table. */
885 mlx5_mr_insert_cache(&dev->mr_scache, mr);
886 rte_rwlock_write_unlock(&dev->mr_scache.rwlock);
891 * Callback to DMA unmap external memory to a device.
894 * Pointer to the generic device.
896 * Starting virtual address of memory to be unmapped.
898 * Starting IOVA address of memory to be unmapped.
900 * Length of memory segment being unmapped.
903 * 0 on success, negative value on error.
906 mlx5_common_dev_dma_unmap(struct rte_device *rte_dev, void *addr,
907 uint64_t iova __rte_unused, size_t len __rte_unused)
909 struct mlx5_common_device *dev;
910 struct mr_cache_entry entry;
913 dev = to_mlx5_device(rte_dev);
916 "Unable to find matching mlx5 device to device %s.",
921 rte_rwlock_read_lock(&dev->mr_scache.rwlock);
922 mr = mlx5_mr_lookup_list(&dev->mr_scache, &entry, (uintptr_t)addr);
924 rte_rwlock_read_unlock(&dev->mr_scache.rwlock);
926 "Address 0x%" PRIxPTR " wasn't registered to device %s",
927 (uintptr_t)addr, rte_dev->name);
932 DRV_LOG(DEBUG, "MR(%p) is removed from list.", (void *)mr);
933 mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb);
934 mlx5_mr_rebuild_cache(&dev->mr_scache);
936 * No explicit wmb is needed after updating dev_gen due to
937 * store-release ordering in unlock that provides the
938 * implicit barrier at the software visible level.
940 ++dev->mr_scache.dev_gen;
941 DRV_LOG(DEBUG, "Broadcasting local cache flush, gen=%d.",
942 dev->mr_scache.dev_gen);
943 rte_rwlock_read_unlock(&dev->mr_scache.rwlock);
948 mlx5_class_driver_register(struct mlx5_class_driver *driver)
950 mlx5_common_driver_on_register_pci(driver);
951 TAILQ_INSERT_TAIL(&drivers_list, driver, next);
954 static void mlx5_common_driver_init(void)
956 mlx5_common_pci_init();
957 #ifdef RTE_EXEC_ENV_LINUX
958 mlx5_common_auxiliary_init();
962 static bool mlx5_common_initialized;
965 * One time initialization routine for run-time dependency on glue library
966 * for multiple PMDs. Each mlx5 PMD that depends on mlx5_common module,
967 * must invoke in its constructor.
970 mlx5_common_init(void)
972 if (mlx5_common_initialized)
975 pthread_mutex_init(&devices_list_lock, NULL);
976 mlx5_glue_constructor();
977 mlx5_common_driver_init();
978 mlx5_common_initialized = true;
982 * This function is responsible of initializing the variable
983 * haswell_broadwell_cpu by checking if the cpu is intel
984 * and reading the data returned from mlx5_cpu_id().
985 * since haswell and broadwell cpus don't have improved performance
986 * when using relaxed ordering we want to check the cpu type before
987 * before deciding whether to enable RO or not.
988 * if the cpu is haswell or broadwell the variable will be set to 1
989 * otherwise it will be 0.
991 RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu, LOG)
993 #ifdef RTE_ARCH_X86_64
994 unsigned int broadwell_models[4] = {0x3d, 0x47, 0x4F, 0x56};
995 unsigned int haswell_models[4] = {0x3c, 0x3f, 0x45, 0x46};
996 unsigned int i, model, family, brand_id, vendor;
997 unsigned int signature_intel_ebx = 0x756e6547;
998 unsigned int extended_model;
999 unsigned int eax = 0;
1000 unsigned int ebx = 0;
1001 unsigned int ecx = 0;
1002 unsigned int edx = 0;
1005 mlx5_cpu_id(0, &eax, &ebx, &ecx, &edx);
1008 if (max_level < 1) {
1009 haswell_broadwell_cpu = 0;
1012 mlx5_cpu_id(1, &eax, &ebx, &ecx, &edx);
1013 model = (eax >> 4) & 0x0f;
1014 family = (eax >> 8) & 0x0f;
1015 brand_id = ebx & 0xff;
1016 extended_model = (eax >> 12) & 0xf0;
1017 /* Check if the processor is Haswell or Broadwell */
1018 if (vendor == signature_intel_ebx) {
1020 model += extended_model;
1021 if (brand_id == 0 && family == 0x6) {
1022 for (i = 0; i < RTE_DIM(broadwell_models); i++)
1023 if (model == broadwell_models[i]) {
1024 haswell_broadwell_cpu = 1;
1027 for (i = 0; i < RTE_DIM(haswell_models); i++)
1028 if (model == haswell_models[i]) {
1029 haswell_broadwell_cpu = 1;
1035 haswell_broadwell_cpu = 0;
1039 * Allocate the User Access Region with DevX on specified device.
1040 * This routine handles the following UAR allocation issues:
1042 * - Try to allocate the UAR with the most appropriate memory mapping
1043 * type from the ones supported by the host.
1045 * - Try to allocate the UAR with non-NULL base address OFED 5.0.x and
1046 * Upstream rdma_core before v29 returned the NULL as UAR base address
1047 * if UAR was not the first object in the UAR page.
1048 * It caused the PMD failure and we should try to get another UAR till
1049 * we get the first one with non-NULL base address returned.
1052 * Pointer to mlx5 device structure to perform allocation on its context.
1055 * UAR object pointer on success, NULL otherwise and rte_errno is set.
1058 mlx5_devx_alloc_uar(struct mlx5_common_device *cdev)
1061 uint32_t retry, uar_mapping;
1064 for (retry = 0; retry < MLX5_ALLOC_UAR_RETRY; ++retry) {
1065 #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
1066 /* Control the mapping type according to the settings. */
1067 uar_mapping = (cdev->config.dbnc == MLX5_TXDB_NCACHED) ?
1068 MLX5DV_UAR_ALLOC_TYPE_NC : MLX5DV_UAR_ALLOC_TYPE_BF;
1071 * It seems we have no way to control the memory mapping type
1072 * for the UAR, the default "Write-Combining" type is supposed.
1076 uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
1077 #ifdef MLX5DV_UAR_ALLOC_TYPE_NC
1078 if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_BF) {
1080 * In some environments like virtual machine the
1081 * Write Combining mapped might be not supported and
1082 * UAR allocation fails. We tried "Non-Cached" mapping
1085 DRV_LOG(DEBUG, "Failed to allocate DevX UAR (BF)");
1086 uar_mapping = MLX5DV_UAR_ALLOC_TYPE_NC;
1087 uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
1088 } else if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_NC) {
1090 * If Verbs/kernel does not support "Non-Cached"
1091 * try the "Write-Combining".
1093 DRV_LOG(DEBUG, "Failed to allocate DevX UAR (NC)");
1094 uar_mapping = MLX5DV_UAR_ALLOC_TYPE_BF;
1095 uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping);
1099 DRV_LOG(ERR, "Failed to allocate DevX UAR (BF/NC)");
1103 base_addr = mlx5_os_get_devx_uar_base_addr(uar);
1107 * The UARs are allocated by rdma_core within the
1108 * IB device context, on context closure all UARs
1109 * will be freed, should be no memory/object leakage.
1111 DRV_LOG(DEBUG, "Retrying to allocate DevX UAR");
1114 /* Check whether we finally succeeded with valid UAR allocation. */
1116 DRV_LOG(ERR, "Failed to allocate DevX UAR (NULL base)");
1120 * Return void * instead of struct mlx5dv_devx_uar *
1121 * is for compatibility with older rdma-core library headers.
1128 mlx5_devx_uar_release(struct mlx5_uar *uar)
1130 if (uar->obj != NULL)
1131 mlx5_glue->devx_free_uar(uar->obj);
1132 memset(uar, 0, sizeof(*uar));
1136 mlx5_devx_uar_prepare(struct mlx5_common_device *cdev, struct mlx5_uar *uar)
1138 off_t uar_mmap_offset;
1139 const size_t page_size = rte_mem_page_size();
1143 if (page_size == (size_t)-1) {
1144 DRV_LOG(ERR, "Failed to get mem page size");
1148 uar_obj = mlx5_devx_alloc_uar(cdev);
1149 if (uar_obj == NULL || mlx5_os_get_devx_uar_reg_addr(uar_obj) == NULL) {
1151 DRV_LOG(ERR, "Failed to allocate UAR.");
1155 uar_mmap_offset = mlx5_os_get_devx_uar_mmap_offset(uar_obj);
1156 base_addr = mlx5_os_get_devx_uar_base_addr(uar_obj);
1157 uar->dbnc = mlx5_db_map_type_get(uar_mmap_offset, page_size);
1158 uar->bf_db.db = mlx5_os_get_devx_uar_reg_addr(uar_obj);
1159 uar->cq_db.db = RTE_PTR_ADD(base_addr, MLX5_CQ_DOORBELL);
1161 rte_spinlock_init(&uar->bf_sl);
1162 rte_spinlock_init(&uar->cq_sl);
1163 uar->bf_db.sl_p = &uar->bf_sl;
1164 uar->cq_db.sl_p = &uar->cq_sl;
1165 #endif /* RTE_ARCH_64 */
1169 RTE_PMD_EXPORT_NAME(mlx5_common_driver, __COUNTER__);