init.c 19 KB

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