cqhci-crypto.c 7.3 KB

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