zcrypt_card.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 card devices.
  33. */
  34. static ssize_t type_show(struct device *dev,
  35. struct device_attribute *attr, char *buf)
  36. {
  37. struct zcrypt_card *zc = dev_get_drvdata(dev);
  38. return scnprintf(buf, PAGE_SIZE, "%s\n", zc->type_string);
  39. }
  40. static DEVICE_ATTR_RO(type);
  41. static ssize_t online_show(struct device *dev,
  42. struct device_attribute *attr,
  43. char *buf)
  44. {
  45. struct zcrypt_card *zc = dev_get_drvdata(dev);
  46. struct ap_card *ac = to_ap_card(dev);
  47. int online = ac->config && zc->online ? 1 : 0;
  48. return scnprintf(buf, PAGE_SIZE, "%d\n", online);
  49. }
  50. static ssize_t online_store(struct device *dev,
  51. struct device_attribute *attr,
  52. const char *buf, size_t count)
  53. {
  54. struct zcrypt_card *zc = dev_get_drvdata(dev);
  55. struct ap_card *ac = to_ap_card(dev);
  56. struct zcrypt_queue *zq;
  57. int online, id, i = 0, maxzqs = 0;
  58. struct zcrypt_queue **zq_uelist = NULL;
  59. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  60. return -EINVAL;
  61. if (online && !ac->config)
  62. return -ENODEV;
  63. zc->online = online;
  64. id = zc->card->id;
  65. ZCRYPT_DBF_INFO("%s card=%02x online=%d\n", __func__, id, online);
  66. ap_send_online_uevent(&ac->ap_dev, online);
  67. spin_lock(&zcrypt_list_lock);
  68. /*
  69. * As we are in atomic context here, directly sending uevents
  70. * does not work. So collect the zqueues in a dynamic array
  71. * and process them after zcrypt_list_lock release. As we get/put
  72. * the zqueue objects, we make sure they exist after lock release.
  73. */
  74. list_for_each_entry(zq, &zc->zqueues, list)
  75. maxzqs++;
  76. if (maxzqs > 0)
  77. zq_uelist = kcalloc(maxzqs + 1, sizeof(*zq_uelist), GFP_ATOMIC);
  78. list_for_each_entry(zq, &zc->zqueues, list)
  79. if (zcrypt_queue_force_online(zq, online))
  80. if (zq_uelist) {
  81. zcrypt_queue_get(zq);
  82. zq_uelist[i++] = zq;
  83. }
  84. spin_unlock(&zcrypt_list_lock);
  85. if (zq_uelist) {
  86. for (i = 0; zq_uelist[i]; i++) {
  87. zq = zq_uelist[i];
  88. ap_send_online_uevent(&zq->queue->ap_dev, online);
  89. zcrypt_queue_put(zq);
  90. }
  91. kfree(zq_uelist);
  92. }
  93. return count;
  94. }
  95. static DEVICE_ATTR_RW(online);
  96. static ssize_t load_show(struct device *dev,
  97. struct device_attribute *attr,
  98. char *buf)
  99. {
  100. struct zcrypt_card *zc = dev_get_drvdata(dev);
  101. return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
  102. }
  103. static DEVICE_ATTR_RO(load);
  104. static struct attribute *zcrypt_card_attrs[] = {
  105. &dev_attr_type.attr,
  106. &dev_attr_online.attr,
  107. &dev_attr_load.attr,
  108. NULL,
  109. };
  110. static const struct attribute_group zcrypt_card_attr_group = {
  111. .attrs = zcrypt_card_attrs,
  112. };
  113. struct zcrypt_card *zcrypt_card_alloc(void)
  114. {
  115. struct zcrypt_card *zc;
  116. zc = kzalloc(sizeof(*zc), GFP_KERNEL);
  117. if (!zc)
  118. return NULL;
  119. INIT_LIST_HEAD(&zc->list);
  120. INIT_LIST_HEAD(&zc->zqueues);
  121. kref_init(&zc->refcount);
  122. return zc;
  123. }
  124. EXPORT_SYMBOL(zcrypt_card_alloc);
  125. void zcrypt_card_free(struct zcrypt_card *zc)
  126. {
  127. kfree(zc);
  128. }
  129. EXPORT_SYMBOL(zcrypt_card_free);
  130. static void zcrypt_card_release(struct kref *kref)
  131. {
  132. struct zcrypt_card *zdev =
  133. container_of(kref, struct zcrypt_card, refcount);
  134. zcrypt_card_free(zdev);
  135. }
  136. void zcrypt_card_get(struct zcrypt_card *zc)
  137. {
  138. kref_get(&zc->refcount);
  139. }
  140. EXPORT_SYMBOL(zcrypt_card_get);
  141. int zcrypt_card_put(struct zcrypt_card *zc)
  142. {
  143. return kref_put(&zc->refcount, zcrypt_card_release);
  144. }
  145. EXPORT_SYMBOL(zcrypt_card_put);
  146. /**
  147. * zcrypt_card_register() - Register a crypto card device.
  148. * @zc: Pointer to a crypto card device
  149. *
  150. * Register a crypto card device. Returns 0 if successful.
  151. */
  152. int zcrypt_card_register(struct zcrypt_card *zc)
  153. {
  154. int rc;
  155. spin_lock(&zcrypt_list_lock);
  156. list_add_tail(&zc->list, &zcrypt_card_list);
  157. spin_unlock(&zcrypt_list_lock);
  158. zc->online = 1;
  159. ZCRYPT_DBF_INFO("%s card=%02x register online=1\n",
  160. __func__, zc->card->id);
  161. rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
  162. &zcrypt_card_attr_group);
  163. if (rc) {
  164. spin_lock(&zcrypt_list_lock);
  165. list_del_init(&zc->list);
  166. spin_unlock(&zcrypt_list_lock);
  167. }
  168. return rc;
  169. }
  170. EXPORT_SYMBOL(zcrypt_card_register);
  171. /**
  172. * zcrypt_card_unregister(): Unregister a crypto card device.
  173. * @zc: Pointer to crypto card device
  174. *
  175. * Unregister a crypto card device.
  176. */
  177. void zcrypt_card_unregister(struct zcrypt_card *zc)
  178. {
  179. ZCRYPT_DBF_INFO("%s card=%02x unregister\n",
  180. __func__, zc->card->id);
  181. spin_lock(&zcrypt_list_lock);
  182. list_del_init(&zc->list);
  183. spin_unlock(&zcrypt_list_lock);
  184. sysfs_remove_group(&zc->card->ap_dev.device.kobj,
  185. &zcrypt_card_attr_group);
  186. zcrypt_card_put(zc);
  187. }
  188. EXPORT_SYMBOL(zcrypt_card_unregister);