xref: /linux/include/linux/pstore.h (revision a61072aae693ba08390f92eed1dd0573fa5c3cd9)
1ca01d6ddSTony Luck /*
2ca01d6ddSTony Luck  * Persistent Storage - pstore.h
3ca01d6ddSTony Luck  *
4ca01d6ddSTony Luck  * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com>
5ca01d6ddSTony Luck  *
6ca01d6ddSTony Luck  * This code is the generic layer to export data records from platform
7ca01d6ddSTony Luck  * level persistent storage via a file system.
8ca01d6ddSTony Luck  *
9ca01d6ddSTony Luck  *  This program is free software; you can redistribute it and/or modify
10ca01d6ddSTony Luck  *  it under the terms of the GNU General Public License version 2 as
11ca01d6ddSTony Luck  *  published by the Free Software Foundation.
12ca01d6ddSTony Luck  *
13ca01d6ddSTony Luck  *  This program is distributed in the hope that it will be useful,
14ca01d6ddSTony Luck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15ca01d6ddSTony Luck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16ca01d6ddSTony Luck  *  GNU General Public License for more details.
17ca01d6ddSTony Luck  *
18ca01d6ddSTony Luck  *  You should have received a copy of the GNU General Public License
19ca01d6ddSTony Luck  *  along with this program; if not, write to the Free Software
20ca01d6ddSTony Luck  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21ca01d6ddSTony Luck  */
22ca01d6ddSTony Luck #ifndef _LINUX_PSTORE_H
23ca01d6ddSTony Luck #define _LINUX_PSTORE_H
24ca01d6ddSTony Luck 
255bf6d1b9SMark Salyzyn #include <linux/compiler.h>
265bf6d1b9SMark Salyzyn #include <linux/errno.h>
273d6d8d20SKees Cook #include <linux/kmsg_dump.h>
2867a101f5SAnton Vorontsov #include <linux/mutex.h>
2967a101f5SAnton Vorontsov #include <linux/spinlock.h>
305bf6d1b9SMark Salyzyn #include <linux/time.h>
315bf6d1b9SMark Salyzyn #include <linux/types.h>
323d6d8d20SKees Cook 
339abdccccSKees Cook struct module;
349abdccccSKees Cook 
350edae0b3SKees Cook /* pstore record types (see fs/pstore/inode.c for filename templates) */
36ca01d6ddSTony Luck enum pstore_type_id {
37ca01d6ddSTony Luck 	PSTORE_TYPE_DMESG	= 0,
38ca01d6ddSTony Luck 	PSTORE_TYPE_MCE		= 1,
39f29e5956SAnton Vorontsov 	PSTORE_TYPE_CONSOLE	= 2,
40060287b8SAnton Vorontsov 	PSTORE_TYPE_FTRACE	= 3,
4169020eeaSAruna Balakrishnaiah 	/* PPC64 partition types */
4269020eeaSAruna Balakrishnaiah 	PSTORE_TYPE_PPC_RTAS	= 4,
43f33f748cSAruna Balakrishnaiah 	PSTORE_TYPE_PPC_OF	= 5,
44a5e4797bSAruna Balakrishnaiah 	PSTORE_TYPE_PPC_COMMON	= 6,
459d5438f4SMark Salyzyn 	PSTORE_TYPE_PMSG	= 7,
46ae011d2eSHari Bathini 	PSTORE_TYPE_PPC_OPAL	= 8,
47ca01d6ddSTony Luck 	PSTORE_TYPE_UNKNOWN	= 255
48ca01d6ddSTony Luck };
49ca01d6ddSTony Luck 
509abdccccSKees Cook struct pstore_info;
519abdccccSKees Cook /**
529abdccccSKees Cook  * struct pstore_record - details of a pstore record entry
539abdccccSKees Cook  * @psi:	pstore backend driver information
549abdccccSKees Cook  * @type:	pstore record type
559abdccccSKees Cook  * @id:		per-type unique identifier for record
569abdccccSKees Cook  * @time:	timestamp of the record
579abdccccSKees Cook  * @buf:	pointer to record contents
589abdccccSKees Cook  * @size:	size of @buf
599abdccccSKees Cook  * @ecc_notice_size:
609abdccccSKees Cook  *		ECC information for @buf
6176cc9580SKees Cook  *
6276cc9580SKees Cook  * Valid for PSTORE_TYPE_DMESG @type:
6376cc9580SKees Cook  *
6476cc9580SKees Cook  * @count:	Oops count since boot
6576cc9580SKees Cook  * @reason:	kdump reason for notification
6676cc9580SKees Cook  * @part:	position in a multipart record
6776cc9580SKees Cook  * @compressed:	whether the buffer is compressed
6876cc9580SKees Cook  *
699abdccccSKees Cook  */
709abdccccSKees Cook struct pstore_record {
719abdccccSKees Cook 	struct pstore_info	*psi;
729abdccccSKees Cook 	enum pstore_type_id	type;
739abdccccSKees Cook 	u64			id;
749abdccccSKees Cook 	struct timespec		time;
759abdccccSKees Cook 	char			*buf;
769abdccccSKees Cook 	ssize_t			size;
779abdccccSKees Cook 	ssize_t			ecc_notice_size;
7876cc9580SKees Cook 
7976cc9580SKees Cook 	int			count;
8076cc9580SKees Cook 	enum kmsg_dump_reason	reason;
8176cc9580SKees Cook 	unsigned int		part;
8276cc9580SKees Cook 	bool			compressed;
839abdccccSKees Cook };
8467a101f5SAnton Vorontsov 
850edae0b3SKees Cook /**
860edae0b3SKees Cook  * struct pstore_info - backend pstore driver structure
870edae0b3SKees Cook  *
880edae0b3SKees Cook  * @owner:	module which is repsonsible for this backend driver
890edae0b3SKees Cook  * @name:	name of the backend driver
900edae0b3SKees Cook  *
910edae0b3SKees Cook  * @buf_lock:	spinlock to serialize access to @buf
920edae0b3SKees Cook  * @buf:	preallocated crash dump buffer
930edae0b3SKees Cook  * @bufsize:	size of @buf available for crash dump writes
940edae0b3SKees Cook  *
950edae0b3SKees Cook  * @read_mutex:	serializes @open, @read, @close, and @erase callbacks
960edae0b3SKees Cook  * @flags:	bitfield of frontends the backend can accept writes for
970edae0b3SKees Cook  * @data:	backend-private pointer passed back during callbacks
980edae0b3SKees Cook  *
990edae0b3SKees Cook  * Callbacks:
1000edae0b3SKees Cook  *
1010edae0b3SKees Cook  * @open:
1020edae0b3SKees Cook  *	Notify backend that pstore is starting a full read of backend
1030edae0b3SKees Cook  *	records. Followed by one or more @read calls, and a final @close.
1040edae0b3SKees Cook  *
1050edae0b3SKees Cook  *	@psi:	in: pointer to the struct pstore_info for the backend
1060edae0b3SKees Cook  *
1070edae0b3SKees Cook  *	Returns 0 on success, and non-zero on error.
1080edae0b3SKees Cook  *
1090edae0b3SKees Cook  * @close:
1100edae0b3SKees Cook  *	Notify backend that pstore has finished a full read of backend
1110edae0b3SKees Cook  *	records. Always preceded by an @open call and one or more @read
1120edae0b3SKees Cook  *	calls.
1130edae0b3SKees Cook  *
1140edae0b3SKees Cook  *	@psi:	in: pointer to the struct pstore_info for the backend
1150edae0b3SKees Cook  *
1160edae0b3SKees Cook  *	Returns 0 on success, and non-zero on error. (Though pstore will
1170edae0b3SKees Cook  *	ignore the error.)
1180edae0b3SKees Cook  *
1190edae0b3SKees Cook  * @read:
1200edae0b3SKees Cook  *	Read next available backend record. Called after a successful
1210edae0b3SKees Cook  *	@open.
1220edae0b3SKees Cook  *
123125cc42bSKees Cook  *	@record:
124125cc42bSKees Cook  *		pointer to record to populate. @buf should be allocated
125125cc42bSKees Cook  *		by the backend and filled. At least @type and @id should
126125cc42bSKees Cook  *		be populated, since these are used when creating pstorefs
127125cc42bSKees Cook  *		file names.
1280edae0b3SKees Cook  *
1290edae0b3SKees Cook  *	Returns record size on success, zero when no more records are
1300edae0b3SKees Cook  *	available, or negative on error.
1310edae0b3SKees Cook  *
1320edae0b3SKees Cook  * @write:
1330edae0b3SKees Cook  *	Perform a frontend notification of a write to a backend record. The
1340edae0b3SKees Cook  *	data to be stored has already been written to the registered @buf
1350edae0b3SKees Cook  *	of the @psi structure.
1360edae0b3SKees Cook  *
13776cc9580SKees Cook  *	@record:
13876cc9580SKees Cook  *		pointer to record metadata. Note that @buf is NULL, since
13976cc9580SKees Cook  *		the @buf registered with @psi is what has been written. The
14076cc9580SKees Cook  *		backend is expected to update @id.
1410edae0b3SKees Cook  *
1420edae0b3SKees Cook  *	Returns 0 on success, and non-zero on error.
1430edae0b3SKees Cook  *
1440edae0b3SKees Cook  * @write_buf:
1450edae0b3SKees Cook  *	Perform a frontend write to a backend record, using a specified
1460edae0b3SKees Cook  *	buffer. Unlike @write, this does not use the @psi @buf.
1470edae0b3SKees Cook  *
1480edae0b3SKees Cook  *	@type:	in: pstore record type to write
1490edae0b3SKees Cook  *	@reason:
1500edae0b3SKees Cook  *		in: pstore write reason
1510edae0b3SKees Cook  *	@id:	out: unique identifier for the record
1520edae0b3SKees Cook  *	@part:	in: position in a multipart write
1530edae0b3SKees Cook  *	@buf:	in: pointer to contents to write to backend record
1540edae0b3SKees Cook  *	@compressed:
1550edae0b3SKees Cook  *		in: if the record is compressed
1560edae0b3SKees Cook  *	@size:	in: size of the write
1570edae0b3SKees Cook  *	@psi:	in: pointer to the struct pstore_info for the backend
1580edae0b3SKees Cook  *
1590edae0b3SKees Cook  *	Returns 0 on success, and non-zero on error.
1600edae0b3SKees Cook  *
1610edae0b3SKees Cook  * @write_buf_user:
1620edae0b3SKees Cook  *	Perform a frontend write to a backend record, using a specified
1630edae0b3SKees Cook  *	buffer that is coming directly from userspace.
1640edae0b3SKees Cook  *
1650edae0b3SKees Cook  *	@type:	in: pstore record type to write
1660edae0b3SKees Cook  *	@reason:
1670edae0b3SKees Cook  *		in: pstore write reason
1680edae0b3SKees Cook  *	@id:	out: unique identifier for the record
1690edae0b3SKees Cook  *	@part:	in: position in a multipart write
1700edae0b3SKees Cook  *	@buf:	in: pointer to userspace contents to write to backend record
1710edae0b3SKees Cook  *	@compressed:
1720edae0b3SKees Cook  *		in: if the record is compressed
1730edae0b3SKees Cook  *	@size:	in: size of the write
1740edae0b3SKees Cook  *	@psi:	in: pointer to the struct pstore_info for the backend
1750edae0b3SKees Cook  *
1760edae0b3SKees Cook  *	Returns 0 on success, and non-zero on error.
1770edae0b3SKees Cook  *
1780edae0b3SKees Cook  * @erase:
1790edae0b3SKees Cook  *	Delete a record from backend storage.  Different backends
180*a61072aaSKees Cook  *	identify records differently, so entire original record is
181*a61072aaSKees Cook  *	passed back to assist in identification of what the backend
182*a61072aaSKees Cook  *	should remove from storage.
1830edae0b3SKees Cook  *
184*a61072aaSKees Cook  *	@record:	pointer to record metadata.
1850edae0b3SKees Cook  *
1860edae0b3SKees Cook  *	Returns 0 on success, and non-zero on error.
1870edae0b3SKees Cook  *
1880edae0b3SKees Cook  */
189ca01d6ddSTony Luck struct pstore_info {
190ca01d6ddSTony Luck 	struct module	*owner;
191ca01d6ddSTony Luck 	char		*name;
1920edae0b3SKees Cook 
1930edae0b3SKees Cook 	spinlock_t	buf_lock;
194ca01d6ddSTony Luck 	char		*buf;
195ca01d6ddSTony Luck 	size_t		bufsize;
1960edae0b3SKees Cook 
1970edae0b3SKees Cook 	struct mutex	read_mutex;
1980edae0b3SKees Cook 
199df36ac1bSLuck, Tony 	int		flags;
2000edae0b3SKees Cook 	void		*data;
2010edae0b3SKees Cook 
20206cf91b4SChen Gong 	int		(*open)(struct pstore_info *psi);
20306cf91b4SChen Gong 	int		(*close)(struct pstore_info *psi);
204125cc42bSKees Cook 	ssize_t		(*read)(struct pstore_record *record);
20576cc9580SKees Cook 	int		(*write)(struct pstore_record *record);
206897dba02SAnton Vorontsov 	int		(*write_buf)(enum pstore_type_id type,
207897dba02SAnton Vorontsov 			enum kmsg_dump_reason reason, u64 *id,
208b3b515bbSAruna Balakrishnaiah 			unsigned int part, const char *buf, bool compressed,
2096bbbca73SAruna Balakrishnaiah 			size_t size, struct pstore_info *psi);
2105bf6d1b9SMark Salyzyn 	int		(*write_buf_user)(enum pstore_type_id type,
2115bf6d1b9SMark Salyzyn 			enum kmsg_dump_reason reason, u64 *id,
2125bf6d1b9SMark Salyzyn 			unsigned int part, const char __user *buf,
2135bf6d1b9SMark Salyzyn 			bool compressed, size_t size, struct pstore_info *psi);
214*a61072aaSKees Cook 	int		(*erase)(struct pstore_record *record);
215ca01d6ddSTony Luck };
216ca01d6ddSTony Luck 
2170edae0b3SKees Cook /* Supported frontends */
218c950fd6fSNamhyung Kim #define PSTORE_FLAGS_DMESG	(1 << 0)
219c950fd6fSNamhyung Kim #define PSTORE_FLAGS_CONSOLE	(1 << 1)
220c950fd6fSNamhyung Kim #define PSTORE_FLAGS_FTRACE	(1 << 2)
221c950fd6fSNamhyung Kim #define PSTORE_FLAGS_PMSG	(1 << 3)
222c950fd6fSNamhyung Kim 
223ca01d6ddSTony Luck extern int pstore_register(struct pstore_info *);
224ee1d2674SGeliang Tang extern void pstore_unregister(struct pstore_info *);
2259f244e9cSSeiji Aguchi extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason);
226ca01d6ddSTony Luck 
227fbccdeb8SJoel Fernandes struct pstore_ftrace_record {
228fbccdeb8SJoel Fernandes 	unsigned long ip;
229fbccdeb8SJoel Fernandes 	unsigned long parent_ip;
230fbccdeb8SJoel Fernandes 	u64 ts;
231fbccdeb8SJoel Fernandes };
232fbccdeb8SJoel Fernandes 
233fbccdeb8SJoel Fernandes /*
234fbccdeb8SJoel Fernandes  * ftrace related stuff: Both backends and frontends need these so expose
235fbccdeb8SJoel Fernandes  * them here.
236fbccdeb8SJoel Fernandes  */
237fbccdeb8SJoel Fernandes 
238fbccdeb8SJoel Fernandes #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB)
239fbccdeb8SJoel Fernandes #define PSTORE_CPU_IN_IP 0x1
240fbccdeb8SJoel Fernandes #elif NR_CPUS <= 4 && defined(CONFIG_ARM)
241fbccdeb8SJoel Fernandes #define PSTORE_CPU_IN_IP 0x3
242fbccdeb8SJoel Fernandes #endif
243fbccdeb8SJoel Fernandes 
244fbccdeb8SJoel Fernandes #define TS_CPU_SHIFT 8
245fbccdeb8SJoel Fernandes #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1)
246fbccdeb8SJoel Fernandes 
247fbccdeb8SJoel Fernandes /*
248fbccdeb8SJoel Fernandes  * If CPU number can be stored in IP, store it there, otherwise store it in
249fbccdeb8SJoel Fernandes  * the time stamp. This means more timestamp resolution is available when
250fbccdeb8SJoel Fernandes  * the CPU can be stored in the IP.
251fbccdeb8SJoel Fernandes  */
252fbccdeb8SJoel Fernandes #ifdef PSTORE_CPU_IN_IP
253fbccdeb8SJoel Fernandes static inline void
254fbccdeb8SJoel Fernandes pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
255fbccdeb8SJoel Fernandes {
256fbccdeb8SJoel Fernandes 	rec->ip |= cpu;
257fbccdeb8SJoel Fernandes }
258fbccdeb8SJoel Fernandes 
259fbccdeb8SJoel Fernandes static inline unsigned int
260fbccdeb8SJoel Fernandes pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
261fbccdeb8SJoel Fernandes {
262fbccdeb8SJoel Fernandes 	return rec->ip & PSTORE_CPU_IN_IP;
263fbccdeb8SJoel Fernandes }
264fbccdeb8SJoel Fernandes 
265fbccdeb8SJoel Fernandes static inline u64
266fbccdeb8SJoel Fernandes pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
267fbccdeb8SJoel Fernandes {
268fbccdeb8SJoel Fernandes 	return rec->ts;
269fbccdeb8SJoel Fernandes }
270fbccdeb8SJoel Fernandes 
271fbccdeb8SJoel Fernandes static inline void
272fbccdeb8SJoel Fernandes pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
273fbccdeb8SJoel Fernandes {
274fbccdeb8SJoel Fernandes 	rec->ts = val;
275fbccdeb8SJoel Fernandes }
276fbccdeb8SJoel Fernandes #else
277fbccdeb8SJoel Fernandes static inline void
278fbccdeb8SJoel Fernandes pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu)
279fbccdeb8SJoel Fernandes {
280fbccdeb8SJoel Fernandes 	rec->ts &= ~(TS_CPU_MASK);
281fbccdeb8SJoel Fernandes 	rec->ts |= cpu;
282fbccdeb8SJoel Fernandes }
283fbccdeb8SJoel Fernandes 
284fbccdeb8SJoel Fernandes static inline unsigned int
285fbccdeb8SJoel Fernandes pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec)
286fbccdeb8SJoel Fernandes {
287fbccdeb8SJoel Fernandes 	return rec->ts & TS_CPU_MASK;
288fbccdeb8SJoel Fernandes }
289fbccdeb8SJoel Fernandes 
290fbccdeb8SJoel Fernandes static inline u64
291fbccdeb8SJoel Fernandes pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec)
292fbccdeb8SJoel Fernandes {
293fbccdeb8SJoel Fernandes 	return rec->ts >> TS_CPU_SHIFT;
294fbccdeb8SJoel Fernandes }
295fbccdeb8SJoel Fernandes 
296fbccdeb8SJoel Fernandes static inline void
297fbccdeb8SJoel Fernandes pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val)
298fbccdeb8SJoel Fernandes {
299fbccdeb8SJoel Fernandes 	rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT);
300fbccdeb8SJoel Fernandes }
301fbccdeb8SJoel Fernandes #endif
302fbccdeb8SJoel Fernandes 
303ca01d6ddSTony Luck #endif /*_LINUX_PSTORE_H*/
304