1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2021 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
9 #include <rte_spinlock.h>
11 #include <rte_compressdev.h>
12 #include <rte_compressdev_pmd.h>
14 #include <mlx5_glue.h>
15 #include <mlx5_common.h>
16 #include <mlx5_common_pci.h>
17 #include <mlx5_devx_cmds.h>
18 #include <mlx5_common_os.h>
19 #include <mlx5_common_devx.h>
20 #include <mlx5_common_mr.h>
23 #include "mlx5_compress_utils.h"
25 #define MLX5_COMPRESS_DRIVER_NAME mlx5_compress
26 #define MLX5_COMPRESS_LOG_NAME pmd.compress.mlx5
27 #define MLX5_COMPRESS_MAX_QPS 1024
28 #define MLX5_COMP_MAX_WIN_SIZE_CONF 6u
30 struct mlx5_compress_xform {
31 LIST_ENTRY(mlx5_compress_xform) next;
32 enum rte_comp_xform_type type;
33 enum rte_comp_checksum_type csum_type;
35 uint32_t gga_ctrl1; /* BE. */
38 struct mlx5_compress_priv {
39 TAILQ_ENTRY(mlx5_compress_priv) next;
40 struct ibv_context *ctx; /* Device context. */
41 struct rte_pci_device *pci_dev;
42 struct rte_compressdev *cdev;
44 uint32_t pdn; /* Protection Domain number. */
45 uint8_t min_block_size;
46 /* Minimum huffman block size supported by the device. */
48 struct rte_compressdev_config dev_config;
49 LIST_HEAD(xform_list, mlx5_compress_xform) xform_list;
50 rte_spinlock_t xform_sl;
51 struct mlx5_mr_share_cache mr_scache; /* Global shared MR cache. */
54 struct mlx5_compress_qp {
59 volatile uint64_t *uar_addr;
60 struct mlx5_mr_ctrl mr_ctrl;
62 struct mlx5_devx_cq cq;
63 struct mlx5_devx_sq sq;
64 struct mlx5_pmd_mr opaque_mr;
65 struct rte_comp_op **ops;
66 struct mlx5_compress_priv *priv;
69 TAILQ_HEAD(mlx5_compress_privs, mlx5_compress_priv) mlx5_compress_priv_list =
70 TAILQ_HEAD_INITIALIZER(mlx5_compress_priv_list);
71 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
73 int mlx5_compress_logtype;
75 const struct rte_compressdev_capabilities mlx5_caps[RTE_COMP_ALGO_LIST_END];
79 mlx5_compress_dev_info_get(struct rte_compressdev *dev,
80 struct rte_compressdev_info *info)
84 info->max_nb_queue_pairs = MLX5_COMPRESS_MAX_QPS;
85 info->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
86 info->capabilities = mlx5_caps;
91 mlx5_compress_dev_configure(struct rte_compressdev *dev,
92 struct rte_compressdev_config *config)
94 struct mlx5_compress_priv *priv;
96 if (dev == NULL || config == NULL)
98 priv = dev->data->dev_private;
99 priv->dev_config = *config;
104 mlx5_compress_dev_close(struct rte_compressdev *dev)
111 mlx5_compress_qp_release(struct rte_compressdev *dev, uint16_t qp_id)
113 struct mlx5_compress_qp *qp = dev->data->queue_pairs[qp_id];
115 if (qp->sq.sq != NULL)
116 mlx5_devx_sq_destroy(&qp->sq);
117 if (qp->cq.cq != NULL)
118 mlx5_devx_cq_destroy(&qp->cq);
119 if (qp->opaque_mr.obj != NULL) {
120 void *opaq = qp->opaque_mr.addr;
122 mlx5_common_verbs_dereg_mr(&qp->opaque_mr);
126 mlx5_mr_btree_free(&qp->mr_ctrl.cache_bh);
128 dev->data->queue_pairs[qp_id] = NULL;
133 mlx5_compress_init_sq(struct mlx5_compress_qp *qp)
135 volatile struct mlx5_gga_wqe *restrict wqe =
136 (volatile struct mlx5_gga_wqe *)qp->sq.wqes;
137 volatile struct mlx5_gga_compress_opaque *opaq = qp->opaque_mr.addr;
138 const uint32_t sq_ds = rte_cpu_to_be_32((qp->sq.sq->id << 8) | 4u);
139 const uint32_t flags = RTE_BE32(MLX5_COMP_ALWAYS <<
140 MLX5_COMP_MODE_OFFSET);
141 const uint32_t opaq_lkey = rte_cpu_to_be_32(qp->opaque_mr.lkey);
144 /* All the next fields state should stay constant. */
145 for (i = 0; i < qp->entries_n; ++i, ++wqe) {
148 wqe->opaque_lkey = opaq_lkey;
149 wqe->opaque_vaddr = rte_cpu_to_be_64
150 ((uint64_t)(uintptr_t)&opaq[i]);
155 mlx5_compress_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
156 uint32_t max_inflight_ops, int socket_id)
158 struct mlx5_compress_priv *priv = dev->data->dev_private;
159 struct mlx5_compress_qp *qp;
160 struct mlx5_devx_cq_attr cq_attr = {
161 .uar_page_id = mlx5_os_get_devx_uar_page_id(priv->uar),
163 struct mlx5_devx_create_sq_attr sq_attr = {
165 .wq_attr = (struct mlx5_devx_wq_attr){
167 .uar_page = mlx5_os_get_devx_uar_page_id(priv->uar),
170 struct mlx5_devx_modify_sq_attr modify_attr = {
171 .state = MLX5_SQC_STATE_RDY,
173 uint32_t log_ops_n = rte_log2_u32(max_inflight_ops);
174 uint32_t alloc_size = sizeof(*qp);
178 alloc_size = RTE_ALIGN(alloc_size, RTE_CACHE_LINE_SIZE);
179 alloc_size += sizeof(struct rte_comp_op *) * (1u << log_ops_n);
180 qp = rte_zmalloc_socket(__func__, alloc_size, RTE_CACHE_LINE_SIZE,
183 DRV_LOG(ERR, "Failed to allocate qp memory.");
187 dev->data->queue_pairs[qp_id] = qp;
188 opaq_buf = rte_calloc(__func__, 1u << log_ops_n,
189 sizeof(struct mlx5_gga_compress_opaque),
190 sizeof(struct mlx5_gga_compress_opaque));
191 if (opaq_buf == NULL) {
192 DRV_LOG(ERR, "Failed to allocate opaque memory.");
196 if (mlx5_mr_btree_init(&qp->mr_ctrl.cache_bh, MLX5_MR_BTREE_CACHE_N,
197 priv->dev_config.socket_id)) {
198 DRV_LOG(ERR, "Cannot allocate MR Btree for qp %u.",
203 qp->entries_n = 1 << log_ops_n;
204 qp->socket_id = socket_id;
207 qp->ops = (struct rte_comp_op **)RTE_ALIGN((uintptr_t)(qp + 1),
208 RTE_CACHE_LINE_SIZE);
209 qp->uar_addr = mlx5_os_get_devx_uar_reg_addr(priv->uar);
210 MLX5_ASSERT(qp->uar_addr);
211 if (mlx5_common_verbs_reg_mr(priv->pd, opaq_buf, qp->entries_n *
212 sizeof(struct mlx5_gga_compress_opaque),
213 &qp->opaque_mr) != 0) {
215 DRV_LOG(ERR, "Failed to register opaque MR.");
219 ret = mlx5_devx_cq_create(priv->ctx, &qp->cq, log_ops_n, &cq_attr,
222 DRV_LOG(ERR, "Failed to create CQ.");
225 sq_attr.cqn = qp->cq.cq->id;
226 ret = mlx5_devx_sq_create(priv->ctx, &qp->sq, log_ops_n, &sq_attr,
229 DRV_LOG(ERR, "Failed to create SQ.");
232 mlx5_compress_init_sq(qp);
233 ret = mlx5_devx_cmd_modify_sq(qp->sq.sq, &modify_attr);
235 DRV_LOG(ERR, "Can't change SQ state to ready.");
238 DRV_LOG(INFO, "QP %u: SQN=0x%X CQN=0x%X entries num = %u\n",
239 (uint32_t)qp_id, qp->sq.sq->id, qp->cq.cq->id, qp->entries_n);
242 mlx5_compress_qp_release(dev, qp_id);
247 mlx5_compress_xform_free(struct rte_compressdev *dev, void *xform)
249 struct mlx5_compress_priv *priv = dev->data->dev_private;
251 rte_spinlock_lock(&priv->xform_sl);
252 LIST_REMOVE((struct mlx5_compress_xform *)xform, next);
253 rte_spinlock_unlock(&priv->xform_sl);
259 mlx5_compress_xform_create(struct rte_compressdev *dev,
260 const struct rte_comp_xform *xform,
261 void **private_xform)
263 struct mlx5_compress_priv *priv = dev->data->dev_private;
264 struct mlx5_compress_xform *xfrm;
267 if (xform->type == RTE_COMP_COMPRESS && xform->compress.level ==
268 RTE_COMP_LEVEL_NONE) {
269 DRV_LOG(ERR, "Non-compressed block is not supported.");
272 if ((xform->type == RTE_COMP_COMPRESS && xform->compress.hash_algo !=
273 RTE_COMP_HASH_ALGO_NONE) || (xform->type == RTE_COMP_DECOMPRESS &&
274 xform->decompress.hash_algo != RTE_COMP_HASH_ALGO_NONE)) {
275 DRV_LOG(ERR, "SHA is not supported.");
278 xfrm = rte_zmalloc_socket(__func__, sizeof(*xfrm), 0,
279 priv->dev_config.socket_id);
282 xfrm->opcode = MLX5_OPCODE_MMO;
283 xfrm->type = xform->type;
284 switch (xform->type) {
285 case RTE_COMP_COMPRESS:
286 switch (xform->compress.algo) {
287 case RTE_COMP_ALGO_NULL:
288 xfrm->opcode += MLX5_OPC_MOD_MMO_DMA <<
289 WQE_CSEG_OPC_MOD_OFFSET;
291 case RTE_COMP_ALGO_DEFLATE:
292 size = 1 << xform->compress.window_size;
293 size /= MLX5_GGA_COMP_WIN_SIZE_UNITS;
294 xfrm->gga_ctrl1 += RTE_MIN(rte_log2_u32(size),
295 MLX5_COMP_MAX_WIN_SIZE_CONF) <<
296 WQE_GGA_COMP_WIN_SIZE_OFFSET;
297 if (xform->compress.level == RTE_COMP_LEVEL_PMD_DEFAULT)
298 size = MLX5_GGA_COMP_LOG_BLOCK_SIZE_MAX;
300 size = priv->min_block_size - 1 +
301 xform->compress.level;
302 xfrm->gga_ctrl1 += RTE_MIN(size,
303 MLX5_GGA_COMP_LOG_BLOCK_SIZE_MAX) <<
304 WQE_GGA_COMP_BLOCK_SIZE_OFFSET;
305 xfrm->opcode += MLX5_OPC_MOD_MMO_COMP <<
306 WQE_CSEG_OPC_MOD_OFFSET;
307 size = xform->compress.deflate.huffman ==
308 RTE_COMP_HUFFMAN_DYNAMIC ?
309 MLX5_GGA_COMP_LOG_DYNAMIC_SIZE_MAX :
310 MLX5_GGA_COMP_LOG_DYNAMIC_SIZE_MIN;
311 xfrm->gga_ctrl1 += size <<
312 WQE_GGA_COMP_DYNAMIC_SIZE_OFFSET;
317 xfrm->csum_type = xform->compress.chksum;
319 case RTE_COMP_DECOMPRESS:
320 switch (xform->decompress.algo) {
321 case RTE_COMP_ALGO_NULL:
322 xfrm->opcode += MLX5_OPC_MOD_MMO_DMA <<
323 WQE_CSEG_OPC_MOD_OFFSET;
325 case RTE_COMP_ALGO_DEFLATE:
326 xfrm->opcode += MLX5_OPC_MOD_MMO_DECOMP <<
327 WQE_CSEG_OPC_MOD_OFFSET;
332 xfrm->csum_type = xform->decompress.chksum;
335 DRV_LOG(ERR, "Algorithm %u is not supported.", xform->type);
338 DRV_LOG(DEBUG, "New xform: gga ctrl1 = 0x%08X opcode = 0x%08X csum "
339 "type = %d.", xfrm->gga_ctrl1, xfrm->opcode, xfrm->csum_type);
340 xfrm->gga_ctrl1 = rte_cpu_to_be_32(xfrm->gga_ctrl1);
341 rte_spinlock_lock(&priv->xform_sl);
342 LIST_INSERT_HEAD(&priv->xform_list, xfrm, next);
343 rte_spinlock_unlock(&priv->xform_sl);
344 *private_xform = xfrm;
351 static struct rte_compressdev_ops mlx5_compress_ops = {
352 .dev_configure = mlx5_compress_dev_configure,
355 .dev_close = mlx5_compress_dev_close,
356 .dev_infos_get = mlx5_compress_dev_info_get,
359 .queue_pair_setup = mlx5_compress_qp_setup,
360 .queue_pair_release = mlx5_compress_qp_release,
361 .private_xform_create = mlx5_compress_xform_create,
362 .private_xform_free = mlx5_compress_xform_free,
363 .stream_create = NULL,
367 static struct ibv_device *
368 mlx5_compress_get_ib_device_match(struct rte_pci_addr *addr)
371 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
372 struct ibv_device *ibv_match = NULL;
374 if (ibv_list == NULL) {
379 struct rte_pci_addr paddr;
381 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
382 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &paddr) != 0)
384 if (rte_pci_addr_cmp(addr, &paddr) != 0)
386 ibv_match = ibv_list[n];
389 if (ibv_match == NULL)
391 mlx5_glue->free_device_list(ibv_list);
396 mlx5_compress_hw_global_release(struct mlx5_compress_priv *priv)
398 if (priv->pd != NULL) {
399 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
402 if (priv->uar != NULL) {
403 mlx5_glue->devx_free_uar(priv->uar);
409 mlx5_compress_pd_create(struct mlx5_compress_priv *priv)
411 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
412 struct mlx5dv_obj obj;
413 struct mlx5dv_pd pd_info;
416 priv->pd = mlx5_glue->alloc_pd(priv->ctx);
417 if (priv->pd == NULL) {
418 DRV_LOG(ERR, "Failed to allocate PD.");
419 return errno ? -errno : -ENOMEM;
421 obj.pd.in = priv->pd;
422 obj.pd.out = &pd_info;
423 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
425 DRV_LOG(ERR, "Fail to get PD object info.");
426 mlx5_glue->dealloc_pd(priv->pd);
430 priv->pdn = pd_info.pdn;
434 DRV_LOG(ERR, "Cannot get pdn - no DV support.");
436 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
440 mlx5_compress_hw_global_prepare(struct mlx5_compress_priv *priv)
442 if (mlx5_compress_pd_create(priv) != 0)
444 priv->uar = mlx5_devx_alloc_uar(priv->ctx, -1);
445 if (priv->uar == NULL || mlx5_os_get_devx_uar_reg_addr(priv->uar) ==
448 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
449 DRV_LOG(ERR, "Failed to allocate UAR.");
456 * DPDK callback to register a PCI device.
458 * This function spawns compress device out of a given PCI device.
461 * PCI driver structure (mlx5_compress_driver).
463 * PCI device information.
466 * 0 on success, 1 to skip this driver, a negative errno value otherwise
467 * and rte_errno is set.
470 mlx5_compress_pci_probe(struct rte_pci_driver *pci_drv,
471 struct rte_pci_device *pci_dev)
473 struct ibv_device *ibv;
474 struct rte_compressdev *cdev;
475 struct ibv_context *ctx;
476 struct mlx5_compress_priv *priv;
477 struct mlx5_hca_attr att = { 0 };
478 struct rte_compressdev_pmd_init_params init_params = {
480 .socket_id = pci_dev->device.numa_node,
483 RTE_SET_USED(pci_drv);
484 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
485 DRV_LOG(ERR, "Non-primary process type is not supported.");
489 ibv = mlx5_compress_get_ib_device_match(&pci_dev->addr);
491 DRV_LOG(ERR, "No matching IB device for PCI slot "
492 PCI_PRI_FMT ".", pci_dev->addr.domain,
493 pci_dev->addr.bus, pci_dev->addr.devid,
494 pci_dev->addr.function);
497 DRV_LOG(INFO, "PCI information matches for device \"%s\".", ibv->name);
498 ctx = mlx5_glue->dv_open_device(ibv);
500 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
504 if (mlx5_devx_cmd_query_hca_attr(ctx, &att) != 0 ||
505 att.mmo_compress_en == 0 || att.mmo_decompress_en == 0 ||
506 att.mmo_dma_en == 0) {
507 DRV_LOG(ERR, "Not enough capabilities to support compress "
508 "operations, maybe old FW/OFED version?");
509 claim_zero(mlx5_glue->close_device(ctx));
513 cdev = rte_compressdev_pmd_create(ibv->name, &pci_dev->device,
514 sizeof(*priv), &init_params);
516 DRV_LOG(ERR, "Failed to create device \"%s\".", ibv->name);
517 claim_zero(mlx5_glue->close_device(ctx));
521 "Compress device %s was created successfully.", ibv->name);
522 cdev->dev_ops = &mlx5_compress_ops;
523 cdev->dequeue_burst = NULL;
524 cdev->enqueue_burst = NULL;
525 cdev->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
526 priv = cdev->data->dev_private;
528 priv->pci_dev = pci_dev;
530 priv->min_block_size = att.compress_min_block_size;
531 if (mlx5_compress_hw_global_prepare(priv) != 0) {
532 rte_compressdev_pmd_destroy(priv->cdev);
533 claim_zero(mlx5_glue->close_device(priv->ctx));
536 if (mlx5_mr_btree_init(&priv->mr_scache.cache,
537 MLX5_MR_BTREE_CACHE_N * 2, rte_socket_id()) != 0) {
538 DRV_LOG(ERR, "Failed to allocate shared cache MR memory.");
539 mlx5_compress_hw_global_release(priv);
540 rte_compressdev_pmd_destroy(priv->cdev);
541 claim_zero(mlx5_glue->close_device(priv->ctx));
545 priv->mr_scache.reg_mr_cb = mlx5_common_verbs_reg_mr;
546 priv->mr_scache.dereg_mr_cb = mlx5_common_verbs_dereg_mr;
547 pthread_mutex_lock(&priv_list_lock);
548 TAILQ_INSERT_TAIL(&mlx5_compress_priv_list, priv, next);
549 pthread_mutex_unlock(&priv_list_lock);
554 * DPDK callback to remove a PCI device.
556 * This function removes all compress devices belong to a given PCI device.
559 * Pointer to the PCI device.
562 * 0 on success, the function cannot fail.
565 mlx5_compress_pci_remove(struct rte_pci_device *pdev)
567 struct mlx5_compress_priv *priv = NULL;
569 pthread_mutex_lock(&priv_list_lock);
570 TAILQ_FOREACH(priv, &mlx5_compress_priv_list, next)
571 if (rte_pci_addr_cmp(&priv->pci_dev->addr, &pdev->addr) != 0)
574 TAILQ_REMOVE(&mlx5_compress_priv_list, priv, next);
575 pthread_mutex_unlock(&priv_list_lock);
577 mlx5_mr_release_cache(&priv->mr_scache);
578 mlx5_compress_hw_global_release(priv);
579 rte_compressdev_pmd_destroy(priv->cdev);
580 claim_zero(mlx5_glue->close_device(priv->ctx));
585 static const struct rte_pci_id mlx5_compress_pci_id_map[] = {
587 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
588 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
595 static struct mlx5_pci_driver mlx5_compress_driver = {
596 .driver_class = MLX5_CLASS_COMPRESS,
599 .name = RTE_STR(MLX5_COMPRESS_DRIVER_NAME),
601 .id_table = mlx5_compress_pci_id_map,
602 .probe = mlx5_compress_pci_probe,
603 .remove = mlx5_compress_pci_remove,
608 RTE_INIT(rte_mlx5_compress_init)
611 if (mlx5_glue != NULL)
612 mlx5_pci_driver_register(&mlx5_compress_driver);
615 RTE_LOG_REGISTER(mlx5_compress_logtype, MLX5_COMPRESS_LOG_NAME, NOTICE)
616 RTE_PMD_EXPORT_NAME(MLX5_COMPRESS_DRIVER_NAME, __COUNTER__);
617 RTE_PMD_REGISTER_PCI_TABLE(MLX5_COMPRESS_DRIVER_NAME, mlx5_compress_pci_id_map);
618 RTE_PMD_REGISTER_KMOD_DEP(MLX5_COMPRESS_DRIVER_NAME, "* ib_uverbs & mlx5_core & mlx5_ib");