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_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
->timer_work
, mei_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
->timer_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 /* Unregistering watchdog device */
245 if (dev
->wd_interface_reg
)
246 watchdog_unregister_device(&amt_wd_dev
);
248 /* remove entry if already in list */
249 dev_dbg(&pdev
->dev
, "list del iamthif and wd file list.\n");
250 mei_remove_client_from_file_list(dev
, dev
->wd_cl
.host_client_id
);
251 mei_remove_client_from_file_list(dev
, dev
->iamthif_cl
.host_client_id
);
253 dev
->iamthif_current_cb
= NULL
;
254 dev
->me_clients_num
= 0;
256 mutex_unlock(&dev
->device_lock
);
258 flush_scheduled_work();
260 /* disable interrupts */
261 mei_disable_interrupts(dev
);
263 free_irq(pdev
->irq
, dev
);
264 pci_disable_msi(pdev
);
265 pci_set_drvdata(pdev
, NULL
);
268 pci_iounmap(pdev
, dev
->mem_addr
);
272 pci_release_regions(pdev
);
273 pci_disable_device(pdev
);
277 * mei_clear_list - removes all callbacks associated with file
280 * @dev: device structure.
281 * @file: file structure
282 * @mei_cb_list: callbacks list
284 * mei_clear_list is called to clear resources associated with file
285 * when application calls close function or Ctrl-C was pressed
287 * returns true if callback removed from the list, false otherwise
289 static bool mei_clear_list(struct mei_device
*dev
,
290 struct file
*file
, struct list_head
*mei_cb_list
)
292 struct mei_cl_cb
*cb_pos
= NULL
;
293 struct mei_cl_cb
*cb_next
= NULL
;
294 struct file
*file_temp
;
295 bool removed
= false;
297 /* list all list member */
298 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, cb_list
) {
299 file_temp
= (struct file
*)cb_pos
->file_object
;
300 /* check if list member associated with a file */
301 if (file_temp
== file
) {
302 /* remove member from the list */
303 list_del(&cb_pos
->cb_list
);
304 /* check if cb equal to current iamthif cb */
305 if (dev
->iamthif_current_cb
== cb_pos
) {
306 dev
->iamthif_current_cb
= NULL
;
307 /* send flow control to iamthif client */
308 mei_send_flow_control(dev
, &dev
->iamthif_cl
);
310 /* free all allocated buffers */
311 mei_free_cb_private(cb_pos
);
320 * mei_clear_lists - removes all callbacks associated with file
322 * @dev: device structure
323 * @file: file structure
325 * mei_clear_lists is called to clear resources associated with file
326 * when application calls close function or Ctrl-C was pressed
328 * returns true if callback removed from the list, false otherwise
330 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
332 bool removed
= false;
334 /* remove callbacks associated with a file */
335 mei_clear_list(dev
, file
, &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
336 if (mei_clear_list(dev
, file
,
337 &dev
->amthi_read_complete_list
.mei_cb
.cb_list
))
340 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.mei_cb
.cb_list
);
342 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.mei_cb
.cb_list
))
345 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.mei_cb
.cb_list
))
348 if (mei_clear_list(dev
, file
, &dev
->write_list
.mei_cb
.cb_list
))
351 /* check if iamthif_current_cb not NULL */
352 if (dev
->iamthif_current_cb
&& !removed
) {
353 /* check file and iamthif current cb association */
354 if (dev
->iamthif_current_cb
->file_object
== file
) {
356 mei_free_cb_private(dev
->iamthif_current_cb
);
357 dev
->iamthif_current_cb
= NULL
;
364 * find_read_list_entry - find read list entry
366 * @dev: device structure
367 * @file: pointer to file structure
369 * returns cb on success, NULL on error
371 static struct mei_cl_cb
*find_read_list_entry(
372 struct mei_device
*dev
,
375 struct mei_cl_cb
*cb_pos
= NULL
;
376 struct mei_cl_cb
*cb_next
= NULL
;
378 if (!dev
->read_list
.status
&&
379 !list_empty(&dev
->read_list
.mei_cb
.cb_list
)) {
381 dev_dbg(&dev
->pdev
->dev
, "remove read_list CB\n");
382 list_for_each_entry_safe(cb_pos
, cb_next
,
383 &dev
->read_list
.mei_cb
.cb_list
, cb_list
) {
384 struct mei_cl
*cl_temp
;
385 cl_temp
= (struct mei_cl
*)cb_pos
->file_private
;
387 if (mei_cl_cmp_id(cl
, cl_temp
))
395 * mei_open - the open function
397 * @inode: pointer to inode structure
398 * @file: pointer to file structure
400 * returns 0 on success, <0 on error
402 static int mei_open(struct inode
*inode
, struct file
*file
)
405 int if_num
= iminor(inode
), err
;
406 struct mei_device
*dev
;
412 dev
= pci_get_drvdata(mei_device
);
413 if (if_num
!= MEI_MINOR_NUMBER
|| !dev
)
416 mutex_lock(&dev
->device_lock
);
418 cl
= mei_cl_allocate(dev
);
423 if (dev
->mei_state
!= MEI_ENABLED
) {
424 dev_dbg(&dev
->pdev
->dev
, "mei_state != MEI_ENABLED mei_state= %d\n",
429 if (dev
->open_handle_count
>= MEI_MAX_OPEN_HANDLE_COUNT
)
432 cl
->host_client_id
= find_first_zero_bit(dev
->host_clients_map
,
434 if (cl
->host_client_id
> MEI_CLIENTS_MAX
)
437 dev_dbg(&dev
->pdev
->dev
, "client_id = %d\n", cl
->host_client_id
);
439 dev
->open_handle_count
++;
440 list_add_tail(&cl
->link
, &dev
->file_list
);
442 set_bit(cl
->host_client_id
, dev
->host_clients_map
);
443 cl
->state
= MEI_FILE_INITIALIZING
;
446 file
->private_data
= cl
;
447 mutex_unlock(&dev
->device_lock
);
452 mutex_unlock(&dev
->device_lock
);
459 * mei_release - the release function
461 * @inode: pointer to inode structure
462 * @file: pointer to file structure
464 * returns 0 on success, <0 on error
466 static int mei_release(struct inode
*inode
, struct file
*file
)
468 struct mei_cl
*cl
= file
->private_data
;
469 struct mei_cl_cb
*cb
;
470 struct mei_device
*dev
;
473 if (WARN_ON(!cl
|| !cl
->dev
))
478 mutex_lock(&dev
->device_lock
);
479 if (cl
!= &dev
->iamthif_cl
) {
480 if (cl
->state
== MEI_FILE_CONNECTED
) {
481 cl
->state
= MEI_FILE_DISCONNECTING
;
482 dev_dbg(&dev
->pdev
->dev
,
483 "disconnecting client host client = %d, "
487 rets
= mei_disconnect_host_client(dev
, cl
);
489 mei_cl_flush_queues(cl
);
490 dev_dbg(&dev
->pdev
->dev
, "remove client host client = %d, ME client = %d\n",
494 if (dev
->open_handle_count
> 0) {
495 clear_bit(cl
->host_client_id
,
496 dev
->host_clients_map
);
497 dev
->open_handle_count
--;
499 mei_remove_client_from_file_list(dev
, cl
->host_client_id
);
504 cb
= find_read_list_entry(dev
, cl
);
505 /* Remove entry from read list */
507 list_del(&cb
->cb_list
);
513 file
->private_data
= NULL
;
516 mei_free_cb_private(cb
);
522 if (dev
->open_handle_count
> 0)
523 dev
->open_handle_count
--;
525 if (dev
->iamthif_file_object
== file
&&
526 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
528 dev_dbg(&dev
->pdev
->dev
, "amthi canceled iamthif state %d\n",
530 dev
->iamthif_canceled
= true;
531 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
532 dev_dbg(&dev
->pdev
->dev
, "run next amthi iamthif cb\n");
533 mei_run_next_iamthif_cmd(dev
);
537 if (mei_clear_lists(dev
, file
))
538 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
541 mutex_unlock(&dev
->device_lock
);
547 * mei_read - the read function.
549 * @file: pointer to file structure
550 * @ubuf: pointer to user buffer
551 * @length: buffer length
552 * @offset: data offset in buffer
554 * returns >=0 data length on success , <0 on error
556 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
557 size_t length
, loff_t
*offset
)
559 struct mei_cl
*cl
= file
->private_data
;
560 struct mei_cl_cb
*cb_pos
= NULL
;
561 struct mei_cl_cb
*cb
= NULL
;
562 struct mei_device
*dev
;
568 if (WARN_ON(!cl
|| !cl
->dev
))
573 mutex_lock(&dev
->device_lock
);
574 if (dev
->mei_state
!= MEI_ENABLED
) {
579 if ((cl
->sm_state
& MEI_WD_STATE_INDEPENDENCE_MSG_SENT
) == 0) {
580 /* Do not allow to read watchdog client */
581 i
= mei_find_me_client_index(dev
, mei_wd_guid
);
583 struct mei_me_client
*me_client
= &dev
->me_clients
[i
];
585 if (cl
->me_client_id
== me_client
->client_id
) {
591 cl
->sm_state
&= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
594 if (cl
== &dev
->iamthif_cl
) {
595 rets
= amthi_read(dev
, file
, ubuf
, length
, offset
);
599 if (cl
->read_cb
&& cl
->read_cb
->information
> *offset
) {
602 } else if (cl
->read_cb
&& cl
->read_cb
->information
> 0 &&
603 cl
->read_cb
->information
<= *offset
) {
607 } else if ((!cl
->read_cb
|| !cl
->read_cb
->information
) &&
609 /*Offset needs to be cleaned for contingous reads*/
615 err
= mei_start_read(dev
, cl
);
616 if (err
&& err
!= -EBUSY
) {
617 dev_dbg(&dev
->pdev
->dev
,
618 "mei start read failure with status = %d\n", err
);
623 if (MEI_READ_COMPLETE
!= cl
->reading_state
&&
624 !waitqueue_active(&cl
->rx_wait
)) {
625 if (file
->f_flags
& O_NONBLOCK
) {
630 mutex_unlock(&dev
->device_lock
);
632 if (wait_event_interruptible(cl
->rx_wait
,
633 (MEI_READ_COMPLETE
== cl
->reading_state
||
634 MEI_FILE_INITIALIZING
== cl
->state
||
635 MEI_FILE_DISCONNECTED
== cl
->state
||
636 MEI_FILE_DISCONNECTING
== cl
->state
))) {
637 if (signal_pending(current
))
642 mutex_lock(&dev
->device_lock
);
643 if (MEI_FILE_INITIALIZING
== cl
->state
||
644 MEI_FILE_DISCONNECTED
== cl
->state
||
645 MEI_FILE_DISCONNECTING
== cl
->state
) {
657 if (cl
->reading_state
!= MEI_READ_COMPLETE
) {
661 /* now copy the data to user space */
663 dev_dbg(&dev
->pdev
->dev
, "cb->response_buffer size - %d\n",
664 cb
->response_buffer
.size
);
665 dev_dbg(&dev
->pdev
->dev
, "cb->information - %lu\n",
667 if (length
== 0 || ubuf
== NULL
|| *offset
> cb
->information
) {
672 /* length is being turncated to PAGE_SIZE, however, */
673 /* information size may be longer */
674 length
= min_t(size_t, length
, (cb
->information
- *offset
));
676 if (copy_to_user(ubuf
,
677 cb
->response_buffer
.data
+ *offset
,
685 if ((unsigned long)*offset
< cb
->information
)
689 cb_pos
= find_read_list_entry(dev
, cl
);
690 /* Remove entry from read list */
692 list_del(&cb_pos
->cb_list
);
693 mei_free_cb_private(cb
);
694 cl
->reading_state
= MEI_IDLE
;
696 cl
->read_pending
= 0;
698 dev_dbg(&dev
->pdev
->dev
, "end mei read rets= %d\n", rets
);
699 mutex_unlock(&dev
->device_lock
);
704 * mei_write - the write function.
706 * @file: pointer to file structure
707 * @ubuf: pointer to user buffer
708 * @length: buffer length
709 * @offset: data offset in buffer
711 * returns >=0 data length on success , <0 on error
713 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
714 size_t length
, loff_t
*offset
)
716 struct mei_cl
*cl
= file
->private_data
;
717 struct mei_cl_cb
*write_cb
= NULL
;
718 struct mei_msg_hdr mei_hdr
;
719 struct mei_device
*dev
;
720 unsigned long timeout
= 0;
724 if (WARN_ON(!cl
|| !cl
->dev
))
729 mutex_lock(&dev
->device_lock
);
731 if (dev
->mei_state
!= MEI_ENABLED
) {
732 mutex_unlock(&dev
->device_lock
);
736 if (cl
== &dev
->iamthif_cl
) {
737 write_cb
= find_amthi_read_list_entry(dev
, file
);
740 timeout
= write_cb
->read_time
+
741 msecs_to_jiffies(IAMTHIF_READ_TIMER
);
743 if (time_after(jiffies
, timeout
) ||
744 cl
->reading_state
== MEI_READ_COMPLETE
) {
746 list_del(&write_cb
->cb_list
);
747 mei_free_cb_private(write_cb
);
753 /* free entry used in read */
754 if (cl
->reading_state
== MEI_READ_COMPLETE
) {
756 write_cb
= find_read_list_entry(dev
, cl
);
758 list_del(&write_cb
->cb_list
);
759 mei_free_cb_private(write_cb
);
761 cl
->reading_state
= MEI_IDLE
;
763 cl
->read_pending
= 0;
765 } else if (cl
->reading_state
== MEI_IDLE
&&
770 write_cb
= kzalloc(sizeof(struct mei_cl_cb
), GFP_KERNEL
);
772 mutex_unlock(&dev
->device_lock
);
776 write_cb
->file_object
= file
;
777 write_cb
->file_private
= cl
;
778 write_cb
->request_buffer
.data
= kmalloc(length
, GFP_KERNEL
);
780 if (!write_cb
->request_buffer
.data
)
783 dev_dbg(&dev
->pdev
->dev
, "length =%d\n", (int) length
);
786 if (copy_from_user(write_cb
->request_buffer
.data
, ubuf
, length
))
791 ((memcmp(mei_wd_state_independence_msg
[0],
792 write_cb
->request_buffer
.data
, 4) == 0) ||
793 (memcmp(mei_wd_state_independence_msg
[1],
794 write_cb
->request_buffer
.data
, 4) == 0) ||
795 (memcmp(mei_wd_state_independence_msg
[2],
796 write_cb
->request_buffer
.data
, 4) == 0)))
797 cl
->sm_state
|= MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
799 INIT_LIST_HEAD(&write_cb
->cb_list
);
800 if (cl
== &dev
->iamthif_cl
) {
801 write_cb
->response_buffer
.data
=
802 kmalloc(dev
->iamthif_mtu
, GFP_KERNEL
);
803 if (!write_cb
->response_buffer
.data
) {
807 if (dev
->mei_state
!= MEI_ENABLED
) {
811 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
812 if (dev
->me_clients
[i
].client_id
==
813 dev
->iamthif_cl
.me_client_id
)
817 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
821 if (i
== dev
->me_clients_num
||
822 (dev
->me_clients
[i
].client_id
!=
823 dev
->iamthif_cl
.me_client_id
)) {
826 } else if (length
> dev
->me_clients
[i
].props
.max_msg_length
||
832 write_cb
->response_buffer
.size
= dev
->iamthif_mtu
;
833 write_cb
->major_file_operations
= MEI_IOCTL
;
834 write_cb
->information
= 0;
835 write_cb
->request_buffer
.size
= length
;
836 if (dev
->iamthif_cl
.state
!= MEI_FILE_CONNECTED
) {
841 if (!list_empty(&dev
->amthi_cmd_list
.mei_cb
.cb_list
) ||
842 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
843 dev_dbg(&dev
->pdev
->dev
, "amthi_state = %d\n",
844 (int) dev
->iamthif_state
);
845 dev_dbg(&dev
->pdev
->dev
, "add amthi cb to amthi cmd waiting list\n");
846 list_add_tail(&write_cb
->cb_list
,
847 &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
850 dev_dbg(&dev
->pdev
->dev
, "call amthi write\n");
851 rets
= amthi_write(dev
, write_cb
);
854 dev_dbg(&dev
->pdev
->dev
, "amthi write failed with status = %d\n",
860 mutex_unlock(&dev
->device_lock
);
864 write_cb
->major_file_operations
= MEI_WRITE
;
865 /* make sure information is zero before we start */
867 write_cb
->information
= 0;
868 write_cb
->request_buffer
.size
= length
;
870 dev_dbg(&dev
->pdev
->dev
, "host client = %d, ME client = %d\n",
871 cl
->host_client_id
, cl
->me_client_id
);
872 if (cl
->state
!= MEI_FILE_CONNECTED
) {
874 dev_dbg(&dev
->pdev
->dev
, "host client = %d, is not connected to ME client = %d",
879 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
880 if (dev
->me_clients
[i
].client_id
==
884 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
888 if (i
== dev
->me_clients_num
) {
892 if (length
> dev
->me_clients
[i
].props
.max_msg_length
|| length
<= 0) {
896 write_cb
->file_private
= cl
;
898 rets
= mei_flow_ctrl_creds(dev
, cl
);
902 if (rets
&& dev
->mei_host_buffer_is_empty
) {
904 dev
->mei_host_buffer_is_empty
= false;
905 if (length
> ((((dev
->host_hw_state
& H_CBD
) >> 24) *
906 sizeof(u32
)) - sizeof(struct mei_msg_hdr
))) {
909 (((dev
->host_hw_state
& H_CBD
) >> 24) *
911 sizeof(struct mei_msg_hdr
);
912 mei_hdr
.msg_complete
= 0;
914 mei_hdr
.length
= length
;
915 mei_hdr
.msg_complete
= 1;
917 mei_hdr
.host_addr
= cl
->host_client_id
;
918 mei_hdr
.me_addr
= cl
->me_client_id
;
919 mei_hdr
.reserved
= 0;
920 dev_dbg(&dev
->pdev
->dev
, "call mei_write_message header=%08x.\n",
921 *((u32
*) &mei_hdr
));
922 if (!mei_write_message(dev
, &mei_hdr
,
923 (unsigned char *) (write_cb
->request_buffer
.data
),
928 cl
->writing_state
= MEI_WRITING
;
929 write_cb
->information
= mei_hdr
.length
;
930 if (mei_hdr
.msg_complete
) {
931 if (mei_flow_ctrl_reduce(dev
, cl
)) {
935 list_add_tail(&write_cb
->cb_list
,
936 &dev
->write_waiting_list
.mei_cb
.cb_list
);
938 list_add_tail(&write_cb
->cb_list
,
939 &dev
->write_list
.mei_cb
.cb_list
);
944 write_cb
->information
= 0;
945 cl
->writing_state
= MEI_WRITING
;
946 list_add_tail(&write_cb
->cb_list
,
947 &dev
->write_list
.mei_cb
.cb_list
);
949 mutex_unlock(&dev
->device_lock
);
953 mutex_unlock(&dev
->device_lock
);
954 mei_free_cb_private(write_cb
);
960 * mei_ioctl - the IOCTL function
962 * @file: pointer to file structure
963 * @cmd: ioctl command
964 * @data: pointer to mei message structure
966 * returns 0 on success , <0 on error
968 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
970 struct mei_device
*dev
;
971 struct mei_cl
*cl
= file
->private_data
;
972 struct mei_connect_client_data
*connect_data
= NULL
;
975 if (cmd
!= IOCTL_MEI_CONNECT_CLIENT
)
978 if (WARN_ON(!cl
|| !cl
->dev
))
983 dev_dbg(&dev
->pdev
->dev
, "IOCTL cmd = 0x%x", cmd
);
985 mutex_lock(&dev
->device_lock
);
986 if (dev
->mei_state
!= MEI_ENABLED
) {
991 dev_dbg(&dev
->pdev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
993 connect_data
= kzalloc(sizeof(struct mei_connect_client_data
),
999 dev_dbg(&dev
->pdev
->dev
, "copy connect data from user\n");
1000 if (copy_from_user(connect_data
, (char __user
*)data
,
1001 sizeof(struct mei_connect_client_data
))) {
1002 dev_dbg(&dev
->pdev
->dev
, "failed to copy data from userland\n");
1006 rets
= mei_ioctl_connect_client(file
, connect_data
);
1008 /* if all is ok, copying the data back to user. */
1012 dev_dbg(&dev
->pdev
->dev
, "copy connect data to user\n");
1013 if (copy_to_user((char __user
*)data
, connect_data
,
1014 sizeof(struct mei_connect_client_data
))) {
1015 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
1021 kfree(connect_data
);
1022 mutex_unlock(&dev
->device_lock
);
1027 * mei_compat_ioctl - the compat IOCTL function
1029 * @file: pointer to file structure
1030 * @cmd: ioctl command
1031 * @data: pointer to mei message structure
1033 * returns 0 on success , <0 on error
1035 #ifdef CONFIG_COMPAT
1036 static long mei_compat_ioctl(struct file
*file
,
1037 unsigned int cmd
, unsigned long data
)
1039 return mei_ioctl(file
, cmd
, (unsigned long)compat_ptr(data
));
1045 * mei_poll - the poll function
1047 * @file: pointer to file structure
1048 * @wait: pointer to poll_table structure
1052 static unsigned int mei_poll(struct file
*file
, poll_table
*wait
)
1054 struct mei_cl
*cl
= file
->private_data
;
1055 struct mei_device
*dev
;
1056 unsigned int mask
= 0;
1058 if (WARN_ON(!cl
|| !cl
->dev
))
1063 mutex_lock(&dev
->device_lock
);
1065 if (dev
->mei_state
!= MEI_ENABLED
)
1069 if (cl
== &dev
->iamthif_cl
) {
1070 mutex_unlock(&dev
->device_lock
);
1071 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
1072 mutex_lock(&dev
->device_lock
);
1073 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
1074 dev
->iamthif_file_object
== file
) {
1075 mask
|= (POLLIN
| POLLRDNORM
);
1076 dev_dbg(&dev
->pdev
->dev
, "run next amthi cb\n");
1077 mei_run_next_iamthif_cmd(dev
);
1082 mutex_unlock(&dev
->device_lock
);
1083 poll_wait(file
, &cl
->tx_wait
, wait
);
1084 mutex_lock(&dev
->device_lock
);
1085 if (MEI_WRITE_COMPLETE
== cl
->writing_state
)
1086 mask
|= (POLLIN
| POLLRDNORM
);
1089 mutex_unlock(&dev
->device_lock
);
1094 static int mei_pci_suspend(struct device
*device
)
1096 struct pci_dev
*pdev
= to_pci_dev(device
);
1097 struct mei_device
*dev
= pci_get_drvdata(pdev
);
1102 mutex_lock(&dev
->device_lock
);
1103 /* Stop watchdog if exists */
1104 err
= mei_wd_stop(dev
, true);
1105 /* Set new mei state */
1106 if (dev
->mei_state
== MEI_ENABLED
||
1107 dev
->mei_state
== MEI_RECOVERING_FROM_RESET
) {
1108 dev
->mei_state
= MEI_POWER_DOWN
;
1111 mutex_unlock(&dev
->device_lock
);
1113 free_irq(pdev
->irq
, dev
);
1114 pci_disable_msi(pdev
);
1119 static int mei_pci_resume(struct device
*device
)
1121 struct pci_dev
*pdev
= to_pci_dev(device
);
1122 struct mei_device
*dev
;
1125 dev
= pci_get_drvdata(pdev
);
1129 pci_enable_msi(pdev
);
1131 /* request and enable interrupt */
1132 if (pci_dev_msi_enabled(pdev
))
1133 err
= request_threaded_irq(pdev
->irq
,
1135 mei_interrupt_thread_handler
,
1136 0, mei_driver_name
, dev
);
1138 err
= request_threaded_irq(pdev
->irq
,
1139 mei_interrupt_quick_handler
,
1140 mei_interrupt_thread_handler
,
1141 IRQF_SHARED
, mei_driver_name
, dev
);
1144 printk(KERN_ERR
"mei: Request_irq failure. irq = %d\n",
1149 mutex_lock(&dev
->device_lock
);
1150 dev
->mei_state
= MEI_POWER_UP
;
1152 mutex_unlock(&dev
->device_lock
);
1154 /* Start timer if stopped in suspend */
1155 schedule_delayed_work(&dev
->timer_work
, HZ
);
1159 static SIMPLE_DEV_PM_OPS(mei_pm_ops
, mei_pci_suspend
, mei_pci_resume
);
1160 #define MEI_PM_OPS (&mei_pm_ops)
1162 #define MEI_PM_OPS NULL
1163 #endif /* CONFIG_PM */
1165 * PCI driver structure
1167 static struct pci_driver mei_driver
= {
1168 .name
= mei_driver_name
,
1169 .id_table
= mei_pci_tbl
,
1171 .remove
= __devexit_p(mei_remove
),
1172 .shutdown
= __devexit_p(mei_remove
),
1173 .driver
.pm
= MEI_PM_OPS
,
1177 * file operations structure will be used for mei char device.
1179 static const struct file_operations mei_fops
= {
1180 .owner
= THIS_MODULE
,
1182 .unlocked_ioctl
= mei_ioctl
,
1183 #ifdef CONFIG_COMPAT
1184 .compat_ioctl
= mei_compat_ioctl
,
1187 .release
= mei_release
,
1193 * mei_registration_cdev - sets up the cdev structure for mei device.
1195 * @dev: char device struct
1196 * @hminor: minor number for registration char device
1197 * @fops: file operations structure
1199 * returns 0 on success, <0 on failure.
1201 static int mei_registration_cdev(struct cdev
*dev
, int hminor
,
1202 const struct file_operations
*fops
)
1204 int ret
, devno
= MKDEV(mei_major
, hminor
);
1206 cdev_init(dev
, fops
);
1207 dev
->owner
= THIS_MODULE
;
1208 ret
= cdev_add(dev
, devno
, 1);
1209 /* Fail gracefully if need be */
1211 printk(KERN_ERR
"mei: Error %d registering mei device %d\n",
1217 * mei_register_cdev - registers mei char device
1219 * returns 0 on success, <0 on failure.
1221 static int mei_register_cdev(void)
1226 /* registration of char devices */
1227 ret
= alloc_chrdev_region(&dev
, MEI_MINORS_BASE
, MEI_MINORS_COUNT
,
1230 printk(KERN_ERR
"mei: Error allocating char device region.\n");
1234 mei_major
= MAJOR(dev
);
1236 ret
= mei_registration_cdev(&mei_cdev
, MEI_MINOR_NUMBER
,
1239 unregister_chrdev_region(MKDEV(mei_major
, MEI_MINORS_BASE
),
1246 * mei_unregister_cdev - unregisters mei char device
1248 static void mei_unregister_cdev(void)
1250 cdev_del(&mei_cdev
);
1251 unregister_chrdev_region(MKDEV(mei_major
, MEI_MINORS_BASE
),
1256 * mei_sysfs_device_create - adds device entry to sysfs
1258 * returns 0 on success, <0 on failure.
1260 static int mei_sysfs_device_create(void)
1262 struct class *class;
1266 class = class_create(THIS_MODULE
, MEI_DRIVER_NAME
);
1267 if (IS_ERR(class)) {
1268 err
= PTR_ERR(class);
1269 printk(KERN_ERR
"mei: Error creating mei class.\n");
1273 tmphdev
= device_create(class, NULL
, mei_cdev
.dev
, NULL
,
1275 if (IS_ERR(tmphdev
)) {
1276 err
= PTR_ERR(tmphdev
);
1284 class_destroy(class);
1290 * mei_sysfs_device_remove - unregisters the device entry on sysfs
1292 static void mei_sysfs_device_remove(void)
1294 if (IS_ERR_OR_NULL(mei_class
))
1297 device_destroy(mei_class
, mei_cdev
.dev
);
1298 class_destroy(mei_class
);
1302 * mei_init_module - Driver Registration Routine
1304 * mei_init_module is the first routine called when the driver is
1305 * loaded. All it does is to register with the PCI subsystem.
1307 * returns 0 on success, <0 on failure.
1309 static int __init
mei_init_module(void)
1313 pr_debug("mei: %s - version %s\n",
1314 mei_driver_string
, mei_driver_version
);
1315 /* init pci module */
1316 ret
= pci_register_driver(&mei_driver
);
1318 printk(KERN_ERR
"mei: Error registering driver.\n");
1322 ret
= mei_register_cdev();
1324 goto unregister_pci
;
1326 ret
= mei_sysfs_device_create();
1328 goto unregister_cdev
;
1333 mei_unregister_cdev();
1335 pci_unregister_driver(&mei_driver
);
1340 module_init(mei_init_module
);
1343 * mei_exit_module - Driver Exit Cleanup Routine
1345 * mei_exit_module is called just before the driver is removed
1348 static void __exit
mei_exit_module(void)
1350 mei_sysfs_device_remove();
1351 mei_unregister_cdev();
1352 pci_unregister_driver(&mei_driver
);
1354 pr_debug("mei: Driver unloaded successfully.\n");
1357 module_exit(mei_exit_module
);
1360 MODULE_AUTHOR("Intel Corporation");
1361 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1362 MODULE_LICENSE("GPL v2");
1363 MODULE_VERSION(MEI_DRIVER_VERSION
);