LCOV - code coverage report
Current view: top level - drivers/net/netvsc - hn_vf.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 304 0.0 %
Date: 2025-04-03 19:37:06 Functions: 0 34 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 116 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright (c) 2018 Microsoft Corp.
       3                 :            :  * All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <stdio.h>
       7                 :            : #include <stdint.h>
       8                 :            : #include <string.h>
       9                 :            : #include <stdbool.h>
      10                 :            : #include <errno.h>
      11                 :            : #include <unistd.h>
      12                 :            : #include <dirent.h>
      13                 :            : #include <fcntl.h>
      14                 :            : #include <sys/types.h>
      15                 :            : #include <sys/uio.h>
      16                 :            : 
      17                 :            : #include <rte_ether.h>
      18                 :            : #include <rte_ethdev.h>
      19                 :            : #include <ethdev_driver.h>
      20                 :            : #include <rte_lcore.h>
      21                 :            : #include <rte_memory.h>
      22                 :            : #include <bus_vmbus_driver.h>
      23                 :            : #include <rte_pci.h>
      24                 :            : #include <bus_pci_driver.h>
      25                 :            : #include <rte_log.h>
      26                 :            : #include <rte_string_fns.h>
      27                 :            : #include <rte_alarm.h>
      28                 :            : 
      29                 :            : #include "hn_logs.h"
      30                 :            : #include "hn_var.h"
      31                 :            : #include "hn_nvs.h"
      32                 :            : 
      33                 :            : /* Search for VF with matching MAC address, return port id */
      34                 :          0 : static int hn_vf_match(const struct rte_eth_dev *dev)
      35                 :            : {
      36                 :          0 :         const struct rte_ether_addr *mac = dev->data->mac_addrs;
      37                 :            :         int i;
      38                 :            : 
      39         [ #  # ]:          0 :         RTE_ETH_FOREACH_DEV(i) {
      40                 :          0 :                 const struct rte_eth_dev *vf_dev = &rte_eth_devices[i];
      41                 :          0 :                 const struct rte_ether_addr *vf_mac = vf_dev->data->mac_addrs;
      42                 :            : 
      43         [ #  # ]:          0 :                 if (vf_dev == dev)
      44                 :          0 :                         continue;
      45                 :            : 
      46         [ #  # ]:          0 :                 if (rte_is_same_ether_addr(mac, vf_mac))
      47                 :          0 :                         return i;
      48                 :            :         }
      49                 :            :         return -ENOENT;
      50                 :            : }
      51                 :            : 
      52                 :            : 
      53                 :            : /*
      54                 :            :  * Attach new PCI VF device and return the port_id
      55                 :            :  */
      56                 :          0 : static int hn_vf_attach(struct rte_eth_dev *dev, struct hn_data *hv)
      57                 :            : {
      58                 :          0 :         struct rte_eth_dev_owner owner = { .id = RTE_ETH_DEV_NO_OWNER };
      59                 :            :         int port, ret;
      60                 :            : 
      61         [ #  # ]:          0 :         if (hv->vf_ctx.vf_attached) {
      62                 :          0 :                 PMD_DRV_LOG(ERR, "VF already attached");
      63                 :          0 :                 return 0;
      64                 :            :         }
      65                 :            : 
      66                 :          0 :         port = hn_vf_match(dev);
      67         [ #  # ]:          0 :         if (port < 0) {
      68                 :          0 :                 PMD_DRV_LOG(NOTICE, "Couldn't find port for VF");
      69                 :          0 :                 return port;
      70                 :            :         }
      71                 :            : 
      72                 :          0 :         PMD_DRV_LOG(NOTICE, "found matching VF port %d", port);
      73                 :          0 :         ret = rte_eth_dev_owner_get(port, &owner);
      74         [ #  # ]:          0 :         if (ret < 0) {
      75                 :          0 :                 PMD_DRV_LOG(ERR, "Can not find owner for port %d", port);
      76                 :          0 :                 return ret;
      77                 :            :         }
      78                 :            : 
      79         [ #  # ]:          0 :         if (owner.id != RTE_ETH_DEV_NO_OWNER) {
      80                 :          0 :                 PMD_DRV_LOG(ERR, "Port %u already owned by other device %s",
      81                 :            :                             port, owner.name);
      82                 :          0 :                 return -EBUSY;
      83                 :            :         }
      84                 :            : 
      85                 :          0 :         ret = rte_eth_dev_owner_set(port, &hv->owner);
      86         [ #  # ]:          0 :         if (ret < 0) {
      87                 :          0 :                 PMD_DRV_LOG(ERR, "Can set owner for port %d", port);
      88                 :          0 :                 return ret;
      89                 :            :         }
      90                 :            : 
      91                 :          0 :         PMD_DRV_LOG(DEBUG, "Attach VF device %u", port);
      92                 :          0 :         hv->vf_ctx.vf_attached = true;
      93                 :          0 :         hv->vf_ctx.vf_port = port;
      94                 :          0 :         return 0;
      95                 :            : }
      96                 :            : 
      97                 :            : static void hn_vf_remove(struct hn_data *hv);
      98                 :            : 
      99                 :          0 : static void hn_remove_delayed(void *args)
     100                 :            : {
     101                 :            :         struct hn_data *hv = args;
     102                 :          0 :         uint16_t port_id = hv->vf_ctx.vf_port;
     103                 :          0 :         struct rte_device *dev = rte_eth_devices[port_id].device;
     104                 :            :         int ret;
     105                 :            :         bool all_eth_removed;
     106                 :            : 
     107                 :            :         /* Tell VSP to switch data path to synthetic */
     108                 :          0 :         hn_vf_remove(hv);
     109                 :            : 
     110                 :          0 :         PMD_DRV_LOG(NOTICE, "Start to remove port %d", port_id);
     111                 :          0 :         rte_rwlock_write_lock(&hv->vf_lock);
     112                 :            : 
     113                 :            :         /* Give back ownership */
     114                 :          0 :         ret = rte_eth_dev_owner_unset(port_id, hv->owner.id);
     115         [ #  # ]:          0 :         if (ret)
     116                 :          0 :                 PMD_DRV_LOG(ERR, "rte_eth_dev_owner_unset failed ret=%d",
     117                 :            :                             ret);
     118                 :          0 :         hv->vf_ctx.vf_attached = false;
     119                 :            : 
     120                 :          0 :         ret = rte_eth_dev_callback_unregister(port_id, RTE_ETH_EVENT_INTR_RMV,
     121                 :            :                                               hn_eth_rmv_event_callback, hv);
     122         [ #  # ]:          0 :         if (ret)
     123                 :          0 :                 PMD_DRV_LOG(ERR,
     124                 :            :                             "rte_eth_dev_callback_unregister failed ret=%d",
     125                 :            :                             ret);
     126                 :            : 
     127                 :            :         /* Detach and release port_id from system */
     128                 :          0 :         ret = rte_eth_dev_stop(port_id);
     129         [ #  # ]:          0 :         if (ret)
     130                 :          0 :                 PMD_DRV_LOG(ERR, "rte_eth_dev_stop failed port_id=%u ret=%d",
     131                 :            :                             port_id, ret);
     132                 :            : 
     133                 :            :         /* Record the device parameters for possible hotplug events */
     134   [ #  #  #  # ]:          0 :         if (dev->devargs && dev->devargs->args)
     135                 :          0 :                 hv->vf_devargs = strdup(dev->devargs->args);
     136                 :            : 
     137                 :          0 :         ret = rte_eth_dev_close(port_id);
     138         [ #  # ]:          0 :         if (ret)
     139                 :          0 :                 PMD_DRV_LOG(ERR, "rte_eth_dev_close failed port_id=%u ret=%d",
     140                 :            :                             port_id, ret);
     141                 :            : 
     142                 :            :         /* Remove the rte device when all its eth devices are removed */
     143                 :            :         all_eth_removed = true;
     144         [ #  # ]:          0 :         RTE_ETH_FOREACH_DEV_OF(port_id, dev) {
     145         [ #  # ]:          0 :                 if (rte_eth_devices[port_id].state != RTE_ETH_DEV_UNUSED) {
     146                 :            :                         all_eth_removed = false;
     147                 :            :                         break;
     148                 :            :                 }
     149                 :            :         }
     150         [ #  # ]:          0 :         if (all_eth_removed)
     151                 :          0 :                 ret = rte_dev_remove(dev);
     152                 :            : 
     153                 :          0 :         hv->vf_ctx.vf_state = vf_removed;
     154                 :            : 
     155                 :            :         rte_rwlock_write_unlock(&hv->vf_lock);
     156                 :          0 : }
     157                 :            : 
     158                 :          0 : int hn_eth_rmv_event_callback(uint16_t port_id,
     159                 :            :                               enum rte_eth_event_type event __rte_unused,
     160                 :            :                               void *cb_arg, void *out __rte_unused)
     161                 :            : {
     162                 :            :         struct hn_data *hv = cb_arg;
     163                 :            : 
     164                 :          0 :         PMD_DRV_LOG(NOTICE, "Removing VF portid %d", port_id);
     165                 :          0 :         rte_eal_alarm_set(1, hn_remove_delayed, hv);
     166                 :            : 
     167                 :          0 :         return 0;
     168                 :            : }
     169                 :            : 
     170                 :          0 : static int hn_setup_vf_queues(int port, struct rte_eth_dev *dev)
     171                 :            : {
     172                 :            :         struct hn_rx_queue *rx_queue;
     173                 :            :         struct rte_eth_txq_info txinfo;
     174                 :            :         struct rte_eth_rxq_info rxinfo;
     175                 :            :         int i, ret = 0;
     176                 :            : 
     177         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     178                 :          0 :                 ret = rte_eth_tx_queue_info_get(dev->data->port_id, i, &txinfo);
     179         [ #  # ]:          0 :                 if (ret) {
     180                 :          0 :                         PMD_DRV_LOG(ERR,
     181                 :            :                                     "rte_eth_tx_queue_info_get failed ret=%d",
     182                 :            :                                     ret);
     183                 :          0 :                         return ret;
     184                 :            :                 }
     185                 :            : 
     186                 :          0 :                 ret = rte_eth_tx_queue_setup(port, i, txinfo.nb_desc, 0,
     187                 :            :                                              &txinfo.conf);
     188         [ #  # ]:          0 :                 if (ret) {
     189                 :          0 :                         PMD_DRV_LOG(ERR,
     190                 :            :                                     "rte_eth_tx_queue_setup failed ret=%d",
     191                 :            :                                     ret);
     192                 :          0 :                         return ret;
     193                 :            :                 }
     194                 :            :         }
     195                 :            : 
     196         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
     197                 :          0 :                 ret = rte_eth_rx_queue_info_get(dev->data->port_id, i, &rxinfo);
     198         [ #  # ]:          0 :                 if (ret) {
     199                 :          0 :                         PMD_DRV_LOG(ERR,
     200                 :            :                                     "rte_eth_rx_queue_info_get failed ret=%d",
     201                 :            :                                     ret);
     202                 :          0 :                         return ret;
     203                 :            :                 }
     204                 :            : 
     205                 :          0 :                 rx_queue = dev->data->rx_queues[i];
     206                 :            : 
     207                 :          0 :                 ret = rte_eth_rx_queue_setup(port, i, rxinfo.nb_desc, 0,
     208                 :            :                                              &rxinfo.conf, rx_queue->mb_pool);
     209         [ #  # ]:          0 :                 if (ret) {
     210                 :          0 :                         PMD_DRV_LOG(ERR,
     211                 :            :                                     "rte_eth_rx_queue_setup failed ret=%d",
     212                 :            :                                     ret);
     213                 :          0 :                         return ret;
     214                 :            :                 }
     215                 :            :         }
     216                 :            : 
     217                 :            :         return ret;
     218                 :            : }
     219                 :            : 
     220                 :            : int hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv);
     221                 :            : 
     222                 :          0 : static void hn_vf_add_retry(void *args)
     223                 :            : {
     224                 :            :         struct rte_eth_dev *dev = args;
     225                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     226                 :            : 
     227                 :          0 :         hn_vf_add(dev, hv);
     228                 :          0 : }
     229                 :            : 
     230                 :            : int hn_vf_configure(struct rte_eth_dev *dev,
     231                 :            :                     const struct rte_eth_conf *dev_conf);
     232                 :            : 
     233                 :            : /* Add new VF device to synthetic device */
     234                 :          0 : int hn_vf_add(struct rte_eth_dev *dev, struct hn_data *hv)
     235                 :            : {
     236                 :            :         int ret, port;
     237                 :            : 
     238   [ #  #  #  # ]:          0 :         if (!hv->vf_ctx.vf_vsp_reported || hv->vf_ctx.vf_vsc_switched)
     239                 :            :                 return 0;
     240                 :            : 
     241                 :          0 :         rte_rwlock_write_lock(&hv->vf_lock);
     242                 :            : 
     243                 :          0 :         ret = hn_vf_attach(dev, hv);
     244         [ #  # ]:          0 :         if (ret) {
     245                 :          0 :                 PMD_DRV_LOG(NOTICE,
     246                 :            :                             "RNDIS reports VF but device not found, retrying");
     247                 :          0 :                 rte_eal_alarm_set(1000000, hn_vf_add_retry, dev);
     248                 :          0 :                 goto exit;
     249                 :            :         }
     250                 :            : 
     251                 :          0 :         port = hv->vf_ctx.vf_port;
     252                 :            : 
     253                 :            :         /* If the primary device has started, this is a VF hot add.
     254                 :            :          * Configure and start VF device.
     255                 :            :          */
     256         [ #  # ]:          0 :         if (dev->data->dev_started) {
     257         [ #  # ]:          0 :                 if (rte_eth_devices[port].data->dev_started) {
     258                 :          0 :                         PMD_DRV_LOG(ERR, "VF already started on hot add");
     259                 :          0 :                         goto exit;
     260                 :            :                 }
     261                 :            : 
     262                 :          0 :                 PMD_DRV_LOG(NOTICE, "configuring VF port %d", port);
     263                 :          0 :                 ret = hn_vf_configure(dev, &dev->data->dev_conf);
     264         [ #  # ]:          0 :                 if (ret) {
     265                 :          0 :                         PMD_DRV_LOG(ERR, "Failed to configure VF port %d",
     266                 :            :                                     port);
     267                 :          0 :                         goto exit;
     268                 :            :                 }
     269                 :            : 
     270                 :          0 :                 ret = hn_setup_vf_queues(port, dev);
     271         [ #  # ]:          0 :                 if (ret) {
     272                 :          0 :                         PMD_DRV_LOG(ERR,
     273                 :            :                                     "Failed to configure VF queues port %d",
     274                 :            :                                     port);
     275                 :          0 :                         goto exit;
     276                 :            :                 }
     277                 :            : 
     278                 :          0 :                 ret = rte_eth_dev_set_mtu(port, dev->data->mtu);
     279         [ #  # ]:          0 :                 if (ret) {
     280                 :          0 :                         PMD_DRV_LOG(ERR, "Failed to set VF MTU");
     281                 :          0 :                         goto exit;
     282                 :            :                 }
     283                 :            : 
     284                 :          0 :                 PMD_DRV_LOG(NOTICE, "Starting VF port %d", port);
     285                 :          0 :                 ret = rte_eth_dev_start(port);
     286         [ #  # ]:          0 :                 if (ret) {
     287                 :          0 :                         PMD_DRV_LOG(ERR, "rte_eth_dev_start failed ret=%d",
     288                 :            :                                     ret);
     289                 :          0 :                         goto exit;
     290                 :            :                 }
     291                 :          0 :                 hv->vf_ctx.vf_state = vf_started;
     292                 :            :         }
     293                 :            : 
     294                 :          0 :         ret = hn_nvs_set_datapath(hv, NVS_DATAPATH_VF);
     295         [ #  # ]:          0 :         if (ret == 0)
     296                 :          0 :                 hv->vf_ctx.vf_vsc_switched = true;
     297                 :            : 
     298                 :          0 : exit:
     299                 :            :         rte_rwlock_write_unlock(&hv->vf_lock);
     300                 :          0 :         return ret;
     301                 :            : }
     302                 :            : 
     303                 :            : /* Switch data path to VF device */
     304                 :          0 : static void hn_vf_remove(struct hn_data *hv)
     305                 :            : {
     306                 :            :         int ret;
     307                 :            : 
     308         [ #  # ]:          0 :         if (!hv->vf_ctx.vf_vsc_switched) {
     309                 :          0 :                 PMD_DRV_LOG(ERR, "VF path not active");
     310                 :          0 :                 return;
     311                 :            :         }
     312                 :            : 
     313                 :          0 :         rte_rwlock_write_lock(&hv->vf_lock);
     314         [ #  # ]:          0 :         if (!hv->vf_ctx.vf_vsc_switched) {
     315                 :          0 :                 PMD_DRV_LOG(ERR, "VF path not active");
     316                 :            :         } else {
     317                 :            :                 /* Stop incoming packets from arriving on VF */
     318                 :          0 :                 ret = hn_nvs_set_datapath(hv, NVS_DATAPATH_SYNTHETIC);
     319         [ #  # ]:          0 :                 if (ret)
     320                 :          0 :                         PMD_DRV_LOG(ERR, "Failed to switch to synthetic");
     321                 :          0 :                 hv->vf_ctx.vf_vsc_switched = false;
     322                 :            :         }
     323                 :            :         rte_rwlock_write_unlock(&hv->vf_lock);
     324                 :            : }
     325                 :            : 
     326                 :            : /* Handle VF association message from host */
     327                 :            : void
     328                 :          0 : hn_nvs_handle_vfassoc(struct rte_eth_dev *dev,
     329                 :            :                       const struct vmbus_chanpkt_hdr *hdr,
     330                 :            :                       const void *data)
     331                 :            : {
     332         [ #  # ]:          0 :         struct hn_data *hv = dev->data->dev_private;
     333                 :            :         const struct hn_nvs_vf_association *vf_assoc = data;
     334                 :            : 
     335         [ #  # ]:          0 :         if (unlikely(vmbus_chanpkt_datalen(hdr) < sizeof(*vf_assoc))) {
     336                 :          0 :                 PMD_DRV_LOG(ERR, "invalid vf association NVS");
     337                 :          0 :                 return;
     338                 :            :         }
     339                 :            : 
     340         [ #  # ]:          0 :         PMD_DRV_LOG(DEBUG, "VF serial %u %s port %u",
     341                 :            :                     vf_assoc->serial,
     342                 :            :                     vf_assoc->allocated ? "add to" : "remove from",
     343                 :            :                     dev->data->port_id);
     344                 :            : 
     345                 :          0 :         hv->vf_ctx.vf_vsp_reported = vf_assoc->allocated;
     346                 :            : 
     347         [ #  # ]:          0 :         if (dev->state == RTE_ETH_DEV_ATTACHED) {
     348         [ #  # ]:          0 :                 if (vf_assoc->allocated)
     349                 :          0 :                         hn_vf_add(dev, hv);
     350                 :            :                 else
     351                 :          0 :                         hn_vf_remove(hv);
     352                 :            :         }
     353                 :            : }
     354                 :            : 
     355                 :            : static void
     356                 :            : hn_vf_merge_desc_lim(struct rte_eth_desc_lim *lim,
     357                 :            :                      const struct rte_eth_desc_lim *vf_lim)
     358                 :            : {
     359                 :          0 :         lim->nb_max = RTE_MIN(vf_lim->nb_max, lim->nb_max);
     360                 :          0 :         lim->nb_min = RTE_MAX(vf_lim->nb_min, lim->nb_min);
     361                 :          0 :         lim->nb_align = RTE_MAX(vf_lim->nb_align, lim->nb_align);
     362                 :          0 :         lim->nb_seg_max = RTE_MIN(vf_lim->nb_seg_max, lim->nb_seg_max);
     363                 :          0 :         lim->nb_mtu_seg_max = RTE_MIN(vf_lim->nb_seg_max, lim->nb_seg_max);
     364                 :            : }
     365                 :            : 
     366                 :            : /*
     367                 :            :  * Merge the info from the VF and synthetic path.
     368                 :            :  * use the default config of the VF
     369                 :            :  * and the minimum number of queues and buffer sizes.
     370                 :            :  */
     371                 :          0 : static int hn_vf_info_merge(struct rte_eth_dev *vf_dev,
     372                 :            :                              struct rte_eth_dev_info *info)
     373                 :            : {
     374                 :            :         struct rte_eth_dev_info vf_info;
     375                 :            :         int ret;
     376                 :            : 
     377                 :          0 :         ret = rte_eth_dev_info_get(vf_dev->data->port_id, &vf_info);
     378         [ #  # ]:          0 :         if (ret != 0)
     379                 :            :                 return ret;
     380                 :            : 
     381                 :          0 :         info->speed_capa = vf_info.speed_capa;
     382                 :          0 :         info->default_rxportconf = vf_info.default_rxportconf;
     383                 :          0 :         info->default_txportconf = vf_info.default_txportconf;
     384                 :            : 
     385                 :          0 :         info->max_rx_queues = RTE_MIN(vf_info.max_rx_queues,
     386                 :            :                                       info->max_rx_queues);
     387                 :          0 :         info->rx_offload_capa &= vf_info.rx_offload_capa;
     388                 :          0 :         info->rx_queue_offload_capa &= vf_info.rx_queue_offload_capa;
     389                 :          0 :         info->flow_type_rss_offloads &= vf_info.flow_type_rss_offloads;
     390                 :            : 
     391                 :          0 :         info->max_tx_queues = RTE_MIN(vf_info.max_tx_queues,
     392                 :            :                                       info->max_tx_queues);
     393                 :          0 :         info->tx_offload_capa &= vf_info.tx_offload_capa;
     394                 :          0 :         info->tx_queue_offload_capa &= vf_info.tx_queue_offload_capa;
     395                 :            :         hn_vf_merge_desc_lim(&info->tx_desc_lim, &vf_info.tx_desc_lim);
     396                 :            : 
     397                 :          0 :         info->min_rx_bufsize = RTE_MAX(vf_info.min_rx_bufsize,
     398                 :            :                                        info->min_rx_bufsize);
     399                 :          0 :         info->max_rx_pktlen  = RTE_MAX(vf_info.max_rx_pktlen,
     400                 :            :                                        info->max_rx_pktlen);
     401                 :            :         hn_vf_merge_desc_lim(&info->rx_desc_lim, &vf_info.rx_desc_lim);
     402                 :            : 
     403                 :          0 :         return 0;
     404                 :            : }
     405                 :            : 
     406                 :          0 : int hn_vf_info_get(struct hn_data *hv, struct rte_eth_dev_info *info)
     407                 :            : {
     408                 :            :         struct rte_eth_dev *vf_dev;
     409                 :            :         int ret = 0;
     410                 :            : 
     411                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     412                 :            :         vf_dev = hn_get_vf_dev(hv);
     413                 :            :         if (vf_dev)
     414                 :          0 :                 ret = hn_vf_info_merge(vf_dev, info);
     415                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     416                 :          0 :         return ret;
     417                 :            : }
     418                 :            : 
     419                 :          0 : int hn_vf_configure(struct rte_eth_dev *dev,
     420                 :            :                     const struct rte_eth_conf *dev_conf)
     421                 :            : {
     422                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     423                 :          0 :         struct rte_eth_conf vf_conf = *dev_conf;
     424                 :            :         int ret = 0;
     425                 :            : 
     426                 :            :         /* link state interrupt does not matter here. */
     427                 :          0 :         vf_conf.intr_conf.lsc = 0;
     428                 :            : 
     429                 :            :         /* need to monitor removal event */
     430                 :          0 :         vf_conf.intr_conf.rmv = 1;
     431                 :            : 
     432         [ #  # ]:          0 :         if (hv->vf_ctx.vf_attached) {
     433                 :          0 :                 ret = rte_eth_dev_callback_register(hv->vf_ctx.vf_port,
     434                 :            :                                                     RTE_ETH_EVENT_INTR_RMV,
     435                 :            :                                                     hn_eth_rmv_event_callback,
     436                 :            :                                                     hv);
     437         [ #  # ]:          0 :                 if (ret) {
     438                 :          0 :                         PMD_DRV_LOG(ERR,
     439                 :            :                                     "Registering callback failed for vf port %d ret %d",
     440                 :            :                                     hv->vf_ctx.vf_port, ret);
     441                 :          0 :                         return ret;
     442                 :            :                 }
     443                 :            : 
     444                 :          0 :                 ret = rte_eth_dev_configure(hv->vf_ctx.vf_port,
     445                 :          0 :                                             dev->data->nb_rx_queues,
     446                 :          0 :                                             dev->data->nb_tx_queues,
     447                 :            :                                             &vf_conf);
     448         [ #  # ]:          0 :                 if (ret) {
     449                 :          0 :                         PMD_DRV_LOG(ERR, "VF configuration failed: %d", ret);
     450                 :            : 
     451                 :          0 :                         rte_eth_dev_callback_unregister(hv->vf_ctx.vf_port,
     452                 :            :                                                         RTE_ETH_EVENT_INTR_RMV,
     453                 :            :                                                         hn_eth_rmv_event_callback,
     454                 :            :                                                         hv);
     455                 :            : 
     456                 :          0 :                         return ret;
     457                 :            :                 }
     458                 :            : 
     459                 :          0 :                 hv->vf_ctx.vf_state = vf_configured;
     460                 :            :         }
     461                 :            : 
     462                 :            :         return ret;
     463                 :            : }
     464                 :            : 
     465                 :            : /* Configure VF if present.
     466                 :            :  * VF device will have the same number of queues as the synthetic device
     467                 :            :  */
     468                 :          0 : int hn_vf_configure_locked(struct rte_eth_dev *dev,
     469                 :            :                            const struct rte_eth_conf *dev_conf)
     470                 :            : {
     471                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     472                 :            :         int ret = 0;
     473                 :            : 
     474                 :          0 :         rte_rwlock_write_lock(&hv->vf_lock);
     475                 :          0 :         ret = hn_vf_configure(dev, dev_conf);
     476                 :            :         rte_rwlock_write_unlock(&hv->vf_lock);
     477                 :            : 
     478                 :          0 :         return ret;
     479                 :            : }
     480                 :            : 
     481                 :          0 : const uint32_t *hn_vf_supported_ptypes(struct rte_eth_dev *dev,
     482                 :            :                                        size_t *no_of_elements)
     483                 :            : {
     484                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     485                 :            :         struct rte_eth_dev *vf_dev;
     486                 :            :         const uint32_t *ptypes = NULL;
     487                 :            : 
     488                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     489                 :            :         vf_dev = hn_get_vf_dev(hv);
     490         [ #  # ]:          0 :         if (vf_dev && vf_dev->dev_ops->dev_supported_ptypes_get)
     491                 :          0 :                 ptypes = (*vf_dev->dev_ops->dev_supported_ptypes_get)(vf_dev,
     492                 :            :                                                               no_of_elements);
     493                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     494                 :            : 
     495                 :          0 :         return ptypes;
     496                 :            : }
     497                 :            : 
     498                 :          0 : int hn_vf_start(struct rte_eth_dev *dev)
     499                 :            : {
     500                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     501                 :            :         struct rte_eth_dev *vf_dev;
     502                 :            :         int ret = 0;
     503                 :            : 
     504                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     505                 :            :         vf_dev = hn_get_vf_dev(hv);
     506                 :            :         if (vf_dev)
     507                 :          0 :                 ret = rte_eth_dev_start(vf_dev->data->port_id);
     508                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     509                 :          0 :         return ret;
     510                 :            : }
     511                 :            : 
     512                 :          0 : int hn_vf_stop(struct rte_eth_dev *dev)
     513                 :            : {
     514                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     515                 :            :         struct rte_eth_dev *vf_dev;
     516                 :            :         int ret = 0;
     517                 :            : 
     518                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     519                 :            :         vf_dev = hn_get_vf_dev(hv);
     520                 :            :         if (vf_dev) {
     521                 :          0 :                 ret = rte_eth_dev_stop(vf_dev->data->port_id);
     522         [ #  # ]:          0 :                 if (ret != 0)
     523                 :          0 :                         PMD_DRV_LOG(ERR, "Failed to stop device on port %u",
     524                 :            :                                     vf_dev->data->port_id);
     525                 :            :         }
     526                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     527                 :            : 
     528                 :          0 :         return ret;
     529                 :            : }
     530                 :            : 
     531                 :            : /* If VF is present, then cascade configuration down */
     532                 :            : #define VF_ETHDEV_FUNC(dev, func)                               \
     533                 :            :         {                                                       \
     534                 :            :                 struct hn_data *hv = (dev)->data->dev_private;    \
     535                 :            :                 struct rte_eth_dev *vf_dev;                     \
     536                 :            :                 rte_rwlock_read_lock(&hv->vf_lock);              \
     537                 :            :                 vf_dev = hn_get_vf_dev(hv);                     \
     538                 :            :                 if (vf_dev)                                     \
     539                 :            :                         func(vf_dev->data->port_id);              \
     540                 :            :                 rte_rwlock_read_unlock(&hv->vf_lock);            \
     541                 :            :         }
     542                 :            : 
     543                 :            : /* If VF is present, then cascade configuration down */
     544                 :            : #define VF_ETHDEV_FUNC_RET_STATUS(dev, func)                    \
     545                 :            :         {                                                       \
     546                 :            :                 struct hn_data *hv = (dev)->data->dev_private;    \
     547                 :            :                 struct rte_eth_dev *vf_dev;                     \
     548                 :            :                 int ret = 0;                                    \
     549                 :            :                 rte_rwlock_read_lock(&hv->vf_lock);              \
     550                 :            :                 vf_dev = hn_get_vf_dev(hv);                     \
     551                 :            :                 if (vf_dev)                                     \
     552                 :            :                         ret = func(vf_dev->data->port_id);        \
     553                 :            :                 rte_rwlock_read_unlock(&hv->vf_lock);            \
     554                 :            :                 return ret;                                     \
     555                 :            :         }
     556                 :            : 
     557                 :          0 : int hn_vf_close(struct rte_eth_dev *dev)
     558                 :            : {
     559                 :            :         int ret = 0;
     560                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     561                 :            : 
     562                 :          0 :         rte_eal_alarm_cancel(hn_vf_add_retry, dev);
     563                 :            : 
     564                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     565         [ #  # ]:          0 :         if (hv->vf_ctx.vf_attached) {
     566                 :          0 :                 rte_eth_dev_callback_unregister(hv->vf_ctx.vf_port,
     567                 :            :                                                 RTE_ETH_EVENT_INTR_RMV,
     568                 :            :                                                 hn_eth_rmv_event_callback,
     569                 :            :                                                 hv);
     570                 :          0 :                 rte_eal_alarm_cancel(hn_remove_delayed, hv);
     571                 :          0 :                 ret = rte_eth_dev_close(hv->vf_ctx.vf_port);
     572                 :          0 :                 hv->vf_ctx.vf_attached = false;
     573                 :            :         }
     574                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     575                 :            : 
     576                 :          0 :         return ret;
     577                 :            : }
     578                 :            : 
     579                 :          0 : int hn_vf_stats_reset(struct rte_eth_dev *dev)
     580                 :            : {
     581                 :          0 :         VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_stats_reset);
     582                 :            : }
     583                 :            : 
     584                 :          0 : int hn_vf_allmulticast_enable(struct rte_eth_dev *dev)
     585                 :            : {
     586                 :          0 :         VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_allmulticast_enable);
     587                 :            : }
     588                 :            : 
     589                 :          0 : int hn_vf_allmulticast_disable(struct rte_eth_dev *dev)
     590                 :            : {
     591                 :          0 :         VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_allmulticast_disable);
     592                 :            : }
     593                 :            : 
     594                 :          0 : int hn_vf_promiscuous_enable(struct rte_eth_dev *dev)
     595                 :            : {
     596                 :          0 :         VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_enable);
     597                 :            : }
     598                 :            : 
     599                 :          0 : int hn_vf_promiscuous_disable(struct rte_eth_dev *dev)
     600                 :            : {
     601                 :          0 :         VF_ETHDEV_FUNC_RET_STATUS(dev, rte_eth_promiscuous_disable);
     602                 :            : }
     603                 :            : 
     604                 :          0 : int hn_vf_mc_addr_list(struct rte_eth_dev *dev,
     605                 :            :                         struct rte_ether_addr *mc_addr_set,
     606                 :            :                         uint32_t nb_mc_addr)
     607                 :            : {
     608                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     609                 :            :         struct rte_eth_dev *vf_dev;
     610                 :            :         int ret = 0;
     611                 :            : 
     612                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     613                 :            :         vf_dev = hn_get_vf_dev(hv);
     614                 :            :         if (vf_dev)
     615                 :          0 :                 ret = rte_eth_dev_set_mc_addr_list(vf_dev->data->port_id,
     616                 :            :                                                    mc_addr_set, nb_mc_addr);
     617                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     618                 :          0 :         return ret;
     619                 :            : }
     620                 :            : 
     621                 :          0 : int hn_vf_tx_queue_setup(struct rte_eth_dev *dev,
     622                 :            :                          uint16_t queue_idx, uint16_t nb_desc,
     623                 :            :                          unsigned int socket_id,
     624                 :            :                          const struct rte_eth_txconf *tx_conf)
     625                 :            : {
     626                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     627                 :            :         struct rte_eth_dev *vf_dev;
     628                 :            :         int ret = 0;
     629                 :            : 
     630                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     631                 :            :         vf_dev = hn_get_vf_dev(hv);
     632                 :            :         if (vf_dev)
     633                 :          0 :                 ret = rte_eth_tx_queue_setup(vf_dev->data->port_id,
     634                 :            :                                              queue_idx, nb_desc,
     635                 :            :                                              socket_id, tx_conf);
     636                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     637                 :          0 :         return ret;
     638                 :            : }
     639                 :            : 
     640                 :          0 : void hn_vf_tx_queue_release(struct hn_data *hv, uint16_t queue_id)
     641                 :            : {
     642                 :            :         struct rte_eth_dev *vf_dev;
     643                 :            : 
     644                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     645                 :            :         vf_dev = hn_get_vf_dev(hv);
     646         [ #  # ]:          0 :         if (vf_dev && vf_dev->dev_ops->tx_queue_release)
     647                 :          0 :                 (*vf_dev->dev_ops->tx_queue_release)(vf_dev, queue_id);
     648                 :            : 
     649                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     650                 :          0 : }
     651                 :            : 
     652                 :          0 : int hn_vf_rx_queue_setup(struct rte_eth_dev *dev,
     653                 :            :                          uint16_t queue_idx, uint16_t nb_desc,
     654                 :            :                          unsigned int socket_id,
     655                 :            :                          const struct rte_eth_rxconf *rx_conf,
     656                 :            :                          struct rte_mempool *mp)
     657                 :            : {
     658                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     659                 :            :         struct rte_eth_dev *vf_dev;
     660                 :            :         int ret = 0;
     661                 :            : 
     662                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     663                 :            :         vf_dev = hn_get_vf_dev(hv);
     664                 :            :         if (vf_dev)
     665                 :          0 :                 ret = rte_eth_rx_queue_setup(vf_dev->data->port_id,
     666                 :            :                                              queue_idx, nb_desc,
     667                 :            :                                              socket_id, rx_conf, mp);
     668                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     669                 :          0 :         return ret;
     670                 :            : }
     671                 :            : 
     672                 :          0 : void hn_vf_rx_queue_release(struct hn_data *hv, uint16_t queue_id)
     673                 :            : {
     674                 :            :         struct rte_eth_dev *vf_dev;
     675                 :            : 
     676                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     677                 :            :         vf_dev = hn_get_vf_dev(hv);
     678         [ #  # ]:          0 :         if (vf_dev && vf_dev->dev_ops->rx_queue_release)
     679                 :          0 :                 (*vf_dev->dev_ops->rx_queue_release)(vf_dev, queue_id);
     680                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     681                 :          0 : }
     682                 :            : 
     683                 :          0 : int hn_vf_stats_get(struct rte_eth_dev *dev,
     684                 :            :                     struct rte_eth_stats *stats)
     685                 :            : {
     686                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     687                 :            :         struct rte_eth_dev *vf_dev;
     688                 :            :         int ret = 0;
     689                 :            : 
     690                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     691                 :            :         vf_dev = hn_get_vf_dev(hv);
     692                 :            :         if (vf_dev)
     693                 :          0 :                 ret = rte_eth_stats_get(vf_dev->data->port_id, stats);
     694                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     695                 :          0 :         return ret;
     696                 :            : }
     697                 :            : 
     698                 :          0 : int hn_vf_xstats_get_names(struct rte_eth_dev *dev,
     699                 :            :                            struct rte_eth_xstat_name *names,
     700                 :            :                            unsigned int n)
     701                 :            : {
     702                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     703                 :            :         struct rte_eth_dev *vf_dev;
     704                 :            :         int i, count = 0;
     705                 :            : 
     706                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     707                 :            :         vf_dev = hn_get_vf_dev(hv);
     708                 :            :         if (vf_dev)
     709                 :          0 :                 count = rte_eth_xstats_get_names(vf_dev->data->port_id,
     710                 :            :                                                  names, n);
     711                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     712                 :            : 
     713                 :            :         /* add vf_ prefix to xstat names */
     714         [ #  # ]:          0 :         if (names) {
     715         [ #  # ]:          0 :                 for (i = 0; i < count; i++) {
     716                 :            :                         char tmp[RTE_ETH_XSTATS_NAME_SIZE];
     717                 :            : 
     718                 :          0 :                         snprintf(tmp, sizeof(tmp), "vf_%s", names[i].name);
     719                 :            :                         strlcpy(names[i].name, tmp, sizeof(names[i].name));
     720                 :            :                 }
     721                 :            :         }
     722                 :            : 
     723                 :          0 :         return count;
     724                 :            : }
     725                 :            : 
     726                 :          0 : int hn_vf_xstats_get(struct rte_eth_dev *dev,
     727                 :            :                      struct rte_eth_xstat *xstats,
     728                 :            :                      unsigned int offset,
     729                 :            :                      unsigned int n)
     730                 :            : {
     731                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     732                 :            :         struct rte_eth_dev *vf_dev;
     733                 :            :         int i, count = 0;
     734                 :            : 
     735                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     736                 :            :         vf_dev = hn_get_vf_dev(hv);
     737                 :            :         if (vf_dev)
     738                 :          0 :                 count = rte_eth_xstats_get(vf_dev->data->port_id,
     739                 :          0 :                                            xstats + offset, n - offset);
     740                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     741                 :            : 
     742                 :            :         /* Offset id's for VF stats */
     743         [ #  # ]:          0 :         if (count > 0) {
     744         [ #  # ]:          0 :                 for (i = 0; i < count; i++)
     745                 :          0 :                         xstats[i + offset].id += offset;
     746                 :            :         }
     747                 :            : 
     748                 :          0 :         return count;
     749                 :            : }
     750                 :            : 
     751                 :          0 : int hn_vf_xstats_reset(struct rte_eth_dev *dev)
     752                 :            : {
     753                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     754                 :            :         struct rte_eth_dev *vf_dev;
     755                 :            :         int ret;
     756                 :            : 
     757                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     758                 :            :         vf_dev = hn_get_vf_dev(hv);
     759                 :            :         if (vf_dev)
     760                 :          0 :                 ret = rte_eth_xstats_reset(vf_dev->data->port_id);
     761                 :            :         else
     762                 :            :                 ret = -EINVAL;
     763                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     764                 :            : 
     765                 :          0 :         return ret;
     766                 :            : }
     767                 :            : 
     768                 :          0 : int hn_vf_rss_hash_update(struct rte_eth_dev *dev,
     769                 :            :                           struct rte_eth_rss_conf *rss_conf)
     770                 :            : {
     771                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     772                 :            :         struct rte_eth_dev *vf_dev;
     773                 :            :         int ret = 0;
     774                 :            : 
     775                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     776                 :            :         vf_dev = hn_get_vf_dev(hv);
     777         [ #  # ]:          0 :         if (vf_dev && vf_dev->dev_ops->rss_hash_update)
     778                 :          0 :                 ret = vf_dev->dev_ops->rss_hash_update(vf_dev, rss_conf);
     779                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     780                 :            : 
     781                 :          0 :         return ret;
     782                 :            : }
     783                 :            : 
     784                 :          0 : int hn_vf_reta_hash_update(struct rte_eth_dev *dev,
     785                 :            :                            struct rte_eth_rss_reta_entry64 *reta_conf,
     786                 :            :                            uint16_t reta_size)
     787                 :            : {
     788                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     789                 :            :         struct rte_eth_dev *vf_dev;
     790                 :            :         int ret = 0;
     791                 :            : 
     792                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     793                 :            :         vf_dev = hn_get_vf_dev(hv);
     794         [ #  # ]:          0 :         if (vf_dev && vf_dev->dev_ops->reta_update)
     795                 :          0 :                 ret = vf_dev->dev_ops->reta_update(vf_dev,
     796                 :            :                                                    reta_conf, reta_size);
     797                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     798                 :            : 
     799                 :          0 :         return ret;
     800                 :            : }
     801                 :            : 
     802                 :          0 : int hn_vf_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
     803                 :            : {
     804                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     805                 :            :         struct rte_eth_dev *vf_dev;
     806                 :            :         int ret = 0;
     807                 :            : 
     808                 :          0 :         rte_rwlock_read_lock(&hv->vf_lock);
     809                 :            :         vf_dev = hn_get_vf_dev(hv);
     810   [ #  #  #  # ]:          0 :         if (hv->vf_ctx.vf_vsc_switched && vf_dev)
     811                 :          0 :                 ret = rte_eth_dev_set_mtu(vf_dev->data->port_id, mtu);
     812                 :            :         rte_rwlock_read_unlock(&hv->vf_lock);
     813                 :            : 
     814                 :          0 :         return ret;
     815                 :            : }

Generated by: LCOV version 1.14