3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/kernel.h>
22 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/fcntl.h>
27 #include <linux/aio.h>
28 #include <linux/pci.h>
29 #include <linux/poll.h>
30 #include <linux/init.h>
31 #include <linux/ioctl.h>
32 #include <linux/cdev.h>
33 #include <linux/sched.h>
34 #include <linux/uuid.h>
35 #include <linux/compat.h>
36 #include <linux/jiffies.h>
37 #include <linux/interrupt.h>
38 #include <linux/miscdevice.h>
41 #include <linux/mei.h>
42 #include "interface.h"
44 static const char mei_driver_name
[] = "mei";
46 /* The device pointer */
47 /* Currently this driver works as long as there is only a single AMT device. */
48 struct pci_dev
*mei_device
;
50 /* mei_pci_tbl - PCI Device ID Table */
51 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl
) = {
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82946GZ
)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82G35
)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82Q965
)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82G965
)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82GM965
)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82GME965
)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82Q35
)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82G33
)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82Q33
)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82X38
)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_3200
)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_6
)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_7
)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_8
)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_9
)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_10
)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_1
)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_2
)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_3
)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_4
)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_1
)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_2
)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_3
)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_4
)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_IBXPK_1
)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_IBXPK_2
)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_CPT_1
)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PBG_1
)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PPT_1
)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PPT_2
)},
82 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PPT_3
)},
84 /* required last entry */
88 MODULE_DEVICE_TABLE(pci
, mei_pci_tbl
);
90 static DEFINE_MUTEX(mei_mutex
);
94 * mei_clear_list - removes all callbacks associated with file
97 * @dev: device structure.
98 * @file: file structure
99 * @mei_cb_list: callbacks list
101 * mei_clear_list is called to clear resources associated with file
102 * when application calls close function or Ctrl-C was pressed
104 * returns true if callback removed from the list, false otherwise
106 static bool mei_clear_list(struct mei_device
*dev
,
107 struct file
*file
, struct list_head
*mei_cb_list
)
109 struct mei_cl_cb
*cb_pos
= NULL
;
110 struct mei_cl_cb
*cb_next
= NULL
;
111 struct file
*file_temp
;
112 bool removed
= false;
114 /* list all list member */
115 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, cb_list
) {
116 file_temp
= (struct file
*)cb_pos
->file_object
;
117 /* check if list member associated with a file */
118 if (file_temp
== file
) {
119 /* remove member from the list */
120 list_del(&cb_pos
->cb_list
);
121 /* check if cb equal to current iamthif cb */
122 if (dev
->iamthif_current_cb
== cb_pos
) {
123 dev
->iamthif_current_cb
= NULL
;
124 /* send flow control to iamthif client */
125 mei_send_flow_control(dev
, &dev
->iamthif_cl
);
127 /* free all allocated buffers */
128 mei_free_cb_private(cb_pos
);
137 * mei_clear_lists - removes all callbacks associated with file
139 * @dev: device structure
140 * @file: file structure
142 * mei_clear_lists is called to clear resources associated with file
143 * when application calls close function or Ctrl-C was pressed
145 * returns true if callback removed from the list, false otherwise
147 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
149 bool removed
= false;
151 /* remove callbacks associated with a file */
152 mei_clear_list(dev
, file
, &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
153 if (mei_clear_list(dev
, file
,
154 &dev
->amthi_read_complete_list
.mei_cb
.cb_list
))
157 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.mei_cb
.cb_list
);
159 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.mei_cb
.cb_list
))
162 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.mei_cb
.cb_list
))
165 if (mei_clear_list(dev
, file
, &dev
->write_list
.mei_cb
.cb_list
))
168 /* check if iamthif_current_cb not NULL */
169 if (dev
->iamthif_current_cb
&& !removed
) {
170 /* check file and iamthif current cb association */
171 if (dev
->iamthif_current_cb
->file_object
== file
) {
173 mei_free_cb_private(dev
->iamthif_current_cb
);
174 dev
->iamthif_current_cb
= NULL
;
181 * find_read_list_entry - find read list entry
183 * @dev: device structure
184 * @file: pointer to file structure
186 * returns cb on success, NULL on error
188 static struct mei_cl_cb
*find_read_list_entry(
189 struct mei_device
*dev
,
192 struct mei_cl_cb
*pos
= NULL
;
193 struct mei_cl_cb
*next
= NULL
;
195 dev_dbg(&dev
->pdev
->dev
, "remove read_list CB\n");
196 list_for_each_entry_safe(pos
, next
,
197 &dev
->read_list
.mei_cb
.cb_list
, cb_list
) {
198 struct mei_cl
*cl_temp
;
199 cl_temp
= (struct mei_cl
*)pos
->file_private
;
201 if (mei_cl_cmp_id(cl
, cl_temp
))
208 * mei_open - the open function
210 * @inode: pointer to inode structure
211 * @file: pointer to file structure
213 * returns 0 on success, <0 on error
215 static int mei_open(struct inode
*inode
, struct file
*file
)
218 struct mei_device
*dev
;
226 dev
= pci_get_drvdata(mei_device
);
230 mutex_lock(&dev
->device_lock
);
232 cl
= mei_cl_allocate(dev
);
237 if (dev
->mei_state
!= MEI_ENABLED
) {
238 dev_dbg(&dev
->pdev
->dev
, "mei_state != MEI_ENABLED mei_state= %d\n",
243 if (dev
->open_handle_count
>= MEI_MAX_OPEN_HANDLE_COUNT
)
246 cl_id
= find_first_zero_bit(dev
->host_clients_map
, MEI_CLIENTS_MAX
);
247 if (cl_id
>= MEI_CLIENTS_MAX
)
250 cl
->host_client_id
= cl_id
;
252 dev_dbg(&dev
->pdev
->dev
, "client_id = %d\n", cl
->host_client_id
);
254 dev
->open_handle_count
++;
256 list_add_tail(&cl
->link
, &dev
->file_list
);
258 set_bit(cl
->host_client_id
, dev
->host_clients_map
);
259 cl
->state
= MEI_FILE_INITIALIZING
;
262 file
->private_data
= cl
;
263 mutex_unlock(&dev
->device_lock
);
265 return nonseekable_open(inode
, file
);
268 mutex_unlock(&dev
->device_lock
);
275 * mei_release - the release function
277 * @inode: pointer to inode structure
278 * @file: pointer to file structure
280 * returns 0 on success, <0 on error
282 static int mei_release(struct inode
*inode
, struct file
*file
)
284 struct mei_cl
*cl
= file
->private_data
;
285 struct mei_cl_cb
*cb
;
286 struct mei_device
*dev
;
289 if (WARN_ON(!cl
|| !cl
->dev
))
294 mutex_lock(&dev
->device_lock
);
295 if (cl
!= &dev
->iamthif_cl
) {
296 if (cl
->state
== MEI_FILE_CONNECTED
) {
297 cl
->state
= MEI_FILE_DISCONNECTING
;
298 dev_dbg(&dev
->pdev
->dev
,
299 "disconnecting client host client = %d, "
303 rets
= mei_disconnect_host_client(dev
, cl
);
305 mei_cl_flush_queues(cl
);
306 dev_dbg(&dev
->pdev
->dev
, "remove client host client = %d, ME client = %d\n",
310 if (dev
->open_handle_count
> 0) {
311 clear_bit(cl
->host_client_id
, dev
->host_clients_map
);
312 dev
->open_handle_count
--;
314 mei_remove_client_from_file_list(dev
, cl
->host_client_id
);
319 cb
= find_read_list_entry(dev
, cl
);
320 /* Remove entry from read list */
322 list_del(&cb
->cb_list
);
328 file
->private_data
= NULL
;
331 mei_free_cb_private(cb
);
337 if (dev
->open_handle_count
> 0)
338 dev
->open_handle_count
--;
340 if (dev
->iamthif_file_object
== file
&&
341 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
343 dev_dbg(&dev
->pdev
->dev
, "amthi canceled iamthif state %d\n",
345 dev
->iamthif_canceled
= true;
346 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
347 dev_dbg(&dev
->pdev
->dev
, "run next amthi iamthif cb\n");
348 mei_run_next_iamthif_cmd(dev
);
352 if (mei_clear_lists(dev
, file
))
353 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
356 mutex_unlock(&dev
->device_lock
);
362 * mei_read - the read function.
364 * @file: pointer to file structure
365 * @ubuf: pointer to user buffer
366 * @length: buffer length
367 * @offset: data offset in buffer
369 * returns >=0 data length on success , <0 on error
371 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
372 size_t length
, loff_t
*offset
)
374 struct mei_cl
*cl
= file
->private_data
;
375 struct mei_cl_cb
*cb_pos
= NULL
;
376 struct mei_cl_cb
*cb
= NULL
;
377 struct mei_device
*dev
;
383 if (WARN_ON(!cl
|| !cl
->dev
))
388 mutex_lock(&dev
->device_lock
);
389 if (dev
->mei_state
!= MEI_ENABLED
) {
394 if ((cl
->sm_state
& MEI_WD_STATE_INDEPENDENCE_MSG_SENT
) == 0) {
395 /* Do not allow to read watchdog client */
396 i
= mei_find_me_client_index(dev
, mei_wd_guid
);
398 struct mei_me_client
*me_client
= &dev
->me_clients
[i
];
400 if (cl
->me_client_id
== me_client
->client_id
) {
406 cl
->sm_state
&= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
409 if (cl
== &dev
->iamthif_cl
) {
410 rets
= amthi_read(dev
, file
, ubuf
, length
, offset
);
414 if (cl
->read_cb
&& cl
->read_cb
->information
> *offset
) {
417 } else if (cl
->read_cb
&& cl
->read_cb
->information
> 0 &&
418 cl
->read_cb
->information
<= *offset
) {
422 } else if ((!cl
->read_cb
|| !cl
->read_cb
->information
) &&
424 /*Offset needs to be cleaned for contiguous reads*/
430 err
= mei_start_read(dev
, cl
);
431 if (err
&& err
!= -EBUSY
) {
432 dev_dbg(&dev
->pdev
->dev
,
433 "mei start read failure with status = %d\n", err
);
438 if (MEI_READ_COMPLETE
!= cl
->reading_state
&&
439 !waitqueue_active(&cl
->rx_wait
)) {
440 if (file
->f_flags
& O_NONBLOCK
) {
445 mutex_unlock(&dev
->device_lock
);
447 if (wait_event_interruptible(cl
->rx_wait
,
448 (MEI_READ_COMPLETE
== cl
->reading_state
||
449 MEI_FILE_INITIALIZING
== cl
->state
||
450 MEI_FILE_DISCONNECTED
== cl
->state
||
451 MEI_FILE_DISCONNECTING
== cl
->state
))) {
452 if (signal_pending(current
))
457 mutex_lock(&dev
->device_lock
);
458 if (MEI_FILE_INITIALIZING
== cl
->state
||
459 MEI_FILE_DISCONNECTED
== cl
->state
||
460 MEI_FILE_DISCONNECTING
== cl
->state
) {
472 if (cl
->reading_state
!= MEI_READ_COMPLETE
) {
476 /* now copy the data to user space */
478 dev_dbg(&dev
->pdev
->dev
, "cb->response_buffer size - %d\n",
479 cb
->response_buffer
.size
);
480 dev_dbg(&dev
->pdev
->dev
, "cb->information - %lu\n",
482 if (length
== 0 || ubuf
== NULL
|| *offset
> cb
->information
) {
487 /* length is being truncated to PAGE_SIZE, however, */
488 /* information size may be longer */
489 length
= min_t(size_t, length
, (cb
->information
- *offset
));
491 if (copy_to_user(ubuf
, cb
->response_buffer
.data
+ *offset
, length
)) {
498 if ((unsigned long)*offset
< cb
->information
)
502 cb_pos
= find_read_list_entry(dev
, cl
);
503 /* Remove entry from read list */
505 list_del(&cb_pos
->cb_list
);
506 mei_free_cb_private(cb
);
507 cl
->reading_state
= MEI_IDLE
;
509 cl
->read_pending
= 0;
511 dev_dbg(&dev
->pdev
->dev
, "end mei read rets= %d\n", rets
);
512 mutex_unlock(&dev
->device_lock
);
517 * mei_write - the write function.
519 * @file: pointer to file structure
520 * @ubuf: pointer to user buffer
521 * @length: buffer length
522 * @offset: data offset in buffer
524 * returns >=0 data length on success , <0 on error
526 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
527 size_t length
, loff_t
*offset
)
529 struct mei_cl
*cl
= file
->private_data
;
530 struct mei_cl_cb
*write_cb
= NULL
;
531 struct mei_msg_hdr mei_hdr
;
532 struct mei_device
*dev
;
533 unsigned long timeout
= 0;
537 if (WARN_ON(!cl
|| !cl
->dev
))
542 mutex_lock(&dev
->device_lock
);
544 if (dev
->mei_state
!= MEI_ENABLED
) {
545 mutex_unlock(&dev
->device_lock
);
549 if (cl
== &dev
->iamthif_cl
) {
550 write_cb
= find_amthi_read_list_entry(dev
, file
);
553 timeout
= write_cb
->read_time
+
554 msecs_to_jiffies(IAMTHIF_READ_TIMER
);
556 if (time_after(jiffies
, timeout
) ||
557 cl
->reading_state
== MEI_READ_COMPLETE
) {
559 list_del(&write_cb
->cb_list
);
560 mei_free_cb_private(write_cb
);
566 /* free entry used in read */
567 if (cl
->reading_state
== MEI_READ_COMPLETE
) {
569 write_cb
= find_read_list_entry(dev
, cl
);
571 list_del(&write_cb
->cb_list
);
572 mei_free_cb_private(write_cb
);
574 cl
->reading_state
= MEI_IDLE
;
576 cl
->read_pending
= 0;
578 } else if (cl
->reading_state
== MEI_IDLE
&& !cl
->read_pending
)
582 write_cb
= kzalloc(sizeof(struct mei_cl_cb
), GFP_KERNEL
);
584 mutex_unlock(&dev
->device_lock
);
588 write_cb
->file_object
= file
;
589 write_cb
->file_private
= cl
;
590 write_cb
->request_buffer
.data
= kmalloc(length
, GFP_KERNEL
);
592 if (!write_cb
->request_buffer
.data
)
595 dev_dbg(&dev
->pdev
->dev
, "length =%d\n", (int) length
);
598 if (copy_from_user(write_cb
->request_buffer
.data
, ubuf
, length
))
603 ((memcmp(mei_wd_state_independence_msg
[0],
604 write_cb
->request_buffer
.data
, 4) == 0) ||
605 (memcmp(mei_wd_state_independence_msg
[1],
606 write_cb
->request_buffer
.data
, 4) == 0) ||
607 (memcmp(mei_wd_state_independence_msg
[2],
608 write_cb
->request_buffer
.data
, 4) == 0)))
609 cl
->sm_state
|= MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
611 INIT_LIST_HEAD(&write_cb
->cb_list
);
612 if (cl
== &dev
->iamthif_cl
) {
613 write_cb
->response_buffer
.data
=
614 kmalloc(dev
->iamthif_mtu
, GFP_KERNEL
);
615 if (!write_cb
->response_buffer
.data
) {
619 if (dev
->mei_state
!= MEI_ENABLED
) {
623 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
624 if (dev
->me_clients
[i
].client_id
==
625 dev
->iamthif_cl
.me_client_id
)
629 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
633 if (i
== dev
->me_clients_num
||
634 (dev
->me_clients
[i
].client_id
!=
635 dev
->iamthif_cl
.me_client_id
)) {
638 } else if (length
> dev
->me_clients
[i
].props
.max_msg_length
||
644 write_cb
->response_buffer
.size
= dev
->iamthif_mtu
;
645 write_cb
->major_file_operations
= MEI_IOCTL
;
646 write_cb
->information
= 0;
647 write_cb
->request_buffer
.size
= length
;
648 if (dev
->iamthif_cl
.state
!= MEI_FILE_CONNECTED
) {
653 if (!list_empty(&dev
->amthi_cmd_list
.mei_cb
.cb_list
) ||
654 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
655 dev_dbg(&dev
->pdev
->dev
, "amthi_state = %d\n",
656 (int) dev
->iamthif_state
);
657 dev_dbg(&dev
->pdev
->dev
, "add amthi cb to amthi cmd waiting list\n");
658 list_add_tail(&write_cb
->cb_list
,
659 &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
662 dev_dbg(&dev
->pdev
->dev
, "call amthi write\n");
663 rets
= amthi_write(dev
, write_cb
);
666 dev_dbg(&dev
->pdev
->dev
, "amthi write failed with status = %d\n",
672 mutex_unlock(&dev
->device_lock
);
676 write_cb
->major_file_operations
= MEI_WRITE
;
677 /* make sure information is zero before we start */
679 write_cb
->information
= 0;
680 write_cb
->request_buffer
.size
= length
;
682 dev_dbg(&dev
->pdev
->dev
, "host client = %d, ME client = %d\n",
683 cl
->host_client_id
, cl
->me_client_id
);
684 if (cl
->state
!= MEI_FILE_CONNECTED
) {
686 dev_dbg(&dev
->pdev
->dev
, "host client = %d, is not connected to ME client = %d",
691 for (i
= 0; i
< dev
->me_clients_num
; i
++) {
692 if (dev
->me_clients
[i
].client_id
==
696 if (WARN_ON(dev
->me_clients
[i
].client_id
!= cl
->me_client_id
)) {
700 if (i
== dev
->me_clients_num
) {
704 if (length
> dev
->me_clients
[i
].props
.max_msg_length
|| length
<= 0) {
708 write_cb
->file_private
= cl
;
710 rets
= mei_flow_ctrl_creds(dev
, cl
);
714 if (rets
&& dev
->mei_host_buffer_is_empty
) {
716 dev
->mei_host_buffer_is_empty
= false;
717 if (length
> ((((dev
->host_hw_state
& H_CBD
) >> 24) *
718 sizeof(u32
)) - sizeof(struct mei_msg_hdr
))) {
721 (((dev
->host_hw_state
& H_CBD
) >> 24) *
723 sizeof(struct mei_msg_hdr
);
724 mei_hdr
.msg_complete
= 0;
726 mei_hdr
.length
= length
;
727 mei_hdr
.msg_complete
= 1;
729 mei_hdr
.host_addr
= cl
->host_client_id
;
730 mei_hdr
.me_addr
= cl
->me_client_id
;
731 mei_hdr
.reserved
= 0;
732 dev_dbg(&dev
->pdev
->dev
, "call mei_write_message header=%08x.\n",
733 *((u32
*) &mei_hdr
));
734 if (mei_write_message(dev
, &mei_hdr
,
735 (unsigned char *) (write_cb
->request_buffer
.data
),
740 cl
->writing_state
= MEI_WRITING
;
741 write_cb
->information
= mei_hdr
.length
;
742 if (mei_hdr
.msg_complete
) {
743 if (mei_flow_ctrl_reduce(dev
, cl
)) {
747 list_add_tail(&write_cb
->cb_list
,
748 &dev
->write_waiting_list
.mei_cb
.cb_list
);
750 list_add_tail(&write_cb
->cb_list
,
751 &dev
->write_list
.mei_cb
.cb_list
);
756 write_cb
->information
= 0;
757 cl
->writing_state
= MEI_WRITING
;
758 list_add_tail(&write_cb
->cb_list
,
759 &dev
->write_list
.mei_cb
.cb_list
);
761 mutex_unlock(&dev
->device_lock
);
765 mutex_unlock(&dev
->device_lock
);
766 mei_free_cb_private(write_cb
);
772 * mei_ioctl - the IOCTL function
774 * @file: pointer to file structure
775 * @cmd: ioctl command
776 * @data: pointer to mei message structure
778 * returns 0 on success , <0 on error
780 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
782 struct mei_device
*dev
;
783 struct mei_cl
*cl
= file
->private_data
;
784 struct mei_connect_client_data
*connect_data
= NULL
;
787 if (cmd
!= IOCTL_MEI_CONNECT_CLIENT
)
790 if (WARN_ON(!cl
|| !cl
->dev
))
795 dev_dbg(&dev
->pdev
->dev
, "IOCTL cmd = 0x%x", cmd
);
797 mutex_lock(&dev
->device_lock
);
798 if (dev
->mei_state
!= MEI_ENABLED
) {
803 dev_dbg(&dev
->pdev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
805 connect_data
= kzalloc(sizeof(struct mei_connect_client_data
),
811 dev_dbg(&dev
->pdev
->dev
, "copy connect data from user\n");
812 if (copy_from_user(connect_data
, (char __user
*)data
,
813 sizeof(struct mei_connect_client_data
))) {
814 dev_dbg(&dev
->pdev
->dev
, "failed to copy data from userland\n");
818 rets
= mei_ioctl_connect_client(file
, connect_data
);
820 /* if all is ok, copying the data back to user. */
824 dev_dbg(&dev
->pdev
->dev
, "copy connect data to user\n");
825 if (copy_to_user((char __user
*)data
, connect_data
,
826 sizeof(struct mei_connect_client_data
))) {
827 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
834 mutex_unlock(&dev
->device_lock
);
839 * mei_compat_ioctl - the compat IOCTL function
841 * @file: pointer to file structure
842 * @cmd: ioctl command
843 * @data: pointer to mei message structure
845 * returns 0 on success , <0 on error
848 static long mei_compat_ioctl(struct file
*file
,
849 unsigned int cmd
, unsigned long data
)
851 return mei_ioctl(file
, cmd
, (unsigned long)compat_ptr(data
));
857 * mei_poll - the poll function
859 * @file: pointer to file structure
860 * @wait: pointer to poll_table structure
864 static unsigned int mei_poll(struct file
*file
, poll_table
*wait
)
866 struct mei_cl
*cl
= file
->private_data
;
867 struct mei_device
*dev
;
868 unsigned int mask
= 0;
870 if (WARN_ON(!cl
|| !cl
->dev
))
875 mutex_lock(&dev
->device_lock
);
877 if (dev
->mei_state
!= MEI_ENABLED
)
881 if (cl
== &dev
->iamthif_cl
) {
882 mutex_unlock(&dev
->device_lock
);
883 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
884 mutex_lock(&dev
->device_lock
);
885 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
886 dev
->iamthif_file_object
== file
) {
887 mask
|= (POLLIN
| POLLRDNORM
);
888 dev_dbg(&dev
->pdev
->dev
, "run next amthi cb\n");
889 mei_run_next_iamthif_cmd(dev
);
894 mutex_unlock(&dev
->device_lock
);
895 poll_wait(file
, &cl
->tx_wait
, wait
);
896 mutex_lock(&dev
->device_lock
);
897 if (MEI_WRITE_COMPLETE
== cl
->writing_state
)
898 mask
|= (POLLIN
| POLLRDNORM
);
901 mutex_unlock(&dev
->device_lock
);
906 * file operations structure will be used for mei char device.
908 static const struct file_operations mei_fops
= {
909 .owner
= THIS_MODULE
,
911 .unlocked_ioctl
= mei_ioctl
,
913 .compat_ioctl
= mei_compat_ioctl
,
916 .release
= mei_release
,
926 static struct miscdevice mei_misc_device
= {
929 .minor
= MISC_DYNAMIC_MINOR
,
933 * mei_probe - Device Initialization Routine
935 * @pdev: PCI device structure
936 * @ent: entry in kcs_pci_tbl
938 * returns 0 on success, <0 on failure.
940 static int __devinit
mei_probe(struct pci_dev
*pdev
,
941 const struct pci_device_id
*ent
)
943 struct mei_device
*dev
;
946 mutex_lock(&mei_mutex
);
952 err
= pci_enable_device(pdev
);
954 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
957 /* set PCI host mastering */
958 pci_set_master(pdev
);
959 /* pci request regions for mei driver */
960 err
= pci_request_regions(pdev
, mei_driver_name
);
962 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
965 /* allocates and initializes the mei dev structure */
966 dev
= mei_device_init(pdev
);
969 goto release_regions
;
971 /* mapping IO device memory */
972 dev
->mem_addr
= pci_iomap(pdev
, 0, 0);
973 if (!dev
->mem_addr
) {
974 dev_err(&pdev
->dev
, "mapping I/O device memory failure.\n");
978 pci_enable_msi(pdev
);
980 /* request and enable interrupt */
981 if (pci_dev_msi_enabled(pdev
))
982 err
= request_threaded_irq(pdev
->irq
,
984 mei_interrupt_thread_handler
,
985 IRQF_ONESHOT
, mei_driver_name
, dev
);
987 err
= request_threaded_irq(pdev
->irq
,
988 mei_interrupt_quick_handler
,
989 mei_interrupt_thread_handler
,
990 IRQF_SHARED
, mei_driver_name
, dev
);
993 dev_err(&pdev
->dev
, "request_threaded_irq failure. irq = %d\n",
997 INIT_DELAYED_WORK(&dev
->timer_work
, mei_timer
);
998 if (mei_hw_init(dev
)) {
999 dev_err(&pdev
->dev
, "init hw failure.\n");
1004 err
= misc_register(&mei_misc_device
);
1009 pci_set_drvdata(pdev
, dev
);
1012 schedule_delayed_work(&dev
->timer_work
, HZ
);
1014 mutex_unlock(&mei_mutex
);
1016 pr_debug("initialization successful.\n");
1021 /* disable interrupts */
1022 dev
->host_hw_state
= mei_hcsr_read(dev
);
1023 mei_disable_interrupts(dev
);
1024 flush_scheduled_work();
1025 free_irq(pdev
->irq
, dev
);
1027 pci_disable_msi(pdev
);
1028 pci_iounmap(pdev
, dev
->mem_addr
);
1032 pci_release_regions(pdev
);
1034 pci_disable_device(pdev
);
1036 mutex_unlock(&mei_mutex
);
1037 dev_err(&pdev
->dev
, "initialization failed.\n");
1042 * mei_remove - Device Removal Routine
1044 * @pdev: PCI device structure
1046 * mei_remove is called by the PCI subsystem to alert the driver
1047 * that it should release a PCI device.
1049 static void __devexit
mei_remove(struct pci_dev
*pdev
)
1051 struct mei_device
*dev
;
1053 if (mei_device
!= pdev
)
1056 dev
= pci_get_drvdata(pdev
);
1060 mutex_lock(&dev
->device_lock
);
1062 mei_wd_stop(dev
, false);
1066 if (dev
->iamthif_cl
.state
== MEI_FILE_CONNECTED
) {
1067 dev
->iamthif_cl
.state
= MEI_FILE_DISCONNECTING
;
1068 mei_disconnect_host_client(dev
, &dev
->iamthif_cl
);
1070 if (dev
->wd_cl
.state
== MEI_FILE_CONNECTED
) {
1071 dev
->wd_cl
.state
= MEI_FILE_DISCONNECTING
;
1072 mei_disconnect_host_client(dev
, &dev
->wd_cl
);
1075 /* Unregistering watchdog device */
1076 mei_watchdog_unregister(dev
);
1078 /* remove entry if already in list */
1079 dev_dbg(&pdev
->dev
, "list del iamthif and wd file list.\n");
1080 mei_remove_client_from_file_list(dev
, dev
->wd_cl
.host_client_id
);
1081 mei_remove_client_from_file_list(dev
, dev
->iamthif_cl
.host_client_id
);
1083 dev
->iamthif_current_cb
= NULL
;
1084 dev
->me_clients_num
= 0;
1086 mutex_unlock(&dev
->device_lock
);
1088 flush_scheduled_work();
1090 /* disable interrupts */
1091 mei_disable_interrupts(dev
);
1093 free_irq(pdev
->irq
, dev
);
1094 pci_disable_msi(pdev
);
1095 pci_set_drvdata(pdev
, NULL
);
1098 pci_iounmap(pdev
, dev
->mem_addr
);
1102 pci_release_regions(pdev
);
1103 pci_disable_device(pdev
);
1105 misc_deregister(&mei_misc_device
);
1108 static int mei_pci_suspend(struct device
*device
)
1110 struct pci_dev
*pdev
= to_pci_dev(device
);
1111 struct mei_device
*dev
= pci_get_drvdata(pdev
);
1116 mutex_lock(&dev
->device_lock
);
1117 /* Stop watchdog if exists */
1118 err
= mei_wd_stop(dev
, true);
1119 /* Set new mei state */
1120 if (dev
->mei_state
== MEI_ENABLED
||
1121 dev
->mei_state
== MEI_RECOVERING_FROM_RESET
) {
1122 dev
->mei_state
= MEI_POWER_DOWN
;
1125 mutex_unlock(&dev
->device_lock
);
1127 free_irq(pdev
->irq
, dev
);
1128 pci_disable_msi(pdev
);
1133 static int mei_pci_resume(struct device
*device
)
1135 struct pci_dev
*pdev
= to_pci_dev(device
);
1136 struct mei_device
*dev
;
1139 dev
= pci_get_drvdata(pdev
);
1143 pci_enable_msi(pdev
);
1145 /* request and enable interrupt */
1146 if (pci_dev_msi_enabled(pdev
))
1147 err
= request_threaded_irq(pdev
->irq
,
1149 mei_interrupt_thread_handler
,
1150 0, mei_driver_name
, dev
);
1152 err
= request_threaded_irq(pdev
->irq
,
1153 mei_interrupt_quick_handler
,
1154 mei_interrupt_thread_handler
,
1155 IRQF_SHARED
, mei_driver_name
, dev
);
1158 dev_err(&pdev
->dev
, "request_threaded_irq failed: irq = %d.\n",
1163 mutex_lock(&dev
->device_lock
);
1164 dev
->mei_state
= MEI_POWER_UP
;
1166 mutex_unlock(&dev
->device_lock
);
1168 /* Start timer if stopped in suspend */
1169 schedule_delayed_work(&dev
->timer_work
, HZ
);
1173 static SIMPLE_DEV_PM_OPS(mei_pm_ops
, mei_pci_suspend
, mei_pci_resume
);
1174 #define MEI_PM_OPS (&mei_pm_ops)
1176 #define MEI_PM_OPS NULL
1177 #endif /* CONFIG_PM */
1179 * PCI driver structure
1181 static struct pci_driver mei_driver
= {
1182 .name
= mei_driver_name
,
1183 .id_table
= mei_pci_tbl
,
1185 .remove
= __devexit_p(mei_remove
),
1186 .shutdown
= __devexit_p(mei_remove
),
1187 .driver
.pm
= MEI_PM_OPS
,
1191 * mei_init_module - Driver Registration Routine
1193 * mei_init_module is the first routine called when the driver is
1194 * loaded. All it does is to register with the PCI subsystem.
1196 * returns 0 on success, <0 on failure.
1198 static int __init
mei_init_module(void)
1202 pr_debug("loading.\n");
1203 /* init pci module */
1204 ret
= pci_register_driver(&mei_driver
);
1206 pr_err("error registering driver.\n");
1211 module_init(mei_init_module
);
1214 * mei_exit_module - Driver Exit Cleanup Routine
1216 * mei_exit_module is called just before the driver is removed
1219 static void __exit
mei_exit_module(void)
1221 pci_unregister_driver(&mei_driver
);
1223 pr_debug("unloaded successfully.\n");
1226 module_exit(mei_exit_module
);
1229 MODULE_AUTHOR("Intel Corporation");
1230 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1231 MODULE_LICENSE("GPL v2");