ipmi_watchdog.c 32 KB

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