staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_queue.c
blob8df82c6ef66edeccb60099f0421b7bfdfba37c85
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * zcrypt 2.1.0
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>
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>
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>
31 #include "zcrypt_debug.h"
32 #include "zcrypt_api.h"
34 #include "zcrypt_msgtype6.h"
35 #include "zcrypt_msgtype50.h"
38 * Device attributes common for all crypto queue devices.
41 static ssize_t online_show(struct device *dev,
42 struct device_attribute *attr,
43 char *buf)
45 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
47 return snprintf(buf, PAGE_SIZE, "%d\n", zq->online);
50 static ssize_t online_store(struct device *dev,
51 struct device_attribute *attr,
52 const char *buf, size_t count)
54 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
55 struct zcrypt_card *zc = zq->zcard;
56 int online;
58 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
59 return -EINVAL;
61 if (online && !zc->online)
62 return -EINVAL;
63 zq->online = online;
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);
70 if (!online)
71 ap_flush_queue(zq->queue);
72 return count;
75 static DEVICE_ATTR_RW(online);
77 static ssize_t load_show(struct device *dev,
78 struct device_attribute *attr,
79 char *buf)
81 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
83 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zq->load));
86 static DEVICE_ATTR_RO(load);
88 static struct attribute *zcrypt_queue_attrs[] = {
89 &dev_attr_online.attr,
90 &dev_attr_load.attr,
91 NULL,
94 static const struct attribute_group zcrypt_queue_attr_group = {
95 .attrs = zcrypt_queue_attrs,
98 void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
100 zq->online = online;
101 if (!online)
102 ap_flush_queue(zq->queue);
105 struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
107 struct zcrypt_queue *zq;
109 zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
110 if (!zq)
111 return NULL;
112 zq->reply.message = kmalloc(max_response_size, GFP_KERNEL);
113 if (!zq->reply.message)
114 goto out_free;
115 zq->reply.length = max_response_size;
116 INIT_LIST_HEAD(&zq->list);
117 kref_init(&zq->refcount);
118 return zq;
120 out_free:
121 kfree(zq);
122 return NULL;
124 EXPORT_SYMBOL(zcrypt_queue_alloc);
126 void zcrypt_queue_free(struct zcrypt_queue *zq)
128 kfree(zq->reply.message);
129 kfree(zq);
131 EXPORT_SYMBOL(zcrypt_queue_free);
133 static void zcrypt_queue_release(struct kref *kref)
135 struct zcrypt_queue *zq =
136 container_of(kref, struct zcrypt_queue, refcount);
137 zcrypt_queue_free(zq);
140 void zcrypt_queue_get(struct zcrypt_queue *zq)
142 kref_get(&zq->refcount);
144 EXPORT_SYMBOL(zcrypt_queue_get);
146 int zcrypt_queue_put(struct zcrypt_queue *zq)
148 return kref_put(&zq->refcount, zcrypt_queue_release);
150 EXPORT_SYMBOL(zcrypt_queue_put);
153 * zcrypt_queue_register() - Register a crypto queue device.
154 * @zq: Pointer to a crypto queue device
156 * Register a crypto queue device. Returns 0 if successful.
158 int zcrypt_queue_register(struct zcrypt_queue *zq)
160 struct zcrypt_card *zc;
161 int rc;
163 spin_lock(&zcrypt_list_lock);
164 zc = zq->queue->card->private;
165 zcrypt_card_get(zc);
166 zq->zcard = zc;
167 zq->online = 1; /* New devices are online by default. */
169 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n",
170 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
172 list_add_tail(&zq->list, &zc->zqueues);
173 zcrypt_device_count++;
174 spin_unlock(&zcrypt_list_lock);
176 rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
177 &zcrypt_queue_attr_group);
178 if (rc)
179 goto out;
180 get_device(&zq->queue->ap_dev.device);
182 if (zq->ops->rng) {
183 rc = zcrypt_rng_device_add();
184 if (rc)
185 goto out_unregister;
187 return 0;
189 out_unregister:
190 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
191 &zcrypt_queue_attr_group);
192 put_device(&zq->queue->ap_dev.device);
193 out:
194 spin_lock(&zcrypt_list_lock);
195 list_del_init(&zq->list);
196 spin_unlock(&zcrypt_list_lock);
197 zcrypt_card_put(zc);
198 return rc;
200 EXPORT_SYMBOL(zcrypt_queue_register);
203 * zcrypt_queue_unregister(): Unregister a crypto queue device.
204 * @zq: Pointer to crypto queue device
206 * Unregister a crypto queue device.
208 void zcrypt_queue_unregister(struct zcrypt_queue *zq)
210 struct zcrypt_card *zc;
212 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n",
213 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
215 zc = zq->zcard;
216 spin_lock(&zcrypt_list_lock);
217 list_del_init(&zq->list);
218 zcrypt_device_count--;
219 spin_unlock(&zcrypt_list_lock);
220 zcrypt_card_put(zc);
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 put_device(&zq->queue->ap_dev.device);
226 zcrypt_queue_put(zq);
228 EXPORT_SYMBOL(zcrypt_queue_unregister);