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