init.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "unkown";
  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 (dev->version.major_version != HBM_MAJOR_VERSION ||
  92. dev->version.minor_version != HBM_MINOR_VERSION) {
  93. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  94. goto err;
  95. }
  96. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  97. mutex_unlock(&dev->device_lock);
  98. return 0;
  99. err:
  100. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  101. dev->dev_state = MEI_DEV_DISABLED;
  102. mutex_unlock(&dev->device_lock);
  103. return -ENODEV;
  104. }
  105. EXPORT_SYMBOL_GPL(mei_start);
  106. /**
  107. * mei_reset - resets host and fw.
  108. *
  109. * @dev: the device structure
  110. * @interrupts_enabled: if interrupt should be enabled after reset.
  111. */
  112. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  113. {
  114. bool unexpected;
  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. mei_hw_reset(dev, interrupts_enabled);
  120. dev->hbm_state = MEI_HBM_IDLE;
  121. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  122. if (dev->dev_state != MEI_DEV_DISABLED &&
  123. dev->dev_state != MEI_DEV_POWER_DOWN)
  124. dev->dev_state = MEI_DEV_RESETTING;
  125. mei_cl_all_disconnect(dev);
  126. /* remove entry if already in list */
  127. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  128. mei_cl_unlink(&dev->wd_cl);
  129. if (dev->open_handle_count > 0)
  130. dev->open_handle_count--;
  131. mei_cl_unlink(&dev->iamthif_cl);
  132. if (dev->open_handle_count > 0)
  133. dev->open_handle_count--;
  134. mei_amthif_reset_params(dev);
  135. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  136. }
  137. dev->me_clients_num = 0;
  138. dev->rd_msg_hdr = 0;
  139. dev->wd_pending = false;
  140. if (unexpected)
  141. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  142. mei_dev_state_str(dev->dev_state));
  143. if (!interrupts_enabled) {
  144. dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
  145. return;
  146. }
  147. mei_hw_start(dev);
  148. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  149. /* link is established * start sending messages. */
  150. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  151. mei_hbm_start_req(dev);
  152. /* wake up all readings so they can be interrupted */
  153. mei_cl_all_read_wakeup(dev);
  154. /* remove all waiting requests */
  155. mei_cl_all_write_clear(dev);
  156. }
  157. EXPORT_SYMBOL_GPL(mei_reset);
  158. void mei_stop(struct mei_device *dev)
  159. {
  160. dev_dbg(&dev->pdev->dev, "stopping the device.\n");
  161. mutex_lock(&dev->device_lock);
  162. cancel_delayed_work(&dev->timer_work);
  163. mei_wd_stop(dev);
  164. mei_nfc_host_exit();
  165. dev->dev_state = MEI_DEV_POWER_DOWN;
  166. mei_reset(dev, 0);
  167. mutex_unlock(&dev->device_lock);
  168. flush_scheduled_work();
  169. mei_watchdog_unregister(dev);
  170. }
  171. EXPORT_SYMBOL_GPL(mei_stop);