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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * This file contains the core framework routines for the
28  * kernel cryptographic framework. These routines are at the
29  * layer, between the kernel API/ioctls and the SPI.
30  */
31 
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/kmem.h>
35 #include <sys/proc.h>
36 #include <sys/cpuvar.h>
37 #include <sys/cpupart.h>
38 #include <sys/ksynch.h>
39 #include <sys/callb.h>
40 #include <sys/cmn_err.h>
41 #include <sys/systm.h>
42 #include <sys/sysmacros.h>
43 #include <sys/kstat.h>
44 #include <sys/crypto/common.h>
45 #include <sys/crypto/impl.h>
46 #include <sys/crypto/sched_impl.h>
47 #include <sys/crypto/api.h>
48 #include <sys/crypto/spi.h>
49 #include <sys/taskq_impl.h>
50 #include <sys/ddi.h>
51 #include <sys/sunddi.h>
52 
53 
54 kcf_global_swq_t *gswq;	/* Global software queue */
55 
56 /* Thread pool related variables */
57 static kcf_pool_t *kcfpool;	/* Thread pool of kcfd LWPs */
58 int kcf_maxthreads = 2;
59 int kcf_minthreads = 1;
60 int kcf_thr_multiple = 2;	/* Boot-time tunable for experimentation */
61 static ulong_t	kcf_idlethr_timeout;
62 static boolean_t kcf_sched_running = B_FALSE;
63 #define	KCF_DEFAULT_THRTIMEOUT	60000000	/* 60 seconds */
64 
65 /* kmem caches used by the scheduler */
66 static struct kmem_cache *kcf_sreq_cache;
67 static struct kmem_cache *kcf_areq_cache;
68 static struct kmem_cache *kcf_context_cache;
69 
70 /* Global request ID table */
71 static kcf_reqid_table_t *kcf_reqid_table[REQID_TABLES];
72 
73 /* KCF stats. Not protected. */
74 static kcf_stats_t kcf_ksdata = {
75 	{ "total threads in pool",	KSTAT_DATA_UINT32},
76 	{ "idle threads in pool",	KSTAT_DATA_UINT32},
77 	{ "min threads in pool",	KSTAT_DATA_UINT32},
78 	{ "max threads in pool",	KSTAT_DATA_UINT32},
79 	{ "requests in gswq",		KSTAT_DATA_UINT32},
80 	{ "max requests in gswq",	KSTAT_DATA_UINT32},
81 	{ "threads for HW taskq",	KSTAT_DATA_UINT32},
82 	{ "minalloc for HW taskq",	KSTAT_DATA_UINT32},
83 	{ "maxalloc for HW taskq",	KSTAT_DATA_UINT32}
84 };
85 
86 static kstat_t *kcf_misc_kstat = NULL;
87 ulong_t kcf_swprov_hndl = 0;
88 
89 static kcf_areq_node_t *kcf_areqnode_alloc(kcf_provider_desc_t *,
90     kcf_context_t *, crypto_call_req_t *, kcf_req_params_t *, boolean_t);
91 static int kcf_disp_sw_request(kcf_areq_node_t *);
92 static void process_req_hwp(void *);
93 static kcf_areq_node_t	*kcf_dequeue();
94 static int kcf_enqueue(kcf_areq_node_t *);
95 static void kcf_failover_thread();
96 static void kcfpool_alloc();
97 static void kcf_reqid_delete(kcf_areq_node_t *areq);
98 static crypto_req_id_t kcf_reqid_insert(kcf_areq_node_t *areq);
99 static int kcf_misc_kstat_update(kstat_t *ksp, int rw);
100 static void compute_min_max_threads();
101 
102 
103 /*
104  * Create a new context.
105  */
106 crypto_ctx_t *
107 kcf_new_ctx(crypto_call_req_t *crq, kcf_provider_desc_t *pd,
108     crypto_session_id_t sid)
109 {
110 	crypto_ctx_t *ctx;
111 	kcf_context_t *kcf_ctx;
112 
113 	kcf_ctx = kmem_cache_alloc(kcf_context_cache,
114 	    (crq == NULL) ? KM_SLEEP : KM_NOSLEEP);
115 	if (kcf_ctx == NULL)
116 		return (NULL);
117 
118 	/* initialize the context for the consumer */
119 	kcf_ctx->kc_refcnt = 1;
120 	kcf_ctx->kc_req_chain_first = NULL;
121 	kcf_ctx->kc_req_chain_last = NULL;
122 	kcf_ctx->kc_secondctx = NULL;
123 	KCF_PROV_REFHOLD(pd);
124 	kcf_ctx->kc_prov_desc = pd;
125 	kcf_ctx->kc_sw_prov_desc = NULL;
126 	kcf_ctx->kc_mech = NULL;
127 
128 	ctx = &kcf_ctx->kc_glbl_ctx;
129 	ctx->cc_provider = pd->pd_prov_handle;
130 	ctx->cc_session = sid;
131 	ctx->cc_provider_private = NULL;
132 	ctx->cc_framework_private = (void *)kcf_ctx;
133 	ctx->cc_flags = 0;
134 	ctx->cc_opstate = NULL;
135 
136 	return (ctx);
137 }
138 
139 /*
140  * Allocate a new async request node.
141  *
142  * ictx - Framework private context pointer
143  * crq - Has callback function and argument. Should be non NULL.
144  * req - The parameters to pass to the SPI
145  */
146 static kcf_areq_node_t *
147 kcf_areqnode_alloc(kcf_provider_desc_t *pd, kcf_context_t *ictx,
148     crypto_call_req_t *crq, kcf_req_params_t *req, boolean_t isdual)
149 {
150 	kcf_areq_node_t	*arptr, *areq;
151 
152 	ASSERT(crq != NULL);
153 	arptr = kmem_cache_alloc(kcf_areq_cache, KM_NOSLEEP);
154 	if (arptr == NULL)
155 		return (NULL);
156 
157 	arptr->an_state = REQ_ALLOCATED;
158 	arptr->an_reqarg = *crq;
159 	arptr->an_params = *req;
160 	arptr->an_context = ictx;
161 	arptr->an_isdual = isdual;
162 
163 	arptr->an_next = arptr->an_prev = NULL;
164 	KCF_PROV_REFHOLD(pd);
165 	arptr->an_provider = pd;
166 	arptr->an_tried_plist = NULL;
167 	arptr->an_refcnt = 1;
168 	arptr->an_idnext = arptr->an_idprev = NULL;
169 
170 	/*
171 	 * Requests for context-less operations do not use the
172 	 * fields - an_is_my_turn, and an_ctxchain_next.
173 	 */
174 	if (ictx == NULL)
175 		return (arptr);
176 
177 	KCF_CONTEXT_REFHOLD(ictx);
178 	/*
179 	 * Chain this request to the context.
180 	 */
181 	mutex_enter(&ictx->kc_in_use_lock);
182 	arptr->an_ctxchain_next = NULL;
183 	if ((areq = ictx->kc_req_chain_last) == NULL) {
184 		arptr->an_is_my_turn = B_TRUE;
185 		ictx->kc_req_chain_last =
186 		    ictx->kc_req_chain_first = arptr;
187 	} else {
188 		ASSERT(ictx->kc_req_chain_first != NULL);
189 		arptr->an_is_my_turn = B_FALSE;
190 		/* Insert the new request to the end of the chain. */
191 		areq->an_ctxchain_next = arptr;
192 		ictx->kc_req_chain_last = arptr;
193 	}
194 	mutex_exit(&ictx->kc_in_use_lock);
195 
196 	return (arptr);
197 }
198 
199 /*
200  * Queue the request node and do one of the following:
201  *	- If there is an idle thread signal it to run.
202  *	- If there is no idle thread and max running threads is not
203  *	  reached, signal the creator thread for more threads.
204  *
205  * If the two conditions above are not met, we don't need to do
206  * any thing. The request will be picked up by one of the
207  * worker threads when it becomes available.
208  */
209 static int
210 kcf_disp_sw_request(kcf_areq_node_t *areq)
211 {
212 	int err;
213 	int cnt = 0;
214 
215 	if ((err = kcf_enqueue(areq)) != 0)
216 		return (err);
217 
218 	if (kcfpool->kp_idlethreads > 0) {
219 		/* Signal an idle thread to run */
220 		mutex_enter(&gswq->gs_lock);
221 		cv_signal(&gswq->gs_cv);
222 		mutex_exit(&gswq->gs_lock);
223 
224 		return (CRYPTO_QUEUED);
225 	}
226 
227 	/*
228 	 * We keep the number of running threads to be at
229 	 * kcf_minthreads to reduce gs_lock contention.
230 	 */
231 	cnt = kcf_minthreads -
232 	    (kcfpool->kp_threads - kcfpool->kp_blockedthreads);
233 	if (cnt > 0) {
234 		/*
235 		 * The following ensures the number of threads in pool
236 		 * does not exceed kcf_maxthreads.
237 		 */
238 		cnt = min(cnt, kcf_maxthreads - kcfpool->kp_threads);
239 		if (cnt > 0) {
240 			/* Signal the creator thread for more threads */
241 			mutex_enter(&kcfpool->kp_user_lock);
242 			if (!kcfpool->kp_signal_create_thread) {
243 				kcfpool->kp_signal_create_thread = B_TRUE;
244 				kcfpool->kp_nthrs = cnt;
245 				cv_signal(&kcfpool->kp_user_cv);
246 			}
247 			mutex_exit(&kcfpool->kp_user_lock);
248 		}
249 	}
250 
251 	return (CRYPTO_QUEUED);
252 }
253 
254 /*
255  * This routine is called by the taskq associated with
256  * each hardware provider. We notify the kernel consumer
257  * via the callback routine in case of CRYPTO_SUCCESS or
258  * a failure.
259  *
260  * A request can be of type kcf_areq_node_t or of type
261  * kcf_sreq_node_t.
262  */
263 static void
264 process_req_hwp(void *ireq)
265 {
266 	int error = 0;
267 	crypto_ctx_t *ctx;
268 	kcf_call_type_t ctype;
269 	kcf_provider_desc_t *pd;
270 	kcf_areq_node_t *areq = (kcf_areq_node_t *)ireq;
271 	kcf_sreq_node_t *sreq = (kcf_sreq_node_t *)ireq;
272 	kcf_prov_cpu_t *mp;
273 
274 	pd = ((ctype = GET_REQ_TYPE(ireq)) == CRYPTO_SYNCH) ?
275 	    sreq->sn_provider : areq->an_provider;
276 
277 	/*
278 	 * Wait if flow control is in effect for the provider. A
279 	 * CRYPTO_PROVIDER_READY or CRYPTO_PROVIDER_FAILED
280 	 * notification will signal us. We also get signaled if
281 	 * the provider is unregistering.
282 	 */
283 	if (pd->pd_state == KCF_PROV_BUSY) {
284 		mutex_enter(&pd->pd_lock);
285 		while (pd->pd_state == KCF_PROV_BUSY)
286 			cv_wait(&pd->pd_resume_cv, &pd->pd_lock);
287 		mutex_exit(&pd->pd_lock);
288 	}
289 
290 	/*
291 	 * Bump the internal reference count while the request is being
292 	 * processed. This is how we know when it's safe to unregister
293 	 * a provider. This step must precede the pd_state check below.
294 	 */
295 	mp = &(pd->pd_percpu_bins[CPU_SEQID]);
296 	KCF_PROV_JOB_HOLD(mp);
297 
298 	/*
299 	 * Fail the request if the provider has failed. We return a
300 	 * recoverable error and the notified clients attempt any
301 	 * recovery. For async clients this is done in kcf_aop_done()
302 	 * and for sync clients it is done in the k-api routines.
303 	 */
304 	if (pd->pd_state >= KCF_PROV_FAILED) {
305 		error = CRYPTO_DEVICE_ERROR;
306 		goto bail;
307 	}
308 
309 	if (ctype == CRYPTO_SYNCH) {
310 		mutex_enter(&sreq->sn_lock);
311 		sreq->sn_state = REQ_INPROGRESS;
312 		sreq->sn_mp = mp;
313 		mutex_exit(&sreq->sn_lock);
314 
315 		ctx = sreq->sn_context ? &sreq->sn_context->kc_glbl_ctx : NULL;
316 		error = common_submit_request(sreq->sn_provider, ctx,
317 		    sreq->sn_params, sreq);
318 	} else {
319 		kcf_context_t *ictx;
320 		ASSERT(ctype == CRYPTO_ASYNCH);
321 
322 		/*
323 		 * We are in the per-hardware provider thread context and
324 		 * hence can sleep. Note that the caller would have done
325 		 * a taskq_dispatch(..., TQ_NOSLEEP) and would have returned.
326 		 */
327 		ctx = (ictx = areq->an_context) ? &ictx->kc_glbl_ctx : NULL;
328 
329 		mutex_enter(&areq->an_lock);
330 		/*
331 		 * We need to maintain ordering for multi-part requests.
332 		 * an_is_my_turn is set to B_TRUE initially for a request
333 		 * when it is enqueued and there are no other requests
334 		 * for that context. It is set later from kcf_aop_done() when
335 		 * the request before us in the chain of requests for the
336 		 * context completes. We get signaled at that point.
337 		 */
338 		if (ictx != NULL) {
339 			ASSERT(ictx->kc_prov_desc == areq->an_provider);
340 
341 			while (areq->an_is_my_turn == B_FALSE) {
342 				cv_wait(&areq->an_turn_cv, &areq->an_lock);
343 			}
344 		}
345 		areq->an_state = REQ_INPROGRESS;
346 		areq->an_mp = mp;
347 		mutex_exit(&areq->an_lock);
348 
349 		error = common_submit_request(areq->an_provider, ctx,
350 		    &areq->an_params, areq);
351 	}
352 
353 bail:
354 	if (error == CRYPTO_QUEUED) {
355 		/*
356 		 * The request is queued by the provider and we should
357 		 * get a crypto_op_notification() from the provider later.
358 		 * We notify the consumer at that time.
359 		 */
360 		return;
361 	} else {		/* CRYPTO_SUCCESS or other failure */
362 		KCF_PROV_JOB_RELE(mp);
363 		if (ctype == CRYPTO_SYNCH)
364 			kcf_sop_done(sreq, error);
365 		else
366 			kcf_aop_done(areq, error);
367 	}
368 }
369 
370 /*
371  * This routine checks if a request can be retried on another
372  * provider. If true, mech1 is initialized to point to the mechanism
373  * structure. mech2 is also initialized in case of a dual operation. fg
374  * is initialized to the correct crypto_func_group_t bit flag. They are
375  * initialized by this routine, so that the caller can pass them to a
376  * kcf_get_mech_provider() or kcf_get_dual_provider() with no further change.
377  *
378  * We check that the request is for a init or atomic routine and that
379  * it is for one of the operation groups used from k-api .
380  */
381 static boolean_t
382 can_resubmit(kcf_areq_node_t *areq, crypto_mechanism_t **mech1,
383     crypto_mechanism_t **mech2, crypto_func_group_t *fg)
384 {
385 	kcf_req_params_t *params;
386 	kcf_op_type_t optype;
387 
388 	params = &areq->an_params;
389 	optype = params->rp_optype;
390 
391 	if (!(IS_INIT_OP(optype) || IS_ATOMIC_OP(optype)))
392 		return (B_FALSE);
393 
394 	switch (params->rp_opgrp) {
395 	case KCF_OG_DIGEST: {
396 		kcf_digest_ops_params_t *dops = &params->rp_u.digest_params;
397 
398 		dops->do_mech.cm_type = dops->do_framework_mechtype;
399 		*mech1 = &dops->do_mech;
400 		*fg = (optype == KCF_OP_INIT) ? CRYPTO_FG_DIGEST :
401 		    CRYPTO_FG_DIGEST_ATOMIC;
402 		break;
403 	}
404 
405 	case KCF_OG_MAC: {
406 		kcf_mac_ops_params_t *mops = &params->rp_u.mac_params;
407 
408 		mops->mo_mech.cm_type = mops->mo_framework_mechtype;
409 		*mech1 = &mops->mo_mech;
410 		*fg = (optype == KCF_OP_INIT) ? CRYPTO_FG_MAC :
411 		    CRYPTO_FG_MAC_ATOMIC;
412 		break;
413 	}
414 
415 	case KCF_OG_SIGN: {
416 		kcf_sign_ops_params_t *sops = &params->rp_u.sign_params;
417 
418 		sops->so_mech.cm_type = sops->so_framework_mechtype;
419 		*mech1 = &sops->so_mech;
420 		switch (optype) {
421 		case KCF_OP_INIT:
422 			*fg = CRYPTO_FG_SIGN;
423 			break;
424 		case KCF_OP_ATOMIC:
425 			*fg = CRYPTO_FG_SIGN_ATOMIC;
426 			break;
427 		default:
428 			ASSERT(optype == KCF_OP_SIGN_RECOVER_ATOMIC);
429 			*fg = CRYPTO_FG_SIGN_RECOVER_ATOMIC;
430 		}
431 		break;
432 	}
433 
434 	case KCF_OG_VERIFY: {
435 		kcf_verify_ops_params_t *vops = &params->rp_u.verify_params;
436 
437 		vops->vo_mech.cm_type = vops->vo_framework_mechtype;
438 		*mech1 = &vops->vo_mech;
439 		switch (optype) {
440 		case KCF_OP_INIT:
441 			*fg = CRYPTO_FG_VERIFY;
442 			break;
443 		case KCF_OP_ATOMIC:
444 			*fg = CRYPTO_FG_VERIFY_ATOMIC;
445 			break;
446 		default:
447 			ASSERT(optype == KCF_OP_VERIFY_RECOVER_ATOMIC);
448 			*fg = CRYPTO_FG_VERIFY_RECOVER_ATOMIC;
449 		}
450 		break;
451 	}
452 
453 	case KCF_OG_ENCRYPT: {
454 		kcf_encrypt_ops_params_t *eops = &params->rp_u.encrypt_params;
455 
456 		eops->eo_mech.cm_type = eops->eo_framework_mechtype;
457 		*mech1 = &eops->eo_mech;
458 		*fg = (optype == KCF_OP_INIT) ? CRYPTO_FG_ENCRYPT :
459 		    CRYPTO_FG_ENCRYPT_ATOMIC;
460 		break;
461 	}
462 
463 	case KCF_OG_DECRYPT: {
464 		kcf_decrypt_ops_params_t *dcrops = &params->rp_u.decrypt_params;
465 
466 		dcrops->dop_mech.cm_type = dcrops->dop_framework_mechtype;
467 		*mech1 = &dcrops->dop_mech;
468 		*fg = (optype == KCF_OP_INIT) ? CRYPTO_FG_DECRYPT :
469 		    CRYPTO_FG_DECRYPT_ATOMIC;
470 		break;
471 	}
472 
473 	case KCF_OG_ENCRYPT_MAC: {
474 		kcf_encrypt_mac_ops_params_t *eops =
475 		    &params->rp_u.encrypt_mac_params;
476 
477 		eops->em_encr_mech.cm_type = eops->em_framework_encr_mechtype;
478 		*mech1 = &eops->em_encr_mech;
479 		eops->em_mac_mech.cm_type = eops->em_framework_mac_mechtype;
480 		*mech2 = &eops->em_mac_mech;
481 		*fg = (optype == KCF_OP_INIT) ? CRYPTO_FG_ENCRYPT_MAC :
482 		    CRYPTO_FG_ENCRYPT_MAC_ATOMIC;
483 		break;
484 	}
485 
486 	case KCF_OG_MAC_DECRYPT: {
487 		kcf_mac_decrypt_ops_params_t *dops =
488 		    &params->rp_u.mac_decrypt_params;
489 
490 		dops->md_mac_mech.cm_type = dops->md_framework_mac_mechtype;
491 		*mech1 = &dops->md_mac_mech;
492 		dops->md_decr_mech.cm_type = dops->md_framework_decr_mechtype;
493 		*mech2 = &dops->md_decr_mech;
494 		*fg = (optype == KCF_OP_INIT) ? CRYPTO_FG_MAC_DECRYPT :
495 		    CRYPTO_FG_MAC_DECRYPT_ATOMIC;
496 		break;
497 	}
498 
499 	default:
500 		return (B_FALSE);
501 	}
502 
503 	return (B_TRUE);
504 }
505 
506 /*
507  * This routine is called when a request to a provider has failed
508  * with a recoverable error. This routine tries to find another provider
509  * and dispatches the request to the new provider, if one is available.
510  * We reuse the request structure.
511  *
512  * A return value of NULL from kcf_get_mech_provider() indicates
513  * we have tried the last provider.
514  */
515 static int
516 kcf_resubmit_request(kcf_areq_node_t *areq)
517 {
518 	int error = CRYPTO_FAILED;
519 	kcf_context_t *ictx;
520 	kcf_provider_desc_t *old_pd;
521 	kcf_provider_desc_t *new_pd;
522 	crypto_mechanism_t *mech1 = NULL, *mech2 = NULL;
523 	crypto_mech_type_t prov_mt1, prov_mt2;
524 	crypto_func_group_t fg;
525 
526 	if (!can_resubmit(areq, &mech1, &mech2, &fg))
527 		return (error);
528 
529 	old_pd = areq->an_provider;
530 	/*
531 	 * Add old_pd to the list of providers already tried.
532 	 * We release the new hold on old_pd in kcf_free_triedlist().
533 	 */
534 	if (kcf_insert_triedlist(&areq->an_tried_plist, old_pd,
535 	    KM_NOSLEEP | KCF_HOLD_PROV) == NULL)
536 		return (error);
537 
538 	if (mech1 && !mech2) {
539 		new_pd = kcf_get_mech_provider(mech1->cm_type, NULL, NULL,
540 		    &error, areq->an_tried_plist, fg,
541 		    (areq->an_reqarg.cr_flag & CRYPTO_RESTRICTED), 0);
542 	} else {
543 		ASSERT(mech1 != NULL && mech2 != NULL);
544 
545 		new_pd = kcf_get_dual_provider(mech1, NULL, mech2, NULL,
546 		    NULL, &prov_mt1,
547 		    &prov_mt2, &error, areq->an_tried_plist, fg, fg,
548 		    (areq->an_reqarg.cr_flag & CRYPTO_RESTRICTED), 0);
549 	}
550 
551 	if (new_pd == NULL)
552 		return (error);
553 
554 	/*
555 	 * We reuse the old context by resetting provider specific
556 	 * fields in it.
557 	 */
558 	if ((ictx = areq->an_context) != NULL) {
559 		crypto_ctx_t *ctx;
560 
561 		ASSERT(old_pd == ictx->kc_prov_desc);
562 		KCF_PROV_REFRELE(ictx->kc_prov_desc);
563 		KCF_PROV_REFHOLD(new_pd);
564 		ictx->kc_prov_desc = new_pd;
565 
566 		ctx = &ictx->kc_glbl_ctx;
567 		ctx->cc_provider = new_pd->pd_prov_handle;
568 		ctx->cc_session = new_pd->pd_sid;
569 		ctx->cc_provider_private = NULL;
570 	}
571 
572 	/* We reuse areq. by resetting the provider and context fields. */
573 	KCF_PROV_REFRELE(old_pd);
574 	KCF_PROV_REFHOLD(new_pd);
575 	areq->an_provider = new_pd;
576 	mutex_enter(&areq->an_lock);
577 	areq->an_state = REQ_WAITING;
578 	mutex_exit(&areq->an_lock);
579 
580 	switch (new_pd->pd_prov_type) {
581 	case CRYPTO_SW_PROVIDER:
582 		error = kcf_disp_sw_request(areq);
583 		break;
584 
585 	case CRYPTO_HW_PROVIDER: {
586 		taskq_t *taskq = new_pd->pd_taskq;
587 
588 		if (taskq_dispatch(taskq, process_req_hwp, areq, TQ_NOSLEEP) ==
589 		    (taskqid_t)0) {
590 			error = CRYPTO_HOST_MEMORY;
591 		} else {
592 			error = CRYPTO_QUEUED;
593 		}
594 
595 		break;
596 	}
597 	}
598 
599 	KCF_PROV_REFRELE(new_pd);
600 	return (error);
601 }
602 
603 #define	EMPTY_TASKQ(tq)	((tq)->tq_task.tqent_next == &(tq)->tq_task)
604 
605 /*
606  * Routine called by both ioctl and k-api. The consumer should
607  * bundle the parameters into a kcf_req_params_t structure. A bunch
608  * of macros are available in ops_impl.h for this bundling. They are:
609  *
610  * 	KCF_WRAP_DIGEST_OPS_PARAMS()
611  *	KCF_WRAP_MAC_OPS_PARAMS()
612  *	KCF_WRAP_ENCRYPT_OPS_PARAMS()
613  *	KCF_WRAP_DECRYPT_OPS_PARAMS() ... etc.
614  *
615  * It is the caller's responsibility to free the ctx argument when
616  * appropriate. See the KCF_CONTEXT_COND_RELEASE macro for details.
617  */
618 int
619 kcf_submit_request(kcf_provider_desc_t *pd, crypto_ctx_t *ctx,
620     crypto_call_req_t *crq, kcf_req_params_t *params, boolean_t cont)
621 {
622 	int error;
623 	kcf_areq_node_t *areq;
624 	kcf_sreq_node_t *sreq;
625 	kcf_context_t *kcf_ctx;
626 	taskq_t *taskq;
627 	kcf_prov_cpu_t *mp;
628 
629 	kcf_ctx = ctx ? (kcf_context_t *)ctx->cc_framework_private : NULL;
630 
631 	/* Synchronous cases */
632 	if (crq == NULL) {
633 		switch (pd->pd_prov_type) {
634 		case CRYPTO_SW_PROVIDER:
635 			error = common_submit_request(pd, ctx, params,
636 			    KCF_RHNDL(KM_SLEEP));
637 			break;
638 
639 		case CRYPTO_HW_PROVIDER:
640 			taskq = pd->pd_taskq;
641 
642 			/*
643 			 * Special case for CRYPTO_SYNCHRONOUS providers that
644 			 * never return a CRYPTO_QUEUED error. We skip any
645 			 * request allocation and call the SPI directly.
646 			 */
647 			if ((pd->pd_flags & CRYPTO_SYNCHRONOUS) &&
648 			    EMPTY_TASKQ(taskq)) {
649 				mp = &(pd->pd_percpu_bins[CPU_SEQID]);
650 				KCF_PROV_JOB_HOLD(mp);
651 
652 				if (pd->pd_state == KCF_PROV_READY) {
653 					error = common_submit_request(pd, ctx,
654 					    params, KCF_RHNDL(KM_SLEEP));
655 					KCF_PROV_JOB_RELE(mp);
656 					ASSERT(error != CRYPTO_QUEUED);
657 					break;
658 				}
659 				KCF_PROV_JOB_RELE(mp);
660 			}
661 
662 			sreq = kmem_cache_alloc(kcf_sreq_cache, KM_SLEEP);
663 			sreq->sn_state = REQ_ALLOCATED;
664 			sreq->sn_rv = CRYPTO_FAILED;
665 			sreq->sn_params = params;
666 
667 			/*
668 			 * Note that we do not need to hold the context
669 			 * for synchronous case as the context will never
670 			 * become invalid underneath us. We do not need to hold
671 			 * the provider here either as the caller has a hold.
672 			 */
673 			sreq->sn_context = kcf_ctx;
674 			ASSERT(KCF_PROV_REFHELD(pd));
675 			sreq->sn_provider = pd;
676 
677 			ASSERT(taskq != NULL);
678 			/*
679 			 * Call the SPI directly if the taskq is empty and the
680 			 * provider is not busy, else dispatch to the taskq.
681 			 * Calling directly is fine as this is the synchronous
682 			 * case. This is unlike the asynchronous case where we
683 			 * must always dispatch to the taskq.
684 			 */
685 			if (EMPTY_TASKQ(taskq) &&
686 			    pd->pd_state == KCF_PROV_READY) {
687 				process_req_hwp(sreq);
688 			} else {
689 				/*
690 				 * We can not tell from taskq_dispatch() return
691 				 * value if we exceeded maxalloc. Hence the
692 				 * check here. Since we are allowed to wait in
693 				 * the synchronous case, we wait for the taskq
694 				 * to become empty.
695 				 */
696 				if (taskq->tq_nalloc >= crypto_taskq_maxalloc) {
697 					taskq_wait(taskq);
698 				}
699 
700 				(void) taskq_dispatch(taskq, process_req_hwp,
701 				    sreq, TQ_SLEEP);
702 			}
703 
704 			/*
705 			 * Wait for the notification to arrive,
706 			 * if the operation is not done yet.
707 			 * Bug# 4722589 will make the wait a cv_wait_sig().
708 			 */
709 			mutex_enter(&sreq->sn_lock);
710 			while (sreq->sn_state < REQ_DONE)
711 				cv_wait(&sreq->sn_cv, &sreq->sn_lock);
712 			mutex_exit(&sreq->sn_lock);
713 
714 			error = sreq->sn_rv;
715 			kmem_cache_free(kcf_sreq_cache, sreq);
716 
717 			break;
718 
719 		default:
720 			error = CRYPTO_FAILED;
721 			break;
722 		}
723 
724 	} else {	/* Asynchronous cases */
725 		switch (pd->pd_prov_type) {
726 		case CRYPTO_SW_PROVIDER:
727 			if (!(crq->cr_flag & CRYPTO_ALWAYS_QUEUE)) {
728 				/*
729 				 * This case has less overhead since there is
730 				 * no switching of context.
731 				 */
732 				error = common_submit_request(pd, ctx, params,
733 				    KCF_RHNDL(KM_NOSLEEP));
734 			} else {
735 				/*
736 				 * CRYPTO_ALWAYS_QUEUE is set. We need to
737 				 * queue the request and return.
738 				 */
739 				areq = kcf_areqnode_alloc(pd, kcf_ctx, crq,
740 				    params, cont);
741 				if (areq == NULL)
742 					error = CRYPTO_HOST_MEMORY;
743 				else {
744 					if (!(crq->cr_flag
745 					    & CRYPTO_SKIP_REQID)) {
746 					/*
747 					 * Set the request handle. This handle
748 					 * is used for any crypto_cancel_req(9f)
749 					 * calls from the consumer. We have to
750 					 * do this before dispatching the
751 					 * request.
752 					 */
753 					crq->cr_reqid = kcf_reqid_insert(areq);
754 					}
755 
756 					error = kcf_disp_sw_request(areq);
757 					/*
758 					 * There is an error processing this
759 					 * request. Remove the handle and
760 					 * release the request structure.
761 					 */
762 					if (error != CRYPTO_QUEUED) {
763 						if (!(crq->cr_flag
764 						    & CRYPTO_SKIP_REQID))
765 							kcf_reqid_delete(areq);
766 						KCF_AREQ_REFRELE(areq);
767 					}
768 				}
769 			}
770 			break;
771 
772 		case CRYPTO_HW_PROVIDER:
773 			/*
774 			 * We need to queue the request and return.
775 			 */
776 			areq = kcf_areqnode_alloc(pd, kcf_ctx, crq, params,
777 			    cont);
778 			if (areq == NULL) {
779 				error = CRYPTO_HOST_MEMORY;
780 				goto done;
781 			}
782 
783 			taskq = pd->pd_taskq;
784 			ASSERT(taskq != NULL);
785 			/*
786 			 * We can not tell from taskq_dispatch() return
787 			 * value if we exceeded maxalloc. Hence the check
788 			 * here.
789 			 */
790 			if (taskq->tq_nalloc >= crypto_taskq_maxalloc) {
791 				error = CRYPTO_BUSY;
792 				KCF_AREQ_REFRELE(areq);
793 				goto done;
794 			}
795 
796 			if (!(crq->cr_flag & CRYPTO_SKIP_REQID)) {
797 			/*
798 			 * Set the request handle. This handle is used
799 			 * for any crypto_cancel_req(9f) calls from the
800 			 * consumer. We have to do this before dispatching
801 			 * the request.
802 			 */
803 			crq->cr_reqid = kcf_reqid_insert(areq);
804 			}
805 
806 			if (taskq_dispatch(taskq,
807 			    process_req_hwp, areq, TQ_NOSLEEP) ==
808 			    (taskqid_t)0) {
809 				error = CRYPTO_HOST_MEMORY;
810 				if (!(crq->cr_flag & CRYPTO_SKIP_REQID))
811 					kcf_reqid_delete(areq);
812 				KCF_AREQ_REFRELE(areq);
813 			} else {
814 				error = CRYPTO_QUEUED;
815 			}
816 			break;
817 
818 		default:
819 			error = CRYPTO_FAILED;
820 			break;
821 		}
822 	}
823 
824 done:
825 	return (error);
826 }
827 
828 /*
829  * We're done with this framework context, so free it. Note that freeing
830  * framework context (kcf_context) frees the global context (crypto_ctx).
831  *
832  * The provider is responsible for freeing provider private context after a
833  * final or single operation and resetting the cc_provider_private field
834  * to NULL. It should do this before it notifies the framework of the
835  * completion. We still need to call KCF_PROV_FREE_CONTEXT to handle cases
836  * like crypto_cancel_ctx(9f).
837  */
838 void
839 kcf_free_context(kcf_context_t *kcf_ctx)
840 {
841 	kcf_provider_desc_t *pd = kcf_ctx->kc_prov_desc;
842 	crypto_ctx_t *gctx = &kcf_ctx->kc_glbl_ctx;
843 	kcf_context_t *kcf_secondctx = kcf_ctx->kc_secondctx;
844 	kcf_prov_cpu_t *mp;
845 
846 	/* Release the second context, if any */
847 
848 	if (kcf_secondctx != NULL)
849 		KCF_CONTEXT_REFRELE(kcf_secondctx);
850 
851 	if (gctx->cc_provider_private != NULL) {
852 		mutex_enter(&pd->pd_lock);
853 		if (!KCF_IS_PROV_REMOVED(pd)) {
854 			/*
855 			 * Increment the provider's internal refcnt so it
856 			 * doesn't unregister from the framework while
857 			 * we're calling the entry point.
858 			 */
859 			mp = &(pd->pd_percpu_bins[CPU_SEQID]);
860 			KCF_PROV_JOB_HOLD(mp);
861 			mutex_exit(&pd->pd_lock);
862 			(void) KCF_PROV_FREE_CONTEXT(pd, gctx);
863 			KCF_PROV_JOB_RELE(mp);
864 		} else {
865 			mutex_exit(&pd->pd_lock);
866 		}
867 	}
868 
869 	/* kcf_ctx->kc_prov_desc has a hold on pd */
870 	KCF_PROV_REFRELE(kcf_ctx->kc_prov_desc);
871 
872 	/* check if this context is shared with a software provider */
873 	if ((gctx->cc_flags & CRYPTO_INIT_OPSTATE) &&
874 	    kcf_ctx->kc_sw_prov_desc != NULL) {
875 		KCF_PROV_REFRELE(kcf_ctx->kc_sw_prov_desc);
876 	}
877 
878 	kmem_cache_free(kcf_context_cache, kcf_ctx);
879 }
880 
881 /*
882  * Free the request after releasing all the holds.
883  */
884 void
885 kcf_free_req(kcf_areq_node_t *areq)
886 {
887 	KCF_PROV_REFRELE(areq->an_provider);
888 	if (areq->an_context != NULL)
889 		KCF_CONTEXT_REFRELE(areq->an_context);
890 
891 	if (areq->an_tried_plist != NULL)
892 		kcf_free_triedlist(areq->an_tried_plist);
893 	kmem_cache_free(kcf_areq_cache, areq);
894 }
895 
896 /*
897  * Utility routine to remove a request from the chain of requests
898  * hanging off a context.
899  */
900 void
901 kcf_removereq_in_ctxchain(kcf_context_t *ictx, kcf_areq_node_t *areq)
902 {
903 	kcf_areq_node_t *cur, *prev;
904 
905 	/*
906 	 * Get context lock, search for areq in the chain and remove it.
907 	 */
908 	ASSERT(ictx != NULL);
909 	mutex_enter(&ictx->kc_in_use_lock);
910 	prev = cur = ictx->kc_req_chain_first;
911 
912 	while (cur != NULL) {
913 		if (cur == areq) {
914 			if (prev == cur) {
915 				if ((ictx->kc_req_chain_first =
916 				    cur->an_ctxchain_next) == NULL)
917 					ictx->kc_req_chain_last = NULL;
918 			} else {
919 				if (cur == ictx->kc_req_chain_last)
920 					ictx->kc_req_chain_last = prev;
921 				prev->an_ctxchain_next = cur->an_ctxchain_next;
922 			}
923 
924 			break;
925 		}
926 		prev = cur;
927 		cur = cur->an_ctxchain_next;
928 	}
929 	mutex_exit(&ictx->kc_in_use_lock);
930 }
931 
932 /*
933  * Remove the specified node from the global software queue.
934  *
935  * The caller must hold the queue lock and request lock (an_lock).
936  */
937 void
938 kcf_remove_node(kcf_areq_node_t *node)
939 {
940 	kcf_areq_node_t *nextp = node->an_next;
941 	kcf_areq_node_t *prevp = node->an_prev;
942 
943 	ASSERT(mutex_owned(&gswq->gs_lock));
944 
945 	if (nextp != NULL)
946 		nextp->an_prev = prevp;
947 	else
948 		gswq->gs_last = prevp;
949 
950 	if (prevp != NULL)
951 		prevp->an_next = nextp;
952 	else
953 		gswq->gs_first = nextp;
954 
955 	ASSERT(mutex_owned(&node->an_lock));
956 	node->an_state = REQ_CANCELED;
957 }
958 
959 /*
960  * Remove and return the first node in the global software queue.
961  *
962  * The caller must hold the queue lock.
963  */
964 static kcf_areq_node_t *
965 kcf_dequeue()
966 {
967 	kcf_areq_node_t *tnode = NULL;
968 
969 	ASSERT(mutex_owned(&gswq->gs_lock));
970 	if ((tnode = gswq->gs_first) == NULL) {
971 		return (NULL);
972 	} else {
973 		ASSERT(gswq->gs_first->an_prev == NULL);
974 		gswq->gs_first = tnode->an_next;
975 		if (tnode->an_next == NULL)
976 			gswq->gs_last = NULL;
977 		else
978 			tnode->an_next->an_prev = NULL;
979 	}
980 
981 	gswq->gs_njobs--;
982 	return (tnode);
983 }
984 
985 /*
986  * Add the request node to the end of the global software queue.
987  *
988  * The caller should not hold the queue lock. Returns 0 if the
989  * request is successfully queued. Returns CRYPTO_BUSY if the limit
990  * on the number of jobs is exceeded.
991  */
992 static int
993 kcf_enqueue(kcf_areq_node_t *node)
994 {
995 	kcf_areq_node_t *tnode;
996 
997 	mutex_enter(&gswq->gs_lock);
998 
999 	if (gswq->gs_njobs >= gswq->gs_maxjobs) {
1000 		mutex_exit(&gswq->gs_lock);
1001 		return (CRYPTO_BUSY);
1002 	}
1003 
1004 	if (gswq->gs_last == NULL) {
1005 		gswq->gs_first = gswq->gs_last = node;
1006 	} else {
1007 		ASSERT(gswq->gs_last->an_next == NULL);
1008 		tnode = gswq->gs_last;
1009 		tnode->an_next = node;
1010 		gswq->gs_last = node;
1011 		node->an_prev = tnode;
1012 	}
1013 
1014 	gswq->gs_njobs++;
1015 
1016 	/* an_lock not needed here as we hold gs_lock */
1017 	node->an_state = REQ_WAITING;
1018 
1019 	mutex_exit(&gswq->gs_lock);
1020 
1021 	return (0);
1022 }
1023 
1024 /*
1025  * Decrement the thread pool count and signal the failover
1026  * thread if we are the last one out.
1027  */
1028 static void
1029 kcf_decrcnt_andsignal()
1030 {
1031 	KCF_ATOMIC_DECR(kcfpool->kp_threads);
1032 
1033 	mutex_enter(&kcfpool->kp_thread_lock);
1034 	if (kcfpool->kp_threads == 0)
1035 		cv_signal(&kcfpool->kp_nothr_cv);
1036 	mutex_exit(&kcfpool->kp_thread_lock);
1037 }
1038 
1039 /*
1040  * Function run by a thread from kcfpool to work on global software queue.
1041  * It is called from ioctl(CRYPTO_POOL_RUN, ...).
1042  */
1043 int
1044 kcf_svc_do_run(void)
1045 {
1046 	int error = 0;
1047 	clock_t rv;
1048 	clock_t timeout_val;
1049 	kcf_areq_node_t *req;
1050 	kcf_context_t *ictx;
1051 	kcf_provider_desc_t *pd;
1052 
1053 	KCF_ATOMIC_INCR(kcfpool->kp_threads);
1054 
1055 	for (;;) {
1056 		mutex_enter(&gswq->gs_lock);
1057 
1058 		while ((req = kcf_dequeue()) == NULL) {
1059 			timeout_val = ddi_get_lbolt() +
1060 			    drv_usectohz(kcf_idlethr_timeout);
1061 
1062 			KCF_ATOMIC_INCR(kcfpool->kp_idlethreads);
1063 			rv = cv_timedwait_sig(&gswq->gs_cv, &gswq->gs_lock,
1064 			    timeout_val);
1065 			KCF_ATOMIC_DECR(kcfpool->kp_idlethreads);
1066 
1067 			switch (rv) {
1068 			case 0:
1069 				/*
1070 				 * A signal (as in kill(2)) is pending. We did
1071 				 * not get any cv_signal().
1072 				 */
1073 				kcf_decrcnt_andsignal();
1074 				mutex_exit(&gswq->gs_lock);
1075 				return (EINTR);
1076 
1077 			case -1:
1078 				/*
1079 				 * Timed out and we are not signaled. Let us
1080 				 * see if this thread should exit. We should
1081 				 * keep at least kcf_minthreads.
1082 				 */
1083 				if (kcfpool->kp_threads > kcf_minthreads) {
1084 					kcf_decrcnt_andsignal();
1085 					mutex_exit(&gswq->gs_lock);
1086 					return (0);
1087 				}
1088 
1089 				/* Resume the wait for work */
1090 				break;
1091 
1092 			default:
1093 				/*
1094 				 * We are signaled to work on the queue.
1095 				 */
1096 				break;
1097 			}
1098 		}
1099 
1100 		mutex_exit(&gswq->gs_lock);
1101 
1102 		ictx = req->an_context;
1103 		if (ictx == NULL) {	/* Context-less operation */
1104 			pd = req->an_provider;
1105 			error = common_submit_request(pd, NULL,
1106 			    &req->an_params, req);
1107 			kcf_aop_done(req, error);
1108 			continue;
1109 		}
1110 
1111 		/*
1112 		 * We check if we can work on the request now.
1113 		 * Solaris does not guarantee any order on how the threads
1114 		 * are scheduled or how the waiters on a mutex are chosen.
1115 		 * So, we need to maintain our own order.
1116 		 *
1117 		 * is_my_turn is set to B_TRUE initially for a request when
1118 		 * it is enqueued and there are no other requests
1119 		 * for that context.  Note that a thread sleeping on
1120 		 * an_turn_cv is not counted as an idle thread. This is
1121 		 * because we define an idle thread as one that sleeps on the
1122 		 * global queue waiting for new requests.
1123 		 */
1124 		mutex_enter(&req->an_lock);
1125 		while (req->an_is_my_turn == B_FALSE) {
1126 			KCF_ATOMIC_INCR(kcfpool->kp_blockedthreads);
1127 			cv_wait(&req->an_turn_cv, &req->an_lock);
1128 			KCF_ATOMIC_DECR(kcfpool->kp_blockedthreads);
1129 		}
1130 
1131 		req->an_state = REQ_INPROGRESS;
1132 		mutex_exit(&req->an_lock);
1133 
1134 		pd = ictx->kc_prov_desc;
1135 		ASSERT(pd == req->an_provider);
1136 		error = common_submit_request(pd, &ictx->kc_glbl_ctx,
1137 		    &req->an_params, req);
1138 
1139 		kcf_aop_done(req, error);
1140 	}
1141 }
1142 
1143 /*
1144  * kmem_cache_alloc constructor for sync request structure.
1145  */
1146 /* ARGSUSED */
1147 static int
1148 kcf_sreq_cache_constructor(void *buf, void *cdrarg, int kmflags)
1149 {
1150 	kcf_sreq_node_t *sreq = (kcf_sreq_node_t *)buf;
1151 
1152 	sreq->sn_type = CRYPTO_SYNCH;
1153 	cv_init(&sreq->sn_cv, NULL, CV_DEFAULT, NULL);
1154 	mutex_init(&sreq->sn_lock, NULL, MUTEX_DEFAULT, NULL);
1155 
1156 	return (0);
1157 }
1158 
1159 /* ARGSUSED */
1160 static void
1161 kcf_sreq_cache_destructor(void *buf, void *cdrarg)
1162 {
1163 	kcf_sreq_node_t *sreq = (kcf_sreq_node_t *)buf;
1164 
1165 	mutex_destroy(&sreq->sn_lock);
1166 	cv_destroy(&sreq->sn_cv);
1167 }
1168 
1169 /*
1170  * kmem_cache_alloc constructor for async request structure.
1171  */
1172 /* ARGSUSED */
1173 static int
1174 kcf_areq_cache_constructor(void *buf, void *cdrarg, int kmflags)
1175 {
1176 	kcf_areq_node_t *areq = (kcf_areq_node_t *)buf;
1177 
1178 	areq->an_type = CRYPTO_ASYNCH;
1179 	areq->an_refcnt = 0;
1180 	mutex_init(&areq->an_lock, NULL, MUTEX_DEFAULT, NULL);
1181 	cv_init(&areq->an_done, NULL, CV_DEFAULT, NULL);
1182 	cv_init(&areq->an_turn_cv, NULL, CV_DEFAULT, NULL);
1183 
1184 	return (0);
1185 }
1186 
1187 /* ARGSUSED */
1188 static void
1189 kcf_areq_cache_destructor(void *buf, void *cdrarg)
1190 {
1191 	kcf_areq_node_t *areq = (kcf_areq_node_t *)buf;
1192 
1193 	ASSERT(areq->an_refcnt == 0);
1194 	mutex_destroy(&areq->an_lock);
1195 	cv_destroy(&areq->an_done);
1196 	cv_destroy(&areq->an_turn_cv);
1197 }
1198 
1199 /*
1200  * kmem_cache_alloc constructor for kcf_context structure.
1201  */
1202 /* ARGSUSED */
1203 static int
1204 kcf_context_cache_constructor(void *buf, void *cdrarg, int kmflags)
1205 {
1206 	kcf_context_t *kctx = (kcf_context_t *)buf;
1207 
1208 	kctx->kc_refcnt = 0;
1209 	mutex_init(&kctx->kc_in_use_lock, NULL, MUTEX_DEFAULT, NULL);
1210 
1211 	return (0);
1212 }
1213 
1214 /* ARGSUSED */
1215 static void
1216 kcf_context_cache_destructor(void *buf, void *cdrarg)
1217 {
1218 	kcf_context_t *kctx = (kcf_context_t *)buf;
1219 
1220 	ASSERT(kctx->kc_refcnt == 0);
1221 	mutex_destroy(&kctx->kc_in_use_lock);
1222 }
1223 
1224 /*
1225  * Creates and initializes all the structures needed by the framework.
1226  */
1227 void
1228 kcf_sched_init(void)
1229 {
1230 	int i;
1231 	kcf_reqid_table_t *rt;
1232 
1233 	/*
1234 	 * Create all the kmem caches needed by the framework. We set the
1235 	 * align argument to 64, to get a slab aligned to 64-byte as well as
1236 	 * have the objects (cache_chunksize) to be a 64-byte multiple.
1237 	 * This helps to avoid false sharing as this is the size of the
1238 	 * CPU cache line.
1239 	 */
1240 	kcf_sreq_cache = kmem_cache_create("kcf_sreq_cache",
1241 	    sizeof (struct kcf_sreq_node), 64, kcf_sreq_cache_constructor,
1242 	    kcf_sreq_cache_destructor, NULL, NULL, NULL, 0);
1243 
1244 	kcf_areq_cache = kmem_cache_create("kcf_areq_cache",
1245 	    sizeof (struct kcf_areq_node), 64, kcf_areq_cache_constructor,
1246 	    kcf_areq_cache_destructor, NULL, NULL, NULL, 0);
1247 
1248 	kcf_context_cache = kmem_cache_create("kcf_context_cache",
1249 	    sizeof (struct kcf_context), 64, kcf_context_cache_constructor,
1250 	    kcf_context_cache_destructor, NULL, NULL, NULL, 0);
1251 
1252 	mutex_init(&kcf_dh_lock, NULL, MUTEX_DEFAULT, NULL);
1253 
1254 	gswq = kmem_alloc(sizeof (kcf_global_swq_t), KM_SLEEP);
1255 
1256 	mutex_init(&gswq->gs_lock, NULL, MUTEX_DEFAULT, NULL);
1257 	cv_init(&gswq->gs_cv, NULL, CV_DEFAULT, NULL);
1258 	gswq->gs_njobs = 0;
1259 	gswq->gs_maxjobs = kcf_maxthreads * crypto_taskq_maxalloc;
1260 	gswq->gs_first = gswq->gs_last = NULL;
1261 
1262 	/* Initialize the global reqid table */
1263 	for (i = 0; i < REQID_TABLES; i++) {
1264 		rt = kmem_zalloc(sizeof (kcf_reqid_table_t), KM_SLEEP);
1265 		kcf_reqid_table[i] = rt;
1266 		mutex_init(&rt->rt_lock, NULL, MUTEX_DEFAULT, NULL);
1267 		rt->rt_curid = i;
1268 	}
1269 
1270 	/* Allocate and initialize the thread pool */
1271 	kcfpool_alloc();
1272 
1273 	/* Initialize the event notification list variables */
1274 	mutex_init(&ntfy_list_lock, NULL, MUTEX_DEFAULT, NULL);
1275 	cv_init(&ntfy_list_cv, NULL, CV_DEFAULT, NULL);
1276 
1277 	/* Initialize the crypto_bufcall list variables */
1278 	mutex_init(&cbuf_list_lock, NULL, MUTEX_DEFAULT, NULL);
1279 	cv_init(&cbuf_list_cv, NULL, CV_DEFAULT, NULL);
1280 
1281 	/* Create the kcf kstat */
1282 	kcf_misc_kstat = kstat_create("kcf", 0, "framework_stats", "crypto",
1283 	    KSTAT_TYPE_NAMED, sizeof (kcf_stats_t) / sizeof (kstat_named_t),
1284 	    KSTAT_FLAG_VIRTUAL);
1285 
1286 	if (kcf_misc_kstat != NULL) {
1287 		kcf_misc_kstat->ks_data = &kcf_ksdata;
1288 		kcf_misc_kstat->ks_update = kcf_misc_kstat_update;
1289 		kstat_install(kcf_misc_kstat);
1290 	}
1291 }
1292 
1293 /*
1294  * This routine should only be called by drv/cryptoadm.
1295  *
1296  * kcf_sched_running flag isn't protected by a lock. But, we are safe because
1297  * the first thread ("cryptoadm refresh") calling this routine during
1298  * boot time completes before any other thread that can call this routine.
1299  */
1300 void
1301 kcf_sched_start(void)
1302 {
1303 	if (kcf_sched_running)
1304 		return;
1305 
1306 	/* Start the failover kernel thread for now */
1307 	(void) thread_create(NULL, 0, &kcf_failover_thread, 0, 0, &p0,
1308 	    TS_RUN, minclsyspri);
1309 
1310 	/* Start the background processing thread. */
1311 	(void) thread_create(NULL, 0, &crypto_bufcall_service, 0, 0, &p0,
1312 	    TS_RUN, minclsyspri);
1313 
1314 	kcf_sched_running = B_TRUE;
1315 }
1316 
1317 /*
1318  * Signal the waiting sync client.
1319  */
1320 void
1321 kcf_sop_done(kcf_sreq_node_t *sreq, int error)
1322 {
1323 	mutex_enter(&sreq->sn_lock);
1324 	sreq->sn_state = REQ_DONE;
1325 	sreq->sn_rv = error;
1326 	cv_signal(&sreq->sn_cv);
1327 	mutex_exit(&sreq->sn_lock);
1328 }
1329 
1330 /*
1331  * Callback the async client with the operation status.
1332  * We free the async request node and possibly the context.
1333  * We also handle any chain of requests hanging off of
1334  * the context.
1335  */
1336 void
1337 kcf_aop_done(kcf_areq_node_t *areq, int error)
1338 {
1339 	kcf_op_type_t optype;
1340 	boolean_t skip_notify = B_FALSE;
1341 	kcf_context_t *ictx;
1342 	kcf_areq_node_t *nextreq;
1343 
1344 	/*
1345 	 * Handle recoverable errors. This has to be done first
1346 	 * before doing any thing else in this routine so that
1347 	 * we do not change the state of the request.
1348 	 */
1349 	if (error != CRYPTO_SUCCESS && IS_RECOVERABLE(error)) {
1350 		/*
1351 		 * We try another provider, if one is available. Else
1352 		 * we continue with the failure notification to the
1353 		 * client.
1354 		 */
1355 		if (kcf_resubmit_request(areq) == CRYPTO_QUEUED)
1356 			return;
1357 	}
1358 
1359 	mutex_enter(&areq->an_lock);
1360 	areq->an_state = REQ_DONE;
1361 	mutex_exit(&areq->an_lock);
1362 
1363 	optype = (&areq->an_params)->rp_optype;
1364 	if ((ictx = areq->an_context) != NULL) {
1365 		/*
1366 		 * A request after it is removed from the request
1367 		 * queue, still stays on a chain of requests hanging
1368 		 * of its context structure. It needs to be removed
1369 		 * from this chain at this point.
1370 		 */
1371 		mutex_enter(&ictx->kc_in_use_lock);
1372 		nextreq = areq->an_ctxchain_next;
1373 		if (nextreq != NULL) {
1374 			mutex_enter(&nextreq->an_lock);
1375 			nextreq->an_is_my_turn = B_TRUE;
1376 			cv_signal(&nextreq->an_turn_cv);
1377 			mutex_exit(&nextreq->an_lock);
1378 		}
1379 
1380 		ictx->kc_req_chain_first = nextreq;
1381 		if (nextreq == NULL)
1382 			ictx->kc_req_chain_last = NULL;
1383 		mutex_exit(&ictx->kc_in_use_lock);
1384 
1385 		if (IS_SINGLE_OP(optype) || IS_FINAL_OP(optype)) {
1386 			ASSERT(nextreq == NULL);
1387 			KCF_CONTEXT_REFRELE(ictx);
1388 		} else if (error != CRYPTO_SUCCESS && IS_INIT_OP(optype)) {
1389 		/*
1390 		 * NOTE - We do not release the context in case of update
1391 		 * operations. We require the consumer to free it explicitly,
1392 		 * in case it wants to abandon an update operation. This is done
1393 		 * as there may be mechanisms in ECB mode that can continue
1394 		 * even if an operation on a block fails.
1395 		 */
1396 			KCF_CONTEXT_REFRELE(ictx);
1397 		}
1398 	}
1399 
1400 	/* Deal with the internal continuation to this request first */
1401 
1402 	if (areq->an_isdual) {
1403 		kcf_dual_req_t *next_arg;
1404 		next_arg = (kcf_dual_req_t *)areq->an_reqarg.cr_callback_arg;
1405 		next_arg->kr_areq = areq;
1406 		KCF_AREQ_REFHOLD(areq);
1407 		areq->an_isdual = B_FALSE;
1408 
1409 		NOTIFY_CLIENT(areq, error);
1410 		return;
1411 	}
1412 
1413 	/*
1414 	 * If CRYPTO_NOTIFY_OPDONE flag is set, we should notify
1415 	 * always. If this flag is clear, we skip the notification
1416 	 * provided there are no errors.  We check this flag for only
1417 	 * init or update operations. It is ignored for single, final or
1418 	 * atomic operations.
1419 	 */
1420 	skip_notify = (IS_UPDATE_OP(optype) || IS_INIT_OP(optype)) &&
1421 	    (!(areq->an_reqarg.cr_flag & CRYPTO_NOTIFY_OPDONE)) &&
1422 	    (error == CRYPTO_SUCCESS);
1423 
1424 	if (!skip_notify) {
1425 		NOTIFY_CLIENT(areq, error);
1426 	}
1427 
1428 	if (!(areq->an_reqarg.cr_flag & CRYPTO_SKIP_REQID))
1429 		kcf_reqid_delete(areq);
1430 
1431 	KCF_AREQ_REFRELE(areq);
1432 }
1433 
1434 /*
1435  * Allocate the thread pool and initialize all the fields.
1436  */
1437 static void
1438 kcfpool_alloc()
1439 {
1440 	kcfpool = kmem_alloc(sizeof (kcf_pool_t), KM_SLEEP);
1441 
1442 	kcfpool->kp_threads = kcfpool->kp_idlethreads = 0;
1443 	kcfpool->kp_blockedthreads = 0;
1444 	kcfpool->kp_signal_create_thread = B_FALSE;
1445 	kcfpool->kp_nthrs = 0;
1446 	kcfpool->kp_user_waiting = B_FALSE;
1447 
1448 	mutex_init(&kcfpool->kp_thread_lock, NULL, MUTEX_DEFAULT, NULL);
1449 	cv_init(&kcfpool->kp_nothr_cv, NULL, CV_DEFAULT, NULL);
1450 
1451 	mutex_init(&kcfpool->kp_user_lock, NULL, MUTEX_DEFAULT, NULL);
1452 	cv_init(&kcfpool->kp_user_cv, NULL, CV_DEFAULT, NULL);
1453 
1454 	kcf_idlethr_timeout = KCF_DEFAULT_THRTIMEOUT;
1455 }
1456 
1457 /*
1458  * This function is run by the 'creator' thread in the pool.
1459  * It is called from ioctl(CRYPTO_POOL_WAIT, ...).
1460  */
1461 int
1462 kcf_svc_wait(int *nthrs)
1463 {
1464 	clock_t rv;
1465 	clock_t timeout_val;
1466 
1467 	if (kcfpool == NULL)
1468 		return (ENOENT);
1469 
1470 	mutex_enter(&kcfpool->kp_user_lock);
1471 	/* Check if there's already a user thread waiting on this kcfpool */
1472 	if (kcfpool->kp_user_waiting) {
1473 		mutex_exit(&kcfpool->kp_user_lock);
1474 		*nthrs = 0;
1475 		return (EBUSY);
1476 	}
1477 
1478 	kcfpool->kp_user_waiting = B_TRUE;
1479 
1480 	/* Go to sleep, waiting for the signaled flag. */
1481 	while (!kcfpool->kp_signal_create_thread) {
1482 		timeout_val = ddi_get_lbolt() +
1483 		    drv_usectohz(kcf_idlethr_timeout);
1484 
1485 		rv = cv_timedwait_sig(&kcfpool->kp_user_cv,
1486 		    &kcfpool->kp_user_lock, timeout_val);
1487 		switch (rv) {
1488 		case 0:
1489 			/* Interrupted, return to handle exit or signal */
1490 			kcfpool->kp_user_waiting = B_FALSE;
1491 			kcfpool->kp_signal_create_thread = B_FALSE;
1492 			mutex_exit(&kcfpool->kp_user_lock);
1493 			/*
1494 			 * kcfd is exiting. Release the door and
1495 			 * invalidate it.
1496 			 */
1497 			mutex_enter(&kcf_dh_lock);
1498 			if (kcf_dh != NULL) {
1499 				door_ki_rele(kcf_dh);
1500 				kcf_dh = NULL;
1501 			}
1502 			mutex_exit(&kcf_dh_lock);
1503 			return (EINTR);
1504 
1505 		case -1:
1506 			/* Timed out. Recalculate the min/max threads */
1507 			compute_min_max_threads();
1508 			break;
1509 
1510 		default:
1511 			/* Worker thread did a cv_signal() */
1512 			break;
1513 		}
1514 	}
1515 
1516 	kcfpool->kp_signal_create_thread = B_FALSE;
1517 	kcfpool->kp_user_waiting = B_FALSE;
1518 
1519 	*nthrs = kcfpool->kp_nthrs;
1520 	mutex_exit(&kcfpool->kp_user_lock);
1521 
1522 	/* Return to userland for possible thread creation. */
1523 	return (0);
1524 }
1525 
1526 
1527 /*
1528  * This routine introduces a locking order for gswq->gs_lock followed
1529  * by cpu_lock.
1530  * This means that no consumer of the k-api should hold cpu_lock when calling
1531  * k-api routines.
1532  */
1533 static void
1534 compute_min_max_threads()
1535 {
1536 	mutex_enter(&gswq->gs_lock);
1537 	mutex_enter(&cpu_lock);
1538 	kcf_minthreads = curthread->t_cpupart->cp_ncpus;
1539 	mutex_exit(&cpu_lock);
1540 	kcf_maxthreads = kcf_thr_multiple * kcf_minthreads;
1541 	gswq->gs_maxjobs = kcf_maxthreads * crypto_taskq_maxalloc;
1542 	mutex_exit(&gswq->gs_lock);
1543 }
1544 
1545 /*
1546  * This is the main routine of the failover kernel thread.
1547  * If there are any threads in the pool we sleep. The last thread in the
1548  * pool to exit will signal us to get to work. We get back to sleep
1549  * once we detect that the pool has threads.
1550  *
1551  * Note that in the hand-off from us to a pool thread we get to run once.
1552  * Since this hand-off is a rare event this should be fine.
1553  */
1554 static void
1555 kcf_failover_thread()
1556 {
1557 	int error = 0;
1558 	kcf_context_t *ictx;
1559 	kcf_areq_node_t *req;
1560 	callb_cpr_t cpr_info;
1561 	kmutex_t cpr_lock;
1562 	static boolean_t is_logged = B_FALSE;
1563 
1564 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
1565 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,
1566 	    "kcf_failover_thread");
1567 
1568 	for (;;) {
1569 		/*
1570 		 * Wait if there are any threads are in the pool.
1571 		 */
1572 		if (kcfpool->kp_threads > 0) {
1573 			mutex_enter(&cpr_lock);
1574 			CALLB_CPR_SAFE_BEGIN(&cpr_info);
1575 			mutex_exit(&cpr_lock);
1576 
1577 			mutex_enter(&kcfpool->kp_thread_lock);
1578 			cv_wait(&kcfpool->kp_nothr_cv,
1579 			    &kcfpool->kp_thread_lock);
1580 			mutex_exit(&kcfpool->kp_thread_lock);
1581 
1582 			mutex_enter(&cpr_lock);
1583 			CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
1584 			mutex_exit(&cpr_lock);
1585 			is_logged = B_FALSE;
1586 		}
1587 
1588 		/*
1589 		 * Get the requests from the queue and wait if needed.
1590 		 */
1591 		mutex_enter(&gswq->gs_lock);
1592 
1593 		while ((req = kcf_dequeue()) == NULL) {
1594 			mutex_enter(&cpr_lock);
1595 			CALLB_CPR_SAFE_BEGIN(&cpr_info);
1596 			mutex_exit(&cpr_lock);
1597 
1598 			KCF_ATOMIC_INCR(kcfpool->kp_idlethreads);
1599 			cv_wait(&gswq->gs_cv, &gswq->gs_lock);
1600 			KCF_ATOMIC_DECR(kcfpool->kp_idlethreads);
1601 
1602 			mutex_enter(&cpr_lock);
1603 			CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
1604 			mutex_exit(&cpr_lock);
1605 		}
1606 
1607 		mutex_exit(&gswq->gs_lock);
1608 
1609 		/*
1610 		 * We check the kp_threads since kcfd could have started
1611 		 * while we are waiting on the global software queue.
1612 		 */
1613 		if (kcfpool->kp_threads <= 0 && !is_logged) {
1614 			cmn_err(CE_WARN, "kcfd is not running. Please check "
1615 			    "and restart kcfd. Using the failover kernel "
1616 			    "thread for now.\n");
1617 			is_logged = B_TRUE;
1618 		}
1619 
1620 		/*
1621 		 * Get to work on the request.
1622 		 */
1623 		ictx = req->an_context;
1624 		mutex_enter(&req->an_lock);
1625 		req->an_state = REQ_INPROGRESS;
1626 		mutex_exit(&req->an_lock);
1627 
1628 		error = common_submit_request(req->an_provider, ictx ?
1629 		    &ictx->kc_glbl_ctx : NULL, &req->an_params, req);
1630 
1631 		kcf_aop_done(req, error);
1632 	}
1633 }
1634 
1635 /*
1636  * Insert the async request in the hash table after assigning it
1637  * an ID. Returns the ID.
1638  *
1639  * The ID is used by the caller to pass as an argument to a
1640  * cancel_req() routine later.
1641  */
1642 static crypto_req_id_t
1643 kcf_reqid_insert(kcf_areq_node_t *areq)
1644 {
1645 	int indx;
1646 	crypto_req_id_t id;
1647 	kcf_areq_node_t *headp;
1648 	kcf_reqid_table_t *rt =
1649 	    kcf_reqid_table[CPU->cpu_seqid & REQID_TABLE_MASK];
1650 
1651 	mutex_enter(&rt->rt_lock);
1652 
1653 	rt->rt_curid = id =
1654 	    (rt->rt_curid - REQID_COUNTER_LOW) | REQID_COUNTER_HIGH;
1655 	SET_REQID(areq, id);
1656 	indx = REQID_HASH(id);
1657 	headp = areq->an_idnext = rt->rt_idhash[indx];
1658 	areq->an_idprev = NULL;
1659 	if (headp != NULL)
1660 		headp->an_idprev = areq;
1661 
1662 	rt->rt_idhash[indx] = areq;
1663 	mutex_exit(&rt->rt_lock);
1664 
1665 	return (id);
1666 }
1667 
1668 /*
1669  * Delete the async request from the hash table.
1670  */
1671 static void
1672 kcf_reqid_delete(kcf_areq_node_t *areq)
1673 {
1674 	int indx;
1675 	kcf_areq_node_t *nextp, *prevp;
1676 	crypto_req_id_t id = GET_REQID(areq);
1677 	kcf_reqid_table_t *rt;
1678 
1679 	rt = kcf_reqid_table[id & REQID_TABLE_MASK];
1680 	indx = REQID_HASH(id);
1681 
1682 	mutex_enter(&rt->rt_lock);
1683 
1684 	nextp = areq->an_idnext;
1685 	prevp = areq->an_idprev;
1686 	if (nextp != NULL)
1687 		nextp->an_idprev = prevp;
1688 	if (prevp != NULL)
1689 		prevp->an_idnext = nextp;
1690 	else
1691 		rt->rt_idhash[indx] = nextp;
1692 
1693 	SET_REQID(areq, 0);
1694 	cv_broadcast(&areq->an_done);
1695 
1696 	mutex_exit(&rt->rt_lock);
1697 }
1698 
1699 /*
1700  * Cancel a single asynchronous request.
1701  *
1702  * We guarantee that no problems will result from calling
1703  * crypto_cancel_req() for a request which is either running, or
1704  * has already completed. We remove the request from any queues
1705  * if it is possible. We wait for request completion if the
1706  * request is dispatched to a provider.
1707  *
1708  * Calling context:
1709  * 	Can be called from user context only.
1710  *
1711  * NOTE: We acquire the following locks in this routine (in order):
1712  *	- rt_lock (kcf_reqid_table_t)
1713  *	- gswq->gs_lock
1714  *	- areq->an_lock
1715  *	- ictx->kc_in_use_lock (from kcf_removereq_in_ctxchain())
1716  *
1717  * This locking order MUST be maintained in code every where else.
1718  */
1719 void
1720 crypto_cancel_req(crypto_req_id_t id)
1721 {
1722 	int indx;
1723 	kcf_areq_node_t *areq;
1724 	kcf_provider_desc_t *pd;
1725 	kcf_context_t *ictx;
1726 	kcf_reqid_table_t *rt;
1727 
1728 	rt = kcf_reqid_table[id & REQID_TABLE_MASK];
1729 	indx = REQID_HASH(id);
1730 
1731 	mutex_enter(&rt->rt_lock);
1732 	for (areq = rt->rt_idhash[indx]; areq; areq = areq->an_idnext) {
1733 	if (GET_REQID(areq) == id) {
1734 		/*
1735 		 * We found the request. It is either still waiting
1736 		 * in the framework queues or running at the provider.
1737 		 */
1738 		pd = areq->an_provider;
1739 		ASSERT(pd != NULL);
1740 
1741 		switch (pd->pd_prov_type) {
1742 		case CRYPTO_SW_PROVIDER:
1743 			mutex_enter(&gswq->gs_lock);
1744 			mutex_enter(&areq->an_lock);
1745 
1746 			/* This request can be safely canceled. */
1747 			if (areq->an_state <= REQ_WAITING) {
1748 				/* Remove from gswq, global software queue. */
1749 				kcf_remove_node(areq);
1750 				if ((ictx = areq->an_context) != NULL)
1751 					kcf_removereq_in_ctxchain(ictx, areq);
1752 
1753 				mutex_exit(&areq->an_lock);
1754 				mutex_exit(&gswq->gs_lock);
1755 				mutex_exit(&rt->rt_lock);
1756 
1757 				/* Remove areq from hash table and free it. */
1758 				kcf_reqid_delete(areq);
1759 				KCF_AREQ_REFRELE(areq);
1760 				return;
1761 			}
1762 
1763 			mutex_exit(&areq->an_lock);
1764 			mutex_exit(&gswq->gs_lock);
1765 			break;
1766 
1767 		case CRYPTO_HW_PROVIDER:
1768 			/*
1769 			 * There is no interface to remove an entry
1770 			 * once it is on the taskq. So, we do not do
1771 			 * any thing for a hardware provider.
1772 			 */
1773 			break;
1774 		}
1775 
1776 		/*
1777 		 * The request is running. Wait for the request completion
1778 		 * to notify us.
1779 		 */
1780 		KCF_AREQ_REFHOLD(areq);
1781 		while (GET_REQID(areq) == id)
1782 			cv_wait(&areq->an_done, &rt->rt_lock);
1783 		KCF_AREQ_REFRELE(areq);
1784 		break;
1785 	}
1786 	}
1787 
1788 	mutex_exit(&rt->rt_lock);
1789 }
1790 
1791 /*
1792  * Cancel all asynchronous requests associated with the
1793  * passed in crypto context and free it.
1794  *
1795  * A client SHOULD NOT call this routine after calling a crypto_*_final
1796  * routine. This routine is called only during intermediate operations.
1797  * The client should not use the crypto context after this function returns
1798  * since we destroy it.
1799  *
1800  * Calling context:
1801  * 	Can be called from user context only.
1802  */
1803 void
1804 crypto_cancel_ctx(crypto_context_t ctx)
1805 {
1806 	kcf_context_t *ictx;
1807 	kcf_areq_node_t *areq;
1808 
1809 	if (ctx == NULL)
1810 		return;
1811 
1812 	ictx = (kcf_context_t *)((crypto_ctx_t *)ctx)->cc_framework_private;
1813 
1814 	mutex_enter(&ictx->kc_in_use_lock);
1815 
1816 	/* Walk the chain and cancel each request */
1817 	while ((areq = ictx->kc_req_chain_first) != NULL) {
1818 		/*
1819 		 * We have to drop the lock here as we may have
1820 		 * to wait for request completion. We hold the
1821 		 * request before dropping the lock though, so that it
1822 		 * won't be freed underneath us.
1823 		 */
1824 		KCF_AREQ_REFHOLD(areq);
1825 		mutex_exit(&ictx->kc_in_use_lock);
1826 
1827 		crypto_cancel_req(GET_REQID(areq));
1828 		KCF_AREQ_REFRELE(areq);
1829 
1830 		mutex_enter(&ictx->kc_in_use_lock);
1831 	}
1832 
1833 	mutex_exit(&ictx->kc_in_use_lock);
1834 	KCF_CONTEXT_REFRELE(ictx);
1835 }
1836 
1837 /*
1838  * Update kstats.
1839  */
1840 static int
1841 kcf_misc_kstat_update(kstat_t *ksp, int rw)
1842 {
1843 	uint_t tcnt;
1844 	kcf_stats_t *ks_data;
1845 
1846 	if (rw == KSTAT_WRITE)
1847 		return (EACCES);
1848 
1849 	ks_data = ksp->ks_data;
1850 
1851 	ks_data->ks_thrs_in_pool.value.ui32 = kcfpool->kp_threads;
1852 	/*
1853 	 * The failover thread is counted in kp_idlethreads in
1854 	 * some corner cases. This is done to avoid doing more checks
1855 	 * when submitting a request. We account for those cases below.
1856 	 */
1857 	if ((tcnt = kcfpool->kp_idlethreads) == (kcfpool->kp_threads + 1))
1858 		tcnt--;
1859 	ks_data->ks_idle_thrs.value.ui32 = tcnt;
1860 	ks_data->ks_minthrs.value.ui32 = kcf_minthreads;
1861 	ks_data->ks_maxthrs.value.ui32 = kcf_maxthreads;
1862 	ks_data->ks_swq_njobs.value.ui32 = gswq->gs_njobs;
1863 	ks_data->ks_swq_maxjobs.value.ui32 = gswq->gs_maxjobs;
1864 	ks_data->ks_taskq_threads.value.ui32 = crypto_taskq_threads;
1865 	ks_data->ks_taskq_minalloc.value.ui32 = crypto_taskq_minalloc;
1866 	ks_data->ks_taskq_maxalloc.value.ui32 = crypto_taskq_maxalloc;
1867 
1868 	return (0);
1869 }
1870 
1871 /*
1872  * Allocate and initiatize a kcf_dual_req, used for saving the arguments of
1873  * a dual operation or an atomic operation that has to be internally
1874  * simulated with multiple single steps.
1875  * crq determines the memory allocation flags.
1876  */
1877 
1878 kcf_dual_req_t *
1879 kcf_alloc_req(crypto_call_req_t *crq)
1880 {
1881 	kcf_dual_req_t *kcr;
1882 
1883 	kcr = kmem_alloc(sizeof (kcf_dual_req_t), KCF_KMFLAG(crq));
1884 
1885 	if (kcr == NULL)
1886 		return (NULL);
1887 
1888 	/* Copy the whole crypto_call_req struct, as it isn't persistant */
1889 	if (crq != NULL)
1890 		kcr->kr_callreq = *crq;
1891 	else
1892 		bzero(&(kcr->kr_callreq), sizeof (crypto_call_req_t));
1893 	kcr->kr_areq = NULL;
1894 	kcr->kr_saveoffset = 0;
1895 	kcr->kr_savelen = 0;
1896 
1897 	return (kcr);
1898 }
1899 
1900 /*
1901  * Callback routine for the next part of a simulated dual part.
1902  * Schedules the next step.
1903  *
1904  * This routine can be called from interrupt context.
1905  */
1906 void
1907 kcf_next_req(void *next_req_arg, int status)
1908 {
1909 	kcf_dual_req_t *next_req = (kcf_dual_req_t *)next_req_arg;
1910 	kcf_req_params_t *params = &(next_req->kr_params);
1911 	kcf_areq_node_t *areq = next_req->kr_areq;
1912 	int error = status;
1913 	kcf_provider_desc_t *pd;
1914 	crypto_dual_data_t *ct;
1915 
1916 	/* Stop the processing if an error occured at this step */
1917 	if (error != CRYPTO_SUCCESS) {
1918 out:
1919 		areq->an_reqarg = next_req->kr_callreq;
1920 		KCF_AREQ_REFRELE(areq);
1921 		kmem_free(next_req, sizeof (kcf_dual_req_t));
1922 		areq->an_isdual = B_FALSE;
1923 		kcf_aop_done(areq, error);
1924 		return;
1925 	}
1926 
1927 	switch (params->rp_opgrp) {
1928 	case KCF_OG_MAC: {
1929 
1930 		/*
1931 		 * The next req is submitted with the same reqid as the
1932 		 * first part. The consumer only got back that reqid, and
1933 		 * should still be able to cancel the operation during its
1934 		 * second step.
1935 		 */
1936 		kcf_mac_ops_params_t *mops = &(params->rp_u.mac_params);
1937 		crypto_ctx_template_t mac_tmpl;
1938 		kcf_mech_entry_t *me;
1939 
1940 		ct = (crypto_dual_data_t *)mops->mo_data;
1941 		mac_tmpl = (crypto_ctx_template_t)mops->mo_templ;
1942 
1943 		/* No expected recoverable failures, so no retry list */
1944 		pd = kcf_get_mech_provider(mops->mo_framework_mechtype, NULL,
1945 		    &me, &error, NULL, CRYPTO_FG_MAC_ATOMIC,
1946 		    (areq->an_reqarg.cr_flag & CRYPTO_RESTRICTED), ct->dd_len2);
1947 
1948 		if (pd == NULL) {
1949 			error = CRYPTO_MECH_NOT_SUPPORTED;
1950 			goto out;
1951 		}
1952 		/* Validate the MAC context template here */
1953 		if ((pd->pd_prov_type == CRYPTO_SW_PROVIDER) &&
1954 		    (mac_tmpl != NULL)) {
1955 			kcf_ctx_template_t *ctx_mac_tmpl;
1956 
1957 			ctx_mac_tmpl = (kcf_ctx_template_t *)mac_tmpl;
1958 
1959 			if (ctx_mac_tmpl->ct_generation != me->me_gen_swprov) {
1960 				KCF_PROV_REFRELE(pd);
1961 				error = CRYPTO_OLD_CTX_TEMPLATE;
1962 				goto out;
1963 			}
1964 			mops->mo_templ = ctx_mac_tmpl->ct_prov_tmpl;
1965 		}
1966 
1967 		break;
1968 	}
1969 	case KCF_OG_DECRYPT: {
1970 		kcf_decrypt_ops_params_t *dcrops =
1971 		    &(params->rp_u.decrypt_params);
1972 
1973 		ct = (crypto_dual_data_t *)dcrops->dop_ciphertext;
1974 		/* No expected recoverable failures, so no retry list */
1975 		pd = kcf_get_mech_provider(dcrops->dop_framework_mechtype,
1976 		    NULL, NULL, &error, NULL, CRYPTO_FG_DECRYPT_ATOMIC,
1977 		    (areq->an_reqarg.cr_flag & CRYPTO_RESTRICTED), ct->dd_len1);
1978 
1979 		if (pd == NULL) {
1980 			error = CRYPTO_MECH_NOT_SUPPORTED;
1981 			goto out;
1982 		}
1983 		break;
1984 	}
1985 	}
1986 
1987 	/* The second step uses len2 and offset2 of the dual_data */
1988 	next_req->kr_saveoffset = ct->dd_offset1;
1989 	next_req->kr_savelen = ct->dd_len1;
1990 	ct->dd_offset1 = ct->dd_offset2;
1991 	ct->dd_len1 = ct->dd_len2;
1992 
1993 	/* preserve if the caller is restricted */
1994 	if (areq->an_reqarg.cr_flag & CRYPTO_RESTRICTED) {
1995 		areq->an_reqarg.cr_flag = CRYPTO_RESTRICTED;
1996 	} else {
1997 		areq->an_reqarg.cr_flag = 0;
1998 	}
1999 
2000 	areq->an_reqarg.cr_callback_func = kcf_last_req;
2001 	areq->an_reqarg.cr_callback_arg = next_req;
2002 	areq->an_isdual = B_TRUE;
2003 
2004 	/*
2005 	 * We would like to call kcf_submit_request() here. But,
2006 	 * that is not possible as that routine allocates a new
2007 	 * kcf_areq_node_t request structure, while we need to
2008 	 * reuse the existing request structure.
2009 	 */
2010 	switch (pd->pd_prov_type) {
2011 	case CRYPTO_SW_PROVIDER:
2012 		error = common_submit_request(pd, NULL, params,
2013 		    KCF_RHNDL(KM_NOSLEEP));
2014 		break;
2015 
2016 	case CRYPTO_HW_PROVIDER: {
2017 		kcf_provider_desc_t *old_pd;
2018 		taskq_t *taskq = pd->pd_taskq;
2019 
2020 		/*
2021 		 * Set the params for the second step in the
2022 		 * dual-ops.
2023 		 */
2024 		areq->an_params = *params;
2025 		old_pd = areq->an_provider;
2026 		KCF_PROV_REFRELE(old_pd);
2027 		KCF_PROV_REFHOLD(pd);
2028 		areq->an_provider = pd;
2029 
2030 		/*
2031 		 * Note that we have to do a taskq_dispatch()
2032 		 * here as we may be in interrupt context.
2033 		 */
2034 		if (taskq_dispatch(taskq, process_req_hwp, areq,
2035 		    TQ_NOSLEEP) == (taskqid_t)0) {
2036 			error = CRYPTO_HOST_MEMORY;
2037 		} else {
2038 			error = CRYPTO_QUEUED;
2039 		}
2040 		break;
2041 	}
2042 	}
2043 
2044 	/*
2045 	 * We have to release the holds on the request and the provider
2046 	 * in all cases.
2047 	 */
2048 	KCF_AREQ_REFRELE(areq);
2049 	KCF_PROV_REFRELE(pd);
2050 
2051 	if (error != CRYPTO_QUEUED) {
2052 		/* restore, clean up, and invoke the client's callback */
2053 
2054 		ct->dd_offset1 = next_req->kr_saveoffset;
2055 		ct->dd_len1 = next_req->kr_savelen;
2056 		areq->an_reqarg = next_req->kr_callreq;
2057 		kmem_free(next_req, sizeof (kcf_dual_req_t));
2058 		areq->an_isdual = B_FALSE;
2059 		kcf_aop_done(areq, error);
2060 	}
2061 }
2062 
2063 /*
2064  * Last part of an emulated dual operation.
2065  * Clean up and restore ...
2066  */
2067 void
2068 kcf_last_req(void *last_req_arg, int status)
2069 {
2070 	kcf_dual_req_t *last_req = (kcf_dual_req_t *)last_req_arg;
2071 
2072 	kcf_req_params_t *params = &(last_req->kr_params);
2073 	kcf_areq_node_t *areq = last_req->kr_areq;
2074 	crypto_dual_data_t *ct;
2075 
2076 	switch (params->rp_opgrp) {
2077 	case KCF_OG_MAC: {
2078 		kcf_mac_ops_params_t *mops = &(params->rp_u.mac_params);
2079 
2080 		ct = (crypto_dual_data_t *)mops->mo_data;
2081 		break;
2082 	}
2083 	case KCF_OG_DECRYPT: {
2084 		kcf_decrypt_ops_params_t *dcrops =
2085 		    &(params->rp_u.decrypt_params);
2086 
2087 		ct = (crypto_dual_data_t *)dcrops->dop_ciphertext;
2088 		break;
2089 	}
2090 	}
2091 	ct->dd_offset1 = last_req->kr_saveoffset;
2092 	ct->dd_len1 = last_req->kr_savelen;
2093 
2094 	/* The submitter used kcf_last_req as its callback */
2095 
2096 	if (areq == NULL) {
2097 		crypto_call_req_t *cr = &last_req->kr_callreq;
2098 
2099 		(*(cr->cr_callback_func))(cr->cr_callback_arg, status);
2100 		kmem_free(last_req, sizeof (kcf_dual_req_t));
2101 		return;
2102 	}
2103 	areq->an_reqarg = last_req->kr_callreq;
2104 	KCF_AREQ_REFRELE(areq);
2105 	kmem_free(last_req, sizeof (kcf_dual_req_t));
2106 	areq->an_isdual = B_FALSE;
2107 	kcf_aop_done(areq, status);
2108 }
2109