xref: /dpdk/drivers/crypto/mvsam/rte_mrvl_pmd.c (revision d54c72ec)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Marvell International Ltd.
3  * Copyright(c) 2017 Semihalf.
4  * All rights reserved.
5  */
6 
7 #include <rte_common.h>
8 #include <rte_hexdump.h>
9 #include <rte_cryptodev.h>
10 #include <cryptodev_pmd.h>
11 #include <rte_security_driver.h>
12 #include <rte_bus_vdev.h>
13 #include <rte_malloc.h>
14 #include <rte_cpuflags.h>
15 #include <rte_kvargs.h>
16 #include <rte_mvep_common.h>
17 
18 #include "mrvl_pmd_private.h"
19 
20 #define MRVL_PMD_MAX_NB_SESS_ARG		("max_nb_sessions")
21 #define MRVL_PMD_DEFAULT_MAX_NB_SESSIONS	2048
22 
23 static uint8_t cryptodev_driver_id;
24 
25 struct mrvl_pmd_init_params {
26 	struct rte_cryptodev_pmd_init_params common;
27 	uint32_t max_nb_sessions;
28 };
29 
30 const char *mrvl_pmd_valid_params[] = {
31 	RTE_CRYPTODEV_PMD_NAME_ARG,
32 	RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
33 	RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
34 	MRVL_PMD_MAX_NB_SESS_ARG
35 };
36 
37 /**
38  * Flag if particular crypto algorithm is supported by PMD/MUSDK.
39  *
40  * The idea is to have Not Supported value as default (0).
41  * This way we need only to define proper map sizes,
42  * non-initialized entries will be by default not supported.
43  */
44 enum algo_supported {
45 	ALGO_NOT_SUPPORTED = 0,
46 	ALGO_SUPPORTED = 1,
47 };
48 
49 /** Map elements for cipher mapping.*/
50 struct cipher_params_mapping {
51 	enum algo_supported  supported;   /**< On/Off switch */
52 	enum sam_cipher_alg  cipher_alg;  /**< Cipher algorithm */
53 	enum sam_cipher_mode cipher_mode; /**< Cipher mode */
54 	unsigned int max_key_len;         /**< Maximum key length (in bytes)*/
55 }
56 /* We want to squeeze in multiple maps into the cache line. */
57 __rte_aligned(32);
58 
59 /** Map elements for auth mapping.*/
60 struct auth_params_mapping {
61 	enum algo_supported supported;  /**< On/off switch */
62 	enum sam_auth_alg   auth_alg;   /**< Auth algorithm */
63 }
64 /* We want to squeeze in multiple maps into the cache line. */
65 __rte_aligned(32);
66 
67 /**
68  * Map of supported cipher algorithms.
69  */
70 static const
71 struct cipher_params_mapping cipher_map[] = {
72 	[RTE_CRYPTO_CIPHER_NULL] = {
73 		.supported = ALGO_SUPPORTED,
74 		.cipher_alg = SAM_CIPHER_NONE },
75 	[RTE_CRYPTO_CIPHER_3DES_CBC] = {
76 		.supported = ALGO_SUPPORTED,
77 		.cipher_alg = SAM_CIPHER_3DES,
78 		.cipher_mode = SAM_CIPHER_CBC,
79 		.max_key_len = BITS2BYTES(192) },
80 	[RTE_CRYPTO_CIPHER_3DES_CTR] = {
81 		.supported = ALGO_SUPPORTED,
82 		.cipher_alg = SAM_CIPHER_3DES,
83 		.cipher_mode = SAM_CIPHER_CTR,
84 		.max_key_len = BITS2BYTES(192) },
85 	[RTE_CRYPTO_CIPHER_3DES_ECB] = {
86 		.supported = ALGO_SUPPORTED,
87 		.cipher_alg = SAM_CIPHER_3DES,
88 		.cipher_mode = SAM_CIPHER_ECB,
89 		.max_key_len = BITS2BYTES(192) },
90 	[RTE_CRYPTO_CIPHER_AES_CBC] = {
91 		.supported = ALGO_SUPPORTED,
92 		.cipher_alg = SAM_CIPHER_AES,
93 		.cipher_mode = SAM_CIPHER_CBC,
94 		.max_key_len = BITS2BYTES(256) },
95 	[RTE_CRYPTO_CIPHER_AES_CTR] = {
96 		.supported = ALGO_SUPPORTED,
97 		.cipher_alg = SAM_CIPHER_AES,
98 		.cipher_mode = SAM_CIPHER_CTR,
99 		.max_key_len = BITS2BYTES(256) },
100 	[RTE_CRYPTO_CIPHER_AES_ECB] = {
101 		.supported = ALGO_SUPPORTED,
102 		.cipher_alg = SAM_CIPHER_AES,
103 		.cipher_mode = SAM_CIPHER_ECB,
104 		.max_key_len = BITS2BYTES(256) },
105 };
106 
107 /**
108  * Map of supported auth algorithms.
109  */
110 static const
111 struct auth_params_mapping auth_map[] = {
112 	[RTE_CRYPTO_AUTH_NULL] = {
113 		.supported = ALGO_SUPPORTED,
114 		.auth_alg = SAM_AUTH_NONE },
115 	[RTE_CRYPTO_AUTH_MD5_HMAC] = {
116 		.supported = ALGO_SUPPORTED,
117 		.auth_alg = SAM_AUTH_HMAC_MD5 },
118 	[RTE_CRYPTO_AUTH_MD5] = {
119 		.supported = ALGO_SUPPORTED,
120 		.auth_alg = SAM_AUTH_HASH_MD5 },
121 	[RTE_CRYPTO_AUTH_SHA1_HMAC] = {
122 		.supported = ALGO_SUPPORTED,
123 		.auth_alg = SAM_AUTH_HMAC_SHA1 },
124 	[RTE_CRYPTO_AUTH_SHA1] = {
125 		.supported = ALGO_SUPPORTED,
126 		.auth_alg = SAM_AUTH_HASH_SHA1 },
127 	[RTE_CRYPTO_AUTH_SHA224_HMAC] = {
128 		.supported = ALGO_SUPPORTED,
129 		.auth_alg = SAM_AUTH_HMAC_SHA2_224 },
130 	[RTE_CRYPTO_AUTH_SHA224] = {
131 		.supported = ALGO_SUPPORTED,
132 		.auth_alg = SAM_AUTH_HASH_SHA2_224 },
133 	[RTE_CRYPTO_AUTH_SHA256_HMAC] = {
134 		.supported = ALGO_SUPPORTED,
135 		.auth_alg = SAM_AUTH_HMAC_SHA2_256 },
136 	[RTE_CRYPTO_AUTH_SHA256] = {
137 		.supported = ALGO_SUPPORTED,
138 		.auth_alg = SAM_AUTH_HASH_SHA2_256 },
139 	[RTE_CRYPTO_AUTH_SHA384_HMAC] = {
140 		.supported = ALGO_SUPPORTED,
141 		.auth_alg = SAM_AUTH_HMAC_SHA2_384 },
142 	[RTE_CRYPTO_AUTH_SHA384] = {
143 		.supported = ALGO_SUPPORTED,
144 		.auth_alg = SAM_AUTH_HASH_SHA2_384 },
145 	[RTE_CRYPTO_AUTH_SHA512_HMAC] = {
146 		.supported = ALGO_SUPPORTED,
147 		.auth_alg = SAM_AUTH_HMAC_SHA2_512 },
148 	[RTE_CRYPTO_AUTH_SHA512] = {
149 		.supported = ALGO_SUPPORTED,
150 		.auth_alg = SAM_AUTH_HASH_SHA2_512 },
151 	[RTE_CRYPTO_AUTH_AES_GMAC] = {
152 		.supported = ALGO_SUPPORTED,
153 		.auth_alg = SAM_AUTH_AES_GMAC },
154 };
155 
156 /**
157  * Map of supported aead algorithms.
158  */
159 static const
160 struct cipher_params_mapping aead_map[] = {
161 	[RTE_CRYPTO_AEAD_AES_GCM] = {
162 		.supported = ALGO_SUPPORTED,
163 		.cipher_alg = SAM_CIPHER_AES,
164 		.cipher_mode = SAM_CIPHER_GCM,
165 		.max_key_len = BITS2BYTES(256) },
166 };
167 
168 /*
169  *-----------------------------------------------------------------------------
170  * Forward declarations.
171  *-----------------------------------------------------------------------------
172  */
173 static int cryptodev_mrvl_crypto_uninit(struct rte_vdev_device *vdev);
174 
175 /*
176  *-----------------------------------------------------------------------------
177  * Session Preparation.
178  *-----------------------------------------------------------------------------
179  */
180 
181 /**
182  * Get xform chain order.
183  *
184  * @param xform Pointer to configuration structure chain for crypto operations.
185  * @returns Order of crypto operations.
186  */
187 static enum mrvl_crypto_chain_order
mrvl_crypto_get_chain_order(const struct rte_crypto_sym_xform * xform)188 mrvl_crypto_get_chain_order(const struct rte_crypto_sym_xform *xform)
189 {
190 	/* Currently, Marvell supports max 2 operations in chain */
191 	if (xform->next != NULL && xform->next->next != NULL)
192 		return MRVL_CRYPTO_CHAIN_NOT_SUPPORTED;
193 
194 	if (xform->next != NULL) {
195 		if ((xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
196 			(xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER))
197 			return MRVL_CRYPTO_CHAIN_AUTH_CIPHER;
198 
199 		if ((xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) &&
200 			(xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH))
201 			return MRVL_CRYPTO_CHAIN_CIPHER_AUTH;
202 	} else {
203 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH)
204 			return MRVL_CRYPTO_CHAIN_AUTH_ONLY;
205 
206 		if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
207 			return MRVL_CRYPTO_CHAIN_CIPHER_ONLY;
208 
209 		if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
210 			return MRVL_CRYPTO_CHAIN_COMBINED;
211 	}
212 	return MRVL_CRYPTO_CHAIN_NOT_SUPPORTED;
213 }
214 
215 /**
216  * Set session parameters for cipher part.
217  *
218  * @param sess Crypto session pointer.
219  * @param cipher_xform Pointer to configuration structure for cipher operations.
220  * @returns 0 in case of success, negative value otherwise.
221  */
222 static int
mrvl_crypto_set_cipher_session_parameters(struct mrvl_crypto_session * sess,const struct rte_crypto_sym_xform * cipher_xform)223 mrvl_crypto_set_cipher_session_parameters(struct mrvl_crypto_session *sess,
224 		const struct rte_crypto_sym_xform *cipher_xform)
225 {
226 	uint8_t *cipher_key;
227 
228 	/* Make sure we've got proper struct */
229 	if (cipher_xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
230 		MRVL_LOG(ERR, "Wrong xform struct provided!");
231 		return -EINVAL;
232 	}
233 
234 	/* See if map data is present and valid */
235 	if ((cipher_xform->cipher.algo > RTE_DIM(cipher_map)) ||
236 		(cipher_map[cipher_xform->cipher.algo].supported
237 			!= ALGO_SUPPORTED)) {
238 		MRVL_LOG(ERR, "Cipher algorithm not supported!");
239 		return -EINVAL;
240 	}
241 
242 	sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
243 
244 	sess->sam_sess_params.dir =
245 		(cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
246 		SAM_DIR_ENCRYPT : SAM_DIR_DECRYPT;
247 	sess->sam_sess_params.cipher_alg =
248 		cipher_map[cipher_xform->cipher.algo].cipher_alg;
249 	sess->sam_sess_params.cipher_mode =
250 		cipher_map[cipher_xform->cipher.algo].cipher_mode;
251 
252 	/* Assume IV will be passed together with data. */
253 	sess->sam_sess_params.cipher_iv = NULL;
254 
255 	/* Get max key length. */
256 	if (cipher_xform->cipher.key.length >
257 		cipher_map[cipher_xform->cipher.algo].max_key_len) {
258 		MRVL_LOG(ERR, "Wrong key length!");
259 		return -EINVAL;
260 	}
261 
262 	cipher_key = malloc(cipher_xform->cipher.key.length);
263 	if (cipher_key == NULL) {
264 		MRVL_LOG(ERR, "Insufficient memory!");
265 		return -ENOMEM;
266 	}
267 
268 	memcpy(cipher_key, cipher_xform->cipher.key.data,
269 			cipher_xform->cipher.key.length);
270 
271 	sess->sam_sess_params.cipher_key_len = cipher_xform->cipher.key.length;
272 	sess->sam_sess_params.cipher_key = cipher_key;
273 
274 	return 0;
275 }
276 
277 /**
278  * Set session parameters for authentication part.
279  *
280  * @param sess Crypto session pointer.
281  * @param auth_xform Pointer to configuration structure for auth operations.
282  * @returns 0 in case of success, negative value otherwise.
283  */
284 static int
mrvl_crypto_set_auth_session_parameters(struct mrvl_crypto_session * sess,const struct rte_crypto_sym_xform * auth_xform)285 mrvl_crypto_set_auth_session_parameters(struct mrvl_crypto_session *sess,
286 		const struct rte_crypto_sym_xform *auth_xform)
287 {
288 	uint8_t *auth_key = NULL;
289 
290 	/* Make sure we've got proper struct */
291 	if (auth_xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
292 		MRVL_LOG(ERR, "Wrong xform struct provided!");
293 		return -EINVAL;
294 	}
295 
296 	/* See if map data is present and valid */
297 	if ((auth_xform->auth.algo > RTE_DIM(auth_map)) ||
298 		(auth_map[auth_xform->auth.algo].supported != ALGO_SUPPORTED)) {
299 		MRVL_LOG(ERR, "Auth algorithm not supported!");
300 		return -EINVAL;
301 	}
302 
303 	sess->sam_sess_params.dir =
304 		(auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
305 		SAM_DIR_ENCRYPT : SAM_DIR_DECRYPT;
306 	sess->sam_sess_params.auth_alg =
307 		auth_map[auth_xform->auth.algo].auth_alg;
308 	sess->sam_sess_params.u.basic.auth_icv_len =
309 		auth_xform->auth.digest_length;
310 
311 	if (auth_xform->auth.key.length > 0) {
312 		auth_key = malloc(auth_xform->auth.key.length);
313 		if (auth_key == NULL) {
314 			MRVL_LOG(ERR, "Not enough memory!");
315 			return -EINVAL;
316 		}
317 
318 		memcpy(auth_key, auth_xform->auth.key.data,
319 				auth_xform->auth.key.length);
320 	}
321 
322 	/* auth_key must be NULL if auth algorithm does not use HMAC */
323 	sess->sam_sess_params.auth_key = auth_key;
324 	sess->sam_sess_params.auth_key_len = auth_xform->auth.key.length;
325 
326 	return 0;
327 }
328 
329 /**
330  * Set session parameters for aead part.
331  *
332  * @param sess Crypto session pointer.
333  * @param aead_xform Pointer to configuration structure for aead operations.
334  * @returns 0 in case of success, negative value otherwise.
335  */
336 static int
mrvl_crypto_set_aead_session_parameters(struct mrvl_crypto_session * sess,const struct rte_crypto_sym_xform * aead_xform)337 mrvl_crypto_set_aead_session_parameters(struct mrvl_crypto_session *sess,
338 		const struct rte_crypto_sym_xform *aead_xform)
339 {
340 	uint8_t *aead_key;
341 
342 	/* Make sure we've got proper struct */
343 	if (aead_xform->type != RTE_CRYPTO_SYM_XFORM_AEAD) {
344 		MRVL_LOG(ERR, "Wrong xform struct provided!");
345 		return -EINVAL;
346 	}
347 
348 	/* See if map data is present and valid */
349 	if ((aead_xform->aead.algo > RTE_DIM(aead_map)) ||
350 		(aead_map[aead_xform->aead.algo].supported
351 			!= ALGO_SUPPORTED)) {
352 		MRVL_LOG(ERR, "AEAD algorithm not supported!");
353 		return -EINVAL;
354 	}
355 
356 	sess->sam_sess_params.dir =
357 		(aead_xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
358 		SAM_DIR_ENCRYPT : SAM_DIR_DECRYPT;
359 	sess->sam_sess_params.cipher_alg =
360 		aead_map[aead_xform->aead.algo].cipher_alg;
361 	sess->sam_sess_params.cipher_mode =
362 		aead_map[aead_xform->aead.algo].cipher_mode;
363 
364 	if (sess->sam_sess_params.cipher_mode == SAM_CIPHER_GCM) {
365 		/* IV must include nonce for all counter modes */
366 		sess->cipher_iv_offset = aead_xform->cipher.iv.offset;
367 
368 		/* Set order of authentication then encryption to 0 in GCM */
369 		sess->sam_sess_params.u.basic.auth_then_encrypt = 0;
370 	}
371 
372 	/* Assume IV will be passed together with data. */
373 	sess->sam_sess_params.cipher_iv = NULL;
374 
375 	/* Get max key length. */
376 	if (aead_xform->aead.key.length >
377 		aead_map[aead_xform->aead.algo].max_key_len) {
378 		MRVL_LOG(ERR, "Wrong key length!");
379 		return -EINVAL;
380 	}
381 
382 	aead_key = malloc(aead_xform->aead.key.length);
383 	if (aead_key == NULL) {
384 		MRVL_LOG(ERR, "Insufficient memory!");
385 		return -ENOMEM;
386 	}
387 
388 	memcpy(aead_key, aead_xform->aead.key.data,
389 			aead_xform->aead.key.length);
390 
391 	sess->sam_sess_params.cipher_key = aead_key;
392 	sess->sam_sess_params.cipher_key_len = aead_xform->aead.key.length;
393 
394 	if (sess->sam_sess_params.cipher_mode == SAM_CIPHER_GCM)
395 		sess->sam_sess_params.auth_alg = SAM_AUTH_AES_GCM;
396 
397 	sess->sam_sess_params.u.basic.auth_icv_len =
398 		aead_xform->aead.digest_length;
399 
400 	sess->sam_sess_params.u.basic.auth_aad_len =
401 		aead_xform->aead.aad_length;
402 
403 	return 0;
404 }
405 
406 /**
407  * Parse crypto transform chain and setup session parameters.
408  *
409  * @param dev Pointer to crypto device
410  * @param sess Pointer to crypto session
411  * @param xform Pointer to configuration structure chain for crypto operations.
412  * @returns 0 in case of success, negative value otherwise.
413  */
414 int
mrvl_crypto_set_session_parameters(struct mrvl_crypto_session * sess,const struct rte_crypto_sym_xform * xform)415 mrvl_crypto_set_session_parameters(struct mrvl_crypto_session *sess,
416 		const struct rte_crypto_sym_xform *xform)
417 {
418 	const struct rte_crypto_sym_xform *cipher_xform = NULL;
419 	const struct rte_crypto_sym_xform *auth_xform = NULL;
420 	const struct rte_crypto_sym_xform *aead_xform = NULL;
421 
422 	/* Filter out spurious/broken requests */
423 	if (xform == NULL)
424 		return -EINVAL;
425 
426 	sess->chain_order = mrvl_crypto_get_chain_order(xform);
427 	switch (sess->chain_order) {
428 	case MRVL_CRYPTO_CHAIN_CIPHER_AUTH:
429 		cipher_xform = xform;
430 		auth_xform = xform->next;
431 		break;
432 	case MRVL_CRYPTO_CHAIN_AUTH_CIPHER:
433 		auth_xform = xform;
434 		cipher_xform = xform->next;
435 		break;
436 	case MRVL_CRYPTO_CHAIN_CIPHER_ONLY:
437 		cipher_xform = xform;
438 		break;
439 	case MRVL_CRYPTO_CHAIN_AUTH_ONLY:
440 		auth_xform = xform;
441 		break;
442 	case MRVL_CRYPTO_CHAIN_COMBINED:
443 		aead_xform = xform;
444 		break;
445 	default:
446 		return -EINVAL;
447 	}
448 
449 	if ((cipher_xform != NULL) &&
450 		(mrvl_crypto_set_cipher_session_parameters(
451 			sess, cipher_xform) < 0)) {
452 		MRVL_LOG(ERR, "Invalid/unsupported cipher parameters!");
453 		return -EINVAL;
454 	}
455 
456 	if ((auth_xform != NULL) &&
457 		(mrvl_crypto_set_auth_session_parameters(
458 			sess, auth_xform) < 0)) {
459 		MRVL_LOG(ERR, "Invalid/unsupported auth parameters!");
460 		return -EINVAL;
461 	}
462 
463 	if ((aead_xform != NULL) &&
464 		(mrvl_crypto_set_aead_session_parameters(
465 			sess, aead_xform) < 0)) {
466 		MRVL_LOG(ERR, "Invalid/unsupported aead parameters!");
467 		return -EINVAL;
468 	}
469 
470 	return 0;
471 }
472 
473 static int
replay_wsz_to_mask(uint32_t replay_win_sz)474 replay_wsz_to_mask(uint32_t replay_win_sz)
475 {
476 	int mask = 0;
477 
478 	switch (replay_win_sz) {
479 	case 0:
480 		mask = SAM_ANTI_REPLY_MASK_NONE;
481 		break;
482 	case 32:
483 		mask = SAM_ANTI_REPLY_MASK_32B;
484 		break;
485 	case 64:
486 		mask = SAM_ANTI_REPLY_MASK_64B;
487 		break;
488 	case 128:
489 		mask = SAM_ANTI_REPLY_MASK_128B;
490 		break;
491 	default:
492 		MRVL_LOG(ERR, "Invalid antireplay window size");
493 		return -EINVAL;
494 	}
495 
496 	return mask;
497 }
498 
499 /**
500  * Parse IPSEC session parameters.
501  *
502  * @param sess Pointer to security session
503  * @param ipsec_xform Pointer to configuration structure IPSEC operations.
504  * @param crypto_xform Pointer to chain for crypto operations.
505  * @returns 0 in case of success, negative value otherwise.
506  */
507 int
mrvl_ipsec_set_session_parameters(struct mrvl_crypto_session * sess,struct rte_security_ipsec_xform * ipsec_xform,struct rte_crypto_sym_xform * crypto_xform)508 mrvl_ipsec_set_session_parameters(struct mrvl_crypto_session *sess,
509 		struct rte_security_ipsec_xform *ipsec_xform,
510 		struct rte_crypto_sym_xform *crypto_xform)
511 {
512 	int seq_mask_size;
513 
514 	/* Filter out spurious/broken requests */
515 	if (ipsec_xform == NULL || crypto_xform == NULL)
516 		return -EINVAL;
517 
518 	/* Crypto parameters handling */
519 	if (mrvl_crypto_set_session_parameters(sess, crypto_xform))
520 		return -EINVAL;
521 
522 	seq_mask_size = replay_wsz_to_mask(ipsec_xform->replay_win_sz);
523 	if (seq_mask_size < 0)
524 		return -EINVAL;
525 
526 	/* IPSEC protocol parameters handling */
527 	sess->sam_sess_params.proto = SAM_PROTO_IPSEC;
528 	sess->sam_sess_params.u.ipsec.is_esp =
529 		(ipsec_xform->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) ?
530 		1 : 0;
531 	sess->sam_sess_params.u.ipsec.is_ip6 = 0;
532 	sess->sam_sess_params.u.ipsec.is_tunnel =
533 		(ipsec_xform->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) ?
534 		1 : 0;
535 	sess->sam_sess_params.u.ipsec.is_esn = ipsec_xform->options.esn;
536 	sess->sam_sess_params.u.ipsec.seq_mask_size = seq_mask_size;
537 
538 	sess->sam_sess_params.u.ipsec.tunnel.u.ipv4.sip =
539 		(uint8_t *)(&ipsec_xform->tunnel.ipv4.src_ip.s_addr);
540 	sess->sam_sess_params.u.ipsec.tunnel.u.ipv4.dip =
541 		(uint8_t *)&(ipsec_xform->tunnel.ipv4.dst_ip.s_addr);
542 
543 	sess->sam_sess_params.u.ipsec.tunnel.u.ipv4.dscp =
544 		ipsec_xform->tunnel.ipv4.dscp;
545 	sess->sam_sess_params.u.ipsec.tunnel.u.ipv4.ttl =
546 		ipsec_xform->tunnel.ipv4.ttl;
547 	sess->sam_sess_params.u.ipsec.tunnel.u.ipv4.df =
548 		ipsec_xform->tunnel.ipv4.df;
549 	sess->sam_sess_params.u.ipsec.tunnel.copy_dscp =
550 		ipsec_xform->options.copy_dscp;
551 	sess->sam_sess_params.u.ipsec.tunnel.copy_flabel =
552 		ipsec_xform->options.copy_flabel;
553 	sess->sam_sess_params.u.ipsec.tunnel.copy_df =
554 		ipsec_xform->options.copy_df;
555 
556 	sess->sam_sess_params.u.ipsec.is_natt = 0;
557 	sess->sam_sess_params.u.ipsec.spi = ipsec_xform->spi;
558 	sess->sam_sess_params.u.ipsec.seq = 0;
559 
560 	return 0;
561 }
562 
563 /*
564  *-----------------------------------------------------------------------------
565  * Process Operations
566  *-----------------------------------------------------------------------------
567  */
568 
569 /**
570  * Prepare a single request.
571  *
572  * This function basically translates DPDK crypto request into one
573  * understandable by MUDSK's SAM. If this is a first request in a session,
574  * it starts the session.
575  *
576  * @param request Pointer to pre-allocated && reset request buffer [Out].
577  * @param src_bd Pointer to pre-allocated source descriptor [Out].
578  * @param dst_bd Pointer to pre-allocated destination descriptor [Out].
579  * @param op Pointer to DPDK crypto operation struct [In].
580  */
581 static inline int
mrvl_request_prepare_crp(struct sam_cio_op_params * request,struct sam_buf_info * src_bd,struct sam_buf_info * dst_bd,struct rte_crypto_op * op)582 mrvl_request_prepare_crp(struct sam_cio_op_params *request,
583 		struct sam_buf_info *src_bd,
584 		struct sam_buf_info *dst_bd,
585 		struct rte_crypto_op *op)
586 {
587 	struct mrvl_crypto_session *sess;
588 	struct rte_mbuf *src_mbuf, *dst_mbuf;
589 	uint16_t segments_nb;
590 	uint8_t *digest;
591 	int i;
592 
593 	if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
594 		MRVL_LOG(ERR, "MRVL CRYPTO PMD only supports session "
595 				"oriented requests, op (%p) is sessionless!",
596 				op);
597 		return -EINVAL;
598 	}
599 
600 	sess = (struct mrvl_crypto_session *)get_sym_session_private_data(
601 					     op->sym->session,
602 					     cryptodev_driver_id);
603 	if (unlikely(sess == NULL)) {
604 		MRVL_LOG(ERR, "Session was not created for this device!");
605 		return -EINVAL;
606 	}
607 
608 	request->sa = sess->sam_sess;
609 	request->cookie = op;
610 
611 	src_mbuf = op->sym->m_src;
612 	segments_nb = src_mbuf->nb_segs;
613 	/* The following conditions must be met:
614 	 * - Destination buffer is required when segmented source buffer
615 	 * - Segmented destination buffer is not supported
616 	 */
617 	if ((segments_nb > 1) && (!op->sym->m_dst)) {
618 		MRVL_LOG(ERR, "op->sym->m_dst = NULL!");
619 		return -1;
620 	}
621 	/* For non SG case:
622 	 * If application delivered us null dst buffer, it means it expects
623 	 * us to deliver the result in src buffer.
624 	 */
625 	dst_mbuf = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
626 
627 	if (!rte_pktmbuf_is_contiguous(dst_mbuf)) {
628 		MRVL_LOG(ERR, "Segmented destination buffer not supported!");
629 		return -1;
630 	}
631 
632 	request->num_bufs = segments_nb;
633 	for (i = 0; i < segments_nb; i++) {
634 		/* Empty source. */
635 		if (rte_pktmbuf_data_len(src_mbuf) == 0) {
636 			/* EIP does not support 0 length buffers. */
637 			MRVL_LOG(ERR, "Buffer length == 0 not supported!");
638 			return -1;
639 		}
640 		src_bd[i].vaddr = rte_pktmbuf_mtod(src_mbuf, void *);
641 		src_bd[i].paddr = rte_pktmbuf_iova(src_mbuf);
642 		src_bd[i].len = rte_pktmbuf_data_len(src_mbuf);
643 
644 		src_mbuf = src_mbuf->next;
645 	}
646 	request->src = src_bd;
647 
648 	/* Empty destination. */
649 	if (rte_pktmbuf_data_len(dst_mbuf) == 0) {
650 		/* Make dst buffer fit at least source data. */
651 		if (rte_pktmbuf_append(dst_mbuf,
652 			rte_pktmbuf_data_len(op->sym->m_src)) == NULL) {
653 			MRVL_LOG(ERR, "Unable to set big enough dst buffer!");
654 			return -1;
655 		}
656 	}
657 
658 	request->dst = dst_bd;
659 	dst_bd->vaddr = rte_pktmbuf_mtod(dst_mbuf, void *);
660 	dst_bd->paddr = rte_pktmbuf_iova(dst_mbuf);
661 
662 	/*
663 	 * We can use all available space in dst_mbuf,
664 	 * not only what's used currently.
665 	 */
666 	dst_bd->len = dst_mbuf->buf_len - rte_pktmbuf_headroom(dst_mbuf);
667 
668 	if (sess->chain_order == MRVL_CRYPTO_CHAIN_COMBINED) {
669 		request->cipher_len = op->sym->aead.data.length;
670 		request->cipher_offset = op->sym->aead.data.offset;
671 		request->cipher_iv = rte_crypto_op_ctod_offset(op, uint8_t *,
672 						  sess->cipher_iv_offset);
673 
674 		request->auth_aad = op->sym->aead.aad.data;
675 		request->auth_offset = request->cipher_offset;
676 		request->auth_len = request->cipher_len;
677 	} else {
678 		request->cipher_len = op->sym->cipher.data.length;
679 		request->cipher_offset = op->sym->cipher.data.offset;
680 		request->cipher_iv = rte_crypto_op_ctod_offset(op, uint8_t *,
681 				sess->cipher_iv_offset);
682 
683 		request->auth_offset = op->sym->auth.data.offset;
684 		request->auth_len = op->sym->auth.data.length;
685 	}
686 
687 	digest = sess->chain_order == MRVL_CRYPTO_CHAIN_COMBINED ?
688 		op->sym->aead.digest.data : op->sym->auth.digest.data;
689 	if (digest == NULL) {
690 		/* No auth - no worry. */
691 		return 0;
692 	}
693 
694 	request->auth_icv_offset = request->auth_offset + request->auth_len;
695 
696 	/*
697 	 * EIP supports only scenarios where ICV(digest buffer) is placed at
698 	 * auth_icv_offset.
699 	 */
700 	if (sess->sam_sess_params.dir == SAM_DIR_ENCRYPT) {
701 		/*
702 		 * This should be the most common case anyway,
703 		 * EIP will overwrite DST buffer at auth_icv_offset.
704 		 */
705 		if (rte_pktmbuf_mtod_offset(
706 				dst_mbuf, uint8_t *,
707 				request->auth_icv_offset) == digest)
708 			return 0;
709 	} else {/* sess->sam_sess_params.dir == SAM_DIR_DECRYPT */
710 		/*
711 		 * EIP will look for digest at auth_icv_offset
712 		 * offset in SRC buffer. It must be placed in the last
713 		 * segment and the offset must be set to reach digest
714 		 * in the last segment
715 		 */
716 		struct rte_mbuf *last_seg =  op->sym->m_src;
717 		uint32_t d_offset = request->auth_icv_offset;
718 		u32 d_size = sess->sam_sess_params.u.basic.auth_icv_len;
719 		unsigned char *d_ptr;
720 
721 		/* Find the last segment and the offset for the last segment */
722 		while ((last_seg->next != NULL) &&
723 				(d_offset >= last_seg->data_len)) {
724 			d_offset -= last_seg->data_len;
725 			last_seg = last_seg->next;
726 		}
727 
728 		if (rte_pktmbuf_mtod_offset(last_seg, uint8_t *,
729 					    d_offset) == digest)
730 			return 0;
731 
732 		/* copy digest to last segment */
733 		if (last_seg->buf_len >= (d_size + d_offset)) {
734 			d_ptr = (unsigned char *)last_seg->buf_addr +
735 				 d_offset;
736 			rte_memcpy(d_ptr, digest, d_size);
737 			return 0;
738 		}
739 	}
740 
741 	/*
742 	 * If we landed here it means that digest pointer is
743 	 * at different than expected place.
744 	 */
745 	return -1;
746 }
747 
748 /**
749  * Prepare a single security protocol request.
750  *
751  * This function basically translates DPDK security request into one
752  * understandable by MUDSK's SAM. If this is a first request in a session,
753  * it starts the session.
754  *
755  * @param request Pointer to pre-allocated && reset request buffer [Out].
756  * @param src_bd Pointer to pre-allocated source descriptor [Out].
757  * @param dst_bd Pointer to pre-allocated destination descriptor [Out].
758  * @param op Pointer to DPDK crypto operation struct [In].
759  */
760 static inline int
mrvl_request_prepare_sec(struct sam_cio_ipsec_params * request,struct sam_buf_info * src_bd,struct sam_buf_info * dst_bd,struct rte_crypto_op * op)761 mrvl_request_prepare_sec(struct sam_cio_ipsec_params *request,
762 		struct sam_buf_info *src_bd,
763 		struct sam_buf_info *dst_bd,
764 		struct rte_crypto_op *op)
765 {
766 	struct mrvl_crypto_session *sess;
767 	struct rte_mbuf *src_mbuf, *dst_mbuf;
768 	uint16_t segments_nb;
769 	int i;
770 
771 	if (unlikely(op->sess_type != RTE_CRYPTO_OP_SECURITY_SESSION)) {
772 		MRVL_LOG(ERR, "MRVL SECURITY: sess_type is not SECURITY_SESSION");
773 		return -EINVAL;
774 	}
775 
776 	sess = (struct mrvl_crypto_session *)get_sec_session_private_data(
777 			op->sym->sec_session);
778 	if (unlikely(sess == NULL)) {
779 		MRVL_LOG(ERR, "Session was not created for this device! %d",
780 			 cryptodev_driver_id);
781 		return -EINVAL;
782 	}
783 
784 	request->sa = sess->sam_sess;
785 	request->cookie = op;
786 	src_mbuf = op->sym->m_src;
787 	segments_nb = src_mbuf->nb_segs;
788 	/* The following conditions must be met:
789 	 * - Destination buffer is required when segmented source buffer
790 	 * - Segmented destination buffer is not supported
791 	 */
792 	if ((segments_nb > 1) && (!op->sym->m_dst)) {
793 		MRVL_LOG(ERR, "op->sym->m_dst = NULL!");
794 		return -1;
795 	}
796 	/* For non SG case:
797 	 * If application delivered us null dst buffer, it means it expects
798 	 * us to deliver the result in src buffer.
799 	 */
800 	dst_mbuf = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
801 
802 	if (!rte_pktmbuf_is_contiguous(dst_mbuf)) {
803 		MRVL_LOG(ERR, "Segmented destination buffer not supported!");
804 		return -1;
805 	}
806 
807 	request->num_bufs = segments_nb;
808 	for (i = 0; i < segments_nb; i++) {
809 		/* Empty source. */
810 		if (rte_pktmbuf_data_len(src_mbuf) == 0) {
811 			/* EIP does not support 0 length buffers. */
812 			MRVL_LOG(ERR, "Buffer length == 0 not supported!");
813 			return -1;
814 		}
815 		src_bd[i].vaddr = rte_pktmbuf_mtod(src_mbuf, void *);
816 		src_bd[i].paddr = rte_pktmbuf_iova(src_mbuf);
817 		src_bd[i].len = rte_pktmbuf_data_len(src_mbuf);
818 
819 		src_mbuf = src_mbuf->next;
820 	}
821 	request->src = src_bd;
822 
823 	/* Empty destination. */
824 	if (rte_pktmbuf_data_len(dst_mbuf) == 0) {
825 		/* Make dst buffer fit at least source data. */
826 		if (rte_pktmbuf_append(dst_mbuf,
827 			rte_pktmbuf_data_len(op->sym->m_src)) == NULL) {
828 			MRVL_LOG(ERR, "Unable to set big enough dst buffer!");
829 			return -1;
830 		}
831 	}
832 
833 	request->dst = dst_bd;
834 	dst_bd->vaddr = rte_pktmbuf_mtod(dst_mbuf, void *);
835 	dst_bd->paddr = rte_pktmbuf_iova(dst_mbuf);
836 
837 	/*
838 	 * We can use all available space in dst_mbuf,
839 	 * not only what's used currently.
840 	 */
841 	dst_bd->len = dst_mbuf->buf_len - rte_pktmbuf_headroom(dst_mbuf);
842 
843 
844 	request->l3_offset = 0;
845 	request->pkt_size = rte_pktmbuf_pkt_len(op->sym->m_src);
846 
847 	return 0;
848 }
849 
850 /*
851  *-----------------------------------------------------------------------------
852  * PMD Framework handlers
853  *-----------------------------------------------------------------------------
854  */
855 
856 /**
857  * Enqueue burst.
858  *
859  * @param queue_pair Pointer to queue pair.
860  * @param ops Pointer to ops requests array.
861  * @param nb_ops Number of elements in ops requests array.
862  * @returns Number of elements consumed from ops.
863  */
864 static uint16_t
mrvl_crypto_pmd_enqueue_burst(void * queue_pair,struct rte_crypto_op ** ops,uint16_t nb_ops)865 mrvl_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
866 		uint16_t nb_ops)
867 {
868 	uint16_t iter_ops = 0;
869 	uint16_t to_enq_crp = 0;
870 	uint16_t to_enq_sec = 0;
871 	uint16_t consumed = 0;
872 	int ret;
873 	int iter;
874 	struct sam_cio_op_params requests_crp[nb_ops];
875 	struct sam_cio_ipsec_params requests_sec[nb_ops];
876 	uint16_t indx_map_crp[nb_ops];
877 	uint16_t indx_map_sec[nb_ops];
878 
879 	/*
880 	 * SAM does not store bd pointers, so on-stack scope will be enough.
881 	 */
882 	struct mrvl_crypto_src_table src_bd[nb_ops];
883 	struct sam_buf_info          dst_bd[nb_ops];
884 	struct mrvl_crypto_qp *qp = (struct mrvl_crypto_qp *)queue_pair;
885 
886 	if (nb_ops == 0)
887 		return 0;
888 
889 	/* Prepare the burst. */
890 	memset(&requests_crp, 0, sizeof(requests_crp));
891 	memset(&requests_sec, 0, sizeof(requests_sec));
892 	memset(&src_bd, 0, sizeof(src_bd));
893 
894 	/* Iterate through */
895 	for (; iter_ops < nb_ops; ++iter_ops) {
896 		/* store the op id for debug */
897 		if (ops[iter_ops]->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
898 			src_bd[iter_ops].iter_ops = to_enq_crp;
899 			indx_map_crp[to_enq_crp] = iter_ops;
900 
901 			if (mrvl_request_prepare_crp(&requests_crp[to_enq_crp],
902 						src_bd[iter_ops].src_bd,
903 						&dst_bd[iter_ops],
904 						ops[iter_ops]) < 0) {
905 				MRVL_LOG(ERR,
906 					"Error while preparing parameters!");
907 				qp->stats.enqueue_err_count++;
908 				ops[iter_ops]->status =
909 					RTE_CRYPTO_OP_STATUS_ERROR;
910 				/*
911 				 * Number of handled ops is increased
912 				 * (even if the result of handling is error).
913 				 */
914 				++consumed;
915 
916 				break;
917 			}
918 			/* Increase the number of ops to enqueue. */
919 			++to_enq_crp;
920 		} else {
921 			src_bd[iter_ops].iter_ops = to_enq_sec;
922 			indx_map_sec[to_enq_sec] = iter_ops;
923 			if (mrvl_request_prepare_sec(&requests_sec[to_enq_sec],
924 						src_bd[iter_ops].src_bd,
925 						&dst_bd[iter_ops],
926 						ops[iter_ops]) < 0) {
927 				MRVL_LOG(ERR,
928 					"Error while preparing parameters!");
929 				qp->stats.enqueue_err_count++;
930 				ops[iter_ops]->status =
931 					RTE_CRYPTO_OP_STATUS_ERROR;
932 				/*
933 				 * Number of handled ops is increased
934 				 * (even if the result of handling is error).
935 				 */
936 				++consumed;
937 
938 				break;
939 			}
940 			/* Increase the number of ops to enqueue. */
941 			++to_enq_sec;
942 		}
943 
944 		ops[iter_ops]->status =
945 			RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
946 
947 	} /* for (; iter_ops < nb_ops;... */
948 
949 	if (to_enq_crp > 0) {
950 		/* Send the burst */
951 		ret = sam_cio_enq(qp->cio, requests_crp, &to_enq_crp);
952 		consumed += to_enq_crp;
953 		if (ret < 0) {
954 			/*
955 			 * Trust SAM that in this case returned value will be at
956 			 * some point correct (now it is returned unmodified).
957 			 */
958 			qp->stats.enqueue_err_count += to_enq_crp;
959 			for (iter = 0; iter < to_enq_crp; ++iter)
960 				ops[indx_map_crp[iter]]->status =
961 					RTE_CRYPTO_OP_STATUS_ERROR;
962 		}
963 	}
964 
965 	if (to_enq_sec > 0) {
966 		/* Send the burst */
967 		ret = sam_cio_enq_ipsec(qp->cio, requests_sec, &to_enq_sec);
968 		consumed += to_enq_sec;
969 		if (ret < 0) {
970 			/*
971 			 * Trust SAM that in this case returned value will be at
972 			 * some point correct (now it is returned unmodified).
973 			 */
974 			qp->stats.enqueue_err_count += to_enq_sec;
975 			for (iter = 0; iter < to_enq_crp; ++iter)
976 				ops[indx_map_sec[iter]]->status =
977 					RTE_CRYPTO_OP_STATUS_ERROR;
978 		}
979 	}
980 
981 	qp->stats.enqueued_count += to_enq_sec + to_enq_crp;
982 	return consumed;
983 }
984 
985 /**
986  * Dequeue burst.
987  *
988  * @param queue_pair Pointer to queue pair.
989  * @param ops Pointer to ops requests array.
990  * @param nb_ops Number of elements in ops requests array.
991  * @returns Number of elements dequeued.
992  */
993 static uint16_t
mrvl_crypto_pmd_dequeue_burst(void * queue_pair,struct rte_crypto_op ** ops,uint16_t nb_ops)994 mrvl_crypto_pmd_dequeue_burst(void *queue_pair,
995 		struct rte_crypto_op **ops,
996 		uint16_t nb_ops)
997 {
998 	int ret;
999 	struct mrvl_crypto_qp *qp = queue_pair;
1000 	struct sam_cio *cio = qp->cio;
1001 	struct sam_cio_op_result results[nb_ops];
1002 	uint16_t i;
1003 	struct rte_mbuf *dst;
1004 
1005 	ret = sam_cio_deq(cio, results, &nb_ops);
1006 	if (ret < 0) {
1007 		/* Count all dequeued as error. */
1008 		qp->stats.dequeue_err_count += nb_ops;
1009 
1010 		/* But act as they were dequeued anyway*/
1011 		qp->stats.dequeued_count += nb_ops;
1012 
1013 		return 0;
1014 	}
1015 
1016 	/* Unpack and check results. */
1017 	for (i = 0; i < nb_ops; ++i) {
1018 		ops[i] = results[i].cookie;
1019 
1020 		switch (results[i].status) {
1021 		case SAM_CIO_OK:
1022 			ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1023 			if (ops[i]->sess_type ==
1024 				RTE_CRYPTO_OP_SECURITY_SESSION) {
1025 
1026 				if (ops[i]->sym->m_dst)
1027 					dst = ops[i]->sym->m_dst;
1028 				else
1029 					dst = ops[i]->sym->m_src;
1030 				dst->pkt_len = results[i].out_len;
1031 				dst->data_len = results[i].out_len;
1032 			}
1033 			break;
1034 		case SAM_CIO_ERR_ICV:
1035 			MRVL_LOG(DEBUG, "CIO returned SAM_CIO_ERR_ICV.");
1036 			ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1037 			break;
1038 		default:
1039 			MRVL_LOG(DEBUG,
1040 				"CIO returned Error: %d.", results[i].status);
1041 			ops[i]->status = RTE_CRYPTO_OP_STATUS_ERROR;
1042 			break;
1043 		}
1044 	}
1045 
1046 	qp->stats.dequeued_count += nb_ops;
1047 	return nb_ops;
1048 }
1049 
1050 /**
1051  * Create a new crypto device.
1052  *
1053  * @param name Driver name.
1054  * @param vdev Pointer to device structure.
1055  * @param init_params Pointer to initialization parameters.
1056  * @returns 0 in case of success, negative value otherwise.
1057  */
1058 static int
cryptodev_mrvl_crypto_create(const char * name,struct rte_vdev_device * vdev,struct mrvl_pmd_init_params * init_params)1059 cryptodev_mrvl_crypto_create(const char *name,
1060 		struct rte_vdev_device *vdev,
1061 		struct mrvl_pmd_init_params *init_params)
1062 {
1063 	struct rte_cryptodev *dev;
1064 	struct mrvl_crypto_private *internals;
1065 	struct sam_init_params	sam_params;
1066 	struct rte_security_ctx *security_instance;
1067 	int ret = -EINVAL;
1068 
1069 	dev = rte_cryptodev_pmd_create(name, &vdev->device,
1070 			&init_params->common);
1071 	if (dev == NULL) {
1072 		MRVL_LOG(ERR, "Failed to create cryptodev vdev!");
1073 		goto init_error;
1074 	}
1075 
1076 	dev->driver_id = cryptodev_driver_id;
1077 	dev->dev_ops = rte_mrvl_crypto_pmd_ops;
1078 
1079 	/* Register rx/tx burst functions for data path. */
1080 	dev->enqueue_burst = mrvl_crypto_pmd_enqueue_burst;
1081 	dev->dequeue_burst = mrvl_crypto_pmd_dequeue_burst;
1082 
1083 	dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
1084 			RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
1085 			RTE_CRYPTODEV_FF_HW_ACCELERATED |
1086 			RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
1087 			RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
1088 			RTE_CRYPTODEV_FF_SECURITY;
1089 
1090 	internals = dev->data->dev_private;
1091 
1092 	internals->max_nb_qpairs = init_params->common.max_nb_queue_pairs;
1093 	internals->max_nb_sessions = init_params->max_nb_sessions;
1094 
1095 	ret = rte_mvep_init(MVEP_MOD_T_SAM, NULL);
1096 	if (ret)
1097 		goto init_error;
1098 
1099 	sam_params.max_num_sessions = internals->max_nb_sessions;
1100 
1101 	/* Initialize security_ctx only for primary process*/
1102 	security_instance = rte_malloc("rte_security_instances_ops",
1103 		sizeof(struct rte_security_ctx), 0);
1104 	if (security_instance == NULL)
1105 		return -ENOMEM;
1106 	security_instance->device = (void *)dev;
1107 	security_instance->ops = rte_mrvl_security_pmd_ops;
1108 	security_instance->sess_cnt = 0;
1109 	dev->security_ctx = security_instance;
1110 
1111 	/*sam_set_debug_flags(3);*/
1112 
1113 	ret = sam_init(&sam_params);
1114 	if (ret)
1115 		goto init_error;
1116 
1117 	rte_cryptodev_pmd_probing_finish(dev);
1118 
1119 	return 0;
1120 
1121 init_error:
1122 	MRVL_LOG(ERR,
1123 		"Driver %s: %s failed!", init_params->common.name, __func__);
1124 
1125 	cryptodev_mrvl_crypto_uninit(vdev);
1126 	return ret;
1127 }
1128 
1129 /** Parse integer from integer argument */
1130 static int
parse_integer_arg(const char * key __rte_unused,const char * value,void * extra_args)1131 parse_integer_arg(const char *key __rte_unused,
1132 		const char *value, void *extra_args)
1133 {
1134 	int *i = (int *) extra_args;
1135 
1136 	*i = atoi(value);
1137 	if (*i < 0) {
1138 		MRVL_LOG(ERR, "Argument has to be positive!");
1139 		return -EINVAL;
1140 	}
1141 
1142 	return 0;
1143 }
1144 
1145 /** Parse name */
1146 static int
parse_name_arg(const char * key __rte_unused,const char * value,void * extra_args)1147 parse_name_arg(const char *key __rte_unused,
1148 		const char *value, void *extra_args)
1149 {
1150 	struct rte_cryptodev_pmd_init_params *params = extra_args;
1151 
1152 	if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
1153 		MRVL_LOG(ERR, "Invalid name %s, should be less than %u bytes!",
1154 			 value, RTE_CRYPTODEV_NAME_MAX_LEN - 1);
1155 		return -EINVAL;
1156 	}
1157 
1158 	strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
1159 
1160 	return 0;
1161 }
1162 
1163 static int
mrvl_pmd_parse_input_args(struct mrvl_pmd_init_params * params,const char * input_args)1164 mrvl_pmd_parse_input_args(struct mrvl_pmd_init_params *params,
1165 			 const char *input_args)
1166 {
1167 	struct rte_kvargs *kvlist = NULL;
1168 	int ret = 0;
1169 
1170 	if (params == NULL)
1171 		return -EINVAL;
1172 
1173 	if (input_args) {
1174 		kvlist = rte_kvargs_parse(input_args,
1175 					  mrvl_pmd_valid_params);
1176 		if (kvlist == NULL)
1177 			return -1;
1178 
1179 		/* Common VDEV parameters */
1180 		ret = rte_kvargs_process(kvlist,
1181 					 RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
1182 					 &parse_integer_arg,
1183 					 &params->common.max_nb_queue_pairs);
1184 		if (ret < 0)
1185 			goto free_kvlist;
1186 
1187 		ret = rte_kvargs_process(kvlist,
1188 					 RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
1189 					 &parse_integer_arg,
1190 					 &params->common.socket_id);
1191 		if (ret < 0)
1192 			goto free_kvlist;
1193 
1194 		ret = rte_kvargs_process(kvlist,
1195 					 RTE_CRYPTODEV_PMD_NAME_ARG,
1196 					 &parse_name_arg,
1197 					 &params->common.name);
1198 		if (ret < 0)
1199 			goto free_kvlist;
1200 
1201 		ret = rte_kvargs_process(kvlist,
1202 					 MRVL_PMD_MAX_NB_SESS_ARG,
1203 					 &parse_integer_arg,
1204 					 &params->max_nb_sessions);
1205 		if (ret < 0)
1206 			goto free_kvlist;
1207 
1208 	}
1209 
1210 free_kvlist:
1211 	rte_kvargs_free(kvlist);
1212 	return ret;
1213 }
1214 
1215 /**
1216  * Initialize the crypto device.
1217  *
1218  * @param vdev Pointer to device structure.
1219  * @returns 0 in case of success, negative value otherwise.
1220  */
1221 static int
cryptodev_mrvl_crypto_init(struct rte_vdev_device * vdev)1222 cryptodev_mrvl_crypto_init(struct rte_vdev_device *vdev)
1223 {
1224 	struct mrvl_pmd_init_params init_params = {
1225 		.common = {
1226 			.name = "",
1227 			.private_data_size =
1228 				sizeof(struct mrvl_crypto_private),
1229 			.max_nb_queue_pairs =
1230 				sam_get_num_inst() * sam_get_num_cios(0),
1231 			.socket_id = rte_socket_id()
1232 		},
1233 		.max_nb_sessions = MRVL_PMD_DEFAULT_MAX_NB_SESSIONS
1234 	};
1235 
1236 	const char *name, *args;
1237 	int ret;
1238 
1239 	name = rte_vdev_device_name(vdev);
1240 	if (name == NULL)
1241 		return -EINVAL;
1242 	args = rte_vdev_device_args(vdev);
1243 
1244 	ret = mrvl_pmd_parse_input_args(&init_params, args);
1245 	if (ret) {
1246 		MRVL_LOG(ERR, "Failed to parse initialisation arguments[%s]!",
1247 			 args);
1248 		return -EINVAL;
1249 	}
1250 
1251 	return cryptodev_mrvl_crypto_create(name, vdev, &init_params);
1252 }
1253 
1254 /**
1255  * Uninitialize the crypto device
1256  *
1257  * @param vdev Pointer to device structure.
1258  * @returns 0 in case of success, negative value otherwise.
1259  */
1260 static int
cryptodev_mrvl_crypto_uninit(struct rte_vdev_device * vdev)1261 cryptodev_mrvl_crypto_uninit(struct rte_vdev_device *vdev)
1262 {
1263 	struct rte_cryptodev *cryptodev;
1264 	const char *name = rte_vdev_device_name(vdev);
1265 
1266 	if (name == NULL)
1267 		return -EINVAL;
1268 
1269 	MRVL_LOG(INFO, "Closing Marvell crypto device %s on numa socket %u.",
1270 		 name, rte_socket_id());
1271 
1272 	sam_deinit();
1273 	rte_mvep_deinit(MVEP_MOD_T_SAM);
1274 
1275 	cryptodev = rte_cryptodev_pmd_get_named_dev(name);
1276 	if (cryptodev == NULL)
1277 		return -ENODEV;
1278 
1279 	return rte_cryptodev_pmd_destroy(cryptodev);
1280 }
1281 
1282 /**
1283  * Basic driver handlers for use in the constructor.
1284  */
1285 static struct rte_vdev_driver cryptodev_mrvl_pmd_drv = {
1286 	.probe = cryptodev_mrvl_crypto_init,
1287 	.remove = cryptodev_mrvl_crypto_uninit
1288 };
1289 
1290 static struct cryptodev_driver mrvl_crypto_drv;
1291 
1292 /* Register the driver in constructor. */
1293 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_MRVL_PMD, cryptodev_mrvl_pmd_drv);
1294 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_MRVL_PMD,
1295 	"max_nb_queue_pairs=<int> "
1296 	"max_nb_sessions=<int> "
1297 	"socket_id=<int>");
1298 RTE_PMD_REGISTER_CRYPTO_DRIVER(mrvl_crypto_drv, cryptodev_mrvl_pmd_drv.driver,
1299 		cryptodev_driver_id);
1300 RTE_LOG_REGISTER_DEFAULT(mrvl_logtype_driver, NOTICE);
1301