debug.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright (c) International Business Machines Corp., 2006
  4. *
  5. * Author: Artem Bityutskiy (Битюцкий Артём)
  6. */
  7. #ifndef __UBI_DEBUG_H__
  8. #define __UBI_DEBUG_H__
  9. void ubi_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
  10. void ubi_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
  11. void ubi_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
  12. #include <linux/random.h>
  13. #define ubi_assert(expr) do { \
  14. if (unlikely(!(expr))) { \
  15. pr_crit("UBI assert failed in %s at %u (pid %d)\n", \
  16. __func__, __LINE__, current->pid); \
  17. dump_stack(); \
  18. } \
  19. } while (0)
  20. #define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \
  21. print_hex_dump(l, ps, pt, r, g, b, len, a)
  22. #define ubi_dbg_msg(type, fmt, ...) \
  23. pr_debug("UBI DBG " type " (pid %d): " fmt "\n", current->pid, \
  24. ##__VA_ARGS__)
  25. /* General debugging messages */
  26. #define dbg_gen(fmt, ...) ubi_dbg_msg("gen", fmt, ##__VA_ARGS__)
  27. /* Messages from the eraseblock association sub-system */
  28. #define dbg_eba(fmt, ...) ubi_dbg_msg("eba", fmt, ##__VA_ARGS__)
  29. /* Messages from the wear-leveling sub-system */
  30. #define dbg_wl(fmt, ...) ubi_dbg_msg("wl", fmt, ##__VA_ARGS__)
  31. /* Messages from the input/output sub-system */
  32. #define dbg_io(fmt, ...) ubi_dbg_msg("io", fmt, ##__VA_ARGS__)
  33. /* Initialization and build messages */
  34. #define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__)
  35. void ubi_dump_vol_info(const struct ubi_volume *vol);
  36. void ubi_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx);
  37. void ubi_dump_av(const struct ubi_ainf_volume *av);
  38. void ubi_dump_aeb(const struct ubi_ainf_peb *aeb, int type);
  39. void ubi_dump_mkvol_req(const struct ubi_mkvol_req *req);
  40. int ubi_self_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
  41. int len);
  42. int ubi_debugfs_init(void);
  43. void ubi_debugfs_exit(void);
  44. int ubi_debugfs_init_dev(struct ubi_device *ubi);
  45. void ubi_debugfs_exit_dev(struct ubi_device *ubi);
  46. /**
  47. * The following function is a legacy implementation of UBI fault-injection
  48. * hook. When using more powerful fault injection capabilities, the legacy
  49. * fault injection interface should be retained.
  50. */
  51. int ubi_dbg_power_cut(struct ubi_device *ubi, int caller);
  52. static inline int ubi_dbg_bitflip(const struct ubi_device *ubi)
  53. {
  54. if (ubi->dbg.emulate_bitflips)
  55. return !get_random_u32_below(200);
  56. return 0;
  57. }
  58. static inline int ubi_dbg_write_failure(const struct ubi_device *ubi)
  59. {
  60. if (ubi->dbg.emulate_io_failures)
  61. return !get_random_u32_below(500);
  62. return 0;
  63. }
  64. static inline int ubi_dbg_erase_failure(const struct ubi_device *ubi)
  65. {
  66. if (ubi->dbg.emulate_io_failures)
  67. return !get_random_u32_below(400);
  68. return 0;
  69. }
  70. /**
  71. * MASK_XXX: Mask for emulate_failures in ubi_debug_info.The mask is used to
  72. * precisely control the type and process of fault injection.
  73. */
  74. /* Emulate a power cut when writing EC/VID header */
  75. #define MASK_POWER_CUT_EC (1 << 0)
  76. #define MASK_POWER_CUT_VID (1 << 1)
  77. /* Emulate a power cut when writing data*/
  78. #define MASK_POWER_CUT_DATA (1 << 2)
  79. /* Emulate bit-flips */
  80. #define MASK_BITFLIPS (1 << 3)
  81. /* Emulate ecc error */
  82. #define MASK_ECCERR (1 << 4)
  83. /* Emulates -EIO during data read */
  84. #define MASK_READ_FAILURE (1 << 5)
  85. #define MASK_READ_FAILURE_EC (1 << 6)
  86. #define MASK_READ_FAILURE_VID (1 << 7)
  87. /* Emulates -EIO during data write */
  88. #define MASK_WRITE_FAILURE (1 << 8)
  89. /* Emulates -EIO during erase a PEB*/
  90. #define MASK_ERASE_FAILURE (1 << 9)
  91. /* Return UBI_IO_FF when reading EC/VID header */
  92. #define MASK_IO_FF_EC (1 << 10)
  93. #define MASK_IO_FF_VID (1 << 11)
  94. /* Return UBI_IO_FF_BITFLIPS when reading EC/VID header */
  95. #define MASK_IO_FF_BITFLIPS_EC (1 << 12)
  96. #define MASK_IO_FF_BITFLIPS_VID (1 << 13)
  97. /* Return UBI_IO_BAD_HDR when reading EC/VID header */
  98. #define MASK_BAD_HDR_EC (1 << 14)
  99. #define MASK_BAD_HDR_VID (1 << 15)
  100. /* Return UBI_IO_BAD_HDR_EBADMSG when reading EC/VID header */
  101. #define MASK_BAD_HDR_EBADMSG_EC (1 << 16)
  102. #define MASK_BAD_HDR_EBADMSG_VID (1 << 17)
  103. #ifdef CONFIG_MTD_UBI_FAULT_INJECTION
  104. extern bool should_fail_eccerr(void);
  105. extern bool should_fail_bitflips(void);
  106. extern bool should_fail_read_failure(void);
  107. extern bool should_fail_write_failure(void);
  108. extern bool should_fail_erase_failure(void);
  109. extern bool should_fail_power_cut(void);
  110. extern bool should_fail_io_ff(void);
  111. extern bool should_fail_io_ff_bitflips(void);
  112. extern bool should_fail_bad_hdr(void);
  113. extern bool should_fail_bad_hdr_ebadmsg(void);
  114. static inline bool ubi_dbg_fail_bitflip(const struct ubi_device *ubi)
  115. {
  116. if (ubi->dbg.emulate_failures & MASK_BITFLIPS)
  117. return should_fail_bitflips();
  118. return false;
  119. }
  120. static inline bool ubi_dbg_fail_write(const struct ubi_device *ubi)
  121. {
  122. if (ubi->dbg.emulate_failures & MASK_WRITE_FAILURE)
  123. return should_fail_write_failure();
  124. return false;
  125. }
  126. static inline bool ubi_dbg_fail_erase(const struct ubi_device *ubi)
  127. {
  128. if (ubi->dbg.emulate_failures & MASK_ERASE_FAILURE)
  129. return should_fail_erase_failure();
  130. return false;
  131. }
  132. static inline bool ubi_dbg_fail_power_cut(const struct ubi_device *ubi,
  133. unsigned int caller)
  134. {
  135. if (ubi->dbg.emulate_failures & caller)
  136. return should_fail_power_cut();
  137. return false;
  138. }
  139. static inline bool ubi_dbg_fail_read(const struct ubi_device *ubi,
  140. unsigned int caller)
  141. {
  142. if (ubi->dbg.emulate_failures & caller)
  143. return should_fail_read_failure();
  144. return false;
  145. }
  146. static inline bool ubi_dbg_fail_eccerr(const struct ubi_device *ubi)
  147. {
  148. if (ubi->dbg.emulate_failures & MASK_ECCERR)
  149. return should_fail_eccerr();
  150. return false;
  151. }
  152. static inline bool ubi_dbg_fail_ff(const struct ubi_device *ubi,
  153. unsigned int caller)
  154. {
  155. if (ubi->dbg.emulate_failures & caller)
  156. return should_fail_io_ff();
  157. return false;
  158. }
  159. static inline bool ubi_dbg_fail_ff_bitflips(const struct ubi_device *ubi,
  160. unsigned int caller)
  161. {
  162. if (ubi->dbg.emulate_failures & caller)
  163. return should_fail_io_ff_bitflips();
  164. return false;
  165. }
  166. static inline bool ubi_dbg_fail_bad_hdr(const struct ubi_device *ubi,
  167. unsigned int caller)
  168. {
  169. if (ubi->dbg.emulate_failures & caller)
  170. return should_fail_bad_hdr();
  171. return false;
  172. }
  173. static inline bool ubi_dbg_fail_bad_hdr_ebadmsg(const struct ubi_device *ubi,
  174. unsigned int caller)
  175. {
  176. if (ubi->dbg.emulate_failures & caller)
  177. return should_fail_bad_hdr_ebadmsg();
  178. return false;
  179. }
  180. #else /* CONFIG_MTD_UBI_FAULT_INJECTION */
  181. #define ubi_dbg_fail_bitflip(u) false
  182. #define ubi_dbg_fail_write(u) false
  183. #define ubi_dbg_fail_erase(u) false
  184. #define ubi_dbg_fail_power_cut(u, c) false
  185. #define ubi_dbg_fail_read(u, c) false
  186. #define ubi_dbg_fail_eccerr(u) false
  187. #define ubi_dbg_fail_ff(u, c) false
  188. #define ubi_dbg_fail_ff_bitflips(u, v) false
  189. #define ubi_dbg_fail_bad_hdr(u, c) false
  190. #define ubi_dbg_fail_bad_hdr_ebadmsg(u, c) false
  191. #endif
  192. /**
  193. * ubi_dbg_is_power_cut - if it is time to emulate power cut.
  194. * @ubi: UBI device description object
  195. *
  196. * Returns true if power cut should be emulated, otherwise returns false.
  197. */
  198. static inline bool ubi_dbg_is_power_cut(struct ubi_device *ubi,
  199. unsigned int caller)
  200. {
  201. if (ubi_dbg_power_cut(ubi, caller))
  202. return true;
  203. return ubi_dbg_fail_power_cut(ubi, caller);
  204. }
  205. /**
  206. * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
  207. * @ubi: UBI device description object
  208. *
  209. * Returns true if a bit-flip should be emulated, otherwise returns false.
  210. */
  211. static inline bool ubi_dbg_is_bitflip(const struct ubi_device *ubi)
  212. {
  213. if (ubi_dbg_bitflip(ubi))
  214. return true;
  215. return ubi_dbg_fail_bitflip(ubi);
  216. }
  217. /**
  218. * ubi_dbg_is_write_failure - if it is time to emulate a write failure.
  219. * @ubi: UBI device description object
  220. *
  221. * Returns true if a write failure should be emulated, otherwise returns
  222. * false.
  223. */
  224. static inline bool ubi_dbg_is_write_failure(const struct ubi_device *ubi)
  225. {
  226. if (ubi_dbg_write_failure(ubi))
  227. return true;
  228. return ubi_dbg_fail_write(ubi);
  229. }
  230. /**
  231. * ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
  232. * @ubi: UBI device description object
  233. *
  234. * Returns true if an erase failure should be emulated, otherwise returns
  235. * false.
  236. */
  237. static inline bool ubi_dbg_is_erase_failure(const struct ubi_device *ubi)
  238. {
  239. if (ubi_dbg_erase_failure(ubi))
  240. return true;
  241. return ubi_dbg_fail_erase(ubi);
  242. }
  243. /**
  244. * ubi_dbg_is_eccerr - if it is time to emulate ECC error.
  245. * @ubi: UBI device description object
  246. *
  247. * Returns true if a ECC error should be emulated, otherwise returns false.
  248. */
  249. static inline bool ubi_dbg_is_eccerr(const struct ubi_device *ubi)
  250. {
  251. return ubi_dbg_fail_eccerr(ubi);
  252. }
  253. /**
  254. * ubi_dbg_is_read_failure - if it is time to emulate a read failure.
  255. * @ubi: UBI device description object
  256. *
  257. * Returns true if a read failure should be emulated, otherwise returns
  258. * false.
  259. */
  260. static inline bool ubi_dbg_is_read_failure(const struct ubi_device *ubi,
  261. unsigned int caller)
  262. {
  263. return ubi_dbg_fail_read(ubi, caller);
  264. }
  265. /**
  266. * ubi_dbg_is_ff - if it is time to emulate that read region is only 0xFF.
  267. * @ubi: UBI device description object
  268. *
  269. * Returns true if read region should be emulated 0xFF, otherwise
  270. * returns false.
  271. */
  272. static inline bool ubi_dbg_is_ff(const struct ubi_device *ubi,
  273. unsigned int caller)
  274. {
  275. return ubi_dbg_fail_ff(ubi, caller);
  276. }
  277. /**
  278. * ubi_dbg_is_ff_bitflips - if it is time to emulate that read region is only 0xFF
  279. * with error reported by the MTD driver
  280. *
  281. * @ubi: UBI device description object
  282. *
  283. * Returns true if read region should be emulated 0xFF and error
  284. * reported by the MTD driver, otherwise returns false.
  285. */
  286. static inline bool ubi_dbg_is_ff_bitflips(const struct ubi_device *ubi,
  287. unsigned int caller)
  288. {
  289. return ubi_dbg_fail_ff_bitflips(ubi, caller);
  290. }
  291. /**
  292. * ubi_dbg_is_bad_hdr - if it is time to emulate a bad header
  293. * @ubi: UBI device description object
  294. *
  295. * Returns true if a bad header error should be emulated, otherwise
  296. * returns false.
  297. */
  298. static inline bool ubi_dbg_is_bad_hdr(const struct ubi_device *ubi,
  299. unsigned int caller)
  300. {
  301. return ubi_dbg_fail_bad_hdr(ubi, caller);
  302. }
  303. /**
  304. * ubi_dbg_is_bad_hdr_ebadmsg - if it is time to emulate a bad header with
  305. * ECC error.
  306. *
  307. * @ubi: UBI device description object
  308. *
  309. * Returns true if a bad header with ECC error should be emulated, otherwise
  310. * returns false.
  311. */
  312. static inline bool ubi_dbg_is_bad_hdr_ebadmsg(const struct ubi_device *ubi,
  313. unsigned int caller)
  314. {
  315. return ubi_dbg_fail_bad_hdr_ebadmsg(ubi, caller);
  316. }
  317. /**
  318. * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
  319. * @ubi: UBI device description object
  320. *
  321. * Returns non-zero if the UBI background thread is disabled for testing
  322. * purposes.
  323. */
  324. static inline int ubi_dbg_is_bgt_disabled(const struct ubi_device *ubi)
  325. {
  326. return ubi->dbg.disable_bgt;
  327. }
  328. static inline int ubi_dbg_chk_io(const struct ubi_device *ubi)
  329. {
  330. return ubi->dbg.chk_io;
  331. }
  332. static inline int ubi_dbg_chk_gen(const struct ubi_device *ubi)
  333. {
  334. return ubi->dbg.chk_gen;
  335. }
  336. static inline int ubi_dbg_chk_fastmap(const struct ubi_device *ubi)
  337. {
  338. return ubi->dbg.chk_fastmap;
  339. }
  340. static inline void ubi_enable_dbg_chk_fastmap(struct ubi_device *ubi)
  341. {
  342. ubi->dbg.chk_fastmap = 1;
  343. }
  344. #endif /* !__UBI_DEBUG_H__ */