139d6af07SAsias He #include <linux/virtio_ring.h> 23fdf659dSSasha Levin #include <linux/types.h> 339d6af07SAsias He #include <sys/uio.h> 402eca50cSAsias He #include <stdlib.h> 52caa836dSIngo Molnar 652f34d2cSAsias He #include "kvm/guest_compat.h" 72caa836dSIngo Molnar #include "kvm/barrier.h" 839d6af07SAsias He #include "kvm/virtio.h" 902eca50cSAsias He #include "kvm/virtio-pci.h" 10755752d6SAsias He #include "kvm/virtio-mmio.h" 1102eca50cSAsias He #include "kvm/util.h" 1202eca50cSAsias He #include "kvm/kvm.h" 1302eca50cSAsias He 1439d6af07SAsias He 15dc7a55d6SSuzuki K. Poulose const char* virtio_trans_name(enum virtio_trans trans) 16dc7a55d6SSuzuki K. Poulose { 17b0d56e3cSJean-Philippe Brucker if (trans == VIRTIO_PCI || trans == VIRTIO_PCI_LEGACY) 18dc7a55d6SSuzuki K. Poulose return "pci"; 195fe5eb04SJean-Philippe Brucker else if (trans == VIRTIO_MMIO || trans == VIRTIO_MMIO_LEGACY) 20dc7a55d6SSuzuki K. Poulose return "mmio"; 21dc7a55d6SSuzuki K. Poulose return "unknown"; 22dc7a55d6SSuzuki K. Poulose } 23dc7a55d6SSuzuki K. Poulose 249b46ebc5SRajnesh Kanwal int virtio_transport_parser(const struct option *opt, const char *arg, int unset) 259b46ebc5SRajnesh Kanwal { 269b46ebc5SRajnesh Kanwal enum virtio_trans *type = opt->value; 279b46ebc5SRajnesh Kanwal struct kvm *kvm; 289b46ebc5SRajnesh Kanwal 299b46ebc5SRajnesh Kanwal if (!strcmp(opt->long_name, "virtio-transport")) { 309b46ebc5SRajnesh Kanwal if (!strcmp(arg, "pci")) { 319b46ebc5SRajnesh Kanwal *type = VIRTIO_PCI; 329b46ebc5SRajnesh Kanwal } else if (!strcmp(arg, "pci-legacy")) { 339b46ebc5SRajnesh Kanwal *type = VIRTIO_PCI_LEGACY; 349b46ebc5SRajnesh Kanwal #if defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_RISCV) 359b46ebc5SRajnesh Kanwal } else if (!strcmp(arg, "mmio")) { 369b46ebc5SRajnesh Kanwal *type = VIRTIO_MMIO; 379b46ebc5SRajnesh Kanwal } else if (!strcmp(arg, "mmio-legacy")) { 389b46ebc5SRajnesh Kanwal *type = VIRTIO_MMIO_LEGACY; 399b46ebc5SRajnesh Kanwal #endif 409b46ebc5SRajnesh Kanwal } else { 419b46ebc5SRajnesh Kanwal pr_err("virtio-transport: unknown type \"%s\"\n", arg); 429b46ebc5SRajnesh Kanwal return -1; 439b46ebc5SRajnesh Kanwal } 449b46ebc5SRajnesh Kanwal } else if (!strcmp(opt->long_name, "virtio-legacy")) { 459b46ebc5SRajnesh Kanwal *type = VIRTIO_PCI_LEGACY; 469b46ebc5SRajnesh Kanwal } else if (!strcmp(opt->long_name, "force-pci")) { 479b46ebc5SRajnesh Kanwal kvm = opt->ptr; 489b46ebc5SRajnesh Kanwal kvm->cfg.virtio_transport = VIRTIO_PCI; 499b46ebc5SRajnesh Kanwal } 509b46ebc5SRajnesh Kanwal 519b46ebc5SRajnesh Kanwal return 0; 529b46ebc5SRajnesh Kanwal } 539b46ebc5SRajnesh Kanwal 543fea89a9SWill Deacon void virt_queue__used_idx_advance(struct virt_queue *queue, u16 jump) 5539d6af07SAsias He { 56b17552eeSAndre Przywara u16 idx = virtio_guest_to_host_u16(queue->endian, 57b17552eeSAndre Przywara queue->vring.used->idx); 58407475bfSPekka Enberg 5994902782SSasha Levin /* 6094902782SSasha Levin * Use wmb to assure that used elem was updated with head and len. 6194902782SSasha Levin * We need a wmb here since we can't advance idx unless we're ready 6294902782SSasha Levin * to pass the used element to the guest. 6394902782SSasha Levin */ 6494902782SSasha Levin wmb(); 653fea89a9SWill Deacon idx += jump; 66b17552eeSAndre Przywara queue->vring.used->idx = virtio_host_to_guest_u16(queue->endian, idx); 673fea89a9SWill Deacon } 683fea89a9SWill Deacon 693fea89a9SWill Deacon struct vring_used_elem * 703fea89a9SWill Deacon virt_queue__set_used_elem_no_update(struct virt_queue *queue, u32 head, 713fea89a9SWill Deacon u32 len, u16 offset) 723fea89a9SWill Deacon { 733fea89a9SWill Deacon struct vring_used_elem *used_elem; 74b17552eeSAndre Przywara u16 idx = virtio_guest_to_host_u16(queue->endian, queue->vring.used->idx); 753fea89a9SWill Deacon 763fea89a9SWill Deacon idx += offset; 773fea89a9SWill Deacon used_elem = &queue->vring.used->ring[idx % queue->vring.num]; 78b17552eeSAndre Przywara used_elem->id = virtio_host_to_guest_u32(queue->endian, head); 79b17552eeSAndre Przywara used_elem->len = virtio_host_to_guest_u32(queue->endian, len); 803fea89a9SWill Deacon 813fea89a9SWill Deacon return used_elem; 823fea89a9SWill Deacon } 833fea89a9SWill Deacon 843fea89a9SWill Deacon struct vring_used_elem *virt_queue__set_used_elem(struct virt_queue *queue, u32 head, u32 len) 853fea89a9SWill Deacon { 863fea89a9SWill Deacon struct vring_used_elem *used_elem; 873fea89a9SWill Deacon 883fea89a9SWill Deacon used_elem = virt_queue__set_used_elem_no_update(queue, head, len, 0); 893fea89a9SWill Deacon virt_queue__used_idx_advance(queue, 1); 9094902782SSasha Levin 9139d6af07SAsias He return used_elem; 9239d6af07SAsias He } 9339d6af07SAsias He 94fb591944SMarc Zyngier static inline bool virt_desc__test_flag(struct virt_queue *vq, 95fb591944SMarc Zyngier struct vring_desc *desc, u16 flag) 96fb591944SMarc Zyngier { 97b17552eeSAndre Przywara return !!(virtio_guest_to_host_u16(vq->endian, desc->flags) & flag); 98fb591944SMarc Zyngier } 99fb591944SMarc Zyngier 100754c8ce3SSasha Levin /* 101754c8ce3SSasha Levin * Each buffer in the virtqueues is actually a chain of descriptors. This 102bbea6c7aSJean-Philippe Brucker * function returns the next descriptor in the chain, or max if we're at the 103bbea6c7aSJean-Philippe Brucker * end. 104754c8ce3SSasha Levin */ 105fb591944SMarc Zyngier static unsigned next_desc(struct virt_queue *vq, struct vring_desc *desc, 106754c8ce3SSasha Levin unsigned int i, unsigned int max) 107754c8ce3SSasha Levin { 108754c8ce3SSasha Levin unsigned int next; 109754c8ce3SSasha Levin 110754c8ce3SSasha Levin /* If this descriptor says it doesn't chain, we're done. */ 111fb591944SMarc Zyngier if (!virt_desc__test_flag(vq, &desc[i], VRING_DESC_F_NEXT)) 112754c8ce3SSasha Levin return max; 113754c8ce3SSasha Levin 114b17552eeSAndre Przywara next = virtio_guest_to_host_u16(vq->endian, desc[i].next); 115754c8ce3SSasha Levin 116bbea6c7aSJean-Philippe Brucker /* Ensure they're not leading us off end of descriptors. */ 117bbea6c7aSJean-Philippe Brucker return min(next, max); 118754c8ce3SSasha Levin } 119754c8ce3SSasha Levin 1202fddfdb5SAsias He u16 virt_queue__get_head_iov(struct virt_queue *vq, struct iovec iov[], u16 *out, u16 *in, u16 head, struct kvm *kvm) 12139d6af07SAsias He { 12239d6af07SAsias He struct vring_desc *desc; 1232fddfdb5SAsias He u16 idx; 124754c8ce3SSasha Levin u16 max; 12539d6af07SAsias He 1262fddfdb5SAsias He idx = head; 12739d6af07SAsias He *out = *in = 0; 128754c8ce3SSasha Levin max = vq->vring.num; 129754c8ce3SSasha Levin desc = vq->vring.desc; 130754c8ce3SSasha Levin 131fb591944SMarc Zyngier if (virt_desc__test_flag(vq, &desc[idx], VRING_DESC_F_INDIRECT)) { 132b17552eeSAndre Przywara max = virtio_guest_to_host_u32(vq->endian, desc[idx].len) / sizeof(struct vring_desc); 133b17552eeSAndre Przywara desc = guest_flat_to_host(kvm, virtio_guest_to_host_u64(vq->endian, desc[idx].addr)); 134754c8ce3SSasha Levin idx = 0; 135754c8ce3SSasha Levin } 13639d6af07SAsias He 13739d6af07SAsias He do { 138754c8ce3SSasha Levin /* Grab the first descriptor, and check it's OK. */ 139b17552eeSAndre Przywara iov[*out + *in].iov_len = virtio_guest_to_host_u32(vq->endian, desc[idx].len); 140fb591944SMarc Zyngier iov[*out + *in].iov_base = guest_flat_to_host(kvm, 141b17552eeSAndre Przywara virtio_guest_to_host_u64(vq->endian, desc[idx].addr)); 142754c8ce3SSasha Levin /* If this is an input descriptor, increment that count. */ 143fb591944SMarc Zyngier if (virt_desc__test_flag(vq, &desc[idx], VRING_DESC_F_WRITE)) 14439d6af07SAsias He (*in)++; 14539d6af07SAsias He else 14639d6af07SAsias He (*out)++; 147fb591944SMarc Zyngier } while ((idx = next_desc(vq, desc, idx, max)) != max); 14839d6af07SAsias He 14939d6af07SAsias He return head; 15039d6af07SAsias He } 1517f5ffaf5SAsias He 1522fddfdb5SAsias He u16 virt_queue__get_iov(struct virt_queue *vq, struct iovec iov[], u16 *out, u16 *in, struct kvm *kvm) 1532fddfdb5SAsias He { 1542fddfdb5SAsias He u16 head; 1552fddfdb5SAsias He 1562fddfdb5SAsias He head = virt_queue__pop(vq); 1572fddfdb5SAsias He 1582fddfdb5SAsias He return virt_queue__get_head_iov(vq, iov, out, in, head, kvm); 1592fddfdb5SAsias He } 1602fddfdb5SAsias He 16108861bcfSAneesh Kumar K.V /* in and out are relative to guest */ 16208861bcfSAneesh Kumar K.V u16 virt_queue__get_inout_iov(struct kvm *kvm, struct virt_queue *queue, 16308861bcfSAneesh Kumar K.V struct iovec in_iov[], struct iovec out_iov[], 16408861bcfSAneesh Kumar K.V u16 *in, u16 *out) 16508861bcfSAneesh Kumar K.V { 16608861bcfSAneesh Kumar K.V struct vring_desc *desc; 1672fddfdb5SAsias He u16 head, idx; 16808861bcfSAneesh Kumar K.V 16908861bcfSAneesh Kumar K.V idx = head = virt_queue__pop(queue); 17008861bcfSAneesh Kumar K.V *out = *in = 0; 17108861bcfSAneesh Kumar K.V do { 172fb591944SMarc Zyngier u64 addr; 17308861bcfSAneesh Kumar K.V desc = virt_queue__get_desc(queue, idx); 174b17552eeSAndre Przywara addr = virtio_guest_to_host_u64(queue->endian, desc->addr); 175fb591944SMarc Zyngier if (virt_desc__test_flag(queue, desc, VRING_DESC_F_WRITE)) { 176fb591944SMarc Zyngier in_iov[*in].iov_base = guest_flat_to_host(kvm, addr); 177b17552eeSAndre Przywara in_iov[*in].iov_len = virtio_guest_to_host_u32(queue->endian, desc->len); 17808861bcfSAneesh Kumar K.V (*in)++; 17908861bcfSAneesh Kumar K.V } else { 180fb591944SMarc Zyngier out_iov[*out].iov_base = guest_flat_to_host(kvm, addr); 181b17552eeSAndre Przywara out_iov[*out].iov_len = virtio_guest_to_host_u32(queue->endian, desc->len); 18208861bcfSAneesh Kumar K.V (*out)++; 18308861bcfSAneesh Kumar K.V } 184fb591944SMarc Zyngier if (virt_desc__test_flag(queue, desc, VRING_DESC_F_NEXT)) 185b17552eeSAndre Przywara idx = virtio_guest_to_host_u16(queue->endian, desc->next); 18608861bcfSAneesh Kumar K.V else 18708861bcfSAneesh Kumar K.V break; 18808861bcfSAneesh Kumar K.V } while (1); 1892fddfdb5SAsias He 19008861bcfSAneesh Kumar K.V return head; 19108861bcfSAneesh Kumar K.V } 19208861bcfSAneesh Kumar K.V 193fd41cde0SJean-Philippe Brucker void virtio_init_device_vq(struct kvm *kvm, struct virtio_device *vdev, 194609ee906SJean-Philippe Brucker struct virt_queue *vq, size_t nr_descs) 195fd41cde0SJean-Philippe Brucker { 196609ee906SJean-Philippe Brucker struct vring_addr *addr = &vq->vring_addr; 197fd41cde0SJean-Philippe Brucker 198fd41cde0SJean-Philippe Brucker vq->endian = vdev->endian; 199717a3ab0STu Dinh Ngoc vq->use_event_idx = (vdev->features & (1UL << VIRTIO_RING_F_EVENT_IDX)); 200fd41cde0SJean-Philippe Brucker vq->enabled = true; 201*46aaf3b8SJean-Philippe Brucker vq->vdev = vdev; 202fd41cde0SJean-Philippe Brucker 203609ee906SJean-Philippe Brucker if (addr->legacy) { 204609ee906SJean-Philippe Brucker unsigned long base = (u64)addr->pfn * addr->pgsize; 205609ee906SJean-Philippe Brucker void *p = guest_flat_to_host(kvm, base); 206609ee906SJean-Philippe Brucker 207609ee906SJean-Philippe Brucker vring_init(&vq->vring, nr_descs, p, addr->align); 208609ee906SJean-Philippe Brucker } else { 209609ee906SJean-Philippe Brucker u64 desc = (u64)addr->desc_hi << 32 | addr->desc_lo; 210609ee906SJean-Philippe Brucker u64 avail = (u64)addr->avail_hi << 32 | addr->avail_lo; 211609ee906SJean-Philippe Brucker u64 used = (u64)addr->used_hi << 32 | addr->used_lo; 212609ee906SJean-Philippe Brucker 213609ee906SJean-Philippe Brucker vq->vring = (struct vring) { 214609ee906SJean-Philippe Brucker .desc = guest_flat_to_host(kvm, desc), 215609ee906SJean-Philippe Brucker .used = guest_flat_to_host(kvm, used), 216609ee906SJean-Philippe Brucker .avail = guest_flat_to_host(kvm, avail), 217609ee906SJean-Philippe Brucker .num = nr_descs, 218609ee906SJean-Philippe Brucker }; 219609ee906SJean-Philippe Brucker } 220fd41cde0SJean-Philippe Brucker } 221fd41cde0SJean-Philippe Brucker 222ad346c2eSJean-Philippe Brucker void virtio_exit_vq(struct kvm *kvm, struct virtio_device *vdev, 223ad346c2eSJean-Philippe Brucker void *dev, int num) 224ad346c2eSJean-Philippe Brucker { 225ad346c2eSJean-Philippe Brucker struct virt_queue *vq = vdev->ops->get_vq(kvm, dev, num); 226ad346c2eSJean-Philippe Brucker 227ad346c2eSJean-Philippe Brucker if (vq->enabled && vdev->ops->exit_vq) 228ad346c2eSJean-Philippe Brucker vdev->ops->exit_vq(kvm, dev, num); 229ad346c2eSJean-Philippe Brucker memset(vq, 0, sizeof(*vq)); 230ad346c2eSJean-Philippe Brucker } 231ad346c2eSJean-Philippe Brucker 2321382aba0SSasha Levin int virtio__get_dev_specific_field(int offset, bool msix, u32 *config_off) 233c3a79fa1SSasha Levin { 234c3a79fa1SSasha Levin if (msix) { 235c3a79fa1SSasha Levin if (offset < 4) 236c3a79fa1SSasha Levin return VIRTIO_PCI_O_MSIX; 237c3a79fa1SSasha Levin else 238c3a79fa1SSasha Levin offset -= 4; 239c3a79fa1SSasha Levin } 240c3a79fa1SSasha Levin 241c3a79fa1SSasha Levin *config_off = offset; 242c3a79fa1SSasha Levin 243c3a79fa1SSasha Levin return VIRTIO_PCI_O_CONFIG; 244c3a79fa1SSasha Levin } 24551b1454fSAsias He 24651b1454fSAsias He bool virtio_queue__should_signal(struct virt_queue *vq) 24751b1454fSAsias He { 24851b1454fSAsias He u16 old_idx, new_idx, event_idx; 24951b1454fSAsias He 250d7d79bd5SAlexandru Elisei /* 251d7d79bd5SAlexandru Elisei * Use mb to assure used idx has been increased before we signal the 252d7d79bd5SAlexandru Elisei * guest, and we don't read a stale value for used_event. Without a mb 253d7d79bd5SAlexandru Elisei * here we might not send a notification that we need to send, or the 254d7d79bd5SAlexandru Elisei * guest may ignore the queue since it won't see an updated idx. 255d7d79bd5SAlexandru Elisei */ 256d7d79bd5SAlexandru Elisei mb(); 257d7d79bd5SAlexandru Elisei 258b7af514cSJean-Philippe Brucker if (!vq->use_event_idx) { 259b7af514cSJean-Philippe Brucker /* 260b7af514cSJean-Philippe Brucker * When VIRTIO_RING_F_EVENT_IDX isn't negotiated, interrupt the 261b7af514cSJean-Philippe Brucker * guest if it didn't explicitly request to be left alone. 262b7af514cSJean-Philippe Brucker */ 263b17552eeSAndre Przywara return !(virtio_guest_to_host_u16(vq->endian, vq->vring.avail->flags) & 264b7af514cSJean-Philippe Brucker VRING_AVAIL_F_NO_INTERRUPT); 265b7af514cSJean-Philippe Brucker } 266b7af514cSJean-Philippe Brucker 26751b1454fSAsias He old_idx = vq->last_used_signalled; 268b17552eeSAndre Przywara new_idx = virtio_guest_to_host_u16(vq->endian, vq->vring.used->idx); 269b17552eeSAndre Przywara event_idx = virtio_guest_to_host_u16(vq->endian, vring_used_event(&vq->vring)); 27051b1454fSAsias He 27151b1454fSAsias He if (vring_need_event(event_idx, new_idx, old_idx)) { 27251b1454fSAsias He vq->last_used_signalled = new_idx; 27351b1454fSAsias He return true; 27451b1454fSAsias He } 27551b1454fSAsias He 27651b1454fSAsias He return false; 27751b1454fSAsias He } 27802eca50cSAsias He 27956a16c90SJean-Philippe Brucker void virtio_set_guest_features(struct kvm *kvm, struct virtio_device *vdev, 2803c8f82b8SJean-Philippe Brucker void *dev, u64 features) 28156a16c90SJean-Philippe Brucker { 28256a16c90SJean-Philippe Brucker /* TODO: fail negotiation if features & ~host_features */ 28356a16c90SJean-Philippe Brucker 2843c8f82b8SJean-Philippe Brucker vdev->features |= features; 28556a16c90SJean-Philippe Brucker } 28656a16c90SJean-Philippe Brucker 28795242e44SJean-Philippe Brucker void virtio_notify_status(struct kvm *kvm, struct virtio_device *vdev, 28895242e44SJean-Philippe Brucker void *dev, u8 status) 28995242e44SJean-Philippe Brucker { 29095242e44SJean-Philippe Brucker u32 ext_status = status; 29195242e44SJean-Philippe Brucker 29295242e44SJean-Philippe Brucker vdev->status &= ~VIRTIO_CONFIG_S_MASK; 29395242e44SJean-Philippe Brucker vdev->status |= status; 29495242e44SJean-Philippe Brucker 29595242e44SJean-Philippe Brucker /* Add a few hints to help devices */ 29695242e44SJean-Philippe Brucker if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && 29795242e44SJean-Philippe Brucker !(vdev->status & VIRTIO__STATUS_START)) { 29895242e44SJean-Philippe Brucker vdev->status |= VIRTIO__STATUS_START; 29995242e44SJean-Philippe Brucker ext_status |= VIRTIO__STATUS_START; 30095242e44SJean-Philippe Brucker 30195242e44SJean-Philippe Brucker } else if (!status && (vdev->status & VIRTIO__STATUS_START)) { 30295242e44SJean-Philippe Brucker vdev->status &= ~VIRTIO__STATUS_START; 30395242e44SJean-Philippe Brucker ext_status |= VIRTIO__STATUS_STOP; 304eb34a8c2SJean-Philippe Brucker 305eb34a8c2SJean-Philippe Brucker /* 306eb34a8c2SJean-Philippe Brucker * Reset virtqueues and stop all traffic now, so that the device 307eb34a8c2SJean-Philippe Brucker * can safely reset the backend in notify_status(). 308eb34a8c2SJean-Philippe Brucker */ 309eb34a8c2SJean-Philippe Brucker vdev->ops->reset(kvm, vdev); 31095242e44SJean-Philippe Brucker } 311867b15ccSJean-Philippe Brucker if (!status) 312867b15ccSJean-Philippe Brucker ext_status |= VIRTIO__STATUS_CONFIG; 31395242e44SJean-Philippe Brucker 31495242e44SJean-Philippe Brucker if (vdev->ops->notify_status) 31595242e44SJean-Philippe Brucker vdev->ops->notify_status(kvm, dev, ext_status); 31695242e44SJean-Philippe Brucker } 31795242e44SJean-Philippe Brucker 31815e6c4e7SJean-Philippe Brucker bool virtio_access_config(struct kvm *kvm, struct virtio_device *vdev, 31915e6c4e7SJean-Philippe Brucker void *dev, unsigned long offset, void *data, 32015e6c4e7SJean-Philippe Brucker size_t size, bool is_write) 32115e6c4e7SJean-Philippe Brucker { 32215e6c4e7SJean-Philippe Brucker void *in, *out, *config; 32315e6c4e7SJean-Philippe Brucker size_t config_size = vdev->ops->get_config_size(kvm, dev); 32415e6c4e7SJean-Philippe Brucker 32515e6c4e7SJean-Philippe Brucker if (WARN_ONCE(offset + size > config_size, 32615e6c4e7SJean-Philippe Brucker "Config access offset (%lu) is beyond config size (%zu)\n", 32715e6c4e7SJean-Philippe Brucker offset, config_size)) 32815e6c4e7SJean-Philippe Brucker return false; 32915e6c4e7SJean-Philippe Brucker 33015e6c4e7SJean-Philippe Brucker config = vdev->ops->get_config(kvm, dev) + offset; 33115e6c4e7SJean-Philippe Brucker 33215e6c4e7SJean-Philippe Brucker in = is_write ? data : config; 33315e6c4e7SJean-Philippe Brucker out = is_write ? config : data; 33415e6c4e7SJean-Philippe Brucker 33515e6c4e7SJean-Philippe Brucker switch (size) { 33615e6c4e7SJean-Philippe Brucker case 1: 33715e6c4e7SJean-Philippe Brucker *(u8 *)out = *(u8 *)in; 33815e6c4e7SJean-Philippe Brucker break; 33915e6c4e7SJean-Philippe Brucker case 2: 34015e6c4e7SJean-Philippe Brucker *(u16 *)out = *(u16 *)in; 34115e6c4e7SJean-Philippe Brucker break; 34215e6c4e7SJean-Philippe Brucker case 4: 34315e6c4e7SJean-Philippe Brucker *(u32 *)out = *(u32 *)in; 34415e6c4e7SJean-Philippe Brucker break; 34515e6c4e7SJean-Philippe Brucker case 8: 34615e6c4e7SJean-Philippe Brucker *(u64 *)out = *(u64 *)in; 34715e6c4e7SJean-Philippe Brucker break; 34815e6c4e7SJean-Philippe Brucker default: 34915e6c4e7SJean-Philippe Brucker WARN_ONCE(1, "%s: invalid access size\n", __func__); 35015e6c4e7SJean-Philippe Brucker return false; 35115e6c4e7SJean-Philippe Brucker } 35215e6c4e7SJean-Philippe Brucker 35315e6c4e7SJean-Philippe Brucker return true; 35415e6c4e7SJean-Philippe Brucker } 35515e6c4e7SJean-Philippe Brucker 35602eca50cSAsias He int virtio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, 35702eca50cSAsias He struct virtio_ops *ops, enum virtio_trans trans, 35802eca50cSAsias He int device_id, int subsys_id, int class) 35902eca50cSAsias He { 36002eca50cSAsias He void *virtio; 361db927775SAlexandru Elisei int r; 36202eca50cSAsias He 36302eca50cSAsias He switch (trans) { 364b0d56e3cSJean-Philippe Brucker case VIRTIO_PCI_LEGACY: 365b231683cSJean-Philippe Brucker vdev->legacy = true; 366b0d56e3cSJean-Philippe Brucker /* fall through */ 367b0d56e3cSJean-Philippe Brucker case VIRTIO_PCI: 36802eca50cSAsias He virtio = calloc(sizeof(struct virtio_pci), 1); 36902eca50cSAsias He if (!virtio) 37002eca50cSAsias He return -ENOMEM; 37102eca50cSAsias He vdev->virtio = virtio; 37202eca50cSAsias He vdev->ops = ops; 37302eca50cSAsias He vdev->ops->signal_vq = virtio_pci__signal_vq; 37402eca50cSAsias He vdev->ops->signal_config = virtio_pci__signal_config; 37502eca50cSAsias He vdev->ops->init = virtio_pci__init; 37602eca50cSAsias He vdev->ops->exit = virtio_pci__exit; 377eb34a8c2SJean-Philippe Brucker vdev->ops->reset = virtio_pci__reset; 378db927775SAlexandru Elisei r = vdev->ops->init(kvm, dev, vdev, device_id, subsys_id, class); 37902eca50cSAsias He break; 3805fe5eb04SJean-Philippe Brucker case VIRTIO_MMIO_LEGACY: 381b231683cSJean-Philippe Brucker vdev->legacy = true; 3825fe5eb04SJean-Philippe Brucker /* fall through */ 3835fe5eb04SJean-Philippe Brucker case VIRTIO_MMIO: 384755752d6SAsias He virtio = calloc(sizeof(struct virtio_mmio), 1); 385755752d6SAsias He if (!virtio) 386755752d6SAsias He return -ENOMEM; 387755752d6SAsias He vdev->virtio = virtio; 388755752d6SAsias He vdev->ops = ops; 389755752d6SAsias He vdev->ops->signal_vq = virtio_mmio_signal_vq; 390755752d6SAsias He vdev->ops->signal_config = virtio_mmio_signal_config; 391755752d6SAsias He vdev->ops->init = virtio_mmio_init; 392755752d6SAsias He vdev->ops->exit = virtio_mmio_exit; 393eb34a8c2SJean-Philippe Brucker vdev->ops->reset = virtio_mmio_reset; 394db927775SAlexandru Elisei r = vdev->ops->init(kvm, dev, vdev, device_id, subsys_id, class); 395755752d6SAsias He break; 39602eca50cSAsias He default: 397db927775SAlexandru Elisei r = -1; 39802eca50cSAsias He }; 39902eca50cSAsias He 400db927775SAlexandru Elisei return r; 40102eca50cSAsias He } 40252f34d2cSAsias He 40352f34d2cSAsias He int virtio_compat_add_message(const char *device, const char *config) 40452f34d2cSAsias He { 40552f34d2cSAsias He int len = 1024; 40652f34d2cSAsias He int compat_id; 40752f34d2cSAsias He char *title; 40852f34d2cSAsias He char *desc; 40952f34d2cSAsias He 41052f34d2cSAsias He title = malloc(len); 41152f34d2cSAsias He if (!title) 41252f34d2cSAsias He return -ENOMEM; 41352f34d2cSAsias He 41452f34d2cSAsias He desc = malloc(len); 41552f34d2cSAsias He if (!desc) { 41652f34d2cSAsias He free(title); 41752f34d2cSAsias He return -ENOMEM; 41852f34d2cSAsias He } 41952f34d2cSAsias He 42027cead0dSAsias He snprintf(title, len, "%s device was not detected.", device); 42152f34d2cSAsias He snprintf(desc, len, "While you have requested a %s device, " 42252f34d2cSAsias He "the guest kernel did not initialize it.\n" 42327cead0dSAsias He "\tPlease make sure that the guest kernel was " 42427cead0dSAsias He "compiled with %s=y enabled in .config.", 42552f34d2cSAsias He device, config); 42652f34d2cSAsias He 42752f34d2cSAsias He compat_id = compat__add_message(title, desc); 42852f34d2cSAsias He 42952f34d2cSAsias He free(desc); 43052f34d2cSAsias He free(title); 43152f34d2cSAsias He 43252f34d2cSAsias He return compat_id; 43352f34d2cSAsias He } 434