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 : :
803 : : /* status part */
804 : : status = &(((struct virtio_crypto_inhdr *)
805 : : ((uint8_t *)ctrl + len_op_ctrl_req))->status);
806 : 0 : *status = VIRTIO_CRYPTO_ERR;
807 : :
808 : : /* indirect desc vring part */
809 : : desc = (struct vring_desc *)((uint8_t *)ctrl + desc_offset);
810 : :
811 : : /* ctrl request part */
812 : 0 : desc[0].addr = malloc_phys_addr;
813 : 0 : desc[0].len = len_op_ctrl_req;
814 : 0 : desc[0].flags = VRING_DESC_F_NEXT;
815 : 0 : desc[0].next = 1;
816 : :
817 : : /* status part */
818 : 0 : desc[1].addr = malloc_phys_addr + len_op_ctrl_req;
819 : 0 : desc[1].len = len_inhdr;
820 : 0 : desc[1].flags = VRING_DESC_F_WRITE;
821 : :
822 : : /* use only a single desc entry */
823 : 0 : head = vq->vq_desc_head_idx;
824 : 0 : vq->vq_split.ring.desc[head].flags = VRING_DESC_F_INDIRECT;
825 : 0 : vq->vq_split.ring.desc[head].addr = malloc_phys_addr + desc_offset;
826 : : vq->vq_split.ring.desc[head].len
827 : 0 : = NUM_ENTRY_SYM_CLEAR_SESSION
828 : : * sizeof(struct vring_desc);
829 : 0 : vq->vq_free_cnt -= needed;
830 : :
831 [ # # ]: 0 : vq->vq_desc_head_idx = vq->vq_split.ring.desc[head].next;
832 : :
833 : : vq_update_avail_ring(vq, head);
834 : : vq_update_avail_idx(vq);
835 : :
836 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_queue_index = %d",
837 : : vq->vq_queue_index);
838 : :
839 : : virtqueue_notify(vq);
840 : :
841 : : rte_rmb();
842 [ # # ]: 0 : while (vq->vq_used_cons_idx == vq->vq_split.ring.used->idx) {
843 : : rte_rmb();
844 : 0 : usleep(100);
845 : : }
846 : :
847 [ # # ]: 0 : while (vq->vq_used_cons_idx != vq->vq_split.ring.used->idx) {
848 : : uint32_t idx, desc_idx, used_idx;
849 : : struct vring_used_elem *uep;
850 : :
851 : 0 : used_idx = (uint32_t)(vq->vq_used_cons_idx
852 : 0 : & (vq->vq_nentries - 1));
853 : : uep = &vq->vq_split.ring.used->ring[used_idx];
854 : 0 : idx = (uint32_t) uep->id;
855 : : desc_idx = idx;
856 [ # # ]: 0 : while (vq->vq_split.ring.desc[desc_idx].flags
857 : : & VRING_DESC_F_NEXT) {
858 : 0 : desc_idx = vq->vq_split.ring.desc[desc_idx].next;
859 : 0 : vq->vq_free_cnt++;
860 : : }
861 : :
862 : 0 : vq->vq_split.ring.desc[desc_idx].next = vq->vq_desc_head_idx;
863 : 0 : vq->vq_desc_head_idx = idx;
864 : 0 : vq->vq_used_cons_idx++;
865 : 0 : vq->vq_free_cnt++;
866 : : }
867 : :
868 [ # # ]: 0 : if (*status != VIRTIO_CRYPTO_OK) {
869 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Close session failed "
870 : : "status=%"PRIu32", session_id=%"PRIu64"",
871 : : *status, session_id);
872 : 0 : rte_free(ctrl);
873 : 0 : return;
874 : : }
875 : :
876 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_free_cnt=%d", vq->vq_free_cnt);
877 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_desc_head_idx=%d", vq->vq_desc_head_idx);
878 : :
879 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Close session %"PRIu64" successfully ",
880 : : session_id);
881 : :
882 : 0 : rte_free(ctrl);
883 : : }
884 : :
885 : : static void
886 : 0 : virtio_crypto_clear_session_packed(
887 : : struct rte_cryptodev *dev,
888 : : struct virtio_crypto_op_ctrl_req *ctrl)
889 : : {
890 : : struct virtio_crypto_hw *hw;
891 : : struct virtqueue *vq;
892 : : struct vring_packed_desc *desc;
893 : : uint8_t *status;
894 : : uint8_t needed = 1;
895 : : uint32_t head;
896 : : uint64_t malloc_phys_addr;
897 : : uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
898 : : uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
899 : 0 : uint64_t session_id = ctrl->u.destroy_session.session_id;
900 : : uint16_t flags;
901 : : uint8_t nb_descs = 0;
902 : :
903 : 0 : hw = dev->data->dev_private;
904 : 0 : vq = virtcrypto_cq_to_vq(hw->cvq);
905 : 0 : head = vq->vq_avail_idx;
906 : 0 : flags = vq->vq_packed.cached_flags;
907 : :
908 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("vq->vq_desc_head_idx = %d, "
909 : : "vq = %p", vq->vq_desc_head_idx, vq);
910 : :
911 [ # # ]: 0 : if (vq->vq_free_cnt < needed) {
912 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
913 : : "vq->vq_free_cnt = %d is less than %d, "
914 : : "not enough", vq->vq_free_cnt, needed);
915 : 0 : return;
916 : : }
917 : :
918 : 0 : malloc_phys_addr = rte_malloc_virt2iova(ctrl);
919 : :
920 : : /* status part */
921 : : status = &(((struct virtio_crypto_inhdr *)
922 : : ((uint8_t *)ctrl + len_op_ctrl_req))->status);
923 : 0 : *status = VIRTIO_CRYPTO_ERR;
924 : :
925 : : /* indirect desc vring part */
926 : 0 : desc = vq->vq_packed.ring.desc;
927 : :
928 : : /* ctrl request part */
929 : 0 : desc[head].addr = malloc_phys_addr;
930 : 0 : desc[head].len = len_op_ctrl_req;
931 : 0 : desc[head].flags = VRING_DESC_F_NEXT | vq->vq_packed.cached_flags;
932 : 0 : vq->vq_free_cnt--;
933 : : nb_descs++;
934 [ # # ]: 0 : if (++vq->vq_avail_idx >= vq->vq_nentries) {
935 : 0 : vq->vq_avail_idx -= vq->vq_nentries;
936 : 0 : vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
937 : : }
938 : :
939 : : /* status part */
940 : 0 : desc[vq->vq_avail_idx].addr = malloc_phys_addr + len_op_ctrl_req;
941 : 0 : desc[vq->vq_avail_idx].len = len_inhdr;
942 : 0 : desc[vq->vq_avail_idx].flags = VRING_DESC_F_WRITE;
943 : 0 : vq->vq_free_cnt--;
944 : : nb_descs++;
945 [ # # ]: 0 : if (++vq->vq_avail_idx >= vq->vq_nentries) {
946 : 0 : vq->vq_avail_idx -= vq->vq_nentries;
947 : 0 : vq->vq_packed.cached_flags ^= VRING_PACKED_DESC_F_AVAIL_USED;
948 : : }
949 : :
950 : 0 : virtqueue_store_flags_packed(&desc[head], VRING_DESC_F_NEXT | flags,
951 [ # # ]: 0 : vq->hw->weak_barriers);
952 : :
953 [ # # ]: 0 : virtio_wmb(vq->hw->weak_barriers);
954 : : virtqueue_notify(vq);
955 : :
956 : : /* wait for used desc in virtqueue
957 : : * desc_is_used has a load-acquire or rte_io_rmb inside
958 : : */
959 : : rte_rmb();
960 : : while (!desc_is_used(&desc[head], vq)) {
961 : : rte_rmb();
962 : 0 : usleep(100);
963 : : }
964 : :
965 : : /* now get used descriptors */
966 : 0 : vq->vq_free_cnt += nb_descs;
967 : 0 : vq->vq_used_cons_idx += nb_descs;
968 [ # # ]: 0 : if (vq->vq_used_cons_idx >= vq->vq_nentries) {
969 : 0 : vq->vq_used_cons_idx -= vq->vq_nentries;
970 : 0 : vq->vq_packed.used_wrap_counter ^= 1;
971 : : }
972 : :
973 : 0 : PMD_INIT_LOG(DEBUG, "vq->vq_free_cnt=%d "
974 : : "vq->vq_queue_idx=%d "
975 : : "vq->vq_avail_idx=%d "
976 : : "vq->vq_used_cons_idx=%d "
977 : : "vq->vq_packed.cached_flags=0x%x "
978 : : "vq->vq_packed.used_wrap_counter=%d",
979 : : vq->vq_free_cnt,
980 : : vq->vq_queue_index,
981 : : vq->vq_avail_idx,
982 : : vq->vq_used_cons_idx,
983 : : vq->vq_packed.cached_flags,
984 : : vq->vq_packed.used_wrap_counter);
985 : :
986 [ # # ]: 0 : if (*status != VIRTIO_CRYPTO_OK) {
987 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Close session failed "
988 : : "status=%"PRIu32", session_id=%"PRIu64"",
989 : : *status, session_id);
990 : 0 : rte_free(ctrl);
991 : 0 : return;
992 : : }
993 : :
994 : 0 : VIRTIO_CRYPTO_INIT_LOG_DBG("vq->vq_free_cnt=%d "
995 : : "vq->vq_desc_head_idx=%d",
996 : : vq->vq_free_cnt, vq->vq_desc_head_idx);
997 : :
998 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Close session %"PRIu64" successfully ",
999 : : session_id);
1000 : :
1001 : 0 : rte_free(ctrl);
1002 : : }
1003 : :
1004 : : static void
1005 : 0 : virtio_crypto_sym_clear_session(
1006 : : struct rte_cryptodev *dev,
1007 : : struct rte_cryptodev_sym_session *sess)
1008 : : {
1009 : : uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
1010 : : uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
1011 : : struct virtio_crypto_op_ctrl_req *ctrl;
1012 : : struct virtio_crypto_session *session;
1013 : : uint8_t *malloc_virt_addr;
1014 : :
1015 : 0 : PMD_INIT_FUNC_TRACE();
1016 : :
1017 [ # # ]: 0 : if (virtio_crypto_check_sym_clear_session_paras(dev, sess) < 0)
1018 : : return;
1019 : :
1020 : : session = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
1021 : :
1022 : : /*
1023 : : * malloc memory to store information of ctrl request op,
1024 : : * returned status and desc vring
1025 : : */
1026 : 0 : malloc_virt_addr = rte_malloc(NULL, len_op_ctrl_req + len_inhdr
1027 : : + NUM_ENTRY_SYM_CLEAR_SESSION
1028 : : * sizeof(struct vring_desc), RTE_CACHE_LINE_SIZE);
1029 [ # # ]: 0 : if (malloc_virt_addr == NULL) {
1030 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("not enough heap room");
1031 : 0 : return;
1032 : : }
1033 : :
1034 : : /* assign ctrl request op part */
1035 : : ctrl = (struct virtio_crypto_op_ctrl_req *)malloc_virt_addr;
1036 : 0 : ctrl->header.opcode = VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION;
1037 : : /* default data virtqueue is 0 */
1038 : 0 : ctrl->header.queue_id = 0;
1039 : 0 : ctrl->u.destroy_session.session_id = session->session_id;
1040 : :
1041 [ # # ]: 0 : if (vtpci_with_packed_queue(dev->data->dev_private))
1042 : 0 : return virtio_crypto_clear_session_packed(dev, ctrl);
1043 : :
1044 : 0 : return virtio_crypto_clear_session(dev, ctrl);
1045 : : }
1046 : :
1047 : : static void
1048 : 0 : virtio_crypto_asym_clear_session(
1049 : : struct rte_cryptodev *dev,
1050 : : struct rte_cryptodev_asym_session *sess)
1051 : : {
1052 : : uint32_t len_op_ctrl_req = sizeof(struct virtio_crypto_op_ctrl_req);
1053 : : uint8_t len_inhdr = sizeof(struct virtio_crypto_inhdr);
1054 : : struct virtio_crypto_op_ctrl_req *ctrl;
1055 : : struct virtio_crypto_session *session;
1056 : : uint8_t *malloc_virt_addr;
1057 : :
1058 : 0 : PMD_INIT_FUNC_TRACE();
1059 : :
1060 : : session = CRYPTODEV_GET_ASYM_SESS_PRIV(sess);
1061 : :
1062 : : /*
1063 : : * malloc memory to store information of ctrl request op,
1064 : : * returned status and desc vring
1065 : : */
1066 : 0 : malloc_virt_addr = rte_malloc(NULL, len_op_ctrl_req + len_inhdr
1067 : : + NUM_ENTRY_SYM_CLEAR_SESSION
1068 : : * sizeof(struct vring_desc), RTE_CACHE_LINE_SIZE);
1069 [ # # ]: 0 : if (malloc_virt_addr == NULL) {
1070 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("not enough heap room");
1071 : 0 : return;
1072 : : }
1073 : :
1074 : : /* assign ctrl request op part */
1075 : : ctrl = (struct virtio_crypto_op_ctrl_req *)malloc_virt_addr;
1076 : 0 : ctrl->header.opcode = VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION;
1077 : : /* default data virtqueue is 0 */
1078 : 0 : ctrl->header.queue_id = 0;
1079 : 0 : ctrl->u.destroy_session.session_id = session->session_id;
1080 : :
1081 [ # # ]: 0 : if (vtpci_with_packed_queue(dev->data->dev_private))
1082 : 0 : return virtio_crypto_clear_session_packed(dev, ctrl);
1083 : :
1084 : 0 : return virtio_crypto_clear_session(dev, ctrl);
1085 : : }
1086 : :
1087 : : static struct rte_crypto_cipher_xform *
1088 : : virtio_crypto_get_cipher_xform(struct rte_crypto_sym_xform *xform)
1089 : : {
1090 : : do {
1091 [ # # # # : 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
# # ]
1092 : 0 : return &xform->cipher;
1093 : :
1094 : 0 : xform = xform->next;
1095 [ # # # # : 0 : } while (xform);
# # ]
1096 : :
1097 : : return NULL;
1098 : : }
1099 : :
1100 : : static struct rte_crypto_auth_xform *
1101 : : virtio_crypto_get_auth_xform(struct rte_crypto_sym_xform *xform)
1102 : : {
1103 : : do {
1104 [ # # # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH)
1105 : 0 : return &xform->auth;
1106 : :
1107 : 0 : xform = xform->next;
1108 [ # # # # ]: 0 : } while (xform);
1109 : :
1110 : : return NULL;
1111 : : }
1112 : :
1113 : : /** Get xform chain order */
1114 : : static int
1115 : 0 : virtio_crypto_get_chain_order(struct rte_crypto_sym_xform *xform)
1116 : : {
1117 [ # # ]: 0 : if (xform == NULL)
1118 : : return -1;
1119 : :
1120 : : /* Cipher Only */
1121 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1122 [ # # ]: 0 : xform->next == NULL)
1123 : : return VIRTIO_CRYPTO_CMD_CIPHER;
1124 : :
1125 : : /* Authentication Only */
1126 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1127 [ # # ]: 0 : xform->next == NULL)
1128 : : return VIRTIO_CRYPTO_CMD_AUTH;
1129 : :
1130 : : /* Authenticate then Cipher */
1131 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
1132 [ # # ]: 0 : xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
1133 : : return VIRTIO_CRYPTO_CMD_HASH_CIPHER;
1134 : :
1135 : : /* Cipher then Authenticate */
1136 [ # # ]: 0 : if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
1137 [ # # ]: 0 : xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
1138 : 0 : return VIRTIO_CRYPTO_CMD_CIPHER_HASH;
1139 : :
1140 : : return -1;
1141 : : }
1142 : :
1143 : : static int
1144 : 0 : virtio_crypto_sym_pad_cipher_param(
1145 : : struct virtio_crypto_cipher_session_para *para,
1146 : : struct rte_crypto_cipher_xform *cipher_xform)
1147 : : {
1148 [ # # ]: 0 : switch (cipher_xform->algo) {
1149 : 0 : case RTE_CRYPTO_CIPHER_AES_CBC:
1150 : 0 : para->algo = VIRTIO_CRYPTO_CIPHER_AES_CBC;
1151 : : break;
1152 : 0 : default:
1153 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Crypto: Unsupported "
1154 : : "Cipher alg %u", cipher_xform->algo);
1155 : 0 : return -1;
1156 : : }
1157 : :
1158 : 0 : para->keylen = cipher_xform->key.length;
1159 [ # # # ]: 0 : switch (cipher_xform->op) {
1160 : 0 : case RTE_CRYPTO_CIPHER_OP_ENCRYPT:
1161 : 0 : para->op = VIRTIO_CRYPTO_OP_ENCRYPT;
1162 : 0 : break;
1163 : 0 : case RTE_CRYPTO_CIPHER_OP_DECRYPT:
1164 : 0 : para->op = VIRTIO_CRYPTO_OP_DECRYPT;
1165 : 0 : break;
1166 : 0 : default:
1167 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Unsupported cipher operation "
1168 : : "parameter");
1169 : 0 : return -1;
1170 : : }
1171 : :
1172 : : return 0;
1173 : : }
1174 : :
1175 : : static int
1176 : 0 : virtio_crypto_sym_pad_auth_param(
1177 : : struct virtio_crypto_op_ctrl_req *ctrl,
1178 : : struct rte_crypto_auth_xform *auth_xform)
1179 : : {
1180 : : uint32_t *algo;
1181 : : struct virtio_crypto_alg_chain_session_para *para =
1182 : : &(ctrl->u.sym_create_session.u.chain.para);
1183 : :
1184 [ # # # ]: 0 : switch (ctrl->u.sym_create_session.u.chain.para.hash_mode) {
1185 : 0 : case VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN:
1186 : 0 : algo = &(para->u.hash_param.algo);
1187 : 0 : break;
1188 : 0 : case VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH:
1189 : 0 : algo = &(para->u.mac_param.algo);
1190 : 0 : break;
1191 : 0 : default:
1192 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Unsupported hash mode %u "
1193 : : "specified",
1194 : : ctrl->u.sym_create_session.u.chain.para.hash_mode);
1195 : 0 : return -1;
1196 : : }
1197 : :
1198 [ # # ]: 0 : switch (auth_xform->algo) {
1199 : 0 : case RTE_CRYPTO_AUTH_SHA1_HMAC:
1200 : 0 : *algo = VIRTIO_CRYPTO_MAC_HMAC_SHA1;
1201 : : break;
1202 : 0 : default:
1203 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1204 : : "Crypto: Undefined Hash algo %u specified",
1205 : : auth_xform->algo);
1206 : 0 : return -1;
1207 : : }
1208 : :
1209 : 0 : return 0;
1210 : : }
1211 : :
1212 : : static int
1213 : 0 : virtio_crypto_sym_pad_op_ctrl_req(
1214 : : struct virtio_crypto_op_ctrl_req *ctrl,
1215 : : struct rte_crypto_sym_xform *xform, bool is_chainned,
1216 : : uint8_t *cipher_key_data, uint8_t *auth_key_data,
1217 : : struct virtio_crypto_session *session)
1218 : : {
1219 : : int ret;
1220 : : struct rte_crypto_auth_xform *auth_xform = NULL;
1221 : : struct rte_crypto_cipher_xform *cipher_xform = NULL;
1222 : :
1223 : : /* Get cipher xform from crypto xform chain */
1224 : : cipher_xform = virtio_crypto_get_cipher_xform(xform);
1225 [ # # ]: 0 : if (cipher_xform) {
1226 [ # # ]: 0 : if (cipher_xform->key.length > VIRTIO_CRYPTO_MAX_KEY_SIZE) {
1227 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1228 : : "cipher key size cannot be longer than %u",
1229 : : VIRTIO_CRYPTO_MAX_KEY_SIZE);
1230 : 0 : return -1;
1231 : : }
1232 [ # # ]: 0 : if (cipher_xform->iv.length > VIRTIO_CRYPTO_MAX_IV_SIZE) {
1233 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1234 : : "cipher IV size cannot be longer than %u",
1235 : : VIRTIO_CRYPTO_MAX_IV_SIZE);
1236 : 0 : return -1;
1237 : : }
1238 [ # # ]: 0 : if (is_chainned)
1239 : 0 : ret = virtio_crypto_sym_pad_cipher_param(
1240 : : &ctrl->u.sym_create_session.u.chain.para
1241 : : .cipher_param, cipher_xform);
1242 : : else
1243 : 0 : ret = virtio_crypto_sym_pad_cipher_param(
1244 : : &ctrl->u.sym_create_session.u.cipher.para,
1245 : : cipher_xform);
1246 : :
1247 [ # # ]: 0 : if (ret < 0) {
1248 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1249 : : "pad cipher parameter failed");
1250 : 0 : return -1;
1251 : : }
1252 : :
1253 : 0 : memcpy(cipher_key_data, cipher_xform->key.data,
1254 : 0 : cipher_xform->key.length);
1255 : :
1256 : 0 : session->iv.offset = cipher_xform->iv.offset;
1257 : 0 : session->iv.length = cipher_xform->iv.length;
1258 : : }
1259 : :
1260 : : /* Get auth xform from crypto xform chain */
1261 : : auth_xform = virtio_crypto_get_auth_xform(xform);
1262 [ # # ]: 0 : if (auth_xform) {
1263 : : /* FIXME: support VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED */
1264 : : struct virtio_crypto_alg_chain_session_para *para =
1265 : : &(ctrl->u.sym_create_session.u.chain.para);
1266 [ # # ]: 0 : if (auth_xform->key.length) {
1267 [ # # ]: 0 : if (auth_xform->key.length >
1268 : : VIRTIO_CRYPTO_MAX_KEY_SIZE) {
1269 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1270 : : "auth key size cannot be longer than %u",
1271 : : VIRTIO_CRYPTO_MAX_KEY_SIZE);
1272 : 0 : return -1;
1273 : : }
1274 : 0 : para->hash_mode = VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH;
1275 : 0 : para->u.mac_param.auth_key_len =
1276 : 0 : (uint32_t)auth_xform->key.length;
1277 : 0 : para->u.mac_param.hash_result_len =
1278 : 0 : auth_xform->digest_length;
1279 : 0 : memcpy(auth_key_data, auth_xform->key.data,
1280 : : auth_xform->key.length);
1281 : : } else {
1282 : 0 : para->hash_mode = VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN;
1283 : 0 : para->u.hash_param.hash_result_len =
1284 : 0 : auth_xform->digest_length;
1285 : : }
1286 : :
1287 : 0 : ret = virtio_crypto_sym_pad_auth_param(ctrl, auth_xform);
1288 [ # # ]: 0 : if (ret < 0) {
1289 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("pad auth parameter "
1290 : : "failed");
1291 : 0 : return -1;
1292 : : }
1293 : : }
1294 : :
1295 : : return 0;
1296 : : }
1297 : :
1298 : : static int
1299 : 0 : virtio_crypto_check_sym_configure_session_paras(
1300 : : struct rte_cryptodev *dev,
1301 : : struct rte_crypto_sym_xform *xform,
1302 : : struct rte_cryptodev_sym_session *sym_sess)
1303 : : {
1304 [ # # # # ]: 0 : if (unlikely(xform == NULL) || unlikely(sym_sess == NULL)) {
1305 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("NULL pointer");
1306 : 0 : return -1;
1307 : : }
1308 : :
1309 [ # # ]: 0 : if (virtio_crypto_check_sym_session_paras(dev) < 0)
1310 : 0 : return -1;
1311 : :
1312 : : return 0;
1313 : : }
1314 : :
1315 : : static int
1316 : 0 : virtio_crypto_check_asym_configure_session_paras(
1317 : : struct rte_cryptodev *dev,
1318 : : struct rte_crypto_asym_xform *xform,
1319 : : struct rte_cryptodev_asym_session *asym_sess)
1320 : : {
1321 [ # # # # ]: 0 : if (unlikely(xform == NULL) || unlikely(asym_sess == NULL)) {
1322 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("NULL pointer");
1323 : 0 : return -1;
1324 : : }
1325 : :
1326 [ # # ]: 0 : if (virtio_crypto_check_sym_session_paras(dev) < 0)
1327 : 0 : return -1;
1328 : :
1329 : : return 0;
1330 : : }
1331 : :
1332 : : static int
1333 : 0 : virtio_crypto_sym_configure_session(
1334 : : struct rte_cryptodev *dev,
1335 : : struct rte_crypto_sym_xform *xform,
1336 : : struct rte_cryptodev_sym_session *sess)
1337 : : {
1338 : 0 : uint8_t cipher_key_data[VIRTIO_CRYPTO_MAX_KEY_SIZE] = {0};
1339 : 0 : uint8_t auth_key_data[VIRTIO_CRYPTO_MAX_KEY_SIZE] = {0};
1340 : : struct virtio_crypto_op_ctrl_req *ctrl_req;
1341 : : struct virtio_crypto_session_input *input;
1342 : : struct virtio_crypto_session *session;
1343 : : enum virtio_crypto_cmd_id cmd_id;
1344 : : struct virtio_crypto_hw *hw;
1345 : : struct virtio_pmd_ctrl *ctrl;
1346 : : int dlen[2], dnum;
1347 : : int ret;
1348 : :
1349 : 0 : PMD_INIT_FUNC_TRACE();
1350 : :
1351 : 0 : ret = virtio_crypto_check_sym_configure_session_paras(dev, xform,
1352 : : sess);
1353 [ # # ]: 0 : if (ret < 0) {
1354 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid parameters");
1355 : 0 : return ret;
1356 : : }
1357 [ # # ]: 0 : session = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
1358 : : memset(session, 0, sizeof(struct virtio_crypto_session));
1359 : 0 : ctrl = &session->ctrl;
1360 : 0 : ctrl_req = &ctrl->hdr;
1361 : 0 : ctrl_req->header.opcode = VIRTIO_CRYPTO_CIPHER_CREATE_SESSION;
1362 : : /* FIXME: support multiqueue */
1363 : 0 : ctrl_req->header.queue_id = 0;
1364 : :
1365 : 0 : hw = dev->data->dev_private;
1366 : :
1367 : 0 : cmd_id = virtio_crypto_get_chain_order(xform);
1368 [ # # ]: 0 : if (cmd_id == VIRTIO_CRYPTO_CMD_CIPHER_HASH)
1369 : : ctrl_req->u.sym_create_session.u.chain.para.alg_chain_order
1370 : 0 : = VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
1371 [ # # ]: 0 : if (cmd_id == VIRTIO_CRYPTO_CMD_HASH_CIPHER)
1372 : : ctrl_req->u.sym_create_session.u.chain.para.alg_chain_order
1373 : 0 : = VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
1374 : :
1375 [ # # # ]: 0 : switch (cmd_id) {
1376 : : case VIRTIO_CRYPTO_CMD_CIPHER_HASH:
1377 : : case VIRTIO_CRYPTO_CMD_HASH_CIPHER: {
1378 : : struct rte_crypto_cipher_xform *cipher_xform = NULL;
1379 : : struct rte_crypto_auth_xform *auth_xform = NULL;
1380 : :
1381 : : cipher_xform = virtio_crypto_get_cipher_xform(xform);
1382 : : auth_xform = virtio_crypto_get_auth_xform(xform);
1383 : :
1384 : : ctrl_req->u.sym_create_session.op_type
1385 : 0 : = VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING;
1386 : :
1387 : 0 : ret = virtio_crypto_sym_pad_op_ctrl_req(ctrl_req,
1388 : : xform, true, cipher_key_data, auth_key_data, session);
1389 [ # # ]: 0 : if (ret < 0) {
1390 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1391 : : "padding sym op ctrl req failed");
1392 : 0 : goto error_out;
1393 : : }
1394 : :
1395 : 0 : dlen[0] = cipher_xform->key.length;
1396 : 0 : memcpy(ctrl->data, cipher_key_data, dlen[0]);
1397 : 0 : dlen[1] = auth_xform->key.length;
1398 : 0 : memcpy(ctrl->data + dlen[0], auth_key_data, dlen[1]);
1399 : : dnum = 2;
1400 : 0 : break;
1401 : : }
1402 : : case VIRTIO_CRYPTO_CMD_CIPHER: {
1403 : : struct rte_crypto_cipher_xform *cipher_xform = NULL;
1404 : :
1405 : : cipher_xform = virtio_crypto_get_cipher_xform(xform);
1406 : :
1407 : : ctrl_req->u.sym_create_session.op_type
1408 : 0 : = VIRTIO_CRYPTO_SYM_OP_CIPHER;
1409 : 0 : ret = virtio_crypto_sym_pad_op_ctrl_req(ctrl_req, xform,
1410 : : false, cipher_key_data, auth_key_data, session);
1411 [ # # ]: 0 : if (ret < 0) {
1412 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1413 : : "padding sym op ctrl req failed");
1414 : 0 : goto error_out;
1415 : : }
1416 : :
1417 : 0 : dlen[0] = cipher_xform->key.length;
1418 : 0 : memcpy(ctrl->data, cipher_key_data, dlen[0]);
1419 : : dnum = 1;
1420 : 0 : break;
1421 : : }
1422 : 0 : default:
1423 : : ret = -ENOTSUP;
1424 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR(
1425 : : "Unsupported operation chain order parameter");
1426 : 0 : goto error_out;
1427 : : }
1428 : :
1429 : : input = &ctrl->input;
1430 : 0 : input->status = VIRTIO_CRYPTO_ERR;
1431 : 0 : input->session_id = ~0ULL;
1432 : :
1433 : 0 : ret = virtio_crypto_send_command(hw->cvq, ctrl, dlen, dnum);
1434 [ # # ]: 0 : if (ret < 0) {
1435 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("create session failed: %d", ret);
1436 : 0 : goto error_out;
1437 : : }
1438 : :
1439 : 0 : ctrl = hw->cvq->hdr_mz->addr;
1440 : : input = &ctrl->input;
1441 [ # # ]: 0 : if (input->status != VIRTIO_CRYPTO_OK) {
1442 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Something wrong on backend! "
1443 : : "status=%u, session_id=%" PRIu64 "",
1444 : : input->status, input->session_id);
1445 : 0 : goto error_out;
1446 : : } else {
1447 : 0 : session->session_id = input->session_id;
1448 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Create session successfully, "
1449 : : "session_id=%" PRIu64 "", input->session_id);
1450 : : }
1451 : :
1452 : 0 : return 0;
1453 : : error_out:
1454 : : return ret;
1455 : : }
1456 : :
1457 : : static size_t
1458 : 0 : tlv_encode(uint8_t *tlv, uint8_t type, uint8_t *data, size_t len)
1459 : : {
1460 : : uint8_t *lenval = tlv;
1461 : : size_t lenval_n = 0;
1462 : :
1463 [ # # ]: 0 : if (len > 65535) {
1464 : 0 : goto _exit;
1465 [ # # ]: 0 : } else if (len > 255) {
1466 : 0 : lenval_n = 4 + len;
1467 : 0 : lenval[0] = type;
1468 : 0 : lenval[1] = 0x82;
1469 : 0 : lenval[2] = (len & 0xFF00) >> 8;
1470 : 0 : lenval[3] = (len & 0xFF);
1471 [ # # ]: 0 : rte_memcpy(&lenval[4], data, len);
1472 [ # # ]: 0 : } else if (len > 127) {
1473 : 0 : lenval_n = 3 + len;
1474 : 0 : lenval[0] = type;
1475 : 0 : lenval[1] = 0x81;
1476 : 0 : lenval[2] = len;
1477 [ # # ]: 0 : rte_memcpy(&lenval[3], data, len);
1478 : : } else {
1479 : 0 : lenval_n = 2 + len;
1480 : 0 : lenval[0] = type;
1481 : 0 : lenval[1] = len;
1482 [ # # ]: 0 : rte_memcpy(&lenval[2], data, len);
1483 : : }
1484 : :
1485 : 0 : _exit:
1486 : 0 : return lenval_n;
1487 : : }
1488 : :
1489 : : static int
1490 : 0 : virtio_crypto_asym_rsa_xform_to_der(
1491 : : struct rte_crypto_asym_xform *xform,
1492 : : uint8_t *der)
1493 : : {
1494 : : uint8_t data[VIRTIO_CRYPTO_MAX_CTRL_DATA];
1495 : 0 : uint8_t ver[3] = {0x02, 0x01, 0x00};
1496 : : size_t tlen, len;
1497 : : uint8_t *tlv;
1498 : :
1499 [ # # ]: 0 : if (xform->xform_type != RTE_CRYPTO_ASYM_XFORM_RSA)
1500 : : return -EINVAL;
1501 : :
1502 : : tlv = data;
1503 : : rte_memcpy(tlv, ver, RTE_DIM(ver));
1504 : : tlen = RTE_DIM(ver);
1505 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.n.data, xform->rsa.n.length);
1506 : 0 : tlen += len;
1507 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.e.data, xform->rsa.e.length);
1508 : 0 : tlen += len;
1509 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.d.data, xform->rsa.d.length);
1510 : 0 : tlen += len;
1511 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.p.data, xform->rsa.qt.p.length);
1512 : 0 : tlen += len;
1513 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.q.data, xform->rsa.qt.q.length);
1514 : 0 : tlen += len;
1515 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.dP.data, xform->rsa.qt.dP.length);
1516 : 0 : tlen += len;
1517 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.dQ.data, xform->rsa.qt.dQ.length);
1518 : 0 : tlen += len;
1519 : 0 : len = tlv_encode(tlv + tlen, 0x02, xform->rsa.qt.qInv.data, xform->rsa.qt.qInv.length);
1520 : 0 : tlen += len;
1521 : :
1522 : : RTE_ASSERT(tlen < VIRTIO_CRYPTO_MAX_CTRL_DATA);
1523 : 0 : len = tlv_encode(der, 0x30, data, tlen);
1524 : 0 : return len;
1525 : : }
1526 : :
1527 : : static int
1528 : 0 : virtio_crypto_asym_rsa_configure_session(
1529 : : struct rte_crypto_rsa_xform *rsa,
1530 : : struct virtio_crypto_akcipher_session_para *para)
1531 : : {
1532 : 0 : para->algo = VIRTIO_CRYPTO_AKCIPHER_RSA;
1533 [ # # ]: 0 : if (rsa->key_type == RTE_RSA_KEY_TYPE_EXP)
1534 : 0 : para->keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC;
1535 : : else
1536 : 0 : para->keytype = VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE;
1537 : :
1538 [ # # ]: 0 : if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_NONE) {
1539 : 0 : para->u.rsa.padding_algo = VIRTIO_CRYPTO_RSA_RAW_PADDING;
1540 [ # # ]: 0 : } else if (rsa->padding.type == RTE_CRYPTO_RSA_PADDING_PKCS1_5) {
1541 : 0 : para->u.rsa.padding_algo = VIRTIO_CRYPTO_RSA_PKCS1_PADDING;
1542 [ # # # # : 0 : switch (rsa->padding.hash) {
# # ]
1543 : 0 : case RTE_CRYPTO_AUTH_SHA1:
1544 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA1;
1545 : 0 : break;
1546 : 0 : case RTE_CRYPTO_AUTH_SHA224:
1547 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA224;
1548 : 0 : break;
1549 : 0 : case RTE_CRYPTO_AUTH_SHA256:
1550 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA256;
1551 : 0 : break;
1552 : 0 : case RTE_CRYPTO_AUTH_SHA512:
1553 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_SHA512;
1554 : 0 : break;
1555 : 0 : case RTE_CRYPTO_AUTH_MD5:
1556 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_MD5;
1557 : 0 : break;
1558 : 0 : default:
1559 : 0 : para->u.rsa.hash_algo = VIRTIO_CRYPTO_RSA_NO_HASH;
1560 : : }
1561 : : } else {
1562 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid padding type");
1563 : 0 : return -EINVAL;
1564 : : }
1565 : :
1566 : : return 0;
1567 : : }
1568 : :
1569 : : static int
1570 : 0 : virtio_crypto_asym_configure_session(
1571 : : struct rte_cryptodev *dev,
1572 : : struct rte_crypto_asym_xform *xform,
1573 : : struct rte_cryptodev_asym_session *sess)
1574 : : {
1575 : : struct virtio_crypto_akcipher_session_para *para;
1576 : : struct virtio_crypto_op_ctrl_req *ctrl_req;
1577 : : struct virtio_crypto_session_input *input;
1578 : : struct virtio_crypto_session *session;
1579 : : struct virtio_crypto_hw *hw;
1580 : : struct virtio_pmd_ctrl *ctrl;
1581 : : int dlen[1];
1582 : : int ret;
1583 : :
1584 : 0 : PMD_INIT_FUNC_TRACE();
1585 : :
1586 : 0 : ret = virtio_crypto_check_asym_configure_session_paras(dev, xform,
1587 : : sess);
1588 [ # # ]: 0 : if (ret < 0) {
1589 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid parameters");
1590 : 0 : return ret;
1591 : : }
1592 : :
1593 [ # # ]: 0 : session = CRYPTODEV_GET_ASYM_SESS_PRIV(sess);
1594 : : memset(session, 0, sizeof(struct virtio_crypto_session));
1595 : 0 : ctrl = &session->ctrl;
1596 : : ctrl_req = &ctrl->hdr;
1597 : 0 : ctrl_req->header.opcode = VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION;
1598 : 0 : ctrl_req->header.queue_id = 0;
1599 : 0 : para = &ctrl_req->u.akcipher_create_session.para;
1600 : :
1601 [ # # ]: 0 : switch (xform->xform_type) {
1602 : 0 : case RTE_CRYPTO_ASYM_XFORM_RSA:
1603 : 0 : ctrl_req->header.algo = VIRTIO_CRYPTO_AKCIPHER_RSA;
1604 : 0 : ret = virtio_crypto_asym_rsa_configure_session(&xform->rsa, para);
1605 [ # # ]: 0 : if (ret < 0) {
1606 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid RSA parameters");
1607 : 0 : return ret;
1608 : : }
1609 : :
1610 : 0 : ret = virtio_crypto_asym_rsa_xform_to_der(xform, ctrl->data);
1611 [ # # ]: 0 : if (ret <= 0) {
1612 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Invalid RSA primitives");
1613 : 0 : return ret;
1614 : : }
1615 : :
1616 : 0 : ctrl_req->u.akcipher_create_session.para.keylen = ret;
1617 : 0 : break;
1618 : 0 : default:
1619 : 0 : para->algo = VIRTIO_CRYPTO_NO_AKCIPHER;
1620 : : }
1621 : :
1622 : 0 : dlen[0] = ret;
1623 : : input = &ctrl->input;
1624 : 0 : input->status = VIRTIO_CRYPTO_ERR;
1625 : 0 : input->session_id = ~0ULL;
1626 : :
1627 : 0 : hw = dev->data->dev_private;
1628 : 0 : ret = virtio_crypto_send_command(hw->cvq, ctrl, dlen, 1);
1629 [ # # ]: 0 : if (ret < 0) {
1630 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("create session failed: %d", ret);
1631 : 0 : goto error_out;
1632 : : }
1633 : :
1634 : 0 : ctrl = hw->cvq->hdr_mz->addr;
1635 : : input = &ctrl->input;
1636 [ # # ]: 0 : if (input->status != VIRTIO_CRYPTO_OK) {
1637 : 0 : VIRTIO_CRYPTO_SESSION_LOG_ERR("Something wrong on backend! "
1638 : : "status=%u, session_id=%" PRIu64 "",
1639 : : input->status, input->session_id);
1640 : 0 : goto error_out;
1641 : : } else {
1642 : 0 : session->session_id = input->session_id;
1643 : 0 : VIRTIO_CRYPTO_SESSION_LOG_INFO("Create session successfully, "
1644 : : "session_id=%" PRIu64 "", input->session_id);
1645 : : }
1646 : :
1647 : 0 : return 0;
1648 : : error_out:
1649 : : return -1;
1650 : : }
1651 : :
1652 : : static void
1653 : 0 : virtio_crypto_dev_info_get(struct rte_cryptodev *dev,
1654 : : struct rte_cryptodev_info *info)
1655 : : {
1656 : 0 : struct virtio_crypto_hw *hw = dev->data->dev_private;
1657 : :
1658 : 0 : PMD_INIT_FUNC_TRACE();
1659 : :
1660 [ # # ]: 0 : if (info != NULL) {
1661 : 0 : info->driver_id = cryptodev_virtio_driver_id;
1662 : 0 : info->feature_flags = dev->feature_flags;
1663 : 0 : info->max_nb_queue_pairs = hw->max_dataqueues;
1664 : : /* No limit of number of sessions */
1665 : 0 : info->sym.max_nb_sessions = 0;
1666 : 0 : info->capabilities = hw->virtio_dev_capabilities;
1667 : : }
1668 : 0 : }
1669 : :
1670 : : static int
1671 : 0 : crypto_virtio_pci_probe(
1672 : : struct rte_pci_driver *pci_drv __rte_unused,
1673 : : struct rte_pci_device *pci_dev)
1674 : : {
1675 : 0 : struct rte_cryptodev_pmd_init_params init_params = {
1676 : : .name = "",
1677 : 0 : .socket_id = pci_dev->device.numa_node,
1678 : : .private_data_size = sizeof(struct virtio_crypto_hw)
1679 : : };
1680 : : char name[RTE_CRYPTODEV_NAME_MAX_LEN];
1681 : :
1682 : 0 : VIRTIO_CRYPTO_DRV_LOG_DBG("Found Crypto device at %02x:%02x.%x",
1683 : : pci_dev->addr.bus,
1684 : : pci_dev->addr.devid,
1685 : : pci_dev->addr.function);
1686 : :
1687 : 0 : rte_pci_device_name(&pci_dev->addr, name, sizeof(name));
1688 : :
1689 : 0 : return crypto_virtio_create(name, pci_dev, &init_params);
1690 : : }
1691 : :
1692 : : static int
1693 : 0 : crypto_virtio_pci_remove(
1694 : : struct rte_pci_device *pci_dev __rte_unused)
1695 : : {
1696 : : struct rte_cryptodev *cryptodev;
1697 : : char cryptodev_name[RTE_CRYPTODEV_NAME_MAX_LEN];
1698 : :
1699 [ # # ]: 0 : if (pci_dev == NULL)
1700 : : return -EINVAL;
1701 : :
1702 : 0 : rte_pci_device_name(&pci_dev->addr, cryptodev_name,
1703 : : sizeof(cryptodev_name));
1704 : :
1705 : 0 : cryptodev = rte_cryptodev_pmd_get_named_dev(cryptodev_name);
1706 [ # # ]: 0 : if (cryptodev == NULL)
1707 : : return -ENODEV;
1708 : :
1709 : 0 : return virtio_crypto_dev_uninit(cryptodev);
1710 : : }
1711 : :
1712 : : static struct rte_pci_driver rte_virtio_crypto_driver = {
1713 : : .id_table = pci_id_virtio_crypto_map,
1714 : : .drv_flags = 0,
1715 : : .probe = crypto_virtio_pci_probe,
1716 : : .remove = crypto_virtio_pci_remove
1717 : : };
1718 : :
1719 : : static struct cryptodev_driver virtio_crypto_drv;
1720 : :
1721 : 252 : RTE_PMD_REGISTER_PCI(CRYPTODEV_NAME_VIRTIO_PMD, rte_virtio_crypto_driver);
1722 : 252 : RTE_PMD_REGISTER_CRYPTO_DRIVER(virtio_crypto_drv,
1723 : : rte_virtio_crypto_driver.driver,
1724 : : cryptodev_virtio_driver_id);
1725 [ - + ]: 252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_init, init, NOTICE);
1726 [ - + ]: 252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_session, session, NOTICE);
1727 [ - + ]: 252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_rx, rx, NOTICE);
1728 [ - + ]: 252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_tx, tx, NOTICE);
1729 [ - + ]: 252 : RTE_LOG_REGISTER_SUFFIX(virtio_crypto_logtype_driver, driver, NOTICE);
|