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