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