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