11e8d44bdSEric Biggers /* SPDX-License-Identifier: GPL-2.0 */ 21e8d44bdSEric Biggers /* 31e8d44bdSEric Biggers * Copyright 2019 Google LLC 41e8d44bdSEric Biggers */ 51e8d44bdSEric Biggers 6cb77cb5aSEric Biggers #ifndef __LINUX_BLK_CRYPTO_PROFILE_H 7cb77cb5aSEric Biggers #define __LINUX_BLK_CRYPTO_PROFILE_H 81e8d44bdSEric Biggers 91e8d44bdSEric Biggers #include <linux/bio.h> 101e8d44bdSEric Biggers #include <linux/blk-crypto.h> 111e8d44bdSEric Biggers 12cb77cb5aSEric Biggers struct blk_crypto_profile; 131e8d44bdSEric Biggers 141e8d44bdSEric Biggers /** 15cb77cb5aSEric Biggers * struct blk_crypto_ll_ops - functions to control inline encryption hardware 161e8d44bdSEric Biggers * 17cb77cb5aSEric Biggers * Low-level operations for controlling inline encryption hardware. This 18cb77cb5aSEric Biggers * interface must be implemented by storage drivers that support inline 19cb77cb5aSEric Biggers * encryption. All functions may sleep, are serialized by profile->lock, and 20cb77cb5aSEric Biggers * are never called while profile->dev (if set) is runtime-suspended. 211e8d44bdSEric Biggers */ 22cb77cb5aSEric Biggers struct blk_crypto_ll_ops { 23cb77cb5aSEric Biggers 24cb77cb5aSEric Biggers /** 25cb77cb5aSEric Biggers * @keyslot_program: Program a key into the inline encryption hardware. 26cb77cb5aSEric Biggers * 27cb77cb5aSEric Biggers * Program @key into the specified @slot in the inline encryption 28cb77cb5aSEric Biggers * hardware, overwriting any key that the keyslot may already contain. 29cb77cb5aSEric Biggers * The keyslot is guaranteed to not be in-use by any I/O. 30cb77cb5aSEric Biggers * 31cb77cb5aSEric Biggers * This is required if the device has keyslots. Otherwise (i.e. if the 32cb77cb5aSEric Biggers * device is a layered device, or if the device is real hardware that 33cb77cb5aSEric Biggers * simply doesn't have the concept of keyslots) it is never called. 34cb77cb5aSEric Biggers * 35cb77cb5aSEric Biggers * Must return 0 on success, or -errno on failure. 36cb77cb5aSEric Biggers */ 37cb77cb5aSEric Biggers int (*keyslot_program)(struct blk_crypto_profile *profile, 381e8d44bdSEric Biggers const struct blk_crypto_key *key, 391e8d44bdSEric Biggers unsigned int slot); 40cb77cb5aSEric Biggers 41cb77cb5aSEric Biggers /** 42cb77cb5aSEric Biggers * @keyslot_evict: Evict a key from the inline encryption hardware. 43cb77cb5aSEric Biggers * 44cb77cb5aSEric Biggers * If the device has keyslots, this function must evict the key from the 45cb77cb5aSEric Biggers * specified @slot. The slot will contain @key, but there should be no 46cb77cb5aSEric Biggers * need for the @key argument to be used as @slot should be sufficient. 47cb77cb5aSEric Biggers * The keyslot is guaranteed to not be in-use by any I/O. 48cb77cb5aSEric Biggers * 49cb77cb5aSEric Biggers * If the device doesn't have keyslots itself, this function must evict 50cb77cb5aSEric Biggers * @key from any underlying devices. @slot won't be valid in this case. 51cb77cb5aSEric Biggers * 52cb77cb5aSEric Biggers * If there are no keyslots and no underlying devices, this function 53cb77cb5aSEric Biggers * isn't required. 54cb77cb5aSEric Biggers * 55cb77cb5aSEric Biggers * Must return 0 on success, or -errno on failure. 56cb77cb5aSEric Biggers */ 57cb77cb5aSEric Biggers int (*keyslot_evict)(struct blk_crypto_profile *profile, 581e8d44bdSEric Biggers const struct blk_crypto_key *key, 591e8d44bdSEric Biggers unsigned int slot); 601e8d44bdSEric Biggers }; 611e8d44bdSEric Biggers 62cb77cb5aSEric Biggers /** 63cb77cb5aSEric Biggers * struct blk_crypto_profile - inline encryption profile for a device 64cb77cb5aSEric Biggers * 65cb77cb5aSEric Biggers * This struct contains a storage device's inline encryption capabilities (e.g. 66cb77cb5aSEric Biggers * the supported crypto algorithms), driver-provided functions to control the 67cb77cb5aSEric Biggers * inline encryption hardware (e.g. programming and evicting keys), and optional 68cb77cb5aSEric Biggers * device-independent keyslot management data. 691e8d44bdSEric Biggers */ 70cb77cb5aSEric Biggers struct blk_crypto_profile { 711e8d44bdSEric Biggers 72cb77cb5aSEric Biggers /* public: Drivers must initialize the following fields. */ 73cb77cb5aSEric Biggers 74cb77cb5aSEric Biggers /** 75cb77cb5aSEric Biggers * @ll_ops: Driver-provided functions to control the inline encryption 76cb77cb5aSEric Biggers * hardware, e.g. program and evict keys. 77cb77cb5aSEric Biggers */ 78cb77cb5aSEric Biggers struct blk_crypto_ll_ops ll_ops; 79cb77cb5aSEric Biggers 80cb77cb5aSEric Biggers /** 81cb77cb5aSEric Biggers * @max_dun_bytes_supported: The maximum number of bytes supported for 82cb77cb5aSEric Biggers * specifying the data unit number (DUN). Specifically, the range of 83cb77cb5aSEric Biggers * supported DUNs is 0 through (1 << (8 * max_dun_bytes_supported)) - 1. 841e8d44bdSEric Biggers */ 851e8d44bdSEric Biggers unsigned int max_dun_bytes_supported; 861e8d44bdSEric Biggers 87cb77cb5aSEric Biggers /** 88cb77cb5aSEric Biggers * @modes_supported: Array of bitmasks that specifies whether each 89cb77cb5aSEric Biggers * combination of crypto mode and data unit size is supported. 90cb77cb5aSEric Biggers * Specifically, the i'th bit of modes_supported[crypto_mode] is set if 91cb77cb5aSEric Biggers * crypto_mode can be used with a data unit size of (1 << i). Note that 92cb77cb5aSEric Biggers * only data unit sizes that are powers of 2 can be supported. 931e8d44bdSEric Biggers */ 94cb77cb5aSEric Biggers unsigned int modes_supported[BLK_ENCRYPTION_MODE_MAX]; 951e8d44bdSEric Biggers 96cb77cb5aSEric Biggers /** 97cb77cb5aSEric Biggers * @dev: An optional device for runtime power management. If the driver 98cb77cb5aSEric Biggers * provides this device, it will be runtime-resumed before any function 99cb77cb5aSEric Biggers * in @ll_ops is called and will remain resumed during the call. 100cb77cb5aSEric Biggers */ 1011e8d44bdSEric Biggers struct device *dev; 1021e8d44bdSEric Biggers 103cb77cb5aSEric Biggers /* private: The following fields shouldn't be accessed by drivers. */ 1041e8d44bdSEric Biggers 105cb77cb5aSEric Biggers /* Number of keyslots, or 0 if not applicable */ 1061e8d44bdSEric Biggers unsigned int num_slots; 1071e8d44bdSEric Biggers 108cb77cb5aSEric Biggers /* 109cb77cb5aSEric Biggers * Serializes all calls to functions in @ll_ops as well as all changes 110cb77cb5aSEric Biggers * to @slot_hashtable. This can also be taken in read mode to look up 111cb77cb5aSEric Biggers * keyslots while ensuring that they can't be changed concurrently. 112cb77cb5aSEric Biggers */ 1131e8d44bdSEric Biggers struct rw_semaphore lock; 114*2fb48d88SEric Biggers struct lock_class_key lockdep_key; 1151e8d44bdSEric Biggers 1161e8d44bdSEric Biggers /* List of idle slots, with least recently used slot at front */ 1171e8d44bdSEric Biggers wait_queue_head_t idle_slots_wait_queue; 1181e8d44bdSEric Biggers struct list_head idle_slots; 1191e8d44bdSEric Biggers spinlock_t idle_slots_lock; 1201e8d44bdSEric Biggers 1211e8d44bdSEric Biggers /* 1221e8d44bdSEric Biggers * Hash table which maps struct *blk_crypto_key to keyslots, so that we 1231e8d44bdSEric Biggers * can find a key's keyslot in O(1) time rather than O(num_slots). 1241e8d44bdSEric Biggers * Protected by 'lock'. 1251e8d44bdSEric Biggers */ 1261e8d44bdSEric Biggers struct hlist_head *slot_hashtable; 1271e8d44bdSEric Biggers unsigned int log_slot_ht_size; 1281e8d44bdSEric Biggers 1291e8d44bdSEric Biggers /* Per-keyslot data */ 130cb77cb5aSEric Biggers struct blk_crypto_keyslot *slots; 1311e8d44bdSEric Biggers }; 1321e8d44bdSEric Biggers 133cb77cb5aSEric Biggers int blk_crypto_profile_init(struct blk_crypto_profile *profile, 1341e8d44bdSEric Biggers unsigned int num_slots); 1351e8d44bdSEric Biggers 136cb77cb5aSEric Biggers int devm_blk_crypto_profile_init(struct device *dev, 137cb77cb5aSEric Biggers struct blk_crypto_profile *profile, 138cb77cb5aSEric Biggers unsigned int num_slots); 139cb77cb5aSEric Biggers 140cb77cb5aSEric Biggers unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot); 141cb77cb5aSEric Biggers 142cb77cb5aSEric Biggers void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile); 1431e8d44bdSEric Biggers 144cb77cb5aSEric Biggers void blk_crypto_profile_destroy(struct blk_crypto_profile *profile); 1451e8d44bdSEric Biggers 146cb77cb5aSEric Biggers void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent, 147cb77cb5aSEric Biggers const struct blk_crypto_profile *child); 1481e8d44bdSEric Biggers 149cb77cb5aSEric Biggers bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target, 150cb77cb5aSEric Biggers const struct blk_crypto_profile *reference); 1511e8d44bdSEric Biggers 152cb77cb5aSEric Biggers void blk_crypto_update_capabilities(struct blk_crypto_profile *dst, 153cb77cb5aSEric Biggers const struct blk_crypto_profile *src); 1541e8d44bdSEric Biggers 155cb77cb5aSEric Biggers #endif /* __LINUX_BLK_CRYPTO_PROFILE_H */ 156