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