init.c 19 KB

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