interrupt.c 20.2 KB
Newer Older
O
Oren Weil 已提交
1 2 3
/*
 *
 * Intel Management Engine Interface (Intel MEI) Linux driver
4
 * Copyright (c) 2003-2012, Intel Corporation.
O
Oren Weil 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 */


#include <linux/pci.h>
#include <linux/kthread.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/jiffies.h>

24
#include <linux/mei.h>
25 26

#include "mei_dev.h"
O
Oren Weil 已提交
27 28 29 30 31 32 33 34 35 36 37
#include "interface.h"


/**
 * _mei_cmpl - processes completed operation.
 *
 * @cl: private data of the file object.
 * @cb_pos: callback block.
 */
static void _mei_cmpl(struct mei_cl *cl, struct mei_cl_cb *cb_pos)
{
38
	if (cb_pos->fop_type == MEI_FOP_WRITE) {
39
		mei_io_cb_free(cb_pos);
O
Oren Weil 已提交
40 41 42 43 44
		cb_pos = NULL;
		cl->writing_state = MEI_WRITE_COMPLETE;
		if (waitqueue_active(&cl->tx_wait))
			wake_up_interruptible(&cl->tx_wait);

45
	} else if (cb_pos->fop_type == MEI_FOP_READ &&
O
Oren Weil 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
			MEI_READING == cl->reading_state) {
		cl->reading_state = MEI_READ_COMPLETE;
		if (waitqueue_active(&cl->rx_wait))
			wake_up_interruptible(&cl->rx_wait);

	}
}

/**
 * _mei_irq_thread_state_ok - checks if mei header matches file private data
 *
 * @cl: private data of the file object
 * @mei_hdr: header of mei client message
 *
 * returns !=0 if matches, 0 if no match.
 */
static int _mei_irq_thread_state_ok(struct mei_cl *cl,
				struct mei_msg_hdr *mei_hdr)
{
	return (cl->host_client_id == mei_hdr->host_addr &&
		cl->me_client_id == mei_hdr->me_addr &&
		cl->state == MEI_FILE_CONNECTED &&
		MEI_READ_COMPLETE != cl->reading_state);
}

/**
 * mei_irq_thread_read_client_message - bottom half read routine after ISR to
 * handle the read mei client message data processing.
 *
 * @complete_list: An instance of our list structure
 * @dev: the device structure
 * @mei_hdr: header of mei client message
 *
 * returns 0 on success, <0 on failure.
 */
81
static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
O
Oren Weil 已提交
82 83 84 85 86
		struct mei_device *dev,
		struct mei_msg_hdr *mei_hdr)
{
	struct mei_cl *cl;
	struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
87
	unsigned char *buffer = NULL;
O
Oren Weil 已提交
88 89

	dev_dbg(&dev->pdev->dev, "start client msg\n");
90
	if (list_empty(&dev->read_list.list))
O
Oren Weil 已提交
91 92
		goto quit;

93
	list_for_each_entry_safe(cb_pos, cb_next, &dev->read_list.list, list) {
94
		cl = cb_pos->cl;
O
Oren Weil 已提交
95 96
		if (cl && _mei_irq_thread_state_ok(cl, mei_hdr)) {
			cl->reading_state = MEI_READING;
97
			buffer = cb_pos->response_buffer.data + cb_pos->buf_idx;
O
Oren Weil 已提交
98 99

			if (cb_pos->response_buffer.size <
100
					mei_hdr->length + cb_pos->buf_idx) {
O
Oren Weil 已提交
101
				dev_dbg(&dev->pdev->dev, "message overflow.\n");
102
				list_del(&cb_pos->list);
O
Oren Weil 已提交
103 104 105 106 107
				return -ENOMEM;
			}
			if (buffer)
				mei_read_slots(dev, buffer, mei_hdr->length);

108
			cb_pos->buf_idx += mei_hdr->length;
O
Oren Weil 已提交
109 110
			if (mei_hdr->msg_complete) {
				cl->status = 0;
111
				list_del(&cb_pos->list);
O
Oren Weil 已提交
112
				dev_dbg(&dev->pdev->dev,
113
					"completed read H cl = %d, ME cl = %d, length = %lu\n",
O
Oren Weil 已提交
114 115
					cl->host_client_id,
					cl->me_client_id,
116 117
					cb_pos->buf_idx);

118 119
				list_add_tail(&cb_pos->list,
						&complete_list->list);
O
Oren Weil 已提交
120 121 122 123 124 125 126 127 128 129
			}

			break;
		}

	}

quit:
	dev_dbg(&dev->pdev->dev, "message read\n");
	if (!buffer) {
130
		mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
131 132
		dev_dbg(&dev->pdev->dev, "discarding message " MEI_HDR_FMT "\n",
				MEI_HDR_PRM(mei_hdr));
O
Oren Weil 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
	}

