amthif.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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/kernel.h>
  17. #include <linux/fs.h>
  18. #include <linux/errno.h>
  19. #include <linux/types.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/ioctl.h>
  22. #include <linux/cdev.h>
  23. #include <linux/list.h>
  24. #include <linux/delay.h>
  25. #include <linux/sched.h>
  26. #include <linux/uuid.h>
  27. #include <linux/jiffies.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/slab.h>
  30. #include <linux/mei.h>
  31. #include "mei_dev.h"
  32. #include "hbm.h"
  33. #include "client.h"
  34. const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
  35. 0xac, 0xa8, 0x46, 0xe0,
  36. 0xff, 0x65, 0x81, 0x4c);
  37. /**
  38. * mei_amthif_reset_params - initializes mei device iamthif
  39. *
  40. * @dev: the device structure
  41. */
  42. void mei_amthif_reset_params(struct mei_device *dev)
  43. {
  44. /* reset iamthif parameters. */
  45. dev->iamthif_current_cb = NULL;
  46. dev->iamthif_canceled = false;
  47. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  48. dev->iamthif_timer = 0;
  49. dev->iamthif_stall_timer = 0;
  50. dev->iamthif_open_count = 0;
  51. }
  52. /**
  53. * mei_amthif_host_init - mei initialization amthif client.
  54. *
  55. * @dev: the device structure
  56. *
  57. * Return: 0 on success, <0 on failure.
  58. */
  59. int mei_amthif_host_init(struct mei_device *dev)
  60. {
  61. struct mei_cl *cl = &dev->iamthif_cl;
  62. struct mei_me_client *me_cl;
  63. int ret;
  64. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  65. mei_cl_init(cl, dev);
  66. me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
  67. if (!me_cl) {
  68. dev_info(dev->dev, "amthif: failed to find the client");
  69. return -ENOTTY;
  70. }
  71. cl->me_client_id = me_cl->client_id;
  72. cl->cl_uuid = me_cl->props.protocol_name;
  73. /* Assign iamthif_mtu to the value received from ME */
  74. dev->iamthif_mtu = me_cl->props.max_msg_length;
  75. dev_dbg(dev->dev, "IAMTHIF_MTU = %d\n", dev->iamthif_mtu);
  76. ret = mei_cl_link(cl, MEI_IAMTHIF_HOST_CLIENT_ID);
  77. if (ret < 0) {
  78. dev_err(dev->dev, "amthif: failed cl_link %d\n", ret);
  79. goto out;
  80. }
  81. ret = mei_cl_connect(cl, NULL);
  82. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  83. out:
  84. mei_me_cl_put(me_cl);
  85. return ret;
  86. }
  87. /**
  88. * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
  89. *
  90. * @dev: the device structure
  91. * @file: pointer to file object
  92. *
  93. * Return: returned a list entry on success, NULL on failure.
  94. */
  95. struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
  96. struct file *file)
  97. {
  98. struct mei_cl_cb *cb;
  99. list_for_each_entry(cb, &dev->amthif_rd_complete_list.list, list)
  100. if (cb->file_object == file)
  101. return cb;
  102. return NULL;
  103. }
  104. /**
  105. * mei_amthif_read - read data from AMTHIF client
  106. *
  107. * @dev: the device structure
  108. * @file: pointer to file object
  109. * @ubuf: pointer to user data in user space
  110. * @length: data length to read
  111. * @offset: data read offset
  112. *
  113. * Locking: called under "dev->device_lock" lock
  114. *
  115. * Return:
  116. * returned data length on success,
  117. * zero if no data to read,
  118. * negative on failure.
  119. */
  120. int mei_amthif_read(struct mei_device *dev, struct file *file,
  121. char __user *ubuf, size_t length, loff_t *offset)
  122. {
  123. struct mei_cl *cl = file->private_data;
  124. struct mei_cl_cb *cb;
  125. unsigned long timeout;
  126. int rets;
  127. int wait_ret;
  128. /* Only possible if we are in timeout */
  129. if (!cl) {
  130. dev_err(dev->dev, "bad file ext.\n");
  131. return -ETIME;
  132. }
  133. dev_dbg(dev->dev, "checking amthif data\n");
  134. cb = mei_amthif_find_read_list_entry(dev, file);
  135. /* Check for if we can block or not*/
  136. if (cb == NULL && file->f_flags & O_NONBLOCK)
  137. return -EAGAIN;
  138. dev_dbg(dev->dev, "waiting for amthif data\n");
  139. while (cb == NULL) {
  140. /* unlock the Mutex */
  141. mutex_unlock(&dev->device_lock);
  142. wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
  143. (cb = mei_amthif_find_read_list_entry(dev, file)));
  144. /* Locking again the Mutex */
  145. mutex_lock(&dev->device_lock);
  146. if (wait_ret)
  147. return -ERESTARTSYS;
  148. dev_dbg(dev->dev, "woke up from sleep\n");
  149. }
  150. if (cb->status) {
  151. rets = cb->status;
  152. dev_dbg(dev->dev, "read operation failed %d\n", rets);
  153. goto free;
  154. }
  155. dev_dbg(dev->dev, "Got amthif data\n");
  156. dev->iamthif_timer = 0;
  157. timeout = cb->read_time +
  158. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  159. dev_dbg(dev->dev, "amthif timeout = %lud\n",
  160. timeout);
  161. if (time_after(jiffies, timeout)) {
  162. dev_dbg(dev->dev, "amthif Time out\n");
  163. /* 15 sec for the message has expired */
  164. list_del_init(&cb->list);
  165. rets = -ETIME;
  166. goto free;
  167. }
  168. /* if the whole message will fit remove it from the list */
  169. if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
  170. list_del_init(&cb->list);
  171. else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  172. /* end of the message has been reached */
  173. list_del_init(&cb->list);
  174. rets = 0;
  175. goto free;
  176. }
  177. /* else means that not full buffer will be read and do not
  178. * remove message from deletion list
  179. */
  180. dev_dbg(dev->dev, "amthif cb->buf size - %d\n",
  181. cb->buf.size);
  182. dev_dbg(dev->dev, "amthif cb->buf_idx - %lu\n", cb->buf_idx);
  183. /* length is being truncated to PAGE_SIZE, however,
  184. * the buf_idx may point beyond */
  185. length = min_t(size_t, length, (cb->buf_idx - *offset));
  186. if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
  187. dev_dbg(dev->dev, "failed to copy data to userland\n");
  188. rets = -EFAULT;
  189. } else {
  190. rets = length;
  191. if ((*offset + length) < cb->buf_idx) {
  192. *offset += length;
  193. goto out;
  194. }
  195. }
  196. free:
  197. dev_dbg(dev->dev, "free amthif cb memory.\n");
  198. *offset = 0;
  199. mei_io_cb_free(cb);
  200. out:
  201. return rets;
  202. }
  203. /**
  204. * mei_amthif_read_start - queue message for sending read credential
  205. *
  206. * @cl: host client
  207. * @file: file pointer of message recipient
  208. *
  209. * Return: 0 on success, <0 on failure.
  210. */
  211. static int mei_amthif_read_start(struct mei_cl *cl, struct file *file)
  212. {
  213. struct mei_device *dev = cl->dev;
  214. struct mei_cl_cb *cb;
  215. size_t length = dev->iamthif_mtu;
  216. int rets;
  217. cb = mei_io_cb_init(cl, MEI_FOP_READ, file);
  218. if (!cb) {
  219. rets = -ENOMEM;
  220. goto err;
  221. }
  222. rets = mei_io_cb_alloc_buf(cb, length);
  223. if (rets)
  224. goto err;
  225. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  226. dev->iamthif_state = MEI_IAMTHIF_READING;
  227. dev->iamthif_file_object = cb->file_object;
  228. dev->iamthif_current_cb = cb;
  229. return 0;
  230. err:
  231. mei_io_cb_free(cb);
  232. return rets;
  233. }
  234. /**
  235. * mei_amthif_send_cmd - send amthif command to the ME
  236. *
  237. * @cl: the host client
  238. * @cb: mei call back struct
  239. *
  240. * Return: 0 on success, <0 on failure.
  241. */
  242. static int mei_amthif_send_cmd(struct mei_cl *cl, struct mei_cl_cb *cb)
  243. {
  244. struct mei_device *dev;
  245. int ret;
  246. if (!cl->dev || !cb)
  247. return -ENODEV;
  248. dev = cl->dev;
  249. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  250. dev->iamthif_current_cb = cb;
  251. dev->iamthif_file_object = cb->file_object;
  252. dev->iamthif_canceled = false;
  253. ret = mei_cl_write(cl, cb, false);
  254. if (ret < 0)
  255. return ret;
  256. if (cb->completed)
  257. cb->status = mei_amthif_read_start(cl, cb->file_object);
  258. return 0;
  259. }
  260. /**
  261. * mei_amthif_run_next_cmd - send next amt command from queue
  262. *
  263. * @dev: the device structure
  264. *
  265. * Return: 0 on success, <0 on failure.
  266. */
  267. int mei_amthif_run_next_cmd(struct mei_device *dev)
  268. {
  269. struct mei_cl *cl = &dev->iamthif_cl;
  270. struct mei_cl_cb *cb;
  271. dev->iamthif_canceled = false;
  272. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  273. dev->iamthif_timer = 0;
  274. dev->iamthif_file_object = NULL;
  275. dev_dbg(dev->dev, "complete amthif cmd_list cb.\n");
  276. cb = list_first_entry_or_null(&dev->amthif_cmd_list.list,
  277. typeof(*cb), list);
  278. if (!cb)
  279. return 0;
  280. list_del_init(&cb->list);
  281. return mei_amthif_send_cmd(cl, cb);
  282. }
  283. /**
  284. * mei_amthif_write - write amthif data to amthif client
  285. *
  286. * @cl: host client
  287. * @cb: mei call back struct
  288. *
  289. * Return: 0 on success, <0 on failure.
  290. */
  291. int mei_amthif_write(struct mei_cl *cl, struct mei_cl_cb *cb)
  292. {
  293. struct mei_device *dev;
  294. if (WARN_ON(!cl || !cl->dev))
  295. return -ENODEV;
  296. if (WARN_ON(!cb))
  297. return -EINVAL;
  298. dev = cl->dev;
  299. list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
  300. return mei_amthif_run_next_cmd(dev);
  301. }
  302. /**
  303. * mei_amthif_poll - the amthif poll function
  304. *
  305. * @dev: the device structure
  306. * @file: pointer to file structure
  307. * @wait: pointer to poll_table structure
  308. *
  309. * Return: poll mask
  310. *
  311. * Locking: called under "dev->device_lock" lock
  312. */
  313. unsigned int mei_amthif_poll(struct mei_device *dev,
  314. struct file *file, poll_table *wait)
  315. {
  316. unsigned int mask = 0;
  317. poll_wait(file, &dev->iamthif_cl.wait, wait);
  318. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
  319. dev->iamthif_file_object == file) {
  320. mask |= POLLIN | POLLRDNORM;
  321. mei_amthif_run_next_cmd(dev);
  322. }
  323. return mask;
  324. }
  325. /**
  326. * mei_amthif_irq_write - write iamthif command in irq thread context.
  327. *
  328. * @cl: private data of the file object.
  329. * @cb: callback block.
  330. * @cmpl_list: complete list.
  331. *
  332. * Return: 0, OK; otherwise, error.
  333. */
  334. int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  335. struct mei_cl_cb *cmpl_list)
  336. {
  337. int ret;
  338. ret = mei_cl_irq_write(cl, cb, cmpl_list);
  339. if (ret)
  340. return ret;
  341. if (cb->completed)
  342. cb->status = mei_amthif_read_start(cl, cb->file_object);
  343. return 0;
  344. }
  345. /**
  346. * mei_amthif_irq_read_msg - read routine after ISR to
  347. * handle the read amthif message
  348. *
  349. * @cl: mei client
  350. * @mei_hdr: header of amthif message
  351. * @cmpl_list: completed callbacks list
  352. *
  353. * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status
  354. */
  355. int mei_amthif_irq_read_msg(struct mei_cl *cl,
  356. struct mei_msg_hdr *mei_hdr,
  357. struct mei_cl_cb *cmpl_list)
  358. {
  359. struct mei_device *dev;
  360. int ret;
  361. dev = cl->dev;
  362. if (dev->iamthif_state != MEI_IAMTHIF_READING)
  363. return 0;
  364. ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
  365. if (ret)
  366. return ret;
  367. if (!mei_hdr->msg_complete)
  368. return 0;
  369. dev_dbg(dev->dev, "completed amthif read.\n ");
  370. dev->iamthif_current_cb = NULL;
  371. dev->iamthif_stall_timer = 0;
  372. return 0;
  373. }
  374. /**
  375. * mei_amthif_complete - complete amthif callback.
  376. *
  377. * @dev: the device structure.
  378. * @cb: callback block.
  379. */
  380. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb)
  381. {
  382. if (cb->fop_type == MEI_FOP_WRITE) {
  383. if (!cb->status) {
  384. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  385. mei_io_cb_free(cb);
  386. return;
  387. }
  388. /*
  389. * in case of error enqueue the write cb to complete read list
  390. * so it can be propagated to the reader
  391. */
  392. list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
  393. wake_up_interruptible(&dev->iamthif_cl.wait);
  394. return;
  395. }
  396. if (dev->iamthif_canceled != 1) {
  397. dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
  398. dev->iamthif_stall_timer = 0;
  399. list_add_tail(&cb->list, &dev->amthif_rd_complete_list.list);
  400. dev_dbg(dev->dev, "amthif read completed\n");
  401. dev->iamthif_timer = jiffies;
  402. dev_dbg(dev->dev, "dev->iamthif_timer = %ld\n",
  403. dev->iamthif_timer);
  404. } else {
  405. mei_amthif_run_next_cmd(dev);
  406. }
  407. dev_dbg(dev->dev, "completing amthif call back.\n");
  408. wake_up_interruptible(&dev->iamthif_cl.wait);
  409. }
  410. /**
  411. * mei_clear_list - removes all callbacks associated with file
  412. * from mei_cb_list
  413. *
  414. * @dev: device structure.
  415. * @file: file structure
  416. * @mei_cb_list: callbacks list
  417. *
  418. * mei_clear_list is called to clear resources associated with file
  419. * when application calls close function or Ctrl-C was pressed
  420. *
  421. * Return: true if callback removed from the list, false otherwise
  422. */
  423. static bool mei_clear_list(struct mei_device *dev,
  424. const struct file *file, struct list_head *mei_cb_list)
  425. {
  426. struct mei_cl *cl = &dev->iamthif_cl;
  427. struct mei_cl_cb *cb, *next;
  428. bool removed = false;
  429. /* list all list member */
  430. list_for_each_entry_safe(cb, next, mei_cb_list, list) {
  431. /* check if list member associated with a file */
  432. if (file == cb->file_object) {
  433. /* check if cb equal to current iamthif cb */
  434. if (dev->iamthif_current_cb == cb) {
  435. dev->iamthif_current_cb = NULL;
  436. /* send flow control to iamthif client */
  437. mei_hbm_cl_flow_control_req(dev, cl);
  438. }
  439. /* free all allocated buffers */
  440. mei_io_cb_free(cb);
  441. removed = true;
  442. }
  443. }
  444. return removed;
  445. }
  446. /**
  447. * mei_clear_lists - removes all callbacks associated with file
  448. *
  449. * @dev: device structure
  450. * @file: file structure
  451. *
  452. * mei_clear_lists is called to clear resources associated with file
  453. * when application calls close function or Ctrl-C was pressed
  454. *
  455. * Return: true if callback removed from the list, false otherwise
  456. */
  457. static bool mei_clear_lists(struct mei_device *dev, struct file *file)
  458. {
  459. bool removed = false;
  460. /* remove callbacks associated with a file */
  461. mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
  462. if (mei_clear_list(dev, file, &dev->amthif_rd_complete_list.list))
  463. removed = true;
  464. mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
  465. if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
  466. removed = true;
  467. if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
  468. removed = true;
  469. if (mei_clear_list(dev, file, &dev->write_list.list))
  470. removed = true;
  471. /* check if iamthif_current_cb not NULL */
  472. if (dev->iamthif_current_cb && !removed) {
  473. /* check file and iamthif current cb association */
  474. if (dev->iamthif_current_cb->file_object == file) {
  475. /* remove cb */
  476. mei_io_cb_free(dev->iamthif_current_cb);
  477. dev->iamthif_current_cb = NULL;
  478. removed = true;
  479. }
  480. }
  481. return removed;
  482. }
  483. /**
  484. * mei_amthif_release - the release function
  485. *
  486. * @dev: device structure
  487. * @file: pointer to file structure
  488. *
  489. * Return: 0 on success, <0 on error
  490. */
  491. int mei_amthif_release(struct mei_device *dev, struct file *file)
  492. {
  493. if (dev->iamthif_open_count > 0)
  494. dev->iamthif_open_count--;
  495. if (dev->iamthif_file_object == file &&
  496. dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  497. dev_dbg(dev->dev, "amthif canceled iamthif state %d\n",
  498. dev->iamthif_state);
  499. dev->iamthif_canceled = true;
  500. if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
  501. dev_dbg(dev->dev, "run next amthif iamthif cb\n");
  502. mei_amthif_run_next_cmd(dev);
  503. }
  504. }
  505. if (mei_clear_lists(dev, file))
  506. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  507. return 0;
  508. }