.. | .. |
---|
3 | 3 | * Copyright 2019 Google LLC |
---|
4 | 4 | */ |
---|
5 | 5 | |
---|
6 | | -#include <linux/keyslot-manager.h> |
---|
7 | 6 | #include "ufshcd.h" |
---|
8 | 7 | #include "ufshcd-crypto.h" |
---|
9 | 8 | |
---|
10 | | -static bool ufshcd_cap_idx_valid(struct ufs_hba *hba, unsigned int cap_idx) |
---|
11 | | -{ |
---|
12 | | - return cap_idx < hba->crypto_capabilities.num_crypto_cap; |
---|
13 | | -} |
---|
| 9 | +#undef CREATE_TRACE_POINTS |
---|
| 10 | +#include <trace/hooks/ufshcd.h> |
---|
14 | 11 | |
---|
15 | | -static u8 get_data_unit_size_mask(unsigned int data_unit_size) |
---|
16 | | -{ |
---|
17 | | - if (data_unit_size < 512 || data_unit_size > 65536 || |
---|
18 | | - !is_power_of_2(data_unit_size)) |
---|
19 | | - return 0; |
---|
20 | | - |
---|
21 | | - return data_unit_size / 512; |
---|
22 | | -} |
---|
23 | | - |
---|
24 | | -static size_t get_keysize_bytes(enum ufs_crypto_key_size size) |
---|
25 | | -{ |
---|
26 | | - switch (size) { |
---|
27 | | - case UFS_CRYPTO_KEY_SIZE_128: |
---|
28 | | - return 16; |
---|
29 | | - case UFS_CRYPTO_KEY_SIZE_192: |
---|
30 | | - return 24; |
---|
31 | | - case UFS_CRYPTO_KEY_SIZE_256: |
---|
32 | | - return 32; |
---|
33 | | - case UFS_CRYPTO_KEY_SIZE_512: |
---|
34 | | - return 64; |
---|
35 | | - default: |
---|
36 | | - return 0; |
---|
37 | | - } |
---|
38 | | -} |
---|
39 | | - |
---|
40 | | -int ufshcd_crypto_cap_find(struct ufs_hba *hba, |
---|
41 | | - enum blk_crypto_mode_num crypto_mode, |
---|
42 | | - unsigned int data_unit_size) |
---|
43 | | -{ |
---|
| 12 | +/* Blk-crypto modes supported by UFS crypto */ |
---|
| 13 | +static const struct ufs_crypto_alg_entry { |
---|
44 | 14 | enum ufs_crypto_alg ufs_alg; |
---|
45 | | - u8 data_unit_mask; |
---|
46 | | - int cap_idx; |
---|
47 | 15 | enum ufs_crypto_key_size ufs_key_size; |
---|
48 | | - union ufs_crypto_cap_entry *ccap_array = hba->crypto_cap_array; |
---|
49 | | - |
---|
50 | | - if (!ufshcd_hba_is_crypto_supported(hba)) |
---|
51 | | - return -EINVAL; |
---|
52 | | - |
---|
53 | | - switch (crypto_mode) { |
---|
54 | | - case BLK_ENCRYPTION_MODE_AES_256_XTS: |
---|
55 | | - ufs_alg = UFS_CRYPTO_ALG_AES_XTS; |
---|
56 | | - ufs_key_size = UFS_CRYPTO_KEY_SIZE_256; |
---|
57 | | - break; |
---|
58 | | - default: |
---|
59 | | - return -EINVAL; |
---|
60 | | - } |
---|
61 | | - |
---|
62 | | - data_unit_mask = get_data_unit_size_mask(data_unit_size); |
---|
63 | | - |
---|
64 | | - for (cap_idx = 0; cap_idx < hba->crypto_capabilities.num_crypto_cap; |
---|
65 | | - cap_idx++) { |
---|
66 | | - if (ccap_array[cap_idx].algorithm_id == ufs_alg && |
---|
67 | | - (ccap_array[cap_idx].sdus_mask & data_unit_mask) && |
---|
68 | | - ccap_array[cap_idx].key_size == ufs_key_size) |
---|
69 | | - return cap_idx; |
---|
70 | | - } |
---|
71 | | - |
---|
72 | | - return -EINVAL; |
---|
73 | | -} |
---|
74 | | -EXPORT_SYMBOL_GPL(ufshcd_crypto_cap_find); |
---|
75 | | - |
---|
76 | | -/** |
---|
77 | | - * ufshcd_crypto_cfg_entry_write_key - Write a key into a crypto_cfg_entry |
---|
78 | | - * |
---|
79 | | - * Writes the key with the appropriate format - for AES_XTS, |
---|
80 | | - * the first half of the key is copied as is, the second half is |
---|
81 | | - * copied with an offset halfway into the cfg->crypto_key array. |
---|
82 | | - * For the other supported crypto algs, the key is just copied. |
---|
83 | | - * |
---|
84 | | - * @cfg: The crypto config to write to |
---|
85 | | - * @key: The key to write |
---|
86 | | - * @cap: The crypto capability (which specifies the crypto alg and key size) |
---|
87 | | - * |
---|
88 | | - * Returns 0 on success, or -EINVAL |
---|
89 | | - */ |
---|
90 | | -static int ufshcd_crypto_cfg_entry_write_key(union ufs_crypto_cfg_entry *cfg, |
---|
91 | | - const u8 *key, |
---|
92 | | - union ufs_crypto_cap_entry cap) |
---|
93 | | -{ |
---|
94 | | - size_t key_size_bytes = get_keysize_bytes(cap.key_size); |
---|
95 | | - |
---|
96 | | - if (key_size_bytes == 0) |
---|
97 | | - return -EINVAL; |
---|
98 | | - |
---|
99 | | - switch (cap.algorithm_id) { |
---|
100 | | - case UFS_CRYPTO_ALG_AES_XTS: |
---|
101 | | - key_size_bytes *= 2; |
---|
102 | | - if (key_size_bytes > UFS_CRYPTO_KEY_MAX_SIZE) |
---|
103 | | - return -EINVAL; |
---|
104 | | - |
---|
105 | | - memcpy(cfg->crypto_key, key, key_size_bytes/2); |
---|
106 | | - memcpy(cfg->crypto_key + UFS_CRYPTO_KEY_MAX_SIZE/2, |
---|
107 | | - key + key_size_bytes/2, key_size_bytes/2); |
---|
108 | | - return 0; |
---|
109 | | - case UFS_CRYPTO_ALG_BITLOCKER_AES_CBC: |
---|
110 | | - /* fall through */ |
---|
111 | | - case UFS_CRYPTO_ALG_AES_ECB: |
---|
112 | | - /* fall through */ |
---|
113 | | - case UFS_CRYPTO_ALG_ESSIV_AES_CBC: |
---|
114 | | - memcpy(cfg->crypto_key, key, key_size_bytes); |
---|
115 | | - return 0; |
---|
116 | | - } |
---|
117 | | - |
---|
118 | | - return -EINVAL; |
---|
119 | | -} |
---|
| 16 | +} ufs_crypto_algs[BLK_ENCRYPTION_MODE_MAX] = { |
---|
| 17 | + [BLK_ENCRYPTION_MODE_AES_256_XTS] = { |
---|
| 18 | + .ufs_alg = UFS_CRYPTO_ALG_AES_XTS, |
---|
| 19 | + .ufs_key_size = UFS_CRYPTO_KEY_SIZE_256, |
---|
| 20 | + }, |
---|
| 21 | +}; |
---|
120 | 22 | |
---|
121 | 23 | static int ufshcd_program_key(struct ufs_hba *hba, |
---|
122 | 24 | const union ufs_crypto_cfg_entry *cfg, int slot) |
---|
123 | 25 | { |
---|
124 | 26 | int i; |
---|
125 | 27 | u32 slot_offset = hba->crypto_cfg_register + slot * sizeof(*cfg); |
---|
126 | | - int err; |
---|
| 28 | + int err = 0; |
---|
127 | 29 | |
---|
128 | 30 | ufshcd_hold(hba, false); |
---|
129 | 31 | |
---|
130 | | - if (hba->vops->program_key) { |
---|
| 32 | + if (hba->vops && hba->vops->program_key) { |
---|
131 | 33 | err = hba->vops->program_key(hba, cfg, slot); |
---|
132 | 34 | goto out; |
---|
133 | 35 | } |
---|
134 | 36 | |
---|
135 | | - /* Clear the dword 16 */ |
---|
136 | | - ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0])); |
---|
137 | 37 | /* Ensure that CFGE is cleared before programming the key */ |
---|
138 | | - wmb(); |
---|
| 38 | + ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0])); |
---|
139 | 39 | for (i = 0; i < 16; i++) { |
---|
140 | 40 | ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[i]), |
---|
141 | 41 | slot_offset + i * sizeof(cfg->reg_val[0])); |
---|
142 | | - /* Spec says each dword in key must be written sequentially */ |
---|
143 | | - wmb(); |
---|
144 | 42 | } |
---|
145 | 43 | /* Write dword 17 */ |
---|
146 | 44 | ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[17]), |
---|
147 | 45 | slot_offset + 17 * sizeof(cfg->reg_val[0])); |
---|
148 | 46 | /* Dword 16 must be written last */ |
---|
149 | | - wmb(); |
---|
150 | | - /* Write dword 16 */ |
---|
151 | 47 | ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[16]), |
---|
152 | 48 | slot_offset + 16 * sizeof(cfg->reg_val[0])); |
---|
153 | | - wmb(); |
---|
154 | | - err = 0; |
---|
155 | 49 | out: |
---|
156 | 50 | ufshcd_release(hba); |
---|
157 | 51 | return err; |
---|
158 | 52 | } |
---|
159 | 53 | |
---|
160 | | -static void ufshcd_clear_keyslot(struct ufs_hba *hba, int slot) |
---|
161 | | -{ |
---|
162 | | - union ufs_crypto_cfg_entry cfg = { 0 }; |
---|
163 | | - int err; |
---|
164 | | - |
---|
165 | | - err = ufshcd_program_key(hba, &cfg, slot); |
---|
166 | | - WARN_ON_ONCE(err); |
---|
167 | | -} |
---|
168 | | - |
---|
169 | | -/* Clear all keyslots at driver init time */ |
---|
170 | | -static void ufshcd_clear_all_keyslots(struct ufs_hba *hba) |
---|
171 | | -{ |
---|
172 | | - int slot; |
---|
173 | | - |
---|
174 | | - for (slot = 0; slot < ufshcd_num_keyslots(hba); slot++) |
---|
175 | | - ufshcd_clear_keyslot(hba, slot); |
---|
176 | | -} |
---|
177 | | - |
---|
178 | | -static int ufshcd_crypto_keyslot_program(struct keyslot_manager *ksm, |
---|
| 54 | +static int ufshcd_crypto_keyslot_program(struct blk_keyslot_manager *ksm, |
---|
179 | 55 | const struct blk_crypto_key *key, |
---|
180 | 56 | unsigned int slot) |
---|
181 | 57 | { |
---|
182 | | - struct ufs_hba *hba = keyslot_manager_private(ksm); |
---|
183 | | - int err = 0; |
---|
184 | | - u8 data_unit_mask; |
---|
185 | | - union ufs_crypto_cfg_entry cfg; |
---|
186 | | - int cap_idx; |
---|
| 58 | + struct ufs_hba *hba = container_of(ksm, struct ufs_hba, ksm); |
---|
| 59 | + const union ufs_crypto_cap_entry *ccap_array = hba->crypto_cap_array; |
---|
| 60 | + const struct ufs_crypto_alg_entry *alg = |
---|
| 61 | + &ufs_crypto_algs[key->crypto_cfg.crypto_mode]; |
---|
| 62 | + u8 data_unit_mask = key->crypto_cfg.data_unit_size / 512; |
---|
| 63 | + int i; |
---|
| 64 | + int cap_idx = -1; |
---|
| 65 | + union ufs_crypto_cfg_entry cfg = {}; |
---|
| 66 | + int err; |
---|
187 | 67 | |
---|
188 | | - cap_idx = ufshcd_crypto_cap_find(hba, key->crypto_mode, |
---|
189 | | - key->data_unit_size); |
---|
| 68 | + BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0); |
---|
| 69 | + for (i = 0; i < hba->crypto_capabilities.num_crypto_cap; i++) { |
---|
| 70 | + if (ccap_array[i].algorithm_id == alg->ufs_alg && |
---|
| 71 | + ccap_array[i].key_size == alg->ufs_key_size && |
---|
| 72 | + (ccap_array[i].sdus_mask & data_unit_mask)) { |
---|
| 73 | + cap_idx = i; |
---|
| 74 | + break; |
---|
| 75 | + } |
---|
| 76 | + } |
---|
190 | 77 | |
---|
191 | | - if (!ufshcd_is_crypto_enabled(hba) || |
---|
192 | | - !ufshcd_keyslot_valid(hba, slot) || |
---|
193 | | - !ufshcd_cap_idx_valid(hba, cap_idx)) |
---|
194 | | - return -EINVAL; |
---|
| 78 | + if (WARN_ON(cap_idx < 0)) |
---|
| 79 | + return -EOPNOTSUPP; |
---|
195 | 80 | |
---|
196 | | - data_unit_mask = get_data_unit_size_mask(key->data_unit_size); |
---|
197 | | - |
---|
198 | | - if (!(data_unit_mask & hba->crypto_cap_array[cap_idx].sdus_mask)) |
---|
199 | | - return -EINVAL; |
---|
200 | | - |
---|
201 | | - memset(&cfg, 0, sizeof(cfg)); |
---|
202 | 81 | cfg.data_unit_size = data_unit_mask; |
---|
203 | 82 | cfg.crypto_cap_idx = cap_idx; |
---|
204 | | - cfg.config_enable |= UFS_CRYPTO_CONFIGURATION_ENABLE; |
---|
| 83 | + cfg.config_enable = UFS_CRYPTO_CONFIGURATION_ENABLE; |
---|
205 | 84 | |
---|
206 | | - err = ufshcd_crypto_cfg_entry_write_key(&cfg, key->raw, |
---|
207 | | - hba->crypto_cap_array[cap_idx]); |
---|
208 | | - if (err) |
---|
209 | | - return err; |
---|
| 85 | + if (ccap_array[cap_idx].algorithm_id == UFS_CRYPTO_ALG_AES_XTS) { |
---|
| 86 | + /* In XTS mode, the blk_crypto_key's size is already doubled */ |
---|
| 87 | + memcpy(cfg.crypto_key, key->raw, key->size/2); |
---|
| 88 | + memcpy(cfg.crypto_key + UFS_CRYPTO_KEY_MAX_SIZE/2, |
---|
| 89 | + key->raw + key->size/2, key->size/2); |
---|
| 90 | + } else { |
---|
| 91 | + memcpy(cfg.crypto_key, key->raw, key->size); |
---|
| 92 | + } |
---|
210 | 93 | |
---|
211 | 94 | err = ufshcd_program_key(hba, &cfg, slot); |
---|
212 | 95 | |
---|
213 | 96 | memzero_explicit(&cfg, sizeof(cfg)); |
---|
214 | | - |
---|
215 | 97 | return err; |
---|
216 | 98 | } |
---|
217 | 99 | |
---|
218 | | -static int ufshcd_crypto_keyslot_evict(struct keyslot_manager *ksm, |
---|
219 | | - const struct blk_crypto_key *key, |
---|
220 | | - unsigned int slot) |
---|
| 100 | +static int ufshcd_clear_keyslot(struct ufs_hba *hba, int slot) |
---|
221 | 101 | { |
---|
222 | | - struct ufs_hba *hba = keyslot_manager_private(ksm); |
---|
223 | | - |
---|
224 | | - if (!ufshcd_is_crypto_enabled(hba) || |
---|
225 | | - !ufshcd_keyslot_valid(hba, slot)) |
---|
226 | | - return -EINVAL; |
---|
227 | | - |
---|
228 | 102 | /* |
---|
229 | 103 | * Clear the crypto cfg on the device. Clearing CFGE |
---|
230 | 104 | * might not be sufficient, so just clear the entire cfg. |
---|
231 | 105 | */ |
---|
232 | | - ufshcd_clear_keyslot(hba, slot); |
---|
| 106 | + union ufs_crypto_cfg_entry cfg = {}; |
---|
233 | 107 | |
---|
234 | | - return 0; |
---|
| 108 | + return ufshcd_program_key(hba, &cfg, slot); |
---|
235 | 109 | } |
---|
236 | 110 | |
---|
237 | | -/* Functions implementing UFSHCI v2.1 specification behaviour */ |
---|
238 | | -void ufshcd_crypto_enable_spec(struct ufs_hba *hba) |
---|
| 111 | +static int ufshcd_crypto_keyslot_evict(struct blk_keyslot_manager *ksm, |
---|
| 112 | + const struct blk_crypto_key *key, |
---|
| 113 | + unsigned int slot) |
---|
239 | 114 | { |
---|
240 | | - if (!ufshcd_hba_is_crypto_supported(hba)) |
---|
241 | | - return; |
---|
| 115 | + struct ufs_hba *hba = container_of(ksm, struct ufs_hba, ksm); |
---|
242 | 116 | |
---|
243 | | - hba->caps |= UFSHCD_CAP_CRYPTO; |
---|
| 117 | + return ufshcd_clear_keyslot(hba, slot); |
---|
| 118 | +} |
---|
| 119 | + |
---|
| 120 | +bool ufshcd_crypto_enable(struct ufs_hba *hba) |
---|
| 121 | +{ |
---|
| 122 | + if (!(hba->caps & UFSHCD_CAP_CRYPTO)) |
---|
| 123 | + return false; |
---|
244 | 124 | |
---|
245 | 125 | /* Reset might clear all keys, so reprogram all the keys. */ |
---|
246 | | - keyslot_manager_reprogram_all_keys(hba->ksm); |
---|
247 | | -} |
---|
248 | | -EXPORT_SYMBOL_GPL(ufshcd_crypto_enable_spec); |
---|
| 126 | + if (hba->ksm.num_slots) { |
---|
| 127 | + int err = -EOPNOTSUPP; |
---|
249 | 128 | |
---|
250 | | -void ufshcd_crypto_disable_spec(struct ufs_hba *hba) |
---|
251 | | -{ |
---|
252 | | - hba->caps &= ~UFSHCD_CAP_CRYPTO; |
---|
253 | | -} |
---|
254 | | -EXPORT_SYMBOL_GPL(ufshcd_crypto_disable_spec); |
---|
| 129 | + trace_android_rvh_ufs_reprogram_all_keys(hba, &err); |
---|
| 130 | + if (err == -EOPNOTSUPP) |
---|
| 131 | + blk_ksm_reprogram_all_keys(&hba->ksm); |
---|
| 132 | + } |
---|
255 | 133 | |
---|
256 | | -static const struct keyslot_mgmt_ll_ops ufshcd_ksm_ops = { |
---|
| 134 | + if (hba->quirks & UFSHCD_QUIRK_BROKEN_CRYPTO_ENABLE) |
---|
| 135 | + return false; |
---|
| 136 | + |
---|
| 137 | + return true; |
---|
| 138 | +} |
---|
| 139 | + |
---|
| 140 | +static const struct blk_ksm_ll_ops ufshcd_ksm_ops = { |
---|
257 | 141 | .keyslot_program = ufshcd_crypto_keyslot_program, |
---|
258 | 142 | .keyslot_evict = ufshcd_crypto_keyslot_evict, |
---|
259 | 143 | }; |
---|
260 | 144 | |
---|
261 | | -enum blk_crypto_mode_num ufshcd_blk_crypto_mode_num_for_alg_dusize( |
---|
262 | | - enum ufs_crypto_alg ufs_crypto_alg, |
---|
263 | | - enum ufs_crypto_key_size key_size) |
---|
| 145 | +static enum blk_crypto_mode_num |
---|
| 146 | +ufshcd_find_blk_crypto_mode(union ufs_crypto_cap_entry cap) |
---|
264 | 147 | { |
---|
265 | | - /* |
---|
266 | | - * This is currently the only mode that UFS and blk-crypto both support. |
---|
267 | | - */ |
---|
268 | | - if (ufs_crypto_alg == UFS_CRYPTO_ALG_AES_XTS && |
---|
269 | | - key_size == UFS_CRYPTO_KEY_SIZE_256) |
---|
270 | | - return BLK_ENCRYPTION_MODE_AES_256_XTS; |
---|
| 148 | + int i; |
---|
271 | 149 | |
---|
| 150 | + for (i = 0; i < ARRAY_SIZE(ufs_crypto_algs); i++) { |
---|
| 151 | + BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0); |
---|
| 152 | + if (ufs_crypto_algs[i].ufs_alg == cap.algorithm_id && |
---|
| 153 | + ufs_crypto_algs[i].ufs_key_size == cap.key_size) { |
---|
| 154 | + return i; |
---|
| 155 | + } |
---|
| 156 | + } |
---|
272 | 157 | return BLK_ENCRYPTION_MODE_INVALID; |
---|
273 | 158 | } |
---|
274 | 159 | |
---|
275 | 160 | /** |
---|
276 | | - * ufshcd_hba_init_crypto - Read crypto capabilities, init crypto fields in hba |
---|
| 161 | + * ufshcd_hba_init_crypto_capabilities - Read crypto capabilities, init crypto |
---|
| 162 | + * fields in hba |
---|
277 | 163 | * @hba: Per adapter instance |
---|
278 | 164 | * |
---|
279 | 165 | * Return: 0 if crypto was initialized or is not supported, else a -errno value. |
---|
280 | 166 | */ |
---|
281 | | -int ufshcd_hba_init_crypto_spec(struct ufs_hba *hba, |
---|
282 | | - const struct keyslot_mgmt_ll_ops *ksm_ops) |
---|
| 167 | +int ufshcd_hba_init_crypto_capabilities(struct ufs_hba *hba) |
---|
283 | 168 | { |
---|
284 | | - int cap_idx = 0; |
---|
| 169 | + int cap_idx; |
---|
285 | 170 | int err = 0; |
---|
286 | | - unsigned int crypto_modes_supported[BLK_ENCRYPTION_MODE_MAX]; |
---|
287 | 171 | enum blk_crypto_mode_num blk_mode_num; |
---|
288 | 172 | |
---|
289 | | - /* Default to disabling crypto */ |
---|
290 | | - hba->caps &= ~UFSHCD_CAP_CRYPTO; |
---|
291 | | - |
---|
292 | | - /* Return 0 if crypto support isn't present */ |
---|
293 | | - if (!(hba->capabilities & MASK_CRYPTO_SUPPORT) || |
---|
294 | | - (hba->quirks & UFSHCD_QUIRK_BROKEN_CRYPTO)) |
---|
295 | | - goto out; |
---|
| 173 | + if (hba->quirks & UFSHCD_QUIRK_CUSTOM_KEYSLOT_MANAGER) |
---|
| 174 | + return 0; |
---|
296 | 175 | |
---|
297 | 176 | /* |
---|
298 | | - * Crypto Capabilities should never be 0, because the |
---|
299 | | - * config_array_ptr > 04h. So we use a 0 value to indicate that |
---|
300 | | - * crypto init failed, and can't be enabled. |
---|
| 177 | + * Don't use crypto if either the hardware doesn't advertise the |
---|
| 178 | + * standard crypto capability bit *or* if the vendor specific driver |
---|
| 179 | + * hasn't advertised that crypto is supported. |
---|
301 | 180 | */ |
---|
| 181 | + if (!(hba->capabilities & MASK_CRYPTO_SUPPORT) || |
---|
| 182 | + !(hba->caps & UFSHCD_CAP_CRYPTO)) |
---|
| 183 | + goto out; |
---|
| 184 | + |
---|
302 | 185 | hba->crypto_capabilities.reg_val = |
---|
303 | 186 | cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP)); |
---|
304 | 187 | hba->crypto_cfg_register = |
---|
305 | 188 | (u32)hba->crypto_capabilities.config_array_ptr * 0x100; |
---|
306 | 189 | hba->crypto_cap_array = |
---|
307 | | - devm_kcalloc(hba->dev, |
---|
308 | | - hba->crypto_capabilities.num_crypto_cap, |
---|
309 | | - sizeof(hba->crypto_cap_array[0]), |
---|
310 | | - GFP_KERNEL); |
---|
| 190 | + devm_kcalloc(hba->dev, hba->crypto_capabilities.num_crypto_cap, |
---|
| 191 | + sizeof(hba->crypto_cap_array[0]), GFP_KERNEL); |
---|
311 | 192 | if (!hba->crypto_cap_array) { |
---|
312 | 193 | err = -ENOMEM; |
---|
313 | 194 | goto out; |
---|
314 | 195 | } |
---|
315 | 196 | |
---|
316 | | - memset(crypto_modes_supported, 0, sizeof(crypto_modes_supported)); |
---|
| 197 | + /* The actual number of configurations supported is (CFGC+1) */ |
---|
| 198 | + err = devm_blk_ksm_init(hba->dev, &hba->ksm, |
---|
| 199 | + hba->crypto_capabilities.config_count + 1); |
---|
| 200 | + if (err) |
---|
| 201 | + goto out_free_caps; |
---|
| 202 | + |
---|
| 203 | + hba->ksm.ksm_ll_ops = ufshcd_ksm_ops; |
---|
| 204 | + /* UFS only supports 8 bytes for any DUN */ |
---|
| 205 | + hba->ksm.max_dun_bytes_supported = 8; |
---|
| 206 | + hba->ksm.features = BLK_CRYPTO_FEATURE_STANDARD_KEYS; |
---|
| 207 | + hba->ksm.dev = hba->dev; |
---|
| 208 | + |
---|
317 | 209 | /* |
---|
318 | | - * Store all the capabilities now so that we don't need to repeatedly |
---|
319 | | - * access the device each time we want to know its capabilities |
---|
| 210 | + * Cache all the UFS crypto capabilities and advertise the supported |
---|
| 211 | + * crypto modes and data unit sizes to the block layer. |
---|
320 | 212 | */ |
---|
321 | 213 | for (cap_idx = 0; cap_idx < hba->crypto_capabilities.num_crypto_cap; |
---|
322 | 214 | cap_idx++) { |
---|
.. | .. |
---|
324 | 216 | cpu_to_le32(ufshcd_readl(hba, |
---|
325 | 217 | REG_UFS_CRYPTOCAP + |
---|
326 | 218 | cap_idx * sizeof(__le32))); |
---|
327 | | - blk_mode_num = ufshcd_blk_crypto_mode_num_for_alg_dusize( |
---|
328 | | - hba->crypto_cap_array[cap_idx].algorithm_id, |
---|
329 | | - hba->crypto_cap_array[cap_idx].key_size); |
---|
330 | | - if (blk_mode_num == BLK_ENCRYPTION_MODE_INVALID) |
---|
331 | | - continue; |
---|
332 | | - crypto_modes_supported[blk_mode_num] |= |
---|
333 | | - hba->crypto_cap_array[cap_idx].sdus_mask * 512; |
---|
| 219 | + blk_mode_num = ufshcd_find_blk_crypto_mode( |
---|
| 220 | + hba->crypto_cap_array[cap_idx]); |
---|
| 221 | + if (blk_mode_num != BLK_ENCRYPTION_MODE_INVALID) |
---|
| 222 | + hba->ksm.crypto_modes_supported[blk_mode_num] |= |
---|
| 223 | + hba->crypto_cap_array[cap_idx].sdus_mask * 512; |
---|
334 | 224 | } |
---|
335 | | - |
---|
336 | | - ufshcd_clear_all_keyslots(hba); |
---|
337 | | - |
---|
338 | | - hba->ksm = keyslot_manager_create(hba->dev, ufshcd_num_keyslots(hba), |
---|
339 | | - ksm_ops, |
---|
340 | | - BLK_CRYPTO_FEATURE_STANDARD_KEYS, |
---|
341 | | - crypto_modes_supported, hba); |
---|
342 | | - |
---|
343 | | - if (!hba->ksm) { |
---|
344 | | - err = -ENOMEM; |
---|
345 | | - goto out_free_caps; |
---|
346 | | - } |
---|
347 | | - keyslot_manager_set_max_dun_bytes(hba->ksm, sizeof(u64)); |
---|
348 | 225 | |
---|
349 | 226 | return 0; |
---|
350 | 227 | |
---|
351 | 228 | out_free_caps: |
---|
352 | 229 | devm_kfree(hba->dev, hba->crypto_cap_array); |
---|
353 | 230 | out: |
---|
354 | | - /* Indicate that init failed by setting crypto_capabilities to 0 */ |
---|
355 | | - hba->crypto_capabilities.reg_val = 0; |
---|
| 231 | + /* Indicate that init failed by clearing UFSHCD_CAP_CRYPTO */ |
---|
| 232 | + hba->caps &= ~UFSHCD_CAP_CRYPTO; |
---|
356 | 233 | return err; |
---|
357 | 234 | } |
---|
358 | | -EXPORT_SYMBOL_GPL(ufshcd_hba_init_crypto_spec); |
---|
359 | 235 | |
---|
360 | | -void ufshcd_crypto_setup_rq_keyslot_manager_spec(struct ufs_hba *hba, |
---|
361 | | - struct request_queue *q) |
---|
| 236 | +/** |
---|
| 237 | + * ufshcd_init_crypto - Initialize crypto hardware |
---|
| 238 | + * @hba: Per adapter instance |
---|
| 239 | + */ |
---|
| 240 | +void ufshcd_init_crypto(struct ufs_hba *hba) |
---|
362 | 241 | { |
---|
363 | | - if (!ufshcd_hba_is_crypto_supported(hba) || !q) |
---|
| 242 | + int slot; |
---|
| 243 | + |
---|
| 244 | + if (!(hba->caps & UFSHCD_CAP_CRYPTO)) |
---|
364 | 245 | return; |
---|
365 | 246 | |
---|
366 | | - q->ksm = hba->ksm; |
---|
367 | | -} |
---|
368 | | -EXPORT_SYMBOL_GPL(ufshcd_crypto_setup_rq_keyslot_manager_spec); |
---|
369 | | - |
---|
370 | | -void ufshcd_crypto_destroy_rq_keyslot_manager_spec(struct ufs_hba *hba, |
---|
371 | | - struct request_queue *q) |
---|
372 | | -{ |
---|
373 | | - keyslot_manager_destroy(hba->ksm); |
---|
374 | | -} |
---|
375 | | -EXPORT_SYMBOL_GPL(ufshcd_crypto_destroy_rq_keyslot_manager_spec); |
---|
376 | | - |
---|
377 | | -int ufshcd_prepare_lrbp_crypto_spec(struct ufs_hba *hba, |
---|
378 | | - struct scsi_cmnd *cmd, |
---|
379 | | - struct ufshcd_lrb *lrbp) |
---|
380 | | -{ |
---|
381 | | - struct bio_crypt_ctx *bc; |
---|
382 | | - |
---|
383 | | - if (!bio_crypt_should_process(cmd->request)) { |
---|
384 | | - lrbp->crypto_enable = false; |
---|
385 | | - return 0; |
---|
386 | | - } |
---|
387 | | - bc = cmd->request->bio->bi_crypt_context; |
---|
388 | | - |
---|
389 | | - if (WARN_ON(!ufshcd_is_crypto_enabled(hba))) { |
---|
390 | | - /* |
---|
391 | | - * Upper layer asked us to do inline encryption |
---|
392 | | - * but that isn't enabled, so we fail this request. |
---|
393 | | - */ |
---|
394 | | - return -EINVAL; |
---|
395 | | - } |
---|
396 | | - if (!ufshcd_keyslot_valid(hba, bc->bc_keyslot)) |
---|
397 | | - return -EINVAL; |
---|
398 | | - |
---|
399 | | - lrbp->crypto_enable = true; |
---|
400 | | - lrbp->crypto_key_slot = bc->bc_keyslot; |
---|
401 | | - lrbp->data_unit_num = bc->bc_dun[0]; |
---|
402 | | - |
---|
403 | | - return 0; |
---|
404 | | -} |
---|
405 | | -EXPORT_SYMBOL_GPL(ufshcd_prepare_lrbp_crypto_spec); |
---|
406 | | - |
---|
407 | | -/* Crypto Variant Ops Support */ |
---|
408 | | - |
---|
409 | | -void ufshcd_crypto_enable(struct ufs_hba *hba) |
---|
410 | | -{ |
---|
411 | | - if (hba->crypto_vops && hba->crypto_vops->enable) |
---|
412 | | - return hba->crypto_vops->enable(hba); |
---|
413 | | - |
---|
414 | | - return ufshcd_crypto_enable_spec(hba); |
---|
415 | | -} |
---|
416 | | - |
---|
417 | | -void ufshcd_crypto_disable(struct ufs_hba *hba) |
---|
418 | | -{ |
---|
419 | | - if (hba->crypto_vops && hba->crypto_vops->disable) |
---|
420 | | - return hba->crypto_vops->disable(hba); |
---|
421 | | - |
---|
422 | | - return ufshcd_crypto_disable_spec(hba); |
---|
423 | | -} |
---|
424 | | - |
---|
425 | | -int ufshcd_hba_init_crypto(struct ufs_hba *hba) |
---|
426 | | -{ |
---|
427 | | - if (hba->crypto_vops && hba->crypto_vops->hba_init_crypto) |
---|
428 | | - return hba->crypto_vops->hba_init_crypto(hba, |
---|
429 | | - &ufshcd_ksm_ops); |
---|
430 | | - |
---|
431 | | - return ufshcd_hba_init_crypto_spec(hba, &ufshcd_ksm_ops); |
---|
| 247 | + /* Clear all keyslots */ |
---|
| 248 | + for (slot = 0; slot < hba->ksm.num_slots; slot++) |
---|
| 249 | + hba->ksm.ksm_ll_ops.keyslot_evict(&hba->ksm, NULL, slot); |
---|
432 | 250 | } |
---|
433 | 251 | |
---|
434 | 252 | void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba, |
---|
435 | 253 | struct request_queue *q) |
---|
436 | 254 | { |
---|
437 | | - if (hba->crypto_vops && hba->crypto_vops->setup_rq_keyslot_manager) |
---|
438 | | - return hba->crypto_vops->setup_rq_keyslot_manager(hba, q); |
---|
439 | | - |
---|
440 | | - return ufshcd_crypto_setup_rq_keyslot_manager_spec(hba, q); |
---|
441 | | -} |
---|
442 | | - |
---|
443 | | -void ufshcd_crypto_destroy_rq_keyslot_manager(struct ufs_hba *hba, |
---|
444 | | - struct request_queue *q) |
---|
445 | | -{ |
---|
446 | | - if (hba->crypto_vops && hba->crypto_vops->destroy_rq_keyslot_manager) |
---|
447 | | - return hba->crypto_vops->destroy_rq_keyslot_manager(hba, q); |
---|
448 | | - |
---|
449 | | - return ufshcd_crypto_destroy_rq_keyslot_manager_spec(hba, q); |
---|
450 | | -} |
---|
451 | | - |
---|
452 | | -int ufshcd_prepare_lrbp_crypto(struct ufs_hba *hba, |
---|
453 | | - struct scsi_cmnd *cmd, |
---|
454 | | - struct ufshcd_lrb *lrbp) |
---|
455 | | -{ |
---|
456 | | - if (hba->crypto_vops && hba->crypto_vops->prepare_lrbp_crypto) |
---|
457 | | - return hba->crypto_vops->prepare_lrbp_crypto(hba, cmd, lrbp); |
---|
458 | | - |
---|
459 | | - return ufshcd_prepare_lrbp_crypto_spec(hba, cmd, lrbp); |
---|
460 | | -} |
---|
461 | | - |
---|
462 | | -int ufshcd_map_sg_crypto(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
---|
463 | | -{ |
---|
464 | | - if (hba->crypto_vops && hba->crypto_vops->map_sg_crypto) |
---|
465 | | - return hba->crypto_vops->map_sg_crypto(hba, lrbp); |
---|
466 | | - |
---|
467 | | - return 0; |
---|
468 | | -} |
---|
469 | | - |
---|
470 | | -int ufshcd_complete_lrbp_crypto(struct ufs_hba *hba, |
---|
471 | | - struct scsi_cmnd *cmd, |
---|
472 | | - struct ufshcd_lrb *lrbp) |
---|
473 | | -{ |
---|
474 | | - if (hba->crypto_vops && hba->crypto_vops->complete_lrbp_crypto) |
---|
475 | | - return hba->crypto_vops->complete_lrbp_crypto(hba, cmd, lrbp); |
---|
476 | | - |
---|
477 | | - return 0; |
---|
478 | | -} |
---|
479 | | - |
---|
480 | | -void ufshcd_crypto_debug(struct ufs_hba *hba) |
---|
481 | | -{ |
---|
482 | | - if (hba->crypto_vops && hba->crypto_vops->debug) |
---|
483 | | - hba->crypto_vops->debug(hba); |
---|
484 | | -} |
---|
485 | | - |
---|
486 | | -int ufshcd_crypto_suspend(struct ufs_hba *hba, |
---|
487 | | - enum ufs_pm_op pm_op) |
---|
488 | | -{ |
---|
489 | | - if (hba->crypto_vops && hba->crypto_vops->suspend) |
---|
490 | | - return hba->crypto_vops->suspend(hba, pm_op); |
---|
491 | | - |
---|
492 | | - return 0; |
---|
493 | | -} |
---|
494 | | - |
---|
495 | | -int ufshcd_crypto_resume(struct ufs_hba *hba, |
---|
496 | | - enum ufs_pm_op pm_op) |
---|
497 | | -{ |
---|
498 | | - if (hba->crypto_vops && hba->crypto_vops->resume) |
---|
499 | | - return hba->crypto_vops->resume(hba, pm_op); |
---|
500 | | - |
---|
501 | | - return 0; |
---|
502 | | -} |
---|
503 | | - |
---|
504 | | -void ufshcd_crypto_set_vops(struct ufs_hba *hba, |
---|
505 | | - struct ufs_hba_crypto_variant_ops *crypto_vops) |
---|
506 | | -{ |
---|
507 | | - hba->crypto_vops = crypto_vops; |
---|
| 255 | + if (hba->caps & UFSHCD_CAP_CRYPTO) |
---|
| 256 | + blk_ksm_register(&hba->ksm, q); |
---|
508 | 257 | } |
---|