init.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/pci.h>
  18. #include <linux/sched.h>
  19. #include <linux/wait.h>
  20. #include <linux/delay.h>
  21. #include <linux/mei.h>
  22. #include "mei_dev.h"
  23. #include "hbm.h"
  24. #include "client.h"
  25. const char *mei_dev_state_str(int state)
  26. {
  27. #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
  28. switch (state) {
  29. MEI_DEV_STATE(INITIALIZING);
  30. MEI_DEV_STATE(INIT_CLIENTS);
  31. MEI_DEV_STATE(ENABLED);
  32. MEI_DEV_STATE(RESETTING);
  33. MEI_DEV_STATE(DISABLED);
  34. MEI_DEV_STATE(POWER_DOWN);
  35. MEI_DEV_STATE(POWER_UP);
  36. default:
  37. return "unknown";
  38. }
  39. #undef MEI_DEV_STATE
  40. }
  41. void mei_device_init(struct mei_device *dev)
  42. {
  43. /* setup our list array */
  44. INIT_LIST_HEAD(&dev->file_list);
  45. INIT_LIST_HEAD(&dev->device_list);
  46. mutex_init(&dev->device_lock);
  47. init_waitqueue_head(&dev->wait_hw_ready);
  48. init_waitqueue_head(&dev->wait_recvd_msg);
  49. init_waitqueue_head(&dev->wait_stop_wd);
  50. dev->dev_state = MEI_DEV_INITIALIZING;
  51. mei_io_list_init(&dev->read_list);
  52. mei_io_list_init(&dev->write_list);
  53. mei_io_list_init(&dev->write_waiting_list);
  54. mei_io_list_init(&dev->ctrl_wr_list);
  55. mei_io_list_init(&dev->ctrl_rd_list);
  56. INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
  57. INIT_WORK(&dev->init_work, mei_host_client_init);
  58. INIT_LIST_HEAD(&dev->wd_cl.link);
  59. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  60. mei_io_list_init(&dev->amthif_cmd_list);
  61. mei_io_list_init(&dev->amthif_rd_complete_list);
  62. }
  63. EXPORT_SYMBOL_GPL(mei_device_init);
  64. /**
  65. * mei_start - initializes host and fw to start work.
  66. *
  67. * @dev: the device structure
  68. *
  69. * returns 0 on success, <0 on failure.
  70. */
  71. int mei_start(struct mei_device *dev)
  72. {
  73. mutex_lock(&dev->device_lock);
  74. /* acknowledge interrupt and stop interupts */
  75. mei_clear_interrupts(dev);
  76. mei_hw_config(dev);
  77. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  78. mei_reset(dev, 1);
  79. if (mei_hbm_start_wait(dev)) {
  80. dev_err(&dev->pdev->dev, "HBM haven't started");
  81. goto err;
  82. }
  83. if (!mei_host_is_ready(dev)) {
  84. dev_err(&dev->pdev->dev, "host is not ready.\n");
  85. goto err;
  86. }
  87. if (!mei_hw_is_ready(dev)) {
  88. dev_err(&dev->pdev->dev, "ME is not ready.\n");
  89. goto err;
  90. }
  91. if (!mei_hbm_version_is_supported(dev)) {
  92. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  93. goto err;
  94. }
  95. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  96. mutex_unlock(&dev->device_lock);
  97. return 0;
  98. err:
  99. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  100. dev->dev_state = MEI_DEV_DISABLED;
  101. mutex_unlock(&dev->device_lock);
  102. return -ENODEV;
  103. }
  104. EXPORT_SYMBOL_GPL(mei_start);
  105. /**
  106. * mei_reset - resets host and fw.
  107. *
  108. * @dev: the device structure
  109. * @interrupts_enabled: if interrupt should be enabled after reset.
  110. */
  111. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  112. {
  113. bool unexpected;
  114. int ret;
  115. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  116. dev->dev_state != MEI_DEV_DISABLED &&
  117. dev->dev_state != MEI_DEV_POWER_DOWN &&
  118. dev->dev_state != MEI_DEV_POWER_UP);
  119. ret = mei_hw_reset(dev, interrupts_enabled);
  120. if (ret) {
  121. dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n");
  122. interrupts_enabled = false;
  123. dev->dev_state = MEI_DEV_DISABLED;
  124. }
  125. dev->hbm_state = MEI_HBM_IDLE;
  126. if (dev->dev_state != MEI_DEV_INITIALIZING &&
  127. dev->dev_state != MEI_DEV_POWER_UP) {
  128. if (dev->dev_state != MEI_DEV_DISABLED &&
  129. dev->dev_state != MEI_DEV_POWER_DOWN)
  130. dev->dev_state = MEI_DEV_RESETTING;
  131. /* remove all waiting requests */
  132. mei_cl_all_write_clear(dev);
  133. mei_cl_all_disconnect(dev);
  134. /* wake up all readings so they can be interrupted */
  135. mei_cl_all_wakeup(dev);
  136. /* remove entry if already in list */
  137. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  138. mei_cl_unlink(&dev->wd_cl);
  139. if (dev->open_handle_count > 0)
  140. dev->open_handle_count--;
  141. mei_cl_unlink(&dev->iamthif_cl);
  142. if (dev->open_handle_count > 0)
  143. dev->open_handle_count--;
  144. mei_amthif_reset_params(dev);
  145. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  146. }
  147. dev->me_clients_num = 0;
  148. dev->rd_msg_hdr = 0;
  149. dev->wd_pending = false;
  150. if (unexpected)
  151. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  152. mei_dev_state_str(dev->dev_state));
  153. if (!interrupts_enabled) {
  154. dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
  155. return;
  156. }
  157. ret = mei_hw_start(dev);
  158. if (ret) {
  159. dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n");
  160. dev->dev_state = MEI_DEV_DISABLED;
  161. return;
  162. }
  163. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  164. /* link is established * start sending messages. */
  165. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  166. mei_hbm_start_req(dev);
  167. }
  168. EXPORT_SYMBOL_GPL(mei_reset);
  169. void mei_stop(struct mei_device *dev)
  170. {
  171. dev_dbg(&dev->pdev->dev, "stopping the device.\n");
  172. flush_scheduled_work();
  173. mutex_lock(&dev->device_lock);
  174. cancel_delayed_work(&dev->timer_work);
  175. mei_wd_stop(dev);
  176. mei_nfc_host_exit();
  177. dev->dev_state = MEI_DEV_POWER_DOWN;
  178. mei_reset(dev, 0);
  179. mutex_unlock(&dev->device_lock);
  180. mei_watchdog_unregister(dev);
  181. }
  182. EXPORT_SYMBOL_GPL(mei_stop);