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