i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / staging / mei / interrupt.c
blob3544fee34e4855913307c3249a1695c620149d02
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2011, 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
13 * more details.
18 #include <linux/pci.h>
19 #include <linux/kthread.h>
20 #include <linux/interrupt.h>
21 #include <linux/fs.h>
22 #include <linux/jiffies.h>
24 #include "mei_dev.h"
25 #include "mei.h"
26 #include "hw.h"
27 #include "interface.h"
30 /**
31 * mei_interrupt_quick_handler - The ISR of the MEI device
33 * @irq: The irq number
34 * @dev_id: pointer to the device structure
36 * returns irqreturn_t
38 irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id)
40 struct mei_device *dev = (struct mei_device *) dev_id;
41 u32 csr_reg = mei_hcsr_read(dev);
43 if ((csr_reg & H_IS) != H_IS)
44 return IRQ_NONE;
46 /* clear H_IS bit in H_CSR */
47 mei_reg_write(dev, H_CSR, csr_reg);
49 return IRQ_WAKE_THREAD;
52 /**
53 * _mei_cmpl - processes completed operation.
55 * @cl: private data of the file object.
56 * @cb_pos: callback block.
58 static void _mei_cmpl(struct mei_cl *cl, struct mei_cl_cb *cb_pos)
60 if (cb_pos->major_file_operations == MEI_WRITE) {
61 mei_free_cb_private(cb_pos);
62 cb_pos = NULL;
63 cl->writing_state = MEI_WRITE_COMPLETE;
64 if (waitqueue_active(&cl->tx_wait))
65 wake_up_interruptible(&cl->tx_wait);
67 } else if (cb_pos->major_file_operations == MEI_READ &&
68 MEI_READING == cl->reading_state) {
69 cl->reading_state = MEI_READ_COMPLETE;
70 if (waitqueue_active(&cl->rx_wait))
71 wake_up_interruptible(&cl->rx_wait);
76 /**
77 * _mei_cmpl_iamthif - processes completed iamthif operation.
79 * @dev: the device structure.
80 * @cb_pos: callback block.
82 static void _mei_cmpl_iamthif(struct mei_device *dev, struct mei_cl_cb *cb_pos)
84 if (dev->iamthif_canceled != 1) {
85 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
86 dev->iamthif_stall_timer = 0;
87 memcpy(cb_pos->response_buffer.data,
88 dev->iamthif_msg_buf,
89 dev->iamthif_msg_buf_index);
90 list_add_tail(&cb_pos->cb_list,
91 &dev->amthi_read_complete_list.mei_cb.cb_list);
92 dev_dbg(&dev->pdev->dev, "amthi read completed.\n");
93 dev->iamthif_timer = jiffies;
94 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
95 dev->iamthif_timer);
96 } else {
97 mei_run_next_iamthif_cmd(dev);
100 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
101 wake_up_interruptible(&dev->iamthif_cl.wait);
106 * mei_irq_thread_read_amthi_message - bottom half read routine after ISR to
107 * handle the read amthi message data processing.
109 * @complete_list: An instance of our list structure
110 * @dev: the device structure
111 * @mei_hdr: header of amthi message
113 * returns 0 on success, <0 on failure.
115 static int mei_irq_thread_read_amthi_message(struct mei_io_list *complete_list,
116 struct mei_device *dev,
117 struct mei_msg_hdr *mei_hdr)
119 struct mei_cl *cl;
120 struct mei_cl_cb *cb;
121 unsigned char *buffer;
123 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
124 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
126 buffer = (unsigned char *) (dev->iamthif_msg_buf +
127 dev->iamthif_msg_buf_index);
128 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
130 mei_read_slots(dev, buffer, mei_hdr->length);
132 dev->iamthif_msg_buf_index += mei_hdr->length;
134 if (!mei_hdr->msg_complete)
135 return 0;
137 dev_dbg(&dev->pdev->dev,
138 "amthi_message_buffer_index =%d\n",
139 mei_hdr->length);
141 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
142 if (!dev->iamthif_current_cb)
143 return -ENODEV;
145 cb = dev->iamthif_current_cb;
146 dev->iamthif_current_cb = NULL;
148 cl = (struct mei_cl *)cb->file_private;
149 if (!cl)
150 return -ENODEV;
152 dev->iamthif_stall_timer = 0;
153 cb->information = dev->iamthif_msg_buf_index;
154 cb->read_time = jiffies;
155 if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
156 /* found the iamthif cb */
157 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
158 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
159 list_add_tail(&cb->cb_list,
160 &complete_list->mei_cb.cb_list);
162 return 0;
166 * _mei_irq_thread_state_ok - checks if mei header matches file private data
168 * @cl: private data of the file object
169 * @mei_hdr: header of mei client message
171 * returns !=0 if matches, 0 if no match.
173 static int _mei_irq_thread_state_ok(struct mei_cl *cl,
174 struct mei_msg_hdr *mei_hdr)
176 return (cl->host_client_id == mei_hdr->host_addr &&
177 cl->me_client_id == mei_hdr->me_addr &&
178 cl->state == MEI_FILE_CONNECTED &&
179 MEI_READ_COMPLETE != cl->reading_state);
183 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
184 * handle the read mei client message data processing.
186 * @complete_list: An instance of our list structure
187 * @dev: the device structure
188 * @mei_hdr: header of mei client message
190 * returns 0 on success, <0 on failure.
192 static int mei_irq_thread_read_client_message(struct mei_io_list *complete_list,
193 struct mei_device *dev,
194 struct mei_msg_hdr *mei_hdr)
196 struct mei_cl *cl;
197 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
198 unsigned char *buffer = NULL;
200 dev_dbg(&dev->pdev->dev, "start client msg\n");
201 if (list_empty(&dev->read_list.mei_cb.cb_list))
202 goto quit;
204 list_for_each_entry_safe(cb_pos, cb_next,
205 &dev->read_list.mei_cb.cb_list, cb_list) {
206 cl = (struct mei_cl *)cb_pos->file_private;
207 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
208 cl->reading_state = MEI_READING;
209 buffer = (unsigned char *)
210 (cb_pos->response_buffer.data +
211 cb_pos->information);
213 if (cb_pos->response_buffer.size <
214 mei_hdr->length + cb_pos->information) {
215 dev_dbg(&dev->pdev->dev, "message overflow.\n");
216 list_del(&cb_pos->cb_list);
217 return -ENOMEM;
219 if (buffer)
220 mei_read_slots(dev, buffer, mei_hdr->length);
222 cb_pos->information += mei_hdr->length;
223 if (mei_hdr->msg_complete) {
224 cl->status = 0;
225 list_del(&cb_pos->cb_list);
226 dev_dbg(&dev->pdev->dev,
227 "completed read host client = %d,"
228 "ME client = %d, "
229 "data length = %lu\n",
230 cl->host_client_id,
231 cl->me_client_id,
232 cb_pos->information);
234 *(cb_pos->response_buffer.data +
235 cb_pos->information) = '\0';
236 dev_dbg(&dev->pdev->dev, "cb_pos->res_buffer - %s\n",
237 cb_pos->response_buffer.data);
238 list_add_tail(&cb_pos->cb_list,
239 &complete_list->mei_cb.cb_list);
242 break;
247 quit:
248 dev_dbg(&dev->pdev->dev, "message read\n");
249 if (!buffer) {
250 mei_read_slots(dev, (unsigned char *) dev->rd_msg_buf,
251 mei_hdr->length);
252 dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
253 *(u32 *) dev->rd_msg_buf);
256 return 0;
260 * _mei_irq_thread_iamthif_read - prepares to read iamthif data.
262 * @dev: the device structure.
263 * @slots: free slots.
265 * returns 0, OK; otherwise, error.
267 static int _mei_irq_thread_iamthif_read(struct mei_device *dev, s32 *slots)
270 if (((*slots) * sizeof(u32)) >= (sizeof(struct mei_msg_hdr)
271 + sizeof(struct hbm_flow_control))) {
272 *slots -= (sizeof(struct mei_msg_hdr) +
273 sizeof(struct hbm_flow_control) + 3) / 4;
274 if (!mei_send_flow_control(dev, &dev->iamthif_cl)) {
275 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
276 } else {
277 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
278 dev->iamthif_state = MEI_IAMTHIF_READING;
279 dev->iamthif_flow_control_pending = false;
280 dev->iamthif_msg_buf_index = 0;
281 dev->iamthif_msg_buf_size = 0;
282 dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
283 dev->mei_host_buffer_is_empty =
284 mei_host_buffer_is_empty(dev);
286 return 0;
287 } else {
288 return -EMSGSIZE;
293 * _mei_irq_thread_close - processes close related operation.
295 * @dev: the device structure.
296 * @slots: free slots.
297 * @cb_pos: callback block.
298 * @cl: private data of the file object.
299 * @cmpl_list: complete list.
301 * returns 0, OK; otherwise, error.
303 static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
304 struct mei_cl_cb *cb_pos,
305 struct mei_cl *cl,
306 struct mei_io_list *cmpl_list)
308 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
309 sizeof(struct hbm_client_disconnect_request))) {
310 *slots -= (sizeof(struct mei_msg_hdr) +
311 sizeof(struct hbm_client_disconnect_request) + 3) / 4;
313 if (!mei_disconnect(dev, cl)) {
314 cl->status = 0;
315 cb_pos->information = 0;
316 list_move_tail(&cb_pos->cb_list,
317 &cmpl_list->mei_cb.cb_list);
318 return -EMSGSIZE;
319 } else {
320 cl->state = MEI_FILE_DISCONNECTING;
321 cl->status = 0;
322 cb_pos->information = 0;
323 list_move_tail(&cb_pos->cb_list,
324 &dev->ctrl_rd_list.mei_cb.cb_list);
325 cl->timer_count = MEI_CONNECT_TIMEOUT;
327 } else {
328 /* return the cancel routine */
329 return -EBADMSG;
332 return 0;
336 * is_treat_specially_client - checks if the message belongs
337 * to the file private data.
339 * @cl: private data of the file object
340 * @rs: connect response bus message
343 static bool is_treat_specially_client(struct mei_cl *cl,
344 struct hbm_client_connect_response *rs)
347 if (cl->host_client_id == rs->host_addr &&
348 cl->me_client_id == rs->me_addr) {
349 if (!rs->status) {
350 cl->state = MEI_FILE_CONNECTED;
351 cl->status = 0;
353 } else {
354 cl->state = MEI_FILE_DISCONNECTED;
355 cl->status = -ENODEV;
357 cl->timer_count = 0;
359 return true;
361 return false;
365 * mei_client_connect_response - connects to response irq routine
367 * @dev: the device structure
368 * @rs: connect response bus message
370 static void mei_client_connect_response(struct mei_device *dev,
371 struct hbm_client_connect_response *rs)
374 struct mei_cl *cl;
375 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
377 dev_dbg(&dev->pdev->dev,
378 "connect_response:\n"
379 "ME Client = %d\n"
380 "Host Client = %d\n"
381 "Status = %d\n",
382 rs->me_addr,
383 rs->host_addr,
384 rs->status);
386 /* if WD or iamthif client treat specially */
388 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
389 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
390 mei_watchdog_register(dev);
392 /* next step in the state maching */
393 mei_host_init_iamthif(dev);
394 return;
397 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
398 dev->iamthif_state = MEI_IAMTHIF_IDLE;
399 return;
401 list_for_each_entry_safe(cb_pos, cb_next,
402 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
404 cl = (struct mei_cl *)cb_pos->file_private;
405 if (!cl) {
406 list_del(&cb_pos->cb_list);
407 return;
409 if (MEI_IOCTL == cb_pos->major_file_operations) {
410 if (is_treat_specially_client(cl, rs)) {
411 list_del(&cb_pos->cb_list);
412 cl->status = 0;
413 cl->timer_count = 0;
414 break;
421 * mei_client_disconnect_response - disconnects from response irq routine
423 * @dev: the device structure
424 * @rs: disconnect response bus message
426 static void mei_client_disconnect_response(struct mei_device *dev,
427 struct hbm_client_connect_response *rs)
429 struct mei_cl *cl;
430 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
432 dev_dbg(&dev->pdev->dev,
433 "disconnect_response:\n"
434 "ME Client = %d\n"
435 "Host Client = %d\n"
436 "Status = %d\n",
437 rs->me_addr,
438 rs->host_addr,
439 rs->status);
441 list_for_each_entry_safe(cb_pos, cb_next,
442 &dev->ctrl_rd_list.mei_cb.cb_list, cb_list) {
443 cl = (struct mei_cl *)cb_pos->file_private;
445 if (!cl) {
446 list_del(&cb_pos->cb_list);
447 return;
450 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
451 if (cl->host_client_id == rs->host_addr &&
452 cl->me_client_id == rs->me_addr) {
454 list_del(&cb_pos->cb_list);
455 if (!rs->status)
456 cl->state = MEI_FILE_DISCONNECTED;
458 cl->status = 0;
459 cl->timer_count = 0;
460 break;
466 * same_flow_addr - tells if they have the same address.
468 * @file: private data of the file object.
469 * @flow: flow control.
471 * returns !=0, same; 0,not.
473 static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
475 return (cl->host_client_id == flow->host_addr &&
476 cl->me_client_id == flow->me_addr);
480 * add_single_flow_creds - adds single buffer credentials.
482 * @file: private data ot the file object.
483 * @flow: flow control.
485 static void add_single_flow_creds(struct mei_device *dev,
486 struct hbm_flow_control *flow)
488 struct mei_me_client *client;
489 int i;
491 for (i = 0; i < dev->me_clients_num; i++) {
492 client = &dev->me_clients[i];
493 if (client && flow->me_addr == client->client_id) {
494 if (client->props.single_recv_buf) {
495 client->mei_flow_ctrl_creds++;
496 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
497 flow->me_addr);
498 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
499 client->mei_flow_ctrl_creds);
500 } else {
501 BUG(); /* error in flow control */
508 * mei_client_flow_control_response - flow control response irq routine
510 * @dev: the device structure
511 * @flow_control: flow control response bus message
513 static void mei_client_flow_control_response(struct mei_device *dev,
514 struct hbm_flow_control *flow_control)
516 struct mei_cl *cl_pos = NULL;
517 struct mei_cl *cl_next = NULL;
519 if (!flow_control->host_addr) {
520 /* single receive buffer */
521 add_single_flow_creds(dev, flow_control);
522 } else {
523 /* normal connection */
524 list_for_each_entry_safe(cl_pos, cl_next,
525 &dev->file_list, link) {
526 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
528 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
529 cl_pos->host_client_id,
530 cl_pos->me_client_id);
531 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
532 flow_control->host_addr,
533 flow_control->me_addr);
534 if (same_flow_addr(cl_pos, flow_control)) {
535 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
536 flow_control->host_addr,
537 flow_control->me_addr);
538 cl_pos->mei_flow_ctrl_creds++;
539 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
540 cl_pos->mei_flow_ctrl_creds);
541 break;
548 * same_disconn_addr - tells if they have the same address
550 * @file: private data of the file object.
551 * @disconn: disconnection request.
553 * returns !=0, same; 0,not.
555 static int same_disconn_addr(struct mei_cl *cl,
556 struct hbm_client_disconnect_request *disconn)
558 return (cl->host_client_id == disconn->host_addr &&
559 cl->me_client_id == disconn->me_addr);
563 * mei_client_disconnect_request - disconnects from request irq routine
565 * @dev: the device structure.
566 * @disconnect_req: disconnect request bus message.
568 static void mei_client_disconnect_request(struct mei_device *dev,
569 struct hbm_client_disconnect_request *disconnect_req)
571 struct mei_msg_hdr *mei_hdr;
572 struct hbm_client_connect_response *disconnect_res;
573 struct mei_cl *cl_pos = NULL;
574 struct mei_cl *cl_next = NULL;
576 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
577 if (same_disconn_addr(cl_pos, disconnect_req)) {
578 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
579 disconnect_req->host_addr,
580 disconnect_req->me_addr);
581 cl_pos->state = MEI_FILE_DISCONNECTED;
582 cl_pos->timer_count = 0;
583 if (cl_pos == &dev->wd_cl) {
584 dev->wd_due_counter = 0;
585 dev->wd_pending = false;
586 } else if (cl_pos == &dev->iamthif_cl)
587 dev->iamthif_timer = 0;
589 /* prepare disconnect response */
590 mei_hdr =
591 (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
592 mei_hdr->host_addr = 0;
593 mei_hdr->me_addr = 0;
594 mei_hdr->length =
595 sizeof(struct hbm_client_connect_response);
596 mei_hdr->msg_complete = 1;
597 mei_hdr->reserved = 0;
599 disconnect_res =
600 (struct hbm_client_connect_response *)
601 &dev->ext_msg_buf[1];
602 disconnect_res->host_addr = cl_pos->host_client_id;
603 disconnect_res->me_addr = cl_pos->me_client_id;
604 *(u8 *) (&disconnect_res->cmd) =
605 CLIENT_DISCONNECT_RES_CMD;
606 disconnect_res->status = 0;
607 dev->extra_write_index = 2;
608 break;
615 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
616 * handle the read bus message cmd processing.
618 * @dev: the device structure
619 * @mei_hdr: header of bus message
621 static void mei_irq_thread_read_bus_message(struct mei_device *dev,
622 struct mei_msg_hdr *mei_hdr)
624 struct mei_bus_message *mei_msg;
625 struct hbm_host_version_response *version_res;
626 struct hbm_client_connect_response *connect_res;
627 struct hbm_client_connect_response *disconnect_res;
628 struct hbm_flow_control *flow_control;
629 struct hbm_props_response *props_res;
630 struct hbm_host_enum_response *enum_res;
631 struct hbm_client_disconnect_request *disconnect_req;
632 struct hbm_host_stop_request *host_stop_req;
633 int res;
635 unsigned char *buffer;
637 /* read the message to our buffer */
638 buffer = (unsigned char *) dev->rd_msg_buf;
639 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
640 mei_read_slots(dev, buffer, mei_hdr->length);
641 mei_msg = (struct mei_bus_message *) buffer;
643 switch (*(u8 *) mei_msg) {
644 case HOST_START_RES_CMD:
645 version_res = (struct hbm_host_version_response *) mei_msg;
646 if (version_res->host_version_supported) {
647 dev->version.major_version = HBM_MAJOR_VERSION;
648 dev->version.minor_version = HBM_MINOR_VERSION;
649 if (dev->mei_state == MEI_INIT_CLIENTS &&
650 dev->init_clients_state == MEI_START_MESSAGE) {
651 dev->init_clients_timer = 0;
652 mei_host_enum_clients_message(dev);
653 } else {
654 dev->recvd_msg = false;
655 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
656 mei_reset(dev, 1);
657 return;
659 } else {
660 dev->version = version_res->me_max_version;
661 /* send stop message */
662 mei_hdr->host_addr = 0;
663 mei_hdr->me_addr = 0;
664 mei_hdr->length = sizeof(struct hbm_host_stop_request);
665 mei_hdr->msg_complete = 1;
666 mei_hdr->reserved = 0;
668 host_stop_req = (struct hbm_host_stop_request *)
669 &dev->wr_msg_buf[1];
671 memset(host_stop_req,
673 sizeof(struct hbm_host_stop_request));
674 host_stop_req->cmd.cmd = HOST_STOP_REQ_CMD;
675 host_stop_req->reason = DRIVER_STOP_REQUEST;
676 mei_write_message(dev, mei_hdr,
677 (unsigned char *) (host_stop_req),
678 mei_hdr->length);
679 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
680 return;
683 dev->recvd_msg = true;
684 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
685 break;
687 case CLIENT_CONNECT_RES_CMD:
688 connect_res =
689 (struct hbm_client_connect_response *) mei_msg;
690 mei_client_connect_response(dev, connect_res);
691 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
692 wake_up(&dev->wait_recvd_msg);
693 break;
695 case CLIENT_DISCONNECT_RES_CMD:
696 disconnect_res =
697 (struct hbm_client_connect_response *) mei_msg;
698 mei_client_disconnect_response(dev, disconnect_res);
699 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
700 wake_up(&dev->wait_recvd_msg);
701 break;
703 case MEI_FLOW_CONTROL_CMD:
704 flow_control = (struct hbm_flow_control *) mei_msg;
705 mei_client_flow_control_response(dev, flow_control);
706 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
707 break;
709 case HOST_CLIENT_PROPERTIES_RES_CMD:
710 props_res = (struct hbm_props_response *)mei_msg;
711 if (props_res->status || !dev->me_clients) {
712 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
713 mei_reset(dev, 1);
714 return;
716 if (dev->me_clients[dev->me_client_presentation_num]
717 .client_id == props_res->address) {
719 dev->me_clients[dev->me_client_presentation_num].props
720 = props_res->client_properties;
722 if (dev->mei_state == MEI_INIT_CLIENTS &&
723 dev->init_clients_state ==
724 MEI_CLIENT_PROPERTIES_MESSAGE) {
725 dev->me_client_index++;
726 dev->me_client_presentation_num++;
728 /** Send Client Propeties request **/
729 res = mei_host_client_properties(dev);
730 if (res < 0) {
731 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
732 return;
733 } else if (!res) {
735 * No more clients to send to.
736 * Clear Map for indicating now ME clients
737 * with associated host client
739 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
740 dev->open_handle_count = 0;
743 * Reserving the first three client IDs
744 * Client Id 0 - Reserved for MEI Bus Message communications
745 * Client Id 1 - Reserved for Watchdog
746 * Client ID 2 - Reserved for AMTHI
748 bitmap_set(dev->host_clients_map, 0, 3);
749 dev->mei_state = MEI_ENABLED;
751 /* if wd initialization fails, initialization the AMTHI client,
752 * otherwise the AMTHI client will be initialized after the WD client connect response
753 * will be received
755 if (mei_wd_host_init(dev))
756 mei_host_init_iamthif(dev);
759 } else {
760 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
761 mei_reset(dev, 1);
762 return;
764 } else {
765 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
766 mei_reset(dev, 1);
767 return;
769 break;
771 case HOST_ENUM_RES_CMD:
772 enum_res = (struct hbm_host_enum_response *) mei_msg;
773 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
774 if (dev->mei_state == MEI_INIT_CLIENTS &&
775 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
776 dev->init_clients_timer = 0;
777 dev->me_client_presentation_num = 0;
778 dev->me_client_index = 0;
779 mei_allocate_me_clients_storage(dev);
780 dev->init_clients_state =
781 MEI_CLIENT_PROPERTIES_MESSAGE;
782 mei_host_client_properties(dev);
783 } else {
784 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
785 mei_reset(dev, 1);
786 return;
788 break;
790 case HOST_STOP_RES_CMD:
791 dev->mei_state = MEI_DISABLED;
792 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
793 mei_reset(dev, 1);
794 break;
796 case CLIENT_DISCONNECT_REQ_CMD:
797 /* search for client */
798 disconnect_req =
799 (struct hbm_client_disconnect_request *) mei_msg;
800 mei_client_disconnect_request(dev, disconnect_req);
801 break;
803 case ME_STOP_REQ_CMD:
804 /* prepare stop request */
805 mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
806 mei_hdr->host_addr = 0;
807 mei_hdr->me_addr = 0;
808 mei_hdr->length = sizeof(struct hbm_host_stop_request);
809 mei_hdr->msg_complete = 1;
810 mei_hdr->reserved = 0;
811 host_stop_req =
812 (struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
813 memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request));
814 host_stop_req->cmd.cmd = HOST_STOP_REQ_CMD;
815 host_stop_req->reason = DRIVER_STOP_REQUEST;
816 host_stop_req->reserved[0] = 0;
817 host_stop_req->reserved[1] = 0;
818 dev->extra_write_index = 2;
819 break;
821 default:
822 BUG();
823 break;
830 * _mei_hb_read - processes read related operation.
832 * @dev: the device structure.
833 * @slots: free slots.
834 * @cb_pos: callback block.
835 * @cl: private data of the file object.
836 * @cmpl_list: complete list.
838 * returns 0, OK; otherwise, error.
840 static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
841 struct mei_cl_cb *cb_pos,
842 struct mei_cl *cl,
843 struct mei_io_list *cmpl_list)
845 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
846 sizeof(struct hbm_flow_control))) {
847 *slots -= (sizeof(struct mei_msg_hdr) +
848 sizeof(struct hbm_flow_control) + 3) / 4;
849 if (!mei_send_flow_control(dev, cl)) {
850 cl->status = -ENODEV;
851 cb_pos->information = 0;
852 list_move_tail(&cb_pos->cb_list,
853 &cmpl_list->mei_cb.cb_list);
854 return -ENODEV;
855 } else {
856 list_move_tail(&cb_pos->cb_list,
857 &dev->read_list.mei_cb.cb_list);
859 } else {
860 /* return the cancel routine */
861 list_del(&cb_pos->cb_list);
862 return -EBADMSG;
865 return 0;
870 * _mei_irq_thread_ioctl - processes ioctl related operation.
872 * @dev: the device structure.
873 * @slots: free slots.
874 * @cb_pos: callback block.
875 * @cl: private data of the file object.
876 * @cmpl_list: complete list.
878 * returns 0, OK; otherwise, error.
880 static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
881 struct mei_cl_cb *cb_pos,
882 struct mei_cl *cl,
883 struct mei_io_list *cmpl_list)
885 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
886 sizeof(struct hbm_client_connect_request))) {
887 cl->state = MEI_FILE_CONNECTING;
888 *slots -= (sizeof(struct mei_msg_hdr) +
889 sizeof(struct hbm_client_connect_request) + 3) / 4;
890 if (!mei_connect(dev, cl)) {
891 cl->status = -ENODEV;
892 cb_pos->information = 0;
893 list_del(&cb_pos->cb_list);
894 return -ENODEV;
895 } else {
896 list_move_tail(&cb_pos->cb_list,
897 &dev->ctrl_rd_list.mei_cb.cb_list);
898 cl->timer_count = MEI_CONNECT_TIMEOUT;
900 } else {
901 /* return the cancel routine */
902 list_del(&cb_pos->cb_list);
903 return -EBADMSG;
906 return 0;
910 * _mei_irq_thread_cmpl - processes completed and no-iamthif operation.
912 * @dev: the device structure.
913 * @slots: free slots.
914 * @cb_pos: callback block.
915 * @cl: private data of the file object.
916 * @cmpl_list: complete list.
918 * returns 0, OK; otherwise, error.
920 static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots,
921 struct mei_cl_cb *cb_pos,
922 struct mei_cl *cl,
923 struct mei_io_list *cmpl_list)
925 struct mei_msg_hdr *mei_hdr;
927 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
928 (cb_pos->request_buffer.size -
929 cb_pos->information))) {
930 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
931 mei_hdr->host_addr = cl->host_client_id;
932 mei_hdr->me_addr = cl->me_client_id;
933 mei_hdr->length = cb_pos->request_buffer.size -
934 cb_pos->information;
935 mei_hdr->msg_complete = 1;
936 mei_hdr->reserved = 0;
937 dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d"
938 "mei_hdr->msg_complete = %d\n",
939 cb_pos->request_buffer.size,
940 mei_hdr->msg_complete);
941 dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
942 cb_pos->information);
943 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
944 mei_hdr->length);
945 *slots -= (sizeof(struct mei_msg_hdr) +
946 mei_hdr->length + 3) / 4;
947 if (!mei_write_message(dev, mei_hdr,
948 (unsigned char *)
949 (cb_pos->request_buffer.data +
950 cb_pos->information),
951 mei_hdr->length)) {
952 cl->status = -ENODEV;
953 list_move_tail(&cb_pos->cb_list,
954 &cmpl_list->mei_cb.cb_list);
955 return -ENODEV;
956 } else {
957 if (mei_flow_ctrl_reduce(dev, cl))
958 return -ENODEV;
959 cl->status = 0;
960 cb_pos->information += mei_hdr->length;
961 list_move_tail(&cb_pos->cb_list,
962 &dev->write_waiting_list.mei_cb.cb_list);
964 } else if (*slots == ((dev->host_hw_state & H_CBD) >> 24)) {
965 /* buffer is still empty */
966 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
967 mei_hdr->host_addr = cl->host_client_id;
968 mei_hdr->me_addr = cl->me_client_id;
969 mei_hdr->length =
970 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
971 mei_hdr->msg_complete = 0;
972 mei_hdr->reserved = 0;
974 (*slots) -= (sizeof(struct mei_msg_hdr) +
975 mei_hdr->length + 3) / 4;
976 if (!mei_write_message(dev, mei_hdr,
977 (unsigned char *)
978 (cb_pos->request_buffer.data +
979 cb_pos->information),
980 mei_hdr->length)) {
981 cl->status = -ENODEV;
982 list_move_tail(&cb_pos->cb_list,
983 &cmpl_list->mei_cb.cb_list);
984 return -ENODEV;
985 } else {
986 cb_pos->information += mei_hdr->length;
987 dev_dbg(&dev->pdev->dev,
988 "cb_pos->request_buffer.size =%d"
989 " mei_hdr->msg_complete = %d\n",
990 cb_pos->request_buffer.size,
991 mei_hdr->msg_complete);
992 dev_dbg(&dev->pdev->dev, "cb_pos->information =%lu\n",
993 cb_pos->information);
994 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
995 mei_hdr->length);
997 return -EMSGSIZE;
998 } else {
999 return -EBADMSG;
1002 return 0;
1006 * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation.
1008 * @dev: the device structure.
1009 * @slots: free slots.
1010 * @cb_pos: callback block.
1011 * @cl: private data of the file object.
1012 * @cmpl_list: complete list.
1014 * returns 0, OK; otherwise, error.
1016 static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots,
1017 struct mei_cl_cb *cb_pos,
1018 struct mei_cl *cl,
1019 struct mei_io_list *cmpl_list)
1021 struct mei_msg_hdr *mei_hdr;
1023 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
1024 dev->iamthif_msg_buf_size -
1025 dev->iamthif_msg_buf_index)) {
1026 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1027 mei_hdr->host_addr = cl->host_client_id;
1028 mei_hdr->me_addr = cl->me_client_id;
1029 mei_hdr->length = dev->iamthif_msg_buf_size -
1030 dev->iamthif_msg_buf_index;
1031 mei_hdr->msg_complete = 1;
1032 mei_hdr->reserved = 0;
1034 *slots -= (sizeof(struct mei_msg_hdr) +
1035 mei_hdr->length + 3) / 4;
1037 if (!mei_write_message(dev, mei_hdr,
1038 (dev->iamthif_msg_buf +
1039 dev->iamthif_msg_buf_index),
1040 mei_hdr->length)) {
1041 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1042 cl->status = -ENODEV;
1043 list_del(&cb_pos->cb_list);
1044 return -ENODEV;
1045 } else {
1046 if (mei_flow_ctrl_reduce(dev, cl))
1047 return -ENODEV;
1048 dev->iamthif_msg_buf_index += mei_hdr->length;
1049 cb_pos->information = dev->iamthif_msg_buf_index;
1050 cl->status = 0;
1051 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
1052 dev->iamthif_flow_control_pending = true;
1053 /* save iamthif cb sent to amthi client */
1054 dev->iamthif_current_cb = cb_pos;
1055 list_move_tail(&cb_pos->cb_list,
1056 &dev->write_waiting_list.mei_cb.cb_list);
1059 } else if (*slots == ((dev->host_hw_state & H_CBD) >> 24)) {
1060 /* buffer is still empty */
1061 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1062 mei_hdr->host_addr = cl->host_client_id;
1063 mei_hdr->me_addr = cl->me_client_id;
1064 mei_hdr->length =
1065 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1066 mei_hdr->msg_complete = 0;
1067 mei_hdr->reserved = 0;
1069 *slots -= (sizeof(struct mei_msg_hdr) +
1070 mei_hdr->length + 3) / 4;
1072 if (!mei_write_message(dev, mei_hdr,
1073 (dev->iamthif_msg_buf +
1074 dev->iamthif_msg_buf_index),
1075 mei_hdr->length)) {
1076 cl->status = -ENODEV;
1077 list_del(&cb_pos->cb_list);
1078 } else {
1079 dev->iamthif_msg_buf_index += mei_hdr->length;
1081 return -EMSGSIZE;
1082 } else {
1083 return -EBADMSG;
1086 return 0;
1090 * mei_irq_thread_read_handler - bottom half read routine after ISR to
1091 * handle the read processing.
1093 * @cmpl_list: An instance of our list structure
1094 * @dev: the device structure
1095 * @slots: slots to read.
1097 * returns 0 on success, <0 on failure.
1099 static int mei_irq_thread_read_handler(struct mei_io_list *cmpl_list,
1100 struct mei_device *dev,
1101 s32 *slots)
1103 struct mei_msg_hdr *mei_hdr;
1104 struct mei_cl *cl_pos = NULL;
1105 struct mei_cl *cl_next = NULL;
1106 int ret = 0;
1108 if (!dev->rd_msg_hdr) {
1109 dev->rd_msg_hdr = mei_mecbrw_read(dev);
1110 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1111 (*slots)--;
1112 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1114 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
1115 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
1117 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
1118 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
1119 ret = -EBADMSG;
1120 goto end;
1123 if (mei_hdr->host_addr || mei_hdr->me_addr) {
1124 list_for_each_entry_safe(cl_pos, cl_next,
1125 &dev->file_list, link) {
1126 dev_dbg(&dev->pdev->dev,
1127 "list_for_each_entry_safe read host"
1128 " client = %d, ME client = %d\n",
1129 cl_pos->host_client_id,
1130 cl_pos->me_client_id);
1131 if (cl_pos->host_client_id == mei_hdr->host_addr &&
1132 cl_pos->me_client_id == mei_hdr->me_addr)
1133 break;
1136 if (&cl_pos->link == &dev->file_list) {
1137 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
1138 ret = -EBADMSG;
1139 goto end;
1142 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
1143 dev_dbg(&dev->pdev->dev,
1144 "we can't read the message slots =%08x.\n",
1145 *slots);
1146 /* we can't read the message */
1147 ret = -ERANGE;
1148 goto end;
1151 /* decide where to read the message too */
1152 if (!mei_hdr->host_addr) {
1153 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
1154 mei_irq_thread_read_bus_message(dev, mei_hdr);
1155 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
1156 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
1157 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
1158 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
1159 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
1160 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
1161 mei_hdr->length);
1162 ret = mei_irq_thread_read_amthi_message(cmpl_list,
1163 dev, mei_hdr);
1164 if (ret)
1165 goto end;
1167 } else {
1168 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
1169 ret = mei_irq_thread_read_client_message(cmpl_list,
1170 dev, mei_hdr);
1171 if (ret)
1172 goto end;
1176 /* reset the number of slots and header */
1177 *slots = mei_count_full_read_slots(dev);
1178 dev->rd_msg_hdr = 0;
1180 if (*slots == -EOVERFLOW) {
1181 /* overflow - reset */
1182 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
1183 /* set the event since message has been read */
1184 ret = -ERANGE;
1185 goto end;
1187 end:
1188 return ret;
1193 * mei_irq_thread_write_handler - bottom half write routine after
1194 * ISR to handle the write processing.
1196 * @cmpl_list: An instance of our list structure
1197 * @dev: the device structure
1198 * @slots: slots to write.
1200 * returns 0 on success, <0 on failure.
1202 static int mei_irq_thread_write_handler(struct mei_io_list *cmpl_list,
1203 struct mei_device *dev,
1204 s32 *slots)
1207 struct mei_cl *cl;
1208 struct mei_cl_cb *pos = NULL, *next = NULL;
1209 struct mei_io_list *list;
1210 int ret;
1212 if (!mei_host_buffer_is_empty(dev)) {
1213 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
1214 return 0;
1216 *slots = mei_count_empty_write_slots(dev);
1217 /* complete all waiting for write CB */
1218 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
1220 list = &dev->write_waiting_list;
1221 list_for_each_entry_safe(pos, next,
1222 &list->mei_cb.cb_list, cb_list) {
1223 cl = (struct mei_cl *)pos->file_private;
1224 if (cl == NULL)
1225 continue;
1227 cl->status = 0;
1228 list_del(&pos->cb_list);
1229 if (MEI_WRITING == cl->writing_state &&
1230 (pos->major_file_operations == MEI_WRITE) &&
1231 (cl != &dev->iamthif_cl)) {
1232 dev_dbg(&dev->pdev->dev,
1233 "MEI WRITE COMPLETE\n");
1234 cl->writing_state = MEI_WRITE_COMPLETE;
1235 list_add_tail(&pos->cb_list,
1236 &cmpl_list->mei_cb.cb_list);
1238 if (cl == &dev->iamthif_cl) {
1239 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
1240 if (dev->iamthif_flow_control_pending) {
1241 ret = _mei_irq_thread_iamthif_read(
1242 dev, slots);
1243 if (ret)
1244 return ret;
1249 if (dev->stop && !dev->wd_pending) {
1250 dev->wd_stopped = true;
1251 wake_up_interruptible(&dev->wait_stop_wd);
1252 return 0;
1255 if (dev->extra_write_index) {
1256 dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n",
1257 dev->extra_write_index);
1258 mei_write_message(dev,
1259 (struct mei_msg_hdr *) &dev->ext_msg_buf[0],
1260 (unsigned char *) &dev->ext_msg_buf[1],
1261 (dev->extra_write_index - 1) * sizeof(u32));
1262 *slots -= dev->extra_write_index;
1263 dev->extra_write_index = 0;
1265 if (dev->mei_state == MEI_ENABLED) {
1266 if (dev->wd_pending &&
1267 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
1268 if (mei_wd_send(dev))
1269 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
1270 else
1271 if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
1272 return -ENODEV;
1274 dev->wd_pending = false;
1276 if (dev->wd_timeout) {
1277 *slots -= (sizeof(struct mei_msg_hdr) +
1278 MEI_START_WD_DATA_SIZE + 3) / 4;
1279 dev->wd_due_counter = 2;
1280 } else {
1281 *slots -= (sizeof(struct mei_msg_hdr) +
1282 MEI_WD_PARAMS_SIZE + 3) / 4;
1283 dev->wd_due_counter = 0;
1288 if (dev->stop)
1289 return ~ENODEV;
1291 /* complete control write list CB */
1292 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
1293 list_for_each_entry_safe(pos, next,
1294 &dev->ctrl_wr_list.mei_cb.cb_list, cb_list) {
1295 cl = (struct mei_cl *) pos->file_private;
1296 if (!cl) {
1297 list_del(&pos->cb_list);
1298 return -ENODEV;
1300 switch (pos->major_file_operations) {
1301 case MEI_CLOSE:
1302 /* send disconnect message */
1303 ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
1304 if (ret)
1305 return ret;
1307 break;
1308 case MEI_READ:
1309 /* send flow control message */
1310 ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
1311 if (ret)
1312 return ret;
1314 break;
1315 case MEI_IOCTL:
1316 /* connect message */
1317 if (mei_other_client_is_connecting(dev, cl))
1318 continue;
1319 ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
1320 if (ret)
1321 return ret;
1323 break;
1325 default:
1326 BUG();
1330 /* complete write list CB */
1331 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
1332 list_for_each_entry_safe(pos, next,
1333 &dev->write_list.mei_cb.cb_list, cb_list) {
1334 cl = (struct mei_cl *)pos->file_private;
1335 if (cl == NULL)
1336 continue;
1338 if (cl != &dev->iamthif_cl) {
1339 if (!mei_flow_ctrl_creds(dev, cl)) {
1340 dev_dbg(&dev->pdev->dev,
1341 "No flow control"
1342 " credentials for client"
1343 " %d, not sending.\n",
1344 cl->host_client_id);
1345 continue;
1347 ret = _mei_irq_thread_cmpl(dev, slots,
1348 pos,
1349 cl, cmpl_list);
1350 if (ret)
1351 return ret;
1353 } else if (cl == &dev->iamthif_cl) {
1354 /* IAMTHIF IOCTL */
1355 dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
1356 if (!mei_flow_ctrl_creds(dev, cl)) {
1357 dev_dbg(&dev->pdev->dev,
1358 "No flow control"
1359 " credentials for amthi"
1360 " client %d.\n",
1361 cl->host_client_id);
1362 continue;
1364 ret = _mei_irq_thread_cmpl_iamthif(dev,
1365 slots,
1366 pos,
1368 cmpl_list);
1369 if (ret)
1370 return ret;
1375 return 0;
1381 * mei_timer - timer function.
1383 * @work: pointer to the work_struct structure
1385 * NOTE: This function is called by timer interrupt work
1387 void mei_timer(struct work_struct *work)
1389 unsigned long timeout;
1390 struct mei_cl *cl_pos = NULL;
1391 struct mei_cl *cl_next = NULL;
1392 struct list_head *amthi_complete_list = NULL;
1393 struct mei_cl_cb *cb_pos = NULL;
1394 struct mei_cl_cb *cb_next = NULL;
1396 struct mei_device *dev = container_of(work,
1397 struct mei_device, timer_work.work);
1400 mutex_lock(&dev->device_lock);
1401 if (dev->mei_state != MEI_ENABLED) {
1402 if (dev->mei_state == MEI_INIT_CLIENTS) {
1403 if (dev->init_clients_timer) {
1404 if (--dev->init_clients_timer == 0) {
1405 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1406 dev->init_clients_state);
1407 mei_reset(dev, 1);
1411 goto out;
1413 /*** connect/disconnect timeouts ***/
1414 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1415 if (cl_pos->timer_count) {
1416 if (--cl_pos->timer_count == 0) {
1417 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1418 mei_reset(dev, 1);
1419 goto out;
1424 if (dev->iamthif_stall_timer) {
1425 if (--dev->iamthif_stall_timer == 0) {
1426 dev_dbg(&dev->pdev->dev, "reseting because of hang to amthi.\n");
1427 mei_reset(dev, 1);
1428 dev->iamthif_msg_buf_size = 0;
1429 dev->iamthif_msg_buf_index = 0;
1430 dev->iamthif_canceled = false;
1431 dev->iamthif_ioctl = true;
1432 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1433 dev->iamthif_timer = 0;
1435 if (dev->iamthif_current_cb)
1436 mei_free_cb_private(dev->iamthif_current_cb);
1438 dev->iamthif_file_object = NULL;
1439 dev->iamthif_current_cb = NULL;
1440 mei_run_next_iamthif_cmd(dev);
1444 if (dev->iamthif_timer) {
1446 timeout = dev->iamthif_timer +
1447 msecs_to_jiffies(IAMTHIF_READ_TIMER);
1449 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1450 dev->iamthif_timer);
1451 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1452 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1453 if (time_after(jiffies, timeout)) {
1455 * User didn't read the AMTHI data on time (15sec)
1456 * freeing AMTHI for other requests
1459 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1461 amthi_complete_list = &dev->amthi_read_complete_list.
1462 mei_cb.cb_list;
1464 list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, cb_list) {
1466 cl_pos = cb_pos->file_object->private_data;
1468 /* Finding the AMTHI entry. */
1469 if (cl_pos == &dev->iamthif_cl)
1470 list_del(&cb_pos->cb_list);
1472 if (dev->iamthif_current_cb)
1473 mei_free_cb_private(dev->iamthif_current_cb);
1475 dev->iamthif_file_object->private_data = NULL;
1476 dev->iamthif_file_object = NULL;
1477 dev->iamthif_current_cb = NULL;
1478 dev->iamthif_timer = 0;
1479 mei_run_next_iamthif_cmd(dev);
1483 out:
1484 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1485 mutex_unlock(&dev->device_lock);
1489 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1490 * processing.
1492 * @irq: The irq number
1493 * @dev_id: pointer to the device structure
1495 * returns irqreturn_t
1498 irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1500 struct mei_device *dev = (struct mei_device *) dev_id;
1501 struct mei_io_list complete_list;
1502 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1503 struct mei_cl *cl;
1504 s32 slots;
1505 int rets;
1506 bool bus_message_received;
1509 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1510 /* initialize our complete list */
1511 mutex_lock(&dev->device_lock);
1512 mei_io_list_init(&complete_list);
1513 dev->host_hw_state = mei_hcsr_read(dev);
1515 /* Ack the interrupt here
1516 * In case of MSI we don't go throuhg the quick handler */
1517 if (pci_dev_msi_enabled(dev->pdev))
1518 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1520 dev->me_hw_state = mei_mecsr_read(dev);
1522 /* check if ME wants a reset */
1523 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
1524 dev->mei_state != MEI_RESETING &&
1525 dev->mei_state != MEI_INITIALIZING) {
1526 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1527 mei_reset(dev, 1);
1528 mutex_unlock(&dev->device_lock);
1529 return IRQ_HANDLED;
1532 /* check if we need to start the dev */
1533 if ((dev->host_hw_state & H_RDY) == 0) {
1534 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1535 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1536 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1537 mei_hcsr_set(dev);
1538 dev->mei_state = MEI_INIT_CLIENTS;
1539 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1540 /* link is established
1541 * start sending messages.
1543 mei_host_start_message(dev);
1544 mutex_unlock(&dev->device_lock);
1545 return IRQ_HANDLED;
1546 } else {
1547 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1548 mutex_unlock(&dev->device_lock);
1549 return IRQ_HANDLED;
1552 /* check slots avalable for reading */
1553 slots = mei_count_full_read_slots(dev);
1554 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1555 slots, dev->extra_write_index);
1556 while (slots > 0 && !dev->extra_write_index) {
1557 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1558 slots, dev->extra_write_index);
1559 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1560 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1561 if (rets)
1562 goto end;
1564 rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
1565 end:
1566 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1567 dev->host_hw_state = mei_hcsr_read(dev);
1568 dev->mei_host_buffer_is_empty = mei_host_buffer_is_empty(dev);
1570 bus_message_received = false;
1571 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1572 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1573 bus_message_received = true;
1575 mutex_unlock(&dev->device_lock);
1576 if (bus_message_received) {
1577 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1578 wake_up_interruptible(&dev->wait_recvd_msg);
1579 bus_message_received = false;
1581 if (list_empty(&complete_list.mei_cb.cb_list))
1582 return IRQ_HANDLED;
1585 list_for_each_entry_safe(cb_pos, cb_next,
1586 &complete_list.mei_cb.cb_list, cb_list) {
1587 cl = (struct mei_cl *)cb_pos->file_private;
1588 list_del(&cb_pos->cb_list);
1589 if (cl) {
1590 if (cl != &dev->iamthif_cl) {
1591 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1592 _mei_cmpl(cl, cb_pos);
1593 cb_pos = NULL;
1594 } else if (cl == &dev->iamthif_cl) {
1595 _mei_cmpl_iamthif(dev, cb_pos);
1599 return IRQ_HANDLED;