pstore.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Persistent Storage - pstore.h
  3. *
  4. * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
  5. *
  6. * This code is the generic layer to export data records from platform
  7. * level persistent storage via a file system.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #ifndef _LINUX_PSTORE_H
  23. #define _LINUX_PSTORE_H
  24. #include <linux/compiler.h>
  25. #include <linux/errno.h>
  26. #include <linux/kmsg_dump.h>
  27. #include <linux/mutex.h>
  28. #include <linux/semaphore.h>
  29. #include <linux/time.h>
  30. #include <linux/types.h>
  31. struct module;
  32. /* pstore record types (see fs/pstore/inode.c for filename templates) */
  33. enum pstore_type_id {
  34. PSTORE_TYPE_DMESG = 0,
  35. PSTORE_TYPE_MCE = 1,
  36. PSTORE_TYPE_CONSOLE = 2,
  37. PSTORE_TYPE_FTRACE = 3,
  38. /* PPC64 partition types */
  39. PSTORE_TYPE_PPC_RTAS = 4,
  40. PSTORE_TYPE_PPC_OF = 5,
  41. PSTORE_TYPE_PPC_COMMON = 6,
  42. PSTORE_TYPE_PMSG = 7,
  43. PSTORE_TYPE_PPC_OPAL = 8,
  44. PSTORE_TYPE_UNKNOWN = 255
  45. };
  46. struct pstore_info;
  47. /**
  48. * struct pstore_record - details of a pstore record entry
  49. * @psi: pstore backend driver information
  50. * @type: pstore record type
  51. * @id: per-type unique identifier for record
  52. * @time: timestamp of the record
  53. * @buf: pointer to record contents
  54. * @size: size of @buf
  55. * @ecc_notice_size:
  56. * ECC information for @buf
  57. *
  58. * Valid for PSTORE_TYPE_DMESG @type:
  59. *
  60. * @count: Oops count since boot
  61. * @reason: kdump reason for notification
  62. * @part: position in a multipart record
  63. * @compressed: whether the buffer is compressed
  64. *
  65. */
  66. struct pstore_record {
  67. struct pstore_info *psi;
  68. enum pstore_type_id type;
  69. u64 id;
  70. struct timespec64 time;
  71. char *buf;
  72. ssize_t size;
  73. ssize_t ecc_notice_size;
  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 repsonsible for this backend driver
  83. * @name: name of the backend driver
  84. *
  85. * @buf_lock: semaphore 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. * @data: backend-private pointer passed back during callbacks
  95. *
  96. * Callbacks:
  97. *
  98. * @open:
  99. * Notify backend that pstore is starting a full read of backend
  100. * records. Followed by one or more @read calls, and a final @close.
  101. *
  102. * @psi: in: pointer to the struct pstore_info for the backend
  103. *
  104. * Returns 0 on success, and non-zero on error.
  105. *
  106. * @close:
  107. * Notify backend that pstore has finished a full read of backend
  108. * records. Always preceded by an @open call and one or more @read
  109. * calls.
  110. *
  111. * @psi: in: pointer to the struct pstore_info for the backend
  112. *
  113. * Returns 0 on success, and non-zero on error. (Though pstore will
  114. * ignore the error.)
  115. *
  116. * @read:
  117. * Read next available backend record. Called after a successful
  118. * @open.
  119. *
  120. * @record:
  121. * pointer to record to populate. @buf should be allocated
  122. * by the backend and filled. At least @type and @id should
  123. * be populated, since these are used when creating pstorefs
  124. * file names.
  125. *
  126. * Returns record size on success, zero when no more records are
  127. * available, or negative on error.
  128. *
  129. * @write:
  130. * A newly generated record needs to be written to backend storage.
  131. *
  132. * @record:
  133. * pointer to record metadata. When @type is PSTORE_TYPE_DMESG,
  134. * @buf will be pointing to the preallocated @psi.buf, since
  135. * memory allocation may be broken during an Oops. Regardless,
  136. * @buf must be proccesed or copied before returning. The
  137. * backend is also expected to write @id with something that
  138. * can help identify this record to a future @erase callback.
  139. * The @time field will be prepopulated with the current time,
  140. * when available. The @size field will have the size of data
  141. * in @buf.
  142. *
  143. * Returns 0 on success, and non-zero on error.
  144. *
  145. * @write_user:
  146. * Perform a frontend write to a backend record, using a specified
  147. * buffer that is coming directly from userspace, instead of the
  148. * @record @buf.
  149. *
  150. * @record: pointer to record metadata.
  151. * @buf: pointer to userspace contents to write to backend
  152. *
  153. * Returns 0 on success, and non-zero on error.
  154. *
  155. * @erase:
  156. * Delete a record from backend storage. Different backends
  157. * identify records differently, so entire original record is
  158. * passed back to assist in identification of what the backend
  159. * should remove from storage.
  160. *
  161. * @record: pointer to record metadata.
  162. *
  163. * Returns 0 on success, and non-zero on error.
  164. *
  165. */
  166. struct pstore_info {
  167. struct module *owner;
  168. char *name;
  169. struct semaphore buf_lock;
  170. char *buf;
  171. size_t bufsize;
  172. struct mutex read_mutex;
  173. int flags;
  174. void *data;
  175. int (*open)(struct pstore_info *psi);
  176. int (*close)(struct pstore_info *psi);
  177. ssize_t (*read)(struct pstore_record *record);
  178. int (*write)(struct pstore_record *record);
  179. int (*write_user)(struct pstore_record *record,
  180. const char __user *buf);
  181. int (*erase)(struct pstore_record *record);
  182. };
  183. /* Supported frontends */
  184. #define PSTORE_FLAGS_DMESG (1 << 0)
  185. #define PSTORE_FLAGS_CONSOLE (1 << 1)
  186. #define PSTORE_FLAGS_FTRACE (1 << 2)
  187. #define PSTORE_FLAGS_PMSG (1 << 3)
  188. extern int pstore_register(struct pstore_info *);
  189. extern void pstore_unregister(struct pstore_info *);
  190. struct pstore_ftrace_record {
  191. unsigned long ip;
  192. unsigned long parent_ip;
  193. u64 ts;
  194. };
  195. /*
  196. * ftrace related stuff: Both backends and frontends need these so expose
  197. * them here.
  198. */
  199. #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB)
  200. #define PSTORE_CPU_IN_IP 0x1
  201. #elif NR_CPUS <= 4 && defined(CONFIG_ARM)
  202. #define PSTORE_CPU_IN_IP 0x3
  203. #endif
  204. #define TS_CPU_SHIFT 8
  205. #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1)
  206. /*
  207. * If CPU number can be stored in IP, store it there, otherwise store it in
  208. * the time stamp. This means more timestamp resolution is available when
  209. * the CPU can be stored in the IP.
  210. */
  211. #ifdef PSTORE_CPU_IN_IP
  212. static inline void
  213. pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
  214. {
  215. rec->ip |= cpu;
  216. }
  217. static inline unsigned int
  218. pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
  219. {
  220. return rec->ip & PSTORE_CPU_IN_IP;
  221. }
  222. static inline u64
  223. pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
  224. {
  225. return rec->ts;
  226. }
  227. static inline void
  228. pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
  229. {
  230. rec->ts = val;
  231. }
  232. #else
  233. static inline void
  234. pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
  235. {
  236. rec->ts &= ~(TS_CPU_MASK);
  237. rec->ts |= cpu;
  238. }
  239. static inline unsigned int
  240. pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
  241. {
  242. return rec->ts & TS_CPU_MASK;
  243. }
  244. static inline u64
  245. pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
  246. {
  247. return rec->ts >> TS_CPU_SHIFT;
  248. }
  249. static inline void
  250. pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
  251. {
  252. rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT);
  253. }
  254. #endif
  255. #endif /*_LINUX_PSTORE_H*/