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 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/fcntl.h>
22 #include <linux/aio.h>
23 #include <linux/pci.h>
24 #include <linux/ioctl.h>
25 #include <linux/cdev.h>
26 #include <linux/list.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/uuid.h>
30 #include <linux/jiffies.h>
31 #include <linux/uaccess.h>
33 #include <linux/mei.h>
39 const uuid_le mei_amthif_guid
= UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
40 0xac, 0xa8, 0x46, 0xe0,
41 0xff, 0x65, 0x81, 0x4c);
44 * mei_amthif_reset_params - initializes mei device iamthif
46 * @dev: the device structure
48 void mei_amthif_reset_params(struct mei_device
*dev
)
50 /* reset iamthif parameters. */
51 dev
->iamthif_current_cb
= NULL
;
52 dev
->iamthif_msg_buf_size
= 0;
53 dev
->iamthif_msg_buf_index
= 0;
54 dev
->iamthif_canceled
= false;
55 dev
->iamthif_ioctl
= false;
56 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
57 dev
->iamthif_timer
= 0;
58 dev
->iamthif_stall_timer
= 0;
59 dev
->iamthif_open_count
= 0;
63 * mei_amthif_host_init - mei initialization amthif client.
65 * @dev: the device structure
68 int mei_amthif_host_init(struct mei_device
*dev
)
70 struct mei_cl
*cl
= &dev
->iamthif_cl
;
71 unsigned char *msg_buf
;
74 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
78 i
= mei_me_cl_by_uuid(dev
, &mei_amthif_guid
);
80 dev_info(&dev
->pdev
->dev
,
81 "amthif: failed to find the client %d\n", i
);
85 cl
->me_client_id
= dev
->me_clients
[i
].client_id
;
87 /* Assign iamthif_mtu to the value received from ME */
89 dev
->iamthif_mtu
= dev
->me_clients
[i
].props
.max_msg_length
;
90 dev_dbg(&dev
->pdev
->dev
, "IAMTHIF_MTU = %d\n",
91 dev
->me_clients
[i
].props
.max_msg_length
);
93 kfree(dev
->iamthif_msg_buf
);
94 dev
->iamthif_msg_buf
= NULL
;
96 /* allocate storage for ME message buffer */
97 msg_buf
= kcalloc(dev
->iamthif_mtu
,
98 sizeof(unsigned char), GFP_KERNEL
);
100 dev_err(&dev
->pdev
->dev
, "amthif: memory allocation for ME message buffer failed.\n");
104 dev
->iamthif_msg_buf
= msg_buf
;
106 ret
= mei_cl_link(cl
, MEI_IAMTHIF_HOST_CLIENT_ID
);
109 dev_err(&dev
->pdev
->dev
,
110 "amthif: failed link client %d\n", ret
);
114 cl
->state
= MEI_FILE_CONNECTING
;
116 ret
= mei_cl_connect(cl
, NULL
);
118 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
124 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
126 * @dev: the device structure
127 * @file: pointer to file object
129 * returns returned a list entry on success, NULL on failure.
131 struct mei_cl_cb
*mei_amthif_find_read_list_entry(struct mei_device
*dev
,
134 struct mei_cl_cb
*cb
;
136 list_for_each_entry(cb
, &dev
->amthif_rd_complete_list
.list
, list
) {
137 if (cb
->cl
&& cb
->cl
== &dev
->iamthif_cl
&&
138 cb
->file_object
== file
)
146 * mei_amthif_read - read data from AMTHIF client
148 * @dev: the device structure
149 * @if_num: minor number
150 * @file: pointer to file object
151 * @*ubuf: pointer to user data in user space
152 * @length: data length to read
153 * @offset: data read offset
155 * Locking: called under "dev->device_lock" lock
158 * returned data length on success,
159 * zero if no data to read,
160 * negative on failure.
162 int mei_amthif_read(struct mei_device
*dev
, struct file
*file
,
163 char __user
*ubuf
, size_t length
, loff_t
*offset
)
167 struct mei_cl_cb
*cb
= NULL
;
168 struct mei_cl
*cl
= file
->private_data
;
169 unsigned long timeout
;
172 /* Only possible if we are in timeout */
173 if (!cl
|| cl
!= &dev
->iamthif_cl
) {
174 dev_dbg(&dev
->pdev
->dev
, "bad file ext.\n");
178 i
= mei_me_cl_by_id(dev
, dev
->iamthif_cl
.me_client_id
);
180 dev_dbg(&dev
->pdev
->dev
, "amthif client not found.\n");
183 dev_dbg(&dev
->pdev
->dev
, "checking amthif data\n");
184 cb
= mei_amthif_find_read_list_entry(dev
, file
);
186 /* Check for if we can block or not*/
187 if (cb
== NULL
&& file
->f_flags
& O_NONBLOCK
)
191 dev_dbg(&dev
->pdev
->dev
, "waiting for amthif data\n");
193 /* unlock the Mutex */
194 mutex_unlock(&dev
->device_lock
);
196 wait_ret
= wait_event_interruptible(dev
->iamthif_cl
.wait
,
197 (cb
= mei_amthif_find_read_list_entry(dev
, file
)));
199 /* Locking again the Mutex */
200 mutex_lock(&dev
->device_lock
);
205 dev_dbg(&dev
->pdev
->dev
, "woke up from sleep\n");
209 dev_dbg(&dev
->pdev
->dev
, "Got amthif data\n");
210 dev
->iamthif_timer
= 0;
213 timeout
= cb
->read_time
+
214 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER
);
215 dev_dbg(&dev
->pdev
->dev
, "amthif timeout = %lud\n",
218 if (time_after(jiffies
, timeout
)) {
219 dev_dbg(&dev
->pdev
->dev
, "amthif Time out\n");
220 /* 15 sec for the message has expired */
226 /* if the whole message will fit remove it from the list */
227 if (cb
->buf_idx
>= *offset
&& length
>= (cb
->buf_idx
- *offset
))
229 else if (cb
->buf_idx
> 0 && cb
->buf_idx
<= *offset
) {
230 /* end of the message has been reached */
235 /* else means that not full buffer will be read and do not
236 * remove message from deletion list
239 dev_dbg(&dev
->pdev
->dev
, "amthif cb->response_buffer size - %d\n",
240 cb
->response_buffer
.size
);
241 dev_dbg(&dev
->pdev
->dev
, "amthif cb->buf_idx - %lu\n", cb
->buf_idx
);
243 /* length is being truncated to PAGE_SIZE, however,
244 * the buf_idx may point beyond */
245 length
= min_t(size_t, length
, (cb
->buf_idx
- *offset
));
247 if (copy_to_user(ubuf
, cb
->response_buffer
.data
+ *offset
, length
)) {
248 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
252 if ((*offset
+ length
) < cb
->buf_idx
) {
258 dev_dbg(&dev
->pdev
->dev
, "free amthif cb memory.\n");
266 * mei_amthif_send_cmd - send amthif command to the ME
268 * @dev: the device structure
269 * @cb: mei call back struct
271 * returns 0 on success, <0 on failure.
274 static int mei_amthif_send_cmd(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
276 struct mei_msg_hdr mei_hdr
;
282 dev_dbg(&dev
->pdev
->dev
, "write data to amthif client.\n");
284 dev
->iamthif_state
= MEI_IAMTHIF_WRITING
;
285 dev
->iamthif_current_cb
= cb
;
286 dev
->iamthif_file_object
= cb
->file_object
;
287 dev
->iamthif_canceled
= false;
288 dev
->iamthif_ioctl
= true;
289 dev
->iamthif_msg_buf_size
= cb
->request_buffer
.size
;
290 memcpy(dev
->iamthif_msg_buf
, cb
->request_buffer
.data
,
291 cb
->request_buffer
.size
);
293 ret
= mei_cl_flow_ctrl_creds(&dev
->iamthif_cl
);
297 if (ret
&& mei_hbuf_acquire(dev
)) {
299 if (cb
->request_buffer
.size
> mei_hbuf_max_len(dev
)) {
300 mei_hdr
.length
= mei_hbuf_max_len(dev
);
301 mei_hdr
.msg_complete
= 0;
303 mei_hdr
.length
= cb
->request_buffer
.size
;
304 mei_hdr
.msg_complete
= 1;
307 mei_hdr
.host_addr
= dev
->iamthif_cl
.host_client_id
;
308 mei_hdr
.me_addr
= dev
->iamthif_cl
.me_client_id
;
309 mei_hdr
.reserved
= 0;
310 mei_hdr
.internal
= 0;
311 dev
->iamthif_msg_buf_index
+= mei_hdr
.length
;
312 ret
= mei_write_message(dev
, &mei_hdr
, dev
->iamthif_msg_buf
);
316 if (mei_hdr
.msg_complete
) {
317 if (mei_cl_flow_ctrl_reduce(&dev
->iamthif_cl
))
319 dev
->iamthif_flow_control_pending
= true;
320 dev
->iamthif_state
= MEI_IAMTHIF_FLOW_CONTROL
;
321 dev_dbg(&dev
->pdev
->dev
, "add amthif cb to write waiting list\n");
322 dev
->iamthif_current_cb
= cb
;
323 dev
->iamthif_file_object
= cb
->file_object
;
324 list_add_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
326 dev_dbg(&dev
->pdev
->dev
, "message does not complete, so add amthif cb to write list.\n");
327 list_add_tail(&cb
->list
, &dev
->write_list
.list
);
330 list_add_tail(&cb
->list
, &dev
->write_list
.list
);
336 * mei_amthif_write - write amthif data to amthif client
338 * @dev: the device structure
339 * @cb: mei call back struct
341 * returns 0 on success, <0 on failure.
344 int mei_amthif_write(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
351 ret
= mei_io_cb_alloc_resp_buf(cb
, dev
->iamthif_mtu
);
355 cb
->fop_type
= MEI_FOP_WRITE
;
357 if (!list_empty(&dev
->amthif_cmd_list
.list
) ||
358 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
359 dev_dbg(&dev
->pdev
->dev
,
360 "amthif state = %d\n", dev
->iamthif_state
);
361 dev_dbg(&dev
->pdev
->dev
, "AMTHIF: add cb to the wait list\n");
362 list_add_tail(&cb
->list
, &dev
->amthif_cmd_list
.list
);
365 return mei_amthif_send_cmd(dev
, cb
);
368 * mei_amthif_run_next_cmd
370 * @dev: the device structure
372 * returns 0 on success, <0 on failure.
374 void mei_amthif_run_next_cmd(struct mei_device
*dev
)
376 struct mei_cl_cb
*pos
= NULL
;
377 struct mei_cl_cb
*next
= NULL
;
383 dev
->iamthif_msg_buf_size
= 0;
384 dev
->iamthif_msg_buf_index
= 0;
385 dev
->iamthif_canceled
= false;
386 dev
->iamthif_ioctl
= true;
387 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
388 dev
->iamthif_timer
= 0;
389 dev
->iamthif_file_object
= NULL
;
391 dev_dbg(&dev
->pdev
->dev
, "complete amthif cmd_list cb.\n");
393 list_for_each_entry_safe(pos
, next
, &dev
->amthif_cmd_list
.list
, list
) {
394 list_del(&pos
->list
);
396 if (pos
->cl
&& pos
->cl
== &dev
->iamthif_cl
) {
397 status
= mei_amthif_send_cmd(dev
, pos
);
399 dev_dbg(&dev
->pdev
->dev
,
400 "amthif write failed status = %d\n",
410 unsigned int mei_amthif_poll(struct mei_device
*dev
,
411 struct file
*file
, poll_table
*wait
)
413 unsigned int mask
= 0;
415 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
417 mutex_lock(&dev
->device_lock
);
418 if (!mei_cl_is_connected(&dev
->iamthif_cl
)) {
422 } else if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
423 dev
->iamthif_file_object
== file
) {
425 mask
|= (POLLIN
| POLLRDNORM
);
426 dev_dbg(&dev
->pdev
->dev
, "run next amthif cb\n");
427 mei_amthif_run_next_cmd(dev
);
429 mutex_unlock(&dev
->device_lock
);
437 * mei_amthif_irq_write - write iamthif command in irq thread context.
439 * @dev: the device structure.
440 * @cb_pos: callback block.
441 * @cl: private data of the file object.
442 * @cmpl_list: complete list.
444 * returns 0, OK; otherwise, error.
446 int mei_amthif_irq_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
447 struct mei_cl_cb
*cmpl_list
)
449 struct mei_device
*dev
= cl
->dev
;
450 struct mei_msg_hdr mei_hdr
;
451 size_t len
= dev
->iamthif_msg_buf_size
- dev
->iamthif_msg_buf_index
;
452 u32 msg_slots
= mei_data2slots(len
);
456 rets
= mei_cl_flow_ctrl_creds(cl
);
461 cl_dbg(dev
, cl
, "No flow control credentials: not sending.\n");
465 mei_hdr
.host_addr
= cl
->host_client_id
;
466 mei_hdr
.me_addr
= cl
->me_client_id
;
467 mei_hdr
.reserved
= 0;
468 mei_hdr
.internal
= 0;
470 slots
= mei_hbuf_empty_slots(dev
);
472 if (slots
>= msg_slots
) {
473 mei_hdr
.length
= len
;
474 mei_hdr
.msg_complete
= 1;
475 /* Split the message only if we can write the whole host buffer */
476 } else if (slots
== dev
->hbuf_depth
) {
478 len
= (slots
* sizeof(u32
)) - sizeof(struct mei_msg_hdr
);
479 mei_hdr
.length
= len
;
480 mei_hdr
.msg_complete
= 0;
482 /* wait for next time the host buffer is empty */
486 dev_dbg(&dev
->pdev
->dev
, MEI_HDR_FMT
, MEI_HDR_PRM(&mei_hdr
));
488 rets
= mei_write_message(dev
, &mei_hdr
,
489 dev
->iamthif_msg_buf
+ dev
->iamthif_msg_buf_index
);
491 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
497 if (mei_cl_flow_ctrl_reduce(cl
))
500 dev
->iamthif_msg_buf_index
+= mei_hdr
.length
;
503 if (mei_hdr
.msg_complete
) {
504 dev
->iamthif_state
= MEI_IAMTHIF_FLOW_CONTROL
;
505 dev
->iamthif_flow_control_pending
= true;
507 /* save iamthif cb sent to amthif client */
508 cb
->buf_idx
= dev
->iamthif_msg_buf_index
;
509 dev
->iamthif_current_cb
= cb
;
511 list_move_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
519 * mei_amthif_irq_read_message - read routine after ISR to
520 * handle the read amthif message
522 * @dev: the device structure
523 * @mei_hdr: header of amthif message
524 * @complete_list: An instance of our list structure
526 * returns 0 on success, <0 on failure.
528 int mei_amthif_irq_read_msg(struct mei_device
*dev
,
529 struct mei_msg_hdr
*mei_hdr
,
530 struct mei_cl_cb
*complete_list
)
532 struct mei_cl_cb
*cb
;
533 unsigned char *buffer
;
535 BUG_ON(mei_hdr
->me_addr
!= dev
->iamthif_cl
.me_client_id
);
536 BUG_ON(dev
->iamthif_state
!= MEI_IAMTHIF_READING
);
538 buffer
= dev
->iamthif_msg_buf
+ dev
->iamthif_msg_buf_index
;
539 BUG_ON(dev
->iamthif_mtu
< dev
->iamthif_msg_buf_index
+ mei_hdr
->length
);
541 mei_read_slots(dev
, buffer
, mei_hdr
->length
);
543 dev
->iamthif_msg_buf_index
+= mei_hdr
->length
;
545 if (!mei_hdr
->msg_complete
)
548 dev_dbg(&dev
->pdev
->dev
, "amthif_message_buffer_index =%d\n",
551 dev_dbg(&dev
->pdev
->dev
, "completed amthif read.\n ");
552 if (!dev
->iamthif_current_cb
)
555 cb
= dev
->iamthif_current_cb
;
556 dev
->iamthif_current_cb
= NULL
;
561 dev
->iamthif_stall_timer
= 0;
562 cb
->buf_idx
= dev
->iamthif_msg_buf_index
;
563 cb
->read_time
= jiffies
;
564 if (dev
->iamthif_ioctl
&& cb
->cl
== &dev
->iamthif_cl
) {
565 /* found the iamthif cb */
566 dev_dbg(&dev
->pdev
->dev
, "complete the amthif read cb.\n ");
567 dev_dbg(&dev
->pdev
->dev
, "add the amthif read cb to complete.\n ");
568 list_add_tail(&cb
->list
, &complete_list
->list
);
574 * mei_amthif_irq_read - prepares to read amthif data.
576 * @dev: the device structure.
577 * @slots: free slots.
579 * returns 0, OK; otherwise, error.
581 int mei_amthif_irq_read(struct mei_device
*dev
, s32
*slots
)
583 u32 msg_slots
= mei_data2slots(sizeof(struct hbm_flow_control
));
585 if (*slots
< msg_slots
)
590 if (mei_hbm_cl_flow_control_req(dev
, &dev
->iamthif_cl
)) {
591 dev_dbg(&dev
->pdev
->dev
, "iamthif flow control failed\n");
595 dev_dbg(&dev
->pdev
->dev
, "iamthif flow control success\n");
596 dev
->iamthif_state
= MEI_IAMTHIF_READING
;
597 dev
->iamthif_flow_control_pending
= false;
598 dev
->iamthif_msg_buf_index
= 0;
599 dev
->iamthif_msg_buf_size
= 0;
600 dev
->iamthif_stall_timer
= MEI_IAMTHIF_STALL_TIMER
;
601 dev
->hbuf_is_ready
= mei_hbuf_is_ready(dev
);
606 * mei_amthif_complete - complete amthif callback.
608 * @dev: the device structure.
609 * @cb_pos: callback block.
611 void mei_amthif_complete(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
613 if (dev
->iamthif_canceled
!= 1) {
614 dev
->iamthif_state
= MEI_IAMTHIF_READ_COMPLETE
;
615 dev
->iamthif_stall_timer
= 0;
616 memcpy(cb
->response_buffer
.data
,
617 dev
->iamthif_msg_buf
,
618 dev
->iamthif_msg_buf_index
);
619 list_add_tail(&cb
->list
, &dev
->amthif_rd_complete_list
.list
);
620 dev_dbg(&dev
->pdev
->dev
, "amthif read completed\n");
621 dev
->iamthif_timer
= jiffies
;
622 dev_dbg(&dev
->pdev
->dev
, "dev->iamthif_timer = %ld\n",
625 mei_amthif_run_next_cmd(dev
);
628 dev_dbg(&dev
->pdev
->dev
, "completing amthif call back.\n");
629 wake_up_interruptible(&dev
->iamthif_cl
.wait
);
633 * mei_clear_list - removes all callbacks associated with file
636 * @dev: device structure.
637 * @file: file structure
638 * @mei_cb_list: callbacks list
640 * mei_clear_list is called to clear resources associated with file
641 * when application calls close function or Ctrl-C was pressed
643 * returns true if callback removed from the list, false otherwise
645 static bool mei_clear_list(struct mei_device
*dev
,
646 const struct file
*file
, struct list_head
*mei_cb_list
)
648 struct mei_cl_cb
*cb_pos
= NULL
;
649 struct mei_cl_cb
*cb_next
= NULL
;
650 bool removed
= false;
652 /* list all list member */
653 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, list
) {
654 /* check if list member associated with a file */
655 if (file
== cb_pos
->file_object
) {
656 /* remove member from the list */
657 list_del(&cb_pos
->list
);
658 /* check if cb equal to current iamthif cb */
659 if (dev
->iamthif_current_cb
== cb_pos
) {
660 dev
->iamthif_current_cb
= NULL
;
661 /* send flow control to iamthif client */
662 mei_hbm_cl_flow_control_req(dev
,
665 /* free all allocated buffers */
666 mei_io_cb_free(cb_pos
);
675 * mei_clear_lists - removes all callbacks associated with file
677 * @dev: device structure
678 * @file: file structure
680 * mei_clear_lists is called to clear resources associated with file
681 * when application calls close function or Ctrl-C was pressed
683 * returns true if callback removed from the list, false otherwise
685 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
687 bool removed
= false;
689 /* remove callbacks associated with a file */
690 mei_clear_list(dev
, file
, &dev
->amthif_cmd_list
.list
);
691 if (mei_clear_list(dev
, file
, &dev
->amthif_rd_complete_list
.list
))
694 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.list
);
696 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.list
))
699 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.list
))
702 if (mei_clear_list(dev
, file
, &dev
->write_list
.list
))
705 /* check if iamthif_current_cb not NULL */
706 if (dev
->iamthif_current_cb
&& !removed
) {
707 /* check file and iamthif current cb association */
708 if (dev
->iamthif_current_cb
->file_object
== file
) {
710 mei_io_cb_free(dev
->iamthif_current_cb
);
711 dev
->iamthif_current_cb
= NULL
;
719 * mei_amthif_release - the release function
721 * @dev: device structure
722 * @file: pointer to file structure
724 * returns 0 on success, <0 on error
726 int mei_amthif_release(struct mei_device
*dev
, struct file
*file
)
728 if (dev
->iamthif_open_count
> 0)
729 dev
->iamthif_open_count
--;
731 if (dev
->iamthif_file_object
== file
&&
732 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
734 dev_dbg(&dev
->pdev
->dev
, "amthif canceled iamthif state %d\n",
736 dev
->iamthif_canceled
= true;
737 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
738 dev_dbg(&dev
->pdev
->dev
, "run next amthif iamthif cb\n");
739 mei_amthif_run_next_cmd(dev
);
743 if (mei_clear_lists(dev
, file
))
744 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;