virtio-blk.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * libqos driver framework
  3. *
  4. * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License version 2.1 as published by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  17. */
  18. #include "qemu/osdep.h"
  19. #include "../libqtest.h"
  20. #include "qemu/module.h"
  21. #include "standard-headers/linux/virtio_blk.h"
  22. #include "qgraph.h"
  23. #include "virtio-blk.h"
  24. #define PCI_SLOT 0x04
  25. #define PCI_FN 0x00
  26. /* virtio-blk-device */
  27. static void *qvirtio_blk_get_driver(QVirtioBlk *v_blk,
  28. const char *interface)
  29. {
  30. if (!g_strcmp0(interface, "virtio-blk")) {
  31. return v_blk;
  32. }
  33. if (!g_strcmp0(interface, "virtio")) {
  34. return v_blk->vdev;
  35. }
  36. fprintf(stderr, "%s not present in virtio-blk-device\n", interface);
  37. g_assert_not_reached();
  38. }
  39. static void *qvirtio_blk_device_get_driver(void *object,
  40. const char *interface)
  41. {
  42. QVirtioBlkDevice *v_blk = object;
  43. return qvirtio_blk_get_driver(&v_blk->blk, interface);
  44. }
  45. static void *virtio_blk_device_create(void *virtio_dev,
  46. QGuestAllocator *t_alloc,
  47. void *addr)
  48. {
  49. QVirtioBlkDevice *virtio_blk = g_new0(QVirtioBlkDevice, 1);
  50. QVirtioBlk *interface = &virtio_blk->blk;
  51. interface->vdev = virtio_dev;
  52. virtio_blk->obj.get_driver = qvirtio_blk_device_get_driver;
  53. return &virtio_blk->obj;
  54. }
  55. /* virtio-blk-pci */
  56. static void *qvirtio_blk_pci_get_driver(void *object, const char *interface)
  57. {
  58. QVirtioBlkPCI *v_blk = object;
  59. if (!g_strcmp0(interface, "pci-device")) {
  60. return v_blk->pci_vdev.pdev;
  61. }
  62. return qvirtio_blk_get_driver(&v_blk->blk, interface);
  63. }
  64. static void *virtio_blk_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
  65. void *addr)
  66. {
  67. QVirtioBlkPCI *virtio_blk = g_new0(QVirtioBlkPCI, 1);
  68. QVirtioBlk *interface = &virtio_blk->blk;
  69. QOSGraphObject *obj = &virtio_blk->pci_vdev.obj;
  70. virtio_pci_init(&virtio_blk->pci_vdev, pci_bus, addr);
  71. interface->vdev = &virtio_blk->pci_vdev.vdev;
  72. g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_BLOCK);
  73. obj->get_driver = qvirtio_blk_pci_get_driver;
  74. return obj;
  75. }
  76. static void virtio_blk_register_nodes(void)
  77. {
  78. /* FIXME: every test using these two nodes needs to setup a
  79. * -drive,id=drive0 otherwise QEMU is not going to start.
  80. * Therefore, we do not include "produces" edge for virtio
  81. * and pci-device yet.
  82. */
  83. char *arg = g_strdup_printf("id=drv0,drive=drive0,addr=%x.%x",
  84. PCI_SLOT, PCI_FN);
  85. QPCIAddress addr = {
  86. .devfn = QPCI_DEVFN(PCI_SLOT, PCI_FN),
  87. };
  88. QOSGraphEdgeOptions opts = { };
  89. /* virtio-blk-device */
  90. opts.extra_device_opts = "drive=drive0";
  91. qos_node_create_driver("virtio-blk-device", virtio_blk_device_create);
  92. qos_node_consumes("virtio-blk-device", "virtio-bus", &opts);
  93. qos_node_produces("virtio-blk-device", "virtio-blk");
  94. /* virtio-blk-pci */
  95. opts.extra_device_opts = arg;
  96. add_qpci_address(&opts, &addr);
  97. qos_node_create_driver("virtio-blk-pci", virtio_blk_pci_create);
  98. qos_node_consumes("virtio-blk-pci", "pci-bus", &opts);
  99. qos_node_produces("virtio-blk-pci", "virtio-blk");
  100. g_free(arg);
  101. }
  102. libqos_init(virtio_blk_register_nodes);