hpwdt.c 22 KB

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