mei: simplify write complete loop in irq handler
[linux-2.6.git] / drivers / misc / mei / interrupt.c
blobe5aa0ed3b8eb849cd9d03cc7bc8f68a2d9b0ed65
1 /*
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
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 <linux/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->fop_type == MEI_FOP_WRITE) {
61 mei_io_cb_free(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->fop_type == MEI_FOP_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_irq_thread_state_ok - checks if mei header matches file private data
79 * @cl: private data of the file object
80 * @mei_hdr: header of mei client message
82 * returns !=0 if matches, 0 if no match.
84 static int _mei_irq_thread_state_ok(struct mei_cl *cl,
85 struct mei_msg_hdr *mei_hdr)
87 return (cl->host_client_id == mei_hdr->host_addr &&
88 cl->me_client_id == mei_hdr->me_addr &&
89 cl->state == MEI_FILE_CONNECTED &&
90 MEI_READ_COMPLETE != cl->reading_state);
93 /**
94 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
95 * handle the read mei client message data processing.
97 * @complete_list: An instance of our list structure
98 * @dev: the device structure
99 * @mei_hdr: header of mei client message
101 * returns 0 on success, <0 on failure.
103 static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
104 struct mei_device *dev,
105 struct mei_msg_hdr *mei_hdr)
107 struct mei_cl *cl;
108 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
109 unsigned char *buffer = NULL;
111 dev_dbg(&dev->pdev->dev, "start client msg\n");
112 if (list_empty(&dev->read_list.list))
113 goto quit;
115 list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
116 cl = cb_pos->cl;
117 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
118 cl->reading_state = MEI_READING;
119 buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
121 if (cb_pos->response_buffer.size <
122 mei_hdr->length + cb_pos->buf_idx) {
123 dev_dbg(&dev->pdev->dev, "message overflow.\n");
124 list_del(&cb_pos->list);
125 return -ENOMEM;
127 if (buffer)
128 mei_read_slots(dev, buffer, mei_hdr->length);
130 cb_pos->buf_idx += mei_hdr->length;
131 if (mei_hdr->msg_complete) {
132 cl->status = 0;
133 list_del(&cb_pos->list);
134 dev_dbg(&dev->pdev->dev,
135 "completed read H cl = %d, ME cl = %d, length = %lu\n",
136 cl->host_client_id,
137 cl->me_client_id,
138 cb_pos->buf_idx);
140 list_add_tail(&cb_pos->list,
141 &complete_list->list);
144 break;
149 quit:
150 dev_dbg(&dev->pdev->dev, "message read\n");
151 if (!buffer) {
152 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
153 dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
154 *(u32 *) dev->rd_msg_buf);
157 return 0;
161 * _mei_irq_thread_close - processes close related operation.
163 * @dev: the device structure.
164 * @slots: free slots.
165 * @cb_pos: callback block.
166 * @cl: private data of the file object.
167 * @cmpl_list: complete list.
169 * returns 0, OK; otherwise, error.
171 static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
172 struct mei_cl_cb *cb_pos,
173 struct mei_cl *cl,
174 struct mei_cl_cb *cmpl_list)
176 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
177 sizeof(struct hbm_client_connect_request)))
178 return -EBADMSG;
180 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
182 if (mei_disconnect(dev, cl)) {
183 cl->status = 0;
184 cb_pos->buf_idx = 0;
185 list_move_tail(&cb_pos->list, &cmpl_list->list);
186 return -EMSGSIZE;
187 } else {
188 cl->state = MEI_FILE_DISCONNECTING;
189 cl->status = 0;
190 cb_pos->buf_idx = 0;
191 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
192 cl->timer_count = MEI_CONNECT_TIMEOUT;
195 return 0;
199 * is_treat_specially_client - checks if the message belongs
200 * to the file private data.
202 * @cl: private data of the file object
203 * @rs: connect response bus message
206 static bool is_treat_specially_client(struct mei_cl *cl,
207 struct hbm_client_connect_response *rs)
210 if (cl->host_client_id == rs->host_addr &&
211 cl->me_client_id == rs->me_addr) {
212 if (!rs->status) {
213 cl->state = MEI_FILE_CONNECTED;
214 cl->status = 0;
216 } else {
217 cl->state = MEI_FILE_DISCONNECTED;
218 cl->status = -ENODEV;
220 cl->timer_count = 0;
222 return true;
224 return false;
228 * mei_client_connect_response - connects to response irq routine
230 * @dev: the device structure
231 * @rs: connect response bus message
233 static void mei_client_connect_response(struct mei_device *dev,
234 struct hbm_client_connect_response *rs)
237 struct mei_cl *cl;
238 struct mei_cl_cb *pos = NULL, *next = NULL;
240 dev_dbg(&dev->pdev->dev,
241 "connect_response:\n"
242 "ME Client = %d\n"
243 "Host Client = %d\n"
244 "Status = %d\n",
245 rs->me_addr,
246 rs->host_addr,
247 rs->status);
249 /* if WD or iamthif client treat specially */
251 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
252 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
253 mei_watchdog_register(dev);
255 /* next step in the state maching */
256 mei_amthif_host_init(dev);
257 return;
260 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
261 dev->iamthif_state = MEI_IAMTHIF_IDLE;
262 return;
264 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
266 cl = pos->cl;
267 if (!cl) {
268 list_del(&pos->list);
269 return;
271 if (pos->fop_type == MEI_FOP_IOCTL) {
272 if (is_treat_specially_client(cl, rs)) {
273 list_del(&pos->list);
274 cl->status = 0;
275 cl->timer_count = 0;
276 break;
283 * mei_client_disconnect_response - disconnects from response irq routine
285 * @dev: the device structure
286 * @rs: disconnect response bus message
288 static void mei_client_disconnect_response(struct mei_device *dev,
289 struct hbm_client_connect_response *rs)
291 struct mei_cl *cl;
292 struct mei_cl_cb *pos = NULL, *next = NULL;
294 dev_dbg(&dev->pdev->dev,
295 "disconnect_response:\n"
296 "ME Client = %d\n"
297 "Host Client = %d\n"
298 "Status = %d\n",
299 rs->me_addr,
300 rs->host_addr,
301 rs->status);
303 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
304 cl = pos->cl;
306 if (!cl) {
307 list_del(&pos->list);
308 return;
311 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
312 if (cl->host_client_id == rs->host_addr &&
313 cl->me_client_id == rs->me_addr) {
315 list_del(&pos->list);
316 if (!rs->status)
317 cl->state = MEI_FILE_DISCONNECTED;
319 cl->status = 0;
320 cl->timer_count = 0;
321 break;
327 * same_flow_addr - tells if they have the same address.
329 * @file: private data of the file object.
330 * @flow: flow control.
332 * returns !=0, same; 0,not.
334 static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
336 return (cl->host_client_id == flow->host_addr &&
337 cl->me_client_id == flow->me_addr);
341 * add_single_flow_creds - adds single buffer credentials.
343 * @file: private data ot the file object.
344 * @flow: flow control.
346 static void add_single_flow_creds(struct mei_device *dev,
347 struct hbm_flow_control *flow)
349 struct mei_me_client *client;
350 int i;
352 for (i = 0; i < dev->me_clients_num; i++) {
353 client = &dev->me_clients[i];
354 if (client && flow->me_addr == client->client_id) {
355 if (client->props.single_recv_buf) {
356 client->mei_flow_ctrl_creds++;
357 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
358 flow->me_addr);
359 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
360 client->mei_flow_ctrl_creds);
361 } else {
362 BUG(); /* error in flow control */
369 * mei_client_flow_control_response - flow control response irq routine
371 * @dev: the device structure
372 * @flow_control: flow control response bus message
374 static void mei_client_flow_control_response(struct mei_device *dev,
375 struct hbm_flow_control *flow_control)
377 struct mei_cl *cl_pos = NULL;
378 struct mei_cl *cl_next = NULL;
380 if (!flow_control->host_addr) {
381 /* single receive buffer */
382 add_single_flow_creds(dev, flow_control);
383 } else {
384 /* normal connection */
385 list_for_each_entry_safe(cl_pos, cl_next,
386 &dev->file_list, link) {
387 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
389 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
390 cl_pos->host_client_id,
391 cl_pos->me_client_id);
392 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
393 flow_control->host_addr,
394 flow_control->me_addr);
395 if (same_flow_addr(cl_pos, flow_control)) {
396 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
397 flow_control->host_addr,
398 flow_control->me_addr);
399 cl_pos->mei_flow_ctrl_creds++;
400 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
401 cl_pos->mei_flow_ctrl_creds);
402 break;
409 * same_disconn_addr - tells if they have the same address
411 * @file: private data of the file object.
412 * @disconn: disconnection request.
414 * returns !=0, same; 0,not.
416 static int same_disconn_addr(struct mei_cl *cl,
417 struct hbm_client_connect_request *req)
419 return (cl->host_client_id == req->host_addr &&
420 cl->me_client_id == req->me_addr);
424 * mei_client_disconnect_request - disconnects from request irq routine
426 * @dev: the device structure.
427 * @disconnect_req: disconnect request bus message.
429 static void mei_client_disconnect_request(struct mei_device *dev,
430 struct hbm_client_connect_request *disconnect_req)
432 struct hbm_client_connect_response *disconnect_res;
433 struct mei_cl *pos, *next;
434 const size_t len = sizeof(struct hbm_client_connect_response);
436 list_for_each_entry_safe(pos, next, &dev->file_list, link) {
437 if (same_disconn_addr(pos, disconnect_req)) {
438 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
439 disconnect_req->host_addr,
440 disconnect_req->me_addr);
441 pos->state = MEI_FILE_DISCONNECTED;
442 pos->timer_count = 0;
443 if (pos == &dev->wd_cl)
444 dev->wd_pending = false;
445 else if (pos == &dev->iamthif_cl)
446 dev->iamthif_timer = 0;
448 /* prepare disconnect response */
449 (void)mei_hbm_hdr((u32 *)&dev->wr_ext_msg.hdr, len);
450 disconnect_res =
451 (struct hbm_client_connect_response *)
452 &dev->wr_ext_msg.data;
453 disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
454 disconnect_res->host_addr = pos->host_client_id;
455 disconnect_res->me_addr = pos->me_client_id;
456 disconnect_res->status = 0;
457 break;
463 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
464 * handle the read bus message cmd processing.
466 * @dev: the device structure
467 * @mei_hdr: header of bus message
469 static void mei_irq_thread_read_bus_message(struct mei_device *dev,
470 struct mei_msg_hdr *mei_hdr)
472 struct mei_bus_message *mei_msg;
473 struct hbm_host_version_response *version_res;
474 struct hbm_client_connect_response *connect_res;
475 struct hbm_client_connect_response *disconnect_res;
476 struct hbm_client_connect_request *disconnect_req;
477 struct hbm_flow_control *flow_control;
478 struct hbm_props_response *props_res;
479 struct hbm_host_enum_response *enum_res;
480 struct hbm_host_stop_request *stop_req;
481 int res;
484 /* read the message to our buffer */
485 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
486 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
487 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
489 switch (mei_msg->hbm_cmd) {
490 case HOST_START_RES_CMD:
491 version_res = (struct hbm_host_version_response *) mei_msg;
492 if (version_res->host_version_supported) {
493 dev->version.major_version = HBM_MAJOR_VERSION;
494 dev->version.minor_version = HBM_MINOR_VERSION;
495 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
496 dev->init_clients_state == MEI_START_MESSAGE) {
497 dev->init_clients_timer = 0;
498 mei_host_enum_clients_message(dev);
499 } else {
500 dev->recvd_msg = false;
501 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
502 mei_reset(dev, 1);
503 return;
505 } else {
506 u32 *buf = dev->wr_msg_buf;
507 const size_t len = sizeof(struct hbm_host_stop_request);
509 dev->version = version_res->me_max_version;
511 /* send stop message */
512 mei_hdr = mei_hbm_hdr(&buf[0], len);
513 stop_req = (struct hbm_host_stop_request *)&buf[1];
514 memset(stop_req, 0, len);
515 stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
516 stop_req->reason = DRIVER_STOP_REQUEST;
518 mei_write_message(dev, mei_hdr,
519 (unsigned char *)stop_req, len);
520 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
521 return;
524 dev->recvd_msg = true;
525 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
526 break;
528 case CLIENT_CONNECT_RES_CMD:
529 connect_res = (struct hbm_client_connect_response *) mei_msg;
530 mei_client_connect_response(dev, connect_res);
531 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
532 wake_up(&dev->wait_recvd_msg);
533 break;
535 case CLIENT_DISCONNECT_RES_CMD:
536 disconnect_res = (struct hbm_client_connect_response *) mei_msg;
537 mei_client_disconnect_response(dev, disconnect_res);
538 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
539 wake_up(&dev->wait_recvd_msg);
540 break;
542 case MEI_FLOW_CONTROL_CMD:
543 flow_control = (struct hbm_flow_control *) mei_msg;
544 mei_client_flow_control_response(dev, flow_control);
545 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
546 break;
548 case HOST_CLIENT_PROPERTIES_RES_CMD:
549 props_res = (struct hbm_props_response *)mei_msg;
550 if (props_res->status || !dev->me_clients) {
551 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
552 mei_reset(dev, 1);
553 return;
555 if (dev->me_clients[dev->me_client_presentation_num]
556 .client_id == props_res->address) {
558 dev->me_clients[dev->me_client_presentation_num].props
559 = props_res->client_properties;
561 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
562 dev->init_clients_state ==
563 MEI_CLIENT_PROPERTIES_MESSAGE) {
564 dev->me_client_index++;
565 dev->me_client_presentation_num++;
567 /** Send Client Properties request **/
568 res = mei_host_client_properties(dev);
569 if (res < 0) {
570 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
571 return;
572 } else if (!res) {
574 * No more clients to send to.
575 * Clear Map for indicating now ME clients
576 * with associated host client
578 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
579 dev->open_handle_count = 0;
582 * Reserving the first three client IDs
583 * Client Id 0 - Reserved for MEI Bus Message communications
584 * Client Id 1 - Reserved for Watchdog
585 * Client ID 2 - Reserved for AMTHI
587 bitmap_set(dev->host_clients_map, 0, 3);
588 dev->dev_state = MEI_DEV_ENABLED;
590 /* if wd initialization fails, initialization the AMTHI client,
591 * otherwise the AMTHI client will be initialized after the WD client connect response
592 * will be received
594 if (mei_wd_host_init(dev))
595 mei_amthif_host_init(dev);
598 } else {
599 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
600 mei_reset(dev, 1);
601 return;
603 } else {
604 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
605 mei_reset(dev, 1);
606 return;
608 break;
610 case HOST_ENUM_RES_CMD:
611 enum_res = (struct hbm_host_enum_response *) mei_msg;
612 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
613 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
614 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
615 dev->init_clients_timer = 0;
616 dev->me_client_presentation_num = 0;
617 dev->me_client_index = 0;
618 mei_allocate_me_clients_storage(dev);
619 dev->init_clients_state =
620 MEI_CLIENT_PROPERTIES_MESSAGE;
621 mei_host_client_properties(dev);
622 } else {
623 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
624 mei_reset(dev, 1);
625 return;
627 break;
629 case HOST_STOP_RES_CMD:
630 dev->dev_state = MEI_DEV_DISABLED;
631 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
632 mei_reset(dev, 1);
633 break;
635 case CLIENT_DISCONNECT_REQ_CMD:
636 /* search for client */
637 disconnect_req = (struct hbm_client_connect_request *)mei_msg;
638 mei_client_disconnect_request(dev, disconnect_req);
639 break;
641 case ME_STOP_REQ_CMD:
643 /* prepare stop request: sent in next interrupt event */
645 const size_t len = sizeof(struct hbm_host_stop_request);
647 mei_hdr = mei_hbm_hdr((u32 *)&dev->wr_ext_msg.hdr, len);
648 stop_req = (struct hbm_host_stop_request *)&dev->wr_ext_msg.data;
649 memset(stop_req, 0, len);
650 stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
651 stop_req->reason = DRIVER_STOP_REQUEST;
652 break;
654 default:
655 BUG();
656 break;
663 * _mei_hb_read - processes read related operation.
665 * @dev: the device structure.
666 * @slots: free slots.
667 * @cb_pos: callback block.
668 * @cl: private data of the file object.
669 * @cmpl_list: complete list.
671 * returns 0, OK; otherwise, error.
673 static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
674 struct mei_cl_cb *cb_pos,
675 struct mei_cl *cl,
676 struct mei_cl_cb *cmpl_list)
678 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
679 sizeof(struct hbm_flow_control))) {
680 /* return the cancel routine */
681 list_del(&cb_pos->list);
682 return -EBADMSG;
685 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
687 if (mei_send_flow_control(dev, cl)) {
688 cl->status = -ENODEV;
689 cb_pos->buf_idx = 0;
690 list_move_tail(&cb_pos->list, &cmpl_list->list);
691 return -ENODEV;
693 list_move_tail(&cb_pos->list, &dev->read_list.list);
695 return 0;
700 * _mei_irq_thread_ioctl - processes ioctl related operation.
702 * @dev: the device structure.
703 * @slots: free slots.
704 * @cb_pos: callback block.
705 * @cl: private data of the file object.
706 * @cmpl_list: complete list.
708 * returns 0, OK; otherwise, error.
710 static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
711 struct mei_cl_cb *cb_pos,
712 struct mei_cl *cl,
713 struct mei_cl_cb *cmpl_list)
715 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
716 sizeof(struct hbm_client_connect_request))) {
717 /* return the cancel routine */
718 list_del(&cb_pos->list);
719 return -EBADMSG;
722 cl->state = MEI_FILE_CONNECTING;
723 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
724 if (mei_connect(dev, cl)) {
725 cl->status = -ENODEV;
726 cb_pos->buf_idx = 0;
727 list_del(&cb_pos->list);
728 return -ENODEV;
729 } else {
730 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
731 cl->timer_count = MEI_CONNECT_TIMEOUT;
733 return 0;
737 * mei_irq_thread_write_complete - write messages to device.
739 * @dev: the device structure.
740 * @slots: free slots.
741 * @cb: callback block.
742 * @cmpl_list: complete list.
744 * returns 0, OK; otherwise, error.
746 static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
747 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
749 struct mei_msg_hdr *mei_hdr;
750 struct mei_cl *cl = cb->cl;
751 size_t len = cb->request_buffer.size - cb->buf_idx;
752 size_t msg_slots = mei_data2slots(len);
754 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
755 mei_hdr->host_addr = cl->host_client_id;
756 mei_hdr->me_addr = cl->me_client_id;
757 mei_hdr->reserved = 0;
759 if (*slots >= msg_slots) {
760 mei_hdr->length = len;
761 mei_hdr->msg_complete = 1;
762 /* Split the message only if we can write the whole host buffer */
763 } else if (*slots == dev->hbuf_depth) {
764 msg_slots = *slots;
765 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
766 mei_hdr->length = len;
767 mei_hdr->msg_complete = 0;
768 } else {
769 /* wait for next time the host buffer is empty */
770 return 0;
773 dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
774 cb->request_buffer.size, cb->buf_idx);
775 dev_dbg(&dev->pdev->dev, "msg: len = %d complete = %d\n",
776 mei_hdr->length, mei_hdr->msg_complete);
778 *slots -= msg_slots;
779 if (mei_write_message(dev, mei_hdr,
780 cb->request_buffer.data + cb->buf_idx, len)) {
781 cl->status = -ENODEV;
782 list_move_tail(&cb->list, &cmpl_list->list);
783 return -ENODEV;
786 if (mei_flow_ctrl_reduce(dev, cl))
787 return -ENODEV;
789 cl->status = 0;
790 cb->buf_idx += mei_hdr->length;
791 if (mei_hdr->msg_complete)
792 list_move_tail(&cb->list, &dev->write_waiting_list.list);
794 return 0;
798 * mei_irq_thread_read_handler - bottom half read routine after ISR to
799 * handle the read processing.
801 * @cmpl_list: An instance of our list structure
802 * @dev: the device structure
803 * @slots: slots to read.
805 * returns 0 on success, <0 on failure.
807 static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
808 struct mei_device *dev,
809 s32 *slots)
811 struct mei_msg_hdr *mei_hdr;
812 struct mei_cl *cl_pos = NULL;
813 struct mei_cl *cl_next = NULL;
814 int ret = 0;
816 if (!dev->rd_msg_hdr) {
817 dev->rd_msg_hdr = mei_mecbrw_read(dev);
818 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
819 (*slots)--;
820 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
822 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
823 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
825 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
826 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
827 ret = -EBADMSG;
828 goto end;
831 if (mei_hdr->host_addr || mei_hdr->me_addr) {
832 list_for_each_entry_safe(cl_pos, cl_next,
833 &dev->file_list, link) {
834 dev_dbg(&dev->pdev->dev,
835 "list_for_each_entry_safe read host"
836 " client = %d, ME client = %d\n",
837 cl_pos->host_client_id,
838 cl_pos->me_client_id);
839 if (cl_pos->host_client_id == mei_hdr->host_addr &&
840 cl_pos->me_client_id == mei_hdr->me_addr)
841 break;
844 if (&cl_pos->link == &dev->file_list) {
845 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
846 ret = -EBADMSG;
847 goto end;
850 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
851 dev_dbg(&dev->pdev->dev,
852 "we can't read the message slots =%08x.\n",
853 *slots);
854 /* we can't read the message */
855 ret = -ERANGE;
856 goto end;
859 /* decide where to read the message too */
860 if (!mei_hdr->host_addr) {
861 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
862 mei_irq_thread_read_bus_message(dev, mei_hdr);
863 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
864 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
865 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
866 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
867 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
868 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
869 mei_hdr->length);
871 ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
872 if (ret)
873 goto end;
875 } else {
876 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
877 ret = mei_irq_thread_read_client_message(cmpl_list,
878 dev, mei_hdr);
879 if (ret)
880 goto end;
884 /* reset the number of slots and header */
885 *slots = mei_count_full_read_slots(dev);
886 dev->rd_msg_hdr = 0;
888 if (*slots == -EOVERFLOW) {
889 /* overflow - reset */
890 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
891 /* set the event since message has been read */
892 ret = -ERANGE;
893 goto end;
895 end:
896 return ret;
901 * mei_irq_thread_write_handler - bottom half write routine after
902 * ISR to handle the write processing.
904 * @dev: the device structure
905 * @cmpl_list: An instance of our list structure
907 * returns 0 on success, <0 on failure.
909 static int mei_irq_thread_write_handler(struct mei_device *dev,
910 struct mei_cl_cb *cmpl_list)
913 struct mei_cl *cl;
914 struct mei_cl_cb *pos = NULL, *next = NULL;
915 struct mei_cl_cb *list;
916 s32 slots;
917 int ret;
919 if (!mei_hbuf_is_empty(dev)) {
920 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
921 return 0;
923 slots = mei_hbuf_empty_slots(dev);
924 if (slots <= 0)
925 return -EMSGSIZE;
927 /* complete all waiting for write CB */
928 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
930 list = &dev->write_waiting_list;
931 list_for_each_entry_safe(pos, next, &list->list, list) {
932 cl = pos->cl;
933 if (cl == NULL)
934 continue;
936 cl->status = 0;
937 list_del(&pos->list);
938 if (MEI_WRITING == cl->writing_state &&
939 pos->fop_type == MEI_FOP_WRITE &&
940 cl != &dev->iamthif_cl) {
941 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
942 cl->writing_state = MEI_WRITE_COMPLETE;
943 list_add_tail(&pos->list, &cmpl_list->list);
945 if (cl == &dev->iamthif_cl) {
946 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
947 if (dev->iamthif_flow_control_pending) {
948 ret = mei_amthif_irq_read(dev, &slots);
949 if (ret)
950 return ret;
955 if (dev->wd_state == MEI_WD_STOPPING) {
956 dev->wd_state = MEI_WD_IDLE;
957 wake_up_interruptible(&dev->wait_stop_wd);
960 if (dev->wr_ext_msg.hdr.length) {
961 mei_write_message(dev, &dev->wr_ext_msg.hdr,
962 dev->wr_ext_msg.data, dev->wr_ext_msg.hdr.length);
963 slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
964 dev->wr_ext_msg.hdr.length = 0;
966 if (dev->dev_state == MEI_DEV_ENABLED) {
967 if (dev->wd_pending &&
968 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
969 if (mei_wd_send(dev))
970 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
971 else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
972 return -ENODEV;
974 dev->wd_pending = false;
976 if (dev->wd_state == MEI_WD_RUNNING)
977 slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
978 else
979 slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
983 /* complete control write list CB */
984 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
985 list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
986 cl = pos->cl;
987 if (!cl) {
988 list_del(&pos->list);
989 return -ENODEV;
991 switch (pos->fop_type) {
992 case MEI_FOP_CLOSE:
993 /* send disconnect message */
994 ret = _mei_irq_thread_close(dev, &slots, pos,
995 cl, cmpl_list);
996 if (ret)
997 return ret;
999 break;
1000 case MEI_FOP_READ:
1001 /* send flow control message */
1002 ret = _mei_irq_thread_read(dev, &slots, pos,
1003 cl, cmpl_list);
1004 if (ret)
1005 return ret;
1007 break;
1008 case MEI_FOP_IOCTL:
1009 /* connect message */
1010 if (mei_other_client_is_connecting(dev, cl))
1011 continue;
1012 ret = _mei_irq_thread_ioctl(dev, &slots, pos,
1013 cl, cmpl_list);
1014 if (ret)
1015 return ret;
1017 break;
1019 default:
1020 BUG();
1024 /* complete write list CB */
1025 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
1026 list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
1027 cl = pos->cl;
1028 if (cl == NULL)
1029 continue;
1030 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
1031 dev_dbg(&dev->pdev->dev,
1032 "No flow control credentials for client %d, not sending.\n",
1033 cl->host_client_id);
1034 continue;
1037 if (cl == &dev->iamthif_cl)
1038 ret = mei_amthif_irq_write_complete(dev, &slots,
1039 pos, cmpl_list);
1040 else
1041 ret = mei_irq_thread_write_complete(dev, &slots, pos,
1042 cmpl_list);
1043 if (ret)
1044 return ret;
1047 return 0;
1053 * mei_timer - timer function.
1055 * @work: pointer to the work_struct structure
1057 * NOTE: This function is called by timer interrupt work
1059 void mei_timer(struct work_struct *work)
1061 unsigned long timeout;
1062 struct mei_cl *cl_pos = NULL;
1063 struct mei_cl *cl_next = NULL;
1064 struct mei_cl_cb *cb_pos = NULL;
1065 struct mei_cl_cb *cb_next = NULL;
1067 struct mei_device *dev = container_of(work,
1068 struct mei_device, timer_work.work);
1071 mutex_lock(&dev->device_lock);
1072 if (dev->dev_state != MEI_DEV_ENABLED) {
1073 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
1074 if (dev->init_clients_timer) {
1075 if (--dev->init_clients_timer == 0) {
1076 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1077 dev->init_clients_state);
1078 mei_reset(dev, 1);
1082 goto out;
1084 /*** connect/disconnect timeouts ***/
1085 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1086 if (cl_pos->timer_count) {
1087 if (--cl_pos->timer_count == 0) {
1088 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1089 mei_reset(dev, 1);
1090 goto out;
1095 if (dev->iamthif_stall_timer) {
1096 if (--dev->iamthif_stall_timer == 0) {
1097 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
1098 mei_reset(dev, 1);
1099 dev->iamthif_msg_buf_size = 0;
1100 dev->iamthif_msg_buf_index = 0;
1101 dev->iamthif_canceled = false;
1102 dev->iamthif_ioctl = true;
1103 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1104 dev->iamthif_timer = 0;
1106 mei_io_cb_free(dev->iamthif_current_cb);
1107 dev->iamthif_current_cb = NULL;
1109 dev->iamthif_file_object = NULL;
1110 mei_amthif_run_next_cmd(dev);
1114 if (dev->iamthif_timer) {
1116 timeout = dev->iamthif_timer +
1117 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
1119 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1120 dev->iamthif_timer);
1121 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1122 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1123 if (time_after(jiffies, timeout)) {
1125 * User didn't read the AMTHI data on time (15sec)
1126 * freeing AMTHI for other requests
1129 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1131 list_for_each_entry_safe(cb_pos, cb_next,
1132 &dev->amthif_rd_complete_list.list, list) {
1134 cl_pos = cb_pos->file_object->private_data;
1136 /* Finding the AMTHI entry. */
1137 if (cl_pos == &dev->iamthif_cl)
1138 list_del(&cb_pos->list);
1140 mei_io_cb_free(dev->iamthif_current_cb);
1141 dev->iamthif_current_cb = NULL;
1143 dev->iamthif_file_object->private_data = NULL;
1144 dev->iamthif_file_object = NULL;
1145 dev->iamthif_timer = 0;
1146 mei_amthif_run_next_cmd(dev);
1150 out:
1151 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1152 mutex_unlock(&dev->device_lock);
1156 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1157 * processing.
1159 * @irq: The irq number
1160 * @dev_id: pointer to the device structure
1162 * returns irqreturn_t
1165 irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1167 struct mei_device *dev = (struct mei_device *) dev_id;
1168 struct mei_cl_cb complete_list;
1169 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1170 struct mei_cl *cl;
1171 s32 slots;
1172 int rets;
1173 bool bus_message_received;
1176 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1177 /* initialize our complete list */
1178 mutex_lock(&dev->device_lock);
1179 mei_io_list_init(&complete_list);
1180 dev->host_hw_state = mei_hcsr_read(dev);
1182 /* Ack the interrupt here
1183 * In case of MSI we don't go through the quick handler */
1184 if (pci_dev_msi_enabled(dev->pdev))
1185 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1187 dev->me_hw_state = mei_mecsr_read(dev);
1189 /* check if ME wants a reset */
1190 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
1191 dev->dev_state != MEI_DEV_RESETING &&
1192 dev->dev_state != MEI_DEV_INITIALIZING) {
1193 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1194 mei_reset(dev, 1);
1195 mutex_unlock(&dev->device_lock);
1196 return IRQ_HANDLED;
1199 /* check if we need to start the dev */
1200 if ((dev->host_hw_state & H_RDY) == 0) {
1201 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1202 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1203 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1204 mei_hcsr_set(dev);
1205 dev->dev_state = MEI_DEV_INIT_CLIENTS;
1206 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1207 /* link is established
1208 * start sending messages.
1210 mei_host_start_message(dev);
1211 mutex_unlock(&dev->device_lock);
1212 return IRQ_HANDLED;
1213 } else {
1214 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1215 mutex_unlock(&dev->device_lock);
1216 return IRQ_HANDLED;
1219 /* check slots available for reading */
1220 slots = mei_count_full_read_slots(dev);
1221 while (slots > 0) {
1222 /* we have urgent data to send so break the read */
1223 if (dev->wr_ext_msg.hdr.length)
1224 break;
1225 dev_dbg(&dev->pdev->dev, "slots =%08x\n", slots);
1226 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1227 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1228 if (rets)
1229 goto end;
1231 rets = mei_irq_thread_write_handler(dev, &complete_list);
1232 end:
1233 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1234 dev->host_hw_state = mei_hcsr_read(dev);
1235 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
1237 bus_message_received = false;
1238 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1239 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1240 bus_message_received = true;
1242 mutex_unlock(&dev->device_lock);
1243 if (bus_message_received) {
1244 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1245 wake_up_interruptible(&dev->wait_recvd_msg);
1246 bus_message_received = false;
1248 if (list_empty(&complete_list.list))
1249 return IRQ_HANDLED;
1252 list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
1253 cl = cb_pos->cl;
1254 list_del(&cb_pos->list);
1255 if (cl) {
1256 if (cl != &dev->iamthif_cl) {
1257 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1258 _mei_cmpl(cl, cb_pos);
1259 cb_pos = NULL;
1260 } else if (cl == &dev->iamthif_cl) {
1261 mei_amthif_complete(dev, cb_pos);
1265 return IRQ_HANDLED;