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