init.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/export.h>
  17. #include <linux/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/delay.h>
  20. #include <linux/mei.h>
  21. #include "mei_dev.h"
  22. #include "hbm.h"
  23. #include "client.h"
  24. const char *mei_dev_state_str(int state)
  25. {
  26. #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
  27. switch (state) {
  28. MEI_DEV_STATE(INITIALIZING);
  29. MEI_DEV_STATE(INIT_CLIENTS);
  30. MEI_DEV_STATE(ENABLED);
  31. MEI_DEV_STATE(RESETTING);
  32. MEI_DEV_STATE(DISABLED);
  33. MEI_DEV_STATE(POWER_DOWN);
  34. MEI_DEV_STATE(POWER_UP);
  35. default:
  36. return "unknown";
  37. }
  38. #undef MEI_DEV_STATE
  39. }
  40. const char *mei_pg_state_str(enum mei_pg_state state)
  41. {
  42. #define MEI_PG_STATE(state) case MEI_PG_##state: return #state
  43. switch (state) {
  44. MEI_PG_STATE(OFF);
  45. MEI_PG_STATE(ON);
  46. default:
  47. return "unknown";
  48. }
  49. #undef MEI_PG_STATE
  50. }
  51. /**
  52. * mei_cancel_work - Cancel mei background jobs
  53. *
  54. * @dev: the device structure
  55. */
  56. void mei_cancel_work(struct mei_device *dev)
  57. {
  58. cancel_work_sync(&dev->init_work);
  59. cancel_work_sync(&dev->reset_work);
  60. cancel_delayed_work(&dev->timer_work);
  61. }
  62. EXPORT_SYMBOL_GPL(mei_cancel_work);
  63. /**
  64. * mei_reset - resets host and fw.
  65. *
  66. * @dev: the device structure
  67. *
  68. * Return: 0 on success or < 0 if the reset hasn't succeeded
  69. */
  70. int mei_reset(struct mei_device *dev)
  71. {
  72. enum mei_dev_state state = dev->dev_state;
  73. bool interrupts_enabled;
  74. int ret;
  75. if (state != MEI_DEV_INITIALIZING &&
  76. state != MEI_DEV_DISABLED &&
  77. state != MEI_DEV_POWER_DOWN &&
  78. state != MEI_DEV_POWER_UP) {
  79. struct mei_fw_status fw_status;
  80. mei_fw_status(dev, &fw_status);
  81. dev_warn(dev->dev,
  82. "unexpected reset: dev_state = %s " FW_STS_FMT "\n",
  83. mei_dev_state_str(state), FW_STS_PRM(fw_status));
  84. }
  85. /* we're already in reset, cancel the init timer
  86. * if the reset was called due the hbm protocol error
  87. * we need to call it before hw start
  88. * so the hbm watchdog won't kick in
  89. */
  90. mei_hbm_idle(dev);
  91. /* enter reset flow */
  92. interrupts_enabled = state != MEI_DEV_POWER_DOWN;
  93. dev->dev_state = MEI_DEV_RESETTING;
  94. dev->reset_count++;
  95. if (dev->reset_count > MEI_MAX_CONSEC_RESET) {
  96. dev_err(dev->dev, "reset: reached maximal consecutive resets: disabling the device\n");
  97. dev->dev_state = MEI_DEV_DISABLED;
  98. return -ENODEV;
  99. }
  100. ret = mei_hw_reset(dev, interrupts_enabled);
  101. /* fall through and remove the sw state even if hw reset has failed */
  102. /* no need to clean up software state in case of power up */
  103. if (state != MEI_DEV_INITIALIZING &&
  104. state != MEI_DEV_POWER_UP) {
  105. /* remove all waiting requests */
  106. mei_cl_all_write_clear(dev);
  107. mei_cl_all_disconnect(dev);
  108. /* wake up all readers and writers so they can be interrupted */
  109. mei_cl_all_wakeup(dev);
  110. /* remove entry if already in list */
  111. dev_dbg(dev->dev, "remove iamthif and wd from the file list.\n");
  112. mei_cl_unlink(&dev->wd_cl);
  113. mei_cl_unlink(&dev->iamthif_cl);
  114. mei_amthif_reset_params(dev);
  115. }
  116. mei_hbm_reset(dev);
  117. dev->rd_msg_hdr = 0;
  118. dev->wd_pending = false;
  119. if (ret) {
  120. dev_err(dev->dev, "hw_reset failed ret = %d\n", ret);
  121. return ret;
  122. }
  123. if (state == MEI_DEV_POWER_DOWN) {
  124. dev_dbg(dev->dev, "powering down: end of reset\n");
  125. dev->dev_state = MEI_DEV_DISABLED;
  126. return 0;
  127. }
  128. ret = mei_hw_start(dev);
  129. if (ret) {
  130. dev_err(dev->dev, "hw_start failed ret = %d\n", ret);
  131. return ret;
  132. }
  133. dev_dbg(dev->dev, "link is established start sending messages.\n");
  134. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  135. ret = mei_hbm_start_req(dev);
  136. if (ret) {
  137. dev_err(dev->dev, "hbm_start failed ret = %d\n", ret);
  138. dev->dev_state = MEI_DEV_RESETTING;
  139. return ret;
  140. }
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(mei_reset);
  144. /**
  145. * mei_start - initializes host and fw to start work.
  146. *
  147. * @dev: the device structure
  148. *
  149. * Return: 0 on success, <0 on failure.
  150. */
  151. int mei_start(struct mei_device *dev)
  152. {
  153. int ret;
  154. mutex_lock(&dev->device_lock);
  155. /* acknowledge interrupt and stop interrupts */
  156. mei_clear_interrupts(dev);
  157. mei_hw_config(dev);
  158. dev_dbg(dev->dev, "reset in start the mei device.\n");
  159. dev->reset_count = 0;
  160. do {
  161. dev->dev_state = MEI_DEV_INITIALIZING;
  162. ret = mei_reset(dev);
  163. if (ret == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
  164. dev_err(dev->dev, "reset failed ret = %d", ret);
  165. goto err;
  166. }
  167. } while (ret);
  168. /* we cannot start the device w/o hbm start message completed */
  169. if (dev->dev_state == MEI_DEV_DISABLED) {
  170. dev_err(dev->dev, "reset failed");
  171. goto err;
  172. }
  173. if (mei_hbm_start_wait(dev)) {
  174. dev_err(dev->dev, "HBM haven't started");
  175. goto err;
  176. }
  177. if (!mei_host_is_ready(dev)) {
  178. dev_err(dev->dev, "host is not ready.\n");
  179. goto err;
  180. }
  181. if (!mei_hw_is_ready(dev)) {
  182. dev_err(dev->dev, "ME is not ready.\n");
  183. goto err;
  184. }
  185. if (!mei_hbm_version_is_supported(dev)) {
  186. dev_dbg(dev->dev, "MEI start failed.\n");
  187. goto err;
  188. }
  189. dev_dbg(dev->dev, "link layer has been established.\n");
  190. mutex_unlock(&dev->device_lock);
  191. return 0;
  192. err:
  193. dev_err(dev->dev, "link layer initialization failed.\n");
  194. dev->dev_state = MEI_DEV_DISABLED;
  195. mutex_unlock(&dev->device_lock);
  196. return -ENODEV;
  197. }
  198. EXPORT_SYMBOL_GPL(mei_start);
  199. /**
  200. * mei_restart - restart device after suspend
  201. *
  202. * @dev: the device structure
  203. *
  204. * Return: 0 on success or -ENODEV if the restart hasn't succeeded
  205. */
  206. int mei_restart(struct mei_device *dev)
  207. {
  208. int err;
  209. mutex_lock(&dev->device_lock);
  210. mei_clear_interrupts(dev);
  211. dev->dev_state = MEI_DEV_POWER_UP;
  212. dev->reset_count = 0;
  213. err = mei_reset(dev);
  214. mutex_unlock(&dev->device_lock);
  215. if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
  216. dev_err(dev->dev, "device disabled = %d\n", err);
  217. return -ENODEV;
  218. }
  219. /* try to start again */
  220. if (err)
  221. schedule_work(&dev->reset_work);
  222. return 0;
  223. }
  224. EXPORT_SYMBOL_GPL(mei_restart);
  225. static void mei_reset_work(struct work_struct *work)
  226. {
  227. struct mei_device *dev =
  228. container_of(work, struct mei_device, reset_work);
  229. int ret;
  230. mutex_lock(&dev->device_lock);
  231. ret = mei_reset(dev);
  232. mutex_unlock(&dev->device_lock);
  233. if (dev->dev_state == MEI_DEV_DISABLED) {
  234. dev_err(dev->dev, "device disabled = %d\n", ret);
  235. return;
  236. }
  237. /* retry reset in case of failure */
  238. if (ret)
  239. schedule_work(&dev->reset_work);
  240. }
  241. void mei_stop(struct mei_device *dev)
  242. {
  243. dev_dbg(dev->dev, "stopping the device.\n");
  244. mei_cancel_work(dev);
  245. mei_nfc_host_exit(dev);
  246. mei_cl_bus_remove_devices(dev);
  247. mutex_lock(&dev->device_lock);
  248. mei_wd_stop(dev);
  249. dev->dev_state = MEI_DEV_POWER_DOWN;
  250. mei_reset(dev);
  251. mutex_unlock(&dev->device_lock);
  252. mei_watchdog_unregister(dev);
  253. }
  254. EXPORT_SYMBOL_GPL(mei_stop);
  255. /**
  256. * mei_write_is_idle - check if the write queues are idle
  257. *
  258. * @dev: the device structure
  259. *
  260. * Return: true of there is no pending write
  261. */
  262. bool mei_write_is_idle(struct mei_device *dev)
  263. {
  264. bool idle = (dev->dev_state == MEI_DEV_ENABLED &&
  265. list_empty(&dev->ctrl_wr_list.list) &&
  266. list_empty(&dev->write_list.list));
  267. dev_dbg(dev->dev, "write pg: is idle[%d] state=%s ctrl=%d write=%d\n",
  268. idle,
  269. mei_dev_state_str(dev->dev_state),
  270. list_empty(&dev->ctrl_wr_list.list),
  271. list_empty(&dev->write_list.list));
  272. return idle;
  273. }
  274. EXPORT_SYMBOL_GPL(mei_write_is_idle);
  275. /**
  276. * mei_device_init -- initialize mei_device structure
  277. *
  278. * @dev: the mei device
  279. * @device: the device structure
  280. * @hw_ops: hw operations
  281. */
  282. void mei_device_init(struct mei_device *dev,
  283. struct device *device,
  284. const struct mei_hw_ops *hw_ops)
  285. {
  286. /* setup our list array */
  287. INIT_LIST_HEAD(&dev->file_list);
  288. INIT_LIST_HEAD(&dev->device_list);
  289. INIT_LIST_HEAD(&dev->me_clients);
  290. mutex_init(&dev->device_lock);
  291. init_waitqueue_head(&dev->wait_hw_ready);
  292. init_waitqueue_head(&dev->wait_pg);
  293. init_waitqueue_head(&dev->wait_hbm_start);
  294. init_waitqueue_head(&dev->wait_stop_wd);
  295. dev->dev_state = MEI_DEV_INITIALIZING;
  296. dev->reset_count = 0;
  297. mei_io_list_init(&dev->read_list);
  298. mei_io_list_init(&dev->write_list);
  299. mei_io_list_init(&dev->write_waiting_list);
  300. mei_io_list_init(&dev->ctrl_wr_list);
  301. mei_io_list_init(&dev->ctrl_rd_list);
  302. INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
  303. INIT_WORK(&dev->init_work, mei_host_client_init);
  304. INIT_WORK(&dev->reset_work, mei_reset_work);
  305. INIT_LIST_HEAD(&dev->wd_cl.link);
  306. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  307. mei_io_list_init(&dev->amthif_cmd_list);
  308. mei_io_list_init(&dev->amthif_rd_complete_list);
  309. bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
  310. dev->open_handle_count = 0;
  311. /*
  312. * Reserving the first client ID
  313. * 0: Reserved for MEI Bus Message communications
  314. */
  315. bitmap_set(dev->host_clients_map, 0, 1);
  316. dev->pg_event = MEI_PG_EVENT_IDLE;
  317. dev->ops = hw_ops;
  318. dev->dev = device;
  319. }
  320. EXPORT_SYMBOL_GPL(mei_device_init);