crypto/dpaa2_sec: introduce poll mode driver
[dpdk.git] / drivers / crypto / dpaa2_sec / dpaa2_sec_dpseci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
5  *   Copyright (c) 2016 NXP. All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of  Freescale Semiconductor, Inc nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <time.h>
35 #include <net/if.h>
36
37 #include <rte_mbuf.h>
38 #include <rte_cryptodev.h>
39 #include <rte_malloc.h>
40 #include <rte_memcpy.h>
41 #include <rte_string_fns.h>
42 #include <rte_cycles.h>
43 #include <rte_kvargs.h>
44 #include <rte_dev.h>
45 #include <rte_cryptodev_pmd.h>
46 #include <rte_common.h>
47 #include <rte_fslmc.h>
48 #include <fslmc_vfio.h>
49 #include <dpaa2_hw_pvt.h>
50 #include <dpaa2_hw_dpio.h>
51
52 #include "dpaa2_sec_priv.h"
53 #include "dpaa2_sec_logs.h"
54
55 #define FSL_VENDOR_ID           0x1957
56 #define FSL_DEVICE_ID           0x410
57 #define FSL_SUBSYSTEM_SEC       1
58 #define FSL_MC_DPSECI_DEVID     3
59
60 static int
61 dpaa2_sec_uninit(const struct rte_cryptodev_driver *crypto_drv __rte_unused,
62                  struct rte_cryptodev *dev)
63 {
64         PMD_INIT_LOG(INFO, "Closing DPAA2_SEC device %s on numa socket %u\n",
65                      dev->data->name, rte_socket_id());
66
67         return 0;
68 }
69
70 static int
71 dpaa2_sec_dev_init(struct rte_cryptodev *cryptodev)
72 {
73         struct dpaa2_sec_dev_private *internals;
74         struct rte_device *dev = cryptodev->device;
75         struct rte_dpaa2_device *dpaa2_dev;
76
77         PMD_INIT_FUNC_TRACE();
78         dpaa2_dev = container_of(dev, struct rte_dpaa2_device, device);
79         if (dpaa2_dev == NULL) {
80                 PMD_INIT_LOG(ERR, "dpaa2_device not found\n");
81                 return -1;
82         }
83
84         cryptodev->dev_type = RTE_CRYPTODEV_DPAA2_SEC_PMD;
85
86         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
87                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
88                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING;
89
90         internals = cryptodev->data->dev_private;
91         internals->max_nb_sessions = RTE_DPAA2_SEC_PMD_MAX_NB_SESSIONS;
92
93         /*
94          * For secondary processes, we don't initialise any further as primary
95          * has already done this work. Only check we don't need a different
96          * RX function
97          */
98         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
99                 PMD_INIT_LOG(DEBUG, "Device already init by primary process");
100                 return 0;
101         }
102
103         PMD_INIT_LOG(DEBUG, "driver %s: created\n", cryptodev->data->name);
104         return 0;
105 }
106
107 static int
108 cryptodev_dpaa2_sec_probe(struct rte_dpaa2_driver *dpaa2_drv,
109                           struct rte_dpaa2_device *dpaa2_dev)
110 {
111         struct rte_cryptodev *cryptodev;
112         char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
113
114         int retval;
115
116         sprintf(cryptodev_name, "dpsec-%d", dpaa2_dev->object_id);
117
118         cryptodev = rte_cryptodev_pmd_allocate(cryptodev_name, rte_socket_id());
119         if (cryptodev == NULL)
120                 return -ENOMEM;
121
122         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
123                 cryptodev->data->dev_private = rte_zmalloc_socket(
124                                         "cryptodev private structure",
125                                         sizeof(struct dpaa2_sec_dev_private),
126                                         RTE_CACHE_LINE_SIZE,
127                                         rte_socket_id());
128
129                 if (cryptodev->data->dev_private == NULL)
130                         rte_panic("Cannot allocate memzone for private "
131                                         "device data");
132         }
133
134         dpaa2_dev->cryptodev = cryptodev;
135         cryptodev->device = &dpaa2_dev->device;
136         cryptodev->driver = (struct rte_cryptodev_driver *)dpaa2_drv;
137
138         /* init user callbacks */
139         TAILQ_INIT(&(cryptodev->link_intr_cbs));
140
141         /* Invoke PMD device initialization function */
142         retval = dpaa2_sec_dev_init(cryptodev);
143         if (retval == 0)
144                 return 0;
145
146         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
147                 rte_free(cryptodev->data->dev_private);
148
149         cryptodev->attached = RTE_CRYPTODEV_DETACHED;
150
151         return -ENXIO;
152 }
153
154 static int
155 cryptodev_dpaa2_sec_remove(struct rte_dpaa2_device *dpaa2_dev)
156 {
157         struct rte_cryptodev *cryptodev;
158         int ret;
159
160         cryptodev = dpaa2_dev->cryptodev;
161         if (cryptodev == NULL)
162                 return -ENODEV;
163
164         ret = dpaa2_sec_uninit(NULL, cryptodev);
165         if (ret)
166                 return ret;
167
168         /* free crypto device */
169         rte_cryptodev_pmd_release_device(cryptodev);
170
171         if (rte_eal_process_type() == RTE_PROC_PRIMARY)
172                 rte_free(cryptodev->data->dev_private);
173
174         cryptodev->device = NULL;
175         cryptodev->driver = NULL;
176         cryptodev->data = NULL;
177
178         return 0;
179 }
180
181 static struct rte_dpaa2_driver rte_dpaa2_sec_driver = {
182         .drv_type = DPAA2_MC_DPSECI_DEVID,
183         .driver = {
184                 .name = "DPAA2 SEC PMD"
185         },
186         .probe = cryptodev_dpaa2_sec_probe,
187         .remove = cryptodev_dpaa2_sec_remove,
188 };
189
190 RTE_PMD_REGISTER_DPAA2(dpaa2_sec_pmd, rte_dpaa2_sec_driver);