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> |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 21 | #include <linux/ctype.h> |
| 22 | #include <linux/zlib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
| 24 | #include <asm/nvram.h> |
| 25 | #include <asm/rtas.h> |
| 26 | #include <asm/prom.h> |
| 27 | #include <asm/machdep.h> |
| 28 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 29 | /* Max bytes to read/write in one go */ |
| 30 | #define NVRW_CNT 0x20 |
| 31 | |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 32 | /* |
| 33 | * Set oops header version to distingush between old and new format header. |
| 34 | * lnx,oops-log partition max size is 4000, header version > 4000 will |
| 35 | * help in identifying new header. |
| 36 | */ |
| 37 | #define OOPS_HDR_VERSION 5000 |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static unsigned int nvram_size; |
| 40 | static int nvram_fetch, nvram_store; |
| 41 | static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */ |
| 42 | static DEFINE_SPINLOCK(nvram_lock); |
| 43 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 44 | struct err_log_info { |
| 45 | int error_type; |
| 46 | unsigned int seq_num; |
| 47 | }; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 48 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 49 | struct nvram_os_partition { |
| 50 | const char *name; |
| 51 | int req_size; /* desired size, in bytes */ |
| 52 | int min_size; /* minimum acceptable size (0 means req_size) */ |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 53 | long size; /* size of data portion (excluding err_log_info) */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 54 | long index; /* offset of data portion of partition */ |
| 55 | }; |
| 56 | |
| 57 | static struct nvram_os_partition rtas_log_partition = { |
| 58 | .name = "ibm,rtas-log", |
| 59 | .req_size = 2079, |
| 60 | .min_size = 1055, |
| 61 | .index = -1 |
| 62 | }; |
| 63 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 64 | static struct nvram_os_partition oops_log_partition = { |
| 65 | .name = "lnx,oops-log", |
| 66 | .req_size = 4000, |
| 67 | .min_size = 2000, |
| 68 | .index = -1 |
| 69 | }; |
| 70 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 71 | static const char *pseries_nvram_os_partitions[] = { |
| 72 | "ibm,rtas-log", |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 73 | "lnx,oops-log", |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 74 | NULL |
| 75 | }; |
Benjamin Herrenschmidt | 9a866b8 | 2010-08-02 10:51:25 +1000 | [diff] [blame] | 76 | |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 77 | struct oops_log_info { |
| 78 | u16 version; |
| 79 | u16 report_length; |
| 80 | u64 timestamp; |
| 81 | } __attribute__((packed)); |
| 82 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 83 | static void oops_to_nvram(struct kmsg_dumper *dumper, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 84 | enum kmsg_dump_reason reason); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 85 | |
| 86 | static struct kmsg_dumper nvram_kmsg_dumper = { |
| 87 | .dump = oops_to_nvram |
| 88 | }; |
| 89 | |
| 90 | /* See clobbering_unread_rtas_event() */ |
| 91 | #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */ |
| 92 | static unsigned long last_unread_rtas_event; /* timestamp */ |
| 93 | |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 94 | /* |
| 95 | * For capturing and compressing an oops or panic report... |
| 96 | |
| 97 | * big_oops_buf[] holds the uncompressed text we're capturing. |
| 98 | * |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 99 | * oops_buf[] holds the compressed text, preceded by a oops header. |
| 100 | * oops header has u16 holding the version of oops header (to differentiate |
| 101 | * between old and new format header) followed by u16 holding the length of |
| 102 | * the compressed* text (*Or uncompressed, if compression fails.) and u64 |
| 103 | * holding the timestamp. oops_buf[] gets written to NVRAM. |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 104 | * |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 105 | * 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] | 106 | * |
| 107 | * +- oops_buf |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 108 | * | +- oops_data |
| 109 | * v v |
| 110 | * +-----------+-----------+-----------+------------------------+ |
| 111 | * | version | length | timestamp | text | |
| 112 | * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) | |
| 113 | * +-----------+-----------+-----------+------------------------+ |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 114 | * ^ |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 115 | * +- oops_log_info |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 116 | * |
| 117 | * We preallocate these buffers during init to avoid kmalloc during oops/panic. |
| 118 | */ |
| 119 | static size_t big_oops_buf_sz; |
| 120 | static char *big_oops_buf, *oops_buf; |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 121 | static char *oops_data; |
| 122 | static size_t oops_data_sz; |
| 123 | |
| 124 | /* Compression parameters */ |
| 125 | #define COMPR_LEVEL 6 |
| 126 | #define WINDOW_BITS 12 |
| 127 | #define MEM_LEVEL 4 |
| 128 | static struct z_stream_s stream; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index) |
| 131 | { |
| 132 | unsigned int i; |
| 133 | unsigned long len; |
| 134 | int done; |
| 135 | unsigned long flags; |
| 136 | char *p = buf; |
| 137 | |
| 138 | |
| 139 | if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE) |
| 140 | return -ENODEV; |
| 141 | |
| 142 | if (*index >= nvram_size) |
| 143 | return 0; |
| 144 | |
| 145 | i = *index; |
| 146 | if (i + count > nvram_size) |
| 147 | count = nvram_size - i; |
| 148 | |
| 149 | spin_lock_irqsave(&nvram_lock, flags); |
| 150 | |
| 151 | for (; count != 0; count -= len) { |
| 152 | len = count; |
| 153 | if (len > NVRW_CNT) |
| 154 | len = NVRW_CNT; |
| 155 | |
| 156 | if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf), |
| 157 | len) != 0) || len != done) { |
| 158 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 159 | return -EIO; |
| 160 | } |
| 161 | |
| 162 | memcpy(p, nvram_buf, len); |
| 163 | |
| 164 | p += len; |
| 165 | i += len; |
| 166 | } |
| 167 | |
| 168 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 169 | |
| 170 | *index = i; |
| 171 | return p - buf; |
| 172 | } |
| 173 | |
| 174 | static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index) |
| 175 | { |
| 176 | unsigned int i; |
| 177 | unsigned long len; |
| 178 | int done; |
| 179 | unsigned long flags; |
| 180 | const char *p = buf; |
| 181 | |
| 182 | if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE) |
| 183 | return -ENODEV; |
| 184 | |
| 185 | if (*index >= nvram_size) |
| 186 | return 0; |
| 187 | |
| 188 | i = *index; |
| 189 | if (i + count > nvram_size) |
| 190 | count = nvram_size - i; |
| 191 | |
| 192 | spin_lock_irqsave(&nvram_lock, flags); |
| 193 | |
| 194 | for (; count != 0; count -= len) { |
| 195 | len = count; |
| 196 | if (len > NVRW_CNT) |
| 197 | len = NVRW_CNT; |
| 198 | |
| 199 | memcpy(nvram_buf, p, len); |
| 200 | |
| 201 | if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf), |
| 202 | len) != 0) || len != done) { |
| 203 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 204 | return -EIO; |
| 205 | } |
| 206 | |
| 207 | p += len; |
| 208 | i += len; |
| 209 | } |
| 210 | spin_unlock_irqrestore(&nvram_lock, flags); |
| 211 | |
| 212 | *index = i; |
| 213 | return p - buf; |
| 214 | } |
| 215 | |
| 216 | static ssize_t pSeries_nvram_get_size(void) |
| 217 | { |
| 218 | return nvram_size ? nvram_size : -ENODEV; |
| 219 | } |
| 220 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 221 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 222 | /* nvram_write_os_partition, nvram_write_error_log |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 223 | * |
| 224 | * We need to buffer the error logs into nvram to ensure that we have |
| 225 | * the failure information to decode. If we have a severe error there |
| 226 | * is no way to guarantee that the OS or the machine is in a state to |
| 227 | * get back to user land and write the error to disk. For example if |
| 228 | * the SCSI device driver causes a Machine Check by writing to a bad |
| 229 | * IO address, there is no way of guaranteeing that the device driver |
| 230 | * is in any state that is would also be able to write the error data |
| 231 | * captured to disk, thus we buffer it in NVRAM for analysis on the |
| 232 | * next boot. |
| 233 | * |
| 234 | * In NVRAM the partition containing the error log buffer will looks like: |
| 235 | * Header (in bytes): |
| 236 | * +-----------+----------+--------+------------+------------------+ |
| 237 | * | signature | checksum | length | name | data | |
| 238 | * |0 |1 |2 3|4 15|16 length-1| |
| 239 | * +-----------+----------+--------+------------+------------------+ |
| 240 | * |
| 241 | * The 'data' section would look like (in bytes): |
| 242 | * +--------------+------------+-----------------------------------+ |
| 243 | * | event_logged | sequence # | error log | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 244 | * |0 3|4 7|8 error_log_size-1| |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 245 | * +--------------+------------+-----------------------------------+ |
| 246 | * |
| 247 | * event_logged: 0 if event has not been logged to syslog, 1 if it has |
| 248 | * sequence #: The unique sequence # for each event. (until it wraps) |
| 249 | * error log: The error log from event_scan |
| 250 | */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 251 | int nvram_write_os_partition(struct nvram_os_partition *part, char * buff, |
| 252 | int length, unsigned int err_type, unsigned int error_log_cnt) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 253 | { |
| 254 | int rc; |
| 255 | loff_t tmp_index; |
| 256 | struct err_log_info info; |
| 257 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 258 | if (part->index == -1) { |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 259 | return -ESPIPE; |
| 260 | } |
| 261 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 262 | if (length > part->size) { |
| 263 | length = part->size; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | info.error_type = err_type; |
| 267 | info.seq_num = error_log_cnt; |
| 268 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 269 | tmp_index = part->index; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 270 | |
| 271 | rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index); |
| 272 | if (rc <= 0) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 273 | pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 274 | return rc; |
| 275 | } |
| 276 | |
| 277 | rc = ppc_md.nvram_write(buff, length, &tmp_index); |
| 278 | if (rc <= 0) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 279 | pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 280 | return rc; |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 286 | int nvram_write_error_log(char * buff, int length, |
| 287 | unsigned int err_type, unsigned int error_log_cnt) |
| 288 | { |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 289 | int rc = nvram_write_os_partition(&rtas_log_partition, buff, length, |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 290 | err_type, error_log_cnt); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 291 | if (!rc) |
| 292 | last_unread_rtas_event = get_seconds(); |
| 293 | return rc; |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 296 | /* nvram_read_partition |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 297 | * |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 298 | * Reads nvram partition for at most 'length' |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 299 | */ |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 300 | int nvram_read_partition(struct nvram_os_partition *part, char *buff, |
| 301 | int length, unsigned int *err_type, |
| 302 | unsigned int *error_log_cnt) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 303 | { |
| 304 | int rc; |
| 305 | loff_t tmp_index; |
| 306 | struct err_log_info info; |
| 307 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 308 | if (part->index == -1) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 309 | return -1; |
| 310 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 311 | if (length > part->size) |
| 312 | length = part->size; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 313 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 314 | tmp_index = part->index; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 315 | |
| 316 | rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index); |
| 317 | if (rc <= 0) { |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 318 | pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 319 | return rc; |
| 320 | } |
| 321 | |
| 322 | rc = ppc_md.nvram_read(buff, length, &tmp_index); |
| 323 | if (rc <= 0) { |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 324 | pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 325 | return rc; |
| 326 | } |
| 327 | |
| 328 | *error_log_cnt = info.seq_num; |
| 329 | *err_type = info.error_type; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
Aruna Balakrishnaiah | 1267461 | 2013-06-06 00:21:16 +0530 | [diff] [blame^] | 334 | /* nvram_read_error_log |
| 335 | * |
| 336 | * Reads nvram for error log for at most 'length' |
| 337 | */ |
| 338 | int nvram_read_error_log(char *buff, int length, |
| 339 | unsigned int *err_type, unsigned int *error_log_cnt) |
| 340 | { |
| 341 | return nvram_read_partition(&rtas_log_partition, buff, length, |
| 342 | err_type, error_log_cnt); |
| 343 | } |
| 344 | |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 345 | /* This doesn't actually zero anything, but it sets the event_logged |
| 346 | * word to tell that this event is safely in syslog. |
| 347 | */ |
| 348 | int nvram_clear_error_log(void) |
| 349 | { |
| 350 | loff_t tmp_index; |
| 351 | int clear_word = ERR_FLAG_ALREADY_LOGGED; |
| 352 | int rc; |
| 353 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 354 | if (rtas_log_partition.index == -1) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 355 | return -1; |
| 356 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 357 | tmp_index = rtas_log_partition.index; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 358 | |
| 359 | rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index); |
| 360 | if (rc <= 0) { |
| 361 | printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); |
| 362 | return rc; |
| 363 | } |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 364 | last_unread_rtas_event = 0; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 369 | /* pseries_nvram_init_os_partition |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 370 | * |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 371 | * This sets up a partition with an "OS" signature. |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 372 | * |
| 373 | * The general strategy is the following: |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 374 | * 1.) If a partition with the indicated name already exists... |
| 375 | * - If it's large enough, use it. |
| 376 | * - Otherwise, recycle it and keep going. |
| 377 | * 2.) Search for a free partition that is large enough. |
| 378 | * 3.) If there's not a free partition large enough, recycle any obsolete |
| 379 | * OS partitions and try again. |
| 380 | * 4.) Will first try getting a chunk that will satisfy the requested size. |
| 381 | * 5.) If a chunk of the requested size cannot be allocated, then try finding |
| 382 | * a chunk that will satisfy the minum needed. |
| 383 | * |
| 384 | * Returns 0 on success, else -1. |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 385 | */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 386 | static int __init pseries_nvram_init_os_partition(struct nvram_os_partition |
| 387 | *part) |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 388 | { |
| 389 | loff_t p; |
| 390 | int size; |
| 391 | |
| 392 | /* Scan nvram for partitions */ |
| 393 | nvram_scan_partitions(); |
| 394 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 395 | /* Look for ours */ |
| 396 | p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 397 | |
| 398 | /* Found one but too small, remove it */ |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 399 | if (p && size < part->min_size) { |
| 400 | pr_info("nvram: Found too small %s partition," |
| 401 | " removing it...\n", part->name); |
| 402 | nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 403 | p = 0; |
| 404 | } |
| 405 | |
| 406 | /* Create one if we didn't find */ |
| 407 | if (!p) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 408 | p = nvram_create_partition(part->name, NVRAM_SIG_OS, |
| 409 | part->req_size, part->min_size); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 410 | if (p == -ENOSPC) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 411 | pr_info("nvram: No room to create %s partition, " |
| 412 | "deleting any obsolete OS partitions...\n", |
| 413 | part->name); |
| 414 | nvram_remove_partition(NULL, NVRAM_SIG_OS, |
| 415 | pseries_nvram_os_partitions); |
| 416 | p = nvram_create_partition(part->name, NVRAM_SIG_OS, |
| 417 | part->req_size, part->min_size); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | if (p <= 0) { |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 422 | pr_err("nvram: Failed to find or create %s" |
| 423 | " partition, err %d\n", part->name, (int)p); |
| 424 | return -1; |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 425 | } |
| 426 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 427 | part->index = p; |
| 428 | part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 429 | |
| 430 | return 0; |
| 431 | } |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 432 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 433 | static void __init nvram_init_oops_partition(int rtas_partition_exists) |
| 434 | { |
| 435 | int rc; |
| 436 | |
| 437 | rc = pseries_nvram_init_os_partition(&oops_log_partition); |
| 438 | if (rc != 0) { |
| 439 | if (!rtas_partition_exists) |
| 440 | return; |
| 441 | pr_notice("nvram: Using %s partition to log both" |
| 442 | " RTAS errors and oops/panic reports\n", |
| 443 | rtas_log_partition.name); |
| 444 | memcpy(&oops_log_partition, &rtas_log_partition, |
| 445 | sizeof(rtas_log_partition)); |
| 446 | } |
| 447 | oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 448 | if (!oops_buf) { |
| 449 | pr_err("nvram: No memory for %s partition\n", |
| 450 | oops_log_partition.name); |
| 451 | return; |
| 452 | } |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 453 | oops_data = oops_buf + sizeof(struct oops_log_info); |
| 454 | oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | * Figure compression (preceded by elimination of each line's <n> |
| 458 | * severity prefix) will reduce the oops/panic report to at most |
| 459 | * 45% of its original size. |
| 460 | */ |
| 461 | big_oops_buf_sz = (oops_data_sz * 100) / 45; |
| 462 | big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL); |
| 463 | if (big_oops_buf) { |
| 464 | stream.workspace = kmalloc(zlib_deflate_workspacesize( |
| 465 | WINDOW_BITS, MEM_LEVEL), GFP_KERNEL); |
| 466 | if (!stream.workspace) { |
| 467 | pr_err("nvram: No memory for compression workspace; " |
| 468 | "skipping compression of %s partition data\n", |
| 469 | oops_log_partition.name); |
| 470 | kfree(big_oops_buf); |
| 471 | big_oops_buf = NULL; |
| 472 | } |
| 473 | } else { |
| 474 | pr_err("No memory for uncompressed %s data; " |
| 475 | "skipping compression\n", oops_log_partition.name); |
| 476 | stream.workspace = NULL; |
| 477 | } |
| 478 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 479 | rc = kmsg_dump_register(&nvram_kmsg_dumper); |
| 480 | if (rc != 0) { |
| 481 | pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc); |
| 482 | kfree(oops_buf); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 483 | kfree(big_oops_buf); |
| 484 | kfree(stream.workspace); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 488 | static int __init pseries_nvram_init_log_partitions(void) |
| 489 | { |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 490 | int rc; |
| 491 | |
| 492 | rc = pseries_nvram_init_os_partition(&rtas_log_partition); |
| 493 | nvram_init_oops_partition(rc == 0); |
Jim Keniston | 0f4ac13 | 2011-02-09 12:43:13 +0000 | [diff] [blame] | 494 | return 0; |
| 495 | } |
| 496 | machine_arch_initcall(pseries, pseries_nvram_init_log_partitions); |
Benjamin Herrenschmidt | edc79a2 | 2010-08-02 11:18:09 +1000 | [diff] [blame] | 497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | int __init pSeries_nvram_init(void) |
| 499 | { |
| 500 | struct device_node *nvram; |
Jeremy Kerr | 954a46e | 2006-07-12 15:39:43 +1000 | [diff] [blame] | 501 | const unsigned int *nbytes_p; |
| 502 | unsigned int proplen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
| 504 | nvram = of_find_node_by_type(NULL, "nvram"); |
| 505 | if (nvram == NULL) |
| 506 | return -ENODEV; |
| 507 | |
Stephen Rothwell | e2eb639 | 2007-04-03 22:26:41 +1000 | [diff] [blame] | 508 | nbytes_p = of_get_property(nvram, "#bytes", &proplen); |
Julia Lawall | bad5232 | 2008-06-09 22:20:04 +1000 | [diff] [blame] | 509 | if (nbytes_p == NULL || proplen != sizeof(unsigned int)) { |
| 510 | of_node_put(nvram); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return -EIO; |
Julia Lawall | bad5232 | 2008-06-09 22:20:04 +1000 | [diff] [blame] | 512 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | nvram_size = *nbytes_p; |
| 515 | |
| 516 | nvram_fetch = rtas_token("nvram-fetch"); |
| 517 | nvram_store = rtas_token("nvram-store"); |
| 518 | printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size); |
| 519 | of_node_put(nvram); |
| 520 | |
| 521 | ppc_md.nvram_read = pSeries_nvram_read; |
| 522 | ppc_md.nvram_write = pSeries_nvram_write; |
| 523 | ppc_md.nvram_size = pSeries_nvram_get_size; |
| 524 | |
| 525 | return 0; |
| 526 | } |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 527 | |
| 528 | /* |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 529 | * Are we using the ibm,rtas-log for oops/panic reports? And if so, |
| 530 | * would logging this oops/panic overwrite an RTAS event that rtas_errd |
| 531 | * hasn't had a chance to read and process? Return 1 if so, else 0. |
| 532 | * |
| 533 | * We assume that if rtas_errd hasn't read the RTAS event in |
| 534 | * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to. |
| 535 | */ |
| 536 | static int clobbering_unread_rtas_event(void) |
| 537 | { |
| 538 | return (oops_log_partition.index == rtas_log_partition.index |
| 539 | && last_unread_rtas_event |
| 540 | && get_seconds() - last_unread_rtas_event <= |
| 541 | NVRAM_RTAS_READ_TIMEOUT); |
| 542 | } |
| 543 | |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 544 | /* Derived from logfs_compress() */ |
| 545 | static int nvram_compress(const void *in, void *out, size_t inlen, |
| 546 | size_t outlen) |
| 547 | { |
| 548 | int err, ret; |
| 549 | |
| 550 | ret = -EIO; |
| 551 | err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS, |
| 552 | MEM_LEVEL, Z_DEFAULT_STRATEGY); |
| 553 | if (err != Z_OK) |
| 554 | goto error; |
| 555 | |
| 556 | stream.next_in = in; |
| 557 | stream.avail_in = inlen; |
| 558 | stream.total_in = 0; |
| 559 | stream.next_out = out; |
| 560 | stream.avail_out = outlen; |
| 561 | stream.total_out = 0; |
| 562 | |
| 563 | err = zlib_deflate(&stream, Z_FINISH); |
| 564 | if (err != Z_STREAM_END) |
| 565 | goto error; |
| 566 | |
| 567 | err = zlib_deflateEnd(&stream); |
| 568 | if (err != Z_OK) |
| 569 | goto error; |
| 570 | |
| 571 | if (stream.total_out >= stream.total_in) |
| 572 | goto error; |
| 573 | |
| 574 | ret = stream.total_out; |
| 575 | error: |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | /* Compress the text from big_oops_buf into oops_buf. */ |
| 580 | static int zip_oops(size_t text_len) |
| 581 | { |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 582 | struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 583 | int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len, |
| 584 | oops_data_sz); |
| 585 | if (zipped_len < 0) { |
| 586 | pr_err("nvram: compression failed; returned %d\n", zipped_len); |
| 587 | pr_err("nvram: logging uncompressed oops/panic report\n"); |
| 588 | return -1; |
| 589 | } |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 590 | oops_hdr->version = OOPS_HDR_VERSION; |
| 591 | oops_hdr->report_length = (u16) zipped_len; |
| 592 | oops_hdr->timestamp = get_seconds(); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * This is our kmsg_dump callback, called after an oops or panic report |
| 598 | * has been written to the printk buffer. We want to capture as much |
| 599 | * of the printk buffer as possible. First, capture as much as we can |
| 600 | * that we think will compress sufficiently to fit in the lnx,oops-log |
| 601 | * partition. If that's too much, go back and capture uncompressed text. |
| 602 | */ |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 603 | static void oops_to_nvram(struct kmsg_dumper *dumper, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 604 | enum kmsg_dump_reason reason) |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 605 | { |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 606 | struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 607 | static unsigned int oops_count = 0; |
Jim Keniston | 15d260b | 2011-03-25 12:47:58 +0000 | [diff] [blame] | 608 | static bool panicking = false; |
Anton Blanchard | 120a52c | 2011-11-30 15:46:45 +0000 | [diff] [blame] | 609 | static DEFINE_SPINLOCK(lock); |
| 610 | unsigned long flags; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 611 | size_t text_len; |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 612 | unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ; |
| 613 | int rc = -1; |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 614 | |
Jim Keniston | 15d260b | 2011-03-25 12:47:58 +0000 | [diff] [blame] | 615 | switch (reason) { |
| 616 | case KMSG_DUMP_RESTART: |
| 617 | case KMSG_DUMP_HALT: |
| 618 | case KMSG_DUMP_POWEROFF: |
| 619 | /* These are almost always orderly shutdowns. */ |
| 620 | return; |
| 621 | case KMSG_DUMP_OOPS: |
Jim Keniston | 15d260b | 2011-03-25 12:47:58 +0000 | [diff] [blame] | 622 | break; |
| 623 | case KMSG_DUMP_PANIC: |
| 624 | panicking = true; |
| 625 | break; |
| 626 | case KMSG_DUMP_EMERG: |
| 627 | if (panicking) |
| 628 | /* Panic report already captured. */ |
| 629 | return; |
| 630 | break; |
| 631 | default: |
| 632 | pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n", |
| 633 | __FUNCTION__, (int) reason); |
| 634 | return; |
| 635 | } |
| 636 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 637 | if (clobbering_unread_rtas_event()) |
| 638 | return; |
| 639 | |
Anton Blanchard | 120a52c | 2011-11-30 15:46:45 +0000 | [diff] [blame] | 640 | if (!spin_trylock_irqsave(&lock, flags)) |
| 641 | return; |
| 642 | |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 643 | if (big_oops_buf) { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 644 | kmsg_dump_get_buffer(dumper, false, |
| 645 | big_oops_buf, big_oops_buf_sz, &text_len); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 646 | rc = zip_oops(text_len); |
| 647 | } |
| 648 | if (rc != 0) { |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 649 | kmsg_dump_rewind(dumper); |
Aruna Balakrishnaiah | 1bf247f | 2013-06-06 00:20:55 +0530 | [diff] [blame] | 650 | kmsg_dump_get_buffer(dumper, false, |
Kay Sievers | e2ae715 | 2012-06-15 14:07:51 +0200 | [diff] [blame] | 651 | oops_data, oops_data_sz, &text_len); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 652 | err_type = ERR_TYPE_KERNEL_PANIC; |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 653 | oops_hdr->version = OOPS_HDR_VERSION; |
| 654 | oops_hdr->report_length = (u16) text_len; |
| 655 | oops_hdr->timestamp = get_seconds(); |
Jim Keniston | 6c49368 | 2011-07-25 07:54:50 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 658 | (void) nvram_write_os_partition(&oops_log_partition, oops_buf, |
Aruna Balakrishnaiah | b1f70e1 | 2013-06-06 00:21:05 +0530 | [diff] [blame] | 659 | (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type, |
| 660 | ++oops_count); |
Anton Blanchard | 120a52c | 2011-11-30 15:46:45 +0000 | [diff] [blame] | 661 | |
| 662 | spin_unlock_irqrestore(&lock, flags); |
Jim Keniston | a5cf4b0 | 2011-02-09 13:00:09 +0000 | [diff] [blame] | 663 | } |