summaryrefslogtreecommitdiffstats
path: root/drivers/char/hw_random/s390-trng.c
blob: 795853dfc46b70a0f38b4a803780971b8900fb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// SPDX-License-Identifier: GPL-2.0-only
/*
 * s390 TRNG device driver
 *
 * Driver for the TRNG (true random number generation) command
 * available via CPACF extension MSA 7 on the s390 arch.

 * Copyright IBM Corp. 2017
 * Author(s): Harald Freudenberger <freude@de.ibm.com>
 */

#define KMSG_COMPONENT "trng"
#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt

#include <linux/hw_random.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cpufeature.h>
#include <linux/miscdevice.h>
#include <linux/debugfs.h>
#include <linux/atomic.h>
#include <linux/random.h>
#include <linux/sched/signal.h>
#include <asm/debug.h>
#include <asm/cpacf.h>

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("IBM Corporation");
MODULE_DESCRIPTION("s390 CPACF TRNG device driver");


/* trng related debug feature things */

static debug_info_t *debug_info;

#define DEBUG_DBG(...)	debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
#define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
#define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
#define DEBUG_ERR(...)	debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)


/* trng helpers */

static atomic64_t trng_dev_counter = ATOMIC64_INIT(0);
static atomic64_t trng_hwrng_counter = ATOMIC64_INIT(0);


/* file io functions */

static int trng_open(struct inode *inode, struct file *file)
{
	return nonseekable_open(inode, file);
}

static ssize_t trng_read(struct file *file, char __user *ubuf,
			 size_t nbytes, loff_t *ppos)
{
	u8 buf[32];
	u8 *p = buf;
	unsigned int n;
	ssize_t ret = 0;

	/*
	 * use buf for requests <= sizeof(buf),
	 * otherwise allocate one page and fetch
	 * pagewise.
	 */

	if (nbytes > sizeof(buf)) {
		p = (u8 *) __get_free_page(GFP_KERNEL);
		if (!p)
			return -ENOMEM;
	}

	while (nbytes) {
		if (need_resched()) {
			if (signal_pending(current)) {
				if (ret == 0)
					ret = -ERESTARTSYS;
				break;
			}
			schedule();
		}
		n = nbytes > PAGE_SIZE ? PAGE_SIZE : nbytes;
		cpacf_trng(NULL, 0, p, n);
		atomic64_add(n, &trng_dev_counter);
		if (copy_to_user(ubuf, p, n)) {
			ret = -EFAULT;
			break;
		}
		nbytes -= n;
		ubuf += n;
		ret += n;
	}

	if (p != buf)
		free_page((unsigned long) p);

	DEBUG_DBG("trng_read()=%zd\n", ret);
	return ret;
}


/* sysfs */

static ssize_t trng_counter_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
{
	u64 dev_counter = atomic64_read(&trng_dev_counter);
	u64 hwrng_counter = atomic64_read(&trng_hwrng_counter);
	u64 arch_counter = atomic64_read(&s390_arch_random_counter);

	return sysfs_emit(buf,
			"trng:  %llu\n"
			"hwrng: %llu\n"
			"arch:  %llu\n"
			"total: %llu\n",
			dev_counter, hwrng_counter, arch_counter,
			dev_counter + hwrng_counter + arch_counter);
}
static DEVICE_ATTR(byte_counter, 0444, trng_counter_show, NULL);

static struct attribute *trng_dev_attrs[] = {
	&dev_attr_byte_counter.attr,
	NULL
};

static const struct attribute_group trng_dev_attr_group = {
	.attrs = trng_dev_attrs
};

static const struct attribute_group *trng_dev_attr_groups[] = {
	&trng_dev_attr_group,
	NULL
};

static const struct file_operations trng_fops = {
	.owner		= THIS_MODULE,
	.open		= &trng_open,
	.release	= NULL,
	.read		= &trng_read,
	.llseek		= noop_llseek,
};

static struct miscdevice trng_dev = {
	.name	= "trng",
	.minor	= MISC_DYNAMIC_MINOR,
	.mode	= 0444,
	.fops	= &trng_fops,
	.groups = trng_dev_attr_groups,
};


/* hwrng_register */

static inline void _trng_hwrng_read(u8 *buf, size_t len)
{
	cpacf_trng(NULL, 0, buf, len);
	atomic64_add(len, &trng_hwrng_counter);
}

static int trng_hwrng_data_read(struct hwrng *rng, u32 *data)
{
	size_t len = sizeof(*data);

	_trng_hwrng_read((u8 *) data, len);

	DEBUG_DBG("trng_hwrng_data_read()=%zu\n", len);

	return len;
}

static int trng_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
{
	size_t len = max <= PAGE_SIZE ? max : PAGE_SIZE;

	_trng_hwrng_read((u8 *) data, len);

	DEBUG_DBG("trng_hwrng_read()=%zu\n", len);

	return len;
}

/*
 * hwrng register struct
 * The trng is supposed to have 100% entropy, and thus we register with a very
 * high quality value. If we ever have a better driver in the future, we should
 * change this value again when we merge this driver.
 */
static struct hwrng trng_hwrng_dev = {
	.name		= "s390-trng",
	.data_read	= trng_hwrng_data_read,
	.read		= trng_hwrng_read,
	.quality	= 1024,
};


/* init and exit */

static void __init trng_debug_init(void)
{
	debug_info = debug_register("trng", 1, 1, 4 * sizeof(long));
	debug_register_view(debug_info, &debug_sprintf_view);
	debug_set_level(debug_info, 3);
}

static void trng_debug_exit(void)
{
	debug_unregister(debug_info);
}

static int __init trng_init(void)
{
	int ret;

	trng_debug_init();

	/* check if subfunction CPACF_PRNO_TRNG is available */
	if (!cpacf_query_func(CPACF_PRNO, CPACF_PRNO_TRNG)) {
		DEBUG_INFO("trng_init CPACF_PRNO_TRNG not available\n");
		ret = -ENODEV;
		goto out_dbg;
	}

	ret = misc_register(&trng_dev);
	if (ret) {
		DEBUG_WARN("trng_init misc_register() failed rc=%d\n", ret);
		goto out_dbg;
	}

	ret = hwrng_register(&trng_hwrng_dev);
	if (ret) {
		DEBUG_WARN("trng_init hwrng_register() failed rc=%d\n", ret);
		goto out_misc;
	}

	DEBUG_DBG("trng_init successful\n");

	return 0;

out_misc:
	misc_deregister(&trng_dev);
out_dbg:
	trng_debug_exit();
	return ret;
}

static void __exit trng_exit(void)
{
	hwrng_unregister(&trng_hwrng_dev);
	misc_deregister(&trng_dev);
	trng_debug_exit();
}

module_cpu_feature_match(S390_CPU_FEATURE_MSA, trng_init);
module_exit(trng_exit);