xref: /titanic_50/usr/src/uts/common/sys/crypto/sched_impl.h (revision d1a180b0452ce86577a43be3245d2eacdeec1a34)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _SYS_CRYPTO_SCHED_IMPL_H
28 #define	_SYS_CRYPTO_SCHED_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * Scheduler internal structures.
34  */
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 #include <sys/types.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/door.h>
44 #include <sys/crypto/api.h>
45 #include <sys/crypto/spi.h>
46 #include <sys/crypto/impl.h>
47 #include <sys/crypto/common.h>
48 #include <sys/crypto/ops_impl.h>
49 
50 typedef void (kcf_func_t)(void *, int);
51 
52 typedef enum kcf_req_status {
53 	REQ_ALLOCATED = 1,
54 	REQ_WAITING,		/* At the framework level */
55 	REQ_INPROGRESS,		/* At the provider level */
56 	REQ_DONE,
57 	REQ_CANCELED
58 } kcf_req_status_t;
59 
60 typedef enum kcf_call_type {
61 	CRYPTO_SYNCH = 1,
62 	CRYPTO_ASYNCH
63 } kcf_call_type_t;
64 
65 #define	CHECK_RESTRICT(crq) (crq != NULL &&	\
66 	((crq)->cr_flag & CRYPTO_RESTRICTED))
67 
68 #define	CHECK_RESTRICT_FALSE	B_FALSE
69 
70 #define	CHECK_FASTPATH(crq, pd) ((crq) == NULL ||	\
71 	!((crq)->cr_flag & CRYPTO_ALWAYS_QUEUE)) &&	\
72 	(pd)->pd_prov_type == CRYPTO_SW_PROVIDER
73 
74 #define	KCF_KMFLAG(crq)	(((crq) == NULL) ? KM_SLEEP : KM_NOSLEEP)
75 
76 /*
77  * The framework keeps an internal handle to use in the adaptive
78  * asynchronous case. This is the case when a client has the
79  * CRYPTO_ALWAYS_QUEUE bit clear and a software provider is used for
80  * the request. The request is completed in the context of the calling
81  * thread and kernel memory must be allocated with KM_NOSLEEP.
82  *
83  * The framework passes a pointer to the handle in crypto_req_handle_t
84  * argument when it calls the SPI of the software provider. The macros
85  * KCF_RHNDL() and KCF_SWFP_RHNDL() are used to do this.
86  *
87  * When a provider asks the framework for kmflag value via
88  * crypto_kmflag(9S) we use REQHNDL2_KMFLAG() macro.
89  */
90 extern ulong_t kcf_swprov_hndl;
91 #define	KCF_RHNDL(kmflag) (((kmflag) == KM_SLEEP) ? NULL : &kcf_swprov_hndl)
92 #define	KCF_SWFP_RHNDL(crq) (((crq) == NULL) ? NULL : &kcf_swprov_hndl)
93 #define	REQHNDL2_KMFLAG(rhndl) \
94 	((rhndl == &kcf_swprov_hndl) ? KM_NOSLEEP : KM_SLEEP)
95 
96 /* Internal call_req flags. They start after the public ones in api.h */
97 
98 #define	CRYPTO_SETDUAL	0x00001000	/* Set the 'cont' boolean before */
99 					/* submitting the request */
100 #define	KCF_ISDUALREQ(crq)	\
101 	(((crq) == NULL) ? B_FALSE : (crq->cr_flag & CRYPTO_SETDUAL))
102 
103 typedef struct kcf_prov_tried {
104 	kcf_provider_desc_t	*pt_pd;
105 	struct kcf_prov_tried	*pt_next;
106 } kcf_prov_tried_t;
107 
108 #define	IS_FG_SUPPORTED(mdesc, fg)		\
109 	(((mdesc)->pm_mech_info.cm_func_group_mask & (fg)) != 0)
110 
111 #define	IS_PROVIDER_TRIED(pd, tlist)		\
112 	(tlist != NULL && is_in_triedlist(pd, tlist))
113 
114 #define	IS_RECOVERABLE(error)			\
115 	(error == CRYPTO_BUFFER_TOO_BIG ||	\
116 	error == CRYPTO_BUSY ||			\
117 	error == CRYPTO_DEVICE_ERROR ||		\
118 	error == CRYPTO_DEVICE_MEMORY ||	\
119 	error == CRYPTO_KEY_SIZE_RANGE ||	\
120 	error == CRYPTO_NO_PERMISSION)
121 
122 #define	KCF_ATOMIC_INCR(x)	atomic_add_32(&(x), 1)
123 #define	KCF_ATOMIC_DECR(x)	atomic_add_32(&(x), -1)
124 
125 /*
126  * Node structure for synchronous requests.
127  */
128 typedef struct kcf_sreq_node {
129 	/* Should always be the first field in this structure */
130 	kcf_call_type_t		sn_type;
131 	/*
132 	 * sn_cv and sr_lock are used to wait for the
133 	 * operation to complete. sn_lock also protects
134 	 * the sn_state field.
135 	 */
136 	kcondvar_t		sn_cv;
137 	kmutex_t		sn_lock;
138 	kcf_req_status_t	sn_state;
139 
140 	/*
141 	 * Return value from the operation. This will be
142 	 * one of the CRYPTO_* errors defined in common.h.
143 	 */
144 	int			sn_rv;
145 
146 	/*
147 	 * parameters to call the SPI with. This can be
148 	 * a pointer as we know the caller context/stack stays.
149 	 */
150 	struct kcf_req_params	*sn_params;
151 
152 	/* Internal context for this request */
153 	struct kcf_context	*sn_context;
154 
155 	/* Provider handling this request */
156 	kcf_provider_desc_t	*sn_provider;
157 } kcf_sreq_node_t;
158 
159 /*
160  * Node structure for asynchronous requests. A node can be on
161  * on a chain of requests hanging of the internal context
162  * structure and can be in the global software provider queue.
163  */
164 typedef struct kcf_areq_node {
165 	/* Should always be the first field in this structure */
166 	kcf_call_type_t		an_type;
167 
168 	/* an_lock protects the field an_state  */
169 	kmutex_t		an_lock;
170 	kcf_req_status_t	an_state;
171 	crypto_call_req_t	an_reqarg;
172 
173 	/*
174 	 * parameters to call the SPI with. We need to
175 	 * save the params since the caller stack can go away.
176 	 */
177 	struct kcf_req_params	an_params;
178 
179 	/*
180 	 * The next two fields should be NULL for operations that
181 	 * don't need a context.
182 	 */
183 	/* Internal context for this request */
184 	struct kcf_context	*an_context;
185 
186 	/* next in chain of requests for context */
187 	struct kcf_areq_node	*an_ctxchain_next;
188 
189 	boolean_t		an_is_my_turn;
190 	boolean_t		an_isdual;	/* for internal reuse */
191 
192 	/*
193 	 * Next and previous nodes in the global software
194 	 * queue. These fields are NULL for a hardware
195 	 * provider since we use a taskq there.
196 	 */
197 	struct kcf_areq_node	*an_next;
198 	struct kcf_areq_node	*an_prev;
199 
200 	/* Provider handling this request */
201 	kcf_provider_desc_t	*an_provider;
202 	kcf_prov_tried_t	*an_tried_plist;
203 
204 	struct kcf_areq_node	*an_idnext;	/* Next in ID hash */
205 	struct kcf_areq_node	*an_idprev;	/* Prev in ID hash */
206 	kcondvar_t		an_done;	/* Signal request completion */
207 	uint_t			an_refcnt;
208 } kcf_areq_node_t;
209 
210 #define	KCF_AREQ_REFHOLD(areq) {		\
211 	atomic_add_32(&(areq)->an_refcnt, 1);	\
212 	ASSERT((areq)->an_refcnt != 0);		\
213 }
214 
215 #define	KCF_AREQ_REFRELE(areq) {				\
216 	ASSERT((areq)->an_refcnt != 0);				\
217 	membar_exit();						\
218 	if (atomic_add_32_nv(&(areq)->an_refcnt, -1) == 0)	\
219 		kcf_free_req(areq);				\
220 }
221 
222 #define	GET_REQ_TYPE(arg) *((kcf_call_type_t *)(arg))
223 
224 #define	NOTIFY_CLIENT(areq, err) (*(areq)->an_reqarg.cr_callback_func)(\
225 	(areq)->an_reqarg.cr_callback_arg, err);
226 
227 /* For internally generated call requests for dual operations */
228 typedef	struct kcf_call_req {
229 	crypto_call_req_t	kr_callreq;	/* external client call req */
230 	kcf_req_params_t	kr_params;	/* Params saved for next call */
231 	kcf_areq_node_t		*kr_areq;	/* Use this areq */
232 	off_t			kr_saveoffset;
233 	size_t			kr_savelen;
234 } kcf_dual_req_t;
235 
236 /*
237  * The following are some what similar to macros in callo.h, which implement
238  * callout tables.
239  *
240  * The lower four bits of the ID are used to encode the table ID to
241  * index in to. The REQID_COUNTER_HIGH bit is used to avoid any check for
242  * wrap around when generating ID. We assume that there won't be a request
243  * which takes more time than 2^^(sizeof (long) - 5) other requests submitted
244  * after it. This ensures there won't be any ID collision.
245  */
246 #define	REQID_COUNTER_HIGH	(1UL << (8 * sizeof (long) - 1))
247 #define	REQID_COUNTER_SHIFT	4
248 #define	REQID_COUNTER_LOW	(1 << REQID_COUNTER_SHIFT)
249 #define	REQID_TABLES		16
250 #define	REQID_TABLE_MASK	(REQID_TABLES - 1)
251 
252 #define	REQID_BUCKETS		512
253 #define	REQID_BUCKET_MASK	(REQID_BUCKETS - 1)
254 #define	REQID_HASH(id)	(((id) >> REQID_COUNTER_SHIFT) & REQID_BUCKET_MASK)
255 
256 #define	GET_REQID(areq) (areq)->an_reqarg.cr_reqid
257 #define	SET_REQID(areq, val)	GET_REQID(areq) = val
258 
259 /*
260  * Hash table for async requests.
261  */
262 typedef struct kcf_reqid_table {
263 	kmutex_t		rt_lock;
264 	crypto_req_id_t		rt_curid;
265 	kcf_areq_node_t		*rt_idhash[REQID_BUCKETS];
266 } kcf_reqid_table_t;
267 
268 /*
269  * Global software provider queue structure. Requests to be
270  * handled by a SW provider and have the ALWAYS_QUEUE flag set
271  * get queued here.
272  */
273 typedef struct kcf_global_swq {
274 	/*
275 	 * gs_cv and gs_lock are used to wait for new requests.
276 	 * gs_lock protects the changes to the queue.
277 	 */
278 	kcondvar_t		gs_cv;
279 	kmutex_t		gs_lock;
280 	uint_t			gs_njobs;
281 	uint_t			gs_maxjobs;
282 	kcf_areq_node_t		*gs_first;
283 	kcf_areq_node_t		*gs_last;
284 } kcf_global_swq_t;
285 
286 
287 /*
288  * Internal representation of a canonical context. We contain crypto_ctx_t
289  * structure in order to have just one memory allocation. The SPI
290  * ((crypto_ctx_t *)ctx)->cc_framework_private maps to this structure.
291  */
292 typedef struct kcf_context {
293 	crypto_ctx_t		kc_glbl_ctx;
294 	uint_t			kc_refcnt;
295 	kcondvar_t		kc_in_use_cv;
296 	kmutex_t		kc_in_use_lock;
297 	/*
298 	 * kc_req_chain_first and kc_req_chain_last are used to chain
299 	 * multiple async requests using the same context. They should be
300 	 * NULL for sync requests.
301 	 */
302 	kcf_areq_node_t		*kc_req_chain_first;
303 	kcf_areq_node_t		*kc_req_chain_last;
304 	boolean_t		kc_need_signal;	/* Initialized to B_FALSE */
305 	kcf_provider_desc_t	*kc_prov_desc;	/* Prov. descriptor */
306 	struct kcf_context	*kc_secondctx;	/* for dual contexts */
307 } kcf_context_t;
308 
309 /*
310  * Bump up the reference count on the framework private context. A
311  * global context or a request that references this structure should
312  * do a hold.
313  */
314 #define	KCF_CONTEXT_REFHOLD(ictx) {		\
315 	atomic_add_32(&(ictx)->kc_refcnt, 1);	\
316 	ASSERT((ictx)->kc_refcnt != 0);		\
317 }
318 
319 /*
320  * Decrement the reference count on the framework private context.
321  * When the last reference is released, the framework private
322  * context structure is freed along with the global context.
323  */
324 #define	KCF_CONTEXT_REFRELE(ictx) {				\
325 	ASSERT((ictx)->kc_refcnt != 0);				\
326 	membar_exit();						\
327 	if (atomic_add_32_nv(&(ictx)->kc_refcnt, -1) == 0)	\
328 		kcf_free_context(ictx);				\
329 }
330 
331 /*
332  * Check if we can release the context now. In case of CRYPTO_QUEUED
333  * we do not release it as we can do it only after the provider notified
334  * us. In case of CRYPTO_BUSY, the client can retry the request using
335  * the context, so we do not release the context.
336  *
337  * This macro should be called only from the final routine in
338  * an init/update/final sequence. We do not release the context in case
339  * of update operations. We require the consumer to free it
340  * explicitly, in case it wants to abandon the operation. This is done
341  * as there may be mechanisms in ECB mode that can continue even if
342  * an operation on a block fails.
343  */
344 #define	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx) {			\
345 	if (KCF_CONTEXT_DONE(rv))				\
346 		KCF_CONTEXT_REFRELE(kcf_ctx);			\
347 }
348 
349 /*
350  * This macro determines whether we're done with a context.
351  */
352 #define	KCF_CONTEXT_DONE(rv)					\
353 	((rv) != CRYPTO_QUEUED && (rv) != CRYPTO_BUSY &&	\
354 	    (rv) != CRYPTO_BUFFER_TOO_SMALL)
355 
356 /*
357  * A crypto_ctx_template_t is internally a pointer to this struct
358  */
359 typedef	struct kcf_ctx_template {
360 	crypto_kcf_provider_handle_t	ct_prov_handle;	/* provider handle */
361 	uint_t				ct_generation;	/* generation # */
362 	size_t				ct_size;	/* for freeing */
363 	crypto_spi_ctx_template_t	ct_prov_tmpl;	/* context template */
364 							/* from the SW prov */
365 } kcf_ctx_template_t;
366 
367 /*
368  * Structure for pool of threads working on global software queue.
369  */
370 typedef struct kcf_pool {
371 	uint32_t	kp_threads;		/* Number of threads in pool */
372 	uint32_t	kp_idlethreads;		/* Idle threads in pool */
373 	uint32_t	kp_blockedthreads;	/* Blocked threads in pool */
374 
375 	/*
376 	 * cv & lock to monitor the condition when no threads
377 	 * are around. In this case the failover thread kicks in.
378 	 */
379 	kcondvar_t	kp_nothr_cv;
380 	kmutex_t	kp_thread_lock;
381 
382 	/* Userspace thread creator variables. */
383 	boolean_t	kp_signal_create_thread; /* Create requested flag  */
384 	int		kp_nthrs;		/* # of threads to create */
385 	boolean_t	kp_user_waiting;	/* Thread waiting for work */
386 
387 	/*
388 	 * cv & lock for the condition where more threads need to be
389 	 * created. kp_user_lock also protects the three fileds above.
390 	 */
391 	kcondvar_t	kp_user_cv;		/* Creator cond. variable */
392 	kmutex_t	kp_user_lock;		/* Creator lock */
393 } kcf_pool_t;
394 
395 
396 /*
397  * State of a crypto bufcall element.
398  */
399 typedef enum cbuf_state {
400 	CBUF_FREE = 1,
401 	CBUF_WAITING,
402 	CBUF_RUNNING
403 } cbuf_state_t;
404 
405 /*
406  * Structure of a crypto bufcall element.
407  */
408 typedef struct kcf_cbuf_elem {
409 	/*
410 	 * lock and cv to wait for CBUF_RUNNING to be done
411 	 * kc_lock also protects kc_state.
412 	 */
413 	kmutex_t		kc_lock;
414 	kcondvar_t		kc_cv;
415 	cbuf_state_t		kc_state;
416 
417 	struct kcf_cbuf_elem	*kc_next;
418 	struct kcf_cbuf_elem	*kc_prev;
419 
420 	void			(*kc_func)(void *arg);
421 	void			*kc_arg;
422 } kcf_cbuf_elem_t;
423 
424 /*
425  * State of a notify element.
426  */
427 typedef enum ntfy_elem_state {
428 	NTFY_WAITING = 1,
429 	NTFY_RUNNING
430 } ntfy_elem_state_t;
431 
432 /*
433  * Structure of a notify list element.
434  */
435 typedef struct kcf_ntfy_elem {
436 	/*
437 	 * lock and cv to wait for NTFY_RUNNING to be done.
438 	 * kn_lock also protects kn_state.
439 	 */
440 	kmutex_t			kn_lock;
441 	kcondvar_t			kn_cv;
442 	ntfy_elem_state_t		kn_state;
443 
444 	struct kcf_ntfy_elem		*kn_next;
445 	struct kcf_ntfy_elem		*kn_prev;
446 
447 	crypto_notify_callback_t	kn_func;
448 	uint32_t			kn_event_mask;
449 } kcf_ntfy_elem_t;
450 
451 
452 /*
453  * The following values are based on the assumption that it would
454  * take around eight cpus to load a hardware provider (This is true for
455  * at least one product) and a kernel client may come from different
456  * low-priority interrupt levels. We will have CYRPTO_TASKQ_MIN number
457  * of cached taskq entries. These are just reasonable estimates and
458  * might need to change in future.
459  */
460 #define	CYRPTO_TASKQ_MIN	64
461 #define	CRYPTO_TASKQ_MAX	1024
462 
463 extern int crypto_taskq_minalloc;
464 extern int crypto_taskq_maxalloc;
465 extern kcf_global_swq_t *gswq;
466 extern int kcf_maxthreads;
467 extern int kcf_minthreads;
468 
469 /* Door handle for talking to kcfd */
470 extern door_handle_t kcf_dh;
471 extern kmutex_t	 kcf_dh_lock;
472 
473 /*
474  * All pending crypto bufcalls are put on a list. cbuf_list_lock
475  * protects changes to this list.
476  */
477 extern kmutex_t cbuf_list_lock;
478 extern kcondvar_t cbuf_list_cv;
479 
480 /*
481  * All event subscribers are put on a list. kcf_notify_list_lock
482  * protects changes to this list.
483  */
484 extern kmutex_t ntfy_list_lock;
485 extern kcondvar_t ntfy_list_cv;
486 
487 boolean_t kcf_get_next_logical_provider_member(kcf_provider_desc_t *,
488     kcf_provider_desc_t *, kcf_provider_desc_t **);
489 extern int kcf_get_hardware_provider(crypto_mech_type_t, crypto_mech_type_t,
490     offset_t, offset_t, boolean_t, kcf_provider_desc_t *,
491     kcf_provider_desc_t **);
492 extern int kcf_get_hardware_provider_nomech(offset_t, offset_t,
493     boolean_t, kcf_provider_desc_t *, kcf_provider_desc_t **);
494 extern void kcf_free_triedlist(kcf_prov_tried_t *);
495 extern kcf_prov_tried_t *kcf_insert_triedlist(kcf_prov_tried_t **,
496     kcf_provider_desc_t *, int);
497 extern kcf_provider_desc_t *kcf_get_mech_provider(crypto_mech_type_t,
498     kcf_mech_entry_t **, int *, kcf_prov_tried_t *, crypto_func_group_t,
499     boolean_t, size_t);
500 extern kcf_provider_desc_t *kcf_get_dual_provider(crypto_mechanism_t *,
501     crypto_mechanism_t *, kcf_mech_entry_t **, crypto_mech_type_t *,
502     crypto_mech_type_t *, int *, kcf_prov_tried_t *,
503     crypto_func_group_t, crypto_func_group_t, boolean_t, size_t);
504 extern crypto_ctx_t *kcf_new_ctx(crypto_call_req_t  *, kcf_provider_desc_t *,
505     crypto_session_id_t);
506 extern int kcf_submit_request(kcf_provider_desc_t *, crypto_ctx_t *,
507     crypto_call_req_t *, kcf_req_params_t *, boolean_t);
508 extern void kcf_sched_init(void);
509 extern void kcf_sched_start(void);
510 extern void kcf_sop_done(kcf_sreq_node_t *, int);
511 extern void kcf_aop_done(kcf_areq_node_t *, int);
512 extern int common_submit_request(kcf_provider_desc_t *,
513     crypto_ctx_t *, kcf_req_params_t *, crypto_req_handle_t);
514 extern void kcf_free_context(kcf_context_t *);
515 
516 extern int kcf_svc_wait(int *);
517 extern int kcf_svc_do_run(void);
518 extern int kcf_verify_signature(kcf_provider_desc_t *);
519 extern struct modctl *kcf_get_modctl(crypto_provider_info_t *);
520 extern void verify_unverified_providers();
521 extern void kcf_free_req(kcf_areq_node_t *areq);
522 extern void crypto_bufcall_service(void);
523 
524 extern void kcf_walk_ntfylist(uint32_t, void *);
525 
526 extern kcf_dual_req_t *kcf_alloc_req(crypto_call_req_t *);
527 extern void kcf_next_req(void *, int);
528 extern void kcf_last_req(void *, int);
529 
530 #ifdef __cplusplus
531 }
532 #endif
533 
534 #endif /* _SYS_CRYPTO_SCHED_IMPL_H */
535