ufshcd-crypto.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. #include <ufs/ufshcd.h>
  6. #include "ufshcd-crypto.h"
  7. /* Blk-crypto modes supported by UFS crypto */
  8. static const struct ufs_crypto_alg_entry {
  9. enum ufs_crypto_alg ufs_alg;
  10. enum ufs_crypto_key_size ufs_key_size;
  11. } ufs_crypto_algs[BLK_ENCRYPTION_MODE_MAX] = {
  12. [BLK_ENCRYPTION_MODE_AES_256_XTS] = {
  13. .ufs_alg = UFS_CRYPTO_ALG_AES_XTS,
  14. .ufs_key_size = UFS_CRYPTO_KEY_SIZE_256,
  15. },
  16. };
  17. static int ufshcd_program_key(struct ufs_hba *hba,
  18. const union ufs_crypto_cfg_entry *cfg, int slot)
  19. {
  20. int i;
  21. u32 slot_offset = hba->crypto_cfg_register + slot * sizeof(*cfg);
  22. int err = 0;
  23. ufshcd_hold(hba);
  24. if (hba->vops && hba->vops->program_key) {
  25. err = hba->vops->program_key(hba, cfg, slot);
  26. goto out;
  27. }
  28. /* Ensure that CFGE is cleared before programming the key */
  29. ufshcd_writel(hba, 0, slot_offset + 16 * sizeof(cfg->reg_val[0]));
  30. for (i = 0; i < 16; i++) {
  31. ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[i]),
  32. slot_offset + i * sizeof(cfg->reg_val[0]));
  33. }
  34. /* Write dword 17 */
  35. ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[17]),
  36. slot_offset + 17 * sizeof(cfg->reg_val[0]));
  37. /* Dword 16 must be written last */
  38. ufshcd_writel(hba, le32_to_cpu(cfg->reg_val[16]),
  39. slot_offset + 16 * sizeof(cfg->reg_val[0]));
  40. out:
  41. ufshcd_release(hba);
  42. return err;
  43. }
  44. static int ufshcd_crypto_keyslot_program(struct blk_crypto_profile *profile,
  45. const struct blk_crypto_key *key,
  46. unsigned int slot)
  47. {
  48. struct ufs_hba *hba =
  49. container_of(profile, struct ufs_hba, crypto_profile);
  50. const union ufs_crypto_cap_entry *ccap_array = hba->crypto_cap_array;
  51. const struct ufs_crypto_alg_entry *alg =
  52. &ufs_crypto_algs[key->crypto_cfg.crypto_mode];
  53. u8 data_unit_mask = key->crypto_cfg.data_unit_size / 512;
  54. int i;
  55. int cap_idx = -1;
  56. union ufs_crypto_cfg_entry cfg = {};
  57. int err;
  58. BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0);
  59. for (i = 0; i < hba->crypto_capabilities.num_crypto_cap; i++) {
  60. if (ccap_array[i].algorithm_id == alg->ufs_alg &&
  61. ccap_array[i].key_size == alg->ufs_key_size &&
  62. (ccap_array[i].sdus_mask & data_unit_mask)) {
  63. cap_idx = i;
  64. break;
  65. }
  66. }
  67. if (WARN_ON(cap_idx < 0))
  68. return -EOPNOTSUPP;
  69. cfg.data_unit_size = data_unit_mask;
  70. cfg.crypto_cap_idx = cap_idx;
  71. cfg.config_enable = UFS_CRYPTO_CONFIGURATION_ENABLE;
  72. if (ccap_array[cap_idx].algorithm_id == UFS_CRYPTO_ALG_AES_XTS) {
  73. /* In XTS mode, the blk_crypto_key's size is already doubled */
  74. memcpy(cfg.crypto_key, key->raw, key->size/2);
  75. memcpy(cfg.crypto_key + UFS_CRYPTO_KEY_MAX_SIZE/2,
  76. key->raw + key->size/2, key->size/2);
  77. } else {
  78. memcpy(cfg.crypto_key, key->raw, key->size);
  79. }
  80. err = ufshcd_program_key(hba, &cfg, slot);
  81. memzero_explicit(&cfg, sizeof(cfg));
  82. return err;
  83. }
  84. static int ufshcd_crypto_keyslot_evict(struct blk_crypto_profile *profile,
  85. const struct blk_crypto_key *key,
  86. unsigned int slot)
  87. {
  88. struct ufs_hba *hba =
  89. container_of(profile, struct ufs_hba, crypto_profile);
  90. /*
  91. * Clear the crypto cfg on the device. Clearing CFGE
  92. * might not be sufficient, so just clear the entire cfg.
  93. */
  94. union ufs_crypto_cfg_entry cfg = {};
  95. return ufshcd_program_key(hba, &cfg, slot);
  96. }
  97. /*
  98. * Reprogram the keyslots if needed, and return true if CRYPTO_GENERAL_ENABLE
  99. * should be used in the host controller initialization sequence.
  100. */
  101. bool ufshcd_crypto_enable(struct ufs_hba *hba)
  102. {
  103. if (!(hba->caps & UFSHCD_CAP_CRYPTO))
  104. return false;
  105. /* Reset might clear all keys, so reprogram all the keys. */
  106. blk_crypto_reprogram_all_keys(&hba->crypto_profile);
  107. if (hba->quirks & UFSHCD_QUIRK_BROKEN_CRYPTO_ENABLE)
  108. return false;
  109. return true;
  110. }
  111. static const struct blk_crypto_ll_ops ufshcd_crypto_ops = {
  112. .keyslot_program = ufshcd_crypto_keyslot_program,
  113. .keyslot_evict = ufshcd_crypto_keyslot_evict,
  114. };
  115. static enum blk_crypto_mode_num
  116. ufshcd_find_blk_crypto_mode(union ufs_crypto_cap_entry cap)
  117. {
  118. int i;
  119. for (i = 0; i < ARRAY_SIZE(ufs_crypto_algs); i++) {
  120. BUILD_BUG_ON(UFS_CRYPTO_KEY_SIZE_INVALID != 0);
  121. if (ufs_crypto_algs[i].ufs_alg == cap.algorithm_id &&
  122. ufs_crypto_algs[i].ufs_key_size == cap.key_size) {
  123. return i;
  124. }
  125. }
  126. return BLK_ENCRYPTION_MODE_INVALID;
  127. }
  128. /**
  129. * ufshcd_hba_init_crypto_capabilities - Read crypto capabilities, init crypto
  130. * fields in hba
  131. * @hba: Per adapter instance
  132. *
  133. * Return: 0 if crypto was initialized or is not supported, else a -errno value.
  134. */
  135. int ufshcd_hba_init_crypto_capabilities(struct ufs_hba *hba)
  136. {
  137. int cap_idx;
  138. int err = 0;
  139. enum blk_crypto_mode_num blk_mode_num;
  140. if (hba->quirks & UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE)
  141. return 0;
  142. /*
  143. * Don't use crypto if either the hardware doesn't advertise the
  144. * standard crypto capability bit *or* if the vendor specific driver
  145. * hasn't advertised that crypto is supported.
  146. */
  147. if (!(hba->capabilities & MASK_CRYPTO_SUPPORT) ||
  148. !(hba->caps & UFSHCD_CAP_CRYPTO))
  149. goto out;
  150. hba->crypto_capabilities.reg_val =
  151. cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP));
  152. hba->crypto_cfg_register =
  153. (u32)hba->crypto_capabilities.config_array_ptr * 0x100;
  154. hba->crypto_cap_array =
  155. devm_kcalloc(hba->dev, hba->crypto_capabilities.num_crypto_cap,
  156. sizeof(hba->crypto_cap_array[0]), GFP_KERNEL);
  157. if (!hba->crypto_cap_array) {
  158. err = -ENOMEM;
  159. goto out;
  160. }
  161. /* The actual number of configurations supported is (CFGC+1) */
  162. err = devm_blk_crypto_profile_init(
  163. hba->dev, &hba->crypto_profile,
  164. hba->crypto_capabilities.config_count + 1);
  165. if (err)
  166. goto out;
  167. hba->crypto_profile.ll_ops = ufshcd_crypto_ops;
  168. /* UFS only supports 8 bytes for any DUN */
  169. hba->crypto_profile.max_dun_bytes_supported = 8;
  170. hba->crypto_profile.dev = hba->dev;
  171. /*
  172. * Cache all the UFS crypto capabilities and advertise the supported
  173. * crypto modes and data unit sizes to the block layer.
  174. */
  175. for (cap_idx = 0; cap_idx < hba->crypto_capabilities.num_crypto_cap;
  176. cap_idx++) {
  177. hba->crypto_cap_array[cap_idx].reg_val =
  178. cpu_to_le32(ufshcd_readl(hba,
  179. REG_UFS_CRYPTOCAP +
  180. cap_idx * sizeof(__le32)));
  181. blk_mode_num = ufshcd_find_blk_crypto_mode(
  182. hba->crypto_cap_array[cap_idx]);
  183. if (blk_mode_num != BLK_ENCRYPTION_MODE_INVALID)
  184. hba->crypto_profile.modes_supported[blk_mode_num] |=
  185. hba->crypto_cap_array[cap_idx].sdus_mask * 512;
  186. }
  187. return 0;
  188. out:
  189. /* Indicate that init failed by clearing UFSHCD_CAP_CRYPTO */
  190. hba->caps &= ~UFSHCD_CAP_CRYPTO;
  191. return err;
  192. }
  193. /**
  194. * ufshcd_init_crypto - Initialize crypto hardware
  195. * @hba: Per adapter instance
  196. */
  197. void ufshcd_init_crypto(struct ufs_hba *hba)
  198. {
  199. int slot;
  200. if (!(hba->caps & UFSHCD_CAP_CRYPTO))
  201. return;
  202. /* Clear all keyslots. */
  203. for (slot = 0; slot < hba->crypto_profile.num_slots; slot++)
  204. hba->crypto_profile.ll_ops.keyslot_evict(&hba->crypto_profile,
  205. NULL, slot);
  206. }
  207. void ufshcd_crypto_register(struct ufs_hba *hba, struct request_queue *q)
  208. {
  209. if (hba->caps & UFSHCD_CAP_CRYPTO)
  210. blk_crypto_register(&hba->crypto_profile, q);
  211. }