blk-crypto-sysfs.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 max_dun_bits_show(struct blk_crypto_profile *profile,
  28. struct blk_crypto_attr *attr, char *page)
  29. {
  30. return sysfs_emit(page, "%u\n", 8 * profile->max_dun_bytes_supported);
  31. }
  32. static ssize_t num_keyslots_show(struct blk_crypto_profile *profile,
  33. struct blk_crypto_attr *attr, char *page)
  34. {
  35. return sysfs_emit(page, "%u\n", profile->num_slots);
  36. }
  37. #define BLK_CRYPTO_RO_ATTR(_name) \
  38. static struct blk_crypto_attr _name##_attr = __ATTR_RO(_name)
  39. BLK_CRYPTO_RO_ATTR(max_dun_bits);
  40. BLK_CRYPTO_RO_ATTR(num_keyslots);
  41. static struct attribute *blk_crypto_attrs[] = {
  42. &max_dun_bits_attr.attr,
  43. &num_keyslots_attr.attr,
  44. NULL,
  45. };
  46. static const struct attribute_group blk_crypto_attr_group = {
  47. .attrs = blk_crypto_attrs,
  48. };
  49. /*
  50. * The encryption mode attributes. To avoid hard-coding the list of encryption
  51. * modes, these are initialized at boot time by blk_crypto_sysfs_init().
  52. */
  53. static struct blk_crypto_attr __blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX];
  54. static struct attribute *blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX + 1];
  55. static umode_t blk_crypto_mode_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. int mode_num = a - __blk_crypto_mode_attrs;
  61. if (profile->modes_supported[mode_num])
  62. return 0444;
  63. return 0;
  64. }
  65. static ssize_t blk_crypto_mode_show(struct blk_crypto_profile *profile,
  66. struct blk_crypto_attr *attr, char *page)
  67. {
  68. int mode_num = attr - __blk_crypto_mode_attrs;
  69. return sysfs_emit(page, "0x%x\n", profile->modes_supported[mode_num]);
  70. }
  71. static const struct attribute_group blk_crypto_modes_attr_group = {
  72. .name = "modes",
  73. .attrs = blk_crypto_mode_attrs,
  74. .is_visible = blk_crypto_mode_is_visible,
  75. };
  76. static const struct attribute_group *blk_crypto_attr_groups[] = {
  77. &blk_crypto_attr_group,
  78. &blk_crypto_modes_attr_group,
  79. NULL,
  80. };
  81. static ssize_t blk_crypto_attr_show(struct kobject *kobj,
  82. struct attribute *attr, char *page)
  83. {
  84. struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
  85. struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
  86. return a->show(profile, a, page);
  87. }
  88. static const struct sysfs_ops blk_crypto_attr_ops = {
  89. .show = blk_crypto_attr_show,
  90. };
  91. static void blk_crypto_release(struct kobject *kobj)
  92. {
  93. kfree(container_of(kobj, struct blk_crypto_kobj, kobj));
  94. }
  95. static struct kobj_type blk_crypto_ktype = {
  96. .default_groups = blk_crypto_attr_groups,
  97. .sysfs_ops = &blk_crypto_attr_ops,
  98. .release = blk_crypto_release,
  99. };
  100. /*
  101. * If the request_queue has a blk_crypto_profile, create the "crypto"
  102. * subdirectory in sysfs (/sys/block/$disk/queue/crypto/).
  103. */
  104. int blk_crypto_sysfs_register(struct gendisk *disk)
  105. {
  106. struct request_queue *q = disk->queue;
  107. struct blk_crypto_kobj *obj;
  108. int err;
  109. if (!q->crypto_profile)
  110. return 0;
  111. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  112. if (!obj)
  113. return -ENOMEM;
  114. obj->profile = q->crypto_profile;
  115. err = kobject_init_and_add(&obj->kobj, &blk_crypto_ktype, &q->kobj,
  116. "crypto");
  117. if (err) {
  118. kobject_put(&obj->kobj);
  119. return err;
  120. }
  121. q->crypto_kobject = &obj->kobj;
  122. return 0;
  123. }
  124. void blk_crypto_sysfs_unregister(struct gendisk *disk)
  125. {
  126. kobject_put(disk->queue->crypto_kobject);
  127. }
  128. static int __init blk_crypto_sysfs_init(void)
  129. {
  130. int i;
  131. BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
  132. for (i = 1; i < BLK_ENCRYPTION_MODE_MAX; i++) {
  133. struct blk_crypto_attr *attr = &__blk_crypto_mode_attrs[i];
  134. attr->attr.name = blk_crypto_modes[i].name;
  135. attr->attr.mode = 0444;
  136. attr->show = blk_crypto_mode_show;
  137. blk_crypto_mode_attrs[i - 1] = &attr->attr;
  138. }
  139. return 0;
  140. }
  141. subsys_initcall(blk_crypto_sysfs_init);