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