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