2017-05-30 21:13:50 +00:00
|
|
|
/*-
|
|
|
|
* BSD LICENSE
|
|
|
|
*
|
|
|
|
* Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* * Neither the name of Intel Corporation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <linux/virtio_scsi.h>
|
|
|
|
|
|
|
|
#include <rte_memcpy.h>
|
|
|
|
#include <rte_string_fns.h>
|
|
|
|
#include <rte_memzone.h>
|
|
|
|
#include <rte_malloc.h>
|
|
|
|
#include <rte_atomic.h>
|
|
|
|
#include <rte_branch_prediction.h>
|
|
|
|
#include <rte_pci.h>
|
|
|
|
#include <rte_common.h>
|
|
|
|
#include <rte_errno.h>
|
|
|
|
|
|
|
|
#include <rte_memory.h>
|
|
|
|
#include <rte_eal.h>
|
|
|
|
#include <rte_dev.h>
|
|
|
|
|
2017-10-05 12:46:24 +00:00
|
|
|
#include "virtio_user/vhost.h"
|
2017-08-30 16:06:04 +00:00
|
|
|
#include "virtio_dev.h"
|
2017-05-30 21:13:50 +00:00
|
|
|
#include "virtio_pci.h"
|
|
|
|
#include "virtio_logs.h"
|
2017-08-31 16:18:31 +00:00
|
|
|
#include "virtio_queue.h"
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
static uint16_t
|
2017-09-01 17:33:53 +00:00
|
|
|
virtio_get_nr_vq(struct virtio_dev *dev)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
2017-09-01 17:33:53 +00:00
|
|
|
return dev->max_queues;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virtio_init_vring(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
int size = vq->vq_nentries;
|
|
|
|
struct vring *vr = &vq->vq_ring;
|
|
|
|
uint8_t *ring_mem = vq->vq_ring_virt_mem;
|
|
|
|
|
|
|
|
PMD_INIT_FUNC_TRACE();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reinitialise since virtio port might have been stopped and restarted
|
|
|
|
*/
|
|
|
|
memset(ring_mem, 0, vq->vq_ring_size);
|
|
|
|
vring_init(vr, size, ring_mem, VIRTIO_PCI_VRING_ALIGN);
|
|
|
|
vq->vq_used_cons_idx = 0;
|
|
|
|
vq->vq_desc_head_idx = 0;
|
|
|
|
vq->vq_avail_idx = 0;
|
|
|
|
vq->vq_desc_tail_idx = (uint16_t)(vq->vq_nentries - 1);
|
|
|
|
vq->vq_free_cnt = vq->vq_nentries;
|
|
|
|
memset(vq->vq_descx, 0, sizeof(struct vq_desc_extra) * vq->vq_nentries);
|
|
|
|
|
|
|
|
vring_desc_init(vr->desc, size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disable device(host) interrupting guest
|
|
|
|
*/
|
|
|
|
virtqueue_disable_intr(vq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-09-01 17:33:53 +00:00
|
|
|
virtio_init_queue(struct virtio_dev *dev, uint16_t vtpci_queue_idx)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
|
|
|
char vq_name[VIRTQUEUE_MAX_NAME_SZ];
|
|
|
|
const struct rte_memzone *mz = NULL;
|
|
|
|
unsigned int vq_size, size;
|
|
|
|
struct virtqueue *vq;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
PMD_INIT_LOG(DEBUG, "setting up queue: %u", vtpci_queue_idx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the virtqueue size from the Queue Size field
|
|
|
|
* Always power of 2 and if 0 virtqueue does not exist
|
|
|
|
*/
|
2017-10-04 11:12:00 +00:00
|
|
|
vq_size = vtpci_ops(dev)->get_queue_num(dev, vtpci_queue_idx);
|
2017-05-30 21:13:50 +00:00
|
|
|
PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
|
|
|
|
if (vq_size == 0) {
|
|
|
|
PMD_INIT_LOG(ERR, "virtqueue does not exist");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rte_is_power_of_2(vq_size)) {
|
|
|
|
PMD_INIT_LOG(ERR, "virtqueue size is not powerof 2");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-10-04 11:19:30 +00:00
|
|
|
snprintf(vq_name, sizeof(vq_name), "dev%d_vq%d",
|
|
|
|
dev->id, vtpci_queue_idx);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
size = RTE_ALIGN_CEIL(sizeof(*vq) +
|
|
|
|
vq_size * sizeof(struct vq_desc_extra),
|
|
|
|
RTE_CACHE_LINE_SIZE);
|
|
|
|
|
|
|
|
vq = rte_zmalloc_socket(vq_name, size, RTE_CACHE_LINE_SIZE,
|
|
|
|
SOCKET_ID_ANY);
|
|
|
|
if (vq == NULL) {
|
|
|
|
PMD_INIT_LOG(ERR, "can not allocate vq");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2017-09-01 17:33:53 +00:00
|
|
|
dev->vqs[vtpci_queue_idx] = vq;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
vq->vdev = dev;
|
2017-05-30 21:13:50 +00:00
|
|
|
vq->vq_queue_index = vtpci_queue_idx;
|
|
|
|
vq->vq_nentries = vq_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reserve a memzone for vring elements
|
|
|
|
*/
|
|
|
|
size = vring_size(vq_size, VIRTIO_PCI_VRING_ALIGN);
|
|
|
|
vq->vq_ring_size = RTE_ALIGN_CEIL(size, VIRTIO_PCI_VRING_ALIGN);
|
|
|
|
PMD_INIT_LOG(DEBUG, "vring_size: %d, rounded_vring_size: %d",
|
|
|
|
size, vq->vq_ring_size);
|
|
|
|
|
|
|
|
mz = rte_memzone_reserve_aligned(vq_name, vq->vq_ring_size,
|
|
|
|
SOCKET_ID_ANY,
|
|
|
|
0, VIRTIO_PCI_VRING_ALIGN);
|
|
|
|
if (mz == NULL) {
|
|
|
|
if (rte_errno == EEXIST)
|
|
|
|
mz = rte_memzone_lookup(vq_name);
|
|
|
|
if (mz == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail_q_alloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(mz->addr, 0, sizeof(mz->len));
|
|
|
|
|
|
|
|
vq->vq_ring_mem = mz->phys_addr;
|
|
|
|
vq->vq_ring_virt_mem = mz->addr;
|
|
|
|
PMD_INIT_LOG(DEBUG, "vq->vq_ring_mem: 0x%" PRIx64,
|
|
|
|
(uint64_t)mz->phys_addr);
|
|
|
|
PMD_INIT_LOG(DEBUG, "vq->vq_ring_virt_mem: 0x%" PRIx64,
|
|
|
|
(uint64_t)(uintptr_t)mz->addr);
|
|
|
|
|
|
|
|
virtio_init_vring(vq);
|
|
|
|
|
2017-08-21 13:42:00 +00:00
|
|
|
vq->mz = mz;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-10-04 11:12:00 +00:00
|
|
|
if (vtpci_ops(dev)->setup_queue(dev, vq) < 0) {
|
2017-05-30 21:13:50 +00:00
|
|
|
PMD_INIT_LOG(ERR, "setup_queue failed");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail_q_alloc:
|
|
|
|
rte_memzone_free(mz);
|
|
|
|
rte_free(vq);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2017-09-01 17:33:53 +00:00
|
|
|
virtio_free_queues(struct virtio_dev *dev)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
2017-09-01 17:33:53 +00:00
|
|
|
uint16_t nr_vq = virtio_get_nr_vq(dev);
|
2017-05-30 21:13:50 +00:00
|
|
|
struct virtqueue *vq;
|
|
|
|
uint16_t i;
|
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
if (dev->vqs == NULL)
|
2017-05-30 21:13:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_vq; i++) {
|
2017-09-01 17:33:53 +00:00
|
|
|
vq = dev->vqs[i];
|
2017-05-30 21:13:50 +00:00
|
|
|
if (!vq)
|
|
|
|
continue;
|
|
|
|
|
2017-08-21 13:42:00 +00:00
|
|
|
rte_memzone_free(vq->mz);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
rte_free(vq);
|
2017-09-01 17:33:53 +00:00
|
|
|
dev->vqs[i] = NULL;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
rte_free(dev->vqs);
|
|
|
|
dev->vqs = NULL;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-09-01 17:33:53 +00:00
|
|
|
virtio_alloc_queues(struct virtio_dev *dev)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
2017-09-01 17:33:53 +00:00
|
|
|
uint16_t nr_vq = virtio_get_nr_vq(dev);
|
2017-05-30 21:13:50 +00:00
|
|
|
uint16_t i;
|
|
|
|
int ret;
|
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
dev->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
|
|
|
|
if (!dev->vqs) {
|
2017-05-30 21:13:50 +00:00
|
|
|
PMD_INIT_LOG(ERR, "failed to allocate vqs");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nr_vq; i++) {
|
2017-09-01 17:33:53 +00:00
|
|
|
ret = virtio_init_queue(dev, i);
|
2017-05-30 21:13:50 +00:00
|
|
|
if (ret < 0) {
|
2017-09-01 17:33:53 +00:00
|
|
|
virtio_free_queues(dev);
|
2017-05-30 21:13:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-04 13:54:42 +00:00
|
|
|
/* Negotiate virtio features. This will also set dev->modern if virtio
|
|
|
|
* device offers VIRTIO_F_VERSION_1 flag. If dev->modern has been set before,
|
|
|
|
* the mentioned flag must be offered. Otherwise an error is returned.
|
|
|
|
*/
|
2017-05-30 21:13:50 +00:00
|
|
|
static int
|
2017-09-01 17:33:53 +00:00
|
|
|
virtio_negotiate_features(struct virtio_dev *dev, uint64_t req_features)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
2017-10-05 12:46:24 +00:00
|
|
|
uint64_t host_features = vtpci_ops(dev)->get_features(dev);
|
|
|
|
int rc;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-10-05 12:46:24 +00:00
|
|
|
PMD_INIT_LOG(DEBUG, "guest features = %" PRIx64, req_features);
|
|
|
|
PMD_INIT_LOG(DEBUG, "device features = %" PRIx64, host_features);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-10-05 12:46:24 +00:00
|
|
|
rc = vtpci_ops(dev)->set_features(dev, req_features & host_features);
|
|
|
|
if (rc != 0) {
|
|
|
|
PMD_INIT_LOG(ERR, "failed to negotiate device features.");
|
|
|
|
return -1;
|
2017-09-04 13:54:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:46:24 +00:00
|
|
|
PMD_INIT_LOG(DEBUG, "negotiated features = %" PRIx64, dev->negotiated_features);
|
|
|
|
|
2017-10-04 10:19:04 +00:00
|
|
|
vtpci_set_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
|
|
|
|
if (!(vtpci_get_status(dev) & VIRTIO_CONFIG_S_FEATURES_OK)) {
|
2017-09-04 13:54:42 +00:00
|
|
|
PMD_INIT_LOG(ERR,
|
|
|
|
"failed to set FEATURES_OK status!");
|
|
|
|
return -1;
|
2017-05-30 21:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reset device and renegotiate features if needed */
|
2017-08-31 15:04:08 +00:00
|
|
|
int
|
2017-09-29 11:03:45 +00:00
|
|
|
virtio_dev_init(struct virtio_dev *dev, uint64_t req_features)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Reset the device although not necessary at startup */
|
2017-09-01 17:33:53 +00:00
|
|
|
vtpci_reset(dev);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
/* Tell the host we've noticed this device. */
|
2017-10-04 10:19:04 +00:00
|
|
|
vtpci_set_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
/* Tell the host we've known how to drive the device. */
|
2017-10-04 10:19:04 +00:00
|
|
|
vtpci_set_status(dev, VIRTIO_CONFIG_S_DRIVER);
|
2017-09-01 17:33:53 +00:00
|
|
|
if (virtio_negotiate_features(dev, req_features) < 0)
|
2017-05-30 21:13:50 +00:00
|
|
|
return -1;
|
|
|
|
|
2017-09-01 07:30:31 +00:00
|
|
|
/* FIXME
|
|
|
|
* Hardcode num_queues to 3 until we add proper
|
|
|
|
* mutli-queue support. This value should be limited
|
|
|
|
* by number of cores assigned to SPDK
|
|
|
|
*/
|
2017-09-01 17:33:53 +00:00
|
|
|
dev->max_queues = 3;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
ret = virtio_alloc_queues(dev);
|
2017-05-30 21:13:50 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
vtpci_reinit_complete(dev);
|
2017-05-30 21:13:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-29 11:03:45 +00:00
|
|
|
void
|
|
|
|
virtio_dev_free(struct virtio_dev *dev)
|
|
|
|
{
|
2017-10-04 11:19:30 +00:00
|
|
|
uint32_t vdev_id = dev->id;
|
|
|
|
|
2017-10-03 18:58:58 +00:00
|
|
|
vtpci_reset(dev);
|
2017-09-29 11:03:45 +00:00
|
|
|
virtio_free_queues(dev);
|
2017-10-04 11:12:00 +00:00
|
|
|
vtpci_ops(dev)->free_vdev(dev);
|
2017-10-04 11:19:30 +00:00
|
|
|
vtpci_deinit(vdev_id);
|
2017-09-29 11:03:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 21:13:50 +00:00
|
|
|
int
|
2017-09-01 17:33:53 +00:00
|
|
|
virtio_dev_start(struct virtio_dev *vdev)
|
2017-05-30 21:13:50 +00:00
|
|
|
{
|
|
|
|
struct virtnet_tx *txvq __rte_unused;
|
|
|
|
|
|
|
|
/* Enable uio/vfio intr/eventfd mapping: althrough we already did that
|
|
|
|
* in device configure, but it could be unmapped when device is
|
|
|
|
* stopped.
|
|
|
|
*/
|
|
|
|
/** TODO: interrupt handling for virtio_scsi */
|
|
|
|
#if 0
|
|
|
|
if (dev->data->dev_conf.intr_conf.lsc ||
|
|
|
|
dev->data->dev_conf.intr_conf.rxq) {
|
|
|
|
rte_intr_disable(dev->intr_handle);
|
|
|
|
|
|
|
|
if (rte_intr_enable(dev->intr_handle) < 0) {
|
|
|
|
PMD_DRV_LOG(ERR, "interrupt enable failed");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PMD_INIT_LOG(DEBUG, "Notified backend at initialization");
|
|
|
|
|
2017-09-01 17:33:53 +00:00
|
|
|
vdev->started = 1;
|
2017-05-30 21:13:50 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|