Linux 5.1.15
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_card.c
blobd4f35a183c157e899e3c505c40fd329aa60ab592
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 snprintf(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 zcrypt_card *zc = to_ap_card(dev)->private;
55 return snprintf(buf, PAGE_SIZE, "%d\n", zc->online);
58 static ssize_t online_store(struct device *dev,
59 struct device_attribute *attr,
60 const char *buf, size_t count)
62 struct zcrypt_card *zc = to_ap_card(dev)->private;
63 struct zcrypt_queue *zq;
64 int online, id;
66 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
67 return -EINVAL;
69 zc->online = online;
70 id = zc->card->id;
72 ZCRYPT_DBF(DBF_INFO, "card=%02x online=%d\n", id, online);
74 spin_lock(&zcrypt_list_lock);
75 list_for_each_entry(zq, &zc->zqueues, list)
76 zcrypt_queue_force_online(zq, online);
77 spin_unlock(&zcrypt_list_lock);
78 return count;
81 static DEVICE_ATTR_RW(online);
83 static ssize_t load_show(struct device *dev,
84 struct device_attribute *attr,
85 char *buf)
87 struct zcrypt_card *zc = to_ap_card(dev)->private;
89 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
92 static DEVICE_ATTR_RO(load);
94 static struct attribute *zcrypt_card_attrs[] = {
95 &dev_attr_type.attr,
96 &dev_attr_online.attr,
97 &dev_attr_load.attr,
98 NULL,
101 static const struct attribute_group zcrypt_card_attr_group = {
102 .attrs = zcrypt_card_attrs,
105 struct zcrypt_card *zcrypt_card_alloc(void)
107 struct zcrypt_card *zc;
109 zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL);
110 if (!zc)
111 return NULL;
112 INIT_LIST_HEAD(&zc->list);
113 INIT_LIST_HEAD(&zc->zqueues);
114 kref_init(&zc->refcount);
115 return zc;
117 EXPORT_SYMBOL(zcrypt_card_alloc);
119 void zcrypt_card_free(struct zcrypt_card *zc)
121 kfree(zc);
123 EXPORT_SYMBOL(zcrypt_card_free);
125 static void zcrypt_card_release(struct kref *kref)
127 struct zcrypt_card *zdev =
128 container_of(kref, struct zcrypt_card, refcount);
129 zcrypt_card_free(zdev);
132 void zcrypt_card_get(struct zcrypt_card *zc)
134 kref_get(&zc->refcount);
136 EXPORT_SYMBOL(zcrypt_card_get);
138 int zcrypt_card_put(struct zcrypt_card *zc)
140 return kref_put(&zc->refcount, zcrypt_card_release);
142 EXPORT_SYMBOL(zcrypt_card_put);
145 * zcrypt_card_register() - Register a crypto card device.
146 * @zc: Pointer to a crypto card device
148 * Register a crypto card device. Returns 0 if successful.
150 int zcrypt_card_register(struct zcrypt_card *zc)
152 int rc;
154 rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
155 &zcrypt_card_attr_group);
156 if (rc)
157 return rc;
159 spin_lock(&zcrypt_list_lock);
160 list_add_tail(&zc->list, &zcrypt_card_list);
161 spin_unlock(&zcrypt_list_lock);
163 zc->online = 1;
165 ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
167 return rc;
169 EXPORT_SYMBOL(zcrypt_card_register);
172 * zcrypt_card_unregister(): Unregister a crypto card device.
173 * @zc: Pointer to crypto card device
175 * Unregister a crypto card device.
177 void zcrypt_card_unregister(struct zcrypt_card *zc)
179 ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id);
181 spin_lock(&zcrypt_list_lock);
182 list_del_init(&zc->list);
183 spin_unlock(&zcrypt_list_lock);
184 sysfs_remove_group(&zc->card->ap_dev.device.kobj,
185 &zcrypt_card_attr_group);
187 EXPORT_SYMBOL(zcrypt_card_unregister);