interrupt.c 12.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
 *
 * 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.
 *
 */


18
#include <linux/export.h>
O
Oren Weil 已提交
19 20 21 22
#include <linux/kthread.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/jiffies.h>
23
#include <linux/slab.h>
O
Oren Weil 已提交
24

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

#include "mei_dev.h"
28
#include "hbm.h"
T
Tomas Winkler 已提交
29
#include "client.h"
O
Oren Weil 已提交
30 31


32
/**
33
 * mei_irq_compl_handler - dispatch complete handlers
34 35
 *	for the completed callbacks
 *
36 37
 * @dev: mei device
 * @compl_list: list of completed cbs
38 39 40 41 42 43 44 45
 */
void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
{
	struct mei_cl_cb *cb, *next;
	struct mei_cl *cl;

	list_for_each_entry_safe(cb, next, &compl_list->list, list) {
		cl = cb->cl;
46
		list_del_init(&cb->list);
47

48
		dev_dbg(dev->dev, "completing call back.\n");
49 50 51
		if (cl == &dev->iamthif_cl)
			mei_amthif_complete(dev, cb);
		else
52
			mei_cl_complete(cl, cb);
53 54
	}
}
55
EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
56

O
Oren Weil 已提交
57
/**
58
 * mei_cl_hbm_equal - check if hbm is addressed to the client
O
Oren Weil 已提交
59
 *
60
 * @cl: host client
O
Oren Weil 已提交
61 62
 * @mei_hdr: header of mei client message
 *
63
 * Return: true if matches, false otherwise
O
Oren Weil 已提交
64
 */
65 66
static inline int mei_cl_hbm_equal(struct mei_cl *cl,
			struct mei_msg_hdr *mei_hdr)
O
Oren Weil 已提交
67
{
68
	return  mei_cl_host_addr(cl) == mei_hdr->host_addr &&
69
		mei_cl_me_id(cl) == mei_hdr->me_addr;
70
}
O
Oren Weil 已提交
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/**
 * mei_irq_discard_msg  - discard received message
 *
 * @dev: mei device
 * @hdr: message header
 */
static inline
void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr)
{
	/*
	 * no need to check for size as it is guarantied
	 * that length fits into rd_msg_buf
	 */
	mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
	dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
		MEI_HDR_PRM(hdr));
}

O
Oren Weil 已提交
90
/**
A
Alexander Usyskin 已提交
91
 * mei_cl_irq_read_msg - process client message
O
Oren Weil 已提交
92
 *
93
 * @cl: reading client
O
Oren Weil 已提交
94
 * @mei_hdr: header of mei client message
95
 * @complete_list: completion list
O
Oren Weil 已提交
96
 *
97
 * Return: always 0
O
Oren Weil 已提交
98
 */
99 100 101
int mei_cl_irq_read_msg(struct mei_cl *cl,
		       struct mei_msg_hdr *mei_hdr,
		       struct mei_cl_cb *complete_list)
