nvram_64.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. #define NVRAM_HEADER_LEN sizeof(struct nvram_header)
  34. #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN
  35. #define NVRAM_MAX_REQ 2079
  36. #define NVRAM_MIN_REQ 1055
  37. /* If change this size, then change the size of NVNAME_LEN */
  38. struct nvram_header {
  39. unsigned char signature;
  40. unsigned char checksum;
  41. unsigned short length;
  42. char name[12];
  43. };
  44. struct nvram_partition {
  45. struct list_head partition;
  46. struct nvram_header header;
  47. unsigned int index;
  48. };
  49. static struct nvram_partition * nvram_part;
  50. static long nvram_error_log_index = -1;
  51. static long nvram_error_log_size = 0;
  52. struct err_log_info {
  53. int error_type;
  54. unsigned int seq_num;
  55. };
  56. static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin)
  57. {
  58. int size;
  59. if (ppc_md.nvram_size == NULL)
  60. return -ENODEV;
  61. size = ppc_md.nvram_size();
  62. switch (origin) {
  63. case 1:
  64. offset += file->f_pos;
  65. break;
  66. case 2:
  67. offset += size;
  68. break;
  69. }
  70. if (offset < 0)
  71. return -EINVAL;
  72. file->f_pos = offset;
  73. return file->f_pos;
  74. }
  75. static ssize_t dev_nvram_read(struct file *file, char __user *buf,
  76. size_t count, loff_t *ppos)
  77. {
  78. ssize_t ret;
  79. char *tmp = NULL;
  80. ssize_t size;
  81. ret = -ENODEV;
  82. if (!ppc_md.nvram_size)
  83. goto out;
  84. ret = 0;
  85. size = ppc_md.nvram_size();
  86. if (*ppos >= size || size < 0)
  87. goto out;
  88. count = min_t(size_t, count, size - *ppos);
  89. count = min(count, PAGE_SIZE);
  90. ret = -ENOMEM;
  91. tmp = kmalloc(count, GFP_KERNEL);
  92. if (!tmp)
  93. goto out;
  94. ret = ppc_md.nvram_read(tmp, count, ppos);
  95. if (ret <= 0)
  96. goto out;
  97. if (copy_to_user(buf, tmp, ret))
  98. ret = -EFAULT;
  99. out:
  100. kfree(tmp);
  101. return ret;
  102. }
  103. static ssize_t dev_nvram_write(struct file *file, const char __user *buf,
  104. size_t count, loff_t *ppos)
  105. {
  106. ssize_t ret;
  107. char *tmp = NULL;
  108. ssize_t size;
  109. ret = -ENODEV;
  110. if (!ppc_md.nvram_size)
  111. goto out;
  112. ret = 0;
  113. size = ppc_md.nvram_size();
  114. if (*ppos >= size || size < 0)
  115. goto out;
  116. count = min_t(size_t, count, size - *ppos);
  117. count = min(count, PAGE_SIZE);
  118. ret = -ENOMEM;
  119. tmp = kmalloc(count, GFP_KERNEL);
  120. if (!tmp)
  121. goto out;
  122. ret = -EFAULT;
  123. if (copy_from_user(tmp, buf, count))
  124. goto out;
  125. ret = ppc_md.nvram_write(tmp, count, ppos);
  126. out:
  127. kfree(tmp);
  128. return ret;
  129. }
  130. static long dev_nvram_ioctl(struct file *file, unsigned int cmd,
  131. unsigned long arg)
  132. {
  133. switch(cmd) {
  134. #ifdef CONFIG_PPC_PMAC
  135. case OBSOLETE_PMAC_NVRAM_GET_OFFSET:
  136. printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n");
  137. case IOC_NVRAM_GET_OFFSET: {
  138. int part, offset;
  139. if (!machine_is(powermac))
  140. return -EINVAL;
  141. if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0)
  142. return -EFAULT;
  143. if (part < pmac_nvram_OF || part > pmac_nvram_NR)
  144. return -EINVAL;
  145. offset = pmac_get_partition(part);
  146. if (offset < 0)
  147. return offset;
  148. if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0)
  149. return -EFAULT;
  150. return 0;
  151. }
  152. #endif /* CONFIG_PPC_PMAC */
  153. default:
  154. return -EINVAL;
  155. }
  156. }
  157. const 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. .unlocked_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 __init 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 "%4d \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 __init 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 __init 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. * nvram_remove_partition - Remove one or more partitions in nvram
  207. * @name: name of the partition to remove, or NULL for a
  208. * signature only match
  209. * @sig: signature of the partition(s) to remove
  210. */
  211. static int __init nvram_remove_partition(const char *name, int sig)
  212. {
  213. struct nvram_partition *part, *prev, *tmp;
  214. int rc;
  215. list_for_each_entry(part, &nvram_part->partition, partition) {
  216. if (part->header.signature != sig)
  217. continue;
  218. if (name && strncmp(name, part->header.name, 12))
  219. continue;
  220. /* Make partition a free partition */
  221. part->header.signature = NVRAM_SIG_FREE;
  222. sprintf(part->header.name, "wwwwwwwwwwww");
  223. part->header.checksum = nvram_checksum(&part->header);
  224. rc = nvram_write_header(part);
  225. if (rc <= 0) {
  226. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  227. return rc;
  228. }
  229. }
  230. /* Merge contiguous ones */
  231. prev = NULL;
  232. list_for_each_entry_safe(part, tmp, &nvram_part->partition, partition) {
  233. if (part->header.signature != NVRAM_SIG_FREE) {
  234. prev = NULL;
  235. continue;
  236. }
  237. if (prev) {
  238. prev->header.length += part->header.length;
  239. prev->header.checksum = nvram_checksum(&part->header);
  240. rc = nvram_write_header(part);
  241. if (rc <= 0) {
  242. printk(KERN_ERR "nvram_remove_partition: nvram_write failed (%d)\n", rc);
  243. return rc;
  244. }
  245. list_del(&part->partition);
  246. kfree(part);
  247. } else
  248. prev = part;
  249. }
  250. return 0;
  251. }
  252. /**
  253. * nvram_create_partition - Create a partition in nvram
  254. * @name: name of the partition to create
  255. * @sig: signature of the partition to create
  256. * @req_size: size of data to allocate in bytes
  257. * @min_size: minimum acceptable size (0 means req_size)
  258. *
  259. * Returns a negative error code or a positive nvram index
  260. * of the beginning of the data area of the newly created
  261. * partition. If you provided a min_size smaller than req_size
  262. * you need to query for the actual size yourself after the
  263. * call using nvram_partition_get_size().
  264. */
  265. static loff_t __init nvram_create_partition(const char *name, int sig,
  266. int req_size, int min_size)
  267. {
  268. struct nvram_partition *part;
  269. struct nvram_partition *new_part;
  270. struct nvram_partition *free_part = NULL;
  271. static char nv_init_vals[16];
  272. loff_t tmp_index;
  273. long size = 0;
  274. int rc;
  275. /* Convert sizes from bytes to blocks */
  276. req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  277. min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN;
  278. /* If no minimum size specified, make it the same as the
  279. * requested size
  280. */
  281. if (min_size == 0)
  282. min_size = req_size;
  283. if (min_size > req_size)
  284. return -EINVAL;
  285. /* Now add one block to each for the header */
  286. req_size += 1;
  287. min_size += 1;
  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 >= req_size) {
  294. size = req_size;
  295. free_part = part;
  296. break;
  297. }
  298. if (part->header.length > size &&
  299. part->header.length >= min_size) {
  300. size = part->header.length;
  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. pr_err("nvram_create_os_partition: kmalloc failed\n");
  310. return -ENOMEM;
  311. }
  312. new_part->index = free_part->index;
  313. new_part->header.signature = sig;
  314. new_part->header.length = size;
  315. strncpy(new_part->header.name, name, 12);
  316. new_part->header.checksum = nvram_checksum(&new_part->header);
  317. rc = nvram_write_header(new_part);
  318. if (rc <= 0) {
  319. pr_err("nvram_create_os_partition: nvram_write_header "
  320. "failed (%d)\n", rc);
  321. return rc;
  322. }
  323. list_add_tail(&new_part->partition, &free_part->partition);
  324. /* Adjust or remove the partition we stole the space from */
  325. if (free_part->header.length > size) {
  326. free_part->index += size * NVRAM_BLOCK_LEN;
  327. free_part->header.length -= size;
  328. free_part->header.checksum = nvram_checksum(&free_part->header);
  329. rc = nvram_write_header(free_part);
  330. if (rc <= 0) {
  331. pr_err("nvram_create_os_partition: nvram_write_header "
  332. "failed (%d)\n", rc);
  333. return rc;
  334. }
  335. } else {
  336. list_del(&free_part->partition);
  337. kfree(free_part);
  338. }
  339. /* Clear the new partition */
  340. for (tmp_index = new_part->index + NVRAM_HEADER_LEN;
  341. tmp_index < ((size - 1) * NVRAM_BLOCK_LEN);
  342. tmp_index += NVRAM_BLOCK_LEN) {
  343. rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index);
  344. if (rc <= 0) {
  345. pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc);
  346. return rc;
  347. }
  348. }
  349. return new_part->index + NVRAM_HEADER_LEN;
  350. }
  351. /**
  352. * nvram_get_partition_size - Get the data size of an nvram partition
  353. * @data_index: This is the offset of the start of the data of
  354. * the partition. The same value that is returned by
  355. * nvram_create_partition().
  356. */
  357. static int nvram_get_partition_size(loff_t data_index)
  358. {
  359. struct nvram_partition *part;
  360. list_for_each_entry(part, &nvram_part->partition, partition) {
  361. if (part->index + NVRAM_HEADER_LEN == data_index)
  362. return (part->header.length - 1) * NVRAM_BLOCK_LEN;
  363. }
  364. return -1;
  365. }
  366. /**
  367. * nvram_find_partition - Find an nvram partition by signature and name
  368. * @name: Name of the partition or NULL for any name
  369. * @sig: Signature to test against
  370. * @out_size: if non-NULL, returns the size of the data part of the partition
  371. */
  372. loff_t nvram_find_partition(const char *name, int sig, int *out_size)
  373. {
  374. struct nvram_partition *p;
  375. list_for_each_entry(p, &nvram_part->partition, partition) {
  376. if (p->header.signature == sig &&
  377. (!name || !strncmp(p->header.name, name, 12))) {
  378. if (out_size)
  379. *out_size = (p->header.length - 1) *
  380. NVRAM_BLOCK_LEN;
  381. return p->index + NVRAM_HEADER_LEN;
  382. }
  383. }
  384. return 0;
  385. }
  386. /* nvram_setup_partition
  387. *
  388. * This will setup the partition we need for buffering the
  389. * error logs and cleanup partitions if needed.
  390. *
  391. * The general strategy is the following:
  392. * 1.) If there is ppc64,linux partition large enough then use it.
  393. * 2.) If there is not a ppc64,linux partition large enough, search
  394. * for a free partition that is large enough.
  395. * 3.) If there is not a free partition large enough remove
  396. * _all_ OS partitions and consolidate the space.
  397. * 4.) Will first try getting a chunk that will satisfy the maximum
  398. * error log size (NVRAM_MAX_REQ).
  399. * 5.) If the max chunk cannot be allocated then try finding a chunk
  400. * that will satisfy the minum needed (NVRAM_MIN_REQ).
  401. */
  402. static int __init nvram_setup_partition(void)
  403. {
  404. struct list_head * p;
  405. struct nvram_partition * part;
  406. int rc;
  407. /* For now, we don't do any of this on pmac, until I
  408. * have figured out if it's worth killing some unused stuffs
  409. * in our nvram, as Apple defined partitions use pretty much
  410. * all of the space
  411. */
  412. if (machine_is(powermac))
  413. return -ENOSPC;
  414. /* see if we have an OS partition that meets our needs.
  415. will try getting the max we need. If not we'll delete
  416. partitions and try again. */
  417. list_for_each(p, &nvram_part->partition) {
  418. part = list_entry(p, struct nvram_partition, partition);
  419. if (part->header.signature != NVRAM_SIG_OS)
  420. continue;
  421. if (strcmp(part->header.name, "ppc64,linux"))
  422. continue;
  423. if ((part->header.length - 1) * NVRAM_BLOCK_LEN >= NVRAM_MIN_REQ) {
  424. /* found our partition */
  425. nvram_error_log_index = part->index + NVRAM_HEADER_LEN;
  426. nvram_error_log_size = ((part->header.length - 1) *
  427. NVRAM_BLOCK_LEN) - sizeof(struct err_log_info);
  428. return 0;
  429. }
  430. /* Found one but it's too small, remove it */
  431. nvram_remove_partition("ppc64,linux", NVRAM_SIG_OS);
  432. }
  433. /* try creating a partition with the free space we have */
  434. rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
  435. NVRAM_MAX_REQ, NVRAM_MIN_REQ);
  436. if (rc < 0) {
  437. /* need to free up some space, remove any "OS" partition */
  438. nvram_remove_partition(NULL, NVRAM_SIG_OS);
  439. /* Try again */
  440. rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS,
  441. NVRAM_MAX_REQ, NVRAM_MIN_REQ);
  442. if (rc < 0) {
  443. pr_err("nvram_create_partition: Could not find"
  444. " enough space in NVRAM for partition\n");
  445. return rc;
  446. }
  447. }
  448. nvram_error_log_index = rc;
  449. nvram_error_log_size = nvram_get_partition_size(rc) - sizeof(struct err_log_info);
  450. return 0;
  451. }
  452. static int __init nvram_scan_partitions(void)
  453. {
  454. loff_t cur_index = 0;
  455. struct nvram_header phead;
  456. struct nvram_partition * tmp_part;
  457. unsigned char c_sum;
  458. char * header;
  459. int total_size;
  460. int err;
  461. if (ppc_md.nvram_size == NULL)
  462. return -ENODEV;
  463. total_size = ppc_md.nvram_size();
  464. header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL);
  465. if (!header) {
  466. printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n");
  467. return -ENOMEM;
  468. }
  469. while (cur_index < total_size) {
  470. err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index);
  471. if (err != NVRAM_HEADER_LEN) {
  472. printk(KERN_ERR "nvram_scan_partitions: Error parsing "
  473. "nvram partitions\n");
  474. goto out;
  475. }
  476. cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */
  477. memcpy(&phead, header, NVRAM_HEADER_LEN);
  478. err = 0;
  479. c_sum = nvram_checksum(&phead);
  480. if (c_sum != phead.checksum) {
  481. printk(KERN_WARNING "WARNING: nvram partition checksum"
  482. " was %02x, should be %02x!\n",
  483. phead.checksum, c_sum);
  484. printk(KERN_WARNING "Terminating nvram partition scan\n");
  485. goto out;
  486. }
  487. if (!phead.length) {
  488. printk(KERN_WARNING "WARNING: nvram corruption "
  489. "detected: 0-length partition\n");
  490. goto out;
  491. }
  492. tmp_part = (struct nvram_partition *)
  493. kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  494. err = -ENOMEM;
  495. if (!tmp_part) {
  496. printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n");
  497. goto out;
  498. }
  499. memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN);
  500. tmp_part->index = cur_index;
  501. list_add_tail(&tmp_part->partition, &nvram_part->partition);
  502. cur_index += phead.length * NVRAM_BLOCK_LEN;
  503. }
  504. err = 0;
  505. out:
  506. kfree(header);
  507. return err;
  508. }
  509. static int __init nvram_init(void)
  510. {
  511. int error;
  512. int rc;
  513. BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16);
  514. if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0)
  515. return -ENODEV;
  516. rc = misc_register(&nvram_dev);
  517. if (rc != 0) {
  518. printk(KERN_ERR "nvram_init: failed to register device\n");
  519. return rc;
  520. }
  521. /* initialize our anchor for the nvram partition list */
  522. nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL);
  523. if (!nvram_part) {
  524. printk(KERN_ERR "nvram_init: Failed kmalloc\n");
  525. return -ENOMEM;
  526. }
  527. INIT_LIST_HEAD(&nvram_part->partition);
  528. /* Get all the NVRAM partitions */
  529. error = nvram_scan_partitions();
  530. if (error) {
  531. printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n");
  532. return error;
  533. }
  534. if(nvram_setup_partition())
  535. printk(KERN_WARNING "nvram_init: Could not find nvram partition"
  536. " for nvram buffered error logging.\n");
  537. #ifdef DEBUG_NVRAM
  538. nvram_print_partitions("NVRAM Partitions");
  539. #endif
  540. return rc;
  541. }
  542. void __exit nvram_cleanup(void)
  543. {
  544. misc_deregister( &nvram_dev );
  545. }
  546. #ifdef CONFIG_PPC_PSERIES
  547. /* nvram_write_error_log
  548. *
  549. * We need to buffer the error logs into nvram to ensure that we have
  550. * the failure information to decode. If we have a severe error there
  551. * is no way to guarantee that the OS or the machine is in a state to
  552. * get back to user land and write the error to disk. For example if
  553. * the SCSI device driver causes a Machine Check by writing to a bad
  554. * IO address, there is no way of guaranteeing that the device driver
  555. * is in any state that is would also be able to write the error data
  556. * captured to disk, thus we buffer it in NVRAM for analysis on the
  557. * next boot.
  558. *
  559. * In NVRAM the partition containing the error log buffer will looks like:
  560. * Header (in bytes):
  561. * +-----------+----------+--------+------------+------------------+
  562. * | signature | checksum | length | name | data |
  563. * |0 |1 |2 3|4 15|16 length-1|
  564. * +-----------+----------+--------+------------+------------------+
  565. *
  566. * The 'data' section would look like (in bytes):
  567. * +--------------+------------+-----------------------------------+
  568. * | event_logged | sequence # | error log |
  569. * |0 3|4 7|8 nvram_error_log_size-1|
  570. * +--------------+------------+-----------------------------------+
  571. *
  572. * event_logged: 0 if event has not been logged to syslog, 1 if it has
  573. * sequence #: The unique sequence # for each event. (until it wraps)
  574. * error log: The error log from event_scan
  575. */
  576. int nvram_write_error_log(char * buff, int length,
  577. unsigned int err_type, unsigned int error_log_cnt)
  578. {
  579. int rc;
  580. loff_t tmp_index;
  581. struct err_log_info info;
  582. if (nvram_error_log_index == -1) {
  583. return -ESPIPE;
  584. }
  585. if (length > nvram_error_log_size) {
  586. length = nvram_error_log_size;
  587. }
  588. info.error_type = err_type;
  589. info.seq_num = error_log_cnt;
  590. tmp_index = nvram_error_log_index;
  591. rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index);
  592. if (rc <= 0) {
  593. printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
  594. return rc;
  595. }
  596. rc = ppc_md.nvram_write(buff, length, &tmp_index);
  597. if (rc <= 0) {
  598. printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc);
  599. return rc;
  600. }
  601. return 0;
  602. }
  603. /* nvram_read_error_log
  604. *
  605. * Reads nvram for error log for at most 'length'
  606. */
  607. int nvram_read_error_log(char * buff, int length,
  608. unsigned int * err_type, unsigned int * error_log_cnt)
  609. {
  610. int rc;
  611. loff_t tmp_index;
  612. struct err_log_info info;
  613. if (nvram_error_log_index == -1)
  614. return -1;
  615. if (length > nvram_error_log_size)
  616. length = nvram_error_log_size;
  617. tmp_index = nvram_error_log_index;
  618. rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index);
  619. if (rc <= 0) {
  620. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  621. return rc;
  622. }
  623. rc = ppc_md.nvram_read(buff, length, &tmp_index);
  624. if (rc <= 0) {
  625. printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc);
  626. return rc;
  627. }
  628. *error_log_cnt = info.seq_num;
  629. *err_type = info.error_type;
  630. return 0;
  631. }
  632. /* This doesn't actually zero anything, but it sets the event_logged
  633. * word to tell that this event is safely in syslog.
  634. */
  635. int nvram_clear_error_log(void)
  636. {
  637. loff_t tmp_index;
  638. int clear_word = ERR_FLAG_ALREADY_LOGGED;
  639. int rc;
  640. if (nvram_error_log_index == -1)
  641. return -1;
  642. tmp_index = nvram_error_log_index;
  643. rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index);
  644. if (rc <= 0) {
  645. printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc);
  646. return rc;
  647. }
  648. return 0;
  649. }
  650. #endif /* CONFIG_PPC_PSERIES */
  651. module_init(nvram_init);
  652. module_exit(nvram_cleanup);
  653. MODULE_LICENSE("GPL");