drm/panel: samsung-s6e88a0-ams452ef01: transition to mipi_dsi wrapped functions
[drm/drm-misc.git] / drivers / s390 / crypto / pkey_base.c
blob64a376501d265933275dc7d07af6a31dfdca26c8
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * pkey base: debug feature, pkey handler registry
5 * Copyright IBM Corp. 2024
6 */
8 #define KMSG_COMPONENT "pkey"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/cpufeature.h>
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/rculist.h>
17 #include "pkey_base.h"
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("IBM Corporation");
21 MODULE_DESCRIPTION("s390 protected key base and api");
24 * pkey debug feature
26 debug_info_t *pkey_dbf_info;
27 EXPORT_SYMBOL(pkey_dbf_info);
30 * pkey handler registry
33 static DEFINE_SPINLOCK(handler_list_write_lock);
34 static LIST_HEAD(handler_list);
36 int pkey_handler_register(struct pkey_handler *handler)
38 const struct pkey_handler *h;
40 if (!handler ||
41 !handler->is_supported_key ||
42 !handler->is_supported_keytype)
43 return -EINVAL;
45 if (!try_module_get(handler->module))
46 return -ENXIO;
48 spin_lock(&handler_list_write_lock);
50 rcu_read_lock();
51 list_for_each_entry_rcu(h, &handler_list, list) {
52 if (h == handler) {
53 rcu_read_unlock();
54 spin_unlock(&handler_list_write_lock);
55 module_put(handler->module);
56 return -EEXIST;
59 rcu_read_unlock();
61 list_add_rcu(&handler->list, &handler_list);
62 spin_unlock(&handler_list_write_lock);
63 synchronize_rcu();
65 module_put(handler->module);
67 PKEY_DBF_INFO("%s pkey handler '%s' registered\n", __func__,
68 handler->name ?: "<no name>");
70 return 0;
72 EXPORT_SYMBOL(pkey_handler_register);
74 int pkey_handler_unregister(struct pkey_handler *handler)
76 spin_lock(&handler_list_write_lock);
77 list_del_rcu(&handler->list);
78 INIT_LIST_HEAD_RCU(&handler->list);
79 spin_unlock(&handler_list_write_lock);
80 synchronize_rcu();
82 PKEY_DBF_INFO("%s pkey handler '%s' unregistered\n", __func__,
83 handler->name ?: "<no name>");
85 return 0;
87 EXPORT_SYMBOL(pkey_handler_unregister);
90 * Handler invocation functions.
93 const struct pkey_handler *pkey_handler_get_keybased(const u8 *key, u32 keylen)
95 const struct pkey_handler *h;
97 rcu_read_lock();
98 list_for_each_entry_rcu(h, &handler_list, list) {
99 if (!try_module_get(h->module))
100 continue;
101 if (h->is_supported_key(key, keylen)) {
102 rcu_read_unlock();
103 return h;
105 module_put(h->module);
107 rcu_read_unlock();
109 return NULL;
111 EXPORT_SYMBOL(pkey_handler_get_keybased);
113 const struct pkey_handler *pkey_handler_get_keytypebased(enum pkey_key_type kt)
115 const struct pkey_handler *h;
117 rcu_read_lock();
118 list_for_each_entry_rcu(h, &handler_list, list) {
119 if (!try_module_get(h->module))
120 continue;
121 if (h->is_supported_keytype(kt)) {
122 rcu_read_unlock();
123 return h;
125 module_put(h->module);
127 rcu_read_unlock();
129 return NULL;
131 EXPORT_SYMBOL(pkey_handler_get_keytypebased);
133 void pkey_handler_put(const struct pkey_handler *handler)
135 const struct pkey_handler *h;
137 if (!handler)
138 return;
140 rcu_read_lock();
141 list_for_each_entry_rcu(h, &handler_list, list) {
142 if (h == handler) {
143 module_put(h->module);
144 break;
147 rcu_read_unlock();
149 EXPORT_SYMBOL(pkey_handler_put);
151 int pkey_handler_key_to_protkey(const struct pkey_apqn *apqns, size_t nr_apqns,
152 const u8 *key, u32 keylen,
153 u8 *protkey, u32 *protkeylen, u32 *protkeytype)
155 const struct pkey_handler *h;
156 int rc = -ENODEV;
158 h = pkey_handler_get_keybased(key, keylen);
159 if (h && h->key_to_protkey) {
160 rc = h->key_to_protkey(apqns, nr_apqns, key, keylen,
161 protkey, protkeylen,
162 protkeytype);
164 pkey_handler_put(h);
166 return rc;
168 EXPORT_SYMBOL(pkey_handler_key_to_protkey);
171 * This handler invocation is special as there may be more than
172 * one handler providing support for the very same key (type).
173 * And the handler may not respond true on is_supported_key(),
174 * so simple try and check return value here.
176 int pkey_handler_slowpath_key_to_protkey(const struct pkey_apqn *apqns,
177 size_t nr_apqns,
178 const u8 *key, u32 keylen,
179 u8 *protkey, u32 *protkeylen,
180 u32 *protkeytype)
182 const struct pkey_handler *h, *htmp[10];
183 int i, n = 0, rc = -ENODEV;
185 rcu_read_lock();
186 list_for_each_entry_rcu(h, &handler_list, list) {
187 if (!try_module_get(h->module))
188 continue;
189 if (h->slowpath_key_to_protkey && n < ARRAY_SIZE(htmp))
190 htmp[n++] = h;
191 else
192 module_put(h->module);
194 rcu_read_unlock();
196 for (i = 0; i < n; i++) {
197 h = htmp[i];
198 if (rc)
199 rc = h->slowpath_key_to_protkey(apqns, nr_apqns,
200 key, keylen,
201 protkey, protkeylen,
202 protkeytype);
203 module_put(h->module);
206 return rc;
208 EXPORT_SYMBOL(pkey_handler_slowpath_key_to_protkey);
210 int pkey_handler_gen_key(const struct pkey_apqn *apqns, size_t nr_apqns,
211 u32 keytype, u32 keysubtype,
212 u32 keybitsize, u32 flags,
213 u8 *keybuf, u32 *keybuflen, u32 *keyinfo)
215 const struct pkey_handler *h;
216 int rc = -ENODEV;
218 h = pkey_handler_get_keytypebased(keysubtype);
219 if (h && h->gen_key) {
220 rc = h->gen_key(apqns, nr_apqns, keytype, keysubtype,
221 keybitsize, flags,
222 keybuf, keybuflen, keyinfo);
224 pkey_handler_put(h);
226 return rc;
228 EXPORT_SYMBOL(pkey_handler_gen_key);
230 int pkey_handler_clr_to_key(const struct pkey_apqn *apqns, size_t nr_apqns,
231 u32 keytype, u32 keysubtype,
232 u32 keybitsize, u32 flags,
233 const u8 *clrkey, u32 clrkeylen,
234 u8 *keybuf, u32 *keybuflen, u32 *keyinfo)
236 const struct pkey_handler *h;
237 int rc = -ENODEV;
239 h = pkey_handler_get_keytypebased(keysubtype);
240 if (h && h->clr_to_key) {
241 rc = h->clr_to_key(apqns, nr_apqns, keytype, keysubtype,
242 keybitsize, flags, clrkey, clrkeylen,
243 keybuf, keybuflen, keyinfo);
245 pkey_handler_put(h);
247 return rc;
249 EXPORT_SYMBOL(pkey_handler_clr_to_key);
251 int pkey_handler_verify_key(const u8 *key, u32 keylen,
252 u16 *card, u16 *dom,
253 u32 *keytype, u32 *keybitsize, u32 *flags)
255 const struct pkey_handler *h;
256 int rc = -ENODEV;
258 h = pkey_handler_get_keybased(key, keylen);
259 if (h && h->verify_key) {
260 rc = h->verify_key(key, keylen, card, dom,
261 keytype, keybitsize, flags);
263 pkey_handler_put(h);
265 return rc;
267 EXPORT_SYMBOL(pkey_handler_verify_key);
269 int pkey_handler_apqns_for_key(const u8 *key, u32 keylen, u32 flags,
270 struct pkey_apqn *apqns, size_t *nr_apqns)
272 const struct pkey_handler *h;
273 int rc = -ENODEV;
275 h = pkey_handler_get_keybased(key, keylen);
276 if (h && h->apqns_for_key)
277 rc = h->apqns_for_key(key, keylen, flags, apqns, nr_apqns);
278 pkey_handler_put(h);
280 return rc;
282 EXPORT_SYMBOL(pkey_handler_apqns_for_key);
284 int pkey_handler_apqns_for_keytype(enum pkey_key_type keysubtype,
285 u8 cur_mkvp[32], u8 alt_mkvp[32], u32 flags,
286 struct pkey_apqn *apqns, size_t *nr_apqns)
288 const struct pkey_handler *h;
289 int rc = -ENODEV;
291 h = pkey_handler_get_keytypebased(keysubtype);
292 if (h && h->apqns_for_keytype) {
293 rc = h->apqns_for_keytype(keysubtype,
294 cur_mkvp, alt_mkvp, flags,
295 apqns, nr_apqns);
297 pkey_handler_put(h);
299 return rc;
301 EXPORT_SYMBOL(pkey_handler_apqns_for_keytype);
303 void pkey_handler_request_modules(void)
305 #ifdef CONFIG_MODULES
306 static const char * const pkey_handler_modules[] = {
307 #if IS_MODULE(CONFIG_PKEY_CCA)
308 "pkey_cca",
309 #endif
310 #if IS_MODULE(CONFIG_PKEY_EP11)
311 "pkey_ep11",
312 #endif
313 #if IS_MODULE(CONFIG_PKEY_PCKMO)
314 "pkey_pckmo",
315 #endif
316 #if IS_MODULE(CONFIG_PKEY_UV)
317 "pkey_uv",
318 #endif
320 int i;
322 for (i = 0; i < ARRAY_SIZE(pkey_handler_modules); i++) {
323 const struct pkey_handler *h;
324 bool found = false;
326 rcu_read_lock();
327 list_for_each_entry_rcu(h, &handler_list, list) {
328 if (h->module &&
329 !strcmp(h->module->name, pkey_handler_modules[i])) {
330 found = true;
331 break;
334 rcu_read_unlock();
335 if (!found) {
336 pr_debug("request_module(%s)\n", pkey_handler_modules[i]);
337 request_module(pkey_handler_modules[i]);
340 #endif
342 EXPORT_SYMBOL(pkey_handler_request_modules);
345 * Module init
347 static int __init pkey_init(void)
349 int rc;
351 /* init debug feature */
352 pkey_dbf_info = debug_register("pkey", 1, 1, 5 * sizeof(long));
353 debug_register_view(pkey_dbf_info, &debug_sprintf_view);
354 debug_set_level(pkey_dbf_info, 4);
356 /* the handler registry does not need any init */
358 rc = pkey_api_init();
359 if (rc)
360 debug_unregister(pkey_dbf_info);
362 return rc;
366 * Module exit
368 static void __exit pkey_exit(void)
370 pkey_api_exit();
373 module_cpu_feature_match(S390_CPU_FEATURE_MSA, pkey_init);
374 module_exit(pkey_exit);