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