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> 87b4f1e6bSMatan Azrad 97b4f1e6bSMatan Azrad #include <rte_errno.h> 10262c7ad0SOri Kam #include <rte_mempool.h> 11ad435d32SXueming Li #include <rte_class.h> 12ad435d32SXueming Li #include <rte_malloc.h> 135dfa003dSMichael Baum #include <rte_eal_paging.h> 147b4f1e6bSMatan Azrad 157b4f1e6bSMatan Azrad #include "mlx5_common.h" 16262c7ad0SOri Kam #include "mlx5_common_os.h" 17fc59a1ecSMichael Baum #include "mlx5_common_mp.h" 1825245d5dSShiri Kuzin #include "mlx5_common_log.h" 19a77bedf2SMichael Baum #include "mlx5_common_defs.h" 20ad435d32SXueming Li #include "mlx5_common_private.h" 217b4f1e6bSMatan Azrad 224c204fe5SShiri Kuzin uint8_t haswell_broadwell_cpu; 234c204fe5SShiri Kuzin 24a729d2f0SMichael Baum /* Driver type key for new device global syntax. */ 25a729d2f0SMichael Baum #define MLX5_DRIVER_KEY "driver" 26a729d2f0SMichael Baum 279d936f4fSMichael Baum /* Device parameter to get file descriptor for import device. */ 289d936f4fSMichael Baum #define MLX5_DEVICE_FD "cmd_fd" 299d936f4fSMichael Baum 309d936f4fSMichael Baum /* Device parameter to get PD number for import Protection Domain. */ 319d936f4fSMichael Baum #define MLX5_PD_HANDLE "pd_handle" 329d936f4fSMichael Baum 33a729d2f0SMichael Baum /* Enable extending memsegs when creating a MR. */ 34a729d2f0SMichael Baum #define MLX5_MR_EXT_MEMSEG_EN "mr_ext_memseg_en" 35a729d2f0SMichael Baum 36a729d2f0SMichael Baum /* Device parameter to configure implicit registration of mempool memory. */ 37a729d2f0SMichael Baum #define MLX5_MR_MEMPOOL_REG_EN "mr_mempool_reg_en" 38a729d2f0SMichael Baum 39a729d2f0SMichael Baum /* The default memory allocator used in PMD. */ 40a729d2f0SMichael Baum #define MLX5_SYS_MEM_EN "sys_mem_en" 41a729d2f0SMichael Baum 42a729d2f0SMichael Baum /* 43a729d2f0SMichael Baum * Device parameter to force doorbell register mapping 44a6b9d5a5SMichael Baum * to non-cached region eliminating the extra write memory barrier. 45a6b9d5a5SMichael Baum * Deprecated, ignored (Name changed to sq_db_nc). 46a729d2f0SMichael Baum */ 47a729d2f0SMichael Baum #define MLX5_TX_DB_NC "tx_db_nc" 48a729d2f0SMichael Baum 49a6b9d5a5SMichael Baum /* 50a6b9d5a5SMichael Baum * Device parameter to force doorbell register mapping 51a6b9d5a5SMichael Baum * to non-cached region eliminating the extra write memory barrier. 52a6b9d5a5SMichael Baum */ 53a6b9d5a5SMichael Baum #define MLX5_SQ_DB_NC "sq_db_nc" 54a6b9d5a5SMichael Baum 554c204fe5SShiri Kuzin /* In case this is an x86_64 intel processor to check if 564c204fe5SShiri Kuzin * we should use relaxed ordering. 574c204fe5SShiri Kuzin */ 584c204fe5SShiri Kuzin #ifdef RTE_ARCH_X86_64 594c204fe5SShiri Kuzin /** 604c204fe5SShiri Kuzin * This function returns processor identification and feature information 614c204fe5SShiri Kuzin * into the registers. 624c204fe5SShiri Kuzin * 634c204fe5SShiri Kuzin * @param eax, ebx, ecx, edx 644c204fe5SShiri Kuzin * Pointers to the registers that will hold cpu information. 654c204fe5SShiri Kuzin * @param level 664c204fe5SShiri Kuzin * The main category of information returned. 674c204fe5SShiri Kuzin */ 684c204fe5SShiri Kuzin static inline void mlx5_cpu_id(unsigned int level, 694c204fe5SShiri Kuzin unsigned int *eax, unsigned int *ebx, 704c204fe5SShiri Kuzin unsigned int *ecx, unsigned int *edx) 714c204fe5SShiri Kuzin { 724c204fe5SShiri Kuzin __asm__("cpuid\n\t" 734c204fe5SShiri Kuzin : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) 744c204fe5SShiri Kuzin : "0" (level)); 754c204fe5SShiri Kuzin } 764c204fe5SShiri Kuzin #endif 774c204fe5SShiri Kuzin 78eeded204SDavid Marchand RTE_LOG_REGISTER_DEFAULT(mlx5_common_logtype, NOTICE) 7983c99c36SThomas Monjalon 80ad435d32SXueming Li /* Head of list of drivers. */ 81ad435d32SXueming Li static TAILQ_HEAD(mlx5_drivers, mlx5_class_driver) drivers_list = 82ad435d32SXueming Li TAILQ_HEAD_INITIALIZER(drivers_list); 83ad435d32SXueming Li 84ad435d32SXueming Li /* Head of devices. */ 85ad435d32SXueming Li static TAILQ_HEAD(mlx5_devices, mlx5_common_device) devices_list = 86ad435d32SXueming Li TAILQ_HEAD_INITIALIZER(devices_list); 87dc26c9c2SMichael Baum static pthread_mutex_t devices_list_lock; 88ad435d32SXueming Li 89ad435d32SXueming Li static const struct { 90ad435d32SXueming Li const char *name; 91ad435d32SXueming Li unsigned int drv_class; 92ad435d32SXueming Li } mlx5_classes[] = { 93ad435d32SXueming Li { .name = "vdpa", .drv_class = MLX5_CLASS_VDPA }, 94ad435d32SXueming Li { .name = "eth", .drv_class = MLX5_CLASS_ETH }, 95ad435d32SXueming Li /* Keep class "net" for backward compatibility. */ 96ad435d32SXueming Li { .name = "net", .drv_class = MLX5_CLASS_ETH }, 97ad435d32SXueming Li { .name = "regex", .drv_class = MLX5_CLASS_REGEX }, 98ad435d32SXueming Li { .name = "compress", .drv_class = MLX5_CLASS_COMPRESS }, 99ad435d32SXueming Li { .name = "crypto", .drv_class = MLX5_CLASS_CRYPTO }, 100ad435d32SXueming Li }; 101ad435d32SXueming Li 102ad435d32SXueming Li static int 103ad435d32SXueming Li class_name_to_value(const char *class_name) 104ad435d32SXueming Li { 105ad435d32SXueming Li unsigned int i; 106ad435d32SXueming Li 107ad435d32SXueming Li for (i = 0; i < RTE_DIM(mlx5_classes); i++) { 108ad435d32SXueming Li if (strcmp(class_name, mlx5_classes[i].name) == 0) 109ad435d32SXueming Li return mlx5_classes[i].drv_class; 110ad435d32SXueming Li } 111ad435d32SXueming Li return -EINVAL; 112ad435d32SXueming Li } 113ad435d32SXueming Li 114ad435d32SXueming Li static struct mlx5_class_driver * 115ad435d32SXueming Li driver_get(uint32_t class) 116ad435d32SXueming Li { 117ad435d32SXueming Li struct mlx5_class_driver *driver; 118ad435d32SXueming Li 119ad435d32SXueming Li TAILQ_FOREACH(driver, &drivers_list, next) { 120ad435d32SXueming Li if ((uint32_t)driver->drv_class == class) 121ad435d32SXueming Li return driver; 122ad435d32SXueming Li } 123ad435d32SXueming Li return NULL; 124ad435d32SXueming Li } 125ad435d32SXueming Li 126a729d2f0SMichael Baum int 127a729d2f0SMichael Baum mlx5_kvargs_process(struct mlx5_kvargs_ctrl *mkvlist, const char *const keys[], 128a729d2f0SMichael Baum arg_handler_t handler, void *opaque_arg) 129a729d2f0SMichael Baum { 130a729d2f0SMichael Baum const struct rte_kvargs_pair *pair; 131a729d2f0SMichael Baum uint32_t i, j; 132a729d2f0SMichael Baum 133a729d2f0SMichael Baum MLX5_ASSERT(mkvlist && mkvlist->kvlist); 134a729d2f0SMichael Baum /* Process parameters. */ 135a729d2f0SMichael Baum for (i = 0; i < mkvlist->kvlist->count; i++) { 136a729d2f0SMichael Baum pair = &mkvlist->kvlist->pairs[i]; 137a729d2f0SMichael Baum for (j = 0; keys[j] != NULL; ++j) { 138a729d2f0SMichael Baum if (strcmp(pair->key, keys[j]) != 0) 139a729d2f0SMichael Baum continue; 140a729d2f0SMichael Baum if ((*handler)(pair->key, pair->value, opaque_arg) < 0) 141a729d2f0SMichael Baum return -1; 142a729d2f0SMichael Baum mkvlist->is_used[i] = true; 143a729d2f0SMichael Baum break; 144a729d2f0SMichael Baum } 145a729d2f0SMichael Baum } 146a729d2f0SMichael Baum return 0; 147a729d2f0SMichael Baum } 148a729d2f0SMichael Baum 149a729d2f0SMichael Baum /** 150a729d2f0SMichael Baum * Prepare a mlx5 kvargs control. 151a729d2f0SMichael Baum * 152a729d2f0SMichael Baum * @param[out] mkvlist 153a729d2f0SMichael Baum * Pointer to mlx5 kvargs control. 154a729d2f0SMichael Baum * @param[in] devargs 155a729d2f0SMichael Baum * The input string containing the key/value associations. 156a729d2f0SMichael Baum * 157a729d2f0SMichael Baum * @return 158a729d2f0SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 159a729d2f0SMichael Baum */ 160a729d2f0SMichael Baum static int 161a729d2f0SMichael Baum mlx5_kvargs_prepare(struct mlx5_kvargs_ctrl *mkvlist, 162a729d2f0SMichael Baum const struct rte_devargs *devargs) 163a729d2f0SMichael Baum { 164a729d2f0SMichael Baum struct rte_kvargs *kvlist; 165a729d2f0SMichael Baum uint32_t i; 166a729d2f0SMichael Baum 167a729d2f0SMichael Baum if (devargs == NULL) 168a729d2f0SMichael Baum return 0; 169a729d2f0SMichael Baum kvlist = rte_kvargs_parse(devargs->args, NULL); 170a729d2f0SMichael Baum if (kvlist == NULL) { 171a729d2f0SMichael Baum rte_errno = EINVAL; 172a729d2f0SMichael Baum return -rte_errno; 173a729d2f0SMichael Baum } 174a729d2f0SMichael Baum /* 175a729d2f0SMichael Baum * rte_kvargs_parse enable key without value, in mlx5 PMDs we disable 176a729d2f0SMichael Baum * this syntax. 177a729d2f0SMichael Baum */ 178a729d2f0SMichael Baum for (i = 0; i < kvlist->count; i++) { 179a729d2f0SMichael Baum const struct rte_kvargs_pair *pair = &kvlist->pairs[i]; 180a729d2f0SMichael Baum if (pair->value == NULL || *(pair->value) == '\0') { 181a729d2f0SMichael Baum DRV_LOG(ERR, "Key %s is missing value.", pair->key); 182a729d2f0SMichael Baum rte_kvargs_free(kvlist); 183a729d2f0SMichael Baum rte_errno = EINVAL; 184a729d2f0SMichael Baum return -rte_errno; 185a729d2f0SMichael Baum } 186a729d2f0SMichael Baum } 187a729d2f0SMichael Baum /* Makes sure all devargs used array is false. */ 188a729d2f0SMichael Baum memset(mkvlist, 0, sizeof(*mkvlist)); 189a729d2f0SMichael Baum mkvlist->kvlist = kvlist; 190a729d2f0SMichael Baum DRV_LOG(DEBUG, "Parse successfully %u devargs.", 191a729d2f0SMichael Baum mkvlist->kvlist->count); 192a729d2f0SMichael Baum return 0; 193a729d2f0SMichael Baum } 194a729d2f0SMichael Baum 195a729d2f0SMichael Baum /** 196a729d2f0SMichael Baum * Release a mlx5 kvargs control. 197a729d2f0SMichael Baum * 198a729d2f0SMichael Baum * @param[out] mkvlist 199a729d2f0SMichael Baum * Pointer to mlx5 kvargs control. 200a729d2f0SMichael Baum */ 201a729d2f0SMichael Baum static void 202a729d2f0SMichael Baum mlx5_kvargs_release(struct mlx5_kvargs_ctrl *mkvlist) 203a729d2f0SMichael Baum { 204a729d2f0SMichael Baum if (mkvlist == NULL) 205a729d2f0SMichael Baum return; 206a729d2f0SMichael Baum rte_kvargs_free(mkvlist->kvlist); 207a729d2f0SMichael Baum memset(mkvlist, 0, sizeof(*mkvlist)); 208a729d2f0SMichael Baum } 209a729d2f0SMichael Baum 210a729d2f0SMichael Baum /** 211a729d2f0SMichael Baum * Validate device arguments list. 212a729d2f0SMichael Baum * It report about the first unknown parameter. 213a729d2f0SMichael Baum * 214a729d2f0SMichael Baum * @param[in] mkvlist 215a729d2f0SMichael Baum * Pointer to mlx5 kvargs control. 216a729d2f0SMichael Baum * 217a729d2f0SMichael Baum * @return 218a729d2f0SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 219a729d2f0SMichael Baum */ 220a729d2f0SMichael Baum static int 221a729d2f0SMichael Baum mlx5_kvargs_validate(struct mlx5_kvargs_ctrl *mkvlist) 222a729d2f0SMichael Baum { 223a729d2f0SMichael Baum uint32_t i; 224a729d2f0SMichael Baum 225a729d2f0SMichael Baum /* Secondary process should not handle devargs. */ 226a729d2f0SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY) 227a729d2f0SMichael Baum return 0; 228a729d2f0SMichael Baum if (mkvlist == NULL) 229a729d2f0SMichael Baum return 0; 230a729d2f0SMichael Baum for (i = 0; i < mkvlist->kvlist->count; i++) { 231a729d2f0SMichael Baum if (mkvlist->is_used[i] == 0) { 232a729d2f0SMichael Baum DRV_LOG(ERR, "Key \"%s\" " 233a729d2f0SMichael Baum "is unknown for the provided classes.", 234a729d2f0SMichael Baum mkvlist->kvlist->pairs[i].key); 235a729d2f0SMichael Baum rte_errno = EINVAL; 236a729d2f0SMichael Baum return -rte_errno; 237a729d2f0SMichael Baum } 238a729d2f0SMichael Baum } 239a729d2f0SMichael Baum return 0; 240a729d2f0SMichael Baum } 241a729d2f0SMichael Baum 24285209924SMichael Baum /** 24385209924SMichael Baum * Verify and store value for devargs. 24485209924SMichael Baum * 24585209924SMichael Baum * @param[in] key 24685209924SMichael Baum * Key argument to verify. 24785209924SMichael Baum * @param[in] val 24885209924SMichael Baum * Value associated with key. 24985209924SMichael Baum * @param opaque 25085209924SMichael Baum * User data. 25185209924SMichael Baum * 25285209924SMichael Baum * @return 25385209924SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 25485209924SMichael Baum */ 25585209924SMichael Baum static int 25685209924SMichael Baum mlx5_common_args_check_handler(const char *key, const char *val, void *opaque) 25785209924SMichael Baum { 25885209924SMichael Baum struct mlx5_common_dev_config *config = opaque; 25985209924SMichael Baum signed long tmp; 26085209924SMichael Baum 261a729d2f0SMichael Baum if (strcmp(MLX5_DRIVER_KEY, key) == 0 || 262a729d2f0SMichael Baum strcmp(RTE_DEVARGS_KEY_CLASS, key) == 0) 263a729d2f0SMichael Baum return 0; 26485209924SMichael Baum errno = 0; 26585209924SMichael Baum tmp = strtol(val, NULL, 0); 26685209924SMichael Baum if (errno) { 26785209924SMichael Baum rte_errno = errno; 26885209924SMichael Baum DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val); 26985209924SMichael Baum return -rte_errno; 27085209924SMichael Baum } 271a6b9d5a5SMichael Baum if (strcmp(key, MLX5_TX_DB_NC) == 0) 272a6b9d5a5SMichael Baum DRV_LOG(WARNING, 273a6b9d5a5SMichael Baum "%s: deprecated parameter, converted to queue_db_nc", 274a6b9d5a5SMichael Baum key); 275a6b9d5a5SMichael Baum if (strcmp(key, MLX5_SQ_DB_NC) == 0 || 276a6b9d5a5SMichael Baum strcmp(key, MLX5_TX_DB_NC) == 0) { 277a6b9d5a5SMichael Baum if (tmp != MLX5_SQ_DB_CACHED && 278a6b9d5a5SMichael Baum tmp != MLX5_SQ_DB_NCACHED && 279a6b9d5a5SMichael Baum tmp != MLX5_SQ_DB_HEURISTIC) { 280a6b9d5a5SMichael Baum DRV_LOG(ERR, 281a6b9d5a5SMichael Baum "Invalid Send Queue doorbell mapping parameter."); 28285209924SMichael Baum rte_errno = EINVAL; 28385209924SMichael Baum return -rte_errno; 28485209924SMichael Baum } 28585209924SMichael Baum config->dbnc = tmp; 286a729d2f0SMichael Baum } else if (strcmp(key, MLX5_MR_EXT_MEMSEG_EN) == 0) { 28785209924SMichael Baum config->mr_ext_memseg_en = !!tmp; 288a729d2f0SMichael Baum } else if (strcmp(key, MLX5_MR_MEMPOOL_REG_EN) == 0) { 28985209924SMichael Baum config->mr_mempool_reg_en = !!tmp; 290a729d2f0SMichael Baum } else if (strcmp(key, MLX5_SYS_MEM_EN) == 0) { 29185209924SMichael Baum config->sys_mem_en = !!tmp; 2929d936f4fSMichael Baum } else if (strcmp(key, MLX5_DEVICE_FD) == 0) { 2939d936f4fSMichael Baum config->device_fd = tmp; 2949d936f4fSMichael Baum } else if (strcmp(key, MLX5_PD_HANDLE) == 0) { 2959d936f4fSMichael Baum config->pd_handle = tmp; 29685209924SMichael Baum } 29785209924SMichael Baum return 0; 29885209924SMichael Baum } 29985209924SMichael Baum 30085209924SMichael Baum /** 30185209924SMichael Baum * Parse common device parameters. 30285209924SMichael Baum * 30385209924SMichael Baum * @param devargs 30485209924SMichael Baum * Device arguments structure. 30585209924SMichael Baum * @param config 30685209924SMichael Baum * Pointer to device configuration structure. 30785209924SMichael Baum * 30885209924SMichael Baum * @return 30985209924SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 31085209924SMichael Baum */ 31185209924SMichael Baum static int 312a729d2f0SMichael Baum mlx5_common_config_get(struct mlx5_kvargs_ctrl *mkvlist, 31385209924SMichael Baum struct mlx5_common_dev_config *config) 31485209924SMichael Baum { 315a729d2f0SMichael Baum const char **params = (const char *[]){ 316a729d2f0SMichael Baum RTE_DEVARGS_KEY_CLASS, 317a729d2f0SMichael Baum MLX5_DRIVER_KEY, 318a729d2f0SMichael Baum MLX5_TX_DB_NC, 319a6b9d5a5SMichael Baum MLX5_SQ_DB_NC, 320a729d2f0SMichael Baum MLX5_MR_EXT_MEMSEG_EN, 321a729d2f0SMichael Baum MLX5_SYS_MEM_EN, 322a729d2f0SMichael Baum MLX5_MR_MEMPOOL_REG_EN, 3239d936f4fSMichael Baum MLX5_DEVICE_FD, 3249d936f4fSMichael Baum MLX5_PD_HANDLE, 325a729d2f0SMichael Baum NULL, 326a729d2f0SMichael Baum }; 32785209924SMichael Baum int ret = 0; 32885209924SMichael Baum 32985209924SMichael Baum /* Set defaults. */ 33085209924SMichael Baum config->mr_ext_memseg_en = 1; 33185209924SMichael Baum config->mr_mempool_reg_en = 1; 33285209924SMichael Baum config->sys_mem_en = 0; 33385209924SMichael Baum config->dbnc = MLX5_ARG_UNSET; 3349d936f4fSMichael Baum config->device_fd = MLX5_ARG_UNSET; 3359d936f4fSMichael Baum config->pd_handle = MLX5_ARG_UNSET; 3362c75b9bcSMichael Baum if (mkvlist == NULL) 3372c75b9bcSMichael Baum return 0; 338a729d2f0SMichael Baum /* Process common parameters. */ 339a729d2f0SMichael Baum ret = mlx5_kvargs_process(mkvlist, params, 340a729d2f0SMichael Baum mlx5_common_args_check_handler, config); 341a729d2f0SMichael Baum if (ret) { 34285209924SMichael Baum rte_errno = EINVAL; 3439d936f4fSMichael Baum return -rte_errno; 344a729d2f0SMichael Baum } 3459d936f4fSMichael Baum /* Validate user arguments for remote PD and CTX if it is given. */ 3469d936f4fSMichael Baum ret = mlx5_os_remote_pd_and_ctx_validate(config); 3479d936f4fSMichael Baum if (ret) 3489d936f4fSMichael Baum return ret; 34985209924SMichael Baum DRV_LOG(DEBUG, "mr_ext_memseg_en is %u.", config->mr_ext_memseg_en); 35085209924SMichael Baum DRV_LOG(DEBUG, "mr_mempool_reg_en is %u.", config->mr_mempool_reg_en); 35185209924SMichael Baum DRV_LOG(DEBUG, "sys_mem_en is %u.", config->sys_mem_en); 352a6b9d5a5SMichael Baum DRV_LOG(DEBUG, "Send Queue doorbell mapping parameter is %d.", 353a6b9d5a5SMichael Baum config->dbnc); 35485209924SMichael Baum return ret; 35585209924SMichael Baum } 35685209924SMichael Baum 357ad435d32SXueming Li static int 358ad435d32SXueming Li devargs_class_handler(__rte_unused const char *key, 359ad435d32SXueming Li const char *class_names, void *opaque) 360ad435d32SXueming Li { 361ad435d32SXueming Li int *ret = opaque; 362ad435d32SXueming Li int class_val; 363ad435d32SXueming Li char *scratch; 364ad435d32SXueming Li char *found; 365ad435d32SXueming Li char *refstr = NULL; 366ad435d32SXueming Li 367ad435d32SXueming Li *ret = 0; 368ad435d32SXueming Li scratch = strdup(class_names); 369ad435d32SXueming Li if (scratch == NULL) { 370ad435d32SXueming Li *ret = -ENOMEM; 371ad435d32SXueming Li return *ret; 372ad435d32SXueming Li } 373ad435d32SXueming Li found = strtok_r(scratch, ":", &refstr); 374ad435d32SXueming Li if (found == NULL) 375ad435d32SXueming Li /* Empty string. */ 376ad435d32SXueming Li goto err; 377ad435d32SXueming Li do { 378ad435d32SXueming Li /* Extract each individual class name. Multiple 379ad435d32SXueming Li * classes can be supplied as class=net:regex:foo:bar. 380ad435d32SXueming Li */ 381ad435d32SXueming Li class_val = class_name_to_value(found); 382ad435d32SXueming Li /* Check if its a valid class. */ 383ad435d32SXueming Li if (class_val < 0) { 384ad435d32SXueming Li *ret = -EINVAL; 385ad435d32SXueming Li goto err; 386ad435d32SXueming Li } 387ad435d32SXueming Li *ret |= class_val; 388ad435d32SXueming Li found = strtok_r(NULL, ":", &refstr); 389ad435d32SXueming Li } while (found != NULL); 390ad435d32SXueming Li err: 391ad435d32SXueming Li free(scratch); 392ad435d32SXueming Li if (*ret < 0) 393ad435d32SXueming Li DRV_LOG(ERR, "Invalid mlx5 class options: %s.\n", class_names); 394ad435d32SXueming Li return *ret; 395ad435d32SXueming Li } 396ad435d32SXueming Li 397ad435d32SXueming Li static int 398a729d2f0SMichael Baum parse_class_options(const struct rte_devargs *devargs, 399a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 400ad435d32SXueming Li { 401ad435d32SXueming Li int ret = 0; 402ad435d32SXueming Li 403ad435d32SXueming Li if (devargs == NULL) 404ad435d32SXueming Li return 0; 405ad435d32SXueming Li if (devargs->cls != NULL && devargs->cls->name != NULL) 406ad435d32SXueming Li /* Global syntax, only one class type. */ 407ad435d32SXueming Li return class_name_to_value(devargs->cls->name); 408ad435d32SXueming Li /* Legacy devargs support multiple classes. */ 409a729d2f0SMichael Baum rte_kvargs_process(mkvlist->kvlist, RTE_DEVARGS_KEY_CLASS, 410ad435d32SXueming Li devargs_class_handler, &ret); 411ad435d32SXueming Li return ret; 412ad435d32SXueming Li } 413ad435d32SXueming Li 414ad435d32SXueming Li static const unsigned int mlx5_class_invalid_combinations[] = { 415ad435d32SXueming Li MLX5_CLASS_ETH | MLX5_CLASS_VDPA, 416ad435d32SXueming Li /* New class combination should be added here. */ 417ad435d32SXueming Li }; 418ad435d32SXueming Li 419ad435d32SXueming Li static int 420ad435d32SXueming Li is_valid_class_combination(uint32_t user_classes) 421ad435d32SXueming Li { 422ad435d32SXueming Li unsigned int i; 423ad435d32SXueming Li 424ad435d32SXueming Li /* Verify if user specified unsupported combination. */ 425ad435d32SXueming Li for (i = 0; i < RTE_DIM(mlx5_class_invalid_combinations); i++) { 426ad435d32SXueming Li if ((mlx5_class_invalid_combinations[i] & user_classes) == 427ad435d32SXueming Li mlx5_class_invalid_combinations[i]) 428ad435d32SXueming Li return -EINVAL; 429ad435d32SXueming Li } 430ad435d32SXueming Li /* Not found any invalid class combination. */ 431ad435d32SXueming Li return 0; 432ad435d32SXueming Li } 433ad435d32SXueming Li 434ad435d32SXueming Li static bool 435ad435d32SXueming Li mlx5_bus_match(const struct mlx5_class_driver *drv, 436ad435d32SXueming Li const struct rte_device *dev) 437ad435d32SXueming Li { 438ad435d32SXueming Li if (mlx5_dev_is_pci(dev)) 439ad435d32SXueming Li return mlx5_dev_pci_match(drv, dev); 440ad435d32SXueming Li return true; 441ad435d32SXueming Li } 442ad435d32SXueming Li 443ad435d32SXueming Li static struct mlx5_common_device * 444ad435d32SXueming Li to_mlx5_device(const struct rte_device *rte_dev) 445ad435d32SXueming Li { 44685209924SMichael Baum struct mlx5_common_device *cdev; 447ad435d32SXueming Li 44885209924SMichael Baum TAILQ_FOREACH(cdev, &devices_list, next) { 44985209924SMichael Baum if (rte_dev == cdev->dev) 45085209924SMichael Baum return cdev; 451ad435d32SXueming Li } 452ad435d32SXueming Li return NULL; 453ad435d32SXueming Li } 454ad435d32SXueming Li 4554d567938SThomas Monjalon int 4564d567938SThomas Monjalon mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size) 4574d567938SThomas Monjalon { 4584d567938SThomas Monjalon struct rte_pci_addr pci_addr = { 0 }; 4594d567938SThomas Monjalon int ret; 4604d567938SThomas Monjalon 4614d567938SThomas Monjalon if (mlx5_dev_is_pci(dev)) { 4624d567938SThomas Monjalon /* Input might be <BDF>, format PCI address to <DBDF>. */ 4634d567938SThomas Monjalon ret = rte_pci_addr_parse(dev->name, &pci_addr); 4644d567938SThomas Monjalon if (ret != 0) 4654d567938SThomas Monjalon return -ENODEV; 4664d567938SThomas Monjalon rte_pci_device_name(&pci_addr, addr, size); 4674d567938SThomas Monjalon return 0; 4684d567938SThomas Monjalon } 4694d567938SThomas Monjalon #ifdef RTE_EXEC_ENV_LINUX 4704d567938SThomas Monjalon return mlx5_auxiliary_get_pci_str(RTE_DEV_TO_AUXILIARY_CONST(dev), 4714d567938SThomas Monjalon addr, size); 4724d567938SThomas Monjalon #else 4734d567938SThomas Monjalon rte_errno = ENODEV; 4744d567938SThomas Monjalon return -rte_errno; 4754d567938SThomas Monjalon #endif 4764d567938SThomas Monjalon } 4774d567938SThomas Monjalon 478ca1418ceSMichael Baum /** 479fc59a1ecSMichael Baum * Register the mempool for the protection domain. 480fc59a1ecSMichael Baum * 481fc59a1ecSMichael Baum * @param cdev 482fc59a1ecSMichael Baum * Pointer to the mlx5 common device. 483fc59a1ecSMichael Baum * @param mp 484fc59a1ecSMichael Baum * Mempool being registered. 485fc59a1ecSMichael Baum * 486fc59a1ecSMichael Baum * @return 487fc59a1ecSMichael Baum * 0 on success, (-1) on failure and rte_errno is set. 488fc59a1ecSMichael Baum */ 489fc59a1ecSMichael Baum static int 490fc59a1ecSMichael Baum mlx5_dev_mempool_register(struct mlx5_common_device *cdev, 49108ac0358SDmitry Kozlyuk struct rte_mempool *mp, bool is_extmem) 492fc59a1ecSMichael Baum { 49308ac0358SDmitry Kozlyuk return mlx5_mr_mempool_register(cdev, mp, is_extmem); 494fc59a1ecSMichael Baum } 495fc59a1ecSMichael Baum 496fc59a1ecSMichael Baum /** 497fc59a1ecSMichael Baum * Unregister the mempool from the protection domain. 498fc59a1ecSMichael Baum * 499fc59a1ecSMichael Baum * @param cdev 500fc59a1ecSMichael Baum * Pointer to the mlx5 common device. 501fc59a1ecSMichael Baum * @param mp 502fc59a1ecSMichael Baum * Mempool being unregistered. 503fc59a1ecSMichael Baum */ 504fc59a1ecSMichael Baum void 505fc59a1ecSMichael Baum mlx5_dev_mempool_unregister(struct mlx5_common_device *cdev, 506fc59a1ecSMichael Baum struct rte_mempool *mp) 507fc59a1ecSMichael Baum { 50820489176SMichael Baum if (mlx5_mr_mempool_unregister(cdev, mp) < 0) 509fc59a1ecSMichael Baum DRV_LOG(WARNING, "Failed to unregister mempool %s for PD %p: %s", 510fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno)); 511fc59a1ecSMichael Baum } 512fc59a1ecSMichael Baum 513fc59a1ecSMichael Baum /** 514fc59a1ecSMichael Baum * rte_mempool_walk() callback to register mempools for the protection domain. 515fc59a1ecSMichael Baum * 516fc59a1ecSMichael Baum * @param mp 517fc59a1ecSMichael Baum * The mempool being walked. 518fc59a1ecSMichael Baum * @param arg 519fc59a1ecSMichael Baum * Pointer to the device shared context. 520fc59a1ecSMichael Baum */ 521fc59a1ecSMichael Baum static void 522fc59a1ecSMichael Baum mlx5_dev_mempool_register_cb(struct rte_mempool *mp, void *arg) 523fc59a1ecSMichael Baum { 524fc59a1ecSMichael Baum struct mlx5_common_device *cdev = arg; 525fc59a1ecSMichael Baum int ret; 526fc59a1ecSMichael Baum 52708ac0358SDmitry Kozlyuk ret = mlx5_dev_mempool_register(cdev, mp, false); 528fc59a1ecSMichael Baum if (ret < 0 && rte_errno != EEXIST) 529fc59a1ecSMichael Baum DRV_LOG(ERR, 530fc59a1ecSMichael Baum "Failed to register existing mempool %s for PD %p: %s", 531fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno)); 532fc59a1ecSMichael Baum } 533fc59a1ecSMichael Baum 534fc59a1ecSMichael Baum /** 535fc59a1ecSMichael Baum * rte_mempool_walk() callback to unregister mempools 536fc59a1ecSMichael Baum * from the protection domain. 537fc59a1ecSMichael Baum * 538fc59a1ecSMichael Baum * @param mp 539fc59a1ecSMichael Baum * The mempool being walked. 540fc59a1ecSMichael Baum * @param arg 541fc59a1ecSMichael Baum * Pointer to the device shared context. 542fc59a1ecSMichael Baum */ 543fc59a1ecSMichael Baum static void 544fc59a1ecSMichael Baum mlx5_dev_mempool_unregister_cb(struct rte_mempool *mp, void *arg) 545fc59a1ecSMichael Baum { 546fc59a1ecSMichael Baum mlx5_dev_mempool_unregister((struct mlx5_common_device *)arg, mp); 547fc59a1ecSMichael Baum } 548fc59a1ecSMichael Baum 549fc59a1ecSMichael Baum /** 550fc59a1ecSMichael Baum * Mempool life cycle callback for mlx5 common devices. 551fc59a1ecSMichael Baum * 552fc59a1ecSMichael Baum * @param event 553fc59a1ecSMichael Baum * Mempool life cycle event. 554fc59a1ecSMichael Baum * @param mp 555fc59a1ecSMichael Baum * Associated mempool. 556fc59a1ecSMichael Baum * @param arg 557fc59a1ecSMichael Baum * Pointer to a device shared context. 558fc59a1ecSMichael Baum */ 559fc59a1ecSMichael Baum static void 560fc59a1ecSMichael Baum mlx5_dev_mempool_event_cb(enum rte_mempool_event event, struct rte_mempool *mp, 561fc59a1ecSMichael Baum void *arg) 562fc59a1ecSMichael Baum { 563fc59a1ecSMichael Baum struct mlx5_common_device *cdev = arg; 564fc59a1ecSMichael Baum 565fc59a1ecSMichael Baum switch (event) { 566fc59a1ecSMichael Baum case RTE_MEMPOOL_EVENT_READY: 56708ac0358SDmitry Kozlyuk if (mlx5_dev_mempool_register(cdev, mp, false) < 0) 568fc59a1ecSMichael Baum DRV_LOG(ERR, 569fc59a1ecSMichael Baum "Failed to register new mempool %s for PD %p: %s", 570fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno)); 571fc59a1ecSMichael Baum break; 572fc59a1ecSMichael Baum case RTE_MEMPOOL_EVENT_DESTROY: 573fc59a1ecSMichael Baum mlx5_dev_mempool_unregister(cdev, mp); 574fc59a1ecSMichael Baum break; 575fc59a1ecSMichael Baum } 576fc59a1ecSMichael Baum } 577fc59a1ecSMichael Baum 578fc59a1ecSMichael Baum int 579fc59a1ecSMichael Baum mlx5_dev_mempool_subscribe(struct mlx5_common_device *cdev) 580fc59a1ecSMichael Baum { 581fc59a1ecSMichael Baum int ret = 0; 582fc59a1ecSMichael Baum 583fc59a1ecSMichael Baum if (!cdev->config.mr_mempool_reg_en) 584fc59a1ecSMichael Baum return 0; 585fc59a1ecSMichael Baum rte_rwlock_write_lock(&cdev->mr_scache.mprwlock); 586fc59a1ecSMichael Baum if (cdev->mr_scache.mp_cb_registered) 587fc59a1ecSMichael Baum goto exit; 588fc59a1ecSMichael Baum /* Callback for this device may be already registered. */ 589fc59a1ecSMichael Baum ret = rte_mempool_event_callback_register(mlx5_dev_mempool_event_cb, 590fc59a1ecSMichael Baum cdev); 591fc59a1ecSMichael Baum if (ret != 0 && rte_errno != EEXIST) 592fc59a1ecSMichael Baum goto exit; 593fc59a1ecSMichael Baum /* Register mempools only once for this device. */ 594fc59a1ecSMichael Baum if (ret == 0) 595fc59a1ecSMichael Baum rte_mempool_walk(mlx5_dev_mempool_register_cb, cdev); 596fc59a1ecSMichael Baum ret = 0; 597fc59a1ecSMichael Baum cdev->mr_scache.mp_cb_registered = 1; 598fc59a1ecSMichael Baum exit: 599fc59a1ecSMichael Baum rte_rwlock_write_unlock(&cdev->mr_scache.mprwlock); 600fc59a1ecSMichael Baum return ret; 601fc59a1ecSMichael Baum } 602fc59a1ecSMichael Baum 603fc59a1ecSMichael Baum static void 604fc59a1ecSMichael Baum mlx5_dev_mempool_unsubscribe(struct mlx5_common_device *cdev) 605fc59a1ecSMichael Baum { 606fc59a1ecSMichael Baum int ret; 607fc59a1ecSMichael Baum 608fc59a1ecSMichael Baum if (!cdev->mr_scache.mp_cb_registered || 609fc59a1ecSMichael Baum !cdev->config.mr_mempool_reg_en) 610fc59a1ecSMichael Baum return; 611fc59a1ecSMichael Baum /* Stop watching for mempool events and unregister all mempools. */ 612fc59a1ecSMichael Baum ret = rte_mempool_event_callback_unregister(mlx5_dev_mempool_event_cb, 613fc59a1ecSMichael Baum cdev); 614fc59a1ecSMichael Baum if (ret == 0) 615fc59a1ecSMichael Baum rte_mempool_walk(mlx5_dev_mempool_unregister_cb, cdev); 616fc59a1ecSMichael Baum } 617fc59a1ecSMichael Baum 618fc59a1ecSMichael Baum /** 6199f1d636fSMichael Baum * Callback for memory event. 6209f1d636fSMichael Baum * 6219f1d636fSMichael Baum * @param event_type 6229f1d636fSMichael Baum * Memory event type. 6239f1d636fSMichael Baum * @param addr 6249f1d636fSMichael Baum * Address of memory. 6259f1d636fSMichael Baum * @param len 6269f1d636fSMichael Baum * Size of memory. 6279f1d636fSMichael Baum */ 6289f1d636fSMichael Baum static void 6299f1d636fSMichael Baum mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr, 6309f1d636fSMichael Baum size_t len, void *arg __rte_unused) 6319f1d636fSMichael Baum { 6329f1d636fSMichael Baum struct mlx5_common_device *cdev; 6339f1d636fSMichael Baum 6349f1d636fSMichael Baum /* Must be called from the primary process. */ 6359f1d636fSMichael Baum MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY); 6369f1d636fSMichael Baum switch (event_type) { 6379f1d636fSMichael Baum case RTE_MEM_EVENT_FREE: 6389f1d636fSMichael Baum pthread_mutex_lock(&devices_list_lock); 6399f1d636fSMichael Baum /* Iterate all the existing mlx5 devices. */ 6409f1d636fSMichael Baum TAILQ_FOREACH(cdev, &devices_list, next) 6419f1d636fSMichael Baum mlx5_free_mr_by_addr(&cdev->mr_scache, 6429f1d636fSMichael Baum mlx5_os_get_ctx_device_name 6439f1d636fSMichael Baum (cdev->ctx), 6449f1d636fSMichael Baum addr, len); 6459f1d636fSMichael Baum pthread_mutex_unlock(&devices_list_lock); 6469f1d636fSMichael Baum break; 6479f1d636fSMichael Baum case RTE_MEM_EVENT_ALLOC: 6489f1d636fSMichael Baum default: 6499f1d636fSMichael Baum break; 6509f1d636fSMichael Baum } 6519f1d636fSMichael Baum } 6529f1d636fSMichael Baum 6539f1d636fSMichael Baum /** 654ca1418ceSMichael Baum * Uninitialize all HW global of device context. 655ca1418ceSMichael Baum * 656ca1418ceSMichael Baum * @param cdev 657ca1418ceSMichael Baum * Pointer to mlx5 device structure. 658ca1418ceSMichael Baum * 659ca1418ceSMichael Baum * @return 660ca1418ceSMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 661ca1418ceSMichael Baum */ 662ca1418ceSMichael Baum static void 663ca1418ceSMichael Baum mlx5_dev_hw_global_release(struct mlx5_common_device *cdev) 664ca1418ceSMichael Baum { 665e35ccf24SMichael Baum if (cdev->pd != NULL) { 6669d936f4fSMichael Baum claim_zero(mlx5_os_pd_release(cdev)); 667e35ccf24SMichael Baum cdev->pd = NULL; 668e35ccf24SMichael Baum } 669ca1418ceSMichael Baum if (cdev->ctx != NULL) { 670ca1418ceSMichael Baum claim_zero(mlx5_glue->close_device(cdev->ctx)); 671ca1418ceSMichael Baum cdev->ctx = NULL; 672ca1418ceSMichael Baum } 673ca1418ceSMichael Baum } 674ca1418ceSMichael Baum 675ca1418ceSMichael Baum /** 676ca1418ceSMichael Baum * Initialize all HW global of device context. 677ca1418ceSMichael Baum * 678ca1418ceSMichael Baum * @param cdev 679ca1418ceSMichael Baum * Pointer to mlx5 device structure. 680ca1418ceSMichael Baum * @param classes 681ca1418ceSMichael Baum * Chosen classes come from user device arguments. 682ca1418ceSMichael Baum * 683ca1418ceSMichael Baum * @return 684ca1418ceSMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 685ca1418ceSMichael Baum */ 686ca1418ceSMichael Baum static int 687ca1418ceSMichael Baum mlx5_dev_hw_global_prepare(struct mlx5_common_device *cdev, uint32_t classes) 688ca1418ceSMichael Baum { 689ca1418ceSMichael Baum int ret; 690ca1418ceSMichael Baum 691ca1418ceSMichael Baum /* Create context device */ 692ca1418ceSMichael Baum ret = mlx5_os_open_device(cdev, classes); 693ca1418ceSMichael Baum if (ret < 0) 694ca1418ceSMichael Baum return ret; 6959d936f4fSMichael Baum /* 6969d936f4fSMichael Baum * When CTX is created by Verbs, query HCA attribute is unsupported. 6979d936f4fSMichael Baum * When CTX is imported, we cannot know if it is created by DevX or 6989d936f4fSMichael Baum * Verbs. So, we use query HCA attribute function to check it. 6999d936f4fSMichael Baum */ 7009d936f4fSMichael Baum if (cdev->config.devx || cdev->config.device_fd != MLX5_ARG_UNSET) { 701fe46b20cSMichael Baum /* Query HCA attributes. */ 7029d936f4fSMichael Baum ret = mlx5_devx_cmd_query_hca_attr(cdev->ctx, 7039d936f4fSMichael Baum &cdev->config.hca_attr); 704fe46b20cSMichael Baum if (ret) { 7059d936f4fSMichael Baum DRV_LOG(ERR, "Unable to read HCA caps in DevX mode."); 706fe46b20cSMichael Baum rte_errno = ENOTSUP; 707fe46b20cSMichael Baum goto error; 708fe46b20cSMichael Baum } 7099d936f4fSMichael Baum cdev->config.devx = 1; 7109d936f4fSMichael Baum } 7119d936f4fSMichael Baum DRV_LOG(DEBUG, "DevX is %ssupported.", cdev->config.devx ? "" : "NOT "); 7129d936f4fSMichael Baum /* Prepare Protection Domain object and extract its pdn. */ 7139d936f4fSMichael Baum ret = mlx5_os_pd_prepare(cdev); 7149d936f4fSMichael Baum if (ret) 7159d936f4fSMichael Baum goto error; 716ca1418ceSMichael Baum return 0; 717e35ccf24SMichael Baum error: 718e35ccf24SMichael Baum mlx5_dev_hw_global_release(cdev); 719e35ccf24SMichael Baum return ret; 720ca1418ceSMichael Baum } 721ca1418ceSMichael Baum 722ad435d32SXueming Li static void 72385209924SMichael Baum mlx5_common_dev_release(struct mlx5_common_device *cdev) 724ad435d32SXueming Li { 725dc26c9c2SMichael Baum pthread_mutex_lock(&devices_list_lock); 72685209924SMichael Baum TAILQ_REMOVE(&devices_list, cdev, next); 727dc26c9c2SMichael Baum pthread_mutex_unlock(&devices_list_lock); 7289f1d636fSMichael Baum if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 7299f1d636fSMichael Baum if (TAILQ_EMPTY(&devices_list)) 7309f1d636fSMichael Baum rte_mem_event_callback_unregister("MLX5_MEM_EVENT_CB", 7319f1d636fSMichael Baum NULL); 732fc59a1ecSMichael Baum mlx5_dev_mempool_unsubscribe(cdev); 7339f1d636fSMichael Baum mlx5_mr_release_cache(&cdev->mr_scache); 734ca1418ceSMichael Baum mlx5_dev_hw_global_release(cdev); 7359f1d636fSMichael Baum } 73685209924SMichael Baum rte_free(cdev); 73785209924SMichael Baum } 73885209924SMichael Baum 73985209924SMichael Baum static struct mlx5_common_device * 740a729d2f0SMichael Baum mlx5_common_dev_create(struct rte_device *eal_dev, uint32_t classes, 741a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 74285209924SMichael Baum { 74385209924SMichael Baum struct mlx5_common_device *cdev; 74485209924SMichael Baum int ret; 74585209924SMichael Baum 74685209924SMichael Baum cdev = rte_zmalloc("mlx5_common_device", sizeof(*cdev), 0); 74785209924SMichael Baum if (!cdev) { 74885209924SMichael Baum DRV_LOG(ERR, "Device allocation failure."); 74985209924SMichael Baum rte_errno = ENOMEM; 75085209924SMichael Baum return NULL; 75185209924SMichael Baum } 75285209924SMichael Baum cdev->dev = eal_dev; 75385209924SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY) 75485209924SMichael Baum goto exit; 75585209924SMichael Baum /* Parse device parameters. */ 756a729d2f0SMichael Baum ret = mlx5_common_config_get(mkvlist, &cdev->config); 75785209924SMichael Baum if (ret < 0) { 75885209924SMichael Baum DRV_LOG(ERR, "Failed to process device arguments: %s", 75985209924SMichael Baum strerror(rte_errno)); 76085209924SMichael Baum rte_free(cdev); 76185209924SMichael Baum return NULL; 76285209924SMichael Baum } 76385209924SMichael Baum mlx5_malloc_mem_select(cdev->config.sys_mem_en); 764ca1418ceSMichael Baum /* Initialize all HW global of device context. */ 765ca1418ceSMichael Baum ret = mlx5_dev_hw_global_prepare(cdev, classes); 766ca1418ceSMichael Baum if (ret) { 767ca1418ceSMichael Baum DRV_LOG(ERR, "Failed to initialize device context."); 768ca1418ceSMichael Baum rte_free(cdev); 769ca1418ceSMichael Baum return NULL; 770ca1418ceSMichael Baum } 7719f1d636fSMichael Baum /* Initialize global MR cache resources and update its functions. */ 7729f1d636fSMichael Baum ret = mlx5_mr_create_cache(&cdev->mr_scache, eal_dev->numa_node); 7739f1d636fSMichael Baum if (ret) { 7749f1d636fSMichael Baum DRV_LOG(ERR, "Failed to initialize global MR share cache."); 7759f1d636fSMichael Baum mlx5_dev_hw_global_release(cdev); 7769f1d636fSMichael Baum rte_free(cdev); 7779f1d636fSMichael Baum return NULL; 7789f1d636fSMichael Baum } 7799f1d636fSMichael Baum /* Register callback function for global shared MR cache management. */ 7809f1d636fSMichael Baum if (TAILQ_EMPTY(&devices_list)) 7819f1d636fSMichael Baum rte_mem_event_callback_register("MLX5_MEM_EVENT_CB", 7829f1d636fSMichael Baum mlx5_mr_mem_event_cb, NULL); 78385209924SMichael Baum exit: 78485209924SMichael Baum pthread_mutex_lock(&devices_list_lock); 78585209924SMichael Baum TAILQ_INSERT_HEAD(&devices_list, cdev, next); 78685209924SMichael Baum pthread_mutex_unlock(&devices_list_lock); 78785209924SMichael Baum return cdev; 788ad435d32SXueming Li } 789ad435d32SXueming Li 790c089eb93SMichael Baum /** 791c089eb93SMichael Baum * Validate common devargs when probing again. 792c089eb93SMichael Baum * 793c089eb93SMichael Baum * When common device probing again, it cannot change its configurations. 794c089eb93SMichael Baum * If user ask non compatible configurations in devargs, it is error. 795c089eb93SMichael Baum * This function checks the match between: 796c089eb93SMichael Baum * - Common device configurations requested by probe again devargs. 797c089eb93SMichael Baum * - Existing common device configurations. 798c089eb93SMichael Baum * 799c089eb93SMichael Baum * @param cdev 800c089eb93SMichael Baum * Pointer to mlx5 device structure. 801a729d2f0SMichael Baum * @param mkvlist 802a729d2f0SMichael Baum * Pointer to mlx5 kvargs control, can be NULL if there is no devargs. 803c089eb93SMichael Baum * 804c089eb93SMichael Baum * @return 805c089eb93SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 806c089eb93SMichael Baum */ 807c089eb93SMichael Baum static int 808a729d2f0SMichael Baum mlx5_common_probe_again_args_validate(struct mlx5_common_device *cdev, 809a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 810c089eb93SMichael Baum { 811c089eb93SMichael Baum struct mlx5_common_dev_config *config; 812c089eb93SMichael Baum int ret; 813c089eb93SMichael Baum 814c089eb93SMichael Baum /* Secondary process should not handle devargs. */ 815c089eb93SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY) 816c089eb93SMichael Baum return 0; 817c089eb93SMichael Baum /* Probe again doesn't have to generate devargs. */ 818a729d2f0SMichael Baum if (mkvlist == NULL) 819c089eb93SMichael Baum return 0; 820c089eb93SMichael Baum config = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE, 821c089eb93SMichael Baum sizeof(struct mlx5_common_dev_config), 822c089eb93SMichael Baum RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY); 823c089eb93SMichael Baum if (config == NULL) { 824c089eb93SMichael Baum rte_errno = -ENOMEM; 825c089eb93SMichael Baum return -rte_errno; 826c089eb93SMichael Baum } 827c089eb93SMichael Baum /* 828c089eb93SMichael Baum * Creates a temporary common configure structure according to new 829c089eb93SMichael Baum * devargs attached in probing again. 830c089eb93SMichael Baum */ 831a729d2f0SMichael Baum ret = mlx5_common_config_get(mkvlist, config); 832c089eb93SMichael Baum if (ret) { 833c089eb93SMichael Baum DRV_LOG(ERR, "Failed to process device configure: %s", 834c089eb93SMichael Baum strerror(rte_errno)); 835c089eb93SMichael Baum mlx5_free(config); 836c089eb93SMichael Baum return ret; 837c089eb93SMichael Baum } 838c089eb93SMichael Baum /* 839c089eb93SMichael Baum * Checks the match between the temporary structure and the existing 840c089eb93SMichael Baum * common device structure. 841c089eb93SMichael Baum */ 8429d936f4fSMichael Baum if (cdev->config.mr_ext_memseg_en != config->mr_ext_memseg_en) { 8439d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_MR_EXT_MEMSEG_EN "\" " 844c089eb93SMichael Baum "configuration mismatch for device %s.", 845c089eb93SMichael Baum cdev->dev->name); 846c089eb93SMichael Baum goto error; 847c089eb93SMichael Baum } 8489d936f4fSMichael Baum if (cdev->config.mr_mempool_reg_en != config->mr_mempool_reg_en) { 8499d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_MR_MEMPOOL_REG_EN "\" " 850c089eb93SMichael Baum "configuration mismatch for device %s.", 851c089eb93SMichael Baum cdev->dev->name); 852c089eb93SMichael Baum goto error; 853c089eb93SMichael Baum } 8549d936f4fSMichael Baum if (cdev->config.device_fd != config->device_fd) { 8559d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_DEVICE_FD "\" " 8569d936f4fSMichael Baum "configuration mismatch for device %s.", 857c089eb93SMichael Baum cdev->dev->name); 858c089eb93SMichael Baum goto error; 859c089eb93SMichael Baum } 8609d936f4fSMichael Baum if (cdev->config.pd_handle != config->pd_handle) { 8619d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_PD_HANDLE "\" " 8629d936f4fSMichael Baum "configuration mismatch for device %s.", 8639d936f4fSMichael Baum cdev->dev->name); 8649d936f4fSMichael Baum goto error; 8659d936f4fSMichael Baum } 8669d936f4fSMichael Baum if (cdev->config.sys_mem_en != config->sys_mem_en) { 8679d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_SYS_MEM_EN "\" " 8689d936f4fSMichael Baum "configuration mismatch for device %s.", 8699d936f4fSMichael Baum cdev->dev->name); 8709d936f4fSMichael Baum goto error; 8719d936f4fSMichael Baum } 8729d936f4fSMichael Baum if (cdev->config.dbnc != config->dbnc) { 8739d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_SQ_DB_NC "\" " 8749d936f4fSMichael Baum "configuration mismatch for device %s.", 875c089eb93SMichael Baum cdev->dev->name); 876c089eb93SMichael Baum goto error; 877c089eb93SMichael Baum } 878c089eb93SMichael Baum mlx5_free(config); 879c089eb93SMichael Baum return 0; 880c089eb93SMichael Baum error: 881c089eb93SMichael Baum mlx5_free(config); 882c089eb93SMichael Baum rte_errno = EINVAL; 883c089eb93SMichael Baum return -rte_errno; 884c089eb93SMichael Baum } 885c089eb93SMichael Baum 886ad435d32SXueming Li static int 88785209924SMichael Baum drivers_remove(struct mlx5_common_device *cdev, uint32_t enabled_classes) 888ad435d32SXueming Li { 889ad435d32SXueming Li struct mlx5_class_driver *driver; 890ad435d32SXueming Li int local_ret = -ENODEV; 891ad435d32SXueming Li unsigned int i = 0; 892ad435d32SXueming Li int ret = 0; 893ad435d32SXueming Li 894ad435d32SXueming Li while (enabled_classes) { 895ad435d32SXueming Li driver = driver_get(RTE_BIT64(i)); 896ad435d32SXueming Li if (driver != NULL) { 89785209924SMichael Baum local_ret = driver->remove(cdev); 898ad435d32SXueming Li if (local_ret == 0) 89985209924SMichael Baum cdev->classes_loaded &= ~RTE_BIT64(i); 900ad435d32SXueming Li else if (ret == 0) 901ad435d32SXueming Li ret = local_ret; 902ad435d32SXueming Li } 903ad435d32SXueming Li enabled_classes &= ~RTE_BIT64(i); 904ad435d32SXueming Li i++; 905ad435d32SXueming Li } 906ad435d32SXueming Li if (local_ret != 0 && ret == 0) 907ad435d32SXueming Li ret = local_ret; 908ad435d32SXueming Li return ret; 909ad435d32SXueming Li } 910ad435d32SXueming Li 911ad435d32SXueming Li static int 912a729d2f0SMichael Baum drivers_probe(struct mlx5_common_device *cdev, uint32_t user_classes, 913a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 914ad435d32SXueming Li { 915ad435d32SXueming Li struct mlx5_class_driver *driver; 916ad435d32SXueming Li uint32_t enabled_classes = 0; 917ad435d32SXueming Li bool already_loaded; 918b4a4159dSBing Zhao int ret = -EINVAL; 919ad435d32SXueming Li 920ad435d32SXueming Li TAILQ_FOREACH(driver, &drivers_list, next) { 921ad435d32SXueming Li if ((driver->drv_class & user_classes) == 0) 922ad435d32SXueming Li continue; 92385209924SMichael Baum if (!mlx5_bus_match(driver, cdev->dev)) 924ad435d32SXueming Li continue; 92585209924SMichael Baum already_loaded = cdev->classes_loaded & driver->drv_class; 926ad435d32SXueming Li if (already_loaded && driver->probe_again == 0) { 927ad435d32SXueming Li DRV_LOG(ERR, "Device %s is already probed", 92885209924SMichael Baum cdev->dev->name); 929ad435d32SXueming Li ret = -EEXIST; 930ad435d32SXueming Li goto probe_err; 931ad435d32SXueming Li } 932a729d2f0SMichael Baum ret = driver->probe(cdev, mkvlist); 933ad435d32SXueming Li if (ret < 0) { 934ad435d32SXueming Li DRV_LOG(ERR, "Failed to load driver %s", 935ad435d32SXueming Li driver->name); 936ad435d32SXueming Li goto probe_err; 937ad435d32SXueming Li } 938ad435d32SXueming Li enabled_classes |= driver->drv_class; 939ad435d32SXueming Li } 940b4a4159dSBing Zhao if (!ret) { 94185209924SMichael Baum cdev->classes_loaded |= enabled_classes; 942ad435d32SXueming Li return 0; 943b4a4159dSBing Zhao } 944ad435d32SXueming Li probe_err: 9458928997aSMichael Baum /* 9468928997aSMichael Baum * Need to remove only drivers which were not probed before this probe 9478928997aSMichael Baum * instance, but have already been probed before this failure. 948ad435d32SXueming Li */ 9498928997aSMichael Baum enabled_classes &= ~cdev->classes_loaded; 95085209924SMichael Baum drivers_remove(cdev, enabled_classes); 951ad435d32SXueming Li return ret; 952ad435d32SXueming Li } 953ad435d32SXueming Li 954ad435d32SXueming Li int 955ad435d32SXueming Li mlx5_common_dev_probe(struct rte_device *eal_dev) 956ad435d32SXueming Li { 95785209924SMichael Baum struct mlx5_common_device *cdev; 958a729d2f0SMichael Baum struct mlx5_kvargs_ctrl mkvlist; 959a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist_p = NULL; 960ad435d32SXueming Li uint32_t classes = 0; 961ad435d32SXueming Li bool new_device = false; 962ad435d32SXueming Li int ret; 963ad435d32SXueming Li 964ad435d32SXueming Li DRV_LOG(INFO, "probe device \"%s\".", eal_dev->name); 965a729d2f0SMichael Baum if (eal_dev->devargs != NULL) 966a729d2f0SMichael Baum mkvlist_p = &mkvlist; 967a729d2f0SMichael Baum ret = mlx5_kvargs_prepare(mkvlist_p, eal_dev->devargs); 968a729d2f0SMichael Baum if (ret < 0) { 969a729d2f0SMichael Baum DRV_LOG(ERR, "Unsupported device arguments: %s", 970a729d2f0SMichael Baum eal_dev->devargs->args); 971a729d2f0SMichael Baum return ret; 972a729d2f0SMichael Baum } 973a729d2f0SMichael Baum ret = parse_class_options(eal_dev->devargs, mkvlist_p); 974ad435d32SXueming Li if (ret < 0) { 975ad435d32SXueming Li DRV_LOG(ERR, "Unsupported mlx5 class type: %s", 976ad435d32SXueming Li eal_dev->devargs->args); 977a729d2f0SMichael Baum goto class_err; 978ad435d32SXueming Li } 979ad435d32SXueming Li classes = ret; 980ad435d32SXueming Li if (classes == 0) 981ad435d32SXueming Li /* Default to net class. */ 982ad435d32SXueming Li classes = MLX5_CLASS_ETH; 983c089eb93SMichael Baum /* 984c089eb93SMichael Baum * MLX5 common driver supports probing again in two scenarios: 985c089eb93SMichael Baum * - Add new driver under existing common device (regardless of the 986c089eb93SMichael Baum * driver's own support in probing again). 987c089eb93SMichael Baum * - Transfer the probing again support of the drivers themselves. 988c089eb93SMichael Baum * 989c089eb93SMichael Baum * In both scenarios it uses in the existing device. here it looks for 990c089eb93SMichael Baum * device that match to rte device, if it exists, the request classes 991c089eb93SMichael Baum * were probed with this device. 992c089eb93SMichael Baum */ 99385209924SMichael Baum cdev = to_mlx5_device(eal_dev); 99485209924SMichael Baum if (!cdev) { 995c089eb93SMichael Baum /* It isn't probing again, creates a new device. */ 996a729d2f0SMichael Baum cdev = mlx5_common_dev_create(eal_dev, classes, mkvlist_p); 997a729d2f0SMichael Baum if (!cdev) { 998a729d2f0SMichael Baum ret = -ENOMEM; 999a729d2f0SMichael Baum goto class_err; 1000a729d2f0SMichael Baum } 1001ad435d32SXueming Li new_device = true; 1002c089eb93SMichael Baum } else { 1003c089eb93SMichael Baum /* It is probing again, validate common devargs match. */ 1004a729d2f0SMichael Baum ret = mlx5_common_probe_again_args_validate(cdev, mkvlist_p); 1005c089eb93SMichael Baum if (ret) { 1006c089eb93SMichael Baum DRV_LOG(ERR, 1007c089eb93SMichael Baum "Probe again parameters aren't compatible : %s", 1008c089eb93SMichael Baum strerror(rte_errno)); 1009a729d2f0SMichael Baum goto class_err; 1010c089eb93SMichael Baum } 1011288d7c3fSMichael Baum } 1012288d7c3fSMichael Baum /* 1013288d7c3fSMichael Baum * Validate combination here. 1014288d7c3fSMichael Baum * For new device, the classes_loaded field is 0 and it check only 1015288d7c3fSMichael Baum * the classes given as user device arguments. 1016288d7c3fSMichael Baum */ 101785209924SMichael Baum ret = is_valid_class_combination(classes | cdev->classes_loaded); 1018ad435d32SXueming Li if (ret != 0) { 1019ad435d32SXueming Li DRV_LOG(ERR, "Unsupported mlx5 classes combination."); 1020288d7c3fSMichael Baum goto class_err; 1021ad435d32SXueming Li } 1022a729d2f0SMichael Baum ret = drivers_probe(cdev, classes, mkvlist_p); 1023ad435d32SXueming Li if (ret) 1024ad435d32SXueming Li goto class_err; 1025a729d2f0SMichael Baum /* 1026a729d2f0SMichael Baum * Validate that all devargs have been used, unused key -> unknown Key. 1027a729d2f0SMichael Baum * When probe again validate is failed, the added drivers aren't removed 1028a729d2f0SMichael Baum * here but when device is released. 1029a729d2f0SMichael Baum */ 1030a729d2f0SMichael Baum ret = mlx5_kvargs_validate(mkvlist_p); 1031a729d2f0SMichael Baum if (ret) 1032a729d2f0SMichael Baum goto class_err; 1033a729d2f0SMichael Baum mlx5_kvargs_release(mkvlist_p); 1034ad435d32SXueming Li return 0; 1035ad435d32SXueming Li class_err: 1036a729d2f0SMichael Baum if (new_device) { 1037a729d2f0SMichael Baum /* 1038a729d2f0SMichael Baum * For new device, classes_loaded is always 0 before 1039a729d2f0SMichael Baum * drivers_probe function. 1040a729d2f0SMichael Baum */ 1041a729d2f0SMichael Baum if (cdev->classes_loaded) 1042a729d2f0SMichael Baum drivers_remove(cdev, cdev->classes_loaded); 104385209924SMichael Baum mlx5_common_dev_release(cdev); 1044a729d2f0SMichael Baum } 1045a729d2f0SMichael Baum mlx5_kvargs_release(mkvlist_p); 1046ad435d32SXueming Li return ret; 1047ad435d32SXueming Li } 1048ad435d32SXueming Li 1049ad435d32SXueming Li int 1050ad435d32SXueming Li mlx5_common_dev_remove(struct rte_device *eal_dev) 1051ad435d32SXueming Li { 105285209924SMichael Baum struct mlx5_common_device *cdev; 1053ad435d32SXueming Li int ret; 1054ad435d32SXueming Li 105585209924SMichael Baum cdev = to_mlx5_device(eal_dev); 105685209924SMichael Baum if (!cdev) 1057ad435d32SXueming Li return -ENODEV; 1058ad435d32SXueming Li /* Matching device found, cleanup and unload drivers. */ 105985209924SMichael Baum ret = drivers_remove(cdev, cdev->classes_loaded); 1060dffae63dSMichael Baum if (ret == 0) 106185209924SMichael Baum mlx5_common_dev_release(cdev); 1062ad435d32SXueming Li return ret; 1063ad435d32SXueming Li } 1064ad435d32SXueming Li 1065a5d06c90SMichael Baum /** 1066a5d06c90SMichael Baum * Callback to DMA map external memory to a device. 1067a5d06c90SMichael Baum * 1068a5d06c90SMichael Baum * @param rte_dev 1069a5d06c90SMichael Baum * Pointer to the generic device. 1070a5d06c90SMichael Baum * @param addr 1071a5d06c90SMichael Baum * Starting virtual address of memory to be mapped. 1072a5d06c90SMichael Baum * @param iova 1073a5d06c90SMichael Baum * Starting IOVA address of memory to be mapped. 1074a5d06c90SMichael Baum * @param len 1075a5d06c90SMichael Baum * Length of memory segment being mapped. 1076a5d06c90SMichael Baum * 1077a5d06c90SMichael Baum * @return 1078a5d06c90SMichael Baum * 0 on success, negative value on error. 1079a5d06c90SMichael Baum */ 1080ad435d32SXueming Li int 1081a5d06c90SMichael Baum mlx5_common_dev_dma_map(struct rte_device *rte_dev, void *addr, 1082a5d06c90SMichael Baum uint64_t iova __rte_unused, size_t len) 1083ad435d32SXueming Li { 1084a5d06c90SMichael Baum struct mlx5_common_device *dev; 1085*e96d3d02SDmitry Kozlyuk struct mlx5_mr_btree *bt; 1086a5d06c90SMichael Baum struct mlx5_mr *mr; 1087ad435d32SXueming Li 1088a5d06c90SMichael Baum dev = to_mlx5_device(rte_dev); 1089a5d06c90SMichael Baum if (!dev) { 1090a5d06c90SMichael Baum DRV_LOG(WARNING, 1091a5d06c90SMichael Baum "Unable to find matching mlx5 device to device %s", 1092a5d06c90SMichael Baum rte_dev->name); 1093a5d06c90SMichael Baum rte_errno = ENODEV; 1094a5d06c90SMichael Baum return -1; 1095ad435d32SXueming Li } 1096a5d06c90SMichael Baum mr = mlx5_create_mr_ext(dev->pd, (uintptr_t)addr, len, 1097a5d06c90SMichael Baum SOCKET_ID_ANY, dev->mr_scache.reg_mr_cb); 1098a5d06c90SMichael Baum if (!mr) { 1099a5d06c90SMichael Baum DRV_LOG(WARNING, "Device %s unable to DMA map", rte_dev->name); 1100a5d06c90SMichael Baum rte_errno = EINVAL; 1101a5d06c90SMichael Baum return -1; 1102ad435d32SXueming Li } 1103*e96d3d02SDmitry Kozlyuk try_insert: 1104a5d06c90SMichael Baum rte_rwlock_write_lock(&dev->mr_scache.rwlock); 1105*e96d3d02SDmitry Kozlyuk bt = &dev->mr_scache.cache; 1106*e96d3d02SDmitry Kozlyuk if (bt->len == bt->size) { 1107*e96d3d02SDmitry Kozlyuk uint32_t size; 1108*e96d3d02SDmitry Kozlyuk int ret; 1109*e96d3d02SDmitry Kozlyuk 1110*e96d3d02SDmitry Kozlyuk size = bt->size + 1; 1111*e96d3d02SDmitry Kozlyuk MLX5_ASSERT(size > bt->size); 1112*e96d3d02SDmitry Kozlyuk /* 1113*e96d3d02SDmitry Kozlyuk * Avoid deadlock (numbers show the sequence of events): 1114*e96d3d02SDmitry Kozlyuk * mlx5_mr_create_primary(): 1115*e96d3d02SDmitry Kozlyuk * 1) take EAL memory lock 1116*e96d3d02SDmitry Kozlyuk * 3) take MR lock 1117*e96d3d02SDmitry Kozlyuk * this function: 1118*e96d3d02SDmitry Kozlyuk * 2) take MR lock 1119*e96d3d02SDmitry Kozlyuk * 4) take EAL memory lock while allocating the new cache 1120*e96d3d02SDmitry Kozlyuk * Releasing the MR lock before step 4 1121*e96d3d02SDmitry Kozlyuk * allows another thread to execute step 3. 1122*e96d3d02SDmitry Kozlyuk */ 1123*e96d3d02SDmitry Kozlyuk rte_rwlock_write_unlock(&dev->mr_scache.rwlock); 1124*e96d3d02SDmitry Kozlyuk ret = mlx5_mr_expand_cache(&dev->mr_scache, size, 1125*e96d3d02SDmitry Kozlyuk rte_dev->numa_node); 1126*e96d3d02SDmitry Kozlyuk if (ret < 0) { 1127*e96d3d02SDmitry Kozlyuk mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb); 1128*e96d3d02SDmitry Kozlyuk rte_errno = ret; 1129*e96d3d02SDmitry Kozlyuk return -1; 1130*e96d3d02SDmitry Kozlyuk } 1131*e96d3d02SDmitry Kozlyuk goto try_insert; 1132*e96d3d02SDmitry Kozlyuk } 1133a5d06c90SMichael Baum LIST_INSERT_HEAD(&dev->mr_scache.mr_list, mr, mr); 1134a5d06c90SMichael Baum /* Insert to the global cache table. */ 1135a5d06c90SMichael Baum mlx5_mr_insert_cache(&dev->mr_scache, mr); 1136a5d06c90SMichael Baum rte_rwlock_write_unlock(&dev->mr_scache.rwlock); 1137a5d06c90SMichael Baum return 0; 1138ad435d32SXueming Li } 1139ad435d32SXueming Li 1140a5d06c90SMichael Baum /** 1141a5d06c90SMichael Baum * Callback to DMA unmap external memory to a device. 1142a5d06c90SMichael Baum * 1143a5d06c90SMichael Baum * @param rte_dev 1144a5d06c90SMichael Baum * Pointer to the generic device. 1145a5d06c90SMichael Baum * @param addr 1146a5d06c90SMichael Baum * Starting virtual address of memory to be unmapped. 1147a5d06c90SMichael Baum * @param iova 1148a5d06c90SMichael Baum * Starting IOVA address of memory to be unmapped. 1149a5d06c90SMichael Baum * @param len 1150a5d06c90SMichael Baum * Length of memory segment being unmapped. 1151a5d06c90SMichael Baum * 1152a5d06c90SMichael Baum * @return 1153a5d06c90SMichael Baum * 0 on success, negative value on error. 1154a5d06c90SMichael Baum */ 1155ad435d32SXueming Li int 1156a5d06c90SMichael Baum mlx5_common_dev_dma_unmap(struct rte_device *rte_dev, void *addr, 1157a5d06c90SMichael Baum uint64_t iova __rte_unused, size_t len __rte_unused) 1158ad435d32SXueming Li { 1159a5d06c90SMichael Baum struct mlx5_common_device *dev; 1160a5d06c90SMichael Baum struct mr_cache_entry entry; 1161a5d06c90SMichael Baum struct mlx5_mr *mr; 1162ad435d32SXueming Li 1163a5d06c90SMichael Baum dev = to_mlx5_device(rte_dev); 1164a5d06c90SMichael Baum if (!dev) { 1165a5d06c90SMichael Baum DRV_LOG(WARNING, 1166a5d06c90SMichael Baum "Unable to find matching mlx5 device to device %s.", 1167a5d06c90SMichael Baum rte_dev->name); 1168a5d06c90SMichael Baum rte_errno = ENODEV; 1169a5d06c90SMichael Baum return -1; 1170ad435d32SXueming Li } 1171a5d06c90SMichael Baum rte_rwlock_read_lock(&dev->mr_scache.rwlock); 1172a5d06c90SMichael Baum mr = mlx5_mr_lookup_list(&dev->mr_scache, &entry, (uintptr_t)addr); 1173a5d06c90SMichael Baum if (!mr) { 1174a5d06c90SMichael Baum rte_rwlock_read_unlock(&dev->mr_scache.rwlock); 1175a5d06c90SMichael Baum DRV_LOG(WARNING, 1176a5d06c90SMichael Baum "Address 0x%" PRIxPTR " wasn't registered to device %s", 1177a5d06c90SMichael Baum (uintptr_t)addr, rte_dev->name); 1178a5d06c90SMichael Baum rte_errno = EINVAL; 1179a5d06c90SMichael Baum return -1; 1180a5d06c90SMichael Baum } 1181a5d06c90SMichael Baum LIST_REMOVE(mr, mr); 1182a5d06c90SMichael Baum DRV_LOG(DEBUG, "MR(%p) is removed from list.", (void *)mr); 1183a5d06c90SMichael Baum mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb); 1184a5d06c90SMichael Baum mlx5_mr_rebuild_cache(&dev->mr_scache); 1185a5d06c90SMichael Baum /* 1186a5d06c90SMichael Baum * No explicit wmb is needed after updating dev_gen due to 1187a5d06c90SMichael Baum * store-release ordering in unlock that provides the 1188a5d06c90SMichael Baum * implicit barrier at the software visible level. 1189a5d06c90SMichael Baum */ 1190a5d06c90SMichael Baum ++dev->mr_scache.dev_gen; 1191a5d06c90SMichael Baum DRV_LOG(DEBUG, "Broadcasting local cache flush, gen=%d.", 1192a5d06c90SMichael Baum dev->mr_scache.dev_gen); 1193a5d06c90SMichael Baum rte_rwlock_read_unlock(&dev->mr_scache.rwlock); 1194a5d06c90SMichael Baum return 0; 1195ad435d32SXueming Li } 1196ad435d32SXueming Li 1197ad435d32SXueming Li void 1198ad435d32SXueming Li mlx5_class_driver_register(struct mlx5_class_driver *driver) 1199ad435d32SXueming Li { 1200ad435d32SXueming Li mlx5_common_driver_on_register_pci(driver); 1201ad435d32SXueming Li TAILQ_INSERT_TAIL(&drivers_list, driver, next); 1202ad435d32SXueming Li } 1203ad435d32SXueming Li 1204ad435d32SXueming Li static void mlx5_common_driver_init(void) 1205ad435d32SXueming Li { 1206ad435d32SXueming Li mlx5_common_pci_init(); 1207777b72a9SXueming Li #ifdef RTE_EXEC_ENV_LINUX 1208777b72a9SXueming Li mlx5_common_auxiliary_init(); 1209777b72a9SXueming Li #endif 1210ad435d32SXueming Li } 1211ad435d32SXueming Li 121282088001SParav Pandit static bool mlx5_common_initialized; 121382088001SParav Pandit 121483c99c36SThomas Monjalon /** 12157be78d02SJosh Soref * One time initialization routine for run-time dependency on glue library 121682088001SParav Pandit * for multiple PMDs. Each mlx5 PMD that depends on mlx5_common module, 121782088001SParav Pandit * must invoke in its constructor. 121883c99c36SThomas Monjalon */ 121982088001SParav Pandit void 122082088001SParav Pandit mlx5_common_init(void) 122183c99c36SThomas Monjalon { 122282088001SParav Pandit if (mlx5_common_initialized) 122382088001SParav Pandit return; 122482088001SParav Pandit 1225dc26c9c2SMichael Baum pthread_mutex_init(&devices_list_lock, NULL); 122679aa4307SOphir Munk mlx5_glue_constructor(); 1227ad435d32SXueming Li mlx5_common_driver_init(); 122882088001SParav Pandit mlx5_common_initialized = true; 12297b4f1e6bSMatan Azrad } 12304c204fe5SShiri Kuzin 12314c204fe5SShiri Kuzin /** 12324c204fe5SShiri Kuzin * This function is responsible of initializing the variable 12334c204fe5SShiri Kuzin * haswell_broadwell_cpu by checking if the cpu is intel 12344c204fe5SShiri Kuzin * and reading the data returned from mlx5_cpu_id(). 12354c204fe5SShiri Kuzin * since haswell and broadwell cpus don't have improved performance 12364c204fe5SShiri Kuzin * when using relaxed ordering we want to check the cpu type before 12374c204fe5SShiri Kuzin * before deciding whether to enable RO or not. 12384c204fe5SShiri Kuzin * if the cpu is haswell or broadwell the variable will be set to 1 12394c204fe5SShiri Kuzin * otherwise it will be 0. 12404c204fe5SShiri Kuzin */ 12414c204fe5SShiri Kuzin RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu, LOG) 12424c204fe5SShiri Kuzin { 12434c204fe5SShiri Kuzin #ifdef RTE_ARCH_X86_64 12444c204fe5SShiri Kuzin unsigned int broadwell_models[4] = {0x3d, 0x47, 0x4F, 0x56}; 12454c204fe5SShiri Kuzin unsigned int haswell_models[4] = {0x3c, 0x3f, 0x45, 0x46}; 12464c204fe5SShiri Kuzin unsigned int i, model, family, brand_id, vendor; 12474c204fe5SShiri Kuzin unsigned int signature_intel_ebx = 0x756e6547; 12484c204fe5SShiri Kuzin unsigned int extended_model; 12494c204fe5SShiri Kuzin unsigned int eax = 0; 12504c204fe5SShiri Kuzin unsigned int ebx = 0; 12514c204fe5SShiri Kuzin unsigned int ecx = 0; 12524c204fe5SShiri Kuzin unsigned int edx = 0; 12534c204fe5SShiri Kuzin int max_level; 12544c204fe5SShiri Kuzin 12554c204fe5SShiri Kuzin mlx5_cpu_id(0, &eax, &ebx, &ecx, &edx); 12564c204fe5SShiri Kuzin vendor = ebx; 12574c204fe5SShiri Kuzin max_level = eax; 12584c204fe5SShiri Kuzin if (max_level < 1) { 12594c204fe5SShiri Kuzin haswell_broadwell_cpu = 0; 12604c204fe5SShiri Kuzin return; 12614c204fe5SShiri Kuzin } 12624c204fe5SShiri Kuzin mlx5_cpu_id(1, &eax, &ebx, &ecx, &edx); 12634c204fe5SShiri Kuzin model = (eax >> 4) & 0x0f; 12644c204fe5SShiri Kuzin family = (eax >> 8) & 0x0f; 12654c204fe5SShiri Kuzin brand_id = ebx & 0xff; 12664c204fe5SShiri Kuzin extended_model = (eax >> 12) & 0xf0; 12674c204fe5SShiri Kuzin /* Check if the processor is Haswell or Broadwell */ 12684c204fe5SShiri Kuzin if (vendor == signature_intel_ebx) { 12694c204fe5SShiri Kuzin if (family == 0x06) 12704c204fe5SShiri Kuzin model += extended_model; 12714c204fe5SShiri Kuzin if (brand_id == 0 && family == 0x6) { 12724c204fe5SShiri Kuzin for (i = 0; i < RTE_DIM(broadwell_models); i++) 12734c204fe5SShiri Kuzin if (model == broadwell_models[i]) { 12744c204fe5SShiri Kuzin haswell_broadwell_cpu = 1; 12754c204fe5SShiri Kuzin return; 12764c204fe5SShiri Kuzin } 12774c204fe5SShiri Kuzin for (i = 0; i < RTE_DIM(haswell_models); i++) 12784c204fe5SShiri Kuzin if (model == haswell_models[i]) { 12794c204fe5SShiri Kuzin haswell_broadwell_cpu = 1; 12804c204fe5SShiri Kuzin return; 12814c204fe5SShiri Kuzin } 12824c204fe5SShiri Kuzin } 12834c204fe5SShiri Kuzin } 12844c204fe5SShiri Kuzin #endif 12854c204fe5SShiri Kuzin haswell_broadwell_cpu = 0; 12864c204fe5SShiri Kuzin } 1287262c7ad0SOri Kam 1288262c7ad0SOri Kam /** 12899cc0e99cSViacheslav Ovsiienko * Allocate the User Access Region with DevX on specified device. 1290b4371d3dSMichael Baum * This routine handles the following UAR allocation issues: 12919cc0e99cSViacheslav Ovsiienko * 12925dfa003dSMichael Baum * - Try to allocate the UAR with the most appropriate memory mapping 1293b4371d3dSMichael Baum * type from the ones supported by the host. 1294b4371d3dSMichael Baum * 12955dfa003dSMichael Baum * - Try to allocate the UAR with non-NULL base address OFED 5.0.x and 1296b4371d3dSMichael Baum * Upstream rdma_core before v29 returned the NULL as UAR base address 1297b4371d3dSMichael Baum * if UAR was not the first object in the UAR page. 1298b4371d3dSMichael Baum * It caused the PMD failure and we should try to get another UAR till 1299b4371d3dSMichael Baum * we get the first one with non-NULL base address returned. 1300b4371d3dSMichael Baum * 1301b4371d3dSMichael Baum * @param [in] cdev 1302b4371d3dSMichael Baum * Pointer to mlx5 device structure to perform allocation on its context. 13039cc0e99cSViacheslav Ovsiienko * 13049cc0e99cSViacheslav Ovsiienko * @return 13059cc0e99cSViacheslav Ovsiienko * UAR object pointer on success, NULL otherwise and rte_errno is set. 13069cc0e99cSViacheslav Ovsiienko */ 13075dfa003dSMichael Baum static void * 1308b4371d3dSMichael Baum mlx5_devx_alloc_uar(struct mlx5_common_device *cdev) 13099cc0e99cSViacheslav Ovsiienko { 13109cc0e99cSViacheslav Ovsiienko void *uar; 13119cc0e99cSViacheslav Ovsiienko uint32_t retry, uar_mapping; 13129cc0e99cSViacheslav Ovsiienko void *base_addr; 13139cc0e99cSViacheslav Ovsiienko 13149cc0e99cSViacheslav Ovsiienko for (retry = 0; retry < MLX5_ALLOC_UAR_RETRY; ++retry) { 13159cc0e99cSViacheslav Ovsiienko #ifdef MLX5DV_UAR_ALLOC_TYPE_NC 13169cc0e99cSViacheslav Ovsiienko /* Control the mapping type according to the settings. */ 1317a6b9d5a5SMichael Baum uar_mapping = (cdev->config.dbnc == MLX5_SQ_DB_NCACHED) ? 1318b4371d3dSMichael Baum MLX5DV_UAR_ALLOC_TYPE_NC : MLX5DV_UAR_ALLOC_TYPE_BF; 13199cc0e99cSViacheslav Ovsiienko #else 13209cc0e99cSViacheslav Ovsiienko /* 13219cc0e99cSViacheslav Ovsiienko * It seems we have no way to control the memory mapping type 13229cc0e99cSViacheslav Ovsiienko * for the UAR, the default "Write-Combining" type is supposed. 13239cc0e99cSViacheslav Ovsiienko */ 13249cc0e99cSViacheslav Ovsiienko uar_mapping = 0; 13259cc0e99cSViacheslav Ovsiienko #endif 1326b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping); 13279cc0e99cSViacheslav Ovsiienko #ifdef MLX5DV_UAR_ALLOC_TYPE_NC 1328b4371d3dSMichael Baum if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_BF) { 1329b4371d3dSMichael Baum /* 1330b4371d3dSMichael Baum * In some environments like virtual machine the 1331b4371d3dSMichael Baum * Write Combining mapped might be not supported and 1332b4371d3dSMichael Baum * UAR allocation fails. We tried "Non-Cached" mapping 1333b4371d3dSMichael Baum * for the case. 1334b4371d3dSMichael Baum */ 1335b4371d3dSMichael Baum DRV_LOG(DEBUG, "Failed to allocate DevX UAR (BF)"); 1336b4371d3dSMichael Baum uar_mapping = MLX5DV_UAR_ALLOC_TYPE_NC; 1337b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping); 1338b4371d3dSMichael Baum } else if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_NC) { 13399cc0e99cSViacheslav Ovsiienko /* 13409cc0e99cSViacheslav Ovsiienko * If Verbs/kernel does not support "Non-Cached" 13419cc0e99cSViacheslav Ovsiienko * try the "Write-Combining". 13429cc0e99cSViacheslav Ovsiienko */ 13433f0e54feSMichael Baum DRV_LOG(DEBUG, "Failed to allocate DevX UAR (NC)"); 13449cc0e99cSViacheslav Ovsiienko uar_mapping = MLX5DV_UAR_ALLOC_TYPE_BF; 1345b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping); 13469cc0e99cSViacheslav Ovsiienko } 13479cc0e99cSViacheslav Ovsiienko #endif 13489cc0e99cSViacheslav Ovsiienko if (!uar) { 13499cc0e99cSViacheslav Ovsiienko DRV_LOG(ERR, "Failed to allocate DevX UAR (BF/NC)"); 13509cc0e99cSViacheslav Ovsiienko rte_errno = ENOMEM; 13519cc0e99cSViacheslav Ovsiienko goto exit; 13529cc0e99cSViacheslav Ovsiienko } 13539cc0e99cSViacheslav Ovsiienko base_addr = mlx5_os_get_devx_uar_base_addr(uar); 13549cc0e99cSViacheslav Ovsiienko if (base_addr) 13559cc0e99cSViacheslav Ovsiienko break; 13569cc0e99cSViacheslav Ovsiienko /* 13579cc0e99cSViacheslav Ovsiienko * The UARs are allocated by rdma_core within the 13589cc0e99cSViacheslav Ovsiienko * IB device context, on context closure all UARs 13599cc0e99cSViacheslav Ovsiienko * will be freed, should be no memory/object leakage. 13609cc0e99cSViacheslav Ovsiienko */ 13613f0e54feSMichael Baum DRV_LOG(DEBUG, "Retrying to allocate DevX UAR"); 13629cc0e99cSViacheslav Ovsiienko uar = NULL; 13639cc0e99cSViacheslav Ovsiienko } 13649cc0e99cSViacheslav Ovsiienko /* Check whether we finally succeeded with valid UAR allocation. */ 13659cc0e99cSViacheslav Ovsiienko if (!uar) { 13669cc0e99cSViacheslav Ovsiienko DRV_LOG(ERR, "Failed to allocate DevX UAR (NULL base)"); 13679cc0e99cSViacheslav Ovsiienko rte_errno = ENOMEM; 13689cc0e99cSViacheslav Ovsiienko } 13699cc0e99cSViacheslav Ovsiienko /* 13709cc0e99cSViacheslav Ovsiienko * Return void * instead of struct mlx5dv_devx_uar * 13719cc0e99cSViacheslav Ovsiienko * is for compatibility with older rdma-core library headers. 13729cc0e99cSViacheslav Ovsiienko */ 13739cc0e99cSViacheslav Ovsiienko exit: 13749cc0e99cSViacheslav Ovsiienko return uar; 13759cc0e99cSViacheslav Ovsiienko } 1376ad435d32SXueming Li 13775dfa003dSMichael Baum void 13785dfa003dSMichael Baum mlx5_devx_uar_release(struct mlx5_uar *uar) 13795dfa003dSMichael Baum { 13805dfa003dSMichael Baum if (uar->obj != NULL) 13815dfa003dSMichael Baum mlx5_glue->devx_free_uar(uar->obj); 13825dfa003dSMichael Baum memset(uar, 0, sizeof(*uar)); 13835dfa003dSMichael Baum } 13845dfa003dSMichael Baum 13855dfa003dSMichael Baum int 13865dfa003dSMichael Baum mlx5_devx_uar_prepare(struct mlx5_common_device *cdev, struct mlx5_uar *uar) 13875dfa003dSMichael Baum { 13885dfa003dSMichael Baum off_t uar_mmap_offset; 13895dfa003dSMichael Baum const size_t page_size = rte_mem_page_size(); 13905dfa003dSMichael Baum void *base_addr; 13915dfa003dSMichael Baum void *uar_obj; 13925dfa003dSMichael Baum 13935dfa003dSMichael Baum if (page_size == (size_t)-1) { 13945dfa003dSMichael Baum DRV_LOG(ERR, "Failed to get mem page size"); 13955dfa003dSMichael Baum rte_errno = ENOMEM; 13965dfa003dSMichael Baum return -1; 13975dfa003dSMichael Baum } 13985dfa003dSMichael Baum uar_obj = mlx5_devx_alloc_uar(cdev); 13995dfa003dSMichael Baum if (uar_obj == NULL || mlx5_os_get_devx_uar_reg_addr(uar_obj) == NULL) { 14005dfa003dSMichael Baum rte_errno = errno; 14015dfa003dSMichael Baum DRV_LOG(ERR, "Failed to allocate UAR."); 14025dfa003dSMichael Baum return -1; 14035dfa003dSMichael Baum } 14045dfa003dSMichael Baum uar->obj = uar_obj; 14055dfa003dSMichael Baum uar_mmap_offset = mlx5_os_get_devx_uar_mmap_offset(uar_obj); 14065dfa003dSMichael Baum base_addr = mlx5_os_get_devx_uar_base_addr(uar_obj); 14075dfa003dSMichael Baum uar->dbnc = mlx5_db_map_type_get(uar_mmap_offset, page_size); 14085dfa003dSMichael Baum uar->bf_db.db = mlx5_os_get_devx_uar_reg_addr(uar_obj); 14095dfa003dSMichael Baum uar->cq_db.db = RTE_PTR_ADD(base_addr, MLX5_CQ_DOORBELL); 14105dfa003dSMichael Baum #ifndef RTE_ARCH_64 14115dfa003dSMichael Baum rte_spinlock_init(&uar->bf_sl); 14125dfa003dSMichael Baum rte_spinlock_init(&uar->cq_sl); 14135dfa003dSMichael Baum uar->bf_db.sl_p = &uar->bf_sl; 14145dfa003dSMichael Baum uar->cq_db.sl_p = &uar->cq_sl; 14155dfa003dSMichael Baum #endif /* RTE_ARCH_64 */ 14165dfa003dSMichael Baum return 0; 14175dfa003dSMichael Baum } 14185dfa003dSMichael Baum 1419ad435d32SXueming Li RTE_PMD_EXPORT_NAME(mlx5_common_driver, __COUNTER__); 1420