blk-mq: Fix stall due to recursive flush plug
[platform/kernel/linux-starfive.git] / block / blk-crypto-internal.h
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>
10 #include <linux/blk-mq.h>
11
12 /* Represents a crypto mode supported by blk-crypto  */
13 struct blk_crypto_mode {
14         const char *name; /* name of this mode, shown in sysfs */
15         const char *cipher_str; /* crypto API name (for fallback case) */
16         unsigned int keysize; /* key size in bytes */
17         unsigned int ivsize; /* iv size in bytes */
18 };
19
20 extern const struct blk_crypto_mode blk_crypto_modes[];
21
22 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
23
24 int blk_crypto_sysfs_register(struct request_queue *q);
25
26 void blk_crypto_sysfs_unregister(struct request_queue *q);
27
28 void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
29                              unsigned int inc);
30
31 bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio);
32
33 bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
34                              struct bio_crypt_ctx *bc2);
35
36 static 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
43 static 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
50 static 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
57 static inline void blk_crypto_rq_set_defaults(struct request *rq)
58 {
59         rq->crypt_ctx = NULL;
60         rq->crypt_keyslot = NULL;
61 }
62
63 static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
64 {
65         return rq->crypt_ctx;
66 }
67
68 static inline bool blk_crypto_rq_has_keyslot(struct request *rq)
69 {
70         return rq->crypt_keyslot;
71 }
72
73 blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
74                                     const struct blk_crypto_key *key,
75                                     struct blk_crypto_keyslot **slot_ptr);
76
77 void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot);
78
79 int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
80                            const struct blk_crypto_key *key);
81
82 bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
83                                 const struct blk_crypto_config *cfg);
84
85 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
86
87 static inline int blk_crypto_sysfs_register(struct request_queue *q)
88 {
89         return 0;
90 }
91
92 static inline void blk_crypto_sysfs_unregister(struct request_queue *q) { }
93
94 static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
95                                                struct bio *bio)
96 {
97         return true;
98 }
99
100 static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
101                                                  struct bio *bio)
102 {
103         return true;
104 }
105
106 static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
107                                                 struct bio *bio)
108 {
109         return true;
110 }
111
112 static inline bool bio_crypt_ctx_merge_rq(struct request *req,
113                                           struct request *next)
114 {
115         return true;
116 }
117
118 static inline void blk_crypto_rq_set_defaults(struct request *rq) { }
119
120 static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
121 {
122         return false;
123 }
124
125 static inline bool blk_crypto_rq_has_keyslot(struct request *rq)
126 {
127         return false;
128 }
129
130 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
131
132 void __bio_crypt_advance(struct bio *bio, unsigned int bytes);
133 static inline void bio_crypt_advance(struct bio *bio, unsigned int bytes)
134 {
135         if (bio_has_crypt_ctx(bio))
136                 __bio_crypt_advance(bio, bytes);
137 }
138
139 void __bio_crypt_free_ctx(struct bio *bio);
140 static inline void bio_crypt_free_ctx(struct bio *bio)
141 {
142         if (bio_has_crypt_ctx(bio))
143                 __bio_crypt_free_ctx(bio);
144 }
145
146 static inline void bio_crypt_do_front_merge(struct request *rq,
147                                             struct bio *bio)
148 {
149 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
150         if (bio_has_crypt_ctx(bio))
151                 memcpy(rq->crypt_ctx->bc_dun, bio->bi_crypt_context->bc_dun,
152                        sizeof(rq->crypt_ctx->bc_dun));
153 #endif
154 }
155
156 bool __blk_crypto_bio_prep(struct bio **bio_ptr);
157 static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)
158 {
159         if (bio_has_crypt_ctx(*bio_ptr))
160                 return __blk_crypto_bio_prep(bio_ptr);
161         return true;
162 }
163
164 blk_status_t __blk_crypto_rq_get_keyslot(struct request *rq);
165 static inline blk_status_t blk_crypto_rq_get_keyslot(struct request *rq)
166 {
167         if (blk_crypto_rq_is_encrypted(rq))
168                 return __blk_crypto_rq_get_keyslot(rq);
169         return BLK_STS_OK;
170 }
171
172 void __blk_crypto_rq_put_keyslot(struct request *rq);
173 static inline void blk_crypto_rq_put_keyslot(struct request *rq)
174 {
175         if (blk_crypto_rq_has_keyslot(rq))
176                 __blk_crypto_rq_put_keyslot(rq);
177 }
178
179 void __blk_crypto_free_request(struct request *rq);
180 static inline void blk_crypto_free_request(struct request *rq)
181 {
182         if (blk_crypto_rq_is_encrypted(rq))
183                 __blk_crypto_free_request(rq);
184 }
185
186 int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
187                              gfp_t gfp_mask);
188 /**
189  * blk_crypto_rq_bio_prep - Prepare a request's crypt_ctx when its first bio
190  *                          is inserted
191  * @rq: The request to prepare
192  * @bio: The first bio being inserted into the request
193  * @gfp_mask: Memory allocation flags
194  *
195  * Return: 0 on success, -ENOMEM if out of memory.  -ENOMEM is only possible if
196  *         @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
197  */
198 static inline int blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
199                                          gfp_t gfp_mask)
200 {
201         if (bio_has_crypt_ctx(bio))
202                 return __blk_crypto_rq_bio_prep(rq, bio, gfp_mask);
203         return 0;
204 }
205
206 /**
207  * blk_crypto_insert_cloned_request - Prepare a cloned request to be inserted
208  *                                    into a request queue.
209  * @rq: the request being queued
210  *
211  * Return: BLK_STS_OK on success, nonzero on error.
212  */
213 static inline blk_status_t blk_crypto_insert_cloned_request(struct request *rq)
214 {
215
216         if (blk_crypto_rq_is_encrypted(rq))
217                 return blk_crypto_rq_get_keyslot(rq);
218         return BLK_STS_OK;
219 }
220
221 #ifdef CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK
222
223 int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num);
224
225 bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr);
226
227 int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key);
228
229 #else /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
230
231 static inline int
232 blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
233 {
234         pr_warn_once("crypto API fallback is disabled\n");
235         return -ENOPKG;
236 }
237
238 static inline bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
239 {
240         pr_warn_once("crypto API fallback disabled; failing request.\n");
241         (*bio_ptr)->bi_status = BLK_STS_NOTSUPP;
242         return false;
243 }
244
245 static inline int
246 blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
247 {
248         return 0;
249 }
250
251 #endif /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
252
253 #endif /* __LINUX_BLK_CRYPTO_INTERNAL_H */