1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright IBM Corp. 2001, 2018
4 * Author(s): Robert Burroughs
5 * Eric Rossman (edrossma@us.ibm.com)
6 * Cornelia Huck <cornelia.huck@de.ibm.com>
7 *
8 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Ralph Wuerthner <rwuerthn@de.ibm.com>
11 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
12 * Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
13 */
14
15 #define pr_fmt(fmt) "zcrypt: " fmt
16
17 #include <linux/export.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/miscdevice.h>
22 #include <linux/fs.h>
23 #include <linux/slab.h>
24 #include <linux/atomic.h>
25 #include <linux/uaccess.h>
26 #include <linux/hw_random.h>
27 #include <linux/debugfs.h>
28 #include <linux/cdev.h>
29 #include <linux/ctype.h>
30 #include <linux/capability.h>
31 #include <asm/debug.h>
32
33 #define CREATE_TRACE_POINTS
34 #include <asm/trace/zcrypt.h>
35
36 #include "zcrypt_api.h"
37 #include "zcrypt_debug.h"
38
39 #include "zcrypt_msgtype6.h"
40 #include "zcrypt_msgtype50.h"
41 #include "zcrypt_ccamisc.h"
42 #include "zcrypt_ep11misc.h"
43
44 /*
45 * Module description.
46 */
47 MODULE_AUTHOR("IBM Corporation");
48 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
49 "Copyright IBM Corp. 2001, 2012");
50 MODULE_LICENSE("GPL");
51
52 unsigned int zcrypt_mempool_threshold = 5;
53 module_param_named(mempool_threshold, zcrypt_mempool_threshold, uint, 0400);
54 MODULE_PARM_DESC(mempool_threshold, "CCA and EP11 request/reply mempool minimal items (min: 1)");
55
56 /*
57 * zcrypt tracepoint functions
58 */
59 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
60 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
61
62 DEFINE_SPINLOCK(zcrypt_list_lock);
63 LIST_HEAD(zcrypt_card_list);
64
65 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
66
67 static LIST_HEAD(zcrypt_ops_list);
68
69 /* Zcrypt related debug feature stuff. */
70 debug_info_t *zcrypt_dbf_info;
71
72 /*
73 * Process a rescan of the transport layer.
74 * Runs a synchronous AP bus rescan.
75 * Returns true if something has changed (for example the
76 * bus scan has found and build up new devices) and it is
77 * worth to do a retry. Otherwise false is returned meaning
78 * no changes on the AP bus level.
79 */
zcrypt_process_rescan(void)80 static inline bool zcrypt_process_rescan(void)
81 {
82 return ap_bus_force_rescan();
83 }
84
zcrypt_msgtype_register(struct zcrypt_ops * zops)85 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
86 {
87 list_add_tail(&zops->list, &zcrypt_ops_list);
88 }
89
zcrypt_msgtype_unregister(struct zcrypt_ops * zops)90 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
91 {
92 list_del_init(&zops->list);
93 }
94
zcrypt_msgtype(unsigned char * name,int variant)95 struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
96 {
97 struct zcrypt_ops *zops;
98
99 list_for_each_entry(zops, &zcrypt_ops_list, list)
100 if (zops->variant == variant &&
101 (!strncmp(zops->name, name, sizeof(zops->name))))
102 return zops;
103 return NULL;
104 }
105 EXPORT_SYMBOL(zcrypt_msgtype);
106
107 /*
108 * Multi device nodes extension functions.
109 */
110
111 struct zcdn_device;
112
113 static void zcdn_device_release(struct device *dev);
114 static const struct class zcrypt_class = {
115 .name = ZCRYPT_NAME,
116 .dev_release = zcdn_device_release,
117 };
118 static dev_t zcrypt_devt;
119 static struct cdev zcrypt_cdev;
120
121 struct zcdn_device {
122 struct device device;
123 struct ap_perms perms;
124 };
125
126 #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
127
128 #define ZCDN_MAX_NAME 32
129
130 static int zcdn_create(const char *name);
131 static int zcdn_destroy(const char *name);
132
133 /*
134 * Find zcdn device by name.
135 * Returns reference to the zcdn device which needs to be released
136 * with put_device() after use.
137 */
find_zcdndev_by_name(const char * name)138 static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
139 {
140 struct device *dev = class_find_device_by_name(&zcrypt_class, name);
141
142 return dev ? to_zcdn_dev(dev) : NULL;
143 }
144
145 /*
146 * Find zcdn device by devt value.
147 * Returns reference to the zcdn device which needs to be released
148 * with put_device() after use.
149 */
find_zcdndev_by_devt(dev_t devt)150 static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
151 {
152 struct device *dev = class_find_device_by_devt(&zcrypt_class, devt);
153
154 return dev ? to_zcdn_dev(dev) : NULL;
155 }
156
ioctlmask_show(struct device * dev,struct device_attribute * attr,char * buf)157 static ssize_t ioctlmask_show(struct device *dev,
158 struct device_attribute *attr,
159 char *buf)
160 {
161 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
162 int i, n;
163
164 if (mutex_lock_interruptible(&ap_attr_mutex))
165 return -ERESTARTSYS;
166
167 n = sysfs_emit(buf, "0x");
168 for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
169 n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.ioctlm[i]);
170 n += sysfs_emit_at(buf, n, "\n");
171
172 mutex_unlock(&ap_attr_mutex);
173
174 return n;
175 }
176
ioctlmask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)177 static ssize_t ioctlmask_store(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf, size_t count)
180 {
181 int rc;
182 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
183
184 rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
185 AP_IOCTLS, &ap_attr_mutex);
186 if (rc)
187 return rc;
188
189 return count;
190 }
191
192 static DEVICE_ATTR_RW(ioctlmask);
193
apmask_show(struct device * dev,struct device_attribute * attr,char * buf)194 static ssize_t apmask_show(struct device *dev,
195 struct device_attribute *attr,
196 char *buf)
197 {
198 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
199 int i, n;
200
201 if (mutex_lock_interruptible(&ap_attr_mutex))
202 return -ERESTARTSYS;
203
204 n = sysfs_emit(buf, "0x");
205 for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
206 n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.apm[i]);
207 n += sysfs_emit_at(buf, n, "\n");
208
209 mutex_unlock(&ap_attr_mutex);
210
211 return n;
212 }
213
apmask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)214 static ssize_t apmask_store(struct device *dev,
215 struct device_attribute *attr,
216 const char *buf, size_t count)
217 {
218 int rc;
219 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
220
221 rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
222 AP_DEVICES, &ap_attr_mutex);
223 if (rc)
224 return rc;
225
226 return count;
227 }
228
229 static DEVICE_ATTR_RW(apmask);
230
aqmask_show(struct device * dev,struct device_attribute * attr,char * buf)231 static ssize_t aqmask_show(struct device *dev,
232 struct device_attribute *attr,
233 char *buf)
234 {
235 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
236 int i, n;
237
238 if (mutex_lock_interruptible(&ap_attr_mutex))
239 return -ERESTARTSYS;
240
241 n = sysfs_emit(buf, "0x");
242 for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
243 n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.aqm[i]);
244 n += sysfs_emit_at(buf, n, "\n");
245
246 mutex_unlock(&ap_attr_mutex);
247
248 return n;
249 }
250
aqmask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)251 static ssize_t aqmask_store(struct device *dev,
252 struct device_attribute *attr,
253 const char *buf, size_t count)
254 {
255 int rc;
256 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
257
258 rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
259 AP_DOMAINS, &ap_attr_mutex);
260 if (rc)
261 return rc;
262
263 return count;
264 }
265
266 static DEVICE_ATTR_RW(aqmask);
267
admask_show(struct device * dev,struct device_attribute * attr,char * buf)268 static ssize_t admask_show(struct device *dev,
269 struct device_attribute *attr,
270 char *buf)
271 {
272 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
273 int i, n;
274
275 if (mutex_lock_interruptible(&ap_attr_mutex))
276 return -ERESTARTSYS;
277
278 n = sysfs_emit(buf, "0x");
279 for (i = 0; i < sizeof(zcdndev->perms.adm) / sizeof(long); i++)
280 n += sysfs_emit_at(buf, n, "%016lx", zcdndev->perms.adm[i]);
281 n += sysfs_emit_at(buf, n, "\n");
282
283 mutex_unlock(&ap_attr_mutex);
284
285 return n;
286 }
287
admask_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)288 static ssize_t admask_store(struct device *dev,
289 struct device_attribute *attr,
290 const char *buf, size_t count)
291 {
292 int rc;
293 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
294
295 rc = ap_parse_mask_str(buf, zcdndev->perms.adm,
296 AP_DOMAINS, &ap_attr_mutex);
297 if (rc)
298 return rc;
299
300 return count;
301 }
302
303 static DEVICE_ATTR_RW(admask);
304
305 static struct attribute *zcdn_dev_attrs[] = {
306 &dev_attr_ioctlmask.attr,
307 &dev_attr_apmask.attr,
308 &dev_attr_aqmask.attr,
309 &dev_attr_admask.attr,
310 NULL
311 };
312
313 static struct attribute_group zcdn_dev_attr_group = {
314 .attrs = zcdn_dev_attrs
315 };
316
317 static const struct attribute_group *zcdn_dev_attr_groups[] = {
318 &zcdn_dev_attr_group,
319 NULL
320 };
321
zcdn_create_store(const struct class * class,const struct class_attribute * attr,const char * buf,size_t count)322 static ssize_t zcdn_create_store(const struct class *class,
323 const struct class_attribute *attr,
324 const char *buf, size_t count)
325 {
326 int rc;
327 char name[ZCDN_MAX_NAME];
328
329 strscpy(name, skip_spaces(buf), sizeof(name));
330
331 rc = zcdn_create(strim(name));
332
333 return rc ? rc : count;
334 }
335
336 static const struct class_attribute class_attr_zcdn_create =
337 __ATTR(create, 0600, NULL, zcdn_create_store);
338
zcdn_destroy_store(const struct class * class,const struct class_attribute * attr,const char * buf,size_t count)339 static ssize_t zcdn_destroy_store(const struct class *class,
340 const struct class_attribute *attr,
341 const char *buf, size_t count)
342 {
343 int rc;
344 char name[ZCDN_MAX_NAME];
345
346 strscpy(name, skip_spaces(buf), sizeof(name));
347
348 rc = zcdn_destroy(strim(name));
349
350 return rc ? rc : count;
351 }
352
353 static const struct class_attribute class_attr_zcdn_destroy =
354 __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
355
zcdn_device_release(struct device * dev)356 static void zcdn_device_release(struct device *dev)
357 {
358 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
359
360 ZCRYPT_DBF_INFO("%s releasing zcdn device %d:%d\n",
361 __func__, MAJOR(dev->devt), MINOR(dev->devt));
362
363 kfree(zcdndev);
364 }
365
zcdn_create(const char * name)366 static int zcdn_create(const char *name)
367 {
368 dev_t devt;
369 int i, rc = 0;
370 struct zcdn_device *zcdndev;
371
372 if (mutex_lock_interruptible(&ap_attr_mutex))
373 return -ERESTARTSYS;
374
375 /* check if device node with this name already exists */
376 if (name[0]) {
377 zcdndev = find_zcdndev_by_name(name);
378 if (zcdndev) {
379 put_device(&zcdndev->device);
380 rc = -EEXIST;
381 goto unlockout;
382 }
383 }
384
385 /* find an unused minor number */
386 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
387 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
388 zcdndev = find_zcdndev_by_devt(devt);
389 if (zcdndev)
390 put_device(&zcdndev->device);
391 else
392 break;
393 }
394 if (i == ZCRYPT_MAX_MINOR_NODES) {
395 rc = -ENOSPC;
396 goto unlockout;
397 }
398
399 /* alloc and prepare a new zcdn device */
400 zcdndev = kzalloc_obj(*zcdndev);
401 if (!zcdndev) {
402 rc = -ENOMEM;
403 goto unlockout;
404 }
405 zcdndev->device.release = zcdn_device_release;
406 zcdndev->device.class = &zcrypt_class;
407 zcdndev->device.devt = devt;
408 zcdndev->device.groups = zcdn_dev_attr_groups;
409 if (name[0])
410 rc = dev_set_name(&zcdndev->device, "%s", name);
411 else
412 rc = dev_set_name(&zcdndev->device, ZCRYPT_NAME "_%d", (int)MINOR(devt));
413 if (rc) {
414 kfree(zcdndev);
415 goto unlockout;
416 }
417 rc = device_register(&zcdndev->device);
418 if (rc) {
419 put_device(&zcdndev->device);
420 goto unlockout;
421 }
422
423 ZCRYPT_DBF_INFO("%s created zcdn device %d:%d\n",
424 __func__, MAJOR(devt), MINOR(devt));
425
426 unlockout:
427 mutex_unlock(&ap_attr_mutex);
428 return rc;
429 }
430
zcdn_destroy(const char * name)431 static int zcdn_destroy(const char *name)
432 {
433 int rc = 0;
434 struct zcdn_device *zcdndev;
435
436 if (mutex_lock_interruptible(&ap_attr_mutex))
437 return -ERESTARTSYS;
438
439 /* try to find this zcdn device */
440 zcdndev = find_zcdndev_by_name(name);
441 if (!zcdndev) {
442 rc = -ENOENT;
443 goto unlockout;
444 }
445
446 /*
447 * The zcdn device is not hard destroyed. It is subject to
448 * reference counting and thus just needs to be unregistered.
449 */
450 put_device(&zcdndev->device);
451 device_unregister(&zcdndev->device);
452
453 unlockout:
454 mutex_unlock(&ap_attr_mutex);
455 return rc;
456 }
457
zcdn_destroy_all(void)458 static void zcdn_destroy_all(void)
459 {
460 int i;
461 dev_t devt;
462 struct zcdn_device *zcdndev;
463
464 mutex_lock(&ap_attr_mutex);
465 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
466 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
467 zcdndev = find_zcdndev_by_devt(devt);
468 if (zcdndev) {
469 put_device(&zcdndev->device);
470 device_unregister(&zcdndev->device);
471 }
472 }
473 mutex_unlock(&ap_attr_mutex);
474 }
475
476 /*
477 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
478 *
479 * This function is not supported beyond zcrypt 1.3.1.
480 */
zcrypt_read(struct file * filp,char __user * buf,size_t count,loff_t * f_pos)481 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
482 size_t count, loff_t *f_pos)
483 {
484 return -EPERM;
485 }
486
487 /*
488 * zcrypt_write(): Not allowed.
489 *
490 * Write is not allowed
491 */
zcrypt_write(struct file * filp,const char __user * buf,size_t count,loff_t * f_pos)492 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
493 size_t count, loff_t *f_pos)
494 {
495 return -EPERM;
496 }
497
498 /*
499 * zcrypt_open(): Count number of users.
500 *
501 * Device open function to count number of users.
502 */
zcrypt_open(struct inode * inode,struct file * filp)503 static int zcrypt_open(struct inode *inode, struct file *filp)
504 {
505 struct ap_perms *perms = &ap_perms;
506
507 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
508 struct zcdn_device *zcdndev;
509
510 if (mutex_lock_interruptible(&ap_attr_mutex))
511 return -ERESTARTSYS;
512 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
513 /* find returns a reference, no get_device() needed */
514 mutex_unlock(&ap_attr_mutex);
515 if (zcdndev)
516 perms = &zcdndev->perms;
517 }
518 filp->private_data = (void *)perms;
519
520 atomic_inc(&zcrypt_open_count);
521 return stream_open(inode, filp);
522 }
523
524 /*
525 * zcrypt_release(): Count number of users.
526 *
527 * Device close function to count number of users.
528 */
zcrypt_release(struct inode * inode,struct file * filp)529 static int zcrypt_release(struct inode *inode, struct file *filp)
530 {
531 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
532 struct zcdn_device *zcdndev;
533
534 mutex_lock(&ap_attr_mutex);
535 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
536 mutex_unlock(&ap_attr_mutex);
537 if (zcdndev) {
538 /* 2 puts here: one for find, one for open */
539 put_device(&zcdndev->device);
540 put_device(&zcdndev->device);
541 }
542 }
543
544 atomic_dec(&zcrypt_open_count);
545 return 0;
546 }
547
zcrypt_check_ioctl(struct ap_perms * perms,unsigned int cmd)548 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
549 unsigned int cmd)
550 {
551 int rc = -EPERM;
552 int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
553
554 if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
555 if (test_bit_inv(ioctlnr, perms->ioctlm))
556 rc = 0;
557 }
558
559 if (rc)
560 ZCRYPT_DBF_WARN("%s ioctl check failed: ioctlnr=0x%04x rc=%d\n",
561 __func__, ioctlnr, rc);
562
563 return rc;
564 }
565
zcrypt_check_card(struct ap_perms * perms,int card)566 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
567 {
568 return test_bit_inv(card, perms->apm) ? true : false;
569 }
570
zcrypt_check_queue(struct ap_perms * perms,int queue)571 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
572 {
573 return test_bit_inv(queue, perms->aqm) ? true : false;
574 }
575
zcrypt_pick_queue(struct zcrypt_card * zc,struct zcrypt_queue * zq,struct module ** pmod,unsigned int weight)576 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
577 struct zcrypt_queue *zq,
578 struct module **pmod,
579 unsigned int weight)
580 {
581 if (!zq || !try_module_get(zq->queue->ap_dev.device.driver->owner))
582 return NULL;
583 zcrypt_card_get(zc);
584 zcrypt_queue_get(zq);
585 get_device(&zq->queue->ap_dev.device);
586 atomic_add(weight, &zc->load);
587 atomic_add(weight, &zq->load);
588 zq->request_count++;
589 *pmod = zq->queue->ap_dev.device.driver->owner;
590 return zq;
591 }
592
zcrypt_drop_queue(struct zcrypt_card * zc,struct zcrypt_queue * zq,struct module * mod,unsigned int weight)593 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
594 struct zcrypt_queue *zq,
595 struct module *mod,
596 unsigned int weight)
597 {
598 zq->request_count--;
599 atomic_sub(weight, &zc->load);
600 atomic_sub(weight, &zq->load);
601 put_device(&zq->queue->ap_dev.device);
602 zcrypt_queue_put(zq);
603 zcrypt_card_put(zc);
604 module_put(mod);
605 }
606
zcrypt_card_compare(struct zcrypt_card * zc,struct zcrypt_card * pref_zc,unsigned int weight,unsigned int pref_weight)607 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
608 struct zcrypt_card *pref_zc,
609 unsigned int weight,
610 unsigned int pref_weight)
611 {
612 if (!pref_zc)
613 return true;
614 weight += atomic_read(&zc->load);
615 pref_weight += atomic_read(&pref_zc->load);
616 if (weight == pref_weight)
617 return atomic64_read(&zc->card->total_request_count) <
618 atomic64_read(&pref_zc->card->total_request_count);
619 return weight < pref_weight;
620 }
621
zcrypt_queue_compare(struct zcrypt_queue * zq,struct zcrypt_queue * pref_zq,unsigned int weight,unsigned int pref_weight)622 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
623 struct zcrypt_queue *pref_zq,
624 unsigned int weight,
625 unsigned int pref_weight)
626 {
627 if (!pref_zq)
628 return true;
629 weight += atomic_read(&zq->load);
630 pref_weight += atomic_read(&pref_zq->load);
631 if (weight == pref_weight)
632 return zq->queue->total_request_count <
633 pref_zq->queue->total_request_count;
634 return weight < pref_weight;
635 }
636
637 /*
638 * zcrypt ioctls.
639 */
zcrypt_rsa_modexpo(struct ap_perms * perms,struct zcrypt_track * tr,struct ica_rsa_modexpo * mex)640 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
641 struct zcrypt_track *tr,
642 struct ica_rsa_modexpo *mex)
643 {
644 struct zcrypt_card *zc, *pref_zc;
645 struct zcrypt_queue *zq, *pref_zq;
646 struct ap_message ap_msg;
647 unsigned int wgt = 0, pref_wgt = 0;
648 unsigned int func_code = 0;
649 int cpen, qpen, qid = 0, rc;
650 struct module *mod;
651
652 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
653
654 rc = ap_init_apmsg(&ap_msg, 0);
655 if (rc)
656 goto out;
657
658 if (mex->outputdatalength < mex->inputdatalength) {
659 rc = -EINVAL;
660 goto out;
661 }
662
663 /*
664 * As long as outputdatalength is big enough, we can set the
665 * outputdatalength equal to the inputdatalength, since that is the
666 * number of bytes we will copy in any case
667 */
668 mex->outputdatalength = mex->inputdatalength;
669
670 rc = get_rsa_modex_fc(mex, &func_code);
671 if (rc)
672 goto out;
673
674 pref_zc = NULL;
675 pref_zq = NULL;
676 spin_lock(&zcrypt_list_lock);
677 for_each_zcrypt_card(zc) {
678 /* Check for usable accelerator or CCA card */
679 if (!zc->online || !zc->card->config || zc->card->chkstop ||
680 !(zc->card->hwinfo.accel || zc->card->hwinfo.cca))
681 continue;
682 /* Check for size limits */
683 if (zc->min_mod_size > mex->inputdatalength ||
684 zc->max_mod_size < mex->inputdatalength)
685 continue;
686 /* check if device node has admission for this card */
687 if (!zcrypt_check_card(perms, zc->card->id))
688 continue;
689 /* get weight index of the card device */
690 wgt = zc->speed_rating[func_code];
691 /* penalty if this msg was previously sent via this card */
692 cpen = (tr && tr->again_counter && tr->last_qid &&
693 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
694 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
695 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
696 continue;
697 for_each_zcrypt_queue(zq, zc) {
698 /* check if device is usable and eligible */
699 if (!zq->online || !zq->ops->rsa_modexpo ||
700 !ap_queue_usable(zq->queue))
701 continue;
702 /* check if device node has admission for this queue */
703 if (!zcrypt_check_queue(perms,
704 AP_QID_QUEUE(zq->queue->qid)))
705 continue;
706 /* penalty if the msg was previously sent at this qid */
707 qpen = (tr && tr->again_counter && tr->last_qid &&
708 tr->last_qid == zq->queue->qid) ?
709 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
710 if (!zcrypt_queue_compare(zq, pref_zq,
711 wgt + cpen + qpen, pref_wgt))
712 continue;
713 pref_zc = zc;
714 pref_zq = zq;
715 pref_wgt = wgt + cpen + qpen;
716 }
717 }
718 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
719 spin_unlock(&zcrypt_list_lock);
720
721 if (!pref_zq) {
722 pr_debug("no matching queue found => ENODEV\n");
723 rc = -ENODEV;
724 goto out;
725 }
726
727 qid = pref_zq->queue->qid;
728 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
729
730 spin_lock(&zcrypt_list_lock);
731 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
732 spin_unlock(&zcrypt_list_lock);
733
734 out:
735 ap_release_apmsg(&ap_msg);
736 if (tr) {
737 tr->last_rc = rc;
738 tr->last_qid = qid;
739 }
740 trace_s390_zcrypt_rep(mex, func_code, rc,
741 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
742 ap_msg.psmid);
743 return rc;
744 }
745
zcrypt_rsa_crt(struct ap_perms * perms,struct zcrypt_track * tr,struct ica_rsa_modexpo_crt * crt)746 static long zcrypt_rsa_crt(struct ap_perms *perms,
747 struct zcrypt_track *tr,
748 struct ica_rsa_modexpo_crt *crt)
749 {
750 struct zcrypt_card *zc, *pref_zc;
751 struct zcrypt_queue *zq, *pref_zq;
752 struct ap_message ap_msg;
753 unsigned int wgt = 0, pref_wgt = 0;
754 unsigned int func_code = 0;
755 int cpen, qpen, qid = 0, rc;
756 struct module *mod;
757
758 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
759
760 rc = ap_init_apmsg(&ap_msg, 0);
761 if (rc)
762 goto out;
763
764 if (crt->outputdatalength < crt->inputdatalength) {
765 rc = -EINVAL;
766 goto out;
767 }
768
769 /*
770 * As long as outputdatalength is big enough, we can set the
771 * outputdatalength equal to the inputdatalength, since that is the
772 * number of bytes we will copy in any case
773 */
774 crt->outputdatalength = crt->inputdatalength;
775
776 rc = get_rsa_crt_fc(crt, &func_code);
777 if (rc)
778 goto out;
779
780 pref_zc = NULL;
781 pref_zq = NULL;
782 spin_lock(&zcrypt_list_lock);
783 for_each_zcrypt_card(zc) {
784 /* Check for usable accelerator or CCA card */
785 if (!zc->online || !zc->card->config || zc->card->chkstop ||
786 !(zc->card->hwinfo.accel || zc->card->hwinfo.cca))
787 continue;
788 /* Check for size limits */
789 if (zc->min_mod_size > crt->inputdatalength ||
790 zc->max_mod_size < crt->inputdatalength)
791 continue;
792 /* check if device node has admission for this card */
793 if (!zcrypt_check_card(perms, zc->card->id))
794 continue;
795 /* get weight index of the card device */
796 wgt = zc->speed_rating[func_code];
797 /* penalty if this msg was previously sent via this card */
798 cpen = (tr && tr->again_counter && tr->last_qid &&
799 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
800 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
801 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
802 continue;
803 for_each_zcrypt_queue(zq, zc) {
804 /* check if device is usable and eligible */
805 if (!zq->online || !zq->ops->rsa_modexpo_crt ||
806 !ap_queue_usable(zq->queue))
807 continue;
808 /* check if device node has admission for this queue */
809 if (!zcrypt_check_queue(perms,
810 AP_QID_QUEUE(zq->queue->qid)))
811 continue;
812 /* penalty if the msg was previously sent at this qid */
813 qpen = (tr && tr->again_counter && tr->last_qid &&
814 tr->last_qid == zq->queue->qid) ?
815 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
816 if (!zcrypt_queue_compare(zq, pref_zq,
817 wgt + cpen + qpen, pref_wgt))
818 continue;
819 pref_zc = zc;
820 pref_zq = zq;
821 pref_wgt = wgt + cpen + qpen;
822 }
823 }
824 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
825 spin_unlock(&zcrypt_list_lock);
826
827 if (!pref_zq) {
828 pr_debug("no matching queue found => ENODEV\n");
829 rc = -ENODEV;
830 goto out;
831 }
832
833 qid = pref_zq->queue->qid;
834 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
835
836 spin_lock(&zcrypt_list_lock);
837 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
838 spin_unlock(&zcrypt_list_lock);
839
840 out:
841 ap_release_apmsg(&ap_msg);
842 if (tr) {
843 tr->last_rc = rc;
844 tr->last_qid = qid;
845 }
846 trace_s390_zcrypt_rep(crt, func_code, rc,
847 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
848 ap_msg.psmid);
849 return rc;
850 }
851
_zcrypt_send_cprb(u32 xflags,struct ap_perms * perms,struct zcrypt_track * tr,struct ica_xcRB * xcrb)852 static long _zcrypt_send_cprb(u32 xflags, struct ap_perms *perms,
853 struct zcrypt_track *tr,
854 struct ica_xcRB *xcrb)
855 {
856 bool userspace = xflags & ZCRYPT_XFLAG_USERSPACE;
857 unsigned int card, domain, _dom, func_code = 0;
858 unsigned int wgt = 0, pref_wgt = 0;
859 struct zcrypt_queue *zq, *pref_zq;
860 struct zcrypt_card *zc, *pref_zc;
861 int cpen, qpen, qid = 0, rc;
862 struct ap_message ap_msg;
863 struct module *mod;
864
865 trace_s390_zcrypt_req(xcrb, TB_ZSECSENDCPRB);
866
867 xcrb->status = 0;
868
869 rc = ap_init_apmsg(&ap_msg, xflags & ZCRYPT_XFLAG_NOMEMALLOC ?
870 AP_MSG_FLAG_MEMPOOL : 0);
871 if (rc)
872 goto out;
873
874 rc = prep_cca_ap_msg(userspace, xcrb, &ap_msg, &func_code, &domain);
875 if (rc)
876 goto out;
877 print_hex_dump_debug("ccareq: ", DUMP_PREFIX_ADDRESS, 16, 1,
878 ap_msg.msg, ap_msg.len, false);
879
880 /* Check domain for either AUTOSEL_DOM or in range 0...AP_DOMAINS-1 */
881 if (domain != AUTOSEL_DOM && domain >= AP_DOMAINS) {
882 rc = -EINVAL;
883 goto out;
884 }
885 /* Spectre-v1: sanitize domain unconditionally for later use */
886 _dom = array_index_nospec(domain, AP_DOMAINS);
887
888 if (perms != &ap_perms && domain < AP_DOMAINS) {
889 if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
890 if (!test_bit_inv(_dom, perms->adm)) {
891 rc = -ENODEV;
892 goto out;
893 }
894 } else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
895 rc = -EOPNOTSUPP;
896 goto out;
897 }
898 }
899 /*
900 * If a valid target domain is set and this domain is NOT a usage
901 * domain but a control only domain, autoselect target domain.
902 */
903 if (domain < AP_DOMAINS) {
904 if (!ap_test_config_usage_domain(_dom) &&
905 ap_test_config_ctrl_domain(_dom))
906 domain = AUTOSEL_DOM;
907 }
908
909 pref_zc = NULL;
910 pref_zq = NULL;
911 card = xcrb->user_defined;
912 spin_lock(&zcrypt_list_lock);
913 for_each_zcrypt_card(zc) {
914 /* Check for usable CCA card */
915 if (!zc->online || !zc->card->config || zc->card->chkstop ||
916 !zc->card->hwinfo.cca)
917 continue;
918 /* Check for user selected CCA card */
919 if (card != AUTOSELECT && card != zc->card->id)
920 continue;
921 /* check if request size exceeds card max msg size */
922 if (ap_msg.len > zc->card->maxmsgsize)
923 continue;
924 /* check if device node has admission for this card */
925 if (!zcrypt_check_card(perms, zc->card->id))
926 continue;
927 /* get weight index of the card device */
928 wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
929 /* penalty if this msg was previously sent via this card */
930 cpen = (tr && tr->again_counter && tr->last_qid &&
931 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
932 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
933 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
934 continue;
935 for_each_zcrypt_queue(zq, zc) {
936 /* check for device usable and eligible */
937 if (!zq->online || !zq->ops->send_cprb ||
938 !ap_queue_usable(zq->queue) ||
939 (domain != AUTOSEL_DOM &&
940 domain != AP_QID_QUEUE(zq->queue->qid)))
941 continue;
942 /* check if device node has admission for this queue */
943 if (!zcrypt_check_queue(perms,
944 AP_QID_QUEUE(zq->queue->qid)))
945 continue;
946 /* penalty if the msg was previously sent at this qid */
947 qpen = (tr && tr->again_counter && tr->last_qid &&
948 tr->last_qid == zq->queue->qid) ?
949 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
950 if (!zcrypt_queue_compare(zq, pref_zq,
951 wgt + cpen + qpen, pref_wgt))
952 continue;
953 pref_zc = zc;
954 pref_zq = zq;
955 pref_wgt = wgt + cpen + qpen;
956 }
957 }
958 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
959 spin_unlock(&zcrypt_list_lock);
960
961 if (!pref_zq) {
962 pr_debug("no match for address %02x.%04x => ENODEV\n",
963 card, domain);
964 rc = -ENODEV;
965 goto out;
966 }
967
968 rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcrb, &ap_msg);
969 if (!rc) {
970 print_hex_dump_debug("ccarpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
971 ap_msg.msg, ap_msg.len, false);
972 }
973
974 spin_lock(&zcrypt_list_lock);
975 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
976 spin_unlock(&zcrypt_list_lock);
977
978 out:
979 ap_release_apmsg(&ap_msg);
980 if (tr) {
981 tr->last_rc = rc;
982 tr->last_qid = qid;
983 }
984 trace_s390_zcrypt_rep(xcrb, func_code, rc,
985 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
986 ap_msg.psmid);
987 return rc;
988 }
989
zcrypt_send_cprb(struct ica_xcRB * xcrb,u32 xflags)990 long zcrypt_send_cprb(struct ica_xcRB *xcrb, u32 xflags)
991 {
992 struct zcrypt_track tr;
993 int rc;
994
995 memset(&tr, 0, sizeof(tr));
996
997 do {
998 rc = _zcrypt_send_cprb(xflags, &ap_perms, &tr, xcrb);
999 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1000
1001 /* on ENODEV failure: retry once again after a requested rescan */
1002 if (rc == -ENODEV && zcrypt_process_rescan())
1003 do {
1004 rc = _zcrypt_send_cprb(xflags, &ap_perms, &tr, xcrb);
1005 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1006 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1007 rc = -EIO;
1008 if (rc)
1009 pr_debug("rc=%d\n", rc);
1010
1011 return rc;
1012 }
1013 EXPORT_SYMBOL(zcrypt_send_cprb);
1014
is_desired_ep11_card(unsigned int dev_id,unsigned short target_num,struct ep11_target_dev * targets)1015 static bool is_desired_ep11_card(unsigned int dev_id,
1016 unsigned short target_num,
1017 struct ep11_target_dev *targets)
1018 {
1019 while (target_num-- > 0) {
1020 if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
1021 return true;
1022 targets++;
1023 }
1024 return false;
1025 }
1026
is_desired_ep11_queue(unsigned int dev_qid,unsigned short target_num,struct ep11_target_dev * targets)1027 static bool is_desired_ep11_queue(unsigned int dev_qid,
1028 unsigned short target_num,
1029 struct ep11_target_dev *targets)
1030 {
1031 int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
1032
1033 while (target_num-- > 0) {
1034 if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
1035 (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
1036 return true;
1037 targets++;
1038 }
1039 return false;
1040 }
1041
_zcrypt_send_ep11_cprb(u32 xflags,struct ap_perms * perms,struct zcrypt_track * tr,struct ep11_urb * xcrb)1042 static long _zcrypt_send_ep11_cprb(u32 xflags, struct ap_perms *perms,
1043 struct zcrypt_track *tr,
1044 struct ep11_urb *xcrb)
1045 {
1046 bool userspace = xflags & ZCRYPT_XFLAG_USERSPACE;
1047 struct zcrypt_card *zc, *pref_zc;
1048 struct zcrypt_queue *zq, *pref_zq;
1049 struct ep11_target_dev *targets = NULL;
1050 unsigned short target_num;
1051 unsigned int wgt = 0, pref_wgt = 0;
1052 unsigned int func_code = 0, domain, _dom;
1053 struct ap_message ap_msg;
1054 int cpen, qpen, qid = 0, rc;
1055 struct module *mod;
1056
1057 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
1058
1059 rc = ap_init_apmsg(&ap_msg, xflags & ZCRYPT_XFLAG_NOMEMALLOC ?
1060 AP_MSG_FLAG_MEMPOOL : 0);
1061 if (rc)
1062 goto out;
1063
1064 target_num = (unsigned short)xcrb->targets_num;
1065
1066 /* empty list indicates autoselect (all available targets) */
1067 rc = -ENOMEM;
1068 if (target_num != 0) {
1069 if (userspace) {
1070 targets = kzalloc_objs(*targets, target_num);
1071 if (!targets)
1072 goto out;
1073 if (copy_from_user(targets, xcrb->targets,
1074 target_num * sizeof(*targets))) {
1075 rc = -EFAULT;
1076 goto out;
1077 }
1078 } else {
1079 targets = (struct ep11_target_dev __force __kernel *)xcrb->targets;
1080 }
1081 }
1082
1083 rc = prep_ep11_ap_msg(userspace, xcrb, &ap_msg, &func_code, &domain);
1084 if (rc)
1085 goto out;
1086 print_hex_dump_debug("ep11req: ", DUMP_PREFIX_ADDRESS, 16, 1,
1087 ap_msg.msg, ap_msg.len, false);
1088
1089 /* Check domain for either AUTOSEL_DOM or in range 0...AP_DOMAINS-1 */
1090 if (domain != AUTOSEL_DOM && domain >= AP_DOMAINS) {
1091 rc = -EINVAL;
1092 goto out;
1093 }
1094 /* Spectre-v1: sanitize domain unconditionally for later use */
1095 _dom = array_index_nospec(domain, AP_DOMAINS);
1096
1097 if (perms != &ap_perms && domain < AP_DOMAINS) {
1098 if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
1099 if (!test_bit_inv(_dom, perms->adm)) {
1100 rc = -ENODEV;
1101 goto out;
1102 }
1103 } else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
1104 rc = -EOPNOTSUPP;
1105 goto out;
1106 }
1107 }
1108
1109 pref_zc = NULL;
1110 pref_zq = NULL;
1111 spin_lock(&zcrypt_list_lock);
1112 for_each_zcrypt_card(zc) {
1113 /* Check for usable EP11 card */
1114 if (!zc->online || !zc->card->config || zc->card->chkstop ||
1115 !zc->card->hwinfo.ep11)
1116 continue;
1117 /* Check for user selected EP11 card */
1118 if (targets &&
1119 !is_desired_ep11_card(zc->card->id, target_num, targets))
1120 continue;
1121 /* check if request size exceeds card max msg size */
1122 if (ap_msg.len > zc->card->maxmsgsize)
1123 continue;
1124 /* check if device node has admission for this card */
1125 if (!zcrypt_check_card(perms, zc->card->id))
1126 continue;
1127 /* get weight index of the card device */
1128 wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1129 /* penalty if this msg was previously sent via this card */
1130 cpen = (tr && tr->again_counter && tr->last_qid &&
1131 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
1132 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
1133 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
1134 continue;
1135 for_each_zcrypt_queue(zq, zc) {
1136 /* check if device is usable and eligible */
1137 if (!zq->online || !zq->ops->send_ep11_cprb ||
1138 !ap_queue_usable(zq->queue) ||
1139 (targets &&
1140 !is_desired_ep11_queue(zq->queue->qid,
1141 target_num, targets)))
1142 continue;
1143 /* check if device node has admission for this queue */
1144 if (!zcrypt_check_queue(perms,
1145 AP_QID_QUEUE(zq->queue->qid)))
1146 continue;
1147 /* penalty if the msg was previously sent at this qid */
1148 qpen = (tr && tr->again_counter && tr->last_qid &&
1149 tr->last_qid == zq->queue->qid) ?
1150 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
1151 if (!zcrypt_queue_compare(zq, pref_zq,
1152 wgt + cpen + qpen, pref_wgt))
1153 continue;
1154 pref_zc = zc;
1155 pref_zq = zq;
1156 pref_wgt = wgt + cpen + qpen;
1157 }
1158 }
1159 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1160 spin_unlock(&zcrypt_list_lock);
1161
1162 if (!pref_zq) {
1163 if (targets && target_num == 1) {
1164 pr_debug("no match for address %02x.%04x => ENODEV\n",
1165 (int)targets->ap_id, (int)targets->dom_id);
1166 } else if (targets) {
1167 pr_debug("no match for %d target addrs => ENODEV\n",
1168 (int)target_num);
1169 } else {
1170 pr_debug("no match for address ff.ffff => ENODEV\n");
1171 }
1172 rc = -ENODEV;
1173 goto out;
1174 }
1175
1176 qid = pref_zq->queue->qid;
1177 rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
1178 if (!rc) {
1179 print_hex_dump_debug("ep11rpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
1180 ap_msg.msg, ap_msg.len, false);
1181 }
1182
1183 spin_lock(&zcrypt_list_lock);
1184 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1185 spin_unlock(&zcrypt_list_lock);
1186
1187 out:
1188 if (userspace)
1189 kfree(targets);
1190 ap_release_apmsg(&ap_msg);
1191 if (tr) {
1192 tr->last_rc = rc;
1193 tr->last_qid = qid;
1194 }
1195 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1196 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1197 ap_msg.psmid);
1198 return rc;
1199 }
1200
zcrypt_send_ep11_cprb(struct ep11_urb * xcrb,u32 xflags)1201 long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb, u32 xflags)
1202 {
1203 struct zcrypt_track tr;
1204 int rc;
1205
1206 memset(&tr, 0, sizeof(tr));
1207
1208 do {
1209 rc = _zcrypt_send_ep11_cprb(xflags, &ap_perms, &tr, xcrb);
1210 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1211
1212 /* on ENODEV failure: retry once again after a requested rescan */
1213 if (rc == -ENODEV && zcrypt_process_rescan())
1214 do {
1215 rc = _zcrypt_send_ep11_cprb(xflags, &ap_perms, &tr, xcrb);
1216 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1217 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1218 rc = -EIO;
1219 if (rc)
1220 pr_debug("rc=%d\n", rc);
1221
1222 return rc;
1223 }
1224 EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1225
zcrypt_rng(char * buffer)1226 static long zcrypt_rng(char *buffer)
1227 {
1228 struct zcrypt_card *zc, *pref_zc;
1229 struct zcrypt_queue *zq, *pref_zq;
1230 unsigned int wgt = 0, pref_wgt = 0;
1231 unsigned int func_code = 0;
1232 struct ap_message ap_msg;
1233 int qid = 0, rc = -ENODEV;
1234 struct module *mod;
1235
1236 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1237
1238 rc = ap_init_apmsg(&ap_msg, 0);
1239 if (rc)
1240 goto out;
1241 rc = prep_rng_ap_msg(&ap_msg, &func_code, NULL);
1242 if (rc)
1243 goto out;
1244
1245 pref_zc = NULL;
1246 pref_zq = NULL;
1247 spin_lock(&zcrypt_list_lock);
1248 for_each_zcrypt_card(zc) {
1249 /* Check for usable CCA card */
1250 if (!zc->online || !zc->card->config || zc->card->chkstop ||
1251 !zc->card->hwinfo.cca)
1252 continue;
1253 /* get weight index of the card device */
1254 wgt = zc->speed_rating[func_code];
1255 if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
1256 continue;
1257 for_each_zcrypt_queue(zq, zc) {
1258 /* check if device is usable and eligible */
1259 if (!zq->online || !zq->ops->rng ||
1260 !ap_queue_usable(zq->queue))
1261 continue;
1262 if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
1263 continue;
1264 pref_zc = zc;
1265 pref_zq = zq;
1266 pref_wgt = wgt;
1267 }
1268 }
1269 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1270 spin_unlock(&zcrypt_list_lock);
1271
1272 if (!pref_zq) {
1273 pr_debug("no matching queue found => ENODEV\n");
1274 rc = -ENODEV;
1275 goto out;
1276 }
1277
1278 qid = pref_zq->queue->qid;
1279 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1280
1281 spin_lock(&zcrypt_list_lock);
1282 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1283 spin_unlock(&zcrypt_list_lock);
1284
1285 out:
1286 ap_release_apmsg(&ap_msg);
1287 trace_s390_zcrypt_rep(buffer, func_code, rc,
1288 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1289 ap_msg.psmid);
1290 return rc;
1291 }
1292
zcrypt_device_status_mask(struct zcrypt_device_status * devstatus)1293 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1294 {
1295 struct zcrypt_card *zc;
1296 struct zcrypt_queue *zq;
1297 struct zcrypt_device_status *stat;
1298 int card, queue;
1299
1300 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1301 * sizeof(struct zcrypt_device_status));
1302
1303 spin_lock(&zcrypt_list_lock);
1304 for_each_zcrypt_card(zc) {
1305 for_each_zcrypt_queue(zq, zc) {
1306 card = AP_QID_CARD(zq->queue->qid);
1307 if (card >= MAX_ZDEV_CARDIDS)
1308 continue;
1309 queue = AP_QID_QUEUE(zq->queue->qid);
1310 stat = &devstatus[card * AP_DOMAINS + queue];
1311 stat->hwtype = zc->card->ap_dev.device_type;
1312 stat->functions = zc->card->hwinfo.fac >> 26;
1313 stat->qid = zq->queue->qid;
1314 stat->online = zq->online ? 0x01 : 0x00;
1315 }
1316 }
1317 spin_unlock(&zcrypt_list_lock);
1318 }
1319
zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext * devstatus,int maxcard,int maxqueue)1320 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus,
1321 int maxcard, int maxqueue)
1322 {
1323 struct zcrypt_card *zc;
1324 struct zcrypt_queue *zq;
1325 struct zcrypt_device_status_ext *stat;
1326 int card, queue;
1327
1328 maxcard = min_t(int, maxcard, MAX_ZDEV_CARDIDS_EXT);
1329 maxqueue = min_t(int, maxqueue, MAX_ZDEV_DOMAINS_EXT);
1330
1331 spin_lock(&zcrypt_list_lock);
1332 for_each_zcrypt_card(zc) {
1333 for_each_zcrypt_queue(zq, zc) {
1334 card = AP_QID_CARD(zq->queue->qid);
1335 queue = AP_QID_QUEUE(zq->queue->qid);
1336 if (card >= maxcard || queue >= maxqueue)
1337 continue;
1338 stat = &devstatus[card * maxqueue + queue];
1339 stat->hwtype = zc->card->ap_dev.device_type;
1340 stat->functions = zc->card->hwinfo.fac >> 26;
1341 stat->qid = zq->queue->qid;
1342 stat->online = zq->online ? 0x01 : 0x00;
1343 }
1344 }
1345 spin_unlock(&zcrypt_list_lock);
1346 }
1347 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1348
zcrypt_device_status_ext(int card,int queue,struct zcrypt_device_status_ext * devstat)1349 int zcrypt_device_status_ext(int card, int queue,
1350 struct zcrypt_device_status_ext *devstat)
1351 {
1352 struct zcrypt_card *zc;
1353 struct zcrypt_queue *zq;
1354
1355 memset(devstat, 0, sizeof(*devstat));
1356
1357 spin_lock(&zcrypt_list_lock);
1358 for_each_zcrypt_card(zc) {
1359 for_each_zcrypt_queue(zq, zc) {
1360 if (card == AP_QID_CARD(zq->queue->qid) &&
1361 queue == AP_QID_QUEUE(zq->queue->qid)) {
1362 devstat->hwtype = zc->card->ap_dev.device_type;
1363 devstat->functions = zc->card->hwinfo.fac >> 26;
1364 devstat->qid = zq->queue->qid;
1365 devstat->online = zq->online ? 0x01 : 0x00;
1366 spin_unlock(&zcrypt_list_lock);
1367 return 0;
1368 }
1369 }
1370 }
1371 spin_unlock(&zcrypt_list_lock);
1372
1373 return -ENODEV;
1374 }
1375 EXPORT_SYMBOL(zcrypt_device_status_ext);
1376
zcrypt_status_mask(char status[],size_t max_adapters)1377 static void zcrypt_status_mask(char status[], size_t max_adapters)
1378 {
1379 struct zcrypt_card *zc;
1380 struct zcrypt_queue *zq;
1381 int card;
1382
1383 memset(status, 0, max_adapters);
1384 spin_lock(&zcrypt_list_lock);
1385 for_each_zcrypt_card(zc) {
1386 for_each_zcrypt_queue(zq, zc) {
1387 card = AP_QID_CARD(zq->queue->qid);
1388 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1389 card >= max_adapters)
1390 continue;
1391 status[card] = zc->online ? zc->user_space_type : 0x0d;
1392 }
1393 }
1394 spin_unlock(&zcrypt_list_lock);
1395 }
1396
zcrypt_qdepth_mask(char qdepth[],size_t max_adapters)1397 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1398 {
1399 struct zcrypt_card *zc;
1400 struct zcrypt_queue *zq;
1401 int card;
1402
1403 memset(qdepth, 0, max_adapters);
1404 spin_lock(&zcrypt_list_lock);
1405 local_bh_disable();
1406 for_each_zcrypt_card(zc) {
1407 for_each_zcrypt_queue(zq, zc) {
1408 card = AP_QID_CARD(zq->queue->qid);
1409 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1410 card >= max_adapters)
1411 continue;
1412 spin_lock(&zq->queue->lock);
1413 qdepth[card] =
1414 zq->queue->pendingq_count +
1415 zq->queue->requestq_count;
1416 spin_unlock(&zq->queue->lock);
1417 }
1418 }
1419 local_bh_enable();
1420 spin_unlock(&zcrypt_list_lock);
1421 }
1422
zcrypt_perdev_reqcnt(u32 reqcnt[],size_t max_adapters)1423 static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1424 {
1425 struct zcrypt_card *zc;
1426 struct zcrypt_queue *zq;
1427 int card;
1428 u64 cnt;
1429
1430 memset(reqcnt, 0, sizeof(int) * max_adapters);
1431 spin_lock(&zcrypt_list_lock);
1432 local_bh_disable();
1433 for_each_zcrypt_card(zc) {
1434 for_each_zcrypt_queue(zq, zc) {
1435 card = AP_QID_CARD(zq->queue->qid);
1436 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1437 card >= max_adapters)
1438 continue;
1439 spin_lock(&zq->queue->lock);
1440 cnt = zq->queue->total_request_count;
1441 spin_unlock(&zq->queue->lock);
1442 reqcnt[card] = (cnt < UINT_MAX) ? (u32)cnt : UINT_MAX;
1443 }
1444 }
1445 local_bh_enable();
1446 spin_unlock(&zcrypt_list_lock);
1447 }
1448
zcrypt_pendingq_count(void)1449 static int zcrypt_pendingq_count(void)
1450 {
1451 struct zcrypt_card *zc;
1452 struct zcrypt_queue *zq;
1453 int pendingq_count;
1454
1455 pendingq_count = 0;
1456 spin_lock(&zcrypt_list_lock);
1457 local_bh_disable();
1458 for_each_zcrypt_card(zc) {
1459 for_each_zcrypt_queue(zq, zc) {
1460 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1461 continue;
1462 spin_lock(&zq->queue->lock);
1463 pendingq_count += zq->queue->pendingq_count;
1464 spin_unlock(&zq->queue->lock);
1465 }
1466 }
1467 local_bh_enable();
1468 spin_unlock(&zcrypt_list_lock);
1469 return pendingq_count;
1470 }
1471
zcrypt_requestq_count(void)1472 static int zcrypt_requestq_count(void)
1473 {
1474 struct zcrypt_card *zc;
1475 struct zcrypt_queue *zq;
1476 int requestq_count;
1477
1478 requestq_count = 0;
1479 spin_lock(&zcrypt_list_lock);
1480 local_bh_disable();
1481 for_each_zcrypt_card(zc) {
1482 for_each_zcrypt_queue(zq, zc) {
1483 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1484 continue;
1485 spin_lock(&zq->queue->lock);
1486 requestq_count += zq->queue->requestq_count;
1487 spin_unlock(&zq->queue->lock);
1488 }
1489 }
1490 local_bh_enable();
1491 spin_unlock(&zcrypt_list_lock);
1492 return requestq_count;
1493 }
1494
icarsamodexpo_ioctl(struct ap_perms * perms,unsigned long arg)1495 static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1496 {
1497 int rc;
1498 struct zcrypt_track tr;
1499 struct ica_rsa_modexpo mex;
1500 struct ica_rsa_modexpo __user *umex = (void __user *)arg;
1501
1502 memset(&tr, 0, sizeof(tr));
1503 if (copy_from_user(&mex, umex, sizeof(mex)))
1504 return -EFAULT;
1505
1506 do {
1507 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1508 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1509
1510 /* on ENODEV failure: retry once again after a requested rescan */
1511 if (rc == -ENODEV && zcrypt_process_rescan())
1512 do {
1513 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1514 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1515 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1516 rc = -EIO;
1517 if (rc) {
1518 pr_debug("ioctl ICARSAMODEXPO rc=%d\n", rc);
1519 return rc;
1520 }
1521 return put_user(mex.outputdatalength, &umex->outputdatalength);
1522 }
1523
icarsacrt_ioctl(struct ap_perms * perms,unsigned long arg)1524 static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1525 {
1526 int rc;
1527 struct zcrypt_track tr;
1528 struct ica_rsa_modexpo_crt crt;
1529 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *)arg;
1530
1531 memset(&tr, 0, sizeof(tr));
1532 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1533 return -EFAULT;
1534
1535 do {
1536 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1537 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1538
1539 /* on ENODEV failure: retry once again after a requested rescan */
1540 if (rc == -ENODEV && zcrypt_process_rescan())
1541 do {
1542 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1543 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1544 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1545 rc = -EIO;
1546 if (rc) {
1547 pr_debug("ioctl ICARSACRT rc=%d\n", rc);
1548 return rc;
1549 }
1550 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1551 }
1552
zsecsendcprb_ioctl(struct ap_perms * perms,unsigned long arg)1553 static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1554 {
1555 int rc;
1556 struct ica_xcRB xcrb;
1557 struct zcrypt_track tr;
1558 u32 xflags = ZCRYPT_XFLAG_USERSPACE;
1559 struct ica_xcRB __user *uxcrb = (void __user *)arg;
1560
1561 memset(&tr, 0, sizeof(tr));
1562 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1563 return -EFAULT;
1564
1565 do {
1566 rc = _zcrypt_send_cprb(xflags, perms, &tr, &xcrb);
1567 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1568
1569 /* on ENODEV failure: retry once again after a requested rescan */
1570 if (rc == -ENODEV && zcrypt_process_rescan())
1571 do {
1572 rc = _zcrypt_send_cprb(xflags, perms, &tr, &xcrb);
1573 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1574 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1575 rc = -EIO;
1576 if (rc)
1577 pr_debug("ioctl ZSENDCPRB rc=%d status=0x%x\n",
1578 rc, xcrb.status);
1579 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1580 return -EFAULT;
1581 return rc;
1582 }
1583
zsendep11cprb_ioctl(struct ap_perms * perms,unsigned long arg)1584 static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1585 {
1586 int rc;
1587 struct ep11_urb xcrb;
1588 struct zcrypt_track tr;
1589 u32 xflags = ZCRYPT_XFLAG_USERSPACE;
1590 struct ep11_urb __user *uxcrb = (void __user *)arg;
1591
1592 memset(&tr, 0, sizeof(tr));
1593 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1594 return -EFAULT;
1595
1596 do {
1597 rc = _zcrypt_send_ep11_cprb(xflags, perms, &tr, &xcrb);
1598 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1599
1600 /* on ENODEV failure: retry once again after a requested rescan */
1601 if (rc == -ENODEV && zcrypt_process_rescan())
1602 do {
1603 rc = _zcrypt_send_ep11_cprb(xflags, perms, &tr, &xcrb);
1604 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1605 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1606 rc = -EIO;
1607 if (rc)
1608 pr_debug("ioctl ZSENDEP11CPRB rc=%d\n", rc);
1609 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1610 return -EFAULT;
1611 return rc;
1612 }
1613
zcrypt_unlocked_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1614 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1615 unsigned long arg)
1616 {
1617 int rc;
1618 struct ap_perms *perms =
1619 (struct ap_perms *)filp->private_data;
1620
1621 rc = zcrypt_check_ioctl(perms, cmd);
1622 if (rc)
1623 return rc;
1624
1625 switch (cmd) {
1626 case ICARSAMODEXPO:
1627 return icarsamodexpo_ioctl(perms, arg);
1628 case ICARSACRT:
1629 return icarsacrt_ioctl(perms, arg);
1630 case ZSECSENDCPRB:
1631 return zsecsendcprb_ioctl(perms, arg);
1632 case ZSENDEP11CPRB:
1633 return zsendep11cprb_ioctl(perms, arg);
1634 case ZCRYPT_DEVICE_STATUS: {
1635 struct zcrypt_device_status_ext *device_status;
1636 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1637 * sizeof(struct zcrypt_device_status_ext);
1638
1639 device_status = kvzalloc_objs(struct zcrypt_device_status_ext,
1640 MAX_ZDEV_ENTRIES_EXT);
1641 if (!device_status)
1642 return -ENOMEM;
1643 zcrypt_device_status_mask_ext(device_status,
1644 MAX_ZDEV_CARDIDS_EXT,
1645 MAX_ZDEV_DOMAINS_EXT);
1646 if (copy_to_user((char __user *)arg, device_status,
1647 total_size))
1648 rc = -EFAULT;
1649 kvfree(device_status);
1650 return rc;
1651 }
1652 case ZCRYPT_STATUS_MASK: {
1653 char status[AP_DEVICES];
1654
1655 zcrypt_status_mask(status, AP_DEVICES);
1656 if (copy_to_user((char __user *)arg, status, sizeof(status)))
1657 return -EFAULT;
1658 return 0;
1659 }
1660 case ZCRYPT_QDEPTH_MASK: {
1661 char qdepth[AP_DEVICES];
1662
1663 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1664 if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
1665 return -EFAULT;
1666 return 0;
1667 }
1668 case ZCRYPT_PERDEV_REQCNT: {
1669 u32 *reqcnt;
1670
1671 reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1672 if (!reqcnt)
1673 return -ENOMEM;
1674 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1675 if (copy_to_user((int __user *)arg, reqcnt,
1676 sizeof(u32) * AP_DEVICES))
1677 rc = -EFAULT;
1678 kfree(reqcnt);
1679 return rc;
1680 }
1681 case Z90STAT_REQUESTQ_COUNT:
1682 return put_user(zcrypt_requestq_count(), (int __user *)arg);
1683 case Z90STAT_PENDINGQ_COUNT:
1684 return put_user(zcrypt_pendingq_count(), (int __user *)arg);
1685 case Z90STAT_TOTALOPEN_COUNT:
1686 return put_user(atomic_read(&zcrypt_open_count),
1687 (int __user *)arg);
1688 case Z90STAT_DOMAIN_INDEX:
1689 return put_user(ap_domain_index, (int __user *)arg);
1690 /*
1691 * Deprecated ioctls
1692 */
1693 case ZDEVICESTATUS: {
1694 /* the old ioctl supports only 64 adapters */
1695 struct zcrypt_device_status *device_status;
1696 size_t total_size = MAX_ZDEV_ENTRIES
1697 * sizeof(struct zcrypt_device_status);
1698
1699 device_status = kzalloc(total_size, GFP_KERNEL);
1700 if (!device_status)
1701 return -ENOMEM;
1702 zcrypt_device_status_mask(device_status);
1703 if (copy_to_user((char __user *)arg, device_status,
1704 total_size))
1705 rc = -EFAULT;
1706 kfree(device_status);
1707 return rc;
1708 }
1709 case Z90STAT_STATUS_MASK: {
1710 /* the old ioctl supports only 64 adapters */
1711 char status[MAX_ZDEV_CARDIDS];
1712
1713 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1714 if (copy_to_user((char __user *)arg, status, sizeof(status)))
1715 return -EFAULT;
1716 return 0;
1717 }
1718 case Z90STAT_QDEPTH_MASK: {
1719 /* the old ioctl supports only 64 adapters */
1720 char qdepth[MAX_ZDEV_CARDIDS];
1721
1722 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1723 if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
1724 return -EFAULT;
1725 return 0;
1726 }
1727 case Z90STAT_PERDEV_REQCNT: {
1728 /* the old ioctl supports only 64 adapters */
1729 u32 reqcnt[MAX_ZDEV_CARDIDS];
1730
1731 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1732 if (copy_to_user((int __user *)arg, reqcnt, sizeof(reqcnt)))
1733 return -EFAULT;
1734 return 0;
1735 }
1736 /* unknown ioctl number */
1737 default:
1738 pr_debug("unknown ioctl 0x%08x\n", cmd);
1739 return -ENOIOCTLCMD;
1740 }
1741 }
1742
1743 /*
1744 * Misc device file operations.
1745 */
1746 static const struct file_operations zcrypt_fops = {
1747 .owner = THIS_MODULE,
1748 .read = zcrypt_read,
1749 .write = zcrypt_write,
1750 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1751 .open = zcrypt_open,
1752 .release = zcrypt_release,
1753 };
1754
1755 /*
1756 * Misc device.
1757 */
1758 static struct miscdevice zcrypt_misc_device = {
1759 .minor = MISC_DYNAMIC_MINOR,
1760 .name = "z90crypt",
1761 .fops = &zcrypt_fops,
1762 };
1763
1764 static int zcrypt_rng_device_count;
1765 static u32 *zcrypt_rng_buffer;
1766 static int zcrypt_rng_buffer_index;
1767 static DEFINE_MUTEX(zcrypt_rng_mutex);
1768
zcrypt_rng_data_read(struct hwrng * rng,u32 * data)1769 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1770 {
1771 int rc;
1772
1773 /*
1774 * We don't need locking here because the RNG API guarantees serialized
1775 * read method calls.
1776 */
1777 if (zcrypt_rng_buffer_index == 0) {
1778 rc = zcrypt_rng((char *)zcrypt_rng_buffer);
1779 /* on ENODEV failure: retry once again after an AP bus rescan */
1780 if (rc == -ENODEV && zcrypt_process_rescan())
1781 rc = zcrypt_rng((char *)zcrypt_rng_buffer);
1782 if (rc < 0)
1783 return -EIO;
1784 zcrypt_rng_buffer_index = rc / sizeof(*data);
1785 }
1786 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1787 return sizeof(*data);
1788 }
1789
1790 static struct hwrng zcrypt_rng_dev = {
1791 .name = "zcrypt",
1792 .data_read = zcrypt_rng_data_read,
1793 .quality = 990,
1794 };
1795
zcrypt_rng_device_add(void)1796 int zcrypt_rng_device_add(void)
1797 {
1798 int rc = 0;
1799
1800 mutex_lock(&zcrypt_rng_mutex);
1801 if (zcrypt_rng_device_count == 0) {
1802 zcrypt_rng_buffer = kzalloc(PAGE_SIZE, GFP_KERNEL);
1803 if (!zcrypt_rng_buffer) {
1804 rc = -ENOMEM;
1805 goto out;
1806 }
1807 zcrypt_rng_buffer_index = 0;
1808 rc = hwrng_register(&zcrypt_rng_dev);
1809 if (rc)
1810 goto out_free;
1811 zcrypt_rng_device_count = 1;
1812 } else {
1813 zcrypt_rng_device_count++;
1814 }
1815 mutex_unlock(&zcrypt_rng_mutex);
1816 return 0;
1817
1818 out_free:
1819 kfree(zcrypt_rng_buffer);
1820 out:
1821 mutex_unlock(&zcrypt_rng_mutex);
1822 return rc;
1823 }
1824
zcrypt_rng_device_remove(void)1825 void zcrypt_rng_device_remove(void)
1826 {
1827 mutex_lock(&zcrypt_rng_mutex);
1828 zcrypt_rng_device_count--;
1829 if (zcrypt_rng_device_count == 0) {
1830 hwrng_unregister(&zcrypt_rng_dev);
1831 kfree(zcrypt_rng_buffer);
1832 }
1833 mutex_unlock(&zcrypt_rng_mutex);
1834 }
1835
1836 /*
1837 * Wait until the zcrypt api is operational.
1838 * The AP bus scan and the binding of ap devices to device drivers is
1839 * an asynchronous job. This function waits until these initial jobs
1840 * are done and so the zcrypt api should be ready to serve crypto
1841 * requests - if there are resources available. The function uses an
1842 * internal timeout of 30s. The very first caller will either wait for
1843 * ap bus bindings complete or the timeout happens. This state will be
1844 * remembered for further callers which will only be blocked until a
1845 * decision is made (timeout or bindings complete).
1846 * On timeout -ETIME is returned, on success the return value is 0.
1847 */
zcrypt_wait_api_operational(void)1848 int zcrypt_wait_api_operational(void)
1849 {
1850 static DEFINE_MUTEX(zcrypt_wait_api_lock);
1851 static int zcrypt_wait_api_state;
1852 int rc;
1853
1854 rc = mutex_lock_interruptible(&zcrypt_wait_api_lock);
1855 if (rc)
1856 return rc;
1857
1858 switch (zcrypt_wait_api_state) {
1859 case 0:
1860 /* initial state, invoke wait for the ap bus complete */
1861 rc = ap_wait_apqn_bindings_complete(
1862 msecs_to_jiffies(ZCRYPT_WAIT_BINDINGS_COMPLETE_MS));
1863 switch (rc) {
1864 case 0:
1865 /* ap bus bindings are complete */
1866 zcrypt_wait_api_state = 1;
1867 break;
1868 case -EINTR:
1869 /* interrupted, go back to caller */
1870 break;
1871 case -ETIME:
1872 /* timeout */
1873 ZCRYPT_DBF_WARN("%s ap_wait_init_apqn_bindings_complete()=ETIME\n",
1874 __func__);
1875 zcrypt_wait_api_state = -ETIME;
1876 break;
1877 default:
1878 /* other failure */
1879 pr_debug("ap_wait_init_apqn_bindings_complete()=%d\n", rc);
1880 break;
1881 }
1882 break;
1883 case 1:
1884 /* a previous caller already found ap bus bindings complete */
1885 rc = 0;
1886 break;
1887 default:
1888 /* a previous caller had timeout or other failure */
1889 rc = zcrypt_wait_api_state;
1890 break;
1891 }
1892
1893 mutex_unlock(&zcrypt_wait_api_lock);
1894
1895 return rc;
1896 }
1897 EXPORT_SYMBOL(zcrypt_wait_api_operational);
1898
zcrypt_debug_init(void)1899 int __init zcrypt_debug_init(void)
1900 {
1901 zcrypt_dbf_info = debug_register("zcrypt", 2, 1,
1902 ZCRYPT_DBF_MAX_SPRINTF_ARGS * sizeof(long));
1903 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1904 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1905
1906 return 0;
1907 }
1908
zcrypt_debug_exit(void)1909 void zcrypt_debug_exit(void)
1910 {
1911 debug_unregister(zcrypt_dbf_info);
1912 }
1913
zcdn_init(void)1914 static int __init zcdn_init(void)
1915 {
1916 int rc;
1917
1918 /* create a new class 'zcrypt' */
1919 rc = class_register(&zcrypt_class);
1920 if (rc)
1921 goto out_class_register_failed;
1922
1923 /* alloc device minor range */
1924 rc = alloc_chrdev_region(&zcrypt_devt,
1925 0, ZCRYPT_MAX_MINOR_NODES,
1926 ZCRYPT_NAME);
1927 if (rc)
1928 goto out_alloc_chrdev_failed;
1929
1930 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1931 zcrypt_cdev.owner = THIS_MODULE;
1932 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1933 if (rc)
1934 goto out_cdev_add_failed;
1935
1936 /* need some class specific sysfs attributes */
1937 rc = class_create_file(&zcrypt_class, &class_attr_zcdn_create);
1938 if (rc)
1939 goto out_class_create_file_1_failed;
1940 rc = class_create_file(&zcrypt_class, &class_attr_zcdn_destroy);
1941 if (rc)
1942 goto out_class_create_file_2_failed;
1943
1944 return 0;
1945
1946 out_class_create_file_2_failed:
1947 class_remove_file(&zcrypt_class, &class_attr_zcdn_create);
1948 out_class_create_file_1_failed:
1949 cdev_del(&zcrypt_cdev);
1950 out_cdev_add_failed:
1951 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1952 out_alloc_chrdev_failed:
1953 class_unregister(&zcrypt_class);
1954 out_class_register_failed:
1955 return rc;
1956 }
1957
zcdn_exit(void)1958 static void zcdn_exit(void)
1959 {
1960 class_remove_file(&zcrypt_class, &class_attr_zcdn_create);
1961 class_remove_file(&zcrypt_class, &class_attr_zcdn_destroy);
1962 zcdn_destroy_all();
1963 cdev_del(&zcrypt_cdev);
1964 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1965 class_unregister(&zcrypt_class);
1966 }
1967
1968 /*
1969 * zcrypt_api_init(): Module initialization.
1970 *
1971 * The module initialization code.
1972 */
zcrypt_api_init(void)1973 int __init zcrypt_api_init(void)
1974 {
1975 int rc;
1976
1977 /* make sure the mempool threshold is >= 1 */
1978 if (zcrypt_mempool_threshold < 1) {
1979 rc = -EINVAL;
1980 goto out;
1981 }
1982
1983 rc = zcrypt_debug_init();
1984 if (rc)
1985 goto out;
1986
1987 rc = zcdn_init();
1988 if (rc)
1989 goto out_zcdn_init_failed;
1990
1991 rc = zcrypt_ccamisc_init();
1992 if (rc)
1993 goto out_ccamisc_init_failed;
1994
1995 rc = zcrypt_ep11misc_init();
1996 if (rc)
1997 goto out_ep11misc_init_failed;
1998
1999 /* Register the request sprayer. */
2000 rc = misc_register(&zcrypt_misc_device);
2001 if (rc < 0)
2002 goto out_misc_register_failed;
2003
2004 zcrypt_msgtype6_init();
2005 zcrypt_msgtype50_init();
2006
2007 return 0;
2008
2009 out_misc_register_failed:
2010 zcrypt_ep11misc_exit();
2011 out_ep11misc_init_failed:
2012 zcrypt_ccamisc_exit();
2013 out_ccamisc_init_failed:
2014 zcdn_exit();
2015 out_zcdn_init_failed:
2016 zcrypt_debug_exit();
2017 out:
2018 return rc;
2019 }
2020
2021 /*
2022 * zcrypt_api_exit(): Module termination.
2023 *
2024 * The module termination code.
2025 */
zcrypt_api_exit(void)2026 void __exit zcrypt_api_exit(void)
2027 {
2028 zcdn_exit();
2029 misc_deregister(&zcrypt_misc_device);
2030 zcrypt_msgtype6_exit();
2031 zcrypt_msgtype50_exit();
2032 zcrypt_ccamisc_exit();
2033 zcrypt_ep11misc_exit();
2034 zcrypt_debug_exit();
2035 }
2036
2037 module_init(zcrypt_api_init);
2038 module_exit(zcrypt_api_exit);
2039