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