/** * ishtp_cl_flush_queues() - Flush all queues for a client * @cl: ishtp client instance * * Used to remove all queues for a client. This is called when a client device * needs reset due to error, S3 resume or during module removal * * Return: 0 on success else -EINVAL if device is NULL
*/ int ishtp_cl_flush_queues(struct ishtp_cl *cl)
{ if (WARN_ON(!cl || !cl->dev)) return -EINVAL;
ishtp_read_list_flush(cl);
return 0;
}
EXPORT_SYMBOL(ishtp_cl_flush_queues);
/** * ishtp_cl_init() - Initialize all fields of a client device * @cl: ishtp client instance * @dev: ishtp device * * Initializes a client device fields: Init spinlocks, init queues etc. * This function is called during new client creation
*/ staticvoid ishtp_cl_init(struct ishtp_cl *cl, struct ishtp_device *dev)
{
memset(cl, 0, sizeof(struct ishtp_cl));
init_waitqueue_head(&cl->wait_ctrl_res);
spin_lock_init(&cl->free_list_spinlock);
spin_lock_init(&cl->in_process_spinlock);
spin_lock_init(&cl->tx_list_spinlock);
spin_lock_init(&cl->tx_free_list_spinlock);
spin_lock_init(&cl->fc_spinlock);
INIT_LIST_HEAD(&cl->link);
cl->dev = dev;
/** * ishtp_cl_link() - Reserve a host id and link the client instance * @cl: client device instance * * This allocates a single bit in the hostmap. This function will make sure * that not many client sessions are opened at the same time. Once allocated * the client device instance is added to the ishtp device in the current * client list * * Return: 0 or error code on failure
*/ int ishtp_cl_link(struct ishtp_cl *cl)
{ struct ishtp_device *dev; unsignedlong flags, flags_cl; int id, ret = 0;
if (WARN_ON(!cl || !cl->dev)) return -EINVAL;
dev = cl->dev;
spin_lock_irqsave(&dev->device_lock, flags);
if (dev->open_handle_count >= ISHTP_MAX_OPEN_HANDLE_COUNT) {
ret = -EMFILE; goto unlock_dev;
}
id = find_first_zero_bit(dev->host_clients_map, ISHTP_CLIENTS_MAX);
if (id >= ISHTP_CLIENTS_MAX) {
spin_unlock_irqrestore(&dev->device_lock, flags);
dev_err(&cl->device->dev, "id exceeded %d", ISHTP_CLIENTS_MAX); return -ENOENT;
}
/* * This checks that 'cl' is actually linked into device's structure, * before attempting 'list_del'
*/
spin_lock_irqsave(&dev->cl_list_lock, flags);
list_for_each_entry(pos, &dev->cl_list, link) if (cl->host_client_id == pos->host_client_id) {
list_del_init(&pos->link); break;
}
spin_unlock_irqrestore(&dev->cl_list_lock, flags);
}
EXPORT_SYMBOL(ishtp_cl_unlink);
/** * ishtp_cl_disconnect() - Send disconnect request to firmware * @cl: client device instance * * Send a disconnect request for a client to firmware. * * Return: 0 if successful disconnect response from the firmware or error * code on failure
*/ int ishtp_cl_disconnect(struct ishtp_cl *cl)
{ struct ishtp_device *dev;
if (WARN_ON(!cl || !cl->dev)) return -ENODEV;
dev = cl->dev;
dev->print_log(dev, "%s() state %d\n", __func__, cl->state);
if (cl->state != ISHTP_CL_DISCONNECTING) {
dev->print_log(dev, "%s() Disconnect in progress\n", __func__); return 0;
}
if (ishtp_hbm_cl_disconnect_req(dev, cl)) {
dev->print_log(dev, "%s() Failed to disconnect\n", __func__);
dev_err(&cl->device->dev, "failed to disconnect.\n"); return -ENODEV;
}
/* * If FW reset arrived, this will happen. Don't check cl->, * as 'cl' may be freed already
*/ if (dev->dev_state != ISHTP_DEV_ENABLED) {
dev->print_log(dev, "%s() dev_state != ISHTP_DEV_ENABLED\n",
__func__); return -ENODEV;
}
/** * ishtp_cl_is_other_connecting() - Check other client is connecting * @cl: client device instance * * Checks if other client with the same fw client id is connecting * * Return: true if other client is connected else false
*/ staticbool ishtp_cl_is_other_connecting(struct ishtp_cl *cl)
{ struct ishtp_device *dev; struct ishtp_cl *pos; unsignedlong flags;
/** * ishtp_cl_connect_to_fw() - Send connect request to firmware * @cl: client device instance * * Send a connect request to the firmware and wait for firmware response. * If there is successful connection response from the firmware, change * client state to ISHTP_CL_CONNECTED, and bind client to related * firmware client_id. * * Return: 0 for success and error code on failure
*/ staticint ishtp_cl_connect_to_fw(struct ishtp_cl *cl)
{ struct ishtp_device *dev; int rets;
if (WARN_ON(!cl || !cl->dev)) return -ENODEV;
dev = cl->dev;
if (ishtp_cl_is_other_connecting(cl)) {
dev->print_log(dev, "%s() Busy\n", __func__); return -EBUSY;
}
/** * ishtp_cl_connect() - Build connection with firmware * @cl: client device instance * * Call ishtp_cl_connect_to_fw() to connect and bind to firmware. If successful, * allocate RX and TX ring buffers, and start flow control with firmware to * start communication. * * Return: 0 if there is successful connection to the firmware, allocate * ring buffers.
*/ int ishtp_cl_connect(struct ishtp_cl *cl)
{ struct ishtp_device *dev; int rets;
rets = ishtp_cl_connect_to_fw(cl); if (rets) {
dev->print_log(dev, "%s() Connect to fw failed\n", __func__); return rets;
}
rets = ishtp_cl_alloc_rx_ring(cl); if (rets) {
dev->print_log(dev, "%s() Alloc RX ring failed\n", __func__); /* if failed allocation, disconnect */
ishtp_cl_disconnect(cl); return rets;
}
rets = ishtp_cl_alloc_tx_ring(cl); if (rets) {
dev->print_log(dev, "%s() Alloc TX ring failed\n", __func__); /* if failed allocation, disconnect */
ishtp_cl_free_rx_ring(cl);
ishtp_cl_disconnect(cl); return rets;
}
/* * Upon successful connection and allocation, start flow-control.
*/
rets = ishtp_cl_read_start(cl);
return rets;
}
EXPORT_SYMBOL(ishtp_cl_connect);
/** * ishtp_cl_establish_connection() - Establish connection with the firmware * @cl: client device instance * @uuid: uuid of the client to search * @tx_size: TX ring buffer size * @rx_size: RX ring buffer size * @reset: true if called for reset connection, otherwise for first connection * * This is a helper function for client driver to build connection with firmware. * If it's first time connecting to the firmware, set reset to false, this * function will link client to bus, find client id and send connect request to * the firmware. * * If it's called for reset handler where client lost connection after * firmware reset, set reset to true, this function will reinit client state and * establish connection again. In this case, this function reuses current client * structure and ring buffers to avoid allocation failure and memory fragments. * * Return: 0 for successful connection with the firmware, * or error code on failure
*/ int ishtp_cl_establish_connection(struct ishtp_cl *cl, const guid_t *uuid, int tx_size, int rx_size, bool reset)
{ struct ishtp_device *dev; struct ishtp_fw_client *fw_client; int rets;
/* * For reset case, not allocate tx/rx ring buffer which are already * done in ishtp_cl_connect() during first connection.
*/ if (reset) {
rets = ishtp_cl_connect_to_fw(cl); if (!rets)
rets = ishtp_cl_read_start(cl); else
dev->print_log(dev, "%s() connect to fw failed\n", __func__);
} else {
rets = ishtp_cl_connect(cl);
}
/** * ishtp_cl_destroy_connection() - Disconnect with the firmware * @cl: client device instance * @reset: true if called for firmware reset, false for normal disconnection * * This is a helper function for client driver to disconnect with firmware, * unlink to bus and flush message queue.
*/ void ishtp_cl_destroy_connection(struct ishtp_cl *cl, bool reset)
{ if (!cl) return;
if (reset) { /* * For reset case, connection is already lost during fw reset. * Just set state to DISCONNECTED is enough.
*/
ishtp_set_connection_state(cl, ISHTP_CL_DISCONNECTED);
} else { if (cl->state != ISHTP_CL_DISCONNECTED) {
ishtp_set_connection_state(cl, ISHTP_CL_DISCONNECTING);
ishtp_cl_disconnect(cl);
}
}
/** * ishtp_cl_read_start() - Prepare to read client message * @cl: client device instance * * Get a free buffer from pool of free read buffers and add to read buffer * pool to add contents. Send a flow control request to firmware to be able * send next message. * * Return: 0 if successful or error code on failure
*/ int ishtp_cl_read_start(struct ishtp_cl *cl)
{ struct ishtp_device *dev; struct ishtp_cl_rb *rb; int rets; int i; unsignedlong flags; unsignedlong dev_flags;
if (WARN_ON(!cl || !cl->dev)) return -ENODEV;
dev = cl->dev;
if (cl->state != ISHTP_CL_CONNECTED) return -ENODEV;
if (dev->dev_state != ISHTP_DEV_ENABLED) return -ENODEV;
i = ishtp_fw_cl_by_id(dev, cl->fw_client_id); if (i < 0) {
dev_err(&cl->device->dev, "no such fw client %d\n",
cl->fw_client_id); return -ENODEV;
}
/* The current rb is the head of the free rb list */
spin_lock_irqsave(&cl->free_list_spinlock, flags); if (list_empty(&cl->free_rb_list.list)) {
dev_warn(&cl->device->dev, "[ishtp-ish] Rx buffers pool is empty\n");
rets = -ENOMEM;
rb = NULL;
spin_unlock_irqrestore(&cl->free_list_spinlock, flags); goto out;
}
rb = list_entry(cl->free_rb_list.list.next, struct ishtp_cl_rb, list);
list_del_init(&rb->list);
spin_unlock_irqrestore(&cl->free_list_spinlock, flags);
rb->cl = cl;
rb->buf_idx = 0;
INIT_LIST_HEAD(&rb->list);
rets = 0;
/* * This must be BEFORE sending flow control - * response in ISR may come too fast...
*/
spin_lock_irqsave(&dev->read_list_spinlock, dev_flags);
list_add_tail(&rb->list, &dev->read_list.list);
spin_unlock_irqrestore(&dev->read_list_spinlock, dev_flags); if (ishtp_hbm_cl_flow_control_req(dev, cl)) {
rets = -ENODEV; goto out;
}
out: /* if ishtp_hbm_cl_flow_control_req failed, return rb to free list */ if (rets && rb) {
spin_lock_irqsave(&dev->read_list_spinlock, dev_flags);
list_del(&rb->list);
spin_unlock_irqrestore(&dev->read_list_spinlock, dev_flags);
/** * ishtp_cl_send() - Send a message to firmware * @cl: client device instance * @buf: message buffer * @length: length of message * * If the client is correct state to send message, this function gets a buffer * from tx ring buffers, copy the message data and call to send the message * using ishtp_cl_send_msg() * * Return: 0 if successful or error code on failure
*/ int ishtp_cl_send(struct ishtp_cl *cl, uint8_t *buf, size_t length)
{ struct ishtp_device *dev; int id; struct ishtp_cl_tx_ring *cl_msg; int have_msg_to_send = 0; unsignedlong tx_flags, tx_free_flags;
if (WARN_ON(!cl || !cl->dev)) return -ENODEV;
dev = cl->dev;
if (cl->state != ISHTP_CL_CONNECTED) {
++cl->err_send_msg; return -EPIPE;
}
if (dev->dev_state != ISHTP_DEV_ENABLED) {
++cl->err_send_msg; return -ENODEV;
}
/* Check if we have fw client device */
id = ishtp_fw_cl_by_id(dev, cl->fw_client_id); if (id < 0) {
++cl->err_send_msg; return -ENOENT;
}
if (length > dev->fw_clients[id].props.max_msg_length) {
++cl->err_send_msg; return -EMSGSIZE;
}
/* No free bufs */
spin_lock_irqsave(&cl->tx_free_list_spinlock, tx_free_flags); if (list_empty(&cl->tx_free_list.list)) {
spin_unlock_irqrestore(&cl->tx_free_list_spinlock,
tx_free_flags);
++cl->err_send_msg; return -ENOMEM;
}
cl_msg = list_first_entry(&cl->tx_free_list.list, struct ishtp_cl_tx_ring, list); if (!cl_msg->send_buf.data) {
spin_unlock_irqrestore(&cl->tx_free_list_spinlock,
tx_free_flags); return -EIO; /* Should not happen, as free list is pre-allocated */
} /* * This is safe, as 'length' is already checked for not exceeding * max ISHTP message size per client
*/
list_del_init(&cl_msg->list);
--cl->tx_ring_free_size;
if (!have_msg_to_send && cl->ishtp_flow_ctrl_creds > 0)
ishtp_cl_send_msg(dev, cl);
return 0;
}
EXPORT_SYMBOL(ishtp_cl_send);
/** * ishtp_cl_read_complete() - read complete * @rb: Pointer to client request block * * If the message is completely received call ishtp_cl_bus_rx_event() * to process message
*/ staticvoid ishtp_cl_read_complete(struct ishtp_cl_rb *rb)
{ unsignedlong flags; int schedule_work_flag = 0; struct ishtp_cl *cl = rb->cl;
spin_lock_irqsave(&cl->in_process_spinlock, flags); /* * if in-process list is empty, then need to schedule * the processing thread
*/
schedule_work_flag = list_empty(&cl->in_process_list.list);
list_add_tail(&rb->list, &cl->in_process_list.list);
spin_unlock_irqrestore(&cl->in_process_spinlock, flags);
if (schedule_work_flag)
ishtp_cl_bus_rx_event(cl->device);
}
/** * ipc_tx_send() - IPC tx send function * @prm: Pointer to client device instance * * Send message over IPC. Message will be split into fragments * if message size is bigger than IPC FIFO size, and all * fragments will be sent one by one.
*/ staticvoid ipc_tx_send(void *prm)
{ struct ishtp_cl *cl = prm; struct ishtp_cl_tx_ring *cl_msg;
size_t rem; struct ishtp_device *dev = (cl ? cl->dev : NULL); struct ishtp_msg_hdr ishtp_hdr; unsignedlong tx_flags, tx_free_flags; unsignedchar *pmsg;
if (!dev) return;
/* * Other conditions if some critical error has * occurred before this callback is called
*/ if (dev->dev_state != ISHTP_DEV_ENABLED) return;
if (cl->state != ISHTP_CL_CONNECTED) return;
spin_lock_irqsave(&cl->tx_list_spinlock, tx_flags); if (list_empty(&cl->tx_list.list)) {
spin_unlock_irqrestore(&cl->tx_list_spinlock, tx_flags); return;
}
if (rem <= dev->mtu) { /* Last fragment or only one packet */
ishtp_hdr.length = rem;
ishtp_hdr.msg_complete = 1; /* Submit to IPC queue with no callback */
ishtp_write_message(dev, &ishtp_hdr, pmsg);
cl->tx_offs = 0;
cl->sending = 0;
break;
} else { /* Send ipc fragment */
ishtp_hdr.length = dev->mtu;
ishtp_hdr.msg_complete = 0; /* All fragments submitted to IPC queue with no callback */
ishtp_write_message(dev, &ishtp_hdr, pmsg);
cl->tx_offs += dev->mtu;
rem = cl_msg->send_buf.size - cl->tx_offs;
}
}
/* write msg to dma buf */
memcpy(msg_addr, cl_msg->send_buf.data, cl_msg->send_buf.size);
/* * if current fw don't support cache snooping, driver have to * flush the cache manually.
*/ if (dev->ops->dma_no_cache_snooping &&
dev->ops->dma_no_cache_snooping(dev))
clflush_cache_range(msg_addr, cl_msg->send_buf.size);
/* If no Rx buffer is allocated, disband the rb */ if (rb->buffer.size == 0 || rb->buffer.data == NULL) {
spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
dev_err(&cl->device->dev, "Rx buffer is not allocated.\n");
list_del(&rb->list);
ishtp_io_rb_free(rb);
cl->status = -ENOMEM; goto eoi;
}
/* * If message buffer overflown (exceeds max. client msg * size, drop message and return to free buffer. * Do we need to disconnect such a client? (We don't send * back FC, so communication will be stuck anyway)
*/ if (rb->buffer.size < ishtp_hdr->length + rb->buf_idx) {
spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
dev_err(&cl->device->dev, "message overflow. size %d len %d idx %ld\n",
rb->buffer.size, ishtp_hdr->length,
rb->buf_idx);
list_del(&rb->list);
ishtp_cl_io_rb_recycle(rb);
cl->status = -EIO; goto eoi;
}
rb->buf_idx += ishtp_hdr->length; if (ishtp_hdr->msg_complete) { /* Last fragment in message - it's complete */
cl->status = 0;
list_del(&rb->list);
complete_rb = rb;
--cl->out_flow_ctrl_creds; /* * the whole msg arrived, send a new FC, and add a new * rb buffer for the next coming msg
*/
spin_lock(&cl->free_list_spinlock);
ishtp_hbm_cl_flow_control_req(dev, cl);
} else {
spin_unlock(&cl->free_list_spinlock);
}
} /* One more fragment in message (even if this was last) */
++cl->recv_msg_num_frags;
/* * We can safely break here (and in BH too), * a single input message can go only to a single request!
*/ break;
}
spin_unlock_irqrestore(&dev->read_list_spinlock, flags); /* If it's nobody's message, just read and discard it */ if (!buffer) {
uint8_t rd_msg_buf[ISHTP_RD_MSG_BUF_SIZE];
/* * If no Rx buffer is allocated, disband the rb
*/ if (rb->buffer.size == 0 || rb->buffer.data == NULL) {
spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
dev_err(&cl->device->dev, "response buffer is not allocated.\n");
list_del(&rb->list);
ishtp_io_rb_free(rb);
cl->status = -ENOMEM; goto eoi;
}
/* * If message buffer overflown (exceeds max. client msg * size, drop message and return to free buffer. * Do we need to disconnect such a client? (We don't send * back FC, so communication will be stuck anyway)
*/ if (rb->buffer.size < hbm->msg_length) {
spin_unlock_irqrestore(&dev->read_list_spinlock, flags);
dev_err(&cl->device->dev, "message overflow. size %d len %d idx %ld\n",
rb->buffer.size, hbm->msg_length, rb->buf_idx);
list_del(&rb->list);
ishtp_cl_io_rb_recycle(rb);
cl->status = -EIO; goto eoi;
}
buffer = rb->buffer.data;
/* * if current fw don't support cache snooping, driver have to * flush the cache manually.
*/ if (dev->ops->dma_no_cache_snooping &&
dev->ops->dma_no_cache_snooping(dev))
clflush_cache_range(msg, hbm->msg_length);
/* Last fragment in message - it's complete */
cl->status = 0;
list_del(&rb->list);
complete_rb = rb;
--cl->out_flow_ctrl_creds; /* * the whole msg arrived, send a new FC, and add a new * rb buffer for the next coming msg
*/
spin_lock(&cl->free_list_spinlock);
/* One more fragment in message (this is always last) */
++cl->recv_msg_num_frags;
/* * We can safely break here (and in BH too), * a single input message can go only to a single request!
*/ break;
}
spin_unlock_irqrestore(&dev->read_list_spinlock, flags); /* If it's nobody's message, just read and discard it */ if (!buffer) {
dev_err(dev->devc, "Dropped Rx (DMA) msg - no request\n"); goto eoi;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.