3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2011, 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 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/device.h>
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/fcntl.h>
25 #include <linux/aio.h>
26 #include <linux/pci.h>
27 #include <linux/poll.h>
28 #include <linux/init.h>
29 #include <linux/ioctl.h>
30 #include <linux/cdev.h>
31 #include <linux/sched.h>
32 #include <linux/uuid.h>
33 #include <linux/compat.h>
34 #include <linux/jiffies.h>
35 #include <linux/interrupt.h>
36 #include <linux/miscdevice.h>
40 #include "interface.h"
41 #include "mei_version.h"
44 #define MEI_READ_TIMEOUT 45
45 #define MEI_DRIVER_NAME "mei"
46 #define MEI_DEV_NAME "mei"
51 static char mei_driver_name[] = MEI_DRIVER_NAME;
52 static const char mei_driver_string[] = "Intel(R) Management Engine Interface";
53 static const char mei_driver_version[] = MEI_DRIVER_VERSION;
55 /* The device pointer */
56 /* Currently this driver works as long as there is only a single AMT device. */
57 struct pci_dev *mei_device;
59 /* mei_pci_tbl - PCI Device ID Table */
60 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
82 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
83 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
84 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
85 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
86 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
87 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
88 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
89 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
90 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
91 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
93 /* required last entry */
97 MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
99 static DEFINE_MUTEX(mei_mutex);
103 * mei_clear_list - removes all callbacks associated with file
106 * @dev: device structure.
107 * @file: file structure
108 * @mei_cb_list: callbacks list
110 * mei_clear_list is called to clear resources associated with file
111 * when application calls close function or Ctrl-C was pressed
113 * returns true if callback removed from the list, false otherwise
115 static bool mei_clear_list(struct mei_device *dev,
116 struct file *file, struct list_head *mei_cb_list)
118 struct mei_cl_cb *cb_pos = NULL;
119 struct mei_cl_cb *cb_next = NULL;
120 struct file *file_temp;
121 bool removed = false;
123 /* list all list member */
124 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
125 file_temp = (struct file *)cb_pos->file_object;
126 /* check if list member associated with a file */
127 if (file_temp == file) {
128 /* remove member from the list */
129 list_del(&cb_pos->cb_list);
130 /* check if cb equal to current iamthif cb */
131 if (dev->iamthif_current_cb == cb_pos) {
132 dev->iamthif_current_cb = NULL;
133 /* send flow control to iamthif client */
134 mei_send_flow_control(dev, &dev->iamthif_cl);
136 /* free all allocated buffers */
137 mei_free_cb_private(cb_pos);
146 * mei_clear_lists - removes all callbacks associated with file
148 * @dev: device structure
149 * @file: file structure
151 * mei_clear_lists is called to clear resources associated with file
152 * when application calls close function or Ctrl-C was pressed
154 * returns true if callback removed from the list, false otherwise
156 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
158 bool removed = false;
160 /* remove callbacks associated with a file */
161 mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
162 if (mei_clear_list(dev, file,
163 &dev->amthi_read_complete_list.mei_cb.cb_list))
166 mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
168 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
171 if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
174 if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
177 /* check if iamthif_current_cb not NULL */
178 if (dev->iamthif_current_cb && !removed) {
179 /* check file and iamthif current cb association */
180 if (dev->iamthif_current_cb->file_object == file) {
182 mei_free_cb_private(dev->iamthif_current_cb);
183 dev->iamthif_current_cb = NULL;
190 * find_read_list_entry - find read list entry
192 * @dev: device structure
193 * @file: pointer to file structure
195 * returns cb on success, NULL on error
197 static struct mei_cl_cb *find_read_list_entry(
198 struct mei_device *dev,
201 struct mei_cl_cb *pos = NULL;
202 struct mei_cl_cb *next = NULL;
204 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
205 list_for_each_entry_safe(pos, next,
206 &dev->read_list.mei_cb.cb_list, cb_list) {
207 struct mei_cl *cl_temp;
208 cl_temp = (struct mei_cl *)pos->file_private;
210 if (mei_cl_cmp_id(cl, cl_temp))
217 * mei_open - the open function
219 * @inode: pointer to inode structure
220 * @file: pointer to file structure
222 * returns 0 on success, <0 on error
224 static int mei_open(struct inode *inode, struct file *file)
227 struct mei_device *dev;
235 dev = pci_get_drvdata(mei_device);
239 mutex_lock(&dev->device_lock);
241 cl = mei_cl_allocate(dev);
246 if (dev->mei_state != MEI_ENABLED) {
247 dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n",
252 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
255 cl_id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
256 if (cl_id >= MEI_CLIENTS_MAX)
259 cl->host_client_id = cl_id;
261 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
263 dev->open_handle_count++;
265 list_add_tail(&cl->link, &dev->file_list);
267 set_bit(cl->host_client_id, dev->host_clients_map);
268 cl->state = MEI_FILE_INITIALIZING;
271 file->private_data = cl;
272 mutex_unlock(&dev->device_lock);
274 return nonseekable_open(inode, file);
277 mutex_unlock(&dev->device_lock);
284 * mei_release - the release function
286 * @inode: pointer to inode structure
287 * @file: pointer to file structure
289 * returns 0 on success, <0 on error
291 static int mei_release(struct inode *inode, struct file *file)
293 struct mei_cl *cl = file->private_data;
294 struct mei_cl_cb *cb;
295 struct mei_device *dev;
298 if (WARN_ON(!cl || !cl->dev))
303 mutex_lock(&dev->device_lock);
304 if (cl != &dev->iamthif_cl) {
305 if (cl->state == MEI_FILE_CONNECTED) {
306 cl->state = MEI_FILE_DISCONNECTING;
307 dev_dbg(&dev->pdev->dev,
308 "disconnecting client host client = %d, "
312 rets = mei_disconnect_host_client(dev, cl);
314 mei_cl_flush_queues(cl);
315 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
319 if (dev->open_handle_count > 0) {
320 clear_bit(cl->host_client_id, dev->host_clients_map);
321 dev->open_handle_count--;
323 mei_remove_client_from_file_list(dev, cl->host_client_id);
328 cb = find_read_list_entry(dev, cl);
329 /* Remove entry from read list */
331 list_del(&cb->cb_list);
337 file->private_data = NULL;
340 mei_free_cb_private(cb);
346 if (dev->open_handle_count > 0)
347 dev->open_handle_count--;
349 if (dev->iamthif_file_object == file &&
350 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
352 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
354 dev->iamthif_canceled = true;
355 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
356 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
357 mei_run_next_iamthif_cmd(dev);
361 if (mei_clear_lists(dev, file))
362 dev->iamthif_state = MEI_IAMTHIF_IDLE;
365 mutex_unlock(&dev->device_lock);
371 * mei_read - the read function.
373 * @file: pointer to file structure
374 * @ubuf: pointer to user buffer
375 * @length: buffer length
376 * @offset: data offset in buffer
378 * returns >=0 data length on success , <0 on error
380 static ssize_t mei_read(struct file *file, char __user *ubuf,
381 size_t length, loff_t *offset)
383 struct mei_cl *cl = file->private_data;
384 struct mei_cl_cb *cb_pos = NULL;
385 struct mei_cl_cb *cb = NULL;
386 struct mei_device *dev;
392 if (WARN_ON(!cl || !cl->dev))
397 mutex_lock(&dev->device_lock);
398 if (dev->mei_state != MEI_ENABLED) {
403 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
404 /* Do not allow to read watchdog client */
405 i = mei_find_me_client_index(dev, mei_wd_guid);
407 struct mei_me_client *me_client = &dev->me_clients[i];
409 if (cl->me_client_id == me_client->client_id) {
415 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
418 if (cl == &dev->iamthif_cl) {
419 rets = amthi_read(dev, file, ubuf, length, offset);
423 if (cl->read_cb && cl->read_cb->information > *offset) {
426 } else if (cl->read_cb && cl->read_cb->information > 0 &&
427 cl->read_cb->information <= *offset) {
431 } else if ((!cl->read_cb || !cl->read_cb->information) &&
433 /*Offset needs to be cleaned for contingous reads*/
439 err = mei_start_read(dev, cl);
440 if (err && err != -EBUSY) {
441 dev_dbg(&dev->pdev->dev,
442 "mei start read failure with status = %d\n", err);
447 if (MEI_READ_COMPLETE != cl->reading_state &&
448 !waitqueue_active(&cl->rx_wait)) {
449 if (file->f_flags & O_NONBLOCK) {
454 mutex_unlock(&dev->device_lock);
456 if (wait_event_interruptible(cl->rx_wait,
457 (MEI_READ_COMPLETE == cl->reading_state ||
458 MEI_FILE_INITIALIZING == cl->state ||
459 MEI_FILE_DISCONNECTED == cl->state ||
460 MEI_FILE_DISCONNECTING == cl->state))) {
461 if (signal_pending(current))
466 mutex_lock(&dev->device_lock);
467 if (MEI_FILE_INITIALIZING == cl->state ||
468 MEI_FILE_DISCONNECTED == cl->state ||
469 MEI_FILE_DISCONNECTING == cl->state) {
481 if (cl->reading_state != MEI_READ_COMPLETE) {
485 /* now copy the data to user space */
487 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
488 cb->response_buffer.size);
489 dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
491 if (length == 0 || ubuf == NULL || *offset > cb->information) {
496 /* length is being turncated to PAGE_SIZE, however, */
497 /* information size may be longer */
498 length = min_t(size_t, length, (cb->information - *offset));
500 if (copy_to_user(ubuf, cb->response_buffer.data + *offset, length)) {
507 if ((unsigned long)*offset < cb->information)
511 cb_pos = find_read_list_entry(dev, cl);
512 /* Remove entry from read list */
514 list_del(&cb_pos->cb_list);
515 mei_free_cb_private(cb);
516 cl->reading_state = MEI_IDLE;
518 cl->read_pending = 0;
520 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
521 mutex_unlock(&dev->device_lock);
526 * mei_write - the write function.
528 * @file: pointer to file structure
529 * @ubuf: pointer to user buffer
530 * @length: buffer length
531 * @offset: data offset in buffer
533 * returns >=0 data length on success , <0 on error
535 static ssize_t mei_write(struct file *file, const char __user *ubuf,
536 size_t length, loff_t *offset)
538 struct mei_cl *cl = file->private_data;
539 struct mei_cl_cb *write_cb = NULL;
540 struct mei_msg_hdr mei_hdr;
541 struct mei_device *dev;
542 unsigned long timeout = 0;
546 if (WARN_ON(!cl || !cl->dev))
551 mutex_lock(&dev->device_lock);
553 if (dev->mei_state != MEI_ENABLED) {
554 mutex_unlock(&dev->device_lock);
558 if (cl == &dev->iamthif_cl) {
559 write_cb = find_amthi_read_list_entry(dev, file);
562 timeout = write_cb->read_time +
563 msecs_to_jiffies(IAMTHIF_READ_TIMER);
565 if (time_after(jiffies, timeout) ||
566 cl->reading_state == MEI_READ_COMPLETE) {
568 list_del(&write_cb->cb_list);
569 mei_free_cb_private(write_cb);
575 /* free entry used in read */
576 if (cl->reading_state == MEI_READ_COMPLETE) {
578 write_cb = find_read_list_entry(dev, cl);
580 list_del(&write_cb->cb_list);
581 mei_free_cb_private(write_cb);
583 cl->reading_state = MEI_IDLE;
585 cl->read_pending = 0;
587 } else if (cl->reading_state == MEI_IDLE && !cl->read_pending)
591 write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
593 mutex_unlock(&dev->device_lock);
597 write_cb->file_object = file;
598 write_cb->file_private = cl;
599 write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
601 if (!write_cb->request_buffer.data)
604 dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
607 if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
612 ((memcmp(mei_wd_state_independence_msg[0],
613 write_cb->request_buffer.data, 4) == 0) ||
614 (memcmp(mei_wd_state_independence_msg[1],
615 write_cb->request_buffer.data, 4) == 0) ||
616 (memcmp(mei_wd_state_independence_msg[2],
617 write_cb->request_buffer.data, 4) == 0)))
618 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
620 INIT_LIST_HEAD(&write_cb->cb_list);
621 if (cl == &dev->iamthif_cl) {
622 write_cb->response_buffer.data =
623 kmalloc(dev->iamthif_mtu, GFP_KERNEL);
624 if (!write_cb->response_buffer.data) {
628 if (dev->mei_state != MEI_ENABLED) {
632 for (i = 0; i < dev->me_clients_num; i++) {
633 if (dev->me_clients[i].client_id ==
634 dev->iamthif_cl.me_client_id)
638 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
642 if (i == dev->me_clients_num ||
643 (dev->me_clients[i].client_id !=
644 dev->iamthif_cl.me_client_id)) {
647 } else if (length > dev->me_clients[i].props.max_msg_length ||
653 write_cb->response_buffer.size = dev->iamthif_mtu;
654 write_cb->major_file_operations = MEI_IOCTL;
655 write_cb->information = 0;
656 write_cb->request_buffer.size = length;
657 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
662 if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
663 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
664 dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
665 (int) dev->iamthif_state);
666 dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
667 list_add_tail(&write_cb->cb_list,
668 &dev->amthi_cmd_list.mei_cb.cb_list);
671 dev_dbg(&dev->pdev->dev, "call amthi write\n");
672 rets = amthi_write(dev, write_cb);
675 dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
681 mutex_unlock(&dev->device_lock);
685 write_cb->major_file_operations = MEI_WRITE;
686 /* make sure information is zero before we start */
688 write_cb->information = 0;
689 write_cb->request_buffer.size = length;
691 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
692 cl->host_client_id, cl->me_client_id);
693 if (cl->state != MEI_FILE_CONNECTED) {
695 dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
700 for (i = 0; i < dev->me_clients_num; i++) {
701 if (dev->me_clients[i].client_id ==
705 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
709 if (i == dev->me_clients_num) {
713 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
717 write_cb->file_private = cl;
719 rets = mei_flow_ctrl_creds(dev, cl);
723 if (rets && dev->mei_host_buffer_is_empty) {
725 dev->mei_host_buffer_is_empty = false;
726 if (length > ((((dev->host_hw_state & H_CBD) >> 24) *
727 sizeof(u32)) - sizeof(struct mei_msg_hdr))) {
730 (((dev->host_hw_state & H_CBD) >> 24) *
732 sizeof(struct mei_msg_hdr);
733 mei_hdr.msg_complete = 0;
735 mei_hdr.length = length;
736 mei_hdr.msg_complete = 1;
738 mei_hdr.host_addr = cl->host_client_id;
739 mei_hdr.me_addr = cl->me_client_id;
740 mei_hdr.reserved = 0;
741 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
742 *((u32 *) &mei_hdr));
743 if (!mei_write_message(dev, &mei_hdr,
744 (unsigned char *) (write_cb->request_buffer.data),
749 cl->writing_state = MEI_WRITING;
750 write_cb->information = mei_hdr.length;
751 if (mei_hdr.msg_complete) {
752 if (mei_flow_ctrl_reduce(dev, cl)) {
756 list_add_tail(&write_cb->cb_list,
757 &dev->write_waiting_list.mei_cb.cb_list);
759 list_add_tail(&write_cb->cb_list,
760 &dev->write_list.mei_cb.cb_list);
765 write_cb->information = 0;
766 cl->writing_state = MEI_WRITING;
767 list_add_tail(&write_cb->cb_list,
768 &dev->write_list.mei_cb.cb_list);
770 mutex_unlock(&dev->device_lock);
774 mutex_unlock(&dev->device_lock);
775 mei_free_cb_private(write_cb);
781 * mei_ioctl - the IOCTL function
783 * @file: pointer to file structure
784 * @cmd: ioctl command
785 * @data: pointer to mei message structure
787 * returns 0 on success , <0 on error
789 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
791 struct mei_device *dev;
792 struct mei_cl *cl = file->private_data;
793 struct mei_connect_client_data *connect_data = NULL;
796 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
799 if (WARN_ON(!cl || !cl->dev))
804 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
806 mutex_lock(&dev->device_lock);
807 if (dev->mei_state != MEI_ENABLED) {
812 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
814 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
820 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
821 if (copy_from_user(connect_data, (char __user *)data,
822 sizeof(struct mei_connect_client_data))) {
823 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
827 rets = mei_ioctl_connect_client(file, connect_data);
829 /* if all is ok, copying the data back to user. */
833 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
834 if (copy_to_user((char __user *)data, connect_data,
835 sizeof(struct mei_connect_client_data))) {
836 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
843 mutex_unlock(&dev->device_lock);
848 * mei_compat_ioctl - the compat IOCTL function
850 * @file: pointer to file structure
851 * @cmd: ioctl command
852 * @data: pointer to mei message structure
854 * returns 0 on success , <0 on error
857 static long mei_compat_ioctl(struct file *file,
858 unsigned int cmd, unsigned long data)
860 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
866 * mei_poll - the poll function
868 * @file: pointer to file structure
869 * @wait: pointer to poll_table structure
873 static unsigned int mei_poll(struct file *file, poll_table *wait)
875 struct mei_cl *cl = file->private_data;
876 struct mei_device *dev;
877 unsigned int mask = 0;
879 if (WARN_ON(!cl || !cl->dev))
884 mutex_lock(&dev->device_lock);
886 if (dev->mei_state != MEI_ENABLED)
890 if (cl == &dev->iamthif_cl) {
891 mutex_unlock(&dev->device_lock);
892 poll_wait(file, &dev->iamthif_cl.wait, wait);
893 mutex_lock(&dev->device_lock);
894 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
895 dev->iamthif_file_object == file) {
896 mask |= (POLLIN | POLLRDNORM);
897 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
898 mei_run_next_iamthif_cmd(dev);
903 mutex_unlock(&dev->device_lock);
904 poll_wait(file, &cl->tx_wait, wait);
905 mutex_lock(&dev->device_lock);
906 if (MEI_WRITE_COMPLETE == cl->writing_state)
907 mask |= (POLLIN | POLLRDNORM);
910 mutex_unlock(&dev->device_lock);
915 * file operations structure will be used for mei char device.
917 static const struct file_operations mei_fops = {
918 .owner = THIS_MODULE,
920 .unlocked_ioctl = mei_ioctl,
922 .compat_ioctl = mei_compat_ioctl,
925 .release = mei_release,
935 static struct miscdevice mei_misc_device = {
936 .name = MEI_DRIVER_NAME,
938 .minor = MISC_DYNAMIC_MINOR,
942 * mei_probe - Device Initialization Routine
944 * @pdev: PCI device structure
945 * @ent: entry in kcs_pci_tbl
947 * returns 0 on success, <0 on failure.
949 static int __devinit mei_probe(struct pci_dev *pdev,
950 const struct pci_device_id *ent)
952 struct mei_device *dev;
955 mutex_lock(&mei_mutex);
961 err = pci_enable_device(pdev);
963 printk(KERN_ERR "mei: Failed to enable pci device.\n");
966 /* set PCI host mastering */
967 pci_set_master(pdev);
968 /* pci request regions for mei driver */
969 err = pci_request_regions(pdev, mei_driver_name);
971 printk(KERN_ERR "mei: Failed to get pci regions.\n");
974 /* allocates and initializes the mei dev structure */
975 dev = mei_device_init(pdev);
978 goto release_regions;
980 /* mapping IO device memory */
981 dev->mem_addr = pci_iomap(pdev, 0, 0);
982 if (!dev->mem_addr) {
983 printk(KERN_ERR "mei: mapping I/O device memory failure.\n");
987 pci_enable_msi(pdev);
989 /* request and enable interrupt */
990 if (pci_dev_msi_enabled(pdev))
991 err = request_threaded_irq(pdev->irq,
993 mei_interrupt_thread_handler,
994 0, mei_driver_name, dev);
996 err = request_threaded_irq(pdev->irq,
997 mei_interrupt_quick_handler,
998 mei_interrupt_thread_handler,
999 IRQF_SHARED, mei_driver_name, dev);
1002 printk(KERN_ERR "mei: request_threaded_irq failure. irq = %d\n",
1006 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
1007 if (mei_hw_init(dev)) {
1008 printk(KERN_ERR "mei: Init hw failure.\n");
1013 err = misc_register(&mei_misc_device);
1018 pci_set_drvdata(pdev, dev);
1021 schedule_delayed_work(&dev->timer_work, HZ);
1023 mutex_unlock(&mei_mutex);
1025 pr_debug("mei: Driver initialization successful.\n");
1030 /* disable interrupts */
1031 dev->host_hw_state = mei_hcsr_read(dev);
1032 mei_disable_interrupts(dev);
1033 flush_scheduled_work();
1034 free_irq(pdev->irq, dev);
1035 pci_disable_msi(pdev);
1037 pci_iounmap(pdev, dev->mem_addr);
1041 pci_release_regions(pdev);
1043 pci_disable_device(pdev);
1045 mutex_unlock(&mei_mutex);
1046 printk(KERN_ERR "mei: Driver initialization failed.\n");
1051 * mei_remove - Device Removal Routine
1053 * @pdev: PCI device structure
1055 * mei_remove is called by the PCI subsystem to alert the driver
1056 * that it should release a PCI device.
1058 static void __devexit mei_remove(struct pci_dev *pdev)
1060 struct mei_device *dev;
1062 if (mei_device != pdev)
1065 dev = pci_get_drvdata(pdev);
1069 mutex_lock(&dev->device_lock);
1071 mei_wd_stop(dev, false);
1075 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
1076 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
1077 mei_disconnect_host_client(dev, &dev->iamthif_cl);
1079 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
1080 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
1081 mei_disconnect_host_client(dev, &dev->wd_cl);
1084 /* Unregistering watchdog device */
1085 mei_watchdog_unregister(dev);
1087 /* remove entry if already in list */
1088 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
1089 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
1090 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
1092 dev->iamthif_current_cb = NULL;
1093 dev->me_clients_num = 0;
1095 mutex_unlock(&dev->device_lock);
1097 flush_scheduled_work();
1099 /* disable interrupts */
1100 mei_disable_interrupts(dev);
1102 free_irq(pdev->irq, dev);
1103 pci_disable_msi(pdev);
1104 pci_set_drvdata(pdev, NULL);
1107 pci_iounmap(pdev, dev->mem_addr);
1111 pci_release_regions(pdev);
1112 pci_disable_device(pdev);
1115 static int mei_pci_suspend(struct device *device)
1117 struct pci_dev *pdev = to_pci_dev(device);
1118 struct mei_device *dev = pci_get_drvdata(pdev);
1123 mutex_lock(&dev->device_lock);
1124 /* Stop watchdog if exists */
1125 err = mei_wd_stop(dev, true);
1126 /* Set new mei state */
1127 if (dev->mei_state == MEI_ENABLED ||
1128 dev->mei_state == MEI_RECOVERING_FROM_RESET) {
1129 dev->mei_state = MEI_POWER_DOWN;
1132 mutex_unlock(&dev->device_lock);
1134 free_irq(pdev->irq, dev);
1135 pci_disable_msi(pdev);
1140 static int mei_pci_resume(struct device *device)
1142 struct pci_dev *pdev = to_pci_dev(device);
1143 struct mei_device *dev;
1146 dev = pci_get_drvdata(pdev);
1150 pci_enable_msi(pdev);
1152 /* request and enable interrupt */
1153 if (pci_dev_msi_enabled(pdev))
1154 err = request_threaded_irq(pdev->irq,
1156 mei_interrupt_thread_handler,
1157 0, mei_driver_name, dev);
1159 err = request_threaded_irq(pdev->irq,
1160 mei_interrupt_quick_handler,
1161 mei_interrupt_thread_handler,
1162 IRQF_SHARED, mei_driver_name, dev);
1165 printk(KERN_ERR "mei: Request_irq failure. irq = %d\n",
1170 mutex_lock(&dev->device_lock);
1171 dev->mei_state = MEI_POWER_UP;
1173 mutex_unlock(&dev->device_lock);
1175 /* Start timer if stopped in suspend */
1176 schedule_delayed_work(&dev->timer_work, HZ);
1180 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1181 #define MEI_PM_OPS (&mei_pm_ops)
1183 #define MEI_PM_OPS NULL
1184 #endif /* CONFIG_PM */
1186 * PCI driver structure
1188 static struct pci_driver mei_driver = {
1189 .name = mei_driver_name,
1190 .id_table = mei_pci_tbl,
1192 .remove = __devexit_p(mei_remove),
1193 .shutdown = __devexit_p(mei_remove),
1194 .driver.pm = MEI_PM_OPS,
1198 * mei_init_module - Driver Registration Routine
1200 * mei_init_module is the first routine called when the driver is
1201 * loaded. All it does is to register with the PCI subsystem.
1203 * returns 0 on success, <0 on failure.
1205 static int __init mei_init_module(void)
1209 pr_debug("mei: %s - version %s\n",
1210 mei_driver_string, mei_driver_version);
1211 /* init pci module */
1212 ret = pci_register_driver(&mei_driver);
1214 printk(KERN_ERR "mei: Error registering driver.\n");
1219 module_init(mei_init_module);
1222 * mei_exit_module - Driver Exit Cleanup Routine
1224 * mei_exit_module is called just before the driver is removed
1227 static void __exit mei_exit_module(void)
1229 misc_deregister(&mei_misc_device);
1230 pci_unregister_driver(&mei_driver);
1232 pr_debug("mei: Driver unloaded successfully.\n");
1235 module_exit(mei_exit_module);
1238 MODULE_AUTHOR("Intel Corporation");
1239 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1240 MODULE_LICENSE("GPL v2");
1241 MODULE_VERSION(MEI_DRIVER_VERSION);