init.c 5.7 KB

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