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

Generated by: LCOV version 1.14