ipmi_watchdog.c 33 KB

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