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 1675fb0c63dSMichael Baum if (mkvlist == NULL) 168a729d2f0SMichael Baum return 0; 1695fb0c63dSMichael Baum MLX5_ASSERT(devargs != NULL && devargs->args != NULL); 170a729d2f0SMichael Baum kvlist = rte_kvargs_parse(devargs->args, NULL); 171a729d2f0SMichael Baum if (kvlist == NULL) { 172a729d2f0SMichael Baum rte_errno = EINVAL; 173a729d2f0SMichael Baum return -rte_errno; 174a729d2f0SMichael Baum } 175a729d2f0SMichael Baum /* 176a729d2f0SMichael Baum * rte_kvargs_parse enable key without value, in mlx5 PMDs we disable 177a729d2f0SMichael Baum * this syntax. 178a729d2f0SMichael Baum */ 179a729d2f0SMichael Baum for (i = 0; i < kvlist->count; i++) { 180a729d2f0SMichael Baum const struct rte_kvargs_pair *pair = &kvlist->pairs[i]; 181a729d2f0SMichael Baum if (pair->value == NULL || *(pair->value) == '\0') { 182a729d2f0SMichael Baum DRV_LOG(ERR, "Key %s is missing value.", pair->key); 183a729d2f0SMichael Baum rte_kvargs_free(kvlist); 184a729d2f0SMichael Baum rte_errno = EINVAL; 185a729d2f0SMichael Baum return -rte_errno; 186a729d2f0SMichael Baum } 187a729d2f0SMichael Baum } 188a729d2f0SMichael Baum /* Makes sure all devargs used array is false. */ 189a729d2f0SMichael Baum memset(mkvlist, 0, sizeof(*mkvlist)); 190a729d2f0SMichael Baum mkvlist->kvlist = kvlist; 191a729d2f0SMichael Baum DRV_LOG(DEBUG, "Parse successfully %u devargs.", 192a729d2f0SMichael Baum mkvlist->kvlist->count); 193a729d2f0SMichael Baum return 0; 194a729d2f0SMichael Baum } 195a729d2f0SMichael Baum 196a729d2f0SMichael Baum /** 197a729d2f0SMichael Baum * Release a mlx5 kvargs control. 198a729d2f0SMichael Baum * 199a729d2f0SMichael Baum * @param[out] mkvlist 200a729d2f0SMichael Baum * Pointer to mlx5 kvargs control. 201a729d2f0SMichael Baum */ 202a729d2f0SMichael Baum static void 203a729d2f0SMichael Baum mlx5_kvargs_release(struct mlx5_kvargs_ctrl *mkvlist) 204a729d2f0SMichael Baum { 205a729d2f0SMichael Baum if (mkvlist == NULL) 206a729d2f0SMichael Baum return; 207a729d2f0SMichael Baum rte_kvargs_free(mkvlist->kvlist); 208a729d2f0SMichael Baum memset(mkvlist, 0, sizeof(*mkvlist)); 209a729d2f0SMichael Baum } 210a729d2f0SMichael Baum 211a729d2f0SMichael Baum /** 212a729d2f0SMichael Baum * Validate device arguments list. 213a729d2f0SMichael Baum * It report about the first unknown parameter. 214a729d2f0SMichael Baum * 215a729d2f0SMichael Baum * @param[in] mkvlist 216a729d2f0SMichael Baum * Pointer to mlx5 kvargs control. 217a729d2f0SMichael Baum * 218a729d2f0SMichael Baum * @return 219a729d2f0SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 220a729d2f0SMichael Baum */ 221a729d2f0SMichael Baum static int 222a729d2f0SMichael Baum mlx5_kvargs_validate(struct mlx5_kvargs_ctrl *mkvlist) 223a729d2f0SMichael Baum { 224a729d2f0SMichael Baum uint32_t i; 225a729d2f0SMichael Baum 226a729d2f0SMichael Baum /* Secondary process should not handle devargs. */ 227a729d2f0SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY) 228a729d2f0SMichael Baum return 0; 229a729d2f0SMichael Baum if (mkvlist == NULL) 230a729d2f0SMichael Baum return 0; 231a729d2f0SMichael Baum for (i = 0; i < mkvlist->kvlist->count; i++) { 232a729d2f0SMichael Baum if (mkvlist->is_used[i] == 0) { 233a729d2f0SMichael Baum DRV_LOG(ERR, "Key \"%s\" " 234a729d2f0SMichael Baum "is unknown for the provided classes.", 235a729d2f0SMichael Baum mkvlist->kvlist->pairs[i].key); 236a729d2f0SMichael Baum rte_errno = EINVAL; 237a729d2f0SMichael Baum return -rte_errno; 238a729d2f0SMichael Baum } 239a729d2f0SMichael Baum } 240a729d2f0SMichael Baum return 0; 241a729d2f0SMichael Baum } 242a729d2f0SMichael Baum 24385209924SMichael Baum /** 24485209924SMichael Baum * Verify and store value for devargs. 24585209924SMichael Baum * 24685209924SMichael Baum * @param[in] key 24785209924SMichael Baum * Key argument to verify. 24885209924SMichael Baum * @param[in] val 24985209924SMichael Baum * Value associated with key. 25085209924SMichael Baum * @param opaque 25185209924SMichael Baum * User data. 25285209924SMichael Baum * 25385209924SMichael Baum * @return 25485209924SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 25585209924SMichael Baum */ 25685209924SMichael Baum static int 25785209924SMichael Baum mlx5_common_args_check_handler(const char *key, const char *val, void *opaque) 25885209924SMichael Baum { 25985209924SMichael Baum struct mlx5_common_dev_config *config = opaque; 26085209924SMichael Baum signed long tmp; 26185209924SMichael Baum 262a729d2f0SMichael Baum if (strcmp(MLX5_DRIVER_KEY, key) == 0 || 263a729d2f0SMichael Baum strcmp(RTE_DEVARGS_KEY_CLASS, key) == 0) 264a729d2f0SMichael Baum return 0; 26585209924SMichael Baum errno = 0; 26685209924SMichael Baum tmp = strtol(val, NULL, 0); 26785209924SMichael Baum if (errno) { 26885209924SMichael Baum rte_errno = errno; 26985209924SMichael Baum DRV_LOG(WARNING, "%s: \"%s\" is an invalid integer.", key, val); 27085209924SMichael Baum return -rte_errno; 27185209924SMichael Baum } 272a6b9d5a5SMichael Baum if (strcmp(key, MLX5_TX_DB_NC) == 0) 273a6b9d5a5SMichael Baum DRV_LOG(WARNING, 274a6b9d5a5SMichael Baum "%s: deprecated parameter, converted to queue_db_nc", 275a6b9d5a5SMichael Baum key); 276a6b9d5a5SMichael Baum if (strcmp(key, MLX5_SQ_DB_NC) == 0 || 277a6b9d5a5SMichael Baum strcmp(key, MLX5_TX_DB_NC) == 0) { 278a6b9d5a5SMichael Baum if (tmp != MLX5_SQ_DB_CACHED && 279a6b9d5a5SMichael Baum tmp != MLX5_SQ_DB_NCACHED && 280a6b9d5a5SMichael Baum tmp != MLX5_SQ_DB_HEURISTIC) { 281a6b9d5a5SMichael Baum DRV_LOG(ERR, 282a6b9d5a5SMichael Baum "Invalid Send Queue doorbell mapping parameter."); 28385209924SMichael Baum rte_errno = EINVAL; 28485209924SMichael Baum return -rte_errno; 28585209924SMichael Baum } 28685209924SMichael Baum config->dbnc = tmp; 287a729d2f0SMichael Baum } else if (strcmp(key, MLX5_MR_EXT_MEMSEG_EN) == 0) { 28885209924SMichael Baum config->mr_ext_memseg_en = !!tmp; 289a729d2f0SMichael Baum } else if (strcmp(key, MLX5_MR_MEMPOOL_REG_EN) == 0) { 29085209924SMichael Baum config->mr_mempool_reg_en = !!tmp; 291a729d2f0SMichael Baum } else if (strcmp(key, MLX5_SYS_MEM_EN) == 0) { 29285209924SMichael Baum config->sys_mem_en = !!tmp; 2939d936f4fSMichael Baum } else if (strcmp(key, MLX5_DEVICE_FD) == 0) { 2949d936f4fSMichael Baum config->device_fd = tmp; 2959d936f4fSMichael Baum } else if (strcmp(key, MLX5_PD_HANDLE) == 0) { 2969d936f4fSMichael Baum config->pd_handle = tmp; 29785209924SMichael Baum } 29885209924SMichael Baum return 0; 29985209924SMichael Baum } 30085209924SMichael Baum 30185209924SMichael Baum /** 30285209924SMichael Baum * Parse common device parameters. 30385209924SMichael Baum * 30485209924SMichael Baum * @param devargs 30585209924SMichael Baum * Device arguments structure. 30685209924SMichael Baum * @param config 30785209924SMichael Baum * Pointer to device configuration structure. 30885209924SMichael Baum * 30985209924SMichael Baum * @return 31085209924SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 31185209924SMichael Baum */ 31285209924SMichael Baum static int 313a729d2f0SMichael Baum mlx5_common_config_get(struct mlx5_kvargs_ctrl *mkvlist, 31485209924SMichael Baum struct mlx5_common_dev_config *config) 31585209924SMichael Baum { 316a729d2f0SMichael Baum const char **params = (const char *[]){ 317a729d2f0SMichael Baum RTE_DEVARGS_KEY_CLASS, 318a729d2f0SMichael Baum MLX5_DRIVER_KEY, 319a729d2f0SMichael Baum MLX5_TX_DB_NC, 320a6b9d5a5SMichael Baum MLX5_SQ_DB_NC, 321a729d2f0SMichael Baum MLX5_MR_EXT_MEMSEG_EN, 322a729d2f0SMichael Baum MLX5_SYS_MEM_EN, 323a729d2f0SMichael Baum MLX5_MR_MEMPOOL_REG_EN, 3249d936f4fSMichael Baum MLX5_DEVICE_FD, 3259d936f4fSMichael Baum MLX5_PD_HANDLE, 326a729d2f0SMichael Baum NULL, 327a729d2f0SMichael Baum }; 32885209924SMichael Baum int ret = 0; 32985209924SMichael Baum 33085209924SMichael Baum /* Set defaults. */ 33185209924SMichael Baum config->mr_ext_memseg_en = 1; 33285209924SMichael Baum config->mr_mempool_reg_en = 1; 33385209924SMichael Baum config->sys_mem_en = 0; 33485209924SMichael Baum config->dbnc = MLX5_ARG_UNSET; 3359d936f4fSMichael Baum config->device_fd = MLX5_ARG_UNSET; 3369d936f4fSMichael Baum config->pd_handle = MLX5_ARG_UNSET; 3372c75b9bcSMichael Baum if (mkvlist == NULL) 3382c75b9bcSMichael Baum return 0; 339a729d2f0SMichael Baum /* Process common parameters. */ 340a729d2f0SMichael Baum ret = mlx5_kvargs_process(mkvlist, params, 341a729d2f0SMichael Baum mlx5_common_args_check_handler, config); 342a729d2f0SMichael Baum if (ret) { 34385209924SMichael Baum rte_errno = EINVAL; 3449d936f4fSMichael Baum return -rte_errno; 345a729d2f0SMichael Baum } 3469d936f4fSMichael Baum /* Validate user arguments for remote PD and CTX if it is given. */ 3479d936f4fSMichael Baum ret = mlx5_os_remote_pd_and_ctx_validate(config); 3489d936f4fSMichael Baum if (ret) 3499d936f4fSMichael Baum return ret; 35085209924SMichael Baum DRV_LOG(DEBUG, "mr_ext_memseg_en is %u.", config->mr_ext_memseg_en); 35185209924SMichael Baum DRV_LOG(DEBUG, "mr_mempool_reg_en is %u.", config->mr_mempool_reg_en); 35285209924SMichael Baum DRV_LOG(DEBUG, "sys_mem_en is %u.", config->sys_mem_en); 353a6b9d5a5SMichael Baum DRV_LOG(DEBUG, "Send Queue doorbell mapping parameter is %d.", 354a6b9d5a5SMichael Baum config->dbnc); 35585209924SMichael Baum return ret; 35685209924SMichael Baum } 35785209924SMichael Baum 358ad435d32SXueming Li static int 359ad435d32SXueming Li devargs_class_handler(__rte_unused const char *key, 360ad435d32SXueming Li const char *class_names, void *opaque) 361ad435d32SXueming Li { 362ad435d32SXueming Li int *ret = opaque; 363ad435d32SXueming Li int class_val; 364ad435d32SXueming Li char *scratch; 365ad435d32SXueming Li char *found; 366ad435d32SXueming Li char *refstr = NULL; 367ad435d32SXueming Li 368ad435d32SXueming Li *ret = 0; 369ad435d32SXueming Li scratch = strdup(class_names); 370ad435d32SXueming Li if (scratch == NULL) { 371ad435d32SXueming Li *ret = -ENOMEM; 372ad435d32SXueming Li return *ret; 373ad435d32SXueming Li } 374ad435d32SXueming Li found = strtok_r(scratch, ":", &refstr); 375ad435d32SXueming Li if (found == NULL) 376ad435d32SXueming Li /* Empty string. */ 377ad435d32SXueming Li goto err; 378ad435d32SXueming Li do { 379ad435d32SXueming Li /* Extract each individual class name. Multiple 380ad435d32SXueming Li * classes can be supplied as class=net:regex:foo:bar. 381ad435d32SXueming Li */ 382ad435d32SXueming Li class_val = class_name_to_value(found); 383ad435d32SXueming Li /* Check if its a valid class. */ 384ad435d32SXueming Li if (class_val < 0) { 385ad435d32SXueming Li *ret = -EINVAL; 386ad435d32SXueming Li goto err; 387ad435d32SXueming Li } 388ad435d32SXueming Li *ret |= class_val; 389ad435d32SXueming Li found = strtok_r(NULL, ":", &refstr); 390ad435d32SXueming Li } while (found != NULL); 391ad435d32SXueming Li err: 392ad435d32SXueming Li free(scratch); 393ad435d32SXueming Li if (*ret < 0) 394ad435d32SXueming Li DRV_LOG(ERR, "Invalid mlx5 class options: %s.\n", class_names); 395ad435d32SXueming Li return *ret; 396ad435d32SXueming Li } 397ad435d32SXueming Li 398ad435d32SXueming Li static int 399a729d2f0SMichael Baum parse_class_options(const struct rte_devargs *devargs, 400a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 401ad435d32SXueming Li { 402ad435d32SXueming Li int ret = 0; 403ad435d32SXueming Li 4045fb0c63dSMichael Baum if (mkvlist == NULL) 405ad435d32SXueming Li return 0; 4065fb0c63dSMichael Baum MLX5_ASSERT(devargs != NULL); 407ad435d32SXueming Li if (devargs->cls != NULL && devargs->cls->name != NULL) 408ad435d32SXueming Li /* Global syntax, only one class type. */ 409ad435d32SXueming Li return class_name_to_value(devargs->cls->name); 410ad435d32SXueming Li /* Legacy devargs support multiple classes. */ 411a729d2f0SMichael Baum rte_kvargs_process(mkvlist->kvlist, RTE_DEVARGS_KEY_CLASS, 412ad435d32SXueming Li devargs_class_handler, &ret); 413ad435d32SXueming Li return ret; 414ad435d32SXueming Li } 415ad435d32SXueming Li 416ad435d32SXueming Li static const unsigned int mlx5_class_invalid_combinations[] = { 417ad435d32SXueming Li MLX5_CLASS_ETH | MLX5_CLASS_VDPA, 418ad435d32SXueming Li /* New class combination should be added here. */ 419ad435d32SXueming Li }; 420ad435d32SXueming Li 421ad435d32SXueming Li static int 422ad435d32SXueming Li is_valid_class_combination(uint32_t user_classes) 423ad435d32SXueming Li { 424ad435d32SXueming Li unsigned int i; 425ad435d32SXueming Li 426ad435d32SXueming Li /* Verify if user specified unsupported combination. */ 427ad435d32SXueming Li for (i = 0; i < RTE_DIM(mlx5_class_invalid_combinations); i++) { 428ad435d32SXueming Li if ((mlx5_class_invalid_combinations[i] & user_classes) == 429ad435d32SXueming Li mlx5_class_invalid_combinations[i]) 430ad435d32SXueming Li return -EINVAL; 431ad435d32SXueming Li } 432ad435d32SXueming Li /* Not found any invalid class combination. */ 433ad435d32SXueming Li return 0; 434ad435d32SXueming Li } 435ad435d32SXueming Li 436ad435d32SXueming Li static bool 437ad435d32SXueming Li mlx5_bus_match(const struct mlx5_class_driver *drv, 438ad435d32SXueming Li const struct rte_device *dev) 439ad435d32SXueming Li { 440ad435d32SXueming Li if (mlx5_dev_is_pci(dev)) 441ad435d32SXueming Li return mlx5_dev_pci_match(drv, dev); 442ad435d32SXueming Li return true; 443ad435d32SXueming Li } 444ad435d32SXueming Li 445ad435d32SXueming Li static struct mlx5_common_device * 446ad435d32SXueming Li to_mlx5_device(const struct rte_device *rte_dev) 447ad435d32SXueming Li { 44885209924SMichael Baum struct mlx5_common_device *cdev; 449ad435d32SXueming Li 45085209924SMichael Baum TAILQ_FOREACH(cdev, &devices_list, next) { 45185209924SMichael Baum if (rte_dev == cdev->dev) 45285209924SMichael Baum return cdev; 453ad435d32SXueming Li } 454ad435d32SXueming Li return NULL; 455ad435d32SXueming Li } 456ad435d32SXueming Li 4574d567938SThomas Monjalon int 4584d567938SThomas Monjalon mlx5_dev_to_pci_str(const struct rte_device *dev, char *addr, size_t size) 4594d567938SThomas Monjalon { 4604d567938SThomas Monjalon struct rte_pci_addr pci_addr = { 0 }; 4614d567938SThomas Monjalon int ret; 4624d567938SThomas Monjalon 4634d567938SThomas Monjalon if (mlx5_dev_is_pci(dev)) { 4644d567938SThomas Monjalon /* Input might be <BDF>, format PCI address to <DBDF>. */ 4654d567938SThomas Monjalon ret = rte_pci_addr_parse(dev->name, &pci_addr); 4664d567938SThomas Monjalon if (ret != 0) 4674d567938SThomas Monjalon return -ENODEV; 4684d567938SThomas Monjalon rte_pci_device_name(&pci_addr, addr, size); 4694d567938SThomas Monjalon return 0; 4704d567938SThomas Monjalon } 4714d567938SThomas Monjalon #ifdef RTE_EXEC_ENV_LINUX 4724d567938SThomas Monjalon return mlx5_auxiliary_get_pci_str(RTE_DEV_TO_AUXILIARY_CONST(dev), 4734d567938SThomas Monjalon addr, size); 4744d567938SThomas Monjalon #else 4754d567938SThomas Monjalon rte_errno = ENODEV; 4764d567938SThomas Monjalon return -rte_errno; 4774d567938SThomas Monjalon #endif 4784d567938SThomas Monjalon } 4794d567938SThomas Monjalon 480ca1418ceSMichael Baum /** 481fc59a1ecSMichael Baum * Register the mempool for the protection domain. 482fc59a1ecSMichael Baum * 483fc59a1ecSMichael Baum * @param cdev 484fc59a1ecSMichael Baum * Pointer to the mlx5 common device. 485fc59a1ecSMichael Baum * @param mp 486fc59a1ecSMichael Baum * Mempool being registered. 487fc59a1ecSMichael Baum * 488fc59a1ecSMichael Baum * @return 489fc59a1ecSMichael Baum * 0 on success, (-1) on failure and rte_errno is set. 490fc59a1ecSMichael Baum */ 491fc59a1ecSMichael Baum static int 492fc59a1ecSMichael Baum mlx5_dev_mempool_register(struct mlx5_common_device *cdev, 49308ac0358SDmitry Kozlyuk struct rte_mempool *mp, bool is_extmem) 494fc59a1ecSMichael Baum { 49508ac0358SDmitry Kozlyuk return mlx5_mr_mempool_register(cdev, mp, is_extmem); 496fc59a1ecSMichael Baum } 497fc59a1ecSMichael Baum 498fc59a1ecSMichael Baum /** 499fc59a1ecSMichael Baum * Unregister the mempool from the protection domain. 500fc59a1ecSMichael Baum * 501fc59a1ecSMichael Baum * @param cdev 502fc59a1ecSMichael Baum * Pointer to the mlx5 common device. 503fc59a1ecSMichael Baum * @param mp 504fc59a1ecSMichael Baum * Mempool being unregistered. 505fc59a1ecSMichael Baum */ 506fc59a1ecSMichael Baum void 507fc59a1ecSMichael Baum mlx5_dev_mempool_unregister(struct mlx5_common_device *cdev, 508fc59a1ecSMichael Baum struct rte_mempool *mp) 509fc59a1ecSMichael Baum { 51020489176SMichael Baum if (mlx5_mr_mempool_unregister(cdev, mp) < 0) 511fc59a1ecSMichael Baum DRV_LOG(WARNING, "Failed to unregister mempool %s for PD %p: %s", 512fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno)); 513fc59a1ecSMichael Baum } 514fc59a1ecSMichael Baum 515fc59a1ecSMichael Baum /** 516fc59a1ecSMichael Baum * rte_mempool_walk() callback to register mempools for the protection domain. 517fc59a1ecSMichael Baum * 518fc59a1ecSMichael Baum * @param mp 519fc59a1ecSMichael Baum * The mempool being walked. 520fc59a1ecSMichael Baum * @param arg 521fc59a1ecSMichael Baum * Pointer to the device shared context. 522fc59a1ecSMichael Baum */ 523fc59a1ecSMichael Baum static void 524fc59a1ecSMichael Baum mlx5_dev_mempool_register_cb(struct rte_mempool *mp, void *arg) 525fc59a1ecSMichael Baum { 526fc59a1ecSMichael Baum struct mlx5_common_device *cdev = arg; 527fc59a1ecSMichael Baum int ret; 528fc59a1ecSMichael Baum 52908ac0358SDmitry Kozlyuk ret = mlx5_dev_mempool_register(cdev, mp, false); 530fc59a1ecSMichael Baum if (ret < 0 && rte_errno != EEXIST) 531fc59a1ecSMichael Baum DRV_LOG(ERR, 532fc59a1ecSMichael Baum "Failed to register existing mempool %s for PD %p: %s", 533fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno)); 534fc59a1ecSMichael Baum } 535fc59a1ecSMichael Baum 536fc59a1ecSMichael Baum /** 537fc59a1ecSMichael Baum * rte_mempool_walk() callback to unregister mempools 538fc59a1ecSMichael Baum * from the protection domain. 539fc59a1ecSMichael Baum * 540fc59a1ecSMichael Baum * @param mp 541fc59a1ecSMichael Baum * The mempool being walked. 542fc59a1ecSMichael Baum * @param arg 543fc59a1ecSMichael Baum * Pointer to the device shared context. 544fc59a1ecSMichael Baum */ 545fc59a1ecSMichael Baum static void 546fc59a1ecSMichael Baum mlx5_dev_mempool_unregister_cb(struct rte_mempool *mp, void *arg) 547fc59a1ecSMichael Baum { 548fc59a1ecSMichael Baum mlx5_dev_mempool_unregister((struct mlx5_common_device *)arg, mp); 549fc59a1ecSMichael Baum } 550fc59a1ecSMichael Baum 551fc59a1ecSMichael Baum /** 552fc59a1ecSMichael Baum * Mempool life cycle callback for mlx5 common devices. 553fc59a1ecSMichael Baum * 554fc59a1ecSMichael Baum * @param event 555fc59a1ecSMichael Baum * Mempool life cycle event. 556fc59a1ecSMichael Baum * @param mp 557fc59a1ecSMichael Baum * Associated mempool. 558fc59a1ecSMichael Baum * @param arg 559fc59a1ecSMichael Baum * Pointer to a device shared context. 560fc59a1ecSMichael Baum */ 561fc59a1ecSMichael Baum static void 562fc59a1ecSMichael Baum mlx5_dev_mempool_event_cb(enum rte_mempool_event event, struct rte_mempool *mp, 563fc59a1ecSMichael Baum void *arg) 564fc59a1ecSMichael Baum { 565fc59a1ecSMichael Baum struct mlx5_common_device *cdev = arg; 566fc59a1ecSMichael Baum 567fc59a1ecSMichael Baum switch (event) { 568fc59a1ecSMichael Baum case RTE_MEMPOOL_EVENT_READY: 56908ac0358SDmitry Kozlyuk if (mlx5_dev_mempool_register(cdev, mp, false) < 0) 570fc59a1ecSMichael Baum DRV_LOG(ERR, 571fc59a1ecSMichael Baum "Failed to register new mempool %s for PD %p: %s", 572fc59a1ecSMichael Baum mp->name, cdev->pd, rte_strerror(rte_errno)); 573fc59a1ecSMichael Baum break; 574fc59a1ecSMichael Baum case RTE_MEMPOOL_EVENT_DESTROY: 575fc59a1ecSMichael Baum mlx5_dev_mempool_unregister(cdev, mp); 576fc59a1ecSMichael Baum break; 577fc59a1ecSMichael Baum } 578fc59a1ecSMichael Baum } 579fc59a1ecSMichael Baum 580*aeca11f8SGregory Etelson /** 581*aeca11f8SGregory Etelson * Primary and secondary processes share the `cdev` pointer. 582*aeca11f8SGregory Etelson * Callbacks addresses are local in each process. 583*aeca11f8SGregory Etelson * Therefore, each process can register private callbacks. 584*aeca11f8SGregory Etelson */ 585fc59a1ecSMichael Baum int 586fc59a1ecSMichael Baum mlx5_dev_mempool_subscribe(struct mlx5_common_device *cdev) 587fc59a1ecSMichael Baum { 588fc59a1ecSMichael Baum int ret = 0; 589fc59a1ecSMichael Baum 590fc59a1ecSMichael Baum if (!cdev->config.mr_mempool_reg_en) 591fc59a1ecSMichael Baum return 0; 592fc59a1ecSMichael Baum rte_rwlock_write_lock(&cdev->mr_scache.mprwlock); 593fc59a1ecSMichael Baum /* Callback for this device may be already registered. */ 594fc59a1ecSMichael Baum ret = rte_mempool_event_callback_register(mlx5_dev_mempool_event_cb, 595fc59a1ecSMichael Baum cdev); 596fc59a1ecSMichael Baum /* Register mempools only once for this device. */ 597*aeca11f8SGregory Etelson if (ret == 0 && rte_eal_process_type() == RTE_PROC_PRIMARY) { 598fc59a1ecSMichael Baum rte_mempool_walk(mlx5_dev_mempool_register_cb, cdev); 599*aeca11f8SGregory Etelson goto exit; 600*aeca11f8SGregory Etelson } 601*aeca11f8SGregory Etelson if (ret != 0 && rte_errno == EEXIST) 602fc59a1ecSMichael Baum ret = 0; 603fc59a1ecSMichael Baum exit: 604fc59a1ecSMichael Baum rte_rwlock_write_unlock(&cdev->mr_scache.mprwlock); 605fc59a1ecSMichael Baum return ret; 606fc59a1ecSMichael Baum } 607fc59a1ecSMichael Baum 608fc59a1ecSMichael Baum static void 609fc59a1ecSMichael Baum mlx5_dev_mempool_unsubscribe(struct mlx5_common_device *cdev) 610fc59a1ecSMichael Baum { 611fc59a1ecSMichael Baum int ret; 612fc59a1ecSMichael Baum 613*aeca11f8SGregory Etelson MLX5_ASSERT(cdev->dev != NULL); 6148ad97e4bSDmitry Kozlyuk if (!cdev->config.mr_mempool_reg_en) 6158ad97e4bSDmitry Kozlyuk return; 616fc59a1ecSMichael Baum /* Stop watching for mempool events and unregister all mempools. */ 617fc59a1ecSMichael Baum ret = rte_mempool_event_callback_unregister(mlx5_dev_mempool_event_cb, 618fc59a1ecSMichael Baum cdev); 619fc59a1ecSMichael Baum if (ret == 0) 620fc59a1ecSMichael Baum rte_mempool_walk(mlx5_dev_mempool_unregister_cb, cdev); 621fc59a1ecSMichael Baum } 622fc59a1ecSMichael Baum 623fc59a1ecSMichael Baum /** 6249f1d636fSMichael Baum * Callback for memory event. 6259f1d636fSMichael Baum * 6269f1d636fSMichael Baum * @param event_type 6279f1d636fSMichael Baum * Memory event type. 6289f1d636fSMichael Baum * @param addr 6299f1d636fSMichael Baum * Address of memory. 6309f1d636fSMichael Baum * @param len 6319f1d636fSMichael Baum * Size of memory. 6329f1d636fSMichael Baum */ 6339f1d636fSMichael Baum static void 6349f1d636fSMichael Baum mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr, 6359f1d636fSMichael Baum size_t len, void *arg __rte_unused) 6369f1d636fSMichael Baum { 6379f1d636fSMichael Baum struct mlx5_common_device *cdev; 6389f1d636fSMichael Baum 6399f1d636fSMichael Baum /* Must be called from the primary process. */ 6409f1d636fSMichael Baum MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY); 6419f1d636fSMichael Baum switch (event_type) { 6429f1d636fSMichael Baum case RTE_MEM_EVENT_FREE: 6439f1d636fSMichael Baum pthread_mutex_lock(&devices_list_lock); 6449f1d636fSMichael Baum /* Iterate all the existing mlx5 devices. */ 6459f1d636fSMichael Baum TAILQ_FOREACH(cdev, &devices_list, next) 6469f1d636fSMichael Baum mlx5_free_mr_by_addr(&cdev->mr_scache, 6479f1d636fSMichael Baum mlx5_os_get_ctx_device_name 6489f1d636fSMichael Baum (cdev->ctx), 6499f1d636fSMichael Baum addr, len); 6509f1d636fSMichael Baum pthread_mutex_unlock(&devices_list_lock); 6519f1d636fSMichael Baum break; 6529f1d636fSMichael Baum case RTE_MEM_EVENT_ALLOC: 6539f1d636fSMichael Baum default: 6549f1d636fSMichael Baum break; 6559f1d636fSMichael Baum } 6569f1d636fSMichael Baum } 6579f1d636fSMichael Baum 6589f1d636fSMichael Baum /** 659ca1418ceSMichael Baum * Uninitialize all HW global of device context. 660ca1418ceSMichael Baum * 661ca1418ceSMichael Baum * @param cdev 662ca1418ceSMichael Baum * Pointer to mlx5 device structure. 663ca1418ceSMichael Baum * 664ca1418ceSMichael Baum * @return 665ca1418ceSMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 666ca1418ceSMichael Baum */ 667ca1418ceSMichael Baum static void 668ca1418ceSMichael Baum mlx5_dev_hw_global_release(struct mlx5_common_device *cdev) 669ca1418ceSMichael Baum { 670e35ccf24SMichael Baum if (cdev->pd != NULL) { 6719d936f4fSMichael Baum claim_zero(mlx5_os_pd_release(cdev)); 672e35ccf24SMichael Baum cdev->pd = NULL; 673e35ccf24SMichael Baum } 674ca1418ceSMichael Baum if (cdev->ctx != NULL) { 675ca1418ceSMichael Baum claim_zero(mlx5_glue->close_device(cdev->ctx)); 676ca1418ceSMichael Baum cdev->ctx = NULL; 677ca1418ceSMichael Baum } 678ca1418ceSMichael Baum } 679ca1418ceSMichael Baum 680ca1418ceSMichael Baum /** 681ca1418ceSMichael Baum * Initialize all HW global of device context. 682ca1418ceSMichael Baum * 683ca1418ceSMichael Baum * @param cdev 684ca1418ceSMichael Baum * Pointer to mlx5 device structure. 685ca1418ceSMichael Baum * @param classes 686ca1418ceSMichael Baum * Chosen classes come from user device arguments. 687ca1418ceSMichael Baum * 688ca1418ceSMichael Baum * @return 689ca1418ceSMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 690ca1418ceSMichael Baum */ 691ca1418ceSMichael Baum static int 692ca1418ceSMichael Baum mlx5_dev_hw_global_prepare(struct mlx5_common_device *cdev, uint32_t classes) 693ca1418ceSMichael Baum { 694ca1418ceSMichael Baum int ret; 695ca1418ceSMichael Baum 696ca1418ceSMichael Baum /* Create context device */ 697ca1418ceSMichael Baum ret = mlx5_os_open_device(cdev, classes); 698ca1418ceSMichael Baum if (ret < 0) 699ca1418ceSMichael Baum return ret; 7009d936f4fSMichael Baum /* 7019d936f4fSMichael Baum * When CTX is created by Verbs, query HCA attribute is unsupported. 7029d936f4fSMichael Baum * When CTX is imported, we cannot know if it is created by DevX or 7039d936f4fSMichael Baum * Verbs. So, we use query HCA attribute function to check it. 7049d936f4fSMichael Baum */ 7059d936f4fSMichael Baum if (cdev->config.devx || cdev->config.device_fd != MLX5_ARG_UNSET) { 706fe46b20cSMichael Baum /* Query HCA attributes. */ 7079d936f4fSMichael Baum ret = mlx5_devx_cmd_query_hca_attr(cdev->ctx, 7089d936f4fSMichael Baum &cdev->config.hca_attr); 709fe46b20cSMichael Baum if (ret) { 7109d936f4fSMichael Baum DRV_LOG(ERR, "Unable to read HCA caps in DevX mode."); 711fe46b20cSMichael Baum rte_errno = ENOTSUP; 712fe46b20cSMichael Baum goto error; 713fe46b20cSMichael Baum } 7149d936f4fSMichael Baum cdev->config.devx = 1; 7159d936f4fSMichael Baum } 7169d936f4fSMichael Baum DRV_LOG(DEBUG, "DevX is %ssupported.", cdev->config.devx ? "" : "NOT "); 7179d936f4fSMichael Baum /* Prepare Protection Domain object and extract its pdn. */ 7189d936f4fSMichael Baum ret = mlx5_os_pd_prepare(cdev); 7199d936f4fSMichael Baum if (ret) 7209d936f4fSMichael Baum goto error; 721ca1418ceSMichael Baum return 0; 722e35ccf24SMichael Baum error: 723e35ccf24SMichael Baum mlx5_dev_hw_global_release(cdev); 724e35ccf24SMichael Baum return ret; 725ca1418ceSMichael Baum } 726ca1418ceSMichael Baum 727ad435d32SXueming Li static void 72885209924SMichael Baum mlx5_common_dev_release(struct mlx5_common_device *cdev) 729ad435d32SXueming Li { 730dc26c9c2SMichael Baum pthread_mutex_lock(&devices_list_lock); 73185209924SMichael Baum TAILQ_REMOVE(&devices_list, cdev, next); 732dc26c9c2SMichael Baum pthread_mutex_unlock(&devices_list_lock); 7339f1d636fSMichael Baum if (rte_eal_process_type() == RTE_PROC_PRIMARY) { 7349f1d636fSMichael Baum if (TAILQ_EMPTY(&devices_list)) 7359f1d636fSMichael Baum rte_mem_event_callback_unregister("MLX5_MEM_EVENT_CB", 7369f1d636fSMichael Baum NULL); 737fc59a1ecSMichael Baum mlx5_dev_mempool_unsubscribe(cdev); 7389f1d636fSMichael Baum mlx5_mr_release_cache(&cdev->mr_scache); 739ca1418ceSMichael Baum mlx5_dev_hw_global_release(cdev); 7409f1d636fSMichael Baum } 74185209924SMichael Baum rte_free(cdev); 74285209924SMichael Baum } 74385209924SMichael Baum 74485209924SMichael Baum static struct mlx5_common_device * 745a729d2f0SMichael Baum mlx5_common_dev_create(struct rte_device *eal_dev, uint32_t classes, 746a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 74785209924SMichael Baum { 74885209924SMichael Baum struct mlx5_common_device *cdev; 74985209924SMichael Baum int ret; 75085209924SMichael Baum 75185209924SMichael Baum cdev = rte_zmalloc("mlx5_common_device", sizeof(*cdev), 0); 75285209924SMichael Baum if (!cdev) { 75385209924SMichael Baum DRV_LOG(ERR, "Device allocation failure."); 75485209924SMichael Baum rte_errno = ENOMEM; 75585209924SMichael Baum return NULL; 75685209924SMichael Baum } 75785209924SMichael Baum cdev->dev = eal_dev; 75885209924SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY) 75985209924SMichael Baum goto exit; 76085209924SMichael Baum /* Parse device parameters. */ 761a729d2f0SMichael Baum ret = mlx5_common_config_get(mkvlist, &cdev->config); 76285209924SMichael Baum if (ret < 0) { 76385209924SMichael Baum DRV_LOG(ERR, "Failed to process device arguments: %s", 76485209924SMichael Baum strerror(rte_errno)); 76585209924SMichael Baum rte_free(cdev); 76685209924SMichael Baum return NULL; 76785209924SMichael Baum } 76885209924SMichael Baum mlx5_malloc_mem_select(cdev->config.sys_mem_en); 769ca1418ceSMichael Baum /* Initialize all HW global of device context. */ 770ca1418ceSMichael Baum ret = mlx5_dev_hw_global_prepare(cdev, classes); 771ca1418ceSMichael Baum if (ret) { 772ca1418ceSMichael Baum DRV_LOG(ERR, "Failed to initialize device context."); 773ca1418ceSMichael Baum rte_free(cdev); 774ca1418ceSMichael Baum return NULL; 775ca1418ceSMichael Baum } 7769f1d636fSMichael Baum /* Initialize global MR cache resources and update its functions. */ 7779f1d636fSMichael Baum ret = mlx5_mr_create_cache(&cdev->mr_scache, eal_dev->numa_node); 7789f1d636fSMichael Baum if (ret) { 7799f1d636fSMichael Baum DRV_LOG(ERR, "Failed to initialize global MR share cache."); 7809f1d636fSMichael Baum mlx5_dev_hw_global_release(cdev); 7819f1d636fSMichael Baum rte_free(cdev); 7829f1d636fSMichael Baum return NULL; 7839f1d636fSMichael Baum } 7849f1d636fSMichael Baum /* Register callback function for global shared MR cache management. */ 7859f1d636fSMichael Baum if (TAILQ_EMPTY(&devices_list)) 7869f1d636fSMichael Baum rte_mem_event_callback_register("MLX5_MEM_EVENT_CB", 7879f1d636fSMichael Baum mlx5_mr_mem_event_cb, NULL); 78885209924SMichael Baum exit: 78985209924SMichael Baum pthread_mutex_lock(&devices_list_lock); 79085209924SMichael Baum TAILQ_INSERT_HEAD(&devices_list, cdev, next); 79185209924SMichael Baum pthread_mutex_unlock(&devices_list_lock); 79285209924SMichael Baum return cdev; 793ad435d32SXueming Li } 794ad435d32SXueming Li 795c089eb93SMichael Baum /** 796c089eb93SMichael Baum * Validate common devargs when probing again. 797c089eb93SMichael Baum * 798c089eb93SMichael Baum * When common device probing again, it cannot change its configurations. 799c089eb93SMichael Baum * If user ask non compatible configurations in devargs, it is error. 800c089eb93SMichael Baum * This function checks the match between: 801c089eb93SMichael Baum * - Common device configurations requested by probe again devargs. 802c089eb93SMichael Baum * - Existing common device configurations. 803c089eb93SMichael Baum * 804c089eb93SMichael Baum * @param cdev 805c089eb93SMichael Baum * Pointer to mlx5 device structure. 806a729d2f0SMichael Baum * @param mkvlist 807a729d2f0SMichael Baum * Pointer to mlx5 kvargs control, can be NULL if there is no devargs. 808c089eb93SMichael Baum * 809c089eb93SMichael Baum * @return 810c089eb93SMichael Baum * 0 on success, a negative errno value otherwise and rte_errno is set. 811c089eb93SMichael Baum */ 812c089eb93SMichael Baum static int 813a729d2f0SMichael Baum mlx5_common_probe_again_args_validate(struct mlx5_common_device *cdev, 814a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 815c089eb93SMichael Baum { 816c089eb93SMichael Baum struct mlx5_common_dev_config *config; 817c089eb93SMichael Baum int ret; 818c089eb93SMichael Baum 819c089eb93SMichael Baum /* Secondary process should not handle devargs. */ 820c089eb93SMichael Baum if (rte_eal_process_type() != RTE_PROC_PRIMARY) 821c089eb93SMichael Baum return 0; 822c089eb93SMichael Baum /* Probe again doesn't have to generate devargs. */ 823a729d2f0SMichael Baum if (mkvlist == NULL) 824c089eb93SMichael Baum return 0; 825c089eb93SMichael Baum config = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE, 826c089eb93SMichael Baum sizeof(struct mlx5_common_dev_config), 827c089eb93SMichael Baum RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY); 828c089eb93SMichael Baum if (config == NULL) { 829c089eb93SMichael Baum rte_errno = -ENOMEM; 830c089eb93SMichael Baum return -rte_errno; 831c089eb93SMichael Baum } 832c089eb93SMichael Baum /* 833c089eb93SMichael Baum * Creates a temporary common configure structure according to new 834c089eb93SMichael Baum * devargs attached in probing again. 835c089eb93SMichael Baum */ 836a729d2f0SMichael Baum ret = mlx5_common_config_get(mkvlist, config); 837c089eb93SMichael Baum if (ret) { 838c089eb93SMichael Baum DRV_LOG(ERR, "Failed to process device configure: %s", 839c089eb93SMichael Baum strerror(rte_errno)); 840c089eb93SMichael Baum mlx5_free(config); 841c089eb93SMichael Baum return ret; 842c089eb93SMichael Baum } 843c089eb93SMichael Baum /* 844c089eb93SMichael Baum * Checks the match between the temporary structure and the existing 845c089eb93SMichael Baum * common device structure. 846c089eb93SMichael Baum */ 8479d936f4fSMichael Baum if (cdev->config.mr_ext_memseg_en != config->mr_ext_memseg_en) { 8489d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_MR_EXT_MEMSEG_EN "\" " 849c089eb93SMichael Baum "configuration mismatch for device %s.", 850c089eb93SMichael Baum cdev->dev->name); 851c089eb93SMichael Baum goto error; 852c089eb93SMichael Baum } 8539d936f4fSMichael Baum if (cdev->config.mr_mempool_reg_en != config->mr_mempool_reg_en) { 8549d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_MR_MEMPOOL_REG_EN "\" " 855c089eb93SMichael Baum "configuration mismatch for device %s.", 856c089eb93SMichael Baum cdev->dev->name); 857c089eb93SMichael Baum goto error; 858c089eb93SMichael Baum } 8599d936f4fSMichael Baum if (cdev->config.device_fd != config->device_fd) { 8609d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_DEVICE_FD "\" " 8619d936f4fSMichael Baum "configuration mismatch for device %s.", 862c089eb93SMichael Baum cdev->dev->name); 863c089eb93SMichael Baum goto error; 864c089eb93SMichael Baum } 8659d936f4fSMichael Baum if (cdev->config.pd_handle != config->pd_handle) { 8669d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_PD_HANDLE "\" " 8679d936f4fSMichael Baum "configuration mismatch for device %s.", 8689d936f4fSMichael Baum cdev->dev->name); 8699d936f4fSMichael Baum goto error; 8709d936f4fSMichael Baum } 8719d936f4fSMichael Baum if (cdev->config.sys_mem_en != config->sys_mem_en) { 8729d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_SYS_MEM_EN "\" " 8739d936f4fSMichael Baum "configuration mismatch for device %s.", 8749d936f4fSMichael Baum cdev->dev->name); 8759d936f4fSMichael Baum goto error; 8769d936f4fSMichael Baum } 8779d936f4fSMichael Baum if (cdev->config.dbnc != config->dbnc) { 8789d936f4fSMichael Baum DRV_LOG(ERR, "\"" MLX5_SQ_DB_NC "\" " 8799d936f4fSMichael Baum "configuration mismatch for device %s.", 880c089eb93SMichael Baum cdev->dev->name); 881c089eb93SMichael Baum goto error; 882c089eb93SMichael Baum } 883c089eb93SMichael Baum mlx5_free(config); 884c089eb93SMichael Baum return 0; 885c089eb93SMichael Baum error: 886c089eb93SMichael Baum mlx5_free(config); 887c089eb93SMichael Baum rte_errno = EINVAL; 888c089eb93SMichael Baum return -rte_errno; 889c089eb93SMichael Baum } 890c089eb93SMichael Baum 891ad435d32SXueming Li static int 89285209924SMichael Baum drivers_remove(struct mlx5_common_device *cdev, uint32_t enabled_classes) 893ad435d32SXueming Li { 894ad435d32SXueming Li struct mlx5_class_driver *driver; 895ad435d32SXueming Li int local_ret = -ENODEV; 896ad435d32SXueming Li unsigned int i = 0; 897ad435d32SXueming Li int ret = 0; 898ad435d32SXueming Li 899ad435d32SXueming Li while (enabled_classes) { 900ad435d32SXueming Li driver = driver_get(RTE_BIT64(i)); 901ad435d32SXueming Li if (driver != NULL) { 90285209924SMichael Baum local_ret = driver->remove(cdev); 903ad435d32SXueming Li if (local_ret == 0) 90485209924SMichael Baum cdev->classes_loaded &= ~RTE_BIT64(i); 905ad435d32SXueming Li else if (ret == 0) 906ad435d32SXueming Li ret = local_ret; 907ad435d32SXueming Li } 908ad435d32SXueming Li enabled_classes &= ~RTE_BIT64(i); 909ad435d32SXueming Li i++; 910ad435d32SXueming Li } 911ad435d32SXueming Li if (local_ret != 0 && ret == 0) 912ad435d32SXueming Li ret = local_ret; 913ad435d32SXueming Li return ret; 914ad435d32SXueming Li } 915ad435d32SXueming Li 916ad435d32SXueming Li static int 917a729d2f0SMichael Baum drivers_probe(struct mlx5_common_device *cdev, uint32_t user_classes, 918a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist) 919ad435d32SXueming Li { 920ad435d32SXueming Li struct mlx5_class_driver *driver; 921ad435d32SXueming Li uint32_t enabled_classes = 0; 922ad435d32SXueming Li bool already_loaded; 923b4a4159dSBing Zhao int ret = -EINVAL; 924ad435d32SXueming Li 925ad435d32SXueming Li TAILQ_FOREACH(driver, &drivers_list, next) { 926ad435d32SXueming Li if ((driver->drv_class & user_classes) == 0) 927ad435d32SXueming Li continue; 92885209924SMichael Baum if (!mlx5_bus_match(driver, cdev->dev)) 929ad435d32SXueming Li continue; 93085209924SMichael Baum already_loaded = cdev->classes_loaded & driver->drv_class; 931ad435d32SXueming Li if (already_loaded && driver->probe_again == 0) { 932ad435d32SXueming Li DRV_LOG(ERR, "Device %s is already probed", 93385209924SMichael Baum cdev->dev->name); 934ad435d32SXueming Li ret = -EEXIST; 935ad435d32SXueming Li goto probe_err; 936ad435d32SXueming Li } 937a729d2f0SMichael Baum ret = driver->probe(cdev, mkvlist); 938ad435d32SXueming Li if (ret < 0) { 939ad435d32SXueming Li DRV_LOG(ERR, "Failed to load driver %s", 940ad435d32SXueming Li driver->name); 941ad435d32SXueming Li goto probe_err; 942ad435d32SXueming Li } 943ad435d32SXueming Li enabled_classes |= driver->drv_class; 944ad435d32SXueming Li } 945b4a4159dSBing Zhao if (!ret) { 94685209924SMichael Baum cdev->classes_loaded |= enabled_classes; 947ad435d32SXueming Li return 0; 948b4a4159dSBing Zhao } 949ad435d32SXueming Li probe_err: 9508928997aSMichael Baum /* 9518928997aSMichael Baum * Need to remove only drivers which were not probed before this probe 9528928997aSMichael Baum * instance, but have already been probed before this failure. 953ad435d32SXueming Li */ 9548928997aSMichael Baum enabled_classes &= ~cdev->classes_loaded; 95585209924SMichael Baum drivers_remove(cdev, enabled_classes); 956ad435d32SXueming Li return ret; 957ad435d32SXueming Li } 958ad435d32SXueming Li 959ad435d32SXueming Li int 960ad435d32SXueming Li mlx5_common_dev_probe(struct rte_device *eal_dev) 961ad435d32SXueming Li { 96285209924SMichael Baum struct mlx5_common_device *cdev; 963a729d2f0SMichael Baum struct mlx5_kvargs_ctrl mkvlist; 964a729d2f0SMichael Baum struct mlx5_kvargs_ctrl *mkvlist_p = NULL; 965ad435d32SXueming Li uint32_t classes = 0; 966ad435d32SXueming Li bool new_device = false; 967ad435d32SXueming Li int ret; 968ad435d32SXueming Li 969ad435d32SXueming Li DRV_LOG(INFO, "probe device \"%s\".", eal_dev->name); 9705fb0c63dSMichael Baum if (eal_dev->devargs != NULL && eal_dev->devargs->args != NULL) 971a729d2f0SMichael Baum mkvlist_p = &mkvlist; 972a729d2f0SMichael Baum ret = mlx5_kvargs_prepare(mkvlist_p, eal_dev->devargs); 973a729d2f0SMichael Baum if (ret < 0) { 974a729d2f0SMichael Baum DRV_LOG(ERR, "Unsupported device arguments: %s", 975a729d2f0SMichael Baum eal_dev->devargs->args); 976a729d2f0SMichael Baum return ret; 977a729d2f0SMichael Baum } 978a729d2f0SMichael Baum ret = parse_class_options(eal_dev->devargs, mkvlist_p); 979ad435d32SXueming Li if (ret < 0) { 980ad435d32SXueming Li DRV_LOG(ERR, "Unsupported mlx5 class type: %s", 981ad435d32SXueming Li eal_dev->devargs->args); 982a729d2f0SMichael Baum goto class_err; 983ad435d32SXueming Li } 984ad435d32SXueming Li classes = ret; 985ad435d32SXueming Li if (classes == 0) 986ad435d32SXueming Li /* Default to net class. */ 987ad435d32SXueming Li classes = MLX5_CLASS_ETH; 988c089eb93SMichael Baum /* 989c089eb93SMichael Baum * MLX5 common driver supports probing again in two scenarios: 990c089eb93SMichael Baum * - Add new driver under existing common device (regardless of the 991c089eb93SMichael Baum * driver's own support in probing again). 992c089eb93SMichael Baum * - Transfer the probing again support of the drivers themselves. 993c089eb93SMichael Baum * 994c089eb93SMichael Baum * In both scenarios it uses in the existing device. here it looks for 995c089eb93SMichael Baum * device that match to rte device, if it exists, the request classes 996c089eb93SMichael Baum * were probed with this device. 997c089eb93SMichael Baum */ 99885209924SMichael Baum cdev = to_mlx5_device(eal_dev); 99985209924SMichael Baum if (!cdev) { 1000c089eb93SMichael Baum /* It isn't probing again, creates a new device. */ 1001a729d2f0SMichael Baum cdev = mlx5_common_dev_create(eal_dev, classes, mkvlist_p); 1002a729d2f0SMichael Baum if (!cdev) { 1003a729d2f0SMichael Baum ret = -ENOMEM; 1004a729d2f0SMichael Baum goto class_err; 1005a729d2f0SMichael Baum } 1006ad435d32SXueming Li new_device = true; 1007c089eb93SMichael Baum } else { 1008c089eb93SMichael Baum /* It is probing again, validate common devargs match. */ 1009a729d2f0SMichael Baum ret = mlx5_common_probe_again_args_validate(cdev, mkvlist_p); 1010c089eb93SMichael Baum if (ret) { 1011c089eb93SMichael Baum DRV_LOG(ERR, 1012c089eb93SMichael Baum "Probe again parameters aren't compatible : %s", 1013c089eb93SMichael Baum strerror(rte_errno)); 1014a729d2f0SMichael Baum goto class_err; 1015c089eb93SMichael Baum } 1016288d7c3fSMichael Baum } 1017288d7c3fSMichael Baum /* 1018288d7c3fSMichael Baum * Validate combination here. 1019288d7c3fSMichael Baum * For new device, the classes_loaded field is 0 and it check only 1020288d7c3fSMichael Baum * the classes given as user device arguments. 1021288d7c3fSMichael Baum */ 102285209924SMichael Baum ret = is_valid_class_combination(classes | cdev->classes_loaded); 1023ad435d32SXueming Li if (ret != 0) { 1024ad435d32SXueming Li DRV_LOG(ERR, "Unsupported mlx5 classes combination."); 1025288d7c3fSMichael Baum goto class_err; 1026ad435d32SXueming Li } 1027a729d2f0SMichael Baum ret = drivers_probe(cdev, classes, mkvlist_p); 1028ad435d32SXueming Li if (ret) 1029ad435d32SXueming Li goto class_err; 1030a729d2f0SMichael Baum /* 1031a729d2f0SMichael Baum * Validate that all devargs have been used, unused key -> unknown Key. 1032a729d2f0SMichael Baum * When probe again validate is failed, the added drivers aren't removed 1033a729d2f0SMichael Baum * here but when device is released. 1034a729d2f0SMichael Baum */ 1035a729d2f0SMichael Baum ret = mlx5_kvargs_validate(mkvlist_p); 1036a729d2f0SMichael Baum if (ret) 1037a729d2f0SMichael Baum goto class_err; 1038a729d2f0SMichael Baum mlx5_kvargs_release(mkvlist_p); 1039ad435d32SXueming Li return 0; 1040ad435d32SXueming Li class_err: 1041a729d2f0SMichael Baum if (new_device) { 1042a729d2f0SMichael Baum /* 1043a729d2f0SMichael Baum * For new device, classes_loaded is always 0 before 1044a729d2f0SMichael Baum * drivers_probe function. 1045a729d2f0SMichael Baum */ 1046a729d2f0SMichael Baum if (cdev->classes_loaded) 1047a729d2f0SMichael Baum drivers_remove(cdev, cdev->classes_loaded); 104885209924SMichael Baum mlx5_common_dev_release(cdev); 1049a729d2f0SMichael Baum } 1050a729d2f0SMichael Baum mlx5_kvargs_release(mkvlist_p); 1051ad435d32SXueming Li return ret; 1052ad435d32SXueming Li } 1053ad435d32SXueming Li 1054ad435d32SXueming Li int 1055ad435d32SXueming Li mlx5_common_dev_remove(struct rte_device *eal_dev) 1056ad435d32SXueming Li { 105785209924SMichael Baum struct mlx5_common_device *cdev; 1058ad435d32SXueming Li int ret; 1059ad435d32SXueming Li 106085209924SMichael Baum cdev = to_mlx5_device(eal_dev); 106185209924SMichael Baum if (!cdev) 1062ad435d32SXueming Li return -ENODEV; 1063ad435d32SXueming Li /* Matching device found, cleanup and unload drivers. */ 106485209924SMichael Baum ret = drivers_remove(cdev, cdev->classes_loaded); 1065dffae63dSMichael Baum if (ret == 0) 106685209924SMichael Baum mlx5_common_dev_release(cdev); 1067ad435d32SXueming Li return ret; 1068ad435d32SXueming Li } 1069ad435d32SXueming Li 1070a5d06c90SMichael Baum /** 1071a5d06c90SMichael Baum * Callback to DMA map external memory to a device. 1072a5d06c90SMichael Baum * 1073a5d06c90SMichael Baum * @param rte_dev 1074a5d06c90SMichael Baum * Pointer to the generic device. 1075a5d06c90SMichael Baum * @param addr 1076a5d06c90SMichael Baum * Starting virtual address of memory to be mapped. 1077a5d06c90SMichael Baum * @param iova 1078a5d06c90SMichael Baum * Starting IOVA address of memory to be mapped. 1079a5d06c90SMichael Baum * @param len 1080a5d06c90SMichael Baum * Length of memory segment being mapped. 1081a5d06c90SMichael Baum * 1082a5d06c90SMichael Baum * @return 1083a5d06c90SMichael Baum * 0 on success, negative value on error. 1084a5d06c90SMichael Baum */ 1085ad435d32SXueming Li int 1086a5d06c90SMichael Baum mlx5_common_dev_dma_map(struct rte_device *rte_dev, void *addr, 1087a5d06c90SMichael Baum uint64_t iova __rte_unused, size_t len) 1088ad435d32SXueming Li { 1089a5d06c90SMichael Baum struct mlx5_common_device *dev; 1090e96d3d02SDmitry Kozlyuk struct mlx5_mr_btree *bt; 1091a5d06c90SMichael Baum struct mlx5_mr *mr; 1092ad435d32SXueming Li 1093a5d06c90SMichael Baum dev = to_mlx5_device(rte_dev); 1094a5d06c90SMichael Baum if (!dev) { 1095a5d06c90SMichael Baum DRV_LOG(WARNING, 1096a5d06c90SMichael Baum "Unable to find matching mlx5 device to device %s", 1097a5d06c90SMichael Baum rte_dev->name); 1098a5d06c90SMichael Baum rte_errno = ENODEV; 1099a5d06c90SMichael Baum return -1; 1100ad435d32SXueming Li } 1101a5d06c90SMichael Baum mr = mlx5_create_mr_ext(dev->pd, (uintptr_t)addr, len, 1102a5d06c90SMichael Baum SOCKET_ID_ANY, dev->mr_scache.reg_mr_cb); 1103a5d06c90SMichael Baum if (!mr) { 1104a5d06c90SMichael Baum DRV_LOG(WARNING, "Device %s unable to DMA map", rte_dev->name); 1105a5d06c90SMichael Baum rte_errno = EINVAL; 1106a5d06c90SMichael Baum return -1; 1107ad435d32SXueming Li } 1108e96d3d02SDmitry Kozlyuk try_insert: 1109a5d06c90SMichael Baum rte_rwlock_write_lock(&dev->mr_scache.rwlock); 1110e96d3d02SDmitry Kozlyuk bt = &dev->mr_scache.cache; 1111e96d3d02SDmitry Kozlyuk if (bt->len == bt->size) { 1112e96d3d02SDmitry Kozlyuk uint32_t size; 1113e96d3d02SDmitry Kozlyuk int ret; 1114e96d3d02SDmitry Kozlyuk 1115e96d3d02SDmitry Kozlyuk size = bt->size + 1; 1116e96d3d02SDmitry Kozlyuk MLX5_ASSERT(size > bt->size); 1117e96d3d02SDmitry Kozlyuk /* 1118e96d3d02SDmitry Kozlyuk * Avoid deadlock (numbers show the sequence of events): 1119e96d3d02SDmitry Kozlyuk * mlx5_mr_create_primary(): 1120e96d3d02SDmitry Kozlyuk * 1) take EAL memory lock 1121e96d3d02SDmitry Kozlyuk * 3) take MR lock 1122e96d3d02SDmitry Kozlyuk * this function: 1123e96d3d02SDmitry Kozlyuk * 2) take MR lock 1124e96d3d02SDmitry Kozlyuk * 4) take EAL memory lock while allocating the new cache 1125e96d3d02SDmitry Kozlyuk * Releasing the MR lock before step 4 1126e96d3d02SDmitry Kozlyuk * allows another thread to execute step 3. 1127e96d3d02SDmitry Kozlyuk */ 1128e96d3d02SDmitry Kozlyuk rte_rwlock_write_unlock(&dev->mr_scache.rwlock); 1129e96d3d02SDmitry Kozlyuk ret = mlx5_mr_expand_cache(&dev->mr_scache, size, 1130e96d3d02SDmitry Kozlyuk rte_dev->numa_node); 1131e96d3d02SDmitry Kozlyuk if (ret < 0) { 1132e96d3d02SDmitry Kozlyuk mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb); 1133e96d3d02SDmitry Kozlyuk rte_errno = ret; 1134e96d3d02SDmitry Kozlyuk return -1; 1135e96d3d02SDmitry Kozlyuk } 1136e96d3d02SDmitry Kozlyuk goto try_insert; 1137e96d3d02SDmitry Kozlyuk } 1138a5d06c90SMichael Baum LIST_INSERT_HEAD(&dev->mr_scache.mr_list, mr, mr); 1139a5d06c90SMichael Baum /* Insert to the global cache table. */ 1140a5d06c90SMichael Baum mlx5_mr_insert_cache(&dev->mr_scache, mr); 1141a5d06c90SMichael Baum rte_rwlock_write_unlock(&dev->mr_scache.rwlock); 1142a5d06c90SMichael Baum return 0; 1143ad435d32SXueming Li } 1144ad435d32SXueming Li 1145a5d06c90SMichael Baum /** 1146a5d06c90SMichael Baum * Callback to DMA unmap external memory to a device. 1147a5d06c90SMichael Baum * 1148a5d06c90SMichael Baum * @param rte_dev 1149a5d06c90SMichael Baum * Pointer to the generic device. 1150a5d06c90SMichael Baum * @param addr 1151a5d06c90SMichael Baum * Starting virtual address of memory to be unmapped. 1152a5d06c90SMichael Baum * @param iova 1153a5d06c90SMichael Baum * Starting IOVA address of memory to be unmapped. 1154a5d06c90SMichael Baum * @param len 1155a5d06c90SMichael Baum * Length of memory segment being unmapped. 1156a5d06c90SMichael Baum * 1157a5d06c90SMichael Baum * @return 1158a5d06c90SMichael Baum * 0 on success, negative value on error. 1159a5d06c90SMichael Baum */ 1160ad435d32SXueming Li int 1161a5d06c90SMichael Baum mlx5_common_dev_dma_unmap(struct rte_device *rte_dev, void *addr, 1162a5d06c90SMichael Baum uint64_t iova __rte_unused, size_t len __rte_unused) 1163ad435d32SXueming Li { 1164a5d06c90SMichael Baum struct mlx5_common_device *dev; 1165a5d06c90SMichael Baum struct mr_cache_entry entry; 1166a5d06c90SMichael Baum struct mlx5_mr *mr; 1167ad435d32SXueming Li 1168a5d06c90SMichael Baum dev = to_mlx5_device(rte_dev); 1169a5d06c90SMichael Baum if (!dev) { 1170a5d06c90SMichael Baum DRV_LOG(WARNING, 1171a5d06c90SMichael Baum "Unable to find matching mlx5 device to device %s.", 1172a5d06c90SMichael Baum rte_dev->name); 1173a5d06c90SMichael Baum rte_errno = ENODEV; 1174a5d06c90SMichael Baum return -1; 1175ad435d32SXueming Li } 1176a5d06c90SMichael Baum rte_rwlock_read_lock(&dev->mr_scache.rwlock); 1177a5d06c90SMichael Baum mr = mlx5_mr_lookup_list(&dev->mr_scache, &entry, (uintptr_t)addr); 1178a5d06c90SMichael Baum if (!mr) { 1179a5d06c90SMichael Baum rte_rwlock_read_unlock(&dev->mr_scache.rwlock); 1180a5d06c90SMichael Baum DRV_LOG(WARNING, 1181a5d06c90SMichael Baum "Address 0x%" PRIxPTR " wasn't registered to device %s", 1182a5d06c90SMichael Baum (uintptr_t)addr, rte_dev->name); 1183a5d06c90SMichael Baum rte_errno = EINVAL; 1184a5d06c90SMichael Baum return -1; 1185a5d06c90SMichael Baum } 1186a5d06c90SMichael Baum LIST_REMOVE(mr, mr); 1187a5d06c90SMichael Baum DRV_LOG(DEBUG, "MR(%p) is removed from list.", (void *)mr); 1188a5d06c90SMichael Baum mlx5_mr_free(mr, dev->mr_scache.dereg_mr_cb); 1189a5d06c90SMichael Baum mlx5_mr_rebuild_cache(&dev->mr_scache); 1190a5d06c90SMichael Baum /* 1191a5d06c90SMichael Baum * No explicit wmb is needed after updating dev_gen due to 1192a5d06c90SMichael Baum * store-release ordering in unlock that provides the 1193a5d06c90SMichael Baum * implicit barrier at the software visible level. 1194a5d06c90SMichael Baum */ 1195a5d06c90SMichael Baum ++dev->mr_scache.dev_gen; 1196a5d06c90SMichael Baum DRV_LOG(DEBUG, "Broadcasting local cache flush, gen=%d.", 1197a5d06c90SMichael Baum dev->mr_scache.dev_gen); 1198a5d06c90SMichael Baum rte_rwlock_read_unlock(&dev->mr_scache.rwlock); 1199a5d06c90SMichael Baum return 0; 1200ad435d32SXueming Li } 1201ad435d32SXueming Li 1202ad435d32SXueming Li void 1203ad435d32SXueming Li mlx5_class_driver_register(struct mlx5_class_driver *driver) 1204ad435d32SXueming Li { 1205ad435d32SXueming Li mlx5_common_driver_on_register_pci(driver); 1206ad435d32SXueming Li TAILQ_INSERT_TAIL(&drivers_list, driver, next); 1207ad435d32SXueming Li } 1208ad435d32SXueming Li 1209ad435d32SXueming Li static void mlx5_common_driver_init(void) 1210ad435d32SXueming Li { 1211ad435d32SXueming Li mlx5_common_pci_init(); 1212777b72a9SXueming Li #ifdef RTE_EXEC_ENV_LINUX 1213777b72a9SXueming Li mlx5_common_auxiliary_init(); 1214777b72a9SXueming Li #endif 1215ad435d32SXueming Li } 1216ad435d32SXueming Li 121782088001SParav Pandit static bool mlx5_common_initialized; 121882088001SParav Pandit 121983c99c36SThomas Monjalon /** 12207be78d02SJosh Soref * One time initialization routine for run-time dependency on glue library 122182088001SParav Pandit * for multiple PMDs. Each mlx5 PMD that depends on mlx5_common module, 122282088001SParav Pandit * must invoke in its constructor. 122383c99c36SThomas Monjalon */ 122482088001SParav Pandit void 122582088001SParav Pandit mlx5_common_init(void) 122683c99c36SThomas Monjalon { 122782088001SParav Pandit if (mlx5_common_initialized) 122882088001SParav Pandit return; 122982088001SParav Pandit 1230dc26c9c2SMichael Baum pthread_mutex_init(&devices_list_lock, NULL); 123179aa4307SOphir Munk mlx5_glue_constructor(); 1232ad435d32SXueming Li mlx5_common_driver_init(); 123382088001SParav Pandit mlx5_common_initialized = true; 12347b4f1e6bSMatan Azrad } 12354c204fe5SShiri Kuzin 12364c204fe5SShiri Kuzin /** 12374c204fe5SShiri Kuzin * This function is responsible of initializing the variable 12384c204fe5SShiri Kuzin * haswell_broadwell_cpu by checking if the cpu is intel 12394c204fe5SShiri Kuzin * and reading the data returned from mlx5_cpu_id(). 12404c204fe5SShiri Kuzin * since haswell and broadwell cpus don't have improved performance 12414c204fe5SShiri Kuzin * when using relaxed ordering we want to check the cpu type before 12424c204fe5SShiri Kuzin * before deciding whether to enable RO or not. 12434c204fe5SShiri Kuzin * if the cpu is haswell or broadwell the variable will be set to 1 12444c204fe5SShiri Kuzin * otherwise it will be 0. 12454c204fe5SShiri Kuzin */ 12464c204fe5SShiri Kuzin RTE_INIT_PRIO(mlx5_is_haswell_broadwell_cpu, LOG) 12474c204fe5SShiri Kuzin { 12484c204fe5SShiri Kuzin #ifdef RTE_ARCH_X86_64 12494c204fe5SShiri Kuzin unsigned int broadwell_models[4] = {0x3d, 0x47, 0x4F, 0x56}; 12504c204fe5SShiri Kuzin unsigned int haswell_models[4] = {0x3c, 0x3f, 0x45, 0x46}; 12514c204fe5SShiri Kuzin unsigned int i, model, family, brand_id, vendor; 12524c204fe5SShiri Kuzin unsigned int signature_intel_ebx = 0x756e6547; 12534c204fe5SShiri Kuzin unsigned int extended_model; 12544c204fe5SShiri Kuzin unsigned int eax = 0; 12554c204fe5SShiri Kuzin unsigned int ebx = 0; 12564c204fe5SShiri Kuzin unsigned int ecx = 0; 12574c204fe5SShiri Kuzin unsigned int edx = 0; 12584c204fe5SShiri Kuzin int max_level; 12594c204fe5SShiri Kuzin 12604c204fe5SShiri Kuzin mlx5_cpu_id(0, &eax, &ebx, &ecx, &edx); 12614c204fe5SShiri Kuzin vendor = ebx; 12624c204fe5SShiri Kuzin max_level = eax; 12634c204fe5SShiri Kuzin if (max_level < 1) { 12644c204fe5SShiri Kuzin haswell_broadwell_cpu = 0; 12654c204fe5SShiri Kuzin return; 12664c204fe5SShiri Kuzin } 12674c204fe5SShiri Kuzin mlx5_cpu_id(1, &eax, &ebx, &ecx, &edx); 12684c204fe5SShiri Kuzin model = (eax >> 4) & 0x0f; 12694c204fe5SShiri Kuzin family = (eax >> 8) & 0x0f; 12704c204fe5SShiri Kuzin brand_id = ebx & 0xff; 12714c204fe5SShiri Kuzin extended_model = (eax >> 12) & 0xf0; 12724c204fe5SShiri Kuzin /* Check if the processor is Haswell or Broadwell */ 12734c204fe5SShiri Kuzin if (vendor == signature_intel_ebx) { 12744c204fe5SShiri Kuzin if (family == 0x06) 12754c204fe5SShiri Kuzin model += extended_model; 12764c204fe5SShiri Kuzin if (brand_id == 0 && family == 0x6) { 12774c204fe5SShiri Kuzin for (i = 0; i < RTE_DIM(broadwell_models); i++) 12784c204fe5SShiri Kuzin if (model == broadwell_models[i]) { 12794c204fe5SShiri Kuzin haswell_broadwell_cpu = 1; 12804c204fe5SShiri Kuzin return; 12814c204fe5SShiri Kuzin } 12824c204fe5SShiri Kuzin for (i = 0; i < RTE_DIM(haswell_models); i++) 12834c204fe5SShiri Kuzin if (model == haswell_models[i]) { 12844c204fe5SShiri Kuzin haswell_broadwell_cpu = 1; 12854c204fe5SShiri Kuzin return; 12864c204fe5SShiri Kuzin } 12874c204fe5SShiri Kuzin } 12884c204fe5SShiri Kuzin } 12894c204fe5SShiri Kuzin #endif 12904c204fe5SShiri Kuzin haswell_broadwell_cpu = 0; 12914c204fe5SShiri Kuzin } 1292262c7ad0SOri Kam 1293262c7ad0SOri Kam /** 12949cc0e99cSViacheslav Ovsiienko * Allocate the User Access Region with DevX on specified device. 1295b4371d3dSMichael Baum * This routine handles the following UAR allocation issues: 12969cc0e99cSViacheslav Ovsiienko * 12975dfa003dSMichael Baum * - Try to allocate the UAR with the most appropriate memory mapping 1298b4371d3dSMichael Baum * type from the ones supported by the host. 1299b4371d3dSMichael Baum * 13005dfa003dSMichael Baum * - Try to allocate the UAR with non-NULL base address OFED 5.0.x and 1301b4371d3dSMichael Baum * Upstream rdma_core before v29 returned the NULL as UAR base address 1302b4371d3dSMichael Baum * if UAR was not the first object in the UAR page. 1303b4371d3dSMichael Baum * It caused the PMD failure and we should try to get another UAR till 1304b4371d3dSMichael Baum * we get the first one with non-NULL base address returned. 1305b4371d3dSMichael Baum * 1306b4371d3dSMichael Baum * @param [in] cdev 1307b4371d3dSMichael Baum * Pointer to mlx5 device structure to perform allocation on its context. 13089cc0e99cSViacheslav Ovsiienko * 13099cc0e99cSViacheslav Ovsiienko * @return 13109cc0e99cSViacheslav Ovsiienko * UAR object pointer on success, NULL otherwise and rte_errno is set. 13119cc0e99cSViacheslav Ovsiienko */ 13125dfa003dSMichael Baum static void * 1313b4371d3dSMichael Baum mlx5_devx_alloc_uar(struct mlx5_common_device *cdev) 13149cc0e99cSViacheslav Ovsiienko { 13159cc0e99cSViacheslav Ovsiienko void *uar; 13169cc0e99cSViacheslav Ovsiienko uint32_t retry, uar_mapping; 13179cc0e99cSViacheslav Ovsiienko void *base_addr; 13189cc0e99cSViacheslav Ovsiienko 13199cc0e99cSViacheslav Ovsiienko for (retry = 0; retry < MLX5_ALLOC_UAR_RETRY; ++retry) { 13209cc0e99cSViacheslav Ovsiienko #ifdef MLX5DV_UAR_ALLOC_TYPE_NC 13219cc0e99cSViacheslav Ovsiienko /* Control the mapping type according to the settings. */ 1322a6b9d5a5SMichael Baum uar_mapping = (cdev->config.dbnc == MLX5_SQ_DB_NCACHED) ? 1323b4371d3dSMichael Baum MLX5DV_UAR_ALLOC_TYPE_NC : MLX5DV_UAR_ALLOC_TYPE_BF; 13249cc0e99cSViacheslav Ovsiienko #else 13259cc0e99cSViacheslav Ovsiienko /* 13269cc0e99cSViacheslav Ovsiienko * It seems we have no way to control the memory mapping type 13279cc0e99cSViacheslav Ovsiienko * for the UAR, the default "Write-Combining" type is supposed. 13289cc0e99cSViacheslav Ovsiienko */ 13299cc0e99cSViacheslav Ovsiienko uar_mapping = 0; 13309cc0e99cSViacheslav Ovsiienko #endif 1331b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping); 13329cc0e99cSViacheslav Ovsiienko #ifdef MLX5DV_UAR_ALLOC_TYPE_NC 1333b4371d3dSMichael Baum if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_BF) { 1334b4371d3dSMichael Baum /* 1335b4371d3dSMichael Baum * In some environments like virtual machine the 1336b4371d3dSMichael Baum * Write Combining mapped might be not supported and 1337b4371d3dSMichael Baum * UAR allocation fails. We tried "Non-Cached" mapping 1338b4371d3dSMichael Baum * for the case. 1339b4371d3dSMichael Baum */ 1340b4371d3dSMichael Baum DRV_LOG(DEBUG, "Failed to allocate DevX UAR (BF)"); 1341b4371d3dSMichael Baum uar_mapping = MLX5DV_UAR_ALLOC_TYPE_NC; 1342b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping); 1343b4371d3dSMichael Baum } else if (!uar && uar_mapping == MLX5DV_UAR_ALLOC_TYPE_NC) { 13449cc0e99cSViacheslav Ovsiienko /* 13459cc0e99cSViacheslav Ovsiienko * If Verbs/kernel does not support "Non-Cached" 13469cc0e99cSViacheslav Ovsiienko * try the "Write-Combining". 13479cc0e99cSViacheslav Ovsiienko */ 13483f0e54feSMichael Baum DRV_LOG(DEBUG, "Failed to allocate DevX UAR (NC)"); 13499cc0e99cSViacheslav Ovsiienko uar_mapping = MLX5DV_UAR_ALLOC_TYPE_BF; 1350b4371d3dSMichael Baum uar = mlx5_glue->devx_alloc_uar(cdev->ctx, uar_mapping); 13519cc0e99cSViacheslav Ovsiienko } 13529cc0e99cSViacheslav Ovsiienko #endif 13539cc0e99cSViacheslav Ovsiienko if (!uar) { 13549cc0e99cSViacheslav Ovsiienko DRV_LOG(ERR, "Failed to allocate DevX UAR (BF/NC)"); 13559cc0e99cSViacheslav Ovsiienko rte_errno = ENOMEM; 13569cc0e99cSViacheslav Ovsiienko goto exit; 13579cc0e99cSViacheslav Ovsiienko } 13589cc0e99cSViacheslav Ovsiienko base_addr = mlx5_os_get_devx_uar_base_addr(uar); 13599cc0e99cSViacheslav Ovsiienko if (base_addr) 13609cc0e99cSViacheslav Ovsiienko break; 13619cc0e99cSViacheslav Ovsiienko /* 13629cc0e99cSViacheslav Ovsiienko * The UARs are allocated by rdma_core within the 13639cc0e99cSViacheslav Ovsiienko * IB device context, on context closure all UARs 13649cc0e99cSViacheslav Ovsiienko * will be freed, should be no memory/object leakage. 13659cc0e99cSViacheslav Ovsiienko */ 13663f0e54feSMichael Baum DRV_LOG(DEBUG, "Retrying to allocate DevX UAR"); 13679cc0e99cSViacheslav Ovsiienko uar = NULL; 13689cc0e99cSViacheslav Ovsiienko } 13699cc0e99cSViacheslav Ovsiienko /* Check whether we finally succeeded with valid UAR allocation. */ 13709cc0e99cSViacheslav Ovsiienko if (!uar) { 13719cc0e99cSViacheslav Ovsiienko DRV_LOG(ERR, "Failed to allocate DevX UAR (NULL base)"); 13729cc0e99cSViacheslav Ovsiienko rte_errno = ENOMEM; 13739cc0e99cSViacheslav Ovsiienko } 13749cc0e99cSViacheslav Ovsiienko /* 13759cc0e99cSViacheslav Ovsiienko * Return void * instead of struct mlx5dv_devx_uar * 13769cc0e99cSViacheslav Ovsiienko * is for compatibility with older rdma-core library headers. 13779cc0e99cSViacheslav Ovsiienko */ 13789cc0e99cSViacheslav Ovsiienko exit: 13799cc0e99cSViacheslav Ovsiienko return uar; 13809cc0e99cSViacheslav Ovsiienko } 1381ad435d32SXueming Li 13825dfa003dSMichael Baum void 13835dfa003dSMichael Baum mlx5_devx_uar_release(struct mlx5_uar *uar) 13845dfa003dSMichael Baum { 13855dfa003dSMichael Baum if (uar->obj != NULL) 13865dfa003dSMichael Baum mlx5_glue->devx_free_uar(uar->obj); 13875dfa003dSMichael Baum memset(uar, 0, sizeof(*uar)); 13885dfa003dSMichael Baum } 13895dfa003dSMichael Baum 13905dfa003dSMichael Baum int 13915dfa003dSMichael Baum mlx5_devx_uar_prepare(struct mlx5_common_device *cdev, struct mlx5_uar *uar) 13925dfa003dSMichael Baum { 13935dfa003dSMichael Baum off_t uar_mmap_offset; 13945dfa003dSMichael Baum const size_t page_size = rte_mem_page_size(); 13955dfa003dSMichael Baum void *base_addr; 13965dfa003dSMichael Baum void *uar_obj; 13975dfa003dSMichael Baum 13985dfa003dSMichael Baum if (page_size == (size_t)-1) { 13995dfa003dSMichael Baum DRV_LOG(ERR, "Failed to get mem page size"); 14005dfa003dSMichael Baum rte_errno = ENOMEM; 14015dfa003dSMichael Baum return -1; 14025dfa003dSMichael Baum } 14035dfa003dSMichael Baum uar_obj = mlx5_devx_alloc_uar(cdev); 14045dfa003dSMichael Baum if (uar_obj == NULL || mlx5_os_get_devx_uar_reg_addr(uar_obj) == NULL) { 14055dfa003dSMichael Baum rte_errno = errno; 14065dfa003dSMichael Baum DRV_LOG(ERR, "Failed to allocate UAR."); 14075dfa003dSMichael Baum return -1; 14085dfa003dSMichael Baum } 14095dfa003dSMichael Baum uar->obj = uar_obj; 14105dfa003dSMichael Baum uar_mmap_offset = mlx5_os_get_devx_uar_mmap_offset(uar_obj); 14115dfa003dSMichael Baum base_addr = mlx5_os_get_devx_uar_base_addr(uar_obj); 14125dfa003dSMichael Baum uar->dbnc = mlx5_db_map_type_get(uar_mmap_offset, page_size); 14135dfa003dSMichael Baum uar->bf_db.db = mlx5_os_get_devx_uar_reg_addr(uar_obj); 14145dfa003dSMichael Baum uar->cq_db.db = RTE_PTR_ADD(base_addr, MLX5_CQ_DOORBELL); 14155dfa003dSMichael Baum #ifndef RTE_ARCH_64 14165dfa003dSMichael Baum rte_spinlock_init(&uar->bf_sl); 14175dfa003dSMichael Baum rte_spinlock_init(&uar->cq_sl); 14185dfa003dSMichael Baum uar->bf_db.sl_p = &uar->bf_sl; 14195dfa003dSMichael Baum uar->cq_db.sl_p = &uar->cq_sl; 14205dfa003dSMichael Baum #endif /* RTE_ARCH_64 */ 14215dfa003dSMichael Baum return 0; 14225dfa003dSMichael Baum } 14235dfa003dSMichael Baum 1424ad435d32SXueming Li RTE_PMD_EXPORT_NAME(mlx5_common_driver, __COUNTER__); 1425