Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2025 ZTE Corporation
3 : : */
4 : :
5 : : #include <rte_cryptodev.h>
6 : :
7 : : #include "zsda_crypto_pmd.h"
8 : : #include "zsda_crypto_session.h"
9 : : #include "zsda_crypto.h"
10 : : #include "zsda_crypto_capabilities.h"
11 : :
12 : : uint8_t zsda_crypto_driver_id;
13 : :
14 : : static int
15 : 0 : zsda_dev_config(__rte_unused struct rte_cryptodev *dev,
16 : : __rte_unused struct rte_cryptodev_config *config)
17 : : {
18 : 0 : return ZSDA_SUCCESS;
19 : : }
20 : :
21 : : static int
22 : 0 : zsda_dev_start(struct rte_cryptodev *dev)
23 : : {
24 : 0 : struct zsda_crypto_dev_private *crypto_dev = dev->data->dev_private;
25 : : int ret;
26 : :
27 : 0 : ret = zsda_queue_start(crypto_dev->zsda_pci_dev->pci_dev);
28 : :
29 : 0 : return ret;
30 : : }
31 : :
32 : : static void
33 : 0 : zsda_dev_stop(struct rte_cryptodev *dev)
34 : : {
35 : 0 : struct zsda_crypto_dev_private *crypto_dev = dev->data->dev_private;
36 : :
37 : 0 : zsda_queue_stop(crypto_dev->zsda_pci_dev->pci_dev);
38 : 0 : }
39 : :
40 : : static int
41 : 0 : zsda_qp_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
42 : : {
43 : 0 : return zsda_queue_pair_release(
44 : 0 : (struct zsda_qp **)&(dev->data->queue_pairs[queue_pair_id]));
45 : : }
46 : :
47 : : static int
48 : 0 : zsda_dev_close(struct rte_cryptodev *dev)
49 : : {
50 : : int ret = ZSDA_SUCCESS;
51 : : uint16_t i;
52 : :
53 [ # # ]: 0 : for (i = 0; i < dev->data->nb_queue_pairs; i++)
54 : 0 : ret |= zsda_qp_release(dev, i);
55 : 0 : return ret;
56 : : }
57 : :
58 : : static uint16_t
59 : : zsda_crypto_max_nb_qps(void)
60 : : {
61 : 0 : uint16_t encrypt = zsda_nb_qps.encrypt;
62 : 0 : uint16_t decrypt = zsda_nb_qps.decrypt;
63 : 0 : uint16_t hash = zsda_nb_qps.hash;
64 : : uint16_t min = 0;
65 : :
66 : 0 : if ((encrypt == MAX_QPS_ON_FUNCTION) ||
67 [ # # # # ]: 0 : (decrypt == MAX_QPS_ON_FUNCTION) ||
68 : : (hash == MAX_QPS_ON_FUNCTION))
69 : : min = MAX_QPS_ON_FUNCTION;
70 : : else {
71 : 0 : min = (encrypt < decrypt) ? encrypt : decrypt;
72 : 0 : min = (min < hash) ? min : hash;
73 : : }
74 : :
75 [ # # ]: 0 : if (min == 0)
76 : 0 : return MAX_QPS_ON_FUNCTION;
77 : : return min;
78 : : }
79 : :
80 : : static void
81 : 0 : zsda_dev_info_get(struct rte_cryptodev *dev,
82 : : struct rte_cryptodev_info *info)
83 : : {
84 : 0 : struct zsda_crypto_dev_private *crypto_dev_priv = dev->data->dev_private;
85 : :
86 [ # # ]: 0 : if (info != NULL) {
87 : 0 : info->max_nb_queue_pairs = zsda_crypto_max_nb_qps();
88 : 0 : info->feature_flags = dev->feature_flags;
89 : 0 : info->capabilities = crypto_dev_priv->zsda_crypto_capabilities;
90 : 0 : info->driver_id = zsda_crypto_driver_id;
91 : 0 : info->sym.max_nb_sessions = 0;
92 : : }
93 : 0 : }
94 : :
95 : : static void
96 : 0 : zsda_crypto_stats_get(struct rte_cryptodev *dev, struct rte_cryptodev_stats *stats)
97 : : {
98 : 0 : struct zsda_qp_stat comm = {0};
99 : :
100 : 0 : zsda_stats_get(dev->data->queue_pairs, dev->data->nb_queue_pairs,
101 : : &comm);
102 : 0 : stats->enqueued_count = comm.enqueued_count;
103 : 0 : stats->dequeued_count = comm.dequeued_count;
104 : 0 : stats->enqueue_err_count = comm.enqueue_err_count;
105 : 0 : stats->dequeue_err_count = comm.dequeue_err_count;
106 : 0 : }
107 : :
108 : : static void
109 : 0 : zsda_crypto_stats_reset(struct rte_cryptodev *dev)
110 : : {
111 : 0 : zsda_stats_reset(dev->data->queue_pairs, dev->data->nb_queue_pairs);
112 : 0 : }
113 : :
114 : :
115 : : static int
116 : 0 : zsda_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
117 : : const struct rte_cryptodev_qp_conf *qp_conf,
118 : : int socket_id)
119 : : {
120 : : int ret = ZSDA_SUCCESS;
121 : : struct zsda_qp *qp_new;
122 : 0 : struct zsda_qp **qp_addr =
123 : 0 : (struct zsda_qp **)&(dev->data->queue_pairs[qp_id]);
124 : 0 : struct zsda_crypto_dev_private *crypto_dev_priv = dev->data->dev_private;
125 : 0 : struct zsda_pci_device *zsda_pci_dev = crypto_dev_priv->zsda_pci_dev;
126 : : uint32_t nb_des = qp_conf->nb_descriptors;
127 : : struct task_queue_info task_q_info;
128 : :
129 : : nb_des = (nb_des == NB_DES) ? nb_des : NB_DES;
130 : :
131 [ # # ]: 0 : if (*qp_addr != NULL) {
132 : : ret = zsda_qp_release(dev, qp_id);
133 [ # # ]: 0 : if (ret)
134 : : return ret;
135 : : }
136 : :
137 : 0 : qp_new = rte_zmalloc_socket("zsda PMD qp metadata", sizeof(*qp_new),
138 : : RTE_CACHE_LINE_SIZE, socket_id);
139 [ # # ]: 0 : if (qp_new == NULL) {
140 : 0 : ZSDA_LOG(ERR, "Failed to alloc mem for qp struct");
141 : 0 : return -ENOMEM;
142 : : }
143 : :
144 : 0 : task_q_info.nb_des = nb_des;
145 : 0 : task_q_info.socket_id = socket_id;
146 : 0 : task_q_info.qp_id = qp_id;
147 : 0 : task_q_info.rx_cb = zsda_crypto_callback;
148 : :
149 : 0 : task_q_info.type = ZSDA_SERVICE_CRYPTO_ENCRY;
150 : 0 : task_q_info.service_str = "encry";
151 : 0 : task_q_info.tx_cb = zsda_crypto_wqe_build;
152 : 0 : task_q_info.match = zsda_encry_match;
153 : 0 : ret = zsda_task_queue_setup(zsda_pci_dev, qp_new, &task_q_info);
154 : :
155 : 0 : task_q_info.type = ZSDA_SERVICE_CRYPTO_DECRY;
156 : 0 : task_q_info.service_str = "decry";
157 : 0 : task_q_info.tx_cb = zsda_crypto_wqe_build;
158 : 0 : task_q_info.match = zsda_decry_match;
159 : 0 : ret |= zsda_task_queue_setup(zsda_pci_dev, qp_new, &task_q_info);
160 : :
161 : 0 : task_q_info.type = ZSDA_SERVICE_HASH_ENCODE;
162 : 0 : task_q_info.service_str = "hash";
163 : 0 : task_q_info.tx_cb = zsda_hash_wqe_build;
164 : 0 : task_q_info.match = zsda_hash_match;
165 : 0 : ret |= zsda_task_queue_setup(zsda_pci_dev, qp_new, &task_q_info);
166 : :
167 [ # # ]: 0 : if (ret) {
168 : 0 : ZSDA_LOG(ERR, "zsda_task_queue_setup crypto is failed!");
169 : 0 : rte_free(qp_new);
170 : 0 : return ret;
171 : : }
172 : :
173 : 0 : *qp_addr = qp_new;
174 : :
175 : 0 : return ret;
176 : : }
177 : :
178 : : static unsigned int
179 : 0 : zsda_sym_session_private_size_get(struct rte_cryptodev *dev __rte_unused)
180 : : {
181 : 0 : return RTE_ALIGN_CEIL(sizeof(struct zsda_sym_session), 8);
182 : : }
183 : :
184 : : static int
185 : 0 : zsda_sym_session_configure(struct rte_cryptodev *dev __rte_unused,
186 : : struct rte_crypto_sym_xform *xform,
187 : : struct rte_cryptodev_sym_session *sess)
188 : : {
189 : : void *sess_private_data;
190 : : int ret;
191 : :
192 [ # # ]: 0 : if (unlikely(sess == NULL)) {
193 : 0 : ZSDA_LOG(ERR, "Invalid session struct");
194 : 0 : return -EINVAL;
195 : : }
196 : :
197 : 0 : sess_private_data = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
198 : :
199 : 0 : ret = zsda_crypto_session_parameters_set(
200 : : sess_private_data, xform);
201 : :
202 [ # # ]: 0 : if (ret != ZSDA_SUCCESS)
203 : 0 : ZSDA_LOG(ERR, "Failed configure session parameters");
204 : :
205 : : return ret;
206 : : }
207 : :
208 : : static void
209 : 0 : zsda_sym_session_clear(struct rte_cryptodev *dev __rte_unused,
210 : : struct rte_cryptodev_sym_session *sess)
211 : : {
212 : 0 : struct zsda_sym_session *sess_priv = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
213 : : memset(sess_priv, 0, sizeof(struct zsda_sym_session));
214 : 0 : }
215 : :
216 : : static struct rte_cryptodev_ops crypto_zsda_ops = {
217 : : .dev_configure = zsda_dev_config,
218 : : .dev_start = zsda_dev_start,
219 : : .dev_stop = zsda_dev_stop,
220 : : .dev_close = zsda_dev_close,
221 : : .dev_infos_get = zsda_dev_info_get,
222 : :
223 : : .stats_get = zsda_crypto_stats_get,
224 : : .stats_reset = zsda_crypto_stats_reset,
225 : : .queue_pair_setup = zsda_qp_setup,
226 : : .queue_pair_release = zsda_qp_release,
227 : :
228 : : .sym_session_get_size = zsda_sym_session_private_size_get,
229 : : .sym_session_configure = zsda_sym_session_configure,
230 : : .sym_session_clear = zsda_sym_session_clear,
231 : : };
232 : :
233 : : static const char zsda_crypto_drv_name[] = RTE_STR(CRYPTODEV_NAME_ZSDA_PMD);
234 : : static const struct rte_driver cryptodev_zsda_crypto_driver = {
235 : : .name = zsda_crypto_drv_name,
236 : : .alias = zsda_crypto_drv_name
237 : : };
238 : :
239 : : static uint16_t
240 : 0 : zsda_crypto_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
241 : : uint16_t nb_ops)
242 : : {
243 : 0 : return zsda_enqueue_burst((struct zsda_qp *)qp, (void **)ops,
244 : : nb_ops);
245 : : }
246 : :
247 : : static uint16_t
248 : 0 : zsda_crypto_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
249 : : uint16_t nb_ops)
250 : : {
251 : 0 : return zsda_dequeue_burst((struct zsda_qp *)qp, (void **)ops,
252 : : nb_ops);
253 : : }
254 : :
255 : : int
256 : 0 : zsda_crypto_dev_create(struct zsda_pci_device *zsda_pci_dev)
257 : : {
258 : : struct zsda_device_info *dev_info =
259 : 0 : &zsda_devs[zsda_pci_dev->zsda_dev_id];
260 : :
261 : 0 : struct rte_cryptodev_pmd_init_params init_params = {
262 : : .name = "",
263 : 0 : .socket_id = (int)rte_socket_id(),
264 : : .private_data_size = sizeof(struct zsda_crypto_dev_private)
265 : : };
266 : :
267 : : char name[RTE_CRYPTODEV_NAME_MAX_LEN];
268 : : char capa_memz_name[RTE_CRYPTODEV_NAME_MAX_LEN];
269 : : struct rte_cryptodev *cryptodev;
270 : : struct zsda_crypto_dev_private *crypto_dev_priv;
271 : : const struct rte_cryptodev_capabilities *capabilities;
272 : : uint64_t capa_size;
273 : :
274 [ # # ]: 0 : if (rte_eal_process_type() != RTE_PROC_PRIMARY)
275 : : return ZSDA_SUCCESS;
276 : :
277 : 0 : snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN, "%s_%s", zsda_pci_dev->name,
278 : : "crypto");
279 : 0 : ZSDA_LOG(DEBUG, "Creating ZSDA crypto device %s", name);
280 : :
281 : 0 : dev_info->crypto_rte_dev.driver = &cryptodev_zsda_crypto_driver;
282 : 0 : dev_info->crypto_rte_dev.numa_node = dev_info->pci_dev->device.numa_node;
283 : :
284 : 0 : cryptodev = rte_cryptodev_pmd_create(name, &(dev_info->crypto_rte_dev),
285 : : &init_params);
286 : :
287 [ # # ]: 0 : if (cryptodev == NULL) {
288 : 0 : ZSDA_LOG(ERR, "Failed! rte_cryptodev_pmd_create");
289 : 0 : goto error;
290 : : }
291 : :
292 : 0 : dev_info->crypto_rte_dev.name = cryptodev->data->name;
293 : 0 : cryptodev->driver_id = zsda_crypto_driver_id;
294 : :
295 : 0 : cryptodev->dev_ops = &crypto_zsda_ops;
296 : :
297 : 0 : cryptodev->enqueue_burst = zsda_crypto_enqueue_op_burst;
298 : 0 : cryptodev->dequeue_burst = zsda_crypto_dequeue_op_burst;
299 : 0 : cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
300 : : RTE_CRYPTODEV_FF_SYM_SESSIONLESS |
301 : : RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
302 : : RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
303 : : RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
304 : : RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
305 : : RTE_CRYPTODEV_FF_HW_ACCELERATED;
306 : : capabilities = zsda_crypto_dev_capabilities;
307 : :
308 : 0 : crypto_dev_priv = cryptodev->data->dev_private;
309 : 0 : crypto_dev_priv->zsda_pci_dev = zsda_pci_dev;
310 : 0 : crypto_dev_priv->cryptodev = cryptodev;
311 : :
312 : : capa_size = sizeof(zsda_crypto_dev_capabilities);
313 : :
314 : : snprintf(capa_memz_name, RTE_CRYPTODEV_NAME_MAX_LEN, "ZSDA_CRYPTO_CAPA");
315 : 0 : crypto_dev_priv->capa_mz = rte_memzone_lookup(capa_memz_name);
316 [ # # ]: 0 : if (crypto_dev_priv->capa_mz == NULL)
317 : 0 : crypto_dev_priv->capa_mz = rte_memzone_reserve(
318 : 0 : capa_memz_name, capa_size, rte_socket_id(), 0);
319 : :
320 [ # # ]: 0 : if (crypto_dev_priv->capa_mz == NULL) {
321 : 0 : ZSDA_LOG(ERR, "Failed! crypto_dev_priv->capa_mz");
322 : 0 : goto error;
323 : : }
324 : :
325 : 0 : memcpy(crypto_dev_priv->capa_mz->addr, capabilities, capa_size);
326 : 0 : crypto_dev_priv->zsda_crypto_capabilities = crypto_dev_priv->capa_mz->addr;
327 : :
328 : 0 : zsda_pci_dev->crypto_dev_priv = crypto_dev_priv;
329 : :
330 : 0 : return ZSDA_SUCCESS;
331 : :
332 : 0 : error:
333 : :
334 : 0 : rte_cryptodev_pmd_destroy(cryptodev);
335 : : memset(&dev_info->crypto_rte_dev, 0, sizeof(dev_info->crypto_rte_dev));
336 : :
337 : 0 : return -EFAULT;
338 : : }
339 : :
340 : : void
341 : 0 : zsda_crypto_dev_destroy(struct zsda_pci_device *zsda_pci_dev)
342 : : {
343 : : struct zsda_crypto_dev_private *crypto_dev_priv;
344 : :
345 [ # # ]: 0 : if (zsda_pci_dev == NULL)
346 : : return;
347 : :
348 : 0 : crypto_dev_priv = zsda_pci_dev->crypto_dev_priv;
349 [ # # ]: 0 : if (crypto_dev_priv == NULL)
350 : : return;
351 : :
352 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
353 : 0 : rte_memzone_free(crypto_dev_priv->capa_mz);
354 : :
355 : 0 : zsda_dev_close(crypto_dev_priv->cryptodev);
356 : :
357 : 0 : rte_cryptodev_pmd_destroy(crypto_dev_priv->cryptodev);
358 : 0 : zsda_devs[zsda_pci_dev->zsda_dev_id].crypto_rte_dev.name = NULL;
359 : 0 : zsda_pci_dev->crypto_dev_priv = NULL;
360 : : }
361 : :
362 : : static struct cryptodev_driver zsda_crypto_drv;
363 : 252 : RTE_PMD_REGISTER_CRYPTO_DRIVER(zsda_crypto_drv, cryptodev_zsda_crypto_driver,
364 : : zsda_crypto_driver_id);
|