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