1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (C) 2020 Marvell International Ltd.
5 #include <rte_cryptodev.h>
7 #include <rte_ethdev.h>
8 #include <rte_eventdev.h>
10 #include <rte_malloc.h>
11 #include <rte_memzone.h>
12 #include <rte_security.h>
13 #include <rte_security_driver.h>
16 #include "otx2_common.h"
17 #include "otx2_cryptodev_qp.h"
18 #include "otx2_ethdev.h"
19 #include "otx2_ethdev_sec.h"
20 #include "otx2_ipsec_fp.h"
21 #include "otx2_sec_idev.h"
22 #include "otx2_security.h"
24 struct eth_sec_tag_const {
28 uint32_t rsvd_11_0 : 12;
30 uint32_t event_type : 4;
31 uint32_t rsvd_31_24 : 8;
37 static struct rte_cryptodev_capabilities otx2_eth_sec_crypto_caps[] = {
39 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
41 .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
43 .algo = RTE_CRYPTO_AEAD_AES_GCM,
69 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
71 .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
73 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
89 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
91 .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
93 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
108 RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
111 static const struct rte_security_capability otx2_eth_sec_capabilities[] = {
112 { /* IPsec Inline Protocol ESP Tunnel Ingress */
113 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
114 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
116 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
117 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
118 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
121 .crypto_capabilities = otx2_eth_sec_crypto_caps,
122 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
124 { /* IPsec Inline Protocol ESP Tunnel Egress */
125 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
126 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
128 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
129 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
130 .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
133 .crypto_capabilities = otx2_eth_sec_crypto_caps,
134 .ol_flags = RTE_SECURITY_TX_OLOAD_NEED_MDATA
137 .action = RTE_SECURITY_ACTION_TYPE_NONE
142 lookup_mem_sa_tbl_clear(struct rte_eth_dev *eth_dev)
144 static const char name[] = OTX2_NIX_FASTPATH_LOOKUP_MEM;
145 uint16_t port = eth_dev->data->port_id;
146 const struct rte_memzone *mz;
150 mz = rte_memzone_lookup(name);
156 sa_tbl = (uint64_t **)RTE_PTR_ADD(mem, OTX2_NIX_SA_TBL_START);
157 if (sa_tbl[port] == NULL)
160 rte_free(sa_tbl[port]);
165 lookup_mem_sa_index_update(struct rte_eth_dev *eth_dev, int spi, void *sa)
167 static const char name[] = OTX2_NIX_FASTPATH_LOOKUP_MEM;
168 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
169 uint16_t port = eth_dev->data->port_id;
170 const struct rte_memzone *mz;
174 mz = rte_memzone_lookup(name);
176 otx2_err("Could not find fastpath lookup table");
182 sa_tbl = (uint64_t **)RTE_PTR_ADD(mem, OTX2_NIX_SA_TBL_START);
184 if (sa_tbl[port] == NULL) {
185 sa_tbl[port] = rte_malloc(NULL, dev->ipsec_in_max_spi *
186 sizeof(uint64_t), 0);
189 sa_tbl[port][spi] = (uint64_t)sa;
195 in_sa_mz_name_get(char *name, int size, uint16_t port)
197 snprintf(name, size, "otx2_ipsec_in_sadb_%u", port);
200 static struct otx2_ipsec_fp_in_sa *
201 in_sa_get(uint16_t port, int sa_index)
203 char name[RTE_MEMZONE_NAMESIZE];
204 struct otx2_ipsec_fp_in_sa *sa;
205 const struct rte_memzone *mz;
207 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
208 mz = rte_memzone_lookup(name);
210 otx2_err("Could not get the memzone reserved for IN SA DB");
216 return sa + sa_index;
220 ipsec_sa_const_set(struct rte_security_ipsec_xform *ipsec,
221 struct rte_crypto_sym_xform *xform,
222 struct otx2_sec_session_ipsec_ip *sess)
224 struct rte_crypto_sym_xform *cipher_xform, *auth_xform;
226 sess->partial_len = sizeof(struct rte_ipv4_hdr);
228 if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
229 sess->partial_len += sizeof(struct rte_esp_hdr);
230 sess->roundup_len = sizeof(struct rte_esp_tail);
231 } else if (ipsec->proto == RTE_SECURITY_IPSEC_SA_PROTO_AH) {
232 sess->partial_len += OTX2_SEC_AH_HDR_LEN;
237 if (ipsec->options.udp_encap)
238 sess->partial_len += sizeof(struct rte_udp_hdr);
240 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
241 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
242 sess->partial_len += OTX2_SEC_AES_GCM_IV_LEN;
243 sess->partial_len += OTX2_SEC_AES_GCM_MAC_LEN;
244 sess->roundup_byte = OTX2_SEC_AES_GCM_ROUNDUP_BYTE_LEN;
249 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
250 cipher_xform = xform;
251 auth_xform = xform->next;
252 } else if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
254 cipher_xform = xform->next;
258 if (cipher_xform->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
259 sess->partial_len += OTX2_SEC_AES_CBC_IV_LEN;
260 sess->roundup_byte = OTX2_SEC_AES_CBC_ROUNDUP_BYTE_LEN;
265 if (auth_xform->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
266 sess->partial_len += OTX2_SEC_SHA1_HMAC_LEN;
274 hmac_init(struct otx2_ipsec_fp_sa_ctl *ctl, struct otx2_cpt_qp *qp,
275 const uint8_t *auth_key, int len, uint8_t *hmac_key)
278 struct otx2_cpt_res cpt_res;
282 volatile struct otx2_cpt_res *res;
283 uint64_t timeout, lmt_status;
284 struct otx2_cpt_inst_s inst;
288 memset(&inst, 0, sizeof(struct otx2_cpt_inst_s));
290 md = rte_zmalloc(NULL, sizeof(struct inst_data), OTX2_CPT_RES_ALIGN);
294 memcpy(md->buffer, auth_key, len);
296 md_iova = rte_malloc_virt2iova(md);
297 if (md_iova == RTE_BAD_IOVA) {
302 inst.res_addr = md_iova + offsetof(struct inst_data, cpt_res);
303 inst.opcode = OTX2_CPT_OP_WRITE_HMAC_IPAD_OPAD;
304 inst.param2 = ctl->auth_type;
306 inst.dptr = md_iova + offsetof(struct inst_data, buffer);
307 inst.rptr = inst.dptr;
308 inst.egrp = OTX2_CPT_EGRP_INLINE_IPSEC;
310 md->cpt_res.compcode = 0;
311 md->cpt_res.uc_compcode = 0xff;
313 timeout = rte_get_timer_cycles() + 5 * rte_get_timer_hz();
318 otx2_lmt_mov(qp->lmtline, &inst, 2);
319 lmt_status = otx2_lmt_submit(qp->lf_nq_reg);
320 } while (lmt_status == 0);
322 res = (volatile struct otx2_cpt_res *)&md->cpt_res;
324 /* Wait until instruction completes or times out */
325 while (res->uc_compcode == 0xff) {
326 if (rte_get_timer_cycles() > timeout)
330 if (res->u16[0] != OTX2_SEC_COMP_GOOD) {
335 /* Retrieve the ipad and opad from rptr */
336 memcpy(hmac_key, md->buffer, 48);
346 eth_sec_ipsec_out_sess_create(struct rte_eth_dev *eth_dev,
347 struct rte_security_ipsec_xform *ipsec,
348 struct rte_crypto_sym_xform *crypto_xform,
349 struct rte_security_session *sec_sess)
351 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
352 struct otx2_sec_session_ipsec_ip *sess;
353 uint16_t port = eth_dev->data->port_id;
354 int cipher_key_len, auth_key_len, ret;
355 const uint8_t *cipher_key, *auth_key;
356 struct otx2_ipsec_fp_sa_ctl *ctl;
357 struct otx2_ipsec_fp_out_sa *sa;
358 struct otx2_sec_session *priv;
359 struct otx2_cpt_inst_s inst;
360 struct otx2_cpt_qp *qp;
362 priv = get_sec_session_private_data(sec_sess);
363 sess = &priv->ipsec.ip;
368 otx2_err("SA already registered");
372 memset(sess, 0, sizeof(struct otx2_sec_session_ipsec_ip));
376 ret = ipsec_sa_const_set(ipsec, crypto_xform, sess);
380 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
381 memcpy(sa->nonce, &ipsec->salt, 4);
383 if (ipsec->options.udp_encap == 1) {
388 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
389 /* Start ip id from 1 */
392 if (ipsec->tunnel.type == RTE_SECURITY_IPSEC_TUNNEL_IPV4) {
393 memcpy(&sa->ip_src, &ipsec->tunnel.ipv4.src_ip,
394 sizeof(struct in_addr));
395 memcpy(&sa->ip_dst, &ipsec->tunnel.ipv4.dst_ip,
396 sizeof(struct in_addr));
404 cipher_xform = crypto_xform;
405 auth_xform = crypto_xform->next;
411 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
412 cipher_key = crypto_xform->aead.key.data;
413 cipher_key_len = crypto_xform->aead.key.length;
415 cipher_key = cipher_xform->cipher.key.data;
416 cipher_key_len = cipher_xform->cipher.key.length;
417 auth_key = auth_xform->auth.key.data;
418 auth_key_len = auth_xform->auth.key.length;
421 if (cipher_key_len != 0)
422 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
426 /* Determine word 7 of CPT instruction */
428 inst.egrp = OTX2_CPT_EGRP_INLINE_IPSEC;
429 inst.cptr = rte_mempool_virt2iova(sa);
430 sess->inst_w7 = inst.u64[7];
432 /* Get CPT QP to be used for this SA */
433 ret = otx2_sec_idev_tx_cpt_qp_get(port, &qp);
439 sess->cpt_lmtline = qp->lmtline;
440 sess->cpt_nq_reg = qp->lf_nq_reg;
442 /* Populate control word */
443 ret = ipsec_fp_sa_ctl_set(ipsec, crypto_xform, ctl);
447 if (auth_key_len && auth_key) {
448 ret = hmac_init(ctl, qp, auth_key, auth_key_len, sa->hmac_key);
455 otx2_sec_idev_tx_cpt_qp_put(sess->qp);
460 eth_sec_ipsec_in_sess_create(struct rte_eth_dev *eth_dev,
461 struct rte_security_ipsec_xform *ipsec,
462 struct rte_crypto_sym_xform *crypto_xform,
463 struct rte_security_session *sec_sess)
465 struct rte_crypto_sym_xform *auth_xform, *cipher_xform;
466 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
467 struct otx2_sec_session_ipsec_ip *sess;
468 uint16_t port = eth_dev->data->port_id;
469 int cipher_key_len, auth_key_len, ret;
470 const uint8_t *cipher_key, *auth_key;
471 struct otx2_ipsec_fp_sa_ctl *ctl;
472 struct otx2_ipsec_fp_in_sa *sa;
473 struct otx2_sec_session *priv;
474 struct otx2_cpt_qp *qp;
476 if (ipsec->spi >= dev->ipsec_in_max_spi) {
477 otx2_err("SPI exceeds max supported");
481 sa = in_sa_get(port, ipsec->spi);
484 priv = get_sec_session_private_data(sec_sess);
485 sess = &priv->ipsec.ip;
488 otx2_err("SA already registered");
492 memset(sa, 0, sizeof(struct otx2_ipsec_fp_in_sa));
494 auth_xform = crypto_xform;
495 cipher_xform = crypto_xform->next;
501 if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
502 if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
503 memcpy(sa->nonce, &ipsec->salt, 4);
504 cipher_key = crypto_xform->aead.key.data;
505 cipher_key_len = crypto_xform->aead.key.length;
507 cipher_key = cipher_xform->cipher.key.data;
508 cipher_key_len = cipher_xform->cipher.key.length;
509 auth_key = auth_xform->auth.key.data;
510 auth_key_len = auth_xform->auth.key.length;
513 if (cipher_key_len != 0)
514 memcpy(sa->cipher_key, cipher_key, cipher_key_len);
520 sa->userdata = priv->userdata;
522 if (lookup_mem_sa_index_update(eth_dev, ipsec->spi, sa))
525 ret = ipsec_fp_sa_ctl_set(ipsec, crypto_xform, ctl);
529 if (auth_key_len && auth_key) {
530 /* Get a queue pair for HMAC init */
531 ret = otx2_sec_idev_tx_cpt_qp_get(port, &qp);
534 ret = hmac_init(ctl, qp, auth_key, auth_key_len, sa->hmac_key);
535 otx2_sec_idev_tx_cpt_qp_put(qp);
541 eth_sec_ipsec_sess_create(struct rte_eth_dev *eth_dev,
542 struct rte_security_ipsec_xform *ipsec,
543 struct rte_crypto_sym_xform *crypto_xform,
544 struct rte_security_session *sess)
548 ret = ipsec_fp_xform_verify(ipsec, crypto_xform);
552 if (ipsec->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
553 return eth_sec_ipsec_in_sess_create(eth_dev, ipsec,
556 return eth_sec_ipsec_out_sess_create(eth_dev, ipsec,
561 otx2_eth_sec_session_create(void *device,
562 struct rte_security_session_conf *conf,
563 struct rte_security_session *sess,
564 struct rte_mempool *mempool)
566 struct otx2_sec_session *priv;
569 if (conf->action_type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
572 if (rte_mempool_get(mempool, (void **)&priv)) {
573 otx2_err("Could not allocate security session private data");
577 set_sec_session_private_data(sess, priv);
580 * Save userdata provided by the application. For ingress packets, this
581 * could be used to identify the SA.
583 priv->userdata = conf->userdata;
585 if (conf->protocol == RTE_SECURITY_PROTOCOL_IPSEC)
586 ret = eth_sec_ipsec_sess_create(device, &conf->ipsec,
598 rte_mempool_put(mempool, priv);
599 set_sec_session_private_data(sess, NULL);
604 otx2_eth_sec_session_destroy(void *device __rte_unused,
605 struct rte_security_session *sess)
607 struct otx2_sec_session_ipsec_ip *sess_ip;
608 struct otx2_sec_session *priv;
609 struct rte_mempool *sess_mp;
612 priv = get_sec_session_private_data(sess);
616 sess_ip = &priv->ipsec.ip;
618 /* Release CPT LF used for this session */
619 if (sess_ip->qp != NULL) {
620 ret = otx2_sec_idev_tx_cpt_qp_put(sess_ip->qp);
625 sess_mp = rte_mempool_from_obj(priv);
627 set_sec_session_private_data(sess, NULL);
628 rte_mempool_put(sess_mp, priv);
634 otx2_eth_sec_session_get_size(void *device __rte_unused)
636 return sizeof(struct otx2_sec_session);
640 otx2_eth_sec_set_pkt_mdata(void *device __rte_unused,
641 struct rte_security_session *session,
642 struct rte_mbuf *m, void *params __rte_unused)
644 /* Set security session as the pkt metadata */
645 m->udata64 = (uint64_t)session;
651 otx2_eth_sec_get_userdata(void *device __rte_unused, uint64_t md,
654 /* Retrieve userdata */
655 *userdata = (void *)md;
660 static const struct rte_security_capability *
661 otx2_eth_sec_capabilities_get(void *device __rte_unused)
663 return otx2_eth_sec_capabilities;
666 static struct rte_security_ops otx2_eth_sec_ops = {
667 .session_create = otx2_eth_sec_session_create,
668 .session_destroy = otx2_eth_sec_session_destroy,
669 .session_get_size = otx2_eth_sec_session_get_size,
670 .set_pkt_metadata = otx2_eth_sec_set_pkt_mdata,
671 .get_userdata = otx2_eth_sec_get_userdata,
672 .capabilities_get = otx2_eth_sec_capabilities_get
676 otx2_eth_sec_ctx_create(struct rte_eth_dev *eth_dev)
678 struct rte_security_ctx *ctx;
681 ctx = rte_malloc("otx2_eth_sec_ctx",
682 sizeof(struct rte_security_ctx), 0);
686 ret = otx2_sec_idev_cfg_init(eth_dev->data->port_id);
694 ctx->device = eth_dev;
695 ctx->ops = &otx2_eth_sec_ops;
698 eth_dev->security_ctx = ctx;
704 otx2_eth_sec_ctx_destroy(struct rte_eth_dev *eth_dev)
706 rte_free(eth_dev->security_ctx);
710 eth_sec_ipsec_cfg(struct rte_eth_dev *eth_dev, uint8_t tt)
712 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
713 uint16_t port = eth_dev->data->port_id;
714 struct nix_inline_ipsec_lf_cfg *req;
715 struct otx2_mbox *mbox = dev->mbox;
716 struct eth_sec_tag_const tag_const;
717 char name[RTE_MEMZONE_NAMESIZE];
718 const struct rte_memzone *mz;
720 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
721 mz = rte_memzone_lookup(name);
725 req = otx2_mbox_alloc_msg_nix_inline_ipsec_lf_cfg(mbox);
727 req->sa_base_addr = mz->iova;
729 req->ipsec_cfg0.tt = tt;
732 tag_const.event_type = RTE_EVENT_TYPE_ETHDEV;
733 tag_const.port = port;
734 req->ipsec_cfg0.tag_const = tag_const.u32;
736 req->ipsec_cfg0.sa_pow2_size =
737 rte_log2_u32(sizeof(struct otx2_ipsec_fp_in_sa));
738 req->ipsec_cfg0.lenm1_max = NIX_MAX_FRS - 1;
740 req->ipsec_cfg1.sa_idx_w = rte_log2_u32(dev->ipsec_in_max_spi);
741 req->ipsec_cfg1.sa_idx_max = dev->ipsec_in_max_spi - 1;
743 return otx2_mbox_process(mbox);
747 otx2_eth_sec_update_tag_type(struct rte_eth_dev *eth_dev)
749 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
750 struct otx2_mbox *mbox = dev->mbox;
751 struct nix_aq_enq_rsp *rsp;
752 struct nix_aq_enq_req *aq;
755 aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
756 aq->qidx = 0; /* Read RQ:0 context */
757 aq->ctype = NIX_AQ_CTYPE_RQ;
758 aq->op = NIX_AQ_INSTOP_READ;
760 ret = otx2_mbox_process_msg(mbox, (void *)&rsp);
762 otx2_err("Could not read RQ context");
766 /* Update tag type */
767 ret = eth_sec_ipsec_cfg(eth_dev, rsp->rq.sso_tt);
769 otx2_err("Could not update sec eth tag type");
775 otx2_eth_sec_init(struct rte_eth_dev *eth_dev)
777 const size_t sa_width = sizeof(struct otx2_ipsec_fp_in_sa);
778 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
779 uint16_t port = eth_dev->data->port_id;
780 char name[RTE_MEMZONE_NAMESIZE];
781 const struct rte_memzone *mz;
785 RTE_BUILD_BUG_ON(sa_width < 32 || sa_width > 512 ||
786 !RTE_IS_POWER_OF_2(sa_width));
788 if (!(dev->tx_offloads & DEV_TX_OFFLOAD_SECURITY) &&
789 !(dev->rx_offloads & DEV_RX_OFFLOAD_SECURITY))
792 nb_sa = dev->ipsec_in_max_spi;
793 mz_sz = nb_sa * sa_width;
794 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
795 mz = rte_memzone_reserve_aligned(name, mz_sz, rte_socket_id(),
796 RTE_MEMZONE_IOVA_CONTIG, OTX2_ALIGN);
799 otx2_err("Could not allocate inbound SA DB");
803 memset(mz->addr, 0, mz_sz);
805 ret = eth_sec_ipsec_cfg(eth_dev, SSO_TT_ORDERED);
807 otx2_err("Could not configure inline IPsec");
814 otx2_err("Could not configure device for security");
815 otx2_eth_sec_fini(eth_dev);
820 otx2_eth_sec_fini(struct rte_eth_dev *eth_dev)
822 struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
823 uint16_t port = eth_dev->data->port_id;
824 char name[RTE_MEMZONE_NAMESIZE];
826 if (!(dev->tx_offloads & DEV_TX_OFFLOAD_SECURITY) &&
827 !(dev->rx_offloads & DEV_RX_OFFLOAD_SECURITY))
830 lookup_mem_sa_tbl_clear(eth_dev);
832 in_sa_mz_name_get(name, RTE_MEMZONE_NAMESIZE, port);
833 rte_memzone_free(rte_memzone_lookup(name));