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;
113 dev = pci_get_drvdata(mei_pdev);
117 mutex_lock(&dev->device_lock);
119 cl = mei_cl_allocate(dev);
124 if (dev->dev_state != MEI_DEV_ENABLED) {
125 dev_dbg(&dev->pdev->dev, "dev_state != MEI_ENABLED dev_state = %s\n",
126 mei_dev_state_str(dev->dev_state));
130 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
131 dev_err(&dev->pdev->dev, "open_handle_count exceded %d",
132 MEI_MAX_OPEN_HANDLE_COUNT);
136 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
137 if (cl_id >= MEI_CLIENTS_MAX) {
138 dev_err(&dev->pdev->dev, "client_id exceded %d",
143 cl->host_client_id = cl_id;
145 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
147 dev->open_handle_count++;
149 list_add_tail(&cl->link, &dev->file_list);
151 set_bit(cl->host_client_id, dev->host_clients_map);
152 cl->state = MEI_FILE_INITIALIZING;
155 file->private_data = cl;
156 mutex_unlock(&dev->device_lock);
158 return nonseekable_open(inode, file);
161 mutex_unlock(&dev->device_lock);
168 * mei_release - the release function
170 * @inode: pointer to inode structure
171 * @file: pointer to file structure
173 * returns 0 on success, <0 on error
175 static int mei_release(struct inode *inode, struct file *file)
177 struct mei_cl *cl = file->private_data;
178 struct mei_cl_cb *cb;
179 struct mei_device *dev;
182 if (WARN_ON(!cl || !cl->dev))
187 mutex_lock(&dev->device_lock);
188 if (cl == &dev->iamthif_cl) {
189 rets = mei_amthif_release(dev, file);
192 if (cl->state == MEI_FILE_CONNECTED) {
193 cl->state = MEI_FILE_DISCONNECTING;
194 dev_dbg(&dev->pdev->dev,
195 "disconnecting client host client = %d, "
199 rets = mei_cl_disconnect(cl);
201 mei_cl_flush_queues(cl);
202 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
206 if (dev->open_handle_count > 0) {
207 clear_bit(cl->host_client_id, dev->host_clients_map);
208 dev->open_handle_count--;
215 cb = mei_cl_find_read_cb(cl);
216 /* Remove entry from read list */
224 file->private_data = NULL;
233 mutex_unlock(&dev->device_lock);
239 * mei_read - the read function.
241 * @file: pointer to file structure
242 * @ubuf: pointer to user buffer
243 * @length: buffer length
244 * @offset: data offset in buffer
246 * returns >=0 data length on success , <0 on error
248 static ssize_t mei_read(struct file *file, char __user *ubuf,
249 size_t length, loff_t *offset)
251 struct mei_cl *cl = file->private_data;
252 struct mei_cl_cb *cb_pos = NULL;
253 struct mei_cl_cb *cb = NULL;
254 struct mei_device *dev;
260 if (WARN_ON(!cl || !cl->dev))
265 mutex_lock(&dev->device_lock);
266 if (dev->dev_state != MEI_DEV_ENABLED) {
271 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
272 /* Do not allow to read watchdog client */
273 i = mei_me_cl_by_uuid(dev, &mei_wd_guid);
275 struct mei_me_client *me_client = &dev->me_clients[i];
276 if (cl->me_client_id == me_client->client_id) {
282 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
285 if (cl == &dev->iamthif_cl) {
286 rets = mei_amthif_read(dev, file, ubuf, length, offset);
290 if (cl->read_cb && cl->read_cb->buf_idx > *offset) {
293 } else if (cl->read_cb && cl->read_cb->buf_idx > 0 &&
294 cl->read_cb->buf_idx <= *offset) {
298 } else if ((!cl->read_cb || !cl->read_cb->buf_idx) && *offset > 0) {
299 /*Offset needs to be cleaned for contiguous reads*/
305 err = mei_cl_read_start(cl);
306 if (err && err != -EBUSY) {
307 dev_dbg(&dev->pdev->dev,
308 "mei start read failure with status = %d\n", err);
313 if (MEI_READ_COMPLETE != cl->reading_state &&
314 !waitqueue_active(&cl->rx_wait)) {
315 if (file->f_flags & O_NONBLOCK) {
320 mutex_unlock(&dev->device_lock);
322 if (wait_event_interruptible(cl->rx_wait,
323 (MEI_READ_COMPLETE == cl->reading_state ||
324 MEI_FILE_INITIALIZING == cl->state ||
325 MEI_FILE_DISCONNECTED == cl->state ||
326 MEI_FILE_DISCONNECTING == cl->state))) {
327 if (signal_pending(current))
332 mutex_lock(&dev->device_lock);
333 if (MEI_FILE_INITIALIZING == cl->state ||
334 MEI_FILE_DISCONNECTED == cl->state ||
335 MEI_FILE_DISCONNECTING == cl->state) {
347 if (cl->reading_state != MEI_READ_COMPLETE) {
351 /* now copy the data to user space */
353 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
354 cb->response_buffer.size);
355 dev_dbg(&dev->pdev->dev, "cb->buf_idx - %lu\n", cb->buf_idx);
356 if (length == 0 || ubuf == NULL || *offset > cb->buf_idx) {
361 /* length is being truncated to PAGE_SIZE,
362 * however buf_idx may point beyond that */
363 length = min_t(size_t, length, cb->buf_idx - *offset);
365 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
372 if ((unsigned long)*offset < cb->buf_idx)
376 cb_pos = mei_cl_find_read_cb(cl);
377 /* Remove entry from read list */
379 list_del(&cb_pos->list);
381 cl->reading_state = MEI_IDLE;
383 cl->read_pending = 0;
385 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
386 mutex_unlock(&dev->device_lock);
390 * mei_write - the write function.
392 * @file: pointer to file structure
393 * @ubuf: pointer to user buffer
394 * @length: buffer length
395 * @offset: data offset in buffer
397 * returns >=0 data length on success , <0 on error
399 static ssize_t mei_write(struct file *file, const char __user *ubuf,
400 size_t length, loff_t *offset)
402 struct mei_cl *cl = file->private_data;
403 struct mei_cl_cb *write_cb = NULL;
404 struct mei_msg_hdr mei_hdr;
405 struct mei_device *dev;
406 unsigned long timeout = 0;
410 if (WARN_ON(!cl || !cl->dev))
415 mutex_lock(&dev->device_lock);
417 if (dev->dev_state != MEI_DEV_ENABLED) {
422 i = mei_me_cl_by_id(dev, cl->me_client_id);
427 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
432 if (cl->state != MEI_FILE_CONNECTED) {
434 dev_err(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
435 cl->host_client_id, cl->me_client_id);
438 if (cl == &dev->iamthif_cl) {
439 write_cb = mei_amthif_find_read_list_entry(dev, file);
442 timeout = write_cb->read_time +
443 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
445 if (time_after(jiffies, timeout) ||
446 cl->reading_state == MEI_READ_COMPLETE) {
448 list_del(&write_cb->list);
449 mei_io_cb_free(write_cb);
455 /* free entry used in read */
456 if (cl->reading_state == MEI_READ_COMPLETE) {
458 write_cb = mei_cl_find_read_cb(cl);
460 list_del(&write_cb->list);
461 mei_io_cb_free(write_cb);
463 cl->reading_state = MEI_IDLE;
465 cl->read_pending = 0;
467 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
471 write_cb = mei_io_cb_init(cl, file);
473 dev_err(&dev->pdev->dev, "write cb allocation failed\n");
477 rets = mei_io_cb_alloc_req_buf(write_cb, length);
481 dev_dbg(&dev->pdev->dev, "cb request size = %zd\n", length);
483 rets = copy_from_user(write_cb->request_buffer.data, ubuf, length);
489 ((memcmp(mei_wd_state_independence_msg[0],
490 write_cb->request_buffer.data, 4) == 0) ||
491 (memcmp(mei_wd_state_independence_msg[1],
492 write_cb->request_buffer.data, 4) == 0) ||
493 (memcmp(mei_wd_state_independence_msg[2],
494 write_cb->request_buffer.data, 4) == 0)))
495 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
497 if (cl == &dev->iamthif_cl) {
498 rets = mei_amthif_write(dev, write_cb);
501 dev_err(&dev->pdev->dev,
502 "amthi write failed with status = %d\n", rets);
505 mutex_unlock(&dev->device_lock);
509 write_cb->fop_type = MEI_FOP_WRITE;
511 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
512 cl->host_client_id, cl->me_client_id);
513 rets = mei_cl_flow_ctrl_creds(cl);
517 if (rets == 0 || dev->mei_host_buffer_is_empty == false) {
518 write_cb->buf_idx = 0;
519 mei_hdr.msg_complete = 0;
520 cl->writing_state = MEI_WRITING;
524 dev->mei_host_buffer_is_empty = false;
525 if (length > mei_hbuf_max_data(dev)) {
526 mei_hdr.length = mei_hbuf_max_data(dev);
527 mei_hdr.msg_complete = 0;
529 mei_hdr.length = length;
530 mei_hdr.msg_complete = 1;
532 mei_hdr.host_addr = cl->host_client_id;
533 mei_hdr.me_addr = cl->me_client_id;
534 mei_hdr.reserved = 0;
536 dev_dbg(&dev->pdev->dev, "write " MEI_HDR_FMT "\n",
537 MEI_HDR_PRM(&mei_hdr));
538 if (mei_write_message(dev, &mei_hdr, write_cb->request_buffer.data)) {
542 cl->writing_state = MEI_WRITING;
543 write_cb->buf_idx = mei_hdr.length;
546 if (mei_hdr.msg_complete) {
547 if (mei_cl_flow_ctrl_reduce(cl)) {
551 list_add_tail(&write_cb->list, &dev->write_waiting_list.list);
553 list_add_tail(&write_cb->list, &dev->write_list.list);
556 mutex_unlock(&dev->device_lock);
560 mutex_unlock(&dev->device_lock);
561 mei_io_cb_free(write_cb);
566 * mei_ioctl_connect_client - the connect to fw client IOCTL function
568 * @dev: the device structure
569 * @data: IOCTL connect data, input and output parameters
570 * @file: private data of the file object
572 * Locking: called under "dev->device_lock" lock
574 * returns 0 on success, <0 on failure.
576 static int mei_ioctl_connect_client(struct file *file,
577 struct mei_connect_client_data *data)
579 struct mei_device *dev;
580 struct mei_client *client;
585 cl = file->private_data;
586 if (WARN_ON(!cl || !cl->dev))
591 if (dev->dev_state != MEI_DEV_ENABLED) {
596 if (cl->state != MEI_FILE_INITIALIZING &&
597 cl->state != MEI_FILE_DISCONNECTED) {
602 /* find ME client we're trying to connect to */
603 i = mei_me_cl_by_uuid(dev, &data->in_client_uuid);
604 if (i >= 0 && !dev->me_clients[i].props.fixed_address) {
605 cl->me_client_id = dev->me_clients[i].client_id;
606 cl->state = MEI_FILE_CONNECTING;
609 dev_dbg(&dev->pdev->dev, "Connect to FW Client ID = %d\n",
611 dev_dbg(&dev->pdev->dev, "FW Client - Protocol Version = %d\n",
612 dev->me_clients[i].props.protocol_version);
613 dev_dbg(&dev->pdev->dev, "FW Client - Max Msg Len = %d\n",
614 dev->me_clients[i].props.max_msg_length);
616 /* if we're connecting to amthi client then we will use the
617 * existing connection
619 if (uuid_le_cmp(data->in_client_uuid, mei_amthi_guid) == 0) {
620 dev_dbg(&dev->pdev->dev, "FW Client is amthi\n");
621 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
625 clear_bit(cl->host_client_id, dev->host_clients_map);
630 file->private_data = &dev->iamthif_cl;
632 client = &data->out_client_properties;
633 client->max_msg_length =
634 dev->me_clients[i].props.max_msg_length;
635 client->protocol_version =
636 dev->me_clients[i].props.protocol_version;
637 rets = dev->iamthif_cl.status;
642 if (cl->state != MEI_FILE_CONNECTING) {
648 /* prepare the output buffer */
649 client = &data->out_client_properties;
650 client->max_msg_length = dev->me_clients[i].props.max_msg_length;
651 client->protocol_version = dev->me_clients[i].props.protocol_version;
652 dev_dbg(&dev->pdev->dev, "Can connect?\n");
655 rets = mei_cl_connect(cl, file);
658 dev_dbg(&dev->pdev->dev, "free connect cb memory.");
664 * mei_ioctl - the IOCTL function
666 * @file: pointer to file structure
667 * @cmd: ioctl command
668 * @data: pointer to mei message structure
670 * returns 0 on success , <0 on error
672 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
674 struct mei_device *dev;
675 struct mei_cl *cl = file->private_data;
676 struct mei_connect_client_data *connect_data = NULL;
679 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
682 if (WARN_ON(!cl || !cl->dev))
687 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
689 mutex_lock(&dev->device_lock);
690 if (dev->dev_state != MEI_DEV_ENABLED) {
695 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
697 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
703 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
704 if (copy_from_user(connect_data, (char __user *)data,
705 sizeof(struct mei_connect_client_data))) {
706 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
711 rets = mei_ioctl_connect_client(file, connect_data);
713 /* if all is ok, copying the data back to user. */
717 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
718 if (copy_to_user((char __user *)data, connect_data,
719 sizeof(struct mei_connect_client_data))) {
720 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
727 mutex_unlock(&dev->device_lock);
732 * mei_compat_ioctl - the compat IOCTL function
734 * @file: pointer to file structure
735 * @cmd: ioctl command
736 * @data: pointer to mei message structure
738 * returns 0 on success , <0 on error
741 static long mei_compat_ioctl(struct file *file,
742 unsigned int cmd, unsigned long data)
744 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
750 * mei_poll - the poll function
752 * @file: pointer to file structure
753 * @wait: pointer to poll_table structure
757 static unsigned int mei_poll(struct file *file, poll_table *wait)
759 struct mei_cl *cl = file->private_data;
760 struct mei_device *dev;
761 unsigned int mask = 0;
763 if (WARN_ON(!cl || !cl->dev))
768 mutex_lock(&dev->device_lock);
770 if (dev->dev_state != MEI_DEV_ENABLED)
774 if (cl == &dev->iamthif_cl) {
775 mask = mei_amthif_poll(dev, file, wait);
779 mutex_unlock(&dev->device_lock);
780 poll_wait(file, &cl->tx_wait, wait);
781 mutex_lock(&dev->device_lock);
782 if (MEI_WRITE_COMPLETE == cl->writing_state)
783 mask |= (POLLIN | POLLRDNORM);
786 mutex_unlock(&dev->device_lock);
791 * file operations structure will be used for mei char device.
793 static const struct file_operations mei_fops = {
794 .owner = THIS_MODULE,
796 .unlocked_ioctl = mei_ioctl,
798 .compat_ioctl = mei_compat_ioctl,
801 .release = mei_release,
811 static struct miscdevice mei_misc_device = {
814 .minor = MISC_DYNAMIC_MINOR,
818 * mei_quirk_probe - probe for devices that doesn't valid ME interface
819 * @pdev: PCI device structure
820 * @ent: entry into pci_device_table
822 * returns true if ME Interface is valid, false otherwise
824 static bool mei_quirk_probe(struct pci_dev *pdev,
825 const struct pci_device_id *ent)
828 if (ent->device == MEI_DEV_ID_PBG_1) {
829 pci_read_config_dword(pdev, 0x48, ®);
830 /* make sure that bit 9 is up and bit 10 is down */
831 if ((reg & 0x600) == 0x200) {
832 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
839 * mei_probe - Device Initialization Routine
841 * @pdev: PCI device structure
842 * @ent: entry in kcs_pci_tbl
844 * returns 0 on success, <0 on failure.
846 static int mei_probe(struct pci_dev *pdev,
847 const struct pci_device_id *ent)
849 struct mei_device *dev;
852 mutex_lock(&mei_mutex);
854 if (!mei_quirk_probe(pdev, ent)) {
864 err = pci_enable_device(pdev);
866 dev_err(&pdev->dev, "failed to enable pci device.\n");
869 /* set PCI host mastering */
870 pci_set_master(pdev);
871 /* pci request regions for mei driver */
872 err = pci_request_regions(pdev, KBUILD_MODNAME);
874 dev_err(&pdev->dev, "failed to get pci regions.\n");
877 /* allocates and initializes the mei dev structure */
878 dev = mei_device_init(pdev);
881 goto release_regions;
883 /* mapping IO device memory */
884 dev->mem_addr = pci_iomap(pdev, 0, 0);
885 if (!dev->mem_addr) {
886 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
890 pci_enable_msi(pdev);
892 /* request and enable interrupt */
893 if (pci_dev_msi_enabled(pdev))
894 err = request_threaded_irq(pdev->irq,
896 mei_interrupt_thread_handler,
897 IRQF_ONESHOT, KBUILD_MODNAME, dev);
899 err = request_threaded_irq(pdev->irq,
900 mei_interrupt_quick_handler,
901 mei_interrupt_thread_handler,
902 IRQF_SHARED, KBUILD_MODNAME, dev);
905 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
909 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
910 INIT_WORK(&dev->init_work, mei_host_client_init);
912 if (mei_hw_init(dev)) {
913 dev_err(&pdev->dev, "init hw failure.\n");
918 err = misc_register(&mei_misc_device);
923 pci_set_drvdata(pdev, dev);
926 schedule_delayed_work(&dev->timer_work, HZ);
928 mutex_unlock(&mei_mutex);
930 pr_debug("initialization successful.\n");
935 /* disable interrupts */
936 dev->host_hw_state = mei_hcsr_read(dev);
937 mei_disable_interrupts(dev);
938 flush_scheduled_work();
939 free_irq(pdev->irq, dev);
941 pci_disable_msi(pdev);
942 pci_iounmap(pdev, dev->mem_addr);
946 pci_release_regions(pdev);
948 pci_disable_device(pdev);
950 mutex_unlock(&mei_mutex);
951 dev_err(&pdev->dev, "initialization failed.\n");
956 * mei_remove - Device Removal Routine
958 * @pdev: PCI device structure
960 * mei_remove is called by the PCI subsystem to alert the driver
961 * that it should release a PCI device.
963 static void mei_remove(struct pci_dev *pdev)
965 struct mei_device *dev;
967 if (mei_pdev != pdev)
970 dev = pci_get_drvdata(pdev);
974 mutex_lock(&dev->device_lock);
976 cancel_delayed_work(&dev->timer_work);
982 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
983 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
984 mei_cl_disconnect(&dev->iamthif_cl);
986 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
987 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
988 mei_cl_disconnect(&dev->wd_cl);
991 /* Unregistering watchdog device */
992 mei_watchdog_unregister(dev);
994 /* remove entry if already in list */
995 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
996 mei_cl_unlink(&dev->wd_cl);
997 mei_cl_unlink(&dev->iamthif_cl);
999 dev->iamthif_current_cb = NULL;
1000 dev->me_clients_num = 0;
1002 mutex_unlock(&dev->device_lock);
1004 flush_scheduled_work();
1006 /* disable interrupts */
1007 mei_disable_interrupts(dev);
1009 free_irq(pdev->irq, dev);
1010 pci_disable_msi(pdev);
1011 pci_set_drvdata(pdev, NULL);
1014 pci_iounmap(pdev, dev->mem_addr);
1018 pci_release_regions(pdev);
1019 pci_disable_device(pdev);
1021 misc_deregister(&mei_misc_device);
1024 static int mei_pci_suspend(struct device *device)
1026 struct pci_dev *pdev = to_pci_dev(device);
1027 struct mei_device *dev = pci_get_drvdata(pdev);
1032 mutex_lock(&dev->device_lock);
1034 cancel_delayed_work(&dev->timer_work);
1036 /* Stop watchdog if exists */
1037 err = mei_wd_stop(dev);
1038 /* Set new mei state */
1039 if (dev->dev_state == MEI_DEV_ENABLED ||
1040 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
1041 dev->dev_state = MEI_DEV_POWER_DOWN;
1044 mutex_unlock(&dev->device_lock);
1046 free_irq(pdev->irq, dev);
1047 pci_disable_msi(pdev);
1052 static int mei_pci_resume(struct device *device)
1054 struct pci_dev *pdev = to_pci_dev(device);
1055 struct mei_device *dev;
1058 dev = pci_get_drvdata(pdev);
1062 pci_enable_msi(pdev);
1064 /* request and enable interrupt */
1065 if (pci_dev_msi_enabled(pdev))
1066 err = request_threaded_irq(pdev->irq,
1068 mei_interrupt_thread_handler,
1069 IRQF_ONESHOT, KBUILD_MODNAME, dev);
1071 err = request_threaded_irq(pdev->irq,
1072 mei_interrupt_quick_handler,
1073 mei_interrupt_thread_handler,
1074 IRQF_SHARED, KBUILD_MODNAME, dev);
1077 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
1082 mutex_lock(&dev->device_lock);
1083 dev->dev_state = MEI_DEV_POWER_UP;
1085 mutex_unlock(&dev->device_lock);
1087 /* Start timer if stopped in suspend */
1088 schedule_delayed_work(&dev->timer_work, HZ);
1092 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1093 #define MEI_PM_OPS (&mei_pm_ops)
1095 #define MEI_PM_OPS NULL
1096 #endif /* CONFIG_PM */
1098 * PCI driver structure
1100 static struct pci_driver mei_driver = {
1101 .name = KBUILD_MODNAME,
1102 .id_table = mei_pci_tbl,
1104 .remove = mei_remove,
1105 .shutdown = mei_remove,
1106 .driver.pm = MEI_PM_OPS,
1109 module_pci_driver(mei_driver);
1111 MODULE_AUTHOR("Intel Corporation");
1112 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1113 MODULE_LICENSE("GPL v2");