17ef37c70db263f880d7b60547cdbe6ecdd91dee
[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 struct rte_cryptodev *
156 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
157
158 /**
159  * Get the rte_cryptodev structure device pointer for the named device.
160  *
161  * @param       name    device name to select the device structure.
162  *
163  * @return
164  *   - The rte_cryptodev structure pointer for the given device ID.
165  */
166 struct rte_cryptodev *
167 rte_cryptodev_pmd_get_named_dev(const char *name);
168
169 /**
170  * Validate if the crypto device index is valid attached crypto device.
171  *
172  * @param       dev_id  Crypto device index.
173  *
174  * @return
175  *   - If the device index is valid (1) or not (0).
176  */
177 unsigned int
178 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id);
179
180 /**
181  * The pool of rte_cryptodev structures.
182  */
183 extern struct rte_cryptodev *rte_cryptodevs;
184
185
186 /**
187  * Definitions of all functions exported by a driver through the
188  * the generic structure of type *crypto_dev_ops* supplied in the
189  * *rte_cryptodev* structure associated with a device.
190  */
191
192 /**
193  *      Function used to configure device.
194  *
195  * @param       dev     Crypto device pointer
196  *              config  Crypto device configurations
197  *
198  * @return      Returns 0 on success
199  */
200 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
201                 struct rte_cryptodev_config *config);
202
203 /**
204  * Function used to start a configured device.
205  *
206  * @param       dev     Crypto device pointer
207  *
208  * @return      Returns 0 on success
209  */
210 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
211
212 /**
213  * Function used to stop a configured device.
214  *
215  * @param       dev     Crypto device pointer
216  */
217 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
218
219 /**
220  * Function used to close a configured device.
221  *
222  * @param       dev     Crypto device pointer
223  * @return
224  * - 0 on success.
225  * - EAGAIN if can't close as device is busy
226  */
227 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
228
229
230 /**
231  * Function used to get statistics of a device.
232  *
233  * @param       dev     Crypto device pointer
234  * @param       stats   Pointer to crypto device stats structure to populate
235  */
236 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
237                                 struct rte_cryptodev_stats *stats);
238
239
240 /**
241  * Function used to reset statistics of a device.
242  *
243  * @param       dev     Crypto device pointer
244  */
245 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
246
247
248 /**
249  * Function used to get specific information of a device.
250  *
251  * @param       dev     Crypto device pointer
252  */
253 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
254                                 struct rte_cryptodev_info *dev_info);
255
256 /**
257  * Start queue pair of a device.
258  *
259  * @param       dev     Crypto device pointer
260  * @param       qp_id   Queue Pair Index
261  *
262  * @return      Returns 0 on success.
263  */
264 typedef int (*cryptodev_queue_pair_start_t)(struct rte_cryptodev *dev,
265                                 uint16_t qp_id);
266
267 /**
268  * Stop queue pair of a device.
269  *
270  * @param       dev     Crypto device pointer
271  * @param       qp_id   Queue Pair Index
272  *
273  * @return      Returns 0 on success.
274  */
275 typedef int (*cryptodev_queue_pair_stop_t)(struct rte_cryptodev *dev,
276                                 uint16_t qp_id);
277
278 /**
279  * Setup a queue pair for a device.
280  *
281  * @param       dev             Crypto device pointer
282  * @param       qp_id           Queue Pair Index
283  * @param       qp_conf         Queue configuration structure
284  * @param       socket_id       Socket Index
285  *
286  * @return      Returns 0 on success.
287  */
288 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
289                 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
290                 int socket_id);
291
292 /**
293  * Release memory resources allocated by given queue pair.
294  *
295  * @param       dev     Crypto device pointer
296  * @param       qp_id   Queue Pair Index
297  *
298  * @return
299  * - 0 on success.
300  * - EAGAIN if can't close as device is busy
301  */
302 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
303                 uint16_t qp_id);
304
305 /**
306  * Get number of available queue pairs of a device.
307  *
308  * @param       dev     Crypto device pointer
309  *
310  * @return      Returns number of queue pairs on success.
311  */
312 typedef uint32_t (*cryptodev_queue_pair_count_t)(struct rte_cryptodev *dev);
313
314 /**
315  * Create a session mempool to allocate sessions from
316  *
317  * @param       dev             Crypto device pointer
318  * @param       nb_objs         number of sessions objects in mempool
319  * @param       obj_cache       l-core object cache size, see *rte_ring_create*
320  * @param       socket_id       Socket Id to allocate  mempool on.
321  *
322  * @return
323  * - On success returns a pointer to a rte_mempool
324  * - On failure returns a NULL pointer
325  */
326 typedef int (*cryptodev_sym_create_session_pool_t)(
327                 struct rte_cryptodev *dev, unsigned nb_objs,
328                 unsigned obj_cache_size, int socket_id);
329
330
331 /**
332  * Get the size of a cryptodev session
333  *
334  * @param       dev             Crypto device pointer
335  *
336  * @return
337  *  - On success returns the size of the session structure for device
338  *  - On failure returns 0
339  */
340 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
341                 struct rte_cryptodev *dev);
342
343 /**
344  * Initialize a Crypto session on a device.
345  *
346  * @param       dev             Crypto device pointer
347  * @param       xform           Single or chain of crypto xforms
348  * @param       priv_sess       Pointer to cryptodev's private session structure
349  *
350  * @return
351  *  - Returns private session structure on success.
352  *  - Returns NULL on failure.
353  */
354 typedef void (*cryptodev_sym_initialize_session_t)(struct rte_mempool *mempool,
355                 void *session_private);
356
357 /**
358  * Configure a Crypto session on a device.
359  *
360  * @param       dev             Crypto device pointer
361  * @param       xform           Single or chain of crypto xforms
362  * @param       priv_sess       Pointer to cryptodev's private session structure
363  *
364  * @return
365  *  - Returns private session structure on success.
366  *  - Returns NULL on failure.
367  */
368 typedef void * (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
369                 struct rte_crypto_sym_xform *xform, void *session_private);
370
371 /**
372  * Free Crypto session.
373  * @param       session         Cryptodev session structure to free
374  */
375 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
376                 void *session_private);
377
378 /**
379  * Optional API for drivers to attach sessions with queue pair.
380  * @param       dev             Crypto device pointer
381  * @param       qp_id           queue pair id for attaching session
382  * @param       priv_sess       Pointer to cryptodev's private session structure
383  * @return
384  *  - Return 0 on success
385  */
386 typedef int (*cryptodev_sym_queue_pair_attach_session_t)(
387                   struct rte_cryptodev *dev,
388                   uint16_t qp_id,
389                   void *session_private);
390
391 /**
392  * Optional API for drivers to detach sessions from queue pair.
393  * @param       dev             Crypto device pointer
394  * @param       qp_id           queue pair id for detaching session
395  * @param       priv_sess       Pointer to cryptodev's private session structure
396  * @return
397  *  - Return 0 on success
398  */
399 typedef int (*cryptodev_sym_queue_pair_detach_session_t)(
400                   struct rte_cryptodev *dev,
401                   uint16_t qp_id,
402                   void *session_private);
403
404 /** Crypto device operations function pointer table */
405 struct rte_cryptodev_ops {
406         cryptodev_configure_t dev_configure;    /**< Configure device. */
407         cryptodev_start_t dev_start;            /**< Start device. */
408         cryptodev_stop_t dev_stop;              /**< Stop device. */
409         cryptodev_close_t dev_close;            /**< Close device. */
410
411         cryptodev_info_get_t dev_infos_get;     /**< Get device info. */
412
413         cryptodev_stats_get_t stats_get;
414         /**< Get device statistics. */
415         cryptodev_stats_reset_t stats_reset;
416         /**< Reset device statistics. */
417
418         cryptodev_queue_pair_setup_t queue_pair_setup;
419         /**< Set up a device queue pair. */
420         cryptodev_queue_pair_release_t queue_pair_release;
421         /**< Release a queue pair. */
422         cryptodev_queue_pair_start_t queue_pair_start;
423         /**< Start a queue pair. */
424         cryptodev_queue_pair_stop_t queue_pair_stop;
425         /**< Stop a queue pair. */
426         cryptodev_queue_pair_count_t queue_pair_count;
427         /**< Get count of the queue pairs. */
428
429         cryptodev_sym_get_session_private_size_t session_get_size;
430         /**< Return private session. */
431         cryptodev_sym_initialize_session_t session_initialize;
432         /**< Initialization function for private session data */
433         cryptodev_sym_configure_session_t session_configure;
434         /**< Configure a Crypto session. */
435         cryptodev_sym_free_session_t session_clear;
436         /**< Clear a Crypto sessions private data. */
437         cryptodev_sym_queue_pair_attach_session_t qp_attach_session;
438         /**< Attach session to queue pair. */
439         cryptodev_sym_queue_pair_attach_session_t qp_detach_session;
440         /**< Detach session from queue pair. */
441 };
442
443
444 /**
445  * Function for internal use by dummy drivers primarily, e.g. ring-based
446  * driver.
447  * Allocates a new cryptodev slot for an crypto device and returns the pointer
448  * to that slot for the driver to use.
449  *
450  * @param       name            Unique identifier name for each device
451  * @param       socket_id       Socket to allocate resources on.
452  * @return
453  *   - Slot in the rte_dev_devices array for a new device;
454  */
455 struct rte_cryptodev *
456 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
457
458 /**
459  * Creates a new virtual crypto device and returns the pointer
460  * to that device.
461  *
462  * @param       name                    PMD type name
463  * @param       dev_private_size        Size of crypto PMDs private data
464  * @param       socket_id               Socket to allocate resources on.
465  *
466  * @return
467  *   - Cryptodev pointer if device is successfully created.
468  *   - NULL if device cannot be created.
469  */
470 struct rte_cryptodev *
471 rte_cryptodev_pmd_virtual_dev_init(const char *name, size_t dev_private_size,
472                 int socket_id);
473
474
475 /**
476  * Function for internal use by dummy drivers primarily, e.g. ring-based
477  * driver.
478  * Release the specified cryptodev device.
479  *
480  * @param cryptodev
481  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
482  * @return
483  *   - 0 on success, negative on error
484  */
485 extern int
486 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
487
488 /**
489  * Executes all the user application registered callbacks for the specific
490  * device.
491  *  *
492  * @param       dev     Pointer to cryptodev struct
493  * @param       event   Crypto device interrupt event type.
494  *
495  * @return
496  *  void
497  */
498 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
499                                 enum rte_cryptodev_event_type event);
500
501 /**
502  * Wrapper for use by pci drivers as a .probe function to attach to a crypto
503  * interface.
504  */
505 int rte_cryptodev_pci_probe(struct rte_pci_driver *pci_drv,
506                             struct rte_pci_device *pci_dev);
507
508 /**
509  * Wrapper for use by pci drivers as a .remove function to detach a crypto
510  * interface.
511  */
512 int rte_cryptodev_pci_remove(struct rte_pci_device *pci_dev);
513
514 /**
515  * @internal
516  * Create unique device name
517  */
518 int
519 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
520
521 #ifdef __cplusplus
522 }
523 #endif
524
525 #endif /* _RTE_CRYPTODEV_PMD_H_ */