1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
6 #include <rte_cycles.h>
7 #include <rte_eal_paging.h>
9 #include <mlx5_malloc.h>
10 #include <mlx5_common_os.h>
11 #include <mlx5_common_devx.h>
14 #include "mlx5_flow.h"
18 * Destroy Completion Queue used for ASO access.
24 mlx5_aso_cq_destroy(struct mlx5_aso_cq *cq)
27 mlx5_devx_cq_destroy(&cq->cq_obj);
28 memset(cq, 0, sizeof(*cq));
32 * Create Completion Queue used for ASO access.
35 * Context returned from mlx5 open_device() glue function.
37 * Pointer to CQ to create.
38 * @param[in] log_desc_n
39 * Log of number of descriptors in queue.
41 * Socket to use for allocation.
42 * @param[in] uar_page_id
46 * 0 on success, a negative errno value otherwise and rte_errno is set.
49 mlx5_aso_cq_create(void *ctx, struct mlx5_aso_cq *cq, uint16_t log_desc_n,
50 int socket, int uar_page_id)
52 struct mlx5_devx_cq_attr attr = {
53 .uar_page_id = uar_page_id,
56 cq->log_desc_n = log_desc_n;
58 return mlx5_devx_cq_create(ctx, &cq->cq_obj, log_desc_n, &attr, socket);
68 mlx5_aso_devx_dereg_mr(struct mlx5_aso_devx_mr *mr)
70 claim_zero(mlx5_devx_cmd_destroy(mr->mkey));
71 if (!mr->is_indirect && mr->umem)
72 claim_zero(mlx5_glue->devx_umem_dereg(mr->umem));
74 memset(mr, 0, sizeof(*mr));
78 * Register Memory Region.
81 * Context returned from mlx5 open_device() glue function.
85 * Pointer to MR to create.
87 * Socket to use for allocation.
89 * Protection Domain number to use.
92 * 0 on success, a negative errno value otherwise and rte_errno is set.
95 mlx5_aso_devx_reg_mr(void *ctx, size_t length, struct mlx5_aso_devx_mr *mr,
98 struct mlx5_devx_mkey_attr mkey_attr;
100 mr->buf = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, length, 4096,
103 DRV_LOG(ERR, "Failed to create ASO bits mem for MR by Devx.");
106 mr->umem = mlx5_os_umem_reg(ctx, mr->buf, length,
107 IBV_ACCESS_LOCAL_WRITE);
109 DRV_LOG(ERR, "Failed to register Umem for MR by Devx.");
112 mkey_attr.addr = (uintptr_t)mr->buf;
113 mkey_attr.size = length;
114 mkey_attr.umem_id = mlx5_os_get_umem_id(mr->umem);
116 mkey_attr.pg_access = 1;
117 mkey_attr.klm_array = NULL;
118 mkey_attr.klm_num = 0;
119 mkey_attr.relaxed_ordering_read = 0;
120 mkey_attr.relaxed_ordering_write = 0;
121 mr->mkey = mlx5_devx_cmd_mkey_create(ctx, &mkey_attr);
123 DRV_LOG(ERR, "Failed to create direct Mkey.");
127 mr->is_indirect = false;
131 claim_zero(mlx5_glue->devx_umem_dereg(mr->umem));
137 * Destroy Send Queue used for ASO access.
143 mlx5_aso_destroy_sq(struct mlx5_aso_sq *sq)
145 mlx5_devx_sq_destroy(&sq->sq_obj);
146 mlx5_aso_cq_destroy(&sq->cq);
147 mlx5_aso_devx_dereg_mr(&sq->mr);
148 memset(sq, 0, sizeof(*sq));
152 * Initialize Send Queue used for ASO access.
155 * ASO SQ to initialize.
158 mlx5_aso_init_sq(struct mlx5_aso_sq *sq)
160 volatile struct mlx5_aso_wqe *restrict wqe;
162 int size = 1 << sq->log_desc_n;
165 /* All the next fields state should stay constant. */
166 for (i = 0, wqe = &sq->sq_obj.aso_wqes[0]; i < size; ++i, ++wqe) {
167 wqe->general_cseg.sq_ds = rte_cpu_to_be_32((sq->sqn << 8) |
168 (sizeof(*wqe) >> 4));
169 wqe->aso_cseg.lkey = rte_cpu_to_be_32(sq->mr.mkey->id);
170 addr = (uint64_t)((uint64_t *)sq->mr.buf + i *
171 MLX5_ASO_AGE_ACTIONS_PER_POOL / 64);
172 wqe->aso_cseg.va_h = rte_cpu_to_be_32((uint32_t)(addr >> 32));
173 wqe->aso_cseg.va_l_r = rte_cpu_to_be_32((uint32_t)addr | 1u);
174 wqe->aso_cseg.operand_masks = rte_cpu_to_be_32
176 (ASO_OPER_LOGICAL_OR << ASO_CSEG_COND_OPER_OFFSET) |
177 (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_1_OPER_OFFSET) |
178 (ASO_OP_ALWAYS_TRUE << ASO_CSEG_COND_0_OPER_OFFSET) |
179 (BYTEWISE_64BYTE << ASO_CSEG_DATA_MASK_MODE_OFFSET));
180 wqe->aso_cseg.data_mask = RTE_BE64(UINT64_MAX);
185 * Create Send Queue used for ASO access.
188 * Context returned from mlx5 open_device() glue function.
190 * Pointer to SQ to create.
192 * Socket to use for allocation.
194 * User Access Region object.
196 * Protection Domain number to use.
197 * @param[in] log_desc_n
198 * Log of number of descriptors in queue.
201 * 0 on success, a negative errno value otherwise and rte_errno is set.
204 mlx5_aso_sq_create(void *ctx, struct mlx5_aso_sq *sq, int socket,
205 void *uar, uint32_t pdn, uint16_t log_desc_n,
208 struct mlx5_devx_create_sq_attr attr = {
209 .user_index = 0xFFFF,
210 .wq_attr = (struct mlx5_devx_wq_attr){
212 .uar_page = mlx5_os_get_devx_uar_page_id(uar),
214 .ts_format = mlx5_ts_format_conv(ts_format),
216 struct mlx5_devx_modify_sq_attr modify_attr = {
217 .state = MLX5_SQC_STATE_RDY,
219 uint32_t sq_desc_n = 1 << log_desc_n;
223 if (mlx5_aso_devx_reg_mr(ctx, (MLX5_ASO_AGE_ACTIONS_PER_POOL / 8) *
224 sq_desc_n, &sq->mr, socket, pdn))
226 if (mlx5_aso_cq_create(ctx, &sq->cq, log_desc_n, socket,
227 mlx5_os_get_devx_uar_page_id(uar)))
229 sq->log_desc_n = log_desc_n;
230 attr.cqn = sq->cq.cq_obj.cq->id;
231 /* for mlx5_aso_wqe that is twice the size of mlx5_wqe */
232 log_wqbb_n = log_desc_n + 1;
233 ret = mlx5_devx_sq_create(ctx, &sq->sq_obj, log_wqbb_n, &attr, socket);
235 DRV_LOG(ERR, "Can't create SQ object.");
239 ret = mlx5_devx_cmd_modify_sq(sq->sq_obj.sq, &modify_attr);
241 DRV_LOG(ERR, "Can't change SQ state to ready.");
248 sq->sqn = sq->sq_obj.sq->id;
249 sq->uar_addr = mlx5_os_get_devx_uar_reg_addr(uar);
250 mlx5_aso_init_sq(sq);
253 mlx5_aso_destroy_sq(sq);
258 * API to create and initialize Send Queue used for ASO access.
261 * Pointer to shared device context.
264 * 0 on success, a negative errno value otherwise and rte_errno is set.
267 mlx5_aso_queue_init(struct mlx5_dev_ctx_shared *sh)
269 return mlx5_aso_sq_create(sh->ctx, &sh->aso_age_mng->aso_sq, 0,
270 sh->tx_uar, sh->pdn, MLX5_ASO_QUEUE_LOG_DESC,
275 * API to destroy Send Queue used for ASO access.
278 * Pointer to shared device context.
281 mlx5_aso_queue_uninit(struct mlx5_dev_ctx_shared *sh)
283 mlx5_aso_destroy_sq(&sh->aso_age_mng->aso_sq);
287 * Write a burst of WQEs to ASO SQ.
290 * ASO management data, contains the SQ.
292 * Index of the last valid pool.
295 * Number of WQEs in burst.
298 mlx5_aso_sq_enqueue_burst(struct mlx5_aso_age_mng *mng, uint16_t n)
300 volatile struct mlx5_aso_wqe *wqe;
301 struct mlx5_aso_sq *sq = &mng->aso_sq;
302 struct mlx5_aso_age_pool *pool;
303 uint16_t size = 1 << sq->log_desc_n;
304 uint16_t mask = size - 1;
306 uint16_t start_head = sq->head;
308 max = RTE_MIN(size - (uint16_t)(sq->head - sq->tail), n - sq->next);
311 sq->elts[start_head & mask].burst_size = max;
313 wqe = &sq->sq_obj.aso_wqes[sq->head & mask];
314 rte_prefetch0(&sq->sq_obj.aso_wqes[(sq->head + 1) & mask]);
316 rte_spinlock_lock(&mng->resize_sl);
317 pool = mng->pools[sq->next];
318 rte_spinlock_unlock(&mng->resize_sl);
319 sq->elts[sq->head & mask].pool = pool;
320 wqe->general_cseg.misc =
321 rte_cpu_to_be_32(((struct mlx5_devx_obj *)
322 (pool->flow_hit_aso_obj))->id);
323 wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ONLY_FIRST_ERR <<
324 MLX5_COMP_MODE_OFFSET);
325 wqe->general_cseg.opcode = rte_cpu_to_be_32
326 (MLX5_OPCODE_ACCESS_ASO |
327 (ASO_OPC_MOD_FLOW_HIT <<
328 WQE_CSEG_OPC_MOD_OFFSET) |
330 WQE_CSEG_WQE_INDEX_OFFSET));
331 sq->pi += 2; /* Each WQE contains 2 WQEBB's. */
336 wqe->general_cseg.flags = RTE_BE32(MLX5_COMP_ALWAYS <<
337 MLX5_COMP_MODE_OFFSET);
339 sq->sq_obj.db_rec[MLX5_SND_DBR] = rte_cpu_to_be_32(sq->pi);
341 *sq->uar_addr = *(volatile uint64_t *)wqe; /* Assume 64 bit ARCH.*/
343 return sq->elts[start_head & mask].burst_size;
347 * Debug utility function. Dump contents of error CQE and WQE.
355 mlx5_aso_dump_err_objs(volatile uint32_t *cqe, volatile uint32_t *wqe)
359 DRV_LOG(ERR, "Error cqe:");
360 for (i = 0; i < 16; i += 4)
361 DRV_LOG(ERR, "%08X %08X %08X %08X", cqe[i], cqe[i + 1],
362 cqe[i + 2], cqe[i + 3]);
363 DRV_LOG(ERR, "\nError wqe:");
364 for (i = 0; i < (int)sizeof(struct mlx5_aso_wqe) / 4; i += 4)
365 DRV_LOG(ERR, "%08X %08X %08X %08X", wqe[i], wqe[i + 1],
366 wqe[i + 2], wqe[i + 3]);
370 * Handle case of error CQE.
376 mlx5_aso_cqe_err_handle(struct mlx5_aso_sq *sq)
378 struct mlx5_aso_cq *cq = &sq->cq;
379 uint32_t idx = cq->cq_ci & ((1 << cq->log_desc_n) - 1);
380 volatile struct mlx5_err_cqe *cqe =
381 (volatile struct mlx5_err_cqe *)&cq->cq_obj.cqes[idx];
384 idx = rte_be_to_cpu_16(cqe->wqe_counter) & (1u << sq->log_desc_n);
385 mlx5_aso_dump_err_objs((volatile uint32_t *)cqe,
386 (volatile uint32_t *)&sq->sq_obj.aso_wqes[idx]);
390 * Update ASO objects upon completion.
393 * Shared device context.
395 * Number of completed ASO objects.
398 mlx5_aso_age_action_update(struct mlx5_dev_ctx_shared *sh, uint16_t n)
400 struct mlx5_aso_age_mng *mng = sh->aso_age_mng;
401 struct mlx5_aso_sq *sq = &mng->aso_sq;
402 struct mlx5_age_info *age_info;
403 const uint16_t size = 1 << sq->log_desc_n;
404 const uint16_t mask = size - 1;
405 const uint64_t curr = MLX5_CURR_TIME_SEC;
406 uint16_t expected = AGE_CANDIDATE;
409 for (i = 0; i < n; ++i) {
410 uint16_t idx = (sq->tail + i) & mask;
411 struct mlx5_aso_age_pool *pool = sq->elts[idx].pool;
412 uint64_t diff = curr - pool->time_of_last_age_check;
413 uint64_t *addr = sq->mr.buf;
416 addr += idx * MLX5_ASO_AGE_ACTIONS_PER_POOL / 64;
417 pool->time_of_last_age_check = curr;
418 for (j = 0; j < MLX5_ASO_AGE_ACTIONS_PER_POOL; j++) {
419 struct mlx5_aso_age_action *act = &pool->actions[j];
420 struct mlx5_age_param *ap = &act->age_params;
426 if (__atomic_load_n(&ap->state, __ATOMIC_RELAXED) !=
431 u8addr = (uint8_t *)addr;
432 hit = (u8addr[byte] >> offset) & 0x1;
434 __atomic_store_n(&ap->sec_since_last_hit, 0,
437 struct mlx5_priv *priv;
439 __atomic_fetch_add(&ap->sec_since_last_hit,
440 diff, __ATOMIC_RELAXED);
441 /* If timeout passed add to aged-out list. */
442 if (ap->sec_since_last_hit <= ap->timeout)
445 rte_eth_devices[ap->port_id].data->dev_private;
446 age_info = GET_PORT_AGE_INFO(priv);
447 rte_spinlock_lock(&age_info->aged_sl);
448 if (__atomic_compare_exchange_n(&ap->state,
454 LIST_INSERT_HEAD(&age_info->aged_aso,
456 MLX5_AGE_SET(age_info,
459 rte_spinlock_unlock(&age_info->aged_sl);
463 mlx5_age_event_prepare(sh);
467 * Handle completions from WQEs sent to ASO SQ.
470 * Shared device context.
473 * Number of CQEs handled.
476 mlx5_aso_completion_handle(struct mlx5_dev_ctx_shared *sh)
478 struct mlx5_aso_age_mng *mng = sh->aso_age_mng;
479 struct mlx5_aso_sq *sq = &mng->aso_sq;
480 struct mlx5_aso_cq *cq = &sq->cq;
481 volatile struct mlx5_cqe *restrict cqe;
482 const unsigned int cq_size = 1 << cq->log_desc_n;
483 const unsigned int mask = cq_size - 1;
485 uint32_t next_idx = cq->cq_ci & mask;
486 const uint16_t max = (uint16_t)(sq->head - sq->tail);
493 next_idx = (cq->cq_ci + 1) & mask;
494 rte_prefetch0(&cq->cq_obj.cqes[next_idx]);
495 cqe = &cq->cq_obj.cqes[idx];
496 ret = check_cqe(cqe, cq_size, cq->cq_ci);
498 * Be sure owner read is done before any other cookie field or
502 if (unlikely(ret != MLX5_CQE_STATUS_SW_OWN)) {
503 if (likely(ret == MLX5_CQE_STATUS_HW_OWN))
505 mlx5_aso_cqe_err_handle(sq);
507 i += sq->elts[(sq->tail + i) & mask].burst_size;
512 mlx5_aso_age_action_update(sh, i);
515 cq->cq_obj.db_rec[0] = rte_cpu_to_be_32(cq->cq_ci);
521 * Periodically read CQEs and send WQEs to ASO SQ.
524 * Shared device context containing the ASO SQ.
527 mlx5_flow_aso_alarm(void *arg)
529 struct mlx5_dev_ctx_shared *sh = arg;
530 struct mlx5_aso_sq *sq = &sh->aso_age_mng->aso_sq;
534 rte_spinlock_lock(&sh->aso_age_mng->resize_sl);
535 n = sh->aso_age_mng->next;
536 rte_spinlock_unlock(&sh->aso_age_mng->resize_sl);
537 mlx5_aso_completion_handle(sh);
539 /* End of loop: wait 1 second. */
543 mlx5_aso_sq_enqueue_burst(sh->aso_age_mng, n);
544 if (rte_eal_alarm_set(us, mlx5_flow_aso_alarm, sh))
545 DRV_LOG(ERR, "Cannot reinitialize aso alarm.");
549 * API to start ASO access using ASO SQ.
552 * Pointer to shared device context.
555 * 0 on success, a negative errno value otherwise and rte_errno is set.
558 mlx5_aso_queue_start(struct mlx5_dev_ctx_shared *sh)
560 if (rte_eal_alarm_set(US_PER_S, mlx5_flow_aso_alarm, sh)) {
561 DRV_LOG(ERR, "Cannot reinitialize ASO age alarm.");
568 * API to stop ASO access using ASO SQ.
571 * Pointer to shared device context.
574 * 0 on success, a negative errno value otherwise and rte_errno is set.
577 mlx5_aso_queue_stop(struct mlx5_dev_ctx_shared *sh)
581 if (!sh->aso_age_mng->aso_sq.sq_obj.sq)
585 rte_eal_alarm_cancel(mlx5_flow_aso_alarm, sh);
586 if (rte_errno != EINPROGRESS)