O
Oren Weil 已提交
102
{
103 104
	struct mei_device *dev = cl->dev;
	struct mei_cl_cb *cb;
105
	unsigned char *buffer = NULL;
O
Oren Weil 已提交
106

T
Tomas Winkler 已提交
107 108 109
	cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
	if (!cb) {
		cl_err(dev, cl, "pending read cb not found\n");
110 111
		goto out;
	}
112

113
	if (!mei_cl_is_connected(cl)) {
T
Tomas Winkler 已提交
114 115
		cl_dbg(dev, cl, "not connected\n");
		cb->status = -ENODEV;
116 117
		goto out;
	}
118

119
	if (cb->buf.size == 0 || cb->buf.data == NULL) {
120 121 122 123 124
		cl_err(dev, cl, "response buffer is not allocated.\n");
		list_move_tail(&cb->list, &complete_list->list);
		cb->status = -ENOMEM;
		goto out;
	}
125

126
	if (cb->buf.size < mei_hdr->length + cb->buf_idx) {
127
		cl_dbg(dev, cl, "message overflow. size %d len %d idx %ld\n",
128 129
			cb->buf.size, mei_hdr->length, cb->buf_idx);
		buffer = krealloc(cb->buf.data, mei_hdr->length + cb->buf_idx,
130 131 132 133 134 135
				  GFP_KERNEL);

		if (!buffer) {
			cb->status = -ENOMEM;
			list_move_tail(&cb->list, &complete_list->list);
			goto out;
136
		}
137 138
		cb->buf.data = buffer;
		cb->buf.size = mei_hdr->length + cb->buf_idx;
O
Oren Weil 已提交
139 140
	}

141
	buffer = cb->buf.data + cb->buf_idx;
142 143 144
	mei_read_slots(dev, buffer, mei_hdr->length);

	cb->buf_idx += mei_hdr->length;
145

146
	if (mei_hdr->msg_complete) {
147
		cb->read_time = jiffies;
T
Tomas Winkler 已提交
148
		cl_dbg(dev, cl, "completed read length = %lu\n", cb->buf_idx);
149 150 151 152
		list_move_tail(&cb->list, &complete_list->list);
	}

out:
153 154
	if (!buffer)
		mei_irq_discard_msg(dev, mei_hdr);
O
Oren Weil 已提交
155 156 157 158

	return 0;
}

T
Tomas Winkler 已提交
159 160 161 162 163 164 165
/**
 * mei_cl_irq_disconnect_rsp - send disconnection response message
 *
 * @cl: client
 * @cb: callback block.
 * @cmpl_list: complete list.
 *
166
 * Return: 0, OK; otherwise, error.
T
Tomas Winkler 已提交
167 168
 */
static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
169
				     struct mei_cl_cb *cmpl_list)
T
Tomas Winkler 已提交
170 171
{
	struct mei_device *dev = cl->dev;
172 173
	u32 msg_slots;
	int slots;
T
Tomas Winkler 已提交
174 175
	int ret;

176 177
	slots = mei_hbuf_empty_slots(dev);
	msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_response));
T
Tomas Winkler 已提交
178

179
	if (slots < msg_slots)
T
Tomas Winkler 已提交
180 181 182
		return -EMSGSIZE;

	ret = mei_hbm_cl_disconnect_rsp(dev, cl);
183
	mei_cl_set_disconnected(cl);
T
Tomas Winkler 已提交
184
	mei_io_cb_free(cb);
185 186
	mei_me_cl_put(cl->me_cl);
	cl->me_cl = NULL;
T
Tomas Winkler 已提交
187 188 189 190

	return ret;
}

O
Oren Weil 已提交
191
/**
A
Alexander Usyskin 已提交
192
 * mei_cl_irq_read - processes client read related operation from the
193
 *	interrupt thread context - request for flow control credits
O
Oren Weil 已提交
194
 *
195 196
 * @cl: client
 * @cb: callback block.
O
Oren Weil 已提交
197 198
 * @cmpl_list: complete list.
 *
199
 * Return: 0, OK; otherwise, error.
O
Oren Weil 已提交
200
 */
201
static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
202
			   struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
203
{
204
	struct mei_device *dev = cl->dev;
205 206
	u32 msg_slots;
	int slots;
207 208
	int ret;

209 210
	msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
	slots = mei_hbuf_empty_slots(dev);
211

212
	if (slots < msg_slots)
T
Tomas Winkler 已提交
213
		return -EMSGSIZE;
214

215 216 217
	ret = mei_hbm_cl_flow_control_req(dev, cl);
	if (ret) {
		cl->status = ret;
218 219
		cb->buf_idx = 0;
		list_move_tail(&cb->list, &cmpl_list->list);
220
		return ret;
221
	}
222

T
Tomas Winkler 已提交
223
	list_move_tail(&cb->list, &cl->rd_pending);
224

O
Oren Weil 已提交
225 226 227 228
	return 0;
}

