hpwdt.c 22.0 KB
Newer Older
1
/*
2
 *	HPE WatchDog Driver
3 4 5 6
 *	based on
 *
 *	SoftDog	0.05:	A Software Watchdog Device
 *
7 8
 *	(c) Copyright 2015 Hewlett Packard Enterprise Development LP
 *	Thomas Mingarelli <thomas.mingarelli@hpe.com>
9 10 11 12 13 14 15
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	version 2 as published by the Free Software Foundation
 *
 */

16 17
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

18 19 20
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/io.h>
21
#include <linux/bitops.h>
22 23 24 25 26 27 28 29 30
#include <linux/kernel.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/watchdog.h>
31
#ifdef CONFIG_HPWDT_NMI_DECODING
32
#include <linux/dmi.h>
33
#include <linux/spinlock.h>
34 35 36
#include <linux/nmi.h>
#include <linux/kdebug.h>
#include <linux/notifier.h>
37
#include <asm/set_memory.h>
38
#endif /* CONFIG_HPWDT_NMI_DECODING */
39
#include <asm/nmi.h>
40
#include <asm/frame.h>
41

42
#define HPWDT_VERSION			"1.4.0"
43
#define SECS_TO_TICKS(secs)		((secs) * 1000 / 128)
44 45
#define TICKS_TO_SECS(ticks)		((ticks) * 128 / 1000)
#define HPWDT_MAX_TIMER			TICKS_TO_SECS(65535)
46 47 48 49
#define DEFAULT_MARGIN			30

static unsigned int soft_margin = DEFAULT_MARGIN;	/* in seconds */
static unsigned int reload;			/* the computed soft_margin */
W
Wim Van Sebroeck 已提交
50
static bool nowayout = WATCHDOG_NOWAYOUT;
51 52 53 54
static char expect_release;
static unsigned long hpwdt_is_open;

static void __iomem *pci_mem_addr;		/* the PCI-memory address */
55
static unsigned long __iomem *hpwdt_nmistat;
56 57 58
static unsigned long __iomem *hpwdt_timer_reg;
static unsigned long __iomem *hpwdt_timer_con;

59
static const struct pci_device_id hpwdt_devices[] = {
60 61
	{ PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) },	/* iLO2 */
	{ PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) },	/* iLO3 */
62 63 64 65
	{0},			/* terminate list */
};
MODULE_DEVICE_TABLE(pci, hpwdt_devices);

66
#ifdef CONFIG_HPWDT_NMI_DECODING
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#define PCI_BIOS32_SD_VALUE		0x5F32335F	/* "_32_" */
#define CRU_BIOS_SIGNATURE_VALUE	0x55524324
#define PCI_BIOS32_PARAGRAPH_LEN	16
#define PCI_ROM_BASE1			0x000F0000
#define ROM_SIZE			0x10000

struct bios32_service_dir {
	u32 signature;
	u32 entry_point;
	u8 revision;
	u8 length;
	u8 checksum;
	u8 reserved[5];
};

/* type 212 */
struct smbios_cru64_info {
	u8 type;
	u8 byte_length;
	u16 handle;
	u32 signature;
	u64 physical_address;
	u32 double_length;
	u32 double_offset;
};
#define SMBIOS_CRU64_INFORMATION	212

94 95 96 97 98 99 100 101 102 103 104 105 106
/* type 219 */
struct smbios_proliant_info {
	u8 type;
	u8 byte_length;
	u16 handle;
	u32 power_features;
	u32 omega_features;
	u32 reserved;
	u32 misc_features;
};
#define SMBIOS_ICRU_INFORMATION		219


107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
struct cmn_registers {
	union {
		struct {
			u8 ral;
			u8 rah;
			u16 rea2;
		};
		u32 reax;
	} u1;
	union {
		struct {
			u8 rbl;
			u8 rbh;
			u8 reb2l;
			u8 reb2h;
		};
		u32 rebx;
	} u2;
	union {
		struct {
			u8 rcl;
			u8 rch;
			u16 rec2;
		};
		u32 recx;
	} u3;
	union {
		struct {
			u8 rdl;
			u8 rdh;
			u16 red2;
		};
		u32 redx;
	} u4;

