interrupt.c 25.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"
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 161

	if (mei_disconnect(dev, cl)) {
		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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	}

	return 0;
}

/**
 * is_treat_specially_client - checks if the message belongs
 * to the file private data.
 *
 * @cl: private data of the file object
 * @rs: connect response bus message
 *
 */
static bool is_treat_specially_client(struct mei_cl *cl,
		struct hbm_client_connect_response *rs)
{

	if (cl->host_client_id == rs->host_addr &&
	    cl->me_client_id == rs->me_addr) {
		if (!rs->status) {
			cl->state = MEI_FILE_CONNECTED;
			cl->status = 0;

		} else {
			cl->state = MEI_FILE_DISCONNECTED;
			cl->status = -ENODEV;
		}
		cl->timer_count = 0;

		return true;
	}
	return false;
}

/**
 * mei_client_connect_response - connects to response irq routine
 *
 * @dev: the device structure
 * @rs: connect response bus message
 */
211
void mei_client_connect_response(struct mei_device *dev,
O
Oren Weil 已提交
212 213 214 215
		struct hbm_client_connect_response *rs)
{

	struct mei_cl *cl;
216
	struct mei_cl_cb *pos = NULL, *next = NULL;
O
Oren Weil 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230

	dev_dbg(&dev->pdev->dev,
			"connect_response:\n"
			"ME Client = %d\n"
			"Host Client = %d\n"
			"Status = %d\n",
			rs->me_addr,
			rs->host_addr,
			rs->status);

	/* if WD or iamthif client treat specially */

	if (is_treat_specially_client(&(dev->wd_cl), rs)) {
		dev_dbg(&dev->pdev->dev, "successfully connected to WD client.\n");
231
		mei_watchdog_register(dev);
232

O
Oren Weil 已提交
233 234 235 236 237 238 239
		return;
	}

	if (is_treat_specially_client(&(dev->iamthif_cl), rs)) {
		dev->iamthif_state = MEI_IAMTHIF_IDLE;
		return;
	}
240
	list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
241

242
		cl = pos->cl;
243
		if (!cl) {
244
			list_del(&pos->list);
245 246
			return;
		}
247
		if (pos->fop_type == MEI_FOP_IOCTL) {
248
			if (is_treat_specially_client(cl, rs)) {
249
				list_del(&pos->list);
250 251 252
				cl->status = 0;
				cl->timer_count = 0;
				break;
O
Oren Weil 已提交
253 254 255 256 257 258 259 260 261 262 263
			}
		}
	}
}

/**
 * mei_client_disconnect_response - disconnects from response irq routine
 *
 * @dev: the device structure
 * @rs: disconnect response bus message
 */
264 265
void mei_client_disconnect_response(struct mei_device *dev,
				    struct hbm_client_connect_response *rs)
O
Oren Weil 已提交
266 267
{
	struct mei_cl *cl;
268
	struct mei_cl_cb *pos = NULL, *next = NULL;
O
Oren Weil 已提交
269 270 271 272 273 274 275 276 277 278

	dev_dbg(&dev->pdev->dev,
			"disconnect_response:\n"
			"ME Client = %d\n"
			"Host Client = %d\n"
			"Status = %d\n",
			rs->me_addr,
			rs->host_addr,
			rs->status);

279
	list_for_each_entry_safe(pos, next, &dev->ctrl_rd_list.list, list) {
280
		cl = pos->cl;
O
Oren Weil 已提交
281

282
		if (!cl) {
283
			list_del(&pos->list);
284 285
			return;
		}
O
Oren Weil 已提交
286

287 288 289
		dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in ctrl_rd_list.\n");
		if (cl->host_client_id == rs->host_addr &&
		    cl->me_client_id == rs->me_addr) {
O
Oren Weil 已提交
290

291
			list_del(&pos->list);
292 293
			if (!rs->status)
				cl->state = MEI_FILE_DISCONNECTED;
O
Oren Weil 已提交
294

295 296 297
			cl->status = 0;
			cl->timer_count = 0;
			break;
O
Oren Weil 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
		}
	}
}

/**
 * same_flow_addr - tells if they have the same address.
 *
 * @file: private data of the file object.
 * @flow: flow control.
 *
 * returns  !=0, same; 0,not.
 */
