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