cryptodev: define value for unlimited sessions
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev_pmd.h
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 #define RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_SESSIONS       2048
36
37 #define RTE_CRYPTODEV_PMD_NAME_ARG                      ("name")
38 #define RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG                 ("max_nb_queue_pairs")
39 #define RTE_CRYPTODEV_PMD_MAX_NB_SESS_ARG               ("max_nb_sessions")
40 #define RTE_CRYPTODEV_PMD_SOCKET_ID_ARG                 ("socket_id")
41
42
43 static const char * const cryptodev_pmd_valid_params[] = {
44         RTE_CRYPTODEV_PMD_NAME_ARG,
45         RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
46         RTE_CRYPTODEV_PMD_MAX_NB_SESS_ARG,
47         RTE_CRYPTODEV_PMD_SOCKET_ID_ARG
48 };
49
50 /**
51  * @internal
52  * Initialisation parameters for crypto devices
53  */
54 struct rte_cryptodev_pmd_init_params {
55         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
56         size_t private_data_size;
57         int socket_id;
58         unsigned int max_nb_queue_pairs;
59         unsigned int max_nb_sessions;
60 };
61
62 /** Global structure used for maintaining state of allocated crypto devices */
63 struct rte_cryptodev_global {
64         struct rte_cryptodev *devs;     /**< Device information array */
65         struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
66         /**< Device private data */
67         uint8_t nb_devs;                /**< Number of devices found */
68         uint8_t max_devs;               /**< Max number of devices */
69 };
70
71 /* Cryptodev driver, containing the driver ID */
72 struct cryptodev_driver {
73         TAILQ_ENTRY(cryptodev_driver) next; /**< Next in list. */
74         const struct rte_driver *driver;
75         uint8_t id;
76 };
77
78 /** pointer to global crypto devices data structure. */
79 extern struct rte_cryptodev_global *rte_cryptodev_globals;
80
81 /**
82  * Get the rte_cryptodev structure device pointer for the device. Assumes a
83  * valid device index.
84  *
85  * @param       dev_id  Device ID value to select the device structure.
86  *
87  * @return
88  *   - The rte_cryptodev structure pointer for the given device ID.
89  */
90 struct rte_cryptodev *
91 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
92
93 /**
94  * Get the rte_cryptodev structure device pointer for the named device.
95  *
96  * @param       name    device name to select the device structure.
97  *
98  * @return
99  *   - The rte_cryptodev structure pointer for the given device ID.
100  */
101 struct rte_cryptodev *
102 rte_cryptodev_pmd_get_named_dev(const char *name);
103
104 /**
105  * Validate if the crypto device index is valid attached crypto device.
106  *
107  * @param       dev_id  Crypto device index.
108  *
109  * @return
110  *   - If the device index is valid (1) or not (0).
111  */
112 unsigned int
113 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id);
114
115 /**
116  * The pool of rte_cryptodev structures.
117  */
118 extern struct rte_cryptodev *rte_cryptodevs;
119
120
121 /**
122  * Definitions of all functions exported by a driver through the
123  * the generic structure of type *crypto_dev_ops* supplied in the
124  * *rte_cryptodev* structure associated with a device.
125  */
126
127 /**
128  *      Function used to configure device.
129  *
130  * @param       dev     Crypto device pointer
131  *              config  Crypto device configurations
132  *
133  * @return      Returns 0 on success
134  */
135 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
136                 struct rte_cryptodev_config *config);
137
138 /**
139  * Function used to start a configured device.
140  *
141  * @param       dev     Crypto device pointer
142  *
143  * @return      Returns 0 on success
144  */
145 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
146
147 /**
148  * Function used to stop a configured device.
149  *
150  * @param       dev     Crypto device pointer
151  */
152 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
153
154 /**
155  * Function used to close a configured device.
156  *
157  * @param       dev     Crypto device pointer
158  * @return
159  * - 0 on success.
160  * - EAGAIN if can't close as device is busy
161  */
162 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
163
164
165 /**
166  * Function used to get statistics of a device.
167  *
168  * @param       dev     Crypto device pointer
169  * @param       stats   Pointer to crypto device stats structure to populate
170  */
171 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
172                                 struct rte_cryptodev_stats *stats);
173
174
175 /**
176  * Function used to reset statistics of a device.
177  *
178  * @param       dev     Crypto device pointer
179  */
180 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
181
182
183 /**
184  * Function used to get specific information of a device.
185  *
186  * @param       dev     Crypto device pointer
187  */
188 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
189                                 struct rte_cryptodev_info *dev_info);
190
191 /**
192  * Start queue pair of a device.
193  *
194  * @param       dev     Crypto device pointer
195  * @param       qp_id   Queue Pair Index
196  *
197  * @return      Returns 0 on success.
198  */
199 typedef int (*cryptodev_queue_pair_start_t)(struct rte_cryptodev *dev,
200                                 uint16_t qp_id);
201
202 /**
203  * Stop queue pair of a device.
204  *
205  * @param       dev     Crypto device pointer
206  * @param       qp_id   Queue Pair Index
207  *
208  * @return      Returns 0 on success.
209  */
210 typedef int (*cryptodev_queue_pair_stop_t)(struct rte_cryptodev *dev,
211                                 uint16_t qp_id);
212
213 /**
214  * Setup a queue pair for a device.
215  *
216  * @param       dev             Crypto device pointer
217  * @param       qp_id           Queue Pair Index
218  * @param       qp_conf         Queue configuration structure
219  * @param       socket_id       Socket Index
220  * @param       session_pool    Pointer to device session mempool
221  *
222  * @return      Returns 0 on success.
223  */
224 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
225                 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
226                 int socket_id, struct rte_mempool *session_pool);
227
228 /**
229  * Release memory resources allocated by given queue pair.
230  *
231  * @param       dev     Crypto device pointer
232  * @param       qp_id   Queue Pair Index
233  *
234  * @return
235  * - 0 on success.
236  * - EAGAIN if can't close as device is busy
237  */
238 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
239                 uint16_t qp_id);
240
241 /**
242  * Get number of available queue pairs of a device.
243  *
244  * @param       dev     Crypto device pointer
245  *
246  * @return      Returns number of queue pairs on success.
247  */
248 typedef uint32_t (*cryptodev_queue_pair_count_t)(struct rte_cryptodev *dev);
249
250 /**
251  * Create a session mempool to allocate sessions from
252  *
253  * @param       dev             Crypto device pointer
254  * @param       nb_objs         number of sessions objects in mempool
255  * @param       obj_cache       l-core object cache size, see *rte_ring_create*
256  * @param       socket_id       Socket Id to allocate  mempool on.
257  *
258  * @return
259  * - On success returns a pointer to a rte_mempool
260  * - On failure returns a NULL pointer
261  */
262 typedef int (*cryptodev_sym_create_session_pool_t)(
263                 struct rte_cryptodev *dev, unsigned nb_objs,
264                 unsigned obj_cache_size, int socket_id);
265
266
267 /**
268  * Get the size of a cryptodev session
269  *
270  * @param       dev             Crypto device pointer
271  *
272  * @return
273  *  - On success returns the size of the session structure for device
274  *  - On failure returns 0
275  */
276 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
277                 struct rte_cryptodev *dev);
278
279 /**
280  * Configure a Crypto 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_sym_configure_session_t)(struct rte_cryptodev *dev,
294                 struct rte_crypto_sym_xform *xform,
295                 struct rte_cryptodev_sym_session *session,
296                 struct rte_mempool *mp);
297
298 /**
299  * Free driver private session data.
300  *
301  * @param       dev             Crypto device pointer
302  * @param       sess            Cryptodev session structure
303  */
304 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
305                 struct rte_cryptodev_sym_session *sess);
306
307 /**
308  * Optional API for drivers to attach sessions with queue pair.
309  * @param       dev             Crypto device pointer
310  * @param       qp_id           queue pair id for attaching session
311  * @param       priv_sess       Pointer to cryptodev's private session structure
312  * @return
313  *  - Return 0 on success
314  */
315 typedef int (*cryptodev_sym_queue_pair_attach_session_t)(
316                   struct rte_cryptodev *dev,
317                   uint16_t qp_id,
318                   void *session_private);
319
320 /**
321  * Optional API for drivers to detach sessions from queue pair.
322  * @param       dev             Crypto device pointer
323  * @param       qp_id           queue pair id for detaching session
324  * @param       priv_sess       Pointer to cryptodev's private session structure
325  * @return
326  *  - Return 0 on success
327  */
328 typedef int (*cryptodev_sym_queue_pair_detach_session_t)(
329                   struct rte_cryptodev *dev,
330                   uint16_t qp_id,
331                   void *session_private);
332
333 /** Crypto device operations function pointer table */
334 struct rte_cryptodev_ops {
335         cryptodev_configure_t dev_configure;    /**< Configure device. */
336         cryptodev_start_t dev_start;            /**< Start device. */
337         cryptodev_stop_t dev_stop;              /**< Stop device. */
338         cryptodev_close_t dev_close;            /**< Close device. */
339
340         cryptodev_info_get_t dev_infos_get;     /**< Get device info. */
341
342         cryptodev_stats_get_t stats_get;
343         /**< Get device statistics. */
344         cryptodev_stats_reset_t stats_reset;
345         /**< Reset device statistics. */
346
347         cryptodev_queue_pair_setup_t queue_pair_setup;
348         /**< Set up a device queue pair. */
349         cryptodev_queue_pair_release_t queue_pair_release;
350         /**< Release a queue pair. */
351         cryptodev_queue_pair_start_t queue_pair_start;
352         /**< Start a queue pair. */
353         cryptodev_queue_pair_stop_t queue_pair_stop;
354         /**< Stop a queue pair. */
355         cryptodev_queue_pair_count_t queue_pair_count;
356         /**< Get count of the queue pairs. */
357
358         cryptodev_sym_get_session_private_size_t session_get_size;
359         /**< Return private session. */
360         cryptodev_sym_configure_session_t session_configure;
361         /**< Configure a Crypto session. */
362         cryptodev_sym_free_session_t session_clear;
363         /**< Clear a Crypto sessions private data. */
364         cryptodev_sym_queue_pair_attach_session_t qp_attach_session;
365         /**< Attach session to queue pair. */
366         cryptodev_sym_queue_pair_detach_session_t qp_detach_session;
367         /**< Detach session from queue pair. */
368 };
369
370
371 /**
372  * Function for internal use by dummy drivers primarily, e.g. ring-based
373  * driver.
374  * Allocates a new cryptodev slot for an crypto device and returns the pointer
375  * to that slot for the driver to use.
376  *
377  * @param       name            Unique identifier name for each device
378  * @param       socket_id       Socket to allocate resources on.
379  * @return
380  *   - Slot in the rte_dev_devices array for a new device;
381  */
382 struct rte_cryptodev *
383 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
384
385 /**
386  * Function for internal use by dummy drivers primarily, e.g. ring-based
387  * driver.
388  * Release the specified cryptodev device.
389  *
390  * @param cryptodev
391  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
392  * @return
393  *   - 0 on success, negative on error
394  */
395 extern int
396 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
397
398
399 /**
400  * @internal
401  *
402  * PMD assist function to parse initialisation arguments for crypto driver
403  * when creating a new crypto PMD device instance.
404  *
405  * PMD driver should set default values for that PMD before calling function,
406  * these default values will be over-written with successfully parsed values
407  * from args string.
408  *
409  * @param       params  parsed PMD initialisation parameters
410  * @param       args    input argument string to parse
411  *
412  * @return
413  *  - 0 on success
414  *  - errno on failure
415  */
416 int
417 rte_cryptodev_pmd_parse_input_args(
418                 struct rte_cryptodev_pmd_init_params *params,
419                 const char *args);
420
421 /**
422  * @internal
423  *
424  * PMD assist function to provide boiler plate code for crypto driver to create
425  * and allocate resources for a new crypto PMD device instance.
426  *
427  * @param       name    crypto device name.
428  * @param       device  base device instance
429  * @param       params  PMD initialisation parameters
430  *
431  * @return
432  *  - crypto device instance on success
433  *  - NULL on creation failure
434  */
435 struct rte_cryptodev *
436 rte_cryptodev_pmd_create(const char *name,
437                 struct rte_device *device,
438                 struct rte_cryptodev_pmd_init_params *params);
439
440 /**
441  * @internal
442  *
443  * PMD assist function to provide boiler plate code for crypto driver to
444  * destroy and free resources associated with a crypto PMD device instance.
445  *
446  * @param       cryptodev       crypto device handle.
447  *
448  * @return
449  *  - 0 on success
450  *  - errno on failure
451  */
452 int
453 rte_cryptodev_pmd_destroy(struct rte_cryptodev *cryptodev);
454
455 /**
456  * Executes all the user application registered callbacks for the specific
457  * device.
458  *  *
459  * @param       dev     Pointer to cryptodev struct
460  * @param       event   Crypto device interrupt event type.
461  *
462  * @return
463  *  void
464  */
465 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
466                                 enum rte_cryptodev_event_type event);
467
468 /**
469  * @internal
470  * Create unique device name
471  */
472 int
473 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
474
475 /**
476  * @internal
477  * Allocate Cryptodev driver.
478  *
479  * @param crypto_drv
480  *   Pointer to cryptodev_driver.
481  * @param drv
482  *   Pointer to rte_driver.
483  *
484  * @return
485  *  The driver type identifier
486  */
487 uint8_t rte_cryptodev_allocate_driver(struct cryptodev_driver *crypto_drv,
488                 const struct rte_driver *drv);
489
490
491 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(crypto_drv, drv, driver_id)\
492 RTE_INIT(init_ ##driver_id);\
493 static void init_ ##driver_id(void)\
494 {\
495         driver_id = rte_cryptodev_allocate_driver(&crypto_drv, &(drv));\
496 }
497
498 static inline void *
499 get_session_private_data(const struct rte_cryptodev_sym_session *sess,
500                 uint8_t driver_id) {
501         return sess->sess_private_data[driver_id];
502 }
503
504 static inline void
505 set_session_private_data(struct rte_cryptodev_sym_session *sess,
506                 uint8_t driver_id, void *private_data)
507 {
508         sess->sess_private_data[driver_id] = private_data;
509 }
510
511 #ifdef __cplusplus
512 }
513 #endif
514
515 #endif /* _RTE_CRYPTODEV_PMD_H_ */