Merge tag 'char-misc-6.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-block.git] / drivers / watchdog / hpwdt.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
7f4da474 2/*
ca22e79f 3 * HPE WatchDog Driver
7f4da474
TM
4 * based on
5 *
6 * SoftDog 0.05: A Software Watchdog Device
7 *
9a46fc4e 8 * (c) Copyright 2018 Hewlett Packard Enterprise Development LP
ca22e79f 9 * Thomas Mingarelli <thomas.mingarelli@hpe.com>
7f4da474
TM
10 */
11
27c766aa
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
7f4da474 14#include <linux/device.h>
7f4da474 15#include <linux/io.h>
7f4da474 16#include <linux/kernel.h>
7f4da474 17#include <linux/module.h>
7f4da474 18#include <linux/moduleparam.h>
7f4da474
TM
19#include <linux/pci.h>
20#include <linux/pci_ids.h>
7f4da474 21#include <linux/types.h>
7f4da474 22#include <linux/watchdog.h>
ed835d81 23#ifdef CONFIG_HPWDT_NMI_DECODING
d48b0e17 24#include <asm/nmi.h>
ed835d81 25#endif
acc195bd 26#include <linux/crash_dump.h>
7f4da474 27
5674b74e 28#define HPWDT_VERSION "2.0.4"
e802e32d 29#define SECS_TO_TICKS(secs) ((secs) * 1000 / 128)
6f681c2e 30#define TICKS_TO_SECS(ticks) ((ticks) * 128 / 1000)
be3d7f7c
JH
31#define HPWDT_MAX_TICKS 65535
32#define HPWDT_MAX_TIMER TICKS_TO_SECS(HPWDT_MAX_TICKS)
923410d0 33#define DEFAULT_MARGIN 30
0458f403 34#define PRETIMEOUT_SEC 9
923410d0 35
36static unsigned int soft_margin = DEFAULT_MARGIN; /* in seconds */
86a1e189 37static bool nowayout = WATCHDOG_NOWAYOUT;
0458f403 38static bool pretimeout = IS_ENABLED(CONFIG_HPWDT_NMI_DECODING);
be3d7f7c 39static int kdumptimeout = -1;
923410d0 40
41static void __iomem *pci_mem_addr; /* the PCI-memory address */
838534e5 42static unsigned long __iomem *hpwdt_nmistat;
923410d0 43static unsigned long __iomem *hpwdt_timer_reg;
44static unsigned long __iomem *hpwdt_timer_con;
45
bc17f9dc 46static const struct pci_device_id hpwdt_devices[] = {
36e3ff44 47 { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) }, /* iLO2 */
48 { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) }, /* iLO3 */
742b80c5 49 { PCI_DEVICE(PCI_VENDOR_ID_HP_3PAR, 0x0389) }, /* PCtrl */
923410d0 50 {0}, /* terminate list */
51};
52MODULE_DEVICE_TABLE(pci, hpwdt_devices);
53
94d6b80c
JH
54static const struct pci_device_id hpwdt_blacklist[] = {
55 { PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3306, PCI_VENDOR_ID_HP, 0x1979) }, /* auxilary iLO */
de2cb0cc 56 { PCI_DEVICE_SUB(PCI_VENDOR_ID_HP, 0x3306, PCI_VENDOR_ID_HP_3PAR, 0x0289) }, /* CL */
94d6b80c
JH
57 {0}, /* terminate list */
58};
7f4da474 59
be3d7f7c 60static struct watchdog_device hpwdt_dev;
7f4da474
TM
61/*
62 * Watchdog operations
63 */
bb721d6b
JH
64static int hpwdt_hw_is_running(void)
65{
66 return ioread8(hpwdt_timer_con) & 0x01;
67}
68
d0a4027f 69static int hpwdt_start(struct watchdog_device *wdd)
7f4da474 70{
0458f403 71 int control = 0x81 | (pretimeout ? 0x4 : 0);
c22d8e38 72 int reload = SECS_TO_TICKS(min(wdd->timeout, wdd->max_hw_heartbeat_ms/1000));
d0a4027f 73
c22d8e38 74 dev_dbg(wdd->parent, "start watchdog 0x%08x:0x%08x:0x%02x\n", wdd->timeout, reload, control);
7f4da474 75 iowrite16(reload, hpwdt_timer_reg);
0458f403 76 iowrite8(control, hpwdt_timer_con);
d0a4027f
JH
77
78 return 0;
7f4da474
TM
79}
80
81static void hpwdt_stop(void)
82{
83 unsigned long data;
84
ccfd6921
JH
85 pr_debug("stop watchdog\n");
86
d08c9a33 87 data = ioread8(hpwdt_timer_con);
7f4da474 88 data &= 0xFE;
d08c9a33 89 iowrite8(data, hpwdt_timer_con);
7f4da474
TM
90}
91
d0a4027f 92static int hpwdt_stop_core(struct watchdog_device *wdd)
7f4da474 93{
d0a4027f
JH
94 hpwdt_stop();
95
96 return 0;
7f4da474
TM
97}
98
be3d7f7c
JH
99static void hpwdt_ping_ticks(int val)
100{
101 val = min(val, HPWDT_MAX_TICKS);
102 iowrite16(val, hpwdt_timer_reg);
103}
104
d0a4027f 105static int hpwdt_ping(struct watchdog_device *wdd)
7f4da474 106{
c22d8e38 107 int reload = SECS_TO_TICKS(min(wdd->timeout, wdd->max_hw_heartbeat_ms/1000));
0458f403 108
c22d8e38 109 dev_dbg(wdd->parent, "ping watchdog 0x%08x:0x%08x\n", wdd->timeout, reload);
be3d7f7c 110 hpwdt_ping_ticks(reload);
0458f403 111
7f4da474
TM
112 return 0;
113}
114
d0a4027f 115static unsigned int hpwdt_gettimeleft(struct watchdog_device *wdd)
aae67f36 116{
117 return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
118}
119
d0a4027f
JH
120static int hpwdt_settimeout(struct watchdog_device *wdd, unsigned int val)
121{
ccfd6921
JH
122 dev_dbg(wdd->parent, "set_timeout = %d\n", val);
123
d0a4027f 124 wdd->timeout = val;
0458f403 125 if (val <= wdd->pretimeout) {
ccfd6921 126 dev_dbg(wdd->parent, "pretimeout < timeout. Setting to zero\n");
0458f403 127 wdd->pretimeout = 0;
17f0d1b9 128 pretimeout = false;
0458f403
JH
129 if (watchdog_active(wdd))
130 hpwdt_start(wdd);
131 }
d0a4027f
JH
132 hpwdt_ping(wdd);
133
134 return 0;
135}
136
aeebc6ba 137#ifdef CONFIG_HPWDT_NMI_DECODING
0458f403
JH
138static int hpwdt_set_pretimeout(struct watchdog_device *wdd, unsigned int req)
139{
140 unsigned int val = 0;
141
ccfd6921 142 dev_dbg(wdd->parent, "set_pretimeout = %d\n", req);
0458f403
JH
143 if (req) {
144 val = PRETIMEOUT_SEC;
145 if (val >= wdd->timeout)
146 return -EINVAL;
147 }
148
ccfd6921
JH
149 if (val != req)
150 dev_dbg(wdd->parent, "Rounding pretimeout to: %d\n", val);
151
0458f403
JH
152 wdd->pretimeout = val;
153 pretimeout = !!val;
154
155 if (watchdog_active(wdd))
156 hpwdt_start(wdd);
157
158 return 0;
159}
160
838534e5
JH
161static int hpwdt_my_nmi(void)
162{
163 return ioread8(hpwdt_nmistat) & 0x6;
164}
165
ab4ba3cd
TM
166/*
167 * NMI Handler
168 */
9c48f1c6 169static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
ab4ba3cd 170{
a042229a
JH
171 unsigned int mynmi = hpwdt_my_nmi();
172 static char panic_msg[] =
173 "00: An NMI occurred. Depending on your system the reason "
174 "for the NMI is logged in any one of the following resources:\n"
175 "1. Integrated Management Log (IML)\n"
176 "2. OA Syslog\n"
177 "3. OA Forward Progress Log\n"
178 "4. iLO Event Log";
179
dced0b3e 180 if (ulReason == NMI_UNKNOWN && !mynmi)
838534e5
JH
181 return NMI_DONE;
182
be3d7f7c
JH
183 if (kdumptimeout < 0)
184 hpwdt_stop();
185 else if (kdumptimeout == 0)
186 ;
187 else {
188 unsigned int val = max((unsigned int)kdumptimeout, hpwdt_dev.timeout);
189 hpwdt_ping_ticks(SECS_TO_TICKS(val));
190 }
dbc018ec 191
a042229a
JH
192 hex_byte_pack(panic_msg, mynmi);
193 nmi_panic(regs, panic_msg);
5efc7a62 194
abc514c5 195 return NMI_HANDLED;
ab4ba3cd 196}
86ded1f3 197#endif /* CONFIG_HPWDT_NMI_DECODING */
ab4ba3cd 198
7f4da474 199
42747d71 200static const struct watchdog_info ident = {
0458f403
JH
201 .options = WDIOF_PRETIMEOUT |
202 WDIOF_SETTIMEOUT |
7f4da474
TM
203 WDIOF_KEEPALIVEPING |
204 WDIOF_MAGICCLOSE,
ca22e79f 205 .identity = "HPE iLO2+ HW Watchdog Timer",
7f4da474
TM
206};
207
7f4da474
TM
208/*
209 * Kernel interfaces
210 */
d0a4027f
JH
211
212static const struct watchdog_ops hpwdt_ops = {
213 .owner = THIS_MODULE,
214 .start = hpwdt_start,
215 .stop = hpwdt_stop_core,
216 .ping = hpwdt_ping,
217 .set_timeout = hpwdt_settimeout,
218 .get_timeleft = hpwdt_gettimeleft,
0458f403
JH
219#ifdef CONFIG_HPWDT_NMI_DECODING
220 .set_pretimeout = hpwdt_set_pretimeout,
221#endif
7f4da474
TM
222};
223
d0a4027f
JH
224static struct watchdog_device hpwdt_dev = {
225 .info = &ident,
226 .ops = &hpwdt_ops,
227 .min_timeout = 1,
d0a4027f 228 .timeout = DEFAULT_MARGIN,
0458f403 229 .pretimeout = PRETIMEOUT_SEC,
c22d8e38 230 .max_hw_heartbeat_ms = HPWDT_MAX_TIMER * 1000,
7f4da474
TM
231};
232
d0a4027f 233
7f4da474
TM
234/*
235 * Init & Exit
236 */
237
2d991a16 238static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
2ec7ed67 239{
2b3d89b4 240#ifdef CONFIG_HPWDT_NMI_DECODING
2ec7ed67 241 int retval;
2ec7ed67 242 /*
09ee1014 243 * Only one function can register for NMI_UNKNOWN
2ec7ed67 244 */
09ee1014 245 retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout, 0, "hpwdt");
553222f3
DZ
246 if (retval)
247 goto error;
248 retval = register_nmi_handler(NMI_SERR, hpwdt_pretimeout, 0, "hpwdt");
249 if (retval)
250 goto error1;
251 retval = register_nmi_handler(NMI_IO_CHECK, hpwdt_pretimeout, 0, "hpwdt");
252 if (retval)
253 goto error2;
2ec7ed67 254
255 dev_info(&dev->dev,
703fc3df
JH
256 "HPE Watchdog Timer Driver: NMI decoding initialized\n");
257
2ec7ed67 258 return 0;
553222f3
DZ
259
260error2:
261 unregister_nmi_handler(NMI_SERR, "hpwdt");
262error1:
263 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
264error:
265 dev_warn(&dev->dev,
266 "Unable to register a die notifier (err=%d).\n",
267 retval);
553222f3 268 return retval;
2b3d89b4
JH
269#endif /* CONFIG_HPWDT_NMI_DECODING */
270 return 0;
2ec7ed67 271}
272
b77b7088 273static void hpwdt_exit_nmi_decoding(void)
2ec7ed67 274{
2b3d89b4 275#ifdef CONFIG_HPWDT_NMI_DECODING
9c48f1c6 276 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
a089361c
MT
277 unregister_nmi_handler(NMI_SERR, "hpwdt");
278 unregister_nmi_handler(NMI_IO_CHECK, "hpwdt");
2b3d89b4 279#endif
86ded1f3 280}
281
2d991a16 282static int hpwdt_init_one(struct pci_dev *dev,
ab4ba3cd 283 const struct pci_device_id *ent)
7f4da474
TM
284{
285 int retval;
286
287 /*
36e3ff44 288 * First let's find out if we are on an iLO2+ server. We will
7f4da474 289 * not run on a legacy ASM box.
ab4ba3cd 290 * So we only support the G5 ProLiant servers and higher.
7f4da474 291 */
fc113d54
BB
292 if (dev->subsystem_vendor != PCI_VENDOR_ID_HP &&
293 dev->subsystem_vendor != PCI_VENDOR_ID_HP_3PAR) {
7f4da474 294 dev_warn(&dev->dev,
36e3ff44 295 "This server does not have an iLO2+ ASIC.\n");
7f4da474
TM
296 return -ENODEV;
297 }
298
94d6b80c
JH
299 if (pci_match_id(hpwdt_blacklist, dev)) {
300 dev_dbg(&dev->dev, "Not supported on this device\n");
0821f20d 301 return -ENODEV;
94d6b80c 302 }
0821f20d 303
7f4da474
TM
304 if (pci_enable_device(dev)) {
305 dev_warn(&dev->dev,
306 "Not possible to enable PCI Device: 0x%x:0x%x.\n",
307 ent->vendor, ent->device);
308 return -ENODEV;
309 }
310
311 pci_mem_addr = pci_iomap(dev, 1, 0x80);
312 if (!pci_mem_addr) {
313 dev_warn(&dev->dev,
36e3ff44 314 "Unable to detect the iLO2+ server memory.\n");
7f4da474
TM
315 retval = -ENOMEM;
316 goto error_pci_iomap;
317 }
838534e5 318 hpwdt_nmistat = pci_mem_addr + 0x6e;
7f4da474
TM
319 hpwdt_timer_reg = pci_mem_addr + 0x70;
320 hpwdt_timer_con = pci_mem_addr + 0x72;
321
bb721d6b
JH
322 /* Have the core update running timer until user space is ready */
323 if (hpwdt_hw_is_running()) {
324 dev_info(&dev->dev, "timer is running\n");
325 set_bit(WDOG_HW_RUNNING, &hpwdt_dev.status);
326 }
308b135e 327
2ec7ed67 328 /* Initialize NMI Decoding functionality */
329 retval = hpwdt_init_nmi_decoding(dev);
330 if (retval != 0)
331 goto error_init_nmi_decoding;
7f4da474 332
48b32199 333 watchdog_stop_on_unregister(&hpwdt_dev);
d0a4027f 334 watchdog_set_nowayout(&hpwdt_dev, nowayout);
87dfe210 335 watchdog_init_timeout(&hpwdt_dev, soft_margin, NULL);
d0a4027f 336
acc195bd 337 if (is_kdump_kernel()) {
17f0d1b9 338 pretimeout = false;
acc195bd
JH
339 kdumptimeout = 0;
340 }
341
10d790d1
JH
342 if (pretimeout && hpwdt_dev.timeout <= PRETIMEOUT_SEC) {
343 dev_warn(&dev->dev, "timeout <= pretimeout. Setting pretimeout to zero\n");
17f0d1b9 344 pretimeout = false;
10d790d1 345 }
4d9186d0 346 hpwdt_dev.pretimeout = pretimeout ? PRETIMEOUT_SEC : 0;
be3d7f7c 347 kdumptimeout = min(kdumptimeout, HPWDT_MAX_TIMER);
4d9186d0 348
d0a4027f
JH
349 hpwdt_dev.parent = &dev->dev;
350 retval = watchdog_register_device(&hpwdt_dev);
f51540b8 351 if (retval < 0)
d0a4027f 352 goto error_wd_register;
7f4da474 353
92301461
JH
354 dev_info(&dev->dev, "HPE Watchdog Timer Driver: Version: %s\n",
355 HPWDT_VERSION);
356 dev_info(&dev->dev, "timeout: %d seconds (nowayout=%d)\n",
357 hpwdt_dev.timeout, nowayout);
358 dev_info(&dev->dev, "pretimeout: %s.\n",
359 pretimeout ? "on" : "off");
be3d7f7c 360 dev_info(&dev->dev, "kdumptimeout: %d.\n", kdumptimeout);
d0a4027f 361
7f4da474
TM
362 return 0;
363
d0a4027f 364error_wd_register:
2ec7ed67 365 hpwdt_exit_nmi_decoding();
366error_init_nmi_decoding:
7f4da474
TM
367 pci_iounmap(dev, pci_mem_addr);
368error_pci_iomap:
369 pci_disable_device(dev);
370 return retval;
371}
372
4b12b896 373static void hpwdt_exit(struct pci_dev *dev)
7f4da474 374{
d0a4027f 375 watchdog_unregister_device(&hpwdt_dev);
2ec7ed67 376 hpwdt_exit_nmi_decoding();
7f4da474
TM
377 pci_iounmap(dev, pci_mem_addr);
378 pci_disable_device(dev);
379}
380
381static struct pci_driver hpwdt_driver = {
382 .name = "hpwdt",
383 .id_table = hpwdt_devices,
384 .probe = hpwdt_init_one,
82268714 385 .remove = hpwdt_exit,
7f4da474
TM
386};
387
7f4da474 388MODULE_AUTHOR("Tom Mingarelli");
9a46fc4e 389MODULE_DESCRIPTION("hpe watchdog driver");
7f4da474 390MODULE_LICENSE("GPL");
d8100c3a 391MODULE_VERSION(HPWDT_VERSION);
7f4da474
TM
392
393module_param(soft_margin, int, 0);
394MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
395
397a35d4
JH
396module_param_named(timeout, soft_margin, int, 0);
397MODULE_PARM_DESC(timeout, "Alias of soft_margin");
398
86a1e189 399module_param(nowayout, bool, 0);
7f4da474
TM
400MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
401 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
402
be3d7f7c
JH
403module_param(kdumptimeout, int, 0444);
404MODULE_PARM_DESC(kdumptimeout, "Timeout applied for crash kernel transition in seconds");
405
0458f403
JH
406#ifdef CONFIG_HPWDT_NMI_DECODING
407module_param(pretimeout, bool, 0);
408MODULE_PARM_DESC(pretimeout, "Watchdog pretimeout enabled");
409#endif
410
5ce9c371 411module_pci_driver(hpwdt_driver);