main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/kernel.h>
  19. #include <linux/device.h>
  20. #include <linux/fs.h>
  21. #include <linux/errno.h>
  22. #include <linux/types.h>
  23. #include <linux/fcntl.h>
  24. #include <linux/aio.h>
  25. #include <linux/pci.h>
  26. #include <linux/poll.h>
  27. #include <linux/init.h>
  28. #include <linux/ioctl.h>
  29. #include <linux/cdev.h>
  30. #include <linux/sched.h>
  31. #include <linux/uuid.h>
  32. #include <linux/compat.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/miscdevice.h>
  36. #include <linux/mei.h>
  37. #include "mei_dev.h"
  38. #include "client.h"
  39. /**
  40. * mei_open - the open function
  41. *
  42. * @inode: pointer to inode structure
  43. * @file: pointer to file structure
  44. *
  45. * returns 0 on success, <0 on error
  46. */
  47. static int mei_open(struct inode *inode, struct file *file)
  48. {
  49. struct miscdevice *misc = file->private_data;
  50. struct pci_dev *pdev;
  51. struct mei_cl *cl;
  52. struct mei_device *dev;
  53. int err;
  54. if (!misc->parent)
  55. return -ENODEV;
  56. pdev = container_of(misc->parent, struct pci_dev, dev);
  57. dev = pci_get_drvdata(pdev);
  58. if (!dev)
  59. return -ENODEV;
  60. mutex_lock(&dev->device_lock);
  61. cl = NULL;
  62. err = -ENODEV;
  63. if (dev->dev_state != MEI_DEV_ENABLED) {
  64. dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
  65. mei_dev_state_str(dev->dev_state));
  66. goto err_unlock;
  67. }
  68. err = -ENOMEM;
  69. cl = mei_cl_allocate(dev);
  70. if (!cl)
  71. goto err_unlock;
  72. /* open_handle_count check is handled in the mei_cl_link */
  73. err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
  74. if (err)
  75. goto err_unlock;
  76. file->private_data = cl;
  77. mutex_unlock(&dev->device_lock);
  78. return nonseekable_open(inode, file);
  79. err_unlock:
  80. mutex_unlock(&dev->device_lock);
  81. kfree(cl);
  82. return err;
  83. }
  84. /**
  85. * mei_release - the release function
  86. *
  87. * @inode: pointer to inode structure
  88. * @file: pointer to file structure
  89. *
  90. * returns 0 on success, <0 on error
  91. */
  92. static int mei_release(struct inode *inode, struct file *file)
  93. {
  94. struct mei_cl *cl = file->private_data;
  95. struct mei_cl_cb *cb;
  96. struct mei_device *dev;
  97. int rets = 0;
  98. if (WARN_ON(!cl || !cl->dev))
  99. return -ENODEV;
  100. dev = cl->dev;
  101. mutex_lock(&dev->device_lock);
  102. if (cl == &dev->iamthif_cl) {
  103. rets = mei_amthif_release(dev, file);
  104. goto out;
  105. }
  106. if (cl->state == MEI_FILE_CONNECTED) {
  107. cl->state = MEI_FILE_DISCONNECTING;
  108. cl_dbg(dev, cl, "disconnecting\n");
  109. rets = mei_cl_disconnect(cl);
  110. }
  111. mei_cl_flush_queues(cl);
  112. cl_dbg(dev, cl, "removing\n");
  113. mei_cl_unlink(cl);
  114. /* free read cb */
  115. cb = NULL;
  116. if (cl->read_cb) {
  117. cb = mei_cl_find_read_cb(cl);
  118. /* Remove entry from read list */
  119. if (cb)
  120. list_del(&cb->list);
  121. cb = cl->read_cb;
  122. cl->read_cb = NULL;
  123. }
  124. file->private_data = NULL;
  125. mei_io_cb_free(cb);
  126. kfree(cl);
  127. out:
  128. mutex_unlock(&dev->device_lock);
  129. return rets;
  130. }
  131. /**
  132. * mei_read - the read function.
  133. *
  134. * @file: pointer to file structure
  135. * @ubuf: pointer to user buffer
  136. * @length: buffer length
  137. * @offset: data offset in buffer
  138. *
  139. * returns >=0 data length on success , <0 on error
  140. */
  141. static ssize_t mei_read(struct file *file, char __user *ubuf,
  142. size_t length, loff_t *offset)
  143. {
  144. struct mei_cl *cl = file->private_data;
  145. struct mei_cl_cb *cb_pos = NULL;
  146. struct mei_cl_cb *cb = NULL;
  147. struct mei_device *dev;
  148. int rets;
  149. int err;
  150. if (WARN_ON(!cl || !cl->dev))
  151. return -ENODEV;
  152. dev = cl->dev;
  153. mutex_lock(&dev->device_lock);
  154. if (dev->dev_state != MEI_DEV_ENABLED) {
  155. rets = -ENODEV;
  156. goto out;
  157. }
  158. if (length == 0) {
  159. rets = 0;
  160. goto out;
  161. }
  162. if (cl == &dev->iamthif_cl) {
  163. rets = mei_amthif_read(dev, file, ubuf, length, offset);
  164. goto out;
  165. }
  166. if (cl->read_cb) {
  167. cb = cl->read_cb;
  168. /* read what left */
  169. if (cb->buf_idx > *offset)
  170. goto copy_buffer;
  171. /* offset is beyond buf_idx we have no more data return 0 */
  172. if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
  173. rets = 0;
  174. goto free;
  175. }
  176. /* Offset needs to be cleaned for contiguous reads*/
  177. if (cb->buf_idx == 0 && *offset > 0)
  178. *offset = 0;
  179. } else if (*offset > 0) {
  180. *offset = 0;
  181. }
  182. err = mei_cl_read_start(cl, length);
  183. if (err && err != -EBUSY) {
  184. dev_dbg(&dev->pdev->dev,
  185. "mei start read failure with status = %d\n", err);
  186. rets = err;
  187. goto out;
  188. }
  189. if (MEI_READ_COMPLETE != cl->reading_state &&
  190. !waitqueue_active(&cl->rx_wait)) {
  191. if (file->f_flags & O_NONBLOCK) {
  192. rets = -EAGAIN;
  193. goto out;
  194. }
  195. mutex_unlock(&dev->device_lock);
  196. if (wait_event_interruptible(cl->rx_wait,
  197. MEI_READ_COMPLETE == cl->reading_state ||
  198. mei_cl_is_transitioning(cl))) {
  199. if (signal_pending(current))
  200. return -EINTR;
  201. return -ERESTARTSYS;
  202. }
  203. mutex_lock(&dev->device_lock);
  204. if (mei_cl_is_transitioning(cl)) {
  205. rets = -EBUSY;
  206. goto out;
  207. }
  208. }
  209. cb = cl->read_cb;
  210. if (!cb) {
  211. rets = -ENODEV;
  212. goto out;
  213. }
  214. if (cl->reading_state != MEI_READ_COMPLETE) {
  215. rets = 0;
  216. goto out;
  217. }
  218. /* now copy the data to user space */
  219. copy_buffer:
  220. dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n",
  221. cb->response_buffer.size, cb->buf_idx);
  222. if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
  223. rets = -EMSGSIZE;
  224. goto free;
  225. }
  226. /* length is being truncated to PAGE_SIZE,
  227. * however buf_idx may point beyond that */
  228. length = min_t(size_t, length, cb->buf_idx - *offset);
  229. if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
  230. dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
  231. rets = -EFAULT;
  232. goto free;
  233. }
  234. rets = length;
  235. *offset += length;
  236. if ((unsigned long)*offset < cb->buf_idx)
  237. goto out;
  238. free:
  239. cb_pos = mei_cl_find_read_cb(cl);
  240. /* Remove entry from read list */
  241. if (cb_pos)
  242. list_del(&cb_pos->list);
  243. mei_io_cb_free(cb);
  244. cl->reading_state = MEI_IDLE;
  245. cl->read_cb = NULL;
  246. out:
  247. dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
  248. mutex_unlock(&dev->device_lock);
  249. return rets;
  250. }
  251. /**
  252. * mei_write - the write function.
  253. *
  254. * @file: pointer to file structure
  255. * @ubuf: pointer to user buffer
  256. * @length: buffer length
  257. * @offset: data offset in buffer
  258. *
  259. * returns >=0 data length on success , <0 on error
  260. */
  261. static ssize_t mei_write(struct file *file, const char __user *ubuf,
  262. size_t length, loff_t *offset)
  263. {
  264. struct mei_cl *cl = file->private_data;
  265. struct mei_cl_cb *write_cb = NULL;
  266. struct mei_device *dev;
  267. unsigned long timeout = 0;
  268. int rets;
  269. int id;
  270. if (WARN_ON(!cl || !cl->dev))
  271. return -ENODEV;
  272. dev = cl->dev;
  273. mutex_lock(&dev->device_lock);
  274. if (dev->dev_state != MEI_DEV_ENABLED) {
  275. rets = -ENODEV;
  276. goto out;
  277. }
  278. id = mei_me_cl_by_id(dev, cl->me_client_id);
  279. if (id < 0) {
  280. rets = -ENOTTY;
  281. goto out;
  282. }
  283. if (length == 0) {
  284. rets = 0;
  285. goto out;
  286. }
  287. if (length > dev->me_clients[id].props.max_msg_length) {
  288. rets = -EFBIG;
  289. goto out;
  290. }
  291. if (cl->state != MEI_FILE_CONNECTED) {
  292. dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
  293. cl->host_client_id, cl->me_client_id);
  294. rets = -ENODEV;
  295. goto out;
  296. }
  297. if (cl == &dev->iamthif_cl) {
  298. write_cb = mei_amthif_find_read_list_entry(dev, file);
  299. if (write_cb) {
  300. timeout = write_cb->read_time +
  301. mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
  302. if (time_after(jiffies, timeout) ||
  303. cl->reading_state == MEI_READ_COMPLETE) {
  304. *offset = 0;
  305. list_del(&write_cb->list);
  306. mei_io_cb_free(write_cb);
  307. write_cb = NULL;
  308. }
  309. }
  310. }
  311. /* free entry used in read */
  312. if (cl->reading_state == MEI_READ_COMPLETE) {
  313. *offset = 0;
  314. write_cb = mei_cl_find_read_cb(cl);
  315. if (write_cb) {
  316. list_del(&write_cb->list);
  317. mei_io_cb_free(write_cb);
  318. write_cb = NULL;
  319. cl->reading_state = MEI_IDLE;
  320. cl->read_cb = NULL;
  321. }
  322. } else if (cl->reading_state == MEI_IDLE)
  323. *offset = 0;
  324. write_cb = mei_io_cb_init(cl, file);
  325. if (!write_cb) {
  326. dev_err(&dev->pdev->dev, "write cb allocation failed\n");
  327. rets = -ENOMEM;
  328. goto out;
  329. }
  330. rets = mei_io_cb_alloc_req_buf(write_cb, length);
  331. if (rets)
  332. goto out;
  333. rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
  334. if (rets) {
  335. dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
  336. rets = -EFAULT;
  337. goto out;
  338. }
  339. if (cl == &dev->iamthif_cl) {
  340. rets = mei_amthif_write(dev, write_cb);
  341. if (rets) {
  342. dev_err(&dev->pdev->dev,
  343. "amthif write failed with status = %d\n", rets);
  344. goto out;
  345. }
  346. mutex_unlock(&dev->device_lock);
  347. return length;
  348. }
  349. rets = mei_cl_write(cl, write_cb, false);
  350. out:
  351. mutex_unlock(&dev->device_lock);
  352. if (rets < 0)
  353. mei_io_cb_free(write_cb);
  354. return rets;
  355. }
  356. /**
  357. * mei_ioctl_connect_client - the connect to fw client IOCTL function
  358. *
  359. * @dev: the device structure
  360. * @data: IOCTL connect data, input and output parameters
  361. * @file: private data of the file object
  362. *
  363. * Locking: called under "dev->device_lock" lock
  364. *
  365. * returns 0 on success, <0 on failure.
  366. */
  367. static int mei_ioctl_connect_client(struct file *file,
  368. struct mei_connect_client_data *data)
  369. {
  370. struct mei_device *dev;
  371. struct mei_client *client;
  372. struct mei_cl *cl;
  373. int i;
  374. int rets;
  375. cl = file->private_data;
  376. if (WARN_ON(!cl || !cl->dev))
  377. return -ENODEV;
  378. dev = cl->dev;
  379. if (dev->dev_state != MEI_DEV_ENABLED) {
  380. rets = -ENODEV;
  381. goto end;
  382. }
  383. if (cl->state != MEI_FILE_INITIALIZING &&
  384. cl->state != MEI_FILE_DISCONNECTED) {
  385. rets = -EBUSY;
  386. goto end;
  387. }
  388. /* find ME client we're trying to connect to */
  389. i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
  390. if (i < 0 || dev->me_clients[i].props.fixed_address) {
  391. dev_dbg(&dev->pdev->dev, "Cannot connect to FW Client UUID = %pUl\n",
  392. &data->in_client_uuid);
  393. rets = -ENOTTY;
  394. goto end;
  395. }
  396. cl->me_client_id = dev->me_clients[i].client_id;
  397. dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
  398. cl->me_client_id);
  399. dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
  400. dev->me_clients[i].props.protocol_version);
  401. dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
  402. dev->me_clients[i].props.max_msg_length);
  403. /* if we're connecting to amthif client then we will use the
  404. * existing connection
  405. */
  406. if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
  407. dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
  408. if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
  409. rets = -ENODEV;
  410. goto end;
  411. }
  412. mei_cl_unlink(cl);
  413. kfree(cl);
  414. cl = NULL;
  415. dev->iamthif_open_count++;
  416. file->private_data = &dev->iamthif_cl;
  417. client = &data->out_client_properties;
  418. client->max_msg_length =
  419. dev->me_clients[i].props.max_msg_length;
  420. client->protocol_version =
  421. dev->me_clients[i].props.protocol_version;
  422. rets = dev->iamthif_cl.status;
  423. goto end;
  424. }
  425. /* prepare the output buffer */
  426. client = &data->out_client_properties;
  427. client->max_msg_length = dev->me_clients[i].props.max_msg_length;
  428. client->protocol_version = dev->me_clients[i].props.protocol_version;
  429. dev_dbg(&dev->pdev->dev, "Can connect?\n");
  430. rets = mei_cl_connect(cl, file);
  431. end:
  432. return rets;
  433. }
  434. /**
  435. * mei_ioctl - the IOCTL function
  436. *
  437. * @file: pointer to file structure
  438. * @cmd: ioctl command
  439. * @data: pointer to mei message structure
  440. *
  441. * returns 0 on success , <0 on error
  442. */
  443. static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
  444. {
  445. struct mei_device *dev;
  446. struct mei_cl *cl = file->private_data;
  447. struct mei_connect_client_data *connect_data = NULL;
  448. int rets;
  449. if (cmd != IOCTL_MEI_CONNECT_CLIENT)
  450. return -EINVAL;
  451. if (WARN_ON(!cl || !cl->dev))
  452. return -ENODEV;
  453. dev = cl->dev;
  454. dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
  455. mutex_lock(&dev->device_lock);
  456. if (dev->dev_state != MEI_DEV_ENABLED) {
  457. rets = -ENODEV;
  458. goto out;
  459. }
  460. dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
  461. connect_data = kzalloc(sizeof(struct mei_connect_client_data),
  462. GFP_KERNEL);
  463. if (!connect_data) {
  464. rets = -ENOMEM;
  465. goto out;
  466. }
  467. dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
  468. if (copy_from_user(connect_data, (char __user *)data,
  469. sizeof(struct mei_connect_client_data))) {
  470. dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
  471. rets = -EFAULT;
  472. goto out;
  473. }
  474. rets = mei_ioctl_connect_client(file, connect_data);
  475. /* if all is ok, copying the data back to user. */
  476. if (rets)
  477. goto out;
  478. dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
  479. if (copy_to_user((char __user *)data, connect_data,
  480. sizeof(struct mei_connect_client_data))) {
  481. dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
  482. rets = -EFAULT;
  483. goto out;
  484. }
  485. out:
  486. kfree(connect_data);
  487. mutex_unlock(&dev->device_lock);
  488. return rets;
  489. }
  490. /**
  491. * mei_compat_ioctl - the compat IOCTL function
  492. *
  493. * @file: pointer to file structure
  494. * @cmd: ioctl command
  495. * @data: pointer to mei message structure
  496. *
  497. * returns 0 on success , <0 on error
  498. */
  499. #ifdef CONFIG_COMPAT
  500. static long mei_compat_ioctl(struct file *file,
  501. unsigned int cmd, unsigned long data)
  502. {
  503. return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
  504. }
  505. #endif
  506. /**
  507. * mei_poll - the poll function
  508. *
  509. * @file: pointer to file structure
  510. * @wait: pointer to poll_table structure
  511. *
  512. * returns poll mask
  513. */
  514. static unsigned int mei_poll(struct file *file, poll_table *wait)
  515. {
  516. struct mei_cl *cl = file->private_data;
  517. struct mei_device *dev;
  518. unsigned int mask = 0;
  519. if (WARN_ON(!cl || !cl->dev))
  520. return POLLERR;
  521. dev = cl->dev;
  522. mutex_lock(&dev->device_lock);
  523. if (!mei_cl_is_connected(cl)) {
  524. mask = POLLERR;
  525. goto out;
  526. }
  527. mutex_unlock(&dev->device_lock);
  528. if (cl == &dev->iamthif_cl)
  529. return mei_amthif_poll(dev, file, wait);
  530. poll_wait(file, &cl->tx_wait, wait);
  531. mutex_lock(&dev->device_lock);
  532. if (!mei_cl_is_connected(cl)) {
  533. mask = POLLERR;
  534. goto out;
  535. }
  536. mask |= (POLLIN | POLLRDNORM);
  537. out:
  538. mutex_unlock(&dev->device_lock);
  539. return mask;
  540. }
  541. /*
  542. * file operations structure will be used for mei char device.
  543. */
  544. static const struct file_operations mei_fops = {
  545. .owner = THIS_MODULE,
  546. .read = mei_read,
  547. .unlocked_ioctl = mei_ioctl,
  548. #ifdef CONFIG_COMPAT
  549. .compat_ioctl = mei_compat_ioctl,
  550. #endif
  551. .open = mei_open,
  552. .release = mei_release,
  553. .write = mei_write,
  554. .poll = mei_poll,
  555. .llseek = no_llseek
  556. };
  557. /*
  558. * Misc Device Struct
  559. */
  560. static struct miscdevice mei_misc_device = {
  561. .name = "mei",
  562. .fops = &mei_fops,
  563. .minor = MISC_DYNAMIC_MINOR,
  564. };
  565. int mei_register(struct mei_device *dev)
  566. {
  567. int ret;
  568. mei_misc_device.parent = &dev->pdev->dev;
  569. ret = misc_register(&mei_misc_device);
  570. if (ret)
  571. return ret;
  572. if (mei_dbgfs_register(dev, mei_misc_device.name))
  573. dev_err(&dev->pdev->dev, "cannot register debugfs\n");
  574. return 0;
  575. }
  576. EXPORT_SYMBOL_GPL(mei_register);
  577. void mei_deregister(struct mei_device *dev)
  578. {
  579. mei_dbgfs_deregister(dev);
  580. misc_deregister(&mei_misc_device);
  581. mei_misc_device.parent = NULL;
  582. }
  583. EXPORT_SYMBOL_GPL(mei_deregister);
  584. static int __init mei_init(void)
  585. {
  586. return mei_cl_bus_init();
  587. }
  588. static void __exit mei_exit(void)
  589. {
  590. mei_cl_bus_exit();
  591. }
  592. module_init(mei_init);
  593. module_exit(mei_exit);
  594. MODULE_AUTHOR("Intel Corporation");
  595. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  596. MODULE_LICENSE("GPL v2");