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
= to_ap_card(dev
)->private;
44 return scnprintf(buf
, PAGE_SIZE
, "%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 ap_card
*ac
= to_ap_card(dev
);
54 struct zcrypt_card
*zc
= ac
->private;
55 int online
= ac
->config
&& zc
->online
? 1 : 0;
57 return scnprintf(buf
, PAGE_SIZE
, "%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 ap_card
*ac
= to_ap_card(dev
);
65 struct zcrypt_card
*zc
= ac
->private;
66 struct zcrypt_queue
*zq
;
69 if (sscanf(buf
, "%d\n", &online
) != 1 || online
< 0 || online
> 1)
72 if (online
&& !ac
->config
)
78 ZCRYPT_DBF(DBF_INFO
, "card=%02x online=%d\n", id
, online
);
80 spin_lock(&zcrypt_list_lock
);
81 list_for_each_entry(zq
, &zc
->zqueues
, list
)
82 zcrypt_queue_force_online(zq
, online
);
83 spin_unlock(&zcrypt_list_lock
);
87 static DEVICE_ATTR_RW(online
);
89 static ssize_t
load_show(struct device
*dev
,
90 struct device_attribute
*attr
,
93 struct zcrypt_card
*zc
= to_ap_card(dev
)->private;
95 return scnprintf(buf
, PAGE_SIZE
, "%d\n", atomic_read(&zc
->load
));
98 static DEVICE_ATTR_RO(load
);
100 static struct attribute
*zcrypt_card_attrs
[] = {
102 &dev_attr_online
.attr
,
107 static const struct attribute_group zcrypt_card_attr_group
= {
108 .attrs
= zcrypt_card_attrs
,
111 struct zcrypt_card
*zcrypt_card_alloc(void)
113 struct zcrypt_card
*zc
;
115 zc
= kzalloc(sizeof(struct zcrypt_card
), GFP_KERNEL
);
118 INIT_LIST_HEAD(&zc
->list
);
119 INIT_LIST_HEAD(&zc
->zqueues
);
120 kref_init(&zc
->refcount
);
123 EXPORT_SYMBOL(zcrypt_card_alloc
);
125 void zcrypt_card_free(struct zcrypt_card
*zc
)
129 EXPORT_SYMBOL(zcrypt_card_free
);
131 static void zcrypt_card_release(struct kref
*kref
)
133 struct zcrypt_card
*zdev
=
134 container_of(kref
, struct zcrypt_card
, refcount
);
135 zcrypt_card_free(zdev
);
138 void zcrypt_card_get(struct zcrypt_card
*zc
)
140 kref_get(&zc
->refcount
);
142 EXPORT_SYMBOL(zcrypt_card_get
);
144 int zcrypt_card_put(struct zcrypt_card
*zc
)
146 return kref_put(&zc
->refcount
, zcrypt_card_release
);
148 EXPORT_SYMBOL(zcrypt_card_put
);
151 * zcrypt_card_register() - Register a crypto card device.
152 * @zc: Pointer to a crypto card device
154 * Register a crypto card device. Returns 0 if successful.
156 int zcrypt_card_register(struct zcrypt_card
*zc
)
160 spin_lock(&zcrypt_list_lock
);
161 list_add_tail(&zc
->list
, &zcrypt_card_list
);
162 spin_unlock(&zcrypt_list_lock
);
166 ZCRYPT_DBF(DBF_INFO
, "card=%02x register online=1\n", zc
->card
->id
);
168 rc
= sysfs_create_group(&zc
->card
->ap_dev
.device
.kobj
,
169 &zcrypt_card_attr_group
);
171 spin_lock(&zcrypt_list_lock
);
172 list_del_init(&zc
->list
);
173 spin_unlock(&zcrypt_list_lock
);
178 EXPORT_SYMBOL(zcrypt_card_register
);
181 * zcrypt_card_unregister(): Unregister a crypto card device.
182 * @zc: Pointer to crypto card device
184 * Unregister a crypto card device.
186 void zcrypt_card_unregister(struct zcrypt_card
*zc
)
188 ZCRYPT_DBF(DBF_INFO
, "card=%02x unregister\n", zc
->card
->id
);
190 spin_lock(&zcrypt_list_lock
);
191 list_del_init(&zc
->list
);
192 spin_unlock(&zcrypt_list_lock
);
193 sysfs_remove_group(&zc
->card
->ap_dev
.device
.kobj
,
194 &zcrypt_card_attr_group
);
196 EXPORT_SYMBOL(zcrypt_card_unregister
);