	return 0;
}

/**
 * _mei_irq_thread_close - processes close related operation.
 *
 * @dev: the device structure.
 * @slots: free slots.
 * @cb_pos: callback block.
 * @cl: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns 0, OK; otherwise, error.
 */
static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
				struct mei_cl_cb *cb_pos,
				struct mei_cl *cl,
152
				struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
153
{
154
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
155
			sizeof(struct hbm_client_connect_request)))
156
		return -EBADMSG;
O
Oren Weil 已提交
157

158
	*slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
159

160
	if (mei_hbm_cl_disconnect_req(dev, cl)) {
161
		cl->status = 0;
162
		cb_pos->buf_idx = 0;
163
		list_move_tail(&cb_pos->list, &cmpl_list->list);
164
		return -EMSGSIZE;
O
Oren Weil 已提交
165
	} else {
166 167
		cl->state = MEI_FILE_DISCONNECTING;
		cl->status = 0;
168
		cb_pos->buf_idx = 0;
169
		list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
170
		cl->timer_count = MEI_CONNECT_TIMEOUT;
O
Oren Weil 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	}

	return 0;
}


/**
 * _mei_hb_read - processes read related operation.
 *
 * @dev: the device structure.
 * @slots: free slots.
 * @cb_pos: callback block.
 * @cl: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns 0, OK; otherwise, error.
 */
static int _mei_irq_thread_read(struct mei_device *dev,	s32 *slots,
			struct mei_cl_cb *cb_pos,
			struct mei_cl *cl,
191
			struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
192
{
T
Tomas Winkler 已提交
193
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
O
Oren Weil 已提交
194 195
			sizeof(struct hbm_flow_control))) {
		/* return the cancel routine */
196
		list_del(&cb_pos->list);
O
Oren Weil 已提交
197 198 199
		return -EBADMSG;
	}

200 201
	*slots -= mei_data2slots(sizeof(struct hbm_flow_control));

202
	if (mei_hbm_cl_flow_control_req(dev, cl)) {
203
		cl->status = -ENODEV;
204
		cb_pos->buf_idx = 0;
205
		list_move_tail(&cb_pos->list, &cmpl_list->list);
206 207
		return -ENODEV;
	}
208
	list_move_tail(&cb_pos->list, &dev->read_list.list);
209

O
Oren Weil 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	return 0;
}


/**
 * _mei_irq_thread_ioctl - processes ioctl related operation.
 *
 * @dev: the device structure.
 * @slots: free slots.
 * @cb_pos: callback block.
 * @cl: private data of the file object.
 * @cmpl_list: complete list.
 *
 * returns 0, OK; otherwise, error.
 */
static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
			struct mei_cl_cb *cb_pos,
			struct mei_cl *cl,
228
			struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
229
{
230
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
O
Oren Weil 已提交
231 232
			sizeof(struct hbm_client_connect_request))) {
		/* return the cancel routine */
233
		list_del(&cb_pos->list);
O
Oren Weil 已提交
234 235 236
		return -EBADMSG;
	}

237
	cl->state = MEI_FILE_CONNECTING;
238 239
	*slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
	if (mei_hbm_cl_connect_req(dev, cl)) {
240
		cl->status = -ENODEV;
241
		cb_pos->buf_idx = 0;
242
		list_del(&cb_pos->list);
243 244
		return -ENODEV;
	} else {
245
		list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
246 247
		cl->timer_count = MEI_CONNECT_TIMEOUT;
	}
O
Oren Weil 已提交
248 249 250 251
	return 0;
}

/**
252
 * mei_irq_thread_write_complete - write messages to device.
O
Oren Weil 已提交
253 254 255
 *
 * @dev: the device structure.
 * @slots: free slots.
256
 * @cb: callback block.
O
Oren Weil 已提交
257 258 259 260
 * @cmpl_list: complete list.
 *
 * returns 0, OK; otherwise, error.
 */
261 262
static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
			struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
