1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2019 Marvell International Ltd.
5 #include <rte_crypto.h>
6 #include <rte_cryptodev.h>
7 #include <rte_cycles.h>
10 #include "nitrox_sym_reqmgr.h"
11 #include "nitrox_logs.h"
13 #define MAX_SGBUF_CNT 16
14 #define MAX_SGCOMP_CNT 5
16 #define MIN_UDD_LEN 16
17 /* PKT_IN_HDR + SLC_STORE_INFO */
19 /* Base destination port for the solicited requests */
20 #define SOLICIT_BASE_DPORT 256
21 #define PENDING_SIG 0xFFFFFFFFFFFFFFFFUL
34 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
35 uint64_t raz_48_63 : 16;
40 uint64_t raz_30_31 : 2;
42 uint64_t raz_16_23 : 8;
46 uint64_t raz_16_23 : 8;
48 uint64_t raz_30_31 : 2;
53 uint64_t raz_48_63 : 16;
61 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
68 uint64_t destport : 9;
70 uint64_t raz_19_23 : 5;
80 uint64_t raz_19_23 : 5;
82 uint64_t destport : 9;
94 union slc_store_info {
97 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
98 uint64_t raz_39_63 : 25;
100 uint64_t raz_0_31 : 32;
102 uint64_t raz_0_31 : 32;
104 uint64_t raz_39_63 : 25;
110 struct nps_pkt_instr {
112 union pkt_instr_hdr ih;
114 union slc_store_info slc;
123 struct nitrox_sglist {
131 struct nitrox_sgcomp {
136 struct nitrox_sgtable {
137 uint8_t map_bufs_cnt;
139 uint16_t total_bytes;
141 struct nitrox_sglist sglist[MAX_SGBUF_CNT];
142 struct nitrox_sgcomp sgcomp[MAX_SGCOMP_CNT];
151 struct nitrox_softreq {
152 struct nitrox_crypto_ctx *ctx;
153 struct rte_crypto_op *op;
155 struct nps_pkt_instr instr;
156 struct resp_hdr resp;
157 struct nitrox_sgtable in;
158 struct nitrox_sgtable out;
167 softreq_init(struct nitrox_softreq *sr, rte_iova_t iova)
169 memset(sr, 0, sizeof(*sr));
174 * 64-Byte Instruction Format
176 * ----------------------
178 * ----------------------
179 * | PKT_IN_INSTR_HDR | 8 bytes
180 * ----------------------
181 * | PKT_IN_HDR | 16 bytes
182 * ----------------------
183 * | SLC_INFO | 16 bytes
184 * ----------------------
185 * | Front data | 16 bytes
186 * ----------------------
189 create_se_instr(struct nitrox_softreq *sr, uint8_t qno)
191 struct nitrox_crypto_ctx *ctx = sr->ctx;
192 rte_iova_t ctx_handle;
194 /* fill the packet instruction */
196 sr->instr.dptr0 = rte_cpu_to_be_64(sr->dptr);
199 sr->instr.ih.value = 0;
200 sr->instr.ih.s.g = 1;
201 sr->instr.ih.s.gsz = sr->in.map_bufs_cnt;
202 sr->instr.ih.s.ssz = sr->out.map_bufs_cnt;
203 sr->instr.ih.s.fsz = FDATA_SIZE + sizeof(struct gphdr);
204 sr->instr.ih.s.tlen = sr->instr.ih.s.fsz + sr->in.total_bytes;
205 sr->instr.ih.value = rte_cpu_to_be_64(sr->instr.ih.value);
208 sr->instr.irh.value[0] = 0;
209 sr->instr.irh.s.uddl = MIN_UDD_LEN;
210 /* context length in 64-bit words */
211 sr->instr.irh.s.ctxl = RTE_ALIGN_MUL_CEIL(sizeof(ctx->fctx), 8) / 8;
212 /* offset from solicit base port 256 */
213 sr->instr.irh.s.destport = SOLICIT_BASE_DPORT + qno;
214 /* Invalid context cache */
215 sr->instr.irh.s.ctxc = 0x3;
216 sr->instr.irh.s.arg = ctx->req_op;
217 sr->instr.irh.s.opcode = ctx->opcode;
218 sr->instr.irh.value[0] = rte_cpu_to_be_64(sr->instr.irh.value[0]);
221 ctx_handle = ctx->iova + offsetof(struct nitrox_crypto_ctx, fctx);
222 sr->instr.irh.s.ctxp = rte_cpu_to_be_64(ctx_handle);
225 sr->instr.slc.value[0] = 0;
226 sr->instr.slc.s.ssz = sr->out.map_bufs_cnt;
227 sr->instr.slc.value[0] = rte_cpu_to_be_64(sr->instr.slc.value[0]);
230 sr->instr.slc.s.rptr = rte_cpu_to_be_64(sr->rptr);
232 * No conversion for front data,
233 * It goes into payload
234 * put GP Header in front data
236 memcpy(&sr->instr.fdata[0], &sr->gph, sizeof(sr->instr.fdata[0]));
237 sr->instr.fdata[1] = 0;
241 softreq_copy_iv(struct nitrox_softreq *sr)
243 sr->iv.virt = rte_crypto_op_ctod_offset(sr->op, uint8_t *,
245 sr->iv.iova = rte_crypto_op_ctophys_offset(sr->op, sr->ctx->iv.offset);
246 sr->iv.len = sr->ctx->iv.length;
250 extract_cipher_auth_digest(struct nitrox_softreq *sr,
251 struct nitrox_sglist *digest)
253 struct rte_crypto_op *op = sr->op;
254 struct rte_mbuf *mdst = op->sym->m_dst ? op->sym->m_dst :
257 if (sr->ctx->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY &&
258 unlikely(!op->sym->auth.digest.data))
261 digest->len = sr->ctx->digest_length;
262 if (op->sym->auth.digest.data) {
263 digest->iova = op->sym->auth.digest.phys_addr;
264 digest->virt = op->sym->auth.digest.data;
268 if (unlikely(rte_pktmbuf_data_len(mdst) < op->sym->auth.data.offset +
269 op->sym->auth.data.length + digest->len))
272 digest->iova = rte_pktmbuf_iova_offset(mdst,
273 op->sym->auth.data.offset +
274 op->sym->auth.data.length);
275 digest->virt = rte_pktmbuf_mtod_offset(mdst, uint8_t *,
276 op->sym->auth.data.offset +
277 op->sym->auth.data.length);
282 fill_sglist(struct nitrox_sgtable *sgtbl, uint16_t len, rte_iova_t iova,
285 struct nitrox_sglist *sglist = sgtbl->sglist;
286 uint8_t cnt = sgtbl->map_bufs_cnt;
291 sglist[cnt].len = len;
292 sglist[cnt].iova = iova;
293 sglist[cnt].virt = virt;
294 sgtbl->total_bytes += len;
296 sgtbl->map_bufs_cnt = cnt;
300 create_sglist_from_mbuf(struct nitrox_sgtable *sgtbl, struct rte_mbuf *mbuf,
301 uint32_t off, int datalen)
303 struct nitrox_sglist *sglist = sgtbl->sglist;
304 uint8_t cnt = sgtbl->map_bufs_cnt;
308 if (unlikely(datalen <= 0))
311 for (m = mbuf; m && off > rte_pktmbuf_data_len(m); m = m->next)
312 off -= rte_pktmbuf_data_len(m);
317 mlen = rte_pktmbuf_data_len(m) - off;
320 sglist[cnt].len = mlen;
321 sglist[cnt].iova = rte_pktmbuf_iova_offset(m, off);
322 sglist[cnt].virt = rte_pktmbuf_mtod_offset(m, uint8_t *, off);
323 sgtbl->total_bytes += mlen;
326 for (m = m->next; m && datalen; m = m->next) {
327 mlen = rte_pktmbuf_data_len(m) < datalen ?
328 rte_pktmbuf_data_len(m) : datalen;
329 sglist[cnt].len = mlen;
330 sglist[cnt].iova = rte_pktmbuf_iova(m);
331 sglist[cnt].virt = rte_pktmbuf_mtod(m, uint8_t *);
332 sgtbl->total_bytes += mlen;
337 RTE_VERIFY(cnt <= MAX_SGBUF_CNT);
338 sgtbl->map_bufs_cnt = cnt;
343 create_cipher_auth_sglist(struct nitrox_softreq *sr,
344 struct nitrox_sgtable *sgtbl, struct rte_mbuf *mbuf)
346 struct rte_crypto_op *op = sr->op;
350 fill_sglist(sgtbl, sr->iv.len, sr->iv.iova, sr->iv.virt);
351 auth_only_len = op->sym->auth.data.length - op->sym->cipher.data.length;
352 if (unlikely(auth_only_len < 0))
355 err = create_sglist_from_mbuf(sgtbl, mbuf, op->sym->auth.data.offset,
360 err = create_sglist_from_mbuf(sgtbl, mbuf, op->sym->cipher.data.offset,
361 op->sym->cipher.data.length);
369 create_sgcomp(struct nitrox_sgtable *sgtbl)
372 struct nitrox_sgcomp *sgcomp = sgtbl->sgcomp;
373 struct nitrox_sglist *sglist = sgtbl->sglist;
375 nr_sgcomp = RTE_ALIGN_MUL_CEIL(sgtbl->map_bufs_cnt, 4) / 4;
376 sgtbl->nr_sgcomp = nr_sgcomp;
377 for (i = 0; i < nr_sgcomp; i++, sgcomp++) {
378 for (j = 0; j < 4; j++, sglist++) {
379 sgcomp->len[j] = rte_cpu_to_be_16(sglist->len);
380 sgcomp->iova[j] = rte_cpu_to_be_64(sglist->iova);
386 create_cipher_auth_inbuf(struct nitrox_softreq *sr,
387 struct nitrox_sglist *digest)
390 struct nitrox_crypto_ctx *ctx = sr->ctx;
392 err = create_cipher_auth_sglist(sr, &sr->in, sr->op->sym->m_src);
396 if (ctx->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY)
397 fill_sglist(&sr->in, digest->len, digest->iova, digest->virt);
399 create_sgcomp(&sr->in);
400 sr->dptr = sr->iova + offsetof(struct nitrox_softreq, in.sgcomp);
405 create_cipher_auth_oop_outbuf(struct nitrox_softreq *sr,
406 struct nitrox_sglist *digest)
409 struct nitrox_crypto_ctx *ctx = sr->ctx;
411 err = create_cipher_auth_sglist(sr, &sr->out, sr->op->sym->m_dst);
415 if (ctx->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE)
416 fill_sglist(&sr->out, digest->len, digest->iova, digest->virt);
422 create_cipher_auth_inplace_outbuf(struct nitrox_softreq *sr,
423 struct nitrox_sglist *digest)
426 struct nitrox_crypto_ctx *ctx = sr->ctx;
428 cnt = sr->out.map_bufs_cnt;
429 for (i = 0; i < sr->in.map_bufs_cnt; i++, cnt++) {
430 sr->out.sglist[cnt].len = sr->in.sglist[i].len;
431 sr->out.sglist[cnt].iova = sr->in.sglist[i].iova;
432 sr->out.sglist[cnt].virt = sr->in.sglist[i].virt;
435 sr->out.map_bufs_cnt = cnt;
436 if (ctx->auth_op == RTE_CRYPTO_AUTH_OP_GENERATE) {
437 fill_sglist(&sr->out, digest->len, digest->iova,
439 } else if (ctx->auth_op == RTE_CRYPTO_AUTH_OP_VERIFY) {
440 sr->out.map_bufs_cnt--;
445 create_cipher_auth_outbuf(struct nitrox_softreq *sr,
446 struct nitrox_sglist *digest)
448 struct rte_crypto_op *op = sr->op;
451 sr->resp.orh = PENDING_SIG;
452 sr->out.sglist[cnt].len = sizeof(sr->resp.orh);
453 sr->out.sglist[cnt].iova = sr->iova + offsetof(struct nitrox_softreq,
455 sr->out.sglist[cnt].virt = &sr->resp.orh;
457 sr->out.map_bufs_cnt = cnt;
458 if (op->sym->m_dst) {
461 err = create_cipher_auth_oop_outbuf(sr, digest);
465 create_cipher_auth_inplace_outbuf(sr, digest);
468 cnt = sr->out.map_bufs_cnt;
469 sr->resp.completion = PENDING_SIG;
470 sr->out.sglist[cnt].len = sizeof(sr->resp.completion);
471 sr->out.sglist[cnt].iova = sr->iova + offsetof(struct nitrox_softreq,
473 sr->out.sglist[cnt].virt = &sr->resp.completion;
475 RTE_VERIFY(cnt <= MAX_SGBUF_CNT);
476 sr->out.map_bufs_cnt = cnt;
478 create_sgcomp(&sr->out);
479 sr->rptr = sr->iova + offsetof(struct nitrox_softreq, out.sgcomp);
484 create_aead_gph(uint32_t cryptlen, uint16_t ivlen, uint32_t authlen,
490 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
491 uint16_t iv_offset : 8;
492 uint16_t auth_offset : 8;
494 uint16_t auth_offset : 8;
495 uint16_t iv_offset : 8;
501 gph->param0 = rte_cpu_to_be_16(cryptlen);
502 gph->param1 = rte_cpu_to_be_16(authlen);
504 auth_only_len = authlen - cryptlen;
505 gph->param2 = rte_cpu_to_be_16(ivlen + auth_only_len);
507 param3.iv_offset = 0;
508 param3.auth_offset = ivlen;
509 gph->param3 = rte_cpu_to_be_16(param3.value);
513 process_cipher_auth_data(struct nitrox_softreq *sr)
515 struct rte_crypto_op *op = sr->op;
517 struct nitrox_sglist digest;
520 err = extract_cipher_auth_digest(sr, &digest);
524 err = create_cipher_auth_inbuf(sr, &digest);
528 err = create_cipher_auth_outbuf(sr, &digest);
532 create_aead_gph(op->sym->cipher.data.length, sr->iv.len,
533 op->sym->auth.data.length, &sr->gph);
538 process_softreq(struct nitrox_softreq *sr)
540 struct nitrox_crypto_ctx *ctx = sr->ctx;
543 switch (ctx->nitrox_chain) {
544 case NITROX_CHAIN_CIPHER_AUTH:
545 case NITROX_CHAIN_AUTH_CIPHER:
546 err = process_cipher_auth_data(sr);
557 nitrox_process_se_req(uint16_t qno, struct rte_crypto_op *op,
558 struct nitrox_crypto_ctx *ctx,
559 struct nitrox_softreq *sr)
561 softreq_init(sr, sr->iova);
565 create_se_instr(sr, qno);
566 sr->timeout = rte_get_timer_cycles() + CMD_TIMEOUT * rte_get_timer_hz();
571 nitrox_check_se_req(struct nitrox_softreq *sr, struct rte_crypto_op **op)
577 cc = *(volatile uint64_t *)(&sr->resp.completion);
578 orh = *(volatile uint64_t *)(&sr->resp.orh);
579 if (cc != PENDING_SIG)
581 else if ((orh != PENDING_SIG) && (orh & 0xff))
583 else if (rte_get_timer_cycles() >= sr->timeout)
589 NITROX_LOG(ERR, "Request err 0x%x, orh 0x%"PRIx64"\n", err,
597 nitrox_sym_instr_addr(struct nitrox_softreq *sr)
603 req_pool_obj_init(__rte_unused struct rte_mempool *mp,
604 __rte_unused void *opaque, void *obj,
605 __rte_unused unsigned int obj_idx)
607 softreq_init(obj, rte_mempool_virt2iova(obj));
611 nitrox_sym_req_pool_create(struct rte_cryptodev *cdev, uint32_t nobjs,
612 uint16_t qp_id, int socket_id)
614 char softreq_pool_name[RTE_RING_NAMESIZE];
615 struct rte_mempool *mp;
617 snprintf(softreq_pool_name, RTE_RING_NAMESIZE, "%s_sr_%d",
618 cdev->data->name, qp_id);
619 mp = rte_mempool_create(softreq_pool_name,
620 RTE_ALIGN_MUL_CEIL(nobjs, 64),
621 sizeof(struct nitrox_softreq),
622 64, 0, NULL, NULL, req_pool_obj_init, NULL,
625 NITROX_LOG(ERR, "Failed to create req pool, qid %d, err %d\n",
632 nitrox_sym_req_pool_free(struct rte_mempool *mp)
634 rte_mempool_free(mp);