init.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. mei_io_list_init(&dev->read_list);
  62. mei_io_list_init(&dev->write_list);
  63. mei_io_list_init(&dev->write_waiting_list);
  64. mei_io_list_init(&dev->ctrl_wr_list);
  65. mei_io_list_init(&dev->ctrl_rd_list);
  66. mei_io_list_init(&dev->amthif_cmd_list);
  67. mei_io_list_init(&dev->amthif_rd_complete_list);
  68. dev->pdev = pdev;
  69. return dev;
  70. }
  71. /**
  72. * mei_hw_init - initializes host and fw to start work.
  73. *
  74. * @dev: the device structure
  75. *
  76. * returns 0 on success, <0 on failure.
  77. */
  78. int mei_hw_init(struct mei_device *dev)
  79. {
  80. int err = 0;
  81. int ret;
  82. mutex_lock(&dev->device_lock);
  83. dev->host_hw_state = mei_hcsr_read(dev);
  84. dev->me_hw_state = mei_mecsr_read(dev);
  85. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
  86. dev->host_hw_state, dev->me_hw_state);
  87. /* acknowledge interrupt and stop interupts */
  88. mei_clear_interrupts(dev);
  89. /* Doesn't change in runtime */
  90. dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
  91. dev->recvd_msg = false;
  92. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  93. mei_reset(dev, 1);
  94. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  95. dev->host_hw_state, dev->me_hw_state);
  96. /* wait for ME to turn on ME_RDY */
  97. if (!dev->recvd_msg) {
  98. mutex_unlock(&dev->device_lock);
  99. err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
  100. dev->recvd_msg,
  101. mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
  102. mutex_lock(&dev->device_lock);
  103. }
  104. if (err <= 0 && !dev->recvd_msg) {
  105. dev->dev_state = MEI_DEV_DISABLED;
  106. dev_dbg(&dev->pdev->dev,
  107. "wait_event_interruptible_timeout failed"
  108. "on wait for ME to turn on ME_RDY.\n");
  109. ret = -ENODEV;
  110. goto out;
  111. }
  112. if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
  113. ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
  114. dev->dev_state = MEI_DEV_DISABLED;
  115. dev_dbg(&dev->pdev->dev,
  116. "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  117. dev->host_hw_state, dev->me_hw_state);
  118. if (!(dev->host_hw_state & H_RDY))
  119. dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
  120. if (!(dev->me_hw_state & ME_RDY_HRA))
  121. dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
  122. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  123. ret = -ENODEV;
  124. goto out;
  125. }
  126. if (dev->version.major_version != HBM_MAJOR_VERSION ||
  127. dev->version.minor_version != HBM_MINOR_VERSION) {
  128. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  129. ret = -ENODEV;
  130. goto out;
  131. }
  132. dev->recvd_msg = false;
  133. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  134. dev->host_hw_state, dev->me_hw_state);
  135. dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
  136. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  137. dev_dbg(&dev->pdev->dev, "MEI start success.\n");
  138. ret = 0;
  139. out:
  140. mutex_unlock(&dev->device_lock);
  141. return ret;
  142. }
  143. /**
  144. * mei_reset - resets host and fw.
  145. *
  146. * @dev: the device structure
  147. * @interrupts_enabled: if interrupt should be enabled after reset.
  148. */
  149. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  150. {
  151. struct mei_cl *cl_pos = NULL;
  152. struct mei_cl *cl_next = NULL;
  153. struct mei_cl_cb *cb_pos = NULL;
  154. struct mei_cl_cb *cb_next = NULL;
  155. bool unexpected;
  156. if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET)
  157. return;
  158. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  159. dev->dev_state != MEI_DEV_DISABLED &&
  160. dev->dev_state != MEI_DEV_POWER_DOWN &&
  161. dev->dev_state != MEI_DEV_POWER_UP);
  162. mei_hw_reset(dev, interrupts_enabled);
  163. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  164. if (dev->dev_state != MEI_DEV_DISABLED &&
  165. dev->dev_state != MEI_DEV_POWER_DOWN)
  166. dev->dev_state = MEI_DEV_RESETING;
  167. list_for_each_entry_safe(cl_pos,
  168. cl_next, &dev->file_list, link) {
  169. cl_pos->state = MEI_FILE_DISCONNECTED;
  170. cl_pos->mei_flow_ctrl_creds = 0;
  171. cl_pos->read_cb = NULL;
  172. cl_pos->timer_count = 0;
  173. }
  174. /* remove entry if already in list */
  175. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  176. mei_cl_unlink(&dev->wd_cl);
  177. if (dev->open_handle_count > 0)
  178. dev->open_handle_count--;
  179. mei_cl_unlink(&dev->iamthif_cl);
  180. if (dev->open_handle_count > 0)
  181. dev->open_handle_count--;
  182. mei_amthif_reset_params(dev);
  183. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  184. }
  185. dev->me_clients_num = 0;
  186. dev->rd_msg_hdr = 0;
  187. dev->wd_pending = false;
  188. /* update the state of the registers after reset */
  189. dev->host_hw_state = mei_hcsr_read(dev);
  190. dev->me_hw_state = mei_mecsr_read(dev);
  191. dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  192. dev->host_hw_state, dev->me_hw_state);
  193. if (unexpected)
  194. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  195. mei_dev_state_str(dev->dev_state));
  196. /* Wake up all readings so they can be interrupted */
  197. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  198. if (waitqueue_active(&cl_pos->rx_wait)) {
  199. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  200. wake_up_interruptible(&cl_pos->rx_wait);
  201. }
  202. }
  203. /* remove all waiting requests */
  204. list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
  205. list_del(&cb_pos->list);
  206. mei_io_cb_free(cb_pos);
  207. }
  208. }