263
{
264
	struct mei_msg_hdr mei_hdr;
265 266 267 268
	struct mei_cl *cl = cb->cl;
	size_t len = cb->request_buffer.size - cb->buf_idx;
	size_t msg_slots = mei_data2slots(len);

269 270 271
	mei_hdr.host_addr = cl->host_client_id;
	mei_hdr.me_addr = cl->me_client_id;
	mei_hdr.reserved = 0;
O
Oren Weil 已提交
272

273
	if (*slots >= msg_slots) {
274 275
		mei_hdr.length = len;
		mei_hdr.msg_complete = 1;
276
	/* Split the message only if we can write the whole host buffer */
277
	} else if (*slots == dev->hbuf_depth) {
278 279
		msg_slots = *slots;
		len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
280 281
		mei_hdr.length = len;
		mei_hdr.msg_complete = 0;
O
Oren Weil 已提交
282
	} else {
283 284
		/* wait for next time the host buffer is empty */
		return 0;
O
Oren Weil 已提交
285 286
	}

287 288
	dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
			cb->request_buffer.size, cb->buf_idx);
289
	dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
290 291

	*slots -=  msg_slots;
292
	if (mei_write_message(dev, &mei_hdr,
293
			cb->request_buffer.data + cb->buf_idx)) {
294 295 296 297 298 299 300 301 302
		cl->status = -ENODEV;
		list_move_tail(&cb->list, &cmpl_list->list);
		return -ENODEV;
	}

	if (mei_flow_ctrl_reduce(dev, cl))
		return -ENODEV;

	cl->status = 0;
303 304
	cb->buf_idx += mei_hdr.length;
	if (mei_hdr.msg_complete)
305 306
		list_move_tail(&cb->list, &dev->write_waiting_list.list);

O
Oren Weil 已提交
307 308 309 310 311 312 313 314 315 316 317 318 319
	return 0;
}

/**
 * mei_irq_thread_read_handler - bottom half read routine after ISR to
 * handle the read processing.
 *
 * @cmpl_list: An instance of our list structure
 * @dev: the device structure
 * @slots: slots to read.
 *
 * returns 0 on success, <0 on failure.
 */
320
static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
O
Oren Weil 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
		struct mei_device *dev,
		s32 *slots)
{
	struct mei_msg_hdr *mei_hdr;
	struct mei_cl *cl_pos = NULL;
	struct mei_cl *cl_next = NULL;
	int ret = 0;

	if (!dev->rd_msg_hdr) {
		dev->rd_msg_hdr = mei_mecbrw_read(dev);
		dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
		(*slots)--;
		dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
	}
	mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
336
	dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
O
Oren Weil 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

	if (mei_hdr->reserved || !dev->rd_msg_hdr) {
		dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
		ret = -EBADMSG;
		goto end;
	}

	if (mei_hdr->host_addr || mei_hdr->me_addr) {
		list_for_each_entry_safe(cl_pos, cl_next,
					&dev->file_list, link) {
			dev_dbg(&dev->pdev->dev,
					"list_for_each_entry_safe read host"
					" client = %d, ME client = %d\n",
					cl_pos->host_client_id,
					cl_pos->me_client_id);
			if (cl_pos->host_client_id == mei_hdr->host_addr &&
			    cl_pos->me_client_id == mei_hdr->me_addr)
				break;
		}

		if (&cl_pos->link == &dev->file_list) {
			dev_dbg(&dev->pdev->dev, "corrupted message header\n");
			ret = -EBADMSG;
			goto end;
		}
	}
	if (((*slots) * sizeof(u32)) < mei_hdr->length) {
		dev_dbg(&dev->pdev->dev,
				"we can't read the message slots =%08x.\n",
				*slots);
		/* we can't read the message */
		ret = -ERANGE;
		goto end;
	}

	/* decide where to read the message too */
	if (!mei_hdr->host_addr) {
		dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
375
		mei_hbm_dispatch(dev, mei_hdr);
O
Oren Weil 已提交
376 377 378 379 380
		dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
	} else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
		   (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
		   (dev->iamthif_state == MEI_IAMTHIF_READING)) {
		dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
381 382

		dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
383 384

		ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
O
Oren Weil 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
		if (ret)
			goto end;
	} else {
		dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_client_message.\n");
		ret = mei_irq_thread_read_client_message(cmpl_list,
							 dev, mei_hdr);
		if (ret)
			goto end;

	}

	/* reset the number of slots and header */
	*slots = mei_count_full_read_slots(dev);
	dev->rd_msg_hdr = 0;

	if (*slots == -EOVERFLOW) {
		/* overflow - reset */
		dev_dbg(&dev->pdev->dev, "resetting due to slots overflow.\n");
		/* set the event since message has been read */
		ret = -ERANGE;
		goto end;
	}
end:
	return ret;
}


