init.c 20 KB

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