LCOV - code coverage report
Current view: top level - drivers/net/netvsc - hn_ethdev.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 3 669 0.4 %
Date: 2025-10-01 17:51:42 Functions: 3 40 7.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 352 0.6 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2016-2018 Microsoft Corporation
       3                 :            :  * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
       4                 :            :  * All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include <stdint.h>
       8                 :            : #include <string.h>
       9                 :            : #include <stdio.h>
      10                 :            : #include <errno.h>
      11                 :            : #include <unistd.h>
      12                 :            : #include <dirent.h>
      13                 :            : #include <net/if.h>
      14                 :            : #include <net/if_arp.h>
      15                 :            : #include <netinet/in.h>
      16                 :            : #include <sys/ioctl.h>
      17                 :            : 
      18                 :            : #include <rte_ethdev.h>
      19                 :            : #include <rte_memcpy.h>
      20                 :            : #include <rte_string_fns.h>
      21                 :            : #include <rte_memzone.h>
      22                 :            : #include <rte_devargs.h>
      23                 :            : #include <rte_malloc.h>
      24                 :            : #include <rte_kvargs.h>
      25                 :            : #include <rte_atomic.h>
      26                 :            : #include <rte_branch_prediction.h>
      27                 :            : #include <rte_ether.h>
      28                 :            : #include <ethdev_driver.h>
      29                 :            : #include <rte_cycles.h>
      30                 :            : #include <rte_errno.h>
      31                 :            : #include <rte_memory.h>
      32                 :            : #include <rte_eal.h>
      33                 :            : #include <dev_driver.h>
      34                 :            : #include <bus_driver.h>
      35                 :            : #include <bus_vmbus_driver.h>
      36                 :            : #include <rte_alarm.h>
      37                 :            : 
      38                 :            : #include "hn_logs.h"
      39                 :            : #include "hn_var.h"
      40                 :            : #include "hn_rndis.h"
      41                 :            : #include "hn_nvs.h"
      42                 :            : #include "ndis.h"
      43                 :            : 
      44                 :            : #define HN_TX_OFFLOAD_CAPS (RTE_ETH_TX_OFFLOAD_IPV4_CKSUM | \
      45                 :            :                             RTE_ETH_TX_OFFLOAD_TCP_CKSUM  | \
      46                 :            :                             RTE_ETH_TX_OFFLOAD_UDP_CKSUM  | \
      47                 :            :                             RTE_ETH_TX_OFFLOAD_TCP_TSO    | \
      48                 :            :                             RTE_ETH_TX_OFFLOAD_MULTI_SEGS | \
      49                 :            :                             RTE_ETH_TX_OFFLOAD_VLAN_INSERT)
      50                 :            : 
      51                 :            : #define HN_RX_OFFLOAD_CAPS (RTE_ETH_RX_OFFLOAD_CHECKSUM | \
      52                 :            :                             RTE_ETH_RX_OFFLOAD_VLAN_STRIP | \
      53                 :            :                             RTE_ETH_RX_OFFLOAD_RSS_HASH)
      54                 :            : 
      55                 :            : #define NETVSC_ARG_LATENCY "latency"
      56                 :            : #define NETVSC_ARG_RXBREAK "rx_copybreak"
      57                 :            : #define NETVSC_ARG_TXBREAK "tx_copybreak"
      58                 :            : #define NETVSC_ARG_RX_EXTMBUF_ENABLE "rx_extmbuf_enable"
      59                 :            : 
      60                 :            : /* The max number of retry when hot adding a VF device */
      61                 :            : #define NETVSC_MAX_HOTADD_RETRY 10
      62                 :            : 
      63                 :            : struct hn_xstats_name_off {
      64                 :            :         char name[RTE_ETH_XSTATS_NAME_SIZE];
      65                 :            :         unsigned int offset;
      66                 :            : };
      67                 :            : 
      68                 :            : static const struct hn_xstats_name_off hn_stat_strings[] = {
      69                 :            :         { "good_packets",           offsetof(struct hn_stats, packets) },
      70                 :            :         { "good_bytes",             offsetof(struct hn_stats, bytes) },
      71                 :            :         { "errors",                 offsetof(struct hn_stats, errors) },
      72                 :            :         { "ring full",              offsetof(struct hn_stats, ring_full) },
      73                 :            :         { "channel full",           offsetof(struct hn_stats, channel_full) },
      74                 :            :         { "multicast_packets",      offsetof(struct hn_stats, multicast) },
      75                 :            :         { "broadcast_packets",      offsetof(struct hn_stats, broadcast) },
      76                 :            :         { "undersize_packets",      offsetof(struct hn_stats, size_bins[0]) },
      77                 :            :         { "size_64_packets",        offsetof(struct hn_stats, size_bins[1]) },
      78                 :            :         { "size_65_127_packets",    offsetof(struct hn_stats, size_bins[2]) },
      79                 :            :         { "size_128_255_packets",   offsetof(struct hn_stats, size_bins[3]) },
      80                 :            :         { "size_256_511_packets",   offsetof(struct hn_stats, size_bins[4]) },
      81                 :            :         { "size_512_1023_packets",  offsetof(struct hn_stats, size_bins[5]) },
      82                 :            :         { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
      83                 :            :         { "size_1519_max_packets",  offsetof(struct hn_stats, size_bins[7]) },
      84                 :            : };
      85                 :            : 
      86                 :            : /* The default RSS key.
      87                 :            :  * This value is the same as MLX5 so that flows will be
      88                 :            :  * received on same path for both VF and synthetic NIC.
      89                 :            :  */
      90                 :            : static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = {
      91                 :            :         0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7,
      92                 :            :         0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94,
      93                 :            :         0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1,
      94                 :            :         0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59,
      95                 :            :         0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a,
      96                 :            : };
      97                 :            : 
      98                 :            : static rte_spinlock_t netvsc_lock = RTE_SPINLOCK_INITIALIZER;
      99                 :            : struct da_cache {
     100                 :            :         LIST_ENTRY(da_cache) list;
     101                 :            :         char name[RTE_DEV_NAME_MAX_LEN];
     102                 :            :         char drv_str[];
     103                 :            : };
     104                 :            : 
     105                 :            : LIST_HEAD(da_cache_list, da_cache) da_cache_list;
     106                 :            : static unsigned int da_cache_usage;
     107                 :            : 
     108                 :            : static struct rte_eth_dev *
     109                 :          0 : eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
     110                 :            : {
     111                 :            :         struct rte_eth_dev *eth_dev;
     112                 :            :         const char *name;
     113                 :            : 
     114         [ #  # ]:          0 :         if (!dev)
     115                 :            :                 return NULL;
     116                 :            : 
     117                 :          0 :         name = dev->device.name;
     118                 :            : 
     119         [ #  # ]:          0 :         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
     120                 :          0 :                 eth_dev = rte_eth_dev_allocate(name);
     121         [ #  # ]:          0 :                 if (!eth_dev) {
     122                 :          0 :                         PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
     123                 :          0 :                         return NULL;
     124                 :            :                 }
     125                 :            : 
     126         [ #  # ]:          0 :                 if (private_data_size) {
     127                 :          0 :                         eth_dev->data->dev_private =
     128                 :          0 :                                 rte_zmalloc_socket(name, private_data_size,
     129                 :            :                                                      RTE_CACHE_LINE_SIZE, dev->device.numa_node);
     130         [ #  # ]:          0 :                         if (!eth_dev->data->dev_private) {
     131                 :          0 :                                 PMD_DRV_LOG(NOTICE, "can not allocate driver data");
     132                 :          0 :                                 rte_eth_dev_release_port(eth_dev);
     133                 :          0 :                                 return NULL;
     134                 :            :                         }
     135                 :            :                 }
     136                 :            :         } else {
     137                 :          0 :                 eth_dev = rte_eth_dev_attach_secondary(name);
     138         [ #  # ]:          0 :                 if (!eth_dev) {
     139                 :          0 :                         PMD_DRV_LOG(NOTICE, "can not attach secondary");
     140                 :          0 :                         return NULL;
     141                 :            :                 }
     142                 :            :         }
     143                 :            : 
     144                 :          0 :         eth_dev->device = &dev->device;
     145                 :            : 
     146                 :            :         /* interrupt is simulated */
     147                 :          0 :         rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_EXT);
     148                 :          0 :         eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
     149                 :          0 :         eth_dev->intr_handle = dev->intr_handle;
     150                 :            : 
     151                 :          0 :         return eth_dev;
     152                 :            : }
     153                 :            : 
     154                 :            : static void
     155                 :            : eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
     156                 :            : {
     157                 :            :         /* free ether device */
     158                 :          0 :         rte_eth_dev_release_port(eth_dev);
     159                 :            : 
     160                 :          0 :         eth_dev->device = NULL;
     161                 :          0 :         eth_dev->intr_handle = NULL;
     162                 :          0 : }
     163                 :            : 
     164                 :          0 : static int hn_set_parameter(const char *key, const char *value, void *opaque)
     165                 :            : {
     166                 :            :         struct hn_data *hv = opaque;
     167                 :          0 :         char *endp = NULL;
     168                 :            :         unsigned long v;
     169                 :            : 
     170                 :          0 :         v = strtoul(value, &endp, 0);
     171   [ #  #  #  # ]:          0 :         if (*value == '\0' || *endp != '\0') {
     172                 :          0 :                 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
     173                 :          0 :                 return -EINVAL;
     174                 :            :         }
     175                 :            : 
     176         [ #  # ]:          0 :         if (!strcmp(key, NETVSC_ARG_LATENCY)) {
     177                 :            :                 /* usec to nsec */
     178                 :          0 :                 hv->latency = v * 1000;
     179                 :          0 :                 PMD_DRV_LOG(DEBUG, "set latency %u usec", hv->latency);
     180         [ #  # ]:          0 :         } else if (!strcmp(key, NETVSC_ARG_RXBREAK)) {
     181                 :          0 :                 hv->rx_copybreak = v;
     182                 :          0 :                 PMD_DRV_LOG(DEBUG, "rx copy break set to %u",
     183                 :            :                             hv->rx_copybreak);
     184         [ #  # ]:          0 :         } else if (!strcmp(key, NETVSC_ARG_TXBREAK)) {
     185                 :          0 :                 hv->tx_copybreak = v;
     186                 :          0 :                 PMD_DRV_LOG(DEBUG, "tx copy break set to %u",
     187                 :            :                             hv->tx_copybreak);
     188         [ #  # ]:          0 :         } else if (!strcmp(key, NETVSC_ARG_RX_EXTMBUF_ENABLE)) {
     189                 :          0 :                 hv->rx_extmbuf_enable = v;
     190                 :          0 :                 PMD_DRV_LOG(DEBUG, "rx extmbuf enable set to %u",
     191                 :            :                             hv->rx_extmbuf_enable);
     192                 :            :         }
     193                 :            : 
     194                 :            :         return 0;
     195                 :            : }
     196                 :            : 
     197                 :            : /* Parse device arguments */
     198                 :          0 : static int hn_parse_args(const struct rte_eth_dev *dev)
     199                 :            : {
     200                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     201                 :          0 :         struct rte_devargs *devargs = dev->device->devargs;
     202                 :            :         static const char * const valid_keys[] = {
     203                 :            :                 NETVSC_ARG_LATENCY,
     204                 :            :                 NETVSC_ARG_RXBREAK,
     205                 :            :                 NETVSC_ARG_TXBREAK,
     206                 :            :                 NETVSC_ARG_RX_EXTMBUF_ENABLE,
     207                 :            :                 NETVSC_ARG_NUMA_AWARE,
     208                 :            :                 NULL
     209                 :            :         };
     210                 :            :         struct rte_kvargs *kvlist;
     211                 :            :         int ret;
     212                 :            : 
     213         [ #  # ]:          0 :         if (!devargs)
     214                 :            :                 return 0;
     215                 :            : 
     216                 :          0 :         PMD_INIT_LOG(DEBUG, "device args %s %s",
     217                 :            :                      devargs->name, devargs->args);
     218                 :            : 
     219                 :          0 :         kvlist = rte_kvargs_parse(devargs->args, valid_keys);
     220         [ #  # ]:          0 :         if (!kvlist) {
     221                 :          0 :                 PMD_DRV_LOG(ERR, "invalid parameters");
     222                 :          0 :                 return -EINVAL;
     223                 :            :         }
     224                 :            : 
     225                 :          0 :         ret = rte_kvargs_process(kvlist, NULL, hn_set_parameter, hv);
     226                 :          0 :         rte_kvargs_free(kvlist);
     227                 :            : 
     228                 :          0 :         return ret;
     229                 :            : }
     230                 :            : 
     231                 :            : /* Update link status.
     232                 :            :  * Note: the DPDK definition of "wait_to_complete"
     233                 :            :  *   means block this call until link is up.
     234                 :            :  *   which is not worth supporting.
     235                 :            :  */
     236                 :            : int
     237                 :          0 : hn_dev_link_update(struct rte_eth_dev *dev,
     238                 :            :                    int wait_to_complete __rte_unused)
     239                 :            : {
     240                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     241                 :            :         struct rte_eth_link link, old;
     242                 :            :         int error;
     243                 :            : 
     244                 :          0 :         old = dev->data->dev_link;
     245                 :            : 
     246                 :          0 :         error = hn_rndis_get_linkstatus(hv);
     247         [ #  # ]:          0 :         if (error)
     248                 :            :                 return error;
     249                 :            : 
     250                 :          0 :         hn_rndis_get_linkspeed(hv);
     251                 :            : 
     252                 :          0 :         link = (struct rte_eth_link) {
     253                 :            :                 .link_duplex = RTE_ETH_LINK_FULL_DUPLEX,
     254                 :            :                 .link_autoneg = RTE_ETH_LINK_SPEED_FIXED,
     255                 :          0 :                 .link_speed = hv->link_speed / 10000,
     256                 :            :         };
     257                 :            : 
     258         [ #  # ]:          0 :         if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
     259                 :          0 :                 link.link_status = RTE_ETH_LINK_UP;
     260                 :            :         else
     261                 :            :                 link.link_status = RTE_ETH_LINK_DOWN;
     262                 :            : 
     263         [ #  # ]:          0 :         if (old.link_status == link.link_status)
     264                 :            :                 return 0;
     265                 :            : 
     266         [ #  # ]:          0 :         PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
     267                 :            :                      (link.link_status == RTE_ETH_LINK_UP) ? "up" : "down");
     268                 :            : 
     269                 :            :         return rte_eth_linkstatus_set(dev, &link);
     270                 :            : }
     271                 :            : 
     272                 :          0 : static int hn_dev_info_get(struct rte_eth_dev *dev,
     273                 :            :                            struct rte_eth_dev_info *dev_info)
     274                 :            : {
     275                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     276                 :            :         int rc;
     277                 :            : 
     278                 :          0 :         dev_info->speed_capa = RTE_ETH_LINK_SPEED_10G;
     279                 :          0 :         dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
     280                 :          0 :         dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
     281                 :          0 :         dev_info->max_mac_addrs  = 1;
     282                 :            : 
     283                 :          0 :         dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
     284                 :          0 :         dev_info->flow_type_rss_offloads = hv->rss_offloads;
     285                 :          0 :         dev_info->reta_size = RTE_ETH_RSS_RETA_SIZE_128;
     286                 :            : 
     287                 :          0 :         dev_info->max_rx_queues = hv->max_queues;
     288                 :          0 :         dev_info->max_tx_queues = hv->max_queues;
     289                 :            : 
     290                 :          0 :         dev_info->tx_desc_lim.nb_min = 1;
     291                 :          0 :         dev_info->tx_desc_lim.nb_max = 4096;
     292                 :            : 
     293         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
     294                 :            :                 return 0;
     295                 :            : 
     296                 :            :         /* fills in rx and tx offload capability */
     297                 :          0 :         rc = hn_rndis_get_offload(hv, dev_info);
     298         [ #  # ]:          0 :         if (rc != 0)
     299                 :            :                 return rc;
     300                 :            : 
     301                 :            :         /* merges the offload and queues of vf */
     302                 :          0 :         return hn_vf_info_get(hv, dev_info);
     303                 :            : }
     304                 :            : 
     305                 :          0 : static int hn_rss_reta_update(struct rte_eth_dev *dev,
     306                 :            :                               struct rte_eth_rss_reta_entry64 *reta_conf,
     307                 :            :                               uint16_t reta_size)
     308                 :            : {
     309                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     310                 :            :         unsigned int i;
     311                 :            :         int err;
     312                 :            : 
     313                 :          0 :         PMD_INIT_FUNC_TRACE();
     314                 :            : 
     315         [ #  # ]:          0 :         if (reta_size != NDIS_HASH_INDCNT) {
     316                 :          0 :                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
     317                 :          0 :                 return -EINVAL;
     318                 :            :         }
     319                 :            : 
     320         [ #  # ]:          0 :         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
     321                 :          0 :                 uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
     322                 :          0 :                 uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
     323                 :          0 :                 uint64_t mask = (uint64_t)1 << shift;
     324                 :            : 
     325         [ #  # ]:          0 :                 if (reta_conf[idx].mask & mask)
     326                 :          0 :                         hv->rss_ind[i] = reta_conf[idx].reta[shift];
     327                 :            : 
     328                 :            :                 /*
     329                 :            :                  * Ensure we don't allow config that directs traffic to an Rx
     330                 :            :                  * queue that we aren't going to poll
     331                 :            :                  */
     332         [ #  # ]:          0 :                 if (hv->rss_ind[i] >=  dev->data->nb_rx_queues) {
     333                 :          0 :                         PMD_DRV_LOG(ERR, "RSS distributing traffic to invalid Rx queue");
     334                 :          0 :                         return -EINVAL;
     335                 :            :                 }
     336                 :            :         }
     337                 :            : 
     338                 :          0 :         err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
     339         [ #  # ]:          0 :         if (err) {
     340                 :          0 :                 PMD_DRV_LOG(NOTICE,
     341                 :            :                         "rss disable failed");
     342                 :          0 :                 return err;
     343                 :            :         }
     344                 :            : 
     345                 :          0 :         err = hn_rndis_conf_rss(hv, 0);
     346         [ #  # ]:          0 :         if (err) {
     347                 :          0 :                 PMD_DRV_LOG(NOTICE,
     348                 :            :                             "reta reconfig failed");
     349                 :          0 :                 return err;
     350                 :            :         }
     351                 :            : 
     352                 :          0 :         return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
     353                 :            : }
     354                 :            : 
     355                 :          0 : static int hn_rss_reta_query(struct rte_eth_dev *dev,
     356                 :            :                              struct rte_eth_rss_reta_entry64 *reta_conf,
     357                 :            :                              uint16_t reta_size)
     358                 :            : {
     359                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     360                 :            :         unsigned int i;
     361                 :            : 
     362                 :          0 :         PMD_INIT_FUNC_TRACE();
     363                 :            : 
     364         [ #  # ]:          0 :         if (reta_size != NDIS_HASH_INDCNT) {
     365                 :          0 :                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
     366                 :          0 :                 return -EINVAL;
     367                 :            :         }
     368                 :            : 
     369         [ #  # ]:          0 :         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
     370                 :          0 :                 uint16_t idx = i / RTE_ETH_RETA_GROUP_SIZE;
     371                 :          0 :                 uint16_t shift = i % RTE_ETH_RETA_GROUP_SIZE;
     372                 :          0 :                 uint64_t mask = (uint64_t)1 << shift;
     373                 :            : 
     374         [ #  # ]:          0 :                 if (reta_conf[idx].mask & mask)
     375                 :          0 :                         reta_conf[idx].reta[shift] = hv->rss_ind[i];
     376                 :            :         }
     377                 :            :         return 0;
     378                 :            : }
     379                 :            : 
     380                 :          0 : static void hn_rss_hash_init(struct hn_data *hv,
     381                 :            :                              const struct rte_eth_rss_conf *rss_conf)
     382                 :            : {
     383                 :            :         /* Convert from DPDK RSS hash flags to NDIS hash flags */
     384                 :          0 :         hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
     385                 :            : 
     386         [ #  # ]:          0 :         if (rss_conf->rss_hf & RTE_ETH_RSS_IPV4)
     387                 :          0 :                 hv->rss_hash |= NDIS_HASH_IPV4;
     388         [ #  # ]:          0 :         if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
     389                 :          0 :                 hv->rss_hash |= NDIS_HASH_TCP_IPV4;
     390         [ #  # ]:          0 :         if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6)
     391                 :          0 :                 hv->rss_hash |=  NDIS_HASH_IPV6;
     392         [ #  # ]:          0 :         if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_EX)
     393                 :          0 :                 hv->rss_hash |=  NDIS_HASH_IPV6_EX;
     394         [ #  # ]:          0 :         if (rss_conf->rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_TCP)
     395                 :          0 :                 hv->rss_hash |= NDIS_HASH_TCP_IPV6;
     396         [ #  # ]:          0 :         if (rss_conf->rss_hf & RTE_ETH_RSS_IPV6_TCP_EX)
     397                 :          0 :                 hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
     398                 :            : 
     399         [ #  # ]:          0 :         memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
     400                 :            :                NDIS_HASH_KEYSIZE_TOEPLITZ);
     401                 :          0 : }
     402                 :            : 
     403                 :          0 : static int hn_rss_hash_update(struct rte_eth_dev *dev,
     404                 :            :                               struct rte_eth_rss_conf *rss_conf)
     405                 :            : {
     406                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     407                 :            :         int err;
     408                 :            : 
     409                 :          0 :         PMD_INIT_FUNC_TRACE();
     410                 :            : 
     411                 :          0 :         err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
     412         [ #  # ]:          0 :         if (err) {
     413                 :          0 :                 PMD_DRV_LOG(NOTICE,
     414                 :            :                             "rss disable failed");
     415                 :          0 :                 return err;
     416                 :            :         }
     417                 :            : 
     418                 :          0 :         hn_rss_hash_init(hv, rss_conf);
     419                 :            : 
     420         [ #  # ]:          0 :         if (rss_conf->rss_hf != 0) {
     421                 :          0 :                 err = hn_rndis_conf_rss(hv, 0);
     422         [ #  # ]:          0 :                 if (err) {
     423                 :          0 :                         PMD_DRV_LOG(NOTICE,
     424                 :            :                                     "rss reconfig failed (RSS disabled)");
     425                 :          0 :                         return err;
     426                 :            :                 }
     427                 :            :         }
     428                 :            : 
     429                 :          0 :         return hn_vf_rss_hash_update(dev, rss_conf);
     430                 :            : }
     431                 :            : 
     432                 :          0 : static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
     433                 :            :                                 struct rte_eth_rss_conf *rss_conf)
     434                 :            : {
     435                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     436                 :            : 
     437                 :          0 :         PMD_INIT_FUNC_TRACE();
     438                 :            : 
     439         [ #  # ]:          0 :         if (hv->ndis_ver < NDIS_VERSION_6_20) {
     440                 :          0 :                 PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
     441                 :          0 :                 return -EOPNOTSUPP;
     442                 :            :         }
     443                 :            : 
     444                 :          0 :         rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ;
     445         [ #  # ]:          0 :         if (rss_conf->rss_key)
     446                 :          0 :                 memcpy(rss_conf->rss_key, hv->rss_key,
     447                 :            :                        NDIS_HASH_KEYSIZE_TOEPLITZ);
     448                 :            : 
     449                 :          0 :         rss_conf->rss_hf = 0;
     450         [ #  # ]:          0 :         if (hv->rss_hash & NDIS_HASH_IPV4)
     451                 :          0 :                 rss_conf->rss_hf |= RTE_ETH_RSS_IPV4;
     452                 :            : 
     453         [ #  # ]:          0 :         if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
     454                 :          0 :                 rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_TCP;
     455                 :            : 
     456         [ #  # ]:          0 :         if (hv->rss_hash & NDIS_HASH_IPV6)
     457                 :          0 :                 rss_conf->rss_hf |= RTE_ETH_RSS_IPV6;
     458                 :            : 
     459         [ #  # ]:          0 :         if (hv->rss_hash & NDIS_HASH_IPV6_EX)
     460                 :          0 :                 rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_EX;
     461                 :            : 
     462         [ #  # ]:          0 :         if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
     463                 :          0 :                 rss_conf->rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_TCP;
     464                 :            : 
     465         [ #  # ]:          0 :         if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
     466                 :          0 :                 rss_conf->rss_hf |= RTE_ETH_RSS_IPV6_TCP_EX;
     467                 :            : 
     468                 :            :         return 0;
     469                 :            : }
     470                 :            : 
     471                 :            : static int
     472                 :          0 : hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
     473                 :            : {
     474                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     475                 :            : 
     476                 :          0 :         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
     477                 :          0 :         return hn_vf_promiscuous_enable(dev);
     478                 :            : }
     479                 :            : 
     480                 :            : static int
     481                 :          0 : hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
     482                 :            : {
     483                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     484                 :            :         uint32_t filter;
     485                 :            : 
     486                 :            :         filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
     487         [ #  # ]:          0 :         if (dev->data->all_multicast)
     488                 :            :                 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
     489                 :          0 :         hn_rndis_set_rxfilter(hv, filter);
     490                 :          0 :         return hn_vf_promiscuous_disable(dev);
     491                 :            : }
     492                 :            : 
     493                 :            : static int
     494                 :          0 : hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
     495                 :            : {
     496                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     497                 :            : 
     498                 :          0 :         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
     499                 :            :                               NDIS_PACKET_TYPE_ALL_MULTICAST |
     500                 :            :                         NDIS_PACKET_TYPE_BROADCAST);
     501                 :          0 :         return hn_vf_allmulticast_enable(dev);
     502                 :            : }
     503                 :            : 
     504                 :            : static int
     505                 :          0 : hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
     506                 :            : {
     507                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     508                 :            : 
     509                 :          0 :         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
     510                 :            :                              NDIS_PACKET_TYPE_BROADCAST);
     511                 :          0 :         return hn_vf_allmulticast_disable(dev);
     512                 :            : }
     513                 :            : 
     514                 :            : static int
     515                 :          0 : hn_dev_mc_addr_list(struct rte_eth_dev *dev,
     516                 :            :                      struct rte_ether_addr *mc_addr_set,
     517                 :            :                      uint32_t nb_mc_addr)
     518                 :            : {
     519                 :            :         /* No filtering on the synthetic path, but can do it on VF */
     520                 :          0 :         return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
     521                 :            : }
     522                 :            : 
     523                 :            : /* Setup shared rx/tx queue data */
     524                 :          0 : static int hn_subchan_configure(struct hn_data *hv,
     525                 :            :                                 uint32_t subchan)
     526                 :            : {
     527                 :            :         struct vmbus_channel *primary = hn_primary_chan(hv);
     528                 :            :         int err;
     529                 :            :         unsigned int retry = 0;
     530                 :            : 
     531                 :          0 :         PMD_DRV_LOG(DEBUG,
     532                 :            :                     "open %u subchannels", subchan);
     533                 :            : 
     534                 :            :         /* Send create sub channels command */
     535                 :          0 :         err = hn_nvs_alloc_subchans(hv, &subchan);
     536         [ #  # ]:          0 :         if (err)
     537                 :            :                 return  err;
     538                 :            : 
     539         [ #  # ]:          0 :         while (subchan > 0) {
     540                 :            :                 struct vmbus_channel *new_sc;
     541                 :            :                 uint16_t chn_index;
     542                 :            : 
     543                 :          0 :                 err = rte_vmbus_subchan_open(primary, &new_sc);
     544   [ #  #  #  # ]:          0 :                 if (err == -ENOENT && ++retry < 1000) {
     545                 :            :                         /* This can happen if not ready yet */
     546                 :            :                         rte_delay_ms(10);
     547                 :          0 :                         continue;
     548                 :            :                 }
     549                 :            : 
     550         [ #  # ]:          0 :                 if (err) {
     551                 :          0 :                         PMD_DRV_LOG(ERR,
     552                 :            :                                     "open subchannel failed: %d", err);
     553                 :          0 :                         return err;
     554                 :            :                 }
     555                 :            : 
     556                 :          0 :                 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
     557                 :            : 
     558                 :            :                 retry = 0;
     559                 :          0 :                 chn_index = rte_vmbus_sub_channel_index(new_sc);
     560   [ #  #  #  # ]:          0 :                 if (chn_index == 0 || chn_index > hv->max_queues) {
     561                 :          0 :                         PMD_DRV_LOG(ERR,
     562                 :            :                                     "Invalid subchannel offermsg channel %u",
     563                 :            :                                     chn_index);
     564                 :          0 :                         return -EIO;
     565                 :            :                 }
     566                 :            : 
     567                 :          0 :                 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
     568                 :          0 :                 hv->channels[chn_index] = new_sc;
     569                 :          0 :                 --subchan;
     570                 :            :         }
     571                 :            : 
     572                 :            :         return err;
     573                 :            : }
     574                 :            : 
     575                 :          0 : static void netvsc_hotplug_retry(void *args)
     576                 :            : {
     577                 :            :         int ret;
     578                 :            :         struct hv_hotadd_context *hot_ctx = args;
     579                 :          0 :         struct hn_data *hv = hot_ctx->hv;
     580                 :          0 :         struct rte_eth_dev *dev = &rte_eth_devices[hv->port_id];
     581                 :            :         struct rte_devargs *d = &hot_ctx->da;
     582                 :            :         char buf[256];
     583                 :            : 
     584                 :            :         DIR *di = NULL;
     585                 :            :         struct dirent *dir;
     586                 :            :         struct ifreq req;
     587                 :            :         struct rte_ether_addr eth_addr;
     588                 :            :         int s;
     589                 :            : 
     590                 :          0 :         PMD_DRV_LOG(DEBUG, "%s: retry count %d",
     591                 :            :                     __func__, hot_ctx->eal_hot_plug_retry);
     592                 :            : 
     593         [ #  # ]:          0 :         if (hot_ctx->eal_hot_plug_retry++ > NETVSC_MAX_HOTADD_RETRY) {
     594                 :          0 :                 PMD_DRV_LOG(NOTICE, "Failed to parse PCI device retry=%d",
     595                 :            :                             hot_ctx->eal_hot_plug_retry);
     596                 :          0 :                 goto free_hotadd_ctx;
     597                 :            :         }
     598                 :            : 
     599                 :          0 :         snprintf(buf, sizeof(buf), "/sys/bus/pci/devices/%s/net", d->name);
     600                 :          0 :         di = opendir(buf);
     601         [ #  # ]:          0 :         if (!di) {
     602                 :          0 :                 PMD_DRV_LOG(DEBUG, "%s: can't open directory %s, "
     603                 :            :                             "retrying in 1 second", __func__, buf);
     604                 :            :                 /* The device is still being initialized, retry after 1 second */
     605                 :          0 :                 rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hot_ctx);
     606                 :          0 :                 return;
     607                 :            :         }
     608                 :            : 
     609         [ #  # ]:          0 :         while ((dir = readdir(di))) {
     610                 :            :                 /* Skip . and .. directories */
     611   [ #  #  #  # ]:          0 :                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
     612                 :          0 :                         continue;
     613                 :            : 
     614                 :            :                 /* trying to get mac address if this is a network device*/
     615                 :          0 :                 s = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
     616         [ #  # ]:          0 :                 if (s == -1) {
     617                 :          0 :                         PMD_DRV_LOG(ERR, "Failed to create socket errno %d",
     618                 :            :                                     errno);
     619                 :          0 :                         break;
     620                 :            :                 }
     621                 :            :                 strlcpy(req.ifr_name, dir->d_name, sizeof(req.ifr_name));
     622                 :          0 :                 ret = ioctl(s, SIOCGIFHWADDR, &req);
     623                 :          0 :                 close(s);
     624         [ #  # ]:          0 :                 if (ret == -1) {
     625                 :          0 :                         PMD_DRV_LOG(ERR,
     626                 :            :                                     "Failed to send SIOCGIFHWADDR for device %s",
     627                 :            :                                     dir->d_name);
     628                 :          0 :                         break;
     629                 :            :                 }
     630         [ #  # ]:          0 :                 if (req.ifr_hwaddr.sa_family != ARPHRD_ETHER)
     631                 :          0 :                         continue;
     632                 :            : 
     633                 :            :                 memcpy(eth_addr.addr_bytes, req.ifr_hwaddr.sa_data,
     634                 :            :                        RTE_DIM(eth_addr.addr_bytes));
     635                 :            : 
     636         [ #  # ]:          0 :                 if (rte_is_same_ether_addr(&eth_addr, dev->data->mac_addrs)) {
     637                 :            :                         struct da_cache *cache;
     638                 :            :                         char *drv_str = NULL;
     639                 :            : 
     640                 :            :                         rte_spinlock_lock(&netvsc_lock);
     641                 :            : 
     642         [ #  # ]:          0 :                         LIST_FOREACH(cache, &da_cache_list, list) {
     643         [ #  # ]:          0 :                                 if (strcmp(cache->name, d->name) == 0)
     644                 :            :                                         break;
     645                 :            :                         }
     646                 :            : 
     647         [ #  # ]:          0 :                         if (cache)
     648                 :          0 :                                 drv_str = strdup(cache->drv_str);
     649                 :            : 
     650                 :            :                         rte_spinlock_unlock(&netvsc_lock);
     651                 :            : 
     652         [ #  # ]:          0 :                         PMD_DRV_LOG(NOTICE,
     653                 :            :                                     "Found matching MAC address, adding device %s network name %s args %s",
     654                 :            :                                     d->name, dir->d_name, drv_str ? drv_str : "");
     655                 :            : 
     656                 :            :                         /* If this device has been hot removed from this
     657                 :            :                          * parent device, restore its args.
     658                 :            :                          */
     659                 :          0 :                         ret = rte_eal_hotplug_add(d->bus->name, d->name, drv_str ? drv_str : "");
     660         [ #  # ]:          0 :                         if (ret) {
     661                 :          0 :                                 PMD_DRV_LOG(ERR,
     662                 :            :                                             "Failed to add PCI device %s",
     663                 :            :                                             d->name);
     664                 :            :                         }
     665                 :            : 
     666                 :          0 :                         free(drv_str);
     667                 :            : 
     668                 :          0 :                         break;
     669                 :            :                 }
     670                 :            :         }
     671                 :            : 
     672                 :          0 : free_hotadd_ctx:
     673                 :            :         if (di)
     674                 :          0 :                 closedir(di);
     675                 :            : 
     676                 :          0 :         rte_spinlock_lock(&hv->hotadd_lock);
     677         [ #  # ]:          0 :         LIST_REMOVE(hot_ctx, list);
     678                 :            :         rte_spinlock_unlock(&hv->hotadd_lock);
     679                 :            : 
     680                 :          0 :         free(hot_ctx);
     681                 :            : }
     682                 :            : 
     683                 :            : static void
     684                 :          0 : netvsc_hotadd_callback(const char *device_name, enum rte_dev_event_type type,
     685                 :            :                        void *arg)
     686                 :            : {
     687                 :            :         struct hn_data *hv = arg;
     688                 :            :         struct hv_hotadd_context *hot_ctx;
     689                 :            :         struct rte_devargs *d;
     690                 :            :         int ret;
     691                 :            : 
     692                 :          0 :         PMD_DRV_LOG(INFO, "Device notification type=%d device_name=%s",
     693                 :            :                     type, device_name);
     694                 :            : 
     695         [ #  # ]:          0 :         switch (type) {
     696                 :          0 :         case RTE_DEV_EVENT_ADD:
     697                 :            :                 /* if we already has a VF, don't check on hot add */
     698         [ #  # ]:          0 :                 if (hv->vf_ctx.vf_state > vf_removed)
     699                 :            :                         break;
     700                 :            : 
     701                 :          0 :                 hot_ctx = calloc(1, sizeof(*hot_ctx));
     702                 :            : 
     703         [ #  # ]:          0 :                 if (!hot_ctx) {
     704                 :          0 :                         PMD_DRV_LOG(ERR, "Failed to allocate hotadd context");
     705                 :          0 :                         return;
     706                 :            :                 }
     707                 :            : 
     708                 :          0 :                 hot_ctx->hv = hv;
     709                 :          0 :                 d = &hot_ctx->da;
     710                 :            : 
     711                 :          0 :                 ret = rte_devargs_parse(d, device_name);
     712         [ #  # ]:          0 :                 if (ret) {
     713                 :          0 :                         PMD_DRV_LOG(ERR,
     714                 :            :                                     "devargs parsing failed ret=%d", ret);
     715                 :          0 :                         goto free_ctx;
     716                 :            :                 }
     717                 :            : 
     718         [ #  # ]:          0 :                 if (!strcmp(d->bus->name, "pci")) {
     719                 :            :                         /* Start the process of figuring out if this
     720                 :            :                          * PCI device is a VF device
     721                 :            :                          */
     722                 :          0 :                         rte_spinlock_lock(&hv->hotadd_lock);
     723         [ #  # ]:          0 :                         LIST_INSERT_HEAD(&hv->hotadd_list, hot_ctx, list);
     724                 :            :                         rte_spinlock_unlock(&hv->hotadd_lock);
     725                 :          0 :                         rte_eal_alarm_set(1000000, netvsc_hotplug_retry, hot_ctx);
     726                 :          0 :                         return;
     727                 :            :                 }
     728                 :            : 
     729                 :            :                 /* We will switch to VF on RDNIS configure message
     730                 :            :                  * sent from VSP
     731                 :            :                  */
     732                 :          0 : free_ctx:
     733                 :          0 :                 free(hot_ctx);
     734                 :          0 :                 break;
     735                 :            : 
     736                 :            :         default:
     737                 :            :                 break;
     738                 :            :         }
     739                 :            : }
     740                 :            : 
     741                 :          0 : static int hn_dev_configure(struct rte_eth_dev *dev)
     742                 :            : {
     743                 :          0 :         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
     744                 :          0 :         struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
     745                 :            :         const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
     746                 :            :         const struct rte_eth_txmode *txmode = &dev_conf->txmode;
     747                 :          0 :         struct hn_data *hv = dev->data->dev_private;
     748                 :            :         uint64_t unsupported;
     749                 :            :         int i, err, subchan;
     750                 :            : 
     751                 :          0 :         PMD_INIT_FUNC_TRACE();
     752                 :            : 
     753         [ #  # ]:          0 :         if (dev_conf->rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG)
     754                 :          0 :                 dev_conf->rxmode.offloads |= RTE_ETH_RX_OFFLOAD_RSS_HASH;
     755                 :            : 
     756                 :          0 :         unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
     757         [ #  # ]:          0 :         if (unsupported) {
     758                 :          0 :                 PMD_DRV_LOG(NOTICE,
     759                 :            :                             "unsupported TX offload: %#" PRIx64,
     760                 :            :                             unsupported);
     761                 :          0 :                 return -EINVAL;
     762                 :            :         }
     763                 :            : 
     764                 :          0 :         unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
     765         [ #  # ]:          0 :         if (unsupported) {
     766                 :          0 :                 PMD_DRV_LOG(NOTICE,
     767                 :            :                             "unsupported RX offload: %#" PRIx64,
     768                 :            :                             rxmode->offloads);
     769                 :          0 :                 return -EINVAL;
     770                 :            :         }
     771                 :            : 
     772                 :          0 :         hv->vlan_strip = !!(rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_STRIP);
     773                 :            : 
     774                 :          0 :         err = hn_rndis_conf_offload(hv, txmode->offloads,
     775                 :            :                                     rxmode->offloads);
     776         [ #  # ]:          0 :         if (err) {
     777                 :          0 :                 PMD_DRV_LOG(NOTICE,
     778                 :            :                             "offload configure failed");
     779                 :          0 :                 return err;
     780                 :            :         }
     781                 :            : 
     782                 :          0 :         hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
     783                 :            :                                  dev->data->nb_tx_queues);
     784                 :            : 
     785         [ #  # ]:          0 :         for (i = 0; i < NDIS_HASH_INDCNT; i++)
     786                 :          0 :                 hv->rss_ind[i] = i % dev->data->nb_rx_queues;
     787                 :            : 
     788                 :          0 :         hn_rss_hash_init(hv, rss_conf);
     789                 :            : 
     790                 :          0 :         subchan = hv->num_queues - 1;
     791         [ #  # ]:          0 :         if (subchan > 0) {
     792                 :          0 :                 err = hn_subchan_configure(hv, subchan);
     793         [ #  # ]:          0 :                 if (err) {
     794                 :          0 :                         PMD_DRV_LOG(NOTICE,
     795                 :            :                                     "subchannel configuration failed");
     796                 :          0 :                         return err;
     797                 :            :                 }
     798                 :            : 
     799                 :          0 :                 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
     800         [ #  # ]:          0 :                 if (err) {
     801                 :          0 :                         PMD_DRV_LOG(NOTICE,
     802                 :            :                                 "rss disable failed");
     803                 :          0 :                         return err;
     804                 :            :                 }
     805                 :            : 
     806         [ #  # ]:          0 :                 if (rss_conf->rss_hf != 0) {
     807                 :          0 :                         err = hn_rndis_conf_rss(hv, 0);
     808         [ #  # ]:          0 :                         if (err) {
     809                 :          0 :                                 PMD_DRV_LOG(NOTICE,
     810                 :            :                                             "initial RSS config failed");
     811                 :          0 :                                 return err;
     812                 :            :                         }
     813                 :            :                 }
     814                 :            :         }
     815                 :            : 
     816                 :          0 :         return hn_vf_configure_locked(dev, dev_conf);
     817                 :            : }
     818                 :            : 
     819                 :          0 : static int hn_dev_stats_get(struct rte_eth_dev *dev,
     820                 :            :                             struct rte_eth_stats *stats)
     821                 :            : {
     822                 :            :         unsigned int i;
     823                 :            : 
     824                 :          0 :         hn_vf_stats_get(dev, stats);
     825                 :            : 
     826         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     827                 :          0 :                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
     828                 :            : 
     829         [ #  # ]:          0 :                 if (!txq)
     830                 :          0 :                         continue;
     831                 :            : 
     832                 :          0 :                 stats->opackets += txq->stats.packets;
     833                 :          0 :                 stats->obytes += txq->stats.bytes;
     834                 :          0 :                 stats->oerrors += txq->stats.errors;
     835                 :            : 
     836         [ #  # ]:          0 :                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
     837                 :          0 :                         stats->q_opackets[i] += txq->stats.packets;
     838                 :          0 :                         stats->q_obytes[i] += txq->stats.bytes;
     839                 :            :                 }
     840                 :            :         }
     841                 :            : 
     842         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
     843                 :          0 :                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
     844                 :            : 
     845         [ #  # ]:          0 :                 if (!rxq)
     846                 :          0 :                         continue;
     847                 :            : 
     848                 :          0 :                 stats->ipackets += rxq->stats.packets;
     849                 :          0 :                 stats->ibytes += rxq->stats.bytes;
     850                 :          0 :                 stats->ierrors += rxq->stats.errors;
     851                 :          0 :                 stats->imissed += rxq->stats.ring_full;
     852                 :            : 
     853         [ #  # ]:          0 :                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
     854                 :          0 :                         stats->q_ipackets[i] += rxq->stats.packets;
     855                 :          0 :                         stats->q_ibytes[i] += rxq->stats.bytes;
     856                 :            :                 }
     857                 :            :         }
     858                 :            : 
     859                 :          0 :         stats->rx_nombuf += dev->data->rx_mbuf_alloc_failed;
     860                 :          0 :         return 0;
     861                 :            : }
     862                 :            : 
     863                 :            : static int
     864                 :          0 : hn_dev_stats_reset(struct rte_eth_dev *dev)
     865                 :            : {
     866                 :            :         unsigned int i;
     867                 :            : 
     868                 :          0 :         PMD_INIT_FUNC_TRACE();
     869                 :            : 
     870         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     871                 :          0 :                 struct hn_tx_queue *txq = dev->data->tx_queues[i];
     872                 :            : 
     873         [ #  # ]:          0 :                 if (!txq)
     874                 :          0 :                         continue;
     875                 :          0 :                 memset(&txq->stats, 0, sizeof(struct hn_stats));
     876                 :            :         }
     877                 :            : 
     878         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
     879                 :          0 :                 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
     880                 :            : 
     881         [ #  # ]:          0 :                 if (!rxq)
     882                 :          0 :                         continue;
     883                 :            : 
     884                 :          0 :                 memset(&rxq->stats, 0, sizeof(struct hn_stats));
     885                 :            :         }
     886                 :            : 
     887                 :          0 :         return 0;
     888                 :            : }
     889                 :            : 
     890                 :            : static int
     891                 :          0 : hn_dev_xstats_reset(struct rte_eth_dev *dev)
     892                 :            : {
     893                 :            :         int ret;
     894                 :            : 
     895                 :          0 :         ret = hn_dev_stats_reset(dev);
     896         [ #  # ]:          0 :         if (ret != 0)
     897                 :            :                 return 0;
     898                 :            : 
     899                 :          0 :         return hn_vf_xstats_reset(dev);
     900                 :            : }
     901                 :            : 
     902                 :            : static int
     903                 :          0 : hn_dev_xstats_count(struct rte_eth_dev *dev)
     904                 :            : {
     905                 :            :         int ret, count;
     906                 :            : 
     907                 :          0 :         count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
     908                 :          0 :         count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
     909                 :            : 
     910                 :          0 :         ret = hn_vf_xstats_get_names(dev, NULL, 0);
     911         [ #  # ]:          0 :         if (ret < 0)
     912                 :            :                 return ret;
     913                 :            : 
     914                 :          0 :         return count + ret;
     915                 :            : }
     916                 :            : 
     917                 :            : static int
     918                 :          0 : hn_dev_xstats_get_names(struct rte_eth_dev *dev,
     919                 :            :                         struct rte_eth_xstat_name *xstats_names,
     920                 :            :                         unsigned int limit)
     921                 :            : {
     922                 :            :         unsigned int i, t, count = 0;
     923                 :            :         int ret;
     924                 :            : 
     925         [ #  # ]:          0 :         if (!xstats_names)
     926                 :          0 :                 return hn_dev_xstats_count(dev);
     927                 :            : 
     928                 :            :         /* Note: limit checked in rte_eth_xstats_names() */
     929         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     930                 :          0 :                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
     931                 :            : 
     932         [ #  # ]:          0 :                 if (!txq)
     933                 :          0 :                         continue;
     934                 :            : 
     935         [ #  # ]:          0 :                 if (count >= limit)
     936                 :            :                         break;
     937                 :            : 
     938         [ #  # ]:          0 :                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
     939                 :          0 :                         snprintf(xstats_names[count++].name,
     940                 :            :                                  RTE_ETH_XSTATS_NAME_SIZE,
     941                 :          0 :                                  "tx_q%u_%s", i, hn_stat_strings[t].name);
     942                 :            :         }
     943                 :            : 
     944         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++)  {
     945                 :          0 :                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
     946                 :            : 
     947         [ #  # ]:          0 :                 if (!rxq)
     948                 :          0 :                         continue;
     949                 :            : 
     950         [ #  # ]:          0 :                 if (count >= limit)
     951                 :            :                         break;
     952                 :            : 
     953         [ #  # ]:          0 :                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
     954                 :          0 :                         snprintf(xstats_names[count++].name,
     955                 :            :                                  RTE_ETH_XSTATS_NAME_SIZE,
     956                 :            :                                  "rx_q%u_%s", i,
     957                 :          0 :                                  hn_stat_strings[t].name);
     958                 :            :         }
     959                 :            : 
     960                 :          0 :         ret = hn_vf_xstats_get_names(dev, xstats_names + count,
     961                 :            :                                      limit - count);
     962         [ #  # ]:          0 :         if (ret < 0)
     963                 :            :                 return ret;
     964                 :            : 
     965                 :          0 :         return count + ret;
     966                 :            : }
     967                 :            : 
     968                 :            : static int
     969                 :          0 : hn_dev_xstats_get(struct rte_eth_dev *dev,
     970                 :            :                   struct rte_eth_xstat *xstats,
     971                 :            :                   unsigned int n)
     972                 :            : {
     973                 :            :         unsigned int i, t, count = 0;
     974                 :          0 :         const unsigned int nstats = hn_dev_xstats_count(dev);
     975                 :            :         const char *stats;
     976                 :            :         int ret;
     977                 :            : 
     978                 :          0 :         PMD_INIT_FUNC_TRACE();
     979                 :            : 
     980         [ #  # ]:          0 :         if (n < nstats)
     981                 :            :                 return nstats;
     982                 :            : 
     983         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_tx_queues; i++) {
     984                 :          0 :                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
     985                 :            : 
     986         [ #  # ]:          0 :                 if (!txq)
     987                 :          0 :                         continue;
     988                 :            : 
     989                 :          0 :                 stats = (const char *)&txq->stats;
     990         [ #  # ]:          0 :                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
     991                 :          0 :                         xstats[count].id = count;
     992                 :          0 :                         xstats[count].value = *(const uint64_t *)
     993                 :          0 :                                 (stats + hn_stat_strings[t].offset);
     994                 :            :                 }
     995                 :            :         }
     996                 :            : 
     997         [ #  # ]:          0 :         for (i = 0; i < dev->data->nb_rx_queues; i++) {
     998                 :          0 :                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
     999                 :            : 
    1000         [ #  # ]:          0 :                 if (!rxq)
    1001                 :          0 :                         continue;
    1002                 :            : 
    1003                 :          0 :                 stats = (const char *)&rxq->stats;
    1004         [ #  # ]:          0 :                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
    1005                 :          0 :                         xstats[count].id = count;
    1006                 :          0 :                         xstats[count].value = *(const uint64_t *)
    1007                 :          0 :                                 (stats + hn_stat_strings[t].offset);
    1008                 :            :                 }
    1009                 :            :         }
    1010                 :            : 
    1011                 :          0 :         ret = hn_vf_xstats_get(dev, xstats, count, n);
    1012         [ #  # ]:          0 :         if (ret < 0)
    1013                 :            :                 return ret;
    1014                 :            : 
    1015                 :          0 :         return count + ret;
    1016                 :            : }
    1017                 :            : 
    1018                 :            : static int
    1019                 :          0 : hn_dev_start(struct rte_eth_dev *dev)
    1020                 :            : {
    1021                 :          0 :         struct hn_data *hv = dev->data->dev_private;
    1022                 :            :         int i, error;
    1023                 :            : 
    1024                 :          0 :         PMD_INIT_FUNC_TRACE();
    1025                 :            : 
    1026                 :            :         /* Register to monitor hot plug events */
    1027                 :          0 :         error = rte_dev_event_callback_register(NULL, netvsc_hotadd_callback,
    1028                 :            :                                                 hv);
    1029         [ #  # ]:          0 :         if (error) {
    1030                 :          0 :                 PMD_DRV_LOG(ERR, "failed to register device event callback");
    1031                 :          0 :                 return error;
    1032                 :            :         }
    1033                 :            : 
    1034                 :          0 :         error = hn_rndis_set_rxfilter(hv,
    1035                 :            :                                       NDIS_PACKET_TYPE_BROADCAST |
    1036                 :            :                                       NDIS_PACKET_TYPE_ALL_MULTICAST |
    1037                 :            :                                       NDIS_PACKET_TYPE_DIRECTED);
    1038         [ #  # ]:          0 :         if (error)
    1039                 :            :                 return error;
    1040                 :            : 
    1041                 :          0 :         error = hn_vf_start(dev);
    1042         [ #  # ]:          0 :         if (error)
    1043                 :          0 :                 hn_rndis_set_rxfilter(hv, 0);
    1044                 :            : 
    1045                 :            :         /* Initialize Link state */
    1046         [ #  # ]:          0 :         if (error == 0)
    1047                 :          0 :                 hn_dev_link_update(dev, 0);
    1048                 :            : 
    1049         [ #  # ]:          0 :         for (i = 0; i < hv->num_queues; i++) {
    1050                 :          0 :                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
    1051                 :          0 :                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
    1052                 :            :         }
    1053                 :            : 
    1054                 :            :         return error;
    1055                 :            : }
    1056                 :            : 
    1057                 :            : static int
    1058                 :          0 : hn_dev_stop(struct rte_eth_dev *dev)
    1059                 :            : {
    1060                 :          0 :         struct hn_data *hv = dev->data->dev_private;
    1061                 :            :         int i, ret;
    1062                 :            : 
    1063                 :          0 :         PMD_INIT_FUNC_TRACE();
    1064                 :          0 :         dev->data->dev_started = 0;
    1065                 :            : 
    1066                 :          0 :         rte_dev_event_callback_unregister(NULL, netvsc_hotadd_callback, hv);
    1067                 :          0 :         hn_rndis_set_rxfilter(hv, 0);
    1068                 :          0 :         ret = hn_vf_stop(dev);
    1069                 :            : 
    1070         [ #  # ]:          0 :         for (i = 0; i < hv->num_queues; i++) {
    1071                 :          0 :                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
    1072                 :          0 :                 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
    1073                 :            :         }
    1074                 :            : 
    1075                 :          0 :         return ret;
    1076                 :            : }
    1077                 :            : 
    1078                 :            : static int
    1079                 :          0 : hn_dev_close(struct rte_eth_dev *dev)
    1080                 :            : {
    1081                 :            :         int ret;
    1082                 :          0 :         struct hn_data *hv = dev->data->dev_private;
    1083                 :            :         struct hv_hotadd_context *hot_ctx;
    1084                 :            : 
    1085                 :          0 :         PMD_INIT_FUNC_TRACE();
    1086         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
    1087                 :            :                 return 0;
    1088                 :            : 
    1089                 :          0 :         rte_spinlock_lock(&hv->hotadd_lock);
    1090         [ #  # ]:          0 :         while (!LIST_EMPTY(&hv->hotadd_list)) {
    1091                 :            :                 hot_ctx = LIST_FIRST(&hv->hotadd_list);
    1092                 :          0 :                 rte_eal_alarm_cancel(netvsc_hotplug_retry, hot_ctx);
    1093         [ #  # ]:          0 :                 LIST_REMOVE(hot_ctx, list);
    1094                 :          0 :                 free(hot_ctx);
    1095                 :            :         }
    1096                 :            :         rte_spinlock_unlock(&hv->hotadd_lock);
    1097                 :            : 
    1098                 :          0 :         ret = hn_vf_close(dev);
    1099                 :          0 :         hn_dev_free_queues(dev);
    1100                 :            : 
    1101                 :          0 :         return ret;
    1102                 :            : }
    1103                 :            : 
    1104                 :            : /*
    1105                 :            :  * Setup connection between PMD and kernel.
    1106                 :            :  */
    1107                 :            : static int
    1108                 :          0 : hn_attach(struct hn_data *hv, unsigned int mtu)
    1109                 :            : {
    1110                 :            :         int error;
    1111                 :            : 
    1112                 :            :         /* Attach NVS */
    1113                 :          0 :         error = hn_nvs_attach(hv, mtu);
    1114         [ #  # ]:          0 :         if (error)
    1115                 :          0 :                 goto failed_nvs;
    1116                 :            : 
    1117                 :            :         /* Attach RNDIS */
    1118                 :          0 :         error = hn_rndis_attach(hv);
    1119         [ #  # ]:          0 :         if (error)
    1120                 :          0 :                 goto failed_rndis;
    1121                 :            : 
    1122                 :            :         /*
    1123                 :            :          * NOTE:
    1124                 :            :          * Under certain conditions on certain versions of Hyper-V,
    1125                 :            :          * the RNDIS rxfilter is _not_ zero on the hypervisor side
    1126                 :            :          * after the successful RNDIS initialization.
    1127                 :            :          */
    1128                 :          0 :         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
    1129                 :          0 :         return 0;
    1130                 :            : failed_rndis:
    1131                 :          0 :         hn_nvs_detach(hv);
    1132                 :            : failed_nvs:
    1133                 :            :         return error;
    1134                 :            : }
    1135                 :            : 
    1136                 :            : static void
    1137                 :            : hn_detach(struct hn_data *hv)
    1138                 :            : {
    1139                 :          0 :         hn_nvs_detach(hv);
    1140                 :          0 :         hn_rndis_detach(hv);
    1141                 :            : }
    1142                 :            : 
    1143                 :            : /*
    1144                 :            :  * Connects EXISTING rx/tx queues to NEW vmbus channel(s), and
    1145                 :            :  * re-initializes NDIS and RNDIS, including re-sending initial
    1146                 :            :  * NDIS/RNDIS configuration. To be used after the underlying vmbus
    1147                 :            :  * has been un- and re-mapped, e.g. as must happen when the device
    1148                 :            :  * MTU is changed.
    1149                 :            :  */
    1150                 :            : static int
    1151                 :          0 : hn_reinit(struct rte_eth_dev *dev, uint16_t mtu)
    1152                 :            : {
    1153                 :          0 :         struct hn_data *hv = dev->data->dev_private;
    1154                 :          0 :         struct hn_rx_queue **rxqs = (struct hn_rx_queue **)dev->data->rx_queues;
    1155                 :          0 :         struct hn_tx_queue **txqs = (struct hn_tx_queue **)dev->data->tx_queues;
    1156                 :            :         int i, ret = 0;
    1157                 :            : 
    1158                 :            :         /* Point primary queues at new primary channel */
    1159         [ #  # ]:          0 :         if (rxqs[0]) {
    1160                 :          0 :                 rxqs[0]->chan = hv->channels[0];
    1161                 :          0 :                 txqs[0]->chan = hv->channels[0];
    1162                 :            :         }
    1163                 :            : 
    1164                 :          0 :         ret = hn_attach(hv, mtu);
    1165         [ #  # ]:          0 :         if (ret)
    1166                 :            :                 return ret;
    1167                 :            : 
    1168                 :            :         /* Create vmbus subchannels, additional RNDIS configuration */
    1169                 :          0 :         ret = hn_dev_configure(dev);
    1170         [ #  # ]:          0 :         if (ret)
    1171                 :            :                 return ret;
    1172                 :            : 
    1173                 :            :         /* Point any additional queues at new subchannels */
    1174         [ #  # ]:          0 :         if (rxqs[0]) {
    1175         [ #  # ]:          0 :                 for (i = 1; i < dev->data->nb_rx_queues; i++)
    1176                 :          0 :                         rxqs[i]->chan = hv->channels[i];
    1177         [ #  # ]:          0 :                 for (i = 1; i < dev->data->nb_tx_queues; i++)
    1178                 :          0 :                         txqs[i]->chan = hv->channels[i];
    1179                 :            :         }
    1180                 :            : 
    1181                 :            :         return ret;
    1182                 :            : }
    1183                 :            : 
    1184                 :            : static int
    1185                 :          0 : hn_dev_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
    1186                 :            : {
    1187                 :          0 :         struct hn_data *hv = dev->data->dev_private;
    1188                 :          0 :         unsigned int orig_mtu = dev->data->mtu;
    1189                 :            :         uint32_t rndis_mtu;
    1190                 :            :         int ret = 0;
    1191                 :            :         int i;
    1192                 :            : 
    1193         [ #  # ]:          0 :         if (dev->data->dev_started) {
    1194                 :          0 :                 PMD_DRV_LOG(ERR, "Device must be stopped before changing MTU");
    1195                 :          0 :                 return -EBUSY;
    1196                 :            :         }
    1197                 :            : 
    1198                 :            :         /* Change MTU of underlying VF dev first, if it exists */
    1199                 :          0 :         ret = hn_vf_mtu_set(dev, mtu);
    1200         [ #  # ]:          0 :         if (ret)
    1201                 :            :                 return ret;
    1202                 :            : 
    1203                 :            :         /* Release channel resources */
    1204                 :            :         hn_detach(hv);
    1205                 :            : 
    1206                 :            :         /* Close any secondary vmbus channels */
    1207         [ #  # ]:          0 :         for (i = 1; i < hv->num_queues; i++)
    1208                 :          0 :                 rte_vmbus_chan_close(hv->channels[i]);
    1209                 :            : 
    1210                 :            :         /* Close primary vmbus channel */
    1211                 :          0 :         rte_free(hv->channels[0]);
    1212                 :            : 
    1213                 :            :         /* Unmap and re-map vmbus device */
    1214                 :          0 :         rte_vmbus_unmap_device(hv->vmbus);
    1215                 :          0 :         ret = rte_vmbus_map_device(hv->vmbus);
    1216         [ #  # ]:          0 :         if (ret) {
    1217                 :            :                 /* This is a catastrophic error - the device is unusable */
    1218                 :          0 :                 PMD_DRV_LOG(ERR, "Could not re-map vmbus device!");
    1219                 :          0 :                 return ret;
    1220                 :            :         }
    1221                 :            : 
    1222                 :            :         /* Update pointers to re-mapped UIO resources */
    1223                 :          0 :         hv->rxbuf_res = hv->vmbus->resource[HV_RECV_BUF_MAP];
    1224                 :          0 :         hv->chim_res  = hv->vmbus->resource[HV_SEND_BUF_MAP];
    1225                 :            : 
    1226                 :            :         /* Re-open the primary vmbus channel */
    1227                 :          0 :         ret = rte_vmbus_chan_open(hv->vmbus, &hv->channels[0]);
    1228         [ #  # ]:          0 :         if (ret) {
    1229                 :            :                 /* This is a catastrophic error - the device is unusable */
    1230                 :          0 :                 PMD_DRV_LOG(ERR, "Could not re-open vmbus channel!");
    1231                 :          0 :                 return ret;
    1232                 :            :         }
    1233                 :            : 
    1234                 :          0 :         rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
    1235                 :            : 
    1236                 :          0 :         ret = hn_reinit(dev, mtu);
    1237         [ #  # ]:          0 :         if (!ret)
    1238                 :          0 :                 goto out;
    1239                 :            : 
    1240                 :            :         /* In case of error, attempt to restore original MTU */
    1241                 :          0 :         ret = hn_reinit(dev, orig_mtu);
    1242         [ #  # ]:          0 :         if (ret)
    1243                 :          0 :                 PMD_DRV_LOG(ERR, "Restoring original MTU failed for netvsc");
    1244                 :            : 
    1245                 :          0 :         ret = hn_vf_mtu_set(dev, orig_mtu);
    1246         [ #  # ]:          0 :         if (ret)
    1247                 :          0 :                 PMD_DRV_LOG(ERR, "Restoring original MTU failed for VF");
    1248                 :            : 
    1249                 :          0 : out:
    1250         [ #  # ]:          0 :         if (hn_rndis_get_mtu(hv, &rndis_mtu)) {
    1251                 :          0 :                 PMD_DRV_LOG(ERR, "Could not get MTU via RNDIS");
    1252                 :            :         } else {
    1253                 :          0 :                 dev->data->mtu = (uint16_t)rndis_mtu;
    1254                 :          0 :                 PMD_DRV_LOG(DEBUG, "RNDIS MTU is %u", dev->data->mtu);
    1255                 :            :         }
    1256                 :            : 
    1257                 :            :         return ret;
    1258                 :            : }
    1259                 :            : 
    1260                 :            : static const struct eth_dev_ops hn_eth_dev_ops = {
    1261                 :            :         .dev_configure          = hn_dev_configure,
    1262                 :            :         .dev_start              = hn_dev_start,
    1263                 :            :         .dev_stop               = hn_dev_stop,
    1264                 :            :         .dev_close              = hn_dev_close,
    1265                 :            :         .dev_infos_get          = hn_dev_info_get,
    1266                 :            :         .txq_info_get           = hn_dev_tx_queue_info,
    1267                 :            :         .rxq_info_get           = hn_dev_rx_queue_info,
    1268                 :            :         .dev_supported_ptypes_get = hn_vf_supported_ptypes,
    1269                 :            :         .promiscuous_enable     = hn_dev_promiscuous_enable,
    1270                 :            :         .promiscuous_disable    = hn_dev_promiscuous_disable,
    1271                 :            :         .allmulticast_enable    = hn_dev_allmulticast_enable,
    1272                 :            :         .allmulticast_disable   = hn_dev_allmulticast_disable,
    1273                 :            :         .set_mc_addr_list       = hn_dev_mc_addr_list,
    1274                 :            :         .mtu_set                = hn_dev_mtu_set,
    1275                 :            :         .reta_update            = hn_rss_reta_update,
    1276                 :            :         .reta_query             = hn_rss_reta_query,
    1277                 :            :         .rss_hash_update        = hn_rss_hash_update,
    1278                 :            :         .rss_hash_conf_get      = hn_rss_hash_conf_get,
    1279                 :            :         .tx_queue_setup         = hn_dev_tx_queue_setup,
    1280                 :            :         .tx_queue_release       = hn_dev_tx_queue_release,
    1281                 :            :         .tx_done_cleanup        = hn_dev_tx_done_cleanup,
    1282                 :            :         .rx_queue_setup         = hn_dev_rx_queue_setup,
    1283                 :            :         .rx_queue_release       = hn_dev_rx_queue_release,
    1284                 :            :         .link_update            = hn_dev_link_update,
    1285                 :            :         .stats_get              = hn_dev_stats_get,
    1286                 :            :         .stats_reset            = hn_dev_stats_reset,
    1287                 :            :         .xstats_get             = hn_dev_xstats_get,
    1288                 :            :         .xstats_get_names       = hn_dev_xstats_get_names,
    1289                 :            :         .xstats_reset           = hn_dev_xstats_reset,
    1290                 :            : };
    1291                 :            : 
    1292                 :            : static int
    1293                 :          0 : eth_hn_dev_init(struct rte_eth_dev *eth_dev)
    1294                 :            : {
    1295                 :          0 :         struct hn_data *hv = eth_dev->data->dev_private;
    1296                 :          0 :         struct rte_device *device = eth_dev->device;
    1297                 :            :         struct rte_vmbus_device *vmbus;
    1298                 :            :         uint32_t mtu;
    1299                 :            :         unsigned int rxr_cnt;
    1300                 :            :         int err, max_chan;
    1301                 :            : 
    1302                 :          0 :         PMD_INIT_FUNC_TRACE();
    1303                 :            : 
    1304                 :            :         rte_spinlock_init(&hv->hotadd_lock);
    1305                 :          0 :         LIST_INIT(&hv->hotadd_list);
    1306                 :            : 
    1307                 :          0 :         vmbus = container_of(device, struct rte_vmbus_device, device);
    1308                 :          0 :         eth_dev->dev_ops = &hn_eth_dev_ops;
    1309                 :          0 :         eth_dev->rx_queue_count = hn_dev_rx_queue_count;
    1310                 :          0 :         eth_dev->rx_descriptor_status = hn_dev_rx_queue_status;
    1311                 :          0 :         eth_dev->tx_descriptor_status = hn_dev_tx_descriptor_status;
    1312                 :          0 :         eth_dev->tx_pkt_burst = &hn_xmit_pkts;
    1313                 :          0 :         eth_dev->rx_pkt_burst = &hn_recv_pkts;
    1314                 :            : 
    1315                 :            :         /*
    1316                 :            :          * for secondary processes, we don't initialize any further as primary
    1317                 :            :          * has already done this work.
    1318                 :            :          */
    1319         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
    1320                 :            :                 return 0;
    1321                 :            : 
    1322                 :          0 :         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
    1323                 :            : 
    1324                 :            :         /* Since Hyper-V only supports one MAC address */
    1325                 :          0 :         eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS,
    1326                 :            :                                               sizeof(struct rte_ether_addr), 0);
    1327         [ #  # ]:          0 :         if (eth_dev->data->mac_addrs == NULL) {
    1328                 :          0 :                 PMD_INIT_LOG(ERR,
    1329                 :            :                              "Failed to allocate memory store MAC addresses");
    1330                 :          0 :                 return -ENOMEM;
    1331                 :            :         }
    1332                 :            : 
    1333                 :          0 :         hv->vmbus = vmbus;
    1334                 :          0 :         hv->rxbuf_res = vmbus->resource[HV_RECV_BUF_MAP];
    1335                 :          0 :         hv->chim_res  = vmbus->resource[HV_SEND_BUF_MAP];
    1336                 :          0 :         hv->port_id = eth_dev->data->port_id;
    1337                 :          0 :         hv->latency = HN_CHAN_LATENCY_NS;
    1338                 :          0 :         hv->rx_copybreak = HN_RXCOPY_THRESHOLD;
    1339                 :          0 :         hv->tx_copybreak = HN_TXCOPY_THRESHOLD;
    1340                 :          0 :         hv->rx_extmbuf_enable = HN_RX_EXTMBUF_ENABLE;
    1341                 :          0 :         hv->max_queues = 1;
    1342                 :            : 
    1343                 :            :         rte_rwlock_init(&hv->vf_lock);
    1344                 :          0 :         hv->vf_ctx.vf_vsc_switched = false;
    1345                 :          0 :         hv->vf_ctx.vf_vsp_reported = false;
    1346                 :          0 :         hv->vf_ctx.vf_attached = false;
    1347                 :          0 :         hv->vf_ctx.vf_state = vf_unknown;
    1348                 :            : 
    1349                 :          0 :         err = hn_parse_args(eth_dev);
    1350         [ #  # ]:          0 :         if (err)
    1351                 :            :                 return err;
    1352                 :            : 
    1353                 :          0 :         strlcpy(hv->owner.name, eth_dev->device->name,
    1354                 :            :                 RTE_ETH_MAX_OWNER_NAME_LEN);
    1355                 :          0 :         err = rte_eth_dev_owner_new(&hv->owner.id);
    1356         [ #  # ]:          0 :         if (err) {
    1357                 :          0 :                 PMD_INIT_LOG(ERR, "Can not get owner id");
    1358                 :          0 :                 return err;
    1359                 :            :         }
    1360                 :            : 
    1361                 :            :         /* Initialize primary channel input for control operations */
    1362                 :          0 :         err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
    1363         [ #  # ]:          0 :         if (err)
    1364                 :            :                 return err;
    1365                 :            : 
    1366                 :          0 :         rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
    1367                 :            : 
    1368                 :          0 :         hv->primary = hn_rx_queue_alloc(hv, 0,
    1369                 :          0 :                                         eth_dev->device->numa_node);
    1370                 :            : 
    1371         [ #  # ]:          0 :         if (!hv->primary)
    1372                 :            :                 return -ENOMEM;
    1373                 :            : 
    1374                 :          0 :         err = hn_attach(hv, RTE_ETHER_MTU);
    1375         [ #  # ]:          0 :         if  (err)
    1376                 :          0 :                 goto failed;
    1377                 :            : 
    1378                 :          0 :         err = hn_chim_init(eth_dev);
    1379         [ #  # ]:          0 :         if (err)
    1380                 :          0 :                 goto failed;
    1381                 :            : 
    1382                 :          0 :         err = hn_rndis_get_mtu(hv, &mtu);
    1383         [ #  # ]:          0 :         if (err)
    1384                 :          0 :                 goto failed;
    1385                 :          0 :         eth_dev->data->mtu = (uint16_t)mtu;
    1386                 :          0 :         PMD_INIT_LOG(DEBUG, "RNDIS MTU is %u", eth_dev->data->mtu);
    1387                 :            : 
    1388                 :          0 :         err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes);
    1389         [ #  # ]:          0 :         if (err)
    1390                 :          0 :                 goto failed;
    1391                 :            : 
    1392                 :            :         /* Multi queue requires later versions of windows server */
    1393         [ #  # ]:          0 :         if (hv->nvs_ver < NVS_VERSION_5)
    1394                 :            :                 return 0;
    1395                 :            : 
    1396                 :          0 :         max_chan = rte_vmbus_max_channels(vmbus);
    1397                 :          0 :         PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
    1398         [ #  # ]:          0 :         if (max_chan <= 0)
    1399                 :          0 :                 goto failed;
    1400                 :            : 
    1401         [ #  # ]:          0 :         if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
    1402                 :          0 :                 rxr_cnt = 1;
    1403                 :            : 
    1404                 :          0 :         hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
    1405                 :            : 
    1406                 :            :         /* If VF was reported but not added, do it now */
    1407   [ #  #  #  # ]:          0 :         if (hv->vf_ctx.vf_vsp_reported && !hv->vf_ctx.vf_vsc_switched) {
    1408                 :          0 :                 PMD_INIT_LOG(DEBUG, "Adding VF device");
    1409                 :            : 
    1410                 :          0 :                 err = hn_vf_add(eth_dev, hv);
    1411                 :            :         }
    1412                 :            : 
    1413                 :            :         return 0;
    1414                 :            : 
    1415                 :          0 : failed:
    1416                 :          0 :         PMD_INIT_LOG(NOTICE, "device init failed");
    1417                 :            : 
    1418                 :          0 :         hn_chim_uninit(eth_dev);
    1419                 :            :         hn_detach(hv);
    1420                 :          0 :         return err;
    1421                 :            : }
    1422                 :            : 
    1423                 :            : static int
    1424                 :          0 : eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
    1425                 :            : {
    1426                 :          0 :         struct hn_data *hv = eth_dev->data->dev_private;
    1427                 :            :         int ret, ret_stop;
    1428                 :            : 
    1429                 :          0 :         PMD_INIT_FUNC_TRACE();
    1430                 :            : 
    1431         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
    1432                 :            :                 return 0;
    1433                 :            : 
    1434                 :          0 :         ret_stop = hn_dev_stop(eth_dev);
    1435                 :          0 :         hn_dev_close(eth_dev);
    1436                 :            : 
    1437                 :            :         hn_detach(hv);
    1438                 :          0 :         hn_chim_uninit(eth_dev);
    1439                 :          0 :         rte_vmbus_chan_close(hv->channels[0]);
    1440                 :          0 :         rte_free(hv->primary);
    1441                 :          0 :         ret = rte_eth_dev_owner_delete(hv->owner.id);
    1442         [ #  # ]:          0 :         if (ret != 0)
    1443                 :          0 :                 return ret;
    1444                 :            : 
    1445                 :            :         return ret_stop;
    1446                 :            : }
    1447                 :            : 
    1448                 :          0 : static int populate_cache_list(void)
    1449                 :            : {
    1450                 :            :         int ret = 0;
    1451                 :            :         struct rte_devargs *da;
    1452                 :            : 
    1453                 :            :         rte_spinlock_lock(&netvsc_lock);
    1454                 :          0 :         da_cache_usage++;
    1455         [ #  # ]:          0 :         if (da_cache_usage > 1) {
    1456                 :            :                 ret = 0;
    1457                 :          0 :                 goto out;
    1458                 :            :         }
    1459                 :            : 
    1460                 :          0 :         LIST_INIT(&da_cache_list);
    1461         [ #  # ]:          0 :         RTE_EAL_DEVARGS_FOREACH("pci", da) {
    1462                 :            :                 struct da_cache *cache;
    1463                 :            : 
    1464                 :          0 :                 cache = calloc(1, sizeof(*cache) + strlen(da->drv_str) + 1);
    1465         [ #  # ]:          0 :                 if (!cache) {
    1466                 :            :                         ret = -ENOMEM;
    1467                 :          0 :                         goto out;
    1468                 :            :                 }
    1469                 :            : 
    1470         [ #  # ]:          0 :                 strlcpy(cache->name, da->name, sizeof(cache->name));
    1471                 :          0 :                 strlcpy(cache->drv_str, da->drv_str, strlen(da->drv_str) + 1);
    1472         [ #  # ]:          0 :                 LIST_INSERT_HEAD(&da_cache_list, cache, list);
    1473                 :            :         }
    1474                 :          0 : out:
    1475                 :            :         rte_spinlock_unlock(&netvsc_lock);
    1476                 :          0 :         return ret;
    1477                 :            : }
    1478                 :            : 
    1479                 :          0 : static void remove_cache_list(void)
    1480                 :            : {
    1481                 :            :         struct da_cache *cache;
    1482                 :            : 
    1483                 :            :         rte_spinlock_lock(&netvsc_lock);
    1484                 :          0 :         da_cache_usage--;
    1485         [ #  # ]:          0 :         if (da_cache_usage)
    1486                 :          0 :                 goto out;
    1487                 :            : 
    1488         [ #  # ]:          0 :         LIST_FOREACH(cache, &da_cache_list, list) {
    1489         [ #  # ]:          0 :                 LIST_REMOVE(cache, list);
    1490                 :          0 :                 free(cache);
    1491                 :            :         }
    1492                 :          0 : out:
    1493                 :            :         rte_spinlock_unlock(&netvsc_lock);
    1494                 :          0 : }
    1495                 :            : 
    1496                 :          0 : static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
    1497                 :            :                         struct rte_vmbus_device *dev)
    1498                 :            : {
    1499                 :            :         struct rte_eth_dev *eth_dev;
    1500                 :            :         struct hn_nvs_process_priv *process_priv;
    1501                 :            :         int ret = 0;
    1502                 :            : 
    1503                 :          0 :         PMD_INIT_FUNC_TRACE();
    1504                 :            : 
    1505                 :          0 :         ret = populate_cache_list();
    1506         [ #  # ]:          0 :         if (ret)
    1507                 :            :                 return ret;
    1508                 :            : 
    1509                 :          0 :         ret = rte_dev_event_monitor_start();
    1510         [ #  # ]:          0 :         if (ret) {
    1511                 :          0 :                 PMD_DRV_LOG(ERR, "Failed to start device event monitoring");
    1512                 :          0 :                 goto fail;
    1513                 :            :         }
    1514                 :            : 
    1515                 :          0 :         eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
    1516         [ #  # ]:          0 :         if (!eth_dev) {
    1517                 :            :                 ret = -ENOMEM;
    1518                 :          0 :                 goto vmbus_alloc_failed;
    1519                 :            :         }
    1520                 :            : 
    1521                 :          0 :         process_priv = rte_zmalloc_socket("netvsc_proc_priv",
    1522                 :            :                                           sizeof(struct hn_nvs_process_priv),
    1523                 :            :                                           RTE_CACHE_LINE_SIZE,
    1524                 :            :                                           dev->device.numa_node);
    1525         [ #  # ]:          0 :         if (!process_priv) {
    1526                 :            :                 ret = -ENOMEM;
    1527                 :          0 :                 goto priv_alloc_failed;
    1528                 :            :         }
    1529                 :            : 
    1530                 :          0 :         process_priv->vmbus_dev = dev;
    1531                 :          0 :         eth_dev->process_private = process_priv;
    1532                 :            : 
    1533                 :          0 :         ret = eth_hn_dev_init(eth_dev);
    1534         [ #  # ]:          0 :         if (ret)
    1535                 :          0 :                 goto dev_init_failed;
    1536                 :            : 
    1537                 :          0 :         rte_eth_dev_probing_finish(eth_dev);
    1538                 :          0 :         return ret;
    1539                 :            : 
    1540                 :            : dev_init_failed:
    1541                 :          0 :         rte_free(process_priv);
    1542                 :            : 
    1543                 :          0 : priv_alloc_failed:
    1544                 :            :         eth_dev_vmbus_release(eth_dev);
    1545                 :            : 
    1546                 :          0 : vmbus_alloc_failed:
    1547                 :          0 :         rte_dev_event_monitor_stop();
    1548                 :            : 
    1549                 :          0 : fail:
    1550                 :          0 :         remove_cache_list();
    1551                 :          0 :         return ret;
    1552                 :            : }
    1553                 :            : 
    1554                 :          0 : static int eth_hn_remove(struct rte_vmbus_device *dev)
    1555                 :            : {
    1556                 :            :         struct rte_eth_dev *eth_dev;
    1557                 :            :         struct hn_nvs_process_priv *process_priv;
    1558                 :            :         int ret;
    1559                 :            : 
    1560                 :          0 :         PMD_INIT_FUNC_TRACE();
    1561                 :            : 
    1562                 :          0 :         eth_dev = rte_eth_dev_allocated(dev->device.name);
    1563         [ #  # ]:          0 :         if (!eth_dev)
    1564                 :            :                 return 0; /* port already released */
    1565                 :            : 
    1566                 :          0 :         ret = eth_hn_dev_uninit(eth_dev);
    1567         [ #  # ]:          0 :         if (ret)
    1568                 :            :                 return ret;
    1569                 :            : 
    1570                 :          0 :         process_priv = eth_dev->process_private;
    1571                 :          0 :         rte_free(process_priv);
    1572                 :            : 
    1573                 :            :         eth_dev_vmbus_release(eth_dev);
    1574                 :          0 :         rte_dev_event_monitor_stop();
    1575                 :            : 
    1576                 :          0 :         remove_cache_list();
    1577                 :            : 
    1578                 :          0 :         return 0;
    1579                 :            : }
    1580                 :            : 
    1581                 :            : /* Network device GUID */
    1582                 :            : static const rte_uuid_t hn_net_ids[] = {
    1583                 :            :         /*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
    1584                 :            :         RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
    1585                 :            :         { 0 }
    1586                 :            : };
    1587                 :            : 
    1588                 :            : static struct rte_vmbus_driver rte_netvsc_pmd = {
    1589                 :            :         .id_table = hn_net_ids,
    1590                 :            :         .probe = eth_hn_probe,
    1591                 :            :         .remove = eth_hn_remove,
    1592                 :            : };
    1593                 :            : 
    1594                 :        253 : RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
    1595                 :            : RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
    1596         [ -  + ]:        253 : RTE_LOG_REGISTER_SUFFIX(hn_logtype_init, init, NOTICE);
    1597         [ -  + ]:        253 : RTE_LOG_REGISTER_SUFFIX(hn_logtype_driver, driver, NOTICE);
    1598                 :            : RTE_PMD_REGISTER_PARAM_STRING(net_netvsc,
    1599                 :            :                               NETVSC_ARG_LATENCY "=<uint32> "
    1600                 :            :                               NETVSC_ARG_RXBREAK "=<uint32> "
    1601                 :            :                               NETVSC_ARG_TXBREAK "=<uint32> "
    1602                 :            :                               NETVSC_ARG_RX_EXTMBUF_ENABLE "=<0|1>");

Generated by: LCOV version 1.14