5 * SoftDog 0.05: A Software Watchdog Device
7 * (c) Copyright 2018 Hewlett Packard Enterprise Development LP
8 * Thomas Mingarelli <thomas.mingarelli@hpe.com>
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.1"
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
);
57 static int hpwdt_start(struct watchdog_device
*wdd
)
59 int control
= 0x81 | (pretimeout
? 0x4 : 0);
60 int reload
= SECS_TO_TICKS(wdd
->timeout
);
62 dev_dbg(wdd
->parent
, "start watchdog 0x%08x:0x%02x\n", reload
, control
);
63 iowrite16(reload
, hpwdt_timer_reg
);
64 iowrite8(control
, hpwdt_timer_con
);
69 static void hpwdt_stop(void)
73 pr_debug("stop watchdog\n");
75 data
= ioread8(hpwdt_timer_con
);
77 iowrite8(data
, hpwdt_timer_con
);
80 static int hpwdt_stop_core(struct watchdog_device
*wdd
)
87 static int hpwdt_ping(struct watchdog_device
*wdd
)
89 int reload
= SECS_TO_TICKS(wdd
->timeout
);
91 dev_dbg(wdd
->parent
, "ping watchdog 0x%08x\n", reload
);
92 iowrite16(reload
, hpwdt_timer_reg
);
97 static unsigned int hpwdt_gettimeleft(struct watchdog_device
*wdd
)
99 return TICKS_TO_SECS(ioread16(hpwdt_timer_reg
));
102 static int hpwdt_settimeout(struct watchdog_device
*wdd
, unsigned int val
)
104 dev_dbg(wdd
->parent
, "set_timeout = %d\n", val
);
107 if (val
<= wdd
->pretimeout
) {
108 dev_dbg(wdd
->parent
, "pretimeout < timeout. Setting to zero\n");
111 if (watchdog_active(wdd
))
119 #ifdef CONFIG_HPWDT_NMI_DECODING
120 static int hpwdt_set_pretimeout(struct watchdog_device
*wdd
, unsigned int req
)
122 unsigned int val
= 0;
124 dev_dbg(wdd
->parent
, "set_pretimeout = %d\n", req
);
126 val
= PRETIMEOUT_SEC
;
127 if (val
>= wdd
->timeout
)
132 dev_dbg(wdd
->parent
, "Rounding pretimeout to: %d\n", val
);
134 wdd
->pretimeout
= val
;
137 if (watchdog_active(wdd
))
143 static int hpwdt_my_nmi(void)
145 return ioread8(hpwdt_nmistat
) & 0x6;
151 static int hpwdt_pretimeout(unsigned int ulReason
, struct pt_regs
*regs
)
153 unsigned int mynmi
= hpwdt_my_nmi();
154 static char panic_msg
[] =
155 "00: An NMI occurred. Depending on your system the reason "
156 "for the NMI is logged in any one of the following resources:\n"
157 "1. Integrated Management Log (IML)\n"
159 "3. OA Forward Progress Log\n"
162 if (ilo5
&& ulReason
== NMI_UNKNOWN
&& !mynmi
)
165 if (ilo5
&& !pretimeout
&& !mynmi
)
170 hex_byte_pack(panic_msg
, mynmi
);
171 nmi_panic(regs
, panic_msg
);
175 #endif /* CONFIG_HPWDT_NMI_DECODING */
178 static const struct watchdog_info ident
= {
179 .options
= WDIOF_PRETIMEOUT
|
181 WDIOF_KEEPALIVEPING
|
183 .identity
= "HPE iLO2+ HW Watchdog Timer",
190 static const struct watchdog_ops hpwdt_ops
= {
191 .owner
= THIS_MODULE
,
192 .start
= hpwdt_start
,
193 .stop
= hpwdt_stop_core
,
195 .set_timeout
= hpwdt_settimeout
,
196 .get_timeleft
= hpwdt_gettimeleft
,
197 #ifdef CONFIG_HPWDT_NMI_DECODING
198 .set_pretimeout
= hpwdt_set_pretimeout
,
202 static struct watchdog_device hpwdt_dev
= {
206 .max_timeout
= HPWDT_MAX_TIMER
,
207 .timeout
= DEFAULT_MARGIN
,
208 .pretimeout
= PRETIMEOUT_SEC
,
216 static int hpwdt_init_nmi_decoding(struct pci_dev
*dev
)
218 #ifdef CONFIG_HPWDT_NMI_DECODING
221 * Only one function can register for NMI_UNKNOWN
223 retval
= register_nmi_handler(NMI_UNKNOWN
, hpwdt_pretimeout
, 0, "hpwdt");
226 retval
= register_nmi_handler(NMI_SERR
, hpwdt_pretimeout
, 0, "hpwdt");
229 retval
= register_nmi_handler(NMI_IO_CHECK
, hpwdt_pretimeout
, 0, "hpwdt");
234 "HPE Watchdog Timer Driver: NMI decoding initialized\n");
239 unregister_nmi_handler(NMI_SERR
, "hpwdt");
241 unregister_nmi_handler(NMI_UNKNOWN
, "hpwdt");
244 "Unable to register a die notifier (err=%d).\n",
247 #endif /* CONFIG_HPWDT_NMI_DECODING */
251 static void hpwdt_exit_nmi_decoding(void)
253 #ifdef CONFIG_HPWDT_NMI_DECODING
254 unregister_nmi_handler(NMI_UNKNOWN
, "hpwdt");
255 unregister_nmi_handler(NMI_SERR
, "hpwdt");
256 unregister_nmi_handler(NMI_IO_CHECK
, "hpwdt");
260 static int hpwdt_init_one(struct pci_dev
*dev
,
261 const struct pci_device_id
*ent
)
266 * First let's find out if we are on an iLO2+ server. We will
267 * not run on a legacy ASM box.
268 * So we only support the G5 ProLiant servers and higher.
270 if (dev
->subsystem_vendor
!= PCI_VENDOR_ID_HP
&&
271 dev
->subsystem_vendor
!= PCI_VENDOR_ID_HP_3PAR
) {
273 "This server does not have an iLO2+ ASIC.\n");
278 * Ignore all auxilary iLO devices with the following PCI ID
280 if (dev
->subsystem_vendor
== PCI_VENDOR_ID_HP
&&
281 dev
->subsystem_device
== 0x1979)
284 if (pci_enable_device(dev
)) {
286 "Not possible to enable PCI Device: 0x%x:0x%x.\n",
287 ent
->vendor
, ent
->device
);
291 pci_mem_addr
= pci_iomap(dev
, 1, 0x80);
294 "Unable to detect the iLO2+ server memory.\n");
296 goto error_pci_iomap
;
298 hpwdt_nmistat
= pci_mem_addr
+ 0x6e;
299 hpwdt_timer_reg
= pci_mem_addr
+ 0x70;
300 hpwdt_timer_con
= pci_mem_addr
+ 0x72;
302 /* Make sure that timer is disabled until /dev/watchdog is opened */
305 /* Initialize NMI Decoding functionality */
306 retval
= hpwdt_init_nmi_decoding(dev
);
308 goto error_init_nmi_decoding
;
310 watchdog_set_nowayout(&hpwdt_dev
, nowayout
);
311 if (watchdog_init_timeout(&hpwdt_dev
, soft_margin
, NULL
))
312 dev_warn(&dev
->dev
, "Invalid soft_margin: %d.\n", soft_margin
);
314 if (pretimeout
&& hpwdt_dev
.timeout
<= PRETIMEOUT_SEC
) {
315 dev_warn(&dev
->dev
, "timeout <= pretimeout. Setting pretimeout to zero\n");
318 hpwdt_dev
.pretimeout
= pretimeout
? PRETIMEOUT_SEC
: 0;
320 hpwdt_dev
.parent
= &dev
->dev
;
321 retval
= watchdog_register_device(&hpwdt_dev
);
323 dev_err(&dev
->dev
, "watchdog register failed: %d.\n", retval
);
324 goto error_wd_register
;
327 dev_info(&dev
->dev
, "HPE Watchdog Timer Driver: Version: %s\n",
329 dev_info(&dev
->dev
, "timeout: %d seconds (nowayout=%d)\n",
330 hpwdt_dev
.timeout
, nowayout
);
331 dev_info(&dev
->dev
, "pretimeout: %s.\n",
332 pretimeout
? "on" : "off");
334 if (dev
->subsystem_vendor
== PCI_VENDOR_ID_HP_3PAR
)
340 hpwdt_exit_nmi_decoding();
341 error_init_nmi_decoding
:
342 pci_iounmap(dev
, pci_mem_addr
);
344 pci_disable_device(dev
);
348 static void hpwdt_exit(struct pci_dev
*dev
)
353 watchdog_unregister_device(&hpwdt_dev
);
354 hpwdt_exit_nmi_decoding();
355 pci_iounmap(dev
, pci_mem_addr
);
356 pci_disable_device(dev
);
359 static struct pci_driver hpwdt_driver
= {
361 .id_table
= hpwdt_devices
,
362 .probe
= hpwdt_init_one
,
363 .remove
= hpwdt_exit
,
366 MODULE_AUTHOR("Tom Mingarelli");
367 MODULE_DESCRIPTION("hpe watchdog driver");
368 MODULE_LICENSE("GPL");
369 MODULE_VERSION(HPWDT_VERSION
);
371 module_param(soft_margin
, int, 0);
372 MODULE_PARM_DESC(soft_margin
, "Watchdog timeout in seconds");
374 module_param_named(timeout
, soft_margin
, int, 0);
375 MODULE_PARM_DESC(timeout
, "Alias of soft_margin");
377 module_param(nowayout
, bool, 0);
378 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default="
379 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");
381 #ifdef CONFIG_HPWDT_NMI_DECODING
382 module_param(pretimeout
, bool, 0);
383 MODULE_PARM_DESC(pretimeout
, "Watchdog pretimeout enabled");
386 module_pci_driver(hpwdt_driver
);