nvram.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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/ctype.h>
  20. #include <linux/zlib.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/nvram.h>
  23. #include <asm/rtas.h>
  24. #include <asm/prom.h>
  25. #include <asm/machdep.h>
  26. /* Max bytes to read/write in one go */
  27. #define NVRW_CNT 0x20
  28. static unsigned int nvram_size;
  29. static int nvram_fetch, nvram_store;
  30. static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
  31. static DEFINE_SPINLOCK(nvram_lock);
  32. struct err_log_info {
  33. int error_type;
  34. unsigned int seq_num;
  35. };
  36. struct nvram_os_partition {
  37. const char *name;
  38. int req_size; /* desired size, in bytes */
  39. int min_size; /* minimum acceptable size (0 means req_size) */
  40. long size; /* size of data portion (excluding err_log_info) */
  41. long index; /* offset of data portion of partition */
  42. };
  43. static struct nvram_os_partition rtas_log_partition = {
  44. .name = "ibm,rtas-log",
  45. .req_size = 2079,
  46. .min_size = 1055,
  47. .index = -1
  48. };
  49. static struct nvram_os_partition oops_log_partition = {
  50. .name = "lnx,oops-log",
  51. .req_size = 4000,
  52. .min_size = 2000,
  53. .index = -1
  54. };
  55. static const char *pseries_nvram_os_partitions[] = {
  56. "ibm,rtas-log",
  57. "lnx,oops-log",
  58. NULL
  59. };
  60. static void oops_to_nvram(struct kmsg_dumper *dumper,
  61. enum kmsg_dump_reason reason);
  62. static struct kmsg_dumper nvram_kmsg_dumper = {
  63. .dump = oops_to_nvram
  64. };
  65. /* See clobbering_unread_rtas_event() */
  66. #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */
  67. static unsigned long last_unread_rtas_event; /* timestamp */
  68. /*
  69. * For capturing and compressing an oops or panic report...
  70. * big_oops_buf[] holds the uncompressed text we're capturing.
  71. *
  72. * oops_buf[] holds the compressed text, preceded by a prefix.
  73. * The prefix is just a u16 holding the length of the compressed* text.
  74. * (*Or uncompressed, if compression fails.) oops_buf[] gets written
  75. * to NVRAM.
  76. *
  77. * oops_len points to the prefix. oops_data points to the compressed text.
  78. *
  79. * +- oops_buf
  80. * | +- oops_data
  81. * v v
  82. * +------------+-----------------------------------------------+
  83. * | length | text |
  84. * | (2 bytes) | (oops_data_sz bytes) |
  85. * +------------+-----------------------------------------------+
  86. * ^
  87. * +- oops_len
  88. *
  89. * We preallocate these buffers during init to avoid kmalloc during oops/panic.
  90. */
  91. static size_t big_oops_buf_sz;
  92. static char *big_oops_buf, *oops_buf;
  93. static u16 *oops_len;
  94. static char *oops_data;
  95. static size_t oops_data_sz;
  96. /* Compression parameters */
  97. #define COMPR_LEVEL 6
  98. #define WINDOW_BITS 12
  99. #define MEM_LEVEL 4
  100. static struct z_stream_s stream;
  101. static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
  102. {
  103. unsigned int i;
  104. unsigned long len;
  105. int done;
  106. unsigned long flags;
  107. char *p = buf;
  108. if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
  109. return -ENODEV;
  110. if (*index >= nvram_size)
  111. return 0;
  112. i = *index;
  113. if (i + count > nvram_size)
  114. count = nvram_size - i;
  115. spin_lock_irqsave(&nvram_lock, flags);
  116. for (; count != 0; count -= len) {
  117. len = count;
  118. if (len > NVRW_CNT)
  119. len = NVRW_CNT;
  120. if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
  121. len) != 0) || len != done) {
  122. spin_unlock_irqrestore(&nvram_lock, flags);
  123. return -EIO;
  124. }
  125. memcpy(p, nvram_buf, len);
  126. p += len;
  127. i += len;
  128. }
  129. spin_unlock_irqrestore(&nvram_lock, flags);
  130. *index = i;
  131. return p - buf;
  132. }
  133. static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
  134. {
  135. unsigned int i;
  136. unsigned long len;
  137. int done;
  138. unsigned long flags;
  139. const char *p = buf;
  140. if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
  141. return -ENODEV;
  142. if (*index >= nvram_size)
  143. return 0;
  144. i = *index;
  145. if (i + count > nvram_size)
  146. count = nvram_size - i;
  147. spin_lock_irqsave(&nvram_lock, flags);
  148. for (; count != 0; count -= len) {
  149. len = count;
  150. if (len > NVRW_CNT)
  151. len = NVRW_CNT;
  152. memcpy(nvram_buf, p, len);
  153. if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
  154. len) != 0) || len != done) {
  155. spin_unlock_irqrestore(&nvram_lock, flags);
  156. return -EIO;
  157. }
  158. p += len;
  159. i += len;
  160. }
  161. spin_unlock_irqrestore(&nvram_lock, flags);
  162. *index = i;
  163. return p - buf;
  164. }
  165. static ssize_t pSeries_nvram_get_size(void)
  166. {
  167. return nvram_size ? nvram_size : -ENODEV;
  168. }
  169. /* nvram_write_os_partition, nvram_write_error_log
  170. *
  171. * We need to buffer the error logs into nvram to ensure that we have
  172. * the failure information to decode. If we have a severe error there
  173. * is no way to guarantee that the OS or the machine is in a state to
  174. * get back to user land and write the error to disk. For example if
  175. * the SCSI device driver causes a Machine Check by writing to a bad
  176. * IO address, there is no way of guaranteeing that the device driver
  177. * is in any state that is would also be able to write the error data
  178. * captured to disk, thus we buffer it in NVRAM for analysis on the
  179. * next boot.
  180. *
  181. * In NVRAM the partition containing the error log buffer will looks like:
  182. * Header (in bytes):
  183. * +-----------+----------+--------+------------+------------------+
  184. * | signature | checksum | length | name | data |
  185. * |0 |1 |2 3|4 15|16 length-1|
  186. * +-----------+----------+--------+------------+------------------+
  187. *
  188. * The 'data' section would look like (in bytes):
  189. * +--------------+------------+-----------------------------------+
  190. * | event_logged | sequence # | error log |
  191. * |0 3|4 7|8 error_log_size-1|
  192. * +--------------+------------+-----------------------------------+
  193. *
  194. * event_logged: 0 if event has not been logged to syslog, 1 if it has
  195. * sequence #: The unique sequence # for each event. (until it wraps)
  196. * error log: The error log from event_scan
  197. */
  198. int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
  199. int length, unsigned int err_type, unsigned int error_log_cnt)
  200. {
  201. int rc;
  202. loff_t tmp_index;
  203. struct err_log_info info;
  204. if (part->index == -1) {
  205. return -ESPIPE;
  206. }
  207. if (length > part->size) {
  208. length = part->size;
  209. }
  210. info.error_type = err_type;
  211. info.seq_num = error_log_cnt;
  212. tmp_index = part->index;
  213. rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
  214. if (rc <= 0) {
  215. pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
  216. return rc;
  217. }
  218. rc = ppc_md.nvram_write(buff, length, &tmp_index);
  219. if (rc <= 0) {
  220. pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
  221. return rc;
  222. }
  223. return 0;
  224. }
  225. int nvram_write_error_log(char * buff, int length,
  226. unsigned int err_type, unsigned int error_log_cnt)
  227. {
  228. int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
  229. err_type, error_log_cnt);
  230. if (!rc)
  231. last_unread_rtas_event = get_seconds();
  232. return rc;
  233. }
  234. /* nvram_read_error_log
  235. *
  236. * Reads nvram for error log for at most 'length'
  237. */
  238. int nvram_read_error_log(char * buff, int length,
  239. unsigned int * err_type, unsigned int * error_log_cnt)
  240. {
  241. int rc;
  242. loff_t tmp_index;
  243. struct err_log_info info;
  244. if (rtas_log_partition.index == -1)
  245. return -1;
  246. if (length > rtas_log_partition.size)
  247. length = rtas_log_partition.size;
  248. tmp_index = rtas_log_partition.index;
  249. rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
  250. if (rc <= 0) {
  251. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  252. return rc;
  253. }
  254. rc = ppc_md.nvram_read(buff, length, &tmp_index);
  255. if (rc <= 0) {
  256. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  257. return rc;
  258. }
  259. *error_log_cnt = info.seq_num;
  260. *err_type = info.error_type;
  261. return 0;
  262. }
  263. /* This doesn't actually zero anything, but it sets the event_logged
  264. * word to tell that this event is safely in syslog.
  265. */
  266. int nvram_clear_error_log(void)
  267. {
  268. loff_t tmp_index;
  269. int clear_word = ERR_FLAG_ALREADY_LOGGED;
  270. int rc;
  271. if (rtas_log_partition.index == -1)
  272. return -1;
  273. tmp_index = rtas_log_partition.index;
  274. rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
  275. if (rc <= 0) {
  276. printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
  277. return rc;
  278. }
  279. last_unread_rtas_event = 0;
  280. return 0;
  281. }
  282. /* pseries_nvram_init_os_partition
  283. *
  284. * This sets up a partition with an "OS" signature.
  285. *
  286. * The general strategy is the following:
  287. * 1.) If a partition with the indicated name already exists...
  288. * - If it's large enough, use it.
  289. * - Otherwise, recycle it and keep going.
  290. * 2.) Search for a free partition that is large enough.
  291. * 3.) If there's not a free partition large enough, recycle any obsolete
  292. * OS partitions and try again.
  293. * 4.) Will first try getting a chunk that will satisfy the requested size.
  294. * 5.) If a chunk of the requested size cannot be allocated, then try finding
  295. * a chunk that will satisfy the minum needed.
  296. *
  297. * Returns 0 on success, else -1.
  298. */
  299. static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
  300. *part)
  301. {
  302. loff_t p;
  303. int size;
  304. /* Scan nvram for partitions */
  305. nvram_scan_partitions();
  306. /* Look for ours */
  307. p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
  308. /* Found one but too small, remove it */
  309. if (p && size < part->min_size) {
  310. pr_info("nvram: Found too small %s partition,"
  311. " removing it...\n", part->name);
  312. nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
  313. p = 0;
  314. }
  315. /* Create one if we didn't find */
  316. if (!p) {
  317. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  318. part->req_size, part->min_size);
  319. if (p == -ENOSPC) {
  320. pr_info("nvram: No room to create %s partition, "
  321. "deleting any obsolete OS partitions...\n",
  322. part->name);
  323. nvram_remove_partition(NULL, NVRAM_SIG_OS,
  324. pseries_nvram_os_partitions);
  325. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  326. part->req_size, part->min_size);
  327. }
  328. }
  329. if (p <= 0) {
  330. pr_err("nvram: Failed to find or create %s"
  331. " partition, err %d\n", part->name, (int)p);
  332. return -1;
  333. }
  334. part->index = p;
  335. part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
  336. return 0;
  337. }
  338. static void __init nvram_init_oops_partition(int rtas_partition_exists)
  339. {
  340. int rc;
  341. rc = pseries_nvram_init_os_partition(&oops_log_partition);
  342. if (rc != 0) {
  343. if (!rtas_partition_exists)
  344. return;
  345. pr_notice("nvram: Using %s partition to log both"
  346. " RTAS errors and oops/panic reports\n",
  347. rtas_log_partition.name);
  348. memcpy(&oops_log_partition, &rtas_log_partition,
  349. sizeof(rtas_log_partition));
  350. }
  351. oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
  352. if (!oops_buf) {
  353. pr_err("nvram: No memory for %s partition\n",
  354. oops_log_partition.name);
  355. return;
  356. }
  357. oops_len = (u16*) oops_buf;
  358. oops_data = oops_buf + sizeof(u16);
  359. oops_data_sz = oops_log_partition.size - sizeof(u16);
  360. /*
  361. * Figure compression (preceded by elimination of each line's <n>
  362. * severity prefix) will reduce the oops/panic report to at most
  363. * 45% of its original size.
  364. */
  365. big_oops_buf_sz = (oops_data_sz * 100) / 45;
  366. big_oops_buf = kmalloc(big_oops_buf_sz, GFP_KERNEL);
  367. if (big_oops_buf) {
  368. stream.workspace = kmalloc(zlib_deflate_workspacesize(
  369. WINDOW_BITS, MEM_LEVEL), GFP_KERNEL);
  370. if (!stream.workspace) {
  371. pr_err("nvram: No memory for compression workspace; "
  372. "skipping compression of %s partition data\n",
  373. oops_log_partition.name);
  374. kfree(big_oops_buf);
  375. big_oops_buf = NULL;
  376. }
  377. } else {
  378. pr_err("No memory for uncompressed %s data; "
  379. "skipping compression\n", oops_log_partition.name);
  380. stream.workspace = NULL;
  381. }
  382. rc = kmsg_dump_register(&nvram_kmsg_dumper);
  383. if (rc != 0) {
  384. pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
  385. kfree(oops_buf);
  386. kfree(big_oops_buf);
  387. kfree(stream.workspace);
  388. }
  389. }
  390. static int __init pseries_nvram_init_log_partitions(void)
  391. {
  392. int rc;
  393. rc = pseries_nvram_init_os_partition(&rtas_log_partition);
  394. nvram_init_oops_partition(rc == 0);
  395. return 0;
  396. }
  397. machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
  398. int __init pSeries_nvram_init(void)
  399. {
  400. struct device_node *nvram;
  401. const unsigned int *nbytes_p;
  402. unsigned int proplen;
  403. nvram = of_find_node_by_type(NULL, "nvram");
  404. if (nvram == NULL)
  405. return -ENODEV;
  406. nbytes_p = of_get_property(nvram, "#bytes", &proplen);
  407. if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
  408. of_node_put(nvram);
  409. return -EIO;
  410. }
  411. nvram_size = *nbytes_p;
  412. nvram_fetch = rtas_token("nvram-fetch");
  413. nvram_store = rtas_token("nvram-store");
  414. printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
  415. of_node_put(nvram);
  416. ppc_md.nvram_read = pSeries_nvram_read;
  417. ppc_md.nvram_write = pSeries_nvram_write;
  418. ppc_md.nvram_size = pSeries_nvram_get_size;
  419. return 0;
  420. }
  421. /*
  422. * Are we using the ibm,rtas-log for oops/panic reports? And if so,
  423. * would logging this oops/panic overwrite an RTAS event that rtas_errd
  424. * hasn't had a chance to read and process? Return 1 if so, else 0.
  425. *
  426. * We assume that if rtas_errd hasn't read the RTAS event in
  427. * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
  428. */
  429. static int clobbering_unread_rtas_event(void)
  430. {
  431. return (oops_log_partition.index == rtas_log_partition.index
  432. && last_unread_rtas_event
  433. && get_seconds() - last_unread_rtas_event <=
  434. NVRAM_RTAS_READ_TIMEOUT);
  435. }
  436. /* Derived from logfs_compress() */
  437. static int nvram_compress(const void *in, void *out, size_t inlen,
  438. size_t outlen)
  439. {
  440. int err, ret;
  441. ret = -EIO;
  442. err = zlib_deflateInit2(&stream, COMPR_LEVEL, Z_DEFLATED, WINDOW_BITS,
  443. MEM_LEVEL, Z_DEFAULT_STRATEGY);
  444. if (err != Z_OK)
  445. goto error;
  446. stream.next_in = in;
  447. stream.avail_in = inlen;
  448. stream.total_in = 0;
  449. stream.next_out = out;
  450. stream.avail_out = outlen;
  451. stream.total_out = 0;
  452. err = zlib_deflate(&stream, Z_FINISH);
  453. if (err != Z_STREAM_END)
  454. goto error;
  455. err = zlib_deflateEnd(&stream);
  456. if (err != Z_OK)
  457. goto error;
  458. if (stream.total_out >= stream.total_in)
  459. goto error;
  460. ret = stream.total_out;
  461. error:
  462. return ret;
  463. }
  464. /* Compress the text from big_oops_buf into oops_buf. */
  465. static int zip_oops(size_t text_len)
  466. {
  467. int zipped_len = nvram_compress(big_oops_buf, oops_data, text_len,
  468. oops_data_sz);
  469. if (zipped_len < 0) {
  470. pr_err("nvram: compression failed; returned %d\n", zipped_len);
  471. pr_err("nvram: logging uncompressed oops/panic report\n");
  472. return -1;
  473. }
  474. *oops_len = (u16) zipped_len;
  475. return 0;
  476. }
  477. /*
  478. * This is our kmsg_dump callback, called after an oops or panic report
  479. * has been written to the printk buffer. We want to capture as much
  480. * of the printk buffer as possible. First, capture as much as we can
  481. * that we think will compress sufficiently to fit in the lnx,oops-log
  482. * partition. If that's too much, go back and capture uncompressed text.
  483. */
  484. static void oops_to_nvram(struct kmsg_dumper *dumper,
  485. enum kmsg_dump_reason reason)
  486. {
  487. static unsigned int oops_count = 0;
  488. static bool panicking = false;
  489. static DEFINE_SPINLOCK(lock);
  490. unsigned long flags;
  491. size_t text_len;
  492. unsigned int err_type = ERR_TYPE_KERNEL_PANIC_GZ;
  493. int rc = -1;
  494. switch (reason) {
  495. case KMSG_DUMP_RESTART:
  496. case KMSG_DUMP_HALT:
  497. case KMSG_DUMP_POWEROFF:
  498. /* These are almost always orderly shutdowns. */
  499. return;
  500. case KMSG_DUMP_OOPS:
  501. break;
  502. case KMSG_DUMP_PANIC:
  503. panicking = true;
  504. break;
  505. case KMSG_DUMP_EMERG:
  506. if (panicking)
  507. /* Panic report already captured. */
  508. return;
  509. break;
  510. default:
  511. pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
  512. __FUNCTION__, (int) reason);
  513. return;
  514. }
  515. if (clobbering_unread_rtas_event())
  516. return;
  517. if (!spin_trylock_irqsave(&lock, flags))
  518. return;
  519. if (big_oops_buf) {
  520. kmsg_dump_get_buffer(dumper, false,
  521. big_oops_buf, big_oops_buf_sz, &text_len);
  522. rc = zip_oops(text_len);
  523. }
  524. if (rc != 0) {
  525. kmsg_dump_rewind(dumper);
  526. kmsg_dump_get_buffer(dumper, true,
  527. oops_data, oops_data_sz, &text_len);
  528. err_type = ERR_TYPE_KERNEL_PANIC;
  529. *oops_len = (u16) text_len;
  530. }
  531. (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
  532. (int) (sizeof(*oops_len) + *oops_len), err_type, ++oops_count);
  533. spin_unlock_irqrestore(&lock, flags);
  534. }