Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / s390 / crypto / zcrypt_card.c
blob050462d952224455860f4274b8d3e12ba602cba6
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>
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>
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>
29 #include "zcrypt_debug.h"
30 #include "zcrypt_api.h"
32 #include "zcrypt_msgtype6.h"
33 #include "zcrypt_msgtype50.h"
36 * Device attributes common for all crypto card devices.
39 static ssize_t type_show(struct device *dev,
40 struct device_attribute *attr, char *buf)
42 struct zcrypt_card *zc = dev_get_drvdata(dev);
44 return sysfs_emit(buf, "%s\n", zc->type_string);
47 static DEVICE_ATTR_RO(type);
49 static ssize_t online_show(struct device *dev,
50 struct device_attribute *attr,
51 char *buf)
53 struct zcrypt_card *zc = dev_get_drvdata(dev);
54 struct ap_card *ac = to_ap_card(dev);
55 int online = ac->config && !ac->chkstop && zc->online ? 1 : 0;
57 return sysfs_emit(buf, "%d\n", online);
60 static ssize_t online_store(struct device *dev,
61 struct device_attribute *attr,
62 const char *buf, size_t count)
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;
70 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
71 return -EINVAL;
73 if (online && (!ac->config || ac->chkstop))
74 return -ENODEV;
76 zc->online = online;
77 id = zc->card->id;
79 ZCRYPT_DBF_INFO("%s card=%02x online=%d\n", __func__, id, online);
81 ap_send_online_uevent(&ac->ap_dev, online);
83 spin_lock(&zcrypt_list_lock);
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.
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;
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);
107 kfree(zq_uelist);
110 return count;
113 static DEVICE_ATTR_RW(online);
115 static ssize_t load_show(struct device *dev,
116 struct device_attribute *attr,
117 char *buf)
119 struct zcrypt_card *zc = dev_get_drvdata(dev);
121 return sysfs_emit(buf, "%d\n", atomic_read(&zc->load));
124 static DEVICE_ATTR_RO(load);
126 static struct attribute *zcrypt_card_attrs[] = {
127 &dev_attr_type.attr,
128 &dev_attr_online.attr,
129 &dev_attr_load.attr,
130 NULL,
133 static const struct attribute_group zcrypt_card_attr_group = {
134 .attrs = zcrypt_card_attrs,
137 struct zcrypt_card *zcrypt_card_alloc(void)
139 struct zcrypt_card *zc;
141 zc = kzalloc(sizeof(*zc), 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;
149 EXPORT_SYMBOL(zcrypt_card_alloc);
151 void zcrypt_card_free(struct zcrypt_card *zc)
153 kfree(zc);
155 EXPORT_SYMBOL(zcrypt_card_free);
157 static void zcrypt_card_release(struct kref *kref)
159 struct zcrypt_card *zdev =
160 container_of(kref, struct zcrypt_card, refcount);
161 zcrypt_card_free(zdev);
164 void zcrypt_card_get(struct zcrypt_card *zc)
166 kref_get(&zc->refcount);
168 EXPORT_SYMBOL(zcrypt_card_get);
170 int zcrypt_card_put(struct zcrypt_card *zc)
172 return kref_put(&zc->refcount, zcrypt_card_release);
174 EXPORT_SYMBOL(zcrypt_card_put);
177 * zcrypt_card_register() - Register a crypto card device.
178 * @zc: Pointer to a crypto card device
180 * Register a crypto card device. Returns 0 if successful.
182 int zcrypt_card_register(struct zcrypt_card *zc)
184 int rc;
186 spin_lock(&zcrypt_list_lock);
187 list_add_tail(&zc->list, &zcrypt_card_list);
188 spin_unlock(&zcrypt_list_lock);
190 zc->online = 1;
192 ZCRYPT_DBF_INFO("%s card=%02x register online=1\n",
193 __func__, zc->card->id);
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);
203 return rc;
205 EXPORT_SYMBOL(zcrypt_card_register);
208 * zcrypt_card_unregister(): Unregister a crypto card device.
209 * @zc: Pointer to crypto card device
211 * Unregister a crypto card device.
213 void zcrypt_card_unregister(struct zcrypt_card *zc)
215 ZCRYPT_DBF_INFO("%s card=%02x unregister\n",
216 __func__, zc->card->id);
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);
225 EXPORT_SYMBOL(zcrypt_card_unregister);