2 * ISHTP bus layer messages handling
4 * Copyright (c) 2003-2016, Intel Corporation.
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.
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
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 "ishtp-dev.h"
27 * ishtp_hbm_fw_cl_allocate() - Allocate FW clients
28 * @dev: ISHTP device instance
30 * Allocates storage for fw clients
32 static void ishtp_hbm_fw_cl_allocate(struct ishtp_device *dev)
34 struct ishtp_fw_client *clients;
37 /* count how many ISH clients we have */
38 for_each_set_bit(b, dev->fw_clients_map, ISHTP_CLIENTS_MAX)
39 dev->fw_clients_num++;
41 if (dev->fw_clients_num <= 0)
44 /* allocate storage for fw clients representation */
45 clients = kcalloc(dev->fw_clients_num, sizeof(struct ishtp_fw_client),
48 dev->dev_state = ISHTP_DEV_RESETTING;
52 dev->fw_clients = clients;
56 * ishtp_hbm_cl_hdr() - construct client hbm header
58 * @hbm_cmd: host bus message command
59 * @buf: buffer for cl header
62 * Initialize HBM buffer
64 static inline void ishtp_hbm_cl_hdr(struct ishtp_cl *cl, uint8_t hbm_cmd,
65 void *buf, size_t len)
67 struct ishtp_hbm_cl_cmd *cmd = buf;
71 cmd->hbm_cmd = hbm_cmd;
72 cmd->host_addr = cl->host_client_id;
73 cmd->fw_addr = cl->fw_client_id;
77 * ishtp_hbm_cl_addr_equal() - Compare client address
79 * @buf: Client command buffer
81 * Compare client address with the address in command buffer
83 * Return: True if they have the same address
85 static inline bool ishtp_hbm_cl_addr_equal(struct ishtp_cl *cl, void *buf)
87 struct ishtp_hbm_cl_cmd *cmd = buf;
89 return cl->host_client_id == cmd->host_addr &&
90 cl->fw_client_id == cmd->fw_addr;
94 * ishtp_hbm_start_wait() - Wait for HBM start message
95 * @dev: ISHTP device instance
97 * Wait for HBM start message from firmware
99 * Return: 0 if HBM start is/was received else timeout error
101 int ishtp_hbm_start_wait(struct ishtp_device *dev)
105 if (dev->hbm_state > ISHTP_HBM_START)
108 dev_dbg(dev->devc, "Going to wait for ishtp start. hbm_state=%08X\n",
110 ret = wait_event_interruptible_timeout(dev->wait_hbm_recvd_msg,
111 dev->hbm_state >= ISHTP_HBM_STARTED,
112 (ISHTP_INTEROP_TIMEOUT * HZ));
115 "Woke up from waiting for ishtp start. hbm_state=%08X\n",
118 if (ret <= 0 && (dev->hbm_state <= ISHTP_HBM_START)) {
119 dev->hbm_state = ISHTP_HBM_IDLE;
121 "waiting for ishtp start failed. ret=%d hbm_state=%08X\n",
122 ret, dev->hbm_state);
129 * ishtp_hbm_start_req() - Send HBM start message
130 * @dev: ISHTP device instance
132 * Send HBM start message to firmware
134 * Return: 0 if success else error code
136 int ishtp_hbm_start_req(struct ishtp_device *dev)
138 struct ishtp_msg_hdr hdr;
139 unsigned char data[128];
140 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
141 struct hbm_host_version_request *start_req;
142 const size_t len = sizeof(struct hbm_host_version_request);
144 ishtp_hbm_hdr(ishtp_hdr, len);
146 /* host start message */
147 start_req = (struct hbm_host_version_request *)data;
148 memset(start_req, 0, len);
149 start_req->hbm_cmd = HOST_START_REQ_CMD;
150 start_req->host_version.major_version = HBM_MAJOR_VERSION;
151 start_req->host_version.minor_version = HBM_MINOR_VERSION;
154 * (!) Response to HBM start may be so quick that this thread would get
155 * preempted BEFORE managing to set hbm_state = ISHTP_HBM_START.
156 * So set it at first, change back to ISHTP_HBM_IDLE upon failure
158 dev->hbm_state = ISHTP_HBM_START;
159 if (ishtp_write_message(dev, ishtp_hdr, data)) {
160 dev_err(dev->devc, "version message send failed\n");
161 dev->dev_state = ISHTP_DEV_RESETTING;
162 dev->hbm_state = ISHTP_HBM_IDLE;
171 * ishtp_hbm_enum_clients_req() - Send client enum req
172 * @dev: ISHTP device instance
174 * Send enumeration client request message
176 * Return: 0 if success else error code
178 void ishtp_hbm_enum_clients_req(struct ishtp_device *dev)
180 struct ishtp_msg_hdr hdr;
181 unsigned char data[128];
182 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
183 struct hbm_host_enum_request *enum_req;
184 const size_t len = sizeof(struct hbm_host_enum_request);
186 /* enumerate clients */
187 ishtp_hbm_hdr(ishtp_hdr, len);
189 enum_req = (struct hbm_host_enum_request *)data;
190 memset(enum_req, 0, len);
191 enum_req->hbm_cmd = HOST_ENUM_REQ_CMD;
193 if (ishtp_write_message(dev, ishtp_hdr, data)) {
194 dev->dev_state = ISHTP_DEV_RESETTING;
195 dev_err(dev->devc, "enumeration request send failed\n");
198 dev->hbm_state = ISHTP_HBM_ENUM_CLIENTS;
202 * ishtp_hbm_prop_req() - Request property
203 * @dev: ISHTP device instance
205 * Request property for a single client
207 * Return: 0 if success else error code
209 static int ishtp_hbm_prop_req(struct ishtp_device *dev)
212 struct ishtp_msg_hdr hdr;
213 unsigned char data[128];
214 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
215 struct hbm_props_request *prop_req;
216 const size_t len = sizeof(struct hbm_props_request);
217 unsigned long next_client_index;
220 client_num = dev->fw_client_presentation_num;
222 next_client_index = find_next_bit(dev->fw_clients_map,
223 ISHTP_CLIENTS_MAX, dev->fw_client_index);
225 /* We got all client properties */
226 if (next_client_index == ISHTP_CLIENTS_MAX) {
227 dev->hbm_state = ISHTP_HBM_WORKING;
228 dev->dev_state = ISHTP_DEV_ENABLED;
230 for (dev->fw_client_presentation_num = 1;
231 dev->fw_client_presentation_num < client_num + 1;
232 ++dev->fw_client_presentation_num)
233 /* Add new client device */
234 ishtp_bus_new_client(dev);
238 dev->fw_clients[client_num].client_id = next_client_index;
240 ishtp_hbm_hdr(ishtp_hdr, len);
241 prop_req = (struct hbm_props_request *)data;
243 memset(prop_req, 0, sizeof(struct hbm_props_request));
245 prop_req->hbm_cmd = HOST_CLIENT_PROPERTIES_REQ_CMD;
246 prop_req->address = next_client_index;
248 if (ishtp_write_message(dev, ishtp_hdr, data)) {
249 dev->dev_state = ISHTP_DEV_RESETTING;
250 dev_err(dev->devc, "properties request send failed\n");
255 dev->fw_client_index = next_client_index;
261 * ishtp_hbm_stop_req() - Send HBM stop
262 * @dev: ISHTP device instance
264 * Send stop request message
266 static void ishtp_hbm_stop_req(struct ishtp_device *dev)
268 struct ishtp_msg_hdr hdr;
269 unsigned char data[128];
270 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
271 struct hbm_host_stop_request *req;
272 const size_t len = sizeof(struct hbm_host_stop_request);
274 ishtp_hbm_hdr(ishtp_hdr, len);
275 req = (struct hbm_host_stop_request *)data;
277 memset(req, 0, sizeof(struct hbm_host_stop_request));
278 req->hbm_cmd = HOST_STOP_REQ_CMD;
279 req->reason = DRIVER_STOP_REQUEST;
281 ishtp_write_message(dev, ishtp_hdr, data);
285 * ishtp_hbm_cl_flow_control_req() - Send flow control request
286 * @dev: ISHTP device instance
287 * @cl: ISHTP client instance
289 * Send flow control request
291 * Return: 0 if success else error code
293 int ishtp_hbm_cl_flow_control_req(struct ishtp_device *dev,
296 struct ishtp_msg_hdr hdr;
297 unsigned char data[128];
298 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
299 const size_t len = sizeof(struct hbm_flow_control);
303 spin_lock_irqsave(&cl->fc_spinlock, flags);
304 ishtp_hbm_hdr(ishtp_hdr, len);
305 ishtp_hbm_cl_hdr(cl, ISHTP_FLOW_CONTROL_CMD, data, len);
308 * Sync possible race when RB recycle and packet receive paths
309 * both try to send an out FC
311 if (cl->out_flow_ctrl_creds) {
312 spin_unlock_irqrestore(&cl->fc_spinlock, flags);
316 cl->recv_msg_num_frags = 0;
318 rv = ishtp_write_message(dev, ishtp_hdr, data);
320 ++cl->out_flow_ctrl_creds;
321 ++cl->out_flow_ctrl_cnt;
322 cl->ts_out_fc = ktime_get();
324 ktime_t ts_diff = ktime_sub(cl->ts_out_fc, cl->ts_rx);
325 if (ktime_after(ts_diff, cl->ts_max_fc_delay))
326 cl->ts_max_fc_delay = ts_diff;
332 spin_unlock_irqrestore(&cl->fc_spinlock, flags);
337 * ishtp_hbm_cl_disconnect_req() - Send disconnect request
338 * @dev: ISHTP device instance
339 * @cl: ISHTP client instance
341 * Send disconnect message to fw
343 * Return: 0 if success else error code
345 int ishtp_hbm_cl_disconnect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
347 struct ishtp_msg_hdr hdr;
348 unsigned char data[128];
349 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
350 const size_t len = sizeof(struct hbm_client_connect_request);
352 ishtp_hbm_hdr(ishtp_hdr, len);
353 ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, data, len);
355 return ishtp_write_message(dev, ishtp_hdr, data);
359 * ishtp_hbm_cl_disconnect_res() - Get disconnect response
360 * @dev: ISHTP device instance
361 * @rs: Response message
363 * Received disconnect response from fw
365 static void ishtp_hbm_cl_disconnect_res(struct ishtp_device *dev,
366 struct hbm_client_connect_response *rs)
368 struct ishtp_cl *cl = NULL;
371 spin_lock_irqsave(&dev->cl_list_lock, flags);
372 list_for_each_entry(cl, &dev->cl_list, link) {
373 if (!rs->status && ishtp_hbm_cl_addr_equal(cl, rs)) {
374 cl->state = ISHTP_CL_DISCONNECTED;
375 wake_up_interruptible(&cl->wait_ctrl_res);
379 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
383 * ishtp_hbm_cl_connect_req() - Send connect request
384 * @dev: ISHTP device instance
385 * @cl: client device instance
387 * Send connection request to specific fw client
389 * Return: 0 if success else error code
391 int ishtp_hbm_cl_connect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
393 struct ishtp_msg_hdr hdr;
394 unsigned char data[128];
395 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
396 const size_t len = sizeof(struct hbm_client_connect_request);
398 ishtp_hbm_hdr(ishtp_hdr, len);
399 ishtp_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, data, len);
401 return ishtp_write_message(dev, ishtp_hdr, data);
405 * ishtp_hbm_cl_connect_res() - Get connect response
406 * @dev: ISHTP device instance
407 * @rs: Response message
409 * Received connect response from fw
411 static void ishtp_hbm_cl_connect_res(struct ishtp_device *dev,
412 struct hbm_client_connect_response *rs)
414 struct ishtp_cl *cl = NULL;
417 spin_lock_irqsave(&dev->cl_list_lock, flags);
418 list_for_each_entry(cl, &dev->cl_list, link) {
419 if (ishtp_hbm_cl_addr_equal(cl, rs)) {
421 cl->state = ISHTP_CL_CONNECTED;
424 cl->state = ISHTP_CL_DISCONNECTED;
425 cl->status = -ENODEV;
427 wake_up_interruptible(&cl->wait_ctrl_res);
431 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
435 * ishtp_client_disconnect_request() - Receive disconnect request
436 * @dev: ISHTP device instance
437 * @disconnect_req: disconnect request structure
439 * Disconnect request bus message from the fw. Send diconnect response.
441 static void ishtp_hbm_fw_disconnect_req(struct ishtp_device *dev,
442 struct hbm_client_connect_request *disconnect_req)
445 const size_t len = sizeof(struct hbm_client_connect_response);
447 struct ishtp_msg_hdr hdr;
448 unsigned char data[4]; /* All HBM messages are 4 bytes */
450 spin_lock_irqsave(&dev->cl_list_lock, flags);
451 list_for_each_entry(cl, &dev->cl_list, link) {
452 if (ishtp_hbm_cl_addr_equal(cl, disconnect_req)) {
453 cl->state = ISHTP_CL_DISCONNECTED;
455 /* send disconnect response */
456 ishtp_hbm_hdr(&hdr, len);
457 ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, data,
459 ishtp_write_message(dev, &hdr, data);
463 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
467 * ishtp_hbm_dma_xfer_ack(() - Receive transfer ACK
468 * @dev: ISHTP device instance
469 * @dma_xfer: HBM transfer message
471 * Receive ack for ISHTP-over-DMA client message
473 static void ishtp_hbm_dma_xfer_ack(struct ishtp_device *dev,
474 struct dma_xfer_hbm *dma_xfer)
478 struct ishtp_msg_hdr *ishtp_hdr =
479 (struct ishtp_msg_hdr *)&dev->ishtp_msg_hdr;
480 unsigned int msg_offs;
483 for (msg_offs = 0; msg_offs < ishtp_hdr->length;
484 msg_offs += sizeof(struct dma_xfer_hbm)) {
485 offs = dma_xfer->msg_addr - dev->ishtp_host_dma_tx_buf_phys;
486 if (offs > dev->ishtp_host_dma_tx_buf_size) {
487 dev_err(dev->devc, "Bad DMA Tx ack message address\n");
490 if (dma_xfer->msg_length >
491 dev->ishtp_host_dma_tx_buf_size - offs) {
492 dev_err(dev->devc, "Bad DMA Tx ack message size\n");
496 /* logical address of the acked mem */
497 msg = (unsigned char *)dev->ishtp_host_dma_tx_buf + offs;
498 ishtp_cl_release_dma_acked_mem(dev, msg, dma_xfer->msg_length);
500 list_for_each_entry(cl, &dev->cl_list, link) {
501 if (cl->fw_client_id == dma_xfer->fw_client_id &&
502 cl->host_client_id == dma_xfer->host_client_id)
504 * in case that a single ack may be sent
505 * over several dma transfers, and the last msg
506 * addr was inside the acked memory, but not in
509 if (cl->last_dma_addr >=
510 (unsigned char *)msg &&
512 (unsigned char *)msg +
513 dma_xfer->msg_length) {
514 cl->last_dma_acked = 1;
516 if (!list_empty(&cl->tx_list.list) &&
517 cl->ishtp_flow_ctrl_creds) {
519 * start sending the first msg
521 ishtp_cl_send_msg(dev, cl);
530 * ishtp_hbm_dma_xfer() - Receive DMA transfer message
531 * @dev: ISHTP device instance
532 * @dma_xfer: HBM transfer message
534 * Receive ISHTP-over-DMA client message
536 static void ishtp_hbm_dma_xfer(struct ishtp_device *dev,
537 struct dma_xfer_hbm *dma_xfer)
541 struct ishtp_msg_hdr hdr;
542 struct ishtp_msg_hdr *ishtp_hdr =
543 (struct ishtp_msg_hdr *) &dev->ishtp_msg_hdr;
544 struct dma_xfer_hbm *prm = dma_xfer;
545 unsigned int msg_offs;
547 for (msg_offs = 0; msg_offs < ishtp_hdr->length;
548 msg_offs += sizeof(struct dma_xfer_hbm)) {
550 offs = dma_xfer->msg_addr - dev->ishtp_host_dma_rx_buf_phys;
551 if (offs > dev->ishtp_host_dma_rx_buf_size) {
552 dev_err(dev->devc, "Bad DMA Rx message address\n");
555 if (dma_xfer->msg_length >
556 dev->ishtp_host_dma_rx_buf_size - offs) {
557 dev_err(dev->devc, "Bad DMA Rx message size\n");
560 msg = dev->ishtp_host_dma_rx_buf + offs;
561 recv_ishtp_cl_msg_dma(dev, msg, dma_xfer);
562 dma_xfer->hbm = DMA_XFER_ACK; /* Prepare for response */
566 /* Send DMA_XFER_ACK [...] */
567 ishtp_hbm_hdr(&hdr, ishtp_hdr->length);
568 ishtp_write_message(dev, &hdr, (unsigned char *)prm);
572 * ishtp_hbm_dispatch() - HBM dispatch function
573 * @dev: ISHTP device instance
576 * Bottom half read routine after ISR to handle the read bus message cmd
579 void ishtp_hbm_dispatch(struct ishtp_device *dev,
580 struct ishtp_bus_message *hdr)
582 struct ishtp_bus_message *ishtp_msg;
583 struct ishtp_fw_client *fw_client;
584 struct hbm_host_version_response *version_res;
585 struct hbm_client_connect_response *connect_res;
586 struct hbm_client_connect_response *disconnect_res;
587 struct hbm_client_connect_request *disconnect_req;
588 struct hbm_props_response *props_res;
589 struct hbm_host_enum_response *enum_res;
590 struct ishtp_msg_hdr ishtp_hdr;
591 struct dma_alloc_notify dma_alloc_notify;
592 struct dma_xfer_hbm *dma_xfer;
596 switch (ishtp_msg->hbm_cmd) {
597 case HOST_START_RES_CMD:
598 version_res = (struct hbm_host_version_response *)ishtp_msg;
599 if (!version_res->host_version_supported) {
600 dev->version = version_res->fw_max_version;
602 dev->hbm_state = ISHTP_HBM_STOPPED;
603 ishtp_hbm_stop_req(dev);
607 dev->version.major_version = HBM_MAJOR_VERSION;
608 dev->version.minor_version = HBM_MINOR_VERSION;
609 if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
610 dev->hbm_state == ISHTP_HBM_START) {
611 dev->hbm_state = ISHTP_HBM_STARTED;
612 ishtp_hbm_enum_clients_req(dev);
615 "reset: wrong host start response\n");
616 /* BUG: why do we arrive here? */
621 wake_up_interruptible(&dev->wait_hbm_recvd_msg);
624 case CLIENT_CONNECT_RES_CMD:
625 connect_res = (struct hbm_client_connect_response *)ishtp_msg;
626 ishtp_hbm_cl_connect_res(dev, connect_res);
629 case CLIENT_DISCONNECT_RES_CMD:
631 (struct hbm_client_connect_response *)ishtp_msg;
632 ishtp_hbm_cl_disconnect_res(dev, disconnect_res);
635 case HOST_CLIENT_PROPERTIES_RES_CMD:
636 props_res = (struct hbm_props_response *)ishtp_msg;
637 fw_client = &dev->fw_clients[dev->fw_client_presentation_num];
639 if (props_res->status || !dev->fw_clients) {
641 "reset: properties response hbm wrong status\n");
646 if (fw_client->client_id != props_res->address) {
648 "reset: host properties response address mismatch [%02X %02X]\n",
649 fw_client->client_id, props_res->address);
654 if (dev->dev_state != ISHTP_DEV_INIT_CLIENTS ||
655 dev->hbm_state != ISHTP_HBM_CLIENT_PROPERTIES) {
657 "reset: unexpected properties response\n");
662 fw_client->props = props_res->client_properties;
663 dev->fw_client_index++;
664 dev->fw_client_presentation_num++;
666 /* request property for the next client */
667 ishtp_hbm_prop_req(dev);
669 if (dev->dev_state != ISHTP_DEV_ENABLED)
672 if (!ishtp_use_dma_transfer())
675 dev_dbg(dev->devc, "Requesting to use DMA\n");
676 ishtp_cl_alloc_dma_buf(dev);
677 if (dev->ishtp_host_dma_rx_buf) {
678 const size_t len = sizeof(dma_alloc_notify);
680 memset(&dma_alloc_notify, 0, sizeof(dma_alloc_notify));
681 dma_alloc_notify.hbm = DMA_BUFFER_ALLOC_NOTIFY;
682 dma_alloc_notify.buf_size =
683 dev->ishtp_host_dma_rx_buf_size;
684 dma_alloc_notify.buf_address =
685 dev->ishtp_host_dma_rx_buf_phys;
686 ishtp_hbm_hdr(&ishtp_hdr, len);
687 ishtp_write_message(dev, &ishtp_hdr,
688 (unsigned char *)&dma_alloc_notify);
693 case HOST_ENUM_RES_CMD:
694 enum_res = (struct hbm_host_enum_response *) ishtp_msg;
695 memcpy(dev->fw_clients_map, enum_res->valid_addresses, 32);
696 if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
697 dev->hbm_state == ISHTP_HBM_ENUM_CLIENTS) {
698 dev->fw_client_presentation_num = 0;
699 dev->fw_client_index = 0;
701 ishtp_hbm_fw_cl_allocate(dev);
702 dev->hbm_state = ISHTP_HBM_CLIENT_PROPERTIES;
704 /* first property request */
705 ishtp_hbm_prop_req(dev);
708 "reset: unexpected enumeration response hbm\n");
714 case HOST_STOP_RES_CMD:
715 if (dev->hbm_state != ISHTP_HBM_STOPPED)
716 dev_err(dev->devc, "unexpected stop response\n");
718 dev->dev_state = ISHTP_DEV_DISABLED;
719 dev_info(dev->devc, "reset: FW stop response\n");
723 case CLIENT_DISCONNECT_REQ_CMD:
724 /* search for client */
726 (struct hbm_client_connect_request *)ishtp_msg;
727 ishtp_hbm_fw_disconnect_req(dev, disconnect_req);
730 case FW_STOP_REQ_CMD:
731 dev->hbm_state = ISHTP_HBM_STOPPED;
734 case DMA_BUFFER_ALLOC_RESPONSE:
735 dev->ishtp_host_dma_enabled = 1;
739 dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
740 if (!dev->ishtp_host_dma_enabled) {
742 "DMA XFER requested but DMA is not enabled\n");
745 ishtp_hbm_dma_xfer(dev, dma_xfer);
749 dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
750 if (!dev->ishtp_host_dma_enabled ||
751 !dev->ishtp_host_dma_tx_buf) {
753 "DMA XFER acked but DMA Tx is not enabled\n");
756 ishtp_hbm_dma_xfer_ack(dev, dma_xfer);
760 dev_err(dev->devc, "unknown HBM: %u\n",
761 (unsigned int)ishtp_msg->hbm_cmd);
768 * bh_hbm_work_fn() - HBM work function
771 * Bottom half processing work function (instead of thread handler)
772 * for processing hbm messages
774 void bh_hbm_work_fn(struct work_struct *work)
777 struct ishtp_device *dev;
778 unsigned char hbm[IPC_PAYLOAD_SIZE];
780 dev = container_of(work, struct ishtp_device, bh_hbm_work);
781 spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
782 if (dev->rd_msg_fifo_head != dev->rd_msg_fifo_tail) {
783 memcpy(hbm, dev->rd_msg_fifo + dev->rd_msg_fifo_head,
785 dev->rd_msg_fifo_head =
786 (dev->rd_msg_fifo_head + IPC_PAYLOAD_SIZE) %
787 (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
788 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
789 ishtp_hbm_dispatch(dev, (struct ishtp_bus_message *)hbm);
791 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
796 * recv_hbm() - Receive HBM message
797 * @dev: ISHTP device instance
798 * @ishtp_hdr: received bus message
800 * Receive and process ISHTP bus messages in ISR context. This will schedule
801 * work function to process message
803 void recv_hbm(struct ishtp_device *dev, struct ishtp_msg_hdr *ishtp_hdr)
805 uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
806 struct ishtp_bus_message *ishtp_msg =
807 (struct ishtp_bus_message *)rd_msg_buf;
810 dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
812 /* Flow control - handle in place */
813 if (ishtp_msg->hbm_cmd == ISHTP_FLOW_CONTROL_CMD) {
814 struct hbm_flow_control *flow_control =
815 (struct hbm_flow_control *)ishtp_msg;
816 struct ishtp_cl *cl = NULL;
817 unsigned long flags, tx_flags;
819 spin_lock_irqsave(&dev->cl_list_lock, flags);
820 list_for_each_entry(cl, &dev->cl_list, link) {
821 if (cl->host_client_id == flow_control->host_addr &&
823 flow_control->fw_addr) {
825 * NOTE: It's valid only for counting
826 * flow-control implementation to receive a
827 * FC in the middle of sending. Meanwhile not
830 if (cl->ishtp_flow_ctrl_creds)
832 "recv extra FC from FW client %u (host client %u) (FC count was %d)\n",
833 (unsigned int)cl->fw_client_id,
834 (unsigned int)cl->host_client_id,
835 cl->ishtp_flow_ctrl_creds);
837 ++cl->ishtp_flow_ctrl_creds;
838 ++cl->ishtp_flow_ctrl_cnt;
839 cl->last_ipc_acked = 1;
841 &cl->tx_list_spinlock,
843 if (!list_empty(&cl->tx_list.list)) {
845 * start sending the first msg
846 * = the callback function
848 spin_unlock_irqrestore(
849 &cl->tx_list_spinlock,
851 ishtp_cl_send_msg(dev, cl);
853 spin_unlock_irqrestore(
854 &cl->tx_list_spinlock,
861 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
866 * Some messages that are safe for ISR processing and important
867 * to be done "quickly" and in-order, go here
869 if (ishtp_msg->hbm_cmd == CLIENT_CONNECT_RES_CMD ||
870 ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_RES_CMD ||
871 ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_REQ_CMD ||
872 ishtp_msg->hbm_cmd == DMA_XFER) {
873 ishtp_hbm_dispatch(dev, ishtp_msg);
878 * All other HBMs go here.
879 * We schedule HBMs for processing serially by using system wq,
880 * possibly there will be multiple HBMs scheduled at the same time.
882 spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
883 if ((dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
884 (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE) ==
885 dev->rd_msg_fifo_head) {
886 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
887 dev_err(dev->devc, "BH buffer overflow, dropping HBM %u\n",
888 (unsigned int)ishtp_msg->hbm_cmd);
891 memcpy(dev->rd_msg_fifo + dev->rd_msg_fifo_tail, ishtp_msg,
893 dev->rd_msg_fifo_tail = (dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
894 (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
895 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
896 schedule_work(&dev->bh_hbm_work);
902 * recv_fixed_cl_msg() - Receive fixed client message
903 * @dev: ISHTP device instance
904 * @ishtp_hdr: received bus message
906 * Receive and process ISHTP fixed client messages (address == 0)
909 void recv_fixed_cl_msg(struct ishtp_device *dev,
910 struct ishtp_msg_hdr *ishtp_hdr)
912 uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
915 "%s() got fixed client msg from client #%d\n",
916 __func__, ishtp_hdr->fw_addr);
917 dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
918 if (ishtp_hdr->fw_addr == ISHTP_SYSTEM_STATE_CLIENT_ADDR) {
919 struct ish_system_states_header *msg_hdr =
920 (struct ish_system_states_header *)rd_msg_buf;
921 if (msg_hdr->cmd == SYSTEM_STATE_SUBSCRIBE)
922 ishtp_send_resume(dev);
923 /* if FW request arrived here, the system is not suspended */
925 dev_err(dev->devc, "unknown fixed client msg [%02X]\n",
931 * fix_cl_hdr() - Initialize fixed client header
932 * @hdr: message header
933 * @length: length of message
934 * @cl_addr: Client address
936 * Initialize message header for fixed client
938 static inline void fix_cl_hdr(struct ishtp_msg_hdr *hdr, size_t length,
942 hdr->fw_addr = cl_addr;
943 hdr->length = length;
944 hdr->msg_complete = 1;
948 /*** Suspend and resume notification ***/
950 static uint32_t current_state;
951 static uint32_t supported_states = 0 | SUSPEND_STATE_BIT;
954 * ishtp_send_suspend() - Send suspend message to FW
955 * @dev: ISHTP device instance
957 * Send suspend message to FW. This is useful for system freeze (non S3) case
959 void ishtp_send_suspend(struct ishtp_device *dev)
961 struct ishtp_msg_hdr ishtp_hdr;
962 struct ish_system_states_status state_status_msg;
963 const size_t len = sizeof(struct ish_system_states_status);
965 fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
967 memset(&state_status_msg, 0, len);
968 state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
969 state_status_msg.supported_states = supported_states;
970 current_state |= SUSPEND_STATE_BIT;
971 dev->print_log(dev, "%s() sends SUSPEND notification\n", __func__);
972 state_status_msg.states_status = current_state;
974 ishtp_write_message(dev, &ishtp_hdr,
975 (unsigned char *)&state_status_msg);
977 EXPORT_SYMBOL(ishtp_send_suspend);
980 * ishtp_send_resume() - Send resume message to FW
981 * @dev: ISHTP device instance
983 * Send resume message to FW. This is useful for system freeze (non S3) case
985 void ishtp_send_resume(struct ishtp_device *dev)
987 struct ishtp_msg_hdr ishtp_hdr;
988 struct ish_system_states_status state_status_msg;
989 const size_t len = sizeof(struct ish_system_states_status);
991 fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
993 memset(&state_status_msg, 0, len);
994 state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
995 state_status_msg.supported_states = supported_states;
996 current_state &= ~SUSPEND_STATE_BIT;
997 dev->print_log(dev, "%s() sends RESUME notification\n", __func__);
998 state_status_msg.states_status = current_state;
1000 ishtp_write_message(dev, &ishtp_hdr,
1001 (unsigned char *)&state_status_msg);
1003 EXPORT_SYMBOL(ishtp_send_resume);
1006 * ishtp_query_subscribers() - Send query subscribers message
1007 * @dev: ISHTP device instance
1009 * Send message to query subscribers
1011 void ishtp_query_subscribers(struct ishtp_device *dev)
1013 struct ishtp_msg_hdr ishtp_hdr;
1014 struct ish_system_states_query_subscribers query_subscribers_msg;
1015 const size_t len = sizeof(struct ish_system_states_query_subscribers);
1017 fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
1019 memset(&query_subscribers_msg, 0, len);
1020 query_subscribers_msg.hdr.cmd = SYSTEM_STATE_QUERY_SUBSCRIBERS;
1022 ishtp_write_message(dev, &ishtp_hdr,
1023 (unsigned char *)&query_subscribers_msg);