Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018 HUAWEI TECHNOLOGIES CO., LTD.
3 : : */
4 : : #include <stdbool.h>
5 : : #include <unistd.h>
6 : :
7 : : #include <rte_common.h>
8 : : #include <rte_errno.h>
9 : : #include <rte_pci.h>
10 : : #include <bus_pci_driver.h>
11 : : #include <rte_cryptodev.h>
12 : : #include <cryptodev_pmd.h>
13 : : #include <rte_eal.h>
14 : :
15 : : #include "virtio_cryptodev.h"
16 : : #include "virtqueue.h"
17 : : #include "virtio_crypto_algs.h"
18 : : #include "virtio_crypto_capabilities.h"
19 : :
20 : : static int virtio_crypto_dev_configure(struct rte_cryptodev *dev,
21 : : struct rte_cryptodev_config *config);
22 : : static int virtio_crypto_dev_start(struct rte_cryptodev *dev);
23 : : static void virtio_crypto_dev_stop(struct rte_cryptodev *dev);
24 : : static int virtio_crypto_dev_close(struct rte_cryptodev *dev);
25 : : static void virtio_crypto_dev_info_get(struct rte_cryptodev *dev,
26 : : struct rte_cryptodev_info *dev_info);
27 : : static void virtio_crypto_dev_stats_get(struct rte_cryptodev *dev,
28 : : struct rte_cryptodev_stats *stats);
29 : : static void virtio_crypto_dev_stats_reset(struct rte_cryptodev *dev);
30 : : static int virtio_crypto_qp_setup(struct rte_cryptodev *dev,
31 : : uint16_t queue_pair_id,
32 : : const struct rte_cryptodev_qp_conf *qp_conf,
33 : : int socket_id);
34 : : static int virtio_crypto_qp_release(struct rte_cryptodev *dev,
35 : : uint16_t queue_pair_id);
36 : : static void virtio_crypto_dev_free_mbufs(struct rte_cryptodev *dev);
37 : : static unsigned int virtio_crypto_sym_get_session_private_size(
38 : : struct rte_cryptodev *dev);
39 : : static void virtio_crypto_sym_clear_session(struct rte_cryptodev *dev,
40 : : struct rte_cryptodev_sym_session *sess);
41 : : static int virtio_crypto_sym_configure_session(struct rte_cryptodev *dev,
42 : : struct rte_crypto_sym_xform *xform,
43 : : struct rte_cryptodev_sym_session *session);
44 : : static void virtio_crypto_asym_clear_session(struct rte_cryptodev *dev,
45 : : struct rte_cryptodev_asym_session *sess);
46 : : static int virtio_crypto_asym_configure_session(struct rte_cryptodev *dev,
47 : : struct rte_crypto_asym_xform *xform,
48 : : struct rte_cryptodev_asym_session *session);
49 : :
50 : : /*
51 : : * The set of PCI devices this driver supports
52 : : */
53 : : static const struct rte_pci_id pci_id_virtio_crypto_map[] = {
54 : : { RTE_PCI_DEVICE(VIRTIO_CRYPTO_PCI_VENDORID,
55 : : VIRTIO_CRYPTO_PCI_DEVICEID) },
56 : : { .vendor_id = 0, /* sentinel */ },
57 : : };
58 : :
59 : : static const struct rte_cryptodev_capabilities virtio_capabilities[] = {
60 : : VIRTIO_SYM_CAPABILITIES,
61 : : VIRTIO_ASYM_CAPABILITIES,
62 : : RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
63 : : };
64 : :
65 : : uint8_t cryptodev_virtio_driver_id;
66 : :
67 : : void
68 : 0 : virtio_crypto_queue_release(struct virtqueue *vq)
69 : : {
70 : : struct virtio_crypto_hw *hw;
71 : :
72 : 0 : PMD_INIT_FUNC_TRACE();
73 : :
74 [ # # ]: 0 : if (vq) {
75 : 0 : hw = vq->hw;
76 : : /* Select and deactivate the queue */
77 : 0 : VTPCI_OPS(hw)->del_queue(hw, vq);
78 : :
79 : 0 : hw->vqs[vq->vq_queue_index] = NULL;
80 : 0 : rte_memzone_free(vq->mz);
81 : 0 : rte_mempool_free(vq->mpool);
82 : 0 : rte_free(vq);
83 : : }
84 : 0 : }
85 : :
86 : : #define MPOOL_MAX_NAME_SZ 32
87 : :
88 : : int
89 : 0 : virtio_crypto_queue_setup(struct rte_cryptodev *dev,
90 : : int queue_type,
91 : : uint16_t vtpci_queue_idx,
92 : : uint16_t nb_desc,
93 : : int socket_id,
94 : : struct virtqueue **pvq)
95 : : {
96 : : char vq_name[VIRTQUEUE_MAX_NAME_SZ];
97 : : char mpool_name[MPOOL_MAX_NAME_SZ];
98 : : unsigned int vq_size;
99 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
100 : : struct virtqueue *vq = NULL;
101 : :
102 : 0 : PMD_INIT_FUNC_TRACE();
103 : :
104 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("setting up queue: %u", vtpci_queue_idx);
105 : :
106 : : /*
107 : : * Read the virtqueue size from the Queue Size field
108 : : * Always power of 2 and if 0 virtqueue does not exist
109 : : */
110 : 0 : vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx);
111 [ # # ]: 0 : if (vq_size == 0) {
112 : 0 : VIRTIO_CRYPTO_INIT_LOG_ERR("virtqueue does not exist");
113 : 0 : return -EINVAL;
114 : : }
115 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq_size: %u", vq_size);
116 : :
117 : : if (!rte_is_power_of_2(vq_size)) {
118 : 0 : VIRTIO_CRYPTO_INIT_LOG_ERR("virtqueue size is not powerof 2");
119 : 0 : return -EINVAL;
120 : : }
121 : :
122 [ # # ]: 0 : if (queue_type == VTCRYPTO_DATAQ) {
123 : 0 : snprintf(vq_name, sizeof(vq_name), "dev%d_dataqueue%d",
124 : 0 : dev->data->dev_id, vtpci_queue_idx);
125 : 0 : snprintf(mpool_name, sizeof(mpool_name),
126 : : "dev%d_dataqueue%d_mpool",
127 : 0 : dev->data->dev_id, vtpci_queue_idx);
128 [ # # ]: 0 : } else if (queue_type == VTCRYPTO_CTRLQ) {
129 : 0 : snprintf(vq_name, sizeof(vq_name), "dev%d_controlqueue",
130 : 0 : dev->data->dev_id);
131 : 0 : snprintf(mpool_name, sizeof(mpool_name),
132 : : "dev%d_controlqueue_mpool",
133 : 0 : dev->data->dev_id);
134 : : }
135 : :
136 : : /*
137 : : * Using part of the vring entries is permitted, but the maximum
138 : : * is vq_size
139 : : */
140 [ # # # # ]: 0 : if (nb_desc == 0 || nb_desc > vq_size)
141 : : nb_desc = vq_size;
142 : :
143 [ # # ]: 0 : if (hw->vqs[vtpci_queue_idx])
144 : : vq = hw->vqs[vtpci_queue_idx];
145 : : else
146 : 0 : vq = virtcrypto_queue_alloc(hw, vtpci_queue_idx, nb_desc,
147 : : socket_id, vq_name);
148 [ # # ]: 0 : if (vq == NULL) {
149 : 0 : VIRTIO_CRYPTO_INIT_LOG_ERR("Can not allocate virtqueue");
150 : 0 : return -ENOMEM;
151 : : }
152 : :
153 : 0 : hw->vqs[vtpci_queue_idx] = vq;
154 : :
155 [ # # ]: 0 : if (queue_type == VTCRYPTO_DATAQ) {
156 : : /* pre-allocate a mempool and use it in the data plane to
157 : : * improve performance
158 : : */
159 : 0 : vq->mpool = rte_mempool_lookup(mpool_name);
160 [ # # ]: 0 : if (vq->mpool == NULL)
161 : 0 : vq->mpool = rte_mempool_create(mpool_name,
162 : : nb_desc,
163 : : sizeof(struct virtio_crypto_op_cookie),
164 : : RTE_CACHE_LINE_SIZE, 0,
165 : : NULL, NULL, NULL, NULL, socket_id,
166 : : 0);
167 [ # # ]: 0 : if (!vq->mpool) {
168 : 0 : VIRTIO_CRYPTO_DRV_LOG_ERR("Virtio Crypto PMD "
169 : : "Cannot create mempool");
170 : 0 : goto mpool_create_err;
171 : : }
172 : : }
173 : :
174 : 0 : *pvq = vq;
175 : :
176 : 0 : return 0;
177 : :
178 : : mpool_create_err:
179 : 0 : rte_free(vq);
180 : 0 : return -ENOMEM;
181 : : }
182 : :
183 : : static void
184 : 0 : virtio_crypto_free_queues(struct rte_cryptodev *dev)
185 : : {
186 : : unsigned int i;
187 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
188 : :
189 : 0 : PMD_INIT_FUNC_TRACE();
190 : :
191 : : /* data queue release */
192 [ # # ]: 0 : for (i = 0; i < hw->max_dataqueues; i++) {
193 : 0 : virtio_crypto_queue_release(dev->data->queue_pairs[i]);
194 : 0 : dev->data->queue_pairs[i] = NULL;
195 : : }
196 : 0 : }
197 : :
198 : : static int
199 : 0 : virtio_crypto_dev_close(struct rte_cryptodev *dev __rte_unused)
200 : : {
201 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
202 : :
203 : 0 : PMD_INIT_FUNC_TRACE();
204 : :
205 : : /* control queue release */
206 [ # # ]: 0 : if (hw->cvq)
207 : 0 : virtio_crypto_queue_release(virtcrypto_cq_to_vq(hw->cvq));
208 : :
209 : 0 : hw->cvq = NULL;
210 : 0 : return 0;
211 : : }
212 : :
213 : : /*
214 : : * dev_ops for virtio, bare necessities for basic operation
215 : : */
216 : : static struct rte_cryptodev_ops virtio_crypto_dev_ops = {
217 : : /* Device related operations */
218 : : .dev_configure = virtio_crypto_dev_configure,
219 : : .dev_start = virtio_crypto_dev_start,
220 : : .dev_stop = virtio_crypto_dev_stop,
221 : : .dev_close = virtio_crypto_dev_close,
222 : : .dev_infos_get = virtio_crypto_dev_info_get,
223 : :
224 : : .stats_get = virtio_crypto_dev_stats_get,
225 : : .stats_reset = virtio_crypto_dev_stats_reset,
226 : :
227 : : .queue_pair_setup = virtio_crypto_qp_setup,
228 : : .queue_pair_release = virtio_crypto_qp_release,
229 : :
230 : : /* Crypto related operations */
231 : : .sym_session_get_size = virtio_crypto_sym_get_session_private_size,
232 : : .sym_session_configure = virtio_crypto_sym_configure_session,
233 : : .sym_session_clear = virtio_crypto_sym_clear_session,
234 : : .asym_session_get_size = virtio_crypto_sym_get_session_private_size,
235 : : .asym_session_configure = virtio_crypto_asym_configure_session,
236 : : .asym_session_clear = virtio_crypto_asym_clear_session
237 : : };
238 : :
239 : : static void
240 : 0 : virtio_crypto_update_stats(struct rte_cryptodev *dev,
241 : : struct rte_cryptodev_stats *stats)
242 : : {
243 : : unsigned int i;
244 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
245 : :
246 : 0 : PMD_INIT_FUNC_TRACE();
247 : :
248 [ # # ]: 0 : if (stats == NULL) {
249 : 0 : VIRTIO_CRYPTO_DRV_LOG_ERR("invalid pointer");
250 : 0 : return;
251 : : }
252 : :
253 [ # # ]: 0 : for (i = 0; i < hw->max_dataqueues; i++) {
254 : 0 : const struct virtqueue *data_queue
255 : 0 : = dev->data->queue_pairs[i];
256 [ # # ]: 0 : if (data_queue == NULL)
257 : 0 : continue;
258 : :
259 : 0 : stats->enqueued_count += data_queue->packets_sent_total;
260 : 0 : stats->enqueue_err_count += data_queue->packets_sent_failed;
261 : :
262 : 0 : stats->dequeued_count += data_queue->packets_received_total;
263 : : stats->dequeue_err_count
264 : 0 : += data_queue->packets_received_failed;
265 : : }
266 : : }
267 : :
268 : : static void
269 : 0 : virtio_crypto_dev_stats_get(struct rte_cryptodev *dev,
270 : : struct rte_cryptodev_stats *stats)
271 : : {
272 : 0 : PMD_INIT_FUNC_TRACE();
273 : :
274 : 0 : virtio_crypto_update_stats(dev, stats);
275 : 0 : }
276 : :
277 : : static void
278 : 0 : virtio_crypto_dev_stats_reset(struct rte_cryptodev *dev)
279 : : {
280 : : unsigned int i;
281 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
282 : :
283 : 0 : PMD_INIT_FUNC_TRACE();
284 : :
285 [ # # ]: 0 : for (i = 0; i < hw->max_dataqueues; i++) {
286 : 0 : struct virtqueue *data_queue = dev->data->queue_pairs[i];
287 [ # # ]: 0 : if (data_queue == NULL)
288 : 0 : continue;
289 : :
290 : 0 : data_queue->packets_sent_total = 0;
291 : 0 : data_queue->packets_sent_failed = 0;
292 : :
293 : 0 : data_queue->packets_received_total = 0;
294 : 0 : data_queue->packets_received_failed = 0;
295 : : }
296 : 0 : }
297 : :
298 : : static int
299 : 0 : virtio_crypto_qp_setup(struct rte_cryptodev *dev, uint16_t queue_pair_id,
300 : : const struct rte_cryptodev_qp_conf *qp_conf,
301 : : int socket_id)
302 : : {
303 : : int ret;
304 : : struct virtqueue *vq;
305 : :
306 : 0 : PMD_INIT_FUNC_TRACE();
307 : :
308 : : /* if virtio dev is started, do not touch the virtqueues */
309 [ # # ]: 0 : if (dev->data->dev_started)
310 : : return 0;
311 : :
312 : 0 : ret = virtio_crypto_queue_setup(dev, VTCRYPTO_DATAQ, queue_pair_id,
313 : 0 : qp_conf->nb_descriptors, socket_id, &vq);
314 [ # # ]: 0 : if (ret < 0) {
315 : 0 : VIRTIO_CRYPTO_INIT_LOG_ERR(
316 : : "virtio crypto data queue initialization failed");
317 : 0 : return ret;
318 : : }
319 : :
320 : 0 : dev->data->queue_pairs[queue_pair_id] = vq;
321 : :
322 : 0 : return 0;
323 : : }
324 : :
325 : : static int
326 : 0 : virtio_crypto_qp_release(struct rte_cryptodev *dev, uint16_t queue_pair_id)
327 : : {
328 : 0 : struct virtqueue *vq
329 : 0 : = (struct virtqueue *)dev->data->queue_pairs[queue_pair_id];
330 : :
331 : 0 : PMD_INIT_FUNC_TRACE();
332 : :
333 [ # # ]: 0 : if (vq == NULL) {
334 : 0 : VIRTIO_CRYPTO_DRV_LOG_DBG("vq already freed");
335 : 0 : return 0;
336 : : }
337 : :
338 : 0 : virtio_crypto_queue_release(vq);
339 : 0 : dev->data->queue_pairs[queue_pair_id] = NULL;
340 : 0 : return 0;
341 : : }
342 : :
343 : : static int
344 : 0 : virtio_negotiate_features(struct virtio_crypto_hw *hw, uint64_t req_features)
345 : : {
346 : : uint64_t host_features;
347 : :
348 : 0 : PMD_INIT_FUNC_TRACE();
349 : :
350 : : /* Prepare guest_features: feature that driver wants to support */
351 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("guest_features before negotiate = %" PRIx64,
352 : : req_features);
353 : :
354 : : /* Read device(host) feature bits */
355 : 0 : host_features = VTPCI_OPS(hw)->get_features(hw);
356 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("host_features before negotiate = %" PRIx64,
357 : : host_features);
358 : :
359 : : /*
360 : : * Negotiate features: Subset of device feature bits are written back
361 : : * guest feature bits.
362 : : */
363 : 0 : hw->guest_features = req_features;
364 : 0 : hw->guest_features = vtpci_cryptodev_negotiate_features(hw,
365 : : host_features);
366 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("features after negotiate = %" PRIx64,
367 : : hw->guest_features);
368 : :
369 [ # # ]: 0 : if (hw->modern) {
370 [ # # ]: 0 : if (!vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) {
371 : 0 : VIRTIO_CRYPTO_INIT_LOG_ERR(
372 : : "VIRTIO_F_VERSION_1 features is not enabled.");
373 : 0 : return -1;
374 : : }
375 : 0 : vtpci_cryptodev_set_status(hw,
376 : : VIRTIO_CONFIG_STATUS_FEATURES_OK);
377 [ # # ]: 0 : if (!(vtpci_cryptodev_get_status(hw) &
378 : : VIRTIO_CONFIG_STATUS_FEATURES_OK)) {
379 : 0 : VIRTIO_CRYPTO_INIT_LOG_ERR("failed to set FEATURES_OK "
380 : : "status!");
381 : 0 : return -1;
382 : : }
383 : : }
384 : :
385 : 0 : hw->req_guest_features = req_features;
386 : :
387 : 0 : return 0;
388 : : }
389 : :
390 : : static void
391 : 0 : virtio_control_queue_notify(struct virtqueue *vq, __rte_unused void *cookie)
392 : : {
393 : : virtqueue_notify(vq);
394 : 0 : }
395 : :
396 : : static int
397 : 0 : virtio_crypto_init_queue(struct rte_cryptodev *dev, uint16_t queue_idx)
398 : : {
399 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
400 [ # # ]: 0 : int queue_type = virtio_get_queue_type(hw, queue_idx);
401 : 0 : int numa_node = dev->device->numa_node;
402 : : char vq_name[VIRTQUEUE_MAX_NAME_SZ];
403 : : unsigned int vq_size;
404 : : struct virtqueue *vq;
405 : : int ret;
406 : :
407 : 0 : PMD_INIT_LOG(INFO, "setting up queue: %u on NUMA node %d",
408 : : queue_idx, numa_node);
409 : :
410 : : /*
411 : : * Read the virtqueue size from the Queue Size field
412 : : * Always power of 2 and if 0 virtqueue does not exist
413 : : */
414 : 0 : vq_size = VTPCI_OPS(hw)->get_queue_num(hw, queue_idx);
415 : 0 : PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size);
416 [ # # ]: 0 : if (vq_size == 0) {
417 : 0 : PMD_INIT_LOG(ERR, "virtqueue does not exist");
418 : 0 : return -EINVAL;
419 : : }
420 : :
421 : : if (!rte_is_power_of_2(vq_size)) {
422 : 0 : PMD_INIT_LOG(ERR, "split virtqueue size is not power of 2");
423 : 0 : return -EINVAL;
424 : : }
425 : :
426 : 0 : snprintf(vq_name, sizeof(vq_name), "dev%d_vq%d", dev->data->dev_id, queue_idx);
427 : :
428 : 0 : vq = virtcrypto_queue_alloc(hw, queue_idx, vq_size, numa_node, vq_name);
429 [ # # ]: 0 : if (!vq) {
430 : 0 : PMD_INIT_LOG(ERR, "virtqueue init failed");
431 : 0 : return -ENOMEM;
432 : : }
433 : :
434 : 0 : hw->vqs[queue_idx] = vq;
435 : :
436 [ # # ]: 0 : if (queue_type == VTCRYPTO_CTRLQ) {
437 : 0 : hw->cvq = &vq->cq;
438 : 0 : vq->cq.notify_queue = &virtio_control_queue_notify;
439 : : }
440 : :
441 [ # # ]: 0 : if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) {
442 : 0 : PMD_INIT_LOG(ERR, "setup_queue failed");
443 : : ret = -EINVAL;
444 : 0 : goto clean_vq;
445 : : }
446 : :
447 : : return 0;
448 : :
449 : : clean_vq:
450 [ # # ]: 0 : if (queue_type == VTCRYPTO_CTRLQ)
451 : 0 : hw->cvq = NULL;
452 : 0 : virtcrypto_queue_free(vq);
453 : 0 : hw->vqs[queue_idx] = NULL;
454 : :
455 : 0 : return ret;
456 : : }
457 : :
458 : : static int
459 : 0 : virtio_crypto_alloc_queues(struct rte_cryptodev *dev)
460 : : {
461 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
462 : 0 : uint16_t nr_vq = hw->max_dataqueues + 1;
463 : : uint16_t i;
464 : : int ret;
465 : :
466 : 0 : hw->vqs = rte_zmalloc(NULL, sizeof(struct virtqueue *) * nr_vq, 0);
467 [ # # ]: 0 : if (!hw->vqs) {
468 : 0 : PMD_INIT_LOG(ERR, "failed to allocate vqs");
469 : 0 : return -ENOMEM;
470 : : }
471 : :
472 [ # # ]: 0 : for (i = 0; i < nr_vq; i++) {
473 : 0 : ret = virtio_crypto_init_queue(dev, i);
474 [ # # ]: 0 : if (ret < 0) {
475 : 0 : virtio_crypto_free_queues(dev);
476 : 0 : return ret;
477 : : }
478 : : }
479 : :
480 : : return 0;
481 : : }
482 : :
483 : : /* reset device and renegotiate features if needed */
484 : : static int
485 : 0 : virtio_crypto_init_device(struct rte_cryptodev *cryptodev,
486 : : uint64_t req_features)
487 : : {
488 : 0 : struct virtio_crypto_hw *hw = cryptodev->data->dev_private;
489 : : struct virtio_crypto_config local_config;
490 : : struct virtio_crypto_config *config = &local_config;
491 : :
492 : 0 : PMD_INIT_FUNC_TRACE();
493 : :
494 : : /* Reset the device although not necessary at startup */
495 : 0 : vtpci_cryptodev_reset(hw);
496 : :
497 : : /* Tell the host we've noticed this device. */
498 : 0 : vtpci_cryptodev_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
499 : :
500 : : /* Tell the host we've known how to drive the device. */
501 : 0 : vtpci_cryptodev_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
502 [ # # ]: 0 : if (virtio_negotiate_features(hw, req_features) < 0)
503 : : return -1;
504 : :
505 : : /* Get status of the device */
506 : 0 : vtpci_read_cryptodev_config(hw,
507 : : offsetof(struct virtio_crypto_config, status),
508 : : &config->status, sizeof(config->status));
509 [ # # ]: 0 : if (config->status != VIRTIO_CRYPTO_S_HW_READY) {
510 : 0 : VIRTIO_CRYPTO_DRV_LOG_ERR("accelerator hardware is "
511 : : "not ready");
512 : 0 : return -1;
513 : : }
514 : :
515 : : /* Get number of data queues */
516 : 0 : vtpci_read_cryptodev_config(hw,
517 : : offsetof(struct virtio_crypto_config, max_dataqueues),
518 : : &config->max_dataqueues,
519 : : sizeof(config->max_dataqueues));
520 : 0 : hw->max_dataqueues = config->max_dataqueues;
521 : :
522 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("hw->max_dataqueues=%d",
523 : : hw->max_dataqueues);
524 : :
525 : 0 : return 0;
526 : : }
527 : :
528 : : int
529 : 0 : crypto_virtio_dev_init(struct rte_cryptodev *cryptodev, uint64_t features,
530 : : struct rte_pci_device *pci_dev)
531 : : {
532 : : struct virtio_crypto_hw *hw;
533 : :
534 : 0 : cryptodev->dev_ops = &virtio_crypto_dev_ops;
535 : :
536 : 0 : cryptodev->enqueue_burst = virtio_crypto_pkt_tx_burst;
537 : 0 : cryptodev->dequeue_burst = virtio_crypto_pkt_rx_burst;
538 : :
539 : 0 : cryptodev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
540 : : RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
541 : : RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
542 : : RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
543 : : RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
544 : :
545 : 0 : hw = cryptodev->data->dev_private;
546 : 0 : hw->dev_id = cryptodev->data->dev_id;
547 : 0 : hw->virtio_dev_capabilities = virtio_capabilities;
548 : :
549 [ # # ]: 0 : if (pci_dev) {
550 : : /* pci device init */
551 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("dev %d vendorID=0x%x deviceID=0x%x",
552 : : cryptodev->data->dev_id, pci_dev->id.vendor_id,
553 : : pci_dev->id.device_id);
554 : :
555 [ # # ]: 0 : if (vtpci_cryptodev_init(pci_dev, hw))
556 : : return -1;
557 : : }
558 : :
559 [ # # ]: 0 : if (virtio_crypto_init_device(cryptodev, features) < 0)
560 : 0 : return -1;
561 : :
562 : : return 0;
563 : : }
564 : :
565 : : /*
566 : : * This function is based on probe() function
567 : : * It returns 0 on success.
568 : : */
569 : : static int
570 : 0 : crypto_virtio_create(const char *name, struct rte_pci_device *pci_dev,
571 : : struct rte_cryptodev_pmd_init_params *init_params)
572 : : {
573 : : struct rte_cryptodev *cryptodev;
574 : :
575 : 0 : PMD_INIT_FUNC_TRACE();
576 : :
577 : 0 : cryptodev = rte_cryptodev_pmd_create(name, &pci_dev->device,
578 : : init_params);
579 [ # # ]: 0 : if (cryptodev == NULL)
580 : : return -ENODEV;
581 : :
582 : 0 : cryptodev->driver_id = cryptodev_virtio_driver_id;
583 [ # # ]: 0 : if (crypto_virtio_dev_init(cryptodev, VIRTIO_CRYPTO_PMD_GUEST_FEATURES,
584 : : pci_dev) < 0)
585 : : return -1;
586 : :
587 : 0 : rte_cryptodev_pmd_probing_finish(cryptodev);
588 : :
589 : 0 : return 0;
590 : : }
591 : :
592 : : static int
593 : 0 : virtio_crypto_dev_uninit(struct rte_cryptodev *cryptodev)
594 : : {
595 : 0 : PMD_INIT_FUNC_TRACE();
596 : :
597 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY)
598 : : return -EPERM;
599 : :
600 [ # # ]: 0 : if (cryptodev->data->dev_started) {
601 : 0 : virtio_crypto_dev_stop(cryptodev);
602 : 0 : virtio_crypto_dev_close(cryptodev);
603 : : }
604 : :
605 : 0 : cryptodev->dev_ops = NULL;
606 : 0 : cryptodev->enqueue_burst = NULL;
607 : 0 : cryptodev->dequeue_burst = NULL;
608 : :
609 : 0 : rte_cryptodev_pmd_release_device(cryptodev);
610 : :
611 : 0 : VIRTIO_CRYPTO_DRV_LOG_INFO("dev_uninit completed");
612 : :
613 : 0 : return 0;
614 : : }
615 : :
616 : : static int
617 : 0 : virtio_crypto_dev_configure(struct rte_cryptodev *cryptodev,
618 : : struct rte_cryptodev_config *config __rte_unused)
619 : : {
620 : 0 : PMD_INIT_FUNC_TRACE();
621 : :
622 [ # # ]: 0 : if (virtio_crypto_init_device(cryptodev,
623 : : VIRTIO_CRYPTO_PMD_GUEST_FEATURES) < 0)
624 : : return -1;
625 : :
626 : : /* setup control queue
627 : : * [0, 1, ... ,(config->max_dataqueues - 1)] are data queues
628 : : * config->max_dataqueues is the control queue
629 : : */
630 [ # # ]: 0 : if (virtio_crypto_alloc_queues(cryptodev) < 0) {
631 : 0 : VIRTIO_CRYPTO_DRV_LOG_ERR("failed to create virtqueues");
632 : 0 : return -1;
633 : : }
634 : :
635 : 0 : virtio_crypto_ctrlq_start(cryptodev);
636 : :
637 : 0 : return 0;
638 : : }
639 : :
640 : : static void
641 : 0 : virtio_crypto_dev_stop(struct rte_cryptodev *dev)
642 : : {
643 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
644 : :
645 : 0 : PMD_INIT_FUNC_TRACE();
646 : 0 : VIRTIO_CRYPTO_DRV_LOG_DBG("virtio_dev_stop");
647 : :
648 : 0 : vtpci_cryptodev_reset(hw);
649 : :
650 : 0 : virtio_crypto_dev_free_mbufs(dev);
651 : 0 : virtio_crypto_free_queues(dev);
652 : :
653 : 0 : dev->data->dev_started = 0;
654 : 0 : }
655 : :
656 : : static int
657 : 0 : virtio_crypto_dev_start(struct rte_cryptodev *dev)
658 : : {
659 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
660 : :
661 [ # # ]: 0 : if (dev->data->dev_started)
662 : : return 0;
663 : :
664 : : /* Do final configuration before queue engine starts */
665 : 0 : virtio_crypto_dataq_start(dev);
666 : 0 : vtpci_cryptodev_reinit_complete(hw);
667 : :
668 : 0 : dev->data->dev_started = 1;
669 : :
670 : 0 : return 0;
671 : : }
672 : :
673 : : static void
674 : 0 : virtio_crypto_dev_free_mbufs(struct rte_cryptodev *dev)
675 : : {
676 : : uint32_t i;
677 : :
678 [ # # ]: 0 : for (i = 0; i < dev->data->nb_queue_pairs; i++) {
679 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("Before freeing dataq[%d] used "
680 : : "and unused buf", i);
681 [ # # ]: 0 : VIRTQUEUE_DUMP((struct virtqueue *)
682 : : dev->data->queue_pairs[i]);
683 : :
684 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("queue_pairs[%d]=%p",
685 : : i, dev->data->queue_pairs[i]);
686 : :
687 : 0 : virtqueue_detatch_unused(dev->data->queue_pairs[i]);
688 : :
689 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("After freeing dataq[%d] used and "
690 : : "unused buf", i);
691 [ # # ]: 0 : VIRTQUEUE_DUMP(
692 : : (struct virtqueue *)dev->data->queue_pairs[i]);
693 : : }
694 : 0 : }
695 : :
696 : : static unsigned int
697 : 0 : virtio_crypto_sym_get_session_private_size(
698 : : struct rte_cryptodev *dev __rte_unused)
699 : : {
700 : 0 : PMD_INIT_FUNC_TRACE();
701 : :
702 : 0 : return RTE_ALIGN_CEIL(sizeof(struct virtio_crypto_session), 16);
703 : : }
704 : :
705 : : static int
706 : 0 : virtio_crypto_check_sym_session_paras(
707 : : struct rte_cryptodev *dev)
708 : : {
709 : : struct virtio_crypto_hw *hw;
710 : :
711 : 0 : PMD_INIT_FUNC_TRACE();
712 : :
713 [ # # ]: 0 : if (unlikely(dev == NULL)) {
714 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("dev is NULL");
715 : 0 : return -1;
716 : : }
717 [ # # ]: 0 : if (unlikely(dev->data == NULL)) {
718 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("dev->data is NULL");
719 : 0 : return -1;
720 : : }
721 : 0 : hw = dev->data->dev_private;
722 [ # # ]: 0 : if (unlikely(hw == NULL)) {
723 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("hw is NULL");
724 : 0 : return -1;
725 : : }
726 [ # # ]: 0 : if (unlikely(hw->cvq == NULL)) {
727 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("vq is NULL");
728 : 0 : return -1;
729 : : }
730 : :
731 : : return 0;
732 : : }
733 : :
734 : : static int
735 : 0 : virtio_crypto_check_sym_clear_session_paras(
736 : : struct rte_cryptodev *dev,
737 : : struct rte_cryptodev_sym_session *sess)
738 : : {
739 : 0 : PMD_INIT_FUNC_TRACE();
740 : :
741 [ # # ]: 0 : if (sess == NULL) {
742 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("sym_session is NULL");
743 : 0 : return -1;
744 : : }
745 : :
746 : 0 : return virtio_crypto_check_sym_session_paras(dev);
747 : : }
748 : :
749 : : #define NUM_ENTRY_SYM_CLEAR_SESSION 2
750 : :
751 : : static void
752 : 0 : virtio_crypto_clear_session(
753 : : struct rte_cryptodev *dev,
754 : : struct virtio_crypto_op_ctrl_req *ctrl)
755 : : {
756 : : struct virtio_crypto_hw *hw;
757 : : struct virtqueue *vq;
758 : : struct vring_desc *desc;
759 : : uint8_t *status;
760 : : uint8_t needed = 1;
761 : : uint32_t head;
762 : : uint64_t malloc_phys_addr;
763 : : uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
764 : : uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
765 : : uint32_t desc_offset = len_op_ctrl_req + len_inhdr;
766 : 0 : uint64_t session_id = ctrl->u.destroy_session.session_id;
767 : :
768 : 0 : hw = dev->data->dev_private;
769 : 0 : vq = virtcrypto_cq_to_vq(hw->cvq);
770 : :
771 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("vq->vq_desc_head_idx = %d, "
772 : : "vq = %p", vq->vq_desc_head_idx, vq);
773 : :
774 [ # # ]: 0 : if (vq->vq_free_cnt < needed) {
775 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
776 : : "vq->vq_free_cnt = %d is less than %d, "
777 : : "not enough", vq->vq_free_cnt, needed);
778 : 0 : return;
779 : : }
780 : :
781 : 0 : malloc_phys_addr = rte_malloc_virt2iova(ctrl);
782 [ # # ]: 0 : if (malloc_phys_addr == RTE_BAD_IOVA) {
783 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("malloc_phys_addr is invalid");
784 : 0 : return;
785 : : }
786 : :
787 : : /* status part */
788 : : status = &(((struct virtio_crypto_inhdr *)
789 : : ((uint8_t *)ctrl + len_op_ctrl_req))->status);
790 : 0 : *status = VIRTIO_CRYPTO_ERR;
791 : :
792 : : /* indirect desc vring part */
793 : : desc = (struct vring_desc *)((uint8_t *)ctrl + desc_offset);
794 : :
795 : : /* ctrl request part */
796 : 0 : desc[0].addr = malloc_phys_addr;
797 : 0 : desc[0].len = len_op_ctrl_req;
798 : 0 : desc[0].flags = VRING_DESC_F_NEXT;
799 : 0 : desc[0].next = 1;
800 : :
801 : : /* status part */
802 : 0 : desc[1].addr = malloc_phys_addr + len_op_ctrl_req;
803 : 0 : desc[1].len = len_inhdr;
804 : 0 : desc[1].flags = VRING_DESC_F_WRITE;
805 : :
806 : : /* use only a single desc entry */
807 : 0 : head = vq->vq_desc_head_idx;
808 : 0 : vq->vq_split.ring.desc[head].flags = VRING_DESC_F_INDIRECT;
809 : 0 : vq->vq_split.ring.desc[head].addr = malloc_phys_addr + desc_offset;
810 : : vq->vq_split.ring.desc[head].len
811 : 0 : = NUM_ENTRY_SYM_CLEAR_SESSION
812 : : * sizeof(struct vring_desc);
813 : 0 : vq->vq_free_cnt -= needed;
814 : :
815 [ # # ]: 0 : vq->vq_desc_head_idx = vq->vq_split.ring.desc[head].next;
816 : :
817 : : vq_update_avail_ring(vq, head);
818 : : vq_update_avail_idx(vq);
819 : :
820 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_queue_index = %d",
821 : : vq->vq_queue_index);
822 : :
823 : : virtqueue_notify(vq);
824 : :
825 : : rte_rmb();
826 [ # # ]: 0 : while (vq->vq_used_cons_idx == vq->vq_split.ring.used->idx) {
827 : : rte_rmb();
828 : 0 : usleep(100);
829 : : }
830 : :
831 [ # # ]: 0 : while (vq->vq_used_cons_idx != vq->vq_split.ring.used->idx) {
832 : : uint32_t idx, desc_idx, used_idx;
833 : : struct vring_used_elem *uep;
834 : :
835 : 0 : used_idx = (uint32_t)(vq->vq_used_cons_idx
836 : 0 : & (vq->vq_nentries - 1));
837 : : uep = &vq->vq_split.ring.used->ring[used_idx];
838 : 0 : idx = (uint32_t) uep->id;
839 : : desc_idx = idx;
840 [ # # ]: 0 : while (vq->vq_split.ring.desc[desc_idx].flags
841 : : & VRING_DESC_F_NEXT) {
842 : 0 : desc_idx = vq->vq_split.ring.desc[desc_idx].next;
843 : 0 : vq->vq_free_cnt++;
844 : : }
845 : :
846 : 0 : vq->vq_split.ring.desc[desc_idx].next = vq->vq_desc_head_idx;
847 : 0 : vq->vq_desc_head_idx = idx;
848 : 0 : vq->vq_used_cons_idx++;
849 : 0 : vq->vq_free_cnt++;
850 : : }
851 : :
852 [ # # ]: 0 : if (*status != VIRTIO_CRYPTO_OK) {
853 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Close session failed "
854 : : "status=%"PRIu32", session_id=%"PRIu64"",
855 : : *status, session_id);
856 : 0 : rte_free(ctrl);
857 : 0 : return;
858 : : }
859 : :
860 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_free_cnt=%d", vq->vq_free_cnt);
861 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_desc_head_idx=%d", vq->vq_desc_head_idx);
862 : :
863 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Close session %"PRIu64" successfully ",
864 : : session_id);
865 : :
866 : 0 : rte_free(ctrl);
867 : : }
868 : :
869 : : static void
870 : 0 : virtio_crypto_clear_session_packed(
871 : : struct rte_cryptodev *dev,
872 : : struct virtio_crypto_op_ctrl_req *ctrl)
873 : : {
874 : : struct virtio_crypto_hw *hw;
875 : : struct virtqueue *vq;
876 : : struct vring_packed_desc *desc;
877 : : uint8_t *status;
878 : : uint8_t needed = 1;
879 : : uint32_t head;
880 : : uint64_t malloc_phys_addr;
881 : : uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
882 : : uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
883 : 0 : uint64_t session_id = ctrl->u.destroy_session.session_id;
884 : : uint16_t flags;
885 : : uint8_t nb_descs = 0;
886 : :
887 : 0 : hw = dev->data->dev_private;
888 : 0 : vq = virtcrypto_cq_to_vq(hw->cvq);
889 : 0 : head = vq->vq_avail_idx;
890 : 0 : flags = vq->vq_packed.cached_flags;
891 : :
892 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("vq->vq_desc_head_idx = %d, "
893 : : "vq = %p", vq->vq_desc_head_idx, vq);
894 : :
895 [ # # ]: 0 : if (vq->vq_free_cnt < needed) {
896 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
897 : : "vq->vq_free_cnt = %d is less than %d, "
898 : : "not enough", vq->vq_free_cnt, needed);
899 : 0 : return;
900 : : }
901 : :
902 : 0 : malloc_phys_addr = rte_malloc_virt2iova(ctrl);
903 [ # # ]: 0 : if (malloc_phys_addr == RTE_BAD_IOVA) {
904 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("malloc_phys_addr is invalid");
905 : 0 : return;
906 : : }
907 : :
908 : : /* status part */
909 : : status = &(((struct virtio_crypto_inhdr *)
910 : : ((uint8_t *)ctrl + len_op_ctrl_req))->status);
911 : 0 : *status = VIRTIO_CRYPTO_ERR;
912 : :
913 : : /* indirect desc vring part */
914 : 0 : desc = vq->vq_packed.ring.desc;
915 : :
916 : : /* ctrl request part */
917 : 0 : desc[head].addr = malloc_phys_addr;
918 : 0 : desc[head].len = len_op_ctrl_req;
919 : 0 : desc[head].flags = VRING_DESC_F_NEXT | vq->vq_packed.cached_flags;
920 : 0 : vq->vq_free_cnt--;
921 : : nb_descs++;
922 [ # # ]: 0 : if (++vq->vq_avail_idx >= vq->vq_nentries) {
923 : 0 : vq->vq_avail_idx -= vq->vq_nentries;
924 : 0 : vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
925 : : }
926 : :
927 : : /* status part */
928 : 0 : desc[vq->vq_avail_idx].addr = malloc_phys_addr + len_op_ctrl_req;
929 : 0 : desc[vq->vq_avail_idx].len = len_inhdr;
930 : 0 : desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE;
931 : 0 : vq->vq_free_cnt--;
932 : : nb_descs++;
933 [ # # ]: 0 : if (++vq->vq_avail_idx >= vq->vq_nentries) {
934 : 0 : vq->vq_avail_idx -= vq->vq_nentries;
935 : 0 : vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
936 : : }
937 : :
938 : 0 : virtqueue_store_flags_packed(&desc[head], VRING_DESC_F_NEXT | flags,
939 [ # # ]: 0 : vq->hw->weak_barriers);
940 : :
941 [ # # ]: 0 : virtio_wmb(vq->hw->weak_barriers);
942 : : virtqueue_notify(vq);
943 : :
944 : : /* wait for used desc in virtqueue
945 : : * desc_is_used has a load-acquire or rte_io_rmb inside
946 : : */
947 : : rte_rmb();
948 : : while (!desc_is_used(&desc[head], vq)) {
949 : : rte_rmb();
950 : 0 : usleep(100);
951 : : }
952 : :
953 : : /* now get used descriptors */
954 : 0 : vq->vq_free_cnt += nb_descs;
955 : 0 : vq->vq_used_cons_idx += nb_descs;
956 [ # # ]: 0 : if (vq->vq_used_cons_idx >= vq->vq_nentries) {
957 : 0 : vq->vq_used_cons_idx -= vq->vq_nentries;
958 : 0 : vq->vq_packed.used_wrap_counter ^= 1;
959 : : }
960 : :
961 : 0 : PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d "
962 : : "vq->vq_queue_idx=%d "
963 : : "vq->vq_avail_idx=%d "
964 : : "vq->vq_used_cons_idx=%d "
965 : : "vq->vq_packed.cached_flags=0x%x "
966 : : "vq->vq_packed.used_wrap_counter=%d",
967 : : vq->vq_free_cnt,
968 : : vq->vq_queue_index,
969 : : vq->vq_avail_idx,
970 : : vq->vq_used_cons_idx,
971 : : vq->vq_packed.cached_flags,
972 : : vq->vq_packed.used_wrap_counter);
973 : :
974 [ # # ]: 0 : if (*status != VIRTIO_CRYPTO_OK) {
975 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Close session failed "
976 : : "status=%"PRIu32", session_id=%"PRIu64"",
977 : : *status, session_id);
978 : 0 : rte_free(ctrl);
979 : 0 : return;
980 : : }
981 : :
982 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_free_cnt=%d "
983 : : "vq->vq_desc_head_idx=%d",
984 : : vq->vq_free_cnt, vq->vq_desc_head_idx);
985 : :
986 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Close session %"PRIu64" successfully ",
987 : : session_id);
988 : :
989 : 0 : rte_free(ctrl);
990 : : }
991 : :
992 : : static void
993 : 0 : virtio_crypto_sym_clear_session(
994 : : struct rte_cryptodev *dev,
995 : : struct rte_cryptodev_sym_session *sess)
996 : : {
997 : : uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
998 : : uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
999 : : struct virtio_crypto_op_ctrl_req *ctrl;
1000 : : struct virtio_crypto_session *session;
1001 : : uint8_t *malloc_virt_addr;
1002 : :
1003 : 0 : PMD_INIT_FUNC_TRACE();
1004 : :
1005 [ # # ]: 0 : if (virtio_crypto_check_sym_clear_session_paras(dev, sess) < 0)
1006 : : return;
1007 : :
1008 : : session = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
1009 : :
1010 : : /*
1011 : : * malloc memory to store information of ctrl request op,
1012 : : * returned status and desc vring
1013 : : */
1014 : 0 : malloc_virt_addr = rte_malloc(NULL, len_op_ctrl_req + len_inhdr
1015 : : + NUM_ENTRY_SYM_CLEAR_SESSION
1016 : : * sizeof(struct vring_desc), RTE_CACHE_LINE_SIZE);
1017 [ # # ]: 0 : if (malloc_virt_addr == NULL) {
1018 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("not enough heap room");
1019 : 0 : return;
1020 : : }
1021 : :
1022 : : /* assign ctrl request op part */
1023 : : ctrl = (struct virtio_crypto_op_ctrl_req *)malloc_virt_addr;
1024 : 0 : ctrl->header.opcode = VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION;
1025 : : /* default data virtqueue is 0 */
1026 : 0 : ctrl->header.queue_id = 0;
1027 : 0 : ctrl->u.destroy_session.session_id = session->session_id;
1028 : :
1029 [ # # ]: 0 : if (vtpci_with_packed_queue(dev->data->dev_private))
1030 : 0 : return virtio_crypto_clear_session_packed(dev, ctrl);
1031 : :
1032 : 0 : return virtio_crypto_clear_session(dev, ctrl);
1033 : : }
1034 : :
1035 : : static void
1036 : 0 : virtio_crypto_asym_clear_session(
1037 : : struct rte_cryptodev *dev,
1038 : : struct rte_cryptodev_asym_session *sess)
1039 : : {
1040 : : uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
1041 : : uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
1042 : : struct virtio_crypto_op_ctrl_req *ctrl;
1043 : : struct virtio_crypto_session *session;
1044 : : uint8_t *malloc_virt_addr;
1045 : :
1046 : 0 : PMD_INIT_FUNC_TRACE();
1047 : :
1048 : : session = CRYPTODEV_GET_ASYM_SESS_PRIV(sess);
1049 : :
1050 : : /*
1051 : : * malloc memory to store information of ctrl request op,
1052 : : * returned status and desc vring
1053 : : */
1054 : 0 : malloc_virt_addr = rte_malloc(NULL, len_op_ctrl_req + len_inhdr
1055 : : + NUM_ENTRY_SYM_CLEAR_SESSION
1056 : : * sizeof(struct vring_desc), RTE_CACHE_LINE_SIZE);
1057 [ # # ]: 0 : if (malloc_virt_addr == NULL) {
1058 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("not enough heap room");
1059 : 0 : return;
1060 : : }
1061 : :
1062 : : /* assign ctrl request op part */
1063 : : ctrl = (struct virtio_crypto_op_ctrl_req *)malloc_virt_addr;
1064 : 0 : ctrl->header.opcode = VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION;
1065 : : /* default data virtqueue is 0 */
1066 : 0 : ctrl->header.queue_id = 0;
1067 : 0 : ctrl->u.destroy_session.session_id = session->session_id;
1068 : :
1069 [ # # ]: 0 : if (vtpci_with_packed_queue(dev->data->dev_private))
1070 : 0 : return virtio_crypto_clear_session_packed(dev, ctrl);
1071 : :
1072 : 0 : return virtio_crypto_clear_session(dev, ctrl);
1073 : : }
1074 : :
1075 : : static struct rte_crypto_cipher_xform *
1076 : : virtio_crypto_get_cipher_xform(struct rte_crypto_sym_xform *xform)
1077 : : {
1078 : : do {
1079 [ # # # # : 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
# # ]
1080 : 0 : return &xform->cipher;
1081 : :
1082 : 0 : xform = xform->next;
1083 [ # # # # : 0 : } while (xform);
# # ]
1084 : :
1085 : : return NULL;
1086 : : }
1087 : :
1088 : : static struct rte_crypto_auth_xform *
1089 : : virtio_crypto_get_auth_xform(struct rte_crypto_sym_xform *xform)
1090 : : {
1091 : : do {
1092 [ # # # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH)
1093 : 0 : return &xform->auth;
1094 : :
1095 : 0 : xform = xform->next;
1096 [ # # # # ]: 0 : } while (xform);
1097 : :
1098 : : return NULL;
1099 : : }
1100 : :
1101 : : /** Get xform chain order */
1102 : : static int
1103 : 0 : virtio_crypto_get_chain_order(struct rte_crypto_sym_xform *xform)
1104 : : {
1105 [ # # ]: 0 : if (xform == NULL)
1106 : : return -1;
1107 : :
1108 : : /* Cipher Only */
1109 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1110 [ # # ]: 0 : xform->next == NULL)
1111 : : return VIRTIO_CRYPTO_CMD_CIPHER;
1112 : :
1113 : : /* Authentication Only */
1114 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1115 [ # # ]: 0 : xform->next == NULL)
1116 : : return VIRTIO_CRYPTO_CMD_AUTH;
1117 : :
1118 : : /* Authenticate then Cipher */
1119 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1120 [ # # ]: 0 : xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
1121 : : return VIRTIO_CRYPTO_CMD_HASH_CIPHER;
1122 : :
1123 : : /* Cipher then Authenticate */
1124 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1125 [ # # ]: 0 : xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
1126 : 0 : return VIRTIO_CRYPTO_CMD_CIPHER_HASH;
1127 : :
1128 : : return -1;
1129 : : }
1130 : :
1131 : : static int
1132 : 0 : virtio_crypto_sym_pad_cipher_param(
1133 : : struct virtio_crypto_cipher_session_para *para,
1134 : : struct rte_crypto_cipher_xform *cipher_xform)
1135 : : {
1136 [ # # ]: 0 : switch (cipher_xform->algo) {
1137 : 0 : case RTE_CRYPTO_CIPHER_AES_CBC:
1138 : 0 : para->algo = VIRTIO_CRYPTO_CIPHER_AES_CBC;
1139 : : break;
1140 : 0 : default:
1141 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Crypto: Unsupported "
1142 : : "Cipher alg %u", cipher_xform->algo);
1143 : 0 : return -1;
1144 : : }
1145 : :
1146 : 0 : para->keylen = cipher_xform->key.length;
1147 [ # # # ]: 0 : switch (cipher_xform->op) {
1148 : 0 : case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
1149 : 0 : para->op = VIRTIO_CRYPTO_OP_ENCRYPT;
1150 : 0 : break;
1151 : 0 : case RTE_CRYPTO_CIPHER_OP_DECRYPT:
1152 : 0 : para->op = VIRTIO_CRYPTO_OP_DECRYPT;
1153 : 0 : break;
1154 : 0 : default:
1155 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Unsupported cipher operation "
1156 : : "parameter");
1157 : 0 : return -1;
1158 : : }
1159 : :
1160 : : return 0;
1161 : : }
1162 : :
1163 : : static int
1164 : 0 : virtio_crypto_sym_pad_auth_param(
1165 : : struct virtio_crypto_op_ctrl_req *ctrl,
1166 : : struct rte_crypto_auth_xform *auth_xform)
1167 : : {
1168 : : uint32_t *algo;
1169 : : struct virtio_crypto_alg_chain_session_para *para =
1170 : : &(ctrl->u.sym_create_session.u.chain.para);
1171 : :
1172 [ # # # ]: 0 : switch (ctrl->u.sym_create_session.u.chain.para.hash_mode) {
1173 : 0 : case VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN:
1174 : 0 : algo = &(para->u.hash_param.algo);
1175 : 0 : break;
1176 : 0 : case VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH:
1177 : 0 : algo = &(para->u.mac_param.algo);
1178 : 0 : break;
1179 : 0 : default:
1180 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Unsupported hash mode %u "
1181 : : "specified",
1182 : : ctrl->u.sym_create_session.u.chain.para.hash_mode);
1183 : 0 : return -1;
1184 : : }
1185 : :
1186 [ # # ]: 0 : switch (auth_xform->algo) {
1187 : 0 : case RTE_CRYPTO_AUTH_SHA1_HMAC:
1188 : 0 : *algo = VIRTIO_CRYPTO_MAC_HMAC_SHA1;
1189 : : break;
1190 : 0 : default:
1191 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1192 : : "Crypto: Undefined Hash algo %u specified",
1193 : : auth_xform->algo);
1194 : 0 : return -1;
1195 : : }
1196 : :
1197 : 0 : return 0;
1198 : : }
1199 : :
1200 : : static int
1201 : 0 : virtio_crypto_sym_pad_op_ctrl_req(
1202 : : struct virtio_crypto_op_ctrl_req *ctrl,
1203 : : struct rte_crypto_sym_xform *xform, bool is_chainned,
1204 : : uint8_t *cipher_key_data, uint8_t *auth_key_data,
1205 : : struct virtio_crypto_session *session)
1206 : : {
1207 : : int ret;
1208 : : struct rte_crypto_auth_xform *auth_xform = NULL;
1209 : : struct rte_crypto_cipher_xform *cipher_xform = NULL;
1210 : :
1211 : : /* Get cipher xform from crypto xform chain */
1212 : : cipher_xform = virtio_crypto_get_cipher_xform(xform);
1213 [ # # ]: 0 : if (cipher_xform) {
1214 [ # # ]: 0 : if (cipher_xform->key.length > VIRTIO_CRYPTO_MAX_KEY_SIZE) {
1215 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1216 : : "cipher key size cannot be longer than %u",
1217 : : VIRTIO_CRYPTO_MAX_KEY_SIZE);
1218 : 0 : return -1;
1219 : : }
1220 [ # # ]: 0 : if (cipher_xform->iv.length > VIRTIO_CRYPTO_MAX_IV_SIZE) {
1221 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1222 : : "cipher IV size cannot be longer than %u",
1223 : : VIRTIO_CRYPTO_MAX_IV_SIZE);
1224 : 0 : return -1;
1225 : : }
1226 [ # # ]: 0 : if (is_chainned)
1227 : 0 : ret = virtio_crypto_sym_pad_cipher_param(
1228 : : &ctrl->u.sym_create_session.u.chain.para
1229 : : .cipher_param, cipher_xform);
1230 : : else
1231 : 0 : ret = virtio_crypto_sym_pad_cipher_param(
1232 : : &ctrl->u.sym_create_session.u.cipher.para,
1233 : : cipher_xform);
1234 : :
1235 [ # # ]: 0 : if (ret < 0) {
1236 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1237 : : "pad cipher parameter failed");
1238 : 0 : return -1;
1239 : : }
1240 : :
1241 : 0 : memcpy(cipher_key_data, cipher_xform->key.data,
1242 : 0 : cipher_xform->key.length);
1243 : :
1244 : 0 : session->iv.offset = cipher_xform->iv.offset;
1245 : 0 : session->iv.length = cipher_xform->iv.length;
1246 : : }
1247 : :
1248 : : /* Get auth xform from crypto xform chain */
1249 : : auth_xform = virtio_crypto_get_auth_xform(xform);
1250 [ # # ]: 0 : if (auth_xform) {
1251 : : /* FIXME: support VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED */
1252 : : struct virtio_crypto_alg_chain_session_para *para =
1253 : : &(ctrl->u.sym_create_session.u.chain.para);
1254 [ # # ]: 0 : if (auth_xform->key.length) {
1255 [ # # ]: 0 : if (auth_xform->key.length >
1256 : : VIRTIO_CRYPTO_MAX_KEY_SIZE) {
1257 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1258 : : "auth key size cannot be longer than %u",
1259 : : VIRTIO_CRYPTO_MAX_KEY_SIZE);
1260 : 0 : return -1;
1261 : : }
1262 : 0 : para->hash_mode = VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH;
1263 : 0 : para->u.mac_param.auth_key_len =
1264 : 0 : (uint32_t)auth_xform->key.length;
1265 : 0 : para->u.mac_param.hash_result_len =
1266 : 0 : auth_xform->digest_length;
1267 : 0 : memcpy(auth_key_data, auth_xform->key.data,
1268 : : auth_xform->key.length);
1269 : : } else {
1270 : 0 : para->hash_mode = VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN;
1271 : 0 : para->u.hash_param.hash_result_len =
1272 : 0 : auth_xform->digest_length;
1273 : : }
1274 : :
1275 : 0 : ret = virtio_crypto_sym_pad_auth_param(ctrl, auth_xform);
1276 [ # # ]: 0 : if (ret < 0) {
1277 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("pad auth parameter "
1278 : : "failed");
1279 : 0 : return -1;
1280 : : }
1281 : : }
1282 : :
1283 : : return 0;
1284 : : }
1285 : :
1286 : : static int
1287 : 0 : virtio_crypto_check_sym_configure_session_paras(
1288 : : struct rte_cryptodev *dev,
1289 : : struct rte_crypto_sym_xform *xform,
1290 : : struct rte_cryptodev_sym_session *sym_sess)
1291 : : {
1292 [ # # # # ]: 0 : if (unlikely(xform == NULL) || unlikely(sym_sess == NULL)) {
1293 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("NULL pointer");
1294 : 0 : return -1;
1295 : : }
1296 : :
1297 [ # # ]: 0 : if (virtio_crypto_check_sym_session_paras(dev) < 0)
1298 : 0 : return -1;
1299 : :
1300 : : return 0;
1301 : : }
1302 : :
1303 : : static int
1304 : 0 : virtio_crypto_check_asym_configure_session_paras(
1305 : : struct rte_cryptodev *dev,
1306 : : struct rte_crypto_asym_xform *xform,
1307 : : struct rte_cryptodev_asym_session *asym_sess)
1308 : : {
1309 [ # # # # ]: 0 : if (unlikely(xform == NULL) || unlikely(asym_sess == NULL)) {
1310 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("NULL pointer");
1311 : 0 : return -1;
1312 : : }
1313 : :
1314 [ # # ]: 0 : if (virtio_crypto_check_sym_session_paras(dev) < 0)
1315 : 0 : return -1;
1316 : :
1317 : : return 0;
1318 : : }
1319 : :
1320 : : static int
1321 : 0 : virtio_crypto_sym_configure_session(
1322 : : struct rte_cryptodev *dev,
1323 : : struct rte_crypto_sym_xform *xform,
1324 : : struct rte_cryptodev_sym_session *sess)
1325 : : {
1326 : 0 : uint8_t cipher_key_data[VIRTIO_CRYPTO_MAX_KEY_SIZE] = {0};
1327 : 0 : uint8_t auth_key_data[VIRTIO_CRYPTO_MAX_KEY_SIZE] = {0};
1328 : : struct virtio_crypto_op_ctrl_req *ctrl_req;
1329 : : struct virtio_crypto_session_input *input;
1330 : : struct virtio_crypto_session *session;
1331 : : enum virtio_crypto_cmd_id cmd_id;
1332 : : struct virtio_crypto_hw *hw;
1333 : : struct virtio_pmd_ctrl *ctrl;
1334 : : int dlen[2], dnum;
1335 : : int ret;
1336 : :
1337 : 0 : PMD_INIT_FUNC_TRACE();
1338 : :
1339 : 0 : ret = virtio_crypto_check_sym_configure_session_paras(dev, xform,
1340 : : sess);
1341 [ # # ]: 0 : if (ret < 0) {
1342 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid parameters");
1343 : 0 : return ret;
1344 : : }
1345 [ # # ]: 0 : session = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
1346 : : memset(session, 0, sizeof(struct virtio_crypto_session));
1347 : 0 : ctrl = &session->ctrl;
1348 : 0 : ctrl_req = &ctrl->hdr;
1349 : 0 : ctrl_req->header.opcode = VIRTIO_CRYPTO_CIPHER_CREATE_SESSION;
1350 : : /* FIXME: support multiqueue */
1351 : 0 : ctrl_req->header.queue_id = 0;
1352 : :
1353 : 0 : hw = dev->data->dev_private;
1354 : :
1355 : 0 : cmd_id = virtio_crypto_get_chain_order(xform);
1356 [ # # ]: 0 : if (cmd_id == VIRTIO_CRYPTO_CMD_CIPHER_HASH)
1357 : : ctrl_req->u.sym_create_session.u.chain.para.alg_chain_order
1358 : 0 : = VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
1359 [ # # ]: 0 : if (cmd_id == VIRTIO_CRYPTO_CMD_HASH_CIPHER)
1360 : : ctrl_req->u.sym_create_session.u.chain.para.alg_chain_order
1361 : 0 : = VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
1362 : :
1363 [ # # # ]: 0 : switch (cmd_id) {
1364 : : case VIRTIO_CRYPTO_CMD_CIPHER_HASH:
1365 : : case VIRTIO_CRYPTO_CMD_HASH_CIPHER: {
1366 : : struct rte_crypto_cipher_xform *cipher_xform = NULL;
1367 : : struct rte_crypto_auth_xform *auth_xform = NULL;
1368 : :
1369 : : cipher_xform = virtio_crypto_get_cipher_xform(xform);
1370 : : auth_xform = virtio_crypto_get_auth_xform(xform);
1371 : :
1372 : : ctrl_req->u.sym_create_session.op_type
1373 : 0 : = VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING;
1374 : :
1375 : 0 : ret = virtio_crypto_sym_pad_op_ctrl_req(ctrl_req,
1376 : : xform, true, cipher_key_data, auth_key_data, session);
1377 [ # # ]: 0 : if (ret < 0) {
1378 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1379 : : "padding sym op ctrl req failed");
1380 : 0 : goto error_out;
1381 : : }
1382 : :
1383 : 0 : dlen[0] = cipher_xform->key.length;
1384 : 0 : memcpy(ctrl->data, cipher_key_data, dlen[0]);
1385 : 0 : dlen[1] = auth_xform->key.length;
1386 : 0 : memcpy(ctrl->data + dlen[0], auth_key_data, dlen[1]);
1387 : : dnum = 2;
1388 : 0 : break;
1389 : : }
1390 : : case VIRTIO_CRYPTO_CMD_CIPHER: {
1391 : : struct rte_crypto_cipher_xform *cipher_xform = NULL;
1392 : :
1393 : : cipher_xform = virtio_crypto_get_cipher_xform(xform);
1394 : :
1395 : : ctrl_req->u.sym_create_session.op_type
1396 : 0 : = VIRTIO_CRYPTO_SYM_OP_CIPHER;
1397 : 0 : ret = virtio_crypto_sym_pad_op_ctrl_req(ctrl_req, xform,
1398 : : false, cipher_key_data, auth_key_data, session);
1399 [ # # ]: 0 : if (ret < 0) {
1400 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1401 : : "padding sym op ctrl req failed");
1402 : 0 : goto error_out;
1403 : : }
1404 : :
1405 : 0 : dlen[0] = cipher_xform->key.length;
1406 : 0 : memcpy(ctrl->data, cipher_key_data, dlen[0]);
1407 : : dnum = 1;
1408 : 0 : break;
1409 : : }
1410 : 0 : default:
1411 : : ret = -ENOTSUP;
1412 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1413 : : "Unsupported operation chain order parameter");
1414 : 0 : goto error_out;
1415 : : }
1416 : :
1417 : : input = &ctrl->input;
1418 : 0 : input->status = VIRTIO_CRYPTO_ERR;
1419 : 0 : input->session_id = ~0ULL;
1420 : :
1421 : 0 : ret = virtio_crypto_send_command(hw->cvq, ctrl, dlen, dnum);
1422 [ # # ]: 0 : if (ret < 0) {
1423 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("create session failed: %d", ret);
1424 : 0 : goto error_out;
1425 : : }
1426 : :
1427 : 0 : ctrl = hw->cvq->hdr_mz->addr;
1428 : : input = &ctrl->input;
1429 [ # # ]: 0 : if (input->status != VIRTIO_CRYPTO_OK) {
1430 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Something wrong on backend! "
1431 : : "status=%u, session_id=%" PRIu64 "",
1432 : : input->status, input->session_id);
1433 : 0 : goto error_out;
1434 : : } else {
1435 : 0 : session->session_id = input->session_id;
1436 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Create session successfully, "
1437 : : "session_id=%" PRIu64 "", input->session_id);
1438 : : }
1439 : :
1440 : 0 : return 0;
1441 : : error_out:
1442 : : return ret;
1443 : : }
1444 : :
1445 : : static size_t
1446 : 0 : tlv_encode(uint8_t *tlv, uint8_t type, uint8_t *data, size_t len)
1447 : : {
1448 : : uint8_t *lenval = tlv;
1449 : : size_t lenval_n = 0;
1450 : :
1451 [ # # ]: 0 : if (len > 65535) {
1452 : 0 : goto _exit;
1453 [ # # ]: 0 : } else if (len > 255) {
1454 : 0 : lenval_n = 4 + len;
1455 : 0 : lenval[0] = type;
1456 : 0 : lenval[1] = 0x82;
1457 : 0 : lenval[2] = (len & 0xFF00) >> 8;
1458 : 0 : lenval[3] = (len & 0xFF);
1459 [ # # ]: 0 : rte_memcpy(&lenval[4], data, len);
1460 [ # # ]: 0 : } else if (len > 127) {
1461 : 0 : lenval_n = 3 + len;
1462 : 0 : lenval[0] = type;
1463 : 0 : lenval[1] = 0x81;
1464 : 0 : lenval[2] = len;
1465 [ # # ]: 0 : rte_memcpy(&lenval[3], data, len);
1466 : : } else {
1467 : 0 : lenval_n = 2 + len;
1468 : 0 : lenval[0] = type;
1469 : 0 : lenval[1] = len;
1470 [ # # ]: 0 : rte_memcpy(&lenval[2], data, len);
1471 : : }
1472 : :
1473 : 0 : _exit:
1474 : 0 : return lenval_n;
1475 : : }
1476 : :
1477 : : static int
1478 : 0 : virtio_crypto_asym_rsa_xform_to_der(
1479 : : struct rte_crypto_asym_xform *xform,
1480 : : uint8_t *der)
1481 : : {
1482 : : uint8_t data[VIRTIO_CRYPTO_MAX_CTRL_DATA];
1483 : : uint8_t ver[3] = {0x02, 0x01, 0x00};
1484 : : size_t tlen, len;
1485 : : uint8_t *tlv;
1486 : :
1487 [ # # ]: 0 : if (xform->xform_type != RTE_CRYPTO_ASYM_XFORM_RSA)
1488 : : return -EINVAL;
1489 : :
1490 : : tlv = data;
1491 : : rte_memcpy(tlv, ver, RTE_DIM(ver));
1492 : : tlen = RTE_DIM(ver);
1493 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.n.data, xform->rsa.n.length);
1494 : 0 : tlen += len;
1495 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.e.data, xform->rsa.e.length);
1496 : 0 : tlen += len;
1497 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.d.data, xform->rsa.d.length);
1498 : 0 : tlen += len;
1499 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.p.data, xform->rsa.qt.p.length);
1500 : 0 : tlen += len;
1501 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.q.data, xform->rsa.qt.q.length);
1502 : 0 : tlen += len;
1503 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.dP.data, xform->rsa.qt.dP.length);
1504 : 0 : tlen += len;
1505 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.dQ.data, xform->rsa.qt.dQ.length);
1506 : 0 : tlen += len;
1507 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.qInv.data, xform->rsa.qt.qInv.length);
1508 : 0 : tlen += len;
1509 : :
1510 : : RTE_ASSERT(tlen < VIRTIO_CRYPTO_MAX_CTRL_DATA);
1511 : 0 : len = tlv_encode(der, 0x30, data, tlen);
1512 : 0 : return len;
1513 : : }
1514 : :
1515 : : static int
1516 : 0 : virtio_crypto_asym_rsa_xform_to_public_der(
1517 : : struct rte_crypto_asym_xform *xform,
1518 : : uint8_t *der)
1519 : : {
1520 : : uint8_t data[VIRTIO_CRYPTO_MAX_CTRL_DATA];
1521 : : size_t tlen = 0, len;
1522 : : uint8_t *tlv;
1523 : :
1524 [ # # ]: 0 : if (xform->xform_type != RTE_CRYPTO_ASYM_XFORM_RSA)
1525 : : return -EINVAL;
1526 : :
1527 : : tlv = data;
1528 : 0 : len = tlv_encode(tlv, 0x02, xform->rsa.n.data, xform->rsa.n.length);
1529 : : tlen += len;
1530 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.e.data, xform->rsa.e.length);
1531 : 0 : tlen += len;
1532 : :
1533 : : RTE_ASSERT(tlen < VIRTIO_CRYPTO_MAX_CTRL_DATA);
1534 : 0 : len = tlv_encode(der, 0x30, data, tlen);
1535 : 0 : return len;
1536 : : }
1537 : :
1538 : : static int
1539 : 0 : virtio_crypto_asym_rsa_configure_session(
1540 : : struct rte_crypto_rsa_xform *rsa,
1541 : : struct virtio_crypto_akcipher_session_para *para)
1542 : : {
1543 : 0 : para->algo = VIRTIO_CRYPTO_AKCIPHER_RSA;
1544 [ # # ]: 0 : if (rsa->key_type == RTE_RSA_KEY_TYPE_EXP)
1545 : 0 : para->keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC;
1546 : : else
1547 : 0 : para->keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE;
1548 : :
1549 [ # # ]: 0 : if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) {
1550 : 0 : para->u.rsa.padding_algo = VIRTIO_CRYPTO_RSA_RAW_PADDING;
1551 [ # # ]: 0 : } else if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_PKCS1_5) {
1552 : 0 : para->u.rsa.padding_algo = VIRTIO_CRYPTO_RSA_PKCS1_PADDING;
1553 [ # # # # : 0 : switch (rsa->padding.hash) {
# # ]
1554 : 0 : case RTE_CRYPTO_AUTH_SHA1:
1555 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA1;
1556 : 0 : break;
1557 : 0 : case RTE_CRYPTO_AUTH_SHA224:
1558 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA224;
1559 : 0 : break;
1560 : 0 : case RTE_CRYPTO_AUTH_SHA256:
1561 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA256;
1562 : 0 : break;
1563 : 0 : case RTE_CRYPTO_AUTH_SHA512:
1564 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA512;
1565 : 0 : break;
1566 : 0 : case RTE_CRYPTO_AUTH_MD5:
1567 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_MD5;
1568 : 0 : break;
1569 : 0 : default:
1570 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_NO_HASH;
1571 : : }
1572 : : } else {
1573 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid padding type");
1574 : 0 : return -EINVAL;
1575 : : }
1576 : :
1577 : : return 0;
1578 : : }
1579 : :
1580 : : static int
1581 : 0 : virtio_crypto_asym_configure_session(
1582 : : struct rte_cryptodev *dev,
1583 : : struct rte_crypto_asym_xform *xform,
1584 : : struct rte_cryptodev_asym_session *sess)
1585 : : {
1586 : : struct virtio_crypto_akcipher_session_para *para;
1587 : : struct virtio_crypto_op_ctrl_req *ctrl_req;
1588 : : struct virtio_crypto_session_input *input;
1589 : : struct virtio_crypto_session *session;
1590 : : struct virtio_crypto_hw *hw;
1591 : : struct virtio_pmd_ctrl *ctrl;
1592 : : int dlen[1];
1593 : : int ret;
1594 : :
1595 : 0 : PMD_INIT_FUNC_TRACE();
1596 : :
1597 : 0 : ret = virtio_crypto_check_asym_configure_session_paras(dev, xform,
1598 : : sess);
1599 [ # # ]: 0 : if (ret < 0) {
1600 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid parameters");
1601 : 0 : return ret;
1602 : : }
1603 : :
1604 [ # # ]: 0 : session = CRYPTODEV_GET_ASYM_SESS_PRIV(sess);
1605 : : memset(session, 0, sizeof(struct virtio_crypto_session));
1606 : 0 : ctrl = &session->ctrl;
1607 : : ctrl_req = &ctrl->hdr;
1608 : 0 : ctrl_req->header.opcode = VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION;
1609 : 0 : ctrl_req->header.queue_id = 0;
1610 : 0 : para = &ctrl_req->u.akcipher_create_session.para;
1611 : :
1612 [ # # ]: 0 : switch (xform->xform_type) {
1613 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:
1614 : 0 : ctrl_req->header.algo = VIRTIO_CRYPTO_AKCIPHER_RSA;
1615 : 0 : ret = virtio_crypto_asym_rsa_configure_session(&xform->rsa, para);
1616 [ # # ]: 0 : if (ret < 0) {
1617 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid RSA parameters");
1618 : 0 : return ret;
1619 : : }
1620 : :
1621 [ # # ]: 0 : if (xform->rsa.key_type == RTE_RSA_KEY_TYPE_EXP) {
1622 : 0 : ret = virtio_crypto_asym_rsa_xform_to_public_der(
1623 : 0 : xform, ctrl->data);
1624 : : } else {
1625 : 0 : ret = virtio_crypto_asym_rsa_xform_to_der(xform,
1626 : 0 : ctrl->data);
1627 : : }
1628 [ # # ]: 0 : if (ret <= 0) {
1629 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid RSA primitives");
1630 : 0 : return ret;
1631 : : }
1632 : :
1633 : 0 : ctrl_req->u.akcipher_create_session.para.keylen = ret;
1634 : 0 : break;
1635 : 0 : default:
1636 : 0 : para->algo = VIRTIO_CRYPTO_NO_AKCIPHER;
1637 : : }
1638 : :
1639 : 0 : dlen[0] = ret;
1640 : : input = &ctrl->input;
1641 : 0 : input->status = VIRTIO_CRYPTO_ERR;
1642 : 0 : input->session_id = ~0ULL;
1643 : :
1644 : 0 : hw = dev->data->dev_private;
1645 : 0 : ret = virtio_crypto_send_command(hw->cvq, ctrl, dlen, 1);
1646 [ # # ]: 0 : if (ret < 0) {
1647 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("create session failed: %d", ret);
1648 : 0 : goto error_out;
1649 : : }
1650 : :
1651 : 0 : ctrl = hw->cvq->hdr_mz->addr;
1652 : : input = &ctrl->input;
1653 [ # # ]: 0 : if (input->status != VIRTIO_CRYPTO_OK) {
1654 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Something wrong on backend! "
1655 : : "status=%u, session_id=%" PRIu64 "",
1656 : : input->status, input->session_id);
1657 : 0 : goto error_out;
1658 : : } else {
1659 : 0 : session->session_id = input->session_id;
1660 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Create session successfully, "
1661 : : "session_id=%" PRIu64 "", input->session_id);
1662 : : }
1663 : :
1664 : 0 : return 0;
1665 : : error_out:
1666 : : return -1;
1667 : : }
1668 : :
1669 : : static void
1670 : 0 : virtio_crypto_dev_info_get(struct rte_cryptodev *dev,
1671 : : struct rte_cryptodev_info *info)
1672 : : {
1673 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
1674 : :
1675 : 0 : PMD_INIT_FUNC_TRACE();
1676 : :
1677 [ # # ]: 0 : if (info != NULL) {
1678 : 0 : info->driver_id = dev->driver_id;
1679 : 0 : info->feature_flags = dev->feature_flags;
1680 : 0 : info->max_nb_queue_pairs = hw->max_dataqueues;
1681 : : /* No limit of number of sessions */
1682 : 0 : info->sym.max_nb_sessions = 0;
1683 : 0 : info->capabilities = hw->virtio_dev_capabilities;
1684 : : }
1685 : 0 : }
1686 : :
1687 : : static int
1688 : 0 : crypto_virtio_pci_probe(
1689 : : struct rte_pci_driver *pci_drv __rte_unused,
1690 : : struct rte_pci_device *pci_dev)
1691 : : {
1692 : 0 : struct rte_cryptodev_pmd_init_params init_params = {
1693 : : .name = "",
1694 : 0 : .socket_id = pci_dev->device.numa_node,
1695 : : .private_data_size = sizeof(struct virtio_crypto_hw)
1696 : : };
1697 : : char name[RTE_CRYPTODEV_NAME_MAX_LEN];
1698 : :
1699 : 0 : VIRTIO_CRYPTO_DRV_LOG_DBG("Found Crypto device at %02x:%02x.%x",
1700 : : pci_dev->addr.bus,
1701 : : pci_dev->addr.devid,
1702 : : pci_dev->addr.function);
1703 : :
1704 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
1705 : :
1706 : 0 : return crypto_virtio_create(name, pci_dev, &init_params);
1707 : : }
1708 : :
1709 : : static int
1710 : 0 : crypto_virtio_pci_remove(
1711 : : struct rte_pci_device *pci_dev __rte_unused)
1712 : : {
1713 : : struct rte_cryptodev *cryptodev;
1714 : : char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1715 : :
1716 [ # # ]: 0 : if (pci_dev == NULL)
1717 : : return -EINVAL;
1718 : :
1719 : 0 : rte_pci_device_name(&pci_dev->addr, cryptodev_name,
1720 : : sizeof(cryptodev_name));
1721 : :
1722 : 0 : cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
1723 [ # # ]: 0 : if (cryptodev == NULL)
1724 : : return -ENODEV;
1725 : :
1726 : 0 : return virtio_crypto_dev_uninit(cryptodev);
1727 : : }
1728 : :
1729 : : static struct rte_pci_driver rte_virtio_crypto_driver = {
1730 : : .id_table = pci_id_virtio_crypto_map,
1731 : : .drv_flags = 0,
1732 : : .probe = crypto_virtio_pci_probe,
1733 : : .remove = crypto_virtio_pci_remove
1734 : : };
1735 : :
1736 : : static struct cryptodev_driver virtio_crypto_drv;
1737 : :
1738 : 303 : RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_VIRTIO_PMD, rte_virtio_crypto_driver);
1739 : 303 : RTE_PMD_REGISTER_CRYPTO_DRIVER(virtio_crypto_drv,
1740 : : rte_virtio_crypto_driver.driver,
1741 : : cryptodev_virtio_driver_id);
1742 [ - + ]: 303 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_init, init, NOTICE);
1743 [ - + ]: 303 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_session, session, NOTICE);
1744 [ - + ]: 303 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_rx, rx, NOTICE);
1745 [ - + ]: 303 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_tx, tx, NOTICE);
1746 [ - + ]: 303 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_driver, driver, NOTICE);
|