]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/s390/crypto/zcrypt_api.c
Merge remote-tracking branches 'asoc/topic/tas2552', 'asoc/topic/tas5086', 'asoc...
[mirror_ubuntu-zesty-kernel.git] / drivers / s390 / crypto / zcrypt_api.c
CommitLineData
2dbc2418 1/*
5432114b 2 * zcrypt 2.1.0
2dbc2418 3 *
5e55a488 4 * Copyright IBM Corp. 2001, 2012
2dbc2418
MS
5 * Author(s): Robert Burroughs
6 * Eric Rossman (edrossma@us.ibm.com)
7 * Cornelia Huck <cornelia.huck@de.ibm.com>
8 *
9 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
10 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
11 * Ralph Wuerthner <rwuerthn@de.ibm.com>
5e55a488 12 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
2dbc2418
MS
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/miscdevice.h>
33#include <linux/fs.h>
34#include <linux/proc_fs.h>
34b9243a 35#include <linux/seq_file.h>
2dbc2418 36#include <linux/compat.h>
5a0e3ad6 37#include <linux/slab.h>
60063497 38#include <linux/atomic.h>
2dbc2418 39#include <asm/uaccess.h>
2f7c8bd6 40#include <linux/hw_random.h>
dabecb29
HD
41#include <linux/debugfs.h>
42#include <asm/debug.h>
2dbc2418 43
dabecb29 44#include "zcrypt_debug.h"
2dbc2418
MS
45#include "zcrypt_api.h"
46
91f3e3ea
IT
47#include "zcrypt_msgtype6.h"
48
1749a81d 49/*
2dbc2418
MS
50 * Module description.
51 */
52MODULE_AUTHOR("IBM Corporation");
5e55a488
HD
53MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
54 "Copyright IBM Corp. 2001, 2012");
2dbc2418
MS
55MODULE_LICENSE("GPL");
56
db490cb9
IT
57static int zcrypt_hwrng_seed = 1;
58module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, S_IRUSR|S_IRGRP);
59MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
60
2dbc2418
MS
61static DEFINE_SPINLOCK(zcrypt_device_lock);
62static LIST_HEAD(zcrypt_device_list);
63static int zcrypt_device_count = 0;
64static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
dabecb29
HD
65static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
66
67atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
68EXPORT_SYMBOL(zcrypt_rescan_req);
2dbc2418 69
2f7c8bd6
RW
70static int zcrypt_rng_device_add(void);
71static void zcrypt_rng_device_remove(void);
72
5e55a488
HD
73static DEFINE_SPINLOCK(zcrypt_ops_list_lock);
74static LIST_HEAD(zcrypt_ops_list);
75
dabecb29
HD
76static debug_info_t *zcrypt_dbf_common;
77static debug_info_t *zcrypt_dbf_devices;
78static struct dentry *debugfs_root;
79
1749a81d 80/*
2dbc2418
MS
81 * Device attributes common for all crypto devices.
82 */
83static ssize_t zcrypt_type_show(struct device *dev,
84 struct device_attribute *attr, char *buf)
85{
86 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
87 return snprintf(buf, PAGE_SIZE, "%s\n", zdev->type_string);
88}
89
90static DEVICE_ATTR(type, 0444, zcrypt_type_show, NULL);
91
92static ssize_t zcrypt_online_show(struct device *dev,
93 struct device_attribute *attr, char *buf)
94{
95 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
96 return snprintf(buf, PAGE_SIZE, "%d\n", zdev->online);
97}
98
99static ssize_t zcrypt_online_store(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf, size_t count)
102{
103 struct zcrypt_device *zdev = to_ap_dev(dev)->private;
104 int online;
105
106 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
107 return -EINVAL;
108 zdev->online = online;
dabecb29
HD
109 ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dman", zdev->ap_dev->qid,
110 zdev->online);
2dbc2418
MS
111 if (!online)
112 ap_flush_queue(zdev->ap_dev);
113 return count;
114}
115
116static DEVICE_ATTR(online, 0644, zcrypt_online_show, zcrypt_online_store);
117
118static struct attribute * zcrypt_device_attrs[] = {
119 &dev_attr_type.attr,
120 &dev_attr_online.attr,
121 NULL,
122};
123
124static struct attribute_group zcrypt_device_attr_group = {
125 .attrs = zcrypt_device_attrs,
126};
127
dabecb29
HD
128/**
129 * Process a rescan of the transport layer.
130 *
131 * Returns 1, if the rescan has been processed, otherwise 0.
132 */
133static inline int zcrypt_process_rescan(void)
134{
135 if (atomic_read(&zcrypt_rescan_req)) {
136 atomic_set(&zcrypt_rescan_req, 0);
137 atomic_inc(&zcrypt_rescan_count);
138 ap_bus_force_rescan();
139 ZCRYPT_DBF_COMMON(DBF_INFO, "rescan%07d",
140 atomic_inc_return(&zcrypt_rescan_count));
141 return 1;
142 }
143 return 0;
144}
145
2dbc2418 146/**
1749a81d
FB
147 * __zcrypt_increase_preference(): Increase preference of a crypto device.
148 * @zdev: Pointer the crypto device
149 *
2dbc2418
MS
150 * Move the device towards the head of the device list.
151 * Need to be called while holding the zcrypt device list lock.
152 * Note: cards with speed_rating of 0 are kept at the end of the list.
153 */
154static void __zcrypt_increase_preference(struct zcrypt_device *zdev)
155{
156 struct zcrypt_device *tmp;
157 struct list_head *l;
158
159 if (zdev->speed_rating == 0)
160 return;
161 for (l = zdev->list.prev; l != &zcrypt_device_list; l = l->prev) {
162 tmp = list_entry(l, struct zcrypt_device, list);
163 if ((tmp->request_count + 1) * tmp->speed_rating <=
164 (zdev->request_count + 1) * zdev->speed_rating &&
165 tmp->speed_rating != 0)
166 break;
167 }
168 if (l == zdev->list.prev)
169 return;
170 /* Move zdev behind l */
1fbc9f46 171 list_move(&zdev->list, l);
2dbc2418
MS
172}
173
174/**
1749a81d
FB
175 * __zcrypt_decrease_preference(): Decrease preference of a crypto device.
176 * @zdev: Pointer to a crypto device.
177 *
2dbc2418
MS
178 * Move the device towards the tail of the device list.
179 * Need to be called while holding the zcrypt device list lock.
180 * Note: cards with speed_rating of 0 are kept at the end of the list.
181 */
182static void __zcrypt_decrease_preference(struct zcrypt_device *zdev)
183{
184 struct zcrypt_device *tmp;
185 struct list_head *l;
186
187 if (zdev->speed_rating == 0)
188 return;
189 for (l = zdev->list.next; l != &zcrypt_device_list; l = l->next) {
190 tmp = list_entry(l, struct zcrypt_device, list);
191 if ((tmp->request_count + 1) * tmp->speed_rating >
192 (zdev->request_count + 1) * zdev->speed_rating ||
193 tmp->speed_rating == 0)
194 break;
195 }
196 if (l == zdev->list.next)
197 return;
198 /* Move zdev before l */
1fbc9f46 199 list_move_tail(&zdev->list, l);
2dbc2418
MS
200}
201
202static void zcrypt_device_release(struct kref *kref)
203{
204 struct zcrypt_device *zdev =
205 container_of(kref, struct zcrypt_device, refcount);
206 zcrypt_device_free(zdev);
207}
208
209void zcrypt_device_get(struct zcrypt_device *zdev)
210{
211 kref_get(&zdev->refcount);
212}
213EXPORT_SYMBOL(zcrypt_device_get);
214
215int zcrypt_device_put(struct zcrypt_device *zdev)
216{
217 return kref_put(&zdev->refcount, zcrypt_device_release);
218}
219EXPORT_SYMBOL(zcrypt_device_put);
220
221struct zcrypt_device *zcrypt_device_alloc(size_t max_response_size)
222{
223 struct zcrypt_device *zdev;
224
225 zdev = kzalloc(sizeof(struct zcrypt_device), GFP_KERNEL);
226 if (!zdev)
227 return NULL;
228 zdev->reply.message = kmalloc(max_response_size, GFP_KERNEL);
229 if (!zdev->reply.message)
230 goto out_free;
231 zdev->reply.length = max_response_size;
232 spin_lock_init(&zdev->lock);
233 INIT_LIST_HEAD(&zdev->list);
dabecb29 234 zdev->dbf_area = zcrypt_dbf_devices;
2dbc2418
MS
235 return zdev;
236
237out_free:
238 kfree(zdev);
239 return NULL;
240}
241EXPORT_SYMBOL(zcrypt_device_alloc);
242
243void zcrypt_device_free(struct zcrypt_device *zdev)
244{
245 kfree(zdev->reply.message);
246 kfree(zdev);
247}
248EXPORT_SYMBOL(zcrypt_device_free);
249
250/**
1749a81d
FB
251 * zcrypt_device_register() - Register a crypto device.
252 * @zdev: Pointer to a crypto device
253 *
254 * Register a crypto device. Returns 0 if successful.
2dbc2418
MS
255 */
256int zcrypt_device_register(struct zcrypt_device *zdev)
257{
258 int rc;
259
5e55a488
HD
260 if (!zdev->ops)
261 return -ENODEV;
2dbc2418
MS
262 rc = sysfs_create_group(&zdev->ap_dev->device.kobj,
263 &zcrypt_device_attr_group);
264 if (rc)
265 goto out;
266 get_device(&zdev->ap_dev->device);
267 kref_init(&zdev->refcount);
268 spin_lock_bh(&zcrypt_device_lock);
269 zdev->online = 1; /* New devices are online by default. */
dabecb29
HD
270 ZCRYPT_DBF_DEV(DBF_INFO, zdev, "dev%04xo%dreg", zdev->ap_dev->qid,
271 zdev->online);
2dbc2418
MS
272 list_add_tail(&zdev->list, &zcrypt_device_list);
273 __zcrypt_increase_preference(zdev);
274 zcrypt_device_count++;
275 spin_unlock_bh(&zcrypt_device_lock);
2f7c8bd6
RW
276 if (zdev->ops->rng) {
277 rc = zcrypt_rng_device_add();
278 if (rc)
279 goto out_unregister;
280 }
281 return 0;
282
283out_unregister:
284 spin_lock_bh(&zcrypt_device_lock);
285 zcrypt_device_count--;
286 list_del_init(&zdev->list);
287 spin_unlock_bh(&zcrypt_device_lock);
288 sysfs_remove_group(&zdev->ap_dev->device.kobj,
289 &zcrypt_device_attr_group);
290 put_device(&zdev->ap_dev->device);
291 zcrypt_device_put(zdev);
2dbc2418
MS
292out:
293 return rc;
294}
295EXPORT_SYMBOL(zcrypt_device_register);
296
297/**
1749a81d
FB
298 * zcrypt_device_unregister(): Unregister a crypto device.
299 * @zdev: Pointer to crypto device
300 *
2dbc2418
MS
301 * Unregister a crypto device.
302 */
303void zcrypt_device_unregister(struct zcrypt_device *zdev)
304{
2f7c8bd6
RW
305 if (zdev->ops->rng)
306 zcrypt_rng_device_remove();
2dbc2418
MS
307 spin_lock_bh(&zcrypt_device_lock);
308 zcrypt_device_count--;
309 list_del_init(&zdev->list);
310 spin_unlock_bh(&zcrypt_device_lock);
311 sysfs_remove_group(&zdev->ap_dev->device.kobj,
312 &zcrypt_device_attr_group);
313 put_device(&zdev->ap_dev->device);
314 zcrypt_device_put(zdev);
315}
316EXPORT_SYMBOL(zcrypt_device_unregister);
317
5e55a488
HD
318void zcrypt_msgtype_register(struct zcrypt_ops *zops)
319{
320 if (zops->owner) {
321 spin_lock_bh(&zcrypt_ops_list_lock);
322 list_add_tail(&zops->list, &zcrypt_ops_list);
323 spin_unlock_bh(&zcrypt_ops_list_lock);
324 }
325}
326EXPORT_SYMBOL(zcrypt_msgtype_register);
327
328void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
329{
330 spin_lock_bh(&zcrypt_ops_list_lock);
331 list_del_init(&zops->list);
332 spin_unlock_bh(&zcrypt_ops_list_lock);
333}
334EXPORT_SYMBOL(zcrypt_msgtype_unregister);
335
336static inline
337struct zcrypt_ops *__ops_lookup(unsigned char *name, int variant)
338{
339 struct zcrypt_ops *zops;
340 int found = 0;
341
342 spin_lock_bh(&zcrypt_ops_list_lock);
343 list_for_each_entry(zops, &zcrypt_ops_list, list) {
344 if ((zops->variant == variant) &&
345 (!strncmp(zops->owner->name, name, MODULE_NAME_LEN))) {
346 found = 1;
347 break;
348 }
349 }
46b05c7b
IT
350 if (!found || !try_module_get(zops->owner))
351 zops = NULL;
352
5e55a488
HD
353 spin_unlock_bh(&zcrypt_ops_list_lock);
354
5e55a488
HD
355 return zops;
356}
357
358struct zcrypt_ops *zcrypt_msgtype_request(unsigned char *name, int variant)
359{
360 struct zcrypt_ops *zops = NULL;
361
362 zops = __ops_lookup(name, variant);
363 if (!zops) {
ef283688 364 request_module("%s", name);
5e55a488
HD
365 zops = __ops_lookup(name, variant);
366 }
5e55a488
HD
367 return zops;
368}
369EXPORT_SYMBOL(zcrypt_msgtype_request);
370
371void zcrypt_msgtype_release(struct zcrypt_ops *zops)
372{
373 if (zops)
374 module_put(zops->owner);
375}
376EXPORT_SYMBOL(zcrypt_msgtype_release);
377
2dbc2418 378/**
1749a81d
FB
379 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
380 *
381 * This function is not supported beyond zcrypt 1.3.1.
2dbc2418
MS
382 */
383static ssize_t zcrypt_read(struct file *filp, char __user *buf,
384 size_t count, loff_t *f_pos)
385{
386 return -EPERM;
387}
388
389/**
1749a81d
FB
390 * zcrypt_write(): Not allowed.
391 *
2dbc2418
MS
392 * Write is is not allowed
393 */
394static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
395 size_t count, loff_t *f_pos)
396{
397 return -EPERM;
398}
399
400/**
1749a81d
FB
401 * zcrypt_open(): Count number of users.
402 *
403 * Device open function to count number of users.
2dbc2418
MS
404 */
405static int zcrypt_open(struct inode *inode, struct file *filp)
406{
407 atomic_inc(&zcrypt_open_count);
58ea91c0 408 return nonseekable_open(inode, filp);
2dbc2418
MS
409}
410
1749a81d
FB
411/**
412 * zcrypt_release(): Count number of users.
413 *
414 * Device close function to count number of users.
415 */
2dbc2418
MS
416static int zcrypt_release(struct inode *inode, struct file *filp)
417{
418 atomic_dec(&zcrypt_open_count);
419 return 0;
420}
421
1749a81d 422/*
2dbc2418
MS
423 * zcrypt ioctls.
424 */
425static long zcrypt_rsa_modexpo(struct ica_rsa_modexpo *mex)
426{
427 struct zcrypt_device *zdev;
428 int rc;
429
430 if (mex->outputdatalength < mex->inputdatalength)
431 return -EINVAL;
1749a81d 432 /*
2dbc2418
MS
433 * As long as outputdatalength is big enough, we can set the
434 * outputdatalength equal to the inputdatalength, since that is the
435 * number of bytes we will copy in any case
436 */
437 mex->outputdatalength = mex->inputdatalength;
438
439 spin_lock_bh(&zcrypt_device_lock);
440 list_for_each_entry(zdev, &zcrypt_device_list, list) {
441 if (!zdev->online ||
442 !zdev->ops->rsa_modexpo ||
443 zdev->min_mod_size > mex->inputdatalength ||
444 zdev->max_mod_size < mex->inputdatalength)
445 continue;
446 zcrypt_device_get(zdev);
447 get_device(&zdev->ap_dev->device);
448 zdev->request_count++;
449 __zcrypt_decrease_preference(zdev);
2dbc2418 450 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
43a867a2 451 spin_unlock_bh(&zcrypt_device_lock);
2dbc2418 452 rc = zdev->ops->rsa_modexpo(zdev, mex);
43a867a2 453 spin_lock_bh(&zcrypt_device_lock);
2dbc2418
MS
454 module_put(zdev->ap_dev->drv->driver.owner);
455 }
456 else
457 rc = -EAGAIN;
2dbc2418
MS
458 zdev->request_count--;
459 __zcrypt_increase_preference(zdev);
460 put_device(&zdev->ap_dev->device);
461 zcrypt_device_put(zdev);
462 spin_unlock_bh(&zcrypt_device_lock);
463 return rc;
464 }
465 spin_unlock_bh(&zcrypt_device_lock);
466 return -ENODEV;
467}
468
469static long zcrypt_rsa_crt(struct ica_rsa_modexpo_crt *crt)
470{
471 struct zcrypt_device *zdev;
472 unsigned long long z1, z2, z3;
473 int rc, copied;
474
475 if (crt->outputdatalength < crt->inputdatalength ||
476 (crt->inputdatalength & 1))
477 return -EINVAL;
1749a81d 478 /*
2dbc2418
MS
479 * As long as outputdatalength is big enough, we can set the
480 * outputdatalength equal to the inputdatalength, since that is the
481 * number of bytes we will copy in any case
482 */
483 crt->outputdatalength = crt->inputdatalength;
484
485 copied = 0;
486 restart:
487 spin_lock_bh(&zcrypt_device_lock);
488 list_for_each_entry(zdev, &zcrypt_device_list, list) {
489 if (!zdev->online ||
490 !zdev->ops->rsa_modexpo_crt ||
491 zdev->min_mod_size > crt->inputdatalength ||
492 zdev->max_mod_size < crt->inputdatalength)
493 continue;
494 if (zdev->short_crt && crt->inputdatalength > 240) {
1749a81d 495 /*
2dbc2418
MS
496 * Check inputdata for leading zeros for cards
497 * that can't handle np_prime, bp_key, or
498 * u_mult_inv > 128 bytes.
499 */
500 if (copied == 0) {
0648f565 501 unsigned int len;
2dbc2418 502 spin_unlock_bh(&zcrypt_device_lock);
078f8eca
FB
503 /* len is max 256 / 2 - 120 = 8
504 * For bigger device just assume len of leading
505 * 0s is 8 as stated in the requirements for
506 * ica_rsa_modexpo_crt struct in zcrypt.h.
507 */
508 if (crt->inputdatalength <= 256)
509 len = crt->inputdatalength / 2 - 120;
510 else
511 len = 8;
0648f565
HC
512 if (len > sizeof(z1))
513 return -EFAULT;
2dbc2418
MS
514 z1 = z2 = z3 = 0;
515 if (copy_from_user(&z1, crt->np_prime, len) ||
516 copy_from_user(&z2, crt->bp_key, len) ||
517 copy_from_user(&z3, crt->u_mult_inv, len))
518 return -EFAULT;
078f8eca 519 z1 = z2 = z3 = 0;
2dbc2418 520 copied = 1;
1749a81d 521 /*
2dbc2418
MS
522 * We have to restart device lookup -
523 * the device list may have changed by now.
524 */
525 goto restart;
526 }
527 if (z1 != 0ULL || z2 != 0ULL || z3 != 0ULL)
528 /* The device can't handle this request. */
529 continue;
530 }
531 zcrypt_device_get(zdev);
532 get_device(&zdev->ap_dev->device);
533 zdev->request_count++;
534 __zcrypt_decrease_preference(zdev);
2dbc2418 535 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
43a867a2 536 spin_unlock_bh(&zcrypt_device_lock);
2dbc2418 537 rc = zdev->ops->rsa_modexpo_crt(zdev, crt);
43a867a2 538 spin_lock_bh(&zcrypt_device_lock);
2dbc2418
MS
539 module_put(zdev->ap_dev->drv->driver.owner);
540 }
541 else
542 rc = -EAGAIN;
2dbc2418
MS
543 zdev->request_count--;
544 __zcrypt_increase_preference(zdev);
545 put_device(&zdev->ap_dev->device);
546 zcrypt_device_put(zdev);
547 spin_unlock_bh(&zcrypt_device_lock);
548 return rc;
549 }
550 spin_unlock_bh(&zcrypt_device_lock);
551 return -ENODEV;
552}
553
5432114b
RW
554static long zcrypt_send_cprb(struct ica_xcRB *xcRB)
555{
556 struct zcrypt_device *zdev;
557 int rc;
558
559 spin_lock_bh(&zcrypt_device_lock);
560 list_for_each_entry(zdev, &zcrypt_device_list, list) {
561 if (!zdev->online || !zdev->ops->send_cprb ||
91f3e3ea
IT
562 (zdev->ops->variant == MSGTYPE06_VARIANT_EP11) ||
563 (xcRB->user_defined != AUTOSELECT &&
564 AP_QID_DEVICE(zdev->ap_dev->qid) != xcRB->user_defined))
5432114b
RW
565 continue;
566 zcrypt_device_get(zdev);
567 get_device(&zdev->ap_dev->device);
568 zdev->request_count++;
569 __zcrypt_decrease_preference(zdev);
5432114b 570 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
43a867a2 571 spin_unlock_bh(&zcrypt_device_lock);
5432114b 572 rc = zdev->ops->send_cprb(zdev, xcRB);
43a867a2 573 spin_lock_bh(&zcrypt_device_lock);
5432114b
RW
574 module_put(zdev->ap_dev->drv->driver.owner);
575 }
576 else
577 rc = -EAGAIN;
5432114b
RW
578 zdev->request_count--;
579 __zcrypt_increase_preference(zdev);
580 put_device(&zdev->ap_dev->device);
581 zcrypt_device_put(zdev);
582 spin_unlock_bh(&zcrypt_device_lock);
583 return rc;
584 }
585 spin_unlock_bh(&zcrypt_device_lock);
586 return -ENODEV;
587}
588
91f3e3ea
IT
589struct ep11_target_dev_list {
590 unsigned short targets_num;
591 struct ep11_target_dev *targets;
592};
593
594static bool is_desired_ep11dev(unsigned int dev_qid,
595 struct ep11_target_dev_list dev_list)
596{
597 int n;
598
599 for (n = 0; n < dev_list.targets_num; n++, dev_list.targets++) {
600 if ((AP_QID_DEVICE(dev_qid) == dev_list.targets->ap_id) &&
601 (AP_QID_QUEUE(dev_qid) == dev_list.targets->dom_id)) {
602 return true;
603 }
604 }
605 return false;
606}
607
608static long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
609{
610 struct zcrypt_device *zdev;
611 bool autoselect = false;
612 int rc;
613 struct ep11_target_dev_list ep11_dev_list = {
614 .targets_num = 0x00,
615 .targets = NULL,
616 };
617
618 ep11_dev_list.targets_num = (unsigned short) xcrb->targets_num;
619
620 /* empty list indicates autoselect (all available targets) */
621 if (ep11_dev_list.targets_num == 0)
622 autoselect = true;
623 else {
624 ep11_dev_list.targets = kcalloc((unsigned short)
625 xcrb->targets_num,
626 sizeof(struct ep11_target_dev),
627 GFP_KERNEL);
628 if (!ep11_dev_list.targets)
629 return -ENOMEM;
630
631 if (copy_from_user(ep11_dev_list.targets,
ce1ce2f3
IT
632 (struct ep11_target_dev __force __user *)
633 xcrb->targets, xcrb->targets_num *
91f3e3ea
IT
634 sizeof(struct ep11_target_dev)))
635 return -EFAULT;
636 }
637
638 spin_lock_bh(&zcrypt_device_lock);
639 list_for_each_entry(zdev, &zcrypt_device_list, list) {
640 /* check if device is eligible */
641 if (!zdev->online ||
642 zdev->ops->variant != MSGTYPE06_VARIANT_EP11)
643 continue;
644
645 /* check if device is selected as valid target */
646 if (!is_desired_ep11dev(zdev->ap_dev->qid, ep11_dev_list) &&
647 !autoselect)
648 continue;
649
650 zcrypt_device_get(zdev);
651 get_device(&zdev->ap_dev->device);
652 zdev->request_count++;
653 __zcrypt_decrease_preference(zdev);
654 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
655 spin_unlock_bh(&zcrypt_device_lock);
656 rc = zdev->ops->send_ep11_cprb(zdev, xcrb);
657 spin_lock_bh(&zcrypt_device_lock);
658 module_put(zdev->ap_dev->drv->driver.owner);
659 } else {
660 rc = -EAGAIN;
661 }
662 zdev->request_count--;
663 __zcrypt_increase_preference(zdev);
664 put_device(&zdev->ap_dev->device);
665 zcrypt_device_put(zdev);
666 spin_unlock_bh(&zcrypt_device_lock);
667 return rc;
668 }
669 spin_unlock_bh(&zcrypt_device_lock);
670 return -ENODEV;
671}
672
2f7c8bd6
RW
673static long zcrypt_rng(char *buffer)
674{
675 struct zcrypt_device *zdev;
676 int rc;
677
678 spin_lock_bh(&zcrypt_device_lock);
679 list_for_each_entry(zdev, &zcrypt_device_list, list) {
680 if (!zdev->online || !zdev->ops->rng)
681 continue;
682 zcrypt_device_get(zdev);
683 get_device(&zdev->ap_dev->device);
684 zdev->request_count++;
685 __zcrypt_decrease_preference(zdev);
686 if (try_module_get(zdev->ap_dev->drv->driver.owner)) {
687 spin_unlock_bh(&zcrypt_device_lock);
688 rc = zdev->ops->rng(zdev, buffer);
689 spin_lock_bh(&zcrypt_device_lock);
690 module_put(zdev->ap_dev->drv->driver.owner);
691 } else
692 rc = -EAGAIN;
693 zdev->request_count--;
694 __zcrypt_increase_preference(zdev);
695 put_device(&zdev->ap_dev->device);
696 zcrypt_device_put(zdev);
697 spin_unlock_bh(&zcrypt_device_lock);
698 return rc;
699 }
700 spin_unlock_bh(&zcrypt_device_lock);
701 return -ENODEV;
702}
703
2dbc2418
MS
704static void zcrypt_status_mask(char status[AP_DEVICES])
705{
706 struct zcrypt_device *zdev;
707
708 memset(status, 0, sizeof(char) * AP_DEVICES);
709 spin_lock_bh(&zcrypt_device_lock);
710 list_for_each_entry(zdev, &zcrypt_device_list, list)
711 status[AP_QID_DEVICE(zdev->ap_dev->qid)] =
712 zdev->online ? zdev->user_space_type : 0x0d;
713 spin_unlock_bh(&zcrypt_device_lock);
714}
715
716static void zcrypt_qdepth_mask(char qdepth[AP_DEVICES])
717{
718 struct zcrypt_device *zdev;
719
720 memset(qdepth, 0, sizeof(char) * AP_DEVICES);
721 spin_lock_bh(&zcrypt_device_lock);
722 list_for_each_entry(zdev, &zcrypt_device_list, list) {
723 spin_lock(&zdev->ap_dev->lock);
724 qdepth[AP_QID_DEVICE(zdev->ap_dev->qid)] =
725 zdev->ap_dev->pendingq_count +
726 zdev->ap_dev->requestq_count;
727 spin_unlock(&zdev->ap_dev->lock);
728 }
729 spin_unlock_bh(&zcrypt_device_lock);
730}
731
732static void zcrypt_perdev_reqcnt(int reqcnt[AP_DEVICES])
733{
734 struct zcrypt_device *zdev;
735
736 memset(reqcnt, 0, sizeof(int) * AP_DEVICES);
737 spin_lock_bh(&zcrypt_device_lock);
738 list_for_each_entry(zdev, &zcrypt_device_list, list) {
739 spin_lock(&zdev->ap_dev->lock);
740 reqcnt[AP_QID_DEVICE(zdev->ap_dev->qid)] =
741 zdev->ap_dev->total_request_count;
742 spin_unlock(&zdev->ap_dev->lock);
743 }
744 spin_unlock_bh(&zcrypt_device_lock);
745}
746
747static int zcrypt_pendingq_count(void)
748{
749 struct zcrypt_device *zdev;
750 int pendingq_count = 0;
751
752 spin_lock_bh(&zcrypt_device_lock);
753 list_for_each_entry(zdev, &zcrypt_device_list, list) {
754 spin_lock(&zdev->ap_dev->lock);
755 pendingq_count += zdev->ap_dev->pendingq_count;
756 spin_unlock(&zdev->ap_dev->lock);
757 }
758 spin_unlock_bh(&zcrypt_device_lock);
759 return pendingq_count;
760}
761
762static int zcrypt_requestq_count(void)
763{
764 struct zcrypt_device *zdev;
765 int requestq_count = 0;
766
767 spin_lock_bh(&zcrypt_device_lock);
768 list_for_each_entry(zdev, &zcrypt_device_list, list) {
769 spin_lock(&zdev->ap_dev->lock);
770 requestq_count += zdev->ap_dev->requestq_count;
771 spin_unlock(&zdev->ap_dev->lock);
772 }
773 spin_unlock_bh(&zcrypt_device_lock);
774 return requestq_count;
775}
776
777static int zcrypt_count_type(int type)
778{
779 struct zcrypt_device *zdev;
780 int device_count = 0;
781
782 spin_lock_bh(&zcrypt_device_lock);
783 list_for_each_entry(zdev, &zcrypt_device_list, list)
784 if (zdev->user_space_type == type)
785 device_count++;
786 spin_unlock_bh(&zcrypt_device_lock);
787 return device_count;
788}
789
790/**
1749a81d
FB
791 * zcrypt_ica_status(): Old, depracted combi status call.
792 *
2dbc2418
MS
793 * Old, deprecated combi status call.
794 */
795static long zcrypt_ica_status(struct file *filp, unsigned long arg)
796{
797 struct ica_z90_status *pstat;
798 int ret;
799
800 pstat = kzalloc(sizeof(*pstat), GFP_KERNEL);
801 if (!pstat)
802 return -ENOMEM;
803 pstat->totalcount = zcrypt_device_count;
804 pstat->leedslitecount = zcrypt_count_type(ZCRYPT_PCICA);
805 pstat->leeds2count = zcrypt_count_type(ZCRYPT_PCICC);
806 pstat->requestqWaitCount = zcrypt_requestq_count();
807 pstat->pendingqWaitCount = zcrypt_pendingq_count();
808 pstat->totalOpenCount = atomic_read(&zcrypt_open_count);
809 pstat->cryptoDomain = ap_domain_index;
810 zcrypt_status_mask(pstat->status);
811 zcrypt_qdepth_mask(pstat->qdepth);
812 ret = 0;
813 if (copy_to_user((void __user *) arg, pstat, sizeof(*pstat)))
814 ret = -EFAULT;
815 kfree(pstat);
816 return ret;
817}
818
819static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
820 unsigned long arg)
821{
822 int rc;
823
824 switch (cmd) {
825 case ICARSAMODEXPO: {
826 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
827 struct ica_rsa_modexpo mex;
828 if (copy_from_user(&mex, umex, sizeof(mex)))
829 return -EFAULT;
830 do {
831 rc = zcrypt_rsa_modexpo(&mex);
832 } while (rc == -EAGAIN);
dabecb29
HD
833 /* on failure: retry once again after a requested rescan */
834 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
835 do {
836 rc = zcrypt_rsa_modexpo(&mex);
837 } while (rc == -EAGAIN);
2dbc2418
MS
838 if (rc)
839 return rc;
840 return put_user(mex.outputdatalength, &umex->outputdatalength);
841 }
842 case ICARSACRT: {
843 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
844 struct ica_rsa_modexpo_crt crt;
845 if (copy_from_user(&crt, ucrt, sizeof(crt)))
846 return -EFAULT;
847 do {
848 rc = zcrypt_rsa_crt(&crt);
849 } while (rc == -EAGAIN);
dabecb29
HD
850 /* on failure: retry once again after a requested rescan */
851 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
852 do {
853 rc = zcrypt_rsa_crt(&crt);
854 } while (rc == -EAGAIN);
2dbc2418
MS
855 if (rc)
856 return rc;
857 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
858 }
5432114b
RW
859 case ZSECSENDCPRB: {
860 struct ica_xcRB __user *uxcRB = (void __user *) arg;
861 struct ica_xcRB xcRB;
862 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
863 return -EFAULT;
864 do {
865 rc = zcrypt_send_cprb(&xcRB);
866 } while (rc == -EAGAIN);
dabecb29
HD
867 /* on failure: retry once again after a requested rescan */
868 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
869 do {
870 rc = zcrypt_send_cprb(&xcRB);
871 } while (rc == -EAGAIN);
5432114b
RW
872 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
873 return -EFAULT;
874 return rc;
875 }
91f3e3ea
IT
876 case ZSENDEP11CPRB: {
877 struct ep11_urb __user *uxcrb = (void __user *)arg;
878 struct ep11_urb xcrb;
879 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
880 return -EFAULT;
881 do {
882 rc = zcrypt_send_ep11_cprb(&xcrb);
883 } while (rc == -EAGAIN);
884 /* on failure: retry once again after a requested rescan */
885 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
886 do {
887 rc = zcrypt_send_ep11_cprb(&xcrb);
888 } while (rc == -EAGAIN);
889 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
890 return -EFAULT;
891 return rc;
892 }
2dbc2418
MS
893 case Z90STAT_STATUS_MASK: {
894 char status[AP_DEVICES];
895 zcrypt_status_mask(status);
896 if (copy_to_user((char __user *) arg, status,
897 sizeof(char) * AP_DEVICES))
898 return -EFAULT;
899 return 0;
900 }
901 case Z90STAT_QDEPTH_MASK: {
902 char qdepth[AP_DEVICES];
903 zcrypt_qdepth_mask(qdepth);
904 if (copy_to_user((char __user *) arg, qdepth,
905 sizeof(char) * AP_DEVICES))
906 return -EFAULT;
907 return 0;
908 }
909 case Z90STAT_PERDEV_REQCNT: {
910 int reqcnt[AP_DEVICES];
911 zcrypt_perdev_reqcnt(reqcnt);
912 if (copy_to_user((int __user *) arg, reqcnt,
913 sizeof(int) * AP_DEVICES))
914 return -EFAULT;
915 return 0;
916 }
917 case Z90STAT_REQUESTQ_COUNT:
918 return put_user(zcrypt_requestq_count(), (int __user *) arg);
919 case Z90STAT_PENDINGQ_COUNT:
920 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
921 case Z90STAT_TOTALOPEN_COUNT:
922 return put_user(atomic_read(&zcrypt_open_count),
923 (int __user *) arg);
924 case Z90STAT_DOMAIN_INDEX:
925 return put_user(ap_domain_index, (int __user *) arg);
1749a81d 926 /*
2dbc2418
MS
927 * Deprecated ioctls. Don't add another device count ioctl,
928 * you can count them yourself in the user space with the
929 * output of the Z90STAT_STATUS_MASK ioctl.
930 */
931 case ICAZ90STATUS:
932 return zcrypt_ica_status(filp, arg);
933 case Z90STAT_TOTALCOUNT:
934 return put_user(zcrypt_device_count, (int __user *) arg);
935 case Z90STAT_PCICACOUNT:
936 return put_user(zcrypt_count_type(ZCRYPT_PCICA),
937 (int __user *) arg);
938 case Z90STAT_PCICCCOUNT:
939 return put_user(zcrypt_count_type(ZCRYPT_PCICC),
940 (int __user *) arg);
941 case Z90STAT_PCIXCCMCL2COUNT:
942 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2),
943 (int __user *) arg);
944 case Z90STAT_PCIXCCMCL3COUNT:
945 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
946 (int __user *) arg);
947 case Z90STAT_PCIXCCCOUNT:
948 return put_user(zcrypt_count_type(ZCRYPT_PCIXCC_MCL2) +
949 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3),
950 (int __user *) arg);
951 case Z90STAT_CEX2CCOUNT:
952 return put_user(zcrypt_count_type(ZCRYPT_CEX2C),
953 (int __user *) arg);
954 case Z90STAT_CEX2ACOUNT:
955 return put_user(zcrypt_count_type(ZCRYPT_CEX2A),
956 (int __user *) arg);
957 default:
958 /* unknown ioctl number */
959 return -ENOIOCTLCMD;
960 }
961}
962
963#ifdef CONFIG_COMPAT
1749a81d 964/*
2dbc2418
MS
965 * ioctl32 conversion routines
966 */
967struct compat_ica_rsa_modexpo {
968 compat_uptr_t inputdata;
969 unsigned int inputdatalength;
970 compat_uptr_t outputdata;
971 unsigned int outputdatalength;
972 compat_uptr_t b_key;
973 compat_uptr_t n_modulus;
974};
975
976static long trans_modexpo32(struct file *filp, unsigned int cmd,
977 unsigned long arg)
978{
979 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
980 struct compat_ica_rsa_modexpo mex32;
981 struct ica_rsa_modexpo mex64;
982 long rc;
983
984 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
985 return -EFAULT;
986 mex64.inputdata = compat_ptr(mex32.inputdata);
987 mex64.inputdatalength = mex32.inputdatalength;
988 mex64.outputdata = compat_ptr(mex32.outputdata);
989 mex64.outputdatalength = mex32.outputdatalength;
990 mex64.b_key = compat_ptr(mex32.b_key);
991 mex64.n_modulus = compat_ptr(mex32.n_modulus);
992 do {
993 rc = zcrypt_rsa_modexpo(&mex64);
994 } while (rc == -EAGAIN);
dabecb29
HD
995 /* on failure: retry once again after a requested rescan */
996 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
997 do {
998 rc = zcrypt_rsa_modexpo(&mex64);
999 } while (rc == -EAGAIN);
1000 if (rc)
1001 return rc;
1002 return put_user(mex64.outputdatalength,
1003 &umex32->outputdatalength);
2dbc2418
MS
1004}
1005
1006struct compat_ica_rsa_modexpo_crt {
1007 compat_uptr_t inputdata;
1008 unsigned int inputdatalength;
1009 compat_uptr_t outputdata;
1010 unsigned int outputdatalength;
1011 compat_uptr_t bp_key;
1012 compat_uptr_t bq_key;
1013 compat_uptr_t np_prime;
1014 compat_uptr_t nq_prime;
1015 compat_uptr_t u_mult_inv;
1016};
1017
1018static long trans_modexpo_crt32(struct file *filp, unsigned int cmd,
1019 unsigned long arg)
1020{
1021 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1022 struct compat_ica_rsa_modexpo_crt crt32;
1023 struct ica_rsa_modexpo_crt crt64;
1024 long rc;
1025
1026 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1027 return -EFAULT;
1028 crt64.inputdata = compat_ptr(crt32.inputdata);
1029 crt64.inputdatalength = crt32.inputdatalength;
1030 crt64.outputdata= compat_ptr(crt32.outputdata);
1031 crt64.outputdatalength = crt32.outputdatalength;
1032 crt64.bp_key = compat_ptr(crt32.bp_key);
1033 crt64.bq_key = compat_ptr(crt32.bq_key);
1034 crt64.np_prime = compat_ptr(crt32.np_prime);
1035 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1036 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1037 do {
1038 rc = zcrypt_rsa_crt(&crt64);
1039 } while (rc == -EAGAIN);
dabecb29
HD
1040 /* on failure: retry once again after a requested rescan */
1041 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1042 do {
1043 rc = zcrypt_rsa_crt(&crt64);
1044 } while (rc == -EAGAIN);
1045 if (rc)
1046 return rc;
1047 return put_user(crt64.outputdatalength,
1048 &ucrt32->outputdatalength);
2dbc2418
MS
1049}
1050
5432114b
RW
1051struct compat_ica_xcRB {
1052 unsigned short agent_ID;
1053 unsigned int user_defined;
1054 unsigned short request_ID;
1055 unsigned int request_control_blk_length;
1056 unsigned char padding1[16 - sizeof (compat_uptr_t)];
1057 compat_uptr_t request_control_blk_addr;
1058 unsigned int request_data_length;
1059 char padding2[16 - sizeof (compat_uptr_t)];
1060 compat_uptr_t request_data_address;
1061 unsigned int reply_control_blk_length;
1062 char padding3[16 - sizeof (compat_uptr_t)];
1063 compat_uptr_t reply_control_blk_addr;
1064 unsigned int reply_data_length;
1065 char padding4[16 - sizeof (compat_uptr_t)];
1066 compat_uptr_t reply_data_addr;
1067 unsigned short priority_window;
1068 unsigned int status;
1069} __attribute__((packed));
1070
1071static long trans_xcRB32(struct file *filp, unsigned int cmd,
1072 unsigned long arg)
1073{
1074 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1075 struct compat_ica_xcRB xcRB32;
1076 struct ica_xcRB xcRB64;
1077 long rc;
1078
1079 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1080 return -EFAULT;
1081 xcRB64.agent_ID = xcRB32.agent_ID;
1082 xcRB64.user_defined = xcRB32.user_defined;
1083 xcRB64.request_ID = xcRB32.request_ID;
1084 xcRB64.request_control_blk_length =
1085 xcRB32.request_control_blk_length;
1086 xcRB64.request_control_blk_addr =
1087 compat_ptr(xcRB32.request_control_blk_addr);
1088 xcRB64.request_data_length =
1089 xcRB32.request_data_length;
1090 xcRB64.request_data_address =
1091 compat_ptr(xcRB32.request_data_address);
1092 xcRB64.reply_control_blk_length =
1093 xcRB32.reply_control_blk_length;
1094 xcRB64.reply_control_blk_addr =
1095 compat_ptr(xcRB32.reply_control_blk_addr);
1096 xcRB64.reply_data_length = xcRB32.reply_data_length;
1097 xcRB64.reply_data_addr =
1098 compat_ptr(xcRB32.reply_data_addr);
1099 xcRB64.priority_window = xcRB32.priority_window;
1100 xcRB64.status = xcRB32.status;
1101 do {
1102 rc = zcrypt_send_cprb(&xcRB64);
1103 } while (rc == -EAGAIN);
dabecb29
HD
1104 /* on failure: retry once again after a requested rescan */
1105 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1106 do {
1107 rc = zcrypt_send_cprb(&xcRB64);
1108 } while (rc == -EAGAIN);
5432114b
RW
1109 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1110 xcRB32.reply_data_length = xcRB64.reply_data_length;
1111 xcRB32.status = xcRB64.status;
1112 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1113 return -EFAULT;
1114 return rc;
1115}
1116
2b67fc46 1117static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
2dbc2418
MS
1118 unsigned long arg)
1119{
1120 if (cmd == ICARSAMODEXPO)
1121 return trans_modexpo32(filp, cmd, arg);
1122 if (cmd == ICARSACRT)
1123 return trans_modexpo_crt32(filp, cmd, arg);
5432114b
RW
1124 if (cmd == ZSECSENDCPRB)
1125 return trans_xcRB32(filp, cmd, arg);
2dbc2418
MS
1126 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1127}
1128#endif
1129
1749a81d 1130/*
2dbc2418
MS
1131 * Misc device file operations.
1132 */
d54b1fdb 1133static const struct file_operations zcrypt_fops = {
2dbc2418
MS
1134 .owner = THIS_MODULE,
1135 .read = zcrypt_read,
1136 .write = zcrypt_write,
1137 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1138#ifdef CONFIG_COMPAT
1139 .compat_ioctl = zcrypt_compat_ioctl,
1140#endif
1141 .open = zcrypt_open,
6038f373
AB
1142 .release = zcrypt_release,
1143 .llseek = no_llseek,
2dbc2418
MS
1144};
1145
1749a81d 1146/*
2dbc2418
MS
1147 * Misc device.
1148 */
1149static struct miscdevice zcrypt_misc_device = {
1150 .minor = MISC_DYNAMIC_MINOR,
1151 .name = "z90crypt",
1152 .fops = &zcrypt_fops,
1153};
1154
1749a81d 1155/*
2dbc2418
MS
1156 * Deprecated /proc entry support.
1157 */
1158static struct proc_dir_entry *zcrypt_entry;
1159
34b9243a 1160static void sprintcl(struct seq_file *m, unsigned char *addr, unsigned int len)
2dbc2418 1161{
34b9243a 1162 int i;
2dbc2418 1163
2dbc2418 1164 for (i = 0; i < len; i++)
34b9243a
AD
1165 seq_printf(m, "%01x", (unsigned int) addr[i]);
1166 seq_putc(m, ' ');
2dbc2418
MS
1167}
1168
34b9243a 1169static void sprintrw(struct seq_file *m, unsigned char *addr, unsigned int len)
2dbc2418 1170{
34b9243a 1171 int inl, c, cx;
2dbc2418 1172
34b9243a 1173 seq_printf(m, " ");
2dbc2418
MS
1174 inl = 0;
1175 for (c = 0; c < (len / 16); c++) {
34b9243a 1176 sprintcl(m, addr+inl, 16);
2dbc2418
MS
1177 inl += 16;
1178 }
1179 cx = len%16;
1180 if (cx) {
34b9243a 1181 sprintcl(m, addr+inl, cx);
2dbc2418
MS
1182 inl += cx;
1183 }
34b9243a 1184 seq_putc(m, '\n');
2dbc2418
MS
1185}
1186
34b9243a
AD
1187static void sprinthx(unsigned char *title, struct seq_file *m,
1188 unsigned char *addr, unsigned int len)
2dbc2418 1189{
34b9243a 1190 int inl, r, rx;
2dbc2418 1191
34b9243a 1192 seq_printf(m, "\n%s\n", title);
2dbc2418
MS
1193 inl = 0;
1194 for (r = 0; r < (len / 64); r++) {
34b9243a 1195 sprintrw(m, addr+inl, 64);
2dbc2418
MS
1196 inl += 64;
1197 }
1198 rx = len % 64;
1199 if (rx) {
34b9243a 1200 sprintrw(m, addr+inl, rx);
2dbc2418
MS
1201 inl += rx;
1202 }
34b9243a 1203 seq_putc(m, '\n');
2dbc2418
MS
1204}
1205
34b9243a
AD
1206static void sprinthx4(unsigned char *title, struct seq_file *m,
1207 unsigned int *array, unsigned int len)
2dbc2418 1208{
34b9243a 1209 int r;
2dbc2418 1210
34b9243a 1211 seq_printf(m, "\n%s\n", title);
2dbc2418
MS
1212 for (r = 0; r < len; r++) {
1213 if ((r % 8) == 0)
34b9243a
AD
1214 seq_printf(m, " ");
1215 seq_printf(m, "%08X ", array[r]);
2dbc2418 1216 if ((r % 8) == 7)
34b9243a 1217 seq_putc(m, '\n');
2dbc2418 1218 }
34b9243a 1219 seq_putc(m, '\n');
2dbc2418
MS
1220}
1221
34b9243a 1222static int zcrypt_proc_show(struct seq_file *m, void *v)
2dbc2418 1223{
34b9243a
AD
1224 char workarea[sizeof(int) * AP_DEVICES];
1225
1226 seq_printf(m, "\nzcrypt version: %d.%d.%d\n",
1227 ZCRYPT_VERSION, ZCRYPT_RELEASE, ZCRYPT_VARIANT);
1228 seq_printf(m, "Cryptographic domain: %d\n", ap_domain_index);
1229 seq_printf(m, "Total device count: %d\n", zcrypt_device_count);
1230 seq_printf(m, "PCICA count: %d\n", zcrypt_count_type(ZCRYPT_PCICA));
1231 seq_printf(m, "PCICC count: %d\n", zcrypt_count_type(ZCRYPT_PCICC));
1232 seq_printf(m, "PCIXCC MCL2 count: %d\n",
1233 zcrypt_count_type(ZCRYPT_PCIXCC_MCL2));
1234 seq_printf(m, "PCIXCC MCL3 count: %d\n",
1235 zcrypt_count_type(ZCRYPT_PCIXCC_MCL3));
1236 seq_printf(m, "CEX2C count: %d\n", zcrypt_count_type(ZCRYPT_CEX2C));
1237 seq_printf(m, "CEX2A count: %d\n", zcrypt_count_type(ZCRYPT_CEX2A));
1238 seq_printf(m, "CEX3C count: %d\n", zcrypt_count_type(ZCRYPT_CEX3C));
1239 seq_printf(m, "CEX3A count: %d\n", zcrypt_count_type(ZCRYPT_CEX3A));
1240 seq_printf(m, "requestq count: %d\n", zcrypt_requestq_count());
1241 seq_printf(m, "pendingq count: %d\n", zcrypt_pendingq_count());
1242 seq_printf(m, "Total open handles: %d\n\n",
1243 atomic_read(&zcrypt_open_count));
2dbc2418 1244 zcrypt_status_mask(workarea);
34b9243a
AD
1245 sprinthx("Online devices: 1=PCICA 2=PCICC 3=PCIXCC(MCL2) "
1246 "4=PCIXCC(MCL3) 5=CEX2C 6=CEX2A 7=CEX3C 8=CEX3A",
1247 m, workarea, AP_DEVICES);
2dbc2418 1248 zcrypt_qdepth_mask(workarea);
34b9243a 1249 sprinthx("Waiting work element counts", m, workarea, AP_DEVICES);
2b67fc46 1250 zcrypt_perdev_reqcnt((int *) workarea);
34b9243a
AD
1251 sprinthx4("Per-device successfully completed request counts",
1252 m, (unsigned int *) workarea, AP_DEVICES);
1253 return 0;
1254}
1255
1256static int zcrypt_proc_open(struct inode *inode, struct file *file)
1257{
1258 return single_open(file, zcrypt_proc_show, NULL);
2dbc2418
MS
1259}
1260
1261static void zcrypt_disable_card(int index)
1262{
1263 struct zcrypt_device *zdev;
1264
1265 spin_lock_bh(&zcrypt_device_lock);
1266 list_for_each_entry(zdev, &zcrypt_device_list, list)
1267 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1268 zdev->online = 0;
1269 ap_flush_queue(zdev->ap_dev);
1270 break;
1271 }
1272 spin_unlock_bh(&zcrypt_device_lock);
1273}
1274
1275static void zcrypt_enable_card(int index)
1276{
1277 struct zcrypt_device *zdev;
1278
1279 spin_lock_bh(&zcrypt_device_lock);
1280 list_for_each_entry(zdev, &zcrypt_device_list, list)
1281 if (AP_QID_DEVICE(zdev->ap_dev->qid) == index) {
1282 zdev->online = 1;
1283 break;
1284 }
1285 spin_unlock_bh(&zcrypt_device_lock);
1286}
1287
34b9243a
AD
1288static ssize_t zcrypt_proc_write(struct file *file, const char __user *buffer,
1289 size_t count, loff_t *pos)
2dbc2418
MS
1290{
1291 unsigned char *lbuf, *ptr;
34b9243a 1292 size_t local_count;
2dbc2418
MS
1293 int j;
1294
1295 if (count <= 0)
1296 return 0;
1297
1298#define LBUFSIZE 1200UL
1299 lbuf = kmalloc(LBUFSIZE, GFP_KERNEL);
1a89dd8f 1300 if (!lbuf)
2dbc2418 1301 return 0;
2dbc2418
MS
1302
1303 local_count = min(LBUFSIZE - 1, count);
1304 if (copy_from_user(lbuf, buffer, local_count) != 0) {
1305 kfree(lbuf);
1306 return -EFAULT;
1307 }
1308 lbuf[local_count] = '\0';
1309
1310 ptr = strstr(lbuf, "Online devices");
1a89dd8f 1311 if (!ptr)
2dbc2418 1312 goto out;
2dbc2418 1313 ptr = strstr(ptr, "\n");
1a89dd8f 1314 if (!ptr)
2dbc2418 1315 goto out;
2dbc2418
MS
1316 ptr++;
1317
1a89dd8f 1318 if (strstr(ptr, "Waiting work element counts") == NULL)
2dbc2418 1319 goto out;
2dbc2418
MS
1320
1321 for (j = 0; j < 64 && *ptr; ptr++) {
1749a81d 1322 /*
2dbc2418
MS
1323 * '0' for no device, '1' for PCICA, '2' for PCICC,
1324 * '3' for PCIXCC_MCL2, '4' for PCIXCC_MCL3,
1325 * '5' for CEX2C and '6' for CEX2A'
ffda4f71 1326 * '7' for CEX3C and '8' for CEX3A
2dbc2418 1327 */
ffda4f71 1328 if (*ptr >= '0' && *ptr <= '8')
2dbc2418
MS
1329 j++;
1330 else if (*ptr == 'd' || *ptr == 'D')
1331 zcrypt_disable_card(j++);
1332 else if (*ptr == 'e' || *ptr == 'E')
1333 zcrypt_enable_card(j++);
1334 else if (*ptr != ' ' && *ptr != '\t')
1335 break;
1336 }
1337out:
1338 kfree(lbuf);
1339 return count;
1340}
1341
34b9243a
AD
1342static const struct file_operations zcrypt_proc_fops = {
1343 .owner = THIS_MODULE,
1344 .open = zcrypt_proc_open,
1345 .read = seq_read,
1346 .llseek = seq_lseek,
1347 .release = single_release,
1348 .write = zcrypt_proc_write,
1349};
1350
2f7c8bd6
RW
1351static int zcrypt_rng_device_count;
1352static u32 *zcrypt_rng_buffer;
1353static int zcrypt_rng_buffer_index;
1354static DEFINE_MUTEX(zcrypt_rng_mutex);
1355
1356static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1357{
1358 int rc;
1359
1749a81d 1360 /*
2f7c8bd6
RW
1361 * We don't need locking here because the RNG API guarantees serialized
1362 * read method calls.
1363 */
1364 if (zcrypt_rng_buffer_index == 0) {
1365 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
dabecb29
HD
1366 /* on failure: retry once again after a requested rescan */
1367 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1368 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
2f7c8bd6
RW
1369 if (rc < 0)
1370 return -EIO;
1371 zcrypt_rng_buffer_index = rc / sizeof *data;
1372 }
1373 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1374 return sizeof *data;
1375}
1376
1377static struct hwrng zcrypt_rng_dev = {
1378 .name = "zcrypt",
1379 .data_read = zcrypt_rng_data_read,
db490cb9 1380 .quality = 990,
2f7c8bd6
RW
1381};
1382
1383static int zcrypt_rng_device_add(void)
1384{
1385 int rc = 0;
1386
1387 mutex_lock(&zcrypt_rng_mutex);
1388 if (zcrypt_rng_device_count == 0) {
1389 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1390 if (!zcrypt_rng_buffer) {
1391 rc = -ENOMEM;
1392 goto out;
1393 }
1394 zcrypt_rng_buffer_index = 0;
db490cb9
IT
1395 if (!zcrypt_hwrng_seed)
1396 zcrypt_rng_dev.quality = 0;
2f7c8bd6
RW
1397 rc = hwrng_register(&zcrypt_rng_dev);
1398 if (rc)
1399 goto out_free;
1400 zcrypt_rng_device_count = 1;
1401 } else
1402 zcrypt_rng_device_count++;
1403 mutex_unlock(&zcrypt_rng_mutex);
1404 return 0;
1405
1406out_free:
1407 free_page((unsigned long) zcrypt_rng_buffer);
1408out:
1409 mutex_unlock(&zcrypt_rng_mutex);
1410 return rc;
1411}
1412
1413static void zcrypt_rng_device_remove(void)
1414{
1415 mutex_lock(&zcrypt_rng_mutex);
1416 zcrypt_rng_device_count--;
1417 if (zcrypt_rng_device_count == 0) {
1418 hwrng_unregister(&zcrypt_rng_dev);
1419 free_page((unsigned long) zcrypt_rng_buffer);
1420 }
1421 mutex_unlock(&zcrypt_rng_mutex);
1422}
1423
dabecb29
HD
1424int __init zcrypt_debug_init(void)
1425{
1426 debugfs_root = debugfs_create_dir("zcrypt", NULL);
1427
1428 zcrypt_dbf_common = debug_register("zcrypt_common", 1, 1, 16);
1429 debug_register_view(zcrypt_dbf_common, &debug_hex_ascii_view);
1430 debug_set_level(zcrypt_dbf_common, DBF_ERR);
1431
1432 zcrypt_dbf_devices = debug_register("zcrypt_devices", 1, 1, 16);
1433 debug_register_view(zcrypt_dbf_devices, &debug_hex_ascii_view);
1434 debug_set_level(zcrypt_dbf_devices, DBF_ERR);
1435
1436 return 0;
1437}
1438
1439void zcrypt_debug_exit(void)
1440{
1441 debugfs_remove(debugfs_root);
1442 if (zcrypt_dbf_common)
1443 debug_unregister(zcrypt_dbf_common);
1444 if (zcrypt_dbf_devices)
1445 debug_unregister(zcrypt_dbf_devices);
1446}
1447
2dbc2418 1448/**
1749a81d
FB
1449 * zcrypt_api_init(): Module initialization.
1450 *
2dbc2418
MS
1451 * The module initialization code.
1452 */
1453int __init zcrypt_api_init(void)
1454{
1455 int rc;
1456
dabecb29
HD
1457 rc = zcrypt_debug_init();
1458 if (rc)
1459 goto out;
1460
1461 atomic_set(&zcrypt_rescan_req, 0);
1462
2dbc2418
MS
1463 /* Register the request sprayer. */
1464 rc = misc_register(&zcrypt_misc_device);
1a89dd8f 1465 if (rc < 0)
2dbc2418 1466 goto out;
2dbc2418
MS
1467
1468 /* Set up the proc file system */
34b9243a 1469 zcrypt_entry = proc_create("driver/z90crypt", 0644, NULL, &zcrypt_proc_fops);
2dbc2418 1470 if (!zcrypt_entry) {
2dbc2418
MS
1471 rc = -ENOMEM;
1472 goto out_misc;
1473 }
2dbc2418
MS
1474
1475 return 0;
1476
1477out_misc:
1478 misc_deregister(&zcrypt_misc_device);
1479out:
1480 return rc;
1481}
1482
1483/**
1749a81d
FB
1484 * zcrypt_api_exit(): Module termination.
1485 *
2dbc2418
MS
1486 * The module termination code.
1487 */
1488void zcrypt_api_exit(void)
1489{
1490 remove_proc_entry("driver/z90crypt", NULL);
1491 misc_deregister(&zcrypt_misc_device);
dabecb29 1492 zcrypt_debug_exit();
2dbc2418
MS
1493}
1494
2dbc2418
MS
1495module_init(zcrypt_api_init);
1496module_exit(zcrypt_api_exit);