hbm.c 33.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5
/*
 * Intel Management Engine Interface (Intel MEI) Linux driver
 * Copyright (c) 2003-2012, Intel Corporation.
 */
6
#include <linux/export.h>
7 8
#include <linux/sched.h>
#include <linux/wait.h>
9
#include <linux/pm_runtime.h>
10 11 12
#include <linux/slab.h>

#include <linux/mei.h>
13 14

#include "mei_dev.h"
15
#include "hbm.h"
16
#include "client.h"
17

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
static const char *mei_hbm_status_str(enum mei_hbm_status status)
{
#define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status
	switch (status) {
	MEI_HBM_STATUS(SUCCESS);
	MEI_HBM_STATUS(CLIENT_NOT_FOUND);
	MEI_HBM_STATUS(ALREADY_EXISTS);
	MEI_HBM_STATUS(REJECTED);
	MEI_HBM_STATUS(INVALID_PARAMETER);
	MEI_HBM_STATUS(NOT_ALLOWED);
	MEI_HBM_STATUS(ALREADY_STARTED);
	MEI_HBM_STATUS(NOT_STARTED);
	default: return "unknown";
	}
#undef MEI_HBM_STATUS
};

35 36 37 38 39 40 41 42 43
static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status)
{
#define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
	switch (status) {
	MEI_CL_CS(SUCCESS);
	MEI_CL_CS(NOT_FOUND);
	MEI_CL_CS(ALREADY_STARTED);
	MEI_CL_CS(OUT_OF_RESOURCES);
	MEI_CL_CS(MESSAGE_SMALL);
44
	MEI_CL_CS(NOT_ALLOWED);
45 46 47 48 49
	default: return "unknown";
	}
#undef MEI_CL_CCS
}

50 51 52 53 54 55 56
const char *mei_hbm_state_str(enum mei_hbm_state state)
{
#define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
	switch (state) {
	MEI_HBM_STATE(IDLE);
	MEI_HBM_STATE(STARTING);
	MEI_HBM_STATE(STARTED);
T
Tomas Winkler 已提交
57
	MEI_HBM_STATE(DR_SETUP);
58 59 60 61 62 63 64 65 66
	MEI_HBM_STATE(ENUM_CLIENTS);
	MEI_HBM_STATE(CLIENT_PROPERTIES);
	MEI_HBM_STATE(STOPPED);
	default:
		return "unknown";
	}
#undef MEI_HBM_STATE
}

67 68 69 70 71 72
/**
 * mei_cl_conn_status_to_errno - convert client connect response
 * status to error code
 *
 * @status: client connect response status
 *
73
 * Return: corresponding error code
74 75 76 77 78 79 80 81 82
 */
