init.c 19 KB

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