1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
5 #ifndef RTE_PMD_MLX5_VDPA_H_
6 #define RTE_PMD_MLX5_VDPA_H_
8 #include <linux/virtio_net.h>
12 #pragma GCC diagnostic ignored "-Wpedantic"
15 #include <vdpa_driver.h>
16 #include <rte_vhost.h>
18 #pragma GCC diagnostic error "-Wpedantic"
20 #include <rte_spinlock.h>
21 #include <rte_interrupts.h>
23 #include <mlx5_glue.h>
24 #include <mlx5_devx_cmds.h>
25 #include <mlx5_common_devx.h>
29 #define MLX5_VDPA_INTR_RETRIES 256
30 #define MLX5_VDPA_INTR_RETRIES_USEC 1000
32 #ifndef VIRTIO_F_ORDER_PLATFORM
33 #define VIRTIO_F_ORDER_PLATFORM 36
36 #ifndef VIRTIO_F_RING_PACKED
37 #define VIRTIO_F_RING_PACKED 34
40 #define MLX5_VDPA_DEFAULT_TIMER_DELAY_US 0u
41 #define MLX5_VDPA_DEFAULT_TIMER_STEP_US 1u
50 struct mlx5_devx_cq cq_obj;
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;
61 struct mlx5_vdpa_query_mr {
62 SLIST_ENTRY(mlx5_vdpa_query_mr) next;
65 struct mlx5_devx_obj *mkey;
71 MLX5_VDPA_NOTIFIER_STATE_DISABLED,
72 MLX5_VDPA_NOTIFIER_STATE_ENABLED,
73 MLX5_VDPA_NOTIFIER_STATE_ERR
76 struct mlx5_vdpa_virtq {
77 SLIST_ENTRY(mlx5_vdpa_virtq) next;
81 uint8_t notifier_state;
83 uint32_t configured:1;
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;
90 struct mlx5dv_devx_umem *obj;
94 struct rte_intr_handle *intr_handle;
95 uint64_t err_time[3]; /* RDTSC time of recent errors. */
97 struct mlx5_devx_virtio_q_couners_attr stats;
98 struct mlx5_devx_virtio_q_couners_attr reset;
101 struct mlx5_vdpa_steer {
102 struct mlx5_devx_obj *rqt;
106 struct mlx5dv_flow_matcher *matcher;
107 struct mlx5_devx_obj *tir;
114 MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER,
115 MLX5_VDPA_EVENT_MODE_FIXED_TIMER,
116 MLX5_VDPA_EVENT_MODE_ONLY_INTERRUPT
119 enum mlx5_dev_state {
120 MLX5_VDPA_STATE_PROBED = 0,
121 MLX5_VDPA_STATE_CONFIGURED,
122 MLX5_VDPA_STATE_IN_PROGRESS /* Shutting down. */
125 struct mlx5_vdpa_priv {
126 TAILQ_ENTRY(mlx5_vdpa_priv) next;
128 enum mlx5_dev_state state;
129 pthread_mutex_t vq_config_lock;
130 uint64_t no_traffic_counter;
133 int event_core; /* Event thread cpu affinity core. */
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;
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. */
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;
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[];
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,
178 * Check whether virtq is for traffic receive.
179 * According to VIRTIO_NET Spec the virtqueues index identity its type by:
184 * 2(N-1)+1 transmitqN
187 static inline uint8_t
188 is_virtq_recvq(int virtq_index, int nr_vring)
190 if (virtq_index % 2 == 0 && virtq_index != nr_vring - 1)
196 * Release all the prepared memory regions and all their related resources.
199 * The vdpa driver private structure.
201 void mlx5_vdpa_mem_dereg(struct mlx5_vdpa_priv *priv);
204 * Register all the memory regions of the virtio device to the HW and allocate
205 * all their related resources.
208 * The vdpa driver private structure.
211 * 0 on success, a negative errno value otherwise and rte_errno is set.
213 int mlx5_vdpa_mem_register(struct mlx5_vdpa_priv *priv);
217 * Create an event QP and all its related resources.
220 * The vdpa driver private structure.
222 * Number of descriptors.
224 * The guest notification file descriptor.
226 * Pointer to the event QP structure.
229 * 0 on success, -1 otherwise and rte_errno is set.
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);
235 * Destroy an event QP and all its related resources.
238 * Pointer to the event QP structure.
240 void mlx5_vdpa_event_qp_destroy(struct mlx5_vdpa_event_qp *eqp);
243 * Create all the event global resources.
246 * The vdpa driver private structure.
249 mlx5_vdpa_event_qp_global_prepare(struct mlx5_vdpa_priv *priv);
252 * Release all the event global resources.
255 * The vdpa driver private structure.
257 void mlx5_vdpa_event_qp_global_release(struct mlx5_vdpa_priv *priv);
263 * The vdpa driver private structure.
266 * 0 on success, a negative errno value otherwise and rte_errno is set.
268 int mlx5_vdpa_cqe_event_setup(struct mlx5_vdpa_priv *priv);
274 * The vdpa driver private structure.
276 void mlx5_vdpa_cqe_event_unset(struct mlx5_vdpa_priv *priv);
279 * Setup error interrupt handler.
282 * The vdpa driver private structure.
285 * 0 on success, a negative errno value otherwise and rte_errno is set.
287 int mlx5_vdpa_err_event_setup(struct mlx5_vdpa_priv *priv);
290 * Unset error event handler.
293 * The vdpa driver private structure.
295 void mlx5_vdpa_err_event_unset(struct mlx5_vdpa_priv *priv);
298 * Release virtqs and resources except that to be reused.
301 * The vdpa driver private structure.
303 void mlx5_vdpa_virtqs_release(struct mlx5_vdpa_priv *priv);
306 * Cleanup cached resources of all virtqs.
309 * The vdpa driver private structure.
311 void mlx5_vdpa_virtqs_cleanup(struct mlx5_vdpa_priv *priv);
314 * Create all the HW virtqs resources and all their related resources.
317 * The vdpa driver private structure.
320 * 0 on success, a negative errno value otherwise and rte_errno is set.
322 int mlx5_vdpa_virtqs_prepare(struct mlx5_vdpa_priv *priv);
325 * Enable\Disable virtq..
328 * The vdpa driver private structure.
332 * Set to enable, otherwise disable.
335 * 0 on success, a negative value otherwise.
337 int mlx5_vdpa_virtq_enable(struct mlx5_vdpa_priv *priv, int index, int enable);
340 * Unset steering - stop traffic.
343 * The vdpa driver private structure.
345 void mlx5_vdpa_steer_unset(struct mlx5_vdpa_priv *priv);
348 * Update steering according to the received queues status.
351 * The vdpa driver private structure.
354 * 0 on success, a negative value otherwise.
356 int mlx5_vdpa_steer_update(struct mlx5_vdpa_priv *priv);
359 * Setup steering and all its related resources to enable RSS traffic from the
360 * device to all the Rx host queues.
363 * The vdpa driver private structure.
366 * 0 on success, a negative value otherwise.
368 int mlx5_vdpa_steer_setup(struct mlx5_vdpa_priv *priv);
371 * Enable\Disable live migration logging.
374 * The vdpa driver private structure.
376 * Set for enable, unset for disable.
379 * 0 on success, a negative value otherwise.
381 int mlx5_vdpa_logging_enable(struct mlx5_vdpa_priv *priv, int enable);
384 * Set dirty bitmap logging to allow live migration.
387 * The vdpa driver private structure.
388 * @param[in] log_base
390 * @param[in] log_size
394 * 0 on success, a negative value otherwise.
396 int mlx5_vdpa_dirty_bitmap_set(struct mlx5_vdpa_priv *priv, uint64_t log_base,
400 * Log all virtqs information for live migration.
403 * The vdpa driver private structure.
405 * Set for enable, unset for disable.
408 * 0 on success, a negative value otherwise.
410 int mlx5_vdpa_lm_log(struct mlx5_vdpa_priv *priv);
413 * Modify virtq state to be ready or suspend.
416 * The vdpa driver private virtq structure.
418 * Set for ready, otherwise suspend.
421 * 0 on success, a negative value otherwise.
423 int mlx5_vdpa_virtq_modify(struct mlx5_vdpa_virtq *virtq, int state);
426 * Stop virtq before destroying it.
429 * The vdpa driver private structure.
434 * 0 on success, a negative value otherwise.
436 int mlx5_vdpa_virtq_stop(struct mlx5_vdpa_priv *priv, int index);
439 * Query virtq information.
442 * The vdpa driver private structure.
447 * 0 on success, a negative value otherwise.
449 int mlx5_vdpa_virtq_query(struct mlx5_vdpa_priv *priv, int index);
452 * Get virtq statistics.
455 * The vdpa driver private structure.
459 * The virtq statistics array to fill.
461 * The number of elements in @p stats array.
464 * A negative value on error, otherwise the number of entries filled in the
468 mlx5_vdpa_virtq_stats_get(struct mlx5_vdpa_priv *priv, int qid,
469 struct rte_vdpa_stat *stats, unsigned int n);
472 * Reset virtq statistics.
475 * The vdpa driver private structure.
480 * A negative value on error, otherwise 0.
483 mlx5_vdpa_virtq_stats_reset(struct mlx5_vdpa_priv *priv, int qid);
486 * Drain virtq CQ CQE.
489 * The vdpa driver private structure.
492 mlx5_vdpa_drain_cq(struct mlx5_vdpa_priv *priv);
495 mlx5_vdpa_is_modify_virtq_supported(struct mlx5_vdpa_priv *priv);
496 #endif /* RTE_PMD_MLX5_VDPA_H_ */