zcrypt_queue.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 zcrypt_queue *zq = to_ap_queue(dev)->private;
  39. return snprintf(buf, PAGE_SIZE, "%d\n", zq->online);
  40. }
  41. static ssize_t online_store(struct device *dev,
  42. struct device_attribute *attr,
  43. const char *buf, size_t count)
  44. {
  45. struct zcrypt_queue *zq = to_ap_queue(dev)->private;
  46. struct zcrypt_card *zc = zq->zcard;
  47. int online;
  48. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  49. return -EINVAL;
  50. if (online && !zc->online)
  51. return -EINVAL;
  52. zq->online = online;
  53. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x online=%d\n",
  54. AP_QID_CARD(zq->queue->qid),
  55. AP_QID_QUEUE(zq->queue->qid),
  56. online);
  57. if (!online)
  58. ap_flush_queue(zq->queue);
  59. return count;
  60. }
  61. static DEVICE_ATTR_RW(online);
  62. static ssize_t load_show(struct device *dev,
  63. struct device_attribute *attr,
  64. char *buf)
  65. {
  66. struct zcrypt_queue *zq = to_ap_queue(dev)->private;
  67. return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zq->load));
  68. }
  69. static DEVICE_ATTR_RO(load);
  70. static struct attribute *zcrypt_queue_attrs[] = {
  71. &dev_attr_online.attr,
  72. &dev_attr_load.attr,
  73. NULL,
  74. };
  75. static const struct attribute_group zcrypt_queue_attr_group = {
  76. .attrs = zcrypt_queue_attrs,
  77. };
  78. void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
  79. {
  80. zq->online = online;
  81. if (!online)
  82. ap_flush_queue(zq->queue);
  83. }
  84. struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
  85. {
  86. struct zcrypt_queue *zq;
  87. zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
  88. if (!zq)
  89. return NULL;
  90. zq->reply.message = kmalloc(max_response_size, GFP_KERNEL);
  91. if (!zq->reply.message)
  92. goto out_free;
  93. zq->reply.length = max_response_size;
  94. INIT_LIST_HEAD(&zq->list);
  95. kref_init(&zq->refcount);
  96. return zq;
  97. out_free:
  98. kfree(zq);
  99. return NULL;
  100. }
  101. EXPORT_SYMBOL(zcrypt_queue_alloc);
  102. void zcrypt_queue_free(struct zcrypt_queue *zq)
  103. {
  104. kfree(zq->reply.message);
  105. kfree(zq);
  106. }
  107. EXPORT_SYMBOL(zcrypt_queue_free);
  108. static void zcrypt_queue_release(struct kref *kref)
  109. {
  110. struct zcrypt_queue *zq =
  111. container_of(kref, struct zcrypt_queue, refcount);
  112. zcrypt_queue_free(zq);
  113. }
  114. void zcrypt_queue_get(struct zcrypt_queue *zq)
  115. {
  116. kref_get(&zq->refcount);
  117. }
  118. EXPORT_SYMBOL(zcrypt_queue_get);
  119. int zcrypt_queue_put(struct zcrypt_queue *zq)
  120. {
  121. return kref_put(&zq->refcount, zcrypt_queue_release);
  122. }
  123. EXPORT_SYMBOL(zcrypt_queue_put);
  124. /**
  125. * zcrypt_queue_register() - Register a crypto queue device.
  126. * @zq: Pointer to a crypto queue device
  127. *
  128. * Register a crypto queue device. Returns 0 if successful.
  129. */
  130. int zcrypt_queue_register(struct zcrypt_queue *zq)
  131. {
  132. struct zcrypt_card *zc;
  133. int rc;
  134. spin_lock(&zcrypt_list_lock);
  135. zc = zq->queue->card->private;
  136. zcrypt_card_get(zc);
  137. zq->zcard = zc;
  138. zq->online = 1; /* New devices are online by default. */
  139. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n",
  140. AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
  141. list_add_tail(&zq->list, &zc->zqueues);
  142. zcrypt_device_count++;
  143. spin_unlock(&zcrypt_list_lock);
  144. rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
  145. &zcrypt_queue_attr_group);
  146. if (rc)
  147. goto out;
  148. get_device(&zq->queue->ap_dev.device);
  149. if (zq->ops->rng) {
  150. rc = zcrypt_rng_device_add();
  151. if (rc)
  152. goto out_unregister;
  153. }
  154. return 0;
  155. out_unregister:
  156. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  157. &zcrypt_queue_attr_group);
  158. put_device(&zq->queue->ap_dev.device);
  159. out:
  160. spin_lock(&zcrypt_list_lock);
  161. list_del_init(&zq->list);
  162. spin_unlock(&zcrypt_list_lock);
  163. zcrypt_card_put(zc);
  164. return rc;
  165. }
  166. EXPORT_SYMBOL(zcrypt_queue_register);
  167. /**
  168. * zcrypt_queue_unregister(): Unregister a crypto queue device.
  169. * @zq: Pointer to crypto queue device
  170. *
  171. * Unregister a crypto queue device.
  172. */
  173. void zcrypt_queue_unregister(struct zcrypt_queue *zq)
  174. {
  175. struct zcrypt_card *zc;
  176. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x unregister\n",
  177. AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
  178. zc = zq->zcard;
  179. spin_lock(&zcrypt_list_lock);
  180. list_del_init(&zq->list);
  181. zcrypt_device_count--;
  182. spin_unlock(&zcrypt_list_lock);
  183. zcrypt_card_put(zc);
  184. if (zq->ops->rng)
  185. zcrypt_rng_device_remove();
  186. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  187. &zcrypt_queue_attr_group);
  188. put_device(&zq->queue->ap_dev.device);
  189. zcrypt_queue_put(zq);
  190. }
  191. EXPORT_SYMBOL(zcrypt_queue_unregister);