mbuf: add namespace to offload flags
[dpdk.git] / drivers / crypto / cnxk / cn10k_cryptodev_ops.c
index 2e1a739..d25a17c 100644 (file)
@@ -3,7 +3,8 @@
  */
 
 #include <rte_cryptodev.h>
-#include <rte_cryptodev_pmd.h>
+#include <cryptodev_pmd.h>
+#include <rte_event_crypto_adapter.h>
 #include <rte_ip.h>
 
 #include "cn10k_cryptodev.h"
@@ -15,6 +16,8 @@
 #include "cnxk_cryptodev_ops.h"
 #include "cnxk_se.h"
 
+#include "roc_api.h"
+
 static inline struct cnxk_se_sess *
 cn10k_cpt_sym_temp_sess_create(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op)
 {
@@ -67,7 +70,7 @@ cpt_sec_inst_fill(struct rte_crypto_op *op, struct cn10k_sec_session *sess,
        sa = &sess->sa;
        w2 = (union roc_ot_ipsec_sa_word2 *)&sa->in_sa.w2;
 
-       if (w2->s.dir == ROC_IE_OT_SA_DIR_OUTBOUND)
+       if (w2->s.dir == ROC_IE_SA_DIR_OUTBOUND)
                ret = process_outb_sa(op, sa, inst);
        else {
                infl_req->op_flags |= CPT_OP_FLAGS_IPSEC_DIR_INBOUND;
@@ -83,7 +86,7 @@ cpt_sym_inst_fill(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op,
                  struct cpt_inst_s *inst)
 {
        uint64_t cpt_op;
-       int ret = -1;
+       int ret;
 
        cpt_op = sess->cpt_op;
 
@@ -193,11 +196,15 @@ cn10k_cpt_enqueue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
        struct pending_queue *pend_q;
        struct cpt_inst_s *inst;
        uint16_t lmt_id;
+       uint64_t head;
        int ret, i;
 
        pend_q = &qp->pend_q;
 
-       nb_allowed = qp->lf.nb_desc - pend_q->pending_count;
+       const uint64_t pq_mask = pend_q->pq_mask;
+
+       head = pend_q->head;
+       nb_allowed = pending_queue_free_cnt(head, pend_q->tail, pq_mask);
        nb_ops = RTE_MIN(nb_ops, nb_allowed);
 
        if (unlikely(nb_ops == 0))
@@ -211,18 +218,18 @@ cn10k_cpt_enqueue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
 
 again:
        for (i = 0; i < RTE_MIN(PKTS_PER_LOOP, nb_ops); i++) {
-               infl_req = &pend_q->req_queue[pend_q->enq_tail];
+               infl_req = &pend_q->req_queue[head];
                infl_req->op_flags = 0;
 
                ret = cn10k_cpt_fill_inst(qp, ops + i, &inst[2 * i], infl_req);
                if (unlikely(ret != 1)) {
                        plt_dp_err("Could not process op: %p", ops + i);
                        if (i == 0)
-                               goto update_pending;
+                               goto pend_q_commit;
                        break;
                }
 
-               MOD_INC(pend_q->enq_tail, qp->lf.nb_desc);
+               pending_queue_advance(&head, pq_mask);
        }
 
        if (i > PKTS_PER_STEORL) {
@@ -248,40 +255,141 @@ again:
                goto again;
        }
 
-update_pending:
-       pend_q->pending_count += count + i;
+pend_q_commit:
+       rte_atomic_thread_fence(__ATOMIC_RELEASE);
 
+       pend_q->head = head;
        pend_q->time_out = rte_get_timer_cycles() +
                           DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
 
        return count + i;
 }
 
+uint16_t
+cn10k_cpt_crypto_adapter_enqueue(uintptr_t tag_op, struct rte_crypto_op *op)
+{
+       union rte_event_crypto_metadata *ec_mdata;
+       struct cpt_inflight_req *infl_req;
+       struct rte_event *rsp_info;
+       uint64_t lmt_base, lmt_arg;
+       struct cpt_inst_s *inst;
+       struct cnxk_cpt_qp *qp;
+       uint8_t cdev_id;
+       uint16_t lmt_id;
+       uint16_t qp_id;
+       int ret;
+
+       ec_mdata = cnxk_event_crypto_mdata_get(op);
+       if (!ec_mdata) {
+               rte_errno = EINVAL;
+               return 0;
+       }
+
+       cdev_id = ec_mdata->request_info.cdev_id;
+       qp_id = ec_mdata->request_info.queue_pair_id;
+       qp = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id];
+       rsp_info = &ec_mdata->response_info;
+
+       if (unlikely(!qp->ca.enabled)) {
+               rte_errno = EINVAL;
+               return 0;
+       }
+
+       if (unlikely(rte_mempool_get(qp->ca.req_mp, (void **)&infl_req))) {
+               rte_errno = ENOMEM;
+               return 0;
+       }
+       infl_req->op_flags = 0;
+
+       lmt_base = qp->lmtline.lmt_base;
+       ROC_LMT_BASE_ID_GET(lmt_base, lmt_id);
+       inst = (struct cpt_inst_s *)lmt_base;
+
+       ret = cn10k_cpt_fill_inst(qp, &op, inst, infl_req);
+       if (unlikely(ret != 1)) {
+               plt_dp_err("Could not process op: %p", op);
+               rte_mempool_put(qp->ca.req_mp, infl_req);
+               return 0;
+       }
+
+       infl_req->cop = op;
+       infl_req->res.cn10k.compcode = CPT_COMP_NOT_DONE;
+       infl_req->qp = qp;
+       inst->w0.u64 = 0;
+       inst->res_addr = (uint64_t)&infl_req->res;
+       inst->w2.u64 = CNXK_CPT_INST_W2(
+               (RTE_EVENT_TYPE_CRYPTODEV << 28) | rsp_info->flow_id,
+               rsp_info->sched_type, rsp_info->queue_id, 0);
+       inst->w3.u64 = CNXK_CPT_INST_W3(1, infl_req);
+
+       if (roc_cpt_is_iq_full(&qp->lf)) {
+               rte_mempool_put(qp->ca.req_mp, infl_req);
+               rte_errno = EAGAIN;
+               return 0;
+       }
+
+       if (!rsp_info->sched_type)
+               roc_sso_hws_head_wait(tag_op);
+
+       lmt_arg = ROC_CN10K_CPT_LMT_ARG | (uint64_t)lmt_id;
+       roc_lmt_submit_steorl(lmt_arg, qp->lmtline.io_addr);
+
+       rte_io_wmb();
+
+       return 1;
+}
+
 static inline void
 cn10k_cpt_sec_post_process(struct rte_crypto_op *cop,
-                          struct cpt_inflight_req *infl_req)
+                          struct cpt_cn10k_res_s *res)
 {
-       struct rte_crypto_sym_op *sym_op = cop->sym;
-       struct rte_mbuf *m = sym_op->m_src;
-       struct rte_ipv6_hdr *ip6;
-       struct rte_ipv4_hdr *ip;
-       uint16_t m_len;
-
-       if (infl_req->op_flags & CPT_OP_FLAGS_IPSEC_DIR_INBOUND) {
-               ip = (struct rte_ipv4_hdr *)rte_pktmbuf_mtod(m, char *);
-
-               if (((ip->version_ihl & 0xf0) >> RTE_IPV4_IHL_MULTIPLIER) ==
-                   IPVERSION) {
-                       m_len = rte_be_to_cpu_16(ip->total_length);
-               } else {
-                       PLT_ASSERT(((ip->version_ihl & 0xf0) >>
-                                   RTE_IPV4_IHL_MULTIPLIER) == 6);
-                       ip6 = (struct rte_ipv6_hdr *)ip;
-                       m_len = rte_be_to_cpu_16(ip6->payload_len) +
-                               sizeof(struct rte_ipv6_hdr);
-               }
-               m->data_len = m_len;
-               m->pkt_len = m_len;
+       struct rte_mbuf *m = cop->sym->m_src;
+       const uint16_t m_len = res->rlen;
+
+       m->data_len = m_len;
+       m->pkt_len = m_len;
+}
+
+static inline void
+cn10k_cpt_sec_ucc_process(struct rte_crypto_op *cop,
+                         struct cpt_inflight_req *infl_req,
+                         const uint8_t uc_compcode)
+{
+       struct cn10k_sec_session *sess;
+       struct cn10k_ipsec_sa *sa;
+       struct rte_mbuf *mbuf;
+
+       if (uc_compcode == ROC_IE_OT_UCC_SUCCESS_SA_SOFTEXP_FIRST)
+               cop->aux_flags = RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY;
+
+       if (!(infl_req->op_flags & CPT_OP_FLAGS_IPSEC_DIR_INBOUND))
+               return;
+
+       sess = get_sec_session_private_data(cop->sym->sec_session);
+       sa = &sess->sa;
+
+       mbuf = cop->sym->m_src;
+
+       switch (uc_compcode) {
+       case ROC_IE_OT_UCC_SUCCESS:
+               if (sa->ip_csum_enable)
+                       mbuf->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_GOOD;
+               break;
+       case ROC_IE_OT_UCC_SUCCESS_PKT_IP_BADCSUM:
+               mbuf->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_BAD;
+               break;
+       case ROC_IE_OT_UCC_SUCCESS_PKT_L4_GOODCSUM:
+               mbuf->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_GOOD;
+               if (sa->ip_csum_enable)
+                       mbuf->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_GOOD;
+               break;
+       case ROC_IE_OT_UCC_SUCCESS_PKT_L4_BADCSUM:
+               mbuf->ol_flags |= RTE_MBUF_F_RX_L4_CKSUM_BAD;
+               if (sa->ip_csum_enable)
+                       mbuf->ol_flags |= RTE_MBUF_F_RX_IP_CKSUM_GOOD;
+               break;
+       default:
+               break;
        }
 }
 
@@ -291,12 +399,35 @@ cn10k_cpt_dequeue_post_process(struct cnxk_cpt_qp *qp,
                               struct cpt_inflight_req *infl_req)
 {
        struct cpt_cn10k_res_s *res = (struct cpt_cn10k_res_s *)&infl_req->res;
+       const uint8_t uc_compcode = res->uc_compcode;
+       const uint8_t compcode = res->compcode;
        unsigned int sz;
 
-       if (likely(res->compcode == CPT_COMP_GOOD ||
-                  res->compcode == CPT_COMP_WARN)) {
-               if (unlikely(res->uc_compcode)) {
-                       if (res->uc_compcode == ROC_SE_ERR_GC_ICV_MISCOMPARE)
+       cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
+
+       if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
+           cop->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
+               if (likely(compcode == CPT_COMP_WARN)) {
+                       /* Success with additional info */
+                       cn10k_cpt_sec_ucc_process(cop, infl_req, uc_compcode);
+                       cn10k_cpt_sec_post_process(cop, res);
+               } else {
+                       cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
+                       plt_dp_info("HW completion code 0x%x", res->compcode);
+                       if (compcode == CPT_COMP_GOOD) {
+                               plt_dp_info(
+                                       "Request failed with microcode error");
+                               plt_dp_info("MC completion code 0x%x",
+                                           uc_compcode);
+                       }
+               }
+
+               return;
+       }
+
+       if (likely(compcode == CPT_COMP_GOOD || compcode == CPT_COMP_WARN)) {
+               if (unlikely(uc_compcode)) {
+                       if (uc_compcode == ROC_SE_ERR_GC_ICV_MISCOMPARE)
                                cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
                        else
                                cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
@@ -307,13 +438,7 @@ cn10k_cpt_dequeue_post_process(struct cnxk_cpt_qp *qp,
                        goto temp_sess_free;
                }
 
-               cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
                if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
-                       if (cop->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
-                               cn10k_cpt_sec_post_process(cop, infl_req);
-                               return;
-                       }
-
                        /* Verify authentication data if required */
                        if (unlikely(infl_req->op_flags &
                                     CPT_OP_FLAGS_AUTH_VERIFY)) {
@@ -335,7 +460,7 @@ cn10k_cpt_dequeue_post_process(struct cnxk_cpt_qp *qp,
                cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
                plt_dp_info("HW completion code 0x%x", res->compcode);
 
-               switch (res->compcode) {
+               switch (compcode) {
                case CPT_COMP_INSTERR:
                        plt_dp_err("Request failed with instruction error");
                        break;
@@ -365,6 +490,26 @@ temp_sess_free:
        }
 }
 
+uintptr_t
+cn10k_cpt_crypto_adapter_dequeue(uintptr_t get_work1)
+{
+       struct cpt_inflight_req *infl_req;
+       struct rte_crypto_op *cop;
+       struct cnxk_cpt_qp *qp;
+
+       infl_req = (struct cpt_inflight_req *)(get_work1);
+       cop = infl_req->cop;
+       qp = infl_req->qp;
+
+       cn10k_cpt_dequeue_post_process(qp, infl_req->cop, infl_req);
+
+       if (unlikely(infl_req->op_flags & CPT_OP_FLAGS_METABUF))
+               rte_mempool_put(qp->meta_info.pool, infl_req->mdata);
+
+       rte_mempool_put(qp->ca.req_mp, infl_req);
+       return (uintptr_t)cop;
+}
+
 static uint16_t
 cn10k_cpt_dequeue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
 {
@@ -372,18 +517,23 @@ cn10k_cpt_dequeue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
        struct cnxk_cpt_qp *qp = qptr;
        struct pending_queue *pend_q;
        struct cpt_cn10k_res_s *res;
+       uint64_t infl_cnt, pq_tail;
        struct rte_crypto_op *cop;
-       int i, nb_pending;
+       int i;
 
        pend_q = &qp->pend_q;
 
-       nb_pending = pend_q->pending_count;
+       const uint64_t pq_mask = pend_q->pq_mask;
+
+       pq_tail = pend_q->tail;
+       infl_cnt = pending_queue_infl_cnt(pend_q->head, pq_tail, pq_mask);
+       nb_ops = RTE_MIN(nb_ops, infl_cnt);
 
-       if (nb_ops > nb_pending)
-               nb_ops = nb_pending;
+       /* Ensure infl_cnt isn't read before data lands */
+       rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
 
        for (i = 0; i < nb_ops; i++) {
-               infl_req = &pend_q->req_queue[pend_q->deq_head];
+               infl_req = &pend_q->req_queue[pq_tail];
 
                res = (struct cpt_cn10k_res_s *)&infl_req->res;
 
@@ -398,7 +548,7 @@ cn10k_cpt_dequeue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
                        break;
                }
 
-               MOD_INC(pend_q->deq_head, qp->lf.nb_desc);
+               pending_queue_advance(&pq_tail, pq_mask);
 
                cop = infl_req->cop;
 
@@ -410,7 +560,7 @@ cn10k_cpt_dequeue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
                        rte_mempool_put(qp->meta_info.pool, infl_req->mdata);
        }
 
-       pend_q->pending_count -= i;
+       pend_q->tail = pq_tail;
 
        return i;
 }