LCOV - code coverage report
Current view: top level - drivers/net/virtio - virtio_ethdev.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 2 1151 0.2 %
Date: 2025-06-01 17:49:23 Functions: 2 71 2.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 728 0.3 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2016 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdint.h>
       6                 :            : #include <string.h>
       7                 :            : #include <stdio.h>
       8                 :            : #include <stdlib.h>
       9                 :            : #include <errno.h>
      10                 :            : #include <unistd.h>
      11                 :            : 
      12                 :            : #include <ethdev_driver.h>
      13                 :            : #include <rte_memcpy.h>
      14                 :            : #include <rte_string_fns.h>
      15                 :            : #include <rte_memzone.h>
      16                 :            : #include <rte_malloc.h>
      17                 :            : #include <rte_branch_prediction.h>
      18                 :            : #include <rte_ether.h>
      19                 :            : #include <rte_ip.h>
      20                 :            : #include <rte_arp.h>
      21                 :            : #include <rte_common.h>
      22                 :            : #include <rte_errno.h>
      23                 :            : #include <rte_cpuflags.h>
      24                 :            : #include <rte_vect.h>
      25                 :            : #include <rte_memory.h>
      26                 :            : #include <rte_eal_paging.h>
      27                 :            : #include <rte_eal.h>
      28                 :            : #include <dev_driver.h>
      29                 :            : #include <rte_cycles.h>
      30                 :            : #include <rte_kvargs.h>
      31                 :            : 
      32                 :            : #include "virtio_ethdev.h"
      33                 :            : #include "virtio.h"
      34                 :            : #include "virtio_logs.h"
      35                 :            : #include "virtqueue.h"
      36                 :            : #include "virtio_cvq.h"
      37                 :            : #include "virtio_rxtx.h"
      38                 :            : #include "virtio_rxtx_simple.h"
      39                 :            : #include "virtio_user/virtio_user_dev.h"
      40                 :            : 
      41                 :            : static int  virtio_dev_configure(struct rte_eth_dev *dev);
      42                 :            : static int  virtio_dev_start(struct rte_eth_dev *dev);
      43                 :            : static int virtio_dev_promiscuous_enable(struct rte_eth_dev *dev);
      44                 :            : static int virtio_dev_promiscuous_disable(struct rte_eth_dev *dev);
      45                 :            : static int virtio_dev_allmulticast_enable(struct rte_eth_dev *dev);
      46                 :            : static int virtio_dev_allmulticast_disable(struct rte_eth_dev *dev);
      47                 :            : static uint32_t virtio_dev_speed_capa_get(uint32_t speed);
      48                 :            : static int virtio_dev_devargs_parse(struct rte_devargs *devargs,
      49                 :            :         uint32_t *speed,
      50                 :            :         int *vectorized);
      51                 :            : static int virtio_dev_info_get(struct rte_eth_dev *dev,
      52                 :            :                                 struct rte_eth_dev_info *dev_info);
      53                 :            : static int virtio_dev_link_update(struct rte_eth_dev *dev,
      54                 :            :         int wait_to_complete);
      55                 :            : static int virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask);
      56                 :            : static int virtio_dev_rss_hash_update(struct rte_eth_dev *dev,
      57                 :            :                 struct rte_eth_rss_conf *rss_conf);
      58                 :            : static int virtio_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
      59                 :            :                 struct rte_eth_rss_conf *rss_conf);
      60                 :            : static int virtio_dev_rss_reta_update(struct rte_eth_dev *dev,
      61                 :            :                          struct rte_eth_rss_reta_entry64 *reta_conf,
      62                 :            :                          uint16_t reta_size);
      63                 :            : static int virtio_dev_rss_reta_query(struct rte_eth_dev *dev,
      64                 :            :                          struct rte_eth_rss_reta_entry64 *reta_conf,
      65                 :            :                          uint16_t reta_size);
      66                 :            : 
      67                 :            : static void virtio_set_hwaddr(struct virtio_hw *hw);
      68                 :            : static void virtio_get_hwaddr(struct virtio_hw *hw);
      69                 :            : 
      70                 :            : static int virtio_dev_stats_get(struct rte_eth_dev *dev,
      71                 :            :                                  struct rte_eth_stats *stats);
      72                 :            : static int virtio_dev_xstats_get(struct rte_eth_dev *dev,
      73                 :            :                                  struct rte_eth_xstat *xstats, unsigned n);
      74                 :            : static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
      75                 :            :                                        struct rte_eth_xstat_name *xstats_names,
      76                 :            :                                        unsigned limit);
      77                 :            : static int virtio_dev_stats_reset(struct rte_eth_dev *dev);
      78                 :            : static void virtio_dev_free_mbufs(struct rte_eth_dev *dev);
      79                 :            : static int virtio_vlan_filter_set(struct rte_eth_dev *dev,
      80                 :            :                                 uint16_t vlan_id, int on);
      81                 :            : static int virtio_mac_addr_add(struct rte_eth_dev *dev,
      82                 :            :                                 struct rte_ether_addr *mac_addr,
      83                 :            :                                 uint32_t index, uint32_t vmdq);
      84                 :            : static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
      85                 :            : static int virtio_mac_addr_set(struct rte_eth_dev *dev,
      86                 :            :                                 struct rte_ether_addr *mac_addr);
      87                 :            : 
      88                 :            : static int virtio_intr_disable(struct rte_eth_dev *dev);
      89                 :            : static int virtio_get_monitor_addr(void *rx_queue,
      90                 :            :                                 struct rte_power_monitor_cond *pmc);
      91                 :            : 
      92                 :            : static int virtio_dev_queue_stats_mapping_set(
      93                 :            :         struct rte_eth_dev *eth_dev,
      94                 :            :         uint16_t queue_id,
      95                 :            :         uint8_t stat_idx,
      96                 :            :         uint8_t is_rx);
      97                 :            : 
      98                 :            : static void virtio_notify_peers(struct rte_eth_dev *dev);
      99                 :            : static void virtio_ack_link_announce(struct rte_eth_dev *dev);
     100                 :            : 
     101                 :            : static int virtio_rx_burst_mode_get(struct rte_eth_dev *dev,
     102                 :            :         __rte_unused uint16_t queue_id, struct rte_eth_burst_mode *mode);
     103                 :            : static int virtio_tx_burst_mode_get(struct rte_eth_dev *dev,
     104                 :            :         __rte_unused uint16_t queue_id, struct rte_eth_burst_mode *mode);
     105                 :            : 
     106                 :            : struct rte_virtio_xstats_name_off {
     107                 :            :         char name[RTE_ETH_XSTATS_NAME_SIZE];
     108                 :            :         unsigned offset;
     109                 :            : };
     110                 :            : 
     111                 :            : /* [rt]x_qX_ is prepended to the name string here */
     112                 :            : static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = {
     113                 :            :         {"good_packets",           offsetof(struct virtnet_rx, stats.packets)},
     114                 :            :         {"good_bytes",             offsetof(struct virtnet_rx, stats.bytes)},
     115                 :            :         {"errors",                 offsetof(struct virtnet_rx, stats.errors)},
     116                 :            :         {"multicast_packets",      offsetof(struct virtnet_rx, stats.multicast)},
     117                 :            :         {"broadcast_packets",      offsetof(struct virtnet_rx, stats.broadcast)},
     118                 :            :         {"undersize_packets",      offsetof(struct virtnet_rx, stats.size_bins[0])},
     119                 :            :         {"size_64_packets",        offsetof(struct virtnet_rx, stats.size_bins[1])},
     120                 :            :         {"size_65_127_packets",    offsetof(struct virtnet_rx, stats.size_bins[2])},
     121                 :            :         {"size_128_255_packets",   offsetof(struct virtnet_rx, stats.size_bins[3])},
     122                 :            :         {"size_256_511_packets",   offsetof(struct virtnet_rx, stats.size_bins[4])},
     123                 :            :         {"size_512_1023_packets",  offsetof(struct virtnet_rx, stats.size_bins[5])},
     124                 :            :         {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])},
     125                 :            :         {"size_1519_max_packets",  offsetof(struct virtnet_rx, stats.size_bins[7])},
     126                 :            : };
     127                 :            : 
     128                 :            : /* [rt]x_qX_ is prepended to the name string here */
     129                 :            : static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
     130                 :            :         {"good_packets",           offsetof(struct virtnet_tx, stats.packets)},
     131                 :            :         {"good_bytes",             offsetof(struct virtnet_tx, stats.bytes)},
     132                 :            :         {"multicast_packets",      offsetof(struct virtnet_tx, stats.multicast)},
     133                 :            :         {"broadcast_packets",      offsetof(struct virtnet_tx, stats.broadcast)},
     134                 :            :         {"undersize_packets",      offsetof(struct virtnet_tx, stats.size_bins[0])},
     135                 :            :         {"size_64_packets",        offsetof(struct virtnet_tx, stats.size_bins[1])},
     136                 :            :         {"size_65_127_packets",    offsetof(struct virtnet_tx, stats.size_bins[2])},
     137                 :            :         {"size_128_255_packets",   offsetof(struct virtnet_tx, stats.size_bins[3])},
     138                 :            :         {"size_256_511_packets",   offsetof(struct virtnet_tx, stats.size_bins[4])},
     139                 :            :         {"size_512_1023_packets",  offsetof(struct virtnet_tx, stats.size_bins[5])},
     140                 :            :         {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])},
     141                 :            :         {"size_1519_max_packets",  offsetof(struct virtnet_tx, stats.size_bins[7])},
     142                 :            : };
     143                 :            : 
     144                 :            : #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \
     145                 :            :                             sizeof(rte_virtio_rxq_stat_strings[0]))
     146                 :            : #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
     147                 :            :                             sizeof(rte_virtio_txq_stat_strings[0]))
     148                 :            : 
     149                 :            : struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS];
     150                 :            : 
     151                 :            : static int
     152                 :          0 : virtio_set_multiple_queues_rss(struct rte_eth_dev *dev, uint16_t nb_queues)
     153                 :            : {
     154                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     155                 :            :         struct virtio_pmd_ctrl ctrl;
     156                 :            :         struct virtio_net_ctrl_rss rss;
     157                 :            :         int dlen, ret;
     158                 :            : 
     159                 :          0 :         rss.hash_types = hw->rss_hash_types & VIRTIO_NET_HASH_TYPE_MASK;
     160                 :            :         RTE_BUILD_BUG_ON(!RTE_IS_POWER_OF_2(VIRTIO_NET_RSS_RETA_SIZE));
     161                 :          0 :         rss.indirection_table_mask = VIRTIO_NET_RSS_RETA_SIZE - 1;
     162                 :          0 :         rss.unclassified_queue = 0;
     163                 :          0 :         memcpy(rss.indirection_table, hw->rss_reta, VIRTIO_NET_RSS_RETA_SIZE * sizeof(uint16_t));
     164                 :          0 :         rss.max_tx_vq = nb_queues;
     165                 :          0 :         rss.hash_key_length = VIRTIO_NET_RSS_KEY_SIZE;
     166                 :          0 :         memcpy(rss.hash_key_data, hw->rss_key, VIRTIO_NET_RSS_KEY_SIZE);
     167                 :            : 
     168                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
     169                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_RSS_CONFIG;
     170                 :            :         memcpy(ctrl.data, &rss, sizeof(rss));
     171                 :            : 
     172                 :          0 :         dlen = sizeof(rss);
     173                 :            : 
     174                 :          0 :         ret = virtio_send_command(hw->cvq, &ctrl, &dlen, 1);
     175         [ #  # ]:          0 :         if (ret) {
     176                 :          0 :                 PMD_INIT_LOG(ERR, "RSS multiqueue configured but send command failed");
     177                 :          0 :                 return -EINVAL;
     178                 :            :         }
     179                 :            : 
     180                 :            :         return 0;
     181                 :            : }
     182                 :            : 
     183                 :            : static int
     184                 :          0 : virtio_set_multiple_queues_auto(struct rte_eth_dev *dev, uint16_t nb_queues)
     185                 :            : {
     186                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     187                 :            :         struct virtio_pmd_ctrl ctrl;
     188                 :            :         int dlen;
     189                 :            :         int ret;
     190                 :            : 
     191                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_MQ;
     192                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET;
     193                 :            :         memcpy(ctrl.data, &nb_queues, sizeof(uint16_t));
     194                 :            : 
     195                 :          0 :         dlen = sizeof(uint16_t);
     196                 :            : 
     197                 :          0 :         ret = virtio_send_command(hw->cvq, &ctrl, &dlen, 1);
     198         [ #  # ]:          0 :         if (ret) {
     199                 :          0 :                 PMD_INIT_LOG(ERR, "Multiqueue configured but send command "
     200                 :            :                           "failed, this is too late now...");
     201                 :          0 :                 return -EINVAL;
     202                 :            :         }
     203                 :            : 
     204                 :            :         return 0;
     205                 :            : }
     206                 :            : 
     207                 :            : static int
     208                 :          0 : virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues)
     209                 :            : {
     210         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
     211                 :            : 
     212         [ #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_RSS))
     213                 :          0 :                 return virtio_set_multiple_queues_rss(dev, nb_queues);
     214                 :            :         else
     215                 :          0 :                 return virtio_set_multiple_queues_auto(dev, nb_queues);
     216                 :            : }
     217                 :            : 
     218                 :            : static uint16_t
     219                 :            : virtio_get_nr_vq(struct virtio_hw *hw)
     220                 :            : {
     221                 :          0 :         uint16_t nr_vq = hw->max_queue_pairs * 2;
     222                 :            : 
     223   [ #  #  #  #  :          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
                   #  # ]
     224                 :          0 :                 nr_vq += 1;
     225                 :            : 
     226                 :            :         return nr_vq;
     227                 :            : }
     228                 :            : 
     229                 :            : static void
     230                 :          0 : virtio_control_queue_notify(struct virtqueue *vq, __rte_unused void *cookie)
     231                 :            : {
     232                 :            :         virtqueue_notify(vq);
     233                 :          0 : }
     234                 :            : 
     235                 :            : static int
     236                 :          0 : virtio_init_queue(struct rte_eth_dev *dev, uint16_t queue_idx)
     237                 :            : {
     238                 :            :         char vq_name[VIRTQUEUE_MAX_NAME_SZ];
     239                 :            :         unsigned int vq_size;
     240                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     241                 :            :         struct virtqueue *vq;
     242         [ #  # ]:          0 :         int queue_type = virtio_get_queue_type(hw, queue_idx);
     243                 :            :         int ret;
     244                 :          0 :         int numa_node = dev->device->numa_node;
     245                 :            : 
     246                 :          0 :         PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
     247                 :            :                         queue_idx, numa_node);
     248                 :            : 
     249                 :            :         /*
     250                 :            :          * Read the virtqueue size from the Queue Size field
     251                 :            :          * Always power of 2 and if 0 virtqueue does not exist
     252                 :            :          */
     253                 :          0 :         vq_size = VIRTIO_OPS(hw)->get_queue_num(hw, queue_idx);
     254                 :          0 :         PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
     255         [ #  # ]:          0 :         if (vq_size == 0) {
     256                 :          0 :                 PMD_INIT_LOG(ERR, "virtqueue does not exist");
     257                 :          0 :                 return -EINVAL;
     258                 :            :         }
     259                 :            : 
     260         [ #  # ]:          0 :         if (!virtio_with_packed_queue(hw) && !rte_is_power_of_2(vq_size)) {
     261                 :          0 :                 PMD_INIT_LOG(ERR, "split virtqueue size is not power of 2");
     262                 :          0 :                 return -EINVAL;
     263                 :            :         }
     264                 :            : 
     265                 :          0 :         snprintf(vq_name, sizeof(vq_name), "port%d_vq%d", dev->data->port_id, queue_idx);
     266                 :            : 
     267                 :          0 :         vq = virtqueue_alloc(hw, queue_idx, vq_size, queue_type, numa_node, vq_name);
     268         [ #  # ]:          0 :         if (!vq) {
     269                 :          0 :                 PMD_INIT_LOG(ERR, "virtqueue init failed");
     270                 :          0 :                 return -ENOMEM;
     271                 :            :         }
     272                 :            : 
     273                 :          0 :         hw->vqs[queue_idx] = vq;
     274                 :            : 
     275         [ #  # ]:          0 :         if (queue_type == VTNET_CQ) {
     276                 :          0 :                 hw->cvq = &vq->cq;
     277                 :          0 :                 vq->cq.notify_queue = &virtio_control_queue_notify;
     278                 :            :         }
     279                 :            : 
     280         [ #  # ]:          0 :         if (VIRTIO_OPS(hw)->setup_queue(hw, vq) < 0) {
     281                 :          0 :                 PMD_INIT_LOG(ERR, "setup_queue failed");
     282                 :            :                 ret = -EINVAL;
     283                 :          0 :                 goto clean_vq;
     284                 :            :         }
     285                 :            : 
     286                 :            :         return 0;
     287                 :            : 
     288                 :            : clean_vq:
     289         [ #  # ]:          0 :         if (queue_type == VTNET_CQ)
     290                 :          0 :                 hw->cvq = NULL;
     291                 :          0 :         virtqueue_free(vq);
     292                 :          0 :         hw->vqs[queue_idx] = NULL;
     293                 :            : 
     294                 :          0 :         return ret;
     295                 :            : }
     296                 :            : 
     297                 :            : static void
     298         [ #  # ]:          0 : virtio_free_queues(struct virtio_hw *hw)
     299                 :            : {
     300                 :            :         uint16_t nr_vq = virtio_get_nr_vq(hw);
     301                 :            :         struct virtqueue *vq;
     302                 :            :         uint16_t i;
     303                 :            : 
     304         [ #  # ]:          0 :         if (hw->vqs == NULL)
     305                 :            :                 return;
     306                 :            : 
     307         [ #  # ]:          0 :         for (i = 0; i < nr_vq; i++) {
     308                 :          0 :                 vq = hw->vqs[i];
     309         [ #  # ]:          0 :                 if (!vq)
     310                 :          0 :                         continue;
     311                 :          0 :                 virtqueue_free(vq);
     312                 :          0 :                 hw->vqs[i] = NULL;
     313                 :            :         }
     314                 :            : 
     315                 :          0 :         rte_free(hw->vqs);
     316                 :          0 :         hw->vqs = NULL;
     317                 :            : }
     318                 :            : 
     319                 :            : static int
     320                 :          0 : virtio_alloc_queues(struct rte_eth_dev *dev)
     321                 :            : {
     322         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
     323                 :            :         uint16_t nr_vq = virtio_get_nr_vq(hw);
     324                 :            :         uint16_t i;
     325                 :            :         int ret;
     326                 :            : 
     327                 :          0 :         hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
     328         [ #  # ]:          0 :         if (!hw->vqs) {
     329                 :          0 :                 PMD_INIT_LOG(ERR, "failed to allocate vqs");
     330                 :          0 :                 return -ENOMEM;
     331                 :            :         }
     332                 :            : 
     333         [ #  # ]:          0 :         for (i = 0; i < nr_vq; i++) {
     334                 :          0 :                 ret = virtio_init_queue(dev, i);
     335         [ #  # ]:          0 :                 if (ret < 0) {
     336                 :          0 :                         virtio_free_queues(hw);
     337                 :          0 :                         return ret;
     338                 :            :                 }
     339                 :            :         }
     340                 :            : 
     341                 :            :         return 0;
     342                 :            : }
     343                 :            : 
     344                 :            : static void virtio_queues_unbind_intr(struct rte_eth_dev *dev);
     345                 :            : 
     346                 :            : static void
     347                 :            : virtio_free_rss(struct virtio_hw *hw)
     348                 :            : {
     349                 :          0 :         rte_free(hw->rss_key);
     350                 :          0 :         hw->rss_key = NULL;
     351                 :            : 
     352                 :          0 :         rte_free(hw->rss_reta);
     353                 :          0 :         hw->rss_reta = NULL;
     354                 :            : }
     355                 :            : 
     356                 :            : int
     357                 :          0 : virtio_dev_close(struct rte_eth_dev *dev)
     358                 :            : {
     359                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     360                 :            :         struct rte_eth_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
     361                 :            : 
     362                 :          0 :         PMD_INIT_LOG(DEBUG, "virtio_dev_close");
     363         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
     364                 :            :                 return 0;
     365                 :            : 
     366         [ #  # ]:          0 :         if (!hw->opened)
     367                 :            :                 return 0;
     368                 :          0 :         hw->opened = 0;
     369                 :            : 
     370                 :            :         /* reset the NIC */
     371         [ #  # ]:          0 :         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
     372                 :          0 :                 VIRTIO_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR);
     373         [ #  # ]:          0 :         if (intr_conf->rxq)
     374                 :          0 :                 virtio_queues_unbind_intr(dev);
     375                 :            : 
     376         [ #  # ]:          0 :         if (intr_conf->lsc || intr_conf->rxq) {
     377                 :          0 :                 virtio_intr_disable(dev);
     378                 :          0 :                 rte_intr_efd_disable(dev->intr_handle);
     379                 :          0 :                 rte_intr_vec_list_free(dev->intr_handle);
     380                 :            :         }
     381                 :            : 
     382                 :          0 :         virtio_reset(hw);
     383                 :          0 :         virtio_dev_free_mbufs(dev);
     384                 :          0 :         virtio_free_queues(hw);
     385                 :            :         virtio_free_rss(hw);
     386                 :            : 
     387                 :          0 :         return VIRTIO_OPS(hw)->dev_close(hw);
     388                 :            : }
     389                 :            : 
     390                 :            : static int
     391                 :          0 : virtio_dev_promiscuous_enable(struct rte_eth_dev *dev)
     392                 :            : {
     393         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
     394                 :            :         struct virtio_pmd_ctrl ctrl;
     395                 :            :         int dlen[1];
     396                 :            :         int ret;
     397                 :            : 
     398         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
     399                 :          0 :                 PMD_INIT_LOG(INFO, "host does not support rx control");
     400                 :          0 :                 return -ENOTSUP;
     401                 :            :         }
     402                 :            : 
     403                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
     404                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
     405                 :          0 :         ctrl.data[0] = 1;
     406                 :          0 :         dlen[0] = 1;
     407                 :            : 
     408                 :          0 :         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
     409         [ #  # ]:          0 :         if (ret) {
     410                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to enable promisc");
     411                 :          0 :                 return -EAGAIN;
     412                 :            :         }
     413                 :            : 
     414                 :            :         return 0;
     415                 :            : }
     416                 :            : 
     417                 :            : static int
     418                 :          0 : virtio_dev_promiscuous_disable(struct rte_eth_dev *dev)
     419                 :            : {
     420         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
     421                 :            :         struct virtio_pmd_ctrl ctrl;
     422                 :            :         int dlen[1];
     423                 :            :         int ret;
     424                 :            : 
     425         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
     426                 :          0 :                 PMD_INIT_LOG(INFO, "host does not support rx control");
     427                 :          0 :                 return -ENOTSUP;
     428                 :            :         }
     429                 :            : 
     430                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
     431                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC;
     432                 :          0 :         ctrl.data[0] = 0;
     433                 :          0 :         dlen[0] = 1;
     434                 :            : 
     435                 :          0 :         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
     436         [ #  # ]:          0 :         if (ret) {
     437                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to disable promisc");
     438                 :          0 :                 return -EAGAIN;
     439                 :            :         }
     440                 :            : 
     441                 :            :         return 0;
     442                 :            : }
     443                 :            : 
     444                 :            : static int
     445                 :          0 : virtio_dev_allmulticast_enable(struct rte_eth_dev *dev)
     446                 :            : {
     447         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
     448                 :            :         struct virtio_pmd_ctrl ctrl;
     449                 :            :         int dlen[1];
     450                 :            :         int ret;
     451                 :            : 
     452         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
     453                 :          0 :                 PMD_INIT_LOG(INFO, "host does not support rx control");
     454                 :          0 :                 return -ENOTSUP;
     455                 :            :         }
     456                 :            : 
     457                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
     458                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
     459                 :          0 :         ctrl.data[0] = 1;
     460                 :          0 :         dlen[0] = 1;
     461                 :            : 
     462                 :          0 :         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
     463         [ #  # ]:          0 :         if (ret) {
     464                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to enable allmulticast");
     465                 :          0 :                 return -EAGAIN;
     466                 :            :         }
     467                 :            : 
     468                 :            :         return 0;
     469                 :            : }
     470                 :            : 
     471                 :            : static int
     472                 :          0 : virtio_dev_allmulticast_disable(struct rte_eth_dev *dev)
     473                 :            : {
     474         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
     475                 :            :         struct virtio_pmd_ctrl ctrl;
     476                 :            :         int dlen[1];
     477                 :            :         int ret;
     478                 :            : 
     479         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) {
     480                 :          0 :                 PMD_INIT_LOG(INFO, "host does not support rx control");
     481                 :          0 :                 return -ENOTSUP;
     482                 :            :         }
     483                 :            : 
     484                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_RX;
     485                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI;
     486                 :          0 :         ctrl.data[0] = 0;
     487                 :          0 :         dlen[0] = 1;
     488                 :            : 
     489                 :          0 :         ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1);
     490         [ #  # ]:          0 :         if (ret) {
     491                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to disable allmulticast");
     492                 :          0 :                 return -EAGAIN;
     493                 :            :         }
     494                 :            : 
     495                 :            :         return 0;
     496                 :            : }
     497                 :            : 
     498                 :            : uint16_t
     499         [ #  # ]:          0 : virtio_rx_mem_pool_buf_size(struct rte_mempool *mp)
     500                 :            : {
     501                 :          0 :         return rte_pktmbuf_data_room_size(mp) - RTE_PKTMBUF_HEADROOM;
     502                 :            : }
     503                 :            : 
     504                 :            : bool
     505                 :          0 : virtio_rx_check_scatter(uint16_t max_rx_pkt_len, uint16_t rx_buf_size,
     506                 :            :                         bool rx_scatter_enabled, const char **error)
     507                 :            : {
     508         [ #  # ]:          0 :         if (!rx_scatter_enabled && max_rx_pkt_len > rx_buf_size) {
     509                 :          0 :                 *error = "Rx scatter is disabled and RxQ mbuf pool object size is too small";
     510                 :          0 :                 return false;
     511                 :            :         }
     512                 :            : 
     513                 :            :         return true;
     514                 :            : }
     515                 :            : 
     516                 :            : static bool
     517                 :          0 : virtio_check_scatter_on_all_rx_queues(struct rte_eth_dev *dev,
     518                 :            :                                       uint16_t frame_size)
     519                 :            : {
     520                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     521                 :            :         struct virtnet_rx *rxvq;
     522                 :            :         struct virtqueue *vq;
     523                 :            :         unsigned int qidx;
     524                 :            :         uint16_t buf_size;
     525                 :            :         const char *error;
     526                 :            : 
     527         [ #  # ]:          0 :         if (hw->vqs == NULL)
     528                 :            :                 return true;
     529                 :            : 
     530         [ #  # ]:          0 :         for (qidx = 0; qidx < hw->max_queue_pairs; qidx++) {
     531                 :          0 :                 vq = hw->vqs[2 * qidx + VTNET_SQ_RQ_QUEUE_IDX];
     532         [ #  # ]:          0 :                 if (vq == NULL)
     533                 :          0 :                         continue;
     534                 :            : 
     535                 :            :                 rxvq = &vq->rxq;
     536         [ #  # ]:          0 :                 if (rxvq->mpool == NULL)
     537                 :          0 :                         continue;
     538                 :          0 :                 buf_size = virtio_rx_mem_pool_buf_size(rxvq->mpool);
     539                 :            : 
     540         [ #  # ]:          0 :                 if (!virtio_rx_check_scatter(frame_size, buf_size,
     541                 :          0 :                                              hw->rx_ol_scatter, &error)) {
     542                 :          0 :                         PMD_INIT_LOG(ERR, "MTU check for RxQ %u failed: %s",
     543                 :            :                                      qidx, error);
     544                 :          0 :                         return false;
     545                 :            :                 }
     546                 :            :         }
     547                 :            : 
     548                 :            :         return true;
     549                 :            : }
     550                 :            : 
     551                 :            : #define VLAN_TAG_LEN           4    /* 802.3ac tag (not DMA'd) */
     552                 :            : static int
     553                 :          0 : virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
     554                 :            : {
     555                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     556                 :          0 :         uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
     557                 :          0 :                                  hw->vtnet_hdr_size;
     558                 :          0 :         uint32_t frame_size = mtu + ether_hdr_len;
     559                 :          0 :         uint32_t max_frame_size = hw->max_mtu + ether_hdr_len;
     560                 :            : 
     561                 :          0 :         max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN);
     562                 :            : 
     563         [ #  # ]:          0 :         if (mtu < RTE_ETHER_MIN_MTU || frame_size > max_frame_size) {
     564                 :          0 :                 PMD_INIT_LOG(ERR, "MTU should be between %d and %d",
     565                 :            :                         RTE_ETHER_MIN_MTU, max_frame_size - ether_hdr_len);
     566                 :          0 :                 return -EINVAL;
     567                 :            :         }
     568                 :            : 
     569         [ #  # ]:          0 :         if (!virtio_check_scatter_on_all_rx_queues(dev, frame_size)) {
     570                 :          0 :                 PMD_INIT_LOG(ERR, "MTU vs Rx scatter and Rx buffers check failed");
     571                 :          0 :                 return -EINVAL;
     572                 :            :         }
     573                 :            : 
     574                 :          0 :         hw->max_rx_pkt_len = frame_size;
     575                 :            : 
     576                 :          0 :         return 0;
     577                 :            : }
     578                 :            : 
     579                 :            : static int
     580                 :          0 : virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
     581                 :            : {
     582                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     583                 :          0 :         struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
     584         [ #  # ]:          0 :         struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
     585                 :            : 
     586                 :            :         virtqueue_enable_intr(vq);
     587         [ #  # ]:          0 :         virtio_mb(hw->weak_barriers);
     588                 :          0 :         return 0;
     589                 :            : }
     590                 :            : 
     591                 :            : static int
     592                 :          0 : virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
     593                 :            : {
     594                 :          0 :         struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id];
     595         [ #  # ]:          0 :         struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
     596                 :            : 
     597                 :            :         virtqueue_disable_intr(vq);
     598                 :          0 :         return 0;
     599                 :            : }
     600                 :            : 
     601                 :            : static int
     602                 :          0 : virtio_dev_priv_dump(struct rte_eth_dev *dev, FILE *f)
     603                 :            : {
     604                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     605                 :            : 
     606                 :          0 :         fprintf(f, "guest_features: 0x%" PRIx64 "\n", hw->guest_features);
     607                 :          0 :         fprintf(f, "vtnet_hdr_size: %u\n", hw->vtnet_hdr_size);
     608                 :          0 :         fprintf(f, "use_vec: rx-%u tx-%u\n", hw->use_vec_rx, hw->use_vec_tx);
     609                 :          0 :         fprintf(f, "use_inorder: rx-%u tx-%u\n", hw->use_inorder_rx, hw->use_inorder_tx);
     610                 :          0 :         fprintf(f, "intr_lsc: %u\n", hw->intr_lsc);
     611                 :          0 :         fprintf(f, "max_mtu: %u\n", hw->max_mtu);
     612                 :          0 :         fprintf(f, "max_rx_pkt_len: %zu\n", hw->max_rx_pkt_len);
     613                 :          0 :         fprintf(f, "max_queue_pairs: %u\n", hw->max_queue_pairs);
     614                 :          0 :         fprintf(f, "req_guest_features: 0x%" PRIx64 "\n", hw->req_guest_features);
     615                 :            : 
     616                 :          0 :         return 0;
     617                 :            : }
     618                 :            : 
     619                 :            : /*
     620                 :            :  * dev_ops for virtio, bare necessities for basic operation
     621                 :            :  */
     622                 :            : static const struct eth_dev_ops virtio_eth_dev_ops = {
     623                 :            :         .dev_configure           = virtio_dev_configure,
     624                 :            :         .dev_start               = virtio_dev_start,
     625                 :            :         .dev_stop                = virtio_dev_stop,
     626                 :            :         .dev_close               = virtio_dev_close,
     627                 :            :         .promiscuous_enable      = virtio_dev_promiscuous_enable,
     628                 :            :         .promiscuous_disable     = virtio_dev_promiscuous_disable,
     629                 :            :         .allmulticast_enable     = virtio_dev_allmulticast_enable,
     630                 :            :         .allmulticast_disable    = virtio_dev_allmulticast_disable,
     631                 :            :         .mtu_set                 = virtio_mtu_set,
     632                 :            :         .dev_infos_get           = virtio_dev_info_get,
     633                 :            :         .stats_get               = virtio_dev_stats_get,
     634                 :            :         .xstats_get              = virtio_dev_xstats_get,
     635                 :            :         .xstats_get_names        = virtio_dev_xstats_get_names,
     636                 :            :         .stats_reset             = virtio_dev_stats_reset,
     637                 :            :         .xstats_reset            = virtio_dev_stats_reset,
     638                 :            :         .link_update             = virtio_dev_link_update,
     639                 :            :         .vlan_offload_set        = virtio_dev_vlan_offload_set,
     640                 :            :         .rx_queue_setup          = virtio_dev_rx_queue_setup,
     641                 :            :         .rx_queue_intr_enable    = virtio_dev_rx_queue_intr_enable,
     642                 :            :         .rx_queue_intr_disable   = virtio_dev_rx_queue_intr_disable,
     643                 :            :         .tx_queue_setup          = virtio_dev_tx_queue_setup,
     644                 :            :         .rx_burst_mode_get       = virtio_rx_burst_mode_get,
     645                 :            :         .tx_burst_mode_get       = virtio_tx_burst_mode_get,
     646                 :            :         .rss_hash_update         = virtio_dev_rss_hash_update,
     647                 :            :         .rss_hash_conf_get       = virtio_dev_rss_hash_conf_get,
     648                 :            :         .reta_update             = virtio_dev_rss_reta_update,
     649                 :            :         .reta_query              = virtio_dev_rss_reta_query,
     650                 :            :         /* collect stats per queue */
     651                 :            :         .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
     652                 :            :         .vlan_filter_set         = virtio_vlan_filter_set,
     653                 :            :         .mac_addr_add            = virtio_mac_addr_add,
     654                 :            :         .mac_addr_remove         = virtio_mac_addr_remove,
     655                 :            :         .mac_addr_set            = virtio_mac_addr_set,
     656                 :            :         .get_monitor_addr        = virtio_get_monitor_addr,
     657                 :            :         .eth_dev_priv_dump       = virtio_dev_priv_dump,
     658                 :            : };
     659                 :            : 
     660                 :            : /*
     661                 :            :  * dev_ops for virtio-user in secondary processes, as we just have
     662                 :            :  * some limited supports currently.
     663                 :            :  */
     664                 :            : const struct eth_dev_ops virtio_user_secondary_eth_dev_ops = {
     665                 :            :         .dev_infos_get           = virtio_dev_info_get,
     666                 :            :         .stats_get               = virtio_dev_stats_get,
     667                 :            :         .xstats_get              = virtio_dev_xstats_get,
     668                 :            :         .xstats_get_names        = virtio_dev_xstats_get_names,
     669                 :            :         .stats_reset             = virtio_dev_stats_reset,
     670                 :            :         .xstats_reset            = virtio_dev_stats_reset,
     671                 :            :         /* collect stats per queue */
     672                 :            :         .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set,
     673                 :            : };
     674                 :            : 
     675                 :            : static void
     676                 :          0 : virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
     677                 :            : {
     678                 :            :         unsigned i;
     679                 :            : 
     680         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     681                 :          0 :                 const struct virtnet_tx *txvq = dev->data->tx_queues[i];
     682         [ #  # ]:          0 :                 if (txvq == NULL)
     683                 :          0 :                         continue;
     684                 :            : 
     685                 :          0 :                 stats->opackets += txvq->stats.packets;
     686                 :          0 :                 stats->obytes += txvq->stats.bytes;
     687                 :            : 
     688         [ #  # ]:          0 :                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
     689                 :          0 :                         stats->q_opackets[i] = txvq->stats.packets;
     690                 :          0 :                         stats->q_obytes[i] = txvq->stats.bytes;
     691                 :            :                 }
     692                 :            :         }
     693                 :            : 
     694         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
     695                 :          0 :                 const struct virtnet_rx *rxvq = dev->data->rx_queues[i];
     696         [ #  # ]:          0 :                 if (rxvq == NULL)
     697                 :          0 :                         continue;
     698                 :            : 
     699                 :          0 :                 stats->ipackets += rxvq->stats.packets;
     700                 :          0 :                 stats->ibytes += rxvq->stats.bytes;
     701                 :          0 :                 stats->ierrors += rxvq->stats.errors;
     702                 :            : 
     703         [ #  # ]:          0 :                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
     704                 :          0 :                         stats->q_ipackets[i] = rxvq->stats.packets;
     705                 :          0 :                         stats->q_ibytes[i] = rxvq->stats.bytes;
     706                 :            :                 }
     707                 :            :         }
     708                 :            : 
     709                 :          0 :         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
     710                 :          0 : }
     711                 :            : 
     712                 :          0 : static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev,
     713                 :            :                                        struct rte_eth_xstat_name *xstats_names,
     714                 :            :                                        __rte_unused unsigned limit)
     715                 :            : {
     716                 :            :         unsigned i;
     717                 :            :         unsigned count = 0;
     718                 :            :         unsigned t;
     719                 :            : 
     720                 :          0 :         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
     721                 :          0 :                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
     722                 :            : 
     723         [ #  # ]:          0 :         if (xstats_names != NULL) {
     724                 :            :                 /* Note: limit checked in rte_eth_xstats_names() */
     725                 :            : 
     726         [ #  # ]:          0 :                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
     727                 :          0 :                         struct virtnet_rx *rxvq = dev->data->rx_queues[i];
     728         [ #  # ]:          0 :                         if (rxvq == NULL)
     729                 :          0 :                                 continue;
     730         [ #  # ]:          0 :                         for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
     731                 :          0 :                                 snprintf(xstats_names[count].name,
     732                 :            :                                         sizeof(xstats_names[count].name),
     733                 :            :                                         "rx_q%u_%s", i,
     734                 :          0 :                                         rte_virtio_rxq_stat_strings[t].name);
     735                 :          0 :                                 count++;
     736                 :            :                         }
     737                 :            :                 }
     738                 :            : 
     739         [ #  # ]:          0 :                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
     740                 :          0 :                         struct virtnet_tx *txvq = dev->data->tx_queues[i];
     741         [ #  # ]:          0 :                         if (txvq == NULL)
     742                 :          0 :                                 continue;
     743         [ #  # ]:          0 :                         for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
     744                 :          0 :                                 snprintf(xstats_names[count].name,
     745                 :            :                                         sizeof(xstats_names[count].name),
     746                 :            :                                         "tx_q%u_%s", i,
     747                 :          0 :                                         rte_virtio_txq_stat_strings[t].name);
     748                 :          0 :                                 count++;
     749                 :            :                         }
     750                 :            :                 }
     751                 :          0 :                 return count;
     752                 :            :         }
     753                 :          0 :         return nstats;
     754                 :            : }
     755                 :            : 
     756                 :            : static int
     757                 :          0 : virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
     758                 :            :                       unsigned n)
     759                 :            : {
     760                 :            :         unsigned i;
     761                 :            :         unsigned count = 0;
     762                 :            : 
     763                 :          0 :         unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS +
     764                 :          0 :                 dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS;
     765                 :            : 
     766         [ #  # ]:          0 :         if (n < nstats)
     767                 :          0 :                 return nstats;
     768                 :            : 
     769         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
     770                 :          0 :                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
     771                 :            : 
     772         [ #  # ]:          0 :                 if (rxvq == NULL)
     773                 :          0 :                         continue;
     774                 :            : 
     775                 :            :                 unsigned t;
     776                 :            : 
     777         [ #  # ]:          0 :                 for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) {
     778                 :          0 :                         xstats[count].value = *(uint64_t *)(((char *)rxvq) +
     779                 :          0 :                                 rte_virtio_rxq_stat_strings[t].offset);
     780                 :          0 :                         xstats[count].id = count;
     781                 :          0 :                         count++;
     782                 :            :                 }
     783                 :            :         }
     784                 :            : 
     785         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     786                 :          0 :                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
     787                 :            : 
     788         [ #  # ]:          0 :                 if (txvq == NULL)
     789                 :          0 :                         continue;
     790                 :            : 
     791                 :            :                 unsigned t;
     792                 :            : 
     793         [ #  # ]:          0 :                 for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) {
     794                 :          0 :                         xstats[count].value = *(uint64_t *)(((char *)txvq) +
     795                 :          0 :                                 rte_virtio_txq_stat_strings[t].offset);
     796                 :          0 :                         xstats[count].id = count;
     797                 :          0 :                         count++;
     798                 :            :                 }
     799                 :            :         }
     800                 :            : 
     801                 :          0 :         return count;
     802                 :            : }
     803                 :            : 
     804                 :            : static int
     805                 :          0 : virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
     806                 :            : {
     807                 :          0 :         virtio_update_stats(dev, stats);
     808                 :            : 
     809                 :          0 :         return 0;
     810                 :            : }
     811                 :            : 
     812                 :            : static int
     813                 :          0 : virtio_dev_stats_reset(struct rte_eth_dev *dev)
     814                 :            : {
     815                 :            :         unsigned int i;
     816                 :            : 
     817         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     818                 :          0 :                 struct virtnet_tx *txvq = dev->data->tx_queues[i];
     819         [ #  # ]:          0 :                 if (txvq == NULL)
     820                 :          0 :                         continue;
     821                 :            : 
     822                 :          0 :                 txvq->stats.packets = 0;
     823                 :          0 :                 txvq->stats.bytes = 0;
     824                 :          0 :                 txvq->stats.multicast = 0;
     825                 :          0 :                 txvq->stats.broadcast = 0;
     826                 :          0 :                 memset(txvq->stats.size_bins, 0,
     827                 :            :                        sizeof(txvq->stats.size_bins[0]) * 8);
     828                 :            :         }
     829                 :            : 
     830         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
     831                 :          0 :                 struct virtnet_rx *rxvq = dev->data->rx_queues[i];
     832         [ #  # ]:          0 :                 if (rxvq == NULL)
     833                 :          0 :                         continue;
     834                 :            : 
     835                 :          0 :                 rxvq->stats.packets = 0;
     836                 :          0 :                 rxvq->stats.bytes = 0;
     837                 :          0 :                 rxvq->stats.errors = 0;
     838                 :          0 :                 rxvq->stats.multicast = 0;
     839                 :          0 :                 rxvq->stats.broadcast = 0;
     840                 :          0 :                 memset(rxvq->stats.size_bins, 0,
     841                 :            :                        sizeof(rxvq->stats.size_bins[0]) * 8);
     842                 :            :         }
     843                 :            : 
     844                 :          0 :         return 0;
     845                 :            : }
     846                 :            : 
     847                 :            : static void
     848                 :            : virtio_set_hwaddr(struct virtio_hw *hw)
     849                 :            : {
     850                 :          0 :         virtio_write_dev_config(hw,
     851                 :            :                         offsetof(struct virtio_net_config, mac),
     852                 :            :                         &hw->mac_addr, RTE_ETHER_ADDR_LEN);
     853                 :          0 : }
     854                 :            : 
     855                 :            : static void
     856         [ #  # ]:          0 : virtio_get_hwaddr(struct virtio_hw *hw)
     857                 :            : {
     858         [ #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_MAC)) {
     859                 :          0 :                 virtio_read_dev_config(hw,
     860                 :            :                         offsetof(struct virtio_net_config, mac),
     861                 :          0 :                         &hw->mac_addr, RTE_ETHER_ADDR_LEN);
     862                 :            :         } else {
     863                 :          0 :                 rte_eth_random_addr(&hw->mac_addr[0]);
     864                 :            :                 virtio_set_hwaddr(hw);
     865                 :            :         }
     866                 :          0 : }
     867                 :            : 
     868                 :            : static int
     869         [ #  # ]:          0 : virtio_mac_table_set(struct virtio_hw *hw,
     870                 :            :                      const struct virtio_net_ctrl_mac *uc,
     871                 :            :                      const struct virtio_net_ctrl_mac *mc)
     872                 :            : {
     873                 :            :         struct virtio_pmd_ctrl ctrl;
     874                 :            :         int err, len[2];
     875                 :            : 
     876         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
     877                 :          0 :                 PMD_DRV_LOG(INFO, "host does not support mac table");
     878                 :          0 :                 return -1;
     879                 :            :         }
     880                 :            : 
     881                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
     882                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET;
     883                 :            : 
     884                 :          0 :         len[0] = uc->entries * RTE_ETHER_ADDR_LEN + sizeof(uc->entries);
     885                 :          0 :         memcpy(ctrl.data, uc, len[0]);
     886                 :            : 
     887                 :          0 :         len[1] = mc->entries * RTE_ETHER_ADDR_LEN + sizeof(mc->entries);
     888                 :          0 :         memcpy(ctrl.data + len[0], mc, len[1]);
     889                 :            : 
     890                 :          0 :         err = virtio_send_command(hw->cvq, &ctrl, len, 2);
     891         [ #  # ]:          0 :         if (err != 0)
     892                 :          0 :                 PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err);
     893                 :            :         return err;
     894                 :            : }
     895                 :            : 
     896                 :            : static int
     897                 :          0 : virtio_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
     898                 :            :                     uint32_t index, uint32_t vmdq __rte_unused)
     899                 :            : {
     900                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     901                 :          0 :         const struct rte_ether_addr *addrs = dev->data->mac_addrs;
     902                 :            :         unsigned int i;
     903                 :            :         struct virtio_net_ctrl_mac *uc, *mc;
     904                 :            : 
     905         [ #  # ]:          0 :         if (index >= VIRTIO_MAX_MAC_ADDRS) {
     906                 :          0 :                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
     907                 :          0 :                 return -EINVAL;
     908                 :            :         }
     909                 :            : 
     910                 :          0 :         uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
     911                 :            :                 sizeof(uc->entries));
     912                 :          0 :         uc->entries = 0;
     913                 :          0 :         mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
     914                 :            :                 sizeof(mc->entries));
     915                 :          0 :         mc->entries = 0;
     916                 :            : 
     917         [ #  # ]:          0 :         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
     918                 :            :                 const struct rte_ether_addr *addr
     919         [ #  # ]:          0 :                         = (i == index) ? mac_addr : addrs + i;
     920                 :            :                 struct virtio_net_ctrl_mac *tbl
     921         [ #  # ]:          0 :                         = rte_is_multicast_ether_addr(addr) ? mc : uc;
     922                 :            : 
     923         [ #  # ]:          0 :                 if (rte_is_zero_ether_addr(addr))
     924                 :            :                         break;
     925                 :          0 :                 memcpy(&tbl->macs[tbl->entries++], addr, RTE_ETHER_ADDR_LEN);
     926                 :            :         }
     927                 :            : 
     928                 :          0 :         return virtio_mac_table_set(hw, uc, mc);
     929                 :            : }
     930                 :            : 
     931                 :            : static void
     932                 :          0 : virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
     933                 :            : {
     934                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     935                 :          0 :         struct rte_ether_addr *addrs = dev->data->mac_addrs;
     936                 :            :         struct virtio_net_ctrl_mac *uc, *mc;
     937                 :            :         unsigned int i;
     938                 :            : 
     939         [ #  # ]:          0 :         if (index >= VIRTIO_MAX_MAC_ADDRS) {
     940                 :          0 :                 PMD_DRV_LOG(ERR, "mac address index %u out of range", index);
     941                 :          0 :                 return;
     942                 :            :         }
     943                 :            : 
     944                 :          0 :         uc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
     945                 :            :                 sizeof(uc->entries));
     946                 :          0 :         uc->entries = 0;
     947                 :          0 :         mc = alloca(VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN +
     948                 :            :                 sizeof(mc->entries));
     949                 :          0 :         mc->entries = 0;
     950                 :            : 
     951         [ #  # ]:          0 :         for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) {
     952                 :            :                 struct virtio_net_ctrl_mac *tbl;
     953                 :            : 
     954   [ #  #  #  # ]:          0 :                 if (i == index || rte_is_zero_ether_addr(addrs + i))
     955                 :          0 :                         continue;
     956                 :            : 
     957         [ #  # ]:          0 :                 tbl = rte_is_multicast_ether_addr(addrs + i) ? mc : uc;
     958                 :          0 :                 memcpy(&tbl->macs[tbl->entries++], addrs + i,
     959                 :            :                         RTE_ETHER_ADDR_LEN);
     960                 :            :         }
     961                 :            : 
     962                 :          0 :         virtio_mac_table_set(hw, uc, mc);
     963                 :            : }
     964                 :            : 
     965                 :            : static int
     966                 :          0 : virtio_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
     967                 :            : {
     968                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
     969                 :            : 
     970         [ #  # ]:          0 :         memcpy(hw->mac_addr, mac_addr, RTE_ETHER_ADDR_LEN);
     971                 :            : 
     972                 :            :         /* Use atomic update if available */
     973         [ #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
     974                 :            :                 struct virtio_pmd_ctrl ctrl;
     975                 :          0 :                 int len = RTE_ETHER_ADDR_LEN;
     976                 :            : 
     977                 :          0 :                 ctrl.hdr.class = VIRTIO_NET_CTRL_MAC;
     978                 :          0 :                 ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET;
     979                 :            : 
     980                 :            :                 memcpy(ctrl.data, mac_addr, RTE_ETHER_ADDR_LEN);
     981                 :          0 :                 return virtio_send_command(hw->cvq, &ctrl, &len, 1);
     982                 :            :         }
     983                 :            : 
     984         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_MAC))
     985                 :            :                 return -ENOTSUP;
     986                 :            : 
     987                 :            :         virtio_set_hwaddr(hw);
     988                 :          0 :         return 0;
     989                 :            : }
     990                 :            : 
     991                 :            : #define CLB_VAL_IDX 0
     992                 :            : #define CLB_MSK_IDX 1
     993                 :            : #define CLB_MATCH_IDX 2
     994                 :            : static int
     995                 :          0 : virtio_monitor_callback(const uint64_t value,
     996                 :            :                 const uint64_t opaque[RTE_POWER_MONITOR_OPAQUE_SZ])
     997                 :            : {
     998                 :          0 :         const uint64_t m = opaque[CLB_MSK_IDX];
     999                 :          0 :         const uint64_t v = opaque[CLB_VAL_IDX];
    1000                 :          0 :         const uint64_t c = opaque[CLB_MATCH_IDX];
    1001                 :            : 
    1002         [ #  # ]:          0 :         if (c)
    1003         [ #  # ]:          0 :                 return (value & m) == v ? -1 : 0;
    1004                 :            :         else
    1005         [ #  # ]:          0 :                 return (value & m) == v ? 0 : -1;
    1006                 :            : }
    1007                 :            : 
    1008                 :            : static int
    1009                 :          0 : virtio_get_monitor_addr(void *rx_queue, struct rte_power_monitor_cond *pmc)
    1010                 :            : {
    1011                 :            :         struct virtnet_rx *rxvq = rx_queue;
    1012                 :          0 :         struct virtqueue *vq = virtnet_rxq_to_vq(rxvq);
    1013                 :            :         struct virtio_hw *hw;
    1014                 :            : 
    1015         [ #  # ]:          0 :         if (vq == NULL)
    1016                 :            :                 return -EINVAL;
    1017                 :            : 
    1018         [ #  # ]:          0 :         hw = vq->hw;
    1019         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw)) {
    1020                 :            :                 struct vring_packed_desc *desc;
    1021                 :          0 :                 desc = vq->vq_packed.ring.desc;
    1022                 :          0 :                 pmc->addr = &desc[vq->vq_used_cons_idx].flags;
    1023         [ #  # ]:          0 :                 if (vq->vq_packed.used_wrap_counter)
    1024                 :          0 :                         pmc->opaque[CLB_VAL_IDX] =
    1025                 :            :                                                 VRING_PACKED_DESC_F_AVAIL_USED;
    1026                 :            :                 else
    1027                 :          0 :                         pmc->opaque[CLB_VAL_IDX] = 0;
    1028                 :          0 :                 pmc->opaque[CLB_MSK_IDX] = VRING_PACKED_DESC_F_AVAIL_USED;
    1029                 :          0 :                 pmc->opaque[CLB_MATCH_IDX] = 1;
    1030                 :          0 :                 pmc->size = sizeof(desc[vq->vq_used_cons_idx].flags);
    1031                 :            :         } else {
    1032                 :          0 :                 pmc->addr = &vq->vq_split.ring.used->idx;
    1033                 :          0 :                 pmc->opaque[CLB_VAL_IDX] = vq->vq_used_cons_idx
    1034                 :          0 :                                         & (vq->vq_nentries - 1);
    1035                 :          0 :                 pmc->opaque[CLB_MSK_IDX] = vq->vq_nentries - 1;
    1036                 :          0 :                 pmc->opaque[CLB_MATCH_IDX] = 0;
    1037                 :          0 :                 pmc->size = sizeof(vq->vq_split.ring.used->idx);
    1038                 :            :         }
    1039                 :          0 :         pmc->fn = virtio_monitor_callback;
    1040                 :            : 
    1041                 :          0 :         return 0;
    1042                 :            : }
    1043                 :            : 
    1044                 :            : static int
    1045                 :          0 : virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
    1046                 :            : {
    1047         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1048                 :            :         struct virtio_pmd_ctrl ctrl;
    1049                 :            :         int len;
    1050                 :            : 
    1051         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN))
    1052                 :            :                 return -ENOTSUP;
    1053                 :            : 
    1054                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN;
    1055                 :          0 :         ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL;
    1056                 :            :         memcpy(ctrl.data, &vlan_id, sizeof(vlan_id));
    1057                 :          0 :         len = sizeof(vlan_id);
    1058                 :            : 
    1059                 :          0 :         return virtio_send_command(hw->cvq, &ctrl, &len, 1);
    1060                 :            : }
    1061                 :            : 
    1062                 :            : static int
    1063                 :          0 : virtio_intr_unmask(struct rte_eth_dev *dev)
    1064                 :            : {
    1065                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1066                 :            : 
    1067         [ #  # ]:          0 :         if (rte_intr_ack(dev->intr_handle) < 0)
    1068                 :            :                 return -1;
    1069                 :            : 
    1070         [ #  # ]:          0 :         if (VIRTIO_OPS(hw)->intr_detect)
    1071                 :          0 :                 VIRTIO_OPS(hw)->intr_detect(hw);
    1072                 :            : 
    1073                 :            :         return 0;
    1074                 :            : }
    1075                 :            : 
    1076                 :            : static int
    1077                 :          0 : virtio_intr_enable(struct rte_eth_dev *dev)
    1078                 :            : {
    1079                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1080                 :            : 
    1081         [ #  # ]:          0 :         if (rte_intr_enable(dev->intr_handle) < 0)
    1082                 :            :                 return -1;
    1083                 :            : 
    1084         [ #  # ]:          0 :         if (VIRTIO_OPS(hw)->intr_detect)
    1085                 :          0 :                 VIRTIO_OPS(hw)->intr_detect(hw);
    1086                 :            : 
    1087                 :            :         return 0;
    1088                 :            : }
    1089                 :            : 
    1090                 :            : static int
    1091                 :          0 : virtio_intr_disable(struct rte_eth_dev *dev)
    1092                 :            : {
    1093                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1094                 :            : 
    1095         [ #  # ]:          0 :         if (rte_intr_disable(dev->intr_handle) < 0)
    1096                 :            :                 return -1;
    1097                 :            : 
    1098         [ #  # ]:          0 :         if (VIRTIO_OPS(hw)->intr_detect)
    1099                 :          0 :                 VIRTIO_OPS(hw)->intr_detect(hw);
    1100                 :            : 
    1101                 :            :         return 0;
    1102                 :            : }
    1103                 :            : 
    1104                 :            : static int
    1105                 :          0 : virtio_ethdev_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
    1106                 :            : {
    1107                 :            :         uint64_t host_features;
    1108                 :            : 
    1109                 :            :         /* Prepare guest_features: feature that driver wants to support */
    1110                 :          0 :         PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64,
    1111                 :            :                 req_features);
    1112                 :            : 
    1113                 :            :         /* Read device(host) feature bits */
    1114                 :          0 :         host_features = VIRTIO_OPS(hw)->get_features(hw);
    1115                 :          0 :         PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64,
    1116                 :            :                 host_features);
    1117                 :            : 
    1118                 :            :         /* If supported, ensure MTU value is valid before acknowledging it. */
    1119         [ #  # ]:          0 :         if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) {
    1120                 :            :                 struct virtio_net_config config;
    1121                 :            : 
    1122                 :          0 :                 virtio_read_dev_config(hw,
    1123                 :            :                         offsetof(struct virtio_net_config, mtu),
    1124                 :            :                         &config.mtu, sizeof(config.mtu));
    1125                 :            : 
    1126         [ #  # ]:          0 :                 if (config.mtu < RTE_ETHER_MIN_MTU)
    1127                 :          0 :                         req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
    1128                 :            :         }
    1129                 :            : 
    1130                 :            :         /*
    1131                 :            :          * Negotiate features: Subset of device feature bits are written back
    1132                 :            :          * guest feature bits.
    1133                 :            :          */
    1134                 :          0 :         hw->guest_features = req_features;
    1135                 :          0 :         hw->guest_features = virtio_negotiate_features(hw, host_features);
    1136                 :          0 :         PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64,
    1137                 :            :                 hw->guest_features);
    1138                 :            : 
    1139         [ #  # ]:          0 :         if (VIRTIO_OPS(hw)->features_ok(hw) < 0)
    1140                 :            :                 return -1;
    1141                 :            : 
    1142         [ #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_F_VERSION_1)) {
    1143                 :          0 :                 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
    1144                 :            : 
    1145         [ #  # ]:          0 :                 if (!(virtio_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
    1146                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to set FEATURES_OK status!");
    1147                 :          0 :                         return -1;
    1148                 :            :                 }
    1149                 :            :         }
    1150                 :            : 
    1151                 :          0 :         hw->req_guest_features = req_features;
    1152                 :            : 
    1153                 :          0 :         return 0;
    1154                 :            : }
    1155                 :            : 
    1156                 :            : static void
    1157                 :          0 : virtio_notify_peers(struct rte_eth_dev *dev)
    1158                 :            : {
    1159                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1160                 :            :         struct virtnet_rx *rxvq;
    1161                 :            :         struct rte_mbuf *rarp_mbuf;
    1162                 :            : 
    1163         [ #  # ]:          0 :         if (!dev->data->rx_queues)
    1164                 :          0 :                 return;
    1165                 :            : 
    1166                 :          0 :         rxvq = dev->data->rx_queues[0];
    1167         [ #  # ]:          0 :         if (!rxvq)
    1168                 :            :                 return;
    1169                 :            : 
    1170                 :          0 :         rarp_mbuf = rte_net_make_rarp_packet(rxvq->mpool,
    1171                 :          0 :                         (struct rte_ether_addr *)hw->mac_addr);
    1172         [ #  # ]:          0 :         if (rarp_mbuf == NULL) {
    1173                 :          0 :                 PMD_DRV_LOG(ERR, "failed to make RARP packet.");
    1174                 :          0 :                 return;
    1175                 :            :         }
    1176                 :            : 
    1177                 :          0 :         rte_spinlock_lock(&hw->state_lock);
    1178         [ #  # ]:          0 :         if (hw->started == 0) {
    1179                 :            :                 /* If virtio port just stopped, no need to send RARP */
    1180                 :          0 :                 rte_pktmbuf_free(rarp_mbuf);
    1181                 :          0 :                 goto out;
    1182                 :            :         }
    1183                 :          0 :         hw->started = 0;
    1184                 :            : 
    1185                 :            :         /*
    1186                 :            :          * Prevent the worker threads from touching queues to avoid contention,
    1187                 :            :          * 1 ms should be enough for the ongoing Tx function to finish.
    1188                 :            :          */
    1189                 :            :         rte_delay_ms(1);
    1190                 :            : 
    1191                 :          0 :         hw->inject_pkts = &rarp_mbuf;
    1192                 :          0 :         dev->tx_pkt_burst(dev->data->tx_queues[0], &rarp_mbuf, 1);
    1193                 :          0 :         hw->inject_pkts = NULL;
    1194                 :            : 
    1195                 :          0 :         hw->started = 1;
    1196                 :            : 
    1197                 :          0 : out:
    1198                 :            :         rte_spinlock_unlock(&hw->state_lock);
    1199                 :            : }
    1200                 :            : 
    1201                 :            : static void
    1202                 :          0 : virtio_ack_link_announce(struct rte_eth_dev *dev)
    1203                 :            : {
    1204                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1205                 :            :         struct virtio_pmd_ctrl ctrl;
    1206                 :            : 
    1207                 :          0 :         ctrl.hdr.class = VIRTIO_NET_CTRL_ANNOUNCE;
    1208                 :          0 :         ctrl.hdr.cmd = VIRTIO_NET_CTRL_ANNOUNCE_ACK;
    1209                 :            : 
    1210                 :          0 :         virtio_send_command(hw->cvq, &ctrl, NULL, 0);
    1211                 :          0 : }
    1212                 :            : 
    1213                 :            : /*
    1214                 :            :  * Process virtio config changed interrupt. Call the callback
    1215                 :            :  * if link state changed, generate gratuitous RARP packet if
    1216                 :            :  * the status indicates an ANNOUNCE.
    1217                 :            :  */
    1218                 :            : void
    1219                 :          0 : virtio_interrupt_handler(void *param)
    1220                 :            : {
    1221                 :            :         struct rte_eth_dev *dev = param;
    1222                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1223                 :            :         uint8_t isr;
    1224                 :            :         uint16_t status;
    1225                 :            : 
    1226                 :            :         /* Read interrupt status which clears interrupt */
    1227                 :          0 :         isr = virtio_get_isr(hw);
    1228                 :          0 :         PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
    1229                 :            : 
    1230         [ #  # ]:          0 :         if (virtio_intr_unmask(dev) < 0)
    1231                 :          0 :                 PMD_DRV_LOG(ERR, "interrupt enable failed");
    1232                 :            : 
    1233         [ #  # ]:          0 :         if (isr & VIRTIO_ISR_CONFIG) {
    1234         [ #  # ]:          0 :                 if (virtio_dev_link_update(dev, 0) == 0)
    1235                 :          0 :                         rte_eth_dev_callback_process(dev,
    1236                 :            :                                                      RTE_ETH_EVENT_INTR_LSC,
    1237                 :            :                                                      NULL);
    1238                 :            : 
    1239         [ #  # ]:          0 :                 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
    1240                 :          0 :                         virtio_read_dev_config(hw,
    1241                 :            :                                 offsetof(struct virtio_net_config, status),
    1242                 :            :                                 &status, sizeof(status));
    1243         [ #  # ]:          0 :                         if (status & VIRTIO_NET_S_ANNOUNCE) {
    1244                 :          0 :                                 virtio_notify_peers(dev);
    1245         [ #  # ]:          0 :                                 if (hw->cvq)
    1246                 :          0 :                                         virtio_ack_link_announce(dev);
    1247                 :            :                         }
    1248                 :            :                 }
    1249                 :            :         }
    1250                 :          0 : }
    1251                 :            : 
    1252                 :            : static const struct {
    1253                 :            :         eth_tx_burst_t pkt_burst;
    1254                 :            :         const char *info;
    1255                 :            : } virtio_tx_burst_info[] = {
    1256                 :            :         {       virtio_xmit_pkts, "Scalar"},
    1257                 :            :         {       virtio_xmit_pkts_packed, "Scalar packed ring"},
    1258                 :            :         {       virtio_xmit_pkts_inorder, "Scalar in order"},
    1259                 :            :         {       virtio_xmit_pkts_packed_vec, "Vector packed ring"},
    1260                 :            : };
    1261                 :            : 
    1262                 :            : static int
    1263                 :          0 : virtio_tx_burst_mode_get(struct rte_eth_dev *dev,
    1264                 :            :                                 __rte_unused uint16_t queue_id,
    1265                 :            :                                 struct rte_eth_burst_mode *mode)
    1266                 :            : {
    1267                 :          0 :         eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
    1268                 :            :         size_t i;
    1269                 :            : 
    1270         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(virtio_tx_burst_info); i++) {
    1271         [ #  # ]:          0 :                 if (pkt_burst == virtio_tx_burst_info[i].pkt_burst) {
    1272                 :          0 :                         snprintf(mode->info, sizeof(mode->info), "%s",
    1273                 :          0 :                                  virtio_tx_burst_info[i].info);
    1274                 :          0 :                         return 0;
    1275                 :            :                 }
    1276                 :            :         }
    1277                 :            : 
    1278                 :            :         return -EINVAL;
    1279                 :            : }
    1280                 :            : 
    1281                 :            : static const struct {
    1282                 :            :         eth_rx_burst_t pkt_burst;
    1283                 :            :         const char *info;
    1284                 :            : } virtio_rx_burst_info[] = {
    1285                 :            :         {       virtio_recv_pkts, "Scalar"},
    1286                 :            :         {       virtio_recv_pkts_packed, "Scalar standard packed ring"},
    1287                 :            :         {       virtio_recv_pkts_inorder, "Scalar"},
    1288                 :            :         {       virtio_recv_mergeable_pkts, "Scalar mergeable"},
    1289                 :            :         {       virtio_recv_mergeable_pkts_packed, "Scalar mergeable packed ring"},
    1290                 :            : #ifdef RTE_ARCH_x86
    1291                 :            :         {       virtio_recv_pkts_vec, "Vector SSE"},
    1292                 :            :         {       virtio_recv_pkts_packed_vec, "Vector AVX512 packed ring"},
    1293                 :            : #elif defined(RTE_ARCH_ARM)
    1294                 :            :         {       virtio_recv_pkts_vec, "Vector NEON"},
    1295                 :            :         {       virtio_recv_pkts_packed_vec, "Vector NEON packed ring"},
    1296                 :            : #elif defined(RTE_ARCH_PPC_64)
    1297                 :            :         {       virtio_recv_pkts_vec, "Vector Altivec"},
    1298                 :            :         {       virtio_recv_pkts_packed_vec, "Vector Altivec packed ring"},
    1299                 :            : #endif
    1300                 :            : };
    1301                 :            : 
    1302                 :            : static int
    1303                 :          0 : virtio_rx_burst_mode_get(struct rte_eth_dev *dev,
    1304                 :            :                                 __rte_unused uint16_t queue_id,
    1305                 :            :                                 struct rte_eth_burst_mode *mode)
    1306                 :            : {
    1307                 :          0 :         eth_tx_burst_t pkt_burst = dev->rx_pkt_burst;
    1308                 :            :         size_t i;
    1309                 :            : 
    1310         [ #  # ]:          0 :         for (i = 0; i < RTE_DIM(virtio_rx_burst_info); i++) {
    1311         [ #  # ]:          0 :                 if (pkt_burst == virtio_rx_burst_info[i].pkt_burst) {
    1312                 :          0 :                         snprintf(mode->info, sizeof(mode->info), "%s",
    1313                 :          0 :                                  virtio_rx_burst_info[i].info);
    1314                 :          0 :                         return 0;
    1315                 :            :                 }
    1316                 :            :         }
    1317                 :            : 
    1318                 :            :         return -EINVAL;
    1319                 :            : }
    1320                 :            : 
    1321                 :            : /* set rx and tx handlers according to what is supported */
    1322                 :            : static void
    1323                 :          0 : set_rxtx_funcs(struct rte_eth_dev *eth_dev)
    1324                 :            : {
    1325                 :          0 :         struct virtio_hw *hw = eth_dev->data->dev_private;
    1326                 :            : 
    1327         [ #  # ]:          0 :         eth_dev->tx_pkt_prepare = virtio_xmit_pkts_prepare;
    1328         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw)) {
    1329         [ #  # ]:          0 :                 PMD_INIT_LOG(INFO,
    1330                 :            :                         "virtio: using packed ring %s Tx path on port %u",
    1331                 :            :                         hw->use_vec_tx ? "vectorized" : "standard",
    1332                 :            :                         eth_dev->data->port_id);
    1333         [ #  # ]:          0 :                 if (hw->use_vec_tx)
    1334                 :          0 :                         eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed_vec;
    1335                 :            :                 else
    1336                 :          0 :                         eth_dev->tx_pkt_burst = virtio_xmit_pkts_packed;
    1337                 :            :         } else {
    1338         [ #  # ]:          0 :                 if (hw->use_inorder_tx) {
    1339                 :          0 :                         PMD_INIT_LOG(INFO, "virtio: using inorder Tx path on port %u",
    1340                 :            :                                 eth_dev->data->port_id);
    1341                 :          0 :                         eth_dev->tx_pkt_burst = virtio_xmit_pkts_inorder;
    1342                 :            :                 } else {
    1343                 :          0 :                         PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u",
    1344                 :            :                                 eth_dev->data->port_id);
    1345                 :          0 :                         eth_dev->tx_pkt_burst = virtio_xmit_pkts;
    1346                 :            :                 }
    1347                 :            :         }
    1348                 :            : 
    1349         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw)) {
    1350         [ #  # ]:          0 :                 if (hw->use_vec_rx) {
    1351                 :          0 :                         PMD_INIT_LOG(INFO,
    1352                 :            :                                 "virtio: using packed ring vectorized Rx path on port %u",
    1353                 :            :                                 eth_dev->data->port_id);
    1354                 :          0 :                         eth_dev->rx_pkt_burst =
    1355                 :            :                                 &virtio_recv_pkts_packed_vec;
    1356         [ #  # ]:          0 :                 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
    1357                 :          0 :                         PMD_INIT_LOG(INFO,
    1358                 :            :                                 "virtio: using packed ring mergeable buffer Rx path on port %u",
    1359                 :            :                                 eth_dev->data->port_id);
    1360                 :          0 :                         eth_dev->rx_pkt_burst =
    1361                 :            :                                 &virtio_recv_mergeable_pkts_packed;
    1362                 :            :                 } else {
    1363                 :          0 :                         PMD_INIT_LOG(INFO,
    1364                 :            :                                 "virtio: using packed ring standard Rx path on port %u",
    1365                 :            :                                 eth_dev->data->port_id);
    1366                 :          0 :                         eth_dev->rx_pkt_burst = &virtio_recv_pkts_packed;
    1367                 :            :                 }
    1368                 :            :         } else {
    1369         [ #  # ]:          0 :                 if (hw->use_vec_rx) {
    1370                 :          0 :                         PMD_INIT_LOG(INFO, "virtio: using vectorized Rx path on port %u",
    1371                 :            :                                 eth_dev->data->port_id);
    1372                 :          0 :                         eth_dev->rx_pkt_burst = virtio_recv_pkts_vec;
    1373         [ #  # ]:          0 :                 } else if (hw->use_inorder_rx) {
    1374                 :          0 :                         PMD_INIT_LOG(INFO,
    1375                 :            :                                 "virtio: using inorder Rx path on port %u",
    1376                 :            :                                 eth_dev->data->port_id);
    1377                 :          0 :                         eth_dev->rx_pkt_burst =      &virtio_recv_pkts_inorder;
    1378         [ #  # ]:          0 :                 } else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
    1379                 :          0 :                         PMD_INIT_LOG(INFO,
    1380                 :            :                                 "virtio: using mergeable buffer Rx path on port %u",
    1381                 :            :                                 eth_dev->data->port_id);
    1382                 :          0 :                         eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts;
    1383                 :            :                 } else {
    1384                 :          0 :                         PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u",
    1385                 :            :                                 eth_dev->data->port_id);
    1386                 :          0 :                         eth_dev->rx_pkt_burst = &virtio_recv_pkts;
    1387                 :            :                 }
    1388                 :            :         }
    1389                 :            : 
    1390                 :          0 : }
    1391                 :            : 
    1392                 :            : /* Only support 1:1 queue/interrupt mapping so far.
    1393                 :            :  * TODO: support n:1 queue/interrupt mapping when there are limited number of
    1394                 :            :  * interrupt vectors (<N+1).
    1395                 :            :  */
    1396                 :            : static int
    1397                 :          0 : virtio_queues_bind_intr(struct rte_eth_dev *dev)
    1398                 :            : {
    1399                 :            :         uint32_t i;
    1400                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1401                 :            : 
    1402                 :          0 :         PMD_INIT_LOG(INFO, "queue/interrupt binding");
    1403         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; ++i) {
    1404         [ #  # ]:          0 :                 if (rte_intr_vec_list_index_set(dev->intr_handle, i,
    1405                 :          0 :                                                        i + 1))
    1406                 :          0 :                         return -rte_errno;
    1407         [ #  # ]:          0 :                 if (VIRTIO_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) ==
    1408                 :            :                                                  VIRTIO_MSI_NO_VECTOR) {
    1409                 :          0 :                         PMD_DRV_LOG(ERR, "failed to set queue vector");
    1410                 :          0 :                         return -EBUSY;
    1411                 :            :                 }
    1412                 :            :         }
    1413                 :            : 
    1414                 :            :         return 0;
    1415                 :            : }
    1416                 :            : 
    1417                 :            : static void
    1418                 :          0 : virtio_queues_unbind_intr(struct rte_eth_dev *dev)
    1419                 :            : {
    1420                 :            :         uint32_t i;
    1421                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1422                 :            : 
    1423                 :          0 :         PMD_INIT_LOG(INFO, "queue/interrupt unbinding");
    1424         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; ++i)
    1425                 :          0 :                 VIRTIO_OPS(hw)->set_queue_irq(hw,
    1426                 :          0 :                                              hw->vqs[i * VTNET_CQ],
    1427                 :            :                                              VIRTIO_MSI_NO_VECTOR);
    1428                 :          0 : }
    1429                 :            : 
    1430                 :            : static int
    1431                 :          0 : virtio_configure_intr(struct rte_eth_dev *dev)
    1432                 :            : {
    1433                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1434                 :            :         int ret;
    1435                 :            : 
    1436         [ #  # ]:          0 :         if (!rte_intr_cap_multiple(dev->intr_handle)) {
    1437                 :          0 :                 PMD_INIT_LOG(ERR, "Multiple intr vector not supported");
    1438                 :          0 :                 return -ENOTSUP;
    1439                 :            :         }
    1440                 :            : 
    1441                 :          0 :         ret = rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues);
    1442         [ #  # ]:          0 :         if (ret < 0) {
    1443                 :          0 :                 PMD_INIT_LOG(ERR, "Fail to create eventfd");
    1444                 :          0 :                 return ret;
    1445                 :            :         }
    1446                 :            : 
    1447                 :          0 :         ret = rte_intr_vec_list_alloc(dev->intr_handle, "intr_vec",
    1448                 :          0 :                                       hw->max_queue_pairs);
    1449         [ #  # ]:          0 :         if (ret < 0) {
    1450                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors",
    1451                 :            :                              hw->max_queue_pairs);
    1452                 :          0 :                 return ret;
    1453                 :            :         }
    1454                 :            : 
    1455         [ #  # ]:          0 :         if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
    1456                 :            :                 /* Re-register callback to update max_intr */
    1457                 :          0 :                 rte_intr_callback_unregister(dev->intr_handle,
    1458                 :            :                                              virtio_interrupt_handler,
    1459                 :            :                                              dev);
    1460                 :          0 :                 rte_intr_callback_register(dev->intr_handle,
    1461                 :            :                                            virtio_interrupt_handler,
    1462                 :            :                                            dev);
    1463                 :            :         }
    1464                 :            : 
    1465                 :            :         /* DO NOT try to remove this! This function will enable msix, or QEMU
    1466                 :            :          * will encounter SIGSEGV when DRIVER_OK is sent.
    1467                 :            :          * And for legacy devices, this should be done before queue/vec binding
    1468                 :            :          * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR
    1469                 :            :          * (22) will be ignored.
    1470                 :            :          */
    1471         [ #  # ]:          0 :         if (virtio_intr_enable(dev) < 0) {
    1472                 :          0 :                 PMD_DRV_LOG(ERR, "interrupt enable failed");
    1473                 :          0 :                 return -EINVAL;
    1474                 :            :         }
    1475                 :            : 
    1476                 :          0 :         ret = virtio_queues_bind_intr(dev);
    1477         [ #  # ]:          0 :         if (ret < 0) {
    1478                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt");
    1479                 :          0 :                 return ret;
    1480                 :            :         }
    1481                 :            : 
    1482                 :            :         return 0;
    1483                 :            : }
    1484                 :            : 
    1485                 :            : static void
    1486                 :          0 : virtio_get_speed_duplex(struct rte_eth_dev *eth_dev,
    1487                 :            :                         struct rte_eth_link *link)
    1488                 :            : {
    1489                 :          0 :         struct virtio_hw *hw = eth_dev->data->dev_private;
    1490                 :            :         struct virtio_net_config *config;
    1491                 :            :         struct virtio_net_config local_config;
    1492                 :            : 
    1493                 :            :         config = &local_config;
    1494                 :          0 :         virtio_read_dev_config(hw,
    1495                 :            :                 offsetof(struct virtio_net_config, speed),
    1496                 :            :                 &config->speed, sizeof(config->speed));
    1497                 :          0 :         virtio_read_dev_config(hw,
    1498                 :            :                 offsetof(struct virtio_net_config, duplex),
    1499                 :            :                 &config->duplex, sizeof(config->duplex));
    1500                 :          0 :         hw->speed = config->speed;
    1501                 :          0 :         hw->duplex = config->duplex;
    1502         [ #  # ]:          0 :         if (link != NULL) {
    1503                 :          0 :                 link->link_duplex = hw->duplex;
    1504                 :          0 :                 link->link_speed  = hw->speed;
    1505                 :            :         }
    1506                 :          0 :         PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
    1507                 :            :                      hw->speed, hw->duplex);
    1508                 :          0 : }
    1509                 :            : 
    1510                 :            : static uint64_t
    1511                 :          0 : ethdev_to_virtio_rss_offloads(uint64_t ethdev_hash_types)
    1512                 :            : {
    1513                 :            :         uint64_t virtio_hash_types = 0;
    1514                 :            : 
    1515         [ #  # ]:          0 :         if (ethdev_hash_types & (RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_FRAG_IPV4 |
    1516                 :            :                                 RTE_ETH_RSS_NONFRAG_IPV4_OTHER))
    1517                 :            :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_IPV4;
    1518                 :            : 
    1519         [ #  # ]:          0 :         if (ethdev_hash_types & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
    1520                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_TCPV4;
    1521                 :            : 
    1522         [ #  # ]:          0 :         if (ethdev_hash_types & RTE_ETH_RSS_NONFRAG_IPV4_UDP)
    1523                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_UDPV4;
    1524                 :            : 
    1525         [ #  # ]:          0 :         if (ethdev_hash_types & (RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_FRAG_IPV6 |
    1526                 :            :                                 RTE_ETH_RSS_NONFRAG_IPV6_OTHER))
    1527                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_IPV6;
    1528                 :            : 
    1529         [ #  # ]:          0 :         if (ethdev_hash_types & RTE_ETH_RSS_NONFRAG_IPV6_TCP)
    1530                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_TCPV6;
    1531                 :            : 
    1532         [ #  # ]:          0 :         if (ethdev_hash_types & RTE_ETH_RSS_NONFRAG_IPV6_UDP)
    1533                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_UDPV6;
    1534                 :            : 
    1535         [ #  # ]:          0 :         if (ethdev_hash_types & RTE_ETH_RSS_IPV6_EX)
    1536                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_IP_EX;
    1537                 :            : 
    1538         [ #  # ]:          0 :         if (ethdev_hash_types & RTE_ETH_RSS_IPV6_TCP_EX)
    1539                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_TCP_EX;
    1540                 :            : 
    1541         [ #  # ]:          0 :         if (ethdev_hash_types & RTE_ETH_RSS_IPV6_UDP_EX)
    1542                 :          0 :                 virtio_hash_types |= VIRTIO_NET_HASH_TYPE_UDP_EX;
    1543                 :            : 
    1544                 :          0 :         return virtio_hash_types;
    1545                 :            : }
    1546                 :            : 
    1547                 :            : static uint64_t
    1548                 :          0 : virtio_to_ethdev_rss_offloads(uint64_t virtio_hash_types)
    1549                 :            : {
    1550                 :            :         uint64_t rss_offloads = 0;
    1551                 :            : 
    1552         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_IPV4)
    1553                 :            :                 rss_offloads |= RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_FRAG_IPV4 |
    1554                 :            :                         RTE_ETH_RSS_NONFRAG_IPV4_OTHER;
    1555                 :            : 
    1556         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_TCPV4)
    1557                 :          0 :                 rss_offloads |= RTE_ETH_RSS_NONFRAG_IPV4_TCP;
    1558                 :            : 
    1559         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_UDPV4)
    1560                 :          0 :                 rss_offloads |= RTE_ETH_RSS_NONFRAG_IPV4_UDP;
    1561                 :            : 
    1562         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_IPV6)
    1563                 :          0 :                 rss_offloads |= RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_FRAG_IPV6 |
    1564                 :            :                         RTE_ETH_RSS_NONFRAG_IPV6_OTHER;
    1565                 :            : 
    1566         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_TCPV6)
    1567                 :          0 :                 rss_offloads |= RTE_ETH_RSS_NONFRAG_IPV6_TCP;
    1568                 :            : 
    1569         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_UDPV6)
    1570                 :          0 :                 rss_offloads |= RTE_ETH_RSS_NONFRAG_IPV6_UDP;
    1571                 :            : 
    1572         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_IP_EX)
    1573                 :          0 :                 rss_offloads |= RTE_ETH_RSS_IPV6_EX;
    1574                 :            : 
    1575         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_TCP_EX)
    1576                 :          0 :                 rss_offloads |= RTE_ETH_RSS_IPV6_TCP_EX;
    1577                 :            : 
    1578         [ #  # ]:          0 :         if (virtio_hash_types & VIRTIO_NET_HASH_TYPE_UDP_EX)
    1579                 :          0 :                 rss_offloads |= RTE_ETH_RSS_IPV6_UDP_EX;
    1580                 :            : 
    1581                 :          0 :         return rss_offloads;
    1582                 :            : }
    1583                 :            : 
    1584                 :            : static int
    1585                 :          0 : virtio_dev_get_rss_config(struct virtio_hw *hw, uint32_t *rss_hash_types)
    1586                 :            : {
    1587                 :            :         struct virtio_net_config local_config;
    1588                 :            :         struct virtio_net_config *config = &local_config;
    1589                 :            : 
    1590                 :          0 :         virtio_read_dev_config(hw,
    1591                 :            :                         offsetof(struct virtio_net_config, rss_max_key_size),
    1592                 :            :                         &config->rss_max_key_size,
    1593                 :            :                         sizeof(config->rss_max_key_size));
    1594         [ #  # ]:          0 :         if (config->rss_max_key_size < VIRTIO_NET_RSS_KEY_SIZE) {
    1595                 :          0 :                 PMD_INIT_LOG(ERR, "Invalid device RSS max key size (%u)",
    1596                 :            :                                 config->rss_max_key_size);
    1597                 :          0 :                 return -EINVAL;
    1598                 :            :         }
    1599                 :            : 
    1600                 :          0 :         virtio_read_dev_config(hw,
    1601                 :            :                         offsetof(struct virtio_net_config,
    1602                 :            :                                 rss_max_indirection_table_length),
    1603                 :            :                         &config->rss_max_indirection_table_length,
    1604                 :            :                         sizeof(config->rss_max_indirection_table_length));
    1605         [ #  # ]:          0 :         if (config->rss_max_indirection_table_length < VIRTIO_NET_RSS_RETA_SIZE) {
    1606                 :          0 :                 PMD_INIT_LOG(ERR, "Invalid device RSS max reta size (%u)",
    1607                 :            :                                 config->rss_max_indirection_table_length);
    1608                 :          0 :                 return -EINVAL;
    1609                 :            :         }
    1610                 :            : 
    1611                 :          0 :         virtio_read_dev_config(hw,
    1612                 :            :                         offsetof(struct virtio_net_config, supported_hash_types),
    1613                 :            :                         &config->supported_hash_types,
    1614                 :            :                         sizeof(config->supported_hash_types));
    1615         [ #  # ]:          0 :         if ((config->supported_hash_types & VIRTIO_NET_HASH_TYPE_MASK) == 0) {
    1616                 :          0 :                 PMD_INIT_LOG(ERR, "Invalid device RSS hash types (0x%x)",
    1617                 :            :                                 config->supported_hash_types);
    1618                 :          0 :                 return -EINVAL;
    1619                 :            :         }
    1620                 :            : 
    1621                 :          0 :         *rss_hash_types = config->supported_hash_types & VIRTIO_NET_HASH_TYPE_MASK;
    1622                 :            : 
    1623                 :          0 :         PMD_INIT_LOG(DEBUG, "Device RSS config:");
    1624                 :          0 :         PMD_INIT_LOG(DEBUG, "\t-Max key size: %u", config->rss_max_key_size);
    1625                 :          0 :         PMD_INIT_LOG(DEBUG, "\t-Max reta size: %u", config->rss_max_indirection_table_length);
    1626                 :          0 :         PMD_INIT_LOG(DEBUG, "\t-Supported hash types: 0x%x", *rss_hash_types);
    1627                 :            : 
    1628                 :          0 :         return 0;
    1629                 :            : }
    1630                 :            : 
    1631                 :            : static int
    1632                 :          0 : virtio_dev_rss_hash_update(struct rte_eth_dev *dev,
    1633                 :            :                 struct rte_eth_rss_conf *rss_conf)
    1634                 :            : {
    1635         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1636                 :            :         char old_rss_key[VIRTIO_NET_RSS_KEY_SIZE];
    1637                 :            :         uint32_t old_hash_types;
    1638                 :            :         uint16_t nb_queues;
    1639                 :            :         int ret;
    1640                 :            : 
    1641         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_RSS))
    1642                 :            :                 return -ENOTSUP;
    1643                 :            : 
    1644         [ #  # ]:          0 :         if (rss_conf->rss_hf & ~virtio_to_ethdev_rss_offloads(VIRTIO_NET_HASH_TYPE_MASK))
    1645                 :            :                 return -EINVAL;
    1646                 :            : 
    1647                 :          0 :         old_hash_types = hw->rss_hash_types;
    1648                 :          0 :         hw->rss_hash_types = ethdev_to_virtio_rss_offloads(rss_conf->rss_hf);
    1649                 :            : 
    1650   [ #  #  #  # ]:          0 :         if (rss_conf->rss_key && rss_conf->rss_key_len) {
    1651         [ #  # ]:          0 :                 if (rss_conf->rss_key_len != VIRTIO_NET_RSS_KEY_SIZE) {
    1652                 :          0 :                         PMD_INIT_LOG(ERR, "Driver only supports %u RSS key length",
    1653                 :            :                                         VIRTIO_NET_RSS_KEY_SIZE);
    1654                 :            :                         ret = -EINVAL;
    1655                 :          0 :                         goto restore_types;
    1656                 :            :                 }
    1657                 :          0 :                 memcpy(old_rss_key, hw->rss_key, VIRTIO_NET_RSS_KEY_SIZE);
    1658                 :            :                 memcpy(hw->rss_key, rss_conf->rss_key, VIRTIO_NET_RSS_KEY_SIZE);
    1659                 :            :         }
    1660                 :            : 
    1661                 :          0 :         nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
    1662                 :          0 :         ret = virtio_set_multiple_queues_rss(dev, nb_queues);
    1663         [ #  # ]:          0 :         if (ret < 0) {
    1664                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to apply new RSS config to the device");
    1665                 :          0 :                 goto restore_key;
    1666                 :            :         }
    1667                 :            : 
    1668                 :            :         return 0;
    1669                 :            : restore_key:
    1670   [ #  #  #  # ]:          0 :         if (rss_conf->rss_key && rss_conf->rss_key_len)
    1671                 :          0 :                 memcpy(hw->rss_key, old_rss_key, VIRTIO_NET_RSS_KEY_SIZE);
    1672                 :          0 : restore_types:
    1673                 :          0 :         hw->rss_hash_types = old_hash_types;
    1674                 :            : 
    1675                 :          0 :         return ret;
    1676                 :            : }
    1677                 :            : 
    1678                 :            : static int
    1679                 :          0 : virtio_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
    1680                 :            :                 struct rte_eth_rss_conf *rss_conf)
    1681                 :            : {
    1682         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1683                 :            : 
    1684         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_RSS))
    1685                 :            :                 return -ENOTSUP;
    1686                 :            : 
    1687   [ #  #  #  # ]:          0 :         if (rss_conf->rss_key && rss_conf->rss_key_len >= VIRTIO_NET_RSS_KEY_SIZE)
    1688                 :          0 :                 memcpy(rss_conf->rss_key, hw->rss_key, VIRTIO_NET_RSS_KEY_SIZE);
    1689                 :          0 :         rss_conf->rss_key_len = VIRTIO_NET_RSS_KEY_SIZE;
    1690                 :          0 :         rss_conf->rss_hf = virtio_to_ethdev_rss_offloads(hw->rss_hash_types);
    1691                 :            : 
    1692                 :          0 :         return 0;
    1693                 :            : }
    1694                 :            : 
    1695                 :          0 : static int virtio_dev_rss_reta_update(struct rte_eth_dev *dev,
    1696                 :            :                          struct rte_eth_rss_reta_entry64 *reta_conf,
    1697                 :            :                          uint16_t reta_size)
    1698                 :            : {
    1699         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1700                 :            :         uint16_t nb_queues;
    1701                 :            :         uint16_t old_reta[VIRTIO_NET_RSS_RETA_SIZE];
    1702                 :            :         int idx, pos, i, ret;
    1703                 :            : 
    1704         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_RSS))
    1705                 :            :                 return -ENOTSUP;
    1706                 :            : 
    1707         [ #  # ]:          0 :         if (reta_size != VIRTIO_NET_RSS_RETA_SIZE)
    1708                 :            :                 return -EINVAL;
    1709                 :            : 
    1710                 :          0 :         memcpy(old_reta, hw->rss_reta, sizeof(old_reta));
    1711                 :            : 
    1712         [ #  # ]:          0 :         for (i = 0; i < reta_size; i++) {
    1713                 :          0 :                 idx = i / RTE_ETH_RETA_GROUP_SIZE;
    1714                 :          0 :                 pos = i % RTE_ETH_RETA_GROUP_SIZE;
    1715                 :            : 
    1716         [ #  # ]:          0 :                 if (((reta_conf[idx].mask >> pos) & 0x1) == 0)
    1717                 :          0 :                         continue;
    1718                 :            : 
    1719                 :          0 :                 hw->rss_reta[i] = reta_conf[idx].reta[pos];
    1720                 :            :         }
    1721                 :            : 
    1722                 :          0 :         nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
    1723                 :          0 :         ret = virtio_set_multiple_queues_rss(dev, nb_queues);
    1724         [ #  # ]:          0 :         if (ret < 0) {
    1725                 :          0 :                 PMD_INIT_LOG(ERR, "Failed to apply new RETA to the device");
    1726                 :          0 :                 memcpy(hw->rss_reta, old_reta, sizeof(old_reta));
    1727                 :            :         }
    1728                 :            : 
    1729                 :          0 :         hw->rss_rx_queues = dev->data->nb_rx_queues;
    1730                 :            : 
    1731                 :          0 :         return ret;
    1732                 :            : }
    1733                 :            : 
    1734                 :          0 : static int virtio_dev_rss_reta_query(struct rte_eth_dev *dev,
    1735                 :            :                          struct rte_eth_rss_reta_entry64 *reta_conf,
    1736                 :            :                          uint16_t reta_size)
    1737                 :            : {
    1738         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    1739                 :            :         int idx, i;
    1740                 :            : 
    1741         [ #  # ]:          0 :         if (!virtio_with_feature(hw, VIRTIO_NET_F_RSS))
    1742                 :            :                 return -ENOTSUP;
    1743                 :            : 
    1744         [ #  # ]:          0 :         if (reta_size != VIRTIO_NET_RSS_RETA_SIZE)
    1745                 :            :                 return -EINVAL;
    1746                 :            : 
    1747         [ #  # ]:          0 :         for (i = 0; i < reta_size; i++) {
    1748                 :          0 :                 idx = i / RTE_ETH_RETA_GROUP_SIZE;
    1749                 :          0 :                 reta_conf[idx].reta[i % RTE_ETH_RETA_GROUP_SIZE] = hw->rss_reta[i];
    1750                 :            :         }
    1751                 :            : 
    1752                 :            :         return 0;
    1753                 :            : }
    1754                 :            : 
    1755                 :            : /*
    1756                 :            :  * As default RSS hash key, it uses the default key of the
    1757                 :            :  * Intel IXGBE devices. It can be updated by the application
    1758                 :            :  * with any 40B key value.
    1759                 :            :  */
    1760                 :            : static uint8_t rss_intel_key[VIRTIO_NET_RSS_KEY_SIZE] = {
    1761                 :            :         0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
    1762                 :            :         0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
    1763                 :            :         0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
    1764                 :            :         0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
    1765                 :            :         0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA,
    1766                 :            : };
    1767                 :            : 
    1768                 :            : static int
    1769                 :          0 : virtio_dev_rss_init(struct rte_eth_dev *eth_dev)
    1770                 :            : {
    1771                 :          0 :         struct virtio_hw *hw = eth_dev->data->dev_private;
    1772                 :          0 :         uint16_t nb_rx_queues = eth_dev->data->nb_rx_queues;
    1773                 :            :         struct rte_eth_rss_conf *rss_conf;
    1774                 :            :         int ret, i;
    1775                 :            : 
    1776         [ #  # ]:          0 :         if (!nb_rx_queues) {
    1777                 :          0 :                 PMD_INIT_LOG(ERR, "Cannot init RSS if no Rx queues");
    1778                 :          0 :                 return -EINVAL;
    1779                 :            :         }
    1780                 :            : 
    1781                 :            :         rss_conf = &eth_dev->data->dev_conf.rx_adv_conf.rss_conf;
    1782                 :            : 
    1783                 :          0 :         ret = virtio_dev_get_rss_config(hw, &hw->rss_hash_types);
    1784         [ #  # ]:          0 :         if (ret)
    1785                 :            :                 return ret;
    1786                 :            : 
    1787         [ #  # ]:          0 :         if (rss_conf->rss_hf) {
    1788                 :            :                 /*  Ensure requested hash types are supported by the device */
    1789         [ #  # ]:          0 :                 if (rss_conf->rss_hf & ~virtio_to_ethdev_rss_offloads(hw->rss_hash_types))
    1790                 :            :                         return -EINVAL;
    1791                 :            : 
    1792                 :          0 :                 hw->rss_hash_types = ethdev_to_virtio_rss_offloads(rss_conf->rss_hf);
    1793                 :            :         }
    1794                 :            : 
    1795         [ #  # ]:          0 :         if (!hw->rss_key) {
    1796                 :            :                 /* Setup default RSS key if not already setup by the user */
    1797                 :          0 :                 hw->rss_key = rte_malloc_socket("rss_key",
    1798                 :            :                                 VIRTIO_NET_RSS_KEY_SIZE, 0,
    1799                 :          0 :                                 eth_dev->device->numa_node);
    1800         [ #  # ]:          0 :                 if (!hw->rss_key) {
    1801                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to allocate RSS key");
    1802                 :          0 :                         return -ENOMEM;
    1803                 :            :                 }
    1804                 :            :         }
    1805                 :            : 
    1806   [ #  #  #  # ]:          0 :         if (rss_conf->rss_key && rss_conf->rss_key_len) {
    1807         [ #  # ]:          0 :                 if (rss_conf->rss_key_len != VIRTIO_NET_RSS_KEY_SIZE) {
    1808                 :          0 :                         PMD_INIT_LOG(ERR, "Driver only supports %u RSS key length",
    1809                 :            :                                         VIRTIO_NET_RSS_KEY_SIZE);
    1810                 :          0 :                         return -EINVAL;
    1811                 :            :                 }
    1812                 :          0 :                 memcpy(hw->rss_key, rss_conf->rss_key, VIRTIO_NET_RSS_KEY_SIZE);
    1813                 :            :         } else {
    1814                 :          0 :                 memcpy(hw->rss_key, rss_intel_key, VIRTIO_NET_RSS_KEY_SIZE);
    1815                 :            :         }
    1816                 :            : 
    1817         [ #  # ]:          0 :         if (!hw->rss_reta) {
    1818                 :            :                 /* Setup default RSS reta if not already setup by the user */
    1819                 :          0 :                 hw->rss_reta = rte_zmalloc_socket("rss_reta",
    1820                 :            :                                 VIRTIO_NET_RSS_RETA_SIZE * sizeof(uint16_t), 0,
    1821                 :          0 :                                 eth_dev->device->numa_node);
    1822         [ #  # ]:          0 :                 if (!hw->rss_reta) {
    1823                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to allocate RSS reta");
    1824                 :          0 :                         return -ENOMEM;
    1825                 :            :                 }
    1826                 :            : 
    1827                 :          0 :                 hw->rss_rx_queues = 0;
    1828                 :            :         }
    1829                 :            : 
    1830                 :            :         /* Re-initialize the RSS reta if the number of RX queues has changed */
    1831         [ #  # ]:          0 :         if (hw->rss_rx_queues != nb_rx_queues) {
    1832         [ #  # ]:          0 :                 for (i = 0; i < VIRTIO_NET_RSS_RETA_SIZE; i++)
    1833                 :          0 :                         hw->rss_reta[i] = i % nb_rx_queues;
    1834                 :          0 :                 hw->rss_rx_queues = nb_rx_queues;
    1835                 :            :         }
    1836                 :            : 
    1837                 :            :         return 0;
    1838                 :            : }
    1839                 :            : 
    1840                 :            : #define DUPLEX_UNKNOWN   0xff
    1841                 :            : /* reset device and renegotiate features if needed */
    1842                 :            : static int
    1843                 :          0 : virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
    1844                 :            : {
    1845                 :          0 :         struct virtio_hw *hw = eth_dev->data->dev_private;
    1846                 :            :         struct virtio_net_config *config;
    1847                 :            :         struct virtio_net_config local_config;
    1848                 :            :         int ret;
    1849                 :            : 
    1850                 :            :         /* Reset the device although not necessary at startup */
    1851                 :          0 :         virtio_reset(hw);
    1852                 :            : 
    1853         [ #  # ]:          0 :         if (hw->vqs) {
    1854                 :          0 :                 virtio_dev_free_mbufs(eth_dev);
    1855                 :          0 :                 virtio_free_queues(hw);
    1856                 :            :         }
    1857                 :            : 
    1858                 :            :         /* Tell the host we've noticed this device. */
    1859                 :          0 :         virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
    1860                 :            : 
    1861                 :            :         /* Tell the host we've known how to drive the device. */
    1862                 :          0 :         virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
    1863         [ #  # ]:          0 :         if (virtio_ethdev_negotiate_features(hw, req_features) < 0)
    1864                 :            :                 return -EINVAL;
    1865                 :            : 
    1866                 :          0 :         hw->weak_barriers = !virtio_with_feature(hw, VIRTIO_F_ORDER_PLATFORM);
    1867                 :            : 
    1868                 :            :         /* If host does not support both status and MSI-X then disable LSC */
    1869   [ #  #  #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->intr_lsc)
    1870                 :          0 :                 eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
    1871                 :            :         else
    1872                 :          0 :                 eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC;
    1873                 :            : 
    1874                 :            :         /* Setting up rx_header size for the device */
    1875         [ #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_HASH_REPORT))
    1876                 :          0 :                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_hash_report);
    1877   [ #  #  #  # ]:          0 :         else if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) ||
    1878         [ #  # ]:          0 :             virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
    1879                 :            :             virtio_with_packed_queue(hw))
    1880                 :          0 :                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
    1881                 :            :         else
    1882                 :          0 :                 hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr);
    1883                 :            : 
    1884                 :            :         /* Copy the permanent MAC address to: virtio_hw */
    1885                 :          0 :         virtio_get_hwaddr(hw);
    1886                 :          0 :         rte_ether_addr_copy((struct rte_ether_addr *)hw->mac_addr,
    1887                 :          0 :                         &eth_dev->data->mac_addrs[0]);
    1888                 :          0 :         PMD_INIT_LOG(DEBUG,
    1889                 :            :                      "PORT MAC: " RTE_ETHER_ADDR_PRT_FMT,
    1890                 :            :                      hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2],
    1891                 :            :                      hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]);
    1892                 :            : 
    1893   [ #  #  #  # ]:          0 :         hw->get_speed_via_feat = hw->speed == RTE_ETH_SPEED_NUM_UNKNOWN &&
    1894                 :            :                              virtio_with_feature(hw, VIRTIO_NET_F_SPEED_DUPLEX);
    1895         [ #  # ]:          0 :         if (hw->get_speed_via_feat)
    1896                 :          0 :                 virtio_get_speed_duplex(eth_dev, NULL);
    1897         [ #  # ]:          0 :         if (hw->duplex == DUPLEX_UNKNOWN)
    1898                 :          0 :                 hw->duplex = RTE_ETH_LINK_FULL_DUPLEX;
    1899                 :          0 :         PMD_INIT_LOG(DEBUG, "link speed = %d, duplex = %d",
    1900                 :            :                 hw->speed, hw->duplex);
    1901         [ #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) {
    1902                 :            :                 config = &local_config;
    1903                 :            : 
    1904                 :          0 :                 virtio_read_dev_config(hw,
    1905                 :            :                         offsetof(struct virtio_net_config, mac),
    1906                 :            :                         &config->mac, sizeof(config->mac));
    1907                 :            : 
    1908         [ #  # ]:          0 :                 if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
    1909                 :          0 :                         virtio_read_dev_config(hw,
    1910                 :            :                                 offsetof(struct virtio_net_config, status),
    1911                 :            :                                 &config->status, sizeof(config->status));
    1912                 :            :                 } else {
    1913                 :          0 :                         PMD_INIT_LOG(DEBUG,
    1914                 :            :                                      "VIRTIO_NET_F_STATUS is not supported");
    1915                 :          0 :                         config->status = 0;
    1916                 :            :                 }
    1917                 :            : 
    1918   [ #  #  #  # ]:          0 :                 if (virtio_with_feature(hw, VIRTIO_NET_F_MQ) ||
    1919                 :            :                                 virtio_with_feature(hw, VIRTIO_NET_F_RSS)) {
    1920                 :          0 :                         virtio_read_dev_config(hw,
    1921                 :            :                                 offsetof(struct virtio_net_config, max_virtqueue_pairs),
    1922                 :            :                                 &config->max_virtqueue_pairs,
    1923                 :            :                                 sizeof(config->max_virtqueue_pairs));
    1924                 :            :                 } else {
    1925                 :          0 :                         PMD_INIT_LOG(DEBUG,
    1926                 :            :                                      "Neither VIRTIO_NET_F_MQ nor VIRTIO_NET_F_RSS are supported");
    1927                 :          0 :                         config->max_virtqueue_pairs = 1;
    1928                 :            :                 }
    1929                 :            : 
    1930         [ #  # ]:          0 :                 hw->max_queue_pairs = config->max_virtqueue_pairs;
    1931                 :            : 
    1932         [ #  # ]:          0 :                 if (virtio_with_feature(hw, VIRTIO_NET_F_MTU)) {
    1933                 :          0 :                         virtio_read_dev_config(hw,
    1934                 :            :                                 offsetof(struct virtio_net_config, mtu),
    1935                 :            :                                 &config->mtu,
    1936                 :            :                                 sizeof(config->mtu));
    1937                 :            : 
    1938                 :            :                         /*
    1939                 :            :                          * MTU value has already been checked at negotiation
    1940                 :            :                          * time, but check again in case it has changed since
    1941                 :            :                          * then, which should not happen.
    1942                 :            :                          */
    1943         [ #  # ]:          0 :                         if (config->mtu < RTE_ETHER_MIN_MTU) {
    1944                 :          0 :                                 PMD_INIT_LOG(ERR, "invalid max MTU value (%u)",
    1945                 :            :                                                 config->mtu);
    1946                 :          0 :                                 return -EINVAL;
    1947                 :            :                         }
    1948                 :            : 
    1949                 :          0 :                         hw->max_mtu = config->mtu;
    1950                 :            :                         /* Set initial MTU to maximum one supported by vhost */
    1951                 :          0 :                         eth_dev->data->mtu = config->mtu;
    1952                 :            : 
    1953                 :            :                 } else {
    1954                 :          0 :                         hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
    1955                 :          0 :                                 VLAN_TAG_LEN - hw->vtnet_hdr_size;
    1956                 :            :                 }
    1957                 :            : 
    1958         [ #  # ]:          0 :                 hw->rss_hash_types = 0;
    1959         [ #  # ]:          0 :                 if (virtio_with_feature(hw, VIRTIO_NET_F_RSS)) {
    1960                 :          0 :                         ret = virtio_dev_rss_init(eth_dev);
    1961         [ #  # ]:          0 :                         if (ret < 0)
    1962                 :            :                                 return ret;
    1963                 :            :                 }
    1964                 :            : 
    1965                 :          0 :                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d",
    1966                 :            :                                 config->max_virtqueue_pairs);
    1967                 :          0 :                 PMD_INIT_LOG(DEBUG, "config->status=%d", config->status);
    1968                 :          0 :                 PMD_INIT_LOG(DEBUG,
    1969                 :            :                                 "PORT MAC: " RTE_ETHER_ADDR_PRT_FMT,
    1970                 :            :                                 config->mac[0], config->mac[1],
    1971                 :            :                                 config->mac[2], config->mac[3],
    1972                 :            :                                 config->mac[4], config->mac[5]);
    1973                 :            :         } else {
    1974                 :          0 :                 PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1");
    1975                 :          0 :                 hw->max_queue_pairs = 1;
    1976                 :          0 :                 hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - RTE_ETHER_HDR_LEN -
    1977                 :          0 :                         VLAN_TAG_LEN - hw->vtnet_hdr_size;
    1978                 :            :         }
    1979                 :            : 
    1980                 :          0 :         ret = virtio_alloc_queues(eth_dev);
    1981         [ #  # ]:          0 :         if (ret < 0)
    1982                 :            :                 return ret;
    1983                 :            : 
    1984         [ #  # ]:          0 :         if (eth_dev->data->dev_conf.intr_conf.rxq) {
    1985                 :          0 :                 ret = virtio_configure_intr(eth_dev);
    1986         [ #  # ]:          0 :                 if (ret < 0) {
    1987                 :          0 :                         PMD_INIT_LOG(ERR, "failed to configure interrupt");
    1988                 :          0 :                         virtio_free_queues(hw);
    1989                 :          0 :                         return ret;
    1990                 :            :                 }
    1991                 :            :         }
    1992                 :            : 
    1993         [ #  # ]:          0 :         if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
    1994                 :            :                 /* Enable vector (0) for Link State Interrupt */
    1995         [ #  # ]:          0 :                 if (VIRTIO_OPS(hw)->set_config_irq(hw, 0) ==
    1996                 :            :                                 VIRTIO_MSI_NO_VECTOR) {
    1997                 :          0 :                         PMD_DRV_LOG(ERR, "failed to set config vector");
    1998                 :          0 :                         return -EBUSY;
    1999                 :            :                 }
    2000                 :            : 
    2001                 :          0 :         virtio_reinit_complete(hw);
    2002                 :            : 
    2003                 :          0 :         return 0;
    2004                 :            : }
    2005                 :            : 
    2006                 :            : /*
    2007                 :            :  * This function is based on probe() function in virtio_pci.c
    2008                 :            :  * It returns 0 on success.
    2009                 :            :  */
    2010                 :            : int
    2011                 :          0 : eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
    2012                 :            : {
    2013                 :          0 :         struct virtio_hw *hw = eth_dev->data->dev_private;
    2014                 :          0 :         uint32_t speed = RTE_ETH_SPEED_NUM_UNKNOWN;
    2015                 :          0 :         int vectorized = 0;
    2016                 :            :         int ret;
    2017                 :            : 
    2018                 :            :         if (sizeof(struct virtio_net_hdr_hash_report) > RTE_PKTMBUF_HEADROOM) {
    2019                 :            :                 PMD_INIT_LOG(ERR,
    2020                 :            :                         "Not sufficient headroom required = %d, avail = %d",
    2021                 :            :                         (int)sizeof(struct virtio_net_hdr_mrg_rxbuf),
    2022                 :            :                         RTE_PKTMBUF_HEADROOM);
    2023                 :            : 
    2024                 :            :                 return -1;
    2025                 :            :         }
    2026                 :            : 
    2027                 :          0 :         eth_dev->dev_ops = &virtio_eth_dev_ops;
    2028                 :            : 
    2029         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
    2030                 :          0 :                 set_rxtx_funcs(eth_dev);
    2031                 :          0 :                 return 0;
    2032                 :            :         }
    2033                 :            : 
    2034                 :          0 :         ret = virtio_dev_devargs_parse(eth_dev->device->devargs, &speed, &vectorized);
    2035         [ #  # ]:          0 :         if (ret < 0)
    2036                 :            :                 return ret;
    2037                 :          0 :         hw->speed = speed;
    2038                 :          0 :         hw->duplex = DUPLEX_UNKNOWN;
    2039                 :            : 
    2040                 :            :         /* Allocate memory for storing MAC addresses */
    2041                 :          0 :         eth_dev->data->mac_addrs = rte_zmalloc("virtio",
    2042                 :            :                                 VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN, 0);
    2043         [ #  # ]:          0 :         if (eth_dev->data->mac_addrs == NULL) {
    2044                 :          0 :                 PMD_INIT_LOG(ERR,
    2045                 :            :                         "Failed to allocate %d bytes needed to store MAC addresses",
    2046                 :            :                         VIRTIO_MAX_MAC_ADDRS * RTE_ETHER_ADDR_LEN);
    2047                 :          0 :                 return -ENOMEM;
    2048                 :            :         }
    2049                 :            : 
    2050                 :            :         rte_spinlock_init(&hw->state_lock);
    2051                 :            : 
    2052         [ #  # ]:          0 :         if (vectorized) {
    2053                 :          0 :                 hw->use_vec_rx = 1;
    2054                 :          0 :                 hw->use_vec_tx = 1;
    2055                 :            :         }
    2056                 :            : 
    2057                 :            :         /* reset device and negotiate default features */
    2058                 :          0 :         ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES);
    2059         [ #  # ]:          0 :         if (ret < 0)
    2060                 :          0 :                 goto err_virtio_init;
    2061                 :            : 
    2062         [ #  # ]:          0 :         if (vectorized) {
    2063         [ #  # ]:          0 :                 if (!virtio_with_packed_queue(hw)) {
    2064                 :          0 :                         hw->use_vec_tx = 0;
    2065                 :            :                 } else {
    2066                 :            : #if !defined(CC_AVX512_SUPPORT) && !defined(RTE_ARCH_ARM)
    2067                 :            :                         hw->use_vec_rx = 0;
    2068                 :            :                         hw->use_vec_tx = 0;
    2069                 :            :                         PMD_DRV_LOG(INFO,
    2070                 :            :                                 "building environment do not support packed ring vectorized");
    2071                 :            : #endif
    2072                 :            :                 }
    2073                 :            :         }
    2074                 :            : 
    2075                 :          0 :         hw->opened = 1;
    2076                 :            : 
    2077                 :          0 :         return 0;
    2078                 :            : 
    2079                 :            : err_virtio_init:
    2080                 :          0 :         rte_free(eth_dev->data->mac_addrs);
    2081                 :          0 :         eth_dev->data->mac_addrs = NULL;
    2082                 :          0 :         return ret;
    2083                 :            : }
    2084                 :            : 
    2085                 :            : static uint32_t
    2086                 :          0 : virtio_dev_speed_capa_get(uint32_t speed)
    2087                 :            : {
    2088   [ #  #  #  #  :          0 :         switch (speed) {
          #  #  #  #  #  
                      # ]
    2089                 :            :         case RTE_ETH_SPEED_NUM_10G:
    2090                 :            :                 return RTE_ETH_LINK_SPEED_10G;
    2091                 :          0 :         case RTE_ETH_SPEED_NUM_20G:
    2092                 :          0 :                 return RTE_ETH_LINK_SPEED_20G;
    2093                 :          0 :         case RTE_ETH_SPEED_NUM_25G:
    2094                 :          0 :                 return RTE_ETH_LINK_SPEED_25G;
    2095                 :          0 :         case RTE_ETH_SPEED_NUM_40G:
    2096                 :          0 :                 return RTE_ETH_LINK_SPEED_40G;
    2097                 :          0 :         case RTE_ETH_SPEED_NUM_50G:
    2098                 :          0 :                 return RTE_ETH_LINK_SPEED_50G;
    2099                 :          0 :         case RTE_ETH_SPEED_NUM_56G:
    2100                 :          0 :                 return RTE_ETH_LINK_SPEED_56G;
    2101                 :          0 :         case RTE_ETH_SPEED_NUM_100G:
    2102                 :          0 :                 return RTE_ETH_LINK_SPEED_100G;
    2103                 :          0 :         case RTE_ETH_SPEED_NUM_200G:
    2104                 :          0 :                 return RTE_ETH_LINK_SPEED_200G;
    2105                 :          0 :         case RTE_ETH_SPEED_NUM_400G:
    2106                 :          0 :                 return RTE_ETH_LINK_SPEED_400G;
    2107                 :          0 :         default:
    2108                 :          0 :                 return 0;
    2109                 :            :         }
    2110                 :            : }
    2111                 :            : 
    2112                 :          0 : static int vectorized_check_handler(__rte_unused const char *key,
    2113                 :            :                 const char *value, void *ret_val)
    2114                 :            : {
    2115         [ #  # ]:          0 :         if (value == NULL || ret_val == NULL)
    2116                 :            :                 return -EINVAL;
    2117                 :            : 
    2118         [ #  # ]:          0 :         if (strcmp(value, "1") == 0)
    2119                 :          0 :                 *(int *)ret_val = 1;
    2120                 :            :         else
    2121                 :          0 :                 *(int *)ret_val = 0;
    2122                 :            : 
    2123                 :            :         return 0;
    2124                 :            : }
    2125                 :            : 
    2126                 :            : #define VIRTIO_ARG_SPEED      "speed"
    2127                 :            : #define VIRTIO_ARG_VECTORIZED "vectorized"
    2128                 :            : 
    2129                 :            : static int
    2130                 :          0 : link_speed_handler(const char *key __rte_unused,
    2131                 :            :                 const char *value, void *ret_val)
    2132                 :            : {
    2133                 :            :         uint32_t val;
    2134         [ #  # ]:          0 :         if (!value || !ret_val)
    2135                 :            :                 return -EINVAL;
    2136                 :          0 :         val = strtoul(value, NULL, 0);
    2137                 :            :         /* validate input */
    2138         [ #  # ]:          0 :         if (virtio_dev_speed_capa_get(val) == 0)
    2139                 :            :                 return -EINVAL;
    2140                 :          0 :         *(uint32_t *)ret_val = val;
    2141                 :            : 
    2142                 :          0 :         return 0;
    2143                 :            : }
    2144                 :            : 
    2145                 :            : 
    2146                 :            : static int
    2147                 :          0 : virtio_dev_devargs_parse(struct rte_devargs *devargs, uint32_t *speed, int *vectorized)
    2148                 :            : {
    2149                 :            :         struct rte_kvargs *kvlist;
    2150                 :            :         int ret = 0;
    2151                 :            : 
    2152         [ #  # ]:          0 :         if (devargs == NULL)
    2153                 :            :                 return 0;
    2154                 :            : 
    2155                 :          0 :         kvlist = rte_kvargs_parse(devargs->args, NULL);
    2156         [ #  # ]:          0 :         if (kvlist == NULL) {
    2157                 :          0 :                 PMD_INIT_LOG(ERR, "error when parsing param");
    2158                 :          0 :                 return 0;
    2159                 :            :         }
    2160                 :            : 
    2161   [ #  #  #  # ]:          0 :         if (speed && rte_kvargs_count(kvlist, VIRTIO_ARG_SPEED) == 1) {
    2162                 :          0 :                 ret = rte_kvargs_process(kvlist,
    2163                 :            :                                         VIRTIO_ARG_SPEED,
    2164                 :            :                                         link_speed_handler, speed);
    2165         [ #  # ]:          0 :                 if (ret < 0) {
    2166                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to parse %s",
    2167                 :            :                                         VIRTIO_ARG_SPEED);
    2168                 :          0 :                         goto exit;
    2169                 :            :                 }
    2170                 :            :         }
    2171                 :            : 
    2172   [ #  #  #  # ]:          0 :         if (vectorized &&
    2173                 :          0 :                 rte_kvargs_count(kvlist, VIRTIO_ARG_VECTORIZED) == 1) {
    2174                 :          0 :                 ret = rte_kvargs_process(kvlist,
    2175                 :            :                                 VIRTIO_ARG_VECTORIZED,
    2176                 :            :                                 vectorized_check_handler, vectorized);
    2177         [ #  # ]:          0 :                 if (ret < 0) {
    2178                 :          0 :                         PMD_INIT_LOG(ERR, "Failed to parse %s",
    2179                 :            :                                         VIRTIO_ARG_VECTORIZED);
    2180                 :          0 :                         goto exit;
    2181                 :            :                 }
    2182                 :            :         }
    2183                 :            : 
    2184                 :          0 : exit:
    2185                 :          0 :         rte_kvargs_free(kvlist);
    2186                 :          0 :         return ret;
    2187                 :            : }
    2188                 :            : 
    2189                 :            : static uint8_t
    2190                 :            : rx_offload_enabled(struct virtio_hw *hw)
    2191                 :            : {
    2192         [ #  # ]:          0 :         return virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM) ||
    2193   [ #  #  #  # ]:          0 :                 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
    2194                 :            :                 virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6);
    2195                 :            : }
    2196                 :            : 
    2197                 :            : static uint8_t
    2198                 :            : tx_offload_enabled(struct virtio_hw *hw)
    2199                 :            : {
    2200         [ #  # ]:          0 :         return virtio_with_feature(hw, VIRTIO_NET_F_CSUM) ||
    2201   [ #  #  #  # ]:          0 :                 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO4) ||
    2202                 :            :                 virtio_with_feature(hw, VIRTIO_NET_F_HOST_TSO6);
    2203                 :            : }
    2204                 :            : 
    2205                 :            : /*
    2206                 :            :  * Configure virtio device
    2207                 :            :  * It returns 0 on success.
    2208                 :            :  */
    2209                 :            : static int
    2210                 :          0 : virtio_dev_configure(struct rte_eth_dev *dev)
    2211                 :            : {
    2212                 :          0 :         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
    2213                 :            :         const struct rte_eth_txmode *txmode = &dev->data->dev_conf.txmode;
    2214                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2215                 :          0 :         uint32_t ether_hdr_len = RTE_ETHER_HDR_LEN + VLAN_TAG_LEN +
    2216                 :          0 :                 hw->vtnet_hdr_size;
    2217                 :          0 :         uint64_t rx_offloads = rxmode->offloads;
    2218                 :          0 :         uint64_t tx_offloads = txmode->offloads;
    2219                 :            :         uint64_t req_features;
    2220                 :            :         int ret;
    2221                 :            : 
    2222                 :          0 :         PMD_INIT_LOG(DEBUG, "configure");
    2223                 :            :         req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES;
    2224                 :            : 
    2225         [ #  # ]:          0 :         if (rxmode->mq_mode != RTE_ETH_MQ_RX_NONE && rxmode->mq_mode != RTE_ETH_MQ_RX_RSS) {
    2226                 :          0 :                 PMD_DRV_LOG(ERR,
    2227                 :            :                         "Unsupported Rx multi queue mode %d",
    2228                 :            :                         rxmode->mq_mode);
    2229                 :          0 :                 return -EINVAL;
    2230                 :            :         }
    2231                 :            : 
    2232         [ #  # ]:          0 :         if (txmode->mq_mode != RTE_ETH_MQ_TX_NONE) {
    2233                 :          0 :                 PMD_DRV_LOG(ERR,
    2234                 :            :                         "Unsupported Tx multi queue mode %d",
    2235                 :            :                         txmode->mq_mode);
    2236                 :          0 :                 return -EINVAL;
    2237                 :            :         }
    2238                 :            : 
    2239         [ #  # ]:          0 :         if (dev->data->dev_conf.intr_conf.rxq) {
    2240                 :          0 :                 ret = virtio_init_device(dev, hw->req_guest_features);
    2241         [ #  # ]:          0 :                 if (ret < 0)
    2242                 :            :                         return ret;
    2243                 :            :         }
    2244                 :            : 
    2245         [ #  # ]:          0 :         if (rxmode->mq_mode == RTE_ETH_MQ_RX_RSS)
    2246                 :            :                 req_features |= (1ULL << VIRTIO_NET_F_RSS);
    2247                 :            : 
    2248         [ #  # ]:          0 :         if (rxmode->mtu > hw->max_mtu)
    2249                 :          0 :                 req_features &= ~(1ULL << VIRTIO_NET_F_MTU);
    2250                 :            : 
    2251                 :          0 :         hw->max_rx_pkt_len = ether_hdr_len + rxmode->mtu;
    2252                 :            : 
    2253         [ #  # ]:          0 :         if (rx_offloads & (RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
    2254                 :            :                            RTE_ETH_RX_OFFLOAD_TCP_CKSUM))
    2255                 :          0 :                 req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
    2256                 :            : 
    2257         [ #  # ]:          0 :         if (rx_offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO)
    2258                 :          0 :                 req_features |=
    2259                 :            :                         (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
    2260                 :            :                         (1ULL << VIRTIO_NET_F_GUEST_TSO6);
    2261                 :            : 
    2262         [ #  # ]:          0 :         if (rx_offloads & RTE_ETH_RX_OFFLOAD_RSS_HASH)
    2263                 :          0 :                 req_features |=
    2264                 :            :                         (1ULL << VIRTIO_NET_F_HASH_REPORT);
    2265                 :            : 
    2266         [ #  # ]:          0 :         if (tx_offloads & (RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
    2267                 :            :                            RTE_ETH_TX_OFFLOAD_TCP_CKSUM))
    2268                 :          0 :                 req_features |= (1ULL << VIRTIO_NET_F_CSUM);
    2269                 :            : 
    2270         [ #  # ]:          0 :         if (tx_offloads & RTE_ETH_TX_OFFLOAD_TCP_TSO)
    2271                 :          0 :                 req_features |=
    2272                 :            :                         (1ULL << VIRTIO_NET_F_HOST_TSO4) |
    2273                 :            :                         (1ULL << VIRTIO_NET_F_HOST_TSO6);
    2274                 :            : 
    2275                 :            :         /* if request features changed, reinit the device */
    2276         [ #  # ]:          0 :         if (req_features != hw->req_guest_features) {
    2277                 :          0 :                 ret = virtio_init_device(dev, req_features);
    2278         [ #  # ]:          0 :                 if (ret < 0)
    2279                 :            :                         return ret;
    2280                 :            :         }
    2281                 :            : 
    2282                 :            :         /* if queues are not allocated, reinit the device */
    2283         [ #  # ]:          0 :         if (hw->vqs == NULL) {
    2284                 :          0 :                 ret = virtio_init_device(dev, hw->req_guest_features);
    2285         [ #  # ]:          0 :                 if (ret < 0)
    2286                 :            :                         return ret;
    2287                 :            :         }
    2288                 :            : 
    2289   [ #  #  #  # ]:          0 :         if ((rxmode->mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) &&
    2290                 :            :                         !virtio_with_feature(hw, VIRTIO_NET_F_RSS)) {
    2291                 :          0 :                 PMD_DRV_LOG(ERR, "RSS support requested but not supported by the device");
    2292                 :          0 :                 return -ENOTSUP;
    2293                 :            :         }
    2294                 :            : 
    2295         [ #  # ]:          0 :         if ((rx_offloads & (RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
    2296         [ #  # ]:          0 :                             RTE_ETH_RX_OFFLOAD_TCP_CKSUM)) &&
    2297                 :            :                 !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) {
    2298                 :          0 :                 PMD_DRV_LOG(ERR,
    2299                 :            :                         "rx checksum not available on this host");
    2300                 :          0 :                 return -ENOTSUP;
    2301                 :            :         }
    2302                 :            : 
    2303   [ #  #  #  # ]:          0 :         if ((rx_offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO) &&
    2304         [ #  # ]:          0 :                 (!virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) ||
    2305                 :            :                  !virtio_with_feature(hw, VIRTIO_NET_F_GUEST_TSO6))) {
    2306                 :          0 :                 PMD_DRV_LOG(ERR,
    2307                 :            :                         "Large Receive Offload not available on this host");
    2308                 :          0 :                 return -ENOTSUP;
    2309                 :            :         }
    2310                 :            : 
    2311                 :            :         /* start control queue */
    2312         [ #  # ]:          0 :         if (virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VQ))
    2313                 :          0 :                 virtio_dev_cq_start(dev);
    2314                 :            : 
    2315         [ #  # ]:          0 :         if (rx_offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP)
    2316                 :          0 :                 hw->vlan_strip = 1;
    2317                 :            : 
    2318         [ #  # ]:          0 :         if (rx_offloads & RTE_ETH_RX_OFFLOAD_RSS_HASH)
    2319                 :          0 :                 hw->has_hash_report = 1;
    2320                 :            : 
    2321                 :          0 :         hw->rx_ol_scatter = (rx_offloads & RTE_ETH_RX_OFFLOAD_SCATTER);
    2322                 :            : 
    2323   [ #  #  #  # ]:          0 :         if ((rx_offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) &&
    2324                 :            :                         !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
    2325                 :          0 :                 PMD_DRV_LOG(ERR,
    2326                 :            :                             "vlan filtering not available on this host");
    2327                 :          0 :                 return -ENOTSUP;
    2328                 :            :         }
    2329                 :            : 
    2330         [ #  # ]:          0 :         hw->has_tx_offload = tx_offload_enabled(hw);
    2331         [ #  # ]:          0 :         hw->has_rx_offload = rx_offload_enabled(hw);
    2332                 :            : 
    2333         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw)) {
    2334                 :            : #if defined(RTE_ARCH_X86_64) && defined(CC_AVX512_SUPPORT)
    2335   [ #  #  #  # ]:          0 :                 if ((hw->use_vec_rx || hw->use_vec_tx) &&
    2336         [ #  # ]:          0 :                     (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) ||
    2337         [ #  # ]:          0 :                      !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
    2338         [ #  # ]:          0 :                      !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
    2339                 :          0 :                      rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_512)) {
    2340                 :          0 :                         PMD_DRV_LOG(INFO,
    2341                 :            :                                 "disabled packed ring vectorized path for requirements not met");
    2342                 :          0 :                         hw->use_vec_rx = 0;
    2343                 :          0 :                         hw->use_vec_tx = 0;
    2344                 :            :                 }
    2345                 :            : #elif defined(RTE_ARCH_ARM)
    2346                 :            :                 if ((hw->use_vec_rx || hw->use_vec_tx) &&
    2347                 :            :                     (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON) ||
    2348                 :            :                      !virtio_with_feature(hw, VIRTIO_F_IN_ORDER) ||
    2349                 :            :                      !virtio_with_feature(hw, VIRTIO_F_VERSION_1) ||
    2350                 :            :                      rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)) {
    2351                 :            :                         PMD_DRV_LOG(INFO,
    2352                 :            :                                 "disabled packed ring vectorized path for requirements not met");
    2353                 :            :                         hw->use_vec_rx = 0;
    2354                 :            :                         hw->use_vec_tx = 0;
    2355                 :            :                 }
    2356                 :            : #else
    2357                 :            :                 hw->use_vec_rx = 0;
    2358                 :            :                 hw->use_vec_tx = 0;
    2359                 :            : #endif
    2360                 :            : 
    2361         [ #  # ]:          0 :                 if (hw->use_vec_rx) {
    2362         [ #  # ]:          0 :                         if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
    2363                 :          0 :                                 PMD_DRV_LOG(INFO,
    2364                 :            :                                         "disabled packed ring vectorized rx for mrg_rxbuf enabled");
    2365                 :          0 :                                 hw->use_vec_rx = 0;
    2366                 :            :                         }
    2367                 :            : 
    2368         [ #  # ]:          0 :                         if (rx_offloads & RTE_ETH_RX_OFFLOAD_TCP_LRO) {
    2369                 :          0 :                                 PMD_DRV_LOG(INFO,
    2370                 :            :                                         "disabled packed ring vectorized rx for TCP_LRO enabled");
    2371                 :          0 :                                 hw->use_vec_rx = 0;
    2372                 :            :                         }
    2373                 :            : 
    2374         [ #  # ]:          0 :                         if (rx_offloads & RTE_ETH_RX_OFFLOAD_RSS_HASH) {
    2375                 :          0 :                                 PMD_DRV_LOG(INFO,
    2376                 :            :                                         "disabled packed ring vectorized rx for RSS_HASH enabled");
    2377                 :          0 :                                 hw->use_vec_rx = 0;
    2378                 :            :                         }
    2379                 :            :                 }
    2380                 :            :         } else {
    2381         [ #  # ]:          0 :                 if (virtio_with_feature(hw, VIRTIO_F_IN_ORDER)) {
    2382                 :          0 :                         hw->use_inorder_tx = 1;
    2383                 :          0 :                         hw->use_inorder_rx = 1;
    2384                 :          0 :                         hw->use_vec_rx = 0;
    2385                 :            :                 }
    2386                 :            : 
    2387         [ #  # ]:          0 :                 if (hw->use_vec_rx) {
    2388                 :            : #if defined RTE_ARCH_ARM
    2389                 :            :                         if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) {
    2390                 :            :                                 PMD_DRV_LOG(INFO,
    2391                 :            :                                         "disabled split ring vectorized path for requirement not met");
    2392                 :            :                                 hw->use_vec_rx = 0;
    2393                 :            :                         }
    2394                 :            : #endif
    2395         [ #  # ]:          0 :                         if (virtio_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) {
    2396                 :          0 :                                 PMD_DRV_LOG(INFO,
    2397                 :            :                                         "disabled split ring vectorized rx for mrg_rxbuf enabled");
    2398                 :          0 :                                 hw->use_vec_rx = 0;
    2399                 :            :                         }
    2400                 :            : 
    2401         [ #  # ]:          0 :                         if (rx_offloads & (RTE_ETH_RX_OFFLOAD_UDP_CKSUM |
    2402                 :            :                                            RTE_ETH_RX_OFFLOAD_TCP_CKSUM |
    2403                 :            :                                            RTE_ETH_RX_OFFLOAD_TCP_LRO |
    2404                 :            :                                            RTE_ETH_RX_OFFLOAD_VLAN_STRIP)) {
    2405                 :          0 :                                 PMD_DRV_LOG(INFO,
    2406                 :            :                                         "disabled split ring vectorized rx for offloading enabled");
    2407                 :          0 :                                 hw->use_vec_rx = 0;
    2408                 :            :                         }
    2409                 :            : 
    2410         [ #  # ]:          0 :                         if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128) {
    2411                 :          0 :                                 PMD_DRV_LOG(INFO,
    2412                 :            :                                         "disabled split ring vectorized rx, max SIMD bitwidth too low");
    2413                 :          0 :                                 hw->use_vec_rx = 0;
    2414                 :            :                         }
    2415                 :            :                 }
    2416                 :            :         }
    2417                 :            : 
    2418                 :            :         return 0;
    2419                 :            : }
    2420                 :            : 
    2421                 :            : 
    2422                 :            : static int
    2423                 :          0 : virtio_dev_start(struct rte_eth_dev *dev)
    2424                 :            : {
    2425                 :            :         uint16_t nb_queues, i;
    2426                 :            :         struct virtqueue *vq;
    2427                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2428                 :            :         int ret;
    2429                 :            : 
    2430                 :            :         /* Finish the initialization of the queues */
    2431         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
    2432                 :          0 :                 ret = virtio_dev_rx_queue_setup_finish(dev, i);
    2433         [ #  # ]:          0 :                 if (ret < 0)
    2434                 :          0 :                         return ret;
    2435                 :            :         }
    2436         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
    2437                 :          0 :                 ret = virtio_dev_tx_queue_setup_finish(dev, i);
    2438         [ #  # ]:          0 :                 if (ret < 0)
    2439                 :          0 :                         return ret;
    2440                 :            :         }
    2441                 :            : 
    2442                 :            :         /* check if lsc interrupt feature is enabled */
    2443         [ #  # ]:          0 :         if (dev->data->dev_conf.intr_conf.lsc) {
    2444         [ #  # ]:          0 :                 if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
    2445                 :          0 :                         PMD_DRV_LOG(ERR, "link status not supported by host");
    2446                 :          0 :                         return -ENOTSUP;
    2447                 :            :                 }
    2448                 :            :         }
    2449                 :            : 
    2450                 :            :         /* Enable uio/vfio intr/eventfd mapping: although we already did that
    2451                 :            :          * in device configure, but it could be unmapped  when device is
    2452                 :            :          * stopped.
    2453                 :            :          */
    2454         [ #  # ]:          0 :         if (dev->data->dev_conf.intr_conf.lsc ||
    2455                 :            :             dev->data->dev_conf.intr_conf.rxq) {
    2456                 :          0 :                 virtio_intr_disable(dev);
    2457                 :            : 
    2458                 :            :                 /* Setup interrupt callback  */
    2459         [ #  # ]:          0 :                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
    2460                 :          0 :                         rte_intr_callback_register(dev->intr_handle,
    2461                 :            :                                                    virtio_interrupt_handler,
    2462                 :            :                                                    dev);
    2463                 :            : 
    2464         [ #  # ]:          0 :                 if (virtio_intr_enable(dev) < 0) {
    2465                 :          0 :                         PMD_DRV_LOG(ERR, "interrupt enable failed");
    2466                 :          0 :                         return -EIO;
    2467                 :            :                 }
    2468                 :            :         }
    2469                 :            : 
    2470                 :            :         /*Notify the backend
    2471                 :            :          *Otherwise the tap backend might already stop its queue due to fullness.
    2472                 :            :          *vhost backend will have no chance to be waked up
    2473                 :            :          */
    2474                 :          0 :         nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
    2475         [ #  # ]:          0 :         if (hw->max_queue_pairs > 1) {
    2476         [ #  # ]:          0 :                 if (virtio_set_multiple_queues(dev, nb_queues) != 0)
    2477                 :            :                         return -EINVAL;
    2478                 :            :         }
    2479                 :            : 
    2480                 :          0 :         PMD_INIT_LOG(DEBUG, "nb_queues=%u (port=%u)", nb_queues,
    2481                 :            :                      dev->data->port_id);
    2482                 :            : 
    2483         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
    2484                 :          0 :                 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
    2485                 :            :                 /* Flush the old packets */
    2486                 :          0 :                 virtqueue_rxvq_flush(vq);
    2487                 :            :                 virtqueue_notify(vq);
    2488                 :            :         }
    2489                 :            : 
    2490         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
    2491                 :          0 :                 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
    2492                 :            :                 virtqueue_notify(vq);
    2493                 :            :         }
    2494                 :            : 
    2495                 :          0 :         PMD_INIT_LOG(DEBUG, "Notified backend at initialization (port=%u)",
    2496                 :            :                      dev->data->port_id);
    2497                 :            : 
    2498         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
    2499                 :            :                 vq = virtnet_rxq_to_vq(dev->data->rx_queues[i]);
    2500                 :            :                 VIRTQUEUE_DUMP(vq);
    2501                 :            :         }
    2502                 :            : 
    2503         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
    2504                 :            :                 vq = virtnet_txq_to_vq(dev->data->tx_queues[i]);
    2505                 :            :                 VIRTQUEUE_DUMP(vq);
    2506                 :            :         }
    2507                 :            : 
    2508                 :          0 :         set_rxtx_funcs(dev);
    2509                 :          0 :         hw->started = 1;
    2510                 :            : 
    2511         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++)
    2512                 :          0 :                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
    2513         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++)
    2514                 :          0 :                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
    2515                 :            : 
    2516                 :            :         /* Initialize Link state */
    2517                 :          0 :         virtio_dev_link_update(dev, 0);
    2518                 :            : 
    2519                 :          0 :         return 0;
    2520                 :            : }
    2521                 :            : 
    2522                 :          0 : static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
    2523                 :            : {
    2524         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2525                 :            :         uint16_t nr_vq = virtio_get_nr_vq(hw);
    2526                 :            :         const char *type __rte_unused;
    2527                 :            :         unsigned int i, mbuf_num = 0;
    2528                 :            :         struct virtqueue *vq;
    2529                 :            :         struct rte_mbuf *buf;
    2530                 :            :         int queue_type;
    2531                 :            : 
    2532         [ #  # ]:          0 :         if (hw->vqs == NULL)
    2533                 :            :                 return;
    2534                 :            : 
    2535         [ #  # ]:          0 :         for (i = 0; i < nr_vq; i++) {
    2536                 :          0 :                 vq = hw->vqs[i];
    2537         [ #  # ]:          0 :                 if (!vq)
    2538                 :          0 :                         continue;
    2539                 :            : 
    2540         [ #  # ]:          0 :                 queue_type = virtio_get_queue_type(hw, i);
    2541                 :            :                 if (queue_type == VTNET_RQ)
    2542                 :            :                         type = "rxq";
    2543         [ #  # ]:          0 :                 else if (queue_type == VTNET_TQ)
    2544                 :            :                         type = "txq";
    2545                 :            :                 else
    2546                 :          0 :                         continue;
    2547                 :            : 
    2548                 :          0 :                 PMD_INIT_LOG(DEBUG,
    2549                 :            :                         "Before freeing %s[%d] used and unused buf",
    2550                 :            :                         type, i);
    2551                 :            :                 VIRTQUEUE_DUMP(vq);
    2552                 :            : 
    2553         [ #  # ]:          0 :                 while ((buf = virtqueue_detach_unused(vq)) != NULL) {
    2554                 :          0 :                         rte_pktmbuf_free(buf);
    2555                 :          0 :                         mbuf_num++;
    2556                 :            :                 }
    2557                 :            : 
    2558                 :          0 :                 PMD_INIT_LOG(DEBUG,
    2559                 :            :                         "After freeing %s[%d] used and unused buf",
    2560                 :            :                         type, i);
    2561                 :            :                 VIRTQUEUE_DUMP(vq);
    2562                 :            :         }
    2563                 :            : 
    2564                 :          0 :         PMD_INIT_LOG(DEBUG, "%d mbufs freed", mbuf_num);
    2565                 :            : }
    2566                 :            : 
    2567                 :            : static void
    2568                 :          0 : virtio_tx_completed_cleanup(struct rte_eth_dev *dev)
    2569                 :            : {
    2570         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2571                 :            :         struct virtqueue *vq;
    2572                 :            :         int qidx;
    2573                 :            :         void (*xmit_cleanup)(struct virtqueue *vq, uint16_t nb_used);
    2574                 :            : 
    2575         [ #  # ]:          0 :         if (virtio_with_packed_queue(hw)) {
    2576         [ #  # ]:          0 :                 if (hw->use_vec_tx)
    2577                 :            :                         xmit_cleanup = &virtio_xmit_cleanup_inorder_packed;
    2578         [ #  # ]:          0 :                 else if (virtio_with_feature(hw, VIRTIO_F_IN_ORDER))
    2579                 :            :                         xmit_cleanup = &virtio_xmit_cleanup_inorder_packed;
    2580                 :            :                 else
    2581                 :            :                         xmit_cleanup = &virtio_xmit_cleanup_normal_packed;
    2582                 :            :         } else {
    2583         [ #  # ]:          0 :                 if (hw->use_inorder_tx)
    2584                 :            :                         xmit_cleanup = &virtio_xmit_cleanup_inorder;
    2585                 :            :                 else
    2586                 :            :                         xmit_cleanup = &virtio_xmit_cleanup;
    2587                 :            :         }
    2588                 :            : 
    2589         [ #  # ]:          0 :         for (qidx = 0; qidx < hw->max_queue_pairs; qidx++) {
    2590                 :          0 :                 vq = hw->vqs[2 * qidx + VTNET_SQ_TQ_QUEUE_IDX];
    2591         [ #  # ]:          0 :                 if (vq != NULL)
    2592                 :          0 :                         xmit_cleanup(vq, virtqueue_nused(vq));
    2593                 :            :         }
    2594                 :          0 : }
    2595                 :            : 
    2596                 :            : /*
    2597                 :            :  * Stop device: disable interrupt and mark link down
    2598                 :            :  */
    2599                 :            : int
    2600                 :          0 : virtio_dev_stop(struct rte_eth_dev *dev)
    2601                 :            : {
    2602                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2603                 :            :         struct rte_eth_link link;
    2604                 :            :         struct rte_eth_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf;
    2605                 :            :         uint16_t i;
    2606                 :            : 
    2607                 :          0 :         PMD_INIT_LOG(DEBUG, "stop");
    2608                 :          0 :         dev->data->dev_started = 0;
    2609                 :            : 
    2610                 :          0 :         rte_spinlock_lock(&hw->state_lock);
    2611         [ #  # ]:          0 :         if (!hw->started)
    2612                 :          0 :                 goto out_unlock;
    2613                 :          0 :         hw->started = 0;
    2614                 :            : 
    2615                 :          0 :         virtio_tx_completed_cleanup(dev);
    2616                 :            : 
    2617         [ #  # ]:          0 :         if (intr_conf->lsc || intr_conf->rxq) {
    2618                 :          0 :                 virtio_intr_disable(dev);
    2619                 :            : 
    2620                 :            :                 /* Reset interrupt callback  */
    2621         [ #  # ]:          0 :                 if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
    2622                 :          0 :                         rte_intr_callback_unregister(dev->intr_handle,
    2623                 :            :                                                      virtio_interrupt_handler,
    2624                 :            :                                                      dev);
    2625                 :            :                 }
    2626                 :            :         }
    2627                 :            : 
    2628                 :            :         memset(&link, 0, sizeof(link));
    2629                 :          0 :         rte_eth_linkstatus_set(dev, &link);
    2630                 :          0 : out_unlock:
    2631                 :            :         rte_spinlock_unlock(&hw->state_lock);
    2632                 :            : 
    2633         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++)
    2634                 :          0 :                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
    2635         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++)
    2636                 :          0 :                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
    2637                 :            : 
    2638                 :          0 :         return 0;
    2639                 :            : }
    2640                 :            : 
    2641                 :            : static int
    2642                 :          0 : virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
    2643                 :            : {
    2644                 :            :         struct rte_eth_link link;
    2645                 :            :         uint16_t status;
    2646         [ #  # ]:          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2647                 :            : 
    2648                 :            :         memset(&link, 0, sizeof(link));
    2649                 :          0 :         link.link_duplex = hw->duplex;
    2650                 :          0 :         link.link_speed  = hw->speed;
    2651                 :          0 :         link.link_autoneg = RTE_ETH_LINK_AUTONEG;
    2652                 :            : 
    2653         [ #  # ]:          0 :         if (!hw->started) {
    2654                 :            :                 link.link_status = RTE_ETH_LINK_DOWN;
    2655                 :          0 :                 link.link_speed = RTE_ETH_SPEED_NUM_NONE;
    2656         [ #  # ]:          0 :         } else if (virtio_with_feature(hw, VIRTIO_NET_F_STATUS)) {
    2657                 :          0 :                 PMD_INIT_LOG(DEBUG, "Get link status from hw");
    2658                 :          0 :                 virtio_read_dev_config(hw,
    2659                 :            :                                 offsetof(struct virtio_net_config, status),
    2660                 :            :                                 &status, sizeof(status));
    2661         [ #  # ]:          0 :                 if ((status & VIRTIO_NET_S_LINK_UP) == 0) {
    2662                 :            :                         link.link_status = RTE_ETH_LINK_DOWN;
    2663                 :          0 :                         link.link_speed = RTE_ETH_SPEED_NUM_NONE;
    2664                 :          0 :                         PMD_INIT_LOG(DEBUG, "Port %d is down",
    2665                 :            :                                      dev->data->port_id);
    2666                 :            :                 } else {
    2667                 :          0 :                         link.link_status = RTE_ETH_LINK_UP;
    2668         [ #  # ]:          0 :                         if (hw->get_speed_via_feat)
    2669                 :          0 :                                 virtio_get_speed_duplex(dev, &link);
    2670                 :          0 :                         PMD_INIT_LOG(DEBUG, "Port %d is up",
    2671                 :            :                                      dev->data->port_id);
    2672                 :            :                 }
    2673                 :            :         } else {
    2674                 :          0 :                 link.link_status = RTE_ETH_LINK_UP;
    2675         [ #  # ]:          0 :                 if (hw->get_speed_via_feat)
    2676                 :          0 :                         virtio_get_speed_duplex(dev, &link);
    2677                 :            :         }
    2678                 :            : 
    2679                 :          0 :         return rte_eth_linkstatus_set(dev, &link);
    2680                 :            : }
    2681                 :            : 
    2682                 :            : static int
    2683                 :          0 : virtio_dev_vlan_offload_set(struct rte_eth_dev *dev, int mask)
    2684                 :            : {
    2685                 :          0 :         const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
    2686                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2687                 :          0 :         uint64_t offloads = rxmode->offloads;
    2688                 :            : 
    2689         [ #  # ]:          0 :         if (mask & RTE_ETH_VLAN_FILTER_MASK) {
    2690   [ #  #  #  # ]:          0 :                 if ((offloads & RTE_ETH_RX_OFFLOAD_VLAN_FILTER) &&
    2691                 :            :                                 !virtio_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) {
    2692                 :            : 
    2693                 :          0 :                         PMD_DRV_LOG(NOTICE,
    2694                 :            :                                 "vlan filtering not available on this host");
    2695                 :            : 
    2696                 :          0 :                         return -ENOTSUP;
    2697                 :            :                 }
    2698                 :            :         }
    2699                 :            : 
    2700         [ #  # ]:          0 :         if (mask & RTE_ETH_VLAN_STRIP_MASK)
    2701                 :          0 :                 hw->vlan_strip = !!(offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
    2702                 :            : 
    2703                 :            :         return 0;
    2704                 :            : }
    2705                 :            : 
    2706                 :            : static int
    2707                 :          0 : virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
    2708                 :            : {
    2709                 :            :         uint64_t tso_mask, host_features;
    2710                 :          0 :         uint32_t rss_hash_types = 0;
    2711                 :          0 :         struct virtio_hw *hw = dev->data->dev_private;
    2712                 :          0 :         dev_info->speed_capa = virtio_dev_speed_capa_get(hw->speed);
    2713                 :            : 
    2714                 :          0 :         dev_info->max_rx_queues =
    2715                 :          0 :                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
    2716                 :          0 :         dev_info->max_tx_queues =
    2717                 :            :                 RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES);
    2718                 :          0 :         dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE;
    2719                 :          0 :         dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN;
    2720                 :          0 :         dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS;
    2721                 :          0 :         dev_info->max_mtu = hw->max_mtu;
    2722                 :            : 
    2723                 :          0 :         host_features = VIRTIO_OPS(hw)->get_features(hw);
    2724                 :          0 :         dev_info->rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP;
    2725         [ #  # ]:          0 :         if (host_features & (1ULL << VIRTIO_NET_F_MRG_RXBUF))
    2726                 :          0 :                 dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_SCATTER;
    2727         [ #  # ]:          0 :         if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) {
    2728                 :          0 :                 dev_info->rx_offload_capa |=
    2729                 :            :                         RTE_ETH_RX_OFFLOAD_TCP_CKSUM |
    2730                 :            :                         RTE_ETH_RX_OFFLOAD_UDP_CKSUM;
    2731                 :            :         }
    2732         [ #  # ]:          0 :         if (host_features & (1ULL << VIRTIO_NET_F_CTRL_VLAN))
    2733                 :          0 :                 dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_VLAN_FILTER;
    2734                 :            :         tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
    2735                 :            :                 (1ULL << VIRTIO_NET_F_GUEST_TSO6);
    2736         [ #  # ]:          0 :         if ((host_features & tso_mask) == tso_mask)
    2737                 :          0 :                 dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_TCP_LRO;
    2738                 :            : 
    2739                 :          0 :         dev_info->tx_offload_capa = RTE_ETH_TX_OFFLOAD_MULTI_SEGS |
    2740                 :            :                                     RTE_ETH_TX_OFFLOAD_VLAN_INSERT;
    2741         [ #  # ]:          0 :         if (host_features & (1ULL << VIRTIO_NET_F_CSUM)) {
    2742                 :          0 :                 dev_info->tx_offload_capa |=
    2743                 :            :                         RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
    2744                 :            :                         RTE_ETH_TX_OFFLOAD_TCP_CKSUM;
    2745                 :            :         }
    2746                 :            :         tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) |
    2747                 :            :                 (1ULL << VIRTIO_NET_F_HOST_TSO6);
    2748         [ #  # ]:          0 :         if ((host_features & tso_mask) == tso_mask)
    2749                 :          0 :                 dev_info->tx_offload_capa |= RTE_ETH_TX_OFFLOAD_TCP_TSO;
    2750                 :            : 
    2751         [ #  # ]:          0 :         if (host_features & (1ULL << VIRTIO_NET_F_RSS)) {
    2752                 :          0 :                 virtio_dev_get_rss_config(hw, &rss_hash_types);
    2753                 :          0 :                 dev_info->hash_key_size = VIRTIO_NET_RSS_KEY_SIZE;
    2754                 :          0 :                 dev_info->reta_size = VIRTIO_NET_RSS_RETA_SIZE;
    2755                 :          0 :                 dev_info->flow_type_rss_offloads =
    2756                 :          0 :                         virtio_to_ethdev_rss_offloads(rss_hash_types);
    2757                 :            :         } else {
    2758                 :          0 :                 dev_info->hash_key_size = 0;
    2759                 :          0 :                 dev_info->reta_size = 0;
    2760                 :          0 :                 dev_info->flow_type_rss_offloads = 0;
    2761                 :            :         }
    2762                 :            : 
    2763         [ #  # ]:          0 :         if (host_features & (1ULL << VIRTIO_NET_F_HASH_REPORT))
    2764                 :          0 :                 dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
    2765                 :            : 
    2766         [ #  # ]:          0 :         if (host_features & (1ULL << VIRTIO_F_RING_PACKED)) {
    2767                 :            :                 /*
    2768                 :            :                  * According to 2.7 Packed Virtqueues,
    2769                 :            :                  * 2.7.10.1 Structure Size and Alignment:
    2770                 :            :                  * The Queue Size value does not have to be a power of 2.
    2771                 :            :                  */
    2772                 :          0 :                 dev_info->rx_desc_lim.nb_max = UINT16_MAX;
    2773                 :          0 :                 dev_info->tx_desc_lim.nb_max = UINT16_MAX;
    2774                 :            :         } else {
    2775                 :            :                 /*
    2776                 :            :                  * According to 2.6 Split Virtqueues:
    2777                 :            :                  * Queue Size value is always a power of 2. The maximum Queue
    2778                 :            :                  * Size value is 32768.
    2779                 :            :                  */
    2780                 :          0 :                 dev_info->rx_desc_lim.nb_max = 32768;
    2781                 :          0 :                 dev_info->tx_desc_lim.nb_max = 32768;
    2782                 :            :         }
    2783                 :            :         /*
    2784                 :            :          * Actual minimum is not the same for virtqueues of different kinds,
    2785                 :            :          * but to avoid tangling the code with separate branches, rely on
    2786                 :            :          * default thresholds since desc number must be at least of their size.
    2787                 :            :          */
    2788                 :          0 :         dev_info->rx_desc_lim.nb_min = RTE_MAX(DEFAULT_RX_FREE_THRESH,
    2789                 :            :                                                RTE_VIRTIO_VPMD_RX_REARM_THRESH);
    2790                 :          0 :         dev_info->tx_desc_lim.nb_min = DEFAULT_TX_FREE_THRESH;
    2791                 :          0 :         dev_info->rx_desc_lim.nb_align = 1;
    2792                 :          0 :         dev_info->tx_desc_lim.nb_align = 1;
    2793                 :            : 
    2794                 :          0 :         return 0;
    2795                 :            : }
    2796                 :            : 
    2797                 :            : /*
    2798                 :            :  * It enables testpmd to collect per queue stats.
    2799                 :            :  */
    2800                 :            : static int
    2801                 :          0 : virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev,
    2802                 :            : __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx,
    2803                 :            : __rte_unused uint8_t is_rx)
    2804                 :            : {
    2805                 :          0 :         return 0;
    2806                 :            : }
    2807                 :            : 
    2808         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(virtio_logtype_init, init, NOTICE);
    2809         [ -  + ]:        252 : RTE_LOG_REGISTER_SUFFIX(virtio_logtype_driver, driver, NOTICE);

Generated by: LCOV version 1.14