wd.c 8.6 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 <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_connect(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 = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  92. hdr->host_addr = dev->wd_cl.host_client_id;
  93. hdr->me_addr = dev->wd_cl.me_client_id;
  94. hdr->msg_complete = 1;
  95. hdr->reserved = 0;
  96. if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_HDR_SIZE))
  97. hdr->length = MEI_WD_START_MSG_SIZE;
  98. else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_HDR_SIZE))
  99. hdr->length = MEI_WD_STOP_MSG_SIZE;
  100. else
  101. return -EINVAL;
  102. return mei_write_message(dev, hdr, dev->wd_data);
  103. }
  104. /**
  105. * mei_wd_stop - sends watchdog stop message to fw.
  106. *
  107. * @dev: the device structure
  108. * @preserve: indicate if to keep the timeout value
  109. *
  110. * returns 0 if success,
  111. * -EIO when message send fails
  112. * -EINVAL when invalid message is to be sent
  113. */
  114. int mei_wd_stop(struct mei_device *dev)
  115. {
  116. int ret;
  117. if (dev->wd_cl.state != MEI_FILE_CONNECTED ||
  118. dev->wd_state != MEI_WD_RUNNING)
  119. return 0;
  120. memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_STOP_MSG_SIZE);
  121. dev->wd_state = MEI_WD_STOPPING;
  122. ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
  123. if (ret < 0)
  124. goto out;
  125. if (ret && dev->mei_host_buffer_is_empty) {
  126. ret = 0;
  127. dev->mei_host_buffer_is_empty = false;
  128. if (!mei_wd_send(dev)) {
  129. ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
  130. if (ret)
  131. goto out;
  132. } else {
  133. dev_err(&dev->pdev->dev, "wd: send stop failed\n");
  134. }
  135. dev->wd_pending = false;
  136. } else {
  137. dev->wd_pending = true;
  138. }
  139. mutex_unlock(&dev->device_lock);
  140. ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
  141. dev->wd_state == MEI_WD_IDLE,
  142. msecs_to_jiffies(MEI_WD_STOP_TIMEOUT));
  143. mutex_lock(&dev->device_lock);
  144. if (dev->wd_state == MEI_WD_IDLE) {
  145. dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
  146. ret = 0;
  147. } else {
  148. if (!ret)
  149. ret = -ETIMEDOUT;
  150. dev_warn(&dev->pdev->dev,
  151. "wd: stop failed to complete ret=%d.\n", ret);
  152. }
  153. out:
  154. return ret;
  155. }
  156. /*
  157. * mei_wd_ops_start - wd start command from the watchdog core.
  158. *
  159. * @wd_dev - watchdog device struct
  160. *
  161. * returns 0 if success, negative errno code for failure
  162. */
  163. static int mei_wd_ops_start(struct watchdog_device *wd_dev)
  164. {
  165. int err = -ENODEV;
  166. struct mei_device *dev;
  167. dev = watchdog_get_drvdata(wd_dev);
  168. if (!dev)
  169. return -ENODEV;
  170. mutex_lock(&dev->device_lock);
  171. if (dev->dev_state != MEI_DEV_ENABLED) {
  172. dev_dbg(&dev->pdev->dev,
  173. "wd: dev_state != MEI_DEV_ENABLED dev_state = %s\n",
  174. mei_dev_state_str(dev->dev_state));
  175. goto end_unlock;
  176. }
  177. if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
  178. dev_dbg(&dev->pdev->dev,
  179. "MEI Driver is not connected to Watchdog Client\n");
  180. goto end_unlock;
  181. }
  182. mei_wd_set_start_timeout(dev, dev->wd_timeout);
  183. err = 0;
  184. end_unlock:
  185. mutex_unlock(&dev->device_lock);
  186. return err;
  187. }
  188. /*
  189. * mei_wd_ops_stop - wd stop command from the watchdog core.
  190. *
  191. * @wd_dev - watchdog device struct
  192. *
  193. * returns 0 if success, negative errno code for failure
  194. */
  195. static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
  196. {
  197. struct mei_device *dev;
  198. dev = watchdog_get_drvdata(wd_dev);
  199. if (!dev)
  200. return -ENODEV;
  201. mutex_lock(&dev->device_lock);
  202. mei_wd_stop(dev);
  203. mutex_unlock(&dev->device_lock);
  204. return 0;
  205. }
  206. /*
  207. * mei_wd_ops_ping - wd ping 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_ping(struct watchdog_device *wd_dev)
  214. {
  215. int ret = 0;
  216. struct mei_device *dev;
  217. dev = watchdog_get_drvdata(wd_dev);
  218. if (!dev)
  219. return -ENODEV;
  220. mutex_lock(&dev->device_lock);
  221. if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
  222. dev_err(&dev->pdev->dev, "wd: not connected.\n");
  223. ret = -ENODEV;
  224. goto end;
  225. }
  226. dev->wd_state = MEI_WD_RUNNING;
  227. /* Check if we can send the ping to HW*/
  228. if (dev->mei_host_buffer_is_empty &&
  229. mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
  230. dev->mei_host_buffer_is_empty = false;
  231. dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
  232. if (mei_wd_send(dev)) {
  233. dev_err(&dev->pdev->dev, "wd: send failed.\n");
  234. ret = -EIO;
  235. goto end;
  236. }
  237. if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
  238. dev_err(&dev->pdev->dev,
  239. "wd: mei_flow_ctrl_reduce() failed.\n");
  240. ret = -EIO;
  241. goto end;
  242. }
  243. } else {
  244. dev->wd_pending = true;
  245. }
  246. end:
  247. mutex_unlock(&dev->device_lock);
  248. return ret;
  249. }
  250. /*
  251. * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
  252. *
  253. * @wd_dev - watchdog device struct
  254. * @timeout - timeout value to set
  255. *
  256. * returns 0 if success, negative errno code for failure
  257. */
  258. static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
  259. {
  260. struct mei_device *dev;
  261. dev = watchdog_get_drvdata(wd_dev);
  262. if (!dev)
  263. return -ENODEV;
  264. /* Check Timeout value */
  265. if (timeout < MEI_WD_MIN_TIMEOUT || timeout > MEI_WD_MAX_TIMEOUT)
  266. return -EINVAL;
  267. mutex_lock(&dev->device_lock);
  268. dev->wd_timeout = timeout;
  269. wd_dev->timeout = timeout;
  270. mei_wd_set_start_timeout(dev, dev->wd_timeout);
  271. mutex_unlock(&dev->device_lock);
  272. return 0;
  273. }
  274. /*
  275. * Watchdog Device structs
  276. */
  277. static const struct watchdog_ops wd_ops = {
  278. .owner = THIS_MODULE,
  279. .start = mei_wd_ops_start,
  280. .stop = mei_wd_ops_stop,
  281. .ping = mei_wd_ops_ping,
  282. .set_timeout = mei_wd_ops_set_timeout,
  283. };
  284. static const struct watchdog_info wd_info = {
  285. .identity = INTEL_AMT_WATCHDOG_ID,
  286. .options = WDIOF_KEEPALIVEPING |
  287. WDIOF_SETTIMEOUT |
  288. WDIOF_ALARMONLY,
  289. };
  290. static struct watchdog_device amt_wd_dev = {
  291. .info = &wd_info,
  292. .ops = &wd_ops,
  293. .timeout = MEI_WD_DEFAULT_TIMEOUT,
  294. .min_timeout = MEI_WD_MIN_TIMEOUT,
  295. .max_timeout = MEI_WD_MAX_TIMEOUT,
  296. };
  297. void mei_watchdog_register(struct mei_device *dev)
  298. {
  299. if (watchdog_register_device(&amt_wd_dev)) {
  300. dev_err(&dev->pdev->dev,
  301. "wd: unable to register watchdog device.\n");
  302. return;
  303. }
  304. dev_dbg(&dev->pdev->dev,
  305. "wd: successfully register watchdog interface.\n");
  306. watchdog_set_drvdata(&amt_wd_dev, dev);
  307. }
  308. void mei_watchdog_unregister(struct mei_device *dev)
  309. {
  310. if (watchdog_get_drvdata(&amt_wd_dev) == NULL)
  311. return;
  312. watchdog_set_drvdata(&amt_wd_dev, NULL);
  313. watchdog_unregister_device(&amt_wd_dev);
  314. }