522edfc78c38fe8fb155a45123349d495bcd9072
[dpdk.git] / drivers / compress / qat / qat_comp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4
5 #include <rte_mempool.h>
6 #include <rte_mbuf.h>
7 #include <rte_hexdump.h>
8 #include <rte_comp.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>
14 #include <rte_log.h>
15 #include <rte_malloc.h>
16
17 #include "qat_logs.h"
18 #include "qat_comp.h"
19 #include "qat_comp_pmd.h"
20
21
22 int
23 qat_comp_build_request(void *in_op, uint8_t *out_msg,
24                        void *op_cookie,
25                        enum qat_device_gen qat_dev_gen __rte_unused)
26 {
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;
34
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                 return -EINVAL;
40         }
41
42         rte_mov128(out_msg, tmpl);
43         comp_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
44
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) -
48                         op->dst.offset;
49
50         if (op->m_src->next != NULL || op->m_dst->next != NULL) {
51                 /* sgl */
52                 int ret = 0;
53
54                 ICP_QAT_FW_COMN_PTR_TYPE_SET(comp_req->comn_hdr.comn_req_flags,
55                                 QAT_COMN_PTR_TYPE_SGL);
56                 ret = qat_sgl_fill_array(op->m_src,
57                                 rte_pktmbuf_mtophys_offset(op->m_src,
58                                                         op->src.offset),
59                                 &cookie->qat_sgl_src,
60                                 op->src.length,
61                                 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
62                 if (ret) {
63                         QAT_DP_LOG(ERR, "QAT PMD Cannot fill sgl array");
64                         return ret;
65                 }
66
67                 ret = qat_sgl_fill_array(op->m_dst,
68                                 rte_pktmbuf_mtophys_offset(op->m_dst,
69                                                         op->dst.offset),
70                                 &cookie->qat_sgl_dst,
71                                 comp_req->comp_pars.out_buffer_sz,
72                                 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
73                 if (ret) {
74                         QAT_DP_LOG(ERR, "QAT PMD Cannot fill sgl array");
75                         return ret;
76                 }
77
78                 comp_req->comn_mid.src_data_addr =
79                                 cookie->qat_sgl_src_phys_addr;
80                 comp_req->comn_mid.dest_data_addr =
81                                 cookie->qat_sgl_dst_phys_addr;
82                 comp_req->comn_mid.src_length = 0;
83                 comp_req->comn_mid.dst_length = 0;
84
85         } else {
86                 /* flat aka linear buffer */
87                 ICP_QAT_FW_COMN_PTR_TYPE_SET(comp_req->comn_hdr.comn_req_flags,
88                                 QAT_COMN_PTR_TYPE_FLAT);
89                 comp_req->comn_mid.src_length = op->src.length;
90                 comp_req->comn_mid.dst_length =
91                                 comp_req->comp_pars.out_buffer_sz;
92
93                 comp_req->comn_mid.src_data_addr =
94                     rte_pktmbuf_mtophys_offset(op->m_src, op->src.offset);
95                 comp_req->comn_mid.dest_data_addr =
96                     rte_pktmbuf_mtophys_offset(op->m_dst, op->dst.offset);
97         }
98
99 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
100         QAT_DP_LOG(DEBUG, "Direction: %s",
101             qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS ?
102                             "decompression" : "compression");
103         QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message:", comp_req,
104                     sizeof(struct icp_qat_fw_comp_req));
105 #endif
106         return 0;
107 }
108
109 int
110 qat_comp_process_response(void **op, uint8_t *resp)
111 {
112         struct icp_qat_fw_comp_resp *resp_msg =
113                         (struct icp_qat_fw_comp_resp *)resp;
114         struct rte_comp_op *rx_op = (struct rte_comp_op *)(uintptr_t)
115                         (resp_msg->opaque_data);
116         struct qat_comp_xform *qat_xform = (struct qat_comp_xform *)
117                                 (rx_op->private_xform);
118
119 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
120         QAT_DP_LOG(DEBUG, "Direction: %s",
121             qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS ?
122             "decompression" : "compression");
123         QAT_DP_HEXDUMP_LOG(DEBUG,  "qat_response:", (uint8_t *)resp_msg,
124                         sizeof(struct icp_qat_fw_comp_resp));
125 #endif
126
127         if (likely(qat_xform->qat_comp_request_type
128                         != QAT_COMP_REQUEST_DECOMPRESS)) {
129                 if (unlikely(ICP_QAT_FW_COMN_HDR_CNV_FLAG_GET(
130                                 resp_msg->comn_resp.hdr_flags)
131                                         == ICP_QAT_FW_COMP_NO_CNV)) {
132                         rx_op->status = RTE_COMP_OP_STATUS_ERROR;
133                         rx_op->debug_status = ERR_CODE_QAT_COMP_WRONG_FW;
134                         *op = (void *)rx_op;
135                         QAT_DP_LOG(ERR, "QAT has wrong firmware");
136                         return 0;
137                 }
138         }
139
140         if ((ICP_QAT_FW_COMN_RESP_CMP_STAT_GET(resp_msg->comn_resp.comn_status)
141                 | ICP_QAT_FW_COMN_RESP_XLAT_STAT_GET(
142                                 resp_msg->comn_resp.comn_status)) !=
143                                 ICP_QAT_FW_COMN_STATUS_FLAG_OK) {
144
145                 rx_op->status = RTE_COMP_OP_STATUS_ERROR;
146                 rx_op->debug_status =
147                         *((uint16_t *)(&resp_msg->comn_resp.comn_error));
148         } else {
149                 struct qat_comp_xform *qat_xform = rx_op->private_xform;
150                 struct icp_qat_fw_resp_comp_pars *comp_resp =
151                   (struct icp_qat_fw_resp_comp_pars *)&resp_msg->comp_resp_pars;
152
153                 rx_op->status = RTE_COMP_OP_STATUS_SUCCESS;
154                 rx_op->consumed = comp_resp->input_byte_counter;
155                 rx_op->produced = comp_resp->output_byte_counter;
156
157                 if (qat_xform->checksum_type != RTE_COMP_CHECKSUM_NONE) {
158                         if (qat_xform->checksum_type == RTE_COMP_CHECKSUM_CRC32)
159                                 rx_op->output_chksum = comp_resp->curr_crc32;
160                         else if (qat_xform->checksum_type ==
161                                         RTE_COMP_CHECKSUM_ADLER32)
162                                 rx_op->output_chksum = comp_resp->curr_adler_32;
163                         else
164                                 rx_op->output_chksum = comp_resp->curr_chksum;
165                 }
166         }
167         *op = (void *)rx_op;
168
169         return 0;
170 }
171
172 unsigned int
173 qat_comp_xform_size(void)
174 {
175         return RTE_ALIGN_CEIL(sizeof(struct qat_comp_xform), 8);
176 }
177
178 static void qat_comp_create_req_hdr(struct icp_qat_fw_comn_req_hdr *header,
179                                     enum qat_comp_request_type request)
180 {
181         if (request == QAT_COMP_REQUEST_FIXED_COMP_STATELESS)
182                 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_STATIC;
183         else if (request == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS)
184                 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DYNAMIC;
185         else if (request == QAT_COMP_REQUEST_DECOMPRESS)
186                 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DECOMPRESS;
187
188         header->service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_COMP;
189         header->hdr_flags =
190             ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
191
192         header->comn_req_flags = ICP_QAT_FW_COMN_FLAGS_BUILD(
193             QAT_COMN_CD_FLD_TYPE_16BYTE_DATA, QAT_COMN_PTR_TYPE_FLAT);
194 }
195
196 static int qat_comp_create_templates(struct qat_comp_xform *qat_xform,
197                         const struct rte_memzone *interm_buff_mz __rte_unused,
198                         const struct rte_comp_xform *xform)
199 {
200         struct icp_qat_fw_comp_req *comp_req;
201         int comp_level, algo;
202         uint32_t req_par_flags;
203         int direction = ICP_QAT_HW_COMPRESSION_DIR_COMPRESS;
204
205         if (unlikely(qat_xform == NULL)) {
206                 QAT_LOG(ERR, "Session was not created for this device");
207                 return -EINVAL;
208         }
209
210         if (qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
211                 direction = ICP_QAT_HW_COMPRESSION_DIR_DECOMPRESS;
212                 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
213                 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
214                                 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
215                                 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_NO_CNV,
216                                 ICP_QAT_FW_COMP_NO_CNV_RECOVERY);
217
218         } else {
219                 if (xform->compress.level == RTE_COMP_LEVEL_PMD_DEFAULT)
220                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
221                 else if (xform->compress.level == 1)
222                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
223                 else if (xform->compress.level == 2)
224                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_4;
225                 else if (xform->compress.level == 3)
226                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
227                 else if (xform->compress.level >= 4 &&
228                          xform->compress.level <= 9)
229                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_16;
230                 else {
231                         QAT_LOG(ERR, "compression level not supported");
232                         return -EINVAL;
233                 }
234                 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
235                                 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
236                                 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_CNV,
237                                 ICP_QAT_FW_COMP_CNV_RECOVERY);
238         }
239
240         switch (xform->compress.algo) {
241         case RTE_COMP_ALGO_DEFLATE:
242                 algo = ICP_QAT_HW_COMPRESSION_ALGO_DEFLATE;
243                 break;
244         case RTE_COMP_ALGO_LZS:
245         default:
246                 /* RTE_COMP_NULL */
247                 QAT_LOG(ERR, "compression algorithm not supported");
248                 return -EINVAL;
249         }
250
251         comp_req = &qat_xform->qat_comp_req_tmpl;
252
253         /* Initialize header */
254         qat_comp_create_req_hdr(&comp_req->comn_hdr,
255                                         qat_xform->qat_comp_request_type);
256
257         comp_req->comn_hdr.serv_specif_flags = ICP_QAT_FW_COMP_FLAGS_BUILD(
258             ICP_QAT_FW_COMP_STATELESS_SESSION,
259             ICP_QAT_FW_COMP_NOT_AUTO_SELECT_BEST,
260             ICP_QAT_FW_COMP_NOT_ENH_AUTO_SELECT_BEST,
261             ICP_QAT_FW_COMP_NOT_DISABLE_TYPE0_ENH_AUTO_SELECT_BEST,
262             ICP_QAT_FW_COMP_DISABLE_SECURE_RAM_USED_AS_INTMD_BUF);
263
264         comp_req->cd_pars.sl.comp_slice_cfg_word[0] =
265             ICP_QAT_HW_COMPRESSION_CONFIG_BUILD(
266                 direction,
267                 /* In CPM 1.6 only valid mode ! */
268                 ICP_QAT_HW_COMPRESSION_DELAYED_MATCH_ENABLED, algo,
269                 /* Translate level to depth */
270                 comp_level, ICP_QAT_HW_COMPRESSION_FILE_TYPE_0);
271
272         comp_req->comp_pars.initial_adler = 1;
273         comp_req->comp_pars.initial_crc32 = 0;
274         comp_req->comp_pars.req_par_flags = req_par_flags;
275
276
277         if (qat_xform->qat_comp_request_type ==
278                         QAT_COMP_REQUEST_FIXED_COMP_STATELESS ||
279             qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
280                 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
281                                             ICP_QAT_FW_SLICE_DRAM_WR);
282                 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
283                                             ICP_QAT_FW_SLICE_COMP);
284         } else if (qat_xform->qat_comp_request_type ==
285                    QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS) {
286
287                 QAT_LOG(ERR, "Dynamic huffman encoding not supported");
288                 return -EINVAL;
289         }
290
291 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
292         QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message template:", comp_req,
293                     sizeof(struct icp_qat_fw_comp_req));
294 #endif
295         return 0;
296 }
297
298 /**
299  * Create driver private_xform data.
300  *
301  * @param dev
302  *   Compressdev device
303  * @param xform
304  *   xform data from application
305  * @param private_xform
306  *   ptr where handle of pmd's private_xform data should be stored
307  * @return
308  *  - if successful returns 0
309  *    and valid private_xform handle
310  *  - <0 in error cases
311  *  - Returns -EINVAL if input parameters are invalid.
312  *  - Returns -ENOTSUP if comp device does not support the comp transform.
313  *  - Returns -ENOMEM if the private_xform could not be allocated.
314  */
315 int
316 qat_comp_private_xform_create(struct rte_compressdev *dev,
317                               const struct rte_comp_xform *xform,
318                               void **private_xform)
319 {
320         struct qat_comp_dev_private *qat = dev->data->dev_private;
321
322         if (unlikely(private_xform == NULL)) {
323                 QAT_LOG(ERR, "QAT: private_xform parameter is NULL");
324                 return -EINVAL;
325         }
326         if (unlikely(qat->xformpool == NULL)) {
327                 QAT_LOG(ERR, "QAT device has no private_xform mempool");
328                 return -ENOMEM;
329         }
330         if (rte_mempool_get(qat->xformpool, private_xform)) {
331                 QAT_LOG(ERR, "Couldn't get object from qat xform mempool");
332                 return -ENOMEM;
333         }
334
335         struct qat_comp_xform *qat_xform =
336                         (struct qat_comp_xform *)*private_xform;
337
338         if (xform->type == RTE_COMP_COMPRESS) {
339                 if (xform->compress.deflate.huffman ==
340                                 RTE_COMP_HUFFMAN_DYNAMIC) {
341                         QAT_LOG(ERR,
342                         "QAT device doesn't support dynamic compression");
343                         return -ENOTSUP;
344                 }
345
346                 if (xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_FIXED ||
347                   ((xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_DEFAULT)
348                                    && qat->interm_buff_mz == NULL))
349
350                         qat_xform->qat_comp_request_type =
351                                         QAT_COMP_REQUEST_FIXED_COMP_STATELESS;
352
353
354         } else {
355                 qat_xform->qat_comp_request_type = QAT_COMP_REQUEST_DECOMPRESS;
356         }
357
358         qat_xform->checksum_type = xform->compress.chksum;
359
360         if (qat_comp_create_templates(qat_xform, qat->interm_buff_mz, xform)) {
361                 QAT_LOG(ERR, "QAT: Problem with setting compression");
362                 return -EINVAL;
363         }
364         return 0;
365 }
366
367 /**
368  * Free driver private_xform data.
369  *
370  * @param dev
371  *   Compressdev device
372  * @param private_xform
373  *   handle of pmd's private_xform data
374  * @return
375  *  - 0 if successful
376  *  - <0 in error cases
377  *  - Returns -EINVAL if input parameters are invalid.
378  */
379 int
380 qat_comp_private_xform_free(struct rte_compressdev *dev __rte_unused,
381                             void *private_xform)
382 {
383         struct qat_comp_xform *qat_xform =
384                         (struct qat_comp_xform *)private_xform;
385
386         if (qat_xform) {
387                 memset(qat_xform, 0, qat_comp_xform_size());
388                 struct rte_mempool *mp = rte_mempool_from_obj(qat_xform);
389
390                 rte_mempool_put(mp, qat_xform);
391                 return 0;
392         }
393         return -EINVAL;
394 }