1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved
6 #include <linux/device.h>
7 #include <linux/eventfd.h>
8 #include <linux/file.h>
9 #include <linux/interrupt.h>
10 #include <linux/iommu.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/notifier.h>
14 #include <linux/pci.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/types.h>
17 #include <linux/uaccess.h>
18 #include <linux/vfio.h>
19 #include <linux/sched/mm.h>
20 #include <linux/anon_inodes.h>
24 /* Device specification max LOAD size */
25 #define MAX_LOAD_SIZE (BIT_ULL(__mlx5_bit_sz(load_vhca_state_in, size)) - 1)
27 #define MAX_CHUNK_SIZE SZ_8M
29 static struct mlx5vf_pci_core_device *mlx5vf_drvdata(struct pci_dev *pdev)
31 struct vfio_pci_core_device *core_device = dev_get_drvdata(&pdev->dev);
33 return container_of(core_device, struct mlx5vf_pci_core_device,
38 mlx5vf_get_migration_page(struct mlx5_vhca_data_buffer *buf,
41 unsigned long cur_offset = 0;
42 struct scatterlist *sg;
45 /* All accesses are sequential */
46 if (offset < buf->last_offset || !buf->last_offset_sg) {
48 buf->last_offset_sg = buf->table.sgt.sgl;
49 buf->sg_last_entry = 0;
52 cur_offset = buf->last_offset;
54 for_each_sg(buf->last_offset_sg, sg,
55 buf->table.sgt.orig_nents - buf->sg_last_entry, i) {
56 if (offset < sg->length + cur_offset) {
57 buf->last_offset_sg = sg;
58 buf->sg_last_entry += i;
59 buf->last_offset = cur_offset;
60 return nth_page(sg_page(sg),
61 (offset - cur_offset) / PAGE_SIZE);
63 cur_offset += sg->length;
68 static void mlx5vf_disable_fd(struct mlx5_vf_migration_file *migf)
70 mutex_lock(&migf->lock);
71 migf->state = MLX5_MIGF_STATE_ERROR;
72 migf->filp->f_pos = 0;
73 mutex_unlock(&migf->lock);
76 static int mlx5vf_release_file(struct inode *inode, struct file *filp)
78 struct mlx5_vf_migration_file *migf = filp->private_data;
80 mlx5vf_disable_fd(migf);
81 mutex_destroy(&migf->lock);
86 static struct mlx5_vhca_data_buffer *
87 mlx5vf_get_data_buff_from_pos(struct mlx5_vf_migration_file *migf, loff_t pos,
90 struct mlx5_vhca_data_buffer *buf;
94 spin_lock_irq(&migf->list_lock);
95 if (list_empty(&migf->buf_list)) {
100 buf = list_first_entry(&migf->buf_list, struct mlx5_vhca_data_buffer,
102 if (pos >= buf->start_pos &&
103 pos < buf->start_pos + buf->length) {
109 * As we use a stream based FD we may expect having the data always
112 migf->state = MLX5_MIGF_STATE_ERROR;
115 spin_unlock_irq(&migf->list_lock);
116 return found ? buf : NULL;
119 static void mlx5vf_buf_read_done(struct mlx5_vhca_data_buffer *vhca_buf)
121 struct mlx5_vf_migration_file *migf = vhca_buf->migf;
123 if (vhca_buf->stop_copy_chunk_num) {
124 bool is_header = vhca_buf->dma_dir == DMA_NONE;
125 u8 chunk_num = vhca_buf->stop_copy_chunk_num;
126 size_t next_required_umem_size = 0;
129 migf->buf_header[chunk_num - 1] = vhca_buf;
131 migf->buf[chunk_num - 1] = vhca_buf;
133 spin_lock_irq(&migf->list_lock);
134 list_del_init(&vhca_buf->buf_elm);
136 next_required_umem_size =
137 migf->next_required_umem_size;
138 migf->next_required_umem_size = 0;
139 migf->num_ready_chunks--;
141 spin_unlock_irq(&migf->list_lock);
142 if (next_required_umem_size)
143 mlx5vf_mig_file_set_save_work(migf, chunk_num,
144 next_required_umem_size);
148 spin_lock_irq(&migf->list_lock);
149 list_del_init(&vhca_buf->buf_elm);
150 list_add_tail(&vhca_buf->buf_elm, &vhca_buf->migf->avail_list);
151 spin_unlock_irq(&migf->list_lock);
154 static ssize_t mlx5vf_buf_read(struct mlx5_vhca_data_buffer *vhca_buf,
155 char __user **buf, size_t *len, loff_t *pos)
157 unsigned long offset;
161 copy_len = min_t(size_t,
162 vhca_buf->start_pos + vhca_buf->length - *pos, *len);
170 offset = *pos - vhca_buf->start_pos;
171 page_offset = offset % PAGE_SIZE;
172 offset -= page_offset;
173 page = mlx5vf_get_migration_page(vhca_buf, offset);
176 page_len = min_t(size_t, copy_len, PAGE_SIZE - page_offset);
177 from_buff = kmap_local_page(page);
178 ret = copy_to_user(*buf, from_buff + page_offset, page_len);
179 kunmap_local(from_buff);
186 copy_len -= page_len;
189 if (*pos >= vhca_buf->start_pos + vhca_buf->length)
190 mlx5vf_buf_read_done(vhca_buf);
195 static ssize_t mlx5vf_save_read(struct file *filp, char __user *buf, size_t len,
198 struct mlx5_vf_migration_file *migf = filp->private_data;
199 struct mlx5_vhca_data_buffer *vhca_buf;
200 bool first_loop_call = true;
208 if (!(filp->f_flags & O_NONBLOCK)) {
209 if (wait_event_interruptible(migf->poll_wait,
210 !list_empty(&migf->buf_list) ||
211 migf->state == MLX5_MIGF_STATE_ERROR ||
212 migf->state == MLX5_MIGF_STATE_PRE_COPY_ERROR ||
213 migf->state == MLX5_MIGF_STATE_PRE_COPY ||
214 migf->state == MLX5_MIGF_STATE_COMPLETE))
218 mutex_lock(&migf->lock);
219 if (migf->state == MLX5_MIGF_STATE_ERROR) {
227 vhca_buf = mlx5vf_get_data_buff_from_pos(migf, *pos,
229 if (first_loop_call) {
230 first_loop_call = false;
231 /* Temporary end of file as part of PRE_COPY */
232 if (end_of_data && (migf->state == MLX5_MIGF_STATE_PRE_COPY ||
233 migf->state == MLX5_MIGF_STATE_PRE_COPY_ERROR)) {
238 if (end_of_data && migf->state != MLX5_MIGF_STATE_COMPLETE) {
239 if (filp->f_flags & O_NONBLOCK) {
254 count = mlx5vf_buf_read(vhca_buf, &buf, &len, pos);
263 mutex_unlock(&migf->lock);
267 static __poll_t mlx5vf_save_poll(struct file *filp,
268 struct poll_table_struct *wait)
270 struct mlx5_vf_migration_file *migf = filp->private_data;
271 __poll_t pollflags = 0;
273 poll_wait(filp, &migf->poll_wait, wait);
275 mutex_lock(&migf->lock);
276 if (migf->state == MLX5_MIGF_STATE_ERROR)
277 pollflags = EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
278 else if (!list_empty(&migf->buf_list) ||
279 migf->state == MLX5_MIGF_STATE_COMPLETE)
280 pollflags = EPOLLIN | EPOLLRDNORM;
281 mutex_unlock(&migf->lock);
287 * FD is exposed and user can use it after receiving an error.
288 * Mark migf in error, and wake the user.
290 static void mlx5vf_mark_err(struct mlx5_vf_migration_file *migf)
292 migf->state = MLX5_MIGF_STATE_ERROR;
293 wake_up_interruptible(&migf->poll_wait);
296 void mlx5vf_mig_file_set_save_work(struct mlx5_vf_migration_file *migf,
297 u8 chunk_num, size_t next_required_umem_size)
299 migf->save_data[chunk_num - 1].next_required_umem_size =
300 next_required_umem_size;
301 migf->save_data[chunk_num - 1].migf = migf;
302 get_file(migf->filp);
303 queue_work(migf->mvdev->cb_wq,
304 &migf->save_data[chunk_num - 1].work);
307 static struct mlx5_vhca_data_buffer *
308 mlx5vf_mig_file_get_stop_copy_buf(struct mlx5_vf_migration_file *migf,
309 u8 index, size_t required_length)
311 struct mlx5_vhca_data_buffer *buf = migf->buf[index];
315 chunk_num = buf->stop_copy_chunk_num;
316 buf->migf->buf[index] = NULL;
317 /* Checking whether the pre-allocated buffer can fit */
318 if (buf->allocated_length >= required_length)
321 mlx5vf_put_data_buffer(buf);
322 buf = mlx5vf_get_data_buffer(buf->migf, required_length,
327 buf->stop_copy_chunk_num = chunk_num;
331 static void mlx5vf_mig_file_save_work(struct work_struct *_work)
333 struct mlx5vf_save_work_data *save_data = container_of(_work,
334 struct mlx5vf_save_work_data, work);
335 struct mlx5_vf_migration_file *migf = save_data->migf;
336 struct mlx5vf_pci_core_device *mvdev = migf->mvdev;
337 struct mlx5_vhca_data_buffer *buf;
339 mutex_lock(&mvdev->state_mutex);
340 if (migf->state == MLX5_MIGF_STATE_ERROR)
343 buf = mlx5vf_mig_file_get_stop_copy_buf(migf,
344 save_data->chunk_num - 1,
345 save_data->next_required_umem_size);
349 if (mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, true, false))
355 mlx5vf_put_data_buffer(buf);
357 mlx5vf_mark_err(migf);
359 mlx5vf_state_mutex_unlock(mvdev);
363 static int mlx5vf_add_stop_copy_header(struct mlx5_vf_migration_file *migf,
366 size_t size = sizeof(struct mlx5_vf_migration_header) +
367 sizeof(struct mlx5_vf_migration_tag_stop_copy_data);
368 struct mlx5_vf_migration_tag_stop_copy_data data = {};
369 struct mlx5_vhca_data_buffer *header_buf = NULL;
370 struct mlx5_vf_migration_header header = {};
376 header_buf = mlx5vf_get_data_buffer(migf, size, DMA_NONE);
377 if (IS_ERR(header_buf))
378 return PTR_ERR(header_buf);
380 header.record_size = cpu_to_le64(sizeof(data));
381 header.flags = cpu_to_le32(MLX5_MIGF_HEADER_FLAGS_TAG_OPTIONAL);
382 header.tag = cpu_to_le32(MLX5_MIGF_HEADER_TAG_STOP_COPY_SIZE);
383 page = mlx5vf_get_migration_page(header_buf, 0);
388 to_buff = kmap_local_page(page);
389 memcpy(to_buff, &header, sizeof(header));
390 header_buf->length = sizeof(header);
391 data.stop_copy_size = cpu_to_le64(migf->buf[0]->allocated_length);
392 memcpy(to_buff + sizeof(header), &data, sizeof(data));
393 header_buf->length += sizeof(data);
394 kunmap_local(to_buff);
395 header_buf->start_pos = header_buf->migf->max_pos;
396 migf->max_pos += header_buf->length;
397 spin_lock_irqsave(&migf->list_lock, flags);
398 list_add_tail(&header_buf->buf_elm, &migf->buf_list);
399 spin_unlock_irqrestore(&migf->list_lock, flags);
401 migf->pre_copy_initial_bytes = size;
404 mlx5vf_put_data_buffer(header_buf);
408 static int mlx5vf_prep_stop_copy(struct mlx5vf_pci_core_device *mvdev,
409 struct mlx5_vf_migration_file *migf,
410 size_t state_size, u64 full_size,
413 struct mlx5_vhca_data_buffer *buf;
414 size_t inc_state_size;
419 if (mvdev->chunk_mode) {
420 size_t chunk_size = min_t(size_t, MAX_CHUNK_SIZE, full_size);
422 /* from firmware perspective at least 'state_size' buffer should be set */
423 inc_state_size = max(state_size, chunk_size);
426 /* let's be ready for stop_copy size that might grow by 10 percents */
427 if (check_add_overflow(state_size, state_size / 10, &inc_state_size))
428 inc_state_size = state_size;
430 inc_state_size = state_size;
434 /* let's not overflow the device specification max SAVE size */
435 inc_state_size = min_t(size_t, inc_state_size,
436 (BIT_ULL(__mlx5_bit_sz(save_vhca_state_in, size)) - PAGE_SIZE));
438 num_chunks = mvdev->chunk_mode ? MAX_NUM_CHUNKS : 1;
439 for (i = 0; i < num_chunks; i++) {
440 buf = mlx5vf_get_data_buffer(migf, inc_state_size, DMA_FROM_DEVICE);
447 buf = mlx5vf_get_data_buffer(migf,
448 sizeof(struct mlx5_vf_migration_header), DMA_NONE);
453 migf->buf_header[i] = buf;
454 if (mvdev->chunk_mode) {
455 migf->buf[i]->stop_copy_chunk_num = i + 1;
456 migf->buf_header[i]->stop_copy_chunk_num = i + 1;
457 INIT_WORK(&migf->save_data[i].work,
458 mlx5vf_mig_file_save_work);
459 migf->save_data[i].chunk_num = i + 1;
463 ret = mlx5vf_add_stop_copy_header(migf, track);
469 for (i = 0; i < num_chunks; i++) {
471 mlx5vf_put_data_buffer(migf->buf[i]);
474 if (migf->buf_header[i]) {
475 mlx5vf_put_data_buffer(migf->buf_header[i]);
476 migf->buf_header[i] = NULL;
483 static long mlx5vf_precopy_ioctl(struct file *filp, unsigned int cmd,
486 struct mlx5_vf_migration_file *migf = filp->private_data;
487 struct mlx5vf_pci_core_device *mvdev = migf->mvdev;
488 struct mlx5_vhca_data_buffer *buf;
489 struct vfio_precopy_info info = {};
490 loff_t *pos = &filp->f_pos;
492 size_t inc_length = 0;
493 bool end_of_data = false;
496 if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
499 minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
501 if (copy_from_user(&info, (void __user *)arg, minsz))
504 if (info.argsz < minsz)
507 mutex_lock(&mvdev->state_mutex);
508 if (mvdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY &&
509 mvdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY_P2P) {
511 goto err_state_unlock;
515 * We can't issue a SAVE command when the device is suspended, so as
516 * part of VFIO_DEVICE_STATE_PRE_COPY_P2P no reason to query for extra
517 * bytes that can't be read.
519 if (mvdev->mig_state == VFIO_DEVICE_STATE_PRE_COPY) {
521 * Once the query returns it's guaranteed that there is no
522 * active SAVE command.
523 * As so, the other code below is safe with the proper locks.
525 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &inc_length,
526 NULL, MLX5VF_QUERY_INC);
528 goto err_state_unlock;
531 mutex_lock(&migf->lock);
532 if (migf->state == MLX5_MIGF_STATE_ERROR) {
534 goto err_migf_unlock;
537 if (migf->pre_copy_initial_bytes > *pos) {
538 info.initial_bytes = migf->pre_copy_initial_bytes - *pos;
540 info.dirty_bytes = migf->max_pos - *pos;
541 if (!info.dirty_bytes)
543 info.dirty_bytes += inc_length;
546 if (!end_of_data || !inc_length) {
547 mutex_unlock(&migf->lock);
551 mutex_unlock(&migf->lock);
553 * We finished transferring the current state and the device has a
554 * dirty state, save a new state to be ready for.
556 buf = mlx5vf_get_data_buffer(migf, inc_length, DMA_FROM_DEVICE);
559 mlx5vf_mark_err(migf);
560 goto err_state_unlock;
563 ret = mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, true, true);
565 mlx5vf_mark_err(migf);
566 mlx5vf_put_data_buffer(buf);
567 goto err_state_unlock;
571 mlx5vf_state_mutex_unlock(mvdev);
572 if (copy_to_user((void __user *)arg, &info, minsz))
577 mutex_unlock(&migf->lock);
579 mlx5vf_state_mutex_unlock(mvdev);
583 static const struct file_operations mlx5vf_save_fops = {
584 .owner = THIS_MODULE,
585 .read = mlx5vf_save_read,
586 .poll = mlx5vf_save_poll,
587 .unlocked_ioctl = mlx5vf_precopy_ioctl,
588 .compat_ioctl = compat_ptr_ioctl,
589 .release = mlx5vf_release_file,
593 static int mlx5vf_pci_save_device_inc_data(struct mlx5vf_pci_core_device *mvdev)
595 struct mlx5_vf_migration_file *migf = mvdev->saving_migf;
596 struct mlx5_vhca_data_buffer *buf;
600 if (migf->state == MLX5_MIGF_STATE_ERROR)
603 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &length, NULL,
604 MLX5VF_QUERY_INC | MLX5VF_QUERY_FINAL);
608 buf = mlx5vf_mig_file_get_stop_copy_buf(migf, 0, length);
614 ret = mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, true, false);
621 mlx5vf_put_data_buffer(buf);
623 mlx5vf_mark_err(migf);
627 static struct mlx5_vf_migration_file *
628 mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev, bool track)
630 struct mlx5_vf_migration_file *migf;
631 struct mlx5_vhca_data_buffer *buf;
636 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
638 return ERR_PTR(-ENOMEM);
640 migf->filp = anon_inode_getfile("mlx5vf_mig", &mlx5vf_save_fops, migf,
642 if (IS_ERR(migf->filp)) {
643 ret = PTR_ERR(migf->filp);
648 ret = mlx5vf_cmd_alloc_pd(migf);
652 stream_open(migf->filp->f_inode, migf->filp);
653 mutex_init(&migf->lock);
654 init_waitqueue_head(&migf->poll_wait);
655 init_completion(&migf->save_comp);
657 * save_comp is being used as a binary semaphore built from
658 * a completion. A normal mutex cannot be used because the lock is
659 * passed between kernel threads and lockdep can't model this.
661 complete(&migf->save_comp);
662 mlx5_cmd_init_async_ctx(mvdev->mdev, &migf->async_ctx);
663 INIT_WORK(&migf->async_data.work, mlx5vf_mig_file_cleanup_cb);
664 INIT_LIST_HEAD(&migf->buf_list);
665 INIT_LIST_HEAD(&migf->avail_list);
666 spin_lock_init(&migf->list_lock);
667 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &length, &full_size, 0);
671 ret = mlx5vf_prep_stop_copy(mvdev, migf, length, full_size, track);
676 /* leave the allocated buffer ready for the stop-copy phase */
677 buf = mlx5vf_alloc_data_buffer(migf,
678 migf->buf[0]->allocated_length, DMA_FROM_DEVICE);
688 ret = mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, false, track);
693 mlx5vf_free_data_buffer(buf);
695 mlx5fv_cmd_clean_migf_resources(migf);
704 mlx5vf_append_page_to_mig_buf(struct mlx5_vhca_data_buffer *vhca_buf,
705 const char __user **buf, size_t *len,
706 loff_t *pos, ssize_t *done)
708 unsigned long offset;
715 offset = *pos - vhca_buf->start_pos;
716 page_offset = offset % PAGE_SIZE;
718 page = mlx5vf_get_migration_page(vhca_buf, offset - page_offset);
721 page_len = min_t(size_t, *len, PAGE_SIZE - page_offset);
722 to_buff = kmap_local_page(page);
723 ret = copy_from_user(to_buff + page_offset, *buf, page_len);
724 kunmap_local(to_buff);
732 vhca_buf->length += page_len;
737 mlx5vf_resume_read_image(struct mlx5_vf_migration_file *migf,
738 struct mlx5_vhca_data_buffer *vhca_buf,
739 size_t image_size, const char __user **buf,
740 size_t *len, loff_t *pos, ssize_t *done,
743 size_t copy_len, to_copy;
746 to_copy = min_t(size_t, *len, image_size - vhca_buf->length);
749 ret = mlx5vf_append_page_to_mig_buf(vhca_buf, buf, &to_copy, pos,
756 if (vhca_buf->length == image_size) {
757 migf->load_state = MLX5_VF_LOAD_STATE_LOAD_IMAGE;
758 migf->max_pos += image_size;
766 mlx5vf_resume_read_header_data(struct mlx5_vf_migration_file *migf,
767 struct mlx5_vhca_data_buffer *vhca_buf,
768 const char __user **buf, size_t *len,
769 loff_t *pos, ssize_t *done)
771 size_t copy_len, to_copy;
772 size_t required_data;
776 required_data = migf->record_size - vhca_buf->length;
777 to_copy = min_t(size_t, *len, required_data);
780 ret = mlx5vf_append_page_to_mig_buf(vhca_buf, buf, &to_copy, pos,
787 if (vhca_buf->length == migf->record_size) {
788 switch (migf->record_tag) {
789 case MLX5_MIGF_HEADER_TAG_STOP_COPY_SIZE:
793 page = mlx5vf_get_migration_page(vhca_buf, 0);
796 to_buff = kmap_local_page(page);
797 migf->stop_copy_prep_size = min_t(u64,
798 le64_to_cpup((__le64 *)to_buff), MAX_LOAD_SIZE);
799 kunmap_local(to_buff);
807 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER;
808 migf->max_pos += migf->record_size;
809 vhca_buf->length = 0;
816 mlx5vf_resume_read_header(struct mlx5_vf_migration_file *migf,
817 struct mlx5_vhca_data_buffer *vhca_buf,
818 const char __user **buf,
819 size_t *len, loff_t *pos,
820 ssize_t *done, bool *has_work)
827 copy_len = min_t(size_t, *len,
828 sizeof(struct mlx5_vf_migration_header) - vhca_buf->length);
829 page = mlx5vf_get_migration_page(vhca_buf, 0);
832 to_buff = kmap_local_page(page);
833 ret = copy_from_user(to_buff + vhca_buf->length, *buf, copy_len);
843 vhca_buf->length += copy_len;
844 if (vhca_buf->length == sizeof(struct mlx5_vf_migration_header)) {
848 record_size = le64_to_cpup((__le64 *)to_buff);
849 if (record_size > MAX_LOAD_SIZE) {
854 migf->record_size = record_size;
855 flags = le32_to_cpup((__le32 *)(to_buff +
856 offsetof(struct mlx5_vf_migration_header, flags)));
857 migf->record_tag = le32_to_cpup((__le32 *)(to_buff +
858 offsetof(struct mlx5_vf_migration_header, tag)));
859 switch (migf->record_tag) {
860 case MLX5_MIGF_HEADER_TAG_FW_DATA:
861 migf->load_state = MLX5_VF_LOAD_STATE_PREP_IMAGE;
863 case MLX5_MIGF_HEADER_TAG_STOP_COPY_SIZE:
864 migf->load_state = MLX5_VF_LOAD_STATE_PREP_HEADER_DATA;
867 if (!(flags & MLX5_MIGF_HEADER_FLAGS_TAG_OPTIONAL)) {
871 /* We may read and skip this optional record data */
872 migf->load_state = MLX5_VF_LOAD_STATE_PREP_HEADER_DATA;
875 migf->max_pos += vhca_buf->length;
876 vhca_buf->length = 0;
880 kunmap_local(to_buff);
884 static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
885 size_t len, loff_t *pos)
887 struct mlx5_vf_migration_file *migf = filp->private_data;
888 struct mlx5_vhca_data_buffer *vhca_buf = migf->buf[0];
889 struct mlx5_vhca_data_buffer *vhca_buf_header = migf->buf_header[0];
890 loff_t requested_length;
891 bool has_work = false;
900 check_add_overflow((loff_t)len, *pos, &requested_length))
903 mutex_lock(&migf->mvdev->state_mutex);
904 mutex_lock(&migf->lock);
905 if (migf->state == MLX5_MIGF_STATE_ERROR) {
910 while (len || has_work) {
912 switch (migf->load_state) {
913 case MLX5_VF_LOAD_STATE_READ_HEADER:
914 ret = mlx5vf_resume_read_header(migf, vhca_buf_header,
920 case MLX5_VF_LOAD_STATE_PREP_HEADER_DATA:
921 if (vhca_buf_header->allocated_length < migf->record_size) {
922 mlx5vf_free_data_buffer(vhca_buf_header);
924 migf->buf_header[0] = mlx5vf_alloc_data_buffer(migf,
925 migf->record_size, DMA_NONE);
926 if (IS_ERR(migf->buf_header[0])) {
927 ret = PTR_ERR(migf->buf_header[0]);
928 migf->buf_header[0] = NULL;
932 vhca_buf_header = migf->buf_header[0];
935 vhca_buf_header->start_pos = migf->max_pos;
936 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER_DATA;
938 case MLX5_VF_LOAD_STATE_READ_HEADER_DATA:
939 ret = mlx5vf_resume_read_header_data(migf, vhca_buf_header,
940 &buf, &len, pos, &done);
944 case MLX5_VF_LOAD_STATE_PREP_IMAGE:
946 u64 size = max(migf->record_size,
947 migf->stop_copy_prep_size);
949 if (vhca_buf->allocated_length < size) {
950 mlx5vf_free_data_buffer(vhca_buf);
952 migf->buf[0] = mlx5vf_alloc_data_buffer(migf,
953 size, DMA_TO_DEVICE);
954 if (IS_ERR(migf->buf[0])) {
955 ret = PTR_ERR(migf->buf[0]);
960 vhca_buf = migf->buf[0];
963 vhca_buf->start_pos = migf->max_pos;
964 migf->load_state = MLX5_VF_LOAD_STATE_READ_IMAGE;
967 case MLX5_VF_LOAD_STATE_READ_IMAGE:
968 ret = mlx5vf_resume_read_image(migf, vhca_buf,
970 &buf, &len, pos, &done, &has_work);
974 case MLX5_VF_LOAD_STATE_LOAD_IMAGE:
975 ret = mlx5vf_cmd_load_vhca_state(migf->mvdev, migf, vhca_buf);
978 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER;
980 /* prep header buf for next image */
981 vhca_buf_header->length = 0;
982 /* prep data buf for next image */
983 vhca_buf->length = 0;
993 migf->state = MLX5_MIGF_STATE_ERROR;
994 mutex_unlock(&migf->lock);
995 mlx5vf_state_mutex_unlock(migf->mvdev);
996 return ret ? ret : done;
999 static const struct file_operations mlx5vf_resume_fops = {
1000 .owner = THIS_MODULE,
1001 .write = mlx5vf_resume_write,
1002 .release = mlx5vf_release_file,
1003 .llseek = no_llseek,
1006 static struct mlx5_vf_migration_file *
1007 mlx5vf_pci_resume_device_data(struct mlx5vf_pci_core_device *mvdev)
1009 struct mlx5_vf_migration_file *migf;
1010 struct mlx5_vhca_data_buffer *buf;
1013 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
1015 return ERR_PTR(-ENOMEM);
1017 migf->filp = anon_inode_getfile("mlx5vf_mig", &mlx5vf_resume_fops, migf,
1019 if (IS_ERR(migf->filp)) {
1020 ret = PTR_ERR(migf->filp);
1024 migf->mvdev = mvdev;
1025 ret = mlx5vf_cmd_alloc_pd(migf);
1029 buf = mlx5vf_alloc_data_buffer(migf, 0, DMA_TO_DEVICE);
1036 buf = mlx5vf_alloc_data_buffer(migf,
1037 sizeof(struct mlx5_vf_migration_header), DMA_NONE);
1043 migf->buf_header[0] = buf;
1044 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER;
1046 stream_open(migf->filp->f_inode, migf->filp);
1047 mutex_init(&migf->lock);
1048 INIT_LIST_HEAD(&migf->buf_list);
1049 INIT_LIST_HEAD(&migf->avail_list);
1050 spin_lock_init(&migf->list_lock);
1053 mlx5vf_free_data_buffer(migf->buf[0]);
1055 mlx5vf_cmd_dealloc_pd(migf);
1060 return ERR_PTR(ret);
1063 void mlx5vf_disable_fds(struct mlx5vf_pci_core_device *mvdev,
1064 enum mlx5_vf_migf_state *last_save_state)
1066 if (mvdev->resuming_migf) {
1067 mlx5vf_disable_fd(mvdev->resuming_migf);
1068 mlx5fv_cmd_clean_migf_resources(mvdev->resuming_migf);
1069 fput(mvdev->resuming_migf->filp);
1070 mvdev->resuming_migf = NULL;
1072 if (mvdev->saving_migf) {
1073 mlx5_cmd_cleanup_async_ctx(&mvdev->saving_migf->async_ctx);
1074 cancel_work_sync(&mvdev->saving_migf->async_data.work);
1075 if (last_save_state)
1076 *last_save_state = mvdev->saving_migf->state;
1077 mlx5vf_disable_fd(mvdev->saving_migf);
1078 wake_up_interruptible(&mvdev->saving_migf->poll_wait);
1079 mlx5fv_cmd_clean_migf_resources(mvdev->saving_migf);
1080 fput(mvdev->saving_migf->filp);
1081 mvdev->saving_migf = NULL;
1085 static struct file *
1086 mlx5vf_pci_step_device_state_locked(struct mlx5vf_pci_core_device *mvdev,
1089 u32 cur = mvdev->mig_state;
1092 if (cur == VFIO_DEVICE_STATE_RUNNING_P2P && new == VFIO_DEVICE_STATE_STOP) {
1093 ret = mlx5vf_cmd_suspend_vhca(mvdev,
1094 MLX5_SUSPEND_VHCA_IN_OP_MOD_SUSPEND_RESPONDER);
1096 return ERR_PTR(ret);
1100 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RUNNING_P2P) {
1101 ret = mlx5vf_cmd_resume_vhca(mvdev,
1102 MLX5_RESUME_VHCA_IN_OP_MOD_RESUME_RESPONDER);
1104 return ERR_PTR(ret);
1108 if ((cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_RUNNING_P2P) ||
1109 (cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {
1110 ret = mlx5vf_cmd_suspend_vhca(mvdev,
1111 MLX5_SUSPEND_VHCA_IN_OP_MOD_SUSPEND_INITIATOR);
1113 return ERR_PTR(ret);
1117 if ((cur == VFIO_DEVICE_STATE_RUNNING_P2P && new == VFIO_DEVICE_STATE_RUNNING) ||
1118 (cur == VFIO_DEVICE_STATE_PRE_COPY_P2P && new == VFIO_DEVICE_STATE_PRE_COPY)) {
1119 ret = mlx5vf_cmd_resume_vhca(mvdev,
1120 MLX5_RESUME_VHCA_IN_OP_MOD_RESUME_INITIATOR);
1122 return ERR_PTR(ret);
1126 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_STOP_COPY) {
1127 struct mlx5_vf_migration_file *migf;
1129 migf = mlx5vf_pci_save_device_data(mvdev, false);
1131 return ERR_CAST(migf);
1132 get_file(migf->filp);
1133 mvdev->saving_migf = migf;
1137 if (cur == VFIO_DEVICE_STATE_STOP_COPY && new == VFIO_DEVICE_STATE_STOP) {
1138 mlx5vf_disable_fds(mvdev, NULL);
1142 if ((cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_RUNNING) ||
1143 (cur == VFIO_DEVICE_STATE_PRE_COPY_P2P &&
1144 new == VFIO_DEVICE_STATE_RUNNING_P2P)) {
1145 struct mlx5_vf_migration_file *migf = mvdev->saving_migf;
1146 struct mlx5_vhca_data_buffer *buf;
1147 enum mlx5_vf_migf_state state;
1150 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &size, NULL,
1151 MLX5VF_QUERY_INC | MLX5VF_QUERY_CLEANUP);
1153 return ERR_PTR(ret);
1154 buf = mlx5vf_get_data_buffer(migf, size, DMA_FROM_DEVICE);
1156 return ERR_CAST(buf);
1157 /* pre_copy cleanup */
1158 ret = mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, false, false);
1160 mlx5vf_put_data_buffer(buf);
1161 return ERR_PTR(ret);
1163 mlx5vf_disable_fds(mvdev, &state);
1164 return (state != MLX5_MIGF_STATE_ERROR) ? NULL : ERR_PTR(-EIO);
1167 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RESUMING) {
1168 struct mlx5_vf_migration_file *migf;
1170 migf = mlx5vf_pci_resume_device_data(mvdev);
1172 return ERR_CAST(migf);
1173 get_file(migf->filp);
1174 mvdev->resuming_migf = migf;
1178 if (cur == VFIO_DEVICE_STATE_RESUMING && new == VFIO_DEVICE_STATE_STOP) {
1179 mlx5vf_disable_fds(mvdev, NULL);
1183 if ((cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_PRE_COPY) ||
1184 (cur == VFIO_DEVICE_STATE_RUNNING_P2P &&
1185 new == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {
1186 struct mlx5_vf_migration_file *migf;
1188 migf = mlx5vf_pci_save_device_data(mvdev, true);
1190 return ERR_CAST(migf);
1191 get_file(migf->filp);
1192 mvdev->saving_migf = migf;
1196 if (cur == VFIO_DEVICE_STATE_PRE_COPY_P2P && new == VFIO_DEVICE_STATE_STOP_COPY) {
1197 ret = mlx5vf_cmd_suspend_vhca(mvdev,
1198 MLX5_SUSPEND_VHCA_IN_OP_MOD_SUSPEND_RESPONDER);
1200 return ERR_PTR(ret);
1201 ret = mlx5vf_pci_save_device_inc_data(mvdev);
1202 return ret ? ERR_PTR(ret) : NULL;
1206 * vfio_mig_get_next_state() does not use arcs other than the above
1209 return ERR_PTR(-EINVAL);
1213 * This function is called in all state_mutex unlock cases to
1214 * handle a 'deferred_reset' if exists.
1216 void mlx5vf_state_mutex_unlock(struct mlx5vf_pci_core_device *mvdev)
1219 spin_lock(&mvdev->reset_lock);
1220 if (mvdev->deferred_reset) {
1221 mvdev->deferred_reset = false;
1222 spin_unlock(&mvdev->reset_lock);
1223 mvdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
1224 mlx5vf_disable_fds(mvdev, NULL);
1227 mutex_unlock(&mvdev->state_mutex);
1228 spin_unlock(&mvdev->reset_lock);
1231 static struct file *
1232 mlx5vf_pci_set_device_state(struct vfio_device *vdev,
1233 enum vfio_device_mig_state new_state)
1235 struct mlx5vf_pci_core_device *mvdev = container_of(
1236 vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1237 enum vfio_device_mig_state next_state;
1238 struct file *res = NULL;
1241 mutex_lock(&mvdev->state_mutex);
1242 while (new_state != mvdev->mig_state) {
1243 ret = vfio_mig_get_next_state(vdev, mvdev->mig_state,
1244 new_state, &next_state);
1249 res = mlx5vf_pci_step_device_state_locked(mvdev, next_state);
1252 mvdev->mig_state = next_state;
1253 if (WARN_ON(res && new_state != mvdev->mig_state)) {
1255 res = ERR_PTR(-EINVAL);
1259 mlx5vf_state_mutex_unlock(mvdev);
1263 static int mlx5vf_pci_get_data_size(struct vfio_device *vdev,
1264 unsigned long *stop_copy_length)
1266 struct mlx5vf_pci_core_device *mvdev = container_of(
1267 vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1272 mutex_lock(&mvdev->state_mutex);
1273 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &state_size,
1276 *stop_copy_length = total_size;
1277 mlx5vf_state_mutex_unlock(mvdev);
1281 static int mlx5vf_pci_get_device_state(struct vfio_device *vdev,
1282 enum vfio_device_mig_state *curr_state)
1284 struct mlx5vf_pci_core_device *mvdev = container_of(
1285 vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1287 mutex_lock(&mvdev->state_mutex);
1288 *curr_state = mvdev->mig_state;
1289 mlx5vf_state_mutex_unlock(mvdev);
1293 static void mlx5vf_pci_aer_reset_done(struct pci_dev *pdev)
1295 struct mlx5vf_pci_core_device *mvdev = mlx5vf_drvdata(pdev);
1297 if (!mvdev->migrate_cap)
1301 * As the higher VFIO layers are holding locks across reset and using
1302 * those same locks with the mm_lock we need to prevent ABBA deadlock
1303 * with the state_mutex and mm_lock.
1304 * In case the state_mutex was taken already we defer the cleanup work
1305 * to the unlock flow of the other running context.
1307 spin_lock(&mvdev->reset_lock);
1308 mvdev->deferred_reset = true;
1309 if (!mutex_trylock(&mvdev->state_mutex)) {
1310 spin_unlock(&mvdev->reset_lock);
1313 spin_unlock(&mvdev->reset_lock);
1314 mlx5vf_state_mutex_unlock(mvdev);
1317 static int mlx5vf_pci_open_device(struct vfio_device *core_vdev)
1319 struct mlx5vf_pci_core_device *mvdev = container_of(
1320 core_vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1321 struct vfio_pci_core_device *vdev = &mvdev->core_device;
1324 ret = vfio_pci_core_enable(vdev);
1328 if (mvdev->migrate_cap)
1329 mvdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
1330 vfio_pci_core_finish_enable(vdev);
1334 static void mlx5vf_pci_close_device(struct vfio_device *core_vdev)
1336 struct mlx5vf_pci_core_device *mvdev = container_of(
1337 core_vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1339 mlx5vf_cmd_close_migratable(mvdev);
1340 vfio_pci_core_close_device(core_vdev);
1343 static const struct vfio_migration_ops mlx5vf_pci_mig_ops = {
1344 .migration_set_state = mlx5vf_pci_set_device_state,
1345 .migration_get_state = mlx5vf_pci_get_device_state,
1346 .migration_get_data_size = mlx5vf_pci_get_data_size,
1349 static const struct vfio_log_ops mlx5vf_pci_log_ops = {
1350 .log_start = mlx5vf_start_page_tracker,
1351 .log_stop = mlx5vf_stop_page_tracker,
1352 .log_read_and_clear = mlx5vf_tracker_read_and_clear,
1355 static int mlx5vf_pci_init_dev(struct vfio_device *core_vdev)
1357 struct mlx5vf_pci_core_device *mvdev = container_of(core_vdev,
1358 struct mlx5vf_pci_core_device, core_device.vdev);
1361 ret = vfio_pci_core_init_dev(core_vdev);
1365 mlx5vf_cmd_set_migratable(mvdev, &mlx5vf_pci_mig_ops,
1366 &mlx5vf_pci_log_ops);
1371 static void mlx5vf_pci_release_dev(struct vfio_device *core_vdev)
1373 struct mlx5vf_pci_core_device *mvdev = container_of(core_vdev,
1374 struct mlx5vf_pci_core_device, core_device.vdev);
1376 mlx5vf_cmd_remove_migratable(mvdev);
1377 vfio_pci_core_release_dev(core_vdev);
1380 static const struct vfio_device_ops mlx5vf_pci_ops = {
1381 .name = "mlx5-vfio-pci",
1382 .init = mlx5vf_pci_init_dev,
1383 .release = mlx5vf_pci_release_dev,
1384 .open_device = mlx5vf_pci_open_device,
1385 .close_device = mlx5vf_pci_close_device,
1386 .ioctl = vfio_pci_core_ioctl,
1387 .device_feature = vfio_pci_core_ioctl_feature,
1388 .read = vfio_pci_core_read,
1389 .write = vfio_pci_core_write,
1390 .mmap = vfio_pci_core_mmap,
1391 .request = vfio_pci_core_request,
1392 .match = vfio_pci_core_match,
1393 .bind_iommufd = vfio_iommufd_physical_bind,
1394 .unbind_iommufd = vfio_iommufd_physical_unbind,
1395 .attach_ioas = vfio_iommufd_physical_attach_ioas,
1396 .detach_ioas = vfio_iommufd_physical_detach_ioas,
1399 static int mlx5vf_pci_probe(struct pci_dev *pdev,
1400 const struct pci_device_id *id)
1402 struct mlx5vf_pci_core_device *mvdev;
1405 mvdev = vfio_alloc_device(mlx5vf_pci_core_device, core_device.vdev,
1406 &pdev->dev, &mlx5vf_pci_ops);
1408 return PTR_ERR(mvdev);
1410 dev_set_drvdata(&pdev->dev, &mvdev->core_device);
1411 ret = vfio_pci_core_register_device(&mvdev->core_device);
1417 vfio_put_device(&mvdev->core_device.vdev);
1421 static void mlx5vf_pci_remove(struct pci_dev *pdev)
1423 struct mlx5vf_pci_core_device *mvdev = mlx5vf_drvdata(pdev);
1425 vfio_pci_core_unregister_device(&mvdev->core_device);
1426 vfio_put_device(&mvdev->core_device.vdev);
1429 static const struct pci_device_id mlx5vf_pci_table[] = {
1430 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_MELLANOX, 0x101e) }, /* ConnectX Family mlx5Gen Virtual Function */
1434 MODULE_DEVICE_TABLE(pci, mlx5vf_pci_table);
1436 static const struct pci_error_handlers mlx5vf_err_handlers = {
1437 .reset_done = mlx5vf_pci_aer_reset_done,
1438 .error_detected = vfio_pci_core_aer_err_detected,
1441 static struct pci_driver mlx5vf_pci_driver = {
1442 .name = KBUILD_MODNAME,
1443 .id_table = mlx5vf_pci_table,
1444 .probe = mlx5vf_pci_probe,
1445 .remove = mlx5vf_pci_remove,
1446 .err_handler = &mlx5vf_err_handlers,
1447 .driver_managed_dma = true,
1450 module_pci_driver(mlx5vf_pci_driver);
1452 MODULE_IMPORT_NS(IOMMUFD);
1453 MODULE_LICENSE("GPL");
1457 "MLX5 VFIO PCI - User Level meta-driver for MLX5 device family");