static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
{
	switch (status) {
	case MEI_CL_CONN_SUCCESS:          return 0;
	case MEI_CL_CONN_NOT_FOUND:        return -ENOTTY;
	case MEI_CL_CONN_ALREADY_STARTED:  return -EBUSY;
	case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
	case MEI_CL_CONN_MESSAGE_SMALL:    return -EINVAL;
83
	case MEI_CL_CONN_NOT_ALLOWED:      return -EBUSY;
84 85 86 87
	default:                           return -EINVAL;
	}
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101
/**
 * mei_hbm_write_message - wrapper for sending hbm messages.
 *
 * @dev: mei device
 * @hdr: mei header
 * @data: payload
 */
static inline int mei_hbm_write_message(struct mei_device *dev,
					struct mei_msg_hdr *hdr,
					const void *data)
{
	return mei_write_message(dev, hdr, sizeof(*hdr), data, hdr->length);
}

102 103 104 105 106 107 108 109 110 111 112
/**
 * mei_hbm_idle - set hbm to idle state
 *
 * @dev: the device structure
 */
void mei_hbm_idle(struct mei_device *dev)
{
	dev->init_clients_timer = 0;
	dev->hbm_state = MEI_HBM_IDLE;
}

113 114 115 116 117 118 119
/**
 * mei_hbm_reset - reset hbm counters and book keeping data structurs
 *
 * @dev: the device structure
 */
void mei_hbm_reset(struct mei_device *dev)
{
120
	mei_me_cl_rm_all(dev);
121 122 123 124

	mei_hbm_idle(dev);
}

125 126 127 128 129 130 131 132 133 134 135 136 137
/**
 * mei_hbm_hdr - construct hbm header
 *
 * @hdr: hbm header
 * @length: payload length
 */

static inline void mei_hbm_hdr(struct mei_msg_hdr *hdr, size_t length)
{
	hdr->host_addr = 0;
	hdr->me_addr = 0;
	hdr->length = length;
	hdr->msg_complete = 1;
138
	hdr->dma_ring = 0;
139
	hdr->reserved = 0;
140
	hdr->internal = 0;
141 142
}

143 144
/**
 * mei_hbm_cl_hdr - construct client hbm header
145
 *
146
 * @cl: client
147 148 149 150 151 152 153 154 155 156 157 158
 * @hbm_cmd: host bus message command
 * @buf: buffer for cl header
 * @len: buffer length
 */
static inline
void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
{
	struct mei_hbm_cl_cmd *cmd = buf;

	memset(cmd, 0, len);

	cmd->hbm_cmd = hbm_cmd;
159
	cmd->host_addr = mei_cl_host_addr(cl);
160
	cmd->me_addr = mei_cl_me_id(cl);
161 162
}

163 164 165 166 167 168
/**
 * mei_hbm_cl_write - write simple hbm client message
 *
 * @dev: the device structure
 * @cl: client
 * @hbm_cmd: host bus message command
169
 * @buf: message buffer
170
 * @len: buffer length
A
Alexander Usyskin 已提交
171 172
 *
 * Return: 0 on success, <0 on failure.
173
 */
174 175
static inline int mei_hbm_cl_write(struct mei_device *dev, struct mei_cl *cl,
				   u8 hbm_cmd, void *buf, size_t len)
176
{
177
	struct mei_msg_hdr mei_hdr;
178

179 180
	mei_hbm_hdr(&mei_hdr, len);
	mei_hbm_cl_hdr(cl, hbm_cmd, buf, len);
181

182
	return mei_hbm_write_message(dev, &mei_hdr, buf);
183 184
}

185
/**
186 187
 * mei_hbm_cl_addr_equal - check if the client's and
 *	the message address match
188
 *
189 190
 * @cl: client
 * @cmd: hbm client message
191
 *
192
 * Return: true if addresses are the same
193 194
 */
static inline
195
bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
196
{
197
	return  mei_cl_host_addr(cl) == cmd->host_addr &&
198
		mei_cl_me_id(cl) == cmd->me_addr;
199 200
}

201 202 203 204 205 206
/**
 * mei_hbm_cl_find_by_cmd - find recipient client
 *
 * @dev: the device structure
 * @buf: a buffer with hbm cl command
 *
207
 * Return: the recipient client or NULL if not found
208 209 210 211 212 213 214 215 216 217 218 219 220 221
 */
static inline
struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
{
	struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
	struct mei_cl *cl;

	list_for_each_entry(cl, &dev->file_list, link)
		if (mei_hbm_cl_addr_equal(cl, cmd))
			return cl;
	return NULL;
}


222 223 224 225 226
/**
 * mei_hbm_start_wait - wait for start response message.
 *
 * @dev: the device structure
 *
227
 * Return: 0 on success and < 0 on failure
228
 */
T
Tomas Winkler 已提交
229 230 231
int mei_hbm_start_wait(struct mei_device *dev)
{
	int ret;
232 233

	if (dev->hbm_state > MEI_HBM_STARTING)
T
Tomas Winkler 已提交
234 235 236
		return 0;

	mutex_unlock(&dev->device_lock);
237 238
	ret = wait_event_timeout(dev->wait_hbm_start,
			dev->hbm_state != MEI_HBM_STARTING,
239
			mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
T
Tomas Winkler 已提交
240 241
	mutex_lock(&dev->device_lock);

242
	if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
T
Tomas Winkler 已提交
243
		dev->hbm_state = MEI_HBM_IDLE;
244
		dev_err(dev->dev, "waiting for mei start failed\n");
245
		return -ETIME;
T
Tomas Winkler 已提交
246 247 248 249
	}
	return 0;
}

250
/**
251
 * mei_hbm_start_req - sends start request message.
252 253
 *
 * @dev: the device structure
254
 *
255
 * Return: 0 on success and < 0 on failure
256
 */
T
Tomas Winkler 已提交
257
int mei_hbm_start_req(struct mei_device *dev)
258
{
259 260
	struct mei_msg_hdr mei_hdr;
	struct hbm_host_version_request start_req;
261
	const size_t len = sizeof(struct hbm_host_version_request);
262
	int ret;
263

264 265
	mei_hbm_reset(dev);

266
	mei_hbm_hdr(&mei_hdr, len);
267 268

	/* host start message */
269 270 271 272
	memset(&start_req, 0, len);
	start_req.hbm_cmd = HOST_START_REQ_CMD;
	start_req.host_version.major_version = HBM_MAJOR_VERSION;
	start_req.host_version.minor_version = HBM_MINOR_VERSION;
273

T
Tomas Winkler 已提交
274
	dev->hbm_state = MEI_HBM_IDLE;
275
	ret = mei_hbm_write_message(dev, &mei_hdr, &start_req);
276
	if (ret) {
277
		dev_err(dev->dev, "version message write failed: ret = %d\n",
278 279
			ret);
		return ret;
280
	}
281

282
	dev->hbm_state = MEI_HBM_STARTING;
283
	dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
284
	mei_schedule_stall_timer(dev);
T
Tomas Winkler 已提交
285
	return 0;
286 287
}

T
Tomas Winkler 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
/**
 * mei_hbm_dma_setup_req() - setup DMA request
 * @dev: the device structure
 *
 * Return: 0 on success and < 0 on failure
 */
static int mei_hbm_dma_setup_req(struct mei_device *dev)
{
	struct mei_msg_hdr mei_hdr;
	struct hbm_dma_setup_request req;
	const size_t len = sizeof(struct hbm_dma_setup_request);
	unsigned int i;
	int ret;

	mei_hbm_hdr(&mei_hdr, len);

	memset(&req, 0, len);
	req.hbm_cmd = MEI_HBM_DMA_SETUP_REQ_CMD;
	for (i = 0; i < DMA_DSCR_NUM; i++) {
		phys_addr_t paddr;

		paddr = dev->dr_dscr[i].daddr;
		req.dma_dscr[i].addr_hi = upper_32_bits(paddr);
		req.dma_dscr[i].addr_lo = lower_32_bits(paddr);
		req.dma_dscr[i].size = dev->dr_dscr[i].size;
	}

315 316
	mei_dma_ring_reset(dev);

T
Tomas Winkler 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329
	ret = mei_hbm_write_message(dev, &mei_hdr, &req);
	if (ret) {
		dev_err(dev->dev, "dma setup request write failed: ret = %d.\n",
			ret);
		return ret;
	}

	dev->hbm_state = MEI_HBM_DR_SETUP;
	dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
	mei_schedule_stall_timer(dev);
	return 0;
}

A
Alexander Usyskin 已提交
330
/**
331
 * mei_hbm_enum_clients_req - sends enumeration client request message.
332 333 334
 *
 * @dev: the device structure
 *
335
 * Return: 0 on success and < 0 on failure
336
 */
337
static int mei_hbm_enum_clients_req(struct mei_device *dev)
338
{
339 340
	struct mei_msg_hdr mei_hdr;
	struct hbm_host_enum_request enum_req;
341
	const size_t len = sizeof(struct hbm_host_enum_request);
342 343
	int ret;

344
	/* enumerate clients */
345
	mei_hbm_hdr(&mei_hdr, len);
346

347 348 349 350 351 352
	memset(&enum_req, 0, len);
	enum_req.hbm_cmd = HOST_ENUM_REQ_CMD;
	enum_req.flags |= dev->hbm_f_dc_supported ?
			  MEI_HBM_ENUM_F_ALLOW_ADD : 0;
	enum_req.flags |= dev->hbm_f_ie_supported ?
			  MEI_HBM_ENUM_F_IMMEDIATE_ENUM : 0;
353

354
	ret = mei_hbm_write_message(dev, &mei_hdr, &enum_req);
355
	if (ret) {
356
		dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
357 358
			ret);
		return ret;
359
	}
T
Tomas Winkler 已提交
360
	dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
361
	dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
362
	mei_schedule_stall_timer(dev);
363
	return 0;
364 365
}

