1bb500dbeSBrett Creeley // SPDX-License-Identifier: GPL-2.0
2bb500dbeSBrett Creeley /* Copyright(c) 2023 Advanced Micro Devices, Inc. */
3bb500dbeSBrett Creeley
4bb500dbeSBrett Creeley #include <linux/anon_inodes.h>
5bb500dbeSBrett Creeley #include <linux/file.h>
6bb500dbeSBrett Creeley #include <linux/fs.h>
7bb500dbeSBrett Creeley #include <linux/highmem.h>
8bb500dbeSBrett Creeley #include <linux/vfio.h>
9bb500dbeSBrett Creeley #include <linux/vfio_pci_core.h>
10bb500dbeSBrett Creeley
11bb500dbeSBrett Creeley #include "vfio_dev.h"
12bb500dbeSBrett Creeley #include "cmds.h"
13bb500dbeSBrett Creeley
14bb500dbeSBrett Creeley static struct pds_vfio_lm_file *
pds_vfio_get_lm_file(const struct file_operations * fops,int flags,u64 size)15bb500dbeSBrett Creeley pds_vfio_get_lm_file(const struct file_operations *fops, int flags, u64 size)
16bb500dbeSBrett Creeley {
17bb500dbeSBrett Creeley struct pds_vfio_lm_file *lm_file = NULL;
18bb500dbeSBrett Creeley unsigned long long npages;
19bb500dbeSBrett Creeley struct page **pages;
20bb500dbeSBrett Creeley void *page_mem;
21bb500dbeSBrett Creeley const void *p;
22bb500dbeSBrett Creeley
23bb500dbeSBrett Creeley if (!size)
24bb500dbeSBrett Creeley return NULL;
25bb500dbeSBrett Creeley
26bb500dbeSBrett Creeley /* Alloc file structure */
27bb500dbeSBrett Creeley lm_file = kzalloc(sizeof(*lm_file), GFP_KERNEL);
28bb500dbeSBrett Creeley if (!lm_file)
29bb500dbeSBrett Creeley return NULL;
30bb500dbeSBrett Creeley
31bb500dbeSBrett Creeley /* Create file */
32bb500dbeSBrett Creeley lm_file->filep =
33bb500dbeSBrett Creeley anon_inode_getfile("pds_vfio_lm", fops, lm_file, flags);
342d12d18fSYang Yingliang if (IS_ERR(lm_file->filep))
35bb500dbeSBrett Creeley goto out_free_file;
36bb500dbeSBrett Creeley
37bb500dbeSBrett Creeley stream_open(lm_file->filep->f_inode, lm_file->filep);
38bb500dbeSBrett Creeley mutex_init(&lm_file->lock);
39bb500dbeSBrett Creeley
40bb500dbeSBrett Creeley /* prevent file from being released before we are done with it */
41bb500dbeSBrett Creeley get_file(lm_file->filep);
42bb500dbeSBrett Creeley
43bb500dbeSBrett Creeley /* Allocate memory for file pages */
44bb500dbeSBrett Creeley npages = DIV_ROUND_UP_ULL(size, PAGE_SIZE);
45bb500dbeSBrett Creeley pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
46bb500dbeSBrett Creeley if (!pages)
47bb500dbeSBrett Creeley goto out_put_file;
48bb500dbeSBrett Creeley
49bb500dbeSBrett Creeley page_mem = kvzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL);
50bb500dbeSBrett Creeley if (!page_mem)
51bb500dbeSBrett Creeley goto out_free_pages_array;
52bb500dbeSBrett Creeley
53bb500dbeSBrett Creeley p = page_mem - offset_in_page(page_mem);
54bb500dbeSBrett Creeley for (unsigned long long i = 0; i < npages; i++) {
55bb500dbeSBrett Creeley if (is_vmalloc_addr(p))
56bb500dbeSBrett Creeley pages[i] = vmalloc_to_page(p);
57bb500dbeSBrett Creeley else
58bb500dbeSBrett Creeley pages[i] = kmap_to_page((void *)p);
59bb500dbeSBrett Creeley if (!pages[i])
60bb500dbeSBrett Creeley goto out_free_page_mem;
61bb500dbeSBrett Creeley
62bb500dbeSBrett Creeley p += PAGE_SIZE;
63bb500dbeSBrett Creeley }
64bb500dbeSBrett Creeley
65bb500dbeSBrett Creeley /* Create scatterlist of file pages to use for DMA mapping later */
66bb500dbeSBrett Creeley if (sg_alloc_table_from_pages(&lm_file->sg_table, pages, npages, 0,
67bb500dbeSBrett Creeley size, GFP_KERNEL))
68bb500dbeSBrett Creeley goto out_free_page_mem;
69bb500dbeSBrett Creeley
70bb500dbeSBrett Creeley lm_file->size = size;
71bb500dbeSBrett Creeley lm_file->pages = pages;
72bb500dbeSBrett Creeley lm_file->npages = npages;
73bb500dbeSBrett Creeley lm_file->page_mem = page_mem;
74bb500dbeSBrett Creeley lm_file->alloc_size = npages * PAGE_SIZE;
75bb500dbeSBrett Creeley
76bb500dbeSBrett Creeley return lm_file;
77bb500dbeSBrett Creeley
78bb500dbeSBrett Creeley out_free_page_mem:
79bb500dbeSBrett Creeley kvfree(page_mem);
80bb500dbeSBrett Creeley out_free_pages_array:
81bb500dbeSBrett Creeley kfree(pages);
82bb500dbeSBrett Creeley out_put_file:
83bb500dbeSBrett Creeley fput(lm_file->filep);
84bb500dbeSBrett Creeley mutex_destroy(&lm_file->lock);
85bb500dbeSBrett Creeley out_free_file:
86bb500dbeSBrett Creeley kfree(lm_file);
87bb500dbeSBrett Creeley
88bb500dbeSBrett Creeley return NULL;
89bb500dbeSBrett Creeley }
90bb500dbeSBrett Creeley
pds_vfio_put_lm_file(struct pds_vfio_lm_file * lm_file)91bb500dbeSBrett Creeley static void pds_vfio_put_lm_file(struct pds_vfio_lm_file *lm_file)
92bb500dbeSBrett Creeley {
93bb500dbeSBrett Creeley mutex_lock(&lm_file->lock);
94bb500dbeSBrett Creeley
95*66aa5d95SBrett Creeley lm_file->disabled = true;
96bb500dbeSBrett Creeley lm_file->size = 0;
97bb500dbeSBrett Creeley lm_file->alloc_size = 0;
98*66aa5d95SBrett Creeley lm_file->filep->f_pos = 0;
99bb500dbeSBrett Creeley
100bb500dbeSBrett Creeley /* Free scatter list of file pages */
101bb500dbeSBrett Creeley sg_free_table(&lm_file->sg_table);
102bb500dbeSBrett Creeley
103bb500dbeSBrett Creeley kvfree(lm_file->page_mem);
104bb500dbeSBrett Creeley lm_file->page_mem = NULL;
105bb500dbeSBrett Creeley kfree(lm_file->pages);
106bb500dbeSBrett Creeley lm_file->pages = NULL;
107bb500dbeSBrett Creeley
108bb500dbeSBrett Creeley mutex_unlock(&lm_file->lock);
109bb500dbeSBrett Creeley
110bb500dbeSBrett Creeley /* allow file to be released since we are done with it */
111bb500dbeSBrett Creeley fput(lm_file->filep);
112bb500dbeSBrett Creeley }
113bb500dbeSBrett Creeley
pds_vfio_put_save_file(struct pds_vfio_pci_device * pds_vfio)114bb500dbeSBrett Creeley void pds_vfio_put_save_file(struct pds_vfio_pci_device *pds_vfio)
115bb500dbeSBrett Creeley {
116bb500dbeSBrett Creeley if (!pds_vfio->save_file)
117bb500dbeSBrett Creeley return;
118bb500dbeSBrett Creeley
119bb500dbeSBrett Creeley pds_vfio_put_lm_file(pds_vfio->save_file);
120bb500dbeSBrett Creeley pds_vfio->save_file = NULL;
121bb500dbeSBrett Creeley }
122bb500dbeSBrett Creeley
pds_vfio_put_restore_file(struct pds_vfio_pci_device * pds_vfio)123bb500dbeSBrett Creeley void pds_vfio_put_restore_file(struct pds_vfio_pci_device *pds_vfio)
124bb500dbeSBrett Creeley {
125bb500dbeSBrett Creeley if (!pds_vfio->restore_file)
126bb500dbeSBrett Creeley return;
127bb500dbeSBrett Creeley
128bb500dbeSBrett Creeley pds_vfio_put_lm_file(pds_vfio->restore_file);
129bb500dbeSBrett Creeley pds_vfio->restore_file = NULL;
130bb500dbeSBrett Creeley }
131bb500dbeSBrett Creeley
pds_vfio_get_file_page(struct pds_vfio_lm_file * lm_file,unsigned long offset)132bb500dbeSBrett Creeley static struct page *pds_vfio_get_file_page(struct pds_vfio_lm_file *lm_file,
133bb500dbeSBrett Creeley unsigned long offset)
134bb500dbeSBrett Creeley {
135bb500dbeSBrett Creeley unsigned long cur_offset = 0;
136bb500dbeSBrett Creeley struct scatterlist *sg;
137bb500dbeSBrett Creeley unsigned int i;
138bb500dbeSBrett Creeley
139bb500dbeSBrett Creeley /* All accesses are sequential */
140bb500dbeSBrett Creeley if (offset < lm_file->last_offset || !lm_file->last_offset_sg) {
141bb500dbeSBrett Creeley lm_file->last_offset = 0;
142bb500dbeSBrett Creeley lm_file->last_offset_sg = lm_file->sg_table.sgl;
143bb500dbeSBrett Creeley lm_file->sg_last_entry = 0;
144bb500dbeSBrett Creeley }
145bb500dbeSBrett Creeley
146bb500dbeSBrett Creeley cur_offset = lm_file->last_offset;
147bb500dbeSBrett Creeley
148bb500dbeSBrett Creeley for_each_sg(lm_file->last_offset_sg, sg,
149bb500dbeSBrett Creeley lm_file->sg_table.orig_nents - lm_file->sg_last_entry, i) {
150bb500dbeSBrett Creeley if (offset < sg->length + cur_offset) {
151bb500dbeSBrett Creeley lm_file->last_offset_sg = sg;
152bb500dbeSBrett Creeley lm_file->sg_last_entry += i;
153bb500dbeSBrett Creeley lm_file->last_offset = cur_offset;
154bb500dbeSBrett Creeley return nth_page(sg_page(sg),
155bb500dbeSBrett Creeley (offset - cur_offset) / PAGE_SIZE);
156bb500dbeSBrett Creeley }
157bb500dbeSBrett Creeley cur_offset += sg->length;
158bb500dbeSBrett Creeley }
159bb500dbeSBrett Creeley
160bb500dbeSBrett Creeley return NULL;
161bb500dbeSBrett Creeley }
162bb500dbeSBrett Creeley
pds_vfio_release_file(struct inode * inode,struct file * filp)163bb500dbeSBrett Creeley static int pds_vfio_release_file(struct inode *inode, struct file *filp)
164bb500dbeSBrett Creeley {
165bb500dbeSBrett Creeley struct pds_vfio_lm_file *lm_file = filp->private_data;
166bb500dbeSBrett Creeley
167bb500dbeSBrett Creeley mutex_lock(&lm_file->lock);
168bb500dbeSBrett Creeley lm_file->filep->f_pos = 0;
169bb500dbeSBrett Creeley lm_file->size = 0;
170bb500dbeSBrett Creeley mutex_unlock(&lm_file->lock);
171bb500dbeSBrett Creeley mutex_destroy(&lm_file->lock);
172bb500dbeSBrett Creeley kfree(lm_file);
173bb500dbeSBrett Creeley
174bb500dbeSBrett Creeley return 0;
175bb500dbeSBrett Creeley }
176bb500dbeSBrett Creeley
pds_vfio_save_read(struct file * filp,char __user * buf,size_t len,loff_t * pos)177bb500dbeSBrett Creeley static ssize_t pds_vfio_save_read(struct file *filp, char __user *buf,
178bb500dbeSBrett Creeley size_t len, loff_t *pos)
179bb500dbeSBrett Creeley {
180bb500dbeSBrett Creeley struct pds_vfio_lm_file *lm_file = filp->private_data;
181bb500dbeSBrett Creeley ssize_t done = 0;
182bb500dbeSBrett Creeley
183bb500dbeSBrett Creeley if (pos)
184bb500dbeSBrett Creeley return -ESPIPE;
185bb500dbeSBrett Creeley pos = &filp->f_pos;
186bb500dbeSBrett Creeley
187bb500dbeSBrett Creeley mutex_lock(&lm_file->lock);
188*66aa5d95SBrett Creeley
189*66aa5d95SBrett Creeley if (lm_file->disabled) {
190*66aa5d95SBrett Creeley done = -ENODEV;
191*66aa5d95SBrett Creeley goto out_unlock;
192*66aa5d95SBrett Creeley }
193*66aa5d95SBrett Creeley
194bb500dbeSBrett Creeley if (*pos > lm_file->size) {
195bb500dbeSBrett Creeley done = -EINVAL;
196bb500dbeSBrett Creeley goto out_unlock;
197bb500dbeSBrett Creeley }
198bb500dbeSBrett Creeley
199bb500dbeSBrett Creeley len = min_t(size_t, lm_file->size - *pos, len);
200bb500dbeSBrett Creeley while (len) {
201bb500dbeSBrett Creeley size_t page_offset;
202bb500dbeSBrett Creeley struct page *page;
203bb500dbeSBrett Creeley size_t page_len;
204bb500dbeSBrett Creeley u8 *from_buff;
205bb500dbeSBrett Creeley int err;
206bb500dbeSBrett Creeley
207bb500dbeSBrett Creeley page_offset = (*pos) % PAGE_SIZE;
208bb500dbeSBrett Creeley page = pds_vfio_get_file_page(lm_file, *pos - page_offset);
209bb500dbeSBrett Creeley if (!page) {
210bb500dbeSBrett Creeley if (done == 0)
211bb500dbeSBrett Creeley done = -EINVAL;
212bb500dbeSBrett Creeley goto out_unlock;
213bb500dbeSBrett Creeley }
214bb500dbeSBrett Creeley
215bb500dbeSBrett Creeley page_len = min_t(size_t, len, PAGE_SIZE - page_offset);
216bb500dbeSBrett Creeley from_buff = kmap_local_page(page);
217bb500dbeSBrett Creeley err = copy_to_user(buf, from_buff + page_offset, page_len);
218bb500dbeSBrett Creeley kunmap_local(from_buff);
219bb500dbeSBrett Creeley if (err) {
220bb500dbeSBrett Creeley done = -EFAULT;
221bb500dbeSBrett Creeley goto out_unlock;
222bb500dbeSBrett Creeley }
223bb500dbeSBrett Creeley *pos += page_len;
224bb500dbeSBrett Creeley len -= page_len;
225bb500dbeSBrett Creeley done += page_len;
226bb500dbeSBrett Creeley buf += page_len;
227bb500dbeSBrett Creeley }
228bb500dbeSBrett Creeley
229bb500dbeSBrett Creeley out_unlock:
230bb500dbeSBrett Creeley mutex_unlock(&lm_file->lock);
231bb500dbeSBrett Creeley return done;
232bb500dbeSBrett Creeley }
233bb500dbeSBrett Creeley
234bb500dbeSBrett Creeley static const struct file_operations pds_vfio_save_fops = {
235bb500dbeSBrett Creeley .owner = THIS_MODULE,
236bb500dbeSBrett Creeley .read = pds_vfio_save_read,
237bb500dbeSBrett Creeley .release = pds_vfio_release_file,
238bb500dbeSBrett Creeley .llseek = no_llseek,
239bb500dbeSBrett Creeley };
240bb500dbeSBrett Creeley
pds_vfio_get_save_file(struct pds_vfio_pci_device * pds_vfio)241bb500dbeSBrett Creeley static int pds_vfio_get_save_file(struct pds_vfio_pci_device *pds_vfio)
242bb500dbeSBrett Creeley {
243bb500dbeSBrett Creeley struct device *dev = &pds_vfio->vfio_coredev.pdev->dev;
244bb500dbeSBrett Creeley struct pds_vfio_lm_file *lm_file;
245bb500dbeSBrett Creeley u64 size;
246bb500dbeSBrett Creeley int err;
247bb500dbeSBrett Creeley
248bb500dbeSBrett Creeley /* Get live migration state size in this state */
249bb500dbeSBrett Creeley err = pds_vfio_get_lm_state_size_cmd(pds_vfio, &size);
250bb500dbeSBrett Creeley if (err) {
251bb500dbeSBrett Creeley dev_err(dev, "failed to get save status: %pe\n", ERR_PTR(err));
252bb500dbeSBrett Creeley return err;
253bb500dbeSBrett Creeley }
254bb500dbeSBrett Creeley
255bb500dbeSBrett Creeley dev_dbg(dev, "save status, size = %lld\n", size);
256bb500dbeSBrett Creeley
257bb500dbeSBrett Creeley if (!size) {
258bb500dbeSBrett Creeley dev_err(dev, "invalid state size\n");
259bb500dbeSBrett Creeley return -EIO;
260bb500dbeSBrett Creeley }
261bb500dbeSBrett Creeley
262bb500dbeSBrett Creeley lm_file = pds_vfio_get_lm_file(&pds_vfio_save_fops, O_RDONLY, size);
263bb500dbeSBrett Creeley if (!lm_file) {
264bb500dbeSBrett Creeley dev_err(dev, "failed to create save file\n");
265bb500dbeSBrett Creeley return -ENOENT;
266bb500dbeSBrett Creeley }
267bb500dbeSBrett Creeley
268bb500dbeSBrett Creeley dev_dbg(dev, "size = %lld, alloc_size = %lld, npages = %lld\n",
269bb500dbeSBrett Creeley lm_file->size, lm_file->alloc_size, lm_file->npages);
270bb500dbeSBrett Creeley
271bb500dbeSBrett Creeley pds_vfio->save_file = lm_file;
272bb500dbeSBrett Creeley
273bb500dbeSBrett Creeley return 0;
274bb500dbeSBrett Creeley }
275bb500dbeSBrett Creeley
pds_vfio_restore_write(struct file * filp,const char __user * buf,size_t len,loff_t * pos)276bb500dbeSBrett Creeley static ssize_t pds_vfio_restore_write(struct file *filp, const char __user *buf,
277bb500dbeSBrett Creeley size_t len, loff_t *pos)
278bb500dbeSBrett Creeley {
279bb500dbeSBrett Creeley struct pds_vfio_lm_file *lm_file = filp->private_data;
280bb500dbeSBrett Creeley loff_t requested_length;
281bb500dbeSBrett Creeley ssize_t done = 0;
282bb500dbeSBrett Creeley
283bb500dbeSBrett Creeley if (pos)
284bb500dbeSBrett Creeley return -ESPIPE;
285bb500dbeSBrett Creeley
286bb500dbeSBrett Creeley pos = &filp->f_pos;
287bb500dbeSBrett Creeley
288bb500dbeSBrett Creeley if (*pos < 0 ||
289bb500dbeSBrett Creeley check_add_overflow((loff_t)len, *pos, &requested_length))
290bb500dbeSBrett Creeley return -EINVAL;
291bb500dbeSBrett Creeley
292bb500dbeSBrett Creeley mutex_lock(&lm_file->lock);
293bb500dbeSBrett Creeley
294*66aa5d95SBrett Creeley if (lm_file->disabled) {
295*66aa5d95SBrett Creeley done = -ENODEV;
296*66aa5d95SBrett Creeley goto out_unlock;
297*66aa5d95SBrett Creeley }
298*66aa5d95SBrett Creeley
299bb500dbeSBrett Creeley while (len) {
300bb500dbeSBrett Creeley size_t page_offset;
301bb500dbeSBrett Creeley struct page *page;
302bb500dbeSBrett Creeley size_t page_len;
303bb500dbeSBrett Creeley u8 *to_buff;
304bb500dbeSBrett Creeley int err;
305bb500dbeSBrett Creeley
306bb500dbeSBrett Creeley page_offset = (*pos) % PAGE_SIZE;
307bb500dbeSBrett Creeley page = pds_vfio_get_file_page(lm_file, *pos - page_offset);
308bb500dbeSBrett Creeley if (!page) {
309bb500dbeSBrett Creeley if (done == 0)
310bb500dbeSBrett Creeley done = -EINVAL;
311bb500dbeSBrett Creeley goto out_unlock;
312bb500dbeSBrett Creeley }
313bb500dbeSBrett Creeley
314bb500dbeSBrett Creeley page_len = min_t(size_t, len, PAGE_SIZE - page_offset);
315bb500dbeSBrett Creeley to_buff = kmap_local_page(page);
316bb500dbeSBrett Creeley err = copy_from_user(to_buff + page_offset, buf, page_len);
317bb500dbeSBrett Creeley kunmap_local(to_buff);
318bb500dbeSBrett Creeley if (err) {
319bb500dbeSBrett Creeley done = -EFAULT;
320bb500dbeSBrett Creeley goto out_unlock;
321bb500dbeSBrett Creeley }
322bb500dbeSBrett Creeley *pos += page_len;
323bb500dbeSBrett Creeley len -= page_len;
324bb500dbeSBrett Creeley done += page_len;
325bb500dbeSBrett Creeley buf += page_len;
326bb500dbeSBrett Creeley lm_file->size += page_len;
327bb500dbeSBrett Creeley }
328bb500dbeSBrett Creeley out_unlock:
329bb500dbeSBrett Creeley mutex_unlock(&lm_file->lock);
330bb500dbeSBrett Creeley return done;
331bb500dbeSBrett Creeley }
332bb500dbeSBrett Creeley
333bb500dbeSBrett Creeley static const struct file_operations pds_vfio_restore_fops = {
334bb500dbeSBrett Creeley .owner = THIS_MODULE,
335bb500dbeSBrett Creeley .write = pds_vfio_restore_write,
336bb500dbeSBrett Creeley .release = pds_vfio_release_file,
337bb500dbeSBrett Creeley .llseek = no_llseek,
338bb500dbeSBrett Creeley };
339bb500dbeSBrett Creeley
pds_vfio_get_restore_file(struct pds_vfio_pci_device * pds_vfio)340bb500dbeSBrett Creeley static int pds_vfio_get_restore_file(struct pds_vfio_pci_device *pds_vfio)
341bb500dbeSBrett Creeley {
342bb500dbeSBrett Creeley struct device *dev = &pds_vfio->vfio_coredev.pdev->dev;
343bb500dbeSBrett Creeley struct pds_vfio_lm_file *lm_file;
344bb500dbeSBrett Creeley u64 size;
345bb500dbeSBrett Creeley
346bb500dbeSBrett Creeley size = sizeof(union pds_lm_dev_state);
347bb500dbeSBrett Creeley dev_dbg(dev, "restore status, size = %lld\n", size);
348bb500dbeSBrett Creeley
349bb500dbeSBrett Creeley if (!size) {
350bb500dbeSBrett Creeley dev_err(dev, "invalid state size");
351bb500dbeSBrett Creeley return -EIO;
352bb500dbeSBrett Creeley }
353bb500dbeSBrett Creeley
354bb500dbeSBrett Creeley lm_file = pds_vfio_get_lm_file(&pds_vfio_restore_fops, O_WRONLY, size);
355bb500dbeSBrett Creeley if (!lm_file) {
356bb500dbeSBrett Creeley dev_err(dev, "failed to create restore file");
357bb500dbeSBrett Creeley return -ENOENT;
358bb500dbeSBrett Creeley }
359bb500dbeSBrett Creeley pds_vfio->restore_file = lm_file;
360bb500dbeSBrett Creeley
361bb500dbeSBrett Creeley return 0;
362bb500dbeSBrett Creeley }
363bb500dbeSBrett Creeley
364bb500dbeSBrett Creeley struct file *
pds_vfio_step_device_state_locked(struct pds_vfio_pci_device * pds_vfio,enum vfio_device_mig_state next)365bb500dbeSBrett Creeley pds_vfio_step_device_state_locked(struct pds_vfio_pci_device *pds_vfio,
366bb500dbeSBrett Creeley enum vfio_device_mig_state next)
367bb500dbeSBrett Creeley {
368bb500dbeSBrett Creeley enum vfio_device_mig_state cur = pds_vfio->state;
369bb500dbeSBrett Creeley int err;
370bb500dbeSBrett Creeley
371bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_STOP && next == VFIO_DEVICE_STATE_STOP_COPY) {
372bb500dbeSBrett Creeley err = pds_vfio_get_save_file(pds_vfio);
373bb500dbeSBrett Creeley if (err)
374bb500dbeSBrett Creeley return ERR_PTR(err);
375bb500dbeSBrett Creeley
376bb500dbeSBrett Creeley err = pds_vfio_get_lm_state_cmd(pds_vfio);
377bb500dbeSBrett Creeley if (err) {
378bb500dbeSBrett Creeley pds_vfio_put_save_file(pds_vfio);
379bb500dbeSBrett Creeley return ERR_PTR(err);
380bb500dbeSBrett Creeley }
381bb500dbeSBrett Creeley
382bb500dbeSBrett Creeley return pds_vfio->save_file->filep;
383bb500dbeSBrett Creeley }
384bb500dbeSBrett Creeley
385bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_STOP_COPY && next == VFIO_DEVICE_STATE_STOP) {
386bb500dbeSBrett Creeley pds_vfio_put_save_file(pds_vfio);
387f232836aSBrett Creeley pds_vfio_dirty_disable(pds_vfio, true);
388bb500dbeSBrett Creeley return NULL;
389bb500dbeSBrett Creeley }
390bb500dbeSBrett Creeley
391bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_STOP && next == VFIO_DEVICE_STATE_RESUMING) {
392bb500dbeSBrett Creeley err = pds_vfio_get_restore_file(pds_vfio);
393bb500dbeSBrett Creeley if (err)
394bb500dbeSBrett Creeley return ERR_PTR(err);
395bb500dbeSBrett Creeley
396bb500dbeSBrett Creeley return pds_vfio->restore_file->filep;
397bb500dbeSBrett Creeley }
398bb500dbeSBrett Creeley
399bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_RESUMING && next == VFIO_DEVICE_STATE_STOP) {
400bb500dbeSBrett Creeley err = pds_vfio_set_lm_state_cmd(pds_vfio);
401bb500dbeSBrett Creeley if (err)
402bb500dbeSBrett Creeley return ERR_PTR(err);
403bb500dbeSBrett Creeley
404bb500dbeSBrett Creeley pds_vfio_put_restore_file(pds_vfio);
405bb500dbeSBrett Creeley return NULL;
406bb500dbeSBrett Creeley }
407bb500dbeSBrett Creeley
408bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_RUNNING && next == VFIO_DEVICE_STATE_RUNNING_P2P) {
409bb500dbeSBrett Creeley pds_vfio_send_host_vf_lm_status_cmd(pds_vfio,
410bb500dbeSBrett Creeley PDS_LM_STA_IN_PROGRESS);
411bb500dbeSBrett Creeley err = pds_vfio_suspend_device_cmd(pds_vfio,
412bb500dbeSBrett Creeley PDS_LM_SUSPEND_RESUME_TYPE_P2P);
413bb500dbeSBrett Creeley if (err)
414bb500dbeSBrett Creeley return ERR_PTR(err);
415bb500dbeSBrett Creeley
416bb500dbeSBrett Creeley return NULL;
417bb500dbeSBrett Creeley }
418bb500dbeSBrett Creeley
419bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_RUNNING_P2P && next == VFIO_DEVICE_STATE_RUNNING) {
420bb500dbeSBrett Creeley err = pds_vfio_resume_device_cmd(pds_vfio,
421bb500dbeSBrett Creeley PDS_LM_SUSPEND_RESUME_TYPE_FULL);
422bb500dbeSBrett Creeley if (err)
423bb500dbeSBrett Creeley return ERR_PTR(err);
424bb500dbeSBrett Creeley
425bb500dbeSBrett Creeley pds_vfio_send_host_vf_lm_status_cmd(pds_vfio, PDS_LM_STA_NONE);
426bb500dbeSBrett Creeley return NULL;
427bb500dbeSBrett Creeley }
428bb500dbeSBrett Creeley
429bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_STOP && next == VFIO_DEVICE_STATE_RUNNING_P2P) {
430bb500dbeSBrett Creeley err = pds_vfio_resume_device_cmd(pds_vfio,
431bb500dbeSBrett Creeley PDS_LM_SUSPEND_RESUME_TYPE_P2P);
432bb500dbeSBrett Creeley if (err)
433bb500dbeSBrett Creeley return ERR_PTR(err);
434bb500dbeSBrett Creeley
435bb500dbeSBrett Creeley return NULL;
436bb500dbeSBrett Creeley }
437bb500dbeSBrett Creeley
438bb500dbeSBrett Creeley if (cur == VFIO_DEVICE_STATE_RUNNING_P2P && next == VFIO_DEVICE_STATE_STOP) {
439bb500dbeSBrett Creeley err = pds_vfio_suspend_device_cmd(pds_vfio,
440bb500dbeSBrett Creeley PDS_LM_SUSPEND_RESUME_TYPE_FULL);
441bb500dbeSBrett Creeley if (err)
442bb500dbeSBrett Creeley return ERR_PTR(err);
443bb500dbeSBrett Creeley return NULL;
444bb500dbeSBrett Creeley }
445bb500dbeSBrett Creeley
446bb500dbeSBrett Creeley return ERR_PTR(-EINVAL);
447bb500dbeSBrett Creeley }
448