cqhci-crypto.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * CQHCI crypto engine (inline encryption) support
  4. *
  5. * Copyright 2020 Google LLC
  6. */
  7. #include <linux/blk-crypto.h>
  8. #include <linux/blk-crypto-profile.h>
  9. #include <linux/mmc/host.h>
  10. #include "cqhci-crypto.h"
  11. /* Map from blk-crypto modes to CQHCI crypto algorithm IDs and key sizes */
  12. static const struct cqhci_crypto_alg_entry {
  13. enum cqhci_crypto_alg alg;
  14. enum cqhci_crypto_key_size key_size;
  15. } cqhci_crypto_algs[BLK_ENCRYPTION_MODE_MAX] = {
  16. [BLK_ENCRYPTION_MODE_AES_256_XTS] = {
  17. .alg = CQHCI_CRYPTO_ALG_AES_XTS,
  18. .key_size = CQHCI_CRYPTO_KEY_SIZE_256,
  19. },
  20. };
  21. static inline struct cqhci_host *
  22. cqhci_host_from_crypto_profile(struct blk_crypto_profile *profile)
  23. {
  24. return mmc_from_crypto_profile(profile)->cqe_private;
  25. }
  26. static void cqhci_crypto_program_key(struct cqhci_host *cq_host,
  27. const union cqhci_crypto_cfg_entry *cfg,
  28. int slot)
  29. {
  30. u32 slot_offset = cq_host->crypto_cfg_register + slot * sizeof(*cfg);
  31. int i;
  32. /* Clear CFGE */
  33. cqhci_writel(cq_host, 0, slot_offset + 16 * sizeof(cfg->reg_val[0]));
  34. /* Write the key */
  35. for (i = 0; i < 16; i++) {
  36. cqhci_writel(cq_host, le32_to_cpu(cfg->reg_val[i]),
  37. slot_offset + i * sizeof(cfg->reg_val[0]));
  38. }
  39. /* Write dword 17 */
  40. cqhci_writel(cq_host, le32_to_cpu(cfg->reg_val[17]),
  41. slot_offset + 17 * sizeof(cfg->reg_val[0]));
  42. /* Write dword 16, which includes the new value of CFGE */
  43. cqhci_writel(cq_host, le32_to_cpu(cfg->reg_val[16]),
  44. slot_offset + 16 * sizeof(cfg->reg_val[0]));
  45. }
  46. static int cqhci_crypto_keyslot_program(struct blk_crypto_profile *profile,
  47. const struct blk_crypto_key *key,
  48. unsigned int slot)
  49. {
  50. struct cqhci_host *cq_host = cqhci_host_from_crypto_profile(profile);
  51. const union cqhci_crypto_cap_entry *ccap_array =
  52. cq_host->crypto_cap_array;
  53. const struct cqhci_crypto_alg_entry *alg =
  54. &cqhci_crypto_algs[key->crypto_cfg.crypto_mode];
  55. u8 data_unit_mask = key->crypto_cfg.data_unit_size / 512;
  56. int i;
  57. int cap_idx = -1;
  58. union cqhci_crypto_cfg_entry cfg = {};
  59. BUILD_BUG_ON(CQHCI_CRYPTO_KEY_SIZE_INVALID != 0);
  60. for (i = 0; i < cq_host->crypto_capabilities.num_crypto_cap; i++) {
  61. if (ccap_array[i].algorithm_id == alg->alg &&
  62. ccap_array[i].key_size == alg->key_size &&
  63. (ccap_array[i].sdus_mask & data_unit_mask)) {
  64. cap_idx = i;
  65. break;
  66. }
  67. }
  68. if (WARN_ON(cap_idx < 0))
  69. return -EOPNOTSUPP;
  70. cfg.data_unit_size = data_unit_mask;
  71. cfg.crypto_cap_idx = cap_idx;
  72. cfg.config_enable = CQHCI_CRYPTO_CONFIGURATION_ENABLE;
  73. if (ccap_array[cap_idx].algorithm_id == CQHCI_CRYPTO_ALG_AES_XTS) {
  74. /* In XTS mode, the blk_crypto_key's size is already doubled */
  75. memcpy(cfg.crypto_key, key->bytes, key->size/2);
  76. memcpy(cfg.crypto_key + CQHCI_CRYPTO_KEY_MAX_SIZE/2,
  77. key->bytes + key->size/2, key->size/2);
  78. } else {
  79. memcpy(cfg.crypto_key, key->bytes, key->size);
  80. }
  81. cqhci_crypto_program_key(cq_host, &cfg, slot);
  82. memzero_explicit(&cfg, sizeof(cfg));
  83. return 0;
  84. }
  85. static int cqhci_crypto_clear_keyslot(struct cqhci_host *cq_host, int slot)
  86. {
  87. /*
  88. * Clear the crypto cfg on the device. Clearing CFGE
  89. * might not be sufficient, so just clear the entire cfg.
  90. */
  91. union cqhci_crypto_cfg_entry cfg = {};
  92. cqhci_crypto_program_key(cq_host, &cfg, slot);
  93. return 0;
  94. }
  95. static int cqhci_crypto_keyslot_evict(struct blk_crypto_profile *profile,
  96. const struct blk_crypto_key *key,
  97. unsigned int slot)
  98. {
  99. struct cqhci_host *cq_host = cqhci_host_from_crypto_profile(profile);
  100. return cqhci_crypto_clear_keyslot(cq_host, slot);
  101. }
  102. /*
  103. * The keyslot management operations for CQHCI crypto.
  104. *
  105. * Note that the block layer ensures that these are never called while the host
  106. * controller is runtime-suspended. However, the CQE won't necessarily be
  107. * "enabled" when these are called, i.e. CQHCI_ENABLE might not be set in the
  108. * CQHCI_CFG register. But the hardware allows that.
  109. */
  110. static const struct blk_crypto_ll_ops cqhci_crypto_ops = {
  111. .keyslot_program = cqhci_crypto_keyslot_program,
  112. .keyslot_evict = cqhci_crypto_keyslot_evict,
  113. };
  114. static enum blk_crypto_mode_num
  115. cqhci_find_blk_crypto_mode(union cqhci_crypto_cap_entry cap)
  116. {
  117. int i;
  118. for (i = 0; i < ARRAY_SIZE(cqhci_crypto_algs); i++) {
  119. BUILD_BUG_ON(CQHCI_CRYPTO_KEY_SIZE_INVALID != 0);
  120. if (cqhci_crypto_algs[i].alg == cap.algorithm_id &&
  121. cqhci_crypto_algs[i].key_size == cap.key_size)
  122. return i;
  123. }
  124. return BLK_ENCRYPTION_MODE_INVALID;
  125. }
  126. /**
  127. * cqhci_crypto_init - initialize CQHCI crypto support
  128. * @cq_host: a cqhci host
  129. *
  130. * If the driver previously set MMC_CAP2_CRYPTO and the CQE declares
  131. * CQHCI_CAP_CS, initialize the crypto support. This involves reading the
  132. * crypto capability registers, initializing the blk_crypto_profile, clearing
  133. * all keyslots, and enabling 128-bit task descriptors.
  134. *
  135. * Return: 0 if crypto was initialized or isn't supported; whether
  136. * MMC_CAP2_CRYPTO remains set indicates which one of those cases it is.
  137. * Also can return a negative errno value on unexpected error.
  138. */
  139. int cqhci_crypto_init(struct cqhci_host *cq_host)
  140. {
  141. struct mmc_host *mmc = cq_host->mmc;
  142. struct device *dev = mmc_dev(mmc);
  143. struct blk_crypto_profile *profile = &mmc->crypto_profile;
  144. unsigned int cap_idx;
  145. enum blk_crypto_mode_num blk_mode_num;
  146. unsigned int slot;
  147. int err = 0;
  148. if (!(mmc->caps2 & MMC_CAP2_CRYPTO) ||
  149. !(cqhci_readl(cq_host, CQHCI_CAP) & CQHCI_CAP_CS))
  150. goto out;
  151. if (cq_host->ops->uses_custom_crypto_profile)
  152. goto profile_initialized;
  153. cq_host->crypto_capabilities.reg_val =
  154. cpu_to_le32(cqhci_readl(cq_host, CQHCI_CCAP));
  155. cq_host->crypto_cfg_register =
  156. (u32)cq_host->crypto_capabilities.config_array_ptr * 0x100;
  157. cq_host->crypto_cap_array =
  158. devm_kcalloc(dev, cq_host->crypto_capabilities.num_crypto_cap,
  159. sizeof(cq_host->crypto_cap_array[0]), GFP_KERNEL);
  160. if (!cq_host->crypto_cap_array) {
  161. err = -ENOMEM;
  162. goto out;
  163. }
  164. /*
  165. * CCAP.CFGC is off by one, so the actual number of crypto
  166. * configurations (a.k.a. keyslots) is CCAP.CFGC + 1.
  167. */
  168. err = devm_blk_crypto_profile_init(
  169. dev, profile, cq_host->crypto_capabilities.config_count + 1);
  170. if (err)
  171. goto out;
  172. profile->ll_ops = cqhci_crypto_ops;
  173. profile->dev = dev;
  174. /* Unfortunately, CQHCI crypto only supports 32 DUN bits. */
  175. profile->max_dun_bytes_supported = 4;
  176. profile->key_types_supported = BLK_CRYPTO_KEY_TYPE_RAW;
  177. /*
  178. * Cache all the crypto capabilities and advertise the supported crypto
  179. * modes and data unit sizes to the block layer.
  180. */
  181. for (cap_idx = 0; cap_idx < cq_host->crypto_capabilities.num_crypto_cap;
  182. cap_idx++) {
  183. cq_host->crypto_cap_array[cap_idx].reg_val =
  184. cpu_to_le32(cqhci_readl(cq_host,
  185. CQHCI_CRYPTOCAP +
  186. cap_idx * sizeof(__le32)));
  187. blk_mode_num = cqhci_find_blk_crypto_mode(
  188. cq_host->crypto_cap_array[cap_idx]);
  189. if (blk_mode_num == BLK_ENCRYPTION_MODE_INVALID)
  190. continue;
  191. profile->modes_supported[blk_mode_num] |=
  192. cq_host->crypto_cap_array[cap_idx].sdus_mask * 512;
  193. }
  194. profile_initialized:
  195. /* Clear all the keyslots so that we start in a known state. */
  196. for (slot = 0; slot < profile->num_slots; slot++)
  197. profile->ll_ops.keyslot_evict(profile, NULL, slot);
  198. /* CQHCI crypto requires the use of 128-bit task descriptors. */
  199. cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
  200. return 0;
  201. out:
  202. mmc->caps2 &= ~MMC_CAP2_CRYPTO;
  203. return err;
  204. }