• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 		kfree(zcdndev);
403 		rc = -EINVAL;
404 		goto unlockout;
405 	}
406 	rc = device_register(&zcdndev->device);
407 	if (rc) {
408 		put_device(&zcdndev->device);
409 		goto unlockout;
410 	}
411 
412 	ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
413 		   MAJOR(devt), MINOR(devt));
414 
415 unlockout:
416 	mutex_unlock(&ap_perms_mutex);
417 	return rc;
418 }
419 
zcdn_destroy(const char * name)420 static int zcdn_destroy(const char *name)
421 {
422 	int rc = 0;
423 	struct zcdn_device *zcdndev;
424 
425 	if (mutex_lock_interruptible(&ap_perms_mutex))
426 		return -ERESTARTSYS;
427 
428 	/* try to find this zcdn device */
429 	zcdndev = find_zcdndev_by_name(name);
430 	if (!zcdndev) {
431 		rc = -ENOENT;
432 		goto unlockout;
433 	}
434 
435 	/*
436 	 * The zcdn device is not hard destroyed. It is subject to
437 	 * reference counting and thus just needs to be unregistered.
438 	 */
439 	put_device(&zcdndev->device);
440 	device_unregister(&zcdndev->device);
441 
442 unlockout:
443 	mutex_unlock(&ap_perms_mutex);
444 	return rc;
445 }
446 
zcdn_destroy_all(void)447 static void zcdn_destroy_all(void)
448 {
449 	int i;
450 	dev_t devt;
451 	struct zcdn_device *zcdndev;
452 
453 	mutex_lock(&ap_perms_mutex);
454 	for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
455 		devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
456 		zcdndev = find_zcdndev_by_devt(devt);
457 		if (zcdndev) {
458 			put_device(&zcdndev->device);
459 			device_unregister(&zcdndev->device);
460 		}
461 	}
462 	mutex_unlock(&ap_perms_mutex);
463 }
464 
465 #endif
466 
467 /**
468  * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
469  *
470  * This function is not supported beyond zcrypt 1.3.1.
471  */
zcrypt_read(struct file * filp,char __user * buf,size_t count,loff_t * f_pos)472 static ssize_t zcrypt_read(struct file *filp, char __user *buf,
473 			   size_t count, loff_t *f_pos)
474 {
475 	return -EPERM;
476 }
477 
478 /**
479  * zcrypt_write(): Not allowed.
480  *
481  * Write is is not allowed
482  */
zcrypt_write(struct file * filp,const char __user * buf,size_t count,loff_t * f_pos)483 static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
484 			    size_t count, loff_t *f_pos)
485 {
486 	return -EPERM;
487 }
488 
489 /**
490  * zcrypt_open(): Count number of users.
491  *
492  * Device open function to count number of users.
493  */
zcrypt_open(struct inode * inode,struct file * filp)494 static int zcrypt_open(struct inode *inode, struct file *filp)
495 {
496 	struct ap_perms *perms = &ap_perms;
497 
498 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
499 	if (filp->f_inode->i_cdev == &zcrypt_cdev) {
500 		struct zcdn_device *zcdndev;
501 
502 		if (mutex_lock_interruptible(&ap_perms_mutex))
503 			return -ERESTARTSYS;
504 		zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
505 		/* find returns a reference, no get_device() needed */
506 		mutex_unlock(&ap_perms_mutex);
507 		if (zcdndev)
508 			perms = &zcdndev->perms;
509 	}
510 #endif
511 	filp->private_data = (void *) perms;
512 
513 	atomic_inc(&zcrypt_open_count);
514 	return stream_open(inode, filp);
515 }
516 
517 /**
518  * zcrypt_release(): Count number of users.
519  *
520  * Device close function to count number of users.
521  */
zcrypt_release(struct inode * inode,struct file * filp)522 static int zcrypt_release(struct inode *inode, struct file *filp)
523 {
524 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
525 	if (filp->f_inode->i_cdev == &zcrypt_cdev) {
526 		struct zcdn_device *zcdndev;
527 
528 		mutex_lock(&ap_perms_mutex);
529 		zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
530 		mutex_unlock(&ap_perms_mutex);
531 		if (zcdndev) {
532 			/* 2 puts here: one for find, one for open */
533 			put_device(&zcdndev->device);
534 			put_device(&zcdndev->device);
535 		}
536 	}
537 #endif
538 
539 	atomic_dec(&zcrypt_open_count);
540 	return 0;
541 }
542 
zcrypt_check_ioctl(struct ap_perms * perms,unsigned int cmd)543 static inline int zcrypt_check_ioctl(struct ap_perms *perms,
544 				     unsigned int cmd)
545 {
546 	int rc = -EPERM;
547 	int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
548 
549 	if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
550 		if (test_bit_inv(ioctlnr, perms->ioctlm))
551 			rc = 0;
552 	}
553 
554 	if (rc)
555 		ZCRYPT_DBF(DBF_WARN,
556 			   "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
557 			   ioctlnr, rc);
558 
559 	return rc;
560 }
561 
zcrypt_check_card(struct ap_perms * perms,int card)562 static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
563 {
564 	return test_bit_inv(card, perms->apm) ? true : false;
565 }
566 
zcrypt_check_queue(struct ap_perms * perms,int queue)567 static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
568 {
569 	return test_bit_inv(queue, perms->aqm) ? true : false;
570 }
571 
zcrypt_pick_queue(struct zcrypt_card * zc,struct zcrypt_queue * zq,struct module ** pmod,unsigned int weight)572 static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
573 						     struct zcrypt_queue *zq,
574 						     struct module **pmod,
575 						     unsigned int weight)
576 {
577 	if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
578 		return NULL;
579 	zcrypt_queue_get(zq);
580 	get_device(&zq->queue->ap_dev.device);
581 	atomic_add(weight, &zc->load);
582 	atomic_add(weight, &zq->load);
583 	zq->request_count++;
584 	*pmod = zq->queue->ap_dev.drv->driver.owner;
585 	return zq;
586 }
587 
zcrypt_drop_queue(struct zcrypt_card * zc,struct zcrypt_queue * zq,struct module * mod,unsigned int weight)588 static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
589 				     struct zcrypt_queue *zq,
590 				     struct module *mod,
591 				     unsigned int weight)
592 {
593 	zq->request_count--;
594 	atomic_sub(weight, &zc->load);
595 	atomic_sub(weight, &zq->load);
596 	put_device(&zq->queue->ap_dev.device);
597 	zcrypt_queue_put(zq);
598 	module_put(mod);
599 }
600 
zcrypt_card_compare(struct zcrypt_card * zc,struct zcrypt_card * pref_zc,unsigned int weight,unsigned int pref_weight)601 static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
602 				       struct zcrypt_card *pref_zc,
603 				       unsigned int weight,
604 				       unsigned int pref_weight)
605 {
606 	if (!pref_zc)
607 		return true;
608 	weight += atomic_read(&zc->load);
609 	pref_weight += atomic_read(&pref_zc->load);
610 	if (weight == pref_weight)
611 		return atomic64_read(&zc->card->total_request_count) <
612 			atomic64_read(&pref_zc->card->total_request_count);
613 	return weight < pref_weight;
614 }
615 
zcrypt_queue_compare(struct zcrypt_queue * zq,struct zcrypt_queue * pref_zq,unsigned int weight,unsigned int pref_weight)616 static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
617 					struct zcrypt_queue *pref_zq,
618 					unsigned int weight,
619 					unsigned int pref_weight)
620 {
621 	if (!pref_zq)
622 		return true;
623 	weight += atomic_read(&zq->load);
624 	pref_weight += atomic_read(&pref_zq->load);
625 	if (weight == pref_weight)
626 		return zq->queue->total_request_count <
627 			pref_zq->queue->total_request_count;
628 	return weight < pref_weight;
629 }
630 
631 /*
632  * zcrypt ioctls.
633  */
zcrypt_rsa_modexpo(struct ap_perms * perms,struct zcrypt_track * tr,struct ica_rsa_modexpo * mex)634 static long zcrypt_rsa_modexpo(struct ap_perms *perms,
635 			       struct zcrypt_track *tr,
636 			       struct ica_rsa_modexpo *mex)
637 {
638 	struct zcrypt_card *zc, *pref_zc;
639 	struct zcrypt_queue *zq, *pref_zq;
640 	struct ap_message ap_msg;
641 	unsigned int wgt = 0, pref_wgt = 0;
642 	unsigned int func_code;
643 	int cpen, qpen, qid = 0, rc = -ENODEV;
644 	struct module *mod;
645 
646 	trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
647 
648 	ap_init_message(&ap_msg);
649 
650 #ifdef CONFIG_ZCRYPT_DEBUG
651 	if (tr && tr->fi.cmd)
652 		ap_msg.fi.cmd = tr->fi.cmd;
653 #endif
654 
655 	if (mex->outputdatalength < mex->inputdatalength) {
656 		func_code = 0;
657 		rc = -EINVAL;
658 		goto out;
659 	}
660 
661 	/*
662 	 * As long as outputdatalength is big enough, we can set the
663 	 * outputdatalength equal to the inputdatalength, since that is the
664 	 * number of bytes we will copy in any case
665 	 */
666 	mex->outputdatalength = mex->inputdatalength;
667 
668 	rc = get_rsa_modex_fc(mex, &func_code);
669 	if (rc)
670 		goto out;
671 
672 	pref_zc = NULL;
673 	pref_zq = NULL;
674 	spin_lock(&zcrypt_list_lock);
675 	for_each_zcrypt_card(zc) {
676 		/* Check for useable accelarator or CCA card */
677 		if (!zc->online || !zc->card->config ||
678 		    !(zc->card->functions & 0x18000000))
679 			continue;
680 		/* Check for size limits */
681 		if (zc->min_mod_size > mex->inputdatalength ||
682 		    zc->max_mod_size < mex->inputdatalength)
683 			continue;
684 		/* check if device node has admission for this card */
685 		if (!zcrypt_check_card(perms, zc->card->id))
686 			continue;
687 		/* get weight index of the card device	*/
688 		wgt = zc->speed_rating[func_code];
689 		/* penalty if this msg was previously sent via this card */
690 		cpen = (tr && tr->again_counter && tr->last_qid &&
691 			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
692 			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
693 		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
694 			continue;
695 		for_each_zcrypt_queue(zq, zc) {
696 			/* check if device is useable and eligible */
697 			if (!zq->online || !zq->ops->rsa_modexpo ||
698 			    !zq->queue->config)
699 				continue;
700 			/* check if device node has admission for this queue */
701 			if (!zcrypt_check_queue(perms,
702 						AP_QID_QUEUE(zq->queue->qid)))
703 				continue;
704 			/* penalty if the msg was previously sent at this qid */
705 			qpen = (tr && tr->again_counter && tr->last_qid &&
706 				tr->last_qid == zq->queue->qid) ?
707 				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
708 			if (!zcrypt_queue_compare(zq, pref_zq,
709 						  wgt + cpen + qpen, pref_wgt))
710 				continue;
711 			pref_zc = zc;
712 			pref_zq = zq;
713 			pref_wgt = wgt + cpen + qpen;
714 		}
715 	}
716 	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
717 	spin_unlock(&zcrypt_list_lock);
718 
719 	if (!pref_zq) {
720 		rc = -ENODEV;
721 		goto out;
722 	}
723 
724 	qid = pref_zq->queue->qid;
725 	rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
726 
727 	spin_lock(&zcrypt_list_lock);
728 	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
729 	spin_unlock(&zcrypt_list_lock);
730 
731 out:
732 	ap_release_message(&ap_msg);
733 	if (tr) {
734 		tr->last_rc = rc;
735 		tr->last_qid = qid;
736 	}
737 	trace_s390_zcrypt_rep(mex, func_code, rc,
738 			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
739 	return rc;
740 }
741 
zcrypt_rsa_crt(struct ap_perms * perms,struct zcrypt_track * tr,struct ica_rsa_modexpo_crt * crt)742 static long zcrypt_rsa_crt(struct ap_perms *perms,
743 			   struct zcrypt_track *tr,
744 			   struct ica_rsa_modexpo_crt *crt)
745 {
746 	struct zcrypt_card *zc, *pref_zc;
747 	struct zcrypt_queue *zq, *pref_zq;
748 	struct ap_message ap_msg;
749 	unsigned int wgt = 0, pref_wgt = 0;
750 	unsigned int func_code;
751 	int cpen, qpen, qid = 0, rc = -ENODEV;
752 	struct module *mod;
753 
754 	trace_s390_zcrypt_req(crt, TP_ICARSACRT);
755 
756 	ap_init_message(&ap_msg);
757 
758 #ifdef CONFIG_ZCRYPT_DEBUG
759 	if (tr && tr->fi.cmd)
760 		ap_msg.fi.cmd = tr->fi.cmd;
761 #endif
762 
763 	if (crt->outputdatalength < crt->inputdatalength) {
764 		func_code = 0;
765 		rc = -EINVAL;
766 		goto out;
767 	}
768 
769 	/*
770 	 * As long as outputdatalength is big enough, we can set the
771 	 * outputdatalength equal to the inputdatalength, since that is the
772 	 * number of bytes we will copy in any case
773 	 */
774 	crt->outputdatalength = crt->inputdatalength;
775 
776 	rc = get_rsa_crt_fc(crt, &func_code);
777 	if (rc)
778 		goto out;
779 
780 	pref_zc = NULL;
781 	pref_zq = NULL;
782 	spin_lock(&zcrypt_list_lock);
783 	for_each_zcrypt_card(zc) {
784 		/* Check for useable accelarator or CCA card */
785 		if (!zc->online || !zc->card->config ||
786 		    !(zc->card->functions & 0x18000000))
787 			continue;
788 		/* Check for size limits */
789 		if (zc->min_mod_size > crt->inputdatalength ||
790 		    zc->max_mod_size < crt->inputdatalength)
791 			continue;
792 		/* check if device node has admission for this card */
793 		if (!zcrypt_check_card(perms, zc->card->id))
794 			continue;
795 		/* get weight index of the card device	*/
796 		wgt = zc->speed_rating[func_code];
797 		/* penalty if this msg was previously sent via this card */
798 		cpen = (tr && tr->again_counter && tr->last_qid &&
799 			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
800 			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
801 		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
802 			continue;
803 		for_each_zcrypt_queue(zq, zc) {
804 			/* check if device is useable and eligible */
805 			if (!zq->online || !zq->ops->rsa_modexpo_crt ||
806 			    !zq->queue->config)
807 				continue;
808 			/* check if device node has admission for this queue */
809 			if (!zcrypt_check_queue(perms,
810 						AP_QID_QUEUE(zq->queue->qid)))
811 				continue;
812 			/* penalty if the msg was previously sent at this qid */
813 			qpen = (tr && tr->again_counter && tr->last_qid &&
814 				tr->last_qid == zq->queue->qid) ?
815 				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
816 			if (!zcrypt_queue_compare(zq, pref_zq,
817 						  wgt + cpen + qpen, pref_wgt))
818 				continue;
819 			pref_zc = zc;
820 			pref_zq = zq;
821 			pref_wgt = wgt + cpen + qpen;
822 		}
823 	}
824 	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
825 	spin_unlock(&zcrypt_list_lock);
826 
827 	if (!pref_zq) {
828 		rc = -ENODEV;
829 		goto out;
830 	}
831 
832 	qid = pref_zq->queue->qid;
833 	rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
834 
835 	spin_lock(&zcrypt_list_lock);
836 	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
837 	spin_unlock(&zcrypt_list_lock);
838 
839 out:
840 	ap_release_message(&ap_msg);
841 	if (tr) {
842 		tr->last_rc = rc;
843 		tr->last_qid = qid;
844 	}
845 	trace_s390_zcrypt_rep(crt, func_code, rc,
846 			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
847 	return rc;
848 }
849 
_zcrypt_send_cprb(bool userspace,struct ap_perms * perms,struct zcrypt_track * tr,struct ica_xcRB * xcRB)850 static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
851 			      struct zcrypt_track *tr,
852 			      struct ica_xcRB *xcRB)
853 {
854 	struct zcrypt_card *zc, *pref_zc;
855 	struct zcrypt_queue *zq, *pref_zq;
856 	struct ap_message ap_msg;
857 	unsigned int wgt = 0, pref_wgt = 0;
858 	unsigned int func_code;
859 	unsigned short *domain, tdom;
860 	int cpen, qpen, qid = 0, rc = -ENODEV;
861 	struct module *mod;
862 
863 	trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
864 
865 	xcRB->status = 0;
866 	ap_init_message(&ap_msg);
867 
868 #ifdef CONFIG_ZCRYPT_DEBUG
869 	if (tr && tr->fi.cmd)
870 		ap_msg.fi.cmd = tr->fi.cmd;
871 	if (tr && tr->fi.action == AP_FI_ACTION_CCA_AGENT_FF) {
872 		ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid agent_ID 'FF'\n",
873 				__func__, tr->fi.cmd);
874 		xcRB->agent_ID = 0x4646;
875 	}
876 #endif
877 
878 	rc = get_cprb_fc(userspace, xcRB, &ap_msg, &func_code, &domain);
879 	if (rc)
880 		goto out;
881 
882 	/*
883 	 * If a valid target domain is set and this domain is NOT a usage
884 	 * domain but a control only domain, use the default domain as target.
885 	 */
886 	tdom = *domain;
887 	if (tdom < AP_DOMAINS &&
888 	    !ap_test_config_usage_domain(tdom) &&
889 	    ap_test_config_ctrl_domain(tdom) &&
890 	    ap_domain_index >= 0)
891 		tdom = ap_domain_index;
892 
893 	pref_zc = NULL;
894 	pref_zq = NULL;
895 	spin_lock(&zcrypt_list_lock);
896 	for_each_zcrypt_card(zc) {
897 		/* Check for useable CCA card */
898 		if (!zc->online || !zc->card->config ||
899 		    !(zc->card->functions & 0x10000000))
900 			continue;
901 		/* Check for user selected CCA card */
902 		if (xcRB->user_defined != AUTOSELECT &&
903 		    xcRB->user_defined != zc->card->id)
904 			continue;
905 		/* check if device node has admission for this card */
906 		if (!zcrypt_check_card(perms, zc->card->id))
907 			continue;
908 		/* get weight index of the card device	*/
909 		wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
910 		/* penalty if this msg was previously sent via this card */
911 		cpen = (tr && tr->again_counter && tr->last_qid &&
912 			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
913 			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
914 		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
915 			continue;
916 		for_each_zcrypt_queue(zq, zc) {
917 			/* check for device useable and eligible */
918 			if (!zq->online ||
919 			    !zq->ops->send_cprb ||
920 			    !zq->queue->config ||
921 			    (tdom != AUTOSEL_DOM &&
922 			     tdom != AP_QID_QUEUE(zq->queue->qid)))
923 				continue;
924 			/* check if device node has admission for this queue */
925 			if (!zcrypt_check_queue(perms,
926 						AP_QID_QUEUE(zq->queue->qid)))
927 				continue;
928 			/* penalty if the msg was previously sent at this qid */
929 			qpen = (tr && tr->again_counter && tr->last_qid &&
930 				tr->last_qid == zq->queue->qid) ?
931 				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
932 			if (!zcrypt_queue_compare(zq, pref_zq,
933 						  wgt + cpen + qpen, pref_wgt))
934 				continue;
935 			pref_zc = zc;
936 			pref_zq = zq;
937 			pref_wgt = wgt + cpen + qpen;
938 		}
939 	}
940 	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
941 	spin_unlock(&zcrypt_list_lock);
942 
943 	if (!pref_zq) {
944 		rc = -ENODEV;
945 		goto out;
946 	}
947 
948 	/* in case of auto select, provide the correct domain */
949 	qid = pref_zq->queue->qid;
950 	if (*domain == AUTOSEL_DOM)
951 		*domain = AP_QID_QUEUE(qid);
952 
953 #ifdef CONFIG_ZCRYPT_DEBUG
954 	if (tr && tr->fi.action == AP_FI_ACTION_CCA_DOM_INVAL) {
955 		ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid domain\n",
956 				__func__, tr->fi.cmd);
957 		*domain = 99;
958 	}
959 #endif
960 
961 	rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcRB, &ap_msg);
962 
963 	spin_lock(&zcrypt_list_lock);
964 	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
965 	spin_unlock(&zcrypt_list_lock);
966 
967 out:
968 	ap_release_message(&ap_msg);
969 	if (tr) {
970 		tr->last_rc = rc;
971 		tr->last_qid = qid;
972 	}
973 	trace_s390_zcrypt_rep(xcRB, func_code, rc,
974 			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
975 	return rc;
976 }
977 
zcrypt_send_cprb(struct ica_xcRB * xcRB)978 long zcrypt_send_cprb(struct ica_xcRB *xcRB)
979 {
980 	return _zcrypt_send_cprb(false, &ap_perms, NULL, xcRB);
981 }
982 EXPORT_SYMBOL(zcrypt_send_cprb);
983 
is_desired_ep11_card(unsigned int dev_id,unsigned short target_num,struct ep11_target_dev * targets)984 static bool is_desired_ep11_card(unsigned int dev_id,
985 				 unsigned short target_num,
986 				 struct ep11_target_dev *targets)
987 {
988 	while (target_num-- > 0) {
989 		if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
990 			return true;
991 		targets++;
992 	}
993 	return false;
994 }
995 
is_desired_ep11_queue(unsigned int dev_qid,unsigned short target_num,struct ep11_target_dev * targets)996 static bool is_desired_ep11_queue(unsigned int dev_qid,
997 				  unsigned short target_num,
998 				  struct ep11_target_dev *targets)
999 {
1000 	int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
1001 
1002 	while (target_num-- > 0) {
1003 		if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
1004 		    (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
1005 			return true;
1006 		targets++;
1007 	}
1008 	return false;
1009 }
1010 
_zcrypt_send_ep11_cprb(bool userspace,struct ap_perms * perms,struct zcrypt_track * tr,struct ep11_urb * xcrb)1011 static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
1012 				   struct zcrypt_track *tr,
1013 				   struct ep11_urb *xcrb)
1014 {
1015 	struct zcrypt_card *zc, *pref_zc;
1016 	struct zcrypt_queue *zq, *pref_zq;
1017 	struct ep11_target_dev *targets;
1018 	unsigned short target_num;
1019 	unsigned int wgt = 0, pref_wgt = 0;
1020 	unsigned int func_code;
1021 	struct ap_message ap_msg;
1022 	int cpen, qpen, qid = 0, rc = -ENODEV;
1023 	struct module *mod;
1024 
1025 	trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
1026 
1027 	ap_init_message(&ap_msg);
1028 
1029 #ifdef CONFIG_ZCRYPT_DEBUG
1030 	if (tr && tr->fi.cmd)
1031 		ap_msg.fi.cmd = tr->fi.cmd;
1032 #endif
1033 
1034 	target_num = (unsigned short) xcrb->targets_num;
1035 
1036 	/* empty list indicates autoselect (all available targets) */
1037 	targets = NULL;
1038 	if (target_num != 0) {
1039 		struct ep11_target_dev __user *uptr;
1040 
1041 		targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
1042 		if (!targets) {
1043 			func_code = 0;
1044 			rc = -ENOMEM;
1045 			goto out;
1046 		}
1047 
1048 		uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
1049 		if (z_copy_from_user(userspace, targets, uptr,
1050 				   target_num * sizeof(*targets))) {
1051 			func_code = 0;
1052 			rc = -EFAULT;
1053 			goto out_free;
1054 		}
1055 	}
1056 
1057 	rc = get_ep11cprb_fc(userspace, xcrb, &ap_msg, &func_code);
1058 	if (rc)
1059 		goto out_free;
1060 
1061 	pref_zc = NULL;
1062 	pref_zq = NULL;
1063 	spin_lock(&zcrypt_list_lock);
1064 	for_each_zcrypt_card(zc) {
1065 		/* Check for useable EP11 card */
1066 		if (!zc->online || !zc->card->config ||
1067 		    !(zc->card->functions & 0x04000000))
1068 			continue;
1069 		/* Check for user selected EP11 card */
1070 		if (targets &&
1071 		    !is_desired_ep11_card(zc->card->id, target_num, targets))
1072 			continue;
1073 		/* check if device node has admission for this card */
1074 		if (!zcrypt_check_card(perms, zc->card->id))
1075 			continue;
1076 		/* get weight index of the card device	*/
1077 		wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
1078 		/* penalty if this msg was previously sent via this card */
1079 		cpen = (tr && tr->again_counter && tr->last_qid &&
1080 			AP_QID_CARD(tr->last_qid) == zc->card->id) ?
1081 			TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
1082 		if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
1083 			continue;
1084 		for_each_zcrypt_queue(zq, zc) {
1085 			/* check if device is useable and eligible */
1086 			if (!zq->online ||
1087 			    !zq->ops->send_ep11_cprb ||
1088 			    !zq->queue->config ||
1089 			    (targets &&
1090 			     !is_desired_ep11_queue(zq->queue->qid,
1091 						    target_num, targets)))
1092 				continue;
1093 			/* check if device node has admission for this queue */
1094 			if (!zcrypt_check_queue(perms,
1095 						AP_QID_QUEUE(zq->queue->qid)))
1096 				continue;
1097 			/* penalty if the msg was previously sent at this qid */
1098 			qpen = (tr && tr->again_counter && tr->last_qid &&
1099 				tr->last_qid == zq->queue->qid) ?
1100 				TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
1101 			if (!zcrypt_queue_compare(zq, pref_zq,
1102 						  wgt + cpen + qpen, pref_wgt))
1103 				continue;
1104 			pref_zc = zc;
1105 			pref_zq = zq;
1106 			pref_wgt = wgt + cpen + qpen;
1107 		}
1108 	}
1109 	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1110 	spin_unlock(&zcrypt_list_lock);
1111 
1112 	if (!pref_zq) {
1113 		rc = -ENODEV;
1114 		goto out_free;
1115 	}
1116 
1117 	qid = pref_zq->queue->qid;
1118 	rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
1119 
1120 	spin_lock(&zcrypt_list_lock);
1121 	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1122 	spin_unlock(&zcrypt_list_lock);
1123 
1124 out_free:
1125 	kfree(targets);
1126 out:
1127 	ap_release_message(&ap_msg);
1128 	if (tr) {
1129 		tr->last_rc = rc;
1130 		tr->last_qid = qid;
1131 	}
1132 	trace_s390_zcrypt_rep(xcrb, func_code, rc,
1133 			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1134 	return rc;
1135 }
1136 
zcrypt_send_ep11_cprb(struct ep11_urb * xcrb)1137 long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
1138 {
1139 	return _zcrypt_send_ep11_cprb(false, &ap_perms, NULL, xcrb);
1140 }
1141 EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
1142 
zcrypt_rng(char * buffer)1143 static long zcrypt_rng(char *buffer)
1144 {
1145 	struct zcrypt_card *zc, *pref_zc;
1146 	struct zcrypt_queue *zq, *pref_zq;
1147 	unsigned int wgt = 0, pref_wgt = 0;
1148 	unsigned int func_code;
1149 	struct ap_message ap_msg;
1150 	unsigned int domain;
1151 	int qid = 0, rc = -ENODEV;
1152 	struct module *mod;
1153 
1154 	trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
1155 
1156 	ap_init_message(&ap_msg);
1157 	rc = get_rng_fc(&ap_msg, &func_code, &domain);
1158 	if (rc)
1159 		goto out;
1160 
1161 	pref_zc = NULL;
1162 	pref_zq = NULL;
1163 	spin_lock(&zcrypt_list_lock);
1164 	for_each_zcrypt_card(zc) {
1165 		/* Check for useable CCA card */
1166 		if (!zc->online || !zc->card->config ||
1167 		    !(zc->card->functions & 0x10000000))
1168 			continue;
1169 		/* get weight index of the card device	*/
1170 		wgt = zc->speed_rating[func_code];
1171 		if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
1172 			continue;
1173 		for_each_zcrypt_queue(zq, zc) {
1174 			/* check if device is useable and eligible */
1175 			if (!zq->online || !zq->ops->rng ||
1176 			    !zq->queue->config)
1177 				continue;
1178 			if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
1179 				continue;
1180 			pref_zc = zc;
1181 			pref_zq = zq;
1182 			pref_wgt = wgt;
1183 		}
1184 	}
1185 	pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
1186 	spin_unlock(&zcrypt_list_lock);
1187 
1188 	if (!pref_zq) {
1189 		rc = -ENODEV;
1190 		goto out;
1191 	}
1192 
1193 	qid = pref_zq->queue->qid;
1194 	rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
1195 
1196 	spin_lock(&zcrypt_list_lock);
1197 	zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
1198 	spin_unlock(&zcrypt_list_lock);
1199 
1200 out:
1201 	ap_release_message(&ap_msg);
1202 	trace_s390_zcrypt_rep(buffer, func_code, rc,
1203 			      AP_QID_CARD(qid), AP_QID_QUEUE(qid));
1204 	return rc;
1205 }
1206 
zcrypt_device_status_mask(struct zcrypt_device_status * devstatus)1207 static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
1208 {
1209 	struct zcrypt_card *zc;
1210 	struct zcrypt_queue *zq;
1211 	struct zcrypt_device_status *stat;
1212 	int card, queue;
1213 
1214 	memset(devstatus, 0, MAX_ZDEV_ENTRIES
1215 	       * sizeof(struct zcrypt_device_status));
1216 
1217 	spin_lock(&zcrypt_list_lock);
1218 	for_each_zcrypt_card(zc) {
1219 		for_each_zcrypt_queue(zq, zc) {
1220 			card = AP_QID_CARD(zq->queue->qid);
1221 			if (card >= MAX_ZDEV_CARDIDS)
1222 				continue;
1223 			queue = AP_QID_QUEUE(zq->queue->qid);
1224 			stat = &devstatus[card * AP_DOMAINS + queue];
1225 			stat->hwtype = zc->card->ap_dev.device_type;
1226 			stat->functions = zc->card->functions >> 26;
1227 			stat->qid = zq->queue->qid;
1228 			stat->online = zq->online ? 0x01 : 0x00;
1229 		}
1230 	}
1231 	spin_unlock(&zcrypt_list_lock);
1232 }
1233 
zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext * devstatus)1234 void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
1235 {
1236 	struct zcrypt_card *zc;
1237 	struct zcrypt_queue *zq;
1238 	struct zcrypt_device_status_ext *stat;
1239 	int card, queue;
1240 
1241 	memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
1242 	       * sizeof(struct zcrypt_device_status_ext));
1243 
1244 	spin_lock(&zcrypt_list_lock);
1245 	for_each_zcrypt_card(zc) {
1246 		for_each_zcrypt_queue(zq, zc) {
1247 			card = AP_QID_CARD(zq->queue->qid);
1248 			queue = AP_QID_QUEUE(zq->queue->qid);
1249 			stat = &devstatus[card * AP_DOMAINS + queue];
1250 			stat->hwtype = zc->card->ap_dev.device_type;
1251 			stat->functions = zc->card->functions >> 26;
1252 			stat->qid = zq->queue->qid;
1253 			stat->online = zq->online ? 0x01 : 0x00;
1254 		}
1255 	}
1256 	spin_unlock(&zcrypt_list_lock);
1257 }
1258 EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
1259 
zcrypt_device_status_ext(int card,int queue,struct zcrypt_device_status_ext * devstat)1260 int zcrypt_device_status_ext(int card, int queue,
1261 			     struct zcrypt_device_status_ext *devstat)
1262 {
1263 	struct zcrypt_card *zc;
1264 	struct zcrypt_queue *zq;
1265 
1266 	memset(devstat, 0, sizeof(*devstat));
1267 
1268 	spin_lock(&zcrypt_list_lock);
1269 	for_each_zcrypt_card(zc) {
1270 		for_each_zcrypt_queue(zq, zc) {
1271 			if (card == AP_QID_CARD(zq->queue->qid) &&
1272 			    queue == AP_QID_QUEUE(zq->queue->qid)) {
1273 				devstat->hwtype = zc->card->ap_dev.device_type;
1274 				devstat->functions = zc->card->functions >> 26;
1275 				devstat->qid = zq->queue->qid;
1276 				devstat->online = zq->online ? 0x01 : 0x00;
1277 				spin_unlock(&zcrypt_list_lock);
1278 				return 0;
1279 			}
1280 		}
1281 	}
1282 	spin_unlock(&zcrypt_list_lock);
1283 
1284 	return -ENODEV;
1285 }
1286 EXPORT_SYMBOL(zcrypt_device_status_ext);
1287 
zcrypt_status_mask(char status[],size_t max_adapters)1288 static void zcrypt_status_mask(char status[], size_t max_adapters)
1289 {
1290 	struct zcrypt_card *zc;
1291 	struct zcrypt_queue *zq;
1292 	int card;
1293 
1294 	memset(status, 0, max_adapters);
1295 	spin_lock(&zcrypt_list_lock);
1296 	for_each_zcrypt_card(zc) {
1297 		for_each_zcrypt_queue(zq, zc) {
1298 			card = AP_QID_CARD(zq->queue->qid);
1299 			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1300 			    || card >= max_adapters)
1301 				continue;
1302 			status[card] = zc->online ? zc->user_space_type : 0x0d;
1303 		}
1304 	}
1305 	spin_unlock(&zcrypt_list_lock);
1306 }
1307 
zcrypt_qdepth_mask(char qdepth[],size_t max_adapters)1308 static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
1309 {
1310 	struct zcrypt_card *zc;
1311 	struct zcrypt_queue *zq;
1312 	int card;
1313 
1314 	memset(qdepth, 0, max_adapters);
1315 	spin_lock(&zcrypt_list_lock);
1316 	local_bh_disable();
1317 	for_each_zcrypt_card(zc) {
1318 		for_each_zcrypt_queue(zq, zc) {
1319 			card = AP_QID_CARD(zq->queue->qid);
1320 			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1321 			    || card >= max_adapters)
1322 				continue;
1323 			spin_lock(&zq->queue->lock);
1324 			qdepth[card] =
1325 				zq->queue->pendingq_count +
1326 				zq->queue->requestq_count;
1327 			spin_unlock(&zq->queue->lock);
1328 		}
1329 	}
1330 	local_bh_enable();
1331 	spin_unlock(&zcrypt_list_lock);
1332 }
1333 
zcrypt_perdev_reqcnt(u32 reqcnt[],size_t max_adapters)1334 static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
1335 {
1336 	struct zcrypt_card *zc;
1337 	struct zcrypt_queue *zq;
1338 	int card;
1339 	u64 cnt;
1340 
1341 	memset(reqcnt, 0, sizeof(int) * max_adapters);
1342 	spin_lock(&zcrypt_list_lock);
1343 	local_bh_disable();
1344 	for_each_zcrypt_card(zc) {
1345 		for_each_zcrypt_queue(zq, zc) {
1346 			card = AP_QID_CARD(zq->queue->qid);
1347 			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
1348 			    || card >= max_adapters)
1349 				continue;
1350 			spin_lock(&zq->queue->lock);
1351 			cnt = zq->queue->total_request_count;
1352 			spin_unlock(&zq->queue->lock);
1353 			reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX;
1354 		}
1355 	}
1356 	local_bh_enable();
1357 	spin_unlock(&zcrypt_list_lock);
1358 }
1359 
zcrypt_pendingq_count(void)1360 static int zcrypt_pendingq_count(void)
1361 {
1362 	struct zcrypt_card *zc;
1363 	struct zcrypt_queue *zq;
1364 	int pendingq_count;
1365 
1366 	pendingq_count = 0;
1367 	spin_lock(&zcrypt_list_lock);
1368 	local_bh_disable();
1369 	for_each_zcrypt_card(zc) {
1370 		for_each_zcrypt_queue(zq, zc) {
1371 			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1372 				continue;
1373 			spin_lock(&zq->queue->lock);
1374 			pendingq_count += zq->queue->pendingq_count;
1375 			spin_unlock(&zq->queue->lock);
1376 		}
1377 	}
1378 	local_bh_enable();
1379 	spin_unlock(&zcrypt_list_lock);
1380 	return pendingq_count;
1381 }
1382 
zcrypt_requestq_count(void)1383 static int zcrypt_requestq_count(void)
1384 {
1385 	struct zcrypt_card *zc;
1386 	struct zcrypt_queue *zq;
1387 	int requestq_count;
1388 
1389 	requestq_count = 0;
1390 	spin_lock(&zcrypt_list_lock);
1391 	local_bh_disable();
1392 	for_each_zcrypt_card(zc) {
1393 		for_each_zcrypt_queue(zq, zc) {
1394 			if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
1395 				continue;
1396 			spin_lock(&zq->queue->lock);
1397 			requestq_count += zq->queue->requestq_count;
1398 			spin_unlock(&zq->queue->lock);
1399 		}
1400 	}
1401 	local_bh_enable();
1402 	spin_unlock(&zcrypt_list_lock);
1403 	return requestq_count;
1404 }
1405 
icarsamodexpo_ioctl(struct ap_perms * perms,unsigned long arg)1406 static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
1407 {
1408 	int rc;
1409 	struct zcrypt_track tr;
1410 	struct ica_rsa_modexpo mex;
1411 	struct ica_rsa_modexpo __user *umex = (void __user *) arg;
1412 
1413 	memset(&tr, 0, sizeof(tr));
1414 	if (copy_from_user(&mex, umex, sizeof(mex)))
1415 		return -EFAULT;
1416 
1417 #ifdef CONFIG_ZCRYPT_DEBUG
1418 	if (mex.inputdatalength & (1U << 31)) {
1419 		if (!capable(CAP_SYS_ADMIN))
1420 			return -EPERM;
1421 		tr.fi.cmd = (u16)(mex.inputdatalength >> 16);
1422 	}
1423 	mex.inputdatalength &= 0x0000FFFF;
1424 #endif
1425 
1426 	do {
1427 		rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1428 		if (rc == -EAGAIN)
1429 			tr.again_counter++;
1430 #ifdef CONFIG_ZCRYPT_DEBUG
1431 		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1432 			break;
1433 #endif
1434 	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1435 	/* on failure: retry once again after a requested rescan */
1436 	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1437 		do {
1438 			rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
1439 			if (rc == -EAGAIN)
1440 				tr.again_counter++;
1441 		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1442 	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1443 		rc = -EIO;
1444 	if (rc) {
1445 		ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
1446 		return rc;
1447 	}
1448 	return put_user(mex.outputdatalength, &umex->outputdatalength);
1449 }
1450 
icarsacrt_ioctl(struct ap_perms * perms,unsigned long arg)1451 static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
1452 {
1453 	int rc;
1454 	struct zcrypt_track tr;
1455 	struct ica_rsa_modexpo_crt crt;
1456 	struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
1457 
1458 	memset(&tr, 0, sizeof(tr));
1459 	if (copy_from_user(&crt, ucrt, sizeof(crt)))
1460 		return -EFAULT;
1461 
1462 #ifdef CONFIG_ZCRYPT_DEBUG
1463 	if (crt.inputdatalength & (1U << 31)) {
1464 		if (!capable(CAP_SYS_ADMIN))
1465 			return -EPERM;
1466 		tr.fi.cmd = (u16)(crt.inputdatalength >> 16);
1467 	}
1468 	crt.inputdatalength &= 0x0000FFFF;
1469 #endif
1470 
1471 	do {
1472 		rc = zcrypt_rsa_crt(perms, &tr, &crt);
1473 		if (rc == -EAGAIN)
1474 			tr.again_counter++;
1475 #ifdef CONFIG_ZCRYPT_DEBUG
1476 		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1477 			break;
1478 #endif
1479 	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1480 	/* on failure: retry once again after a requested rescan */
1481 	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1482 		do {
1483 			rc = zcrypt_rsa_crt(perms, &tr, &crt);
1484 			if (rc == -EAGAIN)
1485 				tr.again_counter++;
1486 		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1487 	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1488 		rc = -EIO;
1489 	if (rc) {
1490 		ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
1491 		return rc;
1492 	}
1493 	return put_user(crt.outputdatalength, &ucrt->outputdatalength);
1494 }
1495 
zsecsendcprb_ioctl(struct ap_perms * perms,unsigned long arg)1496 static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
1497 {
1498 	int rc;
1499 	struct ica_xcRB xcRB;
1500 	struct zcrypt_track tr;
1501 	struct ica_xcRB __user *uxcRB = (void __user *) arg;
1502 
1503 	memset(&tr, 0, sizeof(tr));
1504 	if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
1505 		return -EFAULT;
1506 
1507 #ifdef CONFIG_ZCRYPT_DEBUG
1508 	if (xcRB.status & (1U << 31)) {
1509 		if (!capable(CAP_SYS_ADMIN))
1510 			return -EPERM;
1511 		tr.fi.cmd = (u16)(xcRB.status >> 16);
1512 	}
1513 	xcRB.status &= 0x0000FFFF;
1514 #endif
1515 
1516 	do {
1517 		rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1518 		if (rc == -EAGAIN)
1519 			tr.again_counter++;
1520 #ifdef CONFIG_ZCRYPT_DEBUG
1521 		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1522 			break;
1523 #endif
1524 	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1525 	/* on failure: retry once again after a requested rescan */
1526 	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1527 		do {
1528 			rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
1529 			if (rc == -EAGAIN)
1530 				tr.again_counter++;
1531 		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1532 	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1533 		rc = -EIO;
1534 	if (rc)
1535 		ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
1536 			   rc, xcRB.status);
1537 	if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
1538 		return -EFAULT;
1539 	return rc;
1540 }
1541 
zsendep11cprb_ioctl(struct ap_perms * perms,unsigned long arg)1542 static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
1543 {
1544 	int rc;
1545 	struct ep11_urb xcrb;
1546 	struct zcrypt_track tr;
1547 	struct ep11_urb __user *uxcrb = (void __user *)arg;
1548 
1549 	memset(&tr, 0, sizeof(tr));
1550 	if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
1551 		return -EFAULT;
1552 
1553 #ifdef CONFIG_ZCRYPT_DEBUG
1554 	if (xcrb.req_len & (1ULL << 63)) {
1555 		if (!capable(CAP_SYS_ADMIN))
1556 			return -EPERM;
1557 		tr.fi.cmd = (u16)(xcrb.req_len >> 48);
1558 	}
1559 	xcrb.req_len &= 0x0000FFFFFFFFFFFFULL;
1560 #endif
1561 
1562 	do {
1563 		rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1564 		if (rc == -EAGAIN)
1565 			tr.again_counter++;
1566 #ifdef CONFIG_ZCRYPT_DEBUG
1567 		if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
1568 			break;
1569 #endif
1570 	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1571 	/* on failure: retry once again after a requested rescan */
1572 	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1573 		do {
1574 			rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
1575 			if (rc == -EAGAIN)
1576 				tr.again_counter++;
1577 		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1578 	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1579 		rc = -EIO;
1580 	if (rc)
1581 		ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
1582 	if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
1583 		return -EFAULT;
1584 	return rc;
1585 }
1586 
zcrypt_unlocked_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1587 static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
1588 				  unsigned long arg)
1589 {
1590 	int rc;
1591 	struct ap_perms *perms =
1592 		(struct ap_perms *) filp->private_data;
1593 
1594 	rc = zcrypt_check_ioctl(perms, cmd);
1595 	if (rc)
1596 		return rc;
1597 
1598 	switch (cmd) {
1599 	case ICARSAMODEXPO:
1600 		return icarsamodexpo_ioctl(perms, arg);
1601 	case ICARSACRT:
1602 		return icarsacrt_ioctl(perms, arg);
1603 	case ZSECSENDCPRB:
1604 		return zsecsendcprb_ioctl(perms, arg);
1605 	case ZSENDEP11CPRB:
1606 		return zsendep11cprb_ioctl(perms, arg);
1607 	case ZCRYPT_DEVICE_STATUS: {
1608 		struct zcrypt_device_status_ext *device_status;
1609 		size_t total_size = MAX_ZDEV_ENTRIES_EXT
1610 			* sizeof(struct zcrypt_device_status_ext);
1611 
1612 		device_status = kzalloc(total_size, GFP_KERNEL);
1613 		if (!device_status)
1614 			return -ENOMEM;
1615 		zcrypt_device_status_mask_ext(device_status);
1616 		if (copy_to_user((char __user *) arg, device_status,
1617 				 total_size))
1618 			rc = -EFAULT;
1619 		kfree(device_status);
1620 		return rc;
1621 	}
1622 	case ZCRYPT_STATUS_MASK: {
1623 		char status[AP_DEVICES];
1624 
1625 		zcrypt_status_mask(status, AP_DEVICES);
1626 		if (copy_to_user((char __user *) arg, status, sizeof(status)))
1627 			return -EFAULT;
1628 		return 0;
1629 	}
1630 	case ZCRYPT_QDEPTH_MASK: {
1631 		char qdepth[AP_DEVICES];
1632 
1633 		zcrypt_qdepth_mask(qdepth, AP_DEVICES);
1634 		if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1635 			return -EFAULT;
1636 		return 0;
1637 	}
1638 	case ZCRYPT_PERDEV_REQCNT: {
1639 		u32 *reqcnt;
1640 
1641 		reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
1642 		if (!reqcnt)
1643 			return -ENOMEM;
1644 		zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
1645 		if (copy_to_user((int __user *) arg, reqcnt,
1646 				 sizeof(u32) * AP_DEVICES))
1647 			rc = -EFAULT;
1648 		kfree(reqcnt);
1649 		return rc;
1650 	}
1651 	case Z90STAT_REQUESTQ_COUNT:
1652 		return put_user(zcrypt_requestq_count(), (int __user *) arg);
1653 	case Z90STAT_PENDINGQ_COUNT:
1654 		return put_user(zcrypt_pendingq_count(), (int __user *) arg);
1655 	case Z90STAT_TOTALOPEN_COUNT:
1656 		return put_user(atomic_read(&zcrypt_open_count),
1657 				(int __user *) arg);
1658 	case Z90STAT_DOMAIN_INDEX:
1659 		return put_user(ap_domain_index, (int __user *) arg);
1660 	/*
1661 	 * Deprecated ioctls
1662 	 */
1663 	case ZDEVICESTATUS: {
1664 		/* the old ioctl supports only 64 adapters */
1665 		struct zcrypt_device_status *device_status;
1666 		size_t total_size = MAX_ZDEV_ENTRIES
1667 			* sizeof(struct zcrypt_device_status);
1668 
1669 		device_status = kzalloc(total_size, GFP_KERNEL);
1670 		if (!device_status)
1671 			return -ENOMEM;
1672 		zcrypt_device_status_mask(device_status);
1673 		if (copy_to_user((char __user *) arg, device_status,
1674 				 total_size))
1675 			rc = -EFAULT;
1676 		kfree(device_status);
1677 		return rc;
1678 	}
1679 	case Z90STAT_STATUS_MASK: {
1680 		/* the old ioctl supports only 64 adapters */
1681 		char status[MAX_ZDEV_CARDIDS];
1682 
1683 		zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
1684 		if (copy_to_user((char __user *) arg, status, sizeof(status)))
1685 			return -EFAULT;
1686 		return 0;
1687 	}
1688 	case Z90STAT_QDEPTH_MASK: {
1689 		/* the old ioctl supports only 64 adapters */
1690 		char qdepth[MAX_ZDEV_CARDIDS];
1691 
1692 		zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
1693 		if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
1694 			return -EFAULT;
1695 		return 0;
1696 	}
1697 	case Z90STAT_PERDEV_REQCNT: {
1698 		/* the old ioctl supports only 64 adapters */
1699 		u32 reqcnt[MAX_ZDEV_CARDIDS];
1700 
1701 		zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
1702 		if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
1703 			return -EFAULT;
1704 		return 0;
1705 	}
1706 	/* unknown ioctl number */
1707 	default:
1708 		ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
1709 		return -ENOIOCTLCMD;
1710 	}
1711 }
1712 
1713 #ifdef CONFIG_COMPAT
1714 /*
1715  * ioctl32 conversion routines
1716  */
1717 struct compat_ica_rsa_modexpo {
1718 	compat_uptr_t	inputdata;
1719 	unsigned int	inputdatalength;
1720 	compat_uptr_t	outputdata;
1721 	unsigned int	outputdatalength;
1722 	compat_uptr_t	b_key;
1723 	compat_uptr_t	n_modulus;
1724 };
1725 
trans_modexpo32(struct ap_perms * perms,struct file * filp,unsigned int cmd,unsigned long arg)1726 static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
1727 			    unsigned int cmd, unsigned long arg)
1728 {
1729 	struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
1730 	struct compat_ica_rsa_modexpo mex32;
1731 	struct ica_rsa_modexpo mex64;
1732 	struct zcrypt_track tr;
1733 	long rc;
1734 
1735 	memset(&tr, 0, sizeof(tr));
1736 	if (copy_from_user(&mex32, umex32, sizeof(mex32)))
1737 		return -EFAULT;
1738 	mex64.inputdata = compat_ptr(mex32.inputdata);
1739 	mex64.inputdatalength = mex32.inputdatalength;
1740 	mex64.outputdata = compat_ptr(mex32.outputdata);
1741 	mex64.outputdatalength = mex32.outputdatalength;
1742 	mex64.b_key = compat_ptr(mex32.b_key);
1743 	mex64.n_modulus = compat_ptr(mex32.n_modulus);
1744 	do {
1745 		rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1746 		if (rc == -EAGAIN)
1747 			tr.again_counter++;
1748 	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1749 	/* on failure: retry once again after a requested rescan */
1750 	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1751 		do {
1752 			rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
1753 			if (rc == -EAGAIN)
1754 				tr.again_counter++;
1755 		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1756 	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1757 		rc = -EIO;
1758 	if (rc)
1759 		return rc;
1760 	return put_user(mex64.outputdatalength,
1761 			&umex32->outputdatalength);
1762 }
1763 
1764 struct compat_ica_rsa_modexpo_crt {
1765 	compat_uptr_t	inputdata;
1766 	unsigned int	inputdatalength;
1767 	compat_uptr_t	outputdata;
1768 	unsigned int	outputdatalength;
1769 	compat_uptr_t	bp_key;
1770 	compat_uptr_t	bq_key;
1771 	compat_uptr_t	np_prime;
1772 	compat_uptr_t	nq_prime;
1773 	compat_uptr_t	u_mult_inv;
1774 };
1775 
trans_modexpo_crt32(struct ap_perms * perms,struct file * filp,unsigned int cmd,unsigned long arg)1776 static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
1777 				unsigned int cmd, unsigned long arg)
1778 {
1779 	struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
1780 	struct compat_ica_rsa_modexpo_crt crt32;
1781 	struct ica_rsa_modexpo_crt crt64;
1782 	struct zcrypt_track tr;
1783 	long rc;
1784 
1785 	memset(&tr, 0, sizeof(tr));
1786 	if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
1787 		return -EFAULT;
1788 	crt64.inputdata = compat_ptr(crt32.inputdata);
1789 	crt64.inputdatalength = crt32.inputdatalength;
1790 	crt64.outputdata = compat_ptr(crt32.outputdata);
1791 	crt64.outputdatalength = crt32.outputdatalength;
1792 	crt64.bp_key = compat_ptr(crt32.bp_key);
1793 	crt64.bq_key = compat_ptr(crt32.bq_key);
1794 	crt64.np_prime = compat_ptr(crt32.np_prime);
1795 	crt64.nq_prime = compat_ptr(crt32.nq_prime);
1796 	crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
1797 	do {
1798 		rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1799 		if (rc == -EAGAIN)
1800 			tr.again_counter++;
1801 	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1802 	/* on failure: retry once again after a requested rescan */
1803 	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1804 		do {
1805 			rc = zcrypt_rsa_crt(perms, &tr, &crt64);
1806 			if (rc == -EAGAIN)
1807 				tr.again_counter++;
1808 		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1809 	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1810 		rc = -EIO;
1811 	if (rc)
1812 		return rc;
1813 	return put_user(crt64.outputdatalength,
1814 			&ucrt32->outputdatalength);
1815 }
1816 
1817 struct compat_ica_xcRB {
1818 	unsigned short	agent_ID;
1819 	unsigned int	user_defined;
1820 	unsigned short	request_ID;
1821 	unsigned int	request_control_blk_length;
1822 	unsigned char	padding1[16 - sizeof(compat_uptr_t)];
1823 	compat_uptr_t	request_control_blk_addr;
1824 	unsigned int	request_data_length;
1825 	char		padding2[16 - sizeof(compat_uptr_t)];
1826 	compat_uptr_t	request_data_address;
1827 	unsigned int	reply_control_blk_length;
1828 	char		padding3[16 - sizeof(compat_uptr_t)];
1829 	compat_uptr_t	reply_control_blk_addr;
1830 	unsigned int	reply_data_length;
1831 	char		padding4[16 - sizeof(compat_uptr_t)];
1832 	compat_uptr_t	reply_data_addr;
1833 	unsigned short	priority_window;
1834 	unsigned int	status;
1835 } __packed;
1836 
trans_xcRB32(struct ap_perms * perms,struct file * filp,unsigned int cmd,unsigned long arg)1837 static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
1838 			 unsigned int cmd, unsigned long arg)
1839 {
1840 	struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
1841 	struct compat_ica_xcRB xcRB32;
1842 	struct zcrypt_track tr;
1843 	struct ica_xcRB xcRB64;
1844 	long rc;
1845 
1846 	memset(&tr, 0, sizeof(tr));
1847 	if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
1848 		return -EFAULT;
1849 	xcRB64.agent_ID = xcRB32.agent_ID;
1850 	xcRB64.user_defined = xcRB32.user_defined;
1851 	xcRB64.request_ID = xcRB32.request_ID;
1852 	xcRB64.request_control_blk_length =
1853 		xcRB32.request_control_blk_length;
1854 	xcRB64.request_control_blk_addr =
1855 		compat_ptr(xcRB32.request_control_blk_addr);
1856 	xcRB64.request_data_length =
1857 		xcRB32.request_data_length;
1858 	xcRB64.request_data_address =
1859 		compat_ptr(xcRB32.request_data_address);
1860 	xcRB64.reply_control_blk_length =
1861 		xcRB32.reply_control_blk_length;
1862 	xcRB64.reply_control_blk_addr =
1863 		compat_ptr(xcRB32.reply_control_blk_addr);
1864 	xcRB64.reply_data_length = xcRB32.reply_data_length;
1865 	xcRB64.reply_data_addr =
1866 		compat_ptr(xcRB32.reply_data_addr);
1867 	xcRB64.priority_window = xcRB32.priority_window;
1868 	xcRB64.status = xcRB32.status;
1869 	do {
1870 		rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1871 		if (rc == -EAGAIN)
1872 			tr.again_counter++;
1873 	} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1874 	/* on failure: retry once again after a requested rescan */
1875 	if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1876 		do {
1877 			rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
1878 			if (rc == -EAGAIN)
1879 				tr.again_counter++;
1880 		} while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
1881 	if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
1882 		rc = -EIO;
1883 	xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
1884 	xcRB32.reply_data_length = xcRB64.reply_data_length;
1885 	xcRB32.status = xcRB64.status;
1886 	if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
1887 		return -EFAULT;
1888 	return rc;
1889 }
1890 
zcrypt_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1891 static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
1892 			 unsigned long arg)
1893 {
1894 	int rc;
1895 	struct ap_perms *perms =
1896 		(struct ap_perms *) filp->private_data;
1897 
1898 	rc = zcrypt_check_ioctl(perms, cmd);
1899 	if (rc)
1900 		return rc;
1901 
1902 	if (cmd == ICARSAMODEXPO)
1903 		return trans_modexpo32(perms, filp, cmd, arg);
1904 	if (cmd == ICARSACRT)
1905 		return trans_modexpo_crt32(perms, filp, cmd, arg);
1906 	if (cmd == ZSECSENDCPRB)
1907 		return trans_xcRB32(perms, filp, cmd, arg);
1908 	return zcrypt_unlocked_ioctl(filp, cmd, arg);
1909 }
1910 #endif
1911 
1912 /*
1913  * Misc device file operations.
1914  */
1915 static const struct file_operations zcrypt_fops = {
1916 	.owner		= THIS_MODULE,
1917 	.read		= zcrypt_read,
1918 	.write		= zcrypt_write,
1919 	.unlocked_ioctl	= zcrypt_unlocked_ioctl,
1920 #ifdef CONFIG_COMPAT
1921 	.compat_ioctl	= zcrypt_compat_ioctl,
1922 #endif
1923 	.open		= zcrypt_open,
1924 	.release	= zcrypt_release,
1925 	.llseek		= no_llseek,
1926 };
1927 
1928 /*
1929  * Misc device.
1930  */
1931 static struct miscdevice zcrypt_misc_device = {
1932 	.minor	    = MISC_DYNAMIC_MINOR,
1933 	.name	    = "z90crypt",
1934 	.fops	    = &zcrypt_fops,
1935 };
1936 
1937 static int zcrypt_rng_device_count;
1938 static u32 *zcrypt_rng_buffer;
1939 static int zcrypt_rng_buffer_index;
1940 static DEFINE_MUTEX(zcrypt_rng_mutex);
1941 
zcrypt_rng_data_read(struct hwrng * rng,u32 * data)1942 static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
1943 {
1944 	int rc;
1945 
1946 	/*
1947 	 * We don't need locking here because the RNG API guarantees serialized
1948 	 * read method calls.
1949 	 */
1950 	if (zcrypt_rng_buffer_index == 0) {
1951 		rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1952 		/* on failure: retry once again after a requested rescan */
1953 		if ((rc == -ENODEV) && (zcrypt_process_rescan()))
1954 			rc = zcrypt_rng((char *) zcrypt_rng_buffer);
1955 		if (rc < 0)
1956 			return -EIO;
1957 		zcrypt_rng_buffer_index = rc / sizeof(*data);
1958 	}
1959 	*data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
1960 	return sizeof(*data);
1961 }
1962 
1963 static struct hwrng zcrypt_rng_dev = {
1964 	.name		= "zcrypt",
1965 	.data_read	= zcrypt_rng_data_read,
1966 	.quality	= 990,
1967 };
1968 
zcrypt_rng_device_add(void)1969 int zcrypt_rng_device_add(void)
1970 {
1971 	int rc = 0;
1972 
1973 	mutex_lock(&zcrypt_rng_mutex);
1974 	if (zcrypt_rng_device_count == 0) {
1975 		zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
1976 		if (!zcrypt_rng_buffer) {
1977 			rc = -ENOMEM;
1978 			goto out;
1979 		}
1980 		zcrypt_rng_buffer_index = 0;
1981 		if (!zcrypt_hwrng_seed)
1982 			zcrypt_rng_dev.quality = 0;
1983 		rc = hwrng_register(&zcrypt_rng_dev);
1984 		if (rc)
1985 			goto out_free;
1986 		zcrypt_rng_device_count = 1;
1987 	} else
1988 		zcrypt_rng_device_count++;
1989 	mutex_unlock(&zcrypt_rng_mutex);
1990 	return 0;
1991 
1992 out_free:
1993 	free_page((unsigned long) zcrypt_rng_buffer);
1994 out:
1995 	mutex_unlock(&zcrypt_rng_mutex);
1996 	return rc;
1997 }
1998 
zcrypt_rng_device_remove(void)1999 void zcrypt_rng_device_remove(void)
2000 {
2001 	mutex_lock(&zcrypt_rng_mutex);
2002 	zcrypt_rng_device_count--;
2003 	if (zcrypt_rng_device_count == 0) {
2004 		hwrng_unregister(&zcrypt_rng_dev);
2005 		free_page((unsigned long) zcrypt_rng_buffer);
2006 	}
2007 	mutex_unlock(&zcrypt_rng_mutex);
2008 }
2009 
zcrypt_debug_init(void)2010 int __init zcrypt_debug_init(void)
2011 {
2012 	zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
2013 					 DBF_MAX_SPRINTF_ARGS * sizeof(long));
2014 	debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
2015 	debug_set_level(zcrypt_dbf_info, DBF_ERR);
2016 
2017 	return 0;
2018 }
2019 
zcrypt_debug_exit(void)2020 void zcrypt_debug_exit(void)
2021 {
2022 	debug_unregister(zcrypt_dbf_info);
2023 }
2024 
2025 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2026 
zcdn_init(void)2027 static int __init zcdn_init(void)
2028 {
2029 	int rc;
2030 
2031 	/* create a new class 'zcrypt' */
2032 	zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
2033 	if (IS_ERR(zcrypt_class)) {
2034 		rc = PTR_ERR(zcrypt_class);
2035 		goto out_class_create_failed;
2036 	}
2037 	zcrypt_class->dev_release = zcdn_device_release;
2038 
2039 	/* alloc device minor range */
2040 	rc = alloc_chrdev_region(&zcrypt_devt,
2041 				 0, ZCRYPT_MAX_MINOR_NODES,
2042 				 ZCRYPT_NAME);
2043 	if (rc)
2044 		goto out_alloc_chrdev_failed;
2045 
2046 	cdev_init(&zcrypt_cdev, &zcrypt_fops);
2047 	zcrypt_cdev.owner = THIS_MODULE;
2048 	rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2049 	if (rc)
2050 		goto out_cdev_add_failed;
2051 
2052 	/* need some class specific sysfs attributes */
2053 	rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
2054 	if (rc)
2055 		goto out_class_create_file_1_failed;
2056 	rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
2057 	if (rc)
2058 		goto out_class_create_file_2_failed;
2059 
2060 	return 0;
2061 
2062 out_class_create_file_2_failed:
2063 	class_remove_file(zcrypt_class, &class_attr_zcdn_create);
2064 out_class_create_file_1_failed:
2065 	cdev_del(&zcrypt_cdev);
2066 out_cdev_add_failed:
2067 	unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2068 out_alloc_chrdev_failed:
2069 	class_destroy(zcrypt_class);
2070 out_class_create_failed:
2071 	return rc;
2072 }
2073 
zcdn_exit(void)2074 static void zcdn_exit(void)
2075 {
2076 	class_remove_file(zcrypt_class, &class_attr_zcdn_create);
2077 	class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
2078 	zcdn_destroy_all();
2079 	cdev_del(&zcrypt_cdev);
2080 	unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
2081 	class_destroy(zcrypt_class);
2082 }
2083 
2084 #endif
2085 
2086 /**
2087  * zcrypt_api_init(): Module initialization.
2088  *
2089  * The module initialization code.
2090  */
zcrypt_api_init(void)2091 int __init zcrypt_api_init(void)
2092 {
2093 	int rc;
2094 
2095 	rc = zcrypt_debug_init();
2096 	if (rc)
2097 		goto out;
2098 
2099 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2100 	rc = zcdn_init();
2101 	if (rc)
2102 		goto out;
2103 #endif
2104 
2105 	/* Register the request sprayer. */
2106 	rc = misc_register(&zcrypt_misc_device);
2107 	if (rc < 0)
2108 		goto out_misc_register_failed;
2109 
2110 	zcrypt_msgtype6_init();
2111 	zcrypt_msgtype50_init();
2112 
2113 	return 0;
2114 
2115 out_misc_register_failed:
2116 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2117 	zcdn_exit();
2118 #endif
2119 	zcrypt_debug_exit();
2120 out:
2121 	return rc;
2122 }
2123 
2124 /**
2125  * zcrypt_api_exit(): Module termination.
2126  *
2127  * The module termination code.
2128  */
zcrypt_api_exit(void)2129 void __exit zcrypt_api_exit(void)
2130 {
2131 #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
2132 	zcdn_exit();
2133 #endif
2134 	misc_deregister(&zcrypt_misc_device);
2135 	zcrypt_msgtype6_exit();
2136 	zcrypt_msgtype50_exit();
2137 	zcrypt_ccamisc_exit();
2138 	zcrypt_ep11misc_exit();
2139 	zcrypt_debug_exit();
2140 }
2141 
2142 module_init(zcrypt_api_init);
2143 module_exit(zcrypt_api_exit);
2144