1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
7 *
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>
12 */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
17 #include <linux/miscdevice.h>
18 #include <linux/fs.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>
28
29 #include "zcrypt_debug.h"
30 #include "zcrypt_api.h"
31
32 #include "zcrypt_msgtype6.h"
33 #include "zcrypt_msgtype50.h"
34
35 /*
36 * Device attributes common for all crypto card devices.
37 */
38
type_show(struct device * dev,struct device_attribute * attr,char * buf)39 static ssize_t type_show(struct device *dev,
40 struct device_attribute *attr, char *buf)
41 {
42 struct zcrypt_card *zc = to_ap_card(dev)->private;
43
44 return scnprintf(buf, PAGE_SIZE, "%s\n", zc->type_string);
45 }
46
47 static DEVICE_ATTR_RO(type);
48
online_show(struct device * dev,struct device_attribute * attr,char * buf)49 static ssize_t online_show(struct device *dev,
50 struct device_attribute *attr,
51 char *buf)
52 {
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;
56
57 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
58 }
59
online_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)60 static ssize_t online_store(struct device *dev,
61 struct device_attribute *attr,
62 const char *buf, size_t count)
63 {
64 struct ap_card *ac = to_ap_card(dev);
65 struct zcrypt_card *zc = ac->private;
66 struct zcrypt_queue *zq;
67 int online, id;
68
69 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
70 return -EINVAL;
71
72 if (online && !ac->config)
73 return -ENODEV;
74
75 zc->online = online;
76 id = zc->card->id;
77
78 ZCRYPT_DBF(DBF_INFO, "card=%02x online=%d\n", id, online);
79
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);
84 return count;
85 }
86
87 static DEVICE_ATTR_RW(online);
88
load_show(struct device * dev,struct device_attribute * attr,char * buf)89 static ssize_t load_show(struct device *dev,
90 struct device_attribute *attr,
91 char *buf)
92 {
93 struct zcrypt_card *zc = to_ap_card(dev)->private;
94
95 return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
96 }
97
98 static DEVICE_ATTR_RO(load);
99
100 static struct attribute *zcrypt_card_attrs[] = {
101 &dev_attr_type.attr,
102 &dev_attr_online.attr,
103 &dev_attr_load.attr,
104 NULL,
105 };
106
107 static const struct attribute_group zcrypt_card_attr_group = {
108 .attrs = zcrypt_card_attrs,
109 };
110
zcrypt_card_alloc(void)111 struct zcrypt_card *zcrypt_card_alloc(void)
112 {
113 struct zcrypt_card *zc;
114
115 zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL);
116 if (!zc)
117 return NULL;
118 INIT_LIST_HEAD(&zc->list);
119 INIT_LIST_HEAD(&zc->zqueues);
120 kref_init(&zc->refcount);
121 return zc;
122 }
123 EXPORT_SYMBOL(zcrypt_card_alloc);
124
zcrypt_card_free(struct zcrypt_card * zc)125 void zcrypt_card_free(struct zcrypt_card *zc)
126 {
127 kfree(zc);
128 }
129 EXPORT_SYMBOL(zcrypt_card_free);
130
zcrypt_card_release(struct kref * kref)131 static void zcrypt_card_release(struct kref *kref)
132 {
133 struct zcrypt_card *zdev =
134 container_of(kref, struct zcrypt_card, refcount);
135 zcrypt_card_free(zdev);
136 }
137
zcrypt_card_get(struct zcrypt_card * zc)138 void zcrypt_card_get(struct zcrypt_card *zc)
139 {
140 kref_get(&zc->refcount);
141 }
142 EXPORT_SYMBOL(zcrypt_card_get);
143
zcrypt_card_put(struct zcrypt_card * zc)144 int zcrypt_card_put(struct zcrypt_card *zc)
145 {
146 return kref_put(&zc->refcount, zcrypt_card_release);
147 }
148 EXPORT_SYMBOL(zcrypt_card_put);
149
150 /**
151 * zcrypt_card_register() - Register a crypto card device.
152 * @zc: Pointer to a crypto card device
153 *
154 * Register a crypto card device. Returns 0 if successful.
155 */
zcrypt_card_register(struct zcrypt_card * zc)156 int zcrypt_card_register(struct zcrypt_card *zc)
157 {
158 int rc;
159
160 spin_lock(&zcrypt_list_lock);
161 list_add_tail(&zc->list, &zcrypt_card_list);
162 spin_unlock(&zcrypt_list_lock);
163
164 zc->online = 1;
165
166 ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
167
168 rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
169 &zcrypt_card_attr_group);
170 if (rc) {
171 spin_lock(&zcrypt_list_lock);
172 list_del_init(&zc->list);
173 spin_unlock(&zcrypt_list_lock);
174 }
175
176 return rc;
177 }
178 EXPORT_SYMBOL(zcrypt_card_register);
179
180 /**
181 * zcrypt_card_unregister(): Unregister a crypto card device.
182 * @zc: Pointer to crypto card device
183 *
184 * Unregister a crypto card device.
185 */
zcrypt_card_unregister(struct zcrypt_card * zc)186 void zcrypt_card_unregister(struct zcrypt_card *zc)
187 {
188 ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id);
189
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);
195 zcrypt_card_put(zc);
196 }
197 EXPORT_SYMBOL(zcrypt_card_unregister);
198