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