1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
5 #include <rte_mempool.h>
7 #include <rte_hexdump.h>
9 #include <rte_bus_pci.h>
10 #include <rte_byteorder.h>
11 #include <rte_memcpy.h>
12 #include <rte_common.h>
13 #include <rte_spinlock.h>
15 #include <rte_malloc.h>
19 #include "qat_comp_pmd.h"
23 qat_comp_build_request(void *in_op, uint8_t *out_msg,
25 enum qat_device_gen qat_dev_gen __rte_unused)
27 struct rte_comp_op *op = in_op;
28 struct qat_comp_op_cookie *cookie =
29 (struct qat_comp_op_cookie *)op_cookie;
30 struct qat_comp_xform *qat_xform = op->private_xform;
31 const uint8_t *tmpl = (uint8_t *)&qat_xform->qat_comp_req_tmpl;
32 struct icp_qat_fw_comp_req *comp_req =
33 (struct icp_qat_fw_comp_req *)out_msg;
35 if (unlikely(op->op_type != RTE_COMP_OP_STATELESS)) {
36 QAT_DP_LOG(ERR, "QAT PMD only supports stateless compression "
37 "operation requests, op (%p) is not a "
38 "stateless operation.", op);
39 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
43 rte_mov128(out_msg, tmpl);
44 comp_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
46 /* common for sgl and flat buffers */
47 comp_req->comp_pars.comp_len = op->src.length;
48 comp_req->comp_pars.out_buffer_sz = rte_pktmbuf_pkt_len(op->m_dst) -
51 if (op->m_src->next != NULL || op->m_dst->next != NULL) {
55 ICP_QAT_FW_COMN_PTR_TYPE_SET(comp_req->comn_hdr.comn_req_flags,
56 QAT_COMN_PTR_TYPE_SGL);
58 ret = qat_sgl_fill_array(op->m_src,
62 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
64 QAT_DP_LOG(ERR, "QAT PMD Cannot fill source sgl array");
65 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
69 ret = qat_sgl_fill_array(op->m_dst,
72 comp_req->comp_pars.out_buffer_sz,
73 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
75 QAT_DP_LOG(ERR, "QAT PMD Cannot fill dest. sgl array");
76 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
80 comp_req->comn_mid.src_data_addr =
81 cookie->qat_sgl_src_phys_addr;
82 comp_req->comn_mid.dest_data_addr =
83 cookie->qat_sgl_dst_phys_addr;
84 comp_req->comn_mid.src_length = 0;
85 comp_req->comn_mid.dst_length = 0;
88 /* flat aka linear buffer */
89 ICP_QAT_FW_COMN_PTR_TYPE_SET(comp_req->comn_hdr.comn_req_flags,
90 QAT_COMN_PTR_TYPE_FLAT);
91 comp_req->comn_mid.src_length = op->src.length;
92 comp_req->comn_mid.dst_length =
93 comp_req->comp_pars.out_buffer_sz;
95 comp_req->comn_mid.src_data_addr =
96 rte_pktmbuf_mtophys_offset(op->m_src, op->src.offset);
97 comp_req->comn_mid.dest_data_addr =
98 rte_pktmbuf_mtophys_offset(op->m_dst, op->dst.offset);
101 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
102 QAT_DP_LOG(DEBUG, "Direction: %s",
103 qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS ?
104 "decompression" : "compression");
105 QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message:", comp_req,
106 sizeof(struct icp_qat_fw_comp_req));
112 qat_comp_process_response(void **op, uint8_t *resp, uint64_t *dequeue_err_count)
114 struct icp_qat_fw_comp_resp *resp_msg =
115 (struct icp_qat_fw_comp_resp *)resp;
116 struct rte_comp_op *rx_op = (struct rte_comp_op *)(uintptr_t)
117 (resp_msg->opaque_data);
118 struct qat_comp_xform *qat_xform = (struct qat_comp_xform *)
119 (rx_op->private_xform);
121 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
122 QAT_DP_LOG(DEBUG, "Direction: %s",
123 qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS ?
124 "decompression" : "compression");
125 QAT_DP_HEXDUMP_LOG(DEBUG, "qat_response:", (uint8_t *)resp_msg,
126 sizeof(struct icp_qat_fw_comp_resp));
129 if (likely(qat_xform->qat_comp_request_type
130 != QAT_COMP_REQUEST_DECOMPRESS)) {
131 if (unlikely(ICP_QAT_FW_COMN_HDR_CNV_FLAG_GET(
132 resp_msg->comn_resp.hdr_flags)
133 == ICP_QAT_FW_COMP_NO_CNV)) {
134 rx_op->status = RTE_COMP_OP_STATUS_ERROR;
135 rx_op->debug_status = ERR_CODE_QAT_COMP_WRONG_FW;
137 QAT_DP_LOG(ERR, "QAT has wrong firmware");
138 ++(*dequeue_err_count);
143 if ((ICP_QAT_FW_COMN_RESP_CMP_STAT_GET(resp_msg->comn_resp.comn_status)
144 | ICP_QAT_FW_COMN_RESP_XLAT_STAT_GET(
145 resp_msg->comn_resp.comn_status)) !=
146 ICP_QAT_FW_COMN_STATUS_FLAG_OK) {
148 if (unlikely((ICP_QAT_FW_COMN_RESP_XLAT_STAT_GET(
149 resp_msg->comn_resp.comn_status) !=
150 ICP_QAT_FW_COMN_STATUS_FLAG_OK) &&
151 (qat_xform->qat_comp_request_type
152 == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS)))
153 QAT_DP_LOG(ERR, "QAT intermediate buffer may be too "
154 "small for output, try configuring a larger size");
156 ++(*dequeue_err_count);
157 rx_op->status = RTE_COMP_OP_STATUS_ERROR;
158 rx_op->debug_status =
159 *((uint16_t *)(&resp_msg->comn_resp.comn_error));
161 struct icp_qat_fw_resp_comp_pars *comp_resp =
162 (struct icp_qat_fw_resp_comp_pars *)&resp_msg->comp_resp_pars;
164 rx_op->status = RTE_COMP_OP_STATUS_SUCCESS;
165 rx_op->consumed = comp_resp->input_byte_counter;
166 rx_op->produced = comp_resp->output_byte_counter;
168 if (qat_xform->checksum_type != RTE_COMP_CHECKSUM_NONE) {
169 if (qat_xform->checksum_type == RTE_COMP_CHECKSUM_CRC32)
170 rx_op->output_chksum = comp_resp->curr_crc32;
171 else if (qat_xform->checksum_type ==
172 RTE_COMP_CHECKSUM_ADLER32)
173 rx_op->output_chksum = comp_resp->curr_adler_32;
175 rx_op->output_chksum = comp_resp->curr_chksum;
184 qat_comp_xform_size(void)
186 return RTE_ALIGN_CEIL(sizeof(struct qat_comp_xform), 8);
189 static void qat_comp_create_req_hdr(struct icp_qat_fw_comn_req_hdr *header,
190 enum qat_comp_request_type request)
192 if (request == QAT_COMP_REQUEST_FIXED_COMP_STATELESS)
193 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_STATIC;
194 else if (request == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS)
195 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DYNAMIC;
196 else if (request == QAT_COMP_REQUEST_DECOMPRESS)
197 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DECOMPRESS;
199 header->service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_COMP;
201 ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
203 header->comn_req_flags = ICP_QAT_FW_COMN_FLAGS_BUILD(
204 QAT_COMN_CD_FLD_TYPE_16BYTE_DATA, QAT_COMN_PTR_TYPE_FLAT);
207 static int qat_comp_create_templates(struct qat_comp_xform *qat_xform,
208 const struct rte_memzone *interm_buff_mz,
209 const struct rte_comp_xform *xform)
211 struct icp_qat_fw_comp_req *comp_req;
212 int comp_level, algo;
213 uint32_t req_par_flags;
214 int direction = ICP_QAT_HW_COMPRESSION_DIR_COMPRESS;
216 if (unlikely(qat_xform == NULL)) {
217 QAT_LOG(ERR, "Session was not created for this device");
221 if (qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
222 direction = ICP_QAT_HW_COMPRESSION_DIR_DECOMPRESS;
223 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
224 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
225 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
226 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_NO_CNV,
227 ICP_QAT_FW_COMP_NO_CNV_RECOVERY);
230 if (xform->compress.level == RTE_COMP_LEVEL_PMD_DEFAULT)
231 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
232 else if (xform->compress.level == 1)
233 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
234 else if (xform->compress.level == 2)
235 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_4;
236 else if (xform->compress.level == 3)
237 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
238 else if (xform->compress.level >= 4 &&
239 xform->compress.level <= 9)
240 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_16;
242 QAT_LOG(ERR, "compression level not supported");
245 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
246 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
247 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_CNV,
248 ICP_QAT_FW_COMP_CNV_RECOVERY);
251 switch (xform->compress.algo) {
252 case RTE_COMP_ALGO_DEFLATE:
253 algo = ICP_QAT_HW_COMPRESSION_ALGO_DEFLATE;
255 case RTE_COMP_ALGO_LZS:
258 QAT_LOG(ERR, "compression algorithm not supported");
262 comp_req = &qat_xform->qat_comp_req_tmpl;
264 /* Initialize header */
265 qat_comp_create_req_hdr(&comp_req->comn_hdr,
266 qat_xform->qat_comp_request_type);
268 comp_req->comn_hdr.serv_specif_flags = ICP_QAT_FW_COMP_FLAGS_BUILD(
269 ICP_QAT_FW_COMP_STATELESS_SESSION,
270 ICP_QAT_FW_COMP_NOT_AUTO_SELECT_BEST,
271 ICP_QAT_FW_COMP_NOT_ENH_AUTO_SELECT_BEST,
272 ICP_QAT_FW_COMP_NOT_DISABLE_TYPE0_ENH_AUTO_SELECT_BEST,
273 ICP_QAT_FW_COMP_DISABLE_SECURE_RAM_USED_AS_INTMD_BUF);
275 comp_req->cd_pars.sl.comp_slice_cfg_word[0] =
276 ICP_QAT_HW_COMPRESSION_CONFIG_BUILD(
278 /* In CPM 1.6 only valid mode ! */
279 ICP_QAT_HW_COMPRESSION_DELAYED_MATCH_ENABLED, algo,
280 /* Translate level to depth */
281 comp_level, ICP_QAT_HW_COMPRESSION_FILE_TYPE_0);
283 comp_req->comp_pars.initial_adler = 1;
284 comp_req->comp_pars.initial_crc32 = 0;
285 comp_req->comp_pars.req_par_flags = req_par_flags;
288 if (qat_xform->qat_comp_request_type ==
289 QAT_COMP_REQUEST_FIXED_COMP_STATELESS ||
290 qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
291 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
292 ICP_QAT_FW_SLICE_DRAM_WR);
293 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
294 ICP_QAT_FW_SLICE_COMP);
295 } else if (qat_xform->qat_comp_request_type ==
296 QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS) {
298 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
299 ICP_QAT_FW_SLICE_XLAT);
300 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
301 ICP_QAT_FW_SLICE_COMP);
303 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->u2.xlt_cd_ctrl,
304 ICP_QAT_FW_SLICE_DRAM_WR);
305 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->u2.xlt_cd_ctrl,
306 ICP_QAT_FW_SLICE_XLAT);
308 comp_req->u1.xlt_pars.inter_buff_ptr =
309 interm_buff_mz->phys_addr;
312 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
313 QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message template:", comp_req,
314 sizeof(struct icp_qat_fw_comp_req));
320 * Create driver private_xform data.
325 * xform data from application
326 * @param private_xform
327 * ptr where handle of pmd's private_xform data should be stored
329 * - if successful returns 0
330 * and valid private_xform handle
331 * - <0 in error cases
332 * - Returns -EINVAL if input parameters are invalid.
333 * - Returns -ENOTSUP if comp device does not support the comp transform.
334 * - Returns -ENOMEM if the private_xform could not be allocated.
337 qat_comp_private_xform_create(struct rte_compressdev *dev,
338 const struct rte_comp_xform *xform,
339 void **private_xform)
341 struct qat_comp_dev_private *qat = dev->data->dev_private;
343 if (unlikely(private_xform == NULL)) {
344 QAT_LOG(ERR, "QAT: private_xform parameter is NULL");
347 if (unlikely(qat->xformpool == NULL)) {
348 QAT_LOG(ERR, "QAT device has no private_xform mempool");
351 if (rte_mempool_get(qat->xformpool, private_xform)) {
352 QAT_LOG(ERR, "Couldn't get object from qat xform mempool");
356 struct qat_comp_xform *qat_xform =
357 (struct qat_comp_xform *)*private_xform;
359 if (xform->type == RTE_COMP_COMPRESS) {
361 if (xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_FIXED ||
362 ((xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_DEFAULT)
363 && qat->interm_buff_mz == NULL))
364 qat_xform->qat_comp_request_type =
365 QAT_COMP_REQUEST_FIXED_COMP_STATELESS;
367 else if ((xform->compress.deflate.huffman ==
368 RTE_COMP_HUFFMAN_DYNAMIC ||
369 xform->compress.deflate.huffman ==
370 RTE_COMP_HUFFMAN_DEFAULT) &&
371 qat->interm_buff_mz != NULL)
373 qat_xform->qat_comp_request_type =
374 QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS;
378 "IM buffers needed for dynamic deflate. Set size in config file");
382 qat_xform->checksum_type = xform->compress.chksum;
385 qat_xform->qat_comp_request_type = QAT_COMP_REQUEST_DECOMPRESS;
386 qat_xform->checksum_type = xform->decompress.chksum;
389 if (qat_comp_create_templates(qat_xform, qat->interm_buff_mz, xform)) {
390 QAT_LOG(ERR, "QAT: Problem with setting compression");
397 * Free driver private_xform data.
401 * @param private_xform
402 * handle of pmd's private_xform data
405 * - <0 in error cases
406 * - Returns -EINVAL if input parameters are invalid.
409 qat_comp_private_xform_free(struct rte_compressdev *dev __rte_unused,
412 struct qat_comp_xform *qat_xform =
413 (struct qat_comp_xform *)private_xform;
416 memset(qat_xform, 0, qat_comp_xform_size());
417 struct rte_mempool *mp = rte_mempool_from_obj(qat_xform);
419 rte_mempool_put(mp, qat_xform);