Linux 4.16.11
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_card.c
blob233e1e695208b9b870edb4259127c41e2ee3357a
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * zcrypt 2.1.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>
20 #include <linux/fs.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,
53 char *buf)
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;
66 int online, id;
68 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
69 return -EINVAL;
71 zc->online = online;
72 id = zc->card->id;
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);
80 return count;
83 static DEVICE_ATTR(online, 0644, zcrypt_card_online_show,
84 zcrypt_card_online_store);
86 static struct attribute *zcrypt_card_attrs[] = {
87 &dev_attr_type.attr,
88 &dev_attr_online.attr,
89 NULL,
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);
101 if (!zc)
102 return NULL;
103 INIT_LIST_HEAD(&zc->list);
104 INIT_LIST_HEAD(&zc->zqueues);
105 kref_init(&zc->refcount);
106 return zc;
108 EXPORT_SYMBOL(zcrypt_card_alloc);
110 void zcrypt_card_free(struct zcrypt_card *zc)
112 kfree(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)
143 int rc;
145 rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
146 &zcrypt_card_attr_group);
147 if (rc)
148 return rc;
150 spin_lock(&zcrypt_list_lock);
151 list_add_tail(&zc->list, &zcrypt_card_list);
152 spin_unlock(&zcrypt_list_lock);
154 zc->online = 1;
156 ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
158 return rc;
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);