Skip to content

Commit 30b32c6

Browse files
ebiggersmartinkpetersen
authored andcommitted
scsi: ufs: qcom: Convert to use UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE
By default the UFS core is responsible for initializing the blk_crypto_profile, but Qualcomm platforms have their own way of programming and evicting crypto keys. So currently ufs_hba_variant_ops::program_key is used to redirect control flow from ufshcd_program_key(). This has worked until now, but it's a bit of a hack, given that the key (and algorithm ID etc.) ends up being converted from blk_crypto_key => ufs_crypto_cfg_entry => SCM call parameters, where the intermediate ufs_crypto_cfg_entry step is unnecessary. Taking a similar approach with the upcoming wrapped key support, the implementation of which is similarly platform-specific, would require adding four new methods to ufs_hba_variant_ops, changing program_key to take the struct blk_crypto_key, and adding a new UFSHCD_CAP_* flag to indicate support for wrapped keys. This patch takes a different approach. It changes ufs-qcom to use the existing UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE which was recently added for ufs-exynos. This allows it to override the full blk_crypto_profile, eliminating the need for the existing ufs_hba_variant_ops::program_key and the hooks that would have been needed for wrapped key support. It does require a bit of duplicated code to read the crypto capability registers, but it's worth the simplification in design with ufs-qcom and ufs-exynos now using the same method to customize the crypto profile, and it makes it much easier to add wrapped key support. Tested-by: Bartosz Golaszewski <[email protected]> # sm8650 Signed-off-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 75d0c64 commit 30b32c6

File tree

1 file changed

+72
-19
lines changed

1 file changed

+72
-19
lines changed

drivers/ufs/host/ufs-qcom.c

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,18 @@ static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host)
112112
qcom_ice_enable(host->ice);
113113
}
114114

115+
static const struct blk_crypto_ll_ops ufs_qcom_crypto_ops; /* forward decl */
116+
115117
static int ufs_qcom_ice_init(struct ufs_qcom_host *host)
116118
{
117119
struct ufs_hba *hba = host->hba;
120+
struct blk_crypto_profile *profile = &hba->crypto_profile;
118121
struct device *dev = hba->dev;
119122
struct qcom_ice *ice;
123+
union ufs_crypto_capabilities caps;
124+
union ufs_crypto_cap_entry cap;
125+
int err;
126+
int i;
120127

121128
ice = of_qcom_ice_get(dev);
122129
if (ice == ERR_PTR(-EOPNOTSUPP)) {
@@ -128,8 +135,38 @@ static int ufs_qcom_ice_init(struct ufs_qcom_host *host)
128135
return PTR_ERR_OR_ZERO(ice);
129136

130137
host->ice = ice;
131-
hba->caps |= UFSHCD_CAP_CRYPTO;
132138

139+
/* Initialize the blk_crypto_profile */
140+
141+
caps.reg_val = cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP));
142+
143+
/* The number of keyslots supported is (CFGC+1) */
144+
err = devm_blk_crypto_profile_init(dev, profile, caps.config_count + 1);
145+
if (err)
146+
return err;
147+
148+
profile->ll_ops = ufs_qcom_crypto_ops;
149+
profile->max_dun_bytes_supported = 8;
150+
profile->dev = dev;
151+
152+
/*
153+
* Currently this driver only supports AES-256-XTS. All known versions
154+
* of ICE support it, but to be safe make sure it is really declared in
155+
* the crypto capability registers. The crypto capability registers
156+
* also give the supported data unit size(s).
157+
*/
158+
for (i = 0; i < caps.num_crypto_cap; i++) {
159+
cap.reg_val = cpu_to_le32(ufshcd_readl(hba,
160+
REG_UFS_CRYPTOCAP +
161+
i * sizeof(__le32)));
162+
if (cap.algorithm_id == UFS_CRYPTO_ALG_AES_XTS &&
163+
cap.key_size == UFS_CRYPTO_KEY_SIZE_256)
164+
profile->modes_supported[BLK_ENCRYPTION_MODE_AES_256_XTS] |=
165+
cap.sdus_mask * 512;
166+
}
167+
168+
hba->caps |= UFSHCD_CAP_CRYPTO;
169+
hba->quirks |= UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE;
133170
return 0;
134171
}
135172

