main.c 15 KB

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