• 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_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  */
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 	ufshcd_hold(hba, false);
129 
130 	if (hba->vops->program_key) {
131 		err = hba->vops->program_key(hba, cfg, slot);
132 		goto out;
133 	}
134 
135 	/* Clear the dword 16 */
136 	ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0]));
137 	/* Ensure that CFGE is cleared before programming the key */
138 	wmb();
139 	for (i = 0; i < 16; i++) {
140 		ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[i]),
141 			      slot_offset + i * sizeof(cfg->reg_val[0]));
142 		/* Spec says each dword in key must be written sequentially */
143 		wmb();
144 	}
145 	/* Write dword 17 */
146 	ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[17]),
147 		      slot_offset + 17 * sizeof(cfg->reg_val[0]));
148 	/* Dword 16 must be written last */
149 	wmb();
150 	/* Write dword 16 */
151 	ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[16]),
152 		      slot_offset + 16 * sizeof(cfg->reg_val[0]));
153 	wmb();
154 	err = 0;
155 out:
156 	ufshcd_release(hba);
157 	return err;
158 }
159 
ufshcd_clear_keyslot(struct ufs_hba * hba,int slot)160 static void ufshcd_clear_keyslot(struct ufs_hba *hba, int slot)
161 {
162 	union ufs_crypto_cfg_entry cfg = {};
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 */
ufshcd_clear_all_keyslots(struct ufs_hba * hba)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 
ufshcd_crypto_keyslot_program(struct keyslot_manager * ksm,const struct blk_crypto_key * key,unsigned int slot)178 static int ufshcd_crypto_keyslot_program(struct keyslot_manager *ksm,
179 					 const struct blk_crypto_key *key,
180 					 unsigned int slot)
181 {
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;
187 
188 	cap_idx = ufshcd_crypto_cap_find(hba, key->crypto_mode,
189 					 key->data_unit_size);
190 
191 	if (!ufshcd_is_crypto_enabled(hba) ||
192 	    !ufshcd_keyslot_valid(hba, slot) ||
193 	    !ufshcd_cap_idx_valid(hba, cap_idx))
194 		return -EINVAL;
195 
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 	cfg.data_unit_size = data_unit_mask;
203 	cfg.crypto_cap_idx = cap_idx;
204 	cfg.config_enable |= UFS_CRYPTO_CONFIGURATION_ENABLE;
205 
206 	err = ufshcd_crypto_cfg_entry_write_key(&cfg, key->raw,
207 						hba->crypto_cap_array[cap_idx]);
208 	if (err)
209 		return err;
210 
211 	err = ufshcd_program_key(hba, &cfg, slot);
212 
213 	memzero_explicit(&cfg, sizeof(cfg));
214 
215 	return err;
216 }
217 
ufshcd_crypto_keyslot_evict(struct keyslot_manager * ksm,const struct blk_crypto_key * key,unsigned int slot)218 static int ufshcd_crypto_keyslot_evict(struct keyslot_manager *ksm,
219 				       const struct blk_crypto_key *key,
220 				       unsigned int slot)
221 {
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 	/*
229 	 * Clear the crypto cfg on the device. Clearing CFGE
230 	 * might not be sufficient, so just clear the entire cfg.
231 	 */
232 	ufshcd_clear_keyslot(hba, slot);
233 
234 	return 0;
235 }
236 
237 /* Functions implementing UFSHCI v2.1 specification behaviour */
ufshcd_crypto_enable_spec(struct ufs_hba * hba)238 void ufshcd_crypto_enable_spec(struct ufs_hba *hba)
239 {
240 	if (!ufshcd_hba_is_crypto_supported(hba))
241 		return;
242 
243 	hba->caps |= UFSHCD_CAP_CRYPTO;
244 
245 	/* 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);
249 
ufshcd_crypto_disable_spec(struct ufs_hba * hba)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);
255 
256 static const struct keyslot_mgmt_ll_ops ufshcd_ksm_ops = {
257 	.keyslot_program	= ufshcd_crypto_keyslot_program,
258 	.keyslot_evict		= ufshcd_crypto_keyslot_evict,
259 };
260 
ufshcd_blk_crypto_mode_num_for_alg_dusize(enum ufs_crypto_alg ufs_crypto_alg,enum ufs_crypto_key_size key_size)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)
264 {
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;
271 
272 	return BLK_ENCRYPTION_MODE_INVALID;
273 }
274 
275 /**
276  * ufshcd_hba_init_crypto - Read crypto capabilities, init crypto fields in hba
277  * @hba: Per adapter instance
278  *
279  * Return: 0 if crypto was initialized or is not supported, else a -errno value.
280  */
ufshcd_hba_init_crypto_spec(struct ufs_hba * hba,const struct keyslot_mgmt_ll_ops * ksm_ops)281 int ufshcd_hba_init_crypto_spec(struct ufs_hba *hba,
282 				const struct keyslot_mgmt_ll_ops *ksm_ops)
283 {
284 	int cap_idx = 0;
285 	int err = 0;
286 	unsigned int crypto_modes_supported[BLK_ENCRYPTION_MODE_MAX];
287 	enum blk_crypto_mode_num blk_mode_num;
288 
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;
296 
297 	/*
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.
301 	 */
302 	hba->crypto_capabilities.reg_val =
303 			cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP));
304 	hba->crypto_cfg_register =
305 		(u32)hba->crypto_capabilities.config_array_ptr * 0x100;
306 	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);
311 	if (!hba->crypto_cap_array) {
312 		err = -ENOMEM;
313 		goto out;
314 	}
315 
316 	memset(crypto_modes_supported, 0, sizeof(crypto_modes_supported));
317 	/*
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
320 	 */
321 	for (cap_idx = 0; cap_idx < hba->crypto_capabilities.num_crypto_cap;
322 	     cap_idx++) {
323 		hba->crypto_cap_array[cap_idx].reg_val =
324 			cpu_to_le32(ufshcd_readl(hba,
325 						 REG_UFS_CRYPTOCAP +
326 						 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;
334 	}
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 
349 	return 0;
350 
351 out_free_caps:
352 	devm_kfree(hba->dev, hba->crypto_cap_array);
353 out:
354 	/* Indicate that init failed by setting crypto_capabilities to 0 */
355 	hba->crypto_capabilities.reg_val = 0;
356 	return err;
357 }
358 EXPORT_SYMBOL_GPL(ufshcd_hba_init_crypto_spec);
359 
ufshcd_crypto_setup_rq_keyslot_manager_spec(struct ufs_hba * hba,struct request_queue * q)360 void ufshcd_crypto_setup_rq_keyslot_manager_spec(struct ufs_hba *hba,
361 						 struct request_queue *q)
362 {
363 	if (!ufshcd_hba_is_crypto_supported(hba) || !q)
364 		return;
365 
366 	q->ksm = hba->ksm;
367 }
368 EXPORT_SYMBOL_GPL(ufshcd_crypto_setup_rq_keyslot_manager_spec);
369 
ufshcd_crypto_destroy_rq_keyslot_manager_spec(struct ufs_hba * hba,struct request_queue * q)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 
ufshcd_prepare_lrbp_crypto_spec(struct ufs_hba * hba,struct scsi_cmnd * cmd,struct ufshcd_lrb * lrbp)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 
ufshcd_crypto_enable(struct ufs_hba * hba)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 
ufshcd_crypto_disable(struct ufs_hba * hba)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 
ufshcd_hba_init_crypto(struct ufs_hba * hba)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);
432 }
433 
ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba * hba,struct request_queue * q)434 void ufshcd_crypto_setup_rq_keyslot_manager(struct ufs_hba *hba,
435 					    struct request_queue *q)
436 {
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 
ufshcd_crypto_destroy_rq_keyslot_manager(struct ufs_hba * hba,struct request_queue * q)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 
ufshcd_prepare_lrbp_crypto(struct ufs_hba * hba,struct scsi_cmnd * cmd,struct ufshcd_lrb * lrbp)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 
ufshcd_map_sg_crypto(struct ufs_hba * hba,struct ufshcd_lrb * lrbp)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 
ufshcd_complete_lrbp_crypto(struct ufs_hba * hba,struct scsi_cmnd * cmd,struct ufshcd_lrb * lrbp)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 
ufshcd_crypto_debug(struct ufs_hba * hba)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 
ufshcd_crypto_suspend(struct ufs_hba * hba,enum ufs_pm_op pm_op)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 
ufshcd_crypto_resume(struct ufs_hba * hba,enum ufs_pm_op pm_op)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 
ufshcd_crypto_set_vops(struct ufs_hba * hba,struct ufs_hba_crypto_variant_ops * crypto_vops)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;
508 }
509