ufshcd-crypto-qti.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * UFS Crypto ops QTI implementation.
  4. *
  5. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  6. */
  7. #include <crypto/algapi.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/crypto-qti-common.h>
  10. #include <ufs/ufshcd-crypto-qti.h>
  11. #include "ufs-qcom.h"
  12. #define MINIMUM_DUN_SIZE 512
  13. #define MAXIMUM_DUN_SIZE 65536
  14. /* Blk-crypto modes supported by UFS crypto */
  15. static const struct ufs_crypto_alg_entry {
  16. enum ufs_crypto_alg ufs_alg;
  17. enum ufs_crypto_key_size ufs_key_size;
  18. } ufs_crypto_algs[BLK_ENCRYPTION_MODE_MAX] = {
  19. [BLK_ENCRYPTION_MODE_AES_256_XTS] = {
  20. .ufs_alg = UFS_CRYPTO_ALG_AES_XTS,
  21. .ufs_key_size = UFS_CRYPTO_KEY_SIZE_256,
  22. },
  23. };
  24. static void get_mmio_data(struct ice_mmio_data *data,
  25. struct ufs_qcom_host *host)
  26. {
  27. data->ice_base_mmio = host->ice_mmio;
  28. #if (IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER) || IS_ENABLED(CONFIG_QTI_HW_KEY_MANAGER_V1))
  29. data->ice_hwkm_mmio = host->ice_hwkm_mmio;
  30. #endif
  31. }
  32. static int ufshcd_crypto_qti_keyslot_program(
  33. struct blk_crypto_profile *profile,
  34. const struct blk_crypto_key *key,
  35. unsigned int slot)
  36. {
  37. struct ufs_hba *hba =
  38. container_of(profile, struct ufs_hba, crypto_profile);
  39. int err = 0;
  40. u8 data_unit_mask = -1;
  41. int cap_idx = -1;
  42. const union ufs_crypto_cap_entry *ccap_array = hba->crypto_cap_array;
  43. const struct ufs_crypto_alg_entry *alg;
  44. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  45. int i = 0;
  46. struct ice_mmio_data mmio_data;
  47. if (!key) {
  48. pr_err("Invalid/no key present\n");
  49. return -EINVAL;
  50. }
  51. data_unit_mask = key->crypto_cfg.data_unit_size / MINIMUM_DUN_SIZE;
  52. alg = &ufs_crypto_algs[key->crypto_cfg.crypto_mode];
  53. BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0);
  54. for (i = 0; i < hba->crypto_capabilities.num_crypto_cap; i++) {
  55. if (ccap_array[i].algorithm_id == alg->ufs_alg &&
  56. ccap_array[i].key_size == alg->ufs_key_size &&
  57. (ccap_array[i].sdus_mask & data_unit_mask)) {
  58. cap_idx = i;
  59. break;
  60. }
  61. }
  62. if (WARN_ON(cap_idx < 0))
  63. return -EOPNOTSUPP;
  64. if (host->reset_in_progress) {
  65. pr_err("UFS host reset in progress, state = 0x%x\n",
  66. hba->ufshcd_state);
  67. return -EINVAL;
  68. }
  69. err = ufshcd_hold(hba, false);
  70. if (err) {
  71. pr_err("%s: failed to enable clocks, err %d\n", __func__, err);
  72. goto out;
  73. }
  74. get_mmio_data(&mmio_data, host);
  75. err = crypto_qti_keyslot_program(&mmio_data, key, slot,
  76. data_unit_mask, cap_idx, UFS_CE);
  77. if (err)
  78. pr_err("%s: failed with error %d\n", __func__, err);
  79. ufshcd_release(hba);
  80. out:
  81. return err;
  82. }
  83. static int ufshcd_crypto_qti_keyslot_evict(
  84. struct blk_crypto_profile *profile,
  85. const struct blk_crypto_key *key,
  86. unsigned int slot)
  87. {
  88. int err = 0;
  89. struct ufs_hba *hba =
  90. container_of(profile, struct ufs_hba, crypto_profile);
  91. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  92. struct ice_mmio_data mmio_data;
  93. if (host->reset_in_progress) {
  94. pr_err("UFS host reset in progress, state = 0x%x\n",
  95. hba->ufshcd_state);
  96. return -EINVAL;
  97. }
  98. err = ufshcd_hold(hba, false);
  99. if (err) {
  100. pr_err("%s: failed to enable clocks, err %d\n", __func__, err);
  101. return err;
  102. }
  103. get_mmio_data(&mmio_data, host);
  104. err = crypto_qti_keyslot_evict(&mmio_data, slot, UFS_CE);
  105. if (err)
  106. pr_err("%s: failed with error %d\n", __func__, err);
  107. ufshcd_release(hba);
  108. return err;
  109. }
  110. static int ufshcd_crypto_qti_derive_raw_secret(
  111. struct blk_crypto_profile *profile,
  112. const u8 *eph_key, size_t eph_key_size,
  113. u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])
  114. {
  115. int err = 0;
  116. struct ufs_hba *hba =
  117. container_of(profile, struct ufs_hba, crypto_profile);
  118. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  119. struct ice_mmio_data mmio_data;
  120. if (host->reset_in_progress) {
  121. pr_err("UFS host reset in progress, state = 0x%x\n",
  122. hba->ufshcd_state);
  123. return -EINVAL;
  124. }
  125. err = ufshcd_hold(hba, false);
  126. if (err) {
  127. pr_err("%s: failed to enable clocks, err %d\n", __func__, err);
  128. return err;
  129. }
  130. get_mmio_data(&mmio_data, host);
  131. err = crypto_qti_derive_raw_secret(&mmio_data, eph_key, eph_key_size,
  132. sw_secret, BLK_CRYPTO_SW_SECRET_SIZE);
  133. if (err)
  134. pr_err("%s: failed with error %d\n", __func__, err);
  135. ufshcd_release(hba);
  136. return err;
  137. }
  138. static const struct blk_crypto_ll_ops ufshcd_qti_crypto_ops = {
  139. .keyslot_program = ufshcd_crypto_qti_keyslot_program,
  140. .keyslot_evict = ufshcd_crypto_qti_keyslot_evict,
  141. .derive_sw_secret = ufshcd_crypto_qti_derive_raw_secret,
  142. };
  143. static enum blk_crypto_mode_num
  144. ufshcd_find_blk_crypto_mode(union ufs_crypto_cap_entry cap)
  145. {
  146. int i;
  147. for (i = 0; i < ARRAY_SIZE(ufs_crypto_algs); i++) {
  148. BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0);
  149. if (ufs_crypto_algs[i].ufs_alg == cap.algorithm_id &&
  150. ufs_crypto_algs[i].ufs_key_size == cap.key_size) {
  151. return i;
  152. }
  153. }
  154. return BLK_ENCRYPTION_MODE_INVALID;
  155. }
  156. /**
  157. * ufshcd_hba_init_crypto_capabilities - Read crypto capabilities, init crypto
  158. * fields in hba
  159. * @hba: Per adapter instance
  160. *
  161. * Return: 0 if crypto was initialized or is not supported, else a -errno value.
  162. */
  163. int ufshcd_qti_hba_init_crypto_capabilities(struct ufs_hba *hba)
  164. {
  165. int cap_idx;
  166. int err = 0;
  167. enum blk_crypto_mode_num blk_mode_num;
  168. /*
  169. * Don't use crypto if either the hardware doesn't advertise the
  170. * standard crypto capability bit *or* if the vendor specific driver
  171. * hasn't advertised that crypto is supported.
  172. */
  173. if (!(ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES) &
  174. MASK_CRYPTO_SUPPORT))
  175. goto out;
  176. if (!(hba->caps & UFSHCD_CAP_CRYPTO))
  177. goto out;
  178. hba->crypto_capabilities.reg_val =
  179. cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP));
  180. hba->crypto_cfg_register =
  181. (u32)hba->crypto_capabilities.config_array_ptr * 0x100;
  182. hba->crypto_cap_array =
  183. devm_kcalloc(hba->dev, hba->crypto_capabilities.num_crypto_cap,
  184. sizeof(hba->crypto_cap_array[0]), GFP_KERNEL);
  185. if (!hba->crypto_cap_array) {
  186. err = -ENOMEM;
  187. goto out;
  188. }
  189. /* The actual number of configurations supported is (CFGC+1) */
  190. err = devm_blk_crypto_profile_init(hba->dev, &hba->crypto_profile,
  191. hba->crypto_capabilities.config_count + 1);
  192. if (err)
  193. goto out;
  194. hba->crypto_profile.ll_ops = ufshcd_qti_crypto_ops;
  195. /* UFS only supports 8 bytes for any DUN */
  196. hba->crypto_profile.max_dun_bytes_supported = 8;
  197. hba->crypto_profile.key_types_supported =
  198. BLK_CRYPTO_KEY_TYPE_HW_WRAPPED;
  199. hba->crypto_profile.dev = hba->dev;
  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. return 0;
  217. out:
  218. /* Indicate that init failed by clearing UFSHCD_CAP_CRYPTO */
  219. hba->caps &= ~UFSHCD_CAP_CRYPTO;
  220. return err;
  221. }
  222. EXPORT_SYMBOL(ufshcd_qti_hba_init_crypto_capabilities);
  223. MODULE_LICENSE("GPL");
  224. MODULE_DESCRIPTION("UFS Crypto ops QTI implementation");