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 /* AMT device is a singleton on the platform */
45 static struct pci_dev
*mei_pdev
;
47 /* mei_pci_tbl - PCI Device ID Table */
48 static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl
) = {
49 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82946GZ
)},
50 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82G35
)},
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82Q965
)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82G965
)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82GM965
)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_82GME965
)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82Q35
)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82G33
)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82Q33
)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_82X38
)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_3200
)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_6
)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_7
)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_8
)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_9
)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9_10
)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_1
)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_2
)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_3
)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH9M_4
)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_1
)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_2
)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_3
)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_ICH10_4
)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_IBXPK_1
)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_IBXPK_2
)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_CPT_1
)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PBG_1
)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PPT_1
)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PPT_2
)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_PPT_3
)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_LPT
)},
81 {PCI_DEVICE(PCI_VENDOR_ID_INTEL
, MEI_DEV_ID_LPT_LP
)},
83 /* required last entry */
87 MODULE_DEVICE_TABLE(pci
, mei_pci_tbl
);
89 static DEFINE_MUTEX(mei_mutex
);
93 * mei_clear_list - removes all callbacks associated with file
96 * @dev: device structure.
97 * @file: file structure
98 * @mei_cb_list: callbacks list
100 * mei_clear_list is called to clear resources associated with file
101 * when application calls close function or Ctrl-C was pressed
103 * returns true if callback removed from the list, false otherwise
105 static bool mei_clear_list(struct mei_device
*dev
,
106 struct file
*file
, struct list_head
*mei_cb_list
)
108 struct mei_cl_cb
*cb_pos
= NULL
;
109 struct mei_cl_cb
*cb_next
= NULL
;
110 struct file
*file_temp
;
111 bool removed
= false;
113 /* list all list member */
114 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, cb_list
) {
115 file_temp
= (struct file
*)cb_pos
->file_object
;
116 /* check if list member associated with a file */
117 if (file_temp
== file
) {
118 /* remove member from the list */
119 list_del(&cb_pos
->cb_list
);
120 /* check if cb equal to current iamthif cb */
121 if (dev
->iamthif_current_cb
== cb_pos
) {
122 dev
->iamthif_current_cb
= NULL
;
123 /* send flow control to iamthif client */
124 mei_send_flow_control(dev
, &dev
->iamthif_cl
);
126 /* free all allocated buffers */
127 mei_free_cb_private(cb_pos
);
136 * mei_clear_lists - removes all callbacks associated with file
138 * @dev: device structure
139 * @file: file structure
141 * mei_clear_lists is called to clear resources associated with file
142 * when application calls close function or Ctrl-C was pressed
144 * returns true if callback removed from the list, false otherwise
146 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
148 bool removed
= false;
150 /* remove callbacks associated with a file */
151 mei_clear_list(dev
, file
, &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
152 if (mei_clear_list(dev
, file
,
153 &dev
->amthi_read_complete_list
.mei_cb
.cb_list
))
156 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.mei_cb
.cb_list
);
158 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.mei_cb
.cb_list
))
161 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.mei_cb
.cb_list
))
164 if (mei_clear_list(dev
, file
, &dev
->write_list
.mei_cb
.cb_list
))
167 /* check if iamthif_current_cb not NULL */
168 if (dev
->iamthif_current_cb
&& !removed
) {
169 /* check file and iamthif current cb association */
170 if (dev
->iamthif_current_cb
->file_object
== file
) {
172 mei_free_cb_private(dev
->iamthif_current_cb
);
173 dev
->iamthif_current_cb
= NULL
;
180 * find_read_list_entry - find read list entry
182 * @dev: device structure
183 * @file: pointer to file structure
185 * returns cb on success, NULL on error
187 static struct mei_cl_cb
*find_read_list_entry(
188 struct mei_device
*dev
,
191 struct mei_cl_cb
*pos
= NULL
;
192 struct mei_cl_cb
*next
= NULL
;
194 dev_dbg(&dev
->pdev
->dev
, "remove read_list CB\n");
195 list_for_each_entry_safe(pos
, next
,
196 &dev
->read_list
.mei_cb
.cb_list
, cb_list
) {
197 struct mei_cl
*cl_temp
;
198 cl_temp
= (struct mei_cl
*)pos
->file_private
;
200 if (mei_cl_cmp_id(cl
, cl_temp
))
207 * mei_open - the open function
209 * @inode: pointer to inode structure
210 * @file: pointer to file structure
212 * returns 0 on success, <0 on error
214 static int mei_open(struct inode
*inode
, struct file
*file
)
217 struct mei_device
*dev
;
225 dev
= pci_get_drvdata(mei_pdev
);
229 mutex_lock(&dev
->device_lock
);
231 cl
= mei_cl_allocate(dev
);
236 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
237 dev_dbg(&dev
->pdev
->dev
, "dev_state != MEI_ENABLED dev_state = %s\n",
238 mei_dev_state_str(dev
->dev_state
));
242 if (dev
->open_handle_count
>= MEI_MAX_OPEN_HANDLE_COUNT
) {
243 dev_err(&dev
->pdev
->dev
, "open_handle_count exceded %d",
244 MEI_MAX_OPEN_HANDLE_COUNT
);
248 cl_id
= find_first_zero_bit(dev
->host_clients_map
, MEI_CLIENTS_MAX
);
249 if (cl_id
>= MEI_CLIENTS_MAX
) {
250 dev_err(&dev
->pdev
->dev
, "client_id exceded %d",
255 cl
->host_client_id
= cl_id
;
257 dev_dbg(&dev
->pdev
->dev
, "client_id = %d\n", cl
->host_client_id
);
259 dev
->open_handle_count
++;
261 list_add_tail(&cl
->link
, &dev
->file_list
);
263 set_bit(cl
->host_client_id
, dev
->host_clients_map
);
264 cl
->state
= MEI_FILE_INITIALIZING
;
267 file
->private_data
= cl
;
268 mutex_unlock(&dev
->device_lock
);
270 return nonseekable_open(inode
, file
);
273 mutex_unlock(&dev
->device_lock
);
280 * mei_release - the release function
282 * @inode: pointer to inode structure
283 * @file: pointer to file structure
285 * returns 0 on success, <0 on error
287 static int mei_release(struct inode
*inode
, struct file
*file
)
289 struct mei_cl
*cl
= file
->private_data
;
290 struct mei_cl_cb
*cb
;
291 struct mei_device
*dev
;
294 if (WARN_ON(!cl
|| !cl
->dev
))
299 mutex_lock(&dev
->device_lock
);
300 if (cl
!= &dev
->iamthif_cl
) {
301 if (cl
->state
== MEI_FILE_CONNECTED
) {
302 cl
->state
= MEI_FILE_DISCONNECTING
;
303 dev_dbg(&dev
->pdev
->dev
,
304 "disconnecting client host client = %d, "
308 rets
= mei_disconnect_host_client(dev
, cl
);
310 mei_cl_flush_queues(cl
);
311 dev_dbg(&dev
->pdev
->dev
, "remove client host client = %d, ME client = %d\n",
315 if (dev
->open_handle_count
> 0) {
316 clear_bit(cl
->host_client_id
, dev
->host_clients_map
);
317 dev
->open_handle_count
--;
319 mei_remove_client_from_file_list(dev
, cl
->host_client_id
);
324 cb
= find_read_list_entry(dev
, cl
);
325 /* Remove entry from read list */
327 list_del(&cb
->cb_list
);
333 file
->private_data
= NULL
;
336 mei_free_cb_private(cb
);
342 if (dev
->open_handle_count
> 0)
343 dev
->open_handle_count
--;
345 if (dev
->iamthif_file_object
== file
&&
346 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
348 dev_dbg(&dev
->pdev
->dev
, "amthi canceled iamthif state %d\n",
350 dev
->iamthif_canceled
= true;
351 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
352 dev_dbg(&dev
->pdev
->dev
, "run next amthi iamthif cb\n");
353 mei_run_next_iamthif_cmd(dev
);
357 if (mei_clear_lists(dev
, file
))
358 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
361 mutex_unlock(&dev
->device_lock
);
367 * mei_read - the read function.
369 * @file: pointer to file structure
370 * @ubuf: pointer to user buffer
371 * @length: buffer length
372 * @offset: data offset in buffer
374 * returns >=0 data length on success , <0 on error
376 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
377 size_t length
, loff_t
*offset
)
379 struct mei_cl
*cl
= file
->private_data
;
380 struct mei_cl_cb
*cb_pos
= NULL
;
381 struct mei_cl_cb
*cb
= NULL
;
382 struct mei_device
*dev
;
388 if (WARN_ON(!cl
|| !cl
->dev
))
393 mutex_lock(&dev
->device_lock
);
394 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
399 if ((cl
->sm_state
& MEI_WD_STATE_INDEPENDENCE_MSG_SENT
) == 0) {
400 /* Do not allow to read watchdog client */
401 i
= mei_me_cl_by_uuid(dev
, &mei_wd_guid
);
403 struct mei_me_client
*me_client
= &dev
->me_clients
[i
];
404 if (cl
->me_client_id
== me_client
->client_id
) {
410 cl
->sm_state
&= ~MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
413 if (cl
== &dev
->iamthif_cl
) {
414 rets
= amthi_read(dev
, file
, ubuf
, length
, offset
);
418 if (cl
->read_cb
&& cl
->read_cb
->information
> *offset
) {
421 } else if (cl
->read_cb
&& cl
->read_cb
->information
> 0 &&
422 cl
->read_cb
->information
<= *offset
) {
426 } else if ((!cl
->read_cb
|| !cl
->read_cb
->information
) &&
428 /*Offset needs to be cleaned for contiguous reads*/
434 err
= mei_start_read(dev
, cl
);
435 if (err
&& err
!= -EBUSY
) {
436 dev_dbg(&dev
->pdev
->dev
,
437 "mei start read failure with status = %d\n", err
);
442 if (MEI_READ_COMPLETE
!= cl
->reading_state
&&
443 !waitqueue_active(&cl
->rx_wait
)) {
444 if (file
->f_flags
& O_NONBLOCK
) {
449 mutex_unlock(&dev
->device_lock
);
451 if (wait_event_interruptible(cl
->rx_wait
,
452 (MEI_READ_COMPLETE
== cl
->reading_state
||
453 MEI_FILE_INITIALIZING
== cl
->state
||
454 MEI_FILE_DISCONNECTED
== cl
->state
||
455 MEI_FILE_DISCONNECTING
== cl
->state
))) {
456 if (signal_pending(current
))
461 mutex_lock(&dev
->device_lock
);
462 if (MEI_FILE_INITIALIZING
== cl
->state
||
463 MEI_FILE_DISCONNECTED
== cl
->state
||
464 MEI_FILE_DISCONNECTING
== cl
->state
) {
476 if (cl
->reading_state
!= MEI_READ_COMPLETE
) {
480 /* now copy the data to user space */
482 dev_dbg(&dev
->pdev
->dev
, "cb->response_buffer size - %d\n",
483 cb
->response_buffer
.size
);
484 dev_dbg(&dev
->pdev
->dev
, "cb->information - %lu\n",
486 if (length
== 0 || ubuf
== NULL
|| *offset
> cb
->information
) {
491 /* length is being truncated to PAGE_SIZE, however, */
492 /* information size may be longer */
493 length
= min_t(size_t, length
, (cb
->information
- *offset
));
495 if (copy_to_user(ubuf
, cb
->response_buffer
.data
+ *offset
, length
)) {
502 if ((unsigned long)*offset
< cb
->information
)
506 cb_pos
= find_read_list_entry(dev
, cl
);
507 /* Remove entry from read list */
509 list_del(&cb_pos
->cb_list
);
510 mei_free_cb_private(cb
);
511 cl
->reading_state
= MEI_IDLE
;
513 cl
->read_pending
= 0;
515 dev_dbg(&dev
->pdev
->dev
, "end mei read rets= %d\n", rets
);
516 mutex_unlock(&dev
->device_lock
);
521 * mei_write - the write function.
523 * @file: pointer to file structure
524 * @ubuf: pointer to user buffer
525 * @length: buffer length
526 * @offset: data offset in buffer
528 * returns >=0 data length on success , <0 on error
530 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
531 size_t length
, loff_t
*offset
)
533 struct mei_cl
*cl
= file
->private_data
;
534 struct mei_cl_cb
*write_cb
= NULL
;
535 struct mei_msg_hdr mei_hdr
;
536 struct mei_device
*dev
;
537 unsigned long timeout
= 0;
541 if (WARN_ON(!cl
|| !cl
->dev
))
546 mutex_lock(&dev
->device_lock
);
548 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
549 mutex_unlock(&dev
->device_lock
);
553 if (cl
== &dev
->iamthif_cl
) {
554 write_cb
= find_amthi_read_list_entry(dev
, file
);
557 timeout
= write_cb
->read_time
+
558 msecs_to_jiffies(IAMTHIF_READ_TIMER
);
560 if (time_after(jiffies
, timeout
) ||
561 cl
->reading_state
== MEI_READ_COMPLETE
) {
563 list_del(&write_cb
->cb_list
);
564 mei_free_cb_private(write_cb
);
570 /* free entry used in read */
571 if (cl
->reading_state
== MEI_READ_COMPLETE
) {
573 write_cb
= find_read_list_entry(dev
, cl
);
575 list_del(&write_cb
->cb_list
);
576 mei_free_cb_private(write_cb
);
578 cl
->reading_state
= MEI_IDLE
;
580 cl
->read_pending
= 0;
582 } else if (cl
->reading_state
== MEI_IDLE
&& !cl
->read_pending
)
586 write_cb
= kzalloc(sizeof(struct mei_cl_cb
), GFP_KERNEL
);
588 mutex_unlock(&dev
->device_lock
);
592 write_cb
->file_object
= file
;
593 write_cb
->file_private
= cl
;
594 write_cb
->request_buffer
.data
= kmalloc(length
, GFP_KERNEL
);
596 if (!write_cb
->request_buffer
.data
)
599 dev_dbg(&dev
->pdev
->dev
, "length =%d\n", (int) length
);
602 if (copy_from_user(write_cb
->request_buffer
.data
, ubuf
, length
))
607 ((memcmp(mei_wd_state_independence_msg
[0],
608 write_cb
->request_buffer
.data
, 4) == 0) ||
609 (memcmp(mei_wd_state_independence_msg
[1],
610 write_cb
->request_buffer
.data
, 4) == 0) ||
611 (memcmp(mei_wd_state_independence_msg
[2],
612 write_cb
->request_buffer
.data
, 4) == 0)))
613 cl
->sm_state
|= MEI_WD_STATE_INDEPENDENCE_MSG_SENT
;
615 INIT_LIST_HEAD(&write_cb
->cb_list
);
616 if (cl
== &dev
->iamthif_cl
) {
617 write_cb
->response_buffer
.data
=
618 kmalloc(dev
->iamthif_mtu
, GFP_KERNEL
);
619 if (!write_cb
->response_buffer
.data
) {
623 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
627 i
= mei_me_cl_by_id(dev
, dev
->iamthif_cl
.me_client_id
);
632 if (length
> dev
->me_clients
[i
].props
.max_msg_length
||
638 write_cb
->response_buffer
.size
= dev
->iamthif_mtu
;
639 write_cb
->major_file_operations
= MEI_IOCTL
;
640 write_cb
->information
= 0;
641 write_cb
->request_buffer
.size
= length
;
642 if (dev
->iamthif_cl
.state
!= MEI_FILE_CONNECTED
) {
647 if (!list_empty(&dev
->amthi_cmd_list
.mei_cb
.cb_list
) ||
648 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
649 dev_dbg(&dev
->pdev
->dev
, "amthi_state = %d\n",
650 (int) dev
->iamthif_state
);
651 dev_dbg(&dev
->pdev
->dev
, "add amthi cb to amthi cmd waiting list\n");
652 list_add_tail(&write_cb
->cb_list
,
653 &dev
->amthi_cmd_list
.mei_cb
.cb_list
);
656 dev_dbg(&dev
->pdev
->dev
, "call amthi write\n");
657 rets
= amthi_write(dev
, write_cb
);
660 dev_dbg(&dev
->pdev
->dev
, "amthi write failed with status = %d\n",
666 mutex_unlock(&dev
->device_lock
);
670 write_cb
->major_file_operations
= MEI_WRITE
;
671 /* make sure information is zero before we start */
673 write_cb
->information
= 0;
674 write_cb
->request_buffer
.size
= length
;
676 dev_dbg(&dev
->pdev
->dev
, "host client = %d, ME client = %d\n",
677 cl
->host_client_id
, cl
->me_client_id
);
678 if (cl
->state
!= MEI_FILE_CONNECTED
) {
680 dev_dbg(&dev
->pdev
->dev
, "host client = %d, is not connected to ME client = %d",
685 i
= mei_me_cl_by_id(dev
, cl
->me_client_id
);
690 if (length
> dev
->me_clients
[i
].props
.max_msg_length
|| length
<= 0) {
694 write_cb
->file_private
= cl
;
696 rets
= mei_flow_ctrl_creds(dev
, cl
);
700 if (rets
&& dev
->mei_host_buffer_is_empty
) {
702 dev
->mei_host_buffer_is_empty
= false;
703 if (length
> mei_hbuf_max_data(dev
)) {
704 mei_hdr
.length
= mei_hbuf_max_data(dev
);
705 mei_hdr
.msg_complete
= 0;
707 mei_hdr
.length
= length
;
708 mei_hdr
.msg_complete
= 1;
710 mei_hdr
.host_addr
= cl
->host_client_id
;
711 mei_hdr
.me_addr
= cl
->me_client_id
;
712 mei_hdr
.reserved
= 0;
713 dev_dbg(&dev
->pdev
->dev
, "call mei_write_message header=%08x.\n",
714 *((u32
*) &mei_hdr
));
715 if (mei_write_message(dev
, &mei_hdr
,
716 (unsigned char *) (write_cb
->request_buffer
.data
),
721 cl
->writing_state
= MEI_WRITING
;
722 write_cb
->information
= mei_hdr
.length
;
723 if (mei_hdr
.msg_complete
) {
724 if (mei_flow_ctrl_reduce(dev
, cl
)) {
728 list_add_tail(&write_cb
->cb_list
,
729 &dev
->write_waiting_list
.mei_cb
.cb_list
);
731 list_add_tail(&write_cb
->cb_list
,
732 &dev
->write_list
.mei_cb
.cb_list
);
737 write_cb
->information
= 0;
738 cl
->writing_state
= MEI_WRITING
;
739 list_add_tail(&write_cb
->cb_list
,
740 &dev
->write_list
.mei_cb
.cb_list
);
742 mutex_unlock(&dev
->device_lock
);
746 mutex_unlock(&dev
->device_lock
);
747 mei_free_cb_private(write_cb
);
753 * mei_ioctl - the IOCTL function
755 * @file: pointer to file structure
756 * @cmd: ioctl command
757 * @data: pointer to mei message structure
759 * returns 0 on success , <0 on error
761 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
763 struct mei_device
*dev
;
764 struct mei_cl
*cl
= file
->private_data
;
765 struct mei_connect_client_data
*connect_data
= NULL
;
768 if (cmd
!= IOCTL_MEI_CONNECT_CLIENT
)
771 if (WARN_ON(!cl
|| !cl
->dev
))
776 dev_dbg(&dev
->pdev
->dev
, "IOCTL cmd = 0x%x", cmd
);
778 mutex_lock(&dev
->device_lock
);
779 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
784 dev_dbg(&dev
->pdev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
786 connect_data
= kzalloc(sizeof(struct mei_connect_client_data
),
792 dev_dbg(&dev
->pdev
->dev
, "copy connect data from user\n");
793 if (copy_from_user(connect_data
, (char __user
*)data
,
794 sizeof(struct mei_connect_client_data
))) {
795 dev_dbg(&dev
->pdev
->dev
, "failed to copy data from userland\n");
799 rets
= mei_ioctl_connect_client(file
, connect_data
);
801 /* if all is ok, copying the data back to user. */
805 dev_dbg(&dev
->pdev
->dev
, "copy connect data to user\n");
806 if (copy_to_user((char __user
*)data
, connect_data
,
807 sizeof(struct mei_connect_client_data
))) {
808 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
815 mutex_unlock(&dev
->device_lock
);
820 * mei_compat_ioctl - the compat IOCTL function
822 * @file: pointer to file structure
823 * @cmd: ioctl command
824 * @data: pointer to mei message structure
826 * returns 0 on success , <0 on error
829 static long mei_compat_ioctl(struct file
*file
,
830 unsigned int cmd
, unsigned long data
)
832 return mei_ioctl(file
, cmd
, (unsigned long)compat_ptr(data
));
838 * mei_poll - the poll function
840 * @file: pointer to file structure
841 * @wait: pointer to poll_table structure
845 static unsigned int mei_poll(struct file
*file
, poll_table
*wait
)
847 struct mei_cl
*cl
= file
->private_data
;
848 struct mei_device
*dev
;
849 unsigned int mask
= 0;
851 if (WARN_ON(!cl
|| !cl
->dev
))
856 mutex_lock(&dev
->device_lock
);
858 if (dev
->dev_state
!= MEI_DEV_ENABLED
)
862 if (cl
== &dev
->iamthif_cl
) {
863 mutex_unlock(&dev
->device_lock
);
864 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
865 mutex_lock(&dev
->device_lock
);
866 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
867 dev
->iamthif_file_object
== file
) {
868 mask
|= (POLLIN
| POLLRDNORM
);
869 dev_dbg(&dev
->pdev
->dev
, "run next amthi cb\n");
870 mei_run_next_iamthif_cmd(dev
);
875 mutex_unlock(&dev
->device_lock
);
876 poll_wait(file
, &cl
->tx_wait
, wait
);
877 mutex_lock(&dev
->device_lock
);
878 if (MEI_WRITE_COMPLETE
== cl
->writing_state
)
879 mask
|= (POLLIN
| POLLRDNORM
);
882 mutex_unlock(&dev
->device_lock
);
887 * file operations structure will be used for mei char device.
889 static const struct file_operations mei_fops
= {
890 .owner
= THIS_MODULE
,
892 .unlocked_ioctl
= mei_ioctl
,
894 .compat_ioctl
= mei_compat_ioctl
,
897 .release
= mei_release
,
907 static struct miscdevice mei_misc_device
= {
910 .minor
= MISC_DYNAMIC_MINOR
,
914 * mei_quirk_probe - probe for devices that doesn't valid ME interface
915 * @pdev: PCI device structure
916 * @ent: entry into pci_device_table
918 * returns true if ME Interface is valid, false otherwise
920 static bool __devinit
mei_quirk_probe(struct pci_dev
*pdev
,
921 const struct pci_device_id
*ent
)
924 if (ent
->device
== MEI_DEV_ID_PBG_1
) {
925 pci_read_config_dword(pdev
, 0x48, ®
);
926 /* make sure that bit 9 is up and bit 10 is down */
927 if ((reg
& 0x600) == 0x200) {
928 dev_info(&pdev
->dev
, "Device doesn't have valid ME Interface\n");
935 * mei_probe - Device Initialization Routine
937 * @pdev: PCI device structure
938 * @ent: entry in kcs_pci_tbl
940 * returns 0 on success, <0 on failure.
942 static int __devinit
mei_probe(struct pci_dev
*pdev
,
943 const struct pci_device_id
*ent
)
945 struct mei_device
*dev
;
948 mutex_lock(&mei_mutex
);
950 if (!mei_quirk_probe(pdev
, ent
)) {
960 err
= pci_enable_device(pdev
);
962 dev_err(&pdev
->dev
, "failed to enable pci device.\n");
965 /* set PCI host mastering */
966 pci_set_master(pdev
);
967 /* pci request regions for mei driver */
968 err
= pci_request_regions(pdev
, KBUILD_MODNAME
);
970 dev_err(&pdev
->dev
, "failed to get pci regions.\n");
973 /* allocates and initializes the mei dev structure */
974 dev
= mei_device_init(pdev
);
977 goto release_regions
;
979 /* mapping IO device memory */
980 dev
->mem_addr
= pci_iomap(pdev
, 0, 0);
981 if (!dev
->mem_addr
) {
982 dev_err(&pdev
->dev
, "mapping I/O device memory failure.\n");
986 pci_enable_msi(pdev
);
988 /* request and enable interrupt */
989 if (pci_dev_msi_enabled(pdev
))
990 err
= request_threaded_irq(pdev
->irq
,
992 mei_interrupt_thread_handler
,
993 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
995 err
= request_threaded_irq(pdev
->irq
,
996 mei_interrupt_quick_handler
,
997 mei_interrupt_thread_handler
,
998 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
1001 dev_err(&pdev
->dev
, "request_threaded_irq failure. irq = %d\n",
1005 INIT_DELAYED_WORK(&dev
->timer_work
, mei_timer
);
1006 if (mei_hw_init(dev
)) {
1007 dev_err(&pdev
->dev
, "init hw failure.\n");
1012 err
= misc_register(&mei_misc_device
);
1017 pci_set_drvdata(pdev
, dev
);
1020 schedule_delayed_work(&dev
->timer_work
, HZ
);
1022 mutex_unlock(&mei_mutex
);
1024 pr_debug("initialization successful.\n");
1029 /* disable interrupts */
1030 dev
->host_hw_state
= mei_hcsr_read(dev
);
1031 mei_disable_interrupts(dev
);
1032 flush_scheduled_work();
1033 free_irq(pdev
->irq
, dev
);
1035 pci_disable_msi(pdev
);
1036 pci_iounmap(pdev
, dev
->mem_addr
);
1040 pci_release_regions(pdev
);
1042 pci_disable_device(pdev
);
1044 mutex_unlock(&mei_mutex
);
1045 dev_err(&pdev
->dev
, "initialization failed.\n");
1050 * mei_remove - Device Removal Routine
1052 * @pdev: PCI device structure
1054 * mei_remove is called by the PCI subsystem to alert the driver
1055 * that it should release a PCI device.
1057 static void __devexit
mei_remove(struct pci_dev
*pdev
)
1059 struct mei_device
*dev
;
1061 if (mei_pdev
!= pdev
)
1064 dev
= pci_get_drvdata(pdev
);
1068 mutex_lock(&dev
->device_lock
);
1070 cancel_delayed_work(&dev
->timer_work
);
1076 if (dev
->iamthif_cl
.state
== MEI_FILE_CONNECTED
) {
1077 dev
->iamthif_cl
.state
= MEI_FILE_DISCONNECTING
;
1078 mei_disconnect_host_client(dev
, &dev
->iamthif_cl
);
1080 if (dev
->wd_cl
.state
== MEI_FILE_CONNECTED
) {
1081 dev
->wd_cl
.state
= MEI_FILE_DISCONNECTING
;
1082 mei_disconnect_host_client(dev
, &dev
->wd_cl
);
1085 /* Unregistering watchdog device */
1086 mei_watchdog_unregister(dev
);
1088 /* remove entry if already in list */
1089 dev_dbg(&pdev
->dev
, "list del iamthif and wd file list.\n");
1090 mei_remove_client_from_file_list(dev
, dev
->wd_cl
.host_client_id
);
1091 mei_remove_client_from_file_list(dev
, dev
->iamthif_cl
.host_client_id
);
1093 dev
->iamthif_current_cb
= NULL
;
1094 dev
->me_clients_num
= 0;
1096 mutex_unlock(&dev
->device_lock
);
1098 flush_scheduled_work();
1100 /* disable interrupts */
1101 mei_disable_interrupts(dev
);
1103 free_irq(pdev
->irq
, dev
);
1104 pci_disable_msi(pdev
);
1105 pci_set_drvdata(pdev
, NULL
);
1108 pci_iounmap(pdev
, dev
->mem_addr
);
1112 pci_release_regions(pdev
);
1113 pci_disable_device(pdev
);
1115 misc_deregister(&mei_misc_device
);
1118 static int mei_pci_suspend(struct device
*device
)
1120 struct pci_dev
*pdev
= to_pci_dev(device
);
1121 struct mei_device
*dev
= pci_get_drvdata(pdev
);
1126 mutex_lock(&dev
->device_lock
);
1128 cancel_delayed_work(&dev
->timer_work
);
1130 /* Stop watchdog if exists */
1131 err
= mei_wd_stop(dev
);
1132 /* Set new mei state */
1133 if (dev
->dev_state
== MEI_DEV_ENABLED
||
1134 dev
->dev_state
== MEI_DEV_RECOVERING_FROM_RESET
) {
1135 dev
->dev_state
= MEI_DEV_POWER_DOWN
;
1138 mutex_unlock(&dev
->device_lock
);
1140 free_irq(pdev
->irq
, dev
);
1141 pci_disable_msi(pdev
);
1146 static int mei_pci_resume(struct device
*device
)
1148 struct pci_dev
*pdev
= to_pci_dev(device
);
1149 struct mei_device
*dev
;
1152 dev
= pci_get_drvdata(pdev
);
1156 pci_enable_msi(pdev
);
1158 /* request and enable interrupt */
1159 if (pci_dev_msi_enabled(pdev
))
1160 err
= request_threaded_irq(pdev
->irq
,
1162 mei_interrupt_thread_handler
,
1163 IRQF_ONESHOT
, KBUILD_MODNAME
, dev
);
1165 err
= request_threaded_irq(pdev
->irq
,
1166 mei_interrupt_quick_handler
,
1167 mei_interrupt_thread_handler
,
1168 IRQF_SHARED
, KBUILD_MODNAME
, dev
);
1171 dev_err(&pdev
->dev
, "request_threaded_irq failed: irq = %d.\n",
1176 mutex_lock(&dev
->device_lock
);
1177 dev
->dev_state
= MEI_DEV_POWER_UP
;
1179 mutex_unlock(&dev
->device_lock
);
1181 /* Start timer if stopped in suspend */
1182 schedule_delayed_work(&dev
->timer_work
, HZ
);
1186 static SIMPLE_DEV_PM_OPS(mei_pm_ops
, mei_pci_suspend
, mei_pci_resume
);
1187 #define MEI_PM_OPS (&mei_pm_ops)
1189 #define MEI_PM_OPS NULL
1190 #endif /* CONFIG_PM */
1192 * PCI driver structure
1194 static struct pci_driver mei_driver
= {
1195 .name
= KBUILD_MODNAME
,
1196 .id_table
= mei_pci_tbl
,
1198 .remove
= __devexit_p(mei_remove
),
1199 .shutdown
= __devexit_p(mei_remove
),
1200 .driver
.pm
= MEI_PM_OPS
,
1203 module_pci_driver(mei_driver
);
1205 MODULE_AUTHOR("Intel Corporation");
1206 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1207 MODULE_LICENSE("GPL v2");