| .. | .. |
|---|
| 2 | 2 | /* |
|---|
| 3 | 3 | * Kernel interface for the s390 arch_random_* functions |
|---|
| 4 | 4 | * |
|---|
| 5 | | - * Copyright IBM Corp. 2017 |
|---|
| 5 | + * Copyright IBM Corp. 2017, 2022 |
|---|
| 6 | 6 | * |
|---|
| 7 | 7 | * Author: Harald Freudenberger <freude@de.ibm.com> |
|---|
| 8 | 8 | * |
|---|
| .. | .. |
|---|
| 14 | 14 | #ifdef CONFIG_ARCH_RANDOM |
|---|
| 15 | 15 | |
|---|
| 16 | 16 | #include <linux/static_key.h> |
|---|
| 17 | +#include <linux/preempt.h> |
|---|
| 17 | 18 | #include <linux/atomic.h> |
|---|
| 19 | +#include <asm/cpacf.h> |
|---|
| 18 | 20 | |
|---|
| 19 | 21 | DECLARE_STATIC_KEY_FALSE(s390_arch_random_available); |
|---|
| 20 | 22 | extern atomic64_t s390_arch_random_counter; |
|---|
| 21 | 23 | |
|---|
| 22 | | -bool s390_arch_random_generate(u8 *buf, unsigned int nbytes); |
|---|
| 23 | | - |
|---|
| 24 | | -static inline bool arch_has_random(void) |
|---|
| 24 | +static inline bool __must_check arch_get_random_long(unsigned long *v) |
|---|
| 25 | 25 | { |
|---|
| 26 | 26 | return false; |
|---|
| 27 | 27 | } |
|---|
| 28 | 28 | |
|---|
| 29 | | -static inline bool arch_has_random_seed(void) |
|---|
| 29 | +static inline bool __must_check arch_get_random_int(unsigned int *v) |
|---|
| 30 | 30 | { |
|---|
| 31 | | - if (static_branch_likely(&s390_arch_random_available)) |
|---|
| 31 | + return false; |
|---|
| 32 | +} |
|---|
| 33 | + |
|---|
| 34 | +static inline bool __must_check arch_get_random_seed_long(unsigned long *v) |
|---|
| 35 | +{ |
|---|
| 36 | + if (static_branch_likely(&s390_arch_random_available) && |
|---|
| 37 | + in_task()) { |
|---|
| 38 | + cpacf_trng(NULL, 0, (u8 *)v, sizeof(*v)); |
|---|
| 39 | + atomic64_add(sizeof(*v), &s390_arch_random_counter); |
|---|
| 32 | 40 | return true; |
|---|
| 33 | | - return false; |
|---|
| 34 | | -} |
|---|
| 35 | | - |
|---|
| 36 | | -static inline bool arch_get_random_long(unsigned long *v) |
|---|
| 37 | | -{ |
|---|
| 38 | | - return false; |
|---|
| 39 | | -} |
|---|
| 40 | | - |
|---|
| 41 | | -static inline bool arch_get_random_int(unsigned int *v) |
|---|
| 42 | | -{ |
|---|
| 43 | | - return false; |
|---|
| 44 | | -} |
|---|
| 45 | | - |
|---|
| 46 | | -static inline bool arch_get_random_seed_long(unsigned long *v) |
|---|
| 47 | | -{ |
|---|
| 48 | | - if (static_branch_likely(&s390_arch_random_available)) { |
|---|
| 49 | | - return s390_arch_random_generate((u8 *)v, sizeof(*v)); |
|---|
| 50 | 41 | } |
|---|
| 51 | 42 | return false; |
|---|
| 52 | 43 | } |
|---|
| 53 | 44 | |
|---|
| 54 | | -static inline bool arch_get_random_seed_int(unsigned int *v) |
|---|
| 45 | +static inline bool __must_check arch_get_random_seed_int(unsigned int *v) |
|---|
| 55 | 46 | { |
|---|
| 56 | | - if (static_branch_likely(&s390_arch_random_available)) { |
|---|
| 57 | | - return s390_arch_random_generate((u8 *)v, sizeof(*v)); |
|---|
| 47 | + if (static_branch_likely(&s390_arch_random_available) && |
|---|
| 48 | + in_task()) { |
|---|
| 49 | + cpacf_trng(NULL, 0, (u8 *)v, sizeof(*v)); |
|---|
| 50 | + atomic64_add(sizeof(*v), &s390_arch_random_counter); |
|---|
| 51 | + return true; |
|---|
| 58 | 52 | } |
|---|
| 59 | 53 | return false; |
|---|
| 60 | 54 | } |
|---|