1
2
3
4
5
6
7
8#include <linux/blk-crypto.h>
9#include <linux/keyslot-manager.h>
10#include <linux/mmc/host.h>
11
12#include "cqhci-crypto.h"
13
14
15static const struct cqhci_crypto_alg_entry {
16 enum cqhci_crypto_alg alg;
17 enum cqhci_crypto_key_size key_size;
18} cqhci_crypto_algs[BLK_ENCRYPTION_MODE_MAX] = {
19 [BLK_ENCRYPTION_MODE_AES_256_XTS] = {
20 .alg = CQHCI_CRYPTO_ALG_AES_XTS,
21 .key_size = CQHCI_CRYPTO_KEY_SIZE_256,
22 },
23};
24
25static inline struct cqhci_host *
26cqhci_host_from_ksm(struct blk_keyslot_manager *ksm)
27{
28 struct mmc_host *mmc = container_of(ksm, struct mmc_host, ksm);
29
30 return mmc->cqe_private;
31}
32
33static int cqhci_crypto_program_key(struct cqhci_host *cq_host,
34 const union cqhci_crypto_cfg_entry *cfg,
35 int slot)
36{
37 u32 slot_offset = cq_host->crypto_cfg_register + slot * sizeof(*cfg);
38 int i;
39
40 if (cq_host->ops->program_key)
41 return cq_host->ops->program_key(cq_host, cfg, slot);
42
43
44 cqhci_writel(cq_host, 0, slot_offset + 16 * sizeof(cfg->reg_val[0]));
45
46
47 for (i = 0; i < 16; i++) {
48 cqhci_writel(cq_host, le32_to_cpu(cfg->reg_val[i]),
49 slot_offset + i * sizeof(cfg->reg_val[0]));
50 }
51
52 cqhci_writel(cq_host, le32_to_cpu(cfg->reg_val[17]),
53 slot_offset + 17 * sizeof(cfg->reg_val[0]));
54
55 cqhci_writel(cq_host, le32_to_cpu(cfg->reg_val[16]),
56 slot_offset + 16 * sizeof(cfg->reg_val[0]));
57 return 0;
58}
59
60static int cqhci_crypto_keyslot_program(struct blk_keyslot_manager *ksm,
61 const struct blk_crypto_key *key,
62 unsigned int slot)
63
64{
65 struct cqhci_host *cq_host = cqhci_host_from_ksm(ksm);
66 const union cqhci_crypto_cap_entry *ccap_array =
67 cq_host->crypto_cap_array;
68 const struct cqhci_crypto_alg_entry *alg =
69 &cqhci_crypto_algs[key->crypto_cfg.crypto_mode];
70 u8 data_unit_mask = key->crypto_cfg.data_unit_size / 512;
71 int i;
72 int cap_idx = -1;
73 union cqhci_crypto_cfg_entry cfg = {};
74 int err;
75
76 BUILD_BUG_ON(CQHCI_CRYPTO_KEY_SIZE_INVALID != 0);
77 for (i = 0; i < cq_host->crypto_capabilities.num_crypto_cap; i++) {
78 if (ccap_array[i].algorithm_id == alg->alg &&
79 ccap_array[i].key_size == alg->key_size &&
80 (ccap_array[i].sdus_mask & data_unit_mask)) {
81 cap_idx = i;
82 break;
83 }
84 }
85 if (WARN_ON(cap_idx < 0))
86 return -EOPNOTSUPP;
87
88 cfg.data_unit_size = data_unit_mask;
89 cfg.crypto_cap_idx = cap_idx;
90 cfg.config_enable = CQHCI_CRYPTO_CONFIGURATION_ENABLE;
91
92 if (ccap_array[cap_idx].algorithm_id == CQHCI_CRYPTO_ALG_AES_XTS) {
93
94 memcpy(cfg.crypto_key, key->raw, key->size/2);
95 memcpy(cfg.crypto_key + CQHCI_CRYPTO_KEY_MAX_SIZE/2,
96 key->raw + key->size/2, key->size/2);
97 } else {
98 memcpy(cfg.crypto_key, key->raw, key->size);
99 }
100
101 err = cqhci_crypto_program_key(cq_host, &cfg, slot);
102
103 memzero_explicit(&cfg, sizeof(cfg));
104 return err;
105}
106
107static int cqhci_crypto_clear_keyslot(struct cqhci_host *cq_host, int slot)
108{
109
110
111
112
113 union cqhci_crypto_cfg_entry cfg = {};
114
115 return cqhci_crypto_program_key(cq_host, &cfg, slot);
116}
117
118static int cqhci_crypto_keyslot_evict(struct blk_keyslot_manager *ksm,
119 const struct blk_crypto_key *key,
120 unsigned int slot)
121{
122 struct cqhci_host *cq_host = cqhci_host_from_ksm(ksm);
123
124 return cqhci_crypto_clear_keyslot(cq_host, slot);
125}
126
127
128
129
130
131
132
133
134
135static const struct blk_ksm_ll_ops cqhci_ksm_ops = {
136 .keyslot_program = cqhci_crypto_keyslot_program,
137 .keyslot_evict = cqhci_crypto_keyslot_evict,
138};
139
140static enum blk_crypto_mode_num
141cqhci_find_blk_crypto_mode(union cqhci_crypto_cap_entry cap)
142{
143 int i;
144
145 for (i = 0; i < ARRAY_SIZE(cqhci_crypto_algs); i++) {
146 BUILD_BUG_ON(CQHCI_CRYPTO_KEY_SIZE_INVALID != 0);
147 if (cqhci_crypto_algs[i].alg == cap.algorithm_id &&
148 cqhci_crypto_algs[i].key_size == cap.key_size)
149 return i;
150 }
151 return BLK_ENCRYPTION_MODE_INVALID;
152}
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167int cqhci_crypto_init(struct cqhci_host *cq_host)
168{
169 struct mmc_host *mmc = cq_host->mmc;
170 struct device *dev = mmc_dev(mmc);
171 struct blk_keyslot_manager *ksm = &mmc->ksm;
172 unsigned int num_keyslots;
173 unsigned int cap_idx;
174 enum blk_crypto_mode_num blk_mode_num;
175 unsigned int slot;
176 int err = 0;
177
178 if (!(mmc->caps2 & MMC_CAP2_CRYPTO) ||
179 !(cqhci_readl(cq_host, CQHCI_CAP) & CQHCI_CAP_CS))
180 goto out;
181
182 cq_host->crypto_capabilities.reg_val =
183 cpu_to_le32(cqhci_readl(cq_host, CQHCI_CCAP));
184
185 cq_host->crypto_cfg_register =
186 (u32)cq_host->crypto_capabilities.config_array_ptr * 0x100;
187
188 cq_host->crypto_cap_array =
189 devm_kcalloc(dev, cq_host->crypto_capabilities.num_crypto_cap,
190 sizeof(cq_host->crypto_cap_array[0]), GFP_KERNEL);
191 if (!cq_host->crypto_cap_array) {
192 err = -ENOMEM;
193 goto out;
194 }
195
196
197
198
199
200 num_keyslots = cq_host->crypto_capabilities.config_count + 1;
201
202 err = devm_blk_ksm_init(dev, ksm, num_keyslots);
203 if (err)
204 goto out;
205
206 ksm->ksm_ll_ops = cqhci_ksm_ops;
207 ksm->dev = dev;
208
209
210 ksm->max_dun_bytes_supported = 4;
211
212
213
214
215
216 for (cap_idx = 0; cap_idx < cq_host->crypto_capabilities.num_crypto_cap;
217 cap_idx++) {
218 cq_host->crypto_cap_array[cap_idx].reg_val =
219 cpu_to_le32(cqhci_readl(cq_host,
220 CQHCI_CRYPTOCAP +
221 cap_idx * sizeof(__le32)));
222 blk_mode_num = cqhci_find_blk_crypto_mode(
223 cq_host->crypto_cap_array[cap_idx]);
224 if (blk_mode_num == BLK_ENCRYPTION_MODE_INVALID)
225 continue;
226 ksm->crypto_modes_supported[blk_mode_num] |=
227 cq_host->crypto_cap_array[cap_idx].sdus_mask * 512;
228 }
229
230
231 for (slot = 0; slot < num_keyslots; slot++)
232 cqhci_crypto_clear_keyslot(cq_host, slot);
233
234
235 cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
236
237 return 0;
238
239out:
240 mmc->caps2 &= ~MMC_CAP2_CRYPTO;
241 return err;
242}
243