crypto/dpaax_sec: support HFN override
[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         uint8_t sn_size;        /*!< Sequence number size, 5/7/12/15/18 */
151         uint32_t hfn_ovd_offset;/*!< offset from rte_crypto_op at which
152                                  * per packet hfn is stored
153                                  */
154         uint32_t hfn;   /*!< Hyper Frame Number */
155         uint32_t hfn_threshold; /*!< HFN Threashold for key renegotiation */
156 };
157
158 typedef struct dpaa2_sec_session_entry {
159         void *ctxt;
160         uint8_t ctxt_type;
161         uint8_t dir;         /*!< Operation Direction */
162         enum rte_crypto_cipher_algorithm cipher_alg; /*!< Cipher Algorithm*/
163         enum rte_crypto_auth_algorithm auth_alg; /*!< Authentication Algorithm*/
164         enum rte_crypto_aead_algorithm aead_alg; /*!< AEAD Algorithm*/
165         union {
166                 struct {
167                         uint8_t *data;  /**< pointer to key data */
168                         size_t length;  /**< key length in bytes */
169                 } aead_key;
170                 struct {
171                         struct {
172                                 uint8_t *data;  /**< pointer to key data */
173                                 size_t length;  /**< key length in bytes */
174                         } cipher_key;
175                         struct {
176                                 uint8_t *data;  /**< pointer to key data */
177                                 size_t length;  /**< key length in bytes */
178                         } auth_key;
179                 };
180         };
181         union {
182                 struct {
183                         struct {
184                                 uint16_t length; /**< IV length in bytes */
185                                 uint16_t offset; /**< IV offset in bytes */
186                         } iv;
187                         uint16_t digest_length;
188                         uint8_t status;
189                         union {
190                                 struct dpaa2_sec_aead_ctxt aead_ctxt;
191                         } ext_params;
192                 };
193                 struct dpaa2_pdcp_ctxt pdcp;
194         };
195 } dpaa2_sec_session;
196
197 static const struct rte_cryptodev_capabilities dpaa2_sec_capabilities[] = {
198         {       /* MD5 HMAC */
199                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
200                 {.sym = {
201                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
202                         {.auth = {
203                                 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
204                                 .block_size = 64,
205                                 .key_size = {
206                                         .min = 1,
207                                         .max = 64,
208                                         .increment = 1
209                                 },
210                                 .digest_size = {
211                                         .min = 1,
212                                         .max = 16,
213                                         .increment = 1
214                                 },
215                                 .iv_size = { 0 }
216                         }, }
217                 }, }
218         },
219         {       /* SHA1 HMAC */
220                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
221                 {.sym = {
222                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
223                         {.auth = {
224                                 .algo = RTE_CRYPTO_AUTH_SHA1_HMAC,
225                                 .block_size = 64,
226                                 .key_size = {
227                                         .min = 1,
228                                         .max = 64,
229                                         .increment = 1
230                                 },
231                                 .digest_size = {
232                                         .min = 1,
233                                         .max = 20,
234                                         .increment = 1
235                                 },
236                                 .iv_size = { 0 }
237                         }, }
238                 }, }
239         },
240         {       /* SHA224 HMAC */
241                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
242                 {.sym = {
243                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
244                         {.auth = {
245                                 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
246                                 .block_size = 64,
247                                 .key_size = {
248                                         .min = 1,
249                                         .max = 64,
250                                         .increment = 1
251                                 },
252                                 .digest_size = {
253                                         .min = 1,
254                                         .max = 28,
255                                         .increment = 1
256                                 },
257                                 .iv_size = { 0 }
258                         }, }
259                 }, }
260         },
261         {       /* SHA256 HMAC */
262                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
263                 {.sym = {
264                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
265                         {.auth = {
266                                 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
267                                 .block_size = 64,
268                                 .key_size = {
269                                         .min = 1,
270                                         .max = 64,
271                                         .increment = 1
272                                 },
273                                 .digest_size = {
274                                         .min = 1,
275                                         .max = 32,
276                                         .increment = 1
277                                 },
278                                 .iv_size = { 0 }
279                                 }, }
280                         }, }
281                 },
282         {       /* SHA384 HMAC */
283                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
284                 {.sym = {
285                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
286                         {.auth = {
287                                 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
288                                 .block_size = 128,
289                                 .key_size = {
290                                         .min = 1,
291                                         .max = 128,
292                                         .increment = 1
293                                 },
294                                 .digest_size = {
295                                         .min = 1,
296                                         .max = 48,
297                                         .increment = 1
298                                 },
299                                 .iv_size = { 0 }
300                         }, }
301                 }, }
302         },
303         {       /* SHA512 HMAC */
304                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
305                 {.sym = {
306                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
307                         {.auth = {
308                                 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
309                                 .block_size = 128,
310                                 .key_size = {
311                                         .min = 1,
312                                         .max = 128,
313                                         .increment = 1
314                                 },
315                                 .digest_size = {
316                                         .min = 1,
317                                         .max = 64,
318                                         .increment = 1
319                                 },
320                                 .iv_size = { 0 }
321                         }, }
322                 }, }
323         },
324         {       /* AES GCM */
325                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
326                 {.sym = {
327                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
328                         {.aead = {
329                                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
330                                 .block_size = 16,
331                                 .key_size = {
332                                         .min = 16,
333                                         .max = 32,
334                                         .increment = 8
335                                 },
336                                 .digest_size = {
337                                         .min = 8,
338                                         .max = 16,
339                                         .increment = 4
340                                 },
341                                 .aad_size = {
342                                         .min = 0,
343                                         .max = 240,
344                                         .increment = 1
345                                 },
346                                 .iv_size = {
347                                         .min = 12,
348                                         .max = 12,
349                                         .increment = 0
350                                 },
351                         }, }
352                 }, }
353         },
354         {       /* AES CBC */
355                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
356                 {.sym = {
357                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
358                         {.cipher = {
359                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
360                                 .block_size = 16,
361                                 .key_size = {
362                                         .min = 16,
363                                         .max = 32,
364                                         .increment = 8
365                                 },
366                                 .iv_size = {
367                                         .min = 16,
368                                         .max = 16,
369                                         .increment = 0
370                                 }
371                         }, }
372                 }, }
373         },
374         {       /* AES CTR */
375                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
376                 {.sym = {
377                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
378                         {.cipher = {
379                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
380                                 .block_size = 16,
381                                 .key_size = {
382                                         .min = 16,
383                                         .max = 32,
384                                         .increment = 8
385                                 },
386                                 .iv_size = {
387                                         .min = 16,
388                                         .max = 16,
389                                         .increment = 0
390                                 },
391                         }, }
392                 }, }
393         },
394         {       /* 3DES CBC */
395                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
396                 {.sym = {
397                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
398                         {.cipher = {
399                                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
400                                 .block_size = 8,
401                                 .key_size = {
402                                         .min = 16,
403                                         .max = 24,
404                                         .increment = 8
405                                 },
406                                 .iv_size = {
407                                         .min = 8,
408                                         .max = 8,
409                                         .increment = 0
410                                 }
411                         }, }
412                 }, }
413         },
414
415         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
416 };
417
418 static const struct rte_cryptodev_capabilities dpaa2_pdcp_capabilities[] = {
419         {       /* SNOW 3G (UIA2) */
420                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
421                 {.sym = {
422                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
423                         {.auth = {
424                                 .algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2,
425                                 .block_size = 16,
426                                 .key_size = {
427                                         .min = 16,
428                                         .max = 16,
429                                         .increment = 0
430                                 },
431                                 .digest_size = {
432                                         .min = 4,
433                                         .max = 4,
434                                         .increment = 0
435                                 },
436                                 .iv_size = {
437                                         .min = 16,
438                                         .max = 16,
439                                         .increment = 0
440                                 }
441                         }, }
442                 }, }
443         },
444         {       /* SNOW 3G (UEA2) */
445                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
446                 {.sym = {
447                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
448                         {.cipher = {
449                                 .algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2,
450                                 .block_size = 16,
451                                 .key_size = {
452                                         .min = 16,
453                                         .max = 16,
454                                         .increment = 0
455                                 },
456                                 .iv_size = {
457                                         .min = 16,
458                                         .max = 16,
459                                         .increment = 0
460                                 }
461                         }, }
462                 }, }
463         },
464         {       /* AES CTR */
465                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
466                 {.sym = {
467                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
468                         {.cipher = {
469                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
470                                 .block_size = 16,
471                                 .key_size = {
472                                         .min = 16,
473                                         .max = 32,
474                                         .increment = 8
475                                 },
476                                 .iv_size = {
477                                         .min = 16,
478                                         .max = 16,
479                                         .increment = 0
480                                 }
481                         }, }
482                 }, }
483         },
484         {       /* NULL (AUTH) */
485                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
486                 {.sym = {
487                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
488                         {.auth = {
489                                 .algo = RTE_CRYPTO_AUTH_NULL,
490                                 .block_size = 1,
491                                 .key_size = {
492                                         .min = 0,
493                                         .max = 0,
494                                         .increment = 0
495                                 },
496                                 .digest_size = {
497                                         .min = 0,
498                                         .max = 0,
499                                         .increment = 0
500                                 },
501                                 .iv_size = { 0 }
502                         }, },
503                 }, },
504         },
505         {       /* NULL (CIPHER) */
506                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
507                 {.sym = {
508                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
509                         {.cipher = {
510                                 .algo = RTE_CRYPTO_CIPHER_NULL,
511                                 .block_size = 1,
512                                 .key_size = {
513                                         .min = 0,
514                                         .max = 0,
515                                         .increment = 0
516                                 },
517                                 .iv_size = {
518                                         .min = 0,
519                                         .max = 0,
520                                         .increment = 0
521                                 }
522                         }, },
523                 }, }
524         },
525         {       /* ZUC (EEA3) */
526                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
527                 {.sym = {
528                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
529                         {.cipher = {
530                                 .algo = RTE_CRYPTO_CIPHER_ZUC_EEA3,
531                                 .block_size = 16,
532                                 .key_size = {
533                                         .min = 16,
534                                         .max = 16,
535                                         .increment = 0
536                                 },
537                                 .iv_size = {
538                                         .min = 16,
539                                         .max = 16,
540                                         .increment = 0
541                                 }
542                         }, }
543                 }, }
544         },
545         {       /* ZUC (EIA3) */
546                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
547                 {.sym = {
548                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
549                         {.auth = {
550                                 .algo = RTE_CRYPTO_AUTH_ZUC_EIA3,
551                                 .block_size = 16,
552                                 .key_size = {
553                                         .min = 16,
554                                         .max = 16,
555                                         .increment = 0
556                                 },
557                                 .digest_size = {
558                                         .min = 4,
559                                         .max = 4,
560                                         .increment = 0
561                                 },
562                                 .iv_size = {
563                                         .min = 16,
564                                         .max = 16,
565                                         .increment = 0
566                                 }
567                         }, }
568                 }, }
569         },
570
571         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
572 };
573
574 static const struct rte_security_capability dpaa2_sec_security_cap[] = {
575         { /* IPsec Lookaside Protocol offload ESP Transport Egress */
576                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
577                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
578                 .ipsec = {
579                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
580                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
581                         .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
582                         .options = { 0 }
583                 },
584                 .crypto_capabilities = dpaa2_sec_capabilities
585         },
586         { /* IPsec Lookaside Protocol offload ESP Tunnel Ingress */
587                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
588                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
589                 .ipsec = {
590                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
591                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
592                         .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
593                         .options = { 0 }
594                 },
595                 .crypto_capabilities = dpaa2_sec_capabilities
596         },
597         { /* PDCP Lookaside Protocol offload Data */
598                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
599                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
600                 .pdcp = {
601                         .domain = RTE_SECURITY_PDCP_MODE_DATA,
602                         .capa_flags = 0
603                 },
604                 .crypto_capabilities = dpaa2_pdcp_capabilities
605         },
606         { /* PDCP Lookaside Protocol offload Control */
607                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
608                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
609                 .pdcp = {
610                         .domain = RTE_SECURITY_PDCP_MODE_CONTROL,
611                         .capa_flags = 0
612                 },
613                 .crypto_capabilities = dpaa2_pdcp_capabilities
614         },
615         {
616                 .action = RTE_SECURITY_ACTION_TYPE_NONE
617         }
618 };
619
620 /**
621  * Checksum
622  *
623  * @param buffer calculate chksum for buffer
624  * @param len    buffer length
625  *
626  * @return checksum value in host cpu order
627  */
628 static inline uint16_t
629 calc_chksum(void *buffer, int len)
630 {
631         uint16_t *buf = (uint16_t *)buffer;
632         uint32_t sum = 0;
633         uint16_t result;
634
635         for (sum = 0; len > 1; len -= 2)
636                 sum += *buf++;
637
638         if (len == 1)
639                 sum += *(unsigned char *)buf;
640
641         sum = (sum >> 16) + (sum & 0xFFFF);
642         sum += (sum >> 16);
643         result = ~sum;
644
645         return  result;
646 }
647
648 #endif /* _RTE_DPAA2_SEC_PMD_PRIVATE_H_ */