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