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

source code of linux/drivers/ufs/core/ufshcd-crypto.h