event/dpaa: support Tx adapter
[dpdk.git] / drivers / crypto / qat / qat_sym_pmd.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2018 Intel Corporation
3  */
4
5 #include <rte_bus_pci.h>
6 #include <rte_common.h>
7 #include <rte_dev.h>
8 #include <rte_malloc.h>
9 #include <rte_pci.h>
10 #include <rte_cryptodev_pmd.h>
11
12 #include "qat_logs.h"
13 #include "qat_sym.h"
14 #include "qat_sym_session.h"
15 #include "qat_sym_pmd.h"
16
17 uint8_t cryptodev_qat_driver_id;
18
19 static const struct rte_cryptodev_capabilities qat_gen1_sym_capabilities[] = {
20         QAT_BASE_GEN1_SYM_CAPABILITIES,
21         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
22 };
23
24 static const struct rte_cryptodev_capabilities qat_gen2_sym_capabilities[] = {
25         QAT_BASE_GEN1_SYM_CAPABILITIES,
26         QAT_EXTRA_GEN2_SYM_CAPABILITIES,
27         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
28 };
29
30 static int qat_sym_qp_release(struct rte_cryptodev *dev,
31         uint16_t queue_pair_id);
32
33 static int qat_sym_dev_config(__rte_unused struct rte_cryptodev *dev,
34                 __rte_unused struct rte_cryptodev_config *config)
35 {
36         return 0;
37 }
38
39 static int qat_sym_dev_start(__rte_unused struct rte_cryptodev *dev)
40 {
41         return 0;
42 }
43
44 static void qat_sym_dev_stop(__rte_unused struct rte_cryptodev *dev)
45 {
46         return;
47 }
48
49 static int qat_sym_dev_close(struct rte_cryptodev *dev)
50 {
51         int i, ret;
52
53         for (i = 0; i < dev->data->nb_queue_pairs; i++) {
54                 ret = qat_sym_qp_release(dev, i);
55                 if (ret < 0)
56                         return ret;
57         }
58
59         return 0;
60 }
61
62 static void qat_sym_dev_info_get(struct rte_cryptodev *dev,
63                         struct rte_cryptodev_info *info)
64 {
65         struct qat_sym_dev_private *internals = dev->data->dev_private;
66         const struct qat_qp_hw_data *sym_hw_qps =
67                 qat_gen_config[internals->qat_dev->qat_dev_gen]
68                               .qp_hw_data[QAT_SERVICE_SYMMETRIC];
69
70         if (info != NULL) {
71                 info->max_nb_queue_pairs =
72                         qat_qps_per_service(sym_hw_qps, QAT_SERVICE_SYMMETRIC);
73                 info->feature_flags = dev->feature_flags;
74                 info->capabilities = internals->qat_dev_capabilities;
75                 info->driver_id = cryptodev_qat_driver_id;
76                 /* No limit of number of sessions */
77                 info->sym.max_nb_sessions = 0;
78         }
79 }
80
81 static void qat_sym_stats_get(struct rte_cryptodev *dev,
82                 struct rte_cryptodev_stats *stats)
83 {
84         struct qat_common_stats qat_stats = {0};
85         struct qat_sym_dev_private *qat_priv;
86
87         if (stats == NULL || dev == NULL) {
88                 QAT_LOG(ERR, "invalid ptr: stats %p, dev %p", stats, dev);
89                 return;
90         }
91         qat_priv = dev->data->dev_private;
92
93         qat_stats_get(qat_priv->qat_dev, &qat_stats, QAT_SERVICE_SYMMETRIC);
94         stats->enqueued_count = qat_stats.enqueued_count;
95         stats->dequeued_count = qat_stats.dequeued_count;
96         stats->enqueue_err_count = qat_stats.enqueue_err_count;
97         stats->dequeue_err_count = qat_stats.dequeue_err_count;
98 }
99
100 static void qat_sym_stats_reset(struct rte_cryptodev *dev)
101 {
102         struct qat_sym_dev_private *qat_priv;
103
104         if (dev == NULL) {
105                 QAT_LOG(ERR, "invalid cryptodev ptr %p", dev);
106                 return;
107         }
108         qat_priv = dev->data->dev_private;
109
110         qat_stats_reset(qat_priv->qat_dev, QAT_SERVICE_SYMMETRIC);
111
112 }
113
114 static int qat_sym_qp_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
115 {
116         struct qat_sym_dev_private *qat_private = dev->data->dev_private;
117
118         QAT_LOG(DEBUG, "Release sym qp %u on device %d",
119                                 queue_pair_id, dev->data->dev_id);
120
121         qat_private->qat_dev->qps_in_use[QAT_SERVICE_SYMMETRIC][queue_pair_id]
122                                                 = NULL;
123
124         return qat_qp_release((struct qat_qp **)
125                         &(dev->data->queue_pairs[queue_pair_id]));
126 }
127
128 static int qat_sym_qp_setup(struct rte_cryptodev *dev, uint16_t qp_id,
129         const struct rte_cryptodev_qp_conf *qp_conf,
130         int socket_id)
131 {
132         struct qat_qp *qp;
133         int ret = 0;
134         uint32_t i;
135         struct qat_qp_config qat_qp_conf;
136
137         struct qat_qp **qp_addr =
138                         (struct qat_qp **)&(dev->data->queue_pairs[qp_id]);
139         struct qat_sym_dev_private *qat_private = dev->data->dev_private;
140         const struct qat_qp_hw_data *sym_hw_qps =
141                         qat_gen_config[qat_private->qat_dev->qat_dev_gen]
142                                       .qp_hw_data[QAT_SERVICE_SYMMETRIC];
143         const struct qat_qp_hw_data *qp_hw_data = sym_hw_qps + qp_id;
144
145         /* If qp is already in use free ring memory and qp metadata. */
146         if (*qp_addr != NULL) {
147                 ret = qat_sym_qp_release(dev, qp_id);
148                 if (ret < 0)
149                         return ret;
150         }
151         if (qp_id >= qat_qps_per_service(sym_hw_qps, QAT_SERVICE_SYMMETRIC)) {
152                 QAT_LOG(ERR, "qp_id %u invalid for this device", qp_id);
153                 return -EINVAL;
154         }
155
156         qat_qp_conf.hw = qp_hw_data;
157         qat_qp_conf.build_request = qat_sym_build_request;
158         qat_qp_conf.cookie_size = sizeof(struct qat_sym_op_cookie);
159         qat_qp_conf.nb_descriptors = qp_conf->nb_descriptors;
160         qat_qp_conf.socket_id = socket_id;
161         qat_qp_conf.service_str = "sym";
162
163         ret = qat_qp_setup(qat_private->qat_dev, qp_addr, qp_id, &qat_qp_conf);
164         if (ret != 0)
165                 return ret;
166
167         /* store a link to the qp in the qat_pci_device */
168         qat_private->qat_dev->qps_in_use[QAT_SERVICE_SYMMETRIC][qp_id]
169                                                         = *qp_addr;
170
171         qp = (struct qat_qp *)*qp_addr;
172
173         for (i = 0; i < qp->nb_descriptors; i++) {
174
175                 struct qat_sym_op_cookie *cookie =
176                                 qp->op_cookies[i];
177
178                 cookie->qat_sgl_src_phys_addr =
179                                 rte_mempool_virt2iova(cookie) +
180                                 offsetof(struct qat_sym_op_cookie,
181                                 qat_sgl_src);
182
183                 cookie->qat_sgl_dst_phys_addr =
184                                 rte_mempool_virt2iova(cookie) +
185                                 offsetof(struct qat_sym_op_cookie,
186                                 qat_sgl_dst);
187         }
188
189         return ret;
190 }
191
192 static struct rte_cryptodev_ops crypto_qat_ops = {
193
194                 /* Device related operations */
195                 .dev_configure          = qat_sym_dev_config,
196                 .dev_start              = qat_sym_dev_start,
197                 .dev_stop               = qat_sym_dev_stop,
198                 .dev_close              = qat_sym_dev_close,
199                 .dev_infos_get          = qat_sym_dev_info_get,
200
201                 .stats_get              = qat_sym_stats_get,
202                 .stats_reset            = qat_sym_stats_reset,
203                 .queue_pair_setup       = qat_sym_qp_setup,
204                 .queue_pair_release     = qat_sym_qp_release,
205                 .queue_pair_count       = NULL,
206
207                 /* Crypto related operations */
208                 .sym_session_get_size   = qat_sym_session_get_private_size,
209                 .sym_session_configure  = qat_sym_session_configure,
210                 .sym_session_clear      = qat_sym_session_clear
211 };
212
213 static uint16_t
214 qat_sym_pmd_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
215                 uint16_t nb_ops)
216 {
217         return qat_enqueue_op_burst(qp, (void **)ops, nb_ops);
218 }
219
220 static uint16_t
221 qat_sym_pmd_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
222                 uint16_t nb_ops)
223 {
224         return qat_dequeue_op_burst(qp, (void **)ops, nb_ops);
225 }
226
227 /* An rte_driver is needed in the registration of both the device and the driver
228  * with cryptodev.
229  * The actual qat pci's rte_driver can't be used as its name represents
230  * the whole pci device with all services. Think of this as a holder for a name
231  * for the crypto part of the pci device.
232  */
233 static const char qat_sym_drv_name[] = RTE_STR(CRYPTODEV_NAME_QAT_SYM_PMD);
234 static const struct rte_driver cryptodev_qat_sym_driver = {
235         .name = qat_sym_drv_name,
236         .alias = qat_sym_drv_name
237 };
238
239 int
240 qat_sym_dev_create(struct qat_pci_device *qat_pci_dev)
241 {
242         struct rte_cryptodev_pmd_init_params init_params = {
243                         .name = "",
244                         .socket_id = qat_pci_dev->pci_dev->device.numa_node,
245                         .private_data_size = sizeof(struct qat_sym_dev_private)
246         };
247         char name[RTE_CRYPTODEV_NAME_MAX_LEN];
248         struct rte_cryptodev *cryptodev;
249         struct qat_sym_dev_private *internals;
250
251         snprintf(name, RTE_CRYPTODEV_NAME_MAX_LEN, "%s_%s",
252                         qat_pci_dev->name, "sym");
253         QAT_LOG(DEBUG, "Creating QAT SYM device %s", name);
254
255         /* Populate subset device to use in cryptodev device creation */
256         qat_pci_dev->sym_rte_dev.driver = &cryptodev_qat_sym_driver;
257         qat_pci_dev->sym_rte_dev.numa_node =
258                                 qat_pci_dev->pci_dev->device.numa_node;
259         qat_pci_dev->sym_rte_dev.devargs = NULL;
260
261         cryptodev = rte_cryptodev_pmd_create(name,
262                         &(qat_pci_dev->sym_rte_dev), &init_params);
263
264         if (cryptodev == NULL)
265                 return -ENODEV;
266
267         qat_pci_dev->sym_rte_dev.name = cryptodev->data->name;
268         cryptodev->driver_id = cryptodev_qat_driver_id;
269         cryptodev->dev_ops = &crypto_qat_ops;
270
271         cryptodev->enqueue_burst = qat_sym_pmd_enqueue_op_burst;
272         cryptodev->dequeue_burst = qat_sym_pmd_dequeue_op_burst;
273
274         cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
275                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
276                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
277                         RTE_CRYPTODEV_FF_IN_PLACE_SGL |
278                         RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
279                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
280                         RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
281                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
282                         RTE_CRYPTODEV_FF_DIGEST_ENCRYPTED;
283
284         internals = cryptodev->data->dev_private;
285         internals->qat_dev = qat_pci_dev;
286         qat_pci_dev->sym_dev = internals;
287
288         internals->sym_dev_id = cryptodev->data->dev_id;
289         switch (qat_pci_dev->qat_dev_gen) {
290         case QAT_GEN1:
291                 internals->qat_dev_capabilities = qat_gen1_sym_capabilities;
292                 break;
293         case QAT_GEN2:
294         case QAT_GEN3:
295                 internals->qat_dev_capabilities = qat_gen2_sym_capabilities;
296                 break;
297         default:
298                 internals->qat_dev_capabilities = qat_gen2_sym_capabilities;
299                 QAT_LOG(DEBUG,
300                         "QAT gen %d capabilities unknown, default to GEN2",
301                                         qat_pci_dev->qat_dev_gen);
302                 break;
303         }
304
305         QAT_LOG(DEBUG, "Created QAT SYM device %s as cryptodev instance %d",
306                         cryptodev->data->name, internals->sym_dev_id);
307         return 0;
308 }
309
310 int
311 qat_sym_dev_destroy(struct qat_pci_device *qat_pci_dev)
312 {
313         struct rte_cryptodev *cryptodev;
314
315         if (qat_pci_dev == NULL)
316                 return -ENODEV;
317         if (qat_pci_dev->sym_dev == NULL)
318                 return 0;
319
320         /* free crypto device */
321         cryptodev = rte_cryptodev_pmd_get_dev(qat_pci_dev->sym_dev->sym_dev_id);
322         rte_cryptodev_pmd_destroy(cryptodev);
323         qat_pci_dev->sym_rte_dev.name = NULL;
324         qat_pci_dev->sym_dev = NULL;
325
326         return 0;
327 }
328
329 static struct cryptodev_driver qat_crypto_drv;
330 RTE_PMD_REGISTER_CRYPTO_DRIVER(qat_crypto_drv,
331                 cryptodev_qat_sym_driver,
332                 cryptodev_qat_driver_id);