iorw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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/aio.h>
  22. #include <linux/pci.h>
  23. #include <linux/init.h>
  24. #include <linux/ioctl.h>
  25. #include <linux/cdev.h>
  26. #include <linux/list.h>
  27. #include <linux/delay.h>
  28. #include <linux/sched.h>
  29. #include <linux/uuid.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/uaccess.h>
  32. #include "mei_dev.h"
  33. #include "hw.h"
  34. #include <linux/mei.h>
  35. #include "interface.h"
  36. /**
  37. * mei_me_cl_by_id return index to me_clients for client_id
  38. *
  39. * @dev: the device structure
  40. * @client_id: me client id
  41. *
  42. * Locking: called under "dev->device_lock" lock
  43. *
  44. * returns index on success, -ENOENT on failure.
  45. */
  46. int mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
  47. {
  48. int i;
  49. for (i = 0; i < dev->me_clients_num; i++)
  50. if (dev->me_clients[i].client_id == client_id)
  51. break;
  52. if (WARN_ON(dev->me_clients[i].client_id != client_id))
  53. return -ENOENT;
  54. if (i == dev->me_clients_num)
  55. return -ENOENT;
  56. return i;
  57. }
  58. /**
  59. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  60. *
  61. * @dev: the device structure
  62. * @data: IOCTL connect data, input and output parameters
  63. * @file: private data of the file object
  64. *
  65. * Locking: called under "dev->device_lock" lock
  66. *
  67. * returns 0 on success, <0 on failure.
  68. */
  69. int mei_ioctl_connect_client(struct file *file,
  70. struct mei_connect_client_data *data)
  71. {
  72. struct mei_device *dev;
  73. struct mei_cl_cb *cb;
  74. struct mei_client *client;
  75. struct mei_cl *cl;
  76. struct mei_cl *cl_pos = NULL;
  77. struct mei_cl *cl_next = NULL;
  78. long timeout = CONNECT_TIMEOUT;
  79. int i;
  80. int err;
  81. int rets;
  82. cl = file->private_data;
  83. if (WARN_ON(!cl || !cl->dev))
  84. return -ENODEV;
  85. dev = cl->dev;
  86. dev_dbg(&dev->pdev->dev, "mei_ioctl_connect_client() Entry\n");
  87. /* buffered ioctl cb */
  88. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  89. if (!cb) {
  90. rets = -ENOMEM;
  91. goto end;
  92. }
  93. mei_io_list_init(cb);
  94. cb->major_file_operations = MEI_IOCTL;
  95. if (dev->dev_state != MEI_DEV_ENABLED) {
  96. rets = -ENODEV;
  97. goto end;
  98. }
  99. if (cl->state != MEI_FILE_INITIALIZING &&
  100. cl->state != MEI_FILE_DISCONNECTED) {
  101. rets = -EBUSY;
  102. goto end;
  103. }
  104. /* find ME client we're trying to connect to */
  105. i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  106. if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
  107. cl->me_client_id = dev->me_clients[i].client_id;
  108. cl->state = MEI_FILE_CONNECTING;
  109. }
  110. dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
  111. cl->me_client_id);
  112. dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
  113. dev->me_clients[i].props.protocol_version);
  114. dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
  115. dev->me_clients[i].props.max_msg_length);
  116. /* if we're connecting to amthi client then we will use the
  117. * existing connection
  118. */
  119. if (uuid_le_cmp(data->in_client_uuid, mei_amthi_guid) == 0) {
  120. dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
  121. if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
  122. rets = -ENODEV;
  123. goto end;
  124. }
  125. clear_bit(cl->host_client_id, dev->host_clients_map);
  126. list_for_each_entry_safe(cl_pos, cl_next,
  127. &dev->file_list, link) {
  128. if (mei_cl_cmp_id(cl, cl_pos)) {
  129. dev_dbg(&dev->pdev->dev,
  130. "remove file private data node host"
  131. " client = %d, ME client = %d.\n",
  132. cl_pos->host_client_id,
  133. cl_pos->me_client_id);
  134. list_del(&cl_pos->link);
  135. }
  136. }
  137. dev_dbg(&dev->pdev->dev, "free file private data memory.\n");
  138. kfree(cl);
  139. cl = NULL;
  140. file->private_data = &dev->iamthif_cl;
  141. client = &data->out_client_properties;
  142. client->max_msg_length =
  143. dev->me_clients[i].props.max_msg_length;
  144. client->protocol_version =
  145. dev->me_clients[i].props.protocol_version;
  146. rets = dev->iamthif_cl.status;
  147. goto end;
  148. }
  149. if (cl->state != MEI_FILE_CONNECTING) {
  150. rets = -ENODEV;
  151. goto end;
  152. }
  153. /* prepare the output buffer */
  154. client = &data->out_client_properties;
  155. client->max_msg_length = dev->me_clients[i].props.max_msg_length;
  156. client->protocol_version = dev->me_clients[i].props.protocol_version;
  157. dev_dbg(&dev->pdev->dev, "Can connect?\n");
  158. if (dev->mei_host_buffer_is_empty
  159. && !mei_other_client_is_connecting(dev, cl)) {
  160. dev_dbg(&dev->pdev->dev, "Sending Connect Message\n");
  161. dev->mei_host_buffer_is_empty = false;
  162. if (mei_connect(dev, cl)) {
  163. dev_dbg(&dev->pdev->dev, "Sending connect message - failed\n");
  164. rets = -ENODEV;
  165. goto end;
  166. } else {
  167. dev_dbg(&dev->pdev->dev, "Sending connect message - succeeded\n");
  168. cl->timer_count = MEI_CONNECT_TIMEOUT;
  169. cb->file_private = cl;
  170. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  171. }
  172. } else {
  173. dev_dbg(&dev->pdev->dev, "Queuing the connect request due to device busy\n");
  174. cb->file_private = cl;
  175. dev_dbg(&dev->pdev->dev, "add connect cb to control write list.\n");
  176. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  177. }
  178. mutex_unlock(&dev->device_lock);
  179. err = wait_event_timeout(dev->wait_recvd_msg,
  180. (MEI_FILE_CONNECTED == cl->state ||
  181. MEI_FILE_DISCONNECTED == cl->state),
  182. timeout * HZ);
  183. mutex_lock(&dev->device_lock);
  184. if (MEI_FILE_CONNECTED == cl->state) {
  185. dev_dbg(&dev->pdev->dev, "successfully connected to FW client.\n");
  186. rets = cl->status;
  187. goto end;
  188. } else {
  189. dev_dbg(&dev->pdev->dev, "failed to connect to FW client.cl->state = %d.\n",
  190. cl->state);
  191. if (!err) {
  192. dev_dbg(&dev->pdev->dev,
  193. "wait_event_interruptible_timeout failed on client"
  194. " connect message fw response message.\n");
  195. }
  196. rets = -EFAULT;
  197. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  198. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  199. goto end;
  200. }
  201. rets = 0;
  202. end:
  203. dev_dbg(&dev->pdev->dev, "free connect cb memory.");
  204. kfree(cb);
  205. return rets;
  206. }
  207. /**
  208. * find_amthi_read_list_entry - finds a amthilist entry for current file
  209. *
  210. * @dev: the device structure
  211. * @file: pointer to file object
  212. *
  213. * returns returned a list entry on success, NULL on failure.
  214. */
  215. struct mei_cl_cb *find_amthi_read_list_entry(
  216. struct mei_device *dev,
  217. struct file *file)
  218. {
  219. struct mei_cl *cl_temp;
  220. struct mei_cl_cb *pos = NULL;
  221. struct mei_cl_cb *next = NULL;
  222. list_for_each_entry_safe(pos, next,
  223. &dev->amthi_read_complete_list.list, list) {
  224. cl_temp = (struct mei_cl *)pos->file_private;
  225. if (cl_temp && cl_temp == &dev->iamthif_cl &&
  226. pos->file_object == file)
  227. return pos;
  228. }
  229. return NULL;
  230. }
  231. /**
  232. * amthi_read - read data from AMTHI client
  233. *
  234. * @dev: the device structure
  235. * @if_num: minor number
  236. * @file: pointer to file object
  237. * @*ubuf: pointer to user data in user space
  238. * @length: data length to read
  239. * @offset: data read offset
  240. *
  241. * Locking: called under "dev->device_lock" lock
  242. *
  243. * returns
  244. * returned data length on success,
  245. * zero if no data to read,
  246. * negative on failure.
  247. */
  248. int amthi_read(struct mei_device *dev, struct file *file,
  249. char __user *ubuf, size_t length, loff_t *offset)
  250. {
  251. int rets;
  252. int wait_ret;
  253. struct mei_cl_cb *cb = NULL;
  254. struct mei_cl *cl = file->private_data;
  255. unsigned long timeout;
  256. int i;
  257. /* Only Posible if we are in timeout */
  258. if (!cl || cl != &dev->iamthif_cl) {
  259. dev_dbg(&dev->pdev->dev, "bad file ext.\n");
  260. return -ETIMEDOUT;
  261. }
  262. i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
  263. if (i < 0) {
  264. dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
  265. return -ENODEV;
  266. }
  267. dev_dbg(&dev->pdev->dev, "checking amthi data\n");
  268. cb = find_amthi_read_list_entry(dev, file);
  269. /* Check for if we can block or not*/
  270. if (cb == NULL && file->f_flags & O_NONBLOCK)
  271. return -EAGAIN;
  272. dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
  273. while (cb == NULL) {
  274. /* unlock the Mutex */
  275. mutex_unlock(&dev->device_lock);
  276. wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
  277. (cb = find_amthi_read_list_entry(dev, file)));
  278. if (wait_ret)
  279. return -ERESTARTSYS;
  280. dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
  281. /* Locking again the Mutex */
  282. mutex_lock(&dev->device_lock);
  283. }
  284. dev_dbg(&dev->pdev->dev, "Got amthi data\n");
  285. dev->iamthif_timer = 0;
  286. if (cb) {
  287. timeout = cb->read_time + msecs_to_jiffies(IAMTHIF_READ_TIMER);
  288. dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
  289. timeout);
  290. if (time_after(jiffies, timeout)) {
  291. dev_dbg(&dev->pdev->dev, "amthi Time out\n");
  292. /* 15 sec for the message has expired */
  293. list_del(&cb->list);
  294. rets = -ETIMEDOUT;
  295. goto free;
  296. }
  297. }
  298. /* if the whole message will fit remove it from the list */
  299. if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
  300. list_del(&cb->list);
  301. else if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  302. /* end of the message has been reached */
  303. list_del(&cb->list);
  304. rets = 0;
  305. goto free;
  306. }
  307. /* else means that not full buffer will be read and do not
  308. * remove message from deletion list
  309. */
  310. dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
  311. cb->response_buffer.size);
  312. dev_dbg(&dev->pdev->dev, "amthi cb->buf_idx - %lu\n", cb->buf_idx);
  313. /* length is being turncated to PAGE_SIZE, however,
  314. * the buf_idx may point beyond */
  315. length = min_t(size_t, length, (cb->buf_idx - *offset));
  316. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
  317. rets = -EFAULT;
  318. else {
  319. rets = length;
  320. if ((*offset + length) < cb->buf_idx) {
  321. *offset += length;
  322. goto out;
  323. }
  324. }
  325. free:
  326. dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
  327. *offset = 0;
  328. mei_free_cb_private(cb);
  329. out:
  330. return rets;
  331. }
  332. /**
  333. * mei_start_read - the start read client message function.
  334. *
  335. * @dev: the device structure
  336. * @if_num: minor number
  337. * @cl: private data of the file object
  338. *
  339. * returns 0 on success, <0 on failure.
  340. */
  341. int mei_start_read(struct mei_device *dev, struct mei_cl *cl)
  342. {
  343. struct mei_cl_cb *cb;
  344. int rets = 0;
  345. int i;
  346. if (cl->state != MEI_FILE_CONNECTED)
  347. return -ENODEV;
  348. if (dev->dev_state != MEI_DEV_ENABLED)
  349. return -ENODEV;
  350. dev_dbg(&dev->pdev->dev, "check if read is pending.\n");
  351. if (cl->read_pending || cl->read_cb) {
  352. dev_dbg(&dev->pdev->dev, "read is pending.\n");
  353. return -EBUSY;
  354. }
  355. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  356. if (!cb)
  357. return -ENOMEM;
  358. dev_dbg(&dev->pdev->dev, "allocation call back successful. host client = %d, ME client = %d\n",
  359. cl->host_client_id, cl->me_client_id);
  360. i = mei_me_cl_by_id(dev, cl->me_client_id);
  361. if (i < 0) {
  362. rets = -ENODEV;
  363. goto unlock;
  364. }
  365. cb->response_buffer.size = dev->me_clients[i].props.max_msg_length;
  366. cb->response_buffer.data =
  367. kmalloc(cb->response_buffer.size, GFP_KERNEL);
  368. if (!cb->response_buffer.data) {
  369. rets = -ENOMEM;
  370. goto unlock;
  371. }
  372. dev_dbg(&dev->pdev->dev, "allocation call back data success.\n");
  373. cb->major_file_operations = MEI_READ;
  374. /* make sure buffer index is zero before we start */
  375. cb->buf_idx = 0;
  376. cb->file_private = (void *) cl;
  377. cl->read_cb = cb;
  378. if (dev->mei_host_buffer_is_empty) {
  379. dev->mei_host_buffer_is_empty = false;
  380. if (mei_send_flow_control(dev, cl)) {
  381. rets = -ENODEV;
  382. goto unlock;
  383. }
  384. list_add_tail(&cb->list, &dev->read_list.list);
  385. } else {
  386. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  387. }
  388. return rets;
  389. unlock:
  390. mei_free_cb_private(cb);
  391. return rets;
  392. }
  393. /**
  394. * amthi_write - write iamthif data to amthi client
  395. *
  396. * @dev: the device structure
  397. * @cb: mei call back struct
  398. *
  399. * returns 0 on success, <0 on failure.
  400. */
  401. int amthi_write(struct mei_device *dev, struct mei_cl_cb *cb)
  402. {
  403. struct mei_msg_hdr mei_hdr;
  404. int ret;
  405. if (!dev || !cb)
  406. return -ENODEV;
  407. dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
  408. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  409. dev->iamthif_current_cb = cb;
  410. dev->iamthif_file_object = cb->file_object;
  411. dev->iamthif_canceled = false;
  412. dev->iamthif_ioctl = true;
  413. dev->iamthif_msg_buf_size = cb->request_buffer.size;
  414. memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
  415. cb->request_buffer.size);
  416. ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
  417. if (ret < 0)
  418. return ret;
  419. if (ret && dev->mei_host_buffer_is_empty) {
  420. ret = 0;
  421. dev->mei_host_buffer_is_empty = false;
  422. if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
  423. mei_hdr.length = mei_hbuf_max_data(dev);
  424. mei_hdr.msg_complete = 0;
  425. } else {
  426. mei_hdr.length = cb->request_buffer.size;
  427. mei_hdr.msg_complete = 1;
  428. }
  429. mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
  430. mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
  431. mei_hdr.reserved = 0;
  432. dev->iamthif_msg_buf_index += mei_hdr.length;
  433. if (mei_write_message(dev, &mei_hdr,
  434. (unsigned char *)(dev->iamthif_msg_buf),
  435. mei_hdr.length))
  436. return -ENODEV;
  437. if (mei_hdr.msg_complete) {
  438. if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
  439. return -ENODEV;
  440. dev->iamthif_flow_control_pending = true;
  441. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  442. dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
  443. dev->iamthif_current_cb = cb;
  444. dev->iamthif_file_object = cb->file_object;
  445. list_add_tail(&cb->list, &dev->write_waiting_list.list);
  446. } else {
  447. dev_dbg(&dev->pdev->dev, "message does not complete, "
  448. "so add amthi cb to write list.\n");
  449. list_add_tail(&cb->list, &dev->write_list.list);
  450. }
  451. } else {
  452. if (!(dev->mei_host_buffer_is_empty))
  453. dev_dbg(&dev->pdev->dev, "host buffer is not empty");
  454. dev_dbg(&dev->pdev->dev, "No flow control credentials, "
  455. "so add iamthif cb to write list.\n");
  456. list_add_tail(&cb->list, &dev->write_list.list);
  457. }
  458. return 0;
  459. }
  460. /**
  461. * iamthif_ioctl_send_msg - send cmd data to amthi client
  462. *
  463. * @dev: the device structure
  464. *
  465. * returns 0 on success, <0 on failure.
  466. */
  467. void mei_run_next_iamthif_cmd(struct mei_device *dev)
  468. {
  469. struct mei_cl *cl_tmp;
  470. struct mei_cl_cb *pos = NULL;
  471. struct mei_cl_cb *next = NULL;
  472. int status;
  473. if (!dev)
  474. return;
  475. dev->iamthif_msg_buf_size = 0;
  476. dev->iamthif_msg_buf_index = 0;
  477. dev->iamthif_canceled = false;
  478. dev->iamthif_ioctl = true;
  479. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  480. dev->iamthif_timer = 0;
  481. dev->iamthif_file_object = NULL;
  482. dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
  483. list_for_each_entry_safe(pos, next, &dev->amthi_cmd_list.list, list) {
  484. list_del(&pos->list);
  485. cl_tmp = (struct mei_cl *)pos->file_private;
  486. if (cl_tmp && cl_tmp == &dev->iamthif_cl) {
  487. status = amthi_write(dev, pos);
  488. if (status) {
  489. dev_dbg(&dev->pdev->dev,
  490. "amthi write failed status = %d\n",
  491. status);
  492. return;
  493. }
  494. break;
  495. }
  496. }
  497. }
  498. /**
  499. * mei_free_cb_private - free mei_cb_private related memory
  500. *
  501. * @cb: mei callback struct
  502. */
  503. void mei_free_cb_private(struct mei_cl_cb *cb)
  504. {
  505. if (cb == NULL)
  506. return;
  507. kfree(cb->request_buffer.data);
  508. kfree(cb->response_buffer.data);
  509. kfree(cb);
  510. }