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> 416c3169a3SBruce Richardson #include <rte_memcpy.h> 426c3169a3SBruce Richardson #include <rte_string_fns.h> 436c3169a3SBruce Richardson #include <rte_memzone.h> 446c3169a3SBruce Richardson #include <rte_malloc.h> 456c3169a3SBruce Richardson #include <rte_atomic.h> 466c3169a3SBruce Richardson #include <rte_branch_prediction.h> 476c3169a3SBruce Richardson #include <rte_pci.h> 486c3169a3SBruce Richardson #include <rte_ether.h> 496c3169a3SBruce Richardson #include <rte_common.h> 50abf4c84bSBernard Iremonger #include <rte_errno.h> 516c3169a3SBruce Richardson 526c3169a3SBruce Richardson #include <rte_memory.h> 536c3169a3SBruce Richardson #include <rte_eal.h> 546c3169a3SBruce Richardson #include <rte_dev.h> 556c3169a3SBruce Richardson 566c3169a3SBruce Richardson #include "virtio_ethdev.h" 576c3169a3SBruce Richardson #include "virtio_pci.h" 586c3169a3SBruce Richardson #include "virtio_logs.h" 596c3169a3SBruce Richardson #include "virtqueue.h" 60cab04612SHuawei Xie #include "virtio_rxtx.h" 616c3169a3SBruce Richardson 62abf4c84bSBernard Iremonger static int eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev); 636c3169a3SBruce Richardson static int virtio_dev_configure(struct rte_eth_dev *dev); 646c3169a3SBruce Richardson static int virtio_dev_start(struct rte_eth_dev *dev); 656c3169a3SBruce Richardson static void virtio_dev_stop(struct rte_eth_dev *dev); 666c3169a3SBruce Richardson static void virtio_dev_promiscuous_enable(struct rte_eth_dev *dev); 676c3169a3SBruce Richardson static void virtio_dev_promiscuous_disable(struct rte_eth_dev *dev); 686c3169a3SBruce Richardson static void virtio_dev_allmulticast_enable(struct rte_eth_dev *dev); 696c3169a3SBruce Richardson static void virtio_dev_allmulticast_disable(struct rte_eth_dev *dev); 706c3169a3SBruce Richardson static void virtio_dev_info_get(struct rte_eth_dev *dev, 716c3169a3SBruce Richardson struct rte_eth_dev_info *dev_info); 726c3169a3SBruce Richardson static int virtio_dev_link_update(struct rte_eth_dev *dev, 736c3169a3SBruce Richardson __rte_unused int wait_to_complete); 746c3169a3SBruce Richardson 756c3169a3SBruce Richardson static void virtio_set_hwaddr(struct virtio_hw *hw); 766c3169a3SBruce Richardson static void virtio_get_hwaddr(struct virtio_hw *hw); 776c3169a3SBruce Richardson 7876d4c652SHarry van Haaren static void virtio_dev_stats_get(struct rte_eth_dev *dev, 7976d4c652SHarry van Haaren struct rte_eth_stats *stats); 8076d4c652SHarry van Haaren static int virtio_dev_xstats_get(struct rte_eth_dev *dev, 81e2aae1c1SRemy Horton struct rte_eth_xstat *xstats, unsigned n); 82baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev, 83baf91c39SRemy Horton struct rte_eth_xstat_name *xstats_names, 84baf91c39SRemy Horton unsigned limit); 856c3169a3SBruce Richardson static void virtio_dev_stats_reset(struct rte_eth_dev *dev); 866c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev); 876c3169a3SBruce Richardson static int virtio_vlan_filter_set(struct rte_eth_dev *dev, 886c3169a3SBruce Richardson uint16_t vlan_id, int on); 896c3169a3SBruce Richardson static void virtio_mac_addr_add(struct rte_eth_dev *dev, 906c3169a3SBruce Richardson struct ether_addr *mac_addr, 916c3169a3SBruce Richardson uint32_t index, uint32_t vmdq __rte_unused); 926c3169a3SBruce Richardson static void virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index); 936c3169a3SBruce Richardson static void virtio_mac_addr_set(struct rte_eth_dev *dev, 946c3169a3SBruce Richardson struct ether_addr *mac_addr); 956c3169a3SBruce Richardson 966c3169a3SBruce Richardson static int virtio_dev_queue_stats_mapping_set( 976c3169a3SBruce Richardson __rte_unused struct rte_eth_dev *eth_dev, 986c3169a3SBruce Richardson __rte_unused uint16_t queue_id, 996c3169a3SBruce Richardson __rte_unused uint8_t stat_idx, 1006c3169a3SBruce Richardson __rte_unused uint8_t is_rx); 1016c3169a3SBruce Richardson 1026c3169a3SBruce Richardson /* 1036c3169a3SBruce Richardson * The set of PCI devices this driver supports 1046c3169a3SBruce Richardson */ 1056c3169a3SBruce Richardson static const struct rte_pci_id pci_id_virtio_map[] = { 1064c790365SJason Wang { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_LEGACY_DEVICEID_NET) }, 1074c790365SJason Wang { RTE_PCI_DEVICE(VIRTIO_PCI_VENDORID, VIRTIO_PCI_MODERN_DEVICEID_NET) }, 1086c3169a3SBruce Richardson { .vendor_id = 0, /* sentinel */ }, 1096c3169a3SBruce Richardson }; 1106c3169a3SBruce Richardson 11176d4c652SHarry van Haaren struct rte_virtio_xstats_name_off { 11276d4c652SHarry van Haaren char name[RTE_ETH_XSTATS_NAME_SIZE]; 11376d4c652SHarry van Haaren unsigned offset; 11476d4c652SHarry van Haaren }; 11576d4c652SHarry van Haaren 11676d4c652SHarry van Haaren /* [rt]x_qX_ is prepended to the name string here */ 11701ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_rxq_stat_strings[] = { 11801ad44fdSHuawei Xie {"good_packets", offsetof(struct virtnet_rx, stats.packets)}, 11901ad44fdSHuawei Xie {"good_bytes", offsetof(struct virtnet_rx, stats.bytes)}, 12001ad44fdSHuawei Xie {"errors", offsetof(struct virtnet_rx, stats.errors)}, 12101ad44fdSHuawei Xie {"multicast_packets", offsetof(struct virtnet_rx, stats.multicast)}, 12201ad44fdSHuawei Xie {"broadcast_packets", offsetof(struct virtnet_rx, stats.broadcast)}, 12301ad44fdSHuawei Xie {"undersize_packets", offsetof(struct virtnet_rx, stats.size_bins[0])}, 12401ad44fdSHuawei Xie {"size_64_packets", offsetof(struct virtnet_rx, stats.size_bins[1])}, 12501ad44fdSHuawei Xie {"size_65_127_packets", offsetof(struct virtnet_rx, stats.size_bins[2])}, 12601ad44fdSHuawei Xie {"size_128_255_packets", offsetof(struct virtnet_rx, stats.size_bins[3])}, 12701ad44fdSHuawei Xie {"size_256_511_packets", offsetof(struct virtnet_rx, stats.size_bins[4])}, 12801ad44fdSHuawei Xie {"size_512_1023_packets", offsetof(struct virtnet_rx, stats.size_bins[5])}, 12981f7234bSZhiyong Yang {"size_1024_1518_packets", offsetof(struct virtnet_rx, stats.size_bins[6])}, 13081f7234bSZhiyong Yang {"size_1519_max_packets", offsetof(struct virtnet_rx, stats.size_bins[7])}, 13176d4c652SHarry van Haaren }; 13276d4c652SHarry van Haaren 13301ad44fdSHuawei Xie /* [rt]x_qX_ is prepended to the name string here */ 13401ad44fdSHuawei Xie static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = { 13501ad44fdSHuawei Xie {"good_packets", offsetof(struct virtnet_tx, stats.packets)}, 13601ad44fdSHuawei Xie {"good_bytes", offsetof(struct virtnet_tx, stats.bytes)}, 13701ad44fdSHuawei Xie {"errors", offsetof(struct virtnet_tx, stats.errors)}, 13801ad44fdSHuawei Xie {"multicast_packets", offsetof(struct virtnet_tx, stats.multicast)}, 13901ad44fdSHuawei Xie {"broadcast_packets", offsetof(struct virtnet_tx, stats.broadcast)}, 14001ad44fdSHuawei Xie {"undersize_packets", offsetof(struct virtnet_tx, stats.size_bins[0])}, 14101ad44fdSHuawei Xie {"size_64_packets", offsetof(struct virtnet_tx, stats.size_bins[1])}, 14201ad44fdSHuawei Xie {"size_65_127_packets", offsetof(struct virtnet_tx, stats.size_bins[2])}, 14301ad44fdSHuawei Xie {"size_128_255_packets", offsetof(struct virtnet_tx, stats.size_bins[3])}, 14401ad44fdSHuawei Xie {"size_256_511_packets", offsetof(struct virtnet_tx, stats.size_bins[4])}, 14501ad44fdSHuawei Xie {"size_512_1023_packets", offsetof(struct virtnet_tx, stats.size_bins[5])}, 14681f7234bSZhiyong Yang {"size_1024_1518_packets", offsetof(struct virtnet_tx, stats.size_bins[6])}, 14781f7234bSZhiyong Yang {"size_1519_max_packets", offsetof(struct virtnet_tx, stats.size_bins[7])}, 14801ad44fdSHuawei Xie }; 14901ad44fdSHuawei Xie 15001ad44fdSHuawei Xie #define VIRTIO_NB_RXQ_XSTATS (sizeof(rte_virtio_rxq_stat_strings) / \ 15101ad44fdSHuawei Xie sizeof(rte_virtio_rxq_stat_strings[0])) 15201ad44fdSHuawei Xie #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \ 15301ad44fdSHuawei Xie sizeof(rte_virtio_txq_stat_strings[0])) 15476d4c652SHarry van Haaren 1556c3169a3SBruce Richardson static int 15601ad44fdSHuawei Xie virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl, 1576c3169a3SBruce Richardson int *dlen, int pkt_num) 1586c3169a3SBruce Richardson { 1599e71668bSDamjan Marion uint32_t head, i; 1606c3169a3SBruce Richardson int k, sum = 0; 1616c3169a3SBruce Richardson virtio_net_ctrl_ack status = ~0; 1626c3169a3SBruce Richardson struct virtio_pmd_ctrl result; 16301ad44fdSHuawei Xie struct virtqueue *vq; 1646c3169a3SBruce Richardson 1656c3169a3SBruce Richardson ctrl->status = status; 1666c3169a3SBruce Richardson 1673bd60a27SJianfeng Tan if (!cvq || !cvq->vq) { 1680bb159adSHuawei Xie PMD_INIT_LOG(ERR, "Control queue is not supported."); 1696c3169a3SBruce Richardson return -1; 1706c3169a3SBruce Richardson } 17101ad44fdSHuawei Xie vq = cvq->vq; 1729e71668bSDamjan Marion head = vq->vq_desc_head_idx; 1736c3169a3SBruce Richardson 1746c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "vq->vq_desc_head_idx = %d, status = %d, " 1756c3169a3SBruce Richardson "vq->hw->cvq = %p vq = %p", 1766c3169a3SBruce Richardson vq->vq_desc_head_idx, status, vq->hw->cvq, vq); 1776c3169a3SBruce Richardson 1786c3169a3SBruce Richardson if ((vq->vq_free_cnt < ((uint32_t)pkt_num + 2)) || (pkt_num < 1)) 1796c3169a3SBruce Richardson return -1; 1806c3169a3SBruce Richardson 18101ad44fdSHuawei Xie memcpy(cvq->virtio_net_hdr_mz->addr, ctrl, 1826c3169a3SBruce Richardson sizeof(struct virtio_pmd_ctrl)); 1836c3169a3SBruce Richardson 1846c3169a3SBruce Richardson /* 1856c3169a3SBruce Richardson * Format is enforced in qemu code: 1866c3169a3SBruce Richardson * One TX packet for header; 1876c3169a3SBruce Richardson * At least one TX packet per argument; 1886c3169a3SBruce Richardson * One RX packet for ACK. 1896c3169a3SBruce Richardson */ 1906c3169a3SBruce Richardson vq->vq_ring.desc[head].flags = VRING_DESC_F_NEXT; 191f24f8f9fSJianfeng Tan vq->vq_ring.desc[head].addr = cvq->virtio_net_hdr_mem; 1926c3169a3SBruce Richardson vq->vq_ring.desc[head].len = sizeof(struct virtio_net_ctrl_hdr); 1936c3169a3SBruce Richardson vq->vq_free_cnt--; 1946c3169a3SBruce Richardson i = vq->vq_ring.desc[head].next; 1956c3169a3SBruce Richardson 1966c3169a3SBruce Richardson for (k = 0; k < pkt_num; k++) { 1976c3169a3SBruce Richardson vq->vq_ring.desc[i].flags = VRING_DESC_F_NEXT; 198f24f8f9fSJianfeng Tan vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem 1996c3169a3SBruce Richardson + sizeof(struct virtio_net_ctrl_hdr) 2006c3169a3SBruce Richardson + sizeof(ctrl->status) + sizeof(uint8_t)*sum; 2016c3169a3SBruce Richardson vq->vq_ring.desc[i].len = dlen[k]; 2026c3169a3SBruce Richardson sum += dlen[k]; 2036c3169a3SBruce Richardson vq->vq_free_cnt--; 2046c3169a3SBruce Richardson i = vq->vq_ring.desc[i].next; 2056c3169a3SBruce Richardson } 2066c3169a3SBruce Richardson 2076c3169a3SBruce Richardson vq->vq_ring.desc[i].flags = VRING_DESC_F_WRITE; 208f24f8f9fSJianfeng Tan vq->vq_ring.desc[i].addr = cvq->virtio_net_hdr_mem 2096c3169a3SBruce Richardson + sizeof(struct virtio_net_ctrl_hdr); 2106c3169a3SBruce Richardson vq->vq_ring.desc[i].len = sizeof(ctrl->status); 2116c3169a3SBruce Richardson vq->vq_free_cnt--; 2126c3169a3SBruce Richardson 2136c3169a3SBruce Richardson vq->vq_desc_head_idx = vq->vq_ring.desc[i].next; 2146c3169a3SBruce Richardson 2156c3169a3SBruce Richardson vq_update_avail_ring(vq, head); 2166c3169a3SBruce Richardson vq_update_avail_idx(vq); 2176c3169a3SBruce Richardson 2186c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "vq->vq_queue_index = %d", vq->vq_queue_index); 2196c3169a3SBruce Richardson 2206c3169a3SBruce Richardson virtqueue_notify(vq); 2216c3169a3SBruce Richardson 2226c3169a3SBruce Richardson rte_rmb(); 223b81026f1SHuawei Xie while (VIRTQUEUE_NUSED(vq) == 0) { 2246c3169a3SBruce Richardson rte_rmb(); 2256c3169a3SBruce Richardson usleep(100); 2266c3169a3SBruce Richardson } 2276c3169a3SBruce Richardson 228b81026f1SHuawei Xie while (VIRTQUEUE_NUSED(vq)) { 2296c3169a3SBruce Richardson uint32_t idx, desc_idx, used_idx; 2306c3169a3SBruce Richardson struct vring_used_elem *uep; 2316c3169a3SBruce Richardson 2326c3169a3SBruce Richardson used_idx = (uint32_t)(vq->vq_used_cons_idx 2336c3169a3SBruce Richardson & (vq->vq_nentries - 1)); 2346c3169a3SBruce Richardson uep = &vq->vq_ring.used->ring[used_idx]; 2356c3169a3SBruce Richardson idx = (uint32_t) uep->id; 2366c3169a3SBruce Richardson desc_idx = idx; 2376c3169a3SBruce Richardson 2386c3169a3SBruce Richardson while (vq->vq_ring.desc[desc_idx].flags & VRING_DESC_F_NEXT) { 2396c3169a3SBruce Richardson desc_idx = vq->vq_ring.desc[desc_idx].next; 2406c3169a3SBruce Richardson vq->vq_free_cnt++; 2416c3169a3SBruce Richardson } 2426c3169a3SBruce Richardson 2436c3169a3SBruce Richardson vq->vq_ring.desc[desc_idx].next = vq->vq_desc_head_idx; 2446c3169a3SBruce Richardson vq->vq_desc_head_idx = idx; 2456c3169a3SBruce Richardson 2466c3169a3SBruce Richardson vq->vq_used_cons_idx++; 2476c3169a3SBruce Richardson vq->vq_free_cnt++; 2486c3169a3SBruce Richardson } 2496c3169a3SBruce Richardson 2506c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d\nvq->vq_desc_head_idx=%d", 2516c3169a3SBruce Richardson vq->vq_free_cnt, vq->vq_desc_head_idx); 2526c3169a3SBruce Richardson 25301ad44fdSHuawei Xie memcpy(&result, cvq->virtio_net_hdr_mz->addr, 2546c3169a3SBruce Richardson sizeof(struct virtio_pmd_ctrl)); 2556c3169a3SBruce Richardson 2566c3169a3SBruce Richardson return result.status; 2576c3169a3SBruce Richardson } 2586c3169a3SBruce Richardson 2596c3169a3SBruce Richardson static int 2606c3169a3SBruce Richardson virtio_set_multiple_queues(struct rte_eth_dev *dev, uint16_t nb_queues) 2616c3169a3SBruce Richardson { 2626c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 2636c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 2646c3169a3SBruce Richardson int dlen[1]; 2656c3169a3SBruce Richardson int ret; 2666c3169a3SBruce Richardson 2676c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_MQ; 2686c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET; 2696c3169a3SBruce Richardson memcpy(ctrl.data, &nb_queues, sizeof(uint16_t)); 2706c3169a3SBruce Richardson 2716c3169a3SBruce Richardson dlen[0] = sizeof(uint16_t); 2726c3169a3SBruce Richardson 2736c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 2746c3169a3SBruce Richardson if (ret) { 2756c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Multiqueue configured but send command " 2766c3169a3SBruce Richardson "failed, this is too late now..."); 2776c3169a3SBruce Richardson return -EINVAL; 2786c3169a3SBruce Richardson } 2796c3169a3SBruce Richardson 2806c3169a3SBruce Richardson return 0; 2816c3169a3SBruce Richardson } 2826c3169a3SBruce Richardson 2835382b188SBernard Iremonger void 2842963d99aSJianfeng Tan virtio_dev_queue_release(struct virtqueue *vq) 2852963d99aSJianfeng Tan { 286ce8e1218SBernard Iremonger struct virtio_hw *hw; 2875382b188SBernard Iremonger 2885382b188SBernard Iremonger if (vq) { 289ce8e1218SBernard Iremonger hw = vq->hw; 2902963d99aSJianfeng Tan if (vq->configured) 291d5bbeefcSYuanhan Liu hw->vtpci_ops->del_queue(hw, vq); 2925382b188SBernard Iremonger 293cab04612SHuawei Xie rte_free(vq->sw_ring); 2945382b188SBernard Iremonger rte_free(vq); 2955382b188SBernard Iremonger } 2965382b188SBernard Iremonger } 2975382b188SBernard Iremonger 2986c3169a3SBruce Richardson int virtio_dev_queue_setup(struct rte_eth_dev *dev, 2996c3169a3SBruce Richardson int queue_type, 3006c3169a3SBruce Richardson uint16_t queue_idx, 3016c3169a3SBruce Richardson uint16_t vtpci_queue_idx, 3026c3169a3SBruce Richardson uint16_t nb_desc, 3036c3169a3SBruce Richardson unsigned int socket_id, 30401ad44fdSHuawei Xie void **pvq) 3056c3169a3SBruce Richardson { 3066c3169a3SBruce Richardson char vq_name[VIRTQUEUE_MAX_NAME_SZ]; 30701ad44fdSHuawei Xie char vq_hdr_name[VIRTQUEUE_MAX_NAME_SZ]; 30801ad44fdSHuawei Xie const struct rte_memzone *mz = NULL, *hdr_mz = NULL; 3091e7bd238SStephen Hemminger unsigned int vq_size, size; 3106c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 311f24f8f9fSJianfeng Tan struct virtnet_rx *rxvq = NULL; 312f24f8f9fSJianfeng Tan struct virtnet_tx *txvq = NULL; 313f24f8f9fSJianfeng Tan struct virtnet_ctl *cvq = NULL; 31401ad44fdSHuawei Xie struct virtqueue *vq; 3154166bbf6SJianfeng Tan const char *queue_names[] = {"rvq", "txq", "cvq"}; 31601ad44fdSHuawei Xie size_t sz_vq, sz_q = 0, sz_hdr_mz = 0; 31701ad44fdSHuawei Xie void *sw_ring = NULL; 31801ad44fdSHuawei Xie int ret; 3196c3169a3SBruce Richardson 320d5bbeefcSYuanhan Liu PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx); 3216c3169a3SBruce Richardson 3226c3169a3SBruce Richardson /* 3236c3169a3SBruce Richardson * Read the virtqueue size from the Queue Size field 3246c3169a3SBruce Richardson * Always power of 2 and if 0 virtqueue does not exist 3256c3169a3SBruce Richardson */ 326d5bbeefcSYuanhan Liu vq_size = hw->vtpci_ops->get_queue_num(hw, vtpci_queue_idx); 3271e7bd238SStephen Hemminger PMD_INIT_LOG(DEBUG, "vq_size: %u nb_desc:%u", vq_size, nb_desc); 3286c3169a3SBruce Richardson if (vq_size == 0) { 3290bb159adSHuawei Xie PMD_INIT_LOG(ERR, "virtqueue does not exist"); 3306c3169a3SBruce Richardson return -EINVAL; 331d78deadaSStephen Hemminger } 332d78deadaSStephen Hemminger 333d78deadaSStephen Hemminger if (!rte_is_power_of_2(vq_size)) { 3340bb159adSHuawei Xie PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2"); 3356c3169a3SBruce Richardson return -EINVAL; 336d78deadaSStephen Hemminger } 337d78deadaSStephen Hemminger 3384166bbf6SJianfeng Tan snprintf(vq_name, sizeof(vq_name), "port%d_%s%d", 3394166bbf6SJianfeng Tan dev->data->port_id, queue_names[queue_type], queue_idx); 34001ad44fdSHuawei Xie 34101ad44fdSHuawei Xie sz_vq = RTE_ALIGN_CEIL(sizeof(*vq) + 3426c3169a3SBruce Richardson vq_size * sizeof(struct vq_desc_extra), 3436c3169a3SBruce Richardson RTE_CACHE_LINE_SIZE); 3444166bbf6SJianfeng Tan if (queue_type == VTNET_RQ) { 34501ad44fdSHuawei Xie sz_q = sz_vq + sizeof(*rxvq); 34601ad44fdSHuawei Xie } else if (queue_type == VTNET_TQ) { 34701ad44fdSHuawei Xie sz_q = sz_vq + sizeof(*txvq); 34801ad44fdSHuawei Xie /* 34901ad44fdSHuawei Xie * For each xmit packet, allocate a virtio_net_hdr 35001ad44fdSHuawei Xie * and indirect ring elements 35101ad44fdSHuawei Xie */ 35201ad44fdSHuawei Xie sz_hdr_mz = vq_size * sizeof(struct virtio_tx_region); 35301ad44fdSHuawei Xie } else if (queue_type == VTNET_CQ) { 35401ad44fdSHuawei Xie sz_q = sz_vq + sizeof(*cvq); 35501ad44fdSHuawei Xie /* Allocate a page for control vq command, data and status */ 35601ad44fdSHuawei Xie sz_hdr_mz = PAGE_SIZE; 35701ad44fdSHuawei Xie } 3584166bbf6SJianfeng Tan 35901ad44fdSHuawei Xie vq = rte_zmalloc_socket(vq_name, sz_q, RTE_CACHE_LINE_SIZE, socket_id); 36001ad44fdSHuawei Xie if (vq == NULL) { 36101ad44fdSHuawei Xie PMD_INIT_LOG(ERR, "can not allocate vq"); 362cab04612SHuawei Xie return -ENOMEM; 363cab04612SHuawei Xie } 3646c3169a3SBruce Richardson vq->hw = hw; 3656c3169a3SBruce Richardson vq->vq_queue_index = vtpci_queue_idx; 3666c3169a3SBruce Richardson vq->vq_nentries = vq_size; 367945884f1SStephen Hemminger 368945884f1SStephen Hemminger if (nb_desc == 0 || nb_desc > vq_size) 369945884f1SStephen Hemminger nb_desc = vq_size; 370945884f1SStephen Hemminger vq->vq_free_cnt = nb_desc; 3716c3169a3SBruce Richardson 3726c3169a3SBruce Richardson /* 3736c3169a3SBruce Richardson * Reserve a memzone for vring elements 3746c3169a3SBruce Richardson */ 3756c3169a3SBruce Richardson size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN); 3766c3169a3SBruce Richardson vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN); 37701ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d", 37801ad44fdSHuawei Xie size, vq->vq_ring_size); 3796c3169a3SBruce Richardson 38001ad44fdSHuawei Xie mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size, socket_id, 38101ad44fdSHuawei Xie 0, VIRTIO_PCI_VRING_ALIGN); 3826c3169a3SBruce Richardson if (mz == NULL) { 383abf4c84bSBernard Iremonger if (rte_errno == EEXIST) 384abf4c84bSBernard Iremonger mz = rte_memzone_lookup(vq_name); 385abf4c84bSBernard Iremonger if (mz == NULL) { 38601ad44fdSHuawei Xie ret = -ENOMEM; 38701ad44fdSHuawei Xie goto fail_q_alloc; 3886c3169a3SBruce Richardson } 389abf4c84bSBernard Iremonger } 3906c3169a3SBruce Richardson 3916c3169a3SBruce Richardson memset(mz->addr, 0, sizeof(mz->len)); 39201ad44fdSHuawei Xie 3936c3169a3SBruce Richardson vq->vq_ring_mem = mz->phys_addr; 3946c3169a3SBruce Richardson vq->vq_ring_virt_mem = mz->addr; 39501ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64, 39601ad44fdSHuawei Xie (uint64_t)mz->phys_addr); 39701ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64, 39801ad44fdSHuawei Xie (uint64_t)(uintptr_t)mz->addr); 3996c3169a3SBruce Richardson 40001ad44fdSHuawei Xie if (sz_hdr_mz) { 40101ad44fdSHuawei Xie snprintf(vq_hdr_name, sizeof(vq_hdr_name), "port%d_%s%d_hdr", 40201ad44fdSHuawei Xie dev->data->port_id, queue_names[queue_type], 40301ad44fdSHuawei Xie queue_idx); 40401ad44fdSHuawei Xie hdr_mz = rte_memzone_reserve_aligned(vq_hdr_name, sz_hdr_mz, 4056dc5de3aSStephen Hemminger socket_id, 0, 4066dc5de3aSStephen Hemminger RTE_CACHE_LINE_SIZE); 4076dc5de3aSStephen Hemminger if (hdr_mz == NULL) { 408abf4c84bSBernard Iremonger if (rte_errno == EEXIST) 40901ad44fdSHuawei Xie hdr_mz = rte_memzone_lookup(vq_hdr_name); 4106dc5de3aSStephen Hemminger if (hdr_mz == NULL) { 41101ad44fdSHuawei Xie ret = -ENOMEM; 41201ad44fdSHuawei Xie goto fail_q_alloc; 4136c3169a3SBruce Richardson } 414abf4c84bSBernard Iremonger } 41501ad44fdSHuawei Xie } 41601ad44fdSHuawei Xie 41701ad44fdSHuawei Xie if (queue_type == VTNET_RQ) { 41801ad44fdSHuawei Xie size_t sz_sw = (RTE_PMD_VIRTIO_RX_MAX_BURST + vq_size) * 41901ad44fdSHuawei Xie sizeof(vq->sw_ring[0]); 42001ad44fdSHuawei Xie 42101ad44fdSHuawei Xie sw_ring = rte_zmalloc_socket("sw_ring", sz_sw, 42201ad44fdSHuawei Xie RTE_CACHE_LINE_SIZE, socket_id); 42301ad44fdSHuawei Xie if (!sw_ring) { 42401ad44fdSHuawei Xie PMD_INIT_LOG(ERR, "can not allocate RX soft ring"); 42501ad44fdSHuawei Xie ret = -ENOMEM; 42601ad44fdSHuawei Xie goto fail_q_alloc; 42701ad44fdSHuawei Xie } 42801ad44fdSHuawei Xie 42901ad44fdSHuawei Xie vq->sw_ring = sw_ring; 43001ad44fdSHuawei Xie rxvq = (struct virtnet_rx *)RTE_PTR_ADD(vq, sz_vq); 43101ad44fdSHuawei Xie rxvq->vq = vq; 43201ad44fdSHuawei Xie rxvq->port_id = dev->data->port_id; 43301ad44fdSHuawei Xie rxvq->queue_id = queue_idx; 43401ad44fdSHuawei Xie rxvq->mz = mz; 43501ad44fdSHuawei Xie *pvq = rxvq; 43601ad44fdSHuawei Xie } else if (queue_type == VTNET_TQ) { 43701ad44fdSHuawei Xie txvq = (struct virtnet_tx *)RTE_PTR_ADD(vq, sz_vq); 43801ad44fdSHuawei Xie txvq->vq = vq; 43901ad44fdSHuawei Xie txvq->port_id = dev->data->port_id; 44001ad44fdSHuawei Xie txvq->queue_id = queue_idx; 44101ad44fdSHuawei Xie txvq->mz = mz; 44201ad44fdSHuawei Xie txvq->virtio_net_hdr_mz = hdr_mz; 44301ad44fdSHuawei Xie txvq->virtio_net_hdr_mem = hdr_mz->phys_addr; 4446dc5de3aSStephen Hemminger 445f24f8f9fSJianfeng Tan *pvq = txvq; 446f24f8f9fSJianfeng Tan } else if (queue_type == VTNET_CQ) { 447f24f8f9fSJianfeng Tan cvq = (struct virtnet_ctl *)RTE_PTR_ADD(vq, sz_vq); 448f24f8f9fSJianfeng Tan cvq->vq = vq; 449f24f8f9fSJianfeng Tan cvq->mz = mz; 450f24f8f9fSJianfeng Tan cvq->virtio_net_hdr_mz = hdr_mz; 451f24f8f9fSJianfeng Tan cvq->virtio_net_hdr_mem = hdr_mz->phys_addr; 452f24f8f9fSJianfeng Tan memset(cvq->virtio_net_hdr_mz->addr, 0, PAGE_SIZE); 453f24f8f9fSJianfeng Tan *pvq = cvq; 454f24f8f9fSJianfeng Tan } 455f24f8f9fSJianfeng Tan 456e8df94b8SJianfeng Tan /* For virtio_user case (that is when dev->pci_dev is NULL), we use 457f24f8f9fSJianfeng Tan * virtual address. And we need properly set _offset_, please see 45825f80d10SOlivier Matz * VIRTIO_MBUF_DATA_DMA_ADDR in virtqueue.h for more information. 459f24f8f9fSJianfeng Tan */ 460f24f8f9fSJianfeng Tan if (dev->pci_dev) 461f24f8f9fSJianfeng Tan vq->offset = offsetof(struct rte_mbuf, buf_physaddr); 462f24f8f9fSJianfeng Tan else { 463f24f8f9fSJianfeng Tan vq->vq_ring_mem = (uintptr_t)mz->addr; 464f24f8f9fSJianfeng Tan vq->offset = offsetof(struct rte_mbuf, buf_addr); 465f24f8f9fSJianfeng Tan if (queue_type == VTNET_TQ) 466f24f8f9fSJianfeng Tan txvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr; 467f24f8f9fSJianfeng Tan else if (queue_type == VTNET_CQ) 468f24f8f9fSJianfeng Tan cvq->virtio_net_hdr_mem = (uintptr_t)hdr_mz->addr; 469f24f8f9fSJianfeng Tan } 470f24f8f9fSJianfeng Tan 471f24f8f9fSJianfeng Tan if (queue_type == VTNET_TQ) { 472f24f8f9fSJianfeng Tan struct virtio_tx_region *txr; 473f24f8f9fSJianfeng Tan unsigned int i; 474f24f8f9fSJianfeng Tan 4756dc5de3aSStephen Hemminger txr = hdr_mz->addr; 4766dc5de3aSStephen Hemminger memset(txr, 0, vq_size * sizeof(*txr)); 4776dc5de3aSStephen Hemminger for (i = 0; i < vq_size; i++) { 4786dc5de3aSStephen Hemminger struct vring_desc *start_dp = txr[i].tx_indir; 4796dc5de3aSStephen Hemminger 4806dc5de3aSStephen Hemminger vring_desc_init(start_dp, RTE_DIM(txr[i].tx_indir)); 4816dc5de3aSStephen Hemminger 4826dc5de3aSStephen Hemminger /* first indirect descriptor is always the tx header */ 48301ad44fdSHuawei Xie start_dp->addr = txvq->virtio_net_hdr_mem 4846dc5de3aSStephen Hemminger + i * sizeof(*txr) 4856dc5de3aSStephen Hemminger + offsetof(struct virtio_tx_region, tx_hdr); 4866dc5de3aSStephen Hemminger 48701ad44fdSHuawei Xie start_dp->len = hw->vtnet_hdr_size; 4886dc5de3aSStephen Hemminger start_dp->flags = VRING_DESC_F_NEXT; 4896dc5de3aSStephen Hemminger } 4906c3169a3SBruce Richardson } 4916c3169a3SBruce Richardson 492595454c5SJianfeng Tan if (hw->vtpci_ops->setup_queue(hw, vq) < 0) { 493595454c5SJianfeng Tan PMD_INIT_LOG(ERR, "setup_queue failed"); 494595454c5SJianfeng Tan virtio_dev_queue_release(vq); 495595454c5SJianfeng Tan return -EINVAL; 496595454c5SJianfeng Tan } 497595454c5SJianfeng Tan 4982963d99aSJianfeng Tan vq->configured = 1; 4996c3169a3SBruce Richardson return 0; 50001ad44fdSHuawei Xie 50101ad44fdSHuawei Xie fail_q_alloc: 50201ad44fdSHuawei Xie rte_free(sw_ring); 50301ad44fdSHuawei Xie rte_memzone_free(hdr_mz); 50401ad44fdSHuawei Xie rte_memzone_free(mz); 50501ad44fdSHuawei Xie rte_free(vq); 50601ad44fdSHuawei Xie 50701ad44fdSHuawei Xie return ret; 5086c3169a3SBruce Richardson } 5096c3169a3SBruce Richardson 5106c3169a3SBruce Richardson static int 5116c3169a3SBruce Richardson virtio_dev_cq_queue_setup(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx, 5126c3169a3SBruce Richardson uint32_t socket_id) 5136c3169a3SBruce Richardson { 51401ad44fdSHuawei Xie struct virtnet_ctl *cvq; 5156c3169a3SBruce Richardson int ret; 5166c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 5176c3169a3SBruce Richardson 5186c3169a3SBruce Richardson PMD_INIT_FUNC_TRACE(); 5196c3169a3SBruce Richardson ret = virtio_dev_queue_setup(dev, VTNET_CQ, VTNET_SQ_CQ_QUEUE_IDX, 52001ad44fdSHuawei Xie vtpci_queue_idx, 0, socket_id, (void **)&cvq); 5216c3169a3SBruce Richardson if (ret < 0) { 5226c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "control vq initialization failed"); 5236c3169a3SBruce Richardson return ret; 5246c3169a3SBruce Richardson } 5256c3169a3SBruce Richardson 52601ad44fdSHuawei Xie hw->cvq = cvq; 5276c3169a3SBruce Richardson return 0; 5286c3169a3SBruce Richardson } 5296c3169a3SBruce Richardson 5306c3169a3SBruce Richardson static void 531941d64b5SBernard Iremonger virtio_free_queues(struct rte_eth_dev *dev) 532941d64b5SBernard Iremonger { 533941d64b5SBernard Iremonger unsigned int i; 534941d64b5SBernard Iremonger 535941d64b5SBernard Iremonger for (i = 0; i < dev->data->nb_rx_queues; i++) 536941d64b5SBernard Iremonger virtio_dev_rx_queue_release(dev->data->rx_queues[i]); 537941d64b5SBernard Iremonger 538941d64b5SBernard Iremonger dev->data->nb_rx_queues = 0; 539941d64b5SBernard Iremonger 540941d64b5SBernard Iremonger for (i = 0; i < dev->data->nb_tx_queues; i++) 541941d64b5SBernard Iremonger virtio_dev_tx_queue_release(dev->data->tx_queues[i]); 542941d64b5SBernard Iremonger 543941d64b5SBernard Iremonger dev->data->nb_tx_queues = 0; 544941d64b5SBernard Iremonger } 545941d64b5SBernard Iremonger 546941d64b5SBernard Iremonger static void 5476c3169a3SBruce Richardson virtio_dev_close(struct rte_eth_dev *dev) 5486c3169a3SBruce Richardson { 5496c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 5506c3169a3SBruce Richardson 5516c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "virtio_dev_close"); 5526c3169a3SBruce Richardson 5539a0615afSJianfeng Tan if (hw->started == 1) 5549a0615afSJianfeng Tan virtio_dev_stop(dev); 5559a0615afSJianfeng Tan 55645e4acd4SOlivier Matz if (hw->cvq) 55745e4acd4SOlivier Matz virtio_dev_queue_release(hw->cvq->vq); 55845e4acd4SOlivier Matz 5596c3169a3SBruce Richardson /* reset the NIC */ 56062a785a6SJianfeng Tan if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 5616c3169a3SBruce Richardson vtpci_irq_config(hw, VIRTIO_MSI_NO_VECTOR); 5626c3169a3SBruce Richardson vtpci_reset(hw); 5636c3169a3SBruce Richardson virtio_dev_free_mbufs(dev); 564941d64b5SBernard Iremonger virtio_free_queues(dev); 5656c3169a3SBruce Richardson } 5666c3169a3SBruce Richardson 5676c3169a3SBruce Richardson static void 5686c3169a3SBruce Richardson virtio_dev_promiscuous_enable(struct rte_eth_dev *dev) 5696c3169a3SBruce Richardson { 5706c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 5716c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 5726c3169a3SBruce Richardson int dlen[1]; 5736c3169a3SBruce Richardson int ret; 5746c3169a3SBruce Richardson 575e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 576e9e414a4SStephen Hemminger PMD_INIT_LOG(INFO, "host does not support rx control\n"); 577e9e414a4SStephen Hemminger return; 578e9e414a4SStephen Hemminger } 579e9e414a4SStephen Hemminger 5806c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 5816c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 5826c3169a3SBruce Richardson ctrl.data[0] = 1; 5836c3169a3SBruce Richardson dlen[0] = 1; 5846c3169a3SBruce Richardson 5856c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 5866c3169a3SBruce Richardson if (ret) 5876c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to enable promisc"); 5886c3169a3SBruce Richardson } 5896c3169a3SBruce Richardson 5906c3169a3SBruce Richardson static void 5916c3169a3SBruce Richardson virtio_dev_promiscuous_disable(struct rte_eth_dev *dev) 5926c3169a3SBruce Richardson { 5936c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 5946c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 5956c3169a3SBruce Richardson int dlen[1]; 5966c3169a3SBruce Richardson int ret; 5976c3169a3SBruce Richardson 598e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 599e9e414a4SStephen Hemminger PMD_INIT_LOG(INFO, "host does not support rx control\n"); 600e9e414a4SStephen Hemminger return; 601e9e414a4SStephen Hemminger } 602e9e414a4SStephen Hemminger 6036c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 6046c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_PROMISC; 6056c3169a3SBruce Richardson ctrl.data[0] = 0; 6066c3169a3SBruce Richardson dlen[0] = 1; 6076c3169a3SBruce Richardson 6086c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 6096c3169a3SBruce Richardson if (ret) 6106c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to disable promisc"); 6116c3169a3SBruce Richardson } 6126c3169a3SBruce Richardson 6136c3169a3SBruce Richardson static void 6146c3169a3SBruce Richardson virtio_dev_allmulticast_enable(struct rte_eth_dev *dev) 6156c3169a3SBruce Richardson { 6166c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 6176c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 6186c3169a3SBruce Richardson int dlen[1]; 6196c3169a3SBruce Richardson int ret; 6206c3169a3SBruce Richardson 621e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 622e9e414a4SStephen Hemminger PMD_INIT_LOG(INFO, "host does not support rx control\n"); 623e9e414a4SStephen Hemminger return; 624e9e414a4SStephen Hemminger } 625e9e414a4SStephen Hemminger 6266c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 6276c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 6286c3169a3SBruce Richardson ctrl.data[0] = 1; 6296c3169a3SBruce Richardson dlen[0] = 1; 6306c3169a3SBruce Richardson 6316c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 6326c3169a3SBruce Richardson if (ret) 6336c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to enable allmulticast"); 6346c3169a3SBruce Richardson } 6356c3169a3SBruce Richardson 6366c3169a3SBruce Richardson static void 6376c3169a3SBruce Richardson virtio_dev_allmulticast_disable(struct rte_eth_dev *dev) 6386c3169a3SBruce Richardson { 6396c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 6406c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 6416c3169a3SBruce Richardson int dlen[1]; 6426c3169a3SBruce Richardson int ret; 6436c3169a3SBruce Richardson 644e9e414a4SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_RX)) { 645e9e414a4SStephen Hemminger PMD_INIT_LOG(INFO, "host does not support rx control\n"); 646e9e414a4SStephen Hemminger return; 647e9e414a4SStephen Hemminger } 648e9e414a4SStephen Hemminger 6496c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_RX; 6506c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_RX_ALLMULTI; 6516c3169a3SBruce Richardson ctrl.data[0] = 0; 6526c3169a3SBruce Richardson dlen[0] = 1; 6536c3169a3SBruce Richardson 6546c3169a3SBruce Richardson ret = virtio_send_command(hw->cvq, &ctrl, dlen, 1); 6556c3169a3SBruce Richardson if (ret) 6566c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "Failed to disable allmulticast"); 6576c3169a3SBruce Richardson } 6586c3169a3SBruce Richardson 6594ec2424aSSouvik Dey #define VLAN_TAG_LEN 4 /* 802.3ac tag (not DMA'd) */ 6604ec2424aSSouvik Dey static int 6614ec2424aSSouvik Dey virtio_mtu_set(struct rte_eth_dev *dev, uint16_t mtu) 6624ec2424aSSouvik Dey { 6634ec2424aSSouvik Dey struct virtio_hw *hw = dev->data->dev_private; 6644ec2424aSSouvik Dey uint32_t ether_hdr_len = ETHER_HDR_LEN + VLAN_TAG_LEN + 6654ec2424aSSouvik Dey hw->vtnet_hdr_size; 6664ec2424aSSouvik Dey uint32_t frame_size = mtu + ether_hdr_len; 6674ec2424aSSouvik Dey 6684ec2424aSSouvik Dey if (mtu < ETHER_MIN_MTU || frame_size > VIRTIO_MAX_RX_PKTLEN) { 6694ec2424aSSouvik Dey PMD_INIT_LOG(ERR, "MTU should be between %d and %d\n", 6704ec2424aSSouvik Dey ETHER_MIN_MTU, VIRTIO_MAX_RX_PKTLEN - ether_hdr_len); 6714ec2424aSSouvik Dey return -EINVAL; 6724ec2424aSSouvik Dey } 6734ec2424aSSouvik Dey return 0; 6744ec2424aSSouvik Dey } 6754ec2424aSSouvik Dey 6766c3169a3SBruce Richardson /* 6776c3169a3SBruce Richardson * dev_ops for virtio, bare necessities for basic operation 6786c3169a3SBruce Richardson */ 6796c3169a3SBruce Richardson static const struct eth_dev_ops virtio_eth_dev_ops = { 6806c3169a3SBruce Richardson .dev_configure = virtio_dev_configure, 6816c3169a3SBruce Richardson .dev_start = virtio_dev_start, 6826c3169a3SBruce Richardson .dev_stop = virtio_dev_stop, 6836c3169a3SBruce Richardson .dev_close = virtio_dev_close, 6846c3169a3SBruce Richardson .promiscuous_enable = virtio_dev_promiscuous_enable, 6856c3169a3SBruce Richardson .promiscuous_disable = virtio_dev_promiscuous_disable, 6866c3169a3SBruce Richardson .allmulticast_enable = virtio_dev_allmulticast_enable, 6876c3169a3SBruce Richardson .allmulticast_disable = virtio_dev_allmulticast_disable, 6884ec2424aSSouvik Dey .mtu_set = virtio_mtu_set, 6896c3169a3SBruce Richardson .dev_infos_get = virtio_dev_info_get, 6906c3169a3SBruce Richardson .stats_get = virtio_dev_stats_get, 69176d4c652SHarry van Haaren .xstats_get = virtio_dev_xstats_get, 692baf91c39SRemy Horton .xstats_get_names = virtio_dev_xstats_get_names, 6936c3169a3SBruce Richardson .stats_reset = virtio_dev_stats_reset, 69476d4c652SHarry van Haaren .xstats_reset = virtio_dev_stats_reset, 6956c3169a3SBruce Richardson .link_update = virtio_dev_link_update, 6966c3169a3SBruce Richardson .rx_queue_setup = virtio_dev_rx_queue_setup, 6976c3169a3SBruce Richardson .rx_queue_release = virtio_dev_rx_queue_release, 6986c3169a3SBruce Richardson .tx_queue_setup = virtio_dev_tx_queue_setup, 6996c3169a3SBruce Richardson .tx_queue_release = virtio_dev_tx_queue_release, 7006c3169a3SBruce Richardson /* collect stats per queue */ 7016c3169a3SBruce Richardson .queue_stats_mapping_set = virtio_dev_queue_stats_mapping_set, 7026c3169a3SBruce Richardson .vlan_filter_set = virtio_vlan_filter_set, 7036c3169a3SBruce Richardson .mac_addr_add = virtio_mac_addr_add, 7046c3169a3SBruce Richardson .mac_addr_remove = virtio_mac_addr_remove, 7056c3169a3SBruce Richardson .mac_addr_set = virtio_mac_addr_set, 7066c3169a3SBruce Richardson }; 7076c3169a3SBruce Richardson 7086c3169a3SBruce Richardson static inline int 7096c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(struct rte_eth_dev *dev, 7106c3169a3SBruce Richardson struct rte_eth_link *link) 7116c3169a3SBruce Richardson { 7126c3169a3SBruce Richardson struct rte_eth_link *dst = link; 7136c3169a3SBruce Richardson struct rte_eth_link *src = &(dev->data->dev_link); 7146c3169a3SBruce Richardson 7156c3169a3SBruce Richardson if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst, 7166c3169a3SBruce Richardson *(uint64_t *)src) == 0) 7176c3169a3SBruce Richardson return -1; 7186c3169a3SBruce Richardson 7196c3169a3SBruce Richardson return 0; 7206c3169a3SBruce Richardson } 7216c3169a3SBruce Richardson 7226c3169a3SBruce Richardson /** 7236c3169a3SBruce Richardson * Atomically writes the link status information into global 7246c3169a3SBruce Richardson * structure rte_eth_dev. 7256c3169a3SBruce Richardson * 7266c3169a3SBruce Richardson * @param dev 7276c3169a3SBruce Richardson * - Pointer to the structure rte_eth_dev to read from. 7286c3169a3SBruce Richardson * - Pointer to the buffer to be saved with the link status. 7296c3169a3SBruce Richardson * 7306c3169a3SBruce Richardson * @return 7316c3169a3SBruce Richardson * - On success, zero. 7326c3169a3SBruce Richardson * - On failure, negative value. 7336c3169a3SBruce Richardson */ 7346c3169a3SBruce Richardson static inline int 7356c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(struct rte_eth_dev *dev, 7366c3169a3SBruce Richardson struct rte_eth_link *link) 7376c3169a3SBruce Richardson { 7386c3169a3SBruce Richardson struct rte_eth_link *dst = &(dev->data->dev_link); 7396c3169a3SBruce Richardson struct rte_eth_link *src = link; 7406c3169a3SBruce Richardson 7416c3169a3SBruce Richardson if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst, 7426c3169a3SBruce Richardson *(uint64_t *)src) == 0) 7436c3169a3SBruce Richardson return -1; 7446c3169a3SBruce Richardson 7456c3169a3SBruce Richardson return 0; 7466c3169a3SBruce Richardson } 7476c3169a3SBruce Richardson 7486c3169a3SBruce Richardson static void 74976d4c652SHarry van Haaren virtio_update_stats(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 7506c3169a3SBruce Richardson { 7516c3169a3SBruce Richardson unsigned i; 7526c3169a3SBruce Richardson 7536c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_tx_queues; i++) { 75401ad44fdSHuawei Xie const struct virtnet_tx *txvq = dev->data->tx_queues[i]; 7556c3169a3SBruce Richardson if (txvq == NULL) 7566c3169a3SBruce Richardson continue; 7576c3169a3SBruce Richardson 75801ad44fdSHuawei Xie stats->opackets += txvq->stats.packets; 75901ad44fdSHuawei Xie stats->obytes += txvq->stats.bytes; 76001ad44fdSHuawei Xie stats->oerrors += txvq->stats.errors; 7616c3169a3SBruce Richardson 7626c3169a3SBruce Richardson if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 76301ad44fdSHuawei Xie stats->q_opackets[i] = txvq->stats.packets; 76401ad44fdSHuawei Xie stats->q_obytes[i] = txvq->stats.bytes; 7656c3169a3SBruce Richardson } 7666c3169a3SBruce Richardson } 7676c3169a3SBruce Richardson 7686c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_rx_queues; i++) { 76901ad44fdSHuawei Xie const struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 7706c3169a3SBruce Richardson if (rxvq == NULL) 7716c3169a3SBruce Richardson continue; 7726c3169a3SBruce Richardson 77301ad44fdSHuawei Xie stats->ipackets += rxvq->stats.packets; 77401ad44fdSHuawei Xie stats->ibytes += rxvq->stats.bytes; 77501ad44fdSHuawei Xie stats->ierrors += rxvq->stats.errors; 7766c3169a3SBruce Richardson 7776c3169a3SBruce Richardson if (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) { 77801ad44fdSHuawei Xie stats->q_ipackets[i] = rxvq->stats.packets; 77901ad44fdSHuawei Xie stats->q_ibytes[i] = rxvq->stats.bytes; 7806c3169a3SBruce Richardson } 7816c3169a3SBruce Richardson } 7826c3169a3SBruce Richardson 7836c3169a3SBruce Richardson stats->rx_nombuf = dev->data->rx_mbuf_alloc_failed; 7846c3169a3SBruce Richardson } 7856c3169a3SBruce Richardson 786baf91c39SRemy Horton static int virtio_dev_xstats_get_names(struct rte_eth_dev *dev, 787baf91c39SRemy Horton struct rte_eth_xstat_name *xstats_names, 788baf91c39SRemy Horton __rte_unused unsigned limit) 789baf91c39SRemy Horton { 790baf91c39SRemy Horton unsigned i; 791baf91c39SRemy Horton unsigned count = 0; 792baf91c39SRemy Horton unsigned t; 793baf91c39SRemy Horton 79401ad44fdSHuawei Xie unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS + 79501ad44fdSHuawei Xie dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS; 796baf91c39SRemy Horton 7977e1eb993SYuanhan Liu if (xstats_names != NULL) { 798baf91c39SRemy Horton /* Note: limit checked in rte_eth_xstats_names() */ 799baf91c39SRemy Horton 800baf91c39SRemy Horton for (i = 0; i < dev->data->nb_rx_queues; i++) { 801baf91c39SRemy Horton struct virtqueue *rxvq = dev->data->rx_queues[i]; 802baf91c39SRemy Horton if (rxvq == NULL) 803baf91c39SRemy Horton continue; 80401ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) { 805baf91c39SRemy Horton snprintf(xstats_names[count].name, 806baf91c39SRemy Horton sizeof(xstats_names[count].name), 807baf91c39SRemy Horton "rx_q%u_%s", i, 80801ad44fdSHuawei Xie rte_virtio_rxq_stat_strings[t].name); 809baf91c39SRemy Horton count++; 810baf91c39SRemy Horton } 811baf91c39SRemy Horton } 812baf91c39SRemy Horton 813baf91c39SRemy Horton for (i = 0; i < dev->data->nb_tx_queues; i++) { 814baf91c39SRemy Horton struct virtqueue *txvq = dev->data->tx_queues[i]; 815baf91c39SRemy Horton if (txvq == NULL) 816baf91c39SRemy Horton continue; 81701ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) { 818baf91c39SRemy Horton snprintf(xstats_names[count].name, 819baf91c39SRemy Horton sizeof(xstats_names[count].name), 820baf91c39SRemy Horton "tx_q%u_%s", i, 82101ad44fdSHuawei Xie rte_virtio_txq_stat_strings[t].name); 822baf91c39SRemy Horton count++; 823baf91c39SRemy Horton } 824baf91c39SRemy Horton } 825baf91c39SRemy Horton return count; 826baf91c39SRemy Horton } 827baf91c39SRemy Horton return nstats; 828baf91c39SRemy Horton } 829baf91c39SRemy Horton 83076d4c652SHarry van Haaren static int 831e2aae1c1SRemy Horton virtio_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats, 83276d4c652SHarry van Haaren unsigned n) 83376d4c652SHarry van Haaren { 83476d4c652SHarry van Haaren unsigned i; 83576d4c652SHarry van Haaren unsigned count = 0; 83676d4c652SHarry van Haaren 83701ad44fdSHuawei Xie unsigned nstats = dev->data->nb_tx_queues * VIRTIO_NB_TXQ_XSTATS + 83801ad44fdSHuawei Xie dev->data->nb_rx_queues * VIRTIO_NB_RXQ_XSTATS; 83976d4c652SHarry van Haaren 84076d4c652SHarry van Haaren if (n < nstats) 84176d4c652SHarry van Haaren return nstats; 84276d4c652SHarry van Haaren 84376d4c652SHarry van Haaren for (i = 0; i < dev->data->nb_rx_queues; i++) { 84401ad44fdSHuawei Xie struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 84576d4c652SHarry van Haaren 84676d4c652SHarry van Haaren if (rxvq == NULL) 84776d4c652SHarry van Haaren continue; 84876d4c652SHarry van Haaren 84976d4c652SHarry van Haaren unsigned t; 85076d4c652SHarry van Haaren 85101ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_RXQ_XSTATS; t++) { 85276d4c652SHarry van Haaren xstats[count].value = *(uint64_t *)(((char *)rxvq) + 85301ad44fdSHuawei Xie rte_virtio_rxq_stat_strings[t].offset); 85476d4c652SHarry van Haaren count++; 85576d4c652SHarry van Haaren } 85676d4c652SHarry van Haaren } 85776d4c652SHarry van Haaren 85876d4c652SHarry van Haaren for (i = 0; i < dev->data->nb_tx_queues; i++) { 85901ad44fdSHuawei Xie struct virtnet_tx *txvq = dev->data->tx_queues[i]; 86076d4c652SHarry van Haaren 86176d4c652SHarry van Haaren if (txvq == NULL) 86276d4c652SHarry van Haaren continue; 86376d4c652SHarry van Haaren 86476d4c652SHarry van Haaren unsigned t; 86576d4c652SHarry van Haaren 86601ad44fdSHuawei Xie for (t = 0; t < VIRTIO_NB_TXQ_XSTATS; t++) { 86776d4c652SHarry van Haaren xstats[count].value = *(uint64_t *)(((char *)txvq) + 86801ad44fdSHuawei Xie rte_virtio_txq_stat_strings[t].offset); 86976d4c652SHarry van Haaren count++; 87076d4c652SHarry van Haaren } 87176d4c652SHarry van Haaren } 87276d4c652SHarry van Haaren 87376d4c652SHarry van Haaren return count; 87476d4c652SHarry van Haaren } 87576d4c652SHarry van Haaren 87676d4c652SHarry van Haaren static void 87776d4c652SHarry van Haaren virtio_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats) 87876d4c652SHarry van Haaren { 87976d4c652SHarry van Haaren virtio_update_stats(dev, stats); 88076d4c652SHarry van Haaren } 88176d4c652SHarry van Haaren 8826c3169a3SBruce Richardson static void 8836c3169a3SBruce Richardson virtio_dev_stats_reset(struct rte_eth_dev *dev) 8846c3169a3SBruce Richardson { 8856c3169a3SBruce Richardson unsigned int i; 8866c3169a3SBruce Richardson 8876c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_tx_queues; i++) { 88801ad44fdSHuawei Xie struct virtnet_tx *txvq = dev->data->tx_queues[i]; 8896c3169a3SBruce Richardson if (txvq == NULL) 8906c3169a3SBruce Richardson continue; 8916c3169a3SBruce Richardson 89201ad44fdSHuawei Xie txvq->stats.packets = 0; 89301ad44fdSHuawei Xie txvq->stats.bytes = 0; 89401ad44fdSHuawei Xie txvq->stats.errors = 0; 89501ad44fdSHuawei Xie txvq->stats.multicast = 0; 89601ad44fdSHuawei Xie txvq->stats.broadcast = 0; 89701ad44fdSHuawei Xie memset(txvq->stats.size_bins, 0, 89801ad44fdSHuawei Xie sizeof(txvq->stats.size_bins[0]) * 8); 8996c3169a3SBruce Richardson } 9006c3169a3SBruce Richardson 9016c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_rx_queues; i++) { 90201ad44fdSHuawei Xie struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 9036c3169a3SBruce Richardson if (rxvq == NULL) 9046c3169a3SBruce Richardson continue; 9056c3169a3SBruce Richardson 90601ad44fdSHuawei Xie rxvq->stats.packets = 0; 90701ad44fdSHuawei Xie rxvq->stats.bytes = 0; 90801ad44fdSHuawei Xie rxvq->stats.errors = 0; 90901ad44fdSHuawei Xie rxvq->stats.multicast = 0; 91001ad44fdSHuawei Xie rxvq->stats.broadcast = 0; 91101ad44fdSHuawei Xie memset(rxvq->stats.size_bins, 0, 91201ad44fdSHuawei Xie sizeof(rxvq->stats.size_bins[0]) * 8); 9136c3169a3SBruce Richardson } 9146c3169a3SBruce Richardson } 9156c3169a3SBruce Richardson 9166c3169a3SBruce Richardson static void 9176c3169a3SBruce Richardson virtio_set_hwaddr(struct virtio_hw *hw) 9186c3169a3SBruce Richardson { 9196c3169a3SBruce Richardson vtpci_write_dev_config(hw, 9206c3169a3SBruce Richardson offsetof(struct virtio_net_config, mac), 9216c3169a3SBruce Richardson &hw->mac_addr, ETHER_ADDR_LEN); 9226c3169a3SBruce Richardson } 9236c3169a3SBruce Richardson 9246c3169a3SBruce Richardson static void 9256c3169a3SBruce Richardson virtio_get_hwaddr(struct virtio_hw *hw) 9266c3169a3SBruce Richardson { 9276c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) { 9286c3169a3SBruce Richardson vtpci_read_dev_config(hw, 9296c3169a3SBruce Richardson offsetof(struct virtio_net_config, mac), 9306c3169a3SBruce Richardson &hw->mac_addr, ETHER_ADDR_LEN); 9316c3169a3SBruce Richardson } else { 9326c3169a3SBruce Richardson eth_random_addr(&hw->mac_addr[0]); 9336c3169a3SBruce Richardson virtio_set_hwaddr(hw); 9346c3169a3SBruce Richardson } 9356c3169a3SBruce Richardson } 9366c3169a3SBruce Richardson 93727046236SStephen Hemminger static void 9386c3169a3SBruce Richardson virtio_mac_table_set(struct virtio_hw *hw, 9396c3169a3SBruce Richardson const struct virtio_net_ctrl_mac *uc, 9406c3169a3SBruce Richardson const struct virtio_net_ctrl_mac *mc) 9416c3169a3SBruce Richardson { 9426c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 9436c3169a3SBruce Richardson int err, len[2]; 9446c3169a3SBruce Richardson 94527046236SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 946e9083127SJianfeng Tan PMD_DRV_LOG(INFO, "host does not support mac table"); 94727046236SStephen Hemminger return; 94827046236SStephen Hemminger } 94927046236SStephen Hemminger 9506c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 9516c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_TABLE_SET; 9526c3169a3SBruce Richardson 9536c3169a3SBruce Richardson len[0] = uc->entries * ETHER_ADDR_LEN + sizeof(uc->entries); 9546c3169a3SBruce Richardson memcpy(ctrl.data, uc, len[0]); 9556c3169a3SBruce Richardson 9566c3169a3SBruce Richardson len[1] = mc->entries * ETHER_ADDR_LEN + sizeof(mc->entries); 9576c3169a3SBruce Richardson memcpy(ctrl.data + len[0], mc, len[1]); 9586c3169a3SBruce Richardson 9596c3169a3SBruce Richardson err = virtio_send_command(hw->cvq, &ctrl, len, 2); 9606c3169a3SBruce Richardson if (err != 0) 9616c3169a3SBruce Richardson PMD_DRV_LOG(NOTICE, "mac table set failed: %d", err); 9626c3169a3SBruce Richardson } 9636c3169a3SBruce Richardson 9646c3169a3SBruce Richardson static void 9656c3169a3SBruce Richardson virtio_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr, 9666c3169a3SBruce Richardson uint32_t index, uint32_t vmdq __rte_unused) 9676c3169a3SBruce Richardson { 9686c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 9696c3169a3SBruce Richardson const struct ether_addr *addrs = dev->data->mac_addrs; 9706c3169a3SBruce Richardson unsigned int i; 9716c3169a3SBruce Richardson struct virtio_net_ctrl_mac *uc, *mc; 9726c3169a3SBruce Richardson 9736c3169a3SBruce Richardson if (index >= VIRTIO_MAX_MAC_ADDRS) { 9746c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 9756c3169a3SBruce Richardson return; 9766c3169a3SBruce Richardson } 9776c3169a3SBruce Richardson 9786c3169a3SBruce Richardson uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 9796c3169a3SBruce Richardson uc->entries = 0; 9806c3169a3SBruce Richardson mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 9816c3169a3SBruce Richardson mc->entries = 0; 9826c3169a3SBruce Richardson 9836c3169a3SBruce Richardson for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 9846c3169a3SBruce Richardson const struct ether_addr *addr 9856c3169a3SBruce Richardson = (i == index) ? mac_addr : addrs + i; 9866c3169a3SBruce Richardson struct virtio_net_ctrl_mac *tbl 9876c3169a3SBruce Richardson = is_multicast_ether_addr(addr) ? mc : uc; 9886c3169a3SBruce Richardson 9896c3169a3SBruce Richardson memcpy(&tbl->macs[tbl->entries++], addr, ETHER_ADDR_LEN); 9906c3169a3SBruce Richardson } 9916c3169a3SBruce Richardson 9926c3169a3SBruce Richardson virtio_mac_table_set(hw, uc, mc); 9936c3169a3SBruce Richardson } 9946c3169a3SBruce Richardson 9956c3169a3SBruce Richardson static void 9966c3169a3SBruce Richardson virtio_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index) 9976c3169a3SBruce Richardson { 9986c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 9996c3169a3SBruce Richardson struct ether_addr *addrs = dev->data->mac_addrs; 10006c3169a3SBruce Richardson struct virtio_net_ctrl_mac *uc, *mc; 10016c3169a3SBruce Richardson unsigned int i; 10026c3169a3SBruce Richardson 10036c3169a3SBruce Richardson if (index >= VIRTIO_MAX_MAC_ADDRS) { 10046c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "mac address index %u out of range", index); 10056c3169a3SBruce Richardson return; 10066c3169a3SBruce Richardson } 10076c3169a3SBruce Richardson 10086c3169a3SBruce Richardson uc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(uc->entries)); 10096c3169a3SBruce Richardson uc->entries = 0; 10106c3169a3SBruce Richardson mc = alloca(VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN + sizeof(mc->entries)); 10116c3169a3SBruce Richardson mc->entries = 0; 10126c3169a3SBruce Richardson 10136c3169a3SBruce Richardson for (i = 0; i < VIRTIO_MAX_MAC_ADDRS; i++) { 10146c3169a3SBruce Richardson struct virtio_net_ctrl_mac *tbl; 10156c3169a3SBruce Richardson 10166c3169a3SBruce Richardson if (i == index || is_zero_ether_addr(addrs + i)) 10176c3169a3SBruce Richardson continue; 10186c3169a3SBruce Richardson 10196c3169a3SBruce Richardson tbl = is_multicast_ether_addr(addrs + i) ? mc : uc; 10206c3169a3SBruce Richardson memcpy(&tbl->macs[tbl->entries++], addrs + i, ETHER_ADDR_LEN); 10216c3169a3SBruce Richardson } 10226c3169a3SBruce Richardson 10236c3169a3SBruce Richardson virtio_mac_table_set(hw, uc, mc); 10246c3169a3SBruce Richardson } 10256c3169a3SBruce Richardson 10266c3169a3SBruce Richardson static void 10276c3169a3SBruce Richardson virtio_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr) 10286c3169a3SBruce Richardson { 10296c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 10306c3169a3SBruce Richardson 10316c3169a3SBruce Richardson memcpy(hw->mac_addr, mac_addr, ETHER_ADDR_LEN); 10326c3169a3SBruce Richardson 10336c3169a3SBruce Richardson /* Use atomic update if available */ 10346c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_MAC_ADDR)) { 10356c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 10366c3169a3SBruce Richardson int len = ETHER_ADDR_LEN; 10376c3169a3SBruce Richardson 10386c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_MAC; 10396c3169a3SBruce Richardson ctrl.hdr.cmd = VIRTIO_NET_CTRL_MAC_ADDR_SET; 10406c3169a3SBruce Richardson 10416c3169a3SBruce Richardson memcpy(ctrl.data, mac_addr, ETHER_ADDR_LEN); 10426c3169a3SBruce Richardson virtio_send_command(hw->cvq, &ctrl, &len, 1); 10436c3169a3SBruce Richardson } else if (vtpci_with_feature(hw, VIRTIO_NET_F_MAC)) 10446c3169a3SBruce Richardson virtio_set_hwaddr(hw); 10456c3169a3SBruce Richardson } 10466c3169a3SBruce Richardson 10476c3169a3SBruce Richardson static int 10486c3169a3SBruce Richardson virtio_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on) 10496c3169a3SBruce Richardson { 10506c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 10516c3169a3SBruce Richardson struct virtio_pmd_ctrl ctrl; 10526c3169a3SBruce Richardson int len; 10536c3169a3SBruce Richardson 10546c3169a3SBruce Richardson if (!vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) 10556c3169a3SBruce Richardson return -ENOTSUP; 10566c3169a3SBruce Richardson 10576c3169a3SBruce Richardson ctrl.hdr.class = VIRTIO_NET_CTRL_VLAN; 10586c3169a3SBruce Richardson ctrl.hdr.cmd = on ? VIRTIO_NET_CTRL_VLAN_ADD : VIRTIO_NET_CTRL_VLAN_DEL; 10596c3169a3SBruce Richardson memcpy(ctrl.data, &vlan_id, sizeof(vlan_id)); 10606c3169a3SBruce Richardson len = sizeof(vlan_id); 10616c3169a3SBruce Richardson 10626c3169a3SBruce Richardson return virtio_send_command(hw->cvq, &ctrl, &len, 1); 10636c3169a3SBruce Richardson } 10646c3169a3SBruce Richardson 10656ba1f63bSYuanhan Liu static int 106660e6f470SOlivier Matz virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features) 10676c3169a3SBruce Richardson { 10683891f233SYuanhan Liu uint64_t host_features; 10696c3169a3SBruce Richardson 10706c3169a3SBruce Richardson /* Prepare guest_features: feature that driver wants to support */ 10713891f233SYuanhan Liu PMD_INIT_LOG(DEBUG, "guest_features before negotiate = %" PRIx64, 107260e6f470SOlivier Matz req_features); 10736c3169a3SBruce Richardson 10746c3169a3SBruce Richardson /* Read device(host) feature bits */ 1075d5bbeefcSYuanhan Liu host_features = hw->vtpci_ops->get_features(hw); 10763891f233SYuanhan Liu PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64, 10776c3169a3SBruce Richardson host_features); 10786c3169a3SBruce Richardson 10796c3169a3SBruce Richardson /* 10806c3169a3SBruce Richardson * Negotiate features: Subset of device feature bits are written back 10816c3169a3SBruce Richardson * guest feature bits. 10826c3169a3SBruce Richardson */ 108360e6f470SOlivier Matz hw->guest_features = req_features; 10846c3169a3SBruce Richardson hw->guest_features = vtpci_negotiate_features(hw, host_features); 10853891f233SYuanhan Liu PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64, 10866c3169a3SBruce Richardson hw->guest_features); 10876ba1f63bSYuanhan Liu 10886ba1f63bSYuanhan Liu if (hw->modern) { 10896ba1f63bSYuanhan Liu if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) { 10906ba1f63bSYuanhan Liu PMD_INIT_LOG(ERR, 10916ba1f63bSYuanhan Liu "VIRTIO_F_VERSION_1 features is not enabled."); 10926ba1f63bSYuanhan Liu return -1; 10936ba1f63bSYuanhan Liu } 10946ba1f63bSYuanhan Liu vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK); 10956ba1f63bSYuanhan Liu if (!(vtpci_get_status(hw) & VIRTIO_CONFIG_STATUS_FEATURES_OK)) { 10966ba1f63bSYuanhan Liu PMD_INIT_LOG(ERR, 10976ba1f63bSYuanhan Liu "failed to set FEATURES_OK status!"); 10986ba1f63bSYuanhan Liu return -1; 10996ba1f63bSYuanhan Liu } 11006ba1f63bSYuanhan Liu } 11016ba1f63bSYuanhan Liu 110260e6f470SOlivier Matz hw->req_guest_features = req_features; 110360e6f470SOlivier Matz 11046ba1f63bSYuanhan Liu return 0; 11056c3169a3SBruce Richardson } 11066c3169a3SBruce Richardson 11076c3169a3SBruce Richardson /* 11086c3169a3SBruce Richardson * Process Virtio Config changed interrupt and call the callback 11096c3169a3SBruce Richardson * if link state changed. 11106c3169a3SBruce Richardson */ 11116c3169a3SBruce Richardson static void 11126c3169a3SBruce Richardson virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle, 11136c3169a3SBruce Richardson void *param) 11146c3169a3SBruce Richardson { 11156c3169a3SBruce Richardson struct rte_eth_dev *dev = param; 11166c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 11176c3169a3SBruce Richardson uint8_t isr; 11186c3169a3SBruce Richardson 11196c3169a3SBruce Richardson /* Read interrupt status which clears interrupt */ 11206c3169a3SBruce Richardson isr = vtpci_isr(hw); 11216c3169a3SBruce Richardson PMD_DRV_LOG(INFO, "interrupt status = %#x", isr); 11226c3169a3SBruce Richardson 11236c3169a3SBruce Richardson if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) 11246c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "interrupt enable failed"); 11256c3169a3SBruce Richardson 11266c3169a3SBruce Richardson if (isr & VIRTIO_PCI_ISR_CONFIG) { 11276c3169a3SBruce Richardson if (virtio_dev_link_update(dev, 0) == 0) 11286c3169a3SBruce Richardson _rte_eth_dev_callback_process(dev, 1129*c1ceaf3aSBernard Iremonger RTE_ETH_EVENT_INTR_LSC, NULL); 11306c3169a3SBruce Richardson } 11316c3169a3SBruce Richardson 11326c3169a3SBruce Richardson } 11336c3169a3SBruce Richardson 11346c3169a3SBruce Richardson static void 11356c3169a3SBruce Richardson rx_func_get(struct rte_eth_dev *eth_dev) 11366c3169a3SBruce Richardson { 11376c3169a3SBruce Richardson struct virtio_hw *hw = eth_dev->data->dev_private; 11386c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF)) 11396c3169a3SBruce Richardson eth_dev->rx_pkt_burst = &virtio_recv_mergeable_pkts; 11406c3169a3SBruce Richardson else 11416c3169a3SBruce Richardson eth_dev->rx_pkt_burst = &virtio_recv_pkts; 11426c3169a3SBruce Richardson } 11436c3169a3SBruce Richardson 114460e6f470SOlivier Matz /* reset device and renegotiate features if needed */ 1145198ab336SOlivier Matz static int 114660e6f470SOlivier Matz virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features) 11476c3169a3SBruce Richardson { 11486c3169a3SBruce Richardson struct virtio_hw *hw = eth_dev->data->dev_private; 11496c3169a3SBruce Richardson struct virtio_net_config *config; 11506c3169a3SBruce Richardson struct virtio_net_config local_config; 1151198ab336SOlivier Matz struct rte_pci_device *pci_dev = eth_dev->pci_dev; 11526c3169a3SBruce Richardson 11536c3169a3SBruce Richardson /* Reset the device although not necessary at startup */ 11546c3169a3SBruce Richardson vtpci_reset(hw); 11556c3169a3SBruce Richardson 11566c3169a3SBruce Richardson /* Tell the host we've noticed this device. */ 11576c3169a3SBruce Richardson vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_ACK); 11586c3169a3SBruce Richardson 11596c3169a3SBruce Richardson /* Tell the host we've known how to drive the device. */ 11606c3169a3SBruce Richardson vtpci_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER); 116160e6f470SOlivier Matz if (virtio_negotiate_features(hw, req_features) < 0) 11626ba1f63bSYuanhan Liu return -1; 11636c3169a3SBruce Richardson 1164954ea115SStephen Hemminger /* If host does not support status then disable LSC */ 1165954ea115SStephen Hemminger if (!vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) 1166198ab336SOlivier Matz eth_dev->data->dev_flags &= ~RTE_ETH_DEV_INTR_LSC; 1167198ab336SOlivier Matz else 1168198ab336SOlivier Matz eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC; 1169954ea115SStephen Hemminger 1170d15339b9SBernard Iremonger rte_eth_copy_pci_info(eth_dev, pci_dev); 1171d15339b9SBernard Iremonger 11726c3169a3SBruce Richardson rx_func_get(eth_dev); 11736c3169a3SBruce Richardson 11746c3169a3SBruce Richardson /* Setting up rx_header size for the device */ 11756ba1f63bSYuanhan Liu if (vtpci_with_feature(hw, VIRTIO_NET_F_MRG_RXBUF) || 11766ba1f63bSYuanhan Liu vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) 11776c3169a3SBruce Richardson hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf); 11786c3169a3SBruce Richardson else 11796c3169a3SBruce Richardson hw->vtnet_hdr_size = sizeof(struct virtio_net_hdr); 11806c3169a3SBruce Richardson 11816c3169a3SBruce Richardson /* Copy the permanent MAC address to: virtio_hw */ 11826c3169a3SBruce Richardson virtio_get_hwaddr(hw); 11836c3169a3SBruce Richardson ether_addr_copy((struct ether_addr *) hw->mac_addr, 11846c3169a3SBruce Richardson ð_dev->data->mac_addrs[0]); 11856c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 11866c3169a3SBruce Richardson "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 11876c3169a3SBruce Richardson hw->mac_addr[0], hw->mac_addr[1], hw->mac_addr[2], 11886c3169a3SBruce Richardson hw->mac_addr[3], hw->mac_addr[4], hw->mac_addr[5]); 11896c3169a3SBruce Richardson 11906c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) { 11916c3169a3SBruce Richardson config = &local_config; 11926c3169a3SBruce Richardson 11936d7740e2SChangchun Ouyang vtpci_read_dev_config(hw, 11946d7740e2SChangchun Ouyang offsetof(struct virtio_net_config, mac), 11956d7740e2SChangchun Ouyang &config->mac, sizeof(config->mac)); 11966d7740e2SChangchun Ouyang 11976c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 11986d7740e2SChangchun Ouyang vtpci_read_dev_config(hw, 11996d7740e2SChangchun Ouyang offsetof(struct virtio_net_config, status), 12006d7740e2SChangchun Ouyang &config->status, sizeof(config->status)); 12016c3169a3SBruce Richardson } else { 12026c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 12036c3169a3SBruce Richardson "VIRTIO_NET_F_STATUS is not supported"); 12046c3169a3SBruce Richardson config->status = 0; 12056c3169a3SBruce Richardson } 12066c3169a3SBruce Richardson 12076c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_MQ)) { 12086d7740e2SChangchun Ouyang vtpci_read_dev_config(hw, 12096d7740e2SChangchun Ouyang offsetof(struct virtio_net_config, max_virtqueue_pairs), 12106d7740e2SChangchun Ouyang &config->max_virtqueue_pairs, 12116d7740e2SChangchun Ouyang sizeof(config->max_virtqueue_pairs)); 12126c3169a3SBruce Richardson } else { 12136c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 12146c3169a3SBruce Richardson "VIRTIO_NET_F_MQ is not supported"); 12156c3169a3SBruce Richardson config->max_virtqueue_pairs = 1; 12166c3169a3SBruce Richardson } 12176c3169a3SBruce Richardson 121845e4acd4SOlivier Matz hw->max_queue_pairs = config->max_virtqueue_pairs; 12196c3169a3SBruce Richardson 12206c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=%d", 12216c3169a3SBruce Richardson config->max_virtqueue_pairs); 12226c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "config->status=%d", config->status); 12236c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 12246c3169a3SBruce Richardson "PORT MAC: %02X:%02X:%02X:%02X:%02X:%02X", 12256c3169a3SBruce Richardson config->mac[0], config->mac[1], 12266c3169a3SBruce Richardson config->mac[2], config->mac[3], 12276c3169a3SBruce Richardson config->mac[4], config->mac[5]); 12286c3169a3SBruce Richardson } else { 122945e4acd4SOlivier Matz PMD_INIT_LOG(DEBUG, "config->max_virtqueue_pairs=1"); 123045e4acd4SOlivier Matz hw->max_queue_pairs = 1; 12316c3169a3SBruce Richardson } 12326c3169a3SBruce Richardson 1233ce2eabddSJianfeng Tan if (pci_dev) 12346c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x", 12356c3169a3SBruce Richardson eth_dev->data->port_id, pci_dev->id.vendor_id, 12366c3169a3SBruce Richardson pci_dev->id.device_id); 12376c3169a3SBruce Richardson 1238198ab336SOlivier Matz return 0; 1239198ab336SOlivier Matz } 1240198ab336SOlivier Matz 1241198ab336SOlivier Matz /* 1242198ab336SOlivier Matz * This function is based on probe() function in virtio_pci.c 1243198ab336SOlivier Matz * It returns 0 on success. 1244198ab336SOlivier Matz */ 1245198ab336SOlivier Matz int 1246198ab336SOlivier Matz eth_virtio_dev_init(struct rte_eth_dev *eth_dev) 1247198ab336SOlivier Matz { 1248198ab336SOlivier Matz struct virtio_hw *hw = eth_dev->data->dev_private; 1249198ab336SOlivier Matz struct rte_pci_device *pci_dev; 1250198ab336SOlivier Matz uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE; 1251198ab336SOlivier Matz int ret; 1252198ab336SOlivier Matz 1253198ab336SOlivier Matz RTE_BUILD_BUG_ON(RTE_PKTMBUF_HEADROOM < sizeof(struct virtio_net_hdr_mrg_rxbuf)); 1254198ab336SOlivier Matz 1255198ab336SOlivier Matz eth_dev->dev_ops = &virtio_eth_dev_ops; 1256198ab336SOlivier Matz eth_dev->tx_pkt_burst = &virtio_xmit_pkts; 1257198ab336SOlivier Matz 1258198ab336SOlivier Matz if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 1259198ab336SOlivier Matz rx_func_get(eth_dev); 1260198ab336SOlivier Matz return 0; 1261198ab336SOlivier Matz } 1262198ab336SOlivier Matz 1263198ab336SOlivier Matz /* Allocate memory for storing MAC addresses */ 1264198ab336SOlivier Matz eth_dev->data->mac_addrs = rte_zmalloc("virtio", VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN, 0); 1265198ab336SOlivier Matz if (eth_dev->data->mac_addrs == NULL) { 1266198ab336SOlivier Matz PMD_INIT_LOG(ERR, 1267198ab336SOlivier Matz "Failed to allocate %d bytes needed to store MAC addresses", 1268198ab336SOlivier Matz VIRTIO_MAX_MAC_ADDRS * ETHER_ADDR_LEN); 1269198ab336SOlivier Matz return -ENOMEM; 1270198ab336SOlivier Matz } 1271198ab336SOlivier Matz 1272198ab336SOlivier Matz pci_dev = eth_dev->pci_dev; 1273198ab336SOlivier Matz 1274198ab336SOlivier Matz if (pci_dev) { 1275198ab336SOlivier Matz ret = vtpci_init(pci_dev, hw, &dev_flags); 1276198ab336SOlivier Matz if (ret) 1277198ab336SOlivier Matz return ret; 1278198ab336SOlivier Matz } 1279198ab336SOlivier Matz 1280198ab336SOlivier Matz eth_dev->data->dev_flags = dev_flags; 1281198ab336SOlivier Matz 128260e6f470SOlivier Matz /* reset device and negotiate default features */ 128396cb6711SOlivier Matz ret = virtio_init_device(eth_dev, VIRTIO_PMD_DEFAULT_GUEST_FEATURES); 1284198ab336SOlivier Matz if (ret < 0) 1285198ab336SOlivier Matz return ret; 1286198ab336SOlivier Matz 12876c3169a3SBruce Richardson /* Setup interrupt callback */ 128862a785a6SJianfeng Tan if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 12896c3169a3SBruce Richardson rte_intr_callback_register(&pci_dev->intr_handle, 12906c3169a3SBruce Richardson virtio_interrupt_handler, eth_dev); 12916c3169a3SBruce Richardson 12926c3169a3SBruce Richardson return 0; 12936c3169a3SBruce Richardson } 12946c3169a3SBruce Richardson 1295abf4c84bSBernard Iremonger static int 1296abf4c84bSBernard Iremonger eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev) 1297abf4c84bSBernard Iremonger { 1298abf4c84bSBernard Iremonger struct rte_pci_device *pci_dev; 1299abf4c84bSBernard Iremonger 1300abf4c84bSBernard Iremonger PMD_INIT_FUNC_TRACE(); 1301abf4c84bSBernard Iremonger 1302abf4c84bSBernard Iremonger if (rte_eal_process_type() == RTE_PROC_SECONDARY) 1303abf4c84bSBernard Iremonger return -EPERM; 1304abf4c84bSBernard Iremonger 13059a0615afSJianfeng Tan /* Close it anyway since there's no way to know if closed */ 1306abf4c84bSBernard Iremonger virtio_dev_close(eth_dev); 13079a0615afSJianfeng Tan 1308abf4c84bSBernard Iremonger pci_dev = eth_dev->pci_dev; 1309abf4c84bSBernard Iremonger 1310abf4c84bSBernard Iremonger eth_dev->dev_ops = NULL; 1311abf4c84bSBernard Iremonger eth_dev->tx_pkt_burst = NULL; 1312abf4c84bSBernard Iremonger eth_dev->rx_pkt_burst = NULL; 1313abf4c84bSBernard Iremonger 1314abf4c84bSBernard Iremonger rte_free(eth_dev->data->mac_addrs); 1315abf4c84bSBernard Iremonger eth_dev->data->mac_addrs = NULL; 1316abf4c84bSBernard Iremonger 1317abf4c84bSBernard Iremonger /* reset interrupt callback */ 131862a785a6SJianfeng Tan if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 1319abf4c84bSBernard Iremonger rte_intr_callback_unregister(&pci_dev->intr_handle, 1320abf4c84bSBernard Iremonger virtio_interrupt_handler, 1321abf4c84bSBernard Iremonger eth_dev); 13226ba1f63bSYuanhan Liu rte_eal_pci_unmap_device(pci_dev); 1323abf4c84bSBernard Iremonger 1324abf4c84bSBernard Iremonger PMD_INIT_LOG(DEBUG, "dev_uninit completed"); 1325abf4c84bSBernard Iremonger 1326abf4c84bSBernard Iremonger return 0; 1327abf4c84bSBernard Iremonger } 1328abf4c84bSBernard Iremonger 13296c3169a3SBruce Richardson static struct eth_driver rte_virtio_pmd = { 13306c52c126SStephen Hemminger .pci_drv = { 1331cd433ce5SDavid Marchand .driver = { 1332cd433ce5SDavid Marchand .name = "net_virtio", 1333cd433ce5SDavid Marchand }, 13346c3169a3SBruce Richardson .id_table = pci_id_virtio_map, 1335abf4c84bSBernard Iremonger .drv_flags = RTE_PCI_DRV_DETACHABLE, 1336c830cb29SDavid Marchand .probe = rte_eth_dev_pci_probe, 1337c830cb29SDavid Marchand .remove = rte_eth_dev_pci_remove, 13386c3169a3SBruce Richardson }, 13396c3169a3SBruce Richardson .eth_dev_init = eth_virtio_dev_init, 1340abf4c84bSBernard Iremonger .eth_dev_uninit = eth_virtio_dev_uninit, 13416c3169a3SBruce Richardson .dev_private_size = sizeof(struct virtio_hw), 13426c3169a3SBruce Richardson }; 13436c3169a3SBruce Richardson 1344c830cb29SDavid Marchand RTE_INIT(rte_virtio_pmd_init); 1345c830cb29SDavid Marchand static void 1346c830cb29SDavid Marchand rte_virtio_pmd_init(void) 13476c3169a3SBruce Richardson { 13486c3169a3SBruce Richardson if (rte_eal_iopl_init() != 0) { 13496c3169a3SBruce Richardson PMD_INIT_LOG(ERR, "IOPL call failed - cannot use virtio PMD"); 1350c830cb29SDavid Marchand return; 13516c3169a3SBruce Richardson } 13526c3169a3SBruce Richardson 1353c830cb29SDavid Marchand rte_eal_pci_register(&rte_virtio_pmd.pci_drv); 13546c3169a3SBruce Richardson } 13556c3169a3SBruce Richardson 13566c3169a3SBruce Richardson /* 13576c3169a3SBruce Richardson * Configure virtio device 13586c3169a3SBruce Richardson * It returns 0 on success. 13596c3169a3SBruce Richardson */ 13606c3169a3SBruce Richardson static int 13616c3169a3SBruce Richardson virtio_dev_configure(struct rte_eth_dev *dev) 13626c3169a3SBruce Richardson { 13636c3169a3SBruce Richardson const struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode; 13646c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 136560e6f470SOlivier Matz uint64_t req_features; 136645e4acd4SOlivier Matz int ret; 13676c3169a3SBruce Richardson 13686c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "configure"); 136996cb6711SOlivier Matz req_features = VIRTIO_PMD_DEFAULT_GUEST_FEATURES; 137096cb6711SOlivier Matz if (rxmode->hw_ip_checksum) 137196cb6711SOlivier Matz req_features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM); 137286d59b21SOlivier Matz if (rxmode->enable_lro) 137386d59b21SOlivier Matz req_features |= 137486d59b21SOlivier Matz (1ULL << VIRTIO_NET_F_GUEST_TSO4) | 137586d59b21SOlivier Matz (1ULL << VIRTIO_NET_F_GUEST_TSO6); 13766c3169a3SBruce Richardson 137760e6f470SOlivier Matz /* if request features changed, reinit the device */ 137860e6f470SOlivier Matz if (req_features != hw->req_guest_features) { 137960e6f470SOlivier Matz ret = virtio_init_device(dev, req_features); 138060e6f470SOlivier Matz if (ret < 0) 138160e6f470SOlivier Matz return ret; 138260e6f470SOlivier Matz } 138360e6f470SOlivier Matz 138496cb6711SOlivier Matz if (rxmode->hw_ip_checksum && 138596cb6711SOlivier Matz !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_CSUM)) { 138696cb6711SOlivier Matz PMD_DRV_LOG(NOTICE, 138796cb6711SOlivier Matz "rx ip checksum not available on this host"); 138896cb6711SOlivier Matz return -ENOTSUP; 138996cb6711SOlivier Matz } 139096cb6711SOlivier Matz 139186d59b21SOlivier Matz if (rxmode->enable_lro && 139286d59b21SOlivier Matz (!vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4) || 139386d59b21SOlivier Matz !vtpci_with_feature(hw, VIRTIO_NET_F_GUEST_TSO4))) { 139486d59b21SOlivier Matz PMD_DRV_LOG(NOTICE, 139586d59b21SOlivier Matz "lro not available on this host"); 139686d59b21SOlivier Matz return -ENOTSUP; 139786d59b21SOlivier Matz } 139886d59b21SOlivier Matz 139945e4acd4SOlivier Matz /* Setup and start control queue */ 140045e4acd4SOlivier Matz if (vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VQ)) { 140145e4acd4SOlivier Matz ret = virtio_dev_cq_queue_setup(dev, 140245e4acd4SOlivier Matz hw->max_queue_pairs * 2, 140345e4acd4SOlivier Matz SOCKET_ID_ANY); 140445e4acd4SOlivier Matz if (ret < 0) 140545e4acd4SOlivier Matz return ret; 140645e4acd4SOlivier Matz virtio_dev_cq_start(dev); 140745e4acd4SOlivier Matz } 140845e4acd4SOlivier Matz 14096c3169a3SBruce Richardson hw->vlan_strip = rxmode->hw_vlan_strip; 14106c3169a3SBruce Richardson 14116c3169a3SBruce Richardson if (rxmode->hw_vlan_filter 14126c3169a3SBruce Richardson && !vtpci_with_feature(hw, VIRTIO_NET_F_CTRL_VLAN)) { 14136c3169a3SBruce Richardson PMD_DRV_LOG(NOTICE, 14146c3169a3SBruce Richardson "vlan filtering not available on this host"); 14156c3169a3SBruce Richardson return -ENOTSUP; 14166c3169a3SBruce Richardson } 14176c3169a3SBruce Richardson 141862a785a6SJianfeng Tan if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) 14196c3169a3SBruce Richardson if (vtpci_irq_config(hw, 0) == VIRTIO_MSI_NO_VECTOR) { 14206c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "failed to set config vector"); 14216c3169a3SBruce Richardson return -EBUSY; 14226c3169a3SBruce Richardson } 14236c3169a3SBruce Richardson 14246c3169a3SBruce Richardson return 0; 14256c3169a3SBruce Richardson } 14266c3169a3SBruce Richardson 14276c3169a3SBruce Richardson 14286c3169a3SBruce Richardson static int 14296c3169a3SBruce Richardson virtio_dev_start(struct rte_eth_dev *dev) 14306c3169a3SBruce Richardson { 14316c3169a3SBruce Richardson uint16_t nb_queues, i; 14326c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 143301ad44fdSHuawei Xie struct virtnet_rx *rxvq; 143401ad44fdSHuawei Xie struct virtnet_tx *txvq __rte_unused; 14356c3169a3SBruce Richardson 14366c3169a3SBruce Richardson /* check if lsc interrupt feature is enabled */ 1437954ea115SStephen Hemminger if (dev->data->dev_conf.intr_conf.lsc) { 143862a785a6SJianfeng Tan if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) { 14396c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "link status not supported by host"); 14406c3169a3SBruce Richardson return -ENOTSUP; 14416c3169a3SBruce Richardson } 14426c3169a3SBruce Richardson 14436c3169a3SBruce Richardson if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) { 14446c3169a3SBruce Richardson PMD_DRV_LOG(ERR, "interrupt enable failed"); 14456c3169a3SBruce Richardson return -EIO; 14466c3169a3SBruce Richardson } 14476c3169a3SBruce Richardson } 14486c3169a3SBruce Richardson 14496c3169a3SBruce Richardson /* Initialize Link state */ 14506c3169a3SBruce Richardson virtio_dev_link_update(dev, 0); 14516c3169a3SBruce Richardson 14526c3169a3SBruce Richardson /* On restart after stop do not touch queues */ 14536c3169a3SBruce Richardson if (hw->started) 14546c3169a3SBruce Richardson return 0; 14556c3169a3SBruce Richardson 14566c3169a3SBruce Richardson /* Do final configuration before rx/tx engine starts */ 14576c3169a3SBruce Richardson virtio_dev_rxtx_start(dev); 14586c3169a3SBruce Richardson vtpci_reinit_complete(hw); 14596c3169a3SBruce Richardson 14606c3169a3SBruce Richardson hw->started = 1; 14616c3169a3SBruce Richardson 14626c3169a3SBruce Richardson /*Notify the backend 14636c3169a3SBruce Richardson *Otherwise the tap backend might already stop its queue due to fullness. 14646c3169a3SBruce Richardson *vhost backend will have no chance to be waked up 14656c3169a3SBruce Richardson */ 14666c3169a3SBruce Richardson nb_queues = dev->data->nb_rx_queues; 14676c3169a3SBruce Richardson if (nb_queues > 1) { 14686c3169a3SBruce Richardson if (virtio_set_multiple_queues(dev, nb_queues) != 0) 14696c3169a3SBruce Richardson return -EINVAL; 14706c3169a3SBruce Richardson } 14716c3169a3SBruce Richardson 14726c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "nb_queues=%d", nb_queues); 14736c3169a3SBruce Richardson 147401ad44fdSHuawei Xie for (i = 0; i < nb_queues; i++) { 147501ad44fdSHuawei Xie rxvq = dev->data->rx_queues[i]; 147601ad44fdSHuawei Xie virtqueue_notify(rxvq->vq); 147701ad44fdSHuawei Xie } 14786c3169a3SBruce Richardson 14796c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Notified backend at initialization"); 14806c3169a3SBruce Richardson 148101ad44fdSHuawei Xie for (i = 0; i < dev->data->nb_rx_queues; i++) { 148201ad44fdSHuawei Xie rxvq = dev->data->rx_queues[i]; 148301ad44fdSHuawei Xie VIRTQUEUE_DUMP(rxvq->vq); 148401ad44fdSHuawei Xie } 14856c3169a3SBruce Richardson 148601ad44fdSHuawei Xie for (i = 0; i < dev->data->nb_tx_queues; i++) { 148701ad44fdSHuawei Xie txvq = dev->data->tx_queues[i]; 148801ad44fdSHuawei Xie VIRTQUEUE_DUMP(txvq->vq); 148901ad44fdSHuawei Xie } 14906c3169a3SBruce Richardson 14916c3169a3SBruce Richardson return 0; 14926c3169a3SBruce Richardson } 14936c3169a3SBruce Richardson 14946c3169a3SBruce Richardson static void virtio_dev_free_mbufs(struct rte_eth_dev *dev) 14956c3169a3SBruce Richardson { 14966c3169a3SBruce Richardson struct rte_mbuf *buf; 14976c3169a3SBruce Richardson int i, mbuf_num = 0; 14986c3169a3SBruce Richardson 14996c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_rx_queues; i++) { 150001ad44fdSHuawei Xie struct virtnet_rx *rxvq = dev->data->rx_queues[i]; 150101ad44fdSHuawei Xie 15026c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 15036c3169a3SBruce Richardson "Before freeing rxq[%d] used and unused buf", i); 150401ad44fdSHuawei Xie VIRTQUEUE_DUMP(rxvq->vq); 15056c3169a3SBruce Richardson 150601ad44fdSHuawei Xie PMD_INIT_LOG(DEBUG, "rx_queues[%d]=%p", i, rxvq); 150701ad44fdSHuawei Xie while ((buf = virtqueue_detatch_unused(rxvq->vq)) != NULL) { 15086c3169a3SBruce Richardson rte_pktmbuf_free(buf); 15096c3169a3SBruce Richardson mbuf_num++; 15106c3169a3SBruce Richardson } 15116c3169a3SBruce Richardson 15126c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num); 15136c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 15146c3169a3SBruce Richardson "After freeing rxq[%d] used and unused buf", i); 151501ad44fdSHuawei Xie VIRTQUEUE_DUMP(rxvq->vq); 15166c3169a3SBruce Richardson } 15176c3169a3SBruce Richardson 15186c3169a3SBruce Richardson for (i = 0; i < dev->data->nb_tx_queues; i++) { 151901ad44fdSHuawei Xie struct virtnet_tx *txvq = dev->data->tx_queues[i]; 152001ad44fdSHuawei Xie 15216c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 15226c3169a3SBruce Richardson "Before freeing txq[%d] used and unused bufs", 15236c3169a3SBruce Richardson i); 152401ad44fdSHuawei Xie VIRTQUEUE_DUMP(txvq->vq); 15256c3169a3SBruce Richardson 15266c3169a3SBruce Richardson mbuf_num = 0; 152701ad44fdSHuawei Xie while ((buf = virtqueue_detatch_unused(txvq->vq)) != NULL) { 15286c3169a3SBruce Richardson rte_pktmbuf_free(buf); 15296c3169a3SBruce Richardson mbuf_num++; 15306c3169a3SBruce Richardson } 15316c3169a3SBruce Richardson 15326c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "free %d mbufs", mbuf_num); 15336c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, 15346c3169a3SBruce Richardson "After freeing txq[%d] used and unused buf", i); 153501ad44fdSHuawei Xie VIRTQUEUE_DUMP(txvq->vq); 15366c3169a3SBruce Richardson } 15376c3169a3SBruce Richardson } 15386c3169a3SBruce Richardson 15396c3169a3SBruce Richardson /* 15406c3169a3SBruce Richardson * Stop device: disable interrupt and mark link down 15416c3169a3SBruce Richardson */ 15426c3169a3SBruce Richardson static void 15436c3169a3SBruce Richardson virtio_dev_stop(struct rte_eth_dev *dev) 15446c3169a3SBruce Richardson { 15456c3169a3SBruce Richardson struct rte_eth_link link; 15469a0615afSJianfeng Tan struct virtio_hw *hw = dev->data->dev_private; 15476c3169a3SBruce Richardson 15486c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "stop"); 15496c3169a3SBruce Richardson 15509a0615afSJianfeng Tan hw->started = 0; 15519a0615afSJianfeng Tan 15526c3169a3SBruce Richardson if (dev->data->dev_conf.intr_conf.lsc) 15536c3169a3SBruce Richardson rte_intr_disable(&dev->pci_dev->intr_handle); 15546c3169a3SBruce Richardson 15556c3169a3SBruce Richardson memset(&link, 0, sizeof(link)); 15566c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(dev, &link); 15576c3169a3SBruce Richardson } 15586c3169a3SBruce Richardson 15596c3169a3SBruce Richardson static int 15606c3169a3SBruce Richardson virtio_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete) 15616c3169a3SBruce Richardson { 15626c3169a3SBruce Richardson struct rte_eth_link link, old; 15636c3169a3SBruce Richardson uint16_t status; 15646c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 15656c3169a3SBruce Richardson memset(&link, 0, sizeof(link)); 15666c3169a3SBruce Richardson virtio_dev_atomic_read_link_status(dev, &link); 15676c3169a3SBruce Richardson old = link; 156811319000SMarc Sune link.link_duplex = ETH_LINK_FULL_DUPLEX; 15696c3169a3SBruce Richardson link.link_speed = SPEED_10G; 15706c3169a3SBruce Richardson 15716c3169a3SBruce Richardson if (vtpci_with_feature(hw, VIRTIO_NET_F_STATUS)) { 15726c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Get link status from hw"); 15736c3169a3SBruce Richardson vtpci_read_dev_config(hw, 15746c3169a3SBruce Richardson offsetof(struct virtio_net_config, status), 15756c3169a3SBruce Richardson &status, sizeof(status)); 15766c3169a3SBruce Richardson if ((status & VIRTIO_NET_S_LINK_UP) == 0) { 157709419f23SThomas Monjalon link.link_status = ETH_LINK_DOWN; 15786c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Port %d is down", 15796c3169a3SBruce Richardson dev->data->port_id); 15806c3169a3SBruce Richardson } else { 158109419f23SThomas Monjalon link.link_status = ETH_LINK_UP; 15826c3169a3SBruce Richardson PMD_INIT_LOG(DEBUG, "Port %d is up", 15836c3169a3SBruce Richardson dev->data->port_id); 15846c3169a3SBruce Richardson } 15856c3169a3SBruce Richardson } else { 158609419f23SThomas Monjalon link.link_status = ETH_LINK_UP; 15876c3169a3SBruce Richardson } 15886c3169a3SBruce Richardson virtio_dev_atomic_write_link_status(dev, &link); 15896c3169a3SBruce Richardson 15906c3169a3SBruce Richardson return (old.link_status == link.link_status) ? -1 : 0; 15916c3169a3SBruce Richardson } 15926c3169a3SBruce Richardson 15936c3169a3SBruce Richardson static void 15946c3169a3SBruce Richardson virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) 15956c3169a3SBruce Richardson { 159669657304SOlivier Matz uint64_t tso_mask; 15976c3169a3SBruce Richardson struct virtio_hw *hw = dev->data->dev_private; 15986c3169a3SBruce Richardson 1599ce2eabddSJianfeng Tan if (dev->pci_dev) 16002f3193cfSJan Viktorin dev_info->driver_name = dev->driver->pci_drv.driver.name; 1601ce2eabddSJianfeng Tan else 1602e8df94b8SJianfeng Tan dev_info->driver_name = "virtio_user PMD"; 160345e4acd4SOlivier Matz dev_info->max_rx_queues = 160445e4acd4SOlivier Matz RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES); 160545e4acd4SOlivier Matz dev_info->max_tx_queues = 160645e4acd4SOlivier Matz RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_TX_QUEUES); 16076c3169a3SBruce Richardson dev_info->min_rx_bufsize = VIRTIO_MIN_RX_BUFSIZE; 16086c3169a3SBruce Richardson dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN; 16096c3169a3SBruce Richardson dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS; 16106c3169a3SBruce Richardson dev_info->default_txconf = (struct rte_eth_txconf) { 16116c3169a3SBruce Richardson .txq_flags = ETH_TXQ_FLAGS_NOOFFLOADS 16126c3169a3SBruce Richardson }; 161396cb6711SOlivier Matz dev_info->rx_offload_capa = 161496cb6711SOlivier Matz DEV_RX_OFFLOAD_TCP_CKSUM | 161586d59b21SOlivier Matz DEV_RX_OFFLOAD_UDP_CKSUM | 161686d59b21SOlivier Matz DEV_RX_OFFLOAD_TCP_LRO; 161758169a9cSOlivier Matz dev_info->tx_offload_capa = 0; 161858169a9cSOlivier Matz 161958169a9cSOlivier Matz if (hw->guest_features & (1ULL << VIRTIO_NET_F_CSUM)) { 162058169a9cSOlivier Matz dev_info->tx_offload_capa |= 162158169a9cSOlivier Matz DEV_TX_OFFLOAD_UDP_CKSUM | 162258169a9cSOlivier Matz DEV_TX_OFFLOAD_TCP_CKSUM; 162358169a9cSOlivier Matz } 162469657304SOlivier Matz 162569657304SOlivier Matz tso_mask = (1ULL << VIRTIO_NET_F_HOST_TSO4) | 162669657304SOlivier Matz (1ULL << VIRTIO_NET_F_HOST_TSO6); 162769657304SOlivier Matz if ((hw->guest_features & tso_mask) == tso_mask) 162869657304SOlivier Matz dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO; 16296c3169a3SBruce Richardson } 16306c3169a3SBruce Richardson 16316c3169a3SBruce Richardson /* 16326c3169a3SBruce Richardson * It enables testpmd to collect per queue stats. 16336c3169a3SBruce Richardson */ 16346c3169a3SBruce Richardson static int 16356c3169a3SBruce Richardson virtio_dev_queue_stats_mapping_set(__rte_unused struct rte_eth_dev *eth_dev, 16366c3169a3SBruce Richardson __rte_unused uint16_t queue_id, __rte_unused uint8_t stat_idx, 16376c3169a3SBruce Richardson __rte_unused uint8_t is_rx) 16386c3169a3SBruce Richardson { 16396c3169a3SBruce Richardson return 0; 16406c3169a3SBruce Richardson } 16416c3169a3SBruce Richardson 164201f19227SShreyansh Jain RTE_PMD_EXPORT_NAME(net_virtio, __COUNTER__); 164301f19227SShreyansh Jain RTE_PMD_REGISTER_PCI_TABLE(net_virtio, pci_id_virtio_map); 1644