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