1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
7 #include <sys/eventfd.h>
9 #include <rte_malloc.h>
10 #include <rte_errno.h>
11 #include <rte_lcore.h>
12 #include <rte_atomic.h>
13 #include <rte_common.h>
15 #include <rte_alarm.h>
17 #include <mlx5_common.h>
19 #include "mlx5_vdpa_utils.h"
20 #include "mlx5_vdpa.h"
23 #define MLX5_VDPA_DEFAULT_TIMER_DELAY_US 500u
24 #define MLX5_VDPA_NO_TRAFFIC_TIME_S 2LLU
27 mlx5_vdpa_event_qp_global_release(struct mlx5_vdpa_priv *priv)
30 mlx5_glue->devx_free_uar(priv->uar);
33 #ifdef HAVE_IBV_DEVX_EVENT
36 struct mlx5dv_devx_async_event_hdr event_resp;
37 uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr)
41 /* Clean all pending events. */
42 while (mlx5_glue->devx_get_event(priv->eventc, &out.event_resp,
44 (ssize_t)sizeof(out.event_resp.cookie))
46 mlx5_glue->devx_destroy_event_channel(priv->eventc);
53 /* Prepare all the global resources for all the event objects.*/
55 mlx5_vdpa_event_qp_global_prepare(struct mlx5_vdpa_priv *priv)
61 lcore = (uint32_t)rte_lcore_to_cpu_id(-1);
62 if (mlx5_glue->devx_query_eqn(priv->ctx, lcore, &priv->eqn)) {
64 DRV_LOG(ERR, "Failed to query EQ number %d.", rte_errno);
67 priv->eventc = mlx5_glue->devx_create_event_channel(priv->ctx,
68 MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA);
71 DRV_LOG(ERR, "Failed to create event channel %d.",
75 priv->uar = mlx5_glue->devx_alloc_uar(priv->ctx, 0);
78 DRV_LOG(ERR, "Failed to allocate UAR.");
83 mlx5_vdpa_event_qp_global_release(priv);
88 mlx5_vdpa_cq_destroy(struct mlx5_vdpa_cq *cq)
91 claim_zero(mlx5_devx_cmd_destroy(cq->cq));
93 claim_zero(mlx5_glue->devx_umem_dereg(cq->umem_obj));
95 rte_free((void *)(uintptr_t)cq->umem_buf);
96 memset(cq, 0, sizeof(*cq));
99 static inline void __rte_unused
100 mlx5_vdpa_cq_arm(struct mlx5_vdpa_priv *priv, struct mlx5_vdpa_cq *cq)
102 uint32_t arm_sn = cq->arm_sn << MLX5_CQ_SQN_OFFSET;
103 uint32_t cq_ci = cq->cq_ci & MLX5_CI_MASK;
104 uint32_t doorbell_hi = arm_sn | MLX5_CQ_DBR_CMD_ALL | cq_ci;
105 uint64_t doorbell = ((uint64_t)doorbell_hi << 32) | cq->cq->id;
106 uint64_t db_be = rte_cpu_to_be_64(doorbell);
107 uint32_t *addr = RTE_PTR_ADD(priv->uar->base_addr, MLX5_CQ_DOORBELL);
110 cq->db_rec[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
113 *(uint64_t *)addr = db_be;
115 *(uint32_t *)addr = db_be;
117 *((uint32_t *)addr + 1) = db_be >> 32;
124 mlx5_vdpa_cq_create(struct mlx5_vdpa_priv *priv, uint16_t log_desc_n,
125 int callfd, struct mlx5_vdpa_cq *cq)
127 struct mlx5_devx_cq_attr attr;
128 size_t pgsize = sysconf(_SC_PAGESIZE);
131 uint16_t event_nums[1] = {0};
133 cq->log_desc_n = log_desc_n;
134 umem_size = sizeof(struct mlx5_cqe) * (1 << log_desc_n) +
135 sizeof(*cq->db_rec) * 2;
136 cq->umem_buf = rte_zmalloc(__func__, umem_size, 4096);
138 DRV_LOG(ERR, "Failed to allocate memory for CQ.");
142 cq->umem_obj = mlx5_glue->devx_umem_reg(priv->ctx,
143 (void *)(uintptr_t)cq->umem_buf,
145 IBV_ACCESS_LOCAL_WRITE);
147 DRV_LOG(ERR, "Failed to register umem for CQ.");
150 attr.q_umem_valid = 1;
151 attr.db_umem_valid = 1;
152 attr.use_first_only = 0;
153 attr.overrun_ignore = 0;
154 attr.uar_page_id = priv->uar->page_id;
155 attr.q_umem_id = cq->umem_obj->umem_id;
156 attr.q_umem_offset = 0;
157 attr.db_umem_id = cq->umem_obj->umem_id;
158 attr.db_umem_offset = sizeof(struct mlx5_cqe) * (1 << log_desc_n);
159 attr.eqn = priv->eqn;
160 attr.log_cq_size = log_desc_n;
161 attr.log_page_size = rte_log2_u32(pgsize);
162 cq->cq = mlx5_devx_cmd_create_cq(priv->ctx, &attr);
165 cq->db_rec = RTE_PTR_ADD(cq->umem_buf, (uintptr_t)attr.db_umem_offset);
167 rte_spinlock_init(&cq->sl);
168 /* Subscribe CQ event to the event channel controlled by the driver. */
169 ret = mlx5_glue->devx_subscribe_devx_event(priv->eventc, cq->cq->obj,
172 (uint64_t)(uintptr_t)cq);
174 DRV_LOG(ERR, "Failed to subscribe CQE event.");
179 ret = mlx5_glue->devx_subscribe_devx_event_fd(priv->eventc,
183 DRV_LOG(ERR, "Failed to subscribe CQE event fd.");
189 /* Init CQ to ones to be in HW owner in the start. */
190 memset((void *)(uintptr_t)cq->umem_buf, 0xFF, attr.db_umem_offset);
192 mlx5_vdpa_cq_arm(priv, cq);
195 mlx5_vdpa_cq_destroy(cq);
199 static inline uint32_t
200 mlx5_vdpa_cq_poll(struct mlx5_vdpa_cq *cq)
202 struct mlx5_vdpa_event_qp *eqp =
203 container_of(cq, struct mlx5_vdpa_event_qp, cq);
204 const unsigned int cq_size = 1 << cq->log_desc_n;
205 const unsigned int cq_mask = cq_size - 1;
210 volatile struct mlx5_cqe *cqe = cq->cqes + ((cq->cq_ci + total)
213 ret = check_cqe(cqe, cq_size, cq->cq_ci + total);
215 case MLX5_CQE_STATUS_ERR:
218 case MLX5_CQE_STATUS_SW_OWN:
221 case MLX5_CQE_STATUS_HW_OWN:
225 } while (ret != MLX5_CQE_STATUS_HW_OWN);
228 /* Ring CQ doorbell record. */
229 cq->db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
231 /* Ring SW QP doorbell record. */
232 eqp->db_rec[0] = rte_cpu_to_be_32(cq->cq_ci + cq_size);
237 mlx5_vdpa_arm_all_cqs(struct mlx5_vdpa_priv *priv)
239 struct mlx5_vdpa_cq *cq;
242 for (i = 0; i < priv->nr_virtqs; i++) {
243 cq = &priv->virtqs[i].eqp.cq;
244 if (cq->cq && !cq->armed)
245 mlx5_vdpa_cq_arm(priv, cq);
250 mlx5_vdpa_poll_handle(void *arg)
252 struct mlx5_vdpa_priv *priv = arg;
254 struct mlx5_vdpa_cq *cq;
256 uint64_t current_tic;
258 pthread_mutex_lock(&priv->timer_lock);
259 while (!priv->timer_on)
260 pthread_cond_wait(&priv->timer_cond, &priv->timer_lock);
261 pthread_mutex_unlock(&priv->timer_lock);
264 for (i = 0; i < priv->nr_virtqs; i++) {
265 cq = &priv->virtqs[i].eqp.cq;
266 if (cq->cq && !cq->armed) {
267 uint32_t comp = mlx5_vdpa_cq_poll(cq);
270 /* Notify guest for descs consuming. */
271 if (cq->callfd != -1)
272 eventfd_write(cq->callfd,
278 current_tic = rte_rdtsc();
280 /* No traffic ? stop timer and load interrupts. */
281 if (current_tic - priv->last_traffic_tic >=
282 rte_get_timer_hz() * MLX5_VDPA_NO_TRAFFIC_TIME_S) {
283 DRV_LOG(DEBUG, "Device %s traffic was stopped.",
284 priv->vdev->device->name);
285 mlx5_vdpa_arm_all_cqs(priv);
286 pthread_mutex_lock(&priv->timer_lock);
288 while (!priv->timer_on)
289 pthread_cond_wait(&priv->timer_cond,
291 pthread_mutex_unlock(&priv->timer_lock);
295 priv->last_traffic_tic = current_tic;
297 usleep(priv->timer_delay_us);
303 mlx5_vdpa_interrupt_handler(void *cb_arg)
305 struct mlx5_vdpa_priv *priv = cb_arg;
306 #ifdef HAVE_IBV_DEVX_EVENT
308 struct mlx5dv_devx_async_event_hdr event_resp;
309 uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr) + 128];
312 while (mlx5_glue->devx_get_event(priv->eventc, &out.event_resp,
314 (ssize_t)sizeof(out.event_resp.cookie)) {
315 struct mlx5_vdpa_cq *cq = (struct mlx5_vdpa_cq *)
316 (uintptr_t)out.event_resp.cookie;
317 struct mlx5_vdpa_event_qp *eqp = container_of(cq,
318 struct mlx5_vdpa_event_qp, cq);
319 struct mlx5_vdpa_virtq *virtq = container_of(eqp,
320 struct mlx5_vdpa_virtq, eqp);
322 mlx5_vdpa_cq_poll(cq);
323 /* Don't arm again - timer will take control. */
324 DRV_LOG(DEBUG, "Device %s virtq %d cq %d event was captured."
325 " Timer is %s, cq ci is %u.\n",
326 priv->vdev->device->name,
327 (int)virtq->index, cq->cq->id,
328 priv->timer_on ? "on" : "off", cq->cq_ci);
333 /* Traffic detected: make sure timer is on. */
334 priv->last_traffic_tic = rte_rdtsc();
335 pthread_mutex_lock(&priv->timer_lock);
336 if (!priv->timer_on) {
338 pthread_cond_signal(&priv->timer_cond);
340 pthread_mutex_unlock(&priv->timer_lock);
344 mlx5_vdpa_cqe_event_setup(struct mlx5_vdpa_priv *priv)
350 /* All virtqs are in poll mode. */
352 pthread_mutex_init(&priv->timer_lock, NULL);
353 pthread_cond_init(&priv->timer_cond, NULL);
355 priv->timer_delay_us = MLX5_VDPA_DEFAULT_TIMER_DELAY_US;
356 ret = pthread_create(&priv->timer_tid, NULL, mlx5_vdpa_poll_handle,
359 DRV_LOG(ERR, "Failed to create timer thread.");
362 flags = fcntl(priv->eventc->fd, F_GETFL);
363 ret = fcntl(priv->eventc->fd, F_SETFL, flags | O_NONBLOCK);
365 DRV_LOG(ERR, "Failed to change event channel FD.");
368 priv->intr_handle.fd = priv->eventc->fd;
369 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
370 if (rte_intr_callback_register(&priv->intr_handle,
371 mlx5_vdpa_interrupt_handler, priv)) {
372 priv->intr_handle.fd = 0;
373 DRV_LOG(ERR, "Failed to register CQE interrupt %d.", rte_errno);
378 mlx5_vdpa_cqe_event_unset(priv);
383 mlx5_vdpa_cqe_event_unset(struct mlx5_vdpa_priv *priv)
385 int retries = MLX5_VDPA_INTR_RETRIES;
389 if (priv->intr_handle.fd) {
390 while (retries-- && ret == -EAGAIN) {
391 ret = rte_intr_callback_unregister(&priv->intr_handle,
392 mlx5_vdpa_interrupt_handler,
394 if (ret == -EAGAIN) {
395 DRV_LOG(DEBUG, "Try again to unregister fd %d "
396 "of CQ interrupt, retries = %d.",
397 priv->intr_handle.fd, retries);
401 memset(&priv->intr_handle, 0, sizeof(priv->intr_handle));
403 if (priv->timer_tid) {
404 pthread_cancel(priv->timer_tid);
405 pthread_join(priv->timer_tid, &status);
411 mlx5_vdpa_event_qp_destroy(struct mlx5_vdpa_event_qp *eqp)
414 claim_zero(mlx5_devx_cmd_destroy(eqp->sw_qp));
416 claim_zero(mlx5_glue->devx_umem_dereg(eqp->umem_obj));
418 rte_free(eqp->umem_buf);
420 claim_zero(mlx5_devx_cmd_destroy(eqp->fw_qp));
421 mlx5_vdpa_cq_destroy(&eqp->cq);
422 memset(eqp, 0, sizeof(*eqp));
426 mlx5_vdpa_qps2rts(struct mlx5_vdpa_event_qp *eqp)
428 if (mlx5_devx_cmd_modify_qp_state(eqp->fw_qp, MLX5_CMD_OP_RST2INIT_QP,
430 DRV_LOG(ERR, "Failed to modify FW QP to INIT state(%u).",
434 if (mlx5_devx_cmd_modify_qp_state(eqp->sw_qp, MLX5_CMD_OP_RST2INIT_QP,
436 DRV_LOG(ERR, "Failed to modify SW QP to INIT state(%u).",
440 if (mlx5_devx_cmd_modify_qp_state(eqp->fw_qp, MLX5_CMD_OP_INIT2RTR_QP,
442 DRV_LOG(ERR, "Failed to modify FW QP to RTR state(%u).",
446 if (mlx5_devx_cmd_modify_qp_state(eqp->sw_qp, MLX5_CMD_OP_INIT2RTR_QP,
448 DRV_LOG(ERR, "Failed to modify SW QP to RTR state(%u).",
452 if (mlx5_devx_cmd_modify_qp_state(eqp->fw_qp, MLX5_CMD_OP_RTR2RTS_QP,
454 DRV_LOG(ERR, "Failed to modify FW QP to RTS state(%u).",
458 if (mlx5_devx_cmd_modify_qp_state(eqp->sw_qp, MLX5_CMD_OP_RTR2RTS_QP,
460 DRV_LOG(ERR, "Failed to modify SW QP to RTS state(%u).",
468 mlx5_vdpa_event_qp_create(struct mlx5_vdpa_priv *priv, uint16_t desc_n,
469 int callfd, struct mlx5_vdpa_event_qp *eqp)
471 struct mlx5_devx_qp_attr attr = {0};
472 uint16_t log_desc_n = rte_log2_u32(desc_n);
473 uint32_t umem_size = (1 << log_desc_n) * MLX5_WSEG_SIZE +
474 sizeof(*eqp->db_rec) * 2;
476 if (mlx5_vdpa_event_qp_global_prepare(priv))
478 if (mlx5_vdpa_cq_create(priv, log_desc_n, callfd, &eqp->cq))
481 eqp->fw_qp = mlx5_devx_cmd_create_qp(priv->ctx, &attr);
483 DRV_LOG(ERR, "Failed to create FW QP(%u).", rte_errno);
486 eqp->umem_buf = rte_zmalloc(__func__, umem_size, 4096);
487 if (!eqp->umem_buf) {
488 DRV_LOG(ERR, "Failed to allocate memory for SW QP.");
492 eqp->umem_obj = mlx5_glue->devx_umem_reg(priv->ctx,
493 (void *)(uintptr_t)eqp->umem_buf,
495 IBV_ACCESS_LOCAL_WRITE);
496 if (!eqp->umem_obj) {
497 DRV_LOG(ERR, "Failed to register umem for SW QP.");
500 attr.uar_index = priv->uar->page_id;
501 attr.cqn = eqp->cq.cq->id;
502 attr.log_page_size = rte_log2_u32(sysconf(_SC_PAGESIZE));
503 attr.rq_size = 1 << log_desc_n;
504 attr.log_rq_stride = rte_log2_u32(MLX5_WSEG_SIZE);
505 attr.sq_size = 0; /* No need SQ. */
506 attr.dbr_umem_valid = 1;
507 attr.wq_umem_id = eqp->umem_obj->umem_id;
508 attr.wq_umem_offset = 0;
509 attr.dbr_umem_id = eqp->umem_obj->umem_id;
510 attr.dbr_address = (1 << log_desc_n) * MLX5_WSEG_SIZE;
511 eqp->sw_qp = mlx5_devx_cmd_create_qp(priv->ctx, &attr);
513 DRV_LOG(ERR, "Failed to create SW QP(%u).", rte_errno);
516 eqp->db_rec = RTE_PTR_ADD(eqp->umem_buf, (uintptr_t)attr.dbr_address);
517 if (mlx5_vdpa_qps2rts(eqp))
520 rte_write32(rte_cpu_to_be_32(1 << log_desc_n), &eqp->db_rec[0]);
523 mlx5_vdpa_event_qp_destroy(eqp);