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 int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf,
71 unsigned int to_alloc = npages;
72 struct page **page_list;
77 to_fill = min_t(unsigned int, npages, PAGE_SIZE / sizeof(*page_list));
78 page_list = kvzalloc(to_fill * sizeof(*page_list), GFP_KERNEL_ACCOUNT);
83 filled = alloc_pages_bulk_array(GFP_KERNEL_ACCOUNT, to_fill,
90 ret = sg_alloc_append_table_from_pages(
91 &buf->table, page_list, filled, 0,
92 filled << PAGE_SHIFT, UINT_MAX, SG_MAX_SINGLE_ALLOC,
97 buf->allocated_length += filled * PAGE_SIZE;
98 /* clean input for another bulk allocation */
99 memset(page_list, 0, filled * sizeof(*page_list));
100 to_fill = min_t(unsigned int, to_alloc,
101 PAGE_SIZE / sizeof(*page_list));
102 } while (to_alloc > 0);
112 static void mlx5vf_disable_fd(struct mlx5_vf_migration_file *migf)
114 mutex_lock(&migf->lock);
115 migf->state = MLX5_MIGF_STATE_ERROR;
116 migf->filp->f_pos = 0;
117 mutex_unlock(&migf->lock);
120 static int mlx5vf_release_file(struct inode *inode, struct file *filp)
122 struct mlx5_vf_migration_file *migf = filp->private_data;
124 mlx5vf_disable_fd(migf);
125 mutex_destroy(&migf->lock);
130 static struct mlx5_vhca_data_buffer *
131 mlx5vf_get_data_buff_from_pos(struct mlx5_vf_migration_file *migf, loff_t pos,
134 struct mlx5_vhca_data_buffer *buf;
137 *end_of_data = false;
138 spin_lock_irq(&migf->list_lock);
139 if (list_empty(&migf->buf_list)) {
144 buf = list_first_entry(&migf->buf_list, struct mlx5_vhca_data_buffer,
146 if (pos >= buf->start_pos &&
147 pos < buf->start_pos + buf->length) {
153 * As we use a stream based FD we may expect having the data always
156 migf->state = MLX5_MIGF_STATE_ERROR;
159 spin_unlock_irq(&migf->list_lock);
160 return found ? buf : NULL;
163 static void mlx5vf_buf_read_done(struct mlx5_vhca_data_buffer *vhca_buf)
165 struct mlx5_vf_migration_file *migf = vhca_buf->migf;
167 if (vhca_buf->stop_copy_chunk_num) {
168 bool is_header = vhca_buf->dma_dir == DMA_NONE;
169 u8 chunk_num = vhca_buf->stop_copy_chunk_num;
170 size_t next_required_umem_size = 0;
173 migf->buf_header[chunk_num - 1] = vhca_buf;
175 migf->buf[chunk_num - 1] = vhca_buf;
177 spin_lock_irq(&migf->list_lock);
178 list_del_init(&vhca_buf->buf_elm);
180 next_required_umem_size =
181 migf->next_required_umem_size;
182 migf->next_required_umem_size = 0;
183 migf->num_ready_chunks--;
185 spin_unlock_irq(&migf->list_lock);
186 if (next_required_umem_size)
187 mlx5vf_mig_file_set_save_work(migf, chunk_num,
188 next_required_umem_size);
192 spin_lock_irq(&migf->list_lock);
193 list_del_init(&vhca_buf->buf_elm);
194 list_add_tail(&vhca_buf->buf_elm, &vhca_buf->migf->avail_list);
195 spin_unlock_irq(&migf->list_lock);
198 static ssize_t mlx5vf_buf_read(struct mlx5_vhca_data_buffer *vhca_buf,
199 char __user **buf, size_t *len, loff_t *pos)
201 unsigned long offset;
205 copy_len = min_t(size_t,
206 vhca_buf->start_pos + vhca_buf->length - *pos, *len);
214 offset = *pos - vhca_buf->start_pos;
215 page_offset = offset % PAGE_SIZE;
216 offset -= page_offset;
217 page = mlx5vf_get_migration_page(vhca_buf, offset);
220 page_len = min_t(size_t, copy_len, PAGE_SIZE - page_offset);
221 from_buff = kmap_local_page(page);
222 ret = copy_to_user(*buf, from_buff + page_offset, page_len);
223 kunmap_local(from_buff);
230 copy_len -= page_len;
233 if (*pos >= vhca_buf->start_pos + vhca_buf->length)
234 mlx5vf_buf_read_done(vhca_buf);
239 static ssize_t mlx5vf_save_read(struct file *filp, char __user *buf, size_t len,
242 struct mlx5_vf_migration_file *migf = filp->private_data;
243 struct mlx5_vhca_data_buffer *vhca_buf;
244 bool first_loop_call = true;
252 if (!(filp->f_flags & O_NONBLOCK)) {
253 if (wait_event_interruptible(migf->poll_wait,
254 !list_empty(&migf->buf_list) ||
255 migf->state == MLX5_MIGF_STATE_ERROR ||
256 migf->state == MLX5_MIGF_STATE_PRE_COPY_ERROR ||
257 migf->state == MLX5_MIGF_STATE_PRE_COPY ||
258 migf->state == MLX5_MIGF_STATE_COMPLETE))
262 mutex_lock(&migf->lock);
263 if (migf->state == MLX5_MIGF_STATE_ERROR) {
271 vhca_buf = mlx5vf_get_data_buff_from_pos(migf, *pos,
273 if (first_loop_call) {
274 first_loop_call = false;
275 /* Temporary end of file as part of PRE_COPY */
276 if (end_of_data && (migf->state == MLX5_MIGF_STATE_PRE_COPY ||
277 migf->state == MLX5_MIGF_STATE_PRE_COPY_ERROR)) {
282 if (end_of_data && migf->state != MLX5_MIGF_STATE_COMPLETE) {
283 if (filp->f_flags & O_NONBLOCK) {
298 count = mlx5vf_buf_read(vhca_buf, &buf, &len, pos);
307 mutex_unlock(&migf->lock);
311 static __poll_t mlx5vf_save_poll(struct file *filp,
312 struct poll_table_struct *wait)
314 struct mlx5_vf_migration_file *migf = filp->private_data;
315 __poll_t pollflags = 0;
317 poll_wait(filp, &migf->poll_wait, wait);
319 mutex_lock(&migf->lock);
320 if (migf->state == MLX5_MIGF_STATE_ERROR)
321 pollflags = EPOLLIN | EPOLLRDNORM | EPOLLRDHUP;
322 else if (!list_empty(&migf->buf_list) ||
323 migf->state == MLX5_MIGF_STATE_COMPLETE)
324 pollflags = EPOLLIN | EPOLLRDNORM;
325 mutex_unlock(&migf->lock);
331 * FD is exposed and user can use it after receiving an error.
332 * Mark migf in error, and wake the user.
334 static void mlx5vf_mark_err(struct mlx5_vf_migration_file *migf)
336 migf->state = MLX5_MIGF_STATE_ERROR;
337 wake_up_interruptible(&migf->poll_wait);
340 void mlx5vf_mig_file_set_save_work(struct mlx5_vf_migration_file *migf,
341 u8 chunk_num, size_t next_required_umem_size)
343 migf->save_data[chunk_num - 1].next_required_umem_size =
344 next_required_umem_size;
345 migf->save_data[chunk_num - 1].migf = migf;
346 get_file(migf->filp);
347 queue_work(migf->mvdev->cb_wq,
348 &migf->save_data[chunk_num - 1].work);
351 static struct mlx5_vhca_data_buffer *
352 mlx5vf_mig_file_get_stop_copy_buf(struct mlx5_vf_migration_file *migf,
353 u8 index, size_t required_length)
355 struct mlx5_vhca_data_buffer *buf = migf->buf[index];
359 chunk_num = buf->stop_copy_chunk_num;
360 buf->migf->buf[index] = NULL;
361 /* Checking whether the pre-allocated buffer can fit */
362 if (buf->allocated_length >= required_length)
365 mlx5vf_put_data_buffer(buf);
366 buf = mlx5vf_get_data_buffer(buf->migf, required_length,
371 buf->stop_copy_chunk_num = chunk_num;
375 static void mlx5vf_mig_file_save_work(struct work_struct *_work)
377 struct mlx5vf_save_work_data *save_data = container_of(_work,
378 struct mlx5vf_save_work_data, work);
379 struct mlx5_vf_migration_file *migf = save_data->migf;
380 struct mlx5vf_pci_core_device *mvdev = migf->mvdev;
381 struct mlx5_vhca_data_buffer *buf;
383 mutex_lock(&mvdev->state_mutex);
384 if (migf->state == MLX5_MIGF_STATE_ERROR)
387 buf = mlx5vf_mig_file_get_stop_copy_buf(migf,
388 save_data->chunk_num - 1,
389 save_data->next_required_umem_size);
393 if (mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, true, false))
399 mlx5vf_put_data_buffer(buf);
401 mlx5vf_mark_err(migf);
403 mlx5vf_state_mutex_unlock(mvdev);
407 static int mlx5vf_add_stop_copy_header(struct mlx5_vf_migration_file *migf,
410 size_t size = sizeof(struct mlx5_vf_migration_header) +
411 sizeof(struct mlx5_vf_migration_tag_stop_copy_data);
412 struct mlx5_vf_migration_tag_stop_copy_data data = {};
413 struct mlx5_vhca_data_buffer *header_buf = NULL;
414 struct mlx5_vf_migration_header header = {};
420 header_buf = mlx5vf_get_data_buffer(migf, size, DMA_NONE);
421 if (IS_ERR(header_buf))
422 return PTR_ERR(header_buf);
424 header.record_size = cpu_to_le64(sizeof(data));
425 header.flags = cpu_to_le32(MLX5_MIGF_HEADER_FLAGS_TAG_OPTIONAL);
426 header.tag = cpu_to_le32(MLX5_MIGF_HEADER_TAG_STOP_COPY_SIZE);
427 page = mlx5vf_get_migration_page(header_buf, 0);
432 to_buff = kmap_local_page(page);
433 memcpy(to_buff, &header, sizeof(header));
434 header_buf->length = sizeof(header);
435 data.stop_copy_size = cpu_to_le64(migf->buf[0]->allocated_length);
436 memcpy(to_buff + sizeof(header), &data, sizeof(data));
437 header_buf->length += sizeof(data);
438 kunmap_local(to_buff);
439 header_buf->start_pos = header_buf->migf->max_pos;
440 migf->max_pos += header_buf->length;
441 spin_lock_irqsave(&migf->list_lock, flags);
442 list_add_tail(&header_buf->buf_elm, &migf->buf_list);
443 spin_unlock_irqrestore(&migf->list_lock, flags);
445 migf->pre_copy_initial_bytes = size;
448 mlx5vf_put_data_buffer(header_buf);
452 static int mlx5vf_prep_stop_copy(struct mlx5vf_pci_core_device *mvdev,
453 struct mlx5_vf_migration_file *migf,
454 size_t state_size, u64 full_size,
457 struct mlx5_vhca_data_buffer *buf;
458 size_t inc_state_size;
463 if (mvdev->chunk_mode) {
464 size_t chunk_size = min_t(size_t, MAX_CHUNK_SIZE, full_size);
466 /* from firmware perspective at least 'state_size' buffer should be set */
467 inc_state_size = max(state_size, chunk_size);
470 /* let's be ready for stop_copy size that might grow by 10 percents */
471 if (check_add_overflow(state_size, state_size / 10, &inc_state_size))
472 inc_state_size = state_size;
474 inc_state_size = state_size;
478 /* let's not overflow the device specification max SAVE size */
479 inc_state_size = min_t(size_t, inc_state_size,
480 (BIT_ULL(__mlx5_bit_sz(save_vhca_state_in, size)) - PAGE_SIZE));
482 num_chunks = mvdev->chunk_mode ? MAX_NUM_CHUNKS : 1;
483 for (i = 0; i < num_chunks; i++) {
484 buf = mlx5vf_get_data_buffer(migf, inc_state_size, DMA_FROM_DEVICE);
491 buf = mlx5vf_get_data_buffer(migf,
492 sizeof(struct mlx5_vf_migration_header), DMA_NONE);
497 migf->buf_header[i] = buf;
498 if (mvdev->chunk_mode) {
499 migf->buf[i]->stop_copy_chunk_num = i + 1;
500 migf->buf_header[i]->stop_copy_chunk_num = i + 1;
501 INIT_WORK(&migf->save_data[i].work,
502 mlx5vf_mig_file_save_work);
503 migf->save_data[i].chunk_num = i + 1;
507 ret = mlx5vf_add_stop_copy_header(migf, track);
513 for (i = 0; i < num_chunks; i++) {
515 mlx5vf_put_data_buffer(migf->buf[i]);
518 if (migf->buf_header[i]) {
519 mlx5vf_put_data_buffer(migf->buf_header[i]);
520 migf->buf_header[i] = NULL;
527 static long mlx5vf_precopy_ioctl(struct file *filp, unsigned int cmd,
530 struct mlx5_vf_migration_file *migf = filp->private_data;
531 struct mlx5vf_pci_core_device *mvdev = migf->mvdev;
532 struct mlx5_vhca_data_buffer *buf;
533 struct vfio_precopy_info info = {};
534 loff_t *pos = &filp->f_pos;
536 size_t inc_length = 0;
537 bool end_of_data = false;
540 if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
543 minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
545 if (copy_from_user(&info, (void __user *)arg, minsz))
548 if (info.argsz < minsz)
551 mutex_lock(&mvdev->state_mutex);
552 if (mvdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY &&
553 mvdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY_P2P) {
555 goto err_state_unlock;
559 * We can't issue a SAVE command when the device is suspended, so as
560 * part of VFIO_DEVICE_STATE_PRE_COPY_P2P no reason to query for extra
561 * bytes that can't be read.
563 if (mvdev->mig_state == VFIO_DEVICE_STATE_PRE_COPY) {
565 * Once the query returns it's guaranteed that there is no
566 * active SAVE command.
567 * As so, the other code below is safe with the proper locks.
569 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &inc_length,
570 NULL, MLX5VF_QUERY_INC);
572 goto err_state_unlock;
575 mutex_lock(&migf->lock);
576 if (migf->state == MLX5_MIGF_STATE_ERROR) {
578 goto err_migf_unlock;
581 if (migf->pre_copy_initial_bytes > *pos) {
582 info.initial_bytes = migf->pre_copy_initial_bytes - *pos;
584 info.dirty_bytes = migf->max_pos - *pos;
585 if (!info.dirty_bytes)
587 info.dirty_bytes += inc_length;
590 if (!end_of_data || !inc_length) {
591 mutex_unlock(&migf->lock);
595 mutex_unlock(&migf->lock);
597 * We finished transferring the current state and the device has a
598 * dirty state, save a new state to be ready for.
600 buf = mlx5vf_get_data_buffer(migf, inc_length, DMA_FROM_DEVICE);
603 mlx5vf_mark_err(migf);
604 goto err_state_unlock;
607 ret = mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, true, true);
609 mlx5vf_mark_err(migf);
610 mlx5vf_put_data_buffer(buf);
611 goto err_state_unlock;
615 mlx5vf_state_mutex_unlock(mvdev);
616 if (copy_to_user((void __user *)arg, &info, minsz))
621 mutex_unlock(&migf->lock);
623 mlx5vf_state_mutex_unlock(mvdev);
627 static const struct file_operations mlx5vf_save_fops = {
628 .owner = THIS_MODULE,
629 .read = mlx5vf_save_read,
630 .poll = mlx5vf_save_poll,
631 .unlocked_ioctl = mlx5vf_precopy_ioctl,
632 .compat_ioctl = compat_ptr_ioctl,
633 .release = mlx5vf_release_file,
637 static int mlx5vf_pci_save_device_inc_data(struct mlx5vf_pci_core_device *mvdev)
639 struct mlx5_vf_migration_file *migf = mvdev->saving_migf;
640 struct mlx5_vhca_data_buffer *buf;
644 if (migf->state == MLX5_MIGF_STATE_ERROR)
647 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &length, NULL,
648 MLX5VF_QUERY_INC | MLX5VF_QUERY_FINAL);
652 buf = mlx5vf_mig_file_get_stop_copy_buf(migf, 0, length);
658 ret = mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, true, false);
665 mlx5vf_put_data_buffer(buf);
667 mlx5vf_mark_err(migf);
671 static struct mlx5_vf_migration_file *
672 mlx5vf_pci_save_device_data(struct mlx5vf_pci_core_device *mvdev, bool track)
674 struct mlx5_vf_migration_file *migf;
675 struct mlx5_vhca_data_buffer *buf;
680 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
682 return ERR_PTR(-ENOMEM);
684 migf->filp = anon_inode_getfile("mlx5vf_mig", &mlx5vf_save_fops, migf,
686 if (IS_ERR(migf->filp)) {
687 ret = PTR_ERR(migf->filp);
692 ret = mlx5vf_cmd_alloc_pd(migf);
696 stream_open(migf->filp->f_inode, migf->filp);
697 mutex_init(&migf->lock);
698 init_waitqueue_head(&migf->poll_wait);
699 init_completion(&migf->save_comp);
701 * save_comp is being used as a binary semaphore built from
702 * a completion. A normal mutex cannot be used because the lock is
703 * passed between kernel threads and lockdep can't model this.
705 complete(&migf->save_comp);
706 mlx5_cmd_init_async_ctx(mvdev->mdev, &migf->async_ctx);
707 INIT_WORK(&migf->async_data.work, mlx5vf_mig_file_cleanup_cb);
708 INIT_LIST_HEAD(&migf->buf_list);
709 INIT_LIST_HEAD(&migf->avail_list);
710 spin_lock_init(&migf->list_lock);
711 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &length, &full_size, 0);
715 ret = mlx5vf_prep_stop_copy(mvdev, migf, length, full_size, track);
720 /* leave the allocated buffer ready for the stop-copy phase */
721 buf = mlx5vf_alloc_data_buffer(migf,
722 migf->buf[0]->allocated_length, DMA_FROM_DEVICE);
732 ret = mlx5vf_cmd_save_vhca_state(mvdev, migf, buf, false, track);
737 mlx5vf_free_data_buffer(buf);
739 mlx5fv_cmd_clean_migf_resources(migf);
748 mlx5vf_append_page_to_mig_buf(struct mlx5_vhca_data_buffer *vhca_buf,
749 const char __user **buf, size_t *len,
750 loff_t *pos, ssize_t *done)
752 unsigned long offset;
759 offset = *pos - vhca_buf->start_pos;
760 page_offset = offset % PAGE_SIZE;
762 page = mlx5vf_get_migration_page(vhca_buf, offset - page_offset);
765 page_len = min_t(size_t, *len, PAGE_SIZE - page_offset);
766 to_buff = kmap_local_page(page);
767 ret = copy_from_user(to_buff + page_offset, *buf, page_len);
768 kunmap_local(to_buff);
776 vhca_buf->length += page_len;
781 mlx5vf_resume_read_image_no_header(struct mlx5_vhca_data_buffer *vhca_buf,
782 loff_t requested_length,
783 const char __user **buf, size_t *len,
784 loff_t *pos, ssize_t *done)
788 if (requested_length > MAX_LOAD_SIZE)
791 if (vhca_buf->allocated_length < requested_length) {
792 ret = mlx5vf_add_migration_pages(
794 DIV_ROUND_UP(requested_length - vhca_buf->allocated_length,
801 ret = mlx5vf_append_page_to_mig_buf(vhca_buf, buf, len, pos,
811 mlx5vf_resume_read_image(struct mlx5_vf_migration_file *migf,
812 struct mlx5_vhca_data_buffer *vhca_buf,
813 size_t image_size, const char __user **buf,
814 size_t *len, loff_t *pos, ssize_t *done,
817 size_t copy_len, to_copy;
820 to_copy = min_t(size_t, *len, image_size - vhca_buf->length);
823 ret = mlx5vf_append_page_to_mig_buf(vhca_buf, buf, &to_copy, pos,
830 if (vhca_buf->length == image_size) {
831 migf->load_state = MLX5_VF_LOAD_STATE_LOAD_IMAGE;
832 migf->max_pos += image_size;
840 mlx5vf_resume_read_header_data(struct mlx5_vf_migration_file *migf,
841 struct mlx5_vhca_data_buffer *vhca_buf,
842 const char __user **buf, size_t *len,
843 loff_t *pos, ssize_t *done)
845 size_t copy_len, to_copy;
846 size_t required_data;
850 required_data = migf->record_size - vhca_buf->length;
851 to_copy = min_t(size_t, *len, required_data);
854 ret = mlx5vf_append_page_to_mig_buf(vhca_buf, buf, &to_copy, pos,
861 if (vhca_buf->length == migf->record_size) {
862 switch (migf->record_tag) {
863 case MLX5_MIGF_HEADER_TAG_STOP_COPY_SIZE:
867 page = mlx5vf_get_migration_page(vhca_buf, 0);
870 to_buff = kmap_local_page(page);
871 migf->stop_copy_prep_size = min_t(u64,
872 le64_to_cpup((__le64 *)to_buff), MAX_LOAD_SIZE);
873 kunmap_local(to_buff);
881 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER;
882 migf->max_pos += migf->record_size;
883 vhca_buf->length = 0;
890 mlx5vf_resume_read_header(struct mlx5_vf_migration_file *migf,
891 struct mlx5_vhca_data_buffer *vhca_buf,
892 const char __user **buf,
893 size_t *len, loff_t *pos,
894 ssize_t *done, bool *has_work)
901 copy_len = min_t(size_t, *len,
902 sizeof(struct mlx5_vf_migration_header) - vhca_buf->length);
903 page = mlx5vf_get_migration_page(vhca_buf, 0);
906 to_buff = kmap_local_page(page);
907 ret = copy_from_user(to_buff + vhca_buf->length, *buf, copy_len);
917 vhca_buf->length += copy_len;
918 if (vhca_buf->length == sizeof(struct mlx5_vf_migration_header)) {
922 record_size = le64_to_cpup((__le64 *)to_buff);
923 if (record_size > MAX_LOAD_SIZE) {
928 migf->record_size = record_size;
929 flags = le32_to_cpup((__le32 *)(to_buff +
930 offsetof(struct mlx5_vf_migration_header, flags)));
931 migf->record_tag = le32_to_cpup((__le32 *)(to_buff +
932 offsetof(struct mlx5_vf_migration_header, tag)));
933 switch (migf->record_tag) {
934 case MLX5_MIGF_HEADER_TAG_FW_DATA:
935 migf->load_state = MLX5_VF_LOAD_STATE_PREP_IMAGE;
937 case MLX5_MIGF_HEADER_TAG_STOP_COPY_SIZE:
938 migf->load_state = MLX5_VF_LOAD_STATE_PREP_HEADER_DATA;
941 if (!(flags & MLX5_MIGF_HEADER_FLAGS_TAG_OPTIONAL)) {
945 /* We may read and skip this optional record data */
946 migf->load_state = MLX5_VF_LOAD_STATE_PREP_HEADER_DATA;
949 migf->max_pos += vhca_buf->length;
950 vhca_buf->length = 0;
954 kunmap_local(to_buff);
958 static ssize_t mlx5vf_resume_write(struct file *filp, const char __user *buf,
959 size_t len, loff_t *pos)
961 struct mlx5_vf_migration_file *migf = filp->private_data;
962 struct mlx5_vhca_data_buffer *vhca_buf = migf->buf[0];
963 struct mlx5_vhca_data_buffer *vhca_buf_header = migf->buf_header[0];
964 loff_t requested_length;
965 bool has_work = false;
974 check_add_overflow((loff_t)len, *pos, &requested_length))
977 mutex_lock(&migf->mvdev->state_mutex);
978 mutex_lock(&migf->lock);
979 if (migf->state == MLX5_MIGF_STATE_ERROR) {
984 while (len || has_work) {
986 switch (migf->load_state) {
987 case MLX5_VF_LOAD_STATE_READ_HEADER:
988 ret = mlx5vf_resume_read_header(migf, vhca_buf_header,
994 case MLX5_VF_LOAD_STATE_PREP_HEADER_DATA:
995 if (vhca_buf_header->allocated_length < migf->record_size) {
996 mlx5vf_free_data_buffer(vhca_buf_header);
998 migf->buf_header[0] = mlx5vf_alloc_data_buffer(migf,
999 migf->record_size, DMA_NONE);
1000 if (IS_ERR(migf->buf_header[0])) {
1001 ret = PTR_ERR(migf->buf_header[0]);
1002 migf->buf_header[0] = NULL;
1006 vhca_buf_header = migf->buf_header[0];
1009 vhca_buf_header->start_pos = migf->max_pos;
1010 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER_DATA;
1012 case MLX5_VF_LOAD_STATE_READ_HEADER_DATA:
1013 ret = mlx5vf_resume_read_header_data(migf, vhca_buf_header,
1014 &buf, &len, pos, &done);
1018 case MLX5_VF_LOAD_STATE_PREP_IMAGE:
1020 u64 size = max(migf->record_size,
1021 migf->stop_copy_prep_size);
1023 if (vhca_buf->allocated_length < size) {
1024 mlx5vf_free_data_buffer(vhca_buf);
1026 migf->buf[0] = mlx5vf_alloc_data_buffer(migf,
1027 size, DMA_TO_DEVICE);
1028 if (IS_ERR(migf->buf[0])) {
1029 ret = PTR_ERR(migf->buf[0]);
1030 migf->buf[0] = NULL;
1034 vhca_buf = migf->buf[0];
1037 vhca_buf->start_pos = migf->max_pos;
1038 migf->load_state = MLX5_VF_LOAD_STATE_READ_IMAGE;
1041 case MLX5_VF_LOAD_STATE_READ_IMAGE_NO_HEADER:
1042 ret = mlx5vf_resume_read_image_no_header(vhca_buf,
1044 &buf, &len, pos, &done);
1048 case MLX5_VF_LOAD_STATE_READ_IMAGE:
1049 ret = mlx5vf_resume_read_image(migf, vhca_buf,
1051 &buf, &len, pos, &done, &has_work);
1055 case MLX5_VF_LOAD_STATE_LOAD_IMAGE:
1056 ret = mlx5vf_cmd_load_vhca_state(migf->mvdev, migf, vhca_buf);
1059 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER;
1061 /* prep header buf for next image */
1062 vhca_buf_header->length = 0;
1063 /* prep data buf for next image */
1064 vhca_buf->length = 0;
1074 migf->state = MLX5_MIGF_STATE_ERROR;
1075 mutex_unlock(&migf->lock);
1076 mlx5vf_state_mutex_unlock(migf->mvdev);
1077 return ret ? ret : done;
1080 static const struct file_operations mlx5vf_resume_fops = {
1081 .owner = THIS_MODULE,
1082 .write = mlx5vf_resume_write,
1083 .release = mlx5vf_release_file,
1084 .llseek = no_llseek,
1087 static struct mlx5_vf_migration_file *
1088 mlx5vf_pci_resume_device_data(struct mlx5vf_pci_core_device *mvdev)
1090 struct mlx5_vf_migration_file *migf;
1091 struct mlx5_vhca_data_buffer *buf;
1094 migf = kzalloc(sizeof(*migf), GFP_KERNEL_ACCOUNT);
1096 return ERR_PTR(-ENOMEM);
1098 migf->filp = anon_inode_getfile("mlx5vf_mig", &mlx5vf_resume_fops, migf,
1100 if (IS_ERR(migf->filp)) {
1101 ret = PTR_ERR(migf->filp);
1105 migf->mvdev = mvdev;
1106 ret = mlx5vf_cmd_alloc_pd(migf);
1110 buf = mlx5vf_alloc_data_buffer(migf, 0, DMA_TO_DEVICE);
1117 if (MLX5VF_PRE_COPY_SUPP(mvdev)) {
1118 buf = mlx5vf_alloc_data_buffer(migf,
1119 sizeof(struct mlx5_vf_migration_header), DMA_NONE);
1125 migf->buf_header[0] = buf;
1126 migf->load_state = MLX5_VF_LOAD_STATE_READ_HEADER;
1128 /* Initial state will be to read the image */
1129 migf->load_state = MLX5_VF_LOAD_STATE_READ_IMAGE_NO_HEADER;
1132 stream_open(migf->filp->f_inode, migf->filp);
1133 mutex_init(&migf->lock);
1134 INIT_LIST_HEAD(&migf->buf_list);
1135 INIT_LIST_HEAD(&migf->avail_list);
1136 spin_lock_init(&migf->list_lock);
1139 mlx5vf_free_data_buffer(migf->buf[0]);
1141 mlx5vf_cmd_dealloc_pd(migf);
1146 return ERR_PTR(ret);
1149 void mlx5vf_disable_fds(struct mlx5vf_pci_core_device *mvdev)
1151 if (mvdev->resuming_migf) {
1152 mlx5vf_disable_fd(mvdev->resuming_migf);
1153 mlx5fv_cmd_clean_migf_resources(mvdev->resuming_migf);
1154 fput(mvdev->resuming_migf->filp);
1155 mvdev->resuming_migf = NULL;
1157 if (mvdev->saving_migf) {
1158 mlx5_cmd_cleanup_async_ctx(&mvdev->saving_migf->async_ctx);
1159 cancel_work_sync(&mvdev->saving_migf->async_data.work);
1160 mlx5vf_disable_fd(mvdev->saving_migf);
1161 wake_up_interruptible(&mvdev->saving_migf->poll_wait);
1162 mlx5fv_cmd_clean_migf_resources(mvdev->saving_migf);
1163 fput(mvdev->saving_migf->filp);
1164 mvdev->saving_migf = NULL;
1168 static struct file *
1169 mlx5vf_pci_step_device_state_locked(struct mlx5vf_pci_core_device *mvdev,
1172 u32 cur = mvdev->mig_state;
1175 if (cur == VFIO_DEVICE_STATE_RUNNING_P2P && new == VFIO_DEVICE_STATE_STOP) {
1176 ret = mlx5vf_cmd_suspend_vhca(mvdev,
1177 MLX5_SUSPEND_VHCA_IN_OP_MOD_SUSPEND_RESPONDER);
1179 return ERR_PTR(ret);
1183 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RUNNING_P2P) {
1184 ret = mlx5vf_cmd_resume_vhca(mvdev,
1185 MLX5_RESUME_VHCA_IN_OP_MOD_RESUME_RESPONDER);
1187 return ERR_PTR(ret);
1191 if ((cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_RUNNING_P2P) ||
1192 (cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {
1193 ret = mlx5vf_cmd_suspend_vhca(mvdev,
1194 MLX5_SUSPEND_VHCA_IN_OP_MOD_SUSPEND_INITIATOR);
1196 return ERR_PTR(ret);
1200 if ((cur == VFIO_DEVICE_STATE_RUNNING_P2P && new == VFIO_DEVICE_STATE_RUNNING) ||
1201 (cur == VFIO_DEVICE_STATE_PRE_COPY_P2P && new == VFIO_DEVICE_STATE_PRE_COPY)) {
1202 ret = mlx5vf_cmd_resume_vhca(mvdev,
1203 MLX5_RESUME_VHCA_IN_OP_MOD_RESUME_INITIATOR);
1205 return ERR_PTR(ret);
1209 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_STOP_COPY) {
1210 struct mlx5_vf_migration_file *migf;
1212 migf = mlx5vf_pci_save_device_data(mvdev, false);
1214 return ERR_CAST(migf);
1215 get_file(migf->filp);
1216 mvdev->saving_migf = migf;
1220 if ((cur == VFIO_DEVICE_STATE_STOP_COPY && new == VFIO_DEVICE_STATE_STOP) ||
1221 (cur == VFIO_DEVICE_STATE_PRE_COPY && new == VFIO_DEVICE_STATE_RUNNING) ||
1222 (cur == VFIO_DEVICE_STATE_PRE_COPY_P2P &&
1223 new == VFIO_DEVICE_STATE_RUNNING_P2P)) {
1224 mlx5vf_disable_fds(mvdev);
1228 if (cur == VFIO_DEVICE_STATE_STOP && new == VFIO_DEVICE_STATE_RESUMING) {
1229 struct mlx5_vf_migration_file *migf;
1231 migf = mlx5vf_pci_resume_device_data(mvdev);
1233 return ERR_CAST(migf);
1234 get_file(migf->filp);
1235 mvdev->resuming_migf = migf;
1239 if (cur == VFIO_DEVICE_STATE_RESUMING && new == VFIO_DEVICE_STATE_STOP) {
1240 if (!MLX5VF_PRE_COPY_SUPP(mvdev)) {
1241 ret = mlx5vf_cmd_load_vhca_state(mvdev,
1242 mvdev->resuming_migf,
1243 mvdev->resuming_migf->buf[0]);
1245 return ERR_PTR(ret);
1247 mlx5vf_disable_fds(mvdev);
1251 if ((cur == VFIO_DEVICE_STATE_RUNNING && new == VFIO_DEVICE_STATE_PRE_COPY) ||
1252 (cur == VFIO_DEVICE_STATE_RUNNING_P2P &&
1253 new == VFIO_DEVICE_STATE_PRE_COPY_P2P)) {
1254 struct mlx5_vf_migration_file *migf;
1256 migf = mlx5vf_pci_save_device_data(mvdev, true);
1258 return ERR_CAST(migf);
1259 get_file(migf->filp);
1260 mvdev->saving_migf = migf;
1264 if (cur == VFIO_DEVICE_STATE_PRE_COPY_P2P && new == VFIO_DEVICE_STATE_STOP_COPY) {
1265 ret = mlx5vf_cmd_suspend_vhca(mvdev,
1266 MLX5_SUSPEND_VHCA_IN_OP_MOD_SUSPEND_RESPONDER);
1268 return ERR_PTR(ret);
1269 ret = mlx5vf_pci_save_device_inc_data(mvdev);
1270 return ret ? ERR_PTR(ret) : NULL;
1274 * vfio_mig_get_next_state() does not use arcs other than the above
1277 return ERR_PTR(-EINVAL);
1281 * This function is called in all state_mutex unlock cases to
1282 * handle a 'deferred_reset' if exists.
1284 void mlx5vf_state_mutex_unlock(struct mlx5vf_pci_core_device *mvdev)
1287 spin_lock(&mvdev->reset_lock);
1288 if (mvdev->deferred_reset) {
1289 mvdev->deferred_reset = false;
1290 spin_unlock(&mvdev->reset_lock);
1291 mvdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
1292 mlx5vf_disable_fds(mvdev);
1295 mutex_unlock(&mvdev->state_mutex);
1296 spin_unlock(&mvdev->reset_lock);
1299 static struct file *
1300 mlx5vf_pci_set_device_state(struct vfio_device *vdev,
1301 enum vfio_device_mig_state new_state)
1303 struct mlx5vf_pci_core_device *mvdev = container_of(
1304 vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1305 enum vfio_device_mig_state next_state;
1306 struct file *res = NULL;
1309 mutex_lock(&mvdev->state_mutex);
1310 while (new_state != mvdev->mig_state) {
1311 ret = vfio_mig_get_next_state(vdev, mvdev->mig_state,
1312 new_state, &next_state);
1317 res = mlx5vf_pci_step_device_state_locked(mvdev, next_state);
1320 mvdev->mig_state = next_state;
1321 if (WARN_ON(res && new_state != mvdev->mig_state)) {
1323 res = ERR_PTR(-EINVAL);
1327 mlx5vf_state_mutex_unlock(mvdev);
1331 static int mlx5vf_pci_get_data_size(struct vfio_device *vdev,
1332 unsigned long *stop_copy_length)
1334 struct mlx5vf_pci_core_device *mvdev = container_of(
1335 vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1340 mutex_lock(&mvdev->state_mutex);
1341 ret = mlx5vf_cmd_query_vhca_migration_state(mvdev, &state_size,
1344 *stop_copy_length = total_size;
1345 mlx5vf_state_mutex_unlock(mvdev);
1349 static int mlx5vf_pci_get_device_state(struct vfio_device *vdev,
1350 enum vfio_device_mig_state *curr_state)
1352 struct mlx5vf_pci_core_device *mvdev = container_of(
1353 vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1355 mutex_lock(&mvdev->state_mutex);
1356 *curr_state = mvdev->mig_state;
1357 mlx5vf_state_mutex_unlock(mvdev);
1361 static void mlx5vf_pci_aer_reset_done(struct pci_dev *pdev)
1363 struct mlx5vf_pci_core_device *mvdev = mlx5vf_drvdata(pdev);
1365 if (!mvdev->migrate_cap)
1369 * As the higher VFIO layers are holding locks across reset and using
1370 * those same locks with the mm_lock we need to prevent ABBA deadlock
1371 * with the state_mutex and mm_lock.
1372 * In case the state_mutex was taken already we defer the cleanup work
1373 * to the unlock flow of the other running context.
1375 spin_lock(&mvdev->reset_lock);
1376 mvdev->deferred_reset = true;
1377 if (!mutex_trylock(&mvdev->state_mutex)) {
1378 spin_unlock(&mvdev->reset_lock);
1381 spin_unlock(&mvdev->reset_lock);
1382 mlx5vf_state_mutex_unlock(mvdev);
1385 static int mlx5vf_pci_open_device(struct vfio_device *core_vdev)
1387 struct mlx5vf_pci_core_device *mvdev = container_of(
1388 core_vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1389 struct vfio_pci_core_device *vdev = &mvdev->core_device;
1392 ret = vfio_pci_core_enable(vdev);
1396 if (mvdev->migrate_cap)
1397 mvdev->mig_state = VFIO_DEVICE_STATE_RUNNING;
1398 vfio_pci_core_finish_enable(vdev);
1402 static void mlx5vf_pci_close_device(struct vfio_device *core_vdev)
1404 struct mlx5vf_pci_core_device *mvdev = container_of(
1405 core_vdev, struct mlx5vf_pci_core_device, core_device.vdev);
1407 mlx5vf_cmd_close_migratable(mvdev);
1408 vfio_pci_core_close_device(core_vdev);
1411 static const struct vfio_migration_ops mlx5vf_pci_mig_ops = {
1412 .migration_set_state = mlx5vf_pci_set_device_state,
1413 .migration_get_state = mlx5vf_pci_get_device_state,
1414 .migration_get_data_size = mlx5vf_pci_get_data_size,
1417 static const struct vfio_log_ops mlx5vf_pci_log_ops = {
1418 .log_start = mlx5vf_start_page_tracker,
1419 .log_stop = mlx5vf_stop_page_tracker,
1420 .log_read_and_clear = mlx5vf_tracker_read_and_clear,
1423 static int mlx5vf_pci_init_dev(struct vfio_device *core_vdev)
1425 struct mlx5vf_pci_core_device *mvdev = container_of(core_vdev,
1426 struct mlx5vf_pci_core_device, core_device.vdev);
1429 ret = vfio_pci_core_init_dev(core_vdev);
1433 mlx5vf_cmd_set_migratable(mvdev, &mlx5vf_pci_mig_ops,
1434 &mlx5vf_pci_log_ops);
1439 static void mlx5vf_pci_release_dev(struct vfio_device *core_vdev)
1441 struct mlx5vf_pci_core_device *mvdev = container_of(core_vdev,
1442 struct mlx5vf_pci_core_device, core_device.vdev);
1444 mlx5vf_cmd_remove_migratable(mvdev);
1445 vfio_pci_core_release_dev(core_vdev);
1448 static const struct vfio_device_ops mlx5vf_pci_ops = {
1449 .name = "mlx5-vfio-pci",
1450 .init = mlx5vf_pci_init_dev,
1451 .release = mlx5vf_pci_release_dev,
1452 .open_device = mlx5vf_pci_open_device,
1453 .close_device = mlx5vf_pci_close_device,
1454 .ioctl = vfio_pci_core_ioctl,
1455 .device_feature = vfio_pci_core_ioctl_feature,
1456 .read = vfio_pci_core_read,
1457 .write = vfio_pci_core_write,
1458 .mmap = vfio_pci_core_mmap,
1459 .request = vfio_pci_core_request,
1460 .match = vfio_pci_core_match,
1461 .bind_iommufd = vfio_iommufd_physical_bind,
1462 .unbind_iommufd = vfio_iommufd_physical_unbind,
1463 .attach_ioas = vfio_iommufd_physical_attach_ioas,
1464 .detach_ioas = vfio_iommufd_physical_detach_ioas,
1467 static int mlx5vf_pci_probe(struct pci_dev *pdev,
1468 const struct pci_device_id *id)
1470 struct mlx5vf_pci_core_device *mvdev;
1473 mvdev = vfio_alloc_device(mlx5vf_pci_core_device, core_device.vdev,
1474 &pdev->dev, &mlx5vf_pci_ops);
1476 return PTR_ERR(mvdev);
1478 dev_set_drvdata(&pdev->dev, &mvdev->core_device);
1479 ret = vfio_pci_core_register_device(&mvdev->core_device);
1485 vfio_put_device(&mvdev->core_device.vdev);
1489 static void mlx5vf_pci_remove(struct pci_dev *pdev)
1491 struct mlx5vf_pci_core_device *mvdev = mlx5vf_drvdata(pdev);
1493 vfio_pci_core_unregister_device(&mvdev->core_device);
1494 vfio_put_device(&mvdev->core_device.vdev);
1497 static const struct pci_device_id mlx5vf_pci_table[] = {
1498 { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_MELLANOX, 0x101e) }, /* ConnectX Family mlx5Gen Virtual Function */
1502 MODULE_DEVICE_TABLE(pci, mlx5vf_pci_table);
1504 static const struct pci_error_handlers mlx5vf_err_handlers = {
1505 .reset_done = mlx5vf_pci_aer_reset_done,
1506 .error_detected = vfio_pci_core_aer_err_detected,
1509 static struct pci_driver mlx5vf_pci_driver = {
1510 .name = KBUILD_MODNAME,
1511 .id_table = mlx5vf_pci_table,
1512 .probe = mlx5vf_pci_probe,
1513 .remove = mlx5vf_pci_remove,
1514 .err_handler = &mlx5vf_err_handlers,
1515 .driver_managed_dma = true,
1518 module_pci_driver(mlx5vf_pci_driver);
1520 MODULE_IMPORT_NS(IOMMUFD);
1521 MODULE_LICENSE("GPL");
1522 MODULE_AUTHOR("Max Gurtovoy <mgurtovoy@nvidia.com>");
1523 MODULE_AUTHOR("Yishai Hadas <yishaih@nvidia.com>");
1525 "MLX5 VFIO PCI - User Level meta-driver for MLX5 device family");