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