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/ioctl.h>
23 #include <linux/cdev.h>
24 #include <linux/list.h>
25 #include <linux/delay.h>
26 #include <linux/sched.h>
27 #include <linux/uuid.h>
28 #include <linux/jiffies.h>
29 #include <linux/uaccess.h>
30 #include <linux/slab.h>
32 #include <linux/mei.h>
38 const uuid_le mei_amthif_guid
= UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
39 0xac, 0xa8, 0x46, 0xe0,
40 0xff, 0x65, 0x81, 0x4c);
43 * mei_amthif_reset_params - initializes mei device iamthif
45 * @dev: the device structure
47 void mei_amthif_reset_params(struct mei_device
*dev
)
49 /* reset iamthif parameters. */
50 dev
->iamthif_current_cb
= NULL
;
51 dev
->iamthif_canceled
= false;
52 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
53 dev
->iamthif_timer
= 0;
54 dev
->iamthif_stall_timer
= 0;
55 dev
->iamthif_open_count
= 0;
59 * mei_amthif_host_init - mei initialization amthif client.
61 * @dev: the device structure
63 * Return: 0 on success, <0 on failure.
65 int mei_amthif_host_init(struct mei_device
*dev
)
67 struct mei_cl
*cl
= &dev
->iamthif_cl
;
68 struct mei_me_client
*me_cl
;
71 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
75 me_cl
= mei_me_cl_by_uuid(dev
, &mei_amthif_guid
);
77 dev_info(dev
->dev
, "amthif: failed to find the client");
81 cl
->me_client_id
= me_cl
->client_id
;
82 cl
->cl_uuid
= me_cl
->props
.protocol_name
;
84 /* Assign iamthif_mtu to the value received from ME */
86 dev
->iamthif_mtu
= me_cl
->props
.max_msg_length
;
87 dev_dbg(dev
->dev
, "IAMTHIF_MTU = %d\n", dev
->iamthif_mtu
);
90 ret
= mei_cl_link(cl
, MEI_IAMTHIF_HOST_CLIENT_ID
);
92 dev_err(dev
->dev
, "amthif: failed cl_link %d\n", ret
);
96 ret
= mei_cl_connect(cl
, NULL
);
98 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
101 mei_me_cl_put(me_cl
);
106 * mei_amthif_find_read_list_entry - finds a amthilist entry for current file
108 * @dev: the device structure
109 * @file: pointer to file object
111 * Return: returned a list entry on success, NULL on failure.
113 struct mei_cl_cb
*mei_amthif_find_read_list_entry(struct mei_device
*dev
,
116 struct mei_cl_cb
*cb
;
118 list_for_each_entry(cb
, &dev
->amthif_rd_complete_list
.list
, list
)
119 if (cb
->file_object
== file
)
126 * mei_amthif_read - read data from AMTHIF client
128 * @dev: the device structure
129 * @file: pointer to file object
130 * @ubuf: pointer to user data in user space
131 * @length: data length to read
132 * @offset: data read offset
134 * Locking: called under "dev->device_lock" lock
137 * returned data length on success,
138 * zero if no data to read,
139 * negative on failure.
141 int mei_amthif_read(struct mei_device
*dev
, struct file
*file
,
142 char __user
*ubuf
, size_t length
, loff_t
*offset
)
144 struct mei_cl
*cl
= file
->private_data
;
145 struct mei_cl_cb
*cb
;
146 unsigned long timeout
;
150 /* Only possible if we are in timeout */
152 dev_err(dev
->dev
, "bad file ext.\n");
156 dev_dbg(dev
->dev
, "checking amthif data\n");
157 cb
= mei_amthif_find_read_list_entry(dev
, file
);
159 /* Check for if we can block or not*/
160 if (cb
== NULL
&& file
->f_flags
& O_NONBLOCK
)
164 dev_dbg(dev
->dev
, "waiting for amthif data\n");
166 /* unlock the Mutex */
167 mutex_unlock(&dev
->device_lock
);
169 wait_ret
= wait_event_interruptible(dev
->iamthif_cl
.wait
,
170 (cb
= mei_amthif_find_read_list_entry(dev
, file
)));
172 /* Locking again the Mutex */
173 mutex_lock(&dev
->device_lock
);
178 dev_dbg(dev
->dev
, "woke up from sleep\n");
183 dev_dbg(dev
->dev
, "read operation failed %d\n", rets
);
187 dev_dbg(dev
->dev
, "Got amthif data\n");
188 dev
->iamthif_timer
= 0;
190 timeout
= cb
->read_time
+
191 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER
);
192 dev_dbg(dev
->dev
, "amthif timeout = %lud\n",
195 if (time_after(jiffies
, timeout
)) {
196 dev_dbg(dev
->dev
, "amthif Time out\n");
197 /* 15 sec for the message has expired */
198 list_del_init(&cb
->list
);
202 /* if the whole message will fit remove it from the list */
203 if (cb
->buf_idx
>= *offset
&& length
>= (cb
->buf_idx
- *offset
))
204 list_del_init(&cb
->list
);
205 else if (cb
->buf_idx
> 0 && cb
->buf_idx
<= *offset
) {
206 /* end of the message has been reached */
207 list_del_init(&cb
->list
);
211 /* else means that not full buffer will be read and do not
212 * remove message from deletion list
215 dev_dbg(dev
->dev
, "amthif cb->buf size - %d\n",
217 dev_dbg(dev
->dev
, "amthif cb->buf_idx - %lu\n", cb
->buf_idx
);
219 /* length is being truncated to PAGE_SIZE, however,
220 * the buf_idx may point beyond */
221 length
= min_t(size_t, length
, (cb
->buf_idx
- *offset
));
223 if (copy_to_user(ubuf
, cb
->buf
.data
+ *offset
, length
)) {
224 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
228 if ((*offset
+ length
) < cb
->buf_idx
) {
234 dev_dbg(dev
->dev
, "free amthif cb memory.\n");
242 * mei_amthif_read_start - queue message for sending read credential
245 * @file: file pointer of message recipient
247 * Return: 0 on success, <0 on failure.
249 static int mei_amthif_read_start(struct mei_cl
*cl
, struct file
*file
)
251 struct mei_device
*dev
= cl
->dev
;
252 struct mei_cl_cb
*cb
;
253 size_t length
= dev
->iamthif_mtu
;
256 cb
= mei_io_cb_init(cl
, MEI_FOP_READ
, file
);
262 rets
= mei_io_cb_alloc_buf(cb
, length
);
266 list_add_tail(&cb
->list
, &dev
->ctrl_wr_list
.list
);
268 dev
->iamthif_state
= MEI_IAMTHIF_READING
;
269 dev
->iamthif_file_object
= cb
->file_object
;
270 dev
->iamthif_current_cb
= cb
;
279 * mei_amthif_send_cmd - send amthif command to the ME
281 * @cl: the host client
282 * @cb: mei call back struct
284 * Return: 0 on success, <0 on failure.
286 static int mei_amthif_send_cmd(struct mei_cl
*cl
, struct mei_cl_cb
*cb
)
288 struct mei_device
*dev
;
296 dev
->iamthif_state
= MEI_IAMTHIF_WRITING
;
297 dev
->iamthif_current_cb
= cb
;
298 dev
->iamthif_file_object
= cb
->file_object
;
299 dev
->iamthif_canceled
= false;
301 ret
= mei_cl_write(cl
, cb
, false);
306 cb
->status
= mei_amthif_read_start(cl
, cb
->file_object
);
312 * mei_amthif_run_next_cmd - send next amt command from queue
314 * @dev: the device structure
316 * Return: 0 on success, <0 on failure.
318 int mei_amthif_run_next_cmd(struct mei_device
*dev
)
320 struct mei_cl
*cl
= &dev
->iamthif_cl
;
321 struct mei_cl_cb
*cb
;
323 dev
->iamthif_canceled
= false;
324 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
325 dev
->iamthif_timer
= 0;
326 dev
->iamthif_file_object
= NULL
;
328 dev_dbg(dev
->dev
, "complete amthif cmd_list cb.\n");
330 cb
= list_first_entry_or_null(&dev
->amthif_cmd_list
.list
,
335 list_del_init(&cb
->list
);
336 return mei_amthif_send_cmd(cl
, cb
);
340 * mei_amthif_write - write amthif data to amthif client
343 * @cb: mei call back struct
345 * Return: 0 on success, <0 on failure.
347 int mei_amthif_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
)
350 struct mei_device
*dev
;
352 if (WARN_ON(!cl
|| !cl
->dev
))
360 list_add_tail(&cb
->list
, &dev
->amthif_cmd_list
.list
);
361 return mei_amthif_run_next_cmd(dev
);
365 * mei_amthif_poll - the amthif poll function
367 * @dev: the device structure
368 * @file: pointer to file structure
369 * @wait: pointer to poll_table structure
373 * Locking: called under "dev->device_lock" lock
376 unsigned int mei_amthif_poll(struct mei_device
*dev
,
377 struct file
*file
, poll_table
*wait
)
379 unsigned int mask
= 0;
381 poll_wait(file
, &dev
->iamthif_cl
.wait
, wait
);
383 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
384 dev
->iamthif_file_object
== file
) {
386 mask
|= POLLIN
| POLLRDNORM
;
387 mei_amthif_run_next_cmd(dev
);
396 * mei_amthif_irq_write - write iamthif command in irq thread context.
398 * @cl: private data of the file object.
399 * @cb: callback block.
400 * @cmpl_list: complete list.
402 * Return: 0, OK; otherwise, error.
404 int mei_amthif_irq_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
405 struct mei_cl_cb
*cmpl_list
)
409 ret
= mei_cl_irq_write(cl
, cb
, cmpl_list
);
414 cb
->status
= mei_amthif_read_start(cl
, cb
->file_object
);
420 * mei_amthif_irq_read_msg - read routine after ISR to
421 * handle the read amthif message
424 * @mei_hdr: header of amthif message
425 * @cmpl_list: completed callbacks list
427 * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status
429 int mei_amthif_irq_read_msg(struct mei_cl
*cl
,
430 struct mei_msg_hdr
*mei_hdr
,
431 struct mei_cl_cb
*cmpl_list
)
433 struct mei_device
*dev
;
438 if (dev
->iamthif_state
!= MEI_IAMTHIF_READING
)
441 ret
= mei_cl_irq_read_msg(cl
, mei_hdr
, cmpl_list
);
445 if (!mei_hdr
->msg_complete
)
448 dev_dbg(dev
->dev
, "completed amthif read.\n ");
449 dev
->iamthif_current_cb
= NULL
;
450 dev
->iamthif_stall_timer
= 0;
456 * mei_amthif_complete - complete amthif callback.
458 * @dev: the device structure.
459 * @cb: callback block.
461 void mei_amthif_complete(struct mei_device
*dev
, struct mei_cl_cb
*cb
)
464 if (cb
->fop_type
== MEI_FOP_WRITE
) {
466 dev
->iamthif_stall_timer
= MEI_IAMTHIF_STALL_TIMER
;
471 * in case of error enqueue the write cb to complete read list
472 * so it can be propagated to the reader
474 list_add_tail(&cb
->list
, &dev
->amthif_rd_complete_list
.list
);
475 wake_up_interruptible(&dev
->iamthif_cl
.wait
);
479 if (dev
->iamthif_canceled
!= 1) {
480 dev
->iamthif_state
= MEI_IAMTHIF_READ_COMPLETE
;
481 dev
->iamthif_stall_timer
= 0;
482 list_add_tail(&cb
->list
, &dev
->amthif_rd_complete_list
.list
);
483 dev_dbg(dev
->dev
, "amthif read completed\n");
484 dev
->iamthif_timer
= jiffies
;
485 dev_dbg(dev
->dev
, "dev->iamthif_timer = %ld\n",
488 mei_amthif_run_next_cmd(dev
);
491 dev_dbg(dev
->dev
, "completing amthif call back.\n");
492 wake_up_interruptible(&dev
->iamthif_cl
.wait
);
496 * mei_clear_list - removes all callbacks associated with file
499 * @dev: device structure.
500 * @file: file structure
501 * @mei_cb_list: callbacks list
503 * mei_clear_list is called to clear resources associated with file
504 * when application calls close function or Ctrl-C was pressed
506 * Return: true if callback removed from the list, false otherwise
508 static bool mei_clear_list(struct mei_device
*dev
,
509 const struct file
*file
, struct list_head
*mei_cb_list
)
511 struct mei_cl
*cl
= &dev
->iamthif_cl
;
512 struct mei_cl_cb
*cb
, *next
;
513 bool removed
= false;
515 /* list all list member */
516 list_for_each_entry_safe(cb
, next
, mei_cb_list
, list
) {
517 /* check if list member associated with a file */
518 if (file
== cb
->file_object
) {
519 /* check if cb equal to current iamthif cb */
520 if (dev
->iamthif_current_cb
== cb
) {
521 dev
->iamthif_current_cb
= NULL
;
522 /* send flow control to iamthif client */
523 mei_hbm_cl_flow_control_req(dev
, cl
);
525 /* free all allocated buffers */
534 * mei_clear_lists - removes all callbacks associated with file
536 * @dev: device structure
537 * @file: file structure
539 * mei_clear_lists is called to clear resources associated with file
540 * when application calls close function or Ctrl-C was pressed
542 * Return: true if callback removed from the list, false otherwise
544 static bool mei_clear_lists(struct mei_device
*dev
, struct file
*file
)
546 bool removed
= false;
548 /* remove callbacks associated with a file */
549 mei_clear_list(dev
, file
, &dev
->amthif_cmd_list
.list
);
550 if (mei_clear_list(dev
, file
, &dev
->amthif_rd_complete_list
.list
))
553 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.list
);
555 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.list
))
558 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.list
))
561 if (mei_clear_list(dev
, file
, &dev
->write_list
.list
))
564 /* check if iamthif_current_cb not NULL */
565 if (dev
->iamthif_current_cb
&& !removed
) {
566 /* check file and iamthif current cb association */
567 if (dev
->iamthif_current_cb
->file_object
== file
) {
569 mei_io_cb_free(dev
->iamthif_current_cb
);
570 dev
->iamthif_current_cb
= NULL
;
578 * mei_amthif_release - the release function
580 * @dev: device structure
581 * @file: pointer to file structure
583 * Return: 0 on success, <0 on error
585 int mei_amthif_release(struct mei_device
*dev
, struct file
*file
)
587 if (dev
->iamthif_open_count
> 0)
588 dev
->iamthif_open_count
--;
590 if (dev
->iamthif_file_object
== file
&&
591 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
593 dev_dbg(dev
->dev
, "amthif canceled iamthif state %d\n",
595 dev
->iamthif_canceled
= true;
596 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
597 dev_dbg(dev
->dev
, "run next amthif iamthif cb\n");
598 mei_amthif_run_next_cmd(dev
);
602 if (mei_clear_lists(dev
, file
))
603 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;