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/slab.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/delay.h>
21 #include <linux/dma-mapping.h>
25 int ishtp_cl_get_tx_free_buffer_size(struct ishtp_cl *cl)
27 unsigned long tx_free_flags;
30 spin_lock_irqsave(&cl->tx_free_list_spinlock, tx_free_flags);
31 size = cl->tx_ring_free_size * cl->device->fw_client->props.max_msg_length;
32 spin_unlock_irqrestore(&cl->tx_free_list_spinlock, tx_free_flags);
36 EXPORT_SYMBOL(ishtp_cl_get_tx_free_buffer_size);
38 int ishtp_cl_get_tx_free_rings(struct ishtp_cl *cl)
40 return cl->tx_ring_free_size;
42 EXPORT_SYMBOL(ishtp_cl_get_tx_free_rings);
45 * ishtp_read_list_flush() - Flush read queue
46 * @cl: ishtp client instance
48 * Used to remove all entries from read queue for a client
50 static void ishtp_read_list_flush(struct ishtp_cl *cl)
52 struct ishtp_cl_rb *rb;
53 struct ishtp_cl_rb *next;
56 spin_lock_irqsave(&cl->dev->read_list_spinlock, flags);
57 list_for_each_entry_safe(rb, next, &cl->dev->read_list.list, list)
58 if (rb->cl && ishtp_cl_cmp_id(cl, rb->cl)) {
62 spin_unlock_irqrestore(&cl->dev->read_list_spinlock, flags);
66 * ishtp_cl_flush_queues() - Flush all queues for a client
67 * @cl: ishtp client instance
69 * Used to remove all queues for a client. This is called when a client device
70 * needs reset due to error, S3 resume or during module removal
72 * Return: 0 on success else -EINVAL if device is NULL
74 int ishtp_cl_flush_queues(struct ishtp_cl *cl)
76 if (WARN_ON(!cl || !cl->dev))
79 ishtp_read_list_flush(cl);
83 EXPORT_SYMBOL(ishtp_cl_flush_queues);
86 * ishtp_cl_init() - Initialize all fields of a client device
87 * @cl: ishtp client instance
90 * Initializes a client device fields: Init spinlocks, init queues etc.
91 * This function is called during new client creation
93 static void ishtp_cl_init(struct ishtp_cl *cl, struct ishtp_device *dev)
95 memset(cl, 0, sizeof(struct ishtp_cl));
96 init_waitqueue_head(&cl->wait_ctrl_res);
97 spin_lock_init(&cl->free_list_spinlock);
98 spin_lock_init(&cl->in_process_spinlock);
99 spin_lock_init(&cl->tx_list_spinlock);
100 spin_lock_init(&cl->tx_free_list_spinlock);
101 spin_lock_init(&cl->fc_spinlock);
102 INIT_LIST_HEAD(&cl->link);
105 INIT_LIST_HEAD(&cl->free_rb_list.list);
106 INIT_LIST_HEAD(&cl->tx_list.list);
107 INIT_LIST_HEAD(&cl->tx_free_list.list);
108 INIT_LIST_HEAD(&cl->in_process_list.list);
110 cl->rx_ring_size = CL_DEF_RX_RING_SIZE;
111 cl->tx_ring_size = CL_DEF_TX_RING_SIZE;
112 cl->tx_ring_free_size = cl->tx_ring_size;
115 cl->last_tx_path = CL_TX_PATH_IPC;
116 cl->last_dma_acked = 1;
117 cl->last_dma_addr = NULL;
118 cl->last_ipc_acked = 1;
122 * ishtp_cl_allocate() - allocates client structure and sets it up.
125 * Allocate memory for new client device and call to initialize each field.
127 * Return: The allocated client instance or NULL on failure
129 struct ishtp_cl *ishtp_cl_allocate(struct ishtp_cl_device *cl_device)
133 cl = kmalloc(sizeof(struct ishtp_cl), GFP_KERNEL);
137 ishtp_cl_init(cl, cl_device->ishtp_dev);
140 EXPORT_SYMBOL(ishtp_cl_allocate);
143 * ishtp_cl_free() - Frees a client device
144 * @cl: client device instance
146 * Frees a client device
148 void ishtp_cl_free(struct ishtp_cl *cl)
150 struct ishtp_device *dev;
160 spin_lock_irqsave(&dev->cl_list_lock, flags);
161 ishtp_cl_free_rx_ring(cl);
162 ishtp_cl_free_tx_ring(cl);
164 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
166 EXPORT_SYMBOL(ishtp_cl_free);
169 * ishtp_cl_link() - Reserve a host id and link the client instance
170 * @cl: client device instance
172 * This allocates a single bit in the hostmap. This function will make sure
173 * that not many client sessions are opened at the same time. Once allocated
174 * the client device instance is added to the ishtp device in the current
177 * Return: 0 or error code on failure
179 int ishtp_cl_link(struct ishtp_cl *cl)
181 struct ishtp_device *dev;
182 unsigned long flags, flags_cl;
185 if (WARN_ON(!cl || !cl->dev))
190 spin_lock_irqsave(&dev->device_lock, flags);
192 if (dev->open_handle_count >= ISHTP_MAX_OPEN_HANDLE_COUNT) {
197 id = find_first_zero_bit(dev->host_clients_map, ISHTP_CLIENTS_MAX);
199 if (id >= ISHTP_CLIENTS_MAX) {
200 spin_unlock_irqrestore(&dev->device_lock, flags);
201 dev_err(&cl->device->dev, "id exceeded %d", ISHTP_CLIENTS_MAX);
205 dev->open_handle_count++;
206 cl->host_client_id = id;
207 spin_lock_irqsave(&dev->cl_list_lock, flags_cl);
208 if (dev->dev_state != ISHTP_DEV_ENABLED) {
212 list_add_tail(&cl->link, &dev->cl_list);
213 set_bit(id, dev->host_clients_map);
214 cl->state = ISHTP_CL_INITIALIZING;
217 spin_unlock_irqrestore(&dev->cl_list_lock, flags_cl);
219 spin_unlock_irqrestore(&dev->device_lock, flags);
222 EXPORT_SYMBOL(ishtp_cl_link);
225 * ishtp_cl_unlink() - remove fw_cl from the client device list
226 * @cl: client device instance
228 * Remove a previously linked device to a ishtp device
230 void ishtp_cl_unlink(struct ishtp_cl *cl)
232 struct ishtp_device *dev;
233 struct ishtp_cl *pos;
236 /* don't shout on error exit path */
242 spin_lock_irqsave(&dev->device_lock, flags);
243 if (dev->open_handle_count > 0) {
244 clear_bit(cl->host_client_id, dev->host_clients_map);
245 dev->open_handle_count--;
247 spin_unlock_irqrestore(&dev->device_lock, flags);
250 * This checks that 'cl' is actually linked into device's structure,
251 * before attempting 'list_del'
253 spin_lock_irqsave(&dev->cl_list_lock, flags);
254 list_for_each_entry(pos, &dev->cl_list, link)
255 if (cl->host_client_id == pos->host_client_id) {
256 list_del_init(&pos->link);
259 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
261 EXPORT_SYMBOL(ishtp_cl_unlink);
264 * ishtp_cl_disconnect() - Send disconnect request to firmware
265 * @cl: client device instance
267 * Send a disconnect request for a client to firmware.
269 * Return: 0 if successful disconnect response from the firmware or error
272 int ishtp_cl_disconnect(struct ishtp_cl *cl)
274 struct ishtp_device *dev;
277 if (WARN_ON(!cl || !cl->dev))
282 dev->print_log(dev, "%s() state %d\n", __func__, cl->state);
284 if (cl->state != ISHTP_CL_DISCONNECTING) {
285 dev->print_log(dev, "%s() Disconnect in progress\n", __func__);
289 if (ishtp_hbm_cl_disconnect_req(dev, cl)) {
290 dev->print_log(dev, "%s() Failed to disconnect\n", __func__);
291 dev_err(&cl->device->dev, "failed to disconnect.\n");
295 err = wait_event_interruptible_timeout(cl->wait_ctrl_res,
296 (dev->dev_state != ISHTP_DEV_ENABLED ||
297 cl->state == ISHTP_CL_DISCONNECTED),
298 ishtp_secs_to_jiffies(ISHTP_CL_CONNECT_TIMEOUT));
301 * If FW reset arrived, this will happen. Don't check cl->,
302 * as 'cl' may be freed already
304 if (dev->dev_state != ISHTP_DEV_ENABLED) {
305 dev->print_log(dev, "%s() dev_state != ISHTP_DEV_ENABLED\n",
310 if (cl->state == ISHTP_CL_DISCONNECTED) {
311 dev->print_log(dev, "%s() successful\n", __func__);
317 EXPORT_SYMBOL(ishtp_cl_disconnect);
320 * ishtp_cl_is_other_connecting() - Check other client is connecting
321 * @cl: client device instance
323 * Checks if other client with the same fw client id is connecting
325 * Return: true if other client is connected else false
327 static bool ishtp_cl_is_other_connecting(struct ishtp_cl *cl)
329 struct ishtp_device *dev;
330 struct ishtp_cl *pos;
333 if (WARN_ON(!cl || !cl->dev))
337 spin_lock_irqsave(&dev->cl_list_lock, flags);
338 list_for_each_entry(pos, &dev->cl_list, link) {
339 if ((pos->state == ISHTP_CL_CONNECTING) && (pos != cl) &&
340 cl->fw_client_id == pos->fw_client_id) {
341 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
345 spin_unlock_irqrestore(&dev->cl_list_lock, flags);
351 * ishtp_cl_connect() - Send connect request to firmware
352 * @cl: client device instance
354 * Send a connect request for a client to firmware. If successful it will
355 * RX and TX ring buffers
357 * Return: 0 if successful connect response from the firmware and able
358 * to bind and allocate ring buffers or error code on failure
360 int ishtp_cl_connect(struct ishtp_cl *cl)
362 struct ishtp_device *dev;
365 if (WARN_ON(!cl || !cl->dev))
370 dev->print_log(dev, "%s() current_state = %d\n", __func__, cl->state);
372 if (ishtp_cl_is_other_connecting(cl)) {
373 dev->print_log(dev, "%s() Busy\n", __func__);
377 if (ishtp_hbm_cl_connect_req(dev, cl)) {
378 dev->print_log(dev, "%s() HBM connect req fail\n", __func__);
382 rets = wait_event_interruptible_timeout(cl->wait_ctrl_res,
383 (dev->dev_state == ISHTP_DEV_ENABLED &&
384 (cl->state == ISHTP_CL_CONNECTED ||
385 cl->state == ISHTP_CL_DISCONNECTED)),
386 ishtp_secs_to_jiffies(
387 ISHTP_CL_CONNECT_TIMEOUT));
389 * If FW reset arrived, this will happen. Don't check cl->,
390 * as 'cl' may be freed already
392 if (dev->dev_state != ISHTP_DEV_ENABLED) {
393 dev->print_log(dev, "%s() dev_state != ISHTP_DEV_ENABLED\n",
398 if (cl->state != ISHTP_CL_CONNECTED) {
399 dev->print_log(dev, "%s() state != ISHTP_CL_CONNECTED\n",
406 dev->print_log(dev, "%s() Invalid status\n", __func__);
410 rets = ishtp_cl_device_bind(cl);
412 dev->print_log(dev, "%s() Bind error\n", __func__);
413 ishtp_cl_disconnect(cl);
417 rets = ishtp_cl_alloc_rx_ring(cl);
419 dev->print_log(dev, "%s() Alloc RX ring failed\n", __func__);
420 /* if failed allocation, disconnect */
421 ishtp_cl_disconnect(cl);
425 rets = ishtp_cl_alloc_tx_ring(cl);
427 dev->print_log(dev, "%s() Alloc TX ring failed\n", __func__);
428 /* if failed allocation, disconnect */
429 ishtp_cl_free_rx_ring(cl);
430 ishtp_cl_disconnect(cl);
434 /* Upon successful connection and allocation, emit flow-control */
435 rets = ishtp_cl_read_start(cl);
437 dev->print_log(dev, "%s() successful\n", __func__);
441 EXPORT_SYMBOL(ishtp_cl_connect);
444 * ishtp_cl_read_start() - Prepare to read client message
445 * @cl: client device instance
447 * Get a free buffer from pool of free read buffers and add to read buffer
448 * pool to add contents. Send a flow control request to firmware to be able
451 * Return: 0 if successful or error code on failure
453 int ishtp_cl_read_start(struct ishtp_cl *cl)
455 struct ishtp_device *dev;
456 struct ishtp_cl_rb *rb;
460 unsigned long dev_flags;
462 if (WARN_ON(!cl || !cl->dev))
467 if (cl->state != ISHTP_CL_CONNECTED)
470 if (dev->dev_state != ISHTP_DEV_ENABLED)
473 i = ishtp_fw_cl_by_id(dev, cl->fw_client_id);
475 dev_err(&cl->device->dev, "no such fw client %d\n",
480 /* The current rb is the head of the free rb list */
481 spin_lock_irqsave(&cl->free_list_spinlock, flags);
482 if (list_empty(&cl->free_rb_list.list)) {
483 dev_warn(&cl->device->dev,
484 "[ishtp-ish] Rx buffers pool is empty\n");
487 spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
490 rb = list_entry(cl->free_rb_list.list.next, struct ishtp_cl_rb, list);
491 list_del_init(&rb->list);
492 spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
497 INIT_LIST_HEAD(&rb->list);
501 * This must be BEFORE sending flow control -
502 * response in ISR may come too fast...
504 spin_lock_irqsave(&dev->read_list_spinlock, dev_flags);
505 list_add_tail(&rb->list, &dev->read_list.list);
506 spin_unlock_irqrestore(&dev->read_list_spinlock, dev_flags);
507 if (ishtp_hbm_cl_flow_control_req(dev, cl)) {
512 /* if ishtp_hbm_cl_flow_control_req failed, return rb to free list */
514 spin_lock_irqsave(&dev->read_list_spinlock, dev_flags);
516 spin_unlock_irqrestore(&dev->read_list_spinlock, dev_flags);
518 spin_lock_irqsave(&cl->free_list_spinlock, flags);
519 list_add_tail(&rb->list, &cl->free_rb_list.list);
520 spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
526 * ishtp_cl_send() - Send a message to firmware
527 * @cl: client device instance
528 * @buf: message buffer
529 * @length: length of message
531 * If the client is correct state to send message, this function gets a buffer
532 * from tx ring buffers, copy the message data and call to send the message
533 * using ishtp_cl_send_msg()
535 * Return: 0 if successful or error code on failure
537 int ishtp_cl_send(struct ishtp_cl *cl, uint8_t *buf, size_t length)
539 struct ishtp_device *dev;
541 struct ishtp_cl_tx_ring *cl_msg;
542 int have_msg_to_send = 0;
543 unsigned long tx_flags, tx_free_flags;
545 if (WARN_ON(!cl || !cl->dev))
550 if (cl->state != ISHTP_CL_CONNECTED) {
555 if (dev->dev_state != ISHTP_DEV_ENABLED) {
560 /* Check if we have fw client device */
561 id = ishtp_fw_cl_by_id(dev, cl->fw_client_id);
567 if (length > dev->fw_clients[id].props.max_msg_length) {
573 spin_lock_irqsave(&cl->tx_free_list_spinlock, tx_free_flags);
574 if (list_empty(&cl->tx_free_list.list)) {
575 spin_unlock_irqrestore(&cl->tx_free_list_spinlock,
581 cl_msg = list_first_entry(&cl->tx_free_list.list,
582 struct ishtp_cl_tx_ring, list);
583 if (!cl_msg->send_buf.data) {
584 spin_unlock_irqrestore(&cl->tx_free_list_spinlock,
587 /* Should not happen, as free list is pre-allocated */
590 * This is safe, as 'length' is already checked for not exceeding
591 * max ISHTP message size per client
593 list_del_init(&cl_msg->list);
594 --cl->tx_ring_free_size;
596 spin_unlock_irqrestore(&cl->tx_free_list_spinlock, tx_free_flags);
597 memcpy(cl_msg->send_buf.data, buf, length);
598 cl_msg->send_buf.size = length;
599 spin_lock_irqsave(&cl->tx_list_spinlock, tx_flags);
600 have_msg_to_send = !list_empty(&cl->tx_list.list);
601 list_add_tail(&cl_msg->list, &cl->tx_list.list);
602 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
604 if (!have_msg_to_send && cl->ishtp_flow_ctrl_creds > 0)
605 ishtp_cl_send_msg(dev, cl);
609 EXPORT_SYMBOL(ishtp_cl_send);
612 * ishtp_cl_read_complete() - read complete
613 * @rb: Pointer to client request block
615 * If the message is completely received call ishtp_cl_bus_rx_event()
618 static void ishtp_cl_read_complete(struct ishtp_cl_rb *rb)
621 int schedule_work_flag = 0;
622 struct ishtp_cl *cl = rb->cl;
624 spin_lock_irqsave(&cl->in_process_spinlock, flags);
626 * if in-process list is empty, then need to schedule
627 * the processing thread
629 schedule_work_flag = list_empty(&cl->in_process_list.list);
630 list_add_tail(&rb->list, &cl->in_process_list.list);
631 spin_unlock_irqrestore(&cl->in_process_spinlock, flags);
633 if (schedule_work_flag)
634 ishtp_cl_bus_rx_event(cl->device);
638 * ipc_tx_callback() - IPC tx callback function
639 * @prm: Pointer to client device instance
641 * Send message over IPC either first time or on callback on previous message
644 static void ipc_tx_callback(void *prm)
646 struct ishtp_cl *cl = prm;
647 struct ishtp_cl_tx_ring *cl_msg;
649 struct ishtp_device *dev = (cl ? cl->dev : NULL);
650 struct ishtp_msg_hdr ishtp_hdr;
651 unsigned long tx_flags, tx_free_flags;
658 * Other conditions if some critical error has
659 * occurred before this callback is called
661 if (dev->dev_state != ISHTP_DEV_ENABLED)
664 if (cl->state != ISHTP_CL_CONNECTED)
667 spin_lock_irqsave(&cl->tx_list_spinlock, tx_flags);
668 if (list_empty(&cl->tx_list.list)) {
669 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
673 if (cl->ishtp_flow_ctrl_creds != 1 && !cl->sending) {
674 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
679 --cl->ishtp_flow_ctrl_creds;
680 cl->last_ipc_acked = 0;
681 cl->last_tx_path = CL_TX_PATH_IPC;
685 cl_msg = list_entry(cl->tx_list.list.next, struct ishtp_cl_tx_ring,
687 rem = cl_msg->send_buf.size - cl->tx_offs;
689 ishtp_hdr.host_addr = cl->host_client_id;
690 ishtp_hdr.fw_addr = cl->fw_client_id;
691 ishtp_hdr.reserved = 0;
692 pmsg = cl_msg->send_buf.data + cl->tx_offs;
694 if (rem <= dev->mtu) {
695 ishtp_hdr.length = rem;
696 ishtp_hdr.msg_complete = 1;
698 list_del_init(&cl_msg->list); /* Must be before write */
699 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
700 /* Submit to IPC queue with no callback */
701 ishtp_write_message(dev, &ishtp_hdr, pmsg);
702 spin_lock_irqsave(&cl->tx_free_list_spinlock, tx_free_flags);
703 list_add_tail(&cl_msg->list, &cl->tx_free_list.list);
704 ++cl->tx_ring_free_size;
705 spin_unlock_irqrestore(&cl->tx_free_list_spinlock,
708 /* Send IPC fragment */
709 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
710 cl->tx_offs += dev->mtu;
711 ishtp_hdr.length = dev->mtu;
712 ishtp_hdr.msg_complete = 0;
713 ishtp_send_msg(dev, &ishtp_hdr, pmsg, ipc_tx_callback, cl);
718 * ishtp_cl_send_msg_ipc() -Send message using IPC
719 * @dev: ISHTP device instance
720 * @cl: Pointer to client device instance
722 * Send message over IPC not using DMA
724 static void ishtp_cl_send_msg_ipc(struct ishtp_device *dev,
727 /* If last DMA message wasn't acked yet, leave this one in Tx queue */
728 if (cl->last_tx_path == CL_TX_PATH_DMA && cl->last_dma_acked == 0)
733 ++cl->send_msg_cnt_ipc;
737 * ishtp_cl_send_msg_dma() -Send message using DMA
738 * @dev: ISHTP device instance
739 * @cl: Pointer to client device instance
741 * Send message using DMA
743 static void ishtp_cl_send_msg_dma(struct ishtp_device *dev,
746 struct ishtp_msg_hdr hdr;
747 struct dma_xfer_hbm dma_xfer;
748 unsigned char *msg_addr;
750 struct ishtp_cl_tx_ring *cl_msg;
751 unsigned long tx_flags, tx_free_flags;
753 /* If last IPC message wasn't acked yet, leave this one in Tx queue */
754 if (cl->last_tx_path == CL_TX_PATH_IPC && cl->last_ipc_acked == 0)
757 spin_lock_irqsave(&cl->tx_list_spinlock, tx_flags);
758 if (list_empty(&cl->tx_list.list)) {
759 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
763 cl_msg = list_entry(cl->tx_list.list.next, struct ishtp_cl_tx_ring,
766 msg_addr = ishtp_cl_get_dma_send_buf(dev, cl_msg->send_buf.size);
768 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
769 if (dev->transfer_path == CL_TX_PATH_DEFAULT)
770 ishtp_cl_send_msg_ipc(dev, cl);
774 list_del_init(&cl_msg->list); /* Must be before write */
775 spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags);
777 --cl->ishtp_flow_ctrl_creds;
778 cl->last_dma_acked = 0;
779 cl->last_dma_addr = msg_addr;
780 cl->last_tx_path = CL_TX_PATH_DMA;
782 /* write msg to dma buf */
783 memcpy(msg_addr, cl_msg->send_buf.data, cl_msg->send_buf.size);
785 /* send dma_xfer hbm msg */
786 off = msg_addr - (unsigned char *)dev->ishtp_host_dma_tx_buf;
787 ishtp_hbm_hdr(&hdr, sizeof(struct dma_xfer_hbm));
788 dma_xfer.hbm = DMA_XFER;
789 dma_xfer.fw_client_id = cl->fw_client_id;
790 dma_xfer.host_client_id = cl->host_client_id;
791 dma_xfer.reserved = 0;
792 dma_xfer.msg_addr = dev->ishtp_host_dma_tx_buf_phys + off;
793 dma_xfer.msg_length = cl_msg->send_buf.size;
794 dma_xfer.reserved2 = 0;
795 ishtp_write_message(dev, &hdr, (unsigned char *)&dma_xfer);
796 spin_lock_irqsave(&cl->tx_free_list_spinlock, tx_free_flags);
797 list_add_tail(&cl_msg->list, &cl->tx_free_list.list);
798 ++cl->tx_ring_free_size;
799 spin_unlock_irqrestore(&cl->tx_free_list_spinlock, tx_free_flags);
800 ++cl->send_msg_cnt_dma;
804 * ishtp_cl_send_msg() -Send message using DMA or IPC
805 * @dev: ISHTP device instance
806 * @cl: Pointer to client device instance
808 * Send message using DMA or IPC based on transfer_path
810 void ishtp_cl_send_msg(struct ishtp_device *dev, struct ishtp_cl *cl)
812 if (dev->transfer_path == CL_TX_PATH_DMA)
813 ishtp_cl_send_msg_dma(dev, cl);
815 ishtp_cl_send_msg_ipc(dev, cl);
819 * recv_ishtp_cl_msg() -Receive client message
820 * @dev: ISHTP device instance
821 * @ishtp_hdr: Pointer to message header
823 * Receive and dispatch ISHTP client messages. This function executes in ISR
824 * or work queue context
826 void recv_ishtp_cl_msg(struct ishtp_device *dev,
827 struct ishtp_msg_hdr *ishtp_hdr)
830 struct ishtp_cl_rb *rb;
831 struct ishtp_cl_rb *new_rb;
832 unsigned char *buffer = NULL;
833 struct ishtp_cl_rb *complete_rb = NULL;
837 if (ishtp_hdr->reserved) {
838 dev_err(dev->devc, "corrupted message header.\n");
842 if (ishtp_hdr->length > IPC_PAYLOAD_SIZE) {
844 "ISHTP message length in hdr exceeds IPC MTU\n");
848 spin_lock_irqsave(&dev->read_list_spinlock, flags);
850 list_for_each_entry(rb, &dev->read_list.list, list) {
853 if (!cl || !(cl->host_client_id == ishtp_hdr->host_addr &&
854 cl->fw_client_id == ishtp_hdr->fw_addr) ||
855 !(cl->state == ISHTP_CL_CONNECTED))
858 /* If no Rx buffer is allocated, disband the rb */
859 if (rb->buffer.size == 0 || rb->buffer.data == NULL) {
860 spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
861 dev_err(&cl->device->dev,
862 "Rx buffer is not allocated.\n");
864 ishtp_io_rb_free(rb);
865 cl->status = -ENOMEM;
870 * If message buffer overflown (exceeds max. client msg
871 * size, drop message and return to free buffer.
872 * Do we need to disconnect such a client? (We don't send
873 * back FC, so communication will be stuck anyway)
875 if (rb->buffer.size < ishtp_hdr->length + rb->buf_idx) {
876 spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
877 dev_err(&cl->device->dev,
878 "message overflow. size %d len %d idx %ld\n",
879 rb->buffer.size, ishtp_hdr->length,
882 ishtp_cl_io_rb_recycle(rb);
887 buffer = rb->buffer.data + rb->buf_idx;
888 dev->ops->ishtp_read(dev, buffer, ishtp_hdr->length);
890 rb->buf_idx += ishtp_hdr->length;
891 if (ishtp_hdr->msg_complete) {
892 /* Last fragment in message - it's complete */
897 --cl->out_flow_ctrl_creds;
899 * the whole msg arrived, send a new FC, and add a new
900 * rb buffer for the next coming msg
902 spin_lock(&cl->free_list_spinlock);
904 if (!list_empty(&cl->free_rb_list.list)) {
905 new_rb = list_entry(cl->free_rb_list.list.next,
906 struct ishtp_cl_rb, list);
907 list_del_init(&new_rb->list);
908 spin_unlock(&cl->free_list_spinlock);
911 INIT_LIST_HEAD(&new_rb->list);
912 list_add_tail(&new_rb->list,
913 &dev->read_list.list);
915 ishtp_hbm_cl_flow_control_req(dev, cl);
917 spin_unlock(&cl->free_list_spinlock);
920 /* One more fragment in message (even if this was last) */
921 ++cl->recv_msg_num_frags;
924 * We can safely break here (and in BH too),
925 * a single input message can go only to a single request!
930 spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
931 /* If it's nobody's message, just read and discard it */
933 uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
935 dev_err(dev->devc, "Dropped Rx msg - no request\n");
936 dev->ops->ishtp_read(dev, rd_msg_buf, ishtp_hdr->length);
941 cl = complete_rb->cl;
942 cl->ts_rx = ktime_get();
943 ++cl->recv_msg_cnt_ipc;
944 ishtp_cl_read_complete(complete_rb);
951 * recv_ishtp_cl_msg_dma() -Receive client message
952 * @dev: ISHTP device instance
953 * @msg: message pointer
956 * Receive and dispatch ISHTP client messages using DMA. This function executes
957 * in ISR or work queue context
959 void recv_ishtp_cl_msg_dma(struct ishtp_device *dev, void *msg,
960 struct dma_xfer_hbm *hbm)
963 struct ishtp_cl_rb *rb;
964 struct ishtp_cl_rb *new_rb;
965 unsigned char *buffer = NULL;
966 struct ishtp_cl_rb *complete_rb = NULL;
969 spin_lock_irqsave(&dev->read_list_spinlock, flags);
971 list_for_each_entry(rb, &dev->read_list.list, list) {
973 if (!cl || !(cl->host_client_id == hbm->host_client_id &&
974 cl->fw_client_id == hbm->fw_client_id) ||
975 !(cl->state == ISHTP_CL_CONNECTED))
979 * If no Rx buffer is allocated, disband the rb
981 if (rb->buffer.size == 0 || rb->buffer.data == NULL) {
982 spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
983 dev_err(&cl->device->dev,
984 "response buffer is not allocated.\n");
986 ishtp_io_rb_free(rb);
987 cl->status = -ENOMEM;
992 * If message buffer overflown (exceeds max. client msg
993 * size, drop message and return to free buffer.
994 * Do we need to disconnect such a client? (We don't send
995 * back FC, so communication will be stuck anyway)
997 if (rb->buffer.size < hbm->msg_length) {
998 spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
999 dev_err(&cl->device->dev,
1000 "message overflow. size %d len %d idx %ld\n",
1001 rb->buffer.size, hbm->msg_length, rb->buf_idx);
1002 list_del(&rb->list);
1003 ishtp_cl_io_rb_recycle(rb);
1008 buffer = rb->buffer.data;
1009 memcpy(buffer, msg, hbm->msg_length);
1010 rb->buf_idx = hbm->msg_length;
1012 /* Last fragment in message - it's complete */
1014 list_del(&rb->list);
1017 --cl->out_flow_ctrl_creds;
1019 * the whole msg arrived, send a new FC, and add a new
1020 * rb buffer for the next coming msg
1022 spin_lock(&cl->free_list_spinlock);
1024 if (!list_empty(&cl->free_rb_list.list)) {
1025 new_rb = list_entry(cl->free_rb_list.list.next,
1026 struct ishtp_cl_rb, list);
1027 list_del_init(&new_rb->list);
1028 spin_unlock(&cl->free_list_spinlock);
1030 new_rb->buf_idx = 0;
1031 INIT_LIST_HEAD(&new_rb->list);
1032 list_add_tail(&new_rb->list,
1033 &dev->read_list.list);
1035 ishtp_hbm_cl_flow_control_req(dev, cl);
1037 spin_unlock(&cl->free_list_spinlock);
1040 /* One more fragment in message (this is always last) */
1041 ++cl->recv_msg_num_frags;
1044 * We can safely break here (and in BH too),
1045 * a single input message can go only to a single request!
1050 spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
1051 /* If it's nobody's message, just read and discard it */
1053 dev_err(dev->devc, "Dropped Rx (DMA) msg - no request\n");
1058 cl = complete_rb->cl;
1059 cl->ts_rx = ktime_get();
1060 ++cl->recv_msg_cnt_dma;
1061 ishtp_cl_read_complete(complete_rb);
1067 void *ishtp_get_client_data(struct ishtp_cl *cl)
1069 return cl->client_data;
1071 EXPORT_SYMBOL(ishtp_get_client_data);
1073 void ishtp_set_client_data(struct ishtp_cl *cl, void *data)
1075 cl->client_data = data;
1077 EXPORT_SYMBOL(ishtp_set_client_data);
1079 struct ishtp_device *ishtp_get_ishtp_device(struct ishtp_cl *cl)
1083 EXPORT_SYMBOL(ishtp_get_ishtp_device);
1085 void ishtp_set_tx_ring_size(struct ishtp_cl *cl, int size)
1087 cl->tx_ring_size = size;
1089 EXPORT_SYMBOL(ishtp_set_tx_ring_size);
1091 void ishtp_set_rx_ring_size(struct ishtp_cl *cl, int size)
1093 cl->rx_ring_size = size;
1095 EXPORT_SYMBOL(ishtp_set_rx_ring_size);
1097 void ishtp_set_connection_state(struct ishtp_cl *cl, int state)
1101 EXPORT_SYMBOL(ishtp_set_connection_state);
1103 void ishtp_cl_set_fw_client_id(struct ishtp_cl *cl, int fw_client_id)
1105 cl->fw_client_id = fw_client_id;
1107 EXPORT_SYMBOL(ishtp_cl_set_fw_client_id);