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