init.c 18 KB

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