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