Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2019 Marvell International Ltd.
3 : : */
4 : :
5 : : #include <stdbool.h>
6 : :
7 : : #include <cryptodev_pmd.h>
8 : : #include <rte_crypto.h>
9 : :
10 : : #include "nitrox_sym.h"
11 : : #include "nitrox_device.h"
12 : : #include "nitrox_sym_capabilities.h"
13 : : #include "nitrox_qp.h"
14 : : #include "nitrox_sym_reqmgr.h"
15 : : #include "nitrox_sym_ctx.h"
16 : : #include "nitrox_logs.h"
17 : :
18 : : #define CRYPTODEV_NAME_NITROX_PMD crypto_nitrox_sym
19 : : #define MC_MAC_MISMATCH_ERR_CODE 0x4c
20 : : #define NPS_PKT_IN_INSTR_SIZE 64
21 : : #define IV_FROM_DPTR 1
22 : : #define FLEXI_CRYPTO_ENCRYPT_HMAC 0x33
23 : : #define FLEXI_CRYPTO_MAX_AAD_LEN 512
24 : : #define AES_KEYSIZE_128 16
25 : : #define AES_KEYSIZE_192 24
26 : : #define AES_KEYSIZE_256 32
27 : : #define MAX_IV_LEN 16
28 : :
29 : : struct nitrox_sym_device {
30 : : struct rte_cryptodev *cdev;
31 : : struct nitrox_device *ndev;
32 : : };
33 : :
34 : : /* Cipher opcodes */
35 : : enum flexi_cipher {
36 : : CIPHER_NULL = 0,
37 : : CIPHER_3DES_CBC,
38 : : CIPHER_3DES_ECB,
39 : : CIPHER_AES_CBC,
40 : : CIPHER_AES_ECB,
41 : : CIPHER_AES_CFB,
42 : : CIPHER_AES_CTR,
43 : : CIPHER_AES_GCM,
44 : : CIPHER_AES_XTS,
45 : : CIPHER_AES_CCM,
46 : : CIPHER_AES_CBC_CTS,
47 : : CIPHER_AES_ECB_CTS,
48 : : CIPHER_INVALID
49 : : };
50 : :
51 : : /* Auth opcodes */
52 : : enum flexi_auth {
53 : : AUTH_NULL = 0,
54 : : AUTH_MD5,
55 : : AUTH_SHA1,
56 : : AUTH_SHA2_SHA224,
57 : : AUTH_SHA2_SHA256,
58 : : AUTH_SHA2_SHA384,
59 : : AUTH_SHA2_SHA512,
60 : : AUTH_GMAC,
61 : : AUTH_INVALID
62 : : };
63 : :
64 : : uint8_t nitrox_sym_drv_id;
65 : : static const char nitrox_sym_drv_name[] = RTE_STR(CRYPTODEV_NAME_NITROX_PMD);
66 : : static const struct rte_driver nitrox_rte_sym_drv = {
67 : : .name = nitrox_sym_drv_name,
68 : : .alias = nitrox_sym_drv_name
69 : : };
70 : :
71 : : static int nitrox_sym_dev_qp_release(struct rte_cryptodev *cdev,
72 : : uint16_t qp_id);
73 : :
74 : : static int
75 : 0 : nitrox_sym_dev_config(struct rte_cryptodev *cdev,
76 : : struct rte_cryptodev_config *config)
77 : : {
78 : 0 : struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
79 : 0 : struct nitrox_device *ndev = sym_dev->ndev;
80 : :
81 [ # # ]: 0 : if (config->nb_queue_pairs > ndev->nr_queues) {
82 : 0 : NITROX_LOG(ERR, "Invalid queue pairs, max supported %d\n",
83 : : ndev->nr_queues);
84 : 0 : return -EINVAL;
85 : : }
86 : :
87 : : return 0;
88 : : }
89 : :
90 : : static int
91 : 0 : nitrox_sym_dev_start(struct rte_cryptodev *cdev)
92 : : {
93 : : /* SE cores initialization is done in PF */
94 : : RTE_SET_USED(cdev);
95 : 0 : return 0;
96 : : }
97 : :
98 : : static void
99 : 0 : nitrox_sym_dev_stop(struct rte_cryptodev *cdev)
100 : : {
101 : : /* SE cores cleanup is done in PF */
102 : : RTE_SET_USED(cdev);
103 : 0 : }
104 : :
105 : : static int
106 : 0 : nitrox_sym_dev_close(struct rte_cryptodev *cdev)
107 : : {
108 : : int i, ret;
109 : :
110 [ # # ]: 0 : for (i = 0; i < cdev->data->nb_queue_pairs; i++) {
111 : 0 : ret = nitrox_sym_dev_qp_release(cdev, i);
112 [ # # ]: 0 : if (ret)
113 : 0 : return ret;
114 : : }
115 : :
116 : : return 0;
117 : : }
118 : :
119 : : static void
120 : 0 : nitrox_sym_dev_info_get(struct rte_cryptodev *cdev,
121 : : struct rte_cryptodev_info *info)
122 : : {
123 : 0 : struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
124 : 0 : struct nitrox_device *ndev = sym_dev->ndev;
125 : :
126 [ # # ]: 0 : if (!info)
127 : : return;
128 : :
129 : 0 : info->max_nb_queue_pairs = ndev->nr_queues;
130 : 0 : info->feature_flags = cdev->feature_flags;
131 : 0 : info->capabilities = nitrox_get_sym_capabilities();
132 : 0 : info->driver_id = nitrox_sym_drv_id;
133 : 0 : info->sym.max_nb_sessions = 0;
134 : : }
135 : :
136 : : static void
137 : 0 : nitrox_sym_dev_stats_get(struct rte_cryptodev *cdev,
138 : : struct rte_cryptodev_stats *stats)
139 : : {
140 : : int qp_id;
141 : :
142 [ # # ]: 0 : for (qp_id = 0; qp_id < cdev->data->nb_queue_pairs; qp_id++) {
143 : 0 : struct nitrox_qp *qp = cdev->data->queue_pairs[qp_id];
144 : :
145 [ # # ]: 0 : if (!qp)
146 : 0 : continue;
147 : :
148 : 0 : stats->enqueued_count += qp->stats.enqueued_count;
149 : 0 : stats->dequeued_count += qp->stats.dequeued_count;
150 : 0 : stats->enqueue_err_count += qp->stats.enqueue_err_count;
151 : 0 : stats->dequeue_err_count += qp->stats.dequeue_err_count;
152 : : }
153 : 0 : }
154 : :
155 : : static void
156 : 0 : nitrox_sym_dev_stats_reset(struct rte_cryptodev *cdev)
157 : : {
158 : : int qp_id;
159 : :
160 [ # # ]: 0 : for (qp_id = 0; qp_id < cdev->data->nb_queue_pairs; qp_id++) {
161 : 0 : struct nitrox_qp *qp = cdev->data->queue_pairs[qp_id];
162 : :
163 [ # # ]: 0 : if (!qp)
164 : 0 : continue;
165 : :
166 : 0 : memset(&qp->stats, 0, sizeof(qp->stats));
167 : : }
168 : 0 : }
169 : :
170 : : static int
171 : 0 : nitrox_sym_dev_qp_setup(struct rte_cryptodev *cdev, uint16_t qp_id,
172 : : const struct rte_cryptodev_qp_conf *qp_conf,
173 : : int socket_id)
174 : : {
175 : 0 : struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
176 : 0 : struct nitrox_device *ndev = sym_dev->ndev;
177 : : struct nitrox_qp *qp = NULL;
178 : : int err;
179 : :
180 : 0 : NITROX_LOG(DEBUG, "queue %d\n", qp_id);
181 [ # # ]: 0 : if (qp_id >= ndev->nr_queues) {
182 : 0 : NITROX_LOG(ERR, "queue %u invalid, max queues supported %d\n",
183 : : qp_id, ndev->nr_queues);
184 : 0 : return -EINVAL;
185 : : }
186 : :
187 [ # # ]: 0 : if (cdev->data->queue_pairs[qp_id]) {
188 : 0 : err = nitrox_sym_dev_qp_release(cdev, qp_id);
189 [ # # ]: 0 : if (err)
190 : : return err;
191 : : }
192 : :
193 : 0 : qp = rte_zmalloc_socket("nitrox PMD qp", sizeof(*qp),
194 : : RTE_CACHE_LINE_SIZE,
195 : : socket_id);
196 [ # # ]: 0 : if (!qp) {
197 : 0 : NITROX_LOG(ERR, "Failed to allocate nitrox qp\n");
198 : 0 : return -ENOMEM;
199 : : }
200 : :
201 : 0 : qp->qno = qp_id;
202 : 0 : err = nitrox_qp_setup(qp, ndev->bar_addr, cdev->data->name,
203 : 0 : qp_conf->nb_descriptors, NPS_PKT_IN_INSTR_SIZE,
204 : : socket_id);
205 [ # # ]: 0 : if (unlikely(err))
206 : 0 : goto qp_setup_err;
207 : :
208 : 0 : qp->sr_mp = nitrox_sym_req_pool_create(cdev, qp->count, qp_id,
209 : : socket_id);
210 [ # # ]: 0 : if (unlikely(!qp->sr_mp))
211 : 0 : goto req_pool_err;
212 : :
213 : 0 : cdev->data->queue_pairs[qp_id] = qp;
214 : 0 : NITROX_LOG(DEBUG, "queue %d setup done\n", qp_id);
215 : 0 : return 0;
216 : :
217 : : req_pool_err:
218 : 0 : nitrox_qp_release(qp, ndev->bar_addr);
219 : 0 : qp_setup_err:
220 : 0 : rte_free(qp);
221 : 0 : return err;
222 : : }
223 : :
224 : : static int
225 : 0 : nitrox_sym_dev_qp_release(struct rte_cryptodev *cdev, uint16_t qp_id)
226 : : {
227 : 0 : struct nitrox_sym_device *sym_dev = cdev->data->dev_private;
228 : 0 : struct nitrox_device *ndev = sym_dev->ndev;
229 : : struct nitrox_qp *qp;
230 : : int err;
231 : :
232 : 0 : NITROX_LOG(DEBUG, "queue %d\n", qp_id);
233 [ # # ]: 0 : if (qp_id >= ndev->nr_queues) {
234 : 0 : NITROX_LOG(ERR, "queue %u invalid, max queues supported %d\n",
235 : : qp_id, ndev->nr_queues);
236 : 0 : return -EINVAL;
237 : : }
238 : :
239 : 0 : qp = cdev->data->queue_pairs[qp_id];
240 [ # # ]: 0 : if (!qp) {
241 : 0 : NITROX_LOG(DEBUG, "queue %u already freed\n", qp_id);
242 : 0 : return 0;
243 : : }
244 : :
245 [ # # ]: 0 : if (!nitrox_qp_is_empty(qp)) {
246 : 0 : NITROX_LOG(ERR, "queue %d not empty\n", qp_id);
247 : 0 : return -EAGAIN;
248 : : }
249 : :
250 : 0 : cdev->data->queue_pairs[qp_id] = NULL;
251 : 0 : err = nitrox_qp_release(qp, ndev->bar_addr);
252 : 0 : nitrox_sym_req_pool_free(qp->sr_mp);
253 : 0 : rte_free(qp);
254 : 0 : NITROX_LOG(DEBUG, "queue %d release done\n", qp_id);
255 : 0 : return err;
256 : : }
257 : :
258 : : static unsigned int
259 : 0 : nitrox_sym_dev_sess_get_size(__rte_unused struct rte_cryptodev *cdev)
260 : : {
261 : 0 : return sizeof(struct nitrox_crypto_ctx);
262 : : }
263 : :
264 : : static enum nitrox_chain
265 : 0 : get_crypto_chain_order(const struct rte_crypto_sym_xform *xform)
266 : : {
267 : : enum nitrox_chain res = NITROX_CHAIN_NOT_SUPPORTED;
268 : :
269 [ # # ]: 0 : if (unlikely(xform == NULL))
270 : : return res;
271 : :
272 [ # # # # ]: 0 : switch (xform->type) {
273 : 0 : case RTE_CRYPTO_SYM_XFORM_AUTH:
274 [ # # ]: 0 : if (xform->next == NULL) {
275 : : res = NITROX_CHAIN_NOT_SUPPORTED;
276 [ # # ]: 0 : } else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
277 [ # # ]: 0 : if (xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY &&
278 [ # # ]: 0 : xform->next->cipher.op ==
279 : : RTE_CRYPTO_CIPHER_OP_DECRYPT) {
280 : : res = NITROX_CHAIN_AUTH_CIPHER;
281 : : } else {
282 : 0 : NITROX_LOG(ERR, "auth op %d, cipher op %d\n",
283 : : xform->auth.op, xform->next->cipher.op);
284 : : }
285 : : }
286 : : break;
287 : 0 : case RTE_CRYPTO_SYM_XFORM_CIPHER:
288 [ # # ]: 0 : if (xform->next == NULL) {
289 : : res = NITROX_CHAIN_CIPHER_ONLY;
290 [ # # ]: 0 : } else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
291 [ # # ]: 0 : if (xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
292 [ # # ]: 0 : xform->next->auth.op ==
293 : : RTE_CRYPTO_AUTH_OP_GENERATE) {
294 : : res = NITROX_CHAIN_CIPHER_AUTH;
295 : : } else {
296 : 0 : NITROX_LOG(ERR, "cipher op %d, auth op %d\n",
297 : : xform->cipher.op, xform->next->auth.op);
298 : : }
299 : : }
300 : : break;
301 : 0 : case RTE_CRYPTO_SYM_XFORM_AEAD:
302 : : res = NITROX_CHAIN_COMBINED;
303 : 0 : break;
304 : : default:
305 : : break;
306 : : }
307 : :
308 : : return res;
309 : : }
310 : :
311 : : static enum flexi_cipher
312 : 0 : get_flexi_cipher_type(enum rte_crypto_cipher_algorithm algo, bool *is_aes)
313 : : {
314 : : enum flexi_cipher type;
315 : :
316 [ # # # ]: 0 : switch (algo) {
317 : 0 : case RTE_CRYPTO_CIPHER_AES_CBC:
318 : : type = CIPHER_AES_CBC;
319 : 0 : *is_aes = true;
320 : 0 : break;
321 : 0 : case RTE_CRYPTO_CIPHER_3DES_CBC:
322 : : type = CIPHER_3DES_CBC;
323 : 0 : *is_aes = false;
324 : 0 : break;
325 : 0 : default:
326 : : type = CIPHER_INVALID;
327 : 0 : NITROX_LOG(ERR, "Algorithm not supported %d\n", algo);
328 : 0 : break;
329 : : }
330 : :
331 : 0 : return type;
332 : : }
333 : :
334 : : static int
335 : 0 : flexi_aes_keylen(size_t keylen, bool is_aes)
336 : : {
337 : : int aes_keylen;
338 : :
339 [ # # ]: 0 : if (!is_aes)
340 : : return 0;
341 : :
342 [ # # # # ]: 0 : switch (keylen) {
343 : : case AES_KEYSIZE_128:
344 : : aes_keylen = 1;
345 : : break;
346 : 0 : case AES_KEYSIZE_192:
347 : : aes_keylen = 2;
348 : 0 : break;
349 : 0 : case AES_KEYSIZE_256:
350 : : aes_keylen = 3;
351 : 0 : break;
352 : 0 : default:
353 : 0 : NITROX_LOG(ERR, "Invalid keylen %zu\n", keylen);
354 : : aes_keylen = -EINVAL;
355 : 0 : break;
356 : : }
357 : :
358 : : return aes_keylen;
359 : : }
360 : :
361 : : static bool
362 : : crypto_key_is_valid(struct rte_crypto_cipher_xform *xform,
363 : : struct flexi_crypto_context *fctx)
364 : : {
365 [ # # ]: 0 : if (unlikely(xform->key.length > sizeof(fctx->crypto.key))) {
366 : 0 : NITROX_LOG(ERR, "Invalid crypto key length %d\n",
367 : : xform->key.length);
368 : 0 : return false;
369 : : }
370 : :
371 : : return true;
372 : : }
373 : :
374 : : static int
375 : 0 : configure_cipher_ctx(struct rte_crypto_cipher_xform *xform,
376 : : struct nitrox_crypto_ctx *ctx)
377 : : {
378 : : enum flexi_cipher type;
379 : 0 : bool cipher_is_aes = false;
380 : : int aes_keylen;
381 : : struct flexi_crypto_context *fctx = &ctx->fctx;
382 : :
383 : 0 : type = get_flexi_cipher_type(xform->algo, &cipher_is_aes);
384 [ # # ]: 0 : if (unlikely(type == CIPHER_INVALID))
385 : : return -ENOTSUP;
386 : :
387 : 0 : aes_keylen = flexi_aes_keylen(xform->key.length, cipher_is_aes);
388 [ # # ]: 0 : if (unlikely(aes_keylen < 0))
389 : : return -EINVAL;
390 : :
391 [ # # # # ]: 0 : if (unlikely(!cipher_is_aes && !crypto_key_is_valid(xform, fctx)))
392 : : return -EINVAL;
393 : :
394 [ # # ]: 0 : if (unlikely(xform->iv.length > MAX_IV_LEN))
395 : : return -EINVAL;
396 : :
397 [ # # ]: 0 : fctx->flags = rte_be_to_cpu_64(fctx->flags);
398 : 0 : fctx->w0.cipher_type = type;
399 : 0 : fctx->w0.aes_keylen = aes_keylen;
400 : 0 : fctx->w0.iv_source = IV_FROM_DPTR;
401 [ # # ]: 0 : fctx->flags = rte_cpu_to_be_64(fctx->flags);
402 : 0 : memset(fctx->crypto.key, 0, sizeof(fctx->crypto.key));
403 : 0 : memcpy(fctx->crypto.key, xform->key.data, xform->key.length);
404 : :
405 : 0 : ctx->opcode = FLEXI_CRYPTO_ENCRYPT_HMAC;
406 : 0 : ctx->req_op = (xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
407 : 0 : NITROX_OP_ENCRYPT : NITROX_OP_DECRYPT;
408 : 0 : ctx->iv.offset = xform->iv.offset;
409 : 0 : ctx->iv.length = xform->iv.length;
410 : 0 : return 0;
411 : : }
412 : :
413 : : static enum flexi_auth
414 : 0 : get_flexi_auth_type(enum rte_crypto_auth_algorithm algo)
415 : : {
416 : : enum flexi_auth type;
417 : :
418 [ # # # # ]: 0 : switch (algo) {
419 : : case RTE_CRYPTO_AUTH_SHA1_HMAC:
420 : : type = AUTH_SHA1;
421 : : break;
422 : 0 : case RTE_CRYPTO_AUTH_SHA224_HMAC:
423 : : type = AUTH_SHA2_SHA224;
424 : 0 : break;
425 : 0 : case RTE_CRYPTO_AUTH_SHA256_HMAC:
426 : : type = AUTH_SHA2_SHA256;
427 : 0 : break;
428 : 0 : default:
429 : 0 : NITROX_LOG(ERR, "Algorithm not supported %d\n", algo);
430 : : type = AUTH_INVALID;
431 : 0 : break;
432 : : }
433 : :
434 : 0 : return type;
435 : : }
436 : :
437 : : static bool
438 : 0 : auth_key_is_valid(const uint8_t *data, uint16_t length,
439 : : struct flexi_crypto_context *fctx)
440 : : {
441 [ # # ]: 0 : if (unlikely(!data && length)) {
442 : 0 : NITROX_LOG(ERR, "Invalid auth key\n");
443 : 0 : return false;
444 : : }
445 : :
446 [ # # ]: 0 : if (unlikely(length > sizeof(fctx->auth.opad))) {
447 : 0 : NITROX_LOG(ERR, "Invalid auth key length %d\n",
448 : : length);
449 : 0 : return false;
450 : : }
451 : :
452 : : return true;
453 : : }
454 : :
455 : : static int
456 : 0 : configure_auth_ctx(struct rte_crypto_auth_xform *xform,
457 : : struct nitrox_crypto_ctx *ctx)
458 : : {
459 : : enum flexi_auth type;
460 : 0 : struct flexi_crypto_context *fctx = &ctx->fctx;
461 : :
462 : 0 : type = get_flexi_auth_type(xform->algo);
463 [ # # ]: 0 : if (unlikely(type == AUTH_INVALID))
464 : : return -ENOTSUP;
465 : :
466 [ # # ]: 0 : if (unlikely(!auth_key_is_valid(xform->key.data, xform->key.length,
467 : : fctx)))
468 : : return -EINVAL;
469 : :
470 : 0 : ctx->digest_length = xform->digest_length;
471 : :
472 [ # # ]: 0 : fctx->flags = rte_be_to_cpu_64(fctx->flags);
473 : 0 : fctx->w0.hash_type = type;
474 : 0 : fctx->w0.auth_input_type = 1;
475 : 0 : fctx->w0.mac_len = xform->digest_length;
476 [ # # ]: 0 : fctx->flags = rte_cpu_to_be_64(fctx->flags);
477 : 0 : memset(&fctx->auth, 0, sizeof(fctx->auth));
478 : 0 : memcpy(fctx->auth.opad, xform->key.data, xform->key.length);
479 : 0 : return 0;
480 : : }
481 : :
482 : : static int
483 : 0 : configure_aead_ctx(struct rte_crypto_aead_xform *xform,
484 : : struct nitrox_crypto_ctx *ctx)
485 : : {
486 : : int aes_keylen;
487 : 0 : struct flexi_crypto_context *fctx = &ctx->fctx;
488 : :
489 [ # # ]: 0 : if (unlikely(xform->aad_length > FLEXI_CRYPTO_MAX_AAD_LEN)) {
490 : 0 : NITROX_LOG(ERR, "AAD length %d not supported\n",
491 : : xform->aad_length);
492 : 0 : return -ENOTSUP;
493 : : }
494 : :
495 [ # # ]: 0 : if (unlikely(xform->algo != RTE_CRYPTO_AEAD_AES_GCM &&
496 : : xform->algo != RTE_CRYPTO_AEAD_AES_CCM))
497 : : return -ENOTSUP;
498 : :
499 : 0 : aes_keylen = flexi_aes_keylen(xform->key.length, true);
500 [ # # ]: 0 : if (unlikely(aes_keylen < 0))
501 : : return -EINVAL;
502 : :
503 [ # # ]: 0 : if (unlikely(!auth_key_is_valid(xform->key.data, xform->key.length,
504 : : fctx)))
505 : : return -EINVAL;
506 : :
507 [ # # ]: 0 : if (unlikely(xform->iv.length > MAX_IV_LEN))
508 : : return -EINVAL;
509 : :
510 [ # # ]: 0 : if (xform->algo == RTE_CRYPTO_AEAD_AES_CCM) {
511 : : int L;
512 : :
513 : : /* digest_length must be 4, 6, 8, 10, 12, 14, 16 bytes */
514 [ # # # # ]: 0 : if (unlikely(xform->digest_length < 4 ||
515 : : xform->digest_length > 16 ||
516 : : (xform->digest_length & 1) == 1)) {
517 : 0 : NITROX_LOG(ERR, "Invalid digest length %d\n",
518 : : xform->digest_length);
519 : 0 : return -EINVAL;
520 : : }
521 : :
522 : 0 : L = 15 - xform->iv.length;
523 [ # # ]: 0 : if (unlikely(L < 2 || L > 8)) {
524 : 0 : NITROX_LOG(ERR, "Invalid iv length %d\n",
525 : : xform->iv.length);
526 : 0 : return -EINVAL;
527 : : }
528 : : }
529 : :
530 [ # # ]: 0 : fctx->flags = rte_be_to_cpu_64(fctx->flags);
531 : 0 : fctx->w0.cipher_type = (xform->algo == RTE_CRYPTO_AEAD_AES_GCM) ?
532 [ # # ]: 0 : CIPHER_AES_GCM : CIPHER_AES_CCM;
533 : 0 : fctx->w0.aes_keylen = aes_keylen;
534 : 0 : fctx->w0.iv_source = IV_FROM_DPTR;
535 : 0 : fctx->w0.hash_type = AUTH_NULL;
536 : 0 : fctx->w0.auth_input_type = 1;
537 : 0 : fctx->w0.mac_len = xform->digest_length;
538 [ # # ]: 0 : fctx->flags = rte_cpu_to_be_64(fctx->flags);
539 : 0 : memset(fctx->crypto.key, 0, sizeof(fctx->crypto.key));
540 : 0 : memcpy(fctx->crypto.key, xform->key.data, xform->key.length);
541 : 0 : memset(&fctx->auth, 0, sizeof(fctx->auth));
542 : 0 : memcpy(fctx->auth.opad, xform->key.data, xform->key.length);
543 : :
544 : 0 : ctx->opcode = FLEXI_CRYPTO_ENCRYPT_HMAC;
545 : 0 : ctx->req_op = (xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
546 : 0 : NITROX_OP_ENCRYPT : NITROX_OP_DECRYPT;
547 : 0 : ctx->iv.offset = xform->iv.offset;
548 : 0 : ctx->iv.length = xform->iv.length;
549 : 0 : ctx->digest_length = xform->digest_length;
550 : 0 : ctx->aad_length = xform->aad_length;
551 : 0 : ctx->aead_algo = xform->algo;
552 : 0 : return 0;
553 : : }
554 : :
555 : : static int
556 : 0 : nitrox_sym_dev_sess_configure(struct rte_cryptodev *cdev __rte_unused,
557 : : struct rte_crypto_sym_xform *xform,
558 : : struct rte_cryptodev_sym_session *sess)
559 : : {
560 : 0 : struct nitrox_crypto_ctx *ctx = CRYPTODEV_GET_SYM_SESS_PRIV(sess);
561 : : struct rte_crypto_cipher_xform *cipher_xform = NULL;
562 : : struct rte_crypto_auth_xform *auth_xform = NULL;
563 : : struct rte_crypto_aead_xform *aead_xform = NULL;
564 : : int ret = -EINVAL;
565 : :
566 : 0 : ctx->nitrox_chain = get_crypto_chain_order(xform);
567 [ # # # # : 0 : switch (ctx->nitrox_chain) {
# ]
568 : 0 : case NITROX_CHAIN_CIPHER_ONLY:
569 : 0 : cipher_xform = &xform->cipher;
570 : 0 : break;
571 : 0 : case NITROX_CHAIN_CIPHER_AUTH:
572 : 0 : cipher_xform = &xform->cipher;
573 : 0 : auth_xform = &xform->next->auth;
574 : 0 : break;
575 : 0 : case NITROX_CHAIN_AUTH_CIPHER:
576 : 0 : auth_xform = &xform->auth;
577 : 0 : cipher_xform = &xform->next->cipher;
578 : 0 : break;
579 : 0 : case NITROX_CHAIN_COMBINED:
580 : 0 : aead_xform = &xform->aead;
581 : 0 : break;
582 : 0 : default:
583 : 0 : NITROX_LOG(ERR, "Crypto chain not supported\n");
584 : : ret = -ENOTSUP;
585 : 0 : goto err;
586 : : }
587 : :
588 [ # # # # ]: 0 : if (cipher_xform && unlikely(configure_cipher_ctx(cipher_xform, ctx))) {
589 : 0 : NITROX_LOG(ERR, "Failed to configure cipher ctx\n");
590 : 0 : goto err;
591 : : }
592 : :
593 [ # # # # ]: 0 : if (auth_xform && unlikely(configure_auth_ctx(auth_xform, ctx))) {
594 : 0 : NITROX_LOG(ERR, "Failed to configure auth ctx\n");
595 : 0 : goto err;
596 : : }
597 : :
598 [ # # # # ]: 0 : if (aead_xform && unlikely(configure_aead_ctx(aead_xform, ctx))) {
599 : 0 : NITROX_LOG(ERR, "Failed to configure aead ctx\n");
600 : 0 : goto err;
601 : : }
602 : :
603 : 0 : ctx->iova = CRYPTODEV_GET_SYM_SESS_PRIV_IOVA(sess);
604 : 0 : return 0;
605 : : err:
606 : : return ret;
607 : : }
608 : :
609 : : static void
610 : 0 : nitrox_sym_dev_sess_clear(struct rte_cryptodev *cdev __rte_unused,
611 : : struct rte_cryptodev_sym_session *sess __rte_unused)
612 : 0 : {}
613 : :
614 : : static struct nitrox_crypto_ctx *
615 : : get_crypto_ctx(struct rte_crypto_op *op)
616 : : {
617 : 0 : if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
618 [ # # ]: 0 : if (likely(op->sym->session))
619 : 0 : return CRYPTODEV_GET_SYM_SESS_PRIV(op->sym->session);
620 : : }
621 : :
622 : : return NULL;
623 : : }
624 : :
625 : : static int
626 : 0 : nitrox_enq_single_op(struct nitrox_qp *qp, struct rte_crypto_op *op)
627 : : {
628 : : struct nitrox_crypto_ctx *ctx;
629 : : struct nitrox_softreq *sr;
630 : : int err;
631 : :
632 [ # # ]: 0 : op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
633 : : ctx = get_crypto_ctx(op);
634 [ # # ]: 0 : if (unlikely(!ctx)) {
635 : 0 : op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
636 : 0 : return -EINVAL;
637 : : }
638 : :
639 [ # # # # ]: 0 : if (unlikely(rte_mempool_get(qp->sr_mp, (void **)&sr)))
640 : : return -ENOMEM;
641 : :
642 : 0 : err = nitrox_process_se_req(qp->qno, op, ctx, sr);
643 [ # # ]: 0 : if (unlikely(err)) {
644 [ # # ]: 0 : rte_mempool_put(qp->sr_mp, sr);
645 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
646 : 0 : return err;
647 : : }
648 : :
649 : 0 : nitrox_qp_enqueue(qp, nitrox_sym_instr_addr(sr), sr);
650 : 0 : return 0;
651 : : }
652 : :
653 : : static uint16_t
654 : 0 : nitrox_sym_dev_enq_burst(void *queue_pair, struct rte_crypto_op **ops,
655 : : uint16_t nb_ops)
656 : : {
657 : : struct nitrox_qp *qp = queue_pair;
658 : : uint16_t free_slots = 0;
659 : : uint16_t cnt = 0;
660 : : bool err = false;
661 : :
662 : : free_slots = nitrox_qp_free_count(qp);
663 : : if (nb_ops > free_slots)
664 : : nb_ops = free_slots;
665 : :
666 [ # # ]: 0 : for (cnt = 0; cnt < nb_ops; cnt++) {
667 [ # # ]: 0 : if (unlikely(nitrox_enq_single_op(qp, ops[cnt]))) {
668 : : err = true;
669 : : break;
670 : : }
671 : : }
672 : :
673 : : nitrox_ring_dbell(qp, cnt);
674 : 0 : qp->stats.enqueued_count += cnt;
675 [ # # ]: 0 : if (unlikely(err))
676 : 0 : qp->stats.enqueue_err_count++;
677 : :
678 : 0 : return cnt;
679 : : }
680 : :
681 : : static int
682 : 0 : nitrox_deq_single_op(struct nitrox_qp *qp, struct rte_crypto_op **op_ptr)
683 : : {
684 : : struct nitrox_softreq *sr;
685 : : int ret;
686 : : struct rte_crypto_op *op;
687 : :
688 : : sr = nitrox_qp_get_softreq(qp);
689 : 0 : ret = nitrox_check_se_req(sr, op_ptr);
690 [ # # ]: 0 : if (ret < 0)
691 : : return -EAGAIN;
692 : :
693 : 0 : op = *op_ptr;
694 : : nitrox_qp_dequeue(qp);
695 [ # # ]: 0 : rte_mempool_put(qp->sr_mp, sr);
696 [ # # ]: 0 : if (!ret) {
697 : 0 : op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
698 : 0 : qp->stats.dequeued_count++;
699 : :
700 : 0 : return 0;
701 : : }
702 : :
703 [ # # ]: 0 : if (ret == MC_MAC_MISMATCH_ERR_CODE)
704 : 0 : op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
705 : : else
706 : 0 : op->status = RTE_CRYPTO_OP_STATUS_ERROR;
707 : :
708 : 0 : qp->stats.dequeue_err_count++;
709 : 0 : return 0;
710 : : }
711 : :
712 : : static uint16_t
713 : 0 : nitrox_sym_dev_deq_burst(void *queue_pair, struct rte_crypto_op **ops,
714 : : uint16_t nb_ops)
715 : : {
716 : : struct nitrox_qp *qp = queue_pair;
717 : : uint16_t filled_slots = nitrox_qp_used_count(qp);
718 : : int cnt = 0;
719 : :
720 : : if (nb_ops > filled_slots)
721 : : nb_ops = filled_slots;
722 : :
723 [ # # ]: 0 : for (cnt = 0; cnt < nb_ops; cnt++)
724 [ # # ]: 0 : if (nitrox_deq_single_op(qp, &ops[cnt]))
725 : : break;
726 : :
727 : 0 : return cnt;
728 : : }
729 : :
730 : : static struct rte_cryptodev_ops nitrox_cryptodev_ops = {
731 : : .dev_configure = nitrox_sym_dev_config,
732 : : .dev_start = nitrox_sym_dev_start,
733 : : .dev_stop = nitrox_sym_dev_stop,
734 : : .dev_close = nitrox_sym_dev_close,
735 : : .dev_infos_get = nitrox_sym_dev_info_get,
736 : : .stats_get = nitrox_sym_dev_stats_get,
737 : : .stats_reset = nitrox_sym_dev_stats_reset,
738 : : .queue_pair_setup = nitrox_sym_dev_qp_setup,
739 : : .queue_pair_release = nitrox_sym_dev_qp_release,
740 : : .sym_session_get_size = nitrox_sym_dev_sess_get_size,
741 : : .sym_session_configure = nitrox_sym_dev_sess_configure,
742 : : .sym_session_clear = nitrox_sym_dev_sess_clear
743 : : };
744 : :
745 : : int
746 : 0 : nitrox_sym_pmd_create(struct nitrox_device *ndev)
747 : : {
748 : : char name[RTE_CRYPTODEV_NAME_MAX_LEN];
749 : 0 : struct rte_cryptodev_pmd_init_params init_params = {
750 : : .name = "",
751 : 0 : .socket_id = ndev->pdev->device.numa_node,
752 : : .private_data_size = sizeof(struct nitrox_sym_device)
753 : : };
754 : : struct rte_cryptodev *cdev;
755 : :
756 : 0 : rte_pci_device_name(&ndev->pdev->addr, name, sizeof(name));
757 : 0 : snprintf(name + strlen(name), RTE_CRYPTODEV_NAME_MAX_LEN - strlen(name),
758 : : "_n5sym");
759 : 0 : ndev->rte_sym_dev.driver = &nitrox_rte_sym_drv;
760 : 0 : ndev->rte_sym_dev.numa_node = ndev->pdev->device.numa_node;
761 : 0 : ndev->rte_sym_dev.devargs = NULL;
762 : 0 : cdev = rte_cryptodev_pmd_create(name, &ndev->rte_sym_dev,
763 : : &init_params);
764 [ # # ]: 0 : if (!cdev) {
765 : 0 : NITROX_LOG(ERR, "Cryptodev '%s' creation failed\n", name);
766 : 0 : return -ENODEV;
767 : : }
768 : :
769 : 0 : ndev->rte_sym_dev.name = cdev->data->name;
770 : 0 : cdev->driver_id = nitrox_sym_drv_id;
771 : 0 : cdev->dev_ops = &nitrox_cryptodev_ops;
772 : 0 : cdev->enqueue_burst = nitrox_sym_dev_enq_burst;
773 : 0 : cdev->dequeue_burst = nitrox_sym_dev_deq_burst;
774 : 0 : cdev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
775 : : RTE_CRYPTODEV_FF_HW_ACCELERATED |
776 : : RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
777 : : RTE_CRYPTODEV_FF_IN_PLACE_SGL |
778 : : RTE_CRYPTODEV_FF_OOP_SGL_IN_SGL_OUT |
779 : : RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
780 : : RTE_CRYPTODEV_FF_OOP_LB_IN_SGL_OUT |
781 : : RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
782 : :
783 : 0 : ndev->sym_dev = cdev->data->dev_private;
784 : 0 : ndev->sym_dev->cdev = cdev;
785 : 0 : ndev->sym_dev->ndev = ndev;
786 : :
787 : 0 : rte_cryptodev_pmd_probing_finish(cdev);
788 : :
789 : 0 : NITROX_LOG(DEBUG, "Created cryptodev '%s', dev_id %d, drv_id %d\n",
790 : : cdev->data->name, cdev->data->dev_id, nitrox_sym_drv_id);
791 : 0 : return 0;
792 : : }
793 : :
794 : : int
795 : 0 : nitrox_sym_pmd_destroy(struct nitrox_device *ndev)
796 : : {
797 : 0 : return rte_cryptodev_pmd_destroy(ndev->sym_dev->cdev);
798 : : }
799 : :
800 : : static struct cryptodev_driver nitrox_crypto_drv;
801 : 235 : RTE_PMD_REGISTER_CRYPTO_DRIVER(nitrox_crypto_drv,
802 : : nitrox_rte_sym_drv,
803 : : nitrox_sym_drv_id);
|