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