Imported Upstream version 17.05.2
[deb_dpdk.git] / drivers / crypto / armv8 / rte_armv8_pmd_private.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium networks Ltd. 2017.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium networks nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef _RTE_ARMV8_PMD_PRIVATE_H_
34 #define _RTE_ARMV8_PMD_PRIVATE_H_
35
36 #define ARMV8_CRYPTO_LOG_ERR(fmt, args...) \
37         RTE_LOG(ERR, CRYPTODEV, "[%s] %s() line %u: " fmt "\n",  \
38                         RTE_STR(CRYPTODEV_NAME_ARMV8_CRYPTO_PMD), \
39                         __func__, __LINE__, ## args)
40
41 #ifdef RTE_LIBRTE_ARMV8_CRYPTO_DEBUG
42 #define ARMV8_CRYPTO_LOG_INFO(fmt, args...) \
43         RTE_LOG(INFO, CRYPTODEV, "[%s] %s() line %u: " fmt "\n", \
44                         RTE_STR(CRYPTODEV_NAME_ARMV8_CRYPTO_PMD), \
45                         __func__, __LINE__, ## args)
46
47 #define ARMV8_CRYPTO_LOG_DBG(fmt, args...) \
48         RTE_LOG(DEBUG, CRYPTODEV, "[%s] %s() line %u: " fmt "\n", \
49                         RTE_STR(CRYPTODEV_NAME_ARMV8_CRYPTO_PMD), \
50                         __func__, __LINE__, ## args)
51
52 #define ARMV8_CRYPTO_ASSERT(con)                                \
53 do {                                                            \
54         if (!(con)) {                                           \
55                 rte_panic("%s(): "                              \
56                     con "condition failed, line %u", __func__); \
57         }                                                       \
58 } while (0)
59
60 #else
61 #define ARMV8_CRYPTO_LOG_INFO(fmt, args...)
62 #define ARMV8_CRYPTO_LOG_DBG(fmt, args...)
63 #define ARMV8_CRYPTO_ASSERT(con)
64 #endif
65
66 #define NBBY            8               /* Number of bits in a byte */
67 #define BYTE_LENGTH(x)  ((x) / NBBY)    /* Number of bytes in x (round down) */
68
69 /** ARMv8 operation order mode enumerator */
70 enum armv8_crypto_chain_order {
71         ARMV8_CRYPTO_CHAIN_CIPHER_AUTH,
72         ARMV8_CRYPTO_CHAIN_AUTH_CIPHER,
73         ARMV8_CRYPTO_CHAIN_NOT_SUPPORTED,
74         ARMV8_CRYPTO_CHAIN_LIST_END = ARMV8_CRYPTO_CHAIN_NOT_SUPPORTED
75 };
76
77 /** ARMv8 cipher operation enumerator */
78 enum armv8_crypto_cipher_operation {
79         ARMV8_CRYPTO_CIPHER_OP_ENCRYPT = RTE_CRYPTO_CIPHER_OP_ENCRYPT,
80         ARMV8_CRYPTO_CIPHER_OP_DECRYPT = RTE_CRYPTO_CIPHER_OP_DECRYPT,
81         ARMV8_CRYPTO_CIPHER_OP_NOT_SUPPORTED,
82         ARMV8_CRYPTO_CIPHER_OP_LIST_END = ARMV8_CRYPTO_CIPHER_OP_NOT_SUPPORTED
83 };
84
85 enum armv8_crypto_cipher_keylen {
86         ARMV8_CRYPTO_CIPHER_KEYLEN_128,
87         ARMV8_CRYPTO_CIPHER_KEYLEN_192,
88         ARMV8_CRYPTO_CIPHER_KEYLEN_256,
89         ARMV8_CRYPTO_CIPHER_KEYLEN_NOT_SUPPORTED,
90         ARMV8_CRYPTO_CIPHER_KEYLEN_LIST_END =
91                 ARMV8_CRYPTO_CIPHER_KEYLEN_NOT_SUPPORTED
92 };
93
94 /** ARMv8 auth mode enumerator */
95 enum armv8_crypto_auth_mode {
96         ARMV8_CRYPTO_AUTH_AS_AUTH,
97         ARMV8_CRYPTO_AUTH_AS_HMAC,
98         ARMV8_CRYPTO_AUTH_AS_CIPHER,
99         ARMV8_CRYPTO_AUTH_NOT_SUPPORTED,
100         ARMV8_CRYPTO_AUTH_LIST_END = ARMV8_CRYPTO_AUTH_NOT_SUPPORTED
101 };
102
103 #define CRYPTO_ORDER_MAX                ARMV8_CRYPTO_CHAIN_LIST_END
104 #define CRYPTO_CIPHER_OP_MAX            ARMV8_CRYPTO_CIPHER_OP_LIST_END
105 #define CRYPTO_CIPHER_KEYLEN_MAX        ARMV8_CRYPTO_CIPHER_KEYLEN_LIST_END
106 #define CRYPTO_CIPHER_MAX               RTE_CRYPTO_CIPHER_LIST_END
107 #define CRYPTO_AUTH_MAX                 RTE_CRYPTO_AUTH_LIST_END
108
109 #define HMAC_IPAD_VALUE                 (0x36)
110 #define HMAC_OPAD_VALUE                 (0x5C)
111
112 #define SHA256_AUTH_KEY_LENGTH          (BYTE_LENGTH(256))
113 #define SHA256_BLOCK_SIZE               (BYTE_LENGTH(512))
114
115 #define SHA1_AUTH_KEY_LENGTH            (BYTE_LENGTH(160))
116 #define SHA1_BLOCK_SIZE                 (BYTE_LENGTH(512))
117
118 #define SHA_AUTH_KEY_MAX                SHA256_AUTH_KEY_LENGTH
119 #define SHA_BLOCK_MAX                   SHA256_BLOCK_SIZE
120
121 typedef int (*crypto_func_t)(uint8_t *, uint8_t *, uint64_t,
122                                 uint8_t *, uint8_t *, uint64_t,
123                                 crypto_arg_t *);
124
125 typedef void (*crypto_key_sched_t)(uint8_t *, const uint8_t *);
126
127 /** private data structure for each ARMv8 crypto device */
128 struct armv8_crypto_private {
129         unsigned int max_nb_qpairs;
130         /**< Max number of queue pairs */
131         unsigned int max_nb_sessions;
132         /**< Max number of sessions */
133 };
134
135 /** ARMv8 crypto queue pair */
136 struct armv8_crypto_qp {
137         uint16_t id;
138         /**< Queue Pair Identifier */
139         struct rte_ring *processed_ops;
140         /**< Ring for placing process packets */
141         struct rte_mempool *sess_mp;
142         /**< Session Mempool */
143         struct rte_cryptodev_stats stats;
144         /**< Queue pair statistics */
145         char name[RTE_CRYPTODEV_NAME_LEN];
146         /**< Unique Queue Pair Name */
147 } __rte_cache_aligned;
148
149 /** ARMv8 crypto private session structure */
150 struct armv8_crypto_session {
151         enum armv8_crypto_chain_order chain_order;
152         /**< chain order mode */
153         crypto_func_t crypto_func;
154         /**< cryptographic function to use for this session */
155
156         /** Cipher Parameters */
157         struct {
158                 enum rte_crypto_cipher_operation direction;
159                 /**< cipher operation direction */
160                 enum rte_crypto_cipher_algorithm algo;
161                 /**< cipher algorithm */
162                 int iv_len;
163                 /**< IV length */
164
165                 struct {
166                         uint8_t data[256];
167                         /**< key data */
168                         size_t length;
169                         /**< key length in bytes */
170                 } key;
171
172                 crypto_key_sched_t key_sched;
173                 /**< Key schedule function */
174         } cipher;
175
176         /** Authentication Parameters */
177         struct {
178                 enum rte_crypto_auth_operation operation;
179                 /**< auth operation generate or verify */
180                 enum armv8_crypto_auth_mode mode;
181                 /**< auth operation mode */
182
183                 union {
184                         struct {
185                                 /* Add data if needed */
186                         } auth;
187
188                         struct {
189                                 uint8_t i_key_pad[SHA_BLOCK_MAX]
190                                                         __rte_cache_aligned;
191                                 /**< inner pad (max supported block length) */
192                                 uint8_t o_key_pad[SHA_BLOCK_MAX]
193                                                         __rte_cache_aligned;
194                                 /**< outer pad (max supported block length) */
195                                 uint8_t key[SHA_BLOCK_MAX];
196                                 /**< HMAC key (max supported block length)*/
197                         } hmac;
198                 };
199         } auth;
200
201 } __rte_cache_aligned;
202
203 /** Set and validate ARMv8 crypto session parameters */
204 extern int armv8_crypto_set_session_parameters(
205                 struct armv8_crypto_session *sess,
206                 const struct rte_crypto_sym_xform *xform);
207
208 /** device specific operations function pointer structure */
209 extern struct rte_cryptodev_ops *rte_armv8_crypto_pmd_ops;
210
211 #endif /* _RTE_ARMV8_PMD_PRIVATE_H_ */