zcrypt_queue.c 5.4 KB

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