1 /*
2  * s390 TRNG device driver
3  *
4  * Driver for the TRNG (true random number generation) command
5  * available via CPACF extension MSA 7 on the s390 arch.
6 
7  * Copyright IBM Corp. 2017
8  * Author(s): Harald Freudenberger <freude@de.ibm.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License (version 2 only)
12  * as published by the Free Software Foundation.
13  *
14  */
15 
16 #define KMSG_COMPONENT "trng"
17 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 
19 #include <linux/hw_random.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/cpufeature.h>
23 #include <linux/miscdevice.h>
24 #include <linux/debugfs.h>
25 #include <linux/atomic.h>
26 #include <linux/random.h>
27 #include <linux/sched/signal.h>
28 #include <asm/debug.h>
29 #include <asm/cpacf.h>
30 
31 MODULE_LICENSE("GPL v2");
32 MODULE_AUTHOR("IBM Corporation");
33 MODULE_DESCRIPTION("s390 CPACF TRNG device driver");
34 
35 
36 /* trng related debug feature things */
37 
38 static debug_info_t *debug_info;
39 
40 #define DEBUG_DBG(...)	debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
41 #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
42 #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
43 #define DEBUG_ERR(...)	debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
44 
45 
46 /* trng helpers */
47 
48 static atomic64_t trng_dev_counter = ATOMIC64_INIT(0);
49 static atomic64_t trng_hwrng_counter = ATOMIC64_INIT(0);
50 
51 
52 /* file io functions */
53 
54 static int trng_open(struct inode *inode, struct file *file)
55 {
56 	return nonseekable_open(inode, file);
57 }
58 
59 static ssize_t trng_read(struct file *file, char __user *ubuf,
60 			 size_t nbytes, loff_t *ppos)
61 {
62 	u8 buf[32];
63 	u8 *p = buf;
64 	unsigned int n;
65 	ssize_t ret = 0;
66 
67 	/*
68 	 * use buf for requests <= sizeof(buf),
69 	 * otherwise allocate one page and fetch
70 	 * pagewise.
71 	 */
72 
73 	if (nbytes > sizeof(buf)) {
74 		p = (u8 *) __get_free_page(GFP_KERNEL);
75 		if (!p)
76 			return -ENOMEM;
77 	}
78 
79 	while (nbytes) {
80 		if (need_resched()) {
81 			if (signal_pending(current)) {
82 				if (ret == 0)
83 					ret = -ERESTARTSYS;
84 				break;
85 			}
86 			schedule();
87 		}
88 		n = nbytes > PAGE_SIZE ? PAGE_SIZE : nbytes;
89 		cpacf_trng(NULL, 0, p, n);
90 		atomic64_add(n, &trng_dev_counter);
91 		if (copy_to_user(ubuf, p, n)) {
92 			ret = -EFAULT;
93 			break;
94 		}
95 		nbytes -= n;
96 		ubuf += n;
97 		ret += n;
98 	}
99 
100 	if (p != buf)
101 		free_page((unsigned long) p);
102 
103 	DEBUG_DBG("trng_read()=%zd\n", ret);
104 	return ret;
105 }
106 
107 
108 /* sysfs */
109 
110 static ssize_t trng_counter_show(struct device *dev,
111 				 struct device_attribute *attr, char *buf)
112 {
113 	u64 dev_counter = atomic64_read(&trng_dev_counter);
114 	u64 hwrng_counter = atomic64_read(&trng_hwrng_counter);
115 #if IS_ENABLED(CONFIG_ARCH_RANDOM)
116 	u64 arch_counter = atomic64_read(&s390_arch_random_counter);
117 
118 	return snprintf(buf, PAGE_SIZE,
119 			"trng:  %llu\n"
120 			"hwrng: %llu\n"
121 			"arch:  %llu\n"
122 			"total: %llu\n",
123 			dev_counter, hwrng_counter, arch_counter,
124 			dev_counter + hwrng_counter + arch_counter);
125 #else
126 	return snprintf(buf, PAGE_SIZE,
127 			"trng:  %llu\n"
128 			"hwrng: %llu\n"
129 			"total: %llu\n",
130 			dev_counter, hwrng_counter,
131 			dev_counter + hwrng_counter);
132 #endif
133 }
134 static DEVICE_ATTR(byte_counter, 0444, trng_counter_show, NULL);
135 
136 static struct attribute *trng_dev_attrs[] = {
137 	&dev_attr_byte_counter.attr,
138 	NULL
139 };
140 
141 static const struct attribute_group trng_dev_attr_group = {
142 	.attrs = trng_dev_attrs
143 };
144 
145 static const struct attribute_group *trng_dev_attr_groups[] = {
146 	&trng_dev_attr_group,
147 	NULL
148 };
149 
150 static const struct file_operations trng_fops = {
151 	.owner		= THIS_MODULE,
152 	.open		= &trng_open,
153 	.release	= NULL,
154 	.read		= &trng_read,
155 	.llseek		= noop_llseek,
156 };
157 
158 static struct miscdevice trng_dev = {
159 	.name	= "trng",
160 	.minor	= MISC_DYNAMIC_MINOR,
161 	.mode	= 0444,
162 	.fops	= &trng_fops,
163 	.groups = trng_dev_attr_groups,
164 };
165 
166 
167 /* hwrng_register */
168 
169 static inline void _trng_hwrng_read(u8 *buf, size_t len)
170 {
171 	cpacf_trng(NULL, 0, buf, len);
172 	atomic64_add(len, &trng_hwrng_counter);
173 }
174 
175 static int trng_hwrng_data_read(struct hwrng *rng, u32 *data)
176 {
177 	size_t len = sizeof(*data);
178 
179 	_trng_hwrng_read((u8 *) data, len);
180 
181 	DEBUG_DBG("trng_hwrng_data_read()=%zu\n", len);
182 
183 	return len;
184 }
185 
186 static int trng_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
187 {
188 	size_t len = max <= PAGE_SIZE ? max : PAGE_SIZE;
189 
190 	_trng_hwrng_read((u8 *) data, len);
191 
192 	DEBUG_DBG("trng_hwrng_read()=%zu\n", len);
193 
194 	return len;
195 }
196 
197 /*
198  * hwrng register struct
199  * The trng is suppost to have 100% entropy, and thus
200  * we register with a very high quality value.
201  */
202 static struct hwrng trng_hwrng_dev = {
203 	.name		= "s390-trng",
204 	.data_read	= trng_hwrng_data_read,
205 	.read		= trng_hwrng_read,
206 	.quality	= 999,
207 };
208 
209 
210 /* init and exit */
211 
212 static void __init trng_debug_init(void)
213 {
214 	debug_info = debug_register("trng", 1, 1, 4 * sizeof(long));
215 	debug_register_view(debug_info, &debug_sprintf_view);
216 	debug_set_level(debug_info, 3);
217 }
218 
219 static void trng_debug_exit(void)
220 {
221 	debug_unregister(debug_info);
222 }
223 
224 static int __init trng_init(void)
225 {
226 	int ret;
227 
228 	trng_debug_init();
229 
230 	/* check if subfunction CPACF_PRNO_TRNG is available */
231 	if (!cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG)) {
232 		DEBUG_INFO("trng_init CPACF_PRNO_TRNG not available\n");
233 		ret = -ENODEV;
234 		goto out_dbg;
235 	}
236 
237 	ret = misc_register(&trng_dev);
238 	if (ret) {
239 		DEBUG_WARN("trng_init misc_register() failed rc=%d\n", ret);
240 		goto out_dbg;
241 	}
242 
243 	ret = hwrng_register(&trng_hwrng_dev);
244 	if (ret) {
245 		DEBUG_WARN("trng_init hwrng_register() failed rc=%d\n", ret);
246 		goto out_misc;
247 	}
248 
249 	DEBUG_DBG("trng_init successful\n");
250 
251 	return 0;
252 
253 out_misc:
254 	misc_deregister(&trng_dev);
255 out_dbg:
256 	trng_debug_exit();
257 	return ret;
258 }
259 
260 static void __exit trng_exit(void)
261 {
262 	hwrng_unregister(&trng_hwrng_dev);
263 	misc_deregister(&trng_dev);
264 	trng_debug_exit();
265 }
266 
267 module_cpu_feature_match(MSA, trng_init);
268 module_exit(trng_exit);
269