hpwdt.c 20 KB

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