zcrypt_queue.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * zcrypt 2.1.0
  3. *
  4. * Copyright IBM Corp. 2001, 2012
  5. * Author(s): Robert Burroughs
  6. * Eric Rossman (edrossma@us.ibm.com)
  7. * Cornelia Huck <cornelia.huck@de.ibm.com>
  8. *
  9. * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
  10. * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
  11. * Ralph Wuerthner <rwuerthn@de.ibm.com>
  12. * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2, or (at your option)
  17. * any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/miscdevice.h>
  28. #include <linux/fs.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/compat.h>
  32. #include <linux/slab.h>
  33. #include <linux/atomic.h>
  34. #include <linux/uaccess.h>
  35. #include <linux/hw_random.h>
  36. #include <linux/debugfs.h>
  37. #include <asm/debug.h>
  38. #include "zcrypt_debug.h"
  39. #include "zcrypt_api.h"
  40. #include "zcrypt_msgtype6.h"
  41. #include "zcrypt_msgtype50.h"
  42. /*
  43. * Device attributes common for all crypto queue devices.
  44. */
  45. static ssize_t zcrypt_queue_online_show(struct device *dev,
  46. struct device_attribute *attr,
  47. char *buf)
  48. {
  49. struct zcrypt_queue *zq = to_ap_queue(dev)->private;
  50. return snprintf(buf, PAGE_SIZE, "%d\n", zq->online);
  51. }
  52. static ssize_t zcrypt_queue_online_store(struct device *dev,
  53. struct device_attribute *attr,
  54. const char *buf, size_t count)
  55. {
  56. struct zcrypt_queue *zq = to_ap_queue(dev)->private;
  57. struct zcrypt_card *zc = zq->zcard;
  58. int online;
  59. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  60. return -EINVAL;
  61. if (online && !zc->online)
  62. return -EINVAL;
  63. zq->online = online;
  64. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x online=%d\n",
  65. AP_QID_CARD(zq->queue->qid),
  66. AP_QID_QUEUE(zq->queue->qid),
  67. online);
  68. if (!online)
  69. ap_flush_queue(zq->queue);
  70. return count;
  71. }
  72. static DEVICE_ATTR(online, 0644, zcrypt_queue_online_show,
  73. zcrypt_queue_online_store);
  74. static struct attribute *zcrypt_queue_attrs[] = {
  75. &dev_attr_online.attr,
  76. NULL,
  77. };
  78. static struct attribute_group zcrypt_queue_attr_group = {
  79. .attrs = zcrypt_queue_attrs,
  80. };
  81. void zcrypt_queue_force_online(struct zcrypt_queue *zq, int online)
  82. {
  83. zq->online = online;
  84. if (!online)
  85. ap_flush_queue(zq->queue);
  86. }
  87. struct zcrypt_queue *zcrypt_queue_alloc(size_t max_response_size)
  88. {
  89. struct zcrypt_queue *zq;
  90. zq = kzalloc(sizeof(struct zcrypt_queue), GFP_KERNEL);
  91. if (!zq)
  92. return NULL;
  93. zq->reply.message = kmalloc(max_response_size, GFP_KERNEL);
  94. if (!zq->reply.message)
  95. goto out_free;
  96. zq->reply.length = max_response_size;
  97. INIT_LIST_HEAD(&zq->list);
  98. kref_init(&zq->refcount);
  99. return zq;
  100. out_free:
  101. kfree(zq);
  102. return NULL;
  103. }
  104. EXPORT_SYMBOL(zcrypt_queue_alloc);
  105. void zcrypt_queue_free(struct zcrypt_queue *zq)
  106. {
  107. kfree(zq->reply.message);
  108. kfree(zq);
  109. }
  110. EXPORT_SYMBOL(zcrypt_queue_free);
  111. static void zcrypt_queue_release(struct kref *kref)
  112. {
  113. struct zcrypt_queue *zq =
  114. container_of(kref, struct zcrypt_queue, refcount);
  115. zcrypt_queue_free(zq);
  116. }
  117. void zcrypt_queue_get(struct zcrypt_queue *zq)
  118. {
  119. kref_get(&zq->refcount);
  120. }
  121. EXPORT_SYMBOL(zcrypt_queue_get);
  122. int zcrypt_queue_put(struct zcrypt_queue *zq)
  123. {
  124. return kref_put(&zq->refcount, zcrypt_queue_release);
  125. }
  126. EXPORT_SYMBOL(zcrypt_queue_put);
  127. /**
  128. * zcrypt_queue_register() - Register a crypto queue device.
  129. * @zq: Pointer to a crypto queue device
  130. *
  131. * Register a crypto queue device. Returns 0 if successful.
  132. */
  133. int zcrypt_queue_register(struct zcrypt_queue *zq)
  134. {
  135. struct zcrypt_card *zc;
  136. int rc;
  137. spin_lock(&zcrypt_list_lock);
  138. zc = zq->queue->card->private;
  139. zcrypt_card_get(zc);
  140. zq->zcard = zc;
  141. zq->online = 1; /* New devices are online by default. */
  142. ZCRYPT_DBF(DBF_INFO, "queue=%02x.%04x register online=1\n",
  143. AP_QID_CARD(zq->queue->qid), AP_QID_QUEUE(zq->queue->qid));
  144. list_add_tail(&zq->list, &zc->zqueues);
  145. zcrypt_device_count++;
  146. spin_unlock(&zcrypt_list_lock);
  147. rc = sysfs_create_group(&zq->queue->ap_dev.device.kobj,
  148. &zcrypt_queue_attr_group);
  149. if (rc)
  150. goto out;
  151. get_device(&zq->queue->ap_dev.device);
  152. if (zq->ops->rng) {
  153. rc = zcrypt_rng_device_add();
  154. if (rc)
  155. goto out_unregister;
  156. }
  157. return 0;
  158. out_unregister:
  159. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  160. &zcrypt_queue_attr_group);
  161. put_device(&zq->queue->ap_dev.device);
  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. zcrypt_card_put(zc);
  187. if (zq->ops->rng)
  188. zcrypt_rng_device_remove();
  189. sysfs_remove_group(&zq->queue->ap_dev.device.kobj,
  190. &zcrypt_queue_attr_group);
  191. put_device(&zq->queue->ap_dev.device);
  192. zcrypt_queue_put(zq);
  193. }
  194. EXPORT_SYMBOL(zcrypt_queue_unregister);