xref: /openbmc/u-boot/drivers/mtd/ubi/debug.h (revision 63e22517)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) International Business Machines Corp., 2006
4  *
5  * Author: Artem Bityutskiy (Битюцкий Артём)
6  */
7 
8 #ifndef __UBI_DEBUG_H__
9 #define __UBI_DEBUG_H__
10 
11 void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
12 void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
13 void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
14 
15 #ifndef __UBOOT__
16 #include <linux/random.h>
17 #endif
18 
19 #define ubi_assert(expr)  do {                                               \
20 	if (unlikely(!(expr))) {                                             \
21 		pr_crit("UBI assert failed in %s at %u (pid %d)\n",          \
22 		       __func__, __LINE__, current->pid);                    \
23 		dump_stack();                                                \
24 	}                                                                    \
25 } while (0)
26 
27 #define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a)                   \
28 		print_hex_dump(l, ps, pt, r, g, b, len, a)
29 
30 #define ubi_dbg_msg(type, fmt, ...) \
31 	pr_debug("UBI DBG " type " (pid %d): " fmt "\n", current->pid,       \
32 		 ##__VA_ARGS__)
33 
34 /* General debugging messages */
35 #define dbg_gen(fmt, ...) ubi_dbg_msg("gen", fmt, ##__VA_ARGS__)
36 /* Messages from the eraseblock association sub-system */
37 #define dbg_eba(fmt, ...) ubi_dbg_msg("eba", fmt, ##__VA_ARGS__)
38 /* Messages from the wear-leveling sub-system */
39 #define dbg_wl(fmt, ...)  ubi_dbg_msg("wl", fmt, ##__VA_ARGS__)
40 /* Messages from the input/output sub-system */
41 #define dbg_io(fmt, ...)  ubi_dbg_msg("io", fmt, ##__VA_ARGS__)
42 /* Initialization and build messages */
43 #define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__)
44 
45 void ubi_dump_vol_info(const struct ubi_volume *vol);
46 void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx);
47 void ubi_dump_av(const struct ubi_ainf_volume *av);
48 void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type);
49 void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req);
50 int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
51 			  int len);
52 int ubi_debugfs_init(void);
53 void ubi_debugfs_exit(void);
54 int ubi_debugfs_init_dev(struct ubi_device *ubi);
55 void ubi_debugfs_exit_dev(struct ubi_device *ubi);
56 
57 /**
58  * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
59  * @ubi: UBI device description object
60  *
61  * Returns non-zero if the UBI background thread is disabled for testing
62  * purposes.
63  */
64 static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi)
65 {
66 	return ubi->dbg.disable_bgt;
67 }
68 
69 /**
70  * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
71  * @ubi: UBI device description object
72  *
73  * Returns non-zero if a bit-flip should be emulated, otherwise returns zero.
74  */
75 static inline int ubi_dbg_is_bitflip(const struct ubi_device *ubi)
76 {
77 	if (ubi->dbg.emulate_bitflips)
78 		return !(prandom_u32() % 200);
79 	return 0;
80 }
81 
82 /**
83  * ubi_dbg_is_write_failure - if it is time to emulate a write failure.
84  * @ubi: UBI device description object
85  *
86  * Returns non-zero if a write failure should be emulated, otherwise returns
87  * zero.
88  */
89 static inline int ubi_dbg_is_write_failure(const struct ubi_device *ubi)
90 {
91 	if (ubi->dbg.emulate_io_failures)
92 		return !(prandom_u32() % 500);
93 	return 0;
94 }
95 
96 /**
97  * ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
98  * @ubi: UBI device description object
99  *
100  * Returns non-zero if an erase failure should be emulated, otherwise returns
101  * zero.
102  */
103 static inline int ubi_dbg_is_erase_failure(const struct ubi_device *ubi)
104 {
105 	if (ubi->dbg.emulate_io_failures)
106 		return !(prandom_u32() % 400);
107 	return 0;
108 }
109 
110 static inline int ubi_dbg_chk_io(const struct ubi_device *ubi)
111 {
112 	return ubi->dbg.chk_io;
113 }
114 
115 static inline int ubi_dbg_chk_gen(const struct ubi_device *ubi)
116 {
117 	return ubi->dbg.chk_gen;
118 }
119 
120 static inline int ubi_dbg_chk_fastmap(const struct ubi_device *ubi)
121 {
122 	return ubi->dbg.chk_fastmap;
123 }
124 
125 static inline void ubi_enable_dbg_chk_fastmap(struct ubi_device *ubi)
126 {
127 	ubi->dbg.chk_fastmap = 1;
128 }
129 
130 int ubi_dbg_power_cut(struct ubi_device *ubi, int caller);
131 #endif /* !__UBI_DEBUG_H__ */
132