A
Alexander Usyskin 已提交
366
/**
367 368 369 370 371
 * mei_hbm_me_cl_add - add new me client to the list
 *
 * @dev: the device structure
 * @res: hbm property response
 *
372
 * Return: 0 on success and -ENOMEM on allocation failure
373 374 375 376 377 378
 */

static int mei_hbm_me_cl_add(struct mei_device *dev,
			     struct hbm_props_response *res)
{
	struct mei_me_client *me_cl;
379 380 381
	const uuid_le *uuid = &res->client_properties.protocol_name;

	mei_me_cl_rm_by_uuid(dev, uuid);
382 383 384 385 386

	me_cl = kzalloc(sizeof(struct mei_me_client), GFP_KERNEL);
	if (!me_cl)
		return -ENOMEM;

387 388
	mei_me_cl_init(me_cl);

389 390
	me_cl->props = res->client_properties;
	me_cl->client_id = res->me_addr;
391
	me_cl->tx_flow_ctrl_creds = 0;
392

393 394
	mei_me_cl_add(dev, me_cl);

395 396 397
	return 0;
}

T
Tomas Winkler 已提交
398 399 400 401 402 403 404 405 406 407 408
/**
 * mei_hbm_add_cl_resp - send response to fw on client add request
 *
 * @dev: the device structure
 * @addr: me address
 * @status: response status
 *
 * Return: 0 on success and < 0 on failure
 */
static int mei_hbm_add_cl_resp(struct mei_device *dev, u8 addr, u8 status)
{
409 410
	struct mei_msg_hdr mei_hdr;
	struct hbm_add_client_response resp;
T
Tomas Winkler 已提交
411 412 413 414 415
	const size_t len = sizeof(struct hbm_add_client_response);
	int ret;

	dev_dbg(dev->dev, "adding client response\n");

416
	mei_hbm_hdr(&mei_hdr, len);
T
Tomas Winkler 已提交
417

418 419 420 421
	memset(&resp, 0, sizeof(struct hbm_add_client_response));
	resp.hbm_cmd = MEI_HBM_ADD_CLIENT_RES_CMD;
	resp.me_addr = addr;
	resp.status  = status;
T
Tomas Winkler 已提交
422

423
	ret = mei_hbm_write_message(dev, &mei_hdr, &resp);
T
Tomas Winkler 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	if (ret)
		dev_err(dev->dev, "add client response write failed: ret = %d\n",
			ret);
	return ret;
}

/**
 * mei_hbm_fw_add_cl_req - request from the fw to add a client
 *
 * @dev: the device structure
 * @req: add client request
 *
 * Return: 0 on success and < 0 on failure
 */
static int mei_hbm_fw_add_cl_req(struct mei_device *dev,
			      struct hbm_add_client_request *req)
{
	int ret;
	u8 status = MEI_HBMS_SUCCESS;

	BUILD_BUG_ON(sizeof(struct hbm_add_client_request) !=
			sizeof(struct hbm_props_response));

	ret = mei_hbm_me_cl_add(dev, (struct hbm_props_response *)req);
	if (ret)
		status = !MEI_HBMS_SUCCESS;

451 452 453
	if (dev->dev_state == MEI_DEV_ENABLED)
		schedule_work(&dev->bus_rescan_work);

T
Tomas Winkler 已提交
454 455 456
	return mei_hbm_add_cl_resp(dev, req->me_addr, status);
}

457 458 459 460 461 462 463 464 465 466 467 468 469
/**
 * mei_hbm_cl_notify_req - send notification request
 *
 * @dev: the device structure
 * @cl: a client to disconnect from
 * @start: true for start false for stop
 *
 * Return: 0 on success and -EIO on write failure
 */
int mei_hbm_cl_notify_req(struct mei_device *dev,
			  struct mei_cl *cl, u8 start)
{

470 471
	struct mei_msg_hdr mei_hdr;
	struct hbm_notification_request req;
472 473 474
	const size_t len = sizeof(struct hbm_notification_request);
	int ret;

475 476
	mei_hbm_hdr(&mei_hdr, len);
	mei_hbm_cl_hdr(cl, MEI_HBM_NOTIFY_REQ_CMD, &req, len);
477

478
	req.start = start;
479

480
	ret = mei_hbm_write_message(dev, &mei_hdr, &req);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
	if (ret)
		dev_err(dev->dev, "notify request failed: ret = %d\n", ret);

	return ret;
}

/**
 *  notify_res_to_fop - convert notification response to the proper
 *      notification FOP
 *
 * @cmd: client notification start response command
 *
 * Return:  MEI_FOP_NOTIFY_START or MEI_FOP_NOTIFY_STOP;
 */
static inline enum mei_cb_file_ops notify_res_to_fop(struct mei_hbm_cl_cmd *cmd)
{
	struct hbm_notification_response *rs =
		(struct hbm_notification_response *)cmd;

500
	return mei_cl_notify_req2fop(rs->start);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
}

/**
 * mei_hbm_cl_notify_start_res - update the client state according
 *       notify start response
 *
 * @dev: the device structure
 * @cl: mei host client
 * @cmd: client notification start response command
 */
static void mei_hbm_cl_notify_start_res(struct mei_device *dev,
					struct mei_cl *cl,
					struct mei_hbm_cl_cmd *cmd)
{
	struct hbm_notification_response *rs =
		(struct hbm_notification_response *)cmd;

