nvram.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. /* Derived from logfs_compress() */
  412. static int nvram_compress(const void *in, void *out, size_t inlen,
  413. size_t outlen)
  414. {
  415. int err, ret;
  416. ret = -EIO;
  417. err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
  418. MEM_LEVEL, Z_DEFAULT_STRATEGY);
  419. if (err != Z_OK)
  420. goto error;
  421. stream.next_in = in;
  422. stream.avail_in = inlen;
  423. stream.total_in = 0;
  424. stream.next_out = out;
  425. stream.avail_out = outlen;
  426. stream.total_out = 0;
  427. err = zlib_deflate(&stream, Z_FINISH);
  428. if (err != Z_STREAM_END)
  429. goto error;
  430. err = zlib_deflateEnd(&stream);
  431. if (err != Z_OK)
  432. goto error;
  433. if (stream.total_out >= stream.total_in)
  434. goto error;
  435. ret = stream.total_out;
  436. error:
  437. return ret;
  438. }
  439. /* Compress the text from big_oops_buf into oops_buf. */
  440. static int zip_oops(size_t text_len)
  441. {
  442. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  443. int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
  444. oops_data_sz);
  445. if (zipped_len < 0) {
  446. pr_err("nvram: compression failed; returned %d\n", zipped_len);
  447. pr_err("nvram: logging uncompressed oops/panic report\n");
  448. return -1;
  449. }
  450. oops_hdr->version = OOPS_HDR_VERSION;
  451. oops_hdr->report_length = (u16) zipped_len;
  452. oops_hdr->timestamp = get_seconds();
  453. return 0;
  454. }
  455. #ifdef CONFIG_PSTORE
  456. /* Derived from logfs_uncompress */
  457. int nvram_decompress(void *in, void *out, size_t inlen, size_t outlen)
  458. {
  459. int err, ret;
  460. ret = -EIO;
  461. err = zlib_inflateInit(&stream);
  462. if (err != Z_OK)
  463. goto error;
  464. stream.next_in = in;
  465. stream.avail_in = inlen;
  466. stream.total_in = 0;
  467. stream.next_out = out;
  468. stream.avail_out = outlen;
  469. stream.total_out = 0;
  470. err = zlib_inflate(&stream, Z_FINISH);
  471. if (err != Z_STREAM_END)
  472. goto error;
  473. err = zlib_inflateEnd(&stream);
  474. if (err != Z_OK)
  475. goto error;
  476. ret = stream.total_out;
  477. error:
  478. return ret;
  479. }
  480. static int unzip_oops(char *oops_buf, char *big_buf)
  481. {
  482. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  483. u64 timestamp = oops_hdr->timestamp;
  484. char *big_oops_data = NULL;
  485. char *oops_data_buf = NULL;
  486. size_t big_oops_data_sz;
  487. int unzipped_len;
  488. big_oops_data = big_buf + sizeof(struct oops_log_info);
  489. big_oops_data_sz = big_oops_buf_sz - sizeof(struct oops_log_info);
  490. oops_data_buf = oops_buf + sizeof(struct oops_log_info);
  491. unzipped_len = nvram_decompress(oops_data_buf, big_oops_data,
  492. oops_hdr->report_length,
  493. big_oops_data_sz);
  494. if (unzipped_len < 0) {
  495. pr_err("nvram: decompression failed; returned %d\n",
  496. unzipped_len);
  497. return -1;
  498. }
  499. oops_hdr = (struct oops_log_info *)big_buf;
  500. oops_hdr->version = OOPS_HDR_VERSION;
  501. oops_hdr->report_length = (u16) unzipped_len;
  502. oops_hdr->timestamp = timestamp;
  503. return 0;
  504. }
  505. static int nvram_pstore_open(struct pstore_info *psi)
  506. {
  507. /* Reset the iterator to start reading partitions again */
  508. read_type = -1;
  509. return 0;
  510. }
  511. /**
  512. * nvram_pstore_write - pstore write callback for nvram
  513. * @type: Type of message logged
  514. * @reason: reason behind dump (oops/panic)
  515. * @id: identifier to indicate the write performed
  516. * @part: pstore writes data to registered buffer in parts,
  517. * part number will indicate the same.
  518. * @count: Indicates oops count
  519. * @hsize: Size of header added by pstore
  520. * @size: number of bytes written to the registered buffer
  521. * @psi: registered pstore_info structure
  522. *
  523. * Called by pstore_dump() when an oops or panic report is logged in the
  524. * printk buffer.
  525. * Returns 0 on successful write.
  526. */
  527. static int nvram_pstore_write(enum pstore_type_id type,
  528. enum kmsg_dump_reason reason,
  529. u64 *id, unsigned int part, int count,
  530. size_t hsize, size_t size,
  531. struct pstore_info *psi)
  532. {
  533. int rc;
  534. unsigned int err_type = ERR_TYPE_KERNEL_PANIC;
  535. struct oops_log_info *oops_hdr = (struct oops_log_info *) oops_buf;
  536. /* part 1 has the recent messages from printk buffer */
  537. if (part > 1 || type != PSTORE_TYPE_DMESG ||
  538. clobbering_unread_rtas_event())
  539. return -1;
  540. oops_hdr->version = OOPS_HDR_VERSION;
  541. oops_hdr->report_length = (u16) size;
  542. oops_hdr->timestamp = get_seconds();
  543. if (big_oops_buf) {
  544. rc = zip_oops(size);
  545. /*
  546. * If compression fails copy recent log messages from
  547. * big_oops_buf to oops_data.
  548. */
  549. if (rc != 0) {
  550. size_t diff = size - oops_data_sz + hsize;
  551. if (size > oops_data_sz) {
  552. memcpy(oops_data, big_oops_buf, hsize);
  553. memcpy(oops_data + hsize, big_oops_buf + diff,
  554. oops_data_sz - hsize);
  555. oops_hdr->report_length = (u16) oops_data_sz;
  556. } else
  557. memcpy(oops_data, big_oops_buf, size);
  558. } else
  559. err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  560. }
  561. rc = nvram_write_os_partition(&oops_log_partition, oops_buf,
  562. (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type,
  563. count);
  564. if (rc != 0)
  565. return rc;
  566. *id = part;
  567. return 0;
  568. }
  569. /*
  570. * Reads the oops/panic report, rtas, of-config and common partition.
  571. * Returns the length of the data we read from each partition.
  572. * Returns 0 if we've been called before.
  573. */
  574. static ssize_t nvram_pstore_read(u64 *id, enum pstore_type_id *type,
  575. int *count, struct timespec *time, char **buf,
  576. struct pstore_info *psi)
  577. {
  578. struct oops_log_info *oops_hdr;
  579. unsigned int err_type, id_no, size = 0;
  580. struct nvram_os_partition *part = NULL;
  581. char *buff = NULL, *big_buff = NULL;
  582. int rc, sig = 0;
  583. loff_t p;
  584. read_partition:
  585. read_type++;
  586. switch (nvram_type_ids[read_type]) {
  587. case PSTORE_TYPE_DMESG:
  588. part = &oops_log_partition;
  589. *type = PSTORE_TYPE_DMESG;
  590. break;
  591. case PSTORE_TYPE_PPC_RTAS:
  592. part = &rtas_log_partition;
  593. *type = PSTORE_TYPE_PPC_RTAS;
  594. time->tv_sec = last_rtas_event;
  595. time->tv_nsec = 0;
  596. break;
  597. case PSTORE_TYPE_PPC_OF:
  598. sig = NVRAM_SIG_OF;
  599. part = &of_config_partition;
  600. *type = PSTORE_TYPE_PPC_OF;
  601. *id = PSTORE_TYPE_PPC_OF;
  602. time->tv_sec = 0;
  603. time->tv_nsec = 0;
  604. break;
  605. case PSTORE_TYPE_PPC_COMMON:
  606. sig = NVRAM_SIG_SYS;
  607. part = &common_partition;
  608. *type = PSTORE_TYPE_PPC_COMMON;
  609. *id = PSTORE_TYPE_PPC_COMMON;
  610. time->tv_sec = 0;
  611. time->tv_nsec = 0;
  612. break;
  613. default:
  614. return 0;
  615. }
  616. if (!part->os_partition) {
  617. p = nvram_find_partition(part->name, sig, &size);
  618. if (p <= 0) {
  619. pr_err("nvram: Failed to find partition %s, "
  620. "err %d\n", part->name, (int)p);
  621. return 0;
  622. }
  623. part->index = p;
  624. part->size = size;
  625. }
  626. buff = kmalloc(part->size, GFP_KERNEL);
  627. if (!buff)
  628. return -ENOMEM;
  629. if (nvram_read_partition(part, buff, part->size, &err_type, &id_no)) {
  630. kfree(buff);
  631. return 0;
  632. }
  633. *count = 0;
  634. if (part->os_partition)
  635. *id = id_no;
  636. if (nvram_type_ids[read_type] == PSTORE_TYPE_DMESG) {
  637. oops_hdr = (struct oops_log_info *)buff;
  638. *buf = buff + sizeof(*oops_hdr);
  639. if (err_type == ERR_TYPE_KERNEL_PANIC_GZ) {
  640. big_buff = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  641. if (!big_buff)
  642. return -ENOMEM;
  643. rc = unzip_oops(buff, big_buff);
  644. if (rc != 0) {
  645. kfree(buff);
  646. kfree(big_buff);
  647. goto read_partition;
  648. }
  649. oops_hdr = (struct oops_log_info *)big_buff;
  650. *buf = big_buff + sizeof(*oops_hdr);
  651. kfree(buff);
  652. }
  653. time->tv_sec = oops_hdr->timestamp;
  654. time->tv_nsec = 0;
  655. return oops_hdr->report_length;
  656. }
  657. *buf = buff;
  658. return part->size;
  659. }
  660. static struct pstore_info nvram_pstore_info = {
  661. .owner = THIS_MODULE,
  662. .name = "nvram",
  663. .open = nvram_pstore_open,
  664. .read = nvram_pstore_read,
  665. .write = nvram_pstore_write,
  666. };
  667. static int nvram_pstore_init(void)
  668. {
  669. int rc = 0;
  670. if (big_oops_buf) {
  671. nvram_pstore_info.buf = big_oops_buf;
  672. nvram_pstore_info.bufsize = big_oops_buf_sz;
  673. } else {
  674. nvram_pstore_info.buf = oops_data;
  675. nvram_pstore_info.bufsize = oops_data_sz;
  676. }
  677. rc = pstore_register(&nvram_pstore_info);
  678. if (rc != 0)
  679. pr_err("nvram: pstore_register() failed, defaults to "
  680. "kmsg_dump; returned %d\n", rc);
  681. return rc;
  682. }
  683. #else
  684. static int nvram_pstore_init(void)
  685. {
  686. return -1;
  687. }
  688. #endif
  689. static void __init nvram_init_oops_partition(int rtas_partition_exists)
  690. {
  691. int rc;
  692. rc = pseries_nvram_init_os_partition(&oops_log_partition);
  693. if (rc != 0) {
  694. if (!rtas_partition_exists)
  695. return;
  696. pr_notice("nvram: Using %s partition to log both"
  697. " RTAS errors and oops/panic reports\n",
  698. rtas_log_partition.name);
  699. memcpy(&oops_log_partition, &rtas_log_partition,
  700. sizeof(rtas_log_partition));
  701. }
  702. oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
  703. if (!oops_buf) {
  704. pr_err("nvram: No memory for %s partition\n",
  705. oops_log_partition.name);
  706. return;
  707. }
  708. oops_data = oops_buf + sizeof(struct oops_log_info);
  709. oops_data_sz = oops_log_partition.size - sizeof(struct oops_log_info);
  710. /*
  711. * Figure compression (preceded by elimination of each line's <n>
  712. * severity prefix) will reduce the oops/panic report to at most
  713. * 45% of its original size.
  714. */
  715. big_oops_buf_sz = (oops_data_sz * 100) / 45;
  716. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  717. if (big_oops_buf) {
  718. stream.workspace = kmalloc(zlib_deflate_workspacesize(
  719. WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
  720. if (!stream.workspace) {
  721. pr_err("nvram: No memory for compression workspace; "
  722. "skipping compression of %s partition data\n",
  723. oops_log_partition.name);
  724. kfree(big_oops_buf);
  725. big_oops_buf = NULL;
  726. }
  727. } else {
  728. pr_err("No memory for uncompressed %s data; "
  729. "skipping compression\n", oops_log_partition.name);
  730. stream.workspace = NULL;
  731. }
  732. rc = nvram_pstore_init();
  733. if (!rc)
  734. return;
  735. rc = kmsg_dump_register(&nvram_kmsg_dumper);
  736. if (rc != 0) {
  737. pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
  738. kfree(oops_buf);
  739. kfree(big_oops_buf);
  740. kfree(stream.workspace);
  741. }
  742. }
  743. static int __init pseries_nvram_init_log_partitions(void)
  744. {
  745. int rc;
  746. rc = pseries_nvram_init_os_partition(&rtas_log_partition);
  747. nvram_init_oops_partition(rc == 0);
  748. return 0;
  749. }
  750. machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
  751. int __init pSeries_nvram_init(void)
  752. {
  753. struct device_node *nvram;
  754. const unsigned int *nbytes_p;
  755. unsigned int proplen;
  756. nvram = of_find_node_by_type(NULL, "nvram");
  757. if (nvram == NULL)
  758. return -ENODEV;
  759. nbytes_p = of_get_property(nvram, "#bytes", &proplen);
  760. if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
  761. of_node_put(nvram);
  762. return -EIO;
  763. }
  764. nvram_size = *nbytes_p;
  765. nvram_fetch = rtas_token("nvram-fetch");
  766. nvram_store = rtas_token("nvram-store");
  767. printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
  768. of_node_put(nvram);
  769. ppc_md.nvram_read = pSeries_nvram_read;
  770. ppc_md.nvram_write = pSeries_nvram_write;
  771. ppc_md.nvram_size = pSeries_nvram_get_size;
  772. return 0;
  773. }
  774. /*
  775. * This is our kmsg_dump callback, called after an oops or panic report
  776. * has been written to the printk buffer. We want to capture as much
  777. * of the printk buffer as possible. First, capture as much as we can
  778. * that we think will compress sufficiently to fit in the lnx,oops-log
  779. * partition. If that's too much, go back and capture uncompressed text.
  780. */
  781. static void oops_to_nvram(struct kmsg_dumper *dumper,
  782. enum kmsg_dump_reason reason)
  783. {
  784. struct oops_log_info *oops_hdr = (struct oops_log_info *)oops_buf;
  785. static unsigned int oops_count = 0;
  786. static bool panicking = false;
  787. static DEFINE_SPINLOCK(lock);
  788. unsigned long flags;
  789. size_t text_len;
  790. unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  791. int rc = -1;
  792. switch (reason) {
  793. case KMSG_DUMP_RESTART:
  794. case KMSG_DUMP_HALT:
  795. case KMSG_DUMP_POWEROFF:
  796. /* These are almost always orderly shutdowns. */
  797. return;
  798. case KMSG_DUMP_OOPS:
  799. break;
  800. case KMSG_DUMP_PANIC:
  801. panicking = true;
  802. break;
  803. case KMSG_DUMP_EMERG:
  804. if (panicking)
  805. /* Panic report already captured. */
  806. return;
  807. break;
  808. default:
  809. pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
  810. __FUNCTION__, (int) reason);
  811. return;
  812. }
  813. if (clobbering_unread_rtas_event())
  814. return;
  815. if (!spin_trylock_irqsave(&lock, flags))
  816. return;
  817. if (big_oops_buf) {
  818. kmsg_dump_get_buffer(dumper, false,
  819. big_oops_buf, big_oops_buf_sz, &text_len);
  820. rc = zip_oops(text_len);
  821. }
  822. if (rc != 0) {
  823. kmsg_dump_rewind(dumper);
  824. kmsg_dump_get_buffer(dumper, false,
  825. oops_data, oops_data_sz, &text_len);
  826. err_type = ERR_TYPE_KERNEL_PANIC;
  827. oops_hdr->version = OOPS_HDR_VERSION;
  828. oops_hdr->report_length = (u16) text_len;
  829. oops_hdr->timestamp = get_seconds();
  830. }
  831. (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
  832. (int) (sizeof(*oops_hdr) + oops_hdr->report_length), err_type,
  833. ++oops_count);
  834. spin_unlock_irqrestore(&lock, flags);
  835. }