wd.c 8.6 KB

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