ipmi_watchdog.c 35 KB

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