/* Copyright (c) 2019-2021, The Linux Foundation. All rights reserved. */ /* Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved. */
/* * wire encoding structures for the manage protocol. * All fields are little endian on the wire
*/ struct wire_msg_hdr {
__le32 crc32; /* crc of everything following this field in the message */
__le32 magic_number;
__le32 sequence_number;
__le32 len; /* length of this message */
__le32 count; /* number of transactions in this message */
__le32 handle; /* unique id to track the resources consumed */
__le32 partition_id; /* partition id for the request (signed) */
__le32 padding; /* must be 0 */
} __packed;
/* Each message sent from driver to device are organized in a list of wrapper_msg */ struct wrapper_msg { struct list_head list; struct kref ref_count;
u32 len; /* length of data to transfer */ struct wrapper_list *head; union { struct wire_msg msg; struct wire_trans_hdr trans;
};
};
struct wrapper_list { struct list_head list;
spinlock_t lock; /* Protects the list state during additions and removals */
};
/* Initiated by device to continue the DMA xfer of a large piece of data */ struct wire_trans_dma_xfer_cont { struct wire_trans_hdr hdr;
__le32 dma_chunk_id;
__le32 padding;
__le64 xferred_size;
} __packed;
struct wire_trans_activate_to_dev { struct wire_trans_hdr hdr;
__le64 req_q_addr;
__le64 rsp_q_addr;
__le32 req_q_size;
__le32 rsp_q_size;
__le32 buf_len;
__le32 options; /* unused, but BIT(16) has meaning to the device */
} __packed;
struct xfer_queue_elem { /* * Node in list of ongoing transfer request on control channel. * Maintained by root device struct.
*/ struct list_head list; /* Sequence number of this transfer request */
u32 seq_num; /* This is used to wait on until completion of transfer request */ struct completion xfer_done; /* Received data from device */ void *buf;
};
struct dma_xfer { /* Node in list of DMA transfers which is used for cleanup */ struct list_head list; /* SG table of memory used for DMA */ struct sg_table *sgt; /* Array pages used for DMA */ struct page **page_list; /* Number of pages used for DMA */ unsignedlong nr_pages;
};
struct ioctl_resources { /* List of all DMA transfers which is used later for cleanup */ struct list_head dma_xfers; /* Base address of request queue which belongs to a DBC */ void *buf; /* * Base bus address of request queue which belongs to a DBC. Response * queue base bus address can be calculated by adding size of request * queue to base bus address of request queue.
*/
dma_addr_t dma_addr; /* Total size of request queue and response queue in byte */
u32 total_size; /* Total number of elements that can be queued in each of request and response queue */
u32 nelem; /* Base address of response queue which belongs to a DBC */ void *rsp_q_base; /* Status of the NNC message received */
u32 status; /* DBC id of the DBC received from device */
u32 dbc_id; /* * DMA transfer request messages can be big in size and it may not be * possible to send them in one shot. In such cases the messages are * broken into chunks, this field stores ID of such chunks.
*/
u32 dma_chunk_id; /* Total number of bytes transferred for a DMA xfer request */
u64 xferred_dma_size; /* Header of transaction message received from user. Used during DMA xfer request. */ void *trans_hdr;
};
/* * Since we're working with little endian messages, its useful to be able to * increment without filling a whole line with conversions back and forth just * to add one(1) to a message count.
*/ static __le32 incr_le32(__le32 val)
{ return cpu_to_le32(le32_to_cpu(val) + 1);
}
/* There should be enough space to hold at least one ASP entry. */ if (size_add(msg_hdr_len, sizeof(*out_trans) + sizeof(struct wire_addr_size_pair)) >
QAIC_MANAGE_EXT_MSG_LENGTH) return -ENOMEM;
xfer = kmalloc(sizeof(*xfer), GFP_KERNEL); if (!xfer) return -ENOMEM;
ret = find_and_map_user_pages(qdev, in_trans, resources, xfer); if (ret < 0) goto free_xfer;
need_cont_dma = (bool)ret;
ret = encode_addr_size_pairs(xfer, wrappers, resources, msg_hdr_len, &size, &out_trans); if (ret < 0) goto cleanup_xfer;
if (!resources->buf) /* how did we get an activate response without a request? */ return -EINVAL;
if (out_trans->dbc_id >= qdev->num_dbc) /* * The device assigned an invalid resource, which should never * happen. Return an error so the user can try to recover.
*/ return -ENODEV;
if (out_trans->status) /* * Allocating resources failed on device side. This is not an * expected behaviour, user is expected to handle this situation.
*/ return -ECANCELED;
if (dbc_id >= qdev->num_dbc) /* * The device assigned an invalid resource, which should never * happen. Inject an error so the user can try to recover.
*/ return -ENODEV;
if (status) { /* * Releasing resources failed on the device side, which puts * us in a bind since they may still be in use, so enable the * dbc. User is expected to retry deactivation.
*/
enable_dbc(qdev, dbc_id, usr); return -ECANCELED;
}
switch (le32_to_cpu(trans_hdr->type)) { case QAIC_TRANS_PASSTHROUGH_FROM_DEV:
ret = decode_passthrough(qdev, trans_hdr, user_msg, &msg_len); break; case QAIC_TRANS_ACTIVATE_FROM_DEV:
ret = decode_activate(qdev, trans_hdr, user_msg, &msg_len, resources, usr); break; case QAIC_TRANS_DEACTIVATE_FROM_DEV:
ret = decode_deactivate(qdev, trans_hdr, &msg_len, usr); break; case QAIC_TRANS_STATUS_FROM_DEV:
ret = decode_status(qdev, trans_hdr, user_msg, &msg_len, msg); break; default: return -EINVAL;
}
if (ret) return ret;
}
if (msg_len != (msg_hdr_len - sizeof(msg->hdr))) return -EINVAL;
return 0;
}
staticvoid *msg_xfer(struct qaic_device *qdev, struct wrapper_list *wrappers, u32 seq_num, bool ignore_signal)
{ struct xfer_queue_elem elem; struct wire_msg *out_buf; struct wrapper_msg *w; long ret = -EAGAIN; int xfer_count = 0; int retry_count;
/* Allow QAIC_BOOT state since we need to check control protocol version */ if (qdev->dev_state == QAIC_OFFLINE) {
mutex_unlock(&qdev->cntl_mutex); return ERR_PTR(-ENODEV);
}
/* Attempt to avoid a partial commit of a message */
list_for_each_entry(w, &wrappers->list, list)
xfer_count++;
for (retry_count = 0; retry_count < QAIC_MHI_RETRY_MAX; retry_count++) { if (xfer_count <= mhi_get_free_desc_count(qdev->cntl_ch, DMA_TO_DEVICE)) {
ret = 0; break;
}
msleep_interruptible(QAIC_MHI_RETRY_WAIT_MS); if (signal_pending(current)) break;
}
if (ret) {
mutex_unlock(&qdev->cntl_mutex); return ERR_PTR(ret);
}
elem.seq_num = seq_num;
elem.buf = NULL;
init_completion(&elem.xfer_done); if (likely(!qdev->cntl_lost_buf)) { /* * The max size of request to device is QAIC_MANAGE_EXT_MSG_LENGTH. * The max size of response from device is QAIC_MANAGE_MAX_MSG_LENGTH.
*/
out_buf = kmalloc(QAIC_MANAGE_MAX_MSG_LENGTH, GFP_KERNEL); if (!out_buf) {
mutex_unlock(&qdev->cntl_mutex); return ERR_PTR(-ENOMEM);
}
ret = mhi_queue_buf(qdev->cntl_ch, DMA_FROM_DEVICE, out_buf,
QAIC_MANAGE_MAX_MSG_LENGTH, MHI_EOT); if (ret) {
mutex_unlock(&qdev->cntl_mutex); return ERR_PTR(ret);
}
} else { /* * we lost a buffer because we queued a recv buf, but then * queuing the corresponding tx buf failed. To try to avoid * a memory leak, lets reclaim it and use it for this * transaction.
*/
qdev->cntl_lost_buf = false;
}
if (ignore_signal)
ret = wait_for_completion_timeout(&elem.xfer_done, control_resp_timeout_s * HZ); else
ret = wait_for_completion_interruptible_timeout(&elem.xfer_done,
control_resp_timeout_s * HZ); /* * not using _interruptable because we have to cleanup or we'll * likely cause memory corruption
*/
mutex_lock(&qdev->cntl_mutex); if (!list_empty(&elem.list))
list_del(&elem.list); if (!ret && !elem.buf)
ret = -ETIMEDOUT; elseif (ret > 0 && !elem.buf)
ret = -EIO;
mutex_unlock(&qdev->cntl_mutex);
/* Remove all but the first wrapper which has the msg header */
list_for_each_entry_safe(wrapper, w, &wrappers->list, list) if (!list_is_first(&wrapper->list, &wrappers->list))
kref_put(&wrapper->ref_count, free_wrapper);
ret = encode_message(qdev, user_msg, wrappers, resources, usr); if (ret && resources->dma_chunk_id)
ret = abort_dma_cont(qdev, wrappers, resources->dma_chunk_id); if (ret) goto encode_failed;
ret = mutex_lock_interruptible(&qdev->cntl_mutex); if (ret) goto lock_failed;
if (copy_from_user(msg->data, user_data, user_msg->len)) {
ret = -EFAULT; goto free_msg;
}
ret = qaic_manage(qdev, usr, msg);
/* * If the qaic_manage() is successful then we copy the message onto * userspace memory but we have an exception for -ECANCELED. * For -ECANCELED, it means that device has NACKed the message with a * status error code which userspace would like to know.
*/ if (ret == -ECANCELED || !ret) { if (copy_to_user(user_data, msg->data, msg->len)) {
ret = -EFAULT;
} else {
user_msg->len = msg->len;
user_msg->count = msg->count;
}
}
ret = qaic_manage(qdev, usr, user_msg); if (ret) goto kfree_user_msg;
status_result = (struct qaic_manage_trans_status_from_dev *)user_msg->data;
*major = status_result->major;
*minor = status_result->minor;
if (status_result->status_flags & BIT(0)) { /* device is using CRC */ /* By default qdev->gen_crc is programmed to generate CRC */
qdev->valid_crc = valid_crc;
} else { /* By default qdev->valid_crc is programmed to bypass CRC */
qdev->gen_crc = gen_crc_stub;
}
int qaic_control_open(struct qaic_device *qdev)
{ if (!qdev->cntl_ch) return -ENODEV;
qdev->cntl_lost_buf = false; /* * By default qaic should assume that device has CRC enabled. * Qaic comes to know if device has CRC enabled or disabled during the * device status transaction, which is the first transaction performed * on control channel. * * So CRC validation of first device status transaction response is * ignored (by calling valid_crc_stub) and is done later during decoding * if device has CRC enabled. * Now that qaic knows whether device has CRC enabled or not it acts * accordingly.
*/
qdev->gen_crc = gen_crc;
qdev->valid_crc = valid_crc_stub;
/* * msg_xfer releases the mutex * We don't care about the return of msg_xfer since we will not do * anything different based on what happens. * We ignore pending signals since one will be set if the user is * killed, and we need give the device a chance to cleanup, otherwise * DMA may still be in progress when we return.
*/
rsp = msg_xfer(qdev, wrappers, qdev->next_seq_num - 1, true); if (!IS_ERR(rsp))
kfree(rsp);
free_wrapper_from_list(wrappers, wrapper);
}
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.