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
18 #include <linux/export.h>
19 #include <linux/kthread.h>
20 #include <linux/interrupt.h>
22 #include <linux/jiffies.h>
23 #include <linux/slab.h>
25 #include <linux/mei.h>
33 * mei_irq_compl_handler - dispatch complete handlers
34 * for the completed callbacks
37 * @compl_list: list of completed cbs
39 void mei_irq_compl_handler(struct mei_device
*dev
, struct mei_cl_cb
*compl_list
)
41 struct mei_cl_cb
*cb
, *next
;
44 list_for_each_entry_safe(cb
, next
, &compl_list
->list
, list
) {
48 dev_dbg(dev
->dev
, "completing call back.\n");
49 if (cl
== &dev
->iamthif_cl
)
50 mei_amthif_complete(dev
, cb
);
52 mei_cl_complete(cl
, cb
);
55 EXPORT_SYMBOL_GPL(mei_irq_compl_handler
);
58 * mei_cl_hbm_equal - check if hbm is addressed to the client
61 * @mei_hdr: header of mei client message
63 * Return: true if matches, false otherwise
65 static inline int mei_cl_hbm_equal(struct mei_cl
*cl
,
66 struct mei_msg_hdr
*mei_hdr
)
68 return cl
->host_client_id
== mei_hdr
->host_addr
&&
69 cl
->me_client_id
== mei_hdr
->me_addr
;
72 * mei_cl_is_reading - checks if the client is in reading state
76 * Return: true if the client is reading
78 static bool mei_cl_is_reading(struct mei_cl
*cl
)
80 return cl
->state
== MEI_FILE_CONNECTED
&&
81 cl
->reading_state
!= MEI_READ_COMPLETE
;
85 * mei_cl_irq_read_msg - process client message
88 * @mei_hdr: header of mei client message
89 * @complete_list: completion list
93 static int mei_cl_irq_read_msg(struct mei_cl
*cl
,
94 struct mei_msg_hdr
*mei_hdr
,
95 struct mei_cl_cb
*complete_list
)
97 struct mei_device
*dev
= cl
->dev
;
99 unsigned char *buffer
= NULL
;
101 list_for_each_entry(cb
, &dev
->read_list
.list
, list
) {
106 if (&cb
->list
== &dev
->read_list
.list
) {
107 dev_err(dev
->dev
, "no reader found\n");
111 if (!mei_cl_is_reading(cl
)) {
112 cl_err(dev
, cl
, "cl is not reading state=%d reading state=%d\n",
113 cl
->state
, cl
->reading_state
);
117 cl
->reading_state
= MEI_READING
;
119 if (cb
->response_buffer
.size
== 0 ||
120 cb
->response_buffer
.data
== NULL
) {
121 cl_err(dev
, cl
, "response buffer is not allocated.\n");
122 list_move_tail(&cb
->list
, &complete_list
->list
);
123 cb
->status
= -ENOMEM
;
127 if (cb
->response_buffer
.size
< mei_hdr
->length
+ cb
->buf_idx
) {
128 cl_dbg(dev
, cl
, "message overflow. size %d len %d idx %ld\n",
129 cb
->response_buffer
.size
, mei_hdr
->length
, cb
->buf_idx
);
130 buffer
= krealloc(cb
->response_buffer
.data
,
131 mei_hdr
->length
+ cb
->buf_idx
,
135 cb
->status
= -ENOMEM
;
136 list_move_tail(&cb
->list
, &complete_list
->list
);
139 cb
->response_buffer
.data
= buffer
;
140 cb
->response_buffer
.size
= mei_hdr
->length
+ cb
->buf_idx
;
143 buffer
= cb
->response_buffer
.data
+ cb
->buf_idx
;
144 mei_read_slots(dev
, buffer
, mei_hdr
->length
);
146 cb
->buf_idx
+= mei_hdr
->length
;
147 if (mei_hdr
->msg_complete
) {
148 cl_dbg(dev
, cl
, "completed read length = %lu\n",
150 list_move_tail(&cb
->list
, &complete_list
->list
);
155 /* assume that mei_hdr->length <= MEI_RD_MSG_BUF_SIZE */
156 BUG_ON(mei_hdr
->length
> MEI_RD_MSG_BUF_SIZE
);
157 mei_read_slots(dev
, dev
->rd_msg_buf
, mei_hdr
->length
);
158 dev_dbg(dev
->dev
, "discarding message " MEI_HDR_FMT
"\n",
159 MEI_HDR_PRM(mei_hdr
));
166 * mei_cl_irq_disconnect_rsp - send disconnection response message
169 * @cb: callback block.
170 * @cmpl_list: complete list.
172 * Return: 0, OK; otherwise, error.
174 static int mei_cl_irq_disconnect_rsp(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
175 struct mei_cl_cb
*cmpl_list
)
177 struct mei_device
*dev
= cl
->dev
;
182 slots
= mei_hbuf_empty_slots(dev
);
183 msg_slots
= mei_data2slots(sizeof(struct hbm_client_connect_response
));
185 if (slots
< msg_slots
)
188 ret
= mei_hbm_cl_disconnect_rsp(dev
, cl
);
190 cl
->state
= MEI_FILE_DISCONNECTED
;
201 * mei_cl_irq_disconnect - processes close related operation from
202 * interrupt thread context - send disconnect request
205 * @cb: callback block.
206 * @cmpl_list: complete list.
208 * Return: 0, OK; otherwise, error.
210 static int mei_cl_irq_disconnect(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
211 struct mei_cl_cb
*cmpl_list
)
213 struct mei_device
*dev
= cl
->dev
;
217 msg_slots
= mei_data2slots(sizeof(struct hbm_client_connect_request
));
218 slots
= mei_hbuf_empty_slots(dev
);
220 if (slots
< msg_slots
)
223 if (mei_hbm_cl_disconnect_req(dev
, cl
)) {
226 list_move_tail(&cb
->list
, &cmpl_list
->list
);
230 cl
->state
= MEI_FILE_DISCONNECTING
;
233 list_move_tail(&cb
->list
, &dev
->ctrl_rd_list
.list
);
234 cl
->timer_count
= MEI_CONNECT_TIMEOUT
;
241 * mei_cl_irq_read - processes client read related operation from the
242 * interrupt thread context - request for flow control credits
245 * @cb: callback block.
246 * @cmpl_list: complete list.
248 * Return: 0, OK; otherwise, error.
250 static int mei_cl_irq_read(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
251 struct mei_cl_cb
*cmpl_list
)
253 struct mei_device
*dev
= cl
->dev
;
258 msg_slots
= mei_data2slots(sizeof(struct hbm_flow_control
));
259 slots
= mei_hbuf_empty_slots(dev
);
261 if (slots
< msg_slots
)
264 ret
= mei_hbm_cl_flow_control_req(dev
, cl
);
268 list_move_tail(&cb
->list
, &cmpl_list
->list
);
272 list_move_tail(&cb
->list
, &dev
->read_list
.list
);
279 * mei_cl_irq_connect - send connect request in irq_thread context
282 * @cb: callback block.
283 * @cmpl_list: complete list.
285 * Return: 0, OK; otherwise, error.
287 static int mei_cl_irq_connect(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
288 struct mei_cl_cb
*cmpl_list
)
290 struct mei_device
*dev
= cl
->dev
;
295 msg_slots
= mei_data2slots(sizeof(struct hbm_client_connect_request
));
296 slots
= mei_hbuf_empty_slots(dev
);
298 if (mei_cl_is_other_connecting(cl
))
301 if (slots
< msg_slots
)
304 cl
->state
= MEI_FILE_CONNECTING
;
306 ret
= mei_hbm_cl_connect_req(dev
, cl
);
314 list_move_tail(&cb
->list
, &dev
->ctrl_rd_list
.list
);
315 cl
->timer_count
= MEI_CONNECT_TIMEOUT
;
321 * mei_irq_read_handler - bottom half read routine after ISR to
322 * handle the read processing.
324 * @dev: the device structure
325 * @cmpl_list: An instance of our list structure
326 * @slots: slots to read.
328 * Return: 0 on success, <0 on failure.
330 int mei_irq_read_handler(struct mei_device
*dev
,
331 struct mei_cl_cb
*cmpl_list
, s32
*slots
)
333 struct mei_msg_hdr
*mei_hdr
;
337 if (!dev
->rd_msg_hdr
) {
338 dev
->rd_msg_hdr
= mei_read_hdr(dev
);
340 dev_dbg(dev
->dev
, "slots =%08x.\n", *slots
);
342 mei_hdr
= (struct mei_msg_hdr
*) &dev
->rd_msg_hdr
;
343 dev_dbg(dev
->dev
, MEI_HDR_FMT
, MEI_HDR_PRM(mei_hdr
));
345 if (mei_hdr
->reserved
|| !dev
->rd_msg_hdr
) {
346 dev_err(dev
->dev
, "corrupted message header 0x%08X\n",
352 if (mei_slots2data(*slots
) < mei_hdr
->length
) {
353 dev_err(dev
->dev
, "less data available than length=%08x.\n",
355 /* we can't read the message */
361 if (mei_hdr
->host_addr
== 0 && mei_hdr
->me_addr
== 0) {
362 ret
= mei_hbm_dispatch(dev
, mei_hdr
);
364 dev_dbg(dev
->dev
, "mei_hbm_dispatch failed ret = %d\n",
371 /* find recipient cl */
372 list_for_each_entry(cl
, &dev
->file_list
, link
) {
373 if (mei_cl_hbm_equal(cl
, mei_hdr
)) {
374 cl_dbg(dev
, cl
, "got a message\n");
379 /* if no recipient cl was found we assume corrupted header */
380 if (&cl
->link
== &dev
->file_list
) {
381 dev_err(dev
->dev
, "no destination client found 0x%08X\n",
387 if (mei_hdr
->host_addr
== dev
->iamthif_cl
.host_client_id
&&
388 MEI_FILE_CONNECTED
== dev
->iamthif_cl
.state
&&
389 dev
->iamthif_state
== MEI_IAMTHIF_READING
) {
391 ret
= mei_amthif_irq_read_msg(dev
, mei_hdr
, cmpl_list
);
393 dev_err(dev
->dev
, "mei_amthif_irq_read_msg failed = %d\n",
398 ret
= mei_cl_irq_read_msg(cl
, mei_hdr
, cmpl_list
);
403 /* reset the number of slots and header */
404 *slots
= mei_count_full_read_slots(dev
);
407 if (*slots
== -EOVERFLOW
) {
408 /* overflow - reset */
409 dev_err(dev
->dev
, "resetting due to slots overflow.\n");
410 /* set the event since message has been read */
417 EXPORT_SYMBOL_GPL(mei_irq_read_handler
);
421 * mei_irq_write_handler - dispatch write requests
424 * @dev: the device structure
425 * @cmpl_list: An instance of our list structure
427 * Return: 0 on success, <0 on failure.
429 int mei_irq_write_handler(struct mei_device
*dev
, struct mei_cl_cb
*cmpl_list
)
433 struct mei_cl_cb
*cb
, *next
;
434 struct mei_cl_cb
*list
;
439 if (!mei_hbuf_acquire(dev
))
442 slots
= mei_hbuf_empty_slots(dev
);
446 /* complete all waiting for write CB */
447 dev_dbg(dev
->dev
, "complete all waiting for write cb.\n");
449 list
= &dev
->write_waiting_list
;
450 list_for_each_entry_safe(cb
, next
, &list
->list
, list
) {
455 if (cb
->fop_type
== MEI_FOP_WRITE
&&
456 cl
!= &dev
->iamthif_cl
) {
457 cl_dbg(dev
, cl
, "MEI WRITE COMPLETE\n");
458 cl
->writing_state
= MEI_WRITE_COMPLETE
;
459 list_add_tail(&cb
->list
, &cmpl_list
->list
);
461 if (cl
== &dev
->iamthif_cl
) {
462 cl_dbg(dev
, cl
, "check iamthif flow control.\n");
463 if (dev
->iamthif_flow_control_pending
) {
464 ret
= mei_amthif_irq_read(dev
, &slots
);
471 if (dev
->wd_state
== MEI_WD_STOPPING
) {
472 dev
->wd_state
= MEI_WD_IDLE
;
473 wake_up(&dev
->wait_stop_wd
);
476 if (mei_cl_is_connected(&dev
->wd_cl
)) {
477 if (dev
->wd_pending
&&
478 mei_cl_flow_ctrl_creds(&dev
->wd_cl
) > 0) {
479 ret
= mei_wd_send(dev
);
482 dev
->wd_pending
= false;
486 /* complete control write list CB */
487 dev_dbg(dev
->dev
, "complete control write list cb.\n");
488 list_for_each_entry_safe(cb
, next
, &dev
->ctrl_wr_list
.list
, list
) {
490 switch (cb
->fop_type
) {
491 case MEI_FOP_DISCONNECT
:
492 /* send disconnect message */
493 ret
= mei_cl_irq_disconnect(cl
, cb
, cmpl_list
);
499 /* send flow control message */
500 ret
= mei_cl_irq_read(cl
, cb
, cmpl_list
);
505 case MEI_FOP_CONNECT
:
506 /* connect message */
507 ret
= mei_cl_irq_connect(cl
, cb
, cmpl_list
);
512 case MEI_FOP_DISCONNECT_RSP
:
513 /* send disconnect resp */
514 ret
= mei_cl_irq_disconnect_rsp(cl
, cb
, cmpl_list
);
523 /* complete write list CB */
524 dev_dbg(dev
->dev
, "complete write list cb.\n");
525 list_for_each_entry_safe(cb
, next
, &dev
->write_list
.list
, list
) {
527 if (cl
== &dev
->iamthif_cl
)
528 ret
= mei_amthif_irq_write(cl
, cb
, cmpl_list
);
530 ret
= mei_cl_irq_write(cl
, cb
, cmpl_list
);
536 EXPORT_SYMBOL_GPL(mei_irq_write_handler
);
541 * mei_timer - timer function.
543 * @work: pointer to the work_struct structure
546 void mei_timer(struct work_struct
*work
)
548 unsigned long timeout
;
551 struct mei_device
*dev
= container_of(work
,
552 struct mei_device
, timer_work
.work
);
555 mutex_lock(&dev
->device_lock
);
557 /* Catch interrupt stalls during HBM init handshake */
558 if (dev
->dev_state
== MEI_DEV_INIT_CLIENTS
&&
559 dev
->hbm_state
!= MEI_HBM_IDLE
) {
561 if (dev
->init_clients_timer
) {
562 if (--dev
->init_clients_timer
== 0) {
563 dev_err(dev
->dev
, "timer: init clients timeout hbm_state = %d.\n",
571 if (dev
->dev_state
!= MEI_DEV_ENABLED
)
574 /*** connect/disconnect timeouts ***/
575 list_for_each_entry(cl
, &dev
->file_list
, link
) {
576 if (cl
->timer_count
) {
577 if (--cl
->timer_count
== 0) {
578 dev_err(dev
->dev
, "timer: connect/disconnect timeout.\n");
585 if (!mei_cl_is_connected(&dev
->iamthif_cl
))
588 if (dev
->iamthif_stall_timer
) {
589 if (--dev
->iamthif_stall_timer
== 0) {
590 dev_err(dev
->dev
, "timer: amthif hanged.\n");
592 dev
->iamthif_msg_buf_size
= 0;
593 dev
->iamthif_msg_buf_index
= 0;
594 dev
->iamthif_canceled
= false;
595 dev
->iamthif_ioctl
= true;
596 dev
->iamthif_state
= MEI_IAMTHIF_IDLE
;
597 dev
->iamthif_timer
= 0;
599 mei_io_cb_free(dev
->iamthif_current_cb
);
600 dev
->iamthif_current_cb
= NULL
;
602 dev
->iamthif_file_object
= NULL
;
603 mei_amthif_run_next_cmd(dev
);
607 if (dev
->iamthif_timer
) {
609 timeout
= dev
->iamthif_timer
+
610 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER
);
612 dev_dbg(dev
->dev
, "dev->iamthif_timer = %ld\n",
614 dev_dbg(dev
->dev
, "timeout = %ld\n", timeout
);
615 dev_dbg(dev
->dev
, "jiffies = %ld\n", jiffies
);
616 if (time_after(jiffies
, timeout
)) {
618 * User didn't read the AMTHI data on time (15sec)
619 * freeing AMTHI for other requests
622 dev_dbg(dev
->dev
, "freeing AMTHI for other requests\n");
624 mei_io_list_flush(&dev
->amthif_rd_complete_list
,
626 mei_io_cb_free(dev
->iamthif_current_cb
);
627 dev
->iamthif_current_cb
= NULL
;
629 dev
->iamthif_file_object
->private_data
= NULL
;
630 dev
->iamthif_file_object
= NULL
;
631 dev
->iamthif_timer
= 0;
632 mei_amthif_run_next_cmd(dev
);
637 if (dev
->dev_state
!= MEI_DEV_DISABLED
)
638 schedule_delayed_work(&dev
->timer_work
, 2 * HZ
);
639 mutex_unlock(&dev
->device_lock
);