wd.c 8.7 KB

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