1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2018 Intel Corporation
6 #include "qat_comp_pmd.h"
8 static const struct rte_compressdev_capabilities qat_comp_gen_capabilities[] = {
9 {/* COMPRESSION - deflate */
10 .algo = RTE_COMP_ALGO_DEFLATE,
11 .comp_feature_flags = RTE_COMP_FF_MULTI_PKT_CHECKSUM |
12 RTE_COMP_FF_CRC32_CHECKSUM |
13 RTE_COMP_FF_ADLER32_CHECKSUM |
14 RTE_COMP_FF_CRC32_ADLER32_CHECKSUM |
15 RTE_COMP_FF_SHAREABLE_PRIV_XFORM |
16 RTE_COMP_FF_HUFFMAN_FIXED |
17 RTE_COMP_FF_HUFFMAN_DYNAMIC |
18 RTE_COMP_FF_OOP_SGL_IN_SGL_OUT |
19 RTE_COMP_FF_OOP_SGL_IN_LB_OUT |
20 RTE_COMP_FF_OOP_LB_IN_SGL_OUT,
21 .window_size = {.min = 15, .max = 15, .increment = 0} },
22 {RTE_COMP_ALGO_LIST_END, 0, {0, 0, 0} } };
25 qat_comp_stats_get(struct rte_compressdev *dev,
26 struct rte_compressdev_stats *stats)
28 struct qat_common_stats qat_stats = {0};
29 struct qat_comp_dev_private *qat_priv;
31 if (stats == NULL || dev == NULL) {
32 QAT_LOG(ERR, "invalid ptr: stats %p, dev %p", stats, dev);
35 qat_priv = dev->data->dev_private;
37 qat_stats_get(qat_priv->qat_dev, &qat_stats, QAT_SERVICE_COMPRESSION);
38 stats->enqueued_count = qat_stats.enqueued_count;
39 stats->dequeued_count = qat_stats.dequeued_count;
40 stats->enqueue_err_count = qat_stats.enqueue_err_count;
41 stats->dequeue_err_count = qat_stats.dequeue_err_count;
45 qat_comp_stats_reset(struct rte_compressdev *dev)
47 struct qat_comp_dev_private *qat_priv;
50 QAT_LOG(ERR, "invalid compressdev ptr %p", dev);
53 qat_priv = dev->data->dev_private;
55 qat_stats_reset(qat_priv->qat_dev, QAT_SERVICE_COMPRESSION);
60 qat_comp_qp_release(struct rte_compressdev *dev, uint16_t queue_pair_id)
62 struct qat_comp_dev_private *qat_private = dev->data->dev_private;
64 QAT_LOG(DEBUG, "Release comp qp %u on device %d",
65 queue_pair_id, dev->data->dev_id);
67 qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][queue_pair_id]
70 return qat_qp_release((struct qat_qp **)
71 &(dev->data->queue_pairs[queue_pair_id]));
75 qat_comp_qp_setup(struct rte_compressdev *dev, uint16_t qp_id,
76 uint32_t max_inflight_ops, int socket_id)
81 struct qat_qp_config qat_qp_conf;
83 struct qat_qp **qp_addr =
84 (struct qat_qp **)&(dev->data->queue_pairs[qp_id]);
85 struct qat_comp_dev_private *qat_private = dev->data->dev_private;
86 const struct qat_qp_hw_data *comp_hw_qps =
87 qat_gen_config[qat_private->qat_dev->qat_dev_gen]
88 .qp_hw_data[QAT_SERVICE_COMPRESSION];
89 const struct qat_qp_hw_data *qp_hw_data = comp_hw_qps + qp_id;
91 /* If qp is already in use free ring memory and qp metadata. */
92 if (*qp_addr != NULL) {
93 ret = qat_comp_qp_release(dev, qp_id);
97 if (qp_id >= qat_qps_per_service(comp_hw_qps,
98 QAT_SERVICE_COMPRESSION)) {
99 QAT_LOG(ERR, "qp_id %u invalid for this device", qp_id);
103 qat_qp_conf.hw = qp_hw_data;
104 qat_qp_conf.build_request = qat_comp_build_request;
105 qat_qp_conf.cookie_size = sizeof(struct qat_comp_op_cookie);
106 qat_qp_conf.nb_descriptors = max_inflight_ops;
107 qat_qp_conf.socket_id = socket_id;
108 qat_qp_conf.service_str = "comp";
110 ret = qat_qp_setup(qat_private->qat_dev, qp_addr, qp_id, &qat_qp_conf);
114 /* store a link to the qp in the qat_pci_device */
115 qat_private->qat_dev->qps_in_use[QAT_SERVICE_COMPRESSION][qp_id]
118 qp = (struct qat_qp *)*qp_addr;
120 for (i = 0; i < qp->nb_descriptors; i++) {
122 struct qat_comp_op_cookie *cookie =
125 cookie->qat_sgl_src_phys_addr =
126 rte_mempool_virt2iova(cookie) +
127 offsetof(struct qat_comp_op_cookie,
130 cookie->qat_sgl_dst_phys_addr =
131 rte_mempool_virt2iova(cookie) +
132 offsetof(struct qat_comp_op_cookie,
140 #define QAT_IM_BUFFER_DEBUG 0
141 static const struct rte_memzone *
142 qat_comp_setup_inter_buffers(struct qat_comp_dev_private *comp_dev,
145 char inter_buff_mz_name[RTE_MEMZONE_NAMESIZE];
146 const struct rte_memzone *memzone;
147 uint8_t *mz_start = NULL;
148 rte_iova_t mz_start_phys = 0;
149 struct array_of_ptrs *array_of_pointers;
150 int size_of_ptr_array;
152 uint32_t offset_of_sgls, offset_of_flat_buffs = 0;
154 int num_im_sgls = qat_gen_config[
155 comp_dev->qat_dev->qat_dev_gen].comp_num_im_bufs_required;
157 QAT_LOG(DEBUG, "QAT COMP device %s needs %d sgls",
158 comp_dev->qat_dev->name, num_im_sgls);
159 snprintf(inter_buff_mz_name, RTE_MEMZONE_NAMESIZE,
160 "%s_inter_buff", comp_dev->qat_dev->name);
161 memzone = rte_memzone_lookup(inter_buff_mz_name);
162 if (memzone != NULL) {
163 QAT_LOG(DEBUG, "QAT COMP im buffer memzone created already");
167 /* Create a memzone to hold intermediate buffers and associated
168 * meta-data needed by the firmware. The memzone contains:
169 * - a list of num_im_sgls physical pointers to sgls
170 * - the num_im_sgl sgl structures, each pointing to 2 flat buffers
171 * - the flat buffers: num_im_sgl * 2
172 * where num_im_sgls depends on the hardware generation of the device
175 size_of_ptr_array = num_im_sgls * sizeof(phys_addr_t);
176 offset_of_sgls = (size_of_ptr_array + (~QAT_64_BYTE_ALIGN_MASK))
177 & QAT_64_BYTE_ALIGN_MASK;
178 offset_of_flat_buffs =
179 offset_of_sgls + num_im_sgls * sizeof(struct qat_inter_sgl);
180 full_size = offset_of_flat_buffs +
181 num_im_sgls * buff_size * QAT_NUM_BUFS_IN_IM_SGL;
183 memzone = rte_memzone_reserve_aligned(inter_buff_mz_name, full_size,
184 comp_dev->compressdev->data->socket_id,
185 RTE_MEMZONE_2MB, QAT_64_BYTE_ALIGN);
186 if (memzone == NULL) {
187 QAT_LOG(ERR, "Can't allocate intermediate buffers"
188 " for device %s", comp_dev->qat_dev->name);
192 mz_start = (uint8_t *)memzone->addr;
193 mz_start_phys = memzone->phys_addr;
194 QAT_LOG(DEBUG, "Memzone %s: addr = %p, phys = 0x%"PRIx64
195 ", size required %d, size created %zu",
196 inter_buff_mz_name, mz_start, mz_start_phys,
197 full_size, memzone->len);
199 array_of_pointers = (struct array_of_ptrs *)mz_start;
200 for (i = 0; i < num_im_sgls; i++) {
201 uint32_t curr_sgl_offset =
202 offset_of_sgls + i * sizeof(struct qat_inter_sgl);
203 struct qat_inter_sgl *sgl =
204 (struct qat_inter_sgl *)(mz_start + curr_sgl_offset);
205 array_of_pointers->pointer[i] = mz_start_phys + curr_sgl_offset;
207 sgl->num_bufs = QAT_NUM_BUFS_IN_IM_SGL;
208 sgl->num_mapped_bufs = 0;
210 sgl->buffers[0].addr = mz_start_phys + offset_of_flat_buffs +
211 ((i * QAT_NUM_BUFS_IN_IM_SGL) * buff_size);
212 sgl->buffers[0].len = buff_size;
213 sgl->buffers[0].resrvd = 0;
214 sgl->buffers[1].addr = mz_start_phys + offset_of_flat_buffs +
215 (((i * QAT_NUM_BUFS_IN_IM_SGL) + 1) * buff_size);
216 sgl->buffers[1].len = buff_size;
217 sgl->buffers[1].resrvd = 0;
219 #if QAT_IM_BUFFER_DEBUG
220 QAT_LOG(DEBUG, " : phys addr of sgl[%i] in array_of_pointers"
221 "= 0x%"PRIx64, i, array_of_pointers->pointer[i]);
222 QAT_LOG(DEBUG, " : virt address of sgl[%i] = %p", i, sgl);
223 QAT_LOG(DEBUG, " : sgl->buffers[0].addr = 0x%"PRIx64", len=%d",
224 sgl->buffers[0].addr, sgl->buffers[0].len);
225 QAT_LOG(DEBUG, " : sgl->buffers[1].addr = 0x%"PRIx64", len=%d",
226 sgl->buffers[1].addr, sgl->buffers[1].len);
229 #if QAT_IM_BUFFER_DEBUG
230 QAT_DP_HEXDUMP_LOG(DEBUG, "IM buffer memzone start:",
231 mz_start, offset_of_flat_buffs + 32);
236 static struct rte_mempool *
237 qat_comp_create_xform_pool(struct qat_comp_dev_private *comp_dev,
238 uint32_t num_elements)
240 char xform_pool_name[RTE_MEMPOOL_NAMESIZE];
241 struct rte_mempool *mp;
243 snprintf(xform_pool_name, RTE_MEMPOOL_NAMESIZE,
244 "%s_xforms", comp_dev->qat_dev->name);
246 QAT_LOG(DEBUG, "xformpool: %s", xform_pool_name);
247 mp = rte_mempool_lookup(xform_pool_name);
250 QAT_LOG(DEBUG, "xformpool already created");
251 if (mp->size != num_elements) {
252 QAT_LOG(DEBUG, "xformpool wrong size - delete it");
253 rte_mempool_free(mp);
255 comp_dev->xformpool = NULL;
260 mp = rte_mempool_create(xform_pool_name,
262 qat_comp_xform_size(), 0, 0,
263 NULL, NULL, NULL, NULL, rte_socket_id(),
266 QAT_LOG(ERR, "Err creating mempool %s w %d elements of size %d",
267 xform_pool_name, num_elements, qat_comp_xform_size());
275 _qat_comp_dev_config_clear(struct qat_comp_dev_private *comp_dev)
277 /* Free intermediate buffers */
278 if (comp_dev->interm_buff_mz) {
279 rte_memzone_free(comp_dev->interm_buff_mz);
280 comp_dev->interm_buff_mz = NULL;
283 /* Free private_xform pool */
284 if (comp_dev->xformpool) {
285 /* Free internal mempool for private xforms */
286 rte_mempool_free(comp_dev->xformpool);
287 comp_dev->xformpool = NULL;
292 qat_comp_dev_config(struct rte_compressdev *dev,
293 struct rte_compressdev_config *config)
295 struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
298 if (config->max_nb_streams != 0) {
300 "QAT device does not support STATEFUL so max_nb_streams must be 0");
304 if (RTE_PMD_QAT_COMP_IM_BUFFER_SIZE == 0) {
306 "RTE_PMD_QAT_COMP_IM_BUFFER_SIZE = 0 in config file, so"
307 " QAT device can't be used for Dynamic Deflate. "
308 "Did you really intend to do this?");
310 comp_dev->interm_buff_mz =
311 qat_comp_setup_inter_buffers(comp_dev,
312 RTE_PMD_QAT_COMP_IM_BUFFER_SIZE);
313 if (comp_dev->interm_buff_mz == NULL) {
319 comp_dev->xformpool = qat_comp_create_xform_pool(comp_dev,
320 config->max_nb_priv_xforms);
321 if (comp_dev->xformpool == NULL) {
329 _qat_comp_dev_config_clear(comp_dev);
334 qat_comp_dev_start(struct rte_compressdev *dev __rte_unused)
340 qat_comp_dev_stop(struct rte_compressdev *dev __rte_unused)
346 qat_comp_dev_close(struct rte_compressdev *dev)
350 struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
352 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
353 ret = qat_comp_qp_release(dev, i);
358 _qat_comp_dev_config_clear(comp_dev);
365 qat_comp_dev_info_get(struct rte_compressdev *dev,
366 struct rte_compressdev_info *info)
368 struct qat_comp_dev_private *comp_dev = dev->data->dev_private;
369 const struct qat_qp_hw_data *comp_hw_qps =
370 qat_gen_config[comp_dev->qat_dev->qat_dev_gen]
371 .qp_hw_data[QAT_SERVICE_COMPRESSION];
374 info->max_nb_queue_pairs =
375 qat_qps_per_service(comp_hw_qps,
376 QAT_SERVICE_COMPRESSION);
377 info->feature_flags = dev->feature_flags;
378 info->capabilities = comp_dev->qat_dev_capabilities;
383 qat_comp_pmd_enqueue_op_burst(void *qp, struct rte_comp_op **ops,
386 return qat_enqueue_op_burst(qp, (void **)ops, nb_ops);
390 qat_comp_pmd_dequeue_op_burst(void *qp, struct rte_comp_op **ops,
393 return qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
397 qat_comp_pmd_enq_deq_dummy_op_burst(void *qp __rte_unused,
398 struct rte_comp_op **ops __rte_unused,
399 uint16_t nb_ops __rte_unused)
401 QAT_DP_LOG(ERR, "QAT PMD detected wrong FW version !");
405 static struct rte_compressdev_ops compress_qat_dummy_ops = {
407 /* Device related operations */
408 .dev_configure = NULL,
410 .dev_stop = qat_comp_dev_stop,
411 .dev_close = qat_comp_dev_close,
412 .dev_infos_get = NULL,
415 .stats_reset = qat_comp_stats_reset,
416 .queue_pair_setup = NULL,
417 .queue_pair_release = qat_comp_qp_release,
419 /* Compression related operations */
420 .private_xform_create = NULL,
421 .private_xform_free = qat_comp_private_xform_free
425 qat_comp_pmd_dequeue_frst_op_burst(void *qp, struct rte_comp_op **ops,
428 uint16_t ret = qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
429 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
432 if ((*ops)->debug_status ==
433 (uint64_t)ERR_CODE_QAT_COMP_WRONG_FW) {
434 tmp_qp->qat_dev->comp_dev->compressdev->enqueue_burst =
435 qat_comp_pmd_enq_deq_dummy_op_burst;
436 tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
437 qat_comp_pmd_enq_deq_dummy_op_burst;
439 tmp_qp->qat_dev->comp_dev->compressdev->dev_ops =
440 &compress_qat_dummy_ops;
441 QAT_LOG(ERR, "QAT PMD detected wrong FW version !");
444 tmp_qp->qat_dev->comp_dev->compressdev->dequeue_burst =
445 qat_comp_pmd_dequeue_op_burst;
451 static struct rte_compressdev_ops compress_qat_ops = {
453 /* Device related operations */
454 .dev_configure = qat_comp_dev_config,
455 .dev_start = qat_comp_dev_start,
456 .dev_stop = qat_comp_dev_stop,
457 .dev_close = qat_comp_dev_close,
458 .dev_infos_get = qat_comp_dev_info_get,
460 .stats_get = qat_comp_stats_get,
461 .stats_reset = qat_comp_stats_reset,
462 .queue_pair_setup = qat_comp_qp_setup,
463 .queue_pair_release = qat_comp_qp_release,
465 /* Compression related operations */
466 .private_xform_create = qat_comp_private_xform_create,
467 .private_xform_free = qat_comp_private_xform_free
470 /* An rte_driver is needed in the registration of the device with compressdev.
471 * The actual qat pci's rte_driver can't be used as its name represents
472 * the whole pci device with all services. Think of this as a holder for a name
473 * for the compression part of the pci device.
475 static const char qat_comp_drv_name[] = RTE_STR(COMPRESSDEV_NAME_QAT_PMD);
476 static const struct rte_driver compdev_qat_driver = {
477 .name = qat_comp_drv_name,
478 .alias = qat_comp_drv_name
481 qat_comp_dev_create(struct qat_pci_device *qat_pci_dev)
483 if (qat_pci_dev->qat_dev_gen == QAT_GEN1) {
484 QAT_LOG(ERR, "Compression PMD not supported on QAT dh895xcc");
487 if (qat_pci_dev->qat_dev_gen == QAT_GEN3) {
488 QAT_LOG(ERR, "Compression PMD not supported on QAT c4xxx");
492 struct rte_compressdev_pmd_init_params init_params = {
494 .socket_id = qat_pci_dev->pci_dev->device.numa_node,
496 char name[RTE_COMPRESSDEV_NAME_MAX_LEN];
497 struct rte_compressdev *compressdev;
498 struct qat_comp_dev_private *comp_dev;
500 snprintf(name, RTE_COMPRESSDEV_NAME_MAX_LEN, "%s_%s",
501 qat_pci_dev->name, "comp");
502 QAT_LOG(DEBUG, "Creating QAT COMP device %s", name);
504 /* Populate subset device to use in compressdev device creation */
505 qat_pci_dev->comp_rte_dev.driver = &compdev_qat_driver;
506 qat_pci_dev->comp_rte_dev.numa_node =
507 qat_pci_dev->pci_dev->device.numa_node;
508 qat_pci_dev->comp_rte_dev.devargs = NULL;
510 compressdev = rte_compressdev_pmd_create(name,
511 &(qat_pci_dev->comp_rte_dev),
512 sizeof(struct qat_comp_dev_private),
515 if (compressdev == NULL)
518 compressdev->dev_ops = &compress_qat_ops;
520 compressdev->enqueue_burst = qat_comp_pmd_enqueue_op_burst;
521 compressdev->dequeue_burst = qat_comp_pmd_dequeue_frst_op_burst;
523 compressdev->feature_flags = RTE_COMPDEV_FF_HW_ACCELERATED;
525 comp_dev = compressdev->data->dev_private;
526 comp_dev->qat_dev = qat_pci_dev;
527 comp_dev->compressdev = compressdev;
528 qat_pci_dev->comp_dev = comp_dev;
530 switch (qat_pci_dev->qat_dev_gen) {
534 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
537 comp_dev->qat_dev_capabilities = qat_comp_gen_capabilities;
539 "QAT gen %d capabilities unknown, default to GEN1",
540 qat_pci_dev->qat_dev_gen);
545 "Created QAT COMP device %s as compressdev instance %d",
546 name, compressdev->data->dev_id);
551 qat_comp_dev_destroy(struct qat_pci_device *qat_pci_dev)
553 struct qat_comp_dev_private *comp_dev;
555 if (qat_pci_dev == NULL)
558 comp_dev = qat_pci_dev->comp_dev;
559 if (comp_dev == NULL)
562 /* clean up any resources used by the device */
563 qat_comp_dev_close(comp_dev->compressdev);
565 rte_compressdev_pmd_destroy(comp_dev->compressdev);
566 qat_pci_dev->comp_dev = NULL;