ipmi_watchdog.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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. #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;
  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("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("Unable to extend the watchdog timeout\n");
  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("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("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("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("Unexpected close, not stopping watchdog!\n");
  729. ipmi_heartbeat();
  730. }
  731. clear_bit(0, &ipmi_wdog_open);
  732. }
  733. expect_close = 0;
  734. return 0;
  735. }
  736. static const struct file_operations ipmi_wdog_fops = {
  737. .owner = THIS_MODULE,
  738. .read = ipmi_read,
  739. .poll = ipmi_poll,
  740. .write = ipmi_write,
  741. .unlocked_ioctl = ipmi_unlocked_ioctl,
  742. .open = ipmi_open,
  743. .release = ipmi_close,
  744. .fasync = ipmi_fasync,
  745. .llseek = no_llseek,
  746. };
  747. static struct miscdevice ipmi_wdog_miscdev = {
  748. .minor = WATCHDOG_MINOR,
  749. .name = "watchdog",
  750. .fops = &ipmi_wdog_fops
  751. };
  752. static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
  753. void *handler_data)
  754. {
  755. if (msg->msg.cmd == IPMI_WDOG_RESET_TIMER &&
  756. msg->msg.data[0] == IPMI_WDOG_TIMER_NOT_INIT_RESP)
  757. pr_info("response: The IPMI controller appears to have been reset, will attempt to reinitialize the watchdog timer\n");
  758. else if (msg->msg.data[0] != 0)
  759. pr_err("response: Error %x on cmd %x\n",
  760. msg->msg.data[0],
  761. msg->msg.cmd);
  762. ipmi_free_recv_msg(msg);
  763. }
  764. static void ipmi_wdog_pretimeout_handler(void *handler_data)
  765. {
  766. if (preaction_val != WDOG_PRETIMEOUT_NONE) {
  767. if (preop_val == WDOG_PREOP_PANIC) {
  768. if (atomic_inc_and_test(&preop_panic_excl))
  769. panic("Watchdog pre-timeout");
  770. } else if (preop_val == WDOG_PREOP_GIVE_DATA) {
  771. unsigned long flags;
  772. spin_lock_irqsave(&ipmi_read_lock, flags);
  773. data_to_read = 1;
  774. wake_up_interruptible(&read_q);
  775. kill_fasync(&fasync_q, SIGIO, POLL_IN);
  776. spin_unlock_irqrestore(&ipmi_read_lock, flags);
  777. }
  778. }
  779. /*
  780. * On some machines, the heartbeat will give an error and not
  781. * work unless we re-enable the timer. So do so.
  782. */
  783. atomic_set(&pretimeout_since_last_heartbeat, 1);
  784. }
  785. static void ipmi_wdog_panic_handler(void *user_data)
  786. {
  787. static int panic_event_handled;
  788. /*
  789. * On a panic, if we have a panic timeout, make sure to extend
  790. * the watchdog timer to a reasonable value to complete the
  791. * panic, if the watchdog timer is running. Plus the
  792. * pretimeout is meaningless at panic time.
  793. */
  794. if (watchdog_user && !panic_event_handled &&
  795. ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
  796. /* Make sure we do this only once. */
  797. panic_event_handled = 1;
  798. timeout = panic_wdt_timeout;
  799. pretimeout = 0;
  800. panic_halt_ipmi_set_timeout();
  801. }
  802. }
  803. static const struct ipmi_user_hndl ipmi_hndlrs = {
  804. .ipmi_recv_hndl = ipmi_wdog_msg_handler,
  805. .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler,
  806. .ipmi_panic_handler = ipmi_wdog_panic_handler
  807. };
  808. static void ipmi_register_watchdog(int ipmi_intf)
  809. {
  810. int rv = -EBUSY;
  811. if (watchdog_user)
  812. goto out;
  813. if ((ifnum_to_use >= 0) && (ifnum_to_use != ipmi_intf))
  814. goto out;
  815. watchdog_ifnum = ipmi_intf;
  816. rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
  817. if (rv < 0) {
  818. pr_crit("Unable to register with ipmi\n");
  819. goto out;
  820. }
  821. rv = ipmi_get_version(watchdog_user,
  822. &ipmi_version_major,
  823. &ipmi_version_minor);
  824. if (rv) {
  825. pr_warn("Unable to get IPMI version, assuming 1.0\n");
  826. ipmi_version_major = 1;
  827. ipmi_version_minor = 0;
  828. }
  829. rv = misc_register(&ipmi_wdog_miscdev);
  830. if (rv < 0) {
  831. ipmi_destroy_user(watchdog_user);
  832. watchdog_user = NULL;
  833. pr_crit("Unable to register misc device\n");
  834. }
  835. #ifdef HAVE_DIE_NMI
  836. if (nmi_handler_registered) {
  837. int old_pretimeout = pretimeout;
  838. int old_timeout = timeout;
  839. int old_preop_val = preop_val;
  840. /*
  841. * Set the pretimeout to go off in a second and give
  842. * ourselves plenty of time to stop the timer.
  843. */
  844. ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
  845. preop_val = WDOG_PREOP_NONE; /* Make sure nothing happens */
  846. pretimeout = 99;
  847. timeout = 100;
  848. testing_nmi = 1;
  849. rv = ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  850. if (rv) {
  851. pr_warn("Error starting timer to test NMI: 0x%x. The NMI pretimeout will likely not work\n",
  852. rv);
  853. rv = 0;
  854. goto out_restore;
  855. }
  856. msleep(1500);
  857. if (testing_nmi != 2) {
  858. pr_warn("IPMI NMI didn't seem to occur. The NMI pretimeout will likely not work\n");
  859. }
  860. out_restore:
  861. testing_nmi = 0;
  862. preop_val = old_preop_val;
  863. pretimeout = old_pretimeout;
  864. timeout = old_timeout;
  865. }
  866. #endif
  867. out:
  868. if ((start_now) && (rv == 0)) {
  869. /* Run from startup, so start the timer now. */
  870. start_now = 0; /* Disable this function after first startup. */
  871. ipmi_watchdog_state = action_val;
  872. ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  873. pr_info("Starting now!\n");
  874. } else {
  875. /* Stop the timer now. */
  876. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  877. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  878. }
  879. }
  880. static void ipmi_unregister_watchdog(int ipmi_intf)
  881. {
  882. int rv;
  883. struct ipmi_user *loc_user = watchdog_user;
  884. if (!loc_user)
  885. return;
  886. if (watchdog_ifnum != ipmi_intf)
  887. return;
  888. /* Make sure no one can call us any more. */
  889. misc_deregister(&ipmi_wdog_miscdev);
  890. watchdog_user = NULL;
  891. /*
  892. * Wait to make sure the message makes it out. The lower layer has
  893. * pointers to our buffers, we want to make sure they are done before
  894. * we release our memory.
  895. */
  896. while (atomic_read(&msg_tofree))
  897. msg_free_smi(NULL);
  898. mutex_lock(&ipmi_watchdog_mutex);
  899. /* Disconnect from IPMI. */
  900. rv = ipmi_destroy_user(loc_user);
  901. if (rv)
  902. pr_warn("error unlinking from IPMI: %d\n", rv);
  903. /* If it comes back, restart it properly. */
  904. ipmi_start_timer_on_heartbeat = 1;
  905. mutex_unlock(&ipmi_watchdog_mutex);
  906. }
  907. #ifdef HAVE_DIE_NMI
  908. static int
  909. ipmi_nmi(unsigned int val, struct pt_regs *regs)
  910. {
  911. /*
  912. * If we get here, it's an NMI that's not a memory or I/O
  913. * error. We can't truly tell if it's from IPMI or not
  914. * without sending a message, and sending a message is almost
  915. * impossible because of locking.
  916. */
  917. if (testing_nmi) {
  918. testing_nmi = 2;
  919. return NMI_HANDLED;
  920. }
  921. /* If we are not expecting a timeout, ignore it. */
  922. if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
  923. return NMI_DONE;
  924. if (preaction_val != WDOG_PRETIMEOUT_NMI)
  925. return NMI_DONE;
  926. /*
  927. * If no one else handled the NMI, we assume it was the IPMI
  928. * watchdog.
  929. */
  930. if (preop_val == WDOG_PREOP_PANIC) {
  931. /* On some machines, the heartbeat will give
  932. an error and not work unless we re-enable
  933. the timer. So do so. */
  934. atomic_set(&pretimeout_since_last_heartbeat, 1);
  935. if (atomic_inc_and_test(&preop_panic_excl))
  936. nmi_panic(regs, "pre-timeout");
  937. }
  938. return NMI_HANDLED;
  939. }
  940. #endif
  941. static int wdog_reboot_handler(struct notifier_block *this,
  942. unsigned long code,
  943. void *unused)
  944. {
  945. static int reboot_event_handled;
  946. if ((watchdog_user) && (!reboot_event_handled)) {
  947. /* Make sure we only do this once. */
  948. reboot_event_handled = 1;
  949. if (code == SYS_POWER_OFF || code == SYS_HALT) {
  950. /* Disable the WDT if we are shutting down. */
  951. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  952. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  953. } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
  954. /* Set a long timer to let the reboot happen or
  955. reset if it hangs, but only if the watchdog
  956. timer was already running. */
  957. if (timeout < 120)
  958. timeout = 120;
  959. pretimeout = 0;
  960. ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
  961. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  962. }
  963. }
  964. return NOTIFY_OK;
  965. }
  966. static struct notifier_block wdog_reboot_notifier = {
  967. .notifier_call = wdog_reboot_handler,
  968. .next = NULL,
  969. .priority = 0
  970. };
  971. static void ipmi_new_smi(int if_num, struct device *device)
  972. {
  973. ipmi_register_watchdog(if_num);
  974. }
  975. static void ipmi_smi_gone(int if_num)
  976. {
  977. ipmi_unregister_watchdog(if_num);
  978. }
  979. static struct ipmi_smi_watcher smi_watcher = {
  980. .owner = THIS_MODULE,
  981. .new_smi = ipmi_new_smi,
  982. .smi_gone = ipmi_smi_gone
  983. };
  984. static int action_op(const char *inval, char *outval)
  985. {
  986. if (outval)
  987. strcpy(outval, action);
  988. if (!inval)
  989. return 0;
  990. if (strcmp(inval, "reset") == 0)
  991. action_val = WDOG_TIMEOUT_RESET;
  992. else if (strcmp(inval, "none") == 0)
  993. action_val = WDOG_TIMEOUT_NONE;
  994. else if (strcmp(inval, "power_cycle") == 0)
  995. action_val = WDOG_TIMEOUT_POWER_CYCLE;
  996. else if (strcmp(inval, "power_off") == 0)
  997. action_val = WDOG_TIMEOUT_POWER_DOWN;
  998. else
  999. return -EINVAL;
  1000. strcpy(action, inval);
  1001. return 0;
  1002. }
  1003. static int preaction_op(const char *inval, char *outval)
  1004. {
  1005. if (outval)
  1006. strcpy(outval, preaction);
  1007. if (!inval)
  1008. return 0;
  1009. if (strcmp(inval, "pre_none") == 0)
  1010. preaction_val = WDOG_PRETIMEOUT_NONE;
  1011. else if (strcmp(inval, "pre_smi") == 0)
  1012. preaction_val = WDOG_PRETIMEOUT_SMI;
  1013. #ifdef HAVE_DIE_NMI
  1014. else if (strcmp(inval, "pre_nmi") == 0)
  1015. preaction_val = WDOG_PRETIMEOUT_NMI;
  1016. #endif
  1017. else if (strcmp(inval, "pre_int") == 0)
  1018. preaction_val = WDOG_PRETIMEOUT_MSG_INT;
  1019. else
  1020. return -EINVAL;
  1021. strcpy(preaction, inval);
  1022. return 0;
  1023. }
  1024. static int preop_op(const char *inval, char *outval)
  1025. {
  1026. if (outval)
  1027. strcpy(outval, preop);
  1028. if (!inval)
  1029. return 0;
  1030. if (strcmp(inval, "preop_none") == 0)
  1031. preop_val = WDOG_PREOP_NONE;
  1032. else if (strcmp(inval, "preop_panic") == 0)
  1033. preop_val = WDOG_PREOP_PANIC;
  1034. else if (strcmp(inval, "preop_give_data") == 0)
  1035. preop_val = WDOG_PREOP_GIVE_DATA;
  1036. else
  1037. return -EINVAL;
  1038. strcpy(preop, inval);
  1039. return 0;
  1040. }
  1041. static void check_parms(void)
  1042. {
  1043. #ifdef HAVE_DIE_NMI
  1044. int do_nmi = 0;
  1045. int rv;
  1046. if (preaction_val == WDOG_PRETIMEOUT_NMI) {
  1047. do_nmi = 1;
  1048. if (preop_val == WDOG_PREOP_GIVE_DATA) {
  1049. pr_warn("Pretimeout op is to give data but NMI pretimeout is enabled, setting pretimeout op to none\n");
  1050. preop_op("preop_none", NULL);
  1051. do_nmi = 0;
  1052. }
  1053. }
  1054. if (do_nmi && !nmi_handler_registered) {
  1055. rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
  1056. "ipmi");
  1057. if (rv) {
  1058. pr_warn("Can't register nmi handler\n");
  1059. return;
  1060. } else
  1061. nmi_handler_registered = 1;
  1062. } else if (!do_nmi && nmi_handler_registered) {
  1063. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1064. nmi_handler_registered = 0;
  1065. }
  1066. #endif
  1067. }
  1068. static int __init ipmi_wdog_init(void)
  1069. {
  1070. int rv;
  1071. if (action_op(action, NULL)) {
  1072. action_op("reset", NULL);
  1073. pr_info("Unknown action '%s', defaulting to reset\n", action);
  1074. }
  1075. if (preaction_op(preaction, NULL)) {
  1076. preaction_op("pre_none", NULL);
  1077. pr_info("Unknown preaction '%s', defaulting to none\n",
  1078. preaction);
  1079. }
  1080. if (preop_op(preop, NULL)) {
  1081. preop_op("preop_none", NULL);
  1082. pr_info("Unknown preop '%s', defaulting to none\n", preop);
  1083. }
  1084. check_parms();
  1085. register_reboot_notifier(&wdog_reboot_notifier);
  1086. rv = ipmi_smi_watcher_register(&smi_watcher);
  1087. if (rv) {
  1088. #ifdef HAVE_DIE_NMI
  1089. if (nmi_handler_registered)
  1090. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1091. #endif
  1092. unregister_reboot_notifier(&wdog_reboot_notifier);
  1093. pr_warn("can't register smi watcher\n");
  1094. return rv;
  1095. }
  1096. pr_info("driver initialized\n");
  1097. return 0;
  1098. }
  1099. static void __exit ipmi_wdog_exit(void)
  1100. {
  1101. ipmi_smi_watcher_unregister(&smi_watcher);
  1102. ipmi_unregister_watchdog(watchdog_ifnum);
  1103. #ifdef HAVE_DIE_NMI
  1104. if (nmi_handler_registered)
  1105. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1106. #endif
  1107. unregister_reboot_notifier(&wdog_reboot_notifier);
  1108. }
  1109. module_exit(ipmi_wdog_exit);
  1110. module_init(ipmi_wdog_init);
  1111. MODULE_LICENSE("GPL");
  1112. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  1113. MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");