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/pci.h>
19 #include <linux/kthread.h>
20 #include <linux/interrupt.h>
22 #include <linux/jiffies.h>
25 #include <linux/mei.h>
27 #include "interface.h"
31 * mei_interrupt_quick_handler - The ISR of the MEI device
33 * @irq: The irq number
34 * @dev_id: pointer to the device structure
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)
46 /* clear H_IS bit in H_CSR */
47 mei_reg_write(dev, H_CSR, csr_reg);
49 return IRQ_WAKE_THREAD;
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);
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);
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,
89 dev->iamthif_msg_buf_index);
90 list_add_tail(&cb_pos->list, &dev->amthi_read_complete_list.list);
91 dev_dbg(&dev->pdev->dev, "amthi read completed\n");
92 dev->iamthif_timer = jiffies;
93 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
96 mei_run_next_iamthif_cmd(dev);
99 dev_dbg(&dev->pdev->dev, "completing amthi call back.\n");
100 wake_up_interruptible(&dev->iamthif_cl.wait);
105 * mei_irq_thread_read_amthi_message - bottom half read routine after ISR to
106 * handle the read amthi message data processing.
108 * @complete_list: An instance of our list structure
109 * @dev: the device structure
110 * @mei_hdr: header of amthi message
112 * returns 0 on success, <0 on failure.
114 static int mei_irq_thread_read_amthi_message(struct mei_cl_cb *complete_list,
115 struct mei_device *dev,
116 struct mei_msg_hdr *mei_hdr)
119 struct mei_cl_cb *cb;
120 unsigned char *buffer;
122 BUG_ON(mei_hdr->me_addr != dev->iamthif_cl.me_client_id);
123 BUG_ON(dev->iamthif_state != MEI_IAMTHIF_READING);
125 buffer = dev->iamthif_msg_buf + dev->iamthif_msg_buf_index;
126 BUG_ON(dev->iamthif_mtu < dev->iamthif_msg_buf_index + mei_hdr->length);
128 mei_read_slots(dev, buffer, mei_hdr->length);
130 dev->iamthif_msg_buf_index += mei_hdr->length;
132 if (!mei_hdr->msg_complete)
135 dev_dbg(&dev->pdev->dev,
136 "amthi_message_buffer_index =%d\n",
139 dev_dbg(&dev->pdev->dev, "completed amthi read.\n ");
140 if (!dev->iamthif_current_cb)
143 cb = dev->iamthif_current_cb;
144 dev->iamthif_current_cb = NULL;
146 cl = (struct mei_cl *)cb->file_private;
150 dev->iamthif_stall_timer = 0;
151 cb->buf_idx = dev->iamthif_msg_buf_index;
152 cb->read_time = jiffies;
153 if (dev->iamthif_ioctl && cl == &dev->iamthif_cl) {
154 /* found the iamthif cb */
155 dev_dbg(&dev->pdev->dev, "complete the amthi read cb.\n ");
156 dev_dbg(&dev->pdev->dev, "add the amthi read cb to complete.\n ");
157 list_add_tail(&cb->list, &complete_list->list);
163 * _mei_irq_thread_state_ok - checks if mei header matches file private data
165 * @cl: private data of the file object
166 * @mei_hdr: header of mei client message
168 * returns !=0 if matches, 0 if no match.
170 static int _mei_irq_thread_state_ok(struct mei_cl *cl,
171 struct mei_msg_hdr *mei_hdr)
173 return (cl->host_client_id == mei_hdr->host_addr &&
174 cl->me_client_id == mei_hdr->me_addr &&
175 cl->state == MEI_FILE_CONNECTED &&
176 MEI_READ_COMPLETE != cl->reading_state);
180 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
181 * handle the read mei client message data processing.
183 * @complete_list: An instance of our list structure
184 * @dev: the device structure
185 * @mei_hdr: header of mei client message
187 * returns 0 on success, <0 on failure.
189 static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
190 struct mei_device *dev,
191 struct mei_msg_hdr *mei_hdr)
194 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
195 unsigned char *buffer = NULL;
197 dev_dbg(&dev->pdev->dev, "start client msg\n");
198 if (list_empty(&dev->read_list.list))
201 list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
202 cl = (struct mei_cl *)cb_pos->file_private;
203 if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
204 cl->reading_state = MEI_READING;
205 buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
207 if (cb_pos->response_buffer.size <
208 mei_hdr->length + cb_pos->buf_idx) {
209 dev_dbg(&dev->pdev->dev, "message overflow.\n");
210 list_del(&cb_pos->list);
214 mei_read_slots(dev, buffer, mei_hdr->length);
216 cb_pos->buf_idx += mei_hdr->length;
217 if (mei_hdr->msg_complete) {
219 list_del(&cb_pos->list);
220 dev_dbg(&dev->pdev->dev,
221 "completed read H cl = %d, ME cl = %d, length = %lu\n",
226 list_add_tail(&cb_pos->list,
227 &complete_list->list);
236 dev_dbg(&dev->pdev->dev, "message read\n");
238 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
239 dev_dbg(&dev->pdev->dev, "discarding message, header =%08x.\n",
240 *(u32 *) dev->rd_msg_buf);
247 * _mei_irq_thread_iamthif_read - prepares to read iamthif data.
249 * @dev: the device structure.
250 * @slots: free slots.
252 * returns 0, OK; otherwise, error.
254 static int _mei_irq_thread_iamthif_read(struct mei_device *dev, s32 *slots)
257 if (((*slots) * sizeof(u32)) < (sizeof(struct mei_msg_hdr)
258 + sizeof(struct hbm_flow_control))) {
261 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
262 if (mei_send_flow_control(dev, &dev->iamthif_cl)) {
263 dev_dbg(&dev->pdev->dev, "iamthif flow control failed\n");
267 dev_dbg(&dev->pdev->dev, "iamthif flow control success\n");
268 dev->iamthif_state = MEI_IAMTHIF_READING;
269 dev->iamthif_flow_control_pending = false;
270 dev->iamthif_msg_buf_index = 0;
271 dev->iamthif_msg_buf_size = 0;
272 dev->iamthif_stall_timer = IAMTHIF_STALL_TIMER;
273 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
278 * _mei_irq_thread_close - processes close related operation.
280 * @dev: the device structure.
281 * @slots: free slots.
282 * @cb_pos: callback block.
283 * @cl: private data of the file object.
284 * @cmpl_list: complete list.
286 * returns 0, OK; otherwise, error.
288 static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
289 struct mei_cl_cb *cb_pos,
291 struct mei_cl_cb *cmpl_list)
293 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
294 sizeof(struct hbm_client_disconnect_request)))
297 *slots -= mei_data2slots(sizeof(struct hbm_client_disconnect_request));
299 if (mei_disconnect(dev, cl)) {
302 list_move_tail(&cb_pos->list, &cmpl_list->list);
305 cl->state = MEI_FILE_DISCONNECTING;
308 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
309 cl->timer_count = MEI_CONNECT_TIMEOUT;
316 * is_treat_specially_client - checks if the message belongs
317 * to the file private data.
319 * @cl: private data of the file object
320 * @rs: connect response bus message
323 static bool is_treat_specially_client(struct mei_cl *cl,
324 struct hbm_client_connect_response *rs)
327 if (cl->host_client_id == rs->host_addr &&
328 cl->me_client_id == rs->me_addr) {
330 cl->state = MEI_FILE_CONNECTED;
334 cl->state = MEI_FILE_DISCONNECTED;
335 cl->status = -ENODEV;
345 * mei_client_connect_response - connects to response irq routine
347 * @dev: the device structure
348 * @rs: connect response bus message
350 static void mei_client_connect_response(struct mei_device *dev,
351 struct hbm_client_connect_response *rs)
355 struct mei_cl_cb *pos = NULL, *next = NULL;
357 dev_dbg(&dev->pdev->dev,
358 "connect_response:\n"
366 /* if WD or iamthif client treat specially */
368 if (is_treat_specially_client(&(dev->wd_cl), rs)) {
369 dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
370 mei_watchdog_register(dev);
372 /* next step in the state maching */
373 mei_host_init_iamthif(dev);
377 if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
378 dev->iamthif_state = MEI_IAMTHIF_IDLE;
381 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
383 cl = (struct mei_cl *)pos->file_private;
385 list_del(&pos->list);
388 if (MEI_IOCTL == pos->major_file_operations) {
389 if (is_treat_specially_client(cl, rs)) {
390 list_del(&pos->list);
400 * mei_client_disconnect_response - disconnects from response irq routine
402 * @dev: the device structure
403 * @rs: disconnect response bus message
405 static void mei_client_disconnect_response(struct mei_device *dev,
406 struct hbm_client_connect_response *rs)
409 struct mei_cl_cb *pos = NULL, *next = NULL;
411 dev_dbg(&dev->pdev->dev,
412 "disconnect_response:\n"
420 list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
421 cl = (struct mei_cl *)pos->file_private;
424 list_del(&pos->list);
428 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
429 if (cl->host_client_id == rs->host_addr &&
430 cl->me_client_id == rs->me_addr) {
432 list_del(&pos->list);
434 cl->state = MEI_FILE_DISCONNECTED;
444 * same_flow_addr - tells if they have the same address.
446 * @file: private data of the file object.
447 * @flow: flow control.
449 * returns !=0, same; 0,not.
451 static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
453 return (cl->host_client_id == flow->host_addr &&
454 cl->me_client_id == flow->me_addr);
458 * add_single_flow_creds - adds single buffer credentials.
460 * @file: private data ot the file object.
461 * @flow: flow control.
463 static void add_single_flow_creds(struct mei_device *dev,
464 struct hbm_flow_control *flow)
466 struct mei_me_client *client;
469 for (i = 0; i < dev->me_clients_num; i++) {
470 client = &dev->me_clients[i];
471 if (client && flow->me_addr == client->client_id) {
472 if (client->props.single_recv_buf) {
473 client->mei_flow_ctrl_creds++;
474 dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
476 dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
477 client->mei_flow_ctrl_creds);
479 BUG(); /* error in flow control */
486 * mei_client_flow_control_response - flow control response irq routine
488 * @dev: the device structure
489 * @flow_control: flow control response bus message
491 static void mei_client_flow_control_response(struct mei_device *dev,
492 struct hbm_flow_control *flow_control)
494 struct mei_cl *cl_pos = NULL;
495 struct mei_cl *cl_next = NULL;
497 if (!flow_control->host_addr) {
498 /* single receive buffer */
499 add_single_flow_creds(dev, flow_control);
501 /* normal connection */
502 list_for_each_entry_safe(cl_pos, cl_next,
503 &dev->file_list, link) {
504 dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");
506 dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
507 cl_pos->host_client_id,
508 cl_pos->me_client_id);
509 dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
510 flow_control->host_addr,
511 flow_control->me_addr);
512 if (same_flow_addr(cl_pos, flow_control)) {
513 dev_dbg(&dev->pdev->dev, "recv ctrl msg for host %d ME %d.\n",
514 flow_control->host_addr,
515 flow_control->me_addr);
516 cl_pos->mei_flow_ctrl_creds++;
517 dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
518 cl_pos->mei_flow_ctrl_creds);
526 * same_disconn_addr - tells if they have the same address
528 * @file: private data of the file object.
529 * @disconn: disconnection request.
531 * returns !=0, same; 0,not.
533 static int same_disconn_addr(struct mei_cl *cl,
534 struct hbm_client_disconnect_request *disconn)
536 return (cl->host_client_id == disconn->host_addr &&
537 cl->me_client_id == disconn->me_addr);
541 * mei_client_disconnect_request - disconnects from request irq routine
543 * @dev: the device structure.
544 * @disconnect_req: disconnect request bus message.
546 static void mei_client_disconnect_request(struct mei_device *dev,
547 struct hbm_client_disconnect_request *disconnect_req)
549 struct mei_msg_hdr *mei_hdr;
550 struct hbm_client_connect_response *disconnect_res;
551 struct mei_cl *cl_pos = NULL;
552 struct mei_cl *cl_next = NULL;
554 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
555 if (same_disconn_addr(cl_pos, disconnect_req)) {
556 dev_dbg(&dev->pdev->dev, "disconnect request host client %d ME client %d.\n",
557 disconnect_req->host_addr,
558 disconnect_req->me_addr);
559 cl_pos->state = MEI_FILE_DISCONNECTED;
560 cl_pos->timer_count = 0;
561 if (cl_pos == &dev->wd_cl)
562 dev->wd_pending = false;
563 else if (cl_pos == &dev->iamthif_cl)
564 dev->iamthif_timer = 0;
566 /* prepare disconnect response */
568 (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
569 mei_hdr->host_addr = 0;
570 mei_hdr->me_addr = 0;
572 sizeof(struct hbm_client_connect_response);
573 mei_hdr->msg_complete = 1;
574 mei_hdr->reserved = 0;
577 (struct hbm_client_connect_response *)
578 &dev->ext_msg_buf[1];
579 disconnect_res->host_addr = cl_pos->host_client_id;
580 disconnect_res->me_addr = cl_pos->me_client_id;
581 disconnect_res->hbm_cmd = CLIENT_DISCONNECT_RES_CMD;
582 disconnect_res->status = 0;
583 dev->extra_write_index = 2;
591 * mei_irq_thread_read_bus_message - bottom half read routine after ISR to
592 * handle the read bus message cmd processing.
594 * @dev: the device structure
595 * @mei_hdr: header of bus message
597 static void mei_irq_thread_read_bus_message(struct mei_device *dev,
598 struct mei_msg_hdr *mei_hdr)
600 struct mei_bus_message *mei_msg;
601 struct hbm_host_version_response *version_res;
602 struct hbm_client_connect_response *connect_res;
603 struct hbm_client_connect_response *disconnect_res;
604 struct hbm_flow_control *flow_control;
605 struct hbm_props_response *props_res;
606 struct hbm_host_enum_response *enum_res;
607 struct hbm_client_disconnect_request *disconnect_req;
608 struct hbm_host_stop_request *host_stop_req;
612 /* read the message to our buffer */
613 BUG_ON(mei_hdr->length >= sizeof(dev->rd_msg_buf));
614 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
615 mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
617 switch (mei_msg->hbm_cmd) {
618 case HOST_START_RES_CMD:
619 version_res = (struct hbm_host_version_response *) mei_msg;
620 if (version_res->host_version_supported) {
621 dev->version.major_version = HBM_MAJOR_VERSION;
622 dev->version.minor_version = HBM_MINOR_VERSION;
623 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
624 dev->init_clients_state == MEI_START_MESSAGE) {
625 dev->init_clients_timer = 0;
626 mei_host_enum_clients_message(dev);
628 dev->recvd_msg = false;
629 dev_dbg(&dev->pdev->dev, "IMEI reset due to received host start response bus message.\n");
634 dev->version = version_res->me_max_version;
635 /* send stop message */
636 mei_hdr = (struct mei_msg_hdr *)&dev->wr_msg_buf[0];
637 mei_hdr->host_addr = 0;
638 mei_hdr->me_addr = 0;
639 mei_hdr->length = sizeof(struct hbm_host_stop_request);
640 mei_hdr->msg_complete = 1;
641 mei_hdr->reserved = 0;
643 host_stop_req = (struct hbm_host_stop_request *)
646 memset(host_stop_req,
648 sizeof(struct hbm_host_stop_request));
649 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
650 host_stop_req->reason = DRIVER_STOP_REQUEST;
651 mei_write_message(dev, mei_hdr,
652 (unsigned char *) (host_stop_req),
654 dev_dbg(&dev->pdev->dev, "version mismatch.\n");
658 dev->recvd_msg = true;
659 dev_dbg(&dev->pdev->dev, "host start response message received.\n");
662 case CLIENT_CONNECT_RES_CMD:
664 (struct hbm_client_connect_response *) mei_msg;
665 mei_client_connect_response(dev, connect_res);
666 dev_dbg(&dev->pdev->dev, "client connect response message received.\n");
667 wake_up(&dev->wait_recvd_msg);
670 case CLIENT_DISCONNECT_RES_CMD:
672 (struct hbm_client_connect_response *) mei_msg;
673 mei_client_disconnect_response(dev, disconnect_res);
674 dev_dbg(&dev->pdev->dev, "client disconnect response message received.\n");
675 wake_up(&dev->wait_recvd_msg);
678 case MEI_FLOW_CONTROL_CMD:
679 flow_control = (struct hbm_flow_control *) mei_msg;
680 mei_client_flow_control_response(dev, flow_control);
681 dev_dbg(&dev->pdev->dev, "client flow control response message received.\n");
684 case HOST_CLIENT_PROPERTIES_RES_CMD:
685 props_res = (struct hbm_props_response *)mei_msg;
686 if (props_res->status || !dev->me_clients) {
687 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message wrong status.\n");
691 if (dev->me_clients[dev->me_client_presentation_num]
692 .client_id == props_res->address) {
694 dev->me_clients[dev->me_client_presentation_num].props
695 = props_res->client_properties;
697 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
698 dev->init_clients_state ==
699 MEI_CLIENT_PROPERTIES_MESSAGE) {
700 dev->me_client_index++;
701 dev->me_client_presentation_num++;
703 /** Send Client Properties request **/
704 res = mei_host_client_properties(dev);
706 dev_dbg(&dev->pdev->dev, "mei_host_client_properties() failed");
710 * No more clients to send to.
711 * Clear Map for indicating now ME clients
712 * with associated host client
714 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
715 dev->open_handle_count = 0;
718 * Reserving the first three client IDs
719 * Client Id 0 - Reserved for MEI Bus Message communications
720 * Client Id 1 - Reserved for Watchdog
721 * Client ID 2 - Reserved for AMTHI
723 bitmap_set(dev->host_clients_map, 0, 3);
724 dev->dev_state = MEI_DEV_ENABLED;
726 /* if wd initialization fails, initialization the AMTHI client,
727 * otherwise the AMTHI client will be initialized after the WD client connect response
730 if (mei_wd_host_init(dev))
731 mei_host_init_iamthif(dev);
735 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message");
740 dev_dbg(&dev->pdev->dev, "reset due to received host client properties response bus message for wrong client ID\n");
746 case HOST_ENUM_RES_CMD:
747 enum_res = (struct hbm_host_enum_response *) mei_msg;
748 memcpy(dev->me_clients_map, enum_res->valid_addresses, 32);
749 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
750 dev->init_clients_state == MEI_ENUM_CLIENTS_MESSAGE) {
751 dev->init_clients_timer = 0;
752 dev->me_client_presentation_num = 0;
753 dev->me_client_index = 0;
754 mei_allocate_me_clients_storage(dev);
755 dev->init_clients_state =
756 MEI_CLIENT_PROPERTIES_MESSAGE;
757 mei_host_client_properties(dev);
759 dev_dbg(&dev->pdev->dev, "reset due to received host enumeration clients response bus message.\n");
765 case HOST_STOP_RES_CMD:
766 dev->dev_state = MEI_DEV_DISABLED;
767 dev_dbg(&dev->pdev->dev, "resetting because of FW stop response.\n");
771 case CLIENT_DISCONNECT_REQ_CMD:
772 /* search for client */
774 (struct hbm_client_disconnect_request *) mei_msg;
775 mei_client_disconnect_request(dev, disconnect_req);
778 case ME_STOP_REQ_CMD:
779 /* prepare stop request */
780 mei_hdr = (struct mei_msg_hdr *) &dev->ext_msg_buf[0];
781 mei_hdr->host_addr = 0;
782 mei_hdr->me_addr = 0;
783 mei_hdr->length = sizeof(struct hbm_host_stop_request);
784 mei_hdr->msg_complete = 1;
785 mei_hdr->reserved = 0;
787 (struct hbm_host_stop_request *) &dev->ext_msg_buf[1];
788 memset(host_stop_req, 0, sizeof(struct hbm_host_stop_request));
789 host_stop_req->hbm_cmd = HOST_STOP_REQ_CMD;
790 host_stop_req->reason = DRIVER_STOP_REQUEST;
791 host_stop_req->reserved[0] = 0;
792 host_stop_req->reserved[1] = 0;
793 dev->extra_write_index = 2;
805 * _mei_hb_read - processes read related operation.
807 * @dev: the device structure.
808 * @slots: free slots.
809 * @cb_pos: callback block.
810 * @cl: private data of the file object.
811 * @cmpl_list: complete list.
813 * returns 0, OK; otherwise, error.
815 static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
816 struct mei_cl_cb *cb_pos,
818 struct mei_cl_cb *cmpl_list)
820 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
821 sizeof(struct hbm_flow_control))) {
822 /* return the cancel routine */
823 list_del(&cb_pos->list);
827 *slots -= mei_data2slots(sizeof(struct hbm_flow_control));
829 if (mei_send_flow_control(dev, cl)) {
830 cl->status = -ENODEV;
832 list_move_tail(&cb_pos->list, &cmpl_list->list);
835 list_move_tail(&cb_pos->list, &dev->read_list.list);
842 * _mei_irq_thread_ioctl - processes ioctl related operation.
844 * @dev: the device structure.
845 * @slots: free slots.
846 * @cb_pos: callback block.
847 * @cl: private data of the file object.
848 * @cmpl_list: complete list.
850 * returns 0, OK; otherwise, error.
852 static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
853 struct mei_cl_cb *cb_pos,
855 struct mei_cl_cb *cmpl_list)
857 if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
858 sizeof(struct hbm_client_connect_request))) {
859 /* return the cancel routine */
860 list_del(&cb_pos->list);
864 cl->state = MEI_FILE_CONNECTING;
865 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
866 if (mei_connect(dev, cl)) {
867 cl->status = -ENODEV;
869 list_del(&cb_pos->list);
872 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
873 cl->timer_count = MEI_CONNECT_TIMEOUT;
879 * _mei_irq_thread_cmpl - processes completed and no-iamthif operation.
881 * @dev: the device structure.
882 * @slots: free slots.
883 * @cb_pos: callback block.
884 * @cl: private data of the file object.
885 * @cmpl_list: complete list.
887 * returns 0, OK; otherwise, error.
889 static int _mei_irq_thread_cmpl(struct mei_device *dev, s32 *slots,
890 struct mei_cl_cb *cb_pos,
892 struct mei_cl_cb *cmpl_list)
894 struct mei_msg_hdr *mei_hdr;
896 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
897 (cb_pos->request_buffer.size - cb_pos->buf_idx))) {
898 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
899 mei_hdr->host_addr = cl->host_client_id;
900 mei_hdr->me_addr = cl->me_client_id;
901 mei_hdr->length = cb_pos->request_buffer.size - cb_pos->buf_idx;
902 mei_hdr->msg_complete = 1;
903 mei_hdr->reserved = 0;
904 dev_dbg(&dev->pdev->dev, "cb_pos->request_buffer.size =%d"
905 "mei_hdr->msg_complete = %d\n",
906 cb_pos->request_buffer.size,
907 mei_hdr->msg_complete);
908 dev_dbg(&dev->pdev->dev, "cb_pos->buf_idx =%lu\n",
910 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
912 *slots -= mei_data2slots(mei_hdr->length);
913 if (mei_write_message(dev, mei_hdr,
915 (cb_pos->request_buffer.data +
918 cl->status = -ENODEV;
919 list_move_tail(&cb_pos->list, &cmpl_list->list);
922 if (mei_flow_ctrl_reduce(dev, cl))
925 cb_pos->buf_idx += mei_hdr->length;
926 list_move_tail(&cb_pos->list, &dev->write_waiting_list.list);
928 } else if (*slots == dev->hbuf_depth) {
929 /* buffer is still empty */
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;
934 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
935 mei_hdr->msg_complete = 0;
936 mei_hdr->reserved = 0;
937 *slots -= mei_data2slots(mei_hdr->length);
938 if (mei_write_message(dev, mei_hdr,
940 (cb_pos->request_buffer.data +
943 cl->status = -ENODEV;
944 list_move_tail(&cb_pos->list, &cmpl_list->list);
947 cb_pos->buf_idx += mei_hdr->length;
948 dev_dbg(&dev->pdev->dev,
949 "cb_pos->request_buffer.size =%d"
950 " mei_hdr->msg_complete = %d\n",
951 cb_pos->request_buffer.size,
952 mei_hdr->msg_complete);
953 dev_dbg(&dev->pdev->dev, "cb_pos->buf_idx =%lu\n",
955 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
967 * _mei_irq_thread_cmpl_iamthif - processes completed iamthif operation.
969 * @dev: the device structure.
970 * @slots: free slots.
971 * @cb_pos: callback block.
972 * @cl: private data of the file object.
973 * @cmpl_list: complete list.
975 * returns 0, OK; otherwise, error.
977 static int _mei_irq_thread_cmpl_iamthif(struct mei_device *dev, s32 *slots,
978 struct mei_cl_cb *cb_pos,
980 struct mei_cl_cb *cmpl_list)
982 struct mei_msg_hdr *mei_hdr;
984 if ((*slots * sizeof(u32)) >= (sizeof(struct mei_msg_hdr) +
985 dev->iamthif_msg_buf_size -
986 dev->iamthif_msg_buf_index)) {
987 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
988 mei_hdr->host_addr = cl->host_client_id;
989 mei_hdr->me_addr = cl->me_client_id;
990 mei_hdr->length = dev->iamthif_msg_buf_size -
991 dev->iamthif_msg_buf_index;
992 mei_hdr->msg_complete = 1;
993 mei_hdr->reserved = 0;
995 *slots -= mei_data2slots(mei_hdr->length);
997 if (mei_write_message(dev, mei_hdr,
998 (dev->iamthif_msg_buf +
999 dev->iamthif_msg_buf_index),
1001 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1002 cl->status = -ENODEV;
1003 list_del(&cb_pos->list);
1006 if (mei_flow_ctrl_reduce(dev, cl))
1008 dev->iamthif_msg_buf_index += mei_hdr->length;
1009 cb_pos->buf_idx = dev->iamthif_msg_buf_index;
1011 dev->iamthif_state = MEI_IAMTHIF_FLOW_CONTROL;
1012 dev->iamthif_flow_control_pending = true;
1013 /* save iamthif cb sent to amthi client */
1014 dev->iamthif_current_cb = cb_pos;
1015 list_move_tail(&cb_pos->list, &dev->write_waiting_list.list);
1018 } else if (*slots == dev->hbuf_depth) {
1019 /* buffer is still empty */
1020 mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
1021 mei_hdr->host_addr = cl->host_client_id;
1022 mei_hdr->me_addr = cl->me_client_id;
1024 (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1025 mei_hdr->msg_complete = 0;
1026 mei_hdr->reserved = 0;
1028 *slots -= mei_data2slots(mei_hdr->length);
1030 if (mei_write_message(dev, mei_hdr,
1031 (dev->iamthif_msg_buf +
1032 dev->iamthif_msg_buf_index),
1034 cl->status = -ENODEV;
1035 list_del(&cb_pos->list);
1037 dev->iamthif_msg_buf_index += mei_hdr->length;
1048 * mei_irq_thread_read_handler - bottom half read routine after ISR to
1049 * handle the read processing.
1051 * @cmpl_list: An instance of our list structure
1052 * @dev: the device structure
1053 * @slots: slots to read.
1055 * returns 0 on success, <0 on failure.
1057 static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
1058 struct mei_device *dev,
1061 struct mei_msg_hdr *mei_hdr;
1062 struct mei_cl *cl_pos = NULL;
1063 struct mei_cl *cl_next = NULL;
1066 if (!dev->rd_msg_hdr) {
1067 dev->rd_msg_hdr = mei_mecbrw_read(dev);
1068 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1070 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
1072 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
1073 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n", mei_hdr->length);
1075 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
1076 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
1081 if (mei_hdr->host_addr || mei_hdr->me_addr) {
1082 list_for_each_entry_safe(cl_pos, cl_next,
1083 &dev->file_list, link) {
1084 dev_dbg(&dev->pdev->dev,
1085 "list_for_each_entry_safe read host"
1086 " client = %d, ME client = %d\n",
1087 cl_pos->host_client_id,
1088 cl_pos->me_client_id);
1089 if (cl_pos->host_client_id == mei_hdr->host_addr &&
1090 cl_pos->me_client_id == mei_hdr->me_addr)
1094 if (&cl_pos->link == &dev->file_list) {
1095 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
1100 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
1101 dev_dbg(&dev->pdev->dev,
1102 "we can't read the message slots =%08x.\n",
1104 /* we can't read the message */
1109 /* decide where to read the message too */
1110 if (!mei_hdr->host_addr) {
1111 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
1112 mei_irq_thread_read_bus_message(dev, mei_hdr);
1113 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
1114 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
1115 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
1116 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
1117 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
1118 dev_dbg(&dev->pdev->dev, "mei_hdr->length =%d\n",
1120 ret = mei_irq_thread_read_amthi_message(cmpl_list,
1126 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
1127 ret = mei_irq_thread_read_client_message(cmpl_list,
1134 /* reset the number of slots and header */
1135 *slots = mei_count_full_read_slots(dev);
1136 dev->rd_msg_hdr = 0;
1138 if (*slots == -EOVERFLOW) {
1139 /* overflow - reset */
1140 dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
1141 /* set the event since message has been read */
1151 * mei_irq_thread_write_handler - bottom half write routine after
1152 * ISR to handle the write processing.
1154 * @cmpl_list: An instance of our list structure
1155 * @dev: the device structure
1156 * @slots: slots to write.
1158 * returns 0 on success, <0 on failure.
1160 static int mei_irq_thread_write_handler(struct mei_cl_cb *cmpl_list,
1161 struct mei_device *dev, s32 *slots)
1165 struct mei_cl_cb *pos = NULL, *next = NULL;
1166 struct mei_cl_cb *list;
1169 if (!mei_hbuf_is_empty(dev)) {
1170 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
1173 *slots = mei_hbuf_empty_slots(dev);
1177 /* complete all waiting for write CB */
1178 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
1180 list = &dev->write_waiting_list;
1181 list_for_each_entry_safe(pos, next, &list->list, list) {
1182 cl = (struct mei_cl *)pos->file_private;
1187 list_del(&pos->list);
1188 if (MEI_WRITING == cl->writing_state &&
1189 (pos->major_file_operations == MEI_WRITE) &&
1190 (cl != &dev->iamthif_cl)) {
1191 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
1192 cl->writing_state = MEI_WRITE_COMPLETE;
1193 list_add_tail(&pos->list, &cmpl_list->list);
1195 if (cl == &dev->iamthif_cl) {
1196 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
1197 if (dev->iamthif_flow_control_pending) {
1198 ret = _mei_irq_thread_iamthif_read(dev, slots);
1205 if (dev->wd_state == MEI_WD_STOPPING) {
1206 dev->wd_state = MEI_WD_IDLE;
1207 wake_up_interruptible(&dev->wait_stop_wd);
1210 if (dev->extra_write_index) {
1211 dev_dbg(&dev->pdev->dev, "extra_write_index =%d.\n",
1212 dev->extra_write_index);
1213 mei_write_message(dev,
1214 (struct mei_msg_hdr *) &dev->ext_msg_buf[0],
1215 (unsigned char *) &dev->ext_msg_buf[1],
1216 (dev->extra_write_index - 1) * sizeof(u32));
1217 *slots -= dev->extra_write_index;
1218 dev->extra_write_index = 0;
1220 if (dev->dev_state == MEI_DEV_ENABLED) {
1221 if (dev->wd_pending &&
1222 mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
1223 if (mei_wd_send(dev))
1224 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
1225 else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
1228 dev->wd_pending = false;
1230 if (dev->wd_state == MEI_WD_RUNNING)
1231 *slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
1233 *slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
1237 /* complete control write list CB */
1238 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
1239 list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
1240 cl = (struct mei_cl *) pos->file_private;
1242 list_del(&pos->list);
1245 switch (pos->major_file_operations) {
1247 /* send disconnect message */
1248 ret = _mei_irq_thread_close(dev, slots, pos, cl, cmpl_list);
1254 /* send flow control message */
1255 ret = _mei_irq_thread_read(dev, slots, pos, cl, cmpl_list);
1261 /* connect message */
1262 if (mei_other_client_is_connecting(dev, cl))
1264 ret = _mei_irq_thread_ioctl(dev, slots, pos, cl, cmpl_list);
1275 /* complete write list CB */
1276 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
1277 list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
1278 cl = (struct mei_cl *)pos->file_private;
1282 if (cl != &dev->iamthif_cl) {
1283 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
1284 dev_dbg(&dev->pdev->dev,
1285 "No flow control credentials for client %d, not sending.\n",
1286 cl->host_client_id);
1289 ret = _mei_irq_thread_cmpl(dev, slots, pos,
1294 } else if (cl == &dev->iamthif_cl) {
1296 dev_dbg(&dev->pdev->dev, "complete amthi write cb.\n");
1297 if (mei_flow_ctrl_creds(dev, cl) <= 0) {
1298 dev_dbg(&dev->pdev->dev,
1299 "No flow control credentials for amthi client %d.\n",
1300 cl->host_client_id);
1303 ret = _mei_irq_thread_cmpl_iamthif(dev, slots, pos,
1317 * mei_timer - timer function.
1319 * @work: pointer to the work_struct structure
1321 * NOTE: This function is called by timer interrupt work
1323 void mei_timer(struct work_struct *work)
1325 unsigned long timeout;
1326 struct mei_cl *cl_pos = NULL;
1327 struct mei_cl *cl_next = NULL;
1328 struct list_head *amthi_complete_list = NULL;
1329 struct mei_cl_cb *cb_pos = NULL;
1330 struct mei_cl_cb *cb_next = NULL;
1332 struct mei_device *dev = container_of(work,
1333 struct mei_device, timer_work.work);
1336 mutex_lock(&dev->device_lock);
1337 if (dev->dev_state != MEI_DEV_ENABLED) {
1338 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
1339 if (dev->init_clients_timer) {
1340 if (--dev->init_clients_timer == 0) {
1341 dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
1342 dev->init_clients_state);
1349 /*** connect/disconnect timeouts ***/
1350 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
1351 if (cl_pos->timer_count) {
1352 if (--cl_pos->timer_count == 0) {
1353 dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
1360 if (dev->iamthif_stall_timer) {
1361 if (--dev->iamthif_stall_timer == 0) {
1362 dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
1364 dev->iamthif_msg_buf_size = 0;
1365 dev->iamthif_msg_buf_index = 0;
1366 dev->iamthif_canceled = false;
1367 dev->iamthif_ioctl = true;
1368 dev->iamthif_state = MEI_IAMTHIF_IDLE;
1369 dev->iamthif_timer = 0;
1371 if (dev->iamthif_current_cb)
1372 mei_free_cb_private(dev->iamthif_current_cb);
1374 dev->iamthif_file_object = NULL;
1375 dev->iamthif_current_cb = NULL;
1376 mei_run_next_iamthif_cmd(dev);
1380 if (dev->iamthif_timer) {
1382 timeout = dev->iamthif_timer +
1383 msecs_to_jiffies(IAMTHIF_READ_TIMER);
1385 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
1386 dev->iamthif_timer);
1387 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
1388 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
1389 if (time_after(jiffies, timeout)) {
1391 * User didn't read the AMTHI data on time (15sec)
1392 * freeing AMTHI for other requests
1395 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
1397 amthi_complete_list = &dev->amthi_read_complete_list.list;
1399 list_for_each_entry_safe(cb_pos, cb_next, amthi_complete_list, list) {
1401 cl_pos = cb_pos->file_object->private_data;
1403 /* Finding the AMTHI entry. */
1404 if (cl_pos == &dev->iamthif_cl)
1405 list_del(&cb_pos->list);
1407 if (dev->iamthif_current_cb)
1408 mei_free_cb_private(dev->iamthif_current_cb);
1410 dev->iamthif_file_object->private_data = NULL;
1411 dev->iamthif_file_object = NULL;
1412 dev->iamthif_current_cb = NULL;
1413 dev->iamthif_timer = 0;
1414 mei_run_next_iamthif_cmd(dev);
1419 schedule_delayed_work(&dev->timer_work, 2 * HZ);
1420 mutex_unlock(&dev->device_lock);
1424 * mei_interrupt_thread_handler - function called after ISR to handle the interrupt
1427 * @irq: The irq number
1428 * @dev_id: pointer to the device structure
1430 * returns irqreturn_t
1433 irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
1435 struct mei_device *dev = (struct mei_device *) dev_id;
1436 struct mei_cl_cb complete_list;
1437 struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
1441 bool bus_message_received;
1444 dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
1445 /* initialize our complete list */
1446 mutex_lock(&dev->device_lock);
1447 mei_io_list_init(&complete_list);
1448 dev->host_hw_state = mei_hcsr_read(dev);
1450 /* Ack the interrupt here
1451 * In case of MSI we don't go through the quick handler */
1452 if (pci_dev_msi_enabled(dev->pdev))
1453 mei_reg_write(dev, H_CSR, dev->host_hw_state);
1455 dev->me_hw_state = mei_mecsr_read(dev);
1457 /* check if ME wants a reset */
1458 if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
1459 dev->dev_state != MEI_DEV_RESETING &&
1460 dev->dev_state != MEI_DEV_INITIALIZING) {
1461 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1463 mutex_unlock(&dev->device_lock);
1467 /* check if we need to start the dev */
1468 if ((dev->host_hw_state & H_RDY) == 0) {
1469 if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
1470 dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
1471 dev->host_hw_state |= (H_IE | H_IG | H_RDY);
1473 dev->dev_state = MEI_DEV_INIT_CLIENTS;
1474 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
1475 /* link is established
1476 * start sending messages.
1478 mei_host_start_message(dev);
1479 mutex_unlock(&dev->device_lock);
1482 dev_dbg(&dev->pdev->dev, "FW not ready.\n");
1483 mutex_unlock(&dev->device_lock);
1487 /* check slots available for reading */
1488 slots = mei_count_full_read_slots(dev);
1489 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1490 slots, dev->extra_write_index);
1491 while (slots > 0 && !dev->extra_write_index) {
1492 dev_dbg(&dev->pdev->dev, "slots =%08x extra_write_index =%08x.\n",
1493 slots, dev->extra_write_index);
1494 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
1495 rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
1499 rets = mei_irq_thread_write_handler(&complete_list, dev, &slots);
1501 dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
1502 dev->host_hw_state = mei_hcsr_read(dev);
1503 dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
1505 bus_message_received = false;
1506 if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
1507 dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
1508 bus_message_received = true;
1510 mutex_unlock(&dev->device_lock);
1511 if (bus_message_received) {
1512 dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
1513 wake_up_interruptible(&dev->wait_recvd_msg);
1514 bus_message_received = false;
1516 if (list_empty(&complete_list.list))
1520 list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
1521 cl = (struct mei_cl *)cb_pos->file_private;
1522 list_del(&cb_pos->list);
1524 if (cl != &dev->iamthif_cl) {
1525 dev_dbg(&dev->pdev->dev, "completing call back.\n");
1526 _mei_cmpl(cl, cb_pos);
1528 } else if (cl == &dev->iamthif_cl) {
1529 _mei_cmpl_iamthif(dev, cb_pos);