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