/**
229
 * mei_irq_read_handler - bottom half read routine after ISR to
O
Oren Weil 已提交
230 231 232
 * handle the read processing.
 *
 * @dev: the device structure
233
 * @cmpl_list: An instance of our list structure
O
Oren Weil 已提交
234 235
 * @slots: slots to read.
 *
236
 * Return: 0 on success, <0 on failure.
O
Oren Weil 已提交
237
 */
238 239
int mei_irq_read_handler(struct mei_device *dev,
		struct mei_cl_cb *cmpl_list, s32 *slots)
O
Oren Weil 已提交
240 241
{
	struct mei_msg_hdr *mei_hdr;
T
Tomas Winkler 已提交
242 243
	struct mei_cl *cl;
	int ret;
O
Oren Weil 已提交
244 245

	if (!dev->rd_msg_hdr) {
246
		dev->rd_msg_hdr = mei_read_hdr(dev);
O
Oren Weil 已提交
247
		(*slots)--;
248
		dev_dbg(dev->dev, "slots =%08x.\n", *slots);
O
Oren Weil 已提交
249 250
	}
	mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
251
	dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
O
Oren Weil 已提交
252 253

	if (mei_hdr->reserved || !dev->rd_msg_hdr) {
254
		dev_err(dev->dev, "corrupted message header 0x%08X\n",
T
Tomas Winkler 已提交
255
				dev->rd_msg_hdr);
O
Oren Weil 已提交
256 257 258 259
		ret = -EBADMSG;
		goto end;
	}

T
Tomas Winkler 已提交
260
	if (mei_slots2data(*slots) < mei_hdr->length) {
261
		dev_err(dev->dev, "less data available than length=%08x.\n",
O
Oren Weil 已提交
262 263
				*slots);
		/* we can't read the message */
264
		ret = -ENODATA;
O
Oren Weil 已提交
265 266 267
		goto end;
	}

T
Tomas Winkler 已提交
268 269
	/*  HBM message */
	if (mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0) {
270 271
		ret = mei_hbm_dispatch(dev, mei_hdr);
		if (ret) {
272
			dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
273 274 275
					ret);
			goto end;
		}
T
Tomas Winkler 已提交
276 277
		goto reset_slots;
	}
278

279
	/* find recipient cl */
T
Tomas Winkler 已提交
280 281 282 283 284 285 286
	list_for_each_entry(cl, &dev->file_list, link) {
		if (mei_cl_hbm_equal(cl, mei_hdr)) {
			cl_dbg(dev, cl, "got a message\n");
			break;
		}
	}

287
	/* if no recipient cl was found we assume corrupted header */
T
Tomas Winkler 已提交
288
	if (&cl->link == &dev->file_list) {
289
		dev_err(dev->dev, "no destination client found 0x%08X\n",
T
Tomas Winkler 已提交
290 291 292 293 294
				dev->rd_msg_hdr);
		ret = -EBADMSG;
		goto end;
	}

295 296
	if (cl == &dev->iamthif_cl) {
		ret = mei_amthif_irq_read_msg(cl, mei_hdr, cmpl_list);
O
Oren Weil 已提交
297
	} else {
298
		ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
O
Oren Weil 已提交
299 300
	}

301

T
Tomas Winkler 已提交
302
reset_slots:
O
Oren Weil 已提交
303 304 305 306 307 308
	/* reset the number of slots and header */
	*slots = mei_count_full_read_slots(dev);
	dev->rd_msg_hdr = 0;

	if (*slots == -EOVERFLOW) {
		/* overflow - reset */
309
		dev_err(dev->dev, "resetting due to slots overflow.\n");
O
Oren Weil 已提交
310 311 312 313 314 315 316
		/* set the event since message has been read */
		ret = -ERANGE;
		goto end;
	}
end:
	return ret;
}
317
EXPORT_SYMBOL_GPL(mei_irq_read_handler);
O
Oren Weil 已提交
318 319 320


