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>
39 #include "interface.h"
40 #include "mei_version.h"
43 #define MEI_READ_TIMEOUT 45
44 #define MEI_DRIVER_NAME "mei"
45 #define MEI_DEV_NAME "mei"
50 static char mei_driver_name[] = MEI_DRIVER_NAME;
51 static const char mei_driver_string[] = "Intel(R) Management Engine Interface";
52 static const char mei_driver_version[] = MEI_DRIVER_VERSION;
54 /* mei char device for registration */
55 static struct cdev mei_cdev;
57 /* major number for device */
59 /* The device pointer */
60 /* Currently this driver works as long as there is only a single AMT device. */
61 struct pci_dev *mei_device;
63 static struct class *mei_class;
66 /* mei_pci_tbl - PCI Device ID Table */
67 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
82 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
83 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
84 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
85 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
86 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
87 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
88 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
89 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
90 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
91 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
92 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
93 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
94 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
95 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
96 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
97 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
98 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
100 /* required last entry */
104 MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
106 static DEFINE_MUTEX(mei_mutex);
109 * mei_quirk_probe - probe for devices that doesn't valid ME interface
110 * @pdev: PCI device structure
111 * @ent: entry into pci_device_table
113 * returns true if ME Interface is valid, false otherwise
115 static bool __devinit mei_quirk_probe(struct pci_dev *pdev,
116 const struct pci_device_id *ent)
119 if (ent->device == MEI_DEV_ID_PBG_1) {
120 pci_read_config_dword(pdev, 0x48, ®);
121 /* make sure that bit 9 is up and bit 10 is down */
122 if ((reg & 0x600) == 0x200) {
123 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
130 * mei_probe - Device Initialization Routine
132 * @pdev: PCI device structure
133 * @ent: entry in kcs_pci_tbl
135 * returns 0 on success, <0 on failure.
137 static int __devinit mei_probe(struct pci_dev *pdev,
138 const struct pci_device_id *ent)
140 struct mei_device *dev;
143 mutex_lock(&mei_mutex);
145 if (!mei_quirk_probe(pdev, ent)) {
155 err = pci_enable_device(pdev);
157 printk(KERN_ERR "mei: Failed to enable pci device.\n");
160 /* set PCI host mastering */
161 pci_set_master(pdev);
162 /* pci request regions for mei driver */
163 err = pci_request_regions(pdev, mei_driver_name);
165 printk(KERN_ERR "mei: Failed to get pci regions.\n");
168 /* allocates and initializes the mei dev structure */
169 dev = mei_device_init(pdev);
172 goto release_regions;
174 /* mapping IO device memory */
175 dev->mem_addr = pci_iomap(pdev, 0, 0);
176 if (!dev->mem_addr) {
177 printk(KERN_ERR "mei: mapping I/O device memory failure.\n");
181 pci_enable_msi(pdev);
183 /* request and enable interrupt */
184 if (pci_dev_msi_enabled(pdev))
185 err = request_threaded_irq(pdev->irq,
187 mei_interrupt_thread_handler,
188 0, mei_driver_name, dev);
190 err = request_threaded_irq(pdev->irq,
191 mei_interrupt_quick_handler,
192 mei_interrupt_thread_handler,
193 IRQF_SHARED, mei_driver_name, dev);
196 printk(KERN_ERR "mei: request_threaded_irq failure. irq = %d\n",
200 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
201 if (mei_hw_init(dev)) {
202 printk(KERN_ERR "mei: Init hw failure.\n");
207 pci_set_drvdata(pdev, dev);
208 schedule_delayed_work(&dev->timer_work, HZ);
210 mutex_unlock(&mei_mutex);
212 pr_debug("mei: Driver initialization successful.\n");
217 /* disable interrupts */
218 dev->host_hw_state = mei_hcsr_read(dev);
219 mei_disable_interrupts(dev);
220 flush_scheduled_work();
221 free_irq(pdev->irq, dev);
222 pci_disable_msi(pdev);
224 pci_iounmap(pdev, dev->mem_addr);
228 pci_release_regions(pdev);
230 pci_disable_device(pdev);
232 mutex_unlock(&mei_mutex);
233 printk(KERN_ERR "mei: Driver initialization failed.\n");
238 * mei_remove - Device Removal Routine
240 * @pdev: PCI device structure
242 * mei_remove is called by the PCI subsystem to alert the driver
243 * that it should release a PCI device.
245 static void __devexit mei_remove(struct pci_dev *pdev)
247 struct mei_device *dev;
249 if (mei_device != pdev)
252 dev = pci_get_drvdata(pdev);
256 mutex_lock(&dev->device_lock);
258 mei_wd_stop(dev, false);
262 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
263 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
264 mei_disconnect_host_client(dev, &dev->iamthif_cl);
266 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
267 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
268 mei_disconnect_host_client(dev, &dev->wd_cl);
271 /* Unregistering watchdog device */
272 if (dev->wd_interface_reg)
273 watchdog_unregister_device(&amt_wd_dev);
275 /* remove entry if already in list */
276 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
277 mei_remove_client_from_file_list(dev, dev->wd_cl.host_client_id);
278 mei_remove_client_from_file_list(dev, dev->iamthif_cl.host_client_id);
280 dev->iamthif_current_cb = NULL;
281 dev->me_clients_num = 0;
283 mutex_unlock(&dev->device_lock);
285 flush_scheduled_work();
287 /* disable interrupts */
288 mei_disable_interrupts(dev);
290 free_irq(pdev->irq, dev);
291 pci_disable_msi(pdev);
292 pci_set_drvdata(pdev, NULL);
295 pci_iounmap(pdev, dev->mem_addr);
299 pci_release_regions(pdev);
300 pci_disable_device(pdev);
304 * mei_clear_list - removes all callbacks associated with file
307 * @dev: device structure.
308 * @file: file structure
309 * @mei_cb_list: callbacks list
311 * mei_clear_list is called to clear resources associated with file
312 * when application calls close function or Ctrl-C was pressed
314 * returns true if callback removed from the list, false otherwise
316 static bool mei_clear_list(struct mei_device *dev,
317 struct file *file, struct list_head *mei_cb_list)
319 struct mei_cl_cb *cb_pos = NULL;
320 struct mei_cl_cb *cb_next = NULL;
321 struct file *file_temp;
322 bool removed = false;
324 /* list all list member */
325 list_for_each_entry_safe(cb_pos, cb_next, mei_cb_list, cb_list) {
326 file_temp = (struct file *)cb_pos->file_object;
327 /* check if list member associated with a file */
328 if (file_temp == file) {
329 /* remove member from the list */
330 list_del(&cb_pos->cb_list);
331 /* check if cb equal to current iamthif cb */
332 if (dev->iamthif_current_cb == cb_pos) {
333 dev->iamthif_current_cb = NULL;
334 /* send flow control to iamthif client */
335 mei_send_flow_control(dev, &dev->iamthif_cl);
337 /* free all allocated buffers */
338 mei_free_cb_private(cb_pos);
347 * mei_clear_lists - removes all callbacks associated with file
349 * @dev: device structure
350 * @file: file structure
352 * mei_clear_lists is called to clear resources associated with file
353 * when application calls close function or Ctrl-C was pressed
355 * returns true if callback removed from the list, false otherwise
357 static bool mei_clear_lists(struct mei_device *dev, struct file *file)
359 bool removed = false;
361 /* remove callbacks associated with a file */
362 mei_clear_list(dev, file, &dev->amthi_cmd_list.mei_cb.cb_list);
363 if (mei_clear_list(dev, file,
364 &dev->amthi_read_complete_list.mei_cb.cb_list))
367 mei_clear_list(dev, file, &dev->ctrl_rd_list.mei_cb.cb_list);
369 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.mei_cb.cb_list))
372 if (mei_clear_list(dev, file, &dev->write_waiting_list.mei_cb.cb_list))
375 if (mei_clear_list(dev, file, &dev->write_list.mei_cb.cb_list))
378 /* check if iamthif_current_cb not NULL */
379 if (dev->iamthif_current_cb && !removed) {
380 /* check file and iamthif current cb association */
381 if (dev->iamthif_current_cb->file_object == file) {
383 mei_free_cb_private(dev->iamthif_current_cb);
384 dev->iamthif_current_cb = NULL;
391 * find_read_list_entry - find read list entry
393 * @dev: device structure
394 * @file: pointer to file structure
396 * returns cb on success, NULL on error
398 static struct mei_cl_cb *find_read_list_entry(
399 struct mei_device *dev,
402 struct mei_cl_cb *cb_pos = NULL;
403 struct mei_cl_cb *cb_next = NULL;
405 if (!dev->read_list.status &&
406 !list_empty(&dev->read_list.mei_cb.cb_list)) {
408 dev_dbg(&dev->pdev->dev, "remove read_list CB\n");
409 list_for_each_entry_safe(cb_pos, cb_next,
410 &dev->read_list.mei_cb.cb_list, cb_list) {
411 struct mei_cl *cl_temp;
412 cl_temp = (struct mei_cl *)cb_pos->file_private;
414 if (mei_cl_cmp_id(cl, cl_temp))
422 * mei_open - the open function
424 * @inode: pointer to inode structure
425 * @file: pointer to file structure
427 * returns 0 on success, <0 on error
429 static int mei_open(struct inode *inode, struct file *file)
432 int if_num = iminor(inode), err;
433 struct mei_device *dev;
439 dev = pci_get_drvdata(mei_device);
440 if (if_num != MEI_MINOR_NUMBER || !dev)
443 mutex_lock(&dev->device_lock);
445 cl = mei_cl_allocate(dev);
450 if (dev->mei_state != MEI_ENABLED) {
451 dev_dbg(&dev->pdev->dev, "mei_state != MEI_ENABLED mei_state= %d\n",
456 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT)
459 cl->host_client_id = find_first_zero_bit(dev->host_clients_map,
461 if (cl->host_client_id > MEI_CLIENTS_MAX)
464 dev_dbg(&dev->pdev->dev, "client_id = %d\n", cl->host_client_id);
466 dev->open_handle_count++;
467 list_add_tail(&cl->link, &dev->file_list);
469 set_bit(cl->host_client_id, dev->host_clients_map);
470 cl->state = MEI_FILE_INITIALIZING;
473 file->private_data = cl;
474 mutex_unlock(&dev->device_lock);
479 mutex_unlock(&dev->device_lock);
486 * mei_release - the release function
488 * @inode: pointer to inode structure
489 * @file: pointer to file structure
491 * returns 0 on success, <0 on error
493 static int mei_release(struct inode *inode, struct file *file)
495 struct mei_cl *cl = file->private_data;
496 struct mei_cl_cb *cb;
497 struct mei_device *dev;
500 if (WARN_ON(!cl || !cl->dev))
505 mutex_lock(&dev->device_lock);
506 if (cl != &dev->iamthif_cl) {
507 if (cl->state == MEI_FILE_CONNECTED) {
508 cl->state = MEI_FILE_DISCONNECTING;
509 dev_dbg(&dev->pdev->dev,
510 "disconnecting client host client = %d, "
514 rets = mei_disconnect_host_client(dev, cl);
516 mei_cl_flush_queues(cl);
517 dev_dbg(&dev->pdev->dev, "remove client host client = %d, ME client = %d\n",
521 if (dev->open_handle_count > 0) {
522 clear_bit(cl->host_client_id,
523 dev->host_clients_map);
524 dev->open_handle_count--;
526 mei_remove_client_from_file_list(dev, cl->host_client_id);
531 cb = find_read_list_entry(dev, cl);
532 /* Remove entry from read list */
534 list_del(&cb->cb_list);
540 file->private_data = NULL;
543 mei_free_cb_private(cb);
549 if (dev->open_handle_count > 0)
550 dev->open_handle_count--;
552 if (dev->iamthif_file_object == file &&
553 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
555 dev_dbg(&dev->pdev->dev, "amthi canceled iamthif state %d\n",
557 dev->iamthif_canceled = true;
558 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
559 dev_dbg(&dev->pdev->dev, "run next amthi iamthif cb\n");
560 mei_run_next_iamthif_cmd(dev);
564 if (mei_clear_lists(dev, file))
565 dev->iamthif_state = MEI_IAMTHIF_IDLE;
568 mutex_unlock(&dev->device_lock);
574 * mei_read - the read function.
576 * @file: pointer to file structure
577 * @ubuf: pointer to user buffer
578 * @length: buffer length
579 * @offset: data offset in buffer
581 * returns >=0 data length on success , <0 on error
583 static ssize_t mei_read(struct file *file, char __user *ubuf,
584 size_t length, loff_t *offset)
586 struct mei_cl *cl = file->private_data;
587 struct mei_cl_cb *cb_pos = NULL;
588 struct mei_cl_cb *cb = NULL;
589 struct mei_device *dev;
595 if (WARN_ON(!cl || !cl->dev))
600 mutex_lock(&dev->device_lock);
601 if (dev->mei_state != MEI_ENABLED) {
606 if ((cl->sm_state & MEI_WD_STATE_INDEPENDENCE_MSG_SENT) == 0) {
607 /* Do not allow to read watchdog client */
608 i = mei_find_me_client_index(dev, mei_wd_guid);
610 struct mei_me_client *me_client = &dev->me_clients[i];
612 if (cl->me_client_id == me_client->client_id) {
618 cl->sm_state &= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
621 if (cl == &dev->iamthif_cl) {
622 rets = amthi_read(dev, file, ubuf, length, offset);
626 if (cl->read_cb && cl->read_cb->information > *offset) {
629 } else if (cl->read_cb && cl->read_cb->information > 0 &&
630 cl->read_cb->information <= *offset) {
634 } else if ((!cl->read_cb || !cl->read_cb->information) &&
636 /*Offset needs to be cleaned for contingous reads*/
642 err = mei_start_read(dev, cl);
643 if (err && err != -EBUSY) {
644 dev_dbg(&dev->pdev->dev,
645 "mei start read failure with status = %d\n", err);
650 if (MEI_READ_COMPLETE != cl->reading_state &&
651 !waitqueue_active(&cl->rx_wait)) {
652 if (file->f_flags & O_NONBLOCK) {
657 mutex_unlock(&dev->device_lock);
659 if (wait_event_interruptible(cl->rx_wait,
660 (MEI_READ_COMPLETE == cl->reading_state ||
661 MEI_FILE_INITIALIZING == cl->state ||
662 MEI_FILE_DISCONNECTED == cl->state ||
663 MEI_FILE_DISCONNECTING == cl->state))) {
664 if (signal_pending(current))
669 mutex_lock(&dev->device_lock);
670 if (MEI_FILE_INITIALIZING == cl->state ||
671 MEI_FILE_DISCONNECTED == cl->state ||
672 MEI_FILE_DISCONNECTING == cl->state) {
684 if (cl->reading_state != MEI_READ_COMPLETE) {
688 /* now copy the data to user space */
690 dev_dbg(&dev->pdev->dev, "cb->response_buffer size - %d\n",
691 cb->response_buffer.size);
692 dev_dbg(&dev->pdev->dev, "cb->information - %lu\n",
694 if (length == 0 || ubuf == NULL || *offset > cb->information) {
699 /* length is being turncated to PAGE_SIZE, however, */
700 /* information size may be longer */
701 length = min_t(size_t, length, (cb->information - *offset));
703 if (copy_to_user(ubuf,
704 cb->response_buffer.data + *offset,
712 if ((unsigned long)*offset < cb->information)
716 cb_pos = find_read_list_entry(dev, cl);
717 /* Remove entry from read list */
719 list_del(&cb_pos->cb_list);
720 mei_free_cb_private(cb);
721 cl->reading_state = MEI_IDLE;
723 cl->read_pending = 0;
725 dev_dbg(&dev->pdev->dev, "end mei read rets= %d\n", rets);
726 mutex_unlock(&dev->device_lock);
731 * mei_write - the write function.
733 * @file: pointer to file structure
734 * @ubuf: pointer to user buffer
735 * @length: buffer length
736 * @offset: data offset in buffer
738 * returns >=0 data length on success , <0 on error
740 static ssize_t mei_write(struct file *file, const char __user *ubuf,
741 size_t length, loff_t *offset)
743 struct mei_cl *cl = file->private_data;
744 struct mei_cl_cb *write_cb = NULL;
745 struct mei_msg_hdr mei_hdr;
746 struct mei_device *dev;
747 unsigned long timeout = 0;
751 if (WARN_ON(!cl || !cl->dev))
756 mutex_lock(&dev->device_lock);
758 if (dev->mei_state != MEI_ENABLED) {
759 mutex_unlock(&dev->device_lock);
763 if (cl == &dev->iamthif_cl) {
764 write_cb = find_amthi_read_list_entry(dev, file);
767 timeout = write_cb->read_time +
768 msecs_to_jiffies(IAMTHIF_READ_TIMER);
770 if (time_after(jiffies, timeout) ||
771 cl->reading_state == MEI_READ_COMPLETE) {
773 list_del(&write_cb->cb_list);
774 mei_free_cb_private(write_cb);
780 /* free entry used in read */
781 if (cl->reading_state == MEI_READ_COMPLETE) {
783 write_cb = find_read_list_entry(dev, cl);
785 list_del(&write_cb->cb_list);
786 mei_free_cb_private(write_cb);
788 cl->reading_state = MEI_IDLE;
790 cl->read_pending = 0;
792 } else if (cl->reading_state == MEI_IDLE &&
797 write_cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
799 mutex_unlock(&dev->device_lock);
803 write_cb->file_object = file;
804 write_cb->file_private = cl;
805 write_cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
807 if (!write_cb->request_buffer.data)
810 dev_dbg(&dev->pdev->dev, "length =%d\n", (int) length);
813 if (copy_from_user(write_cb->request_buffer.data, ubuf, length))
818 ((memcmp(mei_wd_state_independence_msg[0],
819 write_cb->request_buffer.data, 4) == 0) ||
820 (memcmp(mei_wd_state_independence_msg[1],
821 write_cb->request_buffer.data, 4) == 0) ||
822 (memcmp(mei_wd_state_independence_msg[2],
823 write_cb->request_buffer.data, 4) == 0)))
824 cl->sm_state |= MEI_WD_STATE_INDEPENDENCE_MSG_SENT;
826 INIT_LIST_HEAD(&write_cb->cb_list);
827 if (cl == &dev->iamthif_cl) {
828 write_cb->response_buffer.data =
829 kmalloc(dev->iamthif_mtu, GFP_KERNEL);
830 if (!write_cb->response_buffer.data) {
834 if (dev->mei_state != MEI_ENABLED) {
838 for (i = 0; i < dev->me_clients_num; i++) {
839 if (dev->me_clients[i].client_id ==
840 dev->iamthif_cl.me_client_id)
844 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
848 if (i == dev->me_clients_num ||
849 (dev->me_clients[i].client_id !=
850 dev->iamthif_cl.me_client_id)) {
853 } else if (length > dev->me_clients[i].props.max_msg_length ||
859 write_cb->response_buffer.size = dev->iamthif_mtu;
860 write_cb->major_file_operations = MEI_IOCTL;
861 write_cb->information = 0;
862 write_cb->request_buffer.size = length;
863 if (dev->iamthif_cl.state != MEI_FILE_CONNECTED) {
868 if (!list_empty(&dev->amthi_cmd_list.mei_cb.cb_list) ||
869 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
870 dev_dbg(&dev->pdev->dev, "amthi_state = %d\n",
871 (int) dev->iamthif_state);
872 dev_dbg(&dev->pdev->dev, "add amthi cb to amthi cmd waiting list\n");
873 list_add_tail(&write_cb->cb_list,
874 &dev->amthi_cmd_list.mei_cb.cb_list);
877 dev_dbg(&dev->pdev->dev, "call amthi write\n");
878 rets = amthi_write(dev, write_cb);
881 dev_dbg(&dev->pdev->dev, "amthi write failed with status = %d\n",
887 mutex_unlock(&dev->device_lock);
891 write_cb->major_file_operations = MEI_WRITE;
892 /* make sure information is zero before we start */
894 write_cb->information = 0;
895 write_cb->request_buffer.size = length;
897 dev_dbg(&dev->pdev->dev, "host client = %d, ME client = %d\n",
898 cl->host_client_id, cl->me_client_id);
899 if (cl->state != MEI_FILE_CONNECTED) {
901 dev_dbg(&dev->pdev->dev, "host client = %d, is not connected to ME client = %d",
906 for (i = 0; i < dev->me_clients_num; i++) {
907 if (dev->me_clients[i].client_id ==
911 if (WARN_ON(dev->me_clients[i].client_id != cl->me_client_id)) {
915 if (i == dev->me_clients_num) {
919 if (length > dev->me_clients[i].props.max_msg_length || length <= 0) {
923 write_cb->file_private = cl;
925 rets = mei_flow_ctrl_creds(dev, cl);
929 if (rets && dev->mei_host_buffer_is_empty) {
931 dev->mei_host_buffer_is_empty = false;
932 if (length > ((((dev->host_hw_state & H_CBD) >> 24) *
933 sizeof(u32)) - sizeof(struct mei_msg_hdr))) {
936 (((dev->host_hw_state & H_CBD) >> 24) *
938 sizeof(struct mei_msg_hdr);
939 mei_hdr.msg_complete = 0;
941 mei_hdr.length = length;
942 mei_hdr.msg_complete = 1;
944 mei_hdr.host_addr = cl->host_client_id;
945 mei_hdr.me_addr = cl->me_client_id;
946 mei_hdr.reserved = 0;
947 dev_dbg(&dev->pdev->dev, "call mei_write_message header=%08x.\n",
948 *((u32 *) &mei_hdr));
949 if (!mei_write_message(dev, &mei_hdr,
950 (unsigned char *) (write_cb->request_buffer.data),
955 cl->writing_state = MEI_WRITING;
956 write_cb->information = mei_hdr.length;
957 if (mei_hdr.msg_complete) {
958 if (mei_flow_ctrl_reduce(dev, cl)) {
962 list_add_tail(&write_cb->cb_list,
963 &dev->write_waiting_list.mei_cb.cb_list);
965 list_add_tail(&write_cb->cb_list,
966 &dev->write_list.mei_cb.cb_list);
971 write_cb->information = 0;
972 cl->writing_state = MEI_WRITING;
973 list_add_tail(&write_cb->cb_list,
974 &dev->write_list.mei_cb.cb_list);
976 mutex_unlock(&dev->device_lock);
980 mutex_unlock(&dev->device_lock);
981 mei_free_cb_private(write_cb);
987 * mei_ioctl - the IOCTL function
989 * @file: pointer to file structure
990 * @cmd: ioctl command
991 * @data: pointer to mei message structure
993 * returns 0 on success , <0 on error
995 static long mei_ioctl(struct file *file, unsigned int cmd, unsigned long data)
997 struct mei_device *dev;
998 struct mei_cl *cl = file->private_data;
999 struct mei_connect_client_data *connect_data = NULL;
1002 if (cmd != IOCTL_MEI_CONNECT_CLIENT)
1005 if (WARN_ON(!cl || !cl->dev))
1010 dev_dbg(&dev->pdev->dev, "IOCTL cmd = 0x%x", cmd);
1012 mutex_lock(&dev->device_lock);
1013 if (dev->mei_state != MEI_ENABLED) {
1018 dev_dbg(&dev->pdev->dev, ": IOCTL_MEI_CONNECT_CLIENT.\n");
1020 connect_data = kzalloc(sizeof(struct mei_connect_client_data),
1022 if (!connect_data) {
1026 dev_dbg(&dev->pdev->dev, "copy connect data from user\n");
1027 if (copy_from_user(connect_data, (char __user *)data,
1028 sizeof(struct mei_connect_client_data))) {
1029 dev_dbg(&dev->pdev->dev, "failed to copy data from userland\n");
1033 rets = mei_ioctl_connect_client(file, connect_data);
1035 /* if all is ok, copying the data back to user. */
1039 dev_dbg(&dev->pdev->dev, "copy connect data to user\n");
1040 if (copy_to_user((char __user *)data, connect_data,
1041 sizeof(struct mei_connect_client_data))) {
1042 dev_dbg(&dev->pdev->dev, "failed to copy data to userland\n");
1048 kfree(connect_data);
1049 mutex_unlock(&dev->device_lock);
1054 * mei_compat_ioctl - the compat IOCTL function
1056 * @file: pointer to file structure
1057 * @cmd: ioctl command
1058 * @data: pointer to mei message structure
1060 * returns 0 on success , <0 on error
1062 #ifdef CONFIG_COMPAT
1063 static long mei_compat_ioctl(struct file *file,
1064 unsigned int cmd, unsigned long data)
1066 return mei_ioctl(file, cmd, (unsigned long)compat_ptr(data));
1072 * mei_poll - the poll function
1074 * @file: pointer to file structure
1075 * @wait: pointer to poll_table structure
1079 static unsigned int mei_poll(struct file *file, poll_table *wait)
1081 struct mei_cl *cl = file->private_data;
1082 struct mei_device *dev;
1083 unsigned int mask = 0;
1085 if (WARN_ON(!cl || !cl->dev))
1090 mutex_lock(&dev->device_lock);
1092 if (dev->mei_state != MEI_ENABLED)
1096 if (cl == &dev->iamthif_cl) {
1097 mutex_unlock(&dev->device_lock);
1098 poll_wait(file, &dev->iamthif_cl.wait, wait);
1099 mutex_lock(&dev->device_lock);
1100 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
1101 dev->iamthif_file_object == file) {
1102 mask |= (POLLIN | POLLRDNORM);
1103 dev_dbg(&dev->pdev->dev, "run next amthi cb\n");
1104 mei_run_next_iamthif_cmd(dev);
1109 mutex_unlock(&dev->device_lock);
1110 poll_wait(file, &cl->tx_wait, wait);
1111 mutex_lock(&dev->device_lock);
1112 if (MEI_WRITE_COMPLETE == cl->writing_state)
1113 mask |= (POLLIN | POLLRDNORM);
1116 mutex_unlock(&dev->device_lock);
1121 static int mei_pci_suspend(struct device *device)
1123 struct pci_dev *pdev = to_pci_dev(device);
1124 struct mei_device *dev = pci_get_drvdata(pdev);
1129 mutex_lock(&dev->device_lock);
1130 /* Stop watchdog if exists */
1131 err = mei_wd_stop(dev, true);
1132 /* Set new mei state */
1133 if (dev->mei_state == MEI_ENABLED ||
1134 dev->mei_state == MEI_RECOVERING_FROM_RESET) {
1135 dev->mei_state = MEI_POWER_DOWN;
1138 mutex_unlock(&dev->device_lock);
1140 free_irq(pdev->irq, dev);
1141 pci_disable_msi(pdev);
1146 static int mei_pci_resume(struct device *device)
1148 struct pci_dev *pdev = to_pci_dev(device);
1149 struct mei_device *dev;
1152 dev = pci_get_drvdata(pdev);
1156 pci_enable_msi(pdev);
1158 /* request and enable interrupt */
1159 if (pci_dev_msi_enabled(pdev))
1160 err = request_threaded_irq(pdev->irq,
1162 mei_interrupt_thread_handler,
1163 0, mei_driver_name, dev);
1165 err = request_threaded_irq(pdev->irq,
1166 mei_interrupt_quick_handler,
1167 mei_interrupt_thread_handler,
1168 IRQF_SHARED, mei_driver_name, dev);
1171 printk(KERN_ERR "mei: Request_irq failure. irq = %d\n",
1176 mutex_lock(&dev->device_lock);
1177 dev->mei_state = MEI_POWER_UP;
1179 mutex_unlock(&dev->device_lock);
1181 /* Start timer if stopped in suspend */
1182 schedule_delayed_work(&dev->timer_work, HZ);
1186 static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
1187 #define MEI_PM_OPS (&mei_pm_ops)
1189 #define MEI_PM_OPS NULL
1190 #endif /* CONFIG_PM */
1192 * PCI driver structure
1194 static struct pci_driver mei_driver = {
1195 .name = mei_driver_name,
1196 .id_table = mei_pci_tbl,
1198 .remove = __devexit_p(mei_remove),
1199 .shutdown = __devexit_p(mei_remove),
1200 .driver.pm = MEI_PM_OPS,
1204 * file operations structure will be used for mei char device.
1206 static const struct file_operations mei_fops = {
1207 .owner = THIS_MODULE,
1209 .unlocked_ioctl = mei_ioctl,
1210 #ifdef CONFIG_COMPAT
1211 .compat_ioctl = mei_compat_ioctl,
1214 .release = mei_release,
1220 * mei_registration_cdev - sets up the cdev structure for mei device.
1222 * @dev: char device struct
1223 * @hminor: minor number for registration char device
1224 * @fops: file operations structure
1226 * returns 0 on success, <0 on failure.
1228 static int mei_registration_cdev(struct cdev *dev, int hminor,
1229 const struct file_operations *fops)
1231 int ret, devno = MKDEV(mei_major, hminor);
1233 cdev_init(dev, fops);
1234 dev->owner = THIS_MODULE;
1235 ret = cdev_add(dev, devno, 1);
1236 /* Fail gracefully if need be */
1238 printk(KERN_ERR "mei: Error %d registering mei device %d\n",
1244 * mei_register_cdev - registers mei char device
1246 * returns 0 on success, <0 on failure.
1248 static int mei_register_cdev(void)
1253 /* registration of char devices */
1254 ret = alloc_chrdev_region(&dev, MEI_MINORS_BASE, MEI_MINORS_COUNT,
1257 printk(KERN_ERR "mei: Error allocating char device region.\n");
1261 mei_major = MAJOR(dev);
1263 ret = mei_registration_cdev(&mei_cdev, MEI_MINOR_NUMBER,
1266 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1273 * mei_unregister_cdev - unregisters mei char device
1275 static void mei_unregister_cdev(void)
1277 cdev_del(&mei_cdev);
1278 unregister_chrdev_region(MKDEV(mei_major, MEI_MINORS_BASE),
1283 * mei_sysfs_device_create - adds device entry to sysfs
1285 * returns 0 on success, <0 on failure.
1287 static int mei_sysfs_device_create(void)
1289 struct class *class;
1293 class = class_create(THIS_MODULE, MEI_DRIVER_NAME);
1294 if (IS_ERR(class)) {
1295 err = PTR_ERR(class);
1296 printk(KERN_ERR "mei: Error creating mei class.\n");
1300 tmphdev = device_create(class, NULL, mei_cdev.dev, NULL,
1302 if (IS_ERR(tmphdev)) {
1303 err = PTR_ERR(tmphdev);
1311 class_destroy(class);
1317 * mei_sysfs_device_remove - unregisters the device entry on sysfs
1319 static void mei_sysfs_device_remove(void)
1321 if (IS_ERR_OR_NULL(mei_class))
1324 device_destroy(mei_class, mei_cdev.dev);
1325 class_destroy(mei_class);
1329 * mei_init_module - Driver Registration Routine
1331 * mei_init_module is the first routine called when the driver is
1332 * loaded. All it does is to register with the PCI subsystem.
1334 * returns 0 on success, <0 on failure.
1336 static int __init mei_init_module(void)
1340 pr_debug("mei: %s - version %s\n",
1341 mei_driver_string, mei_driver_version);
1342 /* init pci module */
1343 ret = pci_register_driver(&mei_driver);
1345 printk(KERN_ERR "mei: Error registering driver.\n");
1349 ret = mei_register_cdev();
1351 goto unregister_pci;
1353 ret = mei_sysfs_device_create();
1355 goto unregister_cdev;
1360 mei_unregister_cdev();
1362 pci_unregister_driver(&mei_driver);
1367 module_init(mei_init_module);
1370 * mei_exit_module - Driver Exit Cleanup Routine
1372 * mei_exit_module is called just before the driver is removed
1375 static void __exit mei_exit_module(void)
1377 mei_sysfs_device_remove();
1378 mei_unregister_cdev();
1379 pci_unregister_driver(&mei_driver);
1381 pr_debug("mei: Driver unloaded successfully.\n");
1384 module_exit(mei_exit_module);
1387 MODULE_AUTHOR("Intel Corporation");
1388 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1389 MODULE_LICENSE("GPL v2");
1390 MODULE_VERSION(MEI_DRIVER_VERSION);