wdrtas.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * FIXME: add wdrtas_get_status and wdrtas_get_boot_status as soon as
  3. * RTAS calls are available
  4. */
  5. /*
  6. * RTAS watchdog driver
  7. *
  8. * (C) Copyright IBM Corp. 2005
  9. * device driver to exploit watchdog RTAS functions
  10. *
  11. * Authors : Utz Bacher <utz.bacher@de.ibm.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2, or (at your option)
  16. * any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  28. #include <linux/fs.h>
  29. #include <linux/init.h>
  30. #include <linux/kernel.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/module.h>
  33. #include <linux/notifier.h>
  34. #include <linux/reboot.h>
  35. #include <linux/types.h>
  36. #include <linux/watchdog.h>
  37. #include <linux/uaccess.h>
  38. #include <asm/rtas.h>
  39. #define WDRTAS_MAGIC_CHAR 42
  40. #define WDRTAS_SUPPORTED_MASK (WDIOF_SETTIMEOUT | \
  41. WDIOF_MAGICCLOSE)
  42. MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com>");
  43. MODULE_DESCRIPTION("RTAS watchdog driver");
  44. MODULE_LICENSE("GPL");
  45. static bool wdrtas_nowayout = WATCHDOG_NOWAYOUT;
  46. static atomic_t wdrtas_miscdev_open = ATOMIC_INIT(0);
  47. static char wdrtas_expect_close;
  48. static int wdrtas_interval;
  49. #define WDRTAS_THERMAL_SENSOR 3
  50. static int wdrtas_token_get_sensor_state;
  51. #define WDRTAS_SURVEILLANCE_IND 9000
  52. static int wdrtas_token_set_indicator;
  53. #define WDRTAS_SP_SPI 28
  54. static int wdrtas_token_get_sp;
  55. static int wdrtas_token_event_scan;
  56. #define WDRTAS_DEFAULT_INTERVAL 300
  57. #define WDRTAS_LOGBUFFER_LEN 128
  58. static char wdrtas_logbuffer[WDRTAS_LOGBUFFER_LEN];
  59. /*** watchdog access functions */
  60. /**
  61. * wdrtas_set_interval - sets the watchdog interval
  62. * @interval: new interval
  63. *
  64. * returns 0 on success, <0 on failures
  65. *
  66. * wdrtas_set_interval sets the watchdog keepalive interval by calling the
  67. * RTAS function set-indicator (surveillance). The unit of interval is
  68. * seconds.
  69. */
  70. static int wdrtas_set_interval(int interval)
  71. {
  72. long result;
  73. static int print_msg = 10;
  74. /* rtas uses minutes */
  75. interval = (interval + 59) / 60;
  76. result = rtas_call(wdrtas_token_set_indicator, 3, 1, NULL,
  77. WDRTAS_SURVEILLANCE_IND, 0, interval);
  78. if (result < 0 && print_msg) {
  79. pr_err("setting the watchdog to %i timeout failed: %li\n",
  80. interval, result);
  81. print_msg--;
  82. }
  83. return result;
  84. }
  85. #define WDRTAS_SP_SPI_LEN 4
  86. /**
  87. * wdrtas_get_interval - returns the current watchdog interval
  88. * @fallback_value: value (in seconds) to use, if the RTAS call fails
  89. *
  90. * returns the interval
  91. *
  92. * wdrtas_get_interval returns the current watchdog keepalive interval
  93. * as reported by the RTAS function ibm,get-system-parameter. The unit
  94. * of the return value is seconds.
  95. */
  96. static int wdrtas_get_interval(int fallback_value)
  97. {
  98. long result;
  99. char value[WDRTAS_SP_SPI_LEN];
  100. spin_lock(&rtas_data_buf_lock);
  101. memset(rtas_data_buf, 0, WDRTAS_SP_SPI_LEN);
  102. result = rtas_call(wdrtas_token_get_sp, 3, 1, NULL,
  103. WDRTAS_SP_SPI, __pa(rtas_data_buf),
  104. WDRTAS_SP_SPI_LEN);
  105. memcpy(value, rtas_data_buf, WDRTAS_SP_SPI_LEN);
  106. spin_unlock(&rtas_data_buf_lock);
  107. if (value[0] != 0 || value[1] != 2 || value[3] != 0 || result < 0) {
  108. pr_warn("could not get sp_spi watchdog timeout (%li). Continuing\n",
  109. result);
  110. return fallback_value;
  111. }
  112. /* rtas uses minutes */
  113. return ((int)value[2]) * 60;
  114. }
  115. /**
  116. * wdrtas_timer_start - starts watchdog
  117. *
  118. * wdrtas_timer_start starts the watchdog by calling the RTAS function
  119. * set-interval (surveillance)
  120. */
  121. static void wdrtas_timer_start(void)
  122. {
  123. wdrtas_set_interval(wdrtas_interval);
  124. }
  125. /**
  126. * wdrtas_timer_stop - stops watchdog
  127. *
  128. * wdrtas_timer_stop stops the watchdog timer by calling the RTAS function
  129. * set-interval (surveillance)
  130. */
  131. static void wdrtas_timer_stop(void)
  132. {
  133. wdrtas_set_interval(0);
  134. }
  135. /**
  136. * wdrtas_timer_keepalive - resets watchdog timer to keep system alive
  137. *
  138. * wdrtas_timer_keepalive restarts the watchdog timer by calling the
  139. * RTAS function event-scan and repeats these calls as long as there are
  140. * events available. All events will be dumped.
  141. */
  142. static void wdrtas_timer_keepalive(void)
  143. {
  144. long result;
  145. do {
  146. result = rtas_call(wdrtas_token_event_scan, 4, 1, NULL,
  147. RTAS_EVENT_SCAN_ALL_EVENTS, 0,
  148. (void *)__pa(wdrtas_logbuffer),
  149. WDRTAS_LOGBUFFER_LEN);
  150. if (result < 0)
  151. pr_err("event-scan failed: %li\n", result);
  152. if (result == 0)
  153. print_hex_dump(KERN_INFO, "dumping event, data: ",
  154. DUMP_PREFIX_OFFSET, 16, 1,
  155. wdrtas_logbuffer, WDRTAS_LOGBUFFER_LEN, false);
  156. } while (result == 0);
  157. }
  158. /**
  159. * wdrtas_get_temperature - returns current temperature
  160. *
  161. * returns temperature or <0 on failures
  162. *
  163. * wdrtas_get_temperature returns the current temperature in Fahrenheit. It
  164. * uses the RTAS call get-sensor-state, token 3 to do so
  165. */
  166. static int wdrtas_get_temperature(void)
  167. {
  168. int result;
  169. int temperature = 0;
  170. result = rtas_get_sensor(WDRTAS_THERMAL_SENSOR, 0, &temperature);
  171. if (result < 0)
  172. pr_warn("reading the thermal sensor failed: %i\n", result);
  173. else
  174. temperature = ((temperature * 9) / 5) + 32; /* fahrenheit */
  175. return temperature;
  176. }
  177. /**
  178. * wdrtas_get_status - returns the status of the watchdog
  179. *
  180. * returns a bitmask of defines WDIOF_... as defined in
  181. * include/linux/watchdog.h
  182. */
  183. static int wdrtas_get_status(void)
  184. {
  185. return 0; /* TODO */
  186. }
  187. /**
  188. * wdrtas_get_boot_status - returns the reason for the last boot
  189. *
  190. * returns a bitmask of defines WDIOF_... as defined in
  191. * include/linux/watchdog.h, indicating why the watchdog rebooted the system
  192. */
  193. static int wdrtas_get_boot_status(void)
  194. {
  195. return 0; /* TODO */
  196. }
  197. /*** watchdog API and operations stuff */
  198. /* wdrtas_write - called when watchdog device is written to
  199. * @file: file structure
  200. * @buf: user buffer with data
  201. * @len: amount to data written
  202. * @ppos: position in file
  203. *
  204. * returns the number of successfully processed characters, which is always
  205. * the number of bytes passed to this function
  206. *
  207. * wdrtas_write processes all the data given to it and looks for the magic
  208. * character 'V'. This character allows the watchdog device to be closed
  209. * properly.
  210. */
  211. static ssize_t wdrtas_write(struct file *file, const char __user *buf,
  212. size_t len, loff_t *ppos)
  213. {
  214. int i;
  215. char c;
  216. if (!len)
  217. goto out;
  218. if (!wdrtas_nowayout) {
  219. wdrtas_expect_close = 0;
  220. /* look for 'V' */
  221. for (i = 0; i < len; i++) {
  222. if (get_user(c, buf + i))
  223. return -EFAULT;
  224. /* allow to close device */
  225. if (c == 'V')
  226. wdrtas_expect_close = WDRTAS_MAGIC_CHAR;
  227. }
  228. }
  229. wdrtas_timer_keepalive();
  230. out:
  231. return len;
  232. }
  233. /**
  234. * wdrtas_ioctl - ioctl function for the watchdog device
  235. * @file: file structure
  236. * @cmd: command for ioctl
  237. * @arg: argument pointer
  238. *
  239. * returns 0 on success, <0 on failure
  240. *
  241. * wdrtas_ioctl implements the watchdog API ioctls
  242. */
  243. static long wdrtas_ioctl(struct file *file, unsigned int cmd,
  244. unsigned long arg)
  245. {
  246. int __user *argp = (void __user *)arg;
  247. int i;
  248. static const struct watchdog_info wdinfo = {
  249. .options = WDRTAS_SUPPORTED_MASK,
  250. .firmware_version = 0,
  251. .identity = "wdrtas",
  252. };
  253. switch (cmd) {
  254. case WDIOC_GETSUPPORT:
  255. if (copy_to_user(argp, &wdinfo, sizeof(wdinfo)))
  256. return -EFAULT;
  257. return 0;
  258. case WDIOC_GETSTATUS:
  259. i = wdrtas_get_status();
  260. return put_user(i, argp);
  261. case WDIOC_GETBOOTSTATUS:
  262. i = wdrtas_get_boot_status();
  263. return put_user(i, argp);
  264. case WDIOC_GETTEMP:
  265. if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE)
  266. return -EOPNOTSUPP;
  267. i = wdrtas_get_temperature();
  268. return put_user(i, argp);
  269. case WDIOC_SETOPTIONS:
  270. if (get_user(i, argp))
  271. return -EFAULT;
  272. if (i & WDIOS_DISABLECARD)
  273. wdrtas_timer_stop();
  274. if (i & WDIOS_ENABLECARD) {
  275. wdrtas_timer_keepalive();
  276. wdrtas_timer_start();
  277. }
  278. /* not implemented. Done by H8
  279. if (i & WDIOS_TEMPPANIC) {
  280. } */
  281. return 0;
  282. case WDIOC_KEEPALIVE:
  283. wdrtas_timer_keepalive();
  284. return 0;
  285. case WDIOC_SETTIMEOUT:
  286. if (get_user(i, argp))
  287. return -EFAULT;
  288. if (wdrtas_set_interval(i))
  289. return -EINVAL;
  290. wdrtas_timer_keepalive();
  291. if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
  292. wdrtas_interval = i;
  293. else
  294. wdrtas_interval = wdrtas_get_interval(i);
  295. /* fallthrough */
  296. case WDIOC_GETTIMEOUT:
  297. return put_user(wdrtas_interval, argp);
  298. default:
  299. return -ENOTTY;
  300. }
  301. }
  302. /**
  303. * wdrtas_open - open function of watchdog device
  304. * @inode: inode structure
  305. * @file: file structure
  306. *
  307. * returns 0 on success, -EBUSY if the file has been opened already, <0 on
  308. * other failures
  309. *
  310. * function called when watchdog device is opened
  311. */
  312. static int wdrtas_open(struct inode *inode, struct file *file)
  313. {
  314. /* only open once */
  315. if (atomic_inc_return(&wdrtas_miscdev_open) > 1) {
  316. atomic_dec(&wdrtas_miscdev_open);
  317. return -EBUSY;
  318. }
  319. wdrtas_timer_start();
  320. wdrtas_timer_keepalive();
  321. return nonseekable_open(inode, file);
  322. }
  323. /**
  324. * wdrtas_close - close function of watchdog device
  325. * @inode: inode structure
  326. * @file: file structure
  327. *
  328. * returns 0 on success
  329. *
  330. * close function. Always succeeds
  331. */
  332. static int wdrtas_close(struct inode *inode, struct file *file)
  333. {
  334. /* only stop watchdog, if this was announced using 'V' before */
  335. if (wdrtas_expect_close == WDRTAS_MAGIC_CHAR)
  336. wdrtas_timer_stop();
  337. else {
  338. pr_warn("got unexpected close. Watchdog not stopped.\n");
  339. wdrtas_timer_keepalive();
  340. }
  341. wdrtas_expect_close = 0;
  342. atomic_dec(&wdrtas_miscdev_open);
  343. return 0;
  344. }
  345. /**
  346. * wdrtas_temp_read - gives back the temperature in fahrenheit
  347. * @file: file structure
  348. * @buf: user buffer
  349. * @count: number of bytes to be read
  350. * @ppos: position in file
  351. *
  352. * returns always 1 or -EFAULT in case of user space copy failures, <0 on
  353. * other failures
  354. *
  355. * wdrtas_temp_read gives the temperature to the users by copying this
  356. * value as one byte into the user space buffer. The unit is Fahrenheit...
  357. */
  358. static ssize_t wdrtas_temp_read(struct file *file, char __user *buf,
  359. size_t count, loff_t *ppos)
  360. {
  361. int temperature = 0;
  362. temperature = wdrtas_get_temperature();
  363. if (temperature < 0)
  364. return temperature;
  365. if (copy_to_user(buf, &temperature, 1))
  366. return -EFAULT;
  367. return 1;
  368. }
  369. /**
  370. * wdrtas_temp_open - open function of temperature device
  371. * @inode: inode structure
  372. * @file: file structure
  373. *
  374. * returns 0 on success, <0 on failure
  375. *
  376. * function called when temperature device is opened
  377. */
  378. static int wdrtas_temp_open(struct inode *inode, struct file *file)
  379. {
  380. return nonseekable_open(inode, file);
  381. }
  382. /**
  383. * wdrtas_temp_close - close function of temperature device
  384. * @inode: inode structure
  385. * @file: file structure
  386. *
  387. * returns 0 on success
  388. *
  389. * close function. Always succeeds
  390. */
  391. static int wdrtas_temp_close(struct inode *inode, struct file *file)
  392. {
  393. return 0;
  394. }
  395. /**
  396. * wdrtas_reboot - reboot notifier function
  397. * @nb: notifier block structure
  398. * @code: reboot code
  399. * @ptr: unused
  400. *
  401. * returns NOTIFY_DONE
  402. *
  403. * wdrtas_reboot stops the watchdog in case of a reboot
  404. */
  405. static int wdrtas_reboot(struct notifier_block *this,
  406. unsigned long code, void *ptr)
  407. {
  408. if (code == SYS_DOWN || code == SYS_HALT)
  409. wdrtas_timer_stop();
  410. return NOTIFY_DONE;
  411. }
  412. /*** initialization stuff */
  413. static const struct file_operations wdrtas_fops = {
  414. .owner = THIS_MODULE,
  415. .llseek = no_llseek,
  416. .write = wdrtas_write,
  417. .unlocked_ioctl = wdrtas_ioctl,
  418. .open = wdrtas_open,
  419. .release = wdrtas_close,
  420. };
  421. static struct miscdevice wdrtas_miscdev = {
  422. .minor = WATCHDOG_MINOR,
  423. .name = "watchdog",
  424. .fops = &wdrtas_fops,
  425. };
  426. static const struct file_operations wdrtas_temp_fops = {
  427. .owner = THIS_MODULE,
  428. .llseek = no_llseek,
  429. .read = wdrtas_temp_read,
  430. .open = wdrtas_temp_open,
  431. .release = wdrtas_temp_close,
  432. };
  433. static struct miscdevice wdrtas_tempdev = {
  434. .minor = TEMP_MINOR,
  435. .name = "temperature",
  436. .fops = &wdrtas_temp_fops,
  437. };
  438. static struct notifier_block wdrtas_notifier = {
  439. .notifier_call = wdrtas_reboot,
  440. };
  441. /**
  442. * wdrtas_get_tokens - reads in RTAS tokens
  443. *
  444. * returns 0 on success, <0 on failure
  445. *
  446. * wdrtas_get_tokens reads in the tokens for the RTAS calls used in
  447. * this watchdog driver. It tolerates, if "get-sensor-state" and
  448. * "ibm,get-system-parameter" are not available.
  449. */
  450. static int wdrtas_get_tokens(void)
  451. {
  452. wdrtas_token_get_sensor_state = rtas_token("get-sensor-state");
  453. if (wdrtas_token_get_sensor_state == RTAS_UNKNOWN_SERVICE) {
  454. pr_warn("couldn't get token for get-sensor-state. Trying to continue without temperature support.\n");
  455. }
  456. wdrtas_token_get_sp = rtas_token("ibm,get-system-parameter");
  457. if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE) {
  458. pr_warn("couldn't get token for ibm,get-system-parameter. Trying to continue with a default timeout value of %i seconds.\n",
  459. WDRTAS_DEFAULT_INTERVAL);
  460. }
  461. wdrtas_token_set_indicator = rtas_token("set-indicator");
  462. if (wdrtas_token_set_indicator == RTAS_UNKNOWN_SERVICE) {
  463. pr_err("couldn't get token for set-indicator. Terminating watchdog code.\n");
  464. return -EIO;
  465. }
  466. wdrtas_token_event_scan = rtas_token("event-scan");
  467. if (wdrtas_token_event_scan == RTAS_UNKNOWN_SERVICE) {
  468. pr_err("couldn't get token for event-scan. Terminating watchdog code.\n");
  469. return -EIO;
  470. }
  471. return 0;
  472. }
  473. /**
  474. * wdrtas_unregister_devs - unregisters the misc dev handlers
  475. *
  476. * wdrtas_register_devs unregisters the watchdog and temperature watchdog
  477. * misc devs
  478. */
  479. static void wdrtas_unregister_devs(void)
  480. {
  481. misc_deregister(&wdrtas_miscdev);
  482. if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE)
  483. misc_deregister(&wdrtas_tempdev);
  484. }
  485. /**
  486. * wdrtas_register_devs - registers the misc dev handlers
  487. *
  488. * returns 0 on success, <0 on failure
  489. *
  490. * wdrtas_register_devs registers the watchdog and temperature watchdog
  491. * misc devs
  492. */
  493. static int wdrtas_register_devs(void)
  494. {
  495. int result;
  496. result = misc_register(&wdrtas_miscdev);
  497. if (result) {
  498. pr_err("couldn't register watchdog misc device. Terminating watchdog code.\n");
  499. return result;
  500. }
  501. if (wdrtas_token_get_sensor_state != RTAS_UNKNOWN_SERVICE) {
  502. result = misc_register(&wdrtas_tempdev);
  503. if (result) {
  504. pr_warn("couldn't register watchdog temperature misc device. Continuing without temperature support.\n");
  505. wdrtas_token_get_sensor_state = RTAS_UNKNOWN_SERVICE;
  506. }
  507. }
  508. return 0;
  509. }
  510. /**
  511. * wdrtas_init - init function of the watchdog driver
  512. *
  513. * returns 0 on success, <0 on failure
  514. *
  515. * registers the file handlers and the reboot notifier
  516. */
  517. static int __init wdrtas_init(void)
  518. {
  519. if (wdrtas_get_tokens())
  520. return -ENODEV;
  521. if (wdrtas_register_devs())
  522. return -ENODEV;
  523. if (register_reboot_notifier(&wdrtas_notifier)) {
  524. pr_err("could not register reboot notifier. Terminating watchdog code.\n");
  525. wdrtas_unregister_devs();
  526. return -ENODEV;
  527. }
  528. if (wdrtas_token_get_sp == RTAS_UNKNOWN_SERVICE)
  529. wdrtas_interval = WDRTAS_DEFAULT_INTERVAL;
  530. else
  531. wdrtas_interval = wdrtas_get_interval(WDRTAS_DEFAULT_INTERVAL);
  532. return 0;
  533. }
  534. /**
  535. * wdrtas_exit - exit function of the watchdog driver
  536. *
  537. * unregisters the file handlers and the reboot notifier
  538. */
  539. static void __exit wdrtas_exit(void)
  540. {
  541. if (!wdrtas_nowayout)
  542. wdrtas_timer_stop();
  543. wdrtas_unregister_devs();
  544. unregister_reboot_notifier(&wdrtas_notifier);
  545. }
  546. module_init(wdrtas_init);
  547. module_exit(wdrtas_exit);