	cl_dbg(dev, cl, "hbm: notify start response status=%d\n", rs->status);

	if (rs->status == MEI_HBMS_SUCCESS ||
	    rs->status == MEI_HBMS_ALREADY_STARTED) {
		cl->notify_en = true;
		cl->status = 0;
	} else {
		cl->status = -EINVAL;
	}
}

/**
 * mei_hbm_cl_notify_stop_res - update the client state according
 *       notify stop response
 *
 * @dev: the device structure
 * @cl: mei host client
 * @cmd: client notification stop response command
 */
static void mei_hbm_cl_notify_stop_res(struct mei_device *dev,
				       struct mei_cl *cl,
				       struct mei_hbm_cl_cmd *cmd)
{
	struct hbm_notification_response *rs =
		(struct hbm_notification_response *)cmd;

	cl_dbg(dev, cl, "hbm: notify stop response status=%d\n", rs->status);

	if (rs->status == MEI_HBMS_SUCCESS ||
	    rs->status == MEI_HBMS_NOT_STARTED) {
		cl->notify_en = false;
		cl->status = 0;
	} else {
		/* TODO: spec is not clear yet about other possible issues */
		cl->status = -EINVAL;
	}
}

/**
 * mei_hbm_cl_notify - signal notification event
 *
 * @dev: the device structure
 * @cmd: notification client message
 */
static void mei_hbm_cl_notify(struct mei_device *dev,
			      struct mei_hbm_cl_cmd *cmd)
{
	struct mei_cl *cl;

	cl = mei_hbm_cl_find_by_cmd(dev, cmd);
568 569
	if (cl)
		mei_cl_notify(cl);
570 571
}

572
/**
573
 * mei_hbm_prop_req - request property for a single client
574 575
 *
 * @dev: the device structure
576
 * @start_idx: client index to start search
577
 *
578
 * Return: 0 on success and < 0 on failure
579
 */
580
static int mei_hbm_prop_req(struct mei_device *dev, unsigned long start_idx)
581
{
582 583
	struct mei_msg_hdr mei_hdr;
	struct hbm_props_request prop_req;
584
	const size_t len = sizeof(struct hbm_props_request);
585
	unsigned long addr;
586
	int ret;
587

588
	addr = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX, start_idx);
589 590

	/* We got all client properties */
591
	if (addr == MEI_CLIENTS_MAX) {
T
Tomas Winkler 已提交
592
		dev->hbm_state = MEI_HBM_STARTED;
593
		mei_host_client_init(dev);
594 595 596 597

		return 0;
	}

598
	mei_hbm_hdr(&mei_hdr, len);
599

600
	memset(&prop_req, 0, sizeof(struct hbm_props_request));
601

602 603
	prop_req.hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
	prop_req.me_addr = addr;
604

605
	ret = mei_hbm_write_message(dev, &mei_hdr, &prop_req);
606
	if (ret) {
607
		dev_err(dev->dev, "properties request write failed: ret = %d\n",
608 609
			ret);
		return ret;
610 611 612
	}

	dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
613
	mei_schedule_stall_timer(dev);
614 615 616 617

	return 0;
}

A
Alexander Usyskin 已提交
618
/**
619 620 621 622 623
 * mei_hbm_pg - sends pg command
 *
 * @dev: the device structure
 * @pg_cmd: the pg command code
 *
624
 * Return: -EIO on write failure
625
 *         -EOPNOTSUPP if the operation is not supported by the protocol
626 627 628
 */
int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
{
629 630
	struct mei_msg_hdr mei_hdr;
	struct hbm_power_gate req;
631 632 633
	const size_t len = sizeof(struct hbm_power_gate);
	int ret;

634 635 636
	if (!dev->hbm_f_pg_supported)
		return -EOPNOTSUPP;

637
	mei_hbm_hdr(&mei_hdr, len);
638

639 640
	memset(&req, 0, len);
	req.hbm_cmd = pg_cmd;
641

642
	ret = mei_hbm_write_message(dev, &mei_hdr, &req);
643
	if (ret)
644
		dev_err(dev->dev, "power gate command write failed.\n");
645 646 647 648
	return ret;
}
EXPORT_SYMBOL_GPL(mei_hbm_pg);

649
/**
T
Tomas Winkler 已提交
650
 * mei_hbm_stop_req - send stop request message
651
 *
652
 * @dev: mei device
T
Tomas Winkler 已提交
653
 *
654
 * Return: -EIO on write failure
655
 */
T
Tomas Winkler 已提交
656
static int mei_hbm_stop_req(struct mei_device *dev)
657
{
658 659
	struct mei_msg_hdr mei_hdr;
	struct hbm_host_stop_request req;
660 661
	const size_t len = sizeof(struct hbm_host_stop_request);

662
	mei_hbm_hdr(&mei_hdr, len);
663

664 665 666
	memset(&req, 0, len);
	req.hbm_cmd = HOST_STOP_REQ_CMD;
	req.reason = DRIVER_STOP_REQUEST;
T
Tomas Winkler 已提交
667

668
	return mei_hbm_write_message(dev, &mei_hdr, &req);
669 670
}

671
/**
672
 * mei_hbm_cl_flow_control_req - sends flow control request.
673 674
 *
 * @dev: the device structure
675
 * @cl: client info
676
 *
677
 * Return: -EIO on write failure
678
 */
679
int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
680
{
681
	struct hbm_flow_control req;
682

683
	cl_dbg(dev, cl, "sending flow control\n");
684 685
	return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD,
				&req, sizeof(req));
686 687
}

688
/**
689
 * mei_hbm_add_single_tx_flow_ctrl_creds - adds single buffer credentials.
690
 *
691
 * @dev: the device structure
692
 * @fctrl: flow control response bus message
693
 *
694
 * Return: 0 on success, < 0 otherwise
695
 */
696 697
static int mei_hbm_add_single_tx_flow_ctrl_creds(struct mei_device *dev,
						 struct hbm_flow_control *fctrl)