/**
321 322
 * mei_irq_write_handler -  dispatch write requests
 *  after irq received
O
Oren Weil 已提交
323 324
 *
 * @dev: the device structure
325
 * @cmpl_list: An instance of our list structure
O
Oren Weil 已提交
326
 *
327
 * Return: 0 on success, <0 on failure.
O
Oren Weil 已提交
328
 */
T
Tomas Winkler 已提交
329
int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
O
Oren Weil 已提交
330 331 332
{

	struct mei_cl *cl;
333
	struct mei_cl_cb *cb, *next;
334
	struct mei_cl_cb *list;
335
	s32 slots;
O
Oren Weil 已提交
336 337
	int ret;

338 339

	if (!mei_hbuf_acquire(dev))
O
Oren Weil 已提交
340
		return 0;
341

342 343
	slots = mei_hbuf_empty_slots(dev);
	if (slots <= 0)
344 345
		return -EMSGSIZE;

O
Oren Weil 已提交
346
	/* complete all waiting for write CB */
347
	dev_dbg(dev->dev, "complete all waiting for write cb.\n");
O
Oren Weil 已提交
348 349

	list = &dev->write_waiting_list;
350 351
	list_for_each_entry_safe(cb, next, &list->list, list) {
		cl = cb->cl;
352 353

		cl->status = 0;
354 355 356
		cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
		cl->writing_state = MEI_WRITE_COMPLETE;
		list_move_tail(&cb->list, &cmpl_list->list);
O
Oren Weil 已提交
357 358
	}

359 360
	if (dev->wd_state == MEI_WD_STOPPING) {
		dev->wd_state = MEI_WD_IDLE;
361
		wake_up(&dev->wait_stop_wd);
O
Oren Weil 已提交
362 363
	}

364
	if (mei_cl_is_connected(&dev->wd_cl)) {
O
Oren Weil 已提交
365
		if (dev->wd_pending &&
T
Tomas Winkler 已提交
366
		    mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
367 368 369
			ret = mei_wd_send(dev);
			if (ret)
				return ret;
370
			dev->wd_pending = false;
O
Oren Weil 已提交
371 372 373 374
		}
	}

	/* complete control write list CB */
375
	dev_dbg(dev->dev, "complete control write list cb.\n");
376 377 378
	list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list.list, list) {
		cl = cb->cl;
		switch (cb->fop_type) {
379
		case MEI_FOP_DISCONNECT:
380
			/* send disconnect message */
381
			ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
382 383
			if (ret)
				return ret;
O
Oren Weil 已提交
384

385
			break;
386
		case MEI_FOP_READ:
387
			/* send flow control message */
388
			ret = mei_cl_irq_read(cl, cb, cmpl_list);
389 390
			if (ret)
				return ret;
O
Oren Weil 已提交
391

392
			break;
393
		case MEI_FOP_CONNECT:
394
			/* connect message */
395
			ret = mei_cl_irq_connect(cl, cb, cmpl_list);
396 397
			if (ret)
				return ret;
O
Oren Weil 已提交
398

399
			break;
T
Tomas Winkler 已提交
400 401
		case MEI_FOP_DISCONNECT_RSP:
			/* send disconnect resp */
402
			ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
T
Tomas Winkler 已提交
403 404
			if (ret)
				return ret;
405
			break;
406 407
		default:
			BUG();
O
Oren Weil 已提交
408
		}
409

O
Oren Weil 已提交
410 411
	}
	/* complete  write list CB */
412
	dev_dbg(dev->dev, "complete write list cb.\n");
413 414
	list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
		cl = cb->cl;
415
		if (cl == &dev->iamthif_cl)
416
			ret = mei_amthif_irq_write(cl, cb, cmpl_list);
417
		else
418
			ret = mei_cl_irq_write(cl, cb, cmpl_list);
419 420
		if (ret)
			return ret;
O
Oren Weil 已提交
421 422 423
	}
	return 0;
}
424
EXPORT_SYMBOL_GPL(mei_irq_write_handler);
O
Oren Weil 已提交
425 426 427 428 429 430 431 432 433



