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