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>
7  *
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>
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  * Device attributes common for all crypto card devices.
37  */
38 
39 static 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 
47 static DEVICE_ATTR_RO(type);
48 
49 static ssize_t online_show(struct device *dev,
50 			   struct device_attribute *attr,
51 			   char *buf)
52 {
53 	struct zcrypt_card *zc = to_ap_card(dev)->private;
54 
55 	return scnprintf(buf, PAGE_SIZE, "%d\n", zc->online);
56 }
57 
58 static ssize_t online_store(struct device *dev,
59 			    struct device_attribute *attr,
60 			    const char *buf, size_t count)
61 {
62 	struct zcrypt_card *zc = to_ap_card(dev)->private;
63 	struct zcrypt_queue *zq;
64 	int online, id;
65 
66 	if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
67 		return -EINVAL;
68 
69 	zc->online = online;
70 	id = zc->card->id;
71 
72 	ZCRYPT_DBF(DBF_INFO, "card=%02x online=%d\n", id, online);
73 
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;
79 }
80 
81 static DEVICE_ATTR_RW(online);
82 
83 static ssize_t load_show(struct device *dev,
84 			 struct device_attribute *attr,
85 			 char *buf)
86 {
87 	struct zcrypt_card *zc = to_ap_card(dev)->private;
88 
89 	return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
90 }
91 
92 static DEVICE_ATTR_RO(load);
93 
94 static struct attribute *zcrypt_card_attrs[] = {
95 	&dev_attr_type.attr,
96 	&dev_attr_online.attr,
97 	&dev_attr_load.attr,
98 	NULL,
99 };
100 
101 static const struct attribute_group zcrypt_card_attr_group = {
102 	.attrs = zcrypt_card_attrs,
103 };
104 
105 struct zcrypt_card *zcrypt_card_alloc(void)
106 {
107 	struct zcrypt_card *zc;
108 
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;
116 }
117 EXPORT_SYMBOL(zcrypt_card_alloc);
118 
119 void zcrypt_card_free(struct zcrypt_card *zc)
120 {
121 	kfree(zc);
122 }
123 EXPORT_SYMBOL(zcrypt_card_free);
124 
125 static void zcrypt_card_release(struct kref *kref)
126 {
127 	struct zcrypt_card *zdev =
128 		container_of(kref, struct zcrypt_card, refcount);
129 	zcrypt_card_free(zdev);
130 }
131 
132 void zcrypt_card_get(struct zcrypt_card *zc)
133 {
134 	kref_get(&zc->refcount);
135 }
136 EXPORT_SYMBOL(zcrypt_card_get);
137 
138 int zcrypt_card_put(struct zcrypt_card *zc)
139 {
140 	return kref_put(&zc->refcount, zcrypt_card_release);
141 }
142 EXPORT_SYMBOL(zcrypt_card_put);
143 
144 /**
145  * zcrypt_card_register() - Register a crypto card device.
146  * @zc: Pointer to a crypto card device
147  *
148  * Register a crypto card device. Returns 0 if successful.
149  */
150 int zcrypt_card_register(struct zcrypt_card *zc)
151 {
152 	int rc;
153 
154 	rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
155 				&zcrypt_card_attr_group);
156 	if (rc)
157 		return rc;
158 
159 	spin_lock(&zcrypt_list_lock);
160 	list_add_tail(&zc->list, &zcrypt_card_list);
161 	spin_unlock(&zcrypt_list_lock);
162 
163 	zc->online = 1;
164 
165 	ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
166 
167 	return rc;
168 }
169 EXPORT_SYMBOL(zcrypt_card_register);
170 
171 /**
172  * zcrypt_card_unregister(): Unregister a crypto card device.
173  * @zc: Pointer to crypto card device
174  *
175  * Unregister a crypto card device.
176  */
177 void zcrypt_card_unregister(struct zcrypt_card *zc)
178 {
179 	ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id);
180 
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);
186 }
187 EXPORT_SYMBOL(zcrypt_card_unregister);
188