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 static 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_probe - Device Initialization Routine
111 * @pdev: PCI device structure
112 * @ent: entry in kcs_pci_tbl
114 * returns 0 on success, <0 on failure.
116 static int __devinit
mei_probe(struct pci_dev
*pdev
,
117 const struct pci_device_id
*ent
)
119 struct mei_device
*dev
;
122 mutex_lock(&mei_mutex
);
128 err
= pci_enable_device(pdev
);
130 printk(KERN_ERR
"mei: Failed to enable pci device.\n");
133 /* set PCI host mastering */
134 pci_set_master(pdev
);
135 /* pci request regions for mei driver */
136 err
= pci_request_regions(pdev
, mei_driver_name
);
138 printk(KERN_ERR
"mei: Failed to get pci regions.\n");
141 /* allocates and initializes the mei dev structure */
142 dev
= mei_device_init(pdev
);
145 goto release_regions
;
147 /* mapping IO device memory */
148 dev
->mem_addr
= pci_iomap(pdev
, 0, 0);
149 if (!dev
->mem_addr
) {
150 printk(KERN_ERR
"mei: mapping I/O device memory failure.\n");
154 pci_enable_msi(pdev
);
156 /* request and enable interrupt */
157 if (pci_dev_msi_enabled(pdev
))
158 err
= request_threaded_irq(pdev
->irq
,
160 mei_interrupt_thread_handler
,
161 0, mei_driver_name
, dev
);
163 err
= request_threaded_irq(pdev
->irq
,
164 mei_interrupt_quick_handler
,
165 mei_interrupt_thread_handler
,
166 IRQF_SHARED
, mei_driver_name
, dev
);
169 printk(KERN_ERR
"mei: request_threaded_irq failure. irq = %d\n",
173 INIT_DELAYED_WORK(&dev
->wd_work
, mei_wd_timer
);
174 if (mei_hw_init(dev
)) {
175 printk(KERN_ERR
"mei: Init hw failure.\n");
180 pci_set_drvdata(pdev
, dev
);
181 schedule_delayed_work(&dev
->wd_work
, HZ
);
183 mutex_unlock(&mei_mutex
);
185 pr_debug("mei: Driver initialization successful.\n");
190 /* disable interrupts */
191 dev
->host_hw_state
= mei_hcsr_read(dev
);
192 mei_disable_interrupts(dev
);
193 flush_scheduled_work();
194 free_irq(pdev
->irq
, dev
);
195 pci_disable_msi(pdev
);
197 pci_iounmap(pdev
, dev
->mem_addr
);
201 pci_release_regions(pdev
);
203 pci_disable_device(pdev
);
205 mutex_unlock(&mei_mutex
);
206 printk(KERN_ERR
"mei: Driver initialization failed.\n");
211 * mei_remove - Device Removal Routine
213 * @pdev: PCI device structure
215 * mei_remove is called by the PCI subsystem to alert the driver
216 * that it should release a PCI device.
218 static void __devexit
mei_remove(struct pci_dev
*pdev
)
220 struct mei_device
*dev
;
222 if (mei_device
!= pdev
)
225 dev
= pci_get_drvdata(pdev
);
229 mutex_lock(&dev
->device_lock
);
231 mei_wd_stop(dev
, false);
235 if (dev
->iamthif_cl
.state
== MEI_FILE_CONNECTED
) {
236 dev
->iamthif_cl
.state
= MEI_FILE_DISCONNECTING
;
237 mei_disconnect_host_client(dev
, &dev
->iamthif_cl
);
239 if (dev
->wd_cl
.state
== MEI_FILE_CONNECTED
) {
240 dev
->wd_cl
.state
= MEI_FILE_DISCONNECTING
;
241 mei_disconnect_host_client(dev
, &dev
->wd_cl
);
244 /* remove entry if already in list */
245 dev_dbg(&pdev
->dev
, "list del iamthif and wd file list.\n");
246 mei_remove_client_from_file_list(dev
, dev
->wd_cl
.host_client_id
);
247 mei_remove_client_from_file_list(dev
, dev
->iamthif_cl
.host_client_id
);
249 dev
->iamthif_current_cb
= NULL
;
250 dev
->me_clients_num
= 0;
252 mutex_unlock(&dev
->device_lock
);
254 flush_scheduled_work();
256 /* disable interrupts */
257 mei_disable_interrupts(dev
);
259 free_irq(pdev
->irq
, dev
);
260 pci_disable_msi(pdev
);
261 pci_set_drvdata(pdev
, NULL
);
264 pci_iounmap(pdev
, dev
->mem_addr
);
268 pci_release_regions(pdev
);
269 pci_disable_device(pdev
);
273 * mei_clear_list - removes all callbacks associated with file
276 * @dev: device structure.
277 * @file: file structure
278 * @mei_cb_list: callbacks list
280 * mei_clear_list is called to clear resources associated with file
281 * when application calls close function or Ctrl-C was pressed
283 * returns true if callback removed from the list, false otherwise
285 static bool mei_clear_list(struct mei_device
*dev
,
286 struct file
*file
, struct list_head
*mei_cb_list
)
288 struct mei_cl_cb
*cb_pos
= NULL
;
289 struct mei_cl_cb
*cb_next
= NULL
;
290 struct file
*file_temp
;
291 bool removed
= false;
293 /* list all list member */
294 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, cb_list
) {
295 file_temp
= (struct file
*)cb_pos
->file_object
;
296 /* check if list member associated with a file */
297 if (file_temp
== file
) {
298 /* remove member from the list */
299 list_del(&cb_pos
->cb_list
);
300 /* check if cb equal to current iamthif cb */
301 if (dev
->iamthif_current_cb
== cb_pos
) {
302 dev
->iamthif_current_cb
= NULL
;
303 /* send flow control to iamthif client */
304 mei_send_flow_control(dev
, &dev
->iamthif_cl
);
306 /* free all allocated buffers */
307 mei_free_cb_private(cb_pos
);
316 * mei_clear_lists - removes all callbacks associated with file
318 * @dev: device structure
319 * @file: file structure
321 * mei_clear_lists is called to clear resources associated with file
322 * when application calls close function or Ctrl-C was pressed
324 * returns true if callback removed from the list, false otherwise
326 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
328 bool removed
= false;
330 /* remove callbacks associated with a file */
331 mei_clear_list(dev
, file
, &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
332 if (mei_clear_list(dev
, file
,
333 &dev
->amthi_read_complete_list
.mei_cb
.cb_list
))
336 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.mei_cb
.cb_list
);
338 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.mei_cb
.cb_list
))
341 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.mei_cb
.cb_list
))
344 if (mei_clear_list(dev
, file
, &dev
->write_list
.mei_cb
.cb_list
))
347 /* check if iamthif_current_cb not NULL */
348 if (dev
->iamthif_current_cb
&& !removed
) {
349 /* check file and iamthif current cb association */
350 if (dev
->iamthif_current_cb
->file_object
== file
) {
352 mei_free_cb_private(dev
->iamthif_current_cb
);
353 dev
->iamthif_current_cb
= NULL
;
360 * find_read_list_entry - find read list entry
362 * @dev: device structure
363 * @file: pointer to file structure
365 * returns cb on success, NULL on error
367 static struct mei_cl_cb
*find_read_list_entry(
368 struct mei_device
*dev
,
371 struct mei_cl_cb
*cb_pos
= NULL
;
372 struct mei_cl_cb
*cb_next
= NULL
;
374 if (!dev
->read_list
.status
&&
375 !list_empty(&dev
->read_list
.mei_cb
.cb_list
)) {
377 dev_dbg(&dev
->pdev
->dev
, "remove read_list CB\n");
378 list_for_each_entry_safe(cb_pos
, cb_next
,
379 &dev
->read_list
.mei_cb
.cb_list
, cb_list
) {
380 struct mei_cl
*cl_temp
;
381 cl_temp
= (struct mei_cl
*)cb_pos
->file_private
;
383 if (mei_cl_cmp_id(cl
, cl_temp
))
391 * mei_open - the open function
393 * @inode: pointer to inode structure
394 * @file: pointer to file structure
396 * returns 0 on success, <0 on error
398 static int mei_open(struct inode
*inode
, struct file
*file
)
401 int if_num
= iminor(inode
), err
;
402 struct mei_device
*dev
;
408 dev
= pci_get_drvdata(mei_device
);
409 if (if_num
!= MEI_MINOR_NUMBER
|| !dev
)
412 mutex_lock(&dev
->device_lock
);
414 cl
= mei_cl_allocate(dev
);
419 if (dev
->mei_state
!= MEI_ENABLED
) {
420 dev_dbg(&dev
->pdev
->dev
, "mei_state != MEI_ENABLED mei_state= %d\n",
425 if (dev
->open_handle_count
>= MEI_MAX_OPEN_HANDLE_COUNT
)
428 cl
->host_client_id
= find_first_zero_bit(dev
->host_clients_map
,
430 if (cl
->host_client_id
> MEI_CLIENTS_MAX
)
433 dev_dbg(&dev
->pdev
->dev
, "client_id = %d\n", cl
->host_client_id
);
435 dev
->open_handle_count
++;
436 list_add_tail(&cl
->link
, &dev
->file_list
);
438 set_bit(cl
->host_client_id
, dev
->host_clients_map
);
439 cl
->state
= MEI_FILE_INITIALIZING
;
442 file
->private_data
= cl
;
443 mutex_unlock(&dev
->device_lock
);
448 mutex_unlock(&dev
->device_lock
);
455 * mei_release - the release function
457 * @inode: pointer to inode structure
458 * @file: pointer to file structure
460 * returns 0 on success, <0 on error
462 static int mei_release(struct inode
*inode
, struct file
*file
)
464 struct mei_cl
*cl
= file
->private_data
;
465 struct mei_cl_cb
*cb
;
466 struct mei_device
*dev
;
469 if (WARN_ON(!cl
|| !cl
->dev
))
474 mutex_lock(&dev
->device_lock
);
475 if (cl
!= &dev
->iamthif_cl
) {
476 if (cl
->state
== MEI_FILE_CONNECTED
) {
477 cl
->state
= MEI_FILE_DISCONNECTING
;
478 dev_dbg(&dev
->pdev
->dev
,
479 "disconnecting client host client = %d, "
483 rets
= mei_disconnect_host_client(dev
, cl
);
485 mei_cl_flush_queues(cl
);
486 dev_dbg(&dev
->pdev
->dev
, "remove client host client = %d, ME client = %d\n",
490 if (dev
->open_handle_count
> 0) {
491 clear_bit(cl
->host_client_id
,
492 dev
->host_clients_map
);
493 dev
->open_handle_count
--;
495 mei_remove_client_from_file_list(dev
, cl
->host_client_id
);
500 cb
= find_read_list_entry(dev
, cl
);
501 /* Remove entry from read list */
503 list_del(&cb
->cb_list
);
509 file
->private_data
= NULL
;
512 mei_free_cb_private(cb
);
518 if (dev
->open_handle_count
> 0)
519 dev
->open_handle_count
--;
521 if (dev
->iamthif_file_object
== file
&&
522 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
524 dev_dbg(&dev
->pdev
->dev
, "amthi canceled iamthif state %d\n",
526 dev
->iamthif_canceled
= true;
527 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
528 dev_dbg(&dev
->pdev
->dev
, "run next amthi iamthif cb\n");
529 mei_run_next_iamthif_cmd(dev
);
533 if (mei_clear_lists(dev
, file
))
534 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
537 mutex_unlock(&dev
->device_lock
);
543 * mei_read - the read function.
545 * @file: pointer to file structure
546 * @ubuf: pointer to user buffer
547 * @length: buffer length
548 * @offset: data offset in buffer
550 * returns >=0 data length on success , <0 on error
552 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
553 size_t length
, loff_t
*offset
)
555 struct mei_cl
*cl
= file
->private_data
;
556 struct mei_cl_cb
*cb_pos
= NULL
;
557 struct mei_cl_cb
*cb
= NULL
;
558 struct mei_device
*dev
;
564 if (WARN_ON(!cl
|| !cl
->dev
))
569 mutex_lock(&dev
->device_lock
);
570 if (dev
->mei_state
!= MEI_ENABLED
) {
575 if ((cl
->sm_state
& MEI_WD_STATE_INDEPENDENCE_MSG_SENT
) == 0) {
576 /* Do not allow to read watchdog client */
577 i
= mei_find_me_client_index(dev
, mei_wd_guid
);
579 struct mei_me_client
*me_client
= &dev
->me_clients
[i
];
581 if (cl
->me_client_id
== me_client
->client_id
) {
587 cl
->sm_state
&= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
590 if (cl
== &dev
->iamthif_cl
) {
591 rets
= amthi_read(dev
, file
, ubuf
, length
, offset
);
595 if (cl
->read_cb
&& cl
->read_cb
->information
> *offset
) {
598 } else if (cl
->read_cb
&& cl
->read_cb
->information
> 0 &&
599 cl
->read_cb
->information
<= *offset
) {
603 } else if ((!cl
->read_cb
|| !cl
->read_cb
->information
) &&
605 /*Offset needs to be cleaned for contingous reads*/
611 err
= mei_start_read(dev
, cl
);
612 if (err
&& err
!= -EBUSY
) {
613 dev_dbg(&dev
->pdev
->dev
,
614 "mei start read failure with status = %d\n", err
);
619 if (MEI_READ_COMPLETE
!= cl
->reading_state
&&
620 !waitqueue_active(&cl
->rx_wait
)) {
621 if (file
->f_flags
& O_NONBLOCK
) {
626 mutex_unlock(&dev
->device_lock
);
628 if (wait_event_interruptible(cl
->rx_wait
,
629 (MEI_READ_COMPLETE
== cl
->reading_state
||
630 MEI_FILE_INITIALIZING
== cl
->state
||
631 MEI_FILE_DISCONNECTED
== cl
->state
||
632 MEI_FILE_DISCONNECTING
== cl
->state
))) {
633 if (signal_pending(current
))
638 mutex_lock(&dev
->device_lock
);
639 if (MEI_FILE_INITIALIZING
== cl
->state
||
640 MEI_FILE_DISCONNECTED
== cl
->state
||
641 MEI_FILE_DISCONNECTING
== cl
->state
) {
653 if (cl
->reading_state
!= MEI_READ_COMPLETE
) {
657 /* now copy the data to user space */
659 dev_dbg(&dev
->pdev
->dev
, "cb->response_buffer size - %d\n",
660 cb
->response_buffer
.size
);
661 dev_dbg(&dev
->pdev
->dev
, "cb->information - %lu\n",
663 if (length
== 0 || ubuf
== NULL
|| *offset
> cb
->information
) {
668 /* length is being turncated to PAGE_SIZE, however, */
669 /* information size may be longer */
670 length
= min_t(size_t, length
, (cb
->information
- *offset
));
672 if (copy_to_user(ubuf
,
673 cb
->response_buffer
.data
+ *offset
,
681 if ((unsigned long)*offset
< cb
->information
)
685 cb_pos
= find_read_list_entry(dev
, cl
);
686 /* Remove entry from read list */
688 list_del(&cb_pos
->cb_list
);
689 mei_free_cb_private(cb
);
690 cl
->reading_state
= MEI_IDLE
;
692 cl
->read_pending
= 0;
694 dev_dbg(&dev
->pdev
->dev
, "end mei read rets= %d\n", rets
);
695 mutex_unlock(&dev
->device_lock
);
700 * mei_write - the write function.
702 * @file: pointer to file structure
703 * @ubuf: pointer to user buffer
704 * @length: buffer length
705 * @offset: data offset in buffer
707 * returns >=0 data length on success , <0 on error
709 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
710 size_t length
, loff_t
*offset
)
712 struct mei_cl
*cl
= file
->private_data
;
713 struct mei_cl_cb
*write_cb
= NULL
;
714 struct mei_msg_hdr mei_hdr
;
715 struct mei_device
*dev
;
716 unsigned long timeout
= 0;
720 if (WARN_ON(!cl
|| !cl
->dev
))
725 mutex_lock(&dev
->device_lock
);
727 if (dev
->mei_state
!= MEI_ENABLED
) {
728 mutex_unlock(&dev
->device_lock
);
732 if (cl
== &dev
->iamthif_cl
) {
733 write_cb
= find_amthi_read_list_entry(dev
, file
);
736 timeout
= write_cb
->read_time
+
737 msecs_to_jiffies(IAMTHIF_READ_TIMER
);
739 if (time_after(jiffies
, timeout
) ||
740 cl
->reading_state
== MEI_READ_COMPLETE
) {
742 list_del(&write_cb
->cb_list
);
743 mei_free_cb_private(write_cb
);
749 /* free entry used in read */
750 if (cl
->reading_state
== MEI_READ_COMPLETE
) {
752 write_cb
= find_read_list_entry(dev
, cl
);
754 list_del(&write_cb
->cb_list
);
755 mei_free_cb_private(write_cb
);
757 cl
->reading_state
= MEI_IDLE
;
759 cl
->read_pending
= 0;
761 } else if (cl
->reading_state
== MEI_IDLE
&&
766 write_cb
= kzalloc(sizeof(struct mei_cl_cb
), GFP_KERNEL
);
768 mutex_unlock(&dev
->device_lock
);
772 write_cb
->file_object
= file
;
773 write_cb
->file_private
= cl
;
774 write_cb
->request_buffer
.data
= kmalloc(length
, GFP_KERNEL
);
776 if (!write_cb
->request_buffer
.data
)
779 dev_dbg(&dev
->pdev
->dev
, "length =%d\n", (int) length
);
782 if (copy_from_user(write_cb
->request_buffer
.data
, ubuf
, length
))
787 ((memcmp(mei_wd_state_independence_msg
[0],
788 write_cb
->request_buffer
.data
, 4) == 0) ||
789 (memcmp(mei_wd_state_independence_msg
[1],
790 write_cb
->request_buffer
.data
, 4) == 0) ||
791 (memcmp(mei_wd_state_independence_msg
[2],
792 write_cb
->request_buffer
.data
, 4) == 0)))
793 cl
->sm_state
|= MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
795 INIT_LIST_HEAD(&write_cb
->cb_list
);
796 if (cl
== &dev
->iamthif_cl
) {
797 write_cb
->response_buffer
.data
=
798 kmalloc(dev
->iamthif_mtu
, GFP_KERNEL
);
799 if (!write_cb
->response_buffer
.data
) {
803 if (dev
->mei_state
!= MEI_ENABLED
) {
807 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
808 if (dev
->me_clients
[i
].client_id
==
809 dev
->iamthif_cl
.me_client_id
)
813 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
817 if (i
== dev
->me_clients_num
||
818 (dev
->me_clients
[i
].client_id
!=
819 dev
->iamthif_cl
.me_client_id
)) {
822 } else if (length
> dev
->me_clients
[i
].props
.max_msg_length
||
828 write_cb
->response_buffer
.size
= dev
->iamthif_mtu
;
829 write_cb
->major_file_operations
= MEI_IOCTL
;
830 write_cb
->information
= 0;
831 write_cb
->request_buffer
.size
= length
;
832 if (dev
->iamthif_cl
.state
!= MEI_FILE_CONNECTED
) {
837 if (!list_empty(&dev
->amthi_cmd_list
.mei_cb
.cb_list
) ||
838 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
839 dev_dbg(&dev
->pdev
->dev
, "amthi_state = %d\n",
840 (int) dev
->iamthif_state
);
841 dev_dbg(&dev
->pdev
->dev
, "add amthi cb to amthi cmd waiting list\n");
842 list_add_tail(&write_cb
->cb_list
,
843 &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
846 dev_dbg(&dev
->pdev
->dev
, "call amthi write\n");
847 rets
= amthi_write(dev
, write_cb
);
850 dev_dbg(&dev
->pdev
->dev
, "amthi write failed with status = %d\n",
856 mutex_unlock(&dev
->device_lock
);
860 write_cb
->major_file_operations
= MEI_WRITE
;
861 /* make sure information is zero before we start */
863 write_cb
->information
= 0;
864 write_cb
->request_buffer
.size
= length
;
866 dev_dbg(&dev
->pdev
->dev
, "host client = %d, ME client = %d\n",
867 cl
->host_client_id
, cl
->me_client_id
);
868 if (cl
->state
!= MEI_FILE_CONNECTED
) {
870 dev_dbg(&dev
->pdev
->dev
, "host client = %d, is not connected to ME client = %d",
875 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
876 if (dev
->me_clients
[i
].client_id
==
880 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
884 if (i
== dev
->me_clients_num
) {
888 if (length
> dev
->me_clients
[i
].props
.max_msg_length
|| length
<= 0) {
892 write_cb
->file_private
= cl
;
894 rets
= mei_flow_ctrl_creds(dev
, cl
);
898 if (rets
&& dev
->mei_host_buffer_is_empty
) {
900 dev
->mei_host_buffer_is_empty
= false;
901 if (length
> ((((dev
->host_hw_state
& H_CBD
) >> 24) *
902 sizeof(u32
)) - sizeof(struct mei_msg_hdr
))) {
905 (((dev
->host_hw_state
& H_CBD
) >> 24) *
907 sizeof(struct mei_msg_hdr
);
908 mei_hdr
.msg_complete
= 0;
910 mei_hdr
.length
= length
;
911 mei_hdr
.msg_complete
= 1;
913 mei_hdr
.host_addr
= cl
->host_client_id
;
914 mei_hdr
.me_addr
= cl
->me_client_id
;
915 mei_hdr
.reserved
= 0;
916 dev_dbg(&dev
->pdev
->dev
, "call mei_write_message header=%08x.\n",
917 *((u32
*) &mei_hdr
));
918 if (!mei_write_message(dev
, &mei_hdr
,
919 (unsigned char *) (write_cb
->request_buffer
.data
),
924 cl
->writing_state
= MEI_WRITING
;
925 write_cb
->information
= mei_hdr
.length
;
926 if (mei_hdr
.msg_complete
) {
927 if (mei_flow_ctrl_reduce(dev
, cl
)) {
931 list_add_tail(&write_cb
->cb_list
,
932 &dev
->write_waiting_list
.mei_cb
.cb_list
);
934 list_add_tail(&write_cb
->cb_list
,
935 &dev
->write_list
.mei_cb
.cb_list
);
940 write_cb
->information
= 0;
941 cl
->writing_state
= MEI_WRITING
;
942 list_add_tail(&write_cb
->cb_list
,
943 &dev
->write_list
.mei_cb
.cb_list
);
945 mutex_unlock(&dev
->device_lock
);
949 mutex_unlock(&dev
->device_lock
);
950 mei_free_cb_private(write_cb
);
956 * mei_ioctl - the IOCTL function
958 * @file: pointer to file structure
959 * @cmd: ioctl command
960 * @data: pointer to mei message structure
962 * returns 0 on success , <0 on error
964 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
966 struct mei_device
*dev
;
967 struct mei_cl
*cl
= file
->private_data
;
968 struct mei_connect_client_data
*connect_data
= NULL
;
971 if (cmd
!= IOCTL_MEI_CONNECT_CLIENT
)
974 if (WARN_ON(!cl
|| !cl
->dev
))
979 dev_dbg(&dev
->pdev
->dev
, "IOCTL cmd = 0x%x", cmd
);
981 mutex_lock(&dev
->device_lock
);
982 if (dev
->mei_state
!= MEI_ENABLED
) {
987 dev_dbg(&dev
->pdev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
989 connect_data
= kzalloc(sizeof(struct mei_connect_client_data
),
995 dev_dbg(&dev
->pdev
->dev
, "copy connect data from user\n");
996 if (copy_from_user(connect_data
, (char __user
*)data
,
997 sizeof(struct mei_connect_client_data
))) {
998 dev_dbg(&dev
->pdev
->dev
, "failed to copy data from userland\n");
1002 rets
= mei_ioctl_connect_client(file
, connect_data
);
1004 /* if all is ok, copying the data back to user. */
1008 dev_dbg(&dev
->pdev
->dev
, "copy connect data to user\n");
1009 if (copy_to_user((char __user
*)data
, connect_data
,
1010 sizeof(struct mei_connect_client_data
))) {
1011 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
1017 kfree(connect_data
);
1018 mutex_unlock(&dev
->device_lock
);
1023 * mei_compat_ioctl - the compat IOCTL function
1025 * @file: pointer to file structure
1026 * @cmd: ioctl command
1027 * @data: pointer to mei message structure
1029 * returns 0 on success , <0 on error
1031 #ifdef CONFIG_COMPAT
1032 static long mei_compat_ioctl(struct file
*file
,
1033 unsigned int cmd
, unsigned long data
)
1035 return mei_ioctl(file
, cmd
, (unsigned long)compat_ptr(data
));
1041 * mei_poll - the poll function
1043 * @file: pointer to file structure
1044 * @wait: pointer to poll_table structure
1048 static unsigned int mei_poll(struct file
*file
, poll_table
*wait
)
1050 struct mei_cl
*cl
= file
->private_data
;
1051 struct mei_device
*dev
;
1052 unsigned int mask
= 0;
1054 if (WARN_ON(!cl
|| !cl
->dev
))
1059 mutex_lock(&dev
->device_lock
);
1061 if (dev
->mei_state
!= MEI_ENABLED
)
1065 if (cl
== &dev
->iamthif_cl
) {
1066 mutex_unlock(&dev
->device_lock
);
1067 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
1068 mutex_lock(&dev
->device_lock
);
1069 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
1070 dev
->iamthif_file_object
== file
) {
1071 mask
|= (POLLIN
| POLLRDNORM
);
1072 dev_dbg(&dev
->pdev
->dev
, "run next amthi cb\n");
1073 mei_run_next_iamthif_cmd(dev
);
1078 mutex_unlock(&dev
->device_lock
);
1079 poll_wait(file
, &cl
->tx_wait
, wait
);
1080 mutex_lock(&dev
->device_lock
);
1081 if (MEI_WRITE_COMPLETE
== cl
->writing_state
)
1082 mask
|= (POLLIN
| POLLRDNORM
);
1085 mutex_unlock(&dev
->device_lock
);
1090 static int mei_pci_suspend(struct device
*device
)
1092 struct pci_dev
*pdev
= to_pci_dev(device
);
1093 struct mei_device
*dev
= pci_get_drvdata(pdev
);
1098 mutex_lock(&dev
->device_lock
);
1099 /* Stop watchdog if exists */
1100 err
= mei_wd_stop(dev
, true);
1101 /* Set new mei state */
1102 if (dev
->mei_state
== MEI_ENABLED
||
1103 dev
->mei_state
== MEI_RECOVERING_FROM_RESET
) {
1104 dev
->mei_state
= MEI_POWER_DOWN
;
1107 mutex_unlock(&dev
->device_lock
);
1109 free_irq(pdev
->irq
, dev
);
1110 pci_disable_msi(pdev
);
1115 static int mei_pci_resume(struct device
*device
)
1117 struct pci_dev
*pdev
= to_pci_dev(device
);
1118 struct mei_device
*dev
;
1121 dev
= pci_get_drvdata(pdev
);
1125 pci_enable_msi(pdev
);
1127 /* request and enable interrupt */
1128 if (pci_dev_msi_enabled(pdev
))
1129 err
= request_threaded_irq(pdev
->irq
,
1131 mei_interrupt_thread_handler
,
1132 0, mei_driver_name
, dev
);
1134 err
= request_threaded_irq(pdev
->irq
,
1135 mei_interrupt_quick_handler
,
1136 mei_interrupt_thread_handler
,
1137 IRQF_SHARED
, mei_driver_name
, dev
);
1140 printk(KERN_ERR
"mei: Request_irq failure. irq = %d\n",
1145 mutex_lock(&dev
->device_lock
);
1146 dev
->mei_state
= MEI_POWER_UP
;
1148 mutex_unlock(&dev
->device_lock
);
1150 /* Start watchdog if stopped in suspend */
1151 if (dev
->wd_timeout
) {
1152 mei_wd_start_setup(dev
);
1153 dev
->wd_due_counter
= 1;
1154 schedule_delayed_work(&dev
->wd_work
, HZ
);
1158 static SIMPLE_DEV_PM_OPS(mei_pm_ops
, mei_pci_suspend
, mei_pci_resume
);
1159 #define MEI_PM_OPS (&mei_pm_ops)
1161 #define MEI_PM_OPS NULL
1162 #endif /* CONFIG_PM */
1164 * PCI driver structure
1166 static struct pci_driver mei_driver
= {
1167 .name
= mei_driver_name
,
1168 .id_table
= mei_pci_tbl
,
1170 .remove
= __devexit_p(mei_remove
),
1171 .shutdown
= __devexit_p(mei_remove
),
1172 .driver
.pm
= MEI_PM_OPS
,
1176 * file operations structure will be used for mei char device.
1178 static const struct file_operations mei_fops
= {
1179 .owner
= THIS_MODULE
,
1181 .unlocked_ioctl
= mei_ioctl
,
1182 #ifdef CONFIG_COMPAT
1183 .compat_ioctl
= mei_compat_ioctl
,
1186 .release
= mei_release
,
1192 * mei_registration_cdev - sets up the cdev structure for mei device.
1194 * @dev: char device struct
1195 * @hminor: minor number for registration char device
1196 * @fops: file operations structure
1198 * returns 0 on success, <0 on failure.
1200 static int mei_registration_cdev(struct cdev
*dev
, int hminor
,
1201 const struct file_operations
*fops
)
1203 int ret
, devno
= MKDEV(mei_major
, hminor
);
1205 cdev_init(dev
, fops
);
1206 dev
->owner
= THIS_MODULE
;
1207 ret
= cdev_add(dev
, devno
, 1);
1208 /* Fail gracefully if need be */
1210 printk(KERN_ERR
"mei: Error %d registering mei device %d\n",
1216 * mei_register_cdev - registers mei char device
1218 * returns 0 on success, <0 on failure.
1220 static int mei_register_cdev(void)
1225 /* registration of char devices */
1226 ret
= alloc_chrdev_region(&dev
, MEI_MINORS_BASE
, MEI_MINORS_COUNT
,
1229 printk(KERN_ERR
"mei: Error allocating char device region.\n");
1233 mei_major
= MAJOR(dev
);
1235 ret
= mei_registration_cdev(&mei_cdev
, MEI_MINOR_NUMBER
,
1238 unregister_chrdev_region(MKDEV(mei_major
, MEI_MINORS_BASE
),
1245 * mei_unregister_cdev - unregisters mei char device
1247 static void mei_unregister_cdev(void)
1249 cdev_del(&mei_cdev
);
1250 unregister_chrdev_region(MKDEV(mei_major
, MEI_MINORS_BASE
),
1255 * mei_sysfs_device_create - adds device entry to sysfs
1257 * returns 0 on success, <0 on failure.
1259 static int mei_sysfs_device_create(void)
1261 struct class *class;
1265 class = class_create(THIS_MODULE
, MEI_DRIVER_NAME
);
1266 if (IS_ERR(class)) {
1267 err
= PTR_ERR(class);
1268 printk(KERN_ERR
"mei: Error creating mei class.\n");
1272 tmphdev
= device_create(class, NULL
, mei_cdev
.dev
, NULL
,
1274 if (IS_ERR(tmphdev
)) {
1275 err
= PTR_ERR(tmphdev
);
1283 class_destroy(class);
1289 * mei_sysfs_device_remove - unregisters the device entry on sysfs
1291 static void mei_sysfs_device_remove(void)
1293 if (IS_ERR_OR_NULL(mei_class
))
1296 device_destroy(mei_class
, mei_cdev
.dev
);
1297 class_destroy(mei_class
);
1301 * mei_init_module - Driver Registration Routine
1303 * mei_init_module is the first routine called when the driver is
1304 * loaded. All it does is to register with the PCI subsystem.
1306 * returns 0 on success, <0 on failure.
1308 static int __init
mei_init_module(void)
1312 pr_debug("mei: %s - version %s\n",
1313 mei_driver_string
, mei_driver_version
);
1314 /* init pci module */
1315 ret
= pci_register_driver(&mei_driver
);
1317 printk(KERN_ERR
"mei: Error registering driver.\n");
1321 ret
= mei_register_cdev();
1323 goto unregister_pci
;
1325 ret
= mei_sysfs_device_create();
1327 goto unregister_cdev
;
1332 mei_unregister_cdev();
1334 pci_unregister_driver(&mei_driver
);
1339 module_init(mei_init_module
);
1342 * mei_exit_module - Driver Exit Cleanup Routine
1344 * mei_exit_module is called just before the driver is removed
1347 static void __exit
mei_exit_module(void)
1349 mei_sysfs_device_remove();
1350 mei_unregister_cdev();
1351 pci_unregister_driver(&mei_driver
);
1353 pr_debug("mei: Driver unloaded successfully.\n");
1356 module_exit(mei_exit_module
);
1359 MODULE_AUTHOR("Intel Corporation");
1360 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1361 MODULE_LICENSE("GPL v2");
1362 MODULE_VERSION(MEI_DRIVER_VERSION
);