hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/scsi/ufs/ufshcd-crypto.c
....@@ -3,320 +3,212 @@
33 * Copyright 2019 Google LLC
44 */
55
6
-#include <linux/keyslot-manager.h>
76 #include "ufshcd.h"
87 #include "ufshcd-crypto.h"
98
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>
1411
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 {
4414 enum ufs_crypto_alg ufs_alg;
45
- u8 data_unit_mask;
46
- int cap_idx;
4715 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
+};
12022
12123 static int ufshcd_program_key(struct ufs_hba *hba,
12224 const union ufs_crypto_cfg_entry *cfg, int slot)
12325 {
12426 int i;
12527 u32 slot_offset = hba->crypto_cfg_register + slot * sizeof(*cfg);
126
- int err;
28
+ int err = 0;
12729
12830 ufshcd_hold(hba, false);
12931
130
- if (hba->vops->program_key) {
32
+ if (hba->vops && hba->vops->program_key) {
13133 err = hba->vops->program_key(hba, cfg, slot);
13234 goto out;
13335 }
13436
135
- /* Clear the dword 16 */
136
- ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0]));
13737 /* Ensure that CFGE is cleared before programming the key */
138
- wmb();
38
+ ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0]));
13939 for (i = 0; i < 16; i++) {
14040 ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[i]),
14141 slot_offset + i * sizeof(cfg->reg_val[0]));
142
- /* Spec says each dword in key must be written sequentially */
143
- wmb();
14442 }
14543 /* Write dword 17 */
14644 ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[17]),
14745 slot_offset + 17 * sizeof(cfg->reg_val[0]));
14846 /* Dword 16 must be written last */
149
- wmb();
150
- /* Write dword 16 */
15147 ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[16]),
15248 slot_offset + 16 * sizeof(cfg->reg_val[0]));
153
- wmb();
154
- err = 0;
15549 out:
15650 ufshcd_release(hba);
15751 return err;
15852 }
15953
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,
17955 const struct blk_crypto_key *key,
18056 unsigned int slot)
18157 {
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;
18767
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
+ }
19077
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;
19580
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));
20281 cfg.data_unit_size = data_unit_mask;
20382 cfg.crypto_cap_idx = cap_idx;
204
- cfg.config_enable |= UFS_CRYPTO_CONFIGURATION_ENABLE;
83
+ cfg.config_enable = UFS_CRYPTO_CONFIGURATION_ENABLE;
20584
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
+ }
21093
21194 err = ufshcd_program_key(hba, &cfg, slot);
21295
21396 memzero_explicit(&cfg, sizeof(cfg));
214
-
21597 return err;
21698 }
21799
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)
221101 {
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
-
228102 /*
229103 * Clear the crypto cfg on the device. Clearing CFGE
230104 * might not be sufficient, so just clear the entire cfg.
231105 */
232
- ufshcd_clear_keyslot(hba, slot);
106
+ union ufs_crypto_cfg_entry cfg = {};
233107
234
- return 0;
108
+ return ufshcd_program_key(hba, &cfg, slot);
235109 }
236110
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)
239114 {
240
- if (!ufshcd_hba_is_crypto_supported(hba))
241
- return;
115
+ struct ufs_hba *hba = container_of(ksm, struct ufs_hba, ksm);
242116
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;
244124
245125 /* 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;
249128
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
+ }
255133
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 = {
257141 .keyslot_program = ufshcd_crypto_keyslot_program,
258142 .keyslot_evict = ufshcd_crypto_keyslot_evict,
259143 };
260144
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)
264147 {
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;
271149
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
+ }
272157 return BLK_ENCRYPTION_MODE_INVALID;
273158 }
274159
275160 /**
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
277163 * @hba: Per adapter instance
278164 *
279165 * Return: 0 if crypto was initialized or is not supported, else a -errno value.
280166 */
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)
283168 {
284
- int cap_idx = 0;
169
+ int cap_idx;
285170 int err = 0;
286
- unsigned int crypto_modes_supported[BLK_ENCRYPTION_MODE_MAX];
287171 enum blk_crypto_mode_num blk_mode_num;
288172
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;
296175
297176 /*
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.
301180 */
181
+ if (!(hba->capabilities & MASK_CRYPTO_SUPPORT) ||
182
+ !(hba->caps & UFSHCD_CAP_CRYPTO))
183
+ goto out;
184
+
302185 hba->crypto_capabilities.reg_val =
303186 cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP));
304187 hba->crypto_cfg_register =
305188 (u32)hba->crypto_capabilities.config_array_ptr * 0x100;
306189 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);
311192 if (!hba->crypto_cap_array) {
312193 err = -ENOMEM;
313194 goto out;
314195 }
315196
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
+
317209 /*
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.
320212 */
321213 for (cap_idx = 0; cap_idx < hba->crypto_capabilities.num_crypto_cap;
322214 cap_idx++) {
....@@ -324,185 +216,42 @@
324216 cpu_to_le32(ufshcd_readl(hba,
325217 REG_UFS_CRYPTOCAP +
326218 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;
334224 }
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));
348225
349226 return 0;
350227
351228 out_free_caps:
352229 devm_kfree(hba->dev, hba->crypto_cap_array);
353230 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;
356233 return err;
357234 }
358
-EXPORT_SYMBOL_GPL(ufshcd_hba_init_crypto_spec);
359235
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)
362241 {
363
- if (!ufshcd_hba_is_crypto_supported(hba) || !q)
242
+ int slot;
243
+
244
+ if (!(hba->caps & UFSHCD_CAP_CRYPTO))
364245 return;
365246
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);
432250 }
433251
434252 void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba,
435253 struct request_queue *q)
436254 {
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);
508257 }