5 * SoftDog 0.05: A Software Watchdog Device
7 * (c) Copyright 2018 Hewlett Packard Enterprise Development LP
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
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/device.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/pci.h>
24 #include <linux/pci_ids.h>
25 #include <linux/types.h>
26 #include <linux/watchdog.h>
29 #define HPWDT_VERSION "2.0.2"
30 #define SECS_TO_TICKS(secs) ((secs) * 1000 / 128)
31 #define TICKS_TO_SECS(ticks) ((ticks) * 128 / 1000)
32 #define HPWDT_MAX_TIMER TICKS_TO_SECS(65535)
33 #define DEFAULT_MARGIN 30
34 #define PRETIMEOUT_SEC 9
37 static unsigned int soft_margin = DEFAULT_MARGIN; /* in seconds */
38 static bool nowayout = WATCHDOG_NOWAYOUT;
39 static bool pretimeout = IS_ENABLED(CONFIG_HPWDT_NMI_DECODING);
41 static void __iomem *pci_mem_addr; /* the PCI-memory address */
42 static unsigned long __iomem *hpwdt_nmistat;
43 static unsigned long __iomem *hpwdt_timer_reg;
44 static unsigned long __iomem *hpwdt_timer_con;
46 static const struct pci_device_id hpwdt_devices[] = {
47 { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) }, /* iLO2 */
48 { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) }, /* iLO3 */
49 {0}, /* terminate list */
51 MODULE_DEVICE_TABLE(pci, hpwdt_devices);
53 static const struct pci_device_id hpwdt_blacklist[] = {
54 { PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3306, PCI_VENDOR_ID_HP, 0x1979) }, /* auxilary iLO */
55 { PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3306, PCI_VENDOR_ID_HP_3PAR, 0x0289) }, /* CL */
56 {0}, /* terminate list */
62 static int hpwdt_start(struct watchdog_device *wdd)
64 int control = 0x81 | (pretimeout ? 0x4 : 0);
65 int reload = SECS_TO_TICKS(wdd->timeout);
67 dev_dbg(wdd->parent, "start watchdog 0x%08x:0x%02x\n", reload, control);
68 iowrite16(reload, hpwdt_timer_reg);
69 iowrite8(control, hpwdt_timer_con);
74 static void hpwdt_stop(void)
78 pr_debug("stop watchdog\n");
80 data = ioread8(hpwdt_timer_con);
82 iowrite8(data, hpwdt_timer_con);
85 static int hpwdt_stop_core(struct watchdog_device *wdd)
92 static int hpwdt_ping(struct watchdog_device *wdd)
94 int reload = SECS_TO_TICKS(wdd->timeout);
96 dev_dbg(wdd->parent, "ping watchdog 0x%08x\n", reload);
97 iowrite16(reload, hpwdt_timer_reg);
102 static unsigned int hpwdt_gettimeleft(struct watchdog_device *wdd)
104 return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
107 static int hpwdt_settimeout(struct watchdog_device *wdd, unsigned int val)
109 dev_dbg(wdd->parent, "set_timeout = %d\n", val);
112 if (val <= wdd->pretimeout) {
113 dev_dbg(wdd->parent, "pretimeout < timeout. Setting to zero\n");
116 if (watchdog_active(wdd))
124 #ifdef CONFIG_HPWDT_NMI_DECODING
125 static int hpwdt_set_pretimeout(struct watchdog_device *wdd, unsigned int req)
127 unsigned int val = 0;
129 dev_dbg(wdd->parent, "set_pretimeout = %d\n", req);
131 val = PRETIMEOUT_SEC;
132 if (val >= wdd->timeout)
137 dev_dbg(wdd->parent, "Rounding pretimeout to: %d\n", val);
139 wdd->pretimeout = val;
142 if (watchdog_active(wdd))
148 static int hpwdt_my_nmi(void)
150 return ioread8(hpwdt_nmistat) & 0x6;
156 static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
158 unsigned int mynmi = hpwdt_my_nmi();
159 static char panic_msg[] =
160 "00: An NMI occurred. Depending on your system the reason "
161 "for the NMI is logged in any one of the following resources:\n"
162 "1. Integrated Management Log (IML)\n"
164 "3. OA Forward Progress Log\n"
167 if (ilo5 && ulReason == NMI_UNKNOWN && !mynmi)
170 if (ilo5 && !pretimeout && !mynmi)
175 hex_byte_pack(panic_msg, mynmi);
176 nmi_panic(regs, panic_msg);
180 #endif /* CONFIG_HPWDT_NMI_DECODING */
183 static const struct watchdog_info ident = {
184 .options = WDIOF_PRETIMEOUT |
186 WDIOF_KEEPALIVEPING |
188 .identity = "HPE iLO2+ HW Watchdog Timer",
195 static const struct watchdog_ops hpwdt_ops = {
196 .owner = THIS_MODULE,
197 .start = hpwdt_start,
198 .stop = hpwdt_stop_core,
200 .set_timeout = hpwdt_settimeout,
201 .get_timeleft = hpwdt_gettimeleft,
202 #ifdef CONFIG_HPWDT_NMI_DECODING
203 .set_pretimeout = hpwdt_set_pretimeout,
207 static struct watchdog_device hpwdt_dev = {
211 .max_timeout = HPWDT_MAX_TIMER,
212 .timeout = DEFAULT_MARGIN,
213 .pretimeout = PRETIMEOUT_SEC,
221 static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
223 #ifdef CONFIG_HPWDT_NMI_DECODING
226 * Only one function can register for NMI_UNKNOWN
228 retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout, 0, "hpwdt");
231 retval = register_nmi_handler(NMI_SERR, hpwdt_pretimeout, 0, "hpwdt");
234 retval = register_nmi_handler(NMI_IO_CHECK, hpwdt_pretimeout, 0, "hpwdt");
239 "HPE Watchdog Timer Driver: NMI decoding initialized\n");
244 unregister_nmi_handler(NMI_SERR, "hpwdt");
246 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
249 "Unable to register a die notifier (err=%d).\n",
252 #endif /* CONFIG_HPWDT_NMI_DECODING */
256 static void hpwdt_exit_nmi_decoding(void)
258 #ifdef CONFIG_HPWDT_NMI_DECODING
259 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
260 unregister_nmi_handler(NMI_SERR, "hpwdt");
261 unregister_nmi_handler(NMI_IO_CHECK, "hpwdt");
265 static int hpwdt_init_one(struct pci_dev *dev,
266 const struct pci_device_id *ent)
271 * First let's find out if we are on an iLO2+ server. We will
272 * not run on a legacy ASM box.
273 * So we only support the G5 ProLiant servers and higher.
275 if (dev->subsystem_vendor != PCI_VENDOR_ID_HP &&
276 dev->subsystem_vendor != PCI_VENDOR_ID_HP_3PAR) {
278 "This server does not have an iLO2+ ASIC.\n");
282 if (pci_match_id(hpwdt_blacklist, dev)) {
283 dev_dbg(&dev->dev, "Not supported on this device\n");
287 if (pci_enable_device(dev)) {
289 "Not possible to enable PCI Device: 0x%x:0x%x.\n",
290 ent->vendor, ent->device);
294 pci_mem_addr = pci_iomap(dev, 1, 0x80);
297 "Unable to detect the iLO2+ server memory.\n");
299 goto error_pci_iomap;
301 hpwdt_nmistat = pci_mem_addr + 0x6e;
302 hpwdt_timer_reg = pci_mem_addr + 0x70;
303 hpwdt_timer_con = pci_mem_addr + 0x72;
305 /* Make sure that timer is disabled until /dev/watchdog is opened */
308 /* Initialize NMI Decoding functionality */
309 retval = hpwdt_init_nmi_decoding(dev);
311 goto error_init_nmi_decoding;
313 watchdog_set_nowayout(&hpwdt_dev, nowayout);
314 watchdog_init_timeout(&hpwdt_dev, soft_margin, NULL);
316 if (pretimeout && hpwdt_dev.timeout <= PRETIMEOUT_SEC) {
317 dev_warn(&dev->dev, "timeout <= pretimeout. Setting pretimeout to zero\n");
320 hpwdt_dev.pretimeout = pretimeout ? PRETIMEOUT_SEC : 0;
322 hpwdt_dev.parent = &dev->dev;
323 retval = watchdog_register_device(&hpwdt_dev);
325 dev_err(&dev->dev, "watchdog register failed: %d.\n", retval);
326 goto error_wd_register;
329 dev_info(&dev->dev, "HPE Watchdog Timer Driver: Version: %s\n",
331 dev_info(&dev->dev, "timeout: %d seconds (nowayout=%d)\n",
332 hpwdt_dev.timeout, nowayout);
333 dev_info(&dev->dev, "pretimeout: %s.\n",
334 pretimeout ? "on" : "off");
336 if (dev->subsystem_vendor == PCI_VENDOR_ID_HP_3PAR)
342 hpwdt_exit_nmi_decoding();
343 error_init_nmi_decoding:
344 pci_iounmap(dev, pci_mem_addr);
346 pci_disable_device(dev);
350 static void hpwdt_exit(struct pci_dev *dev)
355 watchdog_unregister_device(&hpwdt_dev);
356 hpwdt_exit_nmi_decoding();
357 pci_iounmap(dev, pci_mem_addr);
358 pci_disable_device(dev);
361 static struct pci_driver hpwdt_driver = {
363 .id_table = hpwdt_devices,
364 .probe = hpwdt_init_one,
365 .remove = hpwdt_exit,
368 MODULE_AUTHOR("Tom Mingarelli");
369 MODULE_DESCRIPTION("hpe watchdog driver");
370 MODULE_LICENSE("GPL");
371 MODULE_VERSION(HPWDT_VERSION);
373 module_param(soft_margin, int, 0);
374 MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
376 module_param_named(timeout, soft_margin, int, 0);
377 MODULE_PARM_DESC(timeout, "Alias of soft_margin");
379 module_param(nowayout, bool, 0);
380 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
381 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
383 #ifdef CONFIG_HPWDT_NMI_DECODING
384 module_param(pretimeout, bool, 0);
385 MODULE_PARM_DESC(pretimeout, "Watchdog pretimeout enabled");
388 module_pci_driver(hpwdt_driver);