3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/kernel.h>
19 #include <linux/device.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>
36 #include <linux/mei.h>
42 * mei_open - the open function
44 * @inode: pointer to inode structure
45 * @file: pointer to file structure
47 * returns 0 on success, <0 on error
49 static int mei_open(struct inode *inode, struct file *file)
51 struct mei_device *dev;
56 dev = container_of(inode->i_cdev, struct mei_device, cdev);
60 mutex_lock(&dev->device_lock);
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));
72 cl = mei_cl_allocate(dev);
76 /* open_handle_count check is handled in the mei_cl_link */
77 err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
81 file->private_data = cl;
83 mutex_unlock(&dev->device_lock);
85 return nonseekable_open(inode, file);
88 mutex_unlock(&dev->device_lock);
94 * mei_release - the release function
96 * @inode: pointer to inode structure
97 * @file: pointer to file structure
99 * returns 0 on success, <0 on error
101 static int mei_release(struct inode *inode, struct file *file)
103 struct mei_cl *cl = file->private_data;
104 struct mei_cl_cb *cb;
105 struct mei_device *dev;
108 if (WARN_ON(!cl || !cl->dev))
113 mutex_lock(&dev->device_lock);
114 if (cl == &dev->iamthif_cl) {
115 rets = mei_amthif_release(dev, file);
118 if (cl->state == MEI_FILE_CONNECTED) {
119 cl->state = MEI_FILE_DISCONNECTING;
120 cl_dbg(dev, cl, "disconnecting\n");
121 rets = mei_cl_disconnect(cl);
123 mei_cl_flush_queues(cl);
124 cl_dbg(dev, cl, "removing\n");
132 cb = mei_cl_find_read_cb(cl);
133 /* Remove entry from read list */
141 file->private_data = NULL;
147 mutex_unlock(&dev->device_lock);
153 * mei_read - the read function.
155 * @file: pointer to file structure
156 * @ubuf: pointer to user buffer
157 * @length: buffer length
158 * @offset: data offset in buffer
160 * returns >=0 data length on success , <0 on error
162 static ssize_t mei_read(struct file *file, char __user *ubuf,
163 size_t length, loff_t *offset)
165 struct mei_cl *cl = file->private_data;
166 struct mei_cl_cb *cb_pos = NULL;
167 struct mei_cl_cb *cb = NULL;
168 struct mei_device *dev;
173 if (WARN_ON(!cl || !cl->dev))
179 mutex_lock(&dev->device_lock);
180 if (dev->dev_state != MEI_DEV_ENABLED) {
190 if (cl == &dev->iamthif_cl) {
191 rets = mei_amthif_read(dev, file, ubuf, length, offset);
198 if (cb->buf_idx > *offset)
200 /* offset is beyond buf_idx we have no more data return 0 */
201 if (cb->buf_idx > 0 && cb->buf_idx <= *offset) {
205 /* Offset needs to be cleaned for contiguous reads*/
206 if (cb->buf_idx == 0 && *offset > 0)
208 } else if (*offset > 0) {
212 err = mei_cl_read_start(cl, length);
213 if (err && err != -EBUSY) {
214 dev_dbg(&dev->pdev->dev,
215 "mei start read failure with status = %d\n", err);
220 if (MEI_READ_COMPLETE != cl->reading_state &&
221 !waitqueue_active(&cl->rx_wait)) {
222 if (file->f_flags & O_NONBLOCK) {
227 mutex_unlock(&dev->device_lock);
229 if (wait_event_interruptible(cl->rx_wait,
230 MEI_READ_COMPLETE == cl->reading_state ||
231 mei_cl_is_transitioning(cl))) {
233 if (signal_pending(current))
238 mutex_lock(&dev->device_lock);
239 if (mei_cl_is_transitioning(cl)) {
251 if (cl->reading_state != MEI_READ_COMPLETE) {
255 /* now copy the data to user space */
257 dev_dbg(&dev->pdev->dev, "buf.size = %d buf.idx= %ld\n",
258 cb->response_buffer.size, cb->buf_idx);
259 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
264 /* length is being truncated to PAGE_SIZE,
265 * however buf_idx may point beyond that */
266 length = min_t(size_t, length, cb->buf_idx - *offset);
268 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
269 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
276 if ((unsigned long)*offset < cb->buf_idx)
280 cb_pos = mei_cl_find_read_cb(cl);
281 /* Remove entry from read list */
283 list_del(&cb_pos->list);
285 cl->reading_state = MEI_IDLE;
288 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
289 mutex_unlock(&dev->device_lock);
293 * mei_write - the write function.
295 * @file: pointer to file structure
296 * @ubuf: pointer to user buffer
297 * @length: buffer length
298 * @offset: data offset in buffer
300 * returns >=0 data length on success , <0 on error
302 static ssize_t mei_write(struct file *file, const char __user *ubuf,
303 size_t length, loff_t *offset)
305 struct mei_cl *cl = file->private_data;
306 struct mei_cl_cb *write_cb = NULL;
307 struct mei_device *dev;
308 unsigned long timeout = 0;
312 if (WARN_ON(!cl || !cl->dev))
317 mutex_lock(&dev->device_lock);
319 if (dev->dev_state != MEI_DEV_ENABLED) {
324 id = mei_me_cl_by_id(dev, cl->me_client_id);
335 if (length > dev->me_clients[id].props.max_msg_length) {
340 if (cl->state != MEI_FILE_CONNECTED) {
341 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
342 cl->host_client_id, cl->me_client_id);
346 if (cl == &dev->iamthif_cl) {
347 write_cb = mei_amthif_find_read_list_entry(dev, file);
350 timeout = write_cb->read_time +
351 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
353 if (time_after(jiffies, timeout) ||
354 cl->reading_state == MEI_READ_COMPLETE) {
356 list_del(&write_cb->list);
357 mei_io_cb_free(write_cb);
363 /* free entry used in read */
364 if (cl->reading_state == MEI_READ_COMPLETE) {
366 write_cb = mei_cl_find_read_cb(cl);
368 list_del(&write_cb->list);
369 mei_io_cb_free(write_cb);
371 cl->reading_state = MEI_IDLE;
374 } else if (cl->reading_state == MEI_IDLE)
378 write_cb = mei_io_cb_init(cl, file);
380 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
384 rets = mei_io_cb_alloc_req_buf(write_cb, length);
388 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
390 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
395 if (cl == &dev->iamthif_cl) {
396 rets = mei_amthif_write(dev, write_cb);
399 dev_err(&dev->pdev->dev,
400 "amthif write failed with status = %d\n", rets);
403 mutex_unlock(&dev->device_lock);
407 rets = mei_cl_write(cl, write_cb, false);
409 mutex_unlock(&dev->device_lock);
411 mei_io_cb_free(write_cb);
416 * mei_ioctl_connect_client - the connect to fw client IOCTL function
418 * @dev: the device structure
419 * @data: IOCTL connect data, input and output parameters
420 * @file: private data of the file object
422 * Locking: called under "dev->device_lock" lock
424 * returns 0 on success, <0 on failure.
426 static int mei_ioctl_connect_client(struct file *file,
427 struct mei_connect_client_data *data)
429 struct mei_device *dev;
430 struct mei_client *client;
435 cl = file->private_data;
436 if (WARN_ON(!cl || !cl->dev))
441 if (dev->dev_state != MEI_DEV_ENABLED) {
446 if (cl->state != MEI_FILE_INITIALIZING &&
447 cl->state != MEI_FILE_DISCONNECTED) {
452 /* find ME client we're trying to connect to */
453 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
454 if (i < 0 || dev->me_clients[i].props.fixed_address) {
455 dev_dbg(&dev->pdev->dev, "Cannot connect to FW Client UUID = %pUl\n",
456 &data->in_client_uuid);
461 cl->me_client_id = dev->me_clients[i].client_id;
463 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
465 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
466 dev->me_clients[i].props.protocol_version);
467 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
468 dev->me_clients[i].props.max_msg_length);
470 /* if we're connecting to amthif client then we will use the
471 * existing connection
473 if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
474 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
475 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
483 dev->iamthif_open_count++;
484 file->private_data = &dev->iamthif_cl;
486 client = &data->out_client_properties;
487 client->max_msg_length =
488 dev->me_clients[i].props.max_msg_length;
489 client->protocol_version =
490 dev->me_clients[i].props.protocol_version;
491 rets = dev->iamthif_cl.status;
497 /* prepare the output buffer */
498 client = &data->out_client_properties;
499 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
500 client->protocol_version = dev->me_clients[i].props.protocol_version;
501 dev_dbg(&dev->pdev->dev, "Can connect?\n");
504 rets = mei_cl_connect(cl, file);
512 * mei_ioctl - the IOCTL function
514 * @file: pointer to file structure
515 * @cmd: ioctl command
516 * @data: pointer to mei message structure
518 * returns 0 on success , <0 on error
520 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
522 struct mei_device *dev;
523 struct mei_cl *cl = file->private_data;
524 struct mei_connect_client_data *connect_data = NULL;
527 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
530 if (WARN_ON(!cl || !cl->dev))
535 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
537 mutex_lock(&dev->device_lock);
538 if (dev->dev_state != MEI_DEV_ENABLED) {
543 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
545 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
551 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
552 if (copy_from_user(connect_data, (char __user *)data,
553 sizeof(struct mei_connect_client_data))) {
554 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
559 rets = mei_ioctl_connect_client(file, connect_data);
561 /* if all is ok, copying the data back to user. */
565 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
566 if (copy_to_user((char __user *)data, connect_data,
567 sizeof(struct mei_connect_client_data))) {
568 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
575 mutex_unlock(&dev->device_lock);
580 * mei_compat_ioctl - the compat IOCTL function
582 * @file: pointer to file structure
583 * @cmd: ioctl command
584 * @data: pointer to mei message structure
586 * returns 0 on success , <0 on error
589 static long mei_compat_ioctl(struct file *file,
590 unsigned int cmd, unsigned long data)
592 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
598 * mei_poll - the poll function
600 * @file: pointer to file structure
601 * @wait: pointer to poll_table structure
605 static unsigned int mei_poll(struct file *file, poll_table *wait)
607 struct mei_cl *cl = file->private_data;
608 struct mei_device *dev;
609 unsigned int mask = 0;
611 if (WARN_ON(!cl || !cl->dev))
616 mutex_lock(&dev->device_lock);
618 if (!mei_cl_is_connected(cl)) {
623 mutex_unlock(&dev->device_lock);
626 if (cl == &dev->iamthif_cl)
627 return mei_amthif_poll(dev, file, wait);
629 poll_wait(file, &cl->tx_wait, wait);
631 mutex_lock(&dev->device_lock);
633 if (!mei_cl_is_connected(cl)) {
638 mask |= (POLLIN | POLLRDNORM);
641 mutex_unlock(&dev->device_lock);
646 * file operations structure will be used for mei char device.
648 static const struct file_operations mei_fops = {
649 .owner = THIS_MODULE,
651 .unlocked_ioctl = mei_ioctl,
653 .compat_ioctl = mei_compat_ioctl,
656 .release = mei_release,
662 static struct class *mei_class;
663 static dev_t mei_devt;
664 #define MEI_MAX_DEVS MINORMASK
665 static DEFINE_MUTEX(mei_minor_lock);
666 static DEFINE_IDR(mei_idr);
669 * mei_minor_get - obtain next free device minor number
671 * @dev: device pointer
673 * returns allocated minor, or -ENOSPC if no free minor left
675 static int mei_minor_get(struct mei_device *dev)
679 mutex_lock(&mei_minor_lock);
680 ret = idr_alloc(&mei_idr, dev, 0, MEI_MAX_DEVS, GFP_KERNEL);
683 else if (ret == -ENOSPC)
684 dev_err(&dev->pdev->dev, "too many mei devices\n");
686 mutex_unlock(&mei_minor_lock);
691 * mei_minor_free - mark device minor number as free
693 * @dev: device pointer
695 static void mei_minor_free(struct mei_device *dev)
697 mutex_lock(&mei_minor_lock);
698 idr_remove(&mei_idr, dev->minor);
699 mutex_unlock(&mei_minor_lock);
702 int mei_register(struct mei_device *dev, struct device *parent)
704 struct device *clsdev; /* class device */
707 ret = mei_minor_get(dev);
711 /* Fill in the data structures */
712 devno = MKDEV(MAJOR(mei_devt), dev->minor);
713 cdev_init(&dev->cdev, &mei_fops);
714 dev->cdev.owner = mei_fops.owner;
717 ret = cdev_add(&dev->cdev, devno, 1);
719 dev_err(parent, "unable to add device %d:%d\n",
720 MAJOR(mei_devt), dev->minor);
724 clsdev = device_create(mei_class, parent, devno,
725 NULL, "mei%d", dev->minor);
727 if (IS_ERR(clsdev)) {
728 dev_err(parent, "unable to create device %d:%d\n",
729 MAJOR(mei_devt), dev->minor);
730 ret = PTR_ERR(clsdev);
734 ret = mei_dbgfs_register(dev, dev_name(clsdev));
736 dev_err(clsdev, "cannot register debugfs ret = %d\n", ret);
743 device_destroy(mei_class, devno);
745 cdev_del(&dev->cdev);
750 EXPORT_SYMBOL_GPL(mei_register);
752 void mei_deregister(struct mei_device *dev)
756 devno = dev->cdev.dev;
757 cdev_del(&dev->cdev);
759 mei_dbgfs_deregister(dev);
761 device_destroy(mei_class, devno);
765 EXPORT_SYMBOL_GPL(mei_deregister);
767 static int __init mei_init(void)
771 mei_class = class_create(THIS_MODULE, "mei");
772 if (IS_ERR(mei_class)) {
773 pr_err("couldn't create class\n");
774 ret = PTR_ERR(mei_class);
778 ret = alloc_chrdev_region(&mei_devt, 0, MEI_MAX_DEVS, "mei");
780 pr_err("unable to allocate char dev region\n");
784 ret = mei_cl_bus_init();
786 pr_err("unable to initialize bus\n");
793 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
795 class_destroy(mei_class);
800 static void __exit mei_exit(void)
802 unregister_chrdev_region(mei_devt, MEI_MAX_DEVS);
803 class_destroy(mei_class);
807 module_init(mei_init);
808 module_exit(mei_exit);
810 MODULE_AUTHOR("Intel Corporation");
811 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
812 MODULE_LICENSE("GPL v2");