xref: /dpdk/lib/cryptodev/cryptodev_pmd.h (revision 03ab51eafda992874a48c392ca66ffb577fe2b71)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020 Intel Corporation.
3  */
4 
5 #ifndef _CRYPTODEV_PMD_H_
6 #define _CRYPTODEV_PMD_H_
7 
8 /** @file
9  * RTE Crypto PMD APIs
10  *
11  * @note
12  * These API are from crypto PMD only and user applications should not call
13  * them directly.
14  */
15 
16 #include <string.h>
17 
18 #include <rte_config.h>
19 #include <rte_dev.h>
20 #include <rte_malloc.h>
21 #include <rte_mbuf.h>
22 #include <rte_mempool.h>
23 #include <rte_log.h>
24 #include <rte_common.h>
25 
26 #include "rte_crypto.h"
27 #include "rte_cryptodev.h"
28 
29 
30 #define RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS	8
31 
32 #define RTE_CRYPTODEV_PMD_NAME_ARG			("name")
33 #define RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG			("max_nb_queue_pairs")
34 #define RTE_CRYPTODEV_PMD_SOCKET_ID_ARG			("socket_id")
35 
36 
37 static const char * const cryptodev_pmd_valid_params[] = {
38 	RTE_CRYPTODEV_PMD_NAME_ARG,
39 	RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
40 	RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
41 	NULL
42 };
43 
44 /**
45  * @internal
46  * Initialisation parameters for crypto devices
47  */
48 struct rte_cryptodev_pmd_init_params {
49 	char name[RTE_CRYPTODEV_NAME_MAX_LEN];
50 	size_t private_data_size;
51 	int socket_id;
52 	unsigned int max_nb_queue_pairs;
53 };
54 
55 /** Global structure used for maintaining state of allocated crypto devices */
56 struct rte_cryptodev_global {
57 	struct rte_cryptodev *devs;	/**< Device information array */
58 	struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
59 	/**< Device private data */
60 	uint8_t nb_devs;		/**< Number of devices found */
61 };
62 
63 /* Cryptodev driver, containing the driver ID */
64 struct cryptodev_driver {
65 	RTE_TAILQ_ENTRY(cryptodev_driver) next; /**< Next in list. */
66 	const struct rte_driver *driver;
67 	uint8_t id;
68 };
69 
70 /**
71  * Get the rte_cryptodev structure device pointer for the device. Assumes a
72  * valid device index.
73  *
74  * @param	dev_id	Device ID value to select the device structure.
75  *
76  * @return
77  *   - The rte_cryptodev structure pointer for the given device ID.
78  */
79 __rte_internal
80 struct rte_cryptodev *
81 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
82 
83 /**
84  * Get the rte_cryptodev structure device pointer for the named device.
85  *
86  * @param	name	device name to select the device structure.
87  *
88  * @return
89  *   - The rte_cryptodev structure pointer for the given device ID.
90  */
91 __rte_internal
92 struct rte_cryptodev *
93 rte_cryptodev_pmd_get_named_dev(const char *name);
94 
95 /**
96  * The pool of rte_cryptodev structures.
97  */
98 extern struct rte_cryptodev *rte_cryptodevs;
99 
100 
101 /**
102  * Definitions of all functions exported by a driver through the
103  * the generic structure of type *crypto_dev_ops* supplied in the
104  * *rte_cryptodev* structure associated with a device.
105  */
106 
107 /**
108  *	Function used to configure device.
109  *
110  * @param	dev	Crypto device pointer
111  * @param	config	Crypto device configurations
112  *
113  * @return	Returns 0 on success
114  */
115 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
116 		struct rte_cryptodev_config *config);
117 
118 /**
119  * Function used to start a configured device.
120  *
121  * @param	dev	Crypto device pointer
122  *
123  * @return	Returns 0 on success
124  */
125 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
126 
127 /**
128  * Function used to stop a configured device.
129  *
130  * @param	dev	Crypto device pointer
131  */
132 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
133 
134 /**
135  * Function used to close a configured device.
136  *
137  * @param	dev	Crypto device pointer
138  * @return
139  * - 0 on success.
140  * - EAGAIN if can't close as device is busy
141  */
142 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
143 
144 
145 /**
146  * Function used to get statistics of a device.
147  *
148  * @param	dev	Crypto device pointer
149  * @param	stats	Pointer to crypto device stats structure to populate
150  */
151 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
152 				struct rte_cryptodev_stats *stats);
153 
154 
155 /**
156  * Function used to reset statistics of a device.
157  *
158  * @param	dev	Crypto device pointer
159  */
160 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
161 
162 
163 /**
164  * Function used to get specific information of a device.
165  *
166  * @param	dev		Crypto device pointer
167  * @param	dev_info	Pointer to infos structure to populate
168  */
169 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
170 				struct rte_cryptodev_info *dev_info);
171 
172 /**
173  * Setup a queue pair for a device.
174  *
175  * @param	dev		Crypto device pointer
176  * @param	qp_id		Queue Pair Index
177  * @param	qp_conf		Queue configuration structure
178  * @param	socket_id	Socket Index
179  *
180  * @return	Returns 0 on success.
181  */
182 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
183 		uint16_t qp_id,	const struct rte_cryptodev_qp_conf *qp_conf,
184 		int socket_id);
185 
186 /**
187  * Release memory resources allocated by given queue pair.
188  *
189  * @param	dev	Crypto device pointer
190  * @param	qp_id	Queue Pair Index
191  *
192  * @return
193  * - 0 on success.
194  * - EAGAIN if can't close as device is busy
195  */
196 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
197 		uint16_t qp_id);
198 
199 /**
200  * Create a session mempool to allocate sessions from
201  *
202  * @param	dev		Crypto device pointer
203  * @param	nb_objs		number of sessions objects in mempool
204  * @param	obj_cache_size	l-core object cache size, see *rte_ring_create*
205  * @param	socket_id	Socket Id to allocate  mempool on.
206  *
207  * @return
208  * - On success returns a pointer to a rte_mempool
209  * - On failure returns a NULL pointer
210  */
211 typedef int (*cryptodev_sym_create_session_pool_t)(
212 		struct rte_cryptodev *dev, unsigned nb_objs,
213 		unsigned obj_cache_size, int socket_id);
214 
215 
216 /**
217  * Get the size of a cryptodev session
218  *
219  * @param	dev		Crypto device pointer
220  *
221  * @return
222  *  - On success returns the size of the session structure for device
223  *  - On failure returns 0
224  */
225 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
226 		struct rte_cryptodev *dev);
227 /**
228  * Get the size of a asymmetric cryptodev session
229  *
230  * @param	dev		Crypto device pointer
231  *
232  * @return
233  *  - On success returns the size of the session structure for device
234  *  - On failure returns 0
235  */
236 typedef unsigned int (*cryptodev_asym_get_session_private_size_t)(
237 		struct rte_cryptodev *dev);
238 
239 /**
240  * Configure a Crypto session on a device.
241  *
242  * @param	dev		Crypto device pointer
243  * @param	xform		Single or chain of crypto xforms
244  * @param	session		Pointer to cryptodev's private session structure
245  * @param	mp		Mempool where the private session is allocated
246  *
247  * @return
248  *  - Returns 0 if private session structure have been created successfully.
249  *  - Returns -EINVAL if input parameters are invalid.
250  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
251  *  - Returns -ENOMEM if the private session could not be allocated.
252  */
253 typedef int (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
254 		struct rte_crypto_sym_xform *xform,
255 		struct rte_cryptodev_sym_session *session,
256 		struct rte_mempool *mp);
257 /**
258  * Configure a Crypto asymmetric session on a device.
259  *
260  * @param	dev		Crypto device pointer
261  * @param	xform		Single or chain of crypto xforms
262  * @param	session		Pointer to cryptodev's private session structure
263  * @param	mp		Mempool where the private session is allocated
264  *
265  * @return
266  *  - Returns 0 if private session structure have been created successfully.
267  *  - Returns -EINVAL if input parameters are invalid.
268  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
269  *  - Returns -ENOMEM if the private session could not be allocated.
270  */
271 typedef int (*cryptodev_asym_configure_session_t)(struct rte_cryptodev *dev,
272 		struct rte_crypto_asym_xform *xform,
273 		struct rte_cryptodev_asym_session *session,
274 		struct rte_mempool *mp);
275 /**
276  * Free driver private session data.
277  *
278  * @param	dev		Crypto device pointer
279  * @param	sess		Cryptodev session structure
280  */
281 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
282 		struct rte_cryptodev_sym_session *sess);
283 /**
284  * Free asymmetric session private data.
285  *
286  * @param	dev		Crypto device pointer
287  * @param	sess		Cryptodev session structure
288  */
289 typedef void (*cryptodev_asym_free_session_t)(struct rte_cryptodev *dev,
290 		struct rte_cryptodev_asym_session *sess);
291 /**
292  * Perform actual crypto processing (encrypt/digest or auth/decrypt)
293  * on user provided data.
294  *
295  * @param	dev	Crypto device pointer
296  * @param	sess	Cryptodev session structure
297  * @param	ofs	Start and stop offsets for auth and cipher operations
298  * @param	vec	Vectorized operation descriptor
299  *
300  * @return
301  *  - Returns number of successfully processed packets.
302  *
303  */
304 typedef uint32_t (*cryptodev_sym_cpu_crypto_process_t)
305 	(struct rte_cryptodev *dev, struct rte_cryptodev_sym_session *sess,
306 	union rte_crypto_sym_ofs ofs, struct rte_crypto_sym_vec *vec);
307 
308 /**
309  * Typedef that the driver provided to get service context private date size.
310  *
311  * @param	dev	Crypto device pointer.
312  *
313  * @return
314  *   - On success return the size of the device's service context private data.
315  *   - On failure return negative integer.
316  */
317 typedef int (*cryptodev_sym_get_raw_dp_ctx_size_t)(struct rte_cryptodev *dev);
318 
319 /**
320  * Typedef that the driver provided to configure raw data-path context.
321  *
322  * @param	dev		Crypto device pointer.
323  * @param	qp_id		Crypto device queue pair index.
324  * @param	ctx		The raw data-path context data.
325  * @param	sess_type	session type.
326  * @param	session_ctx	Session context data. If NULL the driver
327  *				shall only configure the drv_ctx_data in
328  *				ctx buffer. Otherwise the driver shall only
329  *				parse the session_ctx to set appropriate
330  *				function pointers in ctx.
331  * @param	is_update	Set 0 if it is to initialize the ctx.
332  *				Set 1 if ctx is initialized and only to update
333  *				session context data.
334  * @return
335  *   - On success return 0.
336  *   - On failure return negative integer.
337  */
338 typedef int (*cryptodev_sym_configure_raw_dp_ctx_t)(
339 	struct rte_cryptodev *dev, uint16_t qp_id,
340 	struct rte_crypto_raw_dp_ctx *ctx,
341 	enum rte_crypto_op_sess_type sess_type,
342 	union rte_cryptodev_session_ctx session_ctx, uint8_t is_update);
343 
344 /** Crypto device operations function pointer table */
345 struct rte_cryptodev_ops {
346 	cryptodev_configure_t dev_configure;	/**< Configure device. */
347 	cryptodev_start_t dev_start;		/**< Start device. */
348 	cryptodev_stop_t dev_stop;		/**< Stop device. */
349 	cryptodev_close_t dev_close;		/**< Close device. */
350 
351 	cryptodev_info_get_t dev_infos_get;	/**< Get device info. */
352 
353 	cryptodev_stats_get_t stats_get;
354 	/**< Get device statistics. */
355 	cryptodev_stats_reset_t stats_reset;
356 	/**< Reset device statistics. */
357 
358 	cryptodev_queue_pair_setup_t queue_pair_setup;
359 	/**< Set up a device queue pair. */
360 	cryptodev_queue_pair_release_t queue_pair_release;
361 	/**< Release a queue pair. */
362 
363 	cryptodev_sym_get_session_private_size_t sym_session_get_size;
364 	/**< Return private session. */
365 	cryptodev_asym_get_session_private_size_t asym_session_get_size;
366 	/**< Return asym session private size. */
367 	cryptodev_sym_configure_session_t sym_session_configure;
368 	/**< Configure a Crypto session. */
369 	cryptodev_asym_configure_session_t asym_session_configure;
370 	/**< Configure asymmetric Crypto session. */
371 	cryptodev_sym_free_session_t sym_session_clear;
372 	/**< Clear a Crypto sessions private data. */
373 	cryptodev_asym_free_session_t asym_session_clear;
374 	/**< Clear a Crypto sessions private data. */
375 	union {
376 		cryptodev_sym_cpu_crypto_process_t sym_cpu_process;
377 		/**< process input data synchronously (cpu-crypto). */
378 		__extension__
379 		struct {
380 			cryptodev_sym_get_raw_dp_ctx_size_t
381 				sym_get_raw_dp_ctx_size;
382 			/**< Get raw data path service context data size. */
383 			cryptodev_sym_configure_raw_dp_ctx_t
384 				sym_configure_raw_dp_ctx;
385 			/**< Initialize raw data path context data. */
386 		};
387 	};
388 };
389 
390 
391 /**
392  * Function for internal use by dummy drivers primarily, e.g. ring-based
393  * driver.
394  * Allocates a new cryptodev slot for an crypto device and returns the pointer
395  * to that slot for the driver to use.
396  *
397  * @param	name		Unique identifier name for each device
398  * @param	socket_id	Socket to allocate resources on.
399  * @return
400  *   - Slot in the rte_dev_devices array for a new device;
401  */
402 __rte_internal
403 struct rte_cryptodev *
404 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
405 
406 /**
407  * Function for internal use by dummy drivers primarily, e.g. ring-based
408  * driver.
409  * Release the specified cryptodev device.
410  *
411  * @param cryptodev
412  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
413  * @return
414  *   - 0 on success, negative on error
415  */
416 __rte_internal
417 extern int
418 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
419 
420 
421 /**
422  * @internal
423  *
424  * PMD assist function to parse initialisation arguments for crypto driver
425  * when creating a new crypto PMD device instance.
426  *
427  * PMD driver should set default values for that PMD before calling function,
428  * these default values will be over-written with successfully parsed values
429  * from args string.
430  *
431  * @param	params	parsed PMD initialisation parameters
432  * @param	args	input argument string to parse
433  *
434  * @return
435  *  - 0 on success
436  *  - errno on failure
437  */
438 __rte_internal
439 int
440 rte_cryptodev_pmd_parse_input_args(
441 		struct rte_cryptodev_pmd_init_params *params,
442 		const char *args);
443 
444 /**
445  * @internal
446  *
447  * PMD assist function to provide boiler plate code for crypto driver to create
448  * and allocate resources for a new crypto PMD device instance.
449  *
450  * @param	name	crypto device name.
451  * @param	device	base device instance
452  * @param	params	PMD initialisation parameters
453  *
454  * @return
455  *  - crypto device instance on success
456  *  - NULL on creation failure
457  */
458 __rte_internal
459 struct rte_cryptodev *
460 rte_cryptodev_pmd_create(const char *name,
461 		struct rte_device *device,
462 		struct rte_cryptodev_pmd_init_params *params);
463 
464 /**
465  * @internal
466  *
467  * PMD assist function to provide boiler plate code for crypto driver to
468  * destroy and free resources associated with a crypto PMD device instance.
469  *
470  * @param	cryptodev	crypto device handle.
471  *
472  * @return
473  *  - 0 on success
474  *  - errno on failure
475  */
476 __rte_internal
477 int
478 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
479 
480 /**
481  * Executes all the user application registered callbacks for the specific
482  * device.
483  *  *
484  * @param	dev	Pointer to cryptodev struct
485  * @param	event	Crypto device interrupt event type.
486  *
487  * @return
488  *  void
489  */
490 __rte_internal
491 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
492 				enum rte_cryptodev_event_type event);
493 
494 /**
495  * @internal
496  * Create unique device name
497  */
498 __rte_internal
499 int
500 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
501 
502 /**
503  * @internal
504  * Allocate Cryptodev driver.
505  *
506  * @param crypto_drv
507  *   Pointer to cryptodev_driver.
508  * @param drv
509  *   Pointer to rte_driver.
510  *
511  * @return
512  *  The driver type identifier
513  */
514 __rte_internal
515 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
516 		const struct rte_driver *drv);
517 
518 
519 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
520 RTE_INIT(init_ ##driver_id)\
521 {\
522 	driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
523 }
524 
525 static inline void *
526 get_sym_session_private_data(const struct rte_cryptodev_sym_session *sess,
527 		uint8_t driver_id) {
528 	if (unlikely(sess->nb_drivers <= driver_id))
529 		return NULL;
530 
531 	return sess->sess_data[driver_id].data;
532 }
533 
534 static inline void
535 set_sym_session_private_data(struct rte_cryptodev_sym_session *sess,
536 		uint8_t driver_id, void *private_data)
537 {
538 	if (unlikely(sess->nb_drivers <= driver_id)) {
539 		CDEV_LOG_ERR("Set private data for driver %u not allowed\n",
540 				driver_id);
541 		return;
542 	}
543 
544 	sess->sess_data[driver_id].data = private_data;
545 }
546 
547 static inline void *
548 get_asym_session_private_data(const struct rte_cryptodev_asym_session *sess,
549 		uint8_t driver_id) {
550 	return sess->sess_private_data[driver_id];
551 }
552 
553 static inline void
554 set_asym_session_private_data(struct rte_cryptodev_asym_session *sess,
555 		uint8_t driver_id, void *private_data)
556 {
557 	sess->sess_private_data[driver_id] = private_data;
558 }
559 
560 #endif /* _CRYPTODEV_PMD_H_ */
561