ipmi_watchdog.c 33 KB

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