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"
24 mlx5_vdpa_event_qp_global_release(struct mlx5_vdpa_priv *priv)
27 mlx5_glue->devx_free_uar(priv->uar);
30 #ifdef HAVE_IBV_DEVX_EVENT
33 struct mlx5dv_devx_async_event_hdr event_resp;
34 uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr)
38 /* Clean all pending events. */
39 while (mlx5_glue->devx_get_event(priv->eventc, &out.event_resp,
41 (ssize_t)sizeof(out.event_resp.cookie))
43 mlx5_glue->devx_destroy_event_channel(priv->eventc);
50 /* Prepare all the global resources for all the event objects.*/
52 mlx5_vdpa_event_qp_global_prepare(struct mlx5_vdpa_priv *priv)
58 if (mlx5_glue->devx_query_eqn(priv->ctx, 0, &priv->eqn)) {
60 DRV_LOG(ERR, "Failed to query EQ number %d.", rte_errno);
63 priv->eventc = mlx5_glue->devx_create_event_channel(priv->ctx,
64 MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA);
67 DRV_LOG(ERR, "Failed to create event channel %d.",
71 flags = fcntl(priv->eventc->fd, F_GETFL);
72 ret = fcntl(priv->eventc->fd, F_SETFL, flags | O_NONBLOCK);
74 DRV_LOG(ERR, "Failed to change event channel FD.");
77 priv->uar = mlx5_glue->devx_alloc_uar(priv->ctx, 0);
80 DRV_LOG(ERR, "Failed to allocate UAR.");
85 mlx5_vdpa_event_qp_global_release(priv);
90 mlx5_vdpa_cq_destroy(struct mlx5_vdpa_cq *cq)
93 claim_zero(mlx5_devx_cmd_destroy(cq->cq));
95 claim_zero(mlx5_glue->devx_umem_dereg(cq->umem_obj));
97 rte_free((void *)(uintptr_t)cq->umem_buf);
98 memset(cq, 0, sizeof(*cq));
101 static inline void __rte_unused
102 mlx5_vdpa_cq_arm(struct mlx5_vdpa_priv *priv, struct mlx5_vdpa_cq *cq)
104 uint32_t arm_sn = cq->arm_sn << MLX5_CQ_SQN_OFFSET;
105 uint32_t cq_ci = cq->cq_ci & MLX5_CI_MASK;
106 uint32_t doorbell_hi = arm_sn | MLX5_CQ_DBR_CMD_ALL | cq_ci;
107 uint64_t doorbell = ((uint64_t)doorbell_hi << 32) | cq->cq->id;
108 uint64_t db_be = rte_cpu_to_be_64(doorbell);
109 uint32_t *addr = RTE_PTR_ADD(priv->uar->base_addr, MLX5_CQ_DOORBELL);
112 cq->db_rec[MLX5_CQ_ARM_DB] = rte_cpu_to_be_32(doorbell_hi);
115 *(uint64_t *)addr = db_be;
117 *(uint32_t *)addr = db_be;
119 *((uint32_t *)addr + 1) = db_be >> 32;
126 mlx5_vdpa_cq_create(struct mlx5_vdpa_priv *priv, uint16_t log_desc_n,
127 int callfd, struct mlx5_vdpa_cq *cq)
129 struct mlx5_devx_cq_attr attr = {0};
130 size_t pgsize = sysconf(_SC_PAGESIZE);
132 uint16_t event_nums[1] = {0};
133 uint16_t cq_size = 1 << log_desc_n;
136 cq->log_desc_n = log_desc_n;
137 umem_size = sizeof(struct mlx5_cqe) * cq_size + sizeof(*cq->db_rec) * 2;
138 cq->umem_buf = rte_zmalloc(__func__, umem_size, 4096);
140 DRV_LOG(ERR, "Failed to allocate memory for CQ.");
144 cq->umem_obj = mlx5_glue->devx_umem_reg(priv->ctx,
145 (void *)(uintptr_t)cq->umem_buf,
147 IBV_ACCESS_LOCAL_WRITE);
149 DRV_LOG(ERR, "Failed to register umem for CQ.");
152 attr.q_umem_valid = 1;
153 attr.db_umem_valid = 1;
154 attr.use_first_only = 1;
155 attr.overrun_ignore = 0;
156 attr.uar_page_id = priv->uar->page_id;
157 attr.q_umem_id = cq->umem_obj->umem_id;
158 attr.q_umem_offset = 0;
159 attr.db_umem_id = cq->umem_obj->umem_id;
160 attr.db_umem_offset = sizeof(struct mlx5_cqe) * cq_size;
161 attr.eqn = priv->eqn;
162 attr.log_cq_size = log_desc_n;
163 attr.log_page_size = rte_log2_u32(pgsize);
164 cq->cq = mlx5_devx_cmd_create_cq(priv->ctx, &attr);
167 cq->db_rec = RTE_PTR_ADD(cq->umem_buf, (uintptr_t)attr.db_umem_offset);
169 rte_spinlock_init(&cq->sl);
170 /* Subscribe CQ event to the event channel controlled by the driver. */
171 ret = mlx5_glue->devx_subscribe_devx_event(priv->eventc, cq->cq->obj,
174 (uint64_t)(uintptr_t)cq);
176 DRV_LOG(ERR, "Failed to subscribe CQE event.");
181 /* Init CQ to ones to be in HW owner in the start. */
182 cq->cqes[0].op_own = MLX5_CQE_OWNER_MASK;
183 cq->cqes[0].wqe_counter = rte_cpu_to_be_16(cq_size - 1);
185 mlx5_vdpa_cq_arm(priv, cq);
188 mlx5_vdpa_cq_destroy(cq);
192 static inline uint32_t
193 mlx5_vdpa_cq_poll(struct mlx5_vdpa_cq *cq)
195 struct mlx5_vdpa_event_qp *eqp =
196 container_of(cq, struct mlx5_vdpa_event_qp, cq);
197 const unsigned int cq_size = 1 << cq->log_desc_n;
198 const unsigned int cq_mask = cq_size - 1;
201 uint16_t wqe_counter;
207 uint16_t next_wqe_counter = cq->cq_ci & cq_mask;
208 uint16_t cur_wqe_counter;
211 last_word.word = rte_read32(&cq->cqes[0].wqe_counter);
212 cur_wqe_counter = rte_be_to_cpu_16(last_word.wqe_counter);
213 comp = (cur_wqe_counter + 1u - next_wqe_counter) & cq_mask;
216 MLX5_ASSERT(MLX5_CQE_OPCODE(last_word.op_own) !=
218 if (unlikely(!(MLX5_CQE_OPCODE(last_word.op_own) ==
220 MLX5_CQE_OPCODE(last_word.op_own) ==
224 /* Ring CQ doorbell record. */
225 cq->db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
227 /* Ring SW QP doorbell record. */
228 eqp->db_rec[0] = rte_cpu_to_be_32(cq->cq_ci + cq_size);
234 mlx5_vdpa_arm_all_cqs(struct mlx5_vdpa_priv *priv)
236 struct mlx5_vdpa_cq *cq;
239 for (i = 0; i < priv->nr_virtqs; i++) {
240 cq = &priv->virtqs[i].eqp.cq;
241 if (cq->cq && !cq->armed)
242 mlx5_vdpa_cq_arm(priv, cq);
247 mlx5_vdpa_timer_sleep(struct mlx5_vdpa_priv *priv, uint32_t max)
249 if (priv->event_mode == MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER) {
252 priv->timer_delay_us += priv->event_us;
257 priv->timer_delay_us /= max;
261 usleep(priv->timer_delay_us);
265 mlx5_vdpa_poll_handle(void *arg)
267 struct mlx5_vdpa_priv *priv = arg;
269 struct mlx5_vdpa_cq *cq;
271 uint64_t current_tic;
273 pthread_mutex_lock(&priv->timer_lock);
274 while (!priv->timer_on)
275 pthread_cond_wait(&priv->timer_cond, &priv->timer_lock);
276 pthread_mutex_unlock(&priv->timer_lock);
277 priv->timer_delay_us = priv->event_mode ==
278 MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER ?
279 MLX5_VDPA_DEFAULT_TIMER_DELAY_US :
283 pthread_mutex_lock(&priv->vq_config_lock);
284 for (i = 0; i < priv->nr_virtqs; i++) {
285 cq = &priv->virtqs[i].eqp.cq;
286 if (cq->cq && !cq->armed) {
287 uint32_t comp = mlx5_vdpa_cq_poll(cq);
290 /* Notify guest for descs consuming. */
291 if (cq->callfd != -1)
292 eventfd_write(cq->callfd,
299 current_tic = rte_rdtsc();
301 /* No traffic ? stop timer and load interrupts. */
302 if (current_tic - priv->last_traffic_tic >=
303 rte_get_timer_hz() * priv->no_traffic_time_s) {
304 DRV_LOG(DEBUG, "Device %s traffic was stopped.",
305 priv->vdev->device->name);
306 mlx5_vdpa_arm_all_cqs(priv);
307 pthread_mutex_unlock(&priv->vq_config_lock);
308 pthread_mutex_lock(&priv->timer_lock);
310 while (!priv->timer_on)
311 pthread_cond_wait(&priv->timer_cond,
313 pthread_mutex_unlock(&priv->timer_lock);
314 priv->timer_delay_us = priv->event_mode ==
315 MLX5_VDPA_EVENT_MODE_DYNAMIC_TIMER ?
316 MLX5_VDPA_DEFAULT_TIMER_DELAY_US :
321 priv->last_traffic_tic = current_tic;
323 pthread_mutex_unlock(&priv->vq_config_lock);
324 mlx5_vdpa_timer_sleep(priv, max);
330 mlx5_vdpa_interrupt_handler(void *cb_arg)
332 struct mlx5_vdpa_priv *priv = cb_arg;
333 #ifdef HAVE_IBV_DEVX_EVENT
335 struct mlx5dv_devx_async_event_hdr event_resp;
336 uint8_t buf[sizeof(struct mlx5dv_devx_async_event_hdr) + 128];
339 pthread_mutex_lock(&priv->vq_config_lock);
340 while (mlx5_glue->devx_get_event(priv->eventc, &out.event_resp,
342 (ssize_t)sizeof(out.event_resp.cookie)) {
343 struct mlx5_vdpa_cq *cq = (struct mlx5_vdpa_cq *)
344 (uintptr_t)out.event_resp.cookie;
345 struct mlx5_vdpa_event_qp *eqp = container_of(cq,
346 struct mlx5_vdpa_event_qp, cq);
347 struct mlx5_vdpa_virtq *virtq = container_of(eqp,
348 struct mlx5_vdpa_virtq, eqp);
352 mlx5_vdpa_cq_poll(cq);
353 /* Notify guest for descs consuming. */
354 if (cq->callfd != -1)
355 eventfd_write(cq->callfd, (eventfd_t)1);
356 if (priv->event_mode == MLX5_VDPA_EVENT_MODE_ONLY_INTERRUPT) {
357 mlx5_vdpa_cq_arm(priv, cq);
358 pthread_mutex_unlock(&priv->vq_config_lock);
361 /* Don't arm again - timer will take control. */
362 DRV_LOG(DEBUG, "Device %s virtq %d cq %d event was captured."
363 " Timer is %s, cq ci is %u.\n",
364 priv->vdev->device->name,
365 (int)virtq->index, cq->cq->id,
366 priv->timer_on ? "on" : "off", cq->cq_ci);
371 /* Traffic detected: make sure timer is on. */
372 priv->last_traffic_tic = rte_rdtsc();
373 pthread_mutex_lock(&priv->timer_lock);
374 if (!priv->timer_on) {
376 pthread_cond_signal(&priv->timer_cond);
378 pthread_mutex_unlock(&priv->timer_lock);
379 pthread_mutex_unlock(&priv->vq_config_lock);
383 mlx5_vdpa_cqe_event_setup(struct mlx5_vdpa_priv *priv)
388 /* All virtqs are in poll mode. */
390 if (priv->event_mode != MLX5_VDPA_EVENT_MODE_ONLY_INTERRUPT) {
391 pthread_mutex_init(&priv->timer_lock, NULL);
392 pthread_cond_init(&priv->timer_cond, NULL);
394 ret = pthread_create(&priv->timer_tid, NULL,
395 mlx5_vdpa_poll_handle, (void *)priv);
397 DRV_LOG(ERR, "Failed to create timer thread.");
401 priv->intr_handle.fd = priv->eventc->fd;
402 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
403 if (rte_intr_callback_register(&priv->intr_handle,
404 mlx5_vdpa_interrupt_handler, priv)) {
405 priv->intr_handle.fd = 0;
406 DRV_LOG(ERR, "Failed to register CQE interrupt %d.", rte_errno);
411 mlx5_vdpa_cqe_event_unset(priv);
416 mlx5_vdpa_cqe_event_unset(struct mlx5_vdpa_priv *priv)
418 int retries = MLX5_VDPA_INTR_RETRIES;
422 if (priv->intr_handle.fd) {
423 while (retries-- && ret == -EAGAIN) {
424 ret = rte_intr_callback_unregister(&priv->intr_handle,
425 mlx5_vdpa_interrupt_handler,
427 if (ret == -EAGAIN) {
428 DRV_LOG(DEBUG, "Try again to unregister fd %d "
429 "of CQ interrupt, retries = %d.",
430 priv->intr_handle.fd, retries);
434 memset(&priv->intr_handle, 0, sizeof(priv->intr_handle));
436 if (priv->timer_tid) {
437 pthread_cancel(priv->timer_tid);
438 pthread_join(priv->timer_tid, &status);
444 mlx5_vdpa_event_qp_destroy(struct mlx5_vdpa_event_qp *eqp)
447 claim_zero(mlx5_devx_cmd_destroy(eqp->sw_qp));
449 claim_zero(mlx5_glue->devx_umem_dereg(eqp->umem_obj));
451 rte_free(eqp->umem_buf);
453 claim_zero(mlx5_devx_cmd_destroy(eqp->fw_qp));
454 mlx5_vdpa_cq_destroy(&eqp->cq);
455 memset(eqp, 0, sizeof(*eqp));
459 mlx5_vdpa_qps2rts(struct mlx5_vdpa_event_qp *eqp)
461 if (mlx5_devx_cmd_modify_qp_state(eqp->fw_qp, MLX5_CMD_OP_RST2INIT_QP,
463 DRV_LOG(ERR, "Failed to modify FW QP to INIT state(%u).",
467 if (mlx5_devx_cmd_modify_qp_state(eqp->sw_qp, MLX5_CMD_OP_RST2INIT_QP,
469 DRV_LOG(ERR, "Failed to modify SW QP to INIT state(%u).",
473 if (mlx5_devx_cmd_modify_qp_state(eqp->fw_qp, MLX5_CMD_OP_INIT2RTR_QP,
475 DRV_LOG(ERR, "Failed to modify FW QP to RTR state(%u).",
479 if (mlx5_devx_cmd_modify_qp_state(eqp->sw_qp, MLX5_CMD_OP_INIT2RTR_QP,
481 DRV_LOG(ERR, "Failed to modify SW QP to RTR state(%u).",
485 if (mlx5_devx_cmd_modify_qp_state(eqp->fw_qp, MLX5_CMD_OP_RTR2RTS_QP,
487 DRV_LOG(ERR, "Failed to modify FW QP to RTS state(%u).",
491 if (mlx5_devx_cmd_modify_qp_state(eqp->sw_qp, MLX5_CMD_OP_RTR2RTS_QP,
493 DRV_LOG(ERR, "Failed to modify SW QP to RTS state(%u).",
501 mlx5_vdpa_event_qp_create(struct mlx5_vdpa_priv *priv, uint16_t desc_n,
502 int callfd, struct mlx5_vdpa_event_qp *eqp)
504 struct mlx5_devx_qp_attr attr = {0};
505 uint16_t log_desc_n = rte_log2_u32(desc_n);
506 uint32_t umem_size = (1 << log_desc_n) * MLX5_WSEG_SIZE +
507 sizeof(*eqp->db_rec) * 2;
509 if (mlx5_vdpa_event_qp_global_prepare(priv))
511 if (mlx5_vdpa_cq_create(priv, log_desc_n, callfd, &eqp->cq))
514 eqp->fw_qp = mlx5_devx_cmd_create_qp(priv->ctx, &attr);
516 DRV_LOG(ERR, "Failed to create FW QP(%u).", rte_errno);
519 eqp->umem_buf = rte_zmalloc(__func__, umem_size, 4096);
520 if (!eqp->umem_buf) {
521 DRV_LOG(ERR, "Failed to allocate memory for SW QP.");
525 eqp->umem_obj = mlx5_glue->devx_umem_reg(priv->ctx,
526 (void *)(uintptr_t)eqp->umem_buf,
528 IBV_ACCESS_LOCAL_WRITE);
529 if (!eqp->umem_obj) {
530 DRV_LOG(ERR, "Failed to register umem for SW QP.");
533 attr.uar_index = priv->uar->page_id;
534 attr.cqn = eqp->cq.cq->id;
535 attr.log_page_size = rte_log2_u32(sysconf(_SC_PAGESIZE));
536 attr.rq_size = 1 << log_desc_n;
537 attr.log_rq_stride = rte_log2_u32(MLX5_WSEG_SIZE);
538 attr.sq_size = 0; /* No need SQ. */
539 attr.dbr_umem_valid = 1;
540 attr.wq_umem_id = eqp->umem_obj->umem_id;
541 attr.wq_umem_offset = 0;
542 attr.dbr_umem_id = eqp->umem_obj->umem_id;
543 attr.dbr_address = (1 << log_desc_n) * MLX5_WSEG_SIZE;
544 eqp->sw_qp = mlx5_devx_cmd_create_qp(priv->ctx, &attr);
546 DRV_LOG(ERR, "Failed to create SW QP(%u).", rte_errno);
549 eqp->db_rec = RTE_PTR_ADD(eqp->umem_buf, (uintptr_t)attr.dbr_address);
550 if (mlx5_vdpa_qps2rts(eqp))
553 rte_write32(rte_cpu_to_be_32(1 << log_desc_n), &eqp->db_rec[0]);
556 mlx5_vdpa_event_qp_destroy(eqp);