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