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)
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");
142 if ((ICP_QAT_FW_COMN_RESP_CMP_STAT_GET(resp_msg->comn_resp.comn_status)
143 | ICP_QAT_FW_COMN_RESP_XLAT_STAT_GET(
144 resp_msg->comn_resp.comn_status)) !=
145 ICP_QAT_FW_COMN_STATUS_FLAG_OK) {
147 if (unlikely((ICP_QAT_FW_COMN_RESP_XLAT_STAT_GET(
148 resp_msg->comn_resp.comn_status) !=
149 ICP_QAT_FW_COMN_STATUS_FLAG_OK) &&
150 (qat_xform->qat_comp_request_type
151 == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS)))
152 QAT_DP_LOG(ERR, "QAT intermediate buffer may be too "
153 "small for output, try configuring a larger size");
155 rx_op->status = RTE_COMP_OP_STATUS_ERROR;
156 rx_op->debug_status =
157 *((uint16_t *)(&resp_msg->comn_resp.comn_error));
159 struct icp_qat_fw_resp_comp_pars *comp_resp =
160 (struct icp_qat_fw_resp_comp_pars *)&resp_msg->comp_resp_pars;
162 rx_op->status = RTE_COMP_OP_STATUS_SUCCESS;
163 rx_op->consumed = comp_resp->input_byte_counter;
164 rx_op->produced = comp_resp->output_byte_counter;
166 if (qat_xform->checksum_type != RTE_COMP_CHECKSUM_NONE) {
167 if (qat_xform->checksum_type == RTE_COMP_CHECKSUM_CRC32)
168 rx_op->output_chksum = comp_resp->curr_crc32;
169 else if (qat_xform->checksum_type ==
170 RTE_COMP_CHECKSUM_ADLER32)
171 rx_op->output_chksum = comp_resp->curr_adler_32;
173 rx_op->output_chksum = comp_resp->curr_chksum;
182 qat_comp_xform_size(void)
184 return RTE_ALIGN_CEIL(sizeof(struct qat_comp_xform), 8);
187 static void qat_comp_create_req_hdr(struct icp_qat_fw_comn_req_hdr *header,
188 enum qat_comp_request_type request)
190 if (request == QAT_COMP_REQUEST_FIXED_COMP_STATELESS)
191 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_STATIC;
192 else if (request == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS)
193 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DYNAMIC;
194 else if (request == QAT_COMP_REQUEST_DECOMPRESS)
195 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DECOMPRESS;
197 header->service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_COMP;
199 ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
201 header->comn_req_flags = ICP_QAT_FW_COMN_FLAGS_BUILD(
202 QAT_COMN_CD_FLD_TYPE_16BYTE_DATA, QAT_COMN_PTR_TYPE_FLAT);
205 static int qat_comp_create_templates(struct qat_comp_xform *qat_xform,
206 const struct rte_memzone *interm_buff_mz,
207 const struct rte_comp_xform *xform)
209 struct icp_qat_fw_comp_req *comp_req;
210 int comp_level, algo;
211 uint32_t req_par_flags;
212 int direction = ICP_QAT_HW_COMPRESSION_DIR_COMPRESS;
214 if (unlikely(qat_xform == NULL)) {
215 QAT_LOG(ERR, "Session was not created for this device");
219 if (qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
220 direction = ICP_QAT_HW_COMPRESSION_DIR_DECOMPRESS;
221 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
222 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
223 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
224 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_NO_CNV,
225 ICP_QAT_FW_COMP_NO_CNV_RECOVERY);
228 if (xform->compress.level == RTE_COMP_LEVEL_PMD_DEFAULT)
229 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
230 else if (xform->compress.level == 1)
231 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
232 else if (xform->compress.level == 2)
233 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_4;
234 else if (xform->compress.level == 3)
235 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
236 else if (xform->compress.level >= 4 &&
237 xform->compress.level <= 9)
238 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_16;
240 QAT_LOG(ERR, "compression level not supported");
243 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
244 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
245 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_CNV,
246 ICP_QAT_FW_COMP_CNV_RECOVERY);
249 switch (xform->compress.algo) {
250 case RTE_COMP_ALGO_DEFLATE:
251 algo = ICP_QAT_HW_COMPRESSION_ALGO_DEFLATE;
253 case RTE_COMP_ALGO_LZS:
256 QAT_LOG(ERR, "compression algorithm not supported");
260 comp_req = &qat_xform->qat_comp_req_tmpl;
262 /* Initialize header */
263 qat_comp_create_req_hdr(&comp_req->comn_hdr,
264 qat_xform->qat_comp_request_type);
266 comp_req->comn_hdr.serv_specif_flags = ICP_QAT_FW_COMP_FLAGS_BUILD(
267 ICP_QAT_FW_COMP_STATELESS_SESSION,
268 ICP_QAT_FW_COMP_NOT_AUTO_SELECT_BEST,
269 ICP_QAT_FW_COMP_NOT_ENH_AUTO_SELECT_BEST,
270 ICP_QAT_FW_COMP_NOT_DISABLE_TYPE0_ENH_AUTO_SELECT_BEST,
271 ICP_QAT_FW_COMP_DISABLE_SECURE_RAM_USED_AS_INTMD_BUF);
273 comp_req->cd_pars.sl.comp_slice_cfg_word[0] =
274 ICP_QAT_HW_COMPRESSION_CONFIG_BUILD(
276 /* In CPM 1.6 only valid mode ! */
277 ICP_QAT_HW_COMPRESSION_DELAYED_MATCH_ENABLED, algo,
278 /* Translate level to depth */
279 comp_level, ICP_QAT_HW_COMPRESSION_FILE_TYPE_0);
281 comp_req->comp_pars.initial_adler = 1;
282 comp_req->comp_pars.initial_crc32 = 0;
283 comp_req->comp_pars.req_par_flags = req_par_flags;
286 if (qat_xform->qat_comp_request_type ==
287 QAT_COMP_REQUEST_FIXED_COMP_STATELESS ||
288 qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
289 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
290 ICP_QAT_FW_SLICE_DRAM_WR);
291 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
292 ICP_QAT_FW_SLICE_COMP);
293 } else if (qat_xform->qat_comp_request_type ==
294 QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS) {
296 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
297 ICP_QAT_FW_SLICE_XLAT);
298 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
299 ICP_QAT_FW_SLICE_COMP);
301 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->u2.xlt_cd_ctrl,
302 ICP_QAT_FW_SLICE_DRAM_WR);
303 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->u2.xlt_cd_ctrl,
304 ICP_QAT_FW_SLICE_XLAT);
306 comp_req->u1.xlt_pars.inter_buff_ptr =
307 interm_buff_mz->phys_addr;
310 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
311 QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message template:", comp_req,
312 sizeof(struct icp_qat_fw_comp_req));
318 * Create driver private_xform data.
323 * xform data from application
324 * @param private_xform
325 * ptr where handle of pmd's private_xform data should be stored
327 * - if successful returns 0
328 * and valid private_xform handle
329 * - <0 in error cases
330 * - Returns -EINVAL if input parameters are invalid.
331 * - Returns -ENOTSUP if comp device does not support the comp transform.
332 * - Returns -ENOMEM if the private_xform could not be allocated.
335 qat_comp_private_xform_create(struct rte_compressdev *dev,
336 const struct rte_comp_xform *xform,
337 void **private_xform)
339 struct qat_comp_dev_private *qat = dev->data->dev_private;
341 if (unlikely(private_xform == NULL)) {
342 QAT_LOG(ERR, "QAT: private_xform parameter is NULL");
345 if (unlikely(qat->xformpool == NULL)) {
346 QAT_LOG(ERR, "QAT device has no private_xform mempool");
349 if (rte_mempool_get(qat->xformpool, private_xform)) {
350 QAT_LOG(ERR, "Couldn't get object from qat xform mempool");
354 struct qat_comp_xform *qat_xform =
355 (struct qat_comp_xform *)*private_xform;
357 if (xform->type == RTE_COMP_COMPRESS) {
359 if (xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_FIXED ||
360 ((xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_DEFAULT)
361 && qat->interm_buff_mz == NULL))
362 qat_xform->qat_comp_request_type =
363 QAT_COMP_REQUEST_FIXED_COMP_STATELESS;
365 else if ((xform->compress.deflate.huffman ==
366 RTE_COMP_HUFFMAN_DYNAMIC ||
367 xform->compress.deflate.huffman ==
368 RTE_COMP_HUFFMAN_DEFAULT) &&
369 qat->interm_buff_mz != NULL)
371 qat_xform->qat_comp_request_type =
372 QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS;
376 "IM buffers needed for dynamic deflate. Set size in config file");
380 qat_xform->checksum_type = xform->compress.chksum;
383 qat_xform->qat_comp_request_type = QAT_COMP_REQUEST_DECOMPRESS;
384 qat_xform->checksum_type = xform->decompress.chksum;
387 if (qat_comp_create_templates(qat_xform, qat->interm_buff_mz, xform)) {
388 QAT_LOG(ERR, "QAT: Problem with setting compression");
395 * Free driver private_xform data.
399 * @param private_xform
400 * handle of pmd's private_xform data
403 * - <0 in error cases
404 * - Returns -EINVAL if input parameters are invalid.
407 qat_comp_private_xform_free(struct rte_compressdev *dev __rte_unused,
410 struct qat_comp_xform *qat_xform =
411 (struct qat_comp_xform *)private_xform;
414 memset(qat_xform, 0, qat_comp_xform_size());
415 struct rte_mempool *mp = rte_mempool_from_obj(qat_xform);
417 rte_mempool_put(mp, qat_xform);