net: add rte prefix to ether structures
[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         int ret;
171
172         if (!devargs)
173                 return 0;
174
175         PMD_INIT_LOG(DEBUG, "device args %s %s",
176                      devargs->name, devargs->args);
177
178         kvlist = rte_kvargs_parse(devargs->args, valid_keys);
179         if (!kvlist) {
180                 PMD_DRV_LOG(NOTICE, "invalid parameters");
181                 return -EINVAL;
182         }
183
184         ret = rte_kvargs_process(kvlist, "latency", hn_set_latency, hv);
185         if (ret)
186                 PMD_DRV_LOG(ERR, "Unable to process latency arg\n");
187
188         rte_kvargs_free(kvlist);
189         return ret;
190 }
191
192 /* Update link status.
193  * Note: the DPDK definition of "wait_to_complete"
194  *   means block this call until link is up.
195  *   which is not worth supporting.
196  */
197 int
198 hn_dev_link_update(struct rte_eth_dev *dev,
199                    int wait_to_complete)
200 {
201         struct hn_data *hv = dev->data->dev_private;
202         struct rte_eth_link link, old;
203         int error;
204
205         old = dev->data->dev_link;
206
207         error = hn_rndis_get_linkstatus(hv);
208         if (error)
209                 return error;
210
211         hn_rndis_get_linkspeed(hv);
212
213         hn_vf_link_update(dev, wait_to_complete);
214
215         link = (struct rte_eth_link) {
216                 .link_duplex = ETH_LINK_FULL_DUPLEX,
217                 .link_autoneg = ETH_LINK_SPEED_FIXED,
218                 .link_speed = hv->link_speed / 10000,
219         };
220
221         if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
222                 link.link_status = ETH_LINK_UP;
223         else
224                 link.link_status = ETH_LINK_DOWN;
225
226         if (old.link_status == link.link_status)
227                 return 0;
228
229         PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
230                      (link.link_status == ETH_LINK_UP) ? "up" : "down");
231
232         return rte_eth_linkstatus_set(dev, &link);
233 }
234
235 static void hn_dev_info_get(struct rte_eth_dev *dev,
236                             struct rte_eth_dev_info *dev_info)
237 {
238         struct hn_data *hv = dev->data->dev_private;
239
240         dev_info->speed_capa = ETH_LINK_SPEED_10G;
241         dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
242         dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
243         dev_info->max_mac_addrs  = 1;
244
245         dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
246         dev_info->flow_type_rss_offloads =
247                 ETH_RSS_IPV4 | ETH_RSS_IPV6 | ETH_RSS_TCP | ETH_RSS_UDP;
248
249         dev_info->max_rx_queues = hv->max_queues;
250         dev_info->max_tx_queues = hv->max_queues;
251
252         hn_rndis_get_offload(hv, dev_info);
253         hn_vf_info_get(hv, dev_info);
254 }
255
256 static void
257 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
258 {
259         struct hn_data *hv = dev->data->dev_private;
260
261         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
262         hn_vf_promiscuous_enable(dev);
263 }
264
265 static void
266 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
267 {
268         struct hn_data *hv = dev->data->dev_private;
269         uint32_t filter;
270
271         filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
272         if (dev->data->all_multicast)
273                 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
274         hn_rndis_set_rxfilter(hv, filter);
275         hn_vf_promiscuous_disable(dev);
276 }
277
278 static void
279 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
280 {
281         struct hn_data *hv = dev->data->dev_private;
282
283         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
284                               NDIS_PACKET_TYPE_ALL_MULTICAST |
285                         NDIS_PACKET_TYPE_BROADCAST);
286         hn_vf_allmulticast_enable(dev);
287 }
288
289 static void
290 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
291 {
292         struct hn_data *hv = dev->data->dev_private;
293
294         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
295                              NDIS_PACKET_TYPE_BROADCAST);
296         hn_vf_allmulticast_disable(dev);
297 }
298
299 static int
300 hn_dev_mc_addr_list(struct rte_eth_dev *dev,
301                      struct rte_ether_addr *mc_addr_set,
302                      uint32_t nb_mc_addr)
303 {
304         /* No filtering on the synthetic path, but can do it on VF */
305         return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
306 }
307
308 /* Setup shared rx/tx queue data */
309 static int hn_subchan_configure(struct hn_data *hv,
310                                 uint32_t subchan)
311 {
312         struct vmbus_channel *primary = hn_primary_chan(hv);
313         int err;
314         unsigned int retry = 0;
315
316         PMD_DRV_LOG(DEBUG,
317                     "open %u subchannels", subchan);
318
319         /* Send create sub channels command */
320         err = hn_nvs_alloc_subchans(hv, &subchan);
321         if (err)
322                 return  err;
323
324         while (subchan > 0) {
325                 struct vmbus_channel *new_sc;
326                 uint16_t chn_index;
327
328                 err = rte_vmbus_subchan_open(primary, &new_sc);
329                 if (err == -ENOENT && ++retry < 1000) {
330                         /* This can happen if not ready yet */
331                         rte_delay_ms(10);
332                         continue;
333                 }
334
335                 if (err) {
336                         PMD_DRV_LOG(ERR,
337                                     "open subchannel failed: %d", err);
338                         return err;
339                 }
340
341                 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
342
343                 retry = 0;
344                 chn_index = rte_vmbus_sub_channel_index(new_sc);
345                 if (chn_index == 0 || chn_index > hv->max_queues) {
346                         PMD_DRV_LOG(ERR,
347                                     "Invalid subchannel offermsg channel %u",
348                                     chn_index);
349                         return -EIO;
350                 }
351
352                 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
353                 hv->channels[chn_index] = new_sc;
354                 --subchan;
355         }
356
357         return err;
358 }
359
360 static int hn_dev_configure(struct rte_eth_dev *dev)
361 {
362         const struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
363         const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
364         const struct rte_eth_txmode *txmode = &dev_conf->txmode;
365
366         const struct rte_eth_rss_conf *rss_conf =
367                 &dev_conf->rx_adv_conf.rss_conf;
368         struct hn_data *hv = dev->data->dev_private;
369         uint64_t unsupported;
370         int err, subchan;
371
372         PMD_INIT_FUNC_TRACE();
373
374         unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
375         if (unsupported) {
376                 PMD_DRV_LOG(NOTICE,
377                             "unsupported TX offload: %#" PRIx64,
378                             unsupported);
379                 return -EINVAL;
380         }
381
382         unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
383         if (unsupported) {
384                 PMD_DRV_LOG(NOTICE,
385                             "unsupported RX offload: %#" PRIx64,
386                             rxmode->offloads);
387                 return -EINVAL;
388         }
389
390         err = hn_rndis_conf_offload(hv, txmode->offloads,
391                                     rxmode->offloads);
392         if (err) {
393                 PMD_DRV_LOG(NOTICE,
394                             "offload configure failed");
395                 return err;
396         }
397
398         hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
399                                  dev->data->nb_tx_queues);
400         subchan = hv->num_queues - 1;
401         if (subchan > 0) {
402                 err = hn_subchan_configure(hv, subchan);
403                 if (err) {
404                         PMD_DRV_LOG(NOTICE,
405                                     "subchannel configuration failed");
406                         return err;
407                 }
408
409                 err = hn_rndis_conf_rss(hv, rss_conf);
410                 if (err) {
411                         PMD_DRV_LOG(NOTICE,
412                                     "rss configuration failed");
413                         return err;
414                 }
415         }
416
417         return hn_vf_configure(dev, dev_conf);
418 }
419
420 static int hn_dev_stats_get(struct rte_eth_dev *dev,
421                             struct rte_eth_stats *stats)
422 {
423         unsigned int i;
424
425         hn_vf_stats_get(dev, stats);
426
427         for (i = 0; i < dev->data->nb_tx_queues; i++) {
428                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
429
430                 if (!txq)
431                         continue;
432
433                 stats->opackets += txq->stats.packets;
434                 stats->obytes += txq->stats.bytes;
435                 stats->oerrors += txq->stats.errors;
436
437                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
438                         stats->q_opackets[i] = txq->stats.packets;
439                         stats->q_obytes[i] = txq->stats.bytes;
440                 }
441         }
442
443         for (i = 0; i < dev->data->nb_rx_queues; i++) {
444                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
445
446                 if (!rxq)
447                         continue;
448
449                 stats->ipackets += rxq->stats.packets;
450                 stats->ibytes += rxq->stats.bytes;
451                 stats->ierrors += rxq->stats.errors;
452                 stats->imissed += rxq->stats.ring_full;
453
454                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
455                         stats->q_ipackets[i] = rxq->stats.packets;
456                         stats->q_ibytes[i] = rxq->stats.bytes;
457                 }
458         }
459
460         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
461         return 0;
462 }
463
464 static void
465 hn_dev_stats_reset(struct rte_eth_dev *dev)
466 {
467         unsigned int i;
468
469         PMD_INIT_FUNC_TRACE();
470
471         for (i = 0; i < dev->data->nb_tx_queues; i++) {
472                 struct hn_tx_queue *txq = dev->data->tx_queues[i];
473
474                 if (!txq)
475                         continue;
476                 memset(&txq->stats, 0, sizeof(struct hn_stats));
477         }
478
479         for (i = 0; i < dev->data->nb_rx_queues; i++) {
480                 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
481
482                 if (!rxq)
483                         continue;
484
485                 memset(&rxq->stats, 0, sizeof(struct hn_stats));
486         }
487 }
488
489 static void
490 hn_dev_xstats_reset(struct rte_eth_dev *dev)
491 {
492         hn_dev_stats_reset(dev);
493         hn_vf_xstats_reset(dev);
494 }
495
496 static int
497 hn_dev_xstats_count(struct rte_eth_dev *dev)
498 {
499         int ret, count;
500
501         count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
502         count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
503
504         ret = hn_vf_xstats_get_names(dev, NULL, 0);
505         if (ret < 0)
506                 return ret;
507
508         return count + ret;
509 }
510
511 static int
512 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
513                         struct rte_eth_xstat_name *xstats_names,
514                         unsigned int limit)
515 {
516         unsigned int i, t, count = 0;
517         int ret;
518
519         if (!xstats_names)
520                 return hn_dev_xstats_count(dev);
521
522         /* Note: limit checked in rte_eth_xstats_names() */
523         for (i = 0; i < dev->data->nb_tx_queues; i++) {
524                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
525
526                 if (!txq)
527                         continue;
528
529                 if (count >= limit)
530                         break;
531
532                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
533                         snprintf(xstats_names[count++].name,
534                                  RTE_ETH_XSTATS_NAME_SIZE,
535                                  "tx_q%u_%s", i, hn_stat_strings[t].name);
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                 if (count >= limit)
545                         break;
546
547                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
548                         snprintf(xstats_names[count++].name,
549                                  RTE_ETH_XSTATS_NAME_SIZE,
550                                  "rx_q%u_%s", i,
551                                  hn_stat_strings[t].name);
552         }
553
554         ret = hn_vf_xstats_get_names(dev, xstats_names + count,
555                                      limit - count);
556         if (ret < 0)
557                 return ret;
558
559         return count + ret;
560 }
561
562 static int
563 hn_dev_xstats_get(struct rte_eth_dev *dev,
564                   struct rte_eth_xstat *xstats,
565                   unsigned int n)
566 {
567         unsigned int i, t, count = 0;
568         const unsigned int nstats = hn_dev_xstats_count(dev);
569         const char *stats;
570         int ret;
571
572         PMD_INIT_FUNC_TRACE();
573
574         if (n < nstats)
575                 return nstats;
576
577         for (i = 0; i < dev->data->nb_tx_queues; i++) {
578                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
579
580                 if (!txq)
581                         continue;
582
583                 stats = (const char *)&txq->stats;
584                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
585                         xstats[count++].value = *(const uint64_t *)
586                                 (stats + hn_stat_strings[t].offset);
587         }
588
589         for (i = 0; i < dev->data->nb_rx_queues; i++) {
590                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
591
592                 if (!rxq)
593                         continue;
594
595                 stats = (const char *)&rxq->stats;
596                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
597                         xstats[count++].value = *(const uint64_t *)
598                                 (stats + hn_stat_strings[t].offset);
599         }
600
601         ret = hn_vf_xstats_get(dev, xstats + count, n - count);
602         if (ret < 0)
603                 return ret;
604
605         return count + ret;
606 }
607
608 static int
609 hn_dev_start(struct rte_eth_dev *dev)
610 {
611         struct hn_data *hv = dev->data->dev_private;
612         int error;
613
614         PMD_INIT_FUNC_TRACE();
615
616         error = hn_rndis_set_rxfilter(hv,
617                                       NDIS_PACKET_TYPE_BROADCAST |
618                                       NDIS_PACKET_TYPE_ALL_MULTICAST |
619                                       NDIS_PACKET_TYPE_DIRECTED);
620         if (error)
621                 return error;
622
623         error = hn_vf_start(dev);
624         if (error)
625                 hn_rndis_set_rxfilter(hv, 0);
626
627         return error;
628 }
629
630 static void
631 hn_dev_stop(struct rte_eth_dev *dev)
632 {
633         struct hn_data *hv = dev->data->dev_private;
634
635         PMD_INIT_FUNC_TRACE();
636
637         hn_rndis_set_rxfilter(hv, 0);
638         hn_vf_stop(dev);
639 }
640
641 static void
642 hn_dev_close(struct rte_eth_dev *dev __rte_unused)
643 {
644         PMD_INIT_LOG(DEBUG, "close");
645
646         hn_vf_close(dev);
647 }
648
649 static const struct eth_dev_ops hn_eth_dev_ops = {
650         .dev_configure          = hn_dev_configure,
651         .dev_start              = hn_dev_start,
652         .dev_stop               = hn_dev_stop,
653         .dev_close              = hn_dev_close,
654         .dev_infos_get          = hn_dev_info_get,
655         .dev_supported_ptypes_get = hn_vf_supported_ptypes,
656         .promiscuous_enable     = hn_dev_promiscuous_enable,
657         .promiscuous_disable    = hn_dev_promiscuous_disable,
658         .allmulticast_enable    = hn_dev_allmulticast_enable,
659         .allmulticast_disable   = hn_dev_allmulticast_disable,
660         .set_mc_addr_list       = hn_dev_mc_addr_list,
661         .tx_queue_setup         = hn_dev_tx_queue_setup,
662         .tx_queue_release       = hn_dev_tx_queue_release,
663         .tx_done_cleanup        = hn_dev_tx_done_cleanup,
664         .rx_queue_setup         = hn_dev_rx_queue_setup,
665         .rx_queue_release       = hn_dev_rx_queue_release,
666         .link_update            = hn_dev_link_update,
667         .stats_get              = hn_dev_stats_get,
668         .stats_reset            = hn_dev_stats_reset,
669         .xstats_get             = hn_dev_xstats_get,
670         .xstats_get_names       = hn_dev_xstats_get_names,
671         .xstats_reset           = hn_dev_xstats_reset,
672 };
673
674 /*
675  * Setup connection between PMD and kernel.
676  */
677 static int
678 hn_attach(struct hn_data *hv, unsigned int mtu)
679 {
680         int error;
681
682         /* Attach NVS */
683         error = hn_nvs_attach(hv, mtu);
684         if (error)
685                 goto failed_nvs;
686
687         /* Attach RNDIS */
688         error = hn_rndis_attach(hv);
689         if (error)
690                 goto failed_rndis;
691
692         /*
693          * NOTE:
694          * Under certain conditions on certain versions of Hyper-V,
695          * the RNDIS rxfilter is _not_ zero on the hypervisor side
696          * after the successful RNDIS initialization.
697          */
698         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
699         return 0;
700 failed_rndis:
701         hn_nvs_detach(hv);
702 failed_nvs:
703         return error;
704 }
705
706 static void
707 hn_detach(struct hn_data *hv)
708 {
709         hn_nvs_detach(hv);
710         hn_rndis_detach(hv);
711 }
712
713 static int
714 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
715 {
716         struct hn_data *hv = eth_dev->data->dev_private;
717         struct rte_device *device = eth_dev->device;
718         struct rte_vmbus_device *vmbus;
719         unsigned int rxr_cnt;
720         int err, max_chan;
721
722         PMD_INIT_FUNC_TRACE();
723
724         vmbus = container_of(device, struct rte_vmbus_device, device);
725         eth_dev->dev_ops = &hn_eth_dev_ops;
726         eth_dev->tx_pkt_burst = &hn_xmit_pkts;
727         eth_dev->rx_pkt_burst = &hn_recv_pkts;
728
729         /*
730          * for secondary processes, we don't initialize any further as primary
731          * has already done this work.
732          */
733         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
734                 return 0;
735
736         /* Since Hyper-V only supports one MAC address, just use local data */
737         eth_dev->data->mac_addrs = &hv->mac_addr;
738
739         hv->vmbus = vmbus;
740         hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
741         hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
742         hv->port_id = eth_dev->data->port_id;
743         hv->latency = HN_CHAN_LATENCY_NS;
744
745         err = hn_parse_args(eth_dev);
746         if (err)
747                 return err;
748
749         strlcpy(hv->owner.name, eth_dev->device->name,
750                 RTE_ETH_MAX_OWNER_NAME_LEN);
751         err = rte_eth_dev_owner_new(&hv->owner.id);
752         if (err) {
753                 PMD_INIT_LOG(ERR, "Can not get owner id");
754                 return err;
755         }
756
757         /* Initialize primary channel input for control operations */
758         err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
759         if (err)
760                 return err;
761
762         rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
763
764         hv->primary = hn_rx_queue_alloc(hv, 0,
765                                         eth_dev->device->numa_node);
766
767         if (!hv->primary)
768                 return -ENOMEM;
769
770         err = hn_attach(hv, ETHER_MTU);
771         if  (err)
772                 goto failed;
773
774         err = hn_tx_pool_init(eth_dev);
775         if (err)
776                 goto failed;
777
778         err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
779         if (err)
780                 goto failed;
781
782         max_chan = rte_vmbus_max_channels(vmbus);
783         PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
784         if (max_chan <= 0)
785                 goto failed;
786
787         if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
788                 rxr_cnt = 1;
789
790         hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
791
792         /* If VF was reported but not added, do it now */
793         if (hv->vf_present && !hv->vf_dev) {
794                 PMD_INIT_LOG(DEBUG, "Adding VF device");
795
796                 err = hn_vf_add(eth_dev, hv);
797                 if (err)
798                         goto failed;
799         }
800
801         return 0;
802
803 failed:
804         PMD_INIT_LOG(NOTICE, "device init failed");
805
806         hn_detach(hv);
807         return err;
808 }
809
810 static int
811 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
812 {
813         struct hn_data *hv = eth_dev->data->dev_private;
814
815         PMD_INIT_FUNC_TRACE();
816
817         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
818                 return 0;
819
820         hn_dev_stop(eth_dev);
821         hn_dev_close(eth_dev);
822
823         eth_dev->dev_ops = NULL;
824         eth_dev->tx_pkt_burst = NULL;
825         eth_dev->rx_pkt_burst = NULL;
826
827         hn_detach(hv);
828         rte_vmbus_chan_close(hv->primary->chan);
829         rte_free(hv->primary);
830         rte_eth_dev_owner_delete(hv->owner.id);
831
832         eth_dev->data->mac_addrs = NULL;
833
834         return 0;
835 }
836
837 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
838                         struct rte_vmbus_device *dev)
839 {
840         struct rte_eth_dev *eth_dev;
841         int ret;
842
843         PMD_INIT_FUNC_TRACE();
844
845         eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
846         if (!eth_dev)
847                 return -ENOMEM;
848
849         ret = eth_hn_dev_init(eth_dev);
850         if (ret)
851                 eth_dev_vmbus_release(eth_dev);
852         else
853                 rte_eth_dev_probing_finish(eth_dev);
854
855         return ret;
856 }
857
858 static int eth_hn_remove(struct rte_vmbus_device *dev)
859 {
860         struct rte_eth_dev *eth_dev;
861         int ret;
862
863         PMD_INIT_FUNC_TRACE();
864
865         eth_dev = rte_eth_dev_allocated(dev->device.name);
866         if (!eth_dev)
867                 return -ENODEV;
868
869         ret = eth_hn_dev_uninit(eth_dev);
870         if (ret)
871                 return ret;
872
873         eth_dev_vmbus_release(eth_dev);
874         return 0;
875 }
876
877 /* Network device GUID */
878 static const rte_uuid_t hn_net_ids[] = {
879         /*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
880         RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
881         { 0 }
882 };
883
884 static struct rte_vmbus_driver rte_netvsc_pmd = {
885         .id_table = hn_net_ids,
886         .probe = eth_hn_probe,
887         .remove = eth_hn_remove,
888 };
889
890 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
891 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
892
893 RTE_INIT(hn_init_log);
894 static void
895 hn_init_log(void)
896 {
897         hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
898         if (hn_logtype_init >= 0)
899                 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
900         hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
901         if (hn_logtype_driver >= 0)
902                 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
903 }