init.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 (!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. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  115. dev->dev_state != MEI_DEV_DISABLED &&
  116. dev->dev_state != MEI_DEV_POWER_DOWN &&
  117. dev->dev_state != MEI_DEV_POWER_UP);
  118. mei_hw_reset(dev, interrupts_enabled);
  119. dev->hbm_state = MEI_HBM_IDLE;
  120. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  121. if (dev->dev_state != MEI_DEV_DISABLED &&
  122. dev->dev_state != MEI_DEV_POWER_DOWN)
  123. dev->dev_state = MEI_DEV_RESETTING;
  124. mei_cl_all_disconnect(dev);
  125. /* remove entry if already in list */
  126. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  127. mei_cl_unlink(&dev->wd_cl);
  128. if (dev->open_handle_count > 0)
  129. dev->open_handle_count--;
  130. mei_cl_unlink(&dev->iamthif_cl);
  131. if (dev->open_handle_count > 0)
  132. dev->open_handle_count--;
  133. mei_amthif_reset_params(dev);
  134. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  135. }
  136. dev->me_clients_num = 0;
  137. dev->rd_msg_hdr = 0;
  138. dev->wd_pending = false;
  139. if (unexpected)
  140. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  141. mei_dev_state_str(dev->dev_state));
  142. if (!interrupts_enabled) {
  143. dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
  144. return;
  145. }
  146. mei_hw_start(dev);
  147. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  148. /* link is established * start sending messages. */
  149. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  150. mei_hbm_start_req(dev);
  151. /* wake up all readings so they can be interrupted */
  152. mei_cl_all_read_wakeup(dev);
  153. /* remove all waiting requests */
  154. mei_cl_all_write_clear(dev);
  155. }
  156. EXPORT_SYMBOL_GPL(mei_reset);
  157. void mei_stop(struct mei_device *dev)
  158. {
  159. dev_dbg(&dev->pdev->dev, "stopping the device.\n");
  160. flush_scheduled_work();
  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. mei_watchdog_unregister(dev);
  169. }
  170. EXPORT_SYMBOL_GPL(mei_stop);