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>
20 #include <linux/slab.h>
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/fcntl.h>
25 #include <linux/poll.h>
26 #include <linux/init.h>
27 #include <linux/ioctl.h>
28 #include <linux/cdev.h>
29 #include <linux/sched.h>
30 #include <linux/uuid.h>
31 #include <linux/compat.h>
32 #include <linux/jiffies.h>
33 #include <linux/interrupt.h>
35 #include <linux/mei.h>
41 * mei_open - the open function
43 * @inode: pointer to inode structure
44 * @file: pointer to file structure
46 * Return: 0 on success, <0 on error
48 static int mei_open(struct inode
*inode
, struct file
*file
)
50 struct mei_device
*dev
;
55 dev
= container_of(inode
->i_cdev
, struct mei_device
, cdev
);
59 mutex_lock(&dev
->device_lock
);
61 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
62 dev_dbg(dev
->dev
, "dev_state != MEI_ENABLED dev_state = %s\n",
63 mei_dev_state_str(dev
->dev_state
));
68 cl
= mei_cl_alloc_linked(dev
, MEI_HOST_CLIENT_ID_ANY
);
74 file
->private_data
= cl
;
76 mutex_unlock(&dev
->device_lock
);
78 return nonseekable_open(inode
, file
);
81 mutex_unlock(&dev
->device_lock
);
86 * mei_release - the release function
88 * @inode: pointer to inode structure
89 * @file: pointer to file structure
91 * Return: 0 on success, <0 on error
93 static int mei_release(struct inode
*inode
, struct file
*file
)
95 struct mei_cl
*cl
= file
->private_data
;
96 struct mei_device
*dev
;
99 if (WARN_ON(!cl
|| !cl
->dev
))
104 mutex_lock(&dev
->device_lock
);
105 if (cl
== &dev
->iamthif_cl
) {
106 rets
= mei_amthif_release(dev
, file
);
109 rets
= mei_cl_disconnect(cl
);
111 mei_cl_flush_queues(cl
, file
);
112 cl_dbg(dev
, cl
, "removing\n");
116 file
->private_data
= NULL
;
120 mutex_unlock(&dev
->device_lock
);
126 * mei_read - the read function.
128 * @file: pointer to file structure
129 * @ubuf: pointer to user buffer
130 * @length: buffer length
131 * @offset: data offset in buffer
133 * Return: >=0 data length on success , <0 on error
135 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
136 size_t length
, loff_t
*offset
)
138 struct mei_cl
*cl
= file
->private_data
;
139 struct mei_device
*dev
;
140 struct mei_cl_cb
*cb
= NULL
;
145 if (WARN_ON(!cl
|| !cl
->dev
))
151 mutex_lock(&dev
->device_lock
);
152 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
162 if (cl
== &dev
->iamthif_cl
) {
163 rets
= mei_amthif_read(dev
, file
, ubuf
, length
, offset
);
167 cb
= mei_cl_read_cb(cl
, file
);
170 if (cb
->buf_idx
> *offset
)
172 /* offset is beyond buf_idx we have no more data return 0 */
173 if (cb
->buf_idx
> 0 && cb
->buf_idx
<= *offset
) {
177 /* Offset needs to be cleaned for contiguous reads*/
178 if (cb
->buf_idx
== 0 && *offset
> 0)
180 } else if (*offset
> 0) {
184 err
= mei_cl_read_start(cl
, length
, file
);
185 if (err
&& err
!= -EBUSY
) {
187 "mei start read failure with status = %d\n", err
);
192 if (list_empty(&cl
->rd_completed
) && !waitqueue_active(&cl
->rx_wait
)) {
193 if (file
->f_flags
& O_NONBLOCK
) {
198 mutex_unlock(&dev
->device_lock
);
200 if (wait_event_interruptible(cl
->rx_wait
,
201 (!list_empty(&cl
->rd_completed
)) ||
202 (!mei_cl_is_connected(cl
)))) {
204 if (signal_pending(current
))
209 mutex_lock(&dev
->device_lock
);
210 if (!mei_cl_is_connected(cl
)) {
216 cb
= mei_cl_read_cb(cl
, file
);
218 if (mei_cl_is_fixed_address(cl
) && dev
->allow_fixed_address
) {
219 cb
= mei_cl_read_cb(cl
, NULL
);
228 /* now copy the data to user space */
231 dev_dbg(dev
->dev
, "read operation failed %d\n", rets
);
235 dev_dbg(dev
->dev
, "buf.size = %d buf.idx= %ld\n",
236 cb
->buf
.size
, cb
->buf_idx
);
237 if (length
== 0 || ubuf
== NULL
|| *offset
> cb
->buf_idx
) {
242 /* length is being truncated to PAGE_SIZE,
243 * however buf_idx may point beyond that */
244 length
= min_t(size_t, length
, cb
->buf_idx
- *offset
);
246 if (copy_to_user(ubuf
, cb
->buf
.data
+ *offset
, length
)) {
247 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
254 if ((unsigned long)*offset
< cb
->buf_idx
)
261 dev_dbg(dev
->dev
, "end mei read rets= %d\n", rets
);
262 mutex_unlock(&dev
->device_lock
);
266 * mei_write - the write function.
268 * @file: pointer to file structure
269 * @ubuf: pointer to user buffer
270 * @length: buffer length
271 * @offset: data offset in buffer
273 * Return: >=0 data length on success , <0 on error
275 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
276 size_t length
, loff_t
*offset
)
278 struct mei_cl
*cl
= file
->private_data
;
279 struct mei_cl_cb
*write_cb
= NULL
;
280 struct mei_device
*dev
;
281 unsigned long timeout
= 0;
284 if (WARN_ON(!cl
|| !cl
->dev
))
289 mutex_lock(&dev
->device_lock
);
291 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
296 if (!mei_cl_is_connected(cl
)) {
297 cl_err(dev
, cl
, "is not connected");
302 if (!mei_me_cl_is_active(cl
->me_cl
)) {
307 if (length
> mei_cl_mtu(cl
)) {
317 if (cl
== &dev
->iamthif_cl
) {
318 write_cb
= mei_amthif_find_read_list_entry(dev
, file
);
321 timeout
= write_cb
->read_time
+
322 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER
);
324 if (time_after(jiffies
, timeout
)) {
326 mei_io_cb_free(write_cb
);
333 write_cb
= mei_cl_alloc_cb(cl
, length
, MEI_FOP_WRITE
, file
);
339 rets
= copy_from_user(write_cb
->buf
.data
, ubuf
, length
);
341 dev_dbg(dev
->dev
, "failed to copy data from userland\n");
346 if (cl
== &dev
->iamthif_cl
) {
347 rets
= mei_amthif_write(cl
, write_cb
);
351 "amthif write failed with status = %d\n", rets
);
354 mutex_unlock(&dev
->device_lock
);
358 rets
= mei_cl_write(cl
, write_cb
, false);
360 mutex_unlock(&dev
->device_lock
);
362 mei_io_cb_free(write_cb
);
367 * mei_ioctl_connect_client - the connect to fw client IOCTL function
369 * @file: private data of the file object
370 * @data: IOCTL connect data, input and output parameters
372 * Locking: called under "dev->device_lock" lock
374 * Return: 0 on success, <0 on failure.
376 static int mei_ioctl_connect_client(struct file
*file
,
377 struct mei_connect_client_data
*data
)
379 struct mei_device
*dev
;
380 struct mei_client
*client
;
381 struct mei_me_client
*me_cl
;
385 cl
= file
->private_data
;
388 if (dev
->dev_state
!= MEI_DEV_ENABLED
)
391 if (cl
->state
!= MEI_FILE_INITIALIZING
&&
392 cl
->state
!= MEI_FILE_DISCONNECTED
)
395 /* find ME client we're trying to connect to */
396 me_cl
= mei_me_cl_by_uuid(dev
, &data
->in_client_uuid
);
398 (me_cl
->props
.fixed_address
&& !dev
->allow_fixed_address
)) {
399 dev_dbg(dev
->dev
, "Cannot connect to FW Client UUID = %pUl\n",
400 &data
->in_client_uuid
);
401 mei_me_cl_put(me_cl
);
405 dev_dbg(dev
->dev
, "Connect to FW Client ID = %d\n",
407 dev_dbg(dev
->dev
, "FW Client - Protocol Version = %d\n",
408 me_cl
->props
.protocol_version
);
409 dev_dbg(dev
->dev
, "FW Client - Max Msg Len = %d\n",
410 me_cl
->props
.max_msg_length
);
412 /* if we're connecting to amthif client then we will use the
413 * existing connection
415 if (uuid_le_cmp(data
->in_client_uuid
, mei_amthif_guid
) == 0) {
416 dev_dbg(dev
->dev
, "FW Client is amthi\n");
417 if (!mei_cl_is_connected(&dev
->iamthif_cl
)) {
425 dev
->iamthif_open_count
++;
426 file
->private_data
= &dev
->iamthif_cl
;
428 client
= &data
->out_client_properties
;
429 client
->max_msg_length
= me_cl
->props
.max_msg_length
;
430 client
->protocol_version
= me_cl
->props
.protocol_version
;
431 rets
= dev
->iamthif_cl
.status
;
436 /* prepare the output buffer */
437 client
= &data
->out_client_properties
;
438 client
->max_msg_length
= me_cl
->props
.max_msg_length
;
439 client
->protocol_version
= me_cl
->props
.protocol_version
;
440 dev_dbg(dev
->dev
, "Can connect?\n");
442 rets
= mei_cl_connect(cl
, me_cl
, file
);
445 mei_me_cl_put(me_cl
);
450 * mei_ioctl - the IOCTL function
452 * @file: pointer to file structure
453 * @cmd: ioctl command
454 * @data: pointer to mei message structure
456 * Return: 0 on success , <0 on error
458 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
460 struct mei_device
*dev
;
461 struct mei_cl
*cl
= file
->private_data
;
462 struct mei_connect_client_data connect_data
;
466 if (WARN_ON(!cl
|| !cl
->dev
))
471 dev_dbg(dev
->dev
, "IOCTL cmd = 0x%x", cmd
);
473 mutex_lock(&dev
->device_lock
);
474 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
480 case IOCTL_MEI_CONNECT_CLIENT
:
481 dev_dbg(dev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
482 if (copy_from_user(&connect_data
, (char __user
*)data
,
483 sizeof(struct mei_connect_client_data
))) {
484 dev_dbg(dev
->dev
, "failed to copy data from userland\n");
489 rets
= mei_ioctl_connect_client(file
, &connect_data
);
493 /* if all is ok, copying the data back to user. */
494 if (copy_to_user((char __user
*)data
, &connect_data
,
495 sizeof(struct mei_connect_client_data
))) {
496 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
504 dev_err(dev
->dev
, ": unsupported ioctl %d.\n", cmd
);
509 mutex_unlock(&dev
->device_lock
);
514 * mei_compat_ioctl - the compat IOCTL function
516 * @file: pointer to file structure
517 * @cmd: ioctl command
518 * @data: pointer to mei message structure
520 * Return: 0 on success , <0 on error
523 static long mei_compat_ioctl(struct file
*file
,
524 unsigned int cmd
, unsigned long data
)
526 return mei_ioctl(file
, cmd
, (unsigned long)compat_ptr(data
));
532 * mei_poll - the poll function
534 * @file: pointer to file structure
535 * @wait: pointer to poll_table structure
539 static unsigned int mei_poll(struct file
*file
, poll_table
*wait
)
541 unsigned long req_events
= poll_requested_events(wait
);
542 struct mei_cl
*cl
= file
->private_data
;
543 struct mei_device
*dev
;
544 unsigned int mask
= 0;
546 if (WARN_ON(!cl
|| !cl
->dev
))
551 mutex_lock(&dev
->device_lock
);
554 if (dev
->dev_state
!= MEI_DEV_ENABLED
||
555 !mei_cl_is_connected(cl
)) {
560 if (cl
== &dev
->iamthif_cl
) {
561 mask
= mei_amthif_poll(dev
, file
, wait
);
565 if (req_events
& (POLLIN
| POLLRDNORM
)) {
566 poll_wait(file
, &cl
->rx_wait
, wait
);
568 if (!list_empty(&cl
->rd_completed
))
569 mask
|= POLLIN
| POLLRDNORM
;
571 mei_cl_read_start(cl
, 0, file
);
575 mutex_unlock(&dev
->device_lock
);
580 * fw_status_show - mei device attribute show method
582 * @device: device pointer
583 * @attr: attribute pointer
584 * @buf: char out buffer
586 * Return: number of the bytes printed into buf or error
588 static ssize_t
fw_status_show(struct device
*device
,
589 struct device_attribute
*attr
, char *buf
)
591 struct mei_device
*dev
= dev_get_drvdata(device
);
592 struct mei_fw_status fw_status
;
596 mutex_lock(&dev
->device_lock
);
597 err
= mei_fw_status(dev
, &fw_status
);
598 mutex_unlock(&dev
->device_lock
);
600 dev_err(device
, "read fw_status error = %d\n", err
);
604 for (i
= 0; i
< fw_status
.count
; i
++)
605 cnt
+= scnprintf(buf
+ cnt
, PAGE_SIZE
- cnt
, "%08X\n",
606 fw_status
.status
[i
]);
609 static DEVICE_ATTR_RO(fw_status
);
611 static struct attribute
*mei_attrs
[] = {
612 &dev_attr_fw_status
.attr
,
615 ATTRIBUTE_GROUPS(mei
);
618 * file operations structure will be used for mei char device.
620 static const struct file_operations mei_fops
= {
621 .owner
= THIS_MODULE
,
623 .unlocked_ioctl
= mei_ioctl
,
625 .compat_ioctl
= mei_compat_ioctl
,
628 .release
= mei_release
,
634 static struct class *mei_class
;
635 static dev_t mei_devt
;
636 #define MEI_MAX_DEVS MINORMASK
637 static DEFINE_MUTEX(mei_minor_lock
);
638 static DEFINE_IDR(mei_idr
);
641 * mei_minor_get - obtain next free device minor number
643 * @dev: device pointer
645 * Return: allocated minor, or -ENOSPC if no free minor left
647 static int mei_minor_get(struct mei_device
*dev
)
651 mutex_lock(&mei_minor_lock
);
652 ret
= idr_alloc(&mei_idr
, dev
, 0, MEI_MAX_DEVS
, GFP_KERNEL
);
655 else if (ret
== -ENOSPC
)
656 dev_err(dev
->dev
, "too many mei devices\n");
658 mutex_unlock(&mei_minor_lock
);
663 * mei_minor_free - mark device minor number as free
665 * @dev: device pointer
667 static void mei_minor_free(struct mei_device
*dev
)
669 mutex_lock(&mei_minor_lock
);
670 idr_remove(&mei_idr
, dev
->minor
);
671 mutex_unlock(&mei_minor_lock
);
674 int mei_register(struct mei_device
*dev
, struct device
*parent
)
676 struct device
*clsdev
; /* class device */
679 ret
= mei_minor_get(dev
);
683 /* Fill in the data structures */
684 devno
= MKDEV(MAJOR(mei_devt
), dev
->minor
);
685 cdev_init(&dev
->cdev
, &mei_fops
);
686 dev
->cdev
.owner
= mei_fops
.owner
;
689 ret
= cdev_add(&dev
->cdev
, devno
, 1);
691 dev_err(parent
, "unable to add device %d:%d\n",
692 MAJOR(mei_devt
), dev
->minor
);
696 clsdev
= device_create_with_groups(mei_class
, parent
, devno
,
698 "mei%d", dev
->minor
);
700 if (IS_ERR(clsdev
)) {
701 dev_err(parent
, "unable to create device %d:%d\n",
702 MAJOR(mei_devt
), dev
->minor
);
703 ret
= PTR_ERR(clsdev
);
707 ret
= mei_dbgfs_register(dev
, dev_name(clsdev
));
709 dev_err(clsdev
, "cannot register debugfs ret = %d\n", ret
);
716 device_destroy(mei_class
, devno
);
718 cdev_del(&dev
->cdev
);
723 EXPORT_SYMBOL_GPL(mei_register
);
725 void mei_deregister(struct mei_device
*dev
)
729 devno
= dev
->cdev
.dev
;
730 cdev_del(&dev
->cdev
);
732 mei_dbgfs_deregister(dev
);
734 device_destroy(mei_class
, devno
);
738 EXPORT_SYMBOL_GPL(mei_deregister
);
740 static int __init
mei_init(void)
744 mei_class
= class_create(THIS_MODULE
, "mei");
745 if (IS_ERR(mei_class
)) {
746 pr_err("couldn't create class\n");
747 ret
= PTR_ERR(mei_class
);
751 ret
= alloc_chrdev_region(&mei_devt
, 0, MEI_MAX_DEVS
, "mei");
753 pr_err("unable to allocate char dev region\n");
757 ret
= mei_cl_bus_init();
759 pr_err("unable to initialize bus\n");
766 unregister_chrdev_region(mei_devt
, MEI_MAX_DEVS
);
768 class_destroy(mei_class
);
773 static void __exit
mei_exit(void)
775 unregister_chrdev_region(mei_devt
, MEI_MAX_DEVS
);
776 class_destroy(mei_class
);
780 module_init(mei_init
);
781 module_exit(mei_exit
);
783 MODULE_AUTHOR("Intel Corporation");
784 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
785 MODULE_LICENSE("GPL v2");