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