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 card devices.
39 static ssize_t
type_show(struct device
*dev
,
40 struct device_attribute
*attr
, char *buf
)
42 struct zcrypt_card
*zc
= dev_get_drvdata(dev
);
44 return sysfs_emit(buf
, "%s\n", zc
->type_string
);
47 static DEVICE_ATTR_RO(type
);
49 static ssize_t
online_show(struct device
*dev
,
50 struct device_attribute
*attr
,
53 struct zcrypt_card
*zc
= dev_get_drvdata(dev
);
54 struct ap_card
*ac
= to_ap_card(dev
);
55 int online
= ac
->config
&& !ac
->chkstop
&& zc
->online
? 1 : 0;
57 return sysfs_emit(buf
, "%d\n", online
);
60 static ssize_t
online_store(struct device
*dev
,
61 struct device_attribute
*attr
,
62 const char *buf
, size_t count
)
64 struct zcrypt_card
*zc
= dev_get_drvdata(dev
);
65 struct ap_card
*ac
= to_ap_card(dev
);
66 struct zcrypt_queue
*zq
;
67 int online
, id
, i
= 0, maxzqs
= 0;
68 struct zcrypt_queue
**zq_uelist
= NULL
;
70 if (sscanf(buf
, "%d\n", &online
) != 1 || online
< 0 || online
> 1)
73 if (online
&& (!ac
->config
|| ac
->chkstop
))
79 ZCRYPT_DBF_INFO("%s card=%02x online=%d\n", __func__
, id
, online
);
81 ap_send_online_uevent(&ac
->ap_dev
, online
);
83 spin_lock(&zcrypt_list_lock
);
85 * As we are in atomic context here, directly sending uevents
86 * does not work. So collect the zqueues in a dynamic array
87 * and process them after zcrypt_list_lock release. As we get/put
88 * the zqueue objects, we make sure they exist after lock release.
90 list_for_each_entry(zq
, &zc
->zqueues
, list
)
93 zq_uelist
= kcalloc(maxzqs
+ 1, sizeof(*zq_uelist
), GFP_ATOMIC
);
94 list_for_each_entry(zq
, &zc
->zqueues
, list
)
95 if (zcrypt_queue_force_online(zq
, online
))
100 spin_unlock(&zcrypt_list_lock
);
102 for (i
= 0; zq_uelist
[i
]; i
++) {
104 ap_send_online_uevent(&zq
->queue
->ap_dev
, online
);
105 zcrypt_queue_put(zq
);
113 static DEVICE_ATTR_RW(online
);
115 static ssize_t
load_show(struct device
*dev
,
116 struct device_attribute
*attr
,
119 struct zcrypt_card
*zc
= dev_get_drvdata(dev
);
121 return sysfs_emit(buf
, "%d\n", atomic_read(&zc
->load
));
124 static DEVICE_ATTR_RO(load
);
126 static struct attribute
*zcrypt_card_attrs
[] = {
128 &dev_attr_online
.attr
,
133 static const struct attribute_group zcrypt_card_attr_group
= {
134 .attrs
= zcrypt_card_attrs
,
137 struct zcrypt_card
*zcrypt_card_alloc(void)
139 struct zcrypt_card
*zc
;
141 zc
= kzalloc(sizeof(*zc
), GFP_KERNEL
);
144 INIT_LIST_HEAD(&zc
->list
);
145 INIT_LIST_HEAD(&zc
->zqueues
);
146 kref_init(&zc
->refcount
);
149 EXPORT_SYMBOL(zcrypt_card_alloc
);
151 void zcrypt_card_free(struct zcrypt_card
*zc
)
155 EXPORT_SYMBOL(zcrypt_card_free
);
157 static void zcrypt_card_release(struct kref
*kref
)
159 struct zcrypt_card
*zdev
=
160 container_of(kref
, struct zcrypt_card
, refcount
);
161 zcrypt_card_free(zdev
);
164 void zcrypt_card_get(struct zcrypt_card
*zc
)
166 kref_get(&zc
->refcount
);
168 EXPORT_SYMBOL(zcrypt_card_get
);
170 int zcrypt_card_put(struct zcrypt_card
*zc
)
172 return kref_put(&zc
->refcount
, zcrypt_card_release
);
174 EXPORT_SYMBOL(zcrypt_card_put
);
177 * zcrypt_card_register() - Register a crypto card device.
178 * @zc: Pointer to a crypto card device
180 * Register a crypto card device. Returns 0 if successful.
182 int zcrypt_card_register(struct zcrypt_card
*zc
)
186 spin_lock(&zcrypt_list_lock
);
187 list_add_tail(&zc
->list
, &zcrypt_card_list
);
188 spin_unlock(&zcrypt_list_lock
);
192 ZCRYPT_DBF_INFO("%s card=%02x register online=1\n",
193 __func__
, zc
->card
->id
);
195 rc
= sysfs_create_group(&zc
->card
->ap_dev
.device
.kobj
,
196 &zcrypt_card_attr_group
);
198 spin_lock(&zcrypt_list_lock
);
199 list_del_init(&zc
->list
);
200 spin_unlock(&zcrypt_list_lock
);
205 EXPORT_SYMBOL(zcrypt_card_register
);
208 * zcrypt_card_unregister(): Unregister a crypto card device.
209 * @zc: Pointer to crypto card device
211 * Unregister a crypto card device.
213 void zcrypt_card_unregister(struct zcrypt_card
*zc
)
215 ZCRYPT_DBF_INFO("%s card=%02x unregister\n",
216 __func__
, zc
->card
->id
);
218 spin_lock(&zcrypt_list_lock
);
219 list_del_init(&zc
->list
);
220 spin_unlock(&zcrypt_list_lock
);
221 sysfs_remove_group(&zc
->card
->ap_dev
.device
.kobj
,
222 &zcrypt_card_attr_group
);
225 EXPORT_SYMBOL(zcrypt_card_unregister
);