amthif.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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, typeof(*cb), list);
  114. if (!cb) {
  115. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  116. cl->fp = NULL;
  117. return 0;
  118. }
  119. list_del_init(&cb->list);
  120. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  121. cl->fp = cb->fp;
  122. ret = mei_cl_write(cl, cb);
  123. if (ret < 0)
  124. return ret;
  125. if (cb->completed)
  126. cb->status = mei_amthif_read_start(cl, cb->fp);
  127. return 0;
  128. }
  129. /**
  130. * mei_amthif_write - write amthif data to amthif client
  131. *
  132. * @cl: host client
  133. * @cb: mei call back struct
  134. *
  135. * Return: 0 on success, <0 on failure.
  136. */
  137. int mei_amthif_write(struct mei_cl *cl, struct mei_cl_cb *cb)
  138. {
  139. struct mei_device *dev = cl->dev;
  140. list_add_tail(&cb->list, &dev->amthif_cmd_list);
  141. /*
  142. * The previous request is still in processing, queue this one.
  143. */
  144. if (dev->iamthif_state != MEI_IAMTHIF_IDLE)
  145. return 0;
  146. return mei_amthif_run_next_cmd(dev);
  147. }
  148. /**
  149. * mei_amthif_poll - the amthif poll function
  150. *
  151. * @file: pointer to file structure
  152. * @wait: pointer to poll_table structure
  153. *
  154. * Return: poll mask
  155. *
  156. * Locking: called under "dev->device_lock" lock
  157. */
  158. unsigned int mei_amthif_poll(struct file *file, poll_table *wait)
  159. {
  160. struct mei_cl *cl = file->private_data;
  161. struct mei_cl_cb *cb = mei_cl_read_cb(cl, file);
  162. unsigned int mask = 0;
  163. poll_wait(file, &cl->rx_wait, wait);
  164. if (cb)
  165. mask |= POLLIN | POLLRDNORM;
  166. return mask;
  167. }
  168. /**
  169. * mei_amthif_irq_write - write iamthif command in irq thread context.
  170. *
  171. * @cl: private data of the file object.
  172. * @cb: callback block.
  173. * @cmpl_list: complete list.
  174. *
  175. * Return: 0, OK; otherwise, error.
  176. */
  177. int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
  178. struct list_head *cmpl_list)
  179. {
  180. int ret;
  181. ret = mei_cl_irq_write(cl, cb, cmpl_list);
  182. if (ret)
  183. return ret;
  184. if (cb->completed)
  185. cb->status = mei_amthif_read_start(cl, cb->fp);
  186. return 0;
  187. }
  188. /**
  189. * mei_amthif_irq_read_msg - read routine after ISR to
  190. * handle the read amthif message
  191. *
  192. * @cl: mei client
  193. * @mei_hdr: header of amthif message
  194. * @cmpl_list: completed callbacks list
  195. *
  196. * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status
  197. */
  198. int mei_amthif_irq_read_msg(struct mei_cl *cl,
  199. struct mei_msg_hdr *mei_hdr,
  200. struct list_head *cmpl_list)
  201. {
  202. struct mei_device *dev;
  203. int ret;
  204. dev = cl->dev;
  205. if (dev->iamthif_state != MEI_IAMTHIF_READING) {
  206. mei_irq_discard_msg(dev, mei_hdr);
  207. return 0;
  208. }
  209. ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
  210. if (ret)
  211. return ret;
  212. if (!mei_hdr->msg_complete)
  213. return 0;
  214. dev_dbg(dev->dev, "completed amthif read.\n ");
  215. dev->iamthif_stall_timer = 0;
  216. return 0;
  217. }
  218. /**
  219. * mei_amthif_complete - complete amthif callback.
  220. *
  221. * @cl: host client
  222. * @cb: callback block.
  223. */
  224. void mei_amthif_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
  225. {
  226. struct mei_device *dev = cl->dev;
  227. dev_dbg(dev->dev, "completing amthif call back.\n");
  228. switch (cb->fop_type) {
  229. case MEI_FOP_WRITE:
  230. if (!cb->status) {
  231. dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
  232. mei_schedule_stall_timer(dev);
  233. mei_io_cb_free(cb);
  234. return;
  235. }
  236. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  237. cl->fp = NULL;
  238. if (!dev->iamthif_canceled) {
  239. /*
  240. * in case of error enqueue the write cb to complete
  241. * read list so it can be propagated to the reader
  242. */
  243. list_add_tail(&cb->list, &cl->rd_completed);
  244. wake_up_interruptible(&cl->rx_wait);
  245. } else {
  246. mei_io_cb_free(cb);
  247. }
  248. break;
  249. case MEI_FOP_READ:
  250. if (!dev->iamthif_canceled) {
  251. list_add_tail(&cb->list, &cl->rd_completed);
  252. dev_dbg(dev->dev, "amthif read completed\n");
  253. wake_up_interruptible(&cl->rx_wait);
  254. } else {
  255. mei_io_cb_free(cb);
  256. }
  257. dev->iamthif_stall_timer = 0;
  258. mei_amthif_run_next_cmd(dev);
  259. break;
  260. default:
  261. WARN_ON(1);
  262. }
  263. }
  264. /**
  265. * mei_amthif_release - the release function
  266. *
  267. * @dev: device structure
  268. * @fp: pointer to file structure
  269. *
  270. * Return: 0 on success, <0 on error
  271. */
  272. int mei_amthif_release(struct mei_device *dev, struct file *fp)
  273. {
  274. struct mei_cl *cl = fp->private_data;
  275. if (dev->iamthif_open_count > 0)
  276. dev->iamthif_open_count--;
  277. if (cl->fp == fp && dev->iamthif_state != MEI_IAMTHIF_IDLE) {
  278. dev_dbg(dev->dev, "amthif canceled iamthif state %d\n",
  279. dev->iamthif_state);
  280. dev->iamthif_canceled = true;
  281. }
  282. /* Don't clean ctrl_rd_list here, the reads has to be completed */
  283. mei_io_list_free_fp(&dev->amthif_cmd_list, fp);
  284. mei_io_list_free_fp(&cl->rd_completed, fp);
  285. return 0;
  286. }