Merge tag 'pci-v5.19-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/helgaa...
[linux-block.git] / block / blk-crypto-internal.h
CommitLineData
a892c8d5
ST
1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright 2019 Google LLC
4 */
5
6#ifndef __LINUX_BLK_CRYPTO_INTERNAL_H
7#define __LINUX_BLK_CRYPTO_INTERNAL_H
8
9#include <linux/bio.h>
24b83deb 10#include <linux/blk-mq.h>
a892c8d5
ST
11
12/* Represents a crypto mode supported by blk-crypto */
13struct blk_crypto_mode {
20f01f16 14 const char *name; /* name of this mode, shown in sysfs */
488f6682 15 const char *cipher_str; /* crypto API name (for fallback case) */
a892c8d5
ST
16 unsigned int keysize; /* key size in bytes */
17 unsigned int ivsize; /* iv size in bytes */
18};
19
488f6682
ST
20extern const struct blk_crypto_mode blk_crypto_modes[];
21
a892c8d5
ST
22#ifdef CONFIG_BLK_INLINE_ENCRYPTION
23
20f01f16
EB
24int blk_crypto_sysfs_register(struct request_queue *q);
25
26void blk_crypto_sysfs_unregister(struct request_queue *q);
27
a892c8d5
ST
28void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
29 unsigned int inc);
30
31bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio);
32
33bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
34 struct bio_crypt_ctx *bc2);
35
36static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
37 struct bio *bio)
38{
39 return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
40 bio->bi_crypt_context);
41}
42
43static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
44 struct bio *bio)
45{
46 return bio_crypt_ctx_mergeable(bio->bi_crypt_context,
47 bio->bi_iter.bi_size, req->crypt_ctx);
48}
49
50static inline bool bio_crypt_ctx_merge_rq(struct request *req,
51 struct request *next)
52{
53 return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
54 next->crypt_ctx);
55}
56
57static inline void blk_crypto_rq_set_defaults(struct request *rq)
58{
59 rq->crypt_ctx = NULL;
60 rq->crypt_keyslot = NULL;
61}
62
63static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
64{
65 return rq->crypt_ctx;
66}
67
68#else /* CONFIG_BLK_INLINE_ENCRYPTION */
69
20f01f16
EB
70static inline int blk_crypto_sysfs_register(struct request_queue *q)
71{
72 return 0;
73}
74
75static inline void blk_crypto_sysfs_unregister(struct request_queue *q) { }
76
a892c8d5
ST
77static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
78 struct bio *bio)
79{
80 return true;
81}
82
83static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
84 struct bio *bio)
85{
86 return true;
87}
88
89static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
90 struct bio *bio)
91{
92 return true;
93}
94
95static inline bool bio_crypt_ctx_merge_rq(struct request *req,
96 struct request *next)
97{
98 return true;
99}
100
101static inline void blk_crypto_rq_set_defaults(struct request *rq) { }
102
103static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
104{
105 return false;
106}
107
108#endif /* CONFIG_BLK_INLINE_ENCRYPTION */
109
110void __bio_crypt_advance(struct bio *bio, unsigned int bytes);
111static inline void bio_crypt_advance(struct bio *bio, unsigned int bytes)
112{
113 if (bio_has_crypt_ctx(bio))
114 __bio_crypt_advance(bio, bytes);
115}
116
117void __bio_crypt_free_ctx(struct bio *bio);
118static inline void bio_crypt_free_ctx(struct bio *bio)
119{
120 if (bio_has_crypt_ctx(bio))
121 __bio_crypt_free_ctx(bio);
122}
123
124static inline void bio_crypt_do_front_merge(struct request *rq,
125 struct bio *bio)
126{
127#ifdef CONFIG_BLK_INLINE_ENCRYPTION
128 if (bio_has_crypt_ctx(bio))
129 memcpy(rq->crypt_ctx->bc_dun, bio->bi_crypt_context->bc_dun,
130 sizeof(rq->crypt_ctx->bc_dun));
131#endif
132}
133
134bool __blk_crypto_bio_prep(struct bio **bio_ptr);
135static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)
136{
137 if (bio_has_crypt_ctx(*bio_ptr))
138 return __blk_crypto_bio_prep(bio_ptr);
139 return true;
140}
141
142blk_status_t __blk_crypto_init_request(struct request *rq);
143static inline blk_status_t blk_crypto_init_request(struct request *rq)
144{
145 if (blk_crypto_rq_is_encrypted(rq))
146 return __blk_crypto_init_request(rq);
147 return BLK_STS_OK;
148}
149
150void __blk_crypto_free_request(struct request *rq);
151static inline void blk_crypto_free_request(struct request *rq)
152{
153 if (blk_crypto_rq_is_encrypted(rq))
154 __blk_crypto_free_request(rq);
155}
156
93f221ae
EB
157int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
158 gfp_t gfp_mask);
159/**
160 * blk_crypto_rq_bio_prep - Prepare a request's crypt_ctx when its first bio
161 * is inserted
162 * @rq: The request to prepare
163 * @bio: The first bio being inserted into the request
164 * @gfp_mask: Memory allocation flags
165 *
166 * Return: 0 on success, -ENOMEM if out of memory. -ENOMEM is only possible if
167 * @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
168 */
169static inline int blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
170 gfp_t gfp_mask)
a892c8d5
ST
171{
172 if (bio_has_crypt_ctx(bio))
93f221ae
EB
173 return __blk_crypto_rq_bio_prep(rq, bio, gfp_mask);
174 return 0;
a892c8d5
ST
175}
176
177/**
178 * blk_crypto_insert_cloned_request - Prepare a cloned request to be inserted
179 * into a request queue.
180 * @rq: the request being queued
181 *
182 * Return: BLK_STS_OK on success, nonzero on error.
183 */
184static inline blk_status_t blk_crypto_insert_cloned_request(struct request *rq)
185{
186
187 if (blk_crypto_rq_is_encrypted(rq))
188 return blk_crypto_init_request(rq);
189 return BLK_STS_OK;
190}
191
488f6682
ST
192#ifdef CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK
193
194int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num);
195
196bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr);
197
198int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key);
199
200#else /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
201
202static inline int
203blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
204{
205 pr_warn_once("crypto API fallback is disabled\n");
206 return -ENOPKG;
207}
208
209static inline bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
210{
211 pr_warn_once("crypto API fallback disabled; failing request.\n");
212 (*bio_ptr)->bi_status = BLK_STS_NOTSUPP;
213 return false;
214}
215
216static inline int
217blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
218{
219 return 0;
220}
221
222#endif /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
223
a892c8d5 224#endif /* __LINUX_BLK_CRYPTO_INTERNAL_H */