698
{
699
	struct mei_me_client *me_cl;
700
	int rets;
701

702
	me_cl = mei_me_cl_by_id(dev, fctrl->me_addr);
703
	if (!me_cl) {
704
		dev_err(dev->dev, "no such me client %d\n", fctrl->me_addr);
705
		return -ENOENT;
706 707
	}

708 709 710 711
	if (WARN_ON(me_cl->props.single_recv_buf == 0)) {
		rets = -EINVAL;
		goto out;
	}
712

713
	me_cl->tx_flow_ctrl_creds++;
714
	dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
715
		fctrl->me_addr, me_cl->tx_flow_ctrl_creds);
716

717 718 719 720
	rets = 0;
out:
	mei_me_cl_put(me_cl);
	return rets;
721 722 723 724 725 726
}

/**
 * mei_hbm_cl_flow_control_res - flow control response from me
 *
 * @dev: the device structure
727
 * @fctrl: flow control response bus message
728
 */
729 730
static void mei_hbm_cl_tx_flow_ctrl_creds_res(struct mei_device *dev,
					       struct hbm_flow_control *fctrl)
731
{
732
	struct mei_cl *cl;
733

734
	if (!fctrl->host_addr) {
735
		/* single receive buffer */
736
		mei_hbm_add_single_tx_flow_ctrl_creds(dev, fctrl);
737 738 739
		return;
	}

740
	cl = mei_hbm_cl_find_by_cmd(dev, fctrl);
741
	if (cl) {
742
		cl->tx_flow_ctrl_creds++;
743
		cl_dbg(dev, cl, "flow control creds = %d.\n",
744
				cl->tx_flow_ctrl_creds);
745 746 747 748
	}
}


749
/**
750
 * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
751 752
 *
 * @dev: the device structure
753
 * @cl: a client to disconnect from
754
 *
755
 * Return: -EIO on write failure
756
 */
757
int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
758
{
759
	struct hbm_client_connect_request req;
760

761 762
	return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD,
				&req, sizeof(req));
763 764
}

T
Tomas Winkler 已提交
765 766 767 768 769 770
/**
 * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
 *
 * @dev: the device structure
 * @cl: a client to disconnect from
 *
771
 * Return: -EIO on write failure
T
Tomas Winkler 已提交
772 773 774
 */
int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
{
775
	struct hbm_client_connect_response resp;
776

777 778
	return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD,
				&resp, sizeof(resp));
T
Tomas Winkler 已提交
779 780
}

781
/**
782 783
 * mei_hbm_cl_disconnect_res - update the client state according
 *       disconnect response
784
 *
785
 * @dev: the device structure
786 787
 * @cl: mei host client
 * @cmd: disconnect client response host bus message
788
 */
789
static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl,
790
				      struct mei_hbm_cl_cmd *cmd)
791
{
792 793
	struct hbm_client_connect_response *rs =
		(struct hbm_client_connect_response *)cmd;
794

795
	cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status);
796

797
	if (rs->status == MEI_CL_DISCONN_SUCCESS)
798
		cl->state = MEI_FILE_DISCONNECT_REPLY;
799
	cl->status = 0;
800 801
}

802
/**
803
 * mei_hbm_cl_connect_req - send connection request to specific me client
804 805
 *
 * @dev: the device structure
806
 * @cl: a client to connect to
807
 *
808
 * Return: -EIO on write failure
809
 */
810
int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
811
{
812
	struct hbm_client_connect_request req;
813

814 815
	return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD,
				&req, sizeof(req));
816 817
}

818
/**
819 820
 * mei_hbm_cl_connect_res - update the client state according
 *        connection response
821
 *
822
 * @dev: the device structure
823 824
 * @cl: mei host client
 * @cmd: connect client response host bus message
825
 */
826
static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl,
827
				   struct mei_hbm_cl_cmd *cmd)
828
{
829 830
	struct hbm_client_connect_response *rs =
		(struct hbm_client_connect_response *)cmd;
831

832
	cl_dbg(dev, cl, "hbm: connect response status=%s\n",
833
			mei_cl_conn_status_str(rs->status));
834

835 836
	if (rs->status == MEI_CL_CONN_SUCCESS)
		cl->state = MEI_FILE_CONNECTED;
T
Tomas Winkler 已提交
837
	else {
838
		cl->state = MEI_FILE_DISCONNECT_REPLY;
839
		if (rs->status == MEI_CL_CONN_NOT_FOUND) {
T
Tomas Winkler 已提交
840
			mei_me_cl_del(dev, cl->me_cl);
841 842 843
			if (dev->dev_state == MEI_DEV_ENABLED)
				schedule_work(&dev->bus_rescan_work);
		}
T
Tomas Winkler 已提交
844
	}
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	cl->status = mei_cl_conn_status_to_errno(rs->status);
}

/**
 * mei_hbm_cl_res - process hbm response received on behalf
 *         an client
 *
 * @dev: the device structure
 * @rs:  hbm client message
 * @fop_type: file operation type
 */
static void mei_hbm_cl_res(struct mei_device *dev,
			   struct mei_hbm_cl_cmd *rs,
			   enum mei_cb_file_ops fop_type)
{
	struct mei_cl *cl;
	struct mei_cl_cb *cb, *next;
862

863
	cl = NULL;
864
	list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list, list) {
865

866
		cl = cb->cl;
867

868
		if (cb->fop_type != fop_type)
869
			continue;
870

871
		if (mei_hbm_cl_addr_equal(cl, rs)) {
872
			list_del_init(&cb->list);
873
			break;
874 875
		}
	}
876 877 878 879

	if (!cl)
		return;

880 881
	switch (fop_type) {
	case MEI_FOP_CONNECT:
882
		mei_hbm_cl_connect_res(dev, cl, rs);
883 884
		break;
	case MEI_FOP_DISCONNECT:
885
		mei_hbm_cl_disconnect_res(dev, cl, rs);
886
		break;
887 888 889 890 891 892
	case MEI_FOP_NOTIFY_START:
		mei_hbm_cl_notify_start_res(dev, cl, rs);
		break;
	case MEI_FOP_NOTIFY_STOP:
		mei_hbm_cl_notify_stop_res(dev, cl, rs);
		break;
893 894 895 896
	default:
		return;
	}

