amthif.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_canceled = false;
  46. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  47. dev->iamthif_stall_timer = 0;
  48. dev->iamthif_open_count = 0;
  49. }
  50. /**
  51. * mei_amthif_host_init - mei initialization amthif client.
  52. *
  53. * @dev: the device structure
  54. * @me_cl: me client
  55. *
  56. * Return: 0 on success, <0 on failure.
  57. */
  58. int mei_amthif_host_init(struct mei_device *dev, struct mei_me_client *me_cl)
  59. {
  60. struct mei_cl *cl = &dev->iamthif_cl;
  61. int ret;
  62. mutex_lock(&dev->device_lock);
  63. if (mei_cl_is_connected(cl)) {
  64. ret = 0;
  65. goto out;
  66. }
  67. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  68. mei_cl_init(cl, dev);
  69. ret = mei_cl_link(cl);
  70. if (ret < 0) {
  71. dev_err(dev->dev, "amthif: failed cl_link %d\n", ret);
  72. goto out;
  73. }
  74. ret = mei_cl_connect(cl, me_cl, NULL);
  75. out:
  76. mutex_unlock(&dev->device_lock);
  77. return ret;
  78. }
  79. /**
  80. * mei_amthif_read_start - queue message for sending read credential
  81. *
  82. * @cl: host client
  83. * @fp: file pointer of message recipient
  84. *
  85. * Return: 0 on success, <0 on failure.
  86. */
  87. static int mei_amthif_read_start(struct mei_cl *cl, const struct file *fp)
  88. {
  89. struct mei_device *dev = cl->dev;
  90. struct mei_cl_cb *cb;
  91. cb = mei_cl_enqueue_ctrl_wr_cb(cl, mei_cl_mtu(cl), MEI_FOP_READ, fp);
  92. if (!cb)
  93. return -ENOMEM;
  94. cl->rx_flow_ctrl_creds++;
  95. dev->iamthif_state = MEI_IAMTHIF_READING;
  96. cl->fp = cb->fp;
  97. return 0;
  98. }
  99. /**
  100. * mei_amthif_run_next_cmd - send next amt command from queue
  101. *
  102. * @dev: the device structure
  103. *
  104. * Return: 0 on success, <0 on failure.
  105. */
  106. int mei_amthif_run_next_cmd(struct mei_device *dev)
  107. {
  108. struct mei_cl *cl = &dev->iamthif_cl;
  109. struct mei_cl_cb *cb;
  110. int ret;
  111. dev->iamthif_canceled = false;
  112. dev_dbg(dev->dev, "complete amthif cmd_list cb.\n");
  113. cb = list_first_entry_or_null(&dev->amthif_cmd_list.list,
  114. typeof(*cb), list);
  115. if (!cb) {
  116. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  117. cl->fp = NULL;
  118. return 0;
  119. }
  120. list_del_init(&cb->list);
  121. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  122. cl->fp = cb->fp;
  123. ret = mei_cl_write(cl, cb, false);
  124. if (ret < 0)
  125. return ret;
  126. if (cb->completed)
  127. cb->status = mei_amthif_read_start(cl, cb->fp);
  128. return 0;
  129. }
  130. /**
  131. * mei_amthif_write - write amthif data to amthif client
  132. *
  133. * @cl: host client
  134. * @cb: mei call back struct
  135. *
  136. * Return: 0 on success, <0 on failure.
  137. */
  138. int mei_amthif_write(struct mei_cl *cl, struct mei_cl_cb *cb)
  139. {
  140. struct mei_device *dev = cl->dev;
  141. list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
  142. /*
  143. * The previous request is still in processing, queue this one.
  144. */
  145. if (dev->iamthif_state != MEI_IAMTHIF_IDLE)
  146. return 0;
  147. return mei_amthif_run_next_cmd(dev);
  148. }
  149. /**
  150. * mei_amthif_poll - the amthif poll function
  151. *
  152. * @file: pointer to file structure
  153. * @wait: pointer to poll_table structure
  154. *
  155. * Return: poll mask
  156. *
  157. * Locking: called under "dev->device_lock" lock
  158. */
  159. unsigned int mei_amthif_poll(struct file *file, poll_table *wait)
  160. {
  161. struct mei_cl *cl = file->private_data;
  162. struct mei_cl_cb *cb = mei_cl_read_cb(cl, file);
  163. unsigned int mask = 0;
  164. poll_wait(file, &cl->rx_wait, wait);
  165. if (cb)
  166. mask |= POLLIN | POLLRDNORM;
  167. return mask;
  168. }
  169. /**
  170. * mei_amthif_irq_write - write iamthif command in irq thread context.
  171. *
  172. * @cl: private data of the file object.
  173. * @cb: callback block.
  174. * @cmpl_list: complete list.
  175. *
  176. * Return: 0, OK; otherwise, error.
  177. */
  178. int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  179. struct mei_cl_cb *cmpl_list)
  180. {
  181. int ret;
  182. ret = mei_cl_irq_write(cl, cb, cmpl_list);
  183. if (ret)
  184. return ret;
  185. if (cb->completed)
  186. cb->status = mei_amthif_read_start(cl, cb->fp);
  187. return 0;
  188. }
  189. /**
  190. * mei_amthif_irq_read_msg - read routine after ISR to
  191. * handle the read amthif message
  192. *
  193. * @cl: mei client
  194. * @mei_hdr: header of amthif message
  195. * @cmpl_list: completed callbacks list
  196. *
  197. * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status
  198. */
  199. int mei_amthif_irq_read_msg(struct mei_cl *cl,
  200. struct mei_msg_hdr *mei_hdr,
  201. struct mei_cl_cb *cmpl_list)
  202. {
  203. struct mei_device *dev;
  204. int ret;
  205. dev = cl->dev;
  206. if (dev->iamthif_state != MEI_IAMTHIF_READING) {
  207. mei_irq_discard_msg(dev, mei_hdr);
  208. return 0;
  209. }
  210. ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
  211. if (ret)
  212. return ret;
  213. if (!mei_hdr->msg_complete)
  214. return 0;
  215. dev_dbg(dev->dev, "completed amthif read.\n ");
  216. dev->iamthif_stall_timer = 0;
  217. return 0;
  218. }
  219. /**
  220. * mei_amthif_complete - complete amthif callback.
  221. *
  222. * @cl: host client
  223. * @cb: callback block.
  224. */
  225. void mei_amthif_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  226. {
  227. struct mei_device *dev = cl->dev;
  228. dev_dbg(dev->dev, "completing amthif call back.\n");
  229. switch (cb->fop_type) {
  230. case MEI_FOP_WRITE:
  231. if (!cb->status) {
  232. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  233. mei_schedule_stall_timer(dev);
  234. mei_io_cb_free(cb);
  235. return;
  236. }
  237. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  238. cl->fp = NULL;
  239. if (!dev->iamthif_canceled) {
  240. /*
  241. * in case of error enqueue the write cb to complete
  242. * read list so it can be propagated to the reader
  243. */
  244. list_add_tail(&cb->list, &cl->rd_completed);
  245. wake_up_interruptible(&cl->rx_wait);
  246. } else {
  247. mei_io_cb_free(cb);
  248. }
  249. break;
  250. case MEI_FOP_READ:
  251. if (!dev->iamthif_canceled) {
  252. list_add_tail(&cb->list, &cl->rd_completed);
  253. dev_dbg(dev->dev, "amthif read completed\n");
  254. wake_up_interruptible(&cl->rx_wait);
  255. } else {
  256. mei_io_cb_free(cb);
  257. }
  258. dev->iamthif_stall_timer = 0;
  259. mei_amthif_run_next_cmd(dev);
  260. break;
  261. default:
  262. WARN_ON(1);
  263. }
  264. }
  265. /**
  266. * mei_clear_list - removes all callbacks associated with file
  267. * from mei_cb_list
  268. *
  269. * @file: file structure
  270. * @mei_cb_list: callbacks list
  271. *
  272. * mei_clear_list is called to clear resources associated with file
  273. * when application calls close function or Ctrl-C was pressed
  274. */
  275. static void mei_clear_list(const struct file *file,
  276. struct list_head *mei_cb_list)
  277. {
  278. struct mei_cl_cb *cb, *next;
  279. list_for_each_entry_safe(cb, next, mei_cb_list, list)
  280. if (file == cb->fp)
  281. mei_io_cb_free(cb);
  282. }
  283. /**
  284. * mei_amthif_release - the release function
  285. *
  286. * @dev: device structure
  287. * @file: pointer to file structure
  288. *
  289. * Return: 0 on success, <0 on error
  290. */
  291. int mei_amthif_release(struct mei_device *dev, struct file *file)
  292. {
  293. struct mei_cl *cl = file->private_data;
  294. if (dev->iamthif_open_count > 0)
  295. dev->iamthif_open_count--;
  296. if (cl->fp == file && dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  297. dev_dbg(dev->dev, "amthif canceled iamthif state %d\n",
  298. dev->iamthif_state);
  299. dev->iamthif_canceled = true;
  300. }
  301. mei_clear_list(file, &dev->amthif_cmd_list.list);
  302. mei_clear_list(file, &cl->rd_completed);
  303. mei_clear_list(file, &dev->ctrl_rd_list.list);
  304. return 0;
  305. }