Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
1da177e4 | 18 | #include <linux/spinlock.h> |
a5cf4b08 JK |
19 | #include <linux/slab.h> |
20 | #include <linux/kmsg_dump.h> | |
6c493685 JK |
21 | #include <linux/ctype.h> |
22 | #include <linux/zlib.h> | |
1da177e4 LT |
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 | ||
4e7c77a3 BH |
29 | /* Max bytes to read/write in one go */ |
30 | #define NVRW_CNT 0x20 | |
31 | ||
b1f70e1f AB |
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 | ||
1da177e4 LT |
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 | ||
edc79a2f BH |
44 | struct err_log_info { |
45 | int error_type; | |
46 | unsigned int seq_num; | |
47 | }; | |
edc79a2f | 48 | |
0f4ac132 JK |
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) */ | |
a5cf4b08 | 53 | long size; /* size of data portion (excluding err_log_info) */ |
0f4ac132 JK |
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 | ||
a5cf4b08 JK |
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 | ||
0f4ac132 JK |
71 | static const char *pseries_nvram_os_partitions[] = { |
72 | "ibm,rtas-log", | |
a5cf4b08 | 73 | "lnx,oops-log", |
0f4ac132 JK |
74 | NULL |
75 | }; | |
9a866b87 | 76 | |
b1f70e1f AB |
77 | struct oops_log_info { |
78 | u16 version; | |
79 | u16 report_length; | |
80 | u64 timestamp; | |
81 | } __attribute__((packed)); | |
82 | ||
a5cf4b08 | 83 | static void oops_to_nvram(struct kmsg_dumper *dumper, |
e2ae715d | 84 | enum kmsg_dump_reason reason); |
a5cf4b08 JK |
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 | ||
6c493685 JK |
94 | /* |
95 | * For capturing and compressing an oops or panic report... | |
96 | ||
97 | * big_oops_buf[] holds the uncompressed text we're capturing. | |
98 | * | |
b1f70e1f AB |
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. | |
6c493685 | 104 | * |
b1f70e1f | 105 | * oops_log_info points to the header. oops_data points to the compressed text. |
6c493685 JK |
106 | * |
107 | * +- oops_buf | |
b1f70e1f AB |
108 | * | +- oops_data |
109 | * v v | |
110 | * +-----------+-----------+-----------+------------------------+ | |
111 | * | version | length | timestamp | text | | |
112 | * | (2 bytes) | (2 bytes) | (8 bytes) | (oops_data_sz bytes) | | |
113 | * +-----------+-----------+-----------+------------------------+ | |
6c493685 | 114 | * ^ |
b1f70e1f | 115 | * +- oops_log_info |
6c493685 JK |
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; | |
6c493685 JK |
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; | |
a5cf4b08 | 129 | |
1da177e4 LT |
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 | ||
edc79a2f | 221 | |
0f4ac132 | 222 | /* nvram_write_os_partition, nvram_write_error_log |
edc79a2f BH |
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 | | |
0f4ac132 | 244 | * |0 3|4 7|8 error_log_size-1| |
edc79a2f BH |
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 | */ | |
0f4ac132 JK |
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) | |
edc79a2f BH |
253 | { |
254 | int rc; | |
255 | loff_t tmp_index; | |
256 | struct err_log_info info; | |
257 | ||
0f4ac132 | 258 | if (part->index == -1) { |
edc79a2f BH |
259 | return -ESPIPE; |
260 | } | |
261 | ||
0f4ac132 JK |
262 | if (length > part->size) { |
263 | length = part->size; | |
edc79a2f BH |
264 | } |
265 | ||
266 | info.error_type = err_type; | |
267 | info.seq_num = error_log_cnt; | |
268 | ||
0f4ac132 | 269 | tmp_index = part->index; |
edc79a2f BH |
270 | |
271 | rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index); | |
272 | if (rc <= 0) { | |
0f4ac132 | 273 | pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); |
edc79a2f BH |
274 | return rc; |
275 | } | |
276 | ||
277 | rc = ppc_md.nvram_write(buff, length, &tmp_index); | |
278 | if (rc <= 0) { | |
0f4ac132 | 279 | pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); |
edc79a2f BH |
280 | return rc; |
281 | } | |
282 | ||
283 | return 0; | |
284 | } | |
285 | ||
0f4ac132 JK |
286 | int nvram_write_error_log(char * buff, int length, |
287 | unsigned int err_type, unsigned int error_log_cnt) | |
288 | { | |
a5cf4b08 | 289 | int rc = nvram_write_os_partition(&rtas_log_partition, buff, length, |
0f4ac132 | 290 | err_type, error_log_cnt); |
a5cf4b08 JK |
291 | if (!rc) |
292 | last_unread_rtas_event = get_seconds(); | |
293 | return rc; | |
0f4ac132 JK |
294 | } |
295 | ||
12674610 | 296 | /* nvram_read_partition |
edc79a2f | 297 | * |
12674610 | 298 | * Reads nvram partition for at most 'length' |
edc79a2f | 299 | */ |
12674610 AB |
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) | |
edc79a2f BH |
303 | { |
304 | int rc; | |
305 | loff_t tmp_index; | |
306 | struct err_log_info info; | |
307 | ||
12674610 | 308 | if (part->index == -1) |
edc79a2f BH |
309 | return -1; |
310 | ||
12674610 AB |
311 | if (length > part->size) |
312 | length = part->size; | |
edc79a2f | 313 | |
12674610 | 314 | tmp_index = part->index; |
edc79a2f BH |
315 | |
316 | rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index); | |
317 | if (rc <= 0) { | |
12674610 | 318 | pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc); |
edc79a2f BH |
319 | return rc; |
320 | } | |
321 | ||
322 | rc = ppc_md.nvram_read(buff, length, &tmp_index); | |
323 | if (rc <= 0) { | |
12674610 | 324 | pr_err("%s: Failed nvram_read (%d)\n", __FUNCTION__, rc); |
edc79a2f BH |
325 | return rc; |
326 | } | |
327 | ||
328 | *error_log_cnt = info.seq_num; | |
329 | *err_type = info.error_type; | |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
12674610 AB |
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 | ||
edc79a2f BH |
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 | ||
0f4ac132 | 354 | if (rtas_log_partition.index == -1) |
edc79a2f BH |
355 | return -1; |
356 | ||
0f4ac132 | 357 | tmp_index = rtas_log_partition.index; |
edc79a2f BH |
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 | } | |
a5cf4b08 | 364 | last_unread_rtas_event = 0; |
edc79a2f BH |
365 | |
366 | return 0; | |
367 | } | |
368 | ||
0f4ac132 | 369 | /* pseries_nvram_init_os_partition |
edc79a2f | 370 | * |
0f4ac132 | 371 | * This sets up a partition with an "OS" signature. |
edc79a2f BH |
372 | * |
373 | * The general strategy is the following: | |
0f4ac132 JK |
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. | |
edc79a2f | 385 | */ |
0f4ac132 JK |
386 | static int __init pseries_nvram_init_os_partition(struct nvram_os_partition |
387 | *part) | |
edc79a2f BH |
388 | { |
389 | loff_t p; | |
390 | int size; | |
391 | ||
392 | /* Scan nvram for partitions */ | |
393 | nvram_scan_partitions(); | |
394 | ||
0f4ac132 JK |
395 | /* Look for ours */ |
396 | p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size); | |
edc79a2f BH |
397 | |
398 | /* Found one but too small, remove it */ | |
0f4ac132 JK |
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); | |
edc79a2f BH |
403 | p = 0; |
404 | } | |
405 | ||
406 | /* Create one if we didn't find */ | |
407 | if (!p) { | |
0f4ac132 JK |
408 | p = nvram_create_partition(part->name, NVRAM_SIG_OS, |
409 | part->req_size, part->min_size); | |
edc79a2f | 410 | if (p == -ENOSPC) { |
0f4ac132 JK |
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); | |
edc79a2f BH |
418 | } |
419 | } | |
420 | ||
421 | if (p <= 0) { | |
0f4ac132 JK |
422 | pr_err("nvram: Failed to find or create %s" |
423 | " partition, err %d\n", part->name, (int)p); | |
424 | return -1; | |
edc79a2f BH |
425 | } |
426 | ||
0f4ac132 JK |
427 | part->index = p; |
428 | part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info); | |
edc79a2f BH |
429 | |
430 | return 0; | |
431 | } | |
0f4ac132 | 432 | |
a5cf4b08 JK |
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); | |
6c493685 JK |
448 | if (!oops_buf) { |
449 | pr_err("nvram: No memory for %s partition\n", | |
450 | oops_log_partition.name); | |
451 | return; | |
452 | } | |
b1f70e1f AB |
453 | oops_data = oops_buf + sizeof(struct oops_log_info); |
454 | oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info); | |
6c493685 JK |
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 | ||
a5cf4b08 JK |
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); | |
6c493685 JK |
483 | kfree(big_oops_buf); |
484 | kfree(stream.workspace); | |
a5cf4b08 JK |
485 | } |
486 | } | |
487 | ||
0f4ac132 JK |
488 | static int __init pseries_nvram_init_log_partitions(void) |
489 | { | |
a5cf4b08 JK |
490 | int rc; |
491 | ||
492 | rc = pseries_nvram_init_os_partition(&rtas_log_partition); | |
493 | nvram_init_oops_partition(rc == 0); | |
0f4ac132 JK |
494 | return 0; |
495 | } | |
496 | machine_arch_initcall(pseries, pseries_nvram_init_log_partitions); | |
edc79a2f | 497 | |
1da177e4 LT |
498 | int __init pSeries_nvram_init(void) |
499 | { | |
500 | struct device_node *nvram; | |
954a46e2 JK |
501 | const unsigned int *nbytes_p; |
502 | unsigned int proplen; | |
1da177e4 LT |
503 | |
504 | nvram = of_find_node_by_type(NULL, "nvram"); | |
505 | if (nvram == NULL) | |
506 | return -ENODEV; | |
507 | ||
e2eb6392 | 508 | nbytes_p = of_get_property(nvram, "#bytes", &proplen); |
bad5232b JL |
509 | if (nbytes_p == NULL || proplen != sizeof(unsigned int)) { |
510 | of_node_put(nvram); | |
1da177e4 | 511 | return -EIO; |
bad5232b | 512 | } |
1da177e4 LT |
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 | } | |
a5cf4b08 | 527 | |
a5cf4b08 JK |
528 | /* |
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 | ||
6c493685 JK |
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 | { | |
b1f70e1f | 582 | struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; |
6c493685 JK |
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 | } | |
b1f70e1f AB |
590 | oops_hdr->version = OOPS_HDR_VERSION; |
591 | oops_hdr->report_length = (u16) zipped_len; | |
592 | oops_hdr->timestamp = get_seconds(); | |
6c493685 JK |
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 | */ | |
a5cf4b08 | 603 | static void oops_to_nvram(struct kmsg_dumper *dumper, |
e2ae715d | 604 | enum kmsg_dump_reason reason) |
a5cf4b08 | 605 | { |
b1f70e1f | 606 | struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf; |
a5cf4b08 | 607 | static unsigned int oops_count = 0; |
15d260b3 | 608 | static bool panicking = false; |
120a52c3 AB |
609 | static DEFINE_SPINLOCK(lock); |
610 | unsigned long flags; | |
a5cf4b08 | 611 | size_t text_len; |
6c493685 JK |
612 | unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ; |
613 | int rc = -1; | |
a5cf4b08 | 614 | |
15d260b3 JK |
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: | |
15d260b3 JK |
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 | ||
a5cf4b08 JK |
637 | if (clobbering_unread_rtas_event()) |
638 | return; | |
639 | ||
120a52c3 AB |
640 | if (!spin_trylock_irqsave(&lock, flags)) |
641 | return; | |
642 | ||
6c493685 | 643 | if (big_oops_buf) { |
e2ae715d KS |
644 | kmsg_dump_get_buffer(dumper, false, |
645 | big_oops_buf, big_oops_buf_sz, &text_len); | |
6c493685 JK |
646 | rc = zip_oops(text_len); |
647 | } | |
648 | if (rc != 0) { | |
e2ae715d | 649 | kmsg_dump_rewind(dumper); |
1bf247f8 | 650 | kmsg_dump_get_buffer(dumper, false, |
e2ae715d | 651 | oops_data, oops_data_sz, &text_len); |
6c493685 | 652 | err_type = ERR_TYPE_KERNEL_PANIC; |
b1f70e1f AB |
653 | oops_hdr->version = OOPS_HDR_VERSION; |
654 | oops_hdr->report_length = (u16) text_len; | |
655 | oops_hdr->timestamp = get_seconds(); | |
6c493685 JK |
656 | } |
657 | ||
a5cf4b08 | 658 | (void) nvram_write_os_partition(&oops_log_partition, oops_buf, |
b1f70e1f AB |
659 | (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type, |
660 | ++oops_count); | |
120a52c3 AB |
661 | |
662 | spin_unlock_irqrestore(&lock, flags); | |
a5cf4b08 | 663 | } |