xref: /qemu/hw/vfio-user/pci.c (revision c6ac52a4d8f7a7c03452454d36b60ac309f0b9ce)
19fca2b7dSJohn Levon /*
29fca2b7dSJohn Levon  * vfio PCI device over a UNIX socket.
39fca2b7dSJohn Levon  *
49fca2b7dSJohn Levon  * Copyright © 2018, 2021 Oracle and/or its affiliates.
59fca2b7dSJohn Levon  *
69fca2b7dSJohn Levon  * SPDX-License-Identifier: GPL-2.0-or-later
79fca2b7dSJohn Levon  */
89fca2b7dSJohn Levon 
99fca2b7dSJohn Levon #include <sys/ioctl.h>
109fca2b7dSJohn Levon #include "qemu/osdep.h"
119fca2b7dSJohn Levon #include "qapi-visit-sockets.h"
12*c6ac52a4SJohn Levon #include "qemu/error-report.h"
139fca2b7dSJohn Levon 
149fca2b7dSJohn Levon #include "hw/qdev-properties.h"
159fca2b7dSJohn Levon #include "hw/vfio/pci.h"
16667866d6SJohn Levon #include "hw/vfio-user/device.h"
17438d863fSJohn Levon #include "hw/vfio-user/proxy.h"
189fca2b7dSJohn Levon 
199fca2b7dSJohn Levon #define TYPE_VFIO_USER_PCI "vfio-user-pci"
209fca2b7dSJohn Levon OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)
219fca2b7dSJohn Levon 
229fca2b7dSJohn Levon struct VFIOUserPCIDevice {
239fca2b7dSJohn Levon     VFIOPCIDevice device;
249fca2b7dSJohn Levon     SocketAddress *socket;
2536227628SJohn Levon     bool send_queued;   /* all sends are queued */
269fca2b7dSJohn Levon };
279fca2b7dSJohn Levon 
289fca2b7dSJohn Levon /*
29777e45c7SJohn Levon  * The server maintains the device's pending interrupts,
30777e45c7SJohn Levon  * via its MSIX table and PBA, so we treat these accesses
31777e45c7SJohn Levon  * like PCI config space and forward them.
32777e45c7SJohn Levon  */
33777e45c7SJohn Levon static uint64_t vfio_user_pba_read(void *opaque, hwaddr addr,
34777e45c7SJohn Levon                                    unsigned size)
35777e45c7SJohn Levon {
36777e45c7SJohn Levon     VFIOPCIDevice *vdev = opaque;
37777e45c7SJohn Levon     VFIORegion *region = &vdev->bars[vdev->msix->pba_bar].region;
38777e45c7SJohn Levon     uint64_t data;
39777e45c7SJohn Levon 
40777e45c7SJohn Levon     /* server copy is what matters */
41777e45c7SJohn Levon     data = vfio_region_read(region, addr + vdev->msix->pba_offset, size);
42777e45c7SJohn Levon     return data;
43777e45c7SJohn Levon }
44777e45c7SJohn Levon 
45777e45c7SJohn Levon static void vfio_user_pba_write(void *opaque, hwaddr addr,
46777e45c7SJohn Levon                                   uint64_t data, unsigned size)
47777e45c7SJohn Levon {
48777e45c7SJohn Levon     /* dropped */
49777e45c7SJohn Levon }
50777e45c7SJohn Levon 
51777e45c7SJohn Levon static const MemoryRegionOps vfio_user_pba_ops = {
52777e45c7SJohn Levon     .read = vfio_user_pba_read,
53777e45c7SJohn Levon     .write = vfio_user_pba_write,
54777e45c7SJohn Levon     .endianness = DEVICE_LITTLE_ENDIAN,
55777e45c7SJohn Levon };
56777e45c7SJohn Levon 
57777e45c7SJohn Levon static void vfio_user_msix_setup(VFIOPCIDevice *vdev)
58777e45c7SJohn Levon {
59777e45c7SJohn Levon     MemoryRegion *vfio_reg, *msix_reg, *pba_reg;
60777e45c7SJohn Levon 
61777e45c7SJohn Levon     pba_reg = g_new0(MemoryRegion, 1);
62777e45c7SJohn Levon     vdev->msix->pba_region = pba_reg;
63777e45c7SJohn Levon 
64777e45c7SJohn Levon     vfio_reg = vdev->bars[vdev->msix->pba_bar].mr;
65777e45c7SJohn Levon     msix_reg = &vdev->pdev.msix_pba_mmio;
66777e45c7SJohn Levon     memory_region_init_io(pba_reg, OBJECT(vdev), &vfio_user_pba_ops, vdev,
67777e45c7SJohn Levon                           "VFIO MSIX PBA", int128_get64(msix_reg->size));
68777e45c7SJohn Levon     memory_region_add_subregion_overlap(vfio_reg, vdev->msix->pba_offset,
69777e45c7SJohn Levon                                         pba_reg, 1);
70777e45c7SJohn Levon }
71777e45c7SJohn Levon 
72777e45c7SJohn Levon static void vfio_user_msix_teardown(VFIOPCIDevice *vdev)
73777e45c7SJohn Levon {
74777e45c7SJohn Levon     MemoryRegion *mr, *sub;
75777e45c7SJohn Levon 
76777e45c7SJohn Levon     mr = vdev->bars[vdev->msix->pba_bar].mr;
77777e45c7SJohn Levon     sub = vdev->msix->pba_region;
78777e45c7SJohn Levon     memory_region_del_subregion(mr, sub);
79777e45c7SJohn Levon 
80777e45c7SJohn Levon     g_free(vdev->msix->pba_region);
81777e45c7SJohn Levon     vdev->msix->pba_region = NULL;
82777e45c7SJohn Levon }
83777e45c7SJohn Levon 
84*c6ac52a4SJohn Levon static void vfio_user_dma_read(VFIOPCIDevice *vdev, VFIOUserDMARW *msg)
85*c6ac52a4SJohn Levon {
86*c6ac52a4SJohn Levon     PCIDevice *pdev = &vdev->pdev;
87*c6ac52a4SJohn Levon     VFIOUserProxy *proxy = vdev->vbasedev.proxy;
88*c6ac52a4SJohn Levon     VFIOUserDMARW *res;
89*c6ac52a4SJohn Levon     MemTxResult r;
90*c6ac52a4SJohn Levon     size_t size;
91*c6ac52a4SJohn Levon 
92*c6ac52a4SJohn Levon     if (msg->hdr.size < sizeof(*msg)) {
93*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &msg->hdr, EINVAL);
94*c6ac52a4SJohn Levon         return;
95*c6ac52a4SJohn Levon     }
96*c6ac52a4SJohn Levon     if (msg->count > proxy->max_xfer_size) {
97*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &msg->hdr, E2BIG);
98*c6ac52a4SJohn Levon         return;
99*c6ac52a4SJohn Levon     }
100*c6ac52a4SJohn Levon 
101*c6ac52a4SJohn Levon     /* switch to our own message buffer */
102*c6ac52a4SJohn Levon     size = msg->count + sizeof(VFIOUserDMARW);
103*c6ac52a4SJohn Levon     res = g_malloc0(size);
104*c6ac52a4SJohn Levon     memcpy(res, msg, sizeof(*res));
105*c6ac52a4SJohn Levon     g_free(msg);
106*c6ac52a4SJohn Levon 
107*c6ac52a4SJohn Levon     r = pci_dma_read(pdev, res->offset, &res->data, res->count);
108*c6ac52a4SJohn Levon 
109*c6ac52a4SJohn Levon     switch (r) {
110*c6ac52a4SJohn Levon     case MEMTX_OK:
111*c6ac52a4SJohn Levon         if (res->hdr.flags & VFIO_USER_NO_REPLY) {
112*c6ac52a4SJohn Levon             g_free(res);
113*c6ac52a4SJohn Levon             return;
114*c6ac52a4SJohn Levon         }
115*c6ac52a4SJohn Levon         vfio_user_send_reply(proxy, &res->hdr, size);
116*c6ac52a4SJohn Levon         break;
117*c6ac52a4SJohn Levon     case MEMTX_ERROR:
118*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &res->hdr, EFAULT);
119*c6ac52a4SJohn Levon         break;
120*c6ac52a4SJohn Levon     case MEMTX_DECODE_ERROR:
121*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &res->hdr, ENODEV);
122*c6ac52a4SJohn Levon         break;
123*c6ac52a4SJohn Levon     case MEMTX_ACCESS_ERROR:
124*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &res->hdr, EPERM);
125*c6ac52a4SJohn Levon         break;
126*c6ac52a4SJohn Levon     default:
127*c6ac52a4SJohn Levon         error_printf("vfio_user_dma_read unknown error %d\n", r);
128*c6ac52a4SJohn Levon         vfio_user_send_error(vdev->vbasedev.proxy, &res->hdr, EINVAL);
129*c6ac52a4SJohn Levon     }
130*c6ac52a4SJohn Levon }
131*c6ac52a4SJohn Levon 
132*c6ac52a4SJohn Levon static void vfio_user_dma_write(VFIOPCIDevice *vdev, VFIOUserDMARW *msg)
133*c6ac52a4SJohn Levon {
134*c6ac52a4SJohn Levon     PCIDevice *pdev = &vdev->pdev;
135*c6ac52a4SJohn Levon     VFIOUserProxy *proxy = vdev->vbasedev.proxy;
136*c6ac52a4SJohn Levon     MemTxResult r;
137*c6ac52a4SJohn Levon 
138*c6ac52a4SJohn Levon     if (msg->hdr.size < sizeof(*msg)) {
139*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &msg->hdr, EINVAL);
140*c6ac52a4SJohn Levon         return;
141*c6ac52a4SJohn Levon     }
142*c6ac52a4SJohn Levon     /* make sure transfer count isn't larger than the message data */
143*c6ac52a4SJohn Levon     if (msg->count > msg->hdr.size - sizeof(*msg)) {
144*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &msg->hdr, E2BIG);
145*c6ac52a4SJohn Levon         return;
146*c6ac52a4SJohn Levon     }
147*c6ac52a4SJohn Levon 
148*c6ac52a4SJohn Levon     r = pci_dma_write(pdev, msg->offset, &msg->data, msg->count);
149*c6ac52a4SJohn Levon 
150*c6ac52a4SJohn Levon     switch (r) {
151*c6ac52a4SJohn Levon     case MEMTX_OK:
152*c6ac52a4SJohn Levon         if ((msg->hdr.flags & VFIO_USER_NO_REPLY) == 0) {
153*c6ac52a4SJohn Levon             vfio_user_send_reply(proxy, &msg->hdr, sizeof(msg->hdr));
154*c6ac52a4SJohn Levon         } else {
155*c6ac52a4SJohn Levon             g_free(msg);
156*c6ac52a4SJohn Levon         }
157*c6ac52a4SJohn Levon         break;
158*c6ac52a4SJohn Levon     case MEMTX_ERROR:
159*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &msg->hdr, EFAULT);
160*c6ac52a4SJohn Levon         break;
161*c6ac52a4SJohn Levon     case MEMTX_DECODE_ERROR:
162*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &msg->hdr, ENODEV);
163*c6ac52a4SJohn Levon         break;
164*c6ac52a4SJohn Levon     case MEMTX_ACCESS_ERROR:
165*c6ac52a4SJohn Levon         vfio_user_send_error(proxy, &msg->hdr, EPERM);
166*c6ac52a4SJohn Levon         break;
167*c6ac52a4SJohn Levon     default:
168*c6ac52a4SJohn Levon         error_printf("vfio_user_dma_write unknown error %d\n", r);
169*c6ac52a4SJohn Levon         vfio_user_send_error(vdev->vbasedev.proxy, &msg->hdr, EINVAL);
170*c6ac52a4SJohn Levon     }
171*c6ac52a4SJohn Levon }
172*c6ac52a4SJohn Levon 
173777e45c7SJohn Levon /*
1740b3d881aSJohn Levon  * Incoming request message callback.
1750b3d881aSJohn Levon  *
1760b3d881aSJohn Levon  * Runs off main loop, so BQL held.
1770b3d881aSJohn Levon  */
1780b3d881aSJohn Levon static void vfio_user_pci_process_req(void *opaque, VFIOUserMsg *msg)
1790b3d881aSJohn Levon {
180*c6ac52a4SJohn Levon     VFIOPCIDevice *vdev = opaque;
181*c6ac52a4SJohn Levon     VFIOUserHdr *hdr = msg->hdr;
1820b3d881aSJohn Levon 
183*c6ac52a4SJohn Levon     /* no incoming PCI requests pass FDs */
184*c6ac52a4SJohn Levon     if (msg->fds != NULL) {
185*c6ac52a4SJohn Levon         vfio_user_send_error(vdev->vbasedev.proxy, hdr, EINVAL);
186*c6ac52a4SJohn Levon         vfio_user_putfds(msg);
187*c6ac52a4SJohn Levon         return;
188*c6ac52a4SJohn Levon     }
189*c6ac52a4SJohn Levon 
190*c6ac52a4SJohn Levon     switch (hdr->command) {
191*c6ac52a4SJohn Levon     case VFIO_USER_DMA_READ:
192*c6ac52a4SJohn Levon         vfio_user_dma_read(vdev, (VFIOUserDMARW *)hdr);
193*c6ac52a4SJohn Levon         break;
194*c6ac52a4SJohn Levon     case VFIO_USER_DMA_WRITE:
195*c6ac52a4SJohn Levon         vfio_user_dma_write(vdev, (VFIOUserDMARW *)hdr);
196*c6ac52a4SJohn Levon         break;
197*c6ac52a4SJohn Levon     default:
198*c6ac52a4SJohn Levon         error_printf("vfio_user_pci_process_req unknown cmd %d\n",
199*c6ac52a4SJohn Levon                      hdr->command);
200*c6ac52a4SJohn Levon         vfio_user_send_error(vdev->vbasedev.proxy, hdr, ENOSYS);
201*c6ac52a4SJohn Levon     }
2020b3d881aSJohn Levon }
2030b3d881aSJohn Levon 
2040b3d881aSJohn Levon /*
2059fca2b7dSJohn Levon  * Emulated devices don't use host hot reset
2069fca2b7dSJohn Levon  */
2079fca2b7dSJohn Levon static void vfio_user_compute_needs_reset(VFIODevice *vbasedev)
2089fca2b7dSJohn Levon {
2099fca2b7dSJohn Levon     vbasedev->needs_reset = false;
2109fca2b7dSJohn Levon }
2119fca2b7dSJohn Levon 
2129fca2b7dSJohn Levon static Object *vfio_user_pci_get_object(VFIODevice *vbasedev)
2139fca2b7dSJohn Levon {
2149fca2b7dSJohn Levon     VFIOUserPCIDevice *vdev = container_of(vbasedev, VFIOUserPCIDevice,
2159fca2b7dSJohn Levon                                            device.vbasedev);
2169fca2b7dSJohn Levon 
2179fca2b7dSJohn Levon     return OBJECT(vdev);
2189fca2b7dSJohn Levon }
2199fca2b7dSJohn Levon 
2209fca2b7dSJohn Levon static VFIODeviceOps vfio_user_pci_ops = {
2219fca2b7dSJohn Levon     .vfio_compute_needs_reset = vfio_user_compute_needs_reset,
2229fca2b7dSJohn Levon     .vfio_eoi = vfio_pci_intx_eoi,
2239fca2b7dSJohn Levon     .vfio_get_object = vfio_user_pci_get_object,
2249fca2b7dSJohn Levon     /* No live migration support yet. */
2259fca2b7dSJohn Levon     .vfio_save_config = NULL,
2269fca2b7dSJohn Levon     .vfio_load_config = NULL,
2279fca2b7dSJohn Levon };
2289fca2b7dSJohn Levon 
2299fca2b7dSJohn Levon static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)
2309fca2b7dSJohn Levon {
2319fca2b7dSJohn Levon     ERRP_GUARD();
2329fca2b7dSJohn Levon     VFIOUserPCIDevice *udev = VFIO_USER_PCI(pdev);
2339fca2b7dSJohn Levon     VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev);
2349fca2b7dSJohn Levon     VFIODevice *vbasedev = &vdev->vbasedev;
2359fca2b7dSJohn Levon     const char *sock_name;
2369fca2b7dSJohn Levon     AddressSpace *as;
237438d863fSJohn Levon     SocketAddress addr;
238438d863fSJohn Levon     VFIOUserProxy *proxy;
2399fca2b7dSJohn Levon 
2409fca2b7dSJohn Levon     if (!udev->socket) {
2419fca2b7dSJohn Levon         error_setg(errp, "No socket specified");
2429fca2b7dSJohn Levon         error_append_hint(errp, "e.g. -device '{"
2439fca2b7dSJohn Levon             "\"driver\":\"vfio-user-pci\", "
2449fca2b7dSJohn Levon             "\"socket\": {\"path\": \"/tmp/vfio-user.sock\", "
2459fca2b7dSJohn Levon             "\"type\": \"unix\"}'"
2469fca2b7dSJohn Levon             "}'\n");
2479fca2b7dSJohn Levon         return;
2489fca2b7dSJohn Levon     }
2499fca2b7dSJohn Levon 
2509fca2b7dSJohn Levon     sock_name = udev->socket->u.q_unix.path;
2519fca2b7dSJohn Levon 
2529fca2b7dSJohn Levon     vbasedev->name = g_strdup_printf("vfio-user:%s", sock_name);
2539fca2b7dSJohn Levon 
254438d863fSJohn Levon     memset(&addr, 0, sizeof(addr));
255438d863fSJohn Levon     addr.type = SOCKET_ADDRESS_TYPE_UNIX;
256438d863fSJohn Levon     addr.u.q_unix.path = (char *)sock_name;
257438d863fSJohn Levon     proxy = vfio_user_connect_dev(&addr, errp);
258438d863fSJohn Levon     if (!proxy) {
259438d863fSJohn Levon         return;
260438d863fSJohn Levon     }
261438d863fSJohn Levon     vbasedev->proxy = proxy;
2620b3d881aSJohn Levon     vfio_user_set_handler(vbasedev, vfio_user_pci_process_req, vdev);
263438d863fSJohn Levon 
26436227628SJohn Levon     vbasedev->name = g_strdup_printf("vfio-user:%s", sock_name);
26536227628SJohn Levon 
26636227628SJohn Levon     if (udev->send_queued) {
26736227628SJohn Levon         proxy->flags |= VFIO_PROXY_FORCE_QUEUED;
26836227628SJohn Levon     }
26936227628SJohn Levon 
27036227628SJohn Levon     if (!vfio_user_validate_version(proxy, errp)) {
27136227628SJohn Levon         goto error;
27236227628SJohn Levon     }
27336227628SJohn Levon 
2749fca2b7dSJohn Levon     /*
275667866d6SJohn Levon      * Use socket-based device I/O instead of vfio kernel driver.
276667866d6SJohn Levon      */
277667866d6SJohn Levon     vbasedev->io_ops = &vfio_user_device_io_ops_sock;
278667866d6SJohn Levon 
279667866d6SJohn Levon     /*
2809fca2b7dSJohn Levon      * vfio-user devices are effectively mdevs (don't use a host iommu).
2819fca2b7dSJohn Levon      */
2829fca2b7dSJohn Levon     vbasedev->mdev = true;
2839fca2b7dSJohn Levon 
284667866d6SJohn Levon     /*
285667866d6SJohn Levon      * Enable per-region fds.
286667866d6SJohn Levon      */
287667866d6SJohn Levon     vbasedev->use_region_fds = true;
288667866d6SJohn Levon 
2899fca2b7dSJohn Levon     as = pci_device_iommu_address_space(pdev);
2909fca2b7dSJohn Levon     if (!vfio_device_attach_by_iommu_type(TYPE_VFIO_IOMMU_USER,
2919fca2b7dSJohn Levon                                           vbasedev->name, vbasedev,
2929fca2b7dSJohn Levon                                           as, errp)) {
29336227628SJohn Levon         goto error;
2949fca2b7dSJohn Levon     }
29536227628SJohn Levon 
296692e0ec5SJohn Levon     if (!vfio_pci_populate_device(vdev, errp)) {
297692e0ec5SJohn Levon         goto error;
298692e0ec5SJohn Levon     }
299692e0ec5SJohn Levon 
300692e0ec5SJohn Levon     if (!vfio_pci_config_setup(vdev, errp)) {
301692e0ec5SJohn Levon         goto error;
302692e0ec5SJohn Levon     }
303692e0ec5SJohn Levon 
304692e0ec5SJohn Levon     /*
305692e0ec5SJohn Levon      * vfio_pci_config_setup will have registered the device's BARs
306692e0ec5SJohn Levon      * and setup any MSIX BARs, so errors after it succeeds must
307692e0ec5SJohn Levon      * use out_teardown
308692e0ec5SJohn Levon      */
309692e0ec5SJohn Levon 
310692e0ec5SJohn Levon     if (!vfio_pci_add_capabilities(vdev, errp)) {
311692e0ec5SJohn Levon         goto out_teardown;
312692e0ec5SJohn Levon     }
313692e0ec5SJohn Levon 
314777e45c7SJohn Levon     if (vdev->msix != NULL) {
315777e45c7SJohn Levon         vfio_user_msix_setup(vdev);
316777e45c7SJohn Levon     }
317777e45c7SJohn Levon 
318692e0ec5SJohn Levon     if (!vfio_pci_interrupt_setup(vdev, errp)) {
319692e0ec5SJohn Levon         goto out_teardown;
320692e0ec5SJohn Levon     }
321692e0ec5SJohn Levon 
322692e0ec5SJohn Levon     vfio_pci_register_err_notifier(vdev);
323692e0ec5SJohn Levon     vfio_pci_register_req_notifier(vdev);
324692e0ec5SJohn Levon 
32536227628SJohn Levon     return;
32636227628SJohn Levon 
327692e0ec5SJohn Levon out_teardown:
328692e0ec5SJohn Levon     vfio_pci_teardown_msi(vdev);
329692e0ec5SJohn Levon     vfio_pci_bars_exit(vdev);
33036227628SJohn Levon error:
33136227628SJohn Levon     error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
332692e0ec5SJohn Levon     vfio_pci_put_device(vdev);
3339fca2b7dSJohn Levon }
3349fca2b7dSJohn Levon 
3359fca2b7dSJohn Levon static void vfio_user_instance_init(Object *obj)
3369fca2b7dSJohn Levon {
3379fca2b7dSJohn Levon     PCIDevice *pci_dev = PCI_DEVICE(obj);
3389fca2b7dSJohn Levon     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
3399fca2b7dSJohn Levon     VFIODevice *vbasedev = &vdev->vbasedev;
3409fca2b7dSJohn Levon 
3419fca2b7dSJohn Levon     device_add_bootindex_property(obj, &vdev->bootindex,
3429fca2b7dSJohn Levon                                   "bootindex", NULL,
3439fca2b7dSJohn Levon                                   &pci_dev->qdev);
3449fca2b7dSJohn Levon     vdev->host.domain = ~0U;
3459fca2b7dSJohn Levon     vdev->host.bus = ~0U;
3469fca2b7dSJohn Levon     vdev->host.slot = ~0U;
3479fca2b7dSJohn Levon     vdev->host.function = ~0U;
3489fca2b7dSJohn Levon 
3499fca2b7dSJohn Levon     vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PCI, &vfio_user_pci_ops,
3509fca2b7dSJohn Levon                      DEVICE(vdev), false);
3519fca2b7dSJohn Levon 
3529fca2b7dSJohn Levon     vdev->nv_gpudirect_clique = 0xFF;
3539fca2b7dSJohn Levon 
3549fca2b7dSJohn Levon     /*
3559fca2b7dSJohn Levon      * QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
3569fca2b7dSJohn Levon      * line, therefore, no need to wait to realize like other devices.
3579fca2b7dSJohn Levon      */
3589fca2b7dSJohn Levon     pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
3599fca2b7dSJohn Levon }
3609fca2b7dSJohn Levon 
3619fca2b7dSJohn Levon static void vfio_user_instance_finalize(Object *obj)
3629fca2b7dSJohn Levon {
3639fca2b7dSJohn Levon     VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj);
364438d863fSJohn Levon     VFIODevice *vbasedev = &vdev->vbasedev;
3659fca2b7dSJohn Levon 
366777e45c7SJohn Levon     if (vdev->msix != NULL) {
367777e45c7SJohn Levon         vfio_user_msix_teardown(vdev);
368777e45c7SJohn Levon     }
369777e45c7SJohn Levon 
3709fca2b7dSJohn Levon     vfio_pci_put_device(vdev);
371438d863fSJohn Levon 
372438d863fSJohn Levon     if (vbasedev->proxy != NULL) {
373438d863fSJohn Levon         vfio_user_disconnect(vbasedev->proxy);
374438d863fSJohn Levon     }
3759fca2b7dSJohn Levon }
3769fca2b7dSJohn Levon 
37701923235SJohn Levon static void vfio_user_pci_reset(DeviceState *dev)
37801923235SJohn Levon {
37901923235SJohn Levon     VFIOPCIDevice *vdev = VFIO_PCI_BASE(dev);
38001923235SJohn Levon     VFIODevice *vbasedev = &vdev->vbasedev;
38101923235SJohn Levon 
38201923235SJohn Levon     vfio_pci_pre_reset(vdev);
38301923235SJohn Levon 
38401923235SJohn Levon     if (vbasedev->reset_works) {
38501923235SJohn Levon         vfio_user_device_reset(vbasedev->proxy);
38601923235SJohn Levon     }
38701923235SJohn Levon 
38801923235SJohn Levon     vfio_pci_post_reset(vdev);
38901923235SJohn Levon }
39001923235SJohn Levon 
3919fca2b7dSJohn Levon static const Property vfio_user_pci_dev_properties[] = {
3929fca2b7dSJohn Levon     DEFINE_PROP_UINT32("x-pci-vendor-id", VFIOPCIDevice,
3939fca2b7dSJohn Levon                        vendor_id, PCI_ANY_ID),
3949fca2b7dSJohn Levon     DEFINE_PROP_UINT32("x-pci-device-id", VFIOPCIDevice,
3959fca2b7dSJohn Levon                        device_id, PCI_ANY_ID),
3969fca2b7dSJohn Levon     DEFINE_PROP_UINT32("x-pci-sub-vendor-id", VFIOPCIDevice,
3979fca2b7dSJohn Levon                        sub_vendor_id, PCI_ANY_ID),
3989fca2b7dSJohn Levon     DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice,
3999fca2b7dSJohn Levon                        sub_device_id, PCI_ANY_ID),
40036227628SJohn Levon     DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false),
4019fca2b7dSJohn Levon };
4029fca2b7dSJohn Levon 
4039fca2b7dSJohn Levon static void vfio_user_pci_set_socket(Object *obj, Visitor *v, const char *name,
4049fca2b7dSJohn Levon                                      void *opaque, Error **errp)
4059fca2b7dSJohn Levon {
4069fca2b7dSJohn Levon     VFIOUserPCIDevice *udev = VFIO_USER_PCI(obj);
4079fca2b7dSJohn Levon     bool success;
4089fca2b7dSJohn Levon 
409438d863fSJohn Levon     if (udev->device.vbasedev.proxy) {
410438d863fSJohn Levon         error_setg(errp, "Proxy is connected");
411438d863fSJohn Levon         return;
412438d863fSJohn Levon     }
413438d863fSJohn Levon 
4149fca2b7dSJohn Levon     qapi_free_SocketAddress(udev->socket);
4159fca2b7dSJohn Levon 
4169fca2b7dSJohn Levon     udev->socket = NULL;
4179fca2b7dSJohn Levon 
4189fca2b7dSJohn Levon     success = visit_type_SocketAddress(v, name, &udev->socket, errp);
4199fca2b7dSJohn Levon 
4209fca2b7dSJohn Levon     if (!success) {
4219fca2b7dSJohn Levon         return;
4229fca2b7dSJohn Levon     }
4239fca2b7dSJohn Levon 
4249fca2b7dSJohn Levon     if (udev->socket->type != SOCKET_ADDRESS_TYPE_UNIX) {
4259fca2b7dSJohn Levon         error_setg(errp, "Unsupported socket type %s",
4269fca2b7dSJohn Levon                    SocketAddressType_str(udev->socket->type));
4279fca2b7dSJohn Levon         qapi_free_SocketAddress(udev->socket);
4289fca2b7dSJohn Levon         udev->socket = NULL;
4299fca2b7dSJohn Levon         return;
4309fca2b7dSJohn Levon     }
4319fca2b7dSJohn Levon }
4329fca2b7dSJohn Levon 
4339fca2b7dSJohn Levon static void vfio_user_pci_dev_class_init(ObjectClass *klass, const void *data)
4349fca2b7dSJohn Levon {
4359fca2b7dSJohn Levon     DeviceClass *dc = DEVICE_CLASS(klass);
4369fca2b7dSJohn Levon     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass);
4379fca2b7dSJohn Levon 
43801923235SJohn Levon     device_class_set_legacy_reset(dc, vfio_user_pci_reset);
4399fca2b7dSJohn Levon     device_class_set_props(dc, vfio_user_pci_dev_properties);
4409fca2b7dSJohn Levon 
4419fca2b7dSJohn Levon     object_class_property_add(klass, "socket", "SocketAddress", NULL,
4429fca2b7dSJohn Levon                               vfio_user_pci_set_socket, NULL, NULL);
4439fca2b7dSJohn Levon     object_class_property_set_description(klass, "socket",
4449fca2b7dSJohn Levon                                           "SocketAddress (UNIX sockets only)");
4459fca2b7dSJohn Levon 
4469fca2b7dSJohn Levon     dc->desc = "VFIO over socket PCI device assignment";
4479fca2b7dSJohn Levon     pdc->realize = vfio_user_pci_realize;
4489fca2b7dSJohn Levon }
4499fca2b7dSJohn Levon 
4509fca2b7dSJohn Levon static const TypeInfo vfio_user_pci_dev_info = {
4519fca2b7dSJohn Levon     .name = TYPE_VFIO_USER_PCI,
4529fca2b7dSJohn Levon     .parent = TYPE_VFIO_PCI_BASE,
4539fca2b7dSJohn Levon     .instance_size = sizeof(VFIOUserPCIDevice),
4549fca2b7dSJohn Levon     .class_init = vfio_user_pci_dev_class_init,
4559fca2b7dSJohn Levon     .instance_init = vfio_user_instance_init,
4569fca2b7dSJohn Levon     .instance_finalize = vfio_user_instance_finalize,
4579fca2b7dSJohn Levon };
4589fca2b7dSJohn Levon 
4599fca2b7dSJohn Levon static void register_vfio_user_dev_type(void)
4609fca2b7dSJohn Levon {
4619fca2b7dSJohn Levon     type_register_static(&vfio_user_pci_dev_info);
4629fca2b7dSJohn Levon }
4639fca2b7dSJohn Levon 
4649fca2b7dSJohn Levon  type_init(register_vfio_user_dev_type)
465