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