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