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