mei_wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2015, Intel Corporation.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/debugfs.h>
  10. #include <linux/completion.h>
  11. #include <linux/watchdog.h>
  12. #include <linux/uuid.h>
  13. #include <linux/mei_cl_bus.h>
  14. /*
  15. * iAMT Watchdog Device
  16. */
  17. #define INTEL_AMT_WATCHDOG_ID "iamt_wdt"
  18. #define MEI_WDT_DEFAULT_TIMEOUT 120 /* seconds */
  19. #define MEI_WDT_MIN_TIMEOUT 120 /* seconds */
  20. #define MEI_WDT_MAX_TIMEOUT 65535 /* seconds */
  21. /* Commands */
  22. #define MEI_MANAGEMENT_CONTROL 0x02
  23. /* MEI Management Control version number */
  24. #define MEI_MC_VERSION_NUMBER 0x10
  25. /* Sub Commands */
  26. #define MEI_MC_START_WD_TIMER_REQ 0x13
  27. #define MEI_MC_START_WD_TIMER_RES 0x83
  28. #define MEI_WDT_STATUS_SUCCESS 0
  29. #define MEI_WDT_WDSTATE_NOT_REQUIRED 0x1
  30. #define MEI_MC_STOP_WD_TIMER_REQ 0x14
  31. /**
  32. * enum mei_wdt_state - internal watchdog state
  33. *
  34. * @MEI_WDT_PROBE: wd in probing stage
  35. * @MEI_WDT_IDLE: wd is idle and not opened
  36. * @MEI_WDT_START: wd was opened, start was called
  37. * @MEI_WDT_RUNNING: wd is expecting keep alive pings
  38. * @MEI_WDT_STOPPING: wd is stopping and will move to IDLE
  39. * @MEI_WDT_NOT_REQUIRED: wd device is not required
  40. */
  41. enum mei_wdt_state {
  42. MEI_WDT_PROBE,
  43. MEI_WDT_IDLE,
  44. MEI_WDT_START,
  45. MEI_WDT_RUNNING,
  46. MEI_WDT_STOPPING,
  47. MEI_WDT_NOT_REQUIRED,
  48. };
  49. static const char *mei_wdt_state_str(enum mei_wdt_state state)
  50. {
  51. switch (state) {
  52. case MEI_WDT_PROBE:
  53. return "PROBE";
  54. case MEI_WDT_IDLE:
  55. return "IDLE";
  56. case MEI_WDT_START:
  57. return "START";
  58. case MEI_WDT_RUNNING:
  59. return "RUNNING";
  60. case MEI_WDT_STOPPING:
  61. return "STOPPING";
  62. case MEI_WDT_NOT_REQUIRED:
  63. return "NOT_REQUIRED";
  64. default:
  65. return "unknown";
  66. }
  67. }
  68. /**
  69. * struct mei_wdt - mei watchdog driver
  70. * @wdd: watchdog device
  71. *
  72. * @cldev: mei watchdog client device
  73. * @state: watchdog internal state
  74. * @resp_required: ping required response
  75. * @response: ping response completion
  76. * @unregister: unregister worker
  77. * @reg_lock: watchdog device registration lock
  78. * @timeout: watchdog current timeout
  79. *
  80. * @dbgfs_dir: debugfs dir entry
  81. */
  82. struct mei_wdt {
  83. struct watchdog_device wdd;
  84. struct mei_cl_device *cldev;
  85. enum mei_wdt_state state;
  86. bool resp_required;
  87. struct completion response;
  88. struct work_struct unregister;
  89. struct mutex reg_lock;
  90. u16 timeout;
  91. #if IS_ENABLED(CONFIG_DEBUG_FS)
  92. struct dentry *dbgfs_dir;
  93. #endif /* CONFIG_DEBUG_FS */
  94. };
  95. /*
  96. * struct mei_mc_hdr - Management Control Command Header
  97. *
  98. * @command: Management Control (0x2)
  99. * @bytecount: Number of bytes in the message beyond this byte
  100. * @subcommand: Management Control Subcommand
  101. * @versionnumber: Management Control Version (0x10)
  102. */
  103. struct mei_mc_hdr {
  104. u8 command;
  105. u8 bytecount;
  106. u8 subcommand;
  107. u8 versionnumber;
  108. };
  109. /**
  110. * struct mei_wdt_start_request watchdog start/ping
  111. *
  112. * @hdr: Management Control Command Header
  113. * @timeout: timeout value
  114. * @reserved: reserved (legacy)
  115. */
  116. struct mei_wdt_start_request {
  117. struct mei_mc_hdr hdr;
  118. u16 timeout;
  119. u8 reserved[17];
  120. } __packed;
  121. /**
  122. * struct mei_wdt_start_response watchdog start/ping response
  123. *
  124. * @hdr: Management Control Command Header
  125. * @status: operation status
  126. * @wdstate: watchdog status bit mask
  127. */
  128. struct mei_wdt_start_response {
  129. struct mei_mc_hdr hdr;
  130. u8 status;
  131. u8 wdstate;
  132. } __packed;
  133. /**
  134. * struct mei_wdt_stop_request - watchdog stop
  135. *
  136. * @hdr: Management Control Command Header
  137. */
  138. struct mei_wdt_stop_request {
  139. struct mei_mc_hdr hdr;
  140. } __packed;
  141. /**
  142. * mei_wdt_ping - send wd start/ping command
  143. *
  144. * @wdt: mei watchdog device
  145. *
  146. * Return: 0 on success,
  147. * negative errno code on failure
  148. */
  149. static int mei_wdt_ping(struct mei_wdt *wdt)
  150. {
  151. struct mei_wdt_start_request req;
  152. const size_t req_len = sizeof(req);
  153. int ret;
  154. memset(&req, 0, req_len);
  155. req.hdr.command = MEI_MANAGEMENT_CONTROL;
  156. req.hdr.bytecount = req_len - offsetof(struct mei_mc_hdr, subcommand);
  157. req.hdr.subcommand = MEI_MC_START_WD_TIMER_REQ;
  158. req.hdr.versionnumber = MEI_MC_VERSION_NUMBER;
  159. req.timeout = wdt->timeout;
  160. ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
  161. if (ret < 0)
  162. return ret;
  163. return 0;
  164. }
  165. /**
  166. * mei_wdt_stop - send wd stop command
  167. *
  168. * @wdt: mei watchdog device
  169. *
  170. * Return: 0 on success,
  171. * negative errno code on failure
  172. */
  173. static int mei_wdt_stop(struct mei_wdt *wdt)
  174. {
  175. struct mei_wdt_stop_request req;
  176. const size_t req_len = sizeof(req);
  177. int ret;
  178. memset(&req, 0, req_len);
  179. req.hdr.command = MEI_MANAGEMENT_CONTROL;
  180. req.hdr.bytecount = req_len - offsetof(struct mei_mc_hdr, subcommand);
  181. req.hdr.subcommand = MEI_MC_STOP_WD_TIMER_REQ;
  182. req.hdr.versionnumber = MEI_MC_VERSION_NUMBER;
  183. ret = mei_cldev_send(wdt->cldev, (u8 *)&req, req_len);
  184. if (ret < 0)
  185. return ret;
  186. return 0;
  187. }
  188. /**
  189. * mei_wdt_ops_start - wd start command from the watchdog core.
  190. *
  191. * @wdd: watchdog device
  192. *
  193. * Return: 0 on success or -ENODEV;
  194. */
  195. static int mei_wdt_ops_start(struct watchdog_device *wdd)
  196. {
  197. struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
  198. wdt->state = MEI_WDT_START;
  199. wdd->timeout = wdt->timeout;
  200. return 0;
  201. }
  202. /**
  203. * mei_wdt_ops_stop - wd stop command from the watchdog core.
  204. *
  205. * @wdd: watchdog device
  206. *
  207. * Return: 0 if success, negative errno code for failure
  208. */
  209. static int mei_wdt_ops_stop(struct watchdog_device *wdd)
  210. {
  211. struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
  212. int ret;
  213. if (wdt->state != MEI_WDT_RUNNING)
  214. return 0;
  215. wdt->state = MEI_WDT_STOPPING;
  216. ret = mei_wdt_stop(wdt);
  217. if (ret)
  218. return ret;
  219. wdt->state = MEI_WDT_IDLE;
  220. return 0;
  221. }
  222. /**
  223. * mei_wdt_ops_ping - wd ping command from the watchdog core.
  224. *
  225. * @wdd: watchdog device
  226. *
  227. * Return: 0 if success, negative errno code on failure
  228. */
  229. static int mei_wdt_ops_ping(struct watchdog_device *wdd)
  230. {
  231. struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
  232. int ret;
  233. if (wdt->state != MEI_WDT_START && wdt->state != MEI_WDT_RUNNING)
  234. return 0;
  235. if (wdt->resp_required)
  236. init_completion(&wdt->response);
  237. wdt->state = MEI_WDT_RUNNING;
  238. ret = mei_wdt_ping(wdt);
  239. if (ret)
  240. return ret;
  241. if (wdt->resp_required)
  242. ret = wait_for_completion_killable(&wdt->response);
  243. return ret;
  244. }
  245. /**
  246. * mei_wdt_ops_set_timeout - wd set timeout command from the watchdog core.
  247. *
  248. * @wdd: watchdog device
  249. * @timeout: timeout value to set
  250. *
  251. * Return: 0 if success, negative errno code for failure
  252. */
  253. static int mei_wdt_ops_set_timeout(struct watchdog_device *wdd,
  254. unsigned int timeout)
  255. {
  256. struct mei_wdt *wdt = watchdog_get_drvdata(wdd);
  257. /* valid value is already checked by the caller */
  258. wdt->timeout = timeout;
  259. wdd->timeout = timeout;
  260. return 0;
  261. }
  262. static const struct watchdog_ops wd_ops = {
  263. .owner = THIS_MODULE,
  264. .start = mei_wdt_ops_start,
  265. .stop = mei_wdt_ops_stop,
  266. .ping = mei_wdt_ops_ping,
  267. .set_timeout = mei_wdt_ops_set_timeout,
  268. };
  269. /* not const as the firmware_version field need to be retrieved */
  270. static struct watchdog_info wd_info = {
  271. .identity = INTEL_AMT_WATCHDOG_ID,
  272. .options = WDIOF_KEEPALIVEPING |
  273. WDIOF_SETTIMEOUT |
  274. WDIOF_ALARMONLY,
  275. };
  276. /**
  277. * __mei_wdt_is_registered - check if wdt is registered
  278. *
  279. * @wdt: mei watchdog device
  280. *
  281. * Return: true if the wdt is registered with the watchdog subsystem
  282. * Locking: should be called under wdt->reg_lock
  283. */
  284. static inline bool __mei_wdt_is_registered(struct mei_wdt *wdt)
  285. {
  286. return !!watchdog_get_drvdata(&wdt->wdd);
  287. }
  288. /**
  289. * mei_wdt_unregister - unregister from the watchdog subsystem
  290. *
  291. * @wdt: mei watchdog device
  292. */
  293. static void mei_wdt_unregister(struct mei_wdt *wdt)
  294. {
  295. mutex_lock(&wdt->reg_lock);
  296. if (__mei_wdt_is_registered(wdt)) {
  297. watchdog_unregister_device(&wdt->wdd);
  298. watchdog_set_drvdata(&wdt->wdd, NULL);
  299. memset(&wdt->wdd, 0, sizeof(wdt->wdd));
  300. }
  301. mutex_unlock(&wdt->reg_lock);
  302. }
  303. /**
  304. * mei_wdt_register - register with the watchdog subsystem
  305. *
  306. * @wdt: mei watchdog device
  307. *
  308. * Return: 0 if success, negative errno code for failure
  309. */
  310. static int mei_wdt_register(struct mei_wdt *wdt)
  311. {
  312. struct device *dev;
  313. int ret;
  314. if (!wdt || !wdt->cldev)
  315. return -EINVAL;
  316. dev = &wdt->cldev->dev;
  317. mutex_lock(&wdt->reg_lock);
  318. if (__mei_wdt_is_registered(wdt)) {
  319. ret = 0;
  320. goto out;
  321. }
  322. wdt->wdd.info = &wd_info;
  323. wdt->wdd.ops = &wd_ops;
  324. wdt->wdd.parent = dev;
  325. wdt->wdd.timeout = MEI_WDT_DEFAULT_TIMEOUT;
  326. wdt->wdd.min_timeout = MEI_WDT_MIN_TIMEOUT;
  327. wdt->wdd.max_timeout = MEI_WDT_MAX_TIMEOUT;
  328. watchdog_set_drvdata(&wdt->wdd, wdt);
  329. watchdog_stop_on_reboot(&wdt->wdd);
  330. watchdog_stop_on_unregister(&wdt->wdd);
  331. ret = watchdog_register_device(&wdt->wdd);
  332. if (ret)
  333. watchdog_set_drvdata(&wdt->wdd, NULL);
  334. wdt->state = MEI_WDT_IDLE;
  335. out:
  336. mutex_unlock(&wdt->reg_lock);
  337. return ret;
  338. }
  339. static void mei_wdt_unregister_work(struct work_struct *work)
  340. {
  341. struct mei_wdt *wdt = container_of(work, struct mei_wdt, unregister);
  342. mei_wdt_unregister(wdt);
  343. }
  344. /**
  345. * mei_wdt_rx - callback for data receive
  346. *
  347. * @cldev: bus device
  348. */
  349. static void mei_wdt_rx(struct mei_cl_device *cldev)
  350. {
  351. struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
  352. struct mei_wdt_start_response res;
  353. const size_t res_len = sizeof(res);
  354. int ret;
  355. ret = mei_cldev_recv(wdt->cldev, (u8 *)&res, res_len);
  356. if (ret < 0) {
  357. dev_err(&cldev->dev, "failure in recv %d\n", ret);
  358. return;
  359. }
  360. /* Empty response can be sent on stop */
  361. if (ret == 0)
  362. return;
  363. if (ret < sizeof(struct mei_mc_hdr)) {
  364. dev_err(&cldev->dev, "recv small data %d\n", ret);
  365. return;
  366. }
  367. if (res.hdr.command != MEI_MANAGEMENT_CONTROL ||
  368. res.hdr.versionnumber != MEI_MC_VERSION_NUMBER) {
  369. dev_err(&cldev->dev, "wrong command received\n");
  370. return;
  371. }
  372. if (res.hdr.subcommand != MEI_MC_START_WD_TIMER_RES) {
  373. dev_warn(&cldev->dev, "unsupported command %d :%s[%d]\n",
  374. res.hdr.subcommand,
  375. mei_wdt_state_str(wdt->state),
  376. wdt->state);
  377. return;
  378. }
  379. /* Run the unregistration in a worker as this can be
  380. * run only after ping completion, otherwise the flow will
  381. * deadlock on watchdog core mutex.
  382. */
  383. if (wdt->state == MEI_WDT_RUNNING) {
  384. if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
  385. wdt->state = MEI_WDT_NOT_REQUIRED;
  386. schedule_work(&wdt->unregister);
  387. }
  388. goto out;
  389. }
  390. if (wdt->state == MEI_WDT_PROBE) {
  391. if (res.wdstate & MEI_WDT_WDSTATE_NOT_REQUIRED) {
  392. wdt->state = MEI_WDT_NOT_REQUIRED;
  393. } else {
  394. /* stop the watchdog and register watchdog device */
  395. mei_wdt_stop(wdt);
  396. mei_wdt_register(wdt);
  397. }
  398. return;
  399. }
  400. dev_warn(&cldev->dev, "not in correct state %s[%d]\n",
  401. mei_wdt_state_str(wdt->state), wdt->state);
  402. out:
  403. if (!completion_done(&wdt->response))
  404. complete(&wdt->response);
  405. }
  406. /*
  407. * mei_wdt_notif - callback for event notification
  408. *
  409. * @cldev: bus device
  410. */
  411. static void mei_wdt_notif(struct mei_cl_device *cldev)
  412. {
  413. struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
  414. if (wdt->state != MEI_WDT_NOT_REQUIRED)
  415. return;
  416. mei_wdt_register(wdt);
  417. }
  418. #if IS_ENABLED(CONFIG_DEBUG_FS)
  419. static ssize_t mei_dbgfs_read_activation(struct file *file, char __user *ubuf,
  420. size_t cnt, loff_t *ppos)
  421. {
  422. struct mei_wdt *wdt = file->private_data;
  423. const size_t bufsz = 32;
  424. char buf[32];
  425. ssize_t pos;
  426. mutex_lock(&wdt->reg_lock);
  427. pos = scnprintf(buf, bufsz, "%s\n",
  428. __mei_wdt_is_registered(wdt) ? "activated" : "deactivated");
  429. mutex_unlock(&wdt->reg_lock);
  430. return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  431. }
  432. static const struct file_operations dbgfs_fops_activation = {
  433. .open = simple_open,
  434. .read = mei_dbgfs_read_activation,
  435. .llseek = generic_file_llseek,
  436. };
  437. static ssize_t mei_dbgfs_read_state(struct file *file, char __user *ubuf,
  438. size_t cnt, loff_t *ppos)
  439. {
  440. struct mei_wdt *wdt = file->private_data;
  441. char buf[32];
  442. ssize_t pos;
  443. pos = scnprintf(buf, sizeof(buf), "state: %s\n",
  444. mei_wdt_state_str(wdt->state));
  445. return simple_read_from_buffer(ubuf, cnt, ppos, buf, pos);
  446. }
  447. static const struct file_operations dbgfs_fops_state = {
  448. .open = simple_open,
  449. .read = mei_dbgfs_read_state,
  450. .llseek = generic_file_llseek,
  451. };
  452. static void dbgfs_unregister(struct mei_wdt *wdt)
  453. {
  454. debugfs_remove_recursive(wdt->dbgfs_dir);
  455. wdt->dbgfs_dir = NULL;
  456. }
  457. static void dbgfs_register(struct mei_wdt *wdt)
  458. {
  459. struct dentry *dir;
  460. dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
  461. wdt->dbgfs_dir = dir;
  462. debugfs_create_file("state", S_IRUSR, dir, wdt, &dbgfs_fops_state);
  463. debugfs_create_file("activation", S_IRUSR, dir, wdt,
  464. &dbgfs_fops_activation);
  465. }
  466. #else
  467. static inline void dbgfs_unregister(struct mei_wdt *wdt) {}
  468. static inline void dbgfs_register(struct mei_wdt *wdt) {}
  469. #endif /* CONFIG_DEBUG_FS */
  470. static int mei_wdt_probe(struct mei_cl_device *cldev,
  471. const struct mei_cl_device_id *id)
  472. {
  473. struct mei_wdt *wdt;
  474. int ret;
  475. wdt = kzalloc(sizeof(struct mei_wdt), GFP_KERNEL);
  476. if (!wdt)
  477. return -ENOMEM;
  478. wdt->timeout = MEI_WDT_DEFAULT_TIMEOUT;
  479. wdt->state = MEI_WDT_PROBE;
  480. wdt->cldev = cldev;
  481. wdt->resp_required = mei_cldev_ver(cldev) > 0x1;
  482. mutex_init(&wdt->reg_lock);
  483. init_completion(&wdt->response);
  484. INIT_WORK(&wdt->unregister, mei_wdt_unregister_work);
  485. mei_cldev_set_drvdata(cldev, wdt);
  486. ret = mei_cldev_enable(cldev);
  487. if (ret < 0) {
  488. dev_err(&cldev->dev, "Could not enable cl device\n");
  489. goto err_out;
  490. }
  491. ret = mei_cldev_register_rx_cb(wdt->cldev, mei_wdt_rx);
  492. if (ret) {
  493. dev_err(&cldev->dev, "Could not reg rx event ret=%d\n", ret);
  494. goto err_disable;
  495. }
  496. ret = mei_cldev_register_notif_cb(wdt->cldev, mei_wdt_notif);
  497. /* on legacy devices notification is not supported
  498. */
  499. if (ret && ret != -EOPNOTSUPP) {
  500. dev_err(&cldev->dev, "Could not reg notif event ret=%d\n", ret);
  501. goto err_disable;
  502. }
  503. wd_info.firmware_version = mei_cldev_ver(cldev);
  504. if (wdt->resp_required)
  505. ret = mei_wdt_ping(wdt);
  506. else
  507. ret = mei_wdt_register(wdt);
  508. if (ret)
  509. goto err_disable;
  510. dbgfs_register(wdt);
  511. return 0;
  512. err_disable:
  513. mei_cldev_disable(cldev);
  514. err_out:
  515. kfree(wdt);
  516. return ret;
  517. }
  518. static int mei_wdt_remove(struct mei_cl_device *cldev)
  519. {
  520. struct mei_wdt *wdt = mei_cldev_get_drvdata(cldev);
  521. /* Free the caller in case of fw initiated or unexpected reset */
  522. if (!completion_done(&wdt->response))
  523. complete(&wdt->response);
  524. cancel_work_sync(&wdt->unregister);
  525. mei_wdt_unregister(wdt);
  526. mei_cldev_disable(cldev);
  527. dbgfs_unregister(wdt);
  528. kfree(wdt);
  529. return 0;
  530. }
  531. #define MEI_UUID_WD UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, \
  532. 0x89, 0x9D, 0xA9, 0x15, 0x14, 0xCB, 0x32, 0xAB)
  533. static const struct mei_cl_device_id mei_wdt_tbl[] = {
  534. { .uuid = MEI_UUID_WD, .version = MEI_CL_VERSION_ANY },
  535. /* required last entry */
  536. { }
  537. };
  538. MODULE_DEVICE_TABLE(mei, mei_wdt_tbl);
  539. static struct mei_cl_driver mei_wdt_driver = {
  540. .id_table = mei_wdt_tbl,
  541. .name = KBUILD_MODNAME,
  542. .probe = mei_wdt_probe,
  543. .remove = mei_wdt_remove,
  544. };
  545. module_mei_cl_driver(mei_wdt_driver);
  546. MODULE_AUTHOR("Intel Corporation");
  547. MODULE_LICENSE("GPL v2");
  548. MODULE_DESCRIPTION("Device driver for Intel MEI iAMT watchdog");