	u32 resi;
	u32 redi;
	u16 rds;
	u16 res;
	u32 reflags;
}  __attribute__((packed));

149
static unsigned int hpwdt_nmi_decoding;
150
static unsigned int allow_kdump = 1;
151
static unsigned int is_icru;
152
static unsigned int is_uefi;
153 154 155 156
static DEFINE_SPINLOCK(rom_lock);
static void *cru_rom_addr;
static struct cmn_registers cmn_regs;

157 158
extern asmlinkage void asminline_call(struct cmn_registers *pi86Regs,
						unsigned long *pRomEntry);
159

160
#ifdef CONFIG_X86_32
161 162 163 164
/* --32 Bit Bios------------------------------------------------------------ */

#define HPWDT_ARCH	32

165
asm(".text                          \n\t"
166 167
    ".align 4                       \n\t"
    ".globl asminline_call	    \n"
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    "asminline_call:                \n\t"
    "pushl       %ebp               \n\t"
    "movl        %esp, %ebp         \n\t"
    "pusha                          \n\t"
    "pushf                          \n\t"
    "push        %es                \n\t"
    "push        %ds                \n\t"
    "pop         %es                \n\t"
    "movl        8(%ebp),%eax       \n\t"
    "movl        4(%eax),%ebx       \n\t"
    "movl        8(%eax),%ecx       \n\t"
    "movl        12(%eax),%edx      \n\t"
    "movl        16(%eax),%esi      \n\t"
    "movl        20(%eax),%edi      \n\t"
    "movl        (%eax),%eax        \n\t"
    "push        %cs                \n\t"
    "call        *12(%ebp)          \n\t"
    "pushf                          \n\t"
    "pushl       %eax               \n\t"
    "movl        8(%ebp),%eax       \n\t"
    "movl        %ebx,4(%eax)       \n\t"
    "movl        %ecx,8(%eax)       \n\t"
    "movl        %edx,12(%eax)      \n\t"
    "movl        %esi,16(%eax)      \n\t"
    "movl        %edi,20(%eax)      \n\t"
    "movw        %ds,24(%eax)       \n\t"
    "movw        %es,26(%eax)       \n\t"
    "popl        %ebx               \n\t"
    "movl        %ebx,(%eax)        \n\t"
    "popl        %ebx               \n\t"
    "movl        %ebx,28(%eax)      \n\t"
    "pop         %es                \n\t"
    "popf                           \n\t"
    "popa                           \n\t"
    "leave                          \n\t"
    "ret                            \n\t"
    ".previous");

206 207 208 209 210 211 212 213 214 215 216 217

/*
 *	cru_detect
 *
 *	Routine Description:
 *	This function uses the 32-bit BIOS Service Directory record to
 *	search for a $CRU record.
 *
 *	Return Value:
 *	0        :  SUCCESS
 *	<0       :  FAILURE
 */
