init.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "interface.h"
  23. #include "client.h"
  24. const char *mei_dev_state_str(int state)
  25. {
  26. #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
  27. switch (state) {
  28. MEI_DEV_STATE(INITIALIZING);
  29. MEI_DEV_STATE(INIT_CLIENTS);
  30. MEI_DEV_STATE(ENABLED);
  31. MEI_DEV_STATE(RESETING);
  32. MEI_DEV_STATE(DISABLED);
  33. MEI_DEV_STATE(RECOVERING_FROM_RESET);
  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. /**
  42. * init_mei_device - allocates and initializes the mei device structure
  43. *
  44. * @pdev: The pci device structure
  45. *
  46. * returns The mei_device_device pointer on success, NULL on failure.
  47. */
  48. struct mei_device *mei_device_init(struct pci_dev *pdev)
  49. {
  50. struct mei_device *dev;
  51. dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
  52. if (!dev)
  53. return NULL;
  54. /* setup our list array */
  55. INIT_LIST_HEAD(&dev->file_list);
  56. INIT_LIST_HEAD(&dev->wd_cl.link);
  57. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  58. mutex_init(&dev->device_lock);
  59. init_waitqueue_head(&dev->wait_recvd_msg);
  60. init_waitqueue_head(&dev->wait_stop_wd);
  61. dev->dev_state = MEI_DEV_INITIALIZING;
  62. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  63. mei_io_list_init(&dev->read_list);
  64. mei_io_list_init(&dev->write_list);
  65. mei_io_list_init(&dev->write_waiting_list);
  66. mei_io_list_init(&dev->ctrl_wr_list);
  67. mei_io_list_init(&dev->ctrl_rd_list);
  68. mei_io_list_init(&dev->amthif_cmd_list);
  69. mei_io_list_init(&dev->amthif_rd_complete_list);
  70. dev->pdev = pdev;
  71. return dev;
  72. }
  73. /**
  74. * mei_hw_init - initializes host and fw to start work.
  75. *
  76. * @dev: the device structure
  77. *
  78. * returns 0 on success, <0 on failure.
  79. */
  80. int mei_hw_init(struct mei_device *dev)
  81. {
  82. int err = 0;
  83. int ret;
  84. mutex_lock(&dev->device_lock);
  85. dev->host_hw_state = mei_hcsr_read(dev);
  86. dev->me_hw_state = mei_mecsr_read(dev);
  87. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
  88. dev->host_hw_state, dev->me_hw_state);
  89. /* acknowledge interrupt and stop interupts */
  90. mei_clear_interrupts(dev);
  91. /* Doesn't change in runtime */
  92. dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
  93. dev->recvd_msg = false;
  94. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  95. mei_reset(dev, 1);
  96. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  97. dev->host_hw_state, dev->me_hw_state);
  98. /* wait for ME to turn on ME_RDY */
  99. if (!dev->recvd_msg) {
  100. mutex_unlock(&dev->device_lock);
  101. err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
  102. dev->recvd_msg,
  103. mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
  104. mutex_lock(&dev->device_lock);
  105. }
  106. if (err <= 0 && !dev->recvd_msg) {
  107. dev->dev_state = MEI_DEV_DISABLED;
  108. dev_dbg(&dev->pdev->dev,
  109. "wait_event_interruptible_timeout failed"
  110. "on wait for ME to turn on ME_RDY.\n");
  111. ret = -ENODEV;
  112. goto out;
  113. }
  114. if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
  115. ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
  116. dev->dev_state = MEI_DEV_DISABLED;
  117. dev_dbg(&dev->pdev->dev,
  118. "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  119. dev->host_hw_state, dev->me_hw_state);
  120. if (!(dev->host_hw_state & H_RDY))
  121. dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
  122. if (!(dev->me_hw_state & ME_RDY_HRA))
  123. dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
  124. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  125. ret = -ENODEV;
  126. goto out;
  127. }
  128. if (dev->version.major_version != HBM_MAJOR_VERSION ||
  129. dev->version.minor_version != HBM_MINOR_VERSION) {
  130. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  131. ret = -ENODEV;
  132. goto out;
  133. }
  134. dev->recvd_msg = false;
  135. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  136. dev->host_hw_state, dev->me_hw_state);
  137. dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
  138. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  139. dev_dbg(&dev->pdev->dev, "MEI start success.\n");
  140. ret = 0;
  141. out:
  142. mutex_unlock(&dev->device_lock);
  143. return ret;
  144. }
  145. /**
  146. * mei_hw_reset - resets fw via mei csr register.
  147. *
  148. * @dev: the device structure
  149. * @interrupts_enabled: if interrupt should be enabled after reset.
  150. */
  151. static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
  152. {
  153. dev->host_hw_state |= (H_RST | H_IG);
  154. if (interrupts_enabled)
  155. mei_enable_interrupts(dev);
  156. else
  157. mei_disable_interrupts(dev);
  158. }
  159. /**
  160. * mei_reset - resets host and fw.
  161. *
  162. * @dev: the device structure
  163. * @interrupts_enabled: if interrupt should be enabled after reset.
  164. */
  165. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  166. {
  167. struct mei_cl *cl_pos = NULL;
  168. struct mei_cl *cl_next = NULL;
  169. struct mei_cl_cb *cb_pos = NULL;
  170. struct mei_cl_cb *cb_next = NULL;
  171. bool unexpected;
  172. if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
  173. dev->need_reset = true;
  174. return;
  175. }
  176. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  177. dev->dev_state != MEI_DEV_DISABLED &&
  178. dev->dev_state != MEI_DEV_POWER_DOWN &&
  179. dev->dev_state != MEI_DEV_POWER_UP);
  180. dev->host_hw_state = mei_hcsr_read(dev);
  181. dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
  182. dev->host_hw_state);
  183. mei_hw_reset(dev, interrupts_enabled);
  184. dev->host_hw_state &= ~H_RST;
  185. dev->host_hw_state |= H_IG;
  186. mei_hcsr_set(dev);
  187. dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
  188. dev->host_hw_state);
  189. dev->need_reset = false;
  190. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  191. if (dev->dev_state != MEI_DEV_DISABLED &&
  192. dev->dev_state != MEI_DEV_POWER_DOWN)
  193. dev->dev_state = MEI_DEV_RESETING;
  194. list_for_each_entry_safe(cl_pos,
  195. cl_next, &dev->file_list, link) {
  196. cl_pos->state = MEI_FILE_DISCONNECTED;
  197. cl_pos->mei_flow_ctrl_creds = 0;
  198. cl_pos->read_cb = NULL;
  199. cl_pos->timer_count = 0;
  200. }
  201. /* remove entry if already in list */
  202. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  203. mei_cl_unlink(&dev->wd_cl);
  204. mei_cl_unlink(&dev->iamthif_cl);
  205. mei_amthif_reset_params(dev);
  206. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  207. }
  208. dev->me_clients_num = 0;
  209. dev->rd_msg_hdr = 0;
  210. dev->wd_pending = false;
  211. /* update the state of the registers after reset */
  212. dev->host_hw_state = mei_hcsr_read(dev);
  213. dev->me_hw_state = mei_mecsr_read(dev);
  214. dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  215. dev->host_hw_state, dev->me_hw_state);
  216. if (unexpected)
  217. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  218. mei_dev_state_str(dev->dev_state));
  219. /* Wake up all readings so they can be interrupted */
  220. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  221. if (waitqueue_active(&cl_pos->rx_wait)) {
  222. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  223. wake_up_interruptible(&cl_pos->rx_wait);
  224. }
  225. }
  226. /* remove all waiting requests */
  227. list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
  228. list_del(&cb_pos->list);
  229. mei_io_cb_free(cb_pos);
  230. }
  231. }
  232. /**
  233. * allocate_me_clients_storage - allocates storage for me clients
  234. *
  235. * @dev: the device structure
  236. *
  237. * returns none.
  238. */
  239. void mei_allocate_me_clients_storage(struct mei_device *dev)
  240. {
  241. struct mei_me_client *clients;
  242. int b;
  243. /* count how many ME clients we have */
  244. for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
  245. dev->me_clients_num++;
  246. if (dev->me_clients_num <= 0)
  247. return ;
  248. if (dev->me_clients != NULL) {
  249. kfree(dev->me_clients);
  250. dev->me_clients = NULL;
  251. }
  252. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
  253. dev->me_clients_num * sizeof(struct mei_me_client));
  254. /* allocate storage for ME clients representation */
  255. clients = kcalloc(dev->me_clients_num,
  256. sizeof(struct mei_me_client), GFP_KERNEL);
  257. if (!clients) {
  258. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
  259. dev->dev_state = MEI_DEV_RESETING;
  260. mei_reset(dev, 1);
  261. return ;
  262. }
  263. dev->me_clients = clients;
  264. return ;
  265. }