162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2021 Google LLC
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * sysfs support for blk-crypto.  This file contains the code which exports the
662306a36Sopenharmony_ci * crypto capabilities of devices via /sys/block/$disk/queue/crypto/.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/blk-crypto-profile.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include "blk-crypto-internal.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistruct blk_crypto_kobj {
1462306a36Sopenharmony_ci	struct kobject kobj;
1562306a36Sopenharmony_ci	struct blk_crypto_profile *profile;
1662306a36Sopenharmony_ci};
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistruct blk_crypto_attr {
1962306a36Sopenharmony_ci	struct attribute attr;
2062306a36Sopenharmony_ci	ssize_t (*show)(struct blk_crypto_profile *profile,
2162306a36Sopenharmony_ci			struct blk_crypto_attr *attr, char *page);
2262306a36Sopenharmony_ci};
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic struct blk_crypto_profile *kobj_to_crypto_profile(struct kobject *kobj)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	return container_of(kobj, struct blk_crypto_kobj, kobj)->profile;
2762306a36Sopenharmony_ci}
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_cistatic struct blk_crypto_attr *attr_to_crypto_attr(struct attribute *attr)
3062306a36Sopenharmony_ci{
3162306a36Sopenharmony_ci	return container_of(attr, struct blk_crypto_attr, attr);
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic ssize_t max_dun_bits_show(struct blk_crypto_profile *profile,
3562306a36Sopenharmony_ci				 struct blk_crypto_attr *attr, char *page)
3662306a36Sopenharmony_ci{
3762306a36Sopenharmony_ci	return sysfs_emit(page, "%u\n", 8 * profile->max_dun_bytes_supported);
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic ssize_t num_keyslots_show(struct blk_crypto_profile *profile,
4162306a36Sopenharmony_ci				 struct blk_crypto_attr *attr, char *page)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	return sysfs_emit(page, "%u\n", profile->num_slots);
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define BLK_CRYPTO_RO_ATTR(_name) \
4762306a36Sopenharmony_ci	static struct blk_crypto_attr _name##_attr = __ATTR_RO(_name)
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciBLK_CRYPTO_RO_ATTR(max_dun_bits);
5062306a36Sopenharmony_ciBLK_CRYPTO_RO_ATTR(num_keyslots);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic struct attribute *blk_crypto_attrs[] = {
5362306a36Sopenharmony_ci	&max_dun_bits_attr.attr,
5462306a36Sopenharmony_ci	&num_keyslots_attr.attr,
5562306a36Sopenharmony_ci	NULL,
5662306a36Sopenharmony_ci};
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic const struct attribute_group blk_crypto_attr_group = {
5962306a36Sopenharmony_ci	.attrs = blk_crypto_attrs,
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/*
6362306a36Sopenharmony_ci * The encryption mode attributes.  To avoid hard-coding the list of encryption
6462306a36Sopenharmony_ci * modes, these are initialized at boot time by blk_crypto_sysfs_init().
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_cistatic struct blk_crypto_attr __blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX];
6762306a36Sopenharmony_cistatic struct attribute *blk_crypto_mode_attrs[BLK_ENCRYPTION_MODE_MAX + 1];
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic umode_t blk_crypto_mode_is_visible(struct kobject *kobj,
7062306a36Sopenharmony_ci					  struct attribute *attr, int n)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
7362306a36Sopenharmony_ci	struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
7462306a36Sopenharmony_ci	int mode_num = a - __blk_crypto_mode_attrs;
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	if (profile->modes_supported[mode_num])
7762306a36Sopenharmony_ci		return 0444;
7862306a36Sopenharmony_ci	return 0;
7962306a36Sopenharmony_ci}
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_cistatic ssize_t blk_crypto_mode_show(struct blk_crypto_profile *profile,
8262306a36Sopenharmony_ci				    struct blk_crypto_attr *attr, char *page)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	int mode_num = attr - __blk_crypto_mode_attrs;
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci	return sysfs_emit(page, "0x%x\n", profile->modes_supported[mode_num]);
8762306a36Sopenharmony_ci}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_cistatic const struct attribute_group blk_crypto_modes_attr_group = {
9062306a36Sopenharmony_ci	.name = "modes",
9162306a36Sopenharmony_ci	.attrs = blk_crypto_mode_attrs,
9262306a36Sopenharmony_ci	.is_visible = blk_crypto_mode_is_visible,
9362306a36Sopenharmony_ci};
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_cistatic const struct attribute_group *blk_crypto_attr_groups[] = {
9662306a36Sopenharmony_ci	&blk_crypto_attr_group,
9762306a36Sopenharmony_ci	&blk_crypto_modes_attr_group,
9862306a36Sopenharmony_ci	NULL,
9962306a36Sopenharmony_ci};
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistatic ssize_t blk_crypto_attr_show(struct kobject *kobj,
10262306a36Sopenharmony_ci				    struct attribute *attr, char *page)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	struct blk_crypto_profile *profile = kobj_to_crypto_profile(kobj);
10562306a36Sopenharmony_ci	struct blk_crypto_attr *a = attr_to_crypto_attr(attr);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	return a->show(profile, a, page);
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic const struct sysfs_ops blk_crypto_attr_ops = {
11162306a36Sopenharmony_ci	.show = blk_crypto_attr_show,
11262306a36Sopenharmony_ci};
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic void blk_crypto_release(struct kobject *kobj)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	kfree(container_of(kobj, struct blk_crypto_kobj, kobj));
11762306a36Sopenharmony_ci}
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_cistatic const struct kobj_type blk_crypto_ktype = {
12062306a36Sopenharmony_ci	.default_groups = blk_crypto_attr_groups,
12162306a36Sopenharmony_ci	.sysfs_ops	= &blk_crypto_attr_ops,
12262306a36Sopenharmony_ci	.release	= blk_crypto_release,
12362306a36Sopenharmony_ci};
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/*
12662306a36Sopenharmony_ci * If the request_queue has a blk_crypto_profile, create the "crypto"
12762306a36Sopenharmony_ci * subdirectory in sysfs (/sys/block/$disk/queue/crypto/).
12862306a36Sopenharmony_ci */
12962306a36Sopenharmony_ciint blk_crypto_sysfs_register(struct gendisk *disk)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	struct request_queue *q = disk->queue;
13262306a36Sopenharmony_ci	struct blk_crypto_kobj *obj;
13362306a36Sopenharmony_ci	int err;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	if (!q->crypto_profile)
13662306a36Sopenharmony_ci		return 0;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
13962306a36Sopenharmony_ci	if (!obj)
14062306a36Sopenharmony_ci		return -ENOMEM;
14162306a36Sopenharmony_ci	obj->profile = q->crypto_profile;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	err = kobject_init_and_add(&obj->kobj, &blk_crypto_ktype,
14462306a36Sopenharmony_ci				   &disk->queue_kobj, "crypto");
14562306a36Sopenharmony_ci	if (err) {
14662306a36Sopenharmony_ci		kobject_put(&obj->kobj);
14762306a36Sopenharmony_ci		return err;
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci	q->crypto_kobject = &obj->kobj;
15062306a36Sopenharmony_ci	return 0;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_civoid blk_crypto_sysfs_unregister(struct gendisk *disk)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	kobject_put(disk->queue->crypto_kobject);
15662306a36Sopenharmony_ci}
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_cistatic int __init blk_crypto_sysfs_init(void)
15962306a36Sopenharmony_ci{
16062306a36Sopenharmony_ci	int i;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
16362306a36Sopenharmony_ci	for (i = 1; i < BLK_ENCRYPTION_MODE_MAX; i++) {
16462306a36Sopenharmony_ci		struct blk_crypto_attr *attr = &__blk_crypto_mode_attrs[i];
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci		attr->attr.name = blk_crypto_modes[i].name;
16762306a36Sopenharmony_ci		attr->attr.mode = 0444;
16862306a36Sopenharmony_ci		attr->show = blk_crypto_mode_show;
16962306a36Sopenharmony_ci		blk_crypto_mode_attrs[i - 1] = &attr->attr;
17062306a36Sopenharmony_ci	}
17162306a36Sopenharmony_ci	return 0;
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_cisubsys_initcall(blk_crypto_sysfs_init);
174