B
Bill Pemberton 已提交
218
static int cru_detect(unsigned long map_entry,
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	unsigned long map_offset)
{
	void *bios32_map;
	unsigned long *bios32_entrypoint;
	unsigned long cru_physical_address;
	unsigned long cru_length;
	unsigned long physical_bios_base = 0;
	unsigned long physical_bios_offset = 0;
	int retval = -ENODEV;

	bios32_map = ioremap(map_entry, (2 * PAGE_SIZE));

	if (bios32_map == NULL)
		return -ENODEV;

	bios32_entrypoint = bios32_map + map_offset;

	cmn_regs.u1.reax = CRU_BIOS_SIGNATURE_VALUE;

238
	set_memory_x((unsigned long)bios32_map, 2);
239 240 241
	asminline_call(&cmn_regs, bios32_entrypoint);

	if (cmn_regs.u1.ral != 0) {
242
		pr_warn("Call succeeded but with an error: 0x%x\n",
243
			cmn_regs.u1.ral);
244 245 246 247 248
	} else {
		physical_bios_base = cmn_regs.u2.rebx;
		physical_bios_offset = cmn_regs.u4.redx;
		cru_length = cmn_regs.u3.recx;
		cru_physical_address =
249
			physical_bios_base + physical_bios_offset;
250 251 252 253

		/* If the values look OK, then map it in. */
		if ((physical_bios_base + physical_bios_offset)) {
			cru_rom_addr =
254
				ioremap(cru_physical_address, cru_length);
255
			if (cru_rom_addr) {
256 257
				set_memory_x((unsigned long)cru_rom_addr & PAGE_MASK,
					(cru_length + PAGE_SIZE - 1) >> PAGE_SHIFT);
258
				retval = 0;
259
			}
260 261
		}

262 263 264 265
		pr_debug("CRU Base Address:   0x%lx\n", physical_bios_base);
		pr_debug("CRU Offset Address: 0x%lx\n", physical_bios_offset);
		pr_debug("CRU Length:         0x%lx\n", cru_length);
		pr_debug("CRU Mapped Address: %p\n", &cru_rom_addr);
266 267 268 269 270
	}
	iounmap(bios32_map);
	return retval;
}

271 272 273
/*
 *	bios_checksum
 */
B
Bill Pemberton 已提交
274
static int bios_checksum(const char __iomem *ptr, int len)
275 276 277 278 279 280 281 282 283 284 285 286 287 288
{
	char sum = 0;
	int i;

	/*
	 * calculate checksum of size bytes. This should add up
	 * to zero if we have a valid header.
	 */
	for (i = 0; i < len; i++)
		sum += ptr[i];

	return ((sum == 0) && (len > 0));
}

289 290 291 292 293 294 295 296 297 298
/*
 *	bios32_present
 *
 *	Routine Description:
 *	This function finds the 32-bit BIOS Service Directory
 *
 *	Return Value:
 *	0        :  SUCCESS
 *	<0       :  FAILURE
 */
B
Bill Pemberton 已提交
299
static int bios32_present(const char __iomem *p)
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
{
	struct bios32_service_dir *bios_32_ptr;
	int length;
	unsigned long map_entry, map_offset;

	bios_32_ptr = (struct bios32_service_dir *) p;

	/*
	 * Search for signature by checking equal to the swizzled value
	 * instead of calling another routine to perform a strcmp.
	 */
	if (bios_32_ptr->signature == PCI_BIOS32_SD_VALUE) {
		length = bios_32_ptr->length * PCI_BIOS32_PARAGRAPH_LEN;
		if (bios_checksum(p, length)) {
			/*
			 * According to the spec, we're looking for the
			 * first 4KB-aligned address below the entrypoint
			 * listed in the header. The Service Directory code
			 * is guaranteed to occupy no more than 2 4KB pages.
			 */
			map_entry = bios_32_ptr->entry_point & ~(PAGE_SIZE - 1);
			map_offset = bios_32_ptr->entry_point - map_entry;

			return cru_detect(map_entry, map_offset);
		}
	}
	return -ENODEV;
}

B
Bill Pemberton 已提交
329
static int detect_cru_service(void)
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
{
	char __iomem *p, *q;
	int rc = -1;

	/*
	 * Search from 0x0f0000 through 0x0fffff, inclusive.
	 */
	p = ioremap(PCI_ROM_BASE1, ROM_SIZE);
	if (p == NULL)
		return -ENOMEM;

	for (q = p; q < p + ROM_SIZE; q += 16) {
		rc = bios32_present(q);
		if (!rc)
			break;
	}
	iounmap(p);
	return rc;
}
349 350 351
/* ------------------------------------------------------------------------- */
#endif /* CONFIG_X86_32 */
#ifdef CONFIG_X86_64
352 353 354 355
/* --64 Bit Bios------------------------------------------------------------ */

#define HPWDT_ARCH	64