/**
 * mei_irq_thread_write_handler - bottom half write routine after
 * ISR to handle the write processing.
 *
 * @dev: the device structure
417
 * @cmpl_list: An instance of our list structure
O
Oren Weil 已提交
418 419 420
 *
 * returns 0 on success, <0 on failure.
 */
421 422
static int mei_irq_thread_write_handler(struct mei_device *dev,
				struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
423 424 425
{

	struct mei_cl *cl;
426
	struct mei_cl_cb *pos = NULL, *next = NULL;
427
	struct mei_cl_cb *list;
428
	s32 slots;
O
Oren Weil 已提交
429 430
	int ret;

431
	if (!mei_hbuf_is_empty(dev)) {
O
Oren Weil 已提交
432 433 434
		dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
		return 0;
	}
435 436
	slots = mei_hbuf_empty_slots(dev);
	if (slots <= 0)
437 438
		return -EMSGSIZE;

O
Oren Weil 已提交
439 440 441 442
	/* complete all waiting for write CB */
	dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");

	list = &dev->write_waiting_list;
443
	list_for_each_entry_safe(pos, next, &list->list, list) {
444
		cl = pos->cl;
445 446 447 448
		if (cl == NULL)
			continue;

		cl->status = 0;
449
		list_del(&pos->list);
450
		if (MEI_WRITING == cl->writing_state &&
451 452
		    pos->fop_type == MEI_FOP_WRITE &&
		    cl != &dev->iamthif_cl) {
453
			dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
454
			cl->writing_state = MEI_WRITE_COMPLETE;
455
			list_add_tail(&pos->list, &cmpl_list->list);
456 457 458 459
		}
		if (cl == &dev->iamthif_cl) {
			dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
			if (dev->iamthif_flow_control_pending) {
460
				ret = mei_amthif_irq_read(dev, &slots);
461 462
				if (ret)
					return ret;
O
Oren Weil 已提交
463 464 465 466
			}
		}
	}

467 468
	if (dev->wd_state == MEI_WD_STOPPING) {
		dev->wd_state = MEI_WD_IDLE;
O
Oren Weil 已提交
469 470 471
		wake_up_interruptible(&dev->wait_stop_wd);
	}

472 473
	if (dev->wr_ext_msg.hdr.length) {
		mei_write_message(dev, &dev->wr_ext_msg.hdr,
474
				dev->wr_ext_msg.data);
475
		slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
476
		dev->wr_ext_msg.hdr.length = 0;
O
Oren Weil 已提交
477
	}
478
	if (dev->dev_state == MEI_DEV_ENABLED) {
O
Oren Weil 已提交
479
		if (dev->wd_pending &&
480
		    mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
O
Oren Weil 已提交
481 482
			if (mei_wd_send(dev))
				dev_dbg(&dev->pdev->dev, "wd send failed.\n");
483 484
			else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
				return -ENODEV;
O
Oren Weil 已提交
485

486
			dev->wd_pending = false;
O
Oren Weil 已提交
487

488
			if (dev->wd_state == MEI_WD_RUNNING)
489
				slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
490
			else
491
				slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
O
Oren Weil 已提交
492 493 494 495
		}
	}

	/* complete control write list CB */
496
	dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
497
	list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
498
		cl = pos->cl;
499
		if (!cl) {
500
			list_del(&pos->list);
501 502
			return -ENODEV;
		}
503 504
		switch (pos->fop_type) {
		case MEI_FOP_CLOSE:
505
			/* send disconnect message */
506 507
			ret = _mei_irq_thread_close(dev, &slots, pos,
						cl, cmpl_list);
508 509
			if (ret)
				return ret;
O
Oren Weil 已提交
510

511
			break;
512
		case MEI_FOP_READ:
513
			/* send flow control message */
514 515
			ret = _mei_irq_thread_read(dev, &slots, pos,
						cl, cmpl_list);
516 517
			if (ret)
				return ret;
O
Oren Weil 已提交
518

519
			break;
520
		case MEI_FOP_IOCTL:
521
			/* connect message */
522
			if (mei_other_client_is_connecting(dev, cl))
523
				continue;
524 525
			ret = _mei_irq_thread_ioctl(dev, &slots, pos,
						cl, cmpl_list);
526 527
			if (ret)
				return ret;
O
Oren Weil 已提交
528

529
			break;
O
Oren Weil 已提交
530

531 532
		default:
			BUG();
O
Oren Weil 已提交
533
		}
534

O
Oren Weil 已提交
535 536
	}
	/* complete  write list CB */
537
	dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
538
	list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
539
		cl = pos->cl;
540 541
		if (cl == NULL)
			continue;
542 543 544 545 546 547
		if (mei_flow_ctrl_creds(dev, cl) <= 0) {
			dev_dbg(&dev->pdev->dev,
				"No flow control credentials for client %d, not sending.\n",
				cl->host_client_id);
			continue;
		}
548

549
		if (cl == &dev->iamthif_cl)
550
			ret = mei_amthif_irq_write_complete(dev, &slots,
551
							pos, cmpl_list);
552 553 554 555 556
		else
			ret = mei_irq_thread_write_complete(dev, &slots, pos,
						cmpl_list);
		if (ret)
			return ret;
557

O
Oren Weil 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570
	}
	return 0;
}



