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 <[email protected]>
  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. MODULE_LICENSE("GPL v2");
  24. MODULE_AUTHOR("IBM Corporation");
  25. MODULE_DESCRIPTION("s390 CPACF TRNG device driver");
  26. /* trng related debug feature things */
  27. static debug_info_t *debug_info;
  28. #define DEBUG_DBG(...) debug_sprintf_event(debug_info, 6, ##__VA_ARGS__)
  29. #define DEBUG_INFO(...) debug_sprintf_event(debug_info, 5, ##__VA_ARGS__)
  30. #define DEBUG_WARN(...) debug_sprintf_event(debug_info, 4, ##__VA_ARGS__)
  31. #define DEBUG_ERR(...) debug_sprintf_event(debug_info, 3, ##__VA_ARGS__)
  32. /* trng helpers */
  33. static atomic64_t trng_dev_counter = ATOMIC64_INIT(0);
  34. static atomic64_t trng_hwrng_counter = ATOMIC64_INIT(0);
  35. /* file io functions */
  36. static int trng_open(struct inode *inode, struct file *file)
  37. {
  38. return nonseekable_open(inode, file);
  39. }
  40. static ssize_t trng_read(struct file *file, char __user *ubuf,
  41. size_t nbytes, loff_t *ppos)
  42. {
  43. u8 buf[32];
  44. u8 *p = buf;
  45. unsigned int n;
  46. ssize_t ret = 0;
  47. /*
  48. * use buf for requests <= sizeof(buf),
  49. * otherwise allocate one page and fetch
  50. * pagewise.
  51. */
  52. if (nbytes > sizeof(buf)) {
  53. p = (u8 *) __get_free_page(GFP_KERNEL);
  54. if (!p)
  55. return -ENOMEM;
  56. }
  57. while (nbytes) {
  58. if (need_resched()) {
  59. if (signal_pending(current)) {
  60. if (ret == 0)
  61. ret = -ERESTARTSYS;
  62. break;
  63. }
  64. schedule();
  65. }
  66. n = nbytes > PAGE_SIZE ? PAGE_SIZE : nbytes;
  67. cpacf_trng(NULL, 0, p, n);
  68. atomic64_add(n, &trng_dev_counter);
  69. if (copy_to_user(ubuf, p, n)) {
  70. ret = -EFAULT;
  71. break;
  72. }
  73. nbytes -= n;
  74. ubuf += n;
  75. ret += n;
  76. }
  77. if (p != buf)
  78. free_page((unsigned long) p);
  79. DEBUG_DBG("trng_read()=%zd\n", ret);
  80. return ret;
  81. }
  82. /* sysfs */
  83. static ssize_t trng_counter_show(struct device *dev,
  84. struct device_attribute *attr, char *buf)
  85. {
  86. u64 dev_counter = atomic64_read(&trng_dev_counter);
  87. u64 hwrng_counter = atomic64_read(&trng_hwrng_counter);
  88. u64 arch_counter = atomic64_read(&s390_arch_random_counter);
  89. return sysfs_emit(buf,
  90. "trng: %llu\n"
  91. "hwrng: %llu\n"
  92. "arch: %llu\n"
  93. "total: %llu\n",
  94. dev_counter, hwrng_counter, arch_counter,
  95. dev_counter + hwrng_counter + arch_counter);
  96. }
  97. static DEVICE_ATTR(byte_counter, 0444, trng_counter_show, NULL);
  98. static struct attribute *trng_dev_attrs[] = {
  99. &dev_attr_byte_counter.attr,
  100. NULL
  101. };
  102. static const struct attribute_group trng_dev_attr_group = {
  103. .attrs = trng_dev_attrs
  104. };
  105. static const struct attribute_group *trng_dev_attr_groups[] = {
  106. &trng_dev_attr_group,
  107. NULL
  108. };
  109. static const struct file_operations trng_fops = {
  110. .owner = THIS_MODULE,
  111. .open = &trng_open,
  112. .release = NULL,
  113. .read = &trng_read,
  114. .llseek = noop_llseek,
  115. };
  116. static struct miscdevice trng_dev = {
  117. .name = "trng",
  118. .minor = MISC_DYNAMIC_MINOR,
  119. .mode = 0444,
  120. .fops = &trng_fops,
  121. .groups = trng_dev_attr_groups,
  122. };
  123. /* hwrng_register */
  124. static inline void _trng_hwrng_read(u8 *buf, size_t len)
  125. {
  126. cpacf_trng(NULL, 0, buf, len);
  127. atomic64_add(len, &trng_hwrng_counter);
  128. }
  129. static int trng_hwrng_data_read(struct hwrng *rng, u32 *data)
  130. {
  131. size_t len = sizeof(*data);
  132. _trng_hwrng_read((u8 *) data, len);
  133. DEBUG_DBG("trng_hwrng_data_read()=%zu\n", len);
  134. return len;
  135. }
  136. static int trng_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
  137. {
  138. size_t len = max <= PAGE_SIZE ? max : PAGE_SIZE;
  139. _trng_hwrng_read((u8 *) data, len);
  140. DEBUG_DBG("trng_hwrng_read()=%zu\n", len);
  141. return len;
  142. }
  143. /*
  144. * hwrng register struct
  145. * The trng is supposed to have 100% entropy, and thus we register with a very
  146. * high quality value. If we ever have a better driver in the future, we should
  147. * change this value again when we merge this driver.
  148. */
  149. static struct hwrng trng_hwrng_dev = {
  150. .name = "s390-trng",
  151. .data_read = trng_hwrng_data_read,
  152. .read = trng_hwrng_read,
  153. .quality = 1024,
  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);