356
asm(".text                      \n\t"
357
    ".align 4                   \n\t"
358 359
    ".globl asminline_call	\n\t"
    ".type asminline_call, @function \n\t"
360
    "asminline_call:            \n\t"
361
    FRAME_BEGIN
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    "pushq      %rax            \n\t"
    "pushq      %rbx            \n\t"
    "pushq      %rdx            \n\t"
    "pushq      %r12            \n\t"
    "pushq      %r9             \n\t"
    "movq       %rsi, %r12      \n\t"
    "movq       %rdi, %r9       \n\t"
    "movl       4(%r9),%ebx     \n\t"
    "movl       8(%r9),%ecx     \n\t"
    "movl       12(%r9),%edx    \n\t"
    "movl       16(%r9),%esi    \n\t"
    "movl       20(%r9),%edi    \n\t"
    "movl       (%r9),%eax      \n\t"
    "call       *%r12           \n\t"
    "pushfq                     \n\t"
    "popq        %r12           \n\t"
    "movl       %eax, (%r9)     \n\t"
    "movl       %ebx, 4(%r9)    \n\t"
    "movl       %ecx, 8(%r9)    \n\t"
    "movl       %edx, 12(%r9)   \n\t"
    "movl       %esi, 16(%r9)   \n\t"
    "movl       %edi, 20(%r9)   \n\t"
    "movq       %r12, %rax      \n\t"
    "movl       %eax, 28(%r9)   \n\t"
    "popq       %r9             \n\t"
    "popq       %r12            \n\t"
    "popq       %rdx            \n\t"
    "popq       %rbx            \n\t"
    "popq       %rax            \n\t"
391
    FRAME_END
392 393
    "ret                        \n\t"
    ".previous");
394 395 396 397 398

/*
 *	dmi_find_cru
 *
 *	Routine Description:
399
 *	This function checks whether or not a SMBIOS/DMI record is
400 401
 *	the 64bit CRU info or not
 */
B
Bill Pemberton 已提交
402
static void dmi_find_cru(const struct dmi_header *dm, void *dummy)
403 404 405 406 407 408 409 410
{
	struct smbios_cru64_info *smbios_cru64_ptr;
	unsigned long cru_physical_address;

	if (dm->type == SMBIOS_CRU64_INFORMATION) {
		smbios_cru64_ptr = (struct smbios_cru64_info *) dm;
		if (smbios_cru64_ptr->signature == CRU_BIOS_SIGNATURE_VALUE) {
			cru_physical_address =
411 412
				smbios_cru64_ptr->physical_address +
				smbios_cru64_ptr->double_offset;
413
			cru_rom_addr = ioremap(cru_physical_address,
414
				smbios_cru64_ptr->double_length);
415 416
			set_memory_x((unsigned long)cru_rom_addr & PAGE_MASK,
				smbios_cru64_ptr->double_length >> PAGE_SHIFT);
417 418 419 420
		}
	}
}

B
Bill Pemberton 已提交
421
static int detect_cru_service(void)
422 423 424
{
	cru_rom_addr = NULL;

425
	dmi_walk(dmi_find_cru, NULL);
426 427

	/* if cru_rom_addr has been set then we found a CRU service */
428
	return ((cru_rom_addr != NULL) ? 0 : -ENODEV);
429 430
}
/* ------------------------------------------------------------------------- */
431
#endif /* CONFIG_X86_64 */
432
#endif /* CONFIG_HPWDT_NMI_DECODING */
433 434 435 436 437 438

/*
 *	Watchdog operations
 */
static void hpwdt_start(void)
{
439
	reload = SECS_TO_TICKS(soft_margin);
440
	iowrite16(reload, hpwdt_timer_reg);
441
	iowrite8(0x85, hpwdt_timer_con);
442 443 444 445 446 447
}

static void hpwdt_stop(void)
{
	unsigned long data;

448
	data = ioread8(hpwdt_timer_con);
449
	data &= 0xFE;
450
	iowrite8(data, hpwdt_timer_con);
451 452 453 454 455 456 457 458 459
}

static void hpwdt_ping(void)
{
	iowrite16(reload, hpwdt_timer_reg);
}

