1 // SPDX-License-Identifier: GPL-2.0-only
6 * SoftDog 0.05: A Software Watchdog Device
8 * (c) Copyright 2018 Hewlett Packard Enterprise Development LP
9 * Thomas Mingarelli <thomas.mingarelli@hpe.com>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/device.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/pci.h>
20 #include <linux/pci_ids.h>
21 #include <linux/types.h>
22 #include <linux/watchdog.h>
25 #define HPWDT_VERSION "2.0.2"
26 #define SECS_TO_TICKS(secs) ((secs) * 1000 / 128)
27 #define TICKS_TO_SECS(ticks) ((ticks) * 128 / 1000)
28 #define HPWDT_MAX_TIMER TICKS_TO_SECS(65535)
29 #define DEFAULT_MARGIN 30
30 #define PRETIMEOUT_SEC 9
33 static unsigned int soft_margin = DEFAULT_MARGIN; /* in seconds */
34 static bool nowayout = WATCHDOG_NOWAYOUT;
35 static bool pretimeout = IS_ENABLED(CONFIG_HPWDT_NMI_DECODING);
37 static void __iomem *pci_mem_addr; /* the PCI-memory address */
38 static unsigned long __iomem *hpwdt_nmistat;
39 static unsigned long __iomem *hpwdt_timer_reg;
40 static unsigned long __iomem *hpwdt_timer_con;
42 static const struct pci_device_id hpwdt_devices[] = {
43 { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) }, /* iLO2 */
44 { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) }, /* iLO3 */
45 {0}, /* terminate list */
47 MODULE_DEVICE_TABLE(pci, hpwdt_devices);
49 static const struct pci_device_id hpwdt_blacklist[] = {
50 { PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3306, PCI_VENDOR_ID_HP, 0x1979) }, /* auxilary iLO */
51 { PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3306, PCI_VENDOR_ID_HP_3PAR, 0x0289) }, /* CL */
52 {0}, /* terminate list */
58 static int hpwdt_start(struct watchdog_device *wdd)
60 int control = 0x81 | (pretimeout ? 0x4 : 0);
61 int reload = SECS_TO_TICKS(wdd->timeout);
63 dev_dbg(wdd->parent, "start watchdog 0x%08x:0x%02x\n", reload, control);
64 iowrite16(reload, hpwdt_timer_reg);
65 iowrite8(control, hpwdt_timer_con);
70 static void hpwdt_stop(void)
74 pr_debug("stop watchdog\n");
76 data = ioread8(hpwdt_timer_con);
78 iowrite8(data, hpwdt_timer_con);
81 static int hpwdt_stop_core(struct watchdog_device *wdd)
88 static int hpwdt_ping(struct watchdog_device *wdd)
90 int reload = SECS_TO_TICKS(wdd->timeout);
92 dev_dbg(wdd->parent, "ping watchdog 0x%08x\n", reload);
93 iowrite16(reload, hpwdt_timer_reg);
98 static unsigned int hpwdt_gettimeleft(struct watchdog_device *wdd)
100 return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
103 static int hpwdt_settimeout(struct watchdog_device *wdd, unsigned int val)
105 dev_dbg(wdd->parent, "set_timeout = %d\n", val);
108 if (val <= wdd->pretimeout) {
109 dev_dbg(wdd->parent, "pretimeout < timeout. Setting to zero\n");
112 if (watchdog_active(wdd))
120 #ifdef CONFIG_HPWDT_NMI_DECODING
121 static int hpwdt_set_pretimeout(struct watchdog_device *wdd, unsigned int req)
123 unsigned int val = 0;
125 dev_dbg(wdd->parent, "set_pretimeout = %d\n", req);
127 val = PRETIMEOUT_SEC;
128 if (val >= wdd->timeout)
133 dev_dbg(wdd->parent, "Rounding pretimeout to: %d\n", val);
135 wdd->pretimeout = val;
138 if (watchdog_active(wdd))
144 static int hpwdt_my_nmi(void)
146 return ioread8(hpwdt_nmistat) & 0x6;
152 static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
154 unsigned int mynmi = hpwdt_my_nmi();
155 static char panic_msg[] =
156 "00: An NMI occurred. Depending on your system the reason "
157 "for the NMI is logged in any one of the following resources:\n"
158 "1. Integrated Management Log (IML)\n"
160 "3. OA Forward Progress Log\n"
163 if (ilo5 && ulReason == NMI_UNKNOWN && !mynmi)
166 if (ilo5 && !pretimeout && !mynmi)
171 hex_byte_pack(panic_msg, mynmi);
172 nmi_panic(regs, panic_msg);
176 #endif /* CONFIG_HPWDT_NMI_DECODING */
179 static const struct watchdog_info ident = {
180 .options = WDIOF_PRETIMEOUT |
182 WDIOF_KEEPALIVEPING |
184 .identity = "HPE iLO2+ HW Watchdog Timer",
191 static const struct watchdog_ops hpwdt_ops = {
192 .owner = THIS_MODULE,
193 .start = hpwdt_start,
194 .stop = hpwdt_stop_core,
196 .set_timeout = hpwdt_settimeout,
197 .get_timeleft = hpwdt_gettimeleft,
198 #ifdef CONFIG_HPWDT_NMI_DECODING
199 .set_pretimeout = hpwdt_set_pretimeout,
203 static struct watchdog_device hpwdt_dev = {
207 .max_timeout = HPWDT_MAX_TIMER,
208 .timeout = DEFAULT_MARGIN,
209 .pretimeout = PRETIMEOUT_SEC,
217 static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
219 #ifdef CONFIG_HPWDT_NMI_DECODING
222 * Only one function can register for NMI_UNKNOWN
224 retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout, 0, "hpwdt");
227 retval = register_nmi_handler(NMI_SERR, hpwdt_pretimeout, 0, "hpwdt");
230 retval = register_nmi_handler(NMI_IO_CHECK, hpwdt_pretimeout, 0, "hpwdt");
235 "HPE Watchdog Timer Driver: NMI decoding initialized\n");
240 unregister_nmi_handler(NMI_SERR, "hpwdt");
242 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
245 "Unable to register a die notifier (err=%d).\n",
248 #endif /* CONFIG_HPWDT_NMI_DECODING */
252 static void hpwdt_exit_nmi_decoding(void)
254 #ifdef CONFIG_HPWDT_NMI_DECODING
255 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
256 unregister_nmi_handler(NMI_SERR, "hpwdt");
257 unregister_nmi_handler(NMI_IO_CHECK, "hpwdt");
261 static int hpwdt_init_one(struct pci_dev *dev,
262 const struct pci_device_id *ent)
267 * First let's find out if we are on an iLO2+ server. We will
268 * not run on a legacy ASM box.
269 * So we only support the G5 ProLiant servers and higher.
271 if (dev->subsystem_vendor != PCI_VENDOR_ID_HP &&
272 dev->subsystem_vendor != PCI_VENDOR_ID_HP_3PAR) {
274 "This server does not have an iLO2+ ASIC.\n");
278 if (pci_match_id(hpwdt_blacklist, dev)) {
279 dev_dbg(&dev->dev, "Not supported on this device\n");
283 if (pci_enable_device(dev)) {
285 "Not possible to enable PCI Device: 0x%x:0x%x.\n",
286 ent->vendor, ent->device);
290 pci_mem_addr = pci_iomap(dev, 1, 0x80);
293 "Unable to detect the iLO2+ server memory.\n");
295 goto error_pci_iomap;
297 hpwdt_nmistat = pci_mem_addr + 0x6e;
298 hpwdt_timer_reg = pci_mem_addr + 0x70;
299 hpwdt_timer_con = pci_mem_addr + 0x72;
301 /* Make sure that timer is disabled until /dev/watchdog is opened */
304 /* Initialize NMI Decoding functionality */
305 retval = hpwdt_init_nmi_decoding(dev);
307 goto error_init_nmi_decoding;
309 watchdog_set_nowayout(&hpwdt_dev, nowayout);
310 watchdog_init_timeout(&hpwdt_dev, soft_margin, NULL);
312 if (pretimeout && hpwdt_dev.timeout <= PRETIMEOUT_SEC) {
313 dev_warn(&dev->dev, "timeout <= pretimeout. Setting pretimeout to zero\n");
316 hpwdt_dev.pretimeout = pretimeout ? PRETIMEOUT_SEC : 0;
318 hpwdt_dev.parent = &dev->dev;
319 retval = watchdog_register_device(&hpwdt_dev);
321 dev_err(&dev->dev, "watchdog register failed: %d.\n", retval);
322 goto error_wd_register;
325 dev_info(&dev->dev, "HPE Watchdog Timer Driver: Version: %s\n",
327 dev_info(&dev->dev, "timeout: %d seconds (nowayout=%d)\n",
328 hpwdt_dev.timeout, nowayout);
329 dev_info(&dev->dev, "pretimeout: %s.\n",
330 pretimeout ? "on" : "off");
332 if (dev->subsystem_vendor == PCI_VENDOR_ID_HP_3PAR)
338 hpwdt_exit_nmi_decoding();
339 error_init_nmi_decoding:
340 pci_iounmap(dev, pci_mem_addr);
342 pci_disable_device(dev);
346 static void hpwdt_exit(struct pci_dev *dev)
351 watchdog_unregister_device(&hpwdt_dev);
352 hpwdt_exit_nmi_decoding();
353 pci_iounmap(dev, pci_mem_addr);
354 pci_disable_device(dev);
357 static struct pci_driver hpwdt_driver = {
359 .id_table = hpwdt_devices,
360 .probe = hpwdt_init_one,
361 .remove = hpwdt_exit,
364 MODULE_AUTHOR("Tom Mingarelli");
365 MODULE_DESCRIPTION("hpe watchdog driver");
366 MODULE_LICENSE("GPL");
367 MODULE_VERSION(HPWDT_VERSION);
369 module_param(soft_margin, int, 0);
370 MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
372 module_param_named(timeout, soft_margin, int, 0);
373 MODULE_PARM_DESC(timeout, "Alias of soft_margin");
375 module_param(nowayout, bool, 0);
376 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
377 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
379 #ifdef CONFIG_HPWDT_NMI_DECODING
380 module_param(pretimeout, bool, 0);
381 MODULE_PARM_DESC(pretimeout, "Watchdog pretimeout enabled");
384 module_pci_driver(hpwdt_driver);