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 card devices.
37  */
38 
39 static ssize_t type_show(struct device *dev,
40 			 struct device_attribute *attr, char *buf)
41 {
42 	struct zcrypt_card *zc = dev_get_drvdata(dev);
43 
44 	return scnprintf(buf, PAGE_SIZE, "%s\n", zc->type_string);
45 }
46 
47 static DEVICE_ATTR_RO(type);
48 
49 static ssize_t online_show(struct device *dev,
50 			   struct device_attribute *attr,
51 			   char *buf)
52 {
53 	struct zcrypt_card *zc = dev_get_drvdata(dev);
54 	struct ap_card *ac = to_ap_card(dev);
55 	int online = ac->config && zc->online ? 1 : 0;
56 
57 	return scnprintf(buf, PAGE_SIZE, "%d\n", online);
58 }
59 
60 static ssize_t online_store(struct device *dev,
61 			    struct device_attribute *attr,
62 			    const char *buf, size_t count)
63 {
64 	struct zcrypt_card *zc = dev_get_drvdata(dev);
65 	struct ap_card *ac = to_ap_card(dev);
66 	struct zcrypt_queue *zq;
67 	int online, id, i = 0, maxzqs = 0;
68 	struct zcrypt_queue **zq_uelist = NULL;
69 
70 	if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
71 		return -EINVAL;
72 
73 	if (online && !ac->config)
74 		return -ENODEV;
75 
76 	zc->online = online;
77 	id = zc->card->id;
78 
79 	ZCRYPT_DBF_INFO("%s card=%02x online=%d\n", __func__, id, online);
80 
81 	ap_send_online_uevent(&ac->ap_dev, online);
82 
83 	spin_lock(&zcrypt_list_lock);
84 	/*
85 	 * As we are in atomic context here, directly sending uevents
86 	 * does not work. So collect the zqueues in a dynamic array
87 	 * and process them after zcrypt_list_lock release. As we get/put
88 	 * the zqueue objects, we make sure they exist after lock release.
89 	 */
90 	list_for_each_entry(zq, &zc->zqueues, list)
91 		maxzqs++;
92 	if (maxzqs > 0)
93 		zq_uelist = kcalloc(maxzqs + 1, sizeof(*zq_uelist), GFP_ATOMIC);
94 	list_for_each_entry(zq, &zc->zqueues, list)
95 		if (zcrypt_queue_force_online(zq, online))
96 			if (zq_uelist) {
97 				zcrypt_queue_get(zq);
98 				zq_uelist[i++] = zq;
99 			}
100 	spin_unlock(&zcrypt_list_lock);
101 	if (zq_uelist) {
102 		for (i = 0; zq_uelist[i]; i++) {
103 			zq = zq_uelist[i];
104 			ap_send_online_uevent(&zq->queue->ap_dev, online);
105 			zcrypt_queue_put(zq);
106 		}
107 		kfree(zq_uelist);
108 	}
109 
110 	return count;
111 }
112 
113 static DEVICE_ATTR_RW(online);
114 
115 static ssize_t load_show(struct device *dev,
116 			 struct device_attribute *attr,
117 			 char *buf)
118 {
119 	struct zcrypt_card *zc = dev_get_drvdata(dev);
120 
121 	return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
122 }
123 
124 static DEVICE_ATTR_RO(load);
125 
126 static struct attribute *zcrypt_card_attrs[] = {
127 	&dev_attr_type.attr,
128 	&dev_attr_online.attr,
129 	&dev_attr_load.attr,
130 	NULL,
131 };
132 
133 static const struct attribute_group zcrypt_card_attr_group = {
134 	.attrs = zcrypt_card_attrs,
135 };
136 
137 struct zcrypt_card *zcrypt_card_alloc(void)
138 {
139 	struct zcrypt_card *zc;
140 
141 	zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL);
142 	if (!zc)
143 		return NULL;
144 	INIT_LIST_HEAD(&zc->list);
145 	INIT_LIST_HEAD(&zc->zqueues);
146 	kref_init(&zc->refcount);
147 	return zc;
148 }
149 EXPORT_SYMBOL(zcrypt_card_alloc);
150 
151 void zcrypt_card_free(struct zcrypt_card *zc)
152 {
153 	kfree(zc);
154 }
155 EXPORT_SYMBOL(zcrypt_card_free);
156 
157 static void zcrypt_card_release(struct kref *kref)
158 {
159 	struct zcrypt_card *zdev =
160 		container_of(kref, struct zcrypt_card, refcount);
161 	zcrypt_card_free(zdev);
162 }
163 
164 void zcrypt_card_get(struct zcrypt_card *zc)
165 {
166 	kref_get(&zc->refcount);
167 }
168 EXPORT_SYMBOL(zcrypt_card_get);
169 
170 int zcrypt_card_put(struct zcrypt_card *zc)
171 {
172 	return kref_put(&zc->refcount, zcrypt_card_release);
173 }
174 EXPORT_SYMBOL(zcrypt_card_put);
175 
176 /**
177  * zcrypt_card_register() - Register a crypto card device.
178  * @zc: Pointer to a crypto card device
179  *
180  * Register a crypto card device. Returns 0 if successful.
181  */
182 int zcrypt_card_register(struct zcrypt_card *zc)
183 {
184 	int rc;
185 
186 	spin_lock(&zcrypt_list_lock);
187 	list_add_tail(&zc->list, &zcrypt_card_list);
188 	spin_unlock(&zcrypt_list_lock);
189 
190 	zc->online = 1;
191 
192 	ZCRYPT_DBF_INFO("%s card=%02x register online=1\n",
193 			__func__, zc->card->id);
194 
195 	rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
196 				&zcrypt_card_attr_group);
197 	if (rc) {
198 		spin_lock(&zcrypt_list_lock);
199 		list_del_init(&zc->list);
200 		spin_unlock(&zcrypt_list_lock);
201 	}
202 
203 	return rc;
204 }
205 EXPORT_SYMBOL(zcrypt_card_register);
206 
207 /**
208  * zcrypt_card_unregister(): Unregister a crypto card device.
209  * @zc: Pointer to crypto card device
210  *
211  * Unregister a crypto card device.
212  */
213 void zcrypt_card_unregister(struct zcrypt_card *zc)
214 {
215 	ZCRYPT_DBF_INFO("%s card=%02x unregister\n",
216 			__func__, zc->card->id);
217 
218 	spin_lock(&zcrypt_list_lock);
219 	list_del_init(&zc->list);
220 	spin_unlock(&zcrypt_list_lock);
221 	sysfs_remove_group(&zc->card->ap_dev.device.kobj,
222 			   &zcrypt_card_attr_group);
223 	zcrypt_card_put(zc);
224 }
225 EXPORT_SYMBOL(zcrypt_card_unregister);
226