crypto/dpaa_sec: allocate context dynamically for SG
[dpdk.git] / drivers / crypto / dpaa_sec / dpaa_sec.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright 2016 NXP
4  *
5  */
6
7 #ifndef _DPAA_SEC_H_
8 #define _DPAA_SEC_H_
9
10 #define CRYPTODEV_NAME_DPAA_SEC_PMD     crypto_dpaa_sec
11 /**< NXP DPAA - SEC PMD device name */
12
13 #define MAX_DPAA_CORES          4
14 #define NUM_POOL_CHANNELS       4
15 #define DPAA_SEC_BURST          7
16 #define DPAA_SEC_ALG_UNSUPPORT  (-1)
17 #define TDES_CBC_IV_LEN         8
18 #define AES_CBC_IV_LEN          16
19 #define AES_CTR_IV_LEN          16
20 #define AES_GCM_IV_LEN          12
21
22 #define DPAA_IPv6_DEFAULT_VTC_FLOW      0x60000000
23
24 /* Minimum job descriptor consists of a oneword job descriptor HEADER and
25  * a pointer to the shared descriptor.
26  */
27 #define MIN_JOB_DESC_SIZE       (CAAM_CMD_SZ + CAAM_PTR_SZ)
28 /* CTX_POOL_NUM_BUFS is set as per the ipsec-secgw application */
29 #define CTX_POOL_NUM_BUFS       32000
30 #define CTX_POOL_BUF_SIZE       sizeof(struct dpaa_sec_op_ctx)
31 #define CTX_POOL_CACHE_SIZE     512
32 #define RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS 1024
33
34 #define DIR_ENC                 1
35 #define DIR_DEC                 0
36
37 enum dpaa_sec_op_type {
38         DPAA_SEC_NONE,  /*!< No Cipher operations*/
39         DPAA_SEC_CIPHER,/*!< CIPHER operations */
40         DPAA_SEC_AUTH,  /*!< Authentication Operations */
41         DPAA_SEC_AEAD,  /*!< Authenticated Encryption with associated data */
42         DPAA_SEC_IPSEC, /*!< IPSEC protocol operations*/
43         DPAA_SEC_PDCP,  /*!< PDCP protocol operations*/
44         DPAA_SEC_PKC,   /*!< Public Key Cryptographic Operations */
45         DPAA_SEC_MAX
46 };
47
48
49 #define DPAA_SEC_MAX_DESC_SIZE  64
50 /* code or cmd block to caam */
51 struct sec_cdb {
52         struct {
53                 union {
54                         uint32_t word;
55                         struct {
56 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
57                                 uint16_t rsvd63_48;
58                                 unsigned int rsvd47_39:9;
59                                 unsigned int idlen:7;
60 #else
61                                 unsigned int idlen:7;
62                                 unsigned int rsvd47_39:9;
63                                 uint16_t rsvd63_48;
64 #endif
65                         } field;
66                 } __packed hi;
67
68                 union {
69                         uint32_t word;
70                         struct {
71 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
72                                 unsigned int rsvd31_30:2;
73                                 unsigned int fsgt:1;
74                                 unsigned int lng:1;
75                                 unsigned int offset:2;
76                                 unsigned int abs:1;
77                                 unsigned int add_buf:1;
78                                 uint8_t pool_id;
79                                 uint16_t pool_buffer_size;
80 #else
81                                 uint16_t pool_buffer_size;
82                                 uint8_t pool_id;
83                                 unsigned int add_buf:1;
84                                 unsigned int abs:1;
85                                 unsigned int offset:2;
86                                 unsigned int lng:1;
87                                 unsigned int fsgt:1;
88                                 unsigned int rsvd31_30:2;
89 #endif
90                         } field;
91                 } __packed lo;
92         } __packed sh_hdr;
93
94         uint32_t sh_desc[DPAA_SEC_MAX_DESC_SIZE];
95 };
96
97 /*!
98  * The structure is to be filled by user as a part of
99  * dpaa_sec_proto_ctxt for PDCP Protocol
100  */
101 struct sec_pdcp_ctxt {
102         enum rte_security_pdcp_domain domain; /*!< Data/Control mode*/
103         int8_t bearer;  /*!< PDCP bearer ID */
104         int8_t pkt_dir;/*!< PDCP Frame Direction 0:UL 1:DL*/
105         int8_t hfn_ovd;/*!< Overwrite HFN per packet*/
106         uint8_t sn_size;        /*!< Sequence number size, 5/7/12/15/18 */
107         uint32_t hfn_ovd_offset;/*!< offset from rte_crypto_op at which
108                                  * per packet hfn is stored
109                                  */
110         uint32_t hfn;   /*!< Hyper Frame Number */
111         uint32_t hfn_threshold; /*!< HFN Threashold for key renegotiation */
112 };
113
114 typedef struct dpaa_sec_session_entry {
115         uint8_t dir;         /*!< Operation Direction */
116         enum rte_crypto_cipher_algorithm cipher_alg; /*!< Cipher Algorithm*/
117         enum rte_crypto_auth_algorithm auth_alg; /*!< Authentication Algorithm*/
118         enum rte_crypto_aead_algorithm aead_alg; /*!< AEAD Algorithm*/
119         enum rte_security_session_protocol proto_alg; /*!< Security Algorithm*/
120         union {
121                 struct {
122                         uint8_t *data;  /**< pointer to key data */
123                         size_t length;  /**< key length in bytes */
124                 } aead_key;
125                 struct {
126                         struct {
127                                 uint8_t *data;  /**< pointer to key data */
128                                 size_t length;  /**< key length in bytes */
129                         } cipher_key;
130                         struct {
131                                 uint8_t *data;  /**< pointer to key data */
132                                 size_t length;  /**< key length in bytes */
133                         } auth_key;
134                 };
135         };
136         union {
137                 struct {
138                         struct {
139                                 uint16_t length;
140                                 uint16_t offset;
141                         } iv;   /**< Initialisation vector parameters */
142                         uint16_t auth_only_len;
143                                         /*!< Length of data for Auth only */
144                         uint32_t digest_length;
145                         struct ipsec_decap_pdb decap_pdb;
146                         struct ipsec_encap_pdb encap_pdb;
147                         union {
148                                 struct ip ip4_hdr;
149                                 struct rte_ipv6_hdr ip6_hdr;
150                         };
151                 };
152                 struct sec_pdcp_ctxt pdcp;
153         };
154         struct dpaa_sec_qp *qp[MAX_DPAA_CORES];
155         struct qman_fq *inq[MAX_DPAA_CORES];
156         struct sec_cdb cdb;     /**< cmd block associated with qp */
157         struct rte_mempool *ctx_pool; /* session mempool for dpaa_sec_op_ctx */
158 } dpaa_sec_session;
159
160 struct dpaa_sec_qp {
161         struct dpaa_sec_dev_private *internals;
162         struct qman_fq outq;
163         int rx_pkts;
164         int rx_errs;
165         int tx_pkts;
166         int tx_errs;
167 };
168
169 #define RTE_DPAA_MAX_NB_SEC_QPS 2
170 #define RTE_DPAA_MAX_RX_QUEUE (MAX_DPAA_CORES * RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS)
171 #define DPAA_MAX_DEQUEUE_NUM_FRAMES 63
172
173 /* internal sec queue interface */
174 struct dpaa_sec_dev_private {
175         void *sec_hw;
176         struct rte_mempool *ctx_pool; /* per dev mempool for dpaa_sec_op_ctx */
177         struct dpaa_sec_qp qps[RTE_DPAA_MAX_NB_SEC_QPS]; /* i/o queue for sec */
178         struct qman_fq inq[RTE_DPAA_MAX_RX_QUEUE];
179         unsigned char inq_attach[RTE_DPAA_MAX_RX_QUEUE];
180         unsigned int max_nb_queue_pairs;
181         unsigned int max_nb_sessions;
182         rte_spinlock_t lock;
183 };
184
185 #define MAX_SG_ENTRIES          16
186 #define MAX_JOB_SG_ENTRIES      36
187
188 struct dpaa_sec_job {
189         /* sg[0] output, sg[1] input, others are possible sub frames */
190         struct qm_sg_entry sg[MAX_JOB_SG_ENTRIES];
191 };
192
193 #define DPAA_MAX_NB_MAX_DIGEST  32
194 struct dpaa_sec_op_ctx {
195         struct dpaa_sec_job job;
196         struct rte_crypto_op *op;
197         struct rte_mempool *ctx_pool; /* mempool pointer for dpaa_sec_op_ctx */
198         uint32_t fd_status;
199         int64_t vtop_offset;
200         uint8_t digest[DPAA_MAX_NB_MAX_DIGEST];
201 };
202
203 static const struct rte_cryptodev_capabilities dpaa_sec_capabilities[] = {
204         {       /* MD5 HMAC */
205                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
206                 {.sym = {
207                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
208                         {.auth = {
209                                 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
210                                 .block_size = 64,
211                                 .key_size = {
212                                         .min = 1,
213                                         .max = 64,
214                                         .increment = 1
215                                 },
216                                 .digest_size = {
217                                         .min = 1,
218                                         .max = 16,
219                                         .increment = 1
220                                 },
221                                 .iv_size = { 0 }
222                         }, }
223                 }, }
224         },
225         {       /* SHA1 HMAC */
226                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
227                 {.sym = {
228                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
229                         {.auth = {
230                                 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
231                                 .block_size = 64,
232                                 .key_size = {
233                                         .min = 1,
234                                         .max = 64,
235                                         .increment = 1
236                                 },
237                                 .digest_size = {
238                                         .min = 1,
239                                         .max = 20,
240                                         .increment = 1
241                                 },
242                                 .iv_size = { 0 }
243                         }, }
244                 }, }
245         },
246         {       /* SHA224 HMAC */
247                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
248                 {.sym = {
249                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
250                         {.auth = {
251                                 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
252                                 .block_size = 64,
253                                 .key_size = {
254                                         .min = 1,
255                                         .max = 64,
256                                         .increment = 1
257                                 },
258                                 .digest_size = {
259                                         .min = 1,
260                                         .max = 28,
261                                         .increment = 1
262                                 },
263                                 .iv_size = { 0 }
264                         }, }
265                 }, }
266         },
267         {       /* SHA256 HMAC */
268                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
269                 {.sym = {
270                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
271                         {.auth = {
272                                 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
273                                 .block_size = 64,
274                                 .key_size = {
275                                         .min = 1,
276                                         .max = 64,
277                                         .increment = 1
278                                 },
279                                 .digest_size = {
280                                         .min = 1,
281                                         .max = 32,
282                                         .increment = 1
283                                 },
284                                 .iv_size = { 0 }
285                         }, }
286                 }, }
287         },
288         {       /* SHA384 HMAC */
289                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
290                 {.sym = {
291                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
292                         {.auth = {
293                                 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
294                                 .block_size = 128,
295                                 .key_size = {
296                                         .min = 1,
297                                         .max = 128,
298                                         .increment = 1
299                                 },
300                                 .digest_size = {
301                                         .min = 1,
302                                         .max = 48,
303                                         .increment = 1
304                                 },
305                                 .iv_size = { 0 }
306                         }, }
307                 }, }
308         },
309         {       /* SHA512 HMAC */
310                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
311                 {.sym = {
312                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
313                         {.auth = {
314                                 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
315                                 .block_size = 128,
316                                 .key_size = {
317                                         .min = 1,
318                                         .max = 128,
319                                         .increment = 1
320                                 },
321                                 .digest_size = {
322                                         .min = 1,
323                                         .max = 64,
324                                         .increment = 1
325                                 },
326                                 .iv_size = { 0 }
327                         }, }
328                 }, }
329         },
330         {       /* AES GCM */
331                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
332                 {.sym = {
333                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
334                         {.aead = {
335                                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
336                                 .block_size = 16,
337                                 .key_size = {
338                                         .min = 16,
339                                         .max = 32,
340                                         .increment = 8
341                                 },
342                                 .digest_size = {
343                                         .min = 8,
344                                         .max = 16,
345                                         .increment = 4
346                                 },
347                                 .aad_size = {
348                                         .min = 0,
349                                         .max = 240,
350                                         .increment = 1
351                                 },
352                                 .iv_size = {
353                                         .min = 12,
354                                         .max = 12,
355                                         .increment = 0
356                                 },
357                         }, }
358                 }, }
359         },
360         {       /* AES CBC */
361                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
362                 {.sym = {
363                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
364                         {.cipher = {
365                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
366                                 .block_size = 16,
367                                 .key_size = {
368                                         .min = 16,
369                                         .max = 32,
370                                         .increment = 8
371                                 },
372                                 .iv_size = {
373                                         .min = 16,
374                                         .max = 16,
375                                         .increment = 0
376                                 }
377                         }, }
378                 }, }
379         },
380         {       /* AES CTR */
381                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
382                 {.sym = {
383                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
384                         {.cipher = {
385                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
386                                 .block_size = 16,
387                                 .key_size = {
388                                         .min = 16,
389                                         .max = 32,
390                                         .increment = 8
391                                 },
392                                 .iv_size = {
393                                         .min = 16,
394                                         .max = 16,
395                                         .increment = 0
396                                 },
397                         }, }
398                 }, }
399         },
400         {       /* 3DES CBC */
401                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
402                 {.sym = {
403                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
404                         {.cipher = {
405                                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
406                                 .block_size = 8,
407                                 .key_size = {
408                                         .min = 16,
409                                         .max = 24,
410                                         .increment = 8
411                                 },
412                                 .iv_size = {
413                                         .min = 8,
414                                         .max = 8,
415                                         .increment = 0
416                                 }
417                         }, }
418                 }, }
419         },
420
421         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
422 };
423
424 static const struct rte_cryptodev_capabilities dpaa_pdcp_capabilities[] = {
425         {       /* SNOW 3G (UIA2) */
426                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
427                 {.sym = {
428                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
429                         {.auth = {
430                                 .algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2,
431                                 .block_size = 16,
432                                 .key_size = {
433                                         .min = 16,
434                                         .max = 16,
435                                         .increment = 0
436                                 },
437                                 .digest_size = {
438                                         .min = 4,
439                                         .max = 4,
440                                         .increment = 0
441                                 },
442                                 .iv_size = {
443                                         .min = 16,
444                                         .max = 16,
445                                         .increment = 0
446                                 }
447                         }, }
448                 }, }
449         },
450         {       /* SNOW 3G (UEA2) */
451                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
452                 {.sym = {
453                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
454                         {.cipher = {
455                                 .algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
456                                 .block_size = 16,
457                                 .key_size = {
458                                         .min = 16,
459                                         .max = 16,
460                                         .increment = 0
461                                 },
462                                 .iv_size = {
463                                         .min = 16,
464                                         .max = 16,
465                                         .increment = 0
466                                 }
467                         }, }
468                 }, }
469         },
470         {       /* AES CTR */
471                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
472                 {.sym = {
473                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
474                         {.cipher = {
475                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
476                                 .block_size = 16,
477                                 .key_size = {
478                                         .min = 16,
479                                         .max = 32,
480                                         .increment = 8
481                                 },
482                                 .iv_size = {
483                                         .min = 16,
484                                         .max = 16,
485                                         .increment = 0
486                                 }
487                         }, }
488                 }, }
489         },
490         {       /* NULL (AUTH) */
491                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
492                 {.sym = {
493                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
494                         {.auth = {
495                                 .algo = RTE_CRYPTO_AUTH_NULL,
496                                 .block_size = 1,
497                                 .key_size = {
498                                         .min = 0,
499                                         .max = 0,
500                                         .increment = 0
501                                 },
502                                 .digest_size = {
503                                         .min = 0,
504                                         .max = 0,
505                                         .increment = 0
506                                 },
507                                 .iv_size = { 0 }
508                         }, },
509                 }, },
510         },
511         {       /* NULL (CIPHER) */
512                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
513                 {.sym = {
514                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
515                         {.cipher = {
516                                 .algo = RTE_CRYPTO_CIPHER_NULL,
517                                 .block_size = 1,
518                                 .key_size = {
519                                         .min = 0,
520                                         .max = 0,
521                                         .increment = 0
522                                 },
523                                 .iv_size = {
524                                         .min = 0,
525                                         .max = 0,
526                                         .increment = 0
527                                 }
528                         }, },
529                 }, }
530         },
531         {       /* ZUC (EEA3) */
532                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
533                 {.sym = {
534                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
535                         {.cipher = {
536                                 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
537                                 .block_size = 16,
538                                 .key_size = {
539                                         .min = 16,
540                                         .max = 16,
541                                         .increment = 0
542                                 },
543                                 .iv_size = {
544                                         .min = 16,
545                                         .max = 16,
546                                         .increment = 0
547                                 }
548                         }, }
549                 }, }
550         },
551         {       /* ZUC (EIA3) */
552                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
553                 {.sym = {
554                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
555                         {.auth = {
556                                 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
557                                 .block_size = 16,
558                                 .key_size = {
559                                         .min = 16,
560                                         .max = 16,
561                                         .increment = 0
562                                 },
563                                 .digest_size = {
564                                         .min = 4,
565                                         .max = 4,
566                                         .increment = 0
567                                 },
568                                 .iv_size = {
569                                         .min = 16,
570                                         .max = 16,
571                                         .increment = 0
572                                 }
573                         }, }
574                 }, }
575         },
576
577         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
578 };
579
580 static const struct rte_security_capability dpaa_sec_security_cap[] = {
581         { /* IPsec Lookaside Protocol offload ESP Transport Egress */
582                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
583                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
584                 .ipsec = {
585                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
586                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
587                         .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
588                         .options = { 0 }
589                 },
590                 .crypto_capabilities = dpaa_sec_capabilities
591         },
592         { /* IPsec Lookaside Protocol offload ESP Tunnel Ingress */
593                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
594                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
595                 .ipsec = {
596                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
597                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
598                         .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
599                         .options = { 0 }
600                 },
601                 .crypto_capabilities = dpaa_sec_capabilities
602         },
603         { /* PDCP Lookaside Protocol offload Data */
604                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
605                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
606                 .pdcp = {
607                         .domain = RTE_SECURITY_PDCP_MODE_DATA,
608                 },
609                 .crypto_capabilities = dpaa_pdcp_capabilities
610         },
611         { /* PDCP Lookaside Protocol offload Control */
612                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
613                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
614                 .pdcp = {
615                         .domain = RTE_SECURITY_PDCP_MODE_CONTROL,
616                 },
617                 .crypto_capabilities = dpaa_pdcp_capabilities
618         },
619         {
620                 .action = RTE_SECURITY_ACTION_TYPE_NONE
621         }
622 };
623
624 /**
625  * Checksum
626  *
627  * @param buffer calculate chksum for buffer
628  * @param len    buffer length
629  *
630  * @return checksum value in host cpu order
631  */
632 static inline uint16_t
633 calc_chksum(void *buffer, int len)
634 {
635         uint16_t *buf = (uint16_t *)buffer;
636         uint32_t sum = 0;
637         uint16_t result;
638
639         for (sum = 0; len > 1; len -= 2)
640                 sum += *buf++;
641
642         if (len == 1)
643                 sum += *(unsigned char *)buf;
644
645         sum = (sum >> 16) + (sum & 0xFFFF);
646         sum += (sum >> 16);
647         result = ~sum;
648
649         return  result;
650 }
651
652 #endif /* _DPAA_SEC_H_ */