ipmi_watchdog.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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(1, &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(1, &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(1, &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(1, &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_entry_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 happen or
  976. reset if it hangs, but only if the watchdog
  977. timer was already running. */
  978. if (timeout < 120)
  979. timeout = 120;
  980. pretimeout = 0;
  981. ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
  982. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  983. }
  984. }
  985. return NOTIFY_OK;
  986. }
  987. static struct notifier_block wdog_reboot_notifier = {
  988. .notifier_call = wdog_reboot_handler,
  989. .next = NULL,
  990. .priority = 0
  991. };
  992. static int wdog_panic_handler(struct notifier_block *this,
  993. unsigned long event,
  994. void *unused)
  995. {
  996. static int panic_event_handled;
  997. /* On a panic, if we have a panic timeout, make sure to extend
  998. the watchdog timer to a reasonable value to complete the
  999. panic, if the watchdog timer is running. Plus the
  1000. pretimeout is meaningless at panic time. */
  1001. if (watchdog_user && !panic_event_handled &&
  1002. ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
  1003. /* Make sure we do this only once. */
  1004. panic_event_handled = 1;
  1005. timeout = panic_wdt_timeout;
  1006. pretimeout = 0;
  1007. panic_halt_ipmi_set_timeout();
  1008. }
  1009. return NOTIFY_OK;
  1010. }
  1011. static struct notifier_block wdog_panic_notifier = {
  1012. .notifier_call = wdog_panic_handler,
  1013. .next = NULL,
  1014. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  1015. };
  1016. static void ipmi_new_smi(int if_num, struct device *device)
  1017. {
  1018. ipmi_register_watchdog(if_num);
  1019. }
  1020. static void ipmi_smi_gone(int if_num)
  1021. {
  1022. ipmi_unregister_watchdog(if_num);
  1023. }
  1024. static struct ipmi_smi_watcher smi_watcher = {
  1025. .owner = THIS_MODULE,
  1026. .new_smi = ipmi_new_smi,
  1027. .smi_gone = ipmi_smi_gone
  1028. };
  1029. static int action_op(const char *inval, char *outval)
  1030. {
  1031. if (outval)
  1032. strcpy(outval, action);
  1033. if (!inval)
  1034. return 0;
  1035. if (strcmp(inval, "reset") == 0)
  1036. action_val = WDOG_TIMEOUT_RESET;
  1037. else if (strcmp(inval, "none") == 0)
  1038. action_val = WDOG_TIMEOUT_NONE;
  1039. else if (strcmp(inval, "power_cycle") == 0)
  1040. action_val = WDOG_TIMEOUT_POWER_CYCLE;
  1041. else if (strcmp(inval, "power_off") == 0)
  1042. action_val = WDOG_TIMEOUT_POWER_DOWN;
  1043. else
  1044. return -EINVAL;
  1045. strcpy(action, inval);
  1046. return 0;
  1047. }
  1048. static int preaction_op(const char *inval, char *outval)
  1049. {
  1050. if (outval)
  1051. strcpy(outval, preaction);
  1052. if (!inval)
  1053. return 0;
  1054. if (strcmp(inval, "pre_none") == 0)
  1055. preaction_val = WDOG_PRETIMEOUT_NONE;
  1056. else if (strcmp(inval, "pre_smi") == 0)
  1057. preaction_val = WDOG_PRETIMEOUT_SMI;
  1058. #ifdef HAVE_DIE_NMI
  1059. else if (strcmp(inval, "pre_nmi") == 0)
  1060. preaction_val = WDOG_PRETIMEOUT_NMI;
  1061. #endif
  1062. else if (strcmp(inval, "pre_int") == 0)
  1063. preaction_val = WDOG_PRETIMEOUT_MSG_INT;
  1064. else
  1065. return -EINVAL;
  1066. strcpy(preaction, inval);
  1067. return 0;
  1068. }
  1069. static int preop_op(const char *inval, char *outval)
  1070. {
  1071. if (outval)
  1072. strcpy(outval, preop);
  1073. if (!inval)
  1074. return 0;
  1075. if (strcmp(inval, "preop_none") == 0)
  1076. preop_val = WDOG_PREOP_NONE;
  1077. else if (strcmp(inval, "preop_panic") == 0)
  1078. preop_val = WDOG_PREOP_PANIC;
  1079. else if (strcmp(inval, "preop_give_data") == 0)
  1080. preop_val = WDOG_PREOP_GIVE_DATA;
  1081. else
  1082. return -EINVAL;
  1083. strcpy(preop, inval);
  1084. return 0;
  1085. }
  1086. static void check_parms(void)
  1087. {
  1088. #ifdef HAVE_DIE_NMI
  1089. int do_nmi = 0;
  1090. int rv;
  1091. if (preaction_val == WDOG_PRETIMEOUT_NMI) {
  1092. do_nmi = 1;
  1093. if (preop_val == WDOG_PREOP_GIVE_DATA) {
  1094. printk(KERN_WARNING PFX "Pretimeout op is to give data"
  1095. " but NMI pretimeout is enabled, setting"
  1096. " pretimeout op to none\n");
  1097. preop_op("preop_none", NULL);
  1098. do_nmi = 0;
  1099. }
  1100. }
  1101. if (do_nmi && !nmi_handler_registered) {
  1102. rv = register_nmi_handler(NMI_UNKNOWN, ipmi_nmi, 0,
  1103. "ipmi");
  1104. if (rv) {
  1105. printk(KERN_WARNING PFX
  1106. "Can't register nmi handler\n");
  1107. return;
  1108. } else
  1109. nmi_handler_registered = 1;
  1110. } else if (!do_nmi && nmi_handler_registered) {
  1111. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1112. nmi_handler_registered = 0;
  1113. }
  1114. #endif
  1115. }
  1116. static int __init ipmi_wdog_init(void)
  1117. {
  1118. int rv;
  1119. if (action_op(action, NULL)) {
  1120. action_op("reset", NULL);
  1121. printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
  1122. " reset\n", action);
  1123. }
  1124. if (preaction_op(preaction, NULL)) {
  1125. preaction_op("pre_none", NULL);
  1126. printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
  1127. " none\n", preaction);
  1128. }
  1129. if (preop_op(preop, NULL)) {
  1130. preop_op("preop_none", NULL);
  1131. printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
  1132. " none\n", preop);
  1133. }
  1134. check_parms();
  1135. register_reboot_notifier(&wdog_reboot_notifier);
  1136. atomic_notifier_chain_register(&panic_notifier_list,
  1137. &wdog_panic_notifier);
  1138. rv = ipmi_smi_watcher_register(&smi_watcher);
  1139. if (rv) {
  1140. #ifdef HAVE_DIE_NMI
  1141. if (nmi_handler_registered)
  1142. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1143. #endif
  1144. atomic_notifier_chain_unregister(&panic_notifier_list,
  1145. &wdog_panic_notifier);
  1146. unregister_reboot_notifier(&wdog_reboot_notifier);
  1147. printk(KERN_WARNING PFX "can't register smi watcher\n");
  1148. return rv;
  1149. }
  1150. printk(KERN_INFO PFX "driver initialized\n");
  1151. return 0;
  1152. }
  1153. static void __exit ipmi_wdog_exit(void)
  1154. {
  1155. ipmi_smi_watcher_unregister(&smi_watcher);
  1156. ipmi_unregister_watchdog(watchdog_ifnum);
  1157. #ifdef HAVE_DIE_NMI
  1158. if (nmi_handler_registered)
  1159. unregister_nmi_handler(NMI_UNKNOWN, "ipmi");
  1160. #endif
  1161. atomic_notifier_chain_unregister(&panic_notifier_list,
  1162. &wdog_panic_notifier);
  1163. unregister_reboot_notifier(&wdog_reboot_notifier);
  1164. }
  1165. module_exit(ipmi_wdog_exit);
  1166. module_init(ipmi_wdog_init);
  1167. MODULE_LICENSE("GPL");
  1168. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  1169. MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");