@@ -149,32 +186,49 @@ static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host)
149186
return 0;
150187
}
151188

152-
static int ufs_qcom_ice_program_key(struct ufs_hba *hba,
153-
const union ufs_crypto_cfg_entry *cfg,
154-
int slot)
189+
static int ufs_qcom_ice_keyslot_program(struct blk_crypto_profile *profile,
190+
const struct blk_crypto_key *key,
191+
unsigned int slot)
155192
{
193+
struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
156194
struct ufs_qcom_host *host = ufshcd_get_variant(hba);
157-
union ufs_crypto_cap_entry cap;
158-
159-
if (!(cfg->config_enable & UFS_CRYPTO_CONFIGURATION_ENABLE))
160-
return qcom_ice_evict_key(host->ice, slot);
195+
int err;
161196

162197
/* Only AES-256-XTS has been tested so far. */
163-
cap = hba->crypto_cap_array[cfg->crypto_cap_idx];
164-
if (cap.algorithm_id != UFS_CRYPTO_ALG_AES_XTS ||
165-
cap.key_size != UFS_CRYPTO_KEY_SIZE_256)
198+
if (key->crypto_cfg.crypto_mode != BLK_ENCRYPTION_MODE_AES_256_XTS)
166199
return -EOPNOTSUPP;
167200

168-
return qcom_ice_program_key(host->ice,
169-
QCOM_ICE_CRYPTO_ALG_AES_XTS,
170-
QCOM_ICE_CRYPTO_KEY_SIZE_256,
171-
cfg->crypto_key,
172-
cfg->data_unit_size, slot);
201+
ufshcd_hold(hba);
202+
err = qcom_ice_program_key(host->ice,
203+
QCOM_ICE_CRYPTO_ALG_AES_XTS,
204+
QCOM_ICE_CRYPTO_KEY_SIZE_256,
205+
key->raw,
206+
key->crypto_cfg.data_unit_size / 512,
207+
slot);
208+
ufshcd_release(hba);
209+
return err;
173210
}
174211

175-
#else
212+
static int ufs_qcom_ice_keyslot_evict(struct blk_crypto_profile *profile,
213+
const struct blk_crypto_key *key,
214+
unsigned int slot)
215+
{
216+
struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
217+
struct ufs_qcom_host *host = ufshcd_get_variant(hba);
218+
int err;
219+
220+
ufshcd_hold(hba);
221+
err = qcom_ice_evict_key(host->ice, slot);
222+
ufshcd_release(hba);
223+
return err;
224+
}
176225

177-
#define ufs_qcom_ice_program_key NULL
226+
static const struct blk_crypto_ll_ops ufs_qcom_crypto_ops = {
227+
.keyslot_program = ufs_qcom_ice_keyslot_program,
228+
.keyslot_evict = ufs_qcom_ice_keyslot_evict,
229+
};
230+
231+
#else
178232

179233
static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host)
180234
{
@@ -1822,7 +1876,6 @@ static const struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
18221876
.dbg_register_dump = ufs_qcom_dump_dbg_regs,
18231877
.device_reset = ufs_qcom_device_reset,
18241878
.config_scaling_param = ufs_qcom_config_scaling_param,
1825-
.program_key = ufs_qcom_ice_program_key,
18261879
.reinit_notify = ufs_qcom_reinit_notify,
18271880
.mcq_config_resource = ufs_qcom_mcq_config_resource,
18281881
.get_hba_mac = ufs_qcom_get_hba_mac,

0 commit comments

Comments
 (0)