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>
35 #include <linux/miscdevice.h>
37 #include <linux/mei.h>
43 * mei_open - the open function
45 * @inode: pointer to inode structure
46 * @file: pointer to file structure
48 * returns 0 on success, <0 on error
50 static int mei_open(struct inode
*inode
, struct file
*file
)
52 struct miscdevice
*misc
= file
->private_data
;
55 struct mei_device
*dev
;
62 pdev
= container_of(misc
->parent
, struct pci_dev
, dev
);
64 dev
= pci_get_drvdata(pdev
);
68 mutex_lock(&dev
->device_lock
);
73 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
74 dev_dbg(&dev
->pdev
->dev
, "dev_state != MEI_ENABLED dev_state = %s\n",
75 mei_dev_state_str(dev
->dev_state
));
80 cl
= mei_cl_allocate(dev
);
84 /* open_handle_count check is handled in the mei_cl_link */
85 err
= mei_cl_link(cl
, MEI_HOST_CLIENT_ID_ANY
);
89 file
->private_data
= cl
;
91 mutex_unlock(&dev
->device_lock
);
93 return nonseekable_open(inode
, file
);
96 mutex_unlock(&dev
->device_lock
);
102 * mei_release - the release function
104 * @inode: pointer to inode structure
105 * @file: pointer to file structure
107 * returns 0 on success, <0 on error
109 static int mei_release(struct inode
*inode
, struct file
*file
)
111 struct mei_cl
*cl
= file
->private_data
;
112 struct mei_cl_cb
*cb
;
113 struct mei_device
*dev
;
116 if (WARN_ON(!cl
|| !cl
->dev
))
121 mutex_lock(&dev
->device_lock
);
122 if (cl
== &dev
->iamthif_cl
) {
123 rets
= mei_amthif_release(dev
, file
);
126 if (cl
->state
== MEI_FILE_CONNECTED
) {
127 cl
->state
= MEI_FILE_DISCONNECTING
;
128 cl_dbg(dev
, cl
, "disconnecting\n");
129 rets
= mei_cl_disconnect(cl
);
131 mei_cl_flush_queues(cl
);
132 cl_dbg(dev
, cl
, "removing\n");
140 cb
= mei_cl_find_read_cb(cl
);
141 /* Remove entry from read list */
149 file
->private_data
= NULL
;
155 mutex_unlock(&dev
->device_lock
);
161 * mei_read - the read function.
163 * @file: pointer to file structure
164 * @ubuf: pointer to user buffer
165 * @length: buffer length
166 * @offset: data offset in buffer
168 * returns >=0 data length on success , <0 on error
170 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
171 size_t length
, loff_t
*offset
)
173 struct mei_cl
*cl
= file
->private_data
;
174 struct mei_cl_cb
*cb_pos
= NULL
;
175 struct mei_cl_cb
*cb
= NULL
;
176 struct mei_device
*dev
;
181 if (WARN_ON(!cl
|| !cl
->dev
))
187 mutex_lock(&dev
->device_lock
);
188 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
198 if (cl
== &dev
->iamthif_cl
) {
199 rets
= mei_amthif_read(dev
, file
, ubuf
, length
, offset
);
206 if (cb
->buf_idx
> *offset
)
208 /* offset is beyond buf_idx we have no more data return 0 */
209 if (cb
->buf_idx
> 0 && cb
->buf_idx
<= *offset
) {
213 /* Offset needs to be cleaned for contiguous reads*/
214 if (cb
->buf_idx
== 0 && *offset
> 0)
216 } else if (*offset
> 0) {
220 err
= mei_cl_read_start(cl
, length
);
221 if (err
&& err
!= -EBUSY
) {
222 dev_dbg(&dev
->pdev
->dev
,
223 "mei start read failure with status = %d\n", err
);
228 if (MEI_READ_COMPLETE
!= cl
->reading_state
&&
229 !waitqueue_active(&cl
->rx_wait
)) {
230 if (file
->f_flags
& O_NONBLOCK
) {
235 mutex_unlock(&dev
->device_lock
);
237 if (wait_event_interruptible(cl
->rx_wait
,
238 MEI_READ_COMPLETE
== cl
->reading_state
||
239 mei_cl_is_transitioning(cl
))) {
241 if (signal_pending(current
))
246 mutex_lock(&dev
->device_lock
);
247 if (mei_cl_is_transitioning(cl
)) {
259 if (cl
->reading_state
!= MEI_READ_COMPLETE
) {
263 /* now copy the data to user space */
265 dev_dbg(&dev
->pdev
->dev
, "buf.size = %d buf.idx= %ld\n",
266 cb
->response_buffer
.size
, cb
->buf_idx
);
267 if (length
== 0 || ubuf
== NULL
|| *offset
> cb
->buf_idx
) {
272 /* length is being truncated to PAGE_SIZE,
273 * however buf_idx may point beyond that */
274 length
= min_t(size_t, length
, cb
->buf_idx
- *offset
);
276 if (copy_to_user(ubuf
, cb
->response_buffer
.data
+ *offset
, length
)) {
277 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
284 if ((unsigned long)*offset
< cb
->buf_idx
)
288 cb_pos
= mei_cl_find_read_cb(cl
);
289 /* Remove entry from read list */
291 list_del(&cb_pos
->list
);
293 cl
->reading_state
= MEI_IDLE
;
296 dev_dbg(&dev
->pdev
->dev
, "end mei read rets= %d\n", rets
);
297 mutex_unlock(&dev
->device_lock
);
301 * mei_write - the write function.
303 * @file: pointer to file structure
304 * @ubuf: pointer to user buffer
305 * @length: buffer length
306 * @offset: data offset in buffer
308 * returns >=0 data length on success , <0 on error
310 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
311 size_t length
, loff_t
*offset
)
313 struct mei_cl
*cl
= file
->private_data
;
314 struct mei_cl_cb
*write_cb
= NULL
;
315 struct mei_device
*dev
;
316 unsigned long timeout
= 0;
320 if (WARN_ON(!cl
|| !cl
->dev
))
325 mutex_lock(&dev
->device_lock
);
327 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
332 id
= mei_me_cl_by_id(dev
, cl
->me_client_id
);
343 if (length
> dev
->me_clients
[id
].props
.max_msg_length
) {
348 if (cl
->state
!= MEI_FILE_CONNECTED
) {
349 dev_err(&dev
->pdev
->dev
, "host client = %d, is not connected to ME client = %d",
350 cl
->host_client_id
, cl
->me_client_id
);
354 if (cl
== &dev
->iamthif_cl
) {
355 write_cb
= mei_amthif_find_read_list_entry(dev
, file
);
358 timeout
= write_cb
->read_time
+
359 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER
);
361 if (time_after(jiffies
, timeout
) ||
362 cl
->reading_state
== MEI_READ_COMPLETE
) {
364 list_del(&write_cb
->list
);
365 mei_io_cb_free(write_cb
);
371 /* free entry used in read */
372 if (cl
->reading_state
== MEI_READ_COMPLETE
) {
374 write_cb
= mei_cl_find_read_cb(cl
);
376 list_del(&write_cb
->list
);
377 mei_io_cb_free(write_cb
);
379 cl
->reading_state
= MEI_IDLE
;
382 } else if (cl
->reading_state
== MEI_IDLE
)
386 write_cb
= mei_io_cb_init(cl
, file
);
388 dev_err(&dev
->pdev
->dev
, "write cb allocation failed\n");
392 rets
= mei_io_cb_alloc_req_buf(write_cb
, length
);
396 rets
= copy_from_user(write_cb
->request_buffer
.data
, ubuf
, length
);
398 dev_dbg(&dev
->pdev
->dev
, "failed to copy data from userland\n");
403 if (cl
== &dev
->iamthif_cl
) {
404 rets
= mei_amthif_write(dev
, write_cb
);
407 dev_err(&dev
->pdev
->dev
,
408 "amthif write failed with status = %d\n", rets
);
411 mutex_unlock(&dev
->device_lock
);
415 rets
= mei_cl_write(cl
, write_cb
, false);
417 mutex_unlock(&dev
->device_lock
);
419 mei_io_cb_free(write_cb
);
424 * mei_ioctl_connect_client - the connect to fw client IOCTL function
426 * @dev: the device structure
427 * @data: IOCTL connect data, input and output parameters
428 * @file: private data of the file object
430 * Locking: called under "dev->device_lock" lock
432 * returns 0 on success, <0 on failure.
434 static int mei_ioctl_connect_client(struct file
*file
,
435 struct mei_connect_client_data
*data
)
437 struct mei_device
*dev
;
438 struct mei_client
*client
;
443 cl
= file
->private_data
;
444 if (WARN_ON(!cl
|| !cl
->dev
))
449 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
454 if (cl
->state
!= MEI_FILE_INITIALIZING
&&
455 cl
->state
!= MEI_FILE_DISCONNECTED
) {
460 /* find ME client we're trying to connect to */
461 i
= mei_me_cl_by_uuid(dev
, &data
->in_client_uuid
);
462 if (i
< 0 || dev
->me_clients
[i
].props
.fixed_address
) {
463 dev_dbg(&dev
->pdev
->dev
, "Cannot connect to FW Client UUID = %pUl\n",
464 &data
->in_client_uuid
);
469 cl
->me_client_id
= dev
->me_clients
[i
].client_id
;
471 dev_dbg(&dev
->pdev
->dev
, "Connect to FW Client ID = %d\n",
473 dev_dbg(&dev
->pdev
->dev
, "FW Client - Protocol Version = %d\n",
474 dev
->me_clients
[i
].props
.protocol_version
);
475 dev_dbg(&dev
->pdev
->dev
, "FW Client - Max Msg Len = %d\n",
476 dev
->me_clients
[i
].props
.max_msg_length
);
478 /* if we're connecting to amthif client then we will use the
479 * existing connection
481 if (uuid_le_cmp(data
->in_client_uuid
, mei_amthif_guid
) == 0) {
482 dev_dbg(&dev
->pdev
->dev
, "FW Client is amthi\n");
483 if (dev
->iamthif_cl
.state
!= MEI_FILE_CONNECTED
) {
491 dev
->iamthif_open_count
++;
492 file
->private_data
= &dev
->iamthif_cl
;
494 client
= &data
->out_client_properties
;
495 client
->max_msg_length
=
496 dev
->me_clients
[i
].props
.max_msg_length
;
497 client
->protocol_version
=
498 dev
->me_clients
[i
].props
.protocol_version
;
499 rets
= dev
->iamthif_cl
.status
;
505 /* prepare the output buffer */
506 client
= &data
->out_client_properties
;
507 client
->max_msg_length
= dev
->me_clients
[i
].props
.max_msg_length
;
508 client
->protocol_version
= dev
->me_clients
[i
].props
.protocol_version
;
509 dev_dbg(&dev
->pdev
->dev
, "Can connect?\n");
512 rets
= mei_cl_connect(cl
, file
);
520 * mei_ioctl - the IOCTL function
522 * @file: pointer to file structure
523 * @cmd: ioctl command
524 * @data: pointer to mei message structure
526 * returns 0 on success , <0 on error
528 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
530 struct mei_device
*dev
;
531 struct mei_cl
*cl
= file
->private_data
;
532 struct mei_connect_client_data
*connect_data
= NULL
;
535 if (cmd
!= IOCTL_MEI_CONNECT_CLIENT
)
538 if (WARN_ON(!cl
|| !cl
->dev
))
543 dev_dbg(&dev
->pdev
->dev
, "IOCTL cmd = 0x%x", cmd
);
545 mutex_lock(&dev
->device_lock
);
546 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
551 dev_dbg(&dev
->pdev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
553 connect_data
= kzalloc(sizeof(struct mei_connect_client_data
),
559 dev_dbg(&dev
->pdev
->dev
, "copy connect data from user\n");
560 if (copy_from_user(connect_data
, (char __user
*)data
,
561 sizeof(struct mei_connect_client_data
))) {
562 dev_dbg(&dev
->pdev
->dev
, "failed to copy data from userland\n");
567 rets
= mei_ioctl_connect_client(file
, connect_data
);
569 /* if all is ok, copying the data back to user. */
573 dev_dbg(&dev
->pdev
->dev
, "copy connect data to user\n");
574 if (copy_to_user((char __user
*)data
, connect_data
,
575 sizeof(struct mei_connect_client_data
))) {
576 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
583 mutex_unlock(&dev
->device_lock
);
588 * mei_compat_ioctl - the compat IOCTL function
590 * @file: pointer to file structure
591 * @cmd: ioctl command
592 * @data: pointer to mei message structure
594 * returns 0 on success , <0 on error
597 static long mei_compat_ioctl(struct file
*file
,
598 unsigned int cmd
, unsigned long data
)
600 return mei_ioctl(file
, cmd
, (unsigned long)compat_ptr(data
));
606 * mei_poll - the poll function
608 * @file: pointer to file structure
609 * @wait: pointer to poll_table structure
613 static unsigned int mei_poll(struct file
*file
, poll_table
*wait
)
615 struct mei_cl
*cl
= file
->private_data
;
616 struct mei_device
*dev
;
617 unsigned int mask
= 0;
619 if (WARN_ON(!cl
|| !cl
->dev
))
624 mutex_lock(&dev
->device_lock
);
626 if (!mei_cl_is_connected(cl
)) {
631 mutex_unlock(&dev
->device_lock
);
634 if (cl
== &dev
->iamthif_cl
)
635 return mei_amthif_poll(dev
, file
, wait
);
637 poll_wait(file
, &cl
->tx_wait
, wait
);
639 mutex_lock(&dev
->device_lock
);
641 if (!mei_cl_is_connected(cl
)) {
646 mask
|= (POLLIN
| POLLRDNORM
);
649 mutex_unlock(&dev
->device_lock
);
654 * file operations structure will be used for mei char device.
656 static const struct file_operations mei_fops
= {
657 .owner
= THIS_MODULE
,
659 .unlocked_ioctl
= mei_ioctl
,
661 .compat_ioctl
= mei_compat_ioctl
,
664 .release
= mei_release
,
673 static struct miscdevice mei_misc_device
= {
676 .minor
= MISC_DYNAMIC_MINOR
,
680 int mei_register(struct mei_device
*dev
)
683 mei_misc_device
.parent
= &dev
->pdev
->dev
;
684 ret
= misc_register(&mei_misc_device
);
688 if (mei_dbgfs_register(dev
, mei_misc_device
.name
))
689 dev_err(&dev
->pdev
->dev
, "cannot register debugfs\n");
693 EXPORT_SYMBOL_GPL(mei_register
);
695 void mei_deregister(struct mei_device
*dev
)
697 mei_dbgfs_deregister(dev
);
698 misc_deregister(&mei_misc_device
);
699 mei_misc_device
.parent
= NULL
;
701 EXPORT_SYMBOL_GPL(mei_deregister
);
703 static int __init
mei_init(void)
705 return mei_cl_bus_init();
708 static void __exit
mei_exit(void)
713 module_init(mei_init
);
714 module_exit(mei_exit
);
716 MODULE_AUTHOR("Intel Corporation");
717 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
718 MODULE_LICENSE("GPL v2");