nvram.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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 nvram_partition *part;
  283. struct nvram_partition *new_part;
  284. struct nvram_partition *free_part = NULL;
  285. int seq_init[2] = { 0, 0 };
  286. loff_t tmp_index;
  287. long size = 0;
  288. int rc;
  289. /* Find a free partition that will give us the maximum needed size
  290. If can't find one that will give us the minimum size needed */
  291. list_for_each_entry(part, &nvram_part->partition, partition) {
  292. if (part->header.signature != NVRAM_SIG_FREE)
  293. continue;
  294. if (part->header.length >= NVRAM_MAX_REQ) {
  295. size = NVRAM_MAX_REQ;
  296. free_part = part;
  297. break;
  298. }
  299. if (!size && part->header.length >= NVRAM_MIN_REQ) {
  300. size = NVRAM_MIN_REQ;
  301. free_part = part;
  302. }
  303. }
  304. if (!size)
  305. return -ENOSPC;
  306. /* Create our OS partition */
  307. new_part = kmalloc(sizeof(*new_part), GFP_KERNEL);
  308. if (!new_part) {
  309. printk(KERN_ERR "nvram_create_os_partition: kmalloc failed\n");
  310. return -ENOMEM;
  311. }
  312. new_part->index = free_part->index;
  313. new_part->header.signature = NVRAM_SIG_OS;
  314. new_part->header.length = size;
  315. strcpy(new_part->header.name, "ppc64,linux");
  316. new_part->header.checksum = nvram_checksum(&new_part->header);
  317. rc = nvram_write_header(new_part);
  318. if (rc <= 0) {
  319. printk(KERN_ERR "nvram_create_os_partition: nvram_write_header \
  320. failed (%d)\n", rc);
  321. return rc;
  322. }
  323. /* make sure and initialize to zero the sequence number and the error
  324. type logged */
  325. tmp_index = new_part->index + NVRAM_HEADER_LEN;
  326. rc = ppc_md.nvram_write((char *)&seq_init, sizeof(seq_init), &tmp_index);
  327. if (rc <= 0) {
  328. printk(KERN_ERR "nvram_create_os_partition: nvram_write "
  329. "failed (%d)\n", rc);
  330. return rc;
  331. }
  332. nvram_error_log_index = new_part->index + NVRAM_HEADER_LEN;
  333. nvram_error_log_size = ((part->header.length - 1) *
  334. NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
  335. list_add_tail(&new_part->partition, &free_part->partition);
  336. if (free_part->header.length <= size) {
  337. list_del(&free_part->partition);
  338. kfree(free_part);
  339. return 0;
  340. }
  341. /* Adjust the partition we stole the space from */
  342. free_part->index += size * NVRAM_BLOCK_LEN;
  343. free_part->header.length -= size;
  344. free_part->header.checksum = nvram_checksum(&free_part->header);
  345. rc = nvram_write_header(free_part);
  346. if (rc <= 0) {
  347. printk(KERN_ERR "nvram_create_os_partition: nvram_write_header "
  348. "failed (%d)\n", rc);
  349. return rc;
  350. }
  351. return 0;
  352. }
  353. /* nvram_setup_partition
  354. *
  355. * This will setup the partition we need for buffering the
  356. * error logs and cleanup partitions if needed.
  357. *
  358. * The general strategy is the following:
  359. * 1.) If there is ppc64,linux partition large enough then use it.
  360. * 2.) If there is not a ppc64,linux partition large enough, search
  361. * for a free partition that is large enough.
  362. * 3.) If there is not a free partition large enough remove
  363. * _all_ OS partitions and consolidate the space.
  364. * 4.) Will first try getting a chunk that will satisfy the maximum
  365. * error log size (NVRAM_MAX_REQ).
  366. * 5.) If the max chunk cannot be allocated then try finding a chunk
  367. * that will satisfy the minum needed (NVRAM_MIN_REQ).
  368. */
  369. static int nvram_setup_partition(void)
  370. {
  371. struct list_head * p;
  372. struct nvram_partition * part;
  373. int rc;
  374. /* For now, we don't do any of this on pmac, until I
  375. * have figured out if it's worth killing some unused stuffs
  376. * in our nvram, as Apple defined partitions use pretty much
  377. * all of the space
  378. */
  379. if (systemcfg->platform == PLATFORM_POWERMAC)
  380. return -ENOSPC;
  381. /* see if we have an OS partition that meets our needs.
  382. will try getting the max we need. If not we'll delete
  383. partitions and try again. */
  384. list_for_each(p, &nvram_part->partition) {
  385. part = list_entry(p, struct nvram_partition, partition);
  386. if (part->header.signature != NVRAM_SIG_OS)
  387. continue;
  388. if (strcmp(part->header.name, "ppc64,linux"))
  389. continue;
  390. if (part->header.length >= NVRAM_MIN_REQ) {
  391. /* found our partition */
  392. nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
  393. nvram_error_log_size = ((part->header.length - 1) *
  394. NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
  395. return 0;
  396. }
  397. }
  398. /* try creating a partition with the free space we have */
  399. rc = nvram_create_os_partition();
  400. if (!rc) {
  401. return 0;
  402. }
  403. /* need to free up some space */
  404. rc = nvram_remove_os_partition();
  405. if (rc) {
  406. return rc;
  407. }
  408. /* create a partition in this new space */
  409. rc = nvram_create_os_partition();
  410. if (rc) {
  411. printk(KERN_ERR "nvram_create_os_partition: Could not find a "
  412. "NVRAM partition large enough\n");
  413. return rc;
  414. }
  415. return 0;
  416. }
  417. static int nvram_scan_partitions(void)
  418. {
  419. loff_t cur_index = 0;
  420. struct nvram_header phead;
  421. struct nvram_partition * tmp_part;
  422. unsigned char c_sum;
  423. char * header;
  424. int total_size;
  425. int err;
  426. if (ppc_md.nvram_size == NULL)
  427. return -ENODEV;
  428. total_size = ppc_md.nvram_size();
  429. header = (char *) kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  430. if (!header) {
  431. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  432. return -ENOMEM;
  433. }
  434. while (cur_index < total_size) {
  435. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  436. if (err != NVRAM_HEADER_LEN) {
  437. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  438. "nvram partitions\n");
  439. goto out;
  440. }
  441. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  442. memcpy(&phead, header, NVRAM_HEADER_LEN);
  443. err = 0;
  444. c_sum = nvram_checksum(&phead);
  445. if (c_sum != phead.checksum) {
  446. printk(KERN_WARNING "WARNING: nvram partition checksum"
  447. " was %02x, should be %02x!\n",
  448. phead.checksum, c_sum);
  449. printk(KERN_WARNING "Terminating nvram partition scan\n");
  450. goto out;
  451. }
  452. if (!phead.length) {
  453. printk(KERN_WARNING "WARNING: nvram corruption "
  454. "detected: 0-length partition\n");
  455. goto out;
  456. }
  457. tmp_part = (struct nvram_partition *)
  458. kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  459. err = -ENOMEM;
  460. if (!tmp_part) {
  461. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  462. goto out;
  463. }
  464. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  465. tmp_part->index = cur_index;
  466. list_add_tail(&tmp_part->partition, &nvram_part->partition);
  467. cur_index += phead.length * NVRAM_BLOCK_LEN;
  468. }
  469. err = 0;
  470. out:
  471. kfree(header);
  472. return err;
  473. }
  474. static int __init nvram_init(void)
  475. {
  476. int error;
  477. int rc;
  478. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  479. return -ENODEV;
  480. rc = misc_register(&nvram_dev);
  481. if (rc != 0) {
  482. printk(KERN_ERR "nvram_init: failed to register device\n");
  483. return rc;
  484. }
  485. /* initialize our anchor for the nvram partition list */
  486. nvram_part = (struct nvram_partition *) kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  487. if (!nvram_part) {
  488. printk(KERN_ERR "nvram_init: Failed kmalloc\n");
  489. return -ENOMEM;
  490. }
  491. INIT_LIST_HEAD(&nvram_part->partition);
  492. /* Get all the NVRAM partitions */
  493. error = nvram_scan_partitions();
  494. if (error) {
  495. printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
  496. return error;
  497. }
  498. if(nvram_setup_partition())
  499. printk(KERN_WARNING "nvram_init: Could not find nvram partition"
  500. " for nvram buffered error logging.\n");
  501. #ifdef DEBUG_NVRAM
  502. nvram_print_partitions("NVRAM Partitions");
  503. #endif
  504. return rc;
  505. }
  506. void __exit nvram_cleanup(void)
  507. {
  508. misc_deregister( &nvram_dev );
  509. }
  510. #ifdef CONFIG_PPC_PSERIES
  511. /* nvram_write_error_log
  512. *
  513. * We need to buffer the error logs into nvram to ensure that we have
  514. * the failure information to decode. If we have a severe error there
  515. * is no way to guarantee that the OS or the machine is in a state to
  516. * get back to user land and write the error to disk. For example if
  517. * the SCSI device driver causes a Machine Check by writing to a bad
  518. * IO address, there is no way of guaranteeing that the device driver
  519. * is in any state that is would also be able to write the error data
  520. * captured to disk, thus we buffer it in NVRAM for analysis on the
  521. * next boot.
  522. *
  523. * In NVRAM the partition containing the error log buffer will looks like:
  524. * Header (in bytes):
  525. * +-----------+----------+--------+------------+------------------+
  526. * | signature | checksum | length | name | data |
  527. * |0 |1 |2 3|4 15|16 length-1|
  528. * +-----------+----------+--------+------------+------------------+
  529. *
  530. * The 'data' section would look like (in bytes):
  531. * +--------------+------------+-----------------------------------+
  532. * | event_logged | sequence # | error log |
  533. * |0 3|4 7|8 nvram_error_log_size-1|
  534. * +--------------+------------+-----------------------------------+
  535. *
  536. * event_logged: 0 if event has not been logged to syslog, 1 if it has
  537. * sequence #: The unique sequence # for each event. (until it wraps)
  538. * error log: The error log from event_scan
  539. */
  540. int nvram_write_error_log(char * buff, int length, unsigned int err_type)
  541. {
  542. int rc;
  543. loff_t tmp_index;
  544. struct err_log_info info;
  545. if (no_logging) {
  546. return -EPERM;
  547. }
  548. if (nvram_error_log_index == -1) {
  549. return -ESPIPE;
  550. }
  551. if (length > nvram_error_log_size) {
  552. length = nvram_error_log_size;
  553. }
  554. info.error_type = err_type;
  555. info.seq_num = error_log_cnt;
  556. tmp_index = nvram_error_log_index;
  557. rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
  558. if (rc <= 0) {
  559. printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
  560. return rc;
  561. }
  562. rc = ppc_md.nvram_write(buff, length, &tmp_index);
  563. if (rc <= 0) {
  564. printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
  565. return rc;
  566. }
  567. return 0;
  568. }
  569. /* nvram_read_error_log
  570. *
  571. * Reads nvram for error log for at most 'length'
  572. */
  573. int nvram_read_error_log(char * buff, int length, unsigned int * err_type)
  574. {
  575. int rc;
  576. loff_t tmp_index;
  577. struct err_log_info info;
  578. if (nvram_error_log_index == -1)
  579. return -1;
  580. if (length > nvram_error_log_size)
  581. length = nvram_error_log_size;
  582. tmp_index = nvram_error_log_index;
  583. rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
  584. if (rc <= 0) {
  585. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  586. return rc;
  587. }
  588. rc = ppc_md.nvram_read(buff, length, &tmp_index);
  589. if (rc <= 0) {
  590. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  591. return rc;
  592. }
  593. error_log_cnt = info.seq_num;
  594. *err_type = info.error_type;
  595. return 0;
  596. }
  597. /* This doesn't actually zero anything, but it sets the event_logged
  598. * word to tell that this event is safely in syslog.
  599. */
  600. int nvram_clear_error_log(void)
  601. {
  602. loff_t tmp_index;
  603. int clear_word = ERR_FLAG_ALREADY_LOGGED;
  604. int rc;
  605. tmp_index = nvram_error_log_index;
  606. rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
  607. if (rc <= 0) {
  608. printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
  609. return rc;
  610. }
  611. return 0;
  612. }
  613. #endif /* CONFIG_PPC_PSERIES */
  614. module_init(nvram_init);
  615. module_exit(nvram_cleanup);
  616. MODULE_LICENSE("GPL");