Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scsi: ufs: ufs-qcom: Switch to the new ICE API

Now that there is a new dedicated ICE driver, drop the ufs-qcom-ice and use
the new ICE api provided by the Qualcomm soc driver ice. The platforms that
already have ICE support will use the API as library since there will not
be a devicetree node, but instead they have reg range. In this case, the
of_qcom_ice_get will return an ICE instance created for the consumer's
device. But if there are platforms that do not have ice reg in the consumer
devicetree node and instead provide a dedicated ICE devicetree node, the
of_qcom_ice_get will look up the device based on qcom,ice property and will
get the ICE instance registered by the probe function of the ice driver.

The ICE clock is now handle by the new driver. This is done by enabling it
on the creation of the ICE instance and then enabling/disabling it on UFS
runtime resume/suspend.

Signed-off-by: Abel Vesa <abel.vesa@linaro.org>
Link: https://lore.kernel.org/r/20230612192847.1599416-3-abel.vesa@linaro.org
Reviewed-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Abel Vesa and committed by
Martin K. Petersen
56541c7c 29a6d121

+104 -277
+1 -1
drivers/ufs/host/Kconfig
··· 59 59 depends on SCSI_UFSHCD_PLATFORM && ARCH_QCOM 60 60 depends on GENERIC_MSI_IRQ 61 61 depends on RESET_CONTROLLER 62 - select QCOM_SCM if SCSI_UFS_CRYPTO 62 + select QCOM_INLINE_CRYPTO_ENGINE if SCSI_UFS_CRYPTO 63 63 help 64 64 This selects the QCOM specific additions to UFSHCD platform driver. 65 65 UFS host on QCOM needs some vendor specific configuration before
+1 -3
drivers/ufs/host/Makefile
··· 3 3 obj-$(CONFIG_SCSI_UFS_DWC_TC_PCI) += tc-dwc-g210-pci.o ufshcd-dwc.o tc-dwc-g210.o 4 4 obj-$(CONFIG_SCSI_UFS_DWC_TC_PLATFORM) += tc-dwc-g210-pltfrm.o ufshcd-dwc.o tc-dwc-g210.o 5 5 obj-$(CONFIG_SCSI_UFS_CDNS_PLATFORM) += cdns-pltfrm.o 6 - obj-$(CONFIG_SCSI_UFS_QCOM) += ufs_qcom.o 7 - ufs_qcom-y += ufs-qcom.o 8 - ufs_qcom-$(CONFIG_SCSI_UFS_CRYPTO) += ufs-qcom-ice.o 6 + obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o 9 7 obj-$(CONFIG_SCSI_UFS_EXYNOS) += ufs-exynos.o 10 8 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o 11 9 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
-244
drivers/ufs/host/ufs-qcom-ice.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-only 2 - /* 3 - * Qualcomm ICE (Inline Crypto Engine) support. 4 - * 5 - * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved. 6 - * Copyright 2019 Google LLC 7 - */ 8 - 9 - #include <linux/delay.h> 10 - #include <linux/platform_device.h> 11 - #include <linux/firmware/qcom/qcom_scm.h> 12 - 13 - #include "ufs-qcom.h" 14 - 15 - #define AES_256_XTS_KEY_SIZE 64 16 - 17 - /* QCOM ICE registers */ 18 - 19 - #define QCOM_ICE_REG_CONTROL 0x0000 20 - #define QCOM_ICE_REG_RESET 0x0004 21 - #define QCOM_ICE_REG_VERSION 0x0008 22 - #define QCOM_ICE_REG_FUSE_SETTING 0x0010 23 - #define QCOM_ICE_REG_PARAMETERS_1 0x0014 24 - #define QCOM_ICE_REG_PARAMETERS_2 0x0018 25 - #define QCOM_ICE_REG_PARAMETERS_3 0x001C 26 - #define QCOM_ICE_REG_PARAMETERS_4 0x0020 27 - #define QCOM_ICE_REG_PARAMETERS_5 0x0024 28 - 29 - /* QCOM ICE v3.X only */ 30 - #define QCOM_ICE_GENERAL_ERR_STTS 0x0040 31 - #define QCOM_ICE_INVALID_CCFG_ERR_STTS 0x0030 32 - #define QCOM_ICE_GENERAL_ERR_MASK 0x0044 33 - 34 - /* QCOM ICE v2.X only */ 35 - #define QCOM_ICE_REG_NON_SEC_IRQ_STTS 0x0040 36 - #define QCOM_ICE_REG_NON_SEC_IRQ_MASK 0x0044 37 - 38 - #define QCOM_ICE_REG_NON_SEC_IRQ_CLR 0x0048 39 - #define QCOM_ICE_REG_STREAM1_ERROR_SYNDROME1 0x0050 40 - #define QCOM_ICE_REG_STREAM1_ERROR_SYNDROME2 0x0054 41 - #define QCOM_ICE_REG_STREAM2_ERROR_SYNDROME1 0x0058 42 - #define QCOM_ICE_REG_STREAM2_ERROR_SYNDROME2 0x005C 43 - #define QCOM_ICE_REG_STREAM1_BIST_ERROR_VEC 0x0060 44 - #define QCOM_ICE_REG_STREAM2_BIST_ERROR_VEC 0x0064 45 - #define QCOM_ICE_REG_STREAM1_BIST_FINISH_VEC 0x0068 46 - #define QCOM_ICE_REG_STREAM2_BIST_FINISH_VEC 0x006C 47 - #define QCOM_ICE_REG_BIST_STATUS 0x0070 48 - #define QCOM_ICE_REG_BYPASS_STATUS 0x0074 49 - #define QCOM_ICE_REG_ADVANCED_CONTROL 0x1000 50 - #define QCOM_ICE_REG_ENDIAN_SWAP 0x1004 51 - #define QCOM_ICE_REG_TEST_BUS_CONTROL 0x1010 52 - #define QCOM_ICE_REG_TEST_BUS_REG 0x1014 53 - 54 - /* BIST ("built-in self-test"?) status flags */ 55 - #define QCOM_ICE_BIST_STATUS_MASK 0xF0000000 56 - 57 - #define QCOM_ICE_FUSE_SETTING_MASK 0x1 58 - #define QCOM_ICE_FORCE_HW_KEY0_SETTING_MASK 0x2 59 - #define QCOM_ICE_FORCE_HW_KEY1_SETTING_MASK 0x4 60 - 61 - #define qcom_ice_writel(host, val, reg) \ 62 - writel((val), (host)->ice_mmio + (reg)) 63 - #define qcom_ice_readl(host, reg) \ 64 - readl((host)->ice_mmio + (reg)) 65 - 66 - static bool qcom_ice_supported(struct ufs_qcom_host *host) 67 - { 68 - struct device *dev = host->hba->dev; 69 - u32 regval = qcom_ice_readl(host, QCOM_ICE_REG_VERSION); 70 - int major = regval >> 24; 71 - int minor = (regval >> 16) & 0xFF; 72 - int step = regval & 0xFFFF; 73 - 74 - /* For now this driver only supports ICE version 3. */ 75 - if (major != 3) { 76 - dev_warn(dev, "Unsupported ICE version: v%d.%d.%d\n", 77 - major, minor, step); 78 - return false; 79 - } 80 - 81 - dev_info(dev, "Found QC Inline Crypto Engine (ICE) v%d.%d.%d\n", 82 - major, minor, step); 83 - 84 - /* If fuses are blown, ICE might not work in the standard way. */ 85 - regval = qcom_ice_readl(host, QCOM_ICE_REG_FUSE_SETTING); 86 - if (regval & (QCOM_ICE_FUSE_SETTING_MASK | 87 - QCOM_ICE_FORCE_HW_KEY0_SETTING_MASK | 88 - QCOM_ICE_FORCE_HW_KEY1_SETTING_MASK)) { 89 - dev_warn(dev, "Fuses are blown; ICE is unusable!\n"); 90 - return false; 91 - } 92 - return true; 93 - } 94 - 95 - int ufs_qcom_ice_init(struct ufs_qcom_host *host) 96 - { 97 - struct ufs_hba *hba = host->hba; 98 - struct device *dev = hba->dev; 99 - struct platform_device *pdev = to_platform_device(dev); 100 - struct resource *res; 101 - int err; 102 - 103 - if (!(ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES) & 104 - MASK_CRYPTO_SUPPORT)) 105 - return 0; 106 - 107 - res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ice"); 108 - if (!res) { 109 - dev_warn(dev, "ICE registers not found\n"); 110 - goto disable; 111 - } 112 - 113 - if (!qcom_scm_ice_available()) { 114 - dev_warn(dev, "ICE SCM interface not found\n"); 115 - goto disable; 116 - } 117 - 118 - host->ice_mmio = devm_ioremap_resource(dev, res); 119 - if (IS_ERR(host->ice_mmio)) { 120 - err = PTR_ERR(host->ice_mmio); 121 - return err; 122 - } 123 - 124 - if (!qcom_ice_supported(host)) 125 - goto disable; 126 - 127 - return 0; 128 - 129 - disable: 130 - dev_warn(dev, "Disabling inline encryption support\n"); 131 - hba->caps &= ~UFSHCD_CAP_CRYPTO; 132 - return 0; 133 - } 134 - 135 - static void qcom_ice_low_power_mode_enable(struct ufs_qcom_host *host) 136 - { 137 - u32 regval; 138 - 139 - regval = qcom_ice_readl(host, QCOM_ICE_REG_ADVANCED_CONTROL); 140 - /* 141 - * Enable low power mode sequence 142 - * [0]-0, [1]-0, [2]-0, [3]-E, [4]-0, [5]-0, [6]-0, [7]-0 143 - */ 144 - regval |= 0x7000; 145 - qcom_ice_writel(host, regval, QCOM_ICE_REG_ADVANCED_CONTROL); 146 - } 147 - 148 - static void qcom_ice_optimization_enable(struct ufs_qcom_host *host) 149 - { 150 - u32 regval; 151 - 152 - /* ICE Optimizations Enable Sequence */ 153 - regval = qcom_ice_readl(host, QCOM_ICE_REG_ADVANCED_CONTROL); 154 - regval |= 0xD807100; 155 - /* ICE HPG requires delay before writing */ 156 - udelay(5); 157 - qcom_ice_writel(host, regval, QCOM_ICE_REG_ADVANCED_CONTROL); 158 - udelay(5); 159 - } 160 - 161 - int ufs_qcom_ice_enable(struct ufs_qcom_host *host) 162 - { 163 - if (!(host->hba->caps & UFSHCD_CAP_CRYPTO)) 164 - return 0; 165 - qcom_ice_low_power_mode_enable(host); 166 - qcom_ice_optimization_enable(host); 167 - return ufs_qcom_ice_resume(host); 168 - } 169 - 170 - /* Poll until all BIST bits are reset */ 171 - static int qcom_ice_wait_bist_status(struct ufs_qcom_host *host) 172 - { 173 - int count; 174 - u32 reg; 175 - 176 - for (count = 0; count < 100; count++) { 177 - reg = qcom_ice_readl(host, QCOM_ICE_REG_BIST_STATUS); 178 - if (!(reg & QCOM_ICE_BIST_STATUS_MASK)) 179 - break; 180 - udelay(50); 181 - } 182 - if (reg) 183 - return -ETIMEDOUT; 184 - return 0; 185 - } 186 - 187 - int ufs_qcom_ice_resume(struct ufs_qcom_host *host) 188 - { 189 - int err; 190 - 191 - if (!(host->hba->caps & UFSHCD_CAP_CRYPTO)) 192 - return 0; 193 - 194 - err = qcom_ice_wait_bist_status(host); 195 - if (err) { 196 - dev_err(host->hba->dev, "BIST status error (%d)\n", err); 197 - return err; 198 - } 199 - return 0; 200 - } 201 - 202 - /* 203 - * Program a key into a QC ICE keyslot, or evict a keyslot. QC ICE requires 204 - * vendor-specific SCM calls for this; it doesn't support the standard way. 205 - */ 206 - int ufs_qcom_ice_program_key(struct ufs_hba *hba, 207 - const union ufs_crypto_cfg_entry *cfg, int slot) 208 - { 209 - union ufs_crypto_cap_entry cap; 210 - union { 211 - u8 bytes[AES_256_XTS_KEY_SIZE]; 212 - u32 words[AES_256_XTS_KEY_SIZE / sizeof(u32)]; 213 - } key; 214 - int i; 215 - int err; 216 - 217 - if (!(cfg->config_enable & UFS_CRYPTO_CONFIGURATION_ENABLE)) 218 - return qcom_scm_ice_invalidate_key(slot); 219 - 220 - /* Only AES-256-XTS has been tested so far. */ 221 - cap = hba->crypto_cap_array[cfg->crypto_cap_idx]; 222 - if (cap.algorithm_id != UFS_CRYPTO_ALG_AES_XTS || 223 - cap.key_size != UFS_CRYPTO_KEY_SIZE_256) { 224 - dev_err_ratelimited(hba->dev, 225 - "Unhandled crypto capability; algorithm_id=%d, key_size=%d\n", 226 - cap.algorithm_id, cap.key_size); 227 - return -EINVAL; 228 - } 229 - 230 - memcpy(key.bytes, cfg->crypto_key, AES_256_XTS_KEY_SIZE); 231 - 232 - /* 233 - * The SCM call byte-swaps the 32-bit words of the key. So we have to 234 - * do the same, in order for the final key be correct. 235 - */ 236 - for (i = 0; i < ARRAY_SIZE(key.words); i++) 237 - __cpu_to_be32s(&key.words[i]); 238 - 239 - err = qcom_scm_ice_set_key(slot, key.bytes, AES_256_XTS_KEY_SIZE, 240 - QCOM_SCM_ICE_CIPHER_AES_256_XTS, 241 - cfg->data_unit_size); 242 - memzero_explicit(&key, sizeof(key)); 243 - return err; 244 - }
+97 -2
drivers/ufs/host/ufs-qcom.c
··· 15 15 #include <linux/reset-controller.h> 16 16 #include <linux/devfreq.h> 17 17 18 + #include <soc/qcom/ice.h> 19 + 18 20 #include <ufs/ufshcd.h> 19 21 #include "ufshcd-pltfrm.h" 20 22 #include <ufs/unipro.h> ··· 56 54 { 57 55 return container_of(rcd, struct ufs_qcom_host, rcdev); 58 56 } 57 + 58 + #ifdef CONFIG_SCSI_UFS_CRYPTO 59 + 60 + static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host) 61 + { 62 + if (host->hba->caps & UFSHCD_CAP_CRYPTO) 63 + qcom_ice_enable(host->ice); 64 + } 65 + 66 + static int ufs_qcom_ice_init(struct ufs_qcom_host *host) 67 + { 68 + struct ufs_hba *hba = host->hba; 69 + struct device *dev = hba->dev; 70 + struct qcom_ice *ice; 71 + 72 + ice = of_qcom_ice_get(dev); 73 + if (ice == ERR_PTR(-EOPNOTSUPP)) { 74 + dev_warn(dev, "Disabling inline encryption support\n"); 75 + ice = NULL; 76 + } 77 + 78 + if (IS_ERR_OR_NULL(ice)) 79 + return PTR_ERR_OR_ZERO(ice); 80 + 81 + host->ice = ice; 82 + hba->caps |= UFSHCD_CAP_CRYPTO; 83 + 84 + return 0; 85 + } 86 + 87 + static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host) 88 + { 89 + if (host->hba->caps & UFSHCD_CAP_CRYPTO) 90 + return qcom_ice_resume(host->ice); 91 + 92 + return 0; 93 + } 94 + 95 + static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host) 96 + { 97 + if (host->hba->caps & UFSHCD_CAP_CRYPTO) 98 + return qcom_ice_suspend(host->ice); 99 + 100 + return 0; 101 + } 102 + 103 + static int ufs_qcom_ice_program_key(struct ufs_hba *hba, 104 + const union ufs_crypto_cfg_entry *cfg, 105 + int slot) 106 + { 107 + struct ufs_qcom_host *host = ufshcd_get_variant(hba); 108 + union ufs_crypto_cap_entry cap; 109 + bool config_enable = 110 + cfg->config_enable & UFS_CRYPTO_CONFIGURATION_ENABLE; 111 + 112 + /* Only AES-256-XTS has been tested so far. */ 113 + cap = hba->crypto_cap_array[cfg->crypto_cap_idx]; 114 + if (cap.algorithm_id != UFS_CRYPTO_ALG_AES_XTS || 115 + cap.key_size != UFS_CRYPTO_KEY_SIZE_256) 116 + return -EINVAL; 117 + 118 + if (config_enable) 119 + return qcom_ice_program_key(host->ice, 120 + QCOM_ICE_CRYPTO_ALG_AES_XTS, 121 + QCOM_ICE_CRYPTO_KEY_SIZE_256, 122 + cfg->crypto_key, 123 + cfg->data_unit_size, slot); 124 + else 125 + return qcom_ice_evict_key(host->ice, slot); 126 + } 127 + 128 + #else 129 + 130 + #define ufs_qcom_ice_program_key NULL 131 + 132 + static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host) 133 + { 134 + } 135 + 136 + static int ufs_qcom_ice_init(struct ufs_qcom_host *host) 137 + { 138 + return 0; 139 + } 140 + 141 + static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host) 142 + { 143 + return 0; 144 + } 145 + 146 + static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host) 147 + { 148 + return 0; 149 + } 150 + #endif 59 151 60 152 static int ufs_qcom_host_clk_get(struct device *dev, 61 153 const char *name, struct clk **clk_out, bool optional) ··· 703 607 ufs_qcom_disable_lane_clks(host); 704 608 } 705 609 706 - return 0; 610 + return ufs_qcom_ice_suspend(host); 707 611 } 708 612 709 613 static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) ··· 949 853 hba->caps |= UFSHCD_CAP_CLK_SCALING | UFSHCD_CAP_WB_WITH_CLK_SCALING; 950 854 hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND; 951 855 hba->caps |= UFSHCD_CAP_WB_EN; 952 - hba->caps |= UFSHCD_CAP_CRYPTO; 953 856 hba->caps |= UFSHCD_CAP_AGGR_POWER_COLLAPSE; 954 857 hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND; 955 858
+5 -27
drivers/ufs/host/ufs-qcom.h
··· 7 7 8 8 #include <linux/reset-controller.h> 9 9 #include <linux/reset.h> 10 + #include <soc/qcom/ice.h> 10 11 #include <ufs/ufshcd.h> 11 12 12 13 #define MAX_UFS_QCOM_HOSTS 1 ··· 206 205 struct clk *tx_l1_sync_clk; 207 206 bool is_lane_clks_enabled; 208 207 208 + #ifdef CONFIG_SCSI_UFS_CRYPTO 209 + struct qcom_ice *ice; 210 + #endif 211 + 209 212 void __iomem *dev_ref_clk_ctrl_mmio; 210 213 bool is_dev_ref_clk_enabled; 211 214 struct ufs_hw_version hw_ver; 212 - #ifdef CONFIG_SCSI_UFS_CRYPTO 213 - void __iomem *ice_mmio; 214 - #endif 215 215 216 216 u32 dev_ref_clk_en_mask; 217 217 ··· 249 247 { 250 248 return host->caps & UFS_QCOM_CAP_QUNIPRO; 251 249 } 252 - 253 - /* ufs-qcom-ice.c */ 254 - 255 - #ifdef CONFIG_SCSI_UFS_CRYPTO 256 - int ufs_qcom_ice_init(struct ufs_qcom_host *host); 257 - int ufs_qcom_ice_enable(struct ufs_qcom_host *host); 258 - int ufs_qcom_ice_resume(struct ufs_qcom_host *host); 259 - int ufs_qcom_ice_program_key(struct ufs_hba *hba, 260 - const union ufs_crypto_cfg_entry *cfg, int slot); 261 - #else 262 - static inline int ufs_qcom_ice_init(struct ufs_qcom_host *host) 263 - { 264 - return 0; 265 - } 266 - static inline int ufs_qcom_ice_enable(struct ufs_qcom_host *host) 267 - { 268 - return 0; 269 - } 270 - static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host) 271 - { 272 - return 0; 273 - } 274 - #define ufs_qcom_ice_program_key NULL 275 - #endif /* !CONFIG_SCSI_UFS_CRYPTO */ 276 250 277 251 #endif /* UFS_QCOM_H_ */