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>
40 #include <linux/mei.h>
46 /* AMT device is a singleton on the platform */
47 static struct pci_dev *mei_pdev;
49 /* mei_pci_tbl - PCI Device ID Table */
50 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
82 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
83 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
85 /* required last entry */
89 MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
91 static DEFINE_MUTEX(mei_mutex);
95 * mei_open - the open function
97 * @inode: pointer to inode structure
98 * @file: pointer to file structure
100 * returns 0 on success, <0 on error
102 static int mei_open(struct inode *inode, struct file *file)
105 struct mei_device *dev;
112 dev = pci_get_drvdata(mei_pdev);
116 mutex_lock(&dev->device_lock);
118 cl = mei_cl_allocate(dev);
123 if (dev->dev_state != MEI_DEV_ENABLED) {
124 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
125 mei_dev_state_str(dev->dev_state));
129 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
130 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
131 MEI_MAX_OPEN_HANDLE_COUNT);
135 err = mei_cl_link(cl, MEI_HOST_CLIENT_ID_ANY);
139 file->private_data = cl;
140 mutex_unlock(&dev->device_lock);
142 return nonseekable_open(inode, file);
145 mutex_unlock(&dev->device_lock);
152 * mei_release - the release function
154 * @inode: pointer to inode structure
155 * @file: pointer to file structure
157 * returns 0 on success, <0 on error
159 static int mei_release(struct inode *inode, struct file *file)
161 struct mei_cl *cl = file->private_data;
162 struct mei_cl_cb *cb;
163 struct mei_device *dev;
166 if (WARN_ON(!cl || !cl->dev))
171 mutex_lock(&dev->device_lock);
172 if (cl == &dev->iamthif_cl) {
173 rets = mei_amthif_release(dev, file);
176 if (cl->state == MEI_FILE_CONNECTED) {
177 cl->state = MEI_FILE_DISCONNECTING;
178 dev_dbg(&dev->pdev->dev,
179 "disconnecting client host client = %d, "
183 rets = mei_cl_disconnect(cl);
185 mei_cl_flush_queues(cl);
186 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
190 if (dev->open_handle_count > 0) {
191 clear_bit(cl->host_client_id, dev->host_clients_map);
192 dev->open_handle_count--;
200 cb = mei_cl_find_read_cb(cl);
201 /* Remove entry from read list */
209 file->private_data = NULL;
218 mutex_unlock(&dev->device_lock);
224 * mei_read - the read function.
226 * @file: pointer to file structure
227 * @ubuf: pointer to user buffer
228 * @length: buffer length
229 * @offset: data offset in buffer
231 * returns >=0 data length on success , <0 on error
233 static ssize_t mei_read(struct file *file, char __user *ubuf,
234 size_t length, loff_t *offset)
236 struct mei_cl *cl = file->private_data;
237 struct mei_cl_cb *cb_pos = NULL;
238 struct mei_cl_cb *cb = NULL;
239 struct mei_device *dev;
245 if (WARN_ON(!cl || !cl->dev))
250 mutex_lock(&dev->device_lock);
251 if (dev->dev_state != MEI_DEV_ENABLED) {
256 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
257 /* Do not allow to read watchdog client */
258 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
260 struct mei_me_client *me_client = &dev->me_clients[i];
261 if (cl->me_client_id == me_client->client_id) {
267 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
270 if (cl == &dev->iamthif_cl) {
271 rets = mei_amthif_read(dev, file, ubuf, length, offset);
275 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
278 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
279 cl->read_cb->buf_idx <= *offset) {
283 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
284 /*Offset needs to be cleaned for contiguous reads*/
290 err = mei_cl_read_start(cl);
291 if (err && err != -EBUSY) {
292 dev_dbg(&dev->pdev->dev,
293 "mei start read failure with status = %d\n", err);
298 if (MEI_READ_COMPLETE != cl->reading_state &&
299 !waitqueue_active(&cl->rx_wait)) {
300 if (file->f_flags & O_NONBLOCK) {
305 mutex_unlock(&dev->device_lock);
307 if (wait_event_interruptible(cl->rx_wait,
308 (MEI_READ_COMPLETE == cl->reading_state ||
309 MEI_FILE_INITIALIZING == cl->state ||
310 MEI_FILE_DISCONNECTED == cl->state ||
311 MEI_FILE_DISCONNECTING == cl->state))) {
312 if (signal_pending(current))
317 mutex_lock(&dev->device_lock);
318 if (MEI_FILE_INITIALIZING == cl->state ||
319 MEI_FILE_DISCONNECTED == cl->state ||
320 MEI_FILE_DISCONNECTING == cl->state) {
332 if (cl->reading_state != MEI_READ_COMPLETE) {
336 /* now copy the data to user space */
338 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
339 cb->response_buffer.size);
340 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
341 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
346 /* length is being truncated to PAGE_SIZE,
347 * however buf_idx may point beyond that */
348 length = min_t(size_t, length, cb->buf_idx - *offset);
350 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
357 if ((unsigned long)*offset < cb->buf_idx)
361 cb_pos = mei_cl_find_read_cb(cl);
362 /* Remove entry from read list */
364 list_del(&cb_pos->list);
366 cl->reading_state = MEI_IDLE;
369 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
370 mutex_unlock(&dev->device_lock);
374 * mei_write - the write function.
376 * @file: pointer to file structure
377 * @ubuf: pointer to user buffer
378 * @length: buffer length
379 * @offset: data offset in buffer
381 * returns >=0 data length on success , <0 on error
383 static ssize_t mei_write(struct file *file, const char __user *ubuf,
384 size_t length, loff_t *offset)
386 struct mei_cl *cl = file->private_data;
387 struct mei_cl_cb *write_cb = NULL;
388 struct mei_msg_hdr mei_hdr;
389 struct mei_device *dev;
390 unsigned long timeout = 0;
394 if (WARN_ON(!cl || !cl->dev))
399 mutex_lock(&dev->device_lock);
401 if (dev->dev_state != MEI_DEV_ENABLED) {
406 i = mei_me_cl_by_id(dev, cl->me_client_id);
411 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
416 if (cl->state != MEI_FILE_CONNECTED) {
418 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
419 cl->host_client_id, cl->me_client_id);
422 if (cl == &dev->iamthif_cl) {
423 write_cb = mei_amthif_find_read_list_entry(dev, file);
426 timeout = write_cb->read_time +
427 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
429 if (time_after(jiffies, timeout) ||
430 cl->reading_state == MEI_READ_COMPLETE) {
432 list_del(&write_cb->list);
433 mei_io_cb_free(write_cb);
439 /* free entry used in read */
440 if (cl->reading_state == MEI_READ_COMPLETE) {
442 write_cb = mei_cl_find_read_cb(cl);
444 list_del(&write_cb->list);
445 mei_io_cb_free(write_cb);
447 cl->reading_state = MEI_IDLE;
450 } else if (cl->reading_state == MEI_IDLE)
454 write_cb = mei_io_cb_init(cl, file);
456 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
460 rets = mei_io_cb_alloc_req_buf(write_cb, length);
464 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
466 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
472 ((memcmp(mei_wd_state_independence_msg[0],
473 write_cb->request_buffer.data, 4) == 0) ||
474 (memcmp(mei_wd_state_independence_msg[1],
475 write_cb->request_buffer.data, 4) == 0) ||
476 (memcmp(mei_wd_state_independence_msg[2],
477 write_cb->request_buffer.data, 4) == 0)))
478 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
480 if (cl == &dev->iamthif_cl) {
481 rets = mei_amthif_write(dev, write_cb);
484 dev_err(&dev->pdev->dev,
485 "amthif write failed with status = %d\n", rets);
488 mutex_unlock(&dev->device_lock);
492 write_cb->fop_type = MEI_FOP_WRITE;
494 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
495 cl->host_client_id, cl->me_client_id);
496 rets = mei_cl_flow_ctrl_creds(cl);
500 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
501 write_cb->buf_idx = 0;
502 mei_hdr.msg_complete = 0;
503 cl->writing_state = MEI_WRITING;
507 dev->mei_host_buffer_is_empty = false;
508 if (length > mei_hbuf_max_data(dev)) {
509 mei_hdr.length = mei_hbuf_max_data(dev);
510 mei_hdr.msg_complete = 0;
512 mei_hdr.length = length;
513 mei_hdr.msg_complete = 1;
515 mei_hdr.host_addr = cl->host_client_id;
516 mei_hdr.me_addr = cl->me_client_id;
517 mei_hdr.reserved = 0;
519 dev_dbg(&dev->pdev->dev, "write " MEI_HDR_FMT "\n",
520 MEI_HDR_PRM(&mei_hdr));
521 if (mei_write_message(dev, &mei_hdr, write_cb->request_buffer.data)) {
525 cl->writing_state = MEI_WRITING;
526 write_cb->buf_idx = mei_hdr.length;
529 if (mei_hdr.msg_complete) {
530 if (mei_cl_flow_ctrl_reduce(cl)) {
534 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
536 list_add_tail(&write_cb->list, &dev->write_list.list);
539 mutex_unlock(&dev->device_lock);
543 mutex_unlock(&dev->device_lock);
544 mei_io_cb_free(write_cb);
549 * mei_ioctl_connect_client - the connect to fw client IOCTL function
551 * @dev: the device structure
552 * @data: IOCTL connect data, input and output parameters
553 * @file: private data of the file object
555 * Locking: called under "dev->device_lock" lock
557 * returns 0 on success, <0 on failure.
559 static int mei_ioctl_connect_client(struct file *file,
560 struct mei_connect_client_data *data)
562 struct mei_device *dev;
563 struct mei_client *client;
568 cl = file->private_data;
569 if (WARN_ON(!cl || !cl->dev))
574 if (dev->dev_state != MEI_DEV_ENABLED) {
579 if (cl->state != MEI_FILE_INITIALIZING &&
580 cl->state != MEI_FILE_DISCONNECTED) {
585 /* find ME client we're trying to connect to */
586 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
587 if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
588 cl->me_client_id = dev->me_clients[i].client_id;
589 cl->state = MEI_FILE_CONNECTING;
592 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
594 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
595 dev->me_clients[i].props.protocol_version);
596 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
597 dev->me_clients[i].props.max_msg_length);
599 /* if we're connecting to amthif client then we will use the
600 * existing connection
602 if (uuid_le_cmp(data->in_client_uuid, mei_amthif_guid) == 0) {
603 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
604 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
608 clear_bit(cl->host_client_id, dev->host_clients_map);
613 file->private_data = &dev->iamthif_cl;
615 client = &data->out_client_properties;
616 client->max_msg_length =
617 dev->me_clients[i].props.max_msg_length;
618 client->protocol_version =
619 dev->me_clients[i].props.protocol_version;
620 rets = dev->iamthif_cl.status;
625 if (cl->state != MEI_FILE_CONNECTING) {
631 /* prepare the output buffer */
632 client = &data->out_client_properties;
633 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
634 client->protocol_version = dev->me_clients[i].props.protocol_version;
635 dev_dbg(&dev->pdev->dev, "Can connect?\n");
638 rets = mei_cl_connect(cl, file);
641 dev_dbg(&dev->pdev->dev, "free connect cb memory.");
647 * mei_ioctl - the IOCTL function
649 * @file: pointer to file structure
650 * @cmd: ioctl command
651 * @data: pointer to mei message structure
653 * returns 0 on success , <0 on error
655 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
657 struct mei_device *dev;
658 struct mei_cl *cl = file->private_data;
659 struct mei_connect_client_data *connect_data = NULL;
662 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
665 if (WARN_ON(!cl || !cl->dev))
670 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
672 mutex_lock(&dev->device_lock);
673 if (dev->dev_state != MEI_DEV_ENABLED) {
678 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
680 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
686 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
687 if (copy_from_user(connect_data, (char __user *)data,
688 sizeof(struct mei_connect_client_data))) {
689 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
694 rets = mei_ioctl_connect_client(file, connect_data);
696 /* if all is ok, copying the data back to user. */
700 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
701 if (copy_to_user((char __user *)data, connect_data,
702 sizeof(struct mei_connect_client_data))) {
703 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
710 mutex_unlock(&dev->device_lock);
715 * mei_compat_ioctl - the compat IOCTL function
717 * @file: pointer to file structure
718 * @cmd: ioctl command
719 * @data: pointer to mei message structure
721 * returns 0 on success , <0 on error
724 static long mei_compat_ioctl(struct file *file,
725 unsigned int cmd, unsigned long data)
727 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
733 * mei_poll - the poll function
735 * @file: pointer to file structure
736 * @wait: pointer to poll_table structure
740 static unsigned int mei_poll(struct file *file, poll_table *wait)
742 struct mei_cl *cl = file->private_data;
743 struct mei_device *dev;
744 unsigned int mask = 0;
746 if (WARN_ON(!cl || !cl->dev))
751 mutex_lock(&dev->device_lock);
753 if (dev->dev_state != MEI_DEV_ENABLED)
757 if (cl == &dev->iamthif_cl) {
758 mask = mei_amthif_poll(dev, file, wait);
762 mutex_unlock(&dev->device_lock);
763 poll_wait(file, &cl->tx_wait, wait);
764 mutex_lock(&dev->device_lock);
765 if (MEI_WRITE_COMPLETE == cl->writing_state)
766 mask |= (POLLIN | POLLRDNORM);
769 mutex_unlock(&dev->device_lock);
774 * file operations structure will be used for mei char device.
776 static const struct file_operations mei_fops = {
777 .owner = THIS_MODULE,
779 .unlocked_ioctl = mei_ioctl,
781 .compat_ioctl = mei_compat_ioctl,
784 .release = mei_release,
794 static struct miscdevice mei_misc_device = {
797 .minor = MISC_DYNAMIC_MINOR,
801 * mei_quirk_probe - probe for devices that doesn't valid ME interface
802 * @pdev: PCI device structure
803 * @ent: entry into pci_device_table
805 * returns true if ME Interface is valid, false otherwise
807 static bool mei_quirk_probe(struct pci_dev *pdev,
808 const struct pci_device_id *ent)
811 if (ent->device == MEI_DEV_ID_PBG_1) {
812 pci_read_config_dword(pdev, 0x48, ®);
813 /* make sure that bit 9 is up and bit 10 is down */
814 if ((reg & 0x600) == 0x200) {
815 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
822 * mei_probe - Device Initialization Routine
824 * @pdev: PCI device structure
825 * @ent: entry in kcs_pci_tbl
827 * returns 0 on success, <0 on failure.
829 static int mei_probe(struct pci_dev *pdev,
830 const struct pci_device_id *ent)
832 struct mei_device *dev;
835 mutex_lock(&mei_mutex);
837 if (!mei_quirk_probe(pdev, ent)) {
847 err = pci_enable_device(pdev);
849 dev_err(&pdev->dev, "failed to enable pci device.\n");
852 /* set PCI host mastering */
853 pci_set_master(pdev);
854 /* pci request regions for mei driver */
855 err = pci_request_regions(pdev, KBUILD_MODNAME);
857 dev_err(&pdev->dev, "failed to get pci regions.\n");
860 /* allocates and initializes the mei dev structure */
861 dev = mei_device_init(pdev);
864 goto release_regions;
866 /* mapping IO device memory */
867 dev->mem_addr = pci_iomap(pdev, 0, 0);
868 if (!dev->mem_addr) {
869 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
873 pci_enable_msi(pdev);
875 /* request and enable interrupt */
876 if (pci_dev_msi_enabled(pdev))
877 err = request_threaded_irq(pdev->irq,
879 mei_interrupt_thread_handler,
880 IRQF_ONESHOT, KBUILD_MODNAME, dev);
882 err = request_threaded_irq(pdev->irq,
883 mei_interrupt_quick_handler,
884 mei_interrupt_thread_handler,
885 IRQF_SHARED, KBUILD_MODNAME, dev);
888 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
893 if (mei_hw_init(dev)) {
894 dev_err(&pdev->dev, "init hw failure.\n");
899 err = misc_register(&mei_misc_device);
904 pci_set_drvdata(pdev, dev);
907 schedule_delayed_work(&dev->timer_work, HZ);
909 mutex_unlock(&mei_mutex);
911 pr_debug("initialization successful.\n");
916 mei_disable_interrupts(dev);
917 flush_scheduled_work();
918 free_irq(pdev->irq, dev);
920 pci_disable_msi(pdev);
921 pci_iounmap(pdev, dev->mem_addr);
925 pci_release_regions(pdev);
927 pci_disable_device(pdev);
929 mutex_unlock(&mei_mutex);
930 dev_err(&pdev->dev, "initialization failed.\n");
935 * mei_remove - Device Removal Routine
937 * @pdev: PCI device structure
939 * mei_remove is called by the PCI subsystem to alert the driver
940 * that it should release a PCI device.
942 static void mei_remove(struct pci_dev *pdev)
944 struct mei_device *dev;
946 if (mei_pdev != pdev)
949 dev = pci_get_drvdata(pdev);
953 mutex_lock(&dev->device_lock);
955 cancel_delayed_work(&dev->timer_work);
961 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
962 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
963 mei_cl_disconnect(&dev->iamthif_cl);
965 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
966 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
967 mei_cl_disconnect(&dev->wd_cl);
970 /* Unregistering watchdog device */
971 mei_watchdog_unregister(dev);
973 /* remove entry if already in list */
974 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
976 if (dev->open_handle_count > 0)
977 dev->open_handle_count--;
978 mei_cl_unlink(&dev->wd_cl);
980 if (dev->open_handle_count > 0)
981 dev->open_handle_count--;
982 mei_cl_unlink(&dev->iamthif_cl);
984 dev->iamthif_current_cb = NULL;
985 dev->me_clients_num = 0;
987 mutex_unlock(&dev->device_lock);
989 flush_scheduled_work();
991 /* disable interrupts */
992 mei_disable_interrupts(dev);
994 free_irq(pdev->irq, dev);
995 pci_disable_msi(pdev);
996 pci_set_drvdata(pdev, NULL);
999 pci_iounmap(pdev, dev->mem_addr);
1003 pci_release_regions(pdev);
1004 pci_disable_device(pdev);
1006 misc_deregister(&mei_misc_device);
1009 static int mei_pci_suspend(struct device *device)
1011 struct pci_dev *pdev = to_pci_dev(device);
1012 struct mei_device *dev = pci_get_drvdata(pdev);
1017 mutex_lock(&dev->device_lock);
1019 cancel_delayed_work(&dev->timer_work);
1021 /* Stop watchdog if exists */
1022 err = mei_wd_stop(dev);
1023 /* Set new mei state */
1024 if (dev->dev_state == MEI_DEV_ENABLED ||
1025 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1026 dev->dev_state = MEI_DEV_POWER_DOWN;
1029 mutex_unlock(&dev->device_lock);
1031 free_irq(pdev->irq, dev);
1032 pci_disable_msi(pdev);
1037 static int mei_pci_resume(struct device *device)
1039 struct pci_dev *pdev = to_pci_dev(device);
1040 struct mei_device *dev;
1043 dev = pci_get_drvdata(pdev);
1047 pci_enable_msi(pdev);
1049 /* request and enable interrupt */
1050 if (pci_dev_msi_enabled(pdev))
1051 err = request_threaded_irq(pdev->irq,
1053 mei_interrupt_thread_handler,
1054 IRQF_ONESHOT, KBUILD_MODNAME, dev);
1056 err = request_threaded_irq(pdev->irq,
1057 mei_interrupt_quick_handler,
1058 mei_interrupt_thread_handler,
1059 IRQF_SHARED, KBUILD_MODNAME, dev);
1062 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1067 mutex_lock(&dev->device_lock);
1068 dev->dev_state = MEI_DEV_POWER_UP;
1070 mutex_unlock(&dev->device_lock);
1072 /* Start timer if stopped in suspend */
1073 schedule_delayed_work(&dev->timer_work, HZ);
1077 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1078 #define MEI_PM_OPS (&mei_pm_ops)
1080 #define MEI_PM_OPS NULL
1081 #endif /* CONFIG_PM */
1083 * PCI driver structure
1085 static struct pci_driver mei_driver = {
1086 .name = KBUILD_MODNAME,
1087 .id_table = mei_pci_tbl,
1089 .remove = mei_remove,
1090 .shutdown = mei_remove,
1091 .driver.pm = MEI_PM_OPS,
1094 module_pci_driver(mei_driver);
1096 MODULE_AUTHOR("Intel Corporation");
1097 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1098 MODULE_LICENSE("GPL v2");