iorw.c 16 KB

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