1 /*
2  * ISHTP bus layer messages handling
3  *
4  * Copyright (c) 2003-2016, 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/slab.h>
19 #include <linux/sched.h>
20 #include <linux/wait.h>
21 #include <linux/spinlock.h>
22 #include <linux/miscdevice.h>
23 #include "ishtp-dev.h"
24 #include "hbm.h"
25 #include "client.h"
26 
27 /**
28  * ishtp_hbm_fw_cl_allocate() - Allocate FW clients
29  * @dev: ISHTP device instance
30  *
31  * Allocates storage for fw clients
32  */
33 static void ishtp_hbm_fw_cl_allocate(struct ishtp_device *dev)
34 {
35 	struct ishtp_fw_client *clients;
36 	int b;
37 
38 	/* count how many ISH clients we have */
39 	for_each_set_bit(b, dev->fw_clients_map, ISHTP_CLIENTS_MAX)
40 		dev->fw_clients_num++;
41 
42 	if (dev->fw_clients_num <= 0)
43 		return;
44 
45 	/* allocate storage for fw clients representation */
46 	clients = kcalloc(dev->fw_clients_num, sizeof(struct ishtp_fw_client),
47 			  GFP_KERNEL);
48 	if (!clients) {
49 		dev->dev_state = ISHTP_DEV_RESETTING;
50 		ish_hw_reset(dev);
51 		return;
52 	}
53 	dev->fw_clients = clients;
54 }
55 
56 /**
57  * ishtp_hbm_cl_hdr() - construct client hbm header
58  * @cl: client
59  * @hbm_cmd: host bus message command
60  * @buf: buffer for cl header
61  * @len: buffer length
62  *
63  * Initialize HBM buffer
64  */
65 static inline void ishtp_hbm_cl_hdr(struct ishtp_cl *cl, uint8_t hbm_cmd,
66 	void *buf, size_t len)
67 {
68 	struct ishtp_hbm_cl_cmd *cmd = buf;
69 
70 	memset(cmd, 0, len);
71 
72 	cmd->hbm_cmd = hbm_cmd;
73 	cmd->host_addr = cl->host_client_id;
74 	cmd->fw_addr = cl->fw_client_id;
75 }
76 
77 /**
78  * ishtp_hbm_cl_addr_equal() - Compare client address
79  * @cl: client
80  * @buf: Client command buffer
81  *
82  * Compare client address with the address in command buffer
83  *
84  * Return: True if they have the same address
85  */
86 static inline bool ishtp_hbm_cl_addr_equal(struct ishtp_cl *cl, void *buf)
87 {
88 	struct ishtp_hbm_cl_cmd *cmd = buf;
89 
90 	return cl->host_client_id == cmd->host_addr &&
91 		cl->fw_client_id == cmd->fw_addr;
92 }
93 
94 /**
95  * ishtp_hbm_start_wait() - Wait for HBM start message
96  * @dev: ISHTP device instance
97  *
98  * Wait for HBM start message from firmware
99  *
100  * Return: 0 if HBM start is/was received else timeout error
101  */
102 int ishtp_hbm_start_wait(struct ishtp_device *dev)
103 {
104 	int ret;
105 
106 	if (dev->hbm_state > ISHTP_HBM_START)
107 		return 0;
108 
109 	dev_dbg(dev->devc, "Going to wait for ishtp start. hbm_state=%08X\n",
110 		dev->hbm_state);
111 	ret = wait_event_interruptible_timeout(dev->wait_hbm_recvd_msg,
112 					dev->hbm_state >= ISHTP_HBM_STARTED,
113 					(ISHTP_INTEROP_TIMEOUT * HZ));
114 
115 	dev_dbg(dev->devc,
116 		"Woke up from waiting for ishtp start. hbm_state=%08X\n",
117 		dev->hbm_state);
118 
119 	if (ret <= 0 && (dev->hbm_state <= ISHTP_HBM_START)) {
120 		dev->hbm_state = ISHTP_HBM_IDLE;
121 		dev_err(dev->devc,
122 		"waiting for ishtp start failed. ret=%d hbm_state=%08X\n",
123 			ret, dev->hbm_state);
124 		return -ETIMEDOUT;
125 	}
126 	return 0;
127 }
128 
129 /**
130  * ishtp_hbm_start_req() - Send HBM start message
131  * @dev: ISHTP device instance
132  *
133  * Send HBM start message to firmware
134  *
135  * Return: 0 if success else error code
136  */
137 int ishtp_hbm_start_req(struct ishtp_device *dev)
138 {
139 	struct ishtp_msg_hdr hdr;
140 	unsigned char data[128];
141 	struct ishtp_msg_hdr *ishtp_hdr = &hdr;
142 	struct hbm_host_version_request *start_req;
143 	const size_t len = sizeof(struct hbm_host_version_request);
144 
145 	ishtp_hbm_hdr(ishtp_hdr, len);
146 
147 	/* host start message */
148 	start_req = (struct hbm_host_version_request *)data;
149 	memset(start_req, 0, len);
150 	start_req->hbm_cmd = HOST_START_REQ_CMD;
151 	start_req->host_version.major_version = HBM_MAJOR_VERSION;
152 	start_req->host_version.minor_version = HBM_MINOR_VERSION;
153 
154 	/*
155 	 * (!) Response to HBM start may be so quick that this thread would get
156 	 * preempted BEFORE managing to set hbm_state = ISHTP_HBM_START.
157 	 * So set it at first, change back to ISHTP_HBM_IDLE upon failure
158 	 */
159 	dev->hbm_state = ISHTP_HBM_START;
160 	if (ishtp_write_message(dev, ishtp_hdr, data)) {
161 		dev_err(dev->devc, "version message send failed\n");
162 		dev->dev_state = ISHTP_DEV_RESETTING;
163 		dev->hbm_state = ISHTP_HBM_IDLE;
164 		ish_hw_reset(dev);
165 		return -ENODEV;
166 	}
167 
168 	return 0;
169 }
170 
171 /**
172  * ishtp_hbm_enum_clients_req() - Send client enum req
173  * @dev: ISHTP device instance
174  *
175  * Send enumeration client request message
176  *
177  * Return: 0 if success else error code
178  */
179 void ishtp_hbm_enum_clients_req(struct ishtp_device *dev)
180 {
181 	struct ishtp_msg_hdr hdr;
182 	unsigned char data[128];
183 	struct ishtp_msg_hdr *ishtp_hdr = &hdr;
184 	struct hbm_host_enum_request *enum_req;
185 	const size_t len = sizeof(struct hbm_host_enum_request);
186 
187 	/* enumerate clients */
188 	ishtp_hbm_hdr(ishtp_hdr, len);
189 
190 	enum_req = (struct hbm_host_enum_request *)data;
191 	memset(enum_req, 0, len);
192 	enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
193 
194 	if (ishtp_write_message(dev, ishtp_hdr, data)) {
195 		dev->dev_state = ISHTP_DEV_RESETTING;
196 		dev_err(dev->devc, "enumeration request send failed\n");
197 		ish_hw_reset(dev);
198 	}
199 	dev->hbm_state = ISHTP_HBM_ENUM_CLIENTS;
200 }
201 
202 /**
203  * ishtp_hbm_prop_req() - Request property
204  * @dev: ISHTP device instance
205  *
206  * Request property for a single client
207  *
208  * Return: 0 if success else error code
209  */
210 static int ishtp_hbm_prop_req(struct ishtp_device *dev)
211 {
212 
213 	struct ishtp_msg_hdr hdr;
214 	unsigned char data[128];
215 	struct ishtp_msg_hdr *ishtp_hdr = &hdr;
216 	struct hbm_props_request *prop_req;
217 	const size_t len = sizeof(struct hbm_props_request);
218 	unsigned long next_client_index;
219 	uint8_t client_num;
220 
221 	client_num = dev->fw_client_presentation_num;
222 
223 	next_client_index = find_next_bit(dev->fw_clients_map,
224 		ISHTP_CLIENTS_MAX, dev->fw_client_index);
225 
226 	/* We got all client properties */
227 	if (next_client_index == ISHTP_CLIENTS_MAX) {
228 		dev->hbm_state = ISHTP_HBM_WORKING;
229 		dev->dev_state = ISHTP_DEV_ENABLED;
230 
231 		for (dev->fw_client_presentation_num = 1;
232 			dev->fw_client_presentation_num < client_num + 1;
233 				++dev->fw_client_presentation_num)
234 			/* Add new client device */
235 			ishtp_bus_new_client(dev);
236 		return 0;
237 	}
238 
239 	dev->fw_clients[client_num].client_id = next_client_index;
240 
241 	ishtp_hbm_hdr(ishtp_hdr, len);
242 	prop_req = (struct hbm_props_request *)data;
243 
244 	memset(prop_req, 0, sizeof(struct hbm_props_request));
245 
246 	prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
247 	prop_req->address = next_client_index;
248 
249 	if (ishtp_write_message(dev, ishtp_hdr, data)) {
250 		dev->dev_state = ISHTP_DEV_RESETTING;
251 		dev_err(dev->devc, "properties request send failed\n");
252 		ish_hw_reset(dev);
253 		return -EIO;
254 	}
255 
256 	dev->fw_client_index = next_client_index;
257 
258 	return 0;
259 }
260 
261 /**
262  * ishtp_hbm_stop_req() - Send HBM stop
263  * @dev: ISHTP device instance
264  *
265  * Send stop request message
266  */
267 static void ishtp_hbm_stop_req(struct ishtp_device *dev)
268 {
269 	struct ishtp_msg_hdr hdr;
270 	unsigned char data[128];
271 	struct ishtp_msg_hdr *ishtp_hdr = &hdr;
272 	struct hbm_host_stop_request *req;
273 	const size_t len = sizeof(struct hbm_host_stop_request);
274 
275 	ishtp_hbm_hdr(ishtp_hdr, len);
276 	req = (struct hbm_host_stop_request *)data;
277 
278 	memset(req, 0, sizeof(struct hbm_host_stop_request));
279 	req->hbm_cmd = HOST_STOP_REQ_CMD;
280 	req->reason = DRIVER_STOP_REQUEST;
281 
282 	ishtp_write_message(dev, ishtp_hdr, data);
283 }
284 
285 /**
286  * ishtp_hbm_cl_flow_control_req() - Send flow control request
287  * @dev: ISHTP device instance
288  * @cl: ISHTP client instance
289  *
290  * Send flow control request
291  *
292  * Return: 0 if success else error code
293  */
294 int ishtp_hbm_cl_flow_control_req(struct ishtp_device *dev,
295 				  struct ishtp_cl *cl)
296 {
297 	struct ishtp_msg_hdr hdr;
298 	unsigned char data[128];
299 	struct ishtp_msg_hdr *ishtp_hdr = &hdr;
300 	const size_t len = sizeof(struct hbm_flow_control);
301 	int	rv;
302 	unsigned int	num_frags;
303 	unsigned long	flags;
304 
305 	spin_lock_irqsave(&cl->fc_spinlock, flags);
306 	ishtp_hbm_hdr(ishtp_hdr, len);
307 	ishtp_hbm_cl_hdr(cl, ISHTP_FLOW_CONTROL_CMD, data, len);
308 
309 	/*
310 	 * Sync possible race when RB recycle and packet receive paths
311 	 * both try to send an out FC
312 	 */
313 	if (cl->out_flow_ctrl_creds) {
314 		spin_unlock_irqrestore(&cl->fc_spinlock, flags);
315 		return	0;
316 	}
317 
318 	num_frags = cl->recv_msg_num_frags;
319 	cl->recv_msg_num_frags = 0;
320 
321 	rv = ishtp_write_message(dev, ishtp_hdr, data);
322 	if (!rv) {
323 		++cl->out_flow_ctrl_creds;
324 		++cl->out_flow_ctrl_cnt;
325 		getnstimeofday(&cl->ts_out_fc);
326 		if (cl->ts_rx.tv_sec && cl->ts_rx.tv_nsec) {
327 			struct timespec ts_diff;
328 
329 			ts_diff = timespec_sub(cl->ts_out_fc, cl->ts_rx);
330 			if (timespec_compare(&ts_diff, &cl->ts_max_fc_delay)
331 					> 0)
332 				cl->ts_max_fc_delay = ts_diff;
333 		}
334 	} else {
335 		++cl->err_send_fc;
336 	}
337 
338 	spin_unlock_irqrestore(&cl->fc_spinlock, flags);
339 	return	rv;
340 }
341 
342 /**
343  * ishtp_hbm_cl_disconnect_req() - Send disconnect request
344  * @dev: ISHTP device instance
345  * @cl: ISHTP client instance
346  *
347  * Send disconnect message to fw
348  *
349  * Return: 0 if success else error code
350  */
351 int ishtp_hbm_cl_disconnect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
352 {
353 	struct ishtp_msg_hdr hdr;
354 	unsigned char data[128];
355 	struct ishtp_msg_hdr *ishtp_hdr = &hdr;
356 	const size_t len = sizeof(struct hbm_client_connect_request);
357 
358 	ishtp_hbm_hdr(ishtp_hdr, len);
359 	ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, data, len);
360 
361 	return ishtp_write_message(dev, ishtp_hdr, data);
362 }
363 
364 /**
365  * ishtp_hbm_cl_disconnect_res() - Get disconnect response
366  * @dev: ISHTP device instance
367  * @rs: Response message
368  *
369  * Received disconnect response from fw
370  */
371 static void ishtp_hbm_cl_disconnect_res(struct ishtp_device *dev,
372 	struct hbm_client_connect_response *rs)
373 {
374 	struct ishtp_cl *cl = NULL;
375 	unsigned long	flags;
376 
377 	spin_lock_irqsave(&dev->cl_list_lock, flags);
378 	list_for_each_entry(cl, &dev->cl_list, link) {
379 		if (!rs->status && ishtp_hbm_cl_addr_equal(cl, rs)) {
380 			cl->state = ISHTP_CL_DISCONNECTED;
381 			break;
382 		}
383 	}
384 	if (cl)
385 		wake_up_interruptible(&cl->wait_ctrl_res);
386 	spin_unlock_irqrestore(&dev->cl_list_lock, flags);
387 }
388 
389 /**
390  * ishtp_hbm_cl_connect_req() - Send connect request
391  * @dev: ISHTP device instance
392  * @cl: client device instance
393  *
394  * Send connection request to specific fw client
395  *
396  * Return: 0 if success else error code
397  */
398 int ishtp_hbm_cl_connect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
399 {
400 	struct ishtp_msg_hdr hdr;
401 	unsigned char data[128];
402 	struct ishtp_msg_hdr *ishtp_hdr = &hdr;
403 	const size_t len = sizeof(struct hbm_client_connect_request);
404 
405 	ishtp_hbm_hdr(ishtp_hdr, len);
406 	ishtp_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, data, len);
407 
408 	return ishtp_write_message(dev, ishtp_hdr, data);
409 }
410 
411 /**
412  * ishtp_hbm_cl_connect_res() - Get connect response
413  * @dev: ISHTP device instance
414  * @rs: Response message
415  *
416  * Received connect response from fw
417  */
418 static void ishtp_hbm_cl_connect_res(struct ishtp_device *dev,
419 	struct hbm_client_connect_response *rs)
420 {
421 	struct ishtp_cl *cl = NULL;
422 	unsigned long	flags;
423 
424 	spin_lock_irqsave(&dev->cl_list_lock, flags);
425 	list_for_each_entry(cl, &dev->cl_list, link) {
426 		if (ishtp_hbm_cl_addr_equal(cl, rs)) {
427 			if (!rs->status) {
428 				cl->state = ISHTP_CL_CONNECTED;
429 				cl->status = 0;
430 			} else {
431 				cl->state = ISHTP_CL_DISCONNECTED;
432 				cl->status = -ENODEV;
433 			}
434 			break;
435 		}
436 	}
437 	if (cl)
438 		wake_up_interruptible(&cl->wait_ctrl_res);
439 	spin_unlock_irqrestore(&dev->cl_list_lock, flags);
440 }
441 
442 /**
443  * ishtp_client_disconnect_request() - Receive disconnect request
444  * @dev: ISHTP device instance
445  * @disconnect_req: disconnect request structure
446  *
447  * Disconnect request bus message from the fw. Send diconnect response.
448  */
449 static void ishtp_hbm_fw_disconnect_req(struct ishtp_device *dev,
450 	struct hbm_client_connect_request *disconnect_req)
451 {
452 	struct ishtp_cl *cl;
453 	const size_t len = sizeof(struct hbm_client_connect_response);
454 	unsigned long	flags;
455 	struct ishtp_msg_hdr hdr;
456 	unsigned char data[4];	/* All HBM messages are 4 bytes */
457 
458 	spin_lock_irqsave(&dev->cl_list_lock, flags);
459 	list_for_each_entry(cl, &dev->cl_list, link) {
460 		if (ishtp_hbm_cl_addr_equal(cl, disconnect_req)) {
461 			cl->state = ISHTP_CL_DISCONNECTED;
462 
463 			/* send disconnect response */
464 			ishtp_hbm_hdr(&hdr, len);
465 			ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, data,
466 				len);
467 			ishtp_write_message(dev, &hdr, data);
468 			break;
469 		}
470 	}
471 	spin_unlock_irqrestore(&dev->cl_list_lock, flags);
472 }
473 
474 /**
475  * ishtp_hbm_dma_xfer_ack(() - Receive transfer ACK
476  * @dev: ISHTP device instance
477  * @dma_xfer: HBM transfer message
478  *
479  * Receive ack for ISHTP-over-DMA client message
480  */
481 static void ishtp_hbm_dma_xfer_ack(struct ishtp_device *dev,
482 				   struct dma_xfer_hbm *dma_xfer)
483 {
484 	void	*msg;
485 	uint64_t	offs;
486 	struct ishtp_msg_hdr	*ishtp_hdr =
487 		(struct ishtp_msg_hdr *)&dev->ishtp_msg_hdr;
488 	unsigned int	msg_offs;
489 	struct ishtp_cl *cl;
490 
491 	for (msg_offs = 0; msg_offs < ishtp_hdr->length;
492 		msg_offs += sizeof(struct dma_xfer_hbm)) {
493 		offs = dma_xfer->msg_addr - dev->ishtp_host_dma_tx_buf_phys;
494 		if (offs > dev->ishtp_host_dma_tx_buf_size) {
495 			dev_err(dev->devc, "Bad DMA Tx ack message address\n");
496 			return;
497 		}
498 		if (dma_xfer->msg_length >
499 				dev->ishtp_host_dma_tx_buf_size - offs) {
500 			dev_err(dev->devc, "Bad DMA Tx ack message size\n");
501 			return;
502 		}
503 
504 		/* logical address of the acked mem */
505 		msg = (unsigned char *)dev->ishtp_host_dma_tx_buf + offs;
506 		ishtp_cl_release_dma_acked_mem(dev, msg, dma_xfer->msg_length);
507 
508 		list_for_each_entry(cl, &dev->cl_list, link) {
509 			if (cl->fw_client_id == dma_xfer->fw_client_id &&
510 			    cl->host_client_id == dma_xfer->host_client_id)
511 				/*
512 				 * in case that a single ack may be sent
513 				 * over several dma transfers, and the last msg
514 				 * addr was inside the acked memory, but not in
515 				 * its start
516 				 */
517 				if (cl->last_dma_addr >=
518 							(unsigned char *)msg &&
519 						cl->last_dma_addr <
520 						(unsigned char *)msg +
521 						dma_xfer->msg_length) {
522 					cl->last_dma_acked = 1;
523 
524 					if (!list_empty(&cl->tx_list.list) &&
525 						cl->ishtp_flow_ctrl_creds) {
526 						/*
527 						 * start sending the first msg
528 						 */
529 						ishtp_cl_send_msg(dev, cl);
530 					}
531 				}
532 		}
533 		++dma_xfer;
534 	}
535 }
536 
537 /**
538  * ishtp_hbm_dma_xfer() - Receive DMA transfer message
539  * @dev: ISHTP device instance
540  * @dma_xfer: HBM transfer message
541  *
542  * Receive ISHTP-over-DMA client message
543  */
544 static void ishtp_hbm_dma_xfer(struct ishtp_device *dev,
545 			       struct dma_xfer_hbm *dma_xfer)
546 {
547 	void	*msg;
548 	uint64_t	offs;
549 	struct ishtp_msg_hdr	hdr;
550 	struct ishtp_msg_hdr	*ishtp_hdr =
551 		(struct ishtp_msg_hdr *) &dev->ishtp_msg_hdr;
552 	struct dma_xfer_hbm	*prm = dma_xfer;
553 	unsigned int	msg_offs;
554 
555 	for (msg_offs = 0; msg_offs < ishtp_hdr->length;
556 		msg_offs += sizeof(struct dma_xfer_hbm)) {
557 
558 		offs = dma_xfer->msg_addr - dev->ishtp_host_dma_rx_buf_phys;
559 		if (offs > dev->ishtp_host_dma_rx_buf_size) {
560 			dev_err(dev->devc, "Bad DMA Rx message address\n");
561 			return;
562 		}
563 		if (dma_xfer->msg_length >
564 				dev->ishtp_host_dma_rx_buf_size - offs) {
565 			dev_err(dev->devc, "Bad DMA Rx message size\n");
566 			return;
567 		}
568 		msg = dev->ishtp_host_dma_rx_buf + offs;
569 		recv_ishtp_cl_msg_dma(dev, msg, dma_xfer);
570 		dma_xfer->hbm = DMA_XFER_ACK;	/* Prepare for response */
571 		++dma_xfer;
572 	}
573 
574 	/* Send DMA_XFER_ACK [...] */
575 	ishtp_hbm_hdr(&hdr, ishtp_hdr->length);
576 	ishtp_write_message(dev, &hdr, (unsigned char *)prm);
577 }
578 
579 /**
580  * ishtp_hbm_dispatch() - HBM dispatch function
581  * @dev: ISHTP device instance
582  * @hdr: bus message
583  *
584  * Bottom half read routine after ISR to handle the read bus message cmd
585  * processing
586  */
587 void ishtp_hbm_dispatch(struct ishtp_device *dev,
588 			struct ishtp_bus_message *hdr)
589 {
590 	struct ishtp_bus_message *ishtp_msg;
591 	struct ishtp_fw_client *fw_client;
592 	struct hbm_host_version_response *version_res;
593 	struct hbm_client_connect_response *connect_res;
594 	struct hbm_client_connect_response *disconnect_res;
595 	struct hbm_client_connect_request *disconnect_req;
596 	struct hbm_props_response *props_res;
597 	struct hbm_host_enum_response *enum_res;
598 	struct ishtp_msg_hdr ishtp_hdr;
599 	struct dma_alloc_notify	dma_alloc_notify;
600 	struct dma_xfer_hbm	*dma_xfer;
601 
602 	ishtp_msg = hdr;
603 
604 	switch (ishtp_msg->hbm_cmd) {
605 	case HOST_START_RES_CMD:
606 		version_res = (struct hbm_host_version_response *)ishtp_msg;
607 		if (!version_res->host_version_supported) {
608 			dev->version = version_res->fw_max_version;
609 
610 			dev->hbm_state = ISHTP_HBM_STOPPED;
611 			ishtp_hbm_stop_req(dev);
612 			return;
613 		}
614 
615 		dev->version.major_version = HBM_MAJOR_VERSION;
616 		dev->version.minor_version = HBM_MINOR_VERSION;
617 		if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
618 				dev->hbm_state == ISHTP_HBM_START) {
619 			dev->hbm_state = ISHTP_HBM_STARTED;
620 			ishtp_hbm_enum_clients_req(dev);
621 		} else {
622 			dev_err(dev->devc,
623 				"reset: wrong host start response\n");
624 			/* BUG: why do we arrive here? */
625 			ish_hw_reset(dev);
626 			return;
627 		}
628 
629 		wake_up_interruptible(&dev->wait_hbm_recvd_msg);
630 		break;
631 
632 	case CLIENT_CONNECT_RES_CMD:
633 		connect_res = (struct hbm_client_connect_response *)ishtp_msg;
634 		ishtp_hbm_cl_connect_res(dev, connect_res);
635 		break;
636 
637 	case CLIENT_DISCONNECT_RES_CMD:
638 		disconnect_res =
639 			(struct hbm_client_connect_response *)ishtp_msg;
640 		ishtp_hbm_cl_disconnect_res(dev, disconnect_res);
641 		break;
642 
643 	case HOST_CLIENT_PROPERTIES_RES_CMD:
644 		props_res = (struct hbm_props_response *)ishtp_msg;
645 		fw_client = &dev->fw_clients[dev->fw_client_presentation_num];
646 
647 		if (props_res->status || !dev->fw_clients) {
648 			dev_err(dev->devc,
649 			"reset: properties response hbm wrong status\n");
650 			ish_hw_reset(dev);
651 			return;
652 		}
653 
654 		if (fw_client->client_id != props_res->address) {
655 			dev_err(dev->devc,
656 				"reset: host properties response address mismatch [%02X %02X]\n",
657 				fw_client->client_id, props_res->address);
658 			ish_hw_reset(dev);
659 			return;
660 		}
661 
662 		if (dev->dev_state != ISHTP_DEV_INIT_CLIENTS ||
663 			dev->hbm_state != ISHTP_HBM_CLIENT_PROPERTIES) {
664 			dev_err(dev->devc,
665 				"reset: unexpected properties response\n");
666 			ish_hw_reset(dev);
667 			return;
668 		}
669 
670 		fw_client->props = props_res->client_properties;
671 		dev->fw_client_index++;
672 		dev->fw_client_presentation_num++;
673 
674 		/* request property for the next client */
675 		ishtp_hbm_prop_req(dev);
676 
677 		if (dev->dev_state != ISHTP_DEV_ENABLED)
678 			break;
679 
680 		if (!ishtp_use_dma_transfer())
681 			break;
682 
683 		dev_dbg(dev->devc, "Requesting to use DMA\n");
684 		ishtp_cl_alloc_dma_buf(dev);
685 		if (dev->ishtp_host_dma_rx_buf) {
686 			const size_t len = sizeof(dma_alloc_notify);
687 
688 			memset(&dma_alloc_notify, 0, sizeof(dma_alloc_notify));
689 			dma_alloc_notify.hbm = DMA_BUFFER_ALLOC_NOTIFY;
690 			dma_alloc_notify.buf_size =
691 					dev->ishtp_host_dma_rx_buf_size;
692 			dma_alloc_notify.buf_address =
693 					dev->ishtp_host_dma_rx_buf_phys;
694 			ishtp_hbm_hdr(&ishtp_hdr, len);
695 			ishtp_write_message(dev, &ishtp_hdr,
696 				(unsigned char *)&dma_alloc_notify);
697 		}
698 
699 		break;
700 
701 	case HOST_ENUM_RES_CMD:
702 		enum_res = (struct hbm_host_enum_response *) ishtp_msg;
703 		memcpy(dev->fw_clients_map, enum_res->valid_addresses, 32);
704 		if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
705 			dev->hbm_state == ISHTP_HBM_ENUM_CLIENTS) {
706 			dev->fw_client_presentation_num = 0;
707 			dev->fw_client_index = 0;
708 
709 			ishtp_hbm_fw_cl_allocate(dev);
710 			dev->hbm_state = ISHTP_HBM_CLIENT_PROPERTIES;
711 
712 			/* first property request */
713 			ishtp_hbm_prop_req(dev);
714 		} else {
715 			dev_err(dev->devc,
716 			      "reset: unexpected enumeration response hbm\n");
717 			ish_hw_reset(dev);
718 			return;
719 		}
720 		break;
721 
722 	case HOST_STOP_RES_CMD:
723 		if (dev->hbm_state != ISHTP_HBM_STOPPED)
724 			dev_err(dev->devc, "unexpected stop response\n");
725 
726 		dev->dev_state = ISHTP_DEV_DISABLED;
727 		dev_info(dev->devc, "reset: FW stop response\n");
728 		ish_hw_reset(dev);
729 		break;
730 
731 	case CLIENT_DISCONNECT_REQ_CMD:
732 		/* search for client */
733 		disconnect_req =
734 			(struct hbm_client_connect_request *)ishtp_msg;
735 		ishtp_hbm_fw_disconnect_req(dev, disconnect_req);
736 		break;
737 
738 	case FW_STOP_REQ_CMD:
739 		dev->hbm_state = ISHTP_HBM_STOPPED;
740 		break;
741 
742 	case DMA_BUFFER_ALLOC_RESPONSE:
743 		dev->ishtp_host_dma_enabled = 1;
744 		break;
745 
746 	case DMA_XFER:
747 		dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
748 		if (!dev->ishtp_host_dma_enabled) {
749 			dev_err(dev->devc,
750 				"DMA XFER requested but DMA is not enabled\n");
751 			break;
752 		}
753 		ishtp_hbm_dma_xfer(dev, dma_xfer);
754 		break;
755 
756 	case DMA_XFER_ACK:
757 		dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
758 		if (!dev->ishtp_host_dma_enabled ||
759 		    !dev->ishtp_host_dma_tx_buf) {
760 			dev_err(dev->devc,
761 				"DMA XFER acked but DMA Tx is not enabled\n");
762 			break;
763 		}
764 		ishtp_hbm_dma_xfer_ack(dev, dma_xfer);
765 		break;
766 
767 	default:
768 		dev_err(dev->devc, "unknown HBM: %u\n",
769 			(unsigned int)ishtp_msg->hbm_cmd);
770 
771 		break;
772 	}
773 }
774 
775 /**
776  * bh_hbm_work_fn() - HBM work function
777  * @work: work struct
778  *
779  * Bottom half processing work function (instead of thread handler)
780  * for processing hbm messages
781  */
782 void	bh_hbm_work_fn(struct work_struct *work)
783 {
784 	unsigned long	flags;
785 	struct ishtp_device	*dev;
786 	unsigned char	hbm[IPC_PAYLOAD_SIZE];
787 
788 	dev = container_of(work, struct ishtp_device, bh_hbm_work);
789 	spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
790 	if (dev->rd_msg_fifo_head != dev->rd_msg_fifo_tail) {
791 		memcpy(hbm, dev->rd_msg_fifo + dev->rd_msg_fifo_head,
792 			IPC_PAYLOAD_SIZE);
793 		dev->rd_msg_fifo_head =
794 			(dev->rd_msg_fifo_head + IPC_PAYLOAD_SIZE) %
795 			(RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
796 		spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
797 		ishtp_hbm_dispatch(dev, (struct ishtp_bus_message *)hbm);
798 	} else {
799 		spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
800 	}
801 }
802 
803 /**
804  * recv_hbm() - Receive HBM message
805  * @dev: ISHTP device instance
806  * @ishtp_hdr: received bus message
807  *
808  * Receive and process ISHTP bus messages in ISR context. This will schedule
809  * work function to process message
810  */
811 void	recv_hbm(struct ishtp_device *dev, struct ishtp_msg_hdr *ishtp_hdr)
812 {
813 	uint8_t	rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
814 	struct ishtp_bus_message	*ishtp_msg =
815 		(struct ishtp_bus_message *)rd_msg_buf;
816 	unsigned long	flags;
817 
818 	dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
819 
820 	/* Flow control - handle in place */
821 	if (ishtp_msg->hbm_cmd == ISHTP_FLOW_CONTROL_CMD) {
822 		struct hbm_flow_control *flow_control =
823 			(struct hbm_flow_control *)ishtp_msg;
824 		struct ishtp_cl *cl = NULL;
825 		unsigned long	flags, tx_flags;
826 
827 		spin_lock_irqsave(&dev->cl_list_lock, flags);
828 		list_for_each_entry(cl, &dev->cl_list, link) {
829 			if (cl->host_client_id == flow_control->host_addr &&
830 					cl->fw_client_id ==
831 					flow_control->fw_addr) {
832 				/*
833 				 * NOTE: It's valid only for counting
834 				 * flow-control implementation to receive a
835 				 * FC in the middle of sending. Meanwhile not
836 				 * supported
837 				 */
838 				if (cl->ishtp_flow_ctrl_creds)
839 					dev_err(dev->devc,
840 					 "recv extra FC from FW client %u (host client %u) (FC count was %d)\n",
841 					 (unsigned int)cl->fw_client_id,
842 					 (unsigned int)cl->host_client_id,
843 					 cl->ishtp_flow_ctrl_creds);
844 				else {
845 					++cl->ishtp_flow_ctrl_creds;
846 					++cl->ishtp_flow_ctrl_cnt;
847 					cl->last_ipc_acked = 1;
848 					spin_lock_irqsave(
849 							&cl->tx_list_spinlock,
850 							tx_flags);
851 					if (!list_empty(&cl->tx_list.list)) {
852 						/*
853 						 * start sending the first msg
854 						 *	= the callback function
855 						 */
856 						spin_unlock_irqrestore(
857 							&cl->tx_list_spinlock,
858 							tx_flags);
859 						ishtp_cl_send_msg(dev, cl);
860 					} else {
861 						spin_unlock_irqrestore(
862 							&cl->tx_list_spinlock,
863 							tx_flags);
864 					}
865 				}
866 				break;
867 			}
868 		}
869 		spin_unlock_irqrestore(&dev->cl_list_lock, flags);
870 		goto	eoi;
871 	}
872 
873 	/*
874 	 * Some messages that are safe for ISR processing and important
875 	 * to be done "quickly" and in-order, go here
876 	 */
877 	if (ishtp_msg->hbm_cmd == CLIENT_CONNECT_RES_CMD ||
878 			ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_RES_CMD ||
879 			ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_REQ_CMD ||
880 			ishtp_msg->hbm_cmd == DMA_XFER) {
881 		ishtp_hbm_dispatch(dev, ishtp_msg);
882 		goto	eoi;
883 	}
884 
885 	/*
886 	 * All other HBMs go here.
887 	 * We schedule HBMs for processing serially by using system wq,
888 	 * possibly there will be multiple HBMs scheduled at the same time.
889 	 */
890 	spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
891 	if ((dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
892 			(RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE) ==
893 			dev->rd_msg_fifo_head) {
894 		spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
895 		dev_err(dev->devc, "BH buffer overflow, dropping HBM %u\n",
896 			(unsigned int)ishtp_msg->hbm_cmd);
897 		goto	eoi;
898 	}
899 	memcpy(dev->rd_msg_fifo + dev->rd_msg_fifo_tail, ishtp_msg,
900 		ishtp_hdr->length);
901 	dev->rd_msg_fifo_tail = (dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
902 		(RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
903 	spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
904 	schedule_work(&dev->bh_hbm_work);
905 eoi:
906 	return;
907 }
908 
909 /**
910  * recv_fixed_cl_msg() - Receive fixed client message
911  * @dev: ISHTP device instance
912  * @ishtp_hdr: received bus message
913  *
914  * Receive and process ISHTP fixed client messages (address == 0)
915  * in ISR context
916  */
917 void recv_fixed_cl_msg(struct ishtp_device *dev,
918 	struct ishtp_msg_hdr *ishtp_hdr)
919 {
920 	uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
921 
922 	dev->print_log(dev,
923 		"%s() got fixed client msg from client #%d\n",
924 		__func__, ishtp_hdr->fw_addr);
925 	dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
926 	if (ishtp_hdr->fw_addr == ISHTP_SYSTEM_STATE_CLIENT_ADDR) {
927 		struct ish_system_states_header *msg_hdr =
928 			(struct ish_system_states_header *)rd_msg_buf;
929 		if (msg_hdr->cmd == SYSTEM_STATE_SUBSCRIBE)
930 			ishtp_send_resume(dev);
931 		/* if FW request arrived here, the system is not suspended */
932 		else
933 			dev_err(dev->devc, "unknown fixed client msg [%02X]\n",
934 				msg_hdr->cmd);
935 	}
936 }
937 
938 /**
939  * fix_cl_hdr() - Initialize fixed client header
940  * @hdr: message header
941  * @length: length of message
942  * @cl_addr: Client address
943  *
944  * Initialize message header for fixed client
945  */
946 static inline void fix_cl_hdr(struct ishtp_msg_hdr *hdr, size_t length,
947 	uint8_t cl_addr)
948 {
949 	hdr->host_addr = 0;
950 	hdr->fw_addr = cl_addr;
951 	hdr->length = length;
952 	hdr->msg_complete = 1;
953 	hdr->reserved = 0;
954 }
955 
956 /*** Suspend and resume notification ***/
957 
958 static uint32_t current_state;
959 static uint32_t supported_states = 0 | SUSPEND_STATE_BIT;
960 
961 /**
962  * ishtp_send_suspend() - Send suspend message to FW
963  * @dev: ISHTP device instance
964  *
965  * Send suspend message to FW. This is useful for system freeze (non S3) case
966  */
967 void ishtp_send_suspend(struct ishtp_device *dev)
968 {
969 	struct ishtp_msg_hdr	ishtp_hdr;
970 	struct ish_system_states_status state_status_msg;
971 	const size_t len = sizeof(struct ish_system_states_status);
972 
973 	fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
974 
975 	memset(&state_status_msg, 0, len);
976 	state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
977 	state_status_msg.supported_states = supported_states;
978 	current_state |= SUSPEND_STATE_BIT;
979 	dev->print_log(dev, "%s() sends SUSPEND notification\n", __func__);
980 	state_status_msg.states_status = current_state;
981 
982 	ishtp_write_message(dev, &ishtp_hdr,
983 		(unsigned char *)&state_status_msg);
984 }
985 EXPORT_SYMBOL(ishtp_send_suspend);
986 
987 /**
988  * ishtp_send_resume() - Send resume message to FW
989  * @dev: ISHTP device instance
990  *
991  * Send resume message to FW. This is useful for system freeze (non S3) case
992  */
993 void ishtp_send_resume(struct ishtp_device *dev)
994 {
995 	struct ishtp_msg_hdr	ishtp_hdr;
996 	struct ish_system_states_status state_status_msg;
997 	const size_t len = sizeof(struct ish_system_states_status);
998 
999 	fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
1000 
1001 	memset(&state_status_msg, 0, len);
1002 	state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
1003 	state_status_msg.supported_states = supported_states;
1004 	current_state &= ~SUSPEND_STATE_BIT;
1005 	dev->print_log(dev, "%s() sends RESUME notification\n", __func__);
1006 	state_status_msg.states_status = current_state;
1007 
1008 	ishtp_write_message(dev, &ishtp_hdr,
1009 		(unsigned char *)&state_status_msg);
1010 }
1011 EXPORT_SYMBOL(ishtp_send_resume);
1012 
1013 /**
1014  * ishtp_query_subscribers() - Send query subscribers message
1015  * @dev: ISHTP device instance
1016  *
1017  * Send message to query subscribers
1018  */
1019 void ishtp_query_subscribers(struct ishtp_device *dev)
1020 {
1021 	struct ishtp_msg_hdr	ishtp_hdr;
1022 	struct ish_system_states_query_subscribers query_subscribers_msg;
1023 	const size_t len = sizeof(struct ish_system_states_query_subscribers);
1024 
1025 	fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
1026 
1027 	memset(&query_subscribers_msg, 0, len);
1028 	query_subscribers_msg.hdr.cmd = SYSTEM_STATE_QUERY_SUBSCRIBERS;
1029 
1030 	ishtp_write_message(dev, &ishtp_hdr,
1031 		(unsigned char *)&query_subscribers_msg);
1032 }
1033