5ef7a452cc691aa53b5bbc084deca0f5ae2cd35d
[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                             DEV_RX_OFFLOAD_RSS_HASH)
47
48 int hn_logtype_init;
49 int hn_logtype_driver;
50
51 struct hn_xstats_name_off {
52         char name[RTE_ETH_XSTATS_NAME_SIZE];
53         unsigned int offset;
54 };
55
56 static const struct hn_xstats_name_off hn_stat_strings[] = {
57         { "good_packets",           offsetof(struct hn_stats, packets) },
58         { "good_bytes",             offsetof(struct hn_stats, bytes) },
59         { "errors",                 offsetof(struct hn_stats, errors) },
60         { "ring full",              offsetof(struct hn_stats, ring_full) },
61         { "multicast_packets",      offsetof(struct hn_stats, multicast) },
62         { "broadcast_packets",      offsetof(struct hn_stats, broadcast) },
63         { "undersize_packets",      offsetof(struct hn_stats, size_bins[0]) },
64         { "size_64_packets",        offsetof(struct hn_stats, size_bins[1]) },
65         { "size_65_127_packets",    offsetof(struct hn_stats, size_bins[2]) },
66         { "size_128_255_packets",   offsetof(struct hn_stats, size_bins[3]) },
67         { "size_256_511_packets",   offsetof(struct hn_stats, size_bins[4]) },
68         { "size_512_1023_packets",  offsetof(struct hn_stats, size_bins[5]) },
69         { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
70         { "size_1519_max_packets",  offsetof(struct hn_stats, size_bins[7]) },
71 };
72
73 /* The default RSS key.
74  * This value is the same as MLX5 so that flows will be
75  * received on same path for both VF ans synthetic NIC.
76  */
77 static const uint8_t rss_default_key[NDIS_HASH_KEYSIZE_TOEPLITZ] = {
78         0x2c, 0xc6, 0x81, 0xd1, 0x5b, 0xdb, 0xf4, 0xf7,
79         0xfc, 0xa2, 0x83, 0x19, 0xdb, 0x1a, 0x3e, 0x94,
80         0x6b, 0x9e, 0x38, 0xd9, 0x2c, 0x9c, 0x03, 0xd1,
81         0xad, 0x99, 0x44, 0xa7, 0xd9, 0x56, 0x3d, 0x59,
82         0x06, 0x3c, 0x25, 0xf3, 0xfc, 0x1f, 0xdc, 0x2a,
83 };
84
85 static struct rte_eth_dev *
86 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
87 {
88         struct rte_eth_dev *eth_dev;
89         const char *name;
90
91         if (!dev)
92                 return NULL;
93
94         name = dev->device.name;
95
96         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
97                 eth_dev = rte_eth_dev_allocate(name);
98                 if (!eth_dev) {
99                         PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
100                         return NULL;
101                 }
102
103                 if (private_data_size) {
104                         eth_dev->data->dev_private =
105                                 rte_zmalloc_socket(name, private_data_size,
106                                                      RTE_CACHE_LINE_SIZE, dev->device.numa_node);
107                         if (!eth_dev->data->dev_private) {
108                                 PMD_DRV_LOG(NOTICE, "can not allocate driver data");
109                                 rte_eth_dev_release_port(eth_dev);
110                                 return NULL;
111                         }
112                 }
113         } else {
114                 eth_dev = rte_eth_dev_attach_secondary(name);
115                 if (!eth_dev) {
116                         PMD_DRV_LOG(NOTICE, "can not attach secondary");
117                         return NULL;
118                 }
119         }
120
121         eth_dev->device = &dev->device;
122
123         /* interrupt is simulated */
124         dev->intr_handle.type = RTE_INTR_HANDLE_EXT;
125         eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
126         eth_dev->intr_handle = &dev->intr_handle;
127
128         /* allow ethdev to remove on close */
129         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
130
131         return eth_dev;
132 }
133
134 static void
135 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
136 {
137         /* mac_addrs must not be freed alone because part of dev_private */
138         eth_dev->data->mac_addrs = NULL;
139         /* free ether device */
140         rte_eth_dev_release_port(eth_dev);
141
142         eth_dev->device = NULL;
143         eth_dev->intr_handle = NULL;
144 }
145
146 /* handle "latency=X" from devargs */
147 static int hn_set_latency(const char *key, const char *value, void *opaque)
148 {
149         struct hn_data *hv = opaque;
150         char *endp = NULL;
151         unsigned long lat;
152
153         errno = 0;
154         lat = strtoul(value, &endp, 0);
155
156         if (*value == '\0' || *endp != '\0') {
157                 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
158                 return -EINVAL;
159         }
160
161         PMD_DRV_LOG(DEBUG, "set latency %lu usec", lat);
162
163         hv->latency = lat * 1000;       /* usec to nsec */
164         return 0;
165 }
166
167 /* Parse device arguments */
168 static int hn_parse_args(const struct rte_eth_dev *dev)
169 {
170         struct hn_data *hv = dev->data->dev_private;
171         struct rte_devargs *devargs = dev->device->devargs;
172         static const char * const valid_keys[] = {
173                 "latency",
174                 NULL
175         };
176         struct rte_kvargs *kvlist;
177         int ret;
178
179         if (!devargs)
180                 return 0;
181
182         PMD_INIT_LOG(DEBUG, "device args %s %s",
183                      devargs->name, devargs->args);
184
185         kvlist = rte_kvargs_parse(devargs->args, valid_keys);
186         if (!kvlist) {
187                 PMD_DRV_LOG(NOTICE, "invalid parameters");
188                 return -EINVAL;
189         }
190
191         ret = rte_kvargs_process(kvlist, "latency", hn_set_latency, hv);
192         if (ret)
193                 PMD_DRV_LOG(ERR, "Unable to process latency arg\n");
194
195         rte_kvargs_free(kvlist);
196         return ret;
197 }
198
199 /* Update link status.
200  * Note: the DPDK definition of "wait_to_complete"
201  *   means block this call until link is up.
202  *   which is not worth supporting.
203  */
204 int
205 hn_dev_link_update(struct rte_eth_dev *dev,
206                    int wait_to_complete)
207 {
208         struct hn_data *hv = dev->data->dev_private;
209         struct rte_eth_link link, old;
210         int error;
211
212         old = dev->data->dev_link;
213
214         error = hn_rndis_get_linkstatus(hv);
215         if (error)
216                 return error;
217
218         hn_rndis_get_linkspeed(hv);
219
220         hn_vf_link_update(dev, wait_to_complete);
221
222         link = (struct rte_eth_link) {
223                 .link_duplex = ETH_LINK_FULL_DUPLEX,
224                 .link_autoneg = ETH_LINK_SPEED_FIXED,
225                 .link_speed = hv->link_speed / 10000,
226         };
227
228         if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
229                 link.link_status = ETH_LINK_UP;
230         else
231                 link.link_status = ETH_LINK_DOWN;
232
233         if (old.link_status == link.link_status)
234                 return 0;
235
236         PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
237                      (link.link_status == ETH_LINK_UP) ? "up" : "down");
238
239         return rte_eth_linkstatus_set(dev, &link);
240 }
241
242 static int hn_dev_info_get(struct rte_eth_dev *dev,
243                            struct rte_eth_dev_info *dev_info)
244 {
245         struct hn_data *hv = dev->data->dev_private;
246         int rc;
247
248         dev_info->speed_capa = ETH_LINK_SPEED_10G;
249         dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
250         dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
251         dev_info->max_mac_addrs  = 1;
252
253         dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
254         dev_info->flow_type_rss_offloads = hv->rss_offloads;
255         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
256
257         dev_info->max_rx_queues = hv->max_queues;
258         dev_info->max_tx_queues = hv->max_queues;
259
260         rc = hn_rndis_get_offload(hv, dev_info);
261         if (rc != 0)
262                 return rc;
263
264         rc = hn_vf_info_get(hv, dev_info);
265         if (rc != 0)
266                 return rc;
267
268         return 0;
269 }
270
271 static int hn_rss_reta_update(struct rte_eth_dev *dev,
272                               struct rte_eth_rss_reta_entry64 *reta_conf,
273                               uint16_t reta_size)
274 {
275         struct hn_data *hv = dev->data->dev_private;
276         unsigned int i;
277         int err;
278
279         PMD_INIT_FUNC_TRACE();
280
281         if (reta_size != NDIS_HASH_INDCNT) {
282                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
283                 return -EINVAL;
284         }
285
286         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
287                 uint16_t idx = i / RTE_RETA_GROUP_SIZE;
288                 uint16_t shift = i % RTE_RETA_GROUP_SIZE;
289                 uint64_t mask = (uint64_t)1 << shift;
290
291                 if (reta_conf[idx].mask & mask)
292                         hv->rss_ind[i] = reta_conf[idx].reta[shift];
293         }
294
295         err = hn_rndis_conf_rss(hv, 0);
296         if (err) {
297                 PMD_DRV_LOG(NOTICE,
298                             "reta reconfig failed");
299                 return err;
300         }
301
302         return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
303 }
304
305 static int hn_rss_reta_query(struct rte_eth_dev *dev,
306                              struct rte_eth_rss_reta_entry64 *reta_conf,
307                              uint16_t reta_size)
308 {
309         struct hn_data *hv = dev->data->dev_private;
310         unsigned int i;
311
312         PMD_INIT_FUNC_TRACE();
313
314         if (reta_size != NDIS_HASH_INDCNT) {
315                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
316                 return -EINVAL;
317         }
318
319         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
320                 uint16_t idx = i / RTE_RETA_GROUP_SIZE;
321                 uint16_t shift = i % RTE_RETA_GROUP_SIZE;
322                 uint64_t mask = (uint64_t)1 << shift;
323
324                 if (reta_conf[idx].mask & mask)
325                         reta_conf[idx].reta[shift] = hv->rss_ind[i];
326         }
327         return 0;
328 }
329
330 static void hn_rss_hash_init(struct hn_data *hv,
331                              const struct rte_eth_rss_conf *rss_conf)
332 {
333         /* Convert from DPDK RSS hash flags to NDIS hash flags */
334         hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
335
336         if (rss_conf->rss_hf & ETH_RSS_IPV4)
337                 hv->rss_hash |= NDIS_HASH_IPV4;
338         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
339                 hv->rss_hash |= NDIS_HASH_TCP_IPV4;
340         if (rss_conf->rss_hf & ETH_RSS_IPV6)
341                 hv->rss_hash |=  NDIS_HASH_IPV6;
342         if (rss_conf->rss_hf & ETH_RSS_IPV6_EX)
343                 hv->rss_hash |=  NDIS_HASH_IPV6_EX;
344         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
345                 hv->rss_hash |= NDIS_HASH_TCP_IPV6;
346         if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX)
347                 hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
348
349         memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
350                NDIS_HASH_KEYSIZE_TOEPLITZ);
351 }
352
353 static int hn_rss_hash_update(struct rte_eth_dev *dev,
354                               struct rte_eth_rss_conf *rss_conf)
355 {
356         struct hn_data *hv = dev->data->dev_private;
357         int err;
358
359         PMD_INIT_FUNC_TRACE();
360
361         err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
362         if (err) {
363                 PMD_DRV_LOG(NOTICE,
364                             "rss disable failed");
365                 return err;
366         }
367
368         hn_rss_hash_init(hv, rss_conf);
369
370         err = hn_rndis_conf_rss(hv, 0);
371         if (err) {
372                 PMD_DRV_LOG(NOTICE,
373                             "rss reconfig failed (RSS disabled)");
374                 return err;
375         }
376
377
378         return hn_vf_rss_hash_update(dev, rss_conf);
379 }
380
381 static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
382                                 struct rte_eth_rss_conf *rss_conf)
383 {
384         struct hn_data *hv = dev->data->dev_private;
385
386         PMD_INIT_FUNC_TRACE();
387
388         if (hv->ndis_ver < NDIS_VERSION_6_20) {
389                 PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
390                 return -EOPNOTSUPP;
391         }
392
393         rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ;
394         if (rss_conf->rss_key)
395                 memcpy(rss_conf->rss_key, hv->rss_key,
396                        NDIS_HASH_KEYSIZE_TOEPLITZ);
397
398         rss_conf->rss_hf = 0;
399         if (hv->rss_hash & NDIS_HASH_IPV4)
400                 rss_conf->rss_hf |= ETH_RSS_IPV4;
401
402         if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
403                 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
404
405         if (hv->rss_hash & NDIS_HASH_IPV6)
406                 rss_conf->rss_hf |= ETH_RSS_IPV6;
407
408         if (hv->rss_hash & NDIS_HASH_IPV6_EX)
409                 rss_conf->rss_hf |= ETH_RSS_IPV6_EX;
410
411         if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
412                 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
413
414         if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
415                 rss_conf->rss_hf |= ETH_RSS_IPV6_TCP_EX;
416
417         return 0;
418 }
419
420 static int
421 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
422 {
423         struct hn_data *hv = dev->data->dev_private;
424
425         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
426         return hn_vf_promiscuous_enable(dev);
427 }
428
429 static int
430 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
431 {
432         struct hn_data *hv = dev->data->dev_private;
433         uint32_t filter;
434
435         filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
436         if (dev->data->all_multicast)
437                 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
438         hn_rndis_set_rxfilter(hv, filter);
439         return hn_vf_promiscuous_disable(dev);
440 }
441
442 static int
443 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
444 {
445         struct hn_data *hv = dev->data->dev_private;
446
447         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
448                               NDIS_PACKET_TYPE_ALL_MULTICAST |
449                         NDIS_PACKET_TYPE_BROADCAST);
450         return hn_vf_allmulticast_enable(dev);
451 }
452
453 static int
454 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
455 {
456         struct hn_data *hv = dev->data->dev_private;
457
458         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
459                              NDIS_PACKET_TYPE_BROADCAST);
460         return hn_vf_allmulticast_disable(dev);
461 }
462
463 static int
464 hn_dev_mc_addr_list(struct rte_eth_dev *dev,
465                      struct rte_ether_addr *mc_addr_set,
466                      uint32_t nb_mc_addr)
467 {
468         /* No filtering on the synthetic path, but can do it on VF */
469         return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
470 }
471
472 /* Setup shared rx/tx queue data */
473 static int hn_subchan_configure(struct hn_data *hv,
474                                 uint32_t subchan)
475 {
476         struct vmbus_channel *primary = hn_primary_chan(hv);
477         int err;
478         unsigned int retry = 0;
479
480         PMD_DRV_LOG(DEBUG,
481                     "open %u subchannels", subchan);
482
483         /* Send create sub channels command */
484         err = hn_nvs_alloc_subchans(hv, &subchan);
485         if (err)
486                 return  err;
487
488         while (subchan > 0) {
489                 struct vmbus_channel *new_sc;
490                 uint16_t chn_index;
491
492                 err = rte_vmbus_subchan_open(primary, &new_sc);
493                 if (err == -ENOENT && ++retry < 1000) {
494                         /* This can happen if not ready yet */
495                         rte_delay_ms(10);
496                         continue;
497                 }
498
499                 if (err) {
500                         PMD_DRV_LOG(ERR,
501                                     "open subchannel failed: %d", err);
502                         return err;
503                 }
504
505                 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
506
507                 retry = 0;
508                 chn_index = rte_vmbus_sub_channel_index(new_sc);
509                 if (chn_index == 0 || chn_index > hv->max_queues) {
510                         PMD_DRV_LOG(ERR,
511                                     "Invalid subchannel offermsg channel %u",
512                                     chn_index);
513                         return -EIO;
514                 }
515
516                 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
517                 hv->channels[chn_index] = new_sc;
518                 --subchan;
519         }
520
521         return err;
522 }
523
524 static int hn_dev_configure(struct rte_eth_dev *dev)
525 {
526         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
527         struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
528         const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
529         const struct rte_eth_txmode *txmode = &dev_conf->txmode;
530         struct hn_data *hv = dev->data->dev_private;
531         uint64_t unsupported;
532         int i, err, subchan;
533
534         PMD_INIT_FUNC_TRACE();
535
536         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
537                 dev_conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
538
539         unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
540         if (unsupported) {
541                 PMD_DRV_LOG(NOTICE,
542                             "unsupported TX offload: %#" PRIx64,
543                             unsupported);
544                 return -EINVAL;
545         }
546
547         unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
548         if (unsupported) {
549                 PMD_DRV_LOG(NOTICE,
550                             "unsupported RX offload: %#" PRIx64,
551                             rxmode->offloads);
552                 return -EINVAL;
553         }
554
555         hv->vlan_strip = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
556
557         err = hn_rndis_conf_offload(hv, txmode->offloads,
558                                     rxmode->offloads);
559         if (err) {
560                 PMD_DRV_LOG(NOTICE,
561                             "offload configure failed");
562                 return err;
563         }
564
565         hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
566                                  dev->data->nb_tx_queues);
567
568         for (i = 0; i < NDIS_HASH_INDCNT; i++)
569                 hv->rss_ind[i] = i % hv->num_queues;
570
571         hn_rss_hash_init(hv, rss_conf);
572
573         subchan = hv->num_queues - 1;
574         if (subchan > 0) {
575                 err = hn_subchan_configure(hv, subchan);
576                 if (err) {
577                         PMD_DRV_LOG(NOTICE,
578                                     "subchannel configuration failed");
579                         return err;
580                 }
581
582                 err = hn_rndis_conf_rss(hv, 0);
583                 if (err) {
584                         PMD_DRV_LOG(NOTICE,
585                                     "initial RSS config failed");
586                         return err;
587                 }
588         }
589
590         return hn_vf_configure(dev, dev_conf);
591 }
592
593 static int hn_dev_stats_get(struct rte_eth_dev *dev,
594                             struct rte_eth_stats *stats)
595 {
596         unsigned int i;
597
598         hn_vf_stats_get(dev, stats);
599
600         for (i = 0; i < dev->data->nb_tx_queues; i++) {
601                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
602
603                 if (!txq)
604                         continue;
605
606                 stats->opackets += txq->stats.packets;
607                 stats->obytes += txq->stats.bytes;
608                 stats->oerrors += txq->stats.errors;
609
610                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
611                         stats->q_opackets[i] = txq->stats.packets;
612                         stats->q_obytes[i] = txq->stats.bytes;
613                 }
614         }
615
616         for (i = 0; i < dev->data->nb_rx_queues; i++) {
617                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
618
619                 if (!rxq)
620                         continue;
621
622                 stats->ipackets += rxq->stats.packets;
623                 stats->ibytes += rxq->stats.bytes;
624                 stats->ierrors += rxq->stats.errors;
625                 stats->imissed += rxq->stats.ring_full;
626
627                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
628                         stats->q_ipackets[i] = rxq->stats.packets;
629                         stats->q_ibytes[i] = rxq->stats.bytes;
630                 }
631         }
632
633         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
634         return 0;
635 }
636
637 static int
638 hn_dev_stats_reset(struct rte_eth_dev *dev)
639 {
640         unsigned int i;
641
642         PMD_INIT_FUNC_TRACE();
643
644         for (i = 0; i < dev->data->nb_tx_queues; i++) {
645                 struct hn_tx_queue *txq = dev->data->tx_queues[i];
646
647                 if (!txq)
648                         continue;
649                 memset(&txq->stats, 0, sizeof(struct hn_stats));
650         }
651
652         for (i = 0; i < dev->data->nb_rx_queues; i++) {
653                 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
654
655                 if (!rxq)
656                         continue;
657
658                 memset(&rxq->stats, 0, sizeof(struct hn_stats));
659         }
660
661         return 0;
662 }
663
664 static int
665 hn_dev_xstats_reset(struct rte_eth_dev *dev)
666 {
667         int ret;
668
669         ret = hn_dev_stats_reset(dev);
670         if (ret != 0)
671                 return 0;
672
673         return hn_vf_xstats_reset(dev);
674 }
675
676 static int
677 hn_dev_xstats_count(struct rte_eth_dev *dev)
678 {
679         int ret, count;
680
681         count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
682         count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
683
684         ret = hn_vf_xstats_get_names(dev, NULL, 0);
685         if (ret < 0)
686                 return ret;
687
688         return count + ret;
689 }
690
691 static int
692 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
693                         struct rte_eth_xstat_name *xstats_names,
694                         unsigned int limit)
695 {
696         unsigned int i, t, count = 0;
697         int ret;
698
699         if (!xstats_names)
700                 return hn_dev_xstats_count(dev);
701
702         /* Note: limit checked in rte_eth_xstats_names() */
703         for (i = 0; i < dev->data->nb_tx_queues; i++) {
704                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
705
706                 if (!txq)
707                         continue;
708
709                 if (count >= limit)
710                         break;
711
712                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
713                         snprintf(xstats_names[count++].name,
714                                  RTE_ETH_XSTATS_NAME_SIZE,
715                                  "tx_q%u_%s", i, hn_stat_strings[t].name);
716         }
717
718         for (i = 0; i < dev->data->nb_rx_queues; i++)  {
719                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
720
721                 if (!rxq)
722                         continue;
723
724                 if (count >= limit)
725                         break;
726
727                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
728                         snprintf(xstats_names[count++].name,
729                                  RTE_ETH_XSTATS_NAME_SIZE,
730                                  "rx_q%u_%s", i,
731                                  hn_stat_strings[t].name);
732         }
733
734         ret = hn_vf_xstats_get_names(dev, xstats_names + count,
735                                      limit - count);
736         if (ret < 0)
737                 return ret;
738
739         return count + ret;
740 }
741
742 static int
743 hn_dev_xstats_get(struct rte_eth_dev *dev,
744                   struct rte_eth_xstat *xstats,
745                   unsigned int n)
746 {
747         unsigned int i, t, count = 0;
748         const unsigned int nstats = hn_dev_xstats_count(dev);
749         const char *stats;
750         int ret;
751
752         PMD_INIT_FUNC_TRACE();
753
754         if (n < nstats)
755                 return nstats;
756
757         for (i = 0; i < dev->data->nb_tx_queues; i++) {
758                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
759
760                 if (!txq)
761                         continue;
762
763                 stats = (const char *)&txq->stats;
764                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
765                         xstats[count].id = count;
766                         xstats[count].value = *(const uint64_t *)
767                                 (stats + hn_stat_strings[t].offset);
768                 }
769         }
770
771         for (i = 0; i < dev->data->nb_rx_queues; i++) {
772                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
773
774                 if (!rxq)
775                         continue;
776
777                 stats = (const char *)&rxq->stats;
778                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
779                         xstats[count].id = count;
780                         xstats[count].value = *(const uint64_t *)
781                                 (stats + hn_stat_strings[t].offset);
782                 }
783         }
784
785         ret = hn_vf_xstats_get(dev, xstats, count, n);
786         if (ret < 0)
787                 return ret;
788
789         return count + ret;
790 }
791
792 static int
793 hn_dev_start(struct rte_eth_dev *dev)
794 {
795         struct hn_data *hv = dev->data->dev_private;
796         int error;
797
798         PMD_INIT_FUNC_TRACE();
799
800         error = hn_rndis_set_rxfilter(hv,
801                                       NDIS_PACKET_TYPE_BROADCAST |
802                                       NDIS_PACKET_TYPE_ALL_MULTICAST |
803                                       NDIS_PACKET_TYPE_DIRECTED);
804         if (error)
805                 return error;
806
807         error = hn_vf_start(dev);
808         if (error)
809                 hn_rndis_set_rxfilter(hv, 0);
810
811         return error;
812 }
813
814 static void
815 hn_dev_stop(struct rte_eth_dev *dev)
816 {
817         struct hn_data *hv = dev->data->dev_private;
818
819         PMD_INIT_FUNC_TRACE();
820
821         hn_rndis_set_rxfilter(hv, 0);
822         hn_vf_stop(dev);
823 }
824
825 static void
826 hn_dev_close(struct rte_eth_dev *dev)
827 {
828         PMD_INIT_FUNC_TRACE();
829
830         hn_vf_close(dev);
831         hn_dev_free_queues(dev);
832 }
833
834 static const struct eth_dev_ops hn_eth_dev_ops = {
835         .dev_configure          = hn_dev_configure,
836         .dev_start              = hn_dev_start,
837         .dev_stop               = hn_dev_stop,
838         .dev_close              = hn_dev_close,
839         .dev_infos_get          = hn_dev_info_get,
840         .dev_supported_ptypes_get = hn_vf_supported_ptypes,
841         .promiscuous_enable     = hn_dev_promiscuous_enable,
842         .promiscuous_disable    = hn_dev_promiscuous_disable,
843         .allmulticast_enable    = hn_dev_allmulticast_enable,
844         .allmulticast_disable   = hn_dev_allmulticast_disable,
845         .set_mc_addr_list       = hn_dev_mc_addr_list,
846         .reta_update            = hn_rss_reta_update,
847         .reta_query             = hn_rss_reta_query,
848         .rss_hash_update        = hn_rss_hash_update,
849         .rss_hash_conf_get      = hn_rss_hash_conf_get,
850         .tx_queue_setup         = hn_dev_tx_queue_setup,
851         .tx_queue_release       = hn_dev_tx_queue_release,
852         .tx_done_cleanup        = hn_dev_tx_done_cleanup,
853         .rx_queue_setup         = hn_dev_rx_queue_setup,
854         .rx_queue_release       = hn_dev_rx_queue_release,
855         .link_update            = hn_dev_link_update,
856         .stats_get              = hn_dev_stats_get,
857         .stats_reset            = hn_dev_stats_reset,
858         .xstats_get             = hn_dev_xstats_get,
859         .xstats_get_names       = hn_dev_xstats_get_names,
860         .xstats_reset           = hn_dev_xstats_reset,
861 };
862
863 /*
864  * Setup connection between PMD and kernel.
865  */
866 static int
867 hn_attach(struct hn_data *hv, unsigned int mtu)
868 {
869         int error;
870
871         /* Attach NVS */
872         error = hn_nvs_attach(hv, mtu);
873         if (error)
874                 goto failed_nvs;
875
876         /* Attach RNDIS */
877         error = hn_rndis_attach(hv);
878         if (error)
879                 goto failed_rndis;
880
881         /*
882          * NOTE:
883          * Under certain conditions on certain versions of Hyper-V,
884          * the RNDIS rxfilter is _not_ zero on the hypervisor side
885          * after the successful RNDIS initialization.
886          */
887         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
888         return 0;
889 failed_rndis:
890         hn_nvs_detach(hv);
891 failed_nvs:
892         return error;
893 }
894
895 static void
896 hn_detach(struct hn_data *hv)
897 {
898         hn_nvs_detach(hv);
899         hn_rndis_detach(hv);
900 }
901
902 static int
903 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
904 {
905         struct hn_data *hv = eth_dev->data->dev_private;
906         struct rte_device *device = eth_dev->device;
907         struct rte_vmbus_device *vmbus;
908         unsigned int rxr_cnt;
909         int err, max_chan;
910
911         PMD_INIT_FUNC_TRACE();
912
913         vmbus = container_of(device, struct rte_vmbus_device, device);
914         eth_dev->dev_ops = &hn_eth_dev_ops;
915         eth_dev->tx_pkt_burst = &hn_xmit_pkts;
916         eth_dev->rx_pkt_burst = &hn_recv_pkts;
917
918         /*
919          * for secondary processes, we don't initialize any further as primary
920          * has already done this work.
921          */
922         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
923                 return 0;
924
925         /* Since Hyper-V only supports one MAC address, just use local data */
926         eth_dev->data->mac_addrs = &hv->mac_addr;
927
928         hv->vmbus = vmbus;
929         hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
930         hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
931         hv->port_id = eth_dev->data->port_id;
932         hv->latency = HN_CHAN_LATENCY_NS;
933         hv->max_queues = 1;
934         rte_spinlock_init(&hv->vf_lock);
935         hv->vf_port = HN_INVALID_PORT;
936
937         err = hn_parse_args(eth_dev);
938         if (err)
939                 return err;
940
941         strlcpy(hv->owner.name, eth_dev->device->name,
942                 RTE_ETH_MAX_OWNER_NAME_LEN);
943         err = rte_eth_dev_owner_new(&hv->owner.id);
944         if (err) {
945                 PMD_INIT_LOG(ERR, "Can not get owner id");
946                 return err;
947         }
948
949         /* Initialize primary channel input for control operations */
950         err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
951         if (err)
952                 return err;
953
954         rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
955
956         hv->primary = hn_rx_queue_alloc(hv, 0,
957                                         eth_dev->device->numa_node);
958
959         if (!hv->primary)
960                 return -ENOMEM;
961
962         err = hn_attach(hv, RTE_ETHER_MTU);
963         if  (err)
964                 goto failed;
965
966         err = hn_tx_pool_init(eth_dev);
967         if (err)
968                 goto failed;
969
970         err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
971         if (err)
972                 goto failed;
973
974         /* Multi queue requires later versions of windows server */
975         if (hv->nvs_ver < NVS_VERSION_5)
976                 return 0;
977
978         max_chan = rte_vmbus_max_channels(vmbus);
979         PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
980         if (max_chan <= 0)
981                 goto failed;
982
983         if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
984                 rxr_cnt = 1;
985
986         hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
987
988         /* If VF was reported but not added, do it now */
989         if (hv->vf_present && !hn_vf_attached(hv)) {
990                 PMD_INIT_LOG(DEBUG, "Adding VF device");
991
992                 err = hn_vf_add(eth_dev, hv);
993                 if (err)
994                         hv->vf_present = 0;
995         }
996
997         return 0;
998
999 failed:
1000         PMD_INIT_LOG(NOTICE, "device init failed");
1001
1002         hn_tx_pool_uninit(eth_dev);
1003         hn_detach(hv);
1004         return err;
1005 }
1006
1007 static int
1008 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1009 {
1010         struct hn_data *hv = eth_dev->data->dev_private;
1011         int ret;
1012
1013         PMD_INIT_FUNC_TRACE();
1014
1015         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1016                 return 0;
1017
1018         hn_dev_stop(eth_dev);
1019         hn_dev_close(eth_dev);
1020
1021         eth_dev->dev_ops = NULL;
1022         eth_dev->tx_pkt_burst = NULL;
1023         eth_dev->rx_pkt_burst = NULL;
1024
1025         hn_detach(hv);
1026         hn_tx_pool_uninit(eth_dev);
1027         rte_vmbus_chan_close(hv->primary->chan);
1028         rte_free(hv->primary);
1029         ret = rte_eth_dev_owner_delete(hv->owner.id);
1030         if (ret != 0)
1031                 return ret;
1032
1033         return 0;
1034 }
1035
1036 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
1037                         struct rte_vmbus_device *dev)
1038 {
1039         struct rte_eth_dev *eth_dev;
1040         int ret;
1041
1042         PMD_INIT_FUNC_TRACE();
1043
1044         eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
1045         if (!eth_dev)
1046                 return -ENOMEM;
1047
1048         ret = eth_hn_dev_init(eth_dev);
1049         if (ret)
1050                 eth_dev_vmbus_release(eth_dev);
1051         else
1052                 rte_eth_dev_probing_finish(eth_dev);
1053
1054         return ret;
1055 }
1056
1057 static int eth_hn_remove(struct rte_vmbus_device *dev)
1058 {
1059         struct rte_eth_dev *eth_dev;
1060         int ret;
1061
1062         PMD_INIT_FUNC_TRACE();
1063
1064         eth_dev = rte_eth_dev_allocated(dev->device.name);
1065         if (!eth_dev)
1066                 return -ENODEV;
1067
1068         ret = eth_hn_dev_uninit(eth_dev);
1069         if (ret)
1070                 return ret;
1071
1072         eth_dev_vmbus_release(eth_dev);
1073         return 0;
1074 }
1075
1076 /* Network device GUID */
1077 static const rte_uuid_t hn_net_ids[] = {
1078         /*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
1079         RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
1080         { 0 }
1081 };
1082
1083 static struct rte_vmbus_driver rte_netvsc_pmd = {
1084         .id_table = hn_net_ids,
1085         .probe = eth_hn_probe,
1086         .remove = eth_hn_remove,
1087 };
1088
1089 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
1090 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
1091
1092 RTE_INIT(hn_init_log)
1093 {
1094         hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
1095         if (hn_logtype_init >= 0)
1096                 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
1097         hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
1098         if (hn_logtype_driver >= 0)
1099                 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
1100 }