wd.c 8.7 KB

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