static int same_flow_addr(struct mei_cl *cl, struct hbm_flow_control *flow)
{
	return (cl->host_client_id == flow->host_addr &&
		cl->me_client_id == flow->me_addr);
}

/**
 * add_single_flow_creds - adds single buffer credentials.
 *
 * @file: private data ot the file object.
 * @flow: flow control.
 */
static void add_single_flow_creds(struct mei_device *dev,
				  struct hbm_flow_control *flow)
{
	struct mei_me_client *client;
	int i;

328
	for (i = 0; i < dev->me_clients_num; i++) {
O
Oren Weil 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		client = &dev->me_clients[i];
		if (client && flow->me_addr == client->client_id) {
			if (client->props.single_recv_buf) {
				client->mei_flow_ctrl_creds++;
				dev_dbg(&dev->pdev->dev, "recv flow ctrl msg ME %d (single).\n",
				    flow->me_addr);
				dev_dbg(&dev->pdev->dev, "flow control credentials =%d.\n",
				    client->mei_flow_ctrl_creds);
			} else {
				BUG();	/* error in flow control */
			}
		}
	}
}

/**
 * mei_client_flow_control_response - flow control response irq routine
 *
 * @dev: the device structure
 * @flow_control: flow control response bus message
 */
350
void mei_client_flow_control_response(struct mei_device *dev,
O
Oren Weil 已提交
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
		struct hbm_flow_control *flow_control)
{
	struct mei_cl *cl_pos = NULL;
	struct mei_cl *cl_next = NULL;

	if (!flow_control->host_addr) {
		/* single receive buffer */
		add_single_flow_creds(dev, flow_control);
	} else {
		/* normal connection */
		list_for_each_entry_safe(cl_pos, cl_next,
				&dev->file_list, link) {
			dev_dbg(&dev->pdev->dev, "list_for_each_entry_safe in file_list\n");

			dev_dbg(&dev->pdev->dev, "cl of host client %d ME client %d.\n",
			    cl_pos->host_client_id,
			    cl_pos->me_client_id);
			dev_dbg(&dev->pdev->dev, "flow ctrl msg for host %d ME %d.\n",
			    flow_control->host_addr,
			    flow_control->me_addr);
			if (same_flow_addr(cl_pos, flow_control)) {
				dev_dbg(&dev->pdev->dev, "recv ctrl msg for host  %d ME %d.\n",
				    flow_control->host_addr,
				    flow_control->me_addr);
				cl_pos->mei_flow_ctrl_creds++;
				dev_dbg(&dev->pdev->dev, "flow control credentials = %d.\n",
				    cl_pos->mei_flow_ctrl_creds);
				break;
			}
		}
	}
}


/**
 * _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,
399
			struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
400
{
T
Tomas Winkler 已提交
401
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
O
Oren Weil 已提交
402 403
			sizeof(struct hbm_flow_control))) {
		/* return the cancel routine */
404
		list_del(&cb_pos->list);
O
Oren Weil 已提交
405 406 407
		return -EBADMSG;
	}

408 409
	*slots -= mei_data2slots(sizeof(struct hbm_flow_control));

410 411
	if (mei_send_flow_control(dev, cl)) {
		cl->status = -ENODEV;
412
		cb_pos->buf_idx = 0;
413
		list_move_tail(&cb_pos->list, &cmpl_list->list);
414 415
		return -ENODEV;
	}
416
	list_move_tail(&cb_pos->list, &dev->read_list.list);
417

O
Oren Weil 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	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,
436
			struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
437
{
438
	if ((*slots * sizeof(u32)) < (sizeof(struct mei_msg_hdr) +
O
Oren Weil 已提交
439 440
			sizeof(struct hbm_client_connect_request))) {
		/* return the cancel routine */
441
		list_del(&cb_pos->list);
O
Oren Weil 已提交
442 443 444
		return -EBADMSG;
	}

445 446 447 448
	cl->state = MEI_FILE_CONNECTING;
	 *slots -= mei_data2slots(sizeof(struct hbm_client_connect_request));
	if (mei_connect(dev, cl)) {
		cl->status = -ENODEV;
449
		cb_pos->buf_idx = 0;
450
		list_del(&cb_pos->list);
451 452
		return -ENODEV;
	} else {
453
		list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
454 455
		cl->timer_count = MEI_CONNECT_TIMEOUT;
	}
O
Oren Weil 已提交
456 457 458 459
	return 0;
}

