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