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 /* request and enable interrupt */
155 err
= request_threaded_irq(pdev
->irq
,
156 mei_interrupt_quick_handler
,
157 mei_interrupt_thread_handler
,
158 IRQF_SHARED
, mei_driver_name
, dev
);
160 printk(KERN_ERR
"mei: request_threaded_irq failure. irq = %d\n",
164 INIT_DELAYED_WORK(&dev
->wd_work
, mei_wd_timer
);
165 if (mei_hw_init(dev
)) {
166 printk(KERN_ERR
"mei: Init hw failure.\n");
171 pci_set_drvdata(pdev
, dev
);
172 schedule_delayed_work(&dev
->wd_work
, HZ
);
174 mutex_unlock(&mei_mutex
);
176 pr_debug("mei: Driver initialization successful.\n");
181 /* disable interrupts */
182 dev
->host_hw_state
= mei_hcsr_read(dev
);
183 mei_disable_interrupts(dev
);
184 flush_scheduled_work();
185 free_irq(pdev
->irq
, dev
);
187 pci_iounmap(pdev
, dev
->mem_addr
);
191 pci_release_regions(pdev
);
193 pci_disable_device(pdev
);
195 mutex_unlock(&mei_mutex
);
196 printk(KERN_ERR
"mei: Driver initialization failed.\n");
201 * mei_remove - Device Removal Routine
203 * @pdev: PCI device structure
205 * mei_remove is called by the PCI subsystem to alert the driver
206 * that it should release a PCI device.
208 static void __devexit
mei_remove(struct pci_dev
*pdev
)
210 struct mei_device
*dev
;
212 if (mei_device
!= pdev
)
215 dev
= pci_get_drvdata(pdev
);
219 mutex_lock(&dev
->device_lock
);
221 mei_wd_stop(dev
, false);
225 if (dev
->iamthif_cl
.state
== MEI_FILE_CONNECTED
) {
226 dev
->iamthif_cl
.state
= MEI_FILE_DISCONNECTING
;
227 mei_disconnect_host_client(dev
, &dev
->iamthif_cl
);
229 if (dev
->wd_cl
.state
== MEI_FILE_CONNECTED
) {
230 dev
->wd_cl
.state
= MEI_FILE_DISCONNECTING
;
231 mei_disconnect_host_client(dev
, &dev
->wd_cl
);
234 /* remove entry if already in list */
235 dev_dbg(&pdev
->dev
, "list del iamthif and wd file list.\n");
236 mei_remove_client_from_file_list(dev
, dev
->wd_cl
.host_client_id
);
237 mei_remove_client_from_file_list(dev
, dev
->iamthif_cl
.host_client_id
);
239 dev
->iamthif_current_cb
= NULL
;
240 dev
->me_clients_num
= 0;
242 mutex_unlock(&dev
->device_lock
);
244 flush_scheduled_work();
246 /* disable interrupts */
247 mei_disable_interrupts(dev
);
249 free_irq(pdev
->irq
, dev
);
250 pci_set_drvdata(pdev
, NULL
);
253 pci_iounmap(pdev
, dev
->mem_addr
);
257 pci_release_regions(pdev
);
258 pci_disable_device(pdev
);
262 * mei_clear_list - removes all callbacks associated with file
265 * @dev: device structure.
266 * @file: file structure
267 * @mei_cb_list: callbacks list
269 * mei_clear_list is called to clear resources associated with file
270 * when application calls close function or Ctrl-C was pressed
272 * returns true if callback removed from the list, false otherwise
274 static bool mei_clear_list(struct mei_device
*dev
,
275 struct file
*file
, struct list_head
*mei_cb_list
)
277 struct mei_cl_cb
*cb_pos
= NULL
;
278 struct mei_cl_cb
*cb_next
= NULL
;
279 struct file
*file_temp
;
280 bool removed
= false;
282 /* list all list member */
283 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, cb_list
) {
284 file_temp
= (struct file
*)cb_pos
->file_object
;
285 /* check if list member associated with a file */
286 if (file_temp
== file
) {
287 /* remove member from the list */
288 list_del(&cb_pos
->cb_list
);
289 /* check if cb equal to current iamthif cb */
290 if (dev
->iamthif_current_cb
== cb_pos
) {
291 dev
->iamthif_current_cb
= NULL
;
292 /* send flow control to iamthif client */
293 mei_send_flow_control(dev
, &dev
->iamthif_cl
);
295 /* free all allocated buffers */
296 mei_free_cb_private(cb_pos
);
305 * mei_clear_lists - removes all callbacks associated with file
307 * @dev: device structure
308 * @file: file structure
310 * mei_clear_lists is called to clear resources associated with file
311 * when application calls close function or Ctrl-C was pressed
313 * returns true if callback removed from the list, false otherwise
315 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
317 bool removed
= false;
319 /* remove callbacks associated with a file */
320 mei_clear_list(dev
, file
, &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
321 if (mei_clear_list(dev
, file
,
322 &dev
->amthi_read_complete_list
.mei_cb
.cb_list
))
325 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.mei_cb
.cb_list
);
327 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.mei_cb
.cb_list
))
330 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.mei_cb
.cb_list
))
333 if (mei_clear_list(dev
, file
, &dev
->write_list
.mei_cb
.cb_list
))
336 /* check if iamthif_current_cb not NULL */
337 if (dev
->iamthif_current_cb
&& !removed
) {
338 /* check file and iamthif current cb association */
339 if (dev
->iamthif_current_cb
->file_object
== file
) {
341 mei_free_cb_private(dev
->iamthif_current_cb
);
342 dev
->iamthif_current_cb
= NULL
;
349 * find_read_list_entry - find read list entry
351 * @dev: device structure
352 * @file: pointer to file structure
354 * returns cb on success, NULL on error
356 static struct mei_cl_cb
*find_read_list_entry(
357 struct mei_device
*dev
,
360 struct mei_cl_cb
*cb_pos
= NULL
;
361 struct mei_cl_cb
*cb_next
= NULL
;
363 if (!dev
->read_list
.status
&&
364 !list_empty(&dev
->read_list
.mei_cb
.cb_list
)) {
366 dev_dbg(&dev
->pdev
->dev
, "remove read_list CB\n");
367 list_for_each_entry_safe(cb_pos
, cb_next
,
368 &dev
->read_list
.mei_cb
.cb_list
, cb_list
) {
369 struct mei_cl
*cl_temp
;
370 cl_temp
= (struct mei_cl
*)cb_pos
->file_private
;
372 if (mei_cl_cmp_id(cl
, cl_temp
))
380 * mei_open - the open function
382 * @inode: pointer to inode structure
383 * @file: pointer to file structure
385 * returns 0 on success, <0 on error
387 static int mei_open(struct inode
*inode
, struct file
*file
)
390 int if_num
= iminor(inode
), err
;
391 struct mei_device
*dev
;
397 dev
= pci_get_drvdata(mei_device
);
398 if (if_num
!= MEI_MINOR_NUMBER
|| !dev
)
401 mutex_lock(&dev
->device_lock
);
403 cl
= mei_cl_allocate(dev
);
408 if (dev
->mei_state
!= MEI_ENABLED
) {
409 dev_dbg(&dev
->pdev
->dev
, "mei_state != MEI_ENABLED mei_state= %d\n",
414 if (dev
->open_handle_count
>= MEI_MAX_OPEN_HANDLE_COUNT
)
417 cl
->host_client_id
= find_first_zero_bit(dev
->host_clients_map
,
419 if (cl
->host_client_id
> MEI_CLIENTS_MAX
)
422 dev_dbg(&dev
->pdev
->dev
, "client_id = %d\n", cl
->host_client_id
);
424 dev
->open_handle_count
++;
425 list_add_tail(&cl
->link
, &dev
->file_list
);
427 set_bit(cl
->host_client_id
, dev
->host_clients_map
);
428 cl
->state
= MEI_FILE_INITIALIZING
;
431 file
->private_data
= cl
;
432 mutex_unlock(&dev
->device_lock
);
437 mutex_unlock(&dev
->device_lock
);
444 * mei_release - the release function
446 * @inode: pointer to inode structure
447 * @file: pointer to file structure
449 * returns 0 on success, <0 on error
451 static int mei_release(struct inode
*inode
, struct file
*file
)
453 struct mei_cl
*cl
= file
->private_data
;
454 struct mei_cl_cb
*cb
;
455 struct mei_device
*dev
;
458 if (WARN_ON(!cl
|| !cl
->dev
))
463 mutex_lock(&dev
->device_lock
);
464 if (cl
!= &dev
->iamthif_cl
) {
465 if (cl
->state
== MEI_FILE_CONNECTED
) {
466 cl
->state
= MEI_FILE_DISCONNECTING
;
467 dev_dbg(&dev
->pdev
->dev
,
468 "disconnecting client host client = %d, "
472 rets
= mei_disconnect_host_client(dev
, cl
);
474 mei_cl_flush_queues(cl
);
475 dev_dbg(&dev
->pdev
->dev
, "remove client host client = %d, ME client = %d\n",
479 if (dev
->open_handle_count
> 0) {
480 clear_bit(cl
->host_client_id
,
481 dev
->host_clients_map
);
482 dev
->open_handle_count
--;
484 mei_remove_client_from_file_list(dev
, cl
->host_client_id
);
489 cb
= find_read_list_entry(dev
, cl
);
490 /* Remove entry from read list */
492 list_del(&cb
->cb_list
);
498 file
->private_data
= NULL
;
501 mei_free_cb_private(cb
);
507 if (dev
->open_handle_count
> 0)
508 dev
->open_handle_count
--;
510 if (dev
->iamthif_file_object
== file
&&
511 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
513 dev_dbg(&dev
->pdev
->dev
, "amthi canceled iamthif state %d\n",
515 dev
->iamthif_canceled
= true;
516 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
517 dev_dbg(&dev
->pdev
->dev
, "run next amthi iamthif cb\n");
518 mei_run_next_iamthif_cmd(dev
);
522 if (mei_clear_lists(dev
, file
))
523 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
526 mutex_unlock(&dev
->device_lock
);
532 * mei_read - the read function.
534 * @file: pointer to file structure
535 * @ubuf: pointer to user buffer
536 * @length: buffer length
537 * @offset: data offset in buffer
539 * returns >=0 data length on success , <0 on error
541 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
542 size_t length
, loff_t
*offset
)
544 struct mei_cl
*cl
= file
->private_data
;
545 struct mei_cl_cb
*cb_pos
= NULL
;
546 struct mei_cl_cb
*cb
= NULL
;
547 struct mei_device
*dev
;
553 if (WARN_ON(!cl
|| !cl
->dev
))
558 mutex_lock(&dev
->device_lock
);
559 if (dev
->mei_state
!= MEI_ENABLED
) {
564 if ((cl
->sm_state
& MEI_WD_STATE_INDEPENDENCE_MSG_SENT
) == 0) {
565 /* Do not allow to read watchdog client */
566 i
= mei_find_me_client_index(dev
, mei_wd_guid
);
568 struct mei_me_client
*me_client
= &dev
->me_clients
[i
];
570 if (cl
->me_client_id
== me_client
->client_id
) {
576 cl
->sm_state
&= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
579 if (cl
== &dev
->iamthif_cl
) {
580 rets
= amthi_read(dev
, file
, ubuf
, length
, offset
);
584 if (cl
->read_cb
&& cl
->read_cb
->information
> *offset
) {
587 } else if (cl
->read_cb
&& cl
->read_cb
->information
> 0 &&
588 cl
->read_cb
->information
<= *offset
) {
592 } else if ((!cl
->read_cb
|| !cl
->read_cb
->information
) &&
594 /*Offset needs to be cleaned for contingous reads*/
600 err
= mei_start_read(dev
, cl
);
601 if (err
&& err
!= -EBUSY
) {
602 dev_dbg(&dev
->pdev
->dev
,
603 "mei start read failure with status = %d\n", err
);
608 if (MEI_READ_COMPLETE
!= cl
->reading_state
&&
609 !waitqueue_active(&cl
->rx_wait
)) {
610 if (file
->f_flags
& O_NONBLOCK
) {
615 mutex_unlock(&dev
->device_lock
);
617 if (wait_event_interruptible(cl
->rx_wait
,
618 (MEI_READ_COMPLETE
== cl
->reading_state
||
619 MEI_FILE_INITIALIZING
== cl
->state
||
620 MEI_FILE_DISCONNECTED
== cl
->state
||
621 MEI_FILE_DISCONNECTING
== cl
->state
))) {
622 if (signal_pending(current
))
627 mutex_lock(&dev
->device_lock
);
628 if (MEI_FILE_INITIALIZING
== cl
->state
||
629 MEI_FILE_DISCONNECTED
== cl
->state
||
630 MEI_FILE_DISCONNECTING
== cl
->state
) {
642 if (cl
->reading_state
!= MEI_READ_COMPLETE
) {
646 /* now copy the data to user space */
648 dev_dbg(&dev
->pdev
->dev
, "cb->response_buffer size - %d\n",
649 cb
->response_buffer
.size
);
650 dev_dbg(&dev
->pdev
->dev
, "cb->information - %lu\n",
652 if (length
== 0 || ubuf
== NULL
|| *offset
> cb
->information
) {
657 /* length is being turncated to PAGE_SIZE, however, */
658 /* information size may be longer */
659 length
= min_t(size_t, length
, (cb
->information
- *offset
));
661 if (copy_to_user(ubuf
,
662 cb
->response_buffer
.data
+ *offset
,
670 if ((unsigned long)*offset
< cb
->information
)
674 cb_pos
= find_read_list_entry(dev
, cl
);
675 /* Remove entry from read list */
677 list_del(&cb_pos
->cb_list
);
678 mei_free_cb_private(cb
);
679 cl
->reading_state
= MEI_IDLE
;
681 cl
->read_pending
= 0;
683 dev_dbg(&dev
->pdev
->dev
, "end mei read rets= %d\n", rets
);
684 mutex_unlock(&dev
->device_lock
);
689 * mei_write - the write function.
691 * @file: pointer to file structure
692 * @ubuf: pointer to user buffer
693 * @length: buffer length
694 * @offset: data offset in buffer
696 * returns >=0 data length on success , <0 on error
698 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
699 size_t length
, loff_t
*offset
)
701 struct mei_cl
*cl
= file
->private_data
;
702 struct mei_cl_cb
*write_cb
= NULL
;
703 struct mei_msg_hdr mei_hdr
;
704 struct mei_device
*dev
;
705 unsigned long timeout
= 0;
709 if (WARN_ON(!cl
|| !cl
->dev
))
714 mutex_lock(&dev
->device_lock
);
716 if (dev
->mei_state
!= MEI_ENABLED
) {
717 mutex_unlock(&dev
->device_lock
);
721 if (cl
== &dev
->iamthif_cl
) {
722 write_cb
= find_amthi_read_list_entry(dev
, file
);
725 timeout
= write_cb
->read_time
+
726 msecs_to_jiffies(IAMTHIF_READ_TIMER
);
728 if (time_after(jiffies
, timeout
) ||
729 cl
->reading_state
== MEI_READ_COMPLETE
) {
731 list_del(&write_cb
->cb_list
);
732 mei_free_cb_private(write_cb
);
738 /* free entry used in read */
739 if (cl
->reading_state
== MEI_READ_COMPLETE
) {
741 write_cb
= find_read_list_entry(dev
, cl
);
743 list_del(&write_cb
->cb_list
);
744 mei_free_cb_private(write_cb
);
746 cl
->reading_state
= MEI_IDLE
;
748 cl
->read_pending
= 0;
750 } else if (cl
->reading_state
== MEI_IDLE
&&
755 write_cb
= kzalloc(sizeof(struct mei_cl_cb
), GFP_KERNEL
);
757 mutex_unlock(&dev
->device_lock
);
761 write_cb
->file_object
= file
;
762 write_cb
->file_private
= cl
;
763 write_cb
->request_buffer
.data
= kmalloc(length
, GFP_KERNEL
);
765 if (!write_cb
->request_buffer
.data
)
768 dev_dbg(&dev
->pdev
->dev
, "length =%d\n", (int) length
);
771 if (copy_from_user(write_cb
->request_buffer
.data
, ubuf
, length
))
776 ((memcmp(mei_wd_state_independence_msg
[0],
777 write_cb
->request_buffer
.data
, 4) == 0) ||
778 (memcmp(mei_wd_state_independence_msg
[1],
779 write_cb
->request_buffer
.data
, 4) == 0) ||
780 (memcmp(mei_wd_state_independence_msg
[2],
781 write_cb
->request_buffer
.data
, 4) == 0)))
782 cl
->sm_state
|= MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
784 INIT_LIST_HEAD(&write_cb
->cb_list
);
785 if (cl
== &dev
->iamthif_cl
) {
786 write_cb
->response_buffer
.data
=
787 kmalloc(dev
->iamthif_mtu
, GFP_KERNEL
);
788 if (!write_cb
->response_buffer
.data
) {
792 if (dev
->mei_state
!= MEI_ENABLED
) {
796 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
797 if (dev
->me_clients
[i
].client_id
==
798 dev
->iamthif_cl
.me_client_id
)
802 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
806 if (i
== dev
->me_clients_num
||
807 (dev
->me_clients
[i
].client_id
!=
808 dev
->iamthif_cl
.me_client_id
)) {
811 } else if (length
> dev
->me_clients
[i
].props
.max_msg_length
||
817 write_cb
->response_buffer
.size
= dev
->iamthif_mtu
;
818 write_cb
->major_file_operations
= MEI_IOCTL
;
819 write_cb
->information
= 0;
820 write_cb
->request_buffer
.size
= length
;
821 if (dev
->iamthif_cl
.state
!= MEI_FILE_CONNECTED
) {
826 if (!list_empty(&dev
->amthi_cmd_list
.mei_cb
.cb_list
) ||
827 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
828 dev_dbg(&dev
->pdev
->dev
, "amthi_state = %d\n",
829 (int) dev
->iamthif_state
);
830 dev_dbg(&dev
->pdev
->dev
, "add amthi cb to amthi cmd waiting list\n");
831 list_add_tail(&write_cb
->cb_list
,
832 &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
835 dev_dbg(&dev
->pdev
->dev
, "call amthi write\n");
836 rets
= amthi_write(dev
, write_cb
);
839 dev_dbg(&dev
->pdev
->dev
, "amthi write failed with status = %d\n",
845 mutex_unlock(&dev
->device_lock
);
849 write_cb
->major_file_operations
= MEI_WRITE
;
850 /* make sure information is zero before we start */
852 write_cb
->information
= 0;
853 write_cb
->request_buffer
.size
= length
;
855 dev_dbg(&dev
->pdev
->dev
, "host client = %d, ME client = %d\n",
856 cl
->host_client_id
, cl
->me_client_id
);
857 if (cl
->state
!= MEI_FILE_CONNECTED
) {
859 dev_dbg(&dev
->pdev
->dev
, "host client = %d, is not connected to ME client = %d",
864 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
865 if (dev
->me_clients
[i
].client_id
==
869 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
873 if (i
== dev
->me_clients_num
) {
877 if (length
> dev
->me_clients
[i
].props
.max_msg_length
|| length
<= 0) {
881 write_cb
->file_private
= cl
;
883 rets
= mei_flow_ctrl_creds(dev
, cl
);
887 if (rets
&& dev
->mei_host_buffer_is_empty
) {
889 dev
->mei_host_buffer_is_empty
= false;
890 if (length
> ((((dev
->host_hw_state
& H_CBD
) >> 24) *
891 sizeof(u32
)) - sizeof(struct mei_msg_hdr
))) {
894 (((dev
->host_hw_state
& H_CBD
) >> 24) *
896 sizeof(struct mei_msg_hdr
);
897 mei_hdr
.msg_complete
= 0;
899 mei_hdr
.length
= length
;
900 mei_hdr
.msg_complete
= 1;
902 mei_hdr
.host_addr
= cl
->host_client_id
;
903 mei_hdr
.me_addr
= cl
->me_client_id
;
904 mei_hdr
.reserved
= 0;
905 dev_dbg(&dev
->pdev
->dev
, "call mei_write_message header=%08x.\n",
906 *((u32
*) &mei_hdr
));
907 if (!mei_write_message(dev
, &mei_hdr
,
908 (unsigned char *) (write_cb
->request_buffer
.data
),
913 cl
->writing_state
= MEI_WRITING
;
914 write_cb
->information
= mei_hdr
.length
;
915 if (mei_hdr
.msg_complete
) {
916 if (mei_flow_ctrl_reduce(dev
, cl
)) {
920 list_add_tail(&write_cb
->cb_list
,
921 &dev
->write_waiting_list
.mei_cb
.cb_list
);
923 list_add_tail(&write_cb
->cb_list
,
924 &dev
->write_list
.mei_cb
.cb_list
);
929 write_cb
->information
= 0;
930 cl
->writing_state
= MEI_WRITING
;
931 list_add_tail(&write_cb
->cb_list
,
932 &dev
->write_list
.mei_cb
.cb_list
);
934 mutex_unlock(&dev
->device_lock
);
938 mutex_unlock(&dev
->device_lock
);
939 mei_free_cb_private(write_cb
);
945 * mei_ioctl - the IOCTL function
947 * @file: pointer to file structure
948 * @cmd: ioctl command
949 * @data: pointer to mei message structure
951 * returns 0 on success , <0 on error
953 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
955 struct mei_device
*dev
;
956 struct mei_cl
*cl
= file
->private_data
;
957 struct mei_connect_client_data
*connect_data
= NULL
;
960 if (cmd
!= IOCTL_MEI_CONNECT_CLIENT
)
963 if (WARN_ON(!cl
|| !cl
->dev
))
968 dev_dbg(&dev
->pdev
->dev
, "IOCTL cmd = 0x%x", cmd
);
970 mutex_lock(&dev
->device_lock
);
971 if (dev
->mei_state
!= MEI_ENABLED
) {
976 dev_dbg(&dev
->pdev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
978 connect_data
= kzalloc(sizeof(struct mei_connect_client_data
),
984 dev_dbg(&dev
->pdev
->dev
, "copy connect data from user\n");
985 if (copy_from_user(connect_data
, (char __user
*)data
,
986 sizeof(struct mei_connect_client_data
))) {
987 dev_dbg(&dev
->pdev
->dev
, "failed to copy data from userland\n");
991 rets
= mei_ioctl_connect_client(file
, connect_data
);
993 /* if all is ok, copying the data back to user. */
997 dev_dbg(&dev
->pdev
->dev
, "copy connect data to user\n");
998 if (copy_to_user((char __user
*)data
, connect_data
,
999 sizeof(struct mei_connect_client_data
))) {
1000 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
1006 kfree(connect_data
);
1007 mutex_unlock(&dev
->device_lock
);
1012 * mei_compat_ioctl - the compat IOCTL function
1014 * @file: pointer to file structure
1015 * @cmd: ioctl command
1016 * @data: pointer to mei message structure
1018 * returns 0 on success , <0 on error
1020 #ifdef CONFIG_COMPAT
1021 static long mei_compat_ioctl(struct file
*file
,
1022 unsigned int cmd
, unsigned long data
)
1024 return mei_ioctl(file
, cmd
, (unsigned long)compat_ptr(data
));
1030 * mei_poll - the poll function
1032 * @file: pointer to file structure
1033 * @wait: pointer to poll_table structure
1037 static unsigned int mei_poll(struct file
*file
, poll_table
*wait
)
1039 struct mei_cl
*cl
= file
->private_data
;
1040 struct mei_device
*dev
;
1041 unsigned int mask
= 0;
1043 if (WARN_ON(!cl
|| !cl
->dev
))
1048 mutex_lock(&dev
->device_lock
);
1050 if (dev
->mei_state
!= MEI_ENABLED
)
1054 if (cl
== &dev
->iamthif_cl
) {
1055 mutex_unlock(&dev
->device_lock
);
1056 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
1057 mutex_lock(&dev
->device_lock
);
1058 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
1059 dev
->iamthif_file_object
== file
) {
1060 mask
|= (POLLIN
| POLLRDNORM
);
1061 dev_dbg(&dev
->pdev
->dev
, "run next amthi cb\n");
1062 mei_run_next_iamthif_cmd(dev
);
1067 mutex_unlock(&dev
->device_lock
);
1068 poll_wait(file
, &cl
->tx_wait
, wait
);
1069 mutex_lock(&dev
->device_lock
);
1070 if (MEI_WRITE_COMPLETE
== cl
->writing_state
)
1071 mask
|= (POLLIN
| POLLRDNORM
);
1074 mutex_unlock(&dev
->device_lock
);
1079 static int mei_pci_suspend(struct device
*device
)
1081 struct pci_dev
*pdev
= to_pci_dev(device
);
1082 struct mei_device
*dev
= pci_get_drvdata(pdev
);
1087 mutex_lock(&dev
->device_lock
);
1088 /* Stop watchdog if exists */
1089 err
= mei_wd_stop(dev
, true);
1090 /* Set new mei state */
1091 if (dev
->mei_state
== MEI_ENABLED
||
1092 dev
->mei_state
== MEI_RECOVERING_FROM_RESET
) {
1093 dev
->mei_state
= MEI_POWER_DOWN
;
1096 mutex_unlock(&dev
->device_lock
);
1098 free_irq(pdev
->irq
, dev
);
1104 static int mei_pci_resume(struct device
*device
)
1106 struct pci_dev
*pdev
= to_pci_dev(device
);
1107 struct mei_device
*dev
;
1110 dev
= pci_get_drvdata(pdev
);
1114 /* request and enable interrupt */
1115 err
= request_threaded_irq(pdev
->irq
,
1116 mei_interrupt_quick_handler
,
1117 mei_interrupt_thread_handler
,
1118 IRQF_SHARED
, mei_driver_name
, dev
);
1120 printk(KERN_ERR
"mei: Request_irq failure. irq = %d\n",
1125 mutex_lock(&dev
->device_lock
);
1126 dev
->mei_state
= MEI_POWER_UP
;
1128 mutex_unlock(&dev
->device_lock
);
1130 /* Start watchdog if stopped in suspend */
1131 if (dev
->wd_timeout
) {
1132 mei_wd_start_setup(dev
);
1133 dev
->wd_due_counter
= 1;
1134 schedule_delayed_work(&dev
->wd_work
, HZ
);
1138 static SIMPLE_DEV_PM_OPS(mei_pm_ops
, mei_pci_suspend
, mei_pci_resume
);
1139 #define MEI_PM_OPS (&mei_pm_ops)
1141 #define MEI_PM_OPS NULL
1142 #endif /* CONFIG_PM */
1144 * PCI driver structure
1146 static struct pci_driver mei_driver
= {
1147 .name
= mei_driver_name
,
1148 .id_table
= mei_pci_tbl
,
1150 .remove
= __devexit_p(mei_remove
),
1151 .shutdown
= __devexit_p(mei_remove
),
1152 .driver
.pm
= MEI_PM_OPS
,
1156 * file operations structure will be used for mei char device.
1158 static const struct file_operations mei_fops
= {
1159 .owner
= THIS_MODULE
,
1161 .unlocked_ioctl
= mei_ioctl
,
1162 #ifdef CONFIG_COMPAT
1163 .compat_ioctl
= mei_compat_ioctl
,
1166 .release
= mei_release
,
1172 * mei_registration_cdev - sets up the cdev structure for mei device.
1174 * @dev: char device struct
1175 * @hminor: minor number for registration char device
1176 * @fops: file operations structure
1178 * returns 0 on success, <0 on failure.
1180 static int mei_registration_cdev(struct cdev
*dev
, int hminor
,
1181 const struct file_operations
*fops
)
1183 int ret
, devno
= MKDEV(mei_major
, hminor
);
1185 cdev_init(dev
, fops
);
1186 dev
->owner
= THIS_MODULE
;
1187 ret
= cdev_add(dev
, devno
, 1);
1188 /* Fail gracefully if need be */
1190 printk(KERN_ERR
"mei: Error %d registering mei device %d\n",
1196 * mei_register_cdev - registers mei char device
1198 * returns 0 on success, <0 on failure.
1200 static int mei_register_cdev(void)
1205 /* registration of char devices */
1206 ret
= alloc_chrdev_region(&dev
, MEI_MINORS_BASE
, MEI_MINORS_COUNT
,
1209 printk(KERN_ERR
"mei: Error allocating char device region.\n");
1213 mei_major
= MAJOR(dev
);
1215 ret
= mei_registration_cdev(&mei_cdev
, MEI_MINOR_NUMBER
,
1218 unregister_chrdev_region(MKDEV(mei_major
, MEI_MINORS_BASE
),
1225 * mei_unregister_cdev - unregisters mei char device
1227 static void mei_unregister_cdev(void)
1229 cdev_del(&mei_cdev
);
1230 unregister_chrdev_region(MKDEV(mei_major
, MEI_MINORS_BASE
),
1235 * mei_sysfs_device_create - adds device entry to sysfs
1237 * returns 0 on success, <0 on failure.
1239 static int mei_sysfs_device_create(void)
1241 struct class *class;
1245 class = class_create(THIS_MODULE
, MEI_DRIVER_NAME
);
1246 if (IS_ERR(class)) {
1247 err
= PTR_ERR(class);
1248 printk(KERN_ERR
"mei: Error creating mei class.\n");
1252 tmphdev
= device_create(class, NULL
, mei_cdev
.dev
, NULL
,
1254 if (IS_ERR(tmphdev
)) {
1255 err
= PTR_ERR(tmphdev
);
1263 class_destroy(class);
1269 * mei_sysfs_device_remove - unregisters the device entry on sysfs
1271 static void mei_sysfs_device_remove(void)
1273 if (IS_ERR_OR_NULL(mei_class
))
1276 device_destroy(mei_class
, mei_cdev
.dev
);
1277 class_destroy(mei_class
);
1281 * mei_init_module - Driver Registration Routine
1283 * mei_init_module is the first routine called when the driver is
1284 * loaded. All it does is to register with the PCI subsystem.
1286 * returns 0 on success, <0 on failure.
1288 static int __init
mei_init_module(void)
1292 pr_debug("mei: %s - version %s\n",
1293 mei_driver_string
, mei_driver_version
);
1294 /* init pci module */
1295 ret
= pci_register_driver(&mei_driver
);
1297 printk(KERN_ERR
"mei: Error registering driver.\n");
1301 ret
= mei_register_cdev();
1303 goto unregister_pci
;
1305 ret
= mei_sysfs_device_create();
1307 goto unregister_cdev
;
1312 mei_unregister_cdev();
1314 pci_unregister_driver(&mei_driver
);
1319 module_init(mei_init_module
);
1322 * mei_exit_module - Driver Exit Cleanup Routine
1324 * mei_exit_module is called just before the driver is removed
1327 static void __exit
mei_exit_module(void)
1329 mei_sysfs_device_remove();
1330 mei_unregister_cdev();
1331 pci_unregister_driver(&mei_driver
);
1333 pr_debug("mei: Driver unloaded successfully.\n");
1336 module_exit(mei_exit_module
);
1339 MODULE_AUTHOR("Intel Corporation");
1340 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1341 MODULE_LICENSE("GPL v2");
1342 MODULE_VERSION(MEI_DRIVER_VERSION
);