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