wd.c 8.8 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_PARAMS_SIZE);
  46. memcpy(dev->wd_data + MEI_WD_PARAMS_SIZE, &timeout, sizeof(u16));
  47. }
  48. /**
  49. * host_init_wd - mei initialization wd.
  50. *
  51. * @dev: the device structure
  52. * returns -ENENT if wd client cannot be found
  53. * -EIO if write has failed
  54. */
  55. int mei_wd_host_init(struct mei_device *dev)
  56. {
  57. mei_cl_init(&dev->wd_cl, dev);
  58. /* look for WD client and connect to it */
  59. dev->wd_cl.state = MEI_FILE_DISCONNECTED;
  60. dev->wd_timeout = AMT_WD_DEFAULT_TIMEOUT;
  61. /* find ME WD client */
  62. mei_find_me_client_update_filext(dev, &dev->wd_cl,
  63. &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
  64. dev_dbg(&dev->pdev->dev, "wd: check client\n");
  65. if (MEI_FILE_CONNECTING != dev->wd_cl.state) {
  66. dev_info(&dev->pdev->dev, "wd: failed to find the client\n");
  67. return -ENOENT;
  68. }
  69. if (mei_connect(dev, &dev->wd_cl)) {
  70. dev_err(&dev->pdev->dev, "wd: failed to connect to the client\n");
  71. dev->wd_cl.state = MEI_FILE_DISCONNECTED;
  72. dev->wd_cl.host_client_id = 0;
  73. return -EIO;
  74. }
  75. dev->wd_cl.timer_count = CONNECT_TIMEOUT;
  76. return 0;
  77. }
  78. /**
  79. * mei_wd_send - sends watch dog message to fw.
  80. *
  81. * @dev: the device structure
  82. *
  83. * returns 0 if success,
  84. * -EIO when message send fails
  85. * -EINVAL when invalid message is to be sent
  86. */
  87. int mei_wd_send(struct mei_device *dev)
  88. {
  89. struct mei_msg_hdr *mei_hdr;
  90. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  91. mei_hdr->host_addr = dev->wd_cl.host_client_id;
  92. mei_hdr->me_addr = dev->wd_cl.me_client_id;
  93. mei_hdr->msg_complete = 1;
  94. mei_hdr->reserved = 0;
  95. if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE))
  96. mei_hdr->length = MEI_START_WD_DATA_SIZE;
  97. else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE))
  98. mei_hdr->length = MEI_WD_PARAMS_SIZE;
  99. else
  100. return -EINVAL;
  101. return mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length);
  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, bool preserve)
  114. {
  115. int ret;
  116. u16 wd_timeout = dev->wd_timeout;
  117. cancel_delayed_work(&dev->timer_work);
  118. if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
  119. return 0;
  120. dev->wd_timeout = 0;
  121. dev->wd_due_counter = 0;
  122. memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
  123. dev->stop = true;
  124. ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
  125. if (ret < 0)
  126. goto out;
  127. if (ret && dev->mei_host_buffer_is_empty) {
  128. ret = 0;
  129. dev->mei_host_buffer_is_empty = false;
  130. if (!mei_wd_send(dev)) {
  131. ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
  132. if (ret)
  133. goto out;
  134. } else {
  135. dev_err(&dev->pdev->dev, "wd: send stop failed\n");
  136. }
  137. dev->wd_pending = false;
  138. } else {
  139. dev->wd_pending = true;
  140. }
  141. dev->wd_stopped = false;
  142. mutex_unlock(&dev->device_lock);
  143. ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
  144. dev->wd_stopped, 10 * HZ);
  145. mutex_lock(&dev->device_lock);
  146. if (dev->wd_stopped) {
  147. dev_dbg(&dev->pdev->dev, "wd: stop completed ret=%d.\n", ret);
  148. ret = 0;
  149. } else {
  150. if (!ret)
  151. ret = -ETIMEDOUT;
  152. dev_warn(&dev->pdev->dev,
  153. "wd: stop failed to complete ret=%d.\n", ret);
  154. }
  155. if (preserve)
  156. dev->wd_timeout = wd_timeout;
  157. out:
  158. return ret;
  159. }
  160. /*
  161. * mei_wd_ops_start - wd start command from the watchdog core.
  162. *
  163. * @wd_dev - watchdog device struct
  164. *
  165. * returns 0 if success, negative errno code for failure
  166. */
  167. static int mei_wd_ops_start(struct watchdog_device *wd_dev)
  168. {
  169. int err = -ENODEV;
  170. struct mei_device *dev;
  171. dev = pci_get_drvdata(mei_device);
  172. if (!dev)
  173. return -ENODEV;
  174. mutex_lock(&dev->device_lock);
  175. if (dev->mei_state != MEI_ENABLED) {
  176. dev_dbg(&dev->pdev->dev,
  177. "wd: mei_state != MEI_ENABLED mei_state = %d\n",
  178. dev->mei_state);
  179. goto end_unlock;
  180. }
  181. if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
  182. dev_dbg(&dev->pdev->dev,
  183. "MEI Driver is not connected to Watchdog Client\n");
  184. goto end_unlock;
  185. }
  186. mei_wd_set_start_timeout(dev, dev->wd_timeout);
  187. err = 0;
  188. end_unlock:
  189. mutex_unlock(&dev->device_lock);
  190. return err;
  191. }
  192. /*
  193. * mei_wd_ops_stop - wd stop command from the watchdog core.
  194. *
  195. * @wd_dev - watchdog device struct
  196. *
  197. * returns 0 if success, negative errno code for failure
  198. */
  199. static int mei_wd_ops_stop(struct watchdog_device *wd_dev)
  200. {
  201. struct mei_device *dev;
  202. dev = pci_get_drvdata(mei_device);
  203. if (!dev)
  204. return -ENODEV;
  205. mutex_lock(&dev->device_lock);
  206. mei_wd_stop(dev, false);
  207. mutex_unlock(&dev->device_lock);
  208. return 0;
  209. }
  210. /*
  211. * mei_wd_ops_ping - wd ping command from the watchdog core.
  212. *
  213. * @wd_dev - watchdog device struct
  214. *
  215. * returns 0 if success, negative errno code for failure
  216. */
  217. static int mei_wd_ops_ping(struct watchdog_device *wd_dev)
  218. {
  219. int ret = 0;
  220. struct mei_device *dev;
  221. dev = pci_get_drvdata(mei_device);
  222. if (!dev)
  223. return -ENODEV;
  224. mutex_lock(&dev->device_lock);
  225. if (dev->wd_cl.state != MEI_FILE_CONNECTED) {
  226. dev_err(&dev->pdev->dev, "wd: not connected.\n");
  227. ret = -ENODEV;
  228. goto end;
  229. }
  230. /* Check if we can send the ping to HW*/
  231. if (dev->mei_host_buffer_is_empty &&
  232. mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
  233. dev->mei_host_buffer_is_empty = false;
  234. dev_dbg(&dev->pdev->dev, "wd: sending ping\n");
  235. if (mei_wd_send(dev)) {
  236. dev_err(&dev->pdev->dev, "wd: send failed.\n");
  237. ret = -EIO;
  238. goto end;
  239. }
  240. if (mei_flow_ctrl_reduce(dev, &dev->wd_cl)) {
  241. dev_err(&dev->pdev->dev,
  242. "wd: mei_flow_ctrl_reduce() failed.\n");
  243. ret = -EIO;
  244. goto end;
  245. }
  246. } else {
  247. dev->wd_pending = true;
  248. }
  249. end:
  250. mutex_unlock(&dev->device_lock);
  251. return ret;
  252. }
  253. /*
  254. * mei_wd_ops_set_timeout - wd set timeout command from the watchdog core.
  255. *
  256. * @wd_dev - watchdog device struct
  257. * @timeout - timeout value to set
  258. *
  259. * returns 0 if success, negative errno code for failure
  260. */
  261. static int mei_wd_ops_set_timeout(struct watchdog_device *wd_dev, unsigned int timeout)
  262. {
  263. struct mei_device *dev;
  264. dev = pci_get_drvdata(mei_device);
  265. if (!dev)
  266. return -ENODEV;
  267. /* Check Timeout value */
  268. if (timeout < AMT_WD_MIN_TIMEOUT || timeout > AMT_WD_MAX_TIMEOUT)
  269. return -EINVAL;
  270. mutex_lock(&dev->device_lock);
  271. dev->wd_timeout = timeout;
  272. wd_dev->timeout = timeout;
  273. mei_wd_set_start_timeout(dev, dev->wd_timeout);
  274. mutex_unlock(&dev->device_lock);
  275. return 0;
  276. }
  277. /*
  278. * Watchdog Device structs
  279. */
  280. static const struct watchdog_ops wd_ops = {
  281. .owner = THIS_MODULE,
  282. .start = mei_wd_ops_start,
  283. .stop = mei_wd_ops_stop,
  284. .ping = mei_wd_ops_ping,
  285. .set_timeout = mei_wd_ops_set_timeout,
  286. };
  287. static const struct watchdog_info wd_info = {
  288. .identity = INTEL_AMT_WATCHDOG_ID,
  289. .options = WDIOF_KEEPALIVEPING,
  290. };
  291. static struct watchdog_device amt_wd_dev = {
  292. .info = &wd_info,
  293. .ops = &wd_ops,
  294. .timeout = AMT_WD_DEFAULT_TIMEOUT,
  295. .min_timeout = AMT_WD_MIN_TIMEOUT,
  296. .max_timeout = AMT_WD_MAX_TIMEOUT,
  297. };
  298. void mei_watchdog_register(struct mei_device *dev)
  299. {
  300. dev_dbg(&dev->pdev->dev, "dev->wd_timeout =%d.\n", dev->wd_timeout);
  301. dev->wd_due_counter = !!dev->wd_timeout;
  302. if (watchdog_register_device(&amt_wd_dev)) {
  303. dev_err(&dev->pdev->dev,
  304. "wd: unable to register watchdog device.\n");
  305. dev->wd_interface_reg = false;
  306. } else {
  307. dev_dbg(&dev->pdev->dev,
  308. "wd: successfully register watchdog interface.\n");
  309. dev->wd_interface_reg = true;
  310. }
  311. }
  312. void mei_watchdog_unregister(struct mei_device *dev)
  313. {
  314. if (dev->wd_interface_reg)
  315. watchdog_unregister_device(&amt_wd_dev);
  316. dev->wd_interface_reg = false;
  317. }