init.c 5.4 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 "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(RESETING);
  32. MEI_DEV_STATE(DISABLED);
  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. void mei_device_init(struct mei_device *dev)
  41. {
  42. /* setup our list array */
  43. INIT_LIST_HEAD(&dev->file_list);
  44. mutex_init(&dev->device_lock);
  45. init_waitqueue_head(&dev->wait_hw_ready);
  46. init_waitqueue_head(&dev->wait_recvd_msg);
  47. init_waitqueue_head(&dev->wait_stop_wd);
  48. dev->dev_state = MEI_DEV_INITIALIZING;
  49. mei_io_list_init(&dev->read_list);
  50. mei_io_list_init(&dev->write_list);
  51. mei_io_list_init(&dev->write_waiting_list);
  52. mei_io_list_init(&dev->ctrl_wr_list);
  53. mei_io_list_init(&dev->ctrl_rd_list);
  54. INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
  55. INIT_WORK(&dev->init_work, mei_host_client_init);
  56. INIT_LIST_HEAD(&dev->wd_cl.link);
  57. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  58. mei_io_list_init(&dev->amthif_cmd_list);
  59. mei_io_list_init(&dev->amthif_rd_complete_list);
  60. }
  61. /**
  62. * mei_hw_init - initializes host and fw to start work.
  63. *
  64. * @dev: the device structure
  65. *
  66. * returns 0 on success, <0 on failure.
  67. */
  68. int mei_hw_init(struct mei_device *dev)
  69. {
  70. int ret = 0;
  71. mutex_lock(&dev->device_lock);
  72. /* acknowledge interrupt and stop interupts */
  73. mei_clear_interrupts(dev);
  74. mei_hw_config(dev);
  75. dev->recvd_msg = false;
  76. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  77. mei_reset(dev, 1);
  78. /* wait for ME to turn on ME_RDY */
  79. if (!dev->recvd_msg) {
  80. mutex_unlock(&dev->device_lock);
  81. ret = wait_event_interruptible_timeout(dev->wait_recvd_msg,
  82. dev->recvd_msg,
  83. mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
  84. mutex_lock(&dev->device_lock);
  85. }
  86. if (ret <= 0 && !dev->recvd_msg) {
  87. dev->dev_state = MEI_DEV_DISABLED;
  88. dev_dbg(&dev->pdev->dev,
  89. "wait_event_interruptible_timeout failed"
  90. "on wait for ME to turn on ME_RDY.\n");
  91. goto err;
  92. }
  93. if (!mei_host_is_ready(dev)) {
  94. dev_err(&dev->pdev->dev, "host is not ready.\n");
  95. goto err;
  96. }
  97. if (!mei_hw_is_ready(dev)) {
  98. dev_err(&dev->pdev->dev, "ME is not ready.\n");
  99. goto err;
  100. }
  101. if (dev->version.major_version != HBM_MAJOR_VERSION ||
  102. dev->version.minor_version != HBM_MINOR_VERSION) {
  103. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  104. goto err;
  105. }
  106. dev->recvd_msg = false;
  107. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  108. mutex_unlock(&dev->device_lock);
  109. return 0;
  110. err:
  111. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  112. dev->dev_state = MEI_DEV_DISABLED;
  113. mutex_unlock(&dev->device_lock);
  114. return -ENODEV;
  115. }
  116. /**
  117. * mei_reset - resets host and fw.
  118. *
  119. * @dev: the device structure
  120. * @interrupts_enabled: if interrupt should be enabled after reset.
  121. */
  122. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  123. {
  124. bool unexpected;
  125. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  126. dev->dev_state != MEI_DEV_DISABLED &&
  127. dev->dev_state != MEI_DEV_POWER_DOWN &&
  128. dev->dev_state != MEI_DEV_POWER_UP);
  129. mei_hw_reset(dev, interrupts_enabled);
  130. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  131. if (dev->dev_state != MEI_DEV_DISABLED &&
  132. dev->dev_state != MEI_DEV_POWER_DOWN)
  133. dev->dev_state = MEI_DEV_RESETING;
  134. mei_cl_all_disconnect(dev);
  135. /* remove entry if already in list */
  136. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  137. mei_cl_unlink(&dev->wd_cl);
  138. if (dev->open_handle_count > 0)
  139. dev->open_handle_count--;
  140. mei_cl_unlink(&dev->iamthif_cl);
  141. if (dev->open_handle_count > 0)
  142. dev->open_handle_count--;
  143. mei_amthif_reset_params(dev);
  144. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  145. }
  146. dev->me_clients_num = 0;
  147. dev->rd_msg_hdr = 0;
  148. dev->wd_pending = false;
  149. if (unexpected)
  150. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  151. mei_dev_state_str(dev->dev_state));
  152. if (!interrupts_enabled) {
  153. dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
  154. return;
  155. }
  156. mei_hw_start(dev);
  157. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  158. /* link is established * start sending messages. */
  159. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  160. mei_hbm_start_req(dev);
  161. /* wake up all readings so they can be interrupted */
  162. mei_cl_all_read_wakeup(dev);
  163. /* remove all waiting requests */
  164. mei_cl_all_write_clear(dev);
  165. }
  166. void mei_stop(struct mei_device *dev)
  167. {
  168. dev_dbg(&dev->pdev->dev, "stopping the device.\n");
  169. mutex_lock(&dev->device_lock);
  170. cancel_delayed_work(&dev->timer_work);
  171. mei_wd_stop(dev);
  172. dev->dev_state = MEI_DEV_POWER_DOWN;
  173. mei_reset(dev, 0);
  174. mutex_unlock(&dev->device_lock);
  175. flush_scheduled_work();
  176. mei_watchdog_unregister(dev);
  177. }