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>
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>
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)
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.
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/interrupt.h>
28 #include <linux/miscdevice.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>
40 #include "zcrypt_debug.h"
41 #include "zcrypt_api.h"
43 #include "zcrypt_msgtype6.h"
44 #include "zcrypt_msgtype50.h"
47 * Device attributes common for all crypto queue devices.
50 static ssize_t zcrypt_queue_online_show(struct device *dev,
51 struct device_attribute *attr,
54 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
56 return snprintf(buf, PAGE_SIZE, "%d\n", zq->online);
59 static ssize_t zcrypt_queue_online_store(struct device *dev,
60 struct device_attribute *attr,
61 const char *buf, size_t count)
63 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
64 struct zcrypt_card *zc = zq->zcard;
67 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
70 if (online && !zc->online)
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),
80 ap_flush_queue(zq->queue);
84 static DEVICE_ATTR(online, 0644, zcrypt_queue_online_show,
85 zcrypt_queue_online_store);
87 static struct attribute *zcrypt_queue_attrs[] = {
88 &dev_attr_online.attr,
92 static const struct attribute_group zcrypt_queue_attr_group = {
93 .attrs = zcrypt_queue_attrs,
96 void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
100 ap_flush_queue(zq->queue);
103 struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
105 struct zcrypt_queue *zq;
107 zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
110 zq->reply.message = kmalloc(max_response_size, GFP_KERNEL);
111 if (!zq->reply.message)
113 zq->reply.length = max_response_size;
114 INIT_LIST_HEAD(&zq->list);
115 kref_init(&zq->refcount);
122 EXPORT_SYMBOL(zcrypt_queue_alloc);
124 void zcrypt_queue_free(struct zcrypt_queue *zq)
126 kfree(zq->reply.message);
129 EXPORT_SYMBOL(zcrypt_queue_free);
131 static void zcrypt_queue_release(struct kref *kref)
133 struct zcrypt_queue *zq =
134 container_of(kref, struct zcrypt_queue, refcount);
135 zcrypt_queue_free(zq);
138 void zcrypt_queue_get(struct zcrypt_queue *zq)
140 kref_get(&zq->refcount);
142 EXPORT_SYMBOL(zcrypt_queue_get);
144 int zcrypt_queue_put(struct zcrypt_queue *zq)
146 return kref_put(&zq->refcount, zcrypt_queue_release);
148 EXPORT_SYMBOL(zcrypt_queue_put);
151 * zcrypt_queue_register() - Register a crypto queue device.
152 * @zq: Pointer to a crypto queue device
154 * Register a crypto queue device. Returns 0 if successful.
156 int zcrypt_queue_register(struct zcrypt_queue *zq)
158 struct zcrypt_card *zc;
161 spin_lock(&zcrypt_list_lock);
162 zc = zq->queue->card->private;
165 zq->online = 1; /* New devices are online by default. */
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));
170 list_add_tail(&zq->list, &zc->zqueues);
171 zcrypt_device_count++;
172 spin_unlock(&zcrypt_list_lock);
174 rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
175 &zcrypt_queue_attr_group);
178 get_device(&zq->queue->ap_dev.device);
181 rc = zcrypt_rng_device_add();
188 sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
189 &zcrypt_queue_attr_group);
190 put_device(&zq->queue->ap_dev.device);
192 spin_lock(&zcrypt_list_lock);
193 list_del_init(&zq->list);
194 spin_unlock(&zcrypt_list_lock);
198 EXPORT_SYMBOL(zcrypt_queue_register);
201 * zcrypt_queue_unregister(): Unregister a crypto queue device.
202 * @zq: Pointer to crypto queue device
204 * Unregister a crypto queue device.
206 void zcrypt_queue_unregister(struct zcrypt_queue *zq)
208 struct zcrypt_card *zc;
210 ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n",
211 AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
214 spin_lock(&zcrypt_list_lock);
215 list_del_init(&zq->list);
216 zcrypt_device_count--;
217 spin_unlock(&zcrypt_list_lock);
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);
226 EXPORT_SYMBOL(zcrypt_queue_unregister);