File Index
Files and directories
Sign in
FILE

s390-trng.c

File preview with syntax highlighting.

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