cryptodev: add private session size retrieval function
[dpdk.git] / lib / librte_cryptodev / rte_cryptodev.h
1 /*-
2  *
3  *   Copyright(c) 2015-2017 Intel Corporation. All rights reserved.
4  *
5  *   Redistribution and use in source and binary forms, with or without
6  *   modification, are permitted provided that the following conditions
7  *   are met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above copyright
12  *       notice, this list of conditions and the following disclaimer in
13  *       the documentation and/or other materials provided with the
14  *       distribution.
15  *     * Neither the name of Intel Corporation nor the names of its
16  *       contributors may be used to endorse or promote products derived
17  *       from this software without specific prior written permission.
18  *
19  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #ifndef _RTE_CRYPTODEV_H_
33 #define _RTE_CRYPTODEV_H_
34
35 /**
36  * @file rte_cryptodev.h
37  *
38  * RTE Cryptographic Device APIs
39  *
40  * Defines RTE Crypto Device APIs for the provisioning of cipher and
41  * authentication operations.
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include "rte_kvargs.h"
49 #include "rte_crypto.h"
50 #include "rte_dev.h"
51 #include <rte_common.h>
52 #include <rte_vdev.h>
53
54 extern const char **rte_cyptodev_names;
55
56 /* Logging Macros */
57
58 #define CDEV_LOG_ERR(...) \
59         RTE_LOG(ERR, CRYPTODEV, \
60                 RTE_FMT("%s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
61                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
62
63 #define CDEV_PMD_LOG_ERR(dev, ...) \
64         RTE_LOG(ERR, CRYPTODEV, \
65                 RTE_FMT("[%s] %s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
66                         dev, __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
67
68 #ifdef RTE_LIBRTE_CRYPTODEV_DEBUG
69 #define CDEV_LOG_DEBUG(...) \
70         RTE_LOG(DEBUG, CRYPTODEV, \
71                 RTE_FMT("%s() line %u: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
72                         __func__, __LINE__, RTE_FMT_TAIL(__VA_ARGS__,)))
73
74 #define CDEV_PMD_TRACE(...) \
75         RTE_LOG(DEBUG, CRYPTODEV, \
76                 RTE_FMT("[%s] %s: " RTE_FMT_HEAD(__VA_ARGS__,) "\n", \
77                         dev, __func__, RTE_FMT_TAIL(__VA_ARGS__,)))
78
79 #else
80 #define CDEV_LOG_DEBUG(...) (void)0
81 #define CDEV_PMD_TRACE(...) (void)0
82 #endif
83
84
85
86 /**
87  * A macro that points to an offset from the start
88  * of the crypto operation structure (rte_crypto_op)
89  *
90  * The returned pointer is cast to type t.
91  *
92  * @param c
93  *   The crypto operation.
94  * @param o
95  *   The offset from the start of the crypto operation.
96  * @param t
97  *   The type to cast the result into.
98  */
99 #define rte_crypto_op_ctod_offset(c, t, o)      \
100         ((t)((char *)(c) + (o)))
101
102 /**
103  * A macro that returns the physical address that points
104  * to an offset from the start of the crypto operation
105  * (rte_crypto_op)
106  *
107  * @param c
108  *   The crypto operation.
109  * @param o
110  *   The offset from the start of the crypto operation
111  *   to calculate address from.
112  */
113 #define rte_crypto_op_ctophys_offset(c, o)      \
114         (phys_addr_t)((c)->phys_addr + (o))
115
116 /**
117  * Crypto parameters range description
118  */
119 struct rte_crypto_param_range {
120         uint16_t min;   /**< minimum size */
121         uint16_t max;   /**< maximum size */
122         uint16_t increment;
123         /**< if a range of sizes are supported,
124          * this parameter is used to indicate
125          * increments in byte size that are supported
126          * between the minimum and maximum
127          */
128 };
129
130 /**
131  * Symmetric Crypto Capability
132  */
133 struct rte_cryptodev_symmetric_capability {
134         enum rte_crypto_sym_xform_type xform_type;
135         /**< Transform type : Authentication / Cipher / AEAD */
136         RTE_STD_C11
137         union {
138                 struct {
139                         enum rte_crypto_auth_algorithm algo;
140                         /**< authentication algorithm */
141                         uint16_t block_size;
142                         /**< algorithm block size */
143                         struct rte_crypto_param_range key_size;
144                         /**< auth key size range */
145                         struct rte_crypto_param_range digest_size;
146                         /**< digest size range */
147                         struct rte_crypto_param_range aad_size;
148                         /**< Additional authentication data size range */
149                         struct rte_crypto_param_range iv_size;
150                         /**< Initialisation vector data size range */
151                 } auth;
152                 /**< Symmetric Authentication transform capabilities */
153                 struct {
154                         enum rte_crypto_cipher_algorithm algo;
155                         /**< cipher algorithm */
156                         uint16_t block_size;
157                         /**< algorithm block size */
158                         struct rte_crypto_param_range key_size;
159                         /**< cipher key size range */
160                         struct rte_crypto_param_range iv_size;
161                         /**< Initialisation vector data size range */
162                 } cipher;
163                 /**< Symmetric Cipher transform capabilities */
164                 struct {
165                         enum rte_crypto_aead_algorithm algo;
166                         /**< AEAD algorithm */
167                         uint16_t block_size;
168                         /**< algorithm block size */
169                         struct rte_crypto_param_range key_size;
170                         /**< AEAD key size range */
171                         struct rte_crypto_param_range digest_size;
172                         /**< digest size range */
173                         struct rte_crypto_param_range aad_size;
174                         /**< Additional authentication data size range */
175                         struct rte_crypto_param_range iv_size;
176                         /**< Initialisation vector data size range */
177                 } aead;
178         };
179 };
180
181 /** Structure used to capture a capability of a crypto device */
182 struct rte_cryptodev_capabilities {
183         enum rte_crypto_op_type op;
184         /**< Operation type */
185
186         RTE_STD_C11
187         union {
188                 struct rte_cryptodev_symmetric_capability sym;
189                 /**< Symmetric operation capability parameters */
190         };
191 };
192
193 /** Structure used to describe crypto algorithms */
194 struct rte_cryptodev_sym_capability_idx {
195         enum rte_crypto_sym_xform_type type;
196         union {
197                 enum rte_crypto_cipher_algorithm cipher;
198                 enum rte_crypto_auth_algorithm auth;
199                 enum rte_crypto_aead_algorithm aead;
200         } algo;
201 };
202
203 /**
204  *  Provide capabilities available for defined device and algorithm
205  *
206  * @param       dev_id          The identifier of the device.
207  * @param       idx             Description of crypto algorithms.
208  *
209  * @return
210  *   - Return description of the symmetric crypto capability if exist.
211  *   - Return NULL if the capability not exist.
212  */
213 const struct rte_cryptodev_symmetric_capability *
214 rte_cryptodev_sym_capability_get(uint8_t dev_id,
215                 const struct rte_cryptodev_sym_capability_idx *idx);
216
217 /**
218  * Check if key size and initial vector are supported
219  * in crypto cipher capability
220  *
221  * @param       capability      Description of the symmetric crypto capability.
222  * @param       key_size        Cipher key size.
223  * @param       iv_size         Cipher initial vector size.
224  *
225  * @return
226  *   - Return 0 if the parameters are in range of the capability.
227  *   - Return -1 if the parameters are out of range of the capability.
228  */
229 int
230 rte_cryptodev_sym_capability_check_cipher(
231                 const struct rte_cryptodev_symmetric_capability *capability,
232                 uint16_t key_size, uint16_t iv_size);
233
234 /**
235  * Check if key size and initial vector are supported
236  * in crypto auth capability
237  *
238  * @param       capability      Description of the symmetric crypto capability.
239  * @param       key_size        Auth key size.
240  * @param       digest_size     Auth digest size.
241  * @param       aad_size        Auth aad size.
242  * @param       iv_size         Auth initial vector size.
243  *
244  * @return
245  *   - Return 0 if the parameters are in range of the capability.
246  *   - Return -1 if the parameters are out of range of the capability.
247  */
248 int
249 rte_cryptodev_sym_capability_check_auth(
250                 const struct rte_cryptodev_symmetric_capability *capability,
251                 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
252                 uint16_t iv_size);
253
254 /**
255  * Check if key, digest, AAD and initial vector sizes are supported
256  * in crypto AEAD capability
257  *
258  * @param       capability      Description of the symmetric crypto capability.
259  * @param       key_size        AEAD key size.
260  * @param       digest_size     AEAD digest size.
261  * @param       aad_size        AEAD AAD size.
262  * @param       iv_size         AEAD IV size.
263  *
264  * @return
265  *   - Return 0 if the parameters are in range of the capability.
266  *   - Return -1 if the parameters are out of range of the capability.
267  */
268 int
269 rte_cryptodev_sym_capability_check_aead(
270                 const struct rte_cryptodev_symmetric_capability *capability,
271                 uint16_t key_size, uint16_t digest_size, uint16_t aad_size,
272                 uint16_t iv_size);
273
274 /**
275  * Provide the cipher algorithm enum, given an algorithm string
276  *
277  * @param       algo_enum       A pointer to the cipher algorithm
278  *                              enum to be filled
279  * @param       algo_string     Authentication algo string
280  *
281  * @return
282  * - Return -1 if string is not valid
283  * - Return 0 is the string is valid
284  */
285 int
286 rte_cryptodev_get_cipher_algo_enum(enum rte_crypto_cipher_algorithm *algo_enum,
287                 const char *algo_string);
288
289 /**
290  * Provide the authentication algorithm enum, given an algorithm string
291  *
292  * @param       algo_enum       A pointer to the authentication algorithm
293  *                              enum to be filled
294  * @param       algo_string     Authentication algo string
295  *
296  * @return
297  * - Return -1 if string is not valid
298  * - Return 0 is the string is valid
299  */
300 int
301 rte_cryptodev_get_auth_algo_enum(enum rte_crypto_auth_algorithm *algo_enum,
302                 const char *algo_string);
303
304 /**
305  * Provide the AEAD algorithm enum, given an algorithm string
306  *
307  * @param       algo_enum       A pointer to the AEAD algorithm
308  *                              enum to be filled
309  * @param       algo_string     AEAD algorithm string
310  *
311  * @return
312  * - Return -1 if string is not valid
313  * - Return 0 is the string is valid
314  */
315 int
316 rte_cryptodev_get_aead_algo_enum(enum rte_crypto_aead_algorithm *algo_enum,
317                 const char *algo_string);
318
319 /** Macro used at end of crypto PMD list */
320 #define RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST() \
321         { RTE_CRYPTO_OP_TYPE_UNDEFINED }
322
323
324 /**
325  * Crypto device supported feature flags
326  *
327  * Note:
328  * New features flags should be added to the end of the list
329  *
330  * Keep these flags synchronised with rte_cryptodev_get_feature_name()
331  */
332 #define RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO       (1ULL << 0)
333 /**< Symmetric crypto operations are supported */
334 #define RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO      (1ULL << 1)
335 /**< Asymmetric crypto operations are supported */
336 #define RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING (1ULL << 2)
337 /**< Chaining symmetric crypto operations are supported */
338 #define RTE_CRYPTODEV_FF_CPU_SSE                (1ULL << 3)
339 /**< Utilises CPU SIMD SSE instructions */
340 #define RTE_CRYPTODEV_FF_CPU_AVX                (1ULL << 4)
341 /**< Utilises CPU SIMD AVX instructions */
342 #define RTE_CRYPTODEV_FF_CPU_AVX2               (1ULL << 5)
343 /**< Utilises CPU SIMD AVX2 instructions */
344 #define RTE_CRYPTODEV_FF_CPU_AESNI              (1ULL << 6)
345 /**< Utilises CPU AES-NI instructions */
346 #define RTE_CRYPTODEV_FF_HW_ACCELERATED         (1ULL << 7)
347 /**< Operations are off-loaded to an external hardware accelerator */
348 #define RTE_CRYPTODEV_FF_CPU_AVX512             (1ULL << 8)
349 /**< Utilises CPU SIMD AVX512 instructions */
350 #define RTE_CRYPTODEV_FF_MBUF_SCATTER_GATHER    (1ULL << 9)
351 /**< Scatter-gather mbufs are supported */
352 #define RTE_CRYPTODEV_FF_CPU_NEON               (1ULL << 10)
353 /**< Utilises CPU NEON instructions */
354 #define RTE_CRYPTODEV_FF_CPU_ARM_CE             (1ULL << 11)
355 /**< Utilises ARM CPU Cryptographic Extensions */
356
357
358 /**
359  * Get the name of a crypto device feature flag
360  *
361  * @param       flag    The mask describing the flag.
362  *
363  * @return
364  *   The name of this flag, or NULL if it's not a valid feature flag.
365  */
366
367 extern const char *
368 rte_cryptodev_get_feature_name(uint64_t flag);
369
370 /**  Crypto device information */
371 struct rte_cryptodev_info {
372         const char *driver_name;                /**< Driver name. */
373         uint8_t driver_id;                      /**< Driver identifier */
374         struct rte_pci_device *pci_dev;         /**< PCI information. */
375
376         uint64_t feature_flags;                 /**< Feature flags */
377
378         const struct rte_cryptodev_capabilities *capabilities;
379         /**< Array of devices supported capabilities */
380
381         unsigned max_nb_queue_pairs;
382         /**< Maximum number of queues pairs supported by device. */
383
384         struct {
385                 unsigned max_nb_sessions;
386                 /**< Maximum number of sessions supported by device. */
387                 unsigned int max_nb_sessions_per_qp;
388                 /**< Maximum number of sessions per queue pair.
389                  * Default 0 for infinite sessions
390                  */
391         } sym;
392 };
393
394 #define RTE_CRYPTODEV_DETACHED  (0)
395 #define RTE_CRYPTODEV_ATTACHED  (1)
396
397 /** Definitions of Crypto device event types */
398 enum rte_cryptodev_event_type {
399         RTE_CRYPTODEV_EVENT_UNKNOWN,    /**< unknown event type */
400         RTE_CRYPTODEV_EVENT_ERROR,      /**< error interrupt event */
401         RTE_CRYPTODEV_EVENT_MAX         /**< max value of this enum */
402 };
403
404 /** Crypto device queue pair configuration structure. */
405 struct rte_cryptodev_qp_conf {
406         uint32_t nb_descriptors; /**< Number of descriptors per queue pair */
407 };
408
409 /**
410  * Typedef for application callback function to be registered by application
411  * software for notification of device events
412  *
413  * @param       dev_id  Crypto device identifier
414  * @param       event   Crypto device event to register for notification of.
415  * @param       cb_arg  User specified parameter to be passed as to passed to
416  *                      users callback function.
417  */
418 typedef void (*rte_cryptodev_cb_fn)(uint8_t dev_id,
419                 enum rte_cryptodev_event_type event, void *cb_arg);
420
421
422 /** Crypto Device statistics */
423 struct rte_cryptodev_stats {
424         uint64_t enqueued_count;
425         /**< Count of all operations enqueued */
426         uint64_t dequeued_count;
427         /**< Count of all operations dequeued */
428
429         uint64_t enqueue_err_count;
430         /**< Total error count on operations enqueued */
431         uint64_t dequeue_err_count;
432         /**< Total error count on operations dequeued */
433 };
434
435 #define RTE_CRYPTODEV_NAME_MAX_LEN      (64)
436 /**< Max length of name of crypto PMD */
437
438 /**
439  * Create a virtual crypto device
440  *
441  * @param       name    Cryptodev PMD name of device to be created.
442  * @param       args    Options arguments for device.
443  *
444  * @return
445  * - On successful creation of the cryptodev the device index is returned,
446  *   which will be between 0 and rte_cryptodev_count().
447  * - In the case of a failure, returns -1.
448  */
449 extern int
450 rte_cryptodev_create_vdev(const char *name, const char *args);
451
452 /**
453  * Get the device identifier for the named crypto device.
454  *
455  * @param       name    device name to select the device structure.
456  *
457  * @return
458  *   - Returns crypto device identifier on success.
459  *   - Return -1 on failure to find named crypto device.
460  */
461 extern int
462 rte_cryptodev_get_dev_id(const char *name);
463
464 /**
465  * Get the total number of crypto devices that have been successfully
466  * initialised.
467  *
468  * @return
469  *   - The total number of usable crypto devices.
470  */
471 extern uint8_t
472 rte_cryptodev_count(void);
473
474 /**
475  * Get number of crypto device defined type.
476  *
477  * @param       driver_id       driver identifier.
478  *
479  * @return
480  *   Returns number of crypto device.
481  */
482 extern uint8_t
483 rte_cryptodev_device_count_by_driver(uint8_t driver_id);
484
485 /**
486  * Get number and identifiers of attached crypto devices that
487  * use the same crypto driver.
488  *
489  * @param       driver_name     driver name.
490  * @param       devices         output devices identifiers.
491  * @param       nb_devices      maximal number of devices.
492  *
493  * @return
494  *   Returns number of attached crypto device.
495  */
496 uint8_t
497 rte_cryptodev_devices_get(const char *driver_name, uint8_t *devices,
498                 uint8_t nb_devices);
499 /*
500  * Return the NUMA socket to which a device is connected
501  *
502  * @param dev_id
503  *   The identifier of the device
504  * @return
505  *   The NUMA socket id to which the device is connected or
506  *   a default of zero if the socket could not be determined.
507  *   -1 if returned is the dev_id value is out of range.
508  */
509 extern int
510 rte_cryptodev_socket_id(uint8_t dev_id);
511
512 /** Crypto device configuration structure */
513 struct rte_cryptodev_config {
514         int socket_id;                  /**< Socket to allocate resources on */
515         uint16_t nb_queue_pairs;
516         /**< Number of queue pairs to configure on device */
517
518         struct {
519                 uint32_t nb_objs;       /**< Number of objects in mempool */
520                 uint32_t cache_size;    /**< l-core object cache size */
521         } session_mp;           /**< Session mempool configuration */
522 };
523
524 /**
525  * Configure a device.
526  *
527  * This function must be invoked first before any other function in the
528  * API. This function can also be re-invoked when a device is in the
529  * stopped state.
530  *
531  * @param       dev_id          The identifier of the device to configure.
532  * @param       config          The crypto device configuration structure.
533  *
534  * @return
535  *   - 0: Success, device configured.
536  *   - <0: Error code returned by the driver configuration function.
537  */
538 extern int
539 rte_cryptodev_configure(uint8_t dev_id, struct rte_cryptodev_config *config);
540
541 /**
542  * Start an device.
543  *
544  * The device start step is the last one and consists of setting the configured
545  * offload features and in starting the transmit and the receive units of the
546  * device.
547  * On success, all basic functions exported by the API (link status,
548  * receive/transmit, and so on) can be invoked.
549  *
550  * @param dev_id
551  *   The identifier of the device.
552  * @return
553  *   - 0: Success, device started.
554  *   - <0: Error code of the driver device start function.
555  */
556 extern int
557 rte_cryptodev_start(uint8_t dev_id);
558
559 /**
560  * Stop an device. The device can be restarted with a call to
561  * rte_cryptodev_start()
562  *
563  * @param       dev_id          The identifier of the device.
564  */
565 extern void
566 rte_cryptodev_stop(uint8_t dev_id);
567
568 /**
569  * Close an device. The device cannot be restarted!
570  *
571  * @param       dev_id          The identifier of the device.
572  *
573  * @return
574  *  - 0 on successfully closing device
575  *  - <0 on failure to close device
576  */
577 extern int
578 rte_cryptodev_close(uint8_t dev_id);
579
580 /**
581  * Allocate and set up a receive queue pair for a device.
582  *
583  *
584  * @param       dev_id          The identifier of the device.
585  * @param       queue_pair_id   The index of the queue pairs to set up. The
586  *                              value must be in the range [0, nb_queue_pair
587  *                              - 1] previously supplied to
588  *                              rte_cryptodev_configure().
589  * @param       qp_conf         The pointer to the configuration data to be
590  *                              used for the queue pair. NULL value is
591  *                              allowed, in which case default configuration
592  *                              will be used.
593  * @param       socket_id       The *socket_id* argument is the socket
594  *                              identifier in case of NUMA. The value can be
595  *                              *SOCKET_ID_ANY* if there is no NUMA constraint
596  *                              for the DMA memory allocated for the receive
597  *                              queue pair.
598  *
599  * @return
600  *   - 0: Success, queue pair correctly set up.
601  *   - <0: Queue pair configuration failed
602  */
603 extern int
604 rte_cryptodev_queue_pair_setup(uint8_t dev_id, uint16_t queue_pair_id,
605                 const struct rte_cryptodev_qp_conf *qp_conf, int socket_id);
606
607 /**
608  * Start a specified queue pair of a device. It is used
609  * when deferred_start flag of the specified queue is true.
610  *
611  * @param       dev_id          The identifier of the device
612  * @param       queue_pair_id   The index of the queue pair to start. The value
613  *                              must be in the range [0, nb_queue_pair - 1]
614  *                              previously supplied to
615  *                              rte_crypto_dev_configure().
616  * @return
617  *   - 0: Success, the transmit queue is correctly set up.
618  *   - -EINVAL: The dev_id or the queue_id out of range.
619  *   - -ENOTSUP: The function not supported in PMD driver.
620  */
621 extern int
622 rte_cryptodev_queue_pair_start(uint8_t dev_id, uint16_t queue_pair_id);
623
624 /**
625  * Stop specified queue pair of a device
626  *
627  * @param       dev_id          The identifier of the device
628  * @param       queue_pair_id   The index of the queue pair to stop. The value
629  *                              must be in the range [0, nb_queue_pair - 1]
630  *                              previously supplied to
631  *                              rte_cryptodev_configure().
632  * @return
633  *   - 0: Success, the transmit queue is correctly set up.
634  *   - -EINVAL: The dev_id or the queue_id out of range.
635  *   - -ENOTSUP: The function not supported in PMD driver.
636  */
637 extern int
638 rte_cryptodev_queue_pair_stop(uint8_t dev_id, uint16_t queue_pair_id);
639
640 /**
641  * Get the number of queue pairs on a specific crypto device
642  *
643  * @param       dev_id          Crypto device identifier.
644  * @return
645  *   - The number of configured queue pairs.
646  */
647 extern uint16_t
648 rte_cryptodev_queue_pair_count(uint8_t dev_id);
649
650
651 /**
652  * Retrieve the general I/O statistics of a device.
653  *
654  * @param       dev_id          The identifier of the device.
655  * @param       stats           A pointer to a structure of type
656  *                              *rte_cryptodev_stats* to be filled with the
657  *                              values of device counters.
658  * @return
659  *   - Zero if successful.
660  *   - Non-zero otherwise.
661  */
662 extern int
663 rte_cryptodev_stats_get(uint8_t dev_id, struct rte_cryptodev_stats *stats);
664
665 /**
666  * Reset the general I/O statistics of a device.
667  *
668  * @param       dev_id          The identifier of the device.
669  */
670 extern void
671 rte_cryptodev_stats_reset(uint8_t dev_id);
672
673 /**
674  * Retrieve the contextual information of a device.
675  *
676  * @param       dev_id          The identifier of the device.
677  * @param       dev_info        A pointer to a structure of type
678  *                              *rte_cryptodev_info* to be filled with the
679  *                              contextual information of the device.
680  */
681 extern void
682 rte_cryptodev_info_get(uint8_t dev_id, struct rte_cryptodev_info *dev_info);
683
684
685 /**
686  * Register a callback function for specific device id.
687  *
688  * @param       dev_id          Device id.
689  * @param       event           Event interested.
690  * @param       cb_fn           User supplied callback function to be called.
691  * @param       cb_arg          Pointer to the parameters for the registered
692  *                              callback.
693  *
694  * @return
695  *  - On success, zero.
696  *  - On failure, a negative value.
697  */
698 extern int
699 rte_cryptodev_callback_register(uint8_t dev_id,
700                 enum rte_cryptodev_event_type event,
701                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
702
703 /**
704  * Unregister a callback function for specific device id.
705  *
706  * @param       dev_id          The device identifier.
707  * @param       event           Event interested.
708  * @param       cb_fn           User supplied callback function to be called.
709  * @param       cb_arg          Pointer to the parameters for the registered
710  *                              callback.
711  *
712  * @return
713  *  - On success, zero.
714  *  - On failure, a negative value.
715  */
716 extern int
717 rte_cryptodev_callback_unregister(uint8_t dev_id,
718                 enum rte_cryptodev_event_type event,
719                 rte_cryptodev_cb_fn cb_fn, void *cb_arg);
720
721
722 typedef uint16_t (*dequeue_pkt_burst_t)(void *qp,
723                 struct rte_crypto_op **ops,     uint16_t nb_ops);
724 /**< Dequeue processed packets from queue pair of a device. */
725
726 typedef uint16_t (*enqueue_pkt_burst_t)(void *qp,
727                 struct rte_crypto_op **ops,     uint16_t nb_ops);
728 /**< Enqueue packets for processing on queue pair of a device. */
729
730
731
732
733 struct rte_cryptodev_callback;
734
735 /** Structure to keep track of registered callbacks */
736 TAILQ_HEAD(rte_cryptodev_cb_list, rte_cryptodev_callback);
737
738 /** The data structure associated with each crypto device. */
739 struct rte_cryptodev {
740         dequeue_pkt_burst_t dequeue_burst;
741         /**< Pointer to PMD receive function. */
742         enqueue_pkt_burst_t enqueue_burst;
743         /**< Pointer to PMD transmit function. */
744
745         struct rte_cryptodev_data *data;
746         /**< Pointer to device data */
747         struct rte_cryptodev_ops *dev_ops;
748         /**< Functions exported by PMD */
749         uint64_t feature_flags;
750         /**< Supported features */
751         struct rte_device *device;
752         /**< Backing device */
753
754         uint8_t driver_id;
755         /**< Crypto driver identifier*/
756
757         struct rte_cryptodev_cb_list link_intr_cbs;
758         /**< User application callback for interrupts if present */
759
760         __extension__
761         uint8_t attached : 1;
762         /**< Flag indicating the device is attached */
763 } __rte_cache_aligned;
764
765 /**
766  *
767  * The data part, with no function pointers, associated with each device.
768  *
769  * This structure is safe to place in shared memory to be common among
770  * different processes in a multi-process configuration.
771  */
772 struct rte_cryptodev_data {
773         uint8_t dev_id;
774         /**< Device ID for this instance */
775         uint8_t socket_id;
776         /**< Socket ID where memory is allocated */
777         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
778         /**< Unique identifier name */
779
780         __extension__
781         uint8_t dev_started : 1;
782         /**< Device state: STARTED(1)/STOPPED(0) */
783
784         struct rte_mempool *session_pool;
785         /**< Session memory pool */
786         void **queue_pairs;
787         /**< Array of pointers to queue pairs. */
788         uint16_t nb_queue_pairs;
789         /**< Number of device queue pairs. */
790
791         void *dev_private;
792         /**< PMD-specific private data */
793 } __rte_cache_aligned;
794
795 extern struct rte_cryptodev *rte_cryptodevs;
796 /**
797  *
798  * Dequeue a burst of processed crypto operations from a queue on the crypto
799  * device. The dequeued operation are stored in *rte_crypto_op* structures
800  * whose pointers are supplied in the *ops* array.
801  *
802  * The rte_cryptodev_dequeue_burst() function returns the number of ops
803  * actually dequeued, which is the number of *rte_crypto_op* data structures
804  * effectively supplied into the *ops* array.
805  *
806  * A return value equal to *nb_ops* indicates that the queue contained
807  * at least *nb_ops* operations, and this is likely to signify that other
808  * processed operations remain in the devices output queue. Applications
809  * implementing a "retrieve as many processed operations as possible" policy
810  * can check this specific case and keep invoking the
811  * rte_cryptodev_dequeue_burst() function until a value less than
812  * *nb_ops* is returned.
813  *
814  * The rte_cryptodev_dequeue_burst() function does not provide any error
815  * notification to avoid the corresponding overhead.
816  *
817  * @param       dev_id          The symmetric crypto device identifier
818  * @param       qp_id           The index of the queue pair from which to
819  *                              retrieve processed packets. The value must be
820  *                              in the range [0, nb_queue_pair - 1] previously
821  *                              supplied to rte_cryptodev_configure().
822  * @param       ops             The address of an array of pointers to
823  *                              *rte_crypto_op* structures that must be
824  *                              large enough to store *nb_ops* pointers in it.
825  * @param       nb_ops          The maximum number of operations to dequeue.
826  *
827  * @return
828  *   - The number of operations actually dequeued, which is the number
829  *   of pointers to *rte_crypto_op* structures effectively supplied to the
830  *   *ops* array.
831  */
832 static inline uint16_t
833 rte_cryptodev_dequeue_burst(uint8_t dev_id, uint16_t qp_id,
834                 struct rte_crypto_op **ops, uint16_t nb_ops)
835 {
836         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
837
838         nb_ops = (*dev->dequeue_burst)
839                         (dev->data->queue_pairs[qp_id], ops, nb_ops);
840
841         return nb_ops;
842 }
843
844 /**
845  * Enqueue a burst of operations for processing on a crypto device.
846  *
847  * The rte_cryptodev_enqueue_burst() function is invoked to place
848  * crypto operations on the queue *qp_id* of the device designated by
849  * its *dev_id*.
850  *
851  * The *nb_ops* parameter is the number of operations to process which are
852  * supplied in the *ops* array of *rte_crypto_op* structures.
853  *
854  * The rte_cryptodev_enqueue_burst() function returns the number of
855  * operations it actually enqueued for processing. A return value equal to
856  * *nb_ops* means that all packets have been enqueued.
857  *
858  * @param       dev_id          The identifier of the device.
859  * @param       qp_id           The index of the queue pair which packets are
860  *                              to be enqueued for processing. The value
861  *                              must be in the range [0, nb_queue_pairs - 1]
862  *                              previously supplied to
863  *                               *rte_cryptodev_configure*.
864  * @param       ops             The address of an array of *nb_ops* pointers
865  *                              to *rte_crypto_op* structures which contain
866  *                              the crypto operations to be processed.
867  * @param       nb_ops          The number of operations to process.
868  *
869  * @return
870  * The number of operations actually enqueued on the crypto device. The return
871  * value can be less than the value of the *nb_ops* parameter when the
872  * crypto devices queue is full or if invalid parameters are specified in
873  * a *rte_crypto_op*.
874  */
875 static inline uint16_t
876 rte_cryptodev_enqueue_burst(uint8_t dev_id, uint16_t qp_id,
877                 struct rte_crypto_op **ops, uint16_t nb_ops)
878 {
879         struct rte_cryptodev *dev = &rte_cryptodevs[dev_id];
880
881         return (*dev->enqueue_burst)(
882                         dev->data->queue_pairs[qp_id], ops, nb_ops);
883 }
884
885
886 /** Cryptodev symmetric crypto session */
887 struct rte_cryptodev_sym_session {
888         RTE_STD_C11
889         struct {
890                 uint8_t dev_id;
891                 /**< Device Id */
892                 uint8_t driver_id;
893                 /** Crypto driver identifier session created on */
894                 struct rte_mempool *mp;
895                 /**< Mempool session allocated from */
896         } __rte_aligned(8);
897         /**< Public symmetric session details */
898
899         __extension__ char _private[0];
900         /**< Private session material */
901 };
902
903
904 /**
905  * Initialise a session for symmetric cryptographic operations.
906  *
907  * This function is used by the client to initialize immutable
908  * parameters of symmetric cryptographic operation.
909  * To perform the operation the rte_cryptodev_enqueue_burst function is
910  * used.  Each mbuf should contain a reference to the session
911  * pointer returned from this function contained within it's crypto_op if a
912  * session-based operation is being provisioned. Memory to contain the session
913  * information is allocated from within mempool managed by the cryptodev.
914  *
915  * The rte_cryptodev_session_free must be called to free allocated
916  * memory when the session is no longer required.
917  *
918  * @param       dev_id          The device identifier.
919  * @param       xform           Crypto transform chain.
920
921  *
922  * @return
923  *  Pointer to the created session or NULL
924  */
925 extern struct rte_cryptodev_sym_session *
926 rte_cryptodev_sym_session_create(uint8_t dev_id,
927                 struct rte_crypto_sym_xform *xform);
928
929 /**
930  * Free the memory associated with a previously allocated session.
931  *
932  * @param       dev_id          The device identifier.
933  * @param       session         Session pointer previously allocated by
934  *                              *rte_cryptodev_sym_session_create*.
935  *
936  * @return
937  *   NULL on successful freeing of session.
938  *   Session pointer on failure to free session.
939  */
940 extern struct rte_cryptodev_sym_session *
941 rte_cryptodev_sym_session_free(uint8_t dev_id,
942                 struct rte_cryptodev_sym_session *session);
943
944 /**
945  * Get the size of the private session data for a device.
946  *
947  * @param       dev_id          The device identifier.
948  *
949  * @return
950  *   - Size of the private data, if successful
951  *   - 0 if device is invalid or does not have private session
952  */
953 unsigned int
954 rte_cryptodev_get_private_session_size(uint8_t dev_id);
955
956 /**
957  * Attach queue pair with sym session.
958  *
959  * @param       qp_id           Queue pair to which session will be attached.
960  * @param       session         Session pointer previously allocated by
961  *                              *rte_cryptodev_sym_session_create*.
962  *
963  * @return
964  *  - On success, zero.
965  *  - On failure, a negative value.
966  */
967 int
968 rte_cryptodev_queue_pair_attach_sym_session(uint16_t qp_id,
969                 struct rte_cryptodev_sym_session *session);
970
971 /**
972  * Detach queue pair with sym session.
973  *
974  * @param       qp_id           Queue pair to which session is attached.
975  * @param       session         Session pointer previously allocated by
976  *                              *rte_cryptodev_sym_session_create*.
977  *
978  * @return
979  *  - On success, zero.
980  *  - On failure, a negative value.
981  */
982 int
983 rte_cryptodev_queue_pair_detach_sym_session(uint16_t qp_id,
984                 struct rte_cryptodev_sym_session *session);
985
986 /**
987  * Provide driver identifier.
988  *
989  * @param name
990  *   The pointer to a driver name.
991  * @return
992  *  The driver type identifier or -1 if no driver found
993  */
994 int rte_cryptodev_driver_id_get(const char *name);
995
996 /**
997  * Provide driver name.
998  *
999  * @param driver_id
1000  *   The driver identifier.
1001  * @return
1002  *  The driver name or null if no driver found
1003  */
1004 const char *rte_cryptodev_driver_name_get(uint8_t driver_id);
1005
1006 /**
1007  * @internal
1008  * Allocate Cryptodev driver.
1009  *
1010  * @param driver
1011  *   Pointer to rte_driver.
1012  * @return
1013  *  The driver type identifier
1014  */
1015 uint8_t rte_cryptodev_allocate_driver(const struct rte_driver *driver);
1016
1017
1018 #define RTE_PMD_REGISTER_CRYPTO_DRIVER(drv, driver_id)\
1019 RTE_INIT(init_ ##driver_id);\
1020 static void init_ ##driver_id(void)\
1021 {\
1022         driver_id = rte_cryptodev_allocate_driver(&(drv).driver);\
1023 }
1024
1025
1026 #ifdef __cplusplus
1027 }
1028 #endif
1029
1030 #endif /* _RTE_CRYPTODEV_H_ */