hpwdt.c 21 KB

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