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