vdpa/mlx5: pre-create virtq at probing time
[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 <vdpa_driver.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_common_devx.h>
26 #include <mlx5_prm.h>
27
28
29 #define MLX5_VDPA_INTR_RETRIES 256
30 #define MLX5_VDPA_INTR_RETRIES_USEC 1000
31
32 #ifndef VIRTIO_F_ORDER_PLATFORM
33 #define VIRTIO_F_ORDER_PLATFORM 36
34 #endif
35
36 #ifndef VIRTIO_F_RING_PACKED
37 #define VIRTIO_F_RING_PACKED 34
38 #endif
39
40 #define MLX5_VDPA_DEFAULT_TIMER_DELAY_US 0u
41 #define MLX5_VDPA_DEFAULT_TIMER_STEP_US 1u
42
43 struct mlx5_vdpa_cq {
44         uint16_t log_desc_n;
45         uint32_t cq_ci:24;
46         uint32_t arm_sn:2;
47         uint32_t armed:1;
48         int callfd;
49         rte_spinlock_t sl;
50         struct mlx5_devx_cq cq_obj;
51         uint64_t errors;
52 };
53
54 struct mlx5_vdpa_event_qp {
55         struct mlx5_vdpa_cq cq;
56         struct mlx5_devx_obj *fw_qp;
57         struct mlx5_devx_qp sw_qp;
58         uint16_t qp_pi;
59 };
60
61 struct mlx5_vdpa_query_mr {
62         SLIST_ENTRY(mlx5_vdpa_query_mr) next;
63         union {
64                 struct ibv_mr *mr;
65                 struct mlx5_devx_obj *mkey;
66         };
67         int is_indirect;
68 };
69
70 enum {
71         MLX5_VDPA_NOTIFIER_STATE_DISABLED,
72         MLX5_VDPA_NOTIFIER_STATE_ENABLED,
73         MLX5_VDPA_NOTIFIER_STATE_ERR
74 };
75
76 struct mlx5_vdpa_virtq {
77         SLIST_ENTRY(mlx5_vdpa_virtq) next;
78         uint8_t enable;
79         uint16_t index;
80         uint16_t vq_size;
81         uint8_t notifier_state;
82         bool stopped;
83         uint32_t configured:1;
84         uint32_t version;
85         struct mlx5_vdpa_priv *priv;
86         struct mlx5_devx_obj *virtq;
87         struct mlx5_devx_obj *counters;
88         struct mlx5_vdpa_event_qp eqp;
89         struct {
90                 struct mlx5dv_devx_umem *obj;
91                 void *buf;
92                 uint32_t size;
93         } umems[3];
94         struct rte_intr_handle *intr_handle;
95         uint64_t err_time[3]; /* RDTSC time of recent errors. */
96         uint32_t n_retry;
97         struct mlx5_devx_virtio_q_couners_attr stats;
98         struct mlx5_devx_virtio_q_couners_attr reset;
99 };
100
101 struct mlx5_vdpa_steer {
102         struct mlx5_devx_obj *rqt;
103         void *domain;
104         void *tbl;
105         struct {
106                 struct mlx5dv_flow_matcher *matcher;
107                 struct mlx5_devx_obj *tir;
108                 void *tir_action;
109                 void *flow;
110         } rss[7];
111 };
112
113 enum {
114         MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER,
115         MLX5_VDPA_EVENT_MODE_FIXED_TIMER,
116         MLX5_VDPA_EVENT_MODE_ONLY_INTERRUPT
117 };
118
119 enum mlx5_dev_state {
120         MLX5_VDPA_STATE_PROBED = 0,
121         MLX5_VDPA_STATE_CONFIGURED,
122         MLX5_VDPA_STATE_IN_PROGRESS /* Shutting down. */
123 };
124
125 struct mlx5_vdpa_priv {
126         TAILQ_ENTRY(mlx5_vdpa_priv) next;
127         bool connected;
128         enum mlx5_dev_state state;
129         pthread_mutex_t vq_config_lock;
130         uint64_t no_traffic_counter;
131         pthread_t timer_tid;
132         int event_mode;
133         int event_core; /* Event thread cpu affinity core. */
134         uint32_t event_us;
135         uint32_t timer_delay_us;
136         uint32_t no_traffic_max;
137         uint8_t hw_latency_mode; /* Hardware CQ moderation mode. */
138         uint16_t hw_max_latency_us; /* Hardware CQ moderation period in usec. */
139         uint16_t hw_max_pending_comp; /* Hardware CQ moderation counter. */
140         uint16_t queue_size; /* virtq depth for pre-creating virtq resource */
141         uint16_t queues; /* Max virtq pair for pre-creating virtq resource */
142         struct rte_vdpa_device *vdev; /* vDPA device. */
143         struct mlx5_common_device *cdev; /* Backend mlx5 device. */
144         int vid; /* vhost device id. */
145         struct mlx5_hca_vdpa_attr caps;
146         uint32_t gpa_mkey_index;
147         struct ibv_mr *null_mr;
148         struct rte_vhost_memory *vmem;
149         struct mlx5dv_devx_event_channel *eventc;
150         struct mlx5dv_devx_event_channel *err_chnl;
151         struct mlx5_uar uar;
152         struct rte_intr_handle *err_intr_handle;
153         struct mlx5_devx_obj *td;
154         struct mlx5_devx_obj *tiss[16]; /* TIS list for each LAG port. */
155         uint16_t nr_virtqs;
156         uint8_t num_lag_ports;
157         uint64_t features; /* Negotiated features. */
158         uint16_t log_max_rqt_size;
159         struct mlx5_vdpa_steer steer;
160         struct mlx5dv_var *var;
161         void *virtq_db_addr;
162         struct mlx5_pmd_wrapped_mr lm_mr;
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_prepare(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  * Create all the event global resources.
244  *
245  * @param[in] priv
246  *   The vdpa driver private structure.
247  */
248 int
249 mlx5_vdpa_event_qp_global_prepare(struct mlx5_vdpa_priv *priv);
250
251 /**
252  * Release all the event global resources.
253  *
254  * @param[in] priv
255  *   The vdpa driver private structure.
256  */
257 void mlx5_vdpa_event_qp_global_release(struct mlx5_vdpa_priv *priv);
258
259 /**
260  * Setup CQE event.
261  *
262  * @param[in] priv
263  *   The vdpa driver private structure.
264  *
265  * @return
266  *   0 on success, a negative errno value otherwise and rte_errno is set.
267  */
268 int mlx5_vdpa_cqe_event_setup(struct mlx5_vdpa_priv *priv);
269
270 /**
271  * Unset CQE event .
272  *
273  * @param[in] priv
274  *   The vdpa driver private structure.
275  */
276 void mlx5_vdpa_cqe_event_unset(struct mlx5_vdpa_priv *priv);
277
278 /**
279  * Setup error interrupt handler.
280  *
281  * @param[in] priv
282  *   The vdpa driver private structure.
283  *
284  * @return
285  *   0 on success, a negative errno value otherwise and rte_errno is set.
286  */
287 int mlx5_vdpa_err_event_setup(struct mlx5_vdpa_priv *priv);
288
289 /**
290  * Unset error event handler.
291  *
292  * @param[in] priv
293  *   The vdpa driver private structure.
294  */
295 void mlx5_vdpa_err_event_unset(struct mlx5_vdpa_priv *priv);
296
297 /**
298  * Release virtqs and resources except that to be reused.
299  *
300  * @param[in] priv
301  *   The vdpa driver private structure.
302  */
303 void mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv);
304
305 /**
306  * Cleanup cached resources of all virtqs.
307  *
308  * @param[in] priv
309  *   The vdpa driver private structure.
310  */
311 void mlx5_vdpa_virtqs_cleanup(struct mlx5_vdpa_priv *priv);
312
313 /**
314  * Create all the HW virtqs resources and all their related resources.
315  *
316  * @param[in] priv
317  *   The vdpa driver private structure.
318  *
319  * @return
320  *   0 on success, a negative errno value otherwise and rte_errno is set.
321  */
322 int mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv);
323
324 /**
325  * Enable\Disable virtq..
326  *
327  * @param[in] priv
328  *   The vdpa driver private structure.
329  * @param[in] index
330  *   The virtq index.
331  * @param[in] enable
332  *   Set to enable, otherwise disable.
333  *
334  * @return
335  *   0 on success, a negative value otherwise.
336  */
337 int mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable);
338
339 /**
340  * Unset steering - stop traffic.
341  *
342  * @param[in] priv
343  *   The vdpa driver private structure.
344  */
345 void mlx5_vdpa_steer_unset(struct mlx5_vdpa_priv *priv);
346
347 /**
348  * Update steering according to the received queues status.
349  *
350  * @param[in] priv
351  *   The vdpa driver private structure.
352  *
353  * @return
354  *   0 on success, a negative value otherwise.
355  */
356 int mlx5_vdpa_steer_update(struct mlx5_vdpa_priv *priv);
357
358 /**
359  * Setup steering and all its related resources to enable RSS traffic from the
360  * device to all the Rx host queues.
361  *
362  * @param[in] priv
363  *   The vdpa driver private structure.
364  *
365  * @return
366  *   0 on success, a negative value otherwise.
367  */
368 int mlx5_vdpa_steer_setup(struct mlx5_vdpa_priv *priv);
369
370 /**
371  * Enable\Disable live migration logging.
372  *
373  * @param[in] priv
374  *   The vdpa driver private structure.
375  * @param[in] enable
376  *   Set for enable, unset for disable.
377  *
378  * @return
379  *   0 on success, a negative value otherwise.
380  */
381 int mlx5_vdpa_logging_enable(struct mlx5_vdpa_priv *priv, int enable);
382
383 /**
384  * Set dirty bitmap logging to allow live migration.
385  *
386  * @param[in] priv
387  *   The vdpa driver private structure.
388  * @param[in] log_base
389  *   Vhost log base.
390  * @param[in] log_size
391  *   Vhost log size.
392  *
393  * @return
394  *   0 on success, a negative value otherwise.
395  */
396 int mlx5_vdpa_dirty_bitmap_set(struct mlx5_vdpa_priv *priv, uint64_t log_base,
397                                uint64_t log_size);
398
399 /**
400  * Log all virtqs information for live migration.
401  *
402  * @param[in] priv
403  *   The vdpa driver private structure.
404  * @param[in] enable
405  *   Set for enable, unset for disable.
406  *
407  * @return
408  *   0 on success, a negative value otherwise.
409  */
410 int mlx5_vdpa_lm_log(struct mlx5_vdpa_priv *priv);
411
412 /**
413  * Modify virtq state to be ready or suspend.
414  *
415  * @param[in] virtq
416  *   The vdpa driver private virtq structure.
417  * @param[in] state
418  *   Set for ready, otherwise suspend.
419  *
420  * @return
421  *   0 on success, a negative value otherwise.
422  */
423 int mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state);
424
425 /**
426  * Stop virtq before destroying it.
427  *
428  * @param[in] priv
429  *   The vdpa driver private structure.
430  * @param[in] index
431  *   The virtq index.
432  *
433  * @return
434  *   0 on success, a negative value otherwise.
435  */
436 int mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index);
437
438 /**
439  * Query virtq information.
440  *
441  * @param[in] priv
442  *   The vdpa driver private structure.
443  * @param[in] index
444  *   The virtq index.
445  *
446  * @return
447  *   0 on success, a negative value otherwise.
448  */
449 int mlx5_vdpa_virtq_query(struct mlx5_vdpa_priv *priv, int index);
450
451 /**
452  * Get virtq statistics.
453  *
454  * @param[in] priv
455  *   The vdpa driver private structure.
456  * @param[in] qid
457  *   The virtq index.
458  * @param stats
459  *   The virtq statistics array to fill.
460  * @param n
461  *   The number of elements in @p stats array.
462  *
463  * @return
464  *   A negative value on error, otherwise the number of entries filled in the
465  *   @p stats array.
466  */
467 int
468 mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
469                           struct rte_vdpa_stat *stats, unsigned int n);
470
471 /**
472  * Reset virtq statistics.
473  *
474  * @param[in] priv
475  *   The vdpa driver private structure.
476  * @param[in] qid
477  *   The virtq index.
478  *
479  * @return
480  *   A negative value on error, otherwise 0.
481  */
482 int
483 mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid);
484
485 /**
486  * Drain virtq CQ CQE.
487  *
488  * @param[in] priv
489  *   The vdpa driver private structure.
490  */
491 void
492 mlx5_vdpa_drain_cq(struct mlx5_vdpa_priv *priv);
493
494 bool
495 mlx5_vdpa_is_modify_virtq_supported(struct mlx5_vdpa_priv *priv);
496 #endif /* RTE_PMD_MLX5_VDPA_H_ */