|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright 2019 Google LLC |
| 4 | + */ |
| 5 | + |
| 6 | +#include "ufshcd.h" |
| 7 | +#include "ufshcd-crypto.h" |
| 8 | + |
| 9 | +/* Blk-crypto modes supported by UFS crypto */ |
| 10 | +static const struct ufs_crypto_alg_entry { |
| 11 | + enum ufs_crypto_alg ufs_alg; |
| 12 | + enum ufs_crypto_key_size ufs_key_size; |
| 13 | +} ufs_crypto_algs[BLK_ENCRYPTION_MODE_MAX] = { |
| 14 | + [BLK_ENCRYPTION_MODE_AES_256_XTS] = { |
| 15 | + .ufs_alg = UFS_CRYPTO_ALG_AES_XTS, |
| 16 | + .ufs_key_size = UFS_CRYPTO_KEY_SIZE_256, |
| 17 | + }, |
| 18 | +}; |
| 19 | + |
| 20 | +static void ufshcd_program_key(struct ufs_hba *hba, |
| 21 | + const union ufs_crypto_cfg_entry *cfg, |
| 22 | + int slot) |
| 23 | +{ |
| 24 | + int i; |
| 25 | + u32 slot_offset = hba->crypto_cfg_register + slot * sizeof(*cfg); |
| 26 | + |
| 27 | + ufshcd_hold(hba, false); |
| 28 | + /* Ensure that CFGE is cleared before programming the key */ |
| 29 | + ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0])); |
| 30 | + for (i = 0; i < 16; i++) { |
| 31 | + ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[i]), |
| 32 | + slot_offset + i * sizeof(cfg->reg_val[0])); |
| 33 | + } |
| 34 | + /* Write dword 17 */ |
| 35 | + ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[17]), |
| 36 | + slot_offset + 17 * sizeof(cfg->reg_val[0])); |
| 37 | + /* Dword 16 must be written last */ |
| 38 | + ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[16]), |
| 39 | + slot_offset + 16 * sizeof(cfg->reg_val[0])); |
| 40 | + ufshcd_release(hba); |
| 41 | +} |
| 42 | + |
| 43 | +static int ufshcd_crypto_keyslot_program(struct blk_keyslot_manager *ksm, |
| 44 | + const struct blk_crypto_key *key, |
| 45 | + unsigned int slot) |
| 46 | +{ |
| 47 | + struct ufs_hba *hba = container_of(ksm, struct ufs_hba, ksm); |
| 48 | + const union ufs_crypto_cap_entry *ccap_array = hba->crypto_cap_array; |
| 49 | + const struct ufs_crypto_alg_entry *alg = |
| 50 | + &ufs_crypto_algs[key->crypto_cfg.crypto_mode]; |
| 51 | + u8 data_unit_mask = key->crypto_cfg.data_unit_size / 512; |
| 52 | + int i; |
| 53 | + int cap_idx = -1; |
| 54 | + union ufs_crypto_cfg_entry cfg = { 0 }; |
| 55 | + |
| 56 | + BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0); |
| 57 | + for (i = 0; i < hba->crypto_capabilities.num_crypto_cap; i++) { |
| 58 | + if (ccap_array[i].algorithm_id == alg->ufs_alg && |
| 59 | + ccap_array[i].key_size == alg->ufs_key_size && |
| 60 | + (ccap_array[i].sdus_mask & data_unit_mask)) { |
| 61 | + cap_idx = i; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (WARN_ON(cap_idx < 0)) |
| 67 | + return -EOPNOTSUPP; |
| 68 | + |
| 69 | + cfg.data_unit_size = data_unit_mask; |
| 70 | + cfg.crypto_cap_idx = cap_idx; |
| 71 | + cfg.config_enable = UFS_CRYPTO_CONFIGURATION_ENABLE; |
| 72 | + |
| 73 | + if (ccap_array[cap_idx].algorithm_id == UFS_CRYPTO_ALG_AES_XTS) { |
| 74 | + /* In XTS mode, the blk_crypto_key's size is already doubled */ |
| 75 | + memcpy(cfg.crypto_key, key->raw, key->size/2); |
| 76 | + memcpy(cfg.crypto_key + UFS_CRYPTO_KEY_MAX_SIZE/2, |
| 77 | + key->raw + key->size/2, key->size/2); |
| 78 | + } else { |
| 79 | + memcpy(cfg.crypto_key, key->raw, key->size); |
| 80 | + } |
| 81 | + |
| 82 | + ufshcd_program_key(hba, &cfg, slot); |
| 83 | + |
| 84 | + memzero_explicit(&cfg, sizeof(cfg)); |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static void ufshcd_clear_keyslot(struct ufs_hba *hba, int slot) |
| 89 | +{ |
| 90 | + /* |
| 91 | + * Clear the crypto cfg on the device. Clearing CFGE |
| 92 | + * might not be sufficient, so just clear the entire cfg. |
| 93 | + */ |
| 94 | + union ufs_crypto_cfg_entry cfg = { 0 }; |
| 95 | + |
| 96 | + ufshcd_program_key(hba, &cfg, slot); |
| 97 | +} |
| 98 | + |
| 99 | +static int ufshcd_crypto_keyslot_evict(struct blk_keyslot_manager *ksm, |
| 100 | + const struct blk_crypto_key *key, |
| 101 | + unsigned int slot) |
| 102 | +{ |
| 103 | + struct ufs_hba *hba = container_of(ksm, struct ufs_hba, ksm); |
| 104 | + |
| 105 | + ufshcd_clear_keyslot(hba, slot); |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +bool ufshcd_crypto_enable(struct ufs_hba *hba) |
| 111 | +{ |
| 112 | + if (!(hba->caps & UFSHCD_CAP_CRYPTO)) |
| 113 | + return false; |
| 114 | + |
| 115 | + /* Reset might clear all keys, so reprogram all the keys. */ |
| 116 | + blk_ksm_reprogram_all_keys(&hba->ksm); |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +static const struct blk_ksm_ll_ops ufshcd_ksm_ops = { |
| 121 | + .keyslot_program = ufshcd_crypto_keyslot_program, |
| 122 | + .keyslot_evict = ufshcd_crypto_keyslot_evict, |
| 123 | +}; |
| 124 | + |
| 125 | +static enum blk_crypto_mode_num |
| 126 | +ufshcd_find_blk_crypto_mode(union ufs_crypto_cap_entry cap) |
| 127 | +{ |
| 128 | + int i; |
| 129 | + |
| 130 | + for (i = 0; i < ARRAY_SIZE(ufs_crypto_algs); i++) { |
| 131 | + BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0); |
| 132 | + if (ufs_crypto_algs[i].ufs_alg == cap.algorithm_id && |
| 133 | + ufs_crypto_algs[i].ufs_key_size == cap.key_size) { |
| 134 | + return i; |
| 135 | + } |
| 136 | + } |
| 137 | + return BLK_ENCRYPTION_MODE_INVALID; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * ufshcd_hba_init_crypto_capabilities - Read crypto capabilities, init crypto |
| 142 | + * fields in hba |
| 143 | + * @hba: Per adapter instance |
| 144 | + * |
| 145 | + * Return: 0 if crypto was initialized or is not supported, else a -errno value. |
| 146 | + */ |
| 147 | +int ufshcd_hba_init_crypto_capabilities(struct ufs_hba *hba) |
| 148 | +{ |
| 149 | + int cap_idx; |
| 150 | + int err = 0; |
| 151 | + enum blk_crypto_mode_num blk_mode_num; |
| 152 | + |
| 153 | + /* |
| 154 | + * Don't use crypto if either the hardware doesn't advertise the |
| 155 | + * standard crypto capability bit *or* if the vendor specific driver |
| 156 | + * hasn't advertised that crypto is supported. |
| 157 | + */ |
| 158 | + if (!(hba->capabilities & MASK_CRYPTO_SUPPORT) || |
| 159 | + !(hba->caps & UFSHCD_CAP_CRYPTO)) |
| 160 | + goto out; |
| 161 | + |
| 162 | + hba->crypto_capabilities.reg_val = |
| 163 | + cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP)); |
| 164 | + hba->crypto_cfg_register = |
| 165 | + (u32)hba->crypto_capabilities.config_array_ptr * 0x100; |
| 166 | + hba->crypto_cap_array = |
| 167 | + devm_kcalloc(hba->dev, hba->crypto_capabilities.num_crypto_cap, |
| 168 | + sizeof(hba->crypto_cap_array[0]), GFP_KERNEL); |
| 169 | + if (!hba->crypto_cap_array) { |
| 170 | + err = -ENOMEM; |
| 171 | + goto out; |
| 172 | + } |
| 173 | + |
| 174 | + /* The actual number of configurations supported is (CFGC+1) */ |
| 175 | + err = blk_ksm_init(&hba->ksm, |
| 176 | + hba->crypto_capabilities.config_count + 1); |
| 177 | + if (err) |
| 178 | + goto out_free_caps; |
| 179 | + |
| 180 | + hba->ksm.ksm_ll_ops = ufshcd_ksm_ops; |
| 181 | + /* UFS only supports 8 bytes for any DUN */ |
| 182 | + hba->ksm.max_dun_bytes_supported = 8; |
| 183 | + hba->ksm.dev = hba->dev; |
| 184 | + |
| 185 | + /* |
| 186 | + * Cache all the UFS crypto capabilities and advertise the supported |
| 187 | + * crypto modes and data unit sizes to the block layer. |
| 188 | + */ |
| 189 | + for (cap_idx = 0; cap_idx < hba->crypto_capabilities.num_crypto_cap; |
| 190 | + cap_idx++) { |
| 191 | + hba->crypto_cap_array[cap_idx].reg_val = |
| 192 | + cpu_to_le32(ufshcd_readl(hba, |
| 193 | + REG_UFS_CRYPTOCAP + |
| 194 | + cap_idx * sizeof(__le32))); |
| 195 | + blk_mode_num = ufshcd_find_blk_crypto_mode( |
| 196 | + hba->crypto_cap_array[cap_idx]); |
| 197 | + if (blk_mode_num != BLK_ENCRYPTION_MODE_INVALID) |
| 198 | + hba->ksm.crypto_modes_supported[blk_mode_num] |= |
| 199 | + hba->crypto_cap_array[cap_idx].sdus_mask * 512; |
| 200 | + } |
| 201 | + |
| 202 | + return 0; |
| 203 | + |
| 204 | +out_free_caps: |
| 205 | + devm_kfree(hba->dev, hba->crypto_cap_array); |
| 206 | +out: |
| 207 | + /* Indicate that init failed by clearing UFSHCD_CAP_CRYPTO */ |
| 208 | + hba->caps &= ~UFSHCD_CAP_CRYPTO; |
| 209 | + return err; |
| 210 | +} |
| 211 | + |
| 212 | +/** |
| 213 | + * ufshcd_init_crypto - Initialize crypto hardware |
| 214 | + * @hba: Per adapter instance |
| 215 | + */ |
| 216 | +void ufshcd_init_crypto(struct ufs_hba *hba) |
| 217 | +{ |
| 218 | + int slot; |
| 219 | + |
| 220 | + if (!(hba->caps & UFSHCD_CAP_CRYPTO)) |
| 221 | + return; |
| 222 | + |
| 223 | + /* Clear all keyslots - the number of keyslots is (CFGC + 1) */ |
| 224 | + for (slot = 0; slot < hba->crypto_capabilities.config_count + 1; slot++) |
| 225 | + ufshcd_clear_keyslot(hba, slot); |
| 226 | +} |
| 227 | + |
| 228 | +void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba, |
| 229 | + struct request_queue *q) |
| 230 | +{ |
| 231 | + if (hba->caps & UFSHCD_CAP_CRYPTO) |
| 232 | + blk_ksm_register(&hba->ksm, q); |
| 233 | +} |
| 234 | + |
| 235 | +void ufshcd_crypto_destroy_keyslot_manager(struct ufs_hba *hba) |
| 236 | +{ |
| 237 | + blk_ksm_destroy(&hba->ksm); |
| 238 | +} |
0 commit comments