/**
 * mei_timer - timer function.
 *
 * @work: pointer to the work_struct structure
 *
 * NOTE: This function is called by timer interrupt work
 */
571
void mei_timer(struct work_struct *work)
O
Oren Weil 已提交
572 573 574 575 576 577 578 579
{
	unsigned long timeout;
	struct mei_cl *cl_pos = NULL;
	struct mei_cl *cl_next = NULL;
	struct mei_cl_cb  *cb_pos = NULL;
	struct mei_cl_cb  *cb_next = NULL;

	struct mei_device *dev = container_of(work,
580
					struct mei_device, timer_work.work);
O
Oren Weil 已提交
581 582 583


	mutex_lock(&dev->device_lock);
584 585
	if (dev->dev_state != MEI_DEV_ENABLED) {
		if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
O
Oren Weil 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
			if (dev->init_clients_timer) {
				if (--dev->init_clients_timer == 0) {
					dev_dbg(&dev->pdev->dev, "IMEI reset due to init clients timeout ,init clients state = %d.\n",
						dev->init_clients_state);
					mei_reset(dev, 1);
				}
			}
		}
		goto out;
	}
	/*** connect/disconnect timeouts ***/
	list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
		if (cl_pos->timer_count) {
			if (--cl_pos->timer_count == 0) {
				dev_dbg(&dev->pdev->dev, "HECI reset due to connect/disconnect timeout.\n");
				mei_reset(dev, 1);
				goto out;
			}
		}
	}

	if (dev->iamthif_stall_timer) {
		if (--dev->iamthif_stall_timer == 0) {
609
			dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
O
Oren Weil 已提交
610 611 612
			mei_reset(dev, 1);
			dev->iamthif_msg_buf_size = 0;
			dev->iamthif_msg_buf_index = 0;
613 614
			dev->iamthif_canceled = false;
			dev->iamthif_ioctl = true;
O
Oren Weil 已提交
615 616 617
			dev->iamthif_state = MEI_IAMTHIF_IDLE;
			dev->iamthif_timer = 0;

618 619
			mei_io_cb_free(dev->iamthif_current_cb);
			dev->iamthif_current_cb = NULL;
O
Oren Weil 已提交
620 621

			dev->iamthif_file_object = NULL;
622
			mei_amthif_run_next_cmd(dev);
O
Oren Weil 已提交
623 624 625 626 627 628
		}
	}

	if (dev->iamthif_timer) {

		timeout = dev->iamthif_timer +
629
			mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
O
Oren Weil 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642

		dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
				dev->iamthif_timer);
		dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
		dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
		if (time_after(jiffies, timeout)) {
			/*
			 * User didn't read the AMTHI data on time (15sec)
			 * freeing AMTHI for other requests
			 */

			dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");

643 644
			list_for_each_entry_safe(cb_pos, cb_next,
				&dev->amthif_rd_complete_list.list, list) {
O
Oren Weil 已提交
645

646
				cl_pos = cb_pos->file_object->private_data;
O
Oren Weil 已提交
647

648 649
				/* Finding the AMTHI entry. */
				if (cl_pos == &dev->iamthif_cl)
650
					list_del(&cb_pos->list);
O
Oren Weil 已提交
651
			}
652 653
			mei_io_cb_free(dev->iamthif_current_cb);
			dev->iamthif_current_cb = NULL;
O
Oren Weil 已提交
654 655 656 657

			dev->iamthif_file_object->private_data = NULL;
			dev->iamthif_file_object = NULL;
			dev->iamthif_timer = 0;
658
			mei_amthif_run_next_cmd(dev);
O
Oren Weil 已提交
659 660 661 662

		}
	}
