Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Vhost user blk PCI Bindings |
| 3 | * |
| 4 | * Copyright(C) 2017 Intel Corporation. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Changpeng Liu <changpeng.liu@intel.com> |
| 8 | * |
| 9 | * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by: |
| 10 | * Felipe Franciosi <felipe@nutanix.com> |
| 11 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> |
| 12 | * Nicholas Bellinger <nab@risingtidesystems.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 15 | * See the COPYING.LIB file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "qemu/osdep.h" |
| 20 | |
| 21 | #include "standard-headers/linux/virtio_pci.h" |
| 22 | #include "hw/virtio/virtio.h" |
| 23 | #include "hw/virtio/vhost-user-blk.h" |
| 24 | #include "hw/pci/pci.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 25 | #include "hw/qdev-properties.h" |
Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 26 | #include "qapi/error.h" |
| 27 | #include "qemu/error-report.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 28 | #include "qemu/module.h" |
Alex Bennée | e1b1f53 | 2022-03-21 15:30:25 +0000 | [diff] [blame] | 29 | #include "hw/virtio/virtio-pci.h" |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 30 | #include "qom/object.h" |
Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 31 | |
| 32 | typedef struct VHostUserBlkPCI VHostUserBlkPCI; |
| 33 | |
| 34 | /* |
| 35 | * vhost-user-blk-pci: This extends VirtioPCIProxy. |
| 36 | */ |
| 37 | #define TYPE_VHOST_USER_BLK_PCI "vhost-user-blk-pci-base" |
Eduardo Habkost | 8110fa1 | 2020-08-31 17:07:33 -0400 | [diff] [blame] | 38 | DECLARE_INSTANCE_CHECKER(VHostUserBlkPCI, VHOST_USER_BLK_PCI, |
| 39 | TYPE_VHOST_USER_BLK_PCI) |
Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 40 | |
| 41 | struct VHostUserBlkPCI { |
| 42 | VirtIOPCIProxy parent_obj; |
| 43 | VHostUserBlk vdev; |
| 44 | }; |
| 45 | |
| 46 | static Property vhost_user_blk_pci_properties[] = { |
| 47 | DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0), |
| 48 | DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, |
| 49 | DEV_NVECTORS_UNSPECIFIED), |
| 50 | DEFINE_PROP_END_OF_LIST(), |
| 51 | }; |
| 52 | |
| 53 | static void vhost_user_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) |
| 54 | { |
| 55 | VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(vpci_dev); |
| 56 | DeviceState *vdev = DEVICE(&dev->vdev); |
| 57 | |
Stefan Hajnoczi | a4eef07 | 2020-08-18 15:33:48 +0100 | [diff] [blame] | 58 | if (dev->vdev.num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) { |
| 59 | dev->vdev.num_queues = virtio_pci_optimal_num_queues(0); |
| 60 | } |
| 61 | |
Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 62 | if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) { |
| 63 | vpci_dev->nvectors = dev->vdev.num_queues + 1; |
| 64 | } |
| 65 | |
Markus Armbruster | 99ba777 | 2020-06-10 07:32:00 +0200 | [diff] [blame] | 66 | qdev_realize(vdev, BUS(&vpci_dev->bus), errp); |
Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void vhost_user_blk_pci_class_init(ObjectClass *klass, void *data) |
| 70 | { |
| 71 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 72 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); |
| 73 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); |
| 74 | |
| 75 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 76 | device_class_set_props(dc, vhost_user_blk_pci_properties); |
Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 77 | k->realize = vhost_user_blk_pci_realize; |
| 78 | pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET; |
| 79 | pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK; |
| 80 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; |
| 81 | pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI; |
| 82 | } |
| 83 | |
| 84 | static void vhost_user_blk_pci_instance_init(Object *obj) |
| 85 | { |
| 86 | VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(obj); |
| 87 | |
| 88 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), |
| 89 | TYPE_VHOST_USER_BLK); |
| 90 | object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), |
Markus Armbruster | d262312 | 2020-05-05 17:29:22 +0200 | [diff] [blame] | 91 | "bootindex"); |
Juan Quintela | f170c5e | 2019-01-03 15:10:04 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static const VirtioPCIDeviceTypeInfo vhost_user_blk_pci_info = { |
| 95 | .base_name = TYPE_VHOST_USER_BLK_PCI, |
| 96 | .generic_name = "vhost-user-blk-pci", |
| 97 | .transitional_name = "vhost-user-blk-pci-transitional", |
| 98 | .non_transitional_name = "vhost-user-blk-pci-non-transitional", |
| 99 | .instance_size = sizeof(VHostUserBlkPCI), |
| 100 | .instance_init = vhost_user_blk_pci_instance_init, |
| 101 | .class_init = vhost_user_blk_pci_class_init, |
| 102 | }; |
| 103 | |
| 104 | static void vhost_user_blk_pci_register(void) |
| 105 | { |
| 106 | virtio_pci_types_register(&vhost_user_blk_pci_info); |
| 107 | } |
| 108 | |
| 109 | type_init(vhost_user_blk_pci_register) |