1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2019 Intel Corporation
5 #include <rte_malloc.h>
8 #include "qat_comp_pmd.h"
10 #define QAT_PMD_COMP_SGL_DEF_SEGMENTS 16
12 static const struct rte_compressdev_capabilities qat_comp_gen_capabilities[] = {
13 {/* COMPRESSION - deflate */
14 .algo = RTE_COMP_ALGO_DEFLATE,
15 .comp_feature_flags = RTE_COMP_FF_MULTI_PKT_CHECKSUM |
16 RTE_COMP_FF_CRC32_CHECKSUM |
17 RTE_COMP_FF_ADLER32_CHECKSUM |
18 RTE_COMP_FF_CRC32_ADLER32_CHECKSUM |
19 RTE_COMP_FF_SHAREABLE_PRIV_XFORM |
20 RTE_COMP_FF_HUFFMAN_FIXED |
21 RTE_COMP_FF_HUFFMAN_DYNAMIC |
22 RTE_COMP_FF_OOP_SGL_IN_SGL_OUT |
23 RTE_COMP_FF_OOP_SGL_IN_LB_OUT |
24 RTE_COMP_FF_OOP_LB_IN_SGL_OUT,
25 .window_size = {.min = 15, .max = 15, .increment = 0} },
26 {RTE_COMP_ALGO_LIST_END, 0, {0, 0, 0} } };
29 qat_comp_stats_get(struct rte_compressdev *dev,
30 struct rte_compressdev_stats *stats)
32 struct qat_common_stats qat_stats = {0};
33 struct qat_comp_dev_private *qat_priv;
35 if (stats == NULL || dev == NULL) {
36 QAT_LOG(ERR, "invalid ptr: stats %p, dev %p", stats, dev);
39 qat_priv = dev->data->dev_private;
41 qat_stats_get(qat_priv->qat_dev, &qat_stats, QAT_SERVICE_COMPRESSION);
42 stats->enqueued_count = qat_stats.enqueued_count;
43 stats->dequeued_count = qat_stats.dequeued_count;
44 stats->enqueue_err_count = qat_stats.enqueue_err_count;
45 stats->dequeue_err_count = qat_stats.dequeue_err_count;
49 qat_comp_stats_reset(struct rte_compressdev *dev)
51 struct qat_comp_dev_private *qat_priv;
54 QAT_LOG(ERR, "invalid compressdev ptr %p", dev);
57 qat_priv = dev->data->dev_private;
59 qat_stats_reset(qat_priv->qat_dev, QAT_SERVICE_COMPRESSION);
64 qat_comp_qp_release(struct rte_compressdev *dev, uint16_t queue_pair_id)
66 struct qat_comp_dev_private *qat_private = dev->data->dev_private;
67 struct qat_qp **qp_addr =
68 (struct qat_qp **)&(dev->data->queue_pairs[queue_pair_id]);
69 struct qat_qp *qp = (struct qat_qp *)*qp_addr;
72 QAT_LOG(DEBUG, "Release comp qp %u on device %d",
73 queue_pair_id, dev->data->dev_id);
75 qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][queue_pair_id]
78 for (i = 0; i < qp->nb_descriptors; i++) {
80 struct qat_comp_op_cookie *cookie = qp->op_cookies[i];
82 rte_free(cookie->qat_sgl_src_d);
83 rte_free(cookie->qat_sgl_dst_d);
86 return qat_qp_release((struct qat_qp **)
87 &(dev->data->queue_pairs[queue_pair_id]));
91 qat_comp_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
92 uint32_t max_inflight_ops, int socket_id)
97 struct qat_qp_config qat_qp_conf;
99 struct qat_qp **qp_addr =
100 (struct qat_qp **)&(dev->data->queue_pairs[qp_id]);
101 struct qat_comp_dev_private *qat_private = dev->data->dev_private;
102 const struct qat_qp_hw_data *comp_hw_qps =
103 qat_gen_config[qat_private->qat_dev->qat_dev_gen]
104 .qp_hw_data[QAT_SERVICE_COMPRESSION];
105 const struct qat_qp_hw_data *qp_hw_data = comp_hw_qps + qp_id;
107 /* If qp is already in use free ring memory and qp metadata. */
108 if (*qp_addr != NULL) {
109 ret = qat_comp_qp_release(dev, qp_id);
113 if (qp_id >= qat_qps_per_service(comp_hw_qps,
114 QAT_SERVICE_COMPRESSION)) {
115 QAT_LOG(ERR, "qp_id %u invalid for this device", qp_id);
119 qat_qp_conf.hw = qp_hw_data;
120 qat_qp_conf.build_request = qat_comp_build_request;
121 qat_qp_conf.cookie_size = sizeof(struct qat_comp_op_cookie);
122 qat_qp_conf.nb_descriptors = max_inflight_ops;
123 qat_qp_conf.socket_id = socket_id;
124 qat_qp_conf.service_str = "comp";
126 ret = qat_qp_setup(qat_private->qat_dev, qp_addr, qp_id, &qat_qp_conf);
130 /* store a link to the qp in the qat_pci_device */
131 qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][qp_id]
134 qp = (struct qat_qp *)*qp_addr;
136 for (i = 0; i < qp->nb_descriptors; i++) {
138 struct qat_comp_op_cookie *cookie =
141 cookie->qat_sgl_src_d = rte_zmalloc_socket(NULL,
142 sizeof(struct qat_sgl) +
143 sizeof(struct qat_flat_buf) *
144 QAT_PMD_COMP_SGL_DEF_SEGMENTS,
145 64, dev->data->socket_id);
147 cookie->qat_sgl_dst_d = rte_zmalloc_socket(NULL,
148 sizeof(struct qat_sgl) +
149 sizeof(struct qat_flat_buf) *
150 QAT_PMD_COMP_SGL_DEF_SEGMENTS,
151 64, dev->data->socket_id);
153 if (cookie->qat_sgl_src_d == NULL ||
154 cookie->qat_sgl_dst_d == NULL) {
155 QAT_LOG(ERR, "Can't allocate SGL"
157 qat_private->qat_dev->name);
161 cookie->qat_sgl_src_phys_addr =
162 rte_malloc_virt2iova(cookie->qat_sgl_src_d);
164 cookie->qat_sgl_dst_phys_addr =
165 rte_malloc_virt2iova(cookie->qat_sgl_dst_d);
167 cookie->dst_nb_elems = cookie->src_nb_elems =
168 QAT_PMD_COMP_SGL_DEF_SEGMENTS;
170 cookie->socket_id = dev->data->socket_id;
179 #define QAT_IM_BUFFER_DEBUG 0
180 static const struct rte_memzone *
181 qat_comp_setup_inter_buffers(struct qat_comp_dev_private *comp_dev,
184 char inter_buff_mz_name[RTE_MEMZONE_NAMESIZE];
185 const struct rte_memzone *memzone;
186 uint8_t *mz_start = NULL;
187 rte_iova_t mz_start_phys = 0;
188 struct array_of_ptrs *array_of_pointers;
189 int size_of_ptr_array;
191 uint32_t offset_of_sgls, offset_of_flat_buffs = 0;
193 int num_im_sgls = qat_gen_config[
194 comp_dev->qat_dev->qat_dev_gen].comp_num_im_bufs_required;
196 QAT_LOG(DEBUG, "QAT COMP device %s needs %d sgls",
197 comp_dev->qat_dev->name, num_im_sgls);
198 snprintf(inter_buff_mz_name, RTE_MEMZONE_NAMESIZE,
199 "%s_inter_buff", comp_dev->qat_dev->name);
200 memzone = rte_memzone_lookup(inter_buff_mz_name);
201 if (memzone != NULL) {
202 QAT_LOG(DEBUG, "QAT COMP im buffer memzone created already");
206 /* Create a memzone to hold intermediate buffers and associated
207 * meta-data needed by the firmware. The memzone contains 3 parts:
208 * - a list of num_im_sgls physical pointers to sgls
209 * - the num_im_sgl sgl structures, each pointing to
210 * QAT_NUM_BUFS_IN_IM_SGL flat buffers
211 * - the flat buffers: num_im_sgl * QAT_NUM_BUFS_IN_IM_SGL
212 * buffers, each of buff_size
213 * num_im_sgls depends on the hardware generation of the device
214 * buff_size comes from the user via the config file
217 size_of_ptr_array = num_im_sgls * sizeof(phys_addr_t);
218 offset_of_sgls = (size_of_ptr_array + (~QAT_64_BYTE_ALIGN_MASK))
219 & QAT_64_BYTE_ALIGN_MASK;
220 offset_of_flat_buffs =
221 offset_of_sgls + num_im_sgls * sizeof(struct qat_inter_sgl);
222 full_size = offset_of_flat_buffs +
223 num_im_sgls * buff_size * QAT_NUM_BUFS_IN_IM_SGL;
225 memzone = rte_memzone_reserve_aligned(inter_buff_mz_name, full_size,
226 comp_dev->compressdev->data->socket_id,
227 RTE_MEMZONE_IOVA_CONTIG, QAT_64_BYTE_ALIGN);
228 if (memzone == NULL) {
229 QAT_LOG(ERR, "Can't allocate intermediate buffers"
230 " for device %s", comp_dev->qat_dev->name);
234 mz_start = (uint8_t *)memzone->addr;
235 mz_start_phys = memzone->phys_addr;
236 QAT_LOG(DEBUG, "Memzone %s: addr = %p, phys = 0x%"PRIx64
237 ", size required %d, size created %zu",
238 inter_buff_mz_name, mz_start, mz_start_phys,
239 full_size, memzone->len);
241 array_of_pointers = (struct array_of_ptrs *)mz_start;
242 for (i = 0; i < num_im_sgls; i++) {
243 uint32_t curr_sgl_offset =
244 offset_of_sgls + i * sizeof(struct qat_inter_sgl);
245 struct qat_inter_sgl *sgl =
246 (struct qat_inter_sgl *)(mz_start + curr_sgl_offset);
248 array_of_pointers->pointer[i] = mz_start_phys + curr_sgl_offset;
250 sgl->num_bufs = QAT_NUM_BUFS_IN_IM_SGL;
251 sgl->num_mapped_bufs = 0;
254 #if QAT_IM_BUFFER_DEBUG
255 QAT_LOG(DEBUG, " : phys addr of sgl[%i] in array_of_pointers"
256 " = 0x%"PRIx64, i, array_of_pointers->pointer[i]);
257 QAT_LOG(DEBUG, " : virt address of sgl[%i] = %p", i, sgl);
259 for (lb = 0; lb < QAT_NUM_BUFS_IN_IM_SGL; lb++) {
260 sgl->buffers[lb].addr =
261 mz_start_phys + offset_of_flat_buffs +
262 (((i * QAT_NUM_BUFS_IN_IM_SGL) + lb) * buff_size);
263 sgl->buffers[lb].len = buff_size;
264 sgl->buffers[lb].resrvd = 0;
265 #if QAT_IM_BUFFER_DEBUG
267 " : sgl->buffers[%d].addr = 0x%"PRIx64", len=%d",
268 lb, sgl->buffers[lb].addr, sgl->buffers[lb].len);
272 #if QAT_IM_BUFFER_DEBUG
273 QAT_DP_HEXDUMP_LOG(DEBUG, "IM buffer memzone start:",
274 mz_start, offset_of_flat_buffs + 32);
279 static struct rte_mempool *
280 qat_comp_create_xform_pool(struct qat_comp_dev_private *comp_dev,
281 struct rte_compressdev_config *config,
282 uint32_t num_elements)
284 char xform_pool_name[RTE_MEMPOOL_NAMESIZE];
285 struct rte_mempool *mp;
287 snprintf(xform_pool_name, RTE_MEMPOOL_NAMESIZE,
288 "%s_xforms", comp_dev->qat_dev->name);
290 QAT_LOG(DEBUG, "xformpool: %s", xform_pool_name);
291 mp = rte_mempool_lookup(xform_pool_name);
294 QAT_LOG(DEBUG, "xformpool already created");
295 if (mp->size != num_elements) {
296 QAT_LOG(DEBUG, "xformpool wrong size - delete it");
297 rte_mempool_free(mp);
299 comp_dev->xformpool = NULL;
304 mp = rte_mempool_create(xform_pool_name,
306 qat_comp_xform_size(), 0, 0,
307 NULL, NULL, NULL, NULL, config->socket_id,
310 QAT_LOG(ERR, "Err creating mempool %s w %d elements of size %d",
311 xform_pool_name, num_elements, qat_comp_xform_size());
319 _qat_comp_dev_config_clear(struct qat_comp_dev_private *comp_dev)
321 /* Free intermediate buffers */
322 if (comp_dev->interm_buff_mz) {
323 rte_memzone_free(comp_dev->interm_buff_mz);
324 comp_dev->interm_buff_mz = NULL;
327 /* Free private_xform pool */
328 if (comp_dev->xformpool) {
329 /* Free internal mempool for private xforms */
330 rte_mempool_free(comp_dev->xformpool);
331 comp_dev->xformpool = NULL;
336 qat_comp_dev_config(struct rte_compressdev *dev,
337 struct rte_compressdev_config *config)
339 struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
342 if (config->max_nb_streams != 0) {
344 "QAT device does not support STATEFUL so max_nb_streams must be 0");
348 if (RTE_PMD_QAT_COMP_IM_BUFFER_SIZE == 0) {
350 "RTE_PMD_QAT_COMP_IM_BUFFER_SIZE = 0 in config file, so"
351 " QAT device can't be used for Dynamic Deflate. "
352 "Did you really intend to do this?");
354 comp_dev->interm_buff_mz =
355 qat_comp_setup_inter_buffers(comp_dev,
356 RTE_PMD_QAT_COMP_IM_BUFFER_SIZE);
357 if (comp_dev->interm_buff_mz == NULL) {
363 comp_dev->xformpool = qat_comp_create_xform_pool(comp_dev, config,
364 config->max_nb_priv_xforms);
365 if (comp_dev->xformpool == NULL) {
373 _qat_comp_dev_config_clear(comp_dev);
378 qat_comp_dev_start(struct rte_compressdev *dev __rte_unused)
384 qat_comp_dev_stop(struct rte_compressdev *dev __rte_unused)
390 qat_comp_dev_close(struct rte_compressdev *dev)
394 struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
396 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
397 ret = qat_comp_qp_release(dev, i);
402 _qat_comp_dev_config_clear(comp_dev);
409 qat_comp_dev_info_get(struct rte_compressdev *dev,
410 struct rte_compressdev_info *info)
412 struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
413 const struct qat_qp_hw_data *comp_hw_qps =
414 qat_gen_config[comp_dev->qat_dev->qat_dev_gen]
415 .qp_hw_data[QAT_SERVICE_COMPRESSION];
418 info->max_nb_queue_pairs =
419 qat_qps_per_service(comp_hw_qps,
420 QAT_SERVICE_COMPRESSION);
421 info->feature_flags = dev->feature_flags;
422 info->capabilities = comp_dev->qat_dev_capabilities;
427 qat_comp_pmd_enqueue_op_burst(void *qp, struct rte_comp_op **ops,
430 return qat_enqueue_op_burst(qp, (void **)ops, nb_ops);
434 qat_comp_pmd_dequeue_op_burst(void *qp, struct rte_comp_op **ops,
437 return qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
441 qat_comp_pmd_enq_deq_dummy_op_burst(void *qp __rte_unused,
442 struct rte_comp_op **ops __rte_unused,
443 uint16_t nb_ops __rte_unused)
445 QAT_DP_LOG(ERR, "QAT PMD detected wrong FW version !");
449 static struct rte_compressdev_ops compress_qat_dummy_ops = {
451 /* Device related operations */
452 .dev_configure = NULL,
454 .dev_stop = qat_comp_dev_stop,
455 .dev_close = qat_comp_dev_close,
456 .dev_infos_get = NULL,
459 .stats_reset = qat_comp_stats_reset,
460 .queue_pair_setup = NULL,
461 .queue_pair_release = qat_comp_qp_release,
463 /* Compression related operations */
464 .private_xform_create = NULL,
465 .private_xform_free = qat_comp_private_xform_free
469 qat_comp_pmd_dequeue_frst_op_burst(void *qp, struct rte_comp_op **ops,
472 uint16_t ret = qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
473 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
476 if ((*ops)->debug_status ==
477 (uint64_t)ERR_CODE_QAT_COMP_WRONG_FW) {
478 tmp_qp->qat_dev->comp_dev->compressdev->enqueue_burst =
479 qat_comp_pmd_enq_deq_dummy_op_burst;
480 tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
481 qat_comp_pmd_enq_deq_dummy_op_burst;
483 tmp_qp->qat_dev->comp_dev->compressdev->dev_ops =
484 &compress_qat_dummy_ops;
485 QAT_LOG(ERR, "QAT PMD detected wrong FW version !");
488 tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
489 qat_comp_pmd_dequeue_op_burst;
495 static struct rte_compressdev_ops compress_qat_ops = {
497 /* Device related operations */
498 .dev_configure = qat_comp_dev_config,
499 .dev_start = qat_comp_dev_start,
500 .dev_stop = qat_comp_dev_stop,
501 .dev_close = qat_comp_dev_close,
502 .dev_infos_get = qat_comp_dev_info_get,
504 .stats_get = qat_comp_stats_get,
505 .stats_reset = qat_comp_stats_reset,
506 .queue_pair_setup = qat_comp_qp_setup,
507 .queue_pair_release = qat_comp_qp_release,
509 /* Compression related operations */
510 .private_xform_create = qat_comp_private_xform_create,
511 .private_xform_free = qat_comp_private_xform_free
514 /* An rte_driver is needed in the registration of the device with compressdev.
515 * The actual qat pci's rte_driver can't be used as its name represents
516 * the whole pci device with all services. Think of this as a holder for a name
517 * for the compression part of the pci device.
519 static const char qat_comp_drv_name[] = RTE_STR(COMPRESSDEV_NAME_QAT_PMD);
520 static const struct rte_driver compdev_qat_driver = {
521 .name = qat_comp_drv_name,
522 .alias = qat_comp_drv_name
525 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev)
527 if (qat_pci_dev->qat_dev_gen == QAT_GEN3) {
528 QAT_LOG(ERR, "Compression PMD not supported on QAT c4xxx");
532 struct rte_compressdev_pmd_init_params init_params = {
534 .socket_id = qat_pci_dev->pci_dev->device.numa_node,
536 char name[RTE_COMPRESSDEV_NAME_MAX_LEN];
537 struct rte_compressdev *compressdev;
538 struct qat_comp_dev_private *comp_dev;
540 snprintf(name, RTE_COMPRESSDEV_NAME_MAX_LEN, "%s_%s",
541 qat_pci_dev->name, "comp");
542 QAT_LOG(DEBUG, "Creating QAT COMP device %s", name);
544 /* Populate subset device to use in compressdev device creation */
545 qat_pci_dev->comp_rte_dev.driver = &compdev_qat_driver;
546 qat_pci_dev->comp_rte_dev.numa_node =
547 qat_pci_dev->pci_dev->device.numa_node;
548 qat_pci_dev->comp_rte_dev.devargs = NULL;
550 compressdev = rte_compressdev_pmd_create(name,
551 &(qat_pci_dev->comp_rte_dev),
552 sizeof(struct qat_comp_dev_private),
555 if (compressdev == NULL)
558 compressdev->dev_ops = &compress_qat_ops;
560 compressdev->enqueue_burst = qat_comp_pmd_enqueue_op_burst;
561 compressdev->dequeue_burst = qat_comp_pmd_dequeue_frst_op_burst;
563 compressdev->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
565 comp_dev = compressdev->data->dev_private;
566 comp_dev->qat_dev = qat_pci_dev;
567 comp_dev->compressdev = compressdev;
568 qat_pci_dev->comp_dev = comp_dev;
570 switch (qat_pci_dev->qat_dev_gen) {
574 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
577 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
579 "QAT gen %d capabilities unknown, default to GEN1",
580 qat_pci_dev->qat_dev_gen);
585 "Created QAT COMP device %s as compressdev instance %d",
586 name, compressdev->data->dev_id);
591 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev)
593 struct qat_comp_dev_private *comp_dev;
595 if (qat_pci_dev == NULL)
598 comp_dev = qat_pci_dev->comp_dev;
599 if (comp_dev == NULL)
602 /* clean up any resources used by the device */
603 qat_comp_dev_close(comp_dev->compressdev);
605 rte_compressdev_pmd_destroy(comp_dev->compressdev);
606 qat_pci_dev->comp_dev = NULL;