UBI: turn some macros into static inline
[pandora-kernel.git] / drivers / mtd / ubi / debug.h
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Artem Bityutskiy (Битюцкий Артём)
19  */
20
21 #ifndef __UBI_DEBUG_H__
22 #define __UBI_DEBUG_H__
23
24 struct ubi_ec_hdr;
25 struct ubi_vid_hdr;
26 struct ubi_volume;
27 struct ubi_vtbl_record;
28 struct ubi_scan_volume;
29 struct ubi_scan_leb;
30 struct ubi_mkvol_req;
31
32 #ifdef CONFIG_MTD_UBI_DEBUG
33 #include <linux/random.h>
34
35 #define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__)
36
37 #define ubi_assert(expr)  do {                                               \
38         if (unlikely(!(expr))) {                                             \
39                 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
40                        __func__, __LINE__, current->pid);                    \
41                 ubi_dbg_dump_stack();                                        \
42         }                                                                    \
43 } while (0)
44
45 #define dbg_msg(fmt, ...)                                    \
46         printk(KERN_DEBUG "UBI DBG (pid %d): %s: " fmt "\n", \
47                current->pid, __func__, ##__VA_ARGS__)
48
49 #define dbg_do_msg(typ, fmt, ...) do {                       \
50         if (ubi_msg_flags & typ)                             \
51                 dbg_msg(fmt, ##__VA_ARGS__);                 \
52 } while (0)
53
54 #define ubi_dbg_dump_stack() dump_stack()
55
56 void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
57 void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
58 void ubi_dbg_dump_vol_info(const struct ubi_volume *vol);
59 void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx);
60 void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv);
61 void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type);
62 void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req);
63 void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
64
65 extern unsigned int ubi_msg_flags;
66
67 /*
68  * Debugging message type flags (must match msg_type_names in debug.c).
69  *
70  * UBI_MSG_GEN: general messages
71  * UBI_MSG_EBA: journal messages
72  * UBI_MSG_WL: mount messages
73  * UBI_MSG_IO: commit messages
74  * UBI_MSG_BLD: LEB find messages
75  */
76 enum {
77         UBI_MSG_GEN  = 0x1,
78         UBI_MSG_EBA  = 0x2,
79         UBI_MSG_WL   = 0x4,
80         UBI_MSG_IO   = 0x8,
81         UBI_MSG_BLD  = 0x10,
82 };
83
84 #define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a)  \
85                 print_hex_dump(l, ps, pt, r, g, b, len, a)
86
87 /* General debugging messages */
88 #define dbg_gen(fmt, ...) dbg_do_msg(UBI_MSG_GEN, fmt, ##__VA_ARGS__)
89
90 /* Messages from the eraseblock association sub-system */
91 #define dbg_eba(fmt, ...) dbg_do_msg(UBI_MSG_EBA, fmt, ##__VA_ARGS__)
92
93 /* Messages from the wear-leveling sub-system */
94 #define dbg_wl(fmt, ...) dbg_do_msg(UBI_MSG_WL, fmt, ##__VA_ARGS__)
95
96 /* Messages from the input/output sub-system */
97 #define dbg_io(fmt, ...) dbg_do_msg(UBI_MSG_IO, fmt, ##__VA_ARGS__)
98
99 /* Initialization and build messages */
100 #define dbg_bld(fmt, ...) dbg_do_msg(UBI_MSG_BLD, fmt, ##__VA_ARGS__)
101
102 extern unsigned int ubi_chk_flags;
103
104 /*
105  * Debugging check flags.
106  *
107  * UBI_CHK_GEN: general checks
108  * UBI_CHK_IO: check writes and erases
109  */
110 enum {
111         UBI_CHK_GEN = 0x1,
112         UBI_CHK_IO  = 0x2,
113 };
114
115 int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len);
116 int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
117                         int offset, int len);
118
119 extern unsigned int ubi_tst_flags;
120
121 /*
122  * Special testing flags.
123  *
124  * UBIFS_TST_DISABLE_BGT: disable the background thread
125  * UBI_TST_EMULATE_BITFLIPS: emulate bit-flips
126  * UBI_TST_EMULATE_WRITE_FAILURES: emulate write failures
127  * UBI_TST_EMULATE_ERASE_FAILURES: emulate erase failures
128  */
129 enum {
130         UBI_TST_DISABLE_BGT            = 0x1,
131         UBI_TST_EMULATE_BITFLIPS       = 0x2,
132         UBI_TST_EMULATE_WRITE_FAILURES = 0x4,
133         UBI_TST_EMULATE_ERASE_FAILURES = 0x8,
134 };
135
136 /**
137  * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
138  *
139  * Returns non-zero if the UBI background thread is disabled for testing
140  * purposes.
141  */
142 static inline int ubi_dbg_is_bgt_disabled(void)
143 {
144         return ubi_tst_flags & UBI_TST_DISABLE_BGT;
145 }
146
147 /**
148  * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
149  *
150  * Returns non-zero if a bit-flip should be emulated, otherwise returns zero.
151  */
152 static inline int ubi_dbg_is_bitflip(void)
153 {
154         if (ubi_tst_flags & UBI_TST_EMULATE_BITFLIPS)
155                 return !(random32() % 200);
156         return 0;
157 }
158
159 /**
160  * ubi_dbg_is_write_failure - if it is time to emulate a write failure.
161  *
162  * Returns non-zero if a write failure should be emulated, otherwise returns
163  * zero.
164  */
165 static inline int ubi_dbg_is_write_failure(void)
166 {
167         if (ubi_tst_flags & UBI_TST_EMULATE_WRITE_FAILURES)
168                 return !(random32() % 500);
169         return 0;
170 }
171
172 /**
173  * ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
174  *
175  * Returns non-zero if an erase failure should be emulated, otherwise returns
176  * zero.
177  */
178 static inline int ubi_dbg_is_erase_failure(void)
179 {
180         if (ubi_tst_flags & UBI_TST_EMULATE_ERASE_FAILURES)
181                 return !(random32() % 400);
182         return 0;
183 }
184
185 #else
186
187 /* Use "if (0)" to make compiler check arguments even if debugging is off */
188 #define ubi_assert(expr)  do {                                               \
189         if (0) {                                                             \
190                 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
191                        __func__, __LINE__, current->pid);                    \
192         }                                                                    \
193 } while (0)
194
195 #define dbg_err(fmt, ...) do {                                               \
196         if (0)                                                               \
197                 ubi_err(fmt, ##__VA_ARGS__);                                 \
198 } while (0)
199
200 #define dbg_msg(fmt, ...) do {                                               \
201         if (0)                                                               \
202                 printk(KERN_DEBUG "UBI DBG (pid %d): %s: " fmt "\n",         \
203                        current->pid, __func__, ##__VA_ARGS__);               \
204 } while (0)
205
206 #define dbg_gen(fmt, ...)  dbg_msg(fmt, ##__VA_ARGS__)
207 #define dbg_eba(fmt, ...)  dbg_msg(fmt, ##__VA_ARGS__)
208 #define dbg_wl(fmt, ...)   dbg_msg(fmt, ##__VA_ARGS__)
209 #define dbg_io(fmt, ...)   dbg_msg(fmt, ##__VA_ARGS__)
210 #define dbg_bld(fmt, ...)  dbg_msg(fmt, ##__VA_ARGS__)
211
212 static inline void ubi_dbg_dump_stack(void)                          { return; }
213 static inline void
214 ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)                 { return; }
215 static inline void
216 ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)              { return; }
217 static inline void
218 ubi_dbg_dump_vol_info(const struct ubi_volume *vol)                  { return; }
219 static inline void
220 ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)   { return; }
221 static inline void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) { return; }
222 static inline void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb,
223                                     int type)                        { return; }
224 static inline void
225 ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req)              { return; }
226 static inline void ubi_dbg_dump_flash(struct ubi_device *ubi,
227                                       int pnum, int offset, int len) { return; }
228 static inline void
229 ubi_dbg_print_hex_dump(const char *l, const char *ps, int pt, int r,
230                        int g, const void *b, size_t len, bool a)     { return; }
231
232 static inline int ubi_dbg_is_bgt_disabled(void)                    { return 0; }
233 static inline int ubi_dbg_is_bitflip(void)                         { return 0; }
234 static inline int ubi_dbg_is_write_failure(void)                   { return 0; }
235 static inline int ubi_dbg_is_erase_failure(void)                   { return 0; }
236 static inline int ubi_dbg_check_all_ff(struct ubi_device *ubi,
237                                        int pnum, int offset,
238                                        int len)                    { return 0; }
239 static inline int ubi_dbg_check_write(struct ubi_device *ubi,
240                                       const void *buf, int pnum,
241                                       int offset, int len)         { return 0; }
242
243 #endif /* !CONFIG_MTD_UBI_DEBUG */
244 #endif /* !__UBI_DEBUG_H__ */