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