1 // SPDX-License-Identifier: GPL-2.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>
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
,
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
;
58 if (sscanf(buf
, "%d\n", &online
) != 1 || online
< 0 || online
> 1)
61 if (online
&& !zc
->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
),
71 ap_flush_queue(zq
->queue
);
75 static DEVICE_ATTR_RW(online
);
77 static ssize_t
load_show(struct device
*dev
,
78 struct device_attribute
*attr
,
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
,
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
)
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
);
112 zq
->reply
.message
= kmalloc(max_response_size
, GFP_KERNEL
);
113 if (!zq
->reply
.message
)
115 zq
->reply
.length
= max_response_size
;
116 INIT_LIST_HEAD(&zq
->list
);
117 kref_init(&zq
->refcount
);
124 EXPORT_SYMBOL(zcrypt_queue_alloc
);
126 void zcrypt_queue_free(struct zcrypt_queue
*zq
)
128 kfree(zq
->reply
.message
);
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
;
163 spin_lock(&zcrypt_list_lock
);
164 zc
= zq
->queue
->card
->private;
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
);
180 get_device(&zq
->queue
->ap_dev
.device
);
183 rc
= zcrypt_rng_device_add();
190 sysfs_remove_group(&zq
->queue
->ap_dev
.device
.kobj
,
191 &zcrypt_queue_attr_group
);
192 put_device(&zq
->queue
->ap_dev
.device
);
194 spin_lock(&zcrypt_list_lock
);
195 list_del_init(&zq
->list
);
196 spin_unlock(&zcrypt_list_lock
);
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
));
216 spin_lock(&zcrypt_list_lock
);
217 list_del_init(&zq
->list
);
218 zcrypt_device_count
--;
219 spin_unlock(&zcrypt_list_lock
);
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
);