1 /* 2 * HP WatchDog Driver 3 * based on 4 * 5 * SoftDog 0.05: A Software Watchdog Device 6 * 7 * (c) Copyright 2007 Hewlett-Packard Development Company, L.P. 8 * Thomas Mingarelli <thomas.mingarelli@hp.com> 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * version 2 as published by the Free Software Foundation 13 * 14 */ 15 16 #include <linux/device.h> 17 #include <linux/fs.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/io.h> 21 #include <linux/irq.h> 22 #include <linux/kernel.h> 23 #include <linux/miscdevice.h> 24 #include <linux/mm.h> 25 #include <linux/module.h> 26 #include <linux/kdebug.h> 27 #include <linux/moduleparam.h> 28 #include <linux/notifier.h> 29 #include <linux/pci.h> 30 #include <linux/pci_ids.h> 31 #include <linux/reboot.h> 32 #include <linux/sched.h> 33 #include <linux/timer.h> 34 #include <linux/types.h> 35 #include <linux/uaccess.h> 36 #include <linux/watchdog.h> 37 #include <linux/dmi.h> 38 #include <linux/efi.h> 39 #include <linux/string.h> 40 #include <linux/bootmem.h> 41 #include <linux/slab.h> 42 #include <asm/dmi.h> 43 #include <asm/desc.h> 44 #include <asm/kdebug.h> 45 46 #define PCI_BIOS32_SD_VALUE 0x5F32335F /* "_32_" */ 47 #define CRU_BIOS_SIGNATURE_VALUE 0x55524324 48 #define PCI_BIOS32_PARAGRAPH_LEN 16 49 #define PCI_ROM_BASE1 0x000F0000 50 #define ROM_SIZE 0x10000 51 52 struct bios32_service_dir { 53 u32 signature; 54 u32 entry_point; 55 u8 revision; 56 u8 length; 57 u8 checksum; 58 u8 reserved[5]; 59 }; 60 61 /* type 212 */ 62 struct smbios_cru64_info { 63 u8 type; 64 u8 byte_length; 65 u16 handle; 66 u32 signature; 67 u64 physical_address; 68 u32 double_length; 69 u32 double_offset; 70 }; 71 #define SMBIOS_CRU64_INFORMATION 212 72 73 struct cmn_registers { 74 union { 75 struct { 76 u8 ral; 77 u8 rah; 78 u16 rea2; 79 }; 80 u32 reax; 81 } u1; 82 union { 83 struct { 84 u8 rbl; 85 u8 rbh; 86 u8 reb2l; 87 u8 reb2h; 88 }; 89 u32 rebx; 90 } u2; 91 union { 92 struct { 93 u8 rcl; 94 u8 rch; 95 u16 rec2; 96 }; 97 u32 recx; 98 } u3; 99 union { 100 struct { 101 u8 rdl; 102 u8 rdh; 103 u16 red2; 104 }; 105 u32 redx; 106 } u4; 107 108 u32 resi; 109 u32 redi; 110 u16 rds; 111 u16 res; 112 u32 reflags; 113 } __attribute__((packed)); 114 115 #define DEFAULT_MARGIN 30 116 static unsigned int soft_margin = DEFAULT_MARGIN; /* in seconds */ 117 static unsigned int reload; /* the computed soft_margin */ 118 static int nowayout = WATCHDOG_NOWAYOUT; 119 static char expect_release; 120 static unsigned long hpwdt_is_open; 121 122 static void __iomem *pci_mem_addr; /* the PCI-memory address */ 123 static unsigned long __iomem *hpwdt_timer_reg; 124 static unsigned long __iomem *hpwdt_timer_con; 125 126 static DEFINE_SPINLOCK(rom_lock); 127 128 static void *cru_rom_addr; 129 130 static struct cmn_registers cmn_regs; 131 132 static struct pci_device_id hpwdt_devices[] = { 133 { 134 .vendor = PCI_VENDOR_ID_COMPAQ, 135 .device = 0xB203, 136 .subvendor = PCI_ANY_ID, 137 .subdevice = PCI_ANY_ID, 138 }, 139 {0}, /* terminate list */ 140 }; 141 MODULE_DEVICE_TABLE(pci, hpwdt_devices); 142 143 #ifndef CONFIG_X86_64 144 /* --32 Bit Bios------------------------------------------------------------ */ 145 146 #define HPWDT_ARCH 32 147 148 static void asminline_call(struct cmn_registers *pi86Regs, 149 unsigned long *pRomEntry) 150 { 151 asm("pushl %ebp \n\t" 152 "movl %esp, %ebp \n\t" 153 "pusha \n\t" 154 "pushf \n\t" 155 "push %es \n\t" 156 "push %ds \n\t" 157 "pop %es \n\t" 158 "movl 8(%ebp),%eax \n\t" 159 "movl 4(%eax),%ebx \n\t" 160 "movl 8(%eax),%ecx \n\t" 161 "movl 12(%eax),%edx \n\t" 162 "movl 16(%eax),%esi \n\t" 163 "movl 20(%eax),%edi \n\t" 164 "movl (%eax),%eax \n\t" 165 "push %cs \n\t" 166 "call *12(%ebp) \n\t" 167 "pushf \n\t" 168 "pushl %eax \n\t" 169 "movl 8(%ebp),%eax \n\t" 170 "movl %ebx,4(%eax) \n\t" 171 "movl %ecx,8(%eax) \n\t" 172 "movl %edx,12(%eax) \n\t" 173 "movl %esi,16(%eax) \n\t" 174 "movl %edi,20(%eax) \n\t" 175 "movw %ds,24(%eax) \n\t" 176 "movw %es,26(%eax) \n\t" 177 "popl %ebx \n\t" 178 "movl %ebx,(%eax) \n\t" 179 "popl %ebx \n\t" 180 "movl %ebx,28(%eax) \n\t" 181 "pop %es \n\t" 182 "popf \n\t" 183 "popa \n\t" 184 "leave \n\t" "ret"); 185 } 186 187 /* 188 * cru_detect 189 * 190 * Routine Description: 191 * This function uses the 32-bit BIOS Service Directory record to 192 * search for a $CRU record. 193 * 194 * Return Value: 195 * 0 : SUCCESS 196 * <0 : FAILURE 197 */ 198 static int __devinit cru_detect(unsigned long map_entry, 199 unsigned long map_offset) 200 { 201 void *bios32_map; 202 unsigned long *bios32_entrypoint; 203 unsigned long cru_physical_address; 204 unsigned long cru_length; 205 unsigned long physical_bios_base = 0; 206 unsigned long physical_bios_offset = 0; 207 int retval = -ENODEV; 208 209 bios32_map = ioremap(map_entry, (2 * PAGE_SIZE)); 210 211 if (bios32_map == NULL) 212 return -ENODEV; 213 214 bios32_entrypoint = bios32_map + map_offset; 215 216 cmn_regs.u1.reax = CRU_BIOS_SIGNATURE_VALUE; 217 218 asminline_call(&cmn_regs, bios32_entrypoint); 219 220 if (cmn_regs.u1.ral != 0) { 221 printk(KERN_WARNING 222 "hpwdt: Call succeeded but with an error: 0x%x\n", 223 cmn_regs.u1.ral); 224 } else { 225 physical_bios_base = cmn_regs.u2.rebx; 226 physical_bios_offset = cmn_regs.u4.redx; 227 cru_length = cmn_regs.u3.recx; 228 cru_physical_address = 229 physical_bios_base + physical_bios_offset; 230 231 /* If the values look OK, then map it in. */ 232 if ((physical_bios_base + physical_bios_offset)) { 233 cru_rom_addr = 234 ioremap(cru_physical_address, cru_length); 235 if (cru_rom_addr) 236 retval = 0; 237 } 238 239 printk(KERN_DEBUG "hpwdt: CRU Base Address: 0x%lx\n", 240 physical_bios_base); 241 printk(KERN_DEBUG "hpwdt: CRU Offset Address: 0x%lx\n", 242 physical_bios_offset); 243 printk(KERN_DEBUG "hpwdt: CRU Length: 0x%lx\n", 244 cru_length); 245 printk(KERN_DEBUG "hpwdt: CRU Mapped Address: 0x%x\n", 246 (unsigned int)&cru_rom_addr); 247 } 248 iounmap(bios32_map); 249 return retval; 250 } 251 252 /* 253 * bios_checksum 254 */ 255 static int __devinit bios_checksum(const char __iomem *ptr, int len) 256 { 257 char sum = 0; 258 int i; 259 260 /* 261 * calculate checksum of size bytes. This should add up 262 * to zero if we have a valid header. 263 */ 264 for (i = 0; i < len; i++) 265 sum += ptr[i]; 266 267 return ((sum == 0) && (len > 0)); 268 } 269 270 /* 271 * bios32_present 272 * 273 * Routine Description: 274 * This function finds the 32-bit BIOS Service Directory 275 * 276 * Return Value: 277 * 0 : SUCCESS 278 * <0 : FAILURE 279 */ 280 static int __devinit bios32_present(const char __iomem *p) 281 { 282 struct bios32_service_dir *bios_32_ptr; 283 int length; 284 unsigned long map_entry, map_offset; 285 286 bios_32_ptr = (struct bios32_service_dir *) p; 287 288 /* 289 * Search for signature by checking equal to the swizzled value 290 * instead of calling another routine to perform a strcmp. 291 */ 292 if (bios_32_ptr->signature == PCI_BIOS32_SD_VALUE) { 293 length = bios_32_ptr->length * PCI_BIOS32_PARAGRAPH_LEN; 294 if (bios_checksum(p, length)) { 295 /* 296 * According to the spec, we're looking for the 297 * first 4KB-aligned address below the entrypoint 298 * listed in the header. The Service Directory code 299 * is guaranteed to occupy no more than 2 4KB pages. 300 */ 301 map_entry = bios_32_ptr->entry_point & ~(PAGE_SIZE - 1); 302 map_offset = bios_32_ptr->entry_point - map_entry; 303 304 return cru_detect(map_entry, map_offset); 305 } 306 } 307 return -ENODEV; 308 } 309 310 static int __devinit detect_cru_service(void) 311 { 312 char __iomem *p, *q; 313 int rc = -1; 314 315 /* 316 * Search from 0x0f0000 through 0x0fffff, inclusive. 317 */ 318 p = ioremap(PCI_ROM_BASE1, ROM_SIZE); 319 if (p == NULL) 320 return -ENOMEM; 321 322 for (q = p; q < p + ROM_SIZE; q += 16) { 323 rc = bios32_present(q); 324 if (!rc) 325 break; 326 } 327 iounmap(p); 328 return rc; 329 } 330 331 #else 332 /* --64 Bit Bios------------------------------------------------------------ */ 333 334 #define HPWDT_ARCH 64 335 336 static void asminline_call(struct cmn_registers *pi86Regs, 337 unsigned long *pRomEntry) 338 { 339 asm("pushq %rbp \n\t" 340 "movq %rsp, %rbp \n\t" 341 "pushq %rax \n\t" 342 "pushq %rbx \n\t" 343 "pushq %rdx \n\t" 344 "pushq %r12 \n\t" 345 "pushq %r9 \n\t" 346 "movq %rsi, %r12 \n\t" 347 "movq %rdi, %r9 \n\t" 348 "movl 4(%r9),%ebx \n\t" 349 "movl 8(%r9),%ecx \n\t" 350 "movl 12(%r9),%edx \n\t" 351 "movl 16(%r9),%esi \n\t" 352 "movl 20(%r9),%edi \n\t" 353 "movl (%r9),%eax \n\t" 354 "call *%r12 \n\t" 355 "pushfq \n\t" 356 "popq %r12 \n\t" 357 "popfq \n\t" 358 "movl %eax, (%r9) \n\t" 359 "movl %ebx, 4(%r9) \n\t" 360 "movl %ecx, 8(%r9) \n\t" 361 "movl %edx, 12(%r9) \n\t" 362 "movl %esi, 16(%r9) \n\t" 363 "movl %edi, 20(%r9) \n\t" 364 "movq %r12, %rax \n\t" 365 "movl %eax, 28(%r9) \n\t" 366 "popq %r9 \n\t" 367 "popq %r12 \n\t" 368 "popq %rdx \n\t" 369 "popq %rbx \n\t" 370 "popq %rax \n\t" 371 "leave \n\t" "ret"); 372 } 373 374 /* 375 * dmi_find_cru 376 * 377 * Routine Description: 378 * This function checks whether or not a SMBIOS/DMI record is 379 * the 64bit CRU info or not 380 */ 381 static void __devinit dmi_find_cru(const struct dmi_header *dm) 382 { 383 struct smbios_cru64_info *smbios_cru64_ptr; 384 unsigned long cru_physical_address; 385 386 if (dm->type == SMBIOS_CRU64_INFORMATION) { 387 smbios_cru64_ptr = (struct smbios_cru64_info *) dm; 388 if (smbios_cru64_ptr->signature == CRU_BIOS_SIGNATURE_VALUE) { 389 cru_physical_address = 390 smbios_cru64_ptr->physical_address + 391 smbios_cru64_ptr->double_offset; 392 cru_rom_addr = ioremap(cru_physical_address, 393 smbios_cru64_ptr->double_length); 394 } 395 } 396 } 397 398 static int __devinit detect_cru_service(void) 399 { 400 cru_rom_addr = NULL; 401 402 dmi_walk(dmi_find_cru); 403 404 /* if cru_rom_addr has been set then we found a CRU service */ 405 return ((cru_rom_addr != NULL)? 0: -ENODEV); 406 } 407 408 /* ------------------------------------------------------------------------- */ 409 410 #endif 411 412 /* 413 * NMI Handler 414 */ 415 static int hpwdt_pretimeout(struct notifier_block *nb, unsigned long ulReason, 416 void *data) 417 { 418 static unsigned long rom_pl; 419 static int die_nmi_called; 420 421 if (ulReason != DIE_NMI && ulReason != DIE_NMI_IPI) 422 return NOTIFY_OK; 423 424 spin_lock_irqsave(&rom_lock, rom_pl); 425 if (!die_nmi_called) 426 asminline_call(&cmn_regs, cru_rom_addr); 427 die_nmi_called = 1; 428 spin_unlock_irqrestore(&rom_lock, rom_pl); 429 if (cmn_regs.u1.ral == 0) { 430 printk(KERN_WARNING "hpwdt: An NMI occurred, " 431 "but unable to determine source.\n"); 432 } else { 433 panic("An NMI occurred, please see the Integrated " 434 "Management Log for details.\n"); 435 } 436 437 return NOTIFY_STOP; 438 } 439 440 /* 441 * Watchdog operations 442 */ 443 static void hpwdt_start(void) 444 { 445 reload = (soft_margin * 1000) / 128; 446 iowrite16(reload, hpwdt_timer_reg); 447 iowrite16(0x85, hpwdt_timer_con); 448 } 449 450 static void hpwdt_stop(void) 451 { 452 unsigned long data; 453 454 data = ioread16(hpwdt_timer_con); 455 data &= 0xFE; 456 iowrite16(data, hpwdt_timer_con); 457 } 458 459 static void hpwdt_ping(void) 460 { 461 iowrite16(reload, hpwdt_timer_reg); 462 } 463 464 static int hpwdt_change_timer(int new_margin) 465 { 466 /* Arbitrary, can't find the card's limits */ 467 if (new_margin < 30 || new_margin > 600) { 468 printk(KERN_WARNING 469 "hpwdt: New value passed in is invalid: %d seconds.\n", 470 new_margin); 471 return -EINVAL; 472 } 473 474 soft_margin = new_margin; 475 printk(KERN_DEBUG 476 "hpwdt: New timer passed in is %d seconds.\n", 477 new_margin); 478 reload = (soft_margin * 1000) / 128; 479 480 return 0; 481 } 482 483 /* 484 * /dev/watchdog handling 485 */ 486 static int hpwdt_open(struct inode *inode, struct file *file) 487 { 488 /* /dev/watchdog can only be opened once */ 489 if (test_and_set_bit(0, &hpwdt_is_open)) 490 return -EBUSY; 491 492 /* Start the watchdog */ 493 hpwdt_start(); 494 hpwdt_ping(); 495 496 return nonseekable_open(inode, file); 497 } 498 499 static int hpwdt_release(struct inode *inode, struct file *file) 500 { 501 /* Stop the watchdog */ 502 if (expect_release == 42) { 503 hpwdt_stop(); 504 } else { 505 printk(KERN_CRIT 506 "hpwdt: Unexpected close, not stopping watchdog!\n"); 507 hpwdt_ping(); 508 } 509 510 expect_release = 0; 511 512 /* /dev/watchdog is being closed, make sure it can be re-opened */ 513 clear_bit(0, &hpwdt_is_open); 514 515 return 0; 516 } 517 518 static ssize_t hpwdt_write(struct file *file, const char __user *data, 519 size_t len, loff_t *ppos) 520 { 521 /* See if we got the magic character 'V' and reload the timer */ 522 if (len) { 523 if (!nowayout) { 524 size_t i; 525 526 /* note: just in case someone wrote the magic character 527 * five months ago... */ 528 expect_release = 0; 529 530 /* scan to see whether or not we got the magic char. */ 531 for (i = 0; i != len; i++) { 532 char c; 533 if (get_user(c, data+i)) 534 return -EFAULT; 535 if (c == 'V') 536 expect_release = 42; 537 } 538 } 539 540 /* someone wrote to us, we should reload the timer */ 541 hpwdt_ping(); 542 } 543 544 return len; 545 } 546 547 static struct watchdog_info ident = { 548 .options = WDIOF_SETTIMEOUT | 549 WDIOF_KEEPALIVEPING | 550 WDIOF_MAGICCLOSE, 551 .identity = "HP iLO2 HW Watchdog Timer", 552 }; 553 554 static long hpwdt_ioctl(struct file *file, unsigned int cmd, 555 unsigned long arg) 556 { 557 void __user *argp = (void __user *)arg; 558 int __user *p = argp; 559 int new_margin; 560 int ret = -ENOTTY; 561 562 switch (cmd) { 563 case WDIOC_GETSUPPORT: 564 ret = 0; 565 if (copy_to_user(argp, &ident, sizeof(ident))) 566 ret = -EFAULT; 567 break; 568 569 case WDIOC_GETSTATUS: 570 case WDIOC_GETBOOTSTATUS: 571 ret = put_user(0, p); 572 break; 573 574 case WDIOC_KEEPALIVE: 575 hpwdt_ping(); 576 ret = 0; 577 break; 578 579 case WDIOC_SETTIMEOUT: 580 ret = get_user(new_margin, p); 581 if (ret) 582 break; 583 584 ret = hpwdt_change_timer(new_margin); 585 if (ret) 586 break; 587 588 hpwdt_ping(); 589 /* Fall */ 590 case WDIOC_GETTIMEOUT: 591 ret = put_user(soft_margin, p); 592 break; 593 } 594 return ret; 595 } 596 597 /* 598 * Kernel interfaces 599 */ 600 static struct file_operations hpwdt_fops = { 601 .owner = THIS_MODULE, 602 .llseek = no_llseek, 603 .write = hpwdt_write, 604 .unlocked_ioctl = hpwdt_ioctl, 605 .open = hpwdt_open, 606 .release = hpwdt_release, 607 }; 608 609 static struct miscdevice hpwdt_miscdev = { 610 .minor = WATCHDOG_MINOR, 611 .name = "watchdog", 612 .fops = &hpwdt_fops, 613 }; 614 615 static struct notifier_block die_notifier = { 616 .notifier_call = hpwdt_pretimeout, 617 .priority = 0x7FFFFFFF, 618 }; 619 620 /* 621 * Init & Exit 622 */ 623 624 static int __devinit hpwdt_init_one(struct pci_dev *dev, 625 const struct pci_device_id *ent) 626 { 627 int retval; 628 629 /* 630 * First let's find out if we are on an iLO2 server. We will 631 * not run on a legacy ASM box. 632 */ 633 if (dev->subsystem_vendor != PCI_VENDOR_ID_HP) { 634 dev_warn(&dev->dev, 635 "This server does not have an iLO2 ASIC.\n"); 636 return -ENODEV; 637 } 638 639 if (pci_enable_device(dev)) { 640 dev_warn(&dev->dev, 641 "Not possible to enable PCI Device: 0x%x:0x%x.\n", 642 ent->vendor, ent->device); 643 return -ENODEV; 644 } 645 646 pci_mem_addr = pci_iomap(dev, 1, 0x80); 647 if (!pci_mem_addr) { 648 dev_warn(&dev->dev, 649 "Unable to detect the iLO2 server memory.\n"); 650 retval = -ENOMEM; 651 goto error_pci_iomap; 652 } 653 hpwdt_timer_reg = pci_mem_addr + 0x70; 654 hpwdt_timer_con = pci_mem_addr + 0x72; 655 656 /* Make sure that we have a valid soft_margin */ 657 if (hpwdt_change_timer(soft_margin)) 658 hpwdt_change_timer(DEFAULT_MARGIN); 659 660 /* 661 * We need to map the ROM to get the CRU service. 662 * For 32 bit Operating Systems we need to go through the 32 Bit 663 * BIOS Service Directory 664 * For 64 bit Operating Systems we get that service through SMBIOS. 665 */ 666 retval = detect_cru_service(); 667 if (retval < 0) { 668 dev_warn(&dev->dev, 669 "Unable to detect the %d Bit CRU Service.\n", 670 HPWDT_ARCH); 671 goto error_get_cru; 672 } 673 674 /* 675 * We know this is the only CRU call we need to make so lets keep as 676 * few instructions as possible once the NMI comes in. 677 */ 678 cmn_regs.u1.rah = 0x0D; 679 cmn_regs.u1.ral = 0x02; 680 681 retval = register_die_notifier(&die_notifier); 682 if (retval != 0) { 683 dev_warn(&dev->dev, 684 "Unable to register a die notifier (err=%d).\n", 685 retval); 686 goto error_die_notifier; 687 } 688 689 retval = misc_register(&hpwdt_miscdev); 690 if (retval < 0) { 691 dev_warn(&dev->dev, 692 "Unable to register miscdev on minor=%d (err=%d).\n", 693 WATCHDOG_MINOR, retval); 694 goto error_misc_register; 695 } 696 697 printk(KERN_INFO 698 "hp Watchdog Timer Driver: 1.00" 699 ", timer margin: %d seconds( nowayout=%d).\n", 700 soft_margin, nowayout); 701 702 return 0; 703 704 error_misc_register: 705 unregister_die_notifier(&die_notifier); 706 error_die_notifier: 707 if (cru_rom_addr) 708 iounmap(cru_rom_addr); 709 error_get_cru: 710 pci_iounmap(dev, pci_mem_addr); 711 error_pci_iomap: 712 pci_disable_device(dev); 713 return retval; 714 } 715 716 static void __devexit hpwdt_exit(struct pci_dev *dev) 717 { 718 if (!nowayout) 719 hpwdt_stop(); 720 721 misc_deregister(&hpwdt_miscdev); 722 unregister_die_notifier(&die_notifier); 723 724 if (cru_rom_addr) 725 iounmap(cru_rom_addr); 726 pci_iounmap(dev, pci_mem_addr); 727 pci_disable_device(dev); 728 } 729 730 static struct pci_driver hpwdt_driver = { 731 .name = "hpwdt", 732 .id_table = hpwdt_devices, 733 .probe = hpwdt_init_one, 734 .remove = __devexit_p(hpwdt_exit), 735 }; 736 737 static void __exit hpwdt_cleanup(void) 738 { 739 pci_unregister_driver(&hpwdt_driver); 740 } 741 742 static int __init hpwdt_init(void) 743 { 744 return pci_register_driver(&hpwdt_driver); 745 } 746 747 MODULE_AUTHOR("Tom Mingarelli"); 748 MODULE_DESCRIPTION("hp watchdog driver"); 749 MODULE_LICENSE("GPL"); 750 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); 751 752 module_param(soft_margin, int, 0); 753 MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds"); 754 755 module_param(nowayout, int, 0); 756 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" 757 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 758 759 module_init(hpwdt_init); 760 module_exit(hpwdt_cleanup); 761