static int hpwdt_change_timer(int new_margin)
{
460
	if (new_margin < 1 || new_margin > HPWDT_MAX_TIMER) {
461
		pr_warn("New value passed in is invalid: %d seconds\n",
462 463 464 465 466
			new_margin);
		return -EINVAL;
	}

	soft_margin = new_margin;
467
	pr_debug("New timer passed in is %d seconds\n", new_margin);
468
	reload = SECS_TO_TICKS(soft_margin);
469 470 471 472

	return 0;
}

473 474 475 476 477
static int hpwdt_time_left(void)
{
	return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
}

478
#ifdef CONFIG_HPWDT_NMI_DECODING
479 480 481 482 483
static int hpwdt_my_nmi(void)
{
	return ioread8(hpwdt_nmistat) & 0x6;
}

484 485 486
/*
 *	NMI Handler
 */
487
static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
488 489 490 491
{
	unsigned long rom_pl;
	static int die_nmi_called;

492
	if (!hpwdt_nmi_decoding)
493
		return NMI_DONE;
494

495 496 497
	if ((ulReason == NMI_UNKNOWN) && !hpwdt_my_nmi())
		return NMI_DONE;

498
	spin_lock_irqsave(&rom_lock, rom_pl);
499
	if (!die_nmi_called && !is_icru && !is_uefi)
500 501 502
		asminline_call(&cmn_regs, cru_rom_addr);
	die_nmi_called = 1;
	spin_unlock_irqrestore(&rom_lock, rom_pl);
503 504 505 506

	if (allow_kdump)
		hpwdt_stop();

507
	if (!is_icru && !is_uefi) {
508
		if (cmn_regs.u1.ral == 0) {
509 510
			nmi_panic(regs, "An NMI occurred, but unable to determine source.\n");
			return NMI_HANDLED;
511
		}
512
	}
513
	nmi_panic(regs, "An NMI occurred. Depending on your system the reason "
514 515 516 517 518 519
		"for the NMI is logged in any one of the following "
		"resources:\n"
		"1. Integrated Management Log (IML)\n"
		"2. OA Syslog\n"
		"3. OA Forward Progress Log\n"
		"4. iLO Event Log");
520

521
	return NMI_HANDLED;
522
}
523
#endif /* CONFIG_HPWDT_NMI_DECODING */
524

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
/*
 *	/dev/watchdog handling
 */
static int hpwdt_open(struct inode *inode, struct file *file)
{
	/* /dev/watchdog can only be opened once */
	if (test_and_set_bit(0, &hpwdt_is_open))
		return -EBUSY;

	/* Start the watchdog */
	hpwdt_start();
	hpwdt_ping();

	return nonseekable_open(inode, file);
}

static int hpwdt_release(struct inode *inode, struct file *file)
{
	/* Stop the watchdog */
	if (expect_release == 42) {
		hpwdt_stop();
	} else {
547
		pr_crit("Unexpected close, not stopping watchdog!\n");
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
		hpwdt_ping();
	}

	expect_release = 0;

	/* /dev/watchdog is being closed, make sure it can be re-opened */
	clear_bit(0, &hpwdt_is_open);

	return 0;
}

static ssize_t hpwdt_write(struct file *file, const char __user *data,
	size_t len, loff_t *ppos)
{
	/* See if we got the magic character 'V' and reload the timer */
	if (len) {
		if (!nowayout) {
			size_t i;

			/* note: just in case someone wrote the magic character
			 * five months ago... */
			expect_release = 0;

			/* scan to see whether or not we got the magic char. */
			for (i = 0; i != len; i++) {
				char c;
574
				if (get_user(c, data + i))
575 576 577 578 579 580 581 582 583 584 585 586 587
					return -EFAULT;
				if (c == 'V')
					expect_release = 42;
			}
		}

		/* someone wrote to us, we should reload the timer */
		hpwdt_ping();
	}

	return len;
}

588
static const struct watchdog_info ident = {
589 590 591
	.options = WDIOF_SETTIMEOUT |
		   WDIOF_KEEPALIVEPING |
		   WDIOF_MAGICCLOSE,
592
	.identity = "HPE iLO2+ HW Watchdog Timer",
593 594 595 596 597 598 599
};

