blob: b8205c6e61c149f288f0138f26d0ff5b7c425526 [file] [log] [blame]
Thomas Mingarelli7f4da472007-12-04 17:41:54 +00001/*
Mingarelli, Thomasca22e792015-12-14 20:22:09 +00002 * HPE WatchDog Driver
Thomas Mingarelli7f4da472007-12-04 17:41:54 +00003 * based on
4 *
5 * SoftDog 0.05: A Software Watchdog Device
6 *
Jerry Hoemann9a46fc42018-02-25 20:22:19 -07007 * (c) Copyright 2018 Hewlett Packard Enterprise Development LP
Mingarelli, Thomasca22e792015-12-14 20:22:09 +00008 * Thomas Mingarelli <thomas.mingarelli@hpe.com>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +00009 *
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
Joe Perches27c766a2012-02-15 15:06:19 -080016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000018#include <linux/device.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000019#include <linux/io.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000020#include <linux/kernel.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000021#include <linux/module.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000022#include <linux/moduleparam.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000023#include <linux/pci.h>
24#include <linux/pci_ids.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000025#include <linux/types.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000026#include <linux/watchdog.h>
Ingo Molnard48b0e12011-10-06 14:20:27 +020027#include <asm/nmi.h>
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000028
Brian Boylstonfc113d52016-09-26 13:57:14 -050029#define HPWDT_VERSION "1.4.0"
dann fraziere802e322010-06-02 16:23:39 -060030#define SECS_TO_TICKS(secs) ((secs) * 1000 / 128)
dann frazier6f681c22010-06-02 16:23:40 -060031#define TICKS_TO_SECS(ticks) ((ticks) * 128 / 1000)
32#define HPWDT_MAX_TIMER TICKS_TO_SECS(65535)
dann frazier923410d2010-07-27 17:50:54 -060033#define DEFAULT_MARGIN 30
Jerry Hoemann0458f402018-02-25 20:22:25 -070034#define PRETIMEOUT_SEC 9
dann frazier923410d2010-07-27 17:50:54 -060035
Jerry Hoemanna6c24732018-02-25 20:22:23 -070036static bool ilo5;
dann frazier923410d2010-07-27 17:50:54 -060037static unsigned int soft_margin = DEFAULT_MARGIN; /* in seconds */
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010038static bool nowayout = WATCHDOG_NOWAYOUT;
Jerry Hoemann0458f402018-02-25 20:22:25 -070039static bool pretimeout = IS_ENABLED(CONFIG_HPWDT_NMI_DECODING);
dann frazier923410d2010-07-27 17:50:54 -060040
41static void __iomem *pci_mem_addr; /* the PCI-memory address */
Jerry Hoemann838534e2017-10-23 16:46:17 -060042static unsigned long __iomem *hpwdt_nmistat;
dann frazier923410d2010-07-27 17:50:54 -060043static unsigned long __iomem *hpwdt_timer_reg;
44static unsigned long __iomem *hpwdt_timer_con;
45
Jingoo Hanbc17f9d2013-12-03 08:30:22 +090046static const struct pci_device_id hpwdt_devices[] = {
dann frazier36e3ff42010-07-27 17:50:57 -060047 { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) }, /* iLO2 */
48 { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) }, /* iLO3 */
dann frazier923410d2010-07-27 17:50:54 -060049 {0}, /* terminate list */
50};
51MODULE_DEVICE_TABLE(pci, hpwdt_devices);
52
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000053
54/*
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000055 * Watchdog operations
56 */
Jerry Hoemannd0a40272018-02-25 20:22:22 -070057static int hpwdt_start(struct watchdog_device *wdd)
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000058{
Jerry Hoemann0458f402018-02-25 20:22:25 -070059 int control = 0x81 | (pretimeout ? 0x4 : 0);
60 int reload = SECS_TO_TICKS(wdd->timeout);
Jerry Hoemannd0a40272018-02-25 20:22:22 -070061
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000062 iowrite16(reload, hpwdt_timer_reg);
Jerry Hoemann0458f402018-02-25 20:22:25 -070063 iowrite8(control, hpwdt_timer_con);
Jerry Hoemannd0a40272018-02-25 20:22:22 -070064
65 return 0;
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000066}
67
68static void hpwdt_stop(void)
69{
70 unsigned long data;
71
Mingarelli, Thomasd08c9a32012-04-03 05:37:01 +000072 data = ioread8(hpwdt_timer_con);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000073 data &= 0xFE;
Mingarelli, Thomasd08c9a32012-04-03 05:37:01 +000074 iowrite8(data, hpwdt_timer_con);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000075}
76
Jerry Hoemannd0a40272018-02-25 20:22:22 -070077static int hpwdt_stop_core(struct watchdog_device *wdd)
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000078{
Jerry Hoemannd0a40272018-02-25 20:22:22 -070079 hpwdt_stop();
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000080
81 return 0;
82}
83
Jerry Hoemannd0a40272018-02-25 20:22:22 -070084static int hpwdt_ping(struct watchdog_device *wdd)
85{
Jerry Hoemann0458f402018-02-25 20:22:25 -070086 int reload = SECS_TO_TICKS(wdd->timeout);
87
Jerry Hoemannd0a40272018-02-25 20:22:22 -070088 iowrite16(reload, hpwdt_timer_reg);
Jerry Hoemann0458f402018-02-25 20:22:25 -070089
Jerry Hoemannd0a40272018-02-25 20:22:22 -070090 return 0;
91}
92
93static unsigned int hpwdt_gettimeleft(struct watchdog_device *wdd)
dann frazieraae67f32010-06-02 16:23:41 -060094{
95 return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
96}
97
Jerry Hoemannd0a40272018-02-25 20:22:22 -070098static int hpwdt_settimeout(struct watchdog_device *wdd, unsigned int val)
99{
100 wdd->timeout = val;
Jerry Hoemann0458f402018-02-25 20:22:25 -0700101 if (val <= wdd->pretimeout) {
102 wdd->pretimeout = 0;
103 pretimeout = 0;
104 if (watchdog_active(wdd))
105 hpwdt_start(wdd);
106 }
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700107 hpwdt_ping(wdd);
108
109 return 0;
110}
111
Arnd Bergmannaeebc6b2017-12-06 22:02:37 +0100112#ifdef CONFIG_HPWDT_NMI_DECODING
Jerry Hoemann0458f402018-02-25 20:22:25 -0700113static int hpwdt_set_pretimeout(struct watchdog_device *wdd, unsigned int req)
114{
115 unsigned int val = 0;
116
117 if (req) {
118 val = PRETIMEOUT_SEC;
119 if (val >= wdd->timeout)
120 return -EINVAL;
121 }
122
123 wdd->pretimeout = val;
124 pretimeout = !!val;
125
126 if (watchdog_active(wdd))
127 hpwdt_start(wdd);
128
129 return 0;
130}
131
Jerry Hoemann838534e2017-10-23 16:46:17 -0600132static int hpwdt_my_nmi(void)
133{
134 return ioread8(hpwdt_nmistat) & 0x6;
135}
136
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000137/*
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000138 * NMI Handler
139 */
Don Zickus9c48f1c2011-09-30 15:06:21 -0400140static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000141{
Jerry Hoemanna0422292018-02-25 20:22:21 -0700142 unsigned int mynmi = hpwdt_my_nmi();
143 static char panic_msg[] =
144 "00: An NMI occurred. Depending on your system the reason "
145 "for the NMI is logged in any one of the following resources:\n"
146 "1. Integrated Management Log (IML)\n"
147 "2. OA Syslog\n"
148 "3. OA Forward Progress Log\n"
149 "4. iLO Event Log";
150
Jerry Hoemanna6c24732018-02-25 20:22:23 -0700151 if (ilo5 && ulReason == NMI_UNKNOWN && mynmi)
Jerry Hoemann838534e2017-10-23 16:46:17 -0600152 return NMI_DONE;
153
Jerry Hoemann0458f402018-02-25 20:22:25 -0700154 if (ilo5 && !pretimeout)
155 return NMI_DONE;
156
Jerry Hoemann703fc3d2018-02-25 20:22:24 -0700157 hpwdt_stop();
Naga Chumbalkardbc018e2011-08-09 22:27:26 +0000158
Jerry Hoemanna0422292018-02-25 20:22:21 -0700159 hex_byte_pack(panic_msg, mynmi);
160 nmi_panic(regs, panic_msg);
Thomas Mingarelli5efc7a62011-07-26 14:05:53 +0100161
Hidehiro Kawaiabc514c2016-03-22 14:27:24 -0700162 return NMI_HANDLED;
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000163}
dann frazier86ded1f2010-07-27 17:51:02 -0600164#endif /* CONFIG_HPWDT_NMI_DECODING */
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000165
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000166
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000167static const struct watchdog_info ident = {
Jerry Hoemann0458f402018-02-25 20:22:25 -0700168 .options = WDIOF_PRETIMEOUT |
169 WDIOF_SETTIMEOUT |
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000170 WDIOF_KEEPALIVEPING |
171 WDIOF_MAGICCLOSE,
Mingarelli, Thomasca22e792015-12-14 20:22:09 +0000172 .identity = "HPE iLO2+ HW Watchdog Timer",
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000173};
174
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000175/*
176 * Kernel interfaces
177 */
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700178
179static const struct watchdog_ops hpwdt_ops = {
180 .owner = THIS_MODULE,
181 .start = hpwdt_start,
182 .stop = hpwdt_stop_core,
183 .ping = hpwdt_ping,
184 .set_timeout = hpwdt_settimeout,
185 .get_timeleft = hpwdt_gettimeleft,
Jerry Hoemann0458f402018-02-25 20:22:25 -0700186#ifdef CONFIG_HPWDT_NMI_DECODING
187 .set_pretimeout = hpwdt_set_pretimeout,
188#endif
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000189};
190
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700191static struct watchdog_device hpwdt_dev = {
192 .info = &ident,
193 .ops = &hpwdt_ops,
194 .min_timeout = 1,
195 .max_timeout = HPWDT_MAX_TIMER,
196 .timeout = DEFAULT_MARGIN,
Jerry Hoemann0458f402018-02-25 20:22:25 -0700197#ifdef CONFIG_HPWDT_NMI_DECODING
198 .pretimeout = PRETIMEOUT_SEC,
199#endif
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000200};
201
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700202
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000203/*
204 * Init & Exit
205 */
206
Bill Pemberton2d991a12012-11-19 13:21:41 -0500207static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
dann frazier2ec7ed62010-07-28 12:38:43 -0600208{
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -0700209#ifdef CONFIG_HPWDT_NMI_DECODING
dann frazier2ec7ed62010-07-28 12:38:43 -0600210 int retval;
dann frazier2ec7ed62010-07-28 12:38:43 -0600211 /*
Don Zickus09ee1012012-03-29 16:11:15 -0400212 * Only one function can register for NMI_UNKNOWN
dann frazier2ec7ed62010-07-28 12:38:43 -0600213 */
Don Zickus09ee1012012-03-29 16:11:15 -0400214 retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout, 0, "hpwdt");
Don Zickus553222f2012-03-29 16:11:16 -0400215 if (retval)
216 goto error;
217 retval = register_nmi_handler(NMI_SERR, hpwdt_pretimeout, 0, "hpwdt");
218 if (retval)
219 goto error1;
220 retval = register_nmi_handler(NMI_IO_CHECK, hpwdt_pretimeout, 0, "hpwdt");
221 if (retval)
222 goto error2;
dann frazier2ec7ed62010-07-28 12:38:43 -0600223
224 dev_info(&dev->dev,
Jerry Hoemann703fc3d2018-02-25 20:22:24 -0700225 "HPE Watchdog Timer Driver: NMI decoding initialized\n");
226
dann frazier2ec7ed62010-07-28 12:38:43 -0600227 return 0;
Don Zickus553222f2012-03-29 16:11:16 -0400228
229error2:
230 unregister_nmi_handler(NMI_SERR, "hpwdt");
231error1:
232 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
233error:
234 dev_warn(&dev->dev,
235 "Unable to register a die notifier (err=%d).\n",
236 retval);
Don Zickus553222f2012-03-29 16:11:16 -0400237 return retval;
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -0700238#endif /* CONFIG_HPWDT_NMI_DECODING */
dann frazier86ded1f2010-07-27 17:51:02 -0600239 return 0;
240}
241
Axel Linb77b7082011-03-02 11:49:44 +0800242static void hpwdt_exit_nmi_decoding(void)
dann frazier86ded1f2010-07-27 17:51:02 -0600243{
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -0700244#ifdef CONFIG_HPWDT_NMI_DECODING
245 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
246 unregister_nmi_handler(NMI_SERR, "hpwdt");
247 unregister_nmi_handler(NMI_IO_CHECK, "hpwdt");
248#endif
dann frazier86ded1f2010-07-27 17:51:02 -0600249}
Thomas Mingarelli47bece82009-06-04 19:50:45 +0000250
Bill Pemberton2d991a12012-11-19 13:21:41 -0500251static int hpwdt_init_one(struct pci_dev *dev,
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000252 const struct pci_device_id *ent)
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000253{
254 int retval;
255
256 /*
dann frazier36e3ff42010-07-27 17:50:57 -0600257 * First let's find out if we are on an iLO2+ server. We will
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000258 * not run on a legacy ASM box.
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000259 * So we only support the G5 ProLiant servers and higher.
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000260 */
Brian Boylstonfc113d52016-09-26 13:57:14 -0500261 if (dev->subsystem_vendor != PCI_VENDOR_ID_HP &&
262 dev->subsystem_vendor != PCI_VENDOR_ID_HP_3PAR) {
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000263 dev_warn(&dev->dev,
dann frazier36e3ff42010-07-27 17:50:57 -0600264 "This server does not have an iLO2+ ASIC.\n");
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000265 return -ENODEV;
266 }
267
Mingarelli, Thomas0821f202013-08-09 16:31:09 +0000268 /*
269 * Ignore all auxilary iLO devices with the following PCI ID
270 */
Brian Boylstonfc113d52016-09-26 13:57:14 -0500271 if (dev->subsystem_vendor == PCI_VENDOR_ID_HP &&
272 dev->subsystem_device == 0x1979)
Mingarelli, Thomas0821f202013-08-09 16:31:09 +0000273 return -ENODEV;
274
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000275 if (pci_enable_device(dev)) {
276 dev_warn(&dev->dev,
277 "Not possible to enable PCI Device: 0x%x:0x%x.\n",
278 ent->vendor, ent->device);
279 return -ENODEV;
280 }
281
282 pci_mem_addr = pci_iomap(dev, 1, 0x80);
283 if (!pci_mem_addr) {
284 dev_warn(&dev->dev,
dann frazier36e3ff42010-07-27 17:50:57 -0600285 "Unable to detect the iLO2+ server memory.\n");
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000286 retval = -ENOMEM;
287 goto error_pci_iomap;
288 }
Jerry Hoemann838534e2017-10-23 16:46:17 -0600289 hpwdt_nmistat = pci_mem_addr + 0x6e;
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000290 hpwdt_timer_reg = pci_mem_addr + 0x70;
291 hpwdt_timer_con = pci_mem_addr + 0x72;
292
Toshi Kani308b1352012-08-27 12:52:24 -0600293 /* Make sure that timer is disabled until /dev/watchdog is opened */
294 hpwdt_stop();
295
dann frazier2ec7ed62010-07-28 12:38:43 -0600296 /* Initialize NMI Decoding functionality */
297 retval = hpwdt_init_nmi_decoding(dev);
298 if (retval != 0)
299 goto error_init_nmi_decoding;
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000300
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700301 watchdog_set_nowayout(&hpwdt_dev, nowayout);
302 if (watchdog_init_timeout(&hpwdt_dev, soft_margin, NULL))
303 dev_warn(&dev->dev, "Invalid soft_margin: %d.\n", soft_margin);
304
305 hpwdt_dev.parent = &dev->dev;
306 retval = watchdog_register_device(&hpwdt_dev);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000307 if (retval < 0) {
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700308 dev_err(&dev->dev, "watchdog register failed: %d.\n", retval);
309 goto error_wd_register;
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000310 }
311
Mingarelli, Thomasca22e792015-12-14 20:22:09 +0000312 dev_info(&dev->dev, "HPE Watchdog Timer Driver: %s"
dann frazier2ec7ed62010-07-28 12:38:43 -0600313 ", timer margin: %d seconds (nowayout=%d).\n",
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700314 HPWDT_VERSION, hpwdt_dev.timeout, nowayout);
315
Jerry Hoemanna6c24732018-02-25 20:22:23 -0700316 if (dev->subsystem_vendor == PCI_VENDOR_ID_HP_3PAR)
317 ilo5 = true;
318
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000319 return 0;
320
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700321error_wd_register:
dann frazier2ec7ed62010-07-28 12:38:43 -0600322 hpwdt_exit_nmi_decoding();
323error_init_nmi_decoding:
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000324 pci_iounmap(dev, pci_mem_addr);
325error_pci_iomap:
326 pci_disable_device(dev);
327 return retval;
328}
329
Bill Pemberton4b12b892012-11-19 13:26:24 -0500330static void hpwdt_exit(struct pci_dev *dev)
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000331{
332 if (!nowayout)
333 hpwdt_stop();
334
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700335 watchdog_unregister_device(&hpwdt_dev);
dann frazier2ec7ed62010-07-28 12:38:43 -0600336 hpwdt_exit_nmi_decoding();
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000337 pci_iounmap(dev, pci_mem_addr);
338 pci_disable_device(dev);
339}
340
341static struct pci_driver hpwdt_driver = {
342 .name = "hpwdt",
343 .id_table = hpwdt_devices,
344 .probe = hpwdt_init_one,
Bill Pemberton82268712012-11-19 13:21:12 -0500345 .remove = hpwdt_exit,
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000346};
347
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000348MODULE_AUTHOR("Tom Mingarelli");
Jerry Hoemann9a46fc42018-02-25 20:22:19 -0700349MODULE_DESCRIPTION("hpe watchdog driver");
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000350MODULE_LICENSE("GPL");
Thomas Mingarellid8100c32009-03-03 00:17:16 +0000351MODULE_VERSION(HPWDT_VERSION);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000352
353module_param(soft_margin, int, 0);
354MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
355
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100356module_param(nowayout, bool, 0);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000357MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
358 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
359
Jerry Hoemann0458f402018-02-25 20:22:25 -0700360#ifdef CONFIG_HPWDT_NMI_DECODING
361module_param(pretimeout, bool, 0);
362MODULE_PARM_DESC(pretimeout, "Watchdog pretimeout enabled");
363#endif
364
Wim Van Sebroeck5ce9c372012-05-04 14:43:25 +0200365module_pci_driver(hpwdt_driver);