LCOV - code coverage report
Current view: top level - drivers/bus/auxiliary - auxiliary_common.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 23 147 15.6 %
Date: 2025-05-01 17:49:45 Functions: 8 22 36.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 14 109 12.8 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright (c) 2021 NVIDIA Corporation & Affiliates
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <string.h>
       6                 :            : #include <inttypes.h>
       7                 :            : #include <stdint.h>
       8                 :            : #include <stdbool.h>
       9                 :            : #include <stdlib.h>
      10                 :            : #include <stdio.h>
      11                 :            : #include <sys/queue.h>
      12                 :            : #include <eal_export.h>
      13                 :            : #include <rte_errno.h>
      14                 :            : #include <rte_interrupts.h>
      15                 :            : #include <rte_log.h>
      16                 :            : #include <bus_driver.h>
      17                 :            : #include <rte_per_lcore.h>
      18                 :            : #include <rte_memory.h>
      19                 :            : #include <rte_eal.h>
      20                 :            : #include <rte_eal_paging.h>
      21                 :            : #include <rte_lcore.h>
      22                 :            : #include <rte_string_fns.h>
      23                 :            : #include <rte_common.h>
      24                 :            : #include <rte_devargs.h>
      25                 :            : 
      26                 :            : #include "private.h"
      27                 :            : 
      28                 :            : static struct rte_devargs *
      29                 :          0 : auxiliary_devargs_lookup(const char *name)
      30                 :            : {
      31                 :            :         struct rte_devargs *devargs;
      32                 :            : 
      33         [ #  # ]:          0 :         RTE_EAL_DEVARGS_FOREACH(RTE_BUS_AUXILIARY_NAME, devargs) {
      34         [ #  # ]:          0 :                 if (strcmp(devargs->name, name) == 0)
      35                 :          0 :                         return devargs;
      36                 :            :         }
      37                 :            :         return NULL;
      38                 :            : }
      39                 :            : 
      40                 :            : #ifndef AUXILIARY_OS_SUPPORTED
      41                 :            : /*
      42                 :            :  * Test whether the auxiliary device exist.
      43                 :            :  *
      44                 :            :  * Stub for OS not supporting auxiliary bus.
      45                 :            :  */
      46                 :            : bool
      47                 :            : auxiliary_dev_exists(const char *name)
      48                 :            : {
      49                 :            :         RTE_SET_USED(name);
      50                 :            :         return false;
      51                 :            : }
      52                 :            : 
      53                 :            : /*
      54                 :            :  * Scan the devices in the auxiliary bus.
      55                 :            :  *
      56                 :            :  * Stub for OS not supporting auxiliary bus.
      57                 :            :  */
      58                 :            : int
      59                 :            : auxiliary_scan(void)
      60                 :            : {
      61                 :            :         return 0;
      62                 :            : }
      63                 :            : #endif /* AUXILIARY_OS_SUPPORTED */
      64                 :            : 
      65                 :            : /*
      66                 :            :  * Update a device's devargs being scanned.
      67                 :            :  */
      68                 :            : void
      69                 :          0 : auxiliary_on_scan(struct rte_auxiliary_device *aux_dev)
      70                 :            : {
      71                 :          0 :         aux_dev->device.devargs = auxiliary_devargs_lookup(aux_dev->name);
      72                 :          0 : }
      73                 :            : 
      74                 :            : /*
      75                 :            :  * Match the auxiliary driver and device using driver function.
      76                 :            :  */
      77                 :            : bool
      78                 :          0 : auxiliary_match(const struct rte_auxiliary_driver *aux_drv,
      79                 :            :                 const struct rte_auxiliary_device *aux_dev)
      80                 :            : {
      81         [ #  # ]:          0 :         if (aux_drv->match == NULL)
      82                 :            :                 return false;
      83                 :          0 :         return aux_drv->match(aux_dev->name);
      84                 :            : }
      85                 :            : 
      86                 :            : /*
      87                 :            :  * Call the probe() function of the driver.
      88                 :            :  */
      89                 :            : static int
      90                 :          0 : rte_auxiliary_probe_one_driver(struct rte_auxiliary_driver *drv,
      91                 :            :                                struct rte_auxiliary_device *dev)
      92                 :            : {
      93                 :            :         enum rte_iova_mode iova_mode;
      94                 :            :         int ret;
      95                 :            : 
      96         [ #  # ]:          0 :         if (drv == NULL || dev == NULL)
      97                 :            :                 return -EINVAL;
      98                 :            : 
      99                 :            :         /* Check if driver supports it. */
     100         [ #  # ]:          0 :         if (!auxiliary_match(drv, dev))
     101                 :            :                 /* Match of device and driver failed */
     102                 :            :                 return 1;
     103                 :            : 
     104                 :            :         /* No initialization when marked as blocked, return without error. */
     105         [ #  # ]:          0 :         if (dev->device.devargs != NULL &&
     106         [ #  # ]:          0 :             dev->device.devargs->policy == RTE_DEV_BLOCKED) {
     107                 :          0 :                 AUXILIARY_LOG(INFO, "Device is blocked, not initializing");
     108                 :          0 :                 return -1;
     109                 :            :         }
     110                 :            : 
     111   [ #  #  #  # ]:          0 :         if (dev->device.numa_node < 0 && rte_socket_count() > 1)
     112                 :          0 :                 AUXILIARY_LOG(INFO, "Device %s is not NUMA-aware", dev->name);
     113                 :            : 
     114         [ #  # ]:          0 :         if (rte_dev_is_probed(&dev->device)) {
     115                 :          0 :                 AUXILIARY_LOG(DEBUG, "Device %s is already probed on auxiliary bus",
     116                 :            :                         dev->device.name);
     117                 :          0 :                 return -EEXIST;
     118                 :            :         }
     119                 :            : 
     120                 :          0 :         iova_mode = rte_eal_iova_mode();
     121   [ #  #  #  # ]:          0 :         if ((drv->drv_flags & RTE_AUXILIARY_DRV_NEED_IOVA_AS_VA) > 0 &&
     122                 :            :             iova_mode != RTE_IOVA_VA) {
     123                 :          0 :                 AUXILIARY_LOG(ERR, "Driver %s expecting VA IOVA mode but current mode is PA, not initializing",
     124                 :            :                               drv->driver.name);
     125                 :          0 :                 return -EINVAL;
     126                 :            :         }
     127                 :            : 
     128                 :            :         /* Allocate interrupt instance */
     129                 :          0 :         dev->intr_handle =
     130                 :          0 :                 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
     131         [ #  # ]:          0 :         if (dev->intr_handle == NULL) {
     132                 :          0 :                 AUXILIARY_LOG(ERR, "Could not allocate interrupt instance for device %s",
     133                 :            :                         dev->name);
     134                 :          0 :                 return -ENOMEM;
     135                 :            :         }
     136                 :            : 
     137                 :          0 :         dev->driver = drv;
     138                 :            : 
     139                 :          0 :         AUXILIARY_LOG(INFO, "Probe auxiliary driver: %s device: %s (NUMA node %i)",
     140                 :            :                       drv->driver.name, dev->name, dev->device.numa_node);
     141                 :          0 :         ret = drv->probe(drv, dev);
     142         [ #  # ]:          0 :         if (ret != 0) {
     143                 :          0 :                 dev->driver = NULL;
     144                 :          0 :                 rte_intr_instance_free(dev->intr_handle);
     145                 :          0 :                 dev->intr_handle = NULL;
     146                 :            :         } else {
     147                 :          0 :                 dev->device.driver = &drv->driver;
     148                 :            :         }
     149                 :            : 
     150                 :            :         return ret;
     151                 :            : }
     152                 :            : 
     153                 :            : /*
     154                 :            :  * Call the remove() function of the driver.
     155                 :            :  */
     156                 :            : static int
     157                 :          0 : rte_auxiliary_driver_remove_dev(struct rte_auxiliary_device *dev)
     158                 :            : {
     159                 :            :         struct rte_auxiliary_driver *drv;
     160                 :            :         int ret = 0;
     161                 :            : 
     162         [ #  # ]:          0 :         if (dev == NULL)
     163                 :            :                 return -EINVAL;
     164                 :            : 
     165                 :          0 :         drv = dev->driver;
     166                 :            : 
     167                 :          0 :         AUXILIARY_LOG(DEBUG, "Driver %s remove auxiliary device %s on NUMA node %i",
     168                 :            :                       drv->driver.name, dev->name, dev->device.numa_node);
     169                 :            : 
     170         [ #  # ]:          0 :         if (drv->remove != NULL) {
     171                 :          0 :                 ret = drv->remove(dev);
     172         [ #  # ]:          0 :                 if (ret < 0)
     173                 :            :                         return ret;
     174                 :            :         }
     175                 :            : 
     176                 :            :         /* clear driver structure */
     177                 :          0 :         dev->driver = NULL;
     178                 :          0 :         dev->device.driver = NULL;
     179                 :            : 
     180                 :          0 :         return 0;
     181                 :            : }
     182                 :            : 
     183                 :            : /*
     184                 :            :  * Call the probe() function of all registered drivers for the given device.
     185                 :            :  * Return < 0 if initialization failed.
     186                 :            :  * Return 1 if no driver is found for this device.
     187                 :            :  */
     188                 :            : static int
     189                 :          0 : auxiliary_probe_all_drivers(struct rte_auxiliary_device *dev)
     190                 :            : {
     191                 :            :         struct rte_auxiliary_driver *drv;
     192                 :            :         int rc;
     193                 :            : 
     194         [ #  # ]:          0 :         if (dev == NULL)
     195                 :            :                 return -EINVAL;
     196                 :            : 
     197         [ #  # ]:          0 :         FOREACH_DRIVER_ON_AUXILIARY_BUS(drv) {
     198         [ #  # ]:          0 :                 if (!drv->match(dev->name))
     199                 :          0 :                         continue;
     200                 :            : 
     201                 :          0 :                 rc = rte_auxiliary_probe_one_driver(drv, dev);
     202         [ #  # ]:          0 :                 if (rc < 0)
     203                 :            :                         /* negative value is an error */
     204                 :          0 :                         return rc;
     205         [ #  # ]:          0 :                 if (rc > 0)
     206                 :            :                         /* positive value means driver doesn't support it */
     207                 :          0 :                         continue;
     208                 :            :                 return 0;
     209                 :            :         }
     210                 :            :         return 1;
     211                 :            : }
     212                 :            : 
     213                 :            : /*
     214                 :            :  * Scan the content of the auxiliary bus, and call the probe function for
     215                 :            :  * all registered drivers to try to probe discovered devices.
     216                 :            :  */
     217                 :            : static int
     218                 :        180 : auxiliary_probe(void)
     219                 :            : {
     220                 :            :         struct rte_auxiliary_device *dev = NULL;
     221                 :            :         size_t probed = 0, failed = 0;
     222                 :            :         int ret = 0;
     223                 :            : 
     224         [ -  + ]:        180 :         FOREACH_DEVICE_ON_AUXILIARY_BUS(dev) {
     225                 :          0 :                 probed++;
     226                 :            : 
     227                 :          0 :                 ret = auxiliary_probe_all_drivers(dev);
     228         [ #  # ]:          0 :                 if (ret < 0) {
     229         [ #  # ]:          0 :                         if (ret != -EEXIST) {
     230                 :          0 :                                 AUXILIARY_LOG(ERR, "Requested device %s cannot be used",
     231                 :            :                                               dev->name);
     232                 :          0 :                                 rte_errno = errno;
     233                 :          0 :                                 failed++;
     234                 :            :                         }
     235                 :            :                         ret = 0;
     236                 :            :                 }
     237                 :            :         }
     238                 :            : 
     239         [ +  - ]:        180 :         return (probed && probed == failed) ? -1 : 0;
     240                 :            : }
     241                 :            : 
     242                 :            : static int
     243                 :         37 : auxiliary_parse(const char *name, void *addr)
     244                 :            : {
     245                 :            :         struct rte_auxiliary_driver *drv = NULL;
     246                 :            :         const char **out = addr;
     247                 :            : 
     248                 :            :         /* Allow empty device name "auxiliary:" to bypass entire bus scan. */
     249         [ +  - ]:         37 :         if (strlen(name) == 0)
     250                 :            :                 return 0;
     251                 :            : 
     252         [ +  + ]:         74 :         FOREACH_DRIVER_ON_AUXILIARY_BUS(drv) {
     253         [ +  - ]:         37 :                 if (drv->match(name))
     254                 :            :                         break;
     255                 :            :         }
     256         [ -  + ]:         37 :         if (drv != NULL && addr != NULL)
     257                 :          0 :                 *out = name;
     258         [ +  - ]:         37 :         return drv != NULL ? 0 : -1;
     259                 :            : }
     260                 :            : 
     261                 :            : /* Register a driver */
     262                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_auxiliary_register)
     263                 :            : void
     264                 :        252 : rte_auxiliary_register(struct rte_auxiliary_driver *driver)
     265                 :            : {
     266                 :        252 :         TAILQ_INSERT_TAIL(&auxiliary_bus.driver_list, driver, next);
     267                 :        252 : }
     268                 :            : 
     269                 :            : /* Unregister a driver */
     270                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_auxiliary_unregister)
     271                 :            : void
     272                 :        252 : rte_auxiliary_unregister(struct rte_auxiliary_driver *driver)
     273                 :            : {
     274         [ -  + ]:        252 :         TAILQ_REMOVE(&auxiliary_bus.driver_list, driver, next);
     275                 :        252 : }
     276                 :            : 
     277                 :            : /* Add a device to auxiliary bus */
     278                 :            : void
     279                 :          0 : auxiliary_add_device(struct rte_auxiliary_device *aux_dev)
     280                 :            : {
     281                 :          0 :         TAILQ_INSERT_TAIL(&auxiliary_bus.device_list, aux_dev, next);
     282                 :          0 : }
     283                 :            : 
     284                 :            : /* Insert a device into a predefined position in auxiliary bus */
     285                 :            : void
     286                 :          0 : auxiliary_insert_device(struct rte_auxiliary_device *exist_aux_dev,
     287                 :            :                         struct rte_auxiliary_device *new_aux_dev)
     288                 :            : {
     289                 :          0 :         TAILQ_INSERT_BEFORE(exist_aux_dev, new_aux_dev, next);
     290                 :          0 : }
     291                 :            : 
     292                 :            : /* Remove a device from auxiliary bus */
     293                 :            : static void
     294                 :            : rte_auxiliary_remove_device(struct rte_auxiliary_device *auxiliary_dev)
     295                 :            : {
     296         [ #  # ]:          0 :         TAILQ_REMOVE(&auxiliary_bus.device_list, auxiliary_dev, next);
     297                 :            : }
     298                 :            : 
     299                 :            : static struct rte_device *
     300                 :          0 : auxiliary_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
     301                 :            :                       const void *data)
     302                 :            : {
     303                 :            :         const struct rte_auxiliary_device *pstart;
     304                 :            :         struct rte_auxiliary_device *adev;
     305                 :            : 
     306         [ #  # ]:          0 :         if (start != NULL) {
     307                 :          0 :                 pstart = RTE_DEV_TO_AUXILIARY_CONST(start);
     308                 :          0 :                 adev = TAILQ_NEXT(pstart, next);
     309                 :            :         } else {
     310                 :          0 :                 adev = TAILQ_FIRST(&auxiliary_bus.device_list);
     311                 :            :         }
     312         [ #  # ]:          0 :         while (adev != NULL) {
     313         [ #  # ]:          0 :                 if (cmp(&adev->device, data) == 0)
     314                 :          0 :                         return &adev->device;
     315                 :          0 :                 adev = TAILQ_NEXT(adev, next);
     316                 :            :         }
     317                 :            :         return NULL;
     318                 :            : }
     319                 :            : 
     320                 :            : static int
     321                 :          0 : auxiliary_plug(struct rte_device *dev)
     322                 :            : {
     323         [ #  # ]:          0 :         if (!auxiliary_dev_exists(dev->name))
     324                 :            :                 return -ENOENT;
     325                 :          0 :         return auxiliary_probe_all_drivers(RTE_DEV_TO_AUXILIARY(dev));
     326                 :            : }
     327                 :            : 
     328                 :            : static int
     329                 :          0 : auxiliary_unplug(struct rte_device *dev)
     330                 :            : {
     331                 :            :         struct rte_auxiliary_device *adev;
     332                 :            :         int ret;
     333                 :            : 
     334                 :          0 :         adev = RTE_DEV_TO_AUXILIARY(dev);
     335                 :          0 :         ret = rte_auxiliary_driver_remove_dev(adev);
     336         [ #  # ]:          0 :         if (ret == 0) {
     337                 :            :                 rte_auxiliary_remove_device(adev);
     338                 :          0 :                 rte_devargs_remove(dev->devargs);
     339                 :          0 :                 rte_intr_instance_free(adev->intr_handle);
     340                 :          0 :                 free(adev);
     341                 :            :         }
     342                 :          0 :         return ret;
     343                 :            : }
     344                 :            : 
     345                 :            : static int
     346                 :        252 : auxiliary_cleanup(void)
     347                 :            : {
     348                 :            :         struct rte_auxiliary_device *dev, *tmp_dev;
     349                 :            :         int error = 0;
     350                 :            : 
     351         [ -  + ]:        252 :         RTE_TAILQ_FOREACH_SAFE(dev, &auxiliary_bus.device_list, next, tmp_dev) {
     352                 :            :                 int ret;
     353                 :            : 
     354                 :          0 :                 ret = auxiliary_unplug(&dev->device);
     355         [ #  # ]:          0 :                 if (ret < 0) {
     356                 :          0 :                         rte_errno = errno;
     357                 :            :                         error = -1;
     358                 :            :                 }
     359                 :            :         }
     360                 :            : 
     361                 :        252 :         return error;
     362                 :            : }
     363                 :            : 
     364                 :            : static int
     365                 :          0 : auxiliary_dma_map(struct rte_device *dev, void *addr, uint64_t iova, size_t len)
     366                 :            : {
     367                 :          0 :         struct rte_auxiliary_device *aux_dev = RTE_DEV_TO_AUXILIARY(dev);
     368                 :            : 
     369   [ #  #  #  # ]:          0 :         if (dev == NULL || aux_dev->driver == NULL) {
     370                 :          0 :                 rte_errno = EINVAL;
     371                 :          0 :                 return -1;
     372                 :            :         }
     373         [ #  # ]:          0 :         if (aux_dev->driver->dma_map == NULL) {
     374                 :          0 :                 rte_errno = ENOTSUP;
     375                 :          0 :                 return -1;
     376                 :            :         }
     377                 :          0 :         return aux_dev->driver->dma_map(aux_dev, addr, iova, len);
     378                 :            : }
     379                 :            : 
     380                 :            : static int
     381                 :          0 : auxiliary_dma_unmap(struct rte_device *dev, void *addr, uint64_t iova,
     382                 :            :                     size_t len)
     383                 :            : {
     384                 :          0 :         struct rte_auxiliary_device *aux_dev = RTE_DEV_TO_AUXILIARY(dev);
     385                 :            : 
     386   [ #  #  #  # ]:          0 :         if (dev == NULL || aux_dev->driver == NULL) {
     387                 :          0 :                 rte_errno = EINVAL;
     388                 :          0 :                 return -1;
     389                 :            :         }
     390         [ #  # ]:          0 :         if (aux_dev->driver->dma_unmap == NULL) {
     391                 :          0 :                 rte_errno = ENOTSUP;
     392                 :          0 :                 return -1;
     393                 :            :         }
     394                 :          0 :         return aux_dev->driver->dma_unmap(aux_dev, addr, iova, len);
     395                 :            : }
     396                 :            : 
     397                 :            : bool
     398                 :          0 : auxiliary_is_ignored_device(const char *name)
     399                 :            : {
     400                 :          0 :         struct rte_devargs *devargs = auxiliary_devargs_lookup(name);
     401                 :            : 
     402      [ #  #  # ]:          0 :         switch (auxiliary_bus.bus.conf.scan_mode) {
     403                 :          0 :         case RTE_BUS_SCAN_ALLOWLIST:
     404   [ #  #  #  # ]:          0 :                 if (devargs && devargs->policy == RTE_DEV_ALLOWED)
     405                 :          0 :                         return false;
     406                 :            :                 break;
     407                 :          0 :         case RTE_BUS_SCAN_UNDEFINED:
     408                 :            :         case RTE_BUS_SCAN_BLOCKLIST:
     409   [ #  #  #  # ]:          0 :                 if (devargs == NULL || devargs->policy != RTE_DEV_BLOCKED)
     410                 :          0 :                         return false;
     411                 :            :                 break;
     412                 :            :         }
     413                 :            :         return true;
     414                 :            : }
     415                 :            : 
     416                 :            : static enum rte_iova_mode
     417                 :        185 : auxiliary_get_iommu_class(void)
     418                 :            : {
     419                 :            :         const struct rte_auxiliary_driver *drv;
     420                 :            : 
     421         [ +  + ]:        370 :         FOREACH_DRIVER_ON_AUXILIARY_BUS(drv) {
     422         [ +  - ]:        185 :                 if ((drv->drv_flags & RTE_AUXILIARY_DRV_NEED_IOVA_AS_VA) > 0)
     423                 :            :                         return RTE_IOVA_VA;
     424                 :            :         }
     425                 :            : 
     426                 :            :         return RTE_IOVA_DC;
     427                 :            : }
     428                 :            : 
     429                 :            : struct rte_auxiliary_bus auxiliary_bus = {
     430                 :            :         .bus = {
     431                 :            :                 .scan = auxiliary_scan,
     432                 :            :                 .probe = auxiliary_probe,
     433                 :            :                 .cleanup = auxiliary_cleanup,
     434                 :            :                 .find_device = auxiliary_find_device,
     435                 :            :                 .plug = auxiliary_plug,
     436                 :            :                 .unplug = auxiliary_unplug,
     437                 :            :                 .parse = auxiliary_parse,
     438                 :            :                 .dma_map = auxiliary_dma_map,
     439                 :            :                 .dma_unmap = auxiliary_dma_unmap,
     440                 :            :                 .get_iommu_class = auxiliary_get_iommu_class,
     441                 :            :                 .dev_iterate = auxiliary_dev_iterate,
     442                 :            :         },
     443                 :            :         .device_list = TAILQ_HEAD_INITIALIZER(auxiliary_bus.device_list),
     444                 :            :         .driver_list = TAILQ_HEAD_INITIALIZER(auxiliary_bus.driver_list),
     445                 :            : };
     446                 :            : 
     447                 :        252 : RTE_REGISTER_BUS(auxiliary, auxiliary_bus.bus);
     448         [ -  + ]:        252 : RTE_LOG_REGISTER_DEFAULT(auxiliary_bus_logtype, NOTICE);

Generated by: LCOV version 1.14