amthif.c 12 KB

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