wd.c 8.7 KB

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