• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2019 Google LLC
4  */
5 
6 #include <linux/keyslot-manager.h>
7 #include "ufshcd.h"
8 #include "ufshcd-crypto.h"
9 
ufshcd_cap_idx_valid(struct ufs_hba * hba,unsigned int cap_idx)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 }
14 
get_data_unit_size_mask(unsigned int data_unit_size)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 
get_keysize_bytes(enum ufs_crypto_key_size size)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 
ufshcd_crypto_cap_find(struct ufs_hba * hba,enum blk_crypto_mode_num crypto_mode,unsigned int data_unit_size)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 {
44 	enum ufs_crypto_alg ufs_alg;
45 	u8 data_unit_mask;
46 	int cap_idx;
47 	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(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  */
ufshcd_crypto_cfg_entry_write_key(union ufs_crypto_cfg_entry * cfg,const u8 * key,union ufs_crypto_cap_entry cap)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 }
120 
ufshcd_program_key(struct ufs_hba * hba,const union ufs_crypto_cfg_entry * cfg,int slot)121 static int ufshcd_program_key(struct ufs_hba *hba,
122 			      const union ufs_crypto_cfg_entry *cfg, int slot)
123 {
124 	int i;
125 	u32 slot_offset = hba->crypto_cfg_register + slot * sizeof(*cfg);
126 	int err;
127 
128 	pm_runtime_get_sync(hba->dev);
129 	ufshcd_hold(hba, false);
130 
131 	if (hba->vops->program_key) {
132 		err = hba->vops->program_key(hba, cfg, slot);
133 		goto out;
134 	}
135 
136 	/* Clear the dword 16 */
137 	ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0]));
138 	/* Ensure that CFGE is cleared before programming the key */
139 	wmb();
140 	for (i = 0; i < 16; i++) {
141 		ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[i]),
142 			      slot_offset + i * sizeof(cfg->reg_val[0]));
143 		/* Spec says each dword in key must be written sequentially */
144 		wmb();
145 	}
146 	/* Write dword 17 */
147 	ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[17]),
148 		      slot_offset + 17 * sizeof(cfg->reg_val[0]));
149 	/* Dword 16 must be written last */
150 	wmb();
151 	/* Write dword 16 */
152 	ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[16]),
153 		      slot_offset + 16 * sizeof(cfg->reg_val[0]));
154 	wmb();
155 	err = 0;
156 out:
157 	ufshcd_release(hba);
158 	pm_runtime_put_sync(hba->dev);
159 	return err;
160 }
161 
ufshcd_clear_keyslot(struct ufs_hba * hba,int slot)162 static void ufshcd_clear_keyslot(struct ufs_hba *hba, int slot)
163 {
164 	union ufs_crypto_cfg_entry cfg = { 0 };
165 	int err;
166 
167 	err = ufshcd_program_key(hba, &cfg, slot);
168 	WARN_ON_ONCE(err);
169 }
170 
171 /* Clear all keyslots at driver init time */
ufshcd_clear_all_keyslots(struct ufs_hba * hba)172 static void ufshcd_clear_all_keyslots(struct ufs_hba *hba)
173 {
174 	int slot;
175 
176 	for (slot = 0; slot < ufshcd_num_keyslots(hba); slot++)
177 		ufshcd_clear_keyslot(hba, slot);
178 }
179 
ufshcd_crypto_keyslot_program(struct keyslot_manager * ksm,const struct blk_crypto_key * key,unsigned int slot)180 static int ufshcd_crypto_keyslot_program(struct keyslot_manager *ksm,
181 					 const struct blk_crypto_key *key,
182 					 unsigned int slot)
183 {
184 	struct ufs_hba *hba = keyslot_manager_private(ksm);
185 	int err = 0;
186 	u8 data_unit_mask;
187 	union ufs_crypto_cfg_entry cfg;
188 	int cap_idx;
189 
190 	cap_idx = ufshcd_crypto_cap_find(hba, key->crypto_mode,
191 					 key->data_unit_size);
192 
193 	if (!ufshcd_is_crypto_enabled(hba) ||
194 	    !ufshcd_keyslot_valid(hba, slot) ||
195 	    !ufshcd_cap_idx_valid(hba, cap_idx))
196 		return -EINVAL;
197 
198 	data_unit_mask = get_data_unit_size_mask(key->data_unit_size);
199 
200 	if (!(data_unit_mask & hba->crypto_cap_array[cap_idx].sdus_mask))
201 		return -EINVAL;
202 
203 	memset(&cfg, 0, sizeof(cfg));
204 	cfg.data_unit_size = data_unit_mask;
205 	cfg.crypto_cap_idx = cap_idx;
206 	cfg.config_enable |= UFS_CRYPTO_CONFIGURATION_ENABLE;
207 
208 	err = ufshcd_crypto_cfg_entry_write_key(&cfg, key->raw,
209 						hba->crypto_cap_array[cap_idx]);
210 	if (err)
211 		return err;
212 
213 	err = ufshcd_program_key(hba, &cfg, slot);
214 
215 	memzero_explicit(&cfg, sizeof(cfg));
216 
217 	return err;
218 }
219 
ufshcd_crypto_keyslot_evict(struct keyslot_manager * ksm,const struct blk_crypto_key * key,unsigned int slot)220 static int ufshcd_crypto_keyslot_evict(struct keyslot_manager *ksm,
221 				       const struct blk_crypto_key *key,
222 				       unsigned int slot)
223 {
224 	struct ufs_hba *hba = keyslot_manager_private(ksm);
225 
226 	if (!ufshcd_is_crypto_enabled(hba) ||
227 	    !ufshcd_keyslot_valid(hba, slot))
228 		return -EINVAL;
229 
230 	/*
231 	 * Clear the crypto cfg on the device. Clearing CFGE
232 	 * might not be sufficient, so just clear the entire cfg.
233 	 */
234 	ufshcd_clear_keyslot(hba, slot);
235 
236 	return 0;
237 }
238 
239 /* Functions implementing UFSHCI v2.1 specification behaviour */
ufshcd_crypto_enable_spec(struct ufs_hba * hba)240 void ufshcd_crypto_enable_spec(struct ufs_hba *hba)
241 {
242 	if (!ufshcd_hba_is_crypto_supported(hba))
243 		return;
244 
245 	hba->caps |= UFSHCD_CAP_CRYPTO;
246 
247 	/* Reset might clear all keys, so reprogram all the keys. */
248 	keyslot_manager_reprogram_all_keys(hba->ksm);
249 }
250 EXPORT_SYMBOL_GPL(ufshcd_crypto_enable_spec);
251 
ufshcd_crypto_disable_spec(struct ufs_hba * hba)252 void ufshcd_crypto_disable_spec(struct ufs_hba *hba)
253 {
254 	hba->caps &= ~UFSHCD_CAP_CRYPTO;
255 }
256 EXPORT_SYMBOL_GPL(ufshcd_crypto_disable_spec);
257 
258 static const struct keyslot_mgmt_ll_ops ufshcd_ksm_ops = {
259 	.keyslot_program	= ufshcd_crypto_keyslot_program,
260 	.keyslot_evict		= ufshcd_crypto_keyslot_evict,
261 };
262 
ufshcd_blk_crypto_mode_num_for_alg_dusize(enum ufs_crypto_alg ufs_crypto_alg,enum ufs_crypto_key_size key_size)263 enum blk_crypto_mode_num ufshcd_blk_crypto_mode_num_for_alg_dusize(
264 					enum ufs_crypto_alg ufs_crypto_alg,
265 					enum ufs_crypto_key_size key_size)
266 {
267 	/*
268 	 * This is currently the only mode that UFS and blk-crypto both support.
269 	 */
270 	if (ufs_crypto_alg == UFS_CRYPTO_ALG_AES_XTS &&
271 		key_size == UFS_CRYPTO_KEY_SIZE_256)
272 		return BLK_ENCRYPTION_MODE_AES_256_XTS;
273 
274 	return BLK_ENCRYPTION_MODE_INVALID;
275 }
276 
277 /**
278  * ufshcd_hba_init_crypto - Read crypto capabilities, init crypto fields in hba
279  * @hba: Per adapter instance
280  *
281  * Return: 0 if crypto was initialized or is not supported, else a -errno value.
282  */
ufshcd_hba_init_crypto_spec(struct ufs_hba * hba,const struct keyslot_mgmt_ll_ops * ksm_ops)283 int ufshcd_hba_init_crypto_spec(struct ufs_hba *hba,
284 				const struct keyslot_mgmt_ll_ops *ksm_ops)
285 {
286 	int cap_idx = 0;
287 	int err = 0;
288 	unsigned int crypto_modes_supported[BLK_ENCRYPTION_MODE_MAX];
289 	enum blk_crypto_mode_num blk_mode_num;
290 
291 	/* Default to disabling crypto */
292 	hba->caps &= ~UFSHCD_CAP_CRYPTO;
293 
294 	/* Return 0 if crypto support isn't present */
295 	if (!(hba->capabilities & MASK_CRYPTO_SUPPORT) ||
296 	    (hba->quirks & UFSHCD_QUIRK_BROKEN_CRYPTO))
297 		goto out;
298 
299 	/*
300 	 * Crypto Capabilities should never be 0, because the
301 	 * config_array_ptr > 04h. So we use a 0 value to indicate that
302 	 * crypto init failed, and can't be enabled.
303 	 */
304 	hba->crypto_capabilities.reg_val =
305 			cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP));
306 	hba->crypto_cfg_register =
307 		(u32)hba->crypto_capabilities.config_array_ptr * 0x100;
308 	hba->crypto_cap_array =
309 		devm_kcalloc(hba->dev,
310 			     hba->crypto_capabilities.num_crypto_cap,
311 			     sizeof(hba->crypto_cap_array[0]),
312 			     GFP_KERNEL);
313 	if (!hba->crypto_cap_array) {
314 		err = -ENOMEM;
315 		goto out;
316 	}
317 
318 	memset(crypto_modes_supported, 0, sizeof(crypto_modes_supported));
319 	/*
320 	 * Store all the capabilities now so that we don't need to repeatedly
321 	 * access the device each time we want to know its capabilities
322 	 */
323 	for (cap_idx = 0; cap_idx < hba->crypto_capabilities.num_crypto_cap;
324 	     cap_idx++) {
325 		hba->crypto_cap_array[cap_idx].reg_val =
326 			cpu_to_le32(ufshcd_readl(hba,
327 						 REG_UFS_CRYPTOCAP +
328 						 cap_idx * sizeof(__le32)));
329 		blk_mode_num = ufshcd_blk_crypto_mode_num_for_alg_dusize(
330 				hba->crypto_cap_array[cap_idx].algorithm_id,
331 				hba->crypto_cap_array[cap_idx].key_size);
332 		if (blk_mode_num == BLK_ENCRYPTION_MODE_INVALID)
333 			continue;
334 		crypto_modes_supported[blk_mode_num] |=
335 			hba->crypto_cap_array[cap_idx].sdus_mask * 512;
336 	}
337 
338 	ufshcd_clear_all_keyslots(hba);
339 
340 	hba->ksm = keyslot_manager_create(ufshcd_num_keyslots(hba), ksm_ops,
341 					  crypto_modes_supported, hba);
342 
343 	if (!hba->ksm) {
344 		err = -ENOMEM;
345 		goto out_free_caps;
346 	}
347 
348 	return 0;
349 
350 out_free_caps:
351 	devm_kfree(hba->dev, hba->crypto_cap_array);
352 out:
353 	/* Indicate that init failed by setting crypto_capabilities to 0 */
354 	hba->crypto_capabilities.reg_val = 0;
355 	return err;
356 }
357 EXPORT_SYMBOL_GPL(ufshcd_hba_init_crypto_spec);
358 
ufshcd_crypto_setup_rq_keyslot_manager_spec(struct ufs_hba * hba,struct request_queue * q)359 void ufshcd_crypto_setup_rq_keyslot_manager_spec(struct ufs_hba *hba,
360 						 struct request_queue *q)
361 {
362 	if (!ufshcd_hba_is_crypto_supported(hba) || !q)
363 		return;
364 
365 	q->ksm = hba->ksm;
366 }
367 EXPORT_SYMBOL_GPL(ufshcd_crypto_setup_rq_keyslot_manager_spec);
368 
ufshcd_crypto_destroy_rq_keyslot_manager_spec(struct ufs_hba * hba,struct request_queue * q)369 void ufshcd_crypto_destroy_rq_keyslot_manager_spec(struct ufs_hba *hba,
370 						   struct request_queue *q)
371 {
372 	keyslot_manager_destroy(hba->ksm);
373 }
374 EXPORT_SYMBOL_GPL(ufshcd_crypto_destroy_rq_keyslot_manager_spec);
375 
ufshcd_prepare_lrbp_crypto_spec(struct ufs_hba * hba,struct scsi_cmnd * cmd,struct ufshcd_lrb * lrbp)376 int ufshcd_prepare_lrbp_crypto_spec(struct ufs_hba *hba,
377 				    struct scsi_cmnd *cmd,
378 				    struct ufshcd_lrb *lrbp)
379 {
380 	struct bio_crypt_ctx *bc;
381 
382 	if (!bio_crypt_should_process(cmd->request)) {
383 		lrbp->crypto_enable = false;
384 		return 0;
385 	}
386 	bc = cmd->request->bio->bi_crypt_context;
387 
388 	if (WARN_ON(!ufshcd_is_crypto_enabled(hba))) {
389 		/*
390 		 * Upper layer asked us to do inline encryption
391 		 * but that isn't enabled, so we fail this request.
392 		 */
393 		return -EINVAL;
394 	}
395 	if (!ufshcd_keyslot_valid(hba, bc->bc_keyslot))
396 		return -EINVAL;
397 
398 	lrbp->crypto_enable = true;
399 	lrbp->crypto_key_slot = bc->bc_keyslot;
400 	lrbp->data_unit_num = bc->bc_dun[0];
401 
402 	return 0;
403 }
404 EXPORT_SYMBOL_GPL(ufshcd_prepare_lrbp_crypto_spec);
405 
406 /* Crypto Variant Ops Support */
407 
ufshcd_crypto_enable(struct ufs_hba * hba)408 void ufshcd_crypto_enable(struct ufs_hba *hba)
409 {
410 	if (hba->crypto_vops && hba->crypto_vops->enable)
411 		return hba->crypto_vops->enable(hba);
412 
413 	return ufshcd_crypto_enable_spec(hba);
414 }
415 
ufshcd_crypto_disable(struct ufs_hba * hba)416 void ufshcd_crypto_disable(struct ufs_hba *hba)
417 {
418 	if (hba->crypto_vops && hba->crypto_vops->disable)
419 		return hba->crypto_vops->disable(hba);
420 
421 	return ufshcd_crypto_disable_spec(hba);
422 }
423 
ufshcd_hba_init_crypto(struct ufs_hba * hba)424 int ufshcd_hba_init_crypto(struct ufs_hba *hba)
425 {
426 	if (hba->crypto_vops && hba->crypto_vops->hba_init_crypto)
427 		return hba->crypto_vops->hba_init_crypto(hba,
428 							 &ufshcd_ksm_ops);
429 
430 	return ufshcd_hba_init_crypto_spec(hba, &ufshcd_ksm_ops);
431 }
432 
ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba * hba,struct request_queue * q)433 void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba,
434 					    struct request_queue *q)
435 {
436 	if (hba->crypto_vops && hba->crypto_vops->setup_rq_keyslot_manager)
437 		return hba->crypto_vops->setup_rq_keyslot_manager(hba, q);
438 
439 	return ufshcd_crypto_setup_rq_keyslot_manager_spec(hba, q);
440 }
441 
ufshcd_crypto_destroy_rq_keyslot_manager(struct ufs_hba * hba,struct request_queue * q)442 void ufshcd_crypto_destroy_rq_keyslot_manager(struct ufs_hba *hba,
443 					      struct request_queue *q)
444 {
445 	if (hba->crypto_vops && hba->crypto_vops->destroy_rq_keyslot_manager)
446 		return hba->crypto_vops->destroy_rq_keyslot_manager(hba, q);
447 
448 	return ufshcd_crypto_destroy_rq_keyslot_manager_spec(hba, q);
449 }
450 
ufshcd_prepare_lrbp_crypto(struct ufs_hba * hba,struct scsi_cmnd * cmd,struct ufshcd_lrb * lrbp)451 int ufshcd_prepare_lrbp_crypto(struct ufs_hba *hba,
452 			       struct scsi_cmnd *cmd,
453 			       struct ufshcd_lrb *lrbp)
454 {
455 	if (hba->crypto_vops && hba->crypto_vops->prepare_lrbp_crypto)
456 		return hba->crypto_vops->prepare_lrbp_crypto(hba, cmd, lrbp);
457 
458 	return ufshcd_prepare_lrbp_crypto_spec(hba, cmd, lrbp);
459 }
460 
ufshcd_complete_lrbp_crypto(struct ufs_hba * hba,struct scsi_cmnd * cmd,struct ufshcd_lrb * lrbp)461 int ufshcd_complete_lrbp_crypto(struct ufs_hba *hba,
462 				struct scsi_cmnd *cmd,
463 				struct ufshcd_lrb *lrbp)
464 {
465 	if (hba->crypto_vops && hba->crypto_vops->complete_lrbp_crypto)
466 		return hba->crypto_vops->complete_lrbp_crypto(hba, cmd, lrbp);
467 
468 	return 0;
469 }
470 
ufshcd_crypto_debug(struct ufs_hba * hba)471 void ufshcd_crypto_debug(struct ufs_hba *hba)
472 {
473 	if (hba->crypto_vops && hba->crypto_vops->debug)
474 		hba->crypto_vops->debug(hba);
475 }
476 
ufshcd_crypto_suspend(struct ufs_hba * hba,enum ufs_pm_op pm_op)477 int ufshcd_crypto_suspend(struct ufs_hba *hba,
478 			  enum ufs_pm_op pm_op)
479 {
480 	if (hba->crypto_vops && hba->crypto_vops->suspend)
481 		return hba->crypto_vops->suspend(hba, pm_op);
482 
483 	return 0;
484 }
485 
ufshcd_crypto_resume(struct ufs_hba * hba,enum ufs_pm_op pm_op)486 int ufshcd_crypto_resume(struct ufs_hba *hba,
487 			 enum ufs_pm_op pm_op)
488 {
489 	if (hba->crypto_vops && hba->crypto_vops->resume)
490 		return hba->crypto_vops->resume(hba, pm_op);
491 
492 	return 0;
493 }
494 
ufshcd_crypto_set_vops(struct ufs_hba * hba,struct ufs_hba_crypto_variant_ops * crypto_vops)495 void ufshcd_crypto_set_vops(struct ufs_hba *hba,
496 			    struct ufs_hba_crypto_variant_ops *crypto_vops)
497 {
498 	hba->crypto_vops = crypto_vops;
499 }
500