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