init.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. struct mei_cl *cl_pos = NULL;
  118. struct mei_cl *cl_next = NULL;
  119. struct mei_cl_cb *cb_pos = NULL;
  120. struct mei_cl_cb *cb_next = NULL;
  121. bool unexpected;
  122. if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET)
  123. return;
  124. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  125. dev->dev_state != MEI_DEV_DISABLED &&
  126. dev->dev_state != MEI_DEV_POWER_DOWN &&
  127. dev->dev_state != MEI_DEV_POWER_UP);
  128. mei_hw_reset(dev, interrupts_enabled);
  129. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  130. if (dev->dev_state != MEI_DEV_DISABLED &&
  131. dev->dev_state != MEI_DEV_POWER_DOWN)
  132. dev->dev_state = MEI_DEV_RESETING;
  133. list_for_each_entry_safe(cl_pos,
  134. cl_next, &dev->file_list, link) {
  135. cl_pos->state = MEI_FILE_DISCONNECTED;
  136. cl_pos->mei_flow_ctrl_creds = 0;
  137. cl_pos->read_cb = NULL;
  138. cl_pos->timer_count = 0;
  139. }
  140. /* remove entry if already in list */
  141. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  142. mei_cl_unlink(&dev->wd_cl);
  143. if (dev->open_handle_count > 0)
  144. dev->open_handle_count--;
  145. mei_cl_unlink(&dev->iamthif_cl);
  146. if (dev->open_handle_count > 0)
  147. dev->open_handle_count--;
  148. mei_amthif_reset_params(dev);
  149. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  150. }
  151. dev->me_clients_num = 0;
  152. dev->rd_msg_hdr = 0;
  153. dev->wd_pending = false;
  154. if (unexpected)
  155. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  156. mei_dev_state_str(dev->dev_state));
  157. /* Wake up all readings so they can be interrupted */
  158. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  159. if (waitqueue_active(&cl_pos->rx_wait)) {
  160. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  161. wake_up_interruptible(&cl_pos->rx_wait);
  162. }
  163. }
  164. /* remove all waiting requests */
  165. list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
  166. list_del(&cb_pos->list);
  167. mei_io_cb_free(cb_pos);
  168. }
  169. }