init.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/pci.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 "client.h"
  23. const char *mei_dev_state_str(int state)
  24. {
  25. #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
  26. switch (state) {
  27. MEI_DEV_STATE(INITIALIZING);
  28. MEI_DEV_STATE(INIT_CLIENTS);
  29. MEI_DEV_STATE(ENABLED);
  30. MEI_DEV_STATE(RESETING);
  31. MEI_DEV_STATE(DISABLED);
  32. MEI_DEV_STATE(RECOVERING_FROM_RESET);
  33. MEI_DEV_STATE(POWER_DOWN);
  34. MEI_DEV_STATE(POWER_UP);
  35. default:
  36. return "unkown";
  37. }
  38. #undef MEI_DEV_STATE
  39. }
  40. /**
  41. * init_mei_device - allocates and initializes the mei device structure
  42. *
  43. * @pdev: The pci device structure
  44. *
  45. * returns The mei_device_device pointer on success, NULL on failure.
  46. */
  47. struct mei_device *mei_device_init(struct pci_dev *pdev)
  48. {
  49. struct mei_device *dev;
  50. dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
  51. if (!dev)
  52. return NULL;
  53. /* setup our list array */
  54. INIT_LIST_HEAD(&dev->file_list);
  55. INIT_LIST_HEAD(&dev->wd_cl.link);
  56. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  57. mutex_init(&dev->device_lock);
  58. init_waitqueue_head(&dev->wait_recvd_msg);
  59. init_waitqueue_head(&dev->wait_stop_wd);
  60. dev->dev_state = MEI_DEV_INITIALIZING;
  61. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  62. mei_io_list_init(&dev->read_list);
  63. mei_io_list_init(&dev->write_list);
  64. mei_io_list_init(&dev->write_waiting_list);
  65. mei_io_list_init(&dev->ctrl_wr_list);
  66. mei_io_list_init(&dev->ctrl_rd_list);
  67. mei_io_list_init(&dev->amthif_cmd_list);
  68. mei_io_list_init(&dev->amthif_rd_complete_list);
  69. dev->pdev = pdev;
  70. return dev;
  71. }
  72. /**
  73. * mei_hw_init - initializes host and fw to start work.
  74. *
  75. * @dev: the device structure
  76. *
  77. * returns 0 on success, <0 on failure.
  78. */
  79. int mei_hw_init(struct mei_device *dev)
  80. {
  81. int err = 0;
  82. int ret;
  83. mutex_lock(&dev->device_lock);
  84. dev->host_hw_state = mei_hcsr_read(dev);
  85. dev->me_hw_state = mei_mecsr_read(dev);
  86. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
  87. dev->host_hw_state, dev->me_hw_state);
  88. /* acknowledge interrupt and stop interupts */
  89. mei_clear_interrupts(dev);
  90. /* Doesn't change in runtime */
  91. dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
  92. dev->recvd_msg = false;
  93. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  94. mei_reset(dev, 1);
  95. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  96. dev->host_hw_state, dev->me_hw_state);
  97. /* wait for ME to turn on ME_RDY */
  98. if (!dev->recvd_msg) {
  99. mutex_unlock(&dev->device_lock);
  100. err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
  101. dev->recvd_msg,
  102. mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
  103. mutex_lock(&dev->device_lock);
  104. }
  105. if (err <= 0 && !dev->recvd_msg) {
  106. dev->dev_state = MEI_DEV_DISABLED;
  107. dev_dbg(&dev->pdev->dev,
  108. "wait_event_interruptible_timeout failed"
  109. "on wait for ME to turn on ME_RDY.\n");
  110. ret = -ENODEV;
  111. goto out;
  112. }
  113. if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
  114. ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
  115. dev->dev_state = MEI_DEV_DISABLED;
  116. dev_dbg(&dev->pdev->dev,
  117. "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  118. dev->host_hw_state, dev->me_hw_state);
  119. if (!(dev->host_hw_state & H_RDY))
  120. dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
  121. if (!(dev->me_hw_state & ME_RDY_HRA))
  122. dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
  123. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  124. ret = -ENODEV;
  125. goto out;
  126. }
  127. if (dev->version.major_version != HBM_MAJOR_VERSION ||
  128. dev->version.minor_version != HBM_MINOR_VERSION) {
  129. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  130. ret = -ENODEV;
  131. goto out;
  132. }
  133. dev->recvd_msg = false;
  134. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  135. dev->host_hw_state, dev->me_hw_state);
  136. dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
  137. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  138. dev_dbg(&dev->pdev->dev, "MEI start success.\n");
  139. ret = 0;
  140. out:
  141. mutex_unlock(&dev->device_lock);
  142. return ret;
  143. }
  144. /**
  145. * mei_hw_reset - resets fw via mei csr register.
  146. *
  147. * @dev: the device structure
  148. * @interrupts_enabled: if interrupt should be enabled after reset.
  149. */
  150. static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
  151. {
  152. dev->host_hw_state |= (H_RST | H_IG);
  153. if (interrupts_enabled)
  154. mei_enable_interrupts(dev);
  155. else
  156. mei_disable_interrupts(dev);
  157. }
  158. /**
  159. * mei_reset - resets host and fw.
  160. *
  161. * @dev: the device structure
  162. * @interrupts_enabled: if interrupt should be enabled after reset.
  163. */
  164. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  165. {
  166. struct mei_cl *cl_pos = NULL;
  167. struct mei_cl *cl_next = NULL;
  168. struct mei_cl_cb *cb_pos = NULL;
  169. struct mei_cl_cb *cb_next = NULL;
  170. bool unexpected;
  171. if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
  172. dev->need_reset = true;
  173. return;
  174. }
  175. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  176. dev->dev_state != MEI_DEV_DISABLED &&
  177. dev->dev_state != MEI_DEV_POWER_DOWN &&
  178. dev->dev_state != MEI_DEV_POWER_UP);
  179. dev->host_hw_state = mei_hcsr_read(dev);
  180. dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
  181. dev->host_hw_state);
  182. mei_hw_reset(dev, interrupts_enabled);
  183. dev->host_hw_state &= ~H_RST;
  184. dev->host_hw_state |= H_IG;
  185. mei_hcsr_set(dev);
  186. dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
  187. dev->host_hw_state);
  188. dev->need_reset = false;
  189. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  190. if (dev->dev_state != MEI_DEV_DISABLED &&
  191. dev->dev_state != MEI_DEV_POWER_DOWN)
  192. dev->dev_state = MEI_DEV_RESETING;
  193. list_for_each_entry_safe(cl_pos,
  194. cl_next, &dev->file_list, link) {
  195. cl_pos->state = MEI_FILE_DISCONNECTED;
  196. cl_pos->mei_flow_ctrl_creds = 0;
  197. cl_pos->read_cb = NULL;
  198. cl_pos->timer_count = 0;
  199. }
  200. /* remove entry if already in list */
  201. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  202. mei_cl_unlink(&dev->wd_cl);
  203. mei_cl_unlink(&dev->iamthif_cl);
  204. mei_amthif_reset_params(dev);
  205. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  206. }
  207. dev->me_clients_num = 0;
  208. dev->rd_msg_hdr = 0;
  209. dev->wd_pending = false;
  210. /* update the state of the registers after reset */
  211. dev->host_hw_state = mei_hcsr_read(dev);
  212. dev->me_hw_state = mei_mecsr_read(dev);
  213. dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  214. dev->host_hw_state, dev->me_hw_state);
  215. if (unexpected)
  216. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  217. mei_dev_state_str(dev->dev_state));
  218. /* Wake up all readings so they can be interrupted */
  219. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  220. if (waitqueue_active(&cl_pos->rx_wait)) {
  221. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  222. wake_up_interruptible(&cl_pos->rx_wait);
  223. }
  224. }
  225. /* remove all waiting requests */
  226. list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
  227. list_del(&cb_pos->list);
  228. mei_io_cb_free(cb_pos);
  229. }
  230. }