static long hpwdt_ioctl(struct file *file, unsigned int cmd,
	unsigned long arg)
{
	void __user *argp = (void __user *)arg;
	int __user *p = argp;
600
	int new_margin, options;
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	int ret = -ENOTTY;

	switch (cmd) {
	case WDIOC_GETSUPPORT:
		ret = 0;
		if (copy_to_user(argp, &ident, sizeof(ident)))
			ret = -EFAULT;
		break;

	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
		ret = put_user(0, p);
		break;

	case WDIOC_KEEPALIVE:
		hpwdt_ping();
		ret = 0;
		break;

620 621 622 623 624 625 626 627 628 629 630 631 632 633
	case WDIOC_SETOPTIONS:
		ret = get_user(options, p);
		if (ret)
			break;

		if (options & WDIOS_DISABLECARD)
			hpwdt_stop();

		if (options & WDIOS_ENABLECARD) {
			hpwdt_start();
			hpwdt_ping();
		}
		break;

634 635 636 637 638 639 640 641 642 643 644 645 646 647
	case WDIOC_SETTIMEOUT:
		ret = get_user(new_margin, p);
		if (ret)
			break;

		ret = hpwdt_change_timer(new_margin);
		if (ret)
			break;

		hpwdt_ping();
		/* Fall */
	case WDIOC_GETTIMEOUT:
		ret = put_user(soft_margin, p);
		break;
648 649 650 651

	case WDIOC_GETTIMELEFT:
		ret = put_user(hpwdt_time_left(), p);
		break;
652 653 654 655 656 657 658
	}
	return ret;
}

/*
 *	Kernel interfaces
 */
659
static const struct file_operations hpwdt_fops = {
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	.owner = THIS_MODULE,
	.llseek = no_llseek,
	.write = hpwdt_write,
	.unlocked_ioctl = hpwdt_ioctl,
	.open = hpwdt_open,
	.release = hpwdt_release,
};

static struct miscdevice hpwdt_miscdev = {
	.minor = WATCHDOG_MINOR,
	.name = "watchdog",
	.fops = &hpwdt_fops,
};

/*
 *	Init & Exit
 */

678
#ifdef CONFIG_HPWDT_NMI_DECODING
679
#ifdef CONFIG_X86_LOCAL_APIC
B
Bill Pemberton 已提交
680
static void hpwdt_check_nmi_decoding(struct pci_dev *dev)
681 682 683
{
	/*
	 * If nmi_watchdog is turned off then we can turn on
684
	 * our nmi decoding capability.
685
	 */
686
	hpwdt_nmi_decoding = 1;
687 688
}
#else
B
Bill Pemberton 已提交
689
static void hpwdt_check_nmi_decoding(struct pci_dev *dev)
690
{
691
	dev_warn(&dev->dev, "NMI decoding is disabled. "
692 693
		"Your kernel does not support a NMI Watchdog.\n");
}
694
#endif /* CONFIG_X86_LOCAL_APIC */
695

696 697 698 699 700 701 702 703
/*
 *	dmi_find_icru
 *
 *	Routine Description:
 *	This function checks whether or not we are on an iCRU-based server.
 *	This check is independent of architecture and needs to be made for
 *	any ProLiant system.
 */
B
Bill Pemberton 已提交
704
static void dmi_find_icru(const struct dmi_header *dm, void *dummy)
705 706 707 708 709 710 711
{
	struct smbios_proliant_info *smbios_proliant_ptr;

	if (dm->type == SMBIOS_ICRU_INFORMATION) {
		smbios_proliant_ptr = (struct smbios_proliant_info *) dm;
		if (smbios_proliant_ptr->misc_features & 0x01)
			is_icru = 1;
J
Jerry Hoemann 已提交
712
		if (smbios_proliant_ptr->misc_features & 0x1400)
713
			is_uefi = 1;
714 715 716
	}
}

