vfio/mlx5: Refactor MKEY usage
[linux-2.6-microblaze.git] / drivers / vfio / pci / mlx5 / cmd.h
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /*
3  * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4  */
5
6 #ifndef MLX5_VFIO_CMD_H
7 #define MLX5_VFIO_CMD_H
8
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>
14
15 struct mlx5_vhca_data_buffer {
16         struct sg_append_table table;
17         loff_t start_pos;
18         u64 length;
19         u64 allocated_length;
20         u32 mkey;
21         enum dma_data_direction dma_dir;
22         u8 dmaed:1;
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;
28 };
29
30 struct mlx5vf_async_data {
31         struct mlx5_async_work cb_work;
32         struct work_struct work;
33         int status;
34         void *out;
35 };
36
37 struct mlx5_vf_migration_file {
38         struct file *filp;
39         struct mutex lock;
40         u8 disabled:1;
41         u8 is_err:1;
42
43         u32 pdn;
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;
50 };
51
52 struct mlx5_vhca_cq_buf {
53         struct mlx5_frag_buf_ctrl fbc;
54         struct mlx5_frag_buf frag_buf;
55         int cqe_size;
56         int nent;
57 };
58
59 struct mlx5_vhca_cq {
60         struct mlx5_vhca_cq_buf buf;
61         struct mlx5_db db;
62         struct mlx5_core_cq mcq;
63         size_t ncqe;
64 };
65
66 struct mlx5_vhca_recv_buf {
67         u32 npages;
68         struct page **page_list;
69         dma_addr_t *dma_addrs;
70         u32 next_rq_offset;
71         u32 mkey;
72 };
73
74 struct mlx5_vhca_qp {
75         struct mlx5_frag_buf buf;
76         struct mlx5_db db;
77         struct mlx5_vhca_recv_buf recv_buf;
78         u32 tracked_page_size;
79         u32 max_msg_size;
80         u32 qpn;
81         struct {
82                 unsigned int pc;
83                 unsigned int cc;
84                 unsigned int wqe_cnt;
85                 __be32 *db;
86                 struct mlx5_frag_buf_ctrl fbc;
87         } rq;
88 };
89
90 struct mlx5_vhca_page_tracker {
91         u32 id;
92         u32 pdn;
93         u8 is_err:1;
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;
98         struct mlx5_nb nb;
99         int status;
100 };
101
102 struct mlx5vf_pci_core_device {
103         struct vfio_pci_core_device core_device;
104         int vf_id;
105         u16 vhca_id;
106         u8 migrate_cap:1;
107         u8 deferred_reset:1;
108         u8 mdev_detach:1;
109         u8 log_active:1;
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;
122 };
123
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,
127                                           size_t *state_size);
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 */