xref: /openbmc/linux/drivers/misc/mei/hbm.c (revision 8684014d)
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2012, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  */
16 
17 #include <linux/export.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22 
23 #include <linux/mei.h>
24 
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
28 
29 static const char *mei_hbm_status_str(enum mei_hbm_status status)
30 {
31 #define MEI_HBM_STATUS(status) case MEI_HBMS_##status: return #status
32 	switch (status) {
33 	MEI_HBM_STATUS(SUCCESS);
34 	MEI_HBM_STATUS(CLIENT_NOT_FOUND);
35 	MEI_HBM_STATUS(ALREADY_EXISTS);
36 	MEI_HBM_STATUS(REJECTED);
37 	MEI_HBM_STATUS(INVALID_PARAMETER);
38 	MEI_HBM_STATUS(NOT_ALLOWED);
39 	MEI_HBM_STATUS(ALREADY_STARTED);
40 	MEI_HBM_STATUS(NOT_STARTED);
41 	default: return "unknown";
42 	}
43 #undef MEI_HBM_STATUS
44 };
45 
46 static const char *mei_cl_conn_status_str(enum mei_cl_connect_status status)
47 {
48 #define MEI_CL_CS(status) case MEI_CL_CONN_##status: return #status
49 	switch (status) {
50 	MEI_CL_CS(SUCCESS);
51 	MEI_CL_CS(NOT_FOUND);
52 	MEI_CL_CS(ALREADY_STARTED);
53 	MEI_CL_CS(OUT_OF_RESOURCES);
54 	MEI_CL_CS(MESSAGE_SMALL);
55 	default: return "unknown";
56 	}
57 #undef MEI_CL_CCS
58 }
59 
60 const char *mei_hbm_state_str(enum mei_hbm_state state)
61 {
62 #define MEI_HBM_STATE(state) case MEI_HBM_##state: return #state
63 	switch (state) {
64 	MEI_HBM_STATE(IDLE);
65 	MEI_HBM_STATE(STARTING);
66 	MEI_HBM_STATE(STARTED);
67 	MEI_HBM_STATE(ENUM_CLIENTS);
68 	MEI_HBM_STATE(CLIENT_PROPERTIES);
69 	MEI_HBM_STATE(STOPPED);
70 	default:
71 		return "unknown";
72 	}
73 #undef MEI_HBM_STATE
74 }
75 
76 /**
77  * mei_cl_conn_status_to_errno - convert client connect response
78  * status to error code
79  *
80  * @status: client connect response status
81  *
82  * Return: corresponding error code
83  */
84 static int mei_cl_conn_status_to_errno(enum mei_cl_connect_status status)
85 {
86 	switch (status) {
87 	case MEI_CL_CONN_SUCCESS:          return 0;
88 	case MEI_CL_CONN_NOT_FOUND:        return -ENOTTY;
89 	case MEI_CL_CONN_ALREADY_STARTED:  return -EBUSY;
90 	case MEI_CL_CONN_OUT_OF_RESOURCES: return -EBUSY;
91 	case MEI_CL_CONN_MESSAGE_SMALL:    return -EINVAL;
92 	default:                           return -EINVAL;
93 	}
94 }
95 
96 /**
97  * mei_hbm_idle - set hbm to idle state
98  *
99  * @dev: the device structure
100  */
101 void mei_hbm_idle(struct mei_device *dev)
102 {
103 	dev->init_clients_timer = 0;
104 	dev->hbm_state = MEI_HBM_IDLE;
105 }
106 
107 /**
108  * mei_me_cl_remove_all - remove all me clients
109  *
110  * @dev: the device structure
111  */
112 static void mei_me_cl_remove_all(struct mei_device *dev)
113 {
114 	struct mei_me_client *me_cl, *next;
115 
116 	list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) {
117 			list_del(&me_cl->list);
118 			kfree(me_cl);
119 	}
120 }
121 
122 /**
123  * mei_hbm_reset - reset hbm counters and book keeping data structurs
124  *
125  * @dev: the device structure
126  */
127 void mei_hbm_reset(struct mei_device *dev)
128 {
129 	dev->me_client_index = 0;
130 
131 	mei_me_cl_remove_all(dev);
132 
133 	mei_hbm_idle(dev);
134 }
135 
136 /**
137  * mei_hbm_hdr - construct hbm header
138  *
139  * @hdr: hbm header
140  * @length: payload length
141  */
142 
143 static inline void mei_hbm_hdr(struct mei_msg_hdr *hdr, size_t length)
144 {
145 	hdr->host_addr = 0;
146 	hdr->me_addr = 0;
147 	hdr->length = length;
148 	hdr->msg_complete = 1;
149 	hdr->reserved = 0;
150 }
151 
152 /**
153  * mei_hbm_cl_hdr - construct client hbm header
154  *
155  * @cl: client
156  * @hbm_cmd: host bus message command
157  * @buf: buffer for cl header
158  * @len: buffer length
159  */
160 static inline
161 void mei_hbm_cl_hdr(struct mei_cl *cl, u8 hbm_cmd, void *buf, size_t len)
162 {
163 	struct mei_hbm_cl_cmd *cmd = buf;
164 
165 	memset(cmd, 0, len);
166 
167 	cmd->hbm_cmd = hbm_cmd;
168 	cmd->host_addr = cl->host_client_id;
169 	cmd->me_addr = cl->me_client_id;
170 }
171 
172 /**
173  * mei_hbm_cl_write - write simple hbm client message
174  *
175  * @dev: the device structure
176  * @cl: client
177  * @hbm_cmd: host bus message command
178  * @len: buffer length
179  *
180  * Return: 0 on success, <0 on failure.
181  */
182 static inline
183 int mei_hbm_cl_write(struct mei_device *dev,
184 		     struct mei_cl *cl, u8 hbm_cmd, size_t len)
185 {
186 	struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
187 
188 	mei_hbm_hdr(mei_hdr, len);
189 	mei_hbm_cl_hdr(cl, hbm_cmd, dev->wr_msg.data, len);
190 
191 	return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
192 }
193 
194 /**
195  * mei_hbm_cl_addr_equal - check if the client's and
196  *	the message address match
197  *
198  * @cl: client
199  * @cmd: hbm client message
200  *
201  * Return: true if addresses are the same
202  */
203 static inline
204 bool mei_hbm_cl_addr_equal(struct mei_cl *cl, struct mei_hbm_cl_cmd *cmd)
205 {
206 	return cl->host_client_id == cmd->host_addr &&
207 		cl->me_client_id == cmd->me_addr;
208 }
209 
210 /**
211  * mei_hbm_cl_find_by_cmd - find recipient client
212  *
213  * @dev: the device structure
214  * @buf: a buffer with hbm cl command
215  *
216  * Return: the recipient client or NULL if not found
217  */
218 static inline
219 struct mei_cl *mei_hbm_cl_find_by_cmd(struct mei_device *dev, void *buf)
220 {
221 	struct mei_hbm_cl_cmd *cmd = (struct mei_hbm_cl_cmd *)buf;
222 	struct mei_cl *cl;
223 
224 	list_for_each_entry(cl, &dev->file_list, link)
225 		if (mei_hbm_cl_addr_equal(cl, cmd))
226 			return cl;
227 	return NULL;
228 }
229 
230 
231 /**
232  * mei_hbm_start_wait - wait for start response message.
233  *
234  * @dev: the device structure
235  *
236  * Return: 0 on success and < 0 on failure
237  */
238 int mei_hbm_start_wait(struct mei_device *dev)
239 {
240 	int ret;
241 
242 	if (dev->hbm_state > MEI_HBM_STARTING)
243 		return 0;
244 
245 	mutex_unlock(&dev->device_lock);
246 	ret = wait_event_timeout(dev->wait_hbm_start,
247 			dev->hbm_state != MEI_HBM_STARTING,
248 			mei_secs_to_jiffies(MEI_HBM_TIMEOUT));
249 	mutex_lock(&dev->device_lock);
250 
251 	if (ret == 0 && (dev->hbm_state <= MEI_HBM_STARTING)) {
252 		dev->hbm_state = MEI_HBM_IDLE;
253 		dev_err(dev->dev, "waiting for mei start failed\n");
254 		return -ETIME;
255 	}
256 	return 0;
257 }
258 
259 /**
260  * mei_hbm_start_req - sends start request message.
261  *
262  * @dev: the device structure
263  *
264  * Return: 0 on success and < 0 on failure
265  */
266 int mei_hbm_start_req(struct mei_device *dev)
267 {
268 	struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
269 	struct hbm_host_version_request *start_req;
270 	const size_t len = sizeof(struct hbm_host_version_request);
271 	int ret;
272 
273 	mei_hbm_reset(dev);
274 
275 	mei_hbm_hdr(mei_hdr, len);
276 
277 	/* host start message */
278 	start_req = (struct hbm_host_version_request *)dev->wr_msg.data;
279 	memset(start_req, 0, len);
280 	start_req->hbm_cmd = HOST_START_REQ_CMD;
281 	start_req->host_version.major_version = HBM_MAJOR_VERSION;
282 	start_req->host_version.minor_version = HBM_MINOR_VERSION;
283 
284 	dev->hbm_state = MEI_HBM_IDLE;
285 	ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
286 	if (ret) {
287 		dev_err(dev->dev, "version message write failed: ret = %d\n",
288 			ret);
289 		return ret;
290 	}
291 
292 	dev->hbm_state = MEI_HBM_STARTING;
293 	dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
294 	return 0;
295 }
296 
297 /*
298  * mei_hbm_enum_clients_req - sends enumeration client request message.
299  *
300  * @dev: the device structure
301  *
302  * Return: 0 on success and < 0 on failure
303  */
304 static int mei_hbm_enum_clients_req(struct mei_device *dev)
305 {
306 	struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
307 	struct hbm_host_enum_request *enum_req;
308 	const size_t len = sizeof(struct hbm_host_enum_request);
309 	int ret;
310 
311 	/* enumerate clients */
312 	mei_hbm_hdr(mei_hdr, len);
313 
314 	enum_req = (struct hbm_host_enum_request *)dev->wr_msg.data;
315 	memset(enum_req, 0, len);
316 	enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
317 
318 	ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
319 	if (ret) {
320 		dev_err(dev->dev, "enumeration request write failed: ret = %d.\n",
321 			ret);
322 		return ret;
323 	}
324 	dev->hbm_state = MEI_HBM_ENUM_CLIENTS;
325 	dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
326 	return 0;
327 }
328 
329 /*
330  * mei_hbm_me_cl_add - add new me client to the list
331  *
332  * @dev: the device structure
333  * @res: hbm property response
334  *
335  * Return: 0 on success and -ENOMEM on allocation failure
336  */
337 
338 static int mei_hbm_me_cl_add(struct mei_device *dev,
339 			     struct hbm_props_response *res)
340 {
341 	struct mei_me_client *me_cl;
342 
343 	me_cl = kzalloc(sizeof(struct mei_me_client), GFP_KERNEL);
344 	if (!me_cl)
345 		return -ENOMEM;
346 
347 	me_cl->props = res->client_properties;
348 	me_cl->client_id = res->me_addr;
349 	me_cl->mei_flow_ctrl_creds = 0;
350 
351 	list_add(&me_cl->list, &dev->me_clients);
352 	return 0;
353 }
354 
355 /**
356  * mei_hbm_prop_req - request property for a single client
357  *
358  * @dev: the device structure
359  *
360  * Return: 0 on success and < 0 on failure
361  */
362 
363 static int mei_hbm_prop_req(struct mei_device *dev)
364 {
365 
366 	struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
367 	struct hbm_props_request *prop_req;
368 	const size_t len = sizeof(struct hbm_props_request);
369 	unsigned long next_client_index;
370 	int ret;
371 
372 	next_client_index = find_next_bit(dev->me_clients_map, MEI_CLIENTS_MAX,
373 					  dev->me_client_index);
374 
375 	/* We got all client properties */
376 	if (next_client_index == MEI_CLIENTS_MAX) {
377 		dev->hbm_state = MEI_HBM_STARTED;
378 		schedule_work(&dev->init_work);
379 
380 		return 0;
381 	}
382 
383 	mei_hbm_hdr(mei_hdr, len);
384 	prop_req = (struct hbm_props_request *)dev->wr_msg.data;
385 
386 	memset(prop_req, 0, sizeof(struct hbm_props_request));
387 
388 	prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
389 	prop_req->me_addr = next_client_index;
390 
391 	ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
392 	if (ret) {
393 		dev_err(dev->dev, "properties request write failed: ret = %d\n",
394 			ret);
395 		return ret;
396 	}
397 
398 	dev->init_clients_timer = MEI_CLIENTS_INIT_TIMEOUT;
399 	dev->me_client_index = next_client_index;
400 
401 	return 0;
402 }
403 
404 /*
405  * mei_hbm_pg - sends pg command
406  *
407  * @dev: the device structure
408  * @pg_cmd: the pg command code
409  *
410  * Return: -EIO on write failure
411  *         -EOPNOTSUPP if the operation is not supported by the protocol
412  */
413 int mei_hbm_pg(struct mei_device *dev, u8 pg_cmd)
414 {
415 	struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
416 	struct hbm_power_gate *req;
417 	const size_t len = sizeof(struct hbm_power_gate);
418 	int ret;
419 
420 	if (!dev->hbm_f_pg_supported)
421 		return -EOPNOTSUPP;
422 
423 	mei_hbm_hdr(mei_hdr, len);
424 
425 	req = (struct hbm_power_gate *)dev->wr_msg.data;
426 	memset(req, 0, len);
427 	req->hbm_cmd = pg_cmd;
428 
429 	ret = mei_write_message(dev, mei_hdr, dev->wr_msg.data);
430 	if (ret)
431 		dev_err(dev->dev, "power gate command write failed.\n");
432 	return ret;
433 }
434 EXPORT_SYMBOL_GPL(mei_hbm_pg);
435 
436 /**
437  * mei_hbm_stop_req - send stop request message
438  *
439  * @dev: mei device
440  *
441  * Return: -EIO on write failure
442  */
443 static int mei_hbm_stop_req(struct mei_device *dev)
444 {
445 	struct mei_msg_hdr *mei_hdr = &dev->wr_msg.hdr;
446 	struct hbm_host_stop_request *req =
447 			(struct hbm_host_stop_request *)dev->wr_msg.data;
448 	const size_t len = sizeof(struct hbm_host_stop_request);
449 
450 	mei_hbm_hdr(mei_hdr, len);
451 
452 	memset(req, 0, len);
453 	req->hbm_cmd = HOST_STOP_REQ_CMD;
454 	req->reason = DRIVER_STOP_REQUEST;
455 
456 	return mei_write_message(dev, mei_hdr, dev->wr_msg.data);
457 }
458 
459 /**
460  * mei_hbm_cl_flow_control_req - sends flow control request.
461  *
462  * @dev: the device structure
463  * @cl: client info
464  *
465  * Return: -EIO on write failure
466  */
467 int mei_hbm_cl_flow_control_req(struct mei_device *dev, struct mei_cl *cl)
468 {
469 	const size_t len = sizeof(struct hbm_flow_control);
470 
471 	cl_dbg(dev, cl, "sending flow control\n");
472 	return mei_hbm_cl_write(dev, cl, MEI_FLOW_CONTROL_CMD, len);
473 }
474 
475 /**
476  * mei_hbm_add_single_flow_creds - adds single buffer credentials.
477  *
478  * @dev: the device structure
479  * @flow: flow control.
480  *
481  * Return: 0 on success, < 0 otherwise
482  */
483 static int mei_hbm_add_single_flow_creds(struct mei_device *dev,
484 				  struct hbm_flow_control *flow)
485 {
486 	struct mei_me_client *me_cl;
487 
488 	me_cl = mei_me_cl_by_id(dev, flow->me_addr);
489 	if (!me_cl) {
490 		dev_err(dev->dev, "no such me client %d\n",
491 			flow->me_addr);
492 		return -ENOENT;
493 	}
494 
495 	if (WARN_ON(me_cl->props.single_recv_buf == 0))
496 		return -EINVAL;
497 
498 	me_cl->mei_flow_ctrl_creds++;
499 	dev_dbg(dev->dev, "recv flow ctrl msg ME %d (single) creds = %d.\n",
500 	    flow->me_addr, me_cl->mei_flow_ctrl_creds);
501 
502 	return 0;
503 }
504 
505 /**
506  * mei_hbm_cl_flow_control_res - flow control response from me
507  *
508  * @dev: the device structure
509  * @flow_control: flow control response bus message
510  */
511 static void mei_hbm_cl_flow_control_res(struct mei_device *dev,
512 					struct hbm_flow_control *flow_control)
513 {
514 	struct mei_cl *cl;
515 
516 	if (!flow_control->host_addr) {
517 		/* single receive buffer */
518 		mei_hbm_add_single_flow_creds(dev, flow_control);
519 		return;
520 	}
521 
522 	cl = mei_hbm_cl_find_by_cmd(dev, flow_control);
523 	if (cl) {
524 		cl->mei_flow_ctrl_creds++;
525 		cl_dbg(dev, cl, "flow control creds = %d.\n",
526 				cl->mei_flow_ctrl_creds);
527 	}
528 }
529 
530 
531 /**
532  * mei_hbm_cl_disconnect_req - sends disconnect message to fw.
533  *
534  * @dev: the device structure
535  * @cl: a client to disconnect from
536  *
537  * Return: -EIO on write failure
538  */
539 int mei_hbm_cl_disconnect_req(struct mei_device *dev, struct mei_cl *cl)
540 {
541 	const size_t len = sizeof(struct hbm_client_connect_request);
542 
543 	return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_REQ_CMD, len);
544 }
545 
546 /**
547  * mei_hbm_cl_disconnect_rsp - sends disconnect respose to the FW
548  *
549  * @dev: the device structure
550  * @cl: a client to disconnect from
551  *
552  * Return: -EIO on write failure
553  */
554 int mei_hbm_cl_disconnect_rsp(struct mei_device *dev, struct mei_cl *cl)
555 {
556 	const size_t len = sizeof(struct hbm_client_connect_response);
557 
558 	return mei_hbm_cl_write(dev, cl, CLIENT_DISCONNECT_RES_CMD, len);
559 }
560 
561 /**
562  * mei_hbm_cl_disconnect_res - update the client state according
563  *       disconnect response
564  *
565  * @dev: the device structure
566  * @cl: mei host client
567  * @cmd: disconnect client response host bus message
568  */
569 static void mei_hbm_cl_disconnect_res(struct mei_device *dev, struct mei_cl *cl,
570 				      struct mei_hbm_cl_cmd *cmd)
571 {
572 	struct hbm_client_connect_response *rs =
573 		(struct hbm_client_connect_response *)cmd;
574 
575 	cl_dbg(dev, cl, "hbm: disconnect response status=%d\n", rs->status);
576 
577 	if (rs->status == MEI_CL_DISCONN_SUCCESS)
578 		cl->state = MEI_FILE_DISCONNECTED;
579 	cl->status = 0;
580 }
581 
582 /**
583  * mei_hbm_cl_connect_req - send connection request to specific me client
584  *
585  * @dev: the device structure
586  * @cl: a client to connect to
587  *
588  * Return: -EIO on write failure
589  */
590 int mei_hbm_cl_connect_req(struct mei_device *dev, struct mei_cl *cl)
591 {
592 	const size_t len = sizeof(struct hbm_client_connect_request);
593 
594 	return mei_hbm_cl_write(dev, cl, CLIENT_CONNECT_REQ_CMD, len);
595 }
596 
597 /**
598  * mei_hbm_cl_connect_res - update the client state according
599  *        connection response
600  *
601  * @dev: the device structure
602  * @cl: mei host client
603  * @cmd: connect client response host bus message
604  */
605 static void mei_hbm_cl_connect_res(struct mei_device *dev, struct mei_cl *cl,
606 				   struct mei_hbm_cl_cmd *cmd)
607 {
608 	struct hbm_client_connect_response *rs =
609 		(struct hbm_client_connect_response *)cmd;
610 
611 	cl_dbg(dev, cl, "hbm: connect response status=%s\n",
612 			mei_cl_conn_status_str(rs->status));
613 
614 	if (rs->status == MEI_CL_CONN_SUCCESS)
615 		cl->state = MEI_FILE_CONNECTED;
616 	else
617 		cl->state = MEI_FILE_DISCONNECTED;
618 	cl->status = mei_cl_conn_status_to_errno(rs->status);
619 }
620 
621 /**
622  * mei_hbm_cl_res - process hbm response received on behalf
623  *         an client
624  *
625  * @dev: the device structure
626  * @rs:  hbm client message
627  * @fop_type: file operation type
628  */
629 static void mei_hbm_cl_res(struct mei_device *dev,
630 			   struct mei_hbm_cl_cmd *rs,
631 			   enum mei_cb_file_ops fop_type)
632 {
633 	struct mei_cl *cl;
634 	struct mei_cl_cb *cb, *next;
635 
636 	cl = NULL;
637 	list_for_each_entry_safe(cb, next, &dev->ctrl_rd_list.list, list) {
638 
639 		cl = cb->cl;
640 
641 		if (cb->fop_type != fop_type)
642 			continue;
643 
644 		if (mei_hbm_cl_addr_equal(cl, rs)) {
645 			list_del(&cb->list);
646 			break;
647 		}
648 	}
649 
650 	if (!cl)
651 		return;
652 
653 	switch (fop_type) {
654 	case MEI_FOP_CONNECT:
655 		mei_hbm_cl_connect_res(dev, cl, rs);
656 		break;
657 	case MEI_FOP_DISCONNECT:
658 		mei_hbm_cl_disconnect_res(dev, cl, rs);
659 		break;
660 	default:
661 		return;
662 	}
663 
664 	cl->timer_count = 0;
665 	wake_up(&cl->wait);
666 }
667 
668 
669 /**
670  * mei_hbm_fw_disconnect_req - disconnect request initiated by ME firmware
671  *  host sends disconnect response
672  *
673  * @dev: the device structure.
674  * @disconnect_req: disconnect request bus message from the me
675  *
676  * Return: -ENOMEM on allocation failure
677  */
678 static int mei_hbm_fw_disconnect_req(struct mei_device *dev,
679 		struct hbm_client_connect_request *disconnect_req)
680 {
681 	struct mei_cl *cl;
682 	struct mei_cl_cb *cb;
683 
684 	cl = mei_hbm_cl_find_by_cmd(dev, disconnect_req);
685 	if (cl) {
686 		cl_dbg(dev, cl, "disconnect request received\n");
687 		cl->state = MEI_FILE_DISCONNECTED;
688 		cl->timer_count = 0;
689 
690 		cb = mei_io_cb_init(cl, NULL);
691 		if (!cb)
692 			return -ENOMEM;
693 		cb->fop_type = MEI_FOP_DISCONNECT_RSP;
694 		cl_dbg(dev, cl, "add disconnect response as first\n");
695 		list_add(&cb->list, &dev->ctrl_wr_list.list);
696 	}
697 	return 0;
698 }
699 
700 /**
701  * mei_hbm_config_features - check what hbm features and commands
702  *        are supported by the fw
703  *
704  * @dev: the device structure
705  */
706 static void mei_hbm_config_features(struct mei_device *dev)
707 {
708 	/* Power Gating Isolation Support */
709 	dev->hbm_f_pg_supported = 0;
710 	if (dev->version.major_version > HBM_MAJOR_VERSION_PGI)
711 		dev->hbm_f_pg_supported = 1;
712 
713 	if (dev->version.major_version == HBM_MAJOR_VERSION_PGI &&
714 	    dev->version.minor_version >= HBM_MINOR_VERSION_PGI)
715 		dev->hbm_f_pg_supported = 1;
716 }
717 
718 /**
719  * mei_hbm_version_is_supported - checks whether the driver can
720  *     support the hbm version of the device
721  *
722  * @dev: the device structure
723  * Return: true if driver can support hbm version of the device
724  */
725 bool mei_hbm_version_is_supported(struct mei_device *dev)
726 {
727 	return	(dev->version.major_version < HBM_MAJOR_VERSION) ||
728 		(dev->version.major_version == HBM_MAJOR_VERSION &&
729 		 dev->version.minor_version <= HBM_MINOR_VERSION);
730 }
731 
732 /**
733  * mei_hbm_dispatch - bottom half read routine after ISR to
734  * handle the read bus message cmd processing.
735  *
736  * @dev: the device structure
737  * @hdr: header of bus message
738  *
739  * Return: 0 on success and < 0 on failure
740  */
741 int mei_hbm_dispatch(struct mei_device *dev, struct mei_msg_hdr *hdr)
742 {
743 	struct mei_bus_message *mei_msg;
744 	struct hbm_host_version_response *version_res;
745 	struct hbm_props_response *props_res;
746 	struct hbm_host_enum_response *enum_res;
747 
748 	struct mei_hbm_cl_cmd *cl_cmd;
749 	struct hbm_client_connect_request *disconnect_req;
750 	struct hbm_flow_control *flow_control;
751 
752 	/* read the message to our buffer */
753 	BUG_ON(hdr->length >= sizeof(dev->rd_msg_buf));
754 	mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
755 	mei_msg = (struct mei_bus_message *)dev->rd_msg_buf;
756 	cl_cmd  = (struct mei_hbm_cl_cmd *)mei_msg;
757 
758 	/* ignore spurious message and prevent reset nesting
759 	 * hbm is put to idle during system reset
760 	 */
761 	if (dev->hbm_state == MEI_HBM_IDLE) {
762 		dev_dbg(dev->dev, "hbm: state is idle ignore spurious messages\n");
763 		return 0;
764 	}
765 
766 	switch (mei_msg->hbm_cmd) {
767 	case HOST_START_RES_CMD:
768 		dev_dbg(dev->dev, "hbm: start: response message received.\n");
769 
770 		dev->init_clients_timer = 0;
771 
772 		version_res = (struct hbm_host_version_response *)mei_msg;
773 
774 		dev_dbg(dev->dev, "HBM VERSION: DRIVER=%02d:%02d DEVICE=%02d:%02d\n",
775 				HBM_MAJOR_VERSION, HBM_MINOR_VERSION,
776 				version_res->me_max_version.major_version,
777 				version_res->me_max_version.minor_version);
778 
779 		if (version_res->host_version_supported) {
780 			dev->version.major_version = HBM_MAJOR_VERSION;
781 			dev->version.minor_version = HBM_MINOR_VERSION;
782 		} else {
783 			dev->version.major_version =
784 				version_res->me_max_version.major_version;
785 			dev->version.minor_version =
786 				version_res->me_max_version.minor_version;
787 		}
788 
789 		if (!mei_hbm_version_is_supported(dev)) {
790 			dev_warn(dev->dev, "hbm: start: version mismatch - stopping the driver.\n");
791 
792 			dev->hbm_state = MEI_HBM_STOPPED;
793 			if (mei_hbm_stop_req(dev)) {
794 				dev_err(dev->dev, "hbm: start: failed to send stop request\n");
795 				return -EIO;
796 			}
797 			break;
798 		}
799 
800 		mei_hbm_config_features(dev);
801 
802 		if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
803 		    dev->hbm_state != MEI_HBM_STARTING) {
804 			dev_err(dev->dev, "hbm: start: state mismatch, [%d, %d]\n",
805 				dev->dev_state, dev->hbm_state);
806 			return -EPROTO;
807 		}
808 
809 		if (mei_hbm_enum_clients_req(dev)) {
810 			dev_err(dev->dev, "hbm: start: failed to send enumeration request\n");
811 			return -EIO;
812 		}
813 
814 		wake_up(&dev->wait_hbm_start);
815 		break;
816 
817 	case CLIENT_CONNECT_RES_CMD:
818 		dev_dbg(dev->dev, "hbm: client connect response: message received.\n");
819 		mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_CONNECT);
820 		break;
821 
822 	case CLIENT_DISCONNECT_RES_CMD:
823 		dev_dbg(dev->dev, "hbm: client disconnect response: message received.\n");
824 		mei_hbm_cl_res(dev, cl_cmd, MEI_FOP_DISCONNECT);
825 		break;
826 
827 	case MEI_FLOW_CONTROL_CMD:
828 		dev_dbg(dev->dev, "hbm: client flow control response: message received.\n");
829 
830 		flow_control = (struct hbm_flow_control *) mei_msg;
831 		mei_hbm_cl_flow_control_res(dev, flow_control);
832 		break;
833 
834 	case MEI_PG_ISOLATION_ENTRY_RES_CMD:
835 		dev_dbg(dev->dev, "power gate isolation entry response received\n");
836 		dev->pg_event = MEI_PG_EVENT_RECEIVED;
837 		if (waitqueue_active(&dev->wait_pg))
838 			wake_up(&dev->wait_pg);
839 		break;
840 
841 	case MEI_PG_ISOLATION_EXIT_REQ_CMD:
842 		dev_dbg(dev->dev, "power gate isolation exit request received\n");
843 		dev->pg_event = MEI_PG_EVENT_RECEIVED;
844 		if (waitqueue_active(&dev->wait_pg))
845 			wake_up(&dev->wait_pg);
846 		else
847 			/*
848 			* If the driver is not waiting on this then
849 			* this is HW initiated exit from PG.
850 			* Start runtime pm resume sequence to exit from PG.
851 			*/
852 			pm_request_resume(dev->dev);
853 		break;
854 
855 	case HOST_CLIENT_PROPERTIES_RES_CMD:
856 		dev_dbg(dev->dev, "hbm: properties response: message received.\n");
857 
858 		dev->init_clients_timer = 0;
859 
860 		if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
861 		    dev->hbm_state != MEI_HBM_CLIENT_PROPERTIES) {
862 			dev_err(dev->dev, "hbm: properties response: state mismatch, [%d, %d]\n",
863 				dev->dev_state, dev->hbm_state);
864 			return -EPROTO;
865 		}
866 
867 		props_res = (struct hbm_props_response *)mei_msg;
868 
869 		if (props_res->status) {
870 			dev_err(dev->dev, "hbm: properties response: wrong status = %d %s\n",
871 				props_res->status,
872 				mei_hbm_status_str(props_res->status));
873 			return -EPROTO;
874 		}
875 
876 		mei_hbm_me_cl_add(dev, props_res);
877 
878 		dev->me_client_index++;
879 
880 		/* request property for the next client */
881 		if (mei_hbm_prop_req(dev))
882 			return -EIO;
883 
884 		break;
885 
886 	case HOST_ENUM_RES_CMD:
887 		dev_dbg(dev->dev, "hbm: enumeration response: message received\n");
888 
889 		dev->init_clients_timer = 0;
890 
891 		enum_res = (struct hbm_host_enum_response *) mei_msg;
892 		BUILD_BUG_ON(sizeof(dev->me_clients_map)
893 				< sizeof(enum_res->valid_addresses));
894 		memcpy(dev->me_clients_map, enum_res->valid_addresses,
895 				sizeof(enum_res->valid_addresses));
896 
897 		if (dev->dev_state != MEI_DEV_INIT_CLIENTS ||
898 		    dev->hbm_state != MEI_HBM_ENUM_CLIENTS) {
899 			dev_err(dev->dev, "hbm: enumeration response: state mismatch, [%d, %d]\n",
900 				dev->dev_state, dev->hbm_state);
901 			return -EPROTO;
902 		}
903 
904 		dev->hbm_state = MEI_HBM_CLIENT_PROPERTIES;
905 
906 		/* first property request */
907 		if (mei_hbm_prop_req(dev))
908 			return -EIO;
909 
910 		break;
911 
912 	case HOST_STOP_RES_CMD:
913 		dev_dbg(dev->dev, "hbm: stop response: message received\n");
914 
915 		dev->init_clients_timer = 0;
916 
917 		if (dev->hbm_state != MEI_HBM_STOPPED) {
918 			dev_err(dev->dev, "hbm: stop response: state mismatch, [%d, %d]\n",
919 				dev->dev_state, dev->hbm_state);
920 			return -EPROTO;
921 		}
922 
923 		dev->dev_state = MEI_DEV_POWER_DOWN;
924 		dev_info(dev->dev, "hbm: stop response: resetting.\n");
925 		/* force the reset */
926 		return -EPROTO;
927 		break;
928 
929 	case CLIENT_DISCONNECT_REQ_CMD:
930 		dev_dbg(dev->dev, "hbm: disconnect request: message received\n");
931 
932 		disconnect_req = (struct hbm_client_connect_request *)mei_msg;
933 		mei_hbm_fw_disconnect_req(dev, disconnect_req);
934 		break;
935 
936 	case ME_STOP_REQ_CMD:
937 		dev_dbg(dev->dev, "hbm: stop request: message received\n");
938 		dev->hbm_state = MEI_HBM_STOPPED;
939 		if (mei_hbm_stop_req(dev)) {
940 			dev_err(dev->dev, "hbm: stop request: failed to send stop request\n");
941 			return -EIO;
942 		}
943 		break;
944 	default:
945 		BUG();
946 		break;
947 
948 	}
949 	return 0;
950 }
951 
952