This source file includes following definitions.
- zcrypt_process_rescan
- zcrypt_msgtype_register
- zcrypt_msgtype_unregister
- zcrypt_msgtype
- find_zcdndev_by_name
- find_zcdndev_by_devt
- ioctlmask_show
- ioctlmask_store
- apmask_show
- apmask_store
- aqmask_show
- aqmask_store
- zcdn_create_store
- zcdn_destroy_store
- zcdn_device_release
- zcdn_create
- zcdn_destroy
- zcdn_destroy_all
- zcrypt_read
- zcrypt_write
- zcrypt_open
- zcrypt_release
- zcrypt_check_ioctl
- zcrypt_check_card
- zcrypt_check_queue
- zcrypt_pick_queue
- zcrypt_drop_queue
- zcrypt_card_compare
- zcrypt_queue_compare
- zcrypt_rsa_modexpo
- zcrypt_rsa_crt
- _zcrypt_send_cprb
- zcrypt_send_cprb
- is_desired_ep11_card
- is_desired_ep11_queue
- zcrypt_send_ep11_cprb
- zcrypt_rng
- zcrypt_device_status_mask
- zcrypt_device_status_mask_ext
- zcrypt_device_status_ext
- zcrypt_status_mask
- zcrypt_qdepth_mask
- zcrypt_perdev_reqcnt
- zcrypt_pendingq_count
- zcrypt_requestq_count
- zcrypt_unlocked_ioctl
- trans_modexpo32
- trans_modexpo_crt32
- trans_xcRB32
- zcrypt_compat_ioctl
- zcrypt_rng_data_read
- zcrypt_rng_device_add
- zcrypt_rng_device_remove
- zcrypt_debug_init
- zcrypt_debug_exit
- zcdn_init
- zcdn_exit
- zcrypt_api_init
- zcrypt_api_exit
1
2
3
4
5
6
7
8
9
10
11
12
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 <asm/debug.h>
29
30 #define CREATE_TRACE_POINTS
31 #include <asm/trace/zcrypt.h>
32
33 #include "zcrypt_api.h"
34 #include "zcrypt_debug.h"
35
36 #include "zcrypt_msgtype6.h"
37 #include "zcrypt_msgtype50.h"
38 #include "zcrypt_ccamisc.h"
39
40
41
42
43 MODULE_AUTHOR("IBM Corporation");
44 MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
45 "Copyright IBM Corp. 2001, 2012");
46 MODULE_LICENSE("GPL");
47
48
49
50
51 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
52 EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
53
54 static int zcrypt_hwrng_seed = 1;
55 module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
56 MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
57
58 DEFINE_SPINLOCK(zcrypt_list_lock);
59 LIST_HEAD(zcrypt_card_list);
60 int zcrypt_device_count;
61
62 static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
63 static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
64
65 atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
66 EXPORT_SYMBOL(zcrypt_rescan_req);
67
68 static LIST_HEAD(zcrypt_ops_list);
69
70
71 debug_info_t *zcrypt_dbf_info;
72
73
74
75
76
77
78 static inline int zcrypt_process_rescan(void)
79 {
80 if (atomic_read(&zcrypt_rescan_req)) {
81 atomic_set(&zcrypt_rescan_req, 0);
82 atomic_inc(&zcrypt_rescan_count);
83 ap_bus_force_rescan();
84 ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
85 atomic_inc_return(&zcrypt_rescan_count));
86 return 1;
87 }
88 return 0;
89 }
90
91 void zcrypt_msgtype_register(struct zcrypt_ops *zops)
92 {
93 list_add_tail(&zops->list, &zcrypt_ops_list);
94 }
95
96 void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
97 {
98 list_del_init(&zops->list);
99 }
100
101 struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
102 {
103 struct zcrypt_ops *zops;
104
105 list_for_each_entry(zops, &zcrypt_ops_list, list)
106 if ((zops->variant == variant) &&
107 (!strncmp(zops->name, name, sizeof(zops->name))))
108 return zops;
109 return NULL;
110 }
111 EXPORT_SYMBOL(zcrypt_msgtype);
112
113
114
115
116
117 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
118
119 struct zcdn_device;
120
121 static struct class *zcrypt_class;
122 static dev_t zcrypt_devt;
123 static struct cdev zcrypt_cdev;
124
125 struct zcdn_device {
126 struct device device;
127 struct ap_perms perms;
128 };
129
130 #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
131
132 #define ZCDN_MAX_NAME 32
133
134 static int zcdn_create(const char *name);
135 static int zcdn_destroy(const char *name);
136
137
138
139
140
141
142 static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
143 {
144 struct device *dev = class_find_device_by_name(zcrypt_class, name);
145
146 return dev ? to_zcdn_dev(dev) : NULL;
147 }
148
149
150
151
152
153
154 static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
155 {
156 struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
157
158 return dev ? to_zcdn_dev(dev) : NULL;
159 }
160
161 static ssize_t ioctlmask_show(struct device *dev,
162 struct device_attribute *attr,
163 char *buf)
164 {
165 int i, rc;
166 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
167
168 if (mutex_lock_interruptible(&ap_perms_mutex))
169 return -ERESTARTSYS;
170
171 buf[0] = '0';
172 buf[1] = 'x';
173 for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
174 snprintf(buf + 2 + 2 * i * sizeof(long),
175 PAGE_SIZE - 2 - 2 * i * sizeof(long),
176 "%016lx", zcdndev->perms.ioctlm[i]);
177 buf[2 + 2 * i * sizeof(long)] = '\n';
178 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
179 rc = 2 + 2 * i * sizeof(long) + 1;
180
181 mutex_unlock(&ap_perms_mutex);
182
183 return rc;
184 }
185
186 static ssize_t ioctlmask_store(struct device *dev,
187 struct device_attribute *attr,
188 const char *buf, size_t count)
189 {
190 int rc;
191 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
192
193 rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
194 AP_IOCTLS, &ap_perms_mutex);
195 if (rc)
196 return rc;
197
198 return count;
199 }
200
201 static DEVICE_ATTR_RW(ioctlmask);
202
203 static ssize_t apmask_show(struct device *dev,
204 struct device_attribute *attr,
205 char *buf)
206 {
207 int i, rc;
208 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
209
210 if (mutex_lock_interruptible(&ap_perms_mutex))
211 return -ERESTARTSYS;
212
213 buf[0] = '0';
214 buf[1] = 'x';
215 for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
216 snprintf(buf + 2 + 2 * i * sizeof(long),
217 PAGE_SIZE - 2 - 2 * i * sizeof(long),
218 "%016lx", zcdndev->perms.apm[i]);
219 buf[2 + 2 * i * sizeof(long)] = '\n';
220 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
221 rc = 2 + 2 * i * sizeof(long) + 1;
222
223 mutex_unlock(&ap_perms_mutex);
224
225 return rc;
226 }
227
228 static ssize_t apmask_store(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t count)
231 {
232 int rc;
233 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
234
235 rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
236 AP_DEVICES, &ap_perms_mutex);
237 if (rc)
238 return rc;
239
240 return count;
241 }
242
243 static DEVICE_ATTR_RW(apmask);
244
245 static ssize_t aqmask_show(struct device *dev,
246 struct device_attribute *attr,
247 char *buf)
248 {
249 int i, rc;
250 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
251
252 if (mutex_lock_interruptible(&ap_perms_mutex))
253 return -ERESTARTSYS;
254
255 buf[0] = '0';
256 buf[1] = 'x';
257 for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
258 snprintf(buf + 2 + 2 * i * sizeof(long),
259 PAGE_SIZE - 2 - 2 * i * sizeof(long),
260 "%016lx", zcdndev->perms.aqm[i]);
261 buf[2 + 2 * i * sizeof(long)] = '\n';
262 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
263 rc = 2 + 2 * i * sizeof(long) + 1;
264
265 mutex_unlock(&ap_perms_mutex);
266
267 return rc;
268 }
269
270 static ssize_t aqmask_store(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf, size_t count)
273 {
274 int rc;
275 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
276
277 rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
278 AP_DOMAINS, &ap_perms_mutex);
279 if (rc)
280 return rc;
281
282 return count;
283 }
284
285 static DEVICE_ATTR_RW(aqmask);
286
287 static struct attribute *zcdn_dev_attrs[] = {
288 &dev_attr_ioctlmask.attr,
289 &dev_attr_apmask.attr,
290 &dev_attr_aqmask.attr,
291 NULL
292 };
293
294 static struct attribute_group zcdn_dev_attr_group = {
295 .attrs = zcdn_dev_attrs
296 };
297
298 static const struct attribute_group *zcdn_dev_attr_groups[] = {
299 &zcdn_dev_attr_group,
300 NULL
301 };
302
303 static ssize_t zcdn_create_store(struct class *class,
304 struct class_attribute *attr,
305 const char *buf, size_t count)
306 {
307 int rc;
308 char name[ZCDN_MAX_NAME];
309
310 strncpy(name, skip_spaces(buf), sizeof(name));
311 name[sizeof(name) - 1] = '\0';
312
313 rc = zcdn_create(strim(name));
314
315 return rc ? rc : count;
316 }
317
318 static const struct class_attribute class_attr_zcdn_create =
319 __ATTR(create, 0600, NULL, zcdn_create_store);
320
321 static ssize_t zcdn_destroy_store(struct class *class,
322 struct class_attribute *attr,
323 const char *buf, size_t count)
324 {
325 int rc;
326 char name[ZCDN_MAX_NAME];
327
328 strncpy(name, skip_spaces(buf), sizeof(name));
329 name[sizeof(name) - 1] = '\0';
330
331 rc = zcdn_destroy(strim(name));
332
333 return rc ? rc : count;
334 }
335
336 static const struct class_attribute class_attr_zcdn_destroy =
337 __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
338
339 static void zcdn_device_release(struct device *dev)
340 {
341 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
342
343 ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
344 MAJOR(dev->devt), MINOR(dev->devt));
345
346 kfree(zcdndev);
347 }
348
349 static int zcdn_create(const char *name)
350 {
351 dev_t devt;
352 int i, rc = 0;
353 char nodename[ZCDN_MAX_NAME];
354 struct zcdn_device *zcdndev;
355
356 if (mutex_lock_interruptible(&ap_perms_mutex))
357 return -ERESTARTSYS;
358
359
360 if (name[0]) {
361 zcdndev = find_zcdndev_by_name(name);
362 if (zcdndev) {
363 put_device(&zcdndev->device);
364 rc = -EEXIST;
365 goto unlockout;
366 }
367 }
368
369
370 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
371 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
372 zcdndev = find_zcdndev_by_devt(devt);
373 if (zcdndev)
374 put_device(&zcdndev->device);
375 else
376 break;
377 }
378 if (i == ZCRYPT_MAX_MINOR_NODES) {
379 rc = -ENOSPC;
380 goto unlockout;
381 }
382
383
384 zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
385 if (!zcdndev) {
386 rc = -ENOMEM;
387 goto unlockout;
388 }
389 zcdndev->device.release = zcdn_device_release;
390 zcdndev->device.class = zcrypt_class;
391 zcdndev->device.devt = devt;
392 zcdndev->device.groups = zcdn_dev_attr_groups;
393 if (name[0])
394 strncpy(nodename, name, sizeof(nodename));
395 else
396 snprintf(nodename, sizeof(nodename),
397 ZCRYPT_NAME "_%d", (int) MINOR(devt));
398 nodename[sizeof(nodename)-1] = '\0';
399 if (dev_set_name(&zcdndev->device, nodename)) {
400 rc = -EINVAL;
401 goto unlockout;
402 }
403 rc = device_register(&zcdndev->device);
404 if (rc) {
405 put_device(&zcdndev->device);
406 goto unlockout;
407 }
408
409 ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
410 MAJOR(devt), MINOR(devt));
411
412 unlockout:
413 mutex_unlock(&ap_perms_mutex);
414 return rc;
415 }
416
417 static int zcdn_destroy(const char *name)
418 {
419 int rc = 0;
420 struct zcdn_device *zcdndev;
421
422 if (mutex_lock_interruptible(&ap_perms_mutex))
423 return -ERESTARTSYS;
424
425
426 zcdndev = find_zcdndev_by_name(name);
427 if (!zcdndev) {
428 rc = -ENOENT;
429 goto unlockout;
430 }
431
432
433
434
435
436 put_device(&zcdndev->device);
437 device_unregister(&zcdndev->device);
438
439 unlockout:
440 mutex_unlock(&ap_perms_mutex);
441 return rc;
442 }
443
444 static void zcdn_destroy_all(void)
445 {
446 int i;
447 dev_t devt;
448 struct zcdn_device *zcdndev;
449
450 mutex_lock(&ap_perms_mutex);
451 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
452 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
453 zcdndev = find_zcdndev_by_devt(devt);
454 if (zcdndev) {
455 put_device(&zcdndev->device);
456 device_unregister(&zcdndev->device);
457 }
458 }
459 mutex_unlock(&ap_perms_mutex);
460 }
461
462 #endif
463
464
465
466
467
468
469 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
470 size_t count, loff_t *f_pos)
471 {
472 return -EPERM;
473 }
474
475
476
477
478
479
480 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
481 size_t count, loff_t *f_pos)
482 {
483 return -EPERM;
484 }
485
486
487
488
489
490
491 static int zcrypt_open(struct inode *inode, struct file *filp)
492 {
493 struct ap_perms *perms = &ap_perms;
494
495 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
496 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
497 struct zcdn_device *zcdndev;
498
499 if (mutex_lock_interruptible(&ap_perms_mutex))
500 return -ERESTARTSYS;
501 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
502
503 mutex_unlock(&ap_perms_mutex);
504 if (zcdndev)
505 perms = &zcdndev->perms;
506 }
507 #endif
508 filp->private_data = (void *) perms;
509
510 atomic_inc(&zcrypt_open_count);
511 return stream_open(inode, filp);
512 }
513
514
515
516
517
518
519 static int zcrypt_release(struct inode *inode, struct file *filp)
520 {
521 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
522 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
523 struct zcdn_device *zcdndev;
524
525 mutex_lock(&ap_perms_mutex);
526 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
527 mutex_unlock(&ap_perms_mutex);
528 if (zcdndev) {
529
530 put_device(&zcdndev->device);
531 put_device(&zcdndev->device);
532 }
533 }
534 #endif
535
536 atomic_dec(&zcrypt_open_count);
537 return 0;
538 }
539
540 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
541 unsigned int cmd)
542 {
543 int rc = -EPERM;
544 int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
545
546 if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
547 if (test_bit_inv(ioctlnr, perms->ioctlm))
548 rc = 0;
549 }
550
551 if (rc)
552 ZCRYPT_DBF(DBF_WARN,
553 "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
554 ioctlnr, rc);
555
556 return rc;
557 }
558
559 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
560 {
561 return test_bit_inv(card, perms->apm) ? true : false;
562 }
563
564 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
565 {
566 return test_bit_inv(queue, perms->aqm) ? true : false;
567 }
568
569 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
570 struct zcrypt_queue *zq,
571 struct module **pmod,
572 unsigned int weight)
573 {
574 if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
575 return NULL;
576 zcrypt_queue_get(zq);
577 get_device(&zq->queue->ap_dev.device);
578 atomic_add(weight, &zc->load);
579 atomic_add(weight, &zq->load);
580 zq->request_count++;
581 *pmod = zq->queue->ap_dev.drv->driver.owner;
582 return zq;
583 }
584
585 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
586 struct zcrypt_queue *zq,
587 struct module *mod,
588 unsigned int weight)
589 {
590 zq->request_count--;
591 atomic_sub(weight, &zc->load);
592 atomic_sub(weight, &zq->load);
593 put_device(&zq->queue->ap_dev.device);
594 zcrypt_queue_put(zq);
595 module_put(mod);
596 }
597
598 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
599 struct zcrypt_card *pref_zc,
600 unsigned int weight,
601 unsigned int pref_weight)
602 {
603 if (!pref_zc)
604 return false;
605 weight += atomic_read(&zc->load);
606 pref_weight += atomic_read(&pref_zc->load);
607 if (weight == pref_weight)
608 return atomic64_read(&zc->card->total_request_count) >
609 atomic64_read(&pref_zc->card->total_request_count);
610 return weight > pref_weight;
611 }
612
613 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
614 struct zcrypt_queue *pref_zq,
615 unsigned int weight,
616 unsigned int pref_weight)
617 {
618 if (!pref_zq)
619 return false;
620 weight += atomic_read(&zq->load);
621 pref_weight += atomic_read(&pref_zq->load);
622 if (weight == pref_weight)
623 return zq->queue->total_request_count >
624 pref_zq->queue->total_request_count;
625 return weight > pref_weight;
626 }
627
628
629
630
631 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
632 struct ica_rsa_modexpo *mex)
633 {
634 struct zcrypt_card *zc, *pref_zc;
635 struct zcrypt_queue *zq, *pref_zq;
636 unsigned int weight, pref_weight;
637 unsigned int func_code;
638 int qid = 0, rc = -ENODEV;
639 struct module *mod;
640
641 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
642
643 if (mex->outputdatalength < mex->inputdatalength) {
644 func_code = 0;
645 rc = -EINVAL;
646 goto out;
647 }
648
649
650
651
652
653
654 mex->outputdatalength = mex->inputdatalength;
655
656 rc = get_rsa_modex_fc(mex, &func_code);
657 if (rc)
658 goto out;
659
660 pref_zc = NULL;
661 pref_zq = NULL;
662 spin_lock(&zcrypt_list_lock);
663 for_each_zcrypt_card(zc) {
664
665 if (!zc->online || !(zc->card->functions & 0x18000000))
666 continue;
667
668 if (zc->min_mod_size > mex->inputdatalength ||
669 zc->max_mod_size < mex->inputdatalength)
670 continue;
671
672 if (!zcrypt_check_card(perms, zc->card->id))
673 continue;
674
675 weight = zc->speed_rating[func_code];
676 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
677 continue;
678 for_each_zcrypt_queue(zq, zc) {
679
680 if (!zq->online || !zq->ops->rsa_modexpo)
681 continue;
682
683 if (!zcrypt_check_queue(perms,
684 AP_QID_QUEUE(zq->queue->qid)))
685 continue;
686 if (zcrypt_queue_compare(zq, pref_zq,
687 weight, pref_weight))
688 continue;
689 pref_zc = zc;
690 pref_zq = zq;
691 pref_weight = weight;
692 }
693 }
694 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
695 spin_unlock(&zcrypt_list_lock);
696
697 if (!pref_zq) {
698 rc = -ENODEV;
699 goto out;
700 }
701
702 qid = pref_zq->queue->qid;
703 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex);
704
705 spin_lock(&zcrypt_list_lock);
706 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
707 spin_unlock(&zcrypt_list_lock);
708
709 out:
710 trace_s390_zcrypt_rep(mex, func_code, rc,
711 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
712 return rc;
713 }
714
715 static long zcrypt_rsa_crt(struct ap_perms *perms,
716 struct ica_rsa_modexpo_crt *crt)
717 {
718 struct zcrypt_card *zc, *pref_zc;
719 struct zcrypt_queue *zq, *pref_zq;
720 unsigned int weight, pref_weight;
721 unsigned int func_code;
722 int qid = 0, rc = -ENODEV;
723 struct module *mod;
724
725 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
726
727 if (crt->outputdatalength < crt->inputdatalength) {
728 func_code = 0;
729 rc = -EINVAL;
730 goto out;
731 }
732
733
734
735
736
737
738 crt->outputdatalength = crt->inputdatalength;
739
740 rc = get_rsa_crt_fc(crt, &func_code);
741 if (rc)
742 goto out;
743
744 pref_zc = NULL;
745 pref_zq = NULL;
746 spin_lock(&zcrypt_list_lock);
747 for_each_zcrypt_card(zc) {
748
749 if (!zc->online || !(zc->card->functions & 0x18000000))
750 continue;
751
752 if (zc->min_mod_size > crt->inputdatalength ||
753 zc->max_mod_size < crt->inputdatalength)
754 continue;
755
756 if (!zcrypt_check_card(perms, zc->card->id))
757 continue;
758
759 weight = zc->speed_rating[func_code];
760 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
761 continue;
762 for_each_zcrypt_queue(zq, zc) {
763
764 if (!zq->online || !zq->ops->rsa_modexpo_crt)
765 continue;
766
767 if (!zcrypt_check_queue(perms,
768 AP_QID_QUEUE(zq->queue->qid)))
769 continue;
770 if (zcrypt_queue_compare(zq, pref_zq,
771 weight, pref_weight))
772 continue;
773 pref_zc = zc;
774 pref_zq = zq;
775 pref_weight = weight;
776 }
777 }
778 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
779 spin_unlock(&zcrypt_list_lock);
780
781 if (!pref_zq) {
782 rc = -ENODEV;
783 goto out;
784 }
785
786 qid = pref_zq->queue->qid;
787 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt);
788
789 spin_lock(&zcrypt_list_lock);
790 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
791 spin_unlock(&zcrypt_list_lock);
792
793 out:
794 trace_s390_zcrypt_rep(crt, func_code, rc,
795 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
796 return rc;
797 }
798
799 static long _zcrypt_send_cprb(struct ap_perms *perms,
800 struct ica_xcRB *xcRB)
801 {
802 struct zcrypt_card *zc, *pref_zc;
803 struct zcrypt_queue *zq, *pref_zq;
804 struct ap_message ap_msg;
805 unsigned int weight, pref_weight;
806 unsigned int func_code;
807 unsigned short *domain, tdom;
808 int qid = 0, rc = -ENODEV;
809 struct module *mod;
810
811 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
812
813 xcRB->status = 0;
814 ap_init_message(&ap_msg);
815 rc = get_cprb_fc(xcRB, &ap_msg, &func_code, &domain);
816 if (rc)
817 goto out;
818
819
820
821
822
823 tdom = *domain;
824 if (tdom >= 0 && tdom < AP_DOMAINS &&
825 !ap_test_config_usage_domain(tdom) &&
826 ap_test_config_ctrl_domain(tdom) &&
827 ap_domain_index >= 0)
828 tdom = ap_domain_index;
829
830 pref_zc = NULL;
831 pref_zq = NULL;
832 spin_lock(&zcrypt_list_lock);
833 for_each_zcrypt_card(zc) {
834
835 if (!zc->online || !(zc->card->functions & 0x10000000))
836 continue;
837
838 if (xcRB->user_defined != AUTOSELECT &&
839 xcRB->user_defined != zc->card->id)
840 continue;
841
842 if (!zcrypt_check_card(perms, zc->card->id))
843 continue;
844
845 weight = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
846 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
847 continue;
848 for_each_zcrypt_queue(zq, zc) {
849
850 if (!zq->online ||
851 !zq->ops->send_cprb ||
852 (tdom != (unsigned short) AUTOSELECT &&
853 tdom != AP_QID_QUEUE(zq->queue->qid)))
854 continue;
855
856 if (!zcrypt_check_queue(perms,
857 AP_QID_QUEUE(zq->queue->qid)))
858 continue;
859 if (zcrypt_queue_compare(zq, pref_zq,
860 weight, pref_weight))
861 continue;
862 pref_zc = zc;
863 pref_zq = zq;
864 pref_weight = weight;
865 }
866 }
867 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
868 spin_unlock(&zcrypt_list_lock);
869
870 if (!pref_zq) {
871 rc = -ENODEV;
872 goto out;
873 }
874
875
876 qid = pref_zq->queue->qid;
877 if (*domain == (unsigned short) AUTOSELECT)
878 *domain = AP_QID_QUEUE(qid);
879
880 rc = pref_zq->ops->send_cprb(pref_zq, xcRB, &ap_msg);
881
882 spin_lock(&zcrypt_list_lock);
883 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
884 spin_unlock(&zcrypt_list_lock);
885
886 out:
887 ap_release_message(&ap_msg);
888 trace_s390_zcrypt_rep(xcRB, func_code, rc,
889 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
890 return rc;
891 }
892
893 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
894 {
895 return _zcrypt_send_cprb(&ap_perms, xcRB);
896 }
897 EXPORT_SYMBOL(zcrypt_send_cprb);
898
899 static bool is_desired_ep11_card(unsigned int dev_id,
900 unsigned short target_num,
901 struct ep11_target_dev *targets)
902 {
903 while (target_num-- > 0) {
904 if (dev_id == targets->ap_id)
905 return true;
906 targets++;
907 }
908 return false;
909 }
910
911 static bool is_desired_ep11_queue(unsigned int dev_qid,
912 unsigned short target_num,
913 struct ep11_target_dev *targets)
914 {
915 while (target_num-- > 0) {
916 if (AP_MKQID(targets->ap_id, targets->dom_id) == dev_qid)
917 return true;
918 targets++;
919 }
920 return false;
921 }
922
923 static long zcrypt_send_ep11_cprb(struct ap_perms *perms,
924 struct ep11_urb *xcrb)
925 {
926 struct zcrypt_card *zc, *pref_zc;
927 struct zcrypt_queue *zq, *pref_zq;
928 struct ep11_target_dev *targets;
929 unsigned short target_num;
930 unsigned int weight, pref_weight;
931 unsigned int func_code;
932 struct ap_message ap_msg;
933 int qid = 0, rc = -ENODEV;
934 struct module *mod;
935
936 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
937
938 ap_init_message(&ap_msg);
939
940 target_num = (unsigned short) xcrb->targets_num;
941
942
943 targets = NULL;
944 if (target_num != 0) {
945 struct ep11_target_dev __user *uptr;
946
947 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
948 if (!targets) {
949 func_code = 0;
950 rc = -ENOMEM;
951 goto out;
952 }
953
954 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
955 if (copy_from_user(targets, uptr,
956 target_num * sizeof(*targets))) {
957 func_code = 0;
958 rc = -EFAULT;
959 goto out_free;
960 }
961 }
962
963 rc = get_ep11cprb_fc(xcrb, &ap_msg, &func_code);
964 if (rc)
965 goto out_free;
966
967 pref_zc = NULL;
968 pref_zq = NULL;
969 spin_lock(&zcrypt_list_lock);
970 for_each_zcrypt_card(zc) {
971
972 if (!zc->online || !(zc->card->functions & 0x04000000))
973 continue;
974
975 if (targets &&
976 !is_desired_ep11_card(zc->card->id, target_num, targets))
977 continue;
978
979 if (!zcrypt_check_card(perms, zc->card->id))
980 continue;
981
982 weight = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
983 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
984 continue;
985 for_each_zcrypt_queue(zq, zc) {
986
987 if (!zq->online ||
988 !zq->ops->send_ep11_cprb ||
989 (targets &&
990 !is_desired_ep11_queue(zq->queue->qid,
991 target_num, targets)))
992 continue;
993
994 if (!zcrypt_check_queue(perms,
995 AP_QID_QUEUE(zq->queue->qid)))
996 continue;
997 if (zcrypt_queue_compare(zq, pref_zq,
998 weight, pref_weight))
999 continue;
1000 pref_zc = zc;
1001 pref_zq = zq;
1002 pref_weight = weight;
1003 }
1004 }
1005 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1006 spin_unlock(&zcrypt_list_lock);
1007
1008 if (!pref_zq) {
1009 rc = -ENODEV;
1010 goto out_free;
1011 }
1012
1013 qid = pref_zq->queue->qid;
1014 rc = pref_zq->ops->send_ep11_cprb(pref_zq, xcrb, &ap_msg);
1015
1016 spin_lock(&zcrypt_list_lock);
1017 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1018 spin_unlock(&zcrypt_list_lock);
1019
1020 out_free:
1021 kfree(targets);
1022 out:
1023 ap_release_message(&ap_msg);
1024 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1025 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1026 return rc;
1027 }
1028
1029 static long zcrypt_rng(char *buffer)
1030 {
1031 struct zcrypt_card *zc, *pref_zc;
1032 struct zcrypt_queue *zq, *pref_zq;
1033 unsigned int weight, pref_weight;
1034 unsigned int func_code;
1035 struct ap_message ap_msg;
1036 unsigned int domain;
1037 int qid = 0, rc = -ENODEV;
1038 struct module *mod;
1039
1040 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1041
1042 ap_init_message(&ap_msg);
1043 rc = get_rng_fc(&ap_msg, &func_code, &domain);
1044 if (rc)
1045 goto out;
1046
1047 pref_zc = NULL;
1048 pref_zq = NULL;
1049 spin_lock(&zcrypt_list_lock);
1050 for_each_zcrypt_card(zc) {
1051
1052 if (!zc->online || !(zc->card->functions & 0x10000000))
1053 continue;
1054
1055 weight = zc->speed_rating[func_code];
1056 if (zcrypt_card_compare(zc, pref_zc, weight, pref_weight))
1057 continue;
1058 for_each_zcrypt_queue(zq, zc) {
1059
1060 if (!zq->online || !zq->ops->rng)
1061 continue;
1062 if (zcrypt_queue_compare(zq, pref_zq,
1063 weight, pref_weight))
1064 continue;
1065 pref_zc = zc;
1066 pref_zq = zq;
1067 pref_weight = weight;
1068 }
1069 }
1070 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, weight);
1071 spin_unlock(&zcrypt_list_lock);
1072
1073 if (!pref_zq) {
1074 rc = -ENODEV;
1075 goto out;
1076 }
1077
1078 qid = pref_zq->queue->qid;
1079 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1080
1081 spin_lock(&zcrypt_list_lock);
1082 zcrypt_drop_queue(pref_zc, pref_zq, mod, weight);
1083 spin_unlock(&zcrypt_list_lock);
1084
1085 out:
1086 ap_release_message(&ap_msg);
1087 trace_s390_zcrypt_rep(buffer, func_code, rc,
1088 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1089 return rc;
1090 }
1091
1092 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1093 {
1094 struct zcrypt_card *zc;
1095 struct zcrypt_queue *zq;
1096 struct zcrypt_device_status *stat;
1097 int card, queue;
1098
1099 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1100 * sizeof(struct zcrypt_device_status));
1101
1102 spin_lock(&zcrypt_list_lock);
1103 for_each_zcrypt_card(zc) {
1104 for_each_zcrypt_queue(zq, zc) {
1105 card = AP_QID_CARD(zq->queue->qid);
1106 if (card >= MAX_ZDEV_CARDIDS)
1107 continue;
1108 queue = AP_QID_QUEUE(zq->queue->qid);
1109 stat = &devstatus[card * AP_DOMAINS + queue];
1110 stat->hwtype = zc->card->ap_dev.device_type;
1111 stat->functions = zc->card->functions >> 26;
1112 stat->qid = zq->queue->qid;
1113 stat->online = zq->online ? 0x01 : 0x00;
1114 }
1115 }
1116 spin_unlock(&zcrypt_list_lock);
1117 }
1118
1119 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1120 {
1121 struct zcrypt_card *zc;
1122 struct zcrypt_queue *zq;
1123 struct zcrypt_device_status_ext *stat;
1124 int card, queue;
1125
1126 memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1127 * sizeof(struct zcrypt_device_status_ext));
1128
1129 spin_lock(&zcrypt_list_lock);
1130 for_each_zcrypt_card(zc) {
1131 for_each_zcrypt_queue(zq, zc) {
1132 card = AP_QID_CARD(zq->queue->qid);
1133 queue = AP_QID_QUEUE(zq->queue->qid);
1134 stat = &devstatus[card * AP_DOMAINS + queue];
1135 stat->hwtype = zc->card->ap_dev.device_type;
1136 stat->functions = zc->card->functions >> 26;
1137 stat->qid = zq->queue->qid;
1138 stat->online = zq->online ? 0x01 : 0x00;
1139 }
1140 }
1141 spin_unlock(&zcrypt_list_lock);
1142 }
1143 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1144
1145 int zcrypt_device_status_ext(int card, int queue,
1146 struct zcrypt_device_status_ext *devstat)
1147 {
1148 struct zcrypt_card *zc;
1149 struct zcrypt_queue *zq;
1150
1151 memset(devstat, 0, sizeof(*devstat));
1152
1153 spin_lock(&zcrypt_list_lock);
1154 for_each_zcrypt_card(zc) {
1155 for_each_zcrypt_queue(zq, zc) {
1156 if (card == AP_QID_CARD(zq->queue->qid) &&
1157 queue == AP_QID_QUEUE(zq->queue->qid)) {
1158 devstat->hwtype = zc->card->ap_dev.device_type;
1159 devstat->functions = zc->card->functions >> 26;
1160 devstat->qid = zq->queue->qid;
1161 devstat->online = zq->online ? 0x01 : 0x00;
1162 spin_unlock(&zcrypt_list_lock);
1163 return 0;
1164 }
1165 }
1166 }
1167 spin_unlock(&zcrypt_list_lock);
1168
1169 return -ENODEV;
1170 }
1171 EXPORT_SYMBOL(zcrypt_device_status_ext);
1172
1173 static void zcrypt_status_mask(char status[], size_t max_adapters)
1174 {
1175 struct zcrypt_card *zc;
1176 struct zcrypt_queue *zq;
1177 int card;
1178
1179 memset(status, 0, max_adapters);
1180 spin_lock(&zcrypt_list_lock);
1181 for_each_zcrypt_card(zc) {
1182 for_each_zcrypt_queue(zq, zc) {
1183 card = AP_QID_CARD(zq->queue->qid);
1184 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1185 || card >= max_adapters)
1186 continue;
1187 status[card] = zc->online ? zc->user_space_type : 0x0d;
1188 }
1189 }
1190 spin_unlock(&zcrypt_list_lock);
1191 }
1192
1193 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1194 {
1195 struct zcrypt_card *zc;
1196 struct zcrypt_queue *zq;
1197 int card;
1198
1199 memset(qdepth, 0, max_adapters);
1200 spin_lock(&zcrypt_list_lock);
1201 local_bh_disable();
1202 for_each_zcrypt_card(zc) {
1203 for_each_zcrypt_queue(zq, zc) {
1204 card = AP_QID_CARD(zq->queue->qid);
1205 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1206 || card >= max_adapters)
1207 continue;
1208 spin_lock(&zq->queue->lock);
1209 qdepth[card] =
1210 zq->queue->pendingq_count +
1211 zq->queue->requestq_count;
1212 spin_unlock(&zq->queue->lock);
1213 }
1214 }
1215 local_bh_enable();
1216 spin_unlock(&zcrypt_list_lock);
1217 }
1218
1219 static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1220 {
1221 struct zcrypt_card *zc;
1222 struct zcrypt_queue *zq;
1223 int card;
1224 u64 cnt;
1225
1226 memset(reqcnt, 0, sizeof(int) * max_adapters);
1227 spin_lock(&zcrypt_list_lock);
1228 local_bh_disable();
1229 for_each_zcrypt_card(zc) {
1230 for_each_zcrypt_queue(zq, zc) {
1231 card = AP_QID_CARD(zq->queue->qid);
1232 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1233 || card >= max_adapters)
1234 continue;
1235 spin_lock(&zq->queue->lock);
1236 cnt = zq->queue->total_request_count;
1237 spin_unlock(&zq->queue->lock);
1238 reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX;
1239 }
1240 }
1241 local_bh_enable();
1242 spin_unlock(&zcrypt_list_lock);
1243 }
1244
1245 static int zcrypt_pendingq_count(void)
1246 {
1247 struct zcrypt_card *zc;
1248 struct zcrypt_queue *zq;
1249 int pendingq_count;
1250
1251 pendingq_count = 0;
1252 spin_lock(&zcrypt_list_lock);
1253 local_bh_disable();
1254 for_each_zcrypt_card(zc) {
1255 for_each_zcrypt_queue(zq, zc) {
1256 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1257 continue;
1258 spin_lock(&zq->queue->lock);
1259 pendingq_count += zq->queue->pendingq_count;
1260 spin_unlock(&zq->queue->lock);
1261 }
1262 }
1263 local_bh_enable();
1264 spin_unlock(&zcrypt_list_lock);
1265 return pendingq_count;
1266 }
1267
1268 static int zcrypt_requestq_count(void)
1269 {
1270 struct zcrypt_card *zc;
1271 struct zcrypt_queue *zq;
1272 int requestq_count;
1273
1274 requestq_count = 0;
1275 spin_lock(&zcrypt_list_lock);
1276 local_bh_disable();
1277 for_each_zcrypt_card(zc) {
1278 for_each_zcrypt_queue(zq, zc) {
1279 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1280 continue;
1281 spin_lock(&zq->queue->lock);
1282 requestq_count += zq->queue->requestq_count;
1283 spin_unlock(&zq->queue->lock);
1284 }
1285 }
1286 local_bh_enable();
1287 spin_unlock(&zcrypt_list_lock);
1288 return requestq_count;
1289 }
1290
1291 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1292 unsigned long arg)
1293 {
1294 int rc;
1295 struct ap_perms *perms =
1296 (struct ap_perms *) filp->private_data;
1297
1298 rc = zcrypt_check_ioctl(perms, cmd);
1299 if (rc)
1300 return rc;
1301
1302 switch (cmd) {
1303 case ICARSAMODEXPO: {
1304 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1305 struct ica_rsa_modexpo mex;
1306
1307 if (copy_from_user(&mex, umex, sizeof(mex)))
1308 return -EFAULT;
1309 do {
1310 rc = zcrypt_rsa_modexpo(perms, &mex);
1311 } while (rc == -EAGAIN);
1312
1313 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1314 do {
1315 rc = zcrypt_rsa_modexpo(perms, &mex);
1316 } while (rc == -EAGAIN);
1317 if (rc) {
1318 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1319 return rc;
1320 }
1321 return put_user(mex.outputdatalength, &umex->outputdatalength);
1322 }
1323 case ICARSACRT: {
1324 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1325 struct ica_rsa_modexpo_crt crt;
1326
1327 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1328 return -EFAULT;
1329 do {
1330 rc = zcrypt_rsa_crt(perms, &crt);
1331 } while (rc == -EAGAIN);
1332
1333 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1334 do {
1335 rc = zcrypt_rsa_crt(perms, &crt);
1336 } while (rc == -EAGAIN);
1337 if (rc) {
1338 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1339 return rc;
1340 }
1341 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1342 }
1343 case ZSECSENDCPRB: {
1344 struct ica_xcRB __user *uxcRB = (void __user *) arg;
1345 struct ica_xcRB xcRB;
1346
1347 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1348 return -EFAULT;
1349 do {
1350 rc = _zcrypt_send_cprb(perms, &xcRB);
1351 } while (rc == -EAGAIN);
1352
1353 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1354 do {
1355 rc = _zcrypt_send_cprb(perms, &xcRB);
1356 } while (rc == -EAGAIN);
1357 if (rc)
1358 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1359 rc, xcRB.status);
1360 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1361 return -EFAULT;
1362 return rc;
1363 }
1364 case ZSENDEP11CPRB: {
1365 struct ep11_urb __user *uxcrb = (void __user *)arg;
1366 struct ep11_urb xcrb;
1367
1368 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1369 return -EFAULT;
1370 do {
1371 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1372 } while (rc == -EAGAIN);
1373
1374 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1375 do {
1376 rc = zcrypt_send_ep11_cprb(perms, &xcrb);
1377 } while (rc == -EAGAIN);
1378 if (rc)
1379 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1380 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1381 return -EFAULT;
1382 return rc;
1383 }
1384 case ZCRYPT_DEVICE_STATUS: {
1385 struct zcrypt_device_status_ext *device_status;
1386 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1387 * sizeof(struct zcrypt_device_status_ext);
1388
1389 device_status = kzalloc(total_size, GFP_KERNEL);
1390 if (!device_status)
1391 return -ENOMEM;
1392 zcrypt_device_status_mask_ext(device_status);
1393 if (copy_to_user((char __user *) arg, device_status,
1394 total_size))
1395 rc = -EFAULT;
1396 kfree(device_status);
1397 return rc;
1398 }
1399 case ZCRYPT_STATUS_MASK: {
1400 char status[AP_DEVICES];
1401
1402 zcrypt_status_mask(status, AP_DEVICES);
1403 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1404 return -EFAULT;
1405 return 0;
1406 }
1407 case ZCRYPT_QDEPTH_MASK: {
1408 char qdepth[AP_DEVICES];
1409
1410 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1411 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1412 return -EFAULT;
1413 return 0;
1414 }
1415 case ZCRYPT_PERDEV_REQCNT: {
1416 u32 *reqcnt;
1417
1418 reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1419 if (!reqcnt)
1420 return -ENOMEM;
1421 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1422 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1423 rc = -EFAULT;
1424 kfree(reqcnt);
1425 return rc;
1426 }
1427 case Z90STAT_REQUESTQ_COUNT:
1428 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1429 case Z90STAT_PENDINGQ_COUNT:
1430 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1431 case Z90STAT_TOTALOPEN_COUNT:
1432 return put_user(atomic_read(&zcrypt_open_count),
1433 (int __user *) arg);
1434 case Z90STAT_DOMAIN_INDEX:
1435 return put_user(ap_domain_index, (int __user *) arg);
1436
1437
1438
1439 case ZDEVICESTATUS: {
1440
1441 struct zcrypt_device_status *device_status;
1442 size_t total_size = MAX_ZDEV_ENTRIES
1443 * sizeof(struct zcrypt_device_status);
1444
1445 device_status = kzalloc(total_size, GFP_KERNEL);
1446 if (!device_status)
1447 return -ENOMEM;
1448 zcrypt_device_status_mask(device_status);
1449 if (copy_to_user((char __user *) arg, device_status,
1450 total_size))
1451 rc = -EFAULT;
1452 kfree(device_status);
1453 return rc;
1454 }
1455 case Z90STAT_STATUS_MASK: {
1456
1457 char status[MAX_ZDEV_CARDIDS];
1458
1459 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1460 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1461 return -EFAULT;
1462 return 0;
1463 }
1464 case Z90STAT_QDEPTH_MASK: {
1465
1466 char qdepth[MAX_ZDEV_CARDIDS];
1467
1468 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1469 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1470 return -EFAULT;
1471 return 0;
1472 }
1473 case Z90STAT_PERDEV_REQCNT: {
1474
1475 u32 reqcnt[MAX_ZDEV_CARDIDS];
1476
1477 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1478 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1479 return -EFAULT;
1480 return 0;
1481 }
1482
1483 default:
1484 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1485 return -ENOIOCTLCMD;
1486 }
1487 }
1488
1489 #ifdef CONFIG_COMPAT
1490
1491
1492
1493 struct compat_ica_rsa_modexpo {
1494 compat_uptr_t inputdata;
1495 unsigned int inputdatalength;
1496 compat_uptr_t outputdata;
1497 unsigned int outputdatalength;
1498 compat_uptr_t b_key;
1499 compat_uptr_t n_modulus;
1500 };
1501
1502 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1503 unsigned int cmd, unsigned long arg)
1504 {
1505 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1506 struct compat_ica_rsa_modexpo mex32;
1507 struct ica_rsa_modexpo mex64;
1508 long rc;
1509
1510 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1511 return -EFAULT;
1512 mex64.inputdata = compat_ptr(mex32.inputdata);
1513 mex64.inputdatalength = mex32.inputdatalength;
1514 mex64.outputdata = compat_ptr(mex32.outputdata);
1515 mex64.outputdatalength = mex32.outputdatalength;
1516 mex64.b_key = compat_ptr(mex32.b_key);
1517 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1518 do {
1519 rc = zcrypt_rsa_modexpo(perms, &mex64);
1520 } while (rc == -EAGAIN);
1521
1522 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1523 do {
1524 rc = zcrypt_rsa_modexpo(perms, &mex64);
1525 } while (rc == -EAGAIN);
1526 if (rc)
1527 return rc;
1528 return put_user(mex64.outputdatalength,
1529 &umex32->outputdatalength);
1530 }
1531
1532 struct compat_ica_rsa_modexpo_crt {
1533 compat_uptr_t inputdata;
1534 unsigned int inputdatalength;
1535 compat_uptr_t outputdata;
1536 unsigned int outputdatalength;
1537 compat_uptr_t bp_key;
1538 compat_uptr_t bq_key;
1539 compat_uptr_t np_prime;
1540 compat_uptr_t nq_prime;
1541 compat_uptr_t u_mult_inv;
1542 };
1543
1544 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1545 unsigned int cmd, unsigned long arg)
1546 {
1547 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1548 struct compat_ica_rsa_modexpo_crt crt32;
1549 struct ica_rsa_modexpo_crt crt64;
1550 long rc;
1551
1552 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1553 return -EFAULT;
1554 crt64.inputdata = compat_ptr(crt32.inputdata);
1555 crt64.inputdatalength = crt32.inputdatalength;
1556 crt64.outputdata = compat_ptr(crt32.outputdata);
1557 crt64.outputdatalength = crt32.outputdatalength;
1558 crt64.bp_key = compat_ptr(crt32.bp_key);
1559 crt64.bq_key = compat_ptr(crt32.bq_key);
1560 crt64.np_prime = compat_ptr(crt32.np_prime);
1561 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1562 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1563 do {
1564 rc = zcrypt_rsa_crt(perms, &crt64);
1565 } while (rc == -EAGAIN);
1566
1567 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1568 do {
1569 rc = zcrypt_rsa_crt(perms, &crt64);
1570 } while (rc == -EAGAIN);
1571 if (rc)
1572 return rc;
1573 return put_user(crt64.outputdatalength,
1574 &ucrt32->outputdatalength);
1575 }
1576
1577 struct compat_ica_xcRB {
1578 unsigned short agent_ID;
1579 unsigned int user_defined;
1580 unsigned short request_ID;
1581 unsigned int request_control_blk_length;
1582 unsigned char padding1[16 - sizeof(compat_uptr_t)];
1583 compat_uptr_t request_control_blk_addr;
1584 unsigned int request_data_length;
1585 char padding2[16 - sizeof(compat_uptr_t)];
1586 compat_uptr_t request_data_address;
1587 unsigned int reply_control_blk_length;
1588 char padding3[16 - sizeof(compat_uptr_t)];
1589 compat_uptr_t reply_control_blk_addr;
1590 unsigned int reply_data_length;
1591 char padding4[16 - sizeof(compat_uptr_t)];
1592 compat_uptr_t reply_data_addr;
1593 unsigned short priority_window;
1594 unsigned int status;
1595 } __packed;
1596
1597 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1598 unsigned int cmd, unsigned long arg)
1599 {
1600 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1601 struct compat_ica_xcRB xcRB32;
1602 struct ica_xcRB xcRB64;
1603 long rc;
1604
1605 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1606 return -EFAULT;
1607 xcRB64.agent_ID = xcRB32.agent_ID;
1608 xcRB64.user_defined = xcRB32.user_defined;
1609 xcRB64.request_ID = xcRB32.request_ID;
1610 xcRB64.request_control_blk_length =
1611 xcRB32.request_control_blk_length;
1612 xcRB64.request_control_blk_addr =
1613 compat_ptr(xcRB32.request_control_blk_addr);
1614 xcRB64.request_data_length =
1615 xcRB32.request_data_length;
1616 xcRB64.request_data_address =
1617 compat_ptr(xcRB32.request_data_address);
1618 xcRB64.reply_control_blk_length =
1619 xcRB32.reply_control_blk_length;
1620 xcRB64.reply_control_blk_addr =
1621 compat_ptr(xcRB32.reply_control_blk_addr);
1622 xcRB64.reply_data_length = xcRB32.reply_data_length;
1623 xcRB64.reply_data_addr =
1624 compat_ptr(xcRB32.reply_data_addr);
1625 xcRB64.priority_window = xcRB32.priority_window;
1626 xcRB64.status = xcRB32.status;
1627 do {
1628 rc = _zcrypt_send_cprb(perms, &xcRB64);
1629 } while (rc == -EAGAIN);
1630
1631 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1632 do {
1633 rc = _zcrypt_send_cprb(perms, &xcRB64);
1634 } while (rc == -EAGAIN);
1635 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1636 xcRB32.reply_data_length = xcRB64.reply_data_length;
1637 xcRB32.status = xcRB64.status;
1638 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1639 return -EFAULT;
1640 return rc;
1641 }
1642
1643 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1644 unsigned long arg)
1645 {
1646 int rc;
1647 struct ap_perms *perms =
1648 (struct ap_perms *) filp->private_data;
1649
1650 rc = zcrypt_check_ioctl(perms, cmd);
1651 if (rc)
1652 return rc;
1653
1654 if (cmd == ICARSAMODEXPO)
1655 return trans_modexpo32(perms, filp, cmd, arg);
1656 if (cmd == ICARSACRT)
1657 return trans_modexpo_crt32(perms, filp, cmd, arg);
1658 if (cmd == ZSECSENDCPRB)
1659 return trans_xcRB32(perms, filp, cmd, arg);
1660 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1661 }
1662 #endif
1663
1664
1665
1666
1667 static const struct file_operations zcrypt_fops = {
1668 .owner = THIS_MODULE,
1669 .read = zcrypt_read,
1670 .write = zcrypt_write,
1671 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1672 #ifdef CONFIG_COMPAT
1673 .compat_ioctl = zcrypt_compat_ioctl,
1674 #endif
1675 .open = zcrypt_open,
1676 .release = zcrypt_release,
1677 .llseek = no_llseek,
1678 };
1679
1680
1681
1682
1683 static struct miscdevice zcrypt_misc_device = {
1684 .minor = MISC_DYNAMIC_MINOR,
1685 .name = "z90crypt",
1686 .fops = &zcrypt_fops,
1687 };
1688
1689 static int zcrypt_rng_device_count;
1690 static u32 *zcrypt_rng_buffer;
1691 static int zcrypt_rng_buffer_index;
1692 static DEFINE_MUTEX(zcrypt_rng_mutex);
1693
1694 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1695 {
1696 int rc;
1697
1698
1699
1700
1701
1702 if (zcrypt_rng_buffer_index == 0) {
1703 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1704
1705 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1706 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1707 if (rc < 0)
1708 return -EIO;
1709 zcrypt_rng_buffer_index = rc / sizeof(*data);
1710 }
1711 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1712 return sizeof(*data);
1713 }
1714
1715 static struct hwrng zcrypt_rng_dev = {
1716 .name = "zcrypt",
1717 .data_read = zcrypt_rng_data_read,
1718 .quality = 990,
1719 };
1720
1721 int zcrypt_rng_device_add(void)
1722 {
1723 int rc = 0;
1724
1725 mutex_lock(&zcrypt_rng_mutex);
1726 if (zcrypt_rng_device_count == 0) {
1727 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1728 if (!zcrypt_rng_buffer) {
1729 rc = -ENOMEM;
1730 goto out;
1731 }
1732 zcrypt_rng_buffer_index = 0;
1733 if (!zcrypt_hwrng_seed)
1734 zcrypt_rng_dev.quality = 0;
1735 rc = hwrng_register(&zcrypt_rng_dev);
1736 if (rc)
1737 goto out_free;
1738 zcrypt_rng_device_count = 1;
1739 } else
1740 zcrypt_rng_device_count++;
1741 mutex_unlock(&zcrypt_rng_mutex);
1742 return 0;
1743
1744 out_free:
1745 free_page((unsigned long) zcrypt_rng_buffer);
1746 out:
1747 mutex_unlock(&zcrypt_rng_mutex);
1748 return rc;
1749 }
1750
1751 void zcrypt_rng_device_remove(void)
1752 {
1753 mutex_lock(&zcrypt_rng_mutex);
1754 zcrypt_rng_device_count--;
1755 if (zcrypt_rng_device_count == 0) {
1756 hwrng_unregister(&zcrypt_rng_dev);
1757 free_page((unsigned long) zcrypt_rng_buffer);
1758 }
1759 mutex_unlock(&zcrypt_rng_mutex);
1760 }
1761
1762 int __init zcrypt_debug_init(void)
1763 {
1764 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
1765 DBF_MAX_SPRINTF_ARGS * sizeof(long));
1766 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
1767 debug_set_level(zcrypt_dbf_info, DBF_ERR);
1768
1769 return 0;
1770 }
1771
1772 void zcrypt_debug_exit(void)
1773 {
1774 debug_unregister(zcrypt_dbf_info);
1775 }
1776
1777 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1778
1779 static int __init zcdn_init(void)
1780 {
1781 int rc;
1782
1783
1784 zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
1785 if (IS_ERR(zcrypt_class)) {
1786 rc = PTR_ERR(zcrypt_class);
1787 goto out_class_create_failed;
1788 }
1789 zcrypt_class->dev_release = zcdn_device_release;
1790
1791
1792 rc = alloc_chrdev_region(&zcrypt_devt,
1793 0, ZCRYPT_MAX_MINOR_NODES,
1794 ZCRYPT_NAME);
1795 if (rc)
1796 goto out_alloc_chrdev_failed;
1797
1798 cdev_init(&zcrypt_cdev, &zcrypt_fops);
1799 zcrypt_cdev.owner = THIS_MODULE;
1800 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1801 if (rc)
1802 goto out_cdev_add_failed;
1803
1804
1805 rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
1806 if (rc)
1807 goto out_class_create_file_1_failed;
1808 rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
1809 if (rc)
1810 goto out_class_create_file_2_failed;
1811
1812 return 0;
1813
1814 out_class_create_file_2_failed:
1815 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1816 out_class_create_file_1_failed:
1817 cdev_del(&zcrypt_cdev);
1818 out_cdev_add_failed:
1819 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1820 out_alloc_chrdev_failed:
1821 class_destroy(zcrypt_class);
1822 out_class_create_failed:
1823 return rc;
1824 }
1825
1826 static void zcdn_exit(void)
1827 {
1828 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
1829 class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
1830 zcdn_destroy_all();
1831 cdev_del(&zcrypt_cdev);
1832 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
1833 class_destroy(zcrypt_class);
1834 }
1835
1836 #endif
1837
1838
1839
1840
1841
1842
1843 int __init zcrypt_api_init(void)
1844 {
1845 int rc;
1846
1847 rc = zcrypt_debug_init();
1848 if (rc)
1849 goto out;
1850
1851 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1852 rc = zcdn_init();
1853 if (rc)
1854 goto out;
1855 #endif
1856
1857
1858 rc = misc_register(&zcrypt_misc_device);
1859 if (rc < 0)
1860 goto out_misc_register_failed;
1861
1862 zcrypt_msgtype6_init();
1863 zcrypt_msgtype50_init();
1864
1865 return 0;
1866
1867 out_misc_register_failed:
1868 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1869 zcdn_exit();
1870 #endif
1871 zcrypt_debug_exit();
1872 out:
1873 return rc;
1874 }
1875
1876
1877
1878
1879
1880
1881 void __exit zcrypt_api_exit(void)
1882 {
1883 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
1884 zcdn_exit();
1885 #endif
1886 misc_deregister(&zcrypt_misc_device);
1887 zcrypt_msgtype6_exit();
1888 zcrypt_msgtype50_exit();
1889 zcrypt_ccamisc_exit();
1890 zcrypt_debug_exit();
1891 }
1892
1893 module_init(zcrypt_api_init);
1894 module_exit(zcrypt_api_exit);