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
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/kernel.h>
22 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/fcntl.h>
27 #include <linux/aio.h>
28 #include <linux/pci.h>
29 #include <linux/poll.h>
30 #include <linux/init.h>
31 #include <linux/ioctl.h>
32 #include <linux/cdev.h>
33 #include <linux/sched.h>
34 #include <linux/uuid.h>
35 #include <linux/compat.h>
36 #include <linux/jiffies.h>
37 #include <linux/interrupt.h>
38 #include <linux/miscdevice.h>
41 #include <linux/mei.h>
42 #include "interface.h"
44 /* AMT device is a singleton on the platform */
45 static struct pci_dev *mei_pdev;
47 /* mei_pci_tbl - PCI Device ID Table */
48 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
49 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
50 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
83 /* required last entry */
87 MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
89 static DEFINE_MUTEX(mei_mutex);
93 * find_read_list_entry - find read list entry
95 * @dev: device structure
96 * @file: pointer to file structure
98 * returns cb on success, NULL on error
100 static struct mei_cl_cb *find_read_list_entry(
101 struct mei_device *dev,
104 struct mei_cl_cb *pos = NULL;
105 struct mei_cl_cb *next = NULL;
107 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
108 list_for_each_entry_safe(pos, next, &dev->read_list.list, list)
109 if (mei_cl_cmp_id(cl, pos->cl))
115 * mei_open - the open function
117 * @inode: pointer to inode structure
118 * @file: pointer to file structure
120 * returns 0 on success, <0 on error
122 static int mei_open(struct inode *inode, struct file *file)
125 struct mei_device *dev;
133 dev = pci_get_drvdata(mei_pdev);
137 mutex_lock(&dev->device_lock);
139 cl = mei_cl_allocate(dev);
144 if (dev->dev_state != MEI_DEV_ENABLED) {
145 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
146 mei_dev_state_str(dev->dev_state));
150 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
151 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
152 MEI_MAX_OPEN_HANDLE_COUNT);
156 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
157 if (cl_id >= MEI_CLIENTS_MAX) {
158 dev_err(&dev->pdev->dev, "client_id exceded %d",
163 cl->host_client_id = cl_id;
165 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
167 dev->open_handle_count++;
169 list_add_tail(&cl->link, &dev->file_list);
171 set_bit(cl->host_client_id, dev->host_clients_map);
172 cl->state = MEI_FILE_INITIALIZING;
175 file->private_data = cl;
176 mutex_unlock(&dev->device_lock);
178 return nonseekable_open(inode, file);
181 mutex_unlock(&dev->device_lock);
188 * mei_release - the release function
190 * @inode: pointer to inode structure
191 * @file: pointer to file structure
193 * returns 0 on success, <0 on error
195 static int mei_release(struct inode *inode, struct file *file)
197 struct mei_cl *cl = file->private_data;
198 struct mei_cl_cb *cb;
199 struct mei_device *dev;
202 if (WARN_ON(!cl || !cl->dev))
207 mutex_lock(&dev->device_lock);
208 if (cl == &dev->iamthif_cl) {
209 rets = mei_amthif_release(dev, file);
212 if (cl->state == MEI_FILE_CONNECTED) {
213 cl->state = MEI_FILE_DISCONNECTING;
214 dev_dbg(&dev->pdev->dev,
215 "disconnecting client host client = %d, "
219 rets = mei_disconnect_host_client(dev, cl);
221 mei_cl_flush_queues(cl);
222 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
226 if (dev->open_handle_count > 0) {
227 clear_bit(cl->host_client_id, dev->host_clients_map);
228 dev->open_handle_count--;
230 mei_me_cl_unlink(dev, cl);
235 cb = find_read_list_entry(dev, cl);
236 /* Remove entry from read list */
244 file->private_data = NULL;
253 mutex_unlock(&dev->device_lock);
259 * mei_read - the read function.
261 * @file: pointer to file structure
262 * @ubuf: pointer to user buffer
263 * @length: buffer length
264 * @offset: data offset in buffer
266 * returns >=0 data length on success , <0 on error
268 static ssize_t mei_read(struct file *file, char __user *ubuf,
269 size_t length, loff_t *offset)
271 struct mei_cl *cl = file->private_data;
272 struct mei_cl_cb *cb_pos = NULL;
273 struct mei_cl_cb *cb = NULL;
274 struct mei_device *dev;
280 if (WARN_ON(!cl || !cl->dev))
285 mutex_lock(&dev->device_lock);
286 if (dev->dev_state != MEI_DEV_ENABLED) {
291 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
292 /* Do not allow to read watchdog client */
293 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
295 struct mei_me_client *me_client = &dev->me_clients[i];
296 if (cl->me_client_id == me_client->client_id) {
302 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
305 if (cl == &dev->iamthif_cl) {
306 rets = mei_amthif_read(dev, file, ubuf, length, offset);
310 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
313 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
314 cl->read_cb->buf_idx <= *offset) {
318 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
319 /*Offset needs to be cleaned for contiguous reads*/
325 err = mei_start_read(dev, cl);
326 if (err && err != -EBUSY) {
327 dev_dbg(&dev->pdev->dev,
328 "mei start read failure with status = %d\n", err);
333 if (MEI_READ_COMPLETE != cl->reading_state &&
334 !waitqueue_active(&cl->rx_wait)) {
335 if (file->f_flags & O_NONBLOCK) {
340 mutex_unlock(&dev->device_lock);
342 if (wait_event_interruptible(cl->rx_wait,
343 (MEI_READ_COMPLETE == cl->reading_state ||
344 MEI_FILE_INITIALIZING == cl->state ||
345 MEI_FILE_DISCONNECTED == cl->state ||
346 MEI_FILE_DISCONNECTING == cl->state))) {
347 if (signal_pending(current))
352 mutex_lock(&dev->device_lock);
353 if (MEI_FILE_INITIALIZING == cl->state ||
354 MEI_FILE_DISCONNECTED == cl->state ||
355 MEI_FILE_DISCONNECTING == cl->state) {
367 if (cl->reading_state != MEI_READ_COMPLETE) {
371 /* now copy the data to user space */
373 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
374 cb->response_buffer.size);
375 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
376 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
381 /* length is being truncated to PAGE_SIZE,
382 * however buf_idx may point beyond that */
383 length = min_t(size_t, length, cb->buf_idx - *offset);
385 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
392 if ((unsigned long)*offset < cb->buf_idx)
396 cb_pos = find_read_list_entry(dev, cl);
397 /* Remove entry from read list */
399 list_del(&cb_pos->list);
401 cl->reading_state = MEI_IDLE;
403 cl->read_pending = 0;
405 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
406 mutex_unlock(&dev->device_lock);
410 * mei_write - the write function.
412 * @file: pointer to file structure
413 * @ubuf: pointer to user buffer
414 * @length: buffer length
415 * @offset: data offset in buffer
417 * returns >=0 data length on success , <0 on error
419 static ssize_t mei_write(struct file *file, const char __user *ubuf,
420 size_t length, loff_t *offset)
422 struct mei_cl *cl = file->private_data;
423 struct mei_cl_cb *write_cb = NULL;
424 struct mei_msg_hdr mei_hdr;
425 struct mei_device *dev;
426 unsigned long timeout = 0;
430 if (WARN_ON(!cl || !cl->dev))
435 mutex_lock(&dev->device_lock);
437 if (dev->dev_state != MEI_DEV_ENABLED) {
442 i = mei_me_cl_by_id(dev, cl->me_client_id);
447 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
452 if (cl->state != MEI_FILE_CONNECTED) {
454 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
455 cl->host_client_id, cl->me_client_id);
458 if (cl == &dev->iamthif_cl) {
459 write_cb = mei_amthif_find_read_list_entry(dev, file);
462 timeout = write_cb->read_time +
463 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
465 if (time_after(jiffies, timeout) ||
466 cl->reading_state == MEI_READ_COMPLETE) {
468 list_del(&write_cb->list);
469 mei_io_cb_free(write_cb);
475 /* free entry used in read */
476 if (cl->reading_state == MEI_READ_COMPLETE) {
478 write_cb = find_read_list_entry(dev, cl);
480 list_del(&write_cb->list);
481 mei_io_cb_free(write_cb);
483 cl->reading_state = MEI_IDLE;
485 cl->read_pending = 0;
487 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
491 write_cb = mei_io_cb_init(cl, file);
493 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
497 rets = mei_io_cb_alloc_req_buf(write_cb, length);
501 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
503 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
509 ((memcmp(mei_wd_state_independence_msg[0],
510 write_cb->request_buffer.data, 4) == 0) ||
511 (memcmp(mei_wd_state_independence_msg[1],
512 write_cb->request_buffer.data, 4) == 0) ||
513 (memcmp(mei_wd_state_independence_msg[2],
514 write_cb->request_buffer.data, 4) == 0)))
515 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
517 if (cl == &dev->iamthif_cl) {
518 rets = mei_amthif_write(dev, write_cb);
521 dev_err(&dev->pdev->dev,
522 "amthi write failed with status = %d\n", rets);
525 mutex_unlock(&dev->device_lock);
529 write_cb->fop_type = MEI_FOP_WRITE;
531 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
532 cl->host_client_id, cl->me_client_id);
533 rets = mei_flow_ctrl_creds(dev, cl);
537 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
538 write_cb->buf_idx = 0;
539 mei_hdr.msg_complete = 0;
540 cl->writing_state = MEI_WRITING;
544 dev->mei_host_buffer_is_empty = false;
545 if (length > mei_hbuf_max_data(dev)) {
546 mei_hdr.length = mei_hbuf_max_data(dev);
547 mei_hdr.msg_complete = 0;
549 mei_hdr.length = length;
550 mei_hdr.msg_complete = 1;
552 mei_hdr.host_addr = cl->host_client_id;
553 mei_hdr.me_addr = cl->me_client_id;
554 mei_hdr.reserved = 0;
555 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
556 *((u32 *) &mei_hdr));
557 if (mei_write_message(dev, &mei_hdr,
558 write_cb->request_buffer.data, mei_hdr.length)) {
562 cl->writing_state = MEI_WRITING;
563 write_cb->buf_idx = mei_hdr.length;
566 if (mei_hdr.msg_complete) {
567 if (mei_flow_ctrl_reduce(dev, cl)) {
571 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
573 list_add_tail(&write_cb->list, &dev->write_list.list);
576 mutex_unlock(&dev->device_lock);
580 mutex_unlock(&dev->device_lock);
581 mei_io_cb_free(write_cb);
587 * mei_ioctl - the IOCTL function
589 * @file: pointer to file structure
590 * @cmd: ioctl command
591 * @data: pointer to mei message structure
593 * returns 0 on success , <0 on error
595 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
597 struct mei_device *dev;
598 struct mei_cl *cl = file->private_data;
599 struct mei_connect_client_data *connect_data = NULL;
602 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
605 if (WARN_ON(!cl || !cl->dev))
610 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
612 mutex_lock(&dev->device_lock);
613 if (dev->dev_state != MEI_DEV_ENABLED) {
618 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
620 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
626 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
627 if (copy_from_user(connect_data, (char __user *)data,
628 sizeof(struct mei_connect_client_data))) {
629 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
633 rets = mei_ioctl_connect_client(file, connect_data);
635 /* if all is ok, copying the data back to user. */
639 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
640 if (copy_to_user((char __user *)data, connect_data,
641 sizeof(struct mei_connect_client_data))) {
642 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
649 mutex_unlock(&dev->device_lock);
654 * mei_compat_ioctl - the compat IOCTL function
656 * @file: pointer to file structure
657 * @cmd: ioctl command
658 * @data: pointer to mei message structure
660 * returns 0 on success , <0 on error
663 static long mei_compat_ioctl(struct file *file,
664 unsigned int cmd, unsigned long data)
666 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
672 * mei_poll - the poll function
674 * @file: pointer to file structure
675 * @wait: pointer to poll_table structure
679 static unsigned int mei_poll(struct file *file, poll_table *wait)
681 struct mei_cl *cl = file->private_data;
682 struct mei_device *dev;
683 unsigned int mask = 0;
685 if (WARN_ON(!cl || !cl->dev))
690 mutex_lock(&dev->device_lock);
692 if (dev->dev_state != MEI_DEV_ENABLED)
696 if (cl == &dev->iamthif_cl) {
697 mask = mei_amthif_poll(dev, file, wait);
701 mutex_unlock(&dev->device_lock);
702 poll_wait(file, &cl->tx_wait, wait);
703 mutex_lock(&dev->device_lock);
704 if (MEI_WRITE_COMPLETE == cl->writing_state)
705 mask |= (POLLIN | POLLRDNORM);
708 mutex_unlock(&dev->device_lock);
713 * file operations structure will be used for mei char device.
715 static const struct file_operations mei_fops = {
716 .owner = THIS_MODULE,
718 .unlocked_ioctl = mei_ioctl,
720 .compat_ioctl = mei_compat_ioctl,
723 .release = mei_release,
733 static struct miscdevice mei_misc_device = {
736 .minor = MISC_DYNAMIC_MINOR,
740 * mei_quirk_probe - probe for devices that doesn't valid ME interface
741 * @pdev: PCI device structure
742 * @ent: entry into pci_device_table
744 * returns true if ME Interface is valid, false otherwise
746 static bool mei_quirk_probe(struct pci_dev *pdev,
747 const struct pci_device_id *ent)
750 if (ent->device == MEI_DEV_ID_PBG_1) {
751 pci_read_config_dword(pdev, 0x48, ®);
752 /* make sure that bit 9 is up and bit 10 is down */
753 if ((reg & 0x600) == 0x200) {
754 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
761 * mei_probe - Device Initialization Routine
763 * @pdev: PCI device structure
764 * @ent: entry in kcs_pci_tbl
766 * returns 0 on success, <0 on failure.
768 static int mei_probe(struct pci_dev *pdev,
769 const struct pci_device_id *ent)
771 struct mei_device *dev;
774 mutex_lock(&mei_mutex);
776 if (!mei_quirk_probe(pdev, ent)) {
786 err = pci_enable_device(pdev);
788 dev_err(&pdev->dev, "failed to enable pci device.\n");
791 /* set PCI host mastering */
792 pci_set_master(pdev);
793 /* pci request regions for mei driver */
794 err = pci_request_regions(pdev, KBUILD_MODNAME);
796 dev_err(&pdev->dev, "failed to get pci regions.\n");
799 /* allocates and initializes the mei dev structure */
800 dev = mei_device_init(pdev);
803 goto release_regions;
805 /* mapping IO device memory */
806 dev->mem_addr = pci_iomap(pdev, 0, 0);
807 if (!dev->mem_addr) {
808 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
812 pci_enable_msi(pdev);
814 /* request and enable interrupt */
815 if (pci_dev_msi_enabled(pdev))
816 err = request_threaded_irq(pdev->irq,
818 mei_interrupt_thread_handler,
819 IRQF_ONESHOT, KBUILD_MODNAME, dev);
821 err = request_threaded_irq(pdev->irq,
822 mei_interrupt_quick_handler,
823 mei_interrupt_thread_handler,
824 IRQF_SHARED, KBUILD_MODNAME, dev);
827 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
831 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
832 INIT_WORK(&dev->init_work, mei_host_client_init);
834 if (mei_hw_init(dev)) {
835 dev_err(&pdev->dev, "init hw failure.\n");
840 err = misc_register(&mei_misc_device);
845 pci_set_drvdata(pdev, dev);
848 schedule_delayed_work(&dev->timer_work, HZ);
850 mutex_unlock(&mei_mutex);
852 pr_debug("initialization successful.\n");
857 /* disable interrupts */
858 dev->host_hw_state = mei_hcsr_read(dev);
859 mei_disable_interrupts(dev);
860 flush_scheduled_work();
861 free_irq(pdev->irq, dev);
863 pci_disable_msi(pdev);
864 pci_iounmap(pdev, dev->mem_addr);
868 pci_release_regions(pdev);
870 pci_disable_device(pdev);
872 mutex_unlock(&mei_mutex);
873 dev_err(&pdev->dev, "initialization failed.\n");
878 * mei_remove - Device Removal Routine
880 * @pdev: PCI device structure
882 * mei_remove is called by the PCI subsystem to alert the driver
883 * that it should release a PCI device.
885 static void mei_remove(struct pci_dev *pdev)
887 struct mei_device *dev;
889 if (mei_pdev != pdev)
892 dev = pci_get_drvdata(pdev);
896 mutex_lock(&dev->device_lock);
898 cancel_delayed_work(&dev->timer_work);
904 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
905 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
906 mei_disconnect_host_client(dev, &dev->iamthif_cl);
908 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
909 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
910 mei_disconnect_host_client(dev, &dev->wd_cl);
913 /* Unregistering watchdog device */
914 mei_watchdog_unregister(dev);
916 /* remove entry if already in list */
917 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
918 mei_me_cl_unlink(dev, &dev->wd_cl);
919 mei_me_cl_unlink(dev, &dev->iamthif_cl);
921 dev->iamthif_current_cb = NULL;
922 dev->me_clients_num = 0;
924 mutex_unlock(&dev->device_lock);
926 flush_scheduled_work();
928 /* disable interrupts */
929 mei_disable_interrupts(dev);
931 free_irq(pdev->irq, dev);
932 pci_disable_msi(pdev);
933 pci_set_drvdata(pdev, NULL);
936 pci_iounmap(pdev, dev->mem_addr);
940 pci_release_regions(pdev);
941 pci_disable_device(pdev);
943 misc_deregister(&mei_misc_device);
946 static int mei_pci_suspend(struct device *device)
948 struct pci_dev *pdev = to_pci_dev(device);
949 struct mei_device *dev = pci_get_drvdata(pdev);
954 mutex_lock(&dev->device_lock);
956 cancel_delayed_work(&dev->timer_work);
958 /* Stop watchdog if exists */
959 err = mei_wd_stop(dev);
960 /* Set new mei state */
961 if (dev->dev_state == MEI_DEV_ENABLED ||
962 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
963 dev->dev_state = MEI_DEV_POWER_DOWN;
966 mutex_unlock(&dev->device_lock);
968 free_irq(pdev->irq, dev);
969 pci_disable_msi(pdev);
974 static int mei_pci_resume(struct device *device)
976 struct pci_dev *pdev = to_pci_dev(device);
977 struct mei_device *dev;
980 dev = pci_get_drvdata(pdev);
984 pci_enable_msi(pdev);
986 /* request and enable interrupt */
987 if (pci_dev_msi_enabled(pdev))
988 err = request_threaded_irq(pdev->irq,
990 mei_interrupt_thread_handler,
991 IRQF_ONESHOT, KBUILD_MODNAME, dev);
993 err = request_threaded_irq(pdev->irq,
994 mei_interrupt_quick_handler,
995 mei_interrupt_thread_handler,
996 IRQF_SHARED, KBUILD_MODNAME, dev);
999 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1004 mutex_lock(&dev->device_lock);
1005 dev->dev_state = MEI_DEV_POWER_UP;
1007 mutex_unlock(&dev->device_lock);
1009 /* Start timer if stopped in suspend */
1010 schedule_delayed_work(&dev->timer_work, HZ);
1014 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1015 #define MEI_PM_OPS (&mei_pm_ops)
1017 #define MEI_PM_OPS NULL
1018 #endif /* CONFIG_PM */
1020 * PCI driver structure
1022 static struct pci_driver mei_driver = {
1023 .name = KBUILD_MODNAME,
1024 .id_table = mei_pci_tbl,
1026 .remove = mei_remove,
1027 .shutdown = mei_remove,
1028 .driver.pm = MEI_PM_OPS,
1031 module_pci_driver(mei_driver);
1033 MODULE_AUTHOR("Intel Corporation");
1034 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1035 MODULE_LICENSE("GPL v2");