interrupt.c 20.0 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"
27
#include "hbm.h"
T
Tomas Winkler 已提交
28
#include "hw-me.h"
T
Tomas Winkler 已提交
29
#include "client.h"
O
Oren Weil 已提交
30 31 32 33 34 35 36 37 38 39


/**
 * _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)
{
40
	if (cb_pos->fop_type == MEI_FOP_WRITE) {
41
		mei_io_cb_free(cb_pos);
O
Oren Weil 已提交
42 43 44 45 46
		cb_pos = NULL;
		cl->writing_state = MEI_WRITE_COMPLETE;
		if (waitqueue_active(&cl->tx_wait))
			wake_up_interruptible(&cl->tx_wait);

47
	} else if (cb_pos->fop_type == MEI_FOP_READ &&
O
Oren Weil 已提交
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 81 82
			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.
 */
83
static int mei_irq_thread_read_client_message(struct mei_cl_cb *complete_list,
O
Oren Weil 已提交
84 85 86 87 88
		struct mei_device *dev,
		struct mei_msg_hdr *mei_hdr)
{
	struct mei_cl *cl;
	struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
89
	unsigned char *buffer = NULL;
O
Oren Weil 已提交
90 91

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

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

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

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

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

			break;
		}

	}

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

	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,
154
				struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
155
{
156
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
157
			sizeof(struct hbm_client_connect_request)))
158
		return -EBADMSG;
O
Oren Weil 已提交
159

160
	*slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
161

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

	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,
193
			struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
194
{
T
Tomas Winkler 已提交
195
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
O
Oren Weil 已提交
196 197
			sizeof(struct hbm_flow_control))) {
		/* return the cancel routine */
198
		list_del(&cb_pos->list);
O
Oren Weil 已提交
199 200 201
		return -EBADMSG;
	}

202 203
	*slots -= mei_data2slots(sizeof(struct hbm_flow_control));

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

O
Oren Weil 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
	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,
230
			struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
231
{
232
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
O
Oren Weil 已提交
233 234
			sizeof(struct hbm_client_connect_request))) {
		/* return the cancel routine */
235
		list_del(&cb_pos->list);
O
Oren Weil 已提交
236 237 238
		return -EBADMSG;
	}

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

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

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

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

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

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

T
Tomas Winkler 已提交
301
	if (mei_cl_flow_ctrl_reduce(cl))
302 303 304
		return -ENODEV;

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

O
Oren Weil 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321
	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.
 */
322
static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
O
Oren Weil 已提交
323 324 325 326 327 328 329 330 331
		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) {
332
		dev->rd_msg_hdr = mei_read_hdr(dev);
O
Oren Weil 已提交
333 334 335 336 337
		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;
338
	dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
O
Oren Weil 已提交
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 375 376

	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");
377
		mei_hbm_dispatch(dev, mei_hdr);
O
Oren Weil 已提交
378 379 380 381 382
		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");
383 384

		dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
385 386

		ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
O
Oren Weil 已提交
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 417 418
		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
419
 * @cmpl_list: An instance of our list structure
O
Oren Weil 已提交
420 421 422
 *
 * returns 0 on success, <0 on failure.
 */
423 424
static int mei_irq_thread_write_handler(struct mei_device *dev,
				struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
425 426 427
{

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

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

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

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

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

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

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

488
			dev->wd_pending = false;
O
Oren Weil 已提交
489

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

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

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

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

531
			break;
O
Oren Weil 已提交
532

533 534
		default:
			BUG();
O
Oren Weil 已提交
535
		}
536

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

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

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



/**
 * mei_timer - timer function.
 *
 * @work: pointer to the work_struct structure
 *
 * NOTE: This function is called by timer interrupt work
 */
573
void mei_timer(struct work_struct *work)
O
Oren Weil 已提交
574 575 576 577 578 579 580 581
{
	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,
582
					struct mei_device, timer_work.work);
O
Oren Weil 已提交
583 584 585


	mutex_lock(&dev->device_lock);
586 587
	if (dev->dev_state != MEI_DEV_ENABLED) {
		if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
O
Oren Weil 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
			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) {
611
			dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
O
Oren Weil 已提交
612 613 614
			mei_reset(dev, 1);
			dev->iamthif_msg_buf_size = 0;
			dev->iamthif_msg_buf_index = 0;
615 616
			dev->iamthif_canceled = false;
			dev->iamthif_ioctl = true;
O
Oren Weil 已提交
617 618 619
			dev->iamthif_state = MEI_IAMTHIF_IDLE;
			dev->iamthif_timer = 0;

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

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

	if (dev->iamthif_timer) {

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

		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");

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

648
				cl_pos = cb_pos->file_object->private_data;
O
Oren Weil 已提交
649

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

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

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

/**
 *  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;
682
	struct mei_cl_cb complete_list;
O
Oren Weil 已提交
683 684 685 686 687 688 689 690 691 692
	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);
693
	mei_io_list_init(&complete_list);
694 695

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

O
Oren Weil 已提交
700
	/* check if ME wants a reset */
701
	if (!mei_hw_is_ready(dev) &&
702 703
	    dev->dev_state != MEI_DEV_RESETING &&
	    dev->dev_state != MEI_DEV_INITIALIZING) {
O
Oren Weil 已提交
704 705 706 707 708 709 710
		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 */
711
	if (!mei_host_is_ready(dev)) {
712
		if (mei_hw_is_ready(dev)) {
O
Oren Weil 已提交
713
			dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
714 715 716

			mei_host_set_ready(dev);

O
Oren Weil 已提交
717
			dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
718 719 720 721
			/* link is established * start sending messages.  */

			dev->dev_state = MEI_DEV_INIT_CLIENTS;

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
end:
	dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
746
	dev->mei_host_buffer_is_empty = mei_hbuf_is_ready(dev);
O
Oren Weil 已提交
747 748 749 750 751 752 753 754 755 756 757 758

	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;
	}
759
	if (list_empty(&complete_list.list))
O
Oren Weil 已提交
760 761 762
		return IRQ_HANDLED;


763
	list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
764
		cl = cb_pos->cl;
765
		list_del(&cb_pos->list);
O
Oren Weil 已提交
766 767 768 769 770 771
		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) {
772
				mei_amthif_complete(dev, cb_pos);
O
Oren Weil 已提交
773 774 775 776 777
			}
		}
	}
	return IRQ_HANDLED;
}