Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 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 | |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 25 | #include <linux/compiler.h> |
| 26 | #include <linux/errno.h> |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 27 | #include <linux/kmsg_dump.h> |
Anton Vorontsov | 67a101f | 2012-07-17 11:37:07 -0700 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Anton Vorontsov | 67a101f | 2012-07-17 11:37:07 -0700 | [diff] [blame] | 29 | #include <linux/spinlock.h> |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 30 | #include <linux/time.h> |
| 31 | #include <linux/types.h> |
Kees Cook | 3d6d8d2 | 2011-11-17 13:13:29 -0800 | [diff] [blame] | 32 | |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 33 | struct module; |
| 34 | |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 35 | /* pstore record types (see fs/pstore/inode.c for filename templates) */ |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 36 | enum pstore_type_id { |
| 37 | PSTORE_TYPE_DMESG = 0, |
| 38 | PSTORE_TYPE_MCE = 1, |
Anton Vorontsov | f29e595 | 2012-05-26 06:20:19 -0700 | [diff] [blame] | 39 | PSTORE_TYPE_CONSOLE = 2, |
Anton Vorontsov | 060287b | 2012-07-09 17:10:41 -0700 | [diff] [blame] | 40 | PSTORE_TYPE_FTRACE = 3, |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 41 | /* PPC64 partition types */ |
| 42 | PSTORE_TYPE_PPC_RTAS = 4, |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 43 | PSTORE_TYPE_PPC_OF = 5, |
Aruna Balakrishnaiah | a5e4797 | 2013-06-06 00:22:20 +0530 | [diff] [blame] | 44 | PSTORE_TYPE_PPC_COMMON = 6, |
Mark Salyzyn | 9d5438f | 2015-01-16 16:01:10 -0800 | [diff] [blame] | 45 | PSTORE_TYPE_PMSG = 7, |
Hari Bathini | ae011d2 | 2015-02-06 01:06:28 +0530 | [diff] [blame] | 46 | PSTORE_TYPE_PPC_OPAL = 8, |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 47 | PSTORE_TYPE_UNKNOWN = 255 |
| 48 | }; |
| 49 | |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 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 |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 57 | * @buf: pointer to record contents |
| 58 | * @size: size of @buf |
| 59 | * @ecc_notice_size: |
| 60 | * ECC information for @buf |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 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 | * |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 69 | */ |
| 70 | struct pstore_record { |
| 71 | struct pstore_info *psi; |
| 72 | enum pstore_type_id type; |
| 73 | u64 id; |
| 74 | struct timespec time; |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 75 | char *buf; |
| 76 | ssize_t size; |
| 77 | ssize_t ecc_notice_size; |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 78 | |
| 79 | int count; |
| 80 | enum kmsg_dump_reason reason; |
| 81 | unsigned int part; |
| 82 | bool compressed; |
Kees Cook | 9abdccc | 2017-03-03 16:59:29 -0800 | [diff] [blame] | 83 | }; |
Anton Vorontsov | 67a101f | 2012-07-17 11:37:07 -0700 | [diff] [blame] | 84 | |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 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: spinlock to serialize access to @buf |
| 92 | * @buf: preallocated crash dump buffer |
| 93 | * @bufsize: size of @buf available for crash dump writes |
| 94 | * |
| 95 | * @read_mutex: serializes @open, @read, @close, and @erase callbacks |
| 96 | * @flags: bitfield of frontends the backend can accept writes for |
| 97 | * @data: backend-private pointer passed back during callbacks |
| 98 | * |
| 99 | * Callbacks: |
| 100 | * |
| 101 | * @open: |
| 102 | * Notify backend that pstore is starting a full read of backend |
| 103 | * records. Followed by one or more @read calls, and a final @close. |
| 104 | * |
| 105 | * @psi: in: pointer to the struct pstore_info for the backend |
| 106 | * |
| 107 | * Returns 0 on success, and non-zero on error. |
| 108 | * |
| 109 | * @close: |
| 110 | * Notify backend that pstore has finished a full read of backend |
| 111 | * records. Always preceded by an @open call and one or more @read |
| 112 | * calls. |
| 113 | * |
| 114 | * @psi: in: pointer to the struct pstore_info for the backend |
| 115 | * |
| 116 | * Returns 0 on success, and non-zero on error. (Though pstore will |
| 117 | * ignore the error.) |
| 118 | * |
| 119 | * @read: |
| 120 | * Read next available backend record. Called after a successful |
| 121 | * @open. |
| 122 | * |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 123 | * @record: |
| 124 | * pointer to record to populate. @buf should be allocated |
| 125 | * by the backend and filled. At least @type and @id should |
| 126 | * be populated, since these are used when creating pstorefs |
| 127 | * file names. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 128 | * |
| 129 | * Returns record size on success, zero when no more records are |
| 130 | * available, or negative on error. |
| 131 | * |
| 132 | * @write: |
| 133 | * Perform a frontend notification of a write to a backend record. The |
| 134 | * data to be stored has already been written to the registered @buf |
| 135 | * of the @psi structure. |
| 136 | * |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 137 | * @record: |
| 138 | * pointer to record metadata. Note that @buf is NULL, since |
| 139 | * the @buf registered with @psi is what has been written. The |
| 140 | * backend is expected to update @id. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 141 | * |
| 142 | * Returns 0 on success, and non-zero on error. |
| 143 | * |
| 144 | * @write_buf: |
| 145 | * Perform a frontend write to a backend record, using a specified |
| 146 | * buffer. Unlike @write, this does not use the @psi @buf. |
| 147 | * |
| 148 | * @type: in: pstore record type to write |
| 149 | * @reason: |
| 150 | * in: pstore write reason |
| 151 | * @id: out: unique identifier for the record |
| 152 | * @part: in: position in a multipart write |
| 153 | * @buf: in: pointer to contents to write to backend record |
| 154 | * @compressed: |
| 155 | * in: if the record is compressed |
| 156 | * @size: in: size of the write |
| 157 | * @psi: in: pointer to the struct pstore_info for the backend |
| 158 | * |
| 159 | * Returns 0 on success, and non-zero on error. |
| 160 | * |
| 161 | * @write_buf_user: |
| 162 | * Perform a frontend write to a backend record, using a specified |
| 163 | * buffer that is coming directly from userspace. |
| 164 | * |
| 165 | * @type: in: pstore record type to write |
| 166 | * @reason: |
| 167 | * in: pstore write reason |
| 168 | * @id: out: unique identifier for the record |
| 169 | * @part: in: position in a multipart write |
| 170 | * @buf: in: pointer to userspace contents to write to backend record |
| 171 | * @compressed: |
| 172 | * in: if the record is compressed |
| 173 | * @size: in: size of the write |
| 174 | * @psi: in: pointer to the struct pstore_info for the backend |
| 175 | * |
| 176 | * Returns 0 on success, and non-zero on error. |
| 177 | * |
| 178 | * @erase: |
| 179 | * Delete a record from backend storage. Different backends |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame^] | 180 | * identify records differently, so entire original record is |
| 181 | * passed back to assist in identification of what the backend |
| 182 | * should remove from storage. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 183 | * |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame^] | 184 | * @record: pointer to record metadata. |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 185 | * |
| 186 | * Returns 0 on success, and non-zero on error. |
| 187 | * |
| 188 | */ |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 189 | struct pstore_info { |
| 190 | struct module *owner; |
| 191 | char *name; |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 192 | |
| 193 | spinlock_t buf_lock; |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 194 | char *buf; |
| 195 | size_t bufsize; |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 196 | |
| 197 | struct mutex read_mutex; |
| 198 | |
Luck, Tony | df36ac1 | 2013-12-18 15:17:10 -0800 | [diff] [blame] | 199 | int flags; |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 200 | void *data; |
| 201 | |
Chen Gong | 06cf91b | 2011-05-16 11:00:27 -0700 | [diff] [blame] | 202 | int (*open)(struct pstore_info *psi); |
| 203 | int (*close)(struct pstore_info *psi); |
Kees Cook | 125cc42 | 2017-03-03 22:09:18 -0800 | [diff] [blame] | 204 | ssize_t (*read)(struct pstore_record *record); |
Kees Cook | 76cc958 | 2017-03-03 23:28:53 -0800 | [diff] [blame] | 205 | int (*write)(struct pstore_record *record); |
Anton Vorontsov | 897dba0 | 2012-07-09 17:10:40 -0700 | [diff] [blame] | 206 | int (*write_buf)(enum pstore_type_id type, |
| 207 | enum kmsg_dump_reason reason, u64 *id, |
Aruna Balakrishnaiah | b3b515b | 2013-08-16 13:52:47 -0700 | [diff] [blame] | 208 | unsigned int part, const char *buf, bool compressed, |
Aruna Balakrishnaiah | 6bbbca7 | 2013-06-27 14:02:56 +0530 | [diff] [blame] | 209 | size_t size, struct pstore_info *psi); |
Mark Salyzyn | 5bf6d1b | 2016-09-01 08:13:46 -0700 | [diff] [blame] | 210 | int (*write_buf_user)(enum pstore_type_id type, |
| 211 | enum kmsg_dump_reason reason, u64 *id, |
| 212 | unsigned int part, const char __user *buf, |
| 213 | bool compressed, size_t size, struct pstore_info *psi); |
Kees Cook | a61072a | 2017-03-04 23:31:19 -0800 | [diff] [blame^] | 214 | int (*erase)(struct pstore_record *record); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 215 | }; |
| 216 | |
Kees Cook | 0edae0b | 2017-03-03 12:16:16 -0800 | [diff] [blame] | 217 | /* Supported frontends */ |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 218 | #define PSTORE_FLAGS_DMESG (1 << 0) |
Namhyung Kim | c950fd6 | 2016-07-28 00:08:25 +0900 | [diff] [blame] | 219 | #define PSTORE_FLAGS_CONSOLE (1 << 1) |
| 220 | #define PSTORE_FLAGS_FTRACE (1 << 2) |
| 221 | #define PSTORE_FLAGS_PMSG (1 << 3) |
| 222 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 223 | extern int pstore_register(struct pstore_info *); |
Geliang Tang | ee1d267 | 2015-10-20 00:39:03 -0700 | [diff] [blame] | 224 | extern void pstore_unregister(struct pstore_info *); |
Seiji Aguchi | 9f244e9 | 2013-01-11 18:09:41 +0000 | [diff] [blame] | 225 | extern bool pstore_cannot_block_path(enum kmsg_dump_reason reason); |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 226 | |
Joel Fernandes | fbccdeb | 2016-10-20 00:34:05 -0700 | [diff] [blame] | 227 | struct pstore_ftrace_record { |
| 228 | unsigned long ip; |
| 229 | unsigned long parent_ip; |
| 230 | u64 ts; |
| 231 | }; |
| 232 | |
| 233 | /* |
| 234 | * ftrace related stuff: Both backends and frontends need these so expose |
| 235 | * them here. |
| 236 | */ |
| 237 | |
| 238 | #if NR_CPUS <= 2 && defined(CONFIG_ARM_THUMB) |
| 239 | #define PSTORE_CPU_IN_IP 0x1 |
| 240 | #elif NR_CPUS <= 4 && defined(CONFIG_ARM) |
| 241 | #define PSTORE_CPU_IN_IP 0x3 |
| 242 | #endif |
| 243 | |
| 244 | #define TS_CPU_SHIFT 8 |
| 245 | #define TS_CPU_MASK (BIT(TS_CPU_SHIFT) - 1) |
| 246 | |
| 247 | /* |
| 248 | * If CPU number can be stored in IP, store it there, otherwise store it in |
| 249 | * the time stamp. This means more timestamp resolution is available when |
| 250 | * the CPU can be stored in the IP. |
| 251 | */ |
| 252 | #ifdef PSTORE_CPU_IN_IP |
| 253 | static inline void |
| 254 | pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu) |
| 255 | { |
| 256 | rec->ip |= cpu; |
| 257 | } |
| 258 | |
| 259 | static inline unsigned int |
| 260 | pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec) |
| 261 | { |
| 262 | return rec->ip & PSTORE_CPU_IN_IP; |
| 263 | } |
| 264 | |
| 265 | static inline u64 |
| 266 | pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec) |
| 267 | { |
| 268 | return rec->ts; |
| 269 | } |
| 270 | |
| 271 | static inline void |
| 272 | pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val) |
| 273 | { |
| 274 | rec->ts = val; |
| 275 | } |
| 276 | #else |
| 277 | static inline void |
| 278 | pstore_ftrace_encode_cpu(struct pstore_ftrace_record *rec, unsigned int cpu) |
| 279 | { |
| 280 | rec->ts &= ~(TS_CPU_MASK); |
| 281 | rec->ts |= cpu; |
| 282 | } |
| 283 | |
| 284 | static inline unsigned int |
| 285 | pstore_ftrace_decode_cpu(struct pstore_ftrace_record *rec) |
| 286 | { |
| 287 | return rec->ts & TS_CPU_MASK; |
| 288 | } |
| 289 | |
| 290 | static inline u64 |
| 291 | pstore_ftrace_read_timestamp(struct pstore_ftrace_record *rec) |
| 292 | { |
| 293 | return rec->ts >> TS_CPU_SHIFT; |
| 294 | } |
| 295 | |
| 296 | static inline void |
| 297 | pstore_ftrace_write_timestamp(struct pstore_ftrace_record *rec, u64 val) |
| 298 | { |
| 299 | rec->ts = (rec->ts & TS_CPU_MASK) | (val << TS_CPU_SHIFT); |
| 300 | } |
| 301 | #endif |
| 302 | |
Tony Luck | ca01d6d | 2010-12-28 14:25:21 -0800 | [diff] [blame] | 303 | #endif /*_LINUX_PSTORE_H*/ |