blob: a303f3b2c328624ed3bd299372cbb258f02e409a [file] [log] [blame]
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +02001/*
2 * zcrypt 2.1.0
3 *
4 * Copyright IBM Corp. 2001, 2012
5 * Author(s): Robert Burroughs
6 * Eric Rossman (edrossma@us.ibm.com)
7 * Cornelia Huck <cornelia.huck@de.ibm.com>
8 *
9 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
10 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
11 * Ralph Wuerthner <rwuerthn@de.ibm.com>
12 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/interrupt.h>
28#include <linux/miscdevice.h>
29#include <linux/fs.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
32#include <linux/compat.h>
33#include <linux/slab.h>
34#include <linux/atomic.h>
35#include <linux/uaccess.h>
36#include <linux/hw_random.h>
37#include <linux/debugfs.h>
38#include <asm/debug.h>
39
40#include "zcrypt_debug.h"
41#include "zcrypt_api.h"
42
43#include "zcrypt_msgtype6.h"
44#include "zcrypt_msgtype50.h"
45
46/*
47 * Device attributes common for all crypto queue devices.
48 */
49
50static ssize_t zcrypt_queue_online_show(struct device *dev,
51 struct device_attribute *attr,
52 char *buf)
53{
54 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
55
56 return snprintf(buf, PAGE_SIZE, "%d\n", zq->online);
57}
58
59static ssize_t zcrypt_queue_online_store(struct device *dev,
60 struct device_attribute *attr,
61 const char *buf, size_t count)
62{
63 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
64 struct zcrypt_card *zc = zq->zcard;
65 int online;
66
67 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
68 return -EINVAL;
69
70 if (online && !zc->online)
71 return -EINVAL;
72 zq->online = online;
Harald Freudenbergercccd85b2016-11-24 06:45:21 +010073
74 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x online=%d\n",
75 AP_QID_CARD(zq->queue->qid),
76 AP_QID_QUEUE(zq->queue->qid),
77 online);
78
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020079 if (!online)
80 ap_flush_queue(zq->queue);
81 return count;
82}
83
84static DEVICE_ATTR(online, 0644, zcrypt_queue_online_show,
85 zcrypt_queue_online_store);
86
87static struct attribute *zcrypt_queue_attrs[] = {
88 &dev_attr_online.attr,
89 NULL,
90};
91
92static struct attribute_group zcrypt_queue_attr_group = {
93 .attrs = zcrypt_queue_attrs,
94};
95
96void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
97{
98 zq->online = online;
99 if (!online)
100 ap_flush_queue(zq->queue);
101}
102
103struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
104{
105 struct zcrypt_queue *zq;
106
107 zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
108 if (!zq)
109 return NULL;
110 zq->reply.message = kmalloc(max_response_size, GFP_KERNEL);
111 if (!zq->reply.message)
112 goto out_free;
113 zq->reply.length = max_response_size;
114 INIT_LIST_HEAD(&zq->list);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200115 kref_init(&zq->refcount);
116 return zq;
117
118out_free:
119 kfree(zq);
120 return NULL;
121}
122EXPORT_SYMBOL(zcrypt_queue_alloc);
123
124void zcrypt_queue_free(struct zcrypt_queue *zq)
125{
126 kfree(zq->reply.message);
127 kfree(zq);
128}
129EXPORT_SYMBOL(zcrypt_queue_free);
130
131static void zcrypt_queue_release(struct kref *kref)
132{
133 struct zcrypt_queue *zq =
134 container_of(kref, struct zcrypt_queue, refcount);
135 zcrypt_queue_free(zq);
136}
137
138void zcrypt_queue_get(struct zcrypt_queue *zq)
139{
140 kref_get(&zq->refcount);
141}
142EXPORT_SYMBOL(zcrypt_queue_get);
143
144int zcrypt_queue_put(struct zcrypt_queue *zq)
145{
146 return kref_put(&zq->refcount, zcrypt_queue_release);
147}
148EXPORT_SYMBOL(zcrypt_queue_put);
149
150/**
151 * zcrypt_queue_register() - Register a crypto queue device.
152 * @zq: Pointer to a crypto queue device
153 *
154 * Register a crypto queue device. Returns 0 if successful.
155 */
156int zcrypt_queue_register(struct zcrypt_queue *zq)
157{
158 struct zcrypt_card *zc;
159 int rc;
160
161 spin_lock(&zcrypt_list_lock);
162 zc = zq->queue->card->private;
163 zcrypt_card_get(zc);
164 zq->zcard = zc;
165 zq->online = 1; /* New devices are online by default. */
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100166
167 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n",
168 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
169
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200170 list_add_tail(&zq->list, &zc->zqueues);
171 zcrypt_device_count++;
172 spin_unlock(&zcrypt_list_lock);
173
174 rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
175 &zcrypt_queue_attr_group);
176 if (rc)
177 goto out;
178 get_device(&zq->queue->ap_dev.device);
179
180 if (zq->ops->rng) {
181 rc = zcrypt_rng_device_add();
182 if (rc)
183 goto out_unregister;
184 }
185 return 0;
186
187out_unregister:
188 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
189 &zcrypt_queue_attr_group);
190 put_device(&zq->queue->ap_dev.device);
191out:
192 spin_lock(&zcrypt_list_lock);
193 list_del_init(&zq->list);
194 spin_unlock(&zcrypt_list_lock);
195 zcrypt_card_put(zc);
196 return rc;
197}
198EXPORT_SYMBOL(zcrypt_queue_register);
199
200/**
201 * zcrypt_queue_unregister(): Unregister a crypto queue device.
202 * @zq: Pointer to crypto queue device
203 *
204 * Unregister a crypto queue device.
205 */
206void zcrypt_queue_unregister(struct zcrypt_queue *zq)
207{
208 struct zcrypt_card *zc;
209
Harald Freudenbergercccd85b2016-11-24 06:45:21 +0100210 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n",
211 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
212
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200213 zc = zq->zcard;
214 spin_lock(&zcrypt_list_lock);
215 list_del_init(&zq->list);
216 zcrypt_device_count--;
217 spin_unlock(&zcrypt_list_lock);
218 zcrypt_card_put(zc);
219 if (zq->ops->rng)
220 zcrypt_rng_device_remove();
221 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
222 &zcrypt_queue_attr_group);
223 put_device(&zq->queue->ap_dev.device);
224 zcrypt_queue_put(zq);
225}
226EXPORT_SYMBOL(zcrypt_queue_unregister);