897
	cl->timer_count = 0;
898
	wake_up(&cl->wait);
899 900 901
}


902
/**
903 904
 * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
 *  host sends disconnect response
905 906
 *
 * @dev: the device structure.
907
 * @disconnect_req: disconnect request bus message from the me
T
Tomas Winkler 已提交
908
 *
909
 * Return: -ENOMEM on allocation failure
910
 */
T
Tomas Winkler 已提交
911
static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
912 913
		struct hbm_client_connect_request *disconnect_req)
{
914
	struct mei_cl *cl;
T
Tomas Winkler 已提交
915
	struct mei_cl_cb *cb;
916

917 918
	cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
	if (cl) {
919
		cl_warn(dev, cl, "fw disconnect request received\n");
920
		cl->state = MEI_FILE_DISCONNECTING;
921 922
		cl->timer_count = 0;

923 924
		cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT_RSP,
					       NULL);
925 926
		if (!cb)
			return -ENOMEM;
927
	}
T
Tomas Winkler 已提交
928
	return 0;
929 930
}

931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
/**
 * mei_hbm_pg_enter_res - PG enter response received
 *
 * @dev: the device structure.
 *
 * Return: 0 on success, -EPROTO on state mismatch
 */
static int mei_hbm_pg_enter_res(struct mei_device *dev)
{
	if (mei_pg_state(dev) != MEI_PG_OFF ||
	    dev->pg_event != MEI_PG_EVENT_WAIT) {
		dev_err(dev->dev, "hbm: pg entry response: state mismatch [%s, %d]\n",
			mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
		return -EPROTO;
	}

	dev->pg_event = MEI_PG_EVENT_RECEIVED;
	wake_up(&dev->wait_pg);

	return 0;
}

953 954 955 956 957 958 959 960 961 962 963
/**
 * mei_hbm_pg_resume - process with PG resume
 *
 * @dev: the device structure.
 */
void mei_hbm_pg_resume(struct mei_device *dev)
{
	pm_request_resume(dev->dev);
}
EXPORT_SYMBOL_GPL(mei_hbm_pg_resume);

964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
/**
 * mei_hbm_pg_exit_res - PG exit response received
 *
 * @dev: the device structure.
 *
 * Return: 0 on success, -EPROTO on state mismatch
 */
static int mei_hbm_pg_exit_res(struct mei_device *dev)
{
	if (mei_pg_state(dev) != MEI_PG_ON ||
	    (dev->pg_event != MEI_PG_EVENT_WAIT &&
	     dev->pg_event != MEI_PG_EVENT_IDLE)) {
		dev_err(dev->dev, "hbm: pg exit response: state mismatch [%s, %d]\n",
			mei_pg_state_str(mei_pg_state(dev)), dev->pg_event);
		return -EPROTO;
	}

	switch (dev->pg_event) {
	case MEI_PG_EVENT_WAIT:
		dev->pg_event = MEI_PG_EVENT_RECEIVED;
		wake_up(&dev->wait_pg);
		break;
	case MEI_PG_EVENT_IDLE:
		/*
		* If the driver is not waiting on this then
		* this is HW initiated exit from PG.
		* Start runtime pm resume sequence to exit from PG.
		*/
		dev->pg_event = MEI_PG_EVENT_RECEIVED;
993
		mei_hbm_pg_resume(dev);
994 995 996 997 998 999 1000 1001 1002 1003
		break;
	default:
		WARN(1, "hbm: pg exit response: unexpected pg event = %d\n",
		     dev->pg_event);
		return -EPROTO;
	}

	return 0;
}

1004
/**
1005
 * mei_hbm_config_features - check what hbm features and commands
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
 *        are supported by the fw
 *
 * @dev: the device structure
 */
static void mei_hbm_config_features(struct mei_device *dev)
{
	/* Power Gating Isolation Support */
	dev->hbm_f_pg_supported = 0;
	if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
		dev->hbm_f_pg_supported = 1;

	if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
	    dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
		dev->hbm_f_pg_supported = 1;
T
Tomas Winkler 已提交
1020

1021
	dev->hbm_f_dc_supported = 0;
T
Tomas Winkler 已提交
1022 1023
	if (dev->version.major_version >= HBM_MAJOR_VERSION_DC)
		dev->hbm_f_dc_supported = 1;
1024

1025
	dev->hbm_f_ie_supported = 0;
1026 1027 1028
	if (dev->version.major_version >= HBM_MAJOR_VERSION_IE)
		dev->hbm_f_ie_supported = 1;

1029
	/* disconnect on connect timeout instead of link reset */
1030
	dev->hbm_f_dot_supported = 0;
1031 1032
	if (dev->version.major_version >= HBM_MAJOR_VERSION_DOT)
		dev->hbm_f_dot_supported = 1;
1033 1034

	/* Notification Event Support */
1035
	dev->hbm_f_ev_supported = 0;
1036 1037
	if (dev->version.major_version >= HBM_MAJOR_VERSION_EV)
		dev->hbm_f_ev_supported = 1;
1038 1039

	/* Fixed Address Client Support */
1040
	dev->hbm_f_fa_supported = 0;
1041 1042
	if (dev->version.major_version >= HBM_MAJOR_VERSION_FA)
		dev->hbm_f_fa_supported = 1;
1043 1044

	/* OS ver message Support */
1045
	dev->hbm_f_os_supported = 0;
1046 1047
	if (dev->version.major_version >= HBM_MAJOR_VERSION_OS)
		dev->hbm_f_os_supported = 1;
1048 1049

	/* DMA Ring Support */
1050
	dev->hbm_f_dr_supported = 0;
1051 1052 1053 1054
	if (dev->version.major_version > HBM_MAJOR_VERSION_DR ||
	    (dev->version.major_version == HBM_MAJOR_VERSION_DR &&
	     dev->version.minor_version >= HBM_MINOR_VERSION_DR))
		dev->hbm_f_dr_supported = 1;
1055
}
1056

T
Tomas Winkler 已提交
1057 1058 1059 1060 1061
/**
 * mei_hbm_version_is_supported - checks whether the driver can
 *     support the hbm version of the device
 *
 * @dev: the device structure
1062
 * Return: true if driver can support hbm version of the device
T
Tomas Winkler 已提交
1063 1064 1065 1066 1067 1068 1069 1070
 */
bool mei_hbm_version_is_supported(struct mei_device *dev)
{
	return	(dev->version.major_version < HBM_MAJOR_VERSION) ||
		(dev->version.major_version == HBM_MAJOR_VERSION &&
		 dev->version.minor_version <= HBM_MINOR_VERSION);
}

1071 1072 1073 1074 1075
/**
 * mei_hbm_dispatch - bottom half read routine after ISR to
 * handle the read bus message cmd processing.
 *
 * @dev: the device structure
1076
 * @hdr: header of bus message
1077
 *
1078
 * Return: 0 on success and < 0 on failure
1079
 */
1080
int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
1081 1082 1083 1084 1085
{
	struct mei_bus_message *mei_msg;
	struct hbm_host_version_response *version_res;
	struct hbm_props_response *props_res;
	struct hbm_host_enum_response *enum_res;
T
Tomas Winkler 已提交
1086
	struct hbm_dma_setup_response *dma_setup_res;
T
Tomas Winkler 已提交
1087 1088
	struct hbm_add_client_request *add_cl_req;
	int ret;
1089

1090 1091
	struct mei_hbm_cl_cmd *cl_cmd;
	struct hbm_client_connect_request *disconnect_req;
1092
	struct hbm_flow_control *fctrl;
1093

1094 1095 1096 1097
	/* read the message to our buffer */
	BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
	mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
	mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
1098
	cl_cmd  = (struct mei_hbm_cl_cmd *)mei_msg;
1099

1100 1101 1102 1103
	/* ignore spurious message and prevent reset nesting
	 * hbm is put to idle during system reset
	 */
	if (dev->hbm_state == MEI_HBM_IDLE) {
1104
		dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
1105 1106 1107
		return 0;
	}

1108 1109
	switch (mei_msg->hbm_cmd) {
	case HOST_START_RES_CMD:
1110
		dev_dbg(dev->dev, "hbm: start: response message received.\n");
1111 1112 1113

		dev->init_clients_timer = 0;

1114
		version_res = (struct hbm_host_version_response *)mei_msg;
T
Tomas Winkler 已提交
1115

1116
		dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
T
Tomas Winkler 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
				HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
				version_res->me_max_version.major_version,
				version_res->me_max_version.minor_version);

		if (version_res->host_version_supported) {
			dev->version.major_version = HBM_MAJOR_VERSION;
			dev->version.minor_version = HBM_MINOR_VERSION;
		} else {
			dev->version.major_version =
				version_res->me_max_version.major_version;
			dev->version.minor_version =
				version_res->me_max_version.minor_version;
		}

		if (!mei_hbm_version_is_supported(dev)) {
1132
			dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
1133

1134
			dev->hbm_state = MEI_HBM_STOPPED;
T
Tomas Winkler 已提交
1135
			if (mei_hbm_stop_req(dev)) {
1136
				dev_err(dev->dev, "hbm: start: failed to send stop request\n");
1137 1138 1139 1140
				return -EIO;
			}
			break;
		}
T
Tomas Winkler 已提交
1141

1142 1143
		mei_hbm_config_features(dev);

1144
		if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
1145
		    dev->hbm_state != MEI_HBM_STARTING) {
1146
			dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
1147 1148
				dev->dev_state, dev->hbm_state);
			return -EPROTO;
1149
		}
