This patch cleans up some more style problems in virtio code. Cc: Asias He <asias.hejun@xxxxxxxxx> Cc: Cyrill Gorcunov <gorcunov@xxxxxxxxx> Cc: Ingo Molnar <mingo@xxxxxxx> Cc: Prasad Joshi <prasadjoshi124@xxxxxxxxx> Cc: Sasha Levin <levinsasha928@xxxxxxxxx> Signed-off-by: Pekka Enberg <penberg@xxxxxxxxxx> --- tools/kvm/virtio/blk.c | 86 +++++++++++++++++++------------------------ tools/kvm/virtio/console.c | 32 ++++++++-------- tools/kvm/virtio/core.c | 1 + tools/kvm/virtio/net.c | 36 +++++++++++-------- tools/kvm/virtio/rng.c | 18 +++++---- 5 files changed, 86 insertions(+), 87 deletions(-) diff --git a/tools/kvm/virtio/blk.c b/tools/kvm/virtio/blk.c index 92a8e9e..52bccaf 100644 --- a/tools/kvm/virtio/blk.c +++ b/tools/kvm/virtio/blk.c @@ -51,11 +51,7 @@ struct blk_dev { static struct blk_dev *bdevs[VIRTIO_BLK_MAX_DEV]; -static bool virtio_blk_pci_io_device_specific_in(struct blk_dev *bdev, - void *data, - unsigned long offset, - int size, - u32 count) +static bool virtio_blk_dev_in(struct blk_dev *bdev, void *data, unsigned long offset, int size, u32 count) { u8 *config_space = (u8 *) &bdev->blk_config; @@ -68,23 +64,19 @@ static bool virtio_blk_pci_io_device_specific_in(struct blk_dev *bdev, } /* Translate port into device id + offset in that device addr space */ -static void virtio_blk_port2dev(u16 port, - u16 base, - u16 size, - u16 *dev_idx, - u16 *offset) +static void virtio_blk_port2dev(u16 port, u16 base, u16 size, u16 *dev_idx, u16 *offset) { *dev_idx = (port - base) / size; *offset = port - (base + *dev_idx * size); } + static bool virtio_blk_pci_io_in(struct kvm *self, u16 port, void *data, int size, u32 count) { - u16 offset, dev_idx; - bool ret = true; - struct blk_dev *bdev; + struct blk_dev *bdev; + u16 offset, dev_idx; + bool ret = true; - virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE, - &dev_idx, &offset); + virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE, &dev_idx, &offset); bdev = bdevs[dev_idx]; @@ -118,7 +110,8 @@ static bool virtio_blk_pci_io_in(struct kvm *self, u16 port, void *data, int siz ioport__write16(data, bdev->config_vector); break; default: - ret = virtio_blk_pci_io_device_specific_in(bdev, data, offset, size, count); + ret = virtio_blk_dev_in(bdev, data, offset, size, count); + break; }; mutex_unlock(&bdev->mutex); @@ -143,23 +136,15 @@ static bool virtio_blk_do_io_request(struct kvm *self, switch (req->type) { case VIRTIO_BLK_T_IN: - block_cnt = disk_image__read_sector_iov(bdev->disk, - req->sector, - iov + 1, - in + out - 2); - + block_cnt = disk_image__read_sector_iov(bdev->disk, req->sector, iov + 1, in + out - 2); break; case VIRTIO_BLK_T_OUT: - block_cnt = disk_image__write_sector_iov(bdev->disk, - req->sector, - iov + 1, - in + out - 2); - + block_cnt = disk_image__write_sector_iov(bdev->disk, req->sector, iov + 1, in + out - 2); break; - default: warning("request type %d", req->type); block_cnt = -1; + break; } /* status */ @@ -173,9 +158,12 @@ static bool virtio_blk_do_io_request(struct kvm *self, static void virtio_blk_do_io(struct kvm *kvm, void *param) { - struct blk_dev_job *job = param; - struct virt_queue *vq = job->vq; - struct blk_dev *bdev = job->bdev; + struct blk_dev_job *job = param; + struct virt_queue *vq; + struct blk_dev *bdev; + + vq = job->vq; + bdev = job->bdev; while (virt_queue__available(vq)) virtio_blk_do_io_request(kvm, bdev, vq); @@ -185,12 +173,11 @@ static void virtio_blk_do_io(struct kvm *kvm, void *param) static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int size, u32 count) { + struct blk_dev *bdev; u16 offset, dev_idx; bool ret = true; - struct blk_dev *bdev; - virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE, - &dev_idx, &offset); + virtio_blk_port2dev(port, IOPORT_VIRTIO_BLK, IOPORT_VIRTIO_BLK_SIZE, &dev_idx, &offset); bdev = bdevs[dev_idx]; @@ -207,13 +194,13 @@ static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int si job = &bdev->jobs[bdev->queue_selector]; - queue = &bdev->vqs[bdev->queue_selector]; - queue->pfn = ioport__read32(data); - p = guest_flat_to_host(self, queue->pfn << 12); + queue = &bdev->vqs[bdev->queue_selector]; + queue->pfn = ioport__read32(data); + p = guest_flat_to_host(self, queue->pfn << 12); vring_init(&queue->vring, VIRTIO_BLK_QUEUE_SIZE, p, 4096); - *job = (struct blk_dev_job) { + *job = (struct blk_dev_job) { .vq = queue, .bdev = bdev, }; @@ -223,24 +210,27 @@ static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int si break; } case VIRTIO_PCI_QUEUE_SEL: - bdev->queue_selector = ioport__read16(data); + bdev->queue_selector = ioport__read16(data); break; case VIRTIO_PCI_QUEUE_NOTIFY: { u16 queue_index; - queue_index = ioport__read16(data); + + queue_index = ioport__read16(data); thread_pool__do_job(bdev->jobs[queue_index].job_id); + break; } case VIRTIO_PCI_STATUS: - bdev->status = ioport__read8(data); + bdev->status = ioport__read8(data); break; case VIRTIO_MSI_CONFIG_VECTOR: - bdev->config_vector = VIRTIO_MSI_NO_VECTOR; + bdev->config_vector = VIRTIO_MSI_NO_VECTOR; break; case VIRTIO_MSI_QUEUE_VECTOR: break; default: - ret = false; + ret = false; + break; }; mutex_unlock(&bdev->mutex); @@ -273,26 +263,26 @@ static int virtio_blk_find_empty_dev(void) void virtio_blk__init(struct kvm *self, struct disk_image *disk) { - int new_dev_idx; u16 blk_dev_base_addr; struct blk_dev *bdev; + int new_dev_idx; if (!disk) return; - new_dev_idx = virtio_blk_find_empty_dev(); + new_dev_idx = virtio_blk_find_empty_dev(); if (new_dev_idx < 0) die("Could not find an empty block device slot"); - bdevs[new_dev_idx] = calloc(1, sizeof(struct blk_dev)); + bdevs[new_dev_idx] = calloc(1, sizeof(struct blk_dev)); if (bdevs[new_dev_idx] == NULL) die("Failed allocating bdev"); - bdev = bdevs[new_dev_idx]; + bdev = bdevs[new_dev_idx]; - blk_dev_base_addr = IOPORT_VIRTIO_BLK + new_dev_idx * IOPORT_VIRTIO_BLK_SIZE; + blk_dev_base_addr = IOPORT_VIRTIO_BLK + new_dev_idx * IOPORT_VIRTIO_BLK_SIZE; - *bdev = (struct blk_dev) { + *bdev = (struct blk_dev) { .mutex = PTHREAD_MUTEX_INITIALIZER, .disk = disk, .idx = new_dev_idx, diff --git a/tools/kvm/virtio/console.c b/tools/kvm/virtio/console.c index 6ccb1ef..130bd38 100644 --- a/tools/kvm/virtio/console.c +++ b/tools/kvm/virtio/console.c @@ -147,11 +147,11 @@ static bool virtio_console_pci_io_in(struct kvm *self, u16 port, void *data, int static void virtio_console_handle_callback(struct kvm *self, void *param) { - struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE]; - struct virt_queue *vq; - u16 out, in; - u16 head; - u32 len; + struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE]; + struct virt_queue *vq; + u16 out, in; + u16 head; + u32 len; vq = param; @@ -166,8 +166,8 @@ static void virtio_console_handle_callback(struct kvm *self, void *param) static bool virtio_console_pci_io_out(struct kvm *self, u16 port, void *data, int size, u32 count) { - unsigned long offset = port - IOPORT_VIRTIO_CONSOLE; - bool ret = true; + unsigned long offset = port - IOPORT_VIRTIO_CONSOLE; + bool ret = true; mutex_lock(&cdev.mutex); @@ -181,18 +181,16 @@ static bool virtio_console_pci_io_out(struct kvm *self, u16 port, void *data, in assert(cdev.queue_selector < VIRTIO_CONSOLE_NUM_QUEUES); - queue = &cdev.vqs[cdev.queue_selector]; - queue->pfn = ioport__read32(data); - p = guest_flat_to_host(self, queue->pfn << 12); + queue = &cdev.vqs[cdev.queue_selector]; + queue->pfn = ioport__read32(data); + p = guest_flat_to_host(self, queue->pfn << 12); vring_init(&queue->vring, VIRTIO_CONSOLE_QUEUE_SIZE, p, 4096); if (cdev.queue_selector == VIRTIO_CONSOLE_TX_QUEUE) - cdev.jobs[cdev.queue_selector] = - thread_pool__add_job(self, virtio_console_handle_callback, queue); + cdev.jobs[cdev.queue_selector] = thread_pool__add_job(self, virtio_console_handle_callback, queue); else if (cdev.queue_selector == VIRTIO_CONSOLE_RX_QUEUE) - cdev.jobs[cdev.queue_selector] = - thread_pool__add_job(self, virtio_console__inject_interrupt_callback, queue); + cdev.jobs[cdev.queue_selector] = thread_pool__add_job(self, virtio_console__inject_interrupt_callback, queue); break; } @@ -214,15 +212,17 @@ static bool virtio_console_pci_io_out(struct kvm *self, u16 port, void *data, in break; default: ret = false; + break; }; mutex_unlock(&cdev.mutex); + return ret; } static struct ioport_operations virtio_console_io_ops = { - .io_in = virtio_console_pci_io_in, - .io_out = virtio_console_pci_io_out, + .io_in = virtio_console_pci_io_in, + .io_out = virtio_console_pci_io_out, }; #define PCI_VENDOR_ID_REDHAT_QUMRANET 0x1af4 diff --git a/tools/kvm/virtio/core.c b/tools/kvm/virtio/core.c index dd7a3cf..18d2c41 100644 --- a/tools/kvm/virtio/core.c +++ b/tools/kvm/virtio/core.c @@ -10,6 +10,7 @@ struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, u32 head, u32 len) { struct vring_used_elem *used_elem; + used_elem = &queue->vring.used->ring[queue->vring.used->idx % queue->vring.num]; used_elem->id = head; used_elem->len = len; diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c index 4567118..e404c1a 100644 --- a/tools/kvm/virtio/net.c +++ b/tools/kvm/virtio/net.c @@ -56,17 +56,17 @@ static struct net_device net_device = { .mutex = PTHREAD_MUTEX_INITIALIZER, .net_config = { - .mac = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, + .mac = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }, .status = VIRTIO_NET_S_LINK_UP, }, - .host_features = 1UL << VIRTIO_NET_F_MAC | - 1UL << VIRTIO_NET_F_CSUM | - 1UL << VIRTIO_NET_F_HOST_UFO | - 1UL << VIRTIO_NET_F_HOST_TSO4 | - 1UL << VIRTIO_NET_F_HOST_TSO6 | - 1UL << VIRTIO_NET_F_GUEST_UFO | - 1UL << VIRTIO_NET_F_GUEST_TSO4 | - 1UL << VIRTIO_NET_F_GUEST_TSO6, + .host_features = 1UL << VIRTIO_NET_F_MAC + | 1UL << VIRTIO_NET_F_CSUM + | 1UL << VIRTIO_NET_F_HOST_UFO + | 1UL << VIRTIO_NET_F_HOST_TSO4 + | 1UL << VIRTIO_NET_F_HOST_TSO6 + | 1UL << VIRTIO_NET_F_GUEST_UFO + | 1UL << VIRTIO_NET_F_GUEST_TSO4 + | 1UL << VIRTIO_NET_F_GUEST_TSO6, }; static void *virtio_net_rx_thread(void *p) @@ -130,9 +130,11 @@ static void *virtio_net_tx_thread(void *p) } pthread_exit(NULL); + return NULL; } + static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offset, int size, u32 count) { u8 *config_space = (u8 *) &net_device.net_config; @@ -193,18 +195,21 @@ static bool virtio_net_pci_io_in(struct kvm *self, u16 port, void *data, int siz static void virtio_net_handle_callback(struct kvm *self, u16 queue_index) { - if (queue_index == VIRTIO_NET_TX_QUEUE) { - + switch (queue_index) { + case VIRTIO_NET_TX_QUEUE: { mutex_lock(&net_device.io_tx_mutex); pthread_cond_signal(&net_device.io_tx_cond); mutex_unlock(&net_device.io_tx_mutex); - - } else if (queue_index == VIRTIO_NET_RX_QUEUE) { - + break; + } + case VIRTIO_NET_RX_QUEUE: { mutex_lock(&net_device.io_rx_mutex); pthread_cond_signal(&net_device.io_rx_cond); mutex_unlock(&net_device.io_rx_mutex); - + break; + } + default: + warning("Unknown queue index %u", queue_index); } } @@ -255,6 +260,7 @@ static bool virtio_net_pci_io_out(struct kvm *self, u16 port, void *data, int si }; mutex_unlock(&net_device.mutex); + return ret; } diff --git a/tools/kvm/virtio/rng.c b/tools/kvm/virtio/rng.c index 48c3fe4..884dbb3 100644 --- a/tools/kvm/virtio/rng.c +++ b/tools/kvm/virtio/rng.c @@ -46,8 +46,8 @@ static struct rng_dev rdev; static bool virtio_rng_pci_io_in(struct kvm *kvm, u16 port, void *data, int size, u32 count) { - unsigned long offset; - bool ret = true; + unsigned long offset; + bool ret = true; offset = port - IOPORT_VIRTIO_RNG; @@ -76,6 +76,7 @@ static bool virtio_rng_pci_io_in(struct kvm *kvm, u16 port, void *data, int size break; default: ret = false; + break; }; return ret; @@ -84,11 +85,12 @@ static bool virtio_rng_pci_io_in(struct kvm *kvm, u16 port, void *data, int size static bool virtio_rng_do_io_request(struct kvm *kvm, struct virt_queue *queue) { struct iovec iov[VIRTIO_RNG_QUEUE_SIZE]; - u16 out, in, head; unsigned int len = 0; + u16 out, in, head; + + head = virt_queue__get_iov(queue, iov, &out, &in, kvm); + len = readv(rdev.fd, iov, in); - head = virt_queue__get_iov(queue, iov, &out, &in, kvm); - len = readv(rdev.fd, iov, in); virt_queue__set_used_elem(queue, head, len); return true; @@ -109,7 +111,7 @@ static bool virtio_rng_pci_io_out(struct kvm *kvm, u16 port, void *data, int siz unsigned long offset; bool ret = true; - offset = port - IOPORT_VIRTIO_RNG; + offset = port - IOPORT_VIRTIO_RNG; switch (offset) { case VIRTIO_MSI_QUEUE_VECTOR: @@ -125,8 +127,7 @@ static bool virtio_rng_pci_io_out(struct kvm *kvm, u16 port, void *data, int siz vring_init(&queue->vring, VIRTIO_RNG_QUEUE_SIZE, p, 4096); - rdev.jobs[rdev.queue_selector] = - thread_pool__add_job(kvm, virtio_rng_do_io, queue); + rdev.jobs[rdev.queue_selector] = thread_pool__add_job(kvm, virtio_rng_do_io, queue); break; } @@ -147,6 +148,7 @@ static bool virtio_rng_pci_io_out(struct kvm *kvm, u16 port, void *data, int siz break; default: ret = false; + break; }; return ret; -- 1.7.0.4 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html