iorw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. INIT_LIST_HEAD(&cb->cb_list);
  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->cb_list,
  171. &dev->ctrl_rd_list.mei_cb.
  172. cb_list);
  173. }
  174. } else {
  175. dev_dbg(&dev->pdev->dev, "Queuing the connect request due to device busy\n");
  176. cb->file_private = cl;
  177. dev_dbg(&dev->pdev->dev, "add connect cb to control write list.\n");
  178. list_add_tail(&cb->cb_list,
  179. &dev->ctrl_wr_list.mei_cb.cb_list);
  180. }
  181. mutex_unlock(&dev->device_lock);
  182. err = wait_event_timeout(dev->wait_recvd_msg,
  183. (MEI_FILE_CONNECTED == cl->state ||
  184. MEI_FILE_DISCONNECTED == cl->state),
  185. timeout * HZ);
  186. mutex_lock(&dev->device_lock);
  187. if (MEI_FILE_CONNECTED == cl->state) {
  188. dev_dbg(&dev->pdev->dev, "successfully connected to FW client.\n");
  189. rets = cl->status;
  190. goto end;
  191. } else {
  192. dev_dbg(&dev->pdev->dev, "failed to connect to FW client.cl->state = %d.\n",
  193. cl->state);
  194. if (!err) {
  195. dev_dbg(&dev->pdev->dev,
  196. "wait_event_interruptible_timeout failed on client"
  197. " connect message fw response message.\n");
  198. }
  199. rets = -EFAULT;
  200. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  201. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  202. goto end;
  203. }
  204. rets = 0;
  205. end:
  206. dev_dbg(&dev->pdev->dev, "free connect cb memory.");
  207. kfree(cb);
  208. return rets;
  209. }
  210. /**
  211. * find_amthi_read_list_entry - finds a amthilist entry for current file
  212. *
  213. * @dev: the device structure
  214. * @file: pointer to file object
  215. *
  216. * returns returned a list entry on success, NULL on failure.
  217. */
  218. struct mei_cl_cb *find_amthi_read_list_entry(
  219. struct mei_device *dev,
  220. struct file *file)
  221. {
  222. struct mei_cl *cl_temp;
  223. struct mei_cl_cb *pos = NULL;
  224. struct mei_cl_cb *next = NULL;
  225. list_for_each_entry_safe(pos, next,
  226. &dev->amthi_read_complete_list.mei_cb.cb_list, cb_list) {
  227. cl_temp = (struct mei_cl *)pos->file_private;
  228. if (cl_temp && cl_temp == &dev->iamthif_cl &&
  229. pos->file_object == file)
  230. return pos;
  231. }
  232. return NULL;
  233. }
  234. /**
  235. * amthi_read - read data from AMTHI client
  236. *
  237. * @dev: the device structure
  238. * @if_num: minor number
  239. * @file: pointer to file object
  240. * @*ubuf: pointer to user data in user space
  241. * @length: data length to read
  242. * @offset: data read offset
  243. *
  244. * Locking: called under "dev->device_lock" lock
  245. *
  246. * returns
  247. * returned data length on success,
  248. * zero if no data to read,
  249. * negative on failure.
  250. */
  251. int amthi_read(struct mei_device *dev, struct file *file,
  252. char __user *ubuf, size_t length, loff_t *offset)
  253. {
  254. int rets;
  255. int wait_ret;
  256. struct mei_cl_cb *cb = NULL;
  257. struct mei_cl *cl = file->private_data;
  258. unsigned long timeout;
  259. int i;
  260. /* Only Posible if we are in timeout */
  261. if (!cl || cl != &dev->iamthif_cl) {
  262. dev_dbg(&dev->pdev->dev, "bad file ext.\n");
  263. return -ETIMEDOUT;
  264. }
  265. i = mei_me_cl_by_id(dev, dev->iamthif_cl.me_client_id);
  266. if (i < 0) {
  267. dev_dbg(&dev->pdev->dev, "amthi client not found.\n");
  268. return -ENODEV;
  269. }
  270. dev_dbg(&dev->pdev->dev, "checking amthi data\n");
  271. cb = find_amthi_read_list_entry(dev, file);
  272. /* Check for if we can block or not*/
  273. if (cb == NULL && file->f_flags & O_NONBLOCK)
  274. return -EAGAIN;
  275. dev_dbg(&dev->pdev->dev, "waiting for amthi data\n");
  276. while (cb == NULL) {
  277. /* unlock the Mutex */
  278. mutex_unlock(&dev->device_lock);
  279. wait_ret = wait_event_interruptible(dev->iamthif_cl.wait,
  280. (cb = find_amthi_read_list_entry(dev, file)));
  281. if (wait_ret)
  282. return -ERESTARTSYS;
  283. dev_dbg(&dev->pdev->dev, "woke up from sleep\n");
  284. /* Locking again the Mutex */
  285. mutex_lock(&dev->device_lock);
  286. }
  287. dev_dbg(&dev->pdev->dev, "Got amthi data\n");
  288. dev->iamthif_timer = 0;
  289. if (cb) {
  290. timeout = cb->read_time + msecs_to_jiffies(IAMTHIF_READ_TIMER);
  291. dev_dbg(&dev->pdev->dev, "amthi timeout = %lud\n",
  292. timeout);
  293. if (time_after(jiffies, timeout)) {
  294. dev_dbg(&dev->pdev->dev, "amthi Time out\n");
  295. /* 15 sec for the message has expired */
  296. list_del(&cb->cb_list);
  297. rets = -ETIMEDOUT;
  298. goto free;
  299. }
  300. }
  301. /* if the whole message will fit remove it from the list */
  302. if (cb->information >= *offset && length >= (cb->information - *offset))
  303. list_del(&cb->cb_list);
  304. else if (cb->information > 0 && cb->information <= *offset) {
  305. /* end of the message has been reached */
  306. list_del(&cb->cb_list);
  307. rets = 0;
  308. goto free;
  309. }
  310. /* else means that not full buffer will be read and do not
  311. * remove message from deletion list
  312. */
  313. dev_dbg(&dev->pdev->dev, "amthi cb->response_buffer size - %d\n",
  314. cb->response_buffer.size);
  315. dev_dbg(&dev->pdev->dev, "amthi cb->information - %lu\n",
  316. cb->information);
  317. /* length is being turncated to PAGE_SIZE, however,
  318. * the information may be longer */
  319. length = min_t(size_t, length, (cb->information - *offset));
  320. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length))
  321. rets = -EFAULT;
  322. else {
  323. rets = length;
  324. if ((*offset + length) < cb->information) {
  325. *offset += length;
  326. goto out;
  327. }
  328. }
  329. free:
  330. dev_dbg(&dev->pdev->dev, "free amthi cb memory.\n");
  331. *offset = 0;
  332. mei_free_cb_private(cb);
  333. out:
  334. return rets;
  335. }
  336. /**
  337. * mei_start_read - the start read client message function.
  338. *
  339. * @dev: the device structure
  340. * @if_num: minor number
  341. * @cl: private data of the file object
  342. *
  343. * returns 0 on success, <0 on failure.
  344. */
  345. int mei_start_read(struct mei_device *dev, struct mei_cl *cl)
  346. {
  347. struct mei_cl_cb *cb;
  348. int rets = 0;
  349. int i;
  350. if (cl->state != MEI_FILE_CONNECTED)
  351. return -ENODEV;
  352. if (dev->dev_state != MEI_DEV_ENABLED)
  353. return -ENODEV;
  354. dev_dbg(&dev->pdev->dev, "check if read is pending.\n");
  355. if (cl->read_pending || cl->read_cb) {
  356. dev_dbg(&dev->pdev->dev, "read is pending.\n");
  357. return -EBUSY;
  358. }
  359. cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
  360. if (!cb)
  361. return -ENOMEM;
  362. dev_dbg(&dev->pdev->dev, "allocation call back successful. host client = %d, ME client = %d\n",
  363. cl->host_client_id, cl->me_client_id);
  364. i = mei_me_cl_by_id(dev, cl->me_client_id);
  365. if (i < 0) {
  366. rets = -ENODEV;
  367. goto unlock;
  368. }
  369. cb->response_buffer.size = dev->me_clients[i].props.max_msg_length;
  370. cb->response_buffer.data =
  371. kmalloc(cb->response_buffer.size, GFP_KERNEL);
  372. if (!cb->response_buffer.data) {
  373. rets = -ENOMEM;
  374. goto unlock;
  375. }
  376. dev_dbg(&dev->pdev->dev, "allocation call back data success.\n");
  377. cb->major_file_operations = MEI_READ;
  378. /* make sure information is zero before we start */
  379. cb->information = 0;
  380. cb->file_private = (void *) cl;
  381. cl->read_cb = cb;
  382. if (dev->mei_host_buffer_is_empty) {
  383. dev->mei_host_buffer_is_empty = false;
  384. if (mei_send_flow_control(dev, cl)) {
  385. rets = -ENODEV;
  386. goto unlock;
  387. }
  388. list_add_tail(&cb->cb_list, &dev->read_list.mei_cb.cb_list);
  389. } else {
  390. list_add_tail(&cb->cb_list, &dev->ctrl_wr_list.mei_cb.cb_list);
  391. }
  392. return rets;
  393. unlock:
  394. mei_free_cb_private(cb);
  395. return rets;
  396. }
  397. /**
  398. * amthi_write - write iamthif data to amthi client
  399. *
  400. * @dev: the device structure
  401. * @cb: mei call back struct
  402. *
  403. * returns 0 on success, <0 on failure.
  404. */
  405. int amthi_write(struct mei_device *dev, struct mei_cl_cb *cb)
  406. {
  407. struct mei_msg_hdr mei_hdr;
  408. int ret;
  409. if (!dev || !cb)
  410. return -ENODEV;
  411. dev_dbg(&dev->pdev->dev, "write data to amthi client.\n");
  412. dev->iamthif_state = MEI_IAMTHIF_WRITING;
  413. dev->iamthif_current_cb = cb;
  414. dev->iamthif_file_object = cb->file_object;
  415. dev->iamthif_canceled = false;
  416. dev->iamthif_ioctl = true;
  417. dev->iamthif_msg_buf_size = cb->request_buffer.size;
  418. memcpy(dev->iamthif_msg_buf, cb->request_buffer.data,
  419. cb->request_buffer.size);
  420. ret = mei_flow_ctrl_creds(dev, &dev->iamthif_cl);
  421. if (ret < 0)
  422. return ret;
  423. if (ret && dev->mei_host_buffer_is_empty) {
  424. ret = 0;
  425. dev->mei_host_buffer_is_empty = false;
  426. if (cb->request_buffer.size > mei_hbuf_max_data(dev)) {
  427. mei_hdr.length = mei_hbuf_max_data(dev);
  428. mei_hdr.msg_complete = 0;
  429. } else {
  430. mei_hdr.length = cb->request_buffer.size;
  431. mei_hdr.msg_complete = 1;
  432. }
  433. mei_hdr.host_addr = dev->iamthif_cl.host_client_id;
  434. mei_hdr.me_addr = dev->iamthif_cl.me_client_id;
  435. mei_hdr.reserved = 0;
  436. dev->iamthif_msg_buf_index += mei_hdr.length;
  437. if (mei_write_message(dev, &mei_hdr,
  438. (unsigned char *)(dev->iamthif_msg_buf),
  439. mei_hdr.length))
  440. return -ENODEV;
  441. if (mei_hdr.msg_complete) {
  442. if (mei_flow_ctrl_reduce(dev, &dev->iamthif_cl))
  443. return -ENODEV;
  444. dev->iamthif_flow_control_pending = true;
  445. dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
  446. dev_dbg(&dev->pdev->dev, "add amthi cb to write waiting list\n");
  447. dev->iamthif_current_cb = cb;
  448. dev->iamthif_file_object = cb->file_object;
  449. list_add_tail(&cb->cb_list,
  450. &dev->write_waiting_list.mei_cb.cb_list);
  451. } else {
  452. dev_dbg(&dev->pdev->dev, "message does not complete, "
  453. "so add amthi cb to write list.\n");
  454. list_add_tail(&cb->cb_list,
  455. &dev->write_list.mei_cb.cb_list);
  456. }
  457. } else {
  458. if (!(dev->mei_host_buffer_is_empty))
  459. dev_dbg(&dev->pdev->dev, "host buffer is not empty");
  460. dev_dbg(&dev->pdev->dev, "No flow control credentials, "
  461. "so add iamthif cb to write list.\n");
  462. list_add_tail(&cb->cb_list, &dev->write_list.mei_cb.cb_list);
  463. }
  464. return 0;
  465. }
  466. /**
  467. * iamthif_ioctl_send_msg - send cmd data to amthi client
  468. *
  469. * @dev: the device structure
  470. *
  471. * returns 0 on success, <0 on failure.
  472. */
  473. void mei_run_next_iamthif_cmd(struct mei_device *dev)
  474. {
  475. struct mei_cl *cl_tmp;
  476. struct mei_cl_cb *pos = NULL;
  477. struct mei_cl_cb *next = NULL;
  478. int status;
  479. if (!dev)
  480. return;
  481. dev->iamthif_msg_buf_size = 0;
  482. dev->iamthif_msg_buf_index = 0;
  483. dev->iamthif_canceled = false;
  484. dev->iamthif_ioctl = true;
  485. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  486. dev->iamthif_timer = 0;
  487. dev->iamthif_file_object = NULL;
  488. dev_dbg(&dev->pdev->dev, "complete amthi cmd_list cb.\n");
  489. list_for_each_entry_safe(pos, next,
  490. &dev->amthi_cmd_list.mei_cb.cb_list, cb_list) {
  491. list_del(&pos->cb_list);
  492. cl_tmp = (struct mei_cl *)pos->file_private;
  493. if (cl_tmp && cl_tmp == &dev->iamthif_cl) {
  494. status = amthi_write(dev, pos);
  495. if (status) {
  496. dev_dbg(&dev->pdev->dev,
  497. "amthi write failed status = %d\n",
  498. status);
  499. return;
  500. }
  501. break;
  502. }
  503. }
  504. }
  505. /**
  506. * mei_free_cb_private - free mei_cb_private related memory
  507. *
  508. * @cb: mei callback struct
  509. */
  510. void mei_free_cb_private(struct mei_cl_cb *cb)
  511. {
  512. if (cb == NULL)
  513. return;
  514. kfree(cb->request_buffer.data);
  515. kfree(cb->response_buffer.data);
  516. kfree(cb);
  517. }