wd.c 8.5 KB

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