d02992a6481ecaa3aaf80c9783fad0e8f8718969
[deb_dpdk.git] / drivers / crypto / armv8 / rte_armv8_pmd_private.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 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, Inc 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 CRYPTODEV_NAME_ARMV8_PMD        crypto_armv8
37 /**< ARMv8 Crypto PMD device name */
38
39 #define ARMV8_CRYPTO_LOG_ERR(fmt, args...) \
40         RTE_LOG(ERR, CRYPTODEV, "[%s] %s() line %u: " fmt "\n",  \
41                         RTE_STR(CRYPTODEV_NAME_ARMV8_CRYPTO_PMD), \
42                         __func__, __LINE__, ## args)
43
44 #ifdef RTE_LIBRTE_ARMV8_CRYPTO_DEBUG
45 #define ARMV8_CRYPTO_LOG_INFO(fmt, args...) \
46         RTE_LOG(INFO, CRYPTODEV, "[%s] %s() line %u: " fmt "\n", \
47                         RTE_STR(CRYPTODEV_NAME_ARMV8_CRYPTO_PMD), \
48                         __func__, __LINE__, ## args)
49
50 #define ARMV8_CRYPTO_LOG_DBG(fmt, args...) \
51         RTE_LOG(DEBUG, CRYPTODEV, "[%s] %s() line %u: " fmt "\n", \
52                         RTE_STR(CRYPTODEV_NAME_ARMV8_CRYPTO_PMD), \
53                         __func__, __LINE__, ## args)
54
55 #define ARMV8_CRYPTO_ASSERT(con)                                \
56 do {                                                            \
57         if (!(con)) {                                           \
58                 rte_panic("%s(): "                              \
59                     con "condition failed, line %u", __func__); \
60         }                                                       \
61 } while (0)
62
63 #else
64 #define ARMV8_CRYPTO_LOG_INFO(fmt, args...)
65 #define ARMV8_CRYPTO_LOG_DBG(fmt, args...)
66 #define ARMV8_CRYPTO_ASSERT(con)
67 #endif
68
69 #define NBBY            8               /* Number of bits in a byte */
70 #define BYTE_LENGTH(x)  ((x) / NBBY)    /* Number of bytes in x (round down) */
71
72 /** ARMv8 operation order mode enumerator */
73 enum armv8_crypto_chain_order {
74         ARMV8_CRYPTO_CHAIN_CIPHER_AUTH,
75         ARMV8_CRYPTO_CHAIN_AUTH_CIPHER,
76         ARMV8_CRYPTO_CHAIN_NOT_SUPPORTED,
77         ARMV8_CRYPTO_CHAIN_LIST_END = ARMV8_CRYPTO_CHAIN_NOT_SUPPORTED
78 };
79
80 /** ARMv8 cipher operation enumerator */
81 enum armv8_crypto_cipher_operation {
82         ARMV8_CRYPTO_CIPHER_OP_ENCRYPT = RTE_CRYPTO_CIPHER_OP_ENCRYPT,
83         ARMV8_CRYPTO_CIPHER_OP_DECRYPT = RTE_CRYPTO_CIPHER_OP_DECRYPT,
84         ARMV8_CRYPTO_CIPHER_OP_NOT_SUPPORTED,
85         ARMV8_CRYPTO_CIPHER_OP_LIST_END = ARMV8_CRYPTO_CIPHER_OP_NOT_SUPPORTED
86 };
87
88 enum armv8_crypto_cipher_keylen {
89         ARMV8_CRYPTO_CIPHER_KEYLEN_128,
90         ARMV8_CRYPTO_CIPHER_KEYLEN_192,
91         ARMV8_CRYPTO_CIPHER_KEYLEN_256,
92         ARMV8_CRYPTO_CIPHER_KEYLEN_NOT_SUPPORTED,
93         ARMV8_CRYPTO_CIPHER_KEYLEN_LIST_END =
94                 ARMV8_CRYPTO_CIPHER_KEYLEN_NOT_SUPPORTED
95 };
96
97 /** ARMv8 auth mode enumerator */
98 enum armv8_crypto_auth_mode {
99         ARMV8_CRYPTO_AUTH_AS_AUTH,
100         ARMV8_CRYPTO_AUTH_AS_HMAC,
101         ARMV8_CRYPTO_AUTH_AS_CIPHER,
102         ARMV8_CRYPTO_AUTH_NOT_SUPPORTED,
103         ARMV8_CRYPTO_AUTH_LIST_END = ARMV8_CRYPTO_AUTH_NOT_SUPPORTED
104 };
105
106 #define CRYPTO_ORDER_MAX                ARMV8_CRYPTO_CHAIN_LIST_END
107 #define CRYPTO_CIPHER_OP_MAX            ARMV8_CRYPTO_CIPHER_OP_LIST_END
108 #define CRYPTO_CIPHER_KEYLEN_MAX        ARMV8_CRYPTO_CIPHER_KEYLEN_LIST_END
109 #define CRYPTO_CIPHER_MAX               RTE_CRYPTO_CIPHER_LIST_END
110 #define CRYPTO_AUTH_MAX                 RTE_CRYPTO_AUTH_LIST_END
111
112 #define HMAC_IPAD_VALUE                 (0x36)
113 #define HMAC_OPAD_VALUE                 (0x5C)
114
115 #define SHA256_AUTH_KEY_LENGTH          (BYTE_LENGTH(256))
116 #define SHA256_BLOCK_SIZE               (BYTE_LENGTH(512))
117
118 #define SHA1_AUTH_KEY_LENGTH            (BYTE_LENGTH(160))
119 #define SHA1_BLOCK_SIZE                 (BYTE_LENGTH(512))
120
121 #define SHA_AUTH_KEY_MAX                SHA256_AUTH_KEY_LENGTH
122 #define SHA_BLOCK_MAX                   SHA256_BLOCK_SIZE
123
124 typedef int (*crypto_func_t)(uint8_t *, uint8_t *, uint64_t,
125                                 uint8_t *, uint8_t *, uint64_t,
126                                 crypto_arg_t *);
127
128 typedef void (*crypto_key_sched_t)(uint8_t *, const uint8_t *);
129
130 /** private data structure for each ARMv8 crypto device */
131 struct armv8_crypto_private {
132         unsigned int max_nb_qpairs;
133         /**< Max number of queue pairs */
134         unsigned int max_nb_sessions;
135         /**< Max number of sessions */
136 };
137
138 /** ARMv8 crypto queue pair */
139 struct armv8_crypto_qp {
140         uint16_t id;
141         /**< Queue Pair Identifier */
142         struct rte_ring *processed_ops;
143         /**< Ring for placing process packets */
144         struct rte_mempool *sess_mp;
145         /**< Session Mempool */
146         struct rte_cryptodev_stats stats;
147         /**< Queue pair statistics */
148         char name[RTE_CRYPTODEV_NAME_LEN];
149         /**< Unique Queue Pair Name */
150 } __rte_cache_aligned;
151
152 /** ARMv8 crypto private session structure */
153 struct armv8_crypto_session {
154         enum armv8_crypto_chain_order chain_order;
155         /**< chain order mode */
156         crypto_func_t crypto_func;
157         /**< cryptographic function to use for this session */
158
159         /** Cipher Parameters */
160         struct {
161                 enum rte_crypto_cipher_operation direction;
162                 /**< cipher operation direction */
163                 enum rte_crypto_cipher_algorithm algo;
164                 /**< cipher algorithm */
165                 struct {
166                         uint16_t length;
167                         uint16_t offset;
168                 } iv;
169                 /**< IV parameters */
170
171                 struct {
172                         uint8_t data[256];
173                         /**< key data */
174                         size_t length;
175                         /**< key length in bytes */
176                 } key;
177
178                 crypto_key_sched_t key_sched;
179                 /**< Key schedule function */
180         } cipher;
181
182         /** Authentication Parameters */
183         struct {
184                 enum rte_crypto_auth_operation operation;
185                 /**< auth operation generate or verify */
186                 enum armv8_crypto_auth_mode mode;
187                 /**< auth operation mode */
188
189                 union {
190                         struct {
191                                 /* Add data if needed */
192                         } auth;
193
194                         struct {
195                                 uint8_t i_key_pad[SHA_BLOCK_MAX]
196                                                         __rte_cache_aligned;
197                                 /**< inner pad (max supported block length) */
198                                 uint8_t o_key_pad[SHA_BLOCK_MAX]
199                                                         __rte_cache_aligned;
200                                 /**< outer pad (max supported block length) */
201                                 uint8_t key[SHA_BLOCK_MAX];
202                                 /**< HMAC key (max supported block length)*/
203                         } hmac;
204                 };
205                 uint16_t digest_length;
206                 /* Digest length */
207         } auth;
208
209 } __rte_cache_aligned;
210
211 /** Set and validate ARMv8 crypto session parameters */
212 extern int armv8_crypto_set_session_parameters(
213                 struct armv8_crypto_session *sess,
214                 const struct rte_crypto_sym_xform *xform);
215
216 /** device specific operations function pointer structure */
217 extern struct rte_cryptodev_ops *rte_armv8_crypto_pmd_ops;
218
219 #endif /* _RTE_ARMV8_PMD_PRIVATE_H_ */