LCOV - code coverage report
Current view: top level - drivers/bus/fslmc - fslmc_bus.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 52 303 17.2 %
Date: 2026-11-01 19:11:22 Functions: 8 22 36.4 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 27 238 11.3 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *
       3                 :            :  *   Copyright 2016,2018-2021 NXP
       4                 :            :  *
       5                 :            :  */
       6                 :            : 
       7                 :            : #include <string.h>
       8                 :            : #include <dirent.h>
       9                 :            : #include <stdalign.h>
      10                 :            : #include <stdbool.h>
      11                 :            : 
      12                 :            : #include <eal_export.h>
      13                 :            : #include <rte_log.h>
      14                 :            : #include <bus_driver.h>
      15                 :            : #include <rte_malloc.h>
      16                 :            : #include <rte_devargs.h>
      17                 :            : #include <rte_memcpy.h>
      18                 :            : #include <ethdev_driver.h>
      19                 :            : #include <rte_mbuf_dyn.h>
      20                 :            : #include <dev_vfio.h>
      21                 :            : 
      22                 :            : #include "private.h"
      23                 :            : #include <fslmc_vfio.h>
      24                 :            : #include "fslmc_logs.h"
      25                 :            : 
      26                 :            : #include <dpaax_iova_table.h>
      27                 :            : 
      28                 :            : #define VFIO_IOMMU_GROUP_PATH "/sys/kernel/iommu_groups"
      29                 :            : 
      30                 :            : struct rte_bus rte_fslmc_bus;
      31                 :            : static int fslmc_bus_device_count[DPAA2_DEVTYPE_MAX];
      32                 :            : 
      33                 :            : /*
      34                 :            :  * Control devices (MPORTAL, IO, CON, CI, BPOOL, MUX, DPRC) are bus infrastructure,
      35                 :            :  * not regular devices. They are initialized during scan/setup and don't participate
      36                 :            :  * in normal probe/unplug. Keep them in a separate list to avoid interference with
      37                 :            :  * generic bus operations.
      38                 :            :  */
      39                 :            : struct fslmc_control_device_list fslmc_control_devices =
      40                 :            :         TAILQ_HEAD_INITIALIZER(fslmc_control_devices);
      41                 :            : 
      42                 :            : #define DPAA2_SEQN_DYNFIELD_NAME "dpaa2_seqn_dynfield"
      43                 :            : RTE_EXPORT_INTERNAL_SYMBOL(dpaa2_seqn_dynfield_offset)
      44                 :            : int dpaa2_seqn_dynfield_offset = -1;
      45                 :            : 
      46                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_fslmc_get_device_count)
      47                 :            : uint32_t
      48                 :          0 : rte_fslmc_get_device_count(enum rte_dpaa2_dev_type device_type)
      49                 :            : {
      50         [ #  # ]:          0 :         if (device_type >= DPAA2_DEVTYPE_MAX)
      51                 :            :                 return 0;
      52                 :          0 :         return fslmc_bus_device_count[device_type];
      53                 :            : }
      54                 :            : 
      55                 :            : static int
      56                 :            : compare_dpaa2_devname(struct rte_dpaa2_device *dev1,
      57                 :            :                       struct rte_dpaa2_device *dev2)
      58                 :            : {
      59                 :            :         int comp;
      60                 :            : 
      61         [ #  # ]:          0 :         if (dev1->dev_type > dev2->dev_type) {
      62                 :            :                 comp = 1;
      63   [ #  #  #  # ]:          0 :         } else if (dev1->dev_type < dev2->dev_type) {
      64                 :            :                 comp = -1;
      65                 :            :         } else {
      66                 :            :                 /* Check the ID as types match */
      67   [ #  #  #  # ]:          0 :                 if (dev1->object_id > dev2->object_id)
      68                 :            :                         comp = 1;
      69   [ #  #  #  # ]:          0 :                 else if (dev1->object_id < dev2->object_id)
      70                 :            :                         comp = -1;
      71                 :            :                 else
      72                 :            :                         comp = 0; /* Duplicate device name */
      73                 :            :         }
      74                 :            : 
      75                 :            :         return comp;
      76                 :            : }
      77                 :            : 
      78                 :            : static void
      79                 :          0 : insert_in_device_list(struct rte_dpaa2_device *newdev)
      80                 :            : {
      81                 :            :         int comp, inserted = 0;
      82                 :            :         struct rte_dpaa2_device *dev = NULL;
      83                 :            : 
      84   [ #  #  #  # ]:          0 :         RTE_BUS_FOREACH_DEV(dev, &rte_fslmc_bus) {
      85                 :            :                 comp = compare_dpaa2_devname(newdev, dev);
      86                 :            :                 if (comp < 0) {
      87                 :          0 :                         rte_bus_insert_device(&rte_fslmc_bus, &dev->device, &newdev->device);
      88                 :            :                         inserted = 1;
      89                 :            :                         break;
      90                 :            :                 }
      91                 :            :         }
      92                 :            : 
      93                 :            :         if (!inserted)
      94                 :          0 :                 rte_bus_add_device(&rte_fslmc_bus, &newdev->device);
      95                 :            : 
      96         [ #  # ]:          0 :         if (newdev->dev_type < DPAA2_DEVTYPE_MAX)
      97                 :          0 :                 fslmc_bus_device_count[newdev->dev_type]++;
      98                 :          0 : }
      99                 :            : 
     100                 :            : static void
     101                 :          0 : fslmc_free_device(struct rte_device *rte_dev)
     102                 :            : {
     103                 :            :         struct rte_dpaa2_device *dev = RTE_BUS_DEVICE(rte_dev, *dev);
     104                 :            : 
     105         [ #  # ]:          0 :         if (dev->dev_type < DPAA2_DEVTYPE_MAX)
     106                 :          0 :                 fslmc_bus_device_count[dev->dev_type]--;
     107                 :          0 :         free(dev);
     108                 :          0 : }
     109                 :            : 
     110                 :            : void
     111                 :          0 : fslmc_bus_remove_device(struct rte_dpaa2_device *dev)
     112                 :            : {
     113                 :          0 :         rte_bus_remove_device(&rte_fslmc_bus, &dev->device);
     114                 :          0 :         fslmc_free_device(&dev->device);
     115                 :          0 : }
     116                 :            : 
     117                 :            : static void
     118                 :          0 : insert_in_control_device_list(struct rte_dpaa2_device *dev)
     119                 :            : {
     120                 :            :         struct rte_device *rte_dev;
     121                 :            :         struct rte_device *prev;
     122                 :            : 
     123                 :            :         prev = NULL;
     124         [ #  # ]:          0 :         TAILQ_FOREACH(rte_dev, &fslmc_control_devices, next) {
     125                 :            :                 if (compare_dpaa2_devname(dev, RTE_BUS_DEVICE(rte_dev, *dev)) < 0)
     126                 :            :                         break;
     127                 :            :                 prev = rte_dev;
     128                 :            :         }
     129                 :            : 
     130         [ #  # ]:          0 :         if (prev != NULL)
     131         [ #  # ]:          0 :                 TAILQ_INSERT_AFTER(&fslmc_control_devices, prev, &dev->device, next);
     132                 :            :         else
     133         [ #  # ]:          0 :                 TAILQ_INSERT_HEAD(&fslmc_control_devices, &dev->device, next);
     134                 :            : 
     135         [ #  # ]:          0 :         if (dev->dev_type < DPAA2_DEVTYPE_MAX)
     136                 :          0 :                 fslmc_bus_device_count[dev->dev_type]++;
     137                 :          0 : }
     138                 :            : 
     139                 :            : void
     140                 :          0 : fslmc_remove_control_device(struct rte_dpaa2_device *dev)
     141                 :            : {
     142         [ #  # ]:          0 :         if (dev->dev_type < DPAA2_DEVTYPE_MAX)
     143                 :          0 :                 fslmc_bus_device_count[dev->dev_type]--;
     144                 :            : 
     145         [ #  # ]:          0 :         TAILQ_REMOVE(&fslmc_control_devices, &dev->device, next);
     146                 :          0 :         free(dev);
     147                 :          0 : }
     148                 :            : 
     149                 :            : static void
     150                 :          0 : dump_device_list(void)
     151                 :            : {
     152                 :            :         struct rte_dpaa2_device *dev;
     153                 :            :         struct rte_device *rte_dev;
     154                 :            : 
     155                 :            :         /* Only if the log level has been set to Debugging, print list */
     156         [ #  # ]:          0 :         if (rte_log_can_log(dpaa2_logtype_bus, RTE_LOG_DEBUG)) {
     157                 :          0 :                 DPAA2_BUS_LOG(DEBUG, "List of devices scanned on bus:");
     158         [ #  # ]:          0 :                 RTE_BUS_FOREACH_DEV(dev, &rte_fslmc_bus) {
     159                 :          0 :                         DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
     160                 :            :                 }
     161                 :          0 :                 DPAA2_BUS_LOG(DEBUG, "List of control devices:");
     162         [ #  # ]:          0 :                 TAILQ_FOREACH(rte_dev, &fslmc_control_devices, next) {
     163                 :            :                         dev = RTE_BUS_DEVICE(rte_dev, struct rte_dpaa2_device);
     164                 :          0 :                         DPAA2_BUS_LOG(DEBUG, "\t\t%s", dev->device.name);
     165                 :            :                 }
     166                 :            :         }
     167                 :          0 : }
     168                 :            : 
     169                 :            : static int
     170                 :          0 : scan_one_fslmc_device(char *dev_name)
     171                 :            : {
     172                 :            :         enum rte_dpaa2_dev_type dev_type = DPAA2_UNKNOWN;
     173                 :            :         struct rte_dpaa2_device *dev = NULL;
     174                 :            :         static const struct {
     175                 :            :                 const char *prefix;
     176                 :            :                 enum rte_dpaa2_dev_type type;
     177                 :            :         } dev_types[] = {
     178                 :            :                 { "dpni.", DPAA2_ETH },
     179                 :            :                 { "dpseci.", DPAA2_CRYPTO },
     180                 :            :                 { "dpcon.", DPAA2_CON },
     181                 :            :                 { "dpbp.", DPAA2_BPOOL },
     182                 :            :                 { "dpio.", DPAA2_IO },
     183                 :            :                 { "dpci.", DPAA2_CI },
     184                 :            :                 { "dpmcp.", DPAA2_MPORTAL },
     185                 :            :                 { "dpdmai.", DPAA2_QDMA },
     186                 :            :                 { "dpdmux.", DPAA2_MUX },
     187                 :            :                 { "dprtc.", DPAA2_DPRTC },
     188                 :            :                 { "dprc.", DPAA2_DPRC },
     189                 :            :         };
     190                 :            :         char *dev_id;
     191                 :            :         int ret = -1;
     192                 :            : 
     193         [ #  # ]:          0 :         if (dev_name == NULL)
     194                 :            :                 return ret;
     195                 :            : 
     196         [ #  # ]:          0 :         for (unsigned int i = 0; i < RTE_DIM(dev_types); i++) {
     197         [ #  # ]:          0 :                 if (strncmp(dev_types[i].prefix, dev_name, strlen(dev_types[i].prefix)) != 0)
     198                 :            :                         continue;
     199                 :          0 :                 dev_id = dev_name + strlen(dev_types[i].prefix);
     200                 :          0 :                 dev_type = dev_types[i].type;
     201                 :          0 :                 break;
     202                 :            :         }
     203                 :            : 
     204         [ #  # ]:          0 :         if (dev_type == DPAA2_UNKNOWN) {
     205                 :          0 :                 char *ptr = strchr(dev_name, '.');
     206                 :            : 
     207         [ #  # ]:          0 :                 if (ptr == NULL) {
     208                 :          0 :                         DPAA2_BUS_ERR("Invalid device found: (%s)", dev_name);
     209                 :            :                         ret = 0;
     210                 :          0 :                         goto cleanup;
     211                 :            :                 }
     212                 :          0 :                 dev_id = ptr + 1;
     213                 :            :         }
     214                 :            : 
     215                 :            :         /*
     216                 :            :          * DPAA2_MPORTAL and DPAA2_IO types are handled separately,
     217                 :            :          * see fslmc_filter_control_devices()
     218                 :            :          *
     219                 :            :          * Note: Only check for explicit blocklist (RTE_DEV_BLOCKED).
     220                 :            :          * Control objects (dpbp, dpcon, etc.) are required even in allowlist
     221                 :            :          * mode as they are initialized by fslmc_vfio_process_group(), not probed.
     222                 :            :          */
     223         [ #  # ]:          0 :         if (dev_type != DPAA2_MPORTAL && dev_type != DPAA2_IO) {
     224                 :          0 :                 struct rte_devargs *devargs = rte_bus_find_devargs(&rte_fslmc_bus, dev_name);
     225                 :            : 
     226   [ #  #  #  # ]:          0 :                 if (devargs && devargs->policy == RTE_DEV_BLOCKED) {
     227                 :          0 :                         DPAA2_BUS_DEBUG("Skipping blocklisted device (%s)", dev_name);
     228                 :          0 :                         return 0;
     229                 :            :                 }
     230                 :            :         }
     231                 :            : 
     232                 :            :         /* For secondary processes, control objects are not needed */
     233         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
     234         [ #  # ]:          0 :                 switch (dev_type) {
     235                 :            :                 case DPAA2_ETH:
     236                 :            :                 case DPAA2_CRYPTO:
     237                 :            :                 case DPAA2_QDMA:
     238                 :            :                 case DPAA2_IO:
     239                 :            :                 case DPAA2_MPORTAL:
     240                 :            :                 case DPAA2_DPRC:
     241                 :            :                         break;
     242                 :          0 :                 default:
     243                 :          0 :                         DPAA2_BUS_DEBUG("Skipping device in secondary process (%s)", dev_name);
     244                 :          0 :                         return 0;
     245                 :            :                 }
     246                 :            :         }
     247                 :            : 
     248                 :          0 :         dev = calloc(1, sizeof(struct rte_dpaa2_device));
     249         [ #  # ]:          0 :         if (!dev) {
     250                 :          0 :                 DPAA2_BUS_ERR("Unable to allocate device object");
     251                 :          0 :                 return -ENOMEM;
     252                 :            :         }
     253                 :            : 
     254                 :          0 :         dev->device.numa_node = SOCKET_ID_ANY;
     255                 :          0 :         dev->dev_type = dev_type;
     256                 :            : 
     257         [ #  # ]:          0 :         if (sscanf(dev_id, "%hu", &dev->object_id) != 1) {
     258                 :          0 :                 DPAA2_BUS_ERR("Failed to parse object ID");
     259                 :            :                 ret = -EINVAL;
     260                 :          0 :                 goto cleanup;
     261                 :            :         }
     262                 :            : 
     263         [ #  # ]:          0 :         if (rte_strscpy(dev->name, dev_name, sizeof(dev->name)) < 0) {
     264                 :          0 :                 DPAA2_BUS_ERR("Unable to copy device name: %s", rte_strerror(rte_errno));
     265                 :          0 :                 ret = -rte_errno;
     266                 :          0 :                 goto cleanup;
     267                 :            :         }
     268                 :          0 :         dev->device.name = dev->name;
     269                 :          0 :         dev->device.devargs = rte_bus_find_devargs(&rte_fslmc_bus, dev_name);
     270                 :            : 
     271         [ #  # ]:          0 :         switch (dev_type) {
     272                 :          0 :         case DPAA2_ETH:
     273                 :            :         case DPAA2_CRYPTO:
     274                 :            :         case DPAA2_QDMA:
     275                 :          0 :                 insert_in_device_list(dev);
     276                 :          0 :                 break;
     277                 :          0 :         default:
     278                 :          0 :                 insert_in_control_device_list(dev);
     279                 :          0 :                 break;
     280                 :            :         }
     281                 :            : 
     282                 :            :         return 0;
     283                 :          0 : cleanup:
     284                 :          0 :         free(dev);
     285                 :          0 :         return ret;
     286                 :            : }
     287                 :            : 
     288                 :            : static int
     289                 :         41 : rte_fslmc_parse(const char *name, void *addr)
     290                 :            : {
     291                 :            :         uint16_t dev_id;
     292                 :            :         const char *t_ptr;
     293                 :            :         const char *sep;
     294                 :            :         uint8_t sep_exists = 0;
     295                 :            :         int ret = -1;
     296                 :            : 
     297                 :            :         /* There are multiple ways this can be called, with bus:dev, name=dev
     298                 :            :          * or just dev. In all cases, the 'addr' is actually a string.
     299                 :            :          */
     300                 :         41 :         sep = strchr(name, ':');
     301         [ +  + ]:         41 :         if (!sep) {
     302                 :            :                 /* check for '=' */
     303                 :         19 :                 sep = strchr(name, '=');
     304         [ +  + ]:         19 :                 if (!sep)
     305                 :            :                         sep_exists = 0;
     306                 :            :                 else
     307                 :            :                         sep_exists = 1;
     308                 :            :         } else
     309                 :            :                 sep_exists = 1;
     310                 :            : 
     311                 :            :         /* Check if starting part if either of 'fslmc:' or 'name=', separator
     312                 :            :          * exists.
     313                 :            :          */
     314                 :            :         if (sep_exists) {
     315                 :            :                 /* If either of "fslmc" or "name" are starting part */
     316         [ +  - ]:         24 :                 if (!strncmp(name, rte_fslmc_bus.name, strlen(rte_fslmc_bus.name)) ||
     317         [ -  + ]:         24 :                    (!strncmp(name, "name", strlen("name")))) {
     318                 :          0 :                         goto jump_out;
     319                 :            :                 } else {
     320                 :         24 :                         DPAA2_BUS_DEBUG("Invalid device for matching (%s).",
     321                 :            :                                         name);
     322                 :            :                         ret = -EINVAL;
     323                 :         24 :                         goto err_out;
     324                 :            :                 }
     325                 :            :         } else
     326                 :            :                 sep = name;
     327                 :            : 
     328                 :         17 : jump_out:
     329                 :            :         /* Validate device name */
     330         [ +  - ]:         17 :         if (strncmp("dpni", sep, 4) &&
     331         [ +  - ]:         17 :             strncmp("dpseci", sep, 6) &&
     332         [ +  - ]:         17 :             strncmp("dpcon", sep, 5) &&
     333         [ +  - ]:         17 :             strncmp("dpbp", sep, 4) &&
     334         [ +  - ]:         17 :             strncmp("dpio", sep, 4) &&
     335         [ +  - ]:         17 :             strncmp("dpci", sep, 4) &&
     336         [ +  - ]:         17 :             strncmp("dpmcp", sep, 5) &&
     337         [ +  - ]:         17 :             strncmp("dpdmai", sep, 6) &&
     338         [ +  - ]:         17 :             strncmp("dpdmux", sep, 6)) {
     339                 :         17 :                 DPAA2_BUS_DEBUG("Unknown or unsupported device (%s)", sep);
     340                 :            :                 ret = -EINVAL;
     341                 :         17 :                 goto err_out;
     342                 :            :         }
     343                 :            : 
     344                 :          0 :         t_ptr = strchr(sep, '.');
     345   [ #  #  #  # ]:          0 :         if (!t_ptr || sscanf(t_ptr + 1, "%hu", &dev_id) != 1) {
     346                 :          0 :                 DPAA2_BUS_ERR("Missing device id in device name (%s)", sep);
     347                 :            :                 ret = -EINVAL;
     348                 :          0 :                 goto err_out;
     349                 :            :         }
     350                 :            : 
     351         [ #  # ]:          0 :         if (addr)
     352                 :            :                 strcpy(addr, sep);
     353                 :            : 
     354                 :            :         ret = 0;
     355                 :         41 : err_out:
     356                 :         41 :         return ret;
     357                 :            : }
     358                 :            : 
     359                 :            : static int
     360                 :          0 : fslmc_dev_compare(const char *name1, const char *name2)
     361                 :            : {
     362                 :            :         char devname1[32], devname2[32];
     363                 :            : 
     364   [ #  #  #  # ]:          0 :         if (rte_fslmc_parse(name1, devname1) != 0 ||
     365                 :          0 :                         rte_fslmc_parse(name2, devname2) != 0)
     366                 :          0 :                 return 1;
     367                 :            : 
     368                 :          0 :         return strncmp(devname1, devname2, sizeof(devname1));
     369                 :            : }
     370                 :            : 
     371                 :            : static int
     372                 :          0 : fslmc_filter_control_devices(void)
     373                 :            : {
     374                 :            :         bool is_dpmcp_in_blocklist = false, is_dpio_in_blocklist = false;
     375                 :            :         int dpmcp_count = 0, dpio_count = 0;
     376                 :            :         struct rte_dpaa2_device *dev;
     377                 :            :         struct rte_device *rte_dev;
     378                 :            :         struct rte_device *tmp_dev;
     379                 :            : 
     380                 :            :         /* Track MPORTAL/DPIO blocklists */
     381         [ #  # ]:          0 :         RTE_TAILQ_FOREACH_SAFE(rte_dev, &fslmc_control_devices, next, tmp_dev) {
     382                 :            :                 dev = RTE_BUS_DEVICE(rte_dev, *dev);
     383         [ #  # ]:          0 :                 if (dev->dev_type != DPAA2_MPORTAL && dev->dev_type != DPAA2_IO)
     384                 :          0 :                         continue;
     385                 :            :                 /* Only check for explicit blocklist, not allowlist */
     386   [ #  #  #  # ]:          0 :                 if (dev->device.devargs && dev->device.devargs->policy == RTE_DEV_BLOCKED) {
     387                 :          0 :                         DPAA2_BUS_LOG(DEBUG, "%s Blocked, skipping", dev->device.name);
     388         [ #  # ]:          0 :                         if (dev->dev_type == DPAA2_MPORTAL)
     389                 :            :                                 is_dpmcp_in_blocklist = true;
     390         [ #  # ]:          0 :                         else if (dev->dev_type == DPAA2_IO)
     391                 :            :                                 is_dpio_in_blocklist = true;
     392                 :          0 :                         fslmc_remove_control_device(dev);
     393                 :          0 :                         continue;
     394                 :            :                 }
     395         [ #  # ]:          0 :                 if (dev->dev_type == DPAA2_MPORTAL)
     396                 :          0 :                         dpmcp_count++;
     397                 :            :                 else if (dev->dev_type == DPAA2_IO)
     398                 :          0 :                         dpio_count++;
     399                 :            :         }
     400                 :            : 
     401         [ #  # ]:          0 :         if (dpmcp_count == 0) {
     402                 :          0 :                 DPAA2_BUS_ERR("No MC Portal device found");
     403                 :          0 :                 return -ENODEV;
     404                 :            :         }
     405                 :            : 
     406                 :            :         /* Automatic MPORTAL split: primary keeps first, secondary keeps last */
     407         [ #  # ]:          0 :         if (!is_dpmcp_in_blocklist) {
     408                 :            :                 int current_device = 0;
     409                 :            :                 int keep_index;
     410                 :            : 
     411                 :            :                 /* Check MPORTAL availability for secondary */
     412   [ #  #  #  # ]:          0 :                 if (rte_eal_process_type() == RTE_PROC_SECONDARY && dpmcp_count < 2) {
     413                 :          0 :                         DPAA2_BUS_ERR("No MC Portal device found for secondary");
     414                 :          0 :                         return -ENODEV;
     415                 :            :                 }
     416                 :            : 
     417         [ #  # ]:          0 :                 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
     418                 :            :                         keep_index = 0;
     419                 :            :                 else
     420                 :          0 :                         keep_index = dpmcp_count - 1;
     421                 :            : 
     422         [ #  # ]:          0 :                 RTE_TAILQ_FOREACH_SAFE(rte_dev, &fslmc_control_devices, next, tmp_dev) {
     423                 :            :                         dev = RTE_BUS_DEVICE(rte_dev, *dev);
     424         [ #  # ]:          0 :                         if (dev->dev_type != DPAA2_MPORTAL)
     425                 :          0 :                                 continue;
     426         [ #  # ]:          0 :                         if (current_device != keep_index)
     427                 :          0 :                                 fslmc_remove_control_device(dev);
     428                 :            : 
     429                 :          0 :                         current_device++;
     430         [ #  # ]:          0 :                         if (current_device == dpmcp_count)
     431                 :            :                                 break;
     432                 :            :                 }
     433                 :            :         }
     434                 :            : 
     435                 :            :         /* Automatic DPIO split: secondary keeps last only, primary removes last */
     436         [ #  # ]:          0 :         if (!is_dpio_in_blocklist && dpio_count > 1) {
     437                 :          0 :                 int last_index = dpio_count - 1;
     438                 :            :                 int current_device = 0;
     439                 :            : 
     440         [ #  # ]:          0 :                 RTE_TAILQ_FOREACH_SAFE(rte_dev, &fslmc_control_devices, next, tmp_dev) {
     441                 :            :                         dev = RTE_BUS_DEVICE(rte_dev, *dev);
     442         [ #  # ]:          0 :                         if (dev->dev_type != DPAA2_IO)
     443                 :          0 :                                 continue;
     444                 :            : 
     445   [ #  #  #  # ]:          0 :                         if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
     446                 :            :                                         current_device != last_index) {
     447                 :          0 :                                 fslmc_remove_control_device(dev);
     448   [ #  #  #  # ]:          0 :                         } else if (rte_eal_process_type() == RTE_PROC_PRIMARY &&
     449                 :            :                                         current_device == last_index) {
     450                 :          0 :                                 fslmc_remove_control_device(dev);
     451                 :            :                         }
     452                 :            : 
     453                 :          0 :                         current_device++;
     454         [ #  # ]:          0 :                         if (current_device == dpio_count)
     455                 :            :                                 break;
     456                 :            :                 }
     457                 :            :         }
     458                 :            : 
     459                 :            :         return 0;
     460                 :            : }
     461                 :            : 
     462                 :            : static int
     463                 :        229 : rte_fslmc_scan(void)
     464                 :            : {
     465                 :            :         struct rte_dpaa2_device *dev;
     466                 :            :         int ret;
     467                 :            :         char fslmc_dirpath[PATH_MAX];
     468                 :            :         DIR *dir;
     469                 :            :         struct dirent *entry;
     470                 :            :         static int process_once;
     471                 :            :         int groupid;
     472                 :            :         char *group_name;
     473                 :            : 
     474         [ -  + ]:        229 :         if (process_once) {
     475                 :          0 :                 DPAA2_BUS_DEBUG("Fslmc bus already scanned. Not rescanning");
     476         [ #  # ]:          0 :                 RTE_BUS_FOREACH_DEV(dev, &rte_fslmc_bus) {
     477                 :          0 :                         dev->device.devargs = rte_bus_find_devargs(&rte_fslmc_bus,
     478                 :            :                                 dev->device.name);
     479                 :            :                 }
     480                 :            :                 return 0;
     481                 :            :         }
     482                 :            : 
     483                 :            :         /* Now we only support single group per process.*/
     484                 :        229 :         group_name = getenv("DPRC");
     485         [ +  - ]:        229 :         if (!group_name) {
     486                 :        229 :                 DPAA2_BUS_DEBUG("DPAA2: DPRC not available");
     487                 :            :                 ret = -EINVAL;
     488                 :        229 :                 goto scan_fail;
     489                 :            :         }
     490                 :            : 
     491                 :            :         /* for container groups to work, VFIO must be in group mode */
     492         [ #  # ]:          0 :         if (dev_vfio_get_mode() != DEV_VFIO_MODE_GROUP) {
     493                 :          0 :                 DPAA2_BUS_DEBUG("DPAA2: VFIO not in group mode");
     494                 :            :                 ret = -EINVAL;
     495                 :          0 :                 goto scan_fail;
     496                 :            :         }
     497                 :            : 
     498                 :          0 :         ret = fslmc_get_container_group(group_name, &groupid);
     499         [ #  # ]:          0 :         if (ret != 0)
     500                 :          0 :                 goto scan_fail;
     501                 :            : 
     502                 :            :         /* Scan devices on the group */
     503                 :            :         sprintf(fslmc_dirpath, "%s/%s", SYSFS_FSL_MC_DEVICES, group_name);
     504                 :          0 :         dir = opendir(fslmc_dirpath);
     505         [ #  # ]:          0 :         if (!dir) {
     506                 :          0 :                 DPAA2_BUS_ERR("Unable to open VFIO group directory");
     507                 :          0 :                 goto scan_fail;
     508                 :            :         }
     509                 :            : 
     510                 :            :         /* Scan the DPRC container object */
     511                 :          0 :         ret = scan_one_fslmc_device(group_name);
     512         [ #  # ]:          0 :         if (ret != 0) {
     513                 :            :                 /* Error in parsing directory - exit gracefully */
     514                 :          0 :                 goto scan_fail_cleanup;
     515                 :            :         }
     516                 :            : 
     517         [ #  # ]:          0 :         while ((entry = readdir(dir)) != NULL) {
     518   [ #  #  #  # ]:          0 :                 if (entry->d_name[0] == '.' || entry->d_type != DT_DIR)
     519                 :          0 :                         continue;
     520                 :            : 
     521                 :          0 :                 ret = scan_one_fslmc_device(entry->d_name);
     522         [ #  # ]:          0 :                 if (ret != 0) {
     523                 :            :                         /* Error in parsing directory - exit gracefully */
     524                 :          0 :                         goto scan_fail_cleanup;
     525                 :            :                 }
     526                 :            :         }
     527                 :            : 
     528                 :          0 :         closedir(dir);
     529                 :            : 
     530                 :          0 :         DPAA2_BUS_INFO("FSLMC Bus scan completed");
     531                 :            :         /* If debugging is enabled, device list is dumped to log output */
     532                 :          0 :         dump_device_list();
     533                 :            : 
     534                 :          0 :         process_once = 1;
     535                 :            : 
     536                 :          0 :         return 0;
     537                 :            : 
     538                 :          0 : scan_fail_cleanup:
     539                 :          0 :         closedir(dir);
     540                 :            : 
     541         [ #  # ]:          0 :         while (!TAILQ_EMPTY(&fslmc_control_devices)) {
     542                 :            :                 struct rte_device *rte_dev = TAILQ_FIRST(&fslmc_control_devices);
     543                 :            : 
     544                 :            :                 dev = RTE_BUS_DEVICE(rte_dev, *dev);
     545                 :          0 :                 fslmc_remove_control_device(dev);
     546                 :            :         }
     547                 :            : 
     548         [ #  # ]:          0 :         RTE_BUS_FOREACH_DEV(dev, &rte_fslmc_bus)
     549                 :          0 :                 fslmc_bus_remove_device(dev);
     550                 :        229 : scan_fail:
     551                 :        229 :         DPAA2_BUS_DEBUG("FSLMC Bus Not Available. Skipping (%d)", ret);
     552                 :            :         /* Irrespective of failure, scan only return success */
     553                 :        229 :         return 0;
     554                 :            : }
     555                 :            : 
     556                 :            : /* Bus initialization needs the DPDK heap and DMA mapping of the memory
     557                 :            :  * segments, which EAL only sets up after the bus scan, so it is done here.
     558                 :            :  */
     559                 :            : static int
     560                 :        224 : rte_fslmc_probe(struct rte_bus *bus)
     561                 :            : {
     562                 :            :         static const struct rte_mbuf_dynfield dpaa2_seqn_dynfield_desc = {
     563                 :            :                 .name = DPAA2_SEQN_DYNFIELD_NAME,
     564                 :            :                 .size = sizeof(dpaa2_seqn_t),
     565                 :            :                 .align = alignof(dpaa2_seqn_t),
     566                 :            :         };
     567                 :            :         int ret;
     568                 :            : 
     569   [ +  -  -  + ]:        224 :         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list) && TAILQ_EMPTY(&fslmc_control_devices))
     570                 :            :                 return 0;
     571                 :            : 
     572                 :          0 :         dpaa2_seqn_dynfield_offset =
     573                 :          0 :                 rte_mbuf_dynfield_register(&dpaa2_seqn_dynfield_desc);
     574         [ #  # ]:          0 :         if (dpaa2_seqn_dynfield_offset < 0) {
     575                 :          0 :                 DPAA2_BUS_ERR("Failed to register mbuf field for dpaa sequence number");
     576                 :          0 :                 return dpaa2_seqn_dynfield_offset;
     577                 :            :         }
     578                 :            : 
     579                 :          0 :         ret = fslmc_vfio_setup_group();
     580         [ #  # ]:          0 :         if (ret) {
     581                 :          0 :                 DPAA2_BUS_ERR("Unable to setup VFIO %d", ret);
     582                 :          0 :                 return ret;
     583                 :            :         }
     584                 :            : 
     585                 :            :         /* Map existing segments as well as, in case of hotpluggable memory,
     586                 :            :          * install callback handler.
     587                 :            :          */
     588         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
     589                 :          0 :                 ret = fslmc_vfio_dmamap();
     590         [ #  # ]:          0 :                 if (ret) {
     591                 :          0 :                         DPAA2_BUS_ERR("Unable to DMA map existing VAs: (%d)", ret);
     592                 :          0 :                         DPAA2_BUS_ERR("FSLMC VFIO Mapping failed");
     593                 :          0 :                         goto vfio_close_group;
     594                 :            :                 }
     595                 :            :         }
     596                 :            : 
     597                 :          0 :         ret = fslmc_filter_control_devices();
     598         [ #  # ]:          0 :         if (ret) {
     599                 :          0 :                 DPAA2_BUS_ERR("Unable to filter control devices %d", ret);
     600                 :          0 :                 goto vfio_dma_unmap;
     601                 :            :         }
     602                 :            : 
     603                 :          0 :         ret = fslmc_vfio_process_group();
     604         [ #  # ]:          0 :         if (ret) {
     605                 :          0 :                 DPAA2_BUS_ERR("Unable to setup devices %d", ret);
     606                 :          0 :                 goto vfio_dma_unmap;
     607                 :            :         }
     608                 :            : 
     609                 :          0 :         return rte_bus_generic_probe(bus);
     610                 :            : 
     611                 :          0 : vfio_dma_unmap:
     612                 :          0 :         fslmc_vfio_dmaunmap();
     613                 :          0 : vfio_close_group:
     614                 :          0 :         fslmc_vfio_close_group();
     615                 :          0 :         return ret;
     616                 :            : }
     617                 :            : 
     618                 :            : static bool
     619                 :          0 : fslmc_bus_match(const struct rte_driver *drv, const struct rte_device *dev)
     620                 :            : {
     621                 :            :         const struct rte_dpaa2_driver *dpaa2_drv = RTE_BUS_DRIVER(drv, *dpaa2_drv);
     622                 :            :         const struct rte_dpaa2_device *dpaa2_dev = RTE_BUS_DEVICE(dev, *dpaa2_dev);
     623                 :            : 
     624         [ #  # ]:          0 :         if (dpaa2_drv->drv_type == dpaa2_dev->dev_type)
     625                 :          0 :                 return true;
     626                 :            : 
     627                 :            :         return false;
     628                 :            : }
     629                 :            : 
     630                 :            : static int
     631                 :        298 : fslmc_cleanup(struct rte_bus *bus)
     632                 :            : {
     633                 :            :         int ret;
     634                 :            : 
     635                 :        298 :         ret = rte_bus_generic_cleanup(bus);
     636         [ -  + ]:        298 :         if (ret)
     637                 :          0 :                 DPAA2_BUS_INFO("Unable to cleanup devices %d", ret);
     638                 :            : 
     639                 :        298 :         ret = fslmc_vfio_close_group();
     640         [ +  - ]:        298 :         if (ret)
     641                 :        298 :                 DPAA2_BUS_INFO("Unable to close devices %d", ret);
     642                 :            : 
     643         [ -  + ]:        298 :         while (!TAILQ_EMPTY(&fslmc_control_devices)) {
     644                 :            :                 struct rte_device *rte_dev = TAILQ_FIRST(&fslmc_control_devices);
     645                 :            :                 struct rte_dpaa2_device *dev = RTE_BUS_DEVICE(rte_dev, *dev);
     646                 :            : 
     647                 :          0 :                 fslmc_remove_control_device(dev);
     648                 :            :         }
     649                 :            : 
     650                 :        298 :         return 0;
     651                 :            : }
     652                 :            : 
     653                 :            : /*register a fslmc bus based dpaa2 driver */
     654                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_fslmc_driver_register)
     655                 :            : void
     656                 :        915 : rte_fslmc_driver_register(struct rte_dpaa2_driver *driver)
     657                 :            : {
     658         [ -  + ]:        915 :         RTE_VERIFY(driver);
     659         [ -  + ]:        915 :         RTE_VERIFY(driver->probe != NULL);
     660                 :            : 
     661                 :        915 :         rte_bus_add_driver(&rte_fslmc_bus, &driver->driver);
     662                 :        915 : }
     663                 :            : 
     664                 :            : /*un-register a fslmc bus based dpaa2 driver */
     665                 :            : RTE_EXPORT_INTERNAL_SYMBOL(rte_fslmc_driver_unregister)
     666                 :            : void
     667                 :          0 : rte_fslmc_driver_unregister(struct rte_dpaa2_driver *driver)
     668                 :            : {
     669                 :          0 :         rte_bus_remove_driver(&rte_fslmc_bus, &driver->driver);
     670                 :          0 : }
     671                 :            : 
     672                 :            : /*
     673                 :            :  * All device has iova as va
     674                 :            :  */
     675                 :            : static inline int
     676                 :            : fslmc_all_device_support_iova(void)
     677                 :            : {
     678                 :            :         struct rte_dpaa2_device *dev;
     679                 :            :         struct rte_dpaa2_driver *drv;
     680                 :            : 
     681         [ #  # ]:          0 :         RTE_BUS_FOREACH_DEV(dev, &rte_fslmc_bus) {
     682   [ #  #  #  # ]:          0 :                 RTE_BUS_FOREACH_DRV(drv, &rte_fslmc_bus) {
     683                 :          0 :                         if (!fslmc_bus_match(&drv->driver, &dev->device))
     684                 :          0 :                                 continue;
     685                 :            :                         /* if the driver is not supporting IOVA */
     686         [ #  # ]:          0 :                         if (!(drv->drv_flags & RTE_DPAA2_DRV_IOVA_AS_VA))
     687                 :            :                                 return 0;
     688                 :            :                 }
     689                 :            :         }
     690                 :            :         return 1;
     691                 :            : }
     692                 :            : 
     693                 :            : /*
     694                 :            :  * Get iommu class of DPAA2 devices on the bus.
     695                 :            :  */
     696                 :            : static enum rte_iova_mode
     697                 :        229 : rte_dpaa2_get_iommu_class(void)
     698                 :            : {
     699         [ +  - ]:        229 :         if (rte_eal_iova_mode() == RTE_IOVA_PA)
     700                 :            :                 return RTE_IOVA_PA;
     701                 :            : 
     702         [ -  + ]:        229 :         if (TAILQ_EMPTY(&rte_fslmc_bus.device_list))
     703                 :            :                 return RTE_IOVA_DC;
     704                 :            : 
     705                 :            :         /* check if all devices on the bus support Virtual addressing or not */
     706   [ #  #  #  # ]:          0 :         if (fslmc_all_device_support_iova() != 0 &&
     707                 :          0 :                         dev_vfio_get_iova_mode() == DEV_VFIO_IOVA_MODE_VA)
     708                 :          0 :                 return RTE_IOVA_VA;
     709                 :            : 
     710                 :            :         return RTE_IOVA_PA;
     711                 :            : }
     712                 :            : 
     713                 :            : static int
     714                 :          0 : fslmc_bus_probe_device(struct rte_driver *driver, struct rte_device *rte_dev)
     715                 :            : {
     716                 :            :         struct rte_dpaa2_device *dev = RTE_BUS_DEVICE(rte_dev, *dev);
     717                 :            :         struct rte_dpaa2_driver *drv = RTE_BUS_DRIVER(driver, *drv);
     718                 :            :         int ret = 0;
     719                 :            : 
     720                 :            :         /* Allocate interrupt instance */
     721                 :          0 :         dev->intr_handle =
     722                 :          0 :                 rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
     723         [ #  # ]:          0 :         if (dev->intr_handle == NULL) {
     724                 :          0 :                 DPAA2_BUS_ERR("Failed to allocate intr handle");
     725                 :          0 :                 return -ENOMEM;
     726                 :            :         }
     727                 :            : 
     728                 :          0 :         ret = fslmc_vfio_dev_setup(dev);
     729         [ #  # ]:          0 :         if (ret) {
     730                 :          0 :                 DPAA2_BUS_ERR("Dev (%s) VFIO setup failed", dev->device.name);
     731                 :          0 :                 goto release_intr;
     732                 :            :         }
     733                 :            : 
     734                 :          0 :         ret = drv->probe(drv, dev);
     735         [ #  # ]:          0 :         if (ret != 0) {
     736                 :          0 :                 DPAA2_BUS_ERR("Unable to probe");
     737                 :          0 :                 fslmc_vfio_dev_close(dev);
     738                 :          0 : release_intr:
     739                 :          0 :                 rte_intr_instance_free(dev->intr_handle);
     740                 :          0 :                 dev->intr_handle = NULL;
     741                 :            :         } else {
     742                 :          0 :                 DPAA2_BUS_INFO("%s Plugged",  dev->device.name);
     743                 :            :         }
     744                 :            : 
     745                 :            :         return ret;
     746                 :            : }
     747                 :            : 
     748                 :            : static int
     749                 :          0 : fslmc_bus_unplug_device(struct rte_device *rte_dev)
     750                 :            : {
     751                 :            :         struct rte_dpaa2_device *dev = RTE_BUS_DEVICE(rte_dev, *dev);
     752                 :          0 :         const struct rte_dpaa2_driver *drv = RTE_BUS_DRIVER(rte_dev->driver, *drv);
     753                 :            : 
     754         [ #  # ]:          0 :         if (drv->remove != NULL) {
     755                 :          0 :                 int ret = drv->remove(dev);
     756         [ #  # ]:          0 :                 if (ret != 0)
     757                 :            :                         return ret;
     758                 :            :         }
     759                 :            : 
     760                 :          0 :         fslmc_vfio_dev_close(dev);
     761                 :          0 :         rte_intr_instance_free(dev->intr_handle);
     762                 :          0 :         dev->intr_handle = NULL;
     763                 :          0 :         DPAA2_BUS_INFO("%s Un-Plugged",  dev->device.name);
     764                 :            : 
     765                 :          0 :         return 0;
     766                 :            : }
     767                 :            : 
     768                 :            : struct rte_bus rte_fslmc_bus = {
     769                 :            :         .scan = rte_fslmc_scan,
     770                 :            :         .probe = rte_fslmc_probe,
     771                 :            :         .free_device = fslmc_free_device,
     772                 :            :         .cleanup = fslmc_cleanup,
     773                 :            :         .parse = rte_fslmc_parse,
     774                 :            :         .dev_compare = fslmc_dev_compare,
     775                 :            :         .find_device = rte_bus_generic_find_device,
     776                 :            :         .get_iommu_class = rte_dpaa2_get_iommu_class,
     777                 :            :         .match = fslmc_bus_match,
     778                 :            :         .probe_device = fslmc_bus_probe_device,
     779                 :            :         .unplug_device = fslmc_bus_unplug_device,
     780                 :            :         .dev_iterate = rte_bus_generic_dev_iterate,
     781                 :            : };
     782                 :            : 
     783                 :        305 : RTE_REGISTER_BUS(fslmc, rte_fslmc_bus);
     784         [ -  + ]:        305 : RTE_LOG_REGISTER_DEFAULT(dpaa2_logtype_bus, NOTICE);

Generated by: LCOV version 1.14