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