16c3169a3SBruce Richardson /*- 26c3169a3SBruce Richardson * BSD LICENSE 36c3169a3SBruce Richardson * 4baf91c39SRemy Horton * Copyright(c) 2010-2016 Intel Corporation. All rights reserved. 56c3169a3SBruce Richardson * All rights reserved. 66c3169a3SBruce Richardson * 76c3169a3SBruce Richardson * Redistribution and use in source and binary forms, with or without 86c3169a3SBruce Richardson * modification, are permitted provided that the following conditions 96c3169a3SBruce Richardson * are met: 106c3169a3SBruce Richardson * 116c3169a3SBruce Richardson * * Redistributions of source code must retain the above copyright 126c3169a3SBruce Richardson * notice, this list of conditions and the following disclaimer. 136c3169a3SBruce Richardson * * Redistributions in binary form must reproduce the above copyright 146c3169a3SBruce Richardson * notice, this list of conditions and the following disclaimer in 156c3169a3SBruce Richardson * the documentation and/or other materials provided with the 166c3169a3SBruce Richardson * distribution. 176c3169a3SBruce Richardson * * Neither the name of Intel Corporation nor the names of its 186c3169a3SBruce Richardson * contributors may be used to endorse or promote products derived 196c3169a3SBruce Richardson * from this software without specific prior written permission. 206c3169a3SBruce Richardson * 216c3169a3SBruce Richardson * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 226c3169a3SBruce Richardson * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 236c3169a3SBruce Richardson * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 246c3169a3SBruce Richardson * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 256c3169a3SBruce Richardson * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 266c3169a3SBruce Richardson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 276c3169a3SBruce Richardson * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 286c3169a3SBruce Richardson * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 296c3169a3SBruce Richardson * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 306c3169a3SBruce Richardson * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 316c3169a3SBruce Richardson * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 326c3169a3SBruce Richardson */ 336c3169a3SBruce Richardson 346c3169a3SBruce Richardson #include <stdint.h> 356c3169a3SBruce Richardson #include <string.h> 366c3169a3SBruce Richardson #include <stdio.h> 376c3169a3SBruce Richardson #include <errno.h> 386c3169a3SBruce Richardson #include <unistd.h> 396c3169a3SBruce Richardson 406c3169a3SBruce Richardson #include <rte_ethdev.h> 41fdf91e0fSJan Blunck #include <rte_ethdev_pci.h> 426c3169a3SBruce Richardson #include <rte_memcpy.h> 436c3169a3SBruce Richardson #include <rte_string_fns.h> 446c3169a3SBruce Richardson #include <rte_memzone.h> 456c3169a3SBruce Richardson #include <rte_malloc.h> 466c3169a3SBruce Richardson #include <rte_atomic.h> 476c3169a3SBruce Richardson #include <rte_branch_prediction.h> 486c3169a3SBruce Richardson #include <rte_pci.h> 496c3169a3SBruce Richardson #include <rte_ether.h> 506c3169a3SBruce Richardson #include <rte_common.h> 51abf4c84bSBernard Iremonger #include <rte_errno.h> 524819eae8SOlivier Matz #include <rte_cpuflags.h> 536c3169a3SBruce Richardson 546c3169a3SBruce Richardson #include <rte_memory.h> 556c3169a3SBruce Richardson #include <rte_eal.h> 566c3169a3SBruce Richardson #include <rte_dev.h> 576c3169a3SBruce Richardson 586c3169a3SBruce Richardson #include "virtio_ethdev.h" 596c3169a3SBruce Richardson #include "virtio_pci.h" 606c3169a3SBruce Richardson #include "virtio_logs.h" 616c3169a3SBruce Richardson #include "virtqueue.h" 62cab04612SHuawei Xie #include "virtio_rxtx.h" 636c3169a3SBruce Richardson 64abf4c84bSBernard Iremonger static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev); 656c3169a3SBruce Richardson static int virtio_dev_configure(struct rte_eth_dev *dev); 666c3169a3SBruce Richardson static int virtio_dev_start(struct rte_eth_dev *dev); 676c3169a3SBruce Richardson static void virtio_dev_stop(struct rte_eth_dev *dev); 686c3169a3SBruce Richardson static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev); 696c3169a3SBruce Richardson static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev); 706c3169a3SBruce Richardson static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev); 716c3169a3SBruce Richardson static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev); 726c3169a3SBruce Richardson static void virtio_dev_info_get(struct rte_eth_dev *dev, 736c3169a3SBruce Richardson struct rte_eth_dev_info *dev_info); 746c3169a3SBruce Richardson static int virtio_dev_link_update(struct rte_eth_dev *dev, 75dd2c630aSFerruh Yigit int wait_to_complete); 766c3169a3SBruce Richardson 776c3169a3SBruce Richardson static void virtio_set_hwaddr(struct virtio_hw *hw); 786c3169a3SBruce Richardson static void virtio_get_hwaddr(struct virtio_hw *hw); 796c3169a3SBruce Richardson 8076d4c652SHarry van Haaren static void virtio_dev_stats_get(struct rte_eth_dev *dev, 8176d4c652SHarry van Haaren struct rte_eth_stats *stats); 8276d4c652SHarry van Haaren static int virtio_dev_xstats_get(struct rte_eth_dev *dev, 83e2aae1c1SRemy Horton struct rte_eth_xstat *xstats, unsigned n); 84baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev, 85baf91c39SRemy Horton struct rte_eth_xstat_name *xstats_names, 86baf91c39SRemy Horton unsigned limit); 876c3169a3SBruce Richardson static void virtio_dev_stats_reset(struct rte_eth_dev *dev); 886c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev); 896c3169a3SBruce Richardson static int virtio_vlan_filter_set(struct rte_eth_dev *dev, 906c3169a3SBruce Richardson uint16_t vlan_id, int on); 916d01e580SWei Dai static int virtio_mac_addr_add(struct rte_eth_dev *dev, 926c3169a3SBruce Richardson struct ether_addr *mac_addr, 93dd2c630aSFerruh Yigit uint32_t index, uint32_t vmdq); 946c3169a3SBruce Richardson static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index); 956c3169a3SBruce Richardson static void virtio_mac_addr_set(struct rte_eth_dev *dev, 966c3169a3SBruce Richardson struct ether_addr *mac_addr); 976c3169a3SBruce Richardson 986c3169a3SBruce Richardson static int virtio_dev_queue_stats_mapping_set( 99dd2c630aSFerruh Yigit struct rte_eth_dev *eth_dev, 100dd2c630aSFerruh Yigit uint16_t queue_id, 101dd2c630aSFerruh Yigit uint8_t stat_idx, 102dd2c630aSFerruh Yigit uint8_t is_rx); 1036c3169a3SBruce Richardson 1046c3169a3SBruce Richardson /* 1056c3169a3SBruce Richardson * The set of PCI devices this driver supports 1066c3169a3SBruce Richardson */ 1076c3169a3SBruce Richardson static const struct rte_pci_id pci_id_virtio_map[] = { 1084c790365SJason Wang { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) }, 1094c790365SJason Wang { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) }, 1106c3169a3SBruce Richardson { .vendor_id = 0, /* sentinel */ }, 1116c3169a3SBruce Richardson }; 1126c3169a3SBruce Richardson 11376d4c652SHarry van Haaren struct rte_virtio_xstats_name_off { 11476d4c652SHarry van Haaren char name[RTE_ETH_XSTATS_NAME_SIZE]; 11576d4c652SHarry van Haaren unsigned offset; 11676d4c652SHarry van Haaren }; 11776d4c652SHarry van Haaren 11876d4c652SHarry van Haaren /* [rt]x_qX_ is prepended to the name string here */ 11901ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = { 12001ad44fdSHuawei Xie {"good_packets", offsetof(struct virtnet_rx, stats.packets)}, 12101ad44fdSHuawei Xie {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)}, 12201ad44fdSHuawei Xie {"errors", offsetof(struct virtnet_rx, stats.errors)}, 12301ad44fdSHuawei Xie {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)}, 12401ad44fdSHuawei Xie {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)}, 12501ad44fdSHuawei Xie {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])}, 12601ad44fdSHuawei Xie {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])}, 12701ad44fdSHuawei Xie {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])}, 12801ad44fdSHuawei Xie {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])}, 12901ad44fdSHuawei Xie {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])}, 13001ad44fdSHuawei Xie {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])}, 13181f7234bSZhiyong Yang {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])}, 13281f7234bSZhiyong Yang {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])}, 13376d4c652SHarry van Haaren }; 13476d4c652SHarry van Haaren 13501ad44fdSHuawei Xie /* [rt]x_qX_ is prepended to the name string here */ 13601ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = { 13701ad44fdSHuawei Xie {"good_packets", offsetof(struct virtnet_tx, stats.packets)}, 13801ad44fdSHuawei Xie {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)}, 13901ad44fdSHuawei Xie {"errors", offsetof(struct virtnet_tx, stats.errors)}, 14001ad44fdSHuawei Xie {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)}, 14101ad44fdSHuawei Xie {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)}, 14201ad44fdSHuawei Xie {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])}, 14301ad44fdSHuawei Xie {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])}, 14401ad44fdSHuawei Xie {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])}, 14501ad44fdSHuawei Xie {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])}, 14601ad44fdSHuawei Xie {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])}, 14701ad44fdSHuawei Xie {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])}, 14881f7234bSZhiyong Yang {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])}, 14981f7234bSZhiyong Yang {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])}, 15001ad44fdSHuawei Xie }; 15101ad44fdSHuawei Xie 15201ad44fdSHuawei Xie #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \ 15301ad44fdSHuawei Xie sizeof(rte_virtio_rxq_stat_strings[0])) 15401ad44fdSHuawei Xie #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \ 15501ad44fdSHuawei Xie sizeof(rte_virtio_txq_stat_strings[0])) 15676d4c652SHarry van Haaren 157553f4593SYuanhan Liu struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS]; 158553f4593SYuanhan Liu 1596c3169a3SBruce Richardson static int 16001ad44fdSHuawei Xie virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl, 1616c3169a3SBruce Richardson int *dlen, int pkt_num) 1626c3169a3SBruce Richardson { 1639e71668bSDamjan Marion uint32_t head, i; 1646c3169a3SBruce Richardson int k, sum = 0; 1656c3169a3SBruce Richardson virtio_net_ctrl_ack status = ~0; 1666c3169a3SBruce Richardson struct virtio_pmd_ctrl result; 16701ad44fdSHuawei Xie struct virtqueue *vq; 1686c3169a3SBruce Richardson 1696c3169a3SBruce Richardson ctrl->status = status; 1706c3169a3SBruce Richardson 1713bd60a27SJianfeng Tan if (!cvq || !cvq->vq) { 1720bb159adSHuawei Xie PMD_INIT_LOG(ERR, "Control queue is not supported."); 1736c3169a3SBruce Richardson return -1; 1746c3169a3SBruce Richardson } 17501ad44fdSHuawei Xie vq = cvq->vq; 1769e71668bSDamjan Marion head = vq->vq_desc_head_idx; 1776c3169a3SBruce Richardson 1786c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, " 1796c3169a3SBruce Richardson "vq->hw->cvq = %p vq = %p", 1806c3169a3SBruce Richardson vq->vq_desc_head_idx, status, vq->hw->cvq, vq); 1816c3169a3SBruce Richardson 1826c3169a3SBruce Richardson if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1)) 1836c3169a3SBruce Richardson return -1; 1846c3169a3SBruce Richardson 18501ad44fdSHuawei Xie memcpy(cvq->virtio_net_hdr_mz->addr, ctrl, 1866c3169a3SBruce Richardson sizeof(struct virtio_pmd_ctrl)); 1876c3169a3SBruce Richardson 1886c3169a3SBruce Richardson /* 1896c3169a3SBruce Richardson * Format is enforced in qemu code: 1906c3169a3SBruce Richardson * One TX packet for header; 1916c3169a3SBruce Richardson * At least one TX packet per argument; 1926c3169a3SBruce Richardson * One RX packet for ACK. 1936c3169a3SBruce Richardson */ 1946c3169a3SBruce Richardson vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT; 195f24f8f9fSJianfeng Tan vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem; 1966c3169a3SBruce Richardson vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr); 1976c3169a3SBruce Richardson vq->vq_free_cnt--; 1986c3169a3SBruce Richardson i = vq->vq_ring.desc[head].next; 1996c3169a3SBruce Richardson 2006c3169a3SBruce Richardson for (k = 0; k < pkt_num; k++) { 2016c3169a3SBruce Richardson vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT; 202f24f8f9fSJianfeng Tan vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem 2036c3169a3SBruce Richardson + sizeof(struct virtio_net_ctrl_hdr) 2046c3169a3SBruce Richardson + sizeof(ctrl->status) + sizeof(uint8_t)*sum; 2056c3169a3SBruce Richardson vq->vq_ring.desc[i].len = dlen[k]; 2066c3169a3SBruce Richardson sum += dlen[k]; 2076c3169a3SBruce Richardson vq->vq_free_cnt--; 2086c3169a3SBruce Richardson i = vq->vq_ring.desc[i].next; 2096c3169a3SBruce Richardson } 2106c3169a3SBruce Richardson 2116c3169a3SBruce Richardson vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE; 212f24f8f9fSJianfeng Tan vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem 2136c3169a3SBruce Richardson + sizeof(struct virtio_net_ctrl_hdr); 2146c3169a3SBruce Richardson vq->vq_ring.desc[i].len = sizeof(ctrl->status); 2156c3169a3SBruce Richardson vq->vq_free_cnt--; 2166c3169a3SBruce Richardson 2176c3169a3SBruce Richardson vq->vq_desc_head_idx = vq->vq_ring.desc[i].next; 2186c3169a3SBruce Richardson 2196c3169a3SBruce Richardson vq_update_avail_ring(vq, head); 2206c3169a3SBruce Richardson vq_update_avail_idx(vq); 2216c3169a3SBruce Richardson 2226c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index); 2236c3169a3SBruce Richardson 2246c3169a3SBruce Richardson virtqueue_notify(vq); 2256c3169a3SBruce Richardson 2266c3169a3SBruce Richardson rte_rmb(); 227b81026f1SHuawei Xie while (VIRTQUEUE_NUSED(vq) == 0) { 2286c3169a3SBruce Richardson rte_rmb(); 2296c3169a3SBruce Richardson usleep(100); 2306c3169a3SBruce Richardson } 2316c3169a3SBruce Richardson 232b81026f1SHuawei Xie while (VIRTQUEUE_NUSED(vq)) { 2336c3169a3SBruce Richardson uint32_t idx, desc_idx, used_idx; 2346c3169a3SBruce Richardson struct vring_used_elem *uep; 2356c3169a3SBruce Richardson 2366c3169a3SBruce Richardson used_idx = (uint32_t)(vq->vq_used_cons_idx 2376c3169a3SBruce Richardson & (vq->vq_nentries - 1)); 2386c3169a3SBruce Richardson uep = &vq->vq_ring.used->ring[used_idx]; 2396c3169a3SBruce Richardson idx = (uint32_t) uep->id; 2406c3169a3SBruce Richardson desc_idx = idx; 2416c3169a3SBruce Richardson 2426c3169a3SBruce Richardson while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) { 2436c3169a3SBruce Richardson desc_idx = vq->vq_ring.desc[desc_idx].next; 2446c3169a3SBruce Richardson vq->vq_free_cnt++; 2456c3169a3SBruce Richardson } 2466c3169a3SBruce Richardson 2476c3169a3SBruce Richardson vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx; 2486c3169a3SBruce Richardson vq->vq_desc_head_idx = idx; 2496c3169a3SBruce Richardson 2506c3169a3SBruce Richardson vq->vq_used_cons_idx++; 2516c3169a3SBruce Richardson vq->vq_free_cnt++; 2526c3169a3SBruce Richardson } 2536c3169a3SBruce Richardson 2546c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d", 2556c3169a3SBruce Richardson vq->vq_free_cnt, vq->vq_desc_head_idx); 2566c3169a3SBruce Richardson 25701ad44fdSHuawei Xie memcpy(&result, cvq->virtio_net_hdr_mz->addr, 2586c3169a3SBruce Richardson sizeof(struct virtio_pmd_ctrl)); 2596c3169a3SBruce Richardson 2606c3169a3SBruce Richardson return result.status; 2616c3169a3SBruce Richardson } 2626c3169a3SBruce Richardson 2636c3169a3SBruce Richardson static int 2646c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues) 2656c3169a3SBruce Richardson { 2666c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 2676c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 2686c3169a3SBruce Richardson int dlen[1]; 2696c3169a3SBruce Richardson int ret; 2706c3169a3SBruce Richardson 2716c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_MQ; 2726c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; 2736c3169a3SBruce Richardson memcpy(ctrl.data, &nb_queues, sizeof(uint16_t)); 2746c3169a3SBruce Richardson 2756c3169a3SBruce Richardson dlen[0] = sizeof(uint16_t); 2766c3169a3SBruce Richardson 2776c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 2786c3169a3SBruce Richardson if (ret) { 2796c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Multiqueue configured but send command " 2806c3169a3SBruce Richardson "failed, this is too late now..."); 2816c3169a3SBruce Richardson return -EINVAL; 2826c3169a3SBruce Richardson } 2836c3169a3SBruce Richardson 2846c3169a3SBruce Richardson return 0; 2856c3169a3SBruce Richardson } 2866c3169a3SBruce Richardson 28769c80d4eSYuanhan Liu static void 28869c80d4eSYuanhan Liu virtio_dev_queue_release(void *queue __rte_unused) 2892963d99aSJianfeng Tan { 29069c80d4eSYuanhan Liu /* do nothing */ 2915382b188SBernard Iremonger } 2925382b188SBernard Iremonger 29369c80d4eSYuanhan Liu static int 29469c80d4eSYuanhan Liu virtio_get_queue_type(struct virtio_hw *hw, uint16_t vtpci_queue_idx) 29569c80d4eSYuanhan Liu { 29669c80d4eSYuanhan Liu if (vtpci_queue_idx == hw->max_queue_pairs * 2) 29769c80d4eSYuanhan Liu return VTNET_CQ; 29869c80d4eSYuanhan Liu else if (vtpci_queue_idx % 2 == 0) 29969c80d4eSYuanhan Liu return VTNET_RQ; 30069c80d4eSYuanhan Liu else 30169c80d4eSYuanhan Liu return VTNET_TQ; 30269c80d4eSYuanhan Liu } 30369c80d4eSYuanhan Liu 30469c80d4eSYuanhan Liu static uint16_t 30569c80d4eSYuanhan Liu virtio_get_nr_vq(struct virtio_hw *hw) 30669c80d4eSYuanhan Liu { 30769c80d4eSYuanhan Liu uint16_t nr_vq = hw->max_queue_pairs * 2; 30869c80d4eSYuanhan Liu 30969c80d4eSYuanhan Liu if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) 31069c80d4eSYuanhan Liu nr_vq += 1; 31169c80d4eSYuanhan Liu 31269c80d4eSYuanhan Liu return nr_vq; 31369c80d4eSYuanhan Liu } 31469c80d4eSYuanhan Liu 315f4d1ad15SYuanhan Liu static void 316f4d1ad15SYuanhan Liu virtio_init_vring(struct virtqueue *vq) 317f4d1ad15SYuanhan Liu { 318f4d1ad15SYuanhan Liu int size = vq->vq_nentries; 319f4d1ad15SYuanhan Liu struct vring *vr = &vq->vq_ring; 320f4d1ad15SYuanhan Liu uint8_t *ring_mem = vq->vq_ring_virt_mem; 321f4d1ad15SYuanhan Liu 322f4d1ad15SYuanhan Liu PMD_INIT_FUNC_TRACE(); 323f4d1ad15SYuanhan Liu 324f4d1ad15SYuanhan Liu /* 325f4d1ad15SYuanhan Liu * Reinitialise since virtio port might have been stopped and restarted 326f4d1ad15SYuanhan Liu */ 327f4d1ad15SYuanhan Liu memset(ring_mem, 0, vq->vq_ring_size); 328f4d1ad15SYuanhan Liu vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN); 329f4d1ad15SYuanhan Liu vq->vq_used_cons_idx = 0; 330f4d1ad15SYuanhan Liu vq->vq_desc_head_idx = 0; 331f4d1ad15SYuanhan Liu vq->vq_avail_idx = 0; 332f4d1ad15SYuanhan Liu vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1); 333f4d1ad15SYuanhan Liu vq->vq_free_cnt = vq->vq_nentries; 334f4d1ad15SYuanhan Liu memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries); 335f4d1ad15SYuanhan Liu 336f4d1ad15SYuanhan Liu vring_desc_init(vr->desc, size); 337f4d1ad15SYuanhan Liu 338f4d1ad15SYuanhan Liu /* 339f4d1ad15SYuanhan Liu * Disable device(host) interrupting guest 340f4d1ad15SYuanhan Liu */ 341f4d1ad15SYuanhan Liu virtqueue_disable_intr(vq); 342f4d1ad15SYuanhan Liu } 343f4d1ad15SYuanhan Liu 34469c80d4eSYuanhan Liu static int 34569c80d4eSYuanhan Liu virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx) 3466c3169a3SBruce Richardson { 3476c3169a3SBruce Richardson char vq_name[VIRTQUEUE_MAX_NAME_SZ]; 34801ad44fdSHuawei Xie char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ]; 34901ad44fdSHuawei Xie const struct rte_memzone *mz = NULL, *hdr_mz = NULL; 3501e7bd238SStephen Hemminger unsigned int vq_size, size; 3516c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 352f24f8f9fSJianfeng Tan struct virtnet_rx *rxvq = NULL; 353f24f8f9fSJianfeng Tan struct virtnet_tx *txvq = NULL; 354f24f8f9fSJianfeng Tan struct virtnet_ctl *cvq = NULL; 35501ad44fdSHuawei Xie struct virtqueue *vq; 356905a2469SYuanhan Liu size_t sz_hdr_mz = 0; 35701ad44fdSHuawei Xie void *sw_ring = NULL; 35869c80d4eSYuanhan Liu int queue_type = virtio_get_queue_type(hw, vtpci_queue_idx); 35901ad44fdSHuawei Xie int ret; 3606c3169a3SBruce Richardson 361d5bbeefcSYuanhan Liu PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx); 3626c3169a3SBruce Richardson 3636c3169a3SBruce Richardson /* 3646c3169a3SBruce Richardson * Read the virtqueue size from the Queue Size field 3656c3169a3SBruce Richardson * Always power of 2 and if 0 virtqueue does not exist 3666c3169a3SBruce Richardson */ 367553f4593SYuanhan Liu vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx); 36869c80d4eSYuanhan Liu PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size); 3696c3169a3SBruce Richardson if (vq_size == 0) { 3700bb159adSHuawei Xie PMD_INIT_LOG(ERR, "virtqueue does not exist"); 3716c3169a3SBruce Richardson return -EINVAL; 372d78deadaSStephen Hemminger } 373d78deadaSStephen Hemminger 374d78deadaSStephen Hemminger if (!rte_is_power_of_2(vq_size)) { 3750bb159adSHuawei Xie PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2"); 3766c3169a3SBruce Richardson return -EINVAL; 377d78deadaSStephen Hemminger } 378d78deadaSStephen Hemminger 37973d017ddSYuanhan Liu snprintf(vq_name, sizeof(vq_name), "port%d_vq%d", 38073d017ddSYuanhan Liu dev->data->port_id, vtpci_queue_idx); 38101ad44fdSHuawei Xie 382905a2469SYuanhan Liu size = RTE_ALIGN_CEIL(sizeof(*vq) + 3836c3169a3SBruce Richardson vq_size * sizeof(struct vq_desc_extra), 3846c3169a3SBruce Richardson RTE_CACHE_LINE_SIZE); 385905a2469SYuanhan Liu if (queue_type == VTNET_TQ) { 38601ad44fdSHuawei Xie /* 38701ad44fdSHuawei Xie * For each xmit packet, allocate a virtio_net_hdr 38801ad44fdSHuawei Xie * and indirect ring elements 38901ad44fdSHuawei Xie */ 39001ad44fdSHuawei Xie sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region); 39101ad44fdSHuawei Xie } else if (queue_type == VTNET_CQ) { 39201ad44fdSHuawei Xie /* Allocate a page for control vq command, data and status */ 39301ad44fdSHuawei Xie sz_hdr_mz = PAGE_SIZE; 39401ad44fdSHuawei Xie } 3954166bbf6SJianfeng Tan 39669c80d4eSYuanhan Liu vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE, 39769c80d4eSYuanhan Liu SOCKET_ID_ANY); 39801ad44fdSHuawei Xie if (vq == NULL) { 39901ad44fdSHuawei Xie PMD_INIT_LOG(ERR, "can not allocate vq"); 400cab04612SHuawei Xie return -ENOMEM; 401cab04612SHuawei Xie } 40269c80d4eSYuanhan Liu hw->vqs[vtpci_queue_idx] = vq; 40369c80d4eSYuanhan Liu 4046c3169a3SBruce Richardson vq->hw = hw; 4056c3169a3SBruce Richardson vq->vq_queue_index = vtpci_queue_idx; 4066c3169a3SBruce Richardson vq->vq_nentries = vq_size; 4076c3169a3SBruce Richardson 4086c3169a3SBruce Richardson /* 4096c3169a3SBruce Richardson * Reserve a memzone for vring elements 4106c3169a3SBruce Richardson */ 4116c3169a3SBruce Richardson size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN); 4126c3169a3SBruce Richardson vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN); 41301ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", 41401ad44fdSHuawei Xie size, vq->vq_ring_size); 4156c3169a3SBruce Richardson 41669c80d4eSYuanhan Liu mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size, 41769c80d4eSYuanhan Liu SOCKET_ID_ANY, 41801ad44fdSHuawei Xie 0, VIRTIO_PCI_VRING_ALIGN); 4196c3169a3SBruce Richardson if (mz == NULL) { 420abf4c84bSBernard Iremonger if (rte_errno == EEXIST) 421abf4c84bSBernard Iremonger mz = rte_memzone_lookup(vq_name); 422abf4c84bSBernard Iremonger if (mz == NULL) { 42301ad44fdSHuawei Xie ret = -ENOMEM; 42401ad44fdSHuawei Xie goto fail_q_alloc; 4256c3169a3SBruce Richardson } 426abf4c84bSBernard Iremonger } 4276c3169a3SBruce Richardson 4285bdd24e4STiwei Bie memset(mz->addr, 0, mz->len); 42901ad44fdSHuawei Xie 4306c3169a3SBruce Richardson vq->vq_ring_mem = mz->phys_addr; 4316c3169a3SBruce Richardson vq->vq_ring_virt_mem = mz->addr; 43201ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64, 43301ad44fdSHuawei Xie (uint64_t)mz->phys_addr); 43401ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64, 43501ad44fdSHuawei Xie (uint64_t)(uintptr_t)mz->addr); 4366c3169a3SBruce Richardson 437f4d1ad15SYuanhan Liu virtio_init_vring(vq); 438f4d1ad15SYuanhan Liu 43901ad44fdSHuawei Xie if (sz_hdr_mz) { 44073d017ddSYuanhan Liu snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_vq%d_hdr", 44173d017ddSYuanhan Liu dev->data->port_id, vtpci_queue_idx); 44201ad44fdSHuawei Xie hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz, 44369c80d4eSYuanhan Liu SOCKET_ID_ANY, 0, 4446dc5de3aSStephen Hemminger RTE_CACHE_LINE_SIZE); 4456dc5de3aSStephen Hemminger if (hdr_mz == NULL) { 446abf4c84bSBernard Iremonger if (rte_errno == EEXIST) 44701ad44fdSHuawei Xie hdr_mz = rte_memzone_lookup(vq_hdr_name); 4486dc5de3aSStephen Hemminger if (hdr_mz == NULL) { 44901ad44fdSHuawei Xie ret = -ENOMEM; 45001ad44fdSHuawei Xie goto fail_q_alloc; 4516c3169a3SBruce Richardson } 452abf4c84bSBernard Iremonger } 45301ad44fdSHuawei Xie } 45401ad44fdSHuawei Xie 45501ad44fdSHuawei Xie if (queue_type == VTNET_RQ) { 45601ad44fdSHuawei Xie size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) * 45701ad44fdSHuawei Xie sizeof(vq->sw_ring[0]); 45801ad44fdSHuawei Xie 45901ad44fdSHuawei Xie sw_ring = rte_zmalloc_socket("sw_ring", sz_sw, 46069c80d4eSYuanhan Liu RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY); 46101ad44fdSHuawei Xie if (!sw_ring) { 46201ad44fdSHuawei Xie PMD_INIT_LOG(ERR, "can not allocate RX soft ring"); 46301ad44fdSHuawei Xie ret = -ENOMEM; 46401ad44fdSHuawei Xie goto fail_q_alloc; 46501ad44fdSHuawei Xie } 46601ad44fdSHuawei Xie 46701ad44fdSHuawei Xie vq->sw_ring = sw_ring; 468905a2469SYuanhan Liu rxvq = &vq->rxq; 46901ad44fdSHuawei Xie rxvq->vq = vq; 47001ad44fdSHuawei Xie rxvq->port_id = dev->data->port_id; 47101ad44fdSHuawei Xie rxvq->mz = mz; 47201ad44fdSHuawei Xie } else if (queue_type == VTNET_TQ) { 473905a2469SYuanhan Liu txvq = &vq->txq; 47401ad44fdSHuawei Xie txvq->vq = vq; 47501ad44fdSHuawei Xie txvq->port_id = dev->data->port_id; 47601ad44fdSHuawei Xie txvq->mz = mz; 47701ad44fdSHuawei Xie txvq->virtio_net_hdr_mz = hdr_mz; 47801ad44fdSHuawei Xie txvq->virtio_net_hdr_mem = hdr_mz->phys_addr; 479f24f8f9fSJianfeng Tan } else if (queue_type == VTNET_CQ) { 480905a2469SYuanhan Liu cvq = &vq->cq; 481f24f8f9fSJianfeng Tan cvq->vq = vq; 482f24f8f9fSJianfeng Tan cvq->mz = mz; 483f24f8f9fSJianfeng Tan cvq->virtio_net_hdr_mz = hdr_mz; 484f24f8f9fSJianfeng Tan cvq->virtio_net_hdr_mem = hdr_mz->phys_addr; 485f24f8f9fSJianfeng Tan memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE); 48669c80d4eSYuanhan Liu 48769c80d4eSYuanhan Liu hw->cvq = cvq; 488f24f8f9fSJianfeng Tan } 489f24f8f9fSJianfeng Tan 4906a7c0dfcSJan Blunck /* For virtio_user case (that is when hw->dev is NULL), we use 491f24f8f9fSJianfeng Tan * virtual address. And we need properly set _offset_, please see 49225f80d10SOlivier Matz * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information. 493f24f8f9fSJianfeng Tan */ 4949470427cSYuanhan Liu if (!hw->virtio_user_dev) 495f24f8f9fSJianfeng Tan vq->offset = offsetof(struct rte_mbuf, buf_physaddr); 496f24f8f9fSJianfeng Tan else { 497f24f8f9fSJianfeng Tan vq->vq_ring_mem = (uintptr_t)mz->addr; 498f24f8f9fSJianfeng Tan vq->offset = offsetof(struct rte_mbuf, buf_addr); 499f24f8f9fSJianfeng Tan if (queue_type == VTNET_TQ) 500f24f8f9fSJianfeng Tan txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr; 501f24f8f9fSJianfeng Tan else if (queue_type == VTNET_CQ) 502f24f8f9fSJianfeng Tan cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr; 503f24f8f9fSJianfeng Tan } 504f24f8f9fSJianfeng Tan 505f24f8f9fSJianfeng Tan if (queue_type == VTNET_TQ) { 506f24f8f9fSJianfeng Tan struct virtio_tx_region *txr; 507f24f8f9fSJianfeng Tan unsigned int i; 508f24f8f9fSJianfeng Tan 5096dc5de3aSStephen Hemminger txr = hdr_mz->addr; 5106dc5de3aSStephen Hemminger memset(txr, 0, vq_size * sizeof(*txr)); 5116dc5de3aSStephen Hemminger for (i = 0; i < vq_size; i++) { 5126dc5de3aSStephen Hemminger struct vring_desc *start_dp = txr[i].tx_indir; 5136dc5de3aSStephen Hemminger 5146dc5de3aSStephen Hemminger vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir)); 5156dc5de3aSStephen Hemminger 5166dc5de3aSStephen Hemminger /* first indirect descriptor is always the tx header */ 51701ad44fdSHuawei Xie start_dp->addr = txvq->virtio_net_hdr_mem 5186dc5de3aSStephen Hemminger + i * sizeof(*txr) 5196dc5de3aSStephen Hemminger + offsetof(struct virtio_tx_region, tx_hdr); 5206dc5de3aSStephen Hemminger 52101ad44fdSHuawei Xie start_dp->len = hw->vtnet_hdr_size; 5226dc5de3aSStephen Hemminger start_dp->flags = VRING_DESC_F_NEXT; 5236dc5de3aSStephen Hemminger } 5246c3169a3SBruce Richardson } 5256c3169a3SBruce Richardson 526553f4593SYuanhan Liu if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) { 527595454c5SJianfeng Tan PMD_INIT_LOG(ERR, "setup_queue failed"); 528595454c5SJianfeng Tan return -EINVAL; 529595454c5SJianfeng Tan } 530595454c5SJianfeng Tan 5316c3169a3SBruce Richardson return 0; 53201ad44fdSHuawei Xie 53301ad44fdSHuawei Xie fail_q_alloc: 53401ad44fdSHuawei Xie rte_free(sw_ring); 53501ad44fdSHuawei Xie rte_memzone_free(hdr_mz); 53601ad44fdSHuawei Xie rte_memzone_free(mz); 53701ad44fdSHuawei Xie rte_free(vq); 53801ad44fdSHuawei Xie 53901ad44fdSHuawei Xie return ret; 5406c3169a3SBruce Richardson } 5416c3169a3SBruce Richardson 54269c80d4eSYuanhan Liu static void 54369c80d4eSYuanhan Liu virtio_free_queues(struct virtio_hw *hw) 5446c3169a3SBruce Richardson { 54569c80d4eSYuanhan Liu uint16_t nr_vq = virtio_get_nr_vq(hw); 54669c80d4eSYuanhan Liu struct virtqueue *vq; 54769c80d4eSYuanhan Liu int queue_type; 54869c80d4eSYuanhan Liu uint16_t i; 5496c3169a3SBruce Richardson 5500e78cfddSHuanle Han if (hw->vqs == NULL) 5510e78cfddSHuanle Han return; 5520e78cfddSHuanle Han 55369c80d4eSYuanhan Liu for (i = 0; i < nr_vq; i++) { 55469c80d4eSYuanhan Liu vq = hw->vqs[i]; 55569c80d4eSYuanhan Liu if (!vq) 55669c80d4eSYuanhan Liu continue; 55769c80d4eSYuanhan Liu 55869c80d4eSYuanhan Liu queue_type = virtio_get_queue_type(hw, i); 55969c80d4eSYuanhan Liu if (queue_type == VTNET_RQ) { 56069c80d4eSYuanhan Liu rte_free(vq->sw_ring); 56169c80d4eSYuanhan Liu rte_memzone_free(vq->rxq.mz); 56269c80d4eSYuanhan Liu } else if (queue_type == VTNET_TQ) { 56369c80d4eSYuanhan Liu rte_memzone_free(vq->txq.mz); 56469c80d4eSYuanhan Liu rte_memzone_free(vq->txq.virtio_net_hdr_mz); 56569c80d4eSYuanhan Liu } else { 56669c80d4eSYuanhan Liu rte_memzone_free(vq->cq.mz); 56769c80d4eSYuanhan Liu rte_memzone_free(vq->cq.virtio_net_hdr_mz); 56869c80d4eSYuanhan Liu } 56969c80d4eSYuanhan Liu 57069c80d4eSYuanhan Liu rte_free(vq); 5710e78cfddSHuanle Han hw->vqs[i] = NULL; 57269c80d4eSYuanhan Liu } 57369c80d4eSYuanhan Liu 57469c80d4eSYuanhan Liu rte_free(hw->vqs); 5750e78cfddSHuanle Han hw->vqs = NULL; 57669c80d4eSYuanhan Liu } 57769c80d4eSYuanhan Liu 57869c80d4eSYuanhan Liu static int 57969c80d4eSYuanhan Liu virtio_alloc_queues(struct rte_eth_dev *dev) 58069c80d4eSYuanhan Liu { 58169c80d4eSYuanhan Liu struct virtio_hw *hw = dev->data->dev_private; 58269c80d4eSYuanhan Liu uint16_t nr_vq = virtio_get_nr_vq(hw); 58369c80d4eSYuanhan Liu uint16_t i; 58469c80d4eSYuanhan Liu int ret; 58569c80d4eSYuanhan Liu 58669c80d4eSYuanhan Liu hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0); 58769c80d4eSYuanhan Liu if (!hw->vqs) { 58869c80d4eSYuanhan Liu PMD_INIT_LOG(ERR, "failed to allocate vqs"); 58969c80d4eSYuanhan Liu return -ENOMEM; 59069c80d4eSYuanhan Liu } 59169c80d4eSYuanhan Liu 59269c80d4eSYuanhan Liu for (i = 0; i < nr_vq; i++) { 59369c80d4eSYuanhan Liu ret = virtio_init_queue(dev, i); 5946c3169a3SBruce Richardson if (ret < 0) { 59569c80d4eSYuanhan Liu virtio_free_queues(hw); 5966c3169a3SBruce Richardson return ret; 5976c3169a3SBruce Richardson } 5986c3169a3SBruce Richardson } 5996c3169a3SBruce Richardson 60069c80d4eSYuanhan Liu return 0; 601941d64b5SBernard Iremonger } 602941d64b5SBernard Iremonger 6039ebdeefeSJianfeng Tan static void virtio_queues_unbind_intr(struct rte_eth_dev *dev); 6049ebdeefeSJianfeng Tan 605941d64b5SBernard Iremonger static void 6066c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev) 6076c3169a3SBruce Richardson { 6086c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 6099ebdeefeSJianfeng Tan struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf; 6106c3169a3SBruce Richardson 6116c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "virtio_dev_close"); 6126c3169a3SBruce Richardson 6136c3169a3SBruce Richardson /* reset the NIC */ 61462a785a6SJianfeng Tan if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 615981e61f5SJianfeng Tan VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR); 6169ebdeefeSJianfeng Tan if (intr_conf->rxq) 6179ebdeefeSJianfeng Tan virtio_queues_unbind_intr(dev); 6189ebdeefeSJianfeng Tan 6199ebdeefeSJianfeng Tan if (intr_conf->lsc || intr_conf->rxq) { 6209ebdeefeSJianfeng Tan rte_intr_disable(dev->intr_handle); 6219ebdeefeSJianfeng Tan rte_intr_efd_disable(dev->intr_handle); 6229ebdeefeSJianfeng Tan rte_free(dev->intr_handle->intr_vec); 6239ebdeefeSJianfeng Tan dev->intr_handle->intr_vec = NULL; 6249ebdeefeSJianfeng Tan } 6259ebdeefeSJianfeng Tan 6266c3169a3SBruce Richardson vtpci_reset(hw); 6276c3169a3SBruce Richardson virtio_dev_free_mbufs(dev); 62869c80d4eSYuanhan Liu virtio_free_queues(hw); 6296c3169a3SBruce Richardson } 6306c3169a3SBruce Richardson 6316c3169a3SBruce Richardson static void 6326c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev) 6336c3169a3SBruce Richardson { 6346c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 6356c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 6366c3169a3SBruce Richardson int dlen[1]; 6376c3169a3SBruce Richardson int ret; 6386c3169a3SBruce Richardson 639e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 640f2462150SFerruh Yigit PMD_INIT_LOG(INFO, "host does not support rx control"); 641e9e414a4SStephen Hemminger return; 642e9e414a4SStephen Hemminger } 643e9e414a4SStephen Hemminger 6446c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 6456c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 6466c3169a3SBruce Richardson ctrl.data[0] = 1; 6476c3169a3SBruce Richardson dlen[0] = 1; 6486c3169a3SBruce Richardson 6496c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 6506c3169a3SBruce Richardson if (ret) 6516c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to enable promisc"); 6526c3169a3SBruce Richardson } 6536c3169a3SBruce Richardson 6546c3169a3SBruce Richardson static void 6556c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev) 6566c3169a3SBruce Richardson { 6576c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 6586c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 6596c3169a3SBruce Richardson int dlen[1]; 6606c3169a3SBruce Richardson int ret; 6616c3169a3SBruce Richardson 662e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 663f2462150SFerruh Yigit PMD_INIT_LOG(INFO, "host does not support rx control"); 664e9e414a4SStephen Hemminger return; 665e9e414a4SStephen Hemminger } 666e9e414a4SStephen Hemminger 6676c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 6686c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 6696c3169a3SBruce Richardson ctrl.data[0] = 0; 6706c3169a3SBruce Richardson dlen[0] = 1; 6716c3169a3SBruce Richardson 6726c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 6736c3169a3SBruce Richardson if (ret) 6746c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to disable promisc"); 6756c3169a3SBruce Richardson } 6766c3169a3SBruce Richardson 6776c3169a3SBruce Richardson static void 6786c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev) 6796c3169a3SBruce Richardson { 6806c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 6816c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 6826c3169a3SBruce Richardson int dlen[1]; 6836c3169a3SBruce Richardson int ret; 6846c3169a3SBruce Richardson 685e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 686f2462150SFerruh Yigit PMD_INIT_LOG(INFO, "host does not support rx control"); 687e9e414a4SStephen Hemminger return; 688e9e414a4SStephen Hemminger } 689e9e414a4SStephen Hemminger 6906c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 6916c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 6926c3169a3SBruce Richardson ctrl.data[0] = 1; 6936c3169a3SBruce Richardson dlen[0] = 1; 6946c3169a3SBruce Richardson 6956c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 6966c3169a3SBruce Richardson if (ret) 6976c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to enable allmulticast"); 6986c3169a3SBruce Richardson } 6996c3169a3SBruce Richardson 7006c3169a3SBruce Richardson static void 7016c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev) 7026c3169a3SBruce Richardson { 7036c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 7046c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 7056c3169a3SBruce Richardson int dlen[1]; 7066c3169a3SBruce Richardson int ret; 7076c3169a3SBruce Richardson 708e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 709f2462150SFerruh Yigit PMD_INIT_LOG(INFO, "host does not support rx control"); 710e9e414a4SStephen Hemminger return; 711e9e414a4SStephen Hemminger } 712e9e414a4SStephen Hemminger 7136c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 7146c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 7156c3169a3SBruce Richardson ctrl.data[0] = 0; 7166c3169a3SBruce Richardson dlen[0] = 1; 7176c3169a3SBruce Richardson 7186c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 7196c3169a3SBruce Richardson if (ret) 7206c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to disable allmulticast"); 7216c3169a3SBruce Richardson } 7226c3169a3SBruce Richardson 7234ec2424aSSouvik Dey #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */ 7244ec2424aSSouvik Dey static int 7254ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu) 7264ec2424aSSouvik Dey { 7274ec2424aSSouvik Dey struct virtio_hw *hw = dev->data->dev_private; 7284ec2424aSSouvik Dey uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN + 7294ec2424aSSouvik Dey hw->vtnet_hdr_size; 7304ec2424aSSouvik Dey uint32_t frame_size = mtu + ether_hdr_len; 73149d26d9eSMaxime Coquelin uint32_t max_frame_size = hw->max_mtu + ether_hdr_len; 7324ec2424aSSouvik Dey 73349d26d9eSMaxime Coquelin max_frame_size = RTE_MIN(max_frame_size, VIRTIO_MAX_RX_PKTLEN); 73449d26d9eSMaxime Coquelin 73549d26d9eSMaxime Coquelin if (mtu < ETHER_MIN_MTU || frame_size > max_frame_size) { 736f2462150SFerruh Yigit PMD_INIT_LOG(ERR, "MTU should be between %d and %d", 73749d26d9eSMaxime Coquelin ETHER_MIN_MTU, max_frame_size - ether_hdr_len); 7384ec2424aSSouvik Dey return -EINVAL; 7394ec2424aSSouvik Dey } 7404ec2424aSSouvik Dey return 0; 7414ec2424aSSouvik Dey } 7424ec2424aSSouvik Dey 743c056be23SJianfeng Tan static int 744c056be23SJianfeng Tan virtio_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id) 745c056be23SJianfeng Tan { 746c056be23SJianfeng Tan struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id]; 747c056be23SJianfeng Tan struct virtqueue *vq = rxvq->vq; 748c056be23SJianfeng Tan 749c056be23SJianfeng Tan virtqueue_enable_intr(vq); 750c056be23SJianfeng Tan return 0; 751c056be23SJianfeng Tan } 752c056be23SJianfeng Tan 753c056be23SJianfeng Tan static int 754c056be23SJianfeng Tan virtio_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id) 755c056be23SJianfeng Tan { 756c056be23SJianfeng Tan struct virtnet_rx *rxvq = dev->data->rx_queues[queue_id]; 757c056be23SJianfeng Tan struct virtqueue *vq = rxvq->vq; 758c056be23SJianfeng Tan 759c056be23SJianfeng Tan virtqueue_disable_intr(vq); 760c056be23SJianfeng Tan return 0; 761c056be23SJianfeng Tan } 762c056be23SJianfeng Tan 7636c3169a3SBruce Richardson /* 7646c3169a3SBruce Richardson * dev_ops for virtio, bare necessities for basic operation 7656c3169a3SBruce Richardson */ 7666c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = { 7676c3169a3SBruce Richardson .dev_configure = virtio_dev_configure, 7686c3169a3SBruce Richardson .dev_start = virtio_dev_start, 7696c3169a3SBruce Richardson .dev_stop = virtio_dev_stop, 7706c3169a3SBruce Richardson .dev_close = virtio_dev_close, 7716c3169a3SBruce Richardson .promiscuous_enable = virtio_dev_promiscuous_enable, 7726c3169a3SBruce Richardson .promiscuous_disable = virtio_dev_promiscuous_disable, 7736c3169a3SBruce Richardson .allmulticast_enable = virtio_dev_allmulticast_enable, 7746c3169a3SBruce Richardson .allmulticast_disable = virtio_dev_allmulticast_disable, 7754ec2424aSSouvik Dey .mtu_set = virtio_mtu_set, 7766c3169a3SBruce Richardson .dev_infos_get = virtio_dev_info_get, 7776c3169a3SBruce Richardson .stats_get = virtio_dev_stats_get, 77876d4c652SHarry van Haaren .xstats_get = virtio_dev_xstats_get, 779baf91c39SRemy Horton .xstats_get_names = virtio_dev_xstats_get_names, 7806c3169a3SBruce Richardson .stats_reset = virtio_dev_stats_reset, 78176d4c652SHarry van Haaren .xstats_reset = virtio_dev_stats_reset, 7826c3169a3SBruce Richardson .link_update = virtio_dev_link_update, 7836c3169a3SBruce Richardson .rx_queue_setup = virtio_dev_rx_queue_setup, 784c056be23SJianfeng Tan .rx_queue_intr_enable = virtio_dev_rx_queue_intr_enable, 785c056be23SJianfeng Tan .rx_queue_intr_disable = virtio_dev_rx_queue_intr_disable, 78669c80d4eSYuanhan Liu .rx_queue_release = virtio_dev_queue_release, 787b0caba1aSJianfeng Tan .rx_descriptor_done = virtio_dev_rx_queue_done, 7886c3169a3SBruce Richardson .tx_queue_setup = virtio_dev_tx_queue_setup, 78969c80d4eSYuanhan Liu .tx_queue_release = virtio_dev_queue_release, 7906c3169a3SBruce Richardson /* collect stats per queue */ 7916c3169a3SBruce Richardson .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set, 7926c3169a3SBruce Richardson .vlan_filter_set = virtio_vlan_filter_set, 7936c3169a3SBruce Richardson .mac_addr_add = virtio_mac_addr_add, 7946c3169a3SBruce Richardson .mac_addr_remove = virtio_mac_addr_remove, 7956c3169a3SBruce Richardson .mac_addr_set = virtio_mac_addr_set, 7966c3169a3SBruce Richardson }; 7976c3169a3SBruce Richardson 7986c3169a3SBruce Richardson static inline int 7996c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev, 8006c3169a3SBruce Richardson struct rte_eth_link *link) 8016c3169a3SBruce Richardson { 8026c3169a3SBruce Richardson struct rte_eth_link *dst = link; 8036c3169a3SBruce Richardson struct rte_eth_link *src = &(dev->data->dev_link); 8046c3169a3SBruce Richardson 8056c3169a3SBruce Richardson if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst, 8066c3169a3SBruce Richardson *(uint64_t *)src) == 0) 8076c3169a3SBruce Richardson return -1; 8086c3169a3SBruce Richardson 8096c3169a3SBruce Richardson return 0; 8106c3169a3SBruce Richardson } 8116c3169a3SBruce Richardson 8126c3169a3SBruce Richardson /** 8136c3169a3SBruce Richardson * Atomically writes the link status information into global 8146c3169a3SBruce Richardson * structure rte_eth_dev. 8156c3169a3SBruce Richardson * 8166c3169a3SBruce Richardson * @param dev 8176c3169a3SBruce Richardson * - Pointer to the structure rte_eth_dev to read from. 8186c3169a3SBruce Richardson * - Pointer to the buffer to be saved with the link status. 8196c3169a3SBruce Richardson * 8206c3169a3SBruce Richardson * @return 8216c3169a3SBruce Richardson * - On success, zero. 8226c3169a3SBruce Richardson * - On failure, negative value. 8236c3169a3SBruce Richardson */ 8246c3169a3SBruce Richardson static inline int 8256c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev, 8266c3169a3SBruce Richardson struct rte_eth_link *link) 8276c3169a3SBruce Richardson { 8286c3169a3SBruce Richardson struct rte_eth_link *dst = &(dev->data->dev_link); 8296c3169a3SBruce Richardson struct rte_eth_link *src = link; 8306c3169a3SBruce Richardson 8316c3169a3SBruce Richardson if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst, 8326c3169a3SBruce Richardson *(uint64_t *)src) == 0) 8336c3169a3SBruce Richardson return -1; 8346c3169a3SBruce Richardson 8356c3169a3SBruce Richardson return 0; 8366c3169a3SBruce Richardson } 8376c3169a3SBruce Richardson 8386c3169a3SBruce Richardson static void 83976d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 8406c3169a3SBruce Richardson { 8416c3169a3SBruce Richardson unsigned i; 8426c3169a3SBruce Richardson 8436c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_tx_queues; i++) { 84401ad44fdSHuawei Xie const struct virtnet_tx *txvq = dev->data->tx_queues[i]; 8456c3169a3SBruce Richardson if (txvq == NULL) 8466c3169a3SBruce Richardson continue; 8476c3169a3SBruce Richardson 84801ad44fdSHuawei Xie stats->opackets += txvq->stats.packets; 84901ad44fdSHuawei Xie stats->obytes += txvq->stats.bytes; 85001ad44fdSHuawei Xie stats->oerrors += txvq->stats.errors; 8516c3169a3SBruce Richardson 8526c3169a3SBruce Richardson if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 85301ad44fdSHuawei Xie stats->q_opackets[i] = txvq->stats.packets; 85401ad44fdSHuawei Xie stats->q_obytes[i] = txvq->stats.bytes; 8556c3169a3SBruce Richardson } 8566c3169a3SBruce Richardson } 8576c3169a3SBruce Richardson 8586c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_rx_queues; i++) { 85901ad44fdSHuawei Xie const struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 8606c3169a3SBruce Richardson if (rxvq == NULL) 8616c3169a3SBruce Richardson continue; 8626c3169a3SBruce Richardson 86301ad44fdSHuawei Xie stats->ipackets += rxvq->stats.packets; 86401ad44fdSHuawei Xie stats->ibytes += rxvq->stats.bytes; 86501ad44fdSHuawei Xie stats->ierrors += rxvq->stats.errors; 8666c3169a3SBruce Richardson 8676c3169a3SBruce Richardson if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 86801ad44fdSHuawei Xie stats->q_ipackets[i] = rxvq->stats.packets; 86901ad44fdSHuawei Xie stats->q_ibytes[i] = rxvq->stats.bytes; 8706c3169a3SBruce Richardson } 8716c3169a3SBruce Richardson } 8726c3169a3SBruce Richardson 8736c3169a3SBruce Richardson stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; 8746c3169a3SBruce Richardson } 8756c3169a3SBruce Richardson 876baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev, 877baf91c39SRemy Horton struct rte_eth_xstat_name *xstats_names, 878baf91c39SRemy Horton __rte_unused unsigned limit) 879baf91c39SRemy Horton { 880baf91c39SRemy Horton unsigned i; 881baf91c39SRemy Horton unsigned count = 0; 882baf91c39SRemy Horton unsigned t; 883baf91c39SRemy Horton 88401ad44fdSHuawei Xie unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS + 88501ad44fdSHuawei Xie dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS; 886baf91c39SRemy Horton 8877e1eb993SYuanhan Liu if (xstats_names != NULL) { 888baf91c39SRemy Horton /* Note: limit checked in rte_eth_xstats_names() */ 889baf91c39SRemy Horton 890baf91c39SRemy Horton for (i = 0; i < dev->data->nb_rx_queues; i++) { 891baf91c39SRemy Horton struct virtqueue *rxvq = dev->data->rx_queues[i]; 892baf91c39SRemy Horton if (rxvq == NULL) 893baf91c39SRemy Horton continue; 89401ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) { 895baf91c39SRemy Horton snprintf(xstats_names[count].name, 896baf91c39SRemy Horton sizeof(xstats_names[count].name), 897baf91c39SRemy Horton "rx_q%u_%s", i, 89801ad44fdSHuawei Xie rte_virtio_rxq_stat_strings[t].name); 899baf91c39SRemy Horton count++; 900baf91c39SRemy Horton } 901baf91c39SRemy Horton } 902baf91c39SRemy Horton 903baf91c39SRemy Horton for (i = 0; i < dev->data->nb_tx_queues; i++) { 904baf91c39SRemy Horton struct virtqueue *txvq = dev->data->tx_queues[i]; 905baf91c39SRemy Horton if (txvq == NULL) 906baf91c39SRemy Horton continue; 90701ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) { 908baf91c39SRemy Horton snprintf(xstats_names[count].name, 909baf91c39SRemy Horton sizeof(xstats_names[count].name), 910baf91c39SRemy Horton "tx_q%u_%s", i, 91101ad44fdSHuawei Xie rte_virtio_txq_stat_strings[t].name); 912baf91c39SRemy Horton count++; 913baf91c39SRemy Horton } 914baf91c39SRemy Horton } 915baf91c39SRemy Horton return count; 916baf91c39SRemy Horton } 917baf91c39SRemy Horton return nstats; 918baf91c39SRemy Horton } 919baf91c39SRemy Horton 92076d4c652SHarry van Haaren static int 921e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats, 92276d4c652SHarry van Haaren unsigned n) 92376d4c652SHarry van Haaren { 92476d4c652SHarry van Haaren unsigned i; 92576d4c652SHarry van Haaren unsigned count = 0; 92676d4c652SHarry van Haaren 92701ad44fdSHuawei Xie unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS + 92801ad44fdSHuawei Xie dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS; 92976d4c652SHarry van Haaren 93076d4c652SHarry van Haaren if (n < nstats) 93176d4c652SHarry van Haaren return nstats; 93276d4c652SHarry van Haaren 93376d4c652SHarry van Haaren for (i = 0; i < dev->data->nb_rx_queues; i++) { 93401ad44fdSHuawei Xie struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 93576d4c652SHarry van Haaren 93676d4c652SHarry van Haaren if (rxvq == NULL) 93776d4c652SHarry van Haaren continue; 93876d4c652SHarry van Haaren 93976d4c652SHarry van Haaren unsigned t; 94076d4c652SHarry van Haaren 94101ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) { 94276d4c652SHarry van Haaren xstats[count].value = *(uint64_t *)(((char *)rxvq) + 94301ad44fdSHuawei Xie rte_virtio_rxq_stat_strings[t].offset); 944513c78aeSOlivier Matz xstats[count].id = count; 94576d4c652SHarry van Haaren count++; 94676d4c652SHarry van Haaren } 94776d4c652SHarry van Haaren } 94876d4c652SHarry van Haaren 94976d4c652SHarry van Haaren for (i = 0; i < dev->data->nb_tx_queues; i++) { 95001ad44fdSHuawei Xie struct virtnet_tx *txvq = dev->data->tx_queues[i]; 95176d4c652SHarry van Haaren 95276d4c652SHarry van Haaren if (txvq == NULL) 95376d4c652SHarry van Haaren continue; 95476d4c652SHarry van Haaren 95576d4c652SHarry van Haaren unsigned t; 95676d4c652SHarry van Haaren 95701ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) { 95876d4c652SHarry van Haaren xstats[count].value = *(uint64_t *)(((char *)txvq) + 95901ad44fdSHuawei Xie rte_virtio_txq_stat_strings[t].offset); 960513c78aeSOlivier Matz xstats[count].id = count; 96176d4c652SHarry van Haaren count++; 96276d4c652SHarry van Haaren } 96376d4c652SHarry van Haaren } 96476d4c652SHarry van Haaren 96576d4c652SHarry van Haaren return count; 96676d4c652SHarry van Haaren } 96776d4c652SHarry van Haaren 96876d4c652SHarry van Haaren static void 96976d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 97076d4c652SHarry van Haaren { 97176d4c652SHarry van Haaren virtio_update_stats(dev, stats); 97276d4c652SHarry van Haaren } 97376d4c652SHarry van Haaren 9746c3169a3SBruce Richardson static void 9756c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev) 9766c3169a3SBruce Richardson { 9776c3169a3SBruce Richardson unsigned int i; 9786c3169a3SBruce Richardson 9796c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_tx_queues; i++) { 98001ad44fdSHuawei Xie struct virtnet_tx *txvq = dev->data->tx_queues[i]; 9816c3169a3SBruce Richardson if (txvq == NULL) 9826c3169a3SBruce Richardson continue; 9836c3169a3SBruce Richardson 98401ad44fdSHuawei Xie txvq->stats.packets = 0; 98501ad44fdSHuawei Xie txvq->stats.bytes = 0; 98601ad44fdSHuawei Xie txvq->stats.errors = 0; 98701ad44fdSHuawei Xie txvq->stats.multicast = 0; 98801ad44fdSHuawei Xie txvq->stats.broadcast = 0; 98901ad44fdSHuawei Xie memset(txvq->stats.size_bins, 0, 99001ad44fdSHuawei Xie sizeof(txvq->stats.size_bins[0]) * 8); 9916c3169a3SBruce Richardson } 9926c3169a3SBruce Richardson 9936c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_rx_queues; i++) { 99401ad44fdSHuawei Xie struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 9956c3169a3SBruce Richardson if (rxvq == NULL) 9966c3169a3SBruce Richardson continue; 9976c3169a3SBruce Richardson 99801ad44fdSHuawei Xie rxvq->stats.packets = 0; 99901ad44fdSHuawei Xie rxvq->stats.bytes = 0; 100001ad44fdSHuawei Xie rxvq->stats.errors = 0; 100101ad44fdSHuawei Xie rxvq->stats.multicast = 0; 100201ad44fdSHuawei Xie rxvq->stats.broadcast = 0; 100301ad44fdSHuawei Xie memset(rxvq->stats.size_bins, 0, 100401ad44fdSHuawei Xie sizeof(rxvq->stats.size_bins[0]) * 8); 10056c3169a3SBruce Richardson } 10066c3169a3SBruce Richardson } 10076c3169a3SBruce Richardson 10086c3169a3SBruce Richardson static void 10096c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw) 10106c3169a3SBruce Richardson { 10116c3169a3SBruce Richardson vtpci_write_dev_config(hw, 10126c3169a3SBruce Richardson offsetof(struct virtio_net_config, mac), 10136c3169a3SBruce Richardson &hw->mac_addr, ETHER_ADDR_LEN); 10146c3169a3SBruce Richardson } 10156c3169a3SBruce Richardson 10166c3169a3SBruce Richardson static void 10176c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw) 10186c3169a3SBruce Richardson { 10196c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) { 10206c3169a3SBruce Richardson vtpci_read_dev_config(hw, 10216c3169a3SBruce Richardson offsetof(struct virtio_net_config, mac), 10226c3169a3SBruce Richardson &hw->mac_addr, ETHER_ADDR_LEN); 10236c3169a3SBruce Richardson } else { 10246c3169a3SBruce Richardson eth_random_addr(&hw->mac_addr[0]); 10256c3169a3SBruce Richardson virtio_set_hwaddr(hw); 10266c3169a3SBruce Richardson } 10276c3169a3SBruce Richardson } 10286c3169a3SBruce Richardson 10296d01e580SWei Dai static int 10306c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw, 10316c3169a3SBruce Richardson const struct virtio_net_ctrl_mac *uc, 10326c3169a3SBruce Richardson const struct virtio_net_ctrl_mac *mc) 10336c3169a3SBruce Richardson { 10346c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 10356c3169a3SBruce Richardson int err, len[2]; 10366c3169a3SBruce Richardson 103727046236SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 1038e9083127SJianfeng Tan PMD_DRV_LOG(INFO, "host does not support mac table"); 10396d01e580SWei Dai return -1; 104027046236SStephen Hemminger } 104127046236SStephen Hemminger 10426c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 10436c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET; 10446c3169a3SBruce Richardson 10456c3169a3SBruce Richardson len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries); 10466c3169a3SBruce Richardson memcpy(ctrl.data, uc, len[0]); 10476c3169a3SBruce Richardson 10486c3169a3SBruce Richardson len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries); 10496c3169a3SBruce Richardson memcpy(ctrl.data + len[0], mc, len[1]); 10506c3169a3SBruce Richardson 10516c3169a3SBruce Richardson err = virtio_send_command(hw->cvq, &ctrl, len, 2); 10526c3169a3SBruce Richardson if (err != 0) 10536c3169a3SBruce Richardson PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err); 10546d01e580SWei Dai return err; 10556c3169a3SBruce Richardson } 10566c3169a3SBruce Richardson 10576d01e580SWei Dai static int 10586c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr, 10596c3169a3SBruce Richardson uint32_t index, uint32_t vmdq __rte_unused) 10606c3169a3SBruce Richardson { 10616c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 10626c3169a3SBruce Richardson const struct ether_addr *addrs = dev->data->mac_addrs; 10636c3169a3SBruce Richardson unsigned int i; 10646c3169a3SBruce Richardson struct virtio_net_ctrl_mac *uc, *mc; 10656c3169a3SBruce Richardson 10666c3169a3SBruce Richardson if (index >= VIRTIO_MAX_MAC_ADDRS) { 10676c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 10686d01e580SWei Dai return -EINVAL; 10696c3169a3SBruce Richardson } 10706c3169a3SBruce Richardson 10716c3169a3SBruce Richardson uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 10726c3169a3SBruce Richardson uc->entries = 0; 10736c3169a3SBruce Richardson mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 10746c3169a3SBruce Richardson mc->entries = 0; 10756c3169a3SBruce Richardson 10766c3169a3SBruce Richardson for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 10776c3169a3SBruce Richardson const struct ether_addr *addr 10786c3169a3SBruce Richardson = (i == index) ? mac_addr : addrs + i; 10796c3169a3SBruce Richardson struct virtio_net_ctrl_mac *tbl 10806c3169a3SBruce Richardson = is_multicast_ether_addr(addr) ? mc : uc; 10816c3169a3SBruce Richardson 10826c3169a3SBruce Richardson memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN); 10836c3169a3SBruce Richardson } 10846c3169a3SBruce Richardson 10856d01e580SWei Dai return virtio_mac_table_set(hw, uc, mc); 10866c3169a3SBruce Richardson } 10876c3169a3SBruce Richardson 10886c3169a3SBruce Richardson static void 10896c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index) 10906c3169a3SBruce Richardson { 10916c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 10926c3169a3SBruce Richardson struct ether_addr *addrs = dev->data->mac_addrs; 10936c3169a3SBruce Richardson struct virtio_net_ctrl_mac *uc, *mc; 10946c3169a3SBruce Richardson unsigned int i; 10956c3169a3SBruce Richardson 10966c3169a3SBruce Richardson if (index >= VIRTIO_MAX_MAC_ADDRS) { 10976c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 10986c3169a3SBruce Richardson return; 10996c3169a3SBruce Richardson } 11006c3169a3SBruce Richardson 11016c3169a3SBruce Richardson uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 11026c3169a3SBruce Richardson uc->entries = 0; 11036c3169a3SBruce Richardson mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 11046c3169a3SBruce Richardson mc->entries = 0; 11056c3169a3SBruce Richardson 11066c3169a3SBruce Richardson for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 11076c3169a3SBruce Richardson struct virtio_net_ctrl_mac *tbl; 11086c3169a3SBruce Richardson 11096c3169a3SBruce Richardson if (i == index || is_zero_ether_addr(addrs + i)) 11106c3169a3SBruce Richardson continue; 11116c3169a3SBruce Richardson 11126c3169a3SBruce Richardson tbl = is_multicast_ether_addr(addrs + i) ? mc : uc; 11136c3169a3SBruce Richardson memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN); 11146c3169a3SBruce Richardson } 11156c3169a3SBruce Richardson 11166c3169a3SBruce Richardson virtio_mac_table_set(hw, uc, mc); 11176c3169a3SBruce Richardson } 11186c3169a3SBruce Richardson 11196c3169a3SBruce Richardson static void 11206c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr) 11216c3169a3SBruce Richardson { 11226c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 11236c3169a3SBruce Richardson 11246c3169a3SBruce Richardson memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN); 11256c3169a3SBruce Richardson 11266c3169a3SBruce Richardson /* Use atomic update if available */ 11276c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 11286c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 11296c3169a3SBruce Richardson int len = ETHER_ADDR_LEN; 11306c3169a3SBruce Richardson 11316c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 11326c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET; 11336c3169a3SBruce Richardson 11346c3169a3SBruce Richardson memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN); 11356c3169a3SBruce Richardson virtio_send_command(hw->cvq, &ctrl, &len, 1); 11366c3169a3SBruce Richardson } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) 11376c3169a3SBruce Richardson virtio_set_hwaddr(hw); 11386c3169a3SBruce Richardson } 11396c3169a3SBruce Richardson 11406c3169a3SBruce Richardson static int 11416c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on) 11426c3169a3SBruce Richardson { 11436c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 11446c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 11456c3169a3SBruce Richardson int len; 11466c3169a3SBruce Richardson 11476c3169a3SBruce Richardson if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) 11486c3169a3SBruce Richardson return -ENOTSUP; 11496c3169a3SBruce Richardson 11506c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN; 11516c3169a3SBruce Richardson ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL; 11526c3169a3SBruce Richardson memcpy(ctrl.data, &vlan_id, sizeof(vlan_id)); 11536c3169a3SBruce Richardson len = sizeof(vlan_id); 11546c3169a3SBruce Richardson 11556c3169a3SBruce Richardson return virtio_send_command(hw->cvq, &ctrl, &len, 1); 11566c3169a3SBruce Richardson } 11576c3169a3SBruce Richardson 11586ba1f63bSYuanhan Liu static int 115960e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features) 11606c3169a3SBruce Richardson { 11613891f233SYuanhan Liu uint64_t host_features; 11626c3169a3SBruce Richardson 11636c3169a3SBruce Richardson /* Prepare guest_features: feature that driver wants to support */ 11643891f233SYuanhan Liu PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64, 116560e6f470SOlivier Matz req_features); 11666c3169a3SBruce Richardson 11676c3169a3SBruce Richardson /* Read device(host) feature bits */ 1168553f4593SYuanhan Liu host_features = VTPCI_OPS(hw)->get_features(hw); 11693891f233SYuanhan Liu PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64, 11706c3169a3SBruce Richardson host_features); 11716c3169a3SBruce Richardson 117249d26d9eSMaxime Coquelin /* If supported, ensure MTU value is valid before acknowledging it. */ 117349d26d9eSMaxime Coquelin if (host_features & req_features & (1ULL << VIRTIO_NET_F_MTU)) { 117449d26d9eSMaxime Coquelin struct virtio_net_config config; 117549d26d9eSMaxime Coquelin 117649d26d9eSMaxime Coquelin vtpci_read_dev_config(hw, 117749d26d9eSMaxime Coquelin offsetof(struct virtio_net_config, mtu), 117849d26d9eSMaxime Coquelin &config.mtu, sizeof(config.mtu)); 117949d26d9eSMaxime Coquelin 118049d26d9eSMaxime Coquelin if (config.mtu < ETHER_MIN_MTU) 118149d26d9eSMaxime Coquelin req_features &= ~(1ULL << VIRTIO_NET_F_MTU); 118249d26d9eSMaxime Coquelin } 118349d26d9eSMaxime Coquelin 11846c3169a3SBruce Richardson /* 11856c3169a3SBruce Richardson * Negotiate features: Subset of device feature bits are written back 11866c3169a3SBruce Richardson * guest feature bits. 11876c3169a3SBruce Richardson */ 118860e6f470SOlivier Matz hw->guest_features = req_features; 11896c3169a3SBruce Richardson hw->guest_features = vtpci_negotiate_features(hw, host_features); 11903891f233SYuanhan Liu PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64, 11916c3169a3SBruce Richardson hw->guest_features); 11926ba1f63bSYuanhan Liu 11936ba1f63bSYuanhan Liu if (hw->modern) { 11946ba1f63bSYuanhan Liu if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) { 11956ba1f63bSYuanhan Liu PMD_INIT_LOG(ERR, 11966ba1f63bSYuanhan Liu "VIRTIO_F_VERSION_1 features is not enabled."); 11976ba1f63bSYuanhan Liu return -1; 11986ba1f63bSYuanhan Liu } 11996ba1f63bSYuanhan Liu vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK); 12006ba1f63bSYuanhan Liu if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) { 12016ba1f63bSYuanhan Liu PMD_INIT_LOG(ERR, 12026ba1f63bSYuanhan Liu "failed to set FEATURES_OK status!"); 12036ba1f63bSYuanhan Liu return -1; 12046ba1f63bSYuanhan Liu } 12056ba1f63bSYuanhan Liu } 12066ba1f63bSYuanhan Liu 120760e6f470SOlivier Matz hw->req_guest_features = req_features; 120860e6f470SOlivier Matz 12096ba1f63bSYuanhan Liu return 0; 12106c3169a3SBruce Richardson } 12116c3169a3SBruce Richardson 12126c3169a3SBruce Richardson /* 12136c3169a3SBruce Richardson * Process Virtio Config changed interrupt and call the callback 12146c3169a3SBruce Richardson * if link state changed. 12156c3169a3SBruce Richardson */ 1216ef53b603SJianfeng Tan void 1217c23a1a30SQi Zhang virtio_interrupt_handler(void *param) 12186c3169a3SBruce Richardson { 12196c3169a3SBruce Richardson struct rte_eth_dev *dev = param; 12206c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 12216c3169a3SBruce Richardson uint8_t isr; 12226c3169a3SBruce Richardson 12236c3169a3SBruce Richardson /* Read interrupt status which clears interrupt */ 12246c3169a3SBruce Richardson isr = vtpci_isr(hw); 12256c3169a3SBruce Richardson PMD_DRV_LOG(INFO, "interrupt status = %#x", isr); 12266c3169a3SBruce Richardson 1227c23a1a30SQi Zhang if (rte_intr_enable(dev->intr_handle) < 0) 12286c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "interrupt enable failed"); 12296c3169a3SBruce Richardson 12306c3169a3SBruce Richardson if (isr & VIRTIO_PCI_ISR_CONFIG) { 12316c3169a3SBruce Richardson if (virtio_dev_link_update(dev, 0) == 0) 12326c3169a3SBruce Richardson _rte_eth_dev_callback_process(dev, 1233d6af1a13SBernard Iremonger RTE_ETH_EVENT_INTR_LSC, 1234d6af1a13SBernard Iremonger NULL, NULL); 12356c3169a3SBruce Richardson } 12366c3169a3SBruce Richardson 12376c3169a3SBruce Richardson } 12386c3169a3SBruce Richardson 12394819eae8SOlivier Matz /* set rx and tx handlers according to what is supported */ 12406c3169a3SBruce Richardson static void 12414819eae8SOlivier Matz set_rxtx_funcs(struct rte_eth_dev *eth_dev) 12426c3169a3SBruce Richardson { 12436c3169a3SBruce Richardson struct virtio_hw *hw = eth_dev->data->dev_private; 12444819eae8SOlivier Matz 12454819eae8SOlivier Matz if (hw->use_simple_rxtx) { 12464819eae8SOlivier Matz PMD_INIT_LOG(INFO, "virtio: using simple Rx path on port %u", 12474819eae8SOlivier Matz eth_dev->data->port_id); 12484819eae8SOlivier Matz eth_dev->rx_pkt_burst = virtio_recv_pkts_vec; 12494819eae8SOlivier Matz } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) { 12504819eae8SOlivier Matz PMD_INIT_LOG(INFO, 12514819eae8SOlivier Matz "virtio: using mergeable buffer Rx path on port %u", 12524819eae8SOlivier Matz eth_dev->data->port_id); 12536c3169a3SBruce Richardson eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts; 12544819eae8SOlivier Matz } else { 12554819eae8SOlivier Matz PMD_INIT_LOG(INFO, "virtio: using standard Rx path on port %u", 12564819eae8SOlivier Matz eth_dev->data->port_id); 12576c3169a3SBruce Richardson eth_dev->rx_pkt_burst = &virtio_recv_pkts; 12586c3169a3SBruce Richardson } 12596c3169a3SBruce Richardson 12604819eae8SOlivier Matz if (hw->use_simple_rxtx) { 12614819eae8SOlivier Matz PMD_INIT_LOG(INFO, "virtio: using simple Tx path on port %u", 12624819eae8SOlivier Matz eth_dev->data->port_id); 12634819eae8SOlivier Matz eth_dev->tx_pkt_burst = virtio_xmit_pkts_simple; 12644819eae8SOlivier Matz } else { 12654819eae8SOlivier Matz PMD_INIT_LOG(INFO, "virtio: using standard Tx path on port %u", 12664819eae8SOlivier Matz eth_dev->data->port_id); 12674819eae8SOlivier Matz eth_dev->tx_pkt_burst = virtio_xmit_pkts; 12684819eae8SOlivier Matz } 12694819eae8SOlivier Matz } 12704819eae8SOlivier Matz 127126b683b4SJianfeng Tan /* Only support 1:1 queue/interrupt mapping so far. 127226b683b4SJianfeng Tan * TODO: support n:1 queue/interrupt mapping when there are limited number of 127326b683b4SJianfeng Tan * interrupt vectors (<N+1). 127426b683b4SJianfeng Tan */ 127526b683b4SJianfeng Tan static int 127626b683b4SJianfeng Tan virtio_queues_bind_intr(struct rte_eth_dev *dev) 127726b683b4SJianfeng Tan { 127826b683b4SJianfeng Tan uint32_t i; 127926b683b4SJianfeng Tan struct virtio_hw *hw = dev->data->dev_private; 128026b683b4SJianfeng Tan 1281f2462150SFerruh Yigit PMD_INIT_LOG(INFO, "queue/interrupt binding"); 128226b683b4SJianfeng Tan for (i = 0; i < dev->data->nb_rx_queues; ++i) { 128326b683b4SJianfeng Tan dev->intr_handle->intr_vec[i] = i + 1; 128426b683b4SJianfeng Tan if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) == 128526b683b4SJianfeng Tan VIRTIO_MSI_NO_VECTOR) { 128626b683b4SJianfeng Tan PMD_DRV_LOG(ERR, "failed to set queue vector"); 128726b683b4SJianfeng Tan return -EBUSY; 128826b683b4SJianfeng Tan } 128926b683b4SJianfeng Tan } 129026b683b4SJianfeng Tan 129126b683b4SJianfeng Tan return 0; 129226b683b4SJianfeng Tan } 129326b683b4SJianfeng Tan 12949ebdeefeSJianfeng Tan static void 12959ebdeefeSJianfeng Tan virtio_queues_unbind_intr(struct rte_eth_dev *dev) 12969ebdeefeSJianfeng Tan { 12979ebdeefeSJianfeng Tan uint32_t i; 12989ebdeefeSJianfeng Tan struct virtio_hw *hw = dev->data->dev_private; 12999ebdeefeSJianfeng Tan 1300f2462150SFerruh Yigit PMD_INIT_LOG(INFO, "queue/interrupt unbinding"); 13019ebdeefeSJianfeng Tan for (i = 0; i < dev->data->nb_rx_queues; ++i) 13029ebdeefeSJianfeng Tan VTPCI_OPS(hw)->set_queue_irq(hw, 13039ebdeefeSJianfeng Tan hw->vqs[i * VTNET_CQ], 13049ebdeefeSJianfeng Tan VIRTIO_MSI_NO_VECTOR); 13059ebdeefeSJianfeng Tan } 13069ebdeefeSJianfeng Tan 130726b683b4SJianfeng Tan static int 130826b683b4SJianfeng Tan virtio_configure_intr(struct rte_eth_dev *dev) 130926b683b4SJianfeng Tan { 131026b683b4SJianfeng Tan struct virtio_hw *hw = dev->data->dev_private; 131126b683b4SJianfeng Tan 131226b683b4SJianfeng Tan if (!rte_intr_cap_multiple(dev->intr_handle)) { 131326b683b4SJianfeng Tan PMD_INIT_LOG(ERR, "Multiple intr vector not supported"); 131426b683b4SJianfeng Tan return -ENOTSUP; 131526b683b4SJianfeng Tan } 131626b683b4SJianfeng Tan 131726b683b4SJianfeng Tan if (rte_intr_efd_enable(dev->intr_handle, dev->data->nb_rx_queues)) { 131826b683b4SJianfeng Tan PMD_INIT_LOG(ERR, "Fail to create eventfd"); 131926b683b4SJianfeng Tan return -1; 132026b683b4SJianfeng Tan } 132126b683b4SJianfeng Tan 132226b683b4SJianfeng Tan if (!dev->intr_handle->intr_vec) { 132326b683b4SJianfeng Tan dev->intr_handle->intr_vec = 132426b683b4SJianfeng Tan rte_zmalloc("intr_vec", 132526b683b4SJianfeng Tan hw->max_queue_pairs * sizeof(int), 0); 132626b683b4SJianfeng Tan if (!dev->intr_handle->intr_vec) { 132726b683b4SJianfeng Tan PMD_INIT_LOG(ERR, "Failed to allocate %u rxq vectors", 132826b683b4SJianfeng Tan hw->max_queue_pairs); 132926b683b4SJianfeng Tan return -ENOMEM; 133026b683b4SJianfeng Tan } 133126b683b4SJianfeng Tan } 133226b683b4SJianfeng Tan 133326b683b4SJianfeng Tan /* Re-register callback to update max_intr */ 133426b683b4SJianfeng Tan rte_intr_callback_unregister(dev->intr_handle, 133526b683b4SJianfeng Tan virtio_interrupt_handler, 133626b683b4SJianfeng Tan dev); 133726b683b4SJianfeng Tan rte_intr_callback_register(dev->intr_handle, 133826b683b4SJianfeng Tan virtio_interrupt_handler, 133926b683b4SJianfeng Tan dev); 134026b683b4SJianfeng Tan 134126b683b4SJianfeng Tan /* DO NOT try to remove this! This function will enable msix, or QEMU 134226b683b4SJianfeng Tan * will encounter SIGSEGV when DRIVER_OK is sent. 134326b683b4SJianfeng Tan * And for legacy devices, this should be done before queue/vec binding 134426b683b4SJianfeng Tan * to change the config size from 20 to 24, or VIRTIO_MSI_QUEUE_VECTOR 134526b683b4SJianfeng Tan * (22) will be ignored. 134626b683b4SJianfeng Tan */ 134726b683b4SJianfeng Tan if (rte_intr_enable(dev->intr_handle) < 0) { 134826b683b4SJianfeng Tan PMD_DRV_LOG(ERR, "interrupt enable failed"); 134926b683b4SJianfeng Tan return -1; 135026b683b4SJianfeng Tan } 135126b683b4SJianfeng Tan 135226b683b4SJianfeng Tan if (virtio_queues_bind_intr(dev) < 0) { 135326b683b4SJianfeng Tan PMD_INIT_LOG(ERR, "Failed to bind queue/interrupt"); 135426b683b4SJianfeng Tan return -1; 135526b683b4SJianfeng Tan } 135626b683b4SJianfeng Tan 135726b683b4SJianfeng Tan return 0; 135826b683b4SJianfeng Tan } 135926b683b4SJianfeng Tan 136060e6f470SOlivier Matz /* reset device and renegotiate features if needed */ 1361198ab336SOlivier Matz static int 136260e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features) 13636c3169a3SBruce Richardson { 13646c3169a3SBruce Richardson struct virtio_hw *hw = eth_dev->data->dev_private; 13656c3169a3SBruce Richardson struct virtio_net_config *config; 13666c3169a3SBruce Richardson struct virtio_net_config local_config; 13679470427cSYuanhan Liu struct rte_pci_device *pci_dev = NULL; 136869c80d4eSYuanhan Liu int ret; 13696c3169a3SBruce Richardson 13706c3169a3SBruce Richardson /* Reset the device although not necessary at startup */ 13716c3169a3SBruce Richardson vtpci_reset(hw); 13726c3169a3SBruce Richardson 13736c3169a3SBruce Richardson /* Tell the host we've noticed this device. */ 13746c3169a3SBruce Richardson vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK); 13756c3169a3SBruce Richardson 13766c3169a3SBruce Richardson /* Tell the host we've known how to drive the device. */ 13776c3169a3SBruce Richardson vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER); 137860e6f470SOlivier Matz if (virtio_negotiate_features(hw, req_features) < 0) 13796ba1f63bSYuanhan Liu return -1; 13806c3169a3SBruce Richardson 13812c2201fbSJianfeng Tan if (!hw->virtio_user_dev) { 1382c0802544SFerruh Yigit pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev); 1383f229eb41SJianfeng Tan rte_eth_copy_pci_info(eth_dev, pci_dev); 1384f229eb41SJianfeng Tan } 1385f229eb41SJianfeng Tan 1386a60a0c15SJianfeng Tan eth_dev->data->dev_flags = RTE_ETH_DEV_DETACHABLE; 1387a5ed8448SMatt Peters /* If host does not support both status and MSI-X then disable LSC */ 1388a5ed8448SMatt Peters if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS) && hw->use_msix) 1389198ab336SOlivier Matz eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC; 1390a5ed8448SMatt Peters else 1391a5ed8448SMatt Peters eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC; 1392954ea115SStephen Hemminger 13936c3169a3SBruce Richardson /* Setting up rx_header size for the device */ 13946ba1f63bSYuanhan Liu if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) || 13956ba1f63bSYuanhan Liu vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) 13966c3169a3SBruce Richardson hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf); 13976c3169a3SBruce Richardson else 13986c3169a3SBruce Richardson hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr); 13996c3169a3SBruce Richardson 14006c3169a3SBruce Richardson /* Copy the permanent MAC address to: virtio_hw */ 14016c3169a3SBruce Richardson virtio_get_hwaddr(hw); 14026c3169a3SBruce Richardson ether_addr_copy((struct ether_addr *) hw->mac_addr, 14036c3169a3SBruce Richardson ð_dev->data->mac_addrs[0]); 14046c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 14056c3169a3SBruce Richardson "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 14066c3169a3SBruce Richardson hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2], 14076c3169a3SBruce Richardson hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]); 14086c3169a3SBruce Richardson 14096c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) { 14106c3169a3SBruce Richardson config = &local_config; 14116c3169a3SBruce Richardson 14126d7740e2SChangchun Ouyang vtpci_read_dev_config(hw, 14136d7740e2SChangchun Ouyang offsetof(struct virtio_net_config, mac), 14146d7740e2SChangchun Ouyang &config->mac, sizeof(config->mac)); 14156d7740e2SChangchun Ouyang 14166c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 14176d7740e2SChangchun Ouyang vtpci_read_dev_config(hw, 14186d7740e2SChangchun Ouyang offsetof(struct virtio_net_config, status), 14196d7740e2SChangchun Ouyang &config->status, sizeof(config->status)); 14206c3169a3SBruce Richardson } else { 14216c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 14226c3169a3SBruce Richardson "VIRTIO_NET_F_STATUS is not supported"); 14236c3169a3SBruce Richardson config->status = 0; 14246c3169a3SBruce Richardson } 14256c3169a3SBruce Richardson 14266c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) { 14276d7740e2SChangchun Ouyang vtpci_read_dev_config(hw, 14286d7740e2SChangchun Ouyang offsetof(struct virtio_net_config, max_virtqueue_pairs), 14296d7740e2SChangchun Ouyang &config->max_virtqueue_pairs, 14306d7740e2SChangchun Ouyang sizeof(config->max_virtqueue_pairs)); 14316c3169a3SBruce Richardson } else { 14326c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 14336c3169a3SBruce Richardson "VIRTIO_NET_F_MQ is not supported"); 14346c3169a3SBruce Richardson config->max_virtqueue_pairs = 1; 14356c3169a3SBruce Richardson } 14366c3169a3SBruce Richardson 143745e4acd4SOlivier Matz hw->max_queue_pairs = config->max_virtqueue_pairs; 14386c3169a3SBruce Richardson 143949d26d9eSMaxime Coquelin if (vtpci_with_feature(hw, VIRTIO_NET_F_MTU)) { 144049d26d9eSMaxime Coquelin vtpci_read_dev_config(hw, 144149d26d9eSMaxime Coquelin offsetof(struct virtio_net_config, mtu), 144249d26d9eSMaxime Coquelin &config->mtu, 144349d26d9eSMaxime Coquelin sizeof(config->mtu)); 144449d26d9eSMaxime Coquelin 144549d26d9eSMaxime Coquelin /* 144649d26d9eSMaxime Coquelin * MTU value has already been checked at negotiation 144749d26d9eSMaxime Coquelin * time, but check again in case it has changed since 144849d26d9eSMaxime Coquelin * then, which should not happen. 144949d26d9eSMaxime Coquelin */ 145049d26d9eSMaxime Coquelin if (config->mtu < ETHER_MIN_MTU) { 145149d26d9eSMaxime Coquelin PMD_INIT_LOG(ERR, "invalid max MTU value (%u)", 145249d26d9eSMaxime Coquelin config->mtu); 145349d26d9eSMaxime Coquelin return -1; 145449d26d9eSMaxime Coquelin } 145549d26d9eSMaxime Coquelin 145649d26d9eSMaxime Coquelin hw->max_mtu = config->mtu; 145749d26d9eSMaxime Coquelin /* Set initial MTU to maximum one supported by vhost */ 145849d26d9eSMaxime Coquelin eth_dev->data->mtu = config->mtu; 145949d26d9eSMaxime Coquelin 146049d26d9eSMaxime Coquelin } else { 146149d26d9eSMaxime Coquelin hw->max_mtu = VIRTIO_MAX_RX_PKTLEN - ETHER_HDR_LEN - 146249d26d9eSMaxime Coquelin VLAN_TAG_LEN - hw->vtnet_hdr_size; 146349d26d9eSMaxime Coquelin } 146449d26d9eSMaxime Coquelin 14656c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d", 14666c3169a3SBruce Richardson config->max_virtqueue_pairs); 14676c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "config->status=%d", config->status); 14686c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 14696c3169a3SBruce Richardson "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 14706c3169a3SBruce Richardson config->mac[0], config->mac[1], 14716c3169a3SBruce Richardson config->mac[2], config->mac[3], 14726c3169a3SBruce Richardson config->mac[4], config->mac[5]); 14736c3169a3SBruce Richardson } else { 147445e4acd4SOlivier Matz PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1"); 147545e4acd4SOlivier Matz hw->max_queue_pairs = 1; 14766c3169a3SBruce Richardson } 14776c3169a3SBruce Richardson 147869c80d4eSYuanhan Liu ret = virtio_alloc_queues(eth_dev); 147969c80d4eSYuanhan Liu if (ret < 0) 148069c80d4eSYuanhan Liu return ret; 148126b683b4SJianfeng Tan 148226b683b4SJianfeng Tan if (eth_dev->data->dev_conf.intr_conf.rxq) { 148326b683b4SJianfeng Tan if (virtio_configure_intr(eth_dev) < 0) { 148426b683b4SJianfeng Tan PMD_INIT_LOG(ERR, "failed to configure interrupt"); 148526b683b4SJianfeng Tan return -1; 148626b683b4SJianfeng Tan } 148726b683b4SJianfeng Tan } 148826b683b4SJianfeng Tan 1489ee85024cSYuanhan Liu vtpci_reinit_complete(hw); 149069c80d4eSYuanhan Liu 1491ce2eabddSJianfeng Tan if (pci_dev) 14926c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x", 14936c3169a3SBruce Richardson eth_dev->data->port_id, pci_dev->id.vendor_id, 14946c3169a3SBruce Richardson pci_dev->id.device_id); 14956c3169a3SBruce Richardson 1496198ab336SOlivier Matz return 0; 1497198ab336SOlivier Matz } 1498198ab336SOlivier Matz 1499198ab336SOlivier Matz /* 15006d890f8aSYuanhan Liu * Remap the PCI device again (IO port map for legacy device and 15016d890f8aSYuanhan Liu * memory map for modern device), so that the secondary process 15026d890f8aSYuanhan Liu * could have the PCI initiated correctly. 15036d890f8aSYuanhan Liu */ 15046d890f8aSYuanhan Liu static int 15056d890f8aSYuanhan Liu virtio_remap_pci(struct rte_pci_device *pci_dev, struct virtio_hw *hw) 15066d890f8aSYuanhan Liu { 15076d890f8aSYuanhan Liu if (hw->modern) { 15086d890f8aSYuanhan Liu /* 15096d890f8aSYuanhan Liu * We don't have to re-parse the PCI config space, since 15103dcfe039SThomas Monjalon * rte_pci_map_device() makes sure the mapped address 15116d890f8aSYuanhan Liu * in secondary process would equal to the one mapped in 15126d890f8aSYuanhan Liu * the primary process: error will be returned if that 15136d890f8aSYuanhan Liu * requirement is not met. 15146d890f8aSYuanhan Liu * 15156d890f8aSYuanhan Liu * That said, we could simply reuse all cap pointers 15166d890f8aSYuanhan Liu * (such as dev_cfg, common_cfg, etc.) parsed from the 15176d890f8aSYuanhan Liu * primary process, which is stored in shared memory. 15186d890f8aSYuanhan Liu */ 15193dcfe039SThomas Monjalon if (rte_pci_map_device(pci_dev)) { 15206d890f8aSYuanhan Liu PMD_INIT_LOG(DEBUG, "failed to map pci device!"); 15216d890f8aSYuanhan Liu return -1; 15226d890f8aSYuanhan Liu } 15236d890f8aSYuanhan Liu } else { 15243dcfe039SThomas Monjalon if (rte_pci_ioport_map(pci_dev, 0, VTPCI_IO(hw)) < 0) 15256d890f8aSYuanhan Liu return -1; 15266d890f8aSYuanhan Liu } 15276d890f8aSYuanhan Liu 15286d890f8aSYuanhan Liu return 0; 15296d890f8aSYuanhan Liu } 15306d890f8aSYuanhan Liu 15316d890f8aSYuanhan Liu static void 15326d890f8aSYuanhan Liu virtio_set_vtpci_ops(struct virtio_hw *hw) 15336d890f8aSYuanhan Liu { 1534e754c959SThomas Monjalon #ifdef RTE_VIRTIO_USER 15356d890f8aSYuanhan Liu if (hw->virtio_user_dev) 15366d890f8aSYuanhan Liu VTPCI_OPS(hw) = &virtio_user_ops; 1537e754c959SThomas Monjalon else 1538e754c959SThomas Monjalon #endif 1539e754c959SThomas Monjalon if (hw->modern) 15406d890f8aSYuanhan Liu VTPCI_OPS(hw) = &modern_ops; 15416d890f8aSYuanhan Liu else 15426d890f8aSYuanhan Liu VTPCI_OPS(hw) = &legacy_ops; 15436d890f8aSYuanhan Liu } 15446d890f8aSYuanhan Liu 15456d890f8aSYuanhan Liu /* 1546198ab336SOlivier Matz * This function is based on probe() function in virtio_pci.c 1547198ab336SOlivier Matz * It returns 0 on success. 1548198ab336SOlivier Matz */ 1549198ab336SOlivier Matz int 1550198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev) 1551198ab336SOlivier Matz { 1552198ab336SOlivier Matz struct virtio_hw *hw = eth_dev->data->dev_private; 1553198ab336SOlivier Matz int ret; 1554198ab336SOlivier Matz 1555198ab336SOlivier Matz RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf)); 1556198ab336SOlivier Matz 1557198ab336SOlivier Matz eth_dev->dev_ops = &virtio_eth_dev_ops; 1558198ab336SOlivier Matz 1559198ab336SOlivier Matz if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 15606d890f8aSYuanhan Liu if (!hw->virtio_user_dev) { 1561c0802544SFerruh Yigit ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), hw); 15626d890f8aSYuanhan Liu if (ret) 15636d890f8aSYuanhan Liu return ret; 15646d890f8aSYuanhan Liu } 15656d890f8aSYuanhan Liu 15666d890f8aSYuanhan Liu virtio_set_vtpci_ops(hw); 15674819eae8SOlivier Matz set_rxtx_funcs(eth_dev); 15684819eae8SOlivier Matz 1569198ab336SOlivier Matz return 0; 1570198ab336SOlivier Matz } 1571198ab336SOlivier Matz 1572198ab336SOlivier Matz /* Allocate memory for storing MAC addresses */ 1573198ab336SOlivier Matz eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0); 1574198ab336SOlivier Matz if (eth_dev->data->mac_addrs == NULL) { 1575198ab336SOlivier Matz PMD_INIT_LOG(ERR, 1576198ab336SOlivier Matz "Failed to allocate %d bytes needed to store MAC addresses", 1577198ab336SOlivier Matz VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN); 1578198ab336SOlivier Matz return -ENOMEM; 1579198ab336SOlivier Matz } 1580198ab336SOlivier Matz 158176873125SYuanhan Liu hw->port_id = eth_dev->data->port_id; 15826a7c0dfcSJan Blunck /* For virtio_user case the hw->virtio_user_dev is populated by 15836a7c0dfcSJan Blunck * virtio_user_eth_dev_alloc() before eth_virtio_dev_init() is called. 15846a7c0dfcSJan Blunck */ 15856a7c0dfcSJan Blunck if (!hw->virtio_user_dev) { 1586c0802544SFerruh Yigit ret = vtpci_init(RTE_ETH_DEV_TO_PCI(eth_dev), hw); 1587198ab336SOlivier Matz if (ret) 1588198ab336SOlivier Matz return ret; 1589198ab336SOlivier Matz } 1590198ab336SOlivier Matz 159160e6f470SOlivier Matz /* reset device and negotiate default features */ 159296cb6711SOlivier Matz ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES); 1593198ab336SOlivier Matz if (ret < 0) 1594198ab336SOlivier Matz return ret; 1595198ab336SOlivier Matz 15966c3169a3SBruce Richardson /* Setup interrupt callback */ 159762a785a6SJianfeng Tan if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 159861e3ee17SYuanhan Liu rte_intr_callback_register(eth_dev->intr_handle, 15996c3169a3SBruce Richardson virtio_interrupt_handler, eth_dev); 16006c3169a3SBruce Richardson 16016c3169a3SBruce Richardson return 0; 16026c3169a3SBruce Richardson } 16036c3169a3SBruce Richardson 1604abf4c84bSBernard Iremonger static int 1605abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev) 1606abf4c84bSBernard Iremonger { 1607abf4c84bSBernard Iremonger PMD_INIT_FUNC_TRACE(); 1608abf4c84bSBernard Iremonger 1609abf4c84bSBernard Iremonger if (rte_eal_process_type() == RTE_PROC_SECONDARY) 1610abf4c84bSBernard Iremonger return -EPERM; 1611abf4c84bSBernard Iremonger 16123f3b3d54SYuanhan Liu virtio_dev_stop(eth_dev); 1613abf4c84bSBernard Iremonger virtio_dev_close(eth_dev); 1614abf4c84bSBernard Iremonger 1615abf4c84bSBernard Iremonger eth_dev->dev_ops = NULL; 1616abf4c84bSBernard Iremonger eth_dev->tx_pkt_burst = NULL; 1617abf4c84bSBernard Iremonger eth_dev->rx_pkt_burst = NULL; 1618abf4c84bSBernard Iremonger 1619abf4c84bSBernard Iremonger rte_free(eth_dev->data->mac_addrs); 1620abf4c84bSBernard Iremonger eth_dev->data->mac_addrs = NULL; 1621abf4c84bSBernard Iremonger 1622abf4c84bSBernard Iremonger /* reset interrupt callback */ 162362a785a6SJianfeng Tan if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 162461e3ee17SYuanhan Liu rte_intr_callback_unregister(eth_dev->intr_handle, 1625abf4c84bSBernard Iremonger virtio_interrupt_handler, 1626abf4c84bSBernard Iremonger eth_dev); 16279470427cSYuanhan Liu if (eth_dev->device) 1628c0802544SFerruh Yigit rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(eth_dev)); 1629abf4c84bSBernard Iremonger 1630abf4c84bSBernard Iremonger PMD_INIT_LOG(DEBUG, "dev_uninit completed"); 1631abf4c84bSBernard Iremonger 1632abf4c84bSBernard Iremonger return 0; 1633abf4c84bSBernard Iremonger } 1634abf4c84bSBernard Iremonger 1635fdf91e0fSJan Blunck static int eth_virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, 1636fdf91e0fSJan Blunck struct rte_pci_device *pci_dev) 1637fdf91e0fSJan Blunck { 1638fdf91e0fSJan Blunck return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct virtio_hw), 1639fdf91e0fSJan Blunck eth_virtio_dev_init); 1640fdf91e0fSJan Blunck } 1641fdf91e0fSJan Blunck 1642fdf91e0fSJan Blunck static int eth_virtio_pci_remove(struct rte_pci_device *pci_dev) 1643fdf91e0fSJan Blunck { 1644fdf91e0fSJan Blunck return rte_eth_dev_pci_generic_remove(pci_dev, eth_virtio_dev_uninit); 1645fdf91e0fSJan Blunck } 1646fdf91e0fSJan Blunck 1647fdf91e0fSJan Blunck static struct rte_pci_driver rte_virtio_pmd = { 1648cd433ce5SDavid Marchand .driver = { 1649cd433ce5SDavid Marchand .name = "net_virtio", 1650cd433ce5SDavid Marchand }, 16516c3169a3SBruce Richardson .id_table = pci_id_virtio_map, 165222dda618SBen Walker .drv_flags = 0, 1653fdf91e0fSJan Blunck .probe = eth_virtio_pci_probe, 1654fdf91e0fSJan Blunck .remove = eth_virtio_pci_remove, 16556c3169a3SBruce Richardson }; 16566c3169a3SBruce Richardson 1657c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init); 1658c830cb29SDavid Marchand static void 1659c830cb29SDavid Marchand rte_virtio_pmd_init(void) 16606c3169a3SBruce Richardson { 16616c3169a3SBruce Richardson if (rte_eal_iopl_init() != 0) { 16626c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD"); 1663c830cb29SDavid Marchand return; 16646c3169a3SBruce Richardson } 16656c3169a3SBruce Richardson 16663dcfe039SThomas Monjalon rte_pci_register(&rte_virtio_pmd); 16676c3169a3SBruce Richardson } 16686c3169a3SBruce Richardson 16696c3169a3SBruce Richardson /* 16706c3169a3SBruce Richardson * Configure virtio device 16716c3169a3SBruce Richardson * It returns 0 on success. 16726c3169a3SBruce Richardson */ 16736c3169a3SBruce Richardson static int 16746c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev) 16756c3169a3SBruce Richardson { 16766c3169a3SBruce Richardson const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode; 16776c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 1678ec9f3d12SOlivier Matz uint64_t req_features; 1679e7b9d1d2SJiayu Hu int ret; 16806c3169a3SBruce Richardson 16816c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "configure"); 1682ec9f3d12SOlivier Matz req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES; 16834dab342bSStephen Hemminger 1684e7b9d1d2SJiayu Hu if (dev->data->dev_conf.intr_conf.rxq) { 1685e7b9d1d2SJiayu Hu ret = virtio_init_device(dev, hw->req_guest_features); 1686e7b9d1d2SJiayu Hu if (ret < 0) 1687e7b9d1d2SJiayu Hu return ret; 1688e7b9d1d2SJiayu Hu } 1689e7b9d1d2SJiayu Hu 1690d67d86ceSOlivier Matz /* The name hw_ip_checksum is a bit confusing since it can be 1691d67d86ceSOlivier Matz * set by the application to request L3 and/or L4 checksums. In 1692d67d86ceSOlivier Matz * case of virtio, only L4 checksum is supported. 1693d67d86ceSOlivier Matz */ 1694d67d86ceSOlivier Matz if (rxmode->hw_ip_checksum) 1695d67d86ceSOlivier Matz req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM); 1696d67d86ceSOlivier Matz 1697ec9f3d12SOlivier Matz if (rxmode->enable_lro) 1698ec9f3d12SOlivier Matz req_features |= 1699ec9f3d12SOlivier Matz (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 1700ec9f3d12SOlivier Matz (1ULL << VIRTIO_NET_F_GUEST_TSO6); 17016c3169a3SBruce Richardson 1702ec9f3d12SOlivier Matz /* if request features changed, reinit the device */ 1703ec9f3d12SOlivier Matz if (req_features != hw->req_guest_features) { 1704ec9f3d12SOlivier Matz ret = virtio_init_device(dev, req_features); 1705ec9f3d12SOlivier Matz if (ret < 0) 1706ec9f3d12SOlivier Matz return ret; 1707ec9f3d12SOlivier Matz } 1708ec9f3d12SOlivier Matz 1709d67d86ceSOlivier Matz if (rxmode->hw_ip_checksum && 1710d67d86ceSOlivier Matz !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) { 171178fd97c3SOlivier Matz PMD_DRV_LOG(ERR, 1712d67d86ceSOlivier Matz "rx checksum not available on this host"); 1713d67d86ceSOlivier Matz return -ENOTSUP; 1714d67d86ceSOlivier Matz } 1715d67d86ceSOlivier Matz 1716ec9f3d12SOlivier Matz if (rxmode->enable_lro && 1717ec9f3d12SOlivier Matz (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) || 1718ec9f3d12SOlivier Matz !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) { 171978fd97c3SOlivier Matz PMD_DRV_LOG(ERR, 1720ec9f3d12SOlivier Matz "Large Receive Offload not available on this host"); 172186d59b21SOlivier Matz return -ENOTSUP; 172286d59b21SOlivier Matz } 172386d59b21SOlivier Matz 172469c80d4eSYuanhan Liu /* start control queue */ 172569c80d4eSYuanhan Liu if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) 172645e4acd4SOlivier Matz virtio_dev_cq_start(dev); 172745e4acd4SOlivier Matz 17286c3169a3SBruce Richardson hw->vlan_strip = rxmode->hw_vlan_strip; 17296c3169a3SBruce Richardson 17306c3169a3SBruce Richardson if (rxmode->hw_vlan_filter 17316c3169a3SBruce Richardson && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) { 173278fd97c3SOlivier Matz PMD_DRV_LOG(ERR, 17336c3169a3SBruce Richardson "vlan filtering not available on this host"); 17346c3169a3SBruce Richardson return -ENOTSUP; 17356c3169a3SBruce Richardson } 17366c3169a3SBruce Richardson 173762a785a6SJianfeng Tan if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 1738981e61f5SJianfeng Tan /* Enable vector (0) for Link State Intrerrupt */ 1739981e61f5SJianfeng Tan if (VTPCI_OPS(hw)->set_config_irq(hw, 0) == 1740981e61f5SJianfeng Tan VIRTIO_MSI_NO_VECTOR) { 17416c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "failed to set config vector"); 17426c3169a3SBruce Richardson return -EBUSY; 17436c3169a3SBruce Richardson } 17446c3169a3SBruce Richardson 17454819eae8SOlivier Matz hw->use_simple_rxtx = 1; 17464819eae8SOlivier Matz 1747*02dd0e21SOlivier Matz #if defined RTE_ARCH_ARM64 || defined CONFIG_RTE_ARCH_ARM 17484819eae8SOlivier Matz if (!rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON)) 17494819eae8SOlivier Matz hw->use_simple_rxtx = 0; 17504819eae8SOlivier Matz #endif 17514819eae8SOlivier Matz if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) 17524819eae8SOlivier Matz hw->use_simple_rxtx = 0; 17534819eae8SOlivier Matz 17546c3169a3SBruce Richardson return 0; 17556c3169a3SBruce Richardson } 17566c3169a3SBruce Richardson 17576c3169a3SBruce Richardson 17586c3169a3SBruce Richardson static int 17596c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev) 17606c3169a3SBruce Richardson { 17616c3169a3SBruce Richardson uint16_t nb_queues, i; 176201ad44fdSHuawei Xie struct virtnet_rx *rxvq; 176301ad44fdSHuawei Xie struct virtnet_tx *txvq __rte_unused; 1764663c7679SYuanhan Liu struct virtio_hw *hw = dev->data->dev_private; 1765efc83a1eSOlivier Matz int ret; 1766efc83a1eSOlivier Matz 1767efc83a1eSOlivier Matz /* Finish the initialization of the queues */ 1768efc83a1eSOlivier Matz for (i = 0; i < dev->data->nb_rx_queues; i++) { 1769efc83a1eSOlivier Matz ret = virtio_dev_rx_queue_setup_finish(dev, i); 1770efc83a1eSOlivier Matz if (ret < 0) 1771efc83a1eSOlivier Matz return ret; 1772efc83a1eSOlivier Matz } 1773efc83a1eSOlivier Matz for (i = 0; i < dev->data->nb_tx_queues; i++) { 1774efc83a1eSOlivier Matz ret = virtio_dev_tx_queue_setup_finish(dev, i); 1775efc83a1eSOlivier Matz if (ret < 0) 1776efc83a1eSOlivier Matz return ret; 1777efc83a1eSOlivier Matz } 17786c3169a3SBruce Richardson 17796c3169a3SBruce Richardson /* check if lsc interrupt feature is enabled */ 1780954ea115SStephen Hemminger if (dev->data->dev_conf.intr_conf.lsc) { 178162a785a6SJianfeng Tan if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) { 17826c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "link status not supported by host"); 17836c3169a3SBruce Richardson return -ENOTSUP; 17846c3169a3SBruce Richardson } 1785349a447bSJianfeng Tan } 1786349a447bSJianfeng Tan 1787349a447bSJianfeng Tan /* Enable uio/vfio intr/eventfd mapping: althrough we already did that 1788349a447bSJianfeng Tan * in device configure, but it could be unmapped when device is 1789349a447bSJianfeng Tan * stopped. 1790349a447bSJianfeng Tan */ 1791349a447bSJianfeng Tan if (dev->data->dev_conf.intr_conf.lsc || 1792349a447bSJianfeng Tan dev->data->dev_conf.intr_conf.rxq) { 1793349a447bSJianfeng Tan rte_intr_disable(dev->intr_handle); 17946c3169a3SBruce Richardson 179561e3ee17SYuanhan Liu if (rte_intr_enable(dev->intr_handle) < 0) { 17966c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "interrupt enable failed"); 17976c3169a3SBruce Richardson return -EIO; 17986c3169a3SBruce Richardson } 17996c3169a3SBruce Richardson } 18006c3169a3SBruce Richardson 18016c3169a3SBruce Richardson /*Notify the backend 18026c3169a3SBruce Richardson *Otherwise the tap backend might already stop its queue due to fullness. 18036c3169a3SBruce Richardson *vhost backend will have no chance to be waked up 18046c3169a3SBruce Richardson */ 180560d4a353SYuanhan Liu nb_queues = RTE_MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues); 1806663c7679SYuanhan Liu if (hw->max_queue_pairs > 1) { 18076c3169a3SBruce Richardson if (virtio_set_multiple_queues(dev, nb_queues) != 0) 18086c3169a3SBruce Richardson return -EINVAL; 18096c3169a3SBruce Richardson } 18106c3169a3SBruce Richardson 18116c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues); 18126c3169a3SBruce Richardson 181360d4a353SYuanhan Liu for (i = 0; i < dev->data->nb_rx_queues; i++) { 181401ad44fdSHuawei Xie rxvq = dev->data->rx_queues[i]; 181501ad44fdSHuawei Xie virtqueue_notify(rxvq->vq); 181601ad44fdSHuawei Xie } 18176c3169a3SBruce Richardson 1818924da8f1SSteven Luong for (i = 0; i < dev->data->nb_tx_queues; i++) { 1819924da8f1SSteven Luong txvq = dev->data->tx_queues[i]; 1820924da8f1SSteven Luong virtqueue_notify(txvq->vq); 1821924da8f1SSteven Luong } 1822924da8f1SSteven Luong 18236c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Notified backend at initialization"); 18246c3169a3SBruce Richardson 182501ad44fdSHuawei Xie for (i = 0; i < dev->data->nb_rx_queues; i++) { 182601ad44fdSHuawei Xie rxvq = dev->data->rx_queues[i]; 182701ad44fdSHuawei Xie VIRTQUEUE_DUMP(rxvq->vq); 182801ad44fdSHuawei Xie } 18296c3169a3SBruce Richardson 183001ad44fdSHuawei Xie for (i = 0; i < dev->data->nb_tx_queues; i++) { 183101ad44fdSHuawei Xie txvq = dev->data->tx_queues[i]; 183201ad44fdSHuawei Xie VIRTQUEUE_DUMP(txvq->vq); 183301ad44fdSHuawei Xie } 183458d9fe40SJianfeng Tan 18354819eae8SOlivier Matz set_rxtx_funcs(dev); 1836aa9f0606SYuanhan Liu hw->started = 1; 18376c3169a3SBruce Richardson 183858d9fe40SJianfeng Tan /* Initialize Link state */ 183958d9fe40SJianfeng Tan virtio_dev_link_update(dev, 0); 184058d9fe40SJianfeng Tan 18416c3169a3SBruce Richardson return 0; 18426c3169a3SBruce Richardson } 18436c3169a3SBruce Richardson 18446c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev) 18456c3169a3SBruce Richardson { 18466c3169a3SBruce Richardson struct rte_mbuf *buf; 18476c3169a3SBruce Richardson int i, mbuf_num = 0; 18486c3169a3SBruce Richardson 18496c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_rx_queues; i++) { 185001ad44fdSHuawei Xie struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 185101ad44fdSHuawei Xie 18526c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 18536c3169a3SBruce Richardson "Before freeing rxq[%d] used and unused buf", i); 185401ad44fdSHuawei Xie VIRTQUEUE_DUMP(rxvq->vq); 18556c3169a3SBruce Richardson 185601ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq); 185701ad44fdSHuawei Xie while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) { 18586c3169a3SBruce Richardson rte_pktmbuf_free(buf); 18596c3169a3SBruce Richardson mbuf_num++; 18606c3169a3SBruce Richardson } 18616c3169a3SBruce Richardson 18626c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num); 18636c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 18646c3169a3SBruce Richardson "After freeing rxq[%d] used and unused buf", i); 186501ad44fdSHuawei Xie VIRTQUEUE_DUMP(rxvq->vq); 18666c3169a3SBruce Richardson } 18676c3169a3SBruce Richardson 18686c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_tx_queues; i++) { 186901ad44fdSHuawei Xie struct virtnet_tx *txvq = dev->data->tx_queues[i]; 187001ad44fdSHuawei Xie 18716c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 18726c3169a3SBruce Richardson "Before freeing txq[%d] used and unused bufs", 18736c3169a3SBruce Richardson i); 187401ad44fdSHuawei Xie VIRTQUEUE_DUMP(txvq->vq); 18756c3169a3SBruce Richardson 18766c3169a3SBruce Richardson mbuf_num = 0; 187701ad44fdSHuawei Xie while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) { 18786c3169a3SBruce Richardson rte_pktmbuf_free(buf); 18796c3169a3SBruce Richardson mbuf_num++; 18806c3169a3SBruce Richardson } 18816c3169a3SBruce Richardson 18826c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num); 18836c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 18846c3169a3SBruce Richardson "After freeing txq[%d] used and unused buf", i); 188501ad44fdSHuawei Xie VIRTQUEUE_DUMP(txvq->vq); 18866c3169a3SBruce Richardson } 18876c3169a3SBruce Richardson } 18886c3169a3SBruce Richardson 18896c3169a3SBruce Richardson /* 18906c3169a3SBruce Richardson * Stop device: disable interrupt and mark link down 18916c3169a3SBruce Richardson */ 18926c3169a3SBruce Richardson static void 18936c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev) 18946c3169a3SBruce Richardson { 1895aa9f0606SYuanhan Liu struct virtio_hw *hw = dev->data->dev_private; 18966c3169a3SBruce Richardson struct rte_eth_link link; 1897349a447bSJianfeng Tan struct rte_intr_conf *intr_conf = &dev->data->dev_conf.intr_conf; 18986c3169a3SBruce Richardson 18996c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "stop"); 19006c3169a3SBruce Richardson 1901349a447bSJianfeng Tan if (intr_conf->lsc || intr_conf->rxq) 190261e3ee17SYuanhan Liu rte_intr_disable(dev->intr_handle); 19036c3169a3SBruce Richardson 1904aa9f0606SYuanhan Liu hw->started = 0; 19056c3169a3SBruce Richardson memset(&link, 0, sizeof(link)); 19066c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(dev, &link); 19076c3169a3SBruce Richardson } 19086c3169a3SBruce Richardson 19096c3169a3SBruce Richardson static int 19106c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete) 19116c3169a3SBruce Richardson { 19126c3169a3SBruce Richardson struct rte_eth_link link, old; 19136c3169a3SBruce Richardson uint16_t status; 19146c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 19156c3169a3SBruce Richardson memset(&link, 0, sizeof(link)); 19166c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(dev, &link); 19176c3169a3SBruce Richardson old = link; 191811319000SMarc Sune link.link_duplex = ETH_LINK_FULL_DUPLEX; 19196c3169a3SBruce Richardson link.link_speed = SPEED_10G; 19206c3169a3SBruce Richardson 1921aa9f0606SYuanhan Liu if (hw->started == 0) { 1922aa9f0606SYuanhan Liu link.link_status = ETH_LINK_DOWN; 1923aa9f0606SYuanhan Liu } else if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 19246c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Get link status from hw"); 19256c3169a3SBruce Richardson vtpci_read_dev_config(hw, 19266c3169a3SBruce Richardson offsetof(struct virtio_net_config, status), 19276c3169a3SBruce Richardson &status, sizeof(status)); 19286c3169a3SBruce Richardson if ((status & VIRTIO_NET_S_LINK_UP) == 0) { 192909419f23SThomas Monjalon link.link_status = ETH_LINK_DOWN; 19306c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Port %d is down", 19316c3169a3SBruce Richardson dev->data->port_id); 19326c3169a3SBruce Richardson } else { 193309419f23SThomas Monjalon link.link_status = ETH_LINK_UP; 19346c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Port %d is up", 19356c3169a3SBruce Richardson dev->data->port_id); 19366c3169a3SBruce Richardson } 19376c3169a3SBruce Richardson } else { 193809419f23SThomas Monjalon link.link_status = ETH_LINK_UP; 19396c3169a3SBruce Richardson } 19406c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(dev, &link); 19416c3169a3SBruce Richardson 19426c3169a3SBruce Richardson return (old.link_status == link.link_status) ? -1 : 0; 19436c3169a3SBruce Richardson } 19446c3169a3SBruce Richardson 19456c3169a3SBruce Richardson static void 19466c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 19476c3169a3SBruce Richardson { 1948c1e55ed3SOlivier Matz uint64_t tso_mask, host_features; 19496c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 19506c3169a3SBruce Richardson 1951b392e987SIdo Barnea dev_info->speed_capa = ETH_LINK_SPEED_10G; /* fake value */ 1952b392e987SIdo Barnea 1953c0802544SFerruh Yigit dev_info->pci_dev = dev->device ? RTE_ETH_DEV_TO_PCI(dev) : NULL; 195445e4acd4SOlivier Matz dev_info->max_rx_queues = 195545e4acd4SOlivier Matz RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES); 195645e4acd4SOlivier Matz dev_info->max_tx_queues = 195745e4acd4SOlivier Matz RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES); 19586c3169a3SBruce Richardson dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE; 19596c3169a3SBruce Richardson dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN; 19606c3169a3SBruce Richardson dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS; 19616c3169a3SBruce Richardson dev_info->default_txconf = (struct rte_eth_txconf) { 19626c3169a3SBruce Richardson .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS 19636c3169a3SBruce Richardson }; 196458169a9cSOlivier Matz 1965c1e55ed3SOlivier Matz host_features = VTPCI_OPS(hw)->get_features(hw); 1966c1e55ed3SOlivier Matz dev_info->rx_offload_capa = 0; 1967c1e55ed3SOlivier Matz if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) { 1968c1e55ed3SOlivier Matz dev_info->rx_offload_capa |= 1969c1e55ed3SOlivier Matz DEV_RX_OFFLOAD_TCP_CKSUM | 1970c1e55ed3SOlivier Matz DEV_RX_OFFLOAD_UDP_CKSUM; 1971c1e55ed3SOlivier Matz } 1972c1e55ed3SOlivier Matz tso_mask = (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 1973c1e55ed3SOlivier Matz (1ULL << VIRTIO_NET_F_GUEST_TSO6); 1974ec9f3d12SOlivier Matz if ((host_features & tso_mask) == tso_mask) 1975ec9f3d12SOlivier Matz dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TCP_LRO; 1976c1e55ed3SOlivier Matz 1977c1e55ed3SOlivier Matz dev_info->tx_offload_capa = 0; 197858169a9cSOlivier Matz if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) { 197958169a9cSOlivier Matz dev_info->tx_offload_capa |= 198058169a9cSOlivier Matz DEV_TX_OFFLOAD_UDP_CKSUM | 198158169a9cSOlivier Matz DEV_TX_OFFLOAD_TCP_CKSUM; 198258169a9cSOlivier Matz } 198369657304SOlivier Matz tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) | 198469657304SOlivier Matz (1ULL << VIRTIO_NET_F_HOST_TSO6); 198569657304SOlivier Matz if ((hw->guest_features & tso_mask) == tso_mask) 198669657304SOlivier Matz dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO; 19876c3169a3SBruce Richardson } 19886c3169a3SBruce Richardson 19896c3169a3SBruce Richardson /* 19906c3169a3SBruce Richardson * It enables testpmd to collect per queue stats. 19916c3169a3SBruce Richardson */ 19926c3169a3SBruce Richardson static int 19936c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev, 19946c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx, 19956c3169a3SBruce Richardson __rte_unused uint8_t is_rx) 19966c3169a3SBruce Richardson { 19976c3169a3SBruce Richardson return 0; 19986c3169a3SBruce Richardson } 19996c3169a3SBruce Richardson 200001f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__); 200101f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map); 200206e81dc9SDavid Marchand RTE_PMD_REGISTER_KMOD_DEP(net_virtio, "* igb_uio | uio_pci_generic | vfio-pci"); 2003