ipmi_watchdog.c 33 KB

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