Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * c 2001 PPC 64 Team, IBM Corp |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * /dev/nvram driver for PPC64 |
| 10 | * |
| 11 | * This perhaps should live in drivers/char |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | #include <linux/kmsg_dump.h> |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 21 | #include <linux/pstore.h> |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 22 | #include <linux/ctype.h> |
| 23 | #include <linux/zlib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/uaccess.h> |
| 25 | #include <asm/nvram.h> |
| 26 | #include <asm/rtas.h> |
| 27 | #include <asm/prom.h> |
| 28 | #include <asm/machdep.h> |
| 29 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 30 | /* Max bytes to read/write in one go */ |
| 31 | #define NVRW_CNT 0x20 |
| 32 | |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 33 | /* |
| 34 | * Set oops header version to distingush between old and new format header. |
| 35 | * lnx,oops-log partition max size is 4000, header version > 4000 will |
| 36 | * help in identifying new header. |
| 37 | */ |
| 38 | #define OOPS_HDR_VERSION 5000 |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static unsigned int nvram_size; |
| 41 | static int nvram_fetch, nvram_store; |
| 42 | static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */ |
| 43 | static DEFINE_SPINLOCK(nvram_lock); |
| 44 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 45 | struct err_log_info { |
| 46 | int error_type; |
| 47 | unsigned int seq_num; |
| 48 | }; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 49 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 50 | struct nvram_os_partition { |
| 51 | const char *name; |
| 52 | int req_size; /* desired size, in bytes */ |
| 53 | int min_size; /* minimum acceptable size (0 means req_size) */ |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 54 | long size; /* size of data portion (excluding err_log_info) */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 55 | long index; /* offset of data portion of partition */ |
Aruna Balakrishnaiah | edf3846 | 2013-06-06 00:21:59 +0530 | [diff] [blame] | 56 | bool os_partition; /* partition initialized by OS, not FW */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static struct nvram_os_partition rtas_log_partition = { |
| 60 | .name = "ibm,rtas-log", |
| 61 | .req_size = 2079, |
| 62 | .min_size = 1055, |
Aruna Balakrishnaiah | edf3846 | 2013-06-06 00:21:59 +0530 | [diff] [blame] | 63 | .index = -1, |
| 64 | .os_partition = true |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 67 | static struct nvram_os_partition oops_log_partition = { |
| 68 | .name = "lnx,oops-log", |
| 69 | .req_size = 4000, |
| 70 | .min_size = 2000, |
Aruna Balakrishnaiah | edf3846 | 2013-06-06 00:21:59 +0530 | [diff] [blame] | 71 | .index = -1, |
| 72 | .os_partition = true |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 75 | static const char *pseries_nvram_os_partitions[] = { |
| 76 | "ibm,rtas-log", |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 77 | "lnx,oops-log", |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 78 | NULL |
| 79 | }; |
Benjamin Herrenschmidt | 9a866b8 | 2010-08-02 10:51:25 +1000 | [diff] [blame] | 80 | |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 81 | struct oops_log_info { |
| 82 | u16 version; |
| 83 | u16 report_length; |
| 84 | u64 timestamp; |
| 85 | } __attribute__((packed)); |
| 86 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 87 | static void oops_to_nvram(struct kmsg_dumper *dumper, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 88 | enum kmsg_dump_reason reason); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 89 | |
| 90 | static struct kmsg_dumper nvram_kmsg_dumper = { |
| 91 | .dump = oops_to_nvram |
| 92 | }; |
| 93 | |
| 94 | /* See clobbering_unread_rtas_event() */ |
| 95 | #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */ |
| 96 | static unsigned long last_unread_rtas_event; /* timestamp */ |
| 97 | |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 98 | /* |
| 99 | * For capturing and compressing an oops or panic report... |
| 100 | |
| 101 | * big_oops_buf[] holds the uncompressed text we're capturing. |
| 102 | * |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 103 | * oops_buf[] holds the compressed text, preceded by a oops header. |
| 104 | * oops header has u16 holding the version of oops header (to differentiate |
| 105 | * between old and new format header) followed by u16 holding the length of |
| 106 | * the compressed* text (*Or uncompressed, if compression fails.) and u64 |
| 107 | * holding the timestamp. oops_buf[] gets written to NVRAM. |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 108 | * |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 109 | * oops_log_info points to the header. oops_data points to the compressed text. |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 110 | * |
| 111 | * +- oops_buf |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 112 | * | +- oops_data |
| 113 | * v v |
| 114 | * +-----------+-----------+-----------+------------------------+ |
| 115 | * | version | length | timestamp | text | |
| 116 | * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) | |
| 117 | * +-----------+-----------+-----------+------------------------+ |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 118 | * ^ |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 119 | * +- oops_log_info |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 120 | * |
| 121 | * We preallocate these buffers during init to avoid kmalloc during oops/panic. |
| 122 | */ |
| 123 | static size_t big_oops_buf_sz; |
| 124 | static char *big_oops_buf, *oops_buf; |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 125 | static char *oops_data; |
| 126 | static size_t oops_data_sz; |
| 127 | |
| 128 | /* Compression parameters */ |
| 129 | #define COMPR_LEVEL 6 |
| 130 | #define WINDOW_BITS 12 |
| 131 | #define MEM_LEVEL 4 |
| 132 | static struct z_stream_s stream; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 133 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 134 | #ifdef CONFIG_PSTORE |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 135 | static struct nvram_os_partition of_config_partition = { |
| 136 | .name = "of-config", |
| 137 | .index = -1, |
| 138 | .os_partition = false |
| 139 | }; |
| 140 | |
Aruna Balakrishnaiah | a5e4797 | 2013-06-06 00:22:20 +0530 | [diff] [blame] | 141 | static struct nvram_os_partition common_partition = { |
| 142 | .name = "common", |
| 143 | .index = -1, |
| 144 | .os_partition = false |
| 145 | }; |
| 146 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 147 | static enum pstore_type_id nvram_type_ids[] = { |
| 148 | PSTORE_TYPE_DMESG, |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 149 | PSTORE_TYPE_PPC_RTAS, |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 150 | PSTORE_TYPE_PPC_OF, |
Aruna Balakrishnaiah | a5e4797 | 2013-06-06 00:22:20 +0530 | [diff] [blame] | 151 | PSTORE_TYPE_PPC_COMMON, |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 152 | -1 |
| 153 | }; |
| 154 | static int read_type; |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 155 | static unsigned long last_rtas_event; |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 156 | #endif |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index) |
| 159 | { |
| 160 | unsigned int i; |
| 161 | unsigned long len; |
| 162 | int done; |
| 163 | unsigned long flags; |
| 164 | char *p = buf; |
| 165 | |
| 166 | |
| 167 | if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE) |
| 168 | return -ENODEV; |
| 169 | |
| 170 | if (*index >= nvram_size) |
| 171 | return 0; |
| 172 | |
| 173 | i = *index; |
| 174 | if (i + count > nvram_size) |
| 175 | count = nvram_size - i; |
| 176 | |
| 177 | spin_lock_irqsave(&nvram_lock, flags); |
| 178 | |
| 179 | for (; count != 0; count -= len) { |
| 180 | len = count; |
| 181 | if (len > NVRW_CNT) |
| 182 | len = NVRW_CNT; |
| 183 | |
| 184 | if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf), |
| 185 | len) != 0) || len != done) { |
| 186 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 187 | return -EIO; |
| 188 | } |
| 189 | |
| 190 | memcpy(p, nvram_buf, len); |
| 191 | |
| 192 | p += len; |
| 193 | i += len; |
| 194 | } |
| 195 | |
| 196 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 197 | |
| 198 | *index = i; |
| 199 | return p - buf; |
| 200 | } |
| 201 | |
| 202 | static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index) |
| 203 | { |
| 204 | unsigned int i; |
| 205 | unsigned long len; |
| 206 | int done; |
| 207 | unsigned long flags; |
| 208 | const char *p = buf; |
| 209 | |
| 210 | if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE) |
| 211 | return -ENODEV; |
| 212 | |
| 213 | if (*index >= nvram_size) |
| 214 | return 0; |
| 215 | |
| 216 | i = *index; |
| 217 | if (i + count > nvram_size) |
| 218 | count = nvram_size - i; |
| 219 | |
| 220 | spin_lock_irqsave(&nvram_lock, flags); |
| 221 | |
| 222 | for (; count != 0; count -= len) { |
| 223 | len = count; |
| 224 | if (len > NVRW_CNT) |
| 225 | len = NVRW_CNT; |
| 226 | |
| 227 | memcpy(nvram_buf, p, len); |
| 228 | |
| 229 | if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf), |
| 230 | len) != 0) || len != done) { |
| 231 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 232 | return -EIO; |
| 233 | } |
| 234 | |
| 235 | p += len; |
| 236 | i += len; |
| 237 | } |
| 238 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 239 | |
| 240 | *index = i; |
| 241 | return p - buf; |
| 242 | } |
| 243 | |
| 244 | static ssize_t pSeries_nvram_get_size(void) |
| 245 | { |
| 246 | return nvram_size ? nvram_size : -ENODEV; |
| 247 | } |
| 248 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 249 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 250 | /* nvram_write_os_partition, nvram_write_error_log |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 251 | * |
| 252 | * We need to buffer the error logs into nvram to ensure that we have |
| 253 | * the failure information to decode. If we have a severe error there |
| 254 | * is no way to guarantee that the OS or the machine is in a state to |
| 255 | * get back to user land and write the error to disk. For example if |
| 256 | * the SCSI device driver causes a Machine Check by writing to a bad |
| 257 | * IO address, there is no way of guaranteeing that the device driver |
| 258 | * is in any state that is would also be able to write the error data |
| 259 | * captured to disk, thus we buffer it in NVRAM for analysis on the |
| 260 | * next boot. |
| 261 | * |
| 262 | * In NVRAM the partition containing the error log buffer will looks like: |
| 263 | * Header (in bytes): |
| 264 | * +-----------+----------+--------+------------+------------------+ |
| 265 | * | signature | checksum | length | name | data | |
| 266 | * |0 |1 |2 3|4 15|16 length-1| |
| 267 | * +-----------+----------+--------+------------+------------------+ |
| 268 | * |
| 269 | * The 'data' section would look like (in bytes): |
| 270 | * +--------------+------------+-----------------------------------+ |
| 271 | * | event_logged | sequence # | error log | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 272 | * |0 3|4 7|8 error_log_size-1| |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 273 | * +--------------+------------+-----------------------------------+ |
| 274 | * |
| 275 | * event_logged: 0 if event has not been logged to syslog, 1 if it has |
| 276 | * sequence #: The unique sequence # for each event. (until it wraps) |
| 277 | * error log: The error log from event_scan |
| 278 | */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 279 | int nvram_write_os_partition(struct nvram_os_partition *part, char * buff, |
| 280 | int length, unsigned int err_type, unsigned int error_log_cnt) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 281 | { |
| 282 | int rc; |
| 283 | loff_t tmp_index; |
| 284 | struct err_log_info info; |
| 285 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 286 | if (part->index == -1) { |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 287 | return -ESPIPE; |
| 288 | } |
| 289 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 290 | if (length > part->size) { |
| 291 | length = part->size; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | info.error_type = err_type; |
| 295 | info.seq_num = error_log_cnt; |
| 296 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 297 | tmp_index = part->index; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 298 | |
| 299 | rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index); |
| 300 | if (rc <= 0) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 301 | pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | rc = ppc_md.nvram_write(buff, length, &tmp_index); |
| 306 | if (rc <= 0) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 307 | pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 308 | return rc; |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 314 | int nvram_write_error_log(char * buff, int length, |
| 315 | unsigned int err_type, unsigned int error_log_cnt) |
| 316 | { |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 317 | int rc = nvram_write_os_partition(&rtas_log_partition, buff, length, |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 318 | err_type, error_log_cnt); |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 319 | if (!rc) { |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 320 | last_unread_rtas_event = get_seconds(); |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 321 | #ifdef CONFIG_PSTORE |
| 322 | last_rtas_event = get_seconds(); |
| 323 | #endif |
| 324 | } |
| 325 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 326 | return rc; |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 329 | /* nvram_read_partition |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 330 | * |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 331 | * Reads nvram partition for at most 'length' |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 332 | */ |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 333 | int nvram_read_partition(struct nvram_os_partition *part, char *buff, |
| 334 | int length, unsigned int *err_type, |
| 335 | unsigned int *error_log_cnt) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 336 | { |
| 337 | int rc; |
| 338 | loff_t tmp_index; |
| 339 | struct err_log_info info; |
| 340 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 341 | if (part->index == -1) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 342 | return -1; |
| 343 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 344 | if (length > part->size) |
| 345 | length = part->size; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 346 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 347 | tmp_index = part->index; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 348 | |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 349 | if (part->os_partition) { |
| 350 | rc = ppc_md.nvram_read((char *)&info, |
| 351 | sizeof(struct err_log_info), |
| 352 | &tmp_index); |
| 353 | if (rc <= 0) { |
| 354 | pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, |
| 355 | rc); |
| 356 | return rc; |
| 357 | } |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | rc = ppc_md.nvram_read(buff, length, &tmp_index); |
| 361 | if (rc <= 0) { |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 362 | pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 363 | return rc; |
| 364 | } |
| 365 | |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 366 | if (part->os_partition) { |
| 367 | *error_log_cnt = info.seq_num; |
| 368 | *err_type = info.error_type; |
| 369 | } |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame] | 374 | /* nvram_read_error_log |
| 375 | * |
| 376 | * Reads nvram for error log for at most 'length' |
| 377 | */ |
| 378 | int nvram_read_error_log(char *buff, int length, |
| 379 | unsigned int *err_type, unsigned int *error_log_cnt) |
| 380 | { |
| 381 | return nvram_read_partition(&rtas_log_partition, buff, length, |
| 382 | err_type, error_log_cnt); |
| 383 | } |
| 384 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 385 | /* This doesn't actually zero anything, but it sets the event_logged |
| 386 | * word to tell that this event is safely in syslog. |
| 387 | */ |
| 388 | int nvram_clear_error_log(void) |
| 389 | { |
| 390 | loff_t tmp_index; |
| 391 | int clear_word = ERR_FLAG_ALREADY_LOGGED; |
| 392 | int rc; |
| 393 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 394 | if (rtas_log_partition.index == -1) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 395 | return -1; |
| 396 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 397 | tmp_index = rtas_log_partition.index; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 398 | |
| 399 | rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index); |
| 400 | if (rc <= 0) { |
| 401 | printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); |
| 402 | return rc; |
| 403 | } |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 404 | last_unread_rtas_event = 0; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 409 | /* pseries_nvram_init_os_partition |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 410 | * |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 411 | * This sets up a partition with an "OS" signature. |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 412 | * |
| 413 | * The general strategy is the following: |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 414 | * 1.) If a partition with the indicated name already exists... |
| 415 | * - If it's large enough, use it. |
| 416 | * - Otherwise, recycle it and keep going. |
| 417 | * 2.) Search for a free partition that is large enough. |
| 418 | * 3.) If there's not a free partition large enough, recycle any obsolete |
| 419 | * OS partitions and try again. |
| 420 | * 4.) Will first try getting a chunk that will satisfy the requested size. |
| 421 | * 5.) If a chunk of the requested size cannot be allocated, then try finding |
| 422 | * a chunk that will satisfy the minum needed. |
| 423 | * |
| 424 | * Returns 0 on success, else -1. |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 425 | */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 426 | static int __init pseries_nvram_init_os_partition(struct nvram_os_partition |
| 427 | *part) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 428 | { |
| 429 | loff_t p; |
| 430 | int size; |
| 431 | |
| 432 | /* Scan nvram for partitions */ |
| 433 | nvram_scan_partitions(); |
| 434 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 435 | /* Look for ours */ |
| 436 | p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 437 | |
| 438 | /* Found one but too small, remove it */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 439 | if (p && size < part->min_size) { |
| 440 | pr_info("nvram: Found too small %s partition," |
| 441 | " removing it...\n", part->name); |
| 442 | nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 443 | p = 0; |
| 444 | } |
| 445 | |
| 446 | /* Create one if we didn't find */ |
| 447 | if (!p) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 448 | p = nvram_create_partition(part->name, NVRAM_SIG_OS, |
| 449 | part->req_size, part->min_size); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 450 | if (p == -ENOSPC) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 451 | pr_info("nvram: No room to create %s partition, " |
| 452 | "deleting any obsolete OS partitions...\n", |
| 453 | part->name); |
| 454 | nvram_remove_partition(NULL, NVRAM_SIG_OS, |
| 455 | pseries_nvram_os_partitions); |
| 456 | p = nvram_create_partition(part->name, NVRAM_SIG_OS, |
| 457 | part->req_size, part->min_size); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
| 461 | if (p <= 0) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 462 | pr_err("nvram: Failed to find or create %s" |
| 463 | " partition, err %d\n", part->name, (int)p); |
| 464 | return -1; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 465 | } |
| 466 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 467 | part->index = p; |
| 468 | part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | } |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 472 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 473 | /* |
| 474 | * Are we using the ibm,rtas-log for oops/panic reports? And if so, |
| 475 | * would logging this oops/panic overwrite an RTAS event that rtas_errd |
| 476 | * hasn't had a chance to read and process? Return 1 if so, else 0. |
| 477 | * |
| 478 | * We assume that if rtas_errd hasn't read the RTAS event in |
| 479 | * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to. |
| 480 | */ |
| 481 | static int clobbering_unread_rtas_event(void) |
| 482 | { |
| 483 | return (oops_log_partition.index == rtas_log_partition.index |
| 484 | && last_unread_rtas_event |
| 485 | && get_seconds() - last_unread_rtas_event <= |
| 486 | NVRAM_RTAS_READ_TIMEOUT); |
| 487 | } |
| 488 | |
Aruna Balakrishnaiah | fbfe86f | 2013-06-27 14:03:04 +0530 | [diff] [blame] | 489 | /* Derived from logfs_compress() */ |
| 490 | static int nvram_compress(const void *in, void *out, size_t inlen, |
| 491 | size_t outlen) |
| 492 | { |
| 493 | int err, ret; |
| 494 | |
| 495 | ret = -EIO; |
| 496 | err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, |
| 497 | MEM_LEVEL, Z_DEFAULT_STRATEGY); |
| 498 | if (err != Z_OK) |
| 499 | goto error; |
| 500 | |
| 501 | stream.next_in = in; |
| 502 | stream.avail_in = inlen; |
| 503 | stream.total_in = 0; |
| 504 | stream.next_out = out; |
| 505 | stream.avail_out = outlen; |
| 506 | stream.total_out = 0; |
| 507 | |
| 508 | err = zlib_deflate(&stream, Z_FINISH); |
| 509 | if (err != Z_STREAM_END) |
| 510 | goto error; |
| 511 | |
| 512 | err = zlib_deflateEnd(&stream); |
| 513 | if (err != Z_OK) |
| 514 | goto error; |
| 515 | |
| 516 | if (stream.total_out >= stream.total_in) |
| 517 | goto error; |
| 518 | |
| 519 | ret = stream.total_out; |
| 520 | error: |
| 521 | return ret; |
| 522 | } |
| 523 | |
| 524 | /* Compress the text from big_oops_buf into oops_buf. */ |
| 525 | static int zip_oops(size_t text_len) |
| 526 | { |
| 527 | struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; |
| 528 | int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len, |
| 529 | oops_data_sz); |
| 530 | if (zipped_len < 0) { |
| 531 | pr_err("nvram: compression failed; returned %d\n", zipped_len); |
| 532 | pr_err("nvram: logging uncompressed oops/panic report\n"); |
| 533 | return -1; |
| 534 | } |
| 535 | oops_hdr->version = OOPS_HDR_VERSION; |
| 536 | oops_hdr->report_length = (u16) zipped_len; |
| 537 | oops_hdr->timestamp = get_seconds(); |
| 538 | return 0; |
| 539 | } |
| 540 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 541 | #ifdef CONFIG_PSTORE |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 542 | /* Derived from logfs_uncompress */ |
| 543 | int nvram_decompress(void *in, void *out, size_t inlen, size_t outlen) |
| 544 | { |
| 545 | int err, ret; |
| 546 | |
| 547 | ret = -EIO; |
| 548 | err = zlib_inflateInit(&stream); |
| 549 | if (err != Z_OK) |
| 550 | goto error; |
| 551 | |
| 552 | stream.next_in = in; |
| 553 | stream.avail_in = inlen; |
| 554 | stream.total_in = 0; |
| 555 | stream.next_out = out; |
| 556 | stream.avail_out = outlen; |
| 557 | stream.total_out = 0; |
| 558 | |
| 559 | err = zlib_inflate(&stream, Z_FINISH); |
| 560 | if (err != Z_STREAM_END) |
| 561 | goto error; |
| 562 | |
| 563 | err = zlib_inflateEnd(&stream); |
| 564 | if (err != Z_OK) |
| 565 | goto error; |
| 566 | |
| 567 | ret = stream.total_out; |
| 568 | error: |
| 569 | return ret; |
| 570 | } |
| 571 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 572 | static int nvram_pstore_open(struct pstore_info *psi) |
| 573 | { |
| 574 | /* Reset the iterator to start reading partitions again */ |
| 575 | read_type = -1; |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * nvram_pstore_write - pstore write callback for nvram |
| 581 | * @type: Type of message logged |
| 582 | * @reason: reason behind dump (oops/panic) |
| 583 | * @id: identifier to indicate the write performed |
| 584 | * @part: pstore writes data to registered buffer in parts, |
| 585 | * part number will indicate the same. |
| 586 | * @count: Indicates oops count |
Aruna Balakrishnaiah | 6bbbca7 | 2013-06-27 14:02:56 +0530 | [diff] [blame] | 587 | * @hsize: Size of header added by pstore |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 588 | * @size: number of bytes written to the registered buffer |
| 589 | * @psi: registered pstore_info structure |
| 590 | * |
| 591 | * Called by pstore_dump() when an oops or panic report is logged in the |
| 592 | * printk buffer. |
| 593 | * Returns 0 on successful write. |
| 594 | */ |
| 595 | static int nvram_pstore_write(enum pstore_type_id type, |
| 596 | enum kmsg_dump_reason reason, |
| 597 | u64 *id, unsigned int part, int count, |
Aruna Balakrishnaiah | 6bbbca7 | 2013-06-27 14:02:56 +0530 | [diff] [blame] | 598 | size_t hsize, size_t size, |
| 599 | struct pstore_info *psi) |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 600 | { |
| 601 | int rc; |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 602 | unsigned int err_type = ERR_TYPE_KERNEL_PANIC; |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 603 | struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf; |
| 604 | |
| 605 | /* part 1 has the recent messages from printk buffer */ |
| 606 | if (part > 1 || type != PSTORE_TYPE_DMESG || |
| 607 | clobbering_unread_rtas_event()) |
| 608 | return -1; |
| 609 | |
| 610 | oops_hdr->version = OOPS_HDR_VERSION; |
| 611 | oops_hdr->report_length = (u16) size; |
| 612 | oops_hdr->timestamp = get_seconds(); |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 613 | |
| 614 | if (big_oops_buf) { |
| 615 | rc = zip_oops(size); |
| 616 | /* |
| 617 | * If compression fails copy recent log messages from |
| 618 | * big_oops_buf to oops_data. |
| 619 | */ |
| 620 | if (rc != 0) { |
| 621 | size_t diff = size - oops_data_sz + hsize; |
| 622 | |
| 623 | if (size > oops_data_sz) { |
| 624 | memcpy(oops_data, big_oops_buf, hsize); |
| 625 | memcpy(oops_data + hsize, big_oops_buf + diff, |
| 626 | oops_data_sz - hsize); |
| 627 | |
| 628 | oops_hdr->report_length = (u16) oops_data_sz; |
| 629 | } else |
| 630 | memcpy(oops_data, big_oops_buf, size); |
| 631 | } else |
| 632 | err_type = ERR_TYPE_KERNEL_PANIC_GZ; |
| 633 | } |
| 634 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 635 | rc = nvram_write_os_partition(&oops_log_partition, oops_buf, |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 636 | (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type, |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 637 | count); |
| 638 | |
| 639 | if (rc != 0) |
| 640 | return rc; |
| 641 | |
| 642 | *id = part; |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | /* |
Aruna Balakrishnaiah | a5e4797 | 2013-06-06 00:22:20 +0530 | [diff] [blame] | 647 | * Reads the oops/panic report, rtas, of-config and common partition. |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 648 | * Returns the length of the data we read from each partition. |
| 649 | * Returns 0 if we've been called before. |
| 650 | */ |
| 651 | static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type, |
| 652 | int *count, struct timespec *time, char **buf, |
| 653 | struct pstore_info *psi) |
| 654 | { |
| 655 | struct oops_log_info *oops_hdr; |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 656 | unsigned int err_type, id_no, size = 0; |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 657 | struct nvram_os_partition *part = NULL; |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 658 | char *buff = NULL, *big_buff = NULL; |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 659 | int sig = 0; |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 660 | loff_t p; |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 661 | |
| 662 | read_type++; |
| 663 | |
| 664 | switch (nvram_type_ids[read_type]) { |
| 665 | case PSTORE_TYPE_DMESG: |
| 666 | part = &oops_log_partition; |
| 667 | *type = PSTORE_TYPE_DMESG; |
| 668 | break; |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 669 | case PSTORE_TYPE_PPC_RTAS: |
| 670 | part = &rtas_log_partition; |
| 671 | *type = PSTORE_TYPE_PPC_RTAS; |
| 672 | time->tv_sec = last_rtas_event; |
| 673 | time->tv_nsec = 0; |
| 674 | break; |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 675 | case PSTORE_TYPE_PPC_OF: |
| 676 | sig = NVRAM_SIG_OF; |
| 677 | part = &of_config_partition; |
| 678 | *type = PSTORE_TYPE_PPC_OF; |
| 679 | *id = PSTORE_TYPE_PPC_OF; |
| 680 | time->tv_sec = 0; |
| 681 | time->tv_nsec = 0; |
| 682 | break; |
Aruna Balakrishnaiah | a5e4797 | 2013-06-06 00:22:20 +0530 | [diff] [blame] | 683 | case PSTORE_TYPE_PPC_COMMON: |
| 684 | sig = NVRAM_SIG_SYS; |
| 685 | part = &common_partition; |
| 686 | *type = PSTORE_TYPE_PPC_COMMON; |
| 687 | *id = PSTORE_TYPE_PPC_COMMON; |
| 688 | time->tv_sec = 0; |
| 689 | time->tv_nsec = 0; |
| 690 | break; |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 691 | default: |
| 692 | return 0; |
| 693 | } |
| 694 | |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 695 | if (!part->os_partition) { |
| 696 | p = nvram_find_partition(part->name, sig, &size); |
| 697 | if (p <= 0) { |
| 698 | pr_err("nvram: Failed to find partition %s, " |
| 699 | "err %d\n", part->name, (int)p); |
| 700 | return 0; |
| 701 | } |
| 702 | part->index = p; |
| 703 | part->size = size; |
| 704 | } |
| 705 | |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 706 | buff = kmalloc(part->size, GFP_KERNEL); |
| 707 | |
| 708 | if (!buff) |
| 709 | return -ENOMEM; |
| 710 | |
| 711 | if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) { |
| 712 | kfree(buff); |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | *count = 0; |
Aruna Balakrishnaiah | f33f748 | 2013-06-06 00:22:10 +0530 | [diff] [blame] | 717 | |
| 718 | if (part->os_partition) |
| 719 | *id = id_no; |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 720 | |
| 721 | if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) { |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 722 | int length, unzipped_len; |
Aruna Balakrishnaiah | 156c9eb | 2013-08-08 22:34:00 +0530 | [diff] [blame] | 723 | size_t hdr_size; |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 724 | |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 725 | oops_hdr = (struct oops_log_info *)buff; |
Aruna Balakrishnaiah | 156c9eb | 2013-08-08 22:34:00 +0530 | [diff] [blame] | 726 | if (oops_hdr->version < OOPS_HDR_VERSION) { |
| 727 | /* Old format oops header had 2-byte record size */ |
| 728 | hdr_size = sizeof(u16); |
| 729 | length = oops_hdr->version; |
| 730 | time->tv_sec = 0; |
| 731 | time->tv_nsec = 0; |
| 732 | } else { |
| 733 | hdr_size = sizeof(*oops_hdr); |
| 734 | length = oops_hdr->report_length; |
| 735 | time->tv_sec = oops_hdr->timestamp; |
| 736 | time->tv_nsec = 0; |
| 737 | } |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 738 | *buf = kmalloc(length, GFP_KERNEL); |
| 739 | if (*buf == NULL) |
| 740 | return -ENOMEM; |
Aruna Balakrishnaiah | 156c9eb | 2013-08-08 22:34:00 +0530 | [diff] [blame] | 741 | memcpy(*buf, buff + hdr_size, length); |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 742 | kfree(buff); |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 743 | |
| 744 | if (err_type == ERR_TYPE_KERNEL_PANIC_GZ) { |
| 745 | big_buff = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 746 | if (!big_buff) |
| 747 | return -ENOMEM; |
| 748 | |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 749 | unzipped_len = nvram_decompress(*buf, big_buff, |
| 750 | length, big_oops_buf_sz); |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 751 | |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 752 | if (unzipped_len < 0) { |
| 753 | pr_err("nvram: decompression failed, returned " |
| 754 | "rc %d\n", unzipped_len); |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 755 | kfree(big_buff); |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 756 | } else { |
| 757 | *buf = big_buff; |
| 758 | length = unzipped_len; |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 759 | } |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 760 | } |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 761 | return length; |
Aruna Balakrishnaiah | 69020ee | 2013-06-06 00:21:44 +0530 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | *buf = buff; |
| 765 | return part->size; |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | static struct pstore_info nvram_pstore_info = { |
| 769 | .owner = THIS_MODULE, |
| 770 | .name = "nvram", |
| 771 | .open = nvram_pstore_open, |
| 772 | .read = nvram_pstore_read, |
| 773 | .write = nvram_pstore_write, |
| 774 | }; |
| 775 | |
| 776 | static int nvram_pstore_init(void) |
| 777 | { |
| 778 | int rc = 0; |
| 779 | |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 780 | if (big_oops_buf) { |
| 781 | nvram_pstore_info.buf = big_oops_buf; |
| 782 | nvram_pstore_info.bufsize = big_oops_buf_sz; |
| 783 | } else { |
| 784 | nvram_pstore_info.buf = oops_data; |
| 785 | nvram_pstore_info.bufsize = oops_data_sz; |
| 786 | } |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 787 | |
| 788 | rc = pstore_register(&nvram_pstore_info); |
| 789 | if (rc != 0) |
| 790 | pr_err("nvram: pstore_register() failed, defaults to " |
| 791 | "kmsg_dump; returned %d\n", rc); |
Aruna Balakrishnaiah | d7563c9 | 2013-06-06 00:21:32 +0530 | [diff] [blame] | 792 | |
| 793 | return rc; |
| 794 | } |
| 795 | #else |
| 796 | static int nvram_pstore_init(void) |
| 797 | { |
| 798 | return -1; |
| 799 | } |
| 800 | #endif |
| 801 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 802 | static void __init nvram_init_oops_partition(int rtas_partition_exists) |
| 803 | { |
| 804 | int rc; |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 805 | size_t size; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 806 | |
| 807 | rc = pseries_nvram_init_os_partition(&oops_log_partition); |
| 808 | if (rc != 0) { |
| 809 | if (!rtas_partition_exists) |
| 810 | return; |
| 811 | pr_notice("nvram: Using %s partition to log both" |
| 812 | " RTAS errors and oops/panic reports\n", |
| 813 | rtas_log_partition.name); |
| 814 | memcpy(&oops_log_partition, &rtas_log_partition, |
| 815 | sizeof(rtas_log_partition)); |
| 816 | } |
| 817 | oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 818 | if (!oops_buf) { |
| 819 | pr_err("nvram: No memory for %s partition\n", |
| 820 | oops_log_partition.name); |
| 821 | return; |
| 822 | } |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 823 | oops_data = oops_buf + sizeof(struct oops_log_info); |
| 824 | oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 825 | |
| 826 | /* |
| 827 | * Figure compression (preceded by elimination of each line's <n> |
| 828 | * severity prefix) will reduce the oops/panic report to at most |
| 829 | * 45% of its original size. |
| 830 | */ |
| 831 | big_oops_buf_sz = (oops_data_sz * 100) / 45; |
| 832 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 833 | if (big_oops_buf) { |
Aruna Balakrishnaiah | 7e76f34 | 2013-08-08 22:33:49 +0530 | [diff] [blame] | 834 | size = max(zlib_deflate_workspacesize(WINDOW_BITS, MEM_LEVEL), |
| 835 | zlib_inflate_workspacesize()); |
| 836 | stream.workspace = kmalloc(size, GFP_KERNEL); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 837 | if (!stream.workspace) { |
| 838 | pr_err("nvram: No memory for compression workspace; " |
| 839 | "skipping compression of %s partition data\n", |
| 840 | oops_log_partition.name); |
| 841 | kfree(big_oops_buf); |
| 842 | big_oops_buf = NULL; |
| 843 | } |
| 844 | } else { |
| 845 | pr_err("No memory for uncompressed %s data; " |
| 846 | "skipping compression\n", oops_log_partition.name); |
| 847 | stream.workspace = NULL; |
| 848 | } |
| 849 | |
Aruna Balakrishnaiah | 40847e5 | 2013-06-27 14:03:13 +0530 | [diff] [blame] | 850 | rc = nvram_pstore_init(); |
| 851 | |
| 852 | if (!rc) |
| 853 | return; |
| 854 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 855 | rc = kmsg_dump_register(&nvram_kmsg_dumper); |
| 856 | if (rc != 0) { |
| 857 | pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc); |
| 858 | kfree(oops_buf); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 859 | kfree(big_oops_buf); |
| 860 | kfree(stream.workspace); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 864 | static int __init pseries_nvram_init_log_partitions(void) |
| 865 | { |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 866 | int rc; |
| 867 | |
| 868 | rc = pseries_nvram_init_os_partition(&rtas_log_partition); |
| 869 | nvram_init_oops_partition(rc == 0); |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | machine_arch_initcall(pseries, pseries_nvram_init_log_partitions); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | int __init pSeries_nvram_init(void) |
| 875 | { |
| 876 | struct device_node *nvram; |
Jeremy Kerr | 954a46e | 2006-07-12 15:39:43 +1000 | [diff] [blame] | 877 | const unsigned int *nbytes_p; |
| 878 | unsigned int proplen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | |
| 880 | nvram = of_find_node_by_type(NULL, "nvram"); |
| 881 | if (nvram == NULL) |
| 882 | return -ENODEV; |
| 883 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 884 | nbytes_p = of_get_property(nvram, "#bytes", &proplen); |
Julia Lawall | bad5232 | 2008-06-09 22:20:04 +1000 | [diff] [blame] | 885 | if (nbytes_p == NULL || proplen != sizeof(unsigned int)) { |
| 886 | of_node_put(nvram); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | return -EIO; |
Julia Lawall | bad5232 | 2008-06-09 22:20:04 +1000 | [diff] [blame] | 888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
| 890 | nvram_size = *nbytes_p; |
| 891 | |
| 892 | nvram_fetch = rtas_token("nvram-fetch"); |
| 893 | nvram_store = rtas_token("nvram-store"); |
| 894 | printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size); |
| 895 | of_node_put(nvram); |
| 896 | |
| 897 | ppc_md.nvram_read = pSeries_nvram_read; |
| 898 | ppc_md.nvram_write = pSeries_nvram_write; |
| 899 | ppc_md.nvram_size = pSeries_nvram_get_size; |
| 900 | |
| 901 | return 0; |
| 902 | } |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 903 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 904 | |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 905 | /* |
| 906 | * This is our kmsg_dump callback, called after an oops or panic report |
| 907 | * has been written to the printk buffer. We want to capture as much |
| 908 | * of the printk buffer as possible. First, capture as much as we can |
| 909 | * that we think will compress sufficiently to fit in the lnx,oops-log |
| 910 | * partition. If that's too much, go back and capture uncompressed text. |
| 911 | */ |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 912 | static void oops_to_nvram(struct kmsg_dumper *dumper, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 913 | enum kmsg_dump_reason reason) |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 914 | { |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 915 | struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 916 | static unsigned int oops_count = 0; |
Jim Keniston | 15d260b | 2011-03-25 12:47:58 +0000 | [diff] [blame] | 917 | static bool panicking = false; |
Anton Blanchard | 120a52c | 2011-11-30 15:46:45 +0000 | [diff] [blame] | 918 | static DEFINE_SPINLOCK(lock); |
| 919 | unsigned long flags; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 920 | size_t text_len; |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 921 | unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ; |
| 922 | int rc = -1; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 923 | |
Jim Keniston | 15d260b | 2011-03-25 12:47:58 +0000 | [diff] [blame] | 924 | switch (reason) { |
| 925 | case KMSG_DUMP_RESTART: |
| 926 | case KMSG_DUMP_HALT: |
| 927 | case KMSG_DUMP_POWEROFF: |
| 928 | /* These are almost always orderly shutdowns. */ |
| 929 | return; |
| 930 | case KMSG_DUMP_OOPS: |
Jim Keniston | 15d260b | 2011-03-25 12:47:58 +0000 | [diff] [blame] | 931 | break; |
| 932 | case KMSG_DUMP_PANIC: |
| 933 | panicking = true; |
| 934 | break; |
| 935 | case KMSG_DUMP_EMERG: |
| 936 | if (panicking) |
| 937 | /* Panic report already captured. */ |
| 938 | return; |
| 939 | break; |
| 940 | default: |
| 941 | pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n", |
| 942 | __FUNCTION__, (int) reason); |
| 943 | return; |
| 944 | } |
| 945 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 946 | if (clobbering_unread_rtas_event()) |
| 947 | return; |
| 948 | |
Anton Blanchard | 120a52c | 2011-11-30 15:46:45 +0000 | [diff] [blame] | 949 | if (!spin_trylock_irqsave(&lock, flags)) |
| 950 | return; |
| 951 | |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 952 | if (big_oops_buf) { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 953 | kmsg_dump_get_buffer(dumper, false, |
| 954 | big_oops_buf, big_oops_buf_sz, &text_len); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 955 | rc = zip_oops(text_len); |
| 956 | } |
| 957 | if (rc != 0) { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 958 | kmsg_dump_rewind(dumper); |
Aruna Balakrishnaiah | 1bf247f | 2013-06-06 00:20:55 +0530 | [diff] [blame] | 959 | kmsg_dump_get_buffer(dumper, false, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 960 | oops_data, oops_data_sz, &text_len); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 961 | err_type = ERR_TYPE_KERNEL_PANIC; |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 962 | oops_hdr->version = OOPS_HDR_VERSION; |
| 963 | oops_hdr->report_length = (u16) text_len; |
| 964 | oops_hdr->timestamp = get_seconds(); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 967 | (void) nvram_write_os_partition(&oops_log_partition, oops_buf, |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 968 | (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type, |
| 969 | ++oops_count); |
Anton Blanchard | 120a52c | 2011-11-30 15:46:45 +0000 | [diff] [blame] | 970 | |
| 971 | spin_unlock_irqrestore(&lock, flags); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 972 | } |