2
0

vhost-user-blk-pci.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include "qemu/osdep.h"
  19. #include "standard-headers/linux/virtio_pci.h"
  20. #include "hw/virtio/virtio.h"
  21. #include "hw/virtio/vhost-user-blk.h"
  22. #include "hw/pci/pci.h"
  23. #include "hw/qdev-properties.h"
  24. #include "qapi/error.h"
  25. #include "qemu/error-report.h"
  26. #include "qemu/module.h"
  27. #include "virtio-pci.h"
  28. typedef struct VHostUserBlkPCI VHostUserBlkPCI;
  29. /*
  30. * vhost-user-blk-pci: This extends VirtioPCIProxy.
  31. */
  32. #define TYPE_VHOST_USER_BLK_PCI "vhost-user-blk-pci-base"
  33. #define VHOST_USER_BLK_PCI(obj) \
  34. OBJECT_CHECK(VHostUserBlkPCI, (obj), TYPE_VHOST_USER_BLK_PCI)
  35. struct VHostUserBlkPCI {
  36. VirtIOPCIProxy parent_obj;
  37. VHostUserBlk vdev;
  38. };
  39. static Property vhost_user_blk_pci_properties[] = {
  40. DEFINE_PROP_UINT32("class", VirtIOPCIProxy, class_code, 0),
  41. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
  42. DEV_NVECTORS_UNSPECIFIED),
  43. DEFINE_PROP_END_OF_LIST(),
  44. };
  45. static void vhost_user_blk_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
  46. {
  47. VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(vpci_dev);
  48. DeviceState *vdev = DEVICE(&dev->vdev);
  49. if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
  50. vpci_dev->nvectors = dev->vdev.num_queues + 1;
  51. }
  52. qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
  53. object_property_set_bool(OBJECT(vdev), true, "realized", errp);
  54. }
  55. static void vhost_user_blk_pci_class_init(ObjectClass *klass, void *data)
  56. {
  57. DeviceClass *dc = DEVICE_CLASS(klass);
  58. VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
  59. PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
  60. set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
  61. dc->props = vhost_user_blk_pci_properties;
  62. k->realize = vhost_user_blk_pci_realize;
  63. pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  64. pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
  65. pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
  66. pcidev_k->class_id = PCI_CLASS_STORAGE_SCSI;
  67. }
  68. static void vhost_user_blk_pci_instance_init(Object *obj)
  69. {
  70. VHostUserBlkPCI *dev = VHOST_USER_BLK_PCI(obj);
  71. virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
  72. TYPE_VHOST_USER_BLK);
  73. object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
  74. "bootindex", &error_abort);
  75. }
  76. static const VirtioPCIDeviceTypeInfo vhost_user_blk_pci_info = {
  77. .base_name = TYPE_VHOST_USER_BLK_PCI,
  78. .generic_name = "vhost-user-blk-pci",
  79. .transitional_name = "vhost-user-blk-pci-transitional",
  80. .non_transitional_name = "vhost-user-blk-pci-non-transitional",
  81. .instance_size = sizeof(VHostUserBlkPCI),
  82. .instance_init = vhost_user_blk_pci_instance_init,
  83. .class_init = vhost_user_blk_pci_class_init,
  84. };
  85. static void vhost_user_blk_pci_register(void)
  86. {
  87. virtio_pci_types_register(&vhost_user_blk_pci_info);
  88. }
  89. type_init(vhost_user_blk_pci_register)