Imported Upstream version 16.11.2
[deb_dpdk.git] / lib / librte_cryptodev / rte_cryptodev_pmd.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_PMD_H_
33 #define _RTE_CRYPTODEV_PMD_H_
34
35 /** @file
36  * RTE Crypto PMD APIs
37  *
38  * @note
39  * These API are from crypto PMD only and user applications should not call
40  * them directly.
41  */
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <string.h>
48
49 #include <rte_dev.h>
50 #include <rte_pci.h>
51 #include <rte_malloc.h>
52 #include <rte_mbuf.h>
53 #include <rte_mempool.h>
54 #include <rte_log.h>
55 #include <rte_common.h>
56
57 #include "rte_crypto.h"
58 #include "rte_cryptodev.h"
59
60 struct rte_cryptodev_session {
61         RTE_STD_C11
62         struct {
63                 uint8_t dev_id;
64                 enum rte_cryptodev_type type;
65                 struct rte_mempool *mp;
66         } __rte_aligned(8);
67
68         __extension__ char _private[0];
69 };
70
71 struct rte_cryptodev_driver;
72
73 /**
74  * Initialisation function of a crypto driver invoked for each matching
75  * crypto PCI device detected during the PCI probing phase.
76  *
77  * @param       drv     The pointer to the [matching] crypto driver structure
78  *                      supplied by the PMD when it registered itself.
79  * @param       dev     The dev pointer is the address of the *rte_cryptodev*
80  *                      structure associated with the matching device and which
81  *                      has been [automatically] allocated in the
82  *                      *rte_crypto_devices* array.
83  *
84  * @return
85  *   - 0: Success, the device is properly initialised by the driver.
86  *        In particular, the driver MUST have set up the *dev_ops* pointer
87  *        of the *dev* structure.
88  *   - <0: Error code of the device initialisation failure.
89  */
90 typedef int (*cryptodev_init_t)(struct rte_cryptodev_driver *drv,
91                 struct rte_cryptodev *dev);
92
93 /**
94  * Finalisation function of a driver invoked for each matching
95  * PCI device detected during the PCI closing phase.
96  *
97  * @param       drv     The pointer to the [matching] driver structure supplied
98  *                      by the PMD when it registered itself.
99  * @param       dev     The dev pointer is the address of the *rte_cryptodev*
100  *                      structure associated with the matching device and which
101  *                      has been [automatically] allocated in the
102  *                      *rte_crypto_devices* array.
103  *
104  *  * @return
105  *   - 0: Success, the device is properly finalised by the driver.
106  *        In particular, the driver MUST free the *dev_ops* pointer
107  *        of the *dev* structure.
108  *   - <0: Error code of the device initialisation failure.
109  */
110 typedef int (*cryptodev_uninit_t)(const struct rte_cryptodev_driver  *drv,
111                                 struct rte_cryptodev *dev);
112
113 /**
114  * The structure associated with a PMD driver.
115  *
116  * Each driver acts as a PCI driver and is represented by a generic
117  * *crypto_driver* structure that holds:
118  *
119  * - An *rte_pci_driver* structure (which must be the first field).
120  *
121  * - The *cryptodev_init* function invoked for each matching PCI device.
122  *
123  * - The size of the private data to allocate for each matching device.
124  */
125 struct rte_cryptodev_driver {
126         struct rte_pci_driver pci_drv;  /**< The PMD is also a PCI driver. */
127         unsigned dev_private_size;      /**< Size of device private data. */
128
129         cryptodev_init_t cryptodev_init;        /**< Device init function. */
130         cryptodev_uninit_t cryptodev_uninit;    /**< Device uninit function. */
131 };
132
133
134 /** Global structure used for maintaining state of allocated crypto devices */
135 struct rte_cryptodev_global {
136         struct rte_cryptodev *devs;     /**< Device information array */
137         struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
138         /**< Device private data */
139         uint8_t nb_devs;                /**< Number of devices found */
140         uint8_t max_devs;               /**< Max number of devices */
141 };
142
143 /** pointer to global crypto devices data structure. */
144 extern struct rte_cryptodev_global *rte_cryptodev_globals;
145
146 /**
147  * Get the rte_cryptodev structure device pointer for the device. Assumes a
148  * valid device index.
149  *
150  * @param       dev_id  Device ID value to select the device structure.
151  *
152  * @return
153  *   - The rte_cryptodev structure pointer for the given device ID.
154  */
155 static inline struct rte_cryptodev *
156 rte_cryptodev_pmd_get_dev(uint8_t dev_id)
157 {
158         return &rte_cryptodev_globals->devs[dev_id];
159 }
160
161 /**
162  * Get the rte_cryptodev structure device pointer for the named device.
163  *
164  * @param       name    device name to select the device structure.
165  *
166  * @return
167  *   - The rte_cryptodev structure pointer for the given device ID.
168  */
169 static inline struct rte_cryptodev *
170 rte_cryptodev_pmd_get_named_dev(const char *name)
171 {
172         struct rte_cryptodev *dev;
173         unsigned i;
174
175         if (name == NULL)
176                 return NULL;
177
178         for (i = 0; i < rte_cryptodev_globals->max_devs; i++) {
179                 dev = &rte_cryptodev_globals->devs[i];
180
181                 if ((dev->attached == RTE_CRYPTODEV_ATTACHED) &&
182                                 (strcmp(dev->data->name, name) == 0))
183                         return dev;
184         }
185
186         return NULL;
187 }
188
189 /**
190  * Validate if the crypto device index is valid attached crypto device.
191  *
192  * @param       dev_id  Crypto device index.
193  *
194  * @return
195  *   - If the device index is valid (1) or not (0).
196  */
197 static inline unsigned
198 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id)
199 {
200         struct rte_cryptodev *dev = NULL;
201
202         if (dev_id >= rte_cryptodev_globals->nb_devs)
203                 return 0;
204
205         dev = rte_cryptodev_pmd_get_dev(dev_id);
206         if (dev->attached != RTE_CRYPTODEV_ATTACHED)
207                 return 0;
208         else
209                 return 1;
210 }
211
212 /**
213  * The pool of rte_cryptodev structures.
214  */
215 extern struct rte_cryptodev *rte_cryptodevs;
216
217
218 /**
219  * Definitions of all functions exported by a driver through the
220  * the generic structure of type *crypto_dev_ops* supplied in the
221  * *rte_cryptodev* structure associated with a device.
222  */
223
224 /**
225  *      Function used to configure device.
226  *
227  * @param       dev     Crypto device pointer
228  *
229  * @return      Returns 0 on success
230  */
231 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev);
232
233 /**
234  * Function used to start a configured device.
235  *
236  * @param       dev     Crypto device pointer
237  *
238  * @return      Returns 0 on success
239  */
240 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
241
242 /**
243  * Function used to stop a configured device.
244  *
245  * @param       dev     Crypto device pointer
246  */
247 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
248
249 /**
250  * Function used to close a configured device.
251  *
252  * @param       dev     Crypto device pointer
253  * @return
254  * - 0 on success.
255  * - EAGAIN if can't close as device is busy
256  */
257 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
258
259
260 /**
261  * Function used to get statistics of a device.
262  *
263  * @param       dev     Crypto device pointer
264  * @param       stats   Pointer to crypto device stats structure to populate
265  */
266 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
267                                 struct rte_cryptodev_stats *stats);
268
269
270 /**
271  * Function used to reset statistics of a device.
272  *
273  * @param       dev     Crypto device pointer
274  */
275 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
276
277
278 /**
279  * Function used to get specific information of a device.
280  *
281  * @param       dev     Crypto device pointer
282  */
283 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
284                                 struct rte_cryptodev_info *dev_info);
285
286 /**
287  * Start queue pair of a device.
288  *
289  * @param       dev     Crypto device pointer
290  * @param       qp_id   Queue Pair Index
291  *
292  * @return      Returns 0 on success.
293  */
294 typedef int (*cryptodev_queue_pair_start_t)(struct rte_cryptodev *dev,
295                                 uint16_t qp_id);
296
297 /**
298  * Stop queue pair of a device.
299  *
300  * @param       dev     Crypto device pointer
301  * @param       qp_id   Queue Pair Index
302  *
303  * @return      Returns 0 on success.
304  */
305 typedef int (*cryptodev_queue_pair_stop_t)(struct rte_cryptodev *dev,
306                                 uint16_t qp_id);
307
308 /**
309  * Setup a queue pair for a device.
310  *
311  * @param       dev             Crypto device pointer
312  * @param       qp_id           Queue Pair Index
313  * @param       qp_conf         Queue configuration structure
314  * @param       socket_id       Socket Index
315  *
316  * @return      Returns 0 on success.
317  */
318 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
319                 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
320                 int socket_id);
321
322 /**
323  * Release memory resources allocated by given queue pair.
324  *
325  * @param       dev     Crypto device pointer
326  * @param       qp_id   Queue Pair Index
327  *
328  * @return
329  * - 0 on success.
330  * - EAGAIN if can't close as device is busy
331  */
332 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
333                 uint16_t qp_id);
334
335 /**
336  * Get number of available queue pairs of a device.
337  *
338  * @param       dev     Crypto device pointer
339  *
340  * @return      Returns number of queue pairs on success.
341  */
342 typedef uint32_t (*cryptodev_queue_pair_count_t)(struct rte_cryptodev *dev);
343
344 /**
345  * Create a session mempool to allocate sessions from
346  *
347  * @param       dev             Crypto device pointer
348  * @param       nb_objs         number of sessions objects in mempool
349  * @param       obj_cache       l-core object cache size, see *rte_ring_create*
350  * @param       socket_id       Socket Id to allocate  mempool on.
351  *
352  * @return
353  * - On success returns a pointer to a rte_mempool
354  * - On failure returns a NULL pointer
355  */
356 typedef int (*cryptodev_sym_create_session_pool_t)(
357                 struct rte_cryptodev *dev, unsigned nb_objs,
358                 unsigned obj_cache_size, int socket_id);
359
360
361 /**
362  * Get the size of a cryptodev session
363  *
364  * @param       dev             Crypto device pointer
365  *
366  * @return
367  *  - On success returns the size of the session structure for device
368  *  - On failure returns 0
369  */
370 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
371                 struct rte_cryptodev *dev);
372
373 /**
374  * Initialize a Crypto session on a device.
375  *
376  * @param       dev             Crypto device pointer
377  * @param       xform           Single or chain of crypto xforms
378  * @param       priv_sess       Pointer to cryptodev's private session structure
379  *
380  * @return
381  *  - Returns private session structure on success.
382  *  - Returns NULL on failure.
383  */
384 typedef void (*cryptodev_sym_initialize_session_t)(struct rte_mempool *mempool,
385                 void *session_private);
386
387 /**
388  * Configure a Crypto session on a device.
389  *
390  * @param       dev             Crypto device pointer
391  * @param       xform           Single or chain of crypto xforms
392  * @param       priv_sess       Pointer to cryptodev's private session structure
393  *
394  * @return
395  *  - Returns private session structure on success.
396  *  - Returns NULL on failure.
397  */
398 typedef void * (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
399                 struct rte_crypto_sym_xform *xform, void *session_private);
400
401 /**
402  * Free Crypto session.
403  * @param       session         Cryptodev session structure to free
404  */
405 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
406                 void *session_private);
407
408
409 /** Crypto device operations function pointer table */
410 struct rte_cryptodev_ops {
411         cryptodev_configure_t dev_configure;    /**< Configure device. */
412         cryptodev_start_t dev_start;            /**< Start device. */
413         cryptodev_stop_t dev_stop;              /**< Stop device. */
414         cryptodev_close_t dev_close;            /**< Close device. */
415
416         cryptodev_info_get_t dev_infos_get;     /**< Get device info. */
417
418         cryptodev_stats_get_t stats_get;
419         /**< Get device statistics. */
420         cryptodev_stats_reset_t stats_reset;
421         /**< Reset device statistics. */
422
423         cryptodev_queue_pair_setup_t queue_pair_setup;
424         /**< Set up a device queue pair. */
425         cryptodev_queue_pair_release_t queue_pair_release;
426         /**< Release a queue pair. */
427         cryptodev_queue_pair_start_t queue_pair_start;
428         /**< Start a queue pair. */
429         cryptodev_queue_pair_stop_t queue_pair_stop;
430         /**< Stop a queue pair. */
431         cryptodev_queue_pair_count_t queue_pair_count;
432         /**< Get count of the queue pairs. */
433
434         cryptodev_sym_get_session_private_size_t session_get_size;
435         /**< Return private session. */
436         cryptodev_sym_initialize_session_t session_initialize;
437         /**< Initialization function for private session data */
438         cryptodev_sym_configure_session_t session_configure;
439         /**< Configure a Crypto session. */
440         cryptodev_sym_free_session_t session_clear;
441         /**< Clear a Crypto sessions private data. */
442 };
443
444
445 /**
446  * Function for internal use by dummy drivers primarily, e.g. ring-based
447  * driver.
448  * Allocates a new cryptodev slot for an crypto device and returns the pointer
449  * to that slot for the driver to use.
450  *
451  * @param       name            Unique identifier name for each device
452  * @param       socket_id       Socket to allocate resources on.
453  * @return
454  *   - Slot in the rte_dev_devices array for a new device;
455  */
456 struct rte_cryptodev *
457 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
458
459 /**
460  * Creates a new virtual crypto device and returns the pointer
461  * to that device.
462  *
463  * @param       name                    PMD type name
464  * @param       dev_private_size        Size of crypto PMDs private data
465  * @param       socket_id               Socket to allocate resources on.
466  *
467  * @return
468  *   - Cryptodev pointer if device is successfully created.
469  *   - NULL if device cannot be created.
470  */
471 struct rte_cryptodev *
472 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
473                 int socket_id);
474
475
476 /**
477  * Function for internal use by dummy drivers primarily, e.g. ring-based
478  * driver.
479  * Release the specified cryptodev device.
480  *
481  * @param cryptodev
482  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
483  * @return
484  *   - 0 on success, negative on error
485  */
486 extern int
487 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
488
489 /**
490  * Executes all the user application registered callbacks for the specific
491  * device.
492  *  *
493  * @param       dev     Pointer to cryptodev struct
494  * @param       event   Crypto device interrupt event type.
495  *
496  * @return
497  *  void
498  */
499 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
500                                 enum rte_cryptodev_event_type event);
501
502 /**
503  * Wrapper for use by pci drivers as a .probe function to attach to a crypto
504  * interface.
505  */
506 int rte_cryptodev_pci_probe(struct rte_pci_driver *pci_drv,
507                             struct rte_pci_device *pci_dev);
508
509 /**
510  * Wrapper for use by pci drivers as a .remove function to detach a crypto
511  * interface.
512  */
513 int rte_cryptodev_pci_remove(struct rte_pci_device *pci_dev);
514
515 #ifdef __cplusplus
516 }
517 #endif
518
519 #endif /* _RTE_CRYPTODEV_PMD_H_ */