crypto/dpaa2_sec: support IPv6 tunnel for protocol offload
[dpdk.git] / drivers / crypto / dpaa2_sec / dpaa2_sec_priv.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  *   Copyright (c) 2016 Freescale Semiconductor, Inc. All rights reserved.
4  *   Copyright 2016 NXP
5  *
6  */
7
8 #ifndef _RTE_DPAA2_SEC_PMD_PRIVATE_H_
9 #define _RTE_DPAA2_SEC_PMD_PRIVATE_H_
10
11 #include <rte_security_driver.h>
12
13 #define CRYPTODEV_NAME_DPAA2_SEC_PMD    crypto_dpaa2_sec
14 /**< NXP DPAA2 - SEC PMD device name */
15
16 #define MAX_QUEUES              64
17 #define MAX_DESC_SIZE           64
18 /** private data structure for each DPAA2_SEC device */
19 struct dpaa2_sec_dev_private {
20         void *mc_portal; /**< MC Portal for configuring this device */
21         void *hw; /**< Hardware handle for this device.Used by NADK framework */
22         struct rte_mempool *fle_pool; /* per device memory pool for FLE */
23         int32_t hw_id; /**< An unique ID of this device instance */
24         int32_t vfio_fd; /**< File descriptor received via VFIO */
25         uint16_t token; /**< Token required by DPxxx objects */
26         unsigned int max_nb_queue_pairs;
27         /**< Max number of queue pairs supported by device */
28 };
29
30 struct dpaa2_sec_qp {
31         struct dpaa2_queue rx_vq;
32         struct dpaa2_queue tx_vq;
33 };
34
35 enum shr_desc_type {
36         DESC_UPDATE,
37         DESC_FINAL,
38         DESC_INITFINAL,
39 };
40
41 #define DIR_ENC                 1
42 #define DIR_DEC                 0
43
44 #define DPAA2_IPv6_DEFAULT_VTC_FLOW     0x60000000
45
46 #define DPAA2_SET_FLC_EWS(flc)  (flc->word1_bits23_16 |= 0x1)
47 #define DPAA2_SET_FLC_RSC(flc)  (flc->word1_bits31_24 |= 0x1)
48 #define DPAA2_SET_FLC_REUSE_BS(flc) (flc->mode_bits |= 0x8000)
49 #define DPAA2_SET_FLC_REUSE_FF(flc) (flc->mode_bits |= 0x2000)
50
51 /* SEC Flow Context Descriptor */
52 struct sec_flow_context {
53         /* word 0 */
54         uint16_t word0_sdid;            /* 11-0  SDID */
55         uint16_t word0_res;             /* 31-12 reserved */
56
57         /* word 1 */
58         uint8_t word1_sdl;              /* 5-0 SDL */
59                                         /* 7-6 reserved */
60
61         uint8_t word1_bits_15_8;        /* 11-8 CRID */
62                                         /* 14-12 reserved */
63                                         /* 15 CRJD */
64
65         uint8_t word1_bits23_16;        /* 16  EWS */
66                                         /* 17 DAC */
67                                         /* 18,19,20 ? */
68                                         /* 23-21 reserved */
69
70         uint8_t word1_bits31_24;        /* 24 RSC */
71                                         /* 25 RBMT */
72                                         /* 31-26 reserved */
73
74         /* word 2  RFLC[31-0] */
75         uint32_t word2_rflc_31_0;
76
77         /* word 3  RFLC[63-32] */
78         uint32_t word3_rflc_63_32;
79
80         /* word 4 */
81         uint16_t word4_iicid;           /* 15-0  IICID */
82         uint16_t word4_oicid;           /* 31-16 OICID */
83
84         /* word 5 */
85         uint32_t word5_ofqid:24;                /* 23-0 OFQID */
86         uint32_t word5_31_24:8;
87                                         /* 24 OSC */
88                                         /* 25 OBMT */
89                                         /* 29-26 reserved */
90                                         /* 31-30 ICR */
91
92         /* word 6 */
93         uint32_t word6_oflc_31_0;
94
95         /* word 7 */
96         uint32_t word7_oflc_63_32;
97
98         /* Word 8-15 storage profiles */
99         uint16_t dl;                    /**<  DataLength(correction) */
100         uint16_t reserved;              /**< reserved */
101         uint16_t dhr;                   /**< DataHeadRoom(correction) */
102         uint16_t mode_bits;             /**< mode bits */
103         uint16_t bpv0;                  /**< buffer pool0 valid */
104         uint16_t bpid0;                 /**< Bypass Memory Translation */
105         uint16_t bpv1;                  /**< buffer pool1 valid */
106         uint16_t bpid1;                 /**< Bypass Memory Translation */
107         uint64_t word_12_15[2];         /**< word 12-15 are reserved */
108 };
109
110 struct sec_flc_desc {
111         struct sec_flow_context flc;
112         uint32_t desc[MAX_DESC_SIZE];
113 };
114
115 struct ctxt_priv {
116         struct rte_mempool *fle_pool; /* per device memory pool for FLE */
117         struct sec_flc_desc flc_desc[0];
118 };
119
120 enum dpaa2_sec_op_type {
121         DPAA2_SEC_NONE,  /*!< No Cipher operations*/
122         DPAA2_SEC_CIPHER,/*!< CIPHER operations */
123         DPAA2_SEC_AUTH,  /*!< Authentication Operations */
124         DPAA2_SEC_AEAD,  /*!< AEAD (AES-GCM/CCM) type operations */
125         DPAA2_SEC_CIPHER_HASH,  /*!< Authenticated Encryption with
126                                  * associated data
127                                  */
128         DPAA2_SEC_HASH_CIPHER,  /*!< Encryption with Authenticated
129                                  * associated data
130                                  */
131         DPAA2_SEC_IPSEC, /*!< IPSEC protocol operations*/
132         DPAA2_SEC_PDCP,  /*!< PDCP protocol operations*/
133         DPAA2_SEC_PKC,   /*!< Public Key Cryptographic Operations */
134         DPAA2_SEC_MAX
135 };
136
137 struct dpaa2_sec_aead_ctxt {
138         uint16_t auth_only_len; /*!< Length of data for Auth only */
139         uint8_t auth_cipher_text;       /**< Authenticate/cipher ordering */
140 };
141
142 /*
143  * The structure is to be filled by user for PDCP Protocol
144  */
145 struct dpaa2_pdcp_ctxt {
146         enum rte_security_pdcp_domain domain; /*!< Data/Control mode*/
147         int8_t bearer;  /*!< PDCP bearer ID */
148         int8_t pkt_dir;/*!< PDCP Frame Direction 0:UL 1:DL*/
149         int8_t hfn_ovd;/*!< Overwrite HFN per packet*/
150         uint32_t hfn;   /*!< Hyper Frame Number */
151         uint32_t hfn_threshold; /*!< HFN Threashold for key renegotiation */
152         uint8_t sn_size;        /*!< Sequence number size, 7/12/15 */
153 };
154
155 typedef struct dpaa2_sec_session_entry {
156         void *ctxt;
157         uint8_t ctxt_type;
158         uint8_t dir;         /*!< Operation Direction */
159         enum rte_crypto_cipher_algorithm cipher_alg; /*!< Cipher Algorithm*/
160         enum rte_crypto_auth_algorithm auth_alg; /*!< Authentication Algorithm*/
161         enum rte_crypto_aead_algorithm aead_alg; /*!< AEAD Algorithm*/
162         union {
163                 struct {
164                         uint8_t *data;  /**< pointer to key data */
165                         size_t length;  /**< key length in bytes */
166                 } aead_key;
167                 struct {
168                         struct {
169                                 uint8_t *data;  /**< pointer to key data */
170                                 size_t length;  /**< key length in bytes */
171                         } cipher_key;
172                         struct {
173                                 uint8_t *data;  /**< pointer to key data */
174                                 size_t length;  /**< key length in bytes */
175                         } auth_key;
176                 };
177         };
178         union {
179                 struct {
180                         struct {
181                                 uint16_t length; /**< IV length in bytes */
182                                 uint16_t offset; /**< IV offset in bytes */
183                         } iv;
184                         uint16_t digest_length;
185                         uint8_t status;
186                         union {
187                                 struct dpaa2_sec_aead_ctxt aead_ctxt;
188                         } ext_params;
189                 };
190                 struct dpaa2_pdcp_ctxt pdcp;
191         };
192 } dpaa2_sec_session;
193
194 static const struct rte_cryptodev_capabilities dpaa2_sec_capabilities[] = {
195         {       /* MD5 HMAC */
196                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
197                 {.sym = {
198                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
199                         {.auth = {
200                                 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
201                                 .block_size = 64,
202                                 .key_size = {
203                                         .min = 1,
204                                         .max = 64,
205                                         .increment = 1
206                                 },
207                                 .digest_size = {
208                                         .min = 1,
209                                         .max = 16,
210                                         .increment = 1
211                                 },
212                                 .iv_size = { 0 }
213                         }, }
214                 }, }
215         },
216         {       /* SHA1 HMAC */
217                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
218                 {.sym = {
219                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
220                         {.auth = {
221                                 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
222                                 .block_size = 64,
223                                 .key_size = {
224                                         .min = 1,
225                                         .max = 64,
226                                         .increment = 1
227                                 },
228                                 .digest_size = {
229                                         .min = 1,
230                                         .max = 20,
231                                         .increment = 1
232                                 },
233                                 .iv_size = { 0 }
234                         }, }
235                 }, }
236         },
237         {       /* SHA224 HMAC */
238                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
239                 {.sym = {
240                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
241                         {.auth = {
242                                 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
243                                 .block_size = 64,
244                                 .key_size = {
245                                         .min = 1,
246                                         .max = 64,
247                                         .increment = 1
248                                 },
249                                 .digest_size = {
250                                         .min = 1,
251                                         .max = 28,
252                                         .increment = 1
253                                 },
254                                 .iv_size = { 0 }
255                         }, }
256                 }, }
257         },
258         {       /* SHA256 HMAC */
259                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
260                 {.sym = {
261                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
262                         {.auth = {
263                                 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
264                                 .block_size = 64,
265                                 .key_size = {
266                                         .min = 1,
267                                         .max = 64,
268                                         .increment = 1
269                                 },
270                                 .digest_size = {
271                                         .min = 1,
272                                         .max = 32,
273                                         .increment = 1
274                                 },
275                                 .iv_size = { 0 }
276                                 }, }
277                         }, }
278                 },
279         {       /* SHA384 HMAC */
280                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
281                 {.sym = {
282                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
283                         {.auth = {
284                                 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
285                                 .block_size = 128,
286                                 .key_size = {
287                                         .min = 1,
288                                         .max = 128,
289                                         .increment = 1
290                                 },
291                                 .digest_size = {
292                                         .min = 1,
293                                         .max = 48,
294                                         .increment = 1
295                                 },
296                                 .iv_size = { 0 }
297                         }, }
298                 }, }
299         },
300         {       /* SHA512 HMAC */
301                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
302                 {.sym = {
303                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
304                         {.auth = {
305                                 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
306                                 .block_size = 128,
307                                 .key_size = {
308                                         .min = 1,
309                                         .max = 128,
310                                         .increment = 1
311                                 },
312                                 .digest_size = {
313                                         .min = 1,
314                                         .max = 64,
315                                         .increment = 1
316                                 },
317                                 .iv_size = { 0 }
318                         }, }
319                 }, }
320         },
321         {       /* AES GCM */
322                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
323                 {.sym = {
324                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
325                         {.aead = {
326                                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
327                                 .block_size = 16,
328                                 .key_size = {
329                                         .min = 16,
330                                         .max = 32,
331                                         .increment = 8
332                                 },
333                                 .digest_size = {
334                                         .min = 8,
335                                         .max = 16,
336                                         .increment = 4
337                                 },
338                                 .aad_size = {
339                                         .min = 0,
340                                         .max = 240,
341                                         .increment = 1
342                                 },
343                                 .iv_size = {
344                                         .min = 12,
345                                         .max = 12,
346                                         .increment = 0
347                                 },
348                         }, }
349                 }, }
350         },
351         {       /* AES CBC */
352                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
353                 {.sym = {
354                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
355                         {.cipher = {
356                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
357                                 .block_size = 16,
358                                 .key_size = {
359                                         .min = 16,
360                                         .max = 32,
361                                         .increment = 8
362                                 },
363                                 .iv_size = {
364                                         .min = 16,
365                                         .max = 16,
366                                         .increment = 0
367                                 }
368                         }, }
369                 }, }
370         },
371         {       /* AES CTR */
372                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
373                 {.sym = {
374                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
375                         {.cipher = {
376                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
377                                 .block_size = 16,
378                                 .key_size = {
379                                         .min = 16,
380                                         .max = 32,
381                                         .increment = 8
382                                 },
383                                 .iv_size = {
384                                         .min = 16,
385                                         .max = 16,
386                                         .increment = 0
387                                 },
388                         }, }
389                 }, }
390         },
391         {       /* 3DES CBC */
392                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
393                 {.sym = {
394                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
395                         {.cipher = {
396                                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
397                                 .block_size = 8,
398                                 .key_size = {
399                                         .min = 16,
400                                         .max = 24,
401                                         .increment = 8
402                                 },
403                                 .iv_size = {
404                                         .min = 8,
405                                         .max = 8,
406                                         .increment = 0
407                                 }
408                         }, }
409                 }, }
410         },
411
412         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
413 };
414
415 static const struct rte_cryptodev_capabilities dpaa2_pdcp_capabilities[] = {
416         {       /* SNOW 3G (UIA2) */
417                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
418                 {.sym = {
419                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
420                         {.auth = {
421                                 .algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2,
422                                 .block_size = 16,
423                                 .key_size = {
424                                         .min = 16,
425                                         .max = 16,
426                                         .increment = 0
427                                 },
428                                 .digest_size = {
429                                         .min = 4,
430                                         .max = 4,
431                                         .increment = 0
432                                 },
433                                 .iv_size = {
434                                         .min = 16,
435                                         .max = 16,
436                                         .increment = 0
437                                 }
438                         }, }
439                 }, }
440         },
441         {       /* SNOW 3G (UEA2) */
442                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
443                 {.sym = {
444                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
445                         {.cipher = {
446                                 .algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
447                                 .block_size = 16,
448                                 .key_size = {
449                                         .min = 16,
450                                         .max = 16,
451                                         .increment = 0
452                                 },
453                                 .iv_size = {
454                                         .min = 16,
455                                         .max = 16,
456                                         .increment = 0
457                                 }
458                         }, }
459                 }, }
460         },
461         {       /* AES CTR */
462                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
463                 {.sym = {
464                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
465                         {.cipher = {
466                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
467                                 .block_size = 16,
468                                 .key_size = {
469                                         .min = 16,
470                                         .max = 32,
471                                         .increment = 8
472                                 },
473                                 .iv_size = {
474                                         .min = 16,
475                                         .max = 16,
476                                         .increment = 0
477                                 }
478                         }, }
479                 }, }
480         },
481         {       /* NULL (AUTH) */
482                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
483                 {.sym = {
484                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
485                         {.auth = {
486                                 .algo = RTE_CRYPTO_AUTH_NULL,
487                                 .block_size = 1,
488                                 .key_size = {
489                                         .min = 0,
490                                         .max = 0,
491                                         .increment = 0
492                                 },
493                                 .digest_size = {
494                                         .min = 0,
495                                         .max = 0,
496                                         .increment = 0
497                                 },
498                                 .iv_size = { 0 }
499                         }, },
500                 }, },
501         },
502         {       /* NULL (CIPHER) */
503                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
504                 {.sym = {
505                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
506                         {.cipher = {
507                                 .algo = RTE_CRYPTO_CIPHER_NULL,
508                                 .block_size = 1,
509                                 .key_size = {
510                                         .min = 0,
511                                         .max = 0,
512                                         .increment = 0
513                                 },
514                                 .iv_size = {
515                                         .min = 0,
516                                         .max = 0,
517                                         .increment = 0
518                                 }
519                         }, },
520                 }, }
521         },
522         {       /* ZUC (EEA3) */
523                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
524                 {.sym = {
525                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
526                         {.cipher = {
527                                 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
528                                 .block_size = 16,
529                                 .key_size = {
530                                         .min = 16,
531                                         .max = 16,
532                                         .increment = 0
533                                 },
534                                 .iv_size = {
535                                         .min = 16,
536                                         .max = 16,
537                                         .increment = 0
538                                 }
539                         }, }
540                 }, }
541         },
542         {       /* ZUC (EIA3) */
543                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
544                 {.sym = {
545                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
546                         {.auth = {
547                                 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
548                                 .block_size = 16,
549                                 .key_size = {
550                                         .min = 16,
551                                         .max = 16,
552                                         .increment = 0
553                                 },
554                                 .digest_size = {
555                                         .min = 4,
556                                         .max = 4,
557                                         .increment = 0
558                                 },
559                                 .iv_size = {
560                                         .min = 16,
561                                         .max = 16,
562                                         .increment = 0
563                                 }
564                         }, }
565                 }, }
566         },
567
568         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
569 };
570
571 static const struct rte_security_capability dpaa2_sec_security_cap[] = {
572         { /* IPsec Lookaside Protocol offload ESP Transport Egress */
573                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
574                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
575                 .ipsec = {
576                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
577                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
578                         .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
579                         .options = { 0 }
580                 },
581                 .crypto_capabilities = dpaa2_sec_capabilities
582         },
583         { /* IPsec Lookaside Protocol offload ESP Tunnel Ingress */
584                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
585                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
586                 .ipsec = {
587                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
588                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
589                         .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
590                         .options = { 0 }
591                 },
592                 .crypto_capabilities = dpaa2_sec_capabilities
593         },
594         { /* PDCP Lookaside Protocol offload Data */
595                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
596                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
597                 .pdcp = {
598                         .domain = RTE_SECURITY_PDCP_MODE_DATA,
599                         .capa_flags = 0
600                 },
601                 .crypto_capabilities = dpaa2_pdcp_capabilities
602         },
603         { /* PDCP Lookaside Protocol offload Control */
604                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
605                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
606                 .pdcp = {
607                         .domain = RTE_SECURITY_PDCP_MODE_CONTROL,
608                         .capa_flags = 0
609                 },
610                 .crypto_capabilities = dpaa2_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 /* _RTE_DPAA2_SEC_PMD_PRIVATE_H_ */