11e8d44bdSEric Biggers // SPDX-License-Identifier: GPL-2.0
21e8d44bdSEric Biggers /*
31e8d44bdSEric Biggers * Copyright 2019 Google LLC
41e8d44bdSEric Biggers */
51e8d44bdSEric Biggers
61e8d44bdSEric Biggers /**
7cb77cb5aSEric Biggers * DOC: blk-crypto profiles
81e8d44bdSEric Biggers *
9cb77cb5aSEric Biggers * 'struct blk_crypto_profile' contains all generic inline encryption-related
10cb77cb5aSEric Biggers * state for a particular inline encryption device. blk_crypto_profile serves
11cb77cb5aSEric Biggers * as the way that drivers for inline encryption hardware expose their crypto
12cb77cb5aSEric Biggers * capabilities and certain functions (e.g., functions to program and evict
13cb77cb5aSEric Biggers * keys) to upper layers. Device drivers that want to support inline encryption
14cb77cb5aSEric Biggers * construct a crypto profile, then associate it with the disk's request_queue.
151e8d44bdSEric Biggers *
16cb77cb5aSEric Biggers * If the device has keyslots, then its blk_crypto_profile also handles managing
17cb77cb5aSEric Biggers * these keyslots in a device-independent way, using the driver-provided
18cb77cb5aSEric Biggers * functions to program and evict keys as needed. This includes keeping track
19cb77cb5aSEric Biggers * of which key and how many I/O requests are using each keyslot, getting
20cb77cb5aSEric Biggers * keyslots for I/O requests, and handling key eviction requests.
211e8d44bdSEric Biggers *
22cb77cb5aSEric Biggers * For more information, see Documentation/block/inline-encryption.rst.
231e8d44bdSEric Biggers */
241e8d44bdSEric Biggers
251e8d44bdSEric Biggers #define pr_fmt(fmt) "blk-crypto: " fmt
261e8d44bdSEric Biggers
271e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
281e8d44bdSEric Biggers #include <linux/device.h>
291e8d44bdSEric Biggers #include <linux/atomic.h>
301e8d44bdSEric Biggers #include <linux/mutex.h>
311e8d44bdSEric Biggers #include <linux/pm_runtime.h>
321e8d44bdSEric Biggers #include <linux/wait.h>
331e8d44bdSEric Biggers #include <linux/blkdev.h>
341e8d44bdSEric Biggers #include <linux/blk-integrity.h>
3585168d41SBart Van Assche #include "blk-crypto-internal.h"
361e8d44bdSEric Biggers
37cb77cb5aSEric Biggers struct blk_crypto_keyslot {
381e8d44bdSEric Biggers atomic_t slot_refs;
391e8d44bdSEric Biggers struct list_head idle_slot_node;
401e8d44bdSEric Biggers struct hlist_node hash_node;
411e8d44bdSEric Biggers const struct blk_crypto_key *key;
42cb77cb5aSEric Biggers struct blk_crypto_profile *profile;
431e8d44bdSEric Biggers };
441e8d44bdSEric Biggers
blk_crypto_hw_enter(struct blk_crypto_profile * profile)45cb77cb5aSEric Biggers static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile)
461e8d44bdSEric Biggers {
471e8d44bdSEric Biggers /*
48cb77cb5aSEric Biggers * Calling into the driver requires profile->lock held and the device
491e8d44bdSEric Biggers * resumed. But we must resume the device first, since that can acquire
50cb77cb5aSEric Biggers * and release profile->lock via blk_crypto_reprogram_all_keys().
511e8d44bdSEric Biggers */
52cb77cb5aSEric Biggers if (profile->dev)
53cb77cb5aSEric Biggers pm_runtime_get_sync(profile->dev);
54cb77cb5aSEric Biggers down_write(&profile->lock);
551e8d44bdSEric Biggers }
561e8d44bdSEric Biggers
blk_crypto_hw_exit(struct blk_crypto_profile * profile)57cb77cb5aSEric Biggers static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile)
581e8d44bdSEric Biggers {
59cb77cb5aSEric Biggers up_write(&profile->lock);
60cb77cb5aSEric Biggers if (profile->dev)
61cb77cb5aSEric Biggers pm_runtime_put_sync(profile->dev);
621e8d44bdSEric Biggers }
631e8d44bdSEric Biggers
641e8d44bdSEric Biggers /**
65cb77cb5aSEric Biggers * blk_crypto_profile_init() - Initialize a blk_crypto_profile
66cb77cb5aSEric Biggers * @profile: the blk_crypto_profile to initialize
67cb77cb5aSEric Biggers * @num_slots: the number of keyslots
681e8d44bdSEric Biggers *
69cb77cb5aSEric Biggers * Storage drivers must call this when starting to set up a blk_crypto_profile,
70cb77cb5aSEric Biggers * before filling in additional fields.
711e8d44bdSEric Biggers *
721e8d44bdSEric Biggers * Return: 0 on success, or else a negative error code.
731e8d44bdSEric Biggers */
blk_crypto_profile_init(struct blk_crypto_profile * profile,unsigned int num_slots)74cb77cb5aSEric Biggers int blk_crypto_profile_init(struct blk_crypto_profile *profile,
75cb77cb5aSEric Biggers unsigned int num_slots)
761e8d44bdSEric Biggers {
771e8d44bdSEric Biggers unsigned int slot;
781e8d44bdSEric Biggers unsigned int i;
791e8d44bdSEric Biggers unsigned int slot_hashtable_size;
801e8d44bdSEric Biggers
81cb77cb5aSEric Biggers memset(profile, 0, sizeof(*profile));
82*2fb48d88SEric Biggers
83*2fb48d88SEric Biggers /*
84*2fb48d88SEric Biggers * profile->lock of an underlying device can nest inside profile->lock
85*2fb48d88SEric Biggers * of a device-mapper device, so use a dynamic lock class to avoid
86*2fb48d88SEric Biggers * false-positive lockdep reports.
87*2fb48d88SEric Biggers */
88*2fb48d88SEric Biggers lockdep_register_key(&profile->lockdep_key);
89*2fb48d88SEric Biggers __init_rwsem(&profile->lock, "&profile->lock", &profile->lockdep_key);
901e8d44bdSEric Biggers
911e8d44bdSEric Biggers if (num_slots == 0)
92cb77cb5aSEric Biggers return 0;
931e8d44bdSEric Biggers
94cb77cb5aSEric Biggers /* Initialize keyslot management data. */
95cb77cb5aSEric Biggers
96cb77cb5aSEric Biggers profile->slots = kvcalloc(num_slots, sizeof(profile->slots[0]),
97cb77cb5aSEric Biggers GFP_KERNEL);
98cb77cb5aSEric Biggers if (!profile->slots)
99*2fb48d88SEric Biggers goto err_destroy;
1001e8d44bdSEric Biggers
101cb77cb5aSEric Biggers profile->num_slots = num_slots;
1021e8d44bdSEric Biggers
103cb77cb5aSEric Biggers init_waitqueue_head(&profile->idle_slots_wait_queue);
104cb77cb5aSEric Biggers INIT_LIST_HEAD(&profile->idle_slots);
1051e8d44bdSEric Biggers
1061e8d44bdSEric Biggers for (slot = 0; slot < num_slots; slot++) {
107cb77cb5aSEric Biggers profile->slots[slot].profile = profile;
108cb77cb5aSEric Biggers list_add_tail(&profile->slots[slot].idle_slot_node,
109cb77cb5aSEric Biggers &profile->idle_slots);
1101e8d44bdSEric Biggers }
1111e8d44bdSEric Biggers
112cb77cb5aSEric Biggers spin_lock_init(&profile->idle_slots_lock);
1131e8d44bdSEric Biggers
1141e8d44bdSEric Biggers slot_hashtable_size = roundup_pow_of_two(num_slots);
1151e8d44bdSEric Biggers /*
1161e8d44bdSEric Biggers * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
1171e8d44bdSEric Biggers * buckets. This only makes a difference when there is only 1 keyslot.
1181e8d44bdSEric Biggers */
1191e8d44bdSEric Biggers if (slot_hashtable_size < 2)
1201e8d44bdSEric Biggers slot_hashtable_size = 2;
1211e8d44bdSEric Biggers
122cb77cb5aSEric Biggers profile->log_slot_ht_size = ilog2(slot_hashtable_size);
123cb77cb5aSEric Biggers profile->slot_hashtable =
124cb77cb5aSEric Biggers kvmalloc_array(slot_hashtable_size,
125cb77cb5aSEric Biggers sizeof(profile->slot_hashtable[0]), GFP_KERNEL);
126cb77cb5aSEric Biggers if (!profile->slot_hashtable)
127cb77cb5aSEric Biggers goto err_destroy;
1281e8d44bdSEric Biggers for (i = 0; i < slot_hashtable_size; i++)
129cb77cb5aSEric Biggers INIT_HLIST_HEAD(&profile->slot_hashtable[i]);
1301e8d44bdSEric Biggers
1311e8d44bdSEric Biggers return 0;
1321e8d44bdSEric Biggers
133cb77cb5aSEric Biggers err_destroy:
134cb77cb5aSEric Biggers blk_crypto_profile_destroy(profile);
1351e8d44bdSEric Biggers return -ENOMEM;
1361e8d44bdSEric Biggers }
137cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_profile_init);
1381e8d44bdSEric Biggers
blk_crypto_profile_destroy_callback(void * profile)139cb77cb5aSEric Biggers static void blk_crypto_profile_destroy_callback(void *profile)
1401e8d44bdSEric Biggers {
141cb77cb5aSEric Biggers blk_crypto_profile_destroy(profile);
1421e8d44bdSEric Biggers }
1431e8d44bdSEric Biggers
1441e8d44bdSEric Biggers /**
145cb77cb5aSEric Biggers * devm_blk_crypto_profile_init() - Resource-managed blk_crypto_profile_init()
146cb77cb5aSEric Biggers * @dev: the device which owns the blk_crypto_profile
147cb77cb5aSEric Biggers * @profile: the blk_crypto_profile to initialize
148cb77cb5aSEric Biggers * @num_slots: the number of keyslots
1491e8d44bdSEric Biggers *
150cb77cb5aSEric Biggers * Like blk_crypto_profile_init(), but causes blk_crypto_profile_destroy() to be
151cb77cb5aSEric Biggers * called automatically on driver detach.
1521e8d44bdSEric Biggers *
1531e8d44bdSEric Biggers * Return: 0 on success, or else a negative error code.
1541e8d44bdSEric Biggers */
devm_blk_crypto_profile_init(struct device * dev,struct blk_crypto_profile * profile,unsigned int num_slots)155cb77cb5aSEric Biggers int devm_blk_crypto_profile_init(struct device *dev,
156cb77cb5aSEric Biggers struct blk_crypto_profile *profile,
1571e8d44bdSEric Biggers unsigned int num_slots)
1581e8d44bdSEric Biggers {
159cb77cb5aSEric Biggers int err = blk_crypto_profile_init(profile, num_slots);
1601e8d44bdSEric Biggers
1611e8d44bdSEric Biggers if (err)
1621e8d44bdSEric Biggers return err;
1631e8d44bdSEric Biggers
164cb77cb5aSEric Biggers return devm_add_action_or_reset(dev,
165cb77cb5aSEric Biggers blk_crypto_profile_destroy_callback,
166cb77cb5aSEric Biggers profile);
1671e8d44bdSEric Biggers }
168cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(devm_blk_crypto_profile_init);
1691e8d44bdSEric Biggers
1701e8d44bdSEric Biggers static inline struct hlist_head *
blk_crypto_hash_bucket_for_key(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)171cb77cb5aSEric Biggers blk_crypto_hash_bucket_for_key(struct blk_crypto_profile *profile,
1721e8d44bdSEric Biggers const struct blk_crypto_key *key)
1731e8d44bdSEric Biggers {
174cb77cb5aSEric Biggers return &profile->slot_hashtable[
175cb77cb5aSEric Biggers hash_ptr(key, profile->log_slot_ht_size)];
1761e8d44bdSEric Biggers }
1771e8d44bdSEric Biggers
178cb77cb5aSEric Biggers static void
blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot * slot)179cb77cb5aSEric Biggers blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot *slot)
1801e8d44bdSEric Biggers {
181cb77cb5aSEric Biggers struct blk_crypto_profile *profile = slot->profile;
1821e8d44bdSEric Biggers unsigned long flags;
1831e8d44bdSEric Biggers
184cb77cb5aSEric Biggers spin_lock_irqsave(&profile->idle_slots_lock, flags);
1851e8d44bdSEric Biggers list_del(&slot->idle_slot_node);
186cb77cb5aSEric Biggers spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
1871e8d44bdSEric Biggers }
1881e8d44bdSEric Biggers
189cb77cb5aSEric Biggers static struct blk_crypto_keyslot *
blk_crypto_find_keyslot(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)190cb77cb5aSEric Biggers blk_crypto_find_keyslot(struct blk_crypto_profile *profile,
1911e8d44bdSEric Biggers const struct blk_crypto_key *key)
1921e8d44bdSEric Biggers {
193cb77cb5aSEric Biggers const struct hlist_head *head =
194cb77cb5aSEric Biggers blk_crypto_hash_bucket_for_key(profile, key);
195cb77cb5aSEric Biggers struct blk_crypto_keyslot *slotp;
1961e8d44bdSEric Biggers
1971e8d44bdSEric Biggers hlist_for_each_entry(slotp, head, hash_node) {
1981e8d44bdSEric Biggers if (slotp->key == key)
1991e8d44bdSEric Biggers return slotp;
2001e8d44bdSEric Biggers }
2011e8d44bdSEric Biggers return NULL;
2021e8d44bdSEric Biggers }
2031e8d44bdSEric Biggers
204cb77cb5aSEric Biggers static struct blk_crypto_keyslot *
blk_crypto_find_and_grab_keyslot(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)205cb77cb5aSEric Biggers blk_crypto_find_and_grab_keyslot(struct blk_crypto_profile *profile,
2061e8d44bdSEric Biggers const struct blk_crypto_key *key)
2071e8d44bdSEric Biggers {
208cb77cb5aSEric Biggers struct blk_crypto_keyslot *slot;
2091e8d44bdSEric Biggers
210cb77cb5aSEric Biggers slot = blk_crypto_find_keyslot(profile, key);
2111e8d44bdSEric Biggers if (!slot)
2121e8d44bdSEric Biggers return NULL;
2131e8d44bdSEric Biggers if (atomic_inc_return(&slot->slot_refs) == 1) {
2141e8d44bdSEric Biggers /* Took first reference to this slot; remove it from LRU list */
215cb77cb5aSEric Biggers blk_crypto_remove_slot_from_lru_list(slot);
2161e8d44bdSEric Biggers }
2171e8d44bdSEric Biggers return slot;
2181e8d44bdSEric Biggers }
2191e8d44bdSEric Biggers
220cb77cb5aSEric Biggers /**
221cb77cb5aSEric Biggers * blk_crypto_keyslot_index() - Get the index of a keyslot
222cb77cb5aSEric Biggers * @slot: a keyslot that blk_crypto_get_keyslot() returned
223cb77cb5aSEric Biggers *
224cb77cb5aSEric Biggers * Return: the 0-based index of the keyslot within the device's keyslots.
225cb77cb5aSEric Biggers */
blk_crypto_keyslot_index(struct blk_crypto_keyslot * slot)226cb77cb5aSEric Biggers unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot)
2271e8d44bdSEric Biggers {
228cb77cb5aSEric Biggers return slot - slot->profile->slots;
2291e8d44bdSEric Biggers }
230cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_keyslot_index);
2311e8d44bdSEric Biggers
2321e8d44bdSEric Biggers /**
233cb77cb5aSEric Biggers * blk_crypto_get_keyslot() - Get a keyslot for a key, if needed.
234cb77cb5aSEric Biggers * @profile: the crypto profile of the device the key will be used on
235cb77cb5aSEric Biggers * @key: the key that will be used
236cb77cb5aSEric Biggers * @slot_ptr: If a keyslot is allocated, an opaque pointer to the keyslot struct
2374cf2c3abSEric Biggers * will be stored here. blk_crypto_put_keyslot() must be called
2384cf2c3abSEric Biggers * later to release it. Otherwise, NULL will be stored here.
2391e8d44bdSEric Biggers *
240cb77cb5aSEric Biggers * If the device has keyslots, this gets a keyslot that's been programmed with
241cb77cb5aSEric Biggers * the specified key. If the key is already in a slot, this reuses it;
242cb77cb5aSEric Biggers * otherwise this waits for a slot to become idle and programs the key into it.
2431e8d44bdSEric Biggers *
244cb77cb5aSEric Biggers * Context: Process context. Takes and releases profile->lock.
245cb77cb5aSEric Biggers * Return: BLK_STS_OK on success, meaning that either a keyslot was allocated or
246cb77cb5aSEric Biggers * one wasn't needed; or a blk_status_t error on failure.
2471e8d44bdSEric Biggers */
blk_crypto_get_keyslot(struct blk_crypto_profile * profile,const struct blk_crypto_key * key,struct blk_crypto_keyslot ** slot_ptr)248cb77cb5aSEric Biggers blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
2491e8d44bdSEric Biggers const struct blk_crypto_key *key,
250cb77cb5aSEric Biggers struct blk_crypto_keyslot **slot_ptr)
2511e8d44bdSEric Biggers {
252cb77cb5aSEric Biggers struct blk_crypto_keyslot *slot;
2531e8d44bdSEric Biggers int slot_idx;
2541e8d44bdSEric Biggers int err;
2551e8d44bdSEric Biggers
2561e8d44bdSEric Biggers *slot_ptr = NULL;
2571e8d44bdSEric Biggers
258cb77cb5aSEric Biggers /*
259cb77cb5aSEric Biggers * If the device has no concept of "keyslots", then there is no need to
260cb77cb5aSEric Biggers * get one.
261cb77cb5aSEric Biggers */
262cb77cb5aSEric Biggers if (profile->num_slots == 0)
2631e8d44bdSEric Biggers return BLK_STS_OK;
2641e8d44bdSEric Biggers
265cb77cb5aSEric Biggers down_read(&profile->lock);
266cb77cb5aSEric Biggers slot = blk_crypto_find_and_grab_keyslot(profile, key);
267cb77cb5aSEric Biggers up_read(&profile->lock);
2681e8d44bdSEric Biggers if (slot)
2691e8d44bdSEric Biggers goto success;
2701e8d44bdSEric Biggers
2711e8d44bdSEric Biggers for (;;) {
272cb77cb5aSEric Biggers blk_crypto_hw_enter(profile);
273cb77cb5aSEric Biggers slot = blk_crypto_find_and_grab_keyslot(profile, key);
2741e8d44bdSEric Biggers if (slot) {
275cb77cb5aSEric Biggers blk_crypto_hw_exit(profile);
2761e8d44bdSEric Biggers goto success;
2771e8d44bdSEric Biggers }
2781e8d44bdSEric Biggers
2791e8d44bdSEric Biggers /*
2801e8d44bdSEric Biggers * If we're here, that means there wasn't a slot that was
2811e8d44bdSEric Biggers * already programmed with the key. So try to program it.
2821e8d44bdSEric Biggers */
283cb77cb5aSEric Biggers if (!list_empty(&profile->idle_slots))
2841e8d44bdSEric Biggers break;
2851e8d44bdSEric Biggers
286cb77cb5aSEric Biggers blk_crypto_hw_exit(profile);
287cb77cb5aSEric Biggers wait_event(profile->idle_slots_wait_queue,
288cb77cb5aSEric Biggers !list_empty(&profile->idle_slots));
2891e8d44bdSEric Biggers }
2901e8d44bdSEric Biggers
291cb77cb5aSEric Biggers slot = list_first_entry(&profile->idle_slots, struct blk_crypto_keyslot,
2921e8d44bdSEric Biggers idle_slot_node);
293cb77cb5aSEric Biggers slot_idx = blk_crypto_keyslot_index(slot);
2941e8d44bdSEric Biggers
295cb77cb5aSEric Biggers err = profile->ll_ops.keyslot_program(profile, key, slot_idx);
2961e8d44bdSEric Biggers if (err) {
297cb77cb5aSEric Biggers wake_up(&profile->idle_slots_wait_queue);
298cb77cb5aSEric Biggers blk_crypto_hw_exit(profile);
2991e8d44bdSEric Biggers return errno_to_blk_status(err);
3001e8d44bdSEric Biggers }
3011e8d44bdSEric Biggers
3021e8d44bdSEric Biggers /* Move this slot to the hash list for the new key. */
3031e8d44bdSEric Biggers if (slot->key)
3041e8d44bdSEric Biggers hlist_del(&slot->hash_node);
3051e8d44bdSEric Biggers slot->key = key;
306cb77cb5aSEric Biggers hlist_add_head(&slot->hash_node,
307cb77cb5aSEric Biggers blk_crypto_hash_bucket_for_key(profile, key));
3081e8d44bdSEric Biggers
3091e8d44bdSEric Biggers atomic_set(&slot->slot_refs, 1);
3101e8d44bdSEric Biggers
311cb77cb5aSEric Biggers blk_crypto_remove_slot_from_lru_list(slot);
3121e8d44bdSEric Biggers
313cb77cb5aSEric Biggers blk_crypto_hw_exit(profile);
3141e8d44bdSEric Biggers success:
3151e8d44bdSEric Biggers *slot_ptr = slot;
3161e8d44bdSEric Biggers return BLK_STS_OK;
3171e8d44bdSEric Biggers }
3181e8d44bdSEric Biggers
3191e8d44bdSEric Biggers /**
320cb77cb5aSEric Biggers * blk_crypto_put_keyslot() - Release a reference to a keyslot
3214cf2c3abSEric Biggers * @slot: The keyslot to release the reference of
3221e8d44bdSEric Biggers *
3231e8d44bdSEric Biggers * Context: Any context.
3241e8d44bdSEric Biggers */
blk_crypto_put_keyslot(struct blk_crypto_keyslot * slot)325cb77cb5aSEric Biggers void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot)
3261e8d44bdSEric Biggers {
3274cf2c3abSEric Biggers struct blk_crypto_profile *profile = slot->profile;
3281e8d44bdSEric Biggers unsigned long flags;
3291e8d44bdSEric Biggers
3301e8d44bdSEric Biggers if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
331cb77cb5aSEric Biggers &profile->idle_slots_lock, flags)) {
332cb77cb5aSEric Biggers list_add_tail(&slot->idle_slot_node, &profile->idle_slots);
333cb77cb5aSEric Biggers spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
334cb77cb5aSEric Biggers wake_up(&profile->idle_slots_wait_queue);
3351e8d44bdSEric Biggers }
3361e8d44bdSEric Biggers }
3371e8d44bdSEric Biggers
3381e8d44bdSEric Biggers /**
339cb77cb5aSEric Biggers * __blk_crypto_cfg_supported() - Check whether the given crypto profile
340cb77cb5aSEric Biggers * supports the given crypto configuration.
341cb77cb5aSEric Biggers * @profile: the crypto profile to check
342cb77cb5aSEric Biggers * @cfg: the crypto configuration to check for
3431e8d44bdSEric Biggers *
344cb77cb5aSEric Biggers * Return: %true if @profile supports the given @cfg.
3451e8d44bdSEric Biggers */
__blk_crypto_cfg_supported(struct blk_crypto_profile * profile,const struct blk_crypto_config * cfg)346cb77cb5aSEric Biggers bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
3471e8d44bdSEric Biggers const struct blk_crypto_config *cfg)
3481e8d44bdSEric Biggers {
349cb77cb5aSEric Biggers if (!profile)
3501e8d44bdSEric Biggers return false;
351cb77cb5aSEric Biggers if (!(profile->modes_supported[cfg->crypto_mode] & cfg->data_unit_size))
3521e8d44bdSEric Biggers return false;
353cb77cb5aSEric Biggers if (profile->max_dun_bytes_supported < cfg->dun_bytes)
3541e8d44bdSEric Biggers return false;
3551e8d44bdSEric Biggers return true;
3561e8d44bdSEric Biggers }
3571e8d44bdSEric Biggers
3585c7cb944SEric Biggers /*
3595c7cb944SEric Biggers * This is an internal function that evicts a key from an inline encryption
3605c7cb944SEric Biggers * device that can be either a real device or the blk-crypto-fallback "device".
3615c7cb944SEric Biggers * It is used only by blk_crypto_evict_key(); see that function for details.
3621e8d44bdSEric Biggers */
__blk_crypto_evict_key(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)363cb77cb5aSEric Biggers int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
3641e8d44bdSEric Biggers const struct blk_crypto_key *key)
3651e8d44bdSEric Biggers {
366cb77cb5aSEric Biggers struct blk_crypto_keyslot *slot;
3675c7cb944SEric Biggers int err;
3681e8d44bdSEric Biggers
369cb77cb5aSEric Biggers if (profile->num_slots == 0) {
370cb77cb5aSEric Biggers if (profile->ll_ops.keyslot_evict) {
371cb77cb5aSEric Biggers blk_crypto_hw_enter(profile);
372cb77cb5aSEric Biggers err = profile->ll_ops.keyslot_evict(profile, key, -1);
373cb77cb5aSEric Biggers blk_crypto_hw_exit(profile);
3741e8d44bdSEric Biggers return err;
3751e8d44bdSEric Biggers }
3761e8d44bdSEric Biggers return 0;
3771e8d44bdSEric Biggers }
3781e8d44bdSEric Biggers
379cb77cb5aSEric Biggers blk_crypto_hw_enter(profile);
380cb77cb5aSEric Biggers slot = blk_crypto_find_keyslot(profile, key);
3815c7cb944SEric Biggers if (!slot) {
3825c7cb944SEric Biggers /*
3835c7cb944SEric Biggers * Not an error, since a key not in use by I/O is not guaranteed
3845c7cb944SEric Biggers * to be in a keyslot. There can be more keys than keyslots.
3855c7cb944SEric Biggers */
3865c7cb944SEric Biggers err = 0;
3875c7cb944SEric Biggers goto out;
3885c7cb944SEric Biggers }
3891e8d44bdSEric Biggers
3901e8d44bdSEric Biggers if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
3915c7cb944SEric Biggers /* BUG: key is still in use by I/O */
3921e8d44bdSEric Biggers err = -EBUSY;
3935c7cb944SEric Biggers goto out_remove;
3941e8d44bdSEric Biggers }
395cb77cb5aSEric Biggers err = profile->ll_ops.keyslot_evict(profile, key,
396cb77cb5aSEric Biggers blk_crypto_keyslot_index(slot));
3975c7cb944SEric Biggers out_remove:
3985c7cb944SEric Biggers /*
3995c7cb944SEric Biggers * Callers free the key even on error, so unlink the key from the hash
4005c7cb944SEric Biggers * table and clear slot->key even on error.
4015c7cb944SEric Biggers */
4021e8d44bdSEric Biggers hlist_del(&slot->hash_node);
4031e8d44bdSEric Biggers slot->key = NULL;
4045c7cb944SEric Biggers out:
405cb77cb5aSEric Biggers blk_crypto_hw_exit(profile);
4061e8d44bdSEric Biggers return err;
4071e8d44bdSEric Biggers }
4081e8d44bdSEric Biggers
4091e8d44bdSEric Biggers /**
410cb77cb5aSEric Biggers * blk_crypto_reprogram_all_keys() - Re-program all keyslots.
411cb77cb5aSEric Biggers * @profile: The crypto profile
4121e8d44bdSEric Biggers *
4131e8d44bdSEric Biggers * Re-program all keyslots that are supposed to have a key programmed. This is
4141e8d44bdSEric Biggers * intended only for use by drivers for hardware that loses its keys on reset.
4151e8d44bdSEric Biggers *
416cb77cb5aSEric Biggers * Context: Process context. Takes and releases profile->lock.
4171e8d44bdSEric Biggers */
blk_crypto_reprogram_all_keys(struct blk_crypto_profile * profile)418cb77cb5aSEric Biggers void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile)
4191e8d44bdSEric Biggers {
4201e8d44bdSEric Biggers unsigned int slot;
4211e8d44bdSEric Biggers
422cb77cb5aSEric Biggers if (profile->num_slots == 0)
4231e8d44bdSEric Biggers return;
4241e8d44bdSEric Biggers
4251e8d44bdSEric Biggers /* This is for device initialization, so don't resume the device */
426cb77cb5aSEric Biggers down_write(&profile->lock);
427cb77cb5aSEric Biggers for (slot = 0; slot < profile->num_slots; slot++) {
428cb77cb5aSEric Biggers const struct blk_crypto_key *key = profile->slots[slot].key;
4291e8d44bdSEric Biggers int err;
4301e8d44bdSEric Biggers
4311e8d44bdSEric Biggers if (!key)
4321e8d44bdSEric Biggers continue;
4331e8d44bdSEric Biggers
434cb77cb5aSEric Biggers err = profile->ll_ops.keyslot_program(profile, key, slot);
4351e8d44bdSEric Biggers WARN_ON(err);
4361e8d44bdSEric Biggers }
437cb77cb5aSEric Biggers up_write(&profile->lock);
4381e8d44bdSEric Biggers }
439cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_reprogram_all_keys);
4401e8d44bdSEric Biggers
blk_crypto_profile_destroy(struct blk_crypto_profile * profile)441cb77cb5aSEric Biggers void blk_crypto_profile_destroy(struct blk_crypto_profile *profile)
4421e8d44bdSEric Biggers {
443cb77cb5aSEric Biggers if (!profile)
4441e8d44bdSEric Biggers return;
445*2fb48d88SEric Biggers lockdep_unregister_key(&profile->lockdep_key);
446cb77cb5aSEric Biggers kvfree(profile->slot_hashtable);
447cb77cb5aSEric Biggers kvfree_sensitive(profile->slots,
448cb77cb5aSEric Biggers sizeof(profile->slots[0]) * profile->num_slots);
449cb77cb5aSEric Biggers memzero_explicit(profile, sizeof(*profile));
4501e8d44bdSEric Biggers }
451cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_profile_destroy);
4521e8d44bdSEric Biggers
blk_crypto_register(struct blk_crypto_profile * profile,struct request_queue * q)453cb77cb5aSEric Biggers bool blk_crypto_register(struct blk_crypto_profile *profile,
454cb77cb5aSEric Biggers struct request_queue *q)
4551e8d44bdSEric Biggers {
4561e8d44bdSEric Biggers if (blk_integrity_queue_supports_integrity(q)) {
4571e8d44bdSEric Biggers pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
4581e8d44bdSEric Biggers return false;
4591e8d44bdSEric Biggers }
460cb77cb5aSEric Biggers q->crypto_profile = profile;
4611e8d44bdSEric Biggers return true;
4621e8d44bdSEric Biggers }
463cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_register);
4641e8d44bdSEric Biggers
4651e8d44bdSEric Biggers /**
466cb77cb5aSEric Biggers * blk_crypto_intersect_capabilities() - restrict supported crypto capabilities
467cb77cb5aSEric Biggers * by child device
468cb77cb5aSEric Biggers * @parent: the crypto profile for the parent device
469cb77cb5aSEric Biggers * @child: the crypto profile for the child device, or NULL
4701e8d44bdSEric Biggers *
471cb77cb5aSEric Biggers * This clears all crypto capabilities in @parent that aren't set in @child. If
472cb77cb5aSEric Biggers * @child is NULL, then this clears all parent capabilities.
4731e8d44bdSEric Biggers *
474cb77cb5aSEric Biggers * Only use this when setting up the crypto profile for a layered device, before
475cb77cb5aSEric Biggers * it's been exposed yet.
4761e8d44bdSEric Biggers */
blk_crypto_intersect_capabilities(struct blk_crypto_profile * parent,const struct blk_crypto_profile * child)477cb77cb5aSEric Biggers void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent,
478cb77cb5aSEric Biggers const struct blk_crypto_profile *child)
4791e8d44bdSEric Biggers {
4801e8d44bdSEric Biggers if (child) {
4811e8d44bdSEric Biggers unsigned int i;
4821e8d44bdSEric Biggers
4831e8d44bdSEric Biggers parent->max_dun_bytes_supported =
4841e8d44bdSEric Biggers min(parent->max_dun_bytes_supported,
4851e8d44bdSEric Biggers child->max_dun_bytes_supported);
486cb77cb5aSEric Biggers for (i = 0; i < ARRAY_SIZE(child->modes_supported); i++)
487cb77cb5aSEric Biggers parent->modes_supported[i] &= child->modes_supported[i];
4881e8d44bdSEric Biggers } else {
4891e8d44bdSEric Biggers parent->max_dun_bytes_supported = 0;
490cb77cb5aSEric Biggers memset(parent->modes_supported, 0,
491cb77cb5aSEric Biggers sizeof(parent->modes_supported));
4921e8d44bdSEric Biggers }
4931e8d44bdSEric Biggers }
494cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_intersect_capabilities);
4951e8d44bdSEric Biggers
4961e8d44bdSEric Biggers /**
497cb77cb5aSEric Biggers * blk_crypto_has_capabilities() - Check whether @target supports at least all
498cb77cb5aSEric Biggers * the crypto capabilities that @reference does.
499cb77cb5aSEric Biggers * @target: the target profile
500cb77cb5aSEric Biggers * @reference: the reference profile
5011e8d44bdSEric Biggers *
502cb77cb5aSEric Biggers * Return: %true if @target supports all the crypto capabilities of @reference.
5031e8d44bdSEric Biggers */
blk_crypto_has_capabilities(const struct blk_crypto_profile * target,const struct blk_crypto_profile * reference)504cb77cb5aSEric Biggers bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target,
505cb77cb5aSEric Biggers const struct blk_crypto_profile *reference)
5061e8d44bdSEric Biggers {
5071e8d44bdSEric Biggers int i;
5081e8d44bdSEric Biggers
509cb77cb5aSEric Biggers if (!reference)
5101e8d44bdSEric Biggers return true;
5111e8d44bdSEric Biggers
512cb77cb5aSEric Biggers if (!target)
5131e8d44bdSEric Biggers return false;
5141e8d44bdSEric Biggers
515cb77cb5aSEric Biggers for (i = 0; i < ARRAY_SIZE(target->modes_supported); i++) {
516cb77cb5aSEric Biggers if (reference->modes_supported[i] & ~target->modes_supported[i])
5171e8d44bdSEric Biggers return false;
5181e8d44bdSEric Biggers }
5191e8d44bdSEric Biggers
520cb77cb5aSEric Biggers if (reference->max_dun_bytes_supported >
521cb77cb5aSEric Biggers target->max_dun_bytes_supported)
5221e8d44bdSEric Biggers return false;
5231e8d44bdSEric Biggers
5241e8d44bdSEric Biggers return true;
5251e8d44bdSEric Biggers }
526cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_has_capabilities);
5271e8d44bdSEric Biggers
5281e8d44bdSEric Biggers /**
529cb77cb5aSEric Biggers * blk_crypto_update_capabilities() - Update the capabilities of a crypto
530cb77cb5aSEric Biggers * profile to match those of another crypto
531cb77cb5aSEric Biggers * profile.
532cb77cb5aSEric Biggers * @dst: The crypto profile whose capabilities to update.
533cb77cb5aSEric Biggers * @src: The crypto profile whose capabilities this function will update @dst's
534cb77cb5aSEric Biggers * capabilities to.
5351e8d44bdSEric Biggers *
5361e8d44bdSEric Biggers * Blk-crypto requires that crypto capabilities that were
5371e8d44bdSEric Biggers * advertised when a bio was created continue to be supported by the
5381e8d44bdSEric Biggers * device until that bio is ended. This is turn means that a device cannot
5391e8d44bdSEric Biggers * shrink its advertised crypto capabilities without any explicit
5401e8d44bdSEric Biggers * synchronization with upper layers. So if there's no such explicit
541cb77cb5aSEric Biggers * synchronization, @src must support all the crypto capabilities that
542cb77cb5aSEric Biggers * @dst does (i.e. we need blk_crypto_has_capabilities(@src, @dst)).
5431e8d44bdSEric Biggers *
5441e8d44bdSEric Biggers * Note also that as long as the crypto capabilities are being expanded, the
5451e8d44bdSEric Biggers * order of updates becoming visible is not important because it's alright
5461e8d44bdSEric Biggers * for blk-crypto to see stale values - they only cause blk-crypto to
5471e8d44bdSEric Biggers * believe that a crypto capability isn't supported when it actually is (which
5481e8d44bdSEric Biggers * might result in blk-crypto-fallback being used if available, or the bio being
5491e8d44bdSEric Biggers * failed).
5501e8d44bdSEric Biggers */
blk_crypto_update_capabilities(struct blk_crypto_profile * dst,const struct blk_crypto_profile * src)551cb77cb5aSEric Biggers void blk_crypto_update_capabilities(struct blk_crypto_profile *dst,
552cb77cb5aSEric Biggers const struct blk_crypto_profile *src)
5531e8d44bdSEric Biggers {
554cb77cb5aSEric Biggers memcpy(dst->modes_supported, src->modes_supported,
555cb77cb5aSEric Biggers sizeof(dst->modes_supported));
5561e8d44bdSEric Biggers
557cb77cb5aSEric Biggers dst->max_dun_bytes_supported = src->max_dun_bytes_supported;
5581e8d44bdSEric Biggers }
559cb77cb5aSEric Biggers EXPORT_SYMBOL_GPL(blk_crypto_update_capabilities);
560