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