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