WIP FPC-III support
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_card.c
blob33b23884b133fc1d12ebc7f46b33f3bb01277f47
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>
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>
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>
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,
51 char *buf)
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;
67 int online, id;
69 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
70 return -EINVAL;
72 if (online && !ac->config)
73 return -ENODEV;
75 zc->online = online;
76 id = zc->card->id;
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);
84 return count;
87 static DEVICE_ATTR_RW(online);
89 static ssize_t load_show(struct device *dev,
90 struct device_attribute *attr,
91 char *buf)
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[] = {
101 &dev_attr_type.attr,
102 &dev_attr_online.attr,
103 &dev_attr_load.attr,
104 NULL,
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);
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;
123 EXPORT_SYMBOL(zcrypt_card_alloc);
125 void zcrypt_card_free(struct zcrypt_card *zc)
127 kfree(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)
158 int rc;
160 spin_lock(&zcrypt_list_lock);
161 list_add_tail(&zc->list, &zcrypt_card_list);
162 spin_unlock(&zcrypt_list_lock);
164 zc->online = 1;
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);
170 if (rc) {
171 spin_lock(&zcrypt_list_lock);
172 list_del_init(&zc->list);
173 spin_unlock(&zcrypt_list_lock);
176 return rc;
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);