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 { 56*b17552eeSAndre Przywara u16 idx = virtio_guest_to_host_u16(queue->endian, 57*b17552eeSAndre 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; 66*b17552eeSAndre 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; 74*b17552eeSAndre 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]; 78*b17552eeSAndre Przywara used_elem->id = virtio_host_to_guest_u32(queue->endian, head); 79*b17552eeSAndre 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 { 97*b17552eeSAndre 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 114*b17552eeSAndre 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)) { 132*b17552eeSAndre Przywara max = virtio_guest_to_host_u32(vq->endian, desc[idx].len) / sizeof(struct vring_desc); 133*b17552eeSAndre 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. */ 139*b17552eeSAndre 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, 141*b17552eeSAndre 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); 174*b17552eeSAndre 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); 177*b17552eeSAndre 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); 181*b17552eeSAndre 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)) 185*b17552eeSAndre 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; 201fd41cde0SJean-Philippe Brucker 202609ee906SJean-Philippe Brucker if (addr->legacy) { 203609ee906SJean-Philippe Brucker unsigned long base = (u64)addr->pfn * addr->pgsize; 204609ee906SJean-Philippe Brucker void *p = guest_flat_to_host(kvm, base); 205609ee906SJean-Philippe Brucker 206609ee906SJean-Philippe Brucker vring_init(&vq->vring, nr_descs, p, addr->align); 207609ee906SJean-Philippe Brucker } else { 208609ee906SJean-Philippe Brucker u64 desc = (u64)addr->desc_hi << 32 | addr->desc_lo; 209609ee906SJean-Philippe Brucker u64 avail = (u64)addr->avail_hi << 32 | addr->avail_lo; 210609ee906SJean-Philippe Brucker u64 used = (u64)addr->used_hi << 32 | addr->used_lo; 211609ee906SJean-Philippe Brucker 212609ee906SJean-Philippe Brucker vq->vring = (struct vring) { 213609ee906SJean-Philippe Brucker .desc = guest_flat_to_host(kvm, desc), 214609ee906SJean-Philippe Brucker .used = guest_flat_to_host(kvm, used), 215609ee906SJean-Philippe Brucker .avail = guest_flat_to_host(kvm, avail), 216609ee906SJean-Philippe Brucker .num = nr_descs, 217609ee906SJean-Philippe Brucker }; 218609ee906SJean-Philippe Brucker } 219fd41cde0SJean-Philippe Brucker } 220fd41cde0SJean-Philippe Brucker 221ad346c2eSJean-Philippe Brucker void virtio_exit_vq(struct kvm *kvm, struct virtio_device *vdev, 222ad346c2eSJean-Philippe Brucker void *dev, int num) 223ad346c2eSJean-Philippe Brucker { 224ad346c2eSJean-Philippe Brucker struct virt_queue *vq = vdev->ops->get_vq(kvm, dev, num); 225ad346c2eSJean-Philippe Brucker 226ad346c2eSJean-Philippe Brucker if (vq->enabled && vdev->ops->exit_vq) 227ad346c2eSJean-Philippe Brucker vdev->ops->exit_vq(kvm, dev, num); 228ad346c2eSJean-Philippe Brucker memset(vq, 0, sizeof(*vq)); 229ad346c2eSJean-Philippe Brucker } 230ad346c2eSJean-Philippe Brucker 2311382aba0SSasha Levin int virtio__get_dev_specific_field(int offset, bool msix, u32 *config_off) 232c3a79fa1SSasha Levin { 233c3a79fa1SSasha Levin if (msix) { 234c3a79fa1SSasha Levin if (offset < 4) 235c3a79fa1SSasha Levin return VIRTIO_PCI_O_MSIX; 236c3a79fa1SSasha Levin else 237c3a79fa1SSasha Levin offset -= 4; 238c3a79fa1SSasha Levin } 239c3a79fa1SSasha Levin 240c3a79fa1SSasha Levin *config_off = offset; 241c3a79fa1SSasha Levin 242c3a79fa1SSasha Levin return VIRTIO_PCI_O_CONFIG; 243c3a79fa1SSasha Levin } 24451b1454fSAsias He 24551b1454fSAsias He bool virtio_queue__should_signal(struct virt_queue *vq) 24651b1454fSAsias He { 24751b1454fSAsias He u16 old_idx, new_idx, event_idx; 24851b1454fSAsias He 249d7d79bd5SAlexandru Elisei /* 250d7d79bd5SAlexandru Elisei * Use mb to assure used idx has been increased before we signal the 251d7d79bd5SAlexandru Elisei * guest, and we don't read a stale value for used_event. Without a mb 252d7d79bd5SAlexandru Elisei * here we might not send a notification that we need to send, or the 253d7d79bd5SAlexandru Elisei * guest may ignore the queue since it won't see an updated idx. 254d7d79bd5SAlexandru Elisei */ 255d7d79bd5SAlexandru Elisei mb(); 256d7d79bd5SAlexandru Elisei 257b7af514cSJean-Philippe Brucker if (!vq->use_event_idx) { 258b7af514cSJean-Philippe Brucker /* 259b7af514cSJean-Philippe Brucker * When VIRTIO_RING_F_EVENT_IDX isn't negotiated, interrupt the 260b7af514cSJean-Philippe Brucker * guest if it didn't explicitly request to be left alone. 261b7af514cSJean-Philippe Brucker */ 262*b17552eeSAndre Przywara return !(virtio_guest_to_host_u16(vq->endian, vq->vring.avail->flags) & 263b7af514cSJean-Philippe Brucker VRING_AVAIL_F_NO_INTERRUPT); 264b7af514cSJean-Philippe Brucker } 265b7af514cSJean-Philippe Brucker 26651b1454fSAsias He old_idx = vq->last_used_signalled; 267*b17552eeSAndre Przywara new_idx = virtio_guest_to_host_u16(vq->endian, vq->vring.used->idx); 268*b17552eeSAndre Przywara event_idx = virtio_guest_to_host_u16(vq->endian, vring_used_event(&vq->vring)); 26951b1454fSAsias He 27051b1454fSAsias He if (vring_need_event(event_idx, new_idx, old_idx)) { 27151b1454fSAsias He vq->last_used_signalled = new_idx; 27251b1454fSAsias He return true; 27351b1454fSAsias He } 27451b1454fSAsias He 27551b1454fSAsias He return false; 27651b1454fSAsias He } 27702eca50cSAsias He 27856a16c90SJean-Philippe Brucker void virtio_set_guest_features(struct kvm *kvm, struct virtio_device *vdev, 2793c8f82b8SJean-Philippe Brucker void *dev, u64 features) 28056a16c90SJean-Philippe Brucker { 28156a16c90SJean-Philippe Brucker /* TODO: fail negotiation if features & ~host_features */ 28256a16c90SJean-Philippe Brucker 2833c8f82b8SJean-Philippe Brucker vdev->features |= features; 28456a16c90SJean-Philippe Brucker } 28556a16c90SJean-Philippe Brucker 28695242e44SJean-Philippe Brucker void virtio_notify_status(struct kvm *kvm, struct virtio_device *vdev, 28795242e44SJean-Philippe Brucker void *dev, u8 status) 28895242e44SJean-Philippe Brucker { 28995242e44SJean-Philippe Brucker u32 ext_status = status; 29095242e44SJean-Philippe Brucker 29195242e44SJean-Philippe Brucker vdev->status &= ~VIRTIO_CONFIG_S_MASK; 29295242e44SJean-Philippe Brucker vdev->status |= status; 29395242e44SJean-Philippe Brucker 29495242e44SJean-Philippe Brucker /* Add a few hints to help devices */ 29595242e44SJean-Philippe Brucker if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && 29695242e44SJean-Philippe Brucker !(vdev->status & VIRTIO__STATUS_START)) { 29795242e44SJean-Philippe Brucker vdev->status |= VIRTIO__STATUS_START; 29895242e44SJean-Philippe Brucker ext_status |= VIRTIO__STATUS_START; 29995242e44SJean-Philippe Brucker 30095242e44SJean-Philippe Brucker } else if (!status && (vdev->status & VIRTIO__STATUS_START)) { 30195242e44SJean-Philippe Brucker vdev->status &= ~VIRTIO__STATUS_START; 30295242e44SJean-Philippe Brucker ext_status |= VIRTIO__STATUS_STOP; 303eb34a8c2SJean-Philippe Brucker 304eb34a8c2SJean-Philippe Brucker /* 305eb34a8c2SJean-Philippe Brucker * Reset virtqueues and stop all traffic now, so that the device 306eb34a8c2SJean-Philippe Brucker * can safely reset the backend in notify_status(). 307eb34a8c2SJean-Philippe Brucker */ 308eb34a8c2SJean-Philippe Brucker vdev->ops->reset(kvm, vdev); 30995242e44SJean-Philippe Brucker } 310867b15ccSJean-Philippe Brucker if (!status) 311867b15ccSJean-Philippe Brucker ext_status |= VIRTIO__STATUS_CONFIG; 31295242e44SJean-Philippe Brucker 31395242e44SJean-Philippe Brucker if (vdev->ops->notify_status) 31495242e44SJean-Philippe Brucker vdev->ops->notify_status(kvm, dev, ext_status); 31595242e44SJean-Philippe Brucker } 31695242e44SJean-Philippe Brucker 31715e6c4e7SJean-Philippe Brucker bool virtio_access_config(struct kvm *kvm, struct virtio_device *vdev, 31815e6c4e7SJean-Philippe Brucker void *dev, unsigned long offset, void *data, 31915e6c4e7SJean-Philippe Brucker size_t size, bool is_write) 32015e6c4e7SJean-Philippe Brucker { 32115e6c4e7SJean-Philippe Brucker void *in, *out, *config; 32215e6c4e7SJean-Philippe Brucker size_t config_size = vdev->ops->get_config_size(kvm, dev); 32315e6c4e7SJean-Philippe Brucker 32415e6c4e7SJean-Philippe Brucker if (WARN_ONCE(offset + size > config_size, 32515e6c4e7SJean-Philippe Brucker "Config access offset (%lu) is beyond config size (%zu)\n", 32615e6c4e7SJean-Philippe Brucker offset, config_size)) 32715e6c4e7SJean-Philippe Brucker return false; 32815e6c4e7SJean-Philippe Brucker 32915e6c4e7SJean-Philippe Brucker config = vdev->ops->get_config(kvm, dev) + offset; 33015e6c4e7SJean-Philippe Brucker 33115e6c4e7SJean-Philippe Brucker in = is_write ? data : config; 33215e6c4e7SJean-Philippe Brucker out = is_write ? config : data; 33315e6c4e7SJean-Philippe Brucker 33415e6c4e7SJean-Philippe Brucker switch (size) { 33515e6c4e7SJean-Philippe Brucker case 1: 33615e6c4e7SJean-Philippe Brucker *(u8 *)out = *(u8 *)in; 33715e6c4e7SJean-Philippe Brucker break; 33815e6c4e7SJean-Philippe Brucker case 2: 33915e6c4e7SJean-Philippe Brucker *(u16 *)out = *(u16 *)in; 34015e6c4e7SJean-Philippe Brucker break; 34115e6c4e7SJean-Philippe Brucker case 4: 34215e6c4e7SJean-Philippe Brucker *(u32 *)out = *(u32 *)in; 34315e6c4e7SJean-Philippe Brucker break; 34415e6c4e7SJean-Philippe Brucker case 8: 34515e6c4e7SJean-Philippe Brucker *(u64 *)out = *(u64 *)in; 34615e6c4e7SJean-Philippe Brucker break; 34715e6c4e7SJean-Philippe Brucker default: 34815e6c4e7SJean-Philippe Brucker WARN_ONCE(1, "%s: invalid access size\n", __func__); 34915e6c4e7SJean-Philippe Brucker return false; 35015e6c4e7SJean-Philippe Brucker } 35115e6c4e7SJean-Philippe Brucker 35215e6c4e7SJean-Philippe Brucker return true; 35315e6c4e7SJean-Philippe Brucker } 35415e6c4e7SJean-Philippe Brucker 35502eca50cSAsias He int virtio_init(struct kvm *kvm, void *dev, struct virtio_device *vdev, 35602eca50cSAsias He struct virtio_ops *ops, enum virtio_trans trans, 35702eca50cSAsias He int device_id, int subsys_id, int class) 35802eca50cSAsias He { 35902eca50cSAsias He void *virtio; 360db927775SAlexandru Elisei int r; 36102eca50cSAsias He 36202eca50cSAsias He switch (trans) { 363b0d56e3cSJean-Philippe Brucker case VIRTIO_PCI_LEGACY: 364b231683cSJean-Philippe Brucker vdev->legacy = true; 365b0d56e3cSJean-Philippe Brucker /* fall through */ 366b0d56e3cSJean-Philippe Brucker case VIRTIO_PCI: 36702eca50cSAsias He virtio = calloc(sizeof(struct virtio_pci), 1); 36802eca50cSAsias He if (!virtio) 36902eca50cSAsias He return -ENOMEM; 37002eca50cSAsias He vdev->virtio = virtio; 37102eca50cSAsias He vdev->ops = ops; 37202eca50cSAsias He vdev->ops->signal_vq = virtio_pci__signal_vq; 37302eca50cSAsias He vdev->ops->signal_config = virtio_pci__signal_config; 37402eca50cSAsias He vdev->ops->init = virtio_pci__init; 37502eca50cSAsias He vdev->ops->exit = virtio_pci__exit; 376eb34a8c2SJean-Philippe Brucker vdev->ops->reset = virtio_pci__reset; 377db927775SAlexandru Elisei r = vdev->ops->init(kvm, dev, vdev, device_id, subsys_id, class); 37802eca50cSAsias He break; 3795fe5eb04SJean-Philippe Brucker case VIRTIO_MMIO_LEGACY: 380b231683cSJean-Philippe Brucker vdev->legacy = true; 3815fe5eb04SJean-Philippe Brucker /* fall through */ 3825fe5eb04SJean-Philippe Brucker case VIRTIO_MMIO: 383755752d6SAsias He virtio = calloc(sizeof(struct virtio_mmio), 1); 384755752d6SAsias He if (!virtio) 385755752d6SAsias He return -ENOMEM; 386755752d6SAsias He vdev->virtio = virtio; 387755752d6SAsias He vdev->ops = ops; 388755752d6SAsias He vdev->ops->signal_vq = virtio_mmio_signal_vq; 389755752d6SAsias He vdev->ops->signal_config = virtio_mmio_signal_config; 390755752d6SAsias He vdev->ops->init = virtio_mmio_init; 391755752d6SAsias He vdev->ops->exit = virtio_mmio_exit; 392eb34a8c2SJean-Philippe Brucker vdev->ops->reset = virtio_mmio_reset; 393db927775SAlexandru Elisei r = vdev->ops->init(kvm, dev, vdev, device_id, subsys_id, class); 394755752d6SAsias He break; 39502eca50cSAsias He default: 396db927775SAlexandru Elisei r = -1; 39702eca50cSAsias He }; 39802eca50cSAsias He 399db927775SAlexandru Elisei return r; 40002eca50cSAsias He } 40152f34d2cSAsias He 40252f34d2cSAsias He int virtio_compat_add_message(const char *device, const char *config) 40352f34d2cSAsias He { 40452f34d2cSAsias He int len = 1024; 40552f34d2cSAsias He int compat_id; 40652f34d2cSAsias He char *title; 40752f34d2cSAsias He char *desc; 40852f34d2cSAsias He 40952f34d2cSAsias He title = malloc(len); 41052f34d2cSAsias He if (!title) 41152f34d2cSAsias He return -ENOMEM; 41252f34d2cSAsias He 41352f34d2cSAsias He desc = malloc(len); 41452f34d2cSAsias He if (!desc) { 41552f34d2cSAsias He free(title); 41652f34d2cSAsias He return -ENOMEM; 41752f34d2cSAsias He } 41852f34d2cSAsias He 41927cead0dSAsias He snprintf(title, len, "%s device was not detected.", device); 42052f34d2cSAsias He snprintf(desc, len, "While you have requested a %s device, " 42152f34d2cSAsias He "the guest kernel did not initialize it.\n" 42227cead0dSAsias He "\tPlease make sure that the guest kernel was " 42327cead0dSAsias He "compiled with %s=y enabled in .config.", 42452f34d2cSAsias He device, config); 42552f34d2cSAsias He 42652f34d2cSAsias He compat_id = compat__add_message(title, desc); 42752f34d2cSAsias He 42852f34d2cSAsias He free(desc); 42952f34d2cSAsias He free(title); 43052f34d2cSAsias He 43152f34d2cSAsias He return compat_id; 43252f34d2cSAsias He } 433