Greg Kroah-Hartman | 812141a | 2017-11-14 18:38:01 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 2 | /* |
| 3 | * zcrypt 2.1.0 |
| 4 | * |
| 5 | * Copyright IBM Corp. 2001, 2012 |
| 6 | * Author(s): Robert Burroughs |
| 7 | * Eric Rossman (edrossma@us.ibm.com) |
| 8 | * Cornelia Huck <cornelia.huck@de.ibm.com> |
| 9 | * |
| 10 | * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com) |
| 11 | * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com> |
| 12 | * Ralph Wuerthner <rwuerthn@de.ibm.com> |
| 13 | * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com> |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/proc_fs.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/compat.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/atomic.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/hw_random.h> |
| 28 | #include <linux/debugfs.h> |
| 29 | #include <asm/debug.h> |
| 30 | |
| 31 | #include "zcrypt_debug.h" |
| 32 | #include "zcrypt_api.h" |
| 33 | |
| 34 | #include "zcrypt_msgtype6.h" |
| 35 | #include "zcrypt_msgtype50.h" |
| 36 | |
| 37 | /* |
| 38 | * Device attributes common for all crypto queue devices. |
| 39 | */ |
| 40 | |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 41 | static ssize_t online_show(struct device *dev, |
| 42 | struct device_attribute *attr, |
| 43 | char *buf) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 44 | { |
| 45 | struct zcrypt_queue *zq = to_ap_queue(dev)->private; |
| 46 | |
| 47 | return snprintf(buf, PAGE_SIZE, "%d\n", zq->online); |
| 48 | } |
| 49 | |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 50 | static ssize_t online_store(struct device *dev, |
| 51 | struct device_attribute *attr, |
| 52 | const char *buf, size_t count) |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 53 | { |
| 54 | struct zcrypt_queue *zq = to_ap_queue(dev)->private; |
| 55 | struct zcrypt_card *zc = zq->zcard; |
| 56 | int online; |
| 57 | |
| 58 | if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | if (online && !zc->online) |
| 62 | return -EINVAL; |
| 63 | zq->online = online; |
Harald Freudenberger | cccd85b | 2016-11-24 06:45:21 +0100 | [diff] [blame] | 64 | |
| 65 | ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x online=%d\n", |
| 66 | AP_QID_CARD(zq->queue->qid), |
| 67 | AP_QID_QUEUE(zq->queue->qid), |
| 68 | online); |
| 69 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 70 | if (!online) |
| 71 | ap_flush_queue(zq->queue); |
| 72 | return count; |
| 73 | } |
| 74 | |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 75 | static DEVICE_ATTR_RW(online); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 76 | |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 77 | static ssize_t load_show(struct device *dev, |
| 78 | struct device_attribute *attr, |
| 79 | char *buf) |
Harald Freudenberger | 4a07750 | 2018-06-07 15:09:48 +0200 | [diff] [blame] | 80 | { |
| 81 | struct zcrypt_queue *zq = to_ap_queue(dev)->private; |
| 82 | |
| 83 | return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zq->load)); |
| 84 | } |
| 85 | |
Harald Freudenberger | ac2b96f | 2018-08-17 12:36:01 +0200 | [diff] [blame] | 86 | static DEVICE_ATTR_RO(load); |
Harald Freudenberger | 4a07750 | 2018-06-07 15:09:48 +0200 | [diff] [blame] | 87 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 88 | static struct attribute *zcrypt_queue_attrs[] = { |
| 89 | &dev_attr_online.attr, |
Harald Freudenberger | 4a07750 | 2018-06-07 15:09:48 +0200 | [diff] [blame] | 90 | &dev_attr_load.attr, |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 91 | NULL, |
| 92 | }; |
| 93 | |
Arvind Yadav | 9920dec | 2017-07-19 12:39:10 +0530 | [diff] [blame] | 94 | static const struct attribute_group zcrypt_queue_attr_group = { |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 95 | .attrs = zcrypt_queue_attrs, |
| 96 | }; |
| 97 | |
| 98 | void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online) |
| 99 | { |
| 100 | zq->online = online; |
| 101 | if (!online) |
| 102 | ap_flush_queue(zq->queue); |
| 103 | } |
| 104 | |
| 105 | struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size) |
| 106 | { |
| 107 | struct zcrypt_queue *zq; |
| 108 | |
| 109 | zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL); |
| 110 | if (!zq) |
| 111 | return NULL; |
| 112 | zq->reply.message = kmalloc(max_response_size, GFP_KERNEL); |
| 113 | if (!zq->reply.message) |
| 114 | goto out_free; |
| 115 | zq->reply.length = max_response_size; |
| 116 | INIT_LIST_HEAD(&zq->list); |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 117 | kref_init(&zq->refcount); |
| 118 | return zq; |
| 119 | |
| 120 | out_free: |
| 121 | kfree(zq); |
| 122 | return NULL; |
| 123 | } |
| 124 | EXPORT_SYMBOL(zcrypt_queue_alloc); |
| 125 | |
| 126 | void zcrypt_queue_free(struct zcrypt_queue *zq) |
| 127 | { |
| 128 | kfree(zq->reply.message); |
| 129 | kfree(zq); |
| 130 | } |
| 131 | EXPORT_SYMBOL(zcrypt_queue_free); |
| 132 | |
| 133 | static void zcrypt_queue_release(struct kref *kref) |
| 134 | { |
| 135 | struct zcrypt_queue *zq = |
| 136 | container_of(kref, struct zcrypt_queue, refcount); |
| 137 | zcrypt_queue_free(zq); |
| 138 | } |
| 139 | |
| 140 | void zcrypt_queue_get(struct zcrypt_queue *zq) |
| 141 | { |
| 142 | kref_get(&zq->refcount); |
| 143 | } |
| 144 | EXPORT_SYMBOL(zcrypt_queue_get); |
| 145 | |
| 146 | int zcrypt_queue_put(struct zcrypt_queue *zq) |
| 147 | { |
| 148 | return kref_put(&zq->refcount, zcrypt_queue_release); |
| 149 | } |
| 150 | EXPORT_SYMBOL(zcrypt_queue_put); |
| 151 | |
| 152 | /** |
| 153 | * zcrypt_queue_register() - Register a crypto queue device. |
| 154 | * @zq: Pointer to a crypto queue device |
| 155 | * |
| 156 | * Register a crypto queue device. Returns 0 if successful. |
| 157 | */ |
| 158 | int zcrypt_queue_register(struct zcrypt_queue *zq) |
| 159 | { |
| 160 | struct zcrypt_card *zc; |
| 161 | int rc; |
| 162 | |
| 163 | spin_lock(&zcrypt_list_lock); |
| 164 | zc = zq->queue->card->private; |
| 165 | zcrypt_card_get(zc); |
| 166 | zq->zcard = zc; |
| 167 | zq->online = 1; /* New devices are online by default. */ |
Harald Freudenberger | cccd85b | 2016-11-24 06:45:21 +0100 | [diff] [blame] | 168 | |
| 169 | ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n", |
| 170 | AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid)); |
| 171 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 172 | list_add_tail(&zq->list, &zc->zqueues); |
| 173 | zcrypt_device_count++; |
| 174 | spin_unlock(&zcrypt_list_lock); |
| 175 | |
| 176 | rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj, |
| 177 | &zcrypt_queue_attr_group); |
| 178 | if (rc) |
| 179 | goto out; |
| 180 | get_device(&zq->queue->ap_dev.device); |
| 181 | |
| 182 | if (zq->ops->rng) { |
| 183 | rc = zcrypt_rng_device_add(); |
| 184 | if (rc) |
| 185 | goto out_unregister; |
| 186 | } |
| 187 | return 0; |
| 188 | |
| 189 | out_unregister: |
| 190 | sysfs_remove_group(&zq->queue->ap_dev.device.kobj, |
| 191 | &zcrypt_queue_attr_group); |
| 192 | put_device(&zq->queue->ap_dev.device); |
| 193 | out: |
| 194 | spin_lock(&zcrypt_list_lock); |
| 195 | list_del_init(&zq->list); |
| 196 | spin_unlock(&zcrypt_list_lock); |
| 197 | zcrypt_card_put(zc); |
| 198 | return rc; |
| 199 | } |
| 200 | EXPORT_SYMBOL(zcrypt_queue_register); |
| 201 | |
| 202 | /** |
| 203 | * zcrypt_queue_unregister(): Unregister a crypto queue device. |
| 204 | * @zq: Pointer to crypto queue device |
| 205 | * |
| 206 | * Unregister a crypto queue device. |
| 207 | */ |
| 208 | void zcrypt_queue_unregister(struct zcrypt_queue *zq) |
| 209 | { |
| 210 | struct zcrypt_card *zc; |
| 211 | |
Harald Freudenberger | cccd85b | 2016-11-24 06:45:21 +0100 | [diff] [blame] | 212 | ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n", |
| 213 | AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid)); |
| 214 | |
Ingo Tuchscherer | e28d2af | 2016-08-25 11:16:03 +0200 | [diff] [blame] | 215 | zc = zq->zcard; |
| 216 | spin_lock(&zcrypt_list_lock); |
| 217 | list_del_init(&zq->list); |
| 218 | zcrypt_device_count--; |
| 219 | spin_unlock(&zcrypt_list_lock); |
| 220 | zcrypt_card_put(zc); |
| 221 | if (zq->ops->rng) |
| 222 | zcrypt_rng_device_remove(); |
| 223 | sysfs_remove_group(&zq->queue->ap_dev.device.kobj, |
| 224 | &zcrypt_queue_attr_group); |
| 225 | put_device(&zq->queue->ap_dev.device); |
| 226 | zcrypt_queue_put(zq); |
| 227 | } |
| 228 | EXPORT_SYMBOL(zcrypt_queue_unregister); |