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