xref: /titanic_50/usr/src/uts/common/sys/crypto/dca.h (revision 0209230bf1261579beab4f55226bb509e6b850cb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_CRYPTO_DCA_H
28 #define	_SYS_CRYPTO_DCA_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/types.h>
37 #include <sys/varargs.h>
38 
39 #include <sys/crypto/spi.h>
40 
41 /*
42  * Deimos - cryptographic acceleration based upon Broadcom 582x.
43  *
44  * Note: Everything in this file is private to the Deimos device
45  *	 driver!  Do not include this in any other file.
46  */
47 
48 #define	DRIVER			"dca"
49 #define	DCA_MANUFACTURER_ID	"SUNWdca"
50 
51 #ifdef _KERNEL
52 
53 /*
54  * Tunables.
55  */
56 #define	MCR1LOWATER	16	/* these numbers favor overall throughput */
57 #define	MCR1HIWATER	24
58 #define	MCR1MAXREQS	8
59 #define	MCR2LOWATER	16
60 #define	MCR2HIWATER	24
61 #define	MCR2MAXREQS	4
62 #define	MAXMCR		2	/* there are 2 mcrs */
63 #define	MAXREQSPERMCR	16	/* there are 4 subunits serviced by MCR2 */
64 #define	MAXFRAGS	6	/* Limit on the number of fragments */
65 #define	MAXWORK		6	/* How many work structures to preallocate */
66 
67 /*
68  * These are constants.  Do not change them.
69  */
70 #if defined(i386) || defined(__i386) || defined(__amd64)
71 #define	MAXPACKET	0xefff	/* rootnex INT_MAX_BUF hack. */
72 #else
73 #define	MAXPACKET	0xffff	/* Max size of a packet or fragment */
74 #endif
75 #define	DESBLOCK	8	/* Size of a DES or 3DES block */
76 #define	DSAPARTLEN	20	/* Size of fixed DSA parts (r, s, q, x, v) */
77 #define	DSASIGLEN	40	/* Size of a DSA signature */
78 #define	SHA1LEN		20	/* Size of a SHA1 hash */
79 #define	SECOND		1000000	/* One second in usec */
80 #define	MSEC		1000	/* One millisecond in usec */
81 #define	DES_KEYSIZE	8
82 #define	DES_IV_LEN	8
83 #define	DES3_KEYSIZE	(3 * DES_KEYSIZE)
84 
85 /*
86  * Mechanism info structure passed to KCF during registration.
87  */
88 
89 #define	MD5_HMAC_BLOCK_SIZE	64	/* MD5-HMAC block size */
90 #define	MD5_HMAC_MIN_KEY_LEN	1	/* MD5-HMAC min key length in bytes */
91 #define	MD5_HMAC_MAX_KEY_LEN	64	/* MD5-HMAC max key length in bytes */
92 
93 #define	SHA1_HMAC_BLOCK_SIZE	64	/* SHA1-HMAC block size */
94 #define	SHA1_HMAC_MIN_KEY_LEN	1	/* SHA1-HMAC min key length in bytes */
95 #define	SHA1_HMAC_MAX_KEY_LEN	64	/* SHA1-HMAC max key length in bytes */
96 
97 #define	DES_KEY_LEN		8	/* DES key length in bytes */
98 #define	DES3_KEY_LEN		24	/* 3DES key length in bytes */
99 
100 #define	DSA_MIN_KEY_LEN		64	/* DSA min key length in bytes */
101 #define	DSA_MAX_KEY_LEN		128	/* DSA max key length in bytes */
102 
103 #define	RSA_MIN_KEY_LEN		32	/* RSA min key length in bytes */
104 #define	RSA_MAX_KEY_LEN		256	/* RSA max key length in bytes */
105 
106 /*
107  * RSA implementation.
108  */
109 
110 #define	DCA_RSA_ENC	0
111 #define	DCA_RSA_DEC	1
112 #define	DCA_RSA_SIGN	2
113 #define	DCA_RSA_VRFY	3
114 #define	DCA_RSA_SIGNR	4
115 #define	DCA_RSA_VRFYR	5
116 
117 /*
118  * DSA implementation.
119  */
120 
121 #define	DCA_DSA_SIGN	0
122 #define	DCA_DSA_VRFY	1
123 
124 /*
125  * FMA eclass index definitions. Note that this enum must be consistent
126  * with the dca_fma_eclass_sca1000 and dca_fma_eclass_sca500 string arrays.
127  */
128 typedef enum dca_fma_eclass {
129 	DCA_FM_ECLASS_HW_DEVICE = 0,
130 	DCA_FM_ECLASS_HW_TIMEOUT,
131 	DCA_FM_ECLASS_NONE
132 } dca_fma_eclass_t;
133 
134 /*
135  * Forward typedefs.
136  */
137 typedef struct dca dca_t;
138 typedef struct dca_chain dca_chain_t;
139 typedef struct dca_listnode dca_listnode_t;
140 typedef struct dca_worklist dca_worklist_t;
141 typedef struct dca_work dca_work_t;
142 typedef struct dca_request dca_request_t;
143 typedef struct dca_stat dca_stat_t;
144 typedef struct dca_cookie dca_cookie_t;
145 typedef struct dca_device dca_device_t;
146 
147 /*
148  * This structure is used to identify a specific board.
149  */
150 struct dca_device {
151 	ushort_t		dd_vendor_id;
152 	ushort_t		dd_device_id;
153 	char			*dd_model;
154 };
155 
156 /*
157  * Structure representing a node in a DMA chain.  (Broadcom calls
158  * these "Data Buffer Chain Entries".)
159  *
160  * note, this structure must be a multiple of sizeof (intptr_t)
161  */
162 struct dca_chain {
163 	/* the descriptor */
164 	caddr_t			dc_desc_kaddr;
165 	/* and the buffer to which it points */
166 	size_t			dc_buffer_length;
167 	ddi_dma_handle_t	dc_buffer_dmah;
168 	caddr_t			dc_buffer_kaddr;
169 	/* physical addresses */
170 	uint32_t		dc_desc_paddr;
171 	uint32_t		dc_buffer_paddr;
172 	uint32_t		dc_next_paddr;
173 };
174 
175 /*
176  * Linked-list linkage.
177  */
178 struct dca_listnode {
179 	dca_listnode_t		*dl_next;
180 	dca_listnode_t		*dl_prev;
181 	dca_listnode_t		*dl_next2;
182 	dca_listnode_t		*dl_prev2;
183 };
184 
185 typedef enum dca_mech_type {
186 	DES_CBC_MECH_INFO_TYPE,		/* SUN_CKM_DES_CBC */
187 	DES3_CBC_MECH_INFO_TYPE,	/* SUN_CKM_DES3_CBC */
188 	DSA_MECH_INFO_TYPE,		/* SUN_CKM_DSA */
189 	RSA_X_509_MECH_INFO_TYPE,	/* SUN_CKM_RSA_X_509 */
190 	RSA_PKCS_MECH_INFO_TYPE		/* SUN_CKM_RSA_PKCS */
191 } dca_mech_type_t;
192 
193 #define	SUN_CKM_DSA			"CKM_DSA"
194 
195 struct dca_rng {
196 	uint32_t		dr_chunklen;
197 };
198 
199 union dca_parameters {
200 	struct dca_rng		dp_rng;
201 };
202 
203 typedef struct dca_ctx {
204 	/*
205 	 * The following are context fields for Deimos 2.0.
206 	 */
207 	crypto_mech_type_t	ctx_cm_type;	/* Mechanism type */
208 	int			mode;		/* Mode of operation */
209 	int 			atomic;		/* Boolean */
210 
211 	/* Fields for RSA and DSA */
212 	uchar_t			*mod;		/* RSA modulus */
213 	unsigned		modlen;		/* RSA modulus length */
214 	unsigned		pqfix;		/* RSA flag */
215 
216 	/* Fields for DES and 3DES */
217 	uint32_t		iv[2];
218 	uint32_t		key[6];
219 	int			residlen;
220 	uchar_t			resid[DESBLOCK];
221 	int			activeresidlen;
222 	uchar_t			activeresid[DESBLOCK];
223 	crypto_data_t		in_dup;		/* input data duplicate */
224 } dca_ctx_t;
225 
226 /*
227  * Work structure.  One of these per actual job submitted to an MCR.
228  * Contains everything we need to submit the job, and everything we
229  * need to notify caller and release resources when the completion
230  * interrupt comes.
231  */
232 struct dca_request {
233 	dca_listnode_t		dr_linkage;
234 	uint16_t		dr_pkt_length;
235 	crypto_req_handle_t	dr_kcf_req;
236 	dca_t			*dr_dca;
237 	dca_worklist_t		*dr_wlp;
238 	/*
239 	 * Consumer's I/O buffers.
240 	 */
241 	crypto_data_t		*dr_in;
242 	crypto_data_t		*dr_out;
243 	dca_ctx_t		dr_ctx;
244 	/*
245 	 * Chains and DMA structures.
246 	 */
247 	size_t			dr_dma_size;
248 	uint32_t		dr_ctx_paddr;
249 	caddr_t			dr_ctx_kaddr;
250 	ddi_acc_handle_t	dr_ctx_acch;
251 	ddi_dma_handle_t	dr_ctx_dmah;
252 	/*
253 	 * Scratch input buffer.
254 	 */
255 	ddi_acc_handle_t	dr_ibuf_acch;
256 	ddi_dma_handle_t	dr_ibuf_dmah;
257 	caddr_t			dr_ibuf_kaddr;
258 	uint32_t		dr_ibuf_paddr;
259 
260 	/*
261 	 * Scratch output buffer.
262 	 */
263 	ddi_acc_handle_t	dr_obuf_acch;
264 	ddi_dma_handle_t	dr_obuf_dmah;
265 	caddr_t			dr_obuf_kaddr;
266 	uint32_t		dr_obuf_paddr;
267 
268 	/*
269 	 * Values to program MCR with.
270 	 */
271 	uint32_t		dr_in_paddr;
272 	uint32_t		dr_out_paddr;
273 	uint32_t		dr_in_next;
274 	uint32_t		dr_out_next;
275 	uint16_t		dr_in_len;
276 	uint16_t		dr_out_len;
277 	/*
278 	 * Callback.
279 	 */
280 	void			(*dr_callback)(dca_request_t *, int);
281 	/*
282 	 * Other stuff.
283 	 */
284 	uint32_t		dr_flags;
285 	/*
286 	 * Algorithm specific parameters.
287 	 */
288 	void			*dr_context;
289 	union dca_parameters	dr_param;
290 	/*
291 	 * Statistics.
292 	 */
293 	int			dr_job_stat;
294 	int			dr_byte_stat;
295 
296 	/* Pre-mapped input and output data buffer chain support */
297 	dca_chain_t		dr_ibuf_head;
298 	dca_chain_t		dr_obuf_head;
299 
300 	/*
301 	 * User buffers are mapped to DMA handles dynamically. The physically
302 	 * contigous blocks ( >= a page) are built into a data buffer chain.
303 	 */
304 	dca_chain_t		dr_chain_in_head;
305 	ddi_dma_handle_t	dr_chain_in_dmah;
306 
307 	dca_chain_t		dr_chain_out_head;
308 	ddi_dma_handle_t	dr_chain_out_dmah;
309 
310 	/* Offset in the context page for storing dynamic buffer chains */
311 	int			dr_offset;
312 
313 	/* Destroy this request if true */
314 	int			destroy;
315 };
316 
317 /*
318  * Request flags (dca_request_t.dr_flags).
319  */
320 #define	DR_INPLACE		0x002
321 #define	DR_SCATTER		0x004
322 #define	DR_GATHER		0x008
323 #define	DR_NOCACHE		0x020
324 #define	DR_ENCRYPT		0x040
325 #define	DR_DECRYPT		0x080
326 #define	DR_TRIPLE		0x100	/* triple DES vs. single DES */
327 #define	DR_ATOMIC		0x200	/* for atomic operation */
328 
329 struct dca_work {
330 	dca_listnode_t		dw_linkage;
331 	dca_worklist_t		*dw_wlp;
332 
333 	/* DMA access to the MCR and context */
334 	ddi_acc_handle_t	dw_mcr_acch;
335 	ddi_dma_handle_t	dw_mcr_dmah;
336 	caddr_t			dw_mcr_kaddr;
337 	uint32_t		dw_mcr_paddr;
338 
339 	dca_request_t		*dw_reqs[MAXREQSPERMCR];
340 	clock_t			dw_lbolt;
341 };
342 
343 /*
344  * MCRs.
345  */
346 #define	MCR1			0x1
347 #define	MCR2			0x2
348 
349 struct dca_worklist {
350 	dca_t			*dwl_dca;
351 	crypto_kcf_provider_handle_t	dwl_prov;
352 	char			dwl_name[16];
353 	int			dwl_mcr;
354 	kmutex_t		dwl_lock;
355 	kmutex_t		dwl_freelock;
356 	kmutex_t		dwl_freereqslock;
357 	kcondvar_t		dwl_cv;
358 	dca_listnode_t		dwl_freereqs;	/* available requests */
359 	dca_listnode_t		dwl_waitq;	/* requests arrive here */
360 	dca_listnode_t		dwl_freework;	/* available work structures */
361 	dca_listnode_t		dwl_runq;	/* work structs sent to chip */
362 	timeout_id_t		dwl_schedtid;
363 	clock_t			dwl_lastsubmit;
364 	int			dwl_count;
365 	int			dwl_busy;
366 	int			dwl_lowater;
367 	int			dwl_hiwater;
368 	int			dwl_reqspermcr;
369 	int			dwl_drain;	/* for DR (suspend) */
370 	/* Kstats */
371 	u_longlong_t		dwl_submit;
372 	u_longlong_t		dwl_flowctl;
373 };
374 
375 /*
376  * Operations for MCR1 (bulk stuff).
377  */
378 #define	CMD_IPSEC		0x0	/* IPsec packet processing */
379 #define	CMD_SSLMAC		0x1	/* SSL HMAC processing */
380 #define	CMD_TLSMAC		0x2	/* TLS HMAC processing */
381 #define	CMD_3DES		0x3	/* SSL/TLS/raw 3DES processing */
382 #define	CMD_RC4			0x4	/* ARCFOUR procesing */
383 #define	CMD_PUREHASH		0x5	/* Pure MD5/SHA1 hash processing */
384 
385 /*
386  * Operations for MCR2 (key stuff).
387  */
388 #define	CMD_DHPUBLIC		0x1	/* DH public key generation */
389 #define	CMD_DHSHARED		0x2	/* DH shared secret generation */
390 #define	CMD_RSAPUBLIC		0x3	/* RSA public key operation */
391 #define	CMD_RSAPRIVATE		0x4	/* RSA private key operation (CRT) */
392 #define	CMD_DSASIGN		0x5	/* DSA signing operation */
393 #define	CMD_DSAVERIFY		0x6	/* DSA verification operation */
394 #define	CMD_RNGDIRECT		0x41	/* Direct access to the RNG */
395 #define	CMD_RNGSHA1		0x42	/* RNG output processed by SHA1 */
396 #define	CMD_MODADD		0x43	/* Modular add */
397 #define	CMD_MODSUB		0x44	/* Moduler subtract */
398 #define	CMD_MODMUL		0x45	/* Modular multiply */
399 #define	CMD_MODREM		0x46	/* Modular remainder */
400 #define	CMD_MODEXP		0x47	/* Modular exponentiation */
401 #define	CMD_MODINV		0x48	/* Modular inverse */
402 
403 /*
404  * Kstats.
405  */
406 #define	DS_3DESJOBS		0
407 #define	DS_3DESBYTES		1
408 #define	DS_RSAPUBLIC		2
409 #define	DS_RSAPRIVATE		3
410 #define	DS_DSASIGN		4
411 #define	DS_DSAVERIFY		5
412 #define	DS_RNGJOBS		6
413 #define	DS_RNGBYTES		7
414 #define	DS_RNGSHA1JOBS		8
415 #define	DS_RNGSHA1BYTES		9
416 #define	DS_MAX			10
417 
418 #if 0
419 /*
420  * note that when reenabling any of these stats, DS_MAX will need to
421  * be adjusted.
422  */
423 #define	DS_RC4JOBS		11
424 #define	DS_RC4BYTES		12
425 #define	DS_DHPUBLIC		13
426 #define	DS_DHSECRET		14
427 #endif
428 
429 struct dca_stat {
430 	kstat_named_t		ds_status;
431 	kstat_named_t		ds_algs[DS_MAX];
432 	struct {
433 		kstat_named_t	ds_submit;
434 		kstat_named_t	ds_flowctl;
435 		kstat_named_t	ds_lowater;
436 		kstat_named_t	ds_hiwater;
437 		kstat_named_t	ds_maxreqs;
438 	}			ds_mcr[MAXMCR];
439 };
440 
441 /*
442  * Blocking structure for ioctls.
443  */
444 struct dca_cookie {
445 	kmutex_t		dc_mx;
446 	kcondvar_t		dc_cv;
447 	int			dc_outstanding;
448 	int			dc_status;
449 };
450 
451 /*
452  * Per instance structure.
453  */
454 struct dca {
455 	dev_info_t		*dca_dip;
456 	kmutex_t		dca_intrlock;
457 	caddr_t			dca_regs;
458 	ddi_acc_handle_t	dca_regs_handle;
459 	ddi_iblock_cookie_t	dca_icookie;
460 	timeout_id_t		dca_jobtid;
461 	ulong_t			dca_pagesize;
462 	unsigned		dca_flags;	/* dev state flags */
463 
464 	/*
465 	 * Work requests.
466 	 */
467 	dca_worklist_t		dca_worklist[MAXMCR];
468 
469 	/*
470 	 * hardware model
471 	 */
472 	char			*dca_model;
473 	ushort_t		dca_devid;
474 
475 	/*
476 	 * Kstats.  There is no standard for what standards
477 	 * Cryptographic Providers should supply, so we're
478 	 * making them up for now.
479 	 */
480 	kstat_t			*dca_ksp;
481 	kstat_t			*dca_intrstats;
482 	u_longlong_t		dca_stats[DS_MAX];
483 
484 	/* For the local random number pool used internally by the dca driver */
485 	char 			*dca_buf1;
486 	char 			*dca_buf2;
487 	char 			*dca_buf_ptr;
488 	int 			dca_index;
489 	uint32_t 		dca_random_filling;
490 	kmutex_t 		dca_random_lock;
491 
492 	/* FMA capabilities */
493 	int			fm_capabilities;	/* FMA capabilities */
494 
495 	kmutex_t		dca_ctx_list_lock;
496 	dca_listnode_t		dca_ctx_list;
497 };
498 
499 /*
500  * Device flags (dca_t.dca_flags)
501  */
502 #define	DCA_FAILED		0x1
503 #define	DCA_POWERMGMT		0x4
504 #define	DCA_RNGSHA1		0x8
505 
506 #define	KIOIP(dca)		KSTAT_INTR_PTR((dca)->dca_intrstats)
507 
508 /*
509  * Scatter/gather checks.
510  */
511 typedef enum dca_sg_param {
512 	DCA_SG_CONTIG = 1,
513 	DCA_SG_WALIGN,
514 	DCA_SG_PALIGN
515 } dca_sg_param_t;
516 
517 #define	FALSE		0
518 #define	TRUE		1
519 
520 /*
521  * PCI configuration registers.
522  */
523 #define	PCI_VENID		0x00	/* vendor id, 16 bits */
524 #define	PCI_DEVID		0x02	/* device id, 16 bits */
525 #define	PCI_COMM		0x04	/* command, 16 bits */
526 #define	PCI_STATUS		0x06	/* status, 16 bits */
527 #define	PCI_REVID		0x08	/* revision id, 8 bits */
528 #define	PCI_PROGCLASS		0x09	/* programming class, 8 bits */
529 #define	PCI_SUBCLASS		0x0A	/* subclass, 8 bits */
530 #define	PCI_CACHELINESZ		0x0C	/* cache line size, 8 bits */
531 #define	PCI_LATTMR		0x0D	/* latency timer, 8 bits */
532 #define	PCI_BIST		0x0F	/* builtin-self-test, 8 bits */
533 #define	PCI_SUBVENID		0x2C	/* subsystem vendor id, 16 bits */
534 #define	PCI_SUBSYSID		0x2E	/* subsystem id, 16 bits */
535 #define	PCI_MINGNT		0x3E	/* min grant for burst, 8 bits */
536 #define	PCI_MAXLAT		0x3F	/* maximum grant for burst, 8 bits */
537 #define	PCI_TRDYTO		0x40	/* TRDY timeout, 8 bits */
538 #define	PCI_RETRIES		0x41	/* retries bus will perform, 8 bits */
539 
540 /*
541  * PCI configuration register bit values.
542  */
543 #define	PCICOMM_FBBE		0x0200	/* fast back-to-back enable */
544 #define	PCICOMM_SEE		0x0100	/* system error enable */
545 #define	PCICOMM_PEE		0x0040	/* parity error enable */
546 #define	PCICOMM_MWIE		0x0010	/* memory write & invalidate enable */
547 #define	PCICOMM_BME		0x0004	/* bus master enable */
548 #define	PCICOMM_MAE		0x0002	/* memory access enable */
549 
550 #define	PCISTAT_PERR		0x8000	/* parity error detected */
551 #define	PCISTAT_SERR		0x4000	/* system error detected */
552 #define	PCISTAT_MABRT		0x2000	/* master abort detected */
553 #define	PCISTAT_TABRT		0x1000	/* target abort detected */
554 #define	PCISTAT_TABRTS		0x0800	/* target abort signaled */
555 #define	PCISTAT_PARITY		0x0100	/* data parity error detected */
556 
557 #define	PCIREVID_DOMESTIC	0x01	/* domestic version */
558 #define	PCIREVID_EXPORT		0xE1	/* export version */
559 
560 /* Note: 5820 errata: BIST feature does not work */
561 #define	PCIBIST_CAP		0x80	/* BIST capable */
562 #define	PCIBIST_START		0x40	/* start BIST test */
563 #define	PCIBIST_ERRMASK		0x0F	/* mask of BIST error codes */
564 
565 /*
566  * Command and Status Registers.
567  */
568 #define	CSR_MCR1		0x00	/* pointer to MCR1 (bulk) */
569 #define	CSR_DMACTL		0x04	/* DMA control */
570 #define	CSR_DMASTAT		0x08	/* DMA status */
571 #define	CSR_DMAEA		0x0C	/* DMA error address */
572 #define	CSR_MCR2		0x10	/* pointer to MCR2 (exponentiator) */
573 
574 /*
575  * Command and status register bits.
576  */
577 #define	DMACTL_RESET		0x80000000U	/* reset the chip */
578 #define	DMACTL_MCR2IE		0x40000000U	/* MCR2 interrupt enable */
579 #define	DMACTL_MCR1IE		0x20000000U	/* MCR1 interrupt enable */
580 #define	DMACTL_OFM		0x10000000U	/* output fragment mode */
581 #define	DMACTL_BE32		0x08000000U	/* 32-bit big endian mode */
582 #define	DMACTL_BE64		0x04000000U	/* 64-bit big endian mode */
583 #define	DMACTL_EIE		0x02000000U	/* error interrupt enable */
584 #define	DMACTL_RNGMASK		0x01800000U	/* RNG mode mask */
585 #define	DMACTL_RNG1		0x00000000U	/* 1 RNG bit per cycle */
586 #define	DMACTL_RNG4		0x00800000U	/* 1 RNG bit per 4 cycles */
587 #define	DMACTL_RNG8		0x01000000U	/* 1 RNG bit per 8 cycles */
588 #define	DMACTL_RNG16		0x01800000U	/* 1 RNG bit per 16 cycles */
589 #define	DMACTL_MODNORM		0x00400000U	/* s/w modulus normalization */
590 #define	DMACTL_RD256		0x00020000U	/* 256 byte read DMA size */
591 #define	DMACTL_FRAGMASK		0x0000FFFFU	/* output fragment size */
592 
593 #define	DMASTAT_MAIP		0x80000000U	/* master access in progress */
594 #define	DMASTAT_MCR1FULL	0x40000000U	/* MCR1 is full */
595 #define	DMASTAT_MCR1INT		0x20000000U	/* MCR1 interrupted */
596 #define	DMASTAT_ERRINT		0x10000000U	/* error interrupted */
597 #define	DMASTAT_MCR2FULL	0x08000000U	/* MCR2 is full */
598 #define	DMASTAT_MCR2INT		0x04000000U	/* MCR2 interrupted */
599 #define	DMASTAT_INTERRUPTS	0x34000000U	/* all interrupts */
600 
601 /*
602  * Offsets of things relative to an MCR.
603  */
604 #define	MCR_COUNT	0	/* 16 bits */
605 #define	MCR_FLAGS	2	/* 16 bits */
606 #define	MCR_CTXADDR	4	/* 32 bits */
607 
608 /*
609  * Basis for size (should be optimized by constant folding):
610  *	4 bytes for flags and #packets.
611  *	for each packet:
612  *		2 descriptors (DESC_SIZE)
613  *		4 bytes for context address
614  *		4 bytes for packet length and reserved
615  */
616 #define	MCR_SIZE	(4 + MAXREQSPERMCR * ((2 * DESC_SIZE) + 8))
617 
618 /*
619  * MCR flags.
620  */
621 #define	MCRFLAG_FINISHED	0x0001		/* MCR processing complete */
622 #define	MCRFLAG_ERROR		0x0002		/* set if an error occured */
623 #define	MCRFLAG_ERRORMASK	0xff00		/* error code bits */
624 
625 /*
626  * Fields within a descriptor (data buffer chain).
627  */
628 #define	DESC_BUFADDR	0	/* 32 bits */
629 #define	DESC_NEXT	4	/* 32 bits */
630 #define	DESC_LENGTH	8	/* 16 bits */
631 #define	DESC_RSVD	10	/* 16 bits */
632 #define	DESC_SIZE	16	/* ROUNDUP(12, 16) - descriptor size (bytes) */
633 
634 /*
635  * Offsets of fields within context structures, see Broadcom spec.
636  */
637 #define	CTX_LENGTH		0	/* 16 bits */
638 #define	CTX_CMD			2	/* 16 bits */
639 #define	CTX_MAXLENGTH		768	/* max size of ctx, fits anything */
640 
641 #define	CTX_3DESDIRECTION	4	/* 16 bits */
642 #define	CTX_3DESKEY1HI		8	/* 32 bits */
643 #define	CTX_3DESKEY1LO		12	/* 32 bits */
644 #define	CTX_3DESKEY2HI		16	/* 32 bits */
645 #define	CTX_3DESKEY2LO		20	/* 32 bits */
646 #define	CTX_3DESKEY3HI		24	/* 32 bits */
647 #define	CTX_3DESKEY3LO		28	/* 32 bits */
648 #define	CTX_3DESIVHI		32	/* 32 bits */
649 #define	CTX_3DESIVLO		36	/* 32 bits */
650 
651 #define	CTX_IPSECFLAGS		4	/* 16 bits */
652 #define	CTX_IPSECOFFSET		6	/* 16 bits */
653 #define	CTX_IPSECKEY1HI		8	/* 32 bits */
654 #define	CTX_IPSECKEY1LO		12	/* 32 bits */
655 #define	CTX_IPSECKEY2HI		16	/* 32 bits */
656 #define	CTX_IPSECKEY2LO		20	/* 32 bits */
657 #define	CTX_IPSECKEY3HI		24	/* 32 bits */
658 #define	CTX_IPSECKEY3LO		28	/* 32 bits */
659 #define	CTX_IPSECIVHI		32	/* 32 bits */
660 #define	CTX_IPSECIVLO		36	/* 32 bits */
661 #define	CTX_IPSECHMACINNER1	40	/* 32 bits */
662 #define	CTX_IPSECHMACINNER2	44	/* 32 bits */
663 #define	CTX_IPSECHMACINNER3	48	/* 32 bits */
664 #define	CTX_IPSECHMACINNER4	52	/* 32 bits */
665 #define	CTX_IPSECHMACINNER5	56	/* 32 bits */
666 #define	CTX_IPSECHMACOUTER1	60	/* 32 bits */
667 #define	CTX_IPSECHMACOUTER2	64	/* 32 bits */
668 #define	CTX_IPSECHMACOUTER3	68	/* 32 bits */
669 #define	CTX_IPSECHMACOUTER4	72	/* 32 bits */
670 #define	CTX_IPSECHMACOUTER5	76	/* 32 bits */
671 
672 #define	CTX_RSAEXPLEN		4	/* 16 bits */
673 #define	CTX_RSAMODLEN		6	/* 16 bits */
674 #define	CTX_RSABIGNUMS		8	/* variable length */
675 #define	CTX_RSAQLEN		4	/* 16 bits */
676 #define	CTX_RSAPLEN		6	/* 16 bits */
677 
678 #define	CTX_DSAMSGTYPE		4	/* 16 bits */
679 #define	CTX_DSARSVD		6	/* 16 bits */
680 #define	CTX_DSARNG		8	/* 16 bits */
681 #define	CTX_DSAPLEN		10	/* 16 bits */
682 #define	CTX_DSABIGNUMS		12	/* variable length */
683 
684 /*
685  * Values for specific operations.
686  */
687 #define	CTX_RNG_LENGTH		64	/* context length for RNG (64 min) */
688 #define	CTX_3DES_LENGTH		64	/* context length for 3DES (64 min) */
689 #define	CTX_3DES_DECRYPT	0x4000	/* perform decryption */
690 #define	CTX_3DES_ENCRYPT	0x0000	/* perform encryption */
691 #define	CTX_IPSEC_LENGTH	80	/* context length for IPsec */
692 #define	CTX_IPSEC_ENCRYPT	0x8000	/* perform encryption */
693 #define	CTX_IPSEC_DECRYPT	0xc000	/* perform decryption */
694 #define	CTX_IPSEC_HMAC_MD5	0x1000	/* HMAC-MD5 authentication */
695 #define	CTX_IPSEC_HMAC_SHA1	0x2000	/* HMAC-MD5 authentication */
696 #define	CTX_DSAMSGTYPE_SHA1	0	/* Message is SHA1 */
697 #define	CTX_DSAMSGTYPE_TEXT	1	/* Generate SHA1 hash first */
698 #define	CTX_DSARNG_GEN		1	/* Generate random k */
699 #define	CTX_DSARNG_SUPPLY	0	/* Random k is supplied */
700 
701 /*
702  * Macros to access fields within the MCR.  Note that this includes the
703  * context fields as well, since the context is just offset from the
704  * base of the MCR.
705  */
706 
707 #define	PUTMCR32(work, reg, val)	\
708 	ddi_put32(work->dw_mcr_acch,	\
709 	(uint32_t *)(work->dw_mcr_kaddr + reg), val)
710 
711 #define	PUTMCR16(work, reg, val)	\
712 	ddi_put16(work->dw_mcr_acch,	\
713 	(uint16_t *)(work->dw_mcr_kaddr + reg), val)
714 
715 #define	GETMCR32(work, reg)	\
716 	ddi_get32(work->dw_mcr_acch, (uint32_t *)(work->dw_mcr_kaddr + reg))
717 
718 #define	GETMCR16(work, reg)	\
719 	ddi_get16(work->dw_mcr_acch, (uint16_t *)(work->dw_mcr_kaddr + reg))
720 
721 #define	PUTDESC32(req, dc_desc_kaddr, reg, val)	\
722 	ddi_put32(req->dr_ctx_acch,	\
723 	(uint32_t *)(dc_desc_kaddr + reg), val)
724 
725 #define	PUTDESC16(req, dc_desc_kaddr, reg, val)	\
726 	ddi_put16(req->dr_ctx_acch,	\
727 	(uint16_t *)(dc_desc_kaddr + reg), val)
728 
729 /* XXX: define the GET forms for descriptors only if needed */
730 
731 #define	PUTCTX32(req, reg, val)	\
732 	ddi_put32(req->dr_ctx_acch,	\
733 	(uint32_t *)(req->dr_ctx_kaddr + reg), val)
734 
735 #define	PUTCTX16(req, reg, val)	\
736 	ddi_put16(req->dr_ctx_acch,	\
737 	(uint16_t *)(req->dr_ctx_kaddr + reg), val)
738 
739 #define	CTXBCOPY(req, src, dst, count)	\
740 	ddi_rep_put8(req->dr_ctx_acch, (uchar_t *)src, (uchar_t *)dst, count, \
741 	DDI_DEV_AUTOINCR)
742 
743 /*
744  * Register access.
745  */
746 #define	GETCSR(dca, reg)	\
747 	ddi_get32(dca->dca_regs_handle, (uint_t *)(dca->dca_regs + reg))
748 
749 #define	PUTCSR(dca, reg, val)	\
750 	ddi_put32(dca->dca_regs_handle, (uint_t *)(dca->dca_regs + reg), val)
751 
752 #define	SETBIT(dca, reg, val)	\
753 	PUTCSR(dca, reg, GETCSR(dca, reg) | val)
754 
755 #define	CLRBIT(dca, reg, val)	\
756 	PUTCSR(dca, reg, GETCSR(dca, reg) & ~val)
757 
758 /*
759  * Used to guarantee alignment.
760  */
761 #define	ROUNDUP(a, n)	(((a) + ((n) - 1)) & ~((n) - 1))
762 #define	ROUNDDOWN(a, n)	(((a) & ~((n) - 1)))
763 #define	HIDBLWORD(x)	(((x) & 0xffffffff00000000ULL) >> 32)
764 #define	LODBLWORD(x)	((x) & 0xffffffffULL)
765 
766 /*
767  * Driver hardening related.
768  */
769 #define	CHECK_REGS(dca)	ddi_check_acc_handle(dca->dca_regs_handle)
770 
771 /*
772  * Other utility macros.
773  */
774 #define	QEMPTY(q)	((q)->dl_next == (q))
775 #define	BITS2BYTES(b)	((b) >> 3)
776 #define	WORKLIST(dca, mcr)	(&((dca)->dca_worklist[mcr - 1]))
777 
778 /*
779  * Debug stuff.
780  */
781 #ifdef	DEBUG
782 #define	DWARN		0x0001
783 #define	DPCI		0x0002
784 #define	DINTR		0x0004
785 #define	DSTART		0x0008
786 #define	DRECLAIM	0x0010
787 #define	DCHATTY		0x0020
788 #define	DMOD		0x0040	/* _init/_fini/_info/attach/detach */
789 #define	DENTRY		0x0080	/* crypto routine entry/exit points */
790 
791 void	dca_dprintf(dca_t *, int, const char *, ...);
792 #define	DBG	dca_dprintf
793 #else
794 #define	DBG(dca, lvl, ...)
795 #endif
796 
797 /*
798  * Some pkcs#11 defines as there are no pkcs#11 header files included.
799  */
800 #define	CKO_PUBLIC_KEY		0x00000002
801 #define	CKO_PRIVATE_KEY		0x00000003
802 
803 #define	CKA_CLASS		0x00000000
804 #define	CKA_VALUE		0x00000011
805 #define	CKA_KEY_TYPE		0x00000100
806 #define	CKA_MODULUS		0x00000120
807 #define	CKA_PUBLIC_EXPONENT	0x00000122
808 #define	CKA_PRIVATE_EXPONENT	0x00000123
809 #define	CKA_PRIME_1		0x00000124
810 #define	CKA_PRIME_2		0x00000125
811 #define	CKA_EXPONENT_1		0x00000126
812 #define	CKA_EXPONENT_2		0x00000127
813 #define	CKA_COEFFICIENT		0x00000128
814 #define	CKA_PRIME		0x00000130
815 #define	CKA_SUBPRIME		0x00000131
816 #define	CKA_BASE		0x00000132
817 /*
818  * Driver globals.
819  */
820 extern int	dca_mindma;
821 extern int	dca_hardening;
822 
823 /*
824  * Prototypes.
825  */
826 
827 /*
828  * dca_debug.c
829  */
830 void	dca_error(dca_t *, const char *, ...);
831 void	dca_diperror(dev_info_t *, const char *, ...);
832 void	dca_dipverror(dev_info_t *, const char *, va_list);
833 /*
834  * dca_3des.c
835  */
836 int	dca_3desctxinit(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *,
837     int, int);
838 int	dca_3des(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
839     crypto_req_handle_t, int);
840 int	dca_3desupdate(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
841     crypto_req_handle_t, int);
842 int	dca_3desfinal(crypto_ctx_t *, crypto_data_t *, int);
843 int	dca_3desatomic(crypto_provider_handle_t, crypto_session_id_t,
844     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
845     int, crypto_req_handle_t, int);
846 void	dca_3desctxfree(void *);
847 
848 /*
849  * dca_rsa.c
850  */
851 int	dca_rsastart(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
852     crypto_req_handle_t, int);
853 int	dca_rsainit(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *, int);
854 void	dca_rsactxfree(void *);
855 int	dca_rsaatomic(crypto_provider_handle_t, crypto_session_id_t,
856     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
857     int, crypto_req_handle_t, int);
858 
859 /*
860  * dca_dsa.c
861  */
862 int	dca_dsa_sign(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
863     crypto_req_handle_t);
864 int	dca_dsa_verify(crypto_ctx_t *, crypto_data_t *, crypto_data_t *,
865     crypto_req_handle_t);
866 int	dca_dsainit(crypto_ctx_t *, crypto_mechanism_t *, crypto_key_t *, int,
867     int);
868 void	dca_dsactxfree(void *);
869 int	dca_dsaatomic(crypto_provider_handle_t, crypto_session_id_t,
870     crypto_mechanism_t *, crypto_key_t *, crypto_data_t *, crypto_data_t *,
871     int, crypto_req_handle_t, int);
872 
873 /*
874  * dca_rng.c
875  */
876 int	dca_rng(dca_t *, uchar_t *, size_t len, crypto_req_handle_t);
877 int	dca_random_buffer(dca_t *dca, caddr_t buf, int len);
878 int	dca_random_init();
879 void	dca_random_fini();
880 
881 /*
882  * dca_kstat.c
883  */
884 void	dca_ksinit(dca_t *);
885 /*
886  * dca.c
887  */
888 void	dca_rmqueue(dca_listnode_t *);
889 dca_request_t *dca_getreq(dca_t *, int, int);
890 void	dca_freereq(dca_request_t *);
891 int	dca_bindchains(dca_request_t *, size_t, size_t);
892 int	dca_unbindchains(dca_request_t *);
893 int	dca_start(dca_t *, dca_request_t *, int, int);
894 void	dca_done(dca_request_t *, int);
895 void	dca_destroyreq(dca_request_t *);
896 int	dca_length(crypto_data_t *);
897 int	dca_gather(crypto_data_t *, char *, int, int);
898 int	dca_resid_gather(crypto_data_t *, char *, int *, char *, int);
899 int	dca_scatter(const char *, crypto_data_t *, int, int);
900 int	dca_bcmp_reverse(const void *s1, const void *s2, size_t n);
901 int	dca_dupcrypto(crypto_data_t *, crypto_data_t *);
902 int	dca_verifyio(crypto_data_t *, crypto_data_t *);
903 int	dca_getbufbytes(crypto_data_t *, size_t, int, uchar_t *);
904 int	dca_sgcheck(dca_t *, crypto_data_t *, dca_sg_param_t);
905 crypto_object_attribute_t *
906 	dca_get_key_attr(crypto_key_t *);
907 int	dca_attr_lookup_uint32(crypto_object_attribute_t *, uint_t, uint64_t,
908 	    uint32_t *);
909 int	dca_attr_lookup_uint8_array(crypto_object_attribute_t *, uint_t,
910 	    uint64_t, void **, unsigned int *);
911 crypto_object_attribute_t *
912 	dca_find_attribute(crypto_object_attribute_t *, uint_t, uint64_t);
913 caddr_t	dca_bufdaddr(crypto_data_t *);
914 void	dca_rcoalesce(dca_request_t *, int);
915 void	dca_runcoalesce(dca_request_t *);
916 int	dca_bitlen(unsigned char *, int);
917 uint16_t dca_padhalf(int);
918 uint16_t dca_padfull(int);
919 void	dca_reverse(void *, void *, int, int);
920 int	dca_numcmp(caddr_t, int, caddr_t, int);
921 int dca_check_dma_handle(dca_t *dca, ddi_dma_handle_t handle,
922 	dca_fma_eclass_t eclass_index);
923 int dca_free_context(crypto_ctx_t *ctx);
924 
925 #endif	/* _KERNEL */
926 
927 #ifdef	__cplusplus
928 }
929 #endif
930 
931 #endif	/* _SYS_CRYPTO_DCA_H */
932