ipmi_watchdog.c 34 KB

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