nvram.c 21 KB

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