]> git.proxmox.com Git - mirror_qemu.git/blame - hw/virtio-pci.c
msi: Invoke msi/msix_reset from PCI core
[mirror_qemu.git] / hw / virtio-pci.c
CommitLineData
53c25cea
PB
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 *
6b620ca3
PB
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.
53c25cea
PB
16 */
17
18#include <inttypes.h>
19
20#include "virtio.h"
8172539d
MT
21#include "virtio-blk.h"
22#include "virtio-net.h"
6b331efb 23#include "virtio-serial.h"
973abc7f 24#include "virtio-scsi.h"
53c25cea 25#include "pci.h"
2f792016 26#include "qemu-error.h"
7d37d351 27#include "msi.h"
aba800a3 28#include "msix.h"
a1e0fea5 29#include "net.h"
97b15621 30#include "loader.h"
ade80dc8 31#include "kvm.h"
2446333c 32#include "blockdev.h"
9fe1ebeb 33#include "virtio-pci.h"
1129714f 34#include "range.h"
53c25cea
PB
35
36/* from Linux's linux/virtio_pci.h */
37
38/* A 32-bit r/o bitmask of the features supported by the host */
39#define VIRTIO_PCI_HOST_FEATURES 0
40
41/* A 32-bit r/w bitmask of features activated by the guest */
42#define VIRTIO_PCI_GUEST_FEATURES 4
43
44/* A 32-bit r/w PFN for the currently selected queue */
45#define VIRTIO_PCI_QUEUE_PFN 8
46
47/* A 16-bit r/o queue size for the currently selected queue */
48#define VIRTIO_PCI_QUEUE_NUM 12
49
50/* A 16-bit r/w queue selector */
51#define VIRTIO_PCI_QUEUE_SEL 14
52
53/* A 16-bit r/w queue notifier */
54#define VIRTIO_PCI_QUEUE_NOTIFY 16
55
56/* An 8-bit device status register. */
57#define VIRTIO_PCI_STATUS 18
58
59/* An 8-bit r/o interrupt status register. Reading the value will return the
60 * current contents of the ISR and will also clear it. This is effectively
61 * a read-and-acknowledge. */
62#define VIRTIO_PCI_ISR 19
63
aba800a3
MT
64/* MSI-X registers: only enabled if MSI-X is enabled. */
65/* A 16-bit vector for configuration changes. */
66#define VIRTIO_MSI_CONFIG_VECTOR 20
67/* A 16-bit vector for selected queue notifications. */
68#define VIRTIO_MSI_QUEUE_VECTOR 22
69
70/* Config space size */
71#define VIRTIO_PCI_CONFIG_NOMSI 20
72#define VIRTIO_PCI_CONFIG_MSI 24
73#define VIRTIO_PCI_REGION_SIZE(dev) (msix_present(dev) ? \
74 VIRTIO_PCI_CONFIG_MSI : \
75 VIRTIO_PCI_CONFIG_NOMSI)
76
77/* The remaining space is defined by each driver as the per-driver
78 * configuration space */
79#define VIRTIO_PCI_CONFIG(dev) (msix_enabled(dev) ? \
80 VIRTIO_PCI_CONFIG_MSI : \
81 VIRTIO_PCI_CONFIG_NOMSI)
53c25cea 82
53c25cea
PB
83/* How many bits to shift physical queue address written to QUEUE_PFN.
84 * 12 is historical, and due to x86 page size. */
85#define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
86
3dbca8e6
SH
87/* Flags track per-device state like workarounds for quirks in older guests. */
88#define VIRTIO_PCI_FLAG_BUS_MASTER_BUG (1 << 0)
c81131db 89
53c25cea
PB
90/* QEMU doesn't strictly need write barriers since everything runs in
91 * lock-step. We'll leave the calls to wmb() in though to make it obvious for
92 * KVM or if kqemu gets SMP support.
93 */
94#define wmb() do { } while (0)
95
82afa586
BH
96/* HACK for virtio to determine if it's running a big endian guest */
97bool virtio_is_big_endian(void);
98
53c25cea
PB
99/* virtio device */
100
7055e687 101static void virtio_pci_notify(void *opaque, uint16_t vector)
53c25cea
PB
102{
103 VirtIOPCIProxy *proxy = opaque;
aba800a3
MT
104 if (msix_enabled(&proxy->pci_dev))
105 msix_notify(&proxy->pci_dev, vector);
106 else
107 qemu_set_irq(proxy->pci_dev.irq[0], proxy->vdev->isr & 1);
53c25cea
PB
108}
109
ff24bd58
MT
110static void virtio_pci_save_config(void * opaque, QEMUFile *f)
111{
112 VirtIOPCIProxy *proxy = opaque;
113 pci_device_save(&proxy->pci_dev, f);
114 msix_save(&proxy->pci_dev, f);
115 if (msix_present(&proxy->pci_dev))
116 qemu_put_be16(f, proxy->vdev->config_vector);
117}
118
119static void virtio_pci_save_queue(void * opaque, int n, QEMUFile *f)
120{
121 VirtIOPCIProxy *proxy = opaque;
122 if (msix_present(&proxy->pci_dev))
123 qemu_put_be16(f, virtio_queue_vector(proxy->vdev, n));
124}
125
126static int virtio_pci_load_config(void * opaque, QEMUFile *f)
127{
128 VirtIOPCIProxy *proxy = opaque;
129 int ret;
130 ret = pci_device_load(&proxy->pci_dev, f);
e6da7680 131 if (ret) {
ff24bd58 132 return ret;
e6da7680 133 }
ff24bd58 134 msix_load(&proxy->pci_dev, f);
e6da7680 135 if (msix_present(&proxy->pci_dev)) {
ff24bd58 136 qemu_get_be16s(f, &proxy->vdev->config_vector);
e6da7680
MT
137 } else {
138 proxy->vdev->config_vector = VIRTIO_NO_VECTOR;
139 }
140 if (proxy->vdev->config_vector != VIRTIO_NO_VECTOR) {
141 return msix_vector_use(&proxy->pci_dev, proxy->vdev->config_vector);
142 }
ff24bd58
MT
143 return 0;
144}
145
146static int virtio_pci_load_queue(void * opaque, int n, QEMUFile *f)
147{
148 VirtIOPCIProxy *proxy = opaque;
149 uint16_t vector;
e6da7680
MT
150 if (msix_present(&proxy->pci_dev)) {
151 qemu_get_be16s(f, &vector);
152 } else {
153 vector = VIRTIO_NO_VECTOR;
154 }
ff24bd58 155 virtio_queue_set_vector(proxy->vdev, n, vector);
e6da7680
MT
156 if (vector != VIRTIO_NO_VECTOR) {
157 return msix_vector_use(&proxy->pci_dev, vector);
158 }
ff24bd58
MT
159 return 0;
160}
161
25db9ebe
SH
162static int virtio_pci_set_host_notifier_internal(VirtIOPCIProxy *proxy,
163 int n, bool assign)
164{
165 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
166 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
da146d0a
AK
167 int r = 0;
168
25db9ebe
SH
169 if (assign) {
170 r = event_notifier_init(notifier, 1);
171 if (r < 0) {
b36e3914
MT
172 error_report("%s: unable to init event notifier: %d",
173 __func__, r);
25db9ebe
SH
174 return r;
175 }
da146d0a
AK
176 memory_region_add_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
177 true, n, event_notifier_get_fd(notifier));
25db9ebe 178 } else {
da146d0a
AK
179 memory_region_del_eventfd(&proxy->bar, VIRTIO_PCI_QUEUE_NOTIFY, 2,
180 true, n, event_notifier_get_fd(notifier));
25db9ebe
SH
181 /* Handle the race condition where the guest kicked and we deassigned
182 * before we got around to handling the kick.
183 */
184 if (event_notifier_test_and_clear(notifier)) {
185 virtio_queue_notify_vq(vq);
186 }
187
188 event_notifier_cleanup(notifier);
189 }
190 return r;
191}
192
193static void virtio_pci_host_notifier_read(void *opaque)
194{
195 VirtQueue *vq = opaque;
196 EventNotifier *n = virtio_queue_get_host_notifier(vq);
197 if (event_notifier_test_and_clear(n)) {
198 virtio_queue_notify_vq(vq);
199 }
200}
201
202static void virtio_pci_set_host_notifier_fd_handler(VirtIOPCIProxy *proxy,
203 int n, bool assign)
204{
205 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
206 EventNotifier *notifier = virtio_queue_get_host_notifier(vq);
207 if (assign) {
208 qemu_set_fd_handler(event_notifier_get_fd(notifier),
209 virtio_pci_host_notifier_read, NULL, vq);
210 } else {
211 qemu_set_fd_handler(event_notifier_get_fd(notifier),
212 NULL, NULL, NULL);
213 }
214}
215
b36e3914 216static void virtio_pci_start_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe
SH
217{
218 int n, r;
219
220 if (!(proxy->flags & VIRTIO_PCI_FLAG_USE_IOEVENTFD) ||
221 proxy->ioeventfd_disabled ||
222 proxy->ioeventfd_started) {
b36e3914 223 return;
25db9ebe
SH
224 }
225
226 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
227 if (!virtio_queue_get_num(proxy->vdev, n)) {
228 continue;
229 }
230
231 r = virtio_pci_set_host_notifier_internal(proxy, n, true);
232 if (r < 0) {
233 goto assign_error;
234 }
235
236 virtio_pci_set_host_notifier_fd_handler(proxy, n, true);
237 }
238 proxy->ioeventfd_started = true;
b36e3914 239 return;
25db9ebe
SH
240
241assign_error:
242 while (--n >= 0) {
243 if (!virtio_queue_get_num(proxy->vdev, n)) {
244 continue;
245 }
246
247 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
b36e3914
MT
248 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
249 assert(r >= 0);
25db9ebe
SH
250 }
251 proxy->ioeventfd_started = false;
b36e3914 252 error_report("%s: failed. Fallback to a userspace (slower).", __func__);
25db9ebe
SH
253}
254
b36e3914 255static void virtio_pci_stop_ioeventfd(VirtIOPCIProxy *proxy)
25db9ebe 256{
b36e3914 257 int r;
25db9ebe
SH
258 int n;
259
260 if (!proxy->ioeventfd_started) {
b36e3914 261 return;
25db9ebe
SH
262 }
263
264 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
265 if (!virtio_queue_get_num(proxy->vdev, n)) {
266 continue;
267 }
268
269 virtio_pci_set_host_notifier_fd_handler(proxy, n, false);
b36e3914
MT
270 r = virtio_pci_set_host_notifier_internal(proxy, n, false);
271 assert(r >= 0);
25db9ebe
SH
272 }
273 proxy->ioeventfd_started = false;
25db9ebe
SH
274}
275
8798d6c9 276void virtio_pci_reset(DeviceState *d)
7055e687 277{
e489030d 278 VirtIOPCIProxy *proxy = container_of(d, VirtIOPCIProxy, pci_dev.qdev);
25db9ebe 279 virtio_pci_stop_ioeventfd(proxy);
7055e687 280 virtio_reset(proxy->vdev);
25db9ebe 281 proxy->flags &= ~VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
7055e687
MT
282}
283
53c25cea
PB
284static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)
285{
286 VirtIOPCIProxy *proxy = opaque;
287 VirtIODevice *vdev = proxy->vdev;
c227f099 288 target_phys_addr_t pa;
53c25cea 289
53c25cea
PB
290 switch (addr) {
291 case VIRTIO_PCI_GUEST_FEATURES:
292 /* Guest does not negotiate properly? We have to assume nothing. */
293 if (val & (1 << VIRTIO_F_BAD_FEATURE)) {
ad0c9332 294 val = vdev->bad_features ? vdev->bad_features(vdev) : 0;
53c25cea 295 }
ad0c9332 296 virtio_set_features(vdev, val);
53c25cea
PB
297 break;
298 case VIRTIO_PCI_QUEUE_PFN:
c227f099 299 pa = (target_phys_addr_t)val << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
1b8e9b27 300 if (pa == 0) {
25db9ebe 301 virtio_pci_stop_ioeventfd(proxy);
1b8e9b27
MT
302 virtio_reset(proxy->vdev);
303 msix_unuse_all_vectors(&proxy->pci_dev);
304 }
7055e687
MT
305 else
306 virtio_queue_set_addr(vdev, vdev->queue_sel, pa);
53c25cea
PB
307 break;
308 case VIRTIO_PCI_QUEUE_SEL:
309 if (val < VIRTIO_PCI_QUEUE_MAX)
310 vdev->queue_sel = val;
311 break;
312 case VIRTIO_PCI_QUEUE_NOTIFY:
7157e2e2
SH
313 if (val < VIRTIO_PCI_QUEUE_MAX) {
314 virtio_queue_notify(vdev, val);
315 }
53c25cea
PB
316 break;
317 case VIRTIO_PCI_STATUS:
25db9ebe
SH
318 if (!(val & VIRTIO_CONFIG_S_DRIVER_OK)) {
319 virtio_pci_stop_ioeventfd(proxy);
320 }
321
3e607cb5 322 virtio_set_status(vdev, val & 0xFF);
25db9ebe
SH
323
324 if (val & VIRTIO_CONFIG_S_DRIVER_OK) {
325 virtio_pci_start_ioeventfd(proxy);
326 }
327
1b8e9b27
MT
328 if (vdev->status == 0) {
329 virtio_reset(proxy->vdev);
330 msix_unuse_all_vectors(&proxy->pci_dev);
331 }
c81131db
AG
332
333 /* Linux before 2.6.34 sets the device as OK without enabling
334 the PCI device bus master bit. In this case we need to disable
335 some safety checks. */
336 if ((val & VIRTIO_CONFIG_S_DRIVER_OK) &&
337 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
3dbca8e6 338 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
c81131db 339 }
53c25cea 340 break;
aba800a3
MT
341 case VIRTIO_MSI_CONFIG_VECTOR:
342 msix_vector_unuse(&proxy->pci_dev, vdev->config_vector);
343 /* Make it possible for guest to discover an error took place. */
344 if (msix_vector_use(&proxy->pci_dev, val) < 0)
345 val = VIRTIO_NO_VECTOR;
346 vdev->config_vector = val;
347 break;
348 case VIRTIO_MSI_QUEUE_VECTOR:
349 msix_vector_unuse(&proxy->pci_dev,
350 virtio_queue_vector(vdev, vdev->queue_sel));
351 /* Make it possible for guest to discover an error took place. */
352 if (msix_vector_use(&proxy->pci_dev, val) < 0)
353 val = VIRTIO_NO_VECTOR;
354 virtio_queue_set_vector(vdev, vdev->queue_sel, val);
355 break;
356 default:
4e02d460
SH
357 error_report("%s: unexpected address 0x%x value 0x%x",
358 __func__, addr, val);
aba800a3 359 break;
53c25cea
PB
360 }
361}
362
aba800a3 363static uint32_t virtio_ioport_read(VirtIOPCIProxy *proxy, uint32_t addr)
53c25cea 364{
53c25cea
PB
365 VirtIODevice *vdev = proxy->vdev;
366 uint32_t ret = 0xFFFFFFFF;
367
53c25cea
PB
368 switch (addr) {
369 case VIRTIO_PCI_HOST_FEATURES:
8172539d 370 ret = proxy->host_features;
53c25cea
PB
371 break;
372 case VIRTIO_PCI_GUEST_FEATURES:
704a76fc 373 ret = vdev->guest_features;
53c25cea
PB
374 break;
375 case VIRTIO_PCI_QUEUE_PFN:
376 ret = virtio_queue_get_addr(vdev, vdev->queue_sel)
377 >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
378 break;
379 case VIRTIO_PCI_QUEUE_NUM:
380 ret = virtio_queue_get_num(vdev, vdev->queue_sel);
381 break;
382 case VIRTIO_PCI_QUEUE_SEL:
383 ret = vdev->queue_sel;
384 break;
385 case VIRTIO_PCI_STATUS:
386 ret = vdev->status;
387 break;
388 case VIRTIO_PCI_ISR:
389 /* reading from the ISR also clears it. */
390 ret = vdev->isr;
391 vdev->isr = 0;
7055e687 392 qemu_set_irq(proxy->pci_dev.irq[0], 0);
53c25cea 393 break;
aba800a3
MT
394 case VIRTIO_MSI_CONFIG_VECTOR:
395 ret = vdev->config_vector;
396 break;
397 case VIRTIO_MSI_QUEUE_VECTOR:
398 ret = virtio_queue_vector(vdev, vdev->queue_sel);
399 break;
53c25cea
PB
400 default:
401 break;
402 }
403
404 return ret;
405}
406
407static uint32_t virtio_pci_config_readb(void *opaque, uint32_t addr)
408{
409 VirtIOPCIProxy *proxy = opaque;
aba800a3 410 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
411 if (addr < config)
412 return virtio_ioport_read(proxy, addr);
413 addr -= config;
53c25cea
PB
414 return virtio_config_readb(proxy->vdev, addr);
415}
416
417static uint32_t virtio_pci_config_readw(void *opaque, uint32_t addr)
418{
419 VirtIOPCIProxy *proxy = opaque;
aba800a3 420 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
82afa586 421 uint16_t val;
aba800a3
MT
422 if (addr < config)
423 return virtio_ioport_read(proxy, addr);
424 addr -= config;
82afa586
BH
425 val = virtio_config_readw(proxy->vdev, addr);
426 if (virtio_is_big_endian()) {
427 /*
428 * virtio is odd, ioports are LE but config space is target native
429 * endian. However, in qemu, all PIO is LE, so we need to re-swap
430 * on BE targets
431 */
432 val = bswap16(val);
433 }
434 return val;
53c25cea
PB
435}
436
437static uint32_t virtio_pci_config_readl(void *opaque, uint32_t addr)
438{
439 VirtIOPCIProxy *proxy = opaque;
aba800a3 440 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
82afa586 441 uint32_t val;
aba800a3
MT
442 if (addr < config)
443 return virtio_ioport_read(proxy, addr);
444 addr -= config;
82afa586
BH
445 val = virtio_config_readl(proxy->vdev, addr);
446 if (virtio_is_big_endian()) {
447 val = bswap32(val);
448 }
449 return val;
53c25cea
PB
450}
451
452static void virtio_pci_config_writeb(void *opaque, uint32_t addr, uint32_t val)
453{
454 VirtIOPCIProxy *proxy = opaque;
aba800a3 455 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
456 if (addr < config) {
457 virtio_ioport_write(proxy, addr, val);
458 return;
459 }
460 addr -= config;
53c25cea
PB
461 virtio_config_writeb(proxy->vdev, addr, val);
462}
463
464static void virtio_pci_config_writew(void *opaque, uint32_t addr, uint32_t val)
465{
466 VirtIOPCIProxy *proxy = opaque;
aba800a3 467 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
468 if (addr < config) {
469 virtio_ioport_write(proxy, addr, val);
470 return;
471 }
472 addr -= config;
82afa586
BH
473 if (virtio_is_big_endian()) {
474 val = bswap16(val);
475 }
53c25cea
PB
476 virtio_config_writew(proxy->vdev, addr, val);
477}
478
479static void virtio_pci_config_writel(void *opaque, uint32_t addr, uint32_t val)
480{
481 VirtIOPCIProxy *proxy = opaque;
aba800a3 482 uint32_t config = VIRTIO_PCI_CONFIG(&proxy->pci_dev);
aba800a3
MT
483 if (addr < config) {
484 virtio_ioport_write(proxy, addr, val);
485 return;
486 }
487 addr -= config;
82afa586
BH
488 if (virtio_is_big_endian()) {
489 val = bswap32(val);
490 }
53c25cea
PB
491 virtio_config_writel(proxy->vdev, addr, val);
492}
493
4636b9d1 494static const MemoryRegionPortio virtio_portio[] = {
da146d0a
AK
495 { 0, 0x10000, 1, .write = virtio_pci_config_writeb, },
496 { 0, 0x10000, 2, .write = virtio_pci_config_writew, },
497 { 0, 0x10000, 4, .write = virtio_pci_config_writel, },
498 { 0, 0x10000, 1, .read = virtio_pci_config_readb, },
499 { 0, 0x10000, 2, .read = virtio_pci_config_readw, },
500 { 0, 0x10000, 4, .read = virtio_pci_config_readl, },
501 PORTIO_END_OF_LIST()
502};
53c25cea 503
da146d0a
AK
504static const MemoryRegionOps virtio_pci_config_ops = {
505 .old_portio = virtio_portio,
506 .endianness = DEVICE_LITTLE_ENDIAN,
507};
aba800a3
MT
508
509static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
510 uint32_t val, int len)
511{
ed757e14
YV
512 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
513
1129714f
MT
514 pci_default_write_config(pci_dev, address, val, len);
515
516 if (range_covers_byte(address, len, PCI_COMMAND) &&
517 !(pci_dev->config[PCI_COMMAND] & PCI_COMMAND_MASTER) &&
518 !(proxy->flags & VIRTIO_PCI_FLAG_BUS_MASTER_BUG)) {
519 virtio_pci_stop_ioeventfd(proxy);
520 virtio_set_status(proxy->vdev,
521 proxy->vdev->status & ~VIRTIO_CONFIG_S_DRIVER_OK);
ed757e14
YV
522 }
523
85352471 524 msix_write_config(pci_dev, address, val, len);
53c25cea
PB
525}
526
6d74ca5a
MT
527static unsigned virtio_pci_get_features(void *opaque)
528{
8172539d
MT
529 VirtIOPCIProxy *proxy = opaque;
530 return proxy->host_features;
6d74ca5a
MT
531}
532
ade80dc8
MT
533static void virtio_pci_guest_notifier_read(void *opaque)
534{
535 VirtQueue *vq = opaque;
536 EventNotifier *n = virtio_queue_get_guest_notifier(vq);
537 if (event_notifier_test_and_clear(n)) {
538 virtio_irq(vq);
539 }
540}
541
7d37d351
JK
542static int kvm_virtio_pci_vq_vector_use(VirtIOPCIProxy *proxy,
543 unsigned int queue_no,
544 unsigned int vector,
545 MSIMessage msg)
546{
547 VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
548 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
549 int fd, ret;
550
551 fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vq));
552
553 if (irqfd->users == 0) {
554 ret = kvm_irqchip_add_msi_route(kvm_state, msg);
555 if (ret < 0) {
556 return ret;
557 }
558 irqfd->virq = ret;
559 }
560 irqfd->users++;
561
562 ret = kvm_irqchip_add_irqfd(kvm_state, fd, irqfd->virq);
563 if (ret < 0) {
564 if (--irqfd->users == 0) {
565 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
566 }
567 return ret;
568 }
569
570 qemu_set_fd_handler(fd, NULL, NULL, NULL);
571
572 return 0;
573}
574
575static void kvm_virtio_pci_vq_vector_release(VirtIOPCIProxy *proxy,
576 unsigned int queue_no,
577 unsigned int vector)
578{
579 VirtQueue *vq = virtio_get_queue(proxy->vdev, queue_no);
580 VirtIOIRQFD *irqfd = &proxy->vector_irqfd[vector];
581 int fd, ret;
582
583 fd = event_notifier_get_fd(virtio_queue_get_guest_notifier(vq));
584
585 ret = kvm_irqchip_remove_irqfd(kvm_state, fd, irqfd->virq);
586 assert(ret == 0);
587
588 if (--irqfd->users == 0) {
589 kvm_irqchip_release_virq(kvm_state, irqfd->virq);
590 }
591
592 qemu_set_fd_handler(fd, virtio_pci_guest_notifier_read, NULL, vq);
593}
594
595static int kvm_virtio_pci_vector_use(PCIDevice *dev, unsigned vector,
596 MSIMessage msg)
597{
598 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
599 VirtIODevice *vdev = proxy->vdev;
600 int ret, queue_no;
601
602 for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
603 if (!virtio_queue_get_num(vdev, queue_no)) {
604 break;
605 }
606 if (virtio_queue_vector(vdev, queue_no) != vector) {
607 continue;
608 }
609 ret = kvm_virtio_pci_vq_vector_use(proxy, queue_no, vector, msg);
610 if (ret < 0) {
611 goto undo;
612 }
613 }
614 return 0;
615
616undo:
617 while (--queue_no >= 0) {
618 if (virtio_queue_vector(vdev, queue_no) != vector) {
619 continue;
620 }
621 kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
622 }
623 return ret;
624}
625
626static void kvm_virtio_pci_vector_release(PCIDevice *dev, unsigned vector)
627{
628 VirtIOPCIProxy *proxy = container_of(dev, VirtIOPCIProxy, pci_dev);
629 VirtIODevice *vdev = proxy->vdev;
630 int queue_no;
631
632 for (queue_no = 0; queue_no < VIRTIO_PCI_QUEUE_MAX; queue_no++) {
633 if (!virtio_queue_get_num(vdev, queue_no)) {
634 break;
635 }
636 if (virtio_queue_vector(vdev, queue_no) != vector) {
637 continue;
638 }
639 kvm_virtio_pci_vq_vector_release(proxy, queue_no, vector);
640 }
641}
642
ade80dc8
MT
643static int virtio_pci_set_guest_notifier(void *opaque, int n, bool assign)
644{
645 VirtIOPCIProxy *proxy = opaque;
646 VirtQueue *vq = virtio_get_queue(proxy->vdev, n);
647 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
648
649 if (assign) {
650 int r = event_notifier_init(notifier, 0);
651 if (r < 0) {
652 return r;
653 }
654 qemu_set_fd_handler(event_notifier_get_fd(notifier),
655 virtio_pci_guest_notifier_read, NULL, vq);
656 } else {
657 qemu_set_fd_handler(event_notifier_get_fd(notifier),
658 NULL, NULL, NULL);
7d37d351
JK
659 /* Test and clear notifier before closing it,
660 * in case poll callback didn't have time to run. */
661 virtio_pci_guest_notifier_read(vq);
ade80dc8
MT
662 event_notifier_cleanup(notifier);
663 }
664
665 return 0;
666}
667
5430a28f
MT
668static bool virtio_pci_query_guest_notifiers(void *opaque)
669{
670 VirtIOPCIProxy *proxy = opaque;
671 return msix_enabled(&proxy->pci_dev);
672}
673
54dd9321
MT
674static int virtio_pci_set_guest_notifiers(void *opaque, bool assign)
675{
676 VirtIOPCIProxy *proxy = opaque;
677 VirtIODevice *vdev = proxy->vdev;
678 int r, n;
679
7d37d351
JK
680 /* Must unset vector notifier while guest notifier is still assigned */
681 if (kvm_irqchip_in_kernel() && !assign) {
682 msix_unset_vector_notifiers(&proxy->pci_dev);
683 g_free(proxy->vector_irqfd);
684 proxy->vector_irqfd = NULL;
685 }
686
54dd9321
MT
687 for (n = 0; n < VIRTIO_PCI_QUEUE_MAX; n++) {
688 if (!virtio_queue_get_num(vdev, n)) {
689 break;
690 }
691
692 r = virtio_pci_set_guest_notifier(opaque, n, assign);
693 if (r < 0) {
694 goto assign_error;
695 }
696 }
697
7d37d351
JK
698 /* Must set vector notifier after guest notifier has been assigned */
699 if (kvm_irqchip_in_kernel() && assign) {
700 proxy->vector_irqfd =
701 g_malloc0(sizeof(*proxy->vector_irqfd) *
702 msix_nr_vectors_allocated(&proxy->pci_dev));
703 r = msix_set_vector_notifiers(&proxy->pci_dev,
704 kvm_virtio_pci_vector_use,
705 kvm_virtio_pci_vector_release);
706 if (r < 0) {
707 goto assign_error;
708 }
709 }
710
54dd9321
MT
711 return 0;
712
713assign_error:
714 /* We get here on assignment failure. Recover by undoing for VQs 0 .. n. */
7d37d351 715 assert(assign);
54dd9321
MT
716 while (--n >= 0) {
717 virtio_pci_set_guest_notifier(opaque, n, !assign);
718 }
719 return r;
720}
721
ade80dc8
MT
722static int virtio_pci_set_host_notifier(void *opaque, int n, bool assign)
723{
724 VirtIOPCIProxy *proxy = opaque;
25db9ebe
SH
725
726 /* Stop using ioeventfd for virtqueue kick if the device starts using host
727 * notifiers. This makes it easy to avoid stepping on each others' toes.
728 */
729 proxy->ioeventfd_disabled = assign;
ade80dc8 730 if (assign) {
25db9ebe
SH
731 virtio_pci_stop_ioeventfd(proxy);
732 }
733 /* We don't need to start here: it's not needed because backend
734 * currently only stops on status change away from ok,
735 * reset, vmstop and such. If we do add code to start here,
736 * need to check vmstate, device state etc. */
737 return virtio_pci_set_host_notifier_internal(proxy, n, assign);
738}
739
740static void virtio_pci_vmstate_change(void *opaque, bool running)
741{
742 VirtIOPCIProxy *proxy = opaque;
743
744 if (running) {
89c473fd
MT
745 /* Try to find out if the guest has bus master disabled, but is
746 in ready state. Then we have a buggy guest OS. */
747 if ((proxy->vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) &&
748 !(proxy->pci_dev.config[PCI_COMMAND] & PCI_COMMAND_MASTER)) {
749 proxy->flags |= VIRTIO_PCI_FLAG_BUS_MASTER_BUG;
750 }
25db9ebe 751 virtio_pci_start_ioeventfd(proxy);
ade80dc8 752 } else {
25db9ebe 753 virtio_pci_stop_ioeventfd(proxy);
ade80dc8 754 }
ade80dc8
MT
755}
756
53c25cea 757static const VirtIOBindings virtio_pci_bindings = {
ff24bd58
MT
758 .notify = virtio_pci_notify,
759 .save_config = virtio_pci_save_config,
760 .load_config = virtio_pci_load_config,
761 .save_queue = virtio_pci_save_queue,
762 .load_queue = virtio_pci_load_queue,
6d74ca5a 763 .get_features = virtio_pci_get_features,
5430a28f 764 .query_guest_notifiers = virtio_pci_query_guest_notifiers,
ade80dc8 765 .set_host_notifier = virtio_pci_set_host_notifier,
54dd9321 766 .set_guest_notifiers = virtio_pci_set_guest_notifiers,
25db9ebe 767 .vmstate_change = virtio_pci_vmstate_change,
53c25cea
PB
768};
769
befeac45 770void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev)
53c25cea
PB
771{
772 uint8_t *config;
773 uint32_t size;
774
775 proxy->vdev = vdev;
776
777 config = proxy->pci_dev.config;
53c25cea 778
e75ccf2c
IY
779 if (proxy->class_code) {
780 pci_config_set_class(config, proxy->class_code);
781 }
ad3d11e6
HKR
782 pci_set_word(config + PCI_SUBSYSTEM_VENDOR_ID,
783 pci_get_word(config + PCI_VENDOR_ID));
784 pci_set_word(config + PCI_SUBSYSTEM_ID, vdev->device_id);
785 config[PCI_INTERRUPT_PIN] = 1;
53c25cea 786
95524ae8
AK
787 memory_region_init(&proxy->msix_bar, "virtio-msix", 4096);
788 if (vdev->nvectors && !msix_init(&proxy->pci_dev, vdev->nvectors,
789 &proxy->msix_bar, 1, 0)) {
e824b2cc
AK
790 pci_register_bar(&proxy->pci_dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY,
791 &proxy->msix_bar);
aba800a3
MT
792 } else
793 vdev->nvectors = 0;
794
ed757e14
YV
795 proxy->pci_dev.config_write = virtio_write_config;
796
aba800a3 797 size = VIRTIO_PCI_REGION_SIZE(&proxy->pci_dev) + vdev->config_len;
53c25cea
PB
798 if (size & (size-1))
799 size = 1 << qemu_fls(size);
800
da146d0a
AK
801 memory_region_init_io(&proxy->bar, &virtio_pci_config_ops, proxy,
802 "virtio-pci", size);
e824b2cc
AK
803 pci_register_bar(&proxy->pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO,
804 &proxy->bar);
53c25cea 805
25db9ebe
SH
806 if (!kvm_has_many_ioeventfds()) {
807 proxy->flags &= ~VIRTIO_PCI_FLAG_USE_IOEVENTFD;
808 }
809
53c25cea 810 virtio_bind_device(vdev, &virtio_pci_bindings, proxy);
8172539d
MT
811 proxy->host_features |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY;
812 proxy->host_features |= 0x1 << VIRTIO_F_BAD_FEATURE;
813 proxy->host_features = vdev->get_features(vdev, proxy->host_features);
53c25cea
PB
814}
815
81a322d4 816static int virtio_blk_init_pci(PCIDevice *pci_dev)
53c25cea
PB
817{
818 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
819 VirtIODevice *vdev;
820
ab73ff29
GH
821 if (proxy->class_code != PCI_CLASS_STORAGE_SCSI &&
822 proxy->class_code != PCI_CLASS_STORAGE_OTHER)
823 proxy->class_code = PCI_CLASS_STORAGE_SCSI;
53c25cea 824
12c5674b 825 vdev = virtio_blk_init(&pci_dev->qdev, &proxy->blk);
ac0c14d7
MA
826 if (!vdev) {
827 return -1;
828 }
177539e0 829 vdev->nvectors = proxy->nvectors;
e75ccf2c 830 virtio_init_pci(proxy, vdev);
177539e0
GH
831 /* make the actual value visible */
832 proxy->nvectors = vdev->nvectors;
81a322d4 833 return 0;
21d58b57
MM
834}
835
0f457d91
MT
836static int virtio_exit_pci(PCIDevice *pci_dev)
837{
da146d0a 838 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
95524ae8 839 int r;
da146d0a
AK
840
841 memory_region_destroy(&proxy->bar);
95524ae8
AK
842 r = msix_uninit(pci_dev, &proxy->msix_bar);
843 memory_region_destroy(&proxy->msix_bar);
844 return r;
0f457d91
MT
845}
846
56a14938
GH
847static int virtio_blk_exit_pci(PCIDevice *pci_dev)
848{
849 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
850
25db9ebe 851 virtio_pci_stop_ioeventfd(proxy);
9d0d3138 852 virtio_blk_exit(proxy->vdev);
0f457d91 853 return virtio_exit_pci(pci_dev);
56a14938
GH
854}
855
98b19252 856static int virtio_serial_init_pci(PCIDevice *pci_dev)
21d58b57 857{
d6beee99 858 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
85c2c735
MM
859 VirtIODevice *vdev;
860
d6beee99
GH
861 if (proxy->class_code != PCI_CLASS_COMMUNICATION_OTHER &&
862 proxy->class_code != PCI_CLASS_DISPLAY_OTHER && /* qemu 0.10 */
863 proxy->class_code != PCI_CLASS_OTHERS) /* qemu-kvm */
864 proxy->class_code = PCI_CLASS_COMMUNICATION_OTHER;
865
6b331efb 866 vdev = virtio_serial_init(&pci_dev->qdev, &proxy->serial);
25fe3654
AS
867 if (!vdev) {
868 return -1;
869 }
573fb60c 870 vdev->nvectors = proxy->nvectors == DEV_NVECTORS_UNSPECIFIED
6b331efb 871 ? proxy->serial.max_virtserial_ports + 1
573fb60c 872 : proxy->nvectors;
e75ccf2c 873 virtio_init_pci(proxy, vdev);
a1829205 874 proxy->nvectors = vdev->nvectors;
81a322d4 875 return 0;
53c25cea
PB
876}
877
8b53a865
AS
878static int virtio_serial_exit_pci(PCIDevice *pci_dev)
879{
880 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
881
32059220 882 virtio_pci_stop_ioeventfd(proxy);
8b53a865
AS
883 virtio_serial_exit(proxy->vdev);
884 return virtio_exit_pci(pci_dev);
885}
886
81a322d4 887static int virtio_net_init_pci(PCIDevice *pci_dev)
53c25cea
PB
888{
889 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
890 VirtIODevice *vdev;
891
f0c07c7c 892 vdev = virtio_net_init(&pci_dev->qdev, &proxy->nic, &proxy->net);
a1e0fea5 893
97b15621 894 vdev->nvectors = proxy->nvectors;
e75ccf2c 895 virtio_init_pci(proxy, vdev);
a1e0fea5
GH
896
897 /* make the actual value visible */
898 proxy->nvectors = vdev->nvectors;
81a322d4 899 return 0;
53c25cea
PB
900}
901
97b15621
GH
902static int virtio_net_exit_pci(PCIDevice *pci_dev)
903{
904 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
905
25db9ebe 906 virtio_pci_stop_ioeventfd(proxy);
97b15621
GH
907 virtio_net_exit(proxy->vdev);
908 return virtio_exit_pci(pci_dev);
909}
910
81a322d4 911static int virtio_balloon_init_pci(PCIDevice *pci_dev)
53c25cea
PB
912{
913 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
914 VirtIODevice *vdev;
915
2ba1d381
DG
916 if (proxy->class_code != PCI_CLASS_OTHERS &&
917 proxy->class_code != PCI_CLASS_MEMORY_RAM) { /* qemu < 1.1 */
918 proxy->class_code = PCI_CLASS_OTHERS;
919 }
920
53c25cea 921 vdev = virtio_balloon_init(&pci_dev->qdev);
f76f6655
AS
922 if (!vdev) {
923 return -1;
924 }
e75ccf2c 925 virtio_init_pci(proxy, vdev);
81a322d4 926 return 0;
53c25cea
PB
927}
928
855d7e25
AS
929static int virtio_balloon_exit_pci(PCIDevice *pci_dev)
930{
931 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
932
933 virtio_pci_stop_ioeventfd(proxy);
934 virtio_balloon_exit(proxy->vdev);
935 return virtio_exit_pci(pci_dev);
936}
937
40021f08
AL
938static Property virtio_blk_properties[] = {
939 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
12c5674b
PB
940 DEFINE_BLOCK_PROPERTIES(VirtIOPCIProxy, blk.conf),
941 DEFINE_PROP_STRING("serial", VirtIOPCIProxy, blk.serial),
a6c5c84a
PB
942#ifdef __linux__
943 DEFINE_PROP_BIT("scsi", VirtIOPCIProxy, blk.scsi, 0, true),
944#endif
40021f08
AL
945 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
946 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
947 DEFINE_VIRTIO_BLK_FEATURES(VirtIOPCIProxy, host_features),
948 DEFINE_PROP_END_OF_LIST(),
e855761c
AL
949};
950
40021f08
AL
951static void virtio_blk_class_init(ObjectClass *klass, void *data)
952{
39bffca2 953 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
954 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
955
956 k->init = virtio_blk_init_pci;
957 k->exit = virtio_blk_exit_pci;
958 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
959 k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
960 k->revision = VIRTIO_PCI_ABI_VERSION;
961 k->class_id = PCI_CLASS_STORAGE_SCSI;
39bffca2
AL
962 dc->reset = virtio_pci_reset;
963 dc->props = virtio_blk_properties;
40021f08
AL
964}
965
39bffca2
AL
966static TypeInfo virtio_blk_info = {
967 .name = "virtio-blk-pci",
968 .parent = TYPE_PCI_DEVICE,
969 .instance_size = sizeof(VirtIOPCIProxy),
970 .class_init = virtio_blk_class_init,
40021f08
AL
971};
972
973static Property virtio_net_properties[] = {
974 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, false),
975 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
976 DEFINE_VIRTIO_NET_FEATURES(VirtIOPCIProxy, host_features),
977 DEFINE_NIC_PROPERTIES(VirtIOPCIProxy, nic),
978 DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy, net.txtimer, TX_TIMER_INTERVAL),
979 DEFINE_PROP_INT32("x-txburst", VirtIOPCIProxy, net.txburst, TX_BURST),
980 DEFINE_PROP_STRING("tx", VirtIOPCIProxy, net.tx),
981 DEFINE_PROP_END_OF_LIST(),
982};
983
984static void virtio_net_class_init(ObjectClass *klass, void *data)
985{
39bffca2 986 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
987 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
988
989 k->init = virtio_net_init_pci;
990 k->exit = virtio_net_exit_pci;
991 k->romfile = "pxe-virtio.rom";
992 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
993 k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
994 k->revision = VIRTIO_PCI_ABI_VERSION;
995 k->class_id = PCI_CLASS_NETWORK_ETHERNET;
39bffca2
AL
996 dc->reset = virtio_pci_reset;
997 dc->props = virtio_net_properties;
40021f08
AL
998}
999
39bffca2
AL
1000static TypeInfo virtio_net_info = {
1001 .name = "virtio-net-pci",
1002 .parent = TYPE_PCI_DEVICE,
1003 .instance_size = sizeof(VirtIOPCIProxy),
1004 .class_init = virtio_net_class_init,
e855761c
AL
1005};
1006
40021f08
AL
1007static Property virtio_serial_properties[] = {
1008 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags, VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
1009 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, DEV_NVECTORS_UNSPECIFIED),
1010 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
1011 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
1012 DEFINE_PROP_UINT32("max_ports", VirtIOPCIProxy, serial.max_virtserial_ports, 31),
1013 DEFINE_PROP_END_OF_LIST(),
e855761c
AL
1014};
1015
40021f08
AL
1016static void virtio_serial_class_init(ObjectClass *klass, void *data)
1017{
39bffca2 1018 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1019 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1020
1021 k->init = virtio_serial_init_pci;
1022 k->exit = virtio_serial_exit_pci;
1023 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1024 k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
1025 k->revision = VIRTIO_PCI_ABI_VERSION;
1026 k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
39bffca2
AL
1027 dc->reset = virtio_pci_reset;
1028 dc->props = virtio_serial_properties;
40021f08
AL
1029}
1030
39bffca2
AL
1031static TypeInfo virtio_serial_info = {
1032 .name = "virtio-serial-pci",
1033 .parent = TYPE_PCI_DEVICE,
1034 .instance_size = sizeof(VirtIOPCIProxy),
1035 .class_init = virtio_serial_class_init,
40021f08
AL
1036};
1037
1038static Property virtio_balloon_properties[] = {
1039 DEFINE_VIRTIO_COMMON_FEATURES(VirtIOPCIProxy, host_features),
2ba1d381 1040 DEFINE_PROP_HEX32("class", VirtIOPCIProxy, class_code, 0),
40021f08
AL
1041 DEFINE_PROP_END_OF_LIST(),
1042};
1043
1044static void virtio_balloon_class_init(ObjectClass *klass, void *data)
1045{
39bffca2 1046 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
1047 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1048
1049 k->init = virtio_balloon_init_pci;
1050 k->exit = virtio_balloon_exit_pci;
1051 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1052 k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
1053 k->revision = VIRTIO_PCI_ABI_VERSION;
2ba1d381 1054 k->class_id = PCI_CLASS_OTHERS;
39bffca2
AL
1055 dc->reset = virtio_pci_reset;
1056 dc->props = virtio_balloon_properties;
40021f08
AL
1057}
1058
39bffca2
AL
1059static TypeInfo virtio_balloon_info = {
1060 .name = "virtio-balloon-pci",
1061 .parent = TYPE_PCI_DEVICE,
1062 .instance_size = sizeof(VirtIOPCIProxy),
1063 .class_init = virtio_balloon_class_init,
0aab0d3a
GH
1064};
1065
973abc7f
SH
1066static int virtio_scsi_init_pci(PCIDevice *pci_dev)
1067{
1068 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1069 VirtIODevice *vdev;
1070
1071 vdev = virtio_scsi_init(&pci_dev->qdev, &proxy->scsi);
1072 if (!vdev) {
1073 return -EINVAL;
1074 }
1075
1076 vdev->nvectors = proxy->nvectors;
1077 virtio_init_pci(proxy, vdev);
1078
1079 /* make the actual value visible */
1080 proxy->nvectors = vdev->nvectors;
1081 return 0;
1082}
1083
1084static int virtio_scsi_exit_pci(PCIDevice *pci_dev)
1085{
1086 VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
1087
1088 virtio_scsi_exit(proxy->vdev);
1089 return virtio_exit_pci(pci_dev);
1090}
1091
1092static Property virtio_scsi_properties[] = {
1093 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 2),
1094 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOPCIProxy, host_features, scsi),
1095 DEFINE_PROP_END_OF_LIST(),
1096};
1097
1098static void virtio_scsi_class_init(ObjectClass *klass, void *data)
1099{
1100 DeviceClass *dc = DEVICE_CLASS(klass);
1101 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
1102
1103 k->init = virtio_scsi_init_pci;
1104 k->exit = virtio_scsi_exit_pci;
1105 k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
1106 k->device_id = PCI_DEVICE_ID_VIRTIO_SCSI;
1107 k->revision = 0x00;
1108 k->class_id = PCI_CLASS_STORAGE_SCSI;
1109 dc->reset = virtio_pci_reset;
1110 dc->props = virtio_scsi_properties;
1111}
1112
1113static TypeInfo virtio_scsi_info = {
1114 .name = "virtio-scsi-pci",
1115 .parent = TYPE_PCI_DEVICE,
1116 .instance_size = sizeof(VirtIOPCIProxy),
1117 .class_init = virtio_scsi_class_init,
1118};
1119
83f7d43a 1120static void virtio_pci_register_types(void)
53c25cea 1121{
39bffca2 1122 type_register_static(&virtio_blk_info);
39bffca2 1123 type_register_static(&virtio_net_info);
39bffca2 1124 type_register_static(&virtio_serial_info);
39bffca2 1125 type_register_static(&virtio_balloon_info);
973abc7f 1126 type_register_static(&virtio_scsi_info);
53c25cea
PB
1127}
1128
83f7d43a 1129type_init(virtio_pci_register_types)