x86/unwinder: Handle stack overflows more gracefully
[linux/fpc-iii.git] / drivers / hid / intel-ish-hid / ishtp / hbm.c
blobae4a69f7f2f48d23b50aa639ac69cc68b04d8f3a
1 /*
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
13 * more details.
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"
23 #include "hbm.h"
24 #include "client.h"
26 /**
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;
35 int b;
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)
42 return;
44 /* allocate storage for fw clients representation */
45 clients = kcalloc(dev->fw_clients_num, sizeof(struct ishtp_fw_client),
46 GFP_KERNEL);
47 if (!clients) {
48 dev->dev_state = ISHTP_DEV_RESETTING;
49 ish_hw_reset(dev);
50 return;
52 dev->fw_clients = clients;
55 /**
56 * ishtp_hbm_cl_hdr() - construct client hbm header
57 * @cl: client
58 * @hbm_cmd: host bus message command
59 * @buf: buffer for cl header
60 * @len: buffer length
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;
69 memset(cmd, 0, len);
71 cmd->hbm_cmd = hbm_cmd;
72 cmd->host_addr = cl->host_client_id;
73 cmd->fw_addr = cl->fw_client_id;
76 /**
77 * ishtp_hbm_cl_addr_equal() - Compare client address
78 * @cl: client
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;
93 /**
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)
103 int ret;
105 if (dev->hbm_state > ISHTP_HBM_START)
106 return 0;
108 dev_dbg(dev->devc, "Going to wait for ishtp start. hbm_state=%08X\n",
109 dev->hbm_state);
110 ret = wait_event_interruptible_timeout(dev->wait_hbm_recvd_msg,
111 dev->hbm_state >= ISHTP_HBM_STARTED,
112 (ISHTP_INTEROP_TIMEOUT * HZ));
114 dev_dbg(dev->devc,
115 "Woke up from waiting for ishtp start. hbm_state=%08X\n",
116 dev->hbm_state);
118 if (ret <= 0 && (dev->hbm_state <= ISHTP_HBM_START)) {
119 dev->hbm_state = ISHTP_HBM_IDLE;
120 dev_err(dev->devc,
121 "waiting for ishtp start failed. ret=%d hbm_state=%08X\n",
122 ret, dev->hbm_state);
123 return -ETIMEDOUT;
125 return 0;
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;
163 ish_hw_reset(dev);
164 return -ENODEV;
167 return 0;
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");
196 ish_hw_reset(dev);
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;
218 uint8_t client_num;
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);
235 return 0;
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");
251 ish_hw_reset(dev);
252 return -EIO;
255 dev->fw_client_index = next_client_index;
257 return 0;
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,
294 struct ishtp_cl *cl)
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);
300 int rv;
301 unsigned int num_frags;
302 unsigned long flags;
304 spin_lock_irqsave(&cl->fc_spinlock, flags);
305 ishtp_hbm_hdr(ishtp_hdr, len);
306 ishtp_hbm_cl_hdr(cl, ISHTP_FLOW_CONTROL_CMD, data, len);
309 * Sync possible race when RB recycle and packet receive paths
310 * both try to send an out FC
312 if (cl->out_flow_ctrl_creds) {
313 spin_unlock_irqrestore(&cl->fc_spinlock, flags);
314 return 0;
317 num_frags = cl->recv_msg_num_frags;
318 cl->recv_msg_num_frags = 0;
320 rv = ishtp_write_message(dev, ishtp_hdr, data);
321 if (!rv) {
322 ++cl->out_flow_ctrl_creds;
323 ++cl->out_flow_ctrl_cnt;
324 cl->ts_out_fc = ktime_get();
325 if (cl->ts_rx) {
326 ktime_t ts_diff = ktime_sub(cl->ts_out_fc, cl->ts_rx);
327 if (ktime_after(ts_diff, cl->ts_max_fc_delay))
328 cl->ts_max_fc_delay = ts_diff;
330 } else {
331 ++cl->err_send_fc;
334 spin_unlock_irqrestore(&cl->fc_spinlock, flags);
335 return rv;
339 * ishtp_hbm_cl_disconnect_req() - Send disconnect request
340 * @dev: ISHTP device instance
341 * @cl: ISHTP client instance
343 * Send disconnect message to fw
345 * Return: 0 if success else error code
347 int ishtp_hbm_cl_disconnect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
349 struct ishtp_msg_hdr hdr;
350 unsigned char data[128];
351 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
352 const size_t len = sizeof(struct hbm_client_connect_request);
354 ishtp_hbm_hdr(ishtp_hdr, len);
355 ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_REQ_CMD, data, len);
357 return ishtp_write_message(dev, ishtp_hdr, data);
361 * ishtp_hbm_cl_disconnect_res() - Get disconnect response
362 * @dev: ISHTP device instance
363 * @rs: Response message
365 * Received disconnect response from fw
367 static void ishtp_hbm_cl_disconnect_res(struct ishtp_device *dev,
368 struct hbm_client_connect_response *rs)
370 struct ishtp_cl *cl = NULL;
371 unsigned long flags;
373 spin_lock_irqsave(&dev->cl_list_lock, flags);
374 list_for_each_entry(cl, &dev->cl_list, link) {
375 if (!rs->status && ishtp_hbm_cl_addr_equal(cl, rs)) {
376 cl->state = ISHTP_CL_DISCONNECTED;
377 wake_up_interruptible(&cl->wait_ctrl_res);
378 break;
381 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
385 * ishtp_hbm_cl_connect_req() - Send connect request
386 * @dev: ISHTP device instance
387 * @cl: client device instance
389 * Send connection request to specific fw client
391 * Return: 0 if success else error code
393 int ishtp_hbm_cl_connect_req(struct ishtp_device *dev, struct ishtp_cl *cl)
395 struct ishtp_msg_hdr hdr;
396 unsigned char data[128];
397 struct ishtp_msg_hdr *ishtp_hdr = &hdr;
398 const size_t len = sizeof(struct hbm_client_connect_request);
400 ishtp_hbm_hdr(ishtp_hdr, len);
401 ishtp_hbm_cl_hdr(cl, CLIENT_CONNECT_REQ_CMD, data, len);
403 return ishtp_write_message(dev, ishtp_hdr, data);
407 * ishtp_hbm_cl_connect_res() - Get connect response
408 * @dev: ISHTP device instance
409 * @rs: Response message
411 * Received connect response from fw
413 static void ishtp_hbm_cl_connect_res(struct ishtp_device *dev,
414 struct hbm_client_connect_response *rs)
416 struct ishtp_cl *cl = NULL;
417 unsigned long flags;
419 spin_lock_irqsave(&dev->cl_list_lock, flags);
420 list_for_each_entry(cl, &dev->cl_list, link) {
421 if (ishtp_hbm_cl_addr_equal(cl, rs)) {
422 if (!rs->status) {
423 cl->state = ISHTP_CL_CONNECTED;
424 cl->status = 0;
425 } else {
426 cl->state = ISHTP_CL_DISCONNECTED;
427 cl->status = -ENODEV;
429 wake_up_interruptible(&cl->wait_ctrl_res);
430 break;
433 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
437 * ishtp_client_disconnect_request() - Receive disconnect request
438 * @dev: ISHTP device instance
439 * @disconnect_req: disconnect request structure
441 * Disconnect request bus message from the fw. Send diconnect response.
443 static void ishtp_hbm_fw_disconnect_req(struct ishtp_device *dev,
444 struct hbm_client_connect_request *disconnect_req)
446 struct ishtp_cl *cl;
447 const size_t len = sizeof(struct hbm_client_connect_response);
448 unsigned long flags;
449 struct ishtp_msg_hdr hdr;
450 unsigned char data[4]; /* All HBM messages are 4 bytes */
452 spin_lock_irqsave(&dev->cl_list_lock, flags);
453 list_for_each_entry(cl, &dev->cl_list, link) {
454 if (ishtp_hbm_cl_addr_equal(cl, disconnect_req)) {
455 cl->state = ISHTP_CL_DISCONNECTED;
457 /* send disconnect response */
458 ishtp_hbm_hdr(&hdr, len);
459 ishtp_hbm_cl_hdr(cl, CLIENT_DISCONNECT_RES_CMD, data,
460 len);
461 ishtp_write_message(dev, &hdr, data);
462 break;
465 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
469 * ishtp_hbm_dma_xfer_ack(() - Receive transfer ACK
470 * @dev: ISHTP device instance
471 * @dma_xfer: HBM transfer message
473 * Receive ack for ISHTP-over-DMA client message
475 static void ishtp_hbm_dma_xfer_ack(struct ishtp_device *dev,
476 struct dma_xfer_hbm *dma_xfer)
478 void *msg;
479 uint64_t offs;
480 struct ishtp_msg_hdr *ishtp_hdr =
481 (struct ishtp_msg_hdr *)&dev->ishtp_msg_hdr;
482 unsigned int msg_offs;
483 struct ishtp_cl *cl;
485 for (msg_offs = 0; msg_offs < ishtp_hdr->length;
486 msg_offs += sizeof(struct dma_xfer_hbm)) {
487 offs = dma_xfer->msg_addr - dev->ishtp_host_dma_tx_buf_phys;
488 if (offs > dev->ishtp_host_dma_tx_buf_size) {
489 dev_err(dev->devc, "Bad DMA Tx ack message address\n");
490 return;
492 if (dma_xfer->msg_length >
493 dev->ishtp_host_dma_tx_buf_size - offs) {
494 dev_err(dev->devc, "Bad DMA Tx ack message size\n");
495 return;
498 /* logical address of the acked mem */
499 msg = (unsigned char *)dev->ishtp_host_dma_tx_buf + offs;
500 ishtp_cl_release_dma_acked_mem(dev, msg, dma_xfer->msg_length);
502 list_for_each_entry(cl, &dev->cl_list, link) {
503 if (cl->fw_client_id == dma_xfer->fw_client_id &&
504 cl->host_client_id == dma_xfer->host_client_id)
506 * in case that a single ack may be sent
507 * over several dma transfers, and the last msg
508 * addr was inside the acked memory, but not in
509 * its start
511 if (cl->last_dma_addr >=
512 (unsigned char *)msg &&
513 cl->last_dma_addr <
514 (unsigned char *)msg +
515 dma_xfer->msg_length) {
516 cl->last_dma_acked = 1;
518 if (!list_empty(&cl->tx_list.list) &&
519 cl->ishtp_flow_ctrl_creds) {
521 * start sending the first msg
523 ishtp_cl_send_msg(dev, cl);
527 ++dma_xfer;
532 * ishtp_hbm_dma_xfer() - Receive DMA transfer message
533 * @dev: ISHTP device instance
534 * @dma_xfer: HBM transfer message
536 * Receive ISHTP-over-DMA client message
538 static void ishtp_hbm_dma_xfer(struct ishtp_device *dev,
539 struct dma_xfer_hbm *dma_xfer)
541 void *msg;
542 uint64_t offs;
543 struct ishtp_msg_hdr hdr;
544 struct ishtp_msg_hdr *ishtp_hdr =
545 (struct ishtp_msg_hdr *) &dev->ishtp_msg_hdr;
546 struct dma_xfer_hbm *prm = dma_xfer;
547 unsigned int msg_offs;
549 for (msg_offs = 0; msg_offs < ishtp_hdr->length;
550 msg_offs += sizeof(struct dma_xfer_hbm)) {
552 offs = dma_xfer->msg_addr - dev->ishtp_host_dma_rx_buf_phys;
553 if (offs > dev->ishtp_host_dma_rx_buf_size) {
554 dev_err(dev->devc, "Bad DMA Rx message address\n");
555 return;
557 if (dma_xfer->msg_length >
558 dev->ishtp_host_dma_rx_buf_size - offs) {
559 dev_err(dev->devc, "Bad DMA Rx message size\n");
560 return;
562 msg = dev->ishtp_host_dma_rx_buf + offs;
563 recv_ishtp_cl_msg_dma(dev, msg, dma_xfer);
564 dma_xfer->hbm = DMA_XFER_ACK; /* Prepare for response */
565 ++dma_xfer;
568 /* Send DMA_XFER_ACK [...] */
569 ishtp_hbm_hdr(&hdr, ishtp_hdr->length);
570 ishtp_write_message(dev, &hdr, (unsigned char *)prm);
574 * ishtp_hbm_dispatch() - HBM dispatch function
575 * @dev: ISHTP device instance
576 * @hdr: bus message
578 * Bottom half read routine after ISR to handle the read bus message cmd
579 * processing
581 void ishtp_hbm_dispatch(struct ishtp_device *dev,
582 struct ishtp_bus_message *hdr)
584 struct ishtp_bus_message *ishtp_msg;
585 struct ishtp_fw_client *fw_client;
586 struct hbm_host_version_response *version_res;
587 struct hbm_client_connect_response *connect_res;
588 struct hbm_client_connect_response *disconnect_res;
589 struct hbm_client_connect_request *disconnect_req;
590 struct hbm_props_response *props_res;
591 struct hbm_host_enum_response *enum_res;
592 struct ishtp_msg_hdr ishtp_hdr;
593 struct dma_alloc_notify dma_alloc_notify;
594 struct dma_xfer_hbm *dma_xfer;
596 ishtp_msg = hdr;
598 switch (ishtp_msg->hbm_cmd) {
599 case HOST_START_RES_CMD:
600 version_res = (struct hbm_host_version_response *)ishtp_msg;
601 if (!version_res->host_version_supported) {
602 dev->version = version_res->fw_max_version;
604 dev->hbm_state = ISHTP_HBM_STOPPED;
605 ishtp_hbm_stop_req(dev);
606 return;
609 dev->version.major_version = HBM_MAJOR_VERSION;
610 dev->version.minor_version = HBM_MINOR_VERSION;
611 if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
612 dev->hbm_state == ISHTP_HBM_START) {
613 dev->hbm_state = ISHTP_HBM_STARTED;
614 ishtp_hbm_enum_clients_req(dev);
615 } else {
616 dev_err(dev->devc,
617 "reset: wrong host start response\n");
618 /* BUG: why do we arrive here? */
619 ish_hw_reset(dev);
620 return;
623 wake_up_interruptible(&dev->wait_hbm_recvd_msg);
624 break;
626 case CLIENT_CONNECT_RES_CMD:
627 connect_res = (struct hbm_client_connect_response *)ishtp_msg;
628 ishtp_hbm_cl_connect_res(dev, connect_res);
629 break;
631 case CLIENT_DISCONNECT_RES_CMD:
632 disconnect_res =
633 (struct hbm_client_connect_response *)ishtp_msg;
634 ishtp_hbm_cl_disconnect_res(dev, disconnect_res);
635 break;
637 case HOST_CLIENT_PROPERTIES_RES_CMD:
638 props_res = (struct hbm_props_response *)ishtp_msg;
639 fw_client = &dev->fw_clients[dev->fw_client_presentation_num];
641 if (props_res->status || !dev->fw_clients) {
642 dev_err(dev->devc,
643 "reset: properties response hbm wrong status\n");
644 ish_hw_reset(dev);
645 return;
648 if (fw_client->client_id != props_res->address) {
649 dev_err(dev->devc,
650 "reset: host properties response address mismatch [%02X %02X]\n",
651 fw_client->client_id, props_res->address);
652 ish_hw_reset(dev);
653 return;
656 if (dev->dev_state != ISHTP_DEV_INIT_CLIENTS ||
657 dev->hbm_state != ISHTP_HBM_CLIENT_PROPERTIES) {
658 dev_err(dev->devc,
659 "reset: unexpected properties response\n");
660 ish_hw_reset(dev);
661 return;
664 fw_client->props = props_res->client_properties;
665 dev->fw_client_index++;
666 dev->fw_client_presentation_num++;
668 /* request property for the next client */
669 ishtp_hbm_prop_req(dev);
671 if (dev->dev_state != ISHTP_DEV_ENABLED)
672 break;
674 if (!ishtp_use_dma_transfer())
675 break;
677 dev_dbg(dev->devc, "Requesting to use DMA\n");
678 ishtp_cl_alloc_dma_buf(dev);
679 if (dev->ishtp_host_dma_rx_buf) {
680 const size_t len = sizeof(dma_alloc_notify);
682 memset(&dma_alloc_notify, 0, sizeof(dma_alloc_notify));
683 dma_alloc_notify.hbm = DMA_BUFFER_ALLOC_NOTIFY;
684 dma_alloc_notify.buf_size =
685 dev->ishtp_host_dma_rx_buf_size;
686 dma_alloc_notify.buf_address =
687 dev->ishtp_host_dma_rx_buf_phys;
688 ishtp_hbm_hdr(&ishtp_hdr, len);
689 ishtp_write_message(dev, &ishtp_hdr,
690 (unsigned char *)&dma_alloc_notify);
693 break;
695 case HOST_ENUM_RES_CMD:
696 enum_res = (struct hbm_host_enum_response *) ishtp_msg;
697 memcpy(dev->fw_clients_map, enum_res->valid_addresses, 32);
698 if (dev->dev_state == ISHTP_DEV_INIT_CLIENTS &&
699 dev->hbm_state == ISHTP_HBM_ENUM_CLIENTS) {
700 dev->fw_client_presentation_num = 0;
701 dev->fw_client_index = 0;
703 ishtp_hbm_fw_cl_allocate(dev);
704 dev->hbm_state = ISHTP_HBM_CLIENT_PROPERTIES;
706 /* first property request */
707 ishtp_hbm_prop_req(dev);
708 } else {
709 dev_err(dev->devc,
710 "reset: unexpected enumeration response hbm\n");
711 ish_hw_reset(dev);
712 return;
714 break;
716 case HOST_STOP_RES_CMD:
717 if (dev->hbm_state != ISHTP_HBM_STOPPED)
718 dev_err(dev->devc, "unexpected stop response\n");
720 dev->dev_state = ISHTP_DEV_DISABLED;
721 dev_info(dev->devc, "reset: FW stop response\n");
722 ish_hw_reset(dev);
723 break;
725 case CLIENT_DISCONNECT_REQ_CMD:
726 /* search for client */
727 disconnect_req =
728 (struct hbm_client_connect_request *)ishtp_msg;
729 ishtp_hbm_fw_disconnect_req(dev, disconnect_req);
730 break;
732 case FW_STOP_REQ_CMD:
733 dev->hbm_state = ISHTP_HBM_STOPPED;
734 break;
736 case DMA_BUFFER_ALLOC_RESPONSE:
737 dev->ishtp_host_dma_enabled = 1;
738 break;
740 case DMA_XFER:
741 dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
742 if (!dev->ishtp_host_dma_enabled) {
743 dev_err(dev->devc,
744 "DMA XFER requested but DMA is not enabled\n");
745 break;
747 ishtp_hbm_dma_xfer(dev, dma_xfer);
748 break;
750 case DMA_XFER_ACK:
751 dma_xfer = (struct dma_xfer_hbm *)ishtp_msg;
752 if (!dev->ishtp_host_dma_enabled ||
753 !dev->ishtp_host_dma_tx_buf) {
754 dev_err(dev->devc,
755 "DMA XFER acked but DMA Tx is not enabled\n");
756 break;
758 ishtp_hbm_dma_xfer_ack(dev, dma_xfer);
759 break;
761 default:
762 dev_err(dev->devc, "unknown HBM: %u\n",
763 (unsigned int)ishtp_msg->hbm_cmd);
765 break;
770 * bh_hbm_work_fn() - HBM work function
771 * @work: work struct
773 * Bottom half processing work function (instead of thread handler)
774 * for processing hbm messages
776 void bh_hbm_work_fn(struct work_struct *work)
778 unsigned long flags;
779 struct ishtp_device *dev;
780 unsigned char hbm[IPC_PAYLOAD_SIZE];
782 dev = container_of(work, struct ishtp_device, bh_hbm_work);
783 spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
784 if (dev->rd_msg_fifo_head != dev->rd_msg_fifo_tail) {
785 memcpy(hbm, dev->rd_msg_fifo + dev->rd_msg_fifo_head,
786 IPC_PAYLOAD_SIZE);
787 dev->rd_msg_fifo_head =
788 (dev->rd_msg_fifo_head + IPC_PAYLOAD_SIZE) %
789 (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
790 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
791 ishtp_hbm_dispatch(dev, (struct ishtp_bus_message *)hbm);
792 } else {
793 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
798 * recv_hbm() - Receive HBM message
799 * @dev: ISHTP device instance
800 * @ishtp_hdr: received bus message
802 * Receive and process ISHTP bus messages in ISR context. This will schedule
803 * work function to process message
805 void recv_hbm(struct ishtp_device *dev, struct ishtp_msg_hdr *ishtp_hdr)
807 uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
808 struct ishtp_bus_message *ishtp_msg =
809 (struct ishtp_bus_message *)rd_msg_buf;
810 unsigned long flags;
812 dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
814 /* Flow control - handle in place */
815 if (ishtp_msg->hbm_cmd == ISHTP_FLOW_CONTROL_CMD) {
816 struct hbm_flow_control *flow_control =
817 (struct hbm_flow_control *)ishtp_msg;
818 struct ishtp_cl *cl = NULL;
819 unsigned long flags, tx_flags;
821 spin_lock_irqsave(&dev->cl_list_lock, flags);
822 list_for_each_entry(cl, &dev->cl_list, link) {
823 if (cl->host_client_id == flow_control->host_addr &&
824 cl->fw_client_id ==
825 flow_control->fw_addr) {
827 * NOTE: It's valid only for counting
828 * flow-control implementation to receive a
829 * FC in the middle of sending. Meanwhile not
830 * supported
832 if (cl->ishtp_flow_ctrl_creds)
833 dev_err(dev->devc,
834 "recv extra FC from FW client %u (host client %u) (FC count was %d)\n",
835 (unsigned int)cl->fw_client_id,
836 (unsigned int)cl->host_client_id,
837 cl->ishtp_flow_ctrl_creds);
838 else {
839 ++cl->ishtp_flow_ctrl_creds;
840 ++cl->ishtp_flow_ctrl_cnt;
841 cl->last_ipc_acked = 1;
842 spin_lock_irqsave(
843 &cl->tx_list_spinlock,
844 tx_flags);
845 if (!list_empty(&cl->tx_list.list)) {
847 * start sending the first msg
848 * = the callback function
850 spin_unlock_irqrestore(
851 &cl->tx_list_spinlock,
852 tx_flags);
853 ishtp_cl_send_msg(dev, cl);
854 } else {
855 spin_unlock_irqrestore(
856 &cl->tx_list_spinlock,
857 tx_flags);
860 break;
863 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
864 goto eoi;
868 * Some messages that are safe for ISR processing and important
869 * to be done "quickly" and in-order, go here
871 if (ishtp_msg->hbm_cmd == CLIENT_CONNECT_RES_CMD ||
872 ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_RES_CMD ||
873 ishtp_msg->hbm_cmd == CLIENT_DISCONNECT_REQ_CMD ||
874 ishtp_msg->hbm_cmd == DMA_XFER) {
875 ishtp_hbm_dispatch(dev, ishtp_msg);
876 goto eoi;
880 * All other HBMs go here.
881 * We schedule HBMs for processing serially by using system wq,
882 * possibly there will be multiple HBMs scheduled at the same time.
884 spin_lock_irqsave(&dev->rd_msg_spinlock, flags);
885 if ((dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
886 (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE) ==
887 dev->rd_msg_fifo_head) {
888 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
889 dev_err(dev->devc, "BH buffer overflow, dropping HBM %u\n",
890 (unsigned int)ishtp_msg->hbm_cmd);
891 goto eoi;
893 memcpy(dev->rd_msg_fifo + dev->rd_msg_fifo_tail, ishtp_msg,
894 ishtp_hdr->length);
895 dev->rd_msg_fifo_tail = (dev->rd_msg_fifo_tail + IPC_PAYLOAD_SIZE) %
896 (RD_INT_FIFO_SIZE * IPC_PAYLOAD_SIZE);
897 spin_unlock_irqrestore(&dev->rd_msg_spinlock, flags);
898 schedule_work(&dev->bh_hbm_work);
899 eoi:
900 return;
904 * recv_fixed_cl_msg() - Receive fixed client message
905 * @dev: ISHTP device instance
906 * @ishtp_hdr: received bus message
908 * Receive and process ISHTP fixed client messages (address == 0)
909 * in ISR context
911 void recv_fixed_cl_msg(struct ishtp_device *dev,
912 struct ishtp_msg_hdr *ishtp_hdr)
914 uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
916 dev->print_log(dev,
917 "%s() got fixed client msg from client #%d\n",
918 __func__, ishtp_hdr->fw_addr);
919 dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
920 if (ishtp_hdr->fw_addr == ISHTP_SYSTEM_STATE_CLIENT_ADDR) {
921 struct ish_system_states_header *msg_hdr =
922 (struct ish_system_states_header *)rd_msg_buf;
923 if (msg_hdr->cmd == SYSTEM_STATE_SUBSCRIBE)
924 ishtp_send_resume(dev);
925 /* if FW request arrived here, the system is not suspended */
926 else
927 dev_err(dev->devc, "unknown fixed client msg [%02X]\n",
928 msg_hdr->cmd);
933 * fix_cl_hdr() - Initialize fixed client header
934 * @hdr: message header
935 * @length: length of message
936 * @cl_addr: Client address
938 * Initialize message header for fixed client
940 static inline void fix_cl_hdr(struct ishtp_msg_hdr *hdr, size_t length,
941 uint8_t cl_addr)
943 hdr->host_addr = 0;
944 hdr->fw_addr = cl_addr;
945 hdr->length = length;
946 hdr->msg_complete = 1;
947 hdr->reserved = 0;
950 /*** Suspend and resume notification ***/
952 static uint32_t current_state;
953 static uint32_t supported_states = 0 | SUSPEND_STATE_BIT;
956 * ishtp_send_suspend() - Send suspend message to FW
957 * @dev: ISHTP device instance
959 * Send suspend message to FW. This is useful for system freeze (non S3) case
961 void ishtp_send_suspend(struct ishtp_device *dev)
963 struct ishtp_msg_hdr ishtp_hdr;
964 struct ish_system_states_status state_status_msg;
965 const size_t len = sizeof(struct ish_system_states_status);
967 fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
969 memset(&state_status_msg, 0, len);
970 state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
971 state_status_msg.supported_states = supported_states;
972 current_state |= SUSPEND_STATE_BIT;
973 dev->print_log(dev, "%s() sends SUSPEND notification\n", __func__);
974 state_status_msg.states_status = current_state;
976 ishtp_write_message(dev, &ishtp_hdr,
977 (unsigned char *)&state_status_msg);
979 EXPORT_SYMBOL(ishtp_send_suspend);
982 * ishtp_send_resume() - Send resume message to FW
983 * @dev: ISHTP device instance
985 * Send resume message to FW. This is useful for system freeze (non S3) case
987 void ishtp_send_resume(struct ishtp_device *dev)
989 struct ishtp_msg_hdr ishtp_hdr;
990 struct ish_system_states_status state_status_msg;
991 const size_t len = sizeof(struct ish_system_states_status);
993 fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
995 memset(&state_status_msg, 0, len);
996 state_status_msg.hdr.cmd = SYSTEM_STATE_STATUS;
997 state_status_msg.supported_states = supported_states;
998 current_state &= ~SUSPEND_STATE_BIT;
999 dev->print_log(dev, "%s() sends RESUME notification\n", __func__);
1000 state_status_msg.states_status = current_state;
1002 ishtp_write_message(dev, &ishtp_hdr,
1003 (unsigned char *)&state_status_msg);
1005 EXPORT_SYMBOL(ishtp_send_resume);
1008 * ishtp_query_subscribers() - Send query subscribers message
1009 * @dev: ISHTP device instance
1011 * Send message to query subscribers
1013 void ishtp_query_subscribers(struct ishtp_device *dev)
1015 struct ishtp_msg_hdr ishtp_hdr;
1016 struct ish_system_states_query_subscribers query_subscribers_msg;
1017 const size_t len = sizeof(struct ish_system_states_query_subscribers);
1019 fix_cl_hdr(&ishtp_hdr, len, ISHTP_SYSTEM_STATE_CLIENT_ADDR);
1021 memset(&query_subscribers_msg, 0, len);
1022 query_subscribers_msg.hdr.cmd = SYSTEM_STATE_QUERY_SUBSCRIBERS;
1024 ishtp_write_message(dev, &ishtp_hdr,
1025 (unsigned char *)&query_subscribers_msg);