nvram.c 19 KB

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