out:
663 664
	schedule_delayed_work(&dev->timer_work, 2 * HZ);
	mutex_unlock(&dev->device_lock);
O
Oren Weil 已提交
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
}

/**
 *  mei_interrupt_thread_handler - function called after ISR to handle the interrupt
 * processing.
 *
 * @irq: The irq number
 * @dev_id: pointer to the device structure
 *
 * returns irqreturn_t
 *
 */
irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id)
{
	struct mei_device *dev = (struct mei_device *) dev_id;
680
	struct mei_cl_cb complete_list;
O
Oren Weil 已提交
681 682 683 684 685 686 687 688 689 690
	struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
	struct mei_cl *cl;
	s32 slots;
	int rets;
	bool  bus_message_received;


	dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
	/* initialize our complete list */
	mutex_lock(&dev->device_lock);
691
	mei_io_list_init(&complete_list);
O
Oren Weil 已提交
692
	dev->host_hw_state = mei_hcsr_read(dev);
693 694

	/* Ack the interrupt here
695
	 * In case of MSI we don't go through the quick handler */
696
	if (pci_dev_msi_enabled(dev->pdev))
697
		mei_clear_interrupts(dev);
698

O
Oren Weil 已提交
699 700 701 702
	dev->me_hw_state = mei_mecsr_read(dev);

	/* check if ME wants a reset */
	if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
703 704
	    dev->dev_state != MEI_DEV_RESETING &&
	    dev->dev_state != MEI_DEV_INITIALIZING) {
O
Oren Weil 已提交
705 706 707 708 709 710 711 712 713 714 715 716
		dev_dbg(&dev->pdev->dev, "FW not ready.\n");
		mei_reset(dev, 1);
		mutex_unlock(&dev->device_lock);
		return IRQ_HANDLED;
	}

	/*  check if we need to start the dev */
	if ((dev->host_hw_state & H_RDY) == 0) {
		if ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA) {
			dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
			dev->host_hw_state |= (H_IE | H_IG | H_RDY);
			mei_hcsr_set(dev);
717
			dev->dev_state = MEI_DEV_INIT_CLIENTS;
O
Oren Weil 已提交
718 719 720 721
			dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
			/* link is established
			 * start sending messages.
			 */
722
			mei_hbm_start_req(dev);
O
Oren Weil 已提交
723 724 725 726 727 728 729 730
			mutex_unlock(&dev->device_lock);
			return IRQ_HANDLED;
		} else {
			dev_dbg(&dev->pdev->dev, "FW not ready.\n");
			mutex_unlock(&dev->device_lock);
			return IRQ_HANDLED;
		}
	}
731
	/* check slots available for reading */
O
Oren Weil 已提交
732
	slots = mei_count_full_read_slots(dev);
733 734 735 736 737
	while (slots > 0) {
		/* we have urgent data to send so break the read */
		if (dev->wr_ext_msg.hdr.length)
			break;
		dev_dbg(&dev->pdev->dev, "slots =%08x\n", slots);
O
Oren Weil 已提交
738 739 740 741 742
		dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_handler.\n");
		rets = mei_irq_thread_read_handler(&complete_list, dev, &slots);
		if (rets)
			goto end;
	}
743
	rets = mei_irq_thread_write_handler(dev, &complete_list);
O
Oren Weil 已提交
744 745 746
end:
	dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
	dev->host_hw_state = mei_hcsr_read(dev);
747
	dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
O
Oren Weil 已提交
748 749 750 751 752 753 754 755 756 757 758 759

	bus_message_received = false;
	if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
		dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
		bus_message_received = true;
	}
	mutex_unlock(&dev->device_lock);
	if (bus_message_received) {
		dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
		wake_up_interruptible(&dev->wait_recvd_msg);
		bus_message_received = false;
	}
760
	if (list_empty(&complete_list.list))
O
Oren Weil 已提交
761 762 763
		return IRQ_HANDLED;


764
	list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
765
		cl = cb_pos->cl;
766
		list_del(&cb_pos->list);
O
Oren Weil 已提交
767 768 769 770 771 772
		if (cl) {
			if (cl != &dev->iamthif_cl) {
				dev_dbg(&dev->pdev->dev, "completing call back.\n");
				_mei_cmpl(cl, cb_pos);
				cb_pos = NULL;
			} else if (cl == &dev->iamthif_cl) {
773
				mei_amthif_complete(dev, cb_pos);
O
Oren Weil 已提交
774 775 776 777 778
			}
		}
	}
	return IRQ_HANDLED;
}