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