ufshcd-crypto.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. #ifndef _UFSHCD_CRYPTO_H
  6. #define _UFSHCD_CRYPTO_H
  7. #include <scsi/scsi_cmnd.h>
  8. #include <ufs/ufshcd.h>
  9. #include "ufshcd-priv.h"
  10. #include <ufs/ufshci.h>
  11. #ifdef CONFIG_SCSI_UFS_CRYPTO
  12. static inline void ufshcd_prepare_lrbp_crypto(struct request *rq,
  13. struct ufshcd_lrb *lrbp)
  14. {
  15. if (!rq || !rq->crypt_keyslot) {
  16. lrbp->crypto_key_slot = -1;
  17. return;
  18. }
  19. lrbp->crypto_key_slot = blk_crypto_keyslot_index(rq->crypt_keyslot);
  20. lrbp->data_unit_num = rq->crypt_ctx->bc_dun[0];
  21. }
  22. static inline void
  23. ufshcd_prepare_req_desc_hdr_crypto(struct ufshcd_lrb *lrbp,
  24. struct request_desc_header *h)
  25. {
  26. if (lrbp->crypto_key_slot < 0)
  27. return;
  28. h->enable_crypto = 1;
  29. h->cci = lrbp->crypto_key_slot;
  30. h->dunl = cpu_to_le32(lower_32_bits(lrbp->data_unit_num));
  31. h->dunu = cpu_to_le32(upper_32_bits(lrbp->data_unit_num));
  32. }
  33. static inline int ufshcd_crypto_fill_prdt(struct ufs_hba *hba,
  34. struct ufshcd_lrb *lrbp)
  35. {
  36. struct scsi_cmnd *cmd = lrbp->cmd;
  37. const struct bio_crypt_ctx *crypt_ctx = scsi_cmd_to_rq(cmd)->crypt_ctx;
  38. if (crypt_ctx && hba->vops && hba->vops->fill_crypto_prdt)
  39. return hba->vops->fill_crypto_prdt(hba, crypt_ctx,
  40. lrbp->ucd_prdt_ptr,
  41. scsi_sg_count(cmd));
  42. return 0;
  43. }
  44. static inline void ufshcd_crypto_clear_prdt(struct ufs_hba *hba,
  45. struct ufshcd_lrb *lrbp)
  46. {
  47. if (!(hba->quirks & UFSHCD_QUIRK_KEYS_IN_PRDT))
  48. return;
  49. if (!(scsi_cmd_to_rq(lrbp->cmd)->crypt_ctx))
  50. return;
  51. /* Zeroize the PRDT because it can contain cryptographic keys. */
  52. memzero_explicit(lrbp->ucd_prdt_ptr,
  53. ufshcd_sg_entry_size(hba) * scsi_sg_count(lrbp->cmd));
  54. }
  55. bool ufshcd_crypto_enable(struct ufs_hba *hba);
  56. int ufshcd_hba_init_crypto_capabilities(struct ufs_hba *hba);
  57. void ufshcd_init_crypto(struct ufs_hba *hba);
  58. void ufshcd_crypto_register(struct ufs_hba *hba, struct request_queue *q);
  59. #else /* CONFIG_SCSI_UFS_CRYPTO */
  60. static inline void ufshcd_prepare_lrbp_crypto(struct request *rq,
  61. struct ufshcd_lrb *lrbp) { }
  62. static inline void
  63. ufshcd_prepare_req_desc_hdr_crypto(struct ufshcd_lrb *lrbp,
  64. struct request_desc_header *h) { }
  65. static inline int ufshcd_crypto_fill_prdt(struct ufs_hba *hba,
  66. struct ufshcd_lrb *lrbp)
  67. {
  68. return 0;
  69. }
  70. static inline void ufshcd_crypto_clear_prdt(struct ufs_hba *hba,
  71. struct ufshcd_lrb *lrbp) { }
  72. static inline bool ufshcd_crypto_enable(struct ufs_hba *hba)
  73. {
  74. return false;
  75. }
  76. static inline int ufshcd_hba_init_crypto_capabilities(struct ufs_hba *hba)
  77. {
  78. return 0;
  79. }
  80. static inline void ufshcd_init_crypto(struct ufs_hba *hba) { }
  81. static inline void ufshcd_crypto_register(struct ufs_hba *hba,
  82. struct request_queue *q) { }
  83. #endif /* CONFIG_SCSI_UFS_CRYPTO */
  84. #endif /* _UFSHCD_CRYPTO_H */