wdrtas.c 15 KB

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