LCOV - code coverage report
Current view: top level - drivers/bus/ifpga - ifpga_bus.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 21 138 15.2 %
Date: 2026-07-01 18:02:41 Functions: 5 12 41.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 8 90 8.9 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2018 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <string.h>
       6                 :            : #include <inttypes.h>
       7                 :            : #include <stdint.h>
       8                 :            : #include <stdlib.h>
       9                 :            : #include <stdio.h>
      10                 :            : #include <sys/queue.h>
      11                 :            : #include <sys/mman.h>
      12                 :            : #include <sys/types.h>
      13                 :            : #include <unistd.h>
      14                 :            : #include <fcntl.h>
      15                 :            : 
      16                 :            : #include <eal_export.h>
      17                 :            : #include <rte_errno.h>
      18                 :            : #include <bus_driver.h>
      19                 :            : #include <rte_per_lcore.h>
      20                 :            : #include <rte_memory.h>
      21                 :            : #include <rte_memzone.h>
      22                 :            : #include <rte_eal.h>
      23                 :            : #include <rte_common.h>
      24                 :            : #include <rte_devargs.h>
      25                 :            : #include <rte_kvargs.h>
      26                 :            : #include <rte_alarm.h>
      27                 :            : #include <rte_string_fns.h>
      28                 :            : #include <rte_debug.h>
      29                 :            : 
      30                 :            : #include "rte_rawdev.h"
      31                 :            : #include "rte_rawdev_pmd.h"
      32                 :            : #include "bus_ifpga_driver.h"
      33                 :            : #include "ifpga_logs.h"
      34                 :            : #include "ifpga_common.h"
      35                 :            : 
      36                 :            : /* Forward declaration to access Intel FPGA bus
      37                 :            :  * on which iFPGA devices are connected
      38                 :            :  */
      39                 :            : static struct rte_bus rte_ifpga_bus;
      40                 :            : 
      41                 :            : /* register a ifpga bus based driver */
      42                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_ifpga_driver_register)
      43                 :          0 : void rte_ifpga_driver_register(struct rte_afu_driver *driver)
      44                 :            : {
      45         [ #  # ]:          0 :         RTE_VERIFY(driver);
      46                 :            : 
      47                 :          0 :         rte_bus_add_driver(&rte_ifpga_bus, &driver->driver);
      48                 :          0 : }
      49                 :            : 
      50                 :            : /* un-register a fpga bus based driver */
      51                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_ifpga_driver_unregister)
      52                 :          0 : void rte_ifpga_driver_unregister(struct rte_afu_driver *driver)
      53                 :            : {
      54                 :          0 :         rte_bus_remove_driver(&rte_ifpga_bus, &driver->driver);
      55                 :          0 : }
      56                 :            : 
      57                 :            : static struct rte_afu_device *
      58                 :            : ifpga_find_afu_dev(const struct rte_rawdev *rdev,
      59                 :            :         const struct rte_afu_id *afu_id)
      60                 :            : {
      61                 :            :         struct rte_afu_device *afu_dev = NULL;
      62                 :            : 
      63         [ #  # ]:          0 :         RTE_BUS_FOREACH_DEV(afu_dev, &rte_ifpga_bus) {
      64         [ #  # ]:          0 :                 if (afu_dev->rawdev == rdev &&
      65                 :            :                         !ifpga_afu_id_cmp(&afu_dev->id, afu_id))
      66                 :            :                         return afu_dev;
      67                 :            :         }
      68                 :            :         return NULL;
      69                 :            : }
      70                 :            : 
      71                 :            : static const char * const valid_args[] = {
      72                 :            : #define IFPGA_ARG_NAME         "ifpga"
      73                 :            :         IFPGA_ARG_NAME,
      74                 :            : #define IFPGA_ARG_PORT         "port"
      75                 :            :         IFPGA_ARG_PORT,
      76                 :            : #define IFPGA_AFU_BTS          "afu_bts"
      77                 :            :         IFPGA_AFU_BTS,
      78                 :            :         NULL
      79                 :            : };
      80                 :            : 
      81                 :            : /*
      82                 :            :  * Scan the content of the FPGA bus, and the devices in the devices
      83                 :            :  * list
      84                 :            :  */
      85                 :            : static struct rte_afu_device *
      86                 :          0 : ifpga_scan_one(struct rte_rawdev *rawdev,
      87                 :            :                 struct rte_devargs *devargs)
      88                 :            : {
      89                 :            :         struct rte_kvargs *kvlist = NULL;
      90                 :            :         struct rte_afu_device *afu_dev = NULL;
      91                 :            :         struct rte_afu_pr_conf afu_pr_conf;
      92                 :            :         int ret = 0;
      93                 :          0 :         char *path = NULL;
      94                 :            : 
      95                 :            :         memset(&afu_pr_conf, 0, sizeof(struct rte_afu_pr_conf));
      96                 :            : 
      97                 :          0 :         kvlist = rte_kvargs_parse(devargs->args, valid_args);
      98         [ #  # ]:          0 :         if (!kvlist) {
      99                 :          0 :                 IFPGA_BUS_ERR("error when parsing param");
     100                 :          0 :                 goto end;
     101                 :            :         }
     102                 :            : 
     103         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, IFPGA_ARG_PORT) == 1) {
     104         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, IFPGA_ARG_PORT,
     105                 :            :                                 ifpga_get_integer32_arg,
     106                 :            :                                 &afu_pr_conf.afu_id.port) < 0) {
     107                 :          0 :                         IFPGA_BUS_ERR("error to parse %s", IFPGA_ARG_PORT);
     108                 :          0 :                         goto end;
     109                 :            :                 }
     110                 :            :         } else {
     111                 :          0 :                 IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
     112                 :            :                           IFPGA_ARG_PORT);
     113                 :          0 :                 goto end;
     114                 :            :         }
     115                 :            : 
     116         [ #  # ]:          0 :         if (rte_kvargs_count(kvlist, IFPGA_AFU_BTS) == 1) {
     117         [ #  # ]:          0 :                 if (rte_kvargs_process(kvlist, IFPGA_AFU_BTS,
     118                 :            :                                 ifpga_get_string_arg, &path) < 0) {
     119                 :          0 :                         IFPGA_BUS_ERR("Failed to parse %s", IFPGA_AFU_BTS);
     120                 :          0 :                         goto end;
     121                 :            :                 }
     122                 :          0 :                 afu_pr_conf.pr_enable = 1;
     123                 :          0 :                 strlcpy(afu_pr_conf.bs_path, path,
     124                 :            :                         sizeof(afu_pr_conf.bs_path));
     125                 :            :         } else {
     126                 :          0 :                 afu_pr_conf.pr_enable = 0;
     127                 :            :         }
     128                 :            : 
     129                 :          0 :         afu_pr_conf.afu_id.uuid.uuid_low = 0;
     130                 :          0 :         afu_pr_conf.afu_id.uuid.uuid_high = 0;
     131                 :            : 
     132         [ #  # ]:          0 :         if (ifpga_find_afu_dev(rawdev, &afu_pr_conf.afu_id))
     133                 :          0 :                 goto end;
     134                 :            : 
     135                 :          0 :         afu_dev = calloc(1, sizeof(*afu_dev));
     136         [ #  # ]:          0 :         if (!afu_dev)
     137                 :          0 :                 goto end;
     138                 :            : 
     139                 :          0 :         afu_dev->device.devargs = devargs;
     140                 :          0 :         afu_dev->device.numa_node = SOCKET_ID_ANY;
     141                 :          0 :         afu_dev->device.name = devargs->name;
     142                 :          0 :         afu_dev->rawdev = rawdev;
     143                 :          0 :         afu_dev->id.uuid.uuid_low  = 0;
     144                 :          0 :         afu_dev->id.uuid.uuid_high = 0;
     145                 :          0 :         afu_dev->id.port      = afu_pr_conf.afu_id.port;
     146                 :            : 
     147   [ #  #  #  # ]:          0 :         if (rawdev->dev_ops && rawdev->dev_ops->dev_info_get)
     148                 :          0 :                 rawdev->dev_ops->dev_info_get(rawdev, afu_dev, sizeof(*afu_dev));
     149                 :            : 
     150         [ #  # ]:          0 :         if (rawdev->dev_ops &&
     151   [ #  #  #  # ]:          0 :                 rawdev->dev_ops->dev_start &&
     152                 :          0 :                 rawdev->dev_ops->dev_start(rawdev))
     153                 :          0 :                 goto end;
     154                 :            : 
     155         [ #  # ]:          0 :         if (rawdev->dev_ops &&
     156   [ #  #  #  # ]:          0 :                 rawdev->dev_ops->firmware_load &&
     157                 :          0 :                 rawdev->dev_ops->firmware_load(rawdev,
     158                 :            :                                 &afu_pr_conf)){
     159                 :          0 :                 IFPGA_BUS_ERR("firmware load error %d", ret);
     160                 :          0 :                 goto end;
     161                 :            :         }
     162                 :          0 :         afu_dev->id.uuid.uuid_low  = afu_pr_conf.afu_id.uuid.uuid_low;
     163                 :          0 :         afu_dev->id.uuid.uuid_high = afu_pr_conf.afu_id.uuid.uuid_high;
     164                 :            : 
     165                 :          0 :         rte_kvargs_free(kvlist);
     166                 :          0 :         free(path);
     167                 :          0 :         return afu_dev;
     168                 :            : 
     169                 :          0 : end:
     170                 :          0 :         rte_kvargs_free(kvlist);
     171                 :          0 :         free(path);
     172                 :          0 :         free(afu_dev);
     173                 :            : 
     174                 :          0 :         return NULL;
     175                 :            : }
     176                 :            : 
     177                 :            : /*
     178                 :            :  * Scan the content of the FPGA bus, and the devices in the devices
     179                 :            :  * list
     180                 :            :  */
     181                 :            : static int
     182                 :        225 : ifpga_scan(void)
     183                 :            : {
     184                 :            :         struct rte_devargs *devargs;
     185                 :            :         struct rte_kvargs *kvlist = NULL;
     186                 :            :         struct rte_rawdev *rawdev = NULL;
     187                 :        225 :         char *name = NULL;
     188                 :            :         char name1[RTE_RAWDEV_NAME_MAX_LEN];
     189                 :            :         struct rte_afu_device *afu_dev = NULL;
     190                 :            : 
     191                 :            :         /* for FPGA devices we scan the devargs_list populated via cmdline */
     192         [ -  + ]:        225 :         RTE_EAL_DEVARGS_FOREACH(rte_ifpga_bus.name, devargs) {
     193         [ #  # ]:          0 :                 if (devargs->bus != &rte_ifpga_bus)
     194                 :          0 :                         continue;
     195                 :            : 
     196                 :          0 :                 kvlist = rte_kvargs_parse(devargs->args, valid_args);
     197         [ #  # ]:          0 :                 if (!kvlist) {
     198                 :          0 :                         IFPGA_BUS_ERR("error when parsing param");
     199                 :          0 :                         goto end;
     200                 :            :                 }
     201                 :            : 
     202         [ #  # ]:          0 :                 if (rte_kvargs_count(kvlist, IFPGA_ARG_NAME) == 1) {
     203         [ #  # ]:          0 :                         if (rte_kvargs_process(kvlist, IFPGA_ARG_NAME,
     204                 :            :                                         ifpga_get_string_arg, &name) < 0) {
     205                 :          0 :                                 IFPGA_BUS_ERR("error to parse %s",
     206                 :            :                                      IFPGA_ARG_NAME);
     207                 :          0 :                                 goto end;
     208                 :            :                         }
     209                 :            :                 } else {
     210                 :          0 :                         IFPGA_BUS_ERR("arg %s is mandatory for ifpga bus",
     211                 :            :                           IFPGA_ARG_NAME);
     212                 :          0 :                         goto end;
     213                 :            :                 }
     214                 :            : 
     215                 :            :                 memset(name1, 0, sizeof(name1));
     216         [ #  # ]:          0 :                 snprintf(name1, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", name);
     217                 :            : 
     218                 :          0 :                 rawdev = rte_rawdev_pmd_get_named_dev(name1);
     219         [ #  # ]:          0 :                 if (!rawdev)
     220                 :          0 :                         goto end;
     221                 :            : 
     222                 :          0 :                 afu_dev = ifpga_scan_one(rawdev, devargs);
     223         [ #  # ]:          0 :                 if (afu_dev != NULL)
     224                 :          0 :                         rte_bus_add_device(&rte_ifpga_bus, &afu_dev->device);
     225                 :            :         }
     226                 :            : 
     227                 :        225 : end:
     228                 :        225 :         rte_kvargs_free(kvlist);
     229                 :        225 :         free(name);
     230                 :            : 
     231                 :        225 :         return 0;
     232                 :            : }
     233                 :            : 
     234                 :            : /*
     235                 :            :  * Match the AFU Driver and AFU Device using the ID Table
     236                 :            :  */
     237                 :            : static bool
     238                 :          0 : ifpga_bus_match(const struct rte_driver *drv, const struct rte_device *dev)
     239                 :            : {
     240                 :            :         const struct rte_afu_driver *afu_drv = RTE_BUS_DRIVER(drv, *afu_drv);
     241                 :            :         const struct rte_afu_device *afu_dev = RTE_BUS_DEVICE(dev, *afu_dev);
     242                 :            :         const struct rte_afu_uuid *id_table;
     243                 :            : 
     244                 :          0 :         for (id_table = afu_drv->id_table;
     245   [ #  #  #  # ]:          0 :                 ((id_table->uuid_low != 0) && (id_table->uuid_high != 0));
     246                 :          0 :              id_table++) {
     247                 :            :                 /* check if device's identifiers match the driver's ones */
     248         [ #  # ]:          0 :                 if ((id_table->uuid_low != afu_dev->id.uuid.uuid_low) ||
     249                 :            :                                 id_table->uuid_high !=
     250         [ #  # ]:          0 :                                  afu_dev->id.uuid.uuid_high)
     251                 :            :                         continue;
     252                 :            : 
     253                 :            :                 return true;
     254                 :            :         }
     255                 :            : 
     256                 :            :         return false;
     257                 :            : }
     258                 :            : 
     259                 :            : static int
     260                 :          0 : ifpga_probe_device(struct rte_driver *drv, struct rte_device *dev)
     261                 :            : {
     262                 :            :         struct rte_afu_device *afu_dev = RTE_BUS_DEVICE(dev, *afu_dev);
     263                 :            :         struct rte_afu_driver *afu_drv = RTE_BUS_DRIVER(drv, *afu_drv);
     264                 :            :         int ret;
     265                 :            : 
     266                 :          0 :         afu_dev->intr_handle =
     267                 :          0 :                 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
     268         [ #  # ]:          0 :         if (afu_dev->intr_handle == NULL) {
     269                 :          0 :                 IFPGA_BUS_ERR("Failed to allocate intr handle");
     270                 :          0 :                 return -ENOMEM;
     271                 :            :         }
     272                 :            : 
     273                 :          0 :         ret = afu_drv->probe(afu_dev);
     274         [ #  # ]:          0 :         if (ret != 0) {
     275                 :          0 :                 rte_intr_instance_free(afu_dev->intr_handle);
     276                 :          0 :                 afu_dev->intr_handle = NULL;
     277                 :            :         }
     278                 :            : 
     279                 :            :         return ret;
     280                 :            : }
     281                 :            : 
     282                 :            : static int
     283                 :          0 : ifpga_unplug_device(struct rte_device *dev)
     284                 :            : {
     285                 :          0 :         const struct rte_afu_driver *afu_drv = RTE_BUS_DRIVER(dev->driver, *afu_drv);
     286                 :            :         struct rte_afu_device *afu_dev = RTE_BUS_DEVICE(dev, *afu_dev);
     287                 :            :         int ret = 0;
     288                 :            : 
     289         [ #  # ]:          0 :         if (afu_drv->remove) {
     290                 :          0 :                 ret = afu_drv->remove(afu_dev);
     291         [ #  # ]:          0 :                 if (ret)
     292                 :            :                         return ret;
     293                 :            :         }
     294                 :            : 
     295                 :          0 :         rte_intr_instance_free(afu_dev->intr_handle);
     296                 :          0 :         afu_dev->intr_handle = NULL;
     297                 :            : 
     298                 :          0 :         return 0;
     299                 :            : }
     300                 :            : 
     301                 :            : static void
     302                 :          0 : ifpga_free_device(struct rte_device *dev)
     303                 :            : {
     304                 :          0 :         free(RTE_BUS_DEVICE(dev, struct rte_afu_device));
     305                 :          0 : }
     306                 :            : 
     307                 :            : static int
     308                 :         42 : ifpga_parse(const char *name, void *addr)
     309                 :            : {
     310                 :            :         int *out = addr;
     311                 :            :         struct rte_rawdev *rawdev = NULL;
     312                 :            :         char rawdev_name[RTE_RAWDEV_NAME_MAX_LEN];
     313                 :            :         const char *c1 = NULL;
     314                 :            :         const char *c2 = NULL;
     315         [ -  + ]:         42 :         int port = IFPGA_BUS_DEV_PORT_MAX;
     316                 :            :         char str_port[8];
     317                 :            :         int str_port_len = 0;
     318                 :            :         int ret;
     319                 :            : 
     320                 :            :         memset(str_port, 0, 8);
     321                 :         42 :         c1 = strchr(name, '|');
     322         [ -  + ]:         42 :         if (c1 != NULL) {
     323                 :          0 :                 str_port_len = c1 - name;
     324                 :          0 :                 c2 = c1 + 1;
     325                 :            :         }
     326                 :            : 
     327         [ -  + ]:         42 :         if (str_port_len < 8 &&
     328                 :            :                 str_port_len > 0) {
     329         [ #  # ]:          0 :                 memcpy(str_port, name, str_port_len);
     330                 :          0 :                 ret = sscanf(str_port, "%d", &port);
     331         [ #  # ]:          0 :                 if (ret == -1)
     332                 :            :                         return 0;
     333                 :            :         }
     334                 :            : 
     335                 :            :         memset(rawdev_name, 0, sizeof(rawdev_name));
     336                 :            :         snprintf(rawdev_name, RTE_RAWDEV_NAME_MAX_LEN, "IFPGA:%s", c2);
     337                 :         42 :         rawdev = rte_rawdev_pmd_get_named_dev(rawdev_name);
     338                 :            : 
     339         [ -  + ]:         42 :         if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
     340                 :          0 :                 rawdev &&
     341         [ #  # ]:          0 :                 (addr != NULL))
     342                 :          0 :                 *out = port;
     343                 :            : 
     344   [ -  +  -  - ]:         42 :         if ((port < IFPGA_BUS_DEV_PORT_MAX) &&
     345                 :            :                 rawdev)
     346                 :            :                 return 0;
     347                 :            :         else
     348                 :         42 :                 return 1;
     349                 :            : }
     350                 :            : 
     351                 :            : static struct rte_bus rte_ifpga_bus = {
     352                 :            :         .scan        = ifpga_scan,
     353                 :            :         .probe       = rte_bus_generic_probe,
     354                 :            :         .free_device = ifpga_free_device,
     355                 :            :         .cleanup     = rte_bus_generic_cleanup,
     356                 :            :         .find_device = rte_bus_generic_find_device,
     357                 :            :         .match       = ifpga_bus_match,
     358                 :            :         .probe_device = ifpga_probe_device,
     359                 :            :         .unplug_device = ifpga_unplug_device,
     360                 :            :         .parse       = ifpga_parse,
     361                 :            : };
     362                 :            : 
     363                 :        301 : RTE_REGISTER_BUS(ifpga, rte_ifpga_bus);
     364                 :        301 : RTE_INIT(ifpga_bus_init)
     365                 :            : {
     366         [ -  + ]:        301 :         RTE_VERIFY(strcmp(rte_ifpga_bus.name, RTE_STR(IFPGA_BUS_NAME)) == 0);
     367                 :        301 : }
     368         [ -  + ]:        301 : RTE_LOG_REGISTER_DEFAULT(ifpga_bus_logtype, NOTICE);

Generated by: LCOV version 1.14