zcrypt_card.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 card devices.
  44. */
  45. static ssize_t zcrypt_card_type_show(struct device *dev,
  46. struct device_attribute *attr, char *buf)
  47. {
  48. struct zcrypt_card *zc = to_ap_card(dev)->private;
  49. return snprintf(buf, PAGE_SIZE, "%s\n", zc->type_string);
  50. }
  51. static DEVICE_ATTR(type, 0444, zcrypt_card_type_show, NULL);
  52. static ssize_t zcrypt_card_online_show(struct device *dev,
  53. struct device_attribute *attr,
  54. char *buf)
  55. {
  56. struct zcrypt_card *zc = to_ap_card(dev)->private;
  57. return snprintf(buf, PAGE_SIZE, "%d\n", zc->online);
  58. }
  59. static ssize_t zcrypt_card_online_store(struct device *dev,
  60. struct device_attribute *attr,
  61. const char *buf, size_t count)
  62. {
  63. struct zcrypt_card *zc = to_ap_card(dev)->private;
  64. struct zcrypt_queue *zq;
  65. int online, id;
  66. if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
  67. return -EINVAL;
  68. zc->online = online;
  69. id = zc->card->id;
  70. ZCRYPT_DBF(DBF_INFO, "card=%02x online=%d\n", id, online);
  71. spin_lock(&zcrypt_list_lock);
  72. list_for_each_entry(zq, &zc->zqueues, list)
  73. zcrypt_queue_force_online(zq, online);
  74. spin_unlock(&zcrypt_list_lock);
  75. return count;
  76. }
  77. static DEVICE_ATTR(online, 0644, zcrypt_card_online_show,
  78. zcrypt_card_online_store);
  79. static struct attribute *zcrypt_card_attrs[] = {
  80. &dev_attr_type.attr,
  81. &dev_attr_online.attr,
  82. NULL,
  83. };
  84. static struct attribute_group zcrypt_card_attr_group = {
  85. .attrs = zcrypt_card_attrs,
  86. };
  87. struct zcrypt_card *zcrypt_card_alloc(void)
  88. {
  89. struct zcrypt_card *zc;
  90. zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL);
  91. if (!zc)
  92. return NULL;
  93. INIT_LIST_HEAD(&zc->list);
  94. INIT_LIST_HEAD(&zc->zqueues);
  95. kref_init(&zc->refcount);
  96. return zc;
  97. }
  98. EXPORT_SYMBOL(zcrypt_card_alloc);
  99. void zcrypt_card_free(struct zcrypt_card *zc)
  100. {
  101. kfree(zc);
  102. }
  103. EXPORT_SYMBOL(zcrypt_card_free);
  104. static void zcrypt_card_release(struct kref *kref)
  105. {
  106. struct zcrypt_card *zdev =
  107. container_of(kref, struct zcrypt_card, refcount);
  108. zcrypt_card_free(zdev);
  109. }
  110. void zcrypt_card_get(struct zcrypt_card *zc)
  111. {
  112. kref_get(&zc->refcount);
  113. }
  114. EXPORT_SYMBOL(zcrypt_card_get);
  115. int zcrypt_card_put(struct zcrypt_card *zc)
  116. {
  117. return kref_put(&zc->refcount, zcrypt_card_release);
  118. }
  119. EXPORT_SYMBOL(zcrypt_card_put);
  120. /**
  121. * zcrypt_card_register() - Register a crypto card device.
  122. * @zc: Pointer to a crypto card device
  123. *
  124. * Register a crypto card device. Returns 0 if successful.
  125. */
  126. int zcrypt_card_register(struct zcrypt_card *zc)
  127. {
  128. int rc;
  129. rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
  130. &zcrypt_card_attr_group);
  131. if (rc)
  132. return rc;
  133. spin_lock(&zcrypt_list_lock);
  134. list_add_tail(&zc->list, &zcrypt_card_list);
  135. spin_unlock(&zcrypt_list_lock);
  136. zc->online = 1;
  137. ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
  138. return rc;
  139. }
  140. EXPORT_SYMBOL(zcrypt_card_register);
  141. /**
  142. * zcrypt_card_unregister(): Unregister a crypto card device.
  143. * @zc: Pointer to crypto card device
  144. *
  145. * Unregister a crypto card device.
  146. */
  147. void zcrypt_card_unregister(struct zcrypt_card *zc)
  148. {
  149. ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id);
  150. spin_lock(&zcrypt_list_lock);
  151. list_del_init(&zc->list);
  152. spin_unlock(&zcrypt_list_lock);
  153. sysfs_remove_group(&zc->card->ap_dev.device.kobj,
  154. &zcrypt_card_attr_group);
  155. }
  156. EXPORT_SYMBOL(zcrypt_card_unregister);