init.c 9.1 KB

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