xref: /onnv-gate/usr/src/uts/common/sys/crypto/sched_impl.h (revision 12304:bcfa0838b31e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51808Smcpowers  * Common Development and Distribution License (the "License").
61808Smcpowers  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12304SValerie.Fenwick@Oracle.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #ifndef _SYS_CRYPTO_SCHED_IMPL_H
260Sstevel@tonic-gate #define	_SYS_CRYPTO_SCHED_IMPL_H
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * Scheduler internal structures.
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #ifdef __cplusplus
330Sstevel@tonic-gate extern "C" {
340Sstevel@tonic-gate #endif
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/mutex.h>
380Sstevel@tonic-gate #include <sys/condvar.h>
390Sstevel@tonic-gate #include <sys/door.h>
400Sstevel@tonic-gate #include <sys/crypto/api.h>
410Sstevel@tonic-gate #include <sys/crypto/spi.h>
420Sstevel@tonic-gate #include <sys/crypto/impl.h>
430Sstevel@tonic-gate #include <sys/crypto/common.h>
440Sstevel@tonic-gate #include <sys/crypto/ops_impl.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate typedef void (kcf_func_t)(void *, int);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate typedef enum kcf_req_status {
490Sstevel@tonic-gate 	REQ_ALLOCATED = 1,
500Sstevel@tonic-gate 	REQ_WAITING,		/* At the framework level */
510Sstevel@tonic-gate 	REQ_INPROGRESS,		/* At the provider level */
520Sstevel@tonic-gate 	REQ_DONE,
530Sstevel@tonic-gate 	REQ_CANCELED
540Sstevel@tonic-gate } kcf_req_status_t;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate typedef enum kcf_call_type {
570Sstevel@tonic-gate 	CRYPTO_SYNCH = 1,
580Sstevel@tonic-gate 	CRYPTO_ASYNCH
590Sstevel@tonic-gate } kcf_call_type_t;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	CHECK_FASTPATH(crq, pd) ((crq) == NULL ||	\
620Sstevel@tonic-gate 	!((crq)->cr_flag & CRYPTO_ALWAYS_QUEUE)) &&	\
630Sstevel@tonic-gate 	(pd)->pd_prov_type == CRYPTO_SW_PROVIDER
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	KCF_KMFLAG(crq)	(((crq) == NULL) ? KM_SLEEP : KM_NOSLEEP)
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * The framework keeps an internal handle to use in the adaptive
690Sstevel@tonic-gate  * asynchronous case. This is the case when a client has the
700Sstevel@tonic-gate  * CRYPTO_ALWAYS_QUEUE bit clear and a software provider is used for
710Sstevel@tonic-gate  * the request. The request is completed in the context of the calling
720Sstevel@tonic-gate  * thread and kernel memory must be allocated with KM_NOSLEEP.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * The framework passes a pointer to the handle in crypto_req_handle_t
750Sstevel@tonic-gate  * argument when it calls the SPI of the software provider. The macros
760Sstevel@tonic-gate  * KCF_RHNDL() and KCF_SWFP_RHNDL() are used to do this.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * When a provider asks the framework for kmflag value via
790Sstevel@tonic-gate  * crypto_kmflag(9S) we use REQHNDL2_KMFLAG() macro.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate extern ulong_t kcf_swprov_hndl;
820Sstevel@tonic-gate #define	KCF_RHNDL(kmflag) (((kmflag) == KM_SLEEP) ? NULL : &kcf_swprov_hndl)
830Sstevel@tonic-gate #define	KCF_SWFP_RHNDL(crq) (((crq) == NULL) ? NULL : &kcf_swprov_hndl)
840Sstevel@tonic-gate #define	REQHNDL2_KMFLAG(rhndl) \
850Sstevel@tonic-gate 	((rhndl == &kcf_swprov_hndl) ? KM_NOSLEEP : KM_SLEEP)
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* Internal call_req flags. They start after the public ones in api.h */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	CRYPTO_SETDUAL	0x00001000	/* Set the 'cont' boolean before */
900Sstevel@tonic-gate 					/* submitting the request */
910Sstevel@tonic-gate #define	KCF_ISDUALREQ(crq)	\
920Sstevel@tonic-gate 	(((crq) == NULL) ? B_FALSE : (crq->cr_flag & CRYPTO_SETDUAL))
930Sstevel@tonic-gate 
940Sstevel@tonic-gate typedef struct kcf_prov_tried {
950Sstevel@tonic-gate 	kcf_provider_desc_t	*pt_pd;
960Sstevel@tonic-gate 	struct kcf_prov_tried	*pt_next;
970Sstevel@tonic-gate } kcf_prov_tried_t;
980Sstevel@tonic-gate 
999505SBhargava.Yenduri@Sun.COM /* Must be different from KM_SLEEP and KM_NOSLEEP */
1009505SBhargava.Yenduri@Sun.COM #define	KCF_HOLD_PROV	0x1000
1019505SBhargava.Yenduri@Sun.COM 
1020Sstevel@tonic-gate #define	IS_FG_SUPPORTED(mdesc, fg)		\
1030Sstevel@tonic-gate 	(((mdesc)->pm_mech_info.cm_func_group_mask & (fg)) != 0)
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #define	IS_PROVIDER_TRIED(pd, tlist)		\
1060Sstevel@tonic-gate 	(tlist != NULL && is_in_triedlist(pd, tlist))
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #define	IS_RECOVERABLE(error)			\
1090Sstevel@tonic-gate 	(error == CRYPTO_BUFFER_TOO_BIG ||	\
1100Sstevel@tonic-gate 	error == CRYPTO_BUSY ||			\
1110Sstevel@tonic-gate 	error == CRYPTO_DEVICE_ERROR ||		\
1120Sstevel@tonic-gate 	error == CRYPTO_DEVICE_MEMORY ||	\
1130Sstevel@tonic-gate 	error == CRYPTO_KEY_SIZE_RANGE ||	\
1140Sstevel@tonic-gate 	error == CRYPTO_NO_PERMISSION)
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate #define	KCF_ATOMIC_INCR(x)	atomic_add_32(&(x), 1)
1170Sstevel@tonic-gate #define	KCF_ATOMIC_DECR(x)	atomic_add_32(&(x), -1)
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Node structure for synchronous requests.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate typedef struct kcf_sreq_node {
1230Sstevel@tonic-gate 	/* Should always be the first field in this structure */
1240Sstevel@tonic-gate 	kcf_call_type_t		sn_type;
1250Sstevel@tonic-gate 	/*
1260Sstevel@tonic-gate 	 * sn_cv and sr_lock are used to wait for the
1270Sstevel@tonic-gate 	 * operation to complete. sn_lock also protects
1280Sstevel@tonic-gate 	 * the sn_state field.
1290Sstevel@tonic-gate 	 */
1300Sstevel@tonic-gate 	kcondvar_t		sn_cv;
1310Sstevel@tonic-gate 	kmutex_t		sn_lock;
1320Sstevel@tonic-gate 	kcf_req_status_t	sn_state;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * Return value from the operation. This will be
1360Sstevel@tonic-gate 	 * one of the CRYPTO_* errors defined in common.h.
1370Sstevel@tonic-gate 	 */
1380Sstevel@tonic-gate 	int			sn_rv;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * parameters to call the SPI with. This can be
1420Sstevel@tonic-gate 	 * a pointer as we know the caller context/stack stays.
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	struct kcf_req_params	*sn_params;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/* Internal context for this request */
1470Sstevel@tonic-gate 	struct kcf_context	*sn_context;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/* Provider handling this request */
1500Sstevel@tonic-gate 	kcf_provider_desc_t	*sn_provider;
1519505SBhargava.Yenduri@Sun.COM 
1529505SBhargava.Yenduri@Sun.COM 	kcf_prov_cpu_t		*sn_mp;
1530Sstevel@tonic-gate } kcf_sreq_node_t;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate  * Node structure for asynchronous requests. A node can be on
1570Sstevel@tonic-gate  * on a chain of requests hanging of the internal context
1580Sstevel@tonic-gate  * structure and can be in the global software provider queue.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate typedef struct kcf_areq_node {
1610Sstevel@tonic-gate 	/* Should always be the first field in this structure */
1620Sstevel@tonic-gate 	kcf_call_type_t		an_type;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/* an_lock protects the field an_state  */
1650Sstevel@tonic-gate 	kmutex_t		an_lock;
1660Sstevel@tonic-gate 	kcf_req_status_t	an_state;
1670Sstevel@tonic-gate 	crypto_call_req_t	an_reqarg;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	/*
1700Sstevel@tonic-gate 	 * parameters to call the SPI with. We need to
1710Sstevel@tonic-gate 	 * save the params since the caller stack can go away.
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	struct kcf_req_params	an_params;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * The next two fields should be NULL for operations that
1770Sstevel@tonic-gate 	 * don't need a context.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	/* Internal context for this request */
1800Sstevel@tonic-gate 	struct kcf_context	*an_context;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* next in chain of requests for context */
1830Sstevel@tonic-gate 	struct kcf_areq_node	*an_ctxchain_next;
1840Sstevel@tonic-gate 
1854494Skrishna 	kcondvar_t		an_turn_cv;
1860Sstevel@tonic-gate 	boolean_t		an_is_my_turn;
1870Sstevel@tonic-gate 	boolean_t		an_isdual;	/* for internal reuse */
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	/*
1900Sstevel@tonic-gate 	 * Next and previous nodes in the global software
1910Sstevel@tonic-gate 	 * queue. These fields are NULL for a hardware
1920Sstevel@tonic-gate 	 * provider since we use a taskq there.
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 	struct kcf_areq_node	*an_next;
1950Sstevel@tonic-gate 	struct kcf_areq_node	*an_prev;
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate 	/* Provider handling this request */
1980Sstevel@tonic-gate 	kcf_provider_desc_t	*an_provider;
1999505SBhargava.Yenduri@Sun.COM 	kcf_prov_cpu_t		*an_mp;
2000Sstevel@tonic-gate 	kcf_prov_tried_t	*an_tried_plist;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	struct kcf_areq_node	*an_idnext;	/* Next in ID hash */
2030Sstevel@tonic-gate 	struct kcf_areq_node	*an_idprev;	/* Prev in ID hash */
2040Sstevel@tonic-gate 	kcondvar_t		an_done;	/* Signal request completion */
2050Sstevel@tonic-gate 	uint_t			an_refcnt;
2060Sstevel@tonic-gate } kcf_areq_node_t;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate #define	KCF_AREQ_REFHOLD(areq) {		\
2090Sstevel@tonic-gate 	atomic_add_32(&(areq)->an_refcnt, 1);	\
2100Sstevel@tonic-gate 	ASSERT((areq)->an_refcnt != 0);		\
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate #define	KCF_AREQ_REFRELE(areq) {				\
2140Sstevel@tonic-gate 	ASSERT((areq)->an_refcnt != 0);				\
2150Sstevel@tonic-gate 	membar_exit();						\
2160Sstevel@tonic-gate 	if (atomic_add_32_nv(&(areq)->an_refcnt, -1) == 0)	\
2170Sstevel@tonic-gate 		kcf_free_req(areq);				\
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate #define	GET_REQ_TYPE(arg) *((kcf_call_type_t *)(arg))
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate #define	NOTIFY_CLIENT(areq, err) (*(areq)->an_reqarg.cr_callback_func)(\
2230Sstevel@tonic-gate 	(areq)->an_reqarg.cr_callback_arg, err);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate /* For internally generated call requests for dual operations */
2260Sstevel@tonic-gate typedef	struct kcf_call_req {
2270Sstevel@tonic-gate 	crypto_call_req_t	kr_callreq;	/* external client call req */
2280Sstevel@tonic-gate 	kcf_req_params_t	kr_params;	/* Params saved for next call */
2290Sstevel@tonic-gate 	kcf_areq_node_t		*kr_areq;	/* Use this areq */
2300Sstevel@tonic-gate 	off_t			kr_saveoffset;
2310Sstevel@tonic-gate 	size_t			kr_savelen;
2320Sstevel@tonic-gate } kcf_dual_req_t;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /*
2350Sstevel@tonic-gate  * The following are some what similar to macros in callo.h, which implement
2360Sstevel@tonic-gate  * callout tables.
2370Sstevel@tonic-gate  *
2380Sstevel@tonic-gate  * The lower four bits of the ID are used to encode the table ID to
2390Sstevel@tonic-gate  * index in to. The REQID_COUNTER_HIGH bit is used to avoid any check for
2400Sstevel@tonic-gate  * wrap around when generating ID. We assume that there won't be a request
2410Sstevel@tonic-gate  * which takes more time than 2^^(sizeof (long) - 5) other requests submitted
2420Sstevel@tonic-gate  * after it. This ensures there won't be any ID collision.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate #define	REQID_COUNTER_HIGH	(1UL << (8 * sizeof (long) - 1))
2450Sstevel@tonic-gate #define	REQID_COUNTER_SHIFT	4
2460Sstevel@tonic-gate #define	REQID_COUNTER_LOW	(1 << REQID_COUNTER_SHIFT)
2470Sstevel@tonic-gate #define	REQID_TABLES		16
2480Sstevel@tonic-gate #define	REQID_TABLE_MASK	(REQID_TABLES - 1)
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate #define	REQID_BUCKETS		512
2510Sstevel@tonic-gate #define	REQID_BUCKET_MASK	(REQID_BUCKETS - 1)
2520Sstevel@tonic-gate #define	REQID_HASH(id)	(((id) >> REQID_COUNTER_SHIFT) & REQID_BUCKET_MASK)
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #define	GET_REQID(areq) (areq)->an_reqarg.cr_reqid
2550Sstevel@tonic-gate #define	SET_REQID(areq, val)	GET_REQID(areq) = val
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate  * Hash table for async requests.
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate typedef struct kcf_reqid_table {
2610Sstevel@tonic-gate 	kmutex_t		rt_lock;
2620Sstevel@tonic-gate 	crypto_req_id_t		rt_curid;
2630Sstevel@tonic-gate 	kcf_areq_node_t		*rt_idhash[REQID_BUCKETS];
2640Sstevel@tonic-gate } kcf_reqid_table_t;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate  * Global software provider queue structure. Requests to be
2680Sstevel@tonic-gate  * handled by a SW provider and have the ALWAYS_QUEUE flag set
2690Sstevel@tonic-gate  * get queued here.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate typedef struct kcf_global_swq {
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * gs_cv and gs_lock are used to wait for new requests.
2740Sstevel@tonic-gate 	 * gs_lock protects the changes to the queue.
2750Sstevel@tonic-gate 	 */
2760Sstevel@tonic-gate 	kcondvar_t		gs_cv;
2770Sstevel@tonic-gate 	kmutex_t		gs_lock;
2780Sstevel@tonic-gate 	uint_t			gs_njobs;
2790Sstevel@tonic-gate 	uint_t			gs_maxjobs;
2800Sstevel@tonic-gate 	kcf_areq_node_t		*gs_first;
2810Sstevel@tonic-gate 	kcf_areq_node_t		*gs_last;
2820Sstevel@tonic-gate } kcf_global_swq_t;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2860Sstevel@tonic-gate  * Internal representation of a canonical context. We contain crypto_ctx_t
2870Sstevel@tonic-gate  * structure in order to have just one memory allocation. The SPI
2880Sstevel@tonic-gate  * ((crypto_ctx_t *)ctx)->cc_framework_private maps to this structure.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate typedef struct kcf_context {
2910Sstevel@tonic-gate 	crypto_ctx_t		kc_glbl_ctx;
2920Sstevel@tonic-gate 	uint_t			kc_refcnt;
2930Sstevel@tonic-gate 	kmutex_t		kc_in_use_lock;
2940Sstevel@tonic-gate 	/*
2950Sstevel@tonic-gate 	 * kc_req_chain_first and kc_req_chain_last are used to chain
2960Sstevel@tonic-gate 	 * multiple async requests using the same context. They should be
2970Sstevel@tonic-gate 	 * NULL for sync requests.
2980Sstevel@tonic-gate 	 */
2990Sstevel@tonic-gate 	kcf_areq_node_t		*kc_req_chain_first;
3000Sstevel@tonic-gate 	kcf_areq_node_t		*kc_req_chain_last;
3010Sstevel@tonic-gate 	kcf_provider_desc_t	*kc_prov_desc;	/* Prov. descriptor */
3023708Skrishna 	kcf_provider_desc_t	*kc_sw_prov_desc;	/* Prov. descriptor */
3033708Skrishna 	kcf_mech_entry_t	*kc_mech;
3040Sstevel@tonic-gate 	struct kcf_context	*kc_secondctx;	/* for dual contexts */
3050Sstevel@tonic-gate } kcf_context_t;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate /*
3080Sstevel@tonic-gate  * Bump up the reference count on the framework private context. A
3090Sstevel@tonic-gate  * global context or a request that references this structure should
3100Sstevel@tonic-gate  * do a hold.
3110Sstevel@tonic-gate  */
3120Sstevel@tonic-gate #define	KCF_CONTEXT_REFHOLD(ictx) {		\
3130Sstevel@tonic-gate 	atomic_add_32(&(ictx)->kc_refcnt, 1);	\
3140Sstevel@tonic-gate 	ASSERT((ictx)->kc_refcnt != 0);		\
3150Sstevel@tonic-gate }
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate /*
3180Sstevel@tonic-gate  * Decrement the reference count on the framework private context.
3190Sstevel@tonic-gate  * When the last reference is released, the framework private
3200Sstevel@tonic-gate  * context structure is freed along with the global context.
3210Sstevel@tonic-gate  */
3220Sstevel@tonic-gate #define	KCF_CONTEXT_REFRELE(ictx) {				\
3230Sstevel@tonic-gate 	ASSERT((ictx)->kc_refcnt != 0);				\
3240Sstevel@tonic-gate 	membar_exit();						\
3250Sstevel@tonic-gate 	if (atomic_add_32_nv(&(ictx)->kc_refcnt, -1) == 0)	\
3260Sstevel@tonic-gate 		kcf_free_context(ictx);				\
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate  * Check if we can release the context now. In case of CRYPTO_QUEUED
3310Sstevel@tonic-gate  * we do not release it as we can do it only after the provider notified
3320Sstevel@tonic-gate  * us. In case of CRYPTO_BUSY, the client can retry the request using
3330Sstevel@tonic-gate  * the context, so we do not release the context.
3340Sstevel@tonic-gate  *
3350Sstevel@tonic-gate  * This macro should be called only from the final routine in
3360Sstevel@tonic-gate  * an init/update/final sequence. We do not release the context in case
3370Sstevel@tonic-gate  * of update operations. We require the consumer to free it
3380Sstevel@tonic-gate  * explicitly, in case it wants to abandon the operation. This is done
3390Sstevel@tonic-gate  * as there may be mechanisms in ECB mode that can continue even if
3400Sstevel@tonic-gate  * an operation on a block fails.
3410Sstevel@tonic-gate  */
3420Sstevel@tonic-gate #define	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx) {			\
3430Sstevel@tonic-gate 	if (KCF_CONTEXT_DONE(rv))				\
3440Sstevel@tonic-gate 		KCF_CONTEXT_REFRELE(kcf_ctx);			\
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate  * This macro determines whether we're done with a context.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate #define	KCF_CONTEXT_DONE(rv)					\
3510Sstevel@tonic-gate 	((rv) != CRYPTO_QUEUED && (rv) != CRYPTO_BUSY &&	\
3520Sstevel@tonic-gate 	    (rv) != CRYPTO_BUFFER_TOO_SMALL)
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * A crypto_ctx_template_t is internally a pointer to this struct
3560Sstevel@tonic-gate  */
3570Sstevel@tonic-gate typedef	struct kcf_ctx_template {
3580Sstevel@tonic-gate 	crypto_kcf_provider_handle_t	ct_prov_handle;	/* provider handle */
3590Sstevel@tonic-gate 	uint_t				ct_generation;	/* generation # */
3600Sstevel@tonic-gate 	size_t				ct_size;	/* for freeing */
3610Sstevel@tonic-gate 	crypto_spi_ctx_template_t	ct_prov_tmpl;	/* context template */
3620Sstevel@tonic-gate 							/* from the SW prov */
3630Sstevel@tonic-gate } kcf_ctx_template_t;
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate /*
3660Sstevel@tonic-gate  * Structure for pool of threads working on global software queue.
3670Sstevel@tonic-gate  */
3680Sstevel@tonic-gate typedef struct kcf_pool {
3690Sstevel@tonic-gate 	uint32_t	kp_threads;		/* Number of threads in pool */
3700Sstevel@tonic-gate 	uint32_t	kp_idlethreads;		/* Idle threads in pool */
3710Sstevel@tonic-gate 	uint32_t	kp_blockedthreads;	/* Blocked threads in pool */
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * cv & lock to monitor the condition when no threads
3750Sstevel@tonic-gate 	 * are around. In this case the failover thread kicks in.
3760Sstevel@tonic-gate 	 */
3770Sstevel@tonic-gate 	kcondvar_t	kp_nothr_cv;
3780Sstevel@tonic-gate 	kmutex_t	kp_thread_lock;
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/* Userspace thread creator variables. */
3810Sstevel@tonic-gate 	boolean_t	kp_signal_create_thread; /* Create requested flag  */
3820Sstevel@tonic-gate 	int		kp_nthrs;		/* # of threads to create */
3830Sstevel@tonic-gate 	boolean_t	kp_user_waiting;	/* Thread waiting for work */
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 	/*
3860Sstevel@tonic-gate 	 * cv & lock for the condition where more threads need to be
3870Sstevel@tonic-gate 	 * created. kp_user_lock also protects the three fileds above.
3880Sstevel@tonic-gate 	 */
3890Sstevel@tonic-gate 	kcondvar_t	kp_user_cv;		/* Creator cond. variable */
3900Sstevel@tonic-gate 	kmutex_t	kp_user_lock;		/* Creator lock */
3910Sstevel@tonic-gate } kcf_pool_t;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate /*
3950Sstevel@tonic-gate  * State of a crypto bufcall element.
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate typedef enum cbuf_state {
3980Sstevel@tonic-gate 	CBUF_FREE = 1,
3990Sstevel@tonic-gate 	CBUF_WAITING,
4000Sstevel@tonic-gate 	CBUF_RUNNING
4010Sstevel@tonic-gate } cbuf_state_t;
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate /*
4040Sstevel@tonic-gate  * Structure of a crypto bufcall element.
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate typedef struct kcf_cbuf_elem {
4070Sstevel@tonic-gate 	/*
4080Sstevel@tonic-gate 	 * lock and cv to wait for CBUF_RUNNING to be done
4090Sstevel@tonic-gate 	 * kc_lock also protects kc_state.
4100Sstevel@tonic-gate 	 */
4110Sstevel@tonic-gate 	kmutex_t		kc_lock;
4120Sstevel@tonic-gate 	kcondvar_t		kc_cv;
4130Sstevel@tonic-gate 	cbuf_state_t		kc_state;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	struct kcf_cbuf_elem	*kc_next;
4160Sstevel@tonic-gate 	struct kcf_cbuf_elem	*kc_prev;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	void			(*kc_func)(void *arg);
4190Sstevel@tonic-gate 	void			*kc_arg;
4200Sstevel@tonic-gate } kcf_cbuf_elem_t;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate  * State of a notify element.
4240Sstevel@tonic-gate  */
4250Sstevel@tonic-gate typedef enum ntfy_elem_state {
4260Sstevel@tonic-gate 	NTFY_WAITING = 1,
4270Sstevel@tonic-gate 	NTFY_RUNNING
4280Sstevel@tonic-gate } ntfy_elem_state_t;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate  * Structure of a notify list element.
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate typedef struct kcf_ntfy_elem {
4340Sstevel@tonic-gate 	/*
4350Sstevel@tonic-gate 	 * lock and cv to wait for NTFY_RUNNING to be done.
4360Sstevel@tonic-gate 	 * kn_lock also protects kn_state.
4370Sstevel@tonic-gate 	 */
4380Sstevel@tonic-gate 	kmutex_t			kn_lock;
4390Sstevel@tonic-gate 	kcondvar_t			kn_cv;
4400Sstevel@tonic-gate 	ntfy_elem_state_t		kn_state;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	struct kcf_ntfy_elem		*kn_next;
4430Sstevel@tonic-gate 	struct kcf_ntfy_elem		*kn_prev;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	crypto_notify_callback_t	kn_func;
4460Sstevel@tonic-gate 	uint32_t			kn_event_mask;
4470Sstevel@tonic-gate } kcf_ntfy_elem_t;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate /*
4510Sstevel@tonic-gate  * The following values are based on the assumption that it would
4520Sstevel@tonic-gate  * take around eight cpus to load a hardware provider (This is true for
4530Sstevel@tonic-gate  * at least one product) and a kernel client may come from different
4540Sstevel@tonic-gate  * low-priority interrupt levels. We will have CYRPTO_TASKQ_MIN number
4554494Skrishna  * of cached taskq entries. The CRYPTO_TASKQ_MAX number is based on
4564494Skrishna  * a throughput of 1GB/s using 512-byte buffers. These are just
4574494Skrishna  * reasonable estimates and might need to change in future.
4580Sstevel@tonic-gate  */
4594494Skrishna #define	CRYPTO_TASKQ_THREADS	8
4600Sstevel@tonic-gate #define	CYRPTO_TASKQ_MIN	64
4614494Skrishna #define	CRYPTO_TASKQ_MAX	2 * 1024 * 1024
4620Sstevel@tonic-gate 
4634494Skrishna extern int crypto_taskq_threads;
4640Sstevel@tonic-gate extern int crypto_taskq_minalloc;
4650Sstevel@tonic-gate extern int crypto_taskq_maxalloc;
4660Sstevel@tonic-gate extern kcf_global_swq_t *gswq;
4670Sstevel@tonic-gate extern int kcf_maxthreads;
4680Sstevel@tonic-gate extern int kcf_minthreads;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate /* Door handle for talking to kcfd */
4710Sstevel@tonic-gate extern door_handle_t kcf_dh;
4720Sstevel@tonic-gate extern kmutex_t	 kcf_dh_lock;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * All pending crypto bufcalls are put on a list. cbuf_list_lock
4760Sstevel@tonic-gate  * protects changes to this list.
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate extern kmutex_t cbuf_list_lock;
4790Sstevel@tonic-gate extern kcondvar_t cbuf_list_cv;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /*
4820Sstevel@tonic-gate  * All event subscribers are put on a list. kcf_notify_list_lock
4830Sstevel@tonic-gate  * protects changes to this list.
4840Sstevel@tonic-gate  */
4850Sstevel@tonic-gate extern kmutex_t ntfy_list_lock;
4860Sstevel@tonic-gate extern kcondvar_t ntfy_list_cv;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate boolean_t kcf_get_next_logical_provider_member(kcf_provider_desc_t *,
4890Sstevel@tonic-gate     kcf_provider_desc_t *, kcf_provider_desc_t **);
49010444SVladimir.Kotal@Sun.COM extern int kcf_get_hardware_provider(crypto_mech_type_t, crypto_key_t *,
49110444SVladimir.Kotal@Sun.COM     crypto_mech_type_t, crypto_key_t *,
492*12304SValerie.Fenwick@Oracle.COM     kcf_provider_desc_t *, kcf_provider_desc_t **,
4931808Smcpowers     crypto_func_group_t);
4940Sstevel@tonic-gate extern int kcf_get_hardware_provider_nomech(offset_t, offset_t,
495*12304SValerie.Fenwick@Oracle.COM     kcf_provider_desc_t *, kcf_provider_desc_t **);
4960Sstevel@tonic-gate extern void kcf_free_triedlist(kcf_prov_tried_t *);
4970Sstevel@tonic-gate extern kcf_prov_tried_t *kcf_insert_triedlist(kcf_prov_tried_t **,
4980Sstevel@tonic-gate     kcf_provider_desc_t *, int);
4990Sstevel@tonic-gate extern kcf_provider_desc_t *kcf_get_mech_provider(crypto_mech_type_t,
50010444SVladimir.Kotal@Sun.COM     crypto_key_t *, kcf_mech_entry_t **, int *, kcf_prov_tried_t *,
501*12304SValerie.Fenwick@Oracle.COM     crypto_func_group_t, size_t);
5020Sstevel@tonic-gate extern kcf_provider_desc_t *kcf_get_dual_provider(crypto_mechanism_t *,
50310444SVladimir.Kotal@Sun.COM     crypto_key_t *, crypto_mechanism_t *, crypto_key_t *,
50410444SVladimir.Kotal@Sun.COM     kcf_mech_entry_t **, crypto_mech_type_t *,
5050Sstevel@tonic-gate     crypto_mech_type_t *, int *, kcf_prov_tried_t *,
506*12304SValerie.Fenwick@Oracle.COM     crypto_func_group_t, crypto_func_group_t, size_t);
5070Sstevel@tonic-gate extern crypto_ctx_t *kcf_new_ctx(crypto_call_req_t  *, kcf_provider_desc_t *,
5080Sstevel@tonic-gate     crypto_session_id_t);
5090Sstevel@tonic-gate extern int kcf_submit_request(kcf_provider_desc_t *, crypto_ctx_t *,
5100Sstevel@tonic-gate     crypto_call_req_t *, kcf_req_params_t *, boolean_t);
5110Sstevel@tonic-gate extern void kcf_sched_init(void);
5120Sstevel@tonic-gate extern void kcf_sched_start(void);
5130Sstevel@tonic-gate extern void kcf_sop_done(kcf_sreq_node_t *, int);
5140Sstevel@tonic-gate extern void kcf_aop_done(kcf_areq_node_t *, int);
5150Sstevel@tonic-gate extern int common_submit_request(kcf_provider_desc_t *,
5160Sstevel@tonic-gate     crypto_ctx_t *, kcf_req_params_t *, crypto_req_handle_t);
5170Sstevel@tonic-gate extern void kcf_free_context(kcf_context_t *);
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate extern int kcf_svc_wait(int *);
5200Sstevel@tonic-gate extern int kcf_svc_do_run(void);
52110732SAnthony.Scarpino@Sun.COM extern int kcf_need_fips140_verification(kcf_provider_desc_t *);
5224373Skrishna extern int kcf_need_signature_verification(kcf_provider_desc_t *);
5234373Skrishna extern void kcf_verify_signature(void *);
5240Sstevel@tonic-gate extern struct modctl *kcf_get_modctl(crypto_provider_info_t *);
5250Sstevel@tonic-gate extern void verify_unverified_providers();
5260Sstevel@tonic-gate extern void kcf_free_req(kcf_areq_node_t *areq);
5270Sstevel@tonic-gate extern void crypto_bufcall_service(void);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate extern void kcf_walk_ntfylist(uint32_t, void *);
5304373Skrishna extern void kcf_do_notify(kcf_provider_desc_t *, boolean_t);
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate extern kcf_dual_req_t *kcf_alloc_req(crypto_call_req_t *);
5330Sstevel@tonic-gate extern void kcf_next_req(void *, int);
5340Sstevel@tonic-gate extern void kcf_last_req(void *, int);
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate #ifdef __cplusplus
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate #endif
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate #endif /* _SYS_CRYPTO_SCHED_IMPL_H */
541