init.c 19 KB

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