1 // SPDX-License-Identifier: GPL-2.0+
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>
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 
41 static ssize_t zcrypt_queue_online_show(struct device *dev,
42 					struct device_attribute *attr,
43 					char *buf)
44 {
45 	struct zcrypt_queue *zq = to_ap_queue(dev)->private;
46 
47 	return snprintf(buf, PAGE_SIZE, "%d\n", zq->online);
48 }
49 
50 static ssize_t zcrypt_queue_online_store(struct device *dev,
51 					 struct device_attribute *attr,
52 					 const char *buf, size_t count)
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;
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 
70 	if (!online)
71 		ap_flush_queue(zq->queue);
72 	return count;
73 }
74 
75 static DEVICE_ATTR(online, 0644, zcrypt_queue_online_show,
76 		   zcrypt_queue_online_store);
77 
78 static ssize_t zcrypt_queue_load_show(struct device *dev,
79 				      struct device_attribute *attr,
80 				      char *buf)
81 {
82 	struct zcrypt_queue *zq = to_ap_queue(dev)->private;
83 
84 	return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zq->load));
85 }
86 
87 static DEVICE_ATTR(load, 0444, zcrypt_queue_load_show, NULL);
88 
89 static struct attribute *zcrypt_queue_attrs[] = {
90 	&dev_attr_online.attr,
91 	&dev_attr_load.attr,
92 	NULL,
93 };
94 
95 static const struct attribute_group zcrypt_queue_attr_group = {
96 	.attrs = zcrypt_queue_attrs,
97 };
98 
99 void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
100 {
101 	zq->online = online;
102 	if (!online)
103 		ap_flush_queue(zq->queue);
104 }
105 
106 struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
107 {
108 	struct zcrypt_queue *zq;
109 
110 	zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
111 	if (!zq)
112 		return NULL;
113 	zq->reply.message = kmalloc(max_response_size, GFP_KERNEL);
114 	if (!zq->reply.message)
115 		goto out_free;
116 	zq->reply.length = max_response_size;
117 	INIT_LIST_HEAD(&zq->list);
118 	kref_init(&zq->refcount);
119 	return zq;
120 
121 out_free:
122 	kfree(zq);
123 	return NULL;
124 }
125 EXPORT_SYMBOL(zcrypt_queue_alloc);
126 
127 void zcrypt_queue_free(struct zcrypt_queue *zq)
128 {
129 	kfree(zq->reply.message);
130 	kfree(zq);
131 }
132 EXPORT_SYMBOL(zcrypt_queue_free);
133 
134 static void zcrypt_queue_release(struct kref *kref)
135 {
136 	struct zcrypt_queue *zq =
137 		container_of(kref, struct zcrypt_queue, refcount);
138 	zcrypt_queue_free(zq);
139 }
140 
141 void zcrypt_queue_get(struct zcrypt_queue *zq)
142 {
143 	kref_get(&zq->refcount);
144 }
145 EXPORT_SYMBOL(zcrypt_queue_get);
146 
147 int zcrypt_queue_put(struct zcrypt_queue *zq)
148 {
149 	return kref_put(&zq->refcount, zcrypt_queue_release);
150 }
151 EXPORT_SYMBOL(zcrypt_queue_put);
152 
153 /**
154  * zcrypt_queue_register() - Register a crypto queue device.
155  * @zq: Pointer to a crypto queue device
156  *
157  * Register a crypto queue device. Returns 0 if successful.
158  */
159 int zcrypt_queue_register(struct zcrypt_queue *zq)
160 {
161 	struct zcrypt_card *zc;
162 	int rc;
163 
164 	spin_lock(&zcrypt_list_lock);
165 	zc = zq->queue->card->private;
166 	zcrypt_card_get(zc);
167 	zq->zcard = zc;
168 	zq->online = 1;	/* New devices are online by default. */
169 
170 	ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n",
171 		   AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
172 
173 	list_add_tail(&zq->list, &zc->zqueues);
174 	zcrypt_device_count++;
175 	spin_unlock(&zcrypt_list_lock);
176 
177 	rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
178 				&zcrypt_queue_attr_group);
179 	if (rc)
180 		goto out;
181 	get_device(&zq->queue->ap_dev.device);
182 
183 	if (zq->ops->rng) {
184 		rc = zcrypt_rng_device_add();
185 		if (rc)
186 			goto out_unregister;
187 	}
188 	return 0;
189 
190 out_unregister:
191 	sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
192 			   &zcrypt_queue_attr_group);
193 	put_device(&zq->queue->ap_dev.device);
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  */
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 	zcrypt_card_put(zc);
222 	if (zq->ops->rng)
223 		zcrypt_rng_device_remove();
224 	sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
225 			   &zcrypt_queue_attr_group);
226 	put_device(&zq->queue->ap_dev.device);
227 	zcrypt_queue_put(zq);
228 }
229 EXPORT_SYMBOL(zcrypt_queue_unregister);
230