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/init.h>
25 #include <linux/ioctl.h>
26 #include <linux/cdev.h>
27 #include <linux/list.h>
28 #include <linux/delay.h>
29 #include <linux/sched.h>
30 #include <linux/uuid.h>
31 #include <linux/jiffies.h>
32 #include <linux/uaccess.h>
34 #include <linux/mei.h>
41 const uuid_le mei_amthif_guid
= UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
42 0xac, 0xa8, 0x46, 0xe0,
43 0xff, 0x65, 0x81, 0x4c);
46 * mei_amthif_reset_params - initializes mei device iamthif
48 * @dev: the device structure
50 void mei_amthif_reset_params(struct mei_device
*dev
)
52 /* reset iamthif parameters. */
53 dev
->iamthif_current_cb
= NULL
;
54 dev
->iamthif_msg_buf_size
= 0;
55 dev
->iamthif_msg_buf_index
= 0;
56 dev
->iamthif_canceled
= false;
57 dev
->iamthif_ioctl
= false;
58 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
59 dev
->iamthif_timer
= 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
, "amthif: failed to find the client\n");
84 cl
->me_client_id
= dev
->me_clients
[i
].client_id
;
86 /* Assign iamthif_mtu to the value received from ME */
88 dev
->iamthif_mtu
= dev
->me_clients
[i
].props
.max_msg_length
;
89 dev_dbg(&dev
->pdev
->dev
, "IAMTHIF_MTU = %d\n",
90 dev
->me_clients
[i
].props
.max_msg_length
);
92 kfree(dev
->iamthif_msg_buf
);
93 dev
->iamthif_msg_buf
= NULL
;
95 /* allocate storage for ME message buffer */
96 msg_buf
= kcalloc(dev
->iamthif_mtu
,
97 sizeof(unsigned char), GFP_KERNEL
);
99 dev_err(&dev
->pdev
->dev
, "amthif: memory allocation for ME message buffer failed.\n");
103 dev
->iamthif_msg_buf
= msg_buf
;
105 ret
= mei_cl_link(cl
, MEI_IAMTHIF_HOST_CLIENT_ID
);
108 dev_err(&dev
->pdev
->dev
, "amthif: failed link client\n");
112 cl
->state
= MEI_FILE_CONNECTING
;
114 if (mei_hbm_cl_connect_req(dev
, cl
)) {
115 dev_dbg(&dev
->pdev
->dev
, "amthif: Failed to connect to ME client\n");
116 cl
->state
= MEI_FILE_DISCONNECTED
;
117 cl
->host_client_id
= 0;
119 cl
->timer_count
= MEI_CONNECT_TIMEOUT
;
125 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
127 * @dev: the device structure
128 * @file: pointer to file object
130 * returns returned a list entry on success, NULL on failure.
132 struct mei_cl_cb
*mei_amthif_find_read_list_entry(struct mei_device
*dev
,
135 struct mei_cl_cb
*pos
= NULL
;
136 struct mei_cl_cb
*next
= NULL
;
138 list_for_each_entry_safe(pos
, next
,
139 &dev
->amthif_rd_complete_list
.list
, list
) {
140 if (pos
->cl
&& pos
->cl
== &dev
->iamthif_cl
&&
141 pos
->file_object
== file
)
149 * mei_amthif_read - read data from AMTHIF client
151 * @dev: the device structure
152 * @if_num: minor number
153 * @file: pointer to file object
154 * @*ubuf: pointer to user data in user space
155 * @length: data length to read
156 * @offset: data read offset
158 * Locking: called under "dev->device_lock" lock
161 * returned data length on success,
162 * zero if no data to read,
163 * negative on failure.
165 int mei_amthif_read(struct mei_device
*dev
, struct file
*file
,
166 char __user
*ubuf
, size_t length
, loff_t
*offset
)
170 struct mei_cl_cb
*cb
= NULL
;
171 struct mei_cl
*cl
= file
->private_data
;
172 unsigned long timeout
;
175 /* Only Posible if we are in timeout */
176 if (!cl
|| cl
!= &dev
->iamthif_cl
) {
177 dev_dbg(&dev
->pdev
->dev
, "bad file ext.\n");
181 i
= mei_me_cl_by_id(dev
, dev
->iamthif_cl
.me_client_id
);
184 dev_dbg(&dev
->pdev
->dev
, "amthif client not found.\n");
187 dev_dbg(&dev
->pdev
->dev
, "checking amthif data\n");
188 cb
= mei_amthif_find_read_list_entry(dev
, file
);
190 /* Check for if we can block or not*/
191 if (cb
== NULL
&& file
->f_flags
& O_NONBLOCK
)
195 dev_dbg(&dev
->pdev
->dev
, "waiting for amthif data\n");
197 /* unlock the Mutex */
198 mutex_unlock(&dev
->device_lock
);
200 wait_ret
= wait_event_interruptible(dev
->iamthif_cl
.wait
,
201 (cb
= mei_amthif_find_read_list_entry(dev
, file
)));
203 /* Locking again the Mutex */
204 mutex_lock(&dev
->device_lock
);
209 dev_dbg(&dev
->pdev
->dev
, "woke up from sleep\n");
213 dev_dbg(&dev
->pdev
->dev
, "Got amthif data\n");
214 dev
->iamthif_timer
= 0;
217 timeout
= cb
->read_time
+
218 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER
);
219 dev_dbg(&dev
->pdev
->dev
, "amthif timeout = %lud\n",
222 if (time_after(jiffies
, timeout
)) {
223 dev_dbg(&dev
->pdev
->dev
, "amthif Time out\n");
224 /* 15 sec for the message has expired */
230 /* if the whole message will fit remove it from the list */
231 if (cb
->buf_idx
>= *offset
&& length
>= (cb
->buf_idx
- *offset
))
233 else if (cb
->buf_idx
> 0 && cb
->buf_idx
<= *offset
) {
234 /* end of the message has been reached */
239 /* else means that not full buffer will be read and do not
240 * remove message from deletion list
243 dev_dbg(&dev
->pdev
->dev
, "amthif cb->response_buffer size - %d\n",
244 cb
->response_buffer
.size
);
245 dev_dbg(&dev
->pdev
->dev
, "amthif cb->buf_idx - %lu\n", cb
->buf_idx
);
247 /* length is being turncated to PAGE_SIZE, however,
248 * the buf_idx may point beyond */
249 length
= min_t(size_t, length
, (cb
->buf_idx
- *offset
));
251 if (copy_to_user(ubuf
, cb
->response_buffer
.data
+ *offset
, length
))
255 if ((*offset
+ length
) < cb
->buf_idx
) {
261 dev_dbg(&dev
->pdev
->dev
, "free amthif cb memory.\n");
269 * mei_amthif_send_cmd - send amthif command to the ME
271 * @dev: the device structure
272 * @cb: mei call back struct
274 * returns 0 on success, <0 on failure.
277 static int mei_amthif_send_cmd(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
279 struct mei_msg_hdr mei_hdr
;
285 dev_dbg(&dev
->pdev
->dev
, "write data to amthif client.\n");
287 dev
->iamthif_state
= MEI_IAMTHIF_WRITING
;
288 dev
->iamthif_current_cb
= cb
;
289 dev
->iamthif_file_object
= cb
->file_object
;
290 dev
->iamthif_canceled
= false;
291 dev
->iamthif_ioctl
= true;
292 dev
->iamthif_msg_buf_size
= cb
->request_buffer
.size
;
293 memcpy(dev
->iamthif_msg_buf
, cb
->request_buffer
.data
,
294 cb
->request_buffer
.size
);
296 ret
= mei_cl_flow_ctrl_creds(&dev
->iamthif_cl
);
300 if (ret
&& dev
->hbuf_is_ready
) {
302 dev
->hbuf_is_ready
= false;
303 if (cb
->request_buffer
.size
> mei_hbuf_max_len(dev
)) {
304 mei_hdr
.length
= mei_hbuf_max_len(dev
);
305 mei_hdr
.msg_complete
= 0;
307 mei_hdr
.length
= cb
->request_buffer
.size
;
308 mei_hdr
.msg_complete
= 1;
311 mei_hdr
.host_addr
= dev
->iamthif_cl
.host_client_id
;
312 mei_hdr
.me_addr
= dev
->iamthif_cl
.me_client_id
;
313 mei_hdr
.reserved
= 0;
314 dev
->iamthif_msg_buf_index
+= mei_hdr
.length
;
315 if (mei_write_message(dev
, &mei_hdr
,
316 (unsigned char *)dev
->iamthif_msg_buf
))
319 if (mei_hdr
.msg_complete
) {
320 if (mei_cl_flow_ctrl_reduce(&dev
->iamthif_cl
))
322 dev
->iamthif_flow_control_pending
= true;
323 dev
->iamthif_state
= MEI_IAMTHIF_FLOW_CONTROL
;
324 dev_dbg(&dev
->pdev
->dev
, "add amthif cb to write waiting list\n");
325 dev
->iamthif_current_cb
= cb
;
326 dev
->iamthif_file_object
= cb
->file_object
;
327 list_add_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
329 dev_dbg(&dev
->pdev
->dev
, "message does not complete, so add amthif cb to write list.\n");
330 list_add_tail(&cb
->list
, &dev
->write_list
.list
);
333 if (!dev
->hbuf_is_ready
)
334 dev_dbg(&dev
->pdev
->dev
, "host buffer is not empty");
336 dev_dbg(&dev
->pdev
->dev
, "No flow control credentials, so add iamthif cb to write list.\n");
337 list_add_tail(&cb
->list
, &dev
->write_list
.list
);
343 * mei_amthif_write - write amthif data to amthif client
345 * @dev: the device structure
346 * @cb: mei call back struct
348 * returns 0 on success, <0 on failure.
351 int mei_amthif_write(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
358 ret
= mei_io_cb_alloc_resp_buf(cb
, dev
->iamthif_mtu
);
362 cb
->fop_type
= MEI_FOP_IOCTL
;
364 if (!list_empty(&dev
->amthif_cmd_list
.list
) ||
365 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
366 dev_dbg(&dev
->pdev
->dev
,
367 "amthif state = %d\n", dev
->iamthif_state
);
368 dev_dbg(&dev
->pdev
->dev
, "AMTHIF: add cb to the wait list\n");
369 list_add_tail(&cb
->list
, &dev
->amthif_cmd_list
.list
);
372 return mei_amthif_send_cmd(dev
, cb
);
375 * mei_amthif_run_next_cmd
377 * @dev: the device structure
379 * returns 0 on success, <0 on failure.
381 void mei_amthif_run_next_cmd(struct mei_device
*dev
)
383 struct mei_cl_cb
*pos
= NULL
;
384 struct mei_cl_cb
*next
= NULL
;
390 dev
->iamthif_msg_buf_size
= 0;
391 dev
->iamthif_msg_buf_index
= 0;
392 dev
->iamthif_canceled
= false;
393 dev
->iamthif_ioctl
= true;
394 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
395 dev
->iamthif_timer
= 0;
396 dev
->iamthif_file_object
= NULL
;
398 dev_dbg(&dev
->pdev
->dev
, "complete amthif cmd_list cb.\n");
400 list_for_each_entry_safe(pos
, next
, &dev
->amthif_cmd_list
.list
, list
) {
401 list_del(&pos
->list
);
403 if (pos
->cl
&& pos
->cl
== &dev
->iamthif_cl
) {
404 status
= mei_amthif_send_cmd(dev
, pos
);
406 dev_dbg(&dev
->pdev
->dev
,
407 "amthif write failed status = %d\n",
417 unsigned int mei_amthif_poll(struct mei_device
*dev
,
418 struct file
*file
, poll_table
*wait
)
420 unsigned int mask
= 0;
422 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
424 mutex_lock(&dev
->device_lock
);
425 if (!mei_cl_is_connected(&dev
->iamthif_cl
)) {
429 } else if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
430 dev
->iamthif_file_object
== file
) {
432 mask
|= (POLLIN
| POLLRDNORM
);
433 dev_dbg(&dev
->pdev
->dev
, "run next amthif cb\n");
434 mei_amthif_run_next_cmd(dev
);
436 mutex_unlock(&dev
->device_lock
);
444 * mei_amthif_irq_write_completed - processes completed iamthif operation.
446 * @dev: the device structure.
447 * @slots: free slots.
448 * @cb_pos: callback block.
449 * @cl: private data of the file object.
450 * @cmpl_list: complete list.
452 * returns 0, OK; otherwise, error.
454 int mei_amthif_irq_write_complete(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
455 s32
*slots
, struct mei_cl_cb
*cmpl_list
)
457 struct mei_device
*dev
= cl
->dev
;
458 struct mei_msg_hdr mei_hdr
;
459 size_t len
= dev
->iamthif_msg_buf_size
- dev
->iamthif_msg_buf_index
;
460 u32 msg_slots
= mei_data2slots(len
);
462 mei_hdr
.host_addr
= cl
->host_client_id
;
463 mei_hdr
.me_addr
= cl
->me_client_id
;
464 mei_hdr
.reserved
= 0;
466 if (*slots
>= msg_slots
) {
467 mei_hdr
.length
= len
;
468 mei_hdr
.msg_complete
= 1;
469 /* Split the message only if we can write the whole host buffer */
470 } else if (*slots
== dev
->hbuf_depth
) {
472 len
= (*slots
* sizeof(u32
)) - sizeof(struct mei_msg_hdr
);
473 mei_hdr
.length
= len
;
474 mei_hdr
.msg_complete
= 0;
476 /* wait for next time the host buffer is empty */
480 dev_dbg(&dev
->pdev
->dev
, MEI_HDR_FMT
, MEI_HDR_PRM(&mei_hdr
));
483 if (mei_write_message(dev
, &mei_hdr
,
484 dev
->iamthif_msg_buf
+ dev
->iamthif_msg_buf_index
)) {
485 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
486 cl
->status
= -ENODEV
;
491 if (mei_cl_flow_ctrl_reduce(cl
))
494 dev
->iamthif_msg_buf_index
+= mei_hdr
.length
;
497 if (mei_hdr
.msg_complete
) {
498 dev
->iamthif_state
= MEI_IAMTHIF_FLOW_CONTROL
;
499 dev
->iamthif_flow_control_pending
= true;
501 /* save iamthif cb sent to amthif client */
502 cb
->buf_idx
= dev
->iamthif_msg_buf_index
;
503 dev
->iamthif_current_cb
= cb
;
505 list_move_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
513 * mei_amthif_irq_read_message - read routine after ISR to
514 * handle the read amthif message
516 * @dev: the device structure
517 * @mei_hdr: header of amthif message
518 * @complete_list: An instance of our list structure
520 * returns 0 on success, <0 on failure.
522 int mei_amthif_irq_read_msg(struct mei_device
*dev
,
523 struct mei_msg_hdr
*mei_hdr
,
524 struct mei_cl_cb
*complete_list
)
526 struct mei_cl_cb
*cb
;
527 unsigned char *buffer
;
529 BUG_ON(mei_hdr
->me_addr
!= dev
->iamthif_cl
.me_client_id
);
530 BUG_ON(dev
->iamthif_state
!= MEI_IAMTHIF_READING
);
532 buffer
= dev
->iamthif_msg_buf
+ dev
->iamthif_msg_buf_index
;
533 BUG_ON(dev
->iamthif_mtu
< dev
->iamthif_msg_buf_index
+ mei_hdr
->length
);
535 mei_read_slots(dev
, buffer
, mei_hdr
->length
);
537 dev
->iamthif_msg_buf_index
+= mei_hdr
->length
;
539 if (!mei_hdr
->msg_complete
)
542 dev_dbg(&dev
->pdev
->dev
, "amthif_message_buffer_index =%d\n",
545 dev_dbg(&dev
->pdev
->dev
, "completed amthif read.\n ");
546 if (!dev
->iamthif_current_cb
)
549 cb
= dev
->iamthif_current_cb
;
550 dev
->iamthif_current_cb
= NULL
;
555 dev
->iamthif_stall_timer
= 0;
556 cb
->buf_idx
= dev
->iamthif_msg_buf_index
;
557 cb
->read_time
= jiffies
;
558 if (dev
->iamthif_ioctl
&& cb
->cl
== &dev
->iamthif_cl
) {
559 /* found the iamthif cb */
560 dev_dbg(&dev
->pdev
->dev
, "complete the amthif read cb.\n ");
561 dev_dbg(&dev
->pdev
->dev
, "add the amthif read cb to complete.\n ");
562 list_add_tail(&cb
->list
, &complete_list
->list
);
568 * mei_amthif_irq_read - prepares to read amthif data.
570 * @dev: the device structure.
571 * @slots: free slots.
573 * returns 0, OK; otherwise, error.
575 int mei_amthif_irq_read(struct mei_device
*dev
, s32
*slots
)
577 u32 msg_slots
= mei_data2slots(sizeof(struct hbm_flow_control
));
579 if (*slots
< msg_slots
)
584 if (mei_hbm_cl_flow_control_req(dev
, &dev
->iamthif_cl
)) {
585 dev_dbg(&dev
->pdev
->dev
, "iamthif flow control failed\n");
589 dev_dbg(&dev
->pdev
->dev
, "iamthif flow control success\n");
590 dev
->iamthif_state
= MEI_IAMTHIF_READING
;
591 dev
->iamthif_flow_control_pending
= false;
592 dev
->iamthif_msg_buf_index
= 0;
593 dev
->iamthif_msg_buf_size
= 0;
594 dev
->iamthif_stall_timer
= MEI_IAMTHIF_STALL_TIMER
;
595 dev
->hbuf_is_ready
= mei_hbuf_is_ready(dev
);
600 * mei_amthif_complete - complete amthif callback.
602 * @dev: the device structure.
603 * @cb_pos: callback block.
605 void mei_amthif_complete(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
607 if (dev
->iamthif_canceled
!= 1) {
608 dev
->iamthif_state
= MEI_IAMTHIF_READ_COMPLETE
;
609 dev
->iamthif_stall_timer
= 0;
610 memcpy(cb
->response_buffer
.data
,
611 dev
->iamthif_msg_buf
,
612 dev
->iamthif_msg_buf_index
);
613 list_add_tail(&cb
->list
, &dev
->amthif_rd_complete_list
.list
);
614 dev_dbg(&dev
->pdev
->dev
, "amthif read completed\n");
615 dev
->iamthif_timer
= jiffies
;
616 dev_dbg(&dev
->pdev
->dev
, "dev->iamthif_timer = %ld\n",
619 mei_amthif_run_next_cmd(dev
);
622 dev_dbg(&dev
->pdev
->dev
, "completing amthif call back.\n");
623 wake_up_interruptible(&dev
->iamthif_cl
.wait
);
627 * mei_clear_list - removes all callbacks associated with file
630 * @dev: device structure.
631 * @file: file structure
632 * @mei_cb_list: callbacks list
634 * mei_clear_list is called to clear resources associated with file
635 * when application calls close function or Ctrl-C was pressed
637 * returns true if callback removed from the list, false otherwise
639 static bool mei_clear_list(struct mei_device
*dev
,
640 const struct file
*file
, struct list_head
*mei_cb_list
)
642 struct mei_cl_cb
*cb_pos
= NULL
;
643 struct mei_cl_cb
*cb_next
= NULL
;
644 bool removed
= false;
646 /* list all list member */
647 list_for_each_entry_safe(cb_pos
, cb_next
, mei_cb_list
, list
) {
648 /* check if list member associated with a file */
649 if (file
== cb_pos
->file_object
) {
650 /* remove member from the list */
651 list_del(&cb_pos
->list
);
652 /* check if cb equal to current iamthif cb */
653 if (dev
->iamthif_current_cb
== cb_pos
) {
654 dev
->iamthif_current_cb
= NULL
;
655 /* send flow control to iamthif client */
656 mei_hbm_cl_flow_control_req(dev
,
659 /* free all allocated buffers */
660 mei_io_cb_free(cb_pos
);
669 * mei_clear_lists - removes all callbacks associated with file
671 * @dev: device structure
672 * @file: file structure
674 * mei_clear_lists is called to clear resources associated with file
675 * when application calls close function or Ctrl-C was pressed
677 * returns true if callback removed from the list, false otherwise
679 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
681 bool removed
= false;
683 /* remove callbacks associated with a file */
684 mei_clear_list(dev
, file
, &dev
->amthif_cmd_list
.list
);
685 if (mei_clear_list(dev
, file
, &dev
->amthif_rd_complete_list
.list
))
688 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.list
);
690 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.list
))
693 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.list
))
696 if (mei_clear_list(dev
, file
, &dev
->write_list
.list
))
699 /* check if iamthif_current_cb not NULL */
700 if (dev
->iamthif_current_cb
&& !removed
) {
701 /* check file and iamthif current cb association */
702 if (dev
->iamthif_current_cb
->file_object
== file
) {
704 mei_io_cb_free(dev
->iamthif_current_cb
);
705 dev
->iamthif_current_cb
= NULL
;
713 * mei_amthif_release - the release function
715 * @dev: device structure
716 * @file: pointer to file structure
718 * returns 0 on success, <0 on error
720 int mei_amthif_release(struct mei_device
*dev
, struct file
*file
)
722 if (dev
->open_handle_count
> 0)
723 dev
->open_handle_count
--;
725 if (dev
->iamthif_file_object
== file
&&
726 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
728 dev_dbg(&dev
->pdev
->dev
, "amthif canceled iamthif state %d\n",
730 dev
->iamthif_canceled
= true;
731 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
732 dev_dbg(&dev
->pdev
->dev
, "run next amthif iamthif cb\n");
733 mei_amthif_run_next_cmd(dev
);
737 if (mei_clear_lists(dev
, file
))
738 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;