1801c135cSArtem B. Bityutskiy /* 2801c135cSArtem B. Bityutskiy * Copyright (c) International Business Machines Corp., 2006 3801c135cSArtem B. Bityutskiy * 4801c135cSArtem B. Bityutskiy * This program is free software; you can redistribute it and/or modify 5801c135cSArtem B. Bityutskiy * it under the terms of the GNU General Public License as published by 6801c135cSArtem B. Bityutskiy * the Free Software Foundation; either version 2 of the License, or 7801c135cSArtem B. Bityutskiy * (at your option) any later version. 8801c135cSArtem B. Bityutskiy * 9801c135cSArtem B. Bityutskiy * This program is distributed in the hope that it will be useful, 10801c135cSArtem B. Bityutskiy * but WITHOUT ANY WARRANTY; without even the implied warranty of 11801c135cSArtem B. Bityutskiy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12801c135cSArtem B. Bityutskiy * the GNU General Public License for more details. 13801c135cSArtem B. Bityutskiy * 14801c135cSArtem B. Bityutskiy * You should have received a copy of the GNU General Public License 15801c135cSArtem B. Bityutskiy * along with this program; if not, write to the Free Software 16801c135cSArtem B. Bityutskiy * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17801c135cSArtem B. Bityutskiy * 18801c135cSArtem B. Bityutskiy * Author: Artem Bityutskiy (Битюцкий Артём) 19801c135cSArtem B. Bityutskiy */ 20801c135cSArtem B. Bityutskiy 21801c135cSArtem B. Bityutskiy #ifndef __UBI_DEBUG_H__ 22801c135cSArtem B. Bityutskiy #define __UBI_DEBUG_H__ 23801c135cSArtem B. Bityutskiy 24ebfce01aSArtem Bityutskiy struct ubi_ec_hdr; 25ebfce01aSArtem Bityutskiy struct ubi_vid_hdr; 26ebfce01aSArtem Bityutskiy struct ubi_volume; 27ebfce01aSArtem Bityutskiy struct ubi_vtbl_record; 28ebfce01aSArtem Bityutskiy struct ubi_scan_volume; 29ebfce01aSArtem Bityutskiy struct ubi_scan_leb; 30ebfce01aSArtem Bityutskiy struct ubi_mkvol_req; 31ebfce01aSArtem Bityutskiy 32801c135cSArtem B. Bityutskiy #ifdef CONFIG_MTD_UBI_DEBUG 33801c135cSArtem B. Bityutskiy #include <linux/random.h> 34801c135cSArtem B. Bityutskiy 35801c135cSArtem B. Bityutskiy #define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__) 36801c135cSArtem B. Bityutskiy 37c8566350SArtem Bityutskiy #define ubi_assert(expr) do { \ 38c8566350SArtem Bityutskiy if (unlikely(!(expr))) { \ 39c8566350SArtem Bityutskiy printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \ 40c8566350SArtem Bityutskiy __func__, __LINE__, current->pid); \ 41c8566350SArtem Bityutskiy ubi_dbg_dump_stack(); \ 42c8566350SArtem Bityutskiy } \ 43c8566350SArtem Bityutskiy } while (0) 44801c135cSArtem B. Bityutskiy 45801c135cSArtem B. Bityutskiy #define dbg_msg(fmt, ...) \ 46d19bafd9SArtem Bityutskiy printk(KERN_DEBUG "UBI DBG (pid %d): %s: " fmt "\n", \ 47cb53b3b9SHarvey Harrison current->pid, __func__, ##__VA_ARGS__) 48801c135cSArtem B. Bityutskiy 49b342efd4SArtem Bityutskiy #define dbg_do_msg(typ, fmt, ...) do { \ 50b342efd4SArtem Bityutskiy if (ubi_msg_flags & typ) \ 51b342efd4SArtem Bityutskiy dbg_msg(fmt, ##__VA_ARGS__); \ 52b342efd4SArtem Bityutskiy } while (0) 53b342efd4SArtem Bityutskiy 54801c135cSArtem B. Bityutskiy #define ubi_dbg_dump_stack() dump_stack() 55801c135cSArtem B. Bityutskiy 56801c135cSArtem B. Bityutskiy void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr); 57801c135cSArtem B. Bityutskiy void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr); 58801c135cSArtem B. Bityutskiy void ubi_dbg_dump_vol_info(const struct ubi_volume *vol); 59801c135cSArtem B. Bityutskiy void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx); 60801c135cSArtem B. Bityutskiy void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv); 61801c135cSArtem B. Bityutskiy void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type); 62801c135cSArtem B. Bityutskiy void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req); 63867996b1SArtem Bityutskiy void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len); 64801c135cSArtem B. Bityutskiy 65b342efd4SArtem Bityutskiy extern unsigned int ubi_msg_flags; 66b342efd4SArtem Bityutskiy 67b342efd4SArtem Bityutskiy /* 68b342efd4SArtem Bityutskiy * Debugging message type flags (must match msg_type_names in debug.c). 69b342efd4SArtem Bityutskiy * 70b342efd4SArtem Bityutskiy * UBI_MSG_GEN: general messages 71b342efd4SArtem Bityutskiy * UBI_MSG_EBA: journal messages 72b342efd4SArtem Bityutskiy * UBI_MSG_WL: mount messages 73b342efd4SArtem Bityutskiy * UBI_MSG_IO: commit messages 74b342efd4SArtem Bityutskiy * UBI_MSG_BLD: LEB find messages 75b342efd4SArtem Bityutskiy */ 76b342efd4SArtem Bityutskiy enum { 77b342efd4SArtem Bityutskiy UBI_MSG_GEN = 0x1, 78b342efd4SArtem Bityutskiy UBI_MSG_EBA = 0x2, 79b342efd4SArtem Bityutskiy UBI_MSG_WL = 0x4, 80b342efd4SArtem Bityutskiy UBI_MSG_IO = 0x8, 81b342efd4SArtem Bityutskiy UBI_MSG_BLD = 0x10, 82b342efd4SArtem Bityutskiy }; 83b342efd4SArtem Bityutskiy 8431532494SArtem Bityutskiy #define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \ 8531532494SArtem Bityutskiy print_hex_dump(l, ps, pt, r, g, b, len, a) 8631532494SArtem Bityutskiy 87c8566350SArtem Bityutskiy /* General debugging messages */ 88b342efd4SArtem Bityutskiy #define dbg_gen(fmt, ...) dbg_do_msg(UBI_MSG_GEN, fmt, ##__VA_ARGS__) 89801c135cSArtem B. Bityutskiy 9085c6e6e2SArtem Bityutskiy /* Messages from the eraseblock association sub-system */ 91b342efd4SArtem Bityutskiy #define dbg_eba(fmt, ...) dbg_do_msg(UBI_MSG_EBA, fmt, ##__VA_ARGS__) 92801c135cSArtem B. Bityutskiy 9385c6e6e2SArtem Bityutskiy /* Messages from the wear-leveling sub-system */ 94b342efd4SArtem Bityutskiy #define dbg_wl(fmt, ...) dbg_do_msg(UBI_MSG_WL, fmt, ##__VA_ARGS__) 95801c135cSArtem B. Bityutskiy 9685c6e6e2SArtem Bityutskiy /* Messages from the input/output sub-system */ 97b342efd4SArtem Bityutskiy #define dbg_io(fmt, ...) dbg_do_msg(UBI_MSG_IO, fmt, ##__VA_ARGS__) 98801c135cSArtem B. Bityutskiy 99801c135cSArtem B. Bityutskiy /* Initialization and build messages */ 100b342efd4SArtem Bityutskiy #define dbg_bld(fmt, ...) dbg_do_msg(UBI_MSG_BLD, fmt, ##__VA_ARGS__) 101801c135cSArtem B. Bityutskiy 10292d124f5SArtem Bityutskiy extern unsigned int ubi_chk_flags; 10392d124f5SArtem Bityutskiy 10492d124f5SArtem Bityutskiy /* 10592d124f5SArtem Bityutskiy * Debugging check flags. 10692d124f5SArtem Bityutskiy * 10792d124f5SArtem Bityutskiy * UBI_CHK_GEN: general checks 10892d124f5SArtem Bityutskiy * UBI_CHK_IO: check writes and erases 10992d124f5SArtem Bityutskiy */ 11092d124f5SArtem Bityutskiy enum { 11192d124f5SArtem Bityutskiy UBI_CHK_GEN = 0x1, 11292d124f5SArtem Bityutskiy UBI_CHK_IO = 0x2, 11392d124f5SArtem Bityutskiy }; 11492d124f5SArtem Bityutskiy 11540a71a87SArtem Bityutskiy int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len); 1166e9065d7SArtem Bityutskiy int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, 1176e9065d7SArtem Bityutskiy int offset, int len); 11840a71a87SArtem Bityutskiy 11928237e45SArtem Bityutskiy extern unsigned int ubi_tst_flags; 120c8566350SArtem Bityutskiy 12128237e45SArtem Bityutskiy /* 12228237e45SArtem Bityutskiy * Special testing flags. 12328237e45SArtem Bityutskiy * 12428237e45SArtem Bityutskiy * UBIFS_TST_DISABLE_BGT: disable the background thread 12528237e45SArtem Bityutskiy * UBI_TST_EMULATE_BITFLIPS: emulate bit-flips 12628237e45SArtem Bityutskiy * UBI_TST_EMULATE_WRITE_FAILURES: emulate write failures 12728237e45SArtem Bityutskiy * UBI_TST_EMULATE_ERASE_FAILURES: emulate erase failures 12828237e45SArtem Bityutskiy */ 12928237e45SArtem Bityutskiy enum { 13028237e45SArtem Bityutskiy UBI_TST_DISABLE_BGT = 0x1, 13128237e45SArtem Bityutskiy UBI_TST_EMULATE_BITFLIPS = 0x2, 13228237e45SArtem Bityutskiy UBI_TST_EMULATE_WRITE_FAILURES = 0x4, 13328237e45SArtem Bityutskiy UBI_TST_EMULATE_ERASE_FAILURES = 0x8, 13428237e45SArtem Bityutskiy }; 13528237e45SArtem Bityutskiy 13628237e45SArtem Bityutskiy /** 13728237e45SArtem Bityutskiy * ubi_dbg_is_bgt_disabled - if the background thread is disabled. 13828237e45SArtem Bityutskiy * 13928237e45SArtem Bityutskiy * Returns non-zero if the UBI background thread is disabled for testing 14028237e45SArtem Bityutskiy * purposes. 14128237e45SArtem Bityutskiy */ 14228237e45SArtem Bityutskiy static inline int ubi_dbg_is_bgt_disabled(void) 14328237e45SArtem Bityutskiy { 14428237e45SArtem Bityutskiy return ubi_tst_flags & UBI_TST_DISABLE_BGT; 14528237e45SArtem Bityutskiy } 14628237e45SArtem Bityutskiy 147801c135cSArtem B. Bityutskiy /** 148801c135cSArtem B. Bityutskiy * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip. 149801c135cSArtem B. Bityutskiy * 150801c135cSArtem B. Bityutskiy * Returns non-zero if a bit-flip should be emulated, otherwise returns zero. 151801c135cSArtem B. Bityutskiy */ 152801c135cSArtem B. Bityutskiy static inline int ubi_dbg_is_bitflip(void) 153801c135cSArtem B. Bityutskiy { 15428237e45SArtem Bityutskiy if (ubi_tst_flags & UBI_TST_EMULATE_BITFLIPS) 155801c135cSArtem B. Bityutskiy return !(random32() % 200); 15628237e45SArtem Bityutskiy return 0; 157801c135cSArtem B. Bityutskiy } 158801c135cSArtem B. Bityutskiy 159801c135cSArtem B. Bityutskiy /** 160801c135cSArtem B. Bityutskiy * ubi_dbg_is_write_failure - if it is time to emulate a write failure. 161801c135cSArtem B. Bityutskiy * 162801c135cSArtem B. Bityutskiy * Returns non-zero if a write failure should be emulated, otherwise returns 163801c135cSArtem B. Bityutskiy * zero. 164801c135cSArtem B. Bityutskiy */ 165801c135cSArtem B. Bityutskiy static inline int ubi_dbg_is_write_failure(void) 166801c135cSArtem B. Bityutskiy { 16728237e45SArtem Bityutskiy if (ubi_tst_flags & UBI_TST_EMULATE_WRITE_FAILURES) 168801c135cSArtem B. Bityutskiy return !(random32() % 500); 16928237e45SArtem Bityutskiy return 0; 170801c135cSArtem B. Bityutskiy } 171801c135cSArtem B. Bityutskiy 172801c135cSArtem B. Bityutskiy /** 173801c135cSArtem B. Bityutskiy * ubi_dbg_is_erase_failure - if its time to emulate an erase failure. 174801c135cSArtem B. Bityutskiy * 175801c135cSArtem B. Bityutskiy * Returns non-zero if an erase failure should be emulated, otherwise returns 176801c135cSArtem B. Bityutskiy * zero. 177801c135cSArtem B. Bityutskiy */ 178801c135cSArtem B. Bityutskiy static inline int ubi_dbg_is_erase_failure(void) 179801c135cSArtem B. Bityutskiy { 18028237e45SArtem Bityutskiy if (ubi_tst_flags & UBI_TST_EMULATE_ERASE_FAILURES) 181801c135cSArtem B. Bityutskiy return !(random32() % 400); 18228237e45SArtem Bityutskiy return 0; 183801c135cSArtem B. Bityutskiy } 184801c135cSArtem B. Bityutskiy 185c8566350SArtem Bityutskiy #else 186c8566350SArtem Bityutskiy 1873802243aSArtem Bityutskiy /* Use "if (0)" to make compiler check arguments even if debugging is off */ 1883802243aSArtem Bityutskiy #define ubi_assert(expr) do { \ 1893802243aSArtem Bityutskiy if (0) { \ 1903802243aSArtem Bityutskiy printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \ 1913802243aSArtem Bityutskiy __func__, __LINE__, current->pid); \ 1923802243aSArtem Bityutskiy } \ 1933802243aSArtem Bityutskiy } while (0) 1943802243aSArtem Bityutskiy 1953802243aSArtem Bityutskiy #define dbg_err(fmt, ...) do { \ 1963802243aSArtem Bityutskiy if (0) \ 1973802243aSArtem Bityutskiy ubi_err(fmt, ##__VA_ARGS__); \ 1983802243aSArtem Bityutskiy } while (0) 1993802243aSArtem Bityutskiy 2003802243aSArtem Bityutskiy #define dbg_msg(fmt, ...) do { \ 2013802243aSArtem Bityutskiy if (0) \ 2023802243aSArtem Bityutskiy printk(KERN_DEBUG "UBI DBG (pid %d): %s: " fmt "\n", \ 2033802243aSArtem Bityutskiy current->pid, __func__, ##__VA_ARGS__); \ 2043802243aSArtem Bityutskiy } while (0) 2053802243aSArtem Bityutskiy 2063802243aSArtem Bityutskiy #define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 2073802243aSArtem Bityutskiy #define dbg_eba(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 2083802243aSArtem Bityutskiy #define dbg_wl(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 2093802243aSArtem Bityutskiy #define dbg_io(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 2103802243aSArtem Bityutskiy #define dbg_bld(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 2113802243aSArtem Bityutskiy 212ebfce01aSArtem Bityutskiy static inline void ubi_dbg_dump_stack(void) { return; } 213ebfce01aSArtem Bityutskiy static inline void 214ebfce01aSArtem Bityutskiy ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) { return; } 215ebfce01aSArtem Bityutskiy static inline void 216ebfce01aSArtem Bityutskiy ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) { return; } 217ebfce01aSArtem Bityutskiy static inline void 218ebfce01aSArtem Bityutskiy ubi_dbg_dump_vol_info(const struct ubi_volume *vol) { return; } 219ebfce01aSArtem Bityutskiy static inline void 220ebfce01aSArtem Bityutskiy ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) { return; } 221ebfce01aSArtem Bityutskiy static inline void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) { return; } 222ebfce01aSArtem Bityutskiy static inline void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, 223ebfce01aSArtem Bityutskiy int type) { return; } 224ebfce01aSArtem Bityutskiy static inline void 225ebfce01aSArtem Bityutskiy ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req) { return; } 226ebfce01aSArtem Bityutskiy static inline void ubi_dbg_dump_flash(struct ubi_device *ubi, 227ebfce01aSArtem Bityutskiy int pnum, int offset, int len) { return; } 228ebfce01aSArtem Bityutskiy static inline void 229ebfce01aSArtem Bityutskiy ubi_dbg_print_hex_dump(const char *l, const char *ps, int pt, int r, 230ebfce01aSArtem Bityutskiy int g, const void *b, size_t len, bool a) { return; } 231c8566350SArtem Bityutskiy 232ebfce01aSArtem Bityutskiy static inline int ubi_dbg_is_bgt_disabled(void) { return 0; } 233ebfce01aSArtem Bityutskiy static inline int ubi_dbg_is_bitflip(void) { return 0; } 234ebfce01aSArtem Bityutskiy static inline int ubi_dbg_is_write_failure(void) { return 0; } 235ebfce01aSArtem Bityutskiy static inline int ubi_dbg_is_erase_failure(void) { return 0; } 236ebfce01aSArtem Bityutskiy static inline int ubi_dbg_check_all_ff(struct ubi_device *ubi, 237ebfce01aSArtem Bityutskiy int pnum, int offset, 238ebfce01aSArtem Bityutskiy int len) { return 0; } 239ebfce01aSArtem Bityutskiy static inline int ubi_dbg_check_write(struct ubi_device *ubi, 240ebfce01aSArtem Bityutskiy const void *buf, int pnum, 241ebfce01aSArtem Bityutskiy int offset, int len) { return 0; } 242c8566350SArtem Bityutskiy 243c8566350SArtem Bityutskiy #endif /* !CONFIG_MTD_UBI_DEBUG */ 244801c135cSArtem B. Bityutskiy #endif /* !__UBI_DEBUG_H__ */ 245