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