de4154b183455fbe2e68cbf0660e29137ebae91d
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4
5 #ifndef RTE_PMD_MLX5_VDPA_H_
6 #define RTE_PMD_MLX5_VDPA_H_
7
8 #include <linux/virtio_net.h>
9 #include <sys/queue.h>
10
11 #ifdef PEDANTIC
12 #pragma GCC diagnostic ignored "-Wpedantic"
13 #endif
14 #include <rte_vdpa.h>
15 #include <rte_vdpa_dev.h>
16 #include <rte_vhost.h>
17 #ifdef PEDANTIC
18 #pragma GCC diagnostic error "-Wpedantic"
19 #endif
20 #include <rte_spinlock.h>
21 #include <rte_interrupts.h>
22
23 #include <mlx5_glue.h>
24 #include <mlx5_devx_cmds.h>
25 #include <mlx5_prm.h>
26
27
28 #define MLX5_VDPA_INTR_RETRIES 256
29 #define MLX5_VDPA_INTR_RETRIES_USEC 1000
30
31 #ifndef VIRTIO_F_ORDER_PLATFORM
32 #define VIRTIO_F_ORDER_PLATFORM 36
33 #endif
34
35 #ifndef VIRTIO_F_RING_PACKED
36 #define VIRTIO_F_RING_PACKED 34
37 #endif
38
39 struct mlx5_vdpa_cq {
40         uint16_t log_desc_n;
41         uint32_t cq_ci:24;
42         uint32_t arm_sn:2;
43         int callfd;
44         rte_spinlock_t sl;
45         struct mlx5_devx_obj *cq;
46         struct mlx5dv_devx_umem *umem_obj;
47         union {
48                 volatile void *umem_buf;
49                 volatile struct mlx5_cqe *cqes;
50         };
51         volatile uint32_t *db_rec;
52         uint64_t errors;
53 };
54
55 struct mlx5_vdpa_event_qp {
56         struct mlx5_vdpa_cq cq;
57         struct mlx5_devx_obj *fw_qp;
58         struct mlx5_devx_obj *sw_qp;
59         struct mlx5dv_devx_umem *umem_obj;
60         void *umem_buf;
61         volatile uint32_t *db_rec;
62 };
63
64 struct mlx5_vdpa_query_mr {
65         SLIST_ENTRY(mlx5_vdpa_query_mr) next;
66         void *addr;
67         uint64_t length;
68         struct mlx5dv_devx_umem *umem;
69         struct mlx5_devx_obj *mkey;
70         int is_indirect;
71 };
72
73 struct mlx5_vdpa_virtq {
74         SLIST_ENTRY(mlx5_vdpa_virtq) next;
75         uint8_t enable;
76         uint16_t index;
77         uint16_t vq_size;
78         struct mlx5_vdpa_priv *priv;
79         struct mlx5_devx_obj *virtq;
80         struct mlx5_devx_obj *counters;
81         struct mlx5_vdpa_event_qp eqp;
82         struct {
83                 struct mlx5dv_devx_umem *obj;
84                 void *buf;
85                 uint32_t size;
86         } umems[3];
87         struct rte_intr_handle intr_handle;
88         struct mlx5_devx_virtio_q_couners_attr reset;
89 };
90
91 struct mlx5_vdpa_steer {
92         struct mlx5_devx_obj *rqt;
93         void *domain;
94         void *tbl;
95         struct {
96                 struct mlx5dv_flow_matcher *matcher;
97                 struct mlx5_devx_obj *tir;
98                 void *tir_action;
99                 void *flow;
100         } rss[7];
101 };
102
103 struct mlx5_vdpa_priv {
104         TAILQ_ENTRY(mlx5_vdpa_priv) next;
105         uint8_t configured;
106         uint8_t direct_notifier; /* Whether direct notifier is on or off. */
107         struct rte_vdpa_device *vdev; /* vDPA device. */
108         int vid; /* vhost device id. */
109         struct ibv_context *ctx; /* Device context. */
110         struct rte_pci_device *pci_dev;
111         struct mlx5_hca_vdpa_attr caps;
112         uint32_t pdn; /* Protection Domain number. */
113         struct ibv_pd *pd;
114         uint32_t gpa_mkey_index;
115         struct ibv_mr *null_mr;
116         struct rte_vhost_memory *vmem;
117         uint32_t eqn;
118         struct mlx5dv_devx_event_channel *eventc;
119         struct mlx5dv_devx_uar *uar;
120         struct rte_intr_handle intr_handle;
121         struct mlx5_devx_obj *td;
122         struct mlx5_devx_obj *tis;
123         uint16_t nr_virtqs;
124         uint64_t features; /* Negotiated features. */
125         uint16_t log_max_rqt_size;
126         struct mlx5_vdpa_steer steer;
127         struct mlx5dv_var *var;
128         void *virtq_db_addr;
129         SLIST_HEAD(mr_list, mlx5_vdpa_query_mr) mr_list;
130         struct mlx5_vdpa_virtq virtqs[];
131 };
132
133 enum {
134         MLX5_VDPA_STATS_RECEIVED_DESCRIPTORS,
135         MLX5_VDPA_STATS_COMPLETED_DESCRIPTORS,
136         MLX5_VDPA_STATS_BAD_DESCRIPTOR_ERRORS,
137         MLX5_VDPA_STATS_EXCEED_MAX_CHAIN,
138         MLX5_VDPA_STATS_INVALID_BUFFER,
139         MLX5_VDPA_STATS_COMPLETION_ERRORS,
140         MLX5_VDPA_STATS_MAX
141 };
142
143 /*
144  * Check whether virtq is for traffic receive.
145  * According to VIRTIO_NET Spec the virtqueues index identity its type by:
146  * 0 receiveq1
147  * 1 transmitq1
148  * ...
149  * 2(N-1) receiveqN
150  * 2(N-1)+1 transmitqN
151  * 2N controlq
152  */
153 static inline uint8_t
154 is_virtq_recvq(int virtq_index, int nr_vring)
155 {
156         if (virtq_index % 2 == 0 && virtq_index != nr_vring - 1)
157                 return 1;
158         return 0;
159 }
160
161 /**
162  * Release all the prepared memory regions and all their related resources.
163  *
164  * @param[in] priv
165  *   The vdpa driver private structure.
166  */
167 void mlx5_vdpa_mem_dereg(struct mlx5_vdpa_priv *priv);
168
169 /**
170  * Register all the memory regions of the virtio device to the HW and allocate
171  * all their related resources.
172  *
173  * @param[in] priv
174  *   The vdpa driver private structure.
175  *
176  * @return
177  *   0 on success, a negative errno value otherwise and rte_errno is set.
178  */
179 int mlx5_vdpa_mem_register(struct mlx5_vdpa_priv *priv);
180
181
182 /**
183  * Create an event QP and all its related resources.
184  *
185  * @param[in] priv
186  *   The vdpa driver private structure.
187  * @param[in] desc_n
188  *   Number of descriptors.
189  * @param[in] callfd
190  *   The guest notification file descriptor.
191  * @param[in/out] eqp
192  *   Pointer to the event QP structure.
193  *
194  * @return
195  *   0 on success, -1 otherwise and rte_errno is set.
196  */
197 int mlx5_vdpa_event_qp_create(struct mlx5_vdpa_priv *priv, uint16_t desc_n,
198                               int callfd, struct mlx5_vdpa_event_qp *eqp);
199
200 /**
201  * Destroy an event QP and all its related resources.
202  *
203  * @param[in/out] eqp
204  *   Pointer to the event QP structure.
205  */
206 void mlx5_vdpa_event_qp_destroy(struct mlx5_vdpa_event_qp *eqp);
207
208 /**
209  * Release all the event global resources.
210  *
211  * @param[in] priv
212  *   The vdpa driver private structure.
213  */
214 void mlx5_vdpa_event_qp_global_release(struct mlx5_vdpa_priv *priv);
215
216 /**
217  * Setup CQE event.
218  *
219  * @param[in] priv
220  *   The vdpa driver private structure.
221  *
222  * @return
223  *   0 on success, a negative errno value otherwise and rte_errno is set.
224  */
225 int mlx5_vdpa_cqe_event_setup(struct mlx5_vdpa_priv *priv);
226
227 /**
228  * Unset CQE event .
229  *
230  * @param[in] priv
231  *   The vdpa driver private structure.
232  */
233 void mlx5_vdpa_cqe_event_unset(struct mlx5_vdpa_priv *priv);
234
235 /**
236  * Release a virtq and all its related resources.
237  *
238  * @param[in] priv
239  *   The vdpa driver private structure.
240  */
241 void mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv);
242
243 /**
244  * Create all the HW virtqs resources and all their related resources.
245  *
246  * @param[in] priv
247  *   The vdpa driver private structure.
248  *
249  * @return
250  *   0 on success, a negative errno value otherwise and rte_errno is set.
251  */
252 int mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv);
253
254 /**
255  * Enable\Disable virtq..
256  *
257  * @param[in] priv
258  *   The vdpa driver private structure.
259  * @param[in] index
260  *   The virtq index.
261  * @param[in] enable
262  *   Set to enable, otherwise disable.
263  *
264  * @return
265  *   0 on success, a negative value otherwise.
266  */
267 int mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable);
268
269 /**
270  * Unset steering and release all its related resources- stop traffic.
271  *
272  * @param[in] priv
273  *   The vdpa driver private structure.
274  */
275 void mlx5_vdpa_steer_unset(struct mlx5_vdpa_priv *priv);
276
277 /**
278  * Update steering according to the received queues status.
279  *
280  * @param[in] priv
281  *   The vdpa driver private structure.
282  *
283  * @return
284  *   0 on success, a negative value otherwise.
285  */
286 int mlx5_vdpa_steer_update(struct mlx5_vdpa_priv *priv);
287
288 /**
289  * Setup steering and all its related resources to enable RSS traffic from the
290  * device to all the Rx host queues.
291  *
292  * @param[in] priv
293  *   The vdpa driver private structure.
294  *
295  * @return
296  *   0 on success, a negative value otherwise.
297  */
298 int mlx5_vdpa_steer_setup(struct mlx5_vdpa_priv *priv);
299
300 /**
301  * Enable\Disable live migration logging.
302  *
303  * @param[in] priv
304  *   The vdpa driver private structure.
305  * @param[in] enable
306  *   Set for enable, unset for disable.
307  *
308  * @return
309  *   0 on success, a negative value otherwise.
310  */
311 int mlx5_vdpa_logging_enable(struct mlx5_vdpa_priv *priv, int enable);
312
313 /**
314  * Set dirty bitmap logging to allow live migration.
315  *
316  * @param[in] priv
317  *   The vdpa driver private structure.
318  * @param[in] log_base
319  *   Vhost log base.
320  * @param[in] log_size
321  *   Vhost log size.
322  *
323  * @return
324  *   0 on success, a negative value otherwise.
325  */
326 int mlx5_vdpa_dirty_bitmap_set(struct mlx5_vdpa_priv *priv, uint64_t log_base,
327                                uint64_t log_size);
328
329 /**
330  * Log all virtqs information for live migration.
331  *
332  * @param[in] priv
333  *   The vdpa driver private structure.
334  * @param[in] enable
335  *   Set for enable, unset for disable.
336  *
337  * @return
338  *   0 on success, a negative value otherwise.
339  */
340 int mlx5_vdpa_lm_log(struct mlx5_vdpa_priv *priv);
341
342 /**
343  * Modify virtq state to be ready or suspend.
344  *
345  * @param[in] virtq
346  *   The vdpa driver private virtq structure.
347  * @param[in] state
348  *   Set for ready, otherwise suspend.
349  *
350  * @return
351  *   0 on success, a negative value otherwise.
352  */
353 int mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state);
354
355 /**
356  * Stop virtq before destroying it.
357  *
358  * @param[in] priv
359  *   The vdpa driver private structure.
360  * @param[in] index
361  *   The virtq index.
362  *
363  * @return
364  *   0 on success, a negative value otherwise.
365  */
366 int mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index);
367
368 /**
369  * Get virtq statistics.
370  *
371  * @param[in] priv
372  *   The vdpa driver private structure.
373  * @param[in] qid
374  *   The virtq index.
375  * @param stats
376  *   The virtq statistics array to fill.
377  * @param n
378  *   The number of elements in @p stats array.
379  *
380  * @return
381  *   A negative value on error, otherwise the number of entries filled in the
382  *   @p stats array.
383  */
384 int
385 mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
386                           struct rte_vdpa_stat *stats, unsigned int n);
387
388 /**
389  * Reset virtq statistics.
390  *
391  * @param[in] priv
392  *   The vdpa driver private structure.
393  * @param[in] qid
394  *   The virtq index.
395  *
396  * @return
397  *   A negative value on error, otherwise 0.
398  */
399 int
400 mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid);
401 #endif /* RTE_PMD_MLX5_VDPA_H_ */