From 65a9ec5c6a98587366aca7eb2424666fd50d53f8 Mon Sep 17 00:00:00 2001 From: Asias He Date: Sun, 8 Apr 2012 06:48:20 +0800 Subject: [PATCH] kvm tools: Rework on virtio transport abstraction. This patch reworks on virtio transport abstraction. * Move virtio transport operations to virtio operations and drop virtio/trans.c This makes the abstraction much cleaner. * Rename struct virtio_trans to struct virtio_device struct virtio_trans { void *virtio; enum virtio_trans_type type; struct virtio_trans_ops *trans_ops; struct virtio_ops *virtio_ops; }; struct virtio_device { void *virtio; struct virtio_ops *ops; }; The virtio_trans struct is bit confusing since it also includes virtio operations. * Introduce virtio_init() To init device, e.g. Before: virtio_trans_init() ndev->vtrans.trans_ops->init() ndev->vtrans.virtio_ops = &net_dev_virtio_ops After: virtio_init() Signed-off-by: Asias He Signed-off-by: Pekka Enberg --- tools/kvm/Makefile | 1 - tools/kvm/include/kvm/virtio-9p.h | 3 +- tools/kvm/include/kvm/virtio-pci.h | 15 ++--- tools/kvm/include/kvm/virtio-trans.h | 44 ------------- tools/kvm/include/kvm/virtio.h | 32 +++++++++ tools/kvm/virtio/9p.c | 8 +-- tools/kvm/virtio/balloon.c | 17 ++--- tools/kvm/virtio/blk.c | 11 ++-- tools/kvm/virtio/console.c | 13 ++-- tools/kvm/virtio/core.c | 33 +++++++++- tools/kvm/virtio/net.c | 15 ++--- tools/kvm/virtio/pci.c | 97 ++++++++++++---------------- tools/kvm/virtio/rng.c | 14 ++-- tools/kvm/virtio/trans.c | 24 ------- 14 files changed, 142 insertions(+), 185 deletions(-) delete mode 100644 tools/kvm/include/kvm/virtio-trans.h delete mode 100644 tools/kvm/virtio/trans.c diff --git a/tools/kvm/Makefile b/tools/kvm/Makefile index 0a9c2ccfc000..38d4788b113a 100644 --- a/tools/kvm/Makefile +++ b/tools/kvm/Makefile @@ -59,7 +59,6 @@ OBJS += virtio/net.o OBJS += virtio/rng.o OBJS += virtio/balloon.o OBJS += virtio/pci.o -OBJS += virtio/trans.o OBJS += disk/blk.o OBJS += disk/qcow.o OBJS += disk/raw.o diff --git a/tools/kvm/include/kvm/virtio-9p.h b/tools/kvm/include/kvm/virtio-9p.h index ac3dc45976b4..fdf676c413f7 100644 --- a/tools/kvm/include/kvm/virtio-9p.h +++ b/tools/kvm/include/kvm/virtio-9p.h @@ -3,7 +3,6 @@ #include "kvm/virtio.h" #include "kvm/pci.h" #include "kvm/threadpool.h" -#include "kvm/virtio-trans.h" #include #include @@ -42,7 +41,7 @@ struct p9_dev_job { struct p9_dev { struct list_head list; - struct virtio_trans vtrans; + struct virtio_device vdev; struct virtio_9p_config *config; u32 features; diff --git a/tools/kvm/include/kvm/virtio-pci.h b/tools/kvm/include/kvm/virtio-pci.h index 7133314b8b3a..9e036b24ed97 100644 --- a/tools/kvm/include/kvm/virtio-pci.h +++ b/tools/kvm/include/kvm/virtio-pci.h @@ -2,7 +2,6 @@ #define KVM__VIRTIO_PCI_H #include "kvm/pci.h" -#include "kvm/virtio-trans.h" #include @@ -12,7 +11,7 @@ struct kvm; struct virtio_pci_ioevent_param { - struct virtio_trans *vtrans; + struct virtio_device *vdev; u32 vq; }; @@ -38,12 +37,10 @@ struct virtio_pci { struct virtio_pci_ioevent_param ioeventfds[VIRTIO_PCI_MAX_VQ]; }; -int virtio_pci__init(struct kvm *kvm, struct virtio_trans *vtrans, void *dev, - int device_id, int subsys_id, int class); -int virtio_pci__exit(struct kvm *kvm, struct virtio_trans *vtrans); -int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_trans *vtrans, u32 vq); -int virtio_pci__signal_config(struct kvm *kvm, struct virtio_trans *vtrans); - -struct virtio_trans_ops *virtio_pci__get_trans_ops(void); +int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq); +int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev); +int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev); +int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev, + int device_id, int subsys_id, int class); #endif diff --git a/tools/kvm/include/kvm/virtio-trans.h b/tools/kvm/include/kvm/virtio-trans.h deleted file mode 100644 index f2e433fd697d..000000000000 --- a/tools/kvm/include/kvm/virtio-trans.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef KVM__VIRTIO_TRANS_H -#define KVM__VIRTIO_TRANS_H - -#include "kvm/kvm.h" - -enum virtio_trans_type { - VIRTIO_PCI, -}; - -struct virtio_trans; - -struct virtio_ops { - void (*set_config)(struct kvm *kvm, void *dev, u8 data, u32 offset); - u8 (*get_config)(struct kvm *kvm, void *dev, u32 offset); - - u32 (*get_host_features)(struct kvm *kvm, void *dev); - void (*set_guest_features)(struct kvm *kvm, void *dev, u32 features); - - int (*init_vq)(struct kvm *kvm, void *dev, u32 vq, u32 pfn); - int (*notify_vq)(struct kvm *kvm, void *dev, u32 vq); - int (*get_pfn_vq)(struct kvm *kvm, void *dev, u32 vq); - int (*get_size_vq)(struct kvm *kvm, void *dev, u32 vq); - void (*notify_vq_gsi)(struct kvm *kvm, void *dev, u32 vq, u32 gsi); - void (*notify_vq_eventfd)(struct kvm *kvm, void *dev, u32 vq, u32 efd); -}; - -struct virtio_trans_ops { - int (*init)(struct kvm *kvm, struct virtio_trans *vtrans, void *dev, int device_id, - int subsys_id, int class); - int (*uninit)(struct kvm *kvm, struct virtio_trans *vtrans); - int (*signal_vq)(struct kvm *kvm, struct virtio_trans *virtio_trans, u32 queueid); - int (*signal_config)(struct kvm *kvm, struct virtio_trans *virtio_trans); -}; - -struct virtio_trans { - void *virtio; - enum virtio_trans_type type; - struct virtio_trans_ops *trans_ops; - struct virtio_ops *virtio_ops; -}; - -int virtio_trans_init(struct virtio_trans *vtrans, enum virtio_trans_type type); - -#endif \ No newline at end of file diff --git a/tools/kvm/include/kvm/virtio.h b/tools/kvm/include/kvm/virtio.h index 610f4d00fd68..db2a86e35415 100644 --- a/tools/kvm/include/kvm/virtio.h +++ b/tools/kvm/include/kvm/virtio.h @@ -64,4 +64,36 @@ u16 virt_queue__get_inout_iov(struct kvm *kvm, struct virt_queue *queue, u16 *in, u16 *out); int virtio__get_dev_specific_field(int offset, bool msix, u32 *config_off); +enum virtio_trans { + VIRTIO_PCI, + VIRTIO_MMIO, +}; + +struct virtio_device { + void *virtio; + struct virtio_ops *ops; +}; + +struct virtio_ops { + void (*set_config)(struct kvm *kvm, void *dev, u8 data, u32 offset); + u8 (*get_config)(struct kvm *kvm, void *dev, u32 offset); + u32 (*get_host_features)(struct kvm *kvm, void *dev); + void (*set_guest_features)(struct kvm *kvm, void *dev, u32 features); + int (*init_vq)(struct kvm *kvm, void *dev, u32 vq, u32 pfn); + int (*notify_vq)(struct kvm *kvm, void *dev, u32 vq); + int (*get_pfn_vq)(struct kvm *kvm, void *dev, u32 vq); + int (*get_size_vq)(struct kvm *kvm, void *dev, u32 vq); + int (*set_size_vq)(struct kvm *kvm, void *dev, u32 vq, int size); + void (*notify_vq_gsi)(struct kvm *kvm, void *dev, u32 vq, u32 gsi); + void (*notify_vq_eventfd)(struct kvm *kvm, void *dev, u32 vq, u32 efd); + int (*signal_vq)(struct kvm *kvm, struct virtio_device *vdev, u32 queueid); + int (*signal_config)(struct kvm *kvm, struct virtio_device *vdev); + int (*init)(struct kvm *kvm, void *dev, struct virtio_device *vdev, + int device_id, int subsys_id, int class); + int (*exit)(struct kvm *kvm, struct virtio_device *vdev); +}; + +int virtio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, + struct virtio_ops *ops, enum virtio_trans trans, + int device_id, int subsys_id, int class); #endif /* KVM__VIRTIO_H */ diff --git a/tools/kvm/virtio/9p.c b/tools/kvm/virtio/9p.c index 28f5ed794b28..fd6cf54363c3 100644 --- a/tools/kvm/virtio/9p.c +++ b/tools/kvm/virtio/9p.c @@ -1168,7 +1168,7 @@ static void virtio_p9_do_io(struct kvm *kvm, void *param) while (virt_queue__available(vq)) { virtio_p9_do_io_request(kvm, job); - p9dev->vtrans.trans_ops->signal_vq(kvm, &p9dev->vtrans, vq - p9dev->vqs); + p9dev->vdev.ops->signal_vq(kvm, &p9dev->vdev, vq - p9dev->vqs); } } @@ -1260,10 +1260,8 @@ int virtio_9p__init(struct kvm *kvm) struct p9_dev *p9dev; list_for_each_entry(p9dev, &devs, list) { - virtio_trans_init(&p9dev->vtrans, VIRTIO_PCI); - p9dev->vtrans.trans_ops->init(kvm, &p9dev->vtrans, p9dev, - PCI_DEVICE_ID_VIRTIO_P9, VIRTIO_ID_9P, PCI_CLASS_P9); - p9dev->vtrans.virtio_ops = &p9_dev_virtio_ops; + virtio_init(kvm, p9dev, &p9dev->vdev, &p9_dev_virtio_ops, + VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_P9, VIRTIO_ID_9P, PCI_CLASS_P9); } return 0; diff --git a/tools/kvm/virtio/balloon.c b/tools/kvm/virtio/balloon.c index 09044d0eb0e9..45bd8d342e78 100644 --- a/tools/kvm/virtio/balloon.c +++ b/tools/kvm/virtio/balloon.c @@ -8,7 +8,6 @@ #include "kvm/pci.h" #include "kvm/threadpool.h" #include "kvm/guest_compat.h" -#include "kvm/virtio-trans.h" #include "kvm/kvm-ipc.h" #include @@ -31,7 +30,7 @@ struct bln_dev { struct list_head list; - struct virtio_trans vtrans; + struct virtio_device vdev; u32 features; @@ -116,13 +115,13 @@ static void virtio_bln_do_io(struct kvm *kvm, void *param) if (vq == &bdev.vqs[VIRTIO_BLN_STATS]) { virtio_bln_do_stat_request(kvm, &bdev, vq); - bdev.vtrans.trans_ops->signal_vq(kvm, &bdev.vtrans, VIRTIO_BLN_STATS); + bdev.vdev.ops->signal_vq(kvm, &bdev.vdev, VIRTIO_BLN_STATS); return; } while (virt_queue__available(vq)) { virtio_bln_do_io_request(kvm, &bdev, vq); - bdev.vtrans.trans_ops->signal_vq(kvm, &bdev.vtrans, vq - bdev.vqs); + bdev.vdev.ops->signal_vq(kvm, &bdev.vdev, vq - bdev.vqs); } } @@ -132,7 +131,7 @@ static int virtio_bln__collect_stats(void) virt_queue__set_used_elem(&bdev.vqs[VIRTIO_BLN_STATS], bdev.cur_stat_head, sizeof(struct virtio_balloon_stat)); - bdev.vtrans.trans_ops->signal_vq(kvm, &bdev.vtrans, VIRTIO_BLN_STATS); + bdev.vdev.ops->signal_vq(kvm, &bdev.vdev, VIRTIO_BLN_STATS); if (read(bdev.stat_waitfd, &tmp, sizeof(tmp)) <= 0) return -EFAULT; @@ -173,7 +172,7 @@ static void handle_mem(int fd, u32 type, u32 len, u8 *msg) } /* Notify that the configuration space has changed */ - bdev.vtrans.trans_ops->signal_config(kvm, &bdev.vtrans); + bdev.vdev.ops->signal_config(kvm, &bdev.vdev); } static void set_config(struct kvm *kvm, void *dev, u8 data, u32 offset) @@ -260,10 +259,8 @@ void virtio_bln__init(struct kvm *kvm) bdev.stat_waitfd = eventfd(0, 0); memset(&bdev.config, 0, sizeof(struct virtio_balloon_config)); - virtio_trans_init(&bdev.vtrans, VIRTIO_PCI); - bdev.vtrans.trans_ops->init(kvm, &bdev.vtrans, &bdev, PCI_DEVICE_ID_VIRTIO_BLN, - VIRTIO_ID_BALLOON, PCI_CLASS_BLN); - bdev.vtrans.virtio_ops = &bln_dev_virtio_ops; + virtio_init(kvm, &bdev, &bdev.vdev, &bln_dev_virtio_ops, + VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_BLN, VIRTIO_ID_BALLOON, PCI_CLASS_BLN); if (compat_id != -1) compat_id = compat__add_message("virtio-balloon device was not detected", diff --git a/tools/kvm/virtio/blk.c b/tools/kvm/virtio/blk.c index c230dd463475..f3cb6cf10876 100644 --- a/tools/kvm/virtio/blk.c +++ b/tools/kvm/virtio/blk.c @@ -11,7 +11,6 @@ #include "kvm/guest_compat.h" #include "kvm/virtio-pci.h" #include "kvm/virtio.h" -#include "kvm/virtio-trans.h" #include #include @@ -44,7 +43,7 @@ struct blk_dev { struct list_head list; struct list_head req_list; - struct virtio_trans vtrans; + struct virtio_device vdev; struct virtio_blk_config blk_config; struct disk_image *disk; u32 features; @@ -72,7 +71,7 @@ void virtio_blk_complete(void *param, long len) mutex_unlock(&bdev->mutex); if (virtio_queue__should_signal(&bdev->vqs[queueid])) - bdev->vtrans.trans_ops->signal_vq(req->kvm, &bdev->vtrans, queueid); + bdev->vdev.ops->signal_vq(req->kvm, &bdev->vdev, queueid); } static void virtio_blk_do_io_request(struct kvm *kvm, struct blk_dev_req *req) @@ -230,10 +229,8 @@ static int virtio_blk__init_one(struct kvm *kvm, struct disk_image *disk) }, }; - virtio_trans_init(&bdev->vtrans, VIRTIO_PCI); - bdev->vtrans.trans_ops->init(kvm, &bdev->vtrans, bdev, PCI_DEVICE_ID_VIRTIO_BLK, - VIRTIO_ID_BLOCK, PCI_CLASS_BLK); - bdev->vtrans.virtio_ops = &blk_dev_virtio_ops; + virtio_init(kvm, bdev, &bdev->vdev, &blk_dev_virtio_ops, + VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_BLK, VIRTIO_ID_BLOCK, PCI_CLASS_BLK); list_add_tail(&bdev->list, &bdevs); diff --git a/tools/kvm/virtio/console.c b/tools/kvm/virtio/console.c index 245cebb72edd..173c312c950b 100644 --- a/tools/kvm/virtio/console.c +++ b/tools/kvm/virtio/console.c @@ -11,7 +11,6 @@ #include "kvm/threadpool.h" #include "kvm/irq.h" #include "kvm/guest_compat.h" -#include "kvm/virtio-trans.h" #include #include @@ -32,7 +31,7 @@ struct con_dev { pthread_mutex_t mutex; - struct virtio_trans vtrans; + struct virtio_device vdev; struct virt_queue vqs[VIRTIO_CONSOLE_NUM_QUEUES]; struct virtio_console_config config; u32 features; @@ -71,7 +70,7 @@ static void virtio_console__inject_interrupt_callback(struct kvm *kvm, void *par head = virt_queue__get_iov(vq, iov, &out, &in, kvm); len = term_getc_iov(CONSOLE_VIRTIO, iov, in, 0); virt_queue__set_used_elem(vq, head, len); - cdev.vtrans.trans_ops->signal_vq(kvm, &cdev.vtrans, vq - cdev.vqs); + cdev.vdev.ops->signal_vq(kvm, &cdev.vdev, vq - cdev.vqs); } mutex_unlock(&cdev.mutex); @@ -187,12 +186,8 @@ static struct virtio_ops con_dev_virtio_ops = (struct virtio_ops) { void virtio_console__init(struct kvm *kvm) { - virtio_trans_init(&cdev.vtrans, VIRTIO_PCI); - - cdev.vtrans.trans_ops->init(kvm, &cdev.vtrans, &cdev, PCI_DEVICE_ID_VIRTIO_CONSOLE, - VIRTIO_ID_CONSOLE, PCI_CLASS_CONSOLE); - cdev.vtrans.virtio_ops = &con_dev_virtio_ops; - + virtio_init(kvm, &cdev, &cdev.vdev, &con_dev_virtio_ops, + VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_CONSOLE, VIRTIO_ID_CONSOLE, PCI_CLASS_CONSOLE); if (compat_id != -1) compat_id = compat__add_message("virtio-console device was not detected", "While you have requested a virtio-console device, " diff --git a/tools/kvm/virtio/core.c b/tools/kvm/virtio/core.c index c68572cf02d7..20b90be9154f 100644 --- a/tools/kvm/virtio/core.c +++ b/tools/kvm/virtio/core.c @@ -1,11 +1,14 @@ #include #include #include +#include #include "kvm/barrier.h" - -#include "kvm/kvm.h" #include "kvm/virtio.h" +#include "kvm/virtio-pci.h" +#include "kvm/util.h" +#include "kvm/kvm.h" + struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, u32 head, u32 len) { @@ -156,3 +159,29 @@ bool virtio_queue__should_signal(struct virt_queue *vq) return false; } + +int virtio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, + struct virtio_ops *ops, enum virtio_trans trans, + int device_id, int subsys_id, int class) +{ + void *virtio; + + switch (trans) { + case VIRTIO_PCI: + virtio = calloc(sizeof(struct virtio_pci), 1); + if (!virtio) + return -ENOMEM; + vdev->virtio = virtio; + vdev->ops = ops; + vdev->ops->signal_vq = virtio_pci__signal_vq; + vdev->ops->signal_config = virtio_pci__signal_config; + vdev->ops->init = virtio_pci__init; + vdev->ops->exit = virtio_pci__exit; + vdev->ops->init(kvm, dev, vdev, device_id, subsys_id, class); + break; + default: + return -1; + }; + + return 0; +} diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c index 5e297089f093..33bf6238e81a 100644 --- a/tools/kvm/virtio/net.c +++ b/tools/kvm/virtio/net.c @@ -8,7 +8,6 @@ #include "kvm/irq.h" #include "kvm/uip.h" #include "kvm/guest_compat.h" -#include "kvm/virtio-trans.h" #include #include @@ -43,7 +42,7 @@ struct net_dev_operations { struct net_dev { pthread_mutex_t mutex; - struct virtio_trans vtrans; + struct virtio_device vdev; struct list_head list; struct virt_queue vqs[VIRTIO_NET_NUM_QUEUES]; @@ -98,8 +97,8 @@ static void *virtio_net_rx_thread(void *p) /* We should interrupt guest right now, otherwise latency is huge. */ if (virtio_queue__should_signal(&ndev->vqs[VIRTIO_NET_RX_QUEUE])) - ndev->vtrans.trans_ops->signal_vq(kvm, &ndev->vtrans, - VIRTIO_NET_RX_QUEUE); + ndev->vdev.ops->signal_vq(kvm, &ndev->vdev, + VIRTIO_NET_RX_QUEUE); } } @@ -134,7 +133,7 @@ static void *virtio_net_tx_thread(void *p) } if (virtio_queue__should_signal(&ndev->vqs[VIRTIO_NET_TX_QUEUE])) - ndev->vtrans.trans_ops->signal_vq(kvm, &ndev->vtrans, VIRTIO_NET_TX_QUEUE); + ndev->vdev.ops->signal_vq(kvm, &ndev->vdev, VIRTIO_NET_TX_QUEUE); } pthread_exit(NULL); @@ -527,10 +526,8 @@ void virtio_net__init(const struct virtio_net_params *params) ndev->ops = &uip_ops; } - virtio_trans_init(&ndev->vtrans, VIRTIO_PCI); - ndev->vtrans.trans_ops->init(kvm, &ndev->vtrans, ndev, PCI_DEVICE_ID_VIRTIO_NET, - VIRTIO_ID_NET, PCI_CLASS_NET); - ndev->vtrans.virtio_ops = &net_dev_virtio_ops; + virtio_init(kvm, ndev, &ndev->vdev, &net_dev_virtio_ops, + VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_NET, VIRTIO_ID_NET, PCI_CLASS_NET); if (params->vhost) virtio_net__vhost_init(params->kvm, ndev); diff --git a/tools/kvm/virtio/pci.c b/tools/kvm/virtio/pci.c index 529e30dbd73e..bdc09f5cfb25 100644 --- a/tools/kvm/virtio/pci.c +++ b/tools/kvm/virtio/pci.c @@ -6,39 +6,27 @@ #include "kvm/irq.h" #include "kvm/virtio.h" #include "kvm/ioeventfd.h" -#include "kvm/virtio-trans.h" #include #include #include -struct virtio_trans_ops *virtio_pci__get_trans_ops(void) -{ - static struct virtio_trans_ops virtio_pci_trans = (struct virtio_trans_ops) { - .signal_vq = virtio_pci__signal_vq, - .signal_config = virtio_pci__signal_config, - .init = virtio_pci__init, - .uninit = virtio_pci__exit, - }; - return &virtio_pci_trans; -}; - static void virtio_pci__ioevent_callback(struct kvm *kvm, void *param) { struct virtio_pci_ioevent_param *ioeventfd = param; - struct virtio_pci *vpci = ioeventfd->vtrans->virtio; + struct virtio_pci *vpci = ioeventfd->vdev->virtio; - ioeventfd->vtrans->virtio_ops->notify_vq(kvm, vpci->dev, ioeventfd->vq); + ioeventfd->vdev->ops->notify_vq(kvm, vpci->dev, ioeventfd->vq); } -static int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_trans *vtrans, u32 vq) +static int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_device *vdev, u32 vq) { struct ioevent ioevent; - struct virtio_pci *vpci = vtrans->virtio; + struct virtio_pci *vpci = vdev->virtio; int r; vpci->ioeventfds[vq] = (struct virtio_pci_ioevent_param) { - .vtrans = vtrans, + .vdev = vdev, .vq = vq, }; @@ -56,8 +44,8 @@ static int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_trans *vtra if (r) return r; - if (vtrans->virtio_ops->notify_vq_eventfd) - vtrans->virtio_ops->notify_vq_eventfd(kvm, vpci->dev, vq, ioevent.fd); + if (vdev->ops->notify_vq_eventfd) + vdev->ops->notify_vq_eventfd(kvm, vpci->dev, vq, ioevent.fd); return 0; } @@ -67,11 +55,11 @@ static inline bool virtio_pci__msix_enabled(struct virtio_pci *vpci) return vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_ENABLE); } -static bool virtio_pci__specific_io_in(struct kvm *kvm, struct virtio_trans *vtrans, u16 port, +static bool virtio_pci__specific_io_in(struct kvm *kvm, struct virtio_device *vdev, u16 port, void *data, int size, int offset) { u32 config_offset; - struct virtio_pci *vpci = vtrans->virtio; + struct virtio_pci *vpci = vdev->virtio; int type = virtio__get_dev_specific_field(offset - 20, virtio_pci__msix_enabled(vpci), &config_offset); @@ -89,7 +77,7 @@ static bool virtio_pci__specific_io_in(struct kvm *kvm, struct virtio_trans *vtr } else if (type == VIRTIO_PCI_O_CONFIG) { u8 cfg; - cfg = vtrans->virtio_ops->get_config(kvm, vpci->dev, config_offset); + cfg = vdev->ops->get_config(kvm, vpci->dev, config_offset); ioport__write8(data, cfg); return true; } @@ -101,25 +89,25 @@ static bool virtio_pci__io_in(struct ioport *ioport, struct kvm *kvm, u16 port, { unsigned long offset; bool ret = true; - struct virtio_trans *vtrans; + struct virtio_device *vdev; struct virtio_pci *vpci; u32 val; - vtrans = ioport->priv; - vpci = vtrans->virtio; + vdev = ioport->priv; + vpci = vdev->virtio; offset = port - vpci->base_addr; switch (offset) { case VIRTIO_PCI_HOST_FEATURES: - val = vtrans->virtio_ops->get_host_features(kvm, vpci->dev); + val = vdev->ops->get_host_features(kvm, vpci->dev); ioport__write32(data, val); break; case VIRTIO_PCI_QUEUE_PFN: - val = vtrans->virtio_ops->get_pfn_vq(kvm, vpci->dev, vpci->queue_selector); + val = vdev->ops->get_pfn_vq(kvm, vpci->dev, vpci->queue_selector); ioport__write32(data, val); break; case VIRTIO_PCI_QUEUE_NUM: - val = vtrans->virtio_ops->get_size_vq(kvm, vpci->dev, vpci->queue_selector); + val = vdev->ops->get_size_vq(kvm, vpci->dev, vpci->queue_selector); ioport__write16(data, val); break; case VIRTIO_PCI_STATUS: @@ -131,17 +119,17 @@ static bool virtio_pci__io_in(struct ioport *ioport, struct kvm *kvm, u16 port, vpci->isr = VIRTIO_IRQ_LOW; break; default: - ret = virtio_pci__specific_io_in(kvm, vtrans, port, data, size, offset); + ret = virtio_pci__specific_io_in(kvm, vdev, port, data, size, offset); break; }; return ret; } -static bool virtio_pci__specific_io_out(struct kvm *kvm, struct virtio_trans *vtrans, u16 port, +static bool virtio_pci__specific_io_out(struct kvm *kvm, struct virtio_device *vdev, u16 port, void *data, int size, int offset) { - struct virtio_pci *vpci = vtrans->virtio; + struct virtio_pci *vpci = vdev->virtio; u32 config_offset, gsi, vec; int type = virtio__get_dev_specific_field(offset - 20, virtio_pci__msix_enabled(vpci), &config_offset); @@ -159,15 +147,15 @@ static bool virtio_pci__specific_io_out(struct kvm *kvm, struct virtio_trans *vt gsi = irq__add_msix_route(kvm, &vpci->msix_table[vec].msg); vpci->gsis[vpci->queue_selector] = gsi; - if (vtrans->virtio_ops->notify_vq_gsi) - vtrans->virtio_ops->notify_vq_gsi(kvm, vpci->dev, + if (vdev->ops->notify_vq_gsi) + vdev->ops->notify_vq_gsi(kvm, vpci->dev, vpci->queue_selector, gsi); break; }; return true; } else if (type == VIRTIO_PCI_O_CONFIG) { - vtrans->virtio_ops->set_config(kvm, vpci->dev, *(u8 *)data, config_offset); + vdev->ops->set_config(kvm, vpci->dev, *(u8 *)data, config_offset); return true; } @@ -179,36 +167,36 @@ static bool virtio_pci__io_out(struct ioport *ioport, struct kvm *kvm, u16 port, { unsigned long offset; bool ret = true; - struct virtio_trans *vtrans; + struct virtio_device *vdev; struct virtio_pci *vpci; u32 val; - vtrans = ioport->priv; - vpci = vtrans->virtio; + vdev = ioport->priv; + vpci = vdev->virtio; offset = port - vpci->base_addr; switch (offset) { case VIRTIO_PCI_GUEST_FEATURES: val = ioport__read32(data); - vtrans->virtio_ops->set_guest_features(kvm, vpci->dev, val); + vdev->ops->set_guest_features(kvm, vpci->dev, val); break; case VIRTIO_PCI_QUEUE_PFN: val = ioport__read32(data); - virtio_pci__init_ioeventfd(kvm, vtrans, vpci->queue_selector); - vtrans->virtio_ops->init_vq(kvm, vpci->dev, vpci->queue_selector, val); + virtio_pci__init_ioeventfd(kvm, vdev, vpci->queue_selector); + vdev->ops->init_vq(kvm, vpci->dev, vpci->queue_selector, val); break; case VIRTIO_PCI_QUEUE_SEL: vpci->queue_selector = ioport__read16(data); break; case VIRTIO_PCI_QUEUE_NOTIFY: val = ioport__read16(data); - vtrans->virtio_ops->notify_vq(kvm, vpci->dev, val); + vdev->ops->notify_vq(kvm, vpci->dev, val); break; case VIRTIO_PCI_STATUS: vpci->status = ioport__read8(data); break; default: - ret = virtio_pci__specific_io_out(kvm, vtrans, port, data, size, offset); + ret = virtio_pci__specific_io_out(kvm, vdev, port, data, size, offset); break; }; @@ -240,9 +228,9 @@ static void callback_mmio_table(u64 addr, u8 *data, u32 len, u8 is_write, void * memcpy(data, table + addr - offset, len); } -int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_trans *vtrans, u32 vq) +int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq) { - struct virtio_pci *vpci = vtrans->virtio; + struct virtio_pci *vpci = vdev->virtio; int tbl = vpci->vq_vector[vq]; if (virtio_pci__msix_enabled(vpci)) { @@ -261,9 +249,9 @@ int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_trans *vtrans, u32 vq) return 0; } -int virtio_pci__signal_config(struct kvm *kvm, struct virtio_trans *vtrans) +int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev) { - struct virtio_pci *vpci = vtrans->virtio; + struct virtio_pci *vpci = vdev->virtio; int tbl = vpci->config_vector; if (virtio_pci__msix_enabled(vpci)) { @@ -283,22 +271,23 @@ int virtio_pci__signal_config(struct kvm *kvm, struct virtio_trans *vtrans) return 0; } -int virtio_pci__init(struct kvm *kvm, struct virtio_trans *vtrans, void *dev, - int device_id, int subsys_id, int class) +int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev, + int device_id, int subsys_id, int class) { - struct virtio_pci *vpci = vtrans->virtio; + struct virtio_pci *vpci = vdev->virtio; u8 pin, line, ndev; int r; vpci->dev = dev; vpci->msix_io_block = pci_get_io_space_block(PCI_IO_SIZE * 2); - r = ioport__register(IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vtrans); + r = ioport__register(IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vdev); if (r < 0) return r; vpci->base_addr = (u16)r; - r = kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE, false, callback_mmio_table, vpci); + r = kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE, false, + callback_mmio_table, vpci); if (r < 0) goto free_ioport; @@ -365,9 +354,9 @@ free_ioport: return r; } -int virtio_pci__exit(struct kvm *kvm, struct virtio_trans *vtrans) +int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev) { - struct virtio_pci *vpci = vtrans->virtio; + struct virtio_pci *vpci = vdev->virtio; int i; kvm__deregister_mmio(kvm, vpci->msix_io_block); @@ -377,4 +366,4 @@ int virtio_pci__exit(struct kvm *kvm, struct virtio_trans *vtrans) ioeventfd__del_event(vpci->base_addr + VIRTIO_PCI_QUEUE_NOTIFY, i); return 0; -} \ No newline at end of file +} diff --git a/tools/kvm/virtio/rng.c b/tools/kvm/virtio/rng.c index a942ebdd7f13..105a4298406c 100644 --- a/tools/kvm/virtio/rng.c +++ b/tools/kvm/virtio/rng.c @@ -7,7 +7,6 @@ #include "kvm/kvm.h" #include "kvm/threadpool.h" #include "kvm/guest_compat.h" -#include "kvm/virtio-trans.h" #include #include @@ -30,7 +29,7 @@ struct rng_dev_job { struct rng_dev { struct list_head list; - struct virtio_trans vtrans; + struct virtio_device vdev; int fd; @@ -87,7 +86,7 @@ static void virtio_rng_do_io(struct kvm *kvm, void *param) while (virt_queue__available(vq)) virtio_rng_do_io_request(kvm, rdev, vq); - rdev->vtrans.trans_ops->signal_vq(kvm, &rdev->vtrans, vq - rdev->vqs); + rdev->vdev.ops->signal_vq(kvm, &rdev->vdev, vq - rdev->vqs); } static int init_vq(struct kvm *kvm, void *dev, u32 vq, u32 pfn) @@ -164,14 +163,11 @@ int virtio_rng__init(struct kvm *kvm) goto cleanup; } - virtio_trans_init(&rdev->vtrans, VIRTIO_PCI); - r = rdev->vtrans.trans_ops->init(kvm, &rdev->vtrans, rdev, PCI_DEVICE_ID_VIRTIO_RNG, - VIRTIO_ID_RNG, PCI_CLASS_RNG); + r = virtio_init(kvm, rdev, &rdev->vdev, &rng_dev_virtio_ops, + VIRTIO_PCI, PCI_DEVICE_ID_VIRTIO_RNG, VIRTIO_ID_RNG, PCI_CLASS_RNG); if (r < 0) goto cleanup; - rdev->vtrans.virtio_ops = &rng_dev_virtio_ops; - list_add_tail(&rdev->list, &rdevs); if (compat_id != -1) @@ -195,7 +191,7 @@ int virtio_rng__exit(struct kvm *kvm) list_for_each_entry_safe(rdev, tmp, &rdevs, list) { list_del(&rdev->list); - rdev->vtrans.trans_ops->uninit(kvm, &rdev->vtrans); + rdev->vdev.ops->exit(kvm, &rdev->vdev); free(rdev); } diff --git a/tools/kvm/virtio/trans.c b/tools/kvm/virtio/trans.c deleted file mode 100644 index cd4fc7e80d89..000000000000 --- a/tools/kvm/virtio/trans.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "kvm/virtio-trans.h" - -#include "kvm/virtio-pci.h" -#include "kvm/util.h" - -#include - -int virtio_trans_init(struct virtio_trans *vtrans, enum virtio_trans_type type) -{ - void *trans; - - switch (type) { - case VIRTIO_PCI: - trans = calloc(sizeof(struct virtio_pci), 1); - if (!trans) - return -ENOMEM; - vtrans->virtio = trans; - vtrans->trans_ops = virtio_pci__get_trans_ops(); - default: - return -1; - }; - - return 0; -} \ No newline at end of file -- 2.39.5