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, 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 if (perms != &ap_perms && domain < AP_DOMAINS) {
881 if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
882 domain = array_index_nospec(domain, AP_DOMAINS);
883 if (!test_bit_inv(domain, perms->adm)) {
884 rc = -ENODEV;
885 goto out;
886 }
887 } else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
888 rc = -EOPNOTSUPP;
889 goto out;
890 }
891 }
892 /*
893 * If a valid target domain is set and this domain is NOT a usage
894 * domain but a control only domain, autoselect target domain.
895 */
896 if (domain < AP_DOMAINS &&
897 !ap_test_config_usage_domain(domain) &&
898 ap_test_config_ctrl_domain(domain))
899 domain = AUTOSEL_DOM;
900
901 pref_zc = NULL;
902 pref_zq = NULL;
903 card = xcrb->user_defined;
904 spin_lock(&zcrypt_list_lock);
905 for_each_zcrypt_card(zc) {
906 /* Check for usable CCA card */
907 if (!zc->online || !zc->card->config || zc->card->chkstop ||
908 !zc->card->hwinfo.cca)
909 continue;
910 /* Check for user selected CCA card */
911 if (card != AUTOSELECT && card != zc->card->id)
912 continue;
913 /* check if request size exceeds card max msg size */
914 if (ap_msg.len > zc->card->maxmsgsize)
915 continue;
916 /* check if device node has admission for this card */
917 if (!zcrypt_check_card(perms, zc->card->id))
918 continue;
919 /* get weight index of the card device */
920 wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
921 /* penalty if this msg was previously sent via this card */
922 cpen = (tr && tr->again_counter && tr->last_qid &&
923 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
924 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
925 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
926 continue;
927 for_each_zcrypt_queue(zq, zc) {
928 /* check for device usable and eligible */
929 if (!zq->online || !zq->ops->send_cprb ||
930 !ap_queue_usable(zq->queue) ||
931 (domain != AUTOSEL_DOM &&
932 domain != AP_QID_QUEUE(zq->queue->qid)))
933 continue;
934 /* check if device node has admission for this queue */
935 if (!zcrypt_check_queue(perms,
936 AP_QID_QUEUE(zq->queue->qid)))
937 continue;
938 /* penalty if the msg was previously sent at this qid */
939 qpen = (tr && tr->again_counter && tr->last_qid &&
940 tr->last_qid == zq->queue->qid) ?
941 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
942 if (!zcrypt_queue_compare(zq, pref_zq,
943 wgt + cpen + qpen, pref_wgt))
944 continue;
945 pref_zc = zc;
946 pref_zq = zq;
947 pref_wgt = wgt + cpen + qpen;
948 }
949 }
950 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
951 spin_unlock(&zcrypt_list_lock);
952
953 if (!pref_zq) {
954 pr_debug("no match for address %02x.%04x => ENODEV\n",
955 card, domain);
956 rc = -ENODEV;
957 goto out;
958 }
959
960 rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcrb, &ap_msg);
961 if (!rc) {
962 print_hex_dump_debug("ccarpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
963 ap_msg.msg, ap_msg.len, false);
964 }
965
966 spin_lock(&zcrypt_list_lock);
967 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
968 spin_unlock(&zcrypt_list_lock);
969
970 out:
971 ap_release_apmsg(&ap_msg);
972 if (tr) {
973 tr->last_rc = rc;
974 tr->last_qid = qid;
975 }
976 trace_s390_zcrypt_rep(xcrb, func_code, rc,
977 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
978 ap_msg.psmid);
979 return rc;
980 }
981
zcrypt_send_cprb(struct ica_xcRB * xcrb,u32 xflags)982 long zcrypt_send_cprb(struct ica_xcRB *xcrb, u32 xflags)
983 {
984 struct zcrypt_track tr;
985 int rc;
986
987 memset(&tr, 0, sizeof(tr));
988
989 do {
990 rc = _zcrypt_send_cprb(xflags, &ap_perms, &tr, xcrb);
991 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
992
993 /* on ENODEV failure: retry once again after a requested rescan */
994 if (rc == -ENODEV && zcrypt_process_rescan())
995 do {
996 rc = _zcrypt_send_cprb(xflags, &ap_perms, &tr, xcrb);
997 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
998 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
999 rc = -EIO;
1000 if (rc)
1001 pr_debug("rc=%d\n", rc);
1002
1003 return rc;
1004 }
1005 EXPORT_SYMBOL(zcrypt_send_cprb);
1006
is_desired_ep11_card(unsigned int dev_id,unsigned short target_num,struct ep11_target_dev * targets)1007 static bool is_desired_ep11_card(unsigned int dev_id,
1008 unsigned short target_num,
1009 struct ep11_target_dev *targets)
1010 {
1011 while (target_num-- > 0) {
1012 if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
1013 return true;
1014 targets++;
1015 }
1016 return false;
1017 }
1018
is_desired_ep11_queue(unsigned int dev_qid,unsigned short target_num,struct ep11_target_dev * targets)1019 static bool is_desired_ep11_queue(unsigned int dev_qid,
1020 unsigned short target_num,
1021 struct ep11_target_dev *targets)
1022 {
1023 int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
1024
1025 while (target_num-- > 0) {
1026 if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
1027 (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
1028 return true;
1029 targets++;
1030 }
1031 return false;
1032 }
1033
_zcrypt_send_ep11_cprb(u32 xflags,struct ap_perms * perms,struct zcrypt_track * tr,struct ep11_urb * xcrb)1034 static long _zcrypt_send_ep11_cprb(u32 xflags, struct ap_perms *perms,
1035 struct zcrypt_track *tr,
1036 struct ep11_urb *xcrb)
1037 {
1038 bool userspace = xflags & ZCRYPT_XFLAG_USERSPACE;
1039 struct zcrypt_card *zc, *pref_zc;
1040 struct zcrypt_queue *zq, *pref_zq;
1041 struct ep11_target_dev *targets = NULL;
1042 unsigned short target_num;
1043 unsigned int wgt = 0, pref_wgt = 0;
1044 unsigned int func_code = 0, domain;
1045 struct ap_message ap_msg;
1046 int cpen, qpen, qid = 0, rc;
1047 struct module *mod;
1048
1049 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
1050
1051 rc = ap_init_apmsg(&ap_msg, xflags & ZCRYPT_XFLAG_NOMEMALLOC ?
1052 AP_MSG_FLAG_MEMPOOL : 0);
1053 if (rc)
1054 goto out;
1055
1056 target_num = (unsigned short)xcrb->targets_num;
1057
1058 /* empty list indicates autoselect (all available targets) */
1059 rc = -ENOMEM;
1060 if (target_num != 0) {
1061 if (userspace) {
1062 targets = kzalloc_objs(*targets, target_num);
1063 if (!targets)
1064 goto out;
1065 if (copy_from_user(targets, xcrb->targets,
1066 target_num * sizeof(*targets))) {
1067 rc = -EFAULT;
1068 goto out;
1069 }
1070 } else {
1071 targets = (struct ep11_target_dev __force __kernel *)xcrb->targets;
1072 }
1073 }
1074
1075 rc = prep_ep11_ap_msg(userspace, xcrb, &ap_msg, &func_code, &domain);
1076 if (rc)
1077 goto out;
1078 print_hex_dump_debug("ep11req: ", DUMP_PREFIX_ADDRESS, 16, 1,
1079 ap_msg.msg, ap_msg.len, false);
1080
1081 if (perms != &ap_perms && domain < AP_DOMAINS) {
1082 if (ap_msg.flags & AP_MSG_FLAG_ADMIN) {
1083 domain = array_index_nospec(domain, AP_DOMAINS);
1084 if (!test_bit_inv(domain, perms->adm)) {
1085 rc = -ENODEV;
1086 goto out;
1087 }
1088 } else if ((ap_msg.flags & AP_MSG_FLAG_USAGE) == 0) {
1089 rc = -EOPNOTSUPP;
1090 goto out;
1091 }
1092 }
1093
1094 pref_zc = NULL;
1095 pref_zq = NULL;
1096 spin_lock(&zcrypt_list_lock);
1097 for_each_zcrypt_card(zc) {
1098 /* Check for usable EP11 card */
1099 if (!zc->online || !zc->card->config || zc->card->chkstop ||
1100 !zc->card->hwinfo.ep11)
1101 continue;
1102 /* Check for user selected EP11 card */
1103 if (targets &&
1104 !is_desired_ep11_card(zc->card->id, target_num, targets))
1105 continue;
1106 /* check if request size exceeds card max msg size */
1107 if (ap_msg.len > zc->card->maxmsgsize)
1108 continue;
1109 /* check if device node has admission for this card */
1110 if (!zcrypt_check_card(perms, zc->card->id))
1111 continue;
1112 /* get weight index of the card device */
1113 wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1114 /* penalty if this msg was previously sent via this card */
1115 cpen = (tr && tr->again_counter && tr->last_qid &&
1116 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
1117 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
1118 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
1119 continue;
1120 for_each_zcrypt_queue(zq, zc) {
1121 /* check if device is usable and eligible */
1122 if (!zq->online || !zq->ops->send_ep11_cprb ||
1123 !ap_queue_usable(zq->queue) ||
1124 (targets &&
1125 !is_desired_ep11_queue(zq->queue->qid,
1126 target_num, targets)))
1127 continue;
1128 /* check if device node has admission for this queue */
1129 if (!zcrypt_check_queue(perms,
1130 AP_QID_QUEUE(zq->queue->qid)))
1131 continue;
1132 /* penalty if the msg was previously sent at this qid */
1133 qpen = (tr && tr->again_counter && tr->last_qid &&
1134 tr->last_qid == zq->queue->qid) ?
1135 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
1136 if (!zcrypt_queue_compare(zq, pref_zq,
1137 wgt + cpen + qpen, pref_wgt))
1138 continue;
1139 pref_zc = zc;
1140 pref_zq = zq;
1141 pref_wgt = wgt + cpen + qpen;
1142 }
1143 }
1144 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1145 spin_unlock(&zcrypt_list_lock);
1146
1147 if (!pref_zq) {
1148 if (targets && target_num == 1) {
1149 pr_debug("no match for address %02x.%04x => ENODEV\n",
1150 (int)targets->ap_id, (int)targets->dom_id);
1151 } else if (targets) {
1152 pr_debug("no match for %d target addrs => ENODEV\n",
1153 (int)target_num);
1154 } else {
1155 pr_debug("no match for address ff.ffff => ENODEV\n");
1156 }
1157 rc = -ENODEV;
1158 goto out;
1159 }
1160
1161 qid = pref_zq->queue->qid;
1162 rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
1163 if (!rc) {
1164 print_hex_dump_debug("ep11rpl: ", DUMP_PREFIX_ADDRESS, 16, 1,
1165 ap_msg.msg, ap_msg.len, false);
1166 }
1167
1168 spin_lock(&zcrypt_list_lock);
1169 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1170 spin_unlock(&zcrypt_list_lock);
1171
1172 out:
1173 if (userspace)
1174 kfree(targets);
1175 ap_release_apmsg(&ap_msg);
1176 if (tr) {
1177 tr->last_rc = rc;
1178 tr->last_qid = qid;
1179 }
1180 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1181 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1182 ap_msg.psmid);
1183 return rc;
1184 }
1185
zcrypt_send_ep11_cprb(struct ep11_urb * xcrb,u32 xflags)1186 long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb, u32 xflags)
1187 {
1188 struct zcrypt_track tr;
1189 int rc;
1190
1191 memset(&tr, 0, sizeof(tr));
1192
1193 do {
1194 rc = _zcrypt_send_ep11_cprb(xflags, &ap_perms, &tr, xcrb);
1195 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1196
1197 /* on ENODEV failure: retry once again after a requested rescan */
1198 if (rc == -ENODEV && zcrypt_process_rescan())
1199 do {
1200 rc = _zcrypt_send_ep11_cprb(xflags, &ap_perms, &tr, xcrb);
1201 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1202 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1203 rc = -EIO;
1204 if (rc)
1205 pr_debug("rc=%d\n", rc);
1206
1207 return rc;
1208 }
1209 EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1210
zcrypt_rng(char * buffer)1211 static long zcrypt_rng(char *buffer)
1212 {
1213 struct zcrypt_card *zc, *pref_zc;
1214 struct zcrypt_queue *zq, *pref_zq;
1215 unsigned int wgt = 0, pref_wgt = 0;
1216 unsigned int func_code = 0;
1217 struct ap_message ap_msg;
1218 int qid = 0, rc = -ENODEV;
1219 struct module *mod;
1220
1221 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1222
1223 rc = ap_init_apmsg(&ap_msg, 0);
1224 if (rc)
1225 goto out;
1226 rc = prep_rng_ap_msg(&ap_msg, &func_code, NULL);
1227 if (rc)
1228 goto out;
1229
1230 pref_zc = NULL;
1231 pref_zq = NULL;
1232 spin_lock(&zcrypt_list_lock);
1233 for_each_zcrypt_card(zc) {
1234 /* Check for usable CCA card */
1235 if (!zc->online || !zc->card->config || zc->card->chkstop ||
1236 !zc->card->hwinfo.cca)
1237 continue;
1238 /* get weight index of the card device */
1239 wgt = zc->speed_rating[func_code];
1240 if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
1241 continue;
1242 for_each_zcrypt_queue(zq, zc) {
1243 /* check if device is usable and eligible */
1244 if (!zq->online || !zq->ops->rng ||
1245 !ap_queue_usable(zq->queue))
1246 continue;
1247 if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
1248 continue;
1249 pref_zc = zc;
1250 pref_zq = zq;
1251 pref_wgt = wgt;
1252 }
1253 }
1254 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1255 spin_unlock(&zcrypt_list_lock);
1256
1257 if (!pref_zq) {
1258 pr_debug("no matching queue found => ENODEV\n");
1259 rc = -ENODEV;
1260 goto out;
1261 }
1262
1263 qid = pref_zq->queue->qid;
1264 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1265
1266 spin_lock(&zcrypt_list_lock);
1267 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1268 spin_unlock(&zcrypt_list_lock);
1269
1270 out:
1271 ap_release_apmsg(&ap_msg);
1272 trace_s390_zcrypt_rep(buffer, func_code, rc,
1273 AP_QID_CARD(qid), AP_QID_QUEUE(qid),
1274 ap_msg.psmid);
1275 return rc;
1276 }
1277
zcrypt_device_status_mask(struct zcrypt_device_status * devstatus)1278 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1279 {
1280 struct zcrypt_card *zc;
1281 struct zcrypt_queue *zq;
1282 struct zcrypt_device_status *stat;
1283 int card, queue;
1284
1285 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1286 * sizeof(struct zcrypt_device_status));
1287
1288 spin_lock(&zcrypt_list_lock);
1289 for_each_zcrypt_card(zc) {
1290 for_each_zcrypt_queue(zq, zc) {
1291 card = AP_QID_CARD(zq->queue->qid);
1292 if (card >= MAX_ZDEV_CARDIDS)
1293 continue;
1294 queue = AP_QID_QUEUE(zq->queue->qid);
1295 stat = &devstatus[card * AP_DOMAINS + queue];
1296 stat->hwtype = zc->card->ap_dev.device_type;
1297 stat->functions = zc->card->hwinfo.fac >> 26;
1298 stat->qid = zq->queue->qid;
1299 stat->online = zq->online ? 0x01 : 0x00;
1300 }
1301 }
1302 spin_unlock(&zcrypt_list_lock);
1303 }
1304
zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext * devstatus,int maxcard,int maxqueue)1305 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus,
1306 int maxcard, int maxqueue)
1307 {
1308 struct zcrypt_card *zc;
1309 struct zcrypt_queue *zq;
1310 struct zcrypt_device_status_ext *stat;
1311 int card, queue;
1312
1313 maxcard = min_t(int, maxcard, MAX_ZDEV_CARDIDS_EXT);
1314 maxqueue = min_t(int, maxqueue, MAX_ZDEV_DOMAINS_EXT);
1315
1316 spin_lock(&zcrypt_list_lock);
1317 for_each_zcrypt_card(zc) {
1318 for_each_zcrypt_queue(zq, zc) {
1319 card = AP_QID_CARD(zq->queue->qid);
1320 queue = AP_QID_QUEUE(zq->queue->qid);
1321 if (card >= maxcard || queue >= maxqueue)
1322 continue;
1323 stat = &devstatus[card * maxqueue + queue];
1324 stat->hwtype = zc->card->ap_dev.device_type;
1325 stat->functions = zc->card->hwinfo.fac >> 26;
1326 stat->qid = zq->queue->qid;
1327 stat->online = zq->online ? 0x01 : 0x00;
1328 }
1329 }
1330 spin_unlock(&zcrypt_list_lock);
1331 }
1332 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1333
zcrypt_device_status_ext(int card,int queue,struct zcrypt_device_status_ext * devstat)1334 int zcrypt_device_status_ext(int card, int queue,
1335 struct zcrypt_device_status_ext *devstat)
1336 {
1337 struct zcrypt_card *zc;
1338 struct zcrypt_queue *zq;
1339
1340 memset(devstat, 0, sizeof(*devstat));
1341
1342 spin_lock(&zcrypt_list_lock);
1343 for_each_zcrypt_card(zc) {
1344 for_each_zcrypt_queue(zq, zc) {
1345 if (card == AP_QID_CARD(zq->queue->qid) &&
1346 queue == AP_QID_QUEUE(zq->queue->qid)) {
1347 devstat->hwtype = zc->card->ap_dev.device_type;
1348 devstat->functions = zc->card->hwinfo.fac >> 26;
1349 devstat->qid = zq->queue->qid;
1350 devstat->online = zq->online ? 0x01 : 0x00;
1351 spin_unlock(&zcrypt_list_lock);
1352 return 0;
1353 }
1354 }
1355 }
1356 spin_unlock(&zcrypt_list_lock);
1357
1358 return -ENODEV;
1359 }
1360 EXPORT_SYMBOL(zcrypt_device_status_ext);
1361
zcrypt_status_mask(char status[],size_t max_adapters)1362 static void zcrypt_status_mask(char status[], size_t max_adapters)
1363 {
1364 struct zcrypt_card *zc;
1365 struct zcrypt_queue *zq;
1366 int card;
1367
1368 memset(status, 0, max_adapters);
1369 spin_lock(&zcrypt_list_lock);
1370 for_each_zcrypt_card(zc) {
1371 for_each_zcrypt_queue(zq, zc) {
1372 card = AP_QID_CARD(zq->queue->qid);
1373 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1374 card >= max_adapters)
1375 continue;
1376 status[card] = zc->online ? zc->user_space_type : 0x0d;
1377 }
1378 }
1379 spin_unlock(&zcrypt_list_lock);
1380 }
1381
zcrypt_qdepth_mask(char qdepth[],size_t max_adapters)1382 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1383 {
1384 struct zcrypt_card *zc;
1385 struct zcrypt_queue *zq;
1386 int card;
1387
1388 memset(qdepth, 0, max_adapters);
1389 spin_lock(&zcrypt_list_lock);
1390 local_bh_disable();
1391 for_each_zcrypt_card(zc) {
1392 for_each_zcrypt_queue(zq, zc) {
1393 card = AP_QID_CARD(zq->queue->qid);
1394 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1395 card >= max_adapters)
1396 continue;
1397 spin_lock(&zq->queue->lock);
1398 qdepth[card] =
1399 zq->queue->pendingq_count +
1400 zq->queue->requestq_count;
1401 spin_unlock(&zq->queue->lock);
1402 }
1403 }
1404 local_bh_enable();
1405 spin_unlock(&zcrypt_list_lock);
1406 }
1407
zcrypt_perdev_reqcnt(u32 reqcnt[],size_t max_adapters)1408 static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1409 {
1410 struct zcrypt_card *zc;
1411 struct zcrypt_queue *zq;
1412 int card;
1413 u64 cnt;
1414
1415 memset(reqcnt, 0, sizeof(int) * max_adapters);
1416 spin_lock(&zcrypt_list_lock);
1417 local_bh_disable();
1418 for_each_zcrypt_card(zc) {
1419 for_each_zcrypt_queue(zq, zc) {
1420 card = AP_QID_CARD(zq->queue->qid);
1421 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index ||
1422 card >= max_adapters)
1423 continue;
1424 spin_lock(&zq->queue->lock);
1425 cnt = zq->queue->total_request_count;
1426 spin_unlock(&zq->queue->lock);
1427 reqcnt[card] = (cnt < UINT_MAX) ? (u32)cnt : UINT_MAX;
1428 }
1429 }
1430 local_bh_enable();
1431 spin_unlock(&zcrypt_list_lock);
1432 }
1433
zcrypt_pendingq_count(void)1434 static int zcrypt_pendingq_count(void)
1435 {
1436 struct zcrypt_card *zc;
1437 struct zcrypt_queue *zq;
1438 int pendingq_count;
1439
1440 pendingq_count = 0;
1441 spin_lock(&zcrypt_list_lock);
1442 local_bh_disable();
1443 for_each_zcrypt_card(zc) {
1444 for_each_zcrypt_queue(zq, zc) {
1445 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1446 continue;
1447 spin_lock(&zq->queue->lock);
1448 pendingq_count += zq->queue->pendingq_count;
1449 spin_unlock(&zq->queue->lock);
1450 }
1451 }
1452 local_bh_enable();
1453 spin_unlock(&zcrypt_list_lock);
1454 return pendingq_count;
1455 }
1456
zcrypt_requestq_count(void)1457 static int zcrypt_requestq_count(void)
1458 {
1459 struct zcrypt_card *zc;
1460 struct zcrypt_queue *zq;
1461 int requestq_count;
1462
1463 requestq_count = 0;
1464 spin_lock(&zcrypt_list_lock);
1465 local_bh_disable();
1466 for_each_zcrypt_card(zc) {
1467 for_each_zcrypt_queue(zq, zc) {
1468 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1469 continue;
1470 spin_lock(&zq->queue->lock);
1471 requestq_count += zq->queue->requestq_count;
1472 spin_unlock(&zq->queue->lock);
1473 }
1474 }
1475 local_bh_enable();
1476 spin_unlock(&zcrypt_list_lock);
1477 return requestq_count;
1478 }
1479
icarsamodexpo_ioctl(struct ap_perms * perms,unsigned long arg)1480 static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1481 {
1482 int rc;
1483 struct zcrypt_track tr;
1484 struct ica_rsa_modexpo mex;
1485 struct ica_rsa_modexpo __user *umex = (void __user *)arg;
1486
1487 memset(&tr, 0, sizeof(tr));
1488 if (copy_from_user(&mex, umex, sizeof(mex)))
1489 return -EFAULT;
1490
1491 do {
1492 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1493 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1494
1495 /* on ENODEV failure: retry once again after a requested rescan */
1496 if (rc == -ENODEV && zcrypt_process_rescan())
1497 do {
1498 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1499 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1500 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1501 rc = -EIO;
1502 if (rc) {
1503 pr_debug("ioctl ICARSAMODEXPO rc=%d\n", rc);
1504 return rc;
1505 }
1506 return put_user(mex.outputdatalength, &umex->outputdatalength);
1507 }
1508
icarsacrt_ioctl(struct ap_perms * perms,unsigned long arg)1509 static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1510 {
1511 int rc;
1512 struct zcrypt_track tr;
1513 struct ica_rsa_modexpo_crt crt;
1514 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *)arg;
1515
1516 memset(&tr, 0, sizeof(tr));
1517 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1518 return -EFAULT;
1519
1520 do {
1521 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1522 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1523
1524 /* on ENODEV failure: retry once again after a requested rescan */
1525 if (rc == -ENODEV && zcrypt_process_rescan())
1526 do {
1527 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1528 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1529 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1530 rc = -EIO;
1531 if (rc) {
1532 pr_debug("ioctl ICARSACRT rc=%d\n", rc);
1533 return rc;
1534 }
1535 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1536 }
1537
zsecsendcprb_ioctl(struct ap_perms * perms,unsigned long arg)1538 static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1539 {
1540 int rc;
1541 struct ica_xcRB xcrb;
1542 struct zcrypt_track tr;
1543 u32 xflags = ZCRYPT_XFLAG_USERSPACE;
1544 struct ica_xcRB __user *uxcrb = (void __user *)arg;
1545
1546 memset(&tr, 0, sizeof(tr));
1547 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1548 return -EFAULT;
1549
1550 do {
1551 rc = _zcrypt_send_cprb(xflags, perms, &tr, &xcrb);
1552 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1553
1554 /* on ENODEV failure: retry once again after a requested rescan */
1555 if (rc == -ENODEV && zcrypt_process_rescan())
1556 do {
1557 rc = _zcrypt_send_cprb(xflags, perms, &tr, &xcrb);
1558 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1559 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1560 rc = -EIO;
1561 if (rc)
1562 pr_debug("ioctl ZSENDCPRB rc=%d status=0x%x\n",
1563 rc, xcrb.status);
1564 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1565 return -EFAULT;
1566 return rc;
1567 }
1568
zsendep11cprb_ioctl(struct ap_perms * perms,unsigned long arg)1569 static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1570 {
1571 int rc;
1572 struct ep11_urb xcrb;
1573 struct zcrypt_track tr;
1574 u32 xflags = ZCRYPT_XFLAG_USERSPACE;
1575 struct ep11_urb __user *uxcrb = (void __user *)arg;
1576
1577 memset(&tr, 0, sizeof(tr));
1578 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1579 return -EFAULT;
1580
1581 do {
1582 rc = _zcrypt_send_ep11_cprb(xflags, perms, &tr, &xcrb);
1583 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1584
1585 /* on ENODEV failure: retry once again after a requested rescan */
1586 if (rc == -ENODEV && zcrypt_process_rescan())
1587 do {
1588 rc = _zcrypt_send_ep11_cprb(xflags, perms, &tr, &xcrb);
1589 } while (rc == -EAGAIN && ++tr.again_counter < TRACK_AGAIN_MAX);
1590 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1591 rc = -EIO;
1592 if (rc)
1593 pr_debug("ioctl ZSENDEP11CPRB rc=%d\n", rc);
1594 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1595 return -EFAULT;
1596 return rc;
1597 }
1598
zcrypt_unlocked_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1599 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1600 unsigned long arg)
1601 {
1602 int rc;
1603 struct ap_perms *perms =
1604 (struct ap_perms *)filp->private_data;
1605
1606 rc = zcrypt_check_ioctl(perms, cmd);
1607 if (rc)
1608 return rc;
1609
1610 switch (cmd) {
1611 case ICARSAMODEXPO:
1612 return icarsamodexpo_ioctl(perms, arg);
1613 case ICARSACRT:
1614 return icarsacrt_ioctl(perms, arg);
1615 case ZSECSENDCPRB:
1616 return zsecsendcprb_ioctl(perms, arg);
1617 case ZSENDEP11CPRB:
1618 return zsendep11cprb_ioctl(perms, arg);
1619 case ZCRYPT_DEVICE_STATUS: {
1620 struct zcrypt_device_status_ext *device_status;
1621 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1622 * sizeof(struct zcrypt_device_status_ext);
1623
1624 device_status = kvzalloc_objs(struct zcrypt_device_status_ext,
1625 MAX_ZDEV_ENTRIES_EXT);
1626 if (!device_status)
1627 return -ENOMEM;
1628 zcrypt_device_status_mask_ext(device_status,
1629 MAX_ZDEV_CARDIDS_EXT,
1630 MAX_ZDEV_DOMAINS_EXT);
1631 if (copy_to_user((char __user *)arg, device_status,
1632 total_size))
1633 rc = -EFAULT;
1634 kvfree(device_status);
1635 return rc;
1636 }
1637 case ZCRYPT_STATUS_MASK: {
1638 char status[AP_DEVICES];
1639
1640 zcrypt_status_mask(status, AP_DEVICES);
1641 if (copy_to_user((char __user *)arg, status, sizeof(status)))
1642 return -EFAULT;
1643 return 0;
1644 }
1645 case ZCRYPT_QDEPTH_MASK: {
1646 char qdepth[AP_DEVICES];
1647
1648 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1649 if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
1650 return -EFAULT;
1651 return 0;
1652 }
1653 case ZCRYPT_PERDEV_REQCNT: {
1654 u32 *reqcnt;
1655
1656 reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1657 if (!reqcnt)
1658 return -ENOMEM;
1659 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1660 if (copy_to_user((int __user *)arg, reqcnt,
1661 sizeof(u32) * AP_DEVICES))
1662 rc = -EFAULT;
1663 kfree(reqcnt);
1664 return rc;
1665 }
1666 case Z90STAT_REQUESTQ_COUNT:
1667 return put_user(zcrypt_requestq_count(), (int __user *)arg);
1668 case Z90STAT_PENDINGQ_COUNT:
1669 return put_user(zcrypt_pendingq_count(), (int __user *)arg);
1670 case Z90STAT_TOTALOPEN_COUNT:
1671 return put_user(atomic_read(&zcrypt_open_count),
1672 (int __user *)arg);
1673 case Z90STAT_DOMAIN_INDEX:
1674 return put_user(ap_domain_index, (int __user *)arg);
1675 /*
1676 * Deprecated ioctls
1677 */
1678 case ZDEVICESTATUS: {
1679 /* the old ioctl supports only 64 adapters */
1680 struct zcrypt_device_status *device_status;
1681 size_t total_size = MAX_ZDEV_ENTRIES
1682 * sizeof(struct zcrypt_device_status);
1683
1684 device_status = kzalloc(total_size, GFP_KERNEL);
1685 if (!device_status)
1686 return -ENOMEM;
1687 zcrypt_device_status_mask(device_status);
1688 if (copy_to_user((char __user *)arg, device_status,
1689 total_size))
1690 rc = -EFAULT;
1691 kfree(device_status);
1692 return rc;
1693 }
1694 case Z90STAT_STATUS_MASK: {
1695 /* the old ioctl supports only 64 adapters */
1696 char status[MAX_ZDEV_CARDIDS];
1697
1698 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1699 if (copy_to_user((char __user *)arg, status, sizeof(status)))
1700 return -EFAULT;
1701 return 0;
1702 }
1703 case Z90STAT_QDEPTH_MASK: {
1704 /* the old ioctl supports only 64 adapters */
1705 char qdepth[MAX_ZDEV_CARDIDS];
1706
1707 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1708 if (copy_to_user((char __user *)arg, qdepth, sizeof(qdepth)))
1709 return -EFAULT;
1710 return 0;
1711 }
1712 case Z90STAT_PERDEV_REQCNT: {
1713 /* the old ioctl supports only 64 adapters */
1714 u32 reqcnt[MAX_ZDEV_CARDIDS];
1715
1716 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1717 if (copy_to_user((int __user *)arg, reqcnt, sizeof(reqcnt)))
1718 return -EFAULT;
1719 return 0;
1720 }
1721 /* unknown ioctl number */
1722 default:
1723 pr_debug("unknown ioctl 0x%08x\n", cmd);
1724 return -ENOIOCTLCMD;
1725 }
1726 }
1727
1728 /*
1729 * Misc device file operations.
1730 */
1731 static const struct file_operations zcrypt_fops = {
1732 .owner = THIS_MODULE,
1733 .read = zcrypt_read,
1734 .write = zcrypt_write,
1735 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1736 .open = zcrypt_open,
1737 .release = zcrypt_release,
1738 };
1739
1740 /*
1741 * Misc device.
1742 */
1743 static struct miscdevice zcrypt_misc_device = {
1744 .minor = MISC_DYNAMIC_MINOR,
1745 .name = "z90crypt",
1746 .fops = &zcrypt_fops,
1747 };
1748
1749 static int zcrypt_rng_device_count;
1750 static u32 *zcrypt_rng_buffer;
1751 static int zcrypt_rng_buffer_index;
1752 static DEFINE_MUTEX(zcrypt_rng_mutex);
1753
zcrypt_rng_data_read(struct hwrng * rng,u32 * data)1754 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1755 {
1756 int rc;
1757
1758 /*
1759 * We don't need locking here because the RNG API guarantees serialized
1760 * read method calls.
1761 */
1762 if (zcrypt_rng_buffer_index == 0) {
1763 rc = zcrypt_rng((char *)zcrypt_rng_buffer);
1764 /* on ENODEV failure: retry once again after an AP bus rescan */
1765 if (rc == -ENODEV && zcrypt_process_rescan())
1766 rc = zcrypt_rng((char *)zcrypt_rng_buffer);
1767 if (rc < 0)
1768 return -EIO;
1769 zcrypt_rng_buffer_index = rc / sizeof(*data);
1770 }
1771 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1772 return sizeof(*data);
1773 }
1774
1775 static struct hwrng zcrypt_rng_dev = {
1776 .name = "zcrypt",
1777 .data_read = zcrypt_rng_data_read,
1778 .quality = 990,
1779 };
1780
zcrypt_rng_device_add(void)1781 int zcrypt_rng_device_add(void)
1782 {
1783 int rc = 0;
1784
1785 mutex_lock(&zcrypt_rng_mutex);
1786 if (zcrypt_rng_device_count == 0) {
1787 zcrypt_rng_buffer = kzalloc(PAGE_SIZE, GFP_KERNEL);
1788 if (!zcrypt_rng_buffer) {
1789 rc = -ENOMEM;
1790 goto out;
1791 }
1792 zcrypt_rng_buffer_index = 0;
1793 rc = hwrng_register(&zcrypt_rng_dev);
1794 if (rc)
1795 goto out_free;
1796 zcrypt_rng_device_count = 1;
1797 } else {
1798 zcrypt_rng_device_count++;
1799 }
1800 mutex_unlock(&zcrypt_rng_mutex);
1801 return 0;
1802
1803 out_free:
1804 kfree(zcrypt_rng_buffer);
1805 out:
1806 mutex_unlock(&zcrypt_rng_mutex);
1807 return rc;
1808 }
1809
zcrypt_rng_device_remove(void)1810 void zcrypt_rng_device_remove(void)
1811 {
1812 mutex_lock(&zcrypt_rng_mutex);
1813 zcrypt_rng_device_count--;
1814 if (zcrypt_rng_device_count == 0) {
1815 hwrng_unregister(&zcrypt_rng_dev);
1816 kfree(zcrypt_rng_buffer);
1817 }
1818 mutex_unlock(&zcrypt_rng_mutex);
1819 }
1820
1821 /*
1822 * Wait until the zcrypt api is operational.
1823 * The AP bus scan and the binding of ap devices to device drivers is
1824 * an asynchronous job. This function waits until these initial jobs
1825 * are done and so the zcrypt api should be ready to serve crypto
1826 * requests - if there are resources available. The function uses an
1827 * internal timeout of 30s. The very first caller will either wait for
1828 * ap bus bindings complete or the timeout happens. This state will be
1829 * remembered for further callers which will only be blocked until a
1830 * decision is made (timeout or bindings complete).
1831 * On timeout -ETIME is returned, on success the return value is 0.
1832 */
zcrypt_wait_api_operational(void)1833 int zcrypt_wait_api_operational(void)
1834 {
1835 static DEFINE_MUTEX(zcrypt_wait_api_lock);
1836 static int zcrypt_wait_api_state;
1837 int rc;
1838
1839 rc = mutex_lock_interruptible(&zcrypt_wait_api_lock);
1840 if (rc)
1841 return rc;
1842
1843 switch (zcrypt_wait_api_state) {
1844 case 0:
1845 /* initial state, invoke wait for the ap bus complete */
1846 rc = ap_wait_apqn_bindings_complete(
1847 msecs_to_jiffies(ZCRYPT_WAIT_BINDINGS_COMPLETE_MS));
1848 switch (rc) {
1849 case 0:
1850 /* ap bus bindings are complete */
1851 zcrypt_wait_api_state = 1;
1852 break;
1853 case -EINTR:
1854 /* interrupted, go back to caller */
1855 break;
1856 case -ETIME:
1857 /* timeout */
1858 ZCRYPT_DBF_WARN("%s ap_wait_init_apqn_bindings_complete()=ETIME\n",
1859 __func__);
1860 zcrypt_wait_api_state = -ETIME;
1861 break;
1862 default:
1863 /* other failure */
1864 pr_debug("ap_wait_init_apqn_bindings_complete()=%d\n", rc);
1865 break;
1866 }
1867 break;
1868 case 1:
1869 /* a previous caller already found ap bus bindings complete */
1870 rc = 0;
1871 break;
1872 default:
1873 /* a previous caller had timeout or other failure */
1874 rc = zcrypt_wait_api_state;
1875 break;
1876 }
1877
1878 mutex_unlock(&zcrypt_wait_api_lock);
1879
1880 return rc;
1881 }
1882 EXPORT_SYMBOL(zcrypt_wait_api_operational);
1883
zcrypt_debug_init(void)1884 int __init zcrypt_debug_init(void)
1885 {
1886 zcrypt_dbf_info = debug_register("zcrypt", 2, 1,
1887 ZCRYPT_DBF_MAX_SPRINTF_ARGS * sizeof(long));
1888 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1889 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1890
1891 return 0;
1892 }
1893
zcrypt_debug_exit(void)1894 void zcrypt_debug_exit(void)
1895 {
1896 debug_unregister(zcrypt_dbf_info);
1897 }
1898
zcdn_init(void)1899 static int __init zcdn_init(void)
1900 {
1901 int rc;
1902
1903 /* create a new class 'zcrypt' */
1904 rc = class_register(&zcrypt_class);
1905 if (rc)
1906 goto out_class_register_failed;
1907
1908 /* alloc device minor range */
1909 rc = alloc_chrdev_region(&zcrypt_devt,
1910 0, ZCRYPT_MAX_MINOR_NODES,
1911 ZCRYPT_NAME);
1912 if (rc)
1913 goto out_alloc_chrdev_failed;
1914
1915 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1916 zcrypt_cdev.owner = THIS_MODULE;
1917 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1918 if (rc)
1919 goto out_cdev_add_failed;
1920
1921 /* need some class specific sysfs attributes */
1922 rc = class_create_file(&zcrypt_class, &class_attr_zcdn_create);
1923 if (rc)
1924 goto out_class_create_file_1_failed;
1925 rc = class_create_file(&zcrypt_class, &class_attr_zcdn_destroy);
1926 if (rc)
1927 goto out_class_create_file_2_failed;
1928
1929 return 0;
1930
1931 out_class_create_file_2_failed:
1932 class_remove_file(&zcrypt_class, &class_attr_zcdn_create);
1933 out_class_create_file_1_failed:
1934 cdev_del(&zcrypt_cdev);
1935 out_cdev_add_failed:
1936 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1937 out_alloc_chrdev_failed:
1938 class_unregister(&zcrypt_class);
1939 out_class_register_failed:
1940 return rc;
1941 }
1942
zcdn_exit(void)1943 static void zcdn_exit(void)
1944 {
1945 class_remove_file(&zcrypt_class, &class_attr_zcdn_create);
1946 class_remove_file(&zcrypt_class, &class_attr_zcdn_destroy);
1947 zcdn_destroy_all();
1948 cdev_del(&zcrypt_cdev);
1949 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1950 class_unregister(&zcrypt_class);
1951 }
1952
1953 /*
1954 * zcrypt_api_init(): Module initialization.
1955 *
1956 * The module initialization code.
1957 */
zcrypt_api_init(void)1958 int __init zcrypt_api_init(void)
1959 {
1960 int rc;
1961
1962 /* make sure the mempool threshold is >= 1 */
1963 if (zcrypt_mempool_threshold < 1) {
1964 rc = -EINVAL;
1965 goto out;
1966 }
1967
1968 rc = zcrypt_debug_init();
1969 if (rc)
1970 goto out;
1971
1972 rc = zcdn_init();
1973 if (rc)
1974 goto out_zcdn_init_failed;
1975
1976 rc = zcrypt_ccamisc_init();
1977 if (rc)
1978 goto out_ccamisc_init_failed;
1979
1980 rc = zcrypt_ep11misc_init();
1981 if (rc)
1982 goto out_ep11misc_init_failed;
1983
1984 /* Register the request sprayer. */
1985 rc = misc_register(&zcrypt_misc_device);
1986 if (rc < 0)
1987 goto out_misc_register_failed;
1988
1989 zcrypt_msgtype6_init();
1990 zcrypt_msgtype50_init();
1991
1992 return 0;
1993
1994 out_misc_register_failed:
1995 zcrypt_ep11misc_exit();
1996 out_ep11misc_init_failed:
1997 zcrypt_ccamisc_exit();
1998 out_ccamisc_init_failed:
1999 zcdn_exit();
2000 out_zcdn_init_failed:
2001 zcrypt_debug_exit();
2002 out:
2003 return rc;
2004 }
2005
2006 /*
2007 * zcrypt_api_exit(): Module termination.
2008 *
2009 * The module termination code.
2010 */
zcrypt_api_exit(void)2011 void __exit zcrypt_api_exit(void)
2012 {
2013 zcdn_exit();
2014 misc_deregister(&zcrypt_misc_device);
2015 zcrypt_msgtype6_exit();
2016 zcrypt_msgtype50_exit();
2017 zcrypt_ccamisc_exit();
2018 zcrypt_ep11misc_exit();
2019 zcrypt_debug_exit();
2020 }
2021
2022 module_init(zcrypt_api_init);
2023 module_exit(zcrypt_api_exit);
2024