/**
460
 * mei_irq_thread_write_complete - write messages to device.
O
Oren Weil 已提交
461 462 463
 *
 * @dev: the device structure.
 * @slots: free slots.
464
 * @cb: callback block.
O
Oren Weil 已提交
465 466 467 468
 * @cmpl_list: complete list.
 *
 * returns 0, OK; otherwise, error.
 */
469 470
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 已提交
471
{
472
	struct mei_msg_hdr mei_hdr;
473 474 475 476
	struct mei_cl *cl = cb->cl;
	size_t len = cb->request_buffer.size - cb->buf_idx;
	size_t msg_slots = mei_data2slots(len);

477 478 479
	mei_hdr.host_addr = cl->host_client_id;
	mei_hdr.me_addr = cl->me_client_id;
	mei_hdr.reserved = 0;
O
Oren Weil 已提交
480

481
	if (*slots >= msg_slots) {
482 483
		mei_hdr.length = len;
		mei_hdr.msg_complete = 1;
484
	/* Split the message only if we can write the whole host buffer */
485
	} else if (*slots == dev->hbuf_depth) {
486 487
		msg_slots = *slots;
		len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
488 489
		mei_hdr.length = len;
		mei_hdr.msg_complete = 0;
O
Oren Weil 已提交
490
	} else {
491 492
		/* wait for next time the host buffer is empty */
		return 0;
O
Oren Weil 已提交
493 494
	}

495 496
	dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
			cb->request_buffer.size, cb->buf_idx);
497
	dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
498 499

	*slots -=  msg_slots;
