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 
25 #include <linux/compiler.h>
26 #include <linux/errno.h>
27 #include <linux/kmsg_dump.h>
28 #include <linux/mutex.h>
29 #include <linux/semaphore.h>
30 #include <linux/time.h>
31 #include <linux/types.h>
32 
33 struct module;
34 
35 /* pstore record types (see fs/pstore/inode.c for filename templates) */
36 enum pstore_type_id {
37 	PSTORE_TYPE_DMESG	= 0,
38 	PSTORE_TYPE_MCE		= 1,
39 	PSTORE_TYPE_CONSOLE	= 2,
40 	PSTORE_TYPE_FTRACE	= 3,
41 	/* PPC64 partition types */
42 	PSTORE_TYPE_PPC_RTAS	= 4,
43 	PSTORE_TYPE_PPC_OF	= 5,
44 	PSTORE_TYPE_PPC_COMMON	= 6,
45 	PSTORE_TYPE_PMSG	= 7,
46 	PSTORE_TYPE_PPC_OPAL	= 8,
47 	PSTORE_TYPE_UNKNOWN	= 255
48 };
49 
50 struct pstore_info;
51 /**
52  * struct pstore_record - details of a pstore record entry
53  * @psi:	pstore backend driver information
54  * @type:	pstore record type
55  * @id:		per-type unique identifier for record
56  * @time:	timestamp of the record
57  * @buf:	pointer to record contents
58  * @size:	size of @buf
59  * @ecc_notice_size:
60  *		ECC information for @buf
61  *
62  * Valid for PSTORE_TYPE_DMESG @type:
63  *
64  * @count:	Oops count since boot
65  * @reason:	kdump reason for notification
66  * @part:	position in a multipart record
67  * @compressed:	whether the buffer is compressed
68  *
69  */
70 struct pstore_record {
71 	struct pstore_info	*psi;
72 	enum pstore_type_id	type;
73 	u64			id;
74 	struct timespec64	time;
75 	char			*buf;
76 	ssize_t			size;
77 	ssize_t			ecc_notice_size;
78 
79 	int			count;
80 	enum kmsg_dump_reason	reason;
81 	unsigned int		part;
82 	bool			compressed;
83 };
84 
85 /**
86  * struct pstore_info - backend pstore driver structure
87  *
88  * @owner:	module which is repsonsible for this backend driver
89  * @name:	name of the backend driver
90  *
91  * @buf_lock:	semaphore to serialize access to @buf
92  * @buf:	preallocated crash dump buffer
93  * @bufsize:	size of @buf available for crash dump bytes (must match
94  *		smallest number of bytes available for writing to a
95  *		backend entry, since compressed bytes don't take kindly
96  *		to being truncated)
97  *
98  * @read_mutex:	serializes @open, @read, @close, and @erase callbacks
99  * @flags:	bitfield of frontends the backend can accept writes for
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 	char		*name;
175 
176 	struct semaphore buf_lock;
177 	char		*buf;
178 	size_t		bufsize;
179 
180 	struct mutex	read_mutex;
181 
182 	int		flags;
183 	void		*data;
184 
185 	int		(*open)(struct pstore_info *psi);
186 	int		(*close)(struct pstore_info *psi);
187 	ssize_t		(*read)(struct pstore_record *record);
188 	int		(*write)(struct pstore_record *record);
189 	int		(*write_user)(struct pstore_record *record,
190 				      const char __user *buf);
191 	int		(*erase)(struct pstore_record *record);
192 };
193 
194 /* Supported frontends */
195 #define PSTORE_FLAGS_DMESG	(1 << 0)
196 #define PSTORE_FLAGS_CONSOLE	(1 << 1)
197 #define PSTORE_FLAGS_FTRACE	(1 << 2)
198 #define PSTORE_FLAGS_PMSG	(1 << 3)
199 
200 extern int pstore_register(struct pstore_info *);
201 extern void pstore_unregister(struct pstore_info *);
202 
203 struct pstore_ftrace_record {
204 	unsigned long ip;
205 	unsigned long parent_ip;
206 	u64 ts;
207 };
208 
209 /*
210  * ftrace related stuff: Both backends and frontends need these so expose
211  * them here.
212  */
213 
214 #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB)
215 #define PSTORE_CPU_IN_IP 0x1
216 #elif NR_CPUS <= 4 && defined(CONFIG_ARM)
217 #define PSTORE_CPU_IN_IP 0x3
218 #endif
219 
220 #define TS_CPU_SHIFT 8
221 #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1)
222 
223 /*
224  * If CPU number can be stored in IP, store it there, otherwise store it in
225  * the time stamp. This means more timestamp resolution is available when
226  * the CPU can be stored in the IP.
227  */
228 #ifdef PSTORE_CPU_IN_IP
229 static inline void
pstore_ftrace_encode_cpu(struct pstore_ftrace_record * rec,unsigned int cpu)230 pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
231 {
232 	rec->ip |= cpu;
233 }
234 
235 static inline unsigned int
pstore_ftrace_decode_cpu(struct pstore_ftrace_record * rec)236 pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
237 {
238 	return rec->ip & PSTORE_CPU_IN_IP;
239 }
240 
241 static inline u64
pstore_ftrace_read_timestamp(struct pstore_ftrace_record * rec)242 pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
243 {
244 	return rec->ts;
245 }
246 
247 static inline void
pstore_ftrace_write_timestamp(struct pstore_ftrace_record * rec,u64 val)248 pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
249 {
250 	rec->ts = val;
251 }
252 #else
253 static inline void
pstore_ftrace_encode_cpu(struct pstore_ftrace_record * rec,unsigned int cpu)254 pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
255 {
256 	rec->ts &= ~(TS_CPU_MASK);
257 	rec->ts |= cpu;
258 }
259 
260 static inline unsigned int
pstore_ftrace_decode_cpu(struct pstore_ftrace_record * rec)261 pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
262 {
263 	return rec->ts & TS_CPU_MASK;
264 }
265 
266 static inline u64
pstore_ftrace_read_timestamp(struct pstore_ftrace_record * rec)267 pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
268 {
269 	return rec->ts >> TS_CPU_SHIFT;
270 }
271 
272 static inline void
pstore_ftrace_write_timestamp(struct pstore_ftrace_record * rec,u64 val)273 pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
274 {
275 	rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT);
276 }
277 #endif
278 
279 #endif /*_LINUX_PSTORE_H*/
280