1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
6 #ifndef MLX5_VFIO_CMD_H
7 #define MLX5_VFIO_CMD_H
9 #include <linux/kernel.h>
10 #include <linux/vfio_pci_core.h>
11 #include <linux/mlx5/driver.h>
12 #include <linux/mlx5/cq.h>
13 #include <linux/mlx5/qp.h>
15 struct mlx5_vhca_data_buffer {
16 struct sg_append_table table;
21 enum dma_data_direction dma_dir;
23 struct mlx5_vf_migration_file *migf;
24 /* Optimize mlx5vf_get_migration_page() for sequential access */
25 struct scatterlist *last_offset_sg;
26 unsigned int sg_last_entry;
27 unsigned long last_offset;
30 struct mlx5vf_async_data {
31 struct mlx5_async_work cb_work;
32 struct work_struct work;
37 struct mlx5_vf_migration_file {
44 struct mlx5_vhca_data_buffer *buf;
45 struct mlx5vf_pci_core_device *mvdev;
46 wait_queue_head_t poll_wait;
47 struct completion save_comp;
48 struct mlx5_async_ctx async_ctx;
49 struct mlx5vf_async_data async_data;
52 struct mlx5_vhca_cq_buf {
53 struct mlx5_frag_buf_ctrl fbc;
54 struct mlx5_frag_buf frag_buf;
60 struct mlx5_vhca_cq_buf buf;
62 struct mlx5_core_cq mcq;
66 struct mlx5_vhca_recv_buf {
68 struct page **page_list;
69 dma_addr_t *dma_addrs;
75 struct mlx5_frag_buf buf;
77 struct mlx5_vhca_recv_buf recv_buf;
78 u32 tracked_page_size;
86 struct mlx5_frag_buf_ctrl fbc;
90 struct mlx5_vhca_page_tracker {
94 struct mlx5_uars_page *uar;
95 struct mlx5_vhca_cq cq;
96 struct mlx5_vhca_qp *host_qp;
97 struct mlx5_vhca_qp *fw_qp;
102 struct mlx5vf_pci_core_device {
103 struct vfio_pci_core_device core_device;
110 struct completion tracker_comp;
111 /* protect migration state */
112 struct mutex state_mutex;
113 enum vfio_device_mig_state mig_state;
114 /* protect the reset_done flow */
115 spinlock_t reset_lock;
116 struct mlx5_vf_migration_file *resuming_migf;
117 struct mlx5_vf_migration_file *saving_migf;
118 struct mlx5_vhca_page_tracker tracker;
119 struct workqueue_struct *cb_wq;
120 struct notifier_block nb;
121 struct mlx5_core_dev *mdev;
124 int mlx5vf_cmd_suspend_vhca(struct mlx5vf_pci_core_device *mvdev, u16 op_mod);
125 int mlx5vf_cmd_resume_vhca(struct mlx5vf_pci_core_device *mvdev, u16 op_mod);
126 int mlx5vf_cmd_query_vhca_migration_state(struct mlx5vf_pci_core_device *mvdev,
128 void mlx5vf_cmd_set_migratable(struct mlx5vf_pci_core_device *mvdev,
129 const struct vfio_migration_ops *mig_ops,
130 const struct vfio_log_ops *log_ops);
131 void mlx5vf_cmd_remove_migratable(struct mlx5vf_pci_core_device *mvdev);
132 void mlx5vf_cmd_close_migratable(struct mlx5vf_pci_core_device *mvdev);
133 int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev,
134 struct mlx5_vf_migration_file *migf,
135 struct mlx5_vhca_data_buffer *buf);
136 int mlx5vf_cmd_load_vhca_state(struct mlx5vf_pci_core_device *mvdev,
137 struct mlx5_vf_migration_file *migf,
138 struct mlx5_vhca_data_buffer *buf);
139 int mlx5vf_cmd_alloc_pd(struct mlx5_vf_migration_file *migf);
140 void mlx5vf_cmd_dealloc_pd(struct mlx5_vf_migration_file *migf);
141 void mlx5fv_cmd_clean_migf_resources(struct mlx5_vf_migration_file *migf);
142 struct mlx5_vhca_data_buffer *
143 mlx5vf_alloc_data_buffer(struct mlx5_vf_migration_file *migf,
144 size_t length, enum dma_data_direction dma_dir);
145 void mlx5vf_free_data_buffer(struct mlx5_vhca_data_buffer *buf);
146 int mlx5vf_add_migration_pages(struct mlx5_vhca_data_buffer *buf,
147 unsigned int npages);
148 void mlx5vf_state_mutex_unlock(struct mlx5vf_pci_core_device *mvdev);
149 void mlx5vf_disable_fds(struct mlx5vf_pci_core_device *mvdev);
150 void mlx5vf_mig_file_cleanup_cb(struct work_struct *_work);
151 int mlx5vf_start_page_tracker(struct vfio_device *vdev,
152 struct rb_root_cached *ranges, u32 nnodes, u64 *page_size);
153 int mlx5vf_stop_page_tracker(struct vfio_device *vdev);
154 int mlx5vf_tracker_read_and_clear(struct vfio_device *vdev, unsigned long iova,
155 unsigned long length, struct iova_bitmap *dirty);
156 #endif /* MLX5_VFIO_CMD_H */