init.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 uuid_le mei_amthi_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d, 0xac,
  25. 0xa8, 0x46, 0xe0, 0xff, 0x65,
  26. 0x81, 0x4c);
  27. /**
  28. * mei_io_list_init - Sets up a queue list.
  29. *
  30. * @list: An instance io list structure
  31. * @dev: the device structure
  32. */
  33. void mei_io_list_init(struct mei_io_list *list)
  34. {
  35. /* initialize our queue list */
  36. INIT_LIST_HEAD(&list->mei_cb.cb_list);
  37. }
  38. /**
  39. * mei_io_list_flush - removes list entry belonging to cl.
  40. *
  41. * @list: An instance of our list structure
  42. * @cl: private data of the file object
  43. */
  44. void mei_io_list_flush(struct mei_io_list *list, struct mei_cl *cl)
  45. {
  46. struct mei_cl_cb *pos;
  47. struct mei_cl_cb *next;
  48. list_for_each_entry_safe(pos, next, &list->mei_cb.cb_list, cb_list) {
  49. if (pos->file_private) {
  50. struct mei_cl *cl_tmp;
  51. cl_tmp = (struct mei_cl *)pos->file_private;
  52. if (mei_cl_cmp_id(cl, cl_tmp))
  53. list_del(&pos->cb_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->amthi_cmd_list, cl);
  74. mei_io_list_flush(&cl->dev->amthi_read_complete_list, cl);
  75. return 0;
  76. }
  77. /**
  78. * mei_reset_iamthif_params - initializes mei device iamthif
  79. *
  80. * @dev: the device structure
  81. */
  82. static void mei_reset_iamthif_params(struct mei_device *dev)
  83. {
  84. /* reset iamthif parameters. */
  85. dev->iamthif_current_cb = NULL;
  86. dev->iamthif_msg_buf_size = 0;
  87. dev->iamthif_msg_buf_index = 0;
  88. dev->iamthif_canceled = false;
  89. dev->iamthif_ioctl = false;
  90. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  91. dev->iamthif_timer = 0;
  92. }
  93. /**
  94. * init_mei_device - allocates and initializes the mei device structure
  95. *
  96. * @pdev: The pci device structure
  97. *
  98. * returns The mei_device_device pointer on success, NULL on failure.
  99. */
  100. struct mei_device *mei_device_init(struct pci_dev *pdev)
  101. {
  102. struct mei_device *dev;
  103. dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
  104. if (!dev)
  105. return NULL;
  106. /* setup our list array */
  107. INIT_LIST_HEAD(&dev->file_list);
  108. INIT_LIST_HEAD(&dev->wd_cl.link);
  109. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  110. mutex_init(&dev->device_lock);
  111. init_waitqueue_head(&dev->wait_recvd_msg);
  112. init_waitqueue_head(&dev->wait_stop_wd);
  113. dev->mei_state = MEI_INITIALIZING;
  114. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  115. dev->wd_interface_reg = false;
  116. mei_io_list_init(&dev->read_list);
  117. mei_io_list_init(&dev->write_list);
  118. mei_io_list_init(&dev->write_waiting_list);
  119. mei_io_list_init(&dev->ctrl_wr_list);
  120. mei_io_list_init(&dev->ctrl_rd_list);
  121. mei_io_list_init(&dev->amthi_cmd_list);
  122. mei_io_list_init(&dev->amthi_read_complete_list);
  123. dev->pdev = pdev;
  124. return dev;
  125. }
  126. /**
  127. * mei_hw_init - initializes host and fw to start work.
  128. *
  129. * @dev: the device structure
  130. *
  131. * returns 0 on success, <0 on failure.
  132. */
  133. int mei_hw_init(struct mei_device *dev)
  134. {
  135. int err = 0;
  136. int ret;
  137. mutex_lock(&dev->device_lock);
  138. dev->host_hw_state = mei_hcsr_read(dev);
  139. dev->me_hw_state = mei_mecsr_read(dev);
  140. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
  141. dev->host_hw_state, dev->me_hw_state);
  142. /* acknowledge interrupt and stop interupts */
  143. if ((dev->host_hw_state & H_IS) == H_IS)
  144. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  145. /* Doesn't change in runtime */
  146. dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
  147. dev->recvd_msg = false;
  148. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  149. mei_reset(dev, 1);
  150. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  151. dev->host_hw_state, dev->me_hw_state);
  152. /* wait for ME to turn on ME_RDY */
  153. if (!dev->recvd_msg) {
  154. mutex_unlock(&dev->device_lock);
  155. err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
  156. dev->recvd_msg, MEI_INTEROP_TIMEOUT);
  157. mutex_lock(&dev->device_lock);
  158. }
  159. if (err <= 0 && !dev->recvd_msg) {
  160. dev->mei_state = MEI_DISABLED;
  161. dev_dbg(&dev->pdev->dev,
  162. "wait_event_interruptible_timeout failed"
  163. "on wait for ME to turn on ME_RDY.\n");
  164. ret = -ENODEV;
  165. goto out;
  166. }
  167. if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
  168. ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
  169. dev->mei_state = MEI_DISABLED;
  170. dev_dbg(&dev->pdev->dev,
  171. "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  172. dev->host_hw_state, dev->me_hw_state);
  173. if (!(dev->host_hw_state & H_RDY))
  174. dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
  175. if (!(dev->me_hw_state & ME_RDY_HRA))
  176. dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
  177. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  178. ret = -ENODEV;
  179. goto out;
  180. }
  181. if (dev->version.major_version != HBM_MAJOR_VERSION ||
  182. dev->version.minor_version != HBM_MINOR_VERSION) {
  183. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  184. ret = -ENODEV;
  185. goto out;
  186. }
  187. dev->recvd_msg = false;
  188. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  189. dev->host_hw_state, dev->me_hw_state);
  190. dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
  191. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  192. dev_dbg(&dev->pdev->dev, "MEI start success.\n");
  193. ret = 0;
  194. out:
  195. mutex_unlock(&dev->device_lock);
  196. return ret;
  197. }
  198. /**
  199. * mei_hw_reset - resets fw via mei csr register.
  200. *
  201. * @dev: the device structure
  202. * @interrupts_enabled: if interrupt should be enabled after reset.
  203. */
  204. static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
  205. {
  206. dev->host_hw_state |= (H_RST | H_IG);
  207. if (interrupts_enabled)
  208. mei_enable_interrupts(dev);
  209. else
  210. mei_disable_interrupts(dev);
  211. }
  212. /**
  213. * mei_reset - resets host and fw.
  214. *
  215. * @dev: the device structure
  216. * @interrupts_enabled: if interrupt should be enabled after reset.
  217. */
  218. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  219. {
  220. struct mei_cl *cl_pos = NULL;
  221. struct mei_cl *cl_next = NULL;
  222. struct mei_cl_cb *cb_pos = NULL;
  223. struct mei_cl_cb *cb_next = NULL;
  224. bool unexpected;
  225. if (dev->mei_state == MEI_RECOVERING_FROM_RESET) {
  226. dev->need_reset = true;
  227. return;
  228. }
  229. unexpected = (dev->mei_state != MEI_INITIALIZING &&
  230. dev->mei_state != MEI_DISABLED &&
  231. dev->mei_state != MEI_POWER_DOWN &&
  232. dev->mei_state != MEI_POWER_UP);
  233. dev->host_hw_state = mei_hcsr_read(dev);
  234. dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
  235. dev->host_hw_state);
  236. mei_hw_reset(dev, interrupts_enabled);
  237. dev->host_hw_state &= ~H_RST;
  238. dev->host_hw_state |= H_IG;
  239. mei_hcsr_set(dev);
  240. dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
  241. dev->host_hw_state);
  242. dev->need_reset = false;
  243. if (dev->mei_state != MEI_INITIALIZING) {
  244. if (dev->mei_state != MEI_DISABLED &&
  245. dev->mei_state != MEI_POWER_DOWN)
  246. dev->mei_state = MEI_RESETING;
  247. list_for_each_entry_safe(cl_pos,
  248. cl_next, &dev->file_list, link) {
  249. cl_pos->state = MEI_FILE_DISCONNECTED;
  250. cl_pos->mei_flow_ctrl_creds = 0;
  251. cl_pos->read_cb = NULL;
  252. cl_pos->timer_count = 0;
  253. }
  254. /* remove entry if already in list */
  255. dev_dbg(&dev->pdev->dev, "list del iamthif and wd file list.\n");
  256. mei_remove_client_from_file_list(dev,
  257. dev->wd_cl.host_client_id);
  258. mei_remove_client_from_file_list(dev,
  259. dev->iamthif_cl.host_client_id);
  260. mei_reset_iamthif_params(dev);
  261. dev->wd_due_counter = 0;
  262. dev->extra_write_index = 0;
  263. }
  264. dev->me_clients_num = 0;
  265. dev->rd_msg_hdr = 0;
  266. dev->stop = false;
  267. dev->wd_pending = false;
  268. /* update the state of the registers after reset */
  269. dev->host_hw_state = mei_hcsr_read(dev);
  270. dev->me_hw_state = mei_mecsr_read(dev);
  271. dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  272. dev->host_hw_state, dev->me_hw_state);
  273. if (unexpected)
  274. dev_warn(&dev->pdev->dev, "unexpected reset.\n");
  275. /* Wake up all readings so they can be interrupted */
  276. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  277. if (waitqueue_active(&cl_pos->rx_wait)) {
  278. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  279. wake_up_interruptible(&cl_pos->rx_wait);
  280. }
  281. }
  282. /* remove all waiting requests */
  283. list_for_each_entry_safe(cb_pos, cb_next,
  284. &dev->write_list.mei_cb.cb_list, cb_list) {
  285. list_del(&cb_pos->cb_list);
  286. mei_free_cb_private(cb_pos);
  287. }
  288. }
  289. /**
  290. * host_start_message - mei host sends start message.
  291. *
  292. * @dev: the device structure
  293. *
  294. * returns none.
  295. */
  296. void mei_host_start_message(struct mei_device *dev)
  297. {
  298. struct mei_msg_hdr *mei_hdr;
  299. struct hbm_host_version_request *host_start_req;
  300. /* host start message */
  301. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  302. mei_hdr->host_addr = 0;
  303. mei_hdr->me_addr = 0;
  304. mei_hdr->length = sizeof(struct hbm_host_version_request);
  305. mei_hdr->msg_complete = 1;
  306. mei_hdr->reserved = 0;
  307. host_start_req =
  308. (struct hbm_host_version_request *) &dev->wr_msg_buf[1];
  309. memset(host_start_req, 0, sizeof(struct hbm_host_version_request));
  310. host_start_req->hbm_cmd = HOST_START_REQ_CMD;
  311. host_start_req->host_version.major_version = HBM_MAJOR_VERSION;
  312. host_start_req->host_version.minor_version = HBM_MINOR_VERSION;
  313. dev->recvd_msg = false;
  314. if (mei_write_message(dev, mei_hdr, (unsigned char *)host_start_req,
  315. mei_hdr->length)) {
  316. dev_dbg(&dev->pdev->dev, "write send version message to FW fail.\n");
  317. dev->mei_state = MEI_RESETING;
  318. mei_reset(dev, 1);
  319. }
  320. dev->init_clients_state = MEI_START_MESSAGE;
  321. dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
  322. return ;
  323. }
  324. /**
  325. * host_enum_clients_message - host sends enumeration client request message.
  326. *
  327. * @dev: the device structure
  328. *
  329. * returns none.
  330. */
  331. void mei_host_enum_clients_message(struct mei_device *dev)
  332. {
  333. struct mei_msg_hdr *mei_hdr;
  334. struct hbm_host_enum_request *host_enum_req;
  335. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  336. /* enumerate clients */
  337. mei_hdr->host_addr = 0;
  338. mei_hdr->me_addr = 0;
  339. mei_hdr->length = sizeof(struct hbm_host_enum_request);
  340. mei_hdr->msg_complete = 1;
  341. mei_hdr->reserved = 0;
  342. host_enum_req = (struct hbm_host_enum_request *) &dev->wr_msg_buf[1];
  343. memset(host_enum_req, 0, sizeof(struct hbm_host_enum_request));
  344. host_enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
  345. if (mei_write_message(dev, mei_hdr, (unsigned char *)host_enum_req,
  346. mei_hdr->length)) {
  347. dev->mei_state = MEI_RESETING;
  348. dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
  349. mei_reset(dev, 1);
  350. }
  351. dev->init_clients_state = MEI_ENUM_CLIENTS_MESSAGE;
  352. dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
  353. return;
  354. }
  355. /**
  356. * allocate_me_clients_storage - allocates storage for me clients
  357. *
  358. * @dev: the device structure
  359. *
  360. * returns none.
  361. */
  362. void mei_allocate_me_clients_storage(struct mei_device *dev)
  363. {
  364. struct mei_me_client *clients;
  365. int b;
  366. /* count how many ME clients we have */
  367. for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
  368. dev->me_clients_num++;
  369. if (dev->me_clients_num <= 0)
  370. return ;
  371. if (dev->me_clients != NULL) {
  372. kfree(dev->me_clients);
  373. dev->me_clients = NULL;
  374. }
  375. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
  376. dev->me_clients_num * sizeof(struct mei_me_client));
  377. /* allocate storage for ME clients representation */
  378. clients = kcalloc(dev->me_clients_num,
  379. sizeof(struct mei_me_client), GFP_KERNEL);
  380. if (!clients) {
  381. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
  382. dev->mei_state = MEI_RESETING;
  383. mei_reset(dev, 1);
  384. return ;
  385. }
  386. dev->me_clients = clients;
  387. return ;
  388. }
  389. /**
  390. * host_client_properties - reads properties for client
  391. *
  392. * @dev: the device structure
  393. *
  394. * returns:
  395. * < 0 - Error.
  396. * = 0 - no more clients.
  397. * = 1 - still have clients to send properties request.
  398. */
  399. int mei_host_client_properties(struct mei_device *dev)
  400. {
  401. struct mei_msg_hdr *mei_header;
  402. struct hbm_props_request *host_cli_req;
  403. int b;
  404. u8 client_num = dev->me_client_presentation_num;
  405. b = dev->me_client_index;
  406. b = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, b);
  407. if (b < MEI_CLIENTS_MAX) {
  408. dev->me_clients[client_num].client_id = b;
  409. dev->me_clients[client_num].mei_flow_ctrl_creds = 0;
  410. mei_header = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
  411. mei_header->host_addr = 0;
  412. mei_header->me_addr = 0;
  413. mei_header->length = sizeof(struct hbm_props_request);
  414. mei_header->msg_complete = 1;
  415. mei_header->reserved = 0;
  416. host_cli_req = (struct hbm_props_request *)&dev->wr_msg_buf[1];
  417. memset(host_cli_req, 0, sizeof(struct hbm_props_request));
  418. host_cli_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
  419. host_cli_req->address = b;
  420. if (mei_write_message(dev, mei_header,
  421. (unsigned char *)host_cli_req,
  422. mei_header->length)) {
  423. dev->mei_state = MEI_RESETING;
  424. dev_dbg(&dev->pdev->dev, "write send enumeration request message to FW fail.\n");
  425. mei_reset(dev, 1);
  426. return -EIO;
  427. }
  428. dev->init_clients_timer = INIT_CLIENTS_TIMEOUT;
  429. dev->me_client_index = b;
  430. return 1;
  431. }
  432. return 0;
  433. }
  434. /**
  435. * mei_init_file_private - initializes private file structure.
  436. *
  437. * @priv: private file structure to be initialized
  438. * @file: the file structure
  439. */
  440. void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
  441. {
  442. memset(priv, 0, sizeof(struct mei_cl));
  443. init_waitqueue_head(&priv->wait);
  444. init_waitqueue_head(&priv->rx_wait);
  445. init_waitqueue_head(&priv->tx_wait);
  446. INIT_LIST_HEAD(&priv->link);
  447. priv->reading_state = MEI_IDLE;
  448. priv->writing_state = MEI_IDLE;
  449. priv->dev = dev;
  450. }
  451. int mei_find_me_client_index(const struct mei_device *dev, uuid_le cuuid)
  452. {
  453. int i, res = -1;
  454. for (i = 0; i < dev->me_clients_num; ++i)
  455. if (uuid_le_cmp(cuuid,
  456. dev->me_clients[i].props.protocol_name) == 0) {
  457. res = i;
  458. break;
  459. }
  460. return res;
  461. }
  462. /**
  463. * mei_find_me_client_update_filext - searches for ME client guid
  464. * sets client_id in mei_file_private if found
  465. * @dev: the device structure
  466. * @priv: private file structure to set client_id in
  467. * @cguid: searched guid of ME client
  468. * @client_id: id of host client to be set in file private structure
  469. *
  470. * returns ME client index
  471. */
  472. u8 mei_find_me_client_update_filext(struct mei_device *dev, struct mei_cl *priv,
  473. const uuid_le *cguid, u8 client_id)
  474. {
  475. int i;
  476. if (!dev || !priv || !cguid)
  477. return 0;
  478. /* check for valid client id */
  479. i = mei_find_me_client_index(dev, *cguid);
  480. if (i >= 0) {
  481. priv->me_client_id = dev->me_clients[i].client_id;
  482. priv->state = MEI_FILE_CONNECTING;
  483. priv->host_client_id = client_id;
  484. list_add_tail(&priv->link, &dev->file_list);
  485. return (u8)i;
  486. }
  487. return 0;
  488. }
  489. /**
  490. * host_init_iamthif - mei initialization iamthif client.
  491. *
  492. * @dev: the device structure
  493. *
  494. */
  495. void mei_host_init_iamthif(struct mei_device *dev)
  496. {
  497. u8 i;
  498. unsigned char *msg_buf;
  499. mei_cl_init(&dev->iamthif_cl, dev);
  500. dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
  501. /* find ME amthi client */
  502. i = mei_find_me_client_update_filext(dev, &dev->iamthif_cl,
  503. &mei_amthi_guid, MEI_IAMTHIF_HOST_CLIENT_ID);
  504. if (dev->iamthif_cl.state != MEI_FILE_CONNECTING) {
  505. dev_dbg(&dev->pdev->dev, "failed to find iamthif client.\n");
  506. return;
  507. }
  508. /* Assign iamthif_mtu to the value received from ME */
  509. dev->iamthif_mtu = dev->me_clients[i].props.max_msg_length;
  510. dev_dbg(&dev->pdev->dev, "IAMTHIF_MTU = %d\n",
  511. dev->me_clients[i].props.max_msg_length);
  512. kfree(dev->iamthif_msg_buf);
  513. dev->iamthif_msg_buf = NULL;
  514. /* allocate storage for ME message buffer */
  515. msg_buf = kcalloc(dev->iamthif_mtu,
  516. sizeof(unsigned char), GFP_KERNEL);
  517. if (!msg_buf) {
  518. dev_dbg(&dev->pdev->dev, "memory allocation for ME message buffer failed.\n");
  519. return;
  520. }
  521. dev->iamthif_msg_buf = msg_buf;
  522. if (mei_connect(dev, &dev->iamthif_cl)) {
  523. dev_dbg(&dev->pdev->dev, "Failed to connect to AMTHI client\n");
  524. dev->iamthif_cl.state = MEI_FILE_DISCONNECTED;
  525. dev->iamthif_cl.host_client_id = 0;
  526. } else {
  527. dev->iamthif_cl.timer_count = CONNECT_TIMEOUT;
  528. }
  529. }
  530. /**
  531. * mei_alloc_file_private - allocates a private file structure and sets it up.
  532. * @file: the file structure
  533. *
  534. * returns The allocated file or NULL on failure
  535. */
  536. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  537. {
  538. struct mei_cl *cl;
  539. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  540. if (!cl)
  541. return NULL;
  542. mei_cl_init(cl, dev);
  543. return cl;
  544. }
  545. /**
  546. * mei_disconnect_host_client - sends disconnect message to fw from host client.
  547. *
  548. * @dev: the device structure
  549. * @cl: private data of the file object
  550. *
  551. * Locking: called under "dev->device_lock" lock
  552. *
  553. * returns 0 on success, <0 on failure.
  554. */
  555. int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
  556. {
  557. int rets, err;
  558. long timeout = 15; /* 15 seconds */
  559. struct mei_cl_cb *cb;
  560. if (!dev || !cl)
  561. return -ENODEV;
  562. if (cl->state != MEI_FILE_DISCONNECTING)
  563. return 0;
  564. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  565. if (!cb)
  566. return -ENOMEM;
  567. INIT_LIST_HEAD(&cb->cb_list);
  568. cb->file_private = cl;
  569. cb->major_file_operations = MEI_CLOSE;
  570. if (dev->mei_host_buffer_is_empty) {
  571. dev->mei_host_buffer_is_empty = false;
  572. if (mei_disconnect(dev, cl)) {
  573. rets = -ENODEV;
  574. dev_dbg(&dev->pdev->dev, "failed to call mei_disconnect.\n");
  575. goto free;
  576. }
  577. mdelay(10); /* Wait for hardware disconnection ready */
  578. list_add_tail(&cb->cb_list, &dev->ctrl_rd_list.mei_cb.cb_list);
  579. } else {
  580. dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
  581. list_add_tail(&cb->cb_list,
  582. &dev->ctrl_wr_list.mei_cb.cb_list);
  583. }
  584. mutex_unlock(&dev->device_lock);
  585. err = wait_event_timeout(dev->wait_recvd_msg,
  586. (MEI_FILE_DISCONNECTED == cl->state),
  587. timeout * HZ);
  588. mutex_lock(&dev->device_lock);
  589. if (MEI_FILE_DISCONNECTED == cl->state) {
  590. rets = 0;
  591. dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
  592. } else {
  593. rets = -ENODEV;
  594. if (MEI_FILE_DISCONNECTED != cl->state)
  595. dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
  596. if (err)
  597. dev_dbg(&dev->pdev->dev,
  598. "wait failed disconnect err=%08x\n",
  599. err);
  600. dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
  601. }
  602. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  603. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  604. free:
  605. mei_free_cb_private(cb);
  606. return rets;
  607. }
  608. /**
  609. * mei_remove_client_from_file_list -
  610. * removes file private data from device file list
  611. *
  612. * @dev: the device structure
  613. * @host_client_id: host client id to be removed
  614. */
  615. void mei_remove_client_from_file_list(struct mei_device *dev,
  616. u8 host_client_id)
  617. {
  618. struct mei_cl *cl_pos = NULL;
  619. struct mei_cl *cl_next = NULL;
  620. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  621. if (host_client_id == cl_pos->host_client_id) {
  622. dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
  623. cl_pos->host_client_id,
  624. cl_pos->me_client_id);
  625. list_del_init(&cl_pos->link);
  626. break;
  627. }
  628. }
  629. }