virtio-pci.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. * Virtio PCI Bindings
  3. *
  4. * Copyright IBM, Corp. 2007
  5. * Copyright (c) 2009 CodeSourcery
  6. *
  7. * Authors:
  8. * Anthony Liguori <aliguori@us.ibm.com>
  9. * Paul Brook <paul@codesourcery.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2. See
  12. * the COPYING file in the top-level directory.
  13. *
  14. * Contributions after 2012-01-13 are licensed under the terms of the
  15. * GNU GPL, version 2 or (at your option) any later version.
  16. */
  17. #include <inttypes.h>
  18. #include "virtio.h"
  19. #include "virtio-blk.h"
  20. #include "virtio-net.h"
  21. #include "virtio-serial.h"
  22. #include "virtio-scsi.h"
  23. #include "pci/pci.h"
  24. #include "qemu-error.h"
  25. #include "pci/msi.h"
  26. #include "pci/msix.h"
  27. #include "net.h"
  28. #include "loader.h"
  29. #include "kvm.h"
  30. #include "blockdev.h"
  31. #include "virtio-pci.h"
  32. #include "range.h"
  33. /* from Linux's linux/virtio_pci.h */
  34. /* A 32-bit r/o bitmask of the features supported by the host */
  35. #define VIRTIO_PCI_HOST_FEATURES 0
  36. /* A 32-bit r/w bitmask of features activated by the guest */
  37. #define VIRTIO_PCI_GUEST_FEATURES 4
  38. /* A 32-bit r/w PFN for the currently selected queue */
  39. #define VIRTIO_PCI_QUEUE_PFN 8
  40. /* A 16-bit r/o queue size for the currently selected queue */
  41. #define VIRTIO_PCI_QUEUE_NUM 12
  42. /* A 16-bit r/w queue selector */
  43. #define VIRTIO_PCI_QUEUE_SEL 14
  44. /* A 16-bit r/w queue notifier */
  45. #define VIRTIO_PCI_QUEUE_NOTIFY 16
  46. /* An 8-bit device status register. */
  47. #define VIRTIO_PCI_STATUS 18
  48. /* An 8-bit r/o interrupt status register. Reading the value will return the
  49. * current contents of the ISR and will also clear it. This is effectively
  50. * a read-and-acknowledge. */
  51. #define VIRTIO_PCI_ISR 19
  52. /* MSI-X registers: only enabled if MSI-X is enabled. */
  53. /* A 16-bit vector for configuration changes. */
  54. #define VIRTIO_MSI_CONFIG_VECTOR 20
  55. /* A 16-bit vector for selected queue notifications. */
  56. #define VIRTIO_MSI_QUEUE_VECTOR 22
  57. /* Config space size */
  58. #define VIRTIO_PCI_CONFIG_NOMSI 20
  59. #define VIRTIO_PCI_CONFIG_MSI 24
  60. #define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
  61. VIRTIO_PCI_CONFIG_MSI : \
  62. VIRTIO_PCI_CONFIG_NOMSI)
  63. /* The remaining space is defined by each driver as the per-driver
  64. * configuration space */
  65. #define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
  66. VIRTIO_PCI_CONFIG_MSI : \
  67. VIRTIO_PCI_CONFIG_NOMSI)
  68. /* How many bits to shift physical queue address written to QUEUE_PFN.
  69. * 12 is historical, and due to x86 page size. */
  70. #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
  71. /* Flags track per-device state like workarounds for quirks in older guests. */
  72. #define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
  73. /* QEMU doesn't strictly need write barriers since everything runs in
  74. * lock-step. We'll leave the calls to wmb() in though to make it obvious for
  75. * KVM or if kqemu gets SMP support.
  76. */
  77. #define wmb() do { } while (0)
  78. /* HACK for virtio to determine if it's running a big endian guest */
  79. bool virtio_is_big_endian(void);
  80. /* virtio device */
  81. static void virtio_pci_notify(void *opaque, uint16_t vector)
  82. {
  83. VirtIOPCIProxy *proxy = opaque;
  84. if (msix_enabled(&proxy->pci_dev))
  85. msix_notify(&proxy->pci_dev, vector);
  86. else
  87. qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
  88. }
  89. static void virtio_pci_save_config(void * opaque, QEMUFile *f)
  90. {
  91. VirtIOPCIProxy *proxy = opaque;
  92. pci_device_save(&proxy->pci_dev, f);
  93. msix_save(&proxy->pci_dev, f);
  94. if (msix_present(&proxy->pci_dev))
  95. qemu_put_be16(f, proxy->vdev->config_vector);
  96. }
  97. static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
  98. {
  99. VirtIOPCIProxy *proxy = opaque;
  100. if (msix_present(&proxy->pci_dev))
  101. qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
  102. }
  103. static int virtio_pci_load_config(void * opaque, QEMUFile *f)
  104. {
  105. VirtIOPCIProxy *proxy = opaque;
  106. int ret;
  107. ret = pci_device_load(&proxy->pci_dev, f);
  108. if (ret) {
  109. return ret;
  110. }
  111. msix_unuse_all_vectors(&proxy->pci_dev);
  112. msix_load(&proxy->pci_dev, f);
  113. if (msix_present(&proxy->pci_dev)) {
  114. qemu_get_be16s(f, &proxy->vdev->config_vector);
  115. } else {
  116. proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
  117. }
  118. if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
  119. return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
  120. }
  121. return 0;
  122. }
  123. static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
  124. {
  125. VirtIOPCIProxy *proxy = opaque;
  126. uint16_t vector;
  127. if (msix_present(&proxy->pci_dev)) {
  128. qemu_get_be16s(f, &vector);
  129. } else {
  130. vector = VIRTIO_NO_VECTOR;
  131. }
  132. virtio_queue_set_vector(proxy->vdev, n, vector);
  133. if (vector != VIRTIO_NO_VECTOR) {
  134. return msix_vector_use(&proxy->pci_dev, vector);
  135. }
  136. return 0;
  137. }
  138. static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
  139. int n, bool assign, bool set_handler)
  140. {
  141. VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
  142. EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
  143. int r = 0;
  144. if (assign) {
  145. r = event_notifier_init(notifier, 1);
  146. if (r < 0) {
  147. error_report("%s: unable to init event notifier: %d",
  148. __func__, r);
  149. return r;
  150. }
  151. virtio_queue_set_host_notifier_fd_handler(vq, true, set_handler);
  152. memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
  153. true, n, notifier);
  154. } else {
  155. memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
  156. true, n, notifier);
  157. virtio_queue_set_host_notifier_fd_handler(vq, false, false);
  158. event_notifier_cleanup(notifier);
  159. }
  160. return r;
  161. }
  162. static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
  163. {
  164. int n, r;
  165. if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
  166. proxy->ioeventfd_disabled ||
  167. proxy->ioeventfd_started) {
  168. return;
  169. }
  170. for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
  171. if (!virtio_queue_get_num(proxy->vdev, n)) {
  172. continue;
  173. }
  174. r = virtio_pci_set_host_notifier_internal(proxy, n, true, true);
  175. if (r < 0) {
  176. goto assign_error;
  177. }
  178. }
  179. proxy->ioeventfd_started = true;
  180. return;
  181. assign_error:
  182. while (--n >= 0) {
  183. if (!virtio_queue_get_num(proxy->vdev, n)) {
  184. continue;
  185. }
  186. r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
  187. assert(r >= 0);
  188. }
  189. proxy->ioeventfd_started = false;
  190. error_report("%s: failed. Fallback to a userspace (slower).", __func__);
  191. }
  192. static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
  193. {
  194. int r;
  195. int n;
  196. if (!proxy->ioeventfd_started) {
  197. return;
  198. }
  199. for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
  200. if (!virtio_queue_get_num(proxy->vdev, n)) {
  201. continue;
  202. }
  203. r = virtio_pci_set_host_notifier_internal(proxy, n, false, false);
  204. assert(r >= 0);
  205. }
  206. proxy->ioeventfd_started = false;
  207. }
  208. void virtio_pci_reset(DeviceState *d)
  209. {
  210. VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
  211. virtio_pci_stop_ioeventfd(proxy);
  212. virtio_reset(proxy->vdev);
  213. msix_unuse_all_vectors(&proxy->pci_dev);
  214. proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
  215. }
  216. static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  217. {
  218. VirtIOPCIProxy *proxy = opaque;
  219. VirtIODevice *vdev = proxy->vdev;
  220. hwaddr pa;
  221. switch (addr) {
  222. case VIRTIO_PCI_GUEST_FEATURES:
  223. /* Guest does not negotiate properly? We have to assume nothing. */
  224. if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
  225. val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
  226. }
  227. virtio_set_features(vdev, val);
  228. break;
  229. case VIRTIO_PCI_QUEUE_PFN:
  230. pa = (hwaddr)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
  231. if (pa == 0) {
  232. virtio_pci_stop_ioeventfd(proxy);
  233. virtio_reset(proxy->vdev);
  234. msix_unuse_all_vectors(&proxy->pci_dev);
  235. }
  236. else
  237. virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
  238. break;
  239. case VIRTIO_PCI_QUEUE_SEL:
  240. if (val < VIRTIO_PCI_QUEUE_MAX)
  241. vdev->queue_sel = val;
  242. break;
  243. case VIRTIO_PCI_QUEUE_NOTIFY:
  244. if (val < VIRTIO_PCI_QUEUE_MAX) {
  245. virtio_queue_notify(vdev, val);
  246. }
  247. break;
  248. case VIRTIO_PCI_STATUS:
  249. if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
  250. virtio_pci_stop_ioeventfd(proxy);
  251. }
  252. virtio_set_status(vdev, val & 0xFF);
  253. if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
  254. virtio_pci_start_ioeventfd(proxy);
  255. }
  256. if (vdev->status == 0) {
  257. virtio_reset(proxy->vdev);
  258. msix_unuse_all_vectors(&proxy->pci_dev);
  259. }
  260. /* Linux before 2.6.34 sets the device as OK without enabling
  261. the PCI device bus master bit. In this case we need to disable
  262. some safety checks. */
  263. if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
  264. !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
  265. proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
  266. }
  267. break;
  268. case VIRTIO_MSI_CONFIG_VECTOR:
  269. msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
  270. /* Make it possible for guest to discover an error took place. */
  271. if (msix_vector_use(&proxy->pci_dev, val) < 0)
  272. val = VIRTIO_NO_VECTOR;
  273. vdev->config_vector = val;
  274. break;
  275. case VIRTIO_MSI_QUEUE_VECTOR:
  276. msix_vector_unuse(&proxy->pci_dev,
  277. virtio_queue_vector(vdev, vdev->queue_sel));
  278. /* Make it possible for guest to discover an error took place. */
  279. if (msix_vector_use(&proxy->pci_dev, val) < 0)
  280. val = VIRTIO_NO_VECTOR;
  281. virtio_queue_set_vector(vdev, vdev->queue_sel, val);
  282. break;
  283. default:
  284. error_report("%s: unexpected address 0x%x value 0x%x",
  285. __func__, addr, val);
  286. break;
  287. }
  288. }
  289. static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
  290. {
  291. VirtIODevice *vdev = proxy->vdev;
  292. uint32_t ret = 0xFFFFFFFF;
  293. switch (addr) {
  294. case VIRTIO_PCI_HOST_FEATURES:
  295. ret = proxy->host_features;
  296. break;
  297. case VIRTIO_PCI_GUEST_FEATURES:
  298. ret = vdev->guest_features;
  299. break;
  300. case VIRTIO_PCI_QUEUE_PFN:
  301. ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
  302. >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
  303. break;
  304. case VIRTIO_PCI_QUEUE_NUM:
  305. ret = virtio_queue_get_num(vdev, vdev->queue_sel);
  306. break;
  307. case VIRTIO_PCI_QUEUE_SEL:
  308. ret = vdev->queue_sel;
  309. break;
  310. case VIRTIO_PCI_STATUS:
  311. ret = vdev->status;
  312. break;
  313. case VIRTIO_PCI_ISR:
  314. /* reading from the ISR also clears it. */
  315. ret = vdev->isr;
  316. vdev->isr = 0;
  317. qemu_set_irq(proxy->pci_dev.irq[0], 0);
  318. break;
  319. case VIRTIO_MSI_CONFIG_VECTOR:
  320. ret = vdev->config_vector;
  321. break;
  322. case VIRTIO_MSI_QUEUE_VECTOR:
  323. ret = virtio_queue_vector(vdev, vdev->queue_sel);
  324. break;
  325. default:
  326. break;
  327. }
  328. return ret;
  329. }
  330. static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
  331. unsigned size)
  332. {
  333. VirtIOPCIProxy *proxy = opaque;
  334. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  335. uint64_t val = 0;
  336. if (addr < config) {
  337. return virtio_ioport_read(proxy, addr);
  338. }
  339. addr -= config;
  340. switch (size) {
  341. case 1:
  342. val = virtio_config_readb(proxy->vdev, addr);
  343. break;
  344. case 2:
  345. val = virtio_config_readw(proxy->vdev, addr);
  346. if (virtio_is_big_endian()) {
  347. val = bswap16(val);
  348. }
  349. break;
  350. case 4:
  351. val = virtio_config_readl(proxy->vdev, addr);
  352. if (virtio_is_big_endian()) {
  353. val = bswap32(val);
  354. }
  355. break;
  356. }
  357. return val;
  358. }
  359. static void virtio_pci_config_write(void *opaque, hwaddr addr,
  360. uint64_t val, unsigned size)
  361. {
  362. VirtIOPCIProxy *proxy = opaque;
  363. uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
  364. if (addr < config) {
  365. virtio_ioport_write(proxy, addr, val);
  366. return;
  367. }
  368. addr -= config;
  369. /*
  370. * Virtio-PCI is odd. Ioports are LE but config space is target native
  371. * endian.
  372. */
  373. switch (size) {
  374. case 1:
  375. virtio_config_writeb(proxy->vdev, addr, val);
  376. break;
  377. case 2:
  378. if (virtio_is_big_endian()) {
  379. val = bswap16(val);
  380. }
  381. virtio_config_writew(proxy->vdev, addr, val);
  382. break;
  383. case 4:
  384. if (virtio_is_big_endian()) {
  385. val = bswap32(val);
  386. }
  387. virtio_config_writel(proxy->vdev, addr, val);
  388. break;
  389. }
  390. }
  391. static const MemoryRegionOps virtio_pci_config_ops = {
  392. .read = virtio_pci_config_read,
  393. .write = virtio_pci_config_write,
  394. .impl = {
  395. .min_access_size = 1,
  396. .max_access_size = 4,
  397. },
  398. .endianness = DEVICE_LITTLE_ENDIAN,
  399. };
  400. static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
  401. uint32_t val, int len)
  402. {
  403. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  404. pci_default_write_config(pci_dev, address, val, len);
  405. if (range_covers_byte(address, len, PCI_COMMAND) &&
  406. !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
  407. !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
  408. virtio_pci_stop_ioeventfd(proxy);
  409. virtio_set_status(proxy->vdev,
  410. proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
  411. }
  412. }
  413. static unsigned virtio_pci_get_features(void *opaque)
  414. {
  415. VirtIOPCIProxy *proxy = opaque;
  416. return proxy->host_features;
  417. }
  418. static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
  419. unsigned int queue_no,
  420. unsigned int vector,
  421. MSIMessage msg)
  422. {
  423. VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
  424. EventNotifier *n = virtio_queue_get_guest_notifier(vq);
  425. VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
  426. int ret;
  427. if (irqfd->users == 0) {
  428. ret = kvm_irqchip_add_msi_route(kvm_state, msg);
  429. if (ret < 0) {
  430. return ret;
  431. }
  432. irqfd->virq = ret;
  433. }
  434. irqfd->users++;
  435. ret = kvm_irqchip_add_irqfd_notifier(kvm_state, n, irqfd->virq);
  436. if (ret < 0) {
  437. if (--irqfd->users == 0) {
  438. kvm_irqchip_release_virq(kvm_state, irqfd->virq);
  439. }
  440. return ret;
  441. }
  442. virtio_queue_set_guest_notifier_fd_handler(vq, true, true);
  443. return 0;
  444. }
  445. static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
  446. unsigned int queue_no,
  447. unsigned int vector)
  448. {
  449. VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
  450. EventNotifier *n = virtio_queue_get_guest_notifier(vq);
  451. VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
  452. int ret;
  453. ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, n, irqfd->virq);
  454. assert(ret == 0);
  455. if (--irqfd->users == 0) {
  456. kvm_irqchip_release_virq(kvm_state, irqfd->virq);
  457. }
  458. virtio_queue_set_guest_notifier_fd_handler(vq, true, false);
  459. }
  460. static int kvm_virtio_pci_vector_use(PCIDevice *dev, unsigned vector,
  461. MSIMessage msg)
  462. {
  463. VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
  464. VirtIODevice *vdev = proxy->vdev;
  465. int ret, queue_no;
  466. for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
  467. if (!virtio_queue_get_num(vdev, queue_no)) {
  468. break;
  469. }
  470. if (virtio_queue_vector(vdev, queue_no) != vector) {
  471. continue;
  472. }
  473. ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
  474. if (ret < 0) {
  475. goto undo;
  476. }
  477. }
  478. return 0;
  479. undo:
  480. while (--queue_no >= 0) {
  481. if (virtio_queue_vector(vdev, queue_no) != vector) {
  482. continue;
  483. }
  484. kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
  485. }
  486. return ret;
  487. }
  488. static void kvm_virtio_pci_vector_release(PCIDevice *dev, unsigned vector)
  489. {
  490. VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
  491. VirtIODevice *vdev = proxy->vdev;
  492. int queue_no;
  493. for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
  494. if (!virtio_queue_get_num(vdev, queue_no)) {
  495. break;
  496. }
  497. if (virtio_queue_vector(vdev, queue_no) != vector) {
  498. continue;
  499. }
  500. kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
  501. }
  502. }
  503. static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
  504. {
  505. VirtIOPCIProxy *proxy = opaque;
  506. VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
  507. EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
  508. if (assign) {
  509. int r = event_notifier_init(notifier, 0);
  510. if (r < 0) {
  511. return r;
  512. }
  513. virtio_queue_set_guest_notifier_fd_handler(vq, true, false);
  514. } else {
  515. virtio_queue_set_guest_notifier_fd_handler(vq, false, false);
  516. event_notifier_cleanup(notifier);
  517. }
  518. return 0;
  519. }
  520. static bool virtio_pci_query_guest_notifiers(void *opaque)
  521. {
  522. VirtIOPCIProxy *proxy = opaque;
  523. return msix_enabled(&proxy->pci_dev);
  524. }
  525. static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
  526. {
  527. VirtIOPCIProxy *proxy = opaque;
  528. VirtIODevice *vdev = proxy->vdev;
  529. int r, n;
  530. /* Must unset vector notifier while guest notifier is still assigned */
  531. if (kvm_msi_via_irqfd_enabled() && !assign) {
  532. msix_unset_vector_notifiers(&proxy->pci_dev);
  533. g_free(proxy->vector_irqfd);
  534. proxy->vector_irqfd = NULL;
  535. }
  536. for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
  537. if (!virtio_queue_get_num(vdev, n)) {
  538. break;
  539. }
  540. r = virtio_pci_set_guest_notifier(opaque, n, assign);
  541. if (r < 0) {
  542. goto assign_error;
  543. }
  544. }
  545. /* Must set vector notifier after guest notifier has been assigned */
  546. if (kvm_msi_via_irqfd_enabled() && assign) {
  547. proxy->vector_irqfd =
  548. g_malloc0(sizeof(*proxy->vector_irqfd) *
  549. msix_nr_vectors_allocated(&proxy->pci_dev));
  550. r = msix_set_vector_notifiers(&proxy->pci_dev,
  551. kvm_virtio_pci_vector_use,
  552. kvm_virtio_pci_vector_release);
  553. if (r < 0) {
  554. goto assign_error;
  555. }
  556. }
  557. return 0;
  558. assign_error:
  559. /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
  560. assert(assign);
  561. while (--n >= 0) {
  562. virtio_pci_set_guest_notifier(opaque, n, !assign);
  563. }
  564. return r;
  565. }
  566. static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
  567. {
  568. VirtIOPCIProxy *proxy = opaque;
  569. /* Stop using ioeventfd for virtqueue kick if the device starts using host
  570. * notifiers. This makes it easy to avoid stepping on each others' toes.
  571. */
  572. proxy->ioeventfd_disabled = assign;
  573. if (assign) {
  574. virtio_pci_stop_ioeventfd(proxy);
  575. }
  576. /* We don't need to start here: it's not needed because backend
  577. * currently only stops on status change away from ok,
  578. * reset, vmstop and such. If we do add code to start here,
  579. * need to check vmstate, device state etc. */
  580. return virtio_pci_set_host_notifier_internal(proxy, n, assign, false);
  581. }
  582. static void virtio_pci_vmstate_change(void *opaque, bool running)
  583. {
  584. VirtIOPCIProxy *proxy = opaque;
  585. if (running) {
  586. /* Try to find out if the guest has bus master disabled, but is
  587. in ready state. Then we have a buggy guest OS. */
  588. if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
  589. !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
  590. proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
  591. }
  592. virtio_pci_start_ioeventfd(proxy);
  593. } else {
  594. virtio_pci_stop_ioeventfd(proxy);
  595. }
  596. }
  597. static const VirtIOBindings virtio_pci_bindings = {
  598. .notify = virtio_pci_notify,
  599. .save_config = virtio_pci_save_config,
  600. .load_config = virtio_pci_load_config,
  601. .save_queue = virtio_pci_save_queue,
  602. .load_queue = virtio_pci_load_queue,
  603. .get_features = virtio_pci_get_features,
  604. .query_guest_notifiers = virtio_pci_query_guest_notifiers,
  605. .set_host_notifier = virtio_pci_set_host_notifier,
  606. .set_guest_notifiers = virtio_pci_set_guest_notifiers,
  607. .vmstate_change = virtio_pci_vmstate_change,
  608. };
  609. void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
  610. {
  611. uint8_t *config;
  612. uint32_t size;
  613. proxy->vdev = vdev;
  614. config = proxy->pci_dev.config;
  615. if (proxy->class_code) {
  616. pci_config_set_class(config, proxy->class_code);
  617. }
  618. pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
  619. pci_get_word(config + PCI_VENDOR_ID));
  620. pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
  621. config[PCI_INTERRUPT_PIN] = 1;
  622. if (vdev->nvectors &&
  623. msix_init_exclusive_bar(&proxy->pci_dev, vdev->nvectors, 1)) {
  624. vdev->nvectors = 0;
  625. }
  626. proxy->pci_dev.config_write = virtio_write_config;
  627. size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
  628. if (size & (size-1))
  629. size = 1 << qemu_fls(size);
  630. memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
  631. "virtio-pci", size);
  632. pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
  633. &proxy->bar);
  634. if (!kvm_has_many_ioeventfds()) {
  635. proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
  636. }
  637. virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
  638. proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
  639. proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
  640. proxy->host_features = vdev->get_features(vdev, proxy->host_features);
  641. }
  642. static int virtio_blk_init_pci(PCIDevice *pci_dev)
  643. {
  644. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  645. VirtIODevice *vdev;
  646. if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
  647. proxy->class_code != PCI_CLASS_STORAGE_OTHER)
  648. proxy->class_code = PCI_CLASS_STORAGE_SCSI;
  649. vdev = virtio_blk_init(&pci_dev->qdev, &proxy->blk);
  650. if (!vdev) {
  651. return -1;
  652. }
  653. vdev->nvectors = proxy->nvectors;
  654. virtio_init_pci(proxy, vdev);
  655. /* make the actual value visible */
  656. proxy->nvectors = vdev->nvectors;
  657. return 0;
  658. }
  659. static void virtio_exit_pci(PCIDevice *pci_dev)
  660. {
  661. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  662. memory_region_destroy(&proxy->bar);
  663. msix_uninit_exclusive_bar(pci_dev);
  664. }
  665. static void virtio_blk_exit_pci(PCIDevice *pci_dev)
  666. {
  667. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  668. virtio_pci_stop_ioeventfd(proxy);
  669. virtio_blk_exit(proxy->vdev);
  670. virtio_exit_pci(pci_dev);
  671. }
  672. static int virtio_serial_init_pci(PCIDevice *pci_dev)
  673. {
  674. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  675. VirtIODevice *vdev;
  676. if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
  677. proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
  678. proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
  679. proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
  680. vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
  681. if (!vdev) {
  682. return -1;
  683. }
  684. vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
  685. ? proxy->serial.max_virtserial_ports + 1
  686. : proxy->nvectors;
  687. virtio_init_pci(proxy, vdev);
  688. proxy->nvectors = vdev->nvectors;
  689. return 0;
  690. }
  691. static void virtio_serial_exit_pci(PCIDevice *pci_dev)
  692. {
  693. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  694. virtio_pci_stop_ioeventfd(proxy);
  695. virtio_serial_exit(proxy->vdev);
  696. virtio_exit_pci(pci_dev);
  697. }
  698. static int virtio_net_init_pci(PCIDevice *pci_dev)
  699. {
  700. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  701. VirtIODevice *vdev;
  702. vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
  703. vdev->nvectors = proxy->nvectors;
  704. virtio_init_pci(proxy, vdev);
  705. /* make the actual value visible */
  706. proxy->nvectors = vdev->nvectors;
  707. return 0;
  708. }
  709. static void virtio_net_exit_pci(PCIDevice *pci_dev)
  710. {
  711. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  712. virtio_pci_stop_ioeventfd(proxy);
  713. virtio_net_exit(proxy->vdev);
  714. virtio_exit_pci(pci_dev);
  715. }
  716. static int virtio_balloon_init_pci(PCIDevice *pci_dev)
  717. {
  718. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  719. VirtIODevice *vdev;
  720. if (proxy->class_code != PCI_CLASS_OTHERS &&
  721. proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
  722. proxy->class_code = PCI_CLASS_OTHERS;
  723. }
  724. vdev = virtio_balloon_init(&pci_dev->qdev);
  725. if (!vdev) {
  726. return -1;
  727. }
  728. virtio_init_pci(proxy, vdev);
  729. return 0;
  730. }
  731. static void virtio_balloon_exit_pci(PCIDevice *pci_dev)
  732. {
  733. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  734. virtio_pci_stop_ioeventfd(proxy);
  735. virtio_balloon_exit(proxy->vdev);
  736. virtio_exit_pci(pci_dev);
  737. }
  738. static int virtio_rng_init_pci(PCIDevice *pci_dev)
  739. {
  740. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  741. VirtIODevice *vdev;
  742. if (proxy->rng.rng == NULL) {
  743. proxy->rng.default_backend = RNG_RANDOM(object_new(TYPE_RNG_RANDOM));
  744. object_property_add_child(OBJECT(pci_dev),
  745. "default-backend",
  746. OBJECT(proxy->rng.default_backend),
  747. NULL);
  748. object_property_set_link(OBJECT(pci_dev),
  749. OBJECT(proxy->rng.default_backend),
  750. "rng", NULL);
  751. }
  752. vdev = virtio_rng_init(&pci_dev->qdev, &proxy->rng);
  753. if (!vdev) {
  754. return -1;
  755. }
  756. virtio_init_pci(proxy, vdev);
  757. return 0;
  758. }
  759. static void virtio_rng_exit_pci(PCIDevice *pci_dev)
  760. {
  761. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  762. virtio_pci_stop_ioeventfd(proxy);
  763. virtio_rng_exit(proxy->vdev);
  764. virtio_exit_pci(pci_dev);
  765. }
  766. static Property virtio_blk_properties[] = {
  767. DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
  768. DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, blk.conf),
  769. DEFINE_BLOCK_CHS_PROPERTIES(VirtIOPCIProxy, blk.conf),
  770. DEFINE_PROP_STRING("serial", VirtIOPCIProxy, blk.serial),
  771. #ifdef __linux__
  772. DEFINE_PROP_BIT("scsi", VirtIOPCIProxy, blk.scsi, 0, true),
  773. #endif
  774. DEFINE_PROP_BIT("config-wce", VirtIOPCIProxy, blk.config_wce, 0, true),
  775. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
  776. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
  777. DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
  778. DEFINE_PROP_END_OF_LIST(),
  779. };
  780. static void virtio_blk_class_init(ObjectClass *klass, void *data)
  781. {
  782. DeviceClass *dc = DEVICE_CLASS(klass);
  783. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  784. k->init = virtio_blk_init_pci;
  785. k->exit = virtio_blk_exit_pci;
  786. k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  787. k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
  788. k->revision = VIRTIO_PCI_ABI_VERSION;
  789. k->class_id = PCI_CLASS_STORAGE_SCSI;
  790. dc->reset = virtio_pci_reset;
  791. dc->props = virtio_blk_properties;
  792. }
  793. static TypeInfo virtio_blk_info = {
  794. .name = "virtio-blk-pci",
  795. .parent = TYPE_PCI_DEVICE,
  796. .instance_size = sizeof(VirtIOPCIProxy),
  797. .class_init = virtio_blk_class_init,
  798. };
  799. static Property virtio_net_properties[] = {
  800. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
  801. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
  802. DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
  803. DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
  804. DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL),
  805. DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST),
  806. DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
  807. DEFINE_PROP_END_OF_LIST(),
  808. };
  809. static void virtio_net_class_init(ObjectClass *klass, void *data)
  810. {
  811. DeviceClass *dc = DEVICE_CLASS(klass);
  812. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  813. k->init = virtio_net_init_pci;
  814. k->exit = virtio_net_exit_pci;
  815. k->romfile = "pxe-virtio.rom";
  816. k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  817. k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
  818. k->revision = VIRTIO_PCI_ABI_VERSION;
  819. k->class_id = PCI_CLASS_NETWORK_ETHERNET;
  820. dc->reset = virtio_pci_reset;
  821. dc->props = virtio_net_properties;
  822. }
  823. static TypeInfo virtio_net_info = {
  824. .name = "virtio-net-pci",
  825. .parent = TYPE_PCI_DEVICE,
  826. .instance_size = sizeof(VirtIOPCIProxy),
  827. .class_init = virtio_net_class_init,
  828. };
  829. static Property virtio_serial_properties[] = {
  830. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
  831. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
  832. DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
  833. DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
  834. DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31),
  835. DEFINE_PROP_END_OF_LIST(),
  836. };
  837. static void virtio_serial_class_init(ObjectClass *klass, void *data)
  838. {
  839. DeviceClass *dc = DEVICE_CLASS(klass);
  840. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  841. k->init = virtio_serial_init_pci;
  842. k->exit = virtio_serial_exit_pci;
  843. k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  844. k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
  845. k->revision = VIRTIO_PCI_ABI_VERSION;
  846. k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
  847. dc->reset = virtio_pci_reset;
  848. dc->props = virtio_serial_properties;
  849. }
  850. static TypeInfo virtio_serial_info = {
  851. .name = "virtio-serial-pci",
  852. .parent = TYPE_PCI_DEVICE,
  853. .instance_size = sizeof(VirtIOPCIProxy),
  854. .class_init = virtio_serial_class_init,
  855. };
  856. static Property virtio_balloon_properties[] = {
  857. DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
  858. DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
  859. DEFINE_PROP_END_OF_LIST(),
  860. };
  861. static void virtio_balloon_class_init(ObjectClass *klass, void *data)
  862. {
  863. DeviceClass *dc = DEVICE_CLASS(klass);
  864. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  865. k->init = virtio_balloon_init_pci;
  866. k->exit = virtio_balloon_exit_pci;
  867. k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  868. k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
  869. k->revision = VIRTIO_PCI_ABI_VERSION;
  870. k->class_id = PCI_CLASS_OTHERS;
  871. dc->reset = virtio_pci_reset;
  872. dc->props = virtio_balloon_properties;
  873. }
  874. static TypeInfo virtio_balloon_info = {
  875. .name = "virtio-balloon-pci",
  876. .parent = TYPE_PCI_DEVICE,
  877. .instance_size = sizeof(VirtIOPCIProxy),
  878. .class_init = virtio_balloon_class_init,
  879. };
  880. static void virtio_rng_initfn(Object *obj)
  881. {
  882. PCIDevice *pci_dev = PCI_DEVICE(obj);
  883. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  884. object_property_add_link(obj, "rng", TYPE_RNG_BACKEND,
  885. (Object **)&proxy->rng.rng, NULL);
  886. }
  887. static Property virtio_rng_properties[] = {
  888. DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
  889. /* Set a default rate limit of 2^47 bytes per minute or roughly 2TB/s. If
  890. you have an entropy source capable of generating more entropy than this
  891. and you can pass it through via virtio-rng, then hats off to you. Until
  892. then, this is unlimited for all practical purposes.
  893. */
  894. DEFINE_PROP_UINT64("max-bytes", VirtIOPCIProxy, rng.max_bytes, INT64_MAX),
  895. DEFINE_PROP_UINT32("period", VirtIOPCIProxy, rng.period_ms, 1 << 16),
  896. DEFINE_PROP_END_OF_LIST(),
  897. };
  898. static void virtio_rng_class_init(ObjectClass *klass, void *data)
  899. {
  900. DeviceClass *dc = DEVICE_CLASS(klass);
  901. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  902. k->init = virtio_rng_init_pci;
  903. k->exit = virtio_rng_exit_pci;
  904. k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  905. k->device_id = PCI_DEVICE_ID_VIRTIO_RNG;
  906. k->revision = VIRTIO_PCI_ABI_VERSION;
  907. k->class_id = PCI_CLASS_OTHERS;
  908. dc->reset = virtio_pci_reset;
  909. dc->props = virtio_rng_properties;
  910. }
  911. static TypeInfo virtio_rng_info = {
  912. .name = "virtio-rng-pci",
  913. .parent = TYPE_PCI_DEVICE,
  914. .instance_size = sizeof(VirtIOPCIProxy),
  915. .instance_init = virtio_rng_initfn,
  916. .class_init = virtio_rng_class_init,
  917. };
  918. static int virtio_scsi_init_pci(PCIDevice *pci_dev)
  919. {
  920. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  921. VirtIODevice *vdev;
  922. vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
  923. if (!vdev) {
  924. return -EINVAL;
  925. }
  926. vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
  927. ? proxy->scsi.num_queues + 3
  928. : proxy->nvectors;
  929. virtio_init_pci(proxy, vdev);
  930. /* make the actual value visible */
  931. proxy->nvectors = vdev->nvectors;
  932. return 0;
  933. }
  934. static void virtio_scsi_exit_pci(PCIDevice *pci_dev)
  935. {
  936. VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
  937. virtio_scsi_exit(proxy->vdev);
  938. virtio_exit_pci(pci_dev);
  939. }
  940. static Property virtio_scsi_properties[] = {
  941. DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
  942. DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
  943. DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
  944. DEFINE_PROP_END_OF_LIST(),
  945. };
  946. static void virtio_scsi_class_init(ObjectClass *klass, void *data)
  947. {
  948. DeviceClass *dc = DEVICE_CLASS(klass);
  949. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  950. k->init = virtio_scsi_init_pci;
  951. k->exit = virtio_scsi_exit_pci;
  952. k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
  953. k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
  954. k->revision = 0x00;
  955. k->class_id = PCI_CLASS_STORAGE_SCSI;
  956. dc->reset = virtio_pci_reset;
  957. dc->props = virtio_scsi_properties;
  958. }
  959. static TypeInfo virtio_scsi_info = {
  960. .name = "virtio-scsi-pci",
  961. .parent = TYPE_PCI_DEVICE,
  962. .instance_size = sizeof(VirtIOPCIProxy),
  963. .class_init = virtio_scsi_class_init,
  964. };
  965. static void virtio_pci_register_types(void)
  966. {
  967. type_register_static(&virtio_blk_info);
  968. type_register_static(&virtio_net_info);
  969. type_register_static(&virtio_serial_info);
  970. type_register_static(&virtio_balloon_info);
  971. type_register_static(&virtio_scsi_info);
  972. type_register_static(&virtio_rng_info);
  973. }
  974. type_init(virtio_pci_register_types)