1
2
3
4
5
6
7
8
9
10
11
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
37
38
39static 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
47static DEVICE_ATTR_RO(type);
48
49static 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
60static 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, i = 0, maxzqs = 0;
68 struct zcrypt_queue **zq_uelist = NULL;
69
70 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
71 return -EINVAL;
72
73 if (online && !ac->config)
74 return -ENODEV;
75
76 zc->online = online;
77 id = zc->card->id;
78
79 ZCRYPT_DBF(DBF_INFO, "card=%02x online=%d\n", id, online);
80
81 ap_send_online_uevent(&ac->ap_dev, online);
82
83 spin_lock(&zcrypt_list_lock);
84
85
86
87
88
89
90 list_for_each_entry(zq, &zc->zqueues, list)
91 maxzqs++;
92 if (maxzqs > 0)
93 zq_uelist = kcalloc(maxzqs + 1, sizeof(zq), GFP_ATOMIC);
94 list_for_each_entry(zq, &zc->zqueues, list)
95 if (zcrypt_queue_force_online(zq, online))
96 if (zq_uelist) {
97 zcrypt_queue_get(zq);
98 zq_uelist[i++] = zq;
99 }
100 spin_unlock(&zcrypt_list_lock);
101 if (zq_uelist) {
102 for (i = 0; zq_uelist[i]; i++) {
103 zq = zq_uelist[i];
104 ap_send_online_uevent(&zq->queue->ap_dev, online);
105 zcrypt_queue_put(zq);
106 }
107 kfree(zq_uelist);
108 }
109
110 return count;
111}
112
113static DEVICE_ATTR_RW(online);
114
115static ssize_t load_show(struct device *dev,
116 struct device_attribute *attr,
117 char *buf)
118{
119 struct zcrypt_card *zc = to_ap_card(dev)->private;
120
121 return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
122}
123
124static DEVICE_ATTR_RO(load);
125
126static struct attribute *zcrypt_card_attrs[] = {
127 &dev_attr_type.attr,
128 &dev_attr_online.attr,
129 &dev_attr_load.attr,
130 NULL,
131};
132
133static const struct attribute_group zcrypt_card_attr_group = {
134 .attrs = zcrypt_card_attrs,
135};
136
137struct zcrypt_card *zcrypt_card_alloc(void)
138{
139 struct zcrypt_card *zc;
140
141 zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL);
142 if (!zc)
143 return NULL;
144 INIT_LIST_HEAD(&zc->list);
145 INIT_LIST_HEAD(&zc->zqueues);
146 kref_init(&zc->refcount);
147 return zc;
148}
149EXPORT_SYMBOL(zcrypt_card_alloc);
150
151void zcrypt_card_free(struct zcrypt_card *zc)
152{
153 kfree(zc);
154}
155EXPORT_SYMBOL(zcrypt_card_free);
156
157static void zcrypt_card_release(struct kref *kref)
158{
159 struct zcrypt_card *zdev =
160 container_of(kref, struct zcrypt_card, refcount);
161 zcrypt_card_free(zdev);
162}
163
164void zcrypt_card_get(struct zcrypt_card *zc)
165{
166 kref_get(&zc->refcount);
167}
168EXPORT_SYMBOL(zcrypt_card_get);
169
170int zcrypt_card_put(struct zcrypt_card *zc)
171{
172 return kref_put(&zc->refcount, zcrypt_card_release);
173}
174EXPORT_SYMBOL(zcrypt_card_put);
175
176
177
178
179
180
181
182int zcrypt_card_register(struct zcrypt_card *zc)
183{
184 int rc;
185
186 spin_lock(&zcrypt_list_lock);
187 list_add_tail(&zc->list, &zcrypt_card_list);
188 spin_unlock(&zcrypt_list_lock);
189
190 zc->online = 1;
191
192 ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
193
194 rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
195 &zcrypt_card_attr_group);
196 if (rc) {
197 spin_lock(&zcrypt_list_lock);
198 list_del_init(&zc->list);
199 spin_unlock(&zcrypt_list_lock);
200 }
201
202 return rc;
203}
204EXPORT_SYMBOL(zcrypt_card_register);
205
206
207
208
209
210
211
212void zcrypt_card_unregister(struct zcrypt_card *zc)
213{
214 ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id);
215
216 spin_lock(&zcrypt_list_lock);
217 list_del_init(&zc->list);
218 spin_unlock(&zcrypt_list_lock);
219 sysfs_remove_group(&zc->card->ap_dev.device.kobj,
220 &zcrypt_card_attr_group);
221 zcrypt_card_put(zc);
222}
223EXPORT_SYMBOL(zcrypt_card_unregister);
224