ipmi_watchdog.c 32 KB

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