crypto/dpaa_sec: add macro for device name
[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
30 #define DIR_ENC                 1
31 #define DIR_DEC                 0
32
33 enum dpaa_sec_op_type {
34         DPAA_SEC_NONE,  /*!< No Cipher operations*/
35         DPAA_SEC_CIPHER,/*!< CIPHER operations */
36         DPAA_SEC_AUTH,  /*!< Authentication Operations */
37         DPAA_SEC_AEAD,  /*!< Authenticated Encryption with associated data */
38         DPAA_SEC_IPSEC, /*!< IPSEC protocol operations*/
39         DPAA_SEC_PDCP,  /*!< PDCP protocol operations*/
40         DPAA_SEC_PKC,   /*!< Public Key Cryptographic Operations */
41         DPAA_SEC_MAX
42 };
43
44
45 #define DPAA_SEC_MAX_DESC_SIZE  64
46 /* code or cmd block to caam */
47 struct sec_cdb {
48         struct {
49                 union {
50                         uint32_t word;
51                         struct {
52 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
53                                 uint16_t rsvd63_48;
54                                 unsigned int rsvd47_39:9;
55                                 unsigned int idlen:7;
56 #else
57                                 unsigned int idlen:7;
58                                 unsigned int rsvd47_39:9;
59                                 uint16_t rsvd63_48;
60 #endif
61                         } field;
62                 } __packed hi;
63
64                 union {
65                         uint32_t word;
66                         struct {
67 #if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
68                                 unsigned int rsvd31_30:2;
69                                 unsigned int fsgt:1;
70                                 unsigned int lng:1;
71                                 unsigned int offset:2;
72                                 unsigned int abs:1;
73                                 unsigned int add_buf:1;
74                                 uint8_t pool_id;
75                                 uint16_t pool_buffer_size;
76 #else
77                                 uint16_t pool_buffer_size;
78                                 uint8_t pool_id;
79                                 unsigned int add_buf:1;
80                                 unsigned int abs:1;
81                                 unsigned int offset:2;
82                                 unsigned int lng:1;
83                                 unsigned int fsgt:1;
84                                 unsigned int rsvd31_30:2;
85 #endif
86                         } field;
87                 } __packed lo;
88         } __packed sh_hdr;
89
90         uint32_t sh_desc[DPAA_SEC_MAX_DESC_SIZE];
91 };
92
93 typedef struct dpaa_sec_session_entry {
94         uint8_t dir;         /*!< Operation Direction */
95         enum rte_crypto_cipher_algorithm cipher_alg; /*!< Cipher Algorithm*/
96         enum rte_crypto_auth_algorithm auth_alg; /*!< Authentication Algorithm*/
97         enum rte_crypto_aead_algorithm aead_alg; /*!< AEAD Algorithm*/
98         enum rte_security_session_protocol proto_alg; /*!< Security Algorithm*/
99         union {
100                 struct {
101                         uint8_t *data;  /**< pointer to key data */
102                         size_t length;  /**< key length in bytes */
103                 } aead_key;
104                 struct {
105                         struct {
106                                 uint8_t *data;  /**< pointer to key data */
107                                 size_t length;  /**< key length in bytes */
108                         } cipher_key;
109                         struct {
110                                 uint8_t *data;  /**< pointer to key data */
111                                 size_t length;  /**< key length in bytes */
112                         } auth_key;
113                 };
114         };
115         struct {
116                 uint16_t length;
117                 uint16_t offset;
118         } iv;   /**< Initialisation vector parameters */
119         uint16_t auth_only_len; /*!< Length of data for Auth only */
120         uint32_t digest_length;
121         struct ipsec_encap_pdb encap_pdb;
122         struct ip ip4_hdr;
123         struct ipsec_decap_pdb decap_pdb;
124         struct dpaa_sec_qp *qp;
125         struct qman_fq *inq;
126         struct sec_cdb cdb;     /**< cmd block associated with qp */
127         struct rte_mempool *ctx_pool; /* session mempool for dpaa_sec_op_ctx */
128 } dpaa_sec_session;
129
130 struct dpaa_sec_qp {
131         struct dpaa_sec_dev_private *internals;
132         struct qman_fq outq;
133         int rx_pkts;
134         int rx_errs;
135         int tx_pkts;
136         int tx_errs;
137 };
138
139 #define RTE_DPAA_MAX_NB_SEC_QPS 1
140 #define RTE_DPAA_MAX_RX_QUEUE RTE_DPAA_SEC_PMD_MAX_NB_SESSIONS
141 #define DPAA_MAX_DEQUEUE_NUM_FRAMES 63
142
143 /* internal sec queue interface */
144 struct dpaa_sec_dev_private {
145         void *sec_hw;
146         struct rte_mempool *ctx_pool; /* per dev mempool for dpaa_sec_op_ctx */
147         struct dpaa_sec_qp qps[RTE_DPAA_MAX_NB_SEC_QPS]; /* i/o queue for sec */
148         struct qman_fq inq[RTE_DPAA_MAX_RX_QUEUE];
149         unsigned char inq_attach[RTE_DPAA_MAX_RX_QUEUE];
150         unsigned int max_nb_queue_pairs;
151         unsigned int max_nb_sessions;
152 };
153
154 #define MAX_SG_ENTRIES          16
155 #define SG_CACHELINE_0          0
156 #define SG_CACHELINE_1          4
157 #define SG_CACHELINE_2          8
158 #define SG_CACHELINE_3          12
159 struct dpaa_sec_job {
160         /* sg[0] output, sg[1] input, others are possible sub frames */
161         struct qm_sg_entry sg[MAX_SG_ENTRIES];
162 };
163
164 #define DPAA_MAX_NB_MAX_DIGEST  32
165 struct dpaa_sec_op_ctx {
166         struct dpaa_sec_job job;
167         struct rte_crypto_op *op;
168         struct rte_mempool *ctx_pool; /* mempool pointer for dpaa_sec_op_ctx */
169         uint32_t fd_status;
170         int64_t vtop_offset;
171         uint8_t digest[DPAA_MAX_NB_MAX_DIGEST];
172 };
173
174 static const struct rte_cryptodev_capabilities dpaa_sec_capabilities[] = {
175         {       /* MD5 HMAC */
176                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
177                 {.sym = {
178                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
179                         {.auth = {
180                                 .algo = RTE_CRYPTO_AUTH_MD5_HMAC,
181                                 .block_size = 64,
182                                 .key_size = {
183                                         .min = 1,
184                                         .max = 64,
185                                         .increment = 1
186                                 },
187                                 .digest_size = {
188                                         .min = 16,
189                                         .max = 16,
190                                         .increment = 0
191                                 },
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                         }, }
213                 }, }
214         },
215         {       /* SHA224 HMAC */
216                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
217                 {.sym = {
218                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
219                         {.auth = {
220                                 .algo = RTE_CRYPTO_AUTH_SHA224_HMAC,
221                                 .block_size = 64,
222                                 .key_size = {
223                                         .min = 1,
224                                         .max = 64,
225                                         .increment = 1
226                                 },
227                                 .digest_size = {
228                                         .min = 28,
229                                         .max = 28,
230                                         .increment = 0
231                                 },
232                         }, }
233                 }, }
234         },
235         {       /* SHA256 HMAC */
236                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
237                 {.sym = {
238                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
239                         {.auth = {
240                                 .algo = RTE_CRYPTO_AUTH_SHA256_HMAC,
241                                 .block_size = 64,
242                                 .key_size = {
243                                         .min = 1,
244                                         .max = 64,
245                                         .increment = 1
246                                 },
247                                 .digest_size = {
248                                         .min = 32,
249                                         .max = 32,
250                                         .increment = 0
251                                 },
252                         }, }
253                 }, }
254         },
255         {       /* SHA384 HMAC */
256                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
257                 {.sym = {
258                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
259                         {.auth = {
260                                 .algo = RTE_CRYPTO_AUTH_SHA384_HMAC,
261                                 .block_size = 128,
262                                 .key_size = {
263                                         .min = 1,
264                                         .max = 128,
265                                         .increment = 1
266                                 },
267                                 .digest_size = {
268                                         .min = 48,
269                                         .max = 48,
270                                         .increment = 0
271                                 },
272                         }, }
273                 }, }
274         },
275         {       /* SHA512 HMAC */
276                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
277                 {.sym = {
278                         .xform_type = RTE_CRYPTO_SYM_XFORM_AUTH,
279                         {.auth = {
280                                 .algo = RTE_CRYPTO_AUTH_SHA512_HMAC,
281                                 .block_size = 128,
282                                 .key_size = {
283                                         .min = 1,
284                                         .max = 128,
285                                         .increment = 1
286                                 },
287                                 .digest_size = {
288                                         .min = 64,
289                                         .max = 64,
290                                         .increment = 0
291                                 },
292                         }, }
293                 }, }
294         },
295         {       /* AES GCM */
296                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
297                 {.sym = {
298                         .xform_type = RTE_CRYPTO_SYM_XFORM_AEAD,
299                         {.aead = {
300                                 .algo = RTE_CRYPTO_AEAD_AES_GCM,
301                                 .block_size = 16,
302                                 .key_size = {
303                                         .min = 16,
304                                         .max = 32,
305                                         .increment = 8
306                                 },
307                                 .digest_size = {
308                                         .min = 8,
309                                         .max = 16,
310                                         .increment = 4
311                                 },
312                                 .aad_size = {
313                                         .min = 0,
314                                         .max = 240,
315                                         .increment = 1
316                                 },
317                                 .iv_size = {
318                                         .min = 12,
319                                         .max = 12,
320                                         .increment = 0
321                                 },
322                         }, }
323                 }, }
324         },
325         {       /* AES CBC */
326                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
327                 {.sym = {
328                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
329                         {.cipher = {
330                                 .algo = RTE_CRYPTO_CIPHER_AES_CBC,
331                                 .block_size = 16,
332                                 .key_size = {
333                                         .min = 16,
334                                         .max = 32,
335                                         .increment = 8
336                                 },
337                                 .iv_size = {
338                                         .min = 16,
339                                         .max = 16,
340                                         .increment = 0
341                                 }
342                         }, }
343                 }, }
344         },
345         {       /* AES CTR */
346                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
347                 {.sym = {
348                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
349                         {.cipher = {
350                                 .algo = RTE_CRYPTO_CIPHER_AES_CTR,
351                                 .block_size = 16,
352                                 .key_size = {
353                                         .min = 16,
354                                         .max = 32,
355                                         .increment = 8
356                                 },
357                                 .iv_size = {
358                                         .min = 16,
359                                         .max = 16,
360                                         .increment = 0
361                                 }
362                         }, }
363                 }, }
364         },
365         {       /* 3DES CBC */
366                 .op = RTE_CRYPTO_OP_TYPE_SYMMETRIC,
367                 {.sym = {
368                         .xform_type = RTE_CRYPTO_SYM_XFORM_CIPHER,
369                         {.cipher = {
370                                 .algo = RTE_CRYPTO_CIPHER_3DES_CBC,
371                                 .block_size = 8,
372                                 .key_size = {
373                                         .min = 16,
374                                         .max = 24,
375                                         .increment = 8
376                                 },
377                                 .iv_size = {
378                                         .min = 8,
379                                         .max = 8,
380                                         .increment = 0
381                                 }
382                         }, }
383                 }, }
384         },
385
386         RTE_CRYPTODEV_END_OF_CAPABILITIES_LIST()
387 };
388
389 static const struct rte_security_capability dpaa_sec_security_cap[] = {
390         { /* IPsec Lookaside Protocol offload ESP Transport Egress */
391                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
392                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
393                 .ipsec = {
394                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
395                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
396                         .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
397                         .options = { 0 }
398                 },
399                 .crypto_capabilities = dpaa_sec_capabilities
400         },
401         { /* IPsec Lookaside Protocol offload ESP Tunnel Ingress */
402                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
403                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
404                 .ipsec = {
405                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
406                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
407                         .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
408                         .options = { 0 }
409                 },
410                 .crypto_capabilities = dpaa_sec_capabilities
411         },
412         {
413                 .action = RTE_SECURITY_ACTION_TYPE_NONE
414         }
415 };
416
417 /**
418  * Checksum
419  *
420  * @param buffer calculate chksum for buffer
421  * @param len    buffer length
422  *
423  * @return checksum value in host cpu order
424  */
425 static inline uint16_t
426 calc_chksum(void *buffer, int len)
427 {
428         uint16_t *buf = (uint16_t *)buffer;
429         uint32_t sum = 0;
430         uint16_t result;
431
432         for (sum = 0; len > 1; len -= 2)
433                 sum += *buf++;
434
435         if (len == 1)
436                 sum += *(unsigned char *)buf;
437
438         sum = (sum >> 16) + (sum & 0xFFFF);
439         sum += (sum >> 16);
440         result = ~sum;
441
442         return  result;
443 }
444
445 #endif /* _DPAA_SEC_H_ */