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(ROC_ALIGN);
44 
45 PLT_STATIC_ASSERT(sizeof(struct cpt_inflight_req) == ROC_CACHE_LINE_SZ);
46 
47 struct pending_queue {
48 	/** Array of pending requests */
49 	struct cpt_inflight_req *req_queue;
50 	/** Head of the queue to be used for enqueue */
51 	uint64_t head;
52 	/** Tail of the queue to be used for dequeue */
53 	uint64_t tail;
54 	/** Pending queue mask */
55 	uint64_t pq_mask;
56 	/** Timeout to track h/w being unresponsive */
57 	uint64_t time_out;
58 };
59 
60 struct crypto_adpter_info {
61 	bool enabled;
62 	/**< Set if queue pair is added to crypto adapter */
63 	struct rte_mempool *req_mp;
64 	/**< CPT inflight request mempool */
65 };
66 
67 struct cnxk_cpt_qp {
68 	struct roc_cpt_lf lf;
69 	/**< Crypto LF */
70 	struct pending_queue pend_q;
71 	/**< Pending queue */
72 	struct rte_mempool *sess_mp;
73 	/**< Session mempool */
74 	struct rte_mempool *sess_mp_priv;
75 	/**< Session private data mempool */
76 	struct cpt_qp_meta_info meta_info;
77 	/**< Metabuf info required to support operations on the queue pair */
78 	struct roc_cpt_lmtline lmtline;
79 	/**< Lmtline information */
80 	struct crypto_adpter_info ca;
81 	/**< Crypto adapter related info */
82 };
83 
84 int cnxk_cpt_dev_config(struct rte_cryptodev *dev,
85 			struct rte_cryptodev_config *conf);
86 
87 int cnxk_cpt_dev_start(struct rte_cryptodev *dev);
88 
89 void cnxk_cpt_dev_stop(struct rte_cryptodev *dev);
90 
91 int cnxk_cpt_dev_close(struct rte_cryptodev *dev);
92 
93 void cnxk_cpt_dev_info_get(struct rte_cryptodev *dev,
94 			   struct rte_cryptodev_info *info);
95 
96 int cnxk_cpt_queue_pair_setup(struct rte_cryptodev *dev, uint16_t qp_id,
97 			      const struct rte_cryptodev_qp_conf *conf,
98 			      int socket_id __rte_unused);
99 
100 int cnxk_cpt_queue_pair_release(struct rte_cryptodev *dev, uint16_t qp_id);
101 
102 unsigned int cnxk_cpt_sym_session_get_size(struct rte_cryptodev *dev);
103 
104 int cnxk_cpt_sym_session_configure(struct rte_cryptodev *dev,
105 				   struct rte_crypto_sym_xform *xform,
106 				   struct rte_cryptodev_sym_session *sess,
107 				   struct rte_mempool *pool);
108 
109 int sym_session_configure(struct roc_cpt *roc_cpt, int driver_id,
110 			  struct rte_crypto_sym_xform *xform,
111 			  struct rte_cryptodev_sym_session *sess,
112 			  struct rte_mempool *pool);
113 
114 void cnxk_cpt_sym_session_clear(struct rte_cryptodev *dev,
115 				struct rte_cryptodev_sym_session *sess);
116 
117 void sym_session_clear(int driver_id, struct rte_cryptodev_sym_session *sess);
118 
119 unsigned int cnxk_ae_session_size_get(struct rte_cryptodev *dev __rte_unused);
120 
121 void cnxk_ae_session_clear(struct rte_cryptodev *dev,
122 			   struct rte_cryptodev_asym_session *sess);
123 int cnxk_ae_session_cfg(struct rte_cryptodev *dev,
124 			struct rte_crypto_asym_xform *xform,
125 			struct rte_cryptodev_asym_session *sess);
126 void cnxk_cpt_dump_on_err(struct cnxk_cpt_qp *qp);
127 
128 static inline union rte_event_crypto_metadata *
cnxk_event_crypto_mdata_get(struct rte_crypto_op * op)129 cnxk_event_crypto_mdata_get(struct rte_crypto_op *op)
130 {
131 	union rte_event_crypto_metadata *ec_mdata;
132 
133 	if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION)
134 		ec_mdata = rte_cryptodev_sym_session_get_user_data(
135 			op->sym->session);
136 	else if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS &&
137 		 op->private_data_offset)
138 		ec_mdata = (union rte_event_crypto_metadata
139 				    *)((uint8_t *)op + op->private_data_offset);
140 	else
141 		return NULL;
142 
143 	return ec_mdata;
144 }
145 
146 static __rte_always_inline void
pending_queue_advance(uint64_t * index,const uint64_t mask)147 pending_queue_advance(uint64_t *index, const uint64_t mask)
148 {
149 	*index = (*index + 1) & mask;
150 }
151 
152 static __rte_always_inline void
pending_queue_retreat(uint64_t * index,const uint64_t mask,uint64_t nb_entry)153 pending_queue_retreat(uint64_t *index, const uint64_t mask, uint64_t nb_entry)
154 {
155 	*index = (*index - nb_entry) & mask;
156 }
157 
158 static __rte_always_inline uint64_t
pending_queue_infl_cnt(uint64_t head,uint64_t tail,const uint64_t mask)159 pending_queue_infl_cnt(uint64_t head, uint64_t tail, const uint64_t mask)
160 {
161 	/*
162 	 * Mask is nb_desc - 1. Add nb_desc to head and mask to account for
163 	 * cases when tail > head, which happens during wrap around.
164 	 */
165 	return ((head + mask + 1) - tail) & mask;
166 }
167 
168 static __rte_always_inline uint64_t
pending_queue_free_cnt(uint64_t head,uint64_t tail,const uint64_t mask)169 pending_queue_free_cnt(uint64_t head, uint64_t tail, const uint64_t mask)
170 {
171 	/* mask is nb_desc - 1 */
172 	return mask - pending_queue_infl_cnt(head, tail, mask);
173 }
174 
175 #endif /* _CNXK_CRYPTODEV_OPS_H_ */
176