ipmi_watchdog.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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/config.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/ipmi.h>
  37. #include <linux/ipmi_smi.h>
  38. #include <linux/watchdog.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/init.h>
  41. #include <linux/rwsem.h>
  42. #include <linux/errno.h>
  43. #include <asm/uaccess.h>
  44. #include <linux/notifier.h>
  45. #include <linux/nmi.h>
  46. #include <linux/reboot.h>
  47. #include <linux/wait.h>
  48. #include <linux/poll.h>
  49. #ifdef CONFIG_X86_LOCAL_APIC
  50. #include <asm/apic.h>
  51. #endif
  52. #define PFX "IPMI Watchdog: "
  53. /*
  54. * The IPMI command/response information for the watchdog timer.
  55. */
  56. /* values for byte 1 of the set command, byte 2 of the get response. */
  57. #define WDOG_DONT_LOG (1 << 7)
  58. #define WDOG_DONT_STOP_ON_SET (1 << 6)
  59. #define WDOG_SET_TIMER_USE(byte, use) \
  60. byte = ((byte) & 0xf8) | ((use) & 0x7)
  61. #define WDOG_GET_TIMER_USE(byte) ((byte) & 0x7)
  62. #define WDOG_TIMER_USE_BIOS_FRB2 1
  63. #define WDOG_TIMER_USE_BIOS_POST 2
  64. #define WDOG_TIMER_USE_OS_LOAD 3
  65. #define WDOG_TIMER_USE_SMS_OS 4
  66. #define WDOG_TIMER_USE_OEM 5
  67. /* values for byte 2 of the set command, byte 3 of the get response. */
  68. #define WDOG_SET_PRETIMEOUT_ACT(byte, use) \
  69. byte = ((byte) & 0x8f) | (((use) & 0x7) << 4)
  70. #define WDOG_GET_PRETIMEOUT_ACT(byte) (((byte) >> 4) & 0x7)
  71. #define WDOG_PRETIMEOUT_NONE 0
  72. #define WDOG_PRETIMEOUT_SMI 1
  73. #define WDOG_PRETIMEOUT_NMI 2
  74. #define WDOG_PRETIMEOUT_MSG_INT 3
  75. /* Operations that can be performed on a pretimout. */
  76. #define WDOG_PREOP_NONE 0
  77. #define WDOG_PREOP_PANIC 1
  78. #define WDOG_PREOP_GIVE_DATA 2 /* Cause data to be available to
  79. read. Doesn't work in NMI
  80. mode. */
  81. /* Actions to perform on a full timeout. */
  82. #define WDOG_SET_TIMEOUT_ACT(byte, use) \
  83. byte = ((byte) & 0xf8) | ((use) & 0x7)
  84. #define WDOG_GET_TIMEOUT_ACT(byte) ((byte) & 0x7)
  85. #define WDOG_TIMEOUT_NONE 0
  86. #define WDOG_TIMEOUT_RESET 1
  87. #define WDOG_TIMEOUT_POWER_DOWN 2
  88. #define WDOG_TIMEOUT_POWER_CYCLE 3
  89. /* Byte 3 of the get command, byte 4 of the get response is the
  90. pre-timeout in seconds. */
  91. /* Bits for setting byte 4 of the set command, byte 5 of the get response. */
  92. #define WDOG_EXPIRE_CLEAR_BIOS_FRB2 (1 << 1)
  93. #define WDOG_EXPIRE_CLEAR_BIOS_POST (1 << 2)
  94. #define WDOG_EXPIRE_CLEAR_OS_LOAD (1 << 3)
  95. #define WDOG_EXPIRE_CLEAR_SMS_OS (1 << 4)
  96. #define WDOG_EXPIRE_CLEAR_OEM (1 << 5)
  97. /* Setting/getting the watchdog timer value. This is for bytes 5 and
  98. 6 (the timeout time) of the set command, and bytes 6 and 7 (the
  99. timeout time) and 8 and 9 (the current countdown value) of the
  100. response. The timeout value is given in seconds (in the command it
  101. is 100ms intervals). */
  102. #define WDOG_SET_TIMEOUT(byte1, byte2, val) \
  103. (byte1) = (((val) * 10) & 0xff), (byte2) = (((val) * 10) >> 8)
  104. #define WDOG_GET_TIMEOUT(byte1, byte2) \
  105. (((byte1) | ((byte2) << 8)) / 10)
  106. #define IPMI_WDOG_RESET_TIMER 0x22
  107. #define IPMI_WDOG_SET_TIMER 0x24
  108. #define IPMI_WDOG_GET_TIMER 0x25
  109. /* These are here until the real ones get into the watchdog.h interface. */
  110. #ifndef WDIOC_GETTIMEOUT
  111. #define WDIOC_GETTIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 20, int)
  112. #endif
  113. #ifndef WDIOC_SET_PRETIMEOUT
  114. #define WDIOC_SET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 21, int)
  115. #endif
  116. #ifndef WDIOC_GET_PRETIMEOUT
  117. #define WDIOC_GET_PRETIMEOUT _IOW(WATCHDOG_IOCTL_BASE, 22, int)
  118. #endif
  119. static int nowayout = WATCHDOG_NOWAYOUT;
  120. static ipmi_user_t watchdog_user = NULL;
  121. /* Default the timeout to 10 seconds. */
  122. static int timeout = 10;
  123. /* The pre-timeout is disabled by default. */
  124. static int pretimeout = 0;
  125. /* Default action is to reset the board on a timeout. */
  126. static unsigned char action_val = WDOG_TIMEOUT_RESET;
  127. static char action[16] = "reset";
  128. static unsigned char preaction_val = WDOG_PRETIMEOUT_NONE;
  129. static char preaction[16] = "pre_none";
  130. static unsigned char preop_val = WDOG_PREOP_NONE;
  131. static char preop[16] = "preop_none";
  132. static DEFINE_SPINLOCK(ipmi_read_lock);
  133. static char data_to_read = 0;
  134. static DECLARE_WAIT_QUEUE_HEAD(read_q);
  135. static struct fasync_struct *fasync_q = NULL;
  136. static char pretimeout_since_last_heartbeat = 0;
  137. static char expect_close;
  138. /* If true, the driver will start running as soon as it is configured
  139. and ready. */
  140. static int start_now = 0;
  141. module_param(timeout, int, 0);
  142. MODULE_PARM_DESC(timeout, "Timeout value in seconds.");
  143. module_param(pretimeout, int, 0);
  144. MODULE_PARM_DESC(pretimeout, "Pretimeout value in seconds.");
  145. module_param_string(action, action, sizeof(action), 0);
  146. MODULE_PARM_DESC(action, "Timeout action. One of: "
  147. "reset, none, power_cycle, power_off.");
  148. module_param_string(preaction, preaction, sizeof(preaction), 0);
  149. MODULE_PARM_DESC(preaction, "Pretimeout action. One of: "
  150. "pre_none, pre_smi, pre_nmi, pre_int.");
  151. module_param_string(preop, preop, sizeof(preop), 0);
  152. MODULE_PARM_DESC(preop, "Pretimeout driver operation. One of: "
  153. "preop_none, preop_panic, preop_give_data.");
  154. module_param(start_now, int, 0);
  155. MODULE_PARM_DESC(start_now, "Set to 1 to start the watchdog as"
  156. "soon as the driver is loaded.");
  157. module_param(nowayout, int, 0);
  158. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  159. /* Default state of the timer. */
  160. static unsigned char ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  161. /* If shutting down via IPMI, we ignore the heartbeat. */
  162. static int ipmi_ignore_heartbeat = 0;
  163. /* Is someone using the watchdog? Only one user is allowed. */
  164. static unsigned long ipmi_wdog_open = 0;
  165. /* If set to 1, the heartbeat command will set the state to reset and
  166. start the timer. The timer doesn't normally run when the driver is
  167. first opened until the heartbeat is set the first time, this
  168. variable is used to accomplish this. */
  169. static int ipmi_start_timer_on_heartbeat = 0;
  170. /* IPMI version of the BMC. */
  171. static unsigned char ipmi_version_major;
  172. static unsigned char ipmi_version_minor;
  173. static int ipmi_heartbeat(void);
  174. static void panic_halt_ipmi_heartbeat(void);
  175. /* We use a semaphore to make sure that only one thing can send a set
  176. timeout at one time, because we only have one copy of the data.
  177. The semaphore is claimed when the set_timeout is sent and freed
  178. when both messages are free. */
  179. static atomic_t set_timeout_tofree = ATOMIC_INIT(0);
  180. static DECLARE_MUTEX(set_timeout_lock);
  181. static void set_timeout_free_smi(struct ipmi_smi_msg *msg)
  182. {
  183. if (atomic_dec_and_test(&set_timeout_tofree))
  184. up(&set_timeout_lock);
  185. }
  186. static void set_timeout_free_recv(struct ipmi_recv_msg *msg)
  187. {
  188. if (atomic_dec_and_test(&set_timeout_tofree))
  189. up(&set_timeout_lock);
  190. }
  191. static struct ipmi_smi_msg set_timeout_smi_msg =
  192. {
  193. .done = set_timeout_free_smi
  194. };
  195. static struct ipmi_recv_msg set_timeout_recv_msg =
  196. {
  197. .done = set_timeout_free_recv
  198. };
  199. static int i_ipmi_set_timeout(struct ipmi_smi_msg *smi_msg,
  200. struct ipmi_recv_msg *recv_msg,
  201. int *send_heartbeat_now)
  202. {
  203. struct kernel_ipmi_msg msg;
  204. unsigned char data[6];
  205. int rv;
  206. struct ipmi_system_interface_addr addr;
  207. int hbnow = 0;
  208. data[0] = 0;
  209. WDOG_SET_TIMER_USE(data[0], WDOG_TIMER_USE_SMS_OS);
  210. if ((ipmi_version_major > 1)
  211. || ((ipmi_version_major == 1) && (ipmi_version_minor >= 5)))
  212. {
  213. /* This is an IPMI 1.5-only feature. */
  214. data[0] |= WDOG_DONT_STOP_ON_SET;
  215. } else if (ipmi_watchdog_state != WDOG_TIMEOUT_NONE) {
  216. /* In ipmi 1.0, setting the timer stops the watchdog, we
  217. need to start it back up again. */
  218. hbnow = 1;
  219. }
  220. data[1] = 0;
  221. WDOG_SET_TIMEOUT_ACT(data[1], ipmi_watchdog_state);
  222. if ((pretimeout > 0) && (ipmi_watchdog_state != WDOG_TIMEOUT_NONE)) {
  223. WDOG_SET_PRETIMEOUT_ACT(data[1], preaction_val);
  224. data[2] = pretimeout;
  225. } else {
  226. WDOG_SET_PRETIMEOUT_ACT(data[1], WDOG_PRETIMEOUT_NONE);
  227. data[2] = 0; /* No pretimeout. */
  228. }
  229. data[3] = 0;
  230. WDOG_SET_TIMEOUT(data[4], data[5], timeout);
  231. addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  232. addr.channel = IPMI_BMC_CHANNEL;
  233. addr.lun = 0;
  234. msg.netfn = 0x06;
  235. msg.cmd = IPMI_WDOG_SET_TIMER;
  236. msg.data = data;
  237. msg.data_len = sizeof(data);
  238. rv = ipmi_request_supply_msgs(watchdog_user,
  239. (struct ipmi_addr *) &addr,
  240. 0,
  241. &msg,
  242. NULL,
  243. smi_msg,
  244. recv_msg,
  245. 1);
  246. if (rv) {
  247. printk(KERN_WARNING PFX "set timeout error: %d\n",
  248. rv);
  249. }
  250. if (send_heartbeat_now)
  251. *send_heartbeat_now = hbnow;
  252. return rv;
  253. }
  254. /* Parameters to ipmi_set_timeout */
  255. #define IPMI_SET_TIMEOUT_NO_HB 0
  256. #define IPMI_SET_TIMEOUT_HB_IF_NECESSARY 1
  257. #define IPMI_SET_TIMEOUT_FORCE_HB 2
  258. static int ipmi_set_timeout(int do_heartbeat)
  259. {
  260. int send_heartbeat_now;
  261. int rv;
  262. /* We can only send one of these at a time. */
  263. down(&set_timeout_lock);
  264. atomic_set(&set_timeout_tofree, 2);
  265. rv = i_ipmi_set_timeout(&set_timeout_smi_msg,
  266. &set_timeout_recv_msg,
  267. &send_heartbeat_now);
  268. if (rv) {
  269. up(&set_timeout_lock);
  270. } else {
  271. if ((do_heartbeat == IPMI_SET_TIMEOUT_FORCE_HB)
  272. || ((send_heartbeat_now)
  273. && (do_heartbeat == IPMI_SET_TIMEOUT_HB_IF_NECESSARY)))
  274. {
  275. rv = ipmi_heartbeat();
  276. }
  277. }
  278. return rv;
  279. }
  280. static void dummy_smi_free(struct ipmi_smi_msg *msg)
  281. {
  282. }
  283. static void dummy_recv_free(struct ipmi_recv_msg *msg)
  284. {
  285. }
  286. static struct ipmi_smi_msg panic_halt_smi_msg =
  287. {
  288. .done = dummy_smi_free
  289. };
  290. static struct ipmi_recv_msg panic_halt_recv_msg =
  291. {
  292. .done = dummy_recv_free
  293. };
  294. /* Special call, doesn't claim any locks. This is only to be called
  295. at panic or halt time, in run-to-completion mode, when the caller
  296. is the only CPU and the only thing that will be going is these IPMI
  297. calls. */
  298. static void panic_halt_ipmi_set_timeout(void)
  299. {
  300. int send_heartbeat_now;
  301. int rv;
  302. rv = i_ipmi_set_timeout(&panic_halt_smi_msg,
  303. &panic_halt_recv_msg,
  304. &send_heartbeat_now);
  305. if (!rv) {
  306. if (send_heartbeat_now)
  307. panic_halt_ipmi_heartbeat();
  308. }
  309. }
  310. /* We use a semaphore to make sure that only one thing can send a
  311. heartbeat at one time, because we only have one copy of the data.
  312. The semaphore is claimed when the set_timeout is sent and freed
  313. when both messages are free. */
  314. static atomic_t heartbeat_tofree = ATOMIC_INIT(0);
  315. static DECLARE_MUTEX(heartbeat_lock);
  316. static DECLARE_MUTEX_LOCKED(heartbeat_wait_lock);
  317. static void heartbeat_free_smi(struct ipmi_smi_msg *msg)
  318. {
  319. if (atomic_dec_and_test(&heartbeat_tofree))
  320. up(&heartbeat_wait_lock);
  321. }
  322. static void heartbeat_free_recv(struct ipmi_recv_msg *msg)
  323. {
  324. if (atomic_dec_and_test(&heartbeat_tofree))
  325. up(&heartbeat_wait_lock);
  326. }
  327. static struct ipmi_smi_msg heartbeat_smi_msg =
  328. {
  329. .done = heartbeat_free_smi
  330. };
  331. static struct ipmi_recv_msg heartbeat_recv_msg =
  332. {
  333. .done = heartbeat_free_recv
  334. };
  335. static struct ipmi_smi_msg panic_halt_heartbeat_smi_msg =
  336. {
  337. .done = dummy_smi_free
  338. };
  339. static struct ipmi_recv_msg panic_halt_heartbeat_recv_msg =
  340. {
  341. .done = dummy_recv_free
  342. };
  343. static int ipmi_heartbeat(void)
  344. {
  345. struct kernel_ipmi_msg msg;
  346. int rv;
  347. struct ipmi_system_interface_addr addr;
  348. if (ipmi_ignore_heartbeat) {
  349. return 0;
  350. }
  351. if (ipmi_start_timer_on_heartbeat) {
  352. ipmi_start_timer_on_heartbeat = 0;
  353. ipmi_watchdog_state = action_val;
  354. return ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  355. } else if (pretimeout_since_last_heartbeat) {
  356. /* A pretimeout occurred, make sure we set the timeout.
  357. We don't want to set the action, though, we want to
  358. leave that alone (thus it can't be combined with the
  359. above operation. */
  360. pretimeout_since_last_heartbeat = 0;
  361. return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
  362. }
  363. down(&heartbeat_lock);
  364. atomic_set(&heartbeat_tofree, 2);
  365. /* Don't reset the timer if we have the timer turned off, that
  366. re-enables the watchdog. */
  367. if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE) {
  368. up(&heartbeat_lock);
  369. return 0;
  370. }
  371. addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  372. addr.channel = IPMI_BMC_CHANNEL;
  373. addr.lun = 0;
  374. msg.netfn = 0x06;
  375. msg.cmd = IPMI_WDOG_RESET_TIMER;
  376. msg.data = NULL;
  377. msg.data_len = 0;
  378. rv = ipmi_request_supply_msgs(watchdog_user,
  379. (struct ipmi_addr *) &addr,
  380. 0,
  381. &msg,
  382. NULL,
  383. &heartbeat_smi_msg,
  384. &heartbeat_recv_msg,
  385. 1);
  386. if (rv) {
  387. up(&heartbeat_lock);
  388. printk(KERN_WARNING PFX "heartbeat failure: %d\n",
  389. rv);
  390. return rv;
  391. }
  392. /* Wait for the heartbeat to be sent. */
  393. down(&heartbeat_wait_lock);
  394. if (heartbeat_recv_msg.msg.data[0] != 0) {
  395. /* Got an error in the heartbeat response. It was already
  396. reported in ipmi_wdog_msg_handler, but we should return
  397. an error here. */
  398. rv = -EINVAL;
  399. }
  400. up(&heartbeat_lock);
  401. return rv;
  402. }
  403. static void panic_halt_ipmi_heartbeat(void)
  404. {
  405. struct kernel_ipmi_msg msg;
  406. struct ipmi_system_interface_addr addr;
  407. /* Don't reset the timer if we have the timer turned off, that
  408. re-enables the watchdog. */
  409. if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
  410. return;
  411. addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  412. addr.channel = IPMI_BMC_CHANNEL;
  413. addr.lun = 0;
  414. msg.netfn = 0x06;
  415. msg.cmd = IPMI_WDOG_RESET_TIMER;
  416. msg.data = NULL;
  417. msg.data_len = 0;
  418. ipmi_request_supply_msgs(watchdog_user,
  419. (struct ipmi_addr *) &addr,
  420. 0,
  421. &msg,
  422. NULL,
  423. &panic_halt_heartbeat_smi_msg,
  424. &panic_halt_heartbeat_recv_msg,
  425. 1);
  426. }
  427. static struct watchdog_info ident=
  428. {
  429. .options = 0, /* WDIOF_SETTIMEOUT, */
  430. .firmware_version = 1,
  431. .identity = "IPMI"
  432. };
  433. static int ipmi_ioctl(struct inode *inode, struct file *file,
  434. unsigned int cmd, unsigned long arg)
  435. {
  436. void __user *argp = (void __user *)arg;
  437. int i;
  438. int val;
  439. switch(cmd) {
  440. case WDIOC_GETSUPPORT:
  441. i = copy_to_user(argp, &ident, sizeof(ident));
  442. return i ? -EFAULT : 0;
  443. case WDIOC_SETTIMEOUT:
  444. i = copy_from_user(&val, argp, sizeof(int));
  445. if (i)
  446. return -EFAULT;
  447. timeout = val;
  448. return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
  449. case WDIOC_GETTIMEOUT:
  450. i = copy_to_user(argp, &timeout, sizeof(timeout));
  451. if (i)
  452. return -EFAULT;
  453. return 0;
  454. case WDIOC_SET_PRETIMEOUT:
  455. i = copy_from_user(&val, argp, sizeof(int));
  456. if (i)
  457. return -EFAULT;
  458. pretimeout = val;
  459. return ipmi_set_timeout(IPMI_SET_TIMEOUT_HB_IF_NECESSARY);
  460. case WDIOC_GET_PRETIMEOUT:
  461. i = copy_to_user(argp, &pretimeout, sizeof(pretimeout));
  462. if (i)
  463. return -EFAULT;
  464. return 0;
  465. case WDIOC_KEEPALIVE:
  466. return ipmi_heartbeat();
  467. case WDIOC_SETOPTIONS:
  468. i = copy_from_user(&val, argp, sizeof(int));
  469. if (i)
  470. return -EFAULT;
  471. if (val & WDIOS_DISABLECARD)
  472. {
  473. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  474. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  475. ipmi_start_timer_on_heartbeat = 0;
  476. }
  477. if (val & WDIOS_ENABLECARD)
  478. {
  479. ipmi_watchdog_state = action_val;
  480. ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  481. }
  482. return 0;
  483. case WDIOC_GETSTATUS:
  484. val = 0;
  485. i = copy_to_user(argp, &val, sizeof(val));
  486. if (i)
  487. return -EFAULT;
  488. return 0;
  489. default:
  490. return -ENOIOCTLCMD;
  491. }
  492. }
  493. static ssize_t ipmi_write(struct file *file,
  494. const char __user *buf,
  495. size_t len,
  496. loff_t *ppos)
  497. {
  498. int rv;
  499. if (len) {
  500. if (!nowayout) {
  501. size_t i;
  502. /* In case it was set long ago */
  503. expect_close = 0;
  504. for (i = 0; i != len; i++) {
  505. char c;
  506. if (get_user(c, buf + i))
  507. return -EFAULT;
  508. if (c == 'V')
  509. expect_close = 42;
  510. }
  511. }
  512. rv = ipmi_heartbeat();
  513. if (rv)
  514. return rv;
  515. return 1;
  516. }
  517. return 0;
  518. }
  519. static ssize_t ipmi_read(struct file *file,
  520. char __user *buf,
  521. size_t count,
  522. loff_t *ppos)
  523. {
  524. int rv = 0;
  525. wait_queue_t wait;
  526. if (count <= 0)
  527. return 0;
  528. /* Reading returns if the pretimeout has gone off, and it only does
  529. it once per pretimeout. */
  530. spin_lock(&ipmi_read_lock);
  531. if (!data_to_read) {
  532. if (file->f_flags & O_NONBLOCK) {
  533. rv = -EAGAIN;
  534. goto out;
  535. }
  536. init_waitqueue_entry(&wait, current);
  537. add_wait_queue(&read_q, &wait);
  538. while (!data_to_read) {
  539. set_current_state(TASK_INTERRUPTIBLE);
  540. spin_unlock(&ipmi_read_lock);
  541. schedule();
  542. spin_lock(&ipmi_read_lock);
  543. }
  544. remove_wait_queue(&read_q, &wait);
  545. if (signal_pending(current)) {
  546. rv = -ERESTARTSYS;
  547. goto out;
  548. }
  549. }
  550. data_to_read = 0;
  551. out:
  552. spin_unlock(&ipmi_read_lock);
  553. if (rv == 0) {
  554. if (copy_to_user(buf, &data_to_read, 1))
  555. rv = -EFAULT;
  556. else
  557. rv = 1;
  558. }
  559. return rv;
  560. }
  561. static int ipmi_open(struct inode *ino, struct file *filep)
  562. {
  563. switch (iminor(ino)) {
  564. case WATCHDOG_MINOR:
  565. if (test_and_set_bit(0, &ipmi_wdog_open))
  566. return -EBUSY;
  567. /* Don't start the timer now, let it start on the
  568. first heartbeat. */
  569. ipmi_start_timer_on_heartbeat = 1;
  570. return nonseekable_open(ino, filep);
  571. default:
  572. return (-ENODEV);
  573. }
  574. }
  575. static unsigned int ipmi_poll(struct file *file, poll_table *wait)
  576. {
  577. unsigned int mask = 0;
  578. poll_wait(file, &read_q, wait);
  579. spin_lock(&ipmi_read_lock);
  580. if (data_to_read)
  581. mask |= (POLLIN | POLLRDNORM);
  582. spin_unlock(&ipmi_read_lock);
  583. return mask;
  584. }
  585. static int ipmi_fasync(int fd, struct file *file, int on)
  586. {
  587. int result;
  588. result = fasync_helper(fd, file, on, &fasync_q);
  589. return (result);
  590. }
  591. static int ipmi_close(struct inode *ino, struct file *filep)
  592. {
  593. if (iminor(ino)==WATCHDOG_MINOR)
  594. {
  595. if (expect_close == 42) {
  596. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  597. ipmi_set_timeout(IPMI_SET_TIMEOUT_NO_HB);
  598. } else {
  599. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  600. ipmi_heartbeat();
  601. }
  602. clear_bit(0, &ipmi_wdog_open);
  603. }
  604. ipmi_fasync (-1, filep, 0);
  605. expect_close = 0;
  606. return 0;
  607. }
  608. static struct file_operations ipmi_wdog_fops = {
  609. .owner = THIS_MODULE,
  610. .read = ipmi_read,
  611. .poll = ipmi_poll,
  612. .write = ipmi_write,
  613. .ioctl = ipmi_ioctl,
  614. .open = ipmi_open,
  615. .release = ipmi_close,
  616. .fasync = ipmi_fasync,
  617. };
  618. static struct miscdevice ipmi_wdog_miscdev = {
  619. .minor = WATCHDOG_MINOR,
  620. .name = "watchdog",
  621. .fops = &ipmi_wdog_fops
  622. };
  623. static DECLARE_RWSEM(register_sem);
  624. static void ipmi_wdog_msg_handler(struct ipmi_recv_msg *msg,
  625. void *handler_data)
  626. {
  627. if (msg->msg.data[0] != 0) {
  628. printk(KERN_ERR PFX "response: Error %x on cmd %x\n",
  629. msg->msg.data[0],
  630. msg->msg.cmd);
  631. }
  632. ipmi_free_recv_msg(msg);
  633. }
  634. static void ipmi_wdog_pretimeout_handler(void *handler_data)
  635. {
  636. if (preaction_val != WDOG_PRETIMEOUT_NONE) {
  637. if (preop_val == WDOG_PREOP_PANIC)
  638. panic("Watchdog pre-timeout");
  639. else if (preop_val == WDOG_PREOP_GIVE_DATA) {
  640. spin_lock(&ipmi_read_lock);
  641. data_to_read = 1;
  642. wake_up_interruptible(&read_q);
  643. kill_fasync(&fasync_q, SIGIO, POLL_IN);
  644. spin_unlock(&ipmi_read_lock);
  645. }
  646. }
  647. /* On some machines, the heartbeat will give
  648. an error and not work unless we re-enable
  649. the timer. So do so. */
  650. pretimeout_since_last_heartbeat = 1;
  651. }
  652. static struct ipmi_user_hndl ipmi_hndlrs =
  653. {
  654. .ipmi_recv_hndl = ipmi_wdog_msg_handler,
  655. .ipmi_watchdog_pretimeout = ipmi_wdog_pretimeout_handler
  656. };
  657. static void ipmi_register_watchdog(int ipmi_intf)
  658. {
  659. int rv = -EBUSY;
  660. down_write(&register_sem);
  661. if (watchdog_user)
  662. goto out;
  663. rv = ipmi_create_user(ipmi_intf, &ipmi_hndlrs, NULL, &watchdog_user);
  664. if (rv < 0) {
  665. printk(KERN_CRIT PFX "Unable to register with ipmi\n");
  666. goto out;
  667. }
  668. ipmi_get_version(watchdog_user,
  669. &ipmi_version_major,
  670. &ipmi_version_minor);
  671. rv = misc_register(&ipmi_wdog_miscdev);
  672. if (rv < 0) {
  673. ipmi_destroy_user(watchdog_user);
  674. watchdog_user = NULL;
  675. printk(KERN_CRIT PFX "Unable to register misc device\n");
  676. }
  677. out:
  678. up_write(&register_sem);
  679. if ((start_now) && (rv == 0)) {
  680. /* Run from startup, so start the timer now. */
  681. start_now = 0; /* Disable this function after first startup. */
  682. ipmi_watchdog_state = action_val;
  683. ipmi_set_timeout(IPMI_SET_TIMEOUT_FORCE_HB);
  684. printk(KERN_INFO PFX "Starting now!\n");
  685. }
  686. }
  687. #ifdef HAVE_NMI_HANDLER
  688. static int
  689. ipmi_nmi(void *dev_id, struct pt_regs *regs, int cpu, int handled)
  690. {
  691. /* If we are not expecting a timeout, ignore it. */
  692. if (ipmi_watchdog_state == WDOG_TIMEOUT_NONE)
  693. return NOTIFY_DONE;
  694. /* If no one else handled the NMI, we assume it was the IPMI
  695. watchdog. */
  696. if ((!handled) && (preop_val == WDOG_PREOP_PANIC)) {
  697. /* On some machines, the heartbeat will give
  698. an error and not work unless we re-enable
  699. the timer. So do so. */
  700. pretimeout_since_last_heartbeat = 1;
  701. panic(PFX "pre-timeout");
  702. }
  703. return NOTIFY_DONE;
  704. }
  705. static struct nmi_handler ipmi_nmi_handler =
  706. {
  707. .link = LIST_HEAD_INIT(ipmi_nmi_handler.link),
  708. .dev_name = "ipmi_watchdog",
  709. .dev_id = NULL,
  710. .handler = ipmi_nmi,
  711. .priority = 0, /* Call us last. */
  712. };
  713. #endif
  714. static int wdog_reboot_handler(struct notifier_block *this,
  715. unsigned long code,
  716. void *unused)
  717. {
  718. static int reboot_event_handled = 0;
  719. if ((watchdog_user) && (!reboot_event_handled)) {
  720. /* Make sure we only do this once. */
  721. reboot_event_handled = 1;
  722. if (code == SYS_DOWN || code == SYS_HALT) {
  723. /* Disable the WDT if we are shutting down. */
  724. ipmi_watchdog_state = WDOG_TIMEOUT_NONE;
  725. panic_halt_ipmi_set_timeout();
  726. } else {
  727. /* Set a long timer to let the reboot happens, but
  728. reboot if it hangs. */
  729. timeout = 120;
  730. pretimeout = 0;
  731. ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
  732. panic_halt_ipmi_set_timeout();
  733. }
  734. }
  735. return NOTIFY_OK;
  736. }
  737. static struct notifier_block wdog_reboot_notifier = {
  738. .notifier_call = wdog_reboot_handler,
  739. .next = NULL,
  740. .priority = 0
  741. };
  742. static int wdog_panic_handler(struct notifier_block *this,
  743. unsigned long event,
  744. void *unused)
  745. {
  746. static int panic_event_handled = 0;
  747. /* On a panic, if we have a panic timeout, make sure that the thing
  748. reboots, even if it hangs during that panic. */
  749. if (watchdog_user && !panic_event_handled) {
  750. /* Make sure the panic doesn't hang, and make sure we
  751. do this only once. */
  752. panic_event_handled = 1;
  753. timeout = 255;
  754. pretimeout = 0;
  755. ipmi_watchdog_state = WDOG_TIMEOUT_RESET;
  756. panic_halt_ipmi_set_timeout();
  757. }
  758. return NOTIFY_OK;
  759. }
  760. static struct notifier_block wdog_panic_notifier = {
  761. .notifier_call = wdog_panic_handler,
  762. .next = NULL,
  763. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  764. };
  765. static void ipmi_new_smi(int if_num)
  766. {
  767. ipmi_register_watchdog(if_num);
  768. }
  769. static void ipmi_smi_gone(int if_num)
  770. {
  771. /* This can never be called, because once the watchdog is
  772. registered, the interface can't go away until the watchdog
  773. is unregistered. */
  774. }
  775. static struct ipmi_smi_watcher smi_watcher =
  776. {
  777. .owner = THIS_MODULE,
  778. .new_smi = ipmi_new_smi,
  779. .smi_gone = ipmi_smi_gone
  780. };
  781. static int __init ipmi_wdog_init(void)
  782. {
  783. int rv;
  784. if (strcmp(action, "reset") == 0) {
  785. action_val = WDOG_TIMEOUT_RESET;
  786. } else if (strcmp(action, "none") == 0) {
  787. action_val = WDOG_TIMEOUT_NONE;
  788. } else if (strcmp(action, "power_cycle") == 0) {
  789. action_val = WDOG_TIMEOUT_POWER_CYCLE;
  790. } else if (strcmp(action, "power_off") == 0) {
  791. action_val = WDOG_TIMEOUT_POWER_DOWN;
  792. } else {
  793. action_val = WDOG_TIMEOUT_RESET;
  794. printk(KERN_INFO PFX "Unknown action '%s', defaulting to"
  795. " reset\n", action);
  796. }
  797. if (strcmp(preaction, "pre_none") == 0) {
  798. preaction_val = WDOG_PRETIMEOUT_NONE;
  799. } else if (strcmp(preaction, "pre_smi") == 0) {
  800. preaction_val = WDOG_PRETIMEOUT_SMI;
  801. #ifdef HAVE_NMI_HANDLER
  802. } else if (strcmp(preaction, "pre_nmi") == 0) {
  803. preaction_val = WDOG_PRETIMEOUT_NMI;
  804. #endif
  805. } else if (strcmp(preaction, "pre_int") == 0) {
  806. preaction_val = WDOG_PRETIMEOUT_MSG_INT;
  807. } else {
  808. preaction_val = WDOG_PRETIMEOUT_NONE;
  809. printk(KERN_INFO PFX "Unknown preaction '%s', defaulting to"
  810. " none\n", preaction);
  811. }
  812. if (strcmp(preop, "preop_none") == 0) {
  813. preop_val = WDOG_PREOP_NONE;
  814. } else if (strcmp(preop, "preop_panic") == 0) {
  815. preop_val = WDOG_PREOP_PANIC;
  816. } else if (strcmp(preop, "preop_give_data") == 0) {
  817. preop_val = WDOG_PREOP_GIVE_DATA;
  818. } else {
  819. preop_val = WDOG_PREOP_NONE;
  820. printk(KERN_INFO PFX "Unknown preop '%s', defaulting to"
  821. " none\n", preop);
  822. }
  823. #ifdef HAVE_NMI_HANDLER
  824. if (preaction_val == WDOG_PRETIMEOUT_NMI) {
  825. if (preop_val == WDOG_PREOP_GIVE_DATA) {
  826. printk(KERN_WARNING PFX "Pretimeout op is to give data"
  827. " but NMI pretimeout is enabled, setting"
  828. " pretimeout op to none\n");
  829. preop_val = WDOG_PREOP_NONE;
  830. }
  831. #ifdef CONFIG_X86_LOCAL_APIC
  832. if (nmi_watchdog == NMI_IO_APIC) {
  833. printk(KERN_WARNING PFX "nmi_watchdog is set to IO APIC"
  834. " mode (value is %d), that is incompatible"
  835. " with using NMI in the IPMI watchdog."
  836. " Disabling IPMI nmi pretimeout.\n",
  837. nmi_watchdog);
  838. preaction_val = WDOG_PRETIMEOUT_NONE;
  839. } else {
  840. #endif
  841. rv = request_nmi(&ipmi_nmi_handler);
  842. if (rv) {
  843. printk(KERN_WARNING PFX "Can't register nmi handler\n");
  844. return rv;
  845. }
  846. #ifdef CONFIG_X86_LOCAL_APIC
  847. }
  848. #endif
  849. }
  850. #endif
  851. rv = ipmi_smi_watcher_register(&smi_watcher);
  852. if (rv) {
  853. #ifdef HAVE_NMI_HANDLER
  854. if (preaction_val == WDOG_PRETIMEOUT_NMI)
  855. release_nmi(&ipmi_nmi_handler);
  856. #endif
  857. printk(KERN_WARNING PFX "can't register smi watcher\n");
  858. return rv;
  859. }
  860. register_reboot_notifier(&wdog_reboot_notifier);
  861. notifier_chain_register(&panic_notifier_list, &wdog_panic_notifier);
  862. printk(KERN_INFO PFX "driver initialized\n");
  863. return 0;
  864. }
  865. static __exit void ipmi_unregister_watchdog(void)
  866. {
  867. int rv;
  868. down_write(&register_sem);
  869. #ifdef HAVE_NMI_HANDLER
  870. if (preaction_val == WDOG_PRETIMEOUT_NMI)
  871. release_nmi(&ipmi_nmi_handler);
  872. #endif
  873. notifier_chain_unregister(&panic_notifier_list, &wdog_panic_notifier);
  874. unregister_reboot_notifier(&wdog_reboot_notifier);
  875. if (! watchdog_user)
  876. goto out;
  877. /* Make sure no one can call us any more. */
  878. misc_deregister(&ipmi_wdog_miscdev);
  879. /* Wait to make sure the message makes it out. The lower layer has
  880. pointers to our buffers, we want to make sure they are done before
  881. we release our memory. */
  882. while (atomic_read(&set_timeout_tofree))
  883. schedule_timeout_uninterruptible(1);
  884. /* Disconnect from IPMI. */
  885. rv = ipmi_destroy_user(watchdog_user);
  886. if (rv) {
  887. printk(KERN_WARNING PFX "error unlinking from IPMI: %d\n",
  888. rv);
  889. }
  890. watchdog_user = NULL;
  891. out:
  892. up_write(&register_sem);
  893. }
  894. static void __exit ipmi_wdog_exit(void)
  895. {
  896. ipmi_smi_watcher_unregister(&smi_watcher);
  897. ipmi_unregister_watchdog();
  898. }
  899. module_exit(ipmi_wdog_exit);
  900. module_init(ipmi_wdog_init);
  901. MODULE_LICENSE("GPL");
  902. MODULE_AUTHOR("Corey Minyard <minyard@mvista.com>");
  903. MODULE_DESCRIPTION("watchdog timer based upon the IPMI interface.");