Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
6bb20c1
Documentation
LICENSES
arch
block
certs
crypto
drivers
accel
accessibility
acpi
amba
android
ata
atm
auxdisplay
base
bcma
block
bluetooth
bus
cdrom
char
agp
hw_random
Kconfig
Makefile
amd-rng.c
arm_smccc_trng.c
atmel-rng.c
ba431-rng.c
bcm2835-rng.c
cavium-rng-vf.c
cavium-rng.c
cctrng.c
cctrng.h
cn10k-rng.c
core.c
exynos-trng.c
geode-rng.c
hisi-rng.c
imx-rngc.c
ingenic-rng.c
ingenic-trng.c
intel-rng.c
iproc-rng200.c
ixp4xx-rng.c
ks-sa-rng.c
meson-rng.c
mpfs-rng.c
mtk-rng.c
mxc-rnga.c
n2-asm.S
n2-drv.c
n2rng.h
nomadik-rng.c
npcm-rng.c
octeon-rng.c
omap-rng.c
omap3-rom-rng.c
optee-rng.c
pasemi-rng.c
pic32-rng.c
powernv-rng.c
pseries-rng.c
s390-trng.c
st-rng.c
stm32-rng.c
timeriomem-rng.c
via-rng.c
virtio-rng.c
xgene-rng.c
xiphera-trng.c
ipmi
mwave
pcmcia
tpm
xilinx_hwicap
xillybus
Kconfig
Makefile
adi.c
apm-emulation.c
applicom.c
applicom.h
bsr.c
ds1620.c
dsp56k.c
dtlk.c
hangcheck-timer.c
hpet.c
lp.c
mem.c
misc.c
mspec.c
nsc_gpio.c
nvram.c
nwbutton.c
nwbutton.h
nwflash.c
pc8736x_gpio.c
powernv-op-panel.c
ppdev.c
ps3flash.c
random.c
scx200_gpio.c
sonypi.c
tlclk.c
toshiba.c
ttyprintk.c
uv_mmtimer.c
virtio_console.c
clk
clocksource
comedi
connector
counter
cpufreq
cpuidle
crypto
cxl
dax
dca
devfreq
dio
dma-buf
dma
edac
eisa
extcon
firewire
firmware
fpga
fsi
gnss
gpio
gpu
greybus
hid
hsi
hte
hv
hwmon
hwspinlock
hwtracing
i2c
i3c
idle
iio
infiniband
input
interconnect
iommu
ipack
irqchip
isdn
leds
macintosh
mailbox
mcb
md
media
memory
memstick
message
mfd
misc
mmc
most
mtd
mux
net
nfc
ntb
nubus
nvdimm
nvme
nvmem
of
opp
parisc
parport
pci
pcmcia
peci
perf
phy
pinctrl
platform
pnp
power
powercap
pps
ps3
ptp
pwm
rapidio
ras
regulator
remoteproc
reset
rpmsg
rtc
s390
sbus
scsi
sh
siox
slimbus
soc
soundwire
spi
spmi
ssb
staging
target
tc
tee
thermal
thunderbolt
tty
ufs
uio
usb
vdpa
vfio
vhost
video
virt
virtio
vlynq
w1
watchdog
xen
zorro
Kconfig
Makefile
fs
include
init
io_uring
ipc
kernel
lib
mm
net
rust
samples
scripts
security
sound
tools
usr
virt
.clang-format
.cocciconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
.rustfmt.toml
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
drivers
/
char
/
hw_random
/
s390-trng.c
Copy path
Blame
Blame
Latest commit
History
History
256 lines (201 loc) · 5.44 KB
Breadcrumbs
linux
/
drivers
/
char
/
hw_random
/
s390-trng.c
Top
File metadata and controls
Code
Blame
256 lines (201 loc) · 5.44 KB
Raw
// 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> #include <asm/archrandom.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, }; /* 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);
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
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
You can’t perform that action at this time.