ipmi_watchdog.c 34 KB

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