staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_card.c
blob40cd4c1c2de8db67267f664fd798dadbf740d2f3
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 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_RO(type);
51 static ssize_t 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 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_RW(online);
85 static ssize_t load_show(struct device *dev,
86 struct device_attribute *attr,
87 char *buf)
89 struct zcrypt_card *zc = to_ap_card(dev)->private;
91 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
94 static DEVICE_ATTR_RO(load);
96 static struct attribute *zcrypt_card_attrs[] = {
97 &dev_attr_type.attr,
98 &dev_attr_online.attr,
99 &dev_attr_load.attr,
100 NULL,
103 static const struct attribute_group zcrypt_card_attr_group = {
104 .attrs = zcrypt_card_attrs,
107 struct zcrypt_card *zcrypt_card_alloc(void)
109 struct zcrypt_card *zc;
111 zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL);
112 if (!zc)
113 return NULL;
114 INIT_LIST_HEAD(&zc->list);
115 INIT_LIST_HEAD(&zc->zqueues);
116 kref_init(&zc->refcount);
117 return zc;
119 EXPORT_SYMBOL(zcrypt_card_alloc);
121 void zcrypt_card_free(struct zcrypt_card *zc)
123 kfree(zc);
125 EXPORT_SYMBOL(zcrypt_card_free);
127 static void zcrypt_card_release(struct kref *kref)
129 struct zcrypt_card *zdev =
130 container_of(kref, struct zcrypt_card, refcount);
131 zcrypt_card_free(zdev);
134 void zcrypt_card_get(struct zcrypt_card *zc)
136 kref_get(&zc->refcount);
138 EXPORT_SYMBOL(zcrypt_card_get);
140 int zcrypt_card_put(struct zcrypt_card *zc)
142 return kref_put(&zc->refcount, zcrypt_card_release);
144 EXPORT_SYMBOL(zcrypt_card_put);
147 * zcrypt_card_register() - Register a crypto card device.
148 * @zc: Pointer to a crypto card device
150 * Register a crypto card device. Returns 0 if successful.
152 int zcrypt_card_register(struct zcrypt_card *zc)
154 int rc;
156 rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
157 &zcrypt_card_attr_group);
158 if (rc)
159 return rc;
161 spin_lock(&zcrypt_list_lock);
162 list_add_tail(&zc->list, &zcrypt_card_list);
163 spin_unlock(&zcrypt_list_lock);
165 zc->online = 1;
167 ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
169 return rc;
171 EXPORT_SYMBOL(zcrypt_card_register);
174 * zcrypt_card_unregister(): Unregister a crypto card device.
175 * @zc: Pointer to crypto card device
177 * Unregister a crypto card device.
179 void zcrypt_card_unregister(struct zcrypt_card *zc)
181 ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id);
183 spin_lock(&zcrypt_list_lock);
184 list_del_init(&zc->list);
185 spin_unlock(&zcrypt_list_lock);
186 sysfs_remove_group(&zc->card->ap_dev.device.kobj,
187 &zcrypt_card_attr_group);
189 EXPORT_SYMBOL(zcrypt_card_unregister);