5dd2cd2568cb0282b791dfeeb04b2cc53b7c0ec7
[dpdk.git] / drivers / crypto / cnxk / cn10k_cryptodev_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <rte_cryptodev.h>
6 #include <rte_cryptodev_pmd.h>
7
8 #include "cn10k_cryptodev.h"
9 #include "cn10k_cryptodev_ops.h"
10 #include "cnxk_cryptodev.h"
11 #include "cnxk_cryptodev_ops.h"
12 #include "cnxk_se.h"
13
14 static inline struct cnxk_se_sess *
15 cn10k_cpt_sym_temp_sess_create(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op)
16 {
17         const int driver_id = cn10k_cryptodev_driver_id;
18         struct rte_crypto_sym_op *sym_op = op->sym;
19         struct rte_cryptodev_sym_session *sess;
20         struct cnxk_se_sess *priv;
21         int ret;
22
23         /* Create temporary session */
24         sess = rte_cryptodev_sym_session_create(qp->sess_mp);
25         if (sess == NULL)
26                 return NULL;
27
28         ret = sym_session_configure(qp->lf.roc_cpt, driver_id, sym_op->xform,
29                                     sess, qp->sess_mp_priv);
30         if (ret)
31                 goto sess_put;
32
33         priv = get_sym_session_private_data(sess, driver_id);
34
35         sym_op->session = sess;
36
37         return priv;
38
39 sess_put:
40         rte_mempool_put(qp->sess_mp, sess);
41         return NULL;
42 }
43
44 static __rte_always_inline int __rte_hot
45 cpt_sym_inst_fill(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op,
46                   struct cnxk_se_sess *sess, struct cpt_inflight_req *infl_req,
47                   struct cpt_inst_s *inst)
48 {
49         RTE_SET_USED(qp);
50         RTE_SET_USED(op);
51         RTE_SET_USED(sess);
52         RTE_SET_USED(infl_req);
53         RTE_SET_USED(inst);
54
55         return -ENOTSUP;
56 }
57
58 static inline int
59 cn10k_cpt_fill_inst(struct cnxk_cpt_qp *qp, struct rte_crypto_op *ops[],
60                     struct cpt_inst_s inst[], struct cpt_inflight_req *infl_req)
61 {
62         struct rte_crypto_sym_op *sym_op;
63         struct cnxk_se_sess *sess;
64         struct rte_crypto_op *op;
65         uint64_t w7;
66         int ret;
67
68         op = ops[0];
69
70         inst[0].w0.u64 = 0;
71         inst[0].w2.u64 = 0;
72         inst[0].w3.u64 = 0;
73
74         sym_op = op->sym;
75
76         if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
77                 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
78                         sess = get_sym_session_private_data(
79                                 sym_op->session, cn10k_cryptodev_driver_id);
80                         ret = cpt_sym_inst_fill(qp, op, sess, infl_req,
81                                                 &inst[0]);
82                         if (unlikely(ret))
83                                 return 0;
84                         w7 = sess->cpt_inst_w7;
85                 } else {
86                         sess = cn10k_cpt_sym_temp_sess_create(qp, op);
87                         if (unlikely(sess == NULL)) {
88                                 plt_dp_err("Could not create temp session");
89                                 return 0;
90                         }
91
92                         ret = cpt_sym_inst_fill(qp, op, sess, infl_req,
93                                                 &inst[0]);
94                         if (unlikely(ret)) {
95                                 sym_session_clear(cn10k_cryptodev_driver_id,
96                                                   op->sym->session);
97                                 rte_mempool_put(qp->sess_mp, op->sym->session);
98                                 return 0;
99                         }
100                         w7 = sess->cpt_inst_w7;
101                 }
102         } else {
103                 plt_dp_err("Unsupported op type");
104                 return 0;
105         }
106
107         inst[0].res_addr = (uint64_t)&infl_req->res;
108         infl_req->res.cn10k.compcode = CPT_COMP_NOT_DONE;
109         infl_req->cop = op;
110
111         inst[0].w7.u64 = w7;
112
113         return 1;
114 }
115
116 #define PKTS_PER_LOOP   32
117 #define PKTS_PER_STEORL 16
118
119 static uint16_t
120 cn10k_cpt_enqueue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
121 {
122         uint64_t lmt_base, lmt_arg, io_addr;
123         struct cpt_inflight_req *infl_req;
124         uint16_t nb_allowed, count = 0;
125         struct cnxk_cpt_qp *qp = qptr;
126         struct pending_queue *pend_q;
127         struct cpt_inst_s *inst;
128         uint16_t lmt_id;
129         int ret, i;
130
131         pend_q = &qp->pend_q;
132
133         nb_allowed = qp->lf.nb_desc - pend_q->pending_count;
134         nb_ops = RTE_MIN(nb_ops, nb_allowed);
135
136         if (unlikely(nb_ops == 0))
137                 return 0;
138
139         lmt_base = qp->lmtline.lmt_base;
140         io_addr = qp->lmtline.io_addr;
141
142         ROC_LMT_BASE_ID_GET(lmt_base, lmt_id);
143         inst = (struct cpt_inst_s *)lmt_base;
144
145 again:
146         for (i = 0; i < RTE_MIN(PKTS_PER_LOOP, nb_ops); i++) {
147                 infl_req = &pend_q->req_queue[pend_q->enq_tail];
148                 infl_req->op_flags = 0;
149
150                 ret = cn10k_cpt_fill_inst(qp, ops + i, &inst[2 * i], infl_req);
151                 if (unlikely(ret != 1)) {
152                         plt_dp_err("Could not process op: %p", ops + i);
153                         if (i == 0)
154                                 goto update_pending;
155                         break;
156                 }
157
158                 MOD_INC(pend_q->enq_tail, qp->lf.nb_desc);
159         }
160
161         if (i > PKTS_PER_STEORL) {
162                 lmt_arg = ROC_CN10K_CPT_LMT_ARG | (PKTS_PER_STEORL - 1) << 12 |
163                           (uint64_t)lmt_id;
164                 roc_lmt_submit_steorl(lmt_arg, io_addr);
165                 lmt_arg = ROC_CN10K_CPT_LMT_ARG |
166                           (i - PKTS_PER_STEORL - 1) << 12 |
167                           (uint64_t)(lmt_id + PKTS_PER_STEORL);
168                 roc_lmt_submit_steorl(lmt_arg, io_addr);
169         } else {
170                 lmt_arg = ROC_CN10K_CPT_LMT_ARG | (i - 1) << 12 |
171                           (uint64_t)lmt_id;
172                 roc_lmt_submit_steorl(lmt_arg, io_addr);
173         }
174
175         rte_io_wmb();
176
177         if (nb_ops - i > 0 && i == PKTS_PER_LOOP) {
178                 nb_ops -= i;
179                 ops += i;
180                 count += i;
181                 goto again;
182         }
183
184 update_pending:
185         pend_q->pending_count += count + i;
186
187         pend_q->time_out = rte_get_timer_cycles() +
188                            DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
189
190         return count + i;
191 }
192
193 void
194 cn10k_cpt_set_enqdeq_fns(struct rte_cryptodev *dev)
195 {
196         dev->enqueue_burst = cn10k_cpt_enqueue_burst;
197
198         rte_mb();
199 }
200
201 static void
202 cn10k_cpt_dev_info_get(struct rte_cryptodev *dev,
203                        struct rte_cryptodev_info *info)
204 {
205         if (info != NULL) {
206                 cnxk_cpt_dev_info_get(dev, info);
207                 info->driver_id = cn10k_cryptodev_driver_id;
208         }
209 }
210
211 struct rte_cryptodev_ops cn10k_cpt_ops = {
212         /* Device control ops */
213         .dev_configure = cnxk_cpt_dev_config,
214         .dev_start = cnxk_cpt_dev_start,
215         .dev_stop = cnxk_cpt_dev_stop,
216         .dev_close = cnxk_cpt_dev_close,
217         .dev_infos_get = cn10k_cpt_dev_info_get,
218
219         .stats_get = NULL,
220         .stats_reset = NULL,
221         .queue_pair_setup = cnxk_cpt_queue_pair_setup,
222         .queue_pair_release = cnxk_cpt_queue_pair_release,
223
224         /* Symmetric crypto ops */
225         .sym_session_get_size = cnxk_cpt_sym_session_get_size,
226         .sym_session_configure = cnxk_cpt_sym_session_configure,
227         .sym_session_clear = cnxk_cpt_sym_session_clear,
228
229         /* Asymmetric crypto ops */
230         .asym_session_get_size = NULL,
231         .asym_session_configure = NULL,
232         .asym_session_clear = NULL,
233
234 };