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