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 card devices.
41 static ssize_t
zcrypt_card_type_show(struct device
*dev
,
42 struct device_attribute
*attr
, char *buf
)
44 struct zcrypt_card
*zc
= to_ap_card(dev
)->private;
46 return snprintf(buf
, PAGE_SIZE
, "%s\n", zc
->type_string
);
49 static DEVICE_ATTR(type
, 0444, zcrypt_card_type_show
, NULL
);
51 static ssize_t
zcrypt_card_online_show(struct device
*dev
,
52 struct device_attribute
*attr
,
55 struct zcrypt_card
*zc
= to_ap_card(dev
)->private;
57 return snprintf(buf
, PAGE_SIZE
, "%d\n", zc
->online
);
60 static ssize_t
zcrypt_card_online_store(struct device
*dev
,
61 struct device_attribute
*attr
,
62 const char *buf
, size_t count
)
64 struct zcrypt_card
*zc
= to_ap_card(dev
)->private;
65 struct zcrypt_queue
*zq
;
68 if (sscanf(buf
, "%d\n", &online
) != 1 || online
< 0 || online
> 1)
74 ZCRYPT_DBF(DBF_INFO
, "card=%02x online=%d\n", id
, online
);
76 spin_lock(&zcrypt_list_lock
);
77 list_for_each_entry(zq
, &zc
->zqueues
, list
)
78 zcrypt_queue_force_online(zq
, online
);
79 spin_unlock(&zcrypt_list_lock
);
83 static DEVICE_ATTR(online
, 0644, zcrypt_card_online_show
,
84 zcrypt_card_online_store
);
86 static struct attribute
*zcrypt_card_attrs
[] = {
88 &dev_attr_online
.attr
,
92 static const struct attribute_group zcrypt_card_attr_group
= {
93 .attrs
= zcrypt_card_attrs
,
96 struct zcrypt_card
*zcrypt_card_alloc(void)
98 struct zcrypt_card
*zc
;
100 zc
= kzalloc(sizeof(struct zcrypt_card
), GFP_KERNEL
);
103 INIT_LIST_HEAD(&zc
->list
);
104 INIT_LIST_HEAD(&zc
->zqueues
);
105 kref_init(&zc
->refcount
);
108 EXPORT_SYMBOL(zcrypt_card_alloc
);
110 void zcrypt_card_free(struct zcrypt_card
*zc
)
114 EXPORT_SYMBOL(zcrypt_card_free
);
116 static void zcrypt_card_release(struct kref
*kref
)
118 struct zcrypt_card
*zdev
=
119 container_of(kref
, struct zcrypt_card
, refcount
);
120 zcrypt_card_free(zdev
);
123 void zcrypt_card_get(struct zcrypt_card
*zc
)
125 kref_get(&zc
->refcount
);
127 EXPORT_SYMBOL(zcrypt_card_get
);
129 int zcrypt_card_put(struct zcrypt_card
*zc
)
131 return kref_put(&zc
->refcount
, zcrypt_card_release
);
133 EXPORT_SYMBOL(zcrypt_card_put
);
136 * zcrypt_card_register() - Register a crypto card device.
137 * @zc: Pointer to a crypto card device
139 * Register a crypto card device. Returns 0 if successful.
141 int zcrypt_card_register(struct zcrypt_card
*zc
)
145 rc
= sysfs_create_group(&zc
->card
->ap_dev
.device
.kobj
,
146 &zcrypt_card_attr_group
);
150 spin_lock(&zcrypt_list_lock
);
151 list_add_tail(&zc
->list
, &zcrypt_card_list
);
152 spin_unlock(&zcrypt_list_lock
);
156 ZCRYPT_DBF(DBF_INFO
, "card=%02x register online=1\n", zc
->card
->id
);
160 EXPORT_SYMBOL(zcrypt_card_register
);
163 * zcrypt_card_unregister(): Unregister a crypto card device.
164 * @zc: Pointer to crypto card device
166 * Unregister a crypto card device.
168 void zcrypt_card_unregister(struct zcrypt_card
*zc
)
170 ZCRYPT_DBF(DBF_INFO
, "card=%02x unregister\n", zc
->card
->id
);
172 spin_lock(&zcrypt_list_lock
);
173 list_del_init(&zc
->list
);
174 spin_unlock(&zcrypt_list_lock
);
175 sysfs_remove_group(&zc
->card
->ap_dev
.device
.kobj
,
176 &zcrypt_card_attr_group
);
178 EXPORT_SYMBOL(zcrypt_card_unregister
);