crypto-qti-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Common crypto library for storage encryption.
  4. *
  5. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  6. */
  7. #include <linux/crypto-qti-common.h>
  8. #include <linux/module.h>
  9. #include "crypto-qti-ice-regs.h"
  10. #include "crypto-qti-platform.h"
  11. static int ice_check_fuse_setting(void __iomem *ice_mmio)
  12. {
  13. uint32_t regval;
  14. uint32_t version, major, minor;
  15. version = ice_readl(ice_mmio, ICE_REGS_VERSION);
  16. major = (version & ICE_CORE_MAJOR_REV_MASK) >>
  17. ICE_CORE_MAJOR_REV;
  18. minor = (version & ICE_CORE_MINOR_REV_MASK) >>
  19. ICE_CORE_MINOR_REV;
  20. //Check fuse setting is not supported on ICE 3.2 onwards
  21. if ((major == 0x03) && (minor >= 0x02))
  22. return 0;
  23. regval = ice_readl(ice_mmio, ICE_REGS_FUSE_SETTING);
  24. regval &= (ICE_FUSE_SETTING_MASK |
  25. ICE_FORCE_HW_KEY0_SETTING_MASK |
  26. ICE_FORCE_HW_KEY1_SETTING_MASK);
  27. if (regval) {
  28. pr_err("%s: error: ICE_ERROR_HW_DISABLE_FUSE_BLOWN\n",
  29. __func__);
  30. return -EPERM;
  31. }
  32. return 0;
  33. }
  34. static int ice_check_version(void __iomem *ice_mmio)
  35. {
  36. uint32_t version, major, minor, step;
  37. version = ice_readl(ice_mmio, ICE_REGS_VERSION);
  38. major = (version & ICE_CORE_MAJOR_REV_MASK) >> ICE_CORE_MAJOR_REV;
  39. minor = (version & ICE_CORE_MINOR_REV_MASK) >> ICE_CORE_MINOR_REV;
  40. step = (version & ICE_CORE_STEP_REV_MASK) >> ICE_CORE_STEP_REV;
  41. if (major < ICE_CORE_CURRENT_MAJOR_VERSION) {
  42. pr_err("%s: Unknown ICE device at %lu, rev %d.%d.%d\n",
  43. __func__, (unsigned long)ice_mmio,
  44. major, minor, step);
  45. return -ENODEV;
  46. }
  47. return 0;
  48. }
  49. int crypto_qti_init_crypto(void *mmio_data)
  50. {
  51. int err = 0;
  52. void __iomem *ice_mmio = (void __iomem *) mmio_data;
  53. err = ice_check_version(ice_mmio);
  54. if (err) {
  55. pr_err("%s: check version failed, err %d\n", __func__, err);
  56. return err;
  57. }
  58. err = ice_check_fuse_setting(ice_mmio);
  59. if (err)
  60. pr_err("%s: check fuse failed, err %d\n", __func__, err);
  61. return err;
  62. }
  63. EXPORT_SYMBOL(crypto_qti_init_crypto);
  64. static void ice_low_power_and_optimization_enable(void __iomem *ice_mmio)
  65. {
  66. uint32_t regval;
  67. regval = ice_readl(ice_mmio, ICE_REGS_ADVANCED_CONTROL);
  68. /* Enable low power mode sequence
  69. * [0]-0,[1]-0,[2]-0,[3]-7,[4]-0,[5]-0,[6]-0,[7]-0,
  70. * Enable CONFIG_CLK_GATING, STREAM2_CLK_GATING and STREAM1_CLK_GATING
  71. */
  72. regval |= 0x7000;
  73. /* Optimization enable sequence
  74. */
  75. regval |= 0xD807100;
  76. ice_writel(ice_mmio, regval, ICE_REGS_ADVANCED_CONTROL);
  77. /*
  78. * Memory barrier - to ensure write completion before next transaction
  79. */
  80. wmb();
  81. }
  82. static int ice_wait_bist_status(void __iomem *ice_mmio)
  83. {
  84. int count;
  85. uint32_t regval;
  86. for (count = 0; count < QTI_ICE_MAX_BIST_CHECK_COUNT; count++) {
  87. regval = ice_readl(ice_mmio, ICE_REGS_BIST_STATUS);
  88. if (!(regval & ICE_BIST_STATUS_MASK))
  89. break;
  90. udelay(50);
  91. }
  92. if (regval) {
  93. pr_err("%s: wait bist status failed, reg %d\n",
  94. __func__, regval);
  95. return -ETIMEDOUT;
  96. }
  97. return 0;
  98. }
  99. int crypto_qti_enable(void *mmio_data)
  100. {
  101. int err = 0;
  102. void __iomem *ice_mmio = (void __iomem *) mmio_data;
  103. ice_low_power_and_optimization_enable(ice_mmio);
  104. err = ice_wait_bist_status(ice_mmio);
  105. if (err)
  106. return err;
  107. return err;
  108. }
  109. EXPORT_SYMBOL(crypto_qti_enable);
  110. void crypto_qti_disable(void)
  111. {
  112. crypto_qti_disable_platform();
  113. }
  114. EXPORT_SYMBOL(crypto_qti_disable);
  115. int crypto_qti_resume(void *mmio_data)
  116. {
  117. void __iomem *ice_mmio = (void __iomem *) mmio_data;
  118. return ice_wait_bist_status(ice_mmio);
  119. }
  120. EXPORT_SYMBOL(crypto_qti_resume);
  121. static void ice_dump_test_bus(void __iomem *ice_mmio)
  122. {
  123. uint32_t regval = 0x1;
  124. uint32_t val;
  125. uint8_t bus_selector;
  126. uint8_t stream_selector;
  127. pr_err("ICE TEST BUS DUMP:\n");
  128. for (bus_selector = 0; bus_selector <= 0xF; bus_selector++) {
  129. regval = 0x1; /* enable test bus */
  130. regval |= bus_selector << 28;
  131. if (bus_selector == 0xD)
  132. continue;
  133. ice_writel(ice_mmio, regval, ICE_REGS_TEST_BUS_CONTROL);
  134. /*
  135. * make sure test bus selector is written before reading
  136. * the test bus register
  137. */
  138. wmb();
  139. val = ice_readl(ice_mmio, ICE_REGS_TEST_BUS_REG);
  140. pr_err("ICE_TEST_BUS_CONTROL: 0x%08x | ICE_TEST_BUS_REG: 0x%08x\n",
  141. regval, val);
  142. }
  143. pr_err("ICE TEST BUS DUMP (ICE_STREAM1_DATAPATH_TEST_BUS):\n");
  144. for (stream_selector = 0; stream_selector <= 0xF; stream_selector++) {
  145. regval = 0xD0000001; /* enable stream test bus */
  146. regval |= stream_selector << 16;
  147. ice_writel(ice_mmio, regval, ICE_REGS_TEST_BUS_CONTROL);
  148. /*
  149. * make sure test bus selector is written before reading
  150. * the test bus register
  151. */
  152. wmb();
  153. val = ice_readl(ice_mmio, ICE_REGS_TEST_BUS_REG);
  154. pr_err("ICE_TEST_BUS_CONTROL: 0x%08x | ICE_TEST_BUS_REG: 0x%08x\n",
  155. regval, val);
  156. }
  157. }
  158. static void ice_dump_config_regs(void __iomem *ice_mmio)
  159. {
  160. int i = 0;
  161. uint32_t version = 0;
  162. uint32_t major = 0;
  163. uint32_t minor = 0;
  164. version = ice_readl(ice_mmio, ICE_REGS_VERSION);
  165. major = (version & ICE_CORE_MAJOR_REV_MASK) >> ICE_CORE_MAJOR_REV;
  166. minor = (version & ICE_CORE_MINOR_REV_MASK) >> ICE_CORE_MINOR_REV;
  167. if (((major == 3) && (minor >= 2)) || (major > 3)) {
  168. for (i = 0; i < 64; i++) {
  169. pr_err("ICE_CRYPTOCFG_r_16 slot %d: 0x%08x\n", i,
  170. ice_readl(ice_mmio, ICE_LUT_KEYS_CRYPTOCFG_R_16 +
  171. ICE_LUT_KEYS_CRYPTOCFG_OFFSET*i));
  172. pr_err("ICE_CRYPTOCFG_r_17 slot %d: 0x%08x\n", i,
  173. ice_readl(ice_mmio, ICE_LUT_KEYS_CRYPTOCFG_R_17 +
  174. ICE_LUT_KEYS_CRYPTOCFG_OFFSET*i));
  175. }
  176. } else {
  177. for (i = 0; i < 32; i++) {
  178. pr_err("ICE_CRYPTOCFG_r_16 slot %d: 0x%08x\n", i,
  179. ice_readl(ice_mmio, ICE_LUT_KEYS_SW_CRYPTOCFG_R_16 +
  180. ICE_LUT_KEYS_CRYPTOCFG_OFFSET*i));
  181. pr_err("ICE_CRYPTOCFG_r_17 slot %d: 0x%08x\n", i,
  182. ice_readl(ice_mmio, ICE_LUT_KEYS_SW_CRYPTOCFG_R_17 +
  183. ICE_LUT_KEYS_CRYPTOCFG_OFFSET*i));
  184. }
  185. }
  186. }
  187. int crypto_qti_debug(const struct ice_mmio_data *mmio_data)
  188. {
  189. void __iomem *ice_mmio = mmio_data->ice_base_mmio;
  190. pr_err("%s: Dumping ICE registers\n", __func__);
  191. pr_err("ICE Control: 0x%08x | ICE Reset: 0x%08x\n",
  192. ice_readl(ice_mmio, ICE_REGS_CONTROL),
  193. ice_readl(ice_mmio, ICE_REGS_RESET));
  194. pr_err("ICE Version: 0x%08x\n",
  195. ice_readl(ice_mmio, ICE_REGS_VERSION));
  196. pr_err("ICE Param1: 0x%08x | ICE Param2: 0x%08x\n",
  197. ice_readl(ice_mmio, ICE_REGS_PARAMETERS_1),
  198. ice_readl(ice_mmio, ICE_REGS_PARAMETERS_2));
  199. pr_err("ICE Param3: 0x%08x | ICE Param4: 0x%08x\n",
  200. ice_readl(ice_mmio, ICE_REGS_PARAMETERS_3),
  201. ice_readl(ice_mmio, ICE_REGS_PARAMETERS_4));
  202. pr_err("ICE Param5: 0x%08x | ICE IRQ STTS: 0x%08x\n",
  203. ice_readl(ice_mmio, ICE_REGS_PARAMETERS_5),
  204. ice_readl(ice_mmio, ICE_REGS_NON_SEC_IRQ_STTS));
  205. pr_err("ICE IRQ MASK: 0x%08x | ICE IRQ CLR: 0x%08x\n",
  206. ice_readl(ice_mmio, ICE_REGS_NON_SEC_IRQ_MASK),
  207. ice_readl(ice_mmio, ICE_REGS_NON_SEC_IRQ_CLR));
  208. pr_err("ICE INVALID CCFG ERR STTS: 0x%08x\n",
  209. ice_readl(ice_mmio, ICE_INVALID_CCFG_ERR_STTS));
  210. pr_err("ICE GENERAL ERR STTS: 0x%08x\n",
  211. ice_readl(ice_mmio, ICE_GENERAL_ERR_STTS));
  212. pr_err("ICE BIST Sts: 0x%08x | ICE Bypass Sts: 0x%08x\n",
  213. ice_readl(ice_mmio, ICE_REGS_BIST_STATUS),
  214. ice_readl(ice_mmio, ICE_REGS_BYPASS_STATUS));
  215. pr_err("ICE ADV CTRL: 0x%08x | ICE ENDIAN SWAP: 0x%08x\n",
  216. ice_readl(ice_mmio, ICE_REGS_ADVANCED_CONTROL),
  217. ice_readl(ice_mmio, ICE_REGS_ENDIAN_SWAP));
  218. pr_err("ICE_STM1_ERR_SYND1: 0x%08x | ICE_STM2_ERR_SYND1: 0x%08x\n",
  219. ice_readl(ice_mmio, ICE_REGS_STREAM1_ERROR_SYNDROME1),
  220. ice_readl(ice_mmio, ICE_REGS_STREAM2_ERROR_SYNDROME1));
  221. pr_err("ICE_STM1_ERR_SYND2: 0x%08x | ICE_STM2_ERR_SYND2: 0x%08x\n",
  222. ice_readl(ice_mmio, ICE_REGS_STREAM1_ERROR_SYNDROME2),
  223. ice_readl(ice_mmio, ICE_REGS_STREAM2_ERROR_SYNDROME2));
  224. pr_err("ICE_STM1_ERR_SYND3: 0x%08x | ICE_STM2_ERR_SYND3: 0x%08x\n",
  225. ice_readl(ice_mmio, ICE_REGS_STREAM1_ERROR_SYNDROME3),
  226. ice_readl(ice_mmio, ICE_REGS_STREAM2_ERROR_SYNDROME3));
  227. pr_err("ICE_STM1_COUNTER1: 0x%08x | ICE_STM1_COUNTER2: 0x%08x\n",
  228. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS1),
  229. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS2));
  230. pr_err("ICE_STM1_COUNTER3: 0x%08x | ICE_STM1_COUNTER4: 0x%08x\n",
  231. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS3),
  232. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS4));
  233. pr_err("ICE_STM2_COUNTER1: 0x%08x | ICE_STM2_COUNTER2: 0x%08x\n",
  234. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS1),
  235. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS2));
  236. pr_err("ICE_STM2_COUNTER3: 0x%08x | ICE_STM2_COUNTER4: 0x%08x\n",
  237. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS3),
  238. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS4));
  239. pr_err("ICE_STM1_CTR5_MSB: 0x%08x | ICE_STM1_CTR5_LSB: 0x%08x\n",
  240. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS5_MSB),
  241. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS5_LSB));
  242. pr_err("ICE_STM1_CTR6_MSB: 0x%08x | ICE_STM1_CTR6_LSB: 0x%08x\n",
  243. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS6_MSB),
  244. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS6_LSB));
  245. pr_err("ICE_STM1_CTR7_MSB: 0x%08x | ICE_STM1_CTR7_LSB: 0x%08x\n",
  246. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS7_MSB),
  247. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS7_LSB));
  248. pr_err("ICE_STM1_CTR8_MSB: 0x%08x | ICE_STM1_CTR8_LSB: 0x%08x\n",
  249. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS8_MSB),
  250. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS8_LSB));
  251. pr_err("ICE_STM1_CTR9_MSB: 0x%08x | ICE_STM1_CTR9_LSB: 0x%08x\n",
  252. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS9_MSB),
  253. ice_readl(ice_mmio, ICE_REGS_STREAM1_COUNTERS9_LSB));
  254. pr_err("ICE_STM2_CTR5_MSB: 0x%08x | ICE_STM2_CTR5_LSB: 0x%08x\n",
  255. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS5_MSB),
  256. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS5_LSB));
  257. pr_err("ICE_STM2_CTR6_MSB: 0x%08x | ICE_STM2_CTR6_LSB: 0x%08x\n",
  258. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS6_MSB),
  259. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS6_LSB));
  260. pr_err("ICE_STM2_CTR7_MSB: 0x%08x | ICE_STM2_CTR7_LSB: 0x%08x\n",
  261. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS7_MSB),
  262. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS7_LSB));
  263. pr_err("ICE_STM2_CTR8_MSB: 0x%08x | ICE_STM2_CTR8_LSB: 0x%08x\n",
  264. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS8_MSB),
  265. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS8_LSB));
  266. pr_err("ICE_STM2_CTR9_MSB: 0x%08x | ICE_STM2_CTR9_LSB: 0x%08x\n",
  267. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS9_MSB),
  268. ice_readl(ice_mmio, ICE_REGS_STREAM2_COUNTERS9_LSB));
  269. pr_err("ICE_STREAM1_HWKM_RD_ERR_STS: 0x%08x\n",
  270. ice_readl(ice_mmio, ICE_STREAM1_HWKM_RD_ERR_STTS));
  271. pr_err("ICE_STREAM2_HWKM_RD_ERR_STS: 0x%08x\n",
  272. ice_readl(ice_mmio, ICE_STREAM2_HWKM_RD_ERR_STTS));
  273. pr_err("ICE_CONFIG_HWKM_WR_ERR_STS: 0x%08x\n",
  274. ice_readl(ice_mmio, ICE_CONFIG_HWKM_WR_ERR_STTS));
  275. pr_err("ICE_AES_SHARE_CONTROL: 0x%08x\n",
  276. ice_readl(ice_mmio, ICE_AES_SHARE_CONTROL));
  277. pr_err("ICE_AES_CORE_STTS: 0x%08x\n",
  278. ice_readl(ice_mmio, ICE_AES_CORE_STTS));
  279. pr_err("ICE_AES_CORE_DISABLE: 0x%08x\n",
  280. ice_readl(ice_mmio, ICE_AES_CORE_DISABLE));
  281. ice_dump_config_regs(ice_mmio);
  282. ice_dump_test_bus(ice_mmio);
  283. return 0;
  284. }
  285. EXPORT_SYMBOL(crypto_qti_debug);
  286. int crypto_qti_keyslot_program(const struct ice_mmio_data *mmio_data,
  287. const struct blk_crypto_key *key,
  288. unsigned int slot,
  289. u8 data_unit_mask, int capid, int storage_type)
  290. {
  291. int err = 0;
  292. err = crypto_qti_program_key(mmio_data, key, slot,
  293. data_unit_mask, capid, storage_type);
  294. if (err) {
  295. pr_err("%s: program key failed with error %d\n", __func__, err);
  296. err = crypto_qti_invalidate_key(mmio_data, slot, storage_type);
  297. if (err) {
  298. pr_err("%s: invalidate key failed with error %d\n",
  299. __func__, err);
  300. return err;
  301. }
  302. }
  303. return err;
  304. }
  305. EXPORT_SYMBOL(crypto_qti_keyslot_program);
  306. int crypto_qti_keyslot_evict(const struct ice_mmio_data *mmio_data,
  307. unsigned int slot, int storage_type)
  308. {
  309. int err = 0;
  310. err = crypto_qti_invalidate_key(mmio_data, slot, storage_type);
  311. if (err) {
  312. pr_err("%s: invalidate key failed with error %d\n",
  313. __func__, err);
  314. }
  315. return err;
  316. }
  317. EXPORT_SYMBOL(crypto_qti_keyslot_evict);
  318. int crypto_qti_derive_raw_secret(const struct ice_mmio_data *mmio_data, const u8 *wrapped_key,
  319. unsigned int wrapped_key_size, u8 *secret,
  320. unsigned int secret_size)
  321. {
  322. int err = 0;
  323. if (wrapped_key_size <= RAW_SECRET_SIZE) {
  324. pr_err("%s: Invalid wrapped_key_size: %u\n",
  325. __func__, wrapped_key_size);
  326. err = -EINVAL;
  327. return err;
  328. }
  329. if (secret_size != RAW_SECRET_SIZE) {
  330. pr_err("%s: Invalid secret size: %u\n", __func__, secret_size);
  331. err = -EINVAL;
  332. return err;
  333. }
  334. if (wrapped_key_size > 64)
  335. err = crypto_qti_derive_raw_secret_platform(mmio_data, wrapped_key,
  336. wrapped_key_size, secret, secret_size);
  337. else
  338. memcpy(secret, wrapped_key, secret_size);
  339. return err;
  340. }
  341. EXPORT_SYMBOL(crypto_qti_derive_raw_secret);
  342. MODULE_LICENSE("GPL");
  343. MODULE_DESCRIPTION("Common crypto library for storage encryption");