blk-crypto-sysfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2021 Google LLC
  4. *
  5. * sysfs support for blk-crypto. This file contains the code which exports the
  6. * crypto capabilities of devices via /sys/block/$disk/queue/crypto/.
  7. */
  8. #include <linux/blk-crypto-profile.h>
  9. #include "blk-crypto-internal.h"
  10. struct blk_crypto_kobj {
  11. struct kobject kobj;
  12. struct blk_crypto_profile *profile;
  13. };
  14. struct blk_crypto_attr {
  15. struct attribute attr;
  16. ssize_t (*show)(struct blk_crypto_profile *profile,
  17. struct blk_crypto_attr *attr, char *page);
  18. };
  19. static struct blk_crypto_profile *kobj_to_crypto_profile(struct kobject *kobj)
  20. {
  21. return container_of(kobj, struct blk_crypto_kobj, kobj)->profile;
  22. }
  23. static struct blk_crypto_attr *attr_to_crypto_attr(struct attribute *attr)
  24. {
  25. return container_of(attr, struct blk_crypto_attr, attr);
  26. }
  27. static ssize_t hw_wrapped_keys_show(struct blk_crypto_profile *profile,
  28. struct blk_crypto_attr *attr, char *page)
  29. {
  30. /* Always show supported, since the file doesn't exist otherwise. */
  31. return sysfs_emit(page, "supported\n");
  32. }
  33. static ssize_t max_dun_bits_show(struct blk_crypto_profile *profile,
  34. struct blk_crypto_attr *attr, char *page)
  35. {
  36. return sysfs_emit(page, "%u\n", 8 * profile->max_dun_bytes_supported);
  37. }
  38. static ssize_t num_keyslots_show(struct blk_crypto_profile *profile,
  39. struct blk_crypto_attr *attr, char *page)
  40. {
  41. return sysfs_emit(page, "%u\n", profile->num_slots);
  42. }
  43. static ssize_t raw_keys_show(struct blk_crypto_profile *profile,
  44. struct blk_crypto_attr *attr, char *page)
  45. {
  46. /* Always show supported, since the file doesn't exist otherwise. */
  47. return sysfs_emit(page, "supported\n");
  48. }
  49. #define BLK_CRYPTO_RO_ATTR(_name) \
  50. static struct blk_crypto_attr _name##_attr = __ATTR_RO(_name)
  51. BLK_CRYPTO_RO_ATTR(hw_wrapped_keys);
  52. BLK_CRYPTO_RO_ATTR(max_dun_bits);
  53. BLK_CRYPTO_RO_ATTR(num_keyslots);
  54. BLK_CRYPTO_RO_ATTR(raw_keys);
  55. static umode_t blk_crypto_is_visible(struct kobject *kobj,
  56. struct attribute *attr, int n)
  57. {
  58. struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
  59. struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
  60. if (a == &hw_wrapped_keys_attr &&
  61. !(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_HW_WRAPPED))
  62. return 0;
  63. if (a == &raw_keys_attr &&
  64. !(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_RAW))
  65. return 0;
  66. return 0444;
  67. }
  68. static struct attribute *blk_crypto_attrs[] = {
  69. &hw_wrapped_keys_attr.attr,
  70. &max_dun_bits_attr.attr,
  71. &num_keyslots_attr.attr,
  72. &raw_keys_attr.attr,
  73. NULL,
  74. };
  75. static const struct attribute_group blk_crypto_attr_group = {
  76. .attrs = blk_crypto_attrs,
  77. .is_visible = blk_crypto_is_visible,
  78. };
  79. /*
  80. * The encryption mode attributes. To avoid hard-coding the list of encryption
  81. * modes, these are initialized at boot time by blk_crypto_sysfs_init().
  82. */
  83. static struct blk_crypto_attr __blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX];
  84. static struct attribute *blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX + 1];
  85. static umode_t blk_crypto_mode_is_visible(struct kobject *kobj,
  86. struct attribute *attr, int n)
  87. {
  88. struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
  89. struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
  90. int mode_num = a - __blk_crypto_mode_attrs;
  91. if (profile->modes_supported[mode_num])
  92. return 0444;
  93. return 0;
  94. }
  95. static ssize_t blk_crypto_mode_show(struct blk_crypto_profile *profile,
  96. struct blk_crypto_attr *attr, char *page)
  97. {
  98. int mode_num = attr - __blk_crypto_mode_attrs;
  99. return sysfs_emit(page, "0x%x\n", profile->modes_supported[mode_num]);
  100. }
  101. static const struct attribute_group blk_crypto_modes_attr_group = {
  102. .name = "modes",
  103. .attrs = blk_crypto_mode_attrs,
  104. .is_visible = blk_crypto_mode_is_visible,
  105. };
  106. static const struct attribute_group *blk_crypto_attr_groups[] = {
  107. &blk_crypto_attr_group,
  108. &blk_crypto_modes_attr_group,
  109. NULL,
  110. };
  111. static ssize_t blk_crypto_attr_show(struct kobject *kobj,
  112. struct attribute *attr, char *page)
  113. {
  114. struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
  115. struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
  116. return a->show(profile, a, page);
  117. }
  118. static const struct sysfs_ops blk_crypto_attr_ops = {
  119. .show = blk_crypto_attr_show,
  120. };
  121. static void blk_crypto_release(struct kobject *kobj)
  122. {
  123. kfree(container_of(kobj, struct blk_crypto_kobj, kobj));
  124. }
  125. static const struct kobj_type blk_crypto_ktype = {
  126. .default_groups = blk_crypto_attr_groups,
  127. .sysfs_ops = &blk_crypto_attr_ops,
  128. .release = blk_crypto_release,
  129. };
  130. /*
  131. * If the request_queue has a blk_crypto_profile, create the "crypto"
  132. * subdirectory in sysfs (/sys/block/$disk/queue/crypto/).
  133. */
  134. int blk_crypto_sysfs_register(struct gendisk *disk)
  135. {
  136. struct request_queue *q = disk->queue;
  137. struct blk_crypto_kobj *obj;
  138. int err;
  139. if (!q->crypto_profile)
  140. return 0;
  141. obj = kzalloc_obj(*obj);
  142. if (!obj)
  143. return -ENOMEM;
  144. obj->profile = q->crypto_profile;
  145. err = kobject_init_and_add(&obj->kobj, &blk_crypto_ktype,
  146. &disk->queue_kobj, "crypto");
  147. if (err) {
  148. kobject_put(&obj->kobj);
  149. return err;
  150. }
  151. q->crypto_kobject = &obj->kobj;
  152. return 0;
  153. }
  154. void blk_crypto_sysfs_unregister(struct gendisk *disk)
  155. {
  156. kobject_put(disk->queue->crypto_kobject);
  157. }
  158. static int __init blk_crypto_sysfs_init(void)
  159. {
  160. int i;
  161. BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
  162. for (i = 1; i < BLK_ENCRYPTION_MODE_MAX; i++) {
  163. struct blk_crypto_attr *attr = &__blk_crypto_mode_attrs[i];
  164. attr->attr.name = blk_crypto_modes[i].name;
  165. attr->attr.mode = 0444;
  166. attr->show = blk_crypto_mode_show;
  167. blk_crypto_mode_attrs[i - 1] = &attr->attr;
  168. }
  169. return 0;
  170. }
  171. subsys_initcall(blk_crypto_sysfs_init);