B
Bill Pemberton 已提交
717
static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
718 719 720 721
{
	int retval;

	/*
722 723 724 725 726 727 728 729
	 * On typical CRU-based systems we need to map that service in
	 * the BIOS. For 32 bit Operating Systems we need to go through
	 * the 32 Bit BIOS Service Directory. For 64 bit Operating
	 * Systems we get that service through SMBIOS.
	 *
	 * On systems that support the new iCRU service all we need to
	 * do is call dmi_walk to get the supported flag value and skip
	 * the old cru detect code.
730
	 */
731
	dmi_walk(dmi_find_icru, NULL);
732
	if (!is_icru && !is_uefi) {
733 734 735 736 737 738 739 740 741 742 743 744 745 746

		/*
		* We need to map the ROM to get the CRU service.
		* For 32 bit Operating Systems we need to go through the 32 Bit
		* BIOS Service Directory
		* For 64 bit Operating Systems we get that service through SMBIOS.
		*/
		retval = detect_cru_service();
		if (retval < 0) {
			dev_warn(&dev->dev,
				"Unable to detect the %d Bit CRU Service.\n",
				HPWDT_ARCH);
			return retval;
		}
747

748 749 750 751 752 753 754
		/*
		* We know this is the only CRU call we need to make so lets keep as
		* few instructions as possible once the NMI comes in.
		*/
		cmn_regs.u1.rah = 0x0D;
		cmn_regs.u1.ral = 0x02;
	}
755 756

	/*
757
	 * Only one function can register for NMI_UNKNOWN
758
	 */
759
	retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout, 0, "hpwdt");
760 761 762 763 764 765 766 767
	if (retval)
		goto error;
	retval = register_nmi_handler(NMI_SERR, hpwdt_pretimeout, 0, "hpwdt");
	if (retval)
		goto error1;
	retval = register_nmi_handler(NMI_IO_CHECK, hpwdt_pretimeout, 0, "hpwdt");
	if (retval)
		goto error2;
768 769

	dev_info(&dev->dev,
770
			"HPE Watchdog Timer Driver: NMI decoding initialized"
771
			", allow kernel dump: %s (default = 1/ON)\n",
772
			(allow_kdump == 0) ? "OFF" : "ON");
773
	return 0;
774 775 776 777 778 779 780 781 782 783 784 785

error2:
	unregister_nmi_handler(NMI_SERR, "hpwdt");
error1:
	unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
error:
	dev_warn(&dev->dev,
		"Unable to register a die notifier (err=%d).\n",
		retval);
	if (cru_rom_addr)
		iounmap(cru_rom_addr);
	return retval;
786 787
}

788
static void hpwdt_exit_nmi_decoding(void)
789
{
790
	unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
791 792
	unregister_nmi_handler(NMI_SERR, "hpwdt");
	unregister_nmi_handler(NMI_IO_CHECK, "hpwdt");
793 794 795
	if (cru_rom_addr)
		iounmap(cru_rom_addr);
}
796
#else /* !CONFIG_HPWDT_NMI_DECODING */
B
Bill Pemberton 已提交
797
static void hpwdt_check_nmi_decoding(struct pci_dev *dev)
798 799 800
{
}

B
Bill Pemberton 已提交
801
static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
802 803 804 805
{
	return 0;
}

806
static void hpwdt_exit_nmi_decoding(void)
807 808 809
{
}
#endif /* CONFIG_HPWDT_NMI_DECODING */
810

B
Bill Pemberton 已提交
811
static int hpwdt_init_one(struct pci_dev *dev,
812
					const struct pci_device_id *ent)
