2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2008-2009 Semihalf, Piotr Ziecik
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Each SEC channel can hold up to 24 descriptors. All 4 channels can be
35 * simultaneously active holding 96 descriptors. Each descriptor can use 0 or
36 * more link table entries depending of size and granulation of input/output
37 * data. One link table entry is needed for each 65535 bytes of data.
41 #define SEC_TIMEOUT 100000
42 #define SEC_MAX_SESSIONS 256
43 #define SEC_DESCRIPTORS 256 /* Must be power of 2 */
44 #define SEC_LT_ENTRIES 1024 /* Must be power of 2 */
45 #define SEC_MAX_IV_LEN 16
46 #define SEC_MAX_KEY_LEN 64
49 #define SEC_20_ID 0x0000000000000040ULL
50 #define SEC_30_ID 0x0030030000000000ULL
51 #define SEC_31_ID 0x0030030100000000ULL
52 #define SEC_CHANNELS 4
53 #define SEC_POINTERS 7
54 #define SEC_MAX_DMA_BLOCK_SIZE 0xFFFF
55 #define SEC_MAX_FIFO_LEVEL 24
56 #define SEC_DMA_ALIGNMENT 8
58 #define __packed__ __attribute__ ((__packed__))
63 /* SEC descriptor definition */
64 struct sec_hw_desc_ptr {
65 u_int shdp_length : 16;
67 u_int shdp_extent : 7;
69 uint64_t shdp_ptr : 36;
83 u_int __padding1 : 32;
87 u_int __padding0 : 27;
91 u_int __padding2 : 19;
95 struct sec_hw_desc_ptr shd_pointer[SEC_POINTERS];
97 /* Data below is mapped to descriptor pointers */
98 uint8_t shd_iv[SEC_MAX_IV_LEN];
99 uint8_t shd_key[SEC_MAX_KEY_LEN];
100 uint8_t shd_mkey[SEC_MAX_KEY_LEN];
103 #define shd_eu_sel0 shd_control.request.eu_sel0
104 #define shd_mode0 shd_control.request.mode0
105 #define shd_eu_sel1 shd_control.request.eu_sel1
106 #define shd_mode1 shd_control.request.mode1
107 #define shd_desc_type shd_control.request.desc_type
108 #define shd_dir shd_control.request.dir
109 #define shd_dn shd_control.request.dn
110 #define shd_done shd_control.feedback.done
111 #define shd_iccr0 shd_control.feedback.iccr0
112 #define shd_iccr1 shd_control.feedback.iccr1
114 /* SEC link table entries definition */
116 u_int shl_length : 16;
117 u_int __padding0 : 6;
120 u_int __padding1 : 4;
121 uint64_t shl_ptr : 36;
126 bus_addr_t dma_paddr;
127 bus_dma_tag_t dma_tag;
128 bus_dmamap_t dma_map;
133 struct sec_hw_desc *sd_desc;
134 bus_addr_t sd_desc_paddr;
135 struct sec_dma_mem sd_ptr_dmem[SEC_POINTERS];
136 struct cryptop *sd_crp;
142 struct sec_hw_lt *sl_lt;
143 bus_addr_t sl_lt_paddr;
146 struct sec_eu_methods {
147 int (*sem_newsession)(struct sec_softc *sc,
148 struct sec_session *ses, struct cryptoini *enc,
149 struct cryptoini *mac);
150 int (*sem_make_desc)(struct sec_softc *sc,
151 struct sec_session *ses, struct sec_desc *desc,
152 struct cryptop *crp, int buftype);
157 struct sec_eu_methods *ss_eu;
158 uint8_t ss_key[SEC_MAX_KEY_LEN];
159 uint8_t ss_mkey[SEC_MAX_KEY_LEN];
165 struct sec_desc_map_info {
166 struct sec_softc *sdmi_sc;
167 bus_size_t sdmi_size;
168 bus_size_t sdmi_offset;
169 struct sec_lt *sdmi_lt_first;
170 struct sec_lt *sdmi_lt_last;
181 uint64_t sc_int_error_mask;
182 uint64_t sc_channel_idle_mask;
184 struct sec_session sc_sessions[SEC_MAX_SESSIONS];
186 struct mtx sc_controller_lock;
187 struct mtx sc_descriptors_lock;
188 struct mtx sc_sessions_lock;
190 struct sec_desc sc_desc[SEC_DESCRIPTORS];
191 u_int sc_free_desc_get_cnt;
192 u_int sc_free_desc_put_cnt;
193 u_int sc_ready_desc_get_cnt;
194 u_int sc_ready_desc_put_cnt;
195 u_int sc_queued_desc_get_cnt;
196 u_int sc_queued_desc_put_cnt;
198 struct sec_lt sc_lt[SEC_LT_ENTRIES + 1];
199 u_int sc_lt_alloc_cnt;
200 u_int sc_lt_free_cnt;
202 struct sec_dma_mem sc_desc_dmem; /* descriptors DMA memory */
203 struct sec_dma_mem sc_lt_dmem; /* link tables DMA memory */
205 struct resource *sc_rres; /* register resource */
206 int sc_rrid; /* register rid */
209 bus_space_handle_t bsh;
212 struct resource *sc_pri_ires; /* primary irq resource */
213 void *sc_pri_ihand; /* primary irq handler */
214 int sc_pri_irid; /* primary irq resource id */
216 struct resource *sc_sec_ires; /* secondary irq resource */
217 void *sc_sec_ihand; /* secondary irq handler */
218 int sc_sec_irid; /* secondary irq resource id */
222 #define SEC_LOCK(sc, what) \
223 mtx_lock(&(sc)->sc_ ## what ## _lock)
224 #define SEC_UNLOCK(sc, what) \
225 mtx_unlock(&(sc)->sc_ ## what ## _lock)
226 #define SEC_LOCK_ASSERT(sc, what) \
227 mtx_assert(&(sc)->sc_ ## what ## _lock, MA_OWNED)
229 /* Read/Write definitions */
230 #define SEC_READ(sc, reg) \
231 bus_space_read_8((sc)->sc_bas.bst, (sc)->sc_bas.bsh, (reg))
232 #define SEC_WRITE(sc, reg, val) \
233 bus_space_write_8((sc)->sc_bas.bst, (sc)->sc_bas.bsh, (reg), (val))
235 /* Base allocation macros (warning: wrap must be 2^n) */
236 #define SEC_CNT_INIT(sc, cnt, wrap) \
237 (((sc)->cnt) = ((wrap) - 1))
238 #define SEC_ADD(sc, cnt, wrap, val) \
239 ((sc)->cnt = (((sc)->cnt) + (val)) & ((wrap) - 1))
240 #define SEC_INC(sc, cnt, wrap) \
241 SEC_ADD(sc, cnt, wrap, 1)
242 #define SEC_DEC(sc, cnt, wrap) \
243 SEC_ADD(sc, cnt, wrap, -1)
244 #define SEC_GET_GENERIC(sc, tab, cnt, wrap) \
245 ((sc)->tab[SEC_INC(sc, cnt, wrap)])
246 #define SEC_PUT_GENERIC(sc, tab, cnt, wrap, val) \
247 ((sc)->tab[SEC_INC(sc, cnt, wrap)] = val)
249 /* Interface for descriptors */
250 #define SEC_GET_FREE_DESC(sc) \
251 &SEC_GET_GENERIC(sc, sc_desc, sc_free_desc_get_cnt, SEC_DESCRIPTORS)
253 #define SEC_PUT_BACK_FREE_DESC(sc) \
254 SEC_DEC(sc, sc_free_desc_get_cnt, SEC_DESCRIPTORS)
256 #define SEC_DESC_FREE2READY(sc) \
257 SEC_INC(sc, sc_ready_desc_put_cnt, SEC_DESCRIPTORS)
259 #define SEC_GET_READY_DESC(sc) \
260 &SEC_GET_GENERIC(sc, sc_desc, sc_ready_desc_get_cnt, SEC_DESCRIPTORS)
262 #define SEC_PUT_BACK_READY_DESC(sc) \
263 SEC_DEC(sc, sc_ready_desc_get_cnt, SEC_DESCRIPTORS)
265 #define SEC_DESC_READY2QUEUED(sc) \
266 SEC_INC(sc, sc_queued_desc_put_cnt, SEC_DESCRIPTORS)
268 #define SEC_GET_QUEUED_DESC(sc) \
269 &SEC_GET_GENERIC(sc, sc_desc, sc_queued_desc_get_cnt, SEC_DESCRIPTORS)
271 #define SEC_PUT_BACK_QUEUED_DESC(sc) \
272 SEC_DEC(sc, sc_queued_desc_get_cnt, SEC_DESCRIPTORS)
274 #define SEC_DESC_QUEUED2FREE(sc) \
275 SEC_INC(sc, sc_free_desc_put_cnt, SEC_DESCRIPTORS)
277 #define SEC_FREE_DESC_CNT(sc) \
278 (((sc)->sc_free_desc_put_cnt - (sc)->sc_free_desc_get_cnt - 1) \
279 & (SEC_DESCRIPTORS - 1))
281 #define SEC_READY_DESC_CNT(sc) \
282 (((sc)->sc_ready_desc_put_cnt - (sc)->sc_ready_desc_get_cnt) & \
283 (SEC_DESCRIPTORS - 1))
285 #define SEC_QUEUED_DESC_CNT(sc) \
286 (((sc)->sc_queued_desc_put_cnt - (sc)->sc_queued_desc_get_cnt) \
287 & (SEC_DESCRIPTORS - 1))
289 #define SEC_DESC_SYNC(sc, mode) do { \
290 sec_sync_dma_mem(&((sc)->sc_desc_dmem), (mode)); \
291 sec_sync_dma_mem(&((sc)->sc_lt_dmem), (mode)); \
294 #define SEC_DESC_SYNC_POINTERS(desc, mode) do { \
296 for (i = 0; i < SEC_POINTERS; i++) \
297 sec_sync_dma_mem(&((desc)->sd_ptr_dmem[i]), (mode)); \
300 #define SEC_DESC_FREE_POINTERS(desc) do { \
302 for (i = 0; i < SEC_POINTERS; i++) \
303 sec_free_dma_mem(&(desc)->sd_ptr_dmem[i]); \
306 #define SEC_DESC_PUT_BACK_LT(sc, desc) \
307 SEC_PUT_BACK_LT(sc, (desc)->sd_lt_used)
309 #define SEC_DESC_FREE_LT(sc, desc) \
310 SEC_FREE_LT(sc, (desc)->sd_lt_used)
312 /* Interface for link tables */
313 #define SEC_ALLOC_LT_ENTRY(sc) \
314 &SEC_GET_GENERIC(sc, sc_lt, sc_lt_alloc_cnt, SEC_LT_ENTRIES)
316 #define SEC_PUT_BACK_LT(sc, num) \
317 SEC_ADD(sc, sc_lt_alloc_cnt, SEC_LT_ENTRIES, -(num))
319 #define SEC_FREE_LT(sc, num) \
320 SEC_ADD(sc, sc_lt_free_cnt, SEC_LT_ENTRIES, num)
322 #define SEC_FREE_LT_CNT(sc) \
323 (((sc)->sc_lt_free_cnt - (sc)->sc_lt_alloc_cnt - 1) \
324 & (SEC_LT_ENTRIES - 1))
326 /* DMA Maping defines */
331 /* Size of SEC registers area */
332 #define SEC_IO_SIZE 0x10000
334 /* SEC Controller registers */
335 #define SEC_IER 0x1008
336 #define SEC_INT_CH_DN(n) (1ULL << (((n) * 2) + 32))
337 #define SEC_INT_CH_ERR(n) (1ULL << (((n) * 2) + 33))
338 #define SEC_INT_ITO (1ULL << 55)
340 #define SEC_ISR 0x1010
341 #define SEC_ICR 0x1018
342 #define SEC_ID 0x1020
344 #define SEC_EUASR 0x1028
345 #define SEC_EUASR_RNGU(r) (((r) >> 0) & 0xF)
346 #define SEC_EUASR_PKEU(r) (((r) >> 8) & 0xF)
347 #define SEC_EUASR_KEU(r) (((r) >> 16) & 0xF)
348 #define SEC_EUASR_CRCU(r) (((r) >> 20) & 0xF)
349 #define SEC_EUASR_DEU(r) (((r) >> 32) & 0xF)
350 #define SEC_EUASR_AESU(r) (((r) >> 40) & 0xF)
351 #define SEC_EUASR_MDEU(r) (((r) >> 48) & 0xF)
352 #define SEC_EUASR_AFEU(r) (((r) >> 56) & 0xF)
354 #define SEC_MCR 0x1030
355 #define SEC_MCR_SWR (1ULL << 32)
357 /* SEC Channel registers */
358 #define SEC_CHAN_CCR(n) (((n) * 0x100) + 0x1108)
359 #define SEC_CHAN_CCR_CDIE (1ULL << 1)
360 #define SEC_CHAN_CCR_NT (1ULL << 2)
361 #define SEC_CHAN_CCR_AWSE (1ULL << 3)
362 #define SEC_CHAN_CCR_CDWE (1ULL << 4)
363 #define SEC_CHAN_CCR_BS (1ULL << 8)
364 #define SEC_CHAN_CCR_WGN (1ULL << 13)
365 #define SEC_CHAN_CCR_R (1ULL << 32)
366 #define SEC_CHAN_CCR_CON (1ULL << 33)
368 #define SEC_CHAN_CSR(n) (((n) * 0x100) + 0x1110)
369 #define SEC_CHAN_CSR2_FFLVL_M 0x1FULL
370 #define SEC_CHAN_CSR2_FFLVL_S 56
371 #define SEC_CHAN_CSR2_GSTATE_M 0x0FULL
372 #define SEC_CHAN_CSR2_GSTATE_S 48
373 #define SEC_CHAN_CSR2_PSTATE_M 0x0FULL
374 #define SEC_CHAN_CSR2_PSTATE_S 40
375 #define SEC_CHAN_CSR2_MSTATE_M 0x3FULL
376 #define SEC_CHAN_CSR2_MSTATE_S 32
377 #define SEC_CHAN_CSR3_FFLVL_M 0x1FULL
378 #define SEC_CHAN_CSR3_FFLVL_S 24
379 #define SEC_CHAN_CSR3_MSTATE_M 0x1FFULL
380 #define SEC_CHAN_CSR3_MSTATE_S 32
381 #define SEC_CHAN_CSR3_PSTATE_M 0x7FULL
382 #define SEC_CHAN_CSR3_PSTATE_S 48
383 #define SEC_CHAN_CSR3_GSTATE_M 0x7FULL
384 #define SEC_CHAN_CSR3_GSTATE_S 56
386 #define SEC_CHAN_CDPR(n) (((n) * 0x100) + 0x1140)
387 #define SEC_CHAN_FF(n) (((n) * 0x100) + 0x1148)
389 /* SEC Execution Units numbers */
390 #define SEC_EU_NONE 0x0
391 #define SEC_EU_AFEU 0x1
392 #define SEC_EU_DEU 0x2
393 #define SEC_EU_MDEU_A 0x3
394 #define SEC_EU_MDEU_B 0xB
395 #define SEC_EU_RNGU 0x4
396 #define SEC_EU_PKEU 0x5
397 #define SEC_EU_AESU 0x6
398 #define SEC_EU_KEU 0x7
399 #define SEC_EU_CRCU 0x8
401 /* SEC descriptor types */
402 #define SEC_DT_COMMON_NONSNOOP 0x02
403 #define SEC_DT_HMAC_SNOOP 0x04
405 /* SEC AESU declarations and definitions */
406 #define SEC_AESU_MODE_ED (1ULL << 0)
407 #define SEC_AESU_MODE_CBC (1ULL << 1)
409 /* SEC DEU declarations and definitions */
410 #define SEC_DEU_MODE_ED (1ULL << 0)
411 #define SEC_DEU_MODE_TS (1ULL << 1)
412 #define SEC_DEU_MODE_CBC (1ULL << 2)
414 /* SEC MDEU declarations and definitions */
415 #define SEC_HMAC_HASH_LEN 12
416 #define SEC_MDEU_MODE_SHA1 0x00 /* MDEU A */
417 #define SEC_MDEU_MODE_SHA384 0x00 /* MDEU B */
418 #define SEC_MDEU_MODE_SHA256 0x01
419 #define SEC_MDEU_MODE_MD5 0x02 /* MDEU A */
420 #define SEC_MDEU_MODE_SHA512 0x02 /* MDEU B */
421 #define SEC_MDEU_MODE_SHA224 0x03
422 #define SEC_MDEU_MODE_PD (1ULL << 2)
423 #define SEC_MDEU_MODE_HMAC (1ULL << 3)
424 #define SEC_MDEU_MODE_INIT (1ULL << 4)
425 #define SEC_MDEU_MODE_SMAC (1ULL << 5)
426 #define SEC_MDEU_MODE_CICV (1ULL << 6)
427 #define SEC_MDEU_MODE_CONT (1ULL << 7)