ipmi_watchdog.c 35 KB

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