813 814 815
{
	int retval;

816
	/*
817
	 * Check if we can do NMI decoding or not
818
	 */
819
	hpwdt_check_nmi_decoding(dev);
820

821
	/*
822
	 * First let's find out if we are on an iLO2+ server. We will
823
	 * not run on a legacy ASM box.
824
	 * So we only support the G5 ProLiant servers and higher.
825
	 */
826 827
	if (dev->subsystem_vendor != PCI_VENDOR_ID_HP &&
	    dev->subsystem_vendor != PCI_VENDOR_ID_HP_3PAR) {
828
		dev_warn(&dev->dev,
829
			"This server does not have an iLO2+ ASIC.\n");
830 831 832
		return -ENODEV;
	}

833 834 835
	/*
	 * Ignore all auxilary iLO devices with the following PCI ID
	 */
836 837
	if (dev->subsystem_vendor == PCI_VENDOR_ID_HP &&
	    dev->subsystem_device == 0x1979)
838 839
		return -ENODEV;

840 841 842 843 844 845 846 847 848 849
	if (pci_enable_device(dev)) {
		dev_warn(&dev->dev,
			"Not possible to enable PCI Device: 0x%x:0x%x.\n",
			ent->vendor, ent->device);
		return -ENODEV;
	}

	pci_mem_addr = pci_iomap(dev, 1, 0x80);
	if (!pci_mem_addr) {
		dev_warn(&dev->dev,
850
			"Unable to detect the iLO2+ server memory.\n");
851 852 853
		retval = -ENOMEM;
		goto error_pci_iomap;
	}
854
	hpwdt_nmistat	= pci_mem_addr + 0x6e;
855 856 857
	hpwdt_timer_reg = pci_mem_addr + 0x70;
	hpwdt_timer_con = pci_mem_addr + 0x72;

T
Toshi Kani 已提交
858 859 860
	/* Make sure that timer is disabled until /dev/watchdog is opened */
	hpwdt_stop();

861 862 863 864
	/* Make sure that we have a valid soft_margin */
	if (hpwdt_change_timer(soft_margin))
		hpwdt_change_timer(DEFAULT_MARGIN);

865 866 867 868
	/* Initialize NMI Decoding functionality */
	retval = hpwdt_init_nmi_decoding(dev);
	if (retval != 0)
		goto error_init_nmi_decoding;
869 870 871 872 873 874 875 876 877

	retval = misc_register(&hpwdt_miscdev);
	if (retval < 0) {
		dev_warn(&dev->dev,
			"Unable to register miscdev on minor=%d (err=%d).\n",
			WATCHDOG_MINOR, retval);
		goto error_misc_register;
	}

878
	dev_info(&dev->dev, "HPE Watchdog Timer Driver: %s"
879 880
			", timer margin: %d seconds (nowayout=%d).\n",
			HPWDT_VERSION, soft_margin, nowayout);
881 882 883
	return 0;

error_misc_register:
884 885
	hpwdt_exit_nmi_decoding();
error_init_nmi_decoding:
886 887 888 889 890 891
	pci_iounmap(dev, pci_mem_addr);
error_pci_iomap:
	pci_disable_device(dev);
	return retval;
}

B
Bill Pemberton 已提交
892
static void hpwdt_exit(struct pci_dev *dev)
893 894 895 896 897
{
	if (!nowayout)
		hpwdt_stop();

	misc_deregister(&hpwdt_miscdev);
898
	hpwdt_exit_nmi_decoding();
899 900 901 902 903 904 905 906
	pci_iounmap(dev, pci_mem_addr);
	pci_disable_device(dev);
}

static struct pci_driver hpwdt_driver = {
	.name = "hpwdt",
	.id_table = hpwdt_devices,
	.probe = hpwdt_init_one,
907
	.remove = hpwdt_exit,
908 909 910 911 912
};

MODULE_AUTHOR("Tom Mingarelli");
MODULE_DESCRIPTION("hp watchdog driver");
MODULE_LICENSE("GPL");
913
MODULE_VERSION(HPWDT_VERSION);
914 915 916 917

module_param(soft_margin, int, 0);
MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");

W
Wim Van Sebroeck 已提交
918
module_param(nowayout, bool, 0);
919 920 921
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
		__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");

922
#ifdef CONFIG_HPWDT_NMI_DECODING
923 924
module_param(allow_kdump, int, 0);
MODULE_PARM_DESC(allow_kdump, "Start a kernel dump after NMI occurs");
925
#endif /* !CONFIG_HPWDT_NMI_DECODING */
926

927
module_pci_driver(hpwdt_driver);