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);
42 rte_mov128(out_msg, tmpl);
43 comp_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
45 /* common for sgl and flat buffers */
46 comp_req->comp_pars.comp_len = op->src.length;
47 comp_req->comp_pars.out_buffer_sz = rte_pktmbuf_pkt_len(op->m_dst) -
50 if (op->m_src->next != NULL || op->m_dst->next != NULL) {
54 ICP_QAT_FW_COMN_PTR_TYPE_SET(comp_req->comn_hdr.comn_req_flags,
55 QAT_COMN_PTR_TYPE_SGL);
57 ret = qat_sgl_fill_array(op->m_src,
61 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
63 QAT_DP_LOG(ERR, "QAT PMD Cannot fill source sgl array");
67 ret = qat_sgl_fill_array(op->m_dst,
70 comp_req->comp_pars.out_buffer_sz,
71 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
73 QAT_DP_LOG(ERR, "QAT PMD Cannot fill dest. sgl array");
77 comp_req->comn_mid.src_data_addr =
78 cookie->qat_sgl_src_phys_addr;
79 comp_req->comn_mid.dest_data_addr =
80 cookie->qat_sgl_dst_phys_addr;
81 comp_req->comn_mid.src_length = 0;
82 comp_req->comn_mid.dst_length = 0;
85 /* flat aka linear buffer */
86 ICP_QAT_FW_COMN_PTR_TYPE_SET(comp_req->comn_hdr.comn_req_flags,
87 QAT_COMN_PTR_TYPE_FLAT);
88 comp_req->comn_mid.src_length = op->src.length;
89 comp_req->comn_mid.dst_length =
90 comp_req->comp_pars.out_buffer_sz;
92 comp_req->comn_mid.src_data_addr =
93 rte_pktmbuf_mtophys_offset(op->m_src, op->src.offset);
94 comp_req->comn_mid.dest_data_addr =
95 rte_pktmbuf_mtophys_offset(op->m_dst, op->dst.offset);
98 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
99 QAT_DP_LOG(DEBUG, "Direction: %s",
100 qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS ?
101 "decompression" : "compression");
102 QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message:", comp_req,
103 sizeof(struct icp_qat_fw_comp_req));
109 qat_comp_process_response(void **op, uint8_t *resp)
111 struct icp_qat_fw_comp_resp *resp_msg =
112 (struct icp_qat_fw_comp_resp *)resp;
113 struct rte_comp_op *rx_op = (struct rte_comp_op *)(uintptr_t)
114 (resp_msg->opaque_data);
115 struct qat_comp_xform *qat_xform = (struct qat_comp_xform *)
116 (rx_op->private_xform);
118 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
119 QAT_DP_LOG(DEBUG, "Direction: %s",
120 qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS ?
121 "decompression" : "compression");
122 QAT_DP_HEXDUMP_LOG(DEBUG, "qat_response:", (uint8_t *)resp_msg,
123 sizeof(struct icp_qat_fw_comp_resp));
126 if (likely(qat_xform->qat_comp_request_type
127 != QAT_COMP_REQUEST_DECOMPRESS)) {
128 if (unlikely(ICP_QAT_FW_COMN_HDR_CNV_FLAG_GET(
129 resp_msg->comn_resp.hdr_flags)
130 == ICP_QAT_FW_COMP_NO_CNV)) {
131 rx_op->status = RTE_COMP_OP_STATUS_ERROR;
132 rx_op->debug_status = ERR_CODE_QAT_COMP_WRONG_FW;
134 QAT_DP_LOG(ERR, "QAT has wrong firmware");
139 if ((ICP_QAT_FW_COMN_RESP_CMP_STAT_GET(resp_msg->comn_resp.comn_status)
140 | ICP_QAT_FW_COMN_RESP_XLAT_STAT_GET(
141 resp_msg->comn_resp.comn_status)) !=
142 ICP_QAT_FW_COMN_STATUS_FLAG_OK) {
144 rx_op->status = RTE_COMP_OP_STATUS_ERROR;
145 rx_op->debug_status =
146 *((uint16_t *)(&resp_msg->comn_resp.comn_error));
148 struct qat_comp_xform *qat_xform = rx_op->private_xform;
149 struct icp_qat_fw_resp_comp_pars *comp_resp =
150 (struct icp_qat_fw_resp_comp_pars *)&resp_msg->comp_resp_pars;
152 rx_op->status = RTE_COMP_OP_STATUS_SUCCESS;
153 rx_op->consumed = comp_resp->input_byte_counter;
154 rx_op->produced = comp_resp->output_byte_counter;
156 if (qat_xform->checksum_type != RTE_COMP_CHECKSUM_NONE) {
157 if (qat_xform->checksum_type == RTE_COMP_CHECKSUM_CRC32)
158 rx_op->output_chksum = comp_resp->curr_crc32;
159 else if (qat_xform->checksum_type ==
160 RTE_COMP_CHECKSUM_ADLER32)
161 rx_op->output_chksum = comp_resp->curr_adler_32;
163 rx_op->output_chksum = comp_resp->curr_chksum;
172 qat_comp_xform_size(void)
174 return RTE_ALIGN_CEIL(sizeof(struct qat_comp_xform), 8);
177 static void qat_comp_create_req_hdr(struct icp_qat_fw_comn_req_hdr *header,
178 enum qat_comp_request_type request)
180 if (request == QAT_COMP_REQUEST_FIXED_COMP_STATELESS)
181 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_STATIC;
182 else if (request == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS)
183 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DYNAMIC;
184 else if (request == QAT_COMP_REQUEST_DECOMPRESS)
185 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DECOMPRESS;
187 header->service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_COMP;
189 ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
191 header->comn_req_flags = ICP_QAT_FW_COMN_FLAGS_BUILD(
192 QAT_COMN_CD_FLD_TYPE_16BYTE_DATA, QAT_COMN_PTR_TYPE_FLAT);
195 static int qat_comp_create_templates(struct qat_comp_xform *qat_xform,
196 const struct rte_memzone *interm_buff_mz __rte_unused,
197 const struct rte_comp_xform *xform)
199 struct icp_qat_fw_comp_req *comp_req;
200 int comp_level, algo;
201 uint32_t req_par_flags;
202 int direction = ICP_QAT_HW_COMPRESSION_DIR_COMPRESS;
204 if (unlikely(qat_xform == NULL)) {
205 QAT_LOG(ERR, "Session was not created for this device");
209 if (qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
210 direction = ICP_QAT_HW_COMPRESSION_DIR_DECOMPRESS;
211 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
212 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
213 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
214 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_NO_CNV,
215 ICP_QAT_FW_COMP_NO_CNV_RECOVERY);
218 if (xform->compress.level == RTE_COMP_LEVEL_PMD_DEFAULT)
219 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
220 else if (xform->compress.level == 1)
221 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
222 else if (xform->compress.level == 2)
223 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_4;
224 else if (xform->compress.level == 3)
225 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
226 else if (xform->compress.level >= 4 &&
227 xform->compress.level <= 9)
228 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_16;
230 QAT_LOG(ERR, "compression level not supported");
233 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
234 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
235 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_CNV,
236 ICP_QAT_FW_COMP_CNV_RECOVERY);
239 switch (xform->compress.algo) {
240 case RTE_COMP_ALGO_DEFLATE:
241 algo = ICP_QAT_HW_COMPRESSION_ALGO_DEFLATE;
243 case RTE_COMP_ALGO_LZS:
246 QAT_LOG(ERR, "compression algorithm not supported");
250 comp_req = &qat_xform->qat_comp_req_tmpl;
252 /* Initialize header */
253 qat_comp_create_req_hdr(&comp_req->comn_hdr,
254 qat_xform->qat_comp_request_type);
256 comp_req->comn_hdr.serv_specif_flags = ICP_QAT_FW_COMP_FLAGS_BUILD(
257 ICP_QAT_FW_COMP_STATELESS_SESSION,
258 ICP_QAT_FW_COMP_NOT_AUTO_SELECT_BEST,
259 ICP_QAT_FW_COMP_NOT_ENH_AUTO_SELECT_BEST,
260 ICP_QAT_FW_COMP_NOT_DISABLE_TYPE0_ENH_AUTO_SELECT_BEST,
261 ICP_QAT_FW_COMP_DISABLE_SECURE_RAM_USED_AS_INTMD_BUF);
263 comp_req->cd_pars.sl.comp_slice_cfg_word[0] =
264 ICP_QAT_HW_COMPRESSION_CONFIG_BUILD(
266 /* In CPM 1.6 only valid mode ! */
267 ICP_QAT_HW_COMPRESSION_DELAYED_MATCH_ENABLED, algo,
268 /* Translate level to depth */
269 comp_level, ICP_QAT_HW_COMPRESSION_FILE_TYPE_0);
271 comp_req->comp_pars.initial_adler = 1;
272 comp_req->comp_pars.initial_crc32 = 0;
273 comp_req->comp_pars.req_par_flags = req_par_flags;
276 if (qat_xform->qat_comp_request_type ==
277 QAT_COMP_REQUEST_FIXED_COMP_STATELESS ||
278 qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
279 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
280 ICP_QAT_FW_SLICE_DRAM_WR);
281 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
282 ICP_QAT_FW_SLICE_COMP);
283 } else if (qat_xform->qat_comp_request_type ==
284 QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS) {
286 QAT_LOG(ERR, "Dynamic huffman encoding not supported");
290 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
291 QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message template:", comp_req,
292 sizeof(struct icp_qat_fw_comp_req));
298 * Create driver private_xform data.
303 * xform data from application
304 * @param private_xform
305 * ptr where handle of pmd's private_xform data should be stored
307 * - if successful returns 0
308 * and valid private_xform handle
309 * - <0 in error cases
310 * - Returns -EINVAL if input parameters are invalid.
311 * - Returns -ENOTSUP if comp device does not support the comp transform.
312 * - Returns -ENOMEM if the private_xform could not be allocated.
315 qat_comp_private_xform_create(struct rte_compressdev *dev,
316 const struct rte_comp_xform *xform,
317 void **private_xform)
319 struct qat_comp_dev_private *qat = dev->data->dev_private;
321 if (unlikely(private_xform == NULL)) {
322 QAT_LOG(ERR, "QAT: private_xform parameter is NULL");
325 if (unlikely(qat->xformpool == NULL)) {
326 QAT_LOG(ERR, "QAT device has no private_xform mempool");
329 if (rte_mempool_get(qat->xformpool, private_xform)) {
330 QAT_LOG(ERR, "Couldn't get object from qat xform mempool");
334 struct qat_comp_xform *qat_xform =
335 (struct qat_comp_xform *)*private_xform;
337 if (xform->type == RTE_COMP_COMPRESS) {
338 if (xform->compress.deflate.huffman ==
339 RTE_COMP_HUFFMAN_DYNAMIC) {
341 "QAT device doesn't support dynamic compression");
345 if (xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_FIXED ||
346 ((xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_DEFAULT)
347 && qat->interm_buff_mz == NULL))
349 qat_xform->qat_comp_request_type =
350 QAT_COMP_REQUEST_FIXED_COMP_STATELESS;
354 qat_xform->qat_comp_request_type = QAT_COMP_REQUEST_DECOMPRESS;
357 qat_xform->checksum_type = xform->compress.chksum;
359 if (qat_comp_create_templates(qat_xform, qat->interm_buff_mz, xform)) {
360 QAT_LOG(ERR, "QAT: Problem with setting compression");
367 * Free driver private_xform data.
371 * @param private_xform
372 * handle of pmd's private_xform data
375 * - <0 in error cases
376 * - Returns -EINVAL if input parameters are invalid.
379 qat_comp_private_xform_free(struct rte_compressdev *dev __rte_unused,
382 struct qat_comp_xform *qat_xform =
383 (struct qat_comp_xform *)private_xform;
386 memset(qat_xform, 0, qat_comp_xform_size());
387 struct rte_mempool *mp = rte_mempool_from_obj(qat_xform);
389 rte_mempool_put(mp, qat_xform);