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