init.c 9.9 KB

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