blob: 8df82c6ef66edeccb60099f0421b7bfdfba37c85 [file] [log] [blame]
Greg Kroah-Hartman812141a2017-11-14 18:38:01 +01001// SPDX-License-Identifier: GPL-2.0+
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02002/*
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>
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020014 */
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
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020041static ssize_t online_show(struct device *dev,
42 struct device_attribute *attr,
43 char *buf)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020044{
45 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
46
47 return snprintf(buf, PAGE_SIZE, "%d\n", zq->online);
48}
49
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020050static ssize_t online_store(struct device *dev,
51 struct device_attribute *attr,
52 const char *buf, size_t count)
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020053{
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;
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010064
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
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020070 if (!online)
71 ap_flush_queue(zq->queue);
72 return count;
73}
74
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020075static DEVICE_ATTR_RW(online);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020076
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020077static ssize_t load_show(struct device *dev,
78 struct device_attribute *attr,
79 char *buf)
Harald Freudenberger4a077502018-06-07 15:09:48 +020080{
81 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
82
83 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zq->load));
84}
85
Harald Freudenbergerac2b96f2018-08-17 12:36:01 +020086static DEVICE_ATTR_RO(load);
Harald Freudenberger4a077502018-06-07 15:09:48 +020087
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020088static struct attribute *zcrypt_queue_attrs[] = {
89 &dev_attr_online.attr,
Harald Freudenberger4a077502018-06-07 15:09:48 +020090 &dev_attr_load.attr,
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020091 NULL,
92};
93
Arvind Yadav9920dec2017-07-19 12:39:10 +053094static const struct attribute_group zcrypt_queue_attr_group = {
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020095 .attrs = zcrypt_queue_attrs,
96};
97
98void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
99{
100 zq->online = online;
101 if (!online)
102 ap_flush_queue(zq->queue);
103}
104
105struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
106{
107 struct zcrypt_queue *zq;
108
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);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200117 kref_init(&zq->refcount);
118 return zq;
119
120out_free:
121 kfree(zq);
122 return NULL;
123}
124EXPORT_SYMBOL(zcrypt_queue_alloc);
125
126void zcrypt_queue_free(struct zcrypt_queue *zq)
127{
128 kfree(zq->reply.message);
129 kfree(zq);
130}
131EXPORT_SYMBOL(zcrypt_queue_free);
132
133static void zcrypt_queue_release(struct kref *kref)
134{
135 struct zcrypt_queue *zq =
136 container_of(kref, struct zcrypt_queue, refcount);
137 zcrypt_queue_free(zq);
138}
139
140void zcrypt_queue_get(struct zcrypt_queue *zq)
141{
142 kref_get(&zq->refcount);
143}
144EXPORT_SYMBOL(zcrypt_queue_get);
145
146int zcrypt_queue_put(struct zcrypt_queue *zq)
147{
148 return kref_put(&zq->refcount, zcrypt_queue_release);
149}
150EXPORT_SYMBOL(zcrypt_queue_put);
151
152/**
153 * zcrypt_queue_register() - Register a crypto queue device.
154 * @zq: Pointer to a crypto queue device
155 *
156 * Register a crypto queue device. Returns 0 if successful.
157 */
158int zcrypt_queue_register(struct zcrypt_queue *zq)
159{
160 struct zcrypt_card *zc;
161 int rc;
162
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. */
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100168
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));
171
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200172 list_add_tail(&zq->list, &zc->zqueues);
173 zcrypt_device_count++;
174 spin_unlock(&zcrypt_list_lock);
175
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);
181
182 if (zq->ops->rng) {
183 rc = zcrypt_rng_device_add();
184 if (rc)
185 goto out_unregister;
186 }
187 return 0;
188
189out_unregister:
190 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
191 &zcrypt_queue_attr_group);
192 put_device(&zq->queue->ap_dev.device);
193out:
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;
199}
200EXPORT_SYMBOL(zcrypt_queue_register);
201
202/**
203 * zcrypt_queue_unregister(): Unregister a crypto queue device.
204 * @zq: Pointer to crypto queue device
205 *
206 * Unregister a crypto queue device.
207 */
208void zcrypt_queue_unregister(struct zcrypt_queue *zq)
209{
210 struct zcrypt_card *zc;
211
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100212 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n",
213 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
214
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200215 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);
227}
228EXPORT_SYMBOL(zcrypt_queue_unregister);