162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright 2019 Google LLC 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci/** 762306a36Sopenharmony_ci * DOC: blk-crypto profiles 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * 'struct blk_crypto_profile' contains all generic inline encryption-related 1062306a36Sopenharmony_ci * state for a particular inline encryption device. blk_crypto_profile serves 1162306a36Sopenharmony_ci * as the way that drivers for inline encryption hardware expose their crypto 1262306a36Sopenharmony_ci * capabilities and certain functions (e.g., functions to program and evict 1362306a36Sopenharmony_ci * keys) to upper layers. Device drivers that want to support inline encryption 1462306a36Sopenharmony_ci * construct a crypto profile, then associate it with the disk's request_queue. 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * If the device has keyslots, then its blk_crypto_profile also handles managing 1762306a36Sopenharmony_ci * these keyslots in a device-independent way, using the driver-provided 1862306a36Sopenharmony_ci * functions to program and evict keys as needed. This includes keeping track 1962306a36Sopenharmony_ci * of which key and how many I/O requests are using each keyslot, getting 2062306a36Sopenharmony_ci * keyslots for I/O requests, and handling key eviction requests. 2162306a36Sopenharmony_ci * 2262306a36Sopenharmony_ci * For more information, see Documentation/block/inline-encryption.rst. 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#define pr_fmt(fmt) "blk-crypto: " fmt 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#include <linux/blk-crypto-profile.h> 2862306a36Sopenharmony_ci#include <linux/device.h> 2962306a36Sopenharmony_ci#include <linux/atomic.h> 3062306a36Sopenharmony_ci#include <linux/mutex.h> 3162306a36Sopenharmony_ci#include <linux/pm_runtime.h> 3262306a36Sopenharmony_ci#include <linux/wait.h> 3362306a36Sopenharmony_ci#include <linux/blkdev.h> 3462306a36Sopenharmony_ci#include <linux/blk-integrity.h> 3562306a36Sopenharmony_ci#include "blk-crypto-internal.h" 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_cistruct blk_crypto_keyslot { 3862306a36Sopenharmony_ci atomic_t slot_refs; 3962306a36Sopenharmony_ci struct list_head idle_slot_node; 4062306a36Sopenharmony_ci struct hlist_node hash_node; 4162306a36Sopenharmony_ci const struct blk_crypto_key *key; 4262306a36Sopenharmony_ci struct blk_crypto_profile *profile; 4362306a36Sopenharmony_ci}; 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_cistatic inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile) 4662306a36Sopenharmony_ci{ 4762306a36Sopenharmony_ci /* 4862306a36Sopenharmony_ci * Calling into the driver requires profile->lock held and the device 4962306a36Sopenharmony_ci * resumed. But we must resume the device first, since that can acquire 5062306a36Sopenharmony_ci * and release profile->lock via blk_crypto_reprogram_all_keys(). 5162306a36Sopenharmony_ci */ 5262306a36Sopenharmony_ci if (profile->dev) 5362306a36Sopenharmony_ci pm_runtime_get_sync(profile->dev); 5462306a36Sopenharmony_ci down_write(&profile->lock); 5562306a36Sopenharmony_ci} 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistatic inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile) 5862306a36Sopenharmony_ci{ 5962306a36Sopenharmony_ci up_write(&profile->lock); 6062306a36Sopenharmony_ci if (profile->dev) 6162306a36Sopenharmony_ci pm_runtime_put_sync(profile->dev); 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci/** 6562306a36Sopenharmony_ci * blk_crypto_profile_init() - Initialize a blk_crypto_profile 6662306a36Sopenharmony_ci * @profile: the blk_crypto_profile to initialize 6762306a36Sopenharmony_ci * @num_slots: the number of keyslots 6862306a36Sopenharmony_ci * 6962306a36Sopenharmony_ci * Storage drivers must call this when starting to set up a blk_crypto_profile, 7062306a36Sopenharmony_ci * before filling in additional fields. 7162306a36Sopenharmony_ci * 7262306a36Sopenharmony_ci * Return: 0 on success, or else a negative error code. 7362306a36Sopenharmony_ci */ 7462306a36Sopenharmony_ciint blk_crypto_profile_init(struct blk_crypto_profile *profile, 7562306a36Sopenharmony_ci unsigned int num_slots) 7662306a36Sopenharmony_ci{ 7762306a36Sopenharmony_ci unsigned int slot; 7862306a36Sopenharmony_ci unsigned int i; 7962306a36Sopenharmony_ci unsigned int slot_hashtable_size; 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci memset(profile, 0, sizeof(*profile)); 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci /* 8462306a36Sopenharmony_ci * profile->lock of an underlying device can nest inside profile->lock 8562306a36Sopenharmony_ci * of a device-mapper device, so use a dynamic lock class to avoid 8662306a36Sopenharmony_ci * false-positive lockdep reports. 8762306a36Sopenharmony_ci */ 8862306a36Sopenharmony_ci lockdep_register_key(&profile->lockdep_key); 8962306a36Sopenharmony_ci __init_rwsem(&profile->lock, "&profile->lock", &profile->lockdep_key); 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_ci if (num_slots == 0) 9262306a36Sopenharmony_ci return 0; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci /* Initialize keyslot management data. */ 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci profile->slots = kvcalloc(num_slots, sizeof(profile->slots[0]), 9762306a36Sopenharmony_ci GFP_KERNEL); 9862306a36Sopenharmony_ci if (!profile->slots) 9962306a36Sopenharmony_ci goto err_destroy; 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci profile->num_slots = num_slots; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci init_waitqueue_head(&profile->idle_slots_wait_queue); 10462306a36Sopenharmony_ci INIT_LIST_HEAD(&profile->idle_slots); 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci for (slot = 0; slot < num_slots; slot++) { 10762306a36Sopenharmony_ci profile->slots[slot].profile = profile; 10862306a36Sopenharmony_ci list_add_tail(&profile->slots[slot].idle_slot_node, 10962306a36Sopenharmony_ci &profile->idle_slots); 11062306a36Sopenharmony_ci } 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci spin_lock_init(&profile->idle_slots_lock); 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci slot_hashtable_size = roundup_pow_of_two(num_slots); 11562306a36Sopenharmony_ci /* 11662306a36Sopenharmony_ci * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2 11762306a36Sopenharmony_ci * buckets. This only makes a difference when there is only 1 keyslot. 11862306a36Sopenharmony_ci */ 11962306a36Sopenharmony_ci if (slot_hashtable_size < 2) 12062306a36Sopenharmony_ci slot_hashtable_size = 2; 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ci profile->log_slot_ht_size = ilog2(slot_hashtable_size); 12362306a36Sopenharmony_ci profile->slot_hashtable = 12462306a36Sopenharmony_ci kvmalloc_array(slot_hashtable_size, 12562306a36Sopenharmony_ci sizeof(profile->slot_hashtable[0]), GFP_KERNEL); 12662306a36Sopenharmony_ci if (!profile->slot_hashtable) 12762306a36Sopenharmony_ci goto err_destroy; 12862306a36Sopenharmony_ci for (i = 0; i < slot_hashtable_size; i++) 12962306a36Sopenharmony_ci INIT_HLIST_HEAD(&profile->slot_hashtable[i]); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci return 0; 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_cierr_destroy: 13462306a36Sopenharmony_ci blk_crypto_profile_destroy(profile); 13562306a36Sopenharmony_ci return -ENOMEM; 13662306a36Sopenharmony_ci} 13762306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_profile_init); 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_cistatic void blk_crypto_profile_destroy_callback(void *profile) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci blk_crypto_profile_destroy(profile); 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci/** 14562306a36Sopenharmony_ci * devm_blk_crypto_profile_init() - Resource-managed blk_crypto_profile_init() 14662306a36Sopenharmony_ci * @dev: the device which owns the blk_crypto_profile 14762306a36Sopenharmony_ci * @profile: the blk_crypto_profile to initialize 14862306a36Sopenharmony_ci * @num_slots: the number of keyslots 14962306a36Sopenharmony_ci * 15062306a36Sopenharmony_ci * Like blk_crypto_profile_init(), but causes blk_crypto_profile_destroy() to be 15162306a36Sopenharmony_ci * called automatically on driver detach. 15262306a36Sopenharmony_ci * 15362306a36Sopenharmony_ci * Return: 0 on success, or else a negative error code. 15462306a36Sopenharmony_ci */ 15562306a36Sopenharmony_ciint devm_blk_crypto_profile_init(struct device *dev, 15662306a36Sopenharmony_ci struct blk_crypto_profile *profile, 15762306a36Sopenharmony_ci unsigned int num_slots) 15862306a36Sopenharmony_ci{ 15962306a36Sopenharmony_ci int err = blk_crypto_profile_init(profile, num_slots); 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci if (err) 16262306a36Sopenharmony_ci return err; 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci return devm_add_action_or_reset(dev, 16562306a36Sopenharmony_ci blk_crypto_profile_destroy_callback, 16662306a36Sopenharmony_ci profile); 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_blk_crypto_profile_init); 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_cistatic inline struct hlist_head * 17162306a36Sopenharmony_ciblk_crypto_hash_bucket_for_key(struct blk_crypto_profile *profile, 17262306a36Sopenharmony_ci const struct blk_crypto_key *key) 17362306a36Sopenharmony_ci{ 17462306a36Sopenharmony_ci return &profile->slot_hashtable[ 17562306a36Sopenharmony_ci hash_ptr(key, profile->log_slot_ht_size)]; 17662306a36Sopenharmony_ci} 17762306a36Sopenharmony_ci 17862306a36Sopenharmony_cistatic void 17962306a36Sopenharmony_ciblk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot *slot) 18062306a36Sopenharmony_ci{ 18162306a36Sopenharmony_ci struct blk_crypto_profile *profile = slot->profile; 18262306a36Sopenharmony_ci unsigned long flags; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci spin_lock_irqsave(&profile->idle_slots_lock, flags); 18562306a36Sopenharmony_ci list_del(&slot->idle_slot_node); 18662306a36Sopenharmony_ci spin_unlock_irqrestore(&profile->idle_slots_lock, flags); 18762306a36Sopenharmony_ci} 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_cistatic struct blk_crypto_keyslot * 19062306a36Sopenharmony_ciblk_crypto_find_keyslot(struct blk_crypto_profile *profile, 19162306a36Sopenharmony_ci const struct blk_crypto_key *key) 19262306a36Sopenharmony_ci{ 19362306a36Sopenharmony_ci const struct hlist_head *head = 19462306a36Sopenharmony_ci blk_crypto_hash_bucket_for_key(profile, key); 19562306a36Sopenharmony_ci struct blk_crypto_keyslot *slotp; 19662306a36Sopenharmony_ci 19762306a36Sopenharmony_ci hlist_for_each_entry(slotp, head, hash_node) { 19862306a36Sopenharmony_ci if (slotp->key == key) 19962306a36Sopenharmony_ci return slotp; 20062306a36Sopenharmony_ci } 20162306a36Sopenharmony_ci return NULL; 20262306a36Sopenharmony_ci} 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_cistatic struct blk_crypto_keyslot * 20562306a36Sopenharmony_ciblk_crypto_find_and_grab_keyslot(struct blk_crypto_profile *profile, 20662306a36Sopenharmony_ci const struct blk_crypto_key *key) 20762306a36Sopenharmony_ci{ 20862306a36Sopenharmony_ci struct blk_crypto_keyslot *slot; 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci slot = blk_crypto_find_keyslot(profile, key); 21162306a36Sopenharmony_ci if (!slot) 21262306a36Sopenharmony_ci return NULL; 21362306a36Sopenharmony_ci if (atomic_inc_return(&slot->slot_refs) == 1) { 21462306a36Sopenharmony_ci /* Took first reference to this slot; remove it from LRU list */ 21562306a36Sopenharmony_ci blk_crypto_remove_slot_from_lru_list(slot); 21662306a36Sopenharmony_ci } 21762306a36Sopenharmony_ci return slot; 21862306a36Sopenharmony_ci} 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci/** 22162306a36Sopenharmony_ci * blk_crypto_keyslot_index() - Get the index of a keyslot 22262306a36Sopenharmony_ci * @slot: a keyslot that blk_crypto_get_keyslot() returned 22362306a36Sopenharmony_ci * 22462306a36Sopenharmony_ci * Return: the 0-based index of the keyslot within the device's keyslots. 22562306a36Sopenharmony_ci */ 22662306a36Sopenharmony_ciunsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot) 22762306a36Sopenharmony_ci{ 22862306a36Sopenharmony_ci return slot - slot->profile->slots; 22962306a36Sopenharmony_ci} 23062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_keyslot_index); 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci/** 23362306a36Sopenharmony_ci * blk_crypto_get_keyslot() - Get a keyslot for a key, if needed. 23462306a36Sopenharmony_ci * @profile: the crypto profile of the device the key will be used on 23562306a36Sopenharmony_ci * @key: the key that will be used 23662306a36Sopenharmony_ci * @slot_ptr: If a keyslot is allocated, an opaque pointer to the keyslot struct 23762306a36Sopenharmony_ci * will be stored here. blk_crypto_put_keyslot() must be called 23862306a36Sopenharmony_ci * later to release it. Otherwise, NULL will be stored here. 23962306a36Sopenharmony_ci * 24062306a36Sopenharmony_ci * If the device has keyslots, this gets a keyslot that's been programmed with 24162306a36Sopenharmony_ci * the specified key. If the key is already in a slot, this reuses it; 24262306a36Sopenharmony_ci * otherwise this waits for a slot to become idle and programs the key into it. 24362306a36Sopenharmony_ci * 24462306a36Sopenharmony_ci * Context: Process context. Takes and releases profile->lock. 24562306a36Sopenharmony_ci * Return: BLK_STS_OK on success, meaning that either a keyslot was allocated or 24662306a36Sopenharmony_ci * one wasn't needed; or a blk_status_t error on failure. 24762306a36Sopenharmony_ci */ 24862306a36Sopenharmony_ciblk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile, 24962306a36Sopenharmony_ci const struct blk_crypto_key *key, 25062306a36Sopenharmony_ci struct blk_crypto_keyslot **slot_ptr) 25162306a36Sopenharmony_ci{ 25262306a36Sopenharmony_ci struct blk_crypto_keyslot *slot; 25362306a36Sopenharmony_ci int slot_idx; 25462306a36Sopenharmony_ci int err; 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ci *slot_ptr = NULL; 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci /* 25962306a36Sopenharmony_ci * If the device has no concept of "keyslots", then there is no need to 26062306a36Sopenharmony_ci * get one. 26162306a36Sopenharmony_ci */ 26262306a36Sopenharmony_ci if (profile->num_slots == 0) 26362306a36Sopenharmony_ci return BLK_STS_OK; 26462306a36Sopenharmony_ci 26562306a36Sopenharmony_ci down_read(&profile->lock); 26662306a36Sopenharmony_ci slot = blk_crypto_find_and_grab_keyslot(profile, key); 26762306a36Sopenharmony_ci up_read(&profile->lock); 26862306a36Sopenharmony_ci if (slot) 26962306a36Sopenharmony_ci goto success; 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci for (;;) { 27262306a36Sopenharmony_ci blk_crypto_hw_enter(profile); 27362306a36Sopenharmony_ci slot = blk_crypto_find_and_grab_keyslot(profile, key); 27462306a36Sopenharmony_ci if (slot) { 27562306a36Sopenharmony_ci blk_crypto_hw_exit(profile); 27662306a36Sopenharmony_ci goto success; 27762306a36Sopenharmony_ci } 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci /* 28062306a36Sopenharmony_ci * If we're here, that means there wasn't a slot that was 28162306a36Sopenharmony_ci * already programmed with the key. So try to program it. 28262306a36Sopenharmony_ci */ 28362306a36Sopenharmony_ci if (!list_empty(&profile->idle_slots)) 28462306a36Sopenharmony_ci break; 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ci blk_crypto_hw_exit(profile); 28762306a36Sopenharmony_ci wait_event(profile->idle_slots_wait_queue, 28862306a36Sopenharmony_ci !list_empty(&profile->idle_slots)); 28962306a36Sopenharmony_ci } 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_ci slot = list_first_entry(&profile->idle_slots, struct blk_crypto_keyslot, 29262306a36Sopenharmony_ci idle_slot_node); 29362306a36Sopenharmony_ci slot_idx = blk_crypto_keyslot_index(slot); 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci err = profile->ll_ops.keyslot_program(profile, key, slot_idx); 29662306a36Sopenharmony_ci if (err) { 29762306a36Sopenharmony_ci wake_up(&profile->idle_slots_wait_queue); 29862306a36Sopenharmony_ci blk_crypto_hw_exit(profile); 29962306a36Sopenharmony_ci return errno_to_blk_status(err); 30062306a36Sopenharmony_ci } 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci /* Move this slot to the hash list for the new key. */ 30362306a36Sopenharmony_ci if (slot->key) 30462306a36Sopenharmony_ci hlist_del(&slot->hash_node); 30562306a36Sopenharmony_ci slot->key = key; 30662306a36Sopenharmony_ci hlist_add_head(&slot->hash_node, 30762306a36Sopenharmony_ci blk_crypto_hash_bucket_for_key(profile, key)); 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ci atomic_set(&slot->slot_refs, 1); 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci blk_crypto_remove_slot_from_lru_list(slot); 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci blk_crypto_hw_exit(profile); 31462306a36Sopenharmony_cisuccess: 31562306a36Sopenharmony_ci *slot_ptr = slot; 31662306a36Sopenharmony_ci return BLK_STS_OK; 31762306a36Sopenharmony_ci} 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ci/** 32062306a36Sopenharmony_ci * blk_crypto_put_keyslot() - Release a reference to a keyslot 32162306a36Sopenharmony_ci * @slot: The keyslot to release the reference of 32262306a36Sopenharmony_ci * 32362306a36Sopenharmony_ci * Context: Any context. 32462306a36Sopenharmony_ci */ 32562306a36Sopenharmony_civoid blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot) 32662306a36Sopenharmony_ci{ 32762306a36Sopenharmony_ci struct blk_crypto_profile *profile = slot->profile; 32862306a36Sopenharmony_ci unsigned long flags; 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ci if (atomic_dec_and_lock_irqsave(&slot->slot_refs, 33162306a36Sopenharmony_ci &profile->idle_slots_lock, flags)) { 33262306a36Sopenharmony_ci list_add_tail(&slot->idle_slot_node, &profile->idle_slots); 33362306a36Sopenharmony_ci spin_unlock_irqrestore(&profile->idle_slots_lock, flags); 33462306a36Sopenharmony_ci wake_up(&profile->idle_slots_wait_queue); 33562306a36Sopenharmony_ci } 33662306a36Sopenharmony_ci} 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci/** 33962306a36Sopenharmony_ci * __blk_crypto_cfg_supported() - Check whether the given crypto profile 34062306a36Sopenharmony_ci * supports the given crypto configuration. 34162306a36Sopenharmony_ci * @profile: the crypto profile to check 34262306a36Sopenharmony_ci * @cfg: the crypto configuration to check for 34362306a36Sopenharmony_ci * 34462306a36Sopenharmony_ci * Return: %true if @profile supports the given @cfg. 34562306a36Sopenharmony_ci */ 34662306a36Sopenharmony_cibool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile, 34762306a36Sopenharmony_ci const struct blk_crypto_config *cfg) 34862306a36Sopenharmony_ci{ 34962306a36Sopenharmony_ci if (!profile) 35062306a36Sopenharmony_ci return false; 35162306a36Sopenharmony_ci if (!(profile->modes_supported[cfg->crypto_mode] & cfg->data_unit_size)) 35262306a36Sopenharmony_ci return false; 35362306a36Sopenharmony_ci if (profile->max_dun_bytes_supported < cfg->dun_bytes) 35462306a36Sopenharmony_ci return false; 35562306a36Sopenharmony_ci return true; 35662306a36Sopenharmony_ci} 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci/* 35962306a36Sopenharmony_ci * This is an internal function that evicts a key from an inline encryption 36062306a36Sopenharmony_ci * device that can be either a real device or the blk-crypto-fallback "device". 36162306a36Sopenharmony_ci * It is used only by blk_crypto_evict_key(); see that function for details. 36262306a36Sopenharmony_ci */ 36362306a36Sopenharmony_ciint __blk_crypto_evict_key(struct blk_crypto_profile *profile, 36462306a36Sopenharmony_ci const struct blk_crypto_key *key) 36562306a36Sopenharmony_ci{ 36662306a36Sopenharmony_ci struct blk_crypto_keyslot *slot; 36762306a36Sopenharmony_ci int err; 36862306a36Sopenharmony_ci 36962306a36Sopenharmony_ci if (profile->num_slots == 0) { 37062306a36Sopenharmony_ci if (profile->ll_ops.keyslot_evict) { 37162306a36Sopenharmony_ci blk_crypto_hw_enter(profile); 37262306a36Sopenharmony_ci err = profile->ll_ops.keyslot_evict(profile, key, -1); 37362306a36Sopenharmony_ci blk_crypto_hw_exit(profile); 37462306a36Sopenharmony_ci return err; 37562306a36Sopenharmony_ci } 37662306a36Sopenharmony_ci return 0; 37762306a36Sopenharmony_ci } 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci blk_crypto_hw_enter(profile); 38062306a36Sopenharmony_ci slot = blk_crypto_find_keyslot(profile, key); 38162306a36Sopenharmony_ci if (!slot) { 38262306a36Sopenharmony_ci /* 38362306a36Sopenharmony_ci * Not an error, since a key not in use by I/O is not guaranteed 38462306a36Sopenharmony_ci * to be in a keyslot. There can be more keys than keyslots. 38562306a36Sopenharmony_ci */ 38662306a36Sopenharmony_ci err = 0; 38762306a36Sopenharmony_ci goto out; 38862306a36Sopenharmony_ci } 38962306a36Sopenharmony_ci 39062306a36Sopenharmony_ci if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) { 39162306a36Sopenharmony_ci /* BUG: key is still in use by I/O */ 39262306a36Sopenharmony_ci err = -EBUSY; 39362306a36Sopenharmony_ci goto out_remove; 39462306a36Sopenharmony_ci } 39562306a36Sopenharmony_ci err = profile->ll_ops.keyslot_evict(profile, key, 39662306a36Sopenharmony_ci blk_crypto_keyslot_index(slot)); 39762306a36Sopenharmony_ciout_remove: 39862306a36Sopenharmony_ci /* 39962306a36Sopenharmony_ci * Callers free the key even on error, so unlink the key from the hash 40062306a36Sopenharmony_ci * table and clear slot->key even on error. 40162306a36Sopenharmony_ci */ 40262306a36Sopenharmony_ci hlist_del(&slot->hash_node); 40362306a36Sopenharmony_ci slot->key = NULL; 40462306a36Sopenharmony_ciout: 40562306a36Sopenharmony_ci blk_crypto_hw_exit(profile); 40662306a36Sopenharmony_ci return err; 40762306a36Sopenharmony_ci} 40862306a36Sopenharmony_ci 40962306a36Sopenharmony_ci/** 41062306a36Sopenharmony_ci * blk_crypto_reprogram_all_keys() - Re-program all keyslots. 41162306a36Sopenharmony_ci * @profile: The crypto profile 41262306a36Sopenharmony_ci * 41362306a36Sopenharmony_ci * Re-program all keyslots that are supposed to have a key programmed. This is 41462306a36Sopenharmony_ci * intended only for use by drivers for hardware that loses its keys on reset. 41562306a36Sopenharmony_ci * 41662306a36Sopenharmony_ci * Context: Process context. Takes and releases profile->lock. 41762306a36Sopenharmony_ci */ 41862306a36Sopenharmony_civoid blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile) 41962306a36Sopenharmony_ci{ 42062306a36Sopenharmony_ci unsigned int slot; 42162306a36Sopenharmony_ci 42262306a36Sopenharmony_ci if (profile->num_slots == 0) 42362306a36Sopenharmony_ci return; 42462306a36Sopenharmony_ci 42562306a36Sopenharmony_ci /* This is for device initialization, so don't resume the device */ 42662306a36Sopenharmony_ci down_write(&profile->lock); 42762306a36Sopenharmony_ci for (slot = 0; slot < profile->num_slots; slot++) { 42862306a36Sopenharmony_ci const struct blk_crypto_key *key = profile->slots[slot].key; 42962306a36Sopenharmony_ci int err; 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci if (!key) 43262306a36Sopenharmony_ci continue; 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci err = profile->ll_ops.keyslot_program(profile, key, slot); 43562306a36Sopenharmony_ci WARN_ON(err); 43662306a36Sopenharmony_ci } 43762306a36Sopenharmony_ci up_write(&profile->lock); 43862306a36Sopenharmony_ci} 43962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_reprogram_all_keys); 44062306a36Sopenharmony_ci 44162306a36Sopenharmony_civoid blk_crypto_profile_destroy(struct blk_crypto_profile *profile) 44262306a36Sopenharmony_ci{ 44362306a36Sopenharmony_ci if (!profile) 44462306a36Sopenharmony_ci return; 44562306a36Sopenharmony_ci lockdep_unregister_key(&profile->lockdep_key); 44662306a36Sopenharmony_ci kvfree(profile->slot_hashtable); 44762306a36Sopenharmony_ci kvfree_sensitive(profile->slots, 44862306a36Sopenharmony_ci sizeof(profile->slots[0]) * profile->num_slots); 44962306a36Sopenharmony_ci memzero_explicit(profile, sizeof(*profile)); 45062306a36Sopenharmony_ci} 45162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_profile_destroy); 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_cibool blk_crypto_register(struct blk_crypto_profile *profile, 45462306a36Sopenharmony_ci struct request_queue *q) 45562306a36Sopenharmony_ci{ 45662306a36Sopenharmony_ci if (blk_integrity_queue_supports_integrity(q)) { 45762306a36Sopenharmony_ci pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n"); 45862306a36Sopenharmony_ci return false; 45962306a36Sopenharmony_ci } 46062306a36Sopenharmony_ci q->crypto_profile = profile; 46162306a36Sopenharmony_ci return true; 46262306a36Sopenharmony_ci} 46362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_register); 46462306a36Sopenharmony_ci 46562306a36Sopenharmony_ci/** 46662306a36Sopenharmony_ci * blk_crypto_intersect_capabilities() - restrict supported crypto capabilities 46762306a36Sopenharmony_ci * by child device 46862306a36Sopenharmony_ci * @parent: the crypto profile for the parent device 46962306a36Sopenharmony_ci * @child: the crypto profile for the child device, or NULL 47062306a36Sopenharmony_ci * 47162306a36Sopenharmony_ci * This clears all crypto capabilities in @parent that aren't set in @child. If 47262306a36Sopenharmony_ci * @child is NULL, then this clears all parent capabilities. 47362306a36Sopenharmony_ci * 47462306a36Sopenharmony_ci * Only use this when setting up the crypto profile for a layered device, before 47562306a36Sopenharmony_ci * it's been exposed yet. 47662306a36Sopenharmony_ci */ 47762306a36Sopenharmony_civoid blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent, 47862306a36Sopenharmony_ci const struct blk_crypto_profile *child) 47962306a36Sopenharmony_ci{ 48062306a36Sopenharmony_ci if (child) { 48162306a36Sopenharmony_ci unsigned int i; 48262306a36Sopenharmony_ci 48362306a36Sopenharmony_ci parent->max_dun_bytes_supported = 48462306a36Sopenharmony_ci min(parent->max_dun_bytes_supported, 48562306a36Sopenharmony_ci child->max_dun_bytes_supported); 48662306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(child->modes_supported); i++) 48762306a36Sopenharmony_ci parent->modes_supported[i] &= child->modes_supported[i]; 48862306a36Sopenharmony_ci } else { 48962306a36Sopenharmony_ci parent->max_dun_bytes_supported = 0; 49062306a36Sopenharmony_ci memset(parent->modes_supported, 0, 49162306a36Sopenharmony_ci sizeof(parent->modes_supported)); 49262306a36Sopenharmony_ci } 49362306a36Sopenharmony_ci} 49462306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_intersect_capabilities); 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci/** 49762306a36Sopenharmony_ci * blk_crypto_has_capabilities() - Check whether @target supports at least all 49862306a36Sopenharmony_ci * the crypto capabilities that @reference does. 49962306a36Sopenharmony_ci * @target: the target profile 50062306a36Sopenharmony_ci * @reference: the reference profile 50162306a36Sopenharmony_ci * 50262306a36Sopenharmony_ci * Return: %true if @target supports all the crypto capabilities of @reference. 50362306a36Sopenharmony_ci */ 50462306a36Sopenharmony_cibool blk_crypto_has_capabilities(const struct blk_crypto_profile *target, 50562306a36Sopenharmony_ci const struct blk_crypto_profile *reference) 50662306a36Sopenharmony_ci{ 50762306a36Sopenharmony_ci int i; 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci if (!reference) 51062306a36Sopenharmony_ci return true; 51162306a36Sopenharmony_ci 51262306a36Sopenharmony_ci if (!target) 51362306a36Sopenharmony_ci return false; 51462306a36Sopenharmony_ci 51562306a36Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(target->modes_supported); i++) { 51662306a36Sopenharmony_ci if (reference->modes_supported[i] & ~target->modes_supported[i]) 51762306a36Sopenharmony_ci return false; 51862306a36Sopenharmony_ci } 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci if (reference->max_dun_bytes_supported > 52162306a36Sopenharmony_ci target->max_dun_bytes_supported) 52262306a36Sopenharmony_ci return false; 52362306a36Sopenharmony_ci 52462306a36Sopenharmony_ci return true; 52562306a36Sopenharmony_ci} 52662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_has_capabilities); 52762306a36Sopenharmony_ci 52862306a36Sopenharmony_ci/** 52962306a36Sopenharmony_ci * blk_crypto_update_capabilities() - Update the capabilities of a crypto 53062306a36Sopenharmony_ci * profile to match those of another crypto 53162306a36Sopenharmony_ci * profile. 53262306a36Sopenharmony_ci * @dst: The crypto profile whose capabilities to update. 53362306a36Sopenharmony_ci * @src: The crypto profile whose capabilities this function will update @dst's 53462306a36Sopenharmony_ci * capabilities to. 53562306a36Sopenharmony_ci * 53662306a36Sopenharmony_ci * Blk-crypto requires that crypto capabilities that were 53762306a36Sopenharmony_ci * advertised when a bio was created continue to be supported by the 53862306a36Sopenharmony_ci * device until that bio is ended. This is turn means that a device cannot 53962306a36Sopenharmony_ci * shrink its advertised crypto capabilities without any explicit 54062306a36Sopenharmony_ci * synchronization with upper layers. So if there's no such explicit 54162306a36Sopenharmony_ci * synchronization, @src must support all the crypto capabilities that 54262306a36Sopenharmony_ci * @dst does (i.e. we need blk_crypto_has_capabilities(@src, @dst)). 54362306a36Sopenharmony_ci * 54462306a36Sopenharmony_ci * Note also that as long as the crypto capabilities are being expanded, the 54562306a36Sopenharmony_ci * order of updates becoming visible is not important because it's alright 54662306a36Sopenharmony_ci * for blk-crypto to see stale values - they only cause blk-crypto to 54762306a36Sopenharmony_ci * believe that a crypto capability isn't supported when it actually is (which 54862306a36Sopenharmony_ci * might result in blk-crypto-fallback being used if available, or the bio being 54962306a36Sopenharmony_ci * failed). 55062306a36Sopenharmony_ci */ 55162306a36Sopenharmony_civoid blk_crypto_update_capabilities(struct blk_crypto_profile *dst, 55262306a36Sopenharmony_ci const struct blk_crypto_profile *src) 55362306a36Sopenharmony_ci{ 55462306a36Sopenharmony_ci memcpy(dst->modes_supported, src->modes_supported, 55562306a36Sopenharmony_ci sizeof(dst->modes_supported)); 55662306a36Sopenharmony_ci 55762306a36Sopenharmony_ci dst->max_dun_bytes_supported = src->max_dun_bytes_supported; 55862306a36Sopenharmony_ci} 55962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(blk_crypto_update_capabilities); 560