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