0336ae102ebef47eb41d30cbdfecbddf61260481
[dpdk.git] / drivers / crypto / cnxk / cnxk_cryptodev_ops.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #ifndef _CNXK_CRYPTODEV_OPS_H_
6 #define _CNXK_CRYPTODEV_OPS_H_
7
8 #include <rte_cryptodev.h>
9 #include <rte_event_crypto_adapter.h>
10
11 #include "roc_api.h"
12
13 #define CNXK_CPT_MIN_HEADROOM_REQ 24
14 #define CNXK_CPT_MIN_TAILROOM_REQ 102
15
16 /* Default command timeout in seconds */
17 #define DEFAULT_COMMAND_TIMEOUT 4
18
19 #define MOD_INC(i, l) ((i) == (l - 1) ? (i) = 0 : (i)++)
20
21 /* Macros to form words in CPT instruction */
22 #define CNXK_CPT_INST_W2(tag, tt, grp, rvu_pf_func)                            \
23         ((tag) | ((uint64_t)(tt) << 32) | ((uint64_t)(grp) << 34) |            \
24          ((uint64_t)(rvu_pf_func) << 48))
25 #define CNXK_CPT_INST_W3(qord, wqe_ptr)                                        \
26         (qord | ((uintptr_t)(wqe_ptr) >> 3) << 3)
27
28 struct cpt_qp_meta_info {
29         struct rte_mempool *pool;
30         int mlen;
31 };
32
33 #define CPT_OP_FLAGS_METABUF           (1 << 1)
34 #define CPT_OP_FLAGS_AUTH_VERIFY       (1 << 0)
35 #define CPT_OP_FLAGS_IPSEC_DIR_INBOUND (1 << 2)
36
37 struct cpt_inflight_req {
38         union cpt_res_s res;
39         struct rte_crypto_op *cop;
40         void *mdata;
41         uint8_t op_flags;
42         void *qp;
43 } __rte_aligned(16);
44
45 struct pending_queue {
46         /** Array of pending requests */
47         struct cpt_inflight_req *req_queue;
48         /** Head of the queue to be used for enqueue */
49         uint64_t head;
50         /** Tail of the queue to be used for dequeue */
51         uint64_t tail;
52         /** Pending queue mask */
53         uint64_t pq_mask;
54         /** Timeout to track h/w being unresponsive */
55         uint64_t time_out;
56 };
57
58 struct crypto_adpter_info {
59         bool enabled;
60         /**< Set if queue pair is added to crypto adapter */
61         struct rte_mempool *req_mp;
62         /**< CPT inflight request mempool */
63 };
64
65 struct cnxk_cpt_qp {
66         struct roc_cpt_lf lf;
67         /**< Crypto LF */
68         struct pending_queue pend_q;
69         /**< Pending queue */
70         struct rte_mempool *sess_mp;
71         /**< Session mempool */
72         struct rte_mempool *sess_mp_priv;
73         /**< Session private data mempool */
74         struct cpt_qp_meta_info meta_info;
75         /**< Metabuf info required to support operations on the queue pair */
76         struct roc_cpt_lmtline lmtline;
77         /**< Lmtline information */
78         struct crypto_adpter_info ca;
79         /**< Crypto adapter related info */
80 };
81
82 int cnxk_cpt_dev_config(struct rte_cryptodev *dev,
83                         struct rte_cryptodev_config *conf);
84
85 int cnxk_cpt_dev_start(struct rte_cryptodev *dev);
86
87 void cnxk_cpt_dev_stop(struct rte_cryptodev *dev);
88
89 int cnxk_cpt_dev_close(struct rte_cryptodev *dev);
90
91 void cnxk_cpt_dev_info_get(struct rte_cryptodev *dev,
92                            struct rte_cryptodev_info *info);
93
94 int cnxk_cpt_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
95                               const struct rte_cryptodev_qp_conf *conf,
96                               int socket_id __rte_unused);
97
98 int cnxk_cpt_queue_pair_release(struct rte_cryptodev *dev, uint16_t qp_id);
99
100 unsigned int cnxk_cpt_sym_session_get_size(struct rte_cryptodev *dev);
101
102 int cnxk_cpt_sym_session_configure(struct rte_cryptodev *dev,
103                                    struct rte_crypto_sym_xform *xform,
104                                    struct rte_cryptodev_sym_session *sess,
105                                    struct rte_mempool *pool);
106
107 int sym_session_configure(struct roc_cpt *roc_cpt, int driver_id,
108                           struct rte_crypto_sym_xform *xform,
109                           struct rte_cryptodev_sym_session *sess,
110                           struct rte_mempool *pool);
111
112 void cnxk_cpt_sym_session_clear(struct rte_cryptodev *dev,
113                                 struct rte_cryptodev_sym_session *sess);
114
115 void sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess);
116
117 unsigned int cnxk_ae_session_size_get(struct rte_cryptodev *dev __rte_unused);
118
119 void cnxk_ae_session_clear(struct rte_cryptodev *dev,
120                            struct rte_cryptodev_asym_session *sess);
121 int cnxk_ae_session_cfg(struct rte_cryptodev *dev,
122                         struct rte_crypto_asym_xform *xform,
123                         struct rte_cryptodev_asym_session *sess,
124                         struct rte_mempool *pool);
125
126 static inline union rte_event_crypto_metadata *
127 cnxk_event_crypto_mdata_get(struct rte_crypto_op *op)
128 {
129         union rte_event_crypto_metadata *ec_mdata;
130
131         if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
132                 ec_mdata = rte_cryptodev_sym_session_get_user_data(
133                         op->sym->session);
134         else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
135                  op->private_data_offset)
136                 ec_mdata = (union rte_event_crypto_metadata
137                                     *)((uint8_t *)op + op->private_data_offset);
138         else
139                 return NULL;
140
141         return ec_mdata;
142 }
143
144 static __rte_always_inline void
145 pending_queue_advance(uint64_t *index, const uint64_t mask)
146 {
147         *index = (*index + 1) & mask;
148 }
149
150 static __rte_always_inline void
151 pending_queue_retreat(uint64_t *index, const uint64_t mask, uint64_t nb_entry)
152 {
153         *index = (*index - nb_entry) & mask;
154 }
155
156 static __rte_always_inline uint64_t
157 pending_queue_infl_cnt(uint64_t head, uint64_t tail, const uint64_t mask)
158 {
159         /*
160          * Mask is nb_desc - 1. Add nb_desc to head and mask to account for
161          * cases when tail > head, which happens during wrap around.
162          */
163         return ((head + mask + 1) - tail) & mask;
164 }
165
166 static __rte_always_inline uint64_t
167 pending_queue_free_cnt(uint64_t head, uint64_t tail, const uint64_t mask)
168 {
169         /* mask is nb_desc - 1 */
170         return mask - pending_queue_infl_cnt(head, tail, mask);
171 }
172
173 #endif /* _CNXK_CRYPTODEV_OPS_H_ */