pstore.h 7.8 KB

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