500
	if (mei_write_message(dev, &mei_hdr,
501
			cb->request_buffer.data + cb->buf_idx)) {
502 503 504 505 506 507 508 509 510
		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;
511 512
	cb->buf_idx += mei_hdr.length;
	if (mei_hdr.msg_complete)
513 514
		list_move_tail(&cb->list, &dev->write_waiting_list.list);

O
Oren Weil 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527
	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.
 */
528
static int mei_irq_thread_read_handler(struct mei_cl_cb *cmpl_list,
O
Oren Weil 已提交
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		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;
544
	dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
O
Oren Weil 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582

	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");
583
		mei_hbm_dispatch(dev, mei_hdr);
O
Oren Weil 已提交
584 585 586 587 588
		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");
589 590

		dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
591 592

		ret = mei_amthif_irq_read_message(cmpl_list, dev, mei_hdr);
O
Oren Weil 已提交
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
		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
625
 * @cmpl_list: An instance of our list structure
O
Oren Weil 已提交
626 627 628
 *
 * returns 0 on success, <0 on failure.
 */
629 630
static int mei_irq_thread_write_handler(struct mei_device *dev,
				struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
631 632 633
{

	struct mei_cl *cl;
634
	struct mei_cl_cb *pos = NULL, *next = NULL;
635
	struct mei_cl_cb *list;
636
	s32 slots;
O
Oren Weil 已提交
637 638
	int ret;

639
	if (!mei_hbuf_is_empty(dev)) {
O
Oren Weil 已提交
640 641 642
		dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
		return 0;
	}
643 644
	slots = mei_hbuf_empty_slots(dev);
	if (slots <= 0)
645 646
		return -EMSGSIZE;

O
Oren Weil 已提交
647 648 649 650
	/* complete all waiting for write CB */
	dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");

	list = &dev->write_waiting_list;
651
	list_for_each_entry_safe(pos, next, &list->list, list) {
652
		cl = pos->cl;
653 654 655 656
		if (cl == NULL)
			continue;

		cl->status = 0;
657
		list_del(&pos->list);
658
		if (MEI_WRITING == cl->writing_state &&
659 660
		    pos->fop_type == MEI_FOP_WRITE &&
		    cl != &dev->iamthif_cl) {
661
			dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
662
			cl->writing_state = MEI_WRITE_COMPLETE;
663
			list_add_tail(&pos->list, &cmpl_list->list);
664 665 666 667
		}
		if (cl == &dev->iamthif_cl) {
			dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
			if (dev->iamthif_flow_control_pending) {
668
				ret = mei_amthif_irq_read(dev, &slots);
669 670
				if (ret)
					return ret;
O
Oren Weil 已提交
671 672 673 674
			}
		}
	}

675 676
	if (dev->wd_state == MEI_WD_STOPPING) {
		dev->wd_state = MEI_WD_IDLE;
O
Oren Weil 已提交
677 678 679
		wake_up_interruptible(&dev->wait_stop_wd);
	}

680 681
	if (dev->wr_ext_msg.hdr.length) {
		mei_write_message(dev, &dev->wr_ext_msg.hdr,
682
				dev->wr_ext_msg.data);
683
		slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
684
		dev->wr_ext_msg.hdr.length = 0;
O
Oren Weil 已提交
685
	}
686
	if (dev->dev_state == MEI_DEV_ENABLED) {
O
Oren Weil 已提交
687
		if (dev->wd_pending &&
688
		    mei_flow_ctrl_creds(dev, &dev->wd_cl) > 0) {
O
Oren Weil 已提交
689 690
			if (mei_wd_send(dev))
				dev_dbg(&dev->pdev->dev, "wd send failed.\n");
691 692
			else if (mei_flow_ctrl_reduce(dev, &dev->wd_cl))
				return -ENODEV;
O
Oren Weil 已提交
693

694
			dev->wd_pending = false;
O
Oren Weil 已提交
695

696
			if (dev->wd_state == MEI_WD_RUNNING)
697
				slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
698
			else
699
				slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
O
Oren Weil 已提交
700 701 702 703
		}
	}

	/* complete control write list CB */
704
	dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
705
	list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
706
		cl = pos->cl;
707
		if (!cl) {
708
			list_del(&pos->list);
709 710
			return -ENODEV;
		}
711 712
		switch (pos->fop_type) {
		case MEI_FOP_CLOSE:
713
			/* send disconnect message */
714 715
			ret = _mei_irq_thread_close(dev, &slots, pos,
						cl, cmpl_list);
716 717
			if (ret)
				return ret;
O
Oren Weil 已提交
718

719
			break;
720
		case MEI_FOP_READ:
721
			/* send flow control message */
722 723
			ret = _mei_irq_thread_read(dev, &slots, pos,
						cl, cmpl_list);
724 725
			if (ret)
				return ret;
O
Oren Weil 已提交
726

727
			break;
728
		case MEI_FOP_IOCTL:
729
			/* connect message */
730
			if (mei_other_client_is_connecting(dev, cl))
731
				continue;
732 733
			ret = _mei_irq_thread_ioctl(dev, &slots, pos,
						cl, cmpl_list);
734 735
			if (ret)
				return ret;
O
Oren Weil 已提交
736

737
			break;
O
Oren Weil 已提交
738

739 740
		default:
			BUG();
O
Oren Weil 已提交
741
		}
742

O
Oren Weil 已提交
743 744
	}
	/* complete  write list CB */
745
	dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
746
	list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
747
		cl = pos->cl;
748 749
		if (cl == NULL)
			continue;
750 751 752 753 754 755
		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;
		}
756

757
		if (cl == &dev->iamthif_cl)
758
			ret = mei_amthif_irq_write_complete(dev, &slots,
759
							pos, cmpl_list);
760 761 762 763 764
		else
			ret = mei_irq_thread_write_complete(dev, &slots, pos,
						cmpl_list);
		if (ret)
			return ret;
765

O
Oren Weil 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778
	}
	return 0;
}



/**
 * mei_timer - timer function.
 *
 * @work: pointer to the work_struct structure
 *
 * NOTE: This function is called by timer interrupt work
 */
779
void mei_timer(struct work_struct *work)
O
Oren Weil 已提交
780 781 782 783 784 785 786 787
{
	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,
788
					struct mei_device, timer_work.work);
O
Oren Weil 已提交
789 790 791


	mutex_lock(&dev->device_lock);
