net/bnxt: add Truflow flush-timer to alloc table scope
[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         /* free ether device */
138         rte_eth_dev_release_port(eth_dev);
139
140         eth_dev->device = NULL;
141         eth_dev->intr_handle = NULL;
142 }
143
144 /* handle "latency=X" from devargs */
145 static int hn_set_latency(const char *key, const char *value, void *opaque)
146 {
147         struct hn_data *hv = opaque;
148         char *endp = NULL;
149         unsigned long lat;
150
151         errno = 0;
152         lat = strtoul(value, &endp, 0);
153
154         if (*value == '\0' || *endp != '\0') {
155                 PMD_DRV_LOG(ERR, "invalid parameter %s=%s", key, value);
156                 return -EINVAL;
157         }
158
159         PMD_DRV_LOG(DEBUG, "set latency %lu usec", lat);
160
161         hv->latency = lat * 1000;       /* usec to nsec */
162         return 0;
163 }
164
165 /* Parse device arguments */
166 static int hn_parse_args(const struct rte_eth_dev *dev)
167 {
168         struct hn_data *hv = dev->data->dev_private;
169         struct rte_devargs *devargs = dev->device->devargs;
170         static const char * const valid_keys[] = {
171                 "latency",
172                 NULL
173         };
174         struct rte_kvargs *kvlist;
175         int ret;
176
177         if (!devargs)
178                 return 0;
179
180         PMD_INIT_LOG(DEBUG, "device args %s %s",
181                      devargs->name, devargs->args);
182
183         kvlist = rte_kvargs_parse(devargs->args, valid_keys);
184         if (!kvlist) {
185                 PMD_DRV_LOG(NOTICE, "invalid parameters");
186                 return -EINVAL;
187         }
188
189         ret = rte_kvargs_process(kvlist, "latency", hn_set_latency, hv);
190         if (ret)
191                 PMD_DRV_LOG(ERR, "Unable to process latency arg\n");
192
193         rte_kvargs_free(kvlist);
194         return ret;
195 }
196
197 /* Update link status.
198  * Note: the DPDK definition of "wait_to_complete"
199  *   means block this call until link is up.
200  *   which is not worth supporting.
201  */
202 int
203 hn_dev_link_update(struct rte_eth_dev *dev,
204                    int wait_to_complete)
205 {
206         struct hn_data *hv = dev->data->dev_private;
207         struct rte_eth_link link, old;
208         int error;
209
210         old = dev->data->dev_link;
211
212         error = hn_rndis_get_linkstatus(hv);
213         if (error)
214                 return error;
215
216         hn_rndis_get_linkspeed(hv);
217
218         hn_vf_link_update(dev, wait_to_complete);
219
220         link = (struct rte_eth_link) {
221                 .link_duplex = ETH_LINK_FULL_DUPLEX,
222                 .link_autoneg = ETH_LINK_SPEED_FIXED,
223                 .link_speed = hv->link_speed / 10000,
224         };
225
226         if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
227                 link.link_status = ETH_LINK_UP;
228         else
229                 link.link_status = ETH_LINK_DOWN;
230
231         if (old.link_status == link.link_status)
232                 return 0;
233
234         PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
235                      (link.link_status == ETH_LINK_UP) ? "up" : "down");
236
237         return rte_eth_linkstatus_set(dev, &link);
238 }
239
240 static int hn_dev_info_get(struct rte_eth_dev *dev,
241                            struct rte_eth_dev_info *dev_info)
242 {
243         struct hn_data *hv = dev->data->dev_private;
244         int rc;
245
246         dev_info->speed_capa = ETH_LINK_SPEED_10G;
247         dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
248         dev_info->max_rx_pktlen  = HN_MAX_XFER_LEN;
249         dev_info->max_mac_addrs  = 1;
250
251         dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
252         dev_info->flow_type_rss_offloads = hv->rss_offloads;
253         dev_info->reta_size = ETH_RSS_RETA_SIZE_128;
254
255         dev_info->max_rx_queues = hv->max_queues;
256         dev_info->max_tx_queues = hv->max_queues;
257
258         dev_info->tx_desc_lim.nb_min = 1;
259         dev_info->tx_desc_lim.nb_max = 4096;
260
261         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
262                 return 0;
263
264         /* fills in rx and tx offload capability */
265         rc = hn_rndis_get_offload(hv, dev_info);
266         if (rc != 0)
267                 return rc;
268
269         /* merges the offload and queues of vf */
270         return hn_vf_info_get(hv, dev_info);
271 }
272
273 static int hn_rss_reta_update(struct rte_eth_dev *dev,
274                               struct rte_eth_rss_reta_entry64 *reta_conf,
275                               uint16_t reta_size)
276 {
277         struct hn_data *hv = dev->data->dev_private;
278         unsigned int i;
279         int err;
280
281         PMD_INIT_FUNC_TRACE();
282
283         if (reta_size != NDIS_HASH_INDCNT) {
284                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
285                 return -EINVAL;
286         }
287
288         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
289                 uint16_t idx = i / RTE_RETA_GROUP_SIZE;
290                 uint16_t shift = i % RTE_RETA_GROUP_SIZE;
291                 uint64_t mask = (uint64_t)1 << shift;
292
293                 if (reta_conf[idx].mask & mask)
294                         hv->rss_ind[i] = reta_conf[idx].reta[shift];
295         }
296
297         err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
298         if (err) {
299                 PMD_DRV_LOG(NOTICE,
300                         "rss disable failed");
301                 return err;
302         }
303
304         err = hn_rndis_conf_rss(hv, 0);
305         if (err) {
306                 PMD_DRV_LOG(NOTICE,
307                             "reta reconfig failed");
308                 return err;
309         }
310
311         return hn_vf_reta_hash_update(dev, reta_conf, reta_size);
312 }
313
314 static int hn_rss_reta_query(struct rte_eth_dev *dev,
315                              struct rte_eth_rss_reta_entry64 *reta_conf,
316                              uint16_t reta_size)
317 {
318         struct hn_data *hv = dev->data->dev_private;
319         unsigned int i;
320
321         PMD_INIT_FUNC_TRACE();
322
323         if (reta_size != NDIS_HASH_INDCNT) {
324                 PMD_DRV_LOG(ERR, "Hash lookup table size does not match NDIS");
325                 return -EINVAL;
326         }
327
328         for (i = 0; i < NDIS_HASH_INDCNT; i++) {
329                 uint16_t idx = i / RTE_RETA_GROUP_SIZE;
330                 uint16_t shift = i % RTE_RETA_GROUP_SIZE;
331                 uint64_t mask = (uint64_t)1 << shift;
332
333                 if (reta_conf[idx].mask & mask)
334                         reta_conf[idx].reta[shift] = hv->rss_ind[i];
335         }
336         return 0;
337 }
338
339 static void hn_rss_hash_init(struct hn_data *hv,
340                              const struct rte_eth_rss_conf *rss_conf)
341 {
342         /* Convert from DPDK RSS hash flags to NDIS hash flags */
343         hv->rss_hash = NDIS_HASH_FUNCTION_TOEPLITZ;
344
345         if (rss_conf->rss_hf & ETH_RSS_IPV4)
346                 hv->rss_hash |= NDIS_HASH_IPV4;
347         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
348                 hv->rss_hash |= NDIS_HASH_TCP_IPV4;
349         if (rss_conf->rss_hf & ETH_RSS_IPV6)
350                 hv->rss_hash |=  NDIS_HASH_IPV6;
351         if (rss_conf->rss_hf & ETH_RSS_IPV6_EX)
352                 hv->rss_hash |=  NDIS_HASH_IPV6_EX;
353         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
354                 hv->rss_hash |= NDIS_HASH_TCP_IPV6;
355         if (rss_conf->rss_hf & ETH_RSS_IPV6_TCP_EX)
356                 hv->rss_hash |= NDIS_HASH_TCP_IPV6_EX;
357
358         memcpy(hv->rss_key, rss_conf->rss_key ? : rss_default_key,
359                NDIS_HASH_KEYSIZE_TOEPLITZ);
360 }
361
362 static int hn_rss_hash_update(struct rte_eth_dev *dev,
363                               struct rte_eth_rss_conf *rss_conf)
364 {
365         struct hn_data *hv = dev->data->dev_private;
366         int err;
367
368         PMD_INIT_FUNC_TRACE();
369
370         err = hn_rndis_conf_rss(hv, NDIS_RSS_FLAG_DISABLE);
371         if (err) {
372                 PMD_DRV_LOG(NOTICE,
373                             "rss disable failed");
374                 return err;
375         }
376
377         hn_rss_hash_init(hv, rss_conf);
378
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 % hv->num_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                 err = hn_rndis_conf_rss(hv, 0);
599                 if (err) {
600                         PMD_DRV_LOG(NOTICE,
601                                     "initial RSS config failed");
602                         return err;
603                 }
604         }
605
606         return hn_vf_configure(dev, dev_conf);
607 }
608
609 static int hn_dev_stats_get(struct rte_eth_dev *dev,
610                             struct rte_eth_stats *stats)
611 {
612         unsigned int i;
613
614         hn_vf_stats_get(dev, stats);
615
616         for (i = 0; i < dev->data->nb_tx_queues; i++) {
617                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
618
619                 if (!txq)
620                         continue;
621
622                 stats->opackets += txq->stats.packets;
623                 stats->obytes += txq->stats.bytes;
624                 stats->oerrors += txq->stats.errors;
625
626                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
627                         stats->q_opackets[i] = txq->stats.packets;
628                         stats->q_obytes[i] = txq->stats.bytes;
629                 }
630         }
631
632         for (i = 0; i < dev->data->nb_rx_queues; i++) {
633                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
634
635                 if (!rxq)
636                         continue;
637
638                 stats->ipackets += rxq->stats.packets;
639                 stats->ibytes += rxq->stats.bytes;
640                 stats->ierrors += rxq->stats.errors;
641                 stats->imissed += rxq->stats.ring_full;
642
643                 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
644                         stats->q_ipackets[i] = rxq->stats.packets;
645                         stats->q_ibytes[i] = rxq->stats.bytes;
646                 }
647         }
648
649         stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
650         return 0;
651 }
652
653 static int
654 hn_dev_stats_reset(struct rte_eth_dev *dev)
655 {
656         unsigned int i;
657
658         PMD_INIT_FUNC_TRACE();
659
660         for (i = 0; i < dev->data->nb_tx_queues; i++) {
661                 struct hn_tx_queue *txq = dev->data->tx_queues[i];
662
663                 if (!txq)
664                         continue;
665                 memset(&txq->stats, 0, sizeof(struct hn_stats));
666         }
667
668         for (i = 0; i < dev->data->nb_rx_queues; i++) {
669                 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
670
671                 if (!rxq)
672                         continue;
673
674                 memset(&rxq->stats, 0, sizeof(struct hn_stats));
675         }
676
677         return 0;
678 }
679
680 static int
681 hn_dev_xstats_reset(struct rte_eth_dev *dev)
682 {
683         int ret;
684
685         ret = hn_dev_stats_reset(dev);
686         if (ret != 0)
687                 return 0;
688
689         return hn_vf_xstats_reset(dev);
690 }
691
692 static int
693 hn_dev_xstats_count(struct rte_eth_dev *dev)
694 {
695         int ret, count;
696
697         count = dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings);
698         count += dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
699
700         ret = hn_vf_xstats_get_names(dev, NULL, 0);
701         if (ret < 0)
702                 return ret;
703
704         return count + ret;
705 }
706
707 static int
708 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
709                         struct rte_eth_xstat_name *xstats_names,
710                         unsigned int limit)
711 {
712         unsigned int i, t, count = 0;
713         int ret;
714
715         if (!xstats_names)
716                 return hn_dev_xstats_count(dev);
717
718         /* Note: limit checked in rte_eth_xstats_names() */
719         for (i = 0; i < dev->data->nb_tx_queues; i++) {
720                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
721
722                 if (!txq)
723                         continue;
724
725                 if (count >= limit)
726                         break;
727
728                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
729                         snprintf(xstats_names[count++].name,
730                                  RTE_ETH_XSTATS_NAME_SIZE,
731                                  "tx_q%u_%s", i, hn_stat_strings[t].name);
732         }
733
734         for (i = 0; i < dev->data->nb_rx_queues; i++)  {
735                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
736
737                 if (!rxq)
738                         continue;
739
740                 if (count >= limit)
741                         break;
742
743                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
744                         snprintf(xstats_names[count++].name,
745                                  RTE_ETH_XSTATS_NAME_SIZE,
746                                  "rx_q%u_%s", i,
747                                  hn_stat_strings[t].name);
748         }
749
750         ret = hn_vf_xstats_get_names(dev, xstats_names + count,
751                                      limit - count);
752         if (ret < 0)
753                 return ret;
754
755         return count + ret;
756 }
757
758 static int
759 hn_dev_xstats_get(struct rte_eth_dev *dev,
760                   struct rte_eth_xstat *xstats,
761                   unsigned int n)
762 {
763         unsigned int i, t, count = 0;
764         const unsigned int nstats = hn_dev_xstats_count(dev);
765         const char *stats;
766         int ret;
767
768         PMD_INIT_FUNC_TRACE();
769
770         if (n < nstats)
771                 return nstats;
772
773         for (i = 0; i < dev->data->nb_tx_queues; i++) {
774                 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
775
776                 if (!txq)
777                         continue;
778
779                 stats = (const char *)&txq->stats;
780                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
781                         xstats[count].id = count;
782                         xstats[count].value = *(const uint64_t *)
783                                 (stats + hn_stat_strings[t].offset);
784                 }
785         }
786
787         for (i = 0; i < dev->data->nb_rx_queues; i++) {
788                 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
789
790                 if (!rxq)
791                         continue;
792
793                 stats = (const char *)&rxq->stats;
794                 for (t = 0; t < RTE_DIM(hn_stat_strings); t++, count++) {
795                         xstats[count].id = count;
796                         xstats[count].value = *(const uint64_t *)
797                                 (stats + hn_stat_strings[t].offset);
798                 }
799         }
800
801         ret = hn_vf_xstats_get(dev, xstats, count, n);
802         if (ret < 0)
803                 return ret;
804
805         return count + ret;
806 }
807
808 static int
809 hn_dev_start(struct rte_eth_dev *dev)
810 {
811         struct hn_data *hv = dev->data->dev_private;
812         int error;
813
814         PMD_INIT_FUNC_TRACE();
815
816         error = hn_rndis_set_rxfilter(hv,
817                                       NDIS_PACKET_TYPE_BROADCAST |
818                                       NDIS_PACKET_TYPE_ALL_MULTICAST |
819                                       NDIS_PACKET_TYPE_DIRECTED);
820         if (error)
821                 return error;
822
823         error = hn_vf_start(dev);
824         if (error)
825                 hn_rndis_set_rxfilter(hv, 0);
826
827         /* Initialize Link state */
828         if (error == 0)
829                 hn_dev_link_update(dev, 0);
830
831         return error;
832 }
833
834 static void
835 hn_dev_stop(struct rte_eth_dev *dev)
836 {
837         struct hn_data *hv = dev->data->dev_private;
838
839         PMD_INIT_FUNC_TRACE();
840
841         hn_rndis_set_rxfilter(hv, 0);
842         hn_vf_stop(dev);
843 }
844
845 static void
846 hn_dev_close(struct rte_eth_dev *dev)
847 {
848         PMD_INIT_FUNC_TRACE();
849
850         hn_vf_close(dev);
851         hn_dev_free_queues(dev);
852 }
853
854 static const struct eth_dev_ops hn_eth_dev_ops = {
855         .dev_configure          = hn_dev_configure,
856         .dev_start              = hn_dev_start,
857         .dev_stop               = hn_dev_stop,
858         .dev_close              = hn_dev_close,
859         .dev_infos_get          = hn_dev_info_get,
860         .dev_supported_ptypes_get = hn_vf_supported_ptypes,
861         .promiscuous_enable     = hn_dev_promiscuous_enable,
862         .promiscuous_disable    = hn_dev_promiscuous_disable,
863         .allmulticast_enable    = hn_dev_allmulticast_enable,
864         .allmulticast_disable   = hn_dev_allmulticast_disable,
865         .set_mc_addr_list       = hn_dev_mc_addr_list,
866         .reta_update            = hn_rss_reta_update,
867         .reta_query             = hn_rss_reta_query,
868         .rss_hash_update        = hn_rss_hash_update,
869         .rss_hash_conf_get      = hn_rss_hash_conf_get,
870         .tx_queue_setup         = hn_dev_tx_queue_setup,
871         .tx_queue_release       = hn_dev_tx_queue_release,
872         .tx_done_cleanup        = hn_dev_tx_done_cleanup,
873         .rx_queue_setup         = hn_dev_rx_queue_setup,
874         .rx_queue_release       = hn_dev_rx_queue_release,
875         .link_update            = hn_dev_link_update,
876         .stats_get              = hn_dev_stats_get,
877         .stats_reset            = hn_dev_stats_reset,
878         .xstats_get             = hn_dev_xstats_get,
879         .xstats_get_names       = hn_dev_xstats_get_names,
880         .xstats_reset           = hn_dev_xstats_reset,
881 };
882
883 /*
884  * Setup connection between PMD and kernel.
885  */
886 static int
887 hn_attach(struct hn_data *hv, unsigned int mtu)
888 {
889         int error;
890
891         /* Attach NVS */
892         error = hn_nvs_attach(hv, mtu);
893         if (error)
894                 goto failed_nvs;
895
896         /* Attach RNDIS */
897         error = hn_rndis_attach(hv);
898         if (error)
899                 goto failed_rndis;
900
901         /*
902          * NOTE:
903          * Under certain conditions on certain versions of Hyper-V,
904          * the RNDIS rxfilter is _not_ zero on the hypervisor side
905          * after the successful RNDIS initialization.
906          */
907         hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
908         return 0;
909 failed_rndis:
910         hn_nvs_detach(hv);
911 failed_nvs:
912         return error;
913 }
914
915 static void
916 hn_detach(struct hn_data *hv)
917 {
918         hn_nvs_detach(hv);
919         hn_rndis_detach(hv);
920 }
921
922 static int
923 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
924 {
925         struct hn_data *hv = eth_dev->data->dev_private;
926         struct rte_device *device = eth_dev->device;
927         struct rte_vmbus_device *vmbus;
928         unsigned int rxr_cnt;
929         int err, max_chan;
930
931         PMD_INIT_FUNC_TRACE();
932
933         vmbus = container_of(device, struct rte_vmbus_device, device);
934         eth_dev->dev_ops = &hn_eth_dev_ops;
935         eth_dev->tx_pkt_burst = &hn_xmit_pkts;
936         eth_dev->rx_pkt_burst = &hn_recv_pkts;
937
938         /*
939          * for secondary processes, we don't initialize any further as primary
940          * has already done this work.
941          */
942         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
943                 return 0;
944
945         /* Since Hyper-V only supports one MAC address */
946         eth_dev->data->mac_addrs = rte_calloc("hv_mac", HN_MAX_MAC_ADDRS,
947                                               sizeof(struct rte_ether_addr), 0);
948         if (eth_dev->data->mac_addrs == NULL) {
949                 PMD_INIT_LOG(ERR,
950                              "Failed to allocate memory store MAC addresses");
951                 return -ENOMEM;
952         }
953
954         hv->vmbus = vmbus;
955         hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
956         hv->chim_res  = &vmbus->resource[HV_SEND_BUF_MAP];
957         hv->port_id = eth_dev->data->port_id;
958         hv->latency = HN_CHAN_LATENCY_NS;
959         hv->max_queues = 1;
960         rte_spinlock_init(&hv->vf_lock);
961         hv->vf_port = HN_INVALID_PORT;
962
963         err = hn_parse_args(eth_dev);
964         if (err)
965                 return err;
966
967         strlcpy(hv->owner.name, eth_dev->device->name,
968                 RTE_ETH_MAX_OWNER_NAME_LEN);
969         err = rte_eth_dev_owner_new(&hv->owner.id);
970         if (err) {
971                 PMD_INIT_LOG(ERR, "Can not get owner id");
972                 return err;
973         }
974
975         /* Initialize primary channel input for control operations */
976         err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
977         if (err)
978                 return err;
979
980         rte_vmbus_set_latency(hv->vmbus, hv->channels[0], hv->latency);
981
982         hv->primary = hn_rx_queue_alloc(hv, 0,
983                                         eth_dev->device->numa_node);
984
985         if (!hv->primary)
986                 return -ENOMEM;
987
988         err = hn_attach(hv, RTE_ETHER_MTU);
989         if  (err)
990                 goto failed;
991
992         err = hn_chim_init(eth_dev);
993         if (err)
994                 goto failed;
995
996         err = hn_rndis_get_eaddr(hv, eth_dev->data->mac_addrs->addr_bytes);
997         if (err)
998                 goto failed;
999
1000         /* Multi queue requires later versions of windows server */
1001         if (hv->nvs_ver < NVS_VERSION_5)
1002                 return 0;
1003
1004         max_chan = rte_vmbus_max_channels(vmbus);
1005         PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
1006         if (max_chan <= 0)
1007                 goto failed;
1008
1009         if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
1010                 rxr_cnt = 1;
1011
1012         hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
1013
1014         /* If VF was reported but not added, do it now */
1015         if (hv->vf_present && !hn_vf_attached(hv)) {
1016                 PMD_INIT_LOG(DEBUG, "Adding VF device");
1017
1018                 err = hn_vf_add(eth_dev, hv);
1019                 if (err)
1020                         hv->vf_present = 0;
1021         }
1022
1023         return 0;
1024
1025 failed:
1026         PMD_INIT_LOG(NOTICE, "device init failed");
1027
1028         hn_chim_uninit(eth_dev);
1029         hn_detach(hv);
1030         return err;
1031 }
1032
1033 static int
1034 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
1035 {
1036         struct hn_data *hv = eth_dev->data->dev_private;
1037         int ret;
1038
1039         PMD_INIT_FUNC_TRACE();
1040
1041         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1042                 return 0;
1043
1044         hn_dev_stop(eth_dev);
1045         hn_dev_close(eth_dev);
1046
1047         eth_dev->dev_ops = NULL;
1048         eth_dev->tx_pkt_burst = NULL;
1049         eth_dev->rx_pkt_burst = NULL;
1050
1051         hn_detach(hv);
1052         hn_chim_uninit(eth_dev);
1053         rte_vmbus_chan_close(hv->primary->chan);
1054         rte_free(hv->primary);
1055         ret = rte_eth_dev_owner_delete(hv->owner.id);
1056         if (ret != 0)
1057                 return ret;
1058
1059         return 0;
1060 }
1061
1062 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
1063                         struct rte_vmbus_device *dev)
1064 {
1065         struct rte_eth_dev *eth_dev;
1066         int ret;
1067
1068         PMD_INIT_FUNC_TRACE();
1069
1070         eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
1071         if (!eth_dev)
1072                 return -ENOMEM;
1073
1074         ret = eth_hn_dev_init(eth_dev);
1075         if (ret)
1076                 eth_dev_vmbus_release(eth_dev);
1077         else
1078                 rte_eth_dev_probing_finish(eth_dev);
1079
1080         return ret;
1081 }
1082
1083 static int eth_hn_remove(struct rte_vmbus_device *dev)
1084 {
1085         struct rte_eth_dev *eth_dev;
1086         int ret;
1087
1088         PMD_INIT_FUNC_TRACE();
1089
1090         eth_dev = rte_eth_dev_allocated(dev->device.name);
1091         if (!eth_dev)
1092                 return -ENODEV;
1093
1094         ret = eth_hn_dev_uninit(eth_dev);
1095         if (ret)
1096                 return ret;
1097
1098         eth_dev_vmbus_release(eth_dev);
1099         return 0;
1100 }
1101
1102 /* Network device GUID */
1103 static const rte_uuid_t hn_net_ids[] = {
1104         /*  f8615163-df3e-46c5-913f-f2d2f965ed0e */
1105         RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
1106         { 0 }
1107 };
1108
1109 static struct rte_vmbus_driver rte_netvsc_pmd = {
1110         .id_table = hn_net_ids,
1111         .probe = eth_hn_probe,
1112         .remove = eth_hn_remove,
1113 };
1114
1115 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
1116 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
1117
1118 RTE_INIT(hn_init_log)
1119 {
1120         hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
1121         if (hn_logtype_init >= 0)
1122                 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
1123         hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
1124         if (hn_logtype_driver >= 0)
1125                 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);
1126 }