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