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