792 793
	if (dev->dev_state != MEI_DEV_ENABLED) {
		if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
O
Oren Weil 已提交
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
			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) {
817
			dev_dbg(&dev->pdev->dev, "resetting because of hang to amthi.\n");
O
Oren Weil 已提交
818 819 820
			mei_reset(dev, 1);
			dev->iamthif_msg_buf_size = 0;
			dev->iamthif_msg_buf_index = 0;
821 822
			dev->iamthif_canceled = false;
			dev->iamthif_ioctl = true;
O
Oren Weil 已提交
823 824 825
			dev->iamthif_state = MEI_IAMTHIF_IDLE;
			dev->iamthif_timer = 0;

826 827
			mei_io_cb_free(dev->iamthif_current_cb);
			dev->iamthif_current_cb = NULL;
O
Oren Weil 已提交
828 829

			dev->iamthif_file_object = NULL;
830
			mei_amthif_run_next_cmd(dev);
O
Oren Weil 已提交
831 832 833 834 835 836
		}
	}

	if (dev->iamthif_timer) {

		timeout = dev->iamthif_timer +
837
			mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
O
Oren Weil 已提交
838 839 840 841 842 843 844 845 846 847 848 849 850

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

851 852
			list_for_each_entry_safe(cb_pos, cb_next,
				&dev->amthif_rd_complete_list.list, list) {
O
Oren Weil 已提交
853

854
				cl_pos = cb_pos->file_object->private_data;
O
Oren Weil 已提交
855

856 857
				/* Finding the AMTHI entry. */
				if (cl_pos == &dev->iamthif_cl)
858
					list_del(&cb_pos->list);
O
Oren Weil 已提交
859
			}
860 861
			mei_io_cb_free(dev->iamthif_current_cb);
			dev->iamthif_current_cb = NULL;
O
Oren Weil 已提交
862 863 864 865

			dev->iamthif_file_object->private_data = NULL;
			dev->iamthif_file_object = NULL;
			dev->iamthif_timer = 0;
866
			mei_amthif_run_next_cmd(dev);
O
Oren Weil 已提交
867 868 869 870

		}
	}
out:
871 872
	schedule_delayed_work(&dev->timer_work, 2 * HZ);
	mutex_unlock(&dev->device_lock);
O
Oren Weil 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
}

/**
 *  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;
888
	struct mei_cl_cb complete_list;
O
Oren Weil 已提交
889 890 891 892 893 894 895 896 897 898
	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);
899
	mei_io_list_init(&complete_list);
O
Oren Weil 已提交
900
	dev->host_hw_state = mei_hcsr_read(dev);
901 902

	/* Ack the interrupt here
903
	 * In case of MSI we don't go through the quick handler */
904
	if (pci_dev_msi_enabled(dev->pdev))
905
		mei_clear_interrupts(dev);
906

O
Oren Weil 已提交
907 908 909 910
	dev->me_hw_state = mei_mecsr_read(dev);

	/* check if ME wants a reset */
	if ((dev->me_hw_state & ME_RDY_HRA) == 0 &&
911 912
	    dev->dev_state != MEI_DEV_RESETING &&
	    dev->dev_state != MEI_DEV_INITIALIZING) {
O
Oren Weil 已提交
913 914 915 916 917 918 919 920 921 922 923 924
		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);
925
			dev->dev_state = MEI_DEV_INIT_CLIENTS;
O
Oren Weil 已提交
926 927 928 929
			dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
			/* link is established
			 * start sending messages.
			 */
930
			mei_host_start_message(dev);
O
Oren Weil 已提交
931 932 933 934 935 936 937 938
			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;
		}
	}
939
	/* check slots available for reading */
O
Oren Weil 已提交
940
	slots = mei_count_full_read_slots(dev);
941 942 943 944 945
	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 已提交
946 947 948 949 950
		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;
	}
951
	rets = mei_irq_thread_write_handler(dev, &complete_list);
O
Oren Weil 已提交
952 953 954
end:
	dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
	dev->host_hw_state = mei_hcsr_read(dev);
955
	dev->mei_host_buffer_is_empty = mei_hbuf_is_empty(dev);
O
Oren Weil 已提交
956 957 958 959 960 961 962 963 964 965 966 967

	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;
	}
968
	if (list_empty(&complete_list.list))
O
Oren Weil 已提交
969 970 971
		return IRQ_HANDLED;


972
	list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
973
		cl = cb_pos->cl;
974
		list_del(&cb_pos->list);
O
Oren Weil 已提交
975 976 977 978 979 980
		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) {
981
				mei_amthif_complete(dev, cb_pos);
O
Oren Weil 已提交
982 983 984 985 986
			}
		}
	}
	return IRQ_HANDLED;
}