Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 <ufs/ufshci.h>
12
13#ifdef CONFIG_SCSI_UFS_CRYPTO
14
15static inline void ufshcd_prepare_lrbp_crypto(struct request *rq,
16 struct ufshcd_lrb *lrbp)
17{
18 if (!rq || !rq->crypt_keyslot) {
19 lrbp->crypto_key_slot = -1;
20 return;
21 }
22
23 lrbp->crypto_key_slot = blk_crypto_keyslot_index(rq->crypt_keyslot);
24 lrbp->data_unit_num = rq->crypt_ctx->bc_dun[0];
25}
26
27static inline void
28ufshcd_prepare_req_desc_hdr_crypto(struct ufshcd_lrb *lrbp,
29 struct request_desc_header *h)
30{
31 if (lrbp->crypto_key_slot < 0)
32 return;
33 h->enable_crypto = 1;
34 h->cci = lrbp->crypto_key_slot;
35 h->dunl = cpu_to_le32(lower_32_bits(lrbp->data_unit_num));
36 h->dunu = cpu_to_le32(upper_32_bits(lrbp->data_unit_num));
37}
38
39static inline int ufshcd_crypto_fill_prdt(struct ufs_hba *hba,
40 struct scsi_cmnd *cmd)
41{
42 const struct bio_crypt_ctx *crypt_ctx = scsi_cmd_to_rq(cmd)->crypt_ctx;
43 struct ufshcd_lrb *lrbp = scsi_cmd_priv(cmd);
44
45 if (crypt_ctx && hba->vops && hba->vops->fill_crypto_prdt)
46 return hba->vops->fill_crypto_prdt(hba, crypt_ctx,
47 lrbp->ucd_prdt_ptr,
48 scsi_sg_count(cmd));
49 return 0;
50}
51
52static inline void ufshcd_crypto_clear_prdt(struct ufs_hba *hba,
53 struct scsi_cmnd *cmd)
54{
55 struct ufshcd_lrb *lrbp = scsi_cmd_priv(cmd);
56
57 if (!(hba->quirks & UFSHCD_QUIRK_KEYS_IN_PRDT))
58 return;
59
60 if (!(scsi_cmd_to_rq(cmd)->crypt_ctx))
61 return;
62
63 /* Zeroize the PRDT because it can contain cryptographic keys. */
64 memzero_explicit(lrbp->ucd_prdt_ptr,
65 ufshcd_sg_entry_size(hba) * scsi_sg_count(cmd));
66}
67
68bool ufshcd_crypto_enable(struct ufs_hba *hba);
69
70int ufshcd_hba_init_crypto_capabilities(struct ufs_hba *hba);
71
72void ufshcd_init_crypto(struct ufs_hba *hba);
73
74void ufshcd_crypto_register(struct ufs_hba *hba, struct request_queue *q);
75
76#else /* CONFIG_SCSI_UFS_CRYPTO */
77
78static inline void ufshcd_prepare_lrbp_crypto(struct request *rq,
79 struct ufshcd_lrb *lrbp) { }
80
81static inline void
82ufshcd_prepare_req_desc_hdr_crypto(struct ufshcd_lrb *lrbp,
83 struct request_desc_header *h) { }
84
85static inline int ufshcd_crypto_fill_prdt(struct ufs_hba *hba,
86 struct scsi_cmnd *cmd)
87{
88 return 0;
89}
90
91static inline void ufshcd_crypto_clear_prdt(struct ufs_hba *hba,
92 struct scsi_cmnd *cmd)
93{
94}
95
96static inline bool ufshcd_crypto_enable(struct ufs_hba *hba)
97{
98 return false;
99}
100
101static inline int ufshcd_hba_init_crypto_capabilities(struct ufs_hba *hba)
102{
103 return 0;
104}
105
106static inline void ufshcd_init_crypto(struct ufs_hba *hba) { }
107
108static inline void ufshcd_crypto_register(struct ufs_hba *hba,
109 struct request_queue *q) { }
110
111#endif /* CONFIG_SCSI_UFS_CRYPTO */
112
113#endif /* _UFSHCD_CRYPTO_H */