f6aa84dd5e93270cd197be15db01ed75ed561b2c
[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_malloc.h>
51 #include <rte_mbuf.h>
52 #include <rte_mempool.h>
53 #include <rte_log.h>
54 #include <rte_common.h>
55
56 #include "rte_crypto.h"
57 #include "rte_cryptodev.h"
58
59 struct rte_cryptodev_session {
60         RTE_STD_C11
61         struct {
62                 uint8_t dev_id;
63                 uint8_t driver_id;
64                 struct rte_mempool *mp;
65         } __rte_aligned(8);
66
67         __extension__ char _private[0];
68 };
69
70 /** Global structure used for maintaining state of allocated crypto devices */
71 struct rte_cryptodev_global {
72         struct rte_cryptodev *devs;     /**< Device information array */
73         struct rte_cryptodev_data *data[RTE_CRYPTO_MAX_DEVS];
74         /**< Device private data */
75         uint8_t nb_devs;                /**< Number of devices found */
76         uint8_t max_devs;               /**< Max number of devices */
77 };
78
79 /** pointer to global crypto devices data structure. */
80 extern struct rte_cryptodev_global *rte_cryptodev_globals;
81
82 /**
83  * Get the rte_cryptodev structure device pointer for the device. Assumes a
84  * valid device index.
85  *
86  * @param       dev_id  Device ID value to select the device structure.
87  *
88  * @return
89  *   - The rte_cryptodev structure pointer for the given device ID.
90  */
91 struct rte_cryptodev *
92 rte_cryptodev_pmd_get_dev(uint8_t dev_id);
93
94 /**
95  * Get the rte_cryptodev structure device pointer for the named device.
96  *
97  * @param       name    device name to select the device structure.
98  *
99  * @return
100  *   - The rte_cryptodev structure pointer for the given device ID.
101  */
102 struct rte_cryptodev *
103 rte_cryptodev_pmd_get_named_dev(const char *name);
104
105 /**
106  * Validate if the crypto device index is valid attached crypto device.
107  *
108  * @param       dev_id  Crypto device index.
109  *
110  * @return
111  *   - If the device index is valid (1) or not (0).
112  */
113 unsigned int
114 rte_cryptodev_pmd_is_valid_dev(uint8_t dev_id);
115
116 /**
117  * The pool of rte_cryptodev structures.
118  */
119 extern struct rte_cryptodev *rte_cryptodevs;
120
121
122 /**
123  * Definitions of all functions exported by a driver through the
124  * the generic structure of type *crypto_dev_ops* supplied in the
125  * *rte_cryptodev* structure associated with a device.
126  */
127
128 /**
129  *      Function used to configure device.
130  *
131  * @param       dev     Crypto device pointer
132  *              config  Crypto device configurations
133  *
134  * @return      Returns 0 on success
135  */
136 typedef int (*cryptodev_configure_t)(struct rte_cryptodev *dev,
137                 struct rte_cryptodev_config *config);
138
139 /**
140  * Function used to start a configured device.
141  *
142  * @param       dev     Crypto device pointer
143  *
144  * @return      Returns 0 on success
145  */
146 typedef int (*cryptodev_start_t)(struct rte_cryptodev *dev);
147
148 /**
149  * Function used to stop a configured device.
150  *
151  * @param       dev     Crypto device pointer
152  */
153 typedef void (*cryptodev_stop_t)(struct rte_cryptodev *dev);
154
155 /**
156  * Function used to close a configured device.
157  *
158  * @param       dev     Crypto device pointer
159  * @return
160  * - 0 on success.
161  * - EAGAIN if can't close as device is busy
162  */
163 typedef int (*cryptodev_close_t)(struct rte_cryptodev *dev);
164
165
166 /**
167  * Function used to get statistics of a device.
168  *
169  * @param       dev     Crypto device pointer
170  * @param       stats   Pointer to crypto device stats structure to populate
171  */
172 typedef void (*cryptodev_stats_get_t)(struct rte_cryptodev *dev,
173                                 struct rte_cryptodev_stats *stats);
174
175
176 /**
177  * Function used to reset statistics of a device.
178  *
179  * @param       dev     Crypto device pointer
180  */
181 typedef void (*cryptodev_stats_reset_t)(struct rte_cryptodev *dev);
182
183
184 /**
185  * Function used to get specific information of a device.
186  *
187  * @param       dev     Crypto device pointer
188  */
189 typedef void (*cryptodev_info_get_t)(struct rte_cryptodev *dev,
190                                 struct rte_cryptodev_info *dev_info);
191
192 /**
193  * Start queue pair of a device.
194  *
195  * @param       dev     Crypto device pointer
196  * @param       qp_id   Queue Pair Index
197  *
198  * @return      Returns 0 on success.
199  */
200 typedef int (*cryptodev_queue_pair_start_t)(struct rte_cryptodev *dev,
201                                 uint16_t qp_id);
202
203 /**
204  * Stop queue pair of a device.
205  *
206  * @param       dev     Crypto device pointer
207  * @param       qp_id   Queue Pair Index
208  *
209  * @return      Returns 0 on success.
210  */
211 typedef int (*cryptodev_queue_pair_stop_t)(struct rte_cryptodev *dev,
212                                 uint16_t qp_id);
213
214 /**
215  * Setup a queue pair for a device.
216  *
217  * @param       dev             Crypto device pointer
218  * @param       qp_id           Queue Pair Index
219  * @param       qp_conf         Queue configuration structure
220  * @param       socket_id       Socket Index
221  *
222  * @return      Returns 0 on success.
223  */
224 typedef int (*cryptodev_queue_pair_setup_t)(struct rte_cryptodev *dev,
225                 uint16_t qp_id, const struct rte_cryptodev_qp_conf *qp_conf,
226                 int socket_id);
227
228 /**
229  * Release memory resources allocated by given queue pair.
230  *
231  * @param       dev     Crypto device pointer
232  * @param       qp_id   Queue Pair Index
233  *
234  * @return
235  * - 0 on success.
236  * - EAGAIN if can't close as device is busy
237  */
238 typedef int (*cryptodev_queue_pair_release_t)(struct rte_cryptodev *dev,
239                 uint16_t qp_id);
240
241 /**
242  * Get number of available queue pairs of a device.
243  *
244  * @param       dev     Crypto device pointer
245  *
246  * @return      Returns number of queue pairs on success.
247  */
248 typedef uint32_t (*cryptodev_queue_pair_count_t)(struct rte_cryptodev *dev);
249
250 /**
251  * Create a session mempool to allocate sessions from
252  *
253  * @param       dev             Crypto device pointer
254  * @param       nb_objs         number of sessions objects in mempool
255  * @param       obj_cache       l-core object cache size, see *rte_ring_create*
256  * @param       socket_id       Socket Id to allocate  mempool on.
257  *
258  * @return
259  * - On success returns a pointer to a rte_mempool
260  * - On failure returns a NULL pointer
261  */
262 typedef int (*cryptodev_sym_create_session_pool_t)(
263                 struct rte_cryptodev *dev, unsigned nb_objs,
264                 unsigned obj_cache_size, int socket_id);
265
266
267 /**
268  * Get the size of a cryptodev session
269  *
270  * @param       dev             Crypto device pointer
271  *
272  * @return
273  *  - On success returns the size of the session structure for device
274  *  - On failure returns 0
275  */
276 typedef unsigned (*cryptodev_sym_get_session_private_size_t)(
277                 struct rte_cryptodev *dev);
278
279 /**
280  * Initialize a Crypto session on a device.
281  *
282  * @param       dev             Crypto device pointer
283  * @param       xform           Single or chain of crypto xforms
284  * @param       priv_sess       Pointer to cryptodev's private session structure
285  *
286  * @return
287  *  - Returns private session structure on success.
288  *  - Returns NULL on failure.
289  */
290 typedef void (*cryptodev_sym_initialize_session_t)(struct rte_mempool *mempool,
291                 void *session_private);
292
293 /**
294  * Configure a Crypto session on a device.
295  *
296  * @param       dev             Crypto device pointer
297  * @param       xform           Single or chain of crypto xforms
298  * @param       priv_sess       Pointer to cryptodev's private session structure
299  *
300  * @return
301  *  - Returns private session structure on success.
302  *  - Returns NULL on failure.
303  */
304 typedef void * (*cryptodev_sym_configure_session_t)(struct rte_cryptodev *dev,
305                 struct rte_crypto_sym_xform *xform, void *session_private);
306
307 /**
308  * Free Crypto session.
309  * @param       session         Cryptodev session structure to free
310  */
311 typedef void (*cryptodev_sym_free_session_t)(struct rte_cryptodev *dev,
312                 void *session_private);
313
314 /**
315  * Optional API for drivers to attach sessions with queue pair.
316  * @param       dev             Crypto device pointer
317  * @param       qp_id           queue pair id for attaching session
318  * @param       priv_sess       Pointer to cryptodev's private session structure
319  * @return
320  *  - Return 0 on success
321  */
322 typedef int (*cryptodev_sym_queue_pair_attach_session_t)(
323                   struct rte_cryptodev *dev,
324                   uint16_t qp_id,
325                   void *session_private);
326
327 /**
328  * Optional API for drivers to detach sessions from queue pair.
329  * @param       dev             Crypto device pointer
330  * @param       qp_id           queue pair id for detaching session
331  * @param       priv_sess       Pointer to cryptodev's private session structure
332  * @return
333  *  - Return 0 on success
334  */
335 typedef int (*cryptodev_sym_queue_pair_detach_session_t)(
336                   struct rte_cryptodev *dev,
337                   uint16_t qp_id,
338                   void *session_private);
339
340 /** Crypto device operations function pointer table */
341 struct rte_cryptodev_ops {
342         cryptodev_configure_t dev_configure;    /**< Configure device. */
343         cryptodev_start_t dev_start;            /**< Start device. */
344         cryptodev_stop_t dev_stop;              /**< Stop device. */
345         cryptodev_close_t dev_close;            /**< Close device. */
346
347         cryptodev_info_get_t dev_infos_get;     /**< Get device info. */
348
349         cryptodev_stats_get_t stats_get;
350         /**< Get device statistics. */
351         cryptodev_stats_reset_t stats_reset;
352         /**< Reset device statistics. */
353
354         cryptodev_queue_pair_setup_t queue_pair_setup;
355         /**< Set up a device queue pair. */
356         cryptodev_queue_pair_release_t queue_pair_release;
357         /**< Release a queue pair. */
358         cryptodev_queue_pair_start_t queue_pair_start;
359         /**< Start a queue pair. */
360         cryptodev_queue_pair_stop_t queue_pair_stop;
361         /**< Stop a queue pair. */
362         cryptodev_queue_pair_count_t queue_pair_count;
363         /**< Get count of the queue pairs. */
364
365         cryptodev_sym_get_session_private_size_t session_get_size;
366         /**< Return private session. */
367         cryptodev_sym_initialize_session_t session_initialize;
368         /**< Initialization function for private session data */
369         cryptodev_sym_configure_session_t session_configure;
370         /**< Configure a Crypto session. */
371         cryptodev_sym_free_session_t session_clear;
372         /**< Clear a Crypto sessions private data. */
373         cryptodev_sym_queue_pair_attach_session_t qp_attach_session;
374         /**< Attach session to queue pair. */
375         cryptodev_sym_queue_pair_attach_session_t qp_detach_session;
376         /**< Detach session from queue pair. */
377 };
378
379
380 /**
381  * Function for internal use by dummy drivers primarily, e.g. ring-based
382  * driver.
383  * Allocates a new cryptodev slot for an crypto device and returns the pointer
384  * to that slot for the driver to use.
385  *
386  * @param       name            Unique identifier name for each device
387  * @param       socket_id       Socket to allocate resources on.
388  * @return
389  *   - Slot in the rte_dev_devices array for a new device;
390  */
391 struct rte_cryptodev *
392 rte_cryptodev_pmd_allocate(const char *name, int socket_id);
393
394 /**
395  * Function for internal use by dummy drivers primarily, e.g. ring-based
396  * driver.
397  * Release the specified cryptodev device.
398  *
399  * @param cryptodev
400  * The *cryptodev* pointer is the address of the *rte_cryptodev* structure.
401  * @return
402  *   - 0 on success, negative on error
403  */
404 extern int
405 rte_cryptodev_pmd_release_device(struct rte_cryptodev *cryptodev);
406
407 /**
408  * Executes all the user application registered callbacks for the specific
409  * device.
410  *  *
411  * @param       dev     Pointer to cryptodev struct
412  * @param       event   Crypto device interrupt event type.
413  *
414  * @return
415  *  void
416  */
417 void rte_cryptodev_pmd_callback_process(struct rte_cryptodev *dev,
418                                 enum rte_cryptodev_event_type event);
419
420 /**
421  * @internal
422  * Create unique device name
423  */
424 int
425 rte_cryptodev_pmd_create_dev_name(char *name, const char *dev_name_prefix);
426
427 #ifdef __cplusplus
428 }
429 #endif
430
431 #endif /* _RTE_CRYPTODEV_PMD_H_ */