1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2018 Microsoft Corporation
3 * Copyright(c) 2013-2016 Brocade Communications Systems, Inc.
13 #include <rte_ethdev.h>
14 #include <rte_memcpy.h>
15 #include <rte_string_fns.h>
16 #include <rte_memzone.h>
17 #include <rte_malloc.h>
18 #include <rte_atomic.h>
19 #include <rte_branch_prediction.h>
20 #include <rte_ether.h>
21 #include <rte_ethdev_driver.h>
22 #include <rte_cycles.h>
23 #include <rte_errno.h>
24 #include <rte_memory.h>
27 #include <rte_bus_vmbus.h>
35 #define HN_TX_OFFLOAD_CAPS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
36 DEV_TX_OFFLOAD_TCP_CKSUM | \
37 DEV_TX_OFFLOAD_UDP_CKSUM | \
38 DEV_TX_OFFLOAD_TCP_TSO | \
39 DEV_TX_OFFLOAD_MULTI_SEGS | \
40 DEV_TX_OFFLOAD_VLAN_INSERT)
42 #define HN_RX_OFFLOAD_CAPS (DEV_RX_OFFLOAD_CHECKSUM | \
43 DEV_RX_OFFLOAD_VLAN_STRIP | \
44 DEV_RX_OFFLOAD_CRC_STRIP)
47 int hn_logtype_driver;
49 struct hn_xstats_name_off {
50 char name[RTE_ETH_XSTATS_NAME_SIZE];
54 static const struct hn_xstats_name_off hn_stat_strings[] = {
55 { "good_packets", offsetof(struct hn_stats, packets) },
56 { "good_bytes", offsetof(struct hn_stats, bytes) },
57 { "errors", offsetof(struct hn_stats, errors) },
58 { "allocation_failed", offsetof(struct hn_stats, nomemory) },
59 { "multicast_packets", offsetof(struct hn_stats, multicast) },
60 { "broadcast_packets", offsetof(struct hn_stats, broadcast) },
61 { "undersize_packets", offsetof(struct hn_stats, size_bins[0]) },
62 { "size_64_packets", offsetof(struct hn_stats, size_bins[1]) },
63 { "size_65_127_packets", offsetof(struct hn_stats, size_bins[2]) },
64 { "size_128_255_packets", offsetof(struct hn_stats, size_bins[3]) },
65 { "size_256_511_packets", offsetof(struct hn_stats, size_bins[4]) },
66 { "size_512_1023_packets", offsetof(struct hn_stats, size_bins[5]) },
67 { "size_1024_1518_packets", offsetof(struct hn_stats, size_bins[6]) },
68 { "size_1519_max_packets", offsetof(struct hn_stats, size_bins[7]) },
71 static struct rte_eth_dev *
72 eth_dev_vmbus_allocate(struct rte_vmbus_device *dev, size_t private_data_size)
74 struct rte_eth_dev *eth_dev;
80 name = dev->device.name;
82 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
83 eth_dev = rte_eth_dev_allocate(name);
85 PMD_DRV_LOG(NOTICE, "can not allocate rte ethdev");
89 if (private_data_size) {
90 eth_dev->data->dev_private =
91 rte_zmalloc_socket(name, private_data_size,
92 RTE_CACHE_LINE_SIZE, dev->device.numa_node);
93 if (!eth_dev->data->dev_private) {
94 PMD_DRV_LOG(NOTICE, "can not allocate driver data");
95 rte_eth_dev_release_port(eth_dev);
100 eth_dev = rte_eth_dev_attach_secondary(name);
102 PMD_DRV_LOG(NOTICE, "can not attach secondary");
107 eth_dev->device = &dev->device;
108 eth_dev->intr_handle = &dev->intr_handle;
114 eth_dev_vmbus_release(struct rte_eth_dev *eth_dev)
116 /* free ether device */
117 rte_eth_dev_release_port(eth_dev);
119 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
120 rte_free(eth_dev->data->dev_private);
122 eth_dev->data->dev_private = NULL;
125 * Secondary process will check the name to attach.
126 * Clear this field to avoid attaching a released ports.
128 eth_dev->data->name[0] = '\0';
130 eth_dev->device = NULL;
131 eth_dev->intr_handle = NULL;
134 /* Update link status.
135 * Note: the DPDK definition of "wait_to_complete"
136 * means block this call until link is up.
137 * which is not worth supporting.
140 hn_dev_link_update(struct rte_eth_dev *dev,
141 __rte_unused int wait_to_complete)
143 struct hn_data *hv = dev->data->dev_private;
144 struct rte_eth_link link, old;
147 old = dev->data->dev_link;
149 error = hn_rndis_get_linkstatus(hv);
153 hn_rndis_get_linkspeed(hv);
155 link = (struct rte_eth_link) {
156 .link_duplex = ETH_LINK_FULL_DUPLEX,
157 .link_autoneg = ETH_LINK_SPEED_FIXED,
158 .link_speed = hv->link_speed / 10000,
161 if (hv->link_status == NDIS_MEDIA_STATE_CONNECTED)
162 link.link_status = ETH_LINK_UP;
164 link.link_status = ETH_LINK_DOWN;
166 if (old.link_status == link.link_status)
169 PMD_INIT_LOG(DEBUG, "Port %d is %s", dev->data->port_id,
170 (link.link_status == ETH_LINK_UP) ? "up" : "down");
172 return rte_eth_linkstatus_set(dev, &link);
175 static void hn_dev_info_get(struct rte_eth_dev *dev,
176 struct rte_eth_dev_info *dev_info)
178 struct hn_data *hv = dev->data->dev_private;
180 dev_info->speed_capa = ETH_LINK_SPEED_10G;
181 dev_info->min_rx_bufsize = HN_MIN_RX_BUF_SIZE;
182 dev_info->max_rx_pktlen = HN_MAX_XFER_LEN;
183 dev_info->max_mac_addrs = 1;
185 dev_info->hash_key_size = NDIS_HASH_KEYSIZE_TOEPLITZ;
186 dev_info->flow_type_rss_offloads =
187 ETH_RSS_IPV4 | ETH_RSS_IPV6 | ETH_RSS_TCP | ETH_RSS_UDP;
189 dev_info->max_rx_queues = hv->max_queues;
190 dev_info->max_tx_queues = hv->max_queues;
192 hn_rndis_get_offload(hv, dev_info);
196 hn_dev_promiscuous_enable(struct rte_eth_dev *dev)
198 struct hn_data *hv = dev->data->dev_private;
200 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_PROMISCUOUS);
204 hn_dev_promiscuous_disable(struct rte_eth_dev *dev)
206 struct hn_data *hv = dev->data->dev_private;
209 filter = NDIS_PACKET_TYPE_DIRECTED | NDIS_PACKET_TYPE_BROADCAST;
210 if (dev->data->all_multicast)
211 filter |= NDIS_PACKET_TYPE_ALL_MULTICAST;
212 hn_rndis_set_rxfilter(hv, filter);
216 hn_dev_allmulticast_enable(struct rte_eth_dev *dev)
218 struct hn_data *hv = dev->data->dev_private;
220 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
221 NDIS_PACKET_TYPE_ALL_MULTICAST |
222 NDIS_PACKET_TYPE_BROADCAST);
226 hn_dev_allmulticast_disable(struct rte_eth_dev *dev)
228 struct hn_data *hv = dev->data->dev_private;
230 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_DIRECTED |
231 NDIS_PACKET_TYPE_BROADCAST);
234 /* Setup shared rx/tx queue data */
235 static int hn_subchan_configure(struct hn_data *hv,
238 struct vmbus_channel *primary = hn_primary_chan(hv);
240 unsigned int retry = 0;
243 "open %u subchannels", subchan);
245 /* Send create sub channels command */
246 err = hn_nvs_alloc_subchans(hv, &subchan);
250 while (subchan > 0) {
251 struct vmbus_channel *new_sc;
254 err = rte_vmbus_subchan_open(primary, &new_sc);
255 if (err == -ENOENT && ++retry < 1000) {
256 /* This can happen if not ready yet */
263 "open subchannel failed: %d", err);
267 rte_vmbus_set_latency(hv->vmbus, new_sc,
271 chn_index = rte_vmbus_sub_channel_index(new_sc);
272 if (chn_index == 0 || chn_index > hv->max_queues) {
274 "Invalid subchannel offermsg channel %u",
279 PMD_DRV_LOG(DEBUG, "new sub channel %u", chn_index);
280 hv->channels[chn_index] = new_sc;
287 static int hn_dev_configure(struct rte_eth_dev *dev)
289 const struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
290 const struct rte_eth_rxmode *rxmode = &dev_conf->rxmode;
291 const struct rte_eth_txmode *txmode = &dev_conf->txmode;
293 const struct rte_eth_rss_conf *rss_conf =
294 &dev_conf->rx_adv_conf.rss_conf;
295 struct hn_data *hv = dev->data->dev_private;
296 uint64_t unsupported;
299 PMD_INIT_FUNC_TRACE();
301 unsupported = txmode->offloads & ~HN_TX_OFFLOAD_CAPS;
304 "unsupported TX offload: %#" PRIx64,
309 unsupported = rxmode->offloads & ~HN_RX_OFFLOAD_CAPS;
312 "unsupported RX offload: %#" PRIx64,
317 err = hn_rndis_conf_offload(hv, txmode->offloads,
321 "offload configure failed");
325 hv->num_queues = RTE_MAX(dev->data->nb_rx_queues,
326 dev->data->nb_tx_queues);
327 subchan = hv->num_queues - 1;
329 err = hn_subchan_configure(hv, subchan);
332 "subchannel configuration failed");
336 err = hn_rndis_conf_rss(hv, rss_conf);
339 "rss configuration failed");
347 static int hn_dev_stats_get(struct rte_eth_dev *dev,
348 struct rte_eth_stats *stats)
352 for (i = 0; i < dev->data->nb_tx_queues; i++) {
353 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
358 stats->opackets += txq->stats.packets;
359 stats->obytes += txq->stats.bytes;
360 stats->oerrors += txq->stats.errors + txq->stats.nomemory;
362 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
363 stats->q_opackets[i] = txq->stats.packets;
364 stats->q_obytes[i] = txq->stats.bytes;
368 for (i = 0; i < dev->data->nb_rx_queues; i++) {
369 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
374 stats->ipackets += rxq->stats.packets;
375 stats->ibytes += rxq->stats.bytes;
376 stats->ierrors += rxq->stats.errors;
377 stats->imissed += rxq->ring_full;
379 if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) {
380 stats->q_ipackets[i] = rxq->stats.packets;
381 stats->q_ibytes[i] = rxq->stats.bytes;
385 stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed;
390 hn_dev_stats_reset(struct rte_eth_dev *dev)
394 PMD_INIT_FUNC_TRACE();
396 for (i = 0; i < dev->data->nb_tx_queues; i++) {
397 struct hn_tx_queue *txq = dev->data->tx_queues[i];
401 memset(&txq->stats, 0, sizeof(struct hn_stats));
404 for (i = 0; i < dev->data->nb_rx_queues; i++) {
405 struct hn_rx_queue *rxq = dev->data->rx_queues[i];
410 memset(&rxq->stats, 0, sizeof(struct hn_stats));
416 hn_dev_xstats_get_names(struct rte_eth_dev *dev,
417 struct rte_eth_xstat_name *xstats_names,
418 __rte_unused unsigned int limit)
420 unsigned int i, t, count = 0;
422 PMD_INIT_FUNC_TRACE();
425 return dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
426 + dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
428 /* Note: limit checked in rte_eth_xstats_names() */
429 for (i = 0; i < dev->data->nb_tx_queues; i++) {
430 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
435 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
436 snprintf(xstats_names[count++].name,
437 RTE_ETH_XSTATS_NAME_SIZE,
438 "tx_q%u_%s", i, hn_stat_strings[t].name);
441 for (i = 0; i < dev->data->nb_rx_queues; i++) {
442 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
447 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
448 snprintf(xstats_names[count++].name,
449 RTE_ETH_XSTATS_NAME_SIZE,
451 hn_stat_strings[t].name);
458 hn_dev_xstats_get(struct rte_eth_dev *dev,
459 struct rte_eth_xstat *xstats,
462 unsigned int i, t, count = 0;
464 const unsigned int nstats =
465 dev->data->nb_tx_queues * RTE_DIM(hn_stat_strings)
466 + dev->data->nb_rx_queues * RTE_DIM(hn_stat_strings);
469 PMD_INIT_FUNC_TRACE();
474 for (i = 0; i < dev->data->nb_tx_queues; i++) {
475 const struct hn_tx_queue *txq = dev->data->tx_queues[i];
480 stats = (const char *)&txq->stats;
481 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
482 xstats[count++].value = *(const uint64_t *)
483 (stats + hn_stat_strings[t].offset);
486 for (i = 0; i < dev->data->nb_rx_queues; i++) {
487 const struct hn_rx_queue *rxq = dev->data->rx_queues[i];
492 stats = (const char *)&rxq->stats;
493 for (t = 0; t < RTE_DIM(hn_stat_strings); t++)
494 xstats[count++].value = *(const uint64_t *)
495 (stats + hn_stat_strings[t].offset);
502 hn_dev_start(struct rte_eth_dev *dev)
504 struct hn_data *hv = dev->data->dev_private;
506 PMD_INIT_FUNC_TRACE();
508 /* check if lsc interrupt feature is enabled */
509 if (dev->data->dev_conf.intr_conf.lsc) {
510 PMD_DRV_LOG(ERR, "link status not supported yet");
514 return hn_rndis_set_rxfilter(hv,
515 NDIS_PACKET_TYPE_BROADCAST |
516 NDIS_PACKET_TYPE_ALL_MULTICAST |
517 NDIS_PACKET_TYPE_DIRECTED);
521 hn_dev_stop(struct rte_eth_dev *dev)
523 struct hn_data *hv = dev->data->dev_private;
525 PMD_INIT_FUNC_TRACE();
527 hn_rndis_set_rxfilter(hv, 0);
531 hn_dev_close(struct rte_eth_dev *dev __rte_unused)
533 PMD_INIT_LOG(DEBUG, "close");
536 static const struct eth_dev_ops hn_eth_dev_ops = {
537 .dev_configure = hn_dev_configure,
538 .dev_start = hn_dev_start,
539 .dev_stop = hn_dev_stop,
540 .dev_close = hn_dev_close,
541 .dev_infos_get = hn_dev_info_get,
542 .txq_info_get = hn_dev_tx_queue_info,
543 .rxq_info_get = hn_dev_rx_queue_info,
544 .promiscuous_enable = hn_dev_promiscuous_enable,
545 .promiscuous_disable = hn_dev_promiscuous_disable,
546 .allmulticast_enable = hn_dev_allmulticast_enable,
547 .allmulticast_disable = hn_dev_allmulticast_disable,
548 .tx_queue_setup = hn_dev_tx_queue_setup,
549 .tx_queue_release = hn_dev_tx_queue_release,
550 .tx_done_cleanup = hn_dev_tx_done_cleanup,
551 .rx_queue_setup = hn_dev_rx_queue_setup,
552 .rx_queue_release = hn_dev_rx_queue_release,
553 .link_update = hn_dev_link_update,
554 .stats_get = hn_dev_stats_get,
555 .xstats_get = hn_dev_xstats_get,
556 .xstats_get_names = hn_dev_xstats_get_names,
557 .stats_reset = hn_dev_stats_reset,
558 .xstats_reset = hn_dev_stats_reset,
562 * Setup connection between PMD and kernel.
565 hn_attach(struct hn_data *hv, unsigned int mtu)
570 error = hn_nvs_attach(hv, mtu);
575 error = hn_rndis_attach(hv);
581 * Under certain conditions on certain versions of Hyper-V,
582 * the RNDIS rxfilter is _not_ zero on the hypervisor side
583 * after the successful RNDIS initialization.
585 hn_rndis_set_rxfilter(hv, NDIS_PACKET_TYPE_NONE);
594 hn_detach(struct hn_data *hv)
601 eth_hn_dev_init(struct rte_eth_dev *eth_dev)
603 struct hn_data *hv = eth_dev->data->dev_private;
604 struct rte_device *device = eth_dev->device;
605 struct rte_vmbus_device *vmbus;
606 unsigned int rxr_cnt;
609 PMD_INIT_FUNC_TRACE();
611 vmbus = container_of(device, struct rte_vmbus_device, device);
612 eth_dev->dev_ops = &hn_eth_dev_ops;
613 eth_dev->tx_pkt_burst = &hn_xmit_pkts;
614 eth_dev->rx_pkt_burst = &hn_recv_pkts;
617 * for secondary processes, we don't initialize any further as primary
618 * has already done this work.
620 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
623 /* Since Hyper-V only supports one MAC address, just use local data */
624 eth_dev->data->mac_addrs = &hv->mac_addr;
627 hv->rxbuf_res = &vmbus->resource[HV_RECV_BUF_MAP];
628 hv->chim_res = &vmbus->resource[HV_SEND_BUF_MAP];
629 hv->port_id = eth_dev->data->port_id;
631 /* Initialize primary channel input for control operations */
632 err = rte_vmbus_chan_open(vmbus, &hv->channels[0]);
636 rte_vmbus_set_latency(hv->vmbus, hv->channels[0],
639 hv->primary = hn_rx_queue_alloc(hv, 0,
640 eth_dev->device->numa_node);
645 err = hn_attach(hv, ETHER_MTU);
649 err = hn_tx_pool_init(eth_dev);
653 err = hn_rndis_get_eaddr(hv, hv->mac_addr.addr_bytes);
657 max_chan = rte_vmbus_max_channels(vmbus);
658 PMD_INIT_LOG(DEBUG, "VMBus max channels %d", max_chan);
662 if (hn_rndis_query_rsscaps(hv, &rxr_cnt) != 0)
665 hv->max_queues = RTE_MIN(rxr_cnt, (unsigned int)max_chan);
670 PMD_INIT_LOG(NOTICE, "device init failed");
677 eth_hn_dev_uninit(struct rte_eth_dev *eth_dev)
679 struct hn_data *hv = eth_dev->data->dev_private;
681 PMD_INIT_FUNC_TRACE();
683 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
686 hn_dev_stop(eth_dev);
687 hn_dev_close(eth_dev);
689 eth_dev->dev_ops = NULL;
690 eth_dev->tx_pkt_burst = NULL;
691 eth_dev->rx_pkt_burst = NULL;
694 rte_vmbus_chan_close(hv->primary->chan);
695 rte_free(hv->primary);
697 eth_dev->data->mac_addrs = NULL;
702 static int eth_hn_probe(struct rte_vmbus_driver *drv __rte_unused,
703 struct rte_vmbus_device *dev)
705 struct rte_eth_dev *eth_dev;
708 PMD_INIT_FUNC_TRACE();
710 eth_dev = eth_dev_vmbus_allocate(dev, sizeof(struct hn_data));
714 ret = eth_hn_dev_init(eth_dev);
716 eth_dev_vmbus_release(eth_dev);
718 rte_eth_dev_probing_finish(eth_dev);
723 static int eth_hn_remove(struct rte_vmbus_device *dev)
725 struct rte_eth_dev *eth_dev;
728 PMD_INIT_FUNC_TRACE();
730 eth_dev = rte_eth_dev_allocated(dev->device.name);
734 ret = eth_hn_dev_uninit(eth_dev);
738 eth_dev_vmbus_release(eth_dev);
742 /* Network device GUID */
743 static const rte_uuid_t hn_net_ids[] = {
744 /* f8615163-df3e-46c5-913f-f2d2f965ed0e */
745 RTE_UUID_INIT(0xf8615163, 0xdf3e, 0x46c5, 0x913f, 0xf2d2f965ed0eULL),
749 static struct rte_vmbus_driver rte_netvsc_pmd = {
750 .id_table = hn_net_ids,
751 .probe = eth_hn_probe,
752 .remove = eth_hn_remove,
755 RTE_PMD_REGISTER_VMBUS(net_netvsc, rte_netvsc_pmd);
756 RTE_PMD_REGISTER_KMOD_DEP(net_netvsc, "* uio_hv_generic");
758 RTE_INIT(hn_init_log);
762 hn_logtype_init = rte_log_register("pmd.net.netvsc.init");
763 if (hn_logtype_init >= 0)
764 rte_log_set_level(hn_logtype_init, RTE_LOG_NOTICE);
765 hn_logtype_driver = rte_log_register("pmd.net.netvsc.driver");
766 if (hn_logtype_driver >= 0)
767 rte_log_set_level(hn_logtype_driver, RTE_LOG_NOTICE);