xref: /freebsd/sys/contrib/openzfs/module/icp/include/sys/crypto/sched_impl.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
14  * Use is subject to license terms.
15  */
16 
17 #ifndef _SYS_CRYPTO_SCHED_IMPL_H
18 #define	_SYS_CRYPTO_SCHED_IMPL_H
19 
20 /*
21  * Scheduler internal structures.
22  */
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include <sys/zfs_context.h>
29 #include <sys/crypto/api.h>
30 #include <sys/crypto/spi.h>
31 #include <sys/crypto/impl.h>
32 #include <sys/crypto/common.h>
33 
34 typedef struct kcf_prov_tried {
35 	kcf_provider_desc_t	*pt_pd;
36 	struct kcf_prov_tried	*pt_next;
37 } kcf_prov_tried_t;
38 
39 #define	IS_FG_SUPPORTED(mdesc, fg)		\
40 	(((mdesc)->pm_mech_info.cm_func_group_mask & (fg)) != 0)
41 
42 #define	IS_PROVIDER_TRIED(pd, tlist)		\
43 	(tlist != NULL && is_in_triedlist(pd, tlist))
44 
45 #define	IS_RECOVERABLE(error)			\
46 	(error == CRYPTO_BUSY ||			\
47 	error == CRYPTO_KEY_SIZE_RANGE)
48 
49 /*
50  * Internal representation of a canonical context. We contain crypto_ctx_t
51  * structure in order to have just one memory allocation. The SPI
52  * ((crypto_ctx_t *)ctx)->cc_framework_private maps to this structure.
53  */
54 typedef struct kcf_context {
55 	crypto_ctx_t		kc_glbl_ctx;
56 	uint_t			kc_refcnt;
57 	kcf_provider_desc_t	*kc_prov_desc;	/* Prov. descriptor */
58 	kcf_provider_desc_t	*kc_sw_prov_desc;	/* Prov. descriptor */
59 } kcf_context_t;
60 
61 /*
62  * Decrement the reference count on the framework private context.
63  * When the last reference is released, the framework private
64  * context structure is freed along with the global context.
65  */
66 #define	KCF_CONTEXT_REFRELE(ictx) {				\
67 	membar_producer();					\
68 	int newval = atomic_add_32_nv(&(ictx)->kc_refcnt, -1);	\
69 	ASSERT(newval != -1);					\
70 	if (newval == 0)					\
71 		kcf_free_context(ictx);				\
72 }
73 
74 /*
75  * Check if we can release the context now. In case of CRYPTO_BUSY,
76  * the client can retry the request using the context,
77  * so we do not release the context.
78  *
79  * This macro should be called only from the final routine in
80  * an init/update/final sequence. We do not release the context in case
81  * of update operations. We require the consumer to free it
82  * explicitly, in case it wants to abandon the operation. This is done
83  * as there may be mechanisms in ECB mode that can continue even if
84  * an operation on a block fails.
85  */
86 #define	KCF_CONTEXT_COND_RELEASE(rv, kcf_ctx) {			\
87 	if (KCF_CONTEXT_DONE(rv))				\
88 		KCF_CONTEXT_REFRELE(kcf_ctx);			\
89 }
90 
91 /*
92  * This macro determines whether we're done with a context.
93  */
94 #define	KCF_CONTEXT_DONE(rv)					\
95 	((rv) != CRYPTO_BUSY &&	(rv) != CRYPTO_BUFFER_TOO_SMALL)
96 
97 
98 #define	KCF_SET_PROVIDER_MECHNUM(fmtype, pd, mechp)			\
99 	(mechp)->cm_type =						\
100 	    KCF_TO_PROV_MECHNUM(pd, fmtype);
101 
102 /*
103  * A crypto_ctx_template_t is internally a pointer to this struct
104  */
105 typedef	struct kcf_ctx_template {
106 	size_t				ct_size;	/* for freeing */
107 	crypto_spi_ctx_template_t	ct_prov_tmpl;	/* context template */
108 							/* from the provider */
109 } kcf_ctx_template_t;
110 
111 
112 extern void kcf_free_triedlist(kcf_prov_tried_t *);
113 extern kcf_prov_tried_t *kcf_insert_triedlist(kcf_prov_tried_t **,
114     kcf_provider_desc_t *, int);
115 extern kcf_provider_desc_t *kcf_get_mech_provider(crypto_mech_type_t,
116     kcf_mech_entry_t **, int *, kcf_prov_tried_t *, crypto_func_group_t);
117 extern crypto_ctx_t *kcf_new_ctx(kcf_provider_desc_t *);
118 extern void kcf_sched_destroy(void);
119 extern void kcf_sched_init(void);
120 extern void kcf_free_context(kcf_context_t *);
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif /* _SYS_CRYPTO_SCHED_IMPL_H */
127