net/netvsc: fix crash in secondary process
[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         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
261                 return 0;
262
263         /* fills in rx and tx offload capability */
264         rc = hn_rndis_get_offload(hv, dev_info);
265         if (rc != 0)
266                 return rc;
267
268         /* merges the offload and queues of vf */
269         return hn_vf_info_get(hv, dev_info);
270 }
271
272 static int hn_rss_reta_update(struct rte_eth_dev *dev,
273                               struct rte_eth_rss_reta_entry64 *reta_conf,
274                               uint16_t reta_size)
275 {
276         struct hn_data *hv = dev->data->dev_private;
277         unsigned int i;
278         int err;
279
280         PMD_INIT_FUNC_TRACE();
281
282         if (reta_size != NDIS_HASH_INDCNT) {
283                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
284                 return -EINVAL;
285         }
286
287         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
288                 uint16_t idx = i / RTE_RETA_GROUP_SIZE;
289                 uint16_t shift = i % RTE_RETA_GROUP_SIZE;
290                 uint64_t mask = (uint64_t)1 << shift;
291
292                 if (reta_conf[idx].mask & mask)
293                         hv->rss_ind[i] = reta_conf[idx].reta[shift];
294         }
295
296         err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
297         if (err) {
298                 PMD_DRV_LOG(NOTICE,
299                         "rss disable failed");
300                 return err;
301         }
302
303         err = hn_rndis_conf_rss(hv, 0);
304         if (err) {
305                 PMD_DRV_LOG(NOTICE,
306                             "reta reconfig failed");
307                 return err;
308         }
309
310         return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
311 }
312
313 static int hn_rss_reta_query(struct rte_eth_dev *dev,
314                              struct rte_eth_rss_reta_entry64 *reta_conf,
315                              uint16_t reta_size)
316 {
317         struct hn_data *hv = dev->data->dev_private;
318         unsigned int i;
319
320         PMD_INIT_FUNC_TRACE();
321
322         if (reta_size != NDIS_HASH_INDCNT) {
323                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
324                 return -EINVAL;
325         }
326
327         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
328                 uint16_t idx = i / RTE_RETA_GROUP_SIZE;
329                 uint16_t shift = i % RTE_RETA_GROUP_SIZE;
330                 uint64_t mask = (uint64_t)1 << shift;
331
332                 if (reta_conf[idx].mask & mask)
333                         reta_conf[idx].reta[shift] = hv->rss_ind[i];
334         }
335         return 0;
336 }
337
338 static void hn_rss_hash_init(struct hn_data *hv,
339                              const struct rte_eth_rss_conf *rss_conf)
340 {
341         /* Convert from DPDK RSS hash flags to NDIS hash flags */
342         hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
343
344         if (rss_conf->rss_hf & ETH_RSS_IPV4)
345                 hv->rss_hash |= NDIS_HASH_IPV4;
346         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
347                 hv->rss_hash |= NDIS_HASH_TCP_IPV4;
348         if (rss_conf->rss_hf & ETH_RSS_IPV6)
349                 hv->rss_hash |=  NDIS_HASH_IPV6;
350         if (rss_conf->rss_hf & ETH_RSS_IPV6_EX)
351                 hv->rss_hash |=  NDIS_HASH_IPV6_EX;
352         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
353                 hv->rss_hash |= NDIS_HASH_TCP_IPV6;
354         if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX)
355                 hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
356
357         memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
358                NDIS_HASH_KEYSIZE_TOEPLITZ);
359 }
360
361 static int hn_rss_hash_update(struct rte_eth_dev *dev,
362                               struct rte_eth_rss_conf *rss_conf)
363 {
364         struct hn_data *hv = dev->data->dev_private;
365         int err;
366
367         PMD_INIT_FUNC_TRACE();
368
369         err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
370         if (err) {
371                 PMD_DRV_LOG(NOTICE,
372                             "rss disable failed");
373                 return err;
374         }
375
376         hn_rss_hash_init(hv, rss_conf);
377
378         err = hn_rndis_conf_rss(hv, 0);
379         if (err) {
380                 PMD_DRV_LOG(NOTICE,
381                             "rss reconfig failed (RSS disabled)");
382                 return err;
383         }
384
385
386         return hn_vf_rss_hash_update(dev, rss_conf);
387 }
388
389 static int hn_rss_hash_conf_get(struct rte_eth_dev *dev,
390                                 struct rte_eth_rss_conf *rss_conf)
391 {
392         struct hn_data *hv = dev->data->dev_private;
393
394         PMD_INIT_FUNC_TRACE();
395
396         if (hv->ndis_ver < NDIS_VERSION_6_20) {
397                 PMD_DRV_LOG(DEBUG, "RSS not supported on this host");
398                 return -EOPNOTSUPP;
399         }
400
401         rss_conf->rss_key_len = NDIS_HASH_KEYSIZE_TOEPLITZ;
402         if (rss_conf->rss_key)
403                 memcpy(rss_conf->rss_key, hv->rss_key,
404                        NDIS_HASH_KEYSIZE_TOEPLITZ);
405
406         rss_conf->rss_hf = 0;
407         if (hv->rss_hash & NDIS_HASH_IPV4)
408                 rss_conf->rss_hf |= ETH_RSS_IPV4;
409
410         if (hv->rss_hash & NDIS_HASH_TCP_IPV4)
411                 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
412
413         if (hv->rss_hash & NDIS_HASH_IPV6)
414                 rss_conf->rss_hf |= ETH_RSS_IPV6;
415
416         if (hv->rss_hash & NDIS_HASH_IPV6_EX)
417                 rss_conf->rss_hf |= ETH_RSS_IPV6_EX;
418
419         if (hv->rss_hash & NDIS_HASH_TCP_IPV6)
420                 rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
421
422         if (hv->rss_hash & NDIS_HASH_TCP_IPV6_EX)
423                 rss_conf->rss_hf |= ETH_RSS_IPV6_TCP_EX;
424
425         return 0;
426 }
427
428 static int
429 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
430 {
431         struct hn_data *hv = dev->data->dev_private;
432
433         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
434         return hn_vf_promiscuous_enable(dev);
435 }
436
437 static int
438 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
439 {
440         struct hn_data *hv = dev->data->dev_private;
441         uint32_t filter;
442
443         filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
444         if (dev->data->all_multicast)
445                 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
446         hn_rndis_set_rxfilter(hv, filter);
447         return hn_vf_promiscuous_disable(dev);
448 }
449
450 static int
451 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
452 {
453         struct hn_data *hv = dev->data->dev_private;
454
455         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
456                               NDIS_PACKET_TYPE_ALL_MULTICAST |
457                         NDIS_PACKET_TYPE_BROADCAST);
458         return hn_vf_allmulticast_enable(dev);
459 }
460
461 static int
462 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
463 {
464         struct hn_data *hv = dev->data->dev_private;
465
466         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
467                              NDIS_PACKET_TYPE_BROADCAST);
468         return hn_vf_allmulticast_disable(dev);
469 }
470
471 static int
472 hn_dev_mc_addr_list(struct rte_eth_dev *dev,
473                      struct rte_ether_addr *mc_addr_set,
474                      uint32_t nb_mc_addr)
475 {
476         /* No filtering on the synthetic path, but can do it on VF */
477         return hn_vf_mc_addr_list(dev, mc_addr_set, nb_mc_addr);
478 }
479
480 /* Setup shared rx/tx queue data */
481 static int hn_subchan_configure(struct hn_data *hv,
482                                 uint32_t subchan)
483 {
484         struct vmbus_channel *primary = hn_primary_chan(hv);
485         int err;
486         unsigned int retry = 0;
487
488         PMD_DRV_LOG(DEBUG,
489                     "open %u subchannels", subchan);
490
491         /* Send create sub channels command */
492         err = hn_nvs_alloc_subchans(hv, &subchan);
493         if (err)
494                 return  err;
495
496         while (subchan > 0) {
497                 struct vmbus_channel *new_sc;
498                 uint16_t chn_index;
499
500                 err = rte_vmbus_subchan_open(primary, &new_sc);
501                 if (err == -ENOENT && ++retry < 1000) {
502                         /* This can happen if not ready yet */
503                         rte_delay_ms(10);
504                         continue;
505                 }
506
507                 if (err) {
508                         PMD_DRV_LOG(ERR,
509                                     "open subchannel failed: %d", err);
510                         return err;
511                 }
512
513                 rte_vmbus_set_latency(hv->vmbus, new_sc, hv->latency);
514
515                 retry = 0;
516                 chn_index = rte_vmbus_sub_channel_index(new_sc);
517                 if (chn_index == 0 || chn_index > hv->max_queues) {
518                         PMD_DRV_LOG(ERR,
519                                     "Invalid subchannel offermsg channel %u",
520                                     chn_index);
521                         return -EIO;
522                 }
523
524                 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
525                 hv->channels[chn_index] = new_sc;
526                 --subchan;
527         }
528
529         return err;
530 }
531
532 static int hn_dev_configure(struct rte_eth_dev *dev)
533 {
534         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
535         struct rte_eth_rss_conf *rss_conf = &dev_conf->rx_adv_conf.rss_conf;
536         const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
537         const struct rte_eth_txmode *txmode = &dev_conf->txmode;
538         struct hn_data *hv = dev->data->dev_private;
539         uint64_t unsupported;
540         int i, err, subchan;
541
542         PMD_INIT_FUNC_TRACE();
543
544         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
545                 dev_conf->rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
546
547         unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
548         if (unsupported) {
549                 PMD_DRV_LOG(NOTICE,
550                             "unsupported TX offload: %#" PRIx64,
551                             unsupported);
552                 return -EINVAL;
553         }
554
555         unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
556         if (unsupported) {
557                 PMD_DRV_LOG(NOTICE,
558                             "unsupported RX offload: %#" PRIx64,
559                             rxmode->offloads);
560                 return -EINVAL;
561         }
562
563         hv->vlan_strip = !!(rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP);
564
565         err = hn_rndis_conf_offload(hv, txmode->offloads,
566                                     rxmode->offloads);
567         if (err) {
568                 PMD_DRV_LOG(NOTICE,
569                             "offload configure failed");
570                 return err;
571         }
572
573         hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
574                                  dev->data->nb_tx_queues);
575
576         for (i = 0; i < NDIS_HASH_INDCNT; i++)
577                 hv->rss_ind[i] = i % hv->num_queues;
578
579         hn_rss_hash_init(hv, rss_conf);
580
581         subchan = hv->num_queues - 1;
582         if (subchan > 0) {
583                 err = hn_subchan_configure(hv, subchan);
584                 if (err) {
585                         PMD_DRV_LOG(NOTICE,
586                                     "subchannel configuration failed");
587                         return err;
588                 }
589
590                 err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
591                 if (err) {
592                         PMD_DRV_LOG(NOTICE,
593                                 "rss disable failed");
594                         return err;
595                 }
596
597                 err = hn_rndis_conf_rss(hv, 0);
598                 if (err) {
599                         PMD_DRV_LOG(NOTICE,
600                                     "initial RSS config failed");
601                         return err;
602                 }
603         }
604
605         return hn_vf_configure(dev, dev_conf);
606 }
607
608 static int hn_dev_stats_get(struct rte_eth_dev *dev,
609                             struct rte_eth_stats *stats)
610 {
611         unsigned int i;
612
613         hn_vf_stats_get(dev, stats);
614
615         for (i = 0; i < dev->data->nb_tx_queues; i++) {
616                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
617
618                 if (!txq)
619                         continue;
620
621                 stats->opackets += txq->stats.packets;
622                 stats->obytes += txq->stats.bytes;
623                 stats->oerrors += txq->stats.errors;
624
625                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
626                         stats->q_opackets[i] = txq->stats.packets;
627                         stats->q_obytes[i] = txq->stats.bytes;
628                 }
629         }
630
631         for (i = 0; i < dev->data->nb_rx_queues; i++) {
632                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
633
634                 if (!rxq)
635                         continue;
636
637                 stats->ipackets += rxq->stats.packets;
638                 stats->ibytes += rxq->stats.bytes;
639                 stats->ierrors += rxq->stats.errors;
640                 stats->imissed += rxq->stats.ring_full;
641
642                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
643                         stats->q_ipackets[i] = rxq->stats.packets;
644                         stats->q_ibytes[i] = rxq->stats.bytes;
645                 }
646         }
647
648         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
649         return 0;
650 }
651
652 static int
653 hn_dev_stats_reset(struct rte_eth_dev *dev)
654 {
655         unsigned int i;
656
657         PMD_INIT_FUNC_TRACE();
658
659         for (i = 0; i < dev->data->nb_tx_queues; i++) {
660                 struct hn_tx_queue *txq = dev->data->tx_queues[i];
661
662                 if (!txq)
663                         continue;
664                 memset(&txq->stats, 0, sizeof(struct hn_stats));
665         }
666
667         for (i = 0; i < dev->data->nb_rx_queues; i++) {
668                 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
669
670                 if (!rxq)
671                         continue;
672
673                 memset(&rxq->stats, 0, sizeof(struct hn_stats));
674         }
675
676         return 0;
677 }
678
679 static int
680 hn_dev_xstats_reset(struct rte_eth_dev *dev)
681 {
682         int ret;
683
684         ret = hn_dev_stats_reset(dev);
685         if (ret != 0)
686                 return 0;
687
688         return hn_vf_xstats_reset(dev);
689 }
690
691 static int
692 hn_dev_xstats_count(struct rte_eth_dev *dev)
693 {
694         int ret, count;
695
696         count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
697         count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
698
699         ret = hn_vf_xstats_get_names(dev, NULL, 0);
700         if (ret < 0)
701                 return ret;
702
703         return count + ret;
704 }
705
706 static int
707 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
708                         struct rte_eth_xstat_name *xstats_names,
709                         unsigned int limit)
710 {
711         unsigned int i, t, count = 0;
712         int ret;
713
714         if (!xstats_names)
715                 return hn_dev_xstats_count(dev);
716
717         /* Note: limit checked in rte_eth_xstats_names() */
718         for (i = 0; i < dev->data->nb_tx_queues; i++) {
719                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
720
721                 if (!txq)
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                                  "tx_q%u_%s", i, hn_stat_strings[t].name);
731         }
732
733         for (i = 0; i < dev->data->nb_rx_queues; i++)  {
734                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
735
736                 if (!rxq)
737                         continue;
738
739                 if (count >= limit)
740                         break;
741
742                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
743                         snprintf(xstats_names[count++].name,
744                                  RTE_ETH_XSTATS_NAME_SIZE,
745                                  "rx_q%u_%s", i,
746                                  hn_stat_strings[t].name);
747         }
748
749         ret = hn_vf_xstats_get_names(dev, xstats_names + count,
750                                      limit - count);
751         if (ret < 0)
752                 return ret;
753
754         return count + ret;
755 }
756
757 static int
758 hn_dev_xstats_get(struct rte_eth_dev *dev,
759                   struct rte_eth_xstat *xstats,
760                   unsigned int n)
761 {
762         unsigned int i, t, count = 0;
763         const unsigned int nstats = hn_dev_xstats_count(dev);
764         const char *stats;
765         int ret;
766
767         PMD_INIT_FUNC_TRACE();
768
769         if (n < nstats)
770                 return nstats;
771
772         for (i = 0; i < dev->data->nb_tx_queues; i++) {
773                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
774
775                 if (!txq)
776                         continue;
777
778                 stats = (const char *)&txq->stats;
779                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
780                         xstats[count].id = count;
781                         xstats[count].value = *(const uint64_t *)
782                                 (stats + hn_stat_strings[t].offset);
783                 }
784         }
785
786         for (i = 0; i < dev->data->nb_rx_queues; i++) {
787                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
788
789                 if (!rxq)
790                         continue;
791
792                 stats = (const char *)&rxq->stats;
793                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
794                         xstats[count].id = count;
795                         xstats[count].value = *(const uint64_t *)
796                                 (stats + hn_stat_strings[t].offset);
797                 }
798         }
799
800         ret = hn_vf_xstats_get(dev, xstats, count, n);
801         if (ret < 0)
802                 return ret;
803
804         return count + ret;
805 }
806
807 static int
808 hn_dev_start(struct rte_eth_dev *dev)
809 {
810         struct hn_data *hv = dev->data->dev_private;
811         int error;
812
813         PMD_INIT_FUNC_TRACE();
814
815         error = hn_rndis_set_rxfilter(hv,
816                                       NDIS_PACKET_TYPE_BROADCAST |
817                                       NDIS_PACKET_TYPE_ALL_MULTICAST |
818                                       NDIS_PACKET_TYPE_DIRECTED);
819         if (error)
820                 return error;
821
822         error = hn_vf_start(dev);
823         if (error)
824                 hn_rndis_set_rxfilter(hv, 0);
825
826         return error;
827 }
828
829 static void
830 hn_dev_stop(struct rte_eth_dev *dev)
831 {
832         struct hn_data *hv = dev->data->dev_private;
833
834         PMD_INIT_FUNC_TRACE();
835
836         hn_rndis_set_rxfilter(hv, 0);
837         hn_vf_stop(dev);
838 }
839
840 static void
841 hn_dev_close(struct rte_eth_dev *dev)
842 {
843         PMD_INIT_FUNC_TRACE();
844
845         hn_vf_close(dev);
846         hn_dev_free_queues(dev);
847 }
848
849 static const struct eth_dev_ops hn_eth_dev_ops = {
850         .dev_configure          = hn_dev_configure,
851         .dev_start              = hn_dev_start,
852         .dev_stop               = hn_dev_stop,
853         .dev_close              = hn_dev_close,
854         .dev_infos_get          = hn_dev_info_get,
855         .dev_supported_ptypes_get = hn_vf_supported_ptypes,
856         .promiscuous_enable     = hn_dev_promiscuous_enable,
857         .promiscuous_disable    = hn_dev_promiscuous_disable,
858         .allmulticast_enable    = hn_dev_allmulticast_enable,
859         .allmulticast_disable   = hn_dev_allmulticast_disable,
860         .set_mc_addr_list       = hn_dev_mc_addr_list,
861         .reta_update            = hn_rss_reta_update,
862         .reta_query             = hn_rss_reta_query,
863         .rss_hash_update        = hn_rss_hash_update,
864         .rss_hash_conf_get      = hn_rss_hash_conf_get,
865         .tx_queue_setup         = hn_dev_tx_queue_setup,
866         .tx_queue_release       = hn_dev_tx_queue_release,
867         .tx_done_cleanup        = hn_dev_tx_done_cleanup,
868         .rx_queue_setup         = hn_dev_rx_queue_setup,
869         .rx_queue_release       = hn_dev_rx_queue_release,
870         .link_update            = hn_dev_link_update,
871         .stats_get              = hn_dev_stats_get,
872         .stats_reset            = hn_dev_stats_reset,
873         .xstats_get             = hn_dev_xstats_get,
874         .xstats_get_names       = hn_dev_xstats_get_names,
875         .xstats_reset           = hn_dev_xstats_reset,
876 };
877
878 /*
879  * Setup connection between PMD and kernel.
880  */
881 static int
882 hn_attach(struct hn_data *hv, unsigned int mtu)
883 {
884         int error;
885
886         /* Attach NVS */
887         error = hn_nvs_attach(hv, mtu);
888         if (error)
889                 goto failed_nvs;
890
891         /* Attach RNDIS */
892         error = hn_rndis_attach(hv);
893         if (error)
894                 goto failed_rndis;
895
896         /*
897          * NOTE:
898          * Under certain conditions on certain versions of Hyper-V,
899          * the RNDIS rxfilter is _not_ zero on the hypervisor side
900          * after the successful RNDIS initialization.
901          */
902         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
903         return 0;
904 failed_rndis:
905         hn_nvs_detach(hv);
906 failed_nvs:
907         return error;
908 }
909
910 static void
911 hn_detach(struct hn_data *hv)
912 {
913         hn_nvs_detach(hv);
914         hn_rndis_detach(hv);
915 }
916
917 static int
918 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
919 {
920         struct hn_data *hv = eth_dev->data->dev_private;
921         struct rte_device *device = eth_dev->device;
922         struct rte_vmbus_device *vmbus;
923         unsigned int rxr_cnt;
924         int err, max_chan;
925
926         PMD_INIT_FUNC_TRACE();
927
928         vmbus = container_of(device, struct rte_vmbus_device, device);
929         eth_dev->dev_ops = &hn_eth_dev_ops;
930         eth_dev->tx_pkt_burst = &hn_xmit_pkts;
931         eth_dev->rx_pkt_burst = &hn_recv_pkts;
932
933         /* Since Hyper-V only supports one MAC address, just use local data */
934         eth_dev->data->mac_addrs = &hv->mac_addr;
935
936         /*
937          * for secondary processes, we don't initialize any further as primary
938          * has already done this work.
939          */
940         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
941                 return 0;
942
943         hv->vmbus = vmbus;
944         hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
945         hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
946         hv->port_id = eth_dev->data->port_id;
947         hv->latency = HN_CHAN_LATENCY_NS;
948         hv->max_queues = 1;
949         rte_spinlock_init(&hv->vf_lock);
950         hv->vf_port = HN_INVALID_PORT;
951
952         err = hn_parse_args(eth_dev);
953         if (err)
954                 return err;
955
956         strlcpy(hv->owner.name, eth_dev->device->name,
957                 RTE_ETH_MAX_OWNER_NAME_LEN);
958         err = rte_eth_dev_owner_new(&hv->owner.id);
959         if (err) {
960                 PMD_INIT_LOG(ERR, "Can not get owner id");
961                 return err;
962         }
963
964         /* Initialize primary channel input for control operations */
965         err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
966         if (err)
967                 return err;
968
969         rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
970
971         hv->primary = hn_rx_queue_alloc(hv, 0,
972                                         eth_dev->device->numa_node);
973
974         if (!hv->primary)
975                 return -ENOMEM;
976
977         err = hn_attach(hv, RTE_ETHER_MTU);
978         if  (err)
979                 goto failed;
980
981         err = hn_tx_pool_init(eth_dev);
982         if (err)
983                 goto failed;
984
985         err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
986         if (err)
987                 goto failed;
988
989         /* Multi queue requires later versions of windows server */
990         if (hv->nvs_ver < NVS_VERSION_5)
991                 return 0;
992
993         max_chan = rte_vmbus_max_channels(vmbus);
994         PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
995         if (max_chan <= 0)
996                 goto failed;
997
998         if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
999                 rxr_cnt = 1;
1000
1001         hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
1002
1003         /* If VF was reported but not added, do it now */
1004         if (hv->vf_present && !hn_vf_attached(hv)) {
1005                 PMD_INIT_LOG(DEBUG, "Adding VF device");
1006
1007                 err = hn_vf_add(eth_dev, hv);
1008                 if (err)
1009                         hv->vf_present = 0;
1010         }
1011
1012         return 0;
1013
1014 failed:
1015         PMD_INIT_LOG(NOTICE, "device init failed");
1016
1017         hn_tx_pool_uninit(eth_dev);
1018         hn_detach(hv);
1019         return err;
1020 }
1021
1022 static int
1023 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1024 {
1025         struct hn_data *hv = eth_dev->data->dev_private;
1026         int ret;
1027
1028         PMD_INIT_FUNC_TRACE();
1029
1030         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1031                 return 0;
1032
1033         hn_dev_stop(eth_dev);
1034         hn_dev_close(eth_dev);
1035
1036         eth_dev->dev_ops = NULL;
1037         eth_dev->tx_pkt_burst = NULL;
1038         eth_dev->rx_pkt_burst = NULL;
1039
1040         hn_detach(hv);
1041         hn_tx_pool_uninit(eth_dev);
1042         rte_vmbus_chan_close(hv->primary->chan);
1043         rte_free(hv->primary);
1044         ret = rte_eth_dev_owner_delete(hv->owner.id);
1045         if (ret != 0)
1046                 return ret;
1047
1048         return 0;
1049 }
1050
1051 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
1052                         struct rte_vmbus_device *dev)
1053 {
1054         struct rte_eth_dev *eth_dev;
1055         int ret;
1056
1057         PMD_INIT_FUNC_TRACE();
1058
1059         eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
1060         if (!eth_dev)
1061                 return -ENOMEM;
1062
1063         ret = eth_hn_dev_init(eth_dev);
1064         if (ret)
1065                 eth_dev_vmbus_release(eth_dev);
1066         else
1067                 rte_eth_dev_probing_finish(eth_dev);
1068
1069         return ret;
1070 }
1071
1072 static int eth_hn_remove(struct rte_vmbus_device *dev)
1073 {
1074         struct rte_eth_dev *eth_dev;
1075         int ret;
1076
1077         PMD_INIT_FUNC_TRACE();
1078
1079         eth_dev = rte_eth_dev_allocated(dev->device.name);
1080         if (!eth_dev)
1081                 return -ENODEV;
1082
1083         ret = eth_hn_dev_uninit(eth_dev);
1084         if (ret)
1085                 return ret;
1086
1087         eth_dev_vmbus_release(eth_dev);
1088         return 0;
1089 }
1090
1091 /* Network device GUID */
1092 static const rte_uuid_t hn_net_ids[] = {
1093         /*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
1094         RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
1095         { 0 }
1096 };
1097
1098 static struct rte_vmbus_driver rte_netvsc_pmd = {
1099         .id_table = hn_net_ids,
1100         .probe = eth_hn_probe,
1101         .remove = eth_hn_remove,
1102 };
1103
1104 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
1105 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
1106
1107 RTE_INIT(hn_init_log)
1108 {
1109         hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
1110         if (hn_logtype_init >= 0)
1111                 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
1112         hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
1113         if (hn_logtype_driver >= 0)
1114                 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
1115 }