net/cnxk: add TM shaper and node operations
[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 <cryptodev_pmd.h>
7 #include <rte_event_crypto_adapter.h>
8 #include <rte_ip.h>
9
10 #include "cn10k_cryptodev.h"
11 #include "cn10k_cryptodev_ops.h"
12 #include "cn10k_ipsec_la_ops.h"
13 #include "cn10k_ipsec.h"
14 #include "cnxk_ae.h"
15 #include "cnxk_cryptodev.h"
16 #include "cnxk_cryptodev_ops.h"
17 #include "cnxk_se.h"
18
19 #include "roc_api.h"
20
21 static inline struct cnxk_se_sess *
22 cn10k_cpt_sym_temp_sess_create(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op)
23 {
24         const int driver_id = cn10k_cryptodev_driver_id;
25         struct rte_crypto_sym_op *sym_op = op->sym;
26         struct rte_cryptodev_sym_session *sess;
27         struct cnxk_se_sess *priv;
28         int ret;
29
30         /* Create temporary session */
31         sess = rte_cryptodev_sym_session_create(qp->sess_mp);
32         if (sess == NULL)
33                 return NULL;
34
35         ret = sym_session_configure(qp->lf.roc_cpt, driver_id, sym_op->xform,
36                                     sess, qp->sess_mp_priv);
37         if (ret)
38                 goto sess_put;
39
40         priv = get_sym_session_private_data(sess, driver_id);
41
42         sym_op->session = sess;
43
44         return priv;
45
46 sess_put:
47         rte_mempool_put(qp->sess_mp, sess);
48         return NULL;
49 }
50
51 static __rte_always_inline int __rte_hot
52 cpt_sec_inst_fill(struct rte_crypto_op *op, struct cn10k_sec_session *sess,
53                   struct cpt_inst_s *inst)
54 {
55         struct rte_crypto_sym_op *sym_op = op->sym;
56         union roc_ot_ipsec_sa_word2 *w2;
57         struct cn10k_ipsec_sa *sa;
58         int ret;
59
60         if (unlikely(sym_op->m_dst && sym_op->m_dst != sym_op->m_src)) {
61                 plt_dp_err("Out of place is not supported");
62                 return -ENOTSUP;
63         }
64
65         if (unlikely(!rte_pktmbuf_is_contiguous(sym_op->m_src))) {
66                 plt_dp_err("Scatter Gather mode is not supported");
67                 return -ENOTSUP;
68         }
69
70         sa = &sess->sa;
71         w2 = (union roc_ot_ipsec_sa_word2 *)&sa->in_sa.w2;
72
73         if (w2->s.dir == ROC_IE_SA_DIR_OUTBOUND)
74                 ret = process_outb_sa(op, sa, inst);
75         else
76                 ret = process_inb_sa(op, sa, inst);
77
78         return ret;
79 }
80
81 static __rte_always_inline int __rte_hot
82 cpt_sym_inst_fill(struct cnxk_cpt_qp *qp, struct rte_crypto_op *op,
83                   struct cnxk_se_sess *sess, struct cpt_inflight_req *infl_req,
84                   struct cpt_inst_s *inst)
85 {
86         uint64_t cpt_op;
87         int ret;
88
89         cpt_op = sess->cpt_op;
90
91         if (cpt_op & ROC_SE_OP_CIPHER_MASK)
92                 ret = fill_fc_params(op, sess, &qp->meta_info, infl_req, inst);
93         else
94                 ret = fill_digest_params(op, sess, &qp->meta_info, infl_req,
95                                          inst);
96
97         return ret;
98 }
99
100 static inline int
101 cn10k_cpt_fill_inst(struct cnxk_cpt_qp *qp, struct rte_crypto_op *ops[],
102                     struct cpt_inst_s inst[], struct cpt_inflight_req *infl_req)
103 {
104         struct cn10k_sec_session *sec_sess;
105         struct rte_crypto_asym_op *asym_op;
106         struct rte_crypto_sym_op *sym_op;
107         struct cnxk_ae_sess *ae_sess;
108         struct cnxk_se_sess *sess;
109         struct rte_crypto_op *op;
110         uint64_t w7;
111         int ret;
112
113         op = ops[0];
114
115         inst[0].w0.u64 = 0;
116         inst[0].w2.u64 = 0;
117         inst[0].w3.u64 = 0;
118
119         sym_op = op->sym;
120
121         if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
122                 if (op->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
123                         sec_sess = get_sec_session_private_data(
124                                 sym_op->sec_session);
125                         ret = cpt_sec_inst_fill(op, sec_sess, &inst[0]);
126                         if (unlikely(ret))
127                                 return 0;
128                         w7 = sec_sess->sa.inst.w7;
129                 } else if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
130                         sess = get_sym_session_private_data(
131                                 sym_op->session, cn10k_cryptodev_driver_id);
132                         ret = cpt_sym_inst_fill(qp, op, sess, infl_req,
133                                                 &inst[0]);
134                         if (unlikely(ret))
135                                 return 0;
136                         w7 = sess->cpt_inst_w7;
137                 } else {
138                         sess = cn10k_cpt_sym_temp_sess_create(qp, op);
139                         if (unlikely(sess == NULL)) {
140                                 plt_dp_err("Could not create temp session");
141                                 return 0;
142                         }
143
144                         ret = cpt_sym_inst_fill(qp, op, sess, infl_req,
145                                                 &inst[0]);
146                         if (unlikely(ret)) {
147                                 sym_session_clear(cn10k_cryptodev_driver_id,
148                                                   op->sym->session);
149                                 rte_mempool_put(qp->sess_mp, op->sym->session);
150                                 return 0;
151                         }
152                         w7 = sess->cpt_inst_w7;
153                 }
154         } else if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
155
156                 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
157                         asym_op = op->asym;
158                         ae_sess = get_asym_session_private_data(
159                                 asym_op->session, cn10k_cryptodev_driver_id);
160                         ret = cnxk_ae_enqueue(qp, op, infl_req, &inst[0],
161                                               ae_sess);
162                         if (unlikely(ret))
163                                 return 0;
164                         w7 = ae_sess->cpt_inst_w7;
165                 } else {
166                         plt_dp_err("Not supported Asym op without session");
167                         return 0;
168                 }
169         } else {
170                 plt_dp_err("Unsupported op type");
171                 return 0;
172         }
173
174         inst[0].res_addr = (uint64_t)&infl_req->res;
175         infl_req->res.cn10k.compcode = CPT_COMP_NOT_DONE;
176         infl_req->cop = op;
177
178         inst[0].w7.u64 = w7;
179
180         return 1;
181 }
182
183 #define PKTS_PER_LOOP   32
184 #define PKTS_PER_STEORL 16
185
186 static uint16_t
187 cn10k_cpt_enqueue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
188 {
189         uint64_t lmt_base, lmt_arg, io_addr;
190         struct cpt_inflight_req *infl_req;
191         uint16_t nb_allowed, count = 0;
192         struct cnxk_cpt_qp *qp = qptr;
193         struct pending_queue *pend_q;
194         struct cpt_inst_s *inst;
195         uint16_t lmt_id;
196         int ret, i;
197
198         pend_q = &qp->pend_q;
199
200         nb_allowed = qp->lf.nb_desc - pend_q->pending_count;
201         nb_ops = RTE_MIN(nb_ops, nb_allowed);
202
203         if (unlikely(nb_ops == 0))
204                 return 0;
205
206         lmt_base = qp->lmtline.lmt_base;
207         io_addr = qp->lmtline.io_addr;
208
209         ROC_LMT_BASE_ID_GET(lmt_base, lmt_id);
210         inst = (struct cpt_inst_s *)lmt_base;
211
212 again:
213         for (i = 0; i < RTE_MIN(PKTS_PER_LOOP, nb_ops); i++) {
214                 infl_req = &pend_q->req_queue[pend_q->enq_tail];
215                 infl_req->op_flags = 0;
216
217                 ret = cn10k_cpt_fill_inst(qp, ops + i, &inst[2 * i], infl_req);
218                 if (unlikely(ret != 1)) {
219                         plt_dp_err("Could not process op: %p", ops + i);
220                         if (i == 0)
221                                 goto update_pending;
222                         break;
223                 }
224
225                 MOD_INC(pend_q->enq_tail, qp->lf.nb_desc);
226         }
227
228         if (i > PKTS_PER_STEORL) {
229                 lmt_arg = ROC_CN10K_CPT_LMT_ARG | (PKTS_PER_STEORL - 1) << 12 |
230                           (uint64_t)lmt_id;
231                 roc_lmt_submit_steorl(lmt_arg, io_addr);
232                 lmt_arg = ROC_CN10K_CPT_LMT_ARG |
233                           (i - PKTS_PER_STEORL - 1) << 12 |
234                           (uint64_t)(lmt_id + PKTS_PER_STEORL);
235                 roc_lmt_submit_steorl(lmt_arg, io_addr);
236         } else {
237                 lmt_arg = ROC_CN10K_CPT_LMT_ARG | (i - 1) << 12 |
238                           (uint64_t)lmt_id;
239                 roc_lmt_submit_steorl(lmt_arg, io_addr);
240         }
241
242         rte_io_wmb();
243
244         if (nb_ops - i > 0 && i == PKTS_PER_LOOP) {
245                 nb_ops -= i;
246                 ops += i;
247                 count += i;
248                 goto again;
249         }
250
251 update_pending:
252         pend_q->pending_count += count + i;
253
254         pend_q->time_out = rte_get_timer_cycles() +
255                            DEFAULT_COMMAND_TIMEOUT * rte_get_timer_hz();
256
257         return count + i;
258 }
259
260 uint16_t
261 cn10k_cpt_crypto_adapter_enqueue(uintptr_t tag_op, struct rte_crypto_op *op)
262 {
263         union rte_event_crypto_metadata *ec_mdata;
264         struct cpt_inflight_req *infl_req;
265         struct rte_event *rsp_info;
266         uint64_t lmt_base, lmt_arg;
267         struct cpt_inst_s *inst;
268         struct cnxk_cpt_qp *qp;
269         uint8_t cdev_id;
270         uint16_t lmt_id;
271         uint16_t qp_id;
272         int ret;
273
274         ec_mdata = cnxk_event_crypto_mdata_get(op);
275         if (!ec_mdata) {
276                 rte_errno = EINVAL;
277                 return 0;
278         }
279
280         cdev_id = ec_mdata->request_info.cdev_id;
281         qp_id = ec_mdata->request_info.queue_pair_id;
282         qp = rte_cryptodevs[cdev_id].data->queue_pairs[qp_id];
283         rsp_info = &ec_mdata->response_info;
284
285         if (unlikely(!qp->ca.enabled)) {
286                 rte_errno = EINVAL;
287                 return 0;
288         }
289
290         if (unlikely(rte_mempool_get(qp->ca.req_mp, (void **)&infl_req))) {
291                 rte_errno = ENOMEM;
292                 return 0;
293         }
294         infl_req->op_flags = 0;
295
296         lmt_base = qp->lmtline.lmt_base;
297         ROC_LMT_BASE_ID_GET(lmt_base, lmt_id);
298         inst = (struct cpt_inst_s *)lmt_base;
299
300         ret = cn10k_cpt_fill_inst(qp, &op, inst, infl_req);
301         if (unlikely(ret != 1)) {
302                 plt_dp_err("Could not process op: %p", op);
303                 rte_mempool_put(qp->ca.req_mp, infl_req);
304                 return 0;
305         }
306
307         infl_req->cop = op;
308         infl_req->res.cn10k.compcode = CPT_COMP_NOT_DONE;
309         infl_req->qp = qp;
310         inst->w0.u64 = 0;
311         inst->res_addr = (uint64_t)&infl_req->res;
312         inst->w2.u64 = CNXK_CPT_INST_W2(
313                 (RTE_EVENT_TYPE_CRYPTODEV << 28) | rsp_info->flow_id,
314                 rsp_info->sched_type, rsp_info->queue_id, 0);
315         inst->w3.u64 = CNXK_CPT_INST_W3(1, infl_req);
316
317         if (roc_cpt_is_iq_full(&qp->lf)) {
318                 rte_mempool_put(qp->ca.req_mp, infl_req);
319                 rte_errno = EAGAIN;
320                 return 0;
321         }
322
323         if (!rsp_info->sched_type)
324                 roc_sso_hws_head_wait(tag_op);
325
326         lmt_arg = ROC_CN10K_CPT_LMT_ARG | (uint64_t)lmt_id;
327         roc_lmt_submit_steorl(lmt_arg, qp->lmtline.io_addr);
328
329         rte_io_wmb();
330
331         return 1;
332 }
333
334 static inline void
335 cn10k_cpt_sec_post_process(struct rte_crypto_op *cop,
336                            struct cpt_cn10k_res_s *res)
337 {
338         struct rte_mbuf *m = cop->sym->m_src;
339         const uint16_t m_len = res->rlen;
340
341         m->data_len = m_len;
342         m->pkt_len = m_len;
343 }
344
345 static inline void
346 cn10k_cpt_dequeue_post_process(struct cnxk_cpt_qp *qp,
347                                struct rte_crypto_op *cop,
348                                struct cpt_inflight_req *infl_req)
349 {
350         struct cpt_cn10k_res_s *res = (struct cpt_cn10k_res_s *)&infl_req->res;
351         const uint8_t uc_compcode = res->uc_compcode;
352         const uint8_t compcode = res->compcode;
353         unsigned int sz;
354
355         cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
356
357         if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
358             cop->sess_type == RTE_CRYPTO_OP_SECURITY_SESSION) {
359                 if (likely(compcode == CPT_COMP_WARN)) {
360                         if (unlikely(uc_compcode != ROC_IE_OT_UCC_SUCCESS)) {
361                                 /* Success with additional info */
362                                 switch (uc_compcode) {
363                                 case ROC_IE_OT_UCC_SUCCESS_SA_SOFTEXP_FIRST:
364                                         cop->aux_flags =
365                                                 RTE_CRYPTO_OP_AUX_FLAGS_IPSEC_SOFT_EXPIRY;
366                                         break;
367                                 default:
368                                         break;
369                                 }
370                         }
371                         cn10k_cpt_sec_post_process(cop, res);
372                 } else {
373                         cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
374                         plt_dp_info("HW completion code 0x%x", res->compcode);
375                         if (compcode == CPT_COMP_GOOD) {
376                                 plt_dp_info(
377                                         "Request failed with microcode error");
378                                 plt_dp_info("MC completion code 0x%x",
379                                             uc_compcode);
380                         }
381                 }
382
383                 return;
384         }
385
386         if (likely(compcode == CPT_COMP_GOOD || compcode == CPT_COMP_WARN)) {
387                 if (unlikely(uc_compcode)) {
388                         if (uc_compcode == ROC_SE_ERR_GC_ICV_MISCOMPARE)
389                                 cop->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
390                         else
391                                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
392
393                         plt_dp_info("Request failed with microcode error");
394                         plt_dp_info("MC completion code 0x%x",
395                                     res->uc_compcode);
396                         goto temp_sess_free;
397                 }
398
399                 if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
400                         /* Verify authentication data if required */
401                         if (unlikely(infl_req->op_flags &
402                                      CPT_OP_FLAGS_AUTH_VERIFY)) {
403                                 uintptr_t *rsp = infl_req->mdata;
404                                 compl_auth_verify(cop, (uint8_t *)rsp[0],
405                                                   rsp[1]);
406                         }
407                 } else if (cop->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC) {
408                         struct rte_crypto_asym_op *op = cop->asym;
409                         uintptr_t *mdata = infl_req->mdata;
410                         struct cnxk_ae_sess *sess;
411
412                         sess = get_asym_session_private_data(
413                                 op->session, cn10k_cryptodev_driver_id);
414
415                         cnxk_ae_post_process(cop, sess, (uint8_t *)mdata[0]);
416                 }
417         } else {
418                 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
419                 plt_dp_info("HW completion code 0x%x", res->compcode);
420
421                 switch (compcode) {
422                 case CPT_COMP_INSTERR:
423                         plt_dp_err("Request failed with instruction error");
424                         break;
425                 case CPT_COMP_FAULT:
426                         plt_dp_err("Request failed with DMA fault");
427                         break;
428                 case CPT_COMP_HWERR:
429                         plt_dp_err("Request failed with hardware error");
430                         break;
431                 default:
432                         plt_dp_err(
433                                 "Request failed with unknown completion code");
434                 }
435         }
436
437 temp_sess_free:
438         if (unlikely(cop->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
439                 if (cop->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
440                         sym_session_clear(cn10k_cryptodev_driver_id,
441                                           cop->sym->session);
442                         sz = rte_cryptodev_sym_get_existing_header_session_size(
443                                 cop->sym->session);
444                         memset(cop->sym->session, 0, sz);
445                         rte_mempool_put(qp->sess_mp, cop->sym->session);
446                         cop->sym->session = NULL;
447                 }
448         }
449 }
450
451 uintptr_t
452 cn10k_cpt_crypto_adapter_dequeue(uintptr_t get_work1)
453 {
454         struct cpt_inflight_req *infl_req;
455         struct rte_crypto_op *cop;
456         struct cnxk_cpt_qp *qp;
457
458         infl_req = (struct cpt_inflight_req *)(get_work1);
459         cop = infl_req->cop;
460         qp = infl_req->qp;
461
462         cn10k_cpt_dequeue_post_process(qp, infl_req->cop, infl_req);
463
464         if (unlikely(infl_req->op_flags & CPT_OP_FLAGS_METABUF))
465                 rte_mempool_put(qp->meta_info.pool, infl_req->mdata);
466
467         rte_mempool_put(qp->ca.req_mp, infl_req);
468         return (uintptr_t)cop;
469 }
470
471 static uint16_t
472 cn10k_cpt_dequeue_burst(void *qptr, struct rte_crypto_op **ops, uint16_t nb_ops)
473 {
474         struct cpt_inflight_req *infl_req;
475         struct cnxk_cpt_qp *qp = qptr;
476         struct pending_queue *pend_q;
477         struct cpt_cn10k_res_s *res;
478         struct rte_crypto_op *cop;
479         int i, nb_pending;
480
481         pend_q = &qp->pend_q;
482
483         nb_pending = pend_q->pending_count;
484
485         if (nb_ops > nb_pending)
486                 nb_ops = nb_pending;
487
488         for (i = 0; i < nb_ops; i++) {
489                 infl_req = &pend_q->req_queue[pend_q->deq_head];
490
491                 res = (struct cpt_cn10k_res_s *)&infl_req->res;
492
493                 if (unlikely(res->compcode == CPT_COMP_NOT_DONE)) {
494                         if (unlikely(rte_get_timer_cycles() >
495                                      pend_q->time_out)) {
496                                 plt_err("Request timed out");
497                                 pend_q->time_out = rte_get_timer_cycles() +
498                                                    DEFAULT_COMMAND_TIMEOUT *
499                                                            rte_get_timer_hz();
500                         }
501                         break;
502                 }
503
504                 MOD_INC(pend_q->deq_head, qp->lf.nb_desc);
505
506                 cop = infl_req->cop;
507
508                 ops[i] = cop;
509
510                 cn10k_cpt_dequeue_post_process(qp, cop, infl_req);
511
512                 if (unlikely(infl_req->op_flags & CPT_OP_FLAGS_METABUF))
513                         rte_mempool_put(qp->meta_info.pool, infl_req->mdata);
514         }
515
516         pend_q->pending_count -= i;
517
518         return i;
519 }
520
521 void
522 cn10k_cpt_set_enqdeq_fns(struct rte_cryptodev *dev)
523 {
524         dev->enqueue_burst = cn10k_cpt_enqueue_burst;
525         dev->dequeue_burst = cn10k_cpt_dequeue_burst;
526
527         rte_mb();
528 }
529
530 static void
531 cn10k_cpt_dev_info_get(struct rte_cryptodev *dev,
532                        struct rte_cryptodev_info *info)
533 {
534         if (info != NULL) {
535                 cnxk_cpt_dev_info_get(dev, info);
536                 info->driver_id = cn10k_cryptodev_driver_id;
537         }
538 }
539
540 struct rte_cryptodev_ops cn10k_cpt_ops = {
541         /* Device control ops */
542         .dev_configure = cnxk_cpt_dev_config,
543         .dev_start = cnxk_cpt_dev_start,
544         .dev_stop = cnxk_cpt_dev_stop,
545         .dev_close = cnxk_cpt_dev_close,
546         .dev_infos_get = cn10k_cpt_dev_info_get,
547
548         .stats_get = NULL,
549         .stats_reset = NULL,
550         .queue_pair_setup = cnxk_cpt_queue_pair_setup,
551         .queue_pair_release = cnxk_cpt_queue_pair_release,
552
553         /* Symmetric crypto ops */
554         .sym_session_get_size = cnxk_cpt_sym_session_get_size,
555         .sym_session_configure = cnxk_cpt_sym_session_configure,
556         .sym_session_clear = cnxk_cpt_sym_session_clear,
557
558         /* Asymmetric crypto ops */
559         .asym_session_get_size = cnxk_ae_session_size_get,
560         .asym_session_configure = cnxk_ae_session_cfg,
561         .asym_session_clear = cnxk_ae_session_clear,
562
563 };