init.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  102. }
  103. dev->me_clients_num = 0;
  104. dev->rd_msg_hdr = 0;
  105. dev->wd_pending = false;
  106. if (ret) {
  107. dev_err(&dev->pdev->dev, "hw_reset failed ret = %d\n", ret);
  108. dev->dev_state = MEI_DEV_DISABLED;
  109. return ret;
  110. }
  111. if (state == MEI_DEV_POWER_DOWN) {
  112. dev_dbg(&dev->pdev->dev, "powering down: end of reset\n");
  113. dev->dev_state = MEI_DEV_DISABLED;
  114. return 0;
  115. }
  116. ret = mei_hw_start(dev);
  117. if (ret) {
  118. dev_err(&dev->pdev->dev, "hw_start failed ret = %d\n", ret);
  119. dev->dev_state = MEI_DEV_DISABLED;
  120. return ret;
  121. }
  122. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  123. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  124. ret = mei_hbm_start_req(dev);
  125. if (ret) {
  126. dev_err(&dev->pdev->dev, "hbm_start failed ret = %d\n", ret);
  127. dev->dev_state = MEI_DEV_DISABLED;
  128. return ret;
  129. }
  130. return 0;
  131. }
  132. EXPORT_SYMBOL_GPL(mei_reset);
  133. /**
  134. * mei_start - initializes host and fw to start work.
  135. *
  136. * @dev: the device structure
  137. *
  138. * returns 0 on success, <0 on failure.
  139. */
  140. int mei_start(struct mei_device *dev)
  141. {
  142. mutex_lock(&dev->device_lock);
  143. /* acknowledge interrupt and stop interrupts */
  144. mei_clear_interrupts(dev);
  145. mei_hw_config(dev);
  146. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  147. dev->dev_state = MEI_DEV_INITIALIZING;
  148. dev->reset_count = 0;
  149. mei_reset(dev);
  150. if (dev->dev_state == MEI_DEV_DISABLED) {
  151. dev_err(&dev->pdev->dev, "reset failed");
  152. goto err;
  153. }
  154. if (mei_hbm_start_wait(dev)) {
  155. dev_err(&dev->pdev->dev, "HBM haven't started");
  156. goto err;
  157. }
  158. if (!mei_host_is_ready(dev)) {
  159. dev_err(&dev->pdev->dev, "host is not ready.\n");
  160. goto err;
  161. }
  162. if (!mei_hw_is_ready(dev)) {
  163. dev_err(&dev->pdev->dev, "ME is not ready.\n");
  164. goto err;
  165. }
  166. if (!mei_hbm_version_is_supported(dev)) {
  167. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  168. goto err;
  169. }
  170. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  171. mutex_unlock(&dev->device_lock);
  172. return 0;
  173. err:
  174. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  175. dev->dev_state = MEI_DEV_DISABLED;
  176. mutex_unlock(&dev->device_lock);
  177. return -ENODEV;
  178. }
  179. EXPORT_SYMBOL_GPL(mei_start);
  180. /**
  181. * mei_restart - restart device after suspend
  182. *
  183. * @dev: the device structure
  184. *
  185. * returns 0 on success or -ENODEV if the restart hasn't succeeded
  186. */
  187. int mei_restart(struct mei_device *dev)
  188. {
  189. int err;
  190. mutex_lock(&dev->device_lock);
  191. mei_clear_interrupts(dev);
  192. dev->dev_state = MEI_DEV_POWER_UP;
  193. dev->reset_count = 0;
  194. err = mei_reset(dev);
  195. mutex_unlock(&dev->device_lock);
  196. if (err || dev->dev_state == MEI_DEV_DISABLED)
  197. return -ENODEV;
  198. return 0;
  199. }
  200. EXPORT_SYMBOL_GPL(mei_restart);
  201. static void mei_reset_work(struct work_struct *work)
  202. {
  203. struct mei_device *dev =
  204. container_of(work, struct mei_device, reset_work);
  205. mutex_lock(&dev->device_lock);
  206. mei_reset(dev);
  207. mutex_unlock(&dev->device_lock);
  208. if (dev->dev_state == MEI_DEV_DISABLED)
  209. dev_err(&dev->pdev->dev, "reset failed");
  210. }
  211. void mei_stop(struct mei_device *dev)
  212. {
  213. dev_dbg(&dev->pdev->dev, "stopping the device.\n");
  214. mei_cancel_work(dev);
  215. mei_nfc_host_exit(dev);
  216. mutex_lock(&dev->device_lock);
  217. mei_wd_stop(dev);
  218. dev->dev_state = MEI_DEV_POWER_DOWN;
  219. mei_reset(dev);
  220. mutex_unlock(&dev->device_lock);
  221. mei_watchdog_unregister(dev);
  222. }
  223. EXPORT_SYMBOL_GPL(mei_stop);
  224. void mei_device_init(struct mei_device *dev)
  225. {
  226. /* setup our list array */
  227. INIT_LIST_HEAD(&dev->file_list);
  228. INIT_LIST_HEAD(&dev->device_list);
  229. mutex_init(&dev->device_lock);
  230. init_waitqueue_head(&dev->wait_hw_ready);
  231. init_waitqueue_head(&dev->wait_recvd_msg);
  232. init_waitqueue_head(&dev->wait_stop_wd);
  233. dev->dev_state = MEI_DEV_INITIALIZING;
  234. dev->reset_count = 0;
  235. mei_io_list_init(&dev->read_list);
  236. mei_io_list_init(&dev->write_list);
  237. mei_io_list_init(&dev->write_waiting_list);
  238. mei_io_list_init(&dev->ctrl_wr_list);
  239. mei_io_list_init(&dev->ctrl_rd_list);
  240. INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
  241. INIT_WORK(&dev->init_work, mei_host_client_init);
  242. INIT_WORK(&dev->reset_work, mei_reset_work);
  243. INIT_LIST_HEAD(&dev->wd_cl.link);
  244. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  245. mei_io_list_init(&dev->amthif_cmd_list);
  246. mei_io_list_init(&dev->amthif_rd_complete_list);
  247. bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
  248. dev->open_handle_count = 0;
  249. /*
  250. * Reserving the first client ID
  251. * 0: Reserved for MEI Bus Message communications
  252. */
  253. bitmap_set(dev->host_clients_map, 0, 1);
  254. }
  255. EXPORT_SYMBOL_GPL(mei_device_init);