xref: /linux/drivers/vfio/pci/virtio/common.h (revision 2eff01ee2881becc9daaa0d53477ec202136b1f4)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef VIRTIO_VFIO_COMMON_H
4 #define VIRTIO_VFIO_COMMON_H
5 
6 #include <linux/kernel.h>
7 #include <linux/virtio.h>
8 #include <linux/vfio_pci_core.h>
9 #include <linux/virtio_pci.h>
10 
11 enum virtiovf_migf_state {
12 	VIRTIOVF_MIGF_STATE_ERROR = 1,
13 	VIRTIOVF_MIGF_STATE_PRECOPY = 2,
14 	VIRTIOVF_MIGF_STATE_COMPLETE = 3,
15 };
16 
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,
24 };
25 
26 struct virtiovf_data_buffer {
27 	struct sg_append_table table;
28 	loff_t start_pos;
29 	u64 length;
30 	u64 allocated_length;
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;
38 };
39 
40 enum virtiovf_migf_header_flags {
41 	VIRTIOVF_MIGF_HEADER_FLAGS_TAG_MANDATORY = 0,
42 	VIRTIOVF_MIGF_HEADER_FLAGS_TAG_OPTIONAL = 1 << 0,
43 };
44 
45 enum virtiovf_migf_header_tag {
46 	VIRTIOVF_MIGF_HEADER_TAG_DEVICE_DATA = 0,
47 };
48 
49 struct virtiovf_migration_header {
50 	__le64 record_size;
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 */
55 };
56 
57 struct virtiovf_migration_file {
58 	struct file *filp;
59 	/* synchronize access to the file state */
60 	struct mutex lock;
61 	loff_t max_pos;
62 	u64 pre_copy_initial_bytes;
63 	struct ratelimit_state pre_copy_rl_state;
64 	u64 record_size;
65 	u32 record_tag;
66 	u8 has_obj_id:1;
67 	u32 obj_id;
68 	enum virtiovf_migf_state state;
69 	enum virtiovf_load_state load_state;
70 	/* synchronize access to the lists */
71 	spinlock_t list_lock;
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;
77 };
78 
79 struct virtiovf_pci_core_device {
80 	struct vfio_pci_core_device core_device;
81 #ifdef CONFIG_VIRTIO_VFIO_PCI_ADMIN_LEGACY
82 	u8 *bar0_virtual_buf;
83 	/* synchronize access to the virtual buf */
84 	struct mutex bar_mutex;
85 	void __iomem *notify_addr;
86 	u64 notify_offset;
87 	__le32 pci_base_addr_0;
88 	__le16 pci_cmd;
89 	u8 bar0_virtual_buf_size;
90 	u8 notify_bar;
91 #endif
92 
93 	/* LM related */
94 	u8 migrate_cap:1;
95 	u8 deferred_reset:1;
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;
103 };
104 
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);
109 
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,
118 				loff_t *ppos);
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);
125 #endif
126 
127 #endif /* VIRTIO_VFIO_COMMON_H */
128