nvram_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. * TODO: Split the /dev/nvram part (that one can use
  14. * drivers/char/generic_nvram.c) from the arch & partition
  15. * parsing code.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/errno.h>
  20. #include <linux/fs.h>
  21. #include <linux/miscdevice.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/nvram.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/nvram.h>
  29. #include <asm/rtas.h>
  30. #include <asm/prom.h>
  31. #include <asm/machdep.h>
  32. #undef DEBUG_NVRAM
  33. static int nvram_scan_partitions(void);
  34. static int nvram_setup_partition(void);
  35. static int nvram_create_os_partition(void);
  36. static int nvram_remove_os_partition(void);
  37. static struct nvram_partition * nvram_part;
  38. static long nvram_error_log_index = -1;
  39. static long nvram_error_log_size = 0;
  40. int no_logging = 1; /* Until we initialize everything,
  41. * make sure we don't try logging
  42. * anything */
  43. extern volatile int error_log_cnt;
  44. struct err_log_info {
  45. int error_type;
  46. unsigned int seq_num;
  47. };
  48. static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
  49. {
  50. int size;
  51. if (ppc_md.nvram_size == NULL)
  52. return -ENODEV;
  53. size = ppc_md.nvram_size();
  54. switch (origin) {
  55. case 1:
  56. offset += file->f_pos;
  57. break;
  58. case 2:
  59. offset += size;
  60. break;
  61. }
  62. if (offset < 0)
  63. return -EINVAL;
  64. file->f_pos = offset;
  65. return file->f_pos;
  66. }
  67. static ssize_t dev_nvram_read(struct file *file, char __user *buf,
  68. size_t count, loff_t *ppos)
  69. {
  70. ssize_t len;
  71. char *tmp_buffer;
  72. int size;
  73. if (ppc_md.nvram_size == NULL)
  74. return -ENODEV;
  75. size = ppc_md.nvram_size();
  76. if (!access_ok(VERIFY_WRITE, buf, count))
  77. return -EFAULT;
  78. if (*ppos >= size)
  79. return 0;
  80. if (count > size)
  81. count = size;
  82. tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
  83. if (!tmp_buffer) {
  84. printk(KERN_ERR "dev_read_nvram: kmalloc failed\n");
  85. return -ENOMEM;
  86. }
  87. len = ppc_md.nvram_read(tmp_buffer, count, ppos);
  88. if ((long)len <= 0) {
  89. kfree(tmp_buffer);
  90. return len;
  91. }
  92. if (copy_to_user(buf, tmp_buffer, len)) {
  93. kfree(tmp_buffer);
  94. return -EFAULT;
  95. }
  96. kfree(tmp_buffer);
  97. return len;
  98. }
  99. static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
  100. size_t count, loff_t *ppos)
  101. {
  102. ssize_t len;
  103. char * tmp_buffer;
  104. int size;
  105. if (ppc_md.nvram_size == NULL)
  106. return -ENODEV;
  107. size = ppc_md.nvram_size();
  108. if (!access_ok(VERIFY_READ, buf, count))
  109. return -EFAULT;
  110. if (*ppos >= size)
  111. return 0;
  112. if (count > size)
  113. count = size;
  114. tmp_buffer = (char *) kmalloc(count, GFP_KERNEL);
  115. if (!tmp_buffer) {
  116. printk(KERN_ERR "dev_nvram_write: kmalloc failed\n");
  117. return -ENOMEM;
  118. }
  119. if (copy_from_user(tmp_buffer, buf, count)) {
  120. kfree(tmp_buffer);
  121. return -EFAULT;
  122. }
  123. len = ppc_md.nvram_write(tmp_buffer, count, ppos);
  124. if ((long)len <= 0) {
  125. kfree(tmp_buffer);
  126. return len;
  127. }
  128. kfree(tmp_buffer);
  129. return len;
  130. }
  131. static int dev_nvram_ioctl(struct inode *inode, struct file *file,
  132. unsigned int cmd, unsigned long arg)
  133. {
  134. switch(cmd) {
  135. #ifdef CONFIG_PPC_PMAC
  136. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  137. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  138. case IOC_NVRAM_GET_OFFSET: {
  139. int part, offset;
  140. if (_machine != PLATFORM_POWERMAC)
  141. return -EINVAL;
  142. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  143. return -EFAULT;
  144. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  145. return -EINVAL;
  146. offset = pmac_get_partition(part);
  147. if (offset < 0)
  148. return offset;
  149. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  150. return -EFAULT;
  151. return 0;
  152. }
  153. #endif /* CONFIG_PPC_PMAC */
  154. }
  155. return -EINVAL;
  156. }
  157. struct file_operations nvram_fops = {
  158. .owner = THIS_MODULE,
  159. .llseek = dev_nvram_llseek,
  160. .read = dev_nvram_read,
  161. .write = dev_nvram_write,
  162. .ioctl = dev_nvram_ioctl,
  163. };
  164. static struct miscdevice nvram_dev = {
  165. NVRAM_MINOR,
  166. "nvram",
  167. &nvram_fops
  168. };
  169. #ifdef DEBUG_NVRAM
  170. static void nvram_print_partitions(char * label)
  171. {
  172. struct list_head * p;
  173. struct nvram_partition * tmp_part;
  174. printk(KERN_WARNING "--------%s---------\n", label);
  175. printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n");
  176. list_for_each(p, &nvram_part->partition) {
  177. tmp_part = list_entry(p, struct nvram_partition, partition);
  178. printk(KERN_WARNING "%d \t%02x\t%02x\t%d\t%s\n",
  179. tmp_part->index, tmp_part->header.signature,
  180. tmp_part->header.checksum, tmp_part->header.length,
  181. tmp_part->header.name);
  182. }
  183. }
  184. #endif
  185. static int nvram_write_header(struct nvram_partition * part)
  186. {
  187. loff_t tmp_index;
  188. int rc;
  189. tmp_index = part->index;
  190. rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index);
  191. return rc;
  192. }
  193. static unsigned char nvram_checksum(struct nvram_header *p)
  194. {
  195. unsigned int c_sum, c_sum2;
  196. unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */
  197. c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5];
  198. /* The sum may have spilled into the 3rd byte. Fold it back. */
  199. c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff;
  200. /* The sum cannot exceed 2 bytes. Fold it into a checksum */
  201. c_sum2 = (c_sum >> 8) + (c_sum << 8);
  202. c_sum = ((c_sum + c_sum2) >> 8) & 0xff;
  203. return c_sum;
  204. }
  205. /*
  206. * Find an nvram partition, sig can be 0 for any
  207. * partition or name can be NULL for any name, else
  208. * tries to match both
  209. */
  210. struct nvram_partition *nvram_find_partition(int sig, const char *name)
  211. {
  212. struct nvram_partition * part;
  213. struct list_head * p;
  214. list_for_each(p, &nvram_part->partition) {
  215. part = list_entry(p, struct nvram_partition, partition);
  216. if (sig && part->header.signature != sig)
  217. continue;
  218. if (name && 0 != strncmp(name, part->header.name, 12))
  219. continue;
  220. return part;
  221. }
  222. return NULL;
  223. }
  224. EXPORT_SYMBOL(nvram_find_partition);
  225. static int nvram_remove_os_partition(void)
  226. {
  227. struct list_head *i;
  228. struct list_head *j;
  229. struct nvram_partition * part;
  230. struct nvram_partition * cur_part;
  231. int rc;
  232. list_for_each(i, &nvram_part->partition) {
  233. part = list_entry(i, struct nvram_partition, partition);
  234. if (part->header.signature != NVRAM_SIG_OS)
  235. continue;
  236. /* Make os partition a free partition */
  237. part->header.signature = NVRAM_SIG_FREE;
  238. sprintf(part->header.name, "wwwwwwwwwwww");
  239. part->header.checksum = nvram_checksum(&part->header);
  240. /* Merge contiguous free partitions backwards */
  241. list_for_each_prev(j, &part->partition) {
  242. cur_part = list_entry(j, struct nvram_partition, partition);
  243. if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
  244. break;
  245. }
  246. part->header.length += cur_part->header.length;
  247. part->header.checksum = nvram_checksum(&part->header);
  248. part->index = cur_part->index;
  249. list_del(&cur_part->partition);
  250. kfree(cur_part);
  251. j = &part->partition; /* fixup our loop */
  252. }
  253. /* Merge contiguous free partitions forwards */
  254. list_for_each(j, &part->partition) {
  255. cur_part = list_entry(j, struct nvram_partition, partition);
  256. if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) {
  257. break;
  258. }
  259. part->header.length += cur_part->header.length;
  260. part->header.checksum = nvram_checksum(&part->header);
  261. list_del(&cur_part->partition);
  262. kfree(cur_part);
  263. j = &part->partition; /* fixup our loop */
  264. }
  265. rc = nvram_write_header(part);
  266. if (rc <= 0) {
  267. printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc);
  268. return rc;
  269. }
  270. }
  271. return 0;
  272. }
  273. /* nvram_create_os_partition
  274. *
  275. * Create a OS linux partition to buffer error logs.
  276. * Will create a partition starting at the first free
  277. * space found if space has enough room.
  278. */
  279. static int nvram_create_os_partition(void)
  280. {
  281. struct nvram_partition *part;
  282. struct nvram_partition *new_part;
  283. struct nvram_partition *free_part = NULL;
  284. int seq_init[2] = { 0, 0 };
  285. loff_t tmp_index;
  286. long size = 0;
  287. int rc;
  288. /* Find a free partition that will give us the maximum needed size
  289. If can't find one that will give us the minimum size needed */
  290. list_for_each_entry(part, &nvram_part->partition, partition) {
  291. if (part->header.signature != NVRAM_SIG_FREE)
  292. continue;
  293. if (part->header.length >= NVRAM_MAX_REQ) {
  294. size = NVRAM_MAX_REQ;
  295. free_part = part;
  296. break;
  297. }
  298. if (!size && part->header.length >= NVRAM_MIN_REQ) {
  299. size = NVRAM_MIN_REQ;
  300. free_part = part;
  301. }
  302. }
  303. if (!size)
  304. return -ENOSPC;
  305. /* Create our OS partition */
  306. new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
  307. if (!new_part) {
  308. printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
  309. return -ENOMEM;
  310. }
  311. new_part->index = free_part->index;
  312. new_part->header.signature = NVRAM_SIG_OS;
  313. new_part->header.length = size;
  314. strcpy(new_part->header.name, "ppc64,linux");
  315. new_part->header.checksum = nvram_checksum(&new_part->header);
  316. rc = nvram_write_header(new_part);
  317. if (rc <= 0) {
  318. printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
  319. failed (%d)\n", rc);
  320. return rc;
  321. }
  322. /* make sure and initialize to zero the sequence number and the error
  323. type logged */
  324. tmp_index = new_part->index + NVRAM_HEADER_LEN;
  325. rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
  326. if (rc <= 0) {
  327. printk(KERN_ERR "nvram_create_os_partition: nvram_write "
  328. "failed (%d)\n", rc);
  329. return rc;
  330. }
  331. nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
  332. nvram_error_log_size = ((part->header.length - 1) *
  333. NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
  334. list_add_tail(&new_part->partition, &free_part->partition);
  335. if (free_part->header.length <= size) {
  336. list_del(&free_part->partition);
  337. kfree(free_part);
  338. return 0;
  339. }
  340. /* Adjust the partition we stole the space from */
  341. free_part->index += size * NVRAM_BLOCK_LEN;
  342. free_part->header.length -= size;
  343. free_part->header.checksum = nvram_checksum(&free_part->header);
  344. rc = nvram_write_header(free_part);
  345. if (rc <= 0) {
  346. printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
  347. "failed (%d)\n", rc);
  348. return rc;
  349. }
  350. return 0;
  351. }
  352. /* nvram_setup_partition
  353. *
  354. * This will setup the partition we need for buffering the
  355. * error logs and cleanup partitions if needed.
  356. *
  357. * The general strategy is the following:
  358. * 1.) If there is ppc64,linux partition large enough then use it.
  359. * 2.) If there is not a ppc64,linux partition large enough, search
  360. * for a free partition that is large enough.
  361. * 3.) If there is not a free partition large enough remove
  362. * _all_ OS partitions and consolidate the space.
  363. * 4.) Will first try getting a chunk that will satisfy the maximum
  364. * error log size (NVRAM_MAX_REQ).
  365. * 5.) If the max chunk cannot be allocated then try finding a chunk
  366. * that will satisfy the minum needed (NVRAM_MIN_REQ).
  367. */
  368. static int nvram_setup_partition(void)
  369. {
  370. struct list_head * p;
  371. struct nvram_partition * part;
  372. int rc;
  373. /* For now, we don't do any of this on pmac, until I
  374. * have figured out if it's worth killing some unused stuffs
  375. * in our nvram, as Apple defined partitions use pretty much
  376. * all of the space
  377. */
  378. if (_machine == PLATFORM_POWERMAC)
  379. return -ENOSPC;
  380. /* see if we have an OS partition that meets our needs.
  381. will try getting the max we need. If not we'll delete
  382. partitions and try again. */
  383. list_for_each(p, &nvram_part->partition) {
  384. part = list_entry(p, struct nvram_partition, partition);
  385. if (part->header.signature != NVRAM_SIG_OS)
  386. continue;
  387. if (strcmp(part->header.name, "ppc64,linux"))
  388. continue;
  389. if (part->header.length >= NVRAM_MIN_REQ) {
  390. /* found our partition */
  391. nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
  392. nvram_error_log_size = ((part->header.length - 1) *
  393. NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
  394. return 0;
  395. }
  396. }
  397. /* try creating a partition with the free space we have */
  398. rc = nvram_create_os_partition();
  399. if (!rc) {
  400. return 0;
  401. }
  402. /* need to free up some space */
  403. rc = nvram_remove_os_partition();
  404. if (rc) {
  405. return rc;
  406. }
  407. /* create a partition in this new space */
  408. rc = nvram_create_os_partition();
  409. if (rc) {
  410. printk(KERN_ERR "nvram_create_os_partition: Could not find a "
  411. "NVRAM partition large enough\n");
  412. return rc;
  413. }
  414. return 0;
  415. }
  416. static int nvram_scan_partitions(void)
  417. {
  418. loff_t cur_index = 0;
  419. struct nvram_header phead;
  420. struct nvram_partition * tmp_part;
  421. unsigned char c_sum;
  422. char * header;
  423. int total_size;
  424. int err;
  425. if (ppc_md.nvram_size == NULL)
  426. return -ENODEV;
  427. total_size = ppc_md.nvram_size();
  428. header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  429. if (!header) {
  430. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  431. return -ENOMEM;
  432. }
  433. while (cur_index < total_size) {
  434. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  435. if (err != NVRAM_HEADER_LEN) {
  436. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  437. "nvram partitions\n");
  438. goto out;
  439. }
  440. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  441. memcpy(&phead, header, NVRAM_HEADER_LEN);
  442. err = 0;
  443. c_sum = nvram_checksum(&phead);
  444. if (c_sum != phead.checksum) {
  445. printk(KERN_WARNING "WARNING: nvram partition checksum"
  446. " was %02x, should be %02x!\n",
  447. phead.checksum, c_sum);
  448. printk(KERN_WARNING "Terminating nvram partition scan\n");
  449. goto out;
  450. }
  451. if (!phead.length) {
  452. printk(KERN_WARNING "WARNING: nvram corruption "
  453. "detected: 0-length partition\n");
  454. goto out;
  455. }
  456. tmp_part = (struct nvram_partition *)
  457. kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  458. err = -ENOMEM;
  459. if (!tmp_part) {
  460. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  461. goto out;
  462. }
  463. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  464. tmp_part->index = cur_index;
  465. list_add_tail(&tmp_part->partition, &nvram_part->partition);
  466. cur_index += phead.length * NVRAM_BLOCK_LEN;
  467. }
  468. err = 0;
  469. out:
  470. kfree(header);
  471. return err;
  472. }
  473. static int __init nvram_init(void)
  474. {
  475. int error;
  476. int rc;
  477. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  478. return -ENODEV;
  479. rc = misc_register(&nvram_dev);
  480. if (rc != 0) {
  481. printk(KERN_ERR "nvram_init: failed to register device\n");
  482. return rc;
  483. }
  484. /* initialize our anchor for the nvram partition list */
  485. nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  486. if (!nvram_part) {
  487. printk(KERN_ERR "nvram_init: Failed kmalloc\n");
  488. return -ENOMEM;
  489. }
  490. INIT_LIST_HEAD(&nvram_part->partition);
  491. /* Get all the NVRAM partitions */
  492. error = nvram_scan_partitions();
  493. if (error) {
  494. printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
  495. return error;
  496. }
  497. if(nvram_setup_partition())
  498. printk(KERN_WARNING "nvram_init: Could not find nvram partition"
  499. " for nvram buffered error logging.\n");
  500. #ifdef DEBUG_NVRAM
  501. nvram_print_partitions("NVRAM Partitions");
  502. #endif
  503. return rc;
  504. }
  505. void __exit nvram_cleanup(void)
  506. {
  507. misc_deregister( &nvram_dev );
  508. }
  509. #ifdef CONFIG_PPC_PSERIES
  510. /* nvram_write_error_log
  511. *
  512. * We need to buffer the error logs into nvram to ensure that we have
  513. * the failure information to decode. If we have a severe error there
  514. * is no way to guarantee that the OS or the machine is in a state to
  515. * get back to user land and write the error to disk. For example if
  516. * the SCSI device driver causes a Machine Check by writing to a bad
  517. * IO address, there is no way of guaranteeing that the device driver
  518. * is in any state that is would also be able to write the error data
  519. * captured to disk, thus we buffer it in NVRAM for analysis on the
  520. * next boot.
  521. *
  522. * In NVRAM the partition containing the error log buffer will looks like:
  523. * Header (in bytes):
  524. * +-----------+----------+--------+------------+------------------+
  525. * | signature | checksum | length | name | data |
  526. * |0 |1 |2 3|4 15|16 length-1|
  527. * +-----------+----------+--------+------------+------------------+
  528. *
  529. * The 'data' section would look like (in bytes):
  530. * +--------------+------------+-----------------------------------+
  531. * | event_logged | sequence # | error log |
  532. * |0 3|4 7|8 nvram_error_log_size-1|
  533. * +--------------+------------+-----------------------------------+
  534. *
  535. * event_logged: 0 if event has not been logged to syslog, 1 if it has
  536. * sequence #: The unique sequence # for each event. (until it wraps)
  537. * error log: The error log from event_scan
  538. */
  539. int nvram_write_error_log(char * buff, int length, unsigned int err_type)
  540. {
  541. int rc;
  542. loff_t tmp_index;
  543. struct err_log_info info;
  544. if (no_logging) {
  545. return -EPERM;
  546. }
  547. if (nvram_error_log_index == -1) {
  548. return -ESPIPE;
  549. }
  550. if (length > nvram_error_log_size) {
  551. length = nvram_error_log_size;
  552. }
  553. info.error_type = err_type;
  554. info.seq_num = error_log_cnt;
  555. tmp_index = nvram_error_log_index;
  556. rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
  557. if (rc <= 0) {
  558. printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
  559. return rc;
  560. }
  561. rc = ppc_md.nvram_write(buff, length, &tmp_index);
  562. if (rc <= 0) {
  563. printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
  564. return rc;
  565. }
  566. return 0;
  567. }
  568. /* nvram_read_error_log
  569. *
  570. * Reads nvram for error log for at most 'length'
  571. */
  572. int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
  573. {
  574. int rc;
  575. loff_t tmp_index;
  576. struct err_log_info info;
  577. if (nvram_error_log_index == -1)
  578. return -1;
  579. if (length > nvram_error_log_size)
  580. length = nvram_error_log_size;
  581. tmp_index = nvram_error_log_index;
  582. rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
  583. if (rc <= 0) {
  584. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  585. return rc;
  586. }
  587. rc = ppc_md.nvram_read(buff, length, &tmp_index);
  588. if (rc <= 0) {
  589. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  590. return rc;
  591. }
  592. error_log_cnt = info.seq_num;
  593. *err_type = info.error_type;
  594. return 0;
  595. }
  596. /* This doesn't actually zero anything, but it sets the event_logged
  597. * word to tell that this event is safely in syslog.
  598. */
  599. int nvram_clear_error_log(void)
  600. {
  601. loff_t tmp_index;
  602. int clear_word = ERR_FLAG_ALREADY_LOGGED;
  603. int rc;
  604. tmp_index = nvram_error_log_index;
  605. rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
  606. if (rc <= 0) {
  607. printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
  608. return rc;
  609. }
  610. return 0;
  611. }
  612. #endif /* CONFIG_PPC_PSERIES */
  613. module_init(nvram_init);
  614. module_exit(nvram_cleanup);
  615. MODULE_LICENSE("GPL");