nvram.c 22 KB

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