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> |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 28 | #include <linux/capability.h> |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 29 | #include <asm/debug.h> |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 30 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 31 | #define CREATE_TRACE_POINTS |
| 32 | #include <asm/trace/zcrypt.h> |
| 33 | |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 34 | #include "zcrypt_api.h" |
Harald Freudenberger | cccd85b | 2016-11-24 06:45:21 +0100 | [diff] [blame] | 35 | #include "zcrypt_debug.h" |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 36 | |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 37 | #include "zcrypt_msgtype6.h" |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 38 | #include "zcrypt_msgtype50.h" |
Harald Freudenberger | efc598e | 2019-06-11 11:16:56 +0200 | [diff] [blame] | 39 | #include "zcrypt_ccamisc.h" |
Harald Freudenberger | 7384eb7 | 2019-08-30 16:07:08 +0200 | [diff] [blame] | 40 | #include "zcrypt_ep11misc.h" |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 41 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 42 | /* |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 43 | * Module description. |
| 44 | */ |
| 45 | MODULE_AUTHOR("IBM Corporation"); |
Holger Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 46 | MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \ |
| 47 | "Copyright IBM Corp. 2001, 2012"); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 48 | MODULE_LICENSE("GPL"); |
| 49 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 50 | /* |
| 51 | * zcrypt tracepoint functions |
| 52 | */ |
| 53 | EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req); |
| 54 | EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep); |
| 55 | |
Ingo Tuchscherer | db490cb | 2015-03-17 16:02:20 +0100 | [diff] [blame] | 56 | static int zcrypt_hwrng_seed = 1; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 57 | module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440); |
Ingo Tuchscherer | db490cb | 2015-03-17 16:02:20 +0100 | [diff] [blame] | 58 | MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on)."); |
| 59 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 60 | DEFINE_SPINLOCK(zcrypt_list_lock); |
| 61 | LIST_HEAD(zcrypt_card_list); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 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 | |
Heiko Carstens | 2c57ad6 | 2021-09-07 07:28:20 +0200 | [diff] [blame] | 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 | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 85 | ZCRYPT_DBF_INFO("%s rescan count=%07d\n", __func__, |
| 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 | |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 344 | ZCRYPT_DBF_INFO("%s releasing zcdn device %d:%d\n", |
| 345 | __func__, MAJOR(dev->devt), MINOR(dev->devt)); |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 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 | |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 410 | ZCRYPT_DBF_INFO("%s created zcdn device %d:%d\n", |
| 411 | __func__, MAJOR(devt), MINOR(devt)); |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 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 | |
Heiko Carstens | 2c57ad6 | 2021-09-07 07:28:20 +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 | |
Heiko Carstens | 2c57ad6 | 2021-09-07 07:28:20 +0200 | [diff] [blame] | 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 | |
Heiko Carstens | 2c57ad6 | 2021-09-07 07:28:20 +0200 | [diff] [blame] | 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 | |
Heiko Carstens | 2c57ad6 | 2021-09-07 07:28:20 +0200 | [diff] [blame] | 515 | /* |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 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) |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 553 | ZCRYPT_DBF_WARN("%s ioctl check failed: ioctlnr=0x%04x rc=%d\n", |
| 554 | __func__, ioctlnr, rc); |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 555 | |
| 556 | return rc; |
| 557 | } |
| 558 | |
| 559 | static inline bool zcrypt_check_card(struct ap_perms *perms, int card) |
| 560 | { |
| 561 | return test_bit_inv(card, perms->apm) ? true : false; |
| 562 | } |
| 563 | |
| 564 | static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue) |
| 565 | { |
| 566 | return test_bit_inv(queue, perms->aqm) ? true : false; |
| 567 | } |
| 568 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 569 | static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc, |
| 570 | struct zcrypt_queue *zq, |
Harald Freudenberger | 01396a3 | 2019-02-22 17:24:11 +0100 | [diff] [blame] | 571 | struct module **pmod, |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 572 | unsigned int weight) |
| 573 | { |
Julian Wiedmann | c8c68c5 | 2021-06-07 11:18:43 +0200 | [diff] [blame] | 574 | if (!zq || !try_module_get(zq->queue->ap_dev.device.driver->owner)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 575 | return NULL; |
| 576 | zcrypt_queue_get(zq); |
| 577 | get_device(&zq->queue->ap_dev.device); |
| 578 | atomic_add(weight, &zc->load); |
| 579 | atomic_add(weight, &zq->load); |
| 580 | zq->request_count++; |
Julian Wiedmann | c8c68c5 | 2021-06-07 11:18:43 +0200 | [diff] [blame] | 581 | *pmod = zq->queue->ap_dev.device.driver->owner; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 582 | return zq; |
| 583 | } |
| 584 | |
| 585 | static inline void zcrypt_drop_queue(struct zcrypt_card *zc, |
| 586 | struct zcrypt_queue *zq, |
Harald Freudenberger | 01396a3 | 2019-02-22 17:24:11 +0100 | [diff] [blame] | 587 | struct module *mod, |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 588 | unsigned int weight) |
| 589 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 590 | zq->request_count--; |
| 591 | atomic_sub(weight, &zc->load); |
| 592 | atomic_sub(weight, &zq->load); |
| 593 | put_device(&zq->queue->ap_dev.device); |
| 594 | zcrypt_queue_put(zq); |
| 595 | module_put(mod); |
| 596 | } |
| 597 | |
Ingo Tuchscherer | e47de21 | 2016-10-14 14:34:51 +0200 | [diff] [blame] | 598 | static inline bool zcrypt_card_compare(struct zcrypt_card *zc, |
| 599 | struct zcrypt_card *pref_zc, |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 600 | unsigned int weight, |
| 601 | unsigned int pref_weight) |
Ingo Tuchscherer | e47de21 | 2016-10-14 14:34:51 +0200 | [diff] [blame] | 602 | { |
| 603 | if (!pref_zc) |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 604 | return true; |
Ingo Tuchscherer | e47de21 | 2016-10-14 14:34:51 +0200 | [diff] [blame] | 605 | weight += atomic_read(&zc->load); |
| 606 | pref_weight += atomic_read(&pref_zc->load); |
| 607 | if (weight == pref_weight) |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 608 | return atomic64_read(&zc->card->total_request_count) < |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 609 | atomic64_read(&pref_zc->card->total_request_count); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 610 | return weight < pref_weight; |
Ingo Tuchscherer | e47de21 | 2016-10-14 14:34:51 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq, |
| 614 | struct zcrypt_queue *pref_zq, |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 615 | unsigned int weight, |
| 616 | unsigned int pref_weight) |
Ingo Tuchscherer | e47de21 | 2016-10-14 14:34:51 +0200 | [diff] [blame] | 617 | { |
| 618 | if (!pref_zq) |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 619 | return true; |
Ingo Tuchscherer | e47de21 | 2016-10-14 14:34:51 +0200 | [diff] [blame] | 620 | weight += atomic_read(&zq->load); |
| 621 | pref_weight += atomic_read(&pref_zq->load); |
| 622 | if (weight == pref_weight) |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 623 | return zq->queue->total_request_count < |
Harald Freudenberger | 0b088267 | 2017-11-17 16:32:22 +0100 | [diff] [blame] | 624 | pref_zq->queue->total_request_count; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 625 | return weight < pref_weight; |
Ingo Tuchscherer | e47de21 | 2016-10-14 14:34:51 +0200 | [diff] [blame] | 626 | } |
| 627 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 628 | /* |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 629 | * zcrypt ioctls. |
| 630 | */ |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 631 | static long zcrypt_rsa_modexpo(struct ap_perms *perms, |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 632 | struct zcrypt_track *tr, |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 633 | struct ica_rsa_modexpo *mex) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 634 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 635 | struct zcrypt_card *zc, *pref_zc; |
| 636 | struct zcrypt_queue *zq, *pref_zq; |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 637 | struct ap_message ap_msg; |
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 | |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 645 | ap_init_message(&ap_msg); |
| 646 | |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 647 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 648 | if (tr && tr->fi.cmd) |
| 649 | ap_msg.fi.cmd = tr->fi.cmd; |
| 650 | #endif |
| 651 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 652 | if (mex->outputdatalength < mex->inputdatalength) { |
Arnd Bergmann | 913140e | 2019-04-08 23:26:18 +0200 | [diff] [blame] | 653 | func_code = 0; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 654 | rc = -EINVAL; |
| 655 | goto out; |
| 656 | } |
| 657 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 658 | /* |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 659 | * As long as outputdatalength is big enough, we can set the |
| 660 | * outputdatalength equal to the inputdatalength, since that is the |
| 661 | * number of bytes we will copy in any case |
| 662 | */ |
| 663 | mex->outputdatalength = mex->inputdatalength; |
| 664 | |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 665 | rc = get_rsa_modex_fc(mex, &func_code); |
| 666 | if (rc) |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 667 | goto out; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 668 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 669 | pref_zc = NULL; |
| 670 | pref_zq = NULL; |
| 671 | spin_lock(&zcrypt_list_lock); |
| 672 | for_each_zcrypt_card(zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 673 | /* Check for useable accelarator or CCA card */ |
| 674 | if (!zc->online || !zc->card->config || |
| 675 | !(zc->card->functions & 0x18000000)) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 676 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 677 | /* Check for size limits */ |
| 678 | if (zc->min_mod_size > mex->inputdatalength || |
| 679 | zc->max_mod_size < mex->inputdatalength) |
| 680 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 681 | /* check if device node has admission for this card */ |
| 682 | if (!zcrypt_check_card(perms, zc->card->id)) |
| 683 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 684 | /* get weight index of the card device */ |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 685 | wgt = zc->speed_rating[func_code]; |
| 686 | /* penalty if this msg was previously sent via this card */ |
| 687 | cpen = (tr && tr->again_counter && tr->last_qid && |
| 688 | AP_QID_CARD(tr->last_qid) == zc->card->id) ? |
| 689 | TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0; |
| 690 | if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 691 | continue; |
| 692 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 693 | /* check if device is useable and eligible */ |
| 694 | if (!zq->online || !zq->ops->rsa_modexpo || |
| 695 | !zq->queue->config) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 696 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 697 | /* check if device node has admission for this queue */ |
| 698 | if (!zcrypt_check_queue(perms, |
| 699 | AP_QID_QUEUE(zq->queue->qid))) |
| 700 | continue; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 701 | /* penalty if the msg was previously sent at this qid */ |
| 702 | qpen = (tr && tr->again_counter && tr->last_qid && |
| 703 | tr->last_qid == zq->queue->qid) ? |
| 704 | TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0; |
| 705 | if (!zcrypt_queue_compare(zq, pref_zq, |
| 706 | wgt + cpen + qpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 707 | continue; |
| 708 | pref_zc = zc; |
| 709 | pref_zq = zq; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 710 | pref_wgt = wgt + cpen + qpen; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 711 | } |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 712 | } |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 713 | pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 714 | spin_unlock(&zcrypt_list_lock); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 715 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 716 | if (!pref_zq) { |
| 717 | rc = -ENODEV; |
| 718 | goto out; |
| 719 | } |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 720 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 721 | qid = pref_zq->queue->qid; |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 722 | rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 723 | |
| 724 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 725 | zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 726 | spin_unlock(&zcrypt_list_lock); |
| 727 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 728 | out: |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 729 | ap_release_message(&ap_msg); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 730 | if (tr) { |
| 731 | tr->last_rc = rc; |
| 732 | tr->last_qid = qid; |
| 733 | } |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 734 | trace_s390_zcrypt_rep(mex, func_code, rc, |
| 735 | AP_QID_CARD(qid), AP_QID_QUEUE(qid)); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 736 | return rc; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 737 | } |
| 738 | |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 739 | static long zcrypt_rsa_crt(struct ap_perms *perms, |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 740 | struct zcrypt_track *tr, |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 741 | struct ica_rsa_modexpo_crt *crt) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 742 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 743 | struct zcrypt_card *zc, *pref_zc; |
| 744 | struct zcrypt_queue *zq, *pref_zq; |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 745 | struct ap_message ap_msg; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 746 | unsigned int wgt = 0, pref_wgt = 0; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 747 | unsigned int func_code; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 748 | int cpen, qpen, qid = 0, rc = -ENODEV; |
Harald Freudenberger | 01396a3 | 2019-02-22 17:24:11 +0100 | [diff] [blame] | 749 | struct module *mod; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 750 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 751 | trace_s390_zcrypt_req(crt, TP_ICARSACRT); |
| 752 | |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 753 | ap_init_message(&ap_msg); |
| 754 | |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 755 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 756 | if (tr && tr->fi.cmd) |
| 757 | ap_msg.fi.cmd = tr->fi.cmd; |
| 758 | #endif |
| 759 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 760 | if (crt->outputdatalength < crt->inputdatalength) { |
Arnd Bergmann | 913140e | 2019-04-08 23:26:18 +0200 | [diff] [blame] | 761 | func_code = 0; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 762 | rc = -EINVAL; |
| 763 | goto out; |
| 764 | } |
| 765 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 766 | /* |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 767 | * As long as outputdatalength is big enough, we can set the |
| 768 | * outputdatalength equal to the inputdatalength, since that is the |
| 769 | * number of bytes we will copy in any case |
| 770 | */ |
| 771 | crt->outputdatalength = crt->inputdatalength; |
| 772 | |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 773 | rc = get_rsa_crt_fc(crt, &func_code); |
| 774 | if (rc) |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 775 | goto out; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 776 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 777 | pref_zc = NULL; |
| 778 | pref_zq = NULL; |
| 779 | spin_lock(&zcrypt_list_lock); |
| 780 | for_each_zcrypt_card(zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 781 | /* Check for useable accelarator or CCA card */ |
| 782 | if (!zc->online || !zc->card->config || |
| 783 | !(zc->card->functions & 0x18000000)) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 784 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 785 | /* Check for size limits */ |
| 786 | if (zc->min_mod_size > crt->inputdatalength || |
| 787 | zc->max_mod_size < crt->inputdatalength) |
| 788 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 789 | /* check if device node has admission for this card */ |
| 790 | if (!zcrypt_check_card(perms, zc->card->id)) |
| 791 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 792 | /* get weight index of the card device */ |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 793 | wgt = zc->speed_rating[func_code]; |
| 794 | /* penalty if this msg was previously sent via this card */ |
| 795 | cpen = (tr && tr->again_counter && tr->last_qid && |
| 796 | AP_QID_CARD(tr->last_qid) == zc->card->id) ? |
| 797 | TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0; |
| 798 | if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 799 | continue; |
| 800 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 801 | /* check if device is useable and eligible */ |
| 802 | if (!zq->online || !zq->ops->rsa_modexpo_crt || |
| 803 | !zq->queue->config) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 804 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 805 | /* check if device node has admission for this queue */ |
| 806 | if (!zcrypt_check_queue(perms, |
| 807 | AP_QID_QUEUE(zq->queue->qid))) |
| 808 | continue; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 809 | /* penalty if the msg was previously sent at this qid */ |
| 810 | qpen = (tr && tr->again_counter && tr->last_qid && |
| 811 | tr->last_qid == zq->queue->qid) ? |
| 812 | TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0; |
| 813 | if (!zcrypt_queue_compare(zq, pref_zq, |
| 814 | wgt + cpen + qpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 815 | continue; |
| 816 | pref_zc = zc; |
| 817 | pref_zq = zq; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 818 | pref_wgt = wgt + cpen + qpen; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 819 | } |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 820 | } |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 821 | pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 822 | spin_unlock(&zcrypt_list_lock); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 823 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 824 | if (!pref_zq) { |
| 825 | rc = -ENODEV; |
| 826 | goto out; |
| 827 | } |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 828 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 829 | qid = pref_zq->queue->qid; |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 830 | rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 831 | |
| 832 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 833 | zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 834 | spin_unlock(&zcrypt_list_lock); |
| 835 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 836 | out: |
Harald Freudenberger | 3730f53 | 2020-09-26 22:51:38 +0200 | [diff] [blame] | 837 | ap_release_message(&ap_msg); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 838 | if (tr) { |
| 839 | tr->last_rc = rc; |
| 840 | tr->last_qid = qid; |
| 841 | } |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 842 | trace_s390_zcrypt_rep(crt, func_code, rc, |
| 843 | AP_QID_CARD(qid), AP_QID_QUEUE(qid)); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 844 | return rc; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 845 | } |
| 846 | |
Harald Freudenberger | 52f72fe | 2020-09-15 17:01:58 +0200 | [diff] [blame] | 847 | static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms, |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 848 | struct zcrypt_track *tr, |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 849 | struct ica_xcRB *xcRB) |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 850 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 851 | struct zcrypt_card *zc, *pref_zc; |
| 852 | struct zcrypt_queue *zq, *pref_zq; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 853 | struct ap_message ap_msg; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 854 | unsigned int wgt = 0, pref_wgt = 0; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 855 | unsigned int func_code; |
Harald Freudenberger | 7379e65 | 2019-05-21 13:50:09 +0200 | [diff] [blame] | 856 | unsigned short *domain, tdom; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 857 | int cpen, qpen, qid = 0, rc = -ENODEV; |
Harald Freudenberger | 01396a3 | 2019-02-22 17:24:11 +0100 | [diff] [blame] | 858 | struct module *mod; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 859 | |
| 860 | trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 861 | |
Harald Freudenberger | a17b92e | 2018-10-04 15:37:49 +0200 | [diff] [blame] | 862 | xcRB->status = 0; |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 863 | ap_init_message(&ap_msg); |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 864 | |
| 865 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 866 | if (tr && tr->fi.cmd) |
| 867 | ap_msg.fi.cmd = tr->fi.cmd; |
| 868 | if (tr && tr->fi.action == AP_FI_ACTION_CCA_AGENT_FF) { |
| 869 | ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid agent_ID 'FF'\n", |
| 870 | __func__, tr->fi.cmd); |
| 871 | xcRB->agent_ID = 0x4646; |
| 872 | } |
| 873 | #endif |
| 874 | |
Harald Freudenberger | 52f72fe | 2020-09-15 17:01:58 +0200 | [diff] [blame] | 875 | rc = get_cprb_fc(userspace, xcRB, &ap_msg, &func_code, &domain); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 876 | if (rc) |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 877 | goto out; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 878 | |
Harald Freudenberger | 7379e65 | 2019-05-21 13:50:09 +0200 | [diff] [blame] | 879 | /* |
| 880 | * If a valid target domain is set and this domain is NOT a usage |
Juergen Christ | cff2d3a | 2021-11-22 14:01:34 +0100 | [diff] [blame] | 881 | * domain but a control only domain, autoselect target domain. |
Harald Freudenberger | 7379e65 | 2019-05-21 13:50:09 +0200 | [diff] [blame] | 882 | */ |
| 883 | tdom = *domain; |
Harald Freudenberger | 47c07bf | 2020-06-30 09:54:50 +0200 | [diff] [blame] | 884 | if (tdom < AP_DOMAINS && |
Harald Freudenberger | 7379e65 | 2019-05-21 13:50:09 +0200 | [diff] [blame] | 885 | !ap_test_config_usage_domain(tdom) && |
Juergen Christ | cff2d3a | 2021-11-22 14:01:34 +0100 | [diff] [blame] | 886 | ap_test_config_ctrl_domain(tdom)) |
| 887 | tdom = AUTOSEL_DOM; |
Harald Freudenberger | 7379e65 | 2019-05-21 13:50:09 +0200 | [diff] [blame] | 888 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 889 | pref_zc = NULL; |
| 890 | pref_zq = NULL; |
| 891 | spin_lock(&zcrypt_list_lock); |
| 892 | for_each_zcrypt_card(zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 893 | /* Check for useable CCA card */ |
| 894 | if (!zc->online || !zc->card->config || |
| 895 | !(zc->card->functions & 0x10000000)) |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 896 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 897 | /* Check for user selected CCA card */ |
| 898 | if (xcRB->user_defined != AUTOSELECT && |
| 899 | xcRB->user_defined != zc->card->id) |
| 900 | continue; |
Harald Freudenberger | bd39654 | 2021-06-25 12:29:46 +0200 | [diff] [blame] | 901 | /* check if request size exceeds card max msg size */ |
| 902 | if (ap_msg.len > zc->card->maxmsgsize) |
| 903 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 904 | /* check if device node has admission for this card */ |
| 905 | if (!zcrypt_check_card(perms, zc->card->id)) |
| 906 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 907 | /* get weight index of the card device */ |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 908 | wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY]; |
| 909 | /* penalty if this msg was previously sent via this card */ |
| 910 | cpen = (tr && tr->again_counter && tr->last_qid && |
| 911 | AP_QID_CARD(tr->last_qid) == zc->card->id) ? |
| 912 | TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0; |
| 913 | if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 914 | continue; |
| 915 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 916 | /* check for device useable and eligible */ |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 917 | if (!zq->online || |
Harald Freudenberger | 1487842 | 2016-10-27 08:57:39 +0200 | [diff] [blame] | 918 | !zq->ops->send_cprb || |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 919 | !zq->queue->config || |
Harald Freudenberger | 8f291eb | 2019-08-30 15:17:27 +0200 | [diff] [blame] | 920 | (tdom != AUTOSEL_DOM && |
Harald Freudenberger | 7379e65 | 2019-05-21 13:50:09 +0200 | [diff] [blame] | 921 | tdom != AP_QID_QUEUE(zq->queue->qid))) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 922 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 923 | /* check if device node has admission for this queue */ |
| 924 | if (!zcrypt_check_queue(perms, |
| 925 | AP_QID_QUEUE(zq->queue->qid))) |
| 926 | continue; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 927 | /* penalty if the msg was previously sent at this qid */ |
| 928 | qpen = (tr && tr->again_counter && tr->last_qid && |
| 929 | tr->last_qid == zq->queue->qid) ? |
| 930 | TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0; |
| 931 | if (!zcrypt_queue_compare(zq, pref_zq, |
| 932 | wgt + cpen + qpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 933 | continue; |
| 934 | pref_zc = zc; |
| 935 | pref_zq = zq; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 936 | pref_wgt = wgt + cpen + qpen; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 937 | } |
| 938 | } |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 939 | pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 940 | spin_unlock(&zcrypt_list_lock); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 941 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 942 | if (!pref_zq) { |
| 943 | rc = -ENODEV; |
| 944 | goto out; |
| 945 | } |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 946 | |
| 947 | /* in case of auto select, provide the correct domain */ |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 948 | qid = pref_zq->queue->qid; |
Harald Freudenberger | 8f291eb | 2019-08-30 15:17:27 +0200 | [diff] [blame] | 949 | if (*domain == AUTOSEL_DOM) |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 950 | *domain = AP_QID_QUEUE(qid); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 951 | |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 952 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 953 | if (tr && tr->fi.action == AP_FI_ACTION_CCA_DOM_INVAL) { |
| 954 | ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid domain\n", |
| 955 | __func__, tr->fi.cmd); |
| 956 | *domain = 99; |
| 957 | } |
| 958 | #endif |
| 959 | |
Harald Freudenberger | 52f72fe | 2020-09-15 17:01:58 +0200 | [diff] [blame] | 960 | rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcRB, &ap_msg); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 961 | |
| 962 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 963 | zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 964 | spin_unlock(&zcrypt_list_lock); |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 965 | |
| 966 | out: |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 967 | ap_release_message(&ap_msg); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 968 | if (tr) { |
| 969 | tr->last_rc = rc; |
| 970 | tr->last_qid = qid; |
| 971 | } |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 972 | trace_s390_zcrypt_rep(xcRB, func_code, rc, |
| 973 | AP_QID_CARD(qid), AP_QID_QUEUE(qid)); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 974 | return rc; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 975 | } |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 976 | |
| 977 | long zcrypt_send_cprb(struct ica_xcRB *xcRB) |
| 978 | { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 979 | return _zcrypt_send_cprb(false, &ap_perms, NULL, xcRB); |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 980 | } |
Harald Freudenberger | a1d001e | 2016-11-02 14:32:32 +0100 | [diff] [blame] | 981 | EXPORT_SYMBOL(zcrypt_send_cprb); |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 982 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 983 | static bool is_desired_ep11_card(unsigned int dev_id, |
| 984 | unsigned short target_num, |
| 985 | struct ep11_target_dev *targets) |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 986 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 987 | while (target_num-- > 0) { |
Harald Freudenberger | 8f291eb | 2019-08-30 15:17:27 +0200 | [diff] [blame] | 988 | if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP) |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 989 | return true; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 990 | targets++; |
| 991 | } |
| 992 | return false; |
| 993 | } |
| 994 | |
| 995 | static bool is_desired_ep11_queue(unsigned int dev_qid, |
| 996 | unsigned short target_num, |
| 997 | struct ep11_target_dev *targets) |
| 998 | { |
Harald Freudenberger | 8f291eb | 2019-08-30 15:17:27 +0200 | [diff] [blame] | 999 | int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid); |
| 1000 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1001 | while (target_num-- > 0) { |
Harald Freudenberger | 8f291eb | 2019-08-30 15:17:27 +0200 | [diff] [blame] | 1002 | if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) && |
| 1003 | (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1004 | return true; |
| 1005 | targets++; |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1006 | } |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
Harald Freudenberger | 52f72fe | 2020-09-15 17:01:58 +0200 | [diff] [blame] | 1010 | static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms, |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1011 | struct zcrypt_track *tr, |
Harald Freudenberger | a736799 | 2019-08-30 15:57:17 +0200 | [diff] [blame] | 1012 | struct ep11_urb *xcrb) |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1013 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1014 | struct zcrypt_card *zc, *pref_zc; |
| 1015 | struct zcrypt_queue *zq, *pref_zq; |
| 1016 | struct ep11_target_dev *targets; |
| 1017 | unsigned short target_num; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1018 | unsigned int wgt = 0, pref_wgt = 0; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1019 | unsigned int func_code; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1020 | struct ap_message ap_msg; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1021 | int cpen, qpen, qid = 0, rc = -ENODEV; |
Harald Freudenberger | 01396a3 | 2019-02-22 17:24:11 +0100 | [diff] [blame] | 1022 | struct module *mod; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1023 | |
| 1024 | trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB); |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1025 | |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 1026 | ap_init_message(&ap_msg); |
| 1027 | |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1028 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1029 | if (tr && tr->fi.cmd) |
| 1030 | ap_msg.fi.cmd = tr->fi.cmd; |
| 1031 | #endif |
| 1032 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1033 | target_num = (unsigned short) xcrb->targets_num; |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1034 | |
| 1035 | /* empty list indicates autoselect (all available targets) */ |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1036 | targets = NULL; |
| 1037 | if (target_num != 0) { |
| 1038 | struct ep11_target_dev __user *uptr; |
| 1039 | |
| 1040 | targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL); |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1041 | if (!targets) { |
Arnd Bergmann | 913140e | 2019-04-08 23:26:18 +0200 | [diff] [blame] | 1042 | func_code = 0; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1043 | rc = -ENOMEM; |
| 1044 | goto out; |
| 1045 | } |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1046 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1047 | uptr = (struct ep11_target_dev __force __user *) xcrb->targets; |
Harald Freudenberger | 52f72fe | 2020-09-15 17:01:58 +0200 | [diff] [blame] | 1048 | if (z_copy_from_user(userspace, targets, uptr, |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1049 | target_num * sizeof(*targets))) { |
Arnd Bergmann | 913140e | 2019-04-08 23:26:18 +0200 | [diff] [blame] | 1050 | func_code = 0; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1051 | rc = -EFAULT; |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 1052 | goto out_free; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1053 | } |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Harald Freudenberger | 52f72fe | 2020-09-15 17:01:58 +0200 | [diff] [blame] | 1056 | rc = get_ep11cprb_fc(userspace, xcrb, &ap_msg, &func_code); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1057 | if (rc) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1058 | goto out_free; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1059 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1060 | pref_zc = NULL; |
| 1061 | pref_zq = NULL; |
| 1062 | spin_lock(&zcrypt_list_lock); |
| 1063 | for_each_zcrypt_card(zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 1064 | /* Check for useable EP11 card */ |
| 1065 | if (!zc->online || !zc->card->config || |
| 1066 | !(zc->card->functions & 0x04000000)) |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1067 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1068 | /* Check for user selected EP11 card */ |
| 1069 | if (targets && |
| 1070 | !is_desired_ep11_card(zc->card->id, target_num, targets)) |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1071 | continue; |
Harald Freudenberger | bd39654 | 2021-06-25 12:29:46 +0200 | [diff] [blame] | 1072 | /* check if request size exceeds card max msg size */ |
| 1073 | if (ap_msg.len > zc->card->maxmsgsize) |
| 1074 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1075 | /* check if device node has admission for this card */ |
| 1076 | if (!zcrypt_check_card(perms, zc->card->id)) |
| 1077 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1078 | /* get weight index of the card device */ |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1079 | wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY]; |
| 1080 | /* penalty if this msg was previously sent via this card */ |
| 1081 | cpen = (tr && tr->again_counter && tr->last_qid && |
| 1082 | AP_QID_CARD(tr->last_qid) == zc->card->id) ? |
| 1083 | TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0; |
| 1084 | if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1085 | continue; |
| 1086 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 1087 | /* check if device is useable and eligible */ |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1088 | if (!zq->online || |
Harald Freudenberger | 1487842 | 2016-10-27 08:57:39 +0200 | [diff] [blame] | 1089 | !zq->ops->send_ep11_cprb || |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 1090 | !zq->queue->config || |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1091 | (targets && |
| 1092 | !is_desired_ep11_queue(zq->queue->qid, |
| 1093 | target_num, targets))) |
| 1094 | continue; |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1095 | /* check if device node has admission for this queue */ |
| 1096 | if (!zcrypt_check_queue(perms, |
| 1097 | AP_QID_QUEUE(zq->queue->qid))) |
| 1098 | continue; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1099 | /* penalty if the msg was previously sent at this qid */ |
| 1100 | qpen = (tr && tr->again_counter && tr->last_qid && |
| 1101 | tr->last_qid == zq->queue->qid) ? |
| 1102 | TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0; |
| 1103 | if (!zcrypt_queue_compare(zq, pref_zq, |
| 1104 | wgt + cpen + qpen, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1105 | continue; |
| 1106 | pref_zc = zc; |
| 1107 | pref_zq = zq; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1108 | pref_wgt = wgt + cpen + qpen; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1109 | } |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1110 | } |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1111 | pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1112 | spin_unlock(&zcrypt_list_lock); |
| 1113 | |
| 1114 | if (!pref_zq) { |
| 1115 | rc = -ENODEV; |
| 1116 | goto out_free; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1119 | qid = pref_zq->queue->qid; |
Harald Freudenberger | 52f72fe | 2020-09-15 17:01:58 +0200 | [diff] [blame] | 1120 | 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] | 1121 | |
| 1122 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1123 | zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1124 | spin_unlock(&zcrypt_list_lock); |
| 1125 | |
| 1126 | out_free: |
| 1127 | kfree(targets); |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1128 | out: |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 1129 | ap_release_message(&ap_msg); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1130 | if (tr) { |
| 1131 | tr->last_rc = rc; |
| 1132 | tr->last_qid = qid; |
| 1133 | } |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1134 | trace_s390_zcrypt_rep(xcrb, func_code, rc, |
| 1135 | AP_QID_CARD(qid), AP_QID_QUEUE(qid)); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1136 | return rc; |
Ingo Tuchscherer | 91f3e3ea | 2013-11-20 10:47:13 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
Harald Freudenberger | a736799 | 2019-08-30 15:57:17 +0200 | [diff] [blame] | 1139 | long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb) |
| 1140 | { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1141 | return _zcrypt_send_ep11_cprb(false, &ap_perms, NULL, xcrb); |
Harald Freudenberger | a736799 | 2019-08-30 15:57:17 +0200 | [diff] [blame] | 1142 | } |
| 1143 | EXPORT_SYMBOL(zcrypt_send_ep11_cprb); |
| 1144 | |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1145 | static long zcrypt_rng(char *buffer) |
| 1146 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1147 | struct zcrypt_card *zc, *pref_zc; |
| 1148 | struct zcrypt_queue *zq, *pref_zq; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1149 | unsigned int wgt = 0, pref_wgt = 0; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1150 | unsigned int func_code; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1151 | struct ap_message ap_msg; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1152 | unsigned int domain; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1153 | int qid = 0, rc = -ENODEV; |
Harald Freudenberger | 01396a3 | 2019-02-22 17:24:11 +0100 | [diff] [blame] | 1154 | struct module *mod; |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1155 | |
| 1156 | trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB); |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1157 | |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 1158 | ap_init_message(&ap_msg); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1159 | rc = get_rng_fc(&ap_msg, &func_code, &domain); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1160 | if (rc) |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1161 | goto out; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1162 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1163 | pref_zc = NULL; |
| 1164 | pref_zq = NULL; |
| 1165 | spin_lock(&zcrypt_list_lock); |
| 1166 | for_each_zcrypt_card(zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 1167 | /* Check for useable CCA card */ |
| 1168 | if (!zc->online || !zc->card->config || |
| 1169 | !(zc->card->functions & 0x10000000)) |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1170 | continue; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1171 | /* get weight index of the card device */ |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1172 | wgt = zc->speed_rating[func_code]; |
| 1173 | if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1174 | continue; |
| 1175 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | 4f2fccc | 2020-07-02 16:57:00 +0200 | [diff] [blame] | 1176 | /* check if device is useable and eligible */ |
| 1177 | if (!zq->online || !zq->ops->rng || |
| 1178 | !zq->queue->config) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1179 | continue; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1180 | if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt)) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1181 | continue; |
| 1182 | pref_zc = zc; |
| 1183 | pref_zq = zq; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1184 | pref_wgt = wgt; |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1185 | } |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1186 | } |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1187 | pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1188 | spin_unlock(&zcrypt_list_lock); |
| 1189 | |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 1190 | if (!pref_zq) { |
| 1191 | rc = -ENODEV; |
| 1192 | goto out; |
| 1193 | } |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1194 | |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1195 | qid = pref_zq->queue->qid; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1196 | rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg); |
| 1197 | |
| 1198 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1199 | zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1200 | spin_unlock(&zcrypt_list_lock); |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1201 | |
| 1202 | out: |
Harald Freudenberger | 89a0c0e | 2018-05-28 13:00:42 +0200 | [diff] [blame] | 1203 | ap_release_message(&ap_msg); |
Harald Freudenberger | 13b251b | 2016-11-25 18:04:56 +0100 | [diff] [blame] | 1204 | trace_s390_zcrypt_rep(buffer, func_code, rc, |
| 1205 | AP_QID_CARD(qid), AP_QID_QUEUE(qid)); |
Ingo Tuchscherer | 34a1516 | 2016-08-25 11:14:15 +0200 | [diff] [blame] | 1206 | return rc; |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1209 | static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus) |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1210 | { |
| 1211 | struct zcrypt_card *zc; |
| 1212 | struct zcrypt_queue *zq; |
| 1213 | struct zcrypt_device_status *stat; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1214 | int card, queue; |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1215 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1216 | memset(devstatus, 0, MAX_ZDEV_ENTRIES |
| 1217 | * sizeof(struct zcrypt_device_status)); |
| 1218 | |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1219 | spin_lock(&zcrypt_list_lock); |
| 1220 | for_each_zcrypt_card(zc) { |
| 1221 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1222 | card = AP_QID_CARD(zq->queue->qid); |
| 1223 | if (card >= MAX_ZDEV_CARDIDS) |
| 1224 | continue; |
| 1225 | queue = AP_QID_QUEUE(zq->queue->qid); |
| 1226 | stat = &devstatus[card * AP_DOMAINS + queue]; |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1227 | stat->hwtype = zc->card->ap_dev.device_type; |
| 1228 | stat->functions = zc->card->functions >> 26; |
| 1229 | stat->qid = zq->queue->qid; |
| 1230 | stat->online = zq->online ? 0x01 : 0x00; |
| 1231 | } |
| 1232 | } |
| 1233 | spin_unlock(&zcrypt_list_lock); |
| 1234 | } |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1235 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1236 | void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1237 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1238 | struct zcrypt_card *zc; |
| 1239 | struct zcrypt_queue *zq; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1240 | struct zcrypt_device_status_ext *stat; |
| 1241 | int card, queue; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1242 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1243 | memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT |
| 1244 | * sizeof(struct zcrypt_device_status_ext)); |
| 1245 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1246 | spin_lock(&zcrypt_list_lock); |
| 1247 | for_each_zcrypt_card(zc) { |
| 1248 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1249 | card = AP_QID_CARD(zq->queue->qid); |
| 1250 | queue = AP_QID_QUEUE(zq->queue->qid); |
| 1251 | stat = &devstatus[card * AP_DOMAINS + queue]; |
| 1252 | stat->hwtype = zc->card->ap_dev.device_type; |
| 1253 | stat->functions = zc->card->functions >> 26; |
| 1254 | stat->qid = zq->queue->qid; |
| 1255 | stat->online = zq->online ? 0x01 : 0x00; |
| 1256 | } |
| 1257 | } |
| 1258 | spin_unlock(&zcrypt_list_lock); |
| 1259 | } |
| 1260 | EXPORT_SYMBOL(zcrypt_device_status_mask_ext); |
| 1261 | |
Harald Freudenberger | 4da57a2 | 2019-06-18 15:53:12 +0200 | [diff] [blame] | 1262 | int zcrypt_device_status_ext(int card, int queue, |
| 1263 | struct zcrypt_device_status_ext *devstat) |
| 1264 | { |
| 1265 | struct zcrypt_card *zc; |
| 1266 | struct zcrypt_queue *zq; |
| 1267 | |
| 1268 | memset(devstat, 0, sizeof(*devstat)); |
| 1269 | |
| 1270 | spin_lock(&zcrypt_list_lock); |
| 1271 | for_each_zcrypt_card(zc) { |
| 1272 | for_each_zcrypt_queue(zq, zc) { |
| 1273 | if (card == AP_QID_CARD(zq->queue->qid) && |
| 1274 | queue == AP_QID_QUEUE(zq->queue->qid)) { |
| 1275 | devstat->hwtype = zc->card->ap_dev.device_type; |
| 1276 | devstat->functions = zc->card->functions >> 26; |
| 1277 | devstat->qid = zq->queue->qid; |
| 1278 | devstat->online = zq->online ? 0x01 : 0x00; |
| 1279 | spin_unlock(&zcrypt_list_lock); |
| 1280 | return 0; |
| 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | spin_unlock(&zcrypt_list_lock); |
| 1285 | |
| 1286 | return -ENODEV; |
| 1287 | } |
| 1288 | EXPORT_SYMBOL(zcrypt_device_status_ext); |
| 1289 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1290 | static void zcrypt_status_mask(char status[], size_t max_adapters) |
| 1291 | { |
| 1292 | struct zcrypt_card *zc; |
| 1293 | struct zcrypt_queue *zq; |
| 1294 | int card; |
| 1295 | |
| 1296 | memset(status, 0, max_adapters); |
| 1297 | spin_lock(&zcrypt_list_lock); |
| 1298 | for_each_zcrypt_card(zc) { |
| 1299 | for_each_zcrypt_queue(zq, zc) { |
| 1300 | card = AP_QID_CARD(zq->queue->qid); |
| 1301 | if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index |
| 1302 | || card >= max_adapters) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1303 | continue; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1304 | status[card] = zc->online ? zc->user_space_type : 0x0d; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | spin_unlock(&zcrypt_list_lock); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1310 | static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters) |
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 | struct zcrypt_card *zc; |
| 1313 | struct zcrypt_queue *zq; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1314 | int card; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1315 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1316 | memset(qdepth, 0, max_adapters); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1317 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1318 | local_bh_disable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1319 | for_each_zcrypt_card(zc) { |
| 1320 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1321 | card = AP_QID_CARD(zq->queue->qid); |
| 1322 | if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index |
| 1323 | || card >= max_adapters) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1324 | continue; |
| 1325 | spin_lock(&zq->queue->lock); |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1326 | qdepth[card] = |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1327 | zq->queue->pendingq_count + |
| 1328 | zq->queue->requestq_count; |
| 1329 | spin_unlock(&zq->queue->lock); |
| 1330 | } |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1331 | } |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1332 | local_bh_enable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1333 | spin_unlock(&zcrypt_list_lock); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 1336 | static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1337 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1338 | struct zcrypt_card *zc; |
| 1339 | struct zcrypt_queue *zq; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1340 | int card; |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 1341 | u64 cnt; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1342 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1343 | memset(reqcnt, 0, sizeof(int) * max_adapters); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1344 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1345 | local_bh_disable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1346 | for_each_zcrypt_card(zc) { |
| 1347 | for_each_zcrypt_queue(zq, zc) { |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1348 | card = AP_QID_CARD(zq->queue->qid); |
| 1349 | if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index |
| 1350 | || card >= max_adapters) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1351 | continue; |
| 1352 | spin_lock(&zq->queue->lock); |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 1353 | cnt = zq->queue->total_request_count; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1354 | spin_unlock(&zq->queue->lock); |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 1355 | reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX; |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1356 | } |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1357 | } |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1358 | local_bh_enable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1359 | spin_unlock(&zcrypt_list_lock); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static int zcrypt_pendingq_count(void) |
| 1363 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1364 | struct zcrypt_card *zc; |
| 1365 | struct zcrypt_queue *zq; |
| 1366 | int pendingq_count; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1367 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1368 | pendingq_count = 0; |
| 1369 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1370 | local_bh_disable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1371 | for_each_zcrypt_card(zc) { |
| 1372 | for_each_zcrypt_queue(zq, zc) { |
| 1373 | if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index) |
| 1374 | continue; |
| 1375 | spin_lock(&zq->queue->lock); |
| 1376 | pendingq_count += zq->queue->pendingq_count; |
| 1377 | spin_unlock(&zq->queue->lock); |
| 1378 | } |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1379 | } |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1380 | local_bh_enable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1381 | spin_unlock(&zcrypt_list_lock); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1382 | return pendingq_count; |
| 1383 | } |
| 1384 | |
| 1385 | static int zcrypt_requestq_count(void) |
| 1386 | { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1387 | struct zcrypt_card *zc; |
| 1388 | struct zcrypt_queue *zq; |
| 1389 | int requestq_count; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1390 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1391 | requestq_count = 0; |
| 1392 | spin_lock(&zcrypt_list_lock); |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1393 | local_bh_disable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1394 | for_each_zcrypt_card(zc) { |
| 1395 | for_each_zcrypt_queue(zq, zc) { |
| 1396 | if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index) |
| 1397 | continue; |
| 1398 | spin_lock(&zq->queue->lock); |
| 1399 | requestq_count += zq->queue->requestq_count; |
| 1400 | spin_unlock(&zq->queue->lock); |
| 1401 | } |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1402 | } |
Harald Freudenberger | 7fbe5c0 | 2017-01-16 09:43:29 +0100 | [diff] [blame] | 1403 | local_bh_enable(); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1404 | spin_unlock(&zcrypt_list_lock); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1405 | return requestq_count; |
| 1406 | } |
| 1407 | |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1408 | static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg) |
| 1409 | { |
| 1410 | int rc; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1411 | struct zcrypt_track tr; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1412 | struct ica_rsa_modexpo mex; |
| 1413 | struct ica_rsa_modexpo __user *umex = (void __user *) arg; |
| 1414 | |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1415 | memset(&tr, 0, sizeof(tr)); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1416 | if (copy_from_user(&mex, umex, sizeof(mex))) |
| 1417 | return -EFAULT; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1418 | |
| 1419 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1420 | if (mex.inputdatalength & (1U << 31)) { |
| 1421 | if (!capable(CAP_SYS_ADMIN)) |
| 1422 | return -EPERM; |
| 1423 | tr.fi.cmd = (u16)(mex.inputdatalength >> 16); |
| 1424 | } |
| 1425 | mex.inputdatalength &= 0x0000FFFF; |
| 1426 | #endif |
| 1427 | |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1428 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1429 | rc = zcrypt_rsa_modexpo(perms, &tr, &mex); |
| 1430 | if (rc == -EAGAIN) |
| 1431 | tr.again_counter++; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1432 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1433 | if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY)) |
| 1434 | break; |
| 1435 | #endif |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1436 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1437 | /* on failure: retry once again after a requested rescan */ |
| 1438 | if ((rc == -ENODEV) && (zcrypt_process_rescan())) |
| 1439 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1440 | rc = zcrypt_rsa_modexpo(perms, &tr, &mex); |
| 1441 | if (rc == -EAGAIN) |
| 1442 | tr.again_counter++; |
| 1443 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | d39fae4 | 2021-01-21 16:03:08 +0100 | [diff] [blame] | 1444 | if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX) |
| 1445 | rc = -EIO; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1446 | if (rc) { |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 1447 | ZCRYPT_DBF_DBG("ioctl ICARSAMODEXPO rc=%d\n", rc); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1448 | return rc; |
| 1449 | } |
| 1450 | return put_user(mex.outputdatalength, &umex->outputdatalength); |
| 1451 | } |
| 1452 | |
| 1453 | static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg) |
| 1454 | { |
| 1455 | int rc; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1456 | struct zcrypt_track tr; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1457 | struct ica_rsa_modexpo_crt crt; |
| 1458 | struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg; |
| 1459 | |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1460 | memset(&tr, 0, sizeof(tr)); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1461 | if (copy_from_user(&crt, ucrt, sizeof(crt))) |
| 1462 | return -EFAULT; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1463 | |
| 1464 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1465 | if (crt.inputdatalength & (1U << 31)) { |
| 1466 | if (!capable(CAP_SYS_ADMIN)) |
| 1467 | return -EPERM; |
| 1468 | tr.fi.cmd = (u16)(crt.inputdatalength >> 16); |
| 1469 | } |
| 1470 | crt.inputdatalength &= 0x0000FFFF; |
| 1471 | #endif |
| 1472 | |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1473 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1474 | rc = zcrypt_rsa_crt(perms, &tr, &crt); |
| 1475 | if (rc == -EAGAIN) |
| 1476 | tr.again_counter++; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1477 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1478 | if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY)) |
| 1479 | break; |
| 1480 | #endif |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1481 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1482 | /* on failure: retry once again after a requested rescan */ |
| 1483 | if ((rc == -ENODEV) && (zcrypt_process_rescan())) |
| 1484 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1485 | rc = zcrypt_rsa_crt(perms, &tr, &crt); |
| 1486 | if (rc == -EAGAIN) |
| 1487 | tr.again_counter++; |
| 1488 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | d39fae4 | 2021-01-21 16:03:08 +0100 | [diff] [blame] | 1489 | if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX) |
| 1490 | rc = -EIO; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1491 | if (rc) { |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 1492 | ZCRYPT_DBF_DBG("ioctl ICARSACRT rc=%d\n", rc); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1493 | return rc; |
| 1494 | } |
| 1495 | return put_user(crt.outputdatalength, &ucrt->outputdatalength); |
| 1496 | } |
| 1497 | |
| 1498 | static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg) |
| 1499 | { |
| 1500 | int rc; |
| 1501 | struct ica_xcRB xcRB; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1502 | struct zcrypt_track tr; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1503 | struct ica_xcRB __user *uxcRB = (void __user *) arg; |
| 1504 | |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1505 | memset(&tr, 0, sizeof(tr)); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1506 | if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB))) |
| 1507 | return -EFAULT; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1508 | |
| 1509 | #ifdef CONFIG_ZCRYPT_DEBUG |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 1510 | if ((xcRB.status & 0x8000FFFF) == 0x80004649 /* 'FI' */) { |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1511 | if (!capable(CAP_SYS_ADMIN)) |
| 1512 | return -EPERM; |
| 1513 | tr.fi.cmd = (u16)(xcRB.status >> 16); |
| 1514 | } |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 1515 | xcRB.status = 0; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1516 | #endif |
| 1517 | |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1518 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1519 | rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB); |
| 1520 | if (rc == -EAGAIN) |
| 1521 | tr.again_counter++; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1522 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1523 | if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY)) |
| 1524 | break; |
| 1525 | #endif |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1526 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1527 | /* on failure: retry once again after a requested rescan */ |
| 1528 | if ((rc == -ENODEV) && (zcrypt_process_rescan())) |
| 1529 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1530 | rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB); |
| 1531 | if (rc == -EAGAIN) |
| 1532 | tr.again_counter++; |
| 1533 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | d39fae4 | 2021-01-21 16:03:08 +0100 | [diff] [blame] | 1534 | if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX) |
| 1535 | rc = -EIO; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1536 | if (rc) |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 1537 | ZCRYPT_DBF_DBG("ioctl ZSENDCPRB rc=%d status=0x%x\n", |
| 1538 | rc, xcRB.status); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1539 | if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB))) |
| 1540 | return -EFAULT; |
| 1541 | return rc; |
| 1542 | } |
| 1543 | |
| 1544 | static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg) |
| 1545 | { |
| 1546 | int rc; |
| 1547 | struct ep11_urb xcrb; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1548 | struct zcrypt_track tr; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1549 | struct ep11_urb __user *uxcrb = (void __user *)arg; |
| 1550 | |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1551 | memset(&tr, 0, sizeof(tr)); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1552 | if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb))) |
| 1553 | return -EFAULT; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1554 | |
| 1555 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1556 | if (xcrb.req_len & (1ULL << 63)) { |
| 1557 | if (!capable(CAP_SYS_ADMIN)) |
| 1558 | return -EPERM; |
| 1559 | tr.fi.cmd = (u16)(xcrb.req_len >> 48); |
| 1560 | } |
| 1561 | xcrb.req_len &= 0x0000FFFFFFFFFFFFULL; |
| 1562 | #endif |
| 1563 | |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1564 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1565 | rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb); |
| 1566 | if (rc == -EAGAIN) |
| 1567 | tr.again_counter++; |
Harald Freudenberger | 27c4f67 | 2020-09-29 16:07:22 +0200 | [diff] [blame] | 1568 | #ifdef CONFIG_ZCRYPT_DEBUG |
| 1569 | if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY)) |
| 1570 | break; |
| 1571 | #endif |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1572 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1573 | /* on failure: retry once again after a requested rescan */ |
| 1574 | if ((rc == -ENODEV) && (zcrypt_process_rescan())) |
| 1575 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1576 | rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb); |
| 1577 | if (rc == -EAGAIN) |
| 1578 | tr.again_counter++; |
| 1579 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | d39fae4 | 2021-01-21 16:03:08 +0100 | [diff] [blame] | 1580 | if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX) |
| 1581 | rc = -EIO; |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1582 | if (rc) |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 1583 | ZCRYPT_DBF_DBG("ioctl ZSENDEP11CPRB rc=%d\n", rc); |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1584 | if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb))) |
| 1585 | return -EFAULT; |
| 1586 | return rc; |
| 1587 | } |
| 1588 | |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1589 | static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd, |
| 1590 | unsigned long arg) |
| 1591 | { |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1592 | int rc; |
| 1593 | struct ap_perms *perms = |
| 1594 | (struct ap_perms *) filp->private_data; |
| 1595 | |
| 1596 | rc = zcrypt_check_ioctl(perms, cmd); |
| 1597 | if (rc) |
| 1598 | return rc; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1599 | |
| 1600 | switch (cmd) { |
Harald Freudenberger | 7e202ac | 2020-05-20 16:07:19 +0200 | [diff] [blame] | 1601 | case ICARSAMODEXPO: |
| 1602 | return icarsamodexpo_ioctl(perms, arg); |
| 1603 | case ICARSACRT: |
| 1604 | return icarsacrt_ioctl(perms, arg); |
| 1605 | case ZSECSENDCPRB: |
| 1606 | return zsecsendcprb_ioctl(perms, arg); |
| 1607 | case ZSENDEP11CPRB: |
| 1608 | return zsendep11cprb_ioctl(perms, arg); |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1609 | case ZCRYPT_DEVICE_STATUS: { |
| 1610 | struct zcrypt_device_status_ext *device_status; |
| 1611 | size_t total_size = MAX_ZDEV_ENTRIES_EXT |
| 1612 | * sizeof(struct zcrypt_device_status_ext); |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1613 | |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1614 | device_status = kzalloc(total_size, GFP_KERNEL); |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1615 | if (!device_status) |
| 1616 | return -ENOMEM; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1617 | zcrypt_device_status_mask_ext(device_status); |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1618 | if (copy_to_user((char __user *) arg, device_status, |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1619 | total_size)) |
| 1620 | rc = -EFAULT; |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1621 | kfree(device_status); |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1622 | return rc; |
Ingo Tuchscherer | b886a9d | 2016-08-25 11:19:58 +0200 | [diff] [blame] | 1623 | } |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1624 | case ZCRYPT_STATUS_MASK: { |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1625 | char status[AP_DEVICES]; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1626 | |
| 1627 | zcrypt_status_mask(status, AP_DEVICES); |
| 1628 | if (copy_to_user((char __user *) arg, status, sizeof(status))) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1629 | return -EFAULT; |
| 1630 | return 0; |
| 1631 | } |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1632 | case ZCRYPT_QDEPTH_MASK: { |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1633 | char qdepth[AP_DEVICES]; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1634 | |
| 1635 | zcrypt_qdepth_mask(qdepth, AP_DEVICES); |
| 1636 | if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth))) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1637 | return -EFAULT; |
| 1638 | return 0; |
| 1639 | } |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1640 | case ZCRYPT_PERDEV_REQCNT: { |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 1641 | u32 *reqcnt; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1642 | |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 1643 | reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL); |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1644 | if (!reqcnt) |
| 1645 | return -ENOMEM; |
| 1646 | zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES); |
Christian Borntraeger | f7e8098 | 2020-09-21 12:48:36 +0200 | [diff] [blame] | 1647 | if (copy_to_user((int __user *) arg, reqcnt, |
| 1648 | sizeof(u32) * AP_DEVICES)) |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1649 | rc = -EFAULT; |
| 1650 | kfree(reqcnt); |
| 1651 | return rc; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1652 | } |
| 1653 | case Z90STAT_REQUESTQ_COUNT: |
| 1654 | return put_user(zcrypt_requestq_count(), (int __user *) arg); |
| 1655 | case Z90STAT_PENDINGQ_COUNT: |
| 1656 | return put_user(zcrypt_pendingq_count(), (int __user *) arg); |
| 1657 | case Z90STAT_TOTALOPEN_COUNT: |
| 1658 | return put_user(atomic_read(&zcrypt_open_count), |
| 1659 | (int __user *) arg); |
| 1660 | case Z90STAT_DOMAIN_INDEX: |
| 1661 | return put_user(ap_domain_index, (int __user *) arg); |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1662 | /* |
| 1663 | * Deprecated ioctls |
| 1664 | */ |
| 1665 | case ZDEVICESTATUS: { |
| 1666 | /* the old ioctl supports only 64 adapters */ |
| 1667 | struct zcrypt_device_status *device_status; |
| 1668 | size_t total_size = MAX_ZDEV_ENTRIES |
| 1669 | * sizeof(struct zcrypt_device_status); |
| 1670 | |
| 1671 | device_status = kzalloc(total_size, GFP_KERNEL); |
| 1672 | if (!device_status) |
| 1673 | return -ENOMEM; |
| 1674 | zcrypt_device_status_mask(device_status); |
| 1675 | if (copy_to_user((char __user *) arg, device_status, |
| 1676 | total_size)) |
| 1677 | rc = -EFAULT; |
| 1678 | kfree(device_status); |
| 1679 | return rc; |
| 1680 | } |
| 1681 | case Z90STAT_STATUS_MASK: { |
| 1682 | /* the old ioctl supports only 64 adapters */ |
| 1683 | char status[MAX_ZDEV_CARDIDS]; |
| 1684 | |
| 1685 | zcrypt_status_mask(status, MAX_ZDEV_CARDIDS); |
| 1686 | if (copy_to_user((char __user *) arg, status, sizeof(status))) |
| 1687 | return -EFAULT; |
| 1688 | return 0; |
| 1689 | } |
| 1690 | case Z90STAT_QDEPTH_MASK: { |
| 1691 | /* the old ioctl supports only 64 adapters */ |
| 1692 | char qdepth[MAX_ZDEV_CARDIDS]; |
| 1693 | |
| 1694 | zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS); |
| 1695 | if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth))) |
| 1696 | return -EFAULT; |
| 1697 | return 0; |
| 1698 | } |
| 1699 | case Z90STAT_PERDEV_REQCNT: { |
| 1700 | /* the old ioctl supports only 64 adapters */ |
Harald Freudenberger | fcd98d4 | 2019-12-20 16:02:54 +0100 | [diff] [blame] | 1701 | u32 reqcnt[MAX_ZDEV_CARDIDS]; |
Harald Freudenberger | af4a722 | 2018-04-09 16:18:37 +0200 | [diff] [blame] | 1702 | |
| 1703 | zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS); |
| 1704 | if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt))) |
| 1705 | return -EFAULT; |
| 1706 | return 0; |
| 1707 | } |
Harald Freudenberger | 2a80786 | 2018-04-04 13:25:40 +0200 | [diff] [blame] | 1708 | /* unknown ioctl number */ |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1709 | default: |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 1710 | ZCRYPT_DBF_DBG("unknown ioctl 0x%08x\n", cmd); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1711 | return -ENOIOCTLCMD; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | #ifdef CONFIG_COMPAT |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1716 | /* |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1717 | * ioctl32 conversion routines |
| 1718 | */ |
| 1719 | struct compat_ica_rsa_modexpo { |
| 1720 | compat_uptr_t inputdata; |
| 1721 | unsigned int inputdatalength; |
| 1722 | compat_uptr_t outputdata; |
| 1723 | unsigned int outputdatalength; |
| 1724 | compat_uptr_t b_key; |
| 1725 | compat_uptr_t n_modulus; |
| 1726 | }; |
| 1727 | |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1728 | static long trans_modexpo32(struct ap_perms *perms, struct file *filp, |
| 1729 | unsigned int cmd, unsigned long arg) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1730 | { |
| 1731 | struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg); |
| 1732 | struct compat_ica_rsa_modexpo mex32; |
| 1733 | struct ica_rsa_modexpo mex64; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1734 | struct zcrypt_track tr; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1735 | long rc; |
| 1736 | |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1737 | memset(&tr, 0, sizeof(tr)); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1738 | if (copy_from_user(&mex32, umex32, sizeof(mex32))) |
| 1739 | return -EFAULT; |
| 1740 | mex64.inputdata = compat_ptr(mex32.inputdata); |
| 1741 | mex64.inputdatalength = mex32.inputdatalength; |
| 1742 | mex64.outputdata = compat_ptr(mex32.outputdata); |
| 1743 | mex64.outputdatalength = mex32.outputdatalength; |
| 1744 | mex64.b_key = compat_ptr(mex32.b_key); |
| 1745 | mex64.n_modulus = compat_ptr(mex32.n_modulus); |
| 1746 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1747 | rc = zcrypt_rsa_modexpo(perms, &tr, &mex64); |
| 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_rsa_modexpo(perms, &tr, &mex64); |
| 1755 | if (rc == -EAGAIN) |
| 1756 | tr.again_counter++; |
| 1757 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | d39fae4 | 2021-01-21 16:03:08 +0100 | [diff] [blame] | 1758 | if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX) |
| 1759 | rc = -EIO; |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 1760 | if (rc) |
| 1761 | return rc; |
| 1762 | return put_user(mex64.outputdatalength, |
| 1763 | &umex32->outputdatalength); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | struct compat_ica_rsa_modexpo_crt { |
| 1767 | compat_uptr_t inputdata; |
| 1768 | unsigned int inputdatalength; |
| 1769 | compat_uptr_t outputdata; |
| 1770 | unsigned int outputdatalength; |
| 1771 | compat_uptr_t bp_key; |
| 1772 | compat_uptr_t bq_key; |
| 1773 | compat_uptr_t np_prime; |
| 1774 | compat_uptr_t nq_prime; |
| 1775 | compat_uptr_t u_mult_inv; |
| 1776 | }; |
| 1777 | |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1778 | static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp, |
| 1779 | unsigned int cmd, unsigned long arg) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1780 | { |
| 1781 | struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg); |
| 1782 | struct compat_ica_rsa_modexpo_crt crt32; |
| 1783 | struct ica_rsa_modexpo_crt crt64; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1784 | struct zcrypt_track tr; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1785 | long rc; |
| 1786 | |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1787 | memset(&tr, 0, sizeof(tr)); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1788 | if (copy_from_user(&crt32, ucrt32, sizeof(crt32))) |
| 1789 | return -EFAULT; |
| 1790 | crt64.inputdata = compat_ptr(crt32.inputdata); |
| 1791 | crt64.inputdatalength = crt32.inputdatalength; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1792 | crt64.outputdata = compat_ptr(crt32.outputdata); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1793 | crt64.outputdatalength = crt32.outputdatalength; |
| 1794 | crt64.bp_key = compat_ptr(crt32.bp_key); |
| 1795 | crt64.bq_key = compat_ptr(crt32.bq_key); |
| 1796 | crt64.np_prime = compat_ptr(crt32.np_prime); |
| 1797 | crt64.nq_prime = compat_ptr(crt32.nq_prime); |
| 1798 | crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv); |
| 1799 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1800 | rc = zcrypt_rsa_crt(perms, &tr, &crt64); |
| 1801 | if (rc == -EAGAIN) |
| 1802 | tr.again_counter++; |
| 1803 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 1804 | /* on failure: retry once again after a requested rescan */ |
| 1805 | if ((rc == -ENODEV) && (zcrypt_process_rescan())) |
| 1806 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1807 | rc = zcrypt_rsa_crt(perms, &tr, &crt64); |
| 1808 | if (rc == -EAGAIN) |
| 1809 | tr.again_counter++; |
| 1810 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | d39fae4 | 2021-01-21 16:03:08 +0100 | [diff] [blame] | 1811 | if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX) |
| 1812 | rc = -EIO; |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 1813 | if (rc) |
| 1814 | return rc; |
| 1815 | return put_user(crt64.outputdatalength, |
| 1816 | &ucrt32->outputdatalength); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1817 | } |
| 1818 | |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1819 | struct compat_ica_xcRB { |
| 1820 | unsigned short agent_ID; |
| 1821 | unsigned int user_defined; |
| 1822 | unsigned short request_ID; |
| 1823 | unsigned int request_control_blk_length; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1824 | unsigned char padding1[16 - sizeof(compat_uptr_t)]; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1825 | compat_uptr_t request_control_blk_addr; |
| 1826 | unsigned int request_data_length; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1827 | char padding2[16 - sizeof(compat_uptr_t)]; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1828 | compat_uptr_t request_data_address; |
| 1829 | unsigned int reply_control_blk_length; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1830 | char padding3[16 - sizeof(compat_uptr_t)]; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1831 | compat_uptr_t reply_control_blk_addr; |
| 1832 | unsigned int reply_data_length; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1833 | char padding4[16 - sizeof(compat_uptr_t)]; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1834 | compat_uptr_t reply_data_addr; |
| 1835 | unsigned short priority_window; |
| 1836 | unsigned int status; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1837 | } __packed; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1838 | |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1839 | static long trans_xcRB32(struct ap_perms *perms, struct file *filp, |
| 1840 | unsigned int cmd, unsigned long arg) |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1841 | { |
| 1842 | struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg); |
| 1843 | struct compat_ica_xcRB xcRB32; |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1844 | struct zcrypt_track tr; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1845 | struct ica_xcRB xcRB64; |
| 1846 | long rc; |
| 1847 | |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1848 | memset(&tr, 0, sizeof(tr)); |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1849 | if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32))) |
| 1850 | return -EFAULT; |
| 1851 | xcRB64.agent_ID = xcRB32.agent_ID; |
| 1852 | xcRB64.user_defined = xcRB32.user_defined; |
| 1853 | xcRB64.request_ID = xcRB32.request_ID; |
| 1854 | xcRB64.request_control_blk_length = |
| 1855 | xcRB32.request_control_blk_length; |
| 1856 | xcRB64.request_control_blk_addr = |
| 1857 | compat_ptr(xcRB32.request_control_blk_addr); |
| 1858 | xcRB64.request_data_length = |
| 1859 | xcRB32.request_data_length; |
| 1860 | xcRB64.request_data_address = |
| 1861 | compat_ptr(xcRB32.request_data_address); |
| 1862 | xcRB64.reply_control_blk_length = |
| 1863 | xcRB32.reply_control_blk_length; |
| 1864 | xcRB64.reply_control_blk_addr = |
| 1865 | compat_ptr(xcRB32.reply_control_blk_addr); |
| 1866 | xcRB64.reply_data_length = xcRB32.reply_data_length; |
| 1867 | xcRB64.reply_data_addr = |
| 1868 | compat_ptr(xcRB32.reply_data_addr); |
| 1869 | xcRB64.priority_window = xcRB32.priority_window; |
| 1870 | xcRB64.status = xcRB32.status; |
| 1871 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1872 | rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64); |
| 1873 | if (rc == -EAGAIN) |
| 1874 | tr.again_counter++; |
| 1875 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 1876 | /* on failure: retry once again after a requested rescan */ |
| 1877 | if ((rc == -ENODEV) && (zcrypt_process_rescan())) |
| 1878 | do { |
Harald Freudenberger | 91ffc51 | 2020-07-02 11:10:11 +0200 | [diff] [blame] | 1879 | rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64); |
| 1880 | if (rc == -EAGAIN) |
| 1881 | tr.again_counter++; |
| 1882 | } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX); |
Harald Freudenberger | d39fae4 | 2021-01-21 16:03:08 +0100 | [diff] [blame] | 1883 | if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX) |
| 1884 | rc = -EIO; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1885 | xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length; |
| 1886 | xcRB32.reply_data_length = xcRB64.reply_data_length; |
| 1887 | xcRB32.status = xcRB64.status; |
| 1888 | if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32))) |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1889 | return -EFAULT; |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1890 | return rc; |
| 1891 | } |
| 1892 | |
Heiko Carstens | 2b67fc4 | 2007-02-05 21:16:47 +0100 | [diff] [blame] | 1893 | static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd, |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1894 | unsigned long arg) |
| 1895 | { |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1896 | int rc; |
| 1897 | struct ap_perms *perms = |
| 1898 | (struct ap_perms *) filp->private_data; |
| 1899 | |
| 1900 | rc = zcrypt_check_ioctl(perms, cmd); |
| 1901 | if (rc) |
| 1902 | return rc; |
| 1903 | |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1904 | if (cmd == ICARSAMODEXPO) |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1905 | return trans_modexpo32(perms, filp, cmd, arg); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1906 | if (cmd == ICARSACRT) |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1907 | return trans_modexpo_crt32(perms, filp, cmd, arg); |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 1908 | if (cmd == ZSECSENDCPRB) |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 1909 | return trans_xcRB32(perms, filp, cmd, arg); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1910 | return zcrypt_unlocked_ioctl(filp, cmd, arg); |
| 1911 | } |
| 1912 | #endif |
| 1913 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1914 | /* |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1915 | * Misc device file operations. |
| 1916 | */ |
Arjan van de Ven | d54b1fd | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1917 | static const struct file_operations zcrypt_fops = { |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1918 | .owner = THIS_MODULE, |
| 1919 | .read = zcrypt_read, |
| 1920 | .write = zcrypt_write, |
| 1921 | .unlocked_ioctl = zcrypt_unlocked_ioctl, |
| 1922 | #ifdef CONFIG_COMPAT |
| 1923 | .compat_ioctl = zcrypt_compat_ioctl, |
| 1924 | #endif |
| 1925 | .open = zcrypt_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1926 | .release = zcrypt_release, |
| 1927 | .llseek = no_llseek, |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1928 | }; |
| 1929 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1930 | /* |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 1931 | * Misc device. |
| 1932 | */ |
| 1933 | static struct miscdevice zcrypt_misc_device = { |
| 1934 | .minor = MISC_DYNAMIC_MINOR, |
| 1935 | .name = "z90crypt", |
| 1936 | .fops = &zcrypt_fops, |
| 1937 | }; |
| 1938 | |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1939 | static int zcrypt_rng_device_count; |
| 1940 | static u32 *zcrypt_rng_buffer; |
| 1941 | static int zcrypt_rng_buffer_index; |
| 1942 | static DEFINE_MUTEX(zcrypt_rng_mutex); |
| 1943 | |
| 1944 | static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data) |
| 1945 | { |
| 1946 | int rc; |
| 1947 | |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 1948 | /* |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1949 | * We don't need locking here because the RNG API guarantees serialized |
| 1950 | * read method calls. |
| 1951 | */ |
| 1952 | if (zcrypt_rng_buffer_index == 0) { |
| 1953 | rc = zcrypt_rng((char *) zcrypt_rng_buffer); |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 1954 | /* on failure: retry once again after a requested rescan */ |
| 1955 | if ((rc == -ENODEV) && (zcrypt_process_rescan())) |
| 1956 | rc = zcrypt_rng((char *) zcrypt_rng_buffer); |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1957 | if (rc < 0) |
| 1958 | return -EIO; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1959 | zcrypt_rng_buffer_index = rc / sizeof(*data); |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1960 | } |
| 1961 | *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index]; |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 1962 | return sizeof(*data); |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
| 1965 | static struct hwrng zcrypt_rng_dev = { |
| 1966 | .name = "zcrypt", |
| 1967 | .data_read = zcrypt_rng_data_read, |
Ingo Tuchscherer | db490cb | 2015-03-17 16:02:20 +0100 | [diff] [blame] | 1968 | .quality = 990, |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1969 | }; |
| 1970 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 1971 | int zcrypt_rng_device_add(void) |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1972 | { |
| 1973 | int rc = 0; |
| 1974 | |
| 1975 | mutex_lock(&zcrypt_rng_mutex); |
| 1976 | if (zcrypt_rng_device_count == 0) { |
| 1977 | zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL); |
| 1978 | if (!zcrypt_rng_buffer) { |
| 1979 | rc = -ENOMEM; |
| 1980 | goto out; |
| 1981 | } |
| 1982 | zcrypt_rng_buffer_index = 0; |
Ingo Tuchscherer | db490cb | 2015-03-17 16:02:20 +0100 | [diff] [blame] | 1983 | if (!zcrypt_hwrng_seed) |
| 1984 | zcrypt_rng_dev.quality = 0; |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 1985 | rc = hwrng_register(&zcrypt_rng_dev); |
| 1986 | if (rc) |
| 1987 | goto out_free; |
| 1988 | zcrypt_rng_device_count = 1; |
| 1989 | } else |
| 1990 | zcrypt_rng_device_count++; |
| 1991 | mutex_unlock(&zcrypt_rng_mutex); |
| 1992 | return 0; |
| 1993 | |
| 1994 | out_free: |
| 1995 | free_page((unsigned long) zcrypt_rng_buffer); |
| 1996 | out: |
| 1997 | mutex_unlock(&zcrypt_rng_mutex); |
| 1998 | return rc; |
| 1999 | } |
| 2000 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 2001 | void zcrypt_rng_device_remove(void) |
Ralph Wuerthner | 2f7c8bd | 2008-04-17 07:46:15 +0200 | [diff] [blame] | 2002 | { |
| 2003 | mutex_lock(&zcrypt_rng_mutex); |
| 2004 | zcrypt_rng_device_count--; |
| 2005 | if (zcrypt_rng_device_count == 0) { |
| 2006 | hwrng_unregister(&zcrypt_rng_dev); |
| 2007 | free_page((unsigned long) zcrypt_rng_buffer); |
| 2008 | } |
| 2009 | mutex_unlock(&zcrypt_rng_mutex); |
| 2010 | } |
| 2011 | |
Harald Freudenberger | 43cb5a7 | 2020-09-01 12:48:14 +0200 | [diff] [blame] | 2012 | /* |
| 2013 | * Wait until the zcrypt api is operational. |
| 2014 | * The AP bus scan and the binding of ap devices to device drivers is |
| 2015 | * an asynchronous job. This function waits until these initial jobs |
| 2016 | * are done and so the zcrypt api should be ready to serve crypto |
| 2017 | * requests - if there are resources available. The function uses an |
| 2018 | * internal timeout of 60s. The very first caller will either wait for |
| 2019 | * ap bus bindings complete or the timeout happens. This state will be |
| 2020 | * remembered for further callers which will only be blocked until a |
| 2021 | * decision is made (timeout or bindings complete). |
| 2022 | * On timeout -ETIME is returned, on success the return value is 0. |
| 2023 | */ |
| 2024 | int zcrypt_wait_api_operational(void) |
| 2025 | { |
| 2026 | static DEFINE_MUTEX(zcrypt_wait_api_lock); |
| 2027 | static int zcrypt_wait_api_state; |
| 2028 | int rc; |
| 2029 | |
| 2030 | rc = mutex_lock_interruptible(&zcrypt_wait_api_lock); |
| 2031 | if (rc) |
| 2032 | return rc; |
| 2033 | |
| 2034 | switch (zcrypt_wait_api_state) { |
| 2035 | case 0: |
| 2036 | /* initial state, invoke wait for the ap bus complete */ |
| 2037 | rc = ap_wait_init_apqn_bindings_complete( |
| 2038 | msecs_to_jiffies(60 * 1000)); |
| 2039 | switch (rc) { |
| 2040 | case 0: |
| 2041 | /* ap bus bindings are complete */ |
| 2042 | zcrypt_wait_api_state = 1; |
| 2043 | break; |
| 2044 | case -EINTR: |
| 2045 | /* interrupted, go back to caller */ |
| 2046 | break; |
| 2047 | case -ETIME: |
| 2048 | /* timeout */ |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 2049 | ZCRYPT_DBF_WARN("%s ap_wait_init_apqn_bindings_complete()=ETIME\n", |
| 2050 | __func__); |
Harald Freudenberger | 43cb5a7 | 2020-09-01 12:48:14 +0200 | [diff] [blame] | 2051 | zcrypt_wait_api_state = -ETIME; |
| 2052 | break; |
| 2053 | default: |
| 2054 | /* other failure */ |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 2055 | ZCRYPT_DBF_DBG("%s ap_wait_init_apqn_bindings_complete()=%d\n", |
| 2056 | __func__, rc); |
Harald Freudenberger | 43cb5a7 | 2020-09-01 12:48:14 +0200 | [diff] [blame] | 2057 | break; |
| 2058 | } |
| 2059 | break; |
| 2060 | case 1: |
| 2061 | /* a previous caller already found ap bus bindings complete */ |
| 2062 | rc = 0; |
| 2063 | break; |
| 2064 | default: |
| 2065 | /* a previous caller had timeout or other failure */ |
| 2066 | rc = zcrypt_wait_api_state; |
| 2067 | break; |
| 2068 | } |
| 2069 | |
| 2070 | mutex_unlock(&zcrypt_wait_api_lock); |
| 2071 | |
| 2072 | return rc; |
| 2073 | } |
| 2074 | EXPORT_SYMBOL(zcrypt_wait_api_operational); |
| 2075 | |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 2076 | int __init zcrypt_debug_init(void) |
| 2077 | { |
Harald Freudenberger | 3f74eb5 | 2021-10-15 12:00:22 +0200 | [diff] [blame] | 2078 | zcrypt_dbf_info = debug_register("zcrypt", 2, 1, |
Harald Freudenberger | cccd85b | 2016-11-24 06:45:21 +0100 | [diff] [blame] | 2079 | DBF_MAX_SPRINTF_ARGS * sizeof(long)); |
| 2080 | debug_register_view(zcrypt_dbf_info, &debug_sprintf_view); |
| 2081 | debug_set_level(zcrypt_dbf_info, DBF_ERR); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 2082 | |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | void zcrypt_debug_exit(void) |
| 2087 | { |
Harald Freudenberger | cccd85b | 2016-11-24 06:45:21 +0100 | [diff] [blame] | 2088 | debug_unregister(zcrypt_dbf_info); |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 2089 | } |
| 2090 | |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 2091 | #ifdef CONFIG_ZCRYPT_MULTIDEVNODES |
| 2092 | |
| 2093 | static int __init zcdn_init(void) |
| 2094 | { |
| 2095 | int rc; |
| 2096 | |
| 2097 | /* create a new class 'zcrypt' */ |
| 2098 | zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME); |
| 2099 | if (IS_ERR(zcrypt_class)) { |
| 2100 | rc = PTR_ERR(zcrypt_class); |
| 2101 | goto out_class_create_failed; |
| 2102 | } |
| 2103 | zcrypt_class->dev_release = zcdn_device_release; |
| 2104 | |
| 2105 | /* alloc device minor range */ |
| 2106 | rc = alloc_chrdev_region(&zcrypt_devt, |
| 2107 | 0, ZCRYPT_MAX_MINOR_NODES, |
| 2108 | ZCRYPT_NAME); |
| 2109 | if (rc) |
| 2110 | goto out_alloc_chrdev_failed; |
| 2111 | |
| 2112 | cdev_init(&zcrypt_cdev, &zcrypt_fops); |
| 2113 | zcrypt_cdev.owner = THIS_MODULE; |
| 2114 | rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES); |
| 2115 | if (rc) |
| 2116 | goto out_cdev_add_failed; |
| 2117 | |
| 2118 | /* need some class specific sysfs attributes */ |
| 2119 | rc = class_create_file(zcrypt_class, &class_attr_zcdn_create); |
| 2120 | if (rc) |
| 2121 | goto out_class_create_file_1_failed; |
| 2122 | rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy); |
| 2123 | if (rc) |
| 2124 | goto out_class_create_file_2_failed; |
| 2125 | |
| 2126 | return 0; |
| 2127 | |
| 2128 | out_class_create_file_2_failed: |
| 2129 | class_remove_file(zcrypt_class, &class_attr_zcdn_create); |
| 2130 | out_class_create_file_1_failed: |
| 2131 | cdev_del(&zcrypt_cdev); |
| 2132 | out_cdev_add_failed: |
| 2133 | unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES); |
| 2134 | out_alloc_chrdev_failed: |
| 2135 | class_destroy(zcrypt_class); |
| 2136 | out_class_create_failed: |
| 2137 | return rc; |
| 2138 | } |
| 2139 | |
| 2140 | static void zcdn_exit(void) |
| 2141 | { |
| 2142 | class_remove_file(zcrypt_class, &class_attr_zcdn_create); |
| 2143 | class_remove_file(zcrypt_class, &class_attr_zcdn_destroy); |
| 2144 | zcdn_destroy_all(); |
| 2145 | cdev_del(&zcrypt_cdev); |
| 2146 | unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES); |
| 2147 | class_destroy(zcrypt_class); |
| 2148 | } |
| 2149 | |
| 2150 | #endif |
| 2151 | |
Heiko Carstens | 2c57ad6 | 2021-09-07 07:28:20 +0200 | [diff] [blame] | 2152 | /* |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 2153 | * zcrypt_api_init(): Module initialization. |
| 2154 | * |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2155 | * The module initialization code. |
| 2156 | */ |
| 2157 | int __init zcrypt_api_init(void) |
| 2158 | { |
| 2159 | int rc; |
| 2160 | |
Holger Dengler | dabecb2 | 2012-09-10 21:34:26 +0200 | [diff] [blame] | 2161 | rc = zcrypt_debug_init(); |
| 2162 | if (rc) |
| 2163 | goto out; |
| 2164 | |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 2165 | #ifdef CONFIG_ZCRYPT_MULTIDEVNODES |
| 2166 | rc = zcdn_init(); |
| 2167 | if (rc) |
| 2168 | goto out; |
| 2169 | #endif |
| 2170 | |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2171 | /* Register the request sprayer. */ |
| 2172 | rc = misc_register(&zcrypt_misc_device); |
Felix Beck | 1a89dd8 | 2008-07-14 09:59:27 +0200 | [diff] [blame] | 2173 | if (rc < 0) |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 2174 | goto out_misc_register_failed; |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2175 | |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 2176 | zcrypt_msgtype6_init(); |
| 2177 | zcrypt_msgtype50_init(); |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 2178 | |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2179 | return 0; |
| 2180 | |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 2181 | out_misc_register_failed: |
| 2182 | #ifdef CONFIG_ZCRYPT_MULTIDEVNODES |
| 2183 | zcdn_exit(); |
| 2184 | #endif |
| 2185 | zcrypt_debug_exit(); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2186 | out: |
| 2187 | return rc; |
| 2188 | } |
| 2189 | |
Heiko Carstens | 2c57ad6 | 2021-09-07 07:28:20 +0200 | [diff] [blame] | 2190 | /* |
Felix Beck | 1749a81 | 2008-04-17 07:46:28 +0200 | [diff] [blame] | 2191 | * zcrypt_api_exit(): Module termination. |
| 2192 | * |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2193 | * The module termination code. |
| 2194 | */ |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 2195 | void __exit zcrypt_api_exit(void) |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2196 | { |
Harald Freudenberger | 00fab23 | 2018-09-17 16:18:41 +0200 | [diff] [blame] | 2197 | #ifdef CONFIG_ZCRYPT_MULTIDEVNODES |
| 2198 | zcdn_exit(); |
| 2199 | #endif |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2200 | misc_deregister(&zcrypt_misc_device); |
Ingo Tuchscherer | fc1d3f0 | 2016-08-25 11:11:30 +0200 | [diff] [blame] | 2201 | zcrypt_msgtype6_exit(); |
| 2202 | zcrypt_msgtype50_exit(); |
Harald Freudenberger | efc598e | 2019-06-11 11:16:56 +0200 | [diff] [blame] | 2203 | zcrypt_ccamisc_exit(); |
Harald Freudenberger | 7384eb7 | 2019-08-30 16:07:08 +0200 | [diff] [blame] | 2204 | zcrypt_ep11misc_exit(); |
Harald Freudenberger | cccd85b | 2016-11-24 06:45:21 +0100 | [diff] [blame] | 2205 | zcrypt_debug_exit(); |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2206 | } |
| 2207 | |
Martin Schwidefsky | 2dbc241 | 2006-09-20 15:58:27 +0200 | [diff] [blame] | 2208 | module_init(zcrypt_api_init); |
| 2209 | module_exit(zcrypt_api_exit); |