ipmi_watchdog.c 35 KB

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