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