/**
 * mei_timer - timer function.
 *
 * @work: pointer to the work_struct structure
 *
 */
434
void mei_timer(struct work_struct *work)
O
Oren Weil 已提交
435 436
{
	unsigned long timeout;
437
	struct mei_cl *cl;
O
Oren Weil 已提交
438 439

	struct mei_device *dev = container_of(work,
440
					struct mei_device, timer_work.work);
O
Oren Weil 已提交
441 442 443


	mutex_lock(&dev->device_lock);
444 445 446 447 448 449 450

	/* Catch interrupt stalls during HBM init handshake */
	if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
	    dev->hbm_state != MEI_HBM_IDLE) {

		if (dev->init_clients_timer) {
			if (--dev->init_clients_timer == 0) {
451
				dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
452
					dev->hbm_state);
453
				mei_reset(dev);
454
				goto out;
O
Oren Weil 已提交
455 456 457
			}
		}
	}
458 459 460 461

	if (dev->dev_state != MEI_DEV_ENABLED)
		goto out;

O
Oren Weil 已提交
462
	/*** connect/disconnect timeouts ***/
463 464 465
	list_for_each_entry(cl, &dev->file_list, link) {
		if (cl->timer_count) {
			if (--cl->timer_count == 0) {
466
				dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
467
				mei_reset(dev);
O
Oren Weil 已提交
468 469 470 471 472
				goto out;
			}
		}
	}

473 474 475
	if (!mei_cl_is_connected(&dev->iamthif_cl))
		goto out;

O
Oren Weil 已提交
476 477
	if (dev->iamthif_stall_timer) {
		if (--dev->iamthif_stall_timer == 0) {
478
			dev_err(dev->dev, "timer: amthif  hanged.\n");
479
			mei_reset(dev);
480
			dev->iamthif_canceled = false;
O
Oren Weil 已提交
481 482 483
			dev->iamthif_state = MEI_IAMTHIF_IDLE;
			dev->iamthif_timer = 0;

484 485
			mei_io_cb_free(dev->iamthif_current_cb);
			dev->iamthif_current_cb = NULL;
O
Oren Weil 已提交
486 487

			dev->iamthif_file_object = NULL;
488
			mei_amthif_run_next_cmd(dev);
O
Oren Weil 已提交
489 490 491 492 493 494
		}
	}

	if (dev->iamthif_timer) {

		timeout = dev->iamthif_timer +
495
			mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
O
Oren Weil 已提交
496

497
		dev_dbg(dev->dev, "dev->iamthif_timer = %ld\n",
O
Oren Weil 已提交
498
				dev->iamthif_timer);
499 500
		dev_dbg(dev->dev, "timeout = %ld\n", timeout);
		dev_dbg(dev->dev, "jiffies = %ld\n", jiffies);
O
Oren Weil 已提交
501 502 503 504 505 506
		if (time_after(jiffies, timeout)) {
			/*
			 * User didn't read the AMTHI data on time (15sec)
			 * freeing AMTHI for other requests
			 */

507
			dev_dbg(dev->dev, "freeing AMTHI for other requests\n");
O
Oren Weil 已提交
508

509 510
			mei_io_list_flush(&dev->amthif_rd_complete_list,
				&dev->iamthif_cl);
511 512
			mei_io_cb_free(dev->iamthif_current_cb);
			dev->iamthif_current_cb = NULL;
O
Oren Weil 已提交
513 514 515 516

			dev->iamthif_file_object->private_data = NULL;
			dev->iamthif_file_object = NULL;
			dev->iamthif_timer = 0;
517
			mei_amthif_run_next_cmd(dev);
O
Oren Weil 已提交
518 519 520 521

		}
	}
out:
522 523
	if (dev->dev_state != MEI_DEV_DISABLED)
		schedule_delayed_work(&dev->timer_work, 2 * HZ);
524
	mutex_unlock(&dev->device_lock);
O
Oren Weil 已提交
525
}