LCOV - code coverage report
Current view: top level - drivers/common/mlx5 - mlx5_common.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 31 491 6.3 %
Date: 2025-03-01 20:23:48 Functions: 4 36 11.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 13 309 4.2 %

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

Generated by: LCOV version 1.14