1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2020 Marvell International Ltd.
5 #include <rte_cryptodev.h>
7 #include <rte_ethdev.h>
9 #include <rte_malloc.h>
10 #include <rte_security.h>
11 #include <rte_security_driver.h>
14 #include "otx2_cryptodev.h"
15 #include "otx2_cryptodev_capabilities.h"
16 #include "otx2_cryptodev_hw_access.h"
17 #include "otx2_cryptodev_ops.h"
18 #include "otx2_cryptodev_sec.h"
19 #include "otx2_security.h"
22 ipsec_lp_len_precalc(struct rte_security_ipsec_xform *ipsec,
23 struct rte_crypto_sym_xform *xform,
24 struct otx2_sec_session_ipsec_lp *lp)
26 struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
28 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
29 lp->partial_len = sizeof(struct rte_ipv4_hdr);
30 else if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV6)
31 lp->partial_len = sizeof(struct rte_ipv6_hdr);
35 if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
36 lp->partial_len += sizeof(struct rte_esp_hdr);
37 lp->roundup_len = sizeof(struct rte_esp_tail);
38 } else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH) {
39 lp->partial_len += OTX2_SEC_AH_HDR_LEN;
44 if (ipsec->options.udp_encap)
45 lp->partial_len += sizeof(struct rte_udp_hdr);
47 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
48 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
49 lp->partial_len += OTX2_SEC_AES_GCM_IV_LEN;
50 lp->partial_len += OTX2_SEC_AES_GCM_MAC_LEN;
51 lp->roundup_byte = OTX2_SEC_AES_GCM_ROUNDUP_BYTE_LEN;
58 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
60 auth_xform = xform->next;
61 } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
63 cipher_xform = xform->next;
68 if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
69 lp->partial_len += OTX2_SEC_AES_CBC_IV_LEN;
70 lp->roundup_byte = OTX2_SEC_AES_CBC_ROUNDUP_BYTE_LEN;
75 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
76 lp->partial_len += OTX2_SEC_SHA1_HMAC_LEN;
77 else if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
78 lp->partial_len += OTX2_SEC_SHA2_HMAC_LEN;
86 otx2_cpt_enq_sa_write(struct otx2_sec_session_ipsec_lp *lp,
87 struct otx2_cpt_qp *qptr, uint8_t opcode)
89 uint64_t lmt_status, time_out;
90 void *lmtline = qptr->lmtline;
91 struct otx2_cpt_inst_s inst;
92 struct otx2_cpt_res *res;
96 if (unlikely(rte_mempool_get(qptr->meta_info.pool,
97 (void **)&mdata) < 0))
100 res = (struct otx2_cpt_res *)RTE_PTR_ALIGN(mdata, 16);
101 res->compcode = CPT_9X_COMP_E_NOTDONE;
103 inst.opcode = opcode | (lp->ctx_len << 8);
106 inst.dlen = lp->ctx_len << 3;
107 inst.dptr = rte_mempool_virt2iova(lp);
109 inst.cptr = rte_mempool_virt2iova(lp);
110 inst.egrp = OTX2_CPT_EGRP_SE;
115 inst.res_addr = rte_mempool_virt2iova(res);
120 /* Copy CPT command to LMTLINE */
121 otx2_lmt_mov(lmtline, &inst, 2);
122 lmt_status = otx2_lmt_submit(qptr->lf_nq_reg);
123 } while (lmt_status == 0);
125 time_out = rte_get_timer_cycles() +
126 DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
128 while (res->compcode == CPT_9X_COMP_E_NOTDONE) {
129 if (rte_get_timer_cycles() > time_out) {
130 rte_mempool_put(qptr->meta_info.pool, mdata);
131 otx2_err("Request timed out");
137 if (unlikely(res->compcode != CPT_9X_COMP_E_GOOD)) {
140 case CPT_9X_COMP_E_INSTERR:
141 otx2_err("Request failed with instruction error");
143 case CPT_9X_COMP_E_FAULT:
144 otx2_err("Request failed with DMA fault");
146 case CPT_9X_COMP_E_HWERR:
147 otx2_err("Request failed with hardware error");
150 otx2_err("Request failed with unknown hardware "
151 "completion code : 0x%x", ret);
156 if (unlikely(res->uc_compcode != OTX2_IPSEC_PO_CC_SUCCESS)) {
157 ret = res->uc_compcode;
159 case OTX2_IPSEC_PO_CC_AUTH_UNSUPPORTED:
160 otx2_err("Invalid auth type");
162 case OTX2_IPSEC_PO_CC_ENCRYPT_UNSUPPORTED:
163 otx2_err("Invalid encrypt type");
166 otx2_err("Request failed with unknown microcode "
167 "completion code : 0x%x", ret);
172 rte_mempool_put(qptr->meta_info.pool, mdata);
177 set_session_misc_attributes(struct otx2_sec_session_ipsec_lp *sess,
178 struct rte_crypto_sym_xform *crypto_xform,
179 struct rte_crypto_sym_xform *auth_xform,
180 struct rte_crypto_sym_xform *cipher_xform)
182 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
183 sess->iv_offset = crypto_xform->aead.iv.offset;
184 sess->iv_length = crypto_xform->aead.iv.length;
185 sess->aad_length = crypto_xform->aead.aad_length;
186 sess->mac_len = crypto_xform->aead.digest_length;
188 sess->iv_offset = cipher_xform->cipher.iv.offset;
189 sess->iv_length = cipher_xform->cipher.iv.length;
190 sess->auth_iv_offset = auth_xform->auth.iv.offset;
191 sess->auth_iv_length = auth_xform->auth.iv.length;
192 sess->mac_len = auth_xform->auth.digest_length;
195 sess->ucmd_param1 = OTX2_IPSEC_PO_PER_PKT_IV;
196 sess->ucmd_param2 = 0;
200 crypto_sec_ipsec_outb_session_create(struct rte_cryptodev *crypto_dev,
201 struct rte_security_ipsec_xform *ipsec,
202 struct rte_crypto_sym_xform *crypto_xform,
203 struct rte_security_session *sec_sess)
205 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
206 struct otx2_ipsec_po_ip_template *template;
207 const uint8_t *cipher_key, *auth_key;
208 struct otx2_sec_session_ipsec_lp *lp;
209 struct otx2_ipsec_po_sa_ctl *ctl;
210 int cipher_key_len, auth_key_len;
211 struct otx2_ipsec_po_out_sa *sa;
212 struct otx2_sec_session *sess;
213 struct otx2_cpt_inst_s inst;
214 struct rte_ipv6_hdr *ip6;
215 struct rte_ipv4_hdr *ip;
218 sess = get_sec_session_private_data(sec_sess);
219 sess->ipsec.dir = RTE_SECURITY_IPSEC_SA_DIR_EGRESS;
220 lp = &sess->ipsec.lp;
225 otx2_err("SA already registered");
229 memset(sa, 0, sizeof(struct otx2_ipsec_po_out_sa));
231 /* Initialize lookaside ipsec private data */
235 lp->tunnel_type = ipsec->tunnel.type;
237 ret = ipsec_po_sa_ctl_set(ipsec, crypto_xform, ctl);
241 ret = ipsec_lp_len_precalc(ipsec, crypto_xform, lp);
245 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
246 /* Start ip id from 1 */
249 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
251 if (ctl->enc_type == OTX2_IPSEC_PO_SA_ENC_AES_GCM) {
252 template = &sa->aes_gcm.template;
253 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
254 aes_gcm.template) + sizeof(
255 sa->aes_gcm.template.ip4);
256 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
257 lp->ctx_len = ctx_len >> 3;
258 } else if (ctl->auth_type ==
259 OTX2_IPSEC_PO_SA_AUTH_SHA1) {
260 template = &sa->sha1.template;
261 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
262 sha1.template) + sizeof(
263 sa->sha1.template.ip4);
264 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
265 lp->ctx_len = ctx_len >> 3;
266 } else if (ctl->auth_type ==
267 OTX2_IPSEC_PO_SA_AUTH_SHA2_256) {
268 template = &sa->sha2.template;
269 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
270 sha2.template) + sizeof(
271 sa->sha2.template.ip4);
272 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
273 lp->ctx_len = ctx_len >> 3;
277 ip = &template->ip4.ipv4_hdr;
278 if (ipsec->options.udp_encap) {
279 ip->next_proto_id = IPPROTO_UDP;
280 template->ip4.udp_src = rte_be_to_cpu_16(4500);
281 template->ip4.udp_dst = rte_be_to_cpu_16(4500);
283 ip->next_proto_id = IPPROTO_ESP;
285 ip->version_ihl = RTE_IPV4_VHL_DEF;
286 ip->time_to_live = ipsec->tunnel.ipv4.ttl;
287 ip->type_of_service |= (ipsec->tunnel.ipv4.dscp << 2);
288 if (ipsec->tunnel.ipv4.df)
289 ip->fragment_offset = BIT(14);
290 memcpy(&ip->src_addr, &ipsec->tunnel.ipv4.src_ip,
291 sizeof(struct in_addr));
292 memcpy(&ip->dst_addr, &ipsec->tunnel.ipv4.dst_ip,
293 sizeof(struct in_addr));
294 } else if (ipsec->tunnel.type ==
295 RTE_SECURITY_IPSEC_TUNNEL_IPV6) {
297 if (ctl->enc_type == OTX2_IPSEC_PO_SA_ENC_AES_GCM) {
298 template = &sa->aes_gcm.template;
299 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
300 aes_gcm.template) + sizeof(
301 sa->aes_gcm.template.ip6);
302 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
303 lp->ctx_len = ctx_len >> 3;
304 } else if (ctl->auth_type ==
305 OTX2_IPSEC_PO_SA_AUTH_SHA1) {
306 template = &sa->sha1.template;
307 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
308 sha1.template) + sizeof(
309 sa->sha1.template.ip6);
310 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
311 lp->ctx_len = ctx_len >> 3;
312 } else if (ctl->auth_type ==
313 OTX2_IPSEC_PO_SA_AUTH_SHA2_256) {
314 template = &sa->sha2.template;
315 ctx_len = offsetof(struct otx2_ipsec_po_out_sa,
316 sha2.template) + sizeof(
317 sa->sha2.template.ip6);
318 ctx_len = RTE_ALIGN_CEIL(ctx_len, 8);
319 lp->ctx_len = ctx_len >> 3;
324 ip6 = &template->ip6.ipv6_hdr;
325 if (ipsec->options.udp_encap) {
326 ip6->proto = IPPROTO_UDP;
327 template->ip6.udp_src = rte_be_to_cpu_16(4500);
328 template->ip6.udp_dst = rte_be_to_cpu_16(4500);
330 ip6->proto = (ipsec->proto ==
331 RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
332 IPPROTO_ESP : IPPROTO_AH;
334 ip6->vtc_flow = rte_cpu_to_be_32(0x60000000 |
335 ((ipsec->tunnel.ipv6.dscp <<
336 RTE_IPV6_HDR_TC_SHIFT) &
337 RTE_IPV6_HDR_TC_MASK) |
338 ((ipsec->tunnel.ipv6.flabel <<
339 RTE_IPV6_HDR_FL_SHIFT) &
340 RTE_IPV6_HDR_FL_MASK));
341 ip6->hop_limits = ipsec->tunnel.ipv6.hlimit;
342 memcpy(&ip6->src_addr, &ipsec->tunnel.ipv6.src_addr,
343 sizeof(struct in6_addr));
344 memcpy(&ip6->dst_addr, &ipsec->tunnel.ipv6.dst_addr,
345 sizeof(struct in6_addr));
353 cipher_xform = crypto_xform;
354 auth_xform = crypto_xform->next;
359 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
360 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
361 memcpy(sa->iv.gcm.nonce, &ipsec->salt, 4);
362 cipher_key = crypto_xform->aead.key.data;
363 cipher_key_len = crypto_xform->aead.key.length;
365 cipher_key = cipher_xform->cipher.key.data;
366 cipher_key_len = cipher_xform->cipher.key.length;
367 auth_key = auth_xform->auth.key.data;
368 auth_key_len = auth_xform->auth.key.length;
370 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
371 memcpy(sa->sha1.hmac_key, auth_key, auth_key_len);
372 else if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
373 memcpy(sa->sha2.hmac_key, auth_key, auth_key_len);
376 if (cipher_key_len != 0)
377 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
382 inst.egrp = OTX2_CPT_EGRP_SE;
383 inst.cptr = rte_mempool_virt2iova(sa);
385 lp->cpt_inst_w7 = inst.u64[7];
386 lp->ucmd_opcode = (lp->ctx_len << 8) |
387 (OTX2_IPSEC_PO_PROCESS_IPSEC_OUTB);
389 set_session_misc_attributes(lp, crypto_xform,
390 auth_xform, cipher_xform);
392 return otx2_cpt_enq_sa_write(lp, crypto_dev->data->queue_pairs[0],
393 OTX2_IPSEC_PO_WRITE_IPSEC_OUTB);
397 crypto_sec_ipsec_inb_session_create(struct rte_cryptodev *crypto_dev,
398 struct rte_security_ipsec_xform *ipsec,
399 struct rte_crypto_sym_xform *crypto_xform,
400 struct rte_security_session *sec_sess)
402 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
403 const uint8_t *cipher_key, *auth_key;
404 struct otx2_sec_session_ipsec_lp *lp;
405 struct otx2_ipsec_po_sa_ctl *ctl;
406 int cipher_key_len, auth_key_len;
407 struct otx2_ipsec_po_in_sa *sa;
408 struct otx2_sec_session *sess;
409 struct otx2_cpt_inst_s inst;
412 sess = get_sec_session_private_data(sec_sess);
413 sess->ipsec.dir = RTE_SECURITY_IPSEC_SA_DIR_INGRESS;
414 lp = &sess->ipsec.lp;
420 otx2_err("SA already registered");
424 memset(sa, 0, sizeof(struct otx2_ipsec_po_in_sa));
425 sa->replay_win_sz = ipsec->replay_win_sz;
427 ret = ipsec_po_sa_ctl_set(ipsec, crypto_xform, ctl);
431 lp->tunnel_type = ipsec->tunnel.type;
432 auth_xform = crypto_xform;
433 cipher_xform = crypto_xform->next;
438 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
439 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
440 memcpy(sa->iv.gcm.nonce, &ipsec->salt, 4);
441 cipher_key = crypto_xform->aead.key.data;
442 cipher_key_len = crypto_xform->aead.key.length;
444 lp->ctx_len = offsetof(struct otx2_ipsec_po_in_sa,
445 aes_gcm.hmac_key[0]) >> 3;
446 RTE_ASSERT(lp->ctx_len == OTX2_IPSEC_PO_AES_GCM_INB_CTX_LEN);
448 cipher_key = cipher_xform->cipher.key.data;
449 cipher_key_len = cipher_xform->cipher.key.length;
450 auth_key = auth_xform->auth.key.data;
451 auth_key_len = auth_xform->auth.key.length;
453 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC) {
454 memcpy(sa->aes_gcm.hmac_key, auth_key, auth_key_len);
455 lp->ctx_len = offsetof(struct otx2_ipsec_po_in_sa,
456 aes_gcm.selector) >> 3;
457 } else if (auth_xform->auth.algo ==
458 RTE_CRYPTO_AUTH_SHA256_HMAC) {
459 memcpy(sa->sha2.hmac_key, auth_key, auth_key_len);
460 lp->ctx_len = offsetof(struct otx2_ipsec_po_in_sa,
465 if (cipher_key_len != 0)
466 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
471 inst.egrp = OTX2_CPT_EGRP_SE;
472 inst.cptr = rte_mempool_virt2iova(sa);
474 lp->cpt_inst_w7 = inst.u64[7];
475 lp->ucmd_opcode = (lp->ctx_len << 8) |
476 (OTX2_IPSEC_PO_PROCESS_IPSEC_INB);
478 set_session_misc_attributes(lp, crypto_xform,
479 auth_xform, cipher_xform);
481 if (sa->replay_win_sz) {
482 if (sa->replay_win_sz > OTX2_IPSEC_MAX_REPLAY_WIN_SZ) {
483 otx2_err("Replay window size is not supported");
486 sa->replay = rte_zmalloc(NULL, sizeof(struct otx2_ipsec_replay),
488 if (sa->replay == NULL)
491 /* Set window bottom to 1, base and top to size of window */
492 sa->replay->winb = 1;
493 sa->replay->wint = sa->replay_win_sz;
494 sa->replay->base = sa->replay_win_sz;
499 return otx2_cpt_enq_sa_write(lp, crypto_dev->data->queue_pairs[0],
500 OTX2_IPSEC_PO_WRITE_IPSEC_INB);
504 crypto_sec_ipsec_session_create(struct rte_cryptodev *crypto_dev,
505 struct rte_security_ipsec_xform *ipsec,
506 struct rte_crypto_sym_xform *crypto_xform,
507 struct rte_security_session *sess)
511 if (crypto_dev->data->queue_pairs[0] == NULL) {
512 otx2_err("Setup cpt queue pair before creating sec session");
516 ret = ipsec_po_xform_verify(ipsec, crypto_xform);
520 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
521 return crypto_sec_ipsec_inb_session_create(crypto_dev, ipsec,
524 return crypto_sec_ipsec_outb_session_create(crypto_dev, ipsec,
529 otx2_crypto_sec_session_create(void *device,
530 struct rte_security_session_conf *conf,
531 struct rte_security_session *sess,
532 struct rte_mempool *mempool)
534 struct otx2_sec_session *priv;
537 if (conf->action_type != RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL)
540 if (rte_security_dynfield_register() < 0)
543 if (rte_mempool_get(mempool, (void **)&priv)) {
544 otx2_err("Could not allocate security session private data");
548 set_sec_session_private_data(sess, priv);
550 priv->userdata = conf->userdata;
552 if (conf->protocol == RTE_SECURITY_PROTOCOL_IPSEC)
553 ret = crypto_sec_ipsec_session_create(device, &conf->ipsec,
565 rte_mempool_put(mempool, priv);
566 set_sec_session_private_data(sess, NULL);
571 otx2_crypto_sec_session_destroy(void *device __rte_unused,
572 struct rte_security_session *sess)
574 struct otx2_sec_session *priv;
575 struct rte_mempool *sess_mp;
577 priv = get_sec_session_private_data(sess);
582 sess_mp = rte_mempool_from_obj(priv);
584 set_sec_session_private_data(sess, NULL);
585 rte_mempool_put(sess_mp, priv);
591 otx2_crypto_sec_session_get_size(void *device __rte_unused)
593 return sizeof(struct otx2_sec_session);
597 otx2_crypto_sec_set_pkt_mdata(void *device __rte_unused,
598 struct rte_security_session *session,
599 struct rte_mbuf *m, void *params __rte_unused)
601 /* Set security session as the pkt metadata */
602 *rte_security_dynfield(m) = (rte_security_dynfield_t)session;
608 otx2_crypto_sec_get_userdata(void *device __rte_unused, uint64_t md,
611 /* Retrieve userdata */
612 *userdata = (void *)md;
617 static struct rte_security_ops otx2_crypto_sec_ops = {
618 .session_create = otx2_crypto_sec_session_create,
619 .session_destroy = otx2_crypto_sec_session_destroy,
620 .session_get_size = otx2_crypto_sec_session_get_size,
621 .set_pkt_metadata = otx2_crypto_sec_set_pkt_mdata,
622 .get_userdata = otx2_crypto_sec_get_userdata,
623 .capabilities_get = otx2_crypto_sec_capabilities_get
627 otx2_crypto_sec_ctx_create(struct rte_cryptodev *cdev)
629 struct rte_security_ctx *ctx;
631 ctx = rte_malloc("otx2_cpt_dev_sec_ctx",
632 sizeof(struct rte_security_ctx), 0);
639 ctx->ops = &otx2_crypto_sec_ops;
642 cdev->security_ctx = ctx;
648 otx2_crypto_sec_ctx_destroy(struct rte_cryptodev *cdev)
650 rte_free(cdev->security_ctx);