blob: 0e35bb735d8efe2c6893799ec43e35643d044b4a [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
34
35static unsigned int soft_margin = DEFAULT_MARGIN; /* in seconds */
36static unsigned int reload; /* the computed soft_margin */
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +010037static bool nowayout = WATCHDOG_NOWAYOUT;
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -070038#ifdef CONFIG_HPWDT_NMI_DECODING
39static unsigned int allow_kdump = 1;
40#endif
dann frazier923410d2010-07-27 17:50:54 -060041
42static void __iomem *pci_mem_addr; /* the PCI-memory address */
Jerry Hoemann838534e2017-10-23 16:46:17 -060043static unsigned long __iomem *hpwdt_nmistat;
dann frazier923410d2010-07-27 17:50:54 -060044static unsigned long __iomem *hpwdt_timer_reg;
45static unsigned long __iomem *hpwdt_timer_con;
46
Jingoo Hanbc17f9d2013-12-03 08:30:22 +090047static const struct pci_device_id hpwdt_devices[] = {
dann frazier36e3ff42010-07-27 17:50:57 -060048 { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB203) }, /* iLO2 */
49 { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3306) }, /* iLO3 */
dann frazier923410d2010-07-27 17:50:54 -060050 {0}, /* terminate list */
51};
52MODULE_DEVICE_TABLE(pci, hpwdt_devices);
53
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000054
55/*
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000056 * Watchdog operations
57 */
Jerry Hoemannd0a40272018-02-25 20:22:22 -070058static int hpwdt_start(struct watchdog_device *wdd)
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000059{
Jerry Hoemannd0a40272018-02-25 20:22:22 -070060 reload = SECS_TO_TICKS(wdd->timeout);
61
Thomas Mingarelli7f4da472007-12-04 17:41:54 +000062 iowrite16(reload, hpwdt_timer_reg);
Mingarelli, Thomasd08c9a32012-04-03 05:37:01 +000063 iowrite8(0x85, 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{
86 iowrite16(reload, hpwdt_timer_reg);
87 return 0;
88}
89
90static unsigned int hpwdt_gettimeleft(struct watchdog_device *wdd)
dann frazieraae67f32010-06-02 16:23:41 -060091{
92 return TICKS_TO_SECS(ioread16(hpwdt_timer_reg));
93}
94
Jerry Hoemannd0a40272018-02-25 20:22:22 -070095static int hpwdt_settimeout(struct watchdog_device *wdd, unsigned int val)
96{
97 wdd->timeout = val;
98 hpwdt_ping(wdd);
99
100 return 0;
101}
102
Arnd Bergmannaeebc6b2017-12-06 22:02:37 +0100103#ifdef CONFIG_HPWDT_NMI_DECODING
Jerry Hoemann838534e2017-10-23 16:46:17 -0600104static int hpwdt_my_nmi(void)
105{
106 return ioread8(hpwdt_nmistat) & 0x6;
107}
108
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000109/*
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000110 * NMI Handler
111 */
Don Zickus9c48f1c2011-09-30 15:06:21 -0400112static int hpwdt_pretimeout(unsigned int ulReason, struct pt_regs *regs)
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000113{
Jerry Hoemanna0422292018-02-25 20:22:21 -0700114 unsigned int mynmi = hpwdt_my_nmi();
115 static char panic_msg[] =
116 "00: An NMI occurred. Depending on your system the reason "
117 "for the NMI is logged in any one of the following resources:\n"
118 "1. Integrated Management Log (IML)\n"
119 "2. OA Syslog\n"
120 "3. OA Forward Progress Log\n"
121 "4. iLO Event Log";
122
123 if ((ulReason == NMI_UNKNOWN) && mynmi)
Jerry Hoemann838534e2017-10-23 16:46:17 -0600124 return NMI_DONE;
125
Thomas Mingarelli5efc7a62011-07-26 14:05:53 +0100126 if (allow_kdump)
127 hpwdt_stop();
Naga Chumbalkardbc018e2011-08-09 22:27:26 +0000128
Jerry Hoemanna0422292018-02-25 20:22:21 -0700129 hex_byte_pack(panic_msg, mynmi);
130 nmi_panic(regs, panic_msg);
Thomas Mingarelli5efc7a62011-07-26 14:05:53 +0100131
Hidehiro Kawaiabc514c2016-03-22 14:27:24 -0700132 return NMI_HANDLED;
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000133}
dann frazier86ded1f2010-07-27 17:51:02 -0600134#endif /* CONFIG_HPWDT_NMI_DECODING */
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000135
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000136
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000137static const struct watchdog_info ident = {
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000138 .options = WDIOF_SETTIMEOUT |
139 WDIOF_KEEPALIVEPING |
140 WDIOF_MAGICCLOSE,
Mingarelli, Thomasca22e792015-12-14 20:22:09 +0000141 .identity = "HPE iLO2+ HW Watchdog Timer",
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000142};
143
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000144/*
145 * Kernel interfaces
146 */
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700147
148static const struct watchdog_ops hpwdt_ops = {
149 .owner = THIS_MODULE,
150 .start = hpwdt_start,
151 .stop = hpwdt_stop_core,
152 .ping = hpwdt_ping,
153 .set_timeout = hpwdt_settimeout,
154 .get_timeleft = hpwdt_gettimeleft,
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000155};
156
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700157static struct watchdog_device hpwdt_dev = {
158 .info = &ident,
159 .ops = &hpwdt_ops,
160 .min_timeout = 1,
161 .max_timeout = HPWDT_MAX_TIMER,
162 .timeout = DEFAULT_MARGIN,
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000163};
164
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700165
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000166/*
167 * Init & Exit
168 */
169
Bill Pemberton2d991a12012-11-19 13:21:41 -0500170static int hpwdt_init_nmi_decoding(struct pci_dev *dev)
dann frazier2ec7ed62010-07-28 12:38:43 -0600171{
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -0700172#ifdef CONFIG_HPWDT_NMI_DECODING
dann frazier2ec7ed62010-07-28 12:38:43 -0600173 int retval;
dann frazier2ec7ed62010-07-28 12:38:43 -0600174 /*
Don Zickus09ee1012012-03-29 16:11:15 -0400175 * Only one function can register for NMI_UNKNOWN
dann frazier2ec7ed62010-07-28 12:38:43 -0600176 */
Don Zickus09ee1012012-03-29 16:11:15 -0400177 retval = register_nmi_handler(NMI_UNKNOWN, hpwdt_pretimeout, 0, "hpwdt");
Don Zickus553222f2012-03-29 16:11:16 -0400178 if (retval)
179 goto error;
180 retval = register_nmi_handler(NMI_SERR, hpwdt_pretimeout, 0, "hpwdt");
181 if (retval)
182 goto error1;
183 retval = register_nmi_handler(NMI_IO_CHECK, hpwdt_pretimeout, 0, "hpwdt");
184 if (retval)
185 goto error2;
dann frazier2ec7ed62010-07-28 12:38:43 -0600186
187 dev_info(&dev->dev,
Mingarelli, Thomasca22e792015-12-14 20:22:09 +0000188 "HPE Watchdog Timer Driver: NMI decoding initialized"
Masanari Iidab91b5be2014-10-22 20:24:35 +0900189 ", allow kernel dump: %s (default = 1/ON)\n",
Don Zickus09ee1012012-03-29 16:11:15 -0400190 (allow_kdump == 0) ? "OFF" : "ON");
dann frazier2ec7ed62010-07-28 12:38:43 -0600191 return 0;
Don Zickus553222f2012-03-29 16:11:16 -0400192
193error2:
194 unregister_nmi_handler(NMI_SERR, "hpwdt");
195error1:
196 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
197error:
198 dev_warn(&dev->dev,
199 "Unable to register a die notifier (err=%d).\n",
200 retval);
Don Zickus553222f2012-03-29 16:11:16 -0400201 return retval;
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -0700202#endif /* CONFIG_HPWDT_NMI_DECODING */
dann frazier86ded1f2010-07-27 17:51:02 -0600203 return 0;
204}
205
Axel Linb77b7082011-03-02 11:49:44 +0800206static void hpwdt_exit_nmi_decoding(void)
dann frazier86ded1f2010-07-27 17:51:02 -0600207{
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -0700208#ifdef CONFIG_HPWDT_NMI_DECODING
209 unregister_nmi_handler(NMI_UNKNOWN, "hpwdt");
210 unregister_nmi_handler(NMI_SERR, "hpwdt");
211 unregister_nmi_handler(NMI_IO_CHECK, "hpwdt");
212#endif
dann frazier86ded1f2010-07-27 17:51:02 -0600213}
Thomas Mingarelli47bece82009-06-04 19:50:45 +0000214
Bill Pemberton2d991a12012-11-19 13:21:41 -0500215static int hpwdt_init_one(struct pci_dev *dev,
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000216 const struct pci_device_id *ent)
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000217{
218 int retval;
219
220 /*
dann frazier36e3ff42010-07-27 17:50:57 -0600221 * First let's find out if we are on an iLO2+ server. We will
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000222 * not run on a legacy ASM box.
Thomas Mingarelliab4ba3c2008-07-15 19:40:41 +0000223 * So we only support the G5 ProLiant servers and higher.
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000224 */
Brian Boylstonfc113d52016-09-26 13:57:14 -0500225 if (dev->subsystem_vendor != PCI_VENDOR_ID_HP &&
226 dev->subsystem_vendor != PCI_VENDOR_ID_HP_3PAR) {
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000227 dev_warn(&dev->dev,
dann frazier36e3ff42010-07-27 17:50:57 -0600228 "This server does not have an iLO2+ ASIC.\n");
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000229 return -ENODEV;
230 }
231
Mingarelli, Thomas0821f202013-08-09 16:31:09 +0000232 /*
233 * Ignore all auxilary iLO devices with the following PCI ID
234 */
Brian Boylstonfc113d52016-09-26 13:57:14 -0500235 if (dev->subsystem_vendor == PCI_VENDOR_ID_HP &&
236 dev->subsystem_device == 0x1979)
Mingarelli, Thomas0821f202013-08-09 16:31:09 +0000237 return -ENODEV;
238
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000239 if (pci_enable_device(dev)) {
240 dev_warn(&dev->dev,
241 "Not possible to enable PCI Device: 0x%x:0x%x.\n",
242 ent->vendor, ent->device);
243 return -ENODEV;
244 }
245
246 pci_mem_addr = pci_iomap(dev, 1, 0x80);
247 if (!pci_mem_addr) {
248 dev_warn(&dev->dev,
dann frazier36e3ff42010-07-27 17:50:57 -0600249 "Unable to detect the iLO2+ server memory.\n");
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000250 retval = -ENOMEM;
251 goto error_pci_iomap;
252 }
Jerry Hoemann838534e2017-10-23 16:46:17 -0600253 hpwdt_nmistat = pci_mem_addr + 0x6e;
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000254 hpwdt_timer_reg = pci_mem_addr + 0x70;
255 hpwdt_timer_con = pci_mem_addr + 0x72;
256
Toshi Kani308b1352012-08-27 12:52:24 -0600257 /* Make sure that timer is disabled until /dev/watchdog is opened */
258 hpwdt_stop();
259
dann frazier2ec7ed62010-07-28 12:38:43 -0600260 /* Initialize NMI Decoding functionality */
261 retval = hpwdt_init_nmi_decoding(dev);
262 if (retval != 0)
263 goto error_init_nmi_decoding;
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000264
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700265 watchdog_set_nowayout(&hpwdt_dev, nowayout);
266 if (watchdog_init_timeout(&hpwdt_dev, soft_margin, NULL))
267 dev_warn(&dev->dev, "Invalid soft_margin: %d.\n", soft_margin);
268
269 hpwdt_dev.parent = &dev->dev;
270 retval = watchdog_register_device(&hpwdt_dev);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000271 if (retval < 0) {
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700272 dev_err(&dev->dev, "watchdog register failed: %d.\n", retval);
273 goto error_wd_register;
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000274 }
275
Mingarelli, Thomasca22e792015-12-14 20:22:09 +0000276 dev_info(&dev->dev, "HPE Watchdog Timer Driver: %s"
dann frazier2ec7ed62010-07-28 12:38:43 -0600277 ", timer margin: %d seconds (nowayout=%d).\n",
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700278 HPWDT_VERSION, hpwdt_dev.timeout, nowayout);
279
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000280 return 0;
281
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700282error_wd_register:
dann frazier2ec7ed62010-07-28 12:38:43 -0600283 hpwdt_exit_nmi_decoding();
284error_init_nmi_decoding:
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000285 pci_iounmap(dev, pci_mem_addr);
286error_pci_iomap:
287 pci_disable_device(dev);
288 return retval;
289}
290
Bill Pemberton4b12b892012-11-19 13:26:24 -0500291static void hpwdt_exit(struct pci_dev *dev)
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000292{
293 if (!nowayout)
294 hpwdt_stop();
295
Jerry Hoemannd0a40272018-02-25 20:22:22 -0700296 watchdog_unregister_device(&hpwdt_dev);
dann frazier2ec7ed62010-07-28 12:38:43 -0600297 hpwdt_exit_nmi_decoding();
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000298 pci_iounmap(dev, pci_mem_addr);
299 pci_disable_device(dev);
300}
301
302static struct pci_driver hpwdt_driver = {
303 .name = "hpwdt",
304 .id_table = hpwdt_devices,
305 .probe = hpwdt_init_one,
Bill Pemberton82268712012-11-19 13:21:12 -0500306 .remove = hpwdt_exit,
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000307};
308
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000309MODULE_AUTHOR("Tom Mingarelli");
Jerry Hoemann9a46fc42018-02-25 20:22:19 -0700310MODULE_DESCRIPTION("hpe watchdog driver");
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000311MODULE_LICENSE("GPL");
Thomas Mingarellid8100c32009-03-03 00:17:16 +0000312MODULE_VERSION(HPWDT_VERSION);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000313
314module_param(soft_margin, int, 0);
315MODULE_PARM_DESC(soft_margin, "Watchdog timeout in seconds");
316
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100317module_param(nowayout, bool, 0);
Thomas Mingarelli7f4da472007-12-04 17:41:54 +0000318MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
319 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
320
dann frazier86ded1f2010-07-27 17:51:02 -0600321#ifdef CONFIG_HPWDT_NMI_DECODING
dann frazier550d2992010-07-27 17:50:54 -0600322module_param(allow_kdump, int, 0);
323MODULE_PARM_DESC(allow_kdump, "Start a kernel dump after NMI occurs");
Jerry Hoemann2b3d89b2018-02-25 20:22:20 -0700324#endif /* CONFIG_HPWDT_NMI_DECODING */
Tom Mingarelli44df7532009-06-18 23:28:57 +0000325
Wim Van Sebroeck5ce9c372012-05-04 14:43:25 +0200326module_pci_driver(hpwdt_driver);