nvram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 <asm/uaccess.h>
  20. #include <asm/nvram.h>
  21. #include <asm/rtas.h>
  22. #include <asm/prom.h>
  23. #include <asm/machdep.h>
  24. /* Max bytes to read/write in one go */
  25. #define NVRW_CNT 0x20
  26. static unsigned int nvram_size;
  27. static int nvram_fetch, nvram_store;
  28. static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */
  29. static DEFINE_SPINLOCK(nvram_lock);
  30. struct err_log_info {
  31. int error_type;
  32. unsigned int seq_num;
  33. };
  34. struct nvram_os_partition {
  35. const char *name;
  36. int req_size; /* desired size, in bytes */
  37. int min_size; /* minimum acceptable size (0 means req_size) */
  38. long size; /* size of data portion (excluding err_log_info) */
  39. long index; /* offset of data portion of partition */
  40. };
  41. static struct nvram_os_partition rtas_log_partition = {
  42. .name = "ibm,rtas-log",
  43. .req_size = 2079,
  44. .min_size = 1055,
  45. .index = -1
  46. };
  47. static struct nvram_os_partition oops_log_partition = {
  48. .name = "lnx,oops-log",
  49. .req_size = 4000,
  50. .min_size = 2000,
  51. .index = -1
  52. };
  53. static const char *pseries_nvram_os_partitions[] = {
  54. "ibm,rtas-log",
  55. "lnx,oops-log",
  56. NULL
  57. };
  58. static void oops_to_nvram(struct kmsg_dumper *dumper,
  59. enum kmsg_dump_reason reason,
  60. const char *old_msgs, unsigned long old_len,
  61. const char *new_msgs, unsigned long new_len);
  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. /* We preallocate oops_buf during init to avoid kmalloc during oops/panic. */
  69. static char *oops_buf;
  70. static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index)
  71. {
  72. unsigned int i;
  73. unsigned long len;
  74. int done;
  75. unsigned long flags;
  76. char *p = buf;
  77. if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE)
  78. return -ENODEV;
  79. if (*index >= nvram_size)
  80. return 0;
  81. i = *index;
  82. if (i + count > nvram_size)
  83. count = nvram_size - i;
  84. spin_lock_irqsave(&nvram_lock, flags);
  85. for (; count != 0; count -= len) {
  86. len = count;
  87. if (len > NVRW_CNT)
  88. len = NVRW_CNT;
  89. if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf),
  90. len) != 0) || len != done) {
  91. spin_unlock_irqrestore(&nvram_lock, flags);
  92. return -EIO;
  93. }
  94. memcpy(p, nvram_buf, len);
  95. p += len;
  96. i += len;
  97. }
  98. spin_unlock_irqrestore(&nvram_lock, flags);
  99. *index = i;
  100. return p - buf;
  101. }
  102. static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index)
  103. {
  104. unsigned int i;
  105. unsigned long len;
  106. int done;
  107. unsigned long flags;
  108. const char *p = buf;
  109. if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE)
  110. return -ENODEV;
  111. if (*index >= nvram_size)
  112. return 0;
  113. i = *index;
  114. if (i + count > nvram_size)
  115. count = nvram_size - i;
  116. spin_lock_irqsave(&nvram_lock, flags);
  117. for (; count != 0; count -= len) {
  118. len = count;
  119. if (len > NVRW_CNT)
  120. len = NVRW_CNT;
  121. memcpy(nvram_buf, p, len);
  122. if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf),
  123. len) != 0) || len != done) {
  124. spin_unlock_irqrestore(&nvram_lock, flags);
  125. return -EIO;
  126. }
  127. p += len;
  128. i += len;
  129. }
  130. spin_unlock_irqrestore(&nvram_lock, flags);
  131. *index = i;
  132. return p - buf;
  133. }
  134. static ssize_t pSeries_nvram_get_size(void)
  135. {
  136. return nvram_size ? nvram_size : -ENODEV;
  137. }
  138. /* nvram_write_os_partition, nvram_write_error_log
  139. *
  140. * We need to buffer the error logs into nvram to ensure that we have
  141. * the failure information to decode. If we have a severe error there
  142. * is no way to guarantee that the OS or the machine is in a state to
  143. * get back to user land and write the error to disk. For example if
  144. * the SCSI device driver causes a Machine Check by writing to a bad
  145. * IO address, there is no way of guaranteeing that the device driver
  146. * is in any state that is would also be able to write the error data
  147. * captured to disk, thus we buffer it in NVRAM for analysis on the
  148. * next boot.
  149. *
  150. * In NVRAM the partition containing the error log buffer will looks like:
  151. * Header (in bytes):
  152. * +-----------+----------+--------+------------+------------------+
  153. * | signature | checksum | length | name | data |
  154. * |0 |1 |2 3|4 15|16 length-1|
  155. * +-----------+----------+--------+------------+------------------+
  156. *
  157. * The 'data' section would look like (in bytes):
  158. * +--------------+------------+-----------------------------------+
  159. * | event_logged | sequence # | error log |
  160. * |0 3|4 7|8 error_log_size-1|
  161. * +--------------+------------+-----------------------------------+
  162. *
  163. * event_logged: 0 if event has not been logged to syslog, 1 if it has
  164. * sequence #: The unique sequence # for each event. (until it wraps)
  165. * error log: The error log from event_scan
  166. */
  167. int nvram_write_os_partition(struct nvram_os_partition *part, char * buff,
  168. int length, unsigned int err_type, unsigned int error_log_cnt)
  169. {
  170. int rc;
  171. loff_t tmp_index;
  172. struct err_log_info info;
  173. if (part->index == -1) {
  174. return -ESPIPE;
  175. }
  176. if (length > part->size) {
  177. length = part->size;
  178. }
  179. info.error_type = err_type;
  180. info.seq_num = error_log_cnt;
  181. tmp_index = part->index;
  182. rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
  183. if (rc <= 0) {
  184. pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
  185. return rc;
  186. }
  187. rc = ppc_md.nvram_write(buff, length, &tmp_index);
  188. if (rc <= 0) {
  189. pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc);
  190. return rc;
  191. }
  192. return 0;
  193. }
  194. int nvram_write_error_log(char * buff, int length,
  195. unsigned int err_type, unsigned int error_log_cnt)
  196. {
  197. int rc = nvram_write_os_partition(&rtas_log_partition, buff, length,
  198. err_type, error_log_cnt);
  199. if (!rc)
  200. last_unread_rtas_event = get_seconds();
  201. return rc;
  202. }
  203. /* nvram_read_error_log
  204. *
  205. * Reads nvram for error log for at most 'length'
  206. */
  207. int nvram_read_error_log(char * buff, int length,
  208. unsigned int * err_type, unsigned int * error_log_cnt)
  209. {
  210. int rc;
  211. loff_t tmp_index;
  212. struct err_log_info info;
  213. if (rtas_log_partition.index == -1)
  214. return -1;
  215. if (length > rtas_log_partition.size)
  216. length = rtas_log_partition.size;
  217. tmp_index = rtas_log_partition.index;
  218. rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
  219. if (rc <= 0) {
  220. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  221. return rc;
  222. }
  223. rc = ppc_md.nvram_read(buff, length, &tmp_index);
  224. if (rc <= 0) {
  225. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  226. return rc;
  227. }
  228. *error_log_cnt = info.seq_num;
  229. *err_type = info.error_type;
  230. return 0;
  231. }
  232. /* This doesn't actually zero anything, but it sets the event_logged
  233. * word to tell that this event is safely in syslog.
  234. */
  235. int nvram_clear_error_log(void)
  236. {
  237. loff_t tmp_index;
  238. int clear_word = ERR_FLAG_ALREADY_LOGGED;
  239. int rc;
  240. if (rtas_log_partition.index == -1)
  241. return -1;
  242. tmp_index = rtas_log_partition.index;
  243. rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
  244. if (rc <= 0) {
  245. printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
  246. return rc;
  247. }
  248. last_unread_rtas_event = 0;
  249. return 0;
  250. }
  251. /* pseries_nvram_init_os_partition
  252. *
  253. * This sets up a partition with an "OS" signature.
  254. *
  255. * The general strategy is the following:
  256. * 1.) If a partition with the indicated name already exists...
  257. * - If it's large enough, use it.
  258. * - Otherwise, recycle it and keep going.
  259. * 2.) Search for a free partition that is large enough.
  260. * 3.) If there's not a free partition large enough, recycle any obsolete
  261. * OS partitions and try again.
  262. * 4.) Will first try getting a chunk that will satisfy the requested size.
  263. * 5.) If a chunk of the requested size cannot be allocated, then try finding
  264. * a chunk that will satisfy the minum needed.
  265. *
  266. * Returns 0 on success, else -1.
  267. */
  268. static int __init pseries_nvram_init_os_partition(struct nvram_os_partition
  269. *part)
  270. {
  271. loff_t p;
  272. int size;
  273. /* Scan nvram for partitions */
  274. nvram_scan_partitions();
  275. /* Look for ours */
  276. p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size);
  277. /* Found one but too small, remove it */
  278. if (p && size < part->min_size) {
  279. pr_info("nvram: Found too small %s partition,"
  280. " removing it...\n", part->name);
  281. nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL);
  282. p = 0;
  283. }
  284. /* Create one if we didn't find */
  285. if (!p) {
  286. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  287. part->req_size, part->min_size);
  288. if (p == -ENOSPC) {
  289. pr_info("nvram: No room to create %s partition, "
  290. "deleting any obsolete OS partitions...\n",
  291. part->name);
  292. nvram_remove_partition(NULL, NVRAM_SIG_OS,
  293. pseries_nvram_os_partitions);
  294. p = nvram_create_partition(part->name, NVRAM_SIG_OS,
  295. part->req_size, part->min_size);
  296. }
  297. }
  298. if (p <= 0) {
  299. pr_err("nvram: Failed to find or create %s"
  300. " partition, err %d\n", part->name, (int)p);
  301. return -1;
  302. }
  303. part->index = p;
  304. part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info);
  305. return 0;
  306. }
  307. static void __init nvram_init_oops_partition(int rtas_partition_exists)
  308. {
  309. int rc;
  310. rc = pseries_nvram_init_os_partition(&oops_log_partition);
  311. if (rc != 0) {
  312. if (!rtas_partition_exists)
  313. return;
  314. pr_notice("nvram: Using %s partition to log both"
  315. " RTAS errors and oops/panic reports\n",
  316. rtas_log_partition.name);
  317. memcpy(&oops_log_partition, &rtas_log_partition,
  318. sizeof(rtas_log_partition));
  319. }
  320. oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL);
  321. rc = kmsg_dump_register(&nvram_kmsg_dumper);
  322. if (rc != 0) {
  323. pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc);
  324. kfree(oops_buf);
  325. return;
  326. }
  327. }
  328. static int __init pseries_nvram_init_log_partitions(void)
  329. {
  330. int rc;
  331. rc = pseries_nvram_init_os_partition(&rtas_log_partition);
  332. nvram_init_oops_partition(rc == 0);
  333. return 0;
  334. }
  335. machine_arch_initcall(pseries, pseries_nvram_init_log_partitions);
  336. int __init pSeries_nvram_init(void)
  337. {
  338. struct device_node *nvram;
  339. const unsigned int *nbytes_p;
  340. unsigned int proplen;
  341. nvram = of_find_node_by_type(NULL, "nvram");
  342. if (nvram == NULL)
  343. return -ENODEV;
  344. nbytes_p = of_get_property(nvram, "#bytes", &proplen);
  345. if (nbytes_p == NULL || proplen != sizeof(unsigned int)) {
  346. of_node_put(nvram);
  347. return -EIO;
  348. }
  349. nvram_size = *nbytes_p;
  350. nvram_fetch = rtas_token("nvram-fetch");
  351. nvram_store = rtas_token("nvram-store");
  352. printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size);
  353. of_node_put(nvram);
  354. ppc_md.nvram_read = pSeries_nvram_read;
  355. ppc_md.nvram_write = pSeries_nvram_write;
  356. ppc_md.nvram_size = pSeries_nvram_get_size;
  357. return 0;
  358. }
  359. /*
  360. * Try to capture the last capture_len bytes of the printk buffer. Return
  361. * the amount actually captured.
  362. */
  363. static size_t capture_last_msgs(const char *old_msgs, size_t old_len,
  364. const char *new_msgs, size_t new_len,
  365. char *captured, size_t capture_len)
  366. {
  367. if (new_len >= capture_len) {
  368. memcpy(captured, new_msgs + (new_len - capture_len),
  369. capture_len);
  370. return capture_len;
  371. } else {
  372. /* Grab the end of old_msgs. */
  373. size_t old_tail_len = min(old_len, capture_len - new_len);
  374. memcpy(captured, old_msgs + (old_len - old_tail_len),
  375. old_tail_len);
  376. memcpy(captured + old_tail_len, new_msgs, new_len);
  377. return old_tail_len + new_len;
  378. }
  379. }
  380. /*
  381. * Are we using the ibm,rtas-log for oops/panic reports? And if so,
  382. * would logging this oops/panic overwrite an RTAS event that rtas_errd
  383. * hasn't had a chance to read and process? Return 1 if so, else 0.
  384. *
  385. * We assume that if rtas_errd hasn't read the RTAS event in
  386. * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to.
  387. */
  388. static int clobbering_unread_rtas_event(void)
  389. {
  390. return (oops_log_partition.index == rtas_log_partition.index
  391. && last_unread_rtas_event
  392. && get_seconds() - last_unread_rtas_event <=
  393. NVRAM_RTAS_READ_TIMEOUT);
  394. }
  395. /* our kmsg_dump callback */
  396. static void oops_to_nvram(struct kmsg_dumper *dumper,
  397. enum kmsg_dump_reason reason,
  398. const char *old_msgs, unsigned long old_len,
  399. const char *new_msgs, unsigned long new_len)
  400. {
  401. static unsigned int oops_count = 0;
  402. static bool panicking = false;
  403. size_t text_len;
  404. switch (reason) {
  405. case KMSG_DUMP_RESTART:
  406. case KMSG_DUMP_HALT:
  407. case KMSG_DUMP_POWEROFF:
  408. /* These are almost always orderly shutdowns. */
  409. return;
  410. case KMSG_DUMP_OOPS:
  411. case KMSG_DUMP_KEXEC:
  412. break;
  413. case KMSG_DUMP_PANIC:
  414. panicking = true;
  415. break;
  416. case KMSG_DUMP_EMERG:
  417. if (panicking)
  418. /* Panic report already captured. */
  419. return;
  420. break;
  421. default:
  422. pr_err("%s: ignoring unrecognized KMSG_DUMP_* reason %d\n",
  423. __FUNCTION__, (int) reason);
  424. return;
  425. }
  426. if (clobbering_unread_rtas_event())
  427. return;
  428. text_len = capture_last_msgs(old_msgs, old_len, new_msgs, new_len,
  429. oops_buf, oops_log_partition.size);
  430. (void) nvram_write_os_partition(&oops_log_partition, oops_buf,
  431. (int) text_len, ERR_TYPE_KERNEL_PANIC, ++oops_count);
  432. }