wdrtas.c 16 KB

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