bus/fslmc: support dynamic IOVA
[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 #define CRYPTODEV_NAME_DPAA2_SEC_PMD    crypto_dpaa2_sec
12 /**< NXP DPAA2 - SEC PMD device name */
13
14 #define MAX_QUEUES              64
15 #define MAX_DESC_SIZE           64
16 /** private data structure for each DPAA2_SEC device */
17 struct dpaa2_sec_dev_private {
18         void *mc_portal; /**< MC Portal for configuring this device */
19         void *hw; /**< Hardware handle for this device.Used by NADK framework */
20         struct rte_mempool *fle_pool; /* per device memory pool for FLE */
21         int32_t hw_id; /**< An unique ID of this device instance */
22         int32_t vfio_fd; /**< File descriptor received via VFIO */
23         uint16_t token; /**< Token required by DPxxx objects */
24         unsigned int max_nb_queue_pairs;
25         /**< Max number of queue pairs supported by device */
26         unsigned int max_nb_sessions;
27         /**< Max number of sessions 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_SET_FLC_EWS(flc)  (flc->word1_bits23_16 |= 0x1)
45 #define DPAA2_SET_FLC_RSC(flc)  (flc->word1_bits31_24 |= 0x1)
46 #define DPAA2_SET_FLC_REUSE_BS(flc) (flc->mode_bits |= 0x8000)
47 #define DPAA2_SET_FLC_REUSE_FF(flc) (flc->mode_bits |= 0x2000)
48
49 /* SEC Flow Context Descriptor */
50 struct sec_flow_context {
51         /* word 0 */
52         uint16_t word0_sdid;            /* 11-0  SDID */
53         uint16_t word0_res;             /* 31-12 reserved */
54
55         /* word 1 */
56         uint8_t word1_sdl;              /* 5-0 SDL */
57                                         /* 7-6 reserved */
58
59         uint8_t word1_bits_15_8;        /* 11-8 CRID */
60                                         /* 14-12 reserved */
61                                         /* 15 CRJD */
62
63         uint8_t word1_bits23_16;        /* 16  EWS */
64                                         /* 17 DAC */
65                                         /* 18,19,20 ? */
66                                         /* 23-21 reserved */
67
68         uint8_t word1_bits31_24;        /* 24 RSC */
69                                         /* 25 RBMT */
70                                         /* 31-26 reserved */
71
72         /* word 2  RFLC[31-0] */
73         uint32_t word2_rflc_31_0;
74
75         /* word 3  RFLC[63-32] */
76         uint32_t word3_rflc_63_32;
77
78         /* word 4 */
79         uint16_t word4_iicid;           /* 15-0  IICID */
80         uint16_t word4_oicid;           /* 31-16 OICID */
81
82         /* word 5 */
83         uint32_t word5_ofqid:24;                /* 23-0 OFQID */
84         uint32_t word5_31_24:8;
85                                         /* 24 OSC */
86                                         /* 25 OBMT */
87                                         /* 29-26 reserved */
88                                         /* 31-30 ICR */
89
90         /* word 6 */
91         uint32_t word6_oflc_31_0;
92
93         /* word 7 */
94         uint32_t word7_oflc_63_32;
95
96         /* Word 8-15 storage profiles */
97         uint16_t dl;                    /**<  DataLength(correction) */
98         uint16_t reserved;              /**< reserved */
99         uint16_t dhr;                   /**< DataHeadRoom(correction) */
100         uint16_t mode_bits;             /**< mode bits */
101         uint16_t bpv0;                  /**< buffer pool0 valid */
102         uint16_t bpid0;                 /**< Bypass Memory Translation */
103         uint16_t bpv1;                  /**< buffer pool1 valid */
104         uint16_t bpid1;                 /**< Bypass Memory Translation */
105         uint64_t word_12_15[2];         /**< word 12-15 are reserved */
106 };
107
108 struct sec_flc_desc {
109         struct sec_flow_context flc;
110         uint32_t desc[MAX_DESC_SIZE];
111 };
112
113 struct ctxt_priv {
114         struct rte_mempool *fle_pool; /* per device memory pool for FLE */
115         struct sec_flc_desc flc_desc[0];
116 };
117
118 enum dpaa2_sec_op_type {
119         DPAA2_SEC_NONE,  /*!< No Cipher operations*/
120         DPAA2_SEC_CIPHER,/*!< CIPHER operations */
121         DPAA2_SEC_AUTH,  /*!< Authentication Operations */
122         DPAA2_SEC_AEAD,  /*!< AEAD (AES-GCM/CCM) type operations */
123         DPAA2_SEC_CIPHER_HASH,  /*!< Authenticated Encryption with
124                                  * associated data
125                                  */
126         DPAA2_SEC_HASH_CIPHER,  /*!< Encryption with Authenticated
127                                  * associated data
128                                  */
129         DPAA2_SEC_IPSEC, /*!< IPSEC protocol operations*/
130         DPAA2_SEC_PDCP,  /*!< PDCP protocol operations*/
131         DPAA2_SEC_PKC,   /*!< Public Key Cryptographic Operations */
132         DPAA2_SEC_MAX
133 };
134
135 struct dpaa2_sec_aead_ctxt {
136         uint16_t auth_only_len; /*!< Length of data for Auth only */
137         uint8_t auth_cipher_text;       /**< Authenticate/cipher ordering */
138 };
139
140 typedef struct dpaa2_sec_session_entry {
141         void *ctxt;
142         uint8_t ctxt_type;
143         uint8_t dir;         /*!< Operation Direction */
144         enum rte_crypto_cipher_algorithm cipher_alg; /*!< Cipher Algorithm*/
145         enum rte_crypto_auth_algorithm auth_alg; /*!< Authentication Algorithm*/
146         union {
147                 struct {
148                         uint8_t *data;  /**< pointer to key data */
149                         size_t length;  /**< key length in bytes */
150                 } aead_key;
151                 struct {
152                         struct {
153                                 uint8_t *data;  /**< pointer to key data */
154                                 size_t length;  /**< key length in bytes */
155                         } cipher_key;
156                         struct {
157                                 uint8_t *data;  /**< pointer to key data */
158                                 size_t length;  /**< key length in bytes */
159                         } auth_key;
160                 };
161         };
162         struct {
163                 uint16_t length; /**< IV length in bytes */
164                 uint16_t offset; /**< IV offset in bytes */
165         } iv;
166         uint16_t digest_length;
167         uint8_t status;
168         union {
169                 struct dpaa2_sec_aead_ctxt aead_ctxt;
170         } ext_params;
171 } dpaa2_sec_session;
172
173 static const struct rte_cryptodev_capabilities dpaa2_sec_capabilities[] = {
174         {       /* MD5 HMAC */
175                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
176                 {.sym = {
177                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
178                         {.auth = {
179                                 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
180                                 .block_size = 64,
181                                 .key_size = {
182                                         .min = 1,
183                                         .max = 64,
184                                         .increment = 1
185                                 },
186                                 .digest_size = {
187                                         .min = 16,
188                                         .max = 16,
189                                         .increment = 0
190                                 },
191                                 .iv_size = { 0 }
192                         }, }
193                 }, }
194         },
195         {       /* SHA1 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_SHA1_HMAC,
201                                 .block_size = 64,
202                                 .key_size = {
203                                         .min = 1,
204                                         .max = 64,
205                                         .increment = 1
206                                 },
207                                 .digest_size = {
208                                         .min = 20,
209                                         .max = 20,
210                                         .increment = 0
211                                 },
212                                 .iv_size = { 0 }
213                         }, }
214                 }, }
215         },
216         {       /* SHA224 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_SHA224_HMAC,
222                                 .block_size = 64,
223                                 .key_size = {
224                                         .min = 1,
225                                         .max = 64,
226                                         .increment = 1
227                                 },
228                                 .digest_size = {
229                                         .min = 28,
230                                         .max = 28,
231                                         .increment = 0
232                                 },
233                                 .iv_size = { 0 }
234                         }, }
235                 }, }
236         },
237         {       /* SHA256 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_SHA256_HMAC,
243                                 .block_size = 64,
244                                 .key_size = {
245                                         .min = 1,
246                                         .max = 64,
247                                         .increment = 1
248                                 },
249                                 .digest_size = {
250                                                 .min = 32,
251                                                 .max = 32,
252                                                 .increment = 0
253                                 },
254                                 .iv_size = { 0 }
255                                 }, }
256                         }, }
257                 },
258         {       /* SHA384 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_SHA384_HMAC,
264                                 .block_size = 128,
265                                 .key_size = {
266                                         .min = 1,
267                                         .max = 128,
268                                         .increment = 1
269                                 },
270                                 .digest_size = {
271                                         .min = 48,
272                                         .max = 48,
273                                         .increment = 0
274                                 },
275                                 .iv_size = { 0 }
276                         }, }
277                 }, }
278         },
279         {       /* SHA512 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_SHA512_HMAC,
285                                 .block_size = 128,
286                                 .key_size = {
287                                         .min = 1,
288                                         .max = 128,
289                                         .increment = 1
290                                 },
291                                 .digest_size = {
292                                         .min = 64,
293                                         .max = 64,
294                                         .increment = 0
295                                 },
296                                 .iv_size = { 0 }
297                         }, }
298                 }, }
299         },
300         {       /* AES GCM */
301                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
302                 {.sym = {
303                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
304                         {.aead = {
305                                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
306                                 .block_size = 16,
307                                 .key_size = {
308                                         .min = 16,
309                                         .max = 32,
310                                         .increment = 8
311                                 },
312                                 .digest_size = {
313                                         .min = 8,
314                                         .max = 16,
315                                         .increment = 4
316                                 },
317                                 .aad_size = {
318                                         .min = 0,
319                                         .max = 240,
320                                         .increment = 1
321                                 },
322                                 .iv_size = {
323                                         .min = 12,
324                                         .max = 12,
325                                         .increment = 0
326                                 },
327                         }, }
328                 }, }
329         },
330         {       /* AES CBC */
331                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
332                 {.sym = {
333                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
334                         {.cipher = {
335                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
336                                 .block_size = 16,
337                                 .key_size = {
338                                         .min = 16,
339                                         .max = 32,
340                                         .increment = 8
341                                 },
342                                 .iv_size = {
343                                         .min = 16,
344                                         .max = 16,
345                                         .increment = 0
346                                 }
347                         }, }
348                 }, }
349         },
350         {       /* AES CTR */
351                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
352                 {.sym = {
353                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
354                         {.cipher = {
355                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
356                                 .block_size = 16,
357                                 .key_size = {
358                                         .min = 16,
359                                         .max = 32,
360                                         .increment = 8
361                                 },
362                                 .iv_size = {
363                                         .min = 16,
364                                         .max = 16,
365                                         .increment = 0
366                                 },
367                         }, }
368                 }, }
369         },
370         {       /* 3DES CBC */
371                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
372                 {.sym = {
373                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
374                         {.cipher = {
375                                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
376                                 .block_size = 8,
377                                 .key_size = {
378                                         .min = 16,
379                                         .max = 24,
380                                         .increment = 8
381                                 },
382                                 .iv_size = {
383                                         .min = 8,
384                                         .max = 8,
385                                         .increment = 0
386                                 }
387                         }, }
388                 }, }
389         },
390
391         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
392 };
393
394 static const struct rte_security_capability dpaa2_sec_security_cap[] = {
395         { /* IPsec Lookaside Protocol offload ESP Transport Egress */
396                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
397                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
398                 .ipsec = {
399                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
400                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
401                         .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
402                         .options = { 0 }
403                 },
404                 .crypto_capabilities = dpaa2_sec_capabilities
405         },
406         { /* IPsec Lookaside Protocol offload ESP Tunnel Ingress */
407                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
408                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
409                 .ipsec = {
410                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
411                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
412                         .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
413                         .options = { 0 }
414                 },
415                 .crypto_capabilities = dpaa2_sec_capabilities
416         },
417         {
418                 .action = RTE_SECURITY_ACTION_TYPE_NONE
419         }
420 };
421
422 /**
423  * Checksum
424  *
425  * @param buffer calculate chksum for buffer
426  * @param len    buffer length
427  *
428  * @return checksum value in host cpu order
429  */
430 static inline uint16_t
431 calc_chksum(void *buffer, int len)
432 {
433         uint16_t *buf = (uint16_t *)buffer;
434         uint32_t sum = 0;
435         uint16_t result;
436
437         for (sum = 0; len > 1; len -= 2)
438                 sum += *buf++;
439
440         if (len == 1)
441                 sum += *(unsigned char *)buf;
442
443         sum = (sum >> 16) + (sum & 0xFFFF);
444         sum += (sum >> 16);
445         result = ~sum;
446
447         return  result;
448 }
449
450 #endif /* _RTE_DPAA2_SEC_PMD_PRIVATE_H_ */