init.c 8.5 KB

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