pstore.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Persistent Storage - pstore.h
  4. *
  5. * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
  6. *
  7. * This code is the generic layer to export data records from platform
  8. * level persistent storage via a file system.
  9. */
  10. #ifndef _LINUX_PSTORE_H
  11. #define _LINUX_PSTORE_H
  12. #include <linux/compiler.h>
  13. #include <linux/errno.h>
  14. #include <linux/kmsg_dump.h>
  15. #include <linux/mutex.h>
  16. #include <linux/semaphore.h>
  17. #include <linux/time.h>
  18. #include <linux/types.h>
  19. struct module;
  20. /*
  21. * pstore record types (see fs/pstore/platform.c for pstore_type_names[])
  22. * These values may be written to storage (see EFI vars backend), so
  23. * they are kind of an ABI. Be careful changing the mappings.
  24. */
  25. enum pstore_type_id {
  26. /* Frontend storage types */
  27. PSTORE_TYPE_DMESG = 0,
  28. PSTORE_TYPE_MCE = 1,
  29. PSTORE_TYPE_CONSOLE = 2,
  30. PSTORE_TYPE_FTRACE = 3,
  31. /* PPC64-specific partition types */
  32. PSTORE_TYPE_PPC_RTAS = 4,
  33. PSTORE_TYPE_PPC_OF = 5,
  34. PSTORE_TYPE_PPC_COMMON = 6,
  35. PSTORE_TYPE_PMSG = 7,
  36. PSTORE_TYPE_PPC_OPAL = 8,
  37. /* End of the list */
  38. PSTORE_TYPE_MAX
  39. };
  40. const char *pstore_type_to_name(enum pstore_type_id type);
  41. enum pstore_type_id pstore_name_to_type(const char *name);
  42. struct pstore_info;
  43. /**
  44. * struct pstore_record - details of a pstore record entry
  45. * @psi: pstore backend driver information
  46. * @type: pstore record type
  47. * @id: per-type unique identifier for record
  48. * @time: timestamp of the record
  49. * @buf: pointer to record contents
  50. * @size: size of @buf
  51. * @ecc_notice_size:
  52. * ECC information for @buf
  53. *
  54. * Valid for PSTORE_TYPE_DMESG @type:
  55. *
  56. * @count: Oops count since boot
  57. * @reason: kdump reason for notification
  58. * @part: position in a multipart record
  59. * @compressed: whether the buffer is compressed
  60. *
  61. */
  62. struct pstore_record {
  63. struct pstore_info *psi;
  64. enum pstore_type_id type;
  65. u64 id;
  66. struct timespec64 time;
  67. char *buf;
  68. ssize_t size;
  69. ssize_t ecc_notice_size;
  70. int count;
  71. enum kmsg_dump_reason reason;
  72. unsigned int part;
  73. bool compressed;
  74. };
  75. /**
  76. * struct pstore_info - backend pstore driver structure
  77. *
  78. * @owner: module which is responsible for this backend driver
  79. * @name: name of the backend driver
  80. *
  81. * @buf_lock: semaphore to serialize access to @buf
  82. * @buf: preallocated crash dump buffer
  83. * @bufsize: size of @buf available for crash dump bytes (must match
  84. * smallest number of bytes available for writing to a
  85. * backend entry, since compressed bytes don't take kindly
  86. * to being truncated)
  87. *
  88. * @read_mutex: serializes @open, @read, @close, and @erase callbacks
  89. * @flags: bitfield of frontends the backend can accept writes for
  90. * @max_reason: Used when PSTORE_FLAGS_DMESG is set. Contains the
  91. * kmsg_dump_reason enum value. KMSG_DUMP_UNDEF means
  92. * "use existing kmsg_dump() filtering, based on the
  93. * printk.always_kmsg_dump boot param" (which is either
  94. * KMSG_DUMP_OOPS when false, or KMSG_DUMP_MAX when
  95. * true); see printk.always_kmsg_dump for more details.
  96. * @data: backend-private pointer passed back during callbacks
  97. *
  98. * Callbacks:
  99. *
  100. * @open:
  101. * Notify backend that pstore is starting a full read of backend
  102. * records. Followed by one or more @read calls, and a final @close.
  103. *
  104. * @psi: in: pointer to the struct pstore_info for the backend
  105. *
  106. * Returns 0 on success, and non-zero on error.
  107. *
  108. * @close:
  109. * Notify backend that pstore has finished a full read of backend
  110. * records. Always preceded by an @open call and one or more @read
  111. * calls.
  112. *
  113. * @psi: in: pointer to the struct pstore_info for the backend
  114. *
  115. * Returns 0 on success, and non-zero on error. (Though pstore will
  116. * ignore the error.)
  117. *
  118. * @read:
  119. * Read next available backend record. Called after a successful
  120. * @open.
  121. *
  122. * @record:
  123. * pointer to record to populate. @buf should be allocated
  124. * by the backend and filled. At least @type and @id should
  125. * be populated, since these are used when creating pstorefs
  126. * file names.
  127. *
  128. * Returns record size on success, zero when no more records are
  129. * available, or negative on error.
  130. *
  131. * @write:
  132. * A newly generated record needs to be written to backend storage.
  133. *
  134. * @record:
  135. * pointer to record metadata. When @type is PSTORE_TYPE_DMESG,
  136. * @buf will be pointing to the preallocated @psi.buf, since
  137. * memory allocation may be broken during an Oops. Regardless,
  138. * @buf must be proccesed or copied before returning. The
  139. * backend is also expected to write @id with something that
  140. * can help identify this record to a future @erase callback.
  141. * The @time field will be prepopulated with the current time,
  142. * when available. The @size field will have the size of data
  143. * in @buf.
  144. *
  145. * Returns 0 on success, and non-zero on error.
  146. *
  147. * @write_user:
  148. * Perform a frontend write to a backend record, using a specified
  149. * buffer that is coming directly from userspace, instead of the
  150. * @record @buf.
  151. *
  152. * @record: pointer to record metadata.
  153. * @buf: pointer to userspace contents to write to backend
  154. *
  155. * Returns 0 on success, and non-zero on error.
  156. *
  157. * @erase:
  158. * Delete a record from backend storage. Different backends
  159. * identify records differently, so entire original record is
  160. * passed back to assist in identification of what the backend
  161. * should remove from storage.
  162. *
  163. * @record: pointer to record metadata.
  164. *
  165. * Returns 0 on success, and non-zero on error.
  166. *
  167. */
  168. struct pstore_info {
  169. struct module *owner;
  170. const char *name;
  171. struct semaphore buf_lock;
  172. char *buf;
  173. size_t bufsize;
  174. struct mutex read_mutex;
  175. int flags;
  176. int max_reason;
  177. void *data;
  178. int (*open)(struct pstore_info *psi);
  179. int (*close)(struct pstore_info *psi);
  180. ssize_t (*read)(struct pstore_record *record);
  181. int (*write)(struct pstore_record *record);
  182. int (*write_user)(struct pstore_record *record,
  183. const char __user *buf);
  184. int (*erase)(struct pstore_record *record);
  185. };
  186. /* Supported frontends */
  187. #define PSTORE_FLAGS_DMESG BIT(0)
  188. #define PSTORE_FLAGS_CONSOLE BIT(1)
  189. #define PSTORE_FLAGS_FTRACE BIT(2)
  190. #define PSTORE_FLAGS_PMSG BIT(3)
  191. extern int pstore_register(struct pstore_info *);
  192. extern void pstore_unregister(struct pstore_info *);
  193. struct pstore_ftrace_record {
  194. unsigned long ip;
  195. unsigned long parent_ip;
  196. u64 ts;
  197. };
  198. /*
  199. * ftrace related stuff: Both backends and frontends need these so expose
  200. * them here.
  201. */
  202. #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB)
  203. #define PSTORE_CPU_IN_IP 0x1
  204. #elif NR_CPUS <= 4 && defined(CONFIG_ARM)
  205. #define PSTORE_CPU_IN_IP 0x3
  206. #endif
  207. #define TS_CPU_SHIFT 8
  208. #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1)
  209. /*
  210. * If CPU number can be stored in IP, store it there, otherwise store it in
  211. * the time stamp. This means more timestamp resolution is available when
  212. * the CPU can be stored in the IP.
  213. */
  214. #ifdef PSTORE_CPU_IN_IP
  215. static inline void
  216. pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
  217. {
  218. rec->ip |= cpu;
  219. }
  220. static inline unsigned int
  221. pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
  222. {
  223. return rec->ip & PSTORE_CPU_IN_IP;
  224. }
  225. static inline u64
  226. pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
  227. {
  228. return rec->ts;
  229. }
  230. static inline void
  231. pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
  232. {
  233. rec->ts = val;
  234. }
  235. #else
  236. static inline void
  237. pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
  238. {
  239. rec->ts &= ~(TS_CPU_MASK);
  240. rec->ts |= cpu;
  241. }
  242. static inline unsigned int
  243. pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
  244. {
  245. return rec->ts & TS_CPU_MASK;
  246. }
  247. static inline u64
  248. pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
  249. {
  250. return rec->ts >> TS_CPU_SHIFT;
  251. }
  252. static inline void
  253. pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
  254. {
  255. rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT);
  256. }
  257. #endif
  258. #endif /*_LINUX_PSTORE_H*/