net/netvsc: implement link state change callback
[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
13 #include <rte_ethdev.h>
14 #include <rte_memcpy.h>
15 #include <rte_string_fns.h>
16 #include <rte_memzone.h>
17 #include <rte_devargs.h>
18 #include <rte_malloc.h>
19 #include <rte_kvargs.h>
20 #include <rte_atomic.h>
21 #include <rte_branch_prediction.h>
22 #include <rte_ether.h>
23 #include <rte_ethdev_driver.h>
24 #include <rte_cycles.h>
25 #include <rte_errno.h>
26 #include <rte_memory.h>
27 #include <rte_eal.h>
28 #include <rte_dev.h>
29 #include <rte_bus_vmbus.h>
30
31 #include "hn_logs.h"
32 #include "hn_var.h"
33 #include "hn_rndis.h"
34 #include "hn_nvs.h"
35 #include "ndis.h"
36
37 #define HN_TX_OFFLOAD_CAPS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
38                             DEV_TX_OFFLOAD_TCP_CKSUM  | \
39                             DEV_TX_OFFLOAD_UDP_CKSUM  | \
40                             DEV_TX_OFFLOAD_TCP_TSO    | \
41                             DEV_TX_OFFLOAD_MULTI_SEGS | \
42                             DEV_TX_OFFLOAD_VLAN_INSERT)
43
44 #define HN_RX_OFFLOAD_CAPS (DEV_RX_OFFLOAD_CHECKSUM | \
45                             DEV_RX_OFFLOAD_VLAN_STRIP)
46
47 int hn_logtype_init;
48 int hn_logtype_driver;
49
50 struct hn_xstats_name_off {
51         char name[RTE_ETH_XSTATS_NAME_SIZE];
52         unsigned int offset;
53 };
54
55 static const struct hn_xstats_name_off hn_stat_strings[] = {
56         { "good_packets",           offsetof(struct hn_stats, packets) },
57         { "good_bytes",             offsetof(struct hn_stats, bytes) },
58         { "errors",                 offsetof(struct hn_stats, errors) },
59         { "ring full",              offsetof(struct hn_stats, ring_full) },
60         { "multicast_packets",      offsetof(struct hn_stats, multicast) },
61         { "broadcast_packets",      offsetof(struct hn_stats, broadcast) },
62         { "undersize_packets",      offsetof(struct hn_stats, size_bins[0]) },
63         { "size_64_packets",        offsetof(struct hn_stats, size_bins[1]) },
64         { "size_65_127_packets",    offsetof(struct hn_stats, size_bins[2]) },
65         { "size_128_255_packets",   offsetof(struct hn_stats, size_bins[3]) },
66         { "size_256_511_packets",   offsetof(struct hn_stats, size_bins[4]) },
67         { "size_512_1023_packets",  offsetof(struct hn_stats, size_bins[5]) },
68         { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
69         { "size_1519_max_packets",  offsetof(struct hn_stats, size_bins[7]) },
70 };
71
72 static struct rte_eth_dev *
73 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
74 {
75         struct rte_eth_dev *eth_dev;
76         const char *name;
77
78         if (!dev)
79                 return NULL;
80
81         name = dev->device.name;
82
83         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
84                 eth_dev = rte_eth_dev_allocate(name);
85                 if (!eth_dev) {
86                         PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
87                         return NULL;
88                 }
89
90                 if (private_data_size) {
91                         eth_dev->data->dev_private =
92                                 rte_zmalloc_socket(name, private_data_size,
93                                                      RTE_CACHE_LINE_SIZE, dev->device.numa_node);
94                         if (!eth_dev->data->dev_private) {
95                                 PMD_DRV_LOG(NOTICE, "can not allocate driver data");
96                                 rte_eth_dev_release_port(eth_dev);
97                                 return NULL;
98                         }
99                 }
100         } else {
101                 eth_dev = rte_eth_dev_attach_secondary(name);
102                 if (!eth_dev) {
103                         PMD_DRV_LOG(NOTICE, "can not attach secondary");
104                         return NULL;
105                 }
106         }
107
108         eth_dev->device = &dev->device;
109
110         /* interrupt is simulated */
111         dev->intr_handle.type = RTE_INTR_HANDLE_EXT;
112         eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
113         eth_dev->intr_handle = &dev->intr_handle;
114
115         return eth_dev;
116 }
117
118 static void
119 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
120 {
121         /* free ether device */
122         rte_eth_dev_release_port(eth_dev);
123
124         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
125                 rte_free(eth_dev->data->dev_private);
126
127         eth_dev->data->dev_private = NULL;
128
129         /*
130          * Secondary process will check the name to attach.
131          * Clear this field to avoid attaching a released ports.
132          */
133         eth_dev->data->name[0] = '\0';
134
135         eth_dev->device = NULL;
136         eth_dev->intr_handle = NULL;
137 }
138
139 /* handle "latency=X" from devargs */
140 static int hn_set_latency(const char *key, const char *value, void *opaque)
141 {
142         struct hn_data *hv = opaque;
143         char *endp = NULL;
144         unsigned long lat;
145
146         errno = 0;
147         lat = strtoul(value, &endp, 0);
148
149         if (*value == '\0' || *endp != '\0') {
150                 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
151                 return -EINVAL;
152         }
153
154         PMD_DRV_LOG(DEBUG, "set latency %lu usec", lat);
155
156         hv->latency = lat * 1000;       /* usec to nsec */
157         return 0;
158 }
159
160 /* Parse device arguments */
161 static int hn_parse_args(const struct rte_eth_dev *dev)
162 {
163         struct hn_data *hv = dev->data->dev_private;
164         struct rte_devargs *devargs = dev->device->devargs;
165         static const char * const valid_keys[] = {
166                 "latency",
167                 NULL
168         };
169         struct rte_kvargs *kvlist;
170
171         if (!devargs)
172                 return 0;
173
174         PMD_INIT_LOG(DEBUG, "device args %s %s",
175                      devargs->name, devargs->args);
176
177         kvlist = rte_kvargs_parse(devargs->args, valid_keys);
178         if (!kvlist) {
179                 PMD_DRV_LOG(NOTICE, "invalid parameters");
180                 return -EINVAL;
181         }
182
183         rte_kvargs_process(kvlist, "latency", hn_set_latency, hv);
184         rte_kvargs_free(kvlist);
185         return 0;
186 }
187
188 /* Update link status.
189  * Note: the DPDK definition of "wait_to_complete"
190  *   means block this call until link is up.
191  *   which is not worth supporting.
192  */
193 int
194 hn_dev_link_update(struct rte_eth_dev *dev,
195                    __rte_unused int wait_to_complete)
196 {
197         struct hn_data *hv = dev->data->dev_private;
198         struct rte_eth_link link, old;
199         int error;
200
201         old = dev->data->dev_link;
202
203         error = hn_rndis_get_linkstatus(hv);
204         if (error)
205                 return error;
206
207         hn_rndis_get_linkspeed(hv);
208
209         link = (struct rte_eth_link) {
210                 .link_duplex = ETH_LINK_FULL_DUPLEX,
211                 .link_autoneg = ETH_LINK_SPEED_FIXED,
212                 .link_speed = hv->link_speed / 10000,
213         };
214
215         if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
216                 link.link_status = ETH_LINK_UP;
217         else
218                 link.link_status = ETH_LINK_DOWN;
219
220         if (old.link_status == link.link_status)
221                 return 0;
222
223         PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
224                      (link.link_status == ETH_LINK_UP) ? "up" : "down");
225
226         return rte_eth_linkstatus_set(dev, &link);
227 }
228
229 static void hn_dev_info_get(struct rte_eth_dev *dev,
230                             struct rte_eth_dev_info *dev_info)
231 {
232         struct hn_data *hv = dev->data->dev_private;
233
234         dev_info->speed_capa = ETH_LINK_SPEED_10G;
235         dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
236         dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
237         dev_info->max_mac_addrs  = 1;
238
239         dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
240         dev_info->flow_type_rss_offloads =
241                 ETH_RSS_IPV4 | ETH_RSS_IPV6 | ETH_RSS_TCP | ETH_RSS_UDP;
242
243         dev_info->max_rx_queues = hv->max_queues;
244         dev_info->max_tx_queues = hv->max_queues;
245
246         hn_rndis_get_offload(hv, dev_info);
247 }
248
249 static void
250 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
251 {
252         struct hn_data *hv = dev->data->dev_private;
253
254         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
255 }
256
257 static void
258 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
259 {
260         struct hn_data *hv = dev->data->dev_private;
261         uint32_t filter;
262
263         filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
264         if (dev->data->all_multicast)
265                 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
266         hn_rndis_set_rxfilter(hv, filter);
267 }
268
269 static void
270 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
271 {
272         struct hn_data *hv = dev->data->dev_private;
273
274         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
275                               NDIS_PACKET_TYPE_ALL_MULTICAST |
276                         NDIS_PACKET_TYPE_BROADCAST);
277 }
278
279 static void
280 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
281 {
282         struct hn_data *hv = dev->data->dev_private;
283
284         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
285                              NDIS_PACKET_TYPE_BROADCAST);
286 }
287
288 /* Setup shared rx/tx queue data */
289 static int hn_subchan_configure(struct hn_data *hv,
290                                 uint32_t subchan)
291 {
292         struct vmbus_channel *primary = hn_primary_chan(hv);
293         int err;
294         unsigned int retry = 0;
295
296         PMD_DRV_LOG(DEBUG,
297                     "open %u subchannels", subchan);
298
299         /* Send create sub channels command */
300         err = hn_nvs_alloc_subchans(hv, &subchan);
301         if (err)
302                 return  err;
303
304         while (subchan > 0) {
305                 struct vmbus_channel *new_sc;
306                 uint16_t chn_index;
307
308                 err = rte_vmbus_subchan_open(primary, &new_sc);
309                 if (err == -ENOENT && ++retry < 1000) {
310                         /* This can happen if not ready yet */
311                         rte_delay_ms(10);
312                         continue;
313                 }
314
315                 if (err) {
316                         PMD_DRV_LOG(ERR,
317                                     "open subchannel failed: %d", err);
318                         return err;
319                 }
320
321                 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
322
323                 retry = 0;
324                 chn_index = rte_vmbus_sub_channel_index(new_sc);
325                 if (chn_index == 0 || chn_index > hv->max_queues) {
326                         PMD_DRV_LOG(ERR,
327                                     "Invalid subchannel offermsg channel %u",
328                                     chn_index);
329                         return -EIO;
330                 }
331
332                 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
333                 hv->channels[chn_index] = new_sc;
334                 --subchan;
335         }
336
337         return err;
338 }
339
340 static int hn_dev_configure(struct rte_eth_dev *dev)
341 {
342         const struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
343         const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
344         const struct rte_eth_txmode *txmode = &dev_conf->txmode;
345
346         const struct rte_eth_rss_conf *rss_conf =
347                 &dev_conf->rx_adv_conf.rss_conf;
348         struct hn_data *hv = dev->data->dev_private;
349         uint64_t unsupported;
350         int err, subchan;
351
352         PMD_INIT_FUNC_TRACE();
353
354         unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
355         if (unsupported) {
356                 PMD_DRV_LOG(NOTICE,
357                             "unsupported TX offload: %#" PRIx64,
358                             unsupported);
359                 return -EINVAL;
360         }
361
362         unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
363         if (unsupported) {
364                 PMD_DRV_LOG(NOTICE,
365                             "unsupported RX offload: %#" PRIx64,
366                             rxmode->offloads);
367                 return -EINVAL;
368         }
369
370         err = hn_rndis_conf_offload(hv, txmode->offloads,
371                                     rxmode->offloads);
372         if (err) {
373                 PMD_DRV_LOG(NOTICE,
374                             "offload configure failed");
375                 return err;
376         }
377
378         hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
379                                  dev->data->nb_tx_queues);
380         subchan = hv->num_queues - 1;
381         if (subchan > 0) {
382                 err = hn_subchan_configure(hv, subchan);
383                 if (err) {
384                         PMD_DRV_LOG(NOTICE,
385                                     "subchannel configuration failed");
386                         return err;
387                 }
388
389                 err = hn_rndis_conf_rss(hv, rss_conf);
390                 if (err) {
391                         PMD_DRV_LOG(NOTICE,
392                                     "rss configuration failed");
393                         return err;
394                 }
395         }
396
397         return 0;
398 }
399
400 static int hn_dev_stats_get(struct rte_eth_dev *dev,
401                             struct rte_eth_stats *stats)
402 {
403         unsigned int i;
404
405         for (i = 0; i < dev->data->nb_tx_queues; i++) {
406                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
407
408                 if (!txq)
409                         continue;
410
411                 stats->opackets += txq->stats.packets;
412                 stats->obytes += txq->stats.bytes;
413                 stats->oerrors += txq->stats.errors;
414
415                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
416                         stats->q_opackets[i] = txq->stats.packets;
417                         stats->q_obytes[i] = txq->stats.bytes;
418                 }
419         }
420
421         for (i = 0; i < dev->data->nb_rx_queues; i++) {
422                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
423
424                 if (!rxq)
425                         continue;
426
427                 stats->ipackets += rxq->stats.packets;
428                 stats->ibytes += rxq->stats.bytes;
429                 stats->ierrors += rxq->stats.errors;
430                 stats->imissed += rxq->stats.ring_full;
431
432                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
433                         stats->q_ipackets[i] = rxq->stats.packets;
434                         stats->q_ibytes[i] = rxq->stats.bytes;
435                 }
436         }
437
438         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
439         return 0;
440 }
441
442 static void
443 hn_dev_stats_reset(struct rte_eth_dev *dev)
444 {
445         unsigned int i;
446
447         PMD_INIT_FUNC_TRACE();
448
449         for (i = 0; i < dev->data->nb_tx_queues; i++) {
450                 struct hn_tx_queue *txq = dev->data->tx_queues[i];
451
452                 if (!txq)
453                         continue;
454                 memset(&txq->stats, 0, sizeof(struct hn_stats));
455         }
456
457         for (i = 0; i < dev->data->nb_rx_queues; i++) {
458                 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
459
460                 if (!rxq)
461                         continue;
462
463                 memset(&rxq->stats, 0, sizeof(struct hn_stats));
464         }
465 }
466
467 static int
468 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
469                         struct rte_eth_xstat_name *xstats_names,
470                         __rte_unused unsigned int limit)
471 {
472         unsigned int i, t, count = 0;
473
474         PMD_INIT_FUNC_TRACE();
475
476         if (!xstats_names)
477                 return dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
478                         + dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
479
480         /* Note: limit checked in rte_eth_xstats_names() */
481         for (i = 0; i < dev->data->nb_tx_queues; i++) {
482                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
483
484                 if (!txq)
485                         continue;
486
487                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
488                         snprintf(xstats_names[count++].name,
489                                  RTE_ETH_XSTATS_NAME_SIZE,
490                                  "tx_q%u_%s", i, hn_stat_strings[t].name);
491         }
492
493         for (i = 0; i < dev->data->nb_rx_queues; i++)  {
494                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
495
496                 if (!rxq)
497                         continue;
498
499                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
500                         snprintf(xstats_names[count++].name,
501                                  RTE_ETH_XSTATS_NAME_SIZE,
502                                  "rx_q%u_%s", i,
503                                  hn_stat_strings[t].name);
504         }
505
506         return count;
507 }
508
509 static int
510 hn_dev_xstats_get(struct rte_eth_dev *dev,
511                   struct rte_eth_xstat *xstats,
512                   unsigned int n)
513 {
514         unsigned int i, t, count = 0;
515
516         const unsigned int nstats =
517                 dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
518                 + dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
519         const char *stats;
520
521         PMD_INIT_FUNC_TRACE();
522
523         if (n < nstats)
524                 return nstats;
525
526         for (i = 0; i < dev->data->nb_tx_queues; i++) {
527                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
528
529                 if (!txq)
530                         continue;
531
532                 stats = (const char *)&txq->stats;
533                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
534                         xstats[count++].value = *(const uint64_t *)
535                                 (stats + hn_stat_strings[t].offset);
536         }
537
538         for (i = 0; i < dev->data->nb_rx_queues; i++) {
539                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
540
541                 if (!rxq)
542                         continue;
543
544                 stats = (const char *)&rxq->stats;
545                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
546                         xstats[count++].value = *(const uint64_t *)
547                                 (stats + hn_stat_strings[t].offset);
548         }
549
550         return count;
551 }
552
553 static int
554 hn_dev_start(struct rte_eth_dev *dev)
555 {
556         struct hn_data *hv = dev->data->dev_private;
557
558         PMD_INIT_FUNC_TRACE();
559
560         return hn_rndis_set_rxfilter(hv,
561                                      NDIS_PACKET_TYPE_BROADCAST |
562                                      NDIS_PACKET_TYPE_ALL_MULTICAST |
563                                      NDIS_PACKET_TYPE_DIRECTED);
564 }
565
566 static void
567 hn_dev_stop(struct rte_eth_dev *dev)
568 {
569         struct hn_data *hv = dev->data->dev_private;
570
571         PMD_INIT_FUNC_TRACE();
572
573         hn_rndis_set_rxfilter(hv, 0);
574 }
575
576 static void
577 hn_dev_close(struct rte_eth_dev *dev __rte_unused)
578 {
579         PMD_INIT_LOG(DEBUG, "close");
580 }
581
582 static const struct eth_dev_ops hn_eth_dev_ops = {
583         .dev_configure          = hn_dev_configure,
584         .dev_start              = hn_dev_start,
585         .dev_stop               = hn_dev_stop,
586         .dev_close              = hn_dev_close,
587         .dev_infos_get          = hn_dev_info_get,
588         .txq_info_get           = hn_dev_tx_queue_info,
589         .rxq_info_get           = hn_dev_rx_queue_info,
590         .promiscuous_enable     = hn_dev_promiscuous_enable,
591         .promiscuous_disable    = hn_dev_promiscuous_disable,
592         .allmulticast_enable    = hn_dev_allmulticast_enable,
593         .allmulticast_disable   = hn_dev_allmulticast_disable,
594         .tx_queue_setup         = hn_dev_tx_queue_setup,
595         .tx_queue_release       = hn_dev_tx_queue_release,
596         .tx_done_cleanup        = hn_dev_tx_done_cleanup,
597         .rx_queue_setup         = hn_dev_rx_queue_setup,
598         .rx_queue_release       = hn_dev_rx_queue_release,
599         .link_update            = hn_dev_link_update,
600         .stats_get              = hn_dev_stats_get,
601         .xstats_get             = hn_dev_xstats_get,
602         .xstats_get_names       = hn_dev_xstats_get_names,
603         .stats_reset            = hn_dev_stats_reset,
604         .xstats_reset           = hn_dev_stats_reset,
605 };
606
607 /*
608  * Setup connection between PMD and kernel.
609  */
610 static int
611 hn_attach(struct hn_data *hv, unsigned int mtu)
612 {
613         int error;
614
615         /* Attach NVS */
616         error = hn_nvs_attach(hv, mtu);
617         if (error)
618                 goto failed_nvs;
619
620         /* Attach RNDIS */
621         error = hn_rndis_attach(hv);
622         if (error)
623                 goto failed_rndis;
624
625         /*
626          * NOTE:
627          * Under certain conditions on certain versions of Hyper-V,
628          * the RNDIS rxfilter is _not_ zero on the hypervisor side
629          * after the successful RNDIS initialization.
630          */
631         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
632         return 0;
633 failed_rndis:
634         hn_nvs_detach(hv);
635 failed_nvs:
636         return error;
637 }
638
639 static void
640 hn_detach(struct hn_data *hv)
641 {
642         hn_nvs_detach(hv);
643         hn_rndis_detach(hv);
644 }
645
646 static int
647 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
648 {
649         struct hn_data *hv = eth_dev->data->dev_private;
650         struct rte_device *device = eth_dev->device;
651         struct rte_vmbus_device *vmbus;
652         unsigned int rxr_cnt;
653         int err, max_chan;
654
655         PMD_INIT_FUNC_TRACE();
656
657         vmbus = container_of(device, struct rte_vmbus_device, device);
658         eth_dev->dev_ops = &hn_eth_dev_ops;
659         eth_dev->tx_pkt_burst = &hn_xmit_pkts;
660         eth_dev->rx_pkt_burst = &hn_recv_pkts;
661
662         /*
663          * for secondary processes, we don't initialize any further as primary
664          * has already done this work.
665          */
666         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
667                 return 0;
668
669         /* Since Hyper-V only supports one MAC address, just use local data */
670         eth_dev->data->mac_addrs = &hv->mac_addr;
671
672         hv->vmbus = vmbus;
673         hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
674         hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
675         hv->port_id = eth_dev->data->port_id;
676         hv->latency = HN_CHAN_LATENCY_NS;
677
678         err = hn_parse_args(eth_dev);
679         if (err)
680                 return err;
681
682         /* Initialize primary channel input for control operations */
683         err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
684         if (err)
685                 return err;
686
687         rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
688
689         hv->primary = hn_rx_queue_alloc(hv, 0,
690                                         eth_dev->device->numa_node);
691
692         if (!hv->primary)
693                 return -ENOMEM;
694
695         err = hn_attach(hv, ETHER_MTU);
696         if  (err)
697                 goto failed;
698
699         err = hn_tx_pool_init(eth_dev);
700         if (err)
701                 goto failed;
702
703         err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
704         if (err)
705                 goto failed;
706
707         max_chan = rte_vmbus_max_channels(vmbus);
708         PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
709         if (max_chan <= 0)
710                 goto failed;
711
712         if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
713                 rxr_cnt = 1;
714
715         hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
716
717         return 0;
718
719 failed:
720         PMD_INIT_LOG(NOTICE, "device init failed");
721
722         hn_detach(hv);
723         return err;
724 }
725
726 static int
727 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
728 {
729         struct hn_data *hv = eth_dev->data->dev_private;
730
731         PMD_INIT_FUNC_TRACE();
732
733         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
734                 return 0;
735
736         hn_dev_stop(eth_dev);
737         hn_dev_close(eth_dev);
738
739         eth_dev->dev_ops = NULL;
740         eth_dev->tx_pkt_burst = NULL;
741         eth_dev->rx_pkt_burst = NULL;
742
743         hn_detach(hv);
744         rte_vmbus_chan_close(hv->primary->chan);
745         rte_free(hv->primary);
746
747         eth_dev->data->mac_addrs = NULL;
748
749         return 0;
750 }
751
752 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
753                         struct rte_vmbus_device *dev)
754 {
755         struct rte_eth_dev *eth_dev;
756         int ret;
757
758         PMD_INIT_FUNC_TRACE();
759
760         eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
761         if (!eth_dev)
762                 return -ENOMEM;
763
764         ret = eth_hn_dev_init(eth_dev);
765         if (ret)
766                 eth_dev_vmbus_release(eth_dev);
767         else
768                 rte_eth_dev_probing_finish(eth_dev);
769
770         return ret;
771 }
772
773 static int eth_hn_remove(struct rte_vmbus_device *dev)
774 {
775         struct rte_eth_dev *eth_dev;
776         int ret;
777
778         PMD_INIT_FUNC_TRACE();
779
780         eth_dev = rte_eth_dev_allocated(dev->device.name);
781         if (!eth_dev)
782                 return -ENODEV;
783
784         ret = eth_hn_dev_uninit(eth_dev);
785         if (ret)
786                 return ret;
787
788         eth_dev_vmbus_release(eth_dev);
789         return 0;
790 }
791
792 /* Network device GUID */
793 static const rte_uuid_t hn_net_ids[] = {
794         /*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
795         RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
796         { 0 }
797 };
798
799 static struct rte_vmbus_driver rte_netvsc_pmd = {
800         .id_table = hn_net_ids,
801         .probe = eth_hn_probe,
802         .remove = eth_hn_remove,
803 };
804
805 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
806 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
807
808 RTE_INIT(hn_init_log);
809 static void
810 hn_init_log(void)
811 {
812         hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
813         if (hn_logtype_init >= 0)
814                 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
815         hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
816         if (hn_logtype_driver >= 0)
817                 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
818 }