1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2016 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 };
46 
47 /**
48  * @internal
49  * Initialisation parameters for crypto devices
50  */
51 struct rte_cryptodev_pmd_init_params {
52 	char name[RTE_CRYPTODEV_NAME_MAX_LEN];
53 	size_t private_data_size;
54 	int socket_id;
55 	unsigned int max_nb_queue_pairs;
56 };
57 
58 /** Global structure used for maintaining state of allocated crypto devices */
59 struct rte_cryptodev_global {
60 	struct rte_cryptodev *devs;	/**< Device information array */
61 	struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
62 	/**< Device private data */
63 	uint8_t nb_devs;		/**< Number of devices found */
64 	uint8_t max_devs;		/**< Max number of devices */
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  * @param	session_pool	Pointer to device session mempool
192  *
193  * @return	Returns 0 on success.
194  */
195 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
196 		uint16_t qp_id,	const struct rte_cryptodev_qp_conf *qp_conf,
197 		int socket_id, struct rte_mempool *session_pool);
198 
199 /**
200  * Release memory resources allocated by given queue pair.
201  *
202  * @param	dev	Crypto device pointer
203  * @param	qp_id	Queue Pair Index
204  *
205  * @return
206  * - 0 on success.
207  * - EAGAIN if can't close as device is busy
208  */
209 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
210 		uint16_t qp_id);
211 
212 /**
213  * Get number of available queue pairs of a device.
214  *
215  * @param	dev	Crypto device pointer
216  *
217  * @return	Returns number of queue pairs on success.
218  */
219 typedef uint32_t (*cryptodev_queue_pair_count_t)(struct rte_cryptodev *dev);
220 
221 /**
222  * Create a session mempool to allocate sessions from
223  *
224  * @param	dev		Crypto device pointer
225  * @param	nb_objs		number of sessions objects in mempool
226  * @param	obj_cache	l-core object cache size, see *rte_ring_create*
227  * @param	socket_id	Socket Id to allocate  mempool on.
228  *
229  * @return
230  * - On success returns a pointer to a rte_mempool
231  * - On failure returns a NULL pointer
232  */
233 typedef int (*cryptodev_sym_create_session_pool_t)(
234 		struct rte_cryptodev *dev, unsigned nb_objs,
235 		unsigned obj_cache_size, int socket_id);
236 
237 
238 /**
239  * Get the size of a cryptodev session
240  *
241  * @param	dev		Crypto device pointer
242  *
243  * @return
244  *  - On success returns the size of the session structure for device
245  *  - On failure returns 0
246  */
247 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
248 		struct rte_cryptodev *dev);
249 /**
250  * Get the size of a asymmetric cryptodev session
251  *
252  * @param	dev		Crypto device pointer
253  *
254  * @return
255  *  - On success returns the size of the session structure for device
256  *  - On failure returns 0
257  */
258 typedef unsigned int (*cryptodev_asym_get_session_private_size_t)(
259 		struct rte_cryptodev *dev);
260 
261 /**
262  * Configure a Crypto session on a device.
263  *
264  * @param	dev		Crypto device pointer
265  * @param	xform		Single or chain of crypto xforms
266  * @param	priv_sess	Pointer to cryptodev's private session structure
267  * @param	mp		Mempool where the private session is allocated
268  *
269  * @return
270  *  - Returns 0 if private session structure have been created successfully.
271  *  - Returns -EINVAL if input parameters are invalid.
272  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
273  *  - Returns -ENOMEM if the private session could not be allocated.
274  */
275 typedef int (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
276 		struct rte_crypto_sym_xform *xform,
277 		struct rte_cryptodev_sym_session *session,
278 		struct rte_mempool *mp);
279 /**
280  * Configure a Crypto asymmetric session on a device.
281  *
282  * @param	dev		Crypto device pointer
283  * @param	xform		Single or chain of crypto xforms
284  * @param	priv_sess	Pointer to cryptodev's private session structure
285  * @param	mp		Mempool where the private session is allocated
286  *
287  * @return
288  *  - Returns 0 if private session structure have been created successfully.
289  *  - Returns -EINVAL if input parameters are invalid.
290  *  - Returns -ENOTSUP if crypto device does not support the crypto transform.
291  *  - Returns -ENOMEM if the private session could not be allocated.
292  */
293 typedef int (*cryptodev_asym_configure_session_t)(struct rte_cryptodev *dev,
294 		struct rte_crypto_asym_xform *xform,
295 		struct rte_cryptodev_asym_session *session,
296 		struct rte_mempool *mp);
297 /**
298  * Free driver private session data.
299  *
300  * @param	dev		Crypto device pointer
301  * @param	sess		Cryptodev session structure
302  */
303 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
304 		struct rte_cryptodev_sym_session *sess);
305 /**
306  * Free asymmetric session private data.
307  *
308  * @param	dev		Crypto device pointer
309  * @param	sess		Cryptodev session structure
310  */
311 typedef void (*cryptodev_asym_free_session_t)(struct rte_cryptodev *dev,
312 		struct rte_cryptodev_asym_session *sess);
313 
314 /** Crypto device operations function pointer table */
315 struct rte_cryptodev_ops {
316 	cryptodev_configure_t dev_configure;	/**< Configure device. */
317 	cryptodev_start_t dev_start;		/**< Start device. */
318 	cryptodev_stop_t dev_stop;		/**< Stop device. */
319 	cryptodev_close_t dev_close;		/**< Close device. */
320 
321 	cryptodev_info_get_t dev_infos_get;	/**< Get device info. */
322 
323 	cryptodev_stats_get_t stats_get;
324 	/**< Get device statistics. */
325 	cryptodev_stats_reset_t stats_reset;
326 	/**< Reset device statistics. */
327 
328 	cryptodev_queue_pair_setup_t queue_pair_setup;
329 	/**< Set up a device queue pair. */
330 	cryptodev_queue_pair_release_t queue_pair_release;
331 	/**< Release a queue pair. */
332 	cryptodev_queue_pair_count_t queue_pair_count;
333 	/**< Get count of the queue pairs. */
334 
335 	cryptodev_sym_get_session_private_size_t sym_session_get_size;
336 	/**< Return private session. */
337 	cryptodev_asym_get_session_private_size_t asym_session_get_size;
338 	/**< Return asym session private size. */
339 	cryptodev_sym_configure_session_t sym_session_configure;
340 	/**< Configure a Crypto session. */
341 	cryptodev_asym_configure_session_t asym_session_configure;
342 	/**< Configure asymmetric Crypto session. */
343 	cryptodev_sym_free_session_t sym_session_clear;
344 	/**< Clear a Crypto sessions private data. */
345 	cryptodev_asym_free_session_t asym_session_clear;
346 	/**< Clear a Crypto sessions private data. */
347 };
348 
349 
350 /**
351  * Function for internal use by dummy drivers primarily, e.g. ring-based
352  * driver.
353  * Allocates a new cryptodev slot for an crypto device and returns the pointer
354  * to that slot for the driver to use.
355  *
356  * @param	name		Unique identifier name for each device
357  * @param	socket_id	Socket to allocate resources on.
358  * @return
359  *   - Slot in the rte_dev_devices array for a new device;
360  */
361 struct rte_cryptodev *
362 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
363 
364 /**
365  * Function for internal use by dummy drivers primarily, e.g. ring-based
366  * driver.
367  * Release the specified cryptodev device.
368  *
369  * @param cryptodev
370  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
371  * @return
372  *   - 0 on success, negative on error
373  */
374 extern int
375 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
376 
377 
378 /**
379  * @internal
380  *
381  * PMD assist function to parse initialisation arguments for crypto driver
382  * when creating a new crypto PMD device instance.
383  *
384  * PMD driver should set default values for that PMD before calling function,
385  * these default values will be over-written with successfully parsed values
386  * from args string.
387  *
388  * @param	params	parsed PMD initialisation parameters
389  * @param	args	input argument string to parse
390  *
391  * @return
392  *  - 0 on success
393  *  - errno on failure
394  */
395 int
396 rte_cryptodev_pmd_parse_input_args(
397 		struct rte_cryptodev_pmd_init_params *params,
398 		const char *args);
399 
400 /**
401  * @internal
402  *
403  * PMD assist function to provide boiler plate code for crypto driver to create
404  * and allocate resources for a new crypto PMD device instance.
405  *
406  * @param	name	crypto device name.
407  * @param	device	base device instance
408  * @param	params	PMD initialisation parameters
409  *
410  * @return
411  *  - crypto device instance on success
412  *  - NULL on creation failure
413  */
414 struct rte_cryptodev *
415 rte_cryptodev_pmd_create(const char *name,
416 		struct rte_device *device,
417 		struct rte_cryptodev_pmd_init_params *params);
418 
419 /**
420  * @internal
421  *
422  * PMD assist function to provide boiler plate code for crypto driver to
423  * destroy and free resources associated with a crypto PMD device instance.
424  *
425  * @param	cryptodev	crypto device handle.
426  *
427  * @return
428  *  - 0 on success
429  *  - errno on failure
430  */
431 int
432 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
433 
434 /**
435  * Executes all the user application registered callbacks for the specific
436  * device.
437  *  *
438  * @param	dev	Pointer to cryptodev struct
439  * @param	event	Crypto device interrupt event type.
440  *
441  * @return
442  *  void
443  */
444 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
445 				enum rte_cryptodev_event_type event);
446 
447 /**
448  * @internal
449  * Create unique device name
450  */
451 int
452 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
453 
454 /**
455  * @internal
456  * Allocate Cryptodev driver.
457  *
458  * @param crypto_drv
459  *   Pointer to cryptodev_driver.
460  * @param drv
461  *   Pointer to rte_driver.
462  *
463  * @return
464  *  The driver type identifier
465  */
466 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
467 		const struct rte_driver *drv);
468 
469 
470 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
471 RTE_INIT(init_ ##driver_id)\
472 {\
473 	driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
474 }
475 
476 static inline void *
477 get_sym_session_private_data(const struct rte_cryptodev_sym_session *sess,
478 		uint8_t driver_id) {
479 	return sess->sess_private_data[driver_id];
480 }
481 
482 static inline void
483 set_sym_session_private_data(struct rte_cryptodev_sym_session *sess,
484 		uint8_t driver_id, void *private_data)
485 {
486 	sess->sess_private_data[driver_id] = private_data;
487 }
488 
489 static inline void *
490 get_asym_session_private_data(const struct rte_cryptodev_asym_session *sess,
491 		uint8_t driver_id) {
492 	return sess->sess_private_data[driver_id];
493 }
494 
495 static inline void
496 set_asym_session_private_data(struct rte_cryptodev_asym_session *sess,
497 		uint8_t driver_id, void *private_data)
498 {
499 	sess->sess_private_data[driver_id] = private_data;
500 }
501 
502 #ifdef __cplusplus
503 }
504 #endif
505 
506 #endif /* _RTE_CRYPTODEV_PMD_H_ */
507