spin_lock_irqsave(&connection->lock, flags);
- if (connection->state != GB_CONNECTION_STATE_ENABLED) {
+ if (connection->state != GB_CONNECTION_STATE_ENABLED &&
+ (connection->state != GB_CONNECTION_STATE_ENABLED_TX ||
+ gb_operation_is_incoming(operation))) {
spin_unlock_irqrestore(&connection->lock, flags);
return -ENOTCONN;
}
*/
static void gb_message_cancel(struct gb_message *message)
{
- struct greybus_host_device *hd = message->operation->connection->hd;
+ struct gb_host_device *hd = message->operation->connection->hd;
hd->driver->message_cancel(message);
}
static void gb_operation_request_handle(struct gb_operation *operation)
{
- struct gb_protocol *protocol = operation->connection->protocol;
+ struct gb_connection *connection = operation->connection;
int status;
int ret;
- if (!protocol)
- return;
-
- if (protocol->request_recv) {
- status = protocol->request_recv(operation->type, operation);
+ if (connection->handler) {
+ status = connection->handler(operation);
} else {
- dev_err(&operation->connection->dev,
- "unexpected incoming request type 0x%02hhx\n",
- operation->type);
+ dev_err(&connection->hd->dev,
+ "%s: unexpected incoming request of type 0x%02x\n",
+ connection->name, operation->type);
status = -EPROTONOSUPPORT;
}
ret = gb_operation_response_send(operation, status);
if (ret) {
- dev_err(&operation->connection->dev,
- "failed to send response %d for type 0x%02hhx: %d\n",
- status, operation->type, ret);
- return;
+ dev_err(&connection->hd->dev,
+ "%s: failed to send response %d for type 0x%02x: %d\n",
+ connection->name, status, operation->type, ret);
+ return;
}
}
gb_operation_put(operation);
}
-static void gb_operation_message_init(struct greybus_host_device *hd,
+static void gb_operation_message_init(struct gb_host_device *hd,
struct gb_message *message, u16 operation_id,
size_t payload_size, u8 type)
{
/*
* The type supplied for incoming message buffers will be
- * 0x00. Such buffers will be overwritten by arriving data
- * so there's no need to initialize the message header.
+ * GB_REQUEST_TYPE_INVALID. Such buffers will be overwritten by
+ * arriving data so there's no need to initialize the message header.
*/
- if (type != GB_OPERATION_TYPE_INVALID) {
+ if (type != GB_REQUEST_TYPE_INVALID) {
u16 message_size = (u16)(sizeof(*header) + payload_size);
/*
* message payload / the message size
*/
static struct gb_message *
-gb_operation_message_alloc(struct greybus_host_device *hd, u8 type,
+gb_operation_message_alloc(struct gb_host_device *hd, u8 type,
size_t payload_size, gfp_t gfp_flags)
{
struct gb_message *message;
size_t message_size = payload_size + sizeof(*header);
if (message_size > hd->buffer_size_max) {
- pr_warn("requested message size too big (%zu > %zu)\n",
+ dev_warn(&hd->dev, "requested message size too big (%zu > %zu)\n",
message_size, hd->buffer_size_max);
return NULL;
}
bool gb_operation_response_alloc(struct gb_operation *operation,
size_t response_size, gfp_t gfp)
{
- struct greybus_host_device *hd = operation->connection->hd;
+ struct gb_host_device *hd = operation->connection->hd;
struct gb_operation_msg_hdr *request_header;
struct gb_message *response;
u8 type;
size_t request_size, size_t response_size,
unsigned long op_flags, gfp_t gfp_flags)
{
- struct greybus_host_device *hd = connection->hd;
+ struct gb_host_device *hd = connection->hd;
struct gb_operation *operation;
operation = kmem_cache_zalloc(gb_operation_cache, gfp_flags);
* invalid operation type for all protocols, and this is enforced
* here.
*/
-struct gb_operation *gb_operation_create(struct gb_connection *connection,
- u8 type, size_t request_size,
- size_t response_size,
- gfp_t gfp)
+struct gb_operation *
+gb_operation_create_flags(struct gb_connection *connection,
+ u8 type, size_t request_size,
+ size_t response_size, unsigned long flags,
+ gfp_t gfp)
{
- if (WARN_ON_ONCE(type == GB_OPERATION_TYPE_INVALID))
+ if (WARN_ON_ONCE(type == GB_REQUEST_TYPE_INVALID))
return NULL;
if (WARN_ON_ONCE(type & GB_MESSAGE_TYPE_RESPONSE))
type &= ~GB_MESSAGE_TYPE_RESPONSE;
+ if (WARN_ON_ONCE(flags & ~GB_OPERATION_FLAG_USER_MASK))
+ flags &= GB_OPERATION_FLAG_USER_MASK;
+
return gb_operation_create_common(connection, type,
- request_size, response_size, 0, gfp);
+ request_size, response_size,
+ flags, gfp);
}
-EXPORT_SYMBOL_GPL(gb_operation_create);
+EXPORT_SYMBOL_GPL(gb_operation_create_flags);
size_t gb_operation_get_payload_size_max(struct gb_connection *connection)
{
- struct greybus_host_device *hd = connection->hd;
+ struct gb_host_device *hd = connection->hd;
return hd->buffer_size_max - sizeof(struct gb_operation_msg_hdr);
}
flags |= GB_OPERATION_FLAG_UNIDIRECTIONAL;
operation = gb_operation_create_common(connection, type,
- request_size, 0, flags, GFP_ATOMIC);
+ request_size,
+ GB_REQUEST_TYPE_INVALID,
+ flags, GFP_ATOMIC);
if (!operation)
return NULL;
complete(&operation->completion);
}
-/*
- * Send an operation request message. The caller has filled in any payload so
- * the request message is ready to go. The callback function supplied will be
- * called when the response message has arrived indicating the operation is
- * complete. In that case, the callback function is responsible for fetching
- * the result of the operation using gb_operation_result() if desired, and
- * dropping the initial reference to the operation.
+/**
+ * gb_operation_request_send() - send an operation request message
+ * @operation: the operation to initiate
+ * @callback: the operation completion callback
+ * @gfp: the memory flags to use for any allocations
+ *
+ * The caller has filled in any payload so the request message is ready to go.
+ * The callback function supplied will be called when the response message has
+ * arrived, a unidirectional request has been sent, or the operation is
+ * cancelled, indicating that the operation is complete. The callback function
+ * can fetch the result of the operation using gb_operation_result() if
+ * desired.
+ *
+ * Return: 0 if the request was successfully queued in the host-driver queues,
+ * or a negative errno.
*/
int gb_operation_request_send(struct gb_operation *operation,
gb_operation_callback callback,
unsigned int cycle;
int ret;
+ if (gb_connection_is_offloaded(connection))
+ return -EBUSY;
+
if (!callback)
return -EINVAL;
+
/*
* Record the callback function, which is executed in
* non-atomic (workqueue) context when the final result
/*
* Assign the operation's id, and store it in the request header.
- * Zero is a reserved operation id.
+ * Zero is a reserved operation id for unidirectional operations.
*/
- cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
- operation->id = (u16)(cycle % U16_MAX + 1);
+ if (gb_operation_is_unidirectional(operation)) {
+ operation->id = 0;
+ } else {
+ cycle = (unsigned int)atomic_inc_return(&connection->op_cycle);
+ operation->id = (u16)(cycle % U16_MAX + 1);
+ }
+
header = operation->request->header;
header->operation_id = cpu_to_le16(operation->id);
/* Record the result */
if (!gb_operation_result_set(operation, errno)) {
- dev_err(&connection->dev, "request result already set\n");
+ dev_err(&connection->hd->dev, "request result already set\n");
return -EIO; /* Shouldn't happen */
}
/*
* This function is called when a message send request has completed.
*/
-void greybus_message_sent(struct greybus_host_device *hd,
+void greybus_message_sent(struct gb_host_device *hd,
struct gb_message *message, int status)
{
struct gb_operation *operation = message->operation;
* reference to the operation. If an error occurred, report
* it.
*
- * For requests, if there's no error, there's nothing more
- * to do until the response arrives. If an error occurred
- * attempting to send it, record that as the result of
- * the operation and schedule its completion.
+ * For requests, if there's no error and the operation in not
+ * unidirectional, there's nothing more to do until the response
+ * arrives. If an error occurred attempting to send it, or if the
+ * operation is unidrectional, record the result of the operation and
+ * schedule its completion.
*/
if (message == operation->response) {
if (status) {
- dev_err(&connection->dev,
- "error sending response type 0x%02hhx: %d\n",
- operation->type, status);
+ dev_err(&connection->hd->dev,
+ "%s: error sending response 0x%02x: %d\n",
+ connection->name, operation->type, status);
}
+
gb_operation_put_active(operation);
gb_operation_put(operation);
- } else if (status) {
+ } else if (status || gb_operation_is_unidirectional(operation)) {
if (gb_operation_result_set(operation, status)) {
queue_work(gb_operation_completion_wq,
&operation->work);
operation = gb_operation_create_incoming(connection, operation_id,
type, data, size);
if (!operation) {
- dev_err(&connection->dev, "can't create operation\n");
- return; /* XXX Respond with pre-allocated ENOMEM */
+ dev_err(&connection->hd->dev,
+ "%s: can't create incoming operation\n",
+ connection->name);
+ return;
}
ret = gb_operation_get_active(operation);
static void gb_connection_recv_response(struct gb_connection *connection,
u16 operation_id, u8 result, void *data, size_t size)
{
+ struct gb_operation_msg_hdr *header;
struct gb_operation *operation;
struct gb_message *message;
int errno = gb_operation_status_map(result);
size_t message_size;
+ if (!operation_id) {
+ dev_err_ratelimited(&connection->hd->dev,
+ "%s: invalid response id 0 received\n",
+ connection->name);
+ return;
+ }
+
operation = gb_operation_find_outgoing(connection, operation_id);
if (!operation) {
- dev_err(&connection->dev, "operation not found\n");
+ dev_err_ratelimited(&connection->hd->dev,
+ "%s: unexpected response id 0x%04x received\n",
+ connection->name, operation_id);
return;
}
message = operation->response;
- message_size = sizeof(*message->header) + message->payload_size;
- if (!errno && size != message_size) {
- dev_err(&connection->dev, "bad message (0x%02hhx) size (%zu != %zu)\n",
- message->header->type, size, message_size);
+ header = message->header;
+ message_size = sizeof(*header) + message->payload_size;
+ if (!errno && size > message_size) {
+ dev_err_ratelimited(&connection->hd->dev,
+ "%s: malformed response 0x%02x received (%zu > %zu)\n",
+ connection->name, header->type,
+ size, message_size);
errno = -EMSGSIZE;
+ } else if (!errno && size < message_size) {
+ if (gb_operation_short_response_allowed(operation)) {
+ message->payload_size = size - sizeof(*header);
+ } else {
+ dev_err_ratelimited(&connection->hd->dev,
+ "%s: short response 0x%02x received (%zu < %zu)\n",
+ connection->name, header->type,
+ size, message_size);
+ errno = -EMSGSIZE;
+ }
}
trace_gb_message_recv_response(operation->response);
/* We must ignore the payload if a bad status is returned */
if (errno)
- size = sizeof(*message->header);
+ size = sizeof(*header);
/* The rest will be handled in work queue context */
if (gb_operation_result_set(operation, errno)) {
- memcpy(message->header, data, size);
+ memcpy(header, data, size);
queue_work(gb_operation_completion_wq, &operation->work);
}
void *data, size_t size)
{
struct gb_operation_msg_hdr header;
+ struct device *dev = &connection->hd->dev;
size_t msg_size;
u16 operation_id;
- if (connection->state != GB_CONNECTION_STATE_ENABLED) {
- dev_err(&connection->dev, "dropping %zu received bytes\n",
- size);
+ if ((connection->state != GB_CONNECTION_STATE_ENABLED &&
+ connection->state != GB_CONNECTION_STATE_ENABLED_TX) ||
+ gb_connection_is_offloaded(connection)) {
+ dev_warn_ratelimited(dev, "%s: dropping %zu received bytes\n",
+ connection->name, size);
return;
}
if (size < sizeof(header)) {
- dev_err(&connection->dev, "message too small\n");
+ dev_err_ratelimited(dev, "%s: short message received\n",
+ connection->name);
return;
}
memcpy(&header, data, sizeof(header));
msg_size = le16_to_cpu(header.size);
if (size < msg_size) {
- dev_err(&connection->dev,
- "incomplete message received for type 0x%02hhx: 0x%04x (%zu < %zu)\n",
- header.type, le16_to_cpu(header.operation_id), size,
- msg_size);
+ dev_err_ratelimited(dev,
+ "%s: incomplete message 0x%04x of type 0x%02x received (%zu < %zu)\n",
+ connection->name,
+ le16_to_cpu(header.operation_id),
+ header.type, size, msg_size);
return; /* XXX Should still complete operation */
}
}
/**
- * gb_operation_sync: implement a "simple" synchronous gb operation.
+ * gb_operation_sync_timeout() - implement a "simple" synchronous operation
* @connection: the Greybus connection to send this to
* @type: the type of operation to send
* @request: pointer to a memory buffer to copy the request from
ret = gb_operation_request_send_sync_timeout(operation, timeout);
if (ret) {
- dev_err(&connection->dev, "synchronous operation failed: 0x%02hhx (%d)\n",
- type, ret);
+ dev_err(&connection->hd->dev,
+ "%s: synchronous operation of type 0x%02x failed: %d\n",
+ connection->name, type, ret);
} else {
if (response_size) {
memcpy(response, operation->response->payload,
response_size);
}
}
- gb_operation_destroy(operation);
+
+ gb_operation_put(operation);
return ret;
}
EXPORT_SYMBOL_GPL(gb_operation_sync_timeout);
+/**
+ * gb_operation_unidirectional_timeout() - initiate a unidirectional operation
+ * @connection: connection to use
+ * @type: type of operation to send
+ * @request: memory buffer to copy the request from
+ * @request_size: size of @request
+ * @timeout: send timeout in milliseconds
+ *
+ * Initiate a unidirectional operation by sending a request message and
+ * waiting for it to be acknowledged as sent by the host device.
+ *
+ * Note that successful send of a unidirectional operation does not imply that
+ * the request as actually reached the remote end of the connection.
+ */
+int gb_operation_unidirectional_timeout(struct gb_connection *connection,
+ int type, void *request, int request_size,
+ unsigned int timeout)
+{
+ struct gb_operation *operation;
+ int ret;
+
+ if (request_size && !request)
+ return -EINVAL;
+
+ operation = gb_operation_create_flags(connection, type,
+ request_size, 0,
+ GB_OPERATION_FLAG_UNIDIRECTIONAL,
+ GFP_KERNEL);
+ if (!operation)
+ return -ENOMEM;
+
+ if (request_size)
+ memcpy(operation->request->payload, request, request_size);
+
+ ret = gb_operation_request_send_sync_timeout(operation, timeout);
+ if (ret) {
+ dev_err(&connection->hd->dev,
+ "%s: unidirectional operation of type 0x%02x failed: %d\n",
+ connection->name, type, ret);
+ }
+
+ gb_operation_put(operation);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gb_operation_unidirectional_timeout);
+
int __init gb_operation_init(void)
{
gb_message_cache = kmem_cache_create("gb_message_cache",