145051539SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 2ca01d6ddSTony Luck /* 3ca01d6ddSTony Luck * Persistent Storage - pstore.h 4ca01d6ddSTony Luck * 5ca01d6ddSTony Luck * Copyright (C) 2010 Intel Corporation <tony.luck@intel.com> 6ca01d6ddSTony Luck * 7ca01d6ddSTony Luck * This code is the generic layer to export data records from platform 8ca01d6ddSTony Luck * level persistent storage via a file system. 9ca01d6ddSTony Luck */ 10ca01d6ddSTony Luck #ifndef _LINUX_PSTORE_H 11ca01d6ddSTony Luck #define _LINUX_PSTORE_H 12ca01d6ddSTony Luck 135bf6d1b9SMark Salyzyn #include <linux/compiler.h> 145bf6d1b9SMark Salyzyn #include <linux/errno.h> 153d6d8d20SKees Cook #include <linux/kmsg_dump.h> 1667a101f5SAnton Vorontsov #include <linux/mutex.h> 178126b1c7SJann Horn #include <linux/spinlock.h> 185bf6d1b9SMark Salyzyn #include <linux/time.h> 195bf6d1b9SMark Salyzyn #include <linux/types.h> 203d6d8d20SKees Cook 219abdccccSKees Cook struct module; 229abdccccSKees Cook 23f0f23e54SJoel Fernandes (Google) /* 24f0f23e54SJoel Fernandes (Google) * pstore record types (see fs/pstore/platform.c for pstore_type_names[]) 25f0f23e54SJoel Fernandes (Google) * These values may be written to storage (see EFI vars backend), so 26f0f23e54SJoel Fernandes (Google) * they are kind of an ABI. Be careful changing the mappings. 27f0f23e54SJoel Fernandes (Google) */ 28ca01d6ddSTony Luck enum pstore_type_id { 29f0f23e54SJoel Fernandes (Google) /* Frontend storage types */ 30ca01d6ddSTony Luck PSTORE_TYPE_DMESG = 0, 31ca01d6ddSTony Luck PSTORE_TYPE_MCE = 1, 32f29e5956SAnton Vorontsov PSTORE_TYPE_CONSOLE = 2, 33060287b8SAnton Vorontsov PSTORE_TYPE_FTRACE = 3, 34f0f23e54SJoel Fernandes (Google) 35f0f23e54SJoel Fernandes (Google) /* PPC64-specific partition types */ 3669020eeaSAruna Balakrishnaiah PSTORE_TYPE_PPC_RTAS = 4, 37f33f748cSAruna Balakrishnaiah PSTORE_TYPE_PPC_OF = 5, 38a5e4797bSAruna Balakrishnaiah PSTORE_TYPE_PPC_COMMON = 6, 399d5438f4SMark Salyzyn PSTORE_TYPE_PMSG = 7, 40ae011d2eSHari Bathini PSTORE_TYPE_PPC_OPAL = 8, 41f0f23e54SJoel Fernandes (Google) 42f0f23e54SJoel Fernandes (Google) /* End of the list */ 43f0f23e54SJoel Fernandes (Google) PSTORE_TYPE_MAX 44ca01d6ddSTony Luck }; 45ca01d6ddSTony Luck 46f0f23e54SJoel Fernandes (Google) const char *pstore_type_to_name(enum pstore_type_id type); 47f0f23e54SJoel Fernandes (Google) enum pstore_type_id pstore_name_to_type(const char *name); 48f0f23e54SJoel Fernandes (Google) 499abdccccSKees Cook struct pstore_info; 509abdccccSKees Cook /** 519abdccccSKees Cook * struct pstore_record - details of a pstore record entry 529abdccccSKees Cook * @psi: pstore backend driver information 539abdccccSKees Cook * @type: pstore record type 549abdccccSKees Cook * @id: per-type unique identifier for record 559abdccccSKees Cook * @time: timestamp of the record 569abdccccSKees Cook * @buf: pointer to record contents 579abdccccSKees Cook * @size: size of @buf 589abdccccSKees Cook * @ecc_notice_size: 599abdccccSKees Cook * ECC information for @buf 60*8ca869b2SArd Biesheuvel * @priv: pointer for backend specific use, will be 61*8ca869b2SArd Biesheuvel * kfree()d by the pstore core if non-NULL 62*8ca869b2SArd Biesheuvel * when the record is freed. 6376cc9580SKees Cook * 6476cc9580SKees Cook * Valid for PSTORE_TYPE_DMESG @type: 6576cc9580SKees Cook * 6676cc9580SKees Cook * @count: Oops count since boot 6776cc9580SKees Cook * @reason: kdump reason for notification 6876cc9580SKees Cook * @part: position in a multipart record 6976cc9580SKees Cook * @compressed: whether the buffer is compressed 7076cc9580SKees Cook * 719abdccccSKees Cook */ 729abdccccSKees Cook struct pstore_record { 739abdccccSKees Cook struct pstore_info *psi; 749abdccccSKees Cook enum pstore_type_id type; 759abdccccSKees Cook u64 id; 767aaa822eSKees Cook struct timespec64 time; 779abdccccSKees Cook char *buf; 789abdccccSKees Cook ssize_t size; 799abdccccSKees Cook ssize_t ecc_notice_size; 80*8ca869b2SArd Biesheuvel void *priv; 8176cc9580SKees Cook 8276cc9580SKees Cook int count; 8376cc9580SKees Cook enum kmsg_dump_reason reason; 8476cc9580SKees Cook unsigned int part; 8576cc9580SKees Cook bool compressed; 869abdccccSKees Cook }; 8767a101f5SAnton Vorontsov 880edae0b3SKees Cook /** 890edae0b3SKees Cook * struct pstore_info - backend pstore driver structure 900edae0b3SKees Cook * 910eed84ffSKees Cook * @owner: module which is responsible for this backend driver 920edae0b3SKees Cook * @name: name of the backend driver 930edae0b3SKees Cook * 948126b1c7SJann Horn * @buf_lock: spinlock to serialize access to @buf 950edae0b3SKees Cook * @buf: preallocated crash dump buffer 9689d328f6SKees Cook * @bufsize: size of @buf available for crash dump bytes (must match 9789d328f6SKees Cook * smallest number of bytes available for writing to a 9889d328f6SKees Cook * backend entry, since compressed bytes don't take kindly 9989d328f6SKees Cook * to being truncated) 1000edae0b3SKees Cook * 1010edae0b3SKees Cook * @read_mutex: serializes @open, @read, @close, and @erase callbacks 1020edae0b3SKees Cook * @flags: bitfield of frontends the backend can accept writes for 1033524e688SPavel Tatashin * @max_reason: Used when PSTORE_FLAGS_DMESG is set. Contains the 1043524e688SPavel Tatashin * kmsg_dump_reason enum value. KMSG_DUMP_UNDEF means 1053524e688SPavel Tatashin * "use existing kmsg_dump() filtering, based on the 1063524e688SPavel Tatashin * printk.always_kmsg_dump boot param" (which is either 1073524e688SPavel Tatashin * KMSG_DUMP_OOPS when false, or KMSG_DUMP_MAX when 1083524e688SPavel Tatashin * true); see printk.always_kmsg_dump for more details. 1090edae0b3SKees Cook * @data: backend-private pointer passed back during callbacks 1100edae0b3SKees Cook * 1110edae0b3SKees Cook * Callbacks: 1120edae0b3SKees Cook * 1130edae0b3SKees Cook * @open: 1140edae0b3SKees Cook * Notify backend that pstore is starting a full read of backend 1150edae0b3SKees Cook * records. Followed by one or more @read calls, and a final @close. 1160edae0b3SKees Cook * 1170edae0b3SKees Cook * @psi: in: pointer to the struct pstore_info for the backend 1180edae0b3SKees Cook * 1190edae0b3SKees Cook * Returns 0 on success, and non-zero on error. 1200edae0b3SKees Cook * 1210edae0b3SKees Cook * @close: 1220edae0b3SKees Cook * Notify backend that pstore has finished a full read of backend 1230edae0b3SKees Cook * records. Always preceded by an @open call and one or more @read 1240edae0b3SKees Cook * calls. 1250edae0b3SKees Cook * 1260edae0b3SKees Cook * @psi: in: pointer to the struct pstore_info for the backend 1270edae0b3SKees Cook * 1280edae0b3SKees Cook * Returns 0 on success, and non-zero on error. (Though pstore will 1290edae0b3SKees Cook * ignore the error.) 1300edae0b3SKees Cook * 1310edae0b3SKees Cook * @read: 1320edae0b3SKees Cook * Read next available backend record. Called after a successful 1330edae0b3SKees Cook * @open. 1340edae0b3SKees Cook * 135125cc42bSKees Cook * @record: 136125cc42bSKees Cook * pointer to record to populate. @buf should be allocated 137125cc42bSKees Cook * by the backend and filled. At least @type and @id should 138125cc42bSKees Cook * be populated, since these are used when creating pstorefs 139125cc42bSKees Cook * file names. 1400edae0b3SKees Cook * 1410edae0b3SKees Cook * Returns record size on success, zero when no more records are 1420edae0b3SKees Cook * available, or negative on error. 1430edae0b3SKees Cook * 1440edae0b3SKees Cook * @write: 1454c9ec219SKees Cook * A newly generated record needs to be written to backend storage. 1460edae0b3SKees Cook * 14776cc9580SKees Cook * @record: 1484c9ec219SKees Cook * pointer to record metadata. When @type is PSTORE_TYPE_DMESG, 1494c9ec219SKees Cook * @buf will be pointing to the preallocated @psi.buf, since 1504c9ec219SKees Cook * memory allocation may be broken during an Oops. Regardless, 1514c9ec219SKees Cook * @buf must be proccesed or copied before returning. The 1524c9ec219SKees Cook * backend is also expected to write @id with something that 153c7f3c595SKees Cook * can help identify this record to a future @erase callback. 154c7f3c595SKees Cook * The @time field will be prepopulated with the current time, 155c7f3c595SKees Cook * when available. The @size field will have the size of data 156c7f3c595SKees Cook * in @buf. 1570edae0b3SKees Cook * 1580edae0b3SKees Cook * Returns 0 on success, and non-zero on error. 1590edae0b3SKees Cook * 1604c9ec219SKees Cook * @write_user: 1610edae0b3SKees Cook * Perform a frontend write to a backend record, using a specified 162fdd03118SKees Cook * buffer that is coming directly from userspace, instead of the 163fdd03118SKees Cook * @record @buf. 1640edae0b3SKees Cook * 165fdd03118SKees Cook * @record: pointer to record metadata. 166fdd03118SKees Cook * @buf: pointer to userspace contents to write to backend 1670edae0b3SKees Cook * 1680edae0b3SKees Cook * Returns 0 on success, and non-zero on error. 1690edae0b3SKees Cook * 1700edae0b3SKees Cook * @erase: 1710edae0b3SKees Cook * Delete a record from backend storage. Different backends 172a61072aaSKees Cook * identify records differently, so entire original record is 173a61072aaSKees Cook * passed back to assist in identification of what the backend 174a61072aaSKees Cook * should remove from storage. 1750edae0b3SKees Cook * 176a61072aaSKees Cook * @record: pointer to record metadata. 1770edae0b3SKees Cook * 1780edae0b3SKees Cook * Returns 0 on success, and non-zero on error. 1790edae0b3SKees Cook * 1800edae0b3SKees Cook */ 181ca01d6ddSTony Luck struct pstore_info { 182ca01d6ddSTony Luck struct module *owner; 183563ca40dSKees Cook const char *name; 1840edae0b3SKees Cook 1858126b1c7SJann Horn spinlock_t buf_lock; 186ca01d6ddSTony Luck char *buf; 187ca01d6ddSTony Luck size_t bufsize; 1880edae0b3SKees Cook 1890edae0b3SKees Cook struct mutex read_mutex; 1900edae0b3SKees Cook 191df36ac1bSLuck, Tony int flags; 1923524e688SPavel Tatashin int max_reason; 1930edae0b3SKees Cook void *data; 1940edae0b3SKees Cook 19506cf91b4SChen Gong int (*open)(struct pstore_info *psi); 19606cf91b4SChen Gong int (*close)(struct pstore_info *psi); 197125cc42bSKees Cook ssize_t (*read)(struct pstore_record *record); 19876cc9580SKees Cook int (*write)(struct pstore_record *record); 1994c9ec219SKees Cook int (*write_user)(struct pstore_record *record, 200fdd03118SKees Cook const char __user *buf); 201a61072aaSKees Cook int (*erase)(struct pstore_record *record); 202ca01d6ddSTony Luck }; 203ca01d6ddSTony Luck 2040edae0b3SKees Cook /* Supported frontends */ 2054af62a64SKees Cook #define PSTORE_FLAGS_DMESG BIT(0) 2064af62a64SKees Cook #define PSTORE_FLAGS_CONSOLE BIT(1) 2074af62a64SKees Cook #define PSTORE_FLAGS_FTRACE BIT(2) 2084af62a64SKees Cook #define PSTORE_FLAGS_PMSG BIT(3) 209c950fd6fSNamhyung Kim 210ca01d6ddSTony Luck extern int pstore_register(struct pstore_info *); 211ee1d2674SGeliang Tang extern void pstore_unregister(struct pstore_info *); 212ca01d6ddSTony Luck 213fbccdeb8SJoel Fernandes struct pstore_ftrace_record { 214fbccdeb8SJoel Fernandes unsigned long ip; 215fbccdeb8SJoel Fernandes unsigned long parent_ip; 216fbccdeb8SJoel Fernandes u64 ts; 217fbccdeb8SJoel Fernandes }; 218fbccdeb8SJoel Fernandes 219fbccdeb8SJoel Fernandes /* 220fbccdeb8SJoel Fernandes * ftrace related stuff: Both backends and frontends need these so expose 221fbccdeb8SJoel Fernandes * them here. 222fbccdeb8SJoel Fernandes */ 223fbccdeb8SJoel Fernandes 224fbccdeb8SJoel Fernandes #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB) 225fbccdeb8SJoel Fernandes #define PSTORE_CPU_IN_IP 0x1 226fbccdeb8SJoel Fernandes #elif NR_CPUS <= 4 && defined(CONFIG_ARM) 227fbccdeb8SJoel Fernandes #define PSTORE_CPU_IN_IP 0x3 228fbccdeb8SJoel Fernandes #endif 229fbccdeb8SJoel Fernandes 230fbccdeb8SJoel Fernandes #define TS_CPU_SHIFT 8 231fbccdeb8SJoel Fernandes #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1) 232fbccdeb8SJoel Fernandes 233fbccdeb8SJoel Fernandes /* 234fbccdeb8SJoel Fernandes * If CPU number can be stored in IP, store it there, otherwise store it in 235fbccdeb8SJoel Fernandes * the time stamp. This means more timestamp resolution is available when 236fbccdeb8SJoel Fernandes * the CPU can be stored in the IP. 237fbccdeb8SJoel Fernandes */ 238fbccdeb8SJoel Fernandes #ifdef PSTORE_CPU_IN_IP 239fbccdeb8SJoel Fernandes static inline void 240fbccdeb8SJoel Fernandes pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu) 241fbccdeb8SJoel Fernandes { 242fbccdeb8SJoel Fernandes rec->ip |= cpu; 243fbccdeb8SJoel Fernandes } 244fbccdeb8SJoel Fernandes 245fbccdeb8SJoel Fernandes static inline unsigned int 246fbccdeb8SJoel Fernandes pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec) 247fbccdeb8SJoel Fernandes { 248fbccdeb8SJoel Fernandes return rec->ip & PSTORE_CPU_IN_IP; 249fbccdeb8SJoel Fernandes } 250fbccdeb8SJoel Fernandes 251fbccdeb8SJoel Fernandes static inline u64 252fbccdeb8SJoel Fernandes pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec) 253fbccdeb8SJoel Fernandes { 254fbccdeb8SJoel Fernandes return rec->ts; 255fbccdeb8SJoel Fernandes } 256fbccdeb8SJoel Fernandes 257fbccdeb8SJoel Fernandes static inline void 258fbccdeb8SJoel Fernandes pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val) 259fbccdeb8SJoel Fernandes { 260fbccdeb8SJoel Fernandes rec->ts = val; 261fbccdeb8SJoel Fernandes } 262fbccdeb8SJoel Fernandes #else 263fbccdeb8SJoel Fernandes static inline void 264fbccdeb8SJoel Fernandes pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu) 265fbccdeb8SJoel Fernandes { 266fbccdeb8SJoel Fernandes rec->ts &= ~(TS_CPU_MASK); 267fbccdeb8SJoel Fernandes rec->ts |= cpu; 268fbccdeb8SJoel Fernandes } 269fbccdeb8SJoel Fernandes 270fbccdeb8SJoel Fernandes static inline unsigned int 271fbccdeb8SJoel Fernandes pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec) 272fbccdeb8SJoel Fernandes { 273fbccdeb8SJoel Fernandes return rec->ts & TS_CPU_MASK; 274fbccdeb8SJoel Fernandes } 275fbccdeb8SJoel Fernandes 276fbccdeb8SJoel Fernandes static inline u64 277fbccdeb8SJoel Fernandes pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec) 278fbccdeb8SJoel Fernandes { 279fbccdeb8SJoel Fernandes return rec->ts >> TS_CPU_SHIFT; 280fbccdeb8SJoel Fernandes } 281fbccdeb8SJoel Fernandes 282fbccdeb8SJoel Fernandes static inline void 283fbccdeb8SJoel Fernandes pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val) 284fbccdeb8SJoel Fernandes { 285fbccdeb8SJoel Fernandes rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT); 286fbccdeb8SJoel Fernandes } 287fbccdeb8SJoel Fernandes #endif 288fbccdeb8SJoel Fernandes 289ca01d6ddSTony Luck #endif /*_LINUX_PSTORE_H*/ 290