net/mlx5: fix event handler uninstall
[dpdk.git] / drivers / crypto / mvsam / rte_mrvl_pmd.c
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 <rte_cryptodev_pmd.h>
11 #include <rte_bus_vdev.h>
12 #include <rte_malloc.h>
13 #include <rte_cpuflags.h>
14 #include <rte_kvargs.h>
15 #include <rte_mvep_common.h>
16
17 #include "rte_mrvl_pmd_private.h"
18
19 #define MRVL_PMD_MAX_NB_SESS_ARG                ("max_nb_sessions")
20 #define MRVL_PMD_DEFAULT_MAX_NB_SESSIONS        2048
21
22 static uint8_t cryptodev_driver_id;
23
24 struct mrvl_pmd_init_params {
25         struct rte_cryptodev_pmd_init_params common;
26         uint32_t max_nb_sessions;
27 };
28
29 const char *mrvl_pmd_valid_params[] = {
30         RTE_CRYPTODEV_PMD_NAME_ARG,
31         RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
32         RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
33         MRVL_PMD_MAX_NB_SESS_ARG
34 };
35
36 /**
37  * Flag if particular crypto algorithm is supported by PMD/MUSDK.
38  *
39  * The idea is to have Not Supported value as default (0).
40  * This way we need only to define proper map sizes,
41  * non-initialized entries will be by default not supported.
42  */
43 enum algo_supported {
44         ALGO_NOT_SUPPORTED = 0,
45         ALGO_SUPPORTED = 1,
46 };
47
48 /** Map elements for cipher mapping.*/
49 struct cipher_params_mapping {
50         enum algo_supported  supported;   /**< On/Off switch */
51         enum sam_cipher_alg  cipher_alg;  /**< Cipher algorithm */
52         enum sam_cipher_mode cipher_mode; /**< Cipher mode */
53         unsigned int max_key_len;         /**< Maximum key length (in bytes)*/
54 }
55 /* We want to squeeze in multiple maps into the cache line. */
56 __rte_aligned(32);
57
58 /** Map elements for auth mapping.*/
59 struct auth_params_mapping {
60         enum algo_supported supported;  /**< On/off switch */
61         enum sam_auth_alg   auth_alg;   /**< Auth algorithm */
62 }
63 /* We want to squeeze in multiple maps into the cache line. */
64 __rte_aligned(32);
65
66 /**
67  * Map of supported cipher algorithms.
68  */
69 static const
70 struct cipher_params_mapping cipher_map[RTE_CRYPTO_CIPHER_LIST_END] = {
71         [RTE_CRYPTO_CIPHER_NULL] = {
72                 .supported = ALGO_SUPPORTED,
73                 .cipher_alg = SAM_CIPHER_NONE },
74         [RTE_CRYPTO_CIPHER_3DES_CBC] = {
75                 .supported = ALGO_SUPPORTED,
76                 .cipher_alg = SAM_CIPHER_3DES,
77                 .cipher_mode = SAM_CIPHER_CBC,
78                 .max_key_len = BITS2BYTES(192) },
79         [RTE_CRYPTO_CIPHER_3DES_CTR] = {
80                 .supported = ALGO_SUPPORTED,
81                 .cipher_alg = SAM_CIPHER_3DES,
82                 .cipher_mode = SAM_CIPHER_CTR,
83                 .max_key_len = BITS2BYTES(192) },
84         [RTE_CRYPTO_CIPHER_3DES_ECB] = {
85                 .supported = ALGO_SUPPORTED,
86                 .cipher_alg = SAM_CIPHER_3DES,
87                 .cipher_mode = SAM_CIPHER_ECB,
88                 .max_key_len = BITS2BYTES(192) },
89         [RTE_CRYPTO_CIPHER_AES_CBC] = {
90                 .supported = ALGO_SUPPORTED,
91                 .cipher_alg = SAM_CIPHER_AES,
92                 .cipher_mode = SAM_CIPHER_CBC,
93                 .max_key_len = BITS2BYTES(256) },
94         [RTE_CRYPTO_CIPHER_AES_CTR] = {
95                 .supported = ALGO_SUPPORTED,
96                 .cipher_alg = SAM_CIPHER_AES,
97                 .cipher_mode = SAM_CIPHER_CTR,
98                 .max_key_len = BITS2BYTES(256) },
99         [RTE_CRYPTO_CIPHER_AES_ECB] = {
100                 .supported = ALGO_SUPPORTED,
101                 .cipher_alg = SAM_CIPHER_AES,
102                 .cipher_mode = SAM_CIPHER_ECB,
103                 .max_key_len = BITS2BYTES(256) },
104 };
105
106 /**
107  * Map of supported auth algorithms.
108  */
109 static const
110 struct auth_params_mapping auth_map[RTE_CRYPTO_AUTH_LIST_END] = {
111         [RTE_CRYPTO_AUTH_NULL] = {
112                 .supported = ALGO_SUPPORTED,
113                 .auth_alg = SAM_AUTH_NONE },
114         [RTE_CRYPTO_AUTH_MD5_HMAC] = {
115                 .supported = ALGO_SUPPORTED,
116                 .auth_alg = SAM_AUTH_HMAC_MD5 },
117         [RTE_CRYPTO_AUTH_MD5] = {
118                 .supported = ALGO_SUPPORTED,
119                 .auth_alg = SAM_AUTH_HASH_MD5 },
120         [RTE_CRYPTO_AUTH_SHA1_HMAC] = {
121                 .supported = ALGO_SUPPORTED,
122                 .auth_alg = SAM_AUTH_HMAC_SHA1 },
123         [RTE_CRYPTO_AUTH_SHA1] = {
124                 .supported = ALGO_SUPPORTED,
125                 .auth_alg = SAM_AUTH_HASH_SHA1 },
126         [RTE_CRYPTO_AUTH_SHA224_HMAC] = {
127                 .supported = ALGO_SUPPORTED,
128                 .auth_alg = SAM_AUTH_HMAC_SHA2_224 },
129         [RTE_CRYPTO_AUTH_SHA224] = {
130                 .supported = ALGO_SUPPORTED,
131                 .auth_alg = SAM_AUTH_HASH_SHA2_224 },
132         [RTE_CRYPTO_AUTH_SHA256_HMAC] = {
133                 .supported = ALGO_SUPPORTED,
134                 .auth_alg = SAM_AUTH_HMAC_SHA2_256 },
135         [RTE_CRYPTO_AUTH_SHA256] = {
136                 .supported = ALGO_SUPPORTED,
137                 .auth_alg = SAM_AUTH_HASH_SHA2_256 },
138         [RTE_CRYPTO_AUTH_SHA384_HMAC] = {
139                 .supported = ALGO_SUPPORTED,
140                 .auth_alg = SAM_AUTH_HMAC_SHA2_384 },
141         [RTE_CRYPTO_AUTH_SHA384] = {
142                 .supported = ALGO_SUPPORTED,
143                 .auth_alg = SAM_AUTH_HASH_SHA2_384 },
144         [RTE_CRYPTO_AUTH_SHA512_HMAC] = {
145                 .supported = ALGO_SUPPORTED,
146                 .auth_alg = SAM_AUTH_HMAC_SHA2_512 },
147         [RTE_CRYPTO_AUTH_SHA512] = {
148                 .supported = ALGO_SUPPORTED,
149                 .auth_alg = SAM_AUTH_HASH_SHA2_512 },
150         [RTE_CRYPTO_AUTH_AES_GMAC] = {
151                 .supported = ALGO_SUPPORTED,
152                 .auth_alg = SAM_AUTH_AES_GMAC },
153 };
154
155 /**
156  * Map of supported aead algorithms.
157  */
158 static const
159 struct cipher_params_mapping aead_map[RTE_CRYPTO_AEAD_LIST_END] = {
160         [RTE_CRYPTO_AEAD_AES_GCM] = {
161                 .supported = ALGO_SUPPORTED,
162                 .cipher_alg = SAM_CIPHER_AES,
163                 .cipher_mode = SAM_CIPHER_GCM,
164                 .max_key_len = BITS2BYTES(256) },
165 };
166
167 /*
168  *-----------------------------------------------------------------------------
169  * Forward declarations.
170  *-----------------------------------------------------------------------------
171  */
172 static int cryptodev_mrvl_crypto_uninit(struct rte_vdev_device *vdev);
173
174 /*
175  *-----------------------------------------------------------------------------
176  * Session Preparation.
177  *-----------------------------------------------------------------------------
178  */
179
180 /**
181  * Get xform chain order.
182  *
183  * @param xform Pointer to configuration structure chain for crypto operations.
184  * @returns Order of crypto operations.
185  */
186 static enum mrvl_crypto_chain_order
187 mrvl_crypto_get_chain_order(const struct rte_crypto_sym_xform *xform)
188 {
189         /* Currently, Marvell supports max 2 operations in chain */
190         if (xform->next != NULL && xform->next->next != NULL)
191                 return MRVL_CRYPTO_CHAIN_NOT_SUPPORTED;
192
193         if (xform->next != NULL) {
194                 if ((xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
195                         (xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER))
196                         return MRVL_CRYPTO_CHAIN_AUTH_CIPHER;
197
198                 if ((xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) &&
199                         (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH))
200                         return MRVL_CRYPTO_CHAIN_CIPHER_AUTH;
201         } else {
202                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH)
203                         return MRVL_CRYPTO_CHAIN_AUTH_ONLY;
204
205                 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
206                         return MRVL_CRYPTO_CHAIN_CIPHER_ONLY;
207
208                 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
209                         return MRVL_CRYPTO_CHAIN_COMBINED;
210         }
211         return MRVL_CRYPTO_CHAIN_NOT_SUPPORTED;
212 }
213
214 /**
215  * Set session parameters for cipher part.
216  *
217  * @param sess Crypto session pointer.
218  * @param cipher_xform Pointer to configuration structure for cipher operations.
219  * @returns 0 in case of success, negative value otherwise.
220  */
221 static int
222 mrvl_crypto_set_cipher_session_parameters(struct mrvl_crypto_session *sess,
223                 const struct rte_crypto_sym_xform *cipher_xform)
224 {
225         /* Make sure we've got proper struct */
226         if (cipher_xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) {
227                 MRVL_LOG(ERR, "Wrong xform struct provided!");
228                 return -EINVAL;
229         }
230
231         /* See if map data is present and valid */
232         if ((cipher_xform->cipher.algo > RTE_DIM(cipher_map)) ||
233                 (cipher_map[cipher_xform->cipher.algo].supported
234                         != ALGO_SUPPORTED)) {
235                 MRVL_LOG(ERR, "Cipher algorithm not supported!");
236                 return -EINVAL;
237         }
238
239         sess->cipher_iv_offset = cipher_xform->cipher.iv.offset;
240
241         sess->sam_sess_params.dir =
242                 (cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT) ?
243                 SAM_DIR_ENCRYPT : SAM_DIR_DECRYPT;
244         sess->sam_sess_params.cipher_alg =
245                 cipher_map[cipher_xform->cipher.algo].cipher_alg;
246         sess->sam_sess_params.cipher_mode =
247                 cipher_map[cipher_xform->cipher.algo].cipher_mode;
248
249         /* Assume IV will be passed together with data. */
250         sess->sam_sess_params.cipher_iv = NULL;
251
252         /* Get max key length. */
253         if (cipher_xform->cipher.key.length >
254                 cipher_map[cipher_xform->cipher.algo].max_key_len) {
255                 MRVL_LOG(ERR, "Wrong key length!");
256                 return -EINVAL;
257         }
258
259         sess->sam_sess_params.cipher_key_len = cipher_xform->cipher.key.length;
260         sess->sam_sess_params.cipher_key = cipher_xform->cipher.key.data;
261
262         return 0;
263 }
264
265 /**
266  * Set session parameters for authentication part.
267  *
268  * @param sess Crypto session pointer.
269  * @param auth_xform Pointer to configuration structure for auth operations.
270  * @returns 0 in case of success, negative value otherwise.
271  */
272 static int
273 mrvl_crypto_set_auth_session_parameters(struct mrvl_crypto_session *sess,
274                 const struct rte_crypto_sym_xform *auth_xform)
275 {
276         /* Make sure we've got proper struct */
277         if (auth_xform->type != RTE_CRYPTO_SYM_XFORM_AUTH) {
278                 MRVL_LOG(ERR, "Wrong xform struct provided!");
279                 return -EINVAL;
280         }
281
282         /* See if map data is present and valid */
283         if ((auth_xform->auth.algo > RTE_DIM(auth_map)) ||
284                 (auth_map[auth_xform->auth.algo].supported != ALGO_SUPPORTED)) {
285                 MRVL_LOG(ERR, "Auth algorithm not supported!");
286                 return -EINVAL;
287         }
288
289         sess->sam_sess_params.dir =
290                 (auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE) ?
291                 SAM_DIR_ENCRYPT : SAM_DIR_DECRYPT;
292         sess->sam_sess_params.auth_alg =
293                 auth_map[auth_xform->auth.algo].auth_alg;
294         sess->sam_sess_params.u.basic.auth_icv_len =
295                 auth_xform->auth.digest_length;
296         /* auth_key must be NULL if auth algorithm does not use HMAC */
297         sess->sam_sess_params.auth_key = auth_xform->auth.key.length ?
298                                          auth_xform->auth.key.data : NULL;
299         sess->sam_sess_params.auth_key_len = auth_xform->auth.key.length;
300
301         return 0;
302 }
303
304 /**
305  * Set session parameters for aead part.
306  *
307  * @param sess Crypto session pointer.
308  * @param aead_xform Pointer to configuration structure for aead operations.
309  * @returns 0 in case of success, negative value otherwise.
310  */
311 static int
312 mrvl_crypto_set_aead_session_parameters(struct mrvl_crypto_session *sess,
313                 const struct rte_crypto_sym_xform *aead_xform)
314 {
315         /* Make sure we've got proper struct */
316         if (aead_xform->type != RTE_CRYPTO_SYM_XFORM_AEAD) {
317                 MRVL_LOG(ERR, "Wrong xform struct provided!");
318                 return -EINVAL;
319         }
320
321         /* See if map data is present and valid */
322         if ((aead_xform->aead.algo > RTE_DIM(aead_map)) ||
323                 (aead_map[aead_xform->aead.algo].supported
324                         != ALGO_SUPPORTED)) {
325                 MRVL_LOG(ERR, "AEAD algorithm not supported!");
326                 return -EINVAL;
327         }
328
329         sess->sam_sess_params.dir =
330                 (aead_xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT) ?
331                 SAM_DIR_ENCRYPT : SAM_DIR_DECRYPT;
332         sess->sam_sess_params.cipher_alg =
333                 aead_map[aead_xform->aead.algo].cipher_alg;
334         sess->sam_sess_params.cipher_mode =
335                 aead_map[aead_xform->aead.algo].cipher_mode;
336
337         /* Assume IV will be passed together with data. */
338         sess->sam_sess_params.cipher_iv = NULL;
339
340         /* Get max key length. */
341         if (aead_xform->aead.key.length >
342                 aead_map[aead_xform->aead.algo].max_key_len) {
343                 MRVL_LOG(ERR, "Wrong key length!");
344                 return -EINVAL;
345         }
346
347         sess->sam_sess_params.cipher_key = aead_xform->aead.key.data;
348         sess->sam_sess_params.cipher_key_len = aead_xform->aead.key.length;
349
350         if (sess->sam_sess_params.cipher_mode == SAM_CIPHER_GCM)
351                 sess->sam_sess_params.auth_alg = SAM_AUTH_AES_GCM;
352
353         sess->sam_sess_params.u.basic.auth_icv_len =
354                 aead_xform->aead.digest_length;
355
356         sess->sam_sess_params.u.basic.auth_aad_len =
357                 aead_xform->aead.aad_length;
358
359         return 0;
360 }
361
362 /**
363  * Parse crypto transform chain and setup session parameters.
364  *
365  * @param dev Pointer to crypto device
366  * @param sess Poiner to crypto session
367  * @param xform Pointer to configuration structure chain for crypto operations.
368  * @returns 0 in case of success, negative value otherwise.
369  */
370 int
371 mrvl_crypto_set_session_parameters(struct mrvl_crypto_session *sess,
372                 const struct rte_crypto_sym_xform *xform)
373 {
374         const struct rte_crypto_sym_xform *cipher_xform = NULL;
375         const struct rte_crypto_sym_xform *auth_xform = NULL;
376         const struct rte_crypto_sym_xform *aead_xform = NULL;
377
378         /* Filter out spurious/broken requests */
379         if (xform == NULL)
380                 return -EINVAL;
381
382         sess->chain_order = mrvl_crypto_get_chain_order(xform);
383         switch (sess->chain_order) {
384         case MRVL_CRYPTO_CHAIN_CIPHER_AUTH:
385                 cipher_xform = xform;
386                 auth_xform = xform->next;
387                 break;
388         case MRVL_CRYPTO_CHAIN_AUTH_CIPHER:
389                 auth_xform = xform;
390                 cipher_xform = xform->next;
391                 break;
392         case MRVL_CRYPTO_CHAIN_CIPHER_ONLY:
393                 cipher_xform = xform;
394                 break;
395         case MRVL_CRYPTO_CHAIN_AUTH_ONLY:
396                 auth_xform = xform;
397                 break;
398         case MRVL_CRYPTO_CHAIN_COMBINED:
399                 aead_xform = xform;
400                 break;
401         default:
402                 return -EINVAL;
403         }
404
405         if ((cipher_xform != NULL) &&
406                 (mrvl_crypto_set_cipher_session_parameters(
407                         sess, cipher_xform) < 0)) {
408                 MRVL_LOG(ERR, "Invalid/unsupported cipher parameters!");
409                 return -EINVAL;
410         }
411
412         if ((auth_xform != NULL) &&
413                 (mrvl_crypto_set_auth_session_parameters(
414                         sess, auth_xform) < 0)) {
415                 MRVL_LOG(ERR, "Invalid/unsupported auth parameters!");
416                 return -EINVAL;
417         }
418
419         if ((aead_xform != NULL) &&
420                 (mrvl_crypto_set_aead_session_parameters(
421                         sess, aead_xform) < 0)) {
422                 MRVL_LOG(ERR, "Invalid/unsupported aead parameters!");
423                 return -EINVAL;
424         }
425
426         return 0;
427 }
428
429 /*
430  *-----------------------------------------------------------------------------
431  * Process Operations
432  *-----------------------------------------------------------------------------
433  */
434
435 /**
436  * Prepare a single request.
437  *
438  * This function basically translates DPDK crypto request into one
439  * understandable by MUDSK's SAM. If this is a first request in a session,
440  * it starts the session.
441  *
442  * @param request Pointer to pre-allocated && reset request buffer [Out].
443  * @param src_bd Pointer to pre-allocated source descriptor [Out].
444  * @param dst_bd Pointer to pre-allocated destination descriptor [Out].
445  * @param op Pointer to DPDK crypto operation struct [In].
446  */
447 static inline int
448 mrvl_request_prepare(struct sam_cio_op_params *request,
449                 struct sam_buf_info *src_bd,
450                 struct sam_buf_info *dst_bd,
451                 struct rte_crypto_op *op)
452 {
453         struct mrvl_crypto_session *sess;
454         struct rte_mbuf *src_mbuf, *dst_mbuf;
455         uint16_t segments_nb;
456         uint8_t *digest;
457         int i;
458
459         if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
460                 MRVL_LOG(ERR, "MRVL CRYPTO PMD only supports session "
461                                 "oriented requests, op (%p) is sessionless!",
462                                 op);
463                 return -EINVAL;
464         }
465
466         sess = (struct mrvl_crypto_session *)get_sym_session_private_data(
467                         op->sym->session, cryptodev_driver_id);
468         if (unlikely(sess == NULL)) {
469                 MRVL_LOG(ERR, "Session was not created for this device!");
470                 return -EINVAL;
471         }
472
473         request->sa = sess->sam_sess;
474         request->cookie = op;
475
476         src_mbuf = op->sym->m_src;
477         segments_nb = src_mbuf->nb_segs;
478         /* The following conditions must be met:
479          * - Destination buffer is required when segmented source buffer
480          * - Segmented destination buffer is not supported
481          */
482         if ((segments_nb > 1) && (!op->sym->m_dst)) {
483                 MRVL_LOG(ERR, "op->sym->m_dst = NULL!");
484                 return -1;
485         }
486         /* For non SG case:
487          * If application delivered us null dst buffer, it means it expects
488          * us to deliver the result in src buffer.
489          */
490         dst_mbuf = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
491
492         if (!rte_pktmbuf_is_contiguous(dst_mbuf)) {
493                 MRVL_LOG(ERR, "Segmented destination buffer not supported!");
494                 return -1;
495         }
496
497         request->num_bufs = segments_nb;
498         for (i = 0; i < segments_nb; i++) {
499                 /* Empty source. */
500                 if (rte_pktmbuf_data_len(src_mbuf) == 0) {
501                         /* EIP does not support 0 length buffers. */
502                         MRVL_LOG(ERR, "Buffer length == 0 not supported!");
503                         return -1;
504                 }
505                 src_bd[i].vaddr = rte_pktmbuf_mtod(src_mbuf, void *);
506                 src_bd[i].paddr = rte_pktmbuf_iova(src_mbuf);
507                 src_bd[i].len = rte_pktmbuf_data_len(src_mbuf);
508
509                 src_mbuf = src_mbuf->next;
510         }
511         request->src = src_bd;
512
513         /* Empty destination. */
514         if (rte_pktmbuf_data_len(dst_mbuf) == 0) {
515                 /* Make dst buffer fit at least source data. */
516                 if (rte_pktmbuf_append(dst_mbuf,
517                         rte_pktmbuf_data_len(op->sym->m_src)) == NULL) {
518                         MRVL_LOG(ERR, "Unable to set big enough dst buffer!");
519                         return -1;
520                 }
521         }
522
523         request->dst = dst_bd;
524         dst_bd->vaddr = rte_pktmbuf_mtod(dst_mbuf, void *);
525         dst_bd->paddr = rte_pktmbuf_iova(dst_mbuf);
526
527         /*
528          * We can use all available space in dst_mbuf,
529          * not only what's used currently.
530          */
531         dst_bd->len = dst_mbuf->buf_len - rte_pktmbuf_headroom(dst_mbuf);
532
533         if (sess->chain_order == MRVL_CRYPTO_CHAIN_COMBINED) {
534                 request->cipher_len = op->sym->aead.data.length;
535                 request->cipher_offset = op->sym->aead.data.offset;
536                 request->cipher_iv = rte_crypto_op_ctod_offset(op, uint8_t *,
537                         sess->cipher_iv_offset);
538
539                 request->auth_aad = op->sym->aead.aad.data;
540                 request->auth_offset = request->cipher_offset;
541                 request->auth_len = request->cipher_len;
542         } else {
543                 request->cipher_len = op->sym->cipher.data.length;
544                 request->cipher_offset = op->sym->cipher.data.offset;
545                 request->cipher_iv = rte_crypto_op_ctod_offset(op, uint8_t *,
546                                 sess->cipher_iv_offset);
547
548                 request->auth_offset = op->sym->auth.data.offset;
549                 request->auth_len = op->sym->auth.data.length;
550         }
551
552         digest = sess->chain_order == MRVL_CRYPTO_CHAIN_COMBINED ?
553                 op->sym->aead.digest.data : op->sym->auth.digest.data;
554         if (digest == NULL) {
555                 /* No auth - no worry. */
556                 return 0;
557         }
558
559         request->auth_icv_offset = request->auth_offset + request->auth_len;
560
561         /*
562          * EIP supports only scenarios where ICV(digest buffer) is placed at
563          * auth_icv_offset.
564          */
565         if (sess->sam_sess_params.dir == SAM_DIR_ENCRYPT) {
566                 /*
567                  * This should be the most common case anyway,
568                  * EIP will overwrite DST buffer at auth_icv_offset.
569                  */
570                 if (rte_pktmbuf_mtod_offset(
571                                 dst_mbuf, uint8_t *,
572                                 request->auth_icv_offset) == digest)
573                         return 0;
574         } else {/* sess->sam_sess_params.dir == SAM_DIR_DECRYPT */
575                 /*
576                  * EIP will look for digest at auth_icv_offset
577                  * offset in SRC buffer. It must be placed in the last
578                  * segment and the offset must be set to reach digest
579                  * in the last segment
580                  */
581                 struct rte_mbuf *last_seg =  op->sym->m_src;
582                 uint32_t d_offset = request->auth_icv_offset;
583                 u32 d_size = sess->sam_sess_params.u.basic.auth_icv_len;
584                 unsigned char *d_ptr;
585
586                 /* Find the last segment and the offset for the last segment */
587                 while ((last_seg->next != NULL) &&
588                                 (d_offset >= last_seg->data_len)) {
589                         d_offset -= last_seg->data_len;
590                         last_seg = last_seg->next;
591                 }
592
593                 if (rte_pktmbuf_mtod_offset(last_seg, uint8_t *,
594                                             d_offset) == digest)
595                         return 0;
596
597                 /* copy digest to last segment */
598                 if (last_seg->buf_len >= (d_size + d_offset)) {
599                         d_ptr = (unsigned char *)last_seg->buf_addr +
600                                  d_offset;
601                         rte_memcpy(d_ptr, digest, d_size);
602                         return 0;
603                 }
604         }
605
606         /*
607          * If we landed here it means that digest pointer is
608          * at different than expected place.
609          */
610         return -1;
611 }
612
613 /*
614  *-----------------------------------------------------------------------------
615  * PMD Framework handlers
616  *-----------------------------------------------------------------------------
617  */
618
619 /**
620  * Enqueue burst.
621  *
622  * @param queue_pair Pointer to queue pair.
623  * @param ops Pointer to ops requests array.
624  * @param nb_ops Number of elements in ops requests array.
625  * @returns Number of elements consumed from ops.
626  */
627 static uint16_t
628 mrvl_crypto_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
629                 uint16_t nb_ops)
630 {
631         uint16_t iter_ops = 0;
632         uint16_t to_enq = 0;
633         uint16_t consumed = 0;
634         int ret;
635         struct sam_cio_op_params requests[nb_ops];
636         /*
637          * SAM does not store bd pointers, so on-stack scope will be enough.
638          */
639         struct mrvl_crypto_src_table src_bd[nb_ops];
640         struct sam_buf_info          dst_bd[nb_ops];
641         struct mrvl_crypto_qp *qp = (struct mrvl_crypto_qp *)queue_pair;
642
643         if (nb_ops == 0)
644                 return 0;
645
646         /* Prepare the burst. */
647         memset(&requests, 0, sizeof(requests));
648         memset(&src_bd, 0, sizeof(src_bd));
649
650         /* Iterate through */
651         for (; iter_ops < nb_ops; ++iter_ops) {
652                 /* store the op id for debug */
653                 src_bd[iter_ops].iter_ops = iter_ops;
654                 if (mrvl_request_prepare(&requests[iter_ops],
655                                         src_bd[iter_ops].src_bd,
656                                         &dst_bd[iter_ops],
657                                         ops[iter_ops]) < 0) {
658                         MRVL_LOG(ERR, "Error while preparing parameters!");
659                         qp->stats.enqueue_err_count++;
660                         ops[iter_ops]->status = RTE_CRYPTO_OP_STATUS_ERROR;
661
662                         /*
663                          * Number of handled ops is increased
664                          * (even if the result of handling is error).
665                          */
666                         ++consumed;
667                         break;
668                 }
669
670                 ops[iter_ops]->status =
671                         RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
672
673                 /* Increase the number of ops to enqueue. */
674                 ++to_enq;
675         } /* for (; iter_ops < nb_ops;... */
676
677         if (to_enq > 0) {
678                 /* Send the burst */
679                 ret = sam_cio_enq(qp->cio, requests, &to_enq);
680                 consumed += to_enq;
681                 if (ret < 0) {
682                         /*
683                          * Trust SAM that in this case returned value will be at
684                          * some point correct (now it is returned unmodified).
685                          */
686                         qp->stats.enqueue_err_count += to_enq;
687                         for (iter_ops = 0; iter_ops < to_enq; ++iter_ops)
688                                 ops[iter_ops]->status =
689                                         RTE_CRYPTO_OP_STATUS_ERROR;
690                 }
691         }
692
693         qp->stats.enqueued_count += to_enq;
694         return consumed;
695 }
696
697 /**
698  * Dequeue burst.
699  *
700  * @param queue_pair Pointer to queue pair.
701  * @param ops Pointer to ops requests array.
702  * @param nb_ops Number of elements in ops requests array.
703  * @returns Number of elements dequeued.
704  */
705 static uint16_t
706 mrvl_crypto_pmd_dequeue_burst(void *queue_pair,
707                 struct rte_crypto_op **ops,
708                 uint16_t nb_ops)
709 {
710         int ret;
711         struct mrvl_crypto_qp *qp = queue_pair;
712         struct sam_cio *cio = qp->cio;
713         struct sam_cio_op_result results[nb_ops];
714         uint16_t i;
715
716         ret = sam_cio_deq(cio, results, &nb_ops);
717         if (ret < 0) {
718                 /* Count all dequeued as error. */
719                 qp->stats.dequeue_err_count += nb_ops;
720
721                 /* But act as they were dequeued anyway*/
722                 qp->stats.dequeued_count += nb_ops;
723
724                 return 0;
725         }
726
727         /* Unpack and check results. */
728         for (i = 0; i < nb_ops; ++i) {
729                 ops[i] = results[i].cookie;
730
731                 switch (results[i].status) {
732                 case SAM_CIO_OK:
733                         ops[i]->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
734                         break;
735                 case SAM_CIO_ERR_ICV:
736                         MRVL_LOG(DEBUG, "CIO returned SAM_CIO_ERR_ICV.");
737                         ops[i]->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
738                         break;
739                 default:
740                         MRVL_LOG(DEBUG,
741                                 "CIO returned Error: %d.", results[i].status);
742                         ops[i]->status = RTE_CRYPTO_OP_STATUS_ERROR;
743                         break;
744                 }
745         }
746
747         qp->stats.dequeued_count += nb_ops;
748         return nb_ops;
749 }
750
751 /**
752  * Create a new crypto device.
753  *
754  * @param name Driver name.
755  * @param vdev Pointer to device structure.
756  * @param init_params Pointer to initialization parameters.
757  * @returns 0 in case of success, negative value otherwise.
758  */
759 static int
760 cryptodev_mrvl_crypto_create(const char *name,
761                 struct rte_vdev_device *vdev,
762                 struct mrvl_pmd_init_params *init_params)
763 {
764         struct rte_cryptodev *dev;
765         struct mrvl_crypto_private *internals;
766         struct sam_init_params  sam_params;
767         int ret = -EINVAL;
768
769         dev = rte_cryptodev_pmd_create(name, &vdev->device,
770                         &init_params->common);
771         if (dev == NULL) {
772                 MRVL_LOG(ERR, "Failed to create cryptodev vdev!");
773                 goto init_error;
774         }
775
776         dev->driver_id = cryptodev_driver_id;
777         dev->dev_ops = rte_mrvl_crypto_pmd_ops;
778
779         /* Register rx/tx burst functions for data path. */
780         dev->enqueue_burst = mrvl_crypto_pmd_enqueue_burst;
781         dev->dequeue_burst = mrvl_crypto_pmd_dequeue_burst;
782
783         dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
784                         RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
785                         RTE_CRYPTODEV_FF_HW_ACCELERATED |
786                         RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
787                         RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT;
788
789         /* Set vector instructions mode supported */
790         internals = dev->data->dev_private;
791
792         internals->max_nb_qpairs = init_params->common.max_nb_queue_pairs;
793         internals->max_nb_sessions = init_params->max_nb_sessions;
794
795         ret = rte_mvep_init(MVEP_MOD_T_SAM, NULL);
796         if (ret)
797                 goto init_error;
798
799         sam_params.max_num_sessions = internals->max_nb_sessions;
800
801         /* sam_set_debug_flags(3); */
802
803         ret = sam_init(&sam_params);
804         if (ret)
805                 goto init_error;
806
807         return 0;
808
809 init_error:
810         MRVL_LOG(ERR,
811                 "Driver %s: %s failed!", init_params->common.name, __func__);
812
813         cryptodev_mrvl_crypto_uninit(vdev);
814         return ret;
815 }
816
817 /** Parse integer from integer argument */
818 static int
819 parse_integer_arg(const char *key __rte_unused,
820                 const char *value, void *extra_args)
821 {
822         int *i = (int *) extra_args;
823
824         *i = atoi(value);
825         if (*i < 0) {
826                 MRVL_LOG(ERR, "Argument has to be positive!");
827                 return -EINVAL;
828         }
829
830         return 0;
831 }
832
833 /** Parse name */
834 static int
835 parse_name_arg(const char *key __rte_unused,
836                 const char *value, void *extra_args)
837 {
838         struct rte_cryptodev_pmd_init_params *params = extra_args;
839
840         if (strlen(value) >= RTE_CRYPTODEV_NAME_MAX_LEN - 1) {
841                 MRVL_LOG(ERR, "Invalid name %s, should be less than %u bytes!",
842                          value, RTE_CRYPTODEV_NAME_MAX_LEN - 1);
843                 return -EINVAL;
844         }
845
846         strncpy(params->name, value, RTE_CRYPTODEV_NAME_MAX_LEN);
847
848         return 0;
849 }
850
851 static int
852 mrvl_pmd_parse_input_args(struct mrvl_pmd_init_params *params,
853                          const char *input_args)
854 {
855         struct rte_kvargs *kvlist = NULL;
856         int ret = 0;
857
858         if (params == NULL)
859                 return -EINVAL;
860
861         if (input_args) {
862                 kvlist = rte_kvargs_parse(input_args,
863                                           mrvl_pmd_valid_params);
864                 if (kvlist == NULL)
865                         return -1;
866
867                 /* Common VDEV parameters */
868                 ret = rte_kvargs_process(kvlist,
869                                          RTE_CRYPTODEV_PMD_MAX_NB_QP_ARG,
870                                          &parse_integer_arg,
871                                          &params->common.max_nb_queue_pairs);
872                 if (ret < 0)
873                         goto free_kvlist;
874
875                 ret = rte_kvargs_process(kvlist,
876                                          RTE_CRYPTODEV_PMD_SOCKET_ID_ARG,
877                                          &parse_integer_arg,
878                                          &params->common.socket_id);
879                 if (ret < 0)
880                         goto free_kvlist;
881
882                 ret = rte_kvargs_process(kvlist,
883                                          RTE_CRYPTODEV_PMD_NAME_ARG,
884                                          &parse_name_arg,
885                                          &params->common);
886                 if (ret < 0)
887                         goto free_kvlist;
888
889                 ret = rte_kvargs_process(kvlist,
890                                          MRVL_PMD_MAX_NB_SESS_ARG,
891                                          &parse_integer_arg,
892                                          params);
893                 if (ret < 0)
894                         goto free_kvlist;
895
896         }
897
898 free_kvlist:
899         rte_kvargs_free(kvlist);
900         return ret;
901 }
902
903 /**
904  * Initialize the crypto device.
905  *
906  * @param vdev Pointer to device structure.
907  * @returns 0 in case of success, negative value otherwise.
908  */
909 static int
910 cryptodev_mrvl_crypto_init(struct rte_vdev_device *vdev)
911 {
912         struct mrvl_pmd_init_params init_params = {
913                 .common = {
914                         .name = "",
915                         .private_data_size =
916                                 sizeof(struct mrvl_crypto_private),
917                         .max_nb_queue_pairs =
918                                 sam_get_num_inst() * sam_get_num_cios(0),
919                         .socket_id = rte_socket_id()
920                 },
921                 .max_nb_sessions = MRVL_PMD_DEFAULT_MAX_NB_SESSIONS
922         };
923
924         const char *name, *args;
925         int ret;
926
927         name = rte_vdev_device_name(vdev);
928         if (name == NULL)
929                 return -EINVAL;
930         args = rte_vdev_device_args(vdev);
931
932         ret = mrvl_pmd_parse_input_args(&init_params, args);
933         if (ret) {
934                 MRVL_LOG(ERR, "Failed to parse initialisation arguments[%s]!",
935                          args);
936                 return -EINVAL;
937         }
938
939         return cryptodev_mrvl_crypto_create(name, vdev, &init_params);
940 }
941
942 /**
943  * Uninitialize the crypto device
944  *
945  * @param vdev Pointer to device structure.
946  * @returns 0 in case of success, negative value otherwise.
947  */
948 static int
949 cryptodev_mrvl_crypto_uninit(struct rte_vdev_device *vdev)
950 {
951         struct rte_cryptodev *cryptodev;
952         const char *name = rte_vdev_device_name(vdev);
953
954         if (name == NULL)
955                 return -EINVAL;
956
957         MRVL_LOG(INFO, "Closing Marvell crypto device %s on numa socket %u.",
958                  name, rte_socket_id());
959
960         sam_deinit();
961         rte_mvep_deinit(MVEP_MOD_T_SAM);
962
963         cryptodev = rte_cryptodev_pmd_get_named_dev(name);
964         if (cryptodev == NULL)
965                 return -ENODEV;
966
967         return rte_cryptodev_pmd_destroy(cryptodev);
968 }
969
970 /**
971  * Basic driver handlers for use in the constructor.
972  */
973 static struct rte_vdev_driver cryptodev_mrvl_pmd_drv = {
974         .probe = cryptodev_mrvl_crypto_init,
975         .remove = cryptodev_mrvl_crypto_uninit
976 };
977
978 static struct cryptodev_driver mrvl_crypto_drv;
979
980 /* Register the driver in constructor. */
981 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_MRVL_PMD, cryptodev_mrvl_pmd_drv);
982 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_MRVL_PMD,
983         "max_nb_queue_pairs=<int> "
984         "max_nb_sessions=<int> "
985         "socket_id=<int>");
986 RTE_PMD_REGISTER_CRYPTO_DRIVER(mrvl_crypto_drv, cryptodev_mrvl_pmd_drv.driver,
987                 cryptodev_driver_id);
988
989 RTE_INIT(crypto_mrvl_init_log)
990 {
991         mrvl_logtype_driver = rte_log_register("pmd.crypto.mvsam");
992 }