s390-trng.c 5.7 KB

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