nvram.c 19 KB

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