blob: c70b2528d9a3fbc4e07d3d21035aab76f8f4bd0f [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;
73 ZCRYPT_DBF_DEV(DBF_INFO, zq, "dev%02x%04xo%dman",
74 AP_QID_CARD(zq->queue->qid),
75 AP_QID_QUEUE(zq->queue->qid), online);
76 if (!online)
77 ap_flush_queue(zq->queue);
78 return count;
79}
80
81static DEVICE_ATTR(online, 0644, zcrypt_queue_online_show,
82 zcrypt_queue_online_store);
83
84static struct attribute *zcrypt_queue_attrs[] = {
85 &dev_attr_online.attr,
86 NULL,
87};
88
89static struct attribute_group zcrypt_queue_attr_group = {
90 .attrs = zcrypt_queue_attrs,
91};
92
93void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
94{
95 zq->online = online;
96 if (!online)
97 ap_flush_queue(zq->queue);
98}
99
100struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
101{
102 struct zcrypt_queue *zq;
103
104 zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
105 if (!zq)
106 return NULL;
107 zq->reply.message = kmalloc(max_response_size, GFP_KERNEL);
108 if (!zq->reply.message)
109 goto out_free;
110 zq->reply.length = max_response_size;
111 INIT_LIST_HEAD(&zq->list);
112 zq->dbf_area = zcrypt_dbf_devices;
113 kref_init(&zq->refcount);
114 return zq;
115
116out_free:
117 kfree(zq);
118 return NULL;
119}
120EXPORT_SYMBOL(zcrypt_queue_alloc);
121
122void zcrypt_queue_free(struct zcrypt_queue *zq)
123{
124 kfree(zq->reply.message);
125 kfree(zq);
126}
127EXPORT_SYMBOL(zcrypt_queue_free);
128
129static void zcrypt_queue_release(struct kref *kref)
130{
131 struct zcrypt_queue *zq =
132 container_of(kref, struct zcrypt_queue, refcount);
133 zcrypt_queue_free(zq);
134}
135
136void zcrypt_queue_get(struct zcrypt_queue *zq)
137{
138 kref_get(&zq->refcount);
139}
140EXPORT_SYMBOL(zcrypt_queue_get);
141
142int zcrypt_queue_put(struct zcrypt_queue *zq)
143{
144 return kref_put(&zq->refcount, zcrypt_queue_release);
145}
146EXPORT_SYMBOL(zcrypt_queue_put);
147
148/**
149 * zcrypt_queue_register() - Register a crypto queue device.
150 * @zq: Pointer to a crypto queue device
151 *
152 * Register a crypto queue device. Returns 0 if successful.
153 */
154int zcrypt_queue_register(struct zcrypt_queue *zq)
155{
156 struct zcrypt_card *zc;
157 int rc;
158
159 spin_lock(&zcrypt_list_lock);
160 zc = zq->queue->card->private;
161 zcrypt_card_get(zc);
162 zq->zcard = zc;
163 zq->online = 1; /* New devices are online by default. */
164 ZCRYPT_DBF_DEV(DBF_INFO, zq, "dev%02x%04xo%dreg",
165 AP_QID_CARD(zq->queue->qid),
166 AP_QID_QUEUE(zq->queue->qid),
167 zq->online);
168 list_add_tail(&zq->list, &zc->zqueues);
169 zcrypt_device_count++;
170 spin_unlock(&zcrypt_list_lock);
171
172 rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
173 &zcrypt_queue_attr_group);
174 if (rc)
175 goto out;
176 get_device(&zq->queue->ap_dev.device);
177
178 if (zq->ops->rng) {
179 rc = zcrypt_rng_device_add();
180 if (rc)
181 goto out_unregister;
182 }
183 return 0;
184
185out_unregister:
186 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
187 &zcrypt_queue_attr_group);
188 put_device(&zq->queue->ap_dev.device);
189out:
190 spin_lock(&zcrypt_list_lock);
191 list_del_init(&zq->list);
192 spin_unlock(&zcrypt_list_lock);
193 zcrypt_card_put(zc);
194 return rc;
195}
196EXPORT_SYMBOL(zcrypt_queue_register);
197
198/**
199 * zcrypt_queue_unregister(): Unregister a crypto queue device.
200 * @zq: Pointer to crypto queue device
201 *
202 * Unregister a crypto queue device.
203 */
204void zcrypt_queue_unregister(struct zcrypt_queue *zq)
205{
206 struct zcrypt_card *zc;
207
208 zc = zq->zcard;
209 spin_lock(&zcrypt_list_lock);
210 list_del_init(&zq->list);
211 zcrypt_device_count--;
212 spin_unlock(&zcrypt_list_lock);
213 zcrypt_card_put(zc);
214 if (zq->ops->rng)
215 zcrypt_rng_device_remove();
216 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
217 &zcrypt_queue_attr_group);
218 put_device(&zq->queue->ap_dev.device);
219 zcrypt_queue_put(zq);
220}
221EXPORT_SYMBOL(zcrypt_queue_unregister);