1150

T
Tomas Winkler 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
		if (dev->hbm_f_dr_supported) {
			if (mei_dmam_ring_alloc(dev))
				dev_info(dev->dev, "running w/o dma ring\n");
			if (mei_dma_ring_is_allocated(dev)) {
				if (mei_hbm_dma_setup_req(dev))
					return -EIO;

				wake_up(&dev->wait_hbm_start);
				break;
			}
1161 1162
		}

T
Tomas Winkler 已提交
1163 1164 1165 1166 1167 1168
		dev->hbm_f_dr_supported = 0;
		mei_dmam_ring_free(dev);

		if (mei_hbm_enum_clients_req(dev))
			return -EIO;

1169
		wake_up(&dev->wait_hbm_start);
1170 1171
		break;

T
Tomas Winkler 已提交
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
	case MEI_HBM_DMA_SETUP_RES_CMD:
		dev_dbg(dev->dev, "hbm: dma setup response: message received.\n");

		dev->init_clients_timer = 0;

		if (dev->hbm_state != MEI_HBM_DR_SETUP) {
			dev_err(dev->dev, "hbm: dma setup response: state mismatch, [%d, %d]\n",
				dev->dev_state, dev->hbm_state);
			return -EPROTO;
		}

		dma_setup_res = (struct hbm_dma_setup_response *)mei_msg;

		if (dma_setup_res->status) {
1186 1187 1188 1189 1190 1191 1192 1193 1194
			u8 status = dma_setup_res->status;

			if (status == MEI_HBMS_NOT_ALLOWED) {
				dev_dbg(dev->dev, "hbm: dma setup not allowed\n");
			} else {
				dev_info(dev->dev, "hbm: dma setup response: failure = %d %s\n",
					 status,
					 mei_hbm_status_str(status));
			}
T
Tomas Winkler 已提交
1195 1196 1197 1198 1199 1200 1201 1202
			dev->hbm_f_dr_supported = 0;
			mei_dmam_ring_free(dev);
		}

		if (mei_hbm_enum_clients_req(dev))
			return -EIO;
		break;

1203
	case CLIENT_CONNECT_RES_CMD:
1204
		dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
1205
		mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
1206 1207 1208
		break;

	case CLIENT_DISCONNECT_RES_CMD:
1209
		dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
1210
		mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
