GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / char / ipmi / ipmi_watchdog.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ipmi_watchdog.c
4  *
5  * A watchdog timer based upon the IPMI interface.
6  *
7  * Author: MontaVista Software, Inc.
8  *         Corey Minyard <minyard@mvista.com>
9  *         source@mvista.com
10  *
11  * Copyright 2002 MontaVista Software Inc.
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/ipmi.h>
17 #include <linux/ipmi_smi.h>
18 #include <linux/mutex.h>
19 #include <linux/watchdog.h>
20 #include <linux/miscdevice.h>
21 #include <linux/init.h>
22 #include <linux/completion.h>
23 #include <linux/kdebug.h>
24 #include <linux/rwsem.h>
25 #include <linux/errno.h>
26 #include <linux/uaccess.h>
27 #include <linux/notifier.h>
28 #include <linux/nmi.h>
29 #include <linux/reboot.h>
30 #include <linux/wait.h>
31 #include <linux/poll.h>
32 #include <linux/string.h>
33 #include <linux/ctype.h>
34 #include <linux/delay.h>
35 #include <linux/atomic.h>
36 #include <linux/sched/signal.h>
37
38 #ifdef CONFIG_X86
39 /*
40  * This is ugly, but I've determined that x86 is the only architecture
41  * that can reasonably support the IPMI NMI watchdog timeout at this
42  * time.  If another architecture adds this capability somehow, it
43  * will have to be a somewhat different mechanism and I have no idea
44  * how it will work.  So in the unlikely event that another
45  * architecture supports this, we can figure out a good generic
46  * mechanism for it at that time.
47  */
48 #include <asm/kdebug.h>
49 #include <asm/nmi.h>
50 #define HAVE_DIE_NMI
51 #endif
52
53 #define PFX "IPMI Watchdog: "
54
55 /*
56  * The IPMI command/response information for the watchdog timer.
57  */
58
59 /* values for byte 1 of the set command, byte 2 of the get response. */
60 #define WDOG_DONT_LOG           (1 << 7)
61 #define WDOG_DONT_STOP_ON_SET   (1 << 6)
62 #define WDOG_SET_TIMER_USE(byte, use) \
63         byte = ((byte) & 0xf8) | ((use) & 0x7)
64 #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
65 #define WDOG_TIMER_USE_BIOS_FRB2        1
66 #define WDOG_TIMER_USE_BIOS_POST        2
67 #define WDOG_TIMER_USE_OS_LOAD          3
68 #define WDOG_TIMER_USE_SMS_OS           4
69 #define WDOG_TIMER_USE_OEM              5
70
71 /* values for byte 2 of the set command, byte 3 of the get response. */
72 #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
73         byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
74 #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
75 #define WDOG_PRETIMEOUT_NONE            0
76 #define WDOG_PRETIMEOUT_SMI             1
77 #define WDOG_PRETIMEOUT_NMI             2
78 #define WDOG_PRETIMEOUT_MSG_INT         3
79
80 /* Operations that can be performed on a pretimout. */
81 #define WDOG_PREOP_NONE         0
82 #define WDOG_PREOP_PANIC        1
83 /* Cause data to be available to read.  Doesn't work in NMI mode. */
84 #define WDOG_PREOP_GIVE_DATA    2
85
86 /* Actions to perform on a full timeout. */
87 #define WDOG_SET_TIMEOUT_ACT(byte, use) \
88         byte = ((byte) & 0xf8) | ((use) & 0x7)
89 #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
90 #define WDOG_TIMEOUT_NONE               0
91 #define WDOG_TIMEOUT_RESET              1
92 #define WDOG_TIMEOUT_POWER_DOWN         2
93 #define WDOG_TIMEOUT_POWER_CYCLE        3
94
95 /*
96  * Byte 3 of the get command, byte 4 of the get response is the
97  * pre-timeout in seconds.
98  */
99
100 /* Bits for setting byte 4 of the set command, byte 5 of the get response. */
101 #define WDOG_EXPIRE_CLEAR_BIOS_FRB2     (1 << 1)
102 #define WDOG_EXPIRE_CLEAR_BIOS_POST     (1 << 2)
103 #define WDOG_EXPIRE_CLEAR_OS_LOAD       (1 << 3)
104 #define WDOG_EXPIRE_CLEAR_SMS_OS        (1 << 4)
105 #define WDOG_EXPIRE_CLEAR_OEM           (1 << 5)
106
107 /*
108  * Setting/getting the watchdog timer value.  This is for bytes 5 and
109  * 6 (the timeout time) of the set command, and bytes 6 and 7 (the
110  * timeout time) and 8 and 9 (the current countdown value) of the
111  * response.  The timeout value is given in seconds (in the command it
112  * is 100ms intervals).
113  */
114 #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
115         (byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
116 #define WDOG_GET_TIMEOUT(byte1, byte2) \
117         (((byte1) | ((byte2) << 8)) / 10)
118
119 #define IPMI_WDOG_RESET_TIMER           0x22
120 #define IPMI_WDOG_SET_TIMER             0x24
121 #define IPMI_WDOG_GET_TIMER             0x25
122
123 #define IPMI_WDOG_TIMER_NOT_INIT_RESP   0x80
124
125 static DEFINE_MUTEX(ipmi_watchdog_mutex);
126 static bool nowayout = WATCHDOG_NOWAYOUT;
127
128 static struct ipmi_user *watchdog_user;
129 static int watchdog_ifnum;
130
131 /* Default the timeout to 10 seconds. */
132 static int timeout = 10;
133
134 /* The pre-timeout is disabled by default. */
135 static int pretimeout;
136
137 /* Default timeout to set on panic */
138 static int panic_wdt_timeout = 255;
139
140 /* Default action is to reset the board on a timeout. */
141 static unsigned char action_val = WDOG_TIMEOUT_RESET;
142
143 static char action[16] = "reset";
144
145 static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
146
147 static char preaction[16] = "pre_none";
148
149 static unsigned char preop_val = WDOG_PREOP_NONE;
150
151 static char preop[16] = "preop_none";
152 static DEFINE_SPINLOCK(ipmi_read_lock);
153 static char data_to_read;
154 static DECLARE_WAIT_QUEUE_HEAD(read_q);
155 static struct fasync_struct *fasync_q;
156 static atomic_t pretimeout_since_last_heartbeat;
157 static char expect_close;
158
159 static int ifnum_to_use = -1;
160
161 /* Parameters to ipmi_set_timeout */
162 #define IPMI_SET_TIMEOUT_NO_HB                  0
163 #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY        1
164 #define IPMI_SET_TIMEOUT_FORCE_HB               2
165
166 static int ipmi_set_timeout(int do_heartbeat);
167 static void ipmi_register_watchdog(int ipmi_intf);
168 static void ipmi_unregister_watchdog(int ipmi_intf);
169
170 /*
171  * If true, the driver will start running as soon as it is configured
172  * and ready.
173  */
174 static int start_now;
175
176 static int set_param_timeout(const char *val, const struct kernel_param *kp)
177 {
178         char *endp;
179         int  l;
180         int  rv = 0;
181
182         if (!val)
183                 return -EINVAL;
184         l = simple_strtoul(val, &endp, 0);
185         if (endp == val)
186                 return -EINVAL;
187
188         *((int *)kp->arg) = l;
189         if (watchdog_user)
190                 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
191
192         return rv;
193 }
194
195 static const struct kernel_param_ops param_ops_timeout = {
196         .set = set_param_timeout,
197         .get = param_get_int,
198 };
199 #define param_check_timeout param_check_int
200
201 typedef int (*action_fn)(const char *intval, char *outval);
202
203 static int action_op(const char *inval, char *outval);
204 static int preaction_op(const char *inval, char *outval);
205 static int preop_op(const char *inval, char *outval);
206 static void check_parms(void);
207
208 static int set_param_str(const char *val, const struct kernel_param *kp)
209 {
210         action_fn  fn = (action_fn) kp->arg;
211         int        rv = 0;
212         char       valcp[16];
213         char       *s;
214
215         strncpy(valcp, val, 15);
216         valcp[15] = '\0';
217
218         s = strstrip(valcp);
219
220         rv = fn(s, NULL);
221         if (rv)
222                 goto out;
223
224         check_parms();
225         if (watchdog_user)
226                 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
227
228  out:
229         return rv;
230 }
231
232 static int get_param_str(char *buffer, const struct kernel_param *kp)
233 {
234         action_fn fn = (action_fn) kp->arg;
235         int       rv;
236
237         rv = fn(NULL, buffer);
238         if (rv)
239                 return rv;
240         return strlen(buffer);
241 }
242
243
244 static int set_param_wdog_ifnum(const char *val, const struct kernel_param *kp)
245 {
246         int rv = param_set_int(val, kp);
247         if (rv)
248                 return rv;
249         if ((ifnum_to_use < 0) || (ifnum_to_use == watchdog_ifnum))
250                 return 0;
251
252         ipmi_unregister_watchdog(watchdog_ifnum);
253         ipmi_register_watchdog(ifnum_to_use);
254         return 0;
255 }
256
257 static const struct kernel_param_ops param_ops_wdog_ifnum = {
258         .set = set_param_wdog_ifnum,
259         .get = param_get_int,
260 };
261
262 #define param_check_wdog_ifnum param_check_int
263
264 static const struct kernel_param_ops param_ops_str = {
265         .set = set_param_str,
266         .get = get_param_str,
267 };
268
269 module_param(ifnum_to_use, wdog_ifnum, 0644);
270 MODULE_PARM_DESC(ifnum_to_use, "The interface number to use for the watchdog "
271                  "timer.  Setting to -1 defaults to the first registered "
272                  "interface");
273
274 module_param(timeout, timeout, 0644);
275 MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
276
277 module_param(pretimeout, timeout, 0644);
278 MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
279
280 module_param(panic_wdt_timeout, timeout, 0644);
281 MODULE_PARM_DESC(panic_wdt_timeout, "Timeout value on kernel panic in seconds.");
282
283 module_param_cb(action, &param_ops_str, action_op, 0644);
284 MODULE_PARM_DESC(action, "Timeout action. One of: "
285                  "reset, none, power_cycle, power_off.");
286
287 module_param_cb(preaction, &param_ops_str, preaction_op, 0644);
288 MODULE_PARM_DESC(preaction, "Pretimeout action.  One of: "
289                  "pre_none, pre_smi, pre_nmi, pre_int.");
290
291 module_param_cb(preop, &param_ops_str, preop_op, 0644);
292 MODULE_PARM_DESC(preop, "Pretimeout driver operation.  One of: "
293                  "preop_none, preop_panic, preop_give_data.");
294
295 module_param(start_now, int, 0444);
296 MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
297                  "soon as the driver is loaded.");
298
299 module_param(nowayout, bool, 0644);
300 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
301                  "(default=CONFIG_WATCHDOG_NOWAYOUT)");
302
303 /* Default state of the timer. */
304 static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
305
306 /* Is someone using the watchdog?  Only one user is allowed. */
307 static unsigned long ipmi_wdog_open;
308
309 /*
310  * If set to 1, the heartbeat command will set the state to reset and
311  * start the timer.  The timer doesn't normally run when the driver is
312  * first opened until the heartbeat is set the first time, this
313  * variable is used to accomplish this.
314  */
315 static int ipmi_start_timer_on_heartbeat;
316
317 /* IPMI version of the BMC. */
318 static unsigned char ipmi_version_major;
319 static unsigned char ipmi_version_minor;
320
321 /* If a pretimeout occurs, this is used to allow only one panic to happen. */
322 static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
323
324 #ifdef HAVE_DIE_NMI
325 static int testing_nmi;
326 static int nmi_handler_registered;
327 #endif
328
329 static int __ipmi_heartbeat(void);
330
331 /*
332  * We use a mutex to make sure that only one thing can send a set a
333  * message at one time.  The mutex is claimed when a message is sent
334  * and freed when both the send and receive messages are free.
335  */
336 static atomic_t msg_tofree = ATOMIC_INIT(0);
337 static DECLARE_COMPLETION(msg_wait);
338 static void msg_free_smi(struct ipmi_smi_msg *msg)
339 {
340         if (atomic_dec_and_test(&msg_tofree))
341                 complete(&msg_wait);
342 }
343 static void msg_free_recv(struct ipmi_recv_msg *msg)
344 {
345         if (atomic_dec_and_test(&msg_tofree))
346                 complete(&msg_wait);
347 }
348 static struct ipmi_smi_msg smi_msg = {
349         .done = msg_free_smi
350 };
351 static struct ipmi_recv_msg recv_msg = {
352         .done = msg_free_recv
353 };
354
355 static int __ipmi_set_timeout(struct ipmi_smi_msg  *smi_msg,
356                               struct ipmi_recv_msg *recv_msg,
357                               int                  *send_heartbeat_now)
358 {
359         struct kernel_ipmi_msg            msg;
360         unsigned char                     data[6];
361         int                               rv;
362         struct ipmi_system_interface_addr addr;
363         int                               hbnow = 0;
364
365
366         data[0] = 0;
367         WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
368
369         if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
370                 if ((ipmi_version_major > 1) ||
371                     ((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) {
372                         /* This is an IPMI 1.5-only feature. */
373                         data[0] |= WDOG_DONT_STOP_ON_SET;
374                 } else {
375                         /*
376                          * In ipmi 1.0, setting the timer stops the watchdog, we
377                          * need to start it back up again.
378                          */
379                         hbnow = 1;
380                 }
381         }
382
383         data[1] = 0;
384         WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
385         if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
386             WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
387             data[2] = pretimeout;
388         } else {
389             WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
390             data[2] = 0; /* No pretimeout. */
391         }
392         data[3] = 0;
393         WDOG_SET_TIMEOUT(data[4], data[5], timeout);
394
395         addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
396         addr.channel = IPMI_BMC_CHANNEL;
397         addr.lun = 0;
398
399         msg.netfn = 0x06;
400         msg.cmd = IPMI_WDOG_SET_TIMER;
401         msg.data = data;
402         msg.data_len = sizeof(data);
403         rv = ipmi_request_supply_msgs(watchdog_user,
404                                       (struct ipmi_addr *) &addr,
405                                       0,
406                                       &msg,
407                                       NULL,
408                                       smi_msg,
409                                       recv_msg,
410                                       1);
411         if (rv)
412                 pr_warn(PFX "set timeout error: %d\n", rv);
413         else if (send_heartbeat_now)
414                 *send_heartbeat_now = hbnow;
415
416         return rv;
417 }
418
419 static int _ipmi_set_timeout(int do_heartbeat)
420 {
421         int send_heartbeat_now;
422         int rv;
423
424         if (!watchdog_user)
425                 return -ENODEV;
426
427         atomic_set(&msg_tofree, 2);
428
429         rv = __ipmi_set_timeout(&smi_msg,
430                                 &recv_msg,
431                                 &send_heartbeat_now);
432         if (rv)
433                 return rv;
434
435         wait_for_completion(&msg_wait);
436
437         if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
438                 || ((send_heartbeat_now)
439                     && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
440                 rv = __ipmi_heartbeat();
441
442         return rv;
443 }
444
445 static int ipmi_set_timeout(int do_heartbeat)
446 {
447         int rv;
448
449         mutex_lock(&ipmi_watchdog_mutex);
450         rv = _ipmi_set_timeout(do_heartbeat);
451         mutex_unlock(&ipmi_watchdog_mutex);
452
453         return rv;
454 }
455
456 static atomic_t panic_done_count = ATOMIC_INIT(0);
457
458 static void panic_smi_free(struct ipmi_smi_msg *msg)
459 {
460         atomic_dec(&panic_done_count);
461 }
462 static void panic_recv_free(struct ipmi_recv_msg *msg)
463 {
464         atomic_dec(&panic_done_count);
465 }
466
467 static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = {
468         .done = panic_smi_free
469 };
470 static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = {
471         .done = panic_recv_free
472 };
473
474 static void panic_halt_ipmi_heartbeat(void)
475 {
476         struct kernel_ipmi_msg             msg;
477         struct ipmi_system_interface_addr addr;
478         int rv;
479
480         /*
481          * Don't reset the timer if we have the timer turned off, that
482          * re-enables the watchdog.
483          */
484         if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
485                 return;
486
487         addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
488         addr.channel = IPMI_BMC_CHANNEL;
489         addr.lun = 0;
490
491         msg.netfn = 0x06;
492         msg.cmd = IPMI_WDOG_RESET_TIMER;
493         msg.data = NULL;
494         msg.data_len = 0;
495         atomic_add(1, &panic_done_count);
496         rv = ipmi_request_supply_msgs(watchdog_user,
497                                       (struct ipmi_addr *) &addr,
498                                       0,
499                                       &msg,
500                                       NULL,
501                                       &panic_halt_heartbeat_smi_msg,
502                                       &panic_halt_heartbeat_recv_msg,
503                                       1);
504         if (rv)
505                 atomic_sub(1, &panic_done_count);
506 }
507
508 static struct ipmi_smi_msg panic_halt_smi_msg = {
509         .done = panic_smi_free
510 };
511 static struct ipmi_recv_msg panic_halt_recv_msg = {
512         .done = panic_recv_free
513 };
514
515 /*
516  * Special call, doesn't claim any locks.  This is only to be called
517  * at panic or halt time, in run-to-completion mode, when the caller
518  * is the only CPU and the only thing that will be going is these IPMI
519  * calls.
520  */
521 static void panic_halt_ipmi_set_timeout(void)
522 {
523         int send_heartbeat_now;
524         int rv;
525
526         /* Wait for the messages to be free. */
527         while (atomic_read(&panic_done_count) != 0)
528                 ipmi_poll_interface(watchdog_user);
529         atomic_add(1, &panic_done_count);
530         rv = __ipmi_set_timeout(&panic_halt_smi_msg,
531                                 &panic_halt_recv_msg,
532                                 &send_heartbeat_now);
533         if (rv) {
534                 atomic_sub(1, &panic_done_count);
535                 pr_warn(PFX "Unable to extend the watchdog timeout.");
536         } else {
537                 if (send_heartbeat_now)
538                         panic_halt_ipmi_heartbeat();
539         }
540         while (atomic_read(&panic_done_count) != 0)
541                 ipmi_poll_interface(watchdog_user);
542 }
543
544 static int __ipmi_heartbeat(void)
545 {
546         struct kernel_ipmi_msg msg;
547         int rv;
548         struct ipmi_system_interface_addr addr;
549         int timeout_retries = 0;
550
551 restart:
552         /*
553          * Don't reset the timer if we have the timer turned off, that
554          * re-enables the watchdog.
555          */
556         if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
557                 return 0;
558
559         atomic_set(&msg_tofree, 2);
560
561         addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
562         addr.channel = IPMI_BMC_CHANNEL;
563         addr.lun = 0;
564
565         msg.netfn = 0x06;
566         msg.cmd = IPMI_WDOG_RESET_TIMER;
567         msg.data = NULL;
568         msg.data_len = 0;
569         rv = ipmi_request_supply_msgs(watchdog_user,
570                                       (struct ipmi_addr *) &addr,
571                                       0,
572                                       &msg,
573                                       NULL,
574                                       &smi_msg,
575                                       &recv_msg,
576                                       1);
577         if (rv) {
578                 pr_warn(PFX "heartbeat send failure: %d\n", rv);
579                 return rv;
580         }
581
582         /* Wait for the heartbeat to be sent. */
583         wait_for_completion(&msg_wait);
584
585         if (recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)  {
586                 timeout_retries++;
587                 if (timeout_retries > 3) {
588                         pr_err(PFX ": Unable to restore the IPMI watchdog's settings, giving up.\n");
589                         rv = -EIO;
590                         goto out;
591                 }
592
593                 /*
594                  * The timer was not initialized, that means the BMC was
595                  * probably reset and lost the watchdog information.  Attempt
596                  * to restore the timer's info.  Note that we still hold
597                  * the heartbeat lock, to keep a heartbeat from happening
598                  * in this process, so must say no heartbeat to avoid a
599                  * deadlock on this mutex
600                  */
601                 rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
602                 if (rv) {
603                         pr_err(PFX ": Unable to send the command to set the watchdog's settings, giving up.\n");
604                         goto out;
605                 }
606
607                 /* Might need a heartbeat send, go ahead and do it. */
608                 goto restart;
609         } else if (recv_msg.msg.data[0] != 0) {
610                 /*
611                  * Got an error in the heartbeat response.  It was already
612                  * reported in ipmi_wdog_msg_handler, but we should return
613                  * an error here.
614                  */
615                 rv = -EINVAL;
616         }
617
618 out:
619         return rv;
620 }
621
622 static int _ipmi_heartbeat(void)
623 {
624         int rv;
625
626         if (!watchdog_user)
627                 return -ENODEV;
628
629         if (ipmi_start_timer_on_heartbeat) {
630                 ipmi_start_timer_on_heartbeat = 0;
631                 ipmi_watchdog_state = action_val;
632                 rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
633         } else if (atomic_cmpxchg(&pretimeout_since_last_heartbeat, 1, 0)) {
634                 /*
635                  * A pretimeout occurred, make sure we set the timeout.
636                  * We don't want to set the action, though, we want to
637                  * leave that alone (thus it can't be combined with the
638                  * above operation.
639                  */
640                 rv = _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
641         } else {
642                 rv = __ipmi_heartbeat();
643         }
644
645         return rv;
646 }
647
648 static int ipmi_heartbeat(void)
649 {
650         int rv;
651
652         mutex_lock(&ipmi_watchdog_mutex);
653         rv = _ipmi_heartbeat();
654         mutex_unlock(&ipmi_watchdog_mutex);
655
656         return rv;
657 }
658
659 static struct watchdog_info ident = {
660         .options        = 0,    /* WDIOF_SETTIMEOUT, */
661         .firmware_version = 1,
662         .identity       = "IPMI"
663 };
664
665 static int ipmi_ioctl(struct file *file,
666                       unsigned int cmd, unsigned long arg)
667 {
668         void __user *argp = (void __user *)arg;
669         int i;
670         int val;
671
672         switch (cmd) {
673         case WDIOC_GETSUPPORT:
674                 i = copy_to_user(argp, &ident, sizeof(ident));
675                 return i ? -EFAULT : 0;
676
677         case WDIOC_SETTIMEOUT:
678                 i = copy_from_user(&val, argp, sizeof(int));
679                 if (i)
680                         return -EFAULT;
681                 timeout = val;
682                 return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
683
684         case WDIOC_GETTIMEOUT:
685                 i = copy_to_user(argp, &timeout, sizeof(timeout));
686                 if (i)
687                         return -EFAULT;
688                 return 0;
689
690         case WDIOC_SETPRETIMEOUT:
691                 i = copy_from_user(&val, argp, sizeof(int));
692                 if (i)
693                         return -EFAULT;
694                 pretimeout = val;
695                 return _ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
696
697         case WDIOC_GETPRETIMEOUT:
698                 i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
699                 if (i)
700                         return -EFAULT;
701                 return 0;
702
703         case WDIOC_KEEPALIVE:
704                 return _ipmi_heartbeat();
705
706         case WDIOC_SETOPTIONS:
707                 i = copy_from_user(&val, argp, sizeof(int));
708                 if (i)
709                         return -EFAULT;
710                 if (val & WDIOS_DISABLECARD) {
711                         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
712                         _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
713                         ipmi_start_timer_on_heartbeat = 0;
714                 }
715
716                 if (val & WDIOS_ENABLECARD) {
717                         ipmi_watchdog_state = action_val;
718                         _ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
719                 }
720                 return 0;
721
722         case WDIOC_GETSTATUS:
723                 val = 0;
724                 i = copy_to_user(argp, &val, sizeof(val));
725                 if (i)
726                         return -EFAULT;
727                 return 0;
728
729         default:
730                 return -ENOIOCTLCMD;
731         }
732 }
733
734 static long ipmi_unlocked_ioctl(struct file *file,
735                                 unsigned int cmd,
736                                 unsigned long arg)
737 {
738         int ret;
739
740         mutex_lock(&ipmi_watchdog_mutex);
741         ret = ipmi_ioctl(file, cmd, arg);
742         mutex_unlock(&ipmi_watchdog_mutex);
743
744         return ret;
745 }
746
747 static ssize_t ipmi_write(struct file *file,
748                           const char  __user *buf,
749                           size_t      len,
750                           loff_t      *ppos)
751 {
752         int rv;
753
754         if (len) {
755                 if (!nowayout) {
756                         size_t i;
757
758                         /* In case it was set long ago */
759                         expect_close = 0;
760
761                         for (i = 0; i != len; i++) {
762                                 char c;
763
764                                 if (get_user(c, buf + i))
765                                         return -EFAULT;
766                                 if (c == 'V')
767                                         expect_close = 42;
768                         }
769                 }
770                 rv = ipmi_heartbeat();
771                 if (rv)
772                         return rv;
773         }
774         return len;
775 }
776
777 static ssize_t ipmi_read(struct file *file,
778                          char        __user *buf,
779                          size_t      count,
780                          loff_t      *ppos)
781 {
782         int          rv = 0;
783         wait_queue_entry_t wait;
784
785         if (count <= 0)
786                 return 0;
787
788         /*
789          * Reading returns if the pretimeout has gone off, and it only does
790          * it once per pretimeout.
791          */
792         spin_lock_irq(&ipmi_read_lock);
793         if (!data_to_read) {
794                 if (file->f_flags & O_NONBLOCK) {
795                         rv = -EAGAIN;
796                         goto out;
797                 }
798
799                 init_waitqueue_entry(&wait, current);
800                 add_wait_queue(&read_q, &wait);
801                 while (!data_to_read) {
802                         set_current_state(TASK_INTERRUPTIBLE);
803                         spin_unlock_irq(&ipmi_read_lock);
804                         schedule();
805                         spin_lock_irq(&ipmi_read_lock);
806                 }
807                 remove_wait_queue(&read_q, &wait);
808
809                 if (signal_pending(current)) {
810                         rv = -ERESTARTSYS;
811                         goto out;
812                 }
813         }
814         data_to_read = 0;
815
816  out:
817         spin_unlock_irq(&ipmi_read_lock);
818
819         if (rv == 0) {
820                 if (copy_to_user(buf, &data_to_read, 1))
821                         rv = -EFAULT;
822                 else
823                         rv = 1;
824         }
825
826         return rv;
827 }
828
829 static int ipmi_open(struct inode *ino, struct file *filep)
830 {
831         switch (iminor(ino)) {
832         case WATCHDOG_MINOR:
833                 if (test_and_set_bit(0, &ipmi_wdog_open))
834                         return -EBUSY;
835
836
837                 /*
838                  * Don't start the timer now, let it start on the
839                  * first heartbeat.
840                  */
841                 ipmi_start_timer_on_heartbeat = 1;
842                 return nonseekable_open(ino, filep);
843
844         default:
845                 return (-ENODEV);
846         }
847 }
848
849 static __poll_t ipmi_poll(struct file *file, poll_table *wait)
850 {
851         __poll_t mask = 0;
852
853         poll_wait(file, &read_q, wait);
854
855         spin_lock_irq(&ipmi_read_lock);
856         if (data_to_read)
857                 mask |= (EPOLLIN | EPOLLRDNORM);
858         spin_unlock_irq(&ipmi_read_lock);
859
860         return mask;
861 }
862
863 static int ipmi_fasync(int fd, struct file *file, int on)
864 {
865         int result;
866
867         result = fasync_helper(fd, file, on, &fasync_q);
868
869         return (result);
870 }
871
872 static int ipmi_close(struct inode *ino, struct file *filep)
873 {
874         if (iminor(ino) == WATCHDOG_MINOR) {
875                 if (expect_close == 42) {
876                         mutex_lock(&ipmi_watchdog_mutex);
877                         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
878                         _ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
879                         mutex_unlock(&ipmi_watchdog_mutex);
880                 } else {
881                         pr_crit(PFX
882                                 "Unexpected close, not stopping watchdog!\n");
883                         ipmi_heartbeat();
884                 }
885                 clear_bit(0, &ipmi_wdog_open);
886         }
887
888         expect_close = 0;
889
890         return 0;
891 }
892
893 static const struct file_operations ipmi_wdog_fops = {
894         .owner   = THIS_MODULE,
895         .read    = ipmi_read,
896         .poll    = ipmi_poll,
897         .write   = ipmi_write,
898         .unlocked_ioctl = ipmi_unlocked_ioctl,
899         .open    = ipmi_open,
900         .release = ipmi_close,
901         .fasync  = ipmi_fasync,
902         .llseek  = no_llseek,
903 };
904
905 static struct miscdevice ipmi_wdog_miscdev = {
906         .minor          = WATCHDOG_MINOR,
907         .name           = "watchdog",
908         .fops           = &ipmi_wdog_fops
909 };
910
911 static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
912                                   void                 *handler_data)
913 {
914         if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
915                         msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
916                 pr_info(PFX "response: The IPMI controller appears to have been reset, will attempt to reinitialize the watchdog timer\n");
917         else if (msg->msg.data[0] != 0)
918                 pr_err(PFX "response: Error %x on cmd %x\n",
919                        msg->msg.data[0],
920                        msg->msg.cmd);
921
922         ipmi_free_recv_msg(msg);
923 }
924
925 static void ipmi_wdog_pretimeout_handler(void *handler_data)
926 {
927         if (preaction_val != WDOG_PRETIMEOUT_NONE) {
928                 if (preop_val == WDOG_PREOP_PANIC) {
929                         if (atomic_inc_and_test(&preop_panic_excl))
930                                 panic("Watchdog pre-timeout");
931                 } else if (preop_val == WDOG_PREOP_GIVE_DATA) {
932                         unsigned long flags;
933
934                         spin_lock_irqsave(&ipmi_read_lock, flags);
935                         data_to_read = 1;
936                         wake_up_interruptible(&read_q);
937                         kill_fasync(&fasync_q, SIGIO, POLL_IN);
938                         spin_unlock_irqrestore(&ipmi_read_lock, flags);
939                 }
940         }
941
942         /*
943          * On some machines, the heartbeat will give an error and not
944          * work unless we re-enable the timer.  So do so.
945          */
946         atomic_set(&pretimeout_since_last_heartbeat, 1);
947 }
948
949 static void ipmi_wdog_panic_handler(void *user_data)
950 {
951         static int panic_event_handled;
952
953         /*
954          * On a panic, if we have a panic timeout, make sure to extend
955          * the watchdog timer to a reasonable value to complete the
956          * panic, if the watchdog timer is running.  Plus the
957          * pretimeout is meaningless at panic time.
958          */
959         if (watchdog_user && !panic_event_handled &&
960             ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
961                 /* Make sure we do this only once. */
962                 panic_event_handled = 1;
963
964                 timeout = panic_wdt_timeout;
965                 pretimeout = 0;
966                 panic_halt_ipmi_set_timeout();
967         }
968 }
969
970 static const struct ipmi_user_hndl ipmi_hndlrs = {
971         .ipmi_recv_hndl           = ipmi_wdog_msg_handler,
972         .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler,
973         .ipmi_panic_handler       = ipmi_wdog_panic_handler
974 };
975
976 static void ipmi_register_watchdog(int ipmi_intf)
977 {
978         int rv = -EBUSY;
979
980         if (watchdog_user)
981                 goto out;
982
983         if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
984                 goto out;
985
986         watchdog_ifnum = ipmi_intf;
987
988         rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
989         if (rv < 0) {
990                 pr_crit(PFX "Unable to register with ipmi\n");
991                 goto out;
992         }
993
994         rv = ipmi_get_version(watchdog_user,
995                               &ipmi_version_major,
996                               &ipmi_version_minor);
997         if (rv) {
998                 pr_warn(PFX "Unable to get IPMI version, assuming 1.0\n");
999                 ipmi_version_major = 1;
1000                 ipmi_version_minor = 0;
1001         }
1002
1003         rv = misc_register(&ipmi_wdog_miscdev);
1004         if (rv < 0) {
1005                 ipmi_destroy_user(watchdog_user);
1006                 watchdog_user = NULL;
1007                 pr_crit(PFX "Unable to register misc device\n");
1008         }
1009
1010 #ifdef HAVE_DIE_NMI
1011         if (nmi_handler_registered) {
1012                 int old_pretimeout = pretimeout;
1013                 int old_timeout = timeout;
1014                 int old_preop_val = preop_val;
1015
1016                 /*
1017                  * Set the pretimeout to go off in a second and give
1018                  * ourselves plenty of time to stop the timer.
1019                  */
1020                 ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1021                 preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */
1022                 pretimeout = 99;
1023                 timeout = 100;
1024
1025                 testing_nmi = 1;
1026
1027                 rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
1028                 if (rv) {
1029                         pr_warn(PFX "Error starting timer to test NMI: 0x%x.  The NMI pretimeout will likely not work\n",
1030                                 rv);
1031                         rv = 0;
1032                         goto out_restore;
1033                 }
1034
1035                 msleep(1500);
1036
1037                 if (testing_nmi != 2) {
1038                         pr_warn(PFX "IPMI NMI didn't seem to occur.  The NMI pretimeout will likely not work\n");
1039                 }
1040  out_restore:
1041                 testing_nmi = 0;
1042                 preop_val = old_preop_val;
1043                 pretimeout = old_pretimeout;
1044                 timeout = old_timeout;
1045         }
1046 #endif
1047
1048  out:
1049         if ((start_now) && (rv == 0)) {
1050                 /* Run from startup, so start the timer now. */
1051                 start_now = 0; /* Disable this function after first startup. */
1052                 ipmi_watchdog_state = action_val;
1053                 ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
1054                 pr_info(PFX "Starting now!\n");
1055         } else {
1056                 /* Stop the timer now. */
1057                 ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1058                 ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1059         }
1060 }
1061
1062 static void ipmi_unregister_watchdog(int ipmi_intf)
1063 {
1064         int rv;
1065         struct ipmi_user *loc_user = watchdog_user;
1066
1067         if (!loc_user)
1068                 return;
1069
1070         if (watchdog_ifnum != ipmi_intf)
1071                 return;
1072
1073         /* Make sure no one can call us any more. */
1074         misc_deregister(&ipmi_wdog_miscdev);
1075
1076         watchdog_user = NULL;
1077
1078         /*
1079          * Wait to make sure the message makes it out.  The lower layer has
1080          * pointers to our buffers, we want to make sure they are done before
1081          * we release our memory.
1082          */
1083         while (atomic_read(&msg_tofree))
1084                 msg_free_smi(NULL);
1085
1086         mutex_lock(&ipmi_watchdog_mutex);
1087
1088         /* Disconnect from IPMI. */
1089         rv = ipmi_destroy_user(loc_user);
1090         if (rv)
1091                 pr_warn(PFX "error unlinking from IPMI: %d\n",  rv);
1092
1093         /* If it comes back, restart it properly. */
1094         ipmi_start_timer_on_heartbeat = 1;
1095
1096         mutex_unlock(&ipmi_watchdog_mutex);
1097 }
1098
1099 #ifdef HAVE_DIE_NMI
1100 static int
1101 ipmi_nmi(unsigned int val, struct pt_regs *regs)
1102 {
1103         /*
1104          * If we get here, it's an NMI that's not a memory or I/O
1105          * error.  We can't truly tell if it's from IPMI or not
1106          * without sending a message, and sending a message is almost
1107          * impossible because of locking.
1108          */
1109
1110         if (testing_nmi) {
1111                 testing_nmi = 2;
1112                 return NMI_HANDLED;
1113         }
1114
1115         /* If we are not expecting a timeout, ignore it. */
1116         if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
1117                 return NMI_DONE;
1118
1119         if (preaction_val != WDOG_PRETIMEOUT_NMI)
1120                 return NMI_DONE;
1121
1122         /*
1123          * If no one else handled the NMI, we assume it was the IPMI
1124          * watchdog.
1125          */
1126         if (preop_val == WDOG_PREOP_PANIC) {
1127                 /* On some machines, the heartbeat will give
1128                    an error and not work unless we re-enable
1129                    the timer.   So do so. */
1130                 atomic_set(&pretimeout_since_last_heartbeat, 1);
1131                 if (atomic_inc_and_test(&preop_panic_excl))
1132                         nmi_panic(regs, PFX "pre-timeout");
1133         }
1134
1135         return NMI_HANDLED;
1136 }
1137 #endif
1138
1139 static int wdog_reboot_handler(struct notifier_block *this,
1140                                unsigned long         code,
1141                                void                  *unused)
1142 {
1143         static int reboot_event_handled;
1144
1145         if ((watchdog_user) && (!reboot_event_handled)) {
1146                 /* Make sure we only do this once. */
1147                 reboot_event_handled = 1;
1148
1149                 if (code == SYS_POWER_OFF || code == SYS_HALT) {
1150                         /* Disable the WDT if we are shutting down. */
1151                         ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
1152                         ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1153                 } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
1154                         /* Set a long timer to let the reboot happen or
1155                            reset if it hangs, but only if the watchdog
1156                            timer was already running. */
1157                         if (timeout < 120)
1158                                 timeout = 120;
1159                         pretimeout = 0;
1160                         ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
1161                         ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
1162                 }
1163         }
1164         return NOTIFY_OK;
1165 }
1166
1167 static struct notifier_block wdog_reboot_notifier = {
1168         .notifier_call  = wdog_reboot_handler,
1169         .next           = NULL,
1170         .priority       = 0
1171 };
1172
1173 static void ipmi_new_smi(int if_num, struct device *device)
1174 {
1175         ipmi_register_watchdog(if_num);
1176 }
1177
1178 static void ipmi_smi_gone(int if_num)
1179 {
1180         ipmi_unregister_watchdog(if_num);
1181 }
1182
1183 static struct ipmi_smi_watcher smi_watcher = {
1184         .owner    = THIS_MODULE,
1185         .new_smi  = ipmi_new_smi,
1186         .smi_gone = ipmi_smi_gone
1187 };
1188
1189 static int action_op(const char *inval, char *outval)
1190 {
1191         if (outval)
1192                 strcpy(outval, action);
1193
1194         if (!inval)
1195                 return 0;
1196
1197         if (strcmp(inval, "reset") == 0)
1198                 action_val = WDOG_TIMEOUT_RESET;
1199         else if (strcmp(inval, "none") == 0)
1200                 action_val = WDOG_TIMEOUT_NONE;
1201         else if (strcmp(inval, "power_cycle") == 0)
1202                 action_val = WDOG_TIMEOUT_POWER_CYCLE;
1203         else if (strcmp(inval, "power_off") == 0)
1204                 action_val = WDOG_TIMEOUT_POWER_DOWN;
1205         else
1206                 return -EINVAL;
1207         strcpy(action, inval);
1208         return 0;
1209 }
1210
1211 static int preaction_op(const char *inval, char *outval)
1212 {
1213         if (outval)
1214                 strcpy(outval, preaction);
1215
1216         if (!inval)
1217                 return 0;
1218
1219         if (strcmp(inval, "pre_none") == 0)
1220                 preaction_val = WDOG_PRETIMEOUT_NONE;
1221         else if (strcmp(inval, "pre_smi") == 0)
1222                 preaction_val = WDOG_PRETIMEOUT_SMI;
1223 #ifdef HAVE_DIE_NMI
1224         else if (strcmp(inval, "pre_nmi") == 0)
1225                 preaction_val = WDOG_PRETIMEOUT_NMI;
1226 #endif
1227         else if (strcmp(inval, "pre_int") == 0)
1228                 preaction_val = WDOG_PRETIMEOUT_MSG_INT;
1229         else
1230                 return -EINVAL;
1231         strcpy(preaction, inval);
1232         return 0;
1233 }
1234
1235 static int preop_op(const char *inval, char *outval)
1236 {
1237         if (outval)
1238                 strcpy(outval, preop);
1239
1240         if (!inval)
1241                 return 0;
1242
1243         if (strcmp(inval, "preop_none") == 0)
1244                 preop_val = WDOG_PREOP_NONE;
1245         else if (strcmp(inval, "preop_panic") == 0)
1246                 preop_val = WDOG_PREOP_PANIC;
1247         else if (strcmp(inval, "preop_give_data") == 0)
1248                 preop_val = WDOG_PREOP_GIVE_DATA;
1249         else
1250                 return -EINVAL;
1251         strcpy(preop, inval);
1252         return 0;
1253 }
1254
1255 static void check_parms(void)
1256 {
1257 #ifdef HAVE_DIE_NMI
1258         int do_nmi = 0;
1259         int rv;
1260
1261         if (preaction_val == WDOG_PRETIMEOUT_NMI) {
1262                 do_nmi = 1;
1263                 if (preop_val == WDOG_PREOP_GIVE_DATA) {
1264                         pr_warn(PFX "Pretimeout op is to give data but NMI pretimeout is enabled, setting pretimeout op to none\n");
1265                         preop_op("preop_none", NULL);
1266                         do_nmi = 0;
1267                 }
1268         }
1269         if (do_nmi && !nmi_handler_registered) {
1270                 rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
1271                                                 "ipmi");
1272                 if (rv) {
1273                         pr_warn(PFX "Can't register nmi handler\n");
1274                         return;
1275                 } else
1276                         nmi_handler_registered = 1;
1277         } else if (!do_nmi && nmi_handler_registered) {
1278                 unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1279                 nmi_handler_registered = 0;
1280         }
1281 #endif
1282 }
1283
1284 static int __init ipmi_wdog_init(void)
1285 {
1286         int rv;
1287
1288         if (action_op(action, NULL)) {
1289                 action_op("reset", NULL);
1290                 pr_info(PFX "Unknown action '%s', defaulting to reset\n",
1291                         action);
1292         }
1293
1294         if (preaction_op(preaction, NULL)) {
1295                 preaction_op("pre_none", NULL);
1296                 pr_info(PFX "Unknown preaction '%s', defaulting to none\n",
1297                         preaction);
1298         }
1299
1300         if (preop_op(preop, NULL)) {
1301                 preop_op("preop_none", NULL);
1302                 pr_info(PFX "Unknown preop '%s', defaulting to none\n", preop);
1303         }
1304
1305         check_parms();
1306
1307         register_reboot_notifier(&wdog_reboot_notifier);
1308
1309         rv = ipmi_smi_watcher_register(&smi_watcher);
1310         if (rv) {
1311 #ifdef HAVE_DIE_NMI
1312                 if (nmi_handler_registered)
1313                         unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1314 #endif
1315                 unregister_reboot_notifier(&wdog_reboot_notifier);
1316                 pr_warn(PFX "can't register smi watcher\n");
1317                 return rv;
1318         }
1319
1320         pr_info(PFX "driver initialized\n");
1321
1322         return 0;
1323 }
1324
1325 static void __exit ipmi_wdog_exit(void)
1326 {
1327         ipmi_smi_watcher_unregister(&smi_watcher);
1328         ipmi_unregister_watchdog(watchdog_ifnum);
1329
1330 #ifdef HAVE_DIE_NMI
1331         if (nmi_handler_registered)
1332                 unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
1333 #endif
1334
1335         unregister_reboot_notifier(&wdog_reboot_notifier);
1336 }
1337 module_exit(ipmi_wdog_exit);
1338 module_init(ipmi_wdog_init);
1339 MODULE_LICENSE("GPL");
1340 MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
1341 MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");