nvram.c 22 KB

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