1/*
2 **************************************************************************
3 * Copyright (c) 2014,2015, The Linux Foundation. All rights reserved.
4 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 **************************************************************************
15 */
16
17/*
18 * nss_crypto.h
19 *	NSS to HLOS Crypto interface definitions.
20 */
21
22#ifndef __NSS_CRYPTO_H
23#define __NSS_CRYPTO_H
24
25#define NSS_CRYPTO_MAX_IDXS 16			/**< Max supported sessions */
26#define NSS_CRYPTO_MAX_ENGINES 4		/**< Max engines available */
27#define NSS_CRYPTO_BAM_PP 4			/**< BAM Pipe Pairs */
28
29/**
30 * @brief hash sizes supported by H/W.
31 */
32enum nss_crypto_hash {
33	NSS_CRYPTO_HASH_SHA96 = 12,		/**< 96-bit hash size */
34	NSS_CRYPTO_HASH_SHA128 = 16,		/**< 128-bit hash size */
35	NSS_CRYPTO_HASH_SHA160 = 20,		/**< 160-bit hash size */
36	NSS_CRYPTO_HASH_SHA256 = 32		/**< 256-bit hash size */
37};
38
39/**
40 * @brief supported cipher algorithms
41 */
42enum nss_crypto_cipher {
43	NSS_CRYPTO_CIPHER_NONE = 0,		/**< Cipher not required*/
44	NSS_CRYPTO_CIPHER_AES,			/**< AES, CBC for 128-bit & 256-bit key sizes*/
45	NSS_CRYPTO_CIPHER_DES,			/**< DES, CBC for 64-bit key size */
46	NSS_CRYPTO_CIPHER_NULL,			/**< NULL, CBC */
47	NSS_CRYPTO_CIPHER_MAX
48};
49
50/**
51 * @brief supported authentication algorithms
52 */
53enum nss_crypto_auth {
54	NSS_CRYPTO_AUTH_NONE = 0,		/**< Authentication not required */
55	NSS_CRYPTO_AUTH_SHA1_HMAC,		/**< SHA1_HMAC,160-bit key */
56	NSS_CRYPTO_AUTH_SHA256_HMAC,		/**< SHA256_HMAC,256-bit key */
57	NSS_CRYPTO_AUTH_NULL,			/**< NULL Authentication */
58	NSS_CRYPTO_AUTH_MAX
59};
60
61/**
62 * @brief sync types supported.
63 */
64enum nss_crypto_msg_type {
65	NSS_CRYPTO_MSG_TYPE_NONE = 0,		/**< sync type none */
66	NSS_CRYPTO_MSG_TYPE_OPEN_ENG = 1,	/**< open engine sync */
67	NSS_CRYPTO_MSG_TYPE_CLOSE_ENG = 2,	/**< close engine sync */
68	NSS_CRYPTO_MSG_TYPE_RESET_SESSION = 3,	/**< reset session */
69	NSS_CRYPTO_MSG_TYPE_STATS = 4,		/**< stats sync */
70	NSS_CRYPTO_MSG_TYPE_MAX
71};
72
73/*
74 * @brief Crypto Response types
75 */
76enum nss_crypto_msg_error {
77	NSS_CRYPTO_MSG_ERROR_NONE = 0,
78	NSS_CRYPTO_MSG_ERROR_INVAL_ENG = 1,	/**< invalid engine id */
79	NSS_CRYPTO_MSG_ERROR_UNSUPP_OP = 2,	/**< unsupported operation type */
80	NSS_CRYPTO_MSG_ERROR_INVAL_OP = 3,	/**< invalid operation type */
81	NSS_CRYPTO_MSG_ERROR_MAX
82};
83
84/**
85 * @brief session states
86 */
87enum nss_crypto_session_state {
88	NSS_CRYPTO_SESSION_STATE_NONE = 0,	/**< session state none */
89	NSS_CRYPTO_SESSION_STATE_ACTIVE = 1,	/**< session state is active */
90	NSS_CRYPTO_SESSION_STATE_FREE = 2	/**< session state is free */
91};
92
93/**
94 * @brief crypto origin
95 */
96enum nss_crypto_buf_origin {
97	NSS_CRYPTO_BUF_ORIGIN_HOST = 0x001,		/**< request originated from host */
98	NSS_CRYPTO_BUF_ORIGIN_NSS = 0x0002,		/**< request originates from nss fast path */
99};
100
101/*
102 * @brief crypto session index type
103 */
104struct nss_crypto_idx {
105	uint16_t pp_num;	/**< pipe pair index */
106	uint16_t cmd_len;	/**< command block length to program */
107	uint32_t cblk_paddr;	/**< phy_addr of the command block */
108};
109
110/*
111 * @brief Command sent for opening the engine from host, this is called to
112 * 	  initialize the Crypto NSS engine specific data structures. Ideally
113 * 	  host can send a single probe for all engines but current implementation
114 * 	  relies on probe per engine
115 */
116struct nss_crypto_config_eng {
117	uint32_t eng_id;				/**< engine number to open */
118	uint32_t bam_pbase;				/**< BAM base addr (physical) */
119	uint32_t crypto_pbase;				/**< Crypto base addr (physical) */
120	uint32_t desc_paddr[NSS_CRYPTO_BAM_PP];		/**< pipe desc addr (physical) */
121	struct nss_crypto_idx idx[NSS_CRYPTO_MAX_IDXS];	/**< allocated session indexes */
122};
123
124/*
125 * @brief Reset session related state.
126 */
127struct nss_crypto_config_session {
128	uint32_t idx;				/**< session idx on which will be reset */
129	uint32_t state;				/**< session idx state */
130};
131
132/*
133 * @brief crypto statistics
134 */
135struct nss_crypto_stats {
136	uint32_t queued;	/**< number of frames waiting to be processed*/
137	uint32_t completed;	/**< number of frames processed */
138	uint32_t dropped;	/**< number of frames dropped or not processed */
139};
140
141/**
142 * @brief stats structure synced to HOST
143 */
144struct nss_crypto_sync_stats {
145	struct nss_crypto_stats eng_stats[NSS_CRYPTO_MAX_ENGINES];	/**< Engine stats */
146	struct nss_crypto_stats idx_stats[NSS_CRYPTO_MAX_IDXS];		/**< Session stats */
147	struct nss_crypto_stats total;				/**< Total crypto stats */
148};
149
150/*
151 * @brief Config message.
152 */
153struct nss_crypto_msg {
154	struct nss_cmn_msg cm;					/**< message header */
155	union {
156		struct nss_crypto_config_eng eng;		/**< open engine msg structure */
157		struct nss_crypto_config_session session;	/**< reset stats msg structure */
158		struct nss_crypto_sync_stats stats;		/**< statistics sync */
159	} msg;
160};
161
162#ifdef __KERNEL__  /* only kernel will use */
163
164/**
165 * @brief Message notification callback
166 *
167 * @param app_data[IN] context of the callback user
168 * @param msg[IN] notification event data
169 *
170 * @return
171 */
172typedef void (*nss_crypto_msg_callback_t)(void *app_data, struct nss_crypto_msg *msg);
173
174/**
175 * @brief data callback
176 *
177 * @param app_data[IN] context of the callback user
178 * @param buf[IN] crypto data buffer
179 *
180 * @return
181 */
182typedef void (*nss_crypto_buf_callback_t)(void *app_data, void *buf, uint32_t paddr, uint16_t len);
183
184/**
185 * @brief send an Crypto message
186 *
187 * @param nss_ctx[IN] NSS HLOS driver's context
188 * @param msg[IN] control message
189 *
190 * @return
191 */
192extern nss_tx_status_t nss_crypto_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_crypto_msg *msg);
193
194/**
195 * @brief Send a crypto data
196 *
197 * @param nss_ctx[IN] HLOS driver's context
198 * @param buf[IN] buffer pointer (this is a generic/opaque buffer pointer)
199 * @param buf_paddr[IN] phyical address of the buffer
200 * @param len[IN] length of the buffer data
201 *
202 * @return
203 */
204extern nss_tx_status_t nss_crypto_tx_buf(struct nss_ctx_instance *nss_ctx, void *buf, uint32_t buf_paddr, uint16_t len);
205
206/**
207 * @brief register a event callback handler with HLOS driver
208 *
209 * @param cb[IN] event callback function
210 * @param app_data[IN] context of the callback user
211 *
212 * @return
213 */
214extern struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data);
215
216/**
217 * @brief register a data callback handler with HLOS driver
218 *
219 * @param cb[IN] data callback function
220 * @param app_data[IN] conext of the callback user
221 *
222 * @return
223 */
224extern struct nss_ctx_instance *nss_crypto_data_register(nss_crypto_buf_callback_t cb, void *app_data);
225
226/**
227 * @brief unregister the message notifier
228 *
229 * @param ctx[IN] HLOS driver's context
230 *
231 * @return
232 */
233extern void nss_crypto_notify_unregister(struct nss_ctx_instance *ctx);
234
235/**
236 * @brief unregister the data notifier
237 *
238 * @param ctx[IN] HLOS driver's context
239 *
240 * @return
241 */
242extern void nss_crypto_data_unregister(struct nss_ctx_instance *ctx);
243
244/**
245 * @brief crypto specific message init
246 *	Initialize crypto specific message
247 *
248 * @return
249 */
250extern void nss_crypto_msg_init(struct nss_crypto_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len,
251				nss_crypto_msg_callback_t cb, void *app_data);
252
253#endif /*__KERNEL__ */
254#endif /* __NSS_CRYPTO_H */
255