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
online_show(struct device * dev,struct device_attribute * attr,char * buf)39 static ssize_t online_show(struct device *dev,
40 struct device_attribute *attr,
41 char *buf)
42 {
43 struct ap_queue *aq = to_ap_queue(dev);
44 struct zcrypt_queue *zq = aq->private;
45 int online = aq->config && zq->online ? 1 : 0;
46
47 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
48 }
49
online_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)50 static ssize_t online_store(struct device *dev,
51 struct device_attribute *attr,
52 const char *buf, size_t count)
53 {
54 struct ap_queue *aq = to_ap_queue(dev);
55 struct zcrypt_queue *zq = aq->private;
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 return -ENODEV;
64 if (online && !zc->online)
65 return -EINVAL;
66 zq->online = online;
67
68 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x online=%d\n",
69 AP_QID_CARD(zq->queue->qid),
70 AP_QID_QUEUE(zq->queue->qid),
71 online);
72
73 if (!online)
74 ap_flush_queue(zq->queue);
75 return count;
76 }
77
78 static DEVICE_ATTR_RW(online);
79
load_show(struct device * dev,struct device_attribute * attr,char * buf)80 static ssize_t load_show(struct device *dev,
81 struct device_attribute *attr,
82 char *buf)
83 {
84 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
85
86 return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zq->load));
87 }
88
89 static DEVICE_ATTR_RO(load);
90
91 static struct attribute *zcrypt_queue_attrs[] = {
92 &dev_attr_online.attr,
93 &dev_attr_load.attr,
94 NULL,
95 };
96
97 static const struct attribute_group zcrypt_queue_attr_group = {
98 .attrs = zcrypt_queue_attrs,
99 };
100
zcrypt_queue_force_online(struct zcrypt_queue * zq,int online)101 void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
102 {
103 zq->online = online;
104 if (!online)
105 ap_flush_queue(zq->queue);
106 }
107
zcrypt_queue_alloc(size_t max_response_size)108 struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
109 {
110 struct zcrypt_queue *zq;
111
112 zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
113 if (!zq)
114 return NULL;
115 zq->reply.msg = kmalloc(max_response_size, GFP_KERNEL);
116 if (!zq->reply.msg)
117 goto out_free;
118 zq->reply.len = max_response_size;
119 INIT_LIST_HEAD(&zq->list);
120 kref_init(&zq->refcount);
121 return zq;
122
123 out_free:
124 kfree(zq);
125 return NULL;
126 }
127 EXPORT_SYMBOL(zcrypt_queue_alloc);
128
zcrypt_queue_free(struct zcrypt_queue * zq)129 void zcrypt_queue_free(struct zcrypt_queue *zq)
130 {
131 kfree(zq->reply.msg);
132 kfree(zq);
133 }
134 EXPORT_SYMBOL(zcrypt_queue_free);
135
zcrypt_queue_release(struct kref * kref)136 static void zcrypt_queue_release(struct kref *kref)
137 {
138 struct zcrypt_queue *zq =
139 container_of(kref, struct zcrypt_queue, refcount);
140 zcrypt_queue_free(zq);
141 }
142
zcrypt_queue_get(struct zcrypt_queue * zq)143 void zcrypt_queue_get(struct zcrypt_queue *zq)
144 {
145 kref_get(&zq->refcount);
146 }
147 EXPORT_SYMBOL(zcrypt_queue_get);
148
zcrypt_queue_put(struct zcrypt_queue * zq)149 int zcrypt_queue_put(struct zcrypt_queue *zq)
150 {
151 return kref_put(&zq->refcount, zcrypt_queue_release);
152 }
153 EXPORT_SYMBOL(zcrypt_queue_put);
154
155 /**
156 * zcrypt_queue_register() - Register a crypto queue device.
157 * @zq: Pointer to a crypto queue device
158 *
159 * Register a crypto queue device. Returns 0 if successful.
160 */
zcrypt_queue_register(struct zcrypt_queue * zq)161 int zcrypt_queue_register(struct zcrypt_queue *zq)
162 {
163 struct zcrypt_card *zc;
164 int rc;
165
166 spin_lock(&zcrypt_list_lock);
167 zc = zq->queue->card->private;
168 zcrypt_card_get(zc);
169 zq->zcard = zc;
170 zq->online = 1; /* New devices are online by default. */
171
172 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n",
173 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
174
175 list_add_tail(&zq->list, &zc->zqueues);
176 zcrypt_device_count++;
177 spin_unlock(&zcrypt_list_lock);
178
179 rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
180 &zcrypt_queue_attr_group);
181 if (rc)
182 goto out;
183
184 if (zq->ops->rng) {
185 rc = zcrypt_rng_device_add();
186 if (rc)
187 goto out_unregister;
188 }
189 return 0;
190
191 out_unregister:
192 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
193 &zcrypt_queue_attr_group);
194 out:
195 spin_lock(&zcrypt_list_lock);
196 list_del_init(&zq->list);
197 spin_unlock(&zcrypt_list_lock);
198 zcrypt_card_put(zc);
199 return rc;
200 }
201 EXPORT_SYMBOL(zcrypt_queue_register);
202
203 /**
204 * zcrypt_queue_unregister(): Unregister a crypto queue device.
205 * @zq: Pointer to crypto queue device
206 *
207 * Unregister a crypto queue device.
208 */
zcrypt_queue_unregister(struct zcrypt_queue * zq)209 void zcrypt_queue_unregister(struct zcrypt_queue *zq)
210 {
211 struct zcrypt_card *zc;
212
213 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n",
214 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
215
216 zc = zq->zcard;
217 spin_lock(&zcrypt_list_lock);
218 list_del_init(&zq->list);
219 zcrypt_device_count--;
220 spin_unlock(&zcrypt_list_lock);
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 zcrypt_card_put(zc);
226 zcrypt_queue_put(zq);
227 }
228 EXPORT_SYMBOL(zcrypt_queue_unregister);
229