1211 1212 1213
		break;

	case MEI_FLOW_CONTROL_CMD:
1214
		dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
1215

1216 1217
		fctrl = (struct hbm_flow_control *)mei_msg;
		mei_hbm_cl_tx_flow_ctrl_creds_res(dev, fctrl);
1218 1219
		break;

1220
	case MEI_PG_ISOLATION_ENTRY_RES_CMD:
1221 1222 1223 1224
		dev_dbg(dev->dev, "hbm: power gate isolation entry response received\n");
		ret = mei_hbm_pg_enter_res(dev);
		if (ret)
			return ret;
1225 1226 1227
		break;

	case MEI_PG_ISOLATION_EXIT_REQ_CMD:
1228 1229 1230 1231
		dev_dbg(dev->dev, "hbm: power gate isolation exit request received\n");
		ret = mei_hbm_pg_exit_res(dev);
		if (ret)
			return ret;
1232 1233
		break;

1234
	case HOST_CLIENT_PROPERTIES_RES_CMD:
1235
		dev_dbg(dev->dev, "hbm: properties response: message received.\n");
1236 1237 1238

		dev->init_clients_timer = 0;

1239 1240
		if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
		    dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
1241
			dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
1242
				dev->dev_state, dev->hbm_state);
1243 1244 1245
			return -EPROTO;
		}

1246 1247
		props_res = (struct hbm_props_response *)mei_msg;

1248 1249 1250 1251
		if (props_res->status == MEI_HBMS_CLIENT_NOT_FOUND) {
			dev_dbg(dev->dev, "hbm: properties response: %d CLIENT_NOT_FOUND\n",
				props_res->me_addr);
		} else if (props_res->status) {
1252
			dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
1253 1254
				props_res->status,
				mei_hbm_status_str(props_res->status));
1255
			return -EPROTO;
1256 1257
		} else {
			mei_hbm_me_cl_add(dev, props_res);
1258 1259
		}

1260
		/* request property for the next client */
1261
		if (mei_hbm_prop_req(dev, props_res->me_addr + 1))
1262
			return -EIO;
1263 1264 1265 1266

		break;

	case HOST_ENUM_RES_CMD:
1267
		dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
1268 1269 1270

		dev->init_clients_timer = 0;

1271
		enum_res = (struct hbm_host_enum_response *) mei_msg;
1272 1273 1274
		BUILD_BUG_ON(sizeof(dev->me_clients_map)
				< sizeof(enum_res->valid_addresses));
		memcpy(dev->me_clients_map, enum_res->valid_addresses,
1275
				sizeof(enum_res->valid_addresses));
1276 1277 1278

		if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
		    dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
1279
			dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
1280 1281
				dev->dev_state, dev->hbm_state);
			return -EPROTO;
1282
		}
1283 1284 1285 1286

		dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;

		/* first property request */
1287
		if (mei_hbm_prop_req(dev, 0))
1288 1289
			return -EIO;

1290 1291 1292
		break;

	case HOST_STOP_RES_CMD:
1293
		dev_dbg(dev->dev, "hbm: stop response: message received\n");
1294 1295 1296 1297

		dev->init_clients_timer = 0;

		if (dev->hbm_state != MEI_HBM_STOPPED) {
1298
			dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
1299 1300 1301
				dev->dev_state, dev->hbm_state);
			return -EPROTO;
		}
T
Tomas Winkler 已提交
1302

1303
		dev->dev_state = MEI_DEV_POWER_DOWN;
1304
		dev_info(dev->dev, "hbm: stop response: resetting.\n");
1305 1306
		/* force the reset */
		return -EPROTO;
1307 1308 1309
		break;

	case CLIENT_DISCONNECT_REQ_CMD:
1310
		dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
1311

1312
		disconnect_req = (struct hbm_client_connect_request *)mei_msg;
1313
		mei_hbm_fw_disconnect_req(dev, disconnect_req);
1314 1315 1316
		break;

	case ME_STOP_REQ_CMD:
1317
		dev_dbg(dev->dev, "hbm: stop request: message received\n");
1318
		dev->hbm_state = MEI_HBM_STOPPED;
T
Tomas Winkler 已提交
1319
		if (mei_hbm_stop_req(dev)) {
1320
			dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
T
Tomas Winkler 已提交
1321 1322
			return -EIO;
		}
1323
		break;
T
Tomas Winkler 已提交
1324 1325 1326 1327 1328 1329 1330

	case MEI_HBM_ADD_CLIENT_REQ_CMD:
		dev_dbg(dev->dev, "hbm: add client request received\n");
		/*
		 * after the host receives the enum_resp
		 * message clients may be added or removed
		 */
1331
		if (dev->hbm_state <= MEI_HBM_ENUM_CLIENTS ||
T
Tomas Winkler 已提交
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
		    dev->hbm_state >= MEI_HBM_STOPPED) {
			dev_err(dev->dev, "hbm: add client: state mismatch, [%d, %d]\n",
				dev->dev_state, dev->hbm_state);
			return -EPROTO;
		}
		add_cl_req = (struct hbm_add_client_request *)mei_msg;
		ret = mei_hbm_fw_add_cl_req(dev, add_cl_req);
		if (ret) {
			dev_err(dev->dev, "hbm: add client: failed to send response %d\n",
				ret);
			return -EIO;
		}
		dev_dbg(dev->dev, "hbm: add client request processed\n");
		break;

1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
	case MEI_HBM_NOTIFY_RES_CMD:
		dev_dbg(dev->dev, "hbm: notify response received\n");
		mei_hbm_cl_res(dev, cl_cmd, notify_res_to_fop(cl_cmd));
		break;

	case MEI_HBM_NOTIFICATION_CMD:
		dev_dbg(dev->dev, "hbm: notification\n");
		mei_hbm_cl_notify(dev, cl_cmd);
		break;

1357
	default:
1358 1359
		WARN(1, "hbm: wrong command %d\n", mei_msg->hbm_cmd);
		return -EPROTO;
1360 1361

	}
1362
	return 0;
1363 1364
}