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