zcrypt_queue.c 5.3 KB

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