init.c 4.9 KB

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