1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2001, 2012
4 * Author(s): Robert Burroughs
5 * Eric Rossman (edrossma@us.ibm.com)
6 * Cornelia Huck <cornelia.huck@de.ibm.com>
8 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Ralph Wuerthner <rwuerthn@de.ibm.com>
11 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/miscdevice.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <linux/compat.h>
22 #include <linux/slab.h>
23 #include <linux/atomic.h>
24 #include <linux/uaccess.h>
25 #include <linux/hw_random.h>
26 #include <linux/debugfs.h>
27 #include <asm/debug.h>
29 #include "zcrypt_debug.h"
30 #include "zcrypt_api.h"
32 #include "zcrypt_msgtype6.h"
33 #include "zcrypt_msgtype50.h"
36 * Device attributes common for all crypto queue devices.
39 static ssize_t
online_show(struct device
*dev
,
40 struct device_attribute
*attr
,
43 struct zcrypt_queue
*zq
= dev_get_drvdata(dev
);
44 struct ap_queue
*aq
= to_ap_queue(dev
);
45 int online
= aq
->config
&& !aq
->chkstop
&& zq
->online
? 1 : 0;
47 return sysfs_emit(buf
, "%d\n", 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
= dev_get_drvdata(dev
);
55 struct ap_queue
*aq
= to_ap_queue(dev
);
56 struct zcrypt_card
*zc
= zq
->zcard
;
59 if (sscanf(buf
, "%d\n", &online
) != 1 || online
< 0 || online
> 1)
62 if (online
&& (!aq
->config
|| !aq
->card
->config
||
63 aq
->chkstop
|| aq
->card
->chkstop
))
65 if (online
&& !zc
->online
)
69 ZCRYPT_DBF_INFO("%s queue=%02x.%04x online=%d\n",
70 __func__
, AP_QID_CARD(zq
->queue
->qid
),
71 AP_QID_QUEUE(zq
->queue
->qid
), online
);
73 ap_send_online_uevent(&aq
->ap_dev
, online
);
76 ap_flush_queue(zq
->queue
);
80 static DEVICE_ATTR_RW(online
);
82 static ssize_t
load_show(struct device
*dev
,
83 struct device_attribute
*attr
,
86 struct zcrypt_queue
*zq
= dev_get_drvdata(dev
);
88 return sysfs_emit(buf
, "%d\n", atomic_read(&zq
->load
));
91 static DEVICE_ATTR_RO(load
);
93 static struct attribute
*zcrypt_queue_attrs
[] = {
94 &dev_attr_online
.attr
,
99 static const struct attribute_group zcrypt_queue_attr_group
= {
100 .attrs
= zcrypt_queue_attrs
,
103 bool zcrypt_queue_force_online(struct zcrypt_queue
*zq
, int online
)
105 if (!!zq
->online
!= !!online
) {
108 ap_flush_queue(zq
->queue
);
114 struct zcrypt_queue
*zcrypt_queue_alloc(size_t reply_buf_size
)
116 struct zcrypt_queue
*zq
;
118 zq
= kzalloc(sizeof(*zq
), GFP_KERNEL
);
121 zq
->reply
.msg
= kmalloc(reply_buf_size
, GFP_KERNEL
);
124 zq
->reply
.bufsize
= reply_buf_size
;
125 INIT_LIST_HEAD(&zq
->list
);
126 kref_init(&zq
->refcount
);
133 EXPORT_SYMBOL(zcrypt_queue_alloc
);
135 void zcrypt_queue_free(struct zcrypt_queue
*zq
)
137 kfree(zq
->reply
.msg
);
140 EXPORT_SYMBOL(zcrypt_queue_free
);
142 static void zcrypt_queue_release(struct kref
*kref
)
144 struct zcrypt_queue
*zq
=
145 container_of(kref
, struct zcrypt_queue
, refcount
);
146 zcrypt_queue_free(zq
);
149 void zcrypt_queue_get(struct zcrypt_queue
*zq
)
151 kref_get(&zq
->refcount
);
153 EXPORT_SYMBOL(zcrypt_queue_get
);
155 int zcrypt_queue_put(struct zcrypt_queue
*zq
)
157 return kref_put(&zq
->refcount
, zcrypt_queue_release
);
159 EXPORT_SYMBOL(zcrypt_queue_put
);
162 * zcrypt_queue_register() - Register a crypto queue device.
163 * @zq: Pointer to a crypto queue device
165 * Register a crypto queue device. Returns 0 if successful.
167 int zcrypt_queue_register(struct zcrypt_queue
*zq
)
169 struct zcrypt_card
*zc
;
172 spin_lock(&zcrypt_list_lock
);
173 zc
= dev_get_drvdata(&zq
->queue
->card
->ap_dev
.device
);
176 zq
->online
= 1; /* New devices are online by default. */
178 ZCRYPT_DBF_INFO("%s queue=%02x.%04x register online=1\n",
179 __func__
, AP_QID_CARD(zq
->queue
->qid
),
180 AP_QID_QUEUE(zq
->queue
->qid
));
182 list_add_tail(&zq
->list
, &zc
->zqueues
);
183 spin_unlock(&zcrypt_list_lock
);
185 rc
= sysfs_create_group(&zq
->queue
->ap_dev
.device
.kobj
,
186 &zcrypt_queue_attr_group
);
191 rc
= zcrypt_rng_device_add();
198 sysfs_remove_group(&zq
->queue
->ap_dev
.device
.kobj
,
199 &zcrypt_queue_attr_group
);
201 spin_lock(&zcrypt_list_lock
);
202 list_del_init(&zq
->list
);
203 spin_unlock(&zcrypt_list_lock
);
207 EXPORT_SYMBOL(zcrypt_queue_register
);
210 * zcrypt_queue_unregister(): Unregister a crypto queue device.
211 * @zq: Pointer to crypto queue device
213 * Unregister a crypto queue device.
215 void zcrypt_queue_unregister(struct zcrypt_queue
*zq
)
217 struct zcrypt_card
*zc
;
219 ZCRYPT_DBF_INFO("%s queue=%02x.%04x unregister\n",
220 __func__
, AP_QID_CARD(zq
->queue
->qid
),
221 AP_QID_QUEUE(zq
->queue
->qid
));
224 spin_lock(&zcrypt_list_lock
);
225 list_del_init(&zq
->list
);
226 spin_unlock(&zcrypt_list_lock
);
228 zcrypt_rng_device_remove();
229 sysfs_remove_group(&zq
->queue
->ap_dev
.device
.kobj
,
230 &zcrypt_queue_attr_group
);
232 zcrypt_queue_put(zq
);
234 EXPORT_SYMBOL(zcrypt_queue_unregister
);