interrupt.c 11.9 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 69 70
	return cl->host_client_id == mei_hdr->host_addr &&
		cl->me_client_id == mei_hdr->me_addr;
}
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 185 186 187 188
	mei_io_cb_free(cb);

	return ret;
}

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

207 208
	msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
	slots = mei_hbuf_empty_slots(dev);
209

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

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

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

O
Oren Weil 已提交
223 224 225 226
	return 0;
}

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

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

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

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

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

277
	/* find recipient cl */
T
Tomas Winkler 已提交
278 279 280 281 282 283 284
	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;
		}
	}

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

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

299

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

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


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

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

336 337

	if (!mei_hbuf_acquire(dev))
O
Oren Weil 已提交
338
		return 0;
339

340 341
	slots = mei_hbuf_empty_slots(dev);
	if (slots <= 0)
342 343
		return -EMSGSIZE;

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

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

		cl->status = 0;
352 353 354
		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 已提交
355 356
	}

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

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

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

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

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

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

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



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

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


	mutex_lock(&dev->device_lock);
442 443 444 445 446 447 448

	/* 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) {
449
				dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
450
					dev->hbm_state);
451
				mei_reset(dev);
452
				goto out;
O
Oren Weil 已提交
453 454 455
			}
		}
	}
456 457 458 459

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

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

471 472 473
	if (!mei_cl_is_connected(&dev->iamthif_cl))
		goto out;

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

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

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

	if (dev->iamthif_timer) {

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

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

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

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

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

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