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