New upstream version 18.11.1
[deb_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                 op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
40                 return -EINVAL;
41         }
42
43         rte_mov128(out_msg, tmpl);
44         comp_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
45
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) -
49                         op->dst.offset;
50
51         if (op->m_src->next != NULL || op->m_dst->next != NULL) {
52                 /* sgl */
53                 int ret = 0;
54
55                 ICP_QAT_FW_COMN_PTR_TYPE_SET(comp_req->comn_hdr.comn_req_flags,
56                                 QAT_COMN_PTR_TYPE_SGL);
57
58                 ret = qat_sgl_fill_array(op->m_src,
59                                 op->src.offset,
60                                 &cookie->qat_sgl_src,
61                                 op->src.length,
62                                 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
63                 if (ret) {
64                         QAT_DP_LOG(ERR, "QAT PMD Cannot fill source sgl array");
65                         op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
66                         return ret;
67                 }
68
69                 ret = qat_sgl_fill_array(op->m_dst,
70                                 op->dst.offset,
71                                 &cookie->qat_sgl_dst,
72                                 comp_req->comp_pars.out_buffer_sz,
73                                 RTE_PMD_QAT_COMP_SGL_MAX_SEGMENTS);
74                 if (ret) {
75                         QAT_DP_LOG(ERR, "QAT PMD Cannot fill dest. sgl array");
76                         op->status = RTE_COMP_OP_STATUS_INVALID_ARGS;
77                         return ret;
78                 }
79
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;
86
87         } else {
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;
94
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);
99         }
100
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));
107 #endif
108         return 0;
109 }
110
111 int
112 qat_comp_process_response(void **op, uint8_t *resp, uint64_t *dequeue_err_count)
113 {
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);
120         int err = resp_msg->comn_resp.comn_status &
121                         ((1 << QAT_COMN_RESP_CMP_STATUS_BITPOS) |
122                          (1 << QAT_COMN_RESP_XLAT_STATUS_BITPOS));
123
124 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
125         QAT_DP_LOG(DEBUG, "Direction: %s",
126             qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS ?
127             "decompression" : "compression");
128         QAT_DP_HEXDUMP_LOG(DEBUG,  "qat_response:", (uint8_t *)resp_msg,
129                         sizeof(struct icp_qat_fw_comp_resp));
130 #endif
131
132         if (likely(qat_xform->qat_comp_request_type
133                         != QAT_COMP_REQUEST_DECOMPRESS)) {
134                 if (unlikely(ICP_QAT_FW_COMN_HDR_CNV_FLAG_GET(
135                                 resp_msg->comn_resp.hdr_flags)
136                                         == ICP_QAT_FW_COMP_NO_CNV)) {
137                         rx_op->status = RTE_COMP_OP_STATUS_ERROR;
138                         rx_op->debug_status = ERR_CODE_QAT_COMP_WRONG_FW;
139                         *op = (void *)rx_op;
140                         QAT_DP_LOG(ERR, "QAT has wrong firmware");
141                         ++(*dequeue_err_count);
142                         return 0;
143                 }
144         }
145
146         if (err) {
147                 if (unlikely((err & (1 << QAT_COMN_RESP_XLAT_STATUS_BITPOS))
148                              && (qat_xform->qat_comp_request_type
149                                  == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS))) {
150                         QAT_DP_LOG(ERR, "QAT intermediate buffer may be too "
151                             "small for output, try configuring a larger size");
152                 }
153
154                 int8_t cmp_err_code =
155                         (int8_t)resp_msg->comn_resp.comn_error.cmp_err_code;
156                 int8_t xlat_err_code =
157                         (int8_t)resp_msg->comn_resp.comn_error.xlat_err_code;
158
159                 if ((cmp_err_code == ERR_CODE_OVERFLOW_ERROR && !xlat_err_code)
160                                 ||
161                     (!cmp_err_code && xlat_err_code == ERR_CODE_OVERFLOW_ERROR)
162                                 ||
163                     (cmp_err_code == ERR_CODE_OVERFLOW_ERROR &&
164                      xlat_err_code == ERR_CODE_OVERFLOW_ERROR))
165                         rx_op->status =
166                                 RTE_COMP_OP_STATUS_OUT_OF_SPACE_TERMINATED;
167                 else
168                         rx_op->status = RTE_COMP_OP_STATUS_ERROR;
169
170                 ++(*dequeue_err_count);
171                 rx_op->debug_status =
172                         *((uint16_t *)(&resp_msg->comn_resp.comn_error));
173         } else {
174                 struct icp_qat_fw_resp_comp_pars *comp_resp =
175                   (struct icp_qat_fw_resp_comp_pars *)&resp_msg->comp_resp_pars;
176
177                 rx_op->status = RTE_COMP_OP_STATUS_SUCCESS;
178                 rx_op->consumed = comp_resp->input_byte_counter;
179                 rx_op->produced = comp_resp->output_byte_counter;
180
181                 if (qat_xform->checksum_type != RTE_COMP_CHECKSUM_NONE) {
182                         if (qat_xform->checksum_type == RTE_COMP_CHECKSUM_CRC32)
183                                 rx_op->output_chksum = comp_resp->curr_crc32;
184                         else if (qat_xform->checksum_type ==
185                                         RTE_COMP_CHECKSUM_ADLER32)
186                                 rx_op->output_chksum = comp_resp->curr_adler_32;
187                         else
188                                 rx_op->output_chksum = comp_resp->curr_chksum;
189                 }
190         }
191         *op = (void *)rx_op;
192
193         return 0;
194 }
195
196 unsigned int
197 qat_comp_xform_size(void)
198 {
199         return RTE_ALIGN_CEIL(sizeof(struct qat_comp_xform), 8);
200 }
201
202 static void qat_comp_create_req_hdr(struct icp_qat_fw_comn_req_hdr *header,
203                                     enum qat_comp_request_type request)
204 {
205         if (request == QAT_COMP_REQUEST_FIXED_COMP_STATELESS)
206                 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_STATIC;
207         else if (request == QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS)
208                 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DYNAMIC;
209         else if (request == QAT_COMP_REQUEST_DECOMPRESS)
210                 header->service_cmd_id = ICP_QAT_FW_COMP_CMD_DECOMPRESS;
211
212         header->service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_COMP;
213         header->hdr_flags =
214             ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET);
215
216         header->comn_req_flags = ICP_QAT_FW_COMN_FLAGS_BUILD(
217             QAT_COMN_CD_FLD_TYPE_16BYTE_DATA, QAT_COMN_PTR_TYPE_FLAT);
218 }
219
220 static int qat_comp_create_templates(struct qat_comp_xform *qat_xform,
221                         const struct rte_memzone *interm_buff_mz,
222                         const struct rte_comp_xform *xform)
223 {
224         struct icp_qat_fw_comp_req *comp_req;
225         int comp_level, algo;
226         uint32_t req_par_flags;
227         int direction = ICP_QAT_HW_COMPRESSION_DIR_COMPRESS;
228
229         if (unlikely(qat_xform == NULL)) {
230                 QAT_LOG(ERR, "Session was not created for this device");
231                 return -EINVAL;
232         }
233
234         if (qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
235                 direction = ICP_QAT_HW_COMPRESSION_DIR_DECOMPRESS;
236                 comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
237                 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
238                                 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
239                                 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_NO_CNV,
240                                 ICP_QAT_FW_COMP_NO_CNV_RECOVERY);
241
242         } else {
243                 if (xform->compress.level == RTE_COMP_LEVEL_PMD_DEFAULT)
244                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
245                 else if (xform->compress.level == 1)
246                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_1;
247                 else if (xform->compress.level == 2)
248                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_4;
249                 else if (xform->compress.level == 3)
250                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_8;
251                 else if (xform->compress.level >= 4 &&
252                          xform->compress.level <= 9)
253                         comp_level = ICP_QAT_HW_COMPRESSION_DEPTH_16;
254                 else {
255                         QAT_LOG(ERR, "compression level not supported");
256                         return -EINVAL;
257                 }
258                 req_par_flags = ICP_QAT_FW_COMP_REQ_PARAM_FLAGS_BUILD(
259                                 ICP_QAT_FW_COMP_SOP, ICP_QAT_FW_COMP_EOP,
260                                 ICP_QAT_FW_COMP_BFINAL, ICP_QAT_FW_COMP_CNV,
261                                 ICP_QAT_FW_COMP_CNV_RECOVERY);
262         }
263
264         switch (xform->compress.algo) {
265         case RTE_COMP_ALGO_DEFLATE:
266                 algo = ICP_QAT_HW_COMPRESSION_ALGO_DEFLATE;
267                 break;
268         case RTE_COMP_ALGO_LZS:
269         default:
270                 /* RTE_COMP_NULL */
271                 QAT_LOG(ERR, "compression algorithm not supported");
272                 return -EINVAL;
273         }
274
275         comp_req = &qat_xform->qat_comp_req_tmpl;
276
277         /* Initialize header */
278         qat_comp_create_req_hdr(&comp_req->comn_hdr,
279                                         qat_xform->qat_comp_request_type);
280
281         comp_req->comn_hdr.serv_specif_flags = ICP_QAT_FW_COMP_FLAGS_BUILD(
282             ICP_QAT_FW_COMP_STATELESS_SESSION,
283             ICP_QAT_FW_COMP_NOT_AUTO_SELECT_BEST,
284             ICP_QAT_FW_COMP_NOT_ENH_AUTO_SELECT_BEST,
285             ICP_QAT_FW_COMP_NOT_DISABLE_TYPE0_ENH_AUTO_SELECT_BEST,
286             ICP_QAT_FW_COMP_DISABLE_SECURE_RAM_USED_AS_INTMD_BUF);
287
288         comp_req->cd_pars.sl.comp_slice_cfg_word[0] =
289             ICP_QAT_HW_COMPRESSION_CONFIG_BUILD(
290                 direction,
291                 /* In CPM 1.6 only valid mode ! */
292                 ICP_QAT_HW_COMPRESSION_DELAYED_MATCH_ENABLED, algo,
293                 /* Translate level to depth */
294                 comp_level, ICP_QAT_HW_COMPRESSION_FILE_TYPE_0);
295
296         comp_req->comp_pars.initial_adler = 1;
297         comp_req->comp_pars.initial_crc32 = 0;
298         comp_req->comp_pars.req_par_flags = req_par_flags;
299
300
301         if (qat_xform->qat_comp_request_type ==
302                         QAT_COMP_REQUEST_FIXED_COMP_STATELESS ||
303             qat_xform->qat_comp_request_type == QAT_COMP_REQUEST_DECOMPRESS) {
304                 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
305                                             ICP_QAT_FW_SLICE_DRAM_WR);
306                 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
307                                             ICP_QAT_FW_SLICE_COMP);
308         } else if (qat_xform->qat_comp_request_type ==
309                         QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS) {
310
311                 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->comp_cd_ctrl,
312                                 ICP_QAT_FW_SLICE_XLAT);
313                 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->comp_cd_ctrl,
314                                 ICP_QAT_FW_SLICE_COMP);
315
316                 ICP_QAT_FW_COMN_NEXT_ID_SET(&comp_req->u2.xlt_cd_ctrl,
317                                 ICP_QAT_FW_SLICE_DRAM_WR);
318                 ICP_QAT_FW_COMN_CURR_ID_SET(&comp_req->u2.xlt_cd_ctrl,
319                                 ICP_QAT_FW_SLICE_XLAT);
320
321                 comp_req->u1.xlt_pars.inter_buff_ptr =
322                                 interm_buff_mz->phys_addr;
323         }
324
325 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
326         QAT_DP_HEXDUMP_LOG(DEBUG, "qat compression message template:", comp_req,
327                     sizeof(struct icp_qat_fw_comp_req));
328 #endif
329         return 0;
330 }
331
332 /**
333  * Create driver private_xform data.
334  *
335  * @param dev
336  *   Compressdev device
337  * @param xform
338  *   xform data from application
339  * @param private_xform
340  *   ptr where handle of pmd's private_xform data should be stored
341  * @return
342  *  - if successful returns 0
343  *    and valid private_xform handle
344  *  - <0 in error cases
345  *  - Returns -EINVAL if input parameters are invalid.
346  *  - Returns -ENOTSUP if comp device does not support the comp transform.
347  *  - Returns -ENOMEM if the private_xform could not be allocated.
348  */
349 int
350 qat_comp_private_xform_create(struct rte_compressdev *dev,
351                               const struct rte_comp_xform *xform,
352                               void **private_xform)
353 {
354         struct qat_comp_dev_private *qat = dev->data->dev_private;
355
356         if (unlikely(private_xform == NULL)) {
357                 QAT_LOG(ERR, "QAT: private_xform parameter is NULL");
358                 return -EINVAL;
359         }
360         if (unlikely(qat->xformpool == NULL)) {
361                 QAT_LOG(ERR, "QAT device has no private_xform mempool");
362                 return -ENOMEM;
363         }
364         if (rte_mempool_get(qat->xformpool, private_xform)) {
365                 QAT_LOG(ERR, "Couldn't get object from qat xform mempool");
366                 return -ENOMEM;
367         }
368
369         struct qat_comp_xform *qat_xform =
370                         (struct qat_comp_xform *)*private_xform;
371
372         if (xform->type == RTE_COMP_COMPRESS) {
373
374                 if (xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_FIXED ||
375                   ((xform->compress.deflate.huffman == RTE_COMP_HUFFMAN_DEFAULT)
376                                    && qat->interm_buff_mz == NULL))
377                         qat_xform->qat_comp_request_type =
378                                         QAT_COMP_REQUEST_FIXED_COMP_STATELESS;
379
380                 else if ((xform->compress.deflate.huffman ==
381                                 RTE_COMP_HUFFMAN_DYNAMIC ||
382                                 xform->compress.deflate.huffman ==
383                                                 RTE_COMP_HUFFMAN_DEFAULT) &&
384                                 qat->interm_buff_mz != NULL)
385
386                         qat_xform->qat_comp_request_type =
387                                         QAT_COMP_REQUEST_DYNAMIC_COMP_STATELESS;
388
389                 else {
390                         QAT_LOG(ERR,
391                                         "IM buffers needed for dynamic deflate. Set size in config file");
392                         return -EINVAL;
393                 }
394
395                 qat_xform->checksum_type = xform->compress.chksum;
396
397         } else {
398                 qat_xform->qat_comp_request_type = QAT_COMP_REQUEST_DECOMPRESS;
399                 qat_xform->checksum_type = xform->decompress.chksum;
400         }
401
402         if (qat_comp_create_templates(qat_xform, qat->interm_buff_mz, xform)) {
403                 QAT_LOG(ERR, "QAT: Problem with setting compression");
404                 return -EINVAL;
405         }
406         return 0;
407 }
408
409 /**
410  * Free driver private_xform data.
411  *
412  * @param dev
413  *   Compressdev device
414  * @param private_xform
415  *   handle of pmd's private_xform data
416  * @return
417  *  - 0 if successful
418  *  - <0 in error cases
419  *  - Returns -EINVAL if input parameters are invalid.
420  */
421 int
422 qat_comp_private_xform_free(struct rte_compressdev *dev __rte_unused,
423                             void *private_xform)
424 {
425         struct qat_comp_xform *qat_xform =
426                         (struct qat_comp_xform *)private_xform;
427
428         if (qat_xform) {
429                 memset(qat_xform, 0, qat_comp_xform_size());
430                 struct rte_mempool *mp = rte_mempool_from_obj(qat_xform);
431
432                 rte_mempool_put(mp, qat_xform);
433                 return 0;
434         }
435         return -EINVAL;
436 }