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