1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2018 Intel Corporation
5 #include <rte_mempool.h>
7 #include <rte_hexdump.h>
8 #include <rte_crypto_sym.h>
9 #include <rte_bus_pci.h>
10 #include <rte_byteorder.h>
12 #include <openssl/evp.h>
15 #include "qat_sym_session.h"
17 #include "adf_transport_access_macros.h"
20 /* bpi is only used for partial blocks of DES and AES
21 * so AES block len can be assumed as max len for iv, src and dst
23 #define BPI_MAX_ENCR_IV_LEN ICP_QAT_HW_AES_BLK_SZ
25 /** Encrypt a single partial block
26 * Depends on openssl libcrypto
27 * Uses ECB+XOR to do CFB encryption, same result, more performant
30 bpi_cipher_encrypt(uint8_t *src, uint8_t *dst,
31 uint8_t *iv, int ivlen, int srclen,
34 EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)bpi_ctx;
36 uint8_t encrypted_iv[BPI_MAX_ENCR_IV_LEN];
37 uint8_t *encr = encrypted_iv;
39 /* ECB method: encrypt the IV, then XOR this with plaintext */
40 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen, iv, ivlen)
42 goto cipher_encrypt_err;
44 for (; srclen != 0; --srclen, ++dst, ++src, ++encr)
50 PMD_DRV_LOG(ERR, "libcrypto ECB cipher encrypt failed");
54 /** Decrypt a single partial block
55 * Depends on openssl libcrypto
56 * Uses ECB+XOR to do CFB encryption, same result, more performant
59 bpi_cipher_decrypt(uint8_t *src, uint8_t *dst,
60 uint8_t *iv, int ivlen, int srclen,
63 EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)bpi_ctx;
65 uint8_t encrypted_iv[BPI_MAX_ENCR_IV_LEN];
66 uint8_t *encr = encrypted_iv;
68 /* ECB method: encrypt (not decrypt!) the IV, then XOR with plaintext */
69 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen, iv, ivlen)
71 goto cipher_decrypt_err;
73 for (; srclen != 0; --srclen, ++dst, ++src, ++encr)
79 PMD_DRV_LOG(ERR, "libcrypto ECB cipher decrypt for BPI IV failed");
83 /** Creates a context in either AES or DES in ECB mode
84 * Depends on openssl libcrypto
86 static inline uint32_t
87 adf_modulo(uint32_t data, uint32_t shift);
89 static inline uint32_t
90 qat_bpicipher_preprocess(struct qat_sym_session *ctx,
91 struct rte_crypto_op *op)
93 int block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
94 struct rte_crypto_sym_op *sym_op = op->sym;
95 uint8_t last_block_len = block_len > 0 ?
96 sym_op->cipher.data.length % block_len : 0;
99 ctx->qat_dir == ICP_QAT_HW_CIPHER_DECRYPT) {
101 /* Decrypt last block */
102 uint8_t *last_block, *dst, *iv;
103 uint32_t last_block_offset = sym_op->cipher.data.offset +
104 sym_op->cipher.data.length - last_block_len;
105 last_block = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_src,
106 uint8_t *, last_block_offset);
108 if (unlikely(sym_op->m_dst != NULL))
109 /* out-of-place operation (OOP) */
110 dst = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_dst,
111 uint8_t *, last_block_offset);
115 if (last_block_len < sym_op->cipher.data.length)
116 /* use previous block ciphertext as IV */
117 iv = last_block - block_len;
119 /* runt block, i.e. less than one full block */
120 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
121 ctx->cipher_iv.offset);
123 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
124 rte_hexdump(stdout, "BPI: src before pre-process:", last_block,
126 if (sym_op->m_dst != NULL)
127 rte_hexdump(stdout, "BPI: dst before pre-process:", dst,
130 bpi_cipher_decrypt(last_block, dst, iv, block_len,
131 last_block_len, ctx->bpi_ctx);
132 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
133 rte_hexdump(stdout, "BPI: src after pre-process:", last_block,
135 if (sym_op->m_dst != NULL)
136 rte_hexdump(stdout, "BPI: dst after pre-process:", dst,
141 return sym_op->cipher.data.length - last_block_len;
144 static inline uint32_t
145 qat_bpicipher_postprocess(struct qat_sym_session *ctx,
146 struct rte_crypto_op *op)
148 int block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
149 struct rte_crypto_sym_op *sym_op = op->sym;
150 uint8_t last_block_len = block_len > 0 ?
151 sym_op->cipher.data.length % block_len : 0;
153 if (last_block_len > 0 &&
154 ctx->qat_dir == ICP_QAT_HW_CIPHER_ENCRYPT) {
156 /* Encrypt last block */
157 uint8_t *last_block, *dst, *iv;
158 uint32_t last_block_offset;
160 last_block_offset = sym_op->cipher.data.offset +
161 sym_op->cipher.data.length - last_block_len;
162 last_block = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_src,
163 uint8_t *, last_block_offset);
165 if (unlikely(sym_op->m_dst != NULL))
166 /* out-of-place operation (OOP) */
167 dst = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_dst,
168 uint8_t *, last_block_offset);
172 if (last_block_len < sym_op->cipher.data.length)
173 /* use previous block ciphertext as IV */
174 iv = dst - block_len;
176 /* runt block, i.e. less than one full block */
177 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
178 ctx->cipher_iv.offset);
180 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
181 rte_hexdump(stdout, "BPI: src before post-process:", last_block,
183 if (sym_op->m_dst != NULL)
184 rte_hexdump(stdout, "BPI: dst before post-process:",
185 dst, last_block_len);
187 bpi_cipher_encrypt(last_block, dst, iv, block_len,
188 last_block_len, ctx->bpi_ctx);
189 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
190 rte_hexdump(stdout, "BPI: src after post-process:", last_block,
192 if (sym_op->m_dst != NULL)
193 rte_hexdump(stdout, "BPI: dst after post-process:", dst,
197 return sym_op->cipher.data.length - last_block_len;
201 txq_write_tail(struct qat_qp *qp, struct qat_queue *q) {
202 WRITE_CSR_RING_TAIL(qp->mmap_bar_addr, q->hw_bundle_number,
203 q->hw_queue_number, q->tail);
204 q->nb_pending_requests = 0;
205 q->csr_tail = q->tail;
209 qat_enqueue_op_burst(void *qp, void **ops, uint16_t nb_ops)
211 register struct qat_queue *queue;
212 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
213 register uint32_t nb_ops_sent = 0;
215 uint16_t nb_ops_possible = nb_ops;
216 register uint8_t *base_addr;
217 register uint32_t tail;
220 if (unlikely(nb_ops == 0))
223 /* read params used a lot in main loop into registers */
224 queue = &(tmp_qp->tx_q);
225 base_addr = (uint8_t *)queue->base_addr;
228 /* Find how many can actually fit on the ring */
229 tmp_qp->inflights16 += nb_ops;
230 overflow = tmp_qp->inflights16 - queue->max_inflights;
232 tmp_qp->inflights16 -= overflow;
233 nb_ops_possible = nb_ops - overflow;
234 if (nb_ops_possible == 0)
238 while (nb_ops_sent != nb_ops_possible) {
239 ret = tmp_qp->build_request(*ops, base_addr + tail,
240 tmp_qp->op_cookies[tail / queue->msg_size],
241 tmp_qp->qat_dev_gen);
243 tmp_qp->stats.enqueue_err_count++;
245 * This message cannot be enqueued,
246 * decrease number of ops that wasn't sent
248 tmp_qp->inflights16 -= nb_ops_possible - nb_ops_sent;
249 if (nb_ops_sent == 0)
254 tail = adf_modulo(tail + queue->msg_size, queue->modulo);
260 tmp_qp->stats.enqueued_count += nb_ops_sent;
261 queue->nb_pending_requests += nb_ops_sent;
262 if (tmp_qp->inflights16 < QAT_CSR_TAIL_FORCE_WRITE_THRESH ||
263 queue->nb_pending_requests > QAT_CSR_TAIL_WRITE_THRESH) {
264 txq_write_tail(tmp_qp, queue);
270 void rxq_free_desc(struct qat_qp *qp, struct qat_queue *q)
272 uint32_t old_head, new_head;
275 old_head = q->csr_head;
277 max_head = qp->nb_descriptors * q->msg_size;
279 /* write out free descriptors */
280 void *cur_desc = (uint8_t *)q->base_addr + old_head;
282 if (new_head < old_head) {
283 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
284 memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
286 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
288 q->nb_processed_responses = 0;
289 q->csr_head = new_head;
291 /* write current head to CSR */
292 WRITE_CSR_RING_HEAD(qp->mmap_bar_addr, q->hw_bundle_number,
293 q->hw_queue_number, new_head);
297 qat_sym_pmd_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
300 return qat_enqueue_op_burst(qp, (void **)ops, nb_ops);
304 qat_sym_pmd_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
307 struct qat_queue *rx_queue, *tx_queue;
308 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
309 uint32_t msg_counter = 0;
310 struct rte_crypto_op *rx_op;
311 struct icp_qat_fw_comn_resp *resp_msg;
314 rx_queue = &(tmp_qp->rx_q);
315 tx_queue = &(tmp_qp->tx_q);
316 head = rx_queue->head;
317 resp_msg = (struct icp_qat_fw_comn_resp *)
318 ((uint8_t *)rx_queue->base_addr + head);
320 while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
321 msg_counter != nb_ops) {
322 rx_op = (struct rte_crypto_op *)(uintptr_t)
323 (resp_msg->opaque_data);
325 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
326 rte_hexdump(stdout, "qat_response:", (uint8_t *)resp_msg,
327 sizeof(struct icp_qat_fw_comn_resp));
329 if (ICP_QAT_FW_COMN_STATUS_FLAG_OK !=
330 ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(
331 resp_msg->comn_hdr.comn_status)) {
332 rx_op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
334 struct qat_sym_session *sess =
335 (struct qat_sym_session *)
336 get_session_private_data(
338 cryptodev_qat_driver_id);
341 qat_bpicipher_postprocess(sess, rx_op);
342 rx_op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
345 head = adf_modulo(head + rx_queue->msg_size, rx_queue->modulo);
346 resp_msg = (struct icp_qat_fw_comn_resp *)
347 ((uint8_t *)rx_queue->base_addr + head);
352 if (msg_counter > 0) {
353 rx_queue->head = head;
354 tmp_qp->stats.dequeued_count += msg_counter;
355 rx_queue->nb_processed_responses += msg_counter;
356 tmp_qp->inflights16 -= msg_counter;
358 if (rx_queue->nb_processed_responses > QAT_CSR_HEAD_WRITE_THRESH)
359 rxq_free_desc(tmp_qp, rx_queue);
361 /* also check if tail needs to be advanced */
362 if (tmp_qp->inflights16 <= QAT_CSR_TAIL_FORCE_WRITE_THRESH &&
363 tx_queue->tail != tx_queue->csr_tail) {
364 txq_write_tail(tmp_qp, tx_queue);
370 qat_sgl_fill_array(struct rte_mbuf *buf, uint64_t buff_start,
371 struct qat_alg_buf_list *list, uint32_t data_len)
375 uint32_t buf_len = rte_pktmbuf_iova(buf) -
376 buff_start + rte_pktmbuf_data_len(buf);
378 list->bufers[0].addr = buff_start;
379 list->bufers[0].resrvd = 0;
380 list->bufers[0].len = buf_len;
382 if (data_len <= buf_len) {
384 list->bufers[0].len = data_len;
390 if (unlikely(nr == QAT_SGL_MAX_NUMBER)) {
391 PMD_DRV_LOG(ERR, "QAT PMD exceeded size of QAT SGL"
397 list->bufers[nr].len = rte_pktmbuf_data_len(buf);
398 list->bufers[nr].resrvd = 0;
399 list->bufers[nr].addr = rte_pktmbuf_iova(buf);
401 buf_len += list->bufers[nr].len;
404 if (buf_len > data_len) {
405 list->bufers[nr].len -=
417 set_cipher_iv(uint16_t iv_length, uint16_t iv_offset,
418 struct icp_qat_fw_la_cipher_req_params *cipher_param,
419 struct rte_crypto_op *op,
420 struct icp_qat_fw_la_bulk_req *qat_req)
422 /* copy IV into request if it fits */
423 if (iv_length <= sizeof(cipher_param->u.cipher_IV_array)) {
424 rte_memcpy(cipher_param->u.cipher_IV_array,
425 rte_crypto_op_ctod_offset(op, uint8_t *,
429 ICP_QAT_FW_LA_CIPH_IV_FLD_FLAG_SET(
430 qat_req->comn_hdr.serv_specif_flags,
431 ICP_QAT_FW_CIPH_IV_64BIT_PTR);
432 cipher_param->u.s.cipher_IV_ptr =
433 rte_crypto_op_ctophys_offset(op,
438 /** Set IV for CCM is special case, 0th byte is set to q-1
439 * where q is padding of nonce in 16 byte block
442 set_cipher_iv_ccm(uint16_t iv_length, uint16_t iv_offset,
443 struct icp_qat_fw_la_cipher_req_params *cipher_param,
444 struct rte_crypto_op *op, uint8_t q, uint8_t aad_len_field_sz)
446 rte_memcpy(((uint8_t *)cipher_param->u.cipher_IV_array) +
447 ICP_QAT_HW_CCM_NONCE_OFFSET,
448 rte_crypto_op_ctod_offset(op, uint8_t *,
449 iv_offset) + ICP_QAT_HW_CCM_NONCE_OFFSET,
451 *(uint8_t *)&cipher_param->u.cipher_IV_array[0] =
452 q - ICP_QAT_HW_CCM_NONCE_OFFSET;
454 if (aad_len_field_sz)
455 rte_memcpy(&op->sym->aead.aad.data[ICP_QAT_HW_CCM_NONCE_OFFSET],
456 rte_crypto_op_ctod_offset(op, uint8_t *,
457 iv_offset) + ICP_QAT_HW_CCM_NONCE_OFFSET,
463 qat_sym_build_request(void *in_op, uint8_t *out_msg,
464 void *op_cookie, enum qat_device_gen qat_dev_gen)
467 struct qat_sym_session *ctx;
468 struct icp_qat_fw_la_cipher_req_params *cipher_param;
469 struct icp_qat_fw_la_auth_req_params *auth_param;
470 register struct icp_qat_fw_la_bulk_req *qat_req;
471 uint8_t do_auth = 0, do_cipher = 0, do_aead = 0;
472 uint32_t cipher_len = 0, cipher_ofs = 0;
473 uint32_t auth_len = 0, auth_ofs = 0;
474 uint32_t min_ofs = 0;
475 uint64_t src_buf_start = 0, dst_buf_start = 0;
477 struct rte_crypto_op *op = (struct rte_crypto_op *)in_op;
478 struct qat_sym_op_cookie *cookie =
479 (struct qat_sym_op_cookie *)op_cookie;
481 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
482 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
483 PMD_DRV_LOG(ERR, "QAT PMD only supports symmetric crypto "
484 "operation requests, op (%p) is not a "
485 "symmetric operation.", op);
489 if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
490 PMD_DRV_LOG(ERR, "QAT PMD only supports session oriented"
491 " requests, op (%p) is sessionless.", op);
495 ctx = (struct qat_sym_session *)get_session_private_data(
496 op->sym->session, cryptodev_qat_driver_id);
498 if (unlikely(ctx == NULL)) {
499 PMD_DRV_LOG(ERR, "Session was not created for this device");
503 if (unlikely(ctx->min_qat_dev_gen > qat_dev_gen)) {
504 PMD_DRV_LOG(ERR, "Session alg not supported on this device gen");
505 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
509 qat_req = (struct icp_qat_fw_la_bulk_req *)out_msg;
510 rte_mov128((uint8_t *)qat_req, (const uint8_t *)&(ctx->fw_req));
511 qat_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
512 cipher_param = (void *)&qat_req->serv_specif_rqpars;
513 auth_param = (void *)((uint8_t *)cipher_param + sizeof(*cipher_param));
515 if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_HASH_CIPHER ||
516 ctx->qat_cmd == ICP_QAT_FW_LA_CMD_CIPHER_HASH) {
517 /* AES-GCM or AES-CCM */
518 if (ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
519 ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_64 ||
520 (ctx->qat_cipher_alg == ICP_QAT_HW_CIPHER_ALGO_AES128
521 && ctx->qat_mode == ICP_QAT_HW_CIPHER_CTR_MODE
522 && ctx->qat_hash_alg ==
523 ICP_QAT_HW_AUTH_ALGO_AES_CBC_MAC)) {
529 } else if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_AUTH) {
532 } else if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_CIPHER) {
539 if (ctx->qat_cipher_alg ==
540 ICP_QAT_HW_CIPHER_ALGO_SNOW_3G_UEA2 ||
541 ctx->qat_cipher_alg == ICP_QAT_HW_CIPHER_ALGO_KASUMI ||
542 ctx->qat_cipher_alg ==
543 ICP_QAT_HW_CIPHER_ALGO_ZUC_3G_128_EEA3) {
546 (cipher_param->cipher_length % BYTE_LENGTH != 0)
547 || (cipher_param->cipher_offset
548 % BYTE_LENGTH != 0))) {
550 "SNOW3G/KASUMI/ZUC in QAT PMD only supports byte aligned values");
551 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
554 cipher_len = op->sym->cipher.data.length >> 3;
555 cipher_ofs = op->sym->cipher.data.offset >> 3;
557 } else if (ctx->bpi_ctx) {
558 /* DOCSIS - only send complete blocks to device
559 * Process any partial block using CFB mode.
560 * Even if 0 complete blocks, still send this to device
561 * to get into rx queue for post-process and dequeuing
563 cipher_len = qat_bpicipher_preprocess(ctx, op);
564 cipher_ofs = op->sym->cipher.data.offset;
566 cipher_len = op->sym->cipher.data.length;
567 cipher_ofs = op->sym->cipher.data.offset;
570 set_cipher_iv(ctx->cipher_iv.length, ctx->cipher_iv.offset,
571 cipher_param, op, qat_req);
572 min_ofs = cipher_ofs;
577 if (ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_SNOW_3G_UIA2 ||
578 ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_KASUMI_F9 ||
580 ICP_QAT_HW_AUTH_ALGO_ZUC_3G_128_EIA3) {
581 if (unlikely((auth_param->auth_off % BYTE_LENGTH != 0)
582 || (auth_param->auth_len % BYTE_LENGTH != 0))) {
584 "For SNOW3G/KASUMI/ZUC, QAT PMD only supports byte aligned values");
585 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
588 auth_ofs = op->sym->auth.data.offset >> 3;
589 auth_len = op->sym->auth.data.length >> 3;
591 auth_param->u1.aad_adr =
592 rte_crypto_op_ctophys_offset(op,
593 ctx->auth_iv.offset);
595 } else if (ctx->qat_hash_alg ==
596 ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
598 ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
600 set_cipher_iv(ctx->auth_iv.length,
602 cipher_param, op, qat_req);
603 auth_ofs = op->sym->auth.data.offset;
604 auth_len = op->sym->auth.data.length;
606 auth_param->u1.aad_adr = 0;
607 auth_param->u2.aad_sz = 0;
610 * If len(iv)==12B fw computes J0
612 if (ctx->auth_iv.length == 12) {
613 ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
614 qat_req->comn_hdr.serv_specif_flags,
615 ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
619 auth_ofs = op->sym->auth.data.offset;
620 auth_len = op->sym->auth.data.length;
625 if (likely(ctx->qat_hash_alg != ICP_QAT_HW_AUTH_ALGO_NULL))
626 auth_param->auth_res_addr =
627 op->sym->auth.digest.phys_addr;
633 * This address may used for setting AAD physical pointer
634 * into IV offset from op
636 rte_iova_t aad_phys_addr_aead = op->sym->aead.aad.phys_addr;
637 if (ctx->qat_hash_alg ==
638 ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
640 ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
642 * If len(iv)==12B fw computes J0
644 if (ctx->cipher_iv.length == 12) {
645 ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
646 qat_req->comn_hdr.serv_specif_flags,
647 ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
649 set_cipher_iv(ctx->cipher_iv.length,
650 ctx->cipher_iv.offset,
651 cipher_param, op, qat_req);
653 } else if (ctx->qat_hash_alg ==
654 ICP_QAT_HW_AUTH_ALGO_AES_CBC_MAC) {
656 /* In case of AES-CCM this may point to user selected
657 * memory or iv offset in cypto_op
659 uint8_t *aad_data = op->sym->aead.aad.data;
660 /* This is true AAD length, it not includes 18 bytes of
663 uint8_t aad_ccm_real_len = 0;
664 uint8_t aad_len_field_sz = 0;
665 uint32_t msg_len_be =
666 rte_bswap32(op->sym->aead.data.length);
668 if (ctx->aad_len > ICP_QAT_HW_CCM_AAD_DATA_OFFSET) {
669 aad_len_field_sz = ICP_QAT_HW_CCM_AAD_LEN_INFO;
670 aad_ccm_real_len = ctx->aad_len -
671 ICP_QAT_HW_CCM_AAD_B0_LEN -
672 ICP_QAT_HW_CCM_AAD_LEN_INFO;
675 * aad_len not greater than 18, so no actual aad
676 * data, then use IV after op for B0 block
678 aad_data = rte_crypto_op_ctod_offset(op,
680 ctx->cipher_iv.offset);
682 rte_crypto_op_ctophys_offset(op,
683 ctx->cipher_iv.offset);
686 uint8_t q = ICP_QAT_HW_CCM_NQ_CONST -
687 ctx->cipher_iv.length;
689 aad_data[0] = ICP_QAT_HW_CCM_BUILD_B0_FLAGS(
691 ctx->digest_length, q);
693 if (q > ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE) {
694 memcpy(aad_data + ctx->cipher_iv.length +
695 ICP_QAT_HW_CCM_NONCE_OFFSET +
696 (q - ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE),
697 (uint8_t *)&msg_len_be,
698 ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE);
700 memcpy(aad_data + ctx->cipher_iv.length +
701 ICP_QAT_HW_CCM_NONCE_OFFSET,
702 (uint8_t *)&msg_len_be
703 + (ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE
707 if (aad_len_field_sz > 0) {
708 *(uint16_t *)&aad_data[ICP_QAT_HW_CCM_AAD_B0_LEN]
709 = rte_bswap16(aad_ccm_real_len);
711 if ((aad_ccm_real_len + aad_len_field_sz)
712 % ICP_QAT_HW_CCM_AAD_B0_LEN) {
716 pad_len = ICP_QAT_HW_CCM_AAD_B0_LEN -
717 ((aad_ccm_real_len + aad_len_field_sz) %
718 ICP_QAT_HW_CCM_AAD_B0_LEN);
719 pad_idx = ICP_QAT_HW_CCM_AAD_B0_LEN +
720 aad_ccm_real_len + aad_len_field_sz;
721 memset(&aad_data[pad_idx],
727 set_cipher_iv_ccm(ctx->cipher_iv.length,
728 ctx->cipher_iv.offset,
734 cipher_len = op->sym->aead.data.length;
735 cipher_ofs = op->sym->aead.data.offset;
736 auth_len = op->sym->aead.data.length;
737 auth_ofs = op->sym->aead.data.offset;
739 auth_param->u1.aad_adr = aad_phys_addr_aead;
740 auth_param->auth_res_addr = op->sym->aead.digest.phys_addr;
741 min_ofs = op->sym->aead.data.offset;
744 if (op->sym->m_src->next || (op->sym->m_dst && op->sym->m_dst->next))
747 /* adjust for chain case */
748 if (do_cipher && do_auth)
749 min_ofs = cipher_ofs < auth_ofs ? cipher_ofs : auth_ofs;
751 if (unlikely(min_ofs >= rte_pktmbuf_data_len(op->sym->m_src) && do_sgl))
754 if (unlikely(op->sym->m_dst != NULL)) {
755 /* Out-of-place operation (OOP)
756 * Don't align DMA start. DMA the minimum data-set
757 * so as not to overwrite data in dest buffer
760 rte_pktmbuf_iova_offset(op->sym->m_src, min_ofs);
762 rte_pktmbuf_iova_offset(op->sym->m_dst, min_ofs);
765 /* In-place operation
766 * Start DMA at nearest aligned address below min_ofs
769 rte_pktmbuf_iova_offset(op->sym->m_src, min_ofs)
770 & QAT_64_BTYE_ALIGN_MASK;
772 if (unlikely((rte_pktmbuf_iova(op->sym->m_src) -
773 rte_pktmbuf_headroom(op->sym->m_src))
775 /* alignment has pushed addr ahead of start of mbuf
776 * so revert and take the performance hit
779 rte_pktmbuf_iova_offset(op->sym->m_src,
782 dst_buf_start = src_buf_start;
785 if (do_cipher || do_aead) {
786 cipher_param->cipher_offset =
787 (uint32_t)rte_pktmbuf_iova_offset(
788 op->sym->m_src, cipher_ofs) - src_buf_start;
789 cipher_param->cipher_length = cipher_len;
791 cipher_param->cipher_offset = 0;
792 cipher_param->cipher_length = 0;
795 if (do_auth || do_aead) {
796 auth_param->auth_off = (uint32_t)rte_pktmbuf_iova_offset(
797 op->sym->m_src, auth_ofs) - src_buf_start;
798 auth_param->auth_len = auth_len;
800 auth_param->auth_off = 0;
801 auth_param->auth_len = 0;
804 qat_req->comn_mid.dst_length =
805 qat_req->comn_mid.src_length =
806 (cipher_param->cipher_offset + cipher_param->cipher_length)
807 > (auth_param->auth_off + auth_param->auth_len) ?
808 (cipher_param->cipher_offset + cipher_param->cipher_length)
809 : (auth_param->auth_off + auth_param->auth_len);
813 ICP_QAT_FW_COMN_PTR_TYPE_SET(qat_req->comn_hdr.comn_req_flags,
814 QAT_COMN_PTR_TYPE_SGL);
815 ret = qat_sgl_fill_array(op->sym->m_src, src_buf_start,
816 &cookie->qat_sgl_list_src,
817 qat_req->comn_mid.src_length);
819 PMD_DRV_LOG(ERR, "QAT PMD Cannot fill sgl array");
823 if (likely(op->sym->m_dst == NULL))
824 qat_req->comn_mid.dest_data_addr =
825 qat_req->comn_mid.src_data_addr =
826 cookie->qat_sgl_src_phys_addr;
828 ret = qat_sgl_fill_array(op->sym->m_dst,
830 &cookie->qat_sgl_list_dst,
831 qat_req->comn_mid.dst_length);
834 PMD_DRV_LOG(ERR, "QAT PMD Cannot "
839 qat_req->comn_mid.src_data_addr =
840 cookie->qat_sgl_src_phys_addr;
841 qat_req->comn_mid.dest_data_addr =
842 cookie->qat_sgl_dst_phys_addr;
845 qat_req->comn_mid.src_data_addr = src_buf_start;
846 qat_req->comn_mid.dest_data_addr = dst_buf_start;
849 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
850 rte_hexdump(stdout, "qat_req:", qat_req,
851 sizeof(struct icp_qat_fw_la_bulk_req));
852 rte_hexdump(stdout, "src_data:",
853 rte_pktmbuf_mtod(op->sym->m_src, uint8_t*),
854 rte_pktmbuf_data_len(op->sym->m_src));
856 uint8_t *cipher_iv_ptr = rte_crypto_op_ctod_offset(op,
858 ctx->cipher_iv.offset);
859 rte_hexdump(stdout, "cipher iv:", cipher_iv_ptr,
860 ctx->cipher_iv.length);
864 if (ctx->auth_iv.length) {
865 uint8_t *auth_iv_ptr = rte_crypto_op_ctod_offset(op,
867 ctx->auth_iv.offset);
868 rte_hexdump(stdout, "auth iv:", auth_iv_ptr,
869 ctx->auth_iv.length);
871 rte_hexdump(stdout, "digest:", op->sym->auth.digest.data,
876 rte_hexdump(stdout, "digest:", op->sym->aead.digest.data,
878 rte_hexdump(stdout, "aad:", op->sym->aead.aad.data,
885 static inline uint32_t adf_modulo(uint32_t data, uint32_t shift)
887 uint32_t div = data >> shift;
888 uint32_t mult = div << shift;
893 void qat_sym_stats_get(struct rte_cryptodev *dev,
894 struct rte_cryptodev_stats *stats)
897 struct qat_qp **qp = (struct qat_qp **)(dev->data->queue_pairs);
899 PMD_INIT_FUNC_TRACE();
901 PMD_DRV_LOG(ERR, "invalid stats ptr NULL");
904 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
906 PMD_DRV_LOG(DEBUG, "Uninitialised queue pair");
910 stats->enqueued_count += qp[i]->stats.enqueued_count;
911 stats->dequeued_count += qp[i]->stats.dequeued_count;
912 stats->enqueue_err_count += qp[i]->stats.enqueue_err_count;
913 stats->dequeue_err_count += qp[i]->stats.dequeue_err_count;
917 void qat_sym_stats_reset(struct rte_cryptodev *dev)
920 struct qat_qp **qp = (struct qat_qp **)(dev->data->queue_pairs);
922 PMD_INIT_FUNC_TRACE();
923 for (i = 0; i < dev->data->nb_queue_pairs; i++)
924 memset(&(qp[i]->stats), 0, sizeof(qp[i]->stats));
925 PMD_DRV_LOG(DEBUG, "QAT crypto: stats cleared");