zcrypt_queue.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright IBM Corp. 2001, 2012
  4. * Author(s): Robert Burroughs
  5. * Eric Rossman ([email protected])
  6. * Cornelia Huck <[email protected]>
  7. *
  8. * Hotplug & misc device support: Jochen Roehrig ([email protected])
  9. * Major cleanup & driver split: Martin Schwidefsky <[email protected]>
  10. * Ralph Wuerthner <[email protected]>
  11. * MSGTYPE restruct: Holger Dengler <[email protected]>
  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 = dev_get_drvdata(dev);
  39. struct ap_queue *aq = to_ap_queue(dev);
  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 zcrypt_queue *zq = dev_get_drvdata(dev);
  48. struct ap_queue *aq = to_ap_queue(dev);
  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_INFO("%s queue=%02x.%04x online=%d\n",
  59. __func__, AP_QID_CARD(zq->queue->qid),
  60. AP_QID_QUEUE(zq->queue->qid), online);
  61. ap_send_online_uevent(&aq->ap_dev, 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 = dev_get_drvdata(dev);
  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. bool zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
  84. {
  85. if (!!zq->online != !!online) {
  86. zq->online = online;
  87. if (!online)
  88. ap_flush_queue(zq->queue);
  89. return true;
  90. }
  91. return false;
  92. }
  93. struct zcrypt_queue *zcrypt_queue_alloc(size_t reply_buf_size)
  94. {
  95. struct zcrypt_queue *zq;
  96. zq = kzalloc(sizeof(*zq), GFP_KERNEL);
  97. if (!zq)
  98. return NULL;
  99. zq->reply.msg = kmalloc(reply_buf_size, GFP_KERNEL);
  100. if (!zq->reply.msg)
  101. goto out_free;
  102. zq->reply.bufsize = reply_buf_size;
  103. INIT_LIST_HEAD(&zq->list);
  104. kref_init(&zq->refcount);
  105. return zq;
  106. out_free:
  107. kfree(zq);
  108. return NULL;
  109. }
  110. EXPORT_SYMBOL(zcrypt_queue_alloc);
  111. void zcrypt_queue_free(struct zcrypt_queue *zq)
  112. {
  113. kfree(zq->reply.msg);
  114. kfree(zq);
  115. }
  116. EXPORT_SYMBOL(zcrypt_queue_free);
  117. static void zcrypt_queue_release(struct kref *kref)
  118. {
  119. struct zcrypt_queue *zq =
  120. container_of(kref, struct zcrypt_queue, refcount);
  121. zcrypt_queue_free(zq);
  122. }
  123. void zcrypt_queue_get(struct zcrypt_queue *zq)
  124. {
  125. kref_get(&zq->refcount);
  126. }
  127. EXPORT_SYMBOL(zcrypt_queue_get);
  128. int zcrypt_queue_put(struct zcrypt_queue *zq)
  129. {
  130. return kref_put(&zq->refcount, zcrypt_queue_release);
  131. }
  132. EXPORT_SYMBOL(zcrypt_queue_put);
  133. /**
  134. * zcrypt_queue_register() - Register a crypto queue device.
  135. * @zq: Pointer to a crypto queue device
  136. *
  137. * Register a crypto queue device. Returns 0 if successful.
  138. */
  139. int zcrypt_queue_register(struct zcrypt_queue *zq)
  140. {
  141. struct zcrypt_card *zc;
  142. int rc;
  143. spin_lock(&zcrypt_list_lock);
  144. zc = dev_get_drvdata(&zq->queue->card->ap_dev.device);
  145. zcrypt_card_get(zc);
  146. zq->zcard = zc;
  147. zq->online = 1; /* New devices are online by default. */
  148. ZCRYPT_DBF_INFO("%s queue=%02x.%04x register online=1\n",
  149. __func__, AP_QID_CARD(zq->queue->qid),
  150. AP_QID_QUEUE(zq->queue->qid));
  151. list_add_tail(&zq->list, &zc->zqueues);
  152. spin_unlock(&zcrypt_list_lock);
  153. rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
  154. &zcrypt_queue_attr_group);
  155. if (rc)
  156. goto out;
  157. if (zq->ops->rng) {
  158. rc = zcrypt_rng_device_add();
  159. if (rc)
  160. goto out_unregister;
  161. }
  162. return 0;
  163. out_unregister:
  164. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  165. &zcrypt_queue_attr_group);
  166. out:
  167. spin_lock(&zcrypt_list_lock);
  168. list_del_init(&zq->list);
  169. spin_unlock(&zcrypt_list_lock);
  170. zcrypt_card_put(zc);
  171. return rc;
  172. }
  173. EXPORT_SYMBOL(zcrypt_queue_register);
  174. /**
  175. * zcrypt_queue_unregister(): Unregister a crypto queue device.
  176. * @zq: Pointer to crypto queue device
  177. *
  178. * Unregister a crypto queue device.
  179. */
  180. void zcrypt_queue_unregister(struct zcrypt_queue *zq)
  181. {
  182. struct zcrypt_card *zc;
  183. ZCRYPT_DBF_INFO("%s queue=%02x.%04x unregister\n",
  184. __func__, AP_QID_CARD(zq->queue->qid),
  185. AP_QID_QUEUE(zq->queue->qid));
  186. zc = zq->zcard;
  187. spin_lock(&zcrypt_list_lock);
  188. list_del_init(&zq->list);
  189. spin_unlock(&zcrypt_list_lock);
  190. if (zq->ops->rng)
  191. zcrypt_rng_device_remove();
  192. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  193. &zcrypt_queue_attr_group);
  194. zcrypt_card_put(zc);
  195. zcrypt_queue_put(zq);
  196. }
  197. EXPORT_SYMBOL(zcrypt_queue_unregister);