wd.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/device.h>
  20. #include <linux/pci.h>
  21. #include <linux/sched.h>
  22. #include <linux/watchdog.h>
  23. #include <linux/mei.h>
  24. #include "mei_dev.h"
  25. #include "hbm.h"
  26. #include "client.h"
  27. static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
  28. static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
  29. /*
  30. * AMT Watchdog Device
  31. */
  32. #define INTEL_AMT_WATCHDOG_ID "INTCAMT"
  33. /* UUIDs for AMT F/W clients */
  34. const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
  35. 0x9D, 0xA9, 0x15, 0x14, 0xCB,
  36. 0x32, 0xAB);
  37. static void mei_wd_set_start_timeout(struct mei_device *dev, u16 timeout)
  38. {
  39. dev_dbg(&dev->pdev->dev, "wd: set timeout=%d.\n", timeout);
  40. memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE);
  41. memcpy(dev->wd_data + MEI_WD_HDR_SIZE, &timeout, sizeof(u16));
  42. }
  43. /**
  44. * mei_wd_host_init - connect to the watchdog client
  45. *
  46. * @dev: the device structure
  47. *
  48. * returns -ENOTTY if wd client cannot be found
  49. * -EIO if write has failed
  50. * 0 on success
  51. */
  52. int mei_wd_host_init(struct mei_device *dev)
  53. {
  54. struct mei_cl *cl = &dev->wd_cl;
  55. int id;
  56. int ret;
  57. mei_cl_init(cl, dev);
  58. dev->wd_timeout = MEI_WD_DEFAULT_TIMEOUT;
  59. dev->wd_state = MEI_WD_IDLE;
  60. /* check for valid client id */
  61. id = mei_me_cl_by_uuid(dev, &mei_wd_guid);
  62. if (id < 0) {
  63. dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
  64. return -ENOTTY;
  65. }
  66. cl->me_client_id = dev->me_clients[id].client_id;
  67. ret = mei_cl_link(cl, MEI_WD_HOST_CLIENT_ID);
  68. if (ret < 0) {
  69. dev_info(&dev->pdev->dev, "wd: failed link client\n");
  70. return ret;
  71. }
  72. ret = mei_cl_connect(cl, NULL);
  73. if (ret) {
  74. dev_err(&dev->pdev->dev, "wd: failed to connect = %d\n", ret);
  75. mei_cl_unlink(cl);
  76. return ret;
  77. }
  78. ret = mei_watchdog_register(dev);
  79. if (ret) {
  80. mei_cl_disconnect(cl);
  81. mei_cl_unlink(cl);
  82. }
  83. return ret;
  84. }
  85. /**
  86. * mei_wd_send - sends watch dog message to fw.
  87. *
  88. * @dev: the device structure
  89. *
  90. * returns 0 if success,
  91. * -EIO when message send fails
  92. * -EINVAL when invalid message is to be sent
  93. * -ENODEV on flow control failure
  94. */
  95. int mei_wd_send(struct mei_device *dev)
  96. {
  97. struct mei_cl *cl = &dev->wd_cl;
  98. struct mei_msg_hdr hdr;
  99. int ret;
  100. hdr.host_addr = cl->host_client_id;
  101. hdr.me_addr = cl->me_client_id;
  102. hdr.msg_complete = 1;
  103. hdr.reserved = 0;
  104. hdr.internal = 0;
  105. if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
  106. hdr.length = MEI_WD_START_MSG_SIZE;
  107. else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
  108. hdr.length = MEI_WD_STOP_MSG_SIZE;
  109. else {
  110. dev_err(&dev->pdev->dev, "wd: invalid message is to be sent, aborting\n");
  111. return -EINVAL;
  112. }
  113. ret = mei_write_message(dev, &hdr, dev->wd_data);
  114. if (ret) {
  115. dev_err(&dev->pdev->dev, "wd: write message failed\n");
  116. return ret;
  117. }
  118. ret = mei_cl_flow_ctrl_reduce(cl);
  119. if (ret) {
  120. dev_err(&dev->pdev->dev, "wd: flow_ctrl_reduce failed.\n");
  121. return ret;
  122. }
  123. return 0;
  124. }
  125. /**
  126. * mei_wd_stop - sends watchdog stop message to fw.
  127. *
  128. * @dev: the device structure
  129. * @preserve: indicate if to keep the timeout value
  130. *
  131. * returns 0 if success
  132. * on error:
  133. * -EIO when message send fails
  134. * -EINVAL when invalid message is to be sent
  135. * -ETIME on message timeout
  136. */
  137. int mei_wd_stop(struct mei_device *dev)
  138. {
  139. int ret;
  140. if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
  141. dev->wd_state != MEI_WD_RUNNING)
  142. return 0;
  143. memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
  144. dev->wd_state = MEI_WD_STOPPING;
  145. ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
  146. if (ret < 0)
  147. goto err;
  148. if (ret && mei_hbuf_acquire(dev)) {
  149. ret = mei_wd_send(dev);
  150. if (ret)
  151. goto err;
  152. dev->wd_pending = false;
  153. } else {
  154. dev->wd_pending = true;
  155. }
  156. mutex_unlock(&dev->device_lock);
  157. ret = wait_event_timeout(dev->wait_stop_wd,
  158. dev->wd_state == MEI_WD_IDLE,
  159. msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
  160. mutex_lock(&dev->device_lock);
  161. if (dev->wd_state != MEI_WD_IDLE) {
  162. /* timeout */
  163. ret = -ETIME;
  164. dev_warn(&dev->pdev->dev,
  165. "wd: stop failed to complete ret=%d.\n", ret);
  166. goto err;
  167. }
  168. dev_dbg(&dev->pdev->dev, "wd: stop completed after %u msec\n",
  169. MEI_WD_STOP_TIMEOUT - jiffies_to_msecs(ret));
  170. return 0;
  171. err:
  172. return ret;
  173. }
  174. /*
  175. * mei_wd_ops_start - wd start command from the watchdog core.
  176. *
  177. * @wd_dev - watchdog device struct
  178. *
  179. * returns 0 if success, negative errno code for failure
  180. */
  181. static int mei_wd_ops_start(struct watchdog_device *wd_dev)
  182. {
  183. int err = -ENODEV;
  184. struct mei_device *dev;
  185. dev = watchdog_get_drvdata(wd_dev);
  186. if (!dev)
  187. return -ENODEV;
  188. mutex_lock(&dev->device_lock);
  189. if (dev->dev_state != MEI_DEV_ENABLED) {
  190. dev_dbg(&dev->pdev->dev,
  191. "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
  192. mei_dev_state_str(dev->dev_state));
  193. goto end_unlock;
  194. }
  195. if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
  196. dev_dbg(&dev->pdev->dev,
  197. "MEI Driver is not connected to Watchdog Client\n");
  198. goto end_unlock;
  199. }
  200. mei_wd_set_start_timeout(dev, dev->wd_timeout);
  201. err = 0;
  202. end_unlock:
  203. mutex_unlock(&dev->device_lock);
  204. return err;
  205. }
  206. /*
  207. * mei_wd_ops_stop - wd stop command from the watchdog core.
  208. *
  209. * @wd_dev - watchdog device struct
  210. *
  211. * returns 0 if success, negative errno code for failure
  212. */
  213. static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
  214. {
  215. struct mei_device *dev;
  216. dev = watchdog_get_drvdata(wd_dev);
  217. if (!dev)
  218. return -ENODEV;
  219. mutex_lock(&dev->device_lock);
  220. mei_wd_stop(dev);
  221. mutex_unlock(&dev->device_lock);
  222. return 0;
  223. }
  224. /*
  225. * mei_wd_ops_ping - wd ping command from the watchdog core.
  226. *
  227. * @wd_dev - watchdog device struct
  228. *
  229. * returns 0 if success, negative errno code for failure
  230. */
  231. static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
  232. {
  233. struct mei_device *dev;
  234. int ret;
  235. dev = watchdog_get_drvdata(wd_dev);
  236. if (!dev)
  237. return -ENODEV;
  238. mutex_lock(&dev->device_lock);
  239. if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
  240. dev_err(&dev->pdev->dev, "wd: not connected.\n");
  241. ret = -ENODEV;
  242. goto end;
  243. }
  244. dev->wd_state = MEI_WD_RUNNING;
  245. ret = mei_cl_flow_ctrl_creds(&dev->wd_cl);
  246. if (ret < 0)
  247. goto end;
  248. /* Check if we can send the ping to HW*/
  249. if (ret && mei_hbuf_acquire(dev)) {
  250. dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
  251. ret = mei_wd_send(dev);
  252. if (ret)
  253. goto end;
  254. dev->wd_pending = false;
  255. } else {
  256. dev->wd_pending = true;
  257. }
  258. end:
  259. mutex_unlock(&dev->device_lock);
  260. return ret;
  261. }
  262. /*
  263. * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
  264. *
  265. * @wd_dev - watchdog device struct
  266. * @timeout - timeout value to set
  267. *
  268. * returns 0 if success, negative errno code for failure
  269. */
  270. static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev,
  271. unsigned int timeout)
  272. {
  273. struct mei_device *dev;
  274. dev = watchdog_get_drvdata(wd_dev);
  275. if (!dev)
  276. return -ENODEV;
  277. /* Check Timeout value */
  278. if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
  279. return -EINVAL;
  280. mutex_lock(&dev->device_lock);
  281. dev->wd_timeout = timeout;
  282. wd_dev->timeout = timeout;
  283. mei_wd_set_start_timeout(dev, dev->wd_timeout);
  284. mutex_unlock(&dev->device_lock);
  285. return 0;
  286. }
  287. /*
  288. * Watchdog Device structs
  289. */
  290. static const struct watchdog_ops wd_ops = {
  291. .owner = THIS_MODULE,
  292. .start = mei_wd_ops_start,
  293. .stop = mei_wd_ops_stop,
  294. .ping = mei_wd_ops_ping,
  295. .set_timeout = mei_wd_ops_set_timeout,
  296. };
  297. static const struct watchdog_info wd_info = {
  298. .identity = INTEL_AMT_WATCHDOG_ID,
  299. .options = WDIOF_KEEPALIVEPING |
  300. WDIOF_SETTIMEOUT |
  301. WDIOF_ALARMONLY,
  302. };
  303. static struct watchdog_device amt_wd_dev = {
  304. .info = &wd_info,
  305. .ops = &wd_ops,
  306. .timeout = MEI_WD_DEFAULT_TIMEOUT,
  307. .min_timeout = MEI_WD_MIN_TIMEOUT,
  308. .max_timeout = MEI_WD_MAX_TIMEOUT,
  309. };
  310. int mei_watchdog_register(struct mei_device *dev)
  311. {
  312. int ret;
  313. /* unlock to perserve correct locking order */
  314. mutex_unlock(&dev->device_lock);
  315. ret = watchdog_register_device(&amt_wd_dev);
  316. mutex_lock(&dev->device_lock);
  317. if (ret) {
  318. dev_err(&dev->pdev->dev, "wd: unable to register watchdog device = %d.\n",
  319. ret);
  320. return ret;
  321. }
  322. dev_dbg(&dev->pdev->dev,
  323. "wd: successfully register watchdog interface.\n");
  324. watchdog_set_drvdata(&amt_wd_dev, dev);
  325. return 0;
  326. }
  327. void mei_watchdog_unregister(struct mei_device *dev)
  328. {
  329. if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
  330. return;
  331. watchdog_set_drvdata(&amt_wd_dev, NULL);
  332. watchdog_unregister_device(&amt_wd_dev);
  333. }