ipmi_watchdog.c 33 KB

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