hpwdt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * HPE WatchDog Driver
  3. * based on
  4. *
  5. * SoftDog 0.05: A Software Watchdog Device
  6. *
  7. * (c) Copyright 2015 Hewlett Packard Enterprise Development LP
  8. * Thomas Mingarelli <[email protected]>
  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. #include <asm/nmi.h>
  30. #define HPWDT_VERSION "1.4.0"
  31. #define SECS_TO_TICKS(secs) ((secs) * 1000 / 128)
  32. #define TICKS_TO_SECS(ticks) ((ticks) * 128 / 1000)
  33. #define HPWDT_MAX_TIMER TICKS_TO_SECS(65535)
  34. #define DEFAULT_MARGIN 30
  35. static unsigned int soft_margin = DEFAULT_MARGIN; /* in seconds */
  36. static unsigned int reload; /* the computed soft_margin */
  37. static bool nowayout = WATCHDOG_NOWAYOUT;
  38. #ifdef CONFIG_HPWDT_NMI_DECODING
  39. static unsigned int allow_kdump = 1;
  40. #endif
  41. static char expect_release;
  42. static unsigned long hpwdt_is_open;
  43. static void __iomem *pci_mem_addr; /* the PCI-memory address */
  44. static unsigned long __iomem *hpwdt_nmistat;
  45. static unsigned long __iomem *hpwdt_timer_reg;
  46. static unsigned long __iomem *hpwdt_timer_con;
  47. static const struct pci_device_id hpwdt_devices[] = {
  48. { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) }, /* iLO2 */
  49. { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) }, /* iLO3 */
  50. {0}, /* terminate list */
  51. };
  52. MODULE_DEVICE_TABLE(pci, hpwdt_devices);
  53. /*
  54. * Watchdog operations
  55. */
  56. static void hpwdt_start(void)
  57. {
  58. reload = SECS_TO_TICKS(soft_margin);
  59. iowrite16(reload, hpwdt_timer_reg);
  60. iowrite8(0x85, hpwdt_timer_con);
  61. }
  62. static void hpwdt_stop(void)
  63. {
  64. unsigned long data;
  65. data = ioread8(hpwdt_timer_con);
  66. data &= 0xFE;
  67. iowrite8(data, hpwdt_timer_con);
  68. }
  69. static void hpwdt_ping(void)
  70. {
  71. iowrite16(reload, hpwdt_timer_reg);
  72. }
  73. static int hpwdt_change_timer(int new_margin)
  74. {
  75. if (new_margin < 1 || new_margin > HPWDT_MAX_TIMER) {
  76. pr_warn("New value passed in is invalid: %d seconds\n",
  77. new_margin);
  78. return -EINVAL;
  79. }
  80. soft_margin = new_margin;
  81. pr_debug("New timer passed in is %d seconds\n", new_margin);
  82. reload = SECS_TO_TICKS(soft_margin);
  83. return 0;
  84. }
  85. static int hpwdt_time_left(void)
  86. {
  87. return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
  88. }
  89. #ifdef CONFIG_HPWDT_NMI_DECODING
  90. static int hpwdt_my_nmi(void)
  91. {
  92. return ioread8(hpwdt_nmistat) & 0x6;
  93. }
  94. /*
  95. * NMI Handler
  96. */
  97. static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
  98. {
  99. if ((ulReason == NMI_UNKNOWN) && !hpwdt_my_nmi())
  100. return NMI_DONE;
  101. if (allow_kdump)
  102. hpwdt_stop();
  103. nmi_panic(regs, "An NMI occurred. Depending on your system the reason "
  104. "for the NMI is logged in any one of the following "
  105. "resources:\n"
  106. "1. Integrated Management Log (IML)\n"
  107. "2. OA Syslog\n"
  108. "3. OA Forward Progress Log\n"
  109. "4. iLO Event Log");
  110. return NMI_HANDLED;
  111. }
  112. #endif /* CONFIG_HPWDT_NMI_DECODING */
  113. /*
  114. * /dev/watchdog handling
  115. */
  116. static int hpwdt_open(struct inode *inode, struct file *file)
  117. {
  118. /* /dev/watchdog can only be opened once */
  119. if (test_and_set_bit(0, &hpwdt_is_open))
  120. return -EBUSY;
  121. /* Start the watchdog */
  122. hpwdt_start();
  123. hpwdt_ping();
  124. return nonseekable_open(inode, file);
  125. }
  126. static int hpwdt_release(struct inode *inode, struct file *file)
  127. {
  128. /* Stop the watchdog */
  129. if (expect_release == 42) {
  130. hpwdt_stop();
  131. } else {
  132. pr_crit("Unexpected close, not stopping watchdog!\n");
  133. hpwdt_ping();
  134. }
  135. expect_release = 0;
  136. /* /dev/watchdog is being closed, make sure it can be re-opened */
  137. clear_bit(0, &hpwdt_is_open);
  138. return 0;
  139. }
  140. static ssize_t hpwdt_write(struct file *file, const char __user *data,
  141. size_t len, loff_t *ppos)
  142. {
  143. /* See if we got the magic character 'V' and reload the timer */
  144. if (len) {
  145. if (!nowayout) {
  146. size_t i;
  147. /* note: just in case someone wrote the magic character
  148. * five months ago... */
  149. expect_release = 0;
  150. /* scan to see whether or not we got the magic char. */
  151. for (i = 0; i != len; i++) {
  152. char c;
  153. if (get_user(c, data + i))
  154. return -EFAULT;
  155. if (c == 'V')
  156. expect_release = 42;
  157. }
  158. }
  159. /* someone wrote to us, we should reload the timer */
  160. hpwdt_ping();
  161. }
  162. return len;
  163. }
  164. static const struct watchdog_info ident = {
  165. .options = WDIOF_SETTIMEOUT |
  166. WDIOF_KEEPALIVEPING |
  167. WDIOF_MAGICCLOSE,
  168. .identity = "HPE iLO2+ HW Watchdog Timer",
  169. };
  170. static long hpwdt_ioctl(struct file *file, unsigned int cmd,
  171. unsigned long arg)
  172. {
  173. void __user *argp = (void __user *)arg;
  174. int __user *p = argp;
  175. int new_margin, options;
  176. int ret = -ENOTTY;
  177. switch (cmd) {
  178. case WDIOC_GETSUPPORT:
  179. ret = 0;
  180. if (copy_to_user(argp, &ident, sizeof(ident)))
  181. ret = -EFAULT;
  182. break;
  183. case WDIOC_GETSTATUS:
  184. case WDIOC_GETBOOTSTATUS:
  185. ret = put_user(0, p);
  186. break;
  187. case WDIOC_KEEPALIVE:
  188. hpwdt_ping();
  189. ret = 0;
  190. break;
  191. case WDIOC_SETOPTIONS:
  192. ret = get_user(options, p);
  193. if (ret)
  194. break;
  195. if (options & WDIOS_DISABLECARD)
  196. hpwdt_stop();
  197. if (options & WDIOS_ENABLECARD) {
  198. hpwdt_start();
  199. hpwdt_ping();
  200. }
  201. break;
  202. case WDIOC_SETTIMEOUT:
  203. ret = get_user(new_margin, p);
  204. if (ret)
  205. break;
  206. ret = hpwdt_change_timer(new_margin);
  207. if (ret)
  208. break;
  209. hpwdt_ping();
  210. /* Fall */
  211. case WDIOC_GETTIMEOUT:
  212. ret = put_user(soft_margin, p);
  213. break;
  214. case WDIOC_GETTIMELEFT:
  215. ret = put_user(hpwdt_time_left(), p);
  216. break;
  217. }
  218. return ret;
  219. }
  220. /*
  221. * Kernel interfaces
  222. */
  223. static const struct file_operations hpwdt_fops = {
  224. .owner = THIS_MODULE,
  225. .llseek = no_llseek,
  226. .write = hpwdt_write,
  227. .unlocked_ioctl = hpwdt_ioctl,
  228. .open = hpwdt_open,
  229. .release = hpwdt_release,
  230. };
  231. static struct miscdevice hpwdt_miscdev = {
  232. .minor = WATCHDOG_MINOR,
  233. .name = "watchdog",
  234. .fops = &hpwdt_fops,
  235. };
  236. /*
  237. * Init & Exit
  238. */
  239. static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
  240. {
  241. #ifdef CONFIG_HPWDT_NMI_DECODING
  242. int retval;
  243. /*
  244. * Only one function can register for NMI_UNKNOWN
  245. */
  246. retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout, 0, "hpwdt");
  247. if (retval)
  248. goto error;
  249. retval = register_nmi_handler(NMI_SERR, hpwdt_pretimeout, 0, "hpwdt");
  250. if (retval)
  251. goto error1;
  252. retval = register_nmi_handler(NMI_IO_CHECK, hpwdt_pretimeout, 0, "hpwdt");
  253. if (retval)
  254. goto error2;
  255. dev_info(&dev->dev,
  256. "HPE Watchdog Timer Driver: NMI decoding initialized"
  257. ", allow kernel dump: %s (default = 1/ON)\n",
  258. (allow_kdump == 0) ? "OFF" : "ON");
  259. return 0;
  260. error2:
  261. unregister_nmi_handler(NMI_SERR, "hpwdt");
  262. error1:
  263. unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
  264. error:
  265. dev_warn(&dev->dev,
  266. "Unable to register a die notifier (err=%d).\n",
  267. retval);
  268. return retval;
  269. #endif /* CONFIG_HPWDT_NMI_DECODING */
  270. return 0;
  271. }
  272. static void hpwdt_exit_nmi_decoding(void)
  273. {
  274. #ifdef CONFIG_HPWDT_NMI_DECODING
  275. unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
  276. unregister_nmi_handler(NMI_SERR, "hpwdt");
  277. unregister_nmi_handler(NMI_IO_CHECK, "hpwdt");
  278. #endif
  279. }
  280. static int hpwdt_init_one(struct pci_dev *dev,
  281. const struct pci_device_id *ent)
  282. {
  283. int retval;
  284. /*
  285. * First let's find out if we are on an iLO2+ server. We will
  286. * not run on a legacy ASM box.
  287. * So we only support the G5 ProLiant servers and higher.
  288. */
  289. if (dev->subsystem_vendor != PCI_VENDOR_ID_HP &&
  290. dev->subsystem_vendor != PCI_VENDOR_ID_HP_3PAR) {
  291. dev_warn(&dev->dev,
  292. "This server does not have an iLO2+ ASIC.\n");
  293. return -ENODEV;
  294. }
  295. /*
  296. * Ignore all auxilary iLO devices with the following PCI ID
  297. */
  298. if (dev->subsystem_vendor == PCI_VENDOR_ID_HP &&
  299. dev->subsystem_device == 0x1979)
  300. return -ENODEV;
  301. if (pci_enable_device(dev)) {
  302. dev_warn(&dev->dev,
  303. "Not possible to enable PCI Device: 0x%x:0x%x.\n",
  304. ent->vendor, ent->device);
  305. return -ENODEV;
  306. }
  307. pci_mem_addr = pci_iomap(dev, 1, 0x80);
  308. if (!pci_mem_addr) {
  309. dev_warn(&dev->dev,
  310. "Unable to detect the iLO2+ server memory.\n");
  311. retval = -ENOMEM;
  312. goto error_pci_iomap;
  313. }
  314. hpwdt_nmistat = pci_mem_addr + 0x6e;
  315. hpwdt_timer_reg = pci_mem_addr + 0x70;
  316. hpwdt_timer_con = pci_mem_addr + 0x72;
  317. /* Make sure that timer is disabled until /dev/watchdog is opened */
  318. hpwdt_stop();
  319. /* Make sure that we have a valid soft_margin */
  320. if (hpwdt_change_timer(soft_margin))
  321. hpwdt_change_timer(DEFAULT_MARGIN);
  322. /* Initialize NMI Decoding functionality */
  323. retval = hpwdt_init_nmi_decoding(dev);
  324. if (retval != 0)
  325. goto error_init_nmi_decoding;
  326. retval = misc_register(&hpwdt_miscdev);
  327. if (retval < 0) {
  328. dev_warn(&dev->dev,
  329. "Unable to register miscdev on minor=%d (err=%d).\n",
  330. WATCHDOG_MINOR, retval);
  331. goto error_misc_register;
  332. }
  333. dev_info(&dev->dev, "HPE Watchdog Timer Driver: %s"
  334. ", timer margin: %d seconds (nowayout=%d).\n",
  335. HPWDT_VERSION, soft_margin, nowayout);
  336. return 0;
  337. error_misc_register:
  338. hpwdt_exit_nmi_decoding();
  339. error_init_nmi_decoding:
  340. pci_iounmap(dev, pci_mem_addr);
  341. error_pci_iomap:
  342. pci_disable_device(dev);
  343. return retval;
  344. }
  345. static void hpwdt_exit(struct pci_dev *dev)
  346. {
  347. if (!nowayout)
  348. hpwdt_stop();
  349. misc_deregister(&hpwdt_miscdev);
  350. hpwdt_exit_nmi_decoding();
  351. pci_iounmap(dev, pci_mem_addr);
  352. pci_disable_device(dev);
  353. }
  354. static struct pci_driver hpwdt_driver = {
  355. .name = "hpwdt",
  356. .id_table = hpwdt_devices,
  357. .probe = hpwdt_init_one,
  358. .remove = hpwdt_exit,
  359. };
  360. MODULE_AUTHOR("Tom Mingarelli");
  361. MODULE_DESCRIPTION("hp watchdog driver");
  362. MODULE_LICENSE("GPL");
  363. MODULE_VERSION(HPWDT_VERSION);
  364. module_param(soft_margin, int, 0);
  365. MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
  366. module_param(nowayout, bool, 0);
  367. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  368. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  369. #ifdef CONFIG_HPWDT_NMI_DECODING
  370. module_param(allow_kdump, int, 0);
  371. MODULE_PARM_DESC(allow_kdump, "Start a kernel dump after NMI occurs");
  372. #endif /* CONFIG_HPWDT_NMI_DECODING */
  373. module_pci_driver(hpwdt_driver);