ipmi_watchdog.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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 ipmi_user_t 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 char 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. /* If shutting down via IPMI, we ignore the heartbeat. */
  247. static int ipmi_ignore_heartbeat;
  248. /* Is someone using the watchdog? Only one user is allowed. */
  249. static unsigned long ipmi_wdog_open;
  250. /*
  251. * If set to 1, the heartbeat command will set the state to reset and
  252. * start the timer. The timer doesn't normally run when the driver is
  253. * first opened until the heartbeat is set the first time, this
  254. * variable is used to accomplish this.
  255. */
  256. static int ipmi_start_timer_on_heartbeat;
  257. /* IPMI version of the BMC. */
  258. static unsigned char ipmi_version_major;
  259. static unsigned char ipmi_version_minor;
  260. /* If a pretimeout occurs, this is used to allow only one panic to happen. */
  261. static atomic_t preop_panic_excl = ATOMIC_INIT(-1);
  262. #ifdef HAVE_DIE_NMI
  263. static int testing_nmi;
  264. static int nmi_handler_registered;
  265. #endif
  266. static int ipmi_heartbeat(void);
  267. /*
  268. * We use a mutex to make sure that only one thing can send a set
  269. * timeout at one time, because we only have one copy of the data.
  270. * The mutex is claimed when the set_timeout is sent and freed
  271. * when both messages are free.
  272. */
  273. static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
  274. static DEFINE_MUTEX(set_timeout_lock);
  275. static DECLARE_COMPLETION(set_timeout_wait);
  276. static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
  277. {
  278. if (atomic_dec_and_test(&set_timeout_tofree))
  279. complete(&set_timeout_wait);
  280. }
  281. static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
  282. {
  283. if (atomic_dec_and_test(&set_timeout_tofree))
  284. complete(&set_timeout_wait);
  285. }
  286. static struct ipmi_smi_msg set_timeout_smi_msg = {
  287. .done = set_timeout_free_smi
  288. };
  289. static struct ipmi_recv_msg set_timeout_recv_msg = {
  290. .done = set_timeout_free_recv
  291. };
  292. static int i_ipmi_set_timeout(struct ipmi_smi_msg *smi_msg,
  293. struct ipmi_recv_msg *recv_msg,
  294. int *send_heartbeat_now)
  295. {
  296. struct kernel_ipmi_msg msg;
  297. unsigned char data[6];
  298. int rv;
  299. struct ipmi_system_interface_addr addr;
  300. int hbnow = 0;
  301. /* These can be cleared as we are setting the timeout. */
  302. pretimeout_since_last_heartbeat = 0;
  303. data[0] = 0;
  304. WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
  305. if ((ipmi_version_major > 1)
  306. || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5))) {
  307. /* This is an IPMI 1.5-only feature. */
  308. data[0] |= WDOG_DONT_STOP_ON_SET;
  309. } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
  310. /*
  311. * In ipmi 1.0, setting the timer stops the watchdog, we
  312. * need to start it back up again.
  313. */
  314. hbnow = 1;
  315. }
  316. data[1] = 0;
  317. WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
  318. if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
  319. WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
  320. data[2] = pretimeout;
  321. } else {
  322. WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
  323. data[2] = 0; /* No pretimeout. */
  324. }
  325. data[3] = 0;
  326. WDOG_SET_TIMEOUT(data[4], data[5], timeout);
  327. addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  328. addr.channel = IPMI_BMC_CHANNEL;
  329. addr.lun = 0;
  330. msg.netfn = 0x06;
  331. msg.cmd = IPMI_WDOG_SET_TIMER;
  332. msg.data = data;
  333. msg.data_len = sizeof(data);
  334. rv = ipmi_request_supply_msgs(watchdog_user,
  335. (struct ipmi_addr *) &addr,
  336. 0,
  337. &msg,
  338. NULL,
  339. smi_msg,
  340. recv_msg,
  341. 1);
  342. if (rv) {
  343. printk(KERN_WARNING PFX "set timeout error: %d\n",
  344. rv);
  345. }
  346. if (send_heartbeat_now)
  347. *send_heartbeat_now = hbnow;
  348. return rv;
  349. }
  350. static int ipmi_set_timeout(int do_heartbeat)
  351. {
  352. int send_heartbeat_now;
  353. int rv;
  354. /* We can only send one of these at a time. */
  355. mutex_lock(&set_timeout_lock);
  356. atomic_set(&set_timeout_tofree, 2);
  357. rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
  358. &set_timeout_recv_msg,
  359. &send_heartbeat_now);
  360. if (rv) {
  361. mutex_unlock(&set_timeout_lock);
  362. goto out;
  363. }
  364. wait_for_completion(&set_timeout_wait);
  365. mutex_unlock(&set_timeout_lock);
  366. if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
  367. || ((send_heartbeat_now)
  368. && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
  369. rv = ipmi_heartbeat();
  370. out:
  371. return rv;
  372. }
  373. static atomic_t panic_done_count = ATOMIC_INIT(0);
  374. static void panic_smi_free(struct ipmi_smi_msg *msg)
  375. {
  376. atomic_dec(&panic_done_count);
  377. }
  378. static void panic_recv_free(struct ipmi_recv_msg *msg)
  379. {
  380. atomic_dec(&panic_done_count);
  381. }
  382. static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg = {
  383. .done = panic_smi_free
  384. };
  385. static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg = {
  386. .done = panic_recv_free
  387. };
  388. static void panic_halt_ipmi_heartbeat(void)
  389. {
  390. struct kernel_ipmi_msg msg;
  391. struct ipmi_system_interface_addr addr;
  392. int rv;
  393. /*
  394. * Don't reset the timer if we have the timer turned off, that
  395. * re-enables the watchdog.
  396. */
  397. if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
  398. return;
  399. addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  400. addr.channel = IPMI_BMC_CHANNEL;
  401. addr.lun = 0;
  402. msg.netfn = 0x06;
  403. msg.cmd = IPMI_WDOG_RESET_TIMER;
  404. msg.data = NULL;
  405. msg.data_len = 0;
  406. atomic_add(1, &panic_done_count);
  407. rv = ipmi_request_supply_msgs(watchdog_user,
  408. (struct ipmi_addr *) &addr,
  409. 0,
  410. &msg,
  411. NULL,
  412. &panic_halt_heartbeat_smi_msg,
  413. &panic_halt_heartbeat_recv_msg,
  414. 1);
  415. if (rv)
  416. atomic_sub(1, &panic_done_count);
  417. }
  418. static struct ipmi_smi_msg panic_halt_smi_msg = {
  419. .done = panic_smi_free
  420. };
  421. static struct ipmi_recv_msg panic_halt_recv_msg = {
  422. .done = panic_recv_free
  423. };
  424. /*
  425. * Special call, doesn't claim any locks. This is only to be called
  426. * at panic or halt time, in run-to-completion mode, when the caller
  427. * is the only CPU and the only thing that will be going is these IPMI
  428. * calls.
  429. */
  430. static void panic_halt_ipmi_set_timeout(void)
  431. {
  432. int send_heartbeat_now;
  433. int rv;
  434. /* Wait for the messages to be free. */
  435. while (atomic_read(&panic_done_count) != 0)
  436. ipmi_poll_interface(watchdog_user);
  437. atomic_add(1, &panic_done_count);
  438. rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
  439. &panic_halt_recv_msg,
  440. &send_heartbeat_now);
  441. if (rv) {
  442. atomic_sub(1, &panic_done_count);
  443. printk(KERN_WARNING PFX
  444. "Unable to extend the watchdog timeout.");
  445. } else {
  446. if (send_heartbeat_now)
  447. panic_halt_ipmi_heartbeat();
  448. }
  449. while (atomic_read(&panic_done_count) != 0)
  450. ipmi_poll_interface(watchdog_user);
  451. }
  452. /*
  453. * We use a mutex to make sure that only one thing can send a
  454. * heartbeat at one time, because we only have one copy of the data.
  455. * The semaphore is claimed when the set_timeout is sent and freed
  456. * when both messages are free.
  457. */
  458. static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
  459. static DEFINE_MUTEX(heartbeat_lock);
  460. static DECLARE_COMPLETION(heartbeat_wait);
  461. static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
  462. {
  463. if (atomic_dec_and_test(&heartbeat_tofree))
  464. complete(&heartbeat_wait);
  465. }
  466. static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
  467. {
  468. if (atomic_dec_and_test(&heartbeat_tofree))
  469. complete(&heartbeat_wait);
  470. }
  471. static struct ipmi_smi_msg heartbeat_smi_msg = {
  472. .done = heartbeat_free_smi
  473. };
  474. static struct ipmi_recv_msg heartbeat_recv_msg = {
  475. .done = heartbeat_free_recv
  476. };
  477. static int ipmi_heartbeat(void)
  478. {
  479. struct kernel_ipmi_msg msg;
  480. int rv;
  481. struct ipmi_system_interface_addr addr;
  482. int timeout_retries = 0;
  483. if (ipmi_ignore_heartbeat)
  484. return 0;
  485. if (ipmi_start_timer_on_heartbeat) {
  486. ipmi_start_timer_on_heartbeat = 0;
  487. ipmi_watchdog_state = action_val;
  488. return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  489. } else if (pretimeout_since_last_heartbeat) {
  490. /*
  491. * A pretimeout occurred, make sure we set the timeout.
  492. * We don't want to set the action, though, we want to
  493. * leave that alone (thus it can't be combined with the
  494. * above operation.
  495. */
  496. return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
  497. }
  498. mutex_lock(&heartbeat_lock);
  499. restart:
  500. atomic_set(&heartbeat_tofree, 2);
  501. /*
  502. * Don't reset the timer if we have the timer turned off, that
  503. * re-enables the watchdog.
  504. */
  505. if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
  506. mutex_unlock(&heartbeat_lock);
  507. return 0;
  508. }
  509. addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  510. addr.channel = IPMI_BMC_CHANNEL;
  511. addr.lun = 0;
  512. msg.netfn = 0x06;
  513. msg.cmd = IPMI_WDOG_RESET_TIMER;
  514. msg.data = NULL;
  515. msg.data_len = 0;
  516. rv = ipmi_request_supply_msgs(watchdog_user,
  517. (struct ipmi_addr *) &addr,
  518. 0,
  519. &msg,
  520. NULL,
  521. &heartbeat_smi_msg,
  522. &heartbeat_recv_msg,
  523. 1);
  524. if (rv) {
  525. mutex_unlock(&heartbeat_lock);
  526. printk(KERN_WARNING PFX "heartbeat failure: %d\n",
  527. rv);
  528. return rv;
  529. }
  530. /* Wait for the heartbeat to be sent. */
  531. wait_for_completion(&heartbeat_wait);
  532. if (heartbeat_recv_msg.msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP) {
  533. timeout_retries++;
  534. if (timeout_retries > 3) {
  535. printk(KERN_ERR PFX ": Unable to restore the IPMI"
  536. " watchdog's settings, giving up.\n");
  537. rv = -EIO;
  538. goto out_unlock;
  539. }
  540. /*
  541. * The timer was not initialized, that means the BMC was
  542. * probably reset and lost the watchdog information. Attempt
  543. * to restore the timer's info. Note that we still hold
  544. * the heartbeat lock, to keep a heartbeat from happening
  545. * in this process, so must say no heartbeat to avoid a
  546. * deadlock on this mutex.
  547. */
  548. rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  549. if (rv) {
  550. printk(KERN_ERR PFX ": Unable to send the command to"
  551. " set the watchdog's settings, giving up.\n");
  552. goto out_unlock;
  553. }
  554. /* We might need a new heartbeat, so do it now */
  555. goto restart;
  556. } else if (heartbeat_recv_msg.msg.data[0] != 0) {
  557. /*
  558. * Got an error in the heartbeat response. It was already
  559. * reported in ipmi_wdog_msg_handler, but we should return
  560. * an error here.
  561. */
  562. rv = -EINVAL;
  563. }
  564. out_unlock:
  565. mutex_unlock(&heartbeat_lock);
  566. return rv;
  567. }
  568. static struct watchdog_info ident = {
  569. .options = 0, /* WDIOF_SETTIMEOUT, */
  570. .firmware_version = 1,
  571. .identity = "IPMI"
  572. };
  573. static int ipmi_ioctl(struct file *file,
  574. unsigned int cmd, unsigned long arg)
  575. {
  576. void __user *argp = (void __user *)arg;
  577. int i;
  578. int val;
  579. switch (cmd) {
  580. case WDIOC_GETSUPPORT:
  581. i = copy_to_user(argp, &ident, sizeof(ident));
  582. return i ? -EFAULT : 0;
  583. case WDIOC_SETTIMEOUT:
  584. i = copy_from_user(&val, argp, sizeof(int));
  585. if (i)
  586. return -EFAULT;
  587. timeout = val;
  588. return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
  589. case WDIOC_GETTIMEOUT:
  590. i = copy_to_user(argp, &timeout, sizeof(timeout));
  591. if (i)
  592. return -EFAULT;
  593. return 0;
  594. case WDIOC_SETPRETIMEOUT:
  595. i = copy_from_user(&val, argp, sizeof(int));
  596. if (i)
  597. return -EFAULT;
  598. pretimeout = val;
  599. return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
  600. case WDIOC_GETPRETIMEOUT:
  601. i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
  602. if (i)
  603. return -EFAULT;
  604. return 0;
  605. case WDIOC_KEEPALIVE:
  606. return ipmi_heartbeat();
  607. case WDIOC_SETOPTIONS:
  608. i = copy_from_user(&val, argp, sizeof(int));
  609. if (i)
  610. return -EFAULT;
  611. if (val & WDIOS_DISABLECARD) {
  612. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  613. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  614. ipmi_start_timer_on_heartbeat = 0;
  615. }
  616. if (val & WDIOS_ENABLECARD) {
  617. ipmi_watchdog_state = action_val;
  618. ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  619. }
  620. return 0;
  621. case WDIOC_GETSTATUS:
  622. val = 0;
  623. i = copy_to_user(argp, &val, sizeof(val));
  624. if (i)
  625. return -EFAULT;
  626. return 0;
  627. default:
  628. return -ENOIOCTLCMD;
  629. }
  630. }
  631. static long ipmi_unlocked_ioctl(struct file *file,
  632. unsigned int cmd,
  633. unsigned long arg)
  634. {
  635. int ret;
  636. mutex_lock(&ipmi_watchdog_mutex);
  637. ret = ipmi_ioctl(file, cmd, arg);
  638. mutex_unlock(&ipmi_watchdog_mutex);
  639. return ret;
  640. }
  641. static ssize_t ipmi_write(struct file *file,
  642. const char __user *buf,
  643. size_t len,
  644. loff_t *ppos)
  645. {
  646. int rv;
  647. if (len) {
  648. if (!nowayout) {
  649. size_t i;
  650. /* In case it was set long ago */
  651. expect_close = 0;
  652. for (i = 0; i != len; i++) {
  653. char c;
  654. if (get_user(c, buf + i))
  655. return -EFAULT;
  656. if (c == 'V')
  657. expect_close = 42;
  658. }
  659. }
  660. rv = ipmi_heartbeat();
  661. if (rv)
  662. return rv;
  663. }
  664. return len;
  665. }
  666. static ssize_t ipmi_read(struct file *file,
  667. char __user *buf,
  668. size_t count,
  669. loff_t *ppos)
  670. {
  671. int rv = 0;
  672. wait_queue_entry_t wait;
  673. if (count <= 0)
  674. return 0;
  675. /*
  676. * Reading returns if the pretimeout has gone off, and it only does
  677. * it once per pretimeout.
  678. */
  679. spin_lock(&ipmi_read_lock);
  680. if (!data_to_read) {
  681. if (file->f_flags & O_NONBLOCK) {
  682. rv = -EAGAIN;
  683. goto out;
  684. }
  685. init_waitqueue_entry(&wait, current);
  686. add_wait_queue(&read_q, &wait);
  687. while (!data_to_read) {
  688. set_current_state(TASK_INTERRUPTIBLE);
  689. spin_unlock(&ipmi_read_lock);
  690. schedule();
  691. spin_lock(&ipmi_read_lock);
  692. }
  693. remove_wait_queue(&read_q, &wait);
  694. if (signal_pending(current)) {
  695. rv = -ERESTARTSYS;
  696. goto out;
  697. }
  698. }
  699. data_to_read = 0;
  700. out:
  701. spin_unlock(&ipmi_read_lock);
  702. if (rv == 0) {
  703. if (copy_to_user(buf, &data_to_read, 1))
  704. rv = -EFAULT;
  705. else
  706. rv = 1;
  707. }
  708. return rv;
  709. }
  710. static int ipmi_open(struct inode *ino, struct file *filep)
  711. {
  712. switch (iminor(ino)) {
  713. case WATCHDOG_MINOR:
  714. if (test_and_set_bit(0, &ipmi_wdog_open))
  715. return -EBUSY;
  716. /*
  717. * Don't start the timer now, let it start on the
  718. * first heartbeat.
  719. */
  720. ipmi_start_timer_on_heartbeat = 1;
  721. return nonseekable_open(ino, filep);
  722. default:
  723. return (-ENODEV);
  724. }
  725. }
  726. static __poll_t ipmi_poll(struct file *file, poll_table *wait)
  727. {
  728. __poll_t mask = 0;
  729. poll_wait(file, &read_q, wait);
  730. spin_lock(&ipmi_read_lock);
  731. if (data_to_read)
  732. mask |= (EPOLLIN | EPOLLRDNORM);
  733. spin_unlock(&ipmi_read_lock);
  734. return mask;
  735. }
  736. static int ipmi_fasync(int fd, struct file *file, int on)
  737. {
  738. int result;
  739. result = fasync_helper(fd, file, on, &fasync_q);
  740. return (result);
  741. }
  742. static int ipmi_close(struct inode *ino, struct file *filep)
  743. {
  744. if (iminor(ino) == WATCHDOG_MINOR) {
  745. if (expect_close == 42) {
  746. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  747. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  748. } else {
  749. printk(KERN_CRIT PFX
  750. "Unexpected close, not stopping watchdog!\n");
  751. ipmi_heartbeat();
  752. }
  753. clear_bit(0, &ipmi_wdog_open);
  754. }
  755. expect_close = 0;
  756. return 0;
  757. }
  758. static const struct file_operations ipmi_wdog_fops = {
  759. .owner = THIS_MODULE,
  760. .read = ipmi_read,
  761. .poll = ipmi_poll,
  762. .write = ipmi_write,
  763. .unlocked_ioctl = ipmi_unlocked_ioctl,
  764. .open = ipmi_open,
  765. .release = ipmi_close,
  766. .fasync = ipmi_fasync,
  767. .llseek = no_llseek,
  768. };
  769. static struct miscdevice ipmi_wdog_miscdev = {
  770. .minor = WATCHDOG_MINOR,
  771. .name = "watchdog",
  772. .fops = &ipmi_wdog_fops
  773. };
  774. static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
  775. void *handler_data)
  776. {
  777. if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
  778. msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
  779. printk(KERN_INFO PFX "response: The IPMI controller appears"
  780. " to have been reset, will attempt to reinitialize"
  781. " the watchdog timer\n");
  782. else if (msg->msg.data[0] != 0)
  783. printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
  784. msg->msg.data[0],
  785. msg->msg.cmd);
  786. ipmi_free_recv_msg(msg);
  787. }
  788. static void ipmi_wdog_pretimeout_handler(void *handler_data)
  789. {
  790. if (preaction_val != WDOG_PRETIMEOUT_NONE) {
  791. if (preop_val == WDOG_PREOP_PANIC) {
  792. if (atomic_inc_and_test(&preop_panic_excl))
  793. panic("Watchdog pre-timeout");
  794. } else if (preop_val == WDOG_PREOP_GIVE_DATA) {
  795. spin_lock(&ipmi_read_lock);
  796. data_to_read = 1;
  797. wake_up_interruptible(&read_q);
  798. kill_fasync(&fasync_q, SIGIO, POLL_IN);
  799. spin_unlock(&ipmi_read_lock);
  800. }
  801. }
  802. /*
  803. * On some machines, the heartbeat will give an error and not
  804. * work unless we re-enable the timer. So do so.
  805. */
  806. pretimeout_since_last_heartbeat = 1;
  807. }
  808. static const struct ipmi_user_hndl ipmi_hndlrs = {
  809. .ipmi_recv_hndl = ipmi_wdog_msg_handler,
  810. .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
  811. };
  812. static void ipmi_register_watchdog(int ipmi_intf)
  813. {
  814. int rv = -EBUSY;
  815. if (watchdog_user)
  816. goto out;
  817. if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
  818. goto out;
  819. watchdog_ifnum = ipmi_intf;
  820. rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
  821. if (rv < 0) {
  822. printk(KERN_CRIT PFX "Unable to register with ipmi\n");
  823. goto out;
  824. }
  825. rv = ipmi_get_version(watchdog_user,
  826. &ipmi_version_major,
  827. &ipmi_version_minor);
  828. if (rv) {
  829. pr_warn(PFX "Unable to get IPMI version, assuming 1.0\n");
  830. ipmi_version_major = 1;
  831. ipmi_version_minor = 0;
  832. }
  833. rv = misc_register(&ipmi_wdog_miscdev);
  834. if (rv < 0) {
  835. ipmi_destroy_user(watchdog_user);
  836. watchdog_user = NULL;
  837. printk(KERN_CRIT PFX "Unable to register misc device\n");
  838. }
  839. #ifdef HAVE_DIE_NMI
  840. if (nmi_handler_registered) {
  841. int old_pretimeout = pretimeout;
  842. int old_timeout = timeout;
  843. int old_preop_val = preop_val;
  844. /*
  845. * Set the pretimeout to go off in a second and give
  846. * ourselves plenty of time to stop the timer.
  847. */
  848. ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
  849. preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */
  850. pretimeout = 99;
  851. timeout = 100;
  852. testing_nmi = 1;
  853. rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  854. if (rv) {
  855. printk(KERN_WARNING PFX "Error starting timer to"
  856. " test NMI: 0x%x. The NMI pretimeout will"
  857. " likely not work\n", rv);
  858. rv = 0;
  859. goto out_restore;
  860. }
  861. msleep(1500);
  862. if (testing_nmi != 2) {
  863. printk(KERN_WARNING PFX "IPMI NMI didn't seem to"
  864. " occur. The NMI pretimeout will"
  865. " likely not work\n");
  866. }
  867. out_restore:
  868. testing_nmi = 0;
  869. preop_val = old_preop_val;
  870. pretimeout = old_pretimeout;
  871. timeout = old_timeout;
  872. }
  873. #endif
  874. out:
  875. if ((start_now) && (rv == 0)) {
  876. /* Run from startup, so start the timer now. */
  877. start_now = 0; /* Disable this function after first startup. */
  878. ipmi_watchdog_state = action_val;
  879. ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  880. printk(KERN_INFO PFX "Starting now!\n");
  881. } else {
  882. /* Stop the timer now. */
  883. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  884. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  885. }
  886. }
  887. static void ipmi_unregister_watchdog(int ipmi_intf)
  888. {
  889. int rv;
  890. if (!watchdog_user)
  891. goto out;
  892. if (watchdog_ifnum != ipmi_intf)
  893. goto out;
  894. /* Make sure no one can call us any more. */
  895. misc_deregister(&ipmi_wdog_miscdev);
  896. /*
  897. * Wait to make sure the message makes it out. The lower layer has
  898. * pointers to our buffers, we want to make sure they are done before
  899. * we release our memory.
  900. */
  901. while (atomic_read(&set_timeout_tofree))
  902. schedule_timeout_uninterruptible(1);
  903. /* Disconnect from IPMI. */
  904. rv = ipmi_destroy_user(watchdog_user);
  905. if (rv) {
  906. printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
  907. rv);
  908. }
  909. watchdog_user = NULL;
  910. out:
  911. return;
  912. }
  913. #ifdef HAVE_DIE_NMI
  914. static int
  915. ipmi_nmi(unsigned int val, struct pt_regs *regs)
  916. {
  917. /*
  918. * If we get here, it's an NMI that's not a memory or I/O
  919. * error. We can't truly tell if it's from IPMI or not
  920. * without sending a message, and sending a message is almost
  921. * impossible because of locking.
  922. */
  923. if (testing_nmi) {
  924. testing_nmi = 2;
  925. return NMI_HANDLED;
  926. }
  927. /* If we are not expecting a timeout, ignore it. */
  928. if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
  929. return NMI_DONE;
  930. if (preaction_val != WDOG_PRETIMEOUT_NMI)
  931. return NMI_DONE;
  932. /*
  933. * If no one else handled the NMI, we assume it was the IPMI
  934. * watchdog.
  935. */
  936. if (preop_val == WDOG_PREOP_PANIC) {
  937. /* On some machines, the heartbeat will give
  938. an error and not work unless we re-enable
  939. the timer. So do so. */
  940. pretimeout_since_last_heartbeat = 1;
  941. if (atomic_inc_and_test(&preop_panic_excl))
  942. nmi_panic(regs, PFX "pre-timeout");
  943. }
  944. return NMI_HANDLED;
  945. }
  946. #endif
  947. static int wdog_reboot_handler(struct notifier_block *this,
  948. unsigned long code,
  949. void *unused)
  950. {
  951. static int reboot_event_handled;
  952. if ((watchdog_user) && (!reboot_event_handled)) {
  953. /* Make sure we only do this once. */
  954. reboot_event_handled = 1;
  955. if (code == SYS_POWER_OFF || code == SYS_HALT) {
  956. /* Disable the WDT if we are shutting down. */
  957. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  958. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  959. } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
  960. /* Set a long timer to let the reboot happen or
  961. reset if it hangs, but only if the watchdog
  962. timer was already running. */
  963. if (timeout < 120)
  964. timeout = 120;
  965. pretimeout = 0;
  966. ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
  967. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  968. }
  969. }
  970. return NOTIFY_OK;
  971. }
  972. static struct notifier_block wdog_reboot_notifier = {
  973. .notifier_call = wdog_reboot_handler,
  974. .next = NULL,
  975. .priority = 0
  976. };
  977. static int wdog_panic_handler(struct notifier_block *this,
  978. unsigned long event,
  979. void *unused)
  980. {
  981. static int panic_event_handled;
  982. /* On a panic, if we have a panic timeout, make sure to extend
  983. the watchdog timer to a reasonable value to complete the
  984. panic, if the watchdog timer is running. Plus the
  985. pretimeout is meaningless at panic time. */
  986. if (watchdog_user && !panic_event_handled &&
  987. ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
  988. /* Make sure we do this only once. */
  989. panic_event_handled = 1;
  990. timeout = panic_wdt_timeout;
  991. pretimeout = 0;
  992. panic_halt_ipmi_set_timeout();
  993. }
  994. return NOTIFY_OK;
  995. }
  996. static struct notifier_block wdog_panic_notifier = {
  997. .notifier_call = wdog_panic_handler,
  998. .next = NULL,
  999. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  1000. };
  1001. static void ipmi_new_smi(int if_num, struct device *device)
  1002. {
  1003. ipmi_register_watchdog(if_num);
  1004. }
  1005. static void ipmi_smi_gone(int if_num)
  1006. {
  1007. ipmi_unregister_watchdog(if_num);
  1008. }
  1009. static struct ipmi_smi_watcher smi_watcher = {
  1010. .owner = THIS_MODULE,
  1011. .new_smi = ipmi_new_smi,
  1012. .smi_gone = ipmi_smi_gone
  1013. };
  1014. static int action_op(const char *inval, char *outval)
  1015. {
  1016. if (outval)
  1017. strcpy(outval, action);
  1018. if (!inval)
  1019. return 0;
  1020. if (strcmp(inval, "reset") == 0)
  1021. action_val = WDOG_TIMEOUT_RESET;
  1022. else if (strcmp(inval, "none") == 0)
  1023. action_val = WDOG_TIMEOUT_NONE;
  1024. else if (strcmp(inval, "power_cycle") == 0)
  1025. action_val = WDOG_TIMEOUT_POWER_CYCLE;
  1026. else if (strcmp(inval, "power_off") == 0)
  1027. action_val = WDOG_TIMEOUT_POWER_DOWN;
  1028. else
  1029. return -EINVAL;
  1030. strcpy(action, inval);
  1031. return 0;
  1032. }
  1033. static int preaction_op(const char *inval, char *outval)
  1034. {
  1035. if (outval)
  1036. strcpy(outval, preaction);
  1037. if (!inval)
  1038. return 0;
  1039. if (strcmp(inval, "pre_none") == 0)
  1040. preaction_val = WDOG_PRETIMEOUT_NONE;
  1041. else if (strcmp(inval, "pre_smi") == 0)
  1042. preaction_val = WDOG_PRETIMEOUT_SMI;
  1043. #ifdef HAVE_DIE_NMI
  1044. else if (strcmp(inval, "pre_nmi") == 0)
  1045. preaction_val = WDOG_PRETIMEOUT_NMI;
  1046. #endif
  1047. else if (strcmp(inval, "pre_int") == 0)
  1048. preaction_val = WDOG_PRETIMEOUT_MSG_INT;
  1049. else
  1050. return -EINVAL;
  1051. strcpy(preaction, inval);
  1052. return 0;
  1053. }
  1054. static int preop_op(const char *inval, char *outval)
  1055. {
  1056. if (outval)
  1057. strcpy(outval, preop);
  1058. if (!inval)
  1059. return 0;
  1060. if (strcmp(inval, "preop_none") == 0)
  1061. preop_val = WDOG_PREOP_NONE;
  1062. else if (strcmp(inval, "preop_panic") == 0)
  1063. preop_val = WDOG_PREOP_PANIC;
  1064. else if (strcmp(inval, "preop_give_data") == 0)
  1065. preop_val = WDOG_PREOP_GIVE_DATA;
  1066. else
  1067. return -EINVAL;
  1068. strcpy(preop, inval);
  1069. return 0;
  1070. }
  1071. static void check_parms(void)
  1072. {
  1073. #ifdef HAVE_DIE_NMI
  1074. int do_nmi = 0;
  1075. int rv;
  1076. if (preaction_val == WDOG_PRETIMEOUT_NMI) {
  1077. do_nmi = 1;
  1078. if (preop_val == WDOG_PREOP_GIVE_DATA) {
  1079. printk(KERN_WARNING PFX "Pretimeout op is to give data"
  1080. " but NMI pretimeout is enabled, setting"
  1081. " pretimeout op to none\n");
  1082. preop_op("preop_none", NULL);
  1083. do_nmi = 0;
  1084. }
  1085. }
  1086. if (do_nmi && !nmi_handler_registered) {
  1087. rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
  1088. "ipmi");
  1089. if (rv) {
  1090. printk(KERN_WARNING PFX
  1091. "Can't register nmi handler\n");
  1092. return;
  1093. } else
  1094. nmi_handler_registered = 1;
  1095. } else if (!do_nmi && nmi_handler_registered) {
  1096. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1097. nmi_handler_registered = 0;
  1098. }
  1099. #endif
  1100. }
  1101. static int __init ipmi_wdog_init(void)
  1102. {
  1103. int rv;
  1104. if (action_op(action, NULL)) {
  1105. action_op("reset", NULL);
  1106. printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
  1107. " reset\n", action);
  1108. }
  1109. if (preaction_op(preaction, NULL)) {
  1110. preaction_op("pre_none", NULL);
  1111. printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
  1112. " none\n", preaction);
  1113. }
  1114. if (preop_op(preop, NULL)) {
  1115. preop_op("preop_none", NULL);
  1116. printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
  1117. " none\n", preop);
  1118. }
  1119. check_parms();
  1120. register_reboot_notifier(&wdog_reboot_notifier);
  1121. atomic_notifier_chain_register(&panic_notifier_list,
  1122. &wdog_panic_notifier);
  1123. rv = ipmi_smi_watcher_register(&smi_watcher);
  1124. if (rv) {
  1125. #ifdef HAVE_DIE_NMI
  1126. if (nmi_handler_registered)
  1127. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1128. #endif
  1129. atomic_notifier_chain_unregister(&panic_notifier_list,
  1130. &wdog_panic_notifier);
  1131. unregister_reboot_notifier(&wdog_reboot_notifier);
  1132. printk(KERN_WARNING PFX "can't register smi watcher\n");
  1133. return rv;
  1134. }
  1135. printk(KERN_INFO PFX "driver initialized\n");
  1136. return 0;
  1137. }
  1138. static void __exit ipmi_wdog_exit(void)
  1139. {
  1140. ipmi_smi_watcher_unregister(&smi_watcher);
  1141. ipmi_unregister_watchdog(watchdog_ifnum);
  1142. #ifdef HAVE_DIE_NMI
  1143. if (nmi_handler_registered)
  1144. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1145. #endif
  1146. atomic_notifier_chain_unregister(&panic_notifier_list,
  1147. &wdog_panic_notifier);
  1148. unregister_reboot_notifier(&wdog_reboot_notifier);
  1149. }
  1150. module_exit(ipmi_wdog_exit);
  1151. module_init(ipmi_wdog_init);
  1152. MODULE_LICENSE("GPL");
  1153. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  1154. MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");