init.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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 "mei_dev.h"
  21. #include "hw.h"
  22. #include "interface.h"
  23. #include <linux/mei.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. * mei_io_list_flush - removes list entry belonging to cl.
  43. *
  44. * @list: An instance of our list structure
  45. * @cl: private data of the file object
  46. */
  47. void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
  48. {
  49. struct mei_cl_cb *pos;
  50. struct mei_cl_cb *next;
  51. list_for_each_entry_safe(pos, next, &list->list, list) {
  52. if (pos->cl) {
  53. if (mei_cl_cmp_id(cl, pos->cl))
  54. list_del(&pos->list);
  55. }
  56. }
  57. }
  58. /**
  59. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  60. *
  61. * @dev: the device structure
  62. * @cl: private data of the file object
  63. */
  64. int mei_cl_flush_queues(struct mei_cl *cl)
  65. {
  66. if (!cl || !cl->dev)
  67. return -EINVAL;
  68. dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
  69. mei_io_list_flush(&cl->dev->read_list, cl);
  70. mei_io_list_flush(&cl->dev->write_list, cl);
  71. mei_io_list_flush(&cl->dev->write_waiting_list, cl);
  72. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  73. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  74. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  75. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  76. return 0;
  77. }
  78. /**
  79. * init_mei_device - allocates and initializes the mei device structure
  80. *
  81. * @pdev: The pci device structure
  82. *
  83. * returns The mei_device_device pointer on success, NULL on failure.
  84. */
  85. struct mei_device *mei_device_init(struct pci_dev *pdev)
  86. {
  87. struct mei_device *dev;
  88. dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
  89. if (!dev)
  90. return NULL;
  91. /* setup our list array */
  92. INIT_LIST_HEAD(&dev->file_list);
  93. INIT_LIST_HEAD(&dev->wd_cl.link);
  94. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  95. mutex_init(&dev->device_lock);
  96. init_waitqueue_head(&dev->wait_recvd_msg);
  97. init_waitqueue_head(&dev->wait_stop_wd);
  98. dev->dev_state = MEI_DEV_INITIALIZING;
  99. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  100. mei_io_list_init(&dev->read_list);
  101. mei_io_list_init(&dev->write_list);
  102. mei_io_list_init(&dev->write_waiting_list);
  103. mei_io_list_init(&dev->ctrl_wr_list);
  104. mei_io_list_init(&dev->ctrl_rd_list);
  105. mei_io_list_init(&dev->amthif_cmd_list);
  106. mei_io_list_init(&dev->amthif_rd_complete_list);
  107. dev->pdev = pdev;
  108. return dev;
  109. }
  110. /**
  111. * mei_hw_init - initializes host and fw to start work.
  112. *
  113. * @dev: the device structure
  114. *
  115. * returns 0 on success, <0 on failure.
  116. */
  117. int mei_hw_init(struct mei_device *dev)
  118. {
  119. int err = 0;
  120. int ret;
  121. mutex_lock(&dev->device_lock);
  122. dev->host_hw_state = mei_hcsr_read(dev);
  123. dev->me_hw_state = mei_mecsr_read(dev);
  124. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
  125. dev->host_hw_state, dev->me_hw_state);
  126. /* acknowledge interrupt and stop interupts */
  127. if ((dev->host_hw_state & H_IS) == H_IS)
  128. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  129. /* Doesn't change in runtime */
  130. dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
  131. dev->recvd_msg = false;
  132. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  133. mei_reset(dev, 1);
  134. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  135. dev->host_hw_state, dev->me_hw_state);
  136. /* wait for ME to turn on ME_RDY */
  137. if (!dev->recvd_msg) {
  138. mutex_unlock(&dev->device_lock);
  139. err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
  140. dev->recvd_msg,
  141. mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
  142. mutex_lock(&dev->device_lock);
  143. }
  144. if (err <= 0 && !dev->recvd_msg) {
  145. dev->dev_state = MEI_DEV_DISABLED;
  146. dev_dbg(&dev->pdev->dev,
  147. "wait_event_interruptible_timeout failed"
  148. "on wait for ME to turn on ME_RDY.\n");
  149. ret = -ENODEV;
  150. goto out;
  151. }
  152. if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
  153. ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
  154. dev->dev_state = MEI_DEV_DISABLED;
  155. dev_dbg(&dev->pdev->dev,
  156. "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  157. dev->host_hw_state, dev->me_hw_state);
  158. if (!(dev->host_hw_state & H_RDY))
  159. dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
  160. if (!(dev->me_hw_state & ME_RDY_HRA))
  161. dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
  162. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  163. ret = -ENODEV;
  164. goto out;
  165. }
  166. if (dev->version.major_version != HBM_MAJOR_VERSION ||
  167. dev->version.minor_version != HBM_MINOR_VERSION) {
  168. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  169. ret = -ENODEV;
  170. goto out;
  171. }
  172. dev->recvd_msg = false;
  173. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  174. dev->host_hw_state, dev->me_hw_state);
  175. dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
  176. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  177. dev_dbg(&dev->pdev->dev, "MEI start success.\n");
  178. ret = 0;
  179. out:
  180. mutex_unlock(&dev->device_lock);
  181. return ret;
  182. }
  183. /**
  184. * mei_hw_reset - resets fw via mei csr register.
  185. *
  186. * @dev: the device structure
  187. * @interrupts_enabled: if interrupt should be enabled after reset.
  188. */
  189. static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
  190. {
  191. dev->host_hw_state |= (H_RST | H_IG);
  192. if (interrupts_enabled)
  193. mei_enable_interrupts(dev);
  194. else
  195. mei_disable_interrupts(dev);
  196. }
  197. /**
  198. * mei_reset - resets host and fw.
  199. *
  200. * @dev: the device structure
  201. * @interrupts_enabled: if interrupt should be enabled after reset.
  202. */
  203. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  204. {
  205. struct mei_cl *cl_pos = NULL;
  206. struct mei_cl *cl_next = NULL;
  207. struct mei_cl_cb *cb_pos = NULL;
  208. struct mei_cl_cb *cb_next = NULL;
  209. bool unexpected;
  210. if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
  211. dev->need_reset = true;
  212. return;
  213. }
  214. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  215. dev->dev_state != MEI_DEV_DISABLED &&
  216. dev->dev_state != MEI_DEV_POWER_DOWN &&
  217. dev->dev_state != MEI_DEV_POWER_UP);
  218. dev->host_hw_state = mei_hcsr_read(dev);
  219. dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
  220. dev->host_hw_state);
  221. mei_hw_reset(dev, interrupts_enabled);
  222. dev->host_hw_state &= ~H_RST;
  223. dev->host_hw_state |= H_IG;
  224. mei_hcsr_set(dev);
  225. dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
  226. dev->host_hw_state);
  227. dev->need_reset = false;
  228. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  229. if (dev->dev_state != MEI_DEV_DISABLED &&
  230. dev->dev_state != MEI_DEV_POWER_DOWN)
  231. dev->dev_state = MEI_DEV_RESETING;
  232. list_for_each_entry_safe(cl_pos,
  233. cl_next, &dev->file_list, link) {
  234. cl_pos->state = MEI_FILE_DISCONNECTED;
  235. cl_pos->mei_flow_ctrl_creds = 0;
  236. cl_pos->read_cb = NULL;
  237. cl_pos->timer_count = 0;
  238. }
  239. /* remove entry if already in list */
  240. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  241. mei_me_cl_unlink(dev, &dev->wd_cl);
  242. mei_me_cl_unlink(dev, &dev->iamthif_cl);
  243. mei_amthif_reset_params(dev);
  244. dev->extra_write_index = 0;
  245. }
  246. dev->me_clients_num = 0;
  247. dev->rd_msg_hdr = 0;
  248. dev->wd_pending = false;
  249. /* update the state of the registers after reset */
  250. dev->host_hw_state = mei_hcsr_read(dev);
  251. dev->me_hw_state = mei_mecsr_read(dev);
  252. dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  253. dev->host_hw_state, dev->me_hw_state);
  254. if (unexpected)
  255. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  256. mei_dev_state_str(dev->dev_state));
  257. /* Wake up all readings so they can be interrupted */
  258. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  259. if (waitqueue_active(&cl_pos->rx_wait)) {
  260. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  261. wake_up_interruptible(&cl_pos->rx_wait);
  262. }
  263. }
  264. /* remove all waiting requests */
  265. list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
  266. list_del(&cb_pos->list);
  267. mei_io_cb_free(cb_pos);
  268. }
  269. }
  270. /**
  271. * host_start_message - mei host sends start message.
  272. *
  273. * @dev: the device structure
  274. *
  275. * returns none.
  276. */
  277. void mei_host_start_message(struct mei_device *dev)
  278. {
  279. struct mei_msg_hdr *mei_hdr;
  280. struct hbm_host_version_request *host_start_req;
  281. /* host start message */
  282. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  283. mei_hdr->host_addr = 0;
  284. mei_hdr->me_addr = 0;
  285. mei_hdr->length = sizeof(struct hbm_host_version_request);
  286. mei_hdr->msg_complete = 1;
  287. mei_hdr->reserved = 0;
  288. host_start_req =
  289. (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
  290. memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
  291. host_start_req->hbm_cmd = HOST_START_REQ_CMD;
  292. host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
  293. host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
  294. dev->recvd_msg = false;
  295. if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
  296. mei_hdr->length)) {
  297. dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
  298. dev->dev_state = MEI_DEV_RESETING;
  299. mei_reset(dev, 1);
  300. }
  301. dev->init_clients_state = MEI_START_MESSAGE;
  302. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  303. return ;
  304. }
  305. /**
  306. * host_enum_clients_message - host sends enumeration client request message.
  307. *
  308. * @dev: the device structure
  309. *
  310. * returns none.
  311. */
  312. void mei_host_enum_clients_message(struct mei_device *dev)
  313. {
  314. struct mei_msg_hdr *mei_hdr;
  315. struct hbm_host_enum_request *host_enum_req;
  316. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  317. /* enumerate clients */
  318. mei_hdr->host_addr = 0;
  319. mei_hdr->me_addr = 0;
  320. mei_hdr->length = sizeof(struct hbm_host_enum_request);
  321. mei_hdr->msg_complete = 1;
  322. mei_hdr->reserved = 0;
  323. host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
  324. memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
  325. host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
  326. if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
  327. mei_hdr->length)) {
  328. dev->dev_state = MEI_DEV_RESETING;
  329. dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
  330. mei_reset(dev, 1);
  331. }
  332. dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
  333. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  334. return;
  335. }
  336. /**
  337. * allocate_me_clients_storage - allocates storage for me clients
  338. *
  339. * @dev: the device structure
  340. *
  341. * returns none.
  342. */
  343. void mei_allocate_me_clients_storage(struct mei_device *dev)
  344. {
  345. struct mei_me_client *clients;
  346. int b;
  347. /* count how many ME clients we have */
  348. for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
  349. dev->me_clients_num++;
  350. if (dev->me_clients_num <= 0)
  351. return ;
  352. if (dev->me_clients != NULL) {
  353. kfree(dev->me_clients);
  354. dev->me_clients = NULL;
  355. }
  356. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
  357. dev->me_clients_num * sizeof(struct mei_me_client));
  358. /* allocate storage for ME clients representation */
  359. clients = kcalloc(dev->me_clients_num,
  360. sizeof(struct mei_me_client), GFP_KERNEL);
  361. if (!clients) {
  362. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
  363. dev->dev_state = MEI_DEV_RESETING;
  364. mei_reset(dev, 1);
  365. return ;
  366. }
  367. dev->me_clients = clients;
  368. return ;
  369. }
  370. /**
  371. * host_client_properties - reads properties for client
  372. *
  373. * @dev: the device structure
  374. *
  375. * returns:
  376. * < 0 - Error.
  377. * = 0 - no more clients.
  378. * = 1 - still have clients to send properties request.
  379. */
  380. int mei_host_client_properties(struct mei_device *dev)
  381. {
  382. struct mei_msg_hdr *mei_header;
  383. struct hbm_props_request *host_cli_req;
  384. int b;
  385. u8 client_num = dev->me_client_presentation_num;
  386. b = dev->me_client_index;
  387. b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
  388. if (b < MEI_CLIENTS_MAX) {
  389. dev->me_clients[client_num].client_id = b;
  390. dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
  391. mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
  392. mei_header->host_addr = 0;
  393. mei_header->me_addr = 0;
  394. mei_header->length = sizeof(struct hbm_props_request);
  395. mei_header->msg_complete = 1;
  396. mei_header->reserved = 0;
  397. host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
  398. memset(host_cli_req, 0, sizeof(struct hbm_props_request));
  399. host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
  400. host_cli_req->address = b;
  401. if (mei_write_message(dev, mei_header,
  402. (unsigned char *)host_cli_req,
  403. mei_header->length)) {
  404. dev->dev_state = MEI_DEV_RESETING;
  405. dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
  406. mei_reset(dev, 1);
  407. return -EIO;
  408. }
  409. dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
  410. dev->me_client_index = b;
  411. return 1;
  412. }
  413. return 0;
  414. }
  415. /**
  416. * mei_init_file_private - initializes private file structure.
  417. *
  418. * @priv: private file structure to be initialized
  419. * @file: the file structure
  420. */
  421. void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
  422. {
  423. memset(priv, 0, sizeof(struct mei_cl));
  424. init_waitqueue_head(&priv->wait);
  425. init_waitqueue_head(&priv->rx_wait);
  426. init_waitqueue_head(&priv->tx_wait);
  427. INIT_LIST_HEAD(&priv->link);
  428. priv->reading_state = MEI_IDLE;
  429. priv->writing_state = MEI_IDLE;
  430. priv->dev = dev;
  431. }
  432. int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
  433. {
  434. int i, res = -ENOENT;
  435. for (i = 0; i < dev->me_clients_num; ++i)
  436. if (uuid_le_cmp(*cuuid,
  437. dev->me_clients[i].props.protocol_name) == 0) {
  438. res = i;
  439. break;
  440. }
  441. return res;
  442. }
  443. /**
  444. * mei_me_cl_link - create link between host and me clinet and add
  445. * me_cl to the list
  446. *
  447. * @dev: the device structure
  448. * @cl: link between me and host client assocated with opened file descriptor
  449. * @cuuid: uuid of ME client
  450. * @client_id: id of the host client
  451. *
  452. * returns ME client index if ME client
  453. * -EINVAL on incorrect values
  454. * -ENONET if client not found
  455. */
  456. int mei_me_cl_link(struct mei_device *dev, struct mei_cl *cl,
  457. const uuid_le *cuuid, u8 host_cl_id)
  458. {
  459. int i;
  460. if (!dev || !cl || !cuuid)
  461. return -EINVAL;
  462. /* check for valid client id */
  463. i = mei_me_cl_by_uuid(dev, cuuid);
  464. if (i >= 0) {
  465. cl->me_client_id = dev->me_clients[i].client_id;
  466. cl->state = MEI_FILE_CONNECTING;
  467. cl->host_client_id = host_cl_id;
  468. list_add_tail(&cl->link, &dev->file_list);
  469. return (u8)i;
  470. }
  471. return -ENOENT;
  472. }
  473. /**
  474. * mei_me_cl_unlink - remove me_cl from the list
  475. *
  476. * @dev: the device structure
  477. * @host_client_id: host client id to be removed
  478. */
  479. void mei_me_cl_unlink(struct mei_device *dev, struct mei_cl *cl)
  480. {
  481. struct mei_cl *pos, *next;
  482. list_for_each_entry_safe(pos, next, &dev->file_list, link) {
  483. if (cl->host_client_id == pos->host_client_id) {
  484. dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
  485. pos->host_client_id, pos->me_client_id);
  486. list_del_init(&pos->link);
  487. break;
  488. }
  489. }
  490. }
  491. /**
  492. * mei_alloc_file_private - allocates a private file structure and sets it up.
  493. * @file: the file structure
  494. *
  495. * returns The allocated file or NULL on failure
  496. */
  497. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  498. {
  499. struct mei_cl *cl;
  500. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  501. if (!cl)
  502. return NULL;
  503. mei_cl_init(cl, dev);
  504. return cl;
  505. }
  506. /**
  507. * mei_disconnect_host_client - sends disconnect message to fw from host client.
  508. *
  509. * @dev: the device structure
  510. * @cl: private data of the file object
  511. *
  512. * Locking: called under "dev->device_lock" lock
  513. *
  514. * returns 0 on success, <0 on failure.
  515. */
  516. int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
  517. {
  518. struct mei_cl_cb *cb;
  519. int rets, err;
  520. if (!dev || !cl)
  521. return -ENODEV;
  522. if (cl->state != MEI_FILE_DISCONNECTING)
  523. return 0;
  524. cb = mei_io_cb_init(cl, NULL);
  525. if (!cb)
  526. return -ENOMEM;
  527. cb->fop_type = MEI_FOP_CLOSE;
  528. if (dev->mei_host_buffer_is_empty) {
  529. dev->mei_host_buffer_is_empty = false;
  530. if (mei_disconnect(dev, cl)) {
  531. rets = -ENODEV;
  532. dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
  533. goto free;
  534. }
  535. mdelay(10); /* Wait for hardware disconnection ready */
  536. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  537. } else {
  538. dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
  539. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  540. }
  541. mutex_unlock(&dev->device_lock);
  542. err = wait_event_timeout(dev->wait_recvd_msg,
  543. MEI_FILE_DISCONNECTED == cl->state,
  544. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  545. mutex_lock(&dev->device_lock);
  546. if (MEI_FILE_DISCONNECTED == cl->state) {
  547. rets = 0;
  548. dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
  549. } else {
  550. rets = -ENODEV;
  551. if (MEI_FILE_DISCONNECTED != cl->state)
  552. dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
  553. if (err)
  554. dev_dbg(&dev->pdev->dev,
  555. "wait failed disconnect err=%08x\n",
  556. err);
  557. dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
  558. }
  559. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  560. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  561. free:
  562. mei_io_cb_free(cb);
  563. return rets;
  564. }