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