1 // SPDX-License-Identifier: GPL-2.0-only
3 * s390 TRNG device driver
5 * Driver for the TRNG (true random number generation) command
6 * available via CPACF extension MSA 7 on the s390 arch.
8 * Copyright IBM Corp. 2017
9 * Author(s): Harald Freudenberger <freude@de.ibm.com>
12 #define KMSG_COMPONENT "trng"
13 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
15 #include <linux/hw_random.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/cpufeature.h>
19 #include <linux/miscdevice.h>
20 #include <linux/debugfs.h>
21 #include <linux/atomic.h>
22 #include <linux/random.h>
23 #include <linux/sched/signal.h>
24 #include <asm/debug.h>
25 #include <asm/cpacf.h>
27 MODULE_LICENSE("GPL v2");
28 MODULE_AUTHOR("IBM Corporation");
29 MODULE_DESCRIPTION("s390 CPACF TRNG device driver");
32 /* trng related debug feature things */
34 static debug_info_t
*debug_info
;
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__)
44 static atomic64_t trng_dev_counter
= ATOMIC64_INIT(0);
45 static atomic64_t trng_hwrng_counter
= ATOMIC64_INIT(0);
48 /* file io functions */
50 static int trng_open(struct inode
*inode
, struct file
*file
)
52 return nonseekable_open(inode
, file
);
55 static ssize_t
trng_read(struct file
*file
, char __user
*ubuf
,
56 size_t nbytes
, loff_t
*ppos
)
64 * use buf for requests <= sizeof(buf),
65 * otherwise allocate one page and fetch
69 if (nbytes
> sizeof(buf
)) {
70 p
= (u8
*) __get_free_page(GFP_KERNEL
);
77 if (signal_pending(current
)) {
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
)) {
97 free_page((unsigned long) p
);
99 DEBUG_DBG("trng_read()=%zd\n", ret
);
106 static ssize_t
trng_counter_show(struct device
*dev
,
107 struct device_attribute
*attr
, char *buf
)
109 u64 dev_counter
= atomic64_read(&trng_dev_counter
);
110 u64 hwrng_counter
= atomic64_read(&trng_hwrng_counter
);
111 #if IS_ENABLED(CONFIG_ARCH_RANDOM)
112 u64 arch_counter
= atomic64_read(&s390_arch_random_counter
);
114 return snprintf(buf
, PAGE_SIZE
,
119 dev_counter
, hwrng_counter
, arch_counter
,
120 dev_counter
+ hwrng_counter
+ arch_counter
);
122 return snprintf(buf
, PAGE_SIZE
,
126 dev_counter
, hwrng_counter
,
127 dev_counter
+ hwrng_counter
);
130 static DEVICE_ATTR(byte_counter
, 0444, trng_counter_show
, NULL
);
132 static struct attribute
*trng_dev_attrs
[] = {
133 &dev_attr_byte_counter
.attr
,
137 static const struct attribute_group trng_dev_attr_group
= {
138 .attrs
= trng_dev_attrs
141 static const struct attribute_group
*trng_dev_attr_groups
[] = {
142 &trng_dev_attr_group
,
146 static const struct file_operations trng_fops
= {
147 .owner
= THIS_MODULE
,
151 .llseek
= noop_llseek
,
154 static struct miscdevice trng_dev
= {
156 .minor
= MISC_DYNAMIC_MINOR
,
159 .groups
= trng_dev_attr_groups
,
165 static inline void _trng_hwrng_read(u8
*buf
, size_t len
)
167 cpacf_trng(NULL
, 0, buf
, len
);
168 atomic64_add(len
, &trng_hwrng_counter
);
171 static int trng_hwrng_data_read(struct hwrng
*rng
, u32
*data
)
173 size_t len
= sizeof(*data
);
175 _trng_hwrng_read((u8
*) data
, len
);
177 DEBUG_DBG("trng_hwrng_data_read()=%zu\n", len
);
182 static int trng_hwrng_read(struct hwrng
*rng
, void *data
, size_t max
, bool wait
)
184 size_t len
= max
<= PAGE_SIZE
? max
: PAGE_SIZE
;
186 _trng_hwrng_read((u8
*) data
, len
);
188 DEBUG_DBG("trng_hwrng_read()=%zu\n", len
);
194 * hwrng register struct
195 * The trng is suppost to have 100% entropy, and thus
196 * we register with a very high quality value.
198 static struct hwrng trng_hwrng_dev
= {
200 .data_read
= trng_hwrng_data_read
,
201 .read
= trng_hwrng_read
,
208 static void __init
trng_debug_init(void)
210 debug_info
= debug_register("trng", 1, 1, 4 * sizeof(long));
211 debug_register_view(debug_info
, &debug_sprintf_view
);
212 debug_set_level(debug_info
, 3);
215 static void trng_debug_exit(void)
217 debug_unregister(debug_info
);
220 static int __init
trng_init(void)
226 /* check if subfunction CPACF_PRNO_TRNG is available */
227 if (!cpacf_query_func(CPACF_PRNO
, CPACF_PRNO_TRNG
)) {
228 DEBUG_INFO("trng_init CPACF_PRNO_TRNG not available\n");
233 ret
= misc_register(&trng_dev
);
235 DEBUG_WARN("trng_init misc_register() failed rc=%d\n", ret
);
239 ret
= hwrng_register(&trng_hwrng_dev
);
241 DEBUG_WARN("trng_init hwrng_register() failed rc=%d\n", ret
);
245 DEBUG_DBG("trng_init successful\n");
250 misc_deregister(&trng_dev
);
256 static void __exit
trng_exit(void)
258 hwrng_unregister(&trng_hwrng_dev
);
259 misc_deregister(&trng_dev
);
263 module_cpu_feature_match(MSA
, trng_init
);
264 module_exit(trng_exit
);