amthif.c 14 KB

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