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