zcrypt_queue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2001, 2012
  4. * Author(s): Robert Burroughs
  5. * Eric Rossman (edrossma@us.ibm.com)
  6. * Cornelia Huck <cornelia.huck@de.ibm.com>
  7. *
  8. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  9. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  10. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  11. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/fs.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/compat.h>
  21. #include <linux/slab.h>
  22. #include <linux/atomic.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/hw_random.h>
  25. #include <linux/debugfs.h>
  26. #include <asm/debug.h>
  27. #include "zcrypt_debug.h"
  28. #include "zcrypt_api.h"
  29. #include "zcrypt_msgtype6.h"
  30. #include "zcrypt_msgtype50.h"
  31. /*
  32. * Device attributes common for all crypto queue devices.
  33. */
  34. static ssize_t online_show(struct device *dev,
  35. struct device_attribute *attr,
  36. char *buf)
  37. {
  38. struct ap_queue *aq = to_ap_queue(dev);
  39. struct zcrypt_queue *zq = aq->private;
  40. int online = aq->config && zq->online ? 1 : 0;
  41. return scnprintf(buf, PAGE_SIZE, "%d\n", online);
  42. }
  43. static ssize_t online_store(struct device *dev,
  44. struct device_attribute *attr,
  45. const char *buf, size_t count)
  46. {
  47. struct ap_queue *aq = to_ap_queue(dev);
  48. struct zcrypt_queue *zq = aq->private;
  49. struct zcrypt_card *zc = zq->zcard;
  50. int online;
  51. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  52. return -EINVAL;
  53. if (online && (!aq->config || !aq->card->config))
  54. return -ENODEV;
  55. if (online && !zc->online)
  56. return -EINVAL;
  57. zq->online = online;
  58. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x online=%d\n",
  59. AP_QID_CARD(zq->queue->qid),
  60. AP_QID_QUEUE(zq->queue->qid),
  61. online);
  62. if (!online)
  63. ap_flush_queue(zq->queue);
  64. return count;
  65. }
  66. static DEVICE_ATTR_RW(online);
  67. static ssize_t load_show(struct device *dev,
  68. struct device_attribute *attr,
  69. char *buf)
  70. {
  71. struct zcrypt_queue *zq = to_ap_queue(dev)->private;
  72. return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zq->load));
  73. }
  74. static DEVICE_ATTR_RO(load);
  75. static struct attribute *zcrypt_queue_attrs[] = {
  76. &dev_attr_online.attr,
  77. &dev_attr_load.attr,
  78. NULL,
  79. };
  80. static const struct attribute_group zcrypt_queue_attr_group = {
  81. .attrs = zcrypt_queue_attrs,
  82. };
  83. void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
  84. {
  85. zq->online = online;
  86. if (!online)
  87. ap_flush_queue(zq->queue);
  88. }
  89. struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
  90. {
  91. struct zcrypt_queue *zq;
  92. zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
  93. if (!zq)
  94. return NULL;
  95. zq->reply.msg = kmalloc(max_response_size, GFP_KERNEL);
  96. if (!zq->reply.msg)
  97. goto out_free;
  98. zq->reply.len = max_response_size;
  99. INIT_LIST_HEAD(&zq->list);
  100. kref_init(&zq->refcount);
  101. return zq;
  102. out_free:
  103. kfree(zq);
  104. return NULL;
  105. }
  106. EXPORT_SYMBOL(zcrypt_queue_alloc);
  107. void zcrypt_queue_free(struct zcrypt_queue *zq)
  108. {
  109. kfree(zq->reply.msg);
  110. kfree(zq);
  111. }
  112. EXPORT_SYMBOL(zcrypt_queue_free);
  113. static void zcrypt_queue_release(struct kref *kref)
  114. {
  115. struct zcrypt_queue *zq =
  116. container_of(kref, struct zcrypt_queue, refcount);
  117. zcrypt_queue_free(zq);
  118. }
  119. void zcrypt_queue_get(struct zcrypt_queue *zq)
  120. {
  121. kref_get(&zq->refcount);
  122. }
  123. EXPORT_SYMBOL(zcrypt_queue_get);
  124. int zcrypt_queue_put(struct zcrypt_queue *zq)
  125. {
  126. return kref_put(&zq->refcount, zcrypt_queue_release);
  127. }
  128. EXPORT_SYMBOL(zcrypt_queue_put);
  129. /**
  130. * zcrypt_queue_register() - Register a crypto queue device.
  131. * @zq: Pointer to a crypto queue device
  132. *
  133. * Register a crypto queue device. Returns 0 if successful.
  134. */
  135. int zcrypt_queue_register(struct zcrypt_queue *zq)
  136. {
  137. struct zcrypt_card *zc;
  138. int rc;
  139. spin_lock(&zcrypt_list_lock);
  140. zc = zq->queue->card->private;
  141. zcrypt_card_get(zc);
  142. zq->zcard = zc;
  143. zq->online = 1; /* New devices are online by default. */
  144. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n",
  145. AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
  146. list_add_tail(&zq->list, &zc->zqueues);
  147. zcrypt_device_count++;
  148. spin_unlock(&zcrypt_list_lock);
  149. rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
  150. &zcrypt_queue_attr_group);
  151. if (rc)
  152. goto out;
  153. if (zq->ops->rng) {
  154. rc = zcrypt_rng_device_add();
  155. if (rc)
  156. goto out_unregister;
  157. }
  158. return 0;
  159. out_unregister:
  160. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  161. &zcrypt_queue_attr_group);
  162. out:
  163. spin_lock(&zcrypt_list_lock);
  164. list_del_init(&zq->list);
  165. spin_unlock(&zcrypt_list_lock);
  166. zcrypt_card_put(zc);
  167. return rc;
  168. }
  169. EXPORT_SYMBOL(zcrypt_queue_register);
  170. /**
  171. * zcrypt_queue_unregister(): Unregister a crypto queue device.
  172. * @zq: Pointer to crypto queue device
  173. *
  174. * Unregister a crypto queue device.
  175. */
  176. void zcrypt_queue_unregister(struct zcrypt_queue *zq)
  177. {
  178. struct zcrypt_card *zc;
  179. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n",
  180. AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
  181. zc = zq->zcard;
  182. spin_lock(&zcrypt_list_lock);
  183. list_del_init(&zq->list);
  184. zcrypt_device_count--;
  185. spin_unlock(&zcrypt_list_lock);
  186. if (zq->ops->rng)
  187. zcrypt_rng_device_remove();
  188. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  189. &zcrypt_queue_attr_group);
  190. zcrypt_card_put(zc);
  191. zcrypt_queue_put(zq);
  192. }
  193. EXPORT_SYMBOL(zcrypt_queue_unregister);