1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef VIRTIO_VFIO_COMMON_H
4 #define VIRTIO_VFIO_COMMON_H
6 #include <linux/kernel.h>
7 #include <linux/virtio.h>
8 #include <linux/vfio_pci_core.h>
9 #include <linux/virtio_pci.h>
11 enum virtiovf_migf_state {
12 VIRTIOVF_MIGF_STATE_ERROR = 1,
13 VIRTIOVF_MIGF_STATE_PRECOPY = 2,
14 VIRTIOVF_MIGF_STATE_COMPLETE = 3,
17 enum virtiovf_load_state {
18 VIRTIOVF_LOAD_STATE_READ_HEADER,
19 VIRTIOVF_LOAD_STATE_PREP_HEADER_DATA,
20 VIRTIOVF_LOAD_STATE_READ_HEADER_DATA,
21 VIRTIOVF_LOAD_STATE_PREP_CHUNK,
22 VIRTIOVF_LOAD_STATE_READ_CHUNK,
23 VIRTIOVF_LOAD_STATE_LOAD_CHUNK,
26 struct virtiovf_data_buffer {
27 struct sg_append_table table;
31 struct list_head buf_elm;
32 u8 include_header_object:1;
33 struct virtiovf_migration_file *migf;
34 /* Optimize virtiovf_get_migration_page() for sequential access */
35 struct scatterlist *last_offset_sg;
36 unsigned int sg_last_entry;
37 unsigned long last_offset;
40 enum virtiovf_migf_header_flags {
41 VIRTIOVF_MIGF_HEADER_FLAGS_TAG_MANDATORY = 0,
42 VIRTIOVF_MIGF_HEADER_FLAGS_TAG_OPTIONAL = 1 << 0,
45 enum virtiovf_migf_header_tag {
46 VIRTIOVF_MIGF_HEADER_TAG_DEVICE_DATA = 0,
49 struct virtiovf_migration_header {
51 /* For future use in case we may need to change the kernel protocol */
52 __le32 flags; /* Use virtiovf_migf_header_flags */
53 __le32 tag; /* Use virtiovf_migf_header_tag */
54 __u8 data[]; /* Its size is given in the record_size */
57 struct virtiovf_migration_file {
59 /* synchronize access to the file state */
62 u64 pre_copy_initial_bytes;
63 struct ratelimit_state pre_copy_rl_state;
68 enum virtiovf_migf_state state;
69 enum virtiovf_load_state load_state;
70 /* synchronize access to the lists */
72 struct list_head buf_list;
73 struct list_head avail_list;
74 struct virtiovf_data_buffer *buf;
75 struct virtiovf_data_buffer *buf_header;
76 struct virtiovf_pci_core_device *virtvdev;
79 struct virtiovf_pci_core_device {
80 struct vfio_pci_core_device core_device;
81 #ifdef CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY
83 /* synchronize access to the virtual buf */
84 struct mutex bar_mutex;
85 void __iomem *notify_addr;
87 __le32 pci_base_addr_0;
89 u8 bar0_virtual_buf_size;
96 /* protect migration state */
97 struct mutex state_mutex;
98 enum vfio_device_mig_state mig_state;
99 /* protect the reset_done flow */
100 spinlock_t reset_lock;
101 struct virtiovf_migration_file *resuming_migf;
102 struct virtiovf_migration_file *saving_migf;
105 void virtiovf_set_migratable(struct virtiovf_pci_core_device *virtvdev);
106 void virtiovf_open_migration(struct virtiovf_pci_core_device *virtvdev);
107 void virtiovf_close_migration(struct virtiovf_pci_core_device *virtvdev);
108 void virtiovf_migration_reset_done(struct pci_dev *pdev);
110 #ifdef CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY
111 int virtiovf_open_legacy_io(struct virtiovf_pci_core_device *virtvdev);
112 long virtiovf_vfio_pci_core_ioctl(struct vfio_device *core_vdev,
113 unsigned int cmd, unsigned long arg);
114 int virtiovf_pci_ioctl_get_region_info(struct vfio_device *core_vdev,
115 unsigned int cmd, unsigned long arg);
116 ssize_t virtiovf_pci_core_write(struct vfio_device *core_vdev,
117 const char __user *buf, size_t count,
119 ssize_t virtiovf_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
120 size_t count, loff_t *ppos);
121 bool virtiovf_support_legacy_io(struct pci_dev *pdev);
122 int virtiovf_init_legacy_io(struct virtiovf_pci_core_device *virtvdev);
123 void virtiovf_release_legacy_io(struct virtiovf_pci_core_device *virtvdev);
124 void virtiovf_legacy_io_reset_done(struct pci_dev *pdev);
127 #endif /* VIRTIO_VFIO_COMMON_H */