From 5bea1429e978678c8039c47d18ee6730636817a6 Mon Sep 17 00:00:00 2001 From: Dariusz Stojaczyk Date: Thu, 21 Sep 2017 19:44:03 +0200 Subject: [PATCH] rte_virtio: use spdk_pci_device where possible A mid-step towards porting virtio_pci to use SPDK env/pci layer. See the next patch for details. Change-Id: Ia7cb417415bce686c3a888f949853834ddf6c7a6 Signed-off-by: Dariusz Stojaczyk Reviewed-on: https://review.gerrithub.io/379488 Reviewed-by: Daniel Verkamp Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris --- lib/bdev/virtio/rte_virtio/virtio_dev.c | 2 +- lib/bdev/virtio/rte_virtio/virtio_pci.c | 29 +++++++++++++++---------- lib/bdev/virtio/rte_virtio/virtio_pci.h | 5 +++-- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/bdev/virtio/rte_virtio/virtio_dev.c b/lib/bdev/virtio/rte_virtio/virtio_dev.c index 040f5259c..793090386 100644 --- a/lib/bdev/virtio/rte_virtio/virtio_dev.c +++ b/lib/bdev/virtio/rte_virtio/virtio_dev.c @@ -381,7 +381,7 @@ static int virtio_pci_probe(struct rte_pci_driver *pci_drv __rte_unused, hw = calloc(1, sizeof(*hw)); hw->vdev.is_hw = 1; - hw->pci_dev = pci_dev; + hw->pci_dev = (struct spdk_pci_device *) pci_dev; g_pci_hw = hw; diff --git a/lib/bdev/virtio/rte_virtio/virtio_pci.c b/lib/bdev/virtio/rte_virtio/virtio_pci.c index 8d670af80..e92a10765 100644 --- a/lib/bdev/virtio/rte_virtio/virtio_pci.c +++ b/lib/bdev/virtio/rte_virtio/virtio_pci.c @@ -572,12 +572,13 @@ vtpci_isr(struct virtio_dev *dev) } static void * -get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap) +get_cfg_addr(struct spdk_pci_device *dev, struct virtio_pci_cap *cap) { uint8_t bar = cap->bar; uint32_t length = cap->length; uint32_t offset = cap->offset; - uint8_t *base; + uint8_t *bar_vaddr; + uint64_t bar_paddr, bar_len; if (bar > 5) { PMD_INIT_LOG(ERR, "invalid bar: %u", bar); @@ -590,37 +591,43 @@ get_cfg_addr(struct rte_pci_device *dev, struct virtio_pci_cap *cap) return NULL; } - if (offset + length > dev->mem_resource[bar].len) { + spdk_pci_device_map_bar(dev, bar, (void *) &bar_vaddr, &bar_paddr, &bar_len); + /* FIXME the bar should be also unmapped. + * However, current spdk_pci_device_map_bar + * implementation doesn't alloc anything, + * so there's no leak whatsoever. + */ + + if (offset + length > bar_len) { PMD_INIT_LOG(ERR, "invalid cap: overflows bar space: %u > %" PRIu64, offset + length, dev->mem_resource[bar].len); return NULL; } - base = dev->mem_resource[bar].addr; - if (base == NULL) { + if (bar_vaddr == NULL) { PMD_INIT_LOG(ERR, "bar %u base addr is NULL", bar); return NULL; } - return base + offset; + return bar_vaddr + offset; } static int -virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw) +virtio_read_caps(struct spdk_pci_device *dev, struct virtio_hw *hw) { uint8_t pos; struct virtio_pci_cap cap; int ret; - ret = rte_pci_read_config(dev, &pos, 1, PCI_CAPABILITY_LIST); + ret = spdk_pci_device_cfg_read(dev, &pos, 1, PCI_CAPABILITY_LIST); if (ret < 0) { PMD_INIT_LOG(DEBUG, "failed to read pci capability list"); return -1; } while (pos) { - ret = rte_pci_read_config(dev, &cap, sizeof(cap), pos); + ret = spdk_pci_device_cfg_read(dev, &cap, sizeof(cap), pos); if (ret < 0) { PMD_INIT_LOG(ERR, "failed to read pci cap at pos: %x", pos); @@ -646,7 +653,7 @@ virtio_read_caps(struct rte_pci_device *dev, struct virtio_hw *hw) hw->common_cfg = get_cfg_addr(dev, &cap); break; case VIRTIO_PCI_CAP_NOTIFY_CFG: - rte_pci_read_config(dev, &hw->notify_off_multiplier, + spdk_pci_device_cfg_read(dev, &hw->notify_off_multiplier, 4, pos + sizeof(cap)); hw->notify_base = get_cfg_addr(dev, &cap); break; @@ -688,7 +695,7 @@ next: * Return 0 on success. */ int -vtpci_init(struct rte_pci_device *dev, struct virtio_dev *vdev) +vtpci_init(struct spdk_pci_device *dev, struct virtio_dev *vdev) { struct virtio_hw *hw = virtio_dev_get_hw(vdev); diff --git a/lib/bdev/virtio/rte_virtio/virtio_pci.h b/lib/bdev/virtio/rte_virtio/virtio_pci.h index 5ba87f0d6..43272e324 100644 --- a/lib/bdev/virtio/rte_virtio/virtio_pci.h +++ b/lib/bdev/virtio/rte_virtio/virtio_pci.h @@ -38,6 +38,7 @@ #include +#include "spdk/env.h" #include "virtio_dev.h" struct virtqueue; @@ -218,7 +219,7 @@ struct virtio_hw { uint8_t *isr; uint16_t *notify_base; struct virtio_pci_common_cfg *common_cfg; - struct rte_pci_device *pci_dev; + struct spdk_pci_device *pci_dev; struct virtio_scsi_config *dev_cfg; }; @@ -255,7 +256,7 @@ vtpci_with_feature(struct virtio_dev *dev, uint64_t bit) /* * Function declaration from virtio_pci.c */ -int vtpci_init(struct rte_pci_device *dev, struct virtio_dev *vdev); +int vtpci_init(struct spdk_pci_device *dev, struct virtio_dev *vdev); void vtpci_reset(struct virtio_dev *); void vtpci_reinit_complete(struct virtio_dev *);