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_stall_timer
= 0;
54 dev
->iamthif_open_count
= 0;
58 * mei_amthif_host_init - mei initialization amthif client.
60 * @dev: the device structure
63 * Return: 0 on success, <0 on failure.
65 int mei_amthif_host_init(struct mei_device
*dev
, struct mei_me_client
*me_cl
)
67 struct mei_cl
*cl
= &dev
->iamthif_cl
;
70 if (mei_cl_is_connected(cl
))
73 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
77 ret
= mei_cl_link(cl
);
79 dev_err(dev
->dev
, "amthif: failed cl_link %d\n", ret
);
83 ret
= mei_cl_connect(cl
, me_cl
, NULL
);
89 * mei_amthif_read - read data from AMTHIF client
91 * @dev: the device structure
92 * @file: pointer to file object
93 * @ubuf: pointer to user data in user space
94 * @length: data length to read
95 * @offset: data read offset
97 * Locking: called under "dev->device_lock" lock
100 * returned data length on success,
101 * zero if no data to read,
102 * negative on failure.
104 int mei_amthif_read(struct mei_device
*dev
, struct file
*file
,
105 char __user
*ubuf
, size_t length
, loff_t
*offset
)
107 struct mei_cl
*cl
= file
->private_data
;
108 struct mei_cl_cb
*cb
;
112 dev_dbg(dev
->dev
, "checking amthif data\n");
113 cb
= mei_cl_read_cb(cl
, file
);
115 /* Check for if we can block or not*/
116 if (cb
== NULL
&& file
->f_flags
& O_NONBLOCK
)
120 dev_dbg(dev
->dev
, "waiting for amthif data\n");
122 /* unlock the Mutex */
123 mutex_unlock(&dev
->device_lock
);
125 wait_ret
= wait_event_interruptible(cl
->rx_wait
,
126 !list_empty(&cl
->rd_completed
) ||
127 !mei_cl_is_connected(cl
));
129 /* Locking again the Mutex */
130 mutex_lock(&dev
->device_lock
);
135 if (!mei_cl_is_connected(cl
)) {
140 cb
= mei_cl_read_cb(cl
, file
);
145 dev_dbg(dev
->dev
, "read operation failed %d\n", rets
);
149 dev_dbg(dev
->dev
, "Got amthif data\n");
150 /* if the whole message will fit remove it from the list */
151 if (cb
->buf_idx
>= *offset
&& length
>= (cb
->buf_idx
- *offset
))
152 list_del_init(&cb
->list
);
153 else if (cb
->buf_idx
<= *offset
) {
154 /* end of the message has been reached */
155 list_del_init(&cb
->list
);
159 /* else means that not full buffer will be read and do not
160 * remove message from deletion list
163 dev_dbg(dev
->dev
, "amthif cb->buf.size - %zu cb->buf_idx - %zu\n",
164 cb
->buf
.size
, cb
->buf_idx
);
166 /* length is being truncated to PAGE_SIZE, however,
167 * the buf_idx may point beyond */
168 length
= min_t(size_t, length
, (cb
->buf_idx
- *offset
));
170 if (copy_to_user(ubuf
, cb
->buf
.data
+ *offset
, length
)) {
171 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
175 if ((*offset
+ length
) < cb
->buf_idx
) {
181 dev_dbg(dev
->dev
, "free amthif cb memory.\n");
189 * mei_amthif_read_start - queue message for sending read credential
192 * @file: file pointer of message recipient
194 * Return: 0 on success, <0 on failure.
196 static int mei_amthif_read_start(struct mei_cl
*cl
, const struct file
*file
)
198 struct mei_device
*dev
= cl
->dev
;
199 struct mei_cl_cb
*cb
;
202 cb
= mei_io_cb_init(cl
, MEI_FOP_READ
, file
);
208 rets
= mei_io_cb_alloc_buf(cb
, mei_cl_mtu(cl
));
212 list_add_tail(&cb
->list
, &dev
->ctrl_wr_list
.list
);
214 dev
->iamthif_state
= MEI_IAMTHIF_READING
;
215 dev
->iamthif_fp
= cb
->fp
;
216 dev
->iamthif_current_cb
= cb
;
225 * mei_amthif_send_cmd - send amthif command to the ME
227 * @cl: the host client
228 * @cb: mei call back struct
230 * Return: 0 on success, <0 on failure.
232 static int mei_amthif_send_cmd(struct mei_cl
*cl
, struct mei_cl_cb
*cb
)
234 struct mei_device
*dev
;
242 dev
->iamthif_state
= MEI_IAMTHIF_WRITING
;
243 dev
->iamthif_current_cb
= cb
;
244 dev
->iamthif_fp
= cb
->fp
;
245 dev
->iamthif_canceled
= false;
247 ret
= mei_cl_write(cl
, cb
, false);
252 cb
->status
= mei_amthif_read_start(cl
, cb
->fp
);
258 * mei_amthif_run_next_cmd - send next amt command from queue
260 * @dev: the device structure
262 * Return: 0 on success, <0 on failure.
264 int mei_amthif_run_next_cmd(struct mei_device
*dev
)
266 struct mei_cl
*cl
= &dev
->iamthif_cl
;
267 struct mei_cl_cb
*cb
;
269 dev
->iamthif_canceled
= false;
270 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
271 dev
->iamthif_fp
= NULL
;
273 dev_dbg(dev
->dev
, "complete amthif cmd_list cb.\n");
275 cb
= list_first_entry_or_null(&dev
->amthif_cmd_list
.list
,
280 list_del_init(&cb
->list
);
281 return mei_amthif_send_cmd(cl
, cb
);
285 * mei_amthif_write - write amthif data to amthif client
288 * @cb: mei call back struct
290 * Return: 0 on success, <0 on failure.
292 int mei_amthif_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
)
295 struct mei_device
*dev
= cl
->dev
;
297 list_add_tail(&cb
->list
, &dev
->amthif_cmd_list
.list
);
300 * The previous request is still in processing, queue this one.
302 if (dev
->iamthif_state
> MEI_IAMTHIF_IDLE
&&
303 dev
->iamthif_state
< MEI_IAMTHIF_READ_COMPLETE
)
306 return mei_amthif_run_next_cmd(dev
);
310 * mei_amthif_poll - the amthif poll function
312 * @dev: the device structure
313 * @file: pointer to file structure
314 * @wait: pointer to poll_table structure
318 * Locking: called under "dev->device_lock" lock
321 unsigned int mei_amthif_poll(struct mei_device
*dev
,
322 struct file
*file
, poll_table
*wait
)
324 unsigned int mask
= 0;
326 poll_wait(file
, &dev
->iamthif_cl
.rx_wait
, wait
);
328 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
&&
329 dev
->iamthif_fp
== file
) {
331 mask
|= POLLIN
| POLLRDNORM
;
332 mei_amthif_run_next_cmd(dev
);
341 * mei_amthif_irq_write - write iamthif command in irq thread context.
343 * @cl: private data of the file object.
344 * @cb: callback block.
345 * @cmpl_list: complete list.
347 * Return: 0, OK; otherwise, error.
349 int mei_amthif_irq_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
350 struct mei_cl_cb
*cmpl_list
)
354 ret
= mei_cl_irq_write(cl
, cb
, cmpl_list
);
359 cb
->status
= mei_amthif_read_start(cl
, cb
->fp
);
365 * mei_amthif_irq_read_msg - read routine after ISR to
366 * handle the read amthif message
369 * @mei_hdr: header of amthif message
370 * @cmpl_list: completed callbacks list
372 * Return: -ENODEV if cb is NULL 0 otherwise; error message is in cb->status
374 int mei_amthif_irq_read_msg(struct mei_cl
*cl
,
375 struct mei_msg_hdr
*mei_hdr
,
376 struct mei_cl_cb
*cmpl_list
)
378 struct mei_device
*dev
;
383 if (dev
->iamthif_state
!= MEI_IAMTHIF_READING
)
386 ret
= mei_cl_irq_read_msg(cl
, mei_hdr
, cmpl_list
);
390 if (!mei_hdr
->msg_complete
)
393 dev_dbg(dev
->dev
, "completed amthif read.\n ");
394 dev
->iamthif_current_cb
= NULL
;
395 dev
->iamthif_stall_timer
= 0;
401 * mei_amthif_complete - complete amthif callback.
404 * @cb: callback block.
406 void mei_amthif_complete(struct mei_cl
*cl
, struct mei_cl_cb
*cb
)
408 struct mei_device
*dev
= cl
->dev
;
410 if (cb
->fop_type
== MEI_FOP_WRITE
) {
412 dev
->iamthif_stall_timer
= MEI_IAMTHIF_STALL_TIMER
;
417 * in case of error enqueue the write cb to complete read list
418 * so it can be propagated to the reader
420 list_add_tail(&cb
->list
, &cl
->rd_completed
);
421 wake_up_interruptible(&cl
->rx_wait
);
425 if (!dev
->iamthif_canceled
) {
426 dev
->iamthif_state
= MEI_IAMTHIF_READ_COMPLETE
;
427 dev
->iamthif_stall_timer
= 0;
428 list_add_tail(&cb
->list
, &cl
->rd_completed
);
429 dev_dbg(dev
->dev
, "amthif read completed\n");
431 mei_amthif_run_next_cmd(dev
);
434 dev_dbg(dev
->dev
, "completing amthif call back.\n");
435 wake_up_interruptible(&cl
->rx_wait
);
439 * mei_clear_list - removes all callbacks associated with file
442 * @dev: device structure.
443 * @file: file structure
444 * @mei_cb_list: callbacks list
446 * mei_clear_list is called to clear resources associated with file
447 * when application calls close function or Ctrl-C was pressed
449 * Return: true if callback removed from the list, false otherwise
451 static bool mei_clear_list(struct mei_device
*dev
,
452 const struct file
*file
, struct list_head
*mei_cb_list
)
454 struct mei_cl
*cl
= &dev
->iamthif_cl
;
455 struct mei_cl_cb
*cb
, *next
;
456 bool removed
= false;
458 /* list all list member */
459 list_for_each_entry_safe(cb
, next
, mei_cb_list
, list
) {
460 /* check if list member associated with a file */
461 if (file
== cb
->fp
) {
462 /* check if cb equal to current iamthif cb */
463 if (dev
->iamthif_current_cb
== cb
) {
464 dev
->iamthif_current_cb
= NULL
;
465 /* send flow control to iamthif client */
466 mei_hbm_cl_flow_control_req(dev
, cl
);
468 /* free all allocated buffers */
477 * mei_clear_lists - removes all callbacks associated with file
479 * @dev: device structure
480 * @file: file structure
482 * mei_clear_lists is called to clear resources associated with file
483 * when application calls close function or Ctrl-C was pressed
485 * Return: true if callback removed from the list, false otherwise
487 static bool mei_clear_lists(struct mei_device
*dev
, const struct file
*file
)
489 bool removed
= false;
490 struct mei_cl
*cl
= &dev
->iamthif_cl
;
492 /* remove callbacks associated with a file */
493 mei_clear_list(dev
, file
, &dev
->amthif_cmd_list
.list
);
494 if (mei_clear_list(dev
, file
, &cl
->rd_completed
))
497 mei_clear_list(dev
, file
, &dev
->ctrl_rd_list
.list
);
499 if (mei_clear_list(dev
, file
, &dev
->ctrl_wr_list
.list
))
502 if (mei_clear_list(dev
, file
, &dev
->write_waiting_list
.list
))
505 if (mei_clear_list(dev
, file
, &dev
->write_list
.list
))
508 /* check if iamthif_current_cb not NULL */
509 if (dev
->iamthif_current_cb
&& !removed
) {
510 /* check file and iamthif current cb association */
511 if (dev
->iamthif_current_cb
->fp
== file
) {
513 mei_io_cb_free(dev
->iamthif_current_cb
);
514 dev
->iamthif_current_cb
= NULL
;
522 * mei_amthif_release - the release function
524 * @dev: device structure
525 * @file: pointer to file structure
527 * Return: 0 on success, <0 on error
529 int mei_amthif_release(struct mei_device
*dev
, struct file
*file
)
531 if (dev
->iamthif_open_count
> 0)
532 dev
->iamthif_open_count
--;
534 if (dev
->iamthif_fp
== file
&&
535 dev
->iamthif_state
!= MEI_IAMTHIF_IDLE
) {
537 dev_dbg(dev
->dev
, "amthif canceled iamthif state %d\n",
539 dev
->iamthif_canceled
= true;
540 if (dev
->iamthif_state
== MEI_IAMTHIF_READ_COMPLETE
) {
541 dev_dbg(dev
->dev
, "run next amthif iamthif cb\n");
542 mei_amthif_run_next_cmd(dev
);
546 if (mei_clear_lists(dev
, file
))
547 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;