s390-trng.c 5.4 KB

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