blob: 529ffe26ea9d95465d0ffa01956661c99227dcc2 [file] [log] [blame]
Greg Kroah-Hartman812141a2017-11-14 18:38:01 +01001// SPDX-License-Identifier: GPL-2.0+
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002/*
Harald Freudenberger00fab232018-09-17 16:18:41 +02003 * Copyright IBM Corp. 2001, 2018
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02004 * Author(s): Robert Burroughs
5 * Eric Rossman (edrossma@us.ibm.com)
6 * Cornelia Huck <cornelia.huck@de.ibm.com>
7 *
8 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Ralph Wuerthner <rwuerthn@de.ibm.com>
Holger Dengler5e55a482012-08-28 16:45:36 +020011 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
Harald Freudenberger00fab232018-09-17 16:18:41 +020012 * Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020013 */
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>
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020020#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070022#include <linux/atomic.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +020024#include <linux/hw_random.h>
Holger Denglerdabecb22012-09-10 21:34:26 +020025#include <linux/debugfs.h>
Harald Freudenberger00fab232018-09-17 16:18:41 +020026#include <linux/cdev.h>
27#include <linux/ctype.h>
Harald Freudenberger27c4f672020-09-29 16:07:22 +020028#include <linux/capability.h>
Holger Denglerdabecb22012-09-10 21:34:26 +020029#include <asm/debug.h>
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020030
Harald Freudenberger13b251b2016-11-25 18:04:56 +010031#define CREATE_TRACE_POINTS
32#include <asm/trace/zcrypt.h>
33
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020034#include "zcrypt_api.h"
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010035#include "zcrypt_debug.h"
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020036
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +010037#include "zcrypt_msgtype6.h"
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +020038#include "zcrypt_msgtype50.h"
Harald Freudenbergerefc598e2019-06-11 11:16:56 +020039#include "zcrypt_ccamisc.h"
Harald Freudenberger7384eb72019-08-30 16:07:08 +020040#include "zcrypt_ep11misc.h"
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +010041
Felix Beck1749a812008-04-17 07:46:28 +020042/*
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020043 * Module description.
44 */
45MODULE_AUTHOR("IBM Corporation");
Holger Dengler5e55a482012-08-28 16:45:36 +020046MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
47 "Copyright IBM Corp. 2001, 2012");
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020048MODULE_LICENSE("GPL");
49
Harald Freudenberger13b251b2016-11-25 18:04:56 +010050/*
51 * zcrypt tracepoint functions
52 */
53EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
54EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
55
Ingo Tuchschererdb490cb2015-03-17 16:02:20 +010056static int zcrypt_hwrng_seed = 1;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020057module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
Ingo Tuchschererdb490cb2015-03-17 16:02:20 +010058MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
59
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020060DEFINE_SPINLOCK(zcrypt_list_lock);
61LIST_HEAD(zcrypt_card_list);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020062
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020063static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
Holger Denglerdabecb22012-09-10 21:34:26 +020064static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
65
66atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
67EXPORT_SYMBOL(zcrypt_rescan_req);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020068
Holger Dengler5e55a482012-08-28 16:45:36 +020069static LIST_HEAD(zcrypt_ops_list);
70
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010071/* Zcrypt related debug feature stuff. */
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010072debug_info_t *zcrypt_dbf_info;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +020073
74/**
Holger Denglerdabecb22012-09-10 21:34:26 +020075 * Process a rescan of the transport layer.
76 *
77 * Returns 1, if the rescan has been processed, otherwise 0.
78 */
79static inline int zcrypt_process_rescan(void)
80{
81 if (atomic_read(&zcrypt_rescan_req)) {
82 atomic_set(&zcrypt_rescan_req, 0);
83 atomic_inc(&zcrypt_rescan_count);
84 ap_bus_force_rescan();
Harald Freudenberger792e0e02017-06-29 09:44:11 +020085 ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010086 atomic_inc_return(&zcrypt_rescan_count));
Holger Denglerdabecb22012-09-10 21:34:26 +020087 return 1;
88 }
89 return 0;
90}
91
Holger Dengler5e55a482012-08-28 16:45:36 +020092void zcrypt_msgtype_register(struct zcrypt_ops *zops)
93{
Sascha Silbe121a8682015-10-28 11:06:08 +010094 list_add_tail(&zops->list, &zcrypt_ops_list);
Holger Dengler5e55a482012-08-28 16:45:36 +020095}
Holger Dengler5e55a482012-08-28 16:45:36 +020096
97void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
98{
Holger Dengler5e55a482012-08-28 16:45:36 +020099 list_del_init(&zops->list);
Holger Dengler5e55a482012-08-28 16:45:36 +0200100}
Holger Dengler5e55a482012-08-28 16:45:36 +0200101
Martin Schwidefsky236fb2a2016-09-02 15:21:45 +0200102struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
Holger Dengler5e55a482012-08-28 16:45:36 +0200103{
104 struct zcrypt_ops *zops;
Holger Dengler5e55a482012-08-28 16:45:36 +0200105
Martin Schwidefsky236fb2a2016-09-02 15:21:45 +0200106 list_for_each_entry(zops, &zcrypt_ops_list, list)
Holger Dengler5e55a482012-08-28 16:45:36 +0200107 if ((zops->variant == variant) &&
Martin Schwidefsky236fb2a2016-09-02 15:21:45 +0200108 (!strncmp(zops->name, name, sizeof(zops->name))))
109 return zops;
110 return NULL;
Holger Dengler5e55a482012-08-28 16:45:36 +0200111}
Martin Schwidefsky236fb2a2016-09-02 15:21:45 +0200112EXPORT_SYMBOL(zcrypt_msgtype);
Holger Dengler5e55a482012-08-28 16:45:36 +0200113
Harald Freudenberger00fab232018-09-17 16:18:41 +0200114/*
115 * Multi device nodes extension functions.
116 */
117
118#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
119
120struct zcdn_device;
121
122static struct class *zcrypt_class;
123static dev_t zcrypt_devt;
124static struct cdev zcrypt_cdev;
125
126struct zcdn_device {
127 struct device device;
128 struct ap_perms perms;
129};
130
131#define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
132
133#define ZCDN_MAX_NAME 32
134
135static int zcdn_create(const char *name);
136static int zcdn_destroy(const char *name);
137
Harald Freudenberger00fab232018-09-17 16:18:41 +0200138/*
139 * Find zcdn device by name.
140 * Returns reference to the zcdn device which needs to be released
141 * with put_device() after use.
142 */
143static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
144{
Suzuki K Poulose6cda08a2019-07-23 23:18:32 +0100145 struct device *dev = class_find_device_by_name(zcrypt_class, name);
Harald Freudenberger00fab232018-09-17 16:18:41 +0200146
147 return dev ? to_zcdn_dev(dev) : NULL;
148}
149
150/*
151 * Find zcdn device by devt value.
152 * Returns reference to the zcdn device which needs to be released
153 * with put_device() after use.
154 */
155static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
156{
Suzuki K Poulose4495dfd2019-07-23 23:18:35 +0100157 struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
Harald Freudenberger00fab232018-09-17 16:18:41 +0200158
159 return dev ? to_zcdn_dev(dev) : NULL;
160}
161
162static ssize_t ioctlmask_show(struct device *dev,
163 struct device_attribute *attr,
164 char *buf)
165{
166 int i, rc;
167 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
168
169 if (mutex_lock_interruptible(&ap_perms_mutex))
170 return -ERESTARTSYS;
171
172 buf[0] = '0';
173 buf[1] = 'x';
174 for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
175 snprintf(buf + 2 + 2 * i * sizeof(long),
176 PAGE_SIZE - 2 - 2 * i * sizeof(long),
177 "%016lx", zcdndev->perms.ioctlm[i]);
178 buf[2 + 2 * i * sizeof(long)] = '\n';
179 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
180 rc = 2 + 2 * i * sizeof(long) + 1;
181
182 mutex_unlock(&ap_perms_mutex);
183
184 return rc;
185}
186
187static ssize_t ioctlmask_store(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
190{
191 int rc;
192 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
193
194 rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
195 AP_IOCTLS, &ap_perms_mutex);
196 if (rc)
197 return rc;
198
199 return count;
200}
201
202static DEVICE_ATTR_RW(ioctlmask);
203
204static ssize_t apmask_show(struct device *dev,
205 struct device_attribute *attr,
206 char *buf)
207{
208 int i, rc;
209 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
210
211 if (mutex_lock_interruptible(&ap_perms_mutex))
212 return -ERESTARTSYS;
213
214 buf[0] = '0';
215 buf[1] = 'x';
216 for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
217 snprintf(buf + 2 + 2 * i * sizeof(long),
218 PAGE_SIZE - 2 - 2 * i * sizeof(long),
219 "%016lx", zcdndev->perms.apm[i]);
220 buf[2 + 2 * i * sizeof(long)] = '\n';
221 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
222 rc = 2 + 2 * i * sizeof(long) + 1;
223
224 mutex_unlock(&ap_perms_mutex);
225
226 return rc;
227}
228
229static ssize_t apmask_store(struct device *dev,
230 struct device_attribute *attr,
231 const char *buf, size_t count)
232{
233 int rc;
234 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
235
236 rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
237 AP_DEVICES, &ap_perms_mutex);
238 if (rc)
239 return rc;
240
241 return count;
242}
243
244static DEVICE_ATTR_RW(apmask);
245
246static ssize_t aqmask_show(struct device *dev,
247 struct device_attribute *attr,
248 char *buf)
249{
250 int i, rc;
251 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
252
253 if (mutex_lock_interruptible(&ap_perms_mutex))
254 return -ERESTARTSYS;
255
256 buf[0] = '0';
257 buf[1] = 'x';
258 for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
259 snprintf(buf + 2 + 2 * i * sizeof(long),
260 PAGE_SIZE - 2 - 2 * i * sizeof(long),
261 "%016lx", zcdndev->perms.aqm[i]);
262 buf[2 + 2 * i * sizeof(long)] = '\n';
263 buf[2 + 2 * i * sizeof(long) + 1] = '\0';
264 rc = 2 + 2 * i * sizeof(long) + 1;
265
266 mutex_unlock(&ap_perms_mutex);
267
268 return rc;
269}
270
271static ssize_t aqmask_store(struct device *dev,
272 struct device_attribute *attr,
273 const char *buf, size_t count)
274{
275 int rc;
276 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
277
278 rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
279 AP_DOMAINS, &ap_perms_mutex);
280 if (rc)
281 return rc;
282
283 return count;
284}
285
286static DEVICE_ATTR_RW(aqmask);
287
288static struct attribute *zcdn_dev_attrs[] = {
289 &dev_attr_ioctlmask.attr,
290 &dev_attr_apmask.attr,
291 &dev_attr_aqmask.attr,
292 NULL
293};
294
295static struct attribute_group zcdn_dev_attr_group = {
296 .attrs = zcdn_dev_attrs
297};
298
299static const struct attribute_group *zcdn_dev_attr_groups[] = {
300 &zcdn_dev_attr_group,
301 NULL
302};
303
304static ssize_t zcdn_create_store(struct class *class,
305 struct class_attribute *attr,
306 const char *buf, size_t count)
307{
308 int rc;
309 char name[ZCDN_MAX_NAME];
310
311 strncpy(name, skip_spaces(buf), sizeof(name));
312 name[sizeof(name) - 1] = '\0';
313
314 rc = zcdn_create(strim(name));
315
316 return rc ? rc : count;
317}
318
319static const struct class_attribute class_attr_zcdn_create =
320 __ATTR(create, 0600, NULL, zcdn_create_store);
321
322static ssize_t zcdn_destroy_store(struct class *class,
323 struct class_attribute *attr,
324 const char *buf, size_t count)
325{
326 int rc;
327 char name[ZCDN_MAX_NAME];
328
329 strncpy(name, skip_spaces(buf), sizeof(name));
330 name[sizeof(name) - 1] = '\0';
331
332 rc = zcdn_destroy(strim(name));
333
334 return rc ? rc : count;
335}
336
337static const struct class_attribute class_attr_zcdn_destroy =
338 __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
339
340static void zcdn_device_release(struct device *dev)
341{
342 struct zcdn_device *zcdndev = to_zcdn_dev(dev);
343
344 ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
345 MAJOR(dev->devt), MINOR(dev->devt));
346
347 kfree(zcdndev);
348}
349
350static int zcdn_create(const char *name)
351{
352 dev_t devt;
353 int i, rc = 0;
354 char nodename[ZCDN_MAX_NAME];
355 struct zcdn_device *zcdndev;
356
357 if (mutex_lock_interruptible(&ap_perms_mutex))
358 return -ERESTARTSYS;
359
360 /* check if device node with this name already exists */
361 if (name[0]) {
362 zcdndev = find_zcdndev_by_name(name);
363 if (zcdndev) {
364 put_device(&zcdndev->device);
365 rc = -EEXIST;
366 goto unlockout;
367 }
368 }
369
370 /* find an unused minor number */
371 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
372 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
373 zcdndev = find_zcdndev_by_devt(devt);
374 if (zcdndev)
375 put_device(&zcdndev->device);
376 else
377 break;
378 }
379 if (i == ZCRYPT_MAX_MINOR_NODES) {
380 rc = -ENOSPC;
381 goto unlockout;
382 }
383
384 /* alloc and prepare a new zcdn device */
385 zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
386 if (!zcdndev) {
387 rc = -ENOMEM;
388 goto unlockout;
389 }
390 zcdndev->device.release = zcdn_device_release;
391 zcdndev->device.class = zcrypt_class;
392 zcdndev->device.devt = devt;
393 zcdndev->device.groups = zcdn_dev_attr_groups;
394 if (name[0])
395 strncpy(nodename, name, sizeof(nodename));
396 else
397 snprintf(nodename, sizeof(nodename),
398 ZCRYPT_NAME "_%d", (int) MINOR(devt));
399 nodename[sizeof(nodename)-1] = '\0';
400 if (dev_set_name(&zcdndev->device, nodename)) {
401 rc = -EINVAL;
402 goto unlockout;
403 }
404 rc = device_register(&zcdndev->device);
405 if (rc) {
406 put_device(&zcdndev->device);
407 goto unlockout;
408 }
409
410 ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
411 MAJOR(devt), MINOR(devt));
412
413unlockout:
414 mutex_unlock(&ap_perms_mutex);
415 return rc;
416}
417
418static int zcdn_destroy(const char *name)
419{
420 int rc = 0;
421 struct zcdn_device *zcdndev;
422
423 if (mutex_lock_interruptible(&ap_perms_mutex))
424 return -ERESTARTSYS;
425
426 /* try to find this zcdn device */
427 zcdndev = find_zcdndev_by_name(name);
428 if (!zcdndev) {
429 rc = -ENOENT;
430 goto unlockout;
431 }
432
433 /*
434 * The zcdn device is not hard destroyed. It is subject to
435 * reference counting and thus just needs to be unregistered.
436 */
437 put_device(&zcdndev->device);
438 device_unregister(&zcdndev->device);
439
440unlockout:
441 mutex_unlock(&ap_perms_mutex);
442 return rc;
443}
444
445static void zcdn_destroy_all(void)
446{
447 int i;
448 dev_t devt;
449 struct zcdn_device *zcdndev;
450
451 mutex_lock(&ap_perms_mutex);
452 for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
453 devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
454 zcdndev = find_zcdndev_by_devt(devt);
455 if (zcdndev) {
456 put_device(&zcdndev->device);
457 device_unregister(&zcdndev->device);
458 }
459 }
460 mutex_unlock(&ap_perms_mutex);
461}
462
463#endif
464
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200465/**
Felix Beck1749a812008-04-17 07:46:28 +0200466 * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
467 *
468 * This function is not supported beyond zcrypt 1.3.1.
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200469 */
470static ssize_t zcrypt_read(struct file *filp, char __user *buf,
471 size_t count, loff_t *f_pos)
472{
473 return -EPERM;
474}
475
476/**
Felix Beck1749a812008-04-17 07:46:28 +0200477 * zcrypt_write(): Not allowed.
478 *
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200479 * Write is is not allowed
480 */
481static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
482 size_t count, loff_t *f_pos)
483{
484 return -EPERM;
485}
486
487/**
Felix Beck1749a812008-04-17 07:46:28 +0200488 * zcrypt_open(): Count number of users.
489 *
490 * Device open function to count number of users.
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200491 */
492static int zcrypt_open(struct inode *inode, struct file *filp)
493{
Harald Freudenberger00fab232018-09-17 16:18:41 +0200494 struct ap_perms *perms = &ap_perms;
495
496#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
497 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
498 struct zcdn_device *zcdndev;
499
500 if (mutex_lock_interruptible(&ap_perms_mutex))
501 return -ERESTARTSYS;
502 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
503 /* find returns a reference, no get_device() needed */
504 mutex_unlock(&ap_perms_mutex);
505 if (zcdndev)
506 perms = &zcdndev->perms;
507 }
508#endif
509 filp->private_data = (void *) perms;
510
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200511 atomic_inc(&zcrypt_open_count);
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +0300512 return stream_open(inode, filp);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200513}
514
Felix Beck1749a812008-04-17 07:46:28 +0200515/**
516 * zcrypt_release(): Count number of users.
517 *
518 * Device close function to count number of users.
519 */
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200520static int zcrypt_release(struct inode *inode, struct file *filp)
521{
Harald Freudenberger00fab232018-09-17 16:18:41 +0200522#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
523 if (filp->f_inode->i_cdev == &zcrypt_cdev) {
524 struct zcdn_device *zcdndev;
525
Johan Hovold388bb192019-10-10 15:13:33 +0200526 mutex_lock(&ap_perms_mutex);
Harald Freudenberger00fab232018-09-17 16:18:41 +0200527 zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
528 mutex_unlock(&ap_perms_mutex);
529 if (zcdndev) {
530 /* 2 puts here: one for find, one for open */
531 put_device(&zcdndev->device);
532 put_device(&zcdndev->device);
533 }
534 }
535#endif
536
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200537 atomic_dec(&zcrypt_open_count);
538 return 0;
539}
540
Harald Freudenberger00fab232018-09-17 16:18:41 +0200541static inline int zcrypt_check_ioctl(struct ap_perms *perms,
542 unsigned int cmd)
543{
544 int rc = -EPERM;
545 int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
546
547 if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
548 if (test_bit_inv(ioctlnr, perms->ioctlm))
549 rc = 0;
550 }
551
552 if (rc)
553 ZCRYPT_DBF(DBF_WARN,
554 "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
555 ioctlnr, rc);
556
557 return rc;
558}
559
560static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
561{
562 return test_bit_inv(card, perms->apm) ? true : false;
563}
564
565static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
566{
567 return test_bit_inv(queue, perms->aqm) ? true : false;
568}
569
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200570static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
571 struct zcrypt_queue *zq,
Harald Freudenberger01396a32019-02-22 17:24:11 +0100572 struct module **pmod,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200573 unsigned int weight)
574{
575 if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
576 return NULL;
577 zcrypt_queue_get(zq);
578 get_device(&zq->queue->ap_dev.device);
579 atomic_add(weight, &zc->load);
580 atomic_add(weight, &zq->load);
581 zq->request_count++;
Harald Freudenberger01396a32019-02-22 17:24:11 +0100582 *pmod = zq->queue->ap_dev.drv->driver.owner;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200583 return zq;
584}
585
586static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
587 struct zcrypt_queue *zq,
Harald Freudenberger01396a32019-02-22 17:24:11 +0100588 struct module *mod,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200589 unsigned int weight)
590{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200591 zq->request_count--;
592 atomic_sub(weight, &zc->load);
593 atomic_sub(weight, &zq->load);
594 put_device(&zq->queue->ap_dev.device);
595 zcrypt_queue_put(zq);
596 module_put(mod);
597}
598
Ingo Tuchscherere47de212016-10-14 14:34:51 +0200599static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
600 struct zcrypt_card *pref_zc,
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200601 unsigned int weight,
602 unsigned int pref_weight)
Ingo Tuchscherere47de212016-10-14 14:34:51 +0200603{
604 if (!pref_zc)
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200605 return true;
Ingo Tuchscherere47de212016-10-14 14:34:51 +0200606 weight += atomic_read(&zc->load);
607 pref_weight += atomic_read(&pref_zc->load);
608 if (weight == pref_weight)
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200609 return atomic64_read(&zc->card->total_request_count) <
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +0100610 atomic64_read(&pref_zc->card->total_request_count);
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200611 return weight < pref_weight;
Ingo Tuchscherere47de212016-10-14 14:34:51 +0200612}
613
614static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
615 struct zcrypt_queue *pref_zq,
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +0200616 unsigned int weight,
617 unsigned int pref_weight)
Ingo Tuchscherere47de212016-10-14 14:34:51 +0200618{
619 if (!pref_zq)
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200620 return true;
Ingo Tuchscherere47de212016-10-14 14:34:51 +0200621 weight += atomic_read(&zq->load);
622 pref_weight += atomic_read(&pref_zq->load);
623 if (weight == pref_weight)
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200624 return zq->queue->total_request_count <
Harald Freudenberger0b0882672017-11-17 16:32:22 +0100625 pref_zq->queue->total_request_count;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200626 return weight < pref_weight;
Ingo Tuchscherere47de212016-10-14 14:34:51 +0200627}
628
Felix Beck1749a812008-04-17 07:46:28 +0200629/*
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200630 * zcrypt ioctls.
631 */
Harald Freudenberger00fab232018-09-17 16:18:41 +0200632static long zcrypt_rsa_modexpo(struct ap_perms *perms,
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200633 struct zcrypt_track *tr,
Harald Freudenberger00fab232018-09-17 16:18:41 +0200634 struct ica_rsa_modexpo *mex)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200635{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200636 struct zcrypt_card *zc, *pref_zc;
637 struct zcrypt_queue *zq, *pref_zq;
Harald Freudenberger3730f532020-09-26 22:51:38 +0200638 struct ap_message ap_msg;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200639 unsigned int wgt = 0, pref_wgt = 0;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200640 unsigned int func_code;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200641 int cpen, qpen, qid = 0, rc = -ENODEV;
Harald Freudenberger01396a32019-02-22 17:24:11 +0100642 struct module *mod;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200643
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100644 trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
645
Harald Freudenberger3730f532020-09-26 22:51:38 +0200646 ap_init_message(&ap_msg);
647
Harald Freudenberger27c4f672020-09-29 16:07:22 +0200648#ifdef CONFIG_ZCRYPT_DEBUG
649 if (tr && tr->fi.cmd)
650 ap_msg.fi.cmd = tr->fi.cmd;
651#endif
652
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100653 if (mex->outputdatalength < mex->inputdatalength) {
Arnd Bergmann913140e2019-04-08 23:26:18 +0200654 func_code = 0;
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100655 rc = -EINVAL;
656 goto out;
657 }
658
Felix Beck1749a812008-04-17 07:46:28 +0200659 /*
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200660 * As long as outputdatalength is big enough, we can set the
661 * outputdatalength equal to the inputdatalength, since that is the
662 * number of bytes we will copy in any case
663 */
664 mex->outputdatalength = mex->inputdatalength;
665
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200666 rc = get_rsa_modex_fc(mex, &func_code);
667 if (rc)
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100668 goto out;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200669
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200670 pref_zc = NULL;
671 pref_zq = NULL;
672 spin_lock(&zcrypt_list_lock);
673 for_each_zcrypt_card(zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +0200674 /* Check for useable accelarator or CCA card */
675 if (!zc->online || !zc->card->config ||
676 !(zc->card->functions & 0x18000000))
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200677 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200678 /* Check for size limits */
679 if (zc->min_mod_size > mex->inputdatalength ||
680 zc->max_mod_size < mex->inputdatalength)
681 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +0200682 /* check if device node has admission for this card */
683 if (!zcrypt_check_card(perms, zc->card->id))
684 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200685 /* get weight index of the card device */
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200686 wgt = zc->speed_rating[func_code];
687 /* penalty if this msg was previously sent via this card */
688 cpen = (tr && tr->again_counter && tr->last_qid &&
689 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
690 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
691 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200692 continue;
693 for_each_zcrypt_queue(zq, zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +0200694 /* check if device is useable and eligible */
695 if (!zq->online || !zq->ops->rsa_modexpo ||
696 !zq->queue->config)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200697 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +0200698 /* check if device node has admission for this queue */
699 if (!zcrypt_check_queue(perms,
700 AP_QID_QUEUE(zq->queue->qid)))
701 continue;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200702 /* penalty if the msg was previously sent at this qid */
703 qpen = (tr && tr->again_counter && tr->last_qid &&
704 tr->last_qid == zq->queue->qid) ?
705 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
706 if (!zcrypt_queue_compare(zq, pref_zq,
707 wgt + cpen + qpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200708 continue;
709 pref_zc = zc;
710 pref_zq = zq;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200711 pref_wgt = wgt + cpen + qpen;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200712 }
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200713 }
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200714 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200715 spin_unlock(&zcrypt_list_lock);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200716
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100717 if (!pref_zq) {
718 rc = -ENODEV;
719 goto out;
720 }
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200721
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100722 qid = pref_zq->queue->qid;
Harald Freudenberger3730f532020-09-26 22:51:38 +0200723 rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200724
725 spin_lock(&zcrypt_list_lock);
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200726 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200727 spin_unlock(&zcrypt_list_lock);
728
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100729out:
Harald Freudenberger3730f532020-09-26 22:51:38 +0200730 ap_release_message(&ap_msg);
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200731 if (tr) {
732 tr->last_rc = rc;
733 tr->last_qid = qid;
734 }
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100735 trace_s390_zcrypt_rep(mex, func_code, rc,
736 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200737 return rc;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200738}
739
Harald Freudenberger00fab232018-09-17 16:18:41 +0200740static long zcrypt_rsa_crt(struct ap_perms *perms,
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200741 struct zcrypt_track *tr,
Harald Freudenberger00fab232018-09-17 16:18:41 +0200742 struct ica_rsa_modexpo_crt *crt)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200743{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200744 struct zcrypt_card *zc, *pref_zc;
745 struct zcrypt_queue *zq, *pref_zq;
Harald Freudenberger3730f532020-09-26 22:51:38 +0200746 struct ap_message ap_msg;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200747 unsigned int wgt = 0, pref_wgt = 0;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200748 unsigned int func_code;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200749 int cpen, qpen, qid = 0, rc = -ENODEV;
Harald Freudenberger01396a32019-02-22 17:24:11 +0100750 struct module *mod;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200751
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100752 trace_s390_zcrypt_req(crt, TP_ICARSACRT);
753
Harald Freudenberger3730f532020-09-26 22:51:38 +0200754 ap_init_message(&ap_msg);
755
Harald Freudenberger27c4f672020-09-29 16:07:22 +0200756#ifdef CONFIG_ZCRYPT_DEBUG
757 if (tr && tr->fi.cmd)
758 ap_msg.fi.cmd = tr->fi.cmd;
759#endif
760
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100761 if (crt->outputdatalength < crt->inputdatalength) {
Arnd Bergmann913140e2019-04-08 23:26:18 +0200762 func_code = 0;
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100763 rc = -EINVAL;
764 goto out;
765 }
766
Felix Beck1749a812008-04-17 07:46:28 +0200767 /*
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200768 * As long as outputdatalength is big enough, we can set the
769 * outputdatalength equal to the inputdatalength, since that is the
770 * number of bytes we will copy in any case
771 */
772 crt->outputdatalength = crt->inputdatalength;
773
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200774 rc = get_rsa_crt_fc(crt, &func_code);
775 if (rc)
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100776 goto out;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200777
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200778 pref_zc = NULL;
779 pref_zq = NULL;
780 spin_lock(&zcrypt_list_lock);
781 for_each_zcrypt_card(zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +0200782 /* Check for useable accelarator or CCA card */
783 if (!zc->online || !zc->card->config ||
784 !(zc->card->functions & 0x18000000))
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200785 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200786 /* Check for size limits */
787 if (zc->min_mod_size > crt->inputdatalength ||
788 zc->max_mod_size < crt->inputdatalength)
789 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +0200790 /* check if device node has admission for this card */
791 if (!zcrypt_check_card(perms, zc->card->id))
792 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200793 /* get weight index of the card device */
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200794 wgt = zc->speed_rating[func_code];
795 /* penalty if this msg was previously sent via this card */
796 cpen = (tr && tr->again_counter && tr->last_qid &&
797 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
798 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
799 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200800 continue;
801 for_each_zcrypt_queue(zq, zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +0200802 /* check if device is useable and eligible */
803 if (!zq->online || !zq->ops->rsa_modexpo_crt ||
804 !zq->queue->config)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200805 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +0200806 /* check if device node has admission for this queue */
807 if (!zcrypt_check_queue(perms,
808 AP_QID_QUEUE(zq->queue->qid)))
809 continue;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200810 /* penalty if the msg was previously sent at this qid */
811 qpen = (tr && tr->again_counter && tr->last_qid &&
812 tr->last_qid == zq->queue->qid) ?
813 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
814 if (!zcrypt_queue_compare(zq, pref_zq,
815 wgt + cpen + qpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200816 continue;
817 pref_zc = zc;
818 pref_zq = zq;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200819 pref_wgt = wgt + cpen + qpen;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200820 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200821 }
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200822 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200823 spin_unlock(&zcrypt_list_lock);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200824
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100825 if (!pref_zq) {
826 rc = -ENODEV;
827 goto out;
828 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200829
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100830 qid = pref_zq->queue->qid;
Harald Freudenberger3730f532020-09-26 22:51:38 +0200831 rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200832
833 spin_lock(&zcrypt_list_lock);
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200834 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200835 spin_unlock(&zcrypt_list_lock);
836
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100837out:
Harald Freudenberger3730f532020-09-26 22:51:38 +0200838 ap_release_message(&ap_msg);
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200839 if (tr) {
840 tr->last_rc = rc;
841 tr->last_qid = qid;
842 }
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100843 trace_s390_zcrypt_rep(crt, func_code, rc,
844 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200845 return rc;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +0200846}
847
Harald Freudenberger52f72fe2020-09-15 17:01:58 +0200848static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200849 struct zcrypt_track *tr,
Harald Freudenberger00fab232018-09-17 16:18:41 +0200850 struct ica_xcRB *xcRB)
Ralph Wuerthner54321142006-09-20 15:58:36 +0200851{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200852 struct zcrypt_card *zc, *pref_zc;
853 struct zcrypt_queue *zq, *pref_zq;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200854 struct ap_message ap_msg;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200855 unsigned int wgt = 0, pref_wgt = 0;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200856 unsigned int func_code;
Harald Freudenberger7379e652019-05-21 13:50:09 +0200857 unsigned short *domain, tdom;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200858 int cpen, qpen, qid = 0, rc = -ENODEV;
Harald Freudenberger01396a32019-02-22 17:24:11 +0100859 struct module *mod;
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100860
861 trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200862
Harald Freudenbergera17b92e2018-10-04 15:37:49 +0200863 xcRB->status = 0;
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +0200864 ap_init_message(&ap_msg);
Harald Freudenberger27c4f672020-09-29 16:07:22 +0200865
866#ifdef CONFIG_ZCRYPT_DEBUG
867 if (tr && tr->fi.cmd)
868 ap_msg.fi.cmd = tr->fi.cmd;
869 if (tr && tr->fi.action == AP_FI_ACTION_CCA_AGENT_FF) {
870 ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid agent_ID 'FF'\n",
871 __func__, tr->fi.cmd);
872 xcRB->agent_ID = 0x4646;
873 }
874#endif
875
Harald Freudenberger52f72fe2020-09-15 17:01:58 +0200876 rc = get_cprb_fc(userspace, xcRB, &ap_msg, &func_code, &domain);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200877 if (rc)
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100878 goto out;
Ralph Wuerthner54321142006-09-20 15:58:36 +0200879
Harald Freudenberger7379e652019-05-21 13:50:09 +0200880 /*
881 * If a valid target domain is set and this domain is NOT a usage
882 * domain but a control only domain, use the default domain as target.
883 */
884 tdom = *domain;
Harald Freudenberger47c07bf2020-06-30 09:54:50 +0200885 if (tdom < AP_DOMAINS &&
Harald Freudenberger7379e652019-05-21 13:50:09 +0200886 !ap_test_config_usage_domain(tdom) &&
887 ap_test_config_ctrl_domain(tdom) &&
888 ap_domain_index >= 0)
889 tdom = ap_domain_index;
890
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200891 pref_zc = NULL;
892 pref_zq = NULL;
893 spin_lock(&zcrypt_list_lock);
894 for_each_zcrypt_card(zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +0200895 /* Check for useable CCA card */
896 if (!zc->online || !zc->card->config ||
897 !(zc->card->functions & 0x10000000))
Ralph Wuerthner54321142006-09-20 15:58:36 +0200898 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200899 /* Check for user selected CCA card */
900 if (xcRB->user_defined != AUTOSELECT &&
901 xcRB->user_defined != zc->card->id)
902 continue;
Harald Freudenbergerbd396542021-06-25 12:29:46 +0200903 /* check if request size exceeds card max msg size */
904 if (ap_msg.len > zc->card->maxmsgsize)
905 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +0200906 /* check if device node has admission for this card */
907 if (!zcrypt_check_card(perms, zc->card->id))
908 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200909 /* get weight index of the card device */
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200910 wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
911 /* penalty if this msg was previously sent via this card */
912 cpen = (tr && tr->again_counter && tr->last_qid &&
913 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
914 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
915 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200916 continue;
917 for_each_zcrypt_queue(zq, zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +0200918 /* check for device useable and eligible */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200919 if (!zq->online ||
Harald Freudenberger14878422016-10-27 08:57:39 +0200920 !zq->ops->send_cprb ||
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +0200921 !zq->queue->config ||
Harald Freudenberger8f291eb2019-08-30 15:17:27 +0200922 (tdom != AUTOSEL_DOM &&
Harald Freudenberger7379e652019-05-21 13:50:09 +0200923 tdom != AP_QID_QUEUE(zq->queue->qid)))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200924 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +0200925 /* check if device node has admission for this queue */
926 if (!zcrypt_check_queue(perms,
927 AP_QID_QUEUE(zq->queue->qid)))
928 continue;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200929 /* penalty if the msg was previously sent at this qid */
930 qpen = (tr && tr->again_counter && tr->last_qid &&
931 tr->last_qid == zq->queue->qid) ?
932 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
933 if (!zcrypt_queue_compare(zq, pref_zq,
934 wgt + cpen + qpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200935 continue;
936 pref_zc = zc;
937 pref_zq = zq;
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200938 pref_wgt = wgt + cpen + qpen;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200939 }
940 }
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200941 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200942 spin_unlock(&zcrypt_list_lock);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200943
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100944 if (!pref_zq) {
945 rc = -ENODEV;
946 goto out;
947 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200948
949 /* in case of auto select, provide the correct domain */
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100950 qid = pref_zq->queue->qid;
Harald Freudenberger8f291eb2019-08-30 15:17:27 +0200951 if (*domain == AUTOSEL_DOM)
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100952 *domain = AP_QID_QUEUE(qid);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200953
Harald Freudenberger27c4f672020-09-29 16:07:22 +0200954#ifdef CONFIG_ZCRYPT_DEBUG
955 if (tr && tr->fi.action == AP_FI_ACTION_CCA_DOM_INVAL) {
956 ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid domain\n",
957 __func__, tr->fi.cmd);
958 *domain = 99;
959 }
960#endif
961
Harald Freudenberger52f72fe2020-09-15 17:01:58 +0200962 rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcRB, &ap_msg);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200963
964 spin_lock(&zcrypt_list_lock);
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200965 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200966 spin_unlock(&zcrypt_list_lock);
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100967
968out:
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +0200969 ap_release_message(&ap_msg);
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200970 if (tr) {
971 tr->last_rc = rc;
972 tr->last_qid = qid;
973 }
Harald Freudenberger13b251b2016-11-25 18:04:56 +0100974 trace_s390_zcrypt_rep(xcRB, func_code, rc,
975 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200976 return rc;
Ralph Wuerthner54321142006-09-20 15:58:36 +0200977}
Harald Freudenberger00fab232018-09-17 16:18:41 +0200978
979long zcrypt_send_cprb(struct ica_xcRB *xcRB)
980{
Harald Freudenberger91ffc512020-07-02 11:10:11 +0200981 return _zcrypt_send_cprb(false, &ap_perms, NULL, xcRB);
Harald Freudenberger00fab232018-09-17 16:18:41 +0200982}
Harald Freudenbergera1d001e2016-11-02 14:32:32 +0100983EXPORT_SYMBOL(zcrypt_send_cprb);
Ralph Wuerthner54321142006-09-20 15:58:36 +0200984
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200985static bool is_desired_ep11_card(unsigned int dev_id,
986 unsigned short target_num,
987 struct ep11_target_dev *targets)
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +0100988{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200989 while (target_num-- > 0) {
Harald Freudenberger8f291eb2019-08-30 15:17:27 +0200990 if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +0100991 return true;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200992 targets++;
993 }
994 return false;
995}
996
997static bool is_desired_ep11_queue(unsigned int dev_qid,
998 unsigned short target_num,
999 struct ep11_target_dev *targets)
1000{
Harald Freudenberger8f291eb2019-08-30 15:17:27 +02001001 int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
1002
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001003 while (target_num-- > 0) {
Harald Freudenberger8f291eb2019-08-30 15:17:27 +02001004 if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
1005 (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001006 return true;
1007 targets++;
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001008 }
1009 return false;
1010}
1011
Harald Freudenberger52f72fe2020-09-15 17:01:58 +02001012static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001013 struct zcrypt_track *tr,
Harald Freudenbergera7367992019-08-30 15:57:17 +02001014 struct ep11_urb *xcrb)
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001015{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001016 struct zcrypt_card *zc, *pref_zc;
1017 struct zcrypt_queue *zq, *pref_zq;
1018 struct ep11_target_dev *targets;
1019 unsigned short target_num;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001020 unsigned int wgt = 0, pref_wgt = 0;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001021 unsigned int func_code;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001022 struct ap_message ap_msg;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001023 int cpen, qpen, qid = 0, rc = -ENODEV;
Harald Freudenberger01396a32019-02-22 17:24:11 +01001024 struct module *mod;
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001025
1026 trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001027
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +02001028 ap_init_message(&ap_msg);
1029
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001030#ifdef CONFIG_ZCRYPT_DEBUG
1031 if (tr && tr->fi.cmd)
1032 ap_msg.fi.cmd = tr->fi.cmd;
1033#endif
1034
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001035 target_num = (unsigned short) xcrb->targets_num;
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001036
1037 /* empty list indicates autoselect (all available targets) */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001038 targets = NULL;
1039 if (target_num != 0) {
1040 struct ep11_target_dev __user *uptr;
1041
1042 targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001043 if (!targets) {
Arnd Bergmann913140e2019-04-08 23:26:18 +02001044 func_code = 0;
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001045 rc = -ENOMEM;
1046 goto out;
1047 }
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001048
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001049 uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
Harald Freudenberger52f72fe2020-09-15 17:01:58 +02001050 if (z_copy_from_user(userspace, targets, uptr,
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001051 target_num * sizeof(*targets))) {
Arnd Bergmann913140e2019-04-08 23:26:18 +02001052 func_code = 0;
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001053 rc = -EFAULT;
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +02001054 goto out_free;
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001055 }
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001056 }
1057
Harald Freudenberger52f72fe2020-09-15 17:01:58 +02001058 rc = get_ep11cprb_fc(userspace, xcrb, &ap_msg, &func_code);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001059 if (rc)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001060 goto out_free;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001061
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001062 pref_zc = NULL;
1063 pref_zq = NULL;
1064 spin_lock(&zcrypt_list_lock);
1065 for_each_zcrypt_card(zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +02001066 /* Check for useable EP11 card */
1067 if (!zc->online || !zc->card->config ||
1068 !(zc->card->functions & 0x04000000))
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001069 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001070 /* Check for user selected EP11 card */
1071 if (targets &&
1072 !is_desired_ep11_card(zc->card->id, target_num, targets))
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001073 continue;
Harald Freudenbergerbd396542021-06-25 12:29:46 +02001074 /* check if request size exceeds card max msg size */
1075 if (ap_msg.len > zc->card->maxmsgsize)
1076 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +02001077 /* check if device node has admission for this card */
1078 if (!zcrypt_check_card(perms, zc->card->id))
1079 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001080 /* get weight index of the card device */
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001081 wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1082 /* penalty if this msg was previously sent via this card */
1083 cpen = (tr && tr->again_counter && tr->last_qid &&
1084 AP_QID_CARD(tr->last_qid) == zc->card->id) ?
1085 TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
1086 if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001087 continue;
1088 for_each_zcrypt_queue(zq, zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +02001089 /* check if device is useable and eligible */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001090 if (!zq->online ||
Harald Freudenberger14878422016-10-27 08:57:39 +02001091 !zq->ops->send_ep11_cprb ||
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +02001092 !zq->queue->config ||
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001093 (targets &&
1094 !is_desired_ep11_queue(zq->queue->qid,
1095 target_num, targets)))
1096 continue;
Harald Freudenberger00fab232018-09-17 16:18:41 +02001097 /* check if device node has admission for this queue */
1098 if (!zcrypt_check_queue(perms,
1099 AP_QID_QUEUE(zq->queue->qid)))
1100 continue;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001101 /* penalty if the msg was previously sent at this qid */
1102 qpen = (tr && tr->again_counter && tr->last_qid &&
1103 tr->last_qid == zq->queue->qid) ?
1104 TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
1105 if (!zcrypt_queue_compare(zq, pref_zq,
1106 wgt + cpen + qpen, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001107 continue;
1108 pref_zc = zc;
1109 pref_zq = zq;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001110 pref_wgt = wgt + cpen + qpen;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001111 }
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001112 }
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001113 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001114 spin_unlock(&zcrypt_list_lock);
1115
1116 if (!pref_zq) {
1117 rc = -ENODEV;
1118 goto out_free;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001119 }
1120
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001121 qid = pref_zq->queue->qid;
Harald Freudenberger52f72fe2020-09-15 17:01:58 +02001122 rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001123
1124 spin_lock(&zcrypt_list_lock);
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001125 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001126 spin_unlock(&zcrypt_list_lock);
1127
1128out_free:
1129 kfree(targets);
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001130out:
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +02001131 ap_release_message(&ap_msg);
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001132 if (tr) {
1133 tr->last_rc = rc;
1134 tr->last_qid = qid;
1135 }
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001136 trace_s390_zcrypt_rep(xcrb, func_code, rc,
1137 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001138 return rc;
Ingo Tuchscherer91f3e3ea2013-11-20 10:47:13 +01001139}
1140
Harald Freudenbergera7367992019-08-30 15:57:17 +02001141long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
1142{
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001143 return _zcrypt_send_ep11_cprb(false, &ap_perms, NULL, xcrb);
Harald Freudenbergera7367992019-08-30 15:57:17 +02001144}
1145EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1146
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001147static long zcrypt_rng(char *buffer)
1148{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001149 struct zcrypt_card *zc, *pref_zc;
1150 struct zcrypt_queue *zq, *pref_zq;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001151 unsigned int wgt = 0, pref_wgt = 0;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001152 unsigned int func_code;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001153 struct ap_message ap_msg;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001154 unsigned int domain;
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001155 int qid = 0, rc = -ENODEV;
Harald Freudenberger01396a32019-02-22 17:24:11 +01001156 struct module *mod;
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001157
1158 trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001159
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +02001160 ap_init_message(&ap_msg);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001161 rc = get_rng_fc(&ap_msg, &func_code, &domain);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001162 if (rc)
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001163 goto out;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001164
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001165 pref_zc = NULL;
1166 pref_zq = NULL;
1167 spin_lock(&zcrypt_list_lock);
1168 for_each_zcrypt_card(zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +02001169 /* Check for useable CCA card */
1170 if (!zc->online || !zc->card->config ||
1171 !(zc->card->functions & 0x10000000))
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001172 continue;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001173 /* get weight index of the card device */
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001174 wgt = zc->speed_rating[func_code];
1175 if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001176 continue;
1177 for_each_zcrypt_queue(zq, zc) {
Harald Freudenberger4f2fccc2020-07-02 16:57:00 +02001178 /* check if device is useable and eligible */
1179 if (!zq->online || !zq->ops->rng ||
1180 !zq->queue->config)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001181 continue;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001182 if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001183 continue;
1184 pref_zc = zc;
1185 pref_zq = zq;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001186 pref_wgt = wgt;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001187 }
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001188 }
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001189 pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001190 spin_unlock(&zcrypt_list_lock);
1191
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +02001192 if (!pref_zq) {
1193 rc = -ENODEV;
1194 goto out;
1195 }
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001196
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001197 qid = pref_zq->queue->qid;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001198 rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1199
1200 spin_lock(&zcrypt_list_lock);
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001201 zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001202 spin_unlock(&zcrypt_list_lock);
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001203
1204out:
Harald Freudenberger89a0c0e2018-05-28 13:00:42 +02001205 ap_release_message(&ap_msg);
Harald Freudenberger13b251b2016-11-25 18:04:56 +01001206 trace_s390_zcrypt_rep(buffer, func_code, rc,
1207 AP_QID_CARD(qid), AP_QID_QUEUE(qid));
Ingo Tuchscherer34a15162016-08-25 11:14:15 +02001208 return rc;
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001209}
1210
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001211static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001212{
1213 struct zcrypt_card *zc;
1214 struct zcrypt_queue *zq;
1215 struct zcrypt_device_status *stat;
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001216 int card, queue;
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001217
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001218 memset(devstatus, 0, MAX_ZDEV_ENTRIES
1219 * sizeof(struct zcrypt_device_status));
1220
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001221 spin_lock(&zcrypt_list_lock);
1222 for_each_zcrypt_card(zc) {
1223 for_each_zcrypt_queue(zq, zc) {
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001224 card = AP_QID_CARD(zq->queue->qid);
1225 if (card >= MAX_ZDEV_CARDIDS)
1226 continue;
1227 queue = AP_QID_QUEUE(zq->queue->qid);
1228 stat = &devstatus[card * AP_DOMAINS + queue];
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001229 stat->hwtype = zc->card->ap_dev.device_type;
1230 stat->functions = zc->card->functions >> 26;
1231 stat->qid = zq->queue->qid;
1232 stat->online = zq->online ? 0x01 : 0x00;
1233 }
1234 }
1235 spin_unlock(&zcrypt_list_lock);
1236}
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001237
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001238void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001239{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001240 struct zcrypt_card *zc;
1241 struct zcrypt_queue *zq;
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001242 struct zcrypt_device_status_ext *stat;
1243 int card, queue;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001244
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001245 memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1246 * sizeof(struct zcrypt_device_status_ext));
1247
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001248 spin_lock(&zcrypt_list_lock);
1249 for_each_zcrypt_card(zc) {
1250 for_each_zcrypt_queue(zq, zc) {
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001251 card = AP_QID_CARD(zq->queue->qid);
1252 queue = AP_QID_QUEUE(zq->queue->qid);
1253 stat = &devstatus[card * AP_DOMAINS + queue];
1254 stat->hwtype = zc->card->ap_dev.device_type;
1255 stat->functions = zc->card->functions >> 26;
1256 stat->qid = zq->queue->qid;
1257 stat->online = zq->online ? 0x01 : 0x00;
1258 }
1259 }
1260 spin_unlock(&zcrypt_list_lock);
1261}
1262EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1263
Harald Freudenberger4da57a22019-06-18 15:53:12 +02001264int zcrypt_device_status_ext(int card, int queue,
1265 struct zcrypt_device_status_ext *devstat)
1266{
1267 struct zcrypt_card *zc;
1268 struct zcrypt_queue *zq;
1269
1270 memset(devstat, 0, sizeof(*devstat));
1271
1272 spin_lock(&zcrypt_list_lock);
1273 for_each_zcrypt_card(zc) {
1274 for_each_zcrypt_queue(zq, zc) {
1275 if (card == AP_QID_CARD(zq->queue->qid) &&
1276 queue == AP_QID_QUEUE(zq->queue->qid)) {
1277 devstat->hwtype = zc->card->ap_dev.device_type;
1278 devstat->functions = zc->card->functions >> 26;
1279 devstat->qid = zq->queue->qid;
1280 devstat->online = zq->online ? 0x01 : 0x00;
1281 spin_unlock(&zcrypt_list_lock);
1282 return 0;
1283 }
1284 }
1285 }
1286 spin_unlock(&zcrypt_list_lock);
1287
1288 return -ENODEV;
1289}
1290EXPORT_SYMBOL(zcrypt_device_status_ext);
1291
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001292static void zcrypt_status_mask(char status[], size_t max_adapters)
1293{
1294 struct zcrypt_card *zc;
1295 struct zcrypt_queue *zq;
1296 int card;
1297
1298 memset(status, 0, max_adapters);
1299 spin_lock(&zcrypt_list_lock);
1300 for_each_zcrypt_card(zc) {
1301 for_each_zcrypt_queue(zq, zc) {
1302 card = AP_QID_CARD(zq->queue->qid);
1303 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1304 || card >= max_adapters)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001305 continue;
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001306 status[card] = zc->online ? zc->user_space_type : 0x0d;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001307 }
1308 }
1309 spin_unlock(&zcrypt_list_lock);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001310}
1311
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001312static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001313{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001314 struct zcrypt_card *zc;
1315 struct zcrypt_queue *zq;
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001316 int card;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001317
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001318 memset(qdepth, 0, max_adapters);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001319 spin_lock(&zcrypt_list_lock);
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001320 local_bh_disable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001321 for_each_zcrypt_card(zc) {
1322 for_each_zcrypt_queue(zq, zc) {
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001323 card = AP_QID_CARD(zq->queue->qid);
1324 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1325 || card >= max_adapters)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001326 continue;
1327 spin_lock(&zq->queue->lock);
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001328 qdepth[card] =
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001329 zq->queue->pendingq_count +
1330 zq->queue->requestq_count;
1331 spin_unlock(&zq->queue->lock);
1332 }
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001333 }
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001334 local_bh_enable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001335 spin_unlock(&zcrypt_list_lock);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001336}
1337
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +01001338static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001339{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001340 struct zcrypt_card *zc;
1341 struct zcrypt_queue *zq;
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001342 int card;
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +01001343 u64 cnt;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001344
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001345 memset(reqcnt, 0, sizeof(int) * max_adapters);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001346 spin_lock(&zcrypt_list_lock);
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001347 local_bh_disable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001348 for_each_zcrypt_card(zc) {
1349 for_each_zcrypt_queue(zq, zc) {
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001350 card = AP_QID_CARD(zq->queue->qid);
1351 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1352 || card >= max_adapters)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001353 continue;
1354 spin_lock(&zq->queue->lock);
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +01001355 cnt = zq->queue->total_request_count;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001356 spin_unlock(&zq->queue->lock);
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +01001357 reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001358 }
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001359 }
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001360 local_bh_enable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001361 spin_unlock(&zcrypt_list_lock);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001362}
1363
1364static int zcrypt_pendingq_count(void)
1365{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001366 struct zcrypt_card *zc;
1367 struct zcrypt_queue *zq;
1368 int pendingq_count;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001369
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001370 pendingq_count = 0;
1371 spin_lock(&zcrypt_list_lock);
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001372 local_bh_disable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001373 for_each_zcrypt_card(zc) {
1374 for_each_zcrypt_queue(zq, zc) {
1375 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1376 continue;
1377 spin_lock(&zq->queue->lock);
1378 pendingq_count += zq->queue->pendingq_count;
1379 spin_unlock(&zq->queue->lock);
1380 }
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001381 }
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001382 local_bh_enable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001383 spin_unlock(&zcrypt_list_lock);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001384 return pendingq_count;
1385}
1386
1387static int zcrypt_requestq_count(void)
1388{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001389 struct zcrypt_card *zc;
1390 struct zcrypt_queue *zq;
1391 int requestq_count;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001392
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001393 requestq_count = 0;
1394 spin_lock(&zcrypt_list_lock);
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001395 local_bh_disable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001396 for_each_zcrypt_card(zc) {
1397 for_each_zcrypt_queue(zq, zc) {
1398 if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1399 continue;
1400 spin_lock(&zq->queue->lock);
1401 requestq_count += zq->queue->requestq_count;
1402 spin_unlock(&zq->queue->lock);
1403 }
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001404 }
Harald Freudenberger7fbe5c02017-01-16 09:43:29 +01001405 local_bh_enable();
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001406 spin_unlock(&zcrypt_list_lock);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001407 return requestq_count;
1408}
1409
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001410static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1411{
1412 int rc;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001413 struct zcrypt_track tr;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001414 struct ica_rsa_modexpo mex;
1415 struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1416
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001417 memset(&tr, 0, sizeof(tr));
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001418 if (copy_from_user(&mex, umex, sizeof(mex)))
1419 return -EFAULT;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001420
1421#ifdef CONFIG_ZCRYPT_DEBUG
1422 if (mex.inputdatalength & (1U << 31)) {
1423 if (!capable(CAP_SYS_ADMIN))
1424 return -EPERM;
1425 tr.fi.cmd = (u16)(mex.inputdatalength >> 16);
1426 }
1427 mex.inputdatalength &= 0x0000FFFF;
1428#endif
1429
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001430 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001431 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1432 if (rc == -EAGAIN)
1433 tr.again_counter++;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001434#ifdef CONFIG_ZCRYPT_DEBUG
1435 if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1436 break;
1437#endif
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001438 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001439 /* on failure: retry once again after a requested rescan */
1440 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1441 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001442 rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1443 if (rc == -EAGAIN)
1444 tr.again_counter++;
1445 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenbergerd39fae42021-01-21 16:03:08 +01001446 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1447 rc = -EIO;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001448 if (rc) {
1449 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1450 return rc;
1451 }
1452 return put_user(mex.outputdatalength, &umex->outputdatalength);
1453}
1454
1455static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1456{
1457 int rc;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001458 struct zcrypt_track tr;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001459 struct ica_rsa_modexpo_crt crt;
1460 struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1461
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001462 memset(&tr, 0, sizeof(tr));
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001463 if (copy_from_user(&crt, ucrt, sizeof(crt)))
1464 return -EFAULT;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001465
1466#ifdef CONFIG_ZCRYPT_DEBUG
1467 if (crt.inputdatalength & (1U << 31)) {
1468 if (!capable(CAP_SYS_ADMIN))
1469 return -EPERM;
1470 tr.fi.cmd = (u16)(crt.inputdatalength >> 16);
1471 }
1472 crt.inputdatalength &= 0x0000FFFF;
1473#endif
1474
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001475 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001476 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1477 if (rc == -EAGAIN)
1478 tr.again_counter++;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001479#ifdef CONFIG_ZCRYPT_DEBUG
1480 if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1481 break;
1482#endif
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001483 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001484 /* on failure: retry once again after a requested rescan */
1485 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1486 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001487 rc = zcrypt_rsa_crt(perms, &tr, &crt);
1488 if (rc == -EAGAIN)
1489 tr.again_counter++;
1490 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenbergerd39fae42021-01-21 16:03:08 +01001491 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1492 rc = -EIO;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001493 if (rc) {
1494 ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1495 return rc;
1496 }
1497 return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1498}
1499
1500static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1501{
1502 int rc;
1503 struct ica_xcRB xcRB;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001504 struct zcrypt_track tr;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001505 struct ica_xcRB __user *uxcRB = (void __user *) arg;
1506
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001507 memset(&tr, 0, sizeof(tr));
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001508 if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1509 return -EFAULT;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001510
1511#ifdef CONFIG_ZCRYPT_DEBUG
1512 if (xcRB.status & (1U << 31)) {
1513 if (!capable(CAP_SYS_ADMIN))
1514 return -EPERM;
1515 tr.fi.cmd = (u16)(xcRB.status >> 16);
1516 }
1517 xcRB.status &= 0x0000FFFF;
1518#endif
1519
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001520 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001521 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1522 if (rc == -EAGAIN)
1523 tr.again_counter++;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001524#ifdef CONFIG_ZCRYPT_DEBUG
1525 if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1526 break;
1527#endif
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001528 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001529 /* on failure: retry once again after a requested rescan */
1530 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1531 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001532 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1533 if (rc == -EAGAIN)
1534 tr.again_counter++;
1535 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenbergerd39fae42021-01-21 16:03:08 +01001536 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1537 rc = -EIO;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001538 if (rc)
1539 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1540 rc, xcRB.status);
1541 if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1542 return -EFAULT;
1543 return rc;
1544}
1545
1546static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1547{
1548 int rc;
1549 struct ep11_urb xcrb;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001550 struct zcrypt_track tr;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001551 struct ep11_urb __user *uxcrb = (void __user *)arg;
1552
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001553 memset(&tr, 0, sizeof(tr));
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001554 if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1555 return -EFAULT;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001556
1557#ifdef CONFIG_ZCRYPT_DEBUG
1558 if (xcrb.req_len & (1ULL << 63)) {
1559 if (!capable(CAP_SYS_ADMIN))
1560 return -EPERM;
1561 tr.fi.cmd = (u16)(xcrb.req_len >> 48);
1562 }
1563 xcrb.req_len &= 0x0000FFFFFFFFFFFFULL;
1564#endif
1565
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001566 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001567 rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1568 if (rc == -EAGAIN)
1569 tr.again_counter++;
Harald Freudenberger27c4f672020-09-29 16:07:22 +02001570#ifdef CONFIG_ZCRYPT_DEBUG
1571 if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1572 break;
1573#endif
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001574 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001575 /* on failure: retry once again after a requested rescan */
1576 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1577 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001578 rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1579 if (rc == -EAGAIN)
1580 tr.again_counter++;
1581 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenbergerd39fae42021-01-21 16:03:08 +01001582 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1583 rc = -EIO;
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001584 if (rc)
1585 ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1586 if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1587 return -EFAULT;
1588 return rc;
1589}
1590
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001591static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1592 unsigned long arg)
1593{
Harald Freudenberger00fab232018-09-17 16:18:41 +02001594 int rc;
1595 struct ap_perms *perms =
1596 (struct ap_perms *) filp->private_data;
1597
1598 rc = zcrypt_check_ioctl(perms, cmd);
1599 if (rc)
1600 return rc;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001601
1602 switch (cmd) {
Harald Freudenberger7e202ac2020-05-20 16:07:19 +02001603 case ICARSAMODEXPO:
1604 return icarsamodexpo_ioctl(perms, arg);
1605 case ICARSACRT:
1606 return icarsacrt_ioctl(perms, arg);
1607 case ZSECSENDCPRB:
1608 return zsecsendcprb_ioctl(perms, arg);
1609 case ZSENDEP11CPRB:
1610 return zsendep11cprb_ioctl(perms, arg);
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001611 case ZCRYPT_DEVICE_STATUS: {
1612 struct zcrypt_device_status_ext *device_status;
1613 size_t total_size = MAX_ZDEV_ENTRIES_EXT
1614 * sizeof(struct zcrypt_device_status_ext);
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001615
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001616 device_status = kzalloc(total_size, GFP_KERNEL);
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001617 if (!device_status)
1618 return -ENOMEM;
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001619 zcrypt_device_status_mask_ext(device_status);
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001620 if (copy_to_user((char __user *) arg, device_status,
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001621 total_size))
1622 rc = -EFAULT;
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001623 kfree(device_status);
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001624 return rc;
Ingo Tuchschererb886a9d2016-08-25 11:19:58 +02001625 }
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001626 case ZCRYPT_STATUS_MASK: {
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001627 char status[AP_DEVICES];
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001628
1629 zcrypt_status_mask(status, AP_DEVICES);
1630 if (copy_to_user((char __user *) arg, status, sizeof(status)))
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001631 return -EFAULT;
1632 return 0;
1633 }
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001634 case ZCRYPT_QDEPTH_MASK: {
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001635 char qdepth[AP_DEVICES];
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001636
1637 zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1638 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001639 return -EFAULT;
1640 return 0;
1641 }
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001642 case ZCRYPT_PERDEV_REQCNT: {
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +01001643 u32 *reqcnt;
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001644
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +01001645 reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001646 if (!reqcnt)
1647 return -ENOMEM;
1648 zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
Christian Borntraegerf7e80982020-09-21 12:48:36 +02001649 if (copy_to_user((int __user *) arg, reqcnt,
1650 sizeof(u32) * AP_DEVICES))
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001651 rc = -EFAULT;
1652 kfree(reqcnt);
1653 return rc;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001654 }
1655 case Z90STAT_REQUESTQ_COUNT:
1656 return put_user(zcrypt_requestq_count(), (int __user *) arg);
1657 case Z90STAT_PENDINGQ_COUNT:
1658 return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1659 case Z90STAT_TOTALOPEN_COUNT:
1660 return put_user(atomic_read(&zcrypt_open_count),
1661 (int __user *) arg);
1662 case Z90STAT_DOMAIN_INDEX:
1663 return put_user(ap_domain_index, (int __user *) arg);
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001664 /*
1665 * Deprecated ioctls
1666 */
1667 case ZDEVICESTATUS: {
1668 /* the old ioctl supports only 64 adapters */
1669 struct zcrypt_device_status *device_status;
1670 size_t total_size = MAX_ZDEV_ENTRIES
1671 * sizeof(struct zcrypt_device_status);
1672
1673 device_status = kzalloc(total_size, GFP_KERNEL);
1674 if (!device_status)
1675 return -ENOMEM;
1676 zcrypt_device_status_mask(device_status);
1677 if (copy_to_user((char __user *) arg, device_status,
1678 total_size))
1679 rc = -EFAULT;
1680 kfree(device_status);
1681 return rc;
1682 }
1683 case Z90STAT_STATUS_MASK: {
1684 /* the old ioctl supports only 64 adapters */
1685 char status[MAX_ZDEV_CARDIDS];
1686
1687 zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1688 if (copy_to_user((char __user *) arg, status, sizeof(status)))
1689 return -EFAULT;
1690 return 0;
1691 }
1692 case Z90STAT_QDEPTH_MASK: {
1693 /* the old ioctl supports only 64 adapters */
1694 char qdepth[MAX_ZDEV_CARDIDS];
1695
1696 zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1697 if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1698 return -EFAULT;
1699 return 0;
1700 }
1701 case Z90STAT_PERDEV_REQCNT: {
1702 /* the old ioctl supports only 64 adapters */
Harald Freudenbergerfcd98d42019-12-20 16:02:54 +01001703 u32 reqcnt[MAX_ZDEV_CARDIDS];
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001704
1705 zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1706 if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1707 return -EFAULT;
1708 return 0;
1709 }
Harald Freudenberger2a807862018-04-04 13:25:40 +02001710 /* unknown ioctl number */
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001711 default:
Harald Freudenbergeraf4a7222018-04-09 16:18:37 +02001712 ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001713 return -ENOIOCTLCMD;
1714 }
1715}
1716
1717#ifdef CONFIG_COMPAT
Felix Beck1749a812008-04-17 07:46:28 +02001718/*
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001719 * ioctl32 conversion routines
1720 */
1721struct compat_ica_rsa_modexpo {
1722 compat_uptr_t inputdata;
1723 unsigned int inputdatalength;
1724 compat_uptr_t outputdata;
1725 unsigned int outputdatalength;
1726 compat_uptr_t b_key;
1727 compat_uptr_t n_modulus;
1728};
1729
Harald Freudenberger00fab232018-09-17 16:18:41 +02001730static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1731 unsigned int cmd, unsigned long arg)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001732{
1733 struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1734 struct compat_ica_rsa_modexpo mex32;
1735 struct ica_rsa_modexpo mex64;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001736 struct zcrypt_track tr;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001737 long rc;
1738
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001739 memset(&tr, 0, sizeof(tr));
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001740 if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1741 return -EFAULT;
1742 mex64.inputdata = compat_ptr(mex32.inputdata);
1743 mex64.inputdatalength = mex32.inputdatalength;
1744 mex64.outputdata = compat_ptr(mex32.outputdata);
1745 mex64.outputdatalength = mex32.outputdatalength;
1746 mex64.b_key = compat_ptr(mex32.b_key);
1747 mex64.n_modulus = compat_ptr(mex32.n_modulus);
1748 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001749 rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1750 if (rc == -EAGAIN)
1751 tr.again_counter++;
1752 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Holger Denglerdabecb22012-09-10 21:34:26 +02001753 /* on failure: retry once again after a requested rescan */
1754 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1755 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001756 rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1757 if (rc == -EAGAIN)
1758 tr.again_counter++;
1759 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenbergerd39fae42021-01-21 16:03:08 +01001760 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1761 rc = -EIO;
Holger Denglerdabecb22012-09-10 21:34:26 +02001762 if (rc)
1763 return rc;
1764 return put_user(mex64.outputdatalength,
1765 &umex32->outputdatalength);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001766}
1767
1768struct compat_ica_rsa_modexpo_crt {
1769 compat_uptr_t inputdata;
1770 unsigned int inputdatalength;
1771 compat_uptr_t outputdata;
1772 unsigned int outputdatalength;
1773 compat_uptr_t bp_key;
1774 compat_uptr_t bq_key;
1775 compat_uptr_t np_prime;
1776 compat_uptr_t nq_prime;
1777 compat_uptr_t u_mult_inv;
1778};
1779
Harald Freudenberger00fab232018-09-17 16:18:41 +02001780static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1781 unsigned int cmd, unsigned long arg)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001782{
1783 struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1784 struct compat_ica_rsa_modexpo_crt crt32;
1785 struct ica_rsa_modexpo_crt crt64;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001786 struct zcrypt_track tr;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001787 long rc;
1788
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001789 memset(&tr, 0, sizeof(tr));
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001790 if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1791 return -EFAULT;
1792 crt64.inputdata = compat_ptr(crt32.inputdata);
1793 crt64.inputdatalength = crt32.inputdatalength;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001794 crt64.outputdata = compat_ptr(crt32.outputdata);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001795 crt64.outputdatalength = crt32.outputdatalength;
1796 crt64.bp_key = compat_ptr(crt32.bp_key);
1797 crt64.bq_key = compat_ptr(crt32.bq_key);
1798 crt64.np_prime = compat_ptr(crt32.np_prime);
1799 crt64.nq_prime = compat_ptr(crt32.nq_prime);
1800 crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1801 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001802 rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1803 if (rc == -EAGAIN)
1804 tr.again_counter++;
1805 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Holger Denglerdabecb22012-09-10 21:34:26 +02001806 /* on failure: retry once again after a requested rescan */
1807 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1808 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001809 rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1810 if (rc == -EAGAIN)
1811 tr.again_counter++;
1812 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenbergerd39fae42021-01-21 16:03:08 +01001813 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1814 rc = -EIO;
Holger Denglerdabecb22012-09-10 21:34:26 +02001815 if (rc)
1816 return rc;
1817 return put_user(crt64.outputdatalength,
1818 &ucrt32->outputdatalength);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001819}
1820
Ralph Wuerthner54321142006-09-20 15:58:36 +02001821struct compat_ica_xcRB {
1822 unsigned short agent_ID;
1823 unsigned int user_defined;
1824 unsigned short request_ID;
1825 unsigned int request_control_blk_length;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001826 unsigned char padding1[16 - sizeof(compat_uptr_t)];
Ralph Wuerthner54321142006-09-20 15:58:36 +02001827 compat_uptr_t request_control_blk_addr;
1828 unsigned int request_data_length;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001829 char padding2[16 - sizeof(compat_uptr_t)];
Ralph Wuerthner54321142006-09-20 15:58:36 +02001830 compat_uptr_t request_data_address;
1831 unsigned int reply_control_blk_length;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001832 char padding3[16 - sizeof(compat_uptr_t)];
Ralph Wuerthner54321142006-09-20 15:58:36 +02001833 compat_uptr_t reply_control_blk_addr;
1834 unsigned int reply_data_length;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001835 char padding4[16 - sizeof(compat_uptr_t)];
Ralph Wuerthner54321142006-09-20 15:58:36 +02001836 compat_uptr_t reply_data_addr;
1837 unsigned short priority_window;
1838 unsigned int status;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001839} __packed;
Ralph Wuerthner54321142006-09-20 15:58:36 +02001840
Harald Freudenberger00fab232018-09-17 16:18:41 +02001841static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1842 unsigned int cmd, unsigned long arg)
Ralph Wuerthner54321142006-09-20 15:58:36 +02001843{
1844 struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1845 struct compat_ica_xcRB xcRB32;
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001846 struct zcrypt_track tr;
Ralph Wuerthner54321142006-09-20 15:58:36 +02001847 struct ica_xcRB xcRB64;
1848 long rc;
1849
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001850 memset(&tr, 0, sizeof(tr));
Ralph Wuerthner54321142006-09-20 15:58:36 +02001851 if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1852 return -EFAULT;
1853 xcRB64.agent_ID = xcRB32.agent_ID;
1854 xcRB64.user_defined = xcRB32.user_defined;
1855 xcRB64.request_ID = xcRB32.request_ID;
1856 xcRB64.request_control_blk_length =
1857 xcRB32.request_control_blk_length;
1858 xcRB64.request_control_blk_addr =
1859 compat_ptr(xcRB32.request_control_blk_addr);
1860 xcRB64.request_data_length =
1861 xcRB32.request_data_length;
1862 xcRB64.request_data_address =
1863 compat_ptr(xcRB32.request_data_address);
1864 xcRB64.reply_control_blk_length =
1865 xcRB32.reply_control_blk_length;
1866 xcRB64.reply_control_blk_addr =
1867 compat_ptr(xcRB32.reply_control_blk_addr);
1868 xcRB64.reply_data_length = xcRB32.reply_data_length;
1869 xcRB64.reply_data_addr =
1870 compat_ptr(xcRB32.reply_data_addr);
1871 xcRB64.priority_window = xcRB32.priority_window;
1872 xcRB64.status = xcRB32.status;
1873 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001874 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1875 if (rc == -EAGAIN)
1876 tr.again_counter++;
1877 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Holger Denglerdabecb22012-09-10 21:34:26 +02001878 /* on failure: retry once again after a requested rescan */
1879 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1880 do {
Harald Freudenberger91ffc512020-07-02 11:10:11 +02001881 rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1882 if (rc == -EAGAIN)
1883 tr.again_counter++;
1884 } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
Harald Freudenbergerd39fae42021-01-21 16:03:08 +01001885 if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1886 rc = -EIO;
Ralph Wuerthner54321142006-09-20 15:58:36 +02001887 xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1888 xcRB32.reply_data_length = xcRB64.reply_data_length;
1889 xcRB32.status = xcRB64.status;
1890 if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001891 return -EFAULT;
Ralph Wuerthner54321142006-09-20 15:58:36 +02001892 return rc;
1893}
1894
Heiko Carstens2b67fc42007-02-05 21:16:47 +01001895static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001896 unsigned long arg)
1897{
Harald Freudenberger00fab232018-09-17 16:18:41 +02001898 int rc;
1899 struct ap_perms *perms =
1900 (struct ap_perms *) filp->private_data;
1901
1902 rc = zcrypt_check_ioctl(perms, cmd);
1903 if (rc)
1904 return rc;
1905
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001906 if (cmd == ICARSAMODEXPO)
Harald Freudenberger00fab232018-09-17 16:18:41 +02001907 return trans_modexpo32(perms, filp, cmd, arg);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001908 if (cmd == ICARSACRT)
Harald Freudenberger00fab232018-09-17 16:18:41 +02001909 return trans_modexpo_crt32(perms, filp, cmd, arg);
Ralph Wuerthner54321142006-09-20 15:58:36 +02001910 if (cmd == ZSECSENDCPRB)
Harald Freudenberger00fab232018-09-17 16:18:41 +02001911 return trans_xcRB32(perms, filp, cmd, arg);
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001912 return zcrypt_unlocked_ioctl(filp, cmd, arg);
1913}
1914#endif
1915
Felix Beck1749a812008-04-17 07:46:28 +02001916/*
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001917 * Misc device file operations.
1918 */
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001919static const struct file_operations zcrypt_fops = {
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001920 .owner = THIS_MODULE,
1921 .read = zcrypt_read,
1922 .write = zcrypt_write,
1923 .unlocked_ioctl = zcrypt_unlocked_ioctl,
1924#ifdef CONFIG_COMPAT
1925 .compat_ioctl = zcrypt_compat_ioctl,
1926#endif
1927 .open = zcrypt_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001928 .release = zcrypt_release,
1929 .llseek = no_llseek,
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001930};
1931
Felix Beck1749a812008-04-17 07:46:28 +02001932/*
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02001933 * Misc device.
1934 */
1935static struct miscdevice zcrypt_misc_device = {
1936 .minor = MISC_DYNAMIC_MINOR,
1937 .name = "z90crypt",
1938 .fops = &zcrypt_fops,
1939};
1940
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001941static int zcrypt_rng_device_count;
1942static u32 *zcrypt_rng_buffer;
1943static int zcrypt_rng_buffer_index;
1944static DEFINE_MUTEX(zcrypt_rng_mutex);
1945
1946static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1947{
1948 int rc;
1949
Felix Beck1749a812008-04-17 07:46:28 +02001950 /*
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001951 * We don't need locking here because the RNG API guarantees serialized
1952 * read method calls.
1953 */
1954 if (zcrypt_rng_buffer_index == 0) {
1955 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
Holger Denglerdabecb22012-09-10 21:34:26 +02001956 /* on failure: retry once again after a requested rescan */
1957 if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1958 rc = zcrypt_rng((char *) zcrypt_rng_buffer);
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001959 if (rc < 0)
1960 return -EIO;
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001961 zcrypt_rng_buffer_index = rc / sizeof(*data);
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001962 }
1963 *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +02001964 return sizeof(*data);
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001965}
1966
1967static struct hwrng zcrypt_rng_dev = {
1968 .name = "zcrypt",
1969 .data_read = zcrypt_rng_data_read,
Ingo Tuchschererdb490cb2015-03-17 16:02:20 +01001970 .quality = 990,
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001971};
1972
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001973int zcrypt_rng_device_add(void)
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001974{
1975 int rc = 0;
1976
1977 mutex_lock(&zcrypt_rng_mutex);
1978 if (zcrypt_rng_device_count == 0) {
1979 zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1980 if (!zcrypt_rng_buffer) {
1981 rc = -ENOMEM;
1982 goto out;
1983 }
1984 zcrypt_rng_buffer_index = 0;
Ingo Tuchschererdb490cb2015-03-17 16:02:20 +01001985 if (!zcrypt_hwrng_seed)
1986 zcrypt_rng_dev.quality = 0;
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02001987 rc = hwrng_register(&zcrypt_rng_dev);
1988 if (rc)
1989 goto out_free;
1990 zcrypt_rng_device_count = 1;
1991 } else
1992 zcrypt_rng_device_count++;
1993 mutex_unlock(&zcrypt_rng_mutex);
1994 return 0;
1995
1996out_free:
1997 free_page((unsigned long) zcrypt_rng_buffer);
1998out:
1999 mutex_unlock(&zcrypt_rng_mutex);
2000 return rc;
2001}
2002
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02002003void zcrypt_rng_device_remove(void)
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +02002004{
2005 mutex_lock(&zcrypt_rng_mutex);
2006 zcrypt_rng_device_count--;
2007 if (zcrypt_rng_device_count == 0) {
2008 hwrng_unregister(&zcrypt_rng_dev);
2009 free_page((unsigned long) zcrypt_rng_buffer);
2010 }
2011 mutex_unlock(&zcrypt_rng_mutex);
2012}
2013
Harald Freudenberger43cb5a72020-09-01 12:48:14 +02002014/*
2015 * Wait until the zcrypt api is operational.
2016 * The AP bus scan and the binding of ap devices to device drivers is
2017 * an asynchronous job. This function waits until these initial jobs
2018 * are done and so the zcrypt api should be ready to serve crypto
2019 * requests - if there are resources available. The function uses an
2020 * internal timeout of 60s. The very first caller will either wait for
2021 * ap bus bindings complete or the timeout happens. This state will be
2022 * remembered for further callers which will only be blocked until a
2023 * decision is made (timeout or bindings complete).
2024 * On timeout -ETIME is returned, on success the return value is 0.
2025 */
2026int zcrypt_wait_api_operational(void)
2027{
2028 static DEFINE_MUTEX(zcrypt_wait_api_lock);
2029 static int zcrypt_wait_api_state;
2030 int rc;
2031
2032 rc = mutex_lock_interruptible(&zcrypt_wait_api_lock);
2033 if (rc)
2034 return rc;
2035
2036 switch (zcrypt_wait_api_state) {
2037 case 0:
2038 /* initial state, invoke wait for the ap bus complete */
2039 rc = ap_wait_init_apqn_bindings_complete(
2040 msecs_to_jiffies(60 * 1000));
2041 switch (rc) {
2042 case 0:
2043 /* ap bus bindings are complete */
2044 zcrypt_wait_api_state = 1;
2045 break;
2046 case -EINTR:
2047 /* interrupted, go back to caller */
2048 break;
2049 case -ETIME:
2050 /* timeout */
2051 ZCRYPT_DBF(DBF_WARN,
2052 "%s ap_wait_init_apqn_bindings_complete() returned with ETIME\n",
2053 __func__);
2054 zcrypt_wait_api_state = -ETIME;
2055 break;
2056 default:
2057 /* other failure */
2058 ZCRYPT_DBF(DBF_DEBUG,
2059 "%s ap_wait_init_apqn_bindings_complete() failure rc=%d\n",
2060 __func__, rc);
2061 break;
2062 }
2063 break;
2064 case 1:
2065 /* a previous caller already found ap bus bindings complete */
2066 rc = 0;
2067 break;
2068 default:
2069 /* a previous caller had timeout or other failure */
2070 rc = zcrypt_wait_api_state;
2071 break;
2072 }
2073
2074 mutex_unlock(&zcrypt_wait_api_lock);
2075
2076 return rc;
2077}
2078EXPORT_SYMBOL(zcrypt_wait_api_operational);
2079
Holger Denglerdabecb22012-09-10 21:34:26 +02002080int __init zcrypt_debug_init(void)
2081{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01002082 zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
2083 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2084 debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
2085 debug_set_level(zcrypt_dbf_info, DBF_ERR);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02002086
Holger Denglerdabecb22012-09-10 21:34:26 +02002087 return 0;
2088}
2089
2090void zcrypt_debug_exit(void)
2091{
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01002092 debug_unregister(zcrypt_dbf_info);
Holger Denglerdabecb22012-09-10 21:34:26 +02002093}
2094
Harald Freudenberger00fab232018-09-17 16:18:41 +02002095#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2096
2097static int __init zcdn_init(void)
2098{
2099 int rc;
2100
2101 /* create a new class 'zcrypt' */
2102 zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
2103 if (IS_ERR(zcrypt_class)) {
2104 rc = PTR_ERR(zcrypt_class);
2105 goto out_class_create_failed;
2106 }
2107 zcrypt_class->dev_release = zcdn_device_release;
2108
2109 /* alloc device minor range */
2110 rc = alloc_chrdev_region(&zcrypt_devt,
2111 0, ZCRYPT_MAX_MINOR_NODES,
2112 ZCRYPT_NAME);
2113 if (rc)
2114 goto out_alloc_chrdev_failed;
2115
2116 cdev_init(&zcrypt_cdev, &zcrypt_fops);
2117 zcrypt_cdev.owner = THIS_MODULE;
2118 rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2119 if (rc)
2120 goto out_cdev_add_failed;
2121
2122 /* need some class specific sysfs attributes */
2123 rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
2124 if (rc)
2125 goto out_class_create_file_1_failed;
2126 rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
2127 if (rc)
2128 goto out_class_create_file_2_failed;
2129
2130 return 0;
2131
2132out_class_create_file_2_failed:
2133 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
2134out_class_create_file_1_failed:
2135 cdev_del(&zcrypt_cdev);
2136out_cdev_add_failed:
2137 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2138out_alloc_chrdev_failed:
2139 class_destroy(zcrypt_class);
2140out_class_create_failed:
2141 return rc;
2142}
2143
2144static void zcdn_exit(void)
2145{
2146 class_remove_file(zcrypt_class, &class_attr_zcdn_create);
2147 class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
2148 zcdn_destroy_all();
2149 cdev_del(&zcrypt_cdev);
2150 unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2151 class_destroy(zcrypt_class);
2152}
2153
2154#endif
2155
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002156/**
Felix Beck1749a812008-04-17 07:46:28 +02002157 * zcrypt_api_init(): Module initialization.
2158 *
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002159 * The module initialization code.
2160 */
2161int __init zcrypt_api_init(void)
2162{
2163 int rc;
2164
Holger Denglerdabecb22012-09-10 21:34:26 +02002165 rc = zcrypt_debug_init();
2166 if (rc)
2167 goto out;
2168
Harald Freudenberger00fab232018-09-17 16:18:41 +02002169#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2170 rc = zcdn_init();
2171 if (rc)
2172 goto out;
2173#endif
2174
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002175 /* Register the request sprayer. */
2176 rc = misc_register(&zcrypt_misc_device);
Felix Beck1a89dd82008-07-14 09:59:27 +02002177 if (rc < 0)
Harald Freudenberger00fab232018-09-17 16:18:41 +02002178 goto out_misc_register_failed;
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002179
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02002180 zcrypt_msgtype6_init();
2181 zcrypt_msgtype50_init();
Harald Freudenberger00fab232018-09-17 16:18:41 +02002182
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002183 return 0;
2184
Harald Freudenberger00fab232018-09-17 16:18:41 +02002185out_misc_register_failed:
2186#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2187 zcdn_exit();
2188#endif
2189 zcrypt_debug_exit();
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002190out:
2191 return rc;
2192}
2193
2194/**
Felix Beck1749a812008-04-17 07:46:28 +02002195 * zcrypt_api_exit(): Module termination.
2196 *
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002197 * The module termination code.
2198 */
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02002199void __exit zcrypt_api_exit(void)
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002200{
Harald Freudenberger00fab232018-09-17 16:18:41 +02002201#ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2202 zcdn_exit();
2203#endif
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002204 misc_deregister(&zcrypt_misc_device);
Ingo Tuchschererfc1d3f02016-08-25 11:11:30 +02002205 zcrypt_msgtype6_exit();
2206 zcrypt_msgtype50_exit();
Harald Freudenbergerefc598e2019-06-11 11:16:56 +02002207 zcrypt_ccamisc_exit();
Harald Freudenberger7384eb72019-08-30 16:07:08 +02002208 zcrypt_ep11misc_exit();
Harald Freudenbergercccd85b2016-11-24 06:45:21 +01002209 zcrypt_debug_exit();
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002210}
2211
Martin Schwidefsky2dbc2412006-09-20 15:58:27 +02002212module_init(zcrypt_api_init);
2213module_exit(zcrypt_api_exit);