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