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 struct mei_me_client
*me_cl
;
72 unsigned char *msg_buf
;
75 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
79 me_cl
= mei_me_cl_by_uuid(dev
, &mei_amthif_guid
);
81 dev_info(&dev
->pdev
->dev
, "amthif: failed to find the client");
85 cl
->me_client_id
= me_cl
->client_id
;
86 cl
->cl_uuid
= me_cl
->props
.protocol_name
;
88 /* Assign iamthif_mtu to the value received from ME */
90 dev
->iamthif_mtu
= me_cl
->props
.max_msg_length
;
91 dev_dbg(&dev
->pdev
->dev
, "IAMTHIF_MTU = %d\n", dev
->iamthif_mtu
);
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 ret
= mei_cl_connect(cl
, NULL
);
116 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
122 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
124 * @dev: the device structure
125 * @file: pointer to file object
127 * returns returned a list entry on success, NULL on failure.
129 struct mei_cl_cb
*mei_amthif_find_read_list_entry(struct mei_device
*dev
,
132 struct mei_cl_cb
*cb
;
133 list_for_each_entry(cb
, &dev
->amthif_rd_complete_list
.list
, list
)
134 if (cb
->file_object
== file
)
141 * mei_amthif_read - read data from AMTHIF client
143 * @dev: the device structure
144 * @if_num: minor number
145 * @file: pointer to file object
146 * @*ubuf: pointer to user data in user space
147 * @length: data length to read
148 * @offset: data read offset
150 * Locking: called under "dev->device_lock" lock
153 * returned data length on success,
154 * zero if no data to read,
155 * negative on failure.
157 int mei_amthif_read(struct mei_device
*dev
, struct file
*file
,
158 char __user
*ubuf
, size_t length
, loff_t
*offset
)
160 struct mei_cl
*cl
= file
->private_data
;
161 struct mei_cl_cb
*cb
;
162 unsigned long timeout
;
166 /* Only possible if we are in timeout */
168 dev_err(&dev
->pdev
->dev
, "bad file ext.\n");
172 dev_dbg(&dev
->pdev
->dev
, "checking amthif data\n");
173 cb
= mei_amthif_find_read_list_entry(dev
, file
);
175 /* Check for if we can block or not*/
176 if (cb
== NULL
&& file
->f_flags
& O_NONBLOCK
)
180 dev_dbg(&dev
->pdev
->dev
, "waiting for amthif data\n");
182 /* unlock the Mutex */
183 mutex_unlock(&dev
->device_lock
);
185 wait_ret
= wait_event_interruptible(dev
->iamthif_cl
.wait
,
186 (cb
= mei_amthif_find_read_list_entry(dev
, file
)));
188 /* Locking again the Mutex */
189 mutex_lock(&dev
->device_lock
);
194 dev_dbg(&dev
->pdev
->dev
, "woke up from sleep\n");
198 dev_dbg(&dev
->pdev
->dev
, "Got amthif data\n");
199 dev
->iamthif_timer
= 0;
202 timeout
= cb
->read_time
+
203 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER
);
204 dev_dbg(&dev
->pdev
->dev
, "amthif timeout = %lud\n",
207 if (time_after(jiffies
, timeout
)) {
208 dev_dbg(&dev
->pdev
->dev
, "amthif Time out\n");
209 /* 15 sec for the message has expired */
215 /* if the whole message will fit remove it from the list */
216 if (cb
->buf_idx
>= *offset
&& length
>= (cb
->buf_idx
- *offset
))
218 else if (cb
->buf_idx
> 0 && cb
->buf_idx
<= *offset
) {
219 /* end of the message has been reached */
224 /* else means that not full buffer will be read and do not
225 * remove message from deletion list
228 dev_dbg(&dev
->pdev
->dev
, "amthif cb->response_buffer size - %d\n",
229 cb
->response_buffer
.size
);
230 dev_dbg(&dev
->pdev
->dev
, "amthif cb->buf_idx - %lu\n", cb
->buf_idx
);
232 /* length is being truncated to PAGE_SIZE, however,
233 * the buf_idx may point beyond */
234 length
= min_t(size_t, length
, (cb
->buf_idx
- *offset
));
236 if (copy_to_user(ubuf
, cb
->response_buffer
.data
+ *offset
, length
)) {
237 dev_dbg(&dev
->pdev
->dev
, "failed to copy data to userland\n");
241 if ((*offset
+ length
) < cb
->buf_idx
) {
247 dev_dbg(&dev
->pdev
->dev
, "free amthif cb memory.\n");
255 * mei_amthif_send_cmd - send amthif command to the ME
257 * @dev: the device structure
258 * @cb: mei call back struct
260 * returns 0 on success, <0 on failure.
263 static int mei_amthif_send_cmd(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
265 struct mei_msg_hdr mei_hdr
;
271 dev_dbg(&dev
->pdev
->dev
, "write data to amthif client.\n");
273 dev
->iamthif_state
= MEI_IAMTHIF_WRITING
;
274 dev
->iamthif_current_cb
= cb
;
275 dev
->iamthif_file_object
= cb
->file_object
;
276 dev
->iamthif_canceled
= false;
277 dev
->iamthif_ioctl
= true;
278 dev
->iamthif_msg_buf_size
= cb
->request_buffer
.size
;
279 memcpy(dev
->iamthif_msg_buf
, cb
->request_buffer
.data
,
280 cb
->request_buffer
.size
);
282 ret
= mei_cl_flow_ctrl_creds(&dev
->iamthif_cl
);
286 if (ret
&& mei_hbuf_acquire(dev
)) {
288 if (cb
->request_buffer
.size
> mei_hbuf_max_len(dev
)) {
289 mei_hdr
.length
= mei_hbuf_max_len(dev
);
290 mei_hdr
.msg_complete
= 0;
292 mei_hdr
.length
= cb
->request_buffer
.size
;
293 mei_hdr
.msg_complete
= 1;
296 mei_hdr
.host_addr
= dev
->iamthif_cl
.host_client_id
;
297 mei_hdr
.me_addr
= dev
->iamthif_cl
.me_client_id
;
298 mei_hdr
.reserved
= 0;
299 mei_hdr
.internal
= 0;
300 dev
->iamthif_msg_buf_index
+= mei_hdr
.length
;
301 ret
= mei_write_message(dev
, &mei_hdr
, dev
->iamthif_msg_buf
);
305 if (mei_hdr
.msg_complete
) {
306 if (mei_cl_flow_ctrl_reduce(&dev
->iamthif_cl
))
308 dev
->iamthif_flow_control_pending
= true;
309 dev
->iamthif_state
= MEI_IAMTHIF_FLOW_CONTROL
;
310 dev_dbg(&dev
->pdev
->dev
, "add amthif cb to write waiting list\n");
311 dev
->iamthif_current_cb
= cb
;
312 dev
->iamthif_file_object
= cb
->file_object
;
313 list_add_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
315 dev_dbg(&dev
->pdev
->dev
, "message does not complete, so add amthif cb to write list.\n");
316 list_add_tail(&cb
->list
, &dev
->write_list
.list
);
319 list_add_tail(&cb
->list
, &dev
->write_list
.list
);
325 * mei_amthif_write - write amthif data to amthif client
327 * @dev: the device structure
328 * @cb: mei call back struct
330 * returns 0 on success, <0 on failure.
333 int mei_amthif_write(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
340 ret
= mei_io_cb_alloc_resp_buf(cb
, dev
->iamthif_mtu
);
344 cb
->fop_type
= MEI_FOP_WRITE
;
346 if (!list_empty(&dev
->amthif_cmd_list
.list
) ||
347 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
348 dev_dbg(&dev
->pdev
->dev
,
349 "amthif state = %d\n", dev
->iamthif_state
);
350 dev_dbg(&dev
->pdev
->dev
, "AMTHIF: add cb to the wait list\n");
351 list_add_tail(&cb
->list
, &dev
->amthif_cmd_list
.list
);
354 return mei_amthif_send_cmd(dev
, cb
);
357 * mei_amthif_run_next_cmd
359 * @dev: the device structure
361 * returns 0 on success, <0 on failure.
363 void mei_amthif_run_next_cmd(struct mei_device
*dev
)
365 struct mei_cl_cb
*cb
;
366 struct mei_cl_cb
*next
;
372 dev
->iamthif_msg_buf_size
= 0;
373 dev
->iamthif_msg_buf_index
= 0;
374 dev
->iamthif_canceled
= false;
375 dev
->iamthif_ioctl
= true;
376 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
377 dev
->iamthif_timer
= 0;
378 dev
->iamthif_file_object
= NULL
;
380 dev_dbg(&dev
->pdev
->dev
, "complete amthif cmd_list cb.\n");
382 list_for_each_entry_safe(cb
, next
, &dev
->amthif_cmd_list
.list
, list
) {
386 status
= mei_amthif_send_cmd(dev
, cb
);
388 dev_warn(&dev
->pdev
->dev
, "amthif write failed status = %d\n",
395 unsigned int mei_amthif_poll(struct mei_device
*dev
,
396 struct file
*file
, poll_table
*wait
)
398 unsigned int mask
= 0;
400 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
402 mutex_lock(&dev
->device_lock
);
403 if (!mei_cl_is_connected(&dev
->iamthif_cl
)) {
407 } else if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
408 dev
->iamthif_file_object
== file
) {
410 mask
|= (POLLIN
| POLLRDNORM
);
411 dev_dbg(&dev
->pdev
->dev
, "run next amthif cb\n");
412 mei_amthif_run_next_cmd(dev
);
414 mutex_unlock(&dev
->device_lock
);
422 * mei_amthif_irq_write - write iamthif command in irq thread context.
424 * @dev: the device structure.
425 * @cb_pos: callback block.
426 * @cl: private data of the file object.
427 * @cmpl_list: complete list.
429 * returns 0, OK; otherwise, error.
431 int mei_amthif_irq_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
432 struct mei_cl_cb
*cmpl_list
)
434 struct mei_device
*dev
= cl
->dev
;
435 struct mei_msg_hdr mei_hdr
;
436 size_t len
= dev
->iamthif_msg_buf_size
- dev
->iamthif_msg_buf_index
;
437 u32 msg_slots
= mei_data2slots(len
);
441 rets
= mei_cl_flow_ctrl_creds(cl
);
446 cl_dbg(dev
, cl
, "No flow control credentials: not sending.\n");
450 mei_hdr
.host_addr
= cl
->host_client_id
;
451 mei_hdr
.me_addr
= cl
->me_client_id
;
452 mei_hdr
.reserved
= 0;
453 mei_hdr
.internal
= 0;
455 slots
= mei_hbuf_empty_slots(dev
);
457 if (slots
>= msg_slots
) {
458 mei_hdr
.length
= len
;
459 mei_hdr
.msg_complete
= 1;
460 /* Split the message only if we can write the whole host buffer */
461 } else if (slots
== dev
->hbuf_depth
) {
463 len
= (slots
* sizeof(u32
)) - sizeof(struct mei_msg_hdr
);
464 mei_hdr
.length
= len
;
465 mei_hdr
.msg_complete
= 0;
467 /* wait for next time the host buffer is empty */
471 dev_dbg(&dev
->pdev
->dev
, MEI_HDR_FMT
, MEI_HDR_PRM(&mei_hdr
));
473 rets
= mei_write_message(dev
, &mei_hdr
,
474 dev
->iamthif_msg_buf
+ dev
->iamthif_msg_buf_index
);
476 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
482 if (mei_cl_flow_ctrl_reduce(cl
))
485 dev
->iamthif_msg_buf_index
+= mei_hdr
.length
;
488 if (mei_hdr
.msg_complete
) {
489 dev
->iamthif_state
= MEI_IAMTHIF_FLOW_CONTROL
;
490 dev
->iamthif_flow_control_pending
= true;
492 /* save iamthif cb sent to amthif client */
493 cb
->buf_idx
= dev
->iamthif_msg_buf_index
;
494 dev
->iamthif_current_cb
= cb
;
496 list_move_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
504 * mei_amthif_irq_read_message - read routine after ISR to
505 * handle the read amthif message
507 * @dev: the device structure
508 * @mei_hdr: header of amthif message
509 * @complete_list: An instance of our list structure
511 * returns 0 on success, <0 on failure.
513 int mei_amthif_irq_read_msg(struct mei_device
*dev
,
514 struct mei_msg_hdr
*mei_hdr
,
515 struct mei_cl_cb
*complete_list
)
517 struct mei_cl_cb
*cb
;
518 unsigned char *buffer
;
520 BUG_ON(mei_hdr
->me_addr
!= dev
->iamthif_cl
.me_client_id
);
521 BUG_ON(dev
->iamthif_state
!= MEI_IAMTHIF_READING
);
523 buffer
= dev
->iamthif_msg_buf
+ dev
->iamthif_msg_buf_index
;
524 BUG_ON(dev
->iamthif_mtu
< dev
->iamthif_msg_buf_index
+ mei_hdr
->length
);
526 mei_read_slots(dev
, buffer
, mei_hdr
->length
);
528 dev
->iamthif_msg_buf_index
+= mei_hdr
->length
;
530 if (!mei_hdr
->msg_complete
)
533 dev_dbg(&dev
->pdev
->dev
, "amthif_message_buffer_index =%d\n",
536 dev_dbg(&dev
->pdev
->dev
, "completed amthif read.\n ");
537 if (!dev
->iamthif_current_cb
)
540 cb
= dev
->iamthif_current_cb
;
541 dev
->iamthif_current_cb
= NULL
;
546 dev
->iamthif_stall_timer
= 0;
547 cb
->buf_idx
= dev
->iamthif_msg_buf_index
;
548 cb
->read_time
= jiffies
;
549 if (dev
->iamthif_ioctl
) {
550 /* found the iamthif cb */
551 dev_dbg(&dev
->pdev
->dev
, "complete the amthif read cb.\n ");
552 dev_dbg(&dev
->pdev
->dev
, "add the amthif read cb to complete.\n ");
553 list_add_tail(&cb
->list
, &complete_list
->list
);
559 * mei_amthif_irq_read - prepares to read amthif data.
561 * @dev: the device structure.
562 * @slots: free slots.
564 * returns 0, OK; otherwise, error.
566 int mei_amthif_irq_read(struct mei_device
*dev
, s32
*slots
)
568 u32 msg_slots
= mei_data2slots(sizeof(struct hbm_flow_control
));
570 if (*slots
< msg_slots
)
575 if (mei_hbm_cl_flow_control_req(dev
, &dev
->iamthif_cl
)) {
576 dev_dbg(&dev
->pdev
->dev
, "iamthif flow control failed\n");
580 dev_dbg(&dev
->pdev
->dev
, "iamthif flow control success\n");
581 dev
->iamthif_state
= MEI_IAMTHIF_READING
;
582 dev
->iamthif_flow_control_pending
= false;
583 dev
->iamthif_msg_buf_index
= 0;
584 dev
->iamthif_msg_buf_size
= 0;
585 dev
->iamthif_stall_timer
= MEI_IAMTHIF_STALL_TIMER
;
586 dev
->hbuf_is_ready
= mei_hbuf_is_ready(dev
);
591 * mei_amthif_complete - complete amthif callback.
593 * @dev: the device structure.
594 * @cb_pos: callback block.
596 void mei_amthif_complete(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
598 if (dev
->iamthif_canceled
!= 1) {
599 dev
->iamthif_state
= MEI_IAMTHIF_READ_COMPLETE
;
600 dev
->iamthif_stall_timer
= 0;
601 memcpy(cb
->response_buffer
.data
,
602 dev
->iamthif_msg_buf
,
603 dev
->iamthif_msg_buf_index
);
604 list_add_tail(&cb
->list
, &dev
->amthif_rd_complete_list
.list
);
605 dev_dbg(&dev
->pdev
->dev
, "amthif read completed\n");
606 dev
->iamthif_timer
= jiffies
;
607 dev_dbg(&dev
->pdev
->dev
, "dev->iamthif_timer = %ld\n",
610 mei_amthif_run_next_cmd(dev
);
613 dev_dbg(&dev
->pdev
->dev
, "completing amthif call back.\n");
614 wake_up_interruptible(&dev
->iamthif_cl
.wait
);
618 * mei_clear_list - removes all callbacks associated with file
621 * @dev: device structure.
622 * @file: file structure
623 * @mei_cb_list: callbacks list
625 * mei_clear_list is called to clear resources associated with file
626 * when application calls close function or Ctrl-C was pressed
628 * returns true if callback removed from the list, false otherwise
630 static bool mei_clear_list(struct mei_device
*dev
,
631 const struct file
*file
, struct list_head
*mei_cb_list
)
633 struct mei_cl_cb
*cb_pos
= NULL
;
634 struct mei_cl_cb
*cb_next
= NULL
;
635 bool removed
= false;
637 /* list all list member */
638 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, list
) {
639 /* check if list member associated with a file */
640 if (file
== cb_pos
->file_object
) {
641 /* remove member from the list */
642 list_del(&cb_pos
->list
);
643 /* check if cb equal to current iamthif cb */
644 if (dev
->iamthif_current_cb
== cb_pos
) {
645 dev
->iamthif_current_cb
= NULL
;
646 /* send flow control to iamthif client */
647 mei_hbm_cl_flow_control_req(dev
,
650 /* free all allocated buffers */
651 mei_io_cb_free(cb_pos
);
660 * mei_clear_lists - removes all callbacks associated with file
662 * @dev: device structure
663 * @file: file structure
665 * mei_clear_lists is called to clear resources associated with file
666 * when application calls close function or Ctrl-C was pressed
668 * returns true if callback removed from the list, false otherwise
670 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
672 bool removed
= false;
674 /* remove callbacks associated with a file */
675 mei_clear_list(dev
, file
, &dev
->amthif_cmd_list
.list
);
676 if (mei_clear_list(dev
, file
, &dev
->amthif_rd_complete_list
.list
))
679 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.list
);
681 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.list
))
684 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.list
))
687 if (mei_clear_list(dev
, file
, &dev
->write_list
.list
))
690 /* check if iamthif_current_cb not NULL */
691 if (dev
->iamthif_current_cb
&& !removed
) {
692 /* check file and iamthif current cb association */
693 if (dev
->iamthif_current_cb
->file_object
== file
) {
695 mei_io_cb_free(dev
->iamthif_current_cb
);
696 dev
->iamthif_current_cb
= NULL
;
704 * mei_amthif_release - the release function
706 * @dev: device structure
707 * @file: pointer to file structure
709 * returns 0 on success, <0 on error
711 int mei_amthif_release(struct mei_device
*dev
, struct file
*file
)
713 if (dev
->iamthif_open_count
> 0)
714 dev
->iamthif_open_count
--;
716 if (dev
->iamthif_file_object
== file
&&
717 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
719 dev_dbg(&dev
->pdev
->dev
, "amthif canceled iamthif state %d\n",
721 dev
->iamthif_canceled
= true;
722 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
723 dev_dbg(&dev
->pdev
->dev
, "run next amthif iamthif cb\n");
724 mei_amthif_run_next_cmd(dev
);
728 if (mei_clear_lists(dev
, file
))
729 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;