12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2a739ff3fSSami Tolvanen /*
3a739ff3fSSami Tolvanen * Copyright (C) 2015 Google, Inc.
4a739ff3fSSami Tolvanen *
5a739ff3fSSami Tolvanen * Author: Sami Tolvanen <samitolvanen@google.com>
6a739ff3fSSami Tolvanen */
7a739ff3fSSami Tolvanen
8a739ff3fSSami Tolvanen #ifndef DM_VERITY_FEC_H
9a739ff3fSSami Tolvanen #define DM_VERITY_FEC_H
10a739ff3fSSami Tolvanen
11a739ff3fSSami Tolvanen #include "dm-verity.h"
12a739ff3fSSami Tolvanen #include <linux/rslib.h>
13a739ff3fSSami Tolvanen
14a739ff3fSSami Tolvanen /* Reed-Solomon(M, N) parameters */
15a739ff3fSSami Tolvanen #define DM_VERITY_FEC_RSM 255
16a739ff3fSSami Tolvanen #define DM_VERITY_FEC_MAX_RSN 253
17a739ff3fSSami Tolvanen #define DM_VERITY_FEC_MIN_RSN 231 /* ~10% space overhead */
18a739ff3fSSami Tolvanen
19a739ff3fSSami Tolvanen /* buffers for deinterleaving and decoding */
20a739ff3fSSami Tolvanen #define DM_VERITY_FEC_BUF_PREALLOC 1 /* buffers to preallocate */
21a739ff3fSSami Tolvanen #define DM_VERITY_FEC_BUF_RS_BITS 4 /* 1 << RS blocks per buffer */
22a739ff3fSSami Tolvanen /* we need buffers for at most 1 << block size RS blocks */
23a739ff3fSSami Tolvanen #define DM_VERITY_FEC_BUF_MAX \
24a739ff3fSSami Tolvanen (1 << (PAGE_SHIFT - DM_VERITY_FEC_BUF_RS_BITS))
25a739ff3fSSami Tolvanen
26f1a880a9SSami Tolvanen /* maximum recursion level for verity_fec_decode */
27f1a880a9SSami Tolvanen #define DM_VERITY_FEC_MAX_RECURSION 4
28f1a880a9SSami Tolvanen
29a739ff3fSSami Tolvanen #define DM_VERITY_OPT_FEC_DEV "use_fec_from_device"
30a739ff3fSSami Tolvanen #define DM_VERITY_OPT_FEC_BLOCKS "fec_blocks"
31a739ff3fSSami Tolvanen #define DM_VERITY_OPT_FEC_START "fec_start"
32a739ff3fSSami Tolvanen #define DM_VERITY_OPT_FEC_ROOTS "fec_roots"
33a739ff3fSSami Tolvanen
34a739ff3fSSami Tolvanen /* configuration */
35a739ff3fSSami Tolvanen struct dm_verity_fec {
36a739ff3fSSami Tolvanen struct dm_dev *dev; /* parity data device */
37a739ff3fSSami Tolvanen struct dm_bufio_client *data_bufio; /* for data dev access */
38a739ff3fSSami Tolvanen struct dm_bufio_client *bufio; /* for parity data access */
398ca7cab8SJaegeuk Kim size_t io_size; /* IO size for roots */
40a739ff3fSSami Tolvanen sector_t start; /* parity data start in blocks */
41a739ff3fSSami Tolvanen sector_t blocks; /* number of blocks covered */
42a739ff3fSSami Tolvanen sector_t rounds; /* number of interleaving rounds */
43a739ff3fSSami Tolvanen sector_t hash_blocks; /* blocks covered after v->hash_start */
44a739ff3fSSami Tolvanen unsigned char roots; /* number of parity bytes, M-N of RS(M, N) */
45a739ff3fSSami Tolvanen unsigned char rsn; /* N of RS(M, N) */
466f1c819cSKent Overstreet mempool_t rs_pool; /* mempool for fio->rs */
476f1c819cSKent Overstreet mempool_t prealloc_pool; /* mempool for preallocated buffers */
486f1c819cSKent Overstreet mempool_t extra_pool; /* mempool for extra buffers */
496f1c819cSKent Overstreet mempool_t output_pool; /* mempool for output */
50a739ff3fSSami Tolvanen struct kmem_cache *cache; /* cache for buffers */
51a739ff3fSSami Tolvanen };
52a739ff3fSSami Tolvanen
53a739ff3fSSami Tolvanen /* per-bio data */
54a739ff3fSSami Tolvanen struct dm_verity_fec_io {
55a739ff3fSSami Tolvanen struct rs_control *rs; /* Reed-Solomon state */
56a739ff3fSSami Tolvanen int erasures[DM_VERITY_FEC_MAX_RSN]; /* erasures for decode_rs8 */
57a739ff3fSSami Tolvanen u8 *bufs[DM_VERITY_FEC_BUF_MAX]; /* bufs for deinterleaving */
58*86a3238cSHeinz Mauelshagen unsigned int nbufs; /* number of buffers allocated */
59a739ff3fSSami Tolvanen u8 *output; /* buffer for corrected output */
60a739ff3fSSami Tolvanen size_t output_pos;
61*86a3238cSHeinz Mauelshagen unsigned int level; /* recursion level */
62a739ff3fSSami Tolvanen };
63a739ff3fSSami Tolvanen
64a739ff3fSSami Tolvanen #ifdef CONFIG_DM_VERITY_FEC
65a739ff3fSSami Tolvanen
66a739ff3fSSami Tolvanen /* each feature parameter requires a value */
67a739ff3fSSami Tolvanen #define DM_VERITY_OPTS_FEC 8
68a739ff3fSSami Tolvanen
69a739ff3fSSami Tolvanen extern bool verity_fec_is_enabled(struct dm_verity *v);
70a739ff3fSSami Tolvanen
71a739ff3fSSami Tolvanen extern int verity_fec_decode(struct dm_verity *v, struct dm_verity_io *io,
72a739ff3fSSami Tolvanen enum verity_block_type type, sector_t block,
73a739ff3fSSami Tolvanen u8 *dest, struct bvec_iter *iter);
74a739ff3fSSami Tolvanen
75*86a3238cSHeinz Mauelshagen extern unsigned int verity_fec_status_table(struct dm_verity *v, unsigned int sz,
76*86a3238cSHeinz Mauelshagen char *result, unsigned int maxlen);
77a739ff3fSSami Tolvanen
78a739ff3fSSami Tolvanen extern void verity_fec_finish_io(struct dm_verity_io *io);
79a739ff3fSSami Tolvanen extern void verity_fec_init_io(struct dm_verity_io *io);
80a739ff3fSSami Tolvanen
81a739ff3fSSami Tolvanen extern bool verity_is_fec_opt_arg(const char *arg_name);
82a739ff3fSSami Tolvanen extern int verity_fec_parse_opt_args(struct dm_arg_set *as,
83*86a3238cSHeinz Mauelshagen struct dm_verity *v, unsigned int *argc,
84a739ff3fSSami Tolvanen const char *arg_name);
85a739ff3fSSami Tolvanen
86a739ff3fSSami Tolvanen extern void verity_fec_dtr(struct dm_verity *v);
87a739ff3fSSami Tolvanen
88a739ff3fSSami Tolvanen extern int verity_fec_ctr_alloc(struct dm_verity *v);
89a739ff3fSSami Tolvanen extern int verity_fec_ctr(struct dm_verity *v);
90a739ff3fSSami Tolvanen
91a739ff3fSSami Tolvanen #else /* !CONFIG_DM_VERITY_FEC */
92a739ff3fSSami Tolvanen
93a739ff3fSSami Tolvanen #define DM_VERITY_OPTS_FEC 0
94a739ff3fSSami Tolvanen
verity_fec_is_enabled(struct dm_verity * v)95a739ff3fSSami Tolvanen static inline bool verity_fec_is_enabled(struct dm_verity *v)
96a739ff3fSSami Tolvanen {
97a739ff3fSSami Tolvanen return false;
98a739ff3fSSami Tolvanen }
99a739ff3fSSami Tolvanen
verity_fec_decode(struct dm_verity * v,struct dm_verity_io * io,enum verity_block_type type,sector_t block,u8 * dest,struct bvec_iter * iter)100a739ff3fSSami Tolvanen static inline int verity_fec_decode(struct dm_verity *v,
101a739ff3fSSami Tolvanen struct dm_verity_io *io,
102a739ff3fSSami Tolvanen enum verity_block_type type,
103a739ff3fSSami Tolvanen sector_t block, u8 *dest,
104a739ff3fSSami Tolvanen struct bvec_iter *iter)
105a739ff3fSSami Tolvanen {
106a739ff3fSSami Tolvanen return -EOPNOTSUPP;
107a739ff3fSSami Tolvanen }
108a739ff3fSSami Tolvanen
verity_fec_status_table(struct dm_verity * v,unsigned int sz,char * result,unsigned int maxlen)109*86a3238cSHeinz Mauelshagen static inline unsigned int verity_fec_status_table(struct dm_verity *v,
110*86a3238cSHeinz Mauelshagen unsigned int sz, char *result,
111*86a3238cSHeinz Mauelshagen unsigned int maxlen)
112a739ff3fSSami Tolvanen {
113a739ff3fSSami Tolvanen return sz;
114a739ff3fSSami Tolvanen }
115a739ff3fSSami Tolvanen
verity_fec_finish_io(struct dm_verity_io * io)116a739ff3fSSami Tolvanen static inline void verity_fec_finish_io(struct dm_verity_io *io)
117a739ff3fSSami Tolvanen {
118a739ff3fSSami Tolvanen }
119a739ff3fSSami Tolvanen
verity_fec_init_io(struct dm_verity_io * io)120a739ff3fSSami Tolvanen static inline void verity_fec_init_io(struct dm_verity_io *io)
121a739ff3fSSami Tolvanen {
122a739ff3fSSami Tolvanen }
123a739ff3fSSami Tolvanen
verity_is_fec_opt_arg(const char * arg_name)124a739ff3fSSami Tolvanen static inline bool verity_is_fec_opt_arg(const char *arg_name)
125a739ff3fSSami Tolvanen {
126a739ff3fSSami Tolvanen return false;
127a739ff3fSSami Tolvanen }
128a739ff3fSSami Tolvanen
verity_fec_parse_opt_args(struct dm_arg_set * as,struct dm_verity * v,unsigned int * argc,const char * arg_name)129a739ff3fSSami Tolvanen static inline int verity_fec_parse_opt_args(struct dm_arg_set *as,
130a739ff3fSSami Tolvanen struct dm_verity *v,
131*86a3238cSHeinz Mauelshagen unsigned int *argc,
132a739ff3fSSami Tolvanen const char *arg_name)
133a739ff3fSSami Tolvanen {
134a739ff3fSSami Tolvanen return -EINVAL;
135a739ff3fSSami Tolvanen }
136a739ff3fSSami Tolvanen
verity_fec_dtr(struct dm_verity * v)137a739ff3fSSami Tolvanen static inline void verity_fec_dtr(struct dm_verity *v)
138a739ff3fSSami Tolvanen {
139a739ff3fSSami Tolvanen }
140a739ff3fSSami Tolvanen
verity_fec_ctr_alloc(struct dm_verity * v)141a739ff3fSSami Tolvanen static inline int verity_fec_ctr_alloc(struct dm_verity *v)
142a739ff3fSSami Tolvanen {
143a739ff3fSSami Tolvanen return 0;
144a739ff3fSSami Tolvanen }
145a739ff3fSSami Tolvanen
verity_fec_ctr(struct dm_verity * v)146a739ff3fSSami Tolvanen static inline int verity_fec_ctr(struct dm_verity *v)
147a739ff3fSSami Tolvanen {
148a739ff3fSSami Tolvanen return 0;
149a739ff3fSSami Tolvanen }
150a739ff3fSSami Tolvanen
151a739ff3fSSami Tolvanen #endif /* CONFIG_DM_VERITY_FEC */
152a739ff3fSSami Tolvanen
153a739ff3fSSami Tolvanen #endif /* DM_VERITY_FEC_H */
154