1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2010-2012 Broadcom. All rights reserved. */
4 #include <linux/types.h>
5 #include <linux/completion.h>
6 #include <linux/mutex.h>
7 #include <linux/bitops.h>
8 #include <linux/kthread.h>
9 #include <linux/wait.h>
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/kref.h>
13 #include <linux/rcupdate.h>
14 #include <linux/sched/signal.h>
16 #include "vchiq_core.h"
18 #define VCHIQ_SLOT_HANDLER_STACK 8192
20 #define HANDLE_STATE_SHIFT 12
22 #define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
23 #define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
24 #define SLOT_INDEX_FROM_DATA(state, data) \
25 (((unsigned int)((char *)data - (char *)state->slot_data)) / \
27 #define SLOT_INDEX_FROM_INFO(state, info) \
28 ((unsigned int)(info - state->slot_info))
29 #define SLOT_QUEUE_INDEX_FROM_POS(pos) \
30 ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
31 #define SLOT_QUEUE_INDEX_FROM_POS_MASKED(pos) \
32 (SLOT_QUEUE_INDEX_FROM_POS(pos) & VCHIQ_SLOT_QUEUE_MASK)
34 #define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
36 #define SRVTRACE_LEVEL(srv) \
37 (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
38 #define SRVTRACE_ENABLED(srv, lev) \
39 (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
41 struct vchiq_open_payload {
48 struct vchiq_openack_payload {
53 QMFLAGS_IS_BLOCKING = BIT(0),
54 QMFLAGS_NO_MUTEX_LOCK = BIT(1),
55 QMFLAGS_NO_MUTEX_UNLOCK = BIT(2)
58 /* we require this for consistency between endpoints */
59 vchiq_static_assert(sizeof(struct vchiq_header) == 8);
60 vchiq_static_assert(IS_POW2(sizeof(struct vchiq_header)));
61 vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
62 vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
63 vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
64 vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
66 /* Run time control of log level, based on KERN_XXX level. */
67 int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
68 int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
69 int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
71 DEFINE_SPINLOCK(bulk_waiter_spinlock);
72 static DEFINE_SPINLOCK(quota_spinlock);
74 struct vchiq_state *vchiq_states[VCHIQ_MAX_STATES];
75 static unsigned int handle_seq;
77 static const char *const srvstate_names[] = {
90 static const char *const reason_names[] = {
96 "BULK_TRANSMIT_ABORTED",
97 "BULK_RECEIVE_ABORTED"
100 static const char *const conn_state_names[] = {
113 release_message_sync(struct vchiq_state *state, struct vchiq_header *header);
115 static const char *msg_type_str(unsigned int msg_type)
118 case VCHIQ_MSG_PADDING: return "PADDING";
119 case VCHIQ_MSG_CONNECT: return "CONNECT";
120 case VCHIQ_MSG_OPEN: return "OPEN";
121 case VCHIQ_MSG_OPENACK: return "OPENACK";
122 case VCHIQ_MSG_CLOSE: return "CLOSE";
123 case VCHIQ_MSG_DATA: return "DATA";
124 case VCHIQ_MSG_BULK_RX: return "BULK_RX";
125 case VCHIQ_MSG_BULK_TX: return "BULK_TX";
126 case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
127 case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
128 case VCHIQ_MSG_PAUSE: return "PAUSE";
129 case VCHIQ_MSG_RESUME: return "RESUME";
130 case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
131 case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
132 case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
138 vchiq_set_service_state(struct vchiq_service *service, int newstate)
140 vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
141 service->state->id, service->localport,
142 srvstate_names[service->srvstate],
143 srvstate_names[newstate]);
144 service->srvstate = newstate;
147 struct vchiq_service *
148 find_service_by_handle(unsigned int handle)
150 struct vchiq_service *service;
153 service = handle_to_service(handle);
154 if (service && service->srvstate != VCHIQ_SRVSTATE_FREE &&
155 service->handle == handle &&
156 kref_get_unless_zero(&service->ref_count)) {
157 service = rcu_pointer_handoff(service);
162 vchiq_log_info(vchiq_core_log_level,
163 "Invalid service handle 0x%x", handle);
167 struct vchiq_service *
168 find_service_by_port(struct vchiq_state *state, int localport)
171 if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
172 struct vchiq_service *service;
175 service = rcu_dereference(state->services[localport]);
176 if (service && service->srvstate != VCHIQ_SRVSTATE_FREE &&
177 kref_get_unless_zero(&service->ref_count)) {
178 service = rcu_pointer_handoff(service);
184 vchiq_log_info(vchiq_core_log_level,
185 "Invalid port %d", localport);
189 struct vchiq_service *
190 find_service_for_instance(struct vchiq_instance *instance,
193 struct vchiq_service *service;
196 service = handle_to_service(handle);
197 if (service && service->srvstate != VCHIQ_SRVSTATE_FREE &&
198 service->handle == handle &&
199 service->instance == instance &&
200 kref_get_unless_zero(&service->ref_count)) {
201 service = rcu_pointer_handoff(service);
206 vchiq_log_info(vchiq_core_log_level,
207 "Invalid service handle 0x%x", handle);
211 struct vchiq_service *
212 find_closed_service_for_instance(struct vchiq_instance *instance,
215 struct vchiq_service *service;
218 service = handle_to_service(handle);
220 (service->srvstate == VCHIQ_SRVSTATE_FREE ||
221 service->srvstate == VCHIQ_SRVSTATE_CLOSED) &&
222 service->handle == handle &&
223 service->instance == instance &&
224 kref_get_unless_zero(&service->ref_count)) {
225 service = rcu_pointer_handoff(service);
230 vchiq_log_info(vchiq_core_log_level,
231 "Invalid service handle 0x%x", handle);
235 struct vchiq_service *
236 __next_service_by_instance(struct vchiq_state *state,
237 struct vchiq_instance *instance,
240 struct vchiq_service *service = NULL;
243 while (idx < state->unused_service) {
244 struct vchiq_service *srv;
246 srv = rcu_dereference(state->services[idx++]);
247 if (srv && srv->srvstate != VCHIQ_SRVSTATE_FREE &&
248 srv->instance == instance) {
258 struct vchiq_service *
259 next_service_by_instance(struct vchiq_state *state,
260 struct vchiq_instance *instance,
263 struct vchiq_service *service;
267 service = __next_service_by_instance(state, instance, pidx);
270 if (kref_get_unless_zero(&service->ref_count)) {
271 service = rcu_pointer_handoff(service);
280 lock_service(struct vchiq_service *service)
283 WARN(1, "%s service is NULL\n", __func__);
286 kref_get(&service->ref_count);
289 static void service_release(struct kref *kref)
291 struct vchiq_service *service =
292 container_of(kref, struct vchiq_service, ref_count);
293 struct vchiq_state *state = service->state;
295 WARN_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
296 rcu_assign_pointer(state->services[service->localport], NULL);
297 if (service->userdata_term)
298 service->userdata_term(service->base.userdata);
299 kfree_rcu(service, rcu);
303 unlock_service(struct vchiq_service *service)
306 WARN(1, "%s: service is NULL\n", __func__);
309 kref_put(&service->ref_count, service_release);
313 vchiq_get_client_id(unsigned int handle)
315 struct vchiq_service *service;
319 service = handle_to_service(handle);
320 id = service ? service->client_id : 0;
326 vchiq_get_service_userdata(unsigned int handle)
329 struct vchiq_service *service;
332 service = handle_to_service(handle);
333 userdata = service ? service->base.userdata : NULL;
337 EXPORT_SYMBOL(vchiq_get_service_userdata);
340 mark_service_closing_internal(struct vchiq_service *service, int sh_thread)
342 struct vchiq_state *state = service->state;
343 struct vchiq_service_quota *quota;
345 service->closing = 1;
347 /* Synchronise with other threads. */
348 mutex_lock(&state->recycle_mutex);
349 mutex_unlock(&state->recycle_mutex);
350 if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
352 * If we're pausing then the slot_mutex is held until resume
353 * by the slot handler. Therefore don't try to acquire this
354 * mutex if we're the slot handler and in the pause sent state.
355 * We don't need to in this case anyway.
357 mutex_lock(&state->slot_mutex);
358 mutex_unlock(&state->slot_mutex);
361 /* Unblock any sending thread. */
362 quota = &state->service_quotas[service->localport];
363 complete("a->quota_event);
367 mark_service_closing(struct vchiq_service *service)
369 mark_service_closing_internal(service, 0);
372 static inline enum vchiq_status
373 make_service_callback(struct vchiq_service *service, enum vchiq_reason reason,
374 struct vchiq_header *header, void *bulk_userdata)
376 enum vchiq_status status;
378 vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %pK, %pK)",
379 service->state->id, service->localport, reason_names[reason],
380 header, bulk_userdata);
381 status = service->base.callback(reason, header, service->handle,
383 if (status == VCHIQ_ERROR) {
384 vchiq_log_warning(vchiq_core_log_level,
385 "%d: ignoring ERROR from callback to service %x",
386 service->state->id, service->handle);
387 status = VCHIQ_SUCCESS;
390 if (reason != VCHIQ_MESSAGE_AVAILABLE)
391 vchiq_release_message(service->handle, header);
397 vchiq_set_conn_state(struct vchiq_state *state, enum vchiq_connstate newstate)
399 enum vchiq_connstate oldstate = state->conn_state;
401 vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
402 conn_state_names[oldstate],
403 conn_state_names[newstate]);
404 state->conn_state = newstate;
405 vchiq_platform_conn_state_changed(state, oldstate, newstate);
409 remote_event_create(wait_queue_head_t *wq, struct remote_event *event)
413 * Don't clear the 'fired' flag because it may already have been set
416 init_waitqueue_head(wq);
420 * All the event waiting routines in VCHIQ used a custom semaphore
421 * implementation that filtered most signals. This achieved a behaviour similar
422 * to the "killable" family of functions. While cleaning up this code all the
423 * routines where switched to the "interruptible" family of functions, as the
424 * former was deemed unjustified and the use "killable" set all VCHIQ's
425 * threads in D state.
428 remote_event_wait(wait_queue_head_t *wq, struct remote_event *event)
433 if (wait_event_interruptible(*wq, event->fired)) {
446 remote_event_signal_local(wait_queue_head_t *wq, struct remote_event *event)
454 remote_event_poll(wait_queue_head_t *wq, struct remote_event *event)
456 if (event->fired && event->armed)
457 remote_event_signal_local(wq, event);
461 remote_event_pollall(struct vchiq_state *state)
463 remote_event_poll(&state->sync_trigger_event, &state->local->sync_trigger);
464 remote_event_poll(&state->sync_release_event, &state->local->sync_release);
465 remote_event_poll(&state->trigger_event, &state->local->trigger);
466 remote_event_poll(&state->recycle_event, &state->local->recycle);
470 * Round up message sizes so that any space at the end of a slot is always big
471 * enough for a header. This relies on header size being a power of two, which
472 * has been verified earlier by a static assertion.
476 calc_stride(size_t size)
478 /* Allow room for the header */
479 size += sizeof(struct vchiq_header);
482 return (size + sizeof(struct vchiq_header) - 1) &
483 ~(sizeof(struct vchiq_header) - 1);
486 /* Called by the slot handler thread */
487 static struct vchiq_service *
488 get_listening_service(struct vchiq_state *state, int fourcc)
492 WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
495 for (i = 0; i < state->unused_service; i++) {
496 struct vchiq_service *service;
498 service = rcu_dereference(state->services[i]);
500 service->public_fourcc == fourcc &&
501 (service->srvstate == VCHIQ_SRVSTATE_LISTENING ||
502 (service->srvstate == VCHIQ_SRVSTATE_OPEN &&
503 service->remoteport == VCHIQ_PORT_FREE)) &&
504 kref_get_unless_zero(&service->ref_count)) {
505 service = rcu_pointer_handoff(service);
514 /* Called by the slot handler thread */
515 static struct vchiq_service *
516 get_connected_service(struct vchiq_state *state, unsigned int port)
521 for (i = 0; i < state->unused_service; i++) {
522 struct vchiq_service *service =
523 rcu_dereference(state->services[i]);
525 if (service && service->srvstate == VCHIQ_SRVSTATE_OPEN &&
526 service->remoteport == port &&
527 kref_get_unless_zero(&service->ref_count)) {
528 service = rcu_pointer_handoff(service);
538 request_poll(struct vchiq_state *state, struct vchiq_service *service,
548 value = atomic_read(&service->poll_flags);
549 } while (atomic_cmpxchg(&service->poll_flags, value,
550 value | BIT(poll_type)) != value);
552 index = BITSET_WORD(service->localport);
554 value = atomic_read(&state->poll_services[index]);
555 } while (atomic_cmpxchg(&state->poll_services[index],
556 value, value | BIT(service->localport & 0x1f)) != value);
559 state->poll_needed = 1;
562 /* ... and ensure the slot handler runs. */
563 remote_event_signal_local(&state->trigger_event, &state->local->trigger);
567 * Called from queue_message, by the slot handler and application threads,
568 * with slot_mutex held
570 static struct vchiq_header *
571 reserve_space(struct vchiq_state *state, size_t space, int is_blocking)
573 struct vchiq_shared_state *local = state->local;
574 int tx_pos = state->local_tx_pos;
575 int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
577 if (space > slot_space) {
578 struct vchiq_header *header;
579 /* Fill the remaining space with padding */
580 WARN_ON(!state->tx_data);
581 header = (struct vchiq_header *)
582 (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
583 header->msgid = VCHIQ_MSGID_PADDING;
584 header->size = slot_space - sizeof(struct vchiq_header);
586 tx_pos += slot_space;
589 /* If necessary, get the next slot. */
590 if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
593 /* If there is no free slot... */
595 if (!try_wait_for_completion(&state->slot_available_event)) {
596 /* ...wait for one. */
598 VCHIQ_STATS_INC(state, slot_stalls);
600 /* But first, flush through the last slot. */
601 state->local_tx_pos = tx_pos;
602 local->tx_pos = tx_pos;
603 remote_event_signal(&state->remote->trigger);
606 (wait_for_completion_interruptible(
607 &state->slot_available_event)))
608 return NULL; /* No space available */
611 if (tx_pos == (state->slot_queue_available * VCHIQ_SLOT_SIZE)) {
612 complete(&state->slot_available_event);
613 pr_warn("%s: invalid tx_pos: %d\n", __func__, tx_pos);
617 slot_index = local->slot_queue[
618 SLOT_QUEUE_INDEX_FROM_POS_MASKED(tx_pos)];
620 (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
623 state->local_tx_pos = tx_pos + space;
625 return (struct vchiq_header *)(state->tx_data +
626 (tx_pos & VCHIQ_SLOT_MASK));
629 /* Called by the recycle thread. */
631 process_free_queue(struct vchiq_state *state, BITSET_T *service_found,
634 struct vchiq_shared_state *local = state->local;
635 int slot_queue_available;
638 * Find slots which have been freed by the other side, and return them
639 * to the available queue.
641 slot_queue_available = state->slot_queue_available;
644 * Use a memory barrier to ensure that any state that may have been
645 * modified by another thread is not masked by stale prefetched
650 while (slot_queue_available != local->slot_queue_recycle) {
652 int slot_index = local->slot_queue[slot_queue_available++ &
653 VCHIQ_SLOT_QUEUE_MASK];
654 char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
658 * Beware of the address dependency - data is calculated
659 * using an index written by the other side.
663 vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%pK %x %x",
664 state->id, slot_index, data,
665 local->slot_queue_recycle, slot_queue_available);
667 /* Initialise the bitmask for services which have used this slot */
668 memset(service_found, 0, length);
672 while (pos < VCHIQ_SLOT_SIZE) {
673 struct vchiq_header *header =
674 (struct vchiq_header *)(data + pos);
675 int msgid = header->msgid;
677 if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
678 int port = VCHIQ_MSG_SRCPORT(msgid);
679 struct vchiq_service_quota *quota =
680 &state->service_quotas[port];
683 spin_lock("a_spinlock);
684 count = quota->message_use_count;
686 quota->message_use_count =
688 spin_unlock("a_spinlock);
690 if (count == quota->message_quota)
692 * Signal the service that it
693 * has dropped below its quota
695 complete("a->quota_event);
696 else if (count == 0) {
697 vchiq_log_error(vchiq_core_log_level,
698 "service %d message_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
700 quota->message_use_count,
701 header, msgid, header->msgid,
703 WARN(1, "invalid message use count\n");
705 if (!BITSET_IS_SET(service_found, port)) {
706 /* Set the found bit for this service */
707 BITSET_SET(service_found, port);
709 spin_lock("a_spinlock);
710 count = quota->slot_use_count;
712 quota->slot_use_count =
714 spin_unlock("a_spinlock);
718 * Signal the service in case
719 * it has dropped below its quota
721 complete("a->quota_event);
723 vchiq_core_log_level,
724 "%d: pfq:%d %x@%pK - slot_use->%d",
726 header->size, header,
730 vchiq_core_log_level,
731 "service %d slot_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
733 msgid, header->msgid,
735 WARN(1, "bad slot use count\n");
742 pos += calc_stride(header->size);
743 if (pos > VCHIQ_SLOT_SIZE) {
744 vchiq_log_error(vchiq_core_log_level,
745 "pfq - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
746 pos, header, msgid, header->msgid,
748 WARN(1, "invalid slot position\n");
755 spin_lock("a_spinlock);
756 count = state->data_use_count;
758 state->data_use_count =
760 spin_unlock("a_spinlock);
761 if (count == state->data_quota)
762 complete(&state->data_quota_event);
766 * Don't allow the slot to be reused until we are no
767 * longer interested in it.
771 state->slot_queue_available = slot_queue_available;
772 complete(&state->slot_available_event);
777 memcpy_copy_callback(
778 void *context, void *dest,
779 size_t offset, size_t maxsize)
781 memcpy(dest + offset, context + offset, maxsize);
787 ssize_t (*copy_callback)(void *context, void *dest,
788 size_t offset, size_t maxsize),
796 ssize_t callback_result;
797 size_t max_bytes = size - pos;
800 copy_callback(context, dest + pos,
803 if (callback_result < 0)
804 return callback_result;
806 if (!callback_result)
809 if (callback_result > max_bytes)
812 pos += callback_result;
818 /* Called by the slot handler and application threads */
819 static enum vchiq_status
820 queue_message(struct vchiq_state *state, struct vchiq_service *service,
822 ssize_t (*copy_callback)(void *context, void *dest,
823 size_t offset, size_t maxsize),
824 void *context, size_t size, int flags)
826 struct vchiq_shared_state *local;
827 struct vchiq_service_quota *quota = NULL;
828 struct vchiq_header *header;
829 int type = VCHIQ_MSG_TYPE(msgid);
833 local = state->local;
835 stride = calc_stride(size);
837 WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
839 if (!(flags & QMFLAGS_NO_MUTEX_LOCK) &&
840 mutex_lock_killable(&state->slot_mutex))
843 if (type == VCHIQ_MSG_DATA) {
847 WARN(1, "%s: service is NULL\n", __func__);
848 mutex_unlock(&state->slot_mutex);
852 WARN_ON(flags & (QMFLAGS_NO_MUTEX_LOCK |
853 QMFLAGS_NO_MUTEX_UNLOCK));
855 if (service->closing) {
856 /* The service has been closed */
857 mutex_unlock(&state->slot_mutex);
861 quota = &state->service_quotas[service->localport];
863 spin_lock("a_spinlock);
866 * Ensure this service doesn't use more than its quota of
869 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
870 state->local_tx_pos + stride - 1);
873 * Ensure data messages don't use more than their quota of
876 while ((tx_end_index != state->previous_data_index) &&
877 (state->data_use_count == state->data_quota)) {
878 VCHIQ_STATS_INC(state, data_stalls);
879 spin_unlock("a_spinlock);
880 mutex_unlock(&state->slot_mutex);
882 if (wait_for_completion_interruptible(
883 &state->data_quota_event))
886 mutex_lock(&state->slot_mutex);
887 spin_lock("a_spinlock);
888 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
889 state->local_tx_pos + stride - 1);
890 if ((tx_end_index == state->previous_data_index) ||
891 (state->data_use_count < state->data_quota)) {
892 /* Pass the signal on to other waiters */
893 complete(&state->data_quota_event);
898 while ((quota->message_use_count == quota->message_quota) ||
899 ((tx_end_index != quota->previous_tx_index) &&
900 (quota->slot_use_count ==
901 quota->slot_quota))) {
902 spin_unlock("a_spinlock);
903 vchiq_log_trace(vchiq_core_log_level,
904 "%d: qm:%d %s,%zx - quota stall (msg %d, slot %d)",
905 state->id, service->localport,
906 msg_type_str(type), size,
907 quota->message_use_count,
908 quota->slot_use_count);
909 VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
910 mutex_unlock(&state->slot_mutex);
911 if (wait_for_completion_interruptible(
912 "a->quota_event))
914 if (service->closing)
916 if (mutex_lock_killable(&state->slot_mutex))
918 if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
919 /* The service has been closed */
920 mutex_unlock(&state->slot_mutex);
923 spin_lock("a_spinlock);
924 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
925 state->local_tx_pos + stride - 1);
928 spin_unlock("a_spinlock);
931 header = reserve_space(state, stride, flags & QMFLAGS_IS_BLOCKING);
935 VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
937 * In the event of a failure, return the mutex to the
940 if (!(flags & QMFLAGS_NO_MUTEX_LOCK))
941 mutex_unlock(&state->slot_mutex);
945 if (type == VCHIQ_MSG_DATA) {
946 ssize_t callback_result;
950 vchiq_log_info(vchiq_core_log_level,
951 "%d: qm %s@%pK,%zx (%d->%d)",
952 state->id, msg_type_str(VCHIQ_MSG_TYPE(msgid)),
953 header, size, VCHIQ_MSG_SRCPORT(msgid),
954 VCHIQ_MSG_DSTPORT(msgid));
956 WARN_ON(flags & (QMFLAGS_NO_MUTEX_LOCK |
957 QMFLAGS_NO_MUTEX_UNLOCK));
960 copy_message_data(copy_callback, context,
963 if (callback_result < 0) {
964 mutex_unlock(&state->slot_mutex);
965 VCHIQ_SERVICE_STATS_INC(service,
970 if (SRVTRACE_ENABLED(service,
972 vchiq_log_dump_mem("Sent", 0,
975 (size_t)callback_result));
977 spin_lock("a_spinlock);
978 quota->message_use_count++;
981 SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
984 * If this transmission can't fit in the last slot used by any
985 * service, the data_use_count must be increased.
987 if (tx_end_index != state->previous_data_index) {
988 state->previous_data_index = tx_end_index;
989 state->data_use_count++;
993 * If this isn't the same slot last used by this service,
994 * the service's slot_use_count must be increased.
996 if (tx_end_index != quota->previous_tx_index) {
997 quota->previous_tx_index = tx_end_index;
998 slot_use_count = ++quota->slot_use_count;
1003 spin_unlock("a_spinlock);
1006 vchiq_log_trace(vchiq_core_log_level,
1007 "%d: qm:%d %s,%zx - slot_use->%d (hdr %p)",
1008 state->id, service->localport,
1009 msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
1010 slot_use_count, header);
1012 VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
1013 VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
1015 vchiq_log_info(vchiq_core_log_level,
1016 "%d: qm %s@%pK,%zx (%d->%d)", state->id,
1017 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1018 header, size, VCHIQ_MSG_SRCPORT(msgid),
1019 VCHIQ_MSG_DSTPORT(msgid));
1022 * It is assumed for now that this code path
1023 * only happens from calls inside this file.
1025 * External callers are through the vchiq_queue_message
1026 * path which always sets the type to be VCHIQ_MSG_DATA
1028 * At first glance this appears to be correct but
1029 * more review is needed.
1031 copy_message_data(copy_callback, context,
1032 header->data, size);
1034 VCHIQ_STATS_INC(state, ctrl_tx_count);
1037 header->msgid = msgid;
1038 header->size = size;
1043 svc_fourcc = service
1044 ? service->base.fourcc
1045 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1047 vchiq_log_info(SRVTRACE_LEVEL(service),
1048 "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%zu",
1049 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1050 VCHIQ_MSG_TYPE(msgid),
1051 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1052 VCHIQ_MSG_SRCPORT(msgid),
1053 VCHIQ_MSG_DSTPORT(msgid),
1057 /* Make sure the new header is visible to the peer. */
1060 /* Make the new tx_pos visible to the peer. */
1061 local->tx_pos = state->local_tx_pos;
1064 if (service && (type == VCHIQ_MSG_CLOSE))
1065 vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
1067 if (!(flags & QMFLAGS_NO_MUTEX_UNLOCK))
1068 mutex_unlock(&state->slot_mutex);
1070 remote_event_signal(&state->remote->trigger);
1072 return VCHIQ_SUCCESS;
1075 /* Called by the slot handler and application threads */
1076 static enum vchiq_status
1077 queue_message_sync(struct vchiq_state *state, struct vchiq_service *service,
1079 ssize_t (*copy_callback)(void *context, void *dest,
1080 size_t offset, size_t maxsize),
1081 void *context, int size, int is_blocking)
1083 struct vchiq_shared_state *local;
1084 struct vchiq_header *header;
1085 ssize_t callback_result;
1087 local = state->local;
1089 if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME &&
1090 mutex_lock_killable(&state->sync_mutex))
1093 remote_event_wait(&state->sync_release_event, &local->sync_release);
1097 header = (struct vchiq_header *)SLOT_DATA_FROM_INDEX(state,
1101 int oldmsgid = header->msgid;
1103 if (oldmsgid != VCHIQ_MSGID_PADDING)
1104 vchiq_log_error(vchiq_core_log_level,
1105 "%d: qms - msgid %x, not PADDING",
1106 state->id, oldmsgid);
1109 vchiq_log_info(vchiq_sync_log_level,
1110 "%d: qms %s@%pK,%x (%d->%d)", state->id,
1111 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1112 header, size, VCHIQ_MSG_SRCPORT(msgid),
1113 VCHIQ_MSG_DSTPORT(msgid));
1116 copy_message_data(copy_callback, context,
1117 header->data, size);
1119 if (callback_result < 0) {
1120 mutex_unlock(&state->slot_mutex);
1121 VCHIQ_SERVICE_STATS_INC(service,
1127 if (SRVTRACE_ENABLED(service,
1129 vchiq_log_dump_mem("Sent", 0,
1132 (size_t)callback_result));
1134 VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
1135 VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
1137 VCHIQ_STATS_INC(state, ctrl_tx_count);
1140 header->size = size;
1141 header->msgid = msgid;
1143 if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
1146 svc_fourcc = service
1147 ? service->base.fourcc
1148 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1150 vchiq_log_trace(vchiq_sync_log_level,
1151 "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
1152 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1153 VCHIQ_MSG_TYPE(msgid),
1154 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1155 VCHIQ_MSG_SRCPORT(msgid),
1156 VCHIQ_MSG_DSTPORT(msgid),
1160 remote_event_signal(&state->remote->sync_trigger);
1162 if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
1163 mutex_unlock(&state->sync_mutex);
1165 return VCHIQ_SUCCESS;
1169 claim_slot(struct vchiq_slot_info *slot)
1175 release_slot(struct vchiq_state *state, struct vchiq_slot_info *slot_info,
1176 struct vchiq_header *header, struct vchiq_service *service)
1180 mutex_lock(&state->recycle_mutex);
1183 int msgid = header->msgid;
1185 if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
1186 (service && service->closing)) {
1187 mutex_unlock(&state->recycle_mutex);
1191 /* Rewrite the message header to prevent a double release */
1192 header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
1195 release_count = slot_info->release_count;
1196 slot_info->release_count = ++release_count;
1198 if (release_count == slot_info->use_count) {
1199 int slot_queue_recycle;
1200 /* Add to the freed queue */
1203 * A read barrier is necessary here to prevent speculative
1204 * fetches of remote->slot_queue_recycle from overtaking the
1209 slot_queue_recycle = state->remote->slot_queue_recycle;
1210 state->remote->slot_queue[slot_queue_recycle &
1211 VCHIQ_SLOT_QUEUE_MASK] =
1212 SLOT_INDEX_FROM_INFO(state, slot_info);
1213 state->remote->slot_queue_recycle = slot_queue_recycle + 1;
1214 vchiq_log_info(vchiq_core_log_level,
1215 "%d: %s %d - recycle->%x", state->id, __func__,
1216 SLOT_INDEX_FROM_INFO(state, slot_info),
1217 state->remote->slot_queue_recycle);
1220 * A write barrier is necessary, but remote_event_signal
1223 remote_event_signal(&state->remote->recycle);
1226 mutex_unlock(&state->recycle_mutex);
1229 static inline enum vchiq_reason
1230 get_bulk_reason(struct vchiq_bulk *bulk)
1232 if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
1233 if (bulk->actual == VCHIQ_BULK_ACTUAL_ABORTED)
1234 return VCHIQ_BULK_TRANSMIT_ABORTED;
1236 return VCHIQ_BULK_TRANSMIT_DONE;
1239 if (bulk->actual == VCHIQ_BULK_ACTUAL_ABORTED)
1240 return VCHIQ_BULK_RECEIVE_ABORTED;
1242 return VCHIQ_BULK_RECEIVE_DONE;
1245 /* Called by the slot handler - don't hold the bulk mutex */
1246 static enum vchiq_status
1247 notify_bulks(struct vchiq_service *service, struct vchiq_bulk_queue *queue,
1250 enum vchiq_status status = VCHIQ_SUCCESS;
1252 vchiq_log_trace(vchiq_core_log_level,
1253 "%d: nb:%d %cx - p=%x rn=%x r=%x",
1254 service->state->id, service->localport,
1255 (queue == &service->bulk_tx) ? 't' : 'r',
1256 queue->process, queue->remote_notify, queue->remove);
1258 queue->remote_notify = queue->process;
1260 while (queue->remove != queue->remote_notify) {
1261 struct vchiq_bulk *bulk =
1262 &queue->bulks[BULK_INDEX(queue->remove)];
1265 * Only generate callbacks for non-dummy bulk
1266 * requests, and non-terminated services
1268 if (bulk->data && service->instance) {
1269 if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
1270 if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
1271 VCHIQ_SERVICE_STATS_INC(service,
1273 VCHIQ_SERVICE_STATS_ADD(service,
1277 VCHIQ_SERVICE_STATS_INC(service,
1279 VCHIQ_SERVICE_STATS_ADD(service,
1284 VCHIQ_SERVICE_STATS_INC(service,
1285 bulk_aborted_count);
1287 if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
1288 struct bulk_waiter *waiter;
1290 spin_lock(&bulk_waiter_spinlock);
1291 waiter = bulk->userdata;
1293 waiter->actual = bulk->actual;
1294 complete(&waiter->event);
1296 spin_unlock(&bulk_waiter_spinlock);
1297 } else if (bulk->mode ==
1298 VCHIQ_BULK_MODE_CALLBACK) {
1299 enum vchiq_reason reason =
1300 get_bulk_reason(bulk);
1301 status = make_service_callback(service,
1302 reason, NULL, bulk->userdata);
1303 if (status == VCHIQ_RETRY)
1309 complete(&service->bulk_remove_event);
1312 status = VCHIQ_SUCCESS;
1314 if (status == VCHIQ_RETRY)
1315 request_poll(service->state, service,
1316 (queue == &service->bulk_tx) ?
1317 VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
1322 /* Called by the slot handler thread */
1324 poll_services(struct vchiq_state *state)
1328 for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
1331 flags = atomic_xchg(&state->poll_services[group], 0);
1332 for (i = 0; flags; i++) {
1333 if (flags & BIT(i)) {
1334 struct vchiq_service *service =
1335 find_service_by_port(state,
1343 atomic_xchg(&service->poll_flags, 0);
1345 BIT(VCHIQ_POLL_REMOVE)) {
1346 vchiq_log_info(vchiq_core_log_level,
1347 "%d: ps - remove %d<->%d",
1348 state->id, service->localport,
1349 service->remoteport);
1352 * Make it look like a client, because
1353 * it must be removed and not left in
1354 * the LISTENING state.
1356 service->public_fourcc =
1357 VCHIQ_FOURCC_INVALID;
1359 if (vchiq_close_service_internal(
1360 service, 0/*!close_recvd*/) !=
1362 request_poll(state, service,
1364 } else if (service_flags &
1365 BIT(VCHIQ_POLL_TERMINATE)) {
1366 vchiq_log_info(vchiq_core_log_level,
1367 "%d: ps - terminate %d<->%d",
1368 state->id, service->localport,
1369 service->remoteport);
1370 if (vchiq_close_service_internal(
1371 service, 0/*!close_recvd*/) !=
1373 request_poll(state, service,
1374 VCHIQ_POLL_TERMINATE);
1376 if (service_flags & BIT(VCHIQ_POLL_TXNOTIFY))
1377 notify_bulks(service,
1380 if (service_flags & BIT(VCHIQ_POLL_RXNOTIFY))
1381 notify_bulks(service,
1384 unlock_service(service);
1390 /* Called with the bulk_mutex held */
1392 abort_outstanding_bulks(struct vchiq_service *service,
1393 struct vchiq_bulk_queue *queue)
1395 int is_tx = (queue == &service->bulk_tx);
1397 vchiq_log_trace(vchiq_core_log_level,
1398 "%d: aob:%d %cx - li=%x ri=%x p=%x",
1399 service->state->id, service->localport, is_tx ? 't' : 'r',
1400 queue->local_insert, queue->remote_insert, queue->process);
1402 WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
1403 WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
1405 while ((queue->process != queue->local_insert) ||
1406 (queue->process != queue->remote_insert)) {
1407 struct vchiq_bulk *bulk =
1408 &queue->bulks[BULK_INDEX(queue->process)];
1410 if (queue->process == queue->remote_insert) {
1411 /* fabricate a matching dummy bulk */
1412 bulk->remote_data = NULL;
1413 bulk->remote_size = 0;
1414 queue->remote_insert++;
1417 if (queue->process != queue->local_insert) {
1418 vchiq_complete_bulk(bulk);
1420 vchiq_log_info(SRVTRACE_LEVEL(service),
1421 "%s %c%c%c%c d:%d ABORTED - tx len:%d, rx len:%d",
1422 is_tx ? "Send Bulk to" : "Recv Bulk from",
1423 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1424 service->remoteport,
1428 /* fabricate a matching dummy bulk */
1431 bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
1432 bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
1434 queue->local_insert++;
1442 parse_open(struct vchiq_state *state, struct vchiq_header *header)
1444 struct vchiq_service *service = NULL;
1446 unsigned int localport, remoteport;
1448 msgid = header->msgid;
1449 size = header->size;
1450 localport = VCHIQ_MSG_DSTPORT(msgid);
1451 remoteport = VCHIQ_MSG_SRCPORT(msgid);
1452 if (size >= sizeof(struct vchiq_open_payload)) {
1453 const struct vchiq_open_payload *payload =
1454 (struct vchiq_open_payload *)header->data;
1455 unsigned int fourcc;
1457 fourcc = payload->fourcc;
1458 vchiq_log_info(vchiq_core_log_level,
1459 "%d: prs OPEN@%pK (%d->'%c%c%c%c')",
1460 state->id, header, localport,
1461 VCHIQ_FOURCC_AS_4CHARS(fourcc));
1463 service = get_listening_service(state, fourcc);
1466 /* A matching service exists */
1467 short version = payload->version;
1468 short version_min = payload->version_min;
1470 if ((service->version < version_min) ||
1471 (version < service->version_min)) {
1472 /* Version mismatch */
1473 vchiq_loud_error_header();
1474 vchiq_loud_error("%d: service %d (%c%c%c%c) "
1475 "version mismatch - local (%d, min %d)"
1476 " vs. remote (%d, min %d)",
1477 state->id, service->localport,
1478 VCHIQ_FOURCC_AS_4CHARS(fourcc),
1479 service->version, service->version_min,
1480 version, version_min);
1481 vchiq_loud_error_footer();
1482 unlock_service(service);
1486 service->peer_version = version;
1488 if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
1489 struct vchiq_openack_payload ack_payload = {
1493 if (state->version_common <
1494 VCHIQ_VERSION_SYNCHRONOUS_MODE)
1497 /* Acknowledge the OPEN */
1498 if (service->sync) {
1499 if (queue_message_sync(
1506 memcpy_copy_callback,
1508 sizeof(ack_payload),
1510 goto bail_not_ready;
1512 if (queue_message(state,
1518 memcpy_copy_callback,
1520 sizeof(ack_payload),
1522 goto bail_not_ready;
1525 /* The service is now open */
1526 vchiq_set_service_state(service,
1527 service->sync ? VCHIQ_SRVSTATE_OPENSYNC
1528 : VCHIQ_SRVSTATE_OPEN);
1531 /* Success - the message has been dealt with */
1532 unlock_service(service);
1538 /* No available service, or an invalid request - send a CLOSE */
1539 if (queue_message(state, NULL,
1540 VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
1541 NULL, NULL, 0, 0) == VCHIQ_RETRY)
1542 goto bail_not_ready;
1548 unlock_service(service);
1553 /* Called by the slot handler thread */
1555 parse_rx_slots(struct vchiq_state *state)
1557 struct vchiq_shared_state *remote = state->remote;
1558 struct vchiq_service *service = NULL;
1561 DEBUG_INITIALISE(state->local)
1563 tx_pos = remote->tx_pos;
1565 while (state->rx_pos != tx_pos) {
1566 struct vchiq_header *header;
1569 unsigned int localport, remoteport;
1571 DEBUG_TRACE(PARSE_LINE);
1572 if (!state->rx_data) {
1575 WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
1576 rx_index = remote->slot_queue[
1577 SLOT_QUEUE_INDEX_FROM_POS_MASKED(state->rx_pos)];
1578 state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
1580 state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
1583 * Initialise use_count to one, and increment
1584 * release_count at the end of the slot to avoid
1585 * releasing the slot prematurely.
1587 state->rx_info->use_count = 1;
1588 state->rx_info->release_count = 0;
1591 header = (struct vchiq_header *)(state->rx_data +
1592 (state->rx_pos & VCHIQ_SLOT_MASK));
1593 DEBUG_VALUE(PARSE_HEADER, (int)(long)header);
1594 msgid = header->msgid;
1595 DEBUG_VALUE(PARSE_MSGID, msgid);
1596 size = header->size;
1597 type = VCHIQ_MSG_TYPE(msgid);
1598 localport = VCHIQ_MSG_DSTPORT(msgid);
1599 remoteport = VCHIQ_MSG_SRCPORT(msgid);
1601 if (type != VCHIQ_MSG_DATA)
1602 VCHIQ_STATS_INC(state, ctrl_rx_count);
1605 case VCHIQ_MSG_OPENACK:
1606 case VCHIQ_MSG_CLOSE:
1607 case VCHIQ_MSG_DATA:
1608 case VCHIQ_MSG_BULK_RX:
1609 case VCHIQ_MSG_BULK_TX:
1610 case VCHIQ_MSG_BULK_RX_DONE:
1611 case VCHIQ_MSG_BULK_TX_DONE:
1612 service = find_service_by_port(state, localport);
1614 ((service->remoteport != remoteport) &&
1615 (service->remoteport != VCHIQ_PORT_FREE))) &&
1617 (type == VCHIQ_MSG_CLOSE)) {
1619 * This could be a CLOSE from a client which
1620 * hadn't yet received the OPENACK - look for
1621 * the connected service
1624 unlock_service(service);
1625 service = get_connected_service(state,
1628 vchiq_log_warning(vchiq_core_log_level,
1629 "%d: prs %s@%pK (%d->%d) - found connected service %d",
1630 state->id, msg_type_str(type),
1631 header, remoteport, localport,
1632 service->localport);
1636 vchiq_log_error(vchiq_core_log_level,
1637 "%d: prs %s@%pK (%d->%d) - invalid/closed service %d",
1638 state->id, msg_type_str(type),
1639 header, remoteport, localport,
1648 if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
1651 svc_fourcc = service
1652 ? service->base.fourcc
1653 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1654 vchiq_log_info(SRVTRACE_LEVEL(service),
1655 "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d len:%d",
1656 msg_type_str(type), type,
1657 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1658 remoteport, localport, size);
1660 vchiq_log_dump_mem("Rcvd", 0, header->data,
1664 if (((unsigned long)header & VCHIQ_SLOT_MASK) +
1665 calc_stride(size) > VCHIQ_SLOT_SIZE) {
1666 vchiq_log_error(vchiq_core_log_level,
1667 "header %pK (msgid %x) - size %x too big for slot",
1668 header, (unsigned int)msgid,
1669 (unsigned int)size);
1670 WARN(1, "oversized for slot\n");
1674 case VCHIQ_MSG_OPEN:
1675 WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
1676 if (!parse_open(state, header))
1677 goto bail_not_ready;
1679 case VCHIQ_MSG_OPENACK:
1680 if (size >= sizeof(struct vchiq_openack_payload)) {
1681 const struct vchiq_openack_payload *payload =
1682 (struct vchiq_openack_payload *)
1684 service->peer_version = payload->version;
1686 vchiq_log_info(vchiq_core_log_level,
1687 "%d: prs OPENACK@%pK,%x (%d->%d) v:%d",
1688 state->id, header, size, remoteport, localport,
1689 service->peer_version);
1690 if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
1691 service->remoteport = remoteport;
1692 vchiq_set_service_state(service,
1693 VCHIQ_SRVSTATE_OPEN);
1694 complete(&service->remove_event);
1696 vchiq_log_error(vchiq_core_log_level,
1697 "OPENACK received in state %s",
1698 srvstate_names[service->srvstate]);
1700 case VCHIQ_MSG_CLOSE:
1701 WARN_ON(size != 0); /* There should be no data */
1703 vchiq_log_info(vchiq_core_log_level,
1704 "%d: prs CLOSE@%pK (%d->%d)",
1705 state->id, header, remoteport, localport);
1707 mark_service_closing_internal(service, 1);
1709 if (vchiq_close_service_internal(service,
1710 1/*close_recvd*/) == VCHIQ_RETRY)
1711 goto bail_not_ready;
1713 vchiq_log_info(vchiq_core_log_level,
1714 "Close Service %c%c%c%c s:%u d:%d",
1715 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1717 service->remoteport);
1719 case VCHIQ_MSG_DATA:
1720 vchiq_log_info(vchiq_core_log_level,
1721 "%d: prs DATA@%pK,%x (%d->%d)",
1722 state->id, header, size, remoteport, localport);
1724 if ((service->remoteport == remoteport) &&
1725 (service->srvstate == VCHIQ_SRVSTATE_OPEN)) {
1726 header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
1727 claim_slot(state->rx_info);
1728 DEBUG_TRACE(PARSE_LINE);
1729 if (make_service_callback(service,
1730 VCHIQ_MESSAGE_AVAILABLE, header,
1731 NULL) == VCHIQ_RETRY) {
1732 DEBUG_TRACE(PARSE_LINE);
1733 goto bail_not_ready;
1735 VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
1736 VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
1739 VCHIQ_STATS_INC(state, error_count);
1742 case VCHIQ_MSG_CONNECT:
1743 vchiq_log_info(vchiq_core_log_level,
1744 "%d: prs CONNECT@%pK", state->id, header);
1745 state->version_common = ((struct vchiq_slot_zero *)
1746 state->slot_data)->version;
1747 complete(&state->connect);
1749 case VCHIQ_MSG_BULK_RX:
1750 case VCHIQ_MSG_BULK_TX:
1752 * We should never receive a bulk request from the
1753 * other side since we're not setup to perform as the
1758 case VCHIQ_MSG_BULK_RX_DONE:
1759 case VCHIQ_MSG_BULK_TX_DONE:
1760 if ((service->remoteport == remoteport) &&
1761 (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
1762 struct vchiq_bulk_queue *queue;
1763 struct vchiq_bulk *bulk;
1765 queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
1766 &service->bulk_rx : &service->bulk_tx;
1768 DEBUG_TRACE(PARSE_LINE);
1769 if (mutex_lock_killable(&service->bulk_mutex)) {
1770 DEBUG_TRACE(PARSE_LINE);
1771 goto bail_not_ready;
1773 if ((int)(queue->remote_insert -
1774 queue->local_insert) >= 0) {
1775 vchiq_log_error(vchiq_core_log_level,
1776 "%d: prs %s@%pK (%d->%d) unexpected (ri=%d,li=%d)",
1777 state->id, msg_type_str(type),
1778 header, remoteport, localport,
1779 queue->remote_insert,
1780 queue->local_insert);
1781 mutex_unlock(&service->bulk_mutex);
1784 if (queue->process != queue->remote_insert) {
1785 pr_err("%s: p %x != ri %x\n",
1788 queue->remote_insert);
1789 mutex_unlock(&service->bulk_mutex);
1790 goto bail_not_ready;
1793 bulk = &queue->bulks[
1794 BULK_INDEX(queue->remote_insert)];
1795 bulk->actual = *(int *)header->data;
1796 queue->remote_insert++;
1798 vchiq_log_info(vchiq_core_log_level,
1799 "%d: prs %s@%pK (%d->%d) %x@%pad",
1800 state->id, msg_type_str(type),
1801 header, remoteport, localport,
1802 bulk->actual, &bulk->data);
1804 vchiq_log_trace(vchiq_core_log_level,
1805 "%d: prs:%d %cx li=%x ri=%x p=%x",
1806 state->id, localport,
1807 (type == VCHIQ_MSG_BULK_RX_DONE) ?
1809 queue->local_insert,
1810 queue->remote_insert, queue->process);
1812 DEBUG_TRACE(PARSE_LINE);
1813 WARN_ON(queue->process == queue->local_insert);
1814 vchiq_complete_bulk(bulk);
1816 mutex_unlock(&service->bulk_mutex);
1817 DEBUG_TRACE(PARSE_LINE);
1818 notify_bulks(service, queue, 1/*retry_poll*/);
1819 DEBUG_TRACE(PARSE_LINE);
1822 case VCHIQ_MSG_PADDING:
1823 vchiq_log_trace(vchiq_core_log_level,
1824 "%d: prs PADDING@%pK,%x",
1825 state->id, header, size);
1827 case VCHIQ_MSG_PAUSE:
1828 /* If initiated, signal the application thread */
1829 vchiq_log_trace(vchiq_core_log_level,
1830 "%d: prs PAUSE@%pK,%x",
1831 state->id, header, size);
1832 if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
1833 vchiq_log_error(vchiq_core_log_level,
1834 "%d: PAUSE received in state PAUSED",
1838 if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
1839 /* Send a PAUSE in response */
1840 if (queue_message(state, NULL,
1841 VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
1842 NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK)
1844 goto bail_not_ready;
1846 /* At this point slot_mutex is held */
1847 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
1849 case VCHIQ_MSG_RESUME:
1850 vchiq_log_trace(vchiq_core_log_level,
1851 "%d: prs RESUME@%pK,%x",
1852 state->id, header, size);
1853 /* Release the slot mutex */
1854 mutex_unlock(&state->slot_mutex);
1855 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
1858 case VCHIQ_MSG_REMOTE_USE:
1859 vchiq_on_remote_use(state);
1861 case VCHIQ_MSG_REMOTE_RELEASE:
1862 vchiq_on_remote_release(state);
1864 case VCHIQ_MSG_REMOTE_USE_ACTIVE:
1868 vchiq_log_error(vchiq_core_log_level,
1869 "%d: prs invalid msgid %x@%pK,%x",
1870 state->id, msgid, header, size);
1871 WARN(1, "invalid message\n");
1877 unlock_service(service);
1881 state->rx_pos += calc_stride(size);
1883 DEBUG_TRACE(PARSE_LINE);
1885 * Perform some housekeeping when the end of the slot is
1888 if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
1889 /* Remove the extra reference count. */
1890 release_slot(state, state->rx_info, NULL, NULL);
1891 state->rx_data = NULL;
1897 unlock_service(service);
1900 /* Called by the slot handler thread */
1902 slot_handler_func(void *v)
1904 struct vchiq_state *state = v;
1905 struct vchiq_shared_state *local = state->local;
1907 DEBUG_INITIALISE(local)
1910 DEBUG_COUNT(SLOT_HANDLER_COUNT);
1911 DEBUG_TRACE(SLOT_HANDLER_LINE);
1912 remote_event_wait(&state->trigger_event, &local->trigger);
1916 DEBUG_TRACE(SLOT_HANDLER_LINE);
1917 if (state->poll_needed) {
1919 state->poll_needed = 0;
1922 * Handle service polling and other rare conditions here
1923 * out of the mainline code
1925 switch (state->conn_state) {
1926 case VCHIQ_CONNSTATE_CONNECTED:
1927 /* Poll the services as requested */
1928 poll_services(state);
1931 case VCHIQ_CONNSTATE_PAUSING:
1932 if (queue_message(state, NULL,
1933 VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
1935 QMFLAGS_NO_MUTEX_UNLOCK)
1937 vchiq_set_conn_state(state,
1938 VCHIQ_CONNSTATE_PAUSE_SENT);
1941 state->poll_needed = 1;
1945 case VCHIQ_CONNSTATE_RESUMING:
1946 if (queue_message(state, NULL,
1947 VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
1948 NULL, NULL, 0, QMFLAGS_NO_MUTEX_LOCK)
1950 vchiq_set_conn_state(state,
1951 VCHIQ_CONNSTATE_CONNECTED);
1954 * This should really be impossible,
1955 * since the PAUSE should have flushed
1956 * through outstanding messages.
1958 vchiq_log_error(vchiq_core_log_level,
1959 "Failed to send RESUME message");
1968 DEBUG_TRACE(SLOT_HANDLER_LINE);
1969 parse_rx_slots(state);
1974 /* Called by the recycle thread */
1976 recycle_func(void *v)
1978 struct vchiq_state *state = v;
1979 struct vchiq_shared_state *local = state->local;
1983 length = sizeof(*found) * BITSET_SIZE(VCHIQ_MAX_SERVICES);
1985 found = kmalloc_array(BITSET_SIZE(VCHIQ_MAX_SERVICES), sizeof(*found),
1991 remote_event_wait(&state->recycle_event, &local->recycle);
1993 process_free_queue(state, found, length);
1998 /* Called by the sync thread */
2002 struct vchiq_state *state = v;
2003 struct vchiq_shared_state *local = state->local;
2004 struct vchiq_header *header =
2005 (struct vchiq_header *)SLOT_DATA_FROM_INDEX(state,
2006 state->remote->slot_sync);
2009 struct vchiq_service *service;
2012 unsigned int localport, remoteport;
2014 remote_event_wait(&state->sync_trigger_event, &local->sync_trigger);
2018 msgid = header->msgid;
2019 size = header->size;
2020 type = VCHIQ_MSG_TYPE(msgid);
2021 localport = VCHIQ_MSG_DSTPORT(msgid);
2022 remoteport = VCHIQ_MSG_SRCPORT(msgid);
2024 service = find_service_by_port(state, localport);
2027 vchiq_log_error(vchiq_sync_log_level,
2028 "%d: sf %s@%pK (%d->%d) - invalid/closed service %d",
2029 state->id, msg_type_str(type),
2030 header, remoteport, localport, localport);
2031 release_message_sync(state, header);
2035 if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
2038 svc_fourcc = service
2039 ? service->base.fourcc
2040 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
2041 vchiq_log_trace(vchiq_sync_log_level,
2042 "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
2044 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
2045 remoteport, localport, size);
2047 vchiq_log_dump_mem("Rcvd", 0, header->data,
2052 case VCHIQ_MSG_OPENACK:
2053 if (size >= sizeof(struct vchiq_openack_payload)) {
2054 const struct vchiq_openack_payload *payload =
2055 (struct vchiq_openack_payload *)
2057 service->peer_version = payload->version;
2059 vchiq_log_info(vchiq_sync_log_level,
2060 "%d: sf OPENACK@%pK,%x (%d->%d) v:%d",
2061 state->id, header, size, remoteport, localport,
2062 service->peer_version);
2063 if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
2064 service->remoteport = remoteport;
2065 vchiq_set_service_state(service,
2066 VCHIQ_SRVSTATE_OPENSYNC);
2068 complete(&service->remove_event);
2070 release_message_sync(state, header);
2073 case VCHIQ_MSG_DATA:
2074 vchiq_log_trace(vchiq_sync_log_level,
2075 "%d: sf DATA@%pK,%x (%d->%d)",
2076 state->id, header, size, remoteport, localport);
2078 if ((service->remoteport == remoteport) &&
2079 (service->srvstate ==
2080 VCHIQ_SRVSTATE_OPENSYNC)) {
2081 if (make_service_callback(service,
2082 VCHIQ_MESSAGE_AVAILABLE, header,
2083 NULL) == VCHIQ_RETRY)
2084 vchiq_log_error(vchiq_sync_log_level,
2085 "synchronous callback to service %d returns VCHIQ_RETRY",
2091 vchiq_log_error(vchiq_sync_log_level,
2092 "%d: sf unexpected msgid %x@%pK,%x",
2093 state->id, msgid, header, size);
2094 release_message_sync(state, header);
2098 unlock_service(service);
2105 init_bulk_queue(struct vchiq_bulk_queue *queue)
2107 queue->local_insert = 0;
2108 queue->remote_insert = 0;
2110 queue->remote_notify = 0;
2115 get_conn_state_name(enum vchiq_connstate conn_state)
2117 return conn_state_names[conn_state];
2120 struct vchiq_slot_zero *
2121 vchiq_init_slots(void *mem_base, int mem_size)
2124 (int)((VCHIQ_SLOT_SIZE - (long)mem_base) & VCHIQ_SLOT_MASK);
2125 struct vchiq_slot_zero *slot_zero =
2126 (struct vchiq_slot_zero *)(mem_base + mem_align);
2127 int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
2128 int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
2130 /* Ensure there is enough memory to run an absolutely minimum system */
2131 num_slots -= first_data_slot;
2133 if (num_slots < 4) {
2134 vchiq_log_error(vchiq_core_log_level,
2135 "%s - insufficient memory %x bytes",
2136 __func__, mem_size);
2140 memset(slot_zero, 0, sizeof(struct vchiq_slot_zero));
2142 slot_zero->magic = VCHIQ_MAGIC;
2143 slot_zero->version = VCHIQ_VERSION;
2144 slot_zero->version_min = VCHIQ_VERSION_MIN;
2145 slot_zero->slot_zero_size = sizeof(struct vchiq_slot_zero);
2146 slot_zero->slot_size = VCHIQ_SLOT_SIZE;
2147 slot_zero->max_slots = VCHIQ_MAX_SLOTS;
2148 slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
2150 slot_zero->master.slot_sync = first_data_slot;
2151 slot_zero->master.slot_first = first_data_slot + 1;
2152 slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
2153 slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
2154 slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
2155 slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
2161 vchiq_init_state(struct vchiq_state *state, struct vchiq_slot_zero *slot_zero)
2163 struct vchiq_shared_state *local;
2164 struct vchiq_shared_state *remote;
2165 char threadname[16];
2168 if (vchiq_states[0]) {
2169 pr_err("%s: VCHIQ state already initialized\n", __func__);
2173 local = &slot_zero->slave;
2174 remote = &slot_zero->master;
2176 if (local->initialised) {
2177 vchiq_loud_error_header();
2178 if (remote->initialised)
2179 vchiq_loud_error("local state has already been initialised");
2181 vchiq_loud_error("master/slave mismatch two slaves");
2182 vchiq_loud_error_footer();
2186 memset(state, 0, sizeof(struct vchiq_state));
2189 * initialize shared state pointers
2192 state->local = local;
2193 state->remote = remote;
2194 state->slot_data = (struct vchiq_slot *)slot_zero;
2197 * initialize events and mutexes
2200 init_completion(&state->connect);
2201 mutex_init(&state->mutex);
2202 mutex_init(&state->slot_mutex);
2203 mutex_init(&state->recycle_mutex);
2204 mutex_init(&state->sync_mutex);
2205 mutex_init(&state->bulk_transfer_mutex);
2207 init_completion(&state->slot_available_event);
2208 init_completion(&state->slot_remove_event);
2209 init_completion(&state->data_quota_event);
2211 state->slot_queue_available = 0;
2213 for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
2214 struct vchiq_service_quota *quota =
2215 &state->service_quotas[i];
2216 init_completion("a->quota_event);
2219 for (i = local->slot_first; i <= local->slot_last; i++) {
2220 local->slot_queue[state->slot_queue_available++] = i;
2221 complete(&state->slot_available_event);
2224 state->default_slot_quota = state->slot_queue_available/2;
2225 state->default_message_quota =
2226 min((unsigned short)(state->default_slot_quota * 256),
2227 (unsigned short)~0);
2229 state->previous_data_index = -1;
2230 state->data_use_count = 0;
2231 state->data_quota = state->slot_queue_available - 1;
2233 remote_event_create(&state->trigger_event, &local->trigger);
2235 remote_event_create(&state->recycle_event, &local->recycle);
2236 local->slot_queue_recycle = state->slot_queue_available;
2237 remote_event_create(&state->sync_trigger_event, &local->sync_trigger);
2238 remote_event_create(&state->sync_release_event, &local->sync_release);
2240 /* At start-of-day, the slot is empty and available */
2241 ((struct vchiq_header *)
2242 SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid =
2243 VCHIQ_MSGID_PADDING;
2244 remote_event_signal_local(&state->sync_release_event, &local->sync_release);
2246 local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
2248 ret = vchiq_platform_init_state(state);
2253 * bring up slot handler thread
2255 snprintf(threadname, sizeof(threadname), "vchiq-slot/%d", state->id);
2256 state->slot_handler_thread = kthread_create(&slot_handler_func,
2260 if (IS_ERR(state->slot_handler_thread)) {
2261 vchiq_loud_error_header();
2262 vchiq_loud_error("couldn't create thread %s", threadname);
2263 vchiq_loud_error_footer();
2264 return PTR_ERR(state->slot_handler_thread);
2266 set_user_nice(state->slot_handler_thread, -19);
2268 snprintf(threadname, sizeof(threadname), "vchiq-recy/%d", state->id);
2269 state->recycle_thread = kthread_create(&recycle_func,
2272 if (IS_ERR(state->recycle_thread)) {
2273 vchiq_loud_error_header();
2274 vchiq_loud_error("couldn't create thread %s", threadname);
2275 vchiq_loud_error_footer();
2276 ret = PTR_ERR(state->recycle_thread);
2277 goto fail_free_handler_thread;
2279 set_user_nice(state->recycle_thread, -19);
2281 snprintf(threadname, sizeof(threadname), "vchiq-sync/%d", state->id);
2282 state->sync_thread = kthread_create(&sync_func,
2285 if (IS_ERR(state->sync_thread)) {
2286 vchiq_loud_error_header();
2287 vchiq_loud_error("couldn't create thread %s", threadname);
2288 vchiq_loud_error_footer();
2289 ret = PTR_ERR(state->sync_thread);
2290 goto fail_free_recycle_thread;
2292 set_user_nice(state->sync_thread, -20);
2294 wake_up_process(state->slot_handler_thread);
2295 wake_up_process(state->recycle_thread);
2296 wake_up_process(state->sync_thread);
2298 vchiq_states[0] = state;
2300 /* Indicate readiness to the other side */
2301 local->initialised = 1;
2305 fail_free_recycle_thread:
2306 kthread_stop(state->recycle_thread);
2307 fail_free_handler_thread:
2308 kthread_stop(state->slot_handler_thread);
2313 void vchiq_msg_queue_push(unsigned int handle, struct vchiq_header *header)
2315 struct vchiq_service *service = find_service_by_handle(handle);
2318 while (service->msg_queue_write == service->msg_queue_read +
2320 if (wait_for_completion_interruptible(&service->msg_queue_pop))
2321 flush_signals(current);
2324 pos = service->msg_queue_write++ & (VCHIQ_MAX_SLOTS - 1);
2325 service->msg_queue[pos] = header;
2327 complete(&service->msg_queue_push);
2329 EXPORT_SYMBOL(vchiq_msg_queue_push);
2331 struct vchiq_header *vchiq_msg_hold(unsigned int handle)
2333 struct vchiq_service *service = find_service_by_handle(handle);
2334 struct vchiq_header *header;
2337 if (service->msg_queue_write == service->msg_queue_read)
2340 while (service->msg_queue_write == service->msg_queue_read) {
2341 if (wait_for_completion_interruptible(&service->msg_queue_push))
2342 flush_signals(current);
2345 pos = service->msg_queue_read++ & (VCHIQ_MAX_SLOTS - 1);
2346 header = service->msg_queue[pos];
2348 complete(&service->msg_queue_pop);
2352 EXPORT_SYMBOL(vchiq_msg_hold);
2354 static int vchiq_validate_params(const struct vchiq_service_params_kernel *params)
2356 if (!params->callback || !params->fourcc) {
2357 vchiq_loud_error("Can't add service, invalid params\n");
2364 /* Called from application thread when a client or server service is created. */
2365 struct vchiq_service *
2366 vchiq_add_service_internal(struct vchiq_state *state,
2367 const struct vchiq_service_params_kernel *params,
2368 int srvstate, struct vchiq_instance *instance,
2369 vchiq_userdata_term userdata_term)
2371 struct vchiq_service *service;
2372 struct vchiq_service __rcu **pservice = NULL;
2373 struct vchiq_service_quota *quota;
2377 ret = vchiq_validate_params(params);
2381 service = kmalloc(sizeof(*service), GFP_KERNEL);
2385 service->base.fourcc = params->fourcc;
2386 service->base.callback = params->callback;
2387 service->base.userdata = params->userdata;
2388 service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
2389 kref_init(&service->ref_count);
2390 service->srvstate = VCHIQ_SRVSTATE_FREE;
2391 service->userdata_term = userdata_term;
2392 service->localport = VCHIQ_PORT_FREE;
2393 service->remoteport = VCHIQ_PORT_FREE;
2395 service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
2396 VCHIQ_FOURCC_INVALID : params->fourcc;
2397 service->client_id = 0;
2398 service->auto_close = 1;
2400 service->closing = 0;
2402 atomic_set(&service->poll_flags, 0);
2403 service->version = params->version;
2404 service->version_min = params->version_min;
2405 service->state = state;
2406 service->instance = instance;
2407 service->service_use_count = 0;
2408 service->msg_queue_read = 0;
2409 service->msg_queue_write = 0;
2410 init_bulk_queue(&service->bulk_tx);
2411 init_bulk_queue(&service->bulk_rx);
2412 init_completion(&service->remove_event);
2413 init_completion(&service->bulk_remove_event);
2414 init_completion(&service->msg_queue_pop);
2415 init_completion(&service->msg_queue_push);
2416 mutex_init(&service->bulk_mutex);
2417 memset(&service->stats, 0, sizeof(service->stats));
2418 memset(&service->msg_queue, 0, sizeof(service->msg_queue));
2421 * Although it is perfectly possible to use a spinlock
2422 * to protect the creation of services, it is overkill as it
2423 * disables interrupts while the array is searched.
2424 * The only danger is of another thread trying to create a
2425 * service - service deletion is safe.
2426 * Therefore it is preferable to use state->mutex which,
2427 * although slower to claim, doesn't block interrupts while
2431 mutex_lock(&state->mutex);
2433 /* Prepare to use a previously unused service */
2434 if (state->unused_service < VCHIQ_MAX_SERVICES)
2435 pservice = &state->services[state->unused_service];
2437 if (srvstate == VCHIQ_SRVSTATE_OPENING) {
2438 for (i = 0; i < state->unused_service; i++) {
2439 if (!rcu_access_pointer(state->services[i])) {
2440 pservice = &state->services[i];
2446 for (i = (state->unused_service - 1); i >= 0; i--) {
2447 struct vchiq_service *srv;
2449 srv = rcu_dereference(state->services[i]);
2451 pservice = &state->services[i];
2452 else if ((srv->public_fourcc == params->fourcc) &&
2453 ((srv->instance != instance) ||
2454 (srv->base.callback != params->callback))) {
2456 * There is another server using this
2457 * fourcc which doesn't match.
2467 service->localport = (pservice - state->services);
2469 handle_seq = VCHIQ_MAX_STATES *
2471 service->handle = handle_seq |
2472 (state->id * VCHIQ_MAX_SERVICES) |
2474 handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
2475 rcu_assign_pointer(*pservice, service);
2476 if (pservice == &state->services[state->unused_service])
2477 state->unused_service++;
2480 mutex_unlock(&state->mutex);
2487 quota = &state->service_quotas[service->localport];
2488 quota->slot_quota = state->default_slot_quota;
2489 quota->message_quota = state->default_message_quota;
2490 if (quota->slot_use_count == 0)
2491 quota->previous_tx_index =
2492 SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
2495 /* Bring this service online */
2496 vchiq_set_service_state(service, srvstate);
2498 vchiq_log_info(vchiq_core_msg_log_level,
2499 "%s Service %c%c%c%c SrcPort:%d",
2500 (srvstate == VCHIQ_SRVSTATE_OPENING)
2502 VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
2503 service->localport);
2505 /* Don't unlock the service - leave it with a ref_count of 1. */
2511 vchiq_open_service_internal(struct vchiq_service *service, int client_id)
2513 struct vchiq_open_payload payload = {
2514 service->base.fourcc,
2517 service->version_min
2519 enum vchiq_status status = VCHIQ_SUCCESS;
2521 service->client_id = client_id;
2522 vchiq_use_service_internal(service);
2523 status = queue_message(service->state,
2525 VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN,
2528 memcpy_copy_callback,
2531 QMFLAGS_IS_BLOCKING);
2533 if (status != VCHIQ_SUCCESS)
2536 /* Wait for the ACK/NAK */
2537 if (wait_for_completion_interruptible(&service->remove_event)) {
2538 status = VCHIQ_RETRY;
2539 vchiq_release_service_internal(service);
2540 } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
2541 (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
2542 if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
2543 vchiq_log_error(vchiq_core_log_level,
2544 "%d: osi - srvstate = %s (ref %u)",
2546 srvstate_names[service->srvstate],
2547 kref_read(&service->ref_count));
2548 status = VCHIQ_ERROR;
2549 VCHIQ_SERVICE_STATS_INC(service, error_count);
2550 vchiq_release_service_internal(service);
2557 release_service_messages(struct vchiq_service *service)
2559 struct vchiq_state *state = service->state;
2560 int slot_last = state->remote->slot_last;
2563 /* Release any claimed messages aimed at this service */
2565 if (service->sync) {
2566 struct vchiq_header *header =
2567 (struct vchiq_header *)SLOT_DATA_FROM_INDEX(state,
2568 state->remote->slot_sync);
2569 if (VCHIQ_MSG_DSTPORT(header->msgid) == service->localport)
2570 release_message_sync(state, header);
2575 for (i = state->remote->slot_first; i <= slot_last; i++) {
2576 struct vchiq_slot_info *slot_info =
2577 SLOT_INFO_FROM_INDEX(state, i);
2578 if (slot_info->release_count != slot_info->use_count) {
2580 (char *)SLOT_DATA_FROM_INDEX(state, i);
2581 unsigned int pos, end;
2583 end = VCHIQ_SLOT_SIZE;
2584 if (data == state->rx_data)
2586 * This buffer is still being read from - stop
2587 * at the current read position
2589 end = state->rx_pos & VCHIQ_SLOT_MASK;
2594 struct vchiq_header *header =
2595 (struct vchiq_header *)(data + pos);
2596 int msgid = header->msgid;
2597 int port = VCHIQ_MSG_DSTPORT(msgid);
2599 if ((port == service->localport) &&
2600 (msgid & VCHIQ_MSGID_CLAIMED)) {
2601 vchiq_log_info(vchiq_core_log_level,
2602 " fsi - hdr %pK", header);
2603 release_slot(state, slot_info, header,
2606 pos += calc_stride(header->size);
2607 if (pos > VCHIQ_SLOT_SIZE) {
2608 vchiq_log_error(vchiq_core_log_level,
2609 "fsi - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
2611 header->msgid, header->size);
2612 WARN(1, "invalid slot position\n");
2620 do_abort_bulks(struct vchiq_service *service)
2622 enum vchiq_status status;
2624 /* Abort any outstanding bulk transfers */
2625 if (mutex_lock_killable(&service->bulk_mutex))
2627 abort_outstanding_bulks(service, &service->bulk_tx);
2628 abort_outstanding_bulks(service, &service->bulk_rx);
2629 mutex_unlock(&service->bulk_mutex);
2631 status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
2632 if (status != VCHIQ_SUCCESS)
2635 status = notify_bulks(service, &service->bulk_rx, 0/*!retry_poll*/);
2636 return (status == VCHIQ_SUCCESS);
2639 static enum vchiq_status
2640 close_service_complete(struct vchiq_service *service, int failstate)
2642 enum vchiq_status status;
2643 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2646 switch (service->srvstate) {
2647 case VCHIQ_SRVSTATE_OPEN:
2648 case VCHIQ_SRVSTATE_CLOSESENT:
2649 case VCHIQ_SRVSTATE_CLOSERECVD:
2651 if (service->auto_close) {
2652 service->client_id = 0;
2653 service->remoteport = VCHIQ_PORT_FREE;
2654 newstate = VCHIQ_SRVSTATE_LISTENING;
2656 newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
2658 newstate = VCHIQ_SRVSTATE_CLOSED;
2659 vchiq_set_service_state(service, newstate);
2661 case VCHIQ_SRVSTATE_LISTENING:
2664 vchiq_log_error(vchiq_core_log_level,
2665 "%s(%x) called in state %s", __func__,
2666 service->handle, srvstate_names[service->srvstate]);
2667 WARN(1, "%s in unexpected state\n", __func__);
2671 status = make_service_callback(service,
2672 VCHIQ_SERVICE_CLOSED, NULL, NULL);
2674 if (status != VCHIQ_RETRY) {
2675 int uc = service->service_use_count;
2677 /* Complete the close process */
2678 for (i = 0; i < uc; i++)
2680 * cater for cases where close is forced and the
2681 * client may not close all it's handles
2683 vchiq_release_service_internal(service);
2685 service->client_id = 0;
2686 service->remoteport = VCHIQ_PORT_FREE;
2688 if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
2689 vchiq_free_service_internal(service);
2690 else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
2692 service->closing = 0;
2694 complete(&service->remove_event);
2697 vchiq_set_service_state(service, failstate);
2702 /* Called by the slot handler */
2704 vchiq_close_service_internal(struct vchiq_service *service, int close_recvd)
2706 struct vchiq_state *state = service->state;
2707 enum vchiq_status status = VCHIQ_SUCCESS;
2708 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2710 vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
2711 service->state->id, service->localport, close_recvd,
2712 srvstate_names[service->srvstate]);
2714 switch (service->srvstate) {
2715 case VCHIQ_SRVSTATE_CLOSED:
2716 case VCHIQ_SRVSTATE_HIDDEN:
2717 case VCHIQ_SRVSTATE_LISTENING:
2718 case VCHIQ_SRVSTATE_CLOSEWAIT:
2720 vchiq_log_error(vchiq_core_log_level,
2721 "%s(1) called in state %s",
2722 __func__, srvstate_names[service->srvstate]);
2723 else if (is_server) {
2724 if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
2725 status = VCHIQ_ERROR;
2727 service->client_id = 0;
2728 service->remoteport = VCHIQ_PORT_FREE;
2729 if (service->srvstate ==
2730 VCHIQ_SRVSTATE_CLOSEWAIT)
2731 vchiq_set_service_state(service,
2732 VCHIQ_SRVSTATE_LISTENING);
2734 complete(&service->remove_event);
2736 vchiq_free_service_internal(service);
2738 case VCHIQ_SRVSTATE_OPENING:
2740 /* The open was rejected - tell the user */
2741 vchiq_set_service_state(service,
2742 VCHIQ_SRVSTATE_CLOSEWAIT);
2743 complete(&service->remove_event);
2745 /* Shutdown mid-open - let the other side know */
2746 status = queue_message(state, service,
2750 VCHIQ_MSG_DSTPORT(service->remoteport)),
2755 case VCHIQ_SRVSTATE_OPENSYNC:
2756 mutex_lock(&state->sync_mutex);
2758 case VCHIQ_SRVSTATE_OPEN:
2760 if (!do_abort_bulks(service))
2761 status = VCHIQ_RETRY;
2764 release_service_messages(service);
2766 if (status == VCHIQ_SUCCESS)
2767 status = queue_message(state, service,
2771 VCHIQ_MSG_DSTPORT(service->remoteport)),
2772 NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK);
2774 if (status != VCHIQ_SUCCESS) {
2775 if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC)
2776 mutex_unlock(&state->sync_mutex);
2781 /* Change the state while the mutex is still held */
2782 vchiq_set_service_state(service,
2783 VCHIQ_SRVSTATE_CLOSESENT);
2784 mutex_unlock(&state->slot_mutex);
2786 mutex_unlock(&state->sync_mutex);
2790 /* Change the state while the mutex is still held */
2791 vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSERECVD);
2792 mutex_unlock(&state->slot_mutex);
2794 mutex_unlock(&state->sync_mutex);
2796 status = close_service_complete(service,
2797 VCHIQ_SRVSTATE_CLOSERECVD);
2800 case VCHIQ_SRVSTATE_CLOSESENT:
2802 /* This happens when a process is killed mid-close */
2805 if (!do_abort_bulks(service)) {
2806 status = VCHIQ_RETRY;
2810 if (status == VCHIQ_SUCCESS)
2811 status = close_service_complete(service,
2812 VCHIQ_SRVSTATE_CLOSERECVD);
2815 case VCHIQ_SRVSTATE_CLOSERECVD:
2816 if (!close_recvd && is_server)
2817 /* Force into LISTENING mode */
2818 vchiq_set_service_state(service,
2819 VCHIQ_SRVSTATE_LISTENING);
2820 status = close_service_complete(service,
2821 VCHIQ_SRVSTATE_CLOSERECVD);
2825 vchiq_log_error(vchiq_core_log_level,
2826 "%s(%d) called in state %s", __func__,
2827 close_recvd, srvstate_names[service->srvstate]);
2834 /* Called from the application process upon process death */
2836 vchiq_terminate_service_internal(struct vchiq_service *service)
2838 struct vchiq_state *state = service->state;
2840 vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
2841 state->id, service->localport, service->remoteport);
2843 mark_service_closing(service);
2845 /* Mark the service for removal by the slot handler */
2846 request_poll(state, service, VCHIQ_POLL_REMOVE);
2849 /* Called from the slot handler */
2851 vchiq_free_service_internal(struct vchiq_service *service)
2853 struct vchiq_state *state = service->state;
2855 vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
2856 state->id, service->localport);
2858 switch (service->srvstate) {
2859 case VCHIQ_SRVSTATE_OPENING:
2860 case VCHIQ_SRVSTATE_CLOSED:
2861 case VCHIQ_SRVSTATE_HIDDEN:
2862 case VCHIQ_SRVSTATE_LISTENING:
2863 case VCHIQ_SRVSTATE_CLOSEWAIT:
2866 vchiq_log_error(vchiq_core_log_level,
2867 "%d: fsi - (%d) in state %s",
2868 state->id, service->localport,
2869 srvstate_names[service->srvstate]);
2873 vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
2875 complete(&service->remove_event);
2877 /* Release the initial lock */
2878 unlock_service(service);
2882 vchiq_connect_internal(struct vchiq_state *state, struct vchiq_instance *instance)
2884 struct vchiq_service *service;
2887 /* Find all services registered to this client and enable them. */
2889 while ((service = next_service_by_instance(state, instance,
2891 if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
2892 vchiq_set_service_state(service,
2893 VCHIQ_SRVSTATE_LISTENING);
2894 unlock_service(service);
2897 if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
2898 if (queue_message(state, NULL,
2899 VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, NULL,
2900 0, QMFLAGS_IS_BLOCKING) == VCHIQ_RETRY)
2903 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
2906 if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
2907 if (wait_for_completion_interruptible(&state->connect))
2910 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
2911 complete(&state->connect);
2914 return VCHIQ_SUCCESS;
2918 vchiq_shutdown_internal(struct vchiq_state *state, struct vchiq_instance *instance)
2920 struct vchiq_service *service;
2923 /* Find all services registered to this client and enable them. */
2925 while ((service = next_service_by_instance(state, instance,
2927 (void)vchiq_remove_service(service->handle);
2928 unlock_service(service);
2931 return VCHIQ_SUCCESS;
2935 vchiq_close_service(unsigned int handle)
2937 /* Unregister the service */
2938 struct vchiq_service *service = find_service_by_handle(handle);
2939 enum vchiq_status status = VCHIQ_SUCCESS;
2944 vchiq_log_info(vchiq_core_log_level,
2945 "%d: close_service:%d",
2946 service->state->id, service->localport);
2948 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
2949 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
2950 (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
2951 unlock_service(service);
2955 mark_service_closing(service);
2957 if (current == service->state->slot_handler_thread) {
2958 status = vchiq_close_service_internal(service,
2960 WARN_ON(status == VCHIQ_RETRY);
2962 /* Mark the service for termination by the slot handler */
2963 request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
2967 if (wait_for_completion_interruptible(&service->remove_event)) {
2968 status = VCHIQ_RETRY;
2972 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
2973 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
2974 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
2977 vchiq_log_warning(vchiq_core_log_level,
2978 "%d: close_service:%d - waiting in state %s",
2979 service->state->id, service->localport,
2980 srvstate_names[service->srvstate]);
2983 if ((status == VCHIQ_SUCCESS) &&
2984 (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
2985 (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
2986 status = VCHIQ_ERROR;
2988 unlock_service(service);
2992 EXPORT_SYMBOL(vchiq_close_service);
2995 vchiq_remove_service(unsigned int handle)
2997 /* Unregister the service */
2998 struct vchiq_service *service = find_service_by_handle(handle);
2999 enum vchiq_status status = VCHIQ_SUCCESS;
3004 vchiq_log_info(vchiq_core_log_level,
3005 "%d: remove_service:%d",
3006 service->state->id, service->localport);
3008 if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
3009 unlock_service(service);
3013 mark_service_closing(service);
3015 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3016 (current == service->state->slot_handler_thread)) {
3018 * Make it look like a client, because it must be removed and
3019 * not left in the LISTENING state.
3021 service->public_fourcc = VCHIQ_FOURCC_INVALID;
3023 status = vchiq_close_service_internal(service,
3025 WARN_ON(status == VCHIQ_RETRY);
3027 /* Mark the service for removal by the slot handler */
3028 request_poll(service->state, service, VCHIQ_POLL_REMOVE);
3031 if (wait_for_completion_interruptible(&service->remove_event)) {
3032 status = VCHIQ_RETRY;
3036 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3037 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
3040 vchiq_log_warning(vchiq_core_log_level,
3041 "%d: remove_service:%d - waiting in state %s",
3042 service->state->id, service->localport,
3043 srvstate_names[service->srvstate]);
3046 if ((status == VCHIQ_SUCCESS) &&
3047 (service->srvstate != VCHIQ_SRVSTATE_FREE))
3048 status = VCHIQ_ERROR;
3050 unlock_service(service);
3056 * This function may be called by kernel threads or user threads.
3057 * User threads may receive VCHIQ_RETRY to indicate that a signal has been
3058 * received and the call should be retried after being returned to user
3060 * When called in blocking mode, the userdata field points to a bulk_waiter
3063 enum vchiq_status vchiq_bulk_transfer(unsigned int handle,
3064 void *offset, void __user *uoffset,
3065 int size, void *userdata,
3066 enum vchiq_bulk_mode mode,
3067 enum vchiq_bulk_dir dir)
3069 struct vchiq_service *service = find_service_by_handle(handle);
3070 struct vchiq_bulk_queue *queue;
3071 struct vchiq_bulk *bulk;
3072 struct vchiq_state *state;
3073 struct bulk_waiter *bulk_waiter = NULL;
3074 const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
3075 const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
3076 VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
3077 enum vchiq_status status = VCHIQ_ERROR;
3083 if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
3086 if (!offset && !uoffset)
3089 if (vchiq_check_service(service) != VCHIQ_SUCCESS)
3093 case VCHIQ_BULK_MODE_NOCALLBACK:
3094 case VCHIQ_BULK_MODE_CALLBACK:
3096 case VCHIQ_BULK_MODE_BLOCKING:
3097 bulk_waiter = userdata;
3098 init_completion(&bulk_waiter->event);
3099 bulk_waiter->actual = 0;
3100 bulk_waiter->bulk = NULL;
3102 case VCHIQ_BULK_MODE_WAITING:
3103 bulk_waiter = userdata;
3104 bulk = bulk_waiter->bulk;
3110 state = service->state;
3112 queue = (dir == VCHIQ_BULK_TRANSMIT) ?
3113 &service->bulk_tx : &service->bulk_rx;
3115 if (mutex_lock_killable(&service->bulk_mutex)) {
3116 status = VCHIQ_RETRY;
3120 if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
3121 VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
3123 mutex_unlock(&service->bulk_mutex);
3124 if (wait_for_completion_interruptible(
3125 &service->bulk_remove_event)) {
3126 status = VCHIQ_RETRY;
3129 if (mutex_lock_killable(&service->bulk_mutex)) {
3130 status = VCHIQ_RETRY;
3133 } while (queue->local_insert == queue->remove +
3134 VCHIQ_NUM_SERVICE_BULKS);
3137 bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
3141 bulk->userdata = userdata;
3143 bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
3145 if (vchiq_prepare_bulk_data(bulk, offset, uoffset, size, dir))
3146 goto unlock_error_exit;
3150 vchiq_log_info(vchiq_core_log_level,
3151 "%d: bt (%d->%d) %cx %x@%pad %pK",
3152 state->id, service->localport, service->remoteport, dir_char,
3153 size, &bulk->data, userdata);
3156 * The slot mutex must be held when the service is being closed, so
3157 * claim it here to ensure that isn't happening
3159 if (mutex_lock_killable(&state->slot_mutex)) {
3160 status = VCHIQ_RETRY;
3161 goto cancel_bulk_error_exit;
3164 if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
3165 goto unlock_both_error_exit;
3167 payload[0] = lower_32_bits(bulk->data);
3168 payload[1] = bulk->size;
3169 status = queue_message(state,
3171 VCHIQ_MAKE_MSG(dir_msgtype,
3173 service->remoteport),
3174 memcpy_copy_callback,
3177 QMFLAGS_IS_BLOCKING |
3178 QMFLAGS_NO_MUTEX_LOCK |
3179 QMFLAGS_NO_MUTEX_UNLOCK);
3180 if (status != VCHIQ_SUCCESS)
3181 goto unlock_both_error_exit;
3183 queue->local_insert++;
3185 mutex_unlock(&state->slot_mutex);
3186 mutex_unlock(&service->bulk_mutex);
3188 vchiq_log_trace(vchiq_core_log_level,
3189 "%d: bt:%d %cx li=%x ri=%x p=%x",
3191 service->localport, dir_char,
3192 queue->local_insert, queue->remote_insert, queue->process);
3195 unlock_service(service);
3197 status = VCHIQ_SUCCESS;
3200 bulk_waiter->bulk = bulk;
3201 if (wait_for_completion_interruptible(&bulk_waiter->event))
3202 status = VCHIQ_RETRY;
3203 else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
3204 status = VCHIQ_ERROR;
3209 unlock_both_error_exit:
3210 mutex_unlock(&state->slot_mutex);
3211 cancel_bulk_error_exit:
3212 vchiq_complete_bulk(bulk);
3214 mutex_unlock(&service->bulk_mutex);
3218 unlock_service(service);
3223 vchiq_queue_message(unsigned int handle,
3224 ssize_t (*copy_callback)(void *context, void *dest,
3225 size_t offset, size_t maxsize),
3229 struct vchiq_service *service = find_service_by_handle(handle);
3230 enum vchiq_status status = VCHIQ_ERROR;
3235 if (vchiq_check_service(service) != VCHIQ_SUCCESS)
3239 VCHIQ_SERVICE_STATS_INC(service, error_count);
3244 if (size > VCHIQ_MAX_MSG_SIZE) {
3245 VCHIQ_SERVICE_STATS_INC(service, error_count);
3249 switch (service->srvstate) {
3250 case VCHIQ_SRVSTATE_OPEN:
3251 status = queue_message(service->state, service,
3252 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3254 service->remoteport),
3255 copy_callback, context, size, 1);
3257 case VCHIQ_SRVSTATE_OPENSYNC:
3258 status = queue_message_sync(service->state, service,
3259 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3261 service->remoteport),
3262 copy_callback, context, size, 1);
3265 status = VCHIQ_ERROR;
3271 unlock_service(service);
3276 int vchiq_queue_kernel_message(unsigned int handle, void *data, unsigned int size)
3278 enum vchiq_status status;
3281 status = vchiq_queue_message(handle, memcpy_copy_callback,
3285 * vchiq_queue_message() may return VCHIQ_RETRY, so we need to
3286 * implement a retry mechanism since this function is supposed
3287 * to block until queued
3289 if (status != VCHIQ_RETRY)
3297 EXPORT_SYMBOL(vchiq_queue_kernel_message);
3300 vchiq_release_message(unsigned int handle,
3301 struct vchiq_header *header)
3303 struct vchiq_service *service = find_service_by_handle(handle);
3304 struct vchiq_shared_state *remote;
3305 struct vchiq_state *state;
3311 state = service->state;
3312 remote = state->remote;
3314 slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
3316 if ((slot_index >= remote->slot_first) &&
3317 (slot_index <= remote->slot_last)) {
3318 int msgid = header->msgid;
3320 if (msgid & VCHIQ_MSGID_CLAIMED) {
3321 struct vchiq_slot_info *slot_info =
3322 SLOT_INFO_FROM_INDEX(state, slot_index);
3324 release_slot(state, slot_info, header, service);
3326 } else if (slot_index == remote->slot_sync)
3327 release_message_sync(state, header);
3329 unlock_service(service);
3331 EXPORT_SYMBOL(vchiq_release_message);
3334 release_message_sync(struct vchiq_state *state, struct vchiq_header *header)
3336 header->msgid = VCHIQ_MSGID_PADDING;
3337 remote_event_signal(&state->remote->sync_release);
3341 vchiq_get_peer_version(unsigned int handle, short *peer_version)
3343 enum vchiq_status status = VCHIQ_ERROR;
3344 struct vchiq_service *service = find_service_by_handle(handle);
3349 if (vchiq_check_service(service) != VCHIQ_SUCCESS)
3355 *peer_version = service->peer_version;
3356 status = VCHIQ_SUCCESS;
3360 unlock_service(service);
3363 EXPORT_SYMBOL(vchiq_get_peer_version);
3365 void vchiq_get_config(struct vchiq_config *config)
3367 config->max_msg_size = VCHIQ_MAX_MSG_SIZE;
3368 config->bulk_threshold = VCHIQ_MAX_MSG_SIZE;
3369 config->max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
3370 config->max_services = VCHIQ_MAX_SERVICES;
3371 config->version = VCHIQ_VERSION;
3372 config->version_min = VCHIQ_VERSION_MIN;
3376 vchiq_set_service_option(unsigned int handle,
3377 enum vchiq_service_option option, int value)
3379 struct vchiq_service *service = find_service_by_handle(handle);
3380 struct vchiq_service_quota *quota;
3387 case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
3388 service->auto_close = value;
3392 case VCHIQ_SERVICE_OPTION_SLOT_QUOTA:
3393 quota = &service->state->service_quotas[service->localport];
3395 value = service->state->default_slot_quota;
3396 if ((value >= quota->slot_use_count) &&
3397 (value < (unsigned short)~0)) {
3398 quota->slot_quota = value;
3399 if ((value >= quota->slot_use_count) &&
3400 (quota->message_quota >= quota->message_use_count))
3402 * Signal the service that it may have
3403 * dropped below its quota
3405 complete("a->quota_event);
3410 case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA:
3411 quota = &service->state->service_quotas[service->localport];
3413 value = service->state->default_message_quota;
3414 if ((value >= quota->message_use_count) &&
3415 (value < (unsigned short)~0)) {
3416 quota->message_quota = value;
3417 if ((value >= quota->message_use_count) &&
3418 (quota->slot_quota >= quota->slot_use_count))
3420 * Signal the service that it may have
3421 * dropped below its quota
3423 complete("a->quota_event);
3428 case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
3429 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3430 (service->srvstate == VCHIQ_SRVSTATE_LISTENING)) {
3431 service->sync = value;
3436 case VCHIQ_SERVICE_OPTION_TRACE:
3437 service->trace = value;
3444 unlock_service(service);
3450 vchiq_dump_shared_state(void *dump_context, struct vchiq_state *state,
3451 struct vchiq_shared_state *shared, const char *label)
3453 static const char *const debug_names[] = {
3455 "SLOT_HANDLER_COUNT",
3456 "SLOT_HANDLER_LINE",
3460 "AWAIT_COMPLETION_LINE",
3461 "DEQUEUE_MESSAGE_LINE",
3462 "SERVICE_CALLBACK_LINE",
3463 "MSG_QUEUE_FULL_COUNT",
3464 "COMPLETION_QUEUE_FULL_COUNT"
3471 len = scnprintf(buf, sizeof(buf),
3472 " %s: slots %d-%d tx_pos=%x recycle=%x",
3473 label, shared->slot_first, shared->slot_last,
3474 shared->tx_pos, shared->slot_queue_recycle);
3475 err = vchiq_dump(dump_context, buf, len + 1);
3479 len = scnprintf(buf, sizeof(buf),
3481 err = vchiq_dump(dump_context, buf, len + 1);
3485 for (i = shared->slot_first; i <= shared->slot_last; i++) {
3486 struct vchiq_slot_info slot_info =
3487 *SLOT_INFO_FROM_INDEX(state, i);
3488 if (slot_info.use_count != slot_info.release_count) {
3489 len = scnprintf(buf, sizeof(buf),
3490 " %d: %d/%d", i, slot_info.use_count,
3491 slot_info.release_count);
3492 err = vchiq_dump(dump_context, buf, len + 1);
3498 for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
3499 len = scnprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
3500 debug_names[i], shared->debug[i], shared->debug[i]);
3501 err = vchiq_dump(dump_context, buf, len + 1);
3508 int vchiq_dump_state(void *dump_context, struct vchiq_state *state)
3515 len = scnprintf(buf, sizeof(buf), "State %d: %s", state->id,
3516 conn_state_names[state->conn_state]);
3517 err = vchiq_dump(dump_context, buf, len + 1);
3521 len = scnprintf(buf, sizeof(buf),
3522 " tx_pos=%x(@%pK), rx_pos=%x(@%pK)",
3523 state->local->tx_pos,
3524 state->tx_data + (state->local_tx_pos & VCHIQ_SLOT_MASK),
3526 state->rx_data + (state->rx_pos & VCHIQ_SLOT_MASK));
3527 err = vchiq_dump(dump_context, buf, len + 1);
3531 len = scnprintf(buf, sizeof(buf),
3532 " Version: %d (min %d)",
3533 VCHIQ_VERSION, VCHIQ_VERSION_MIN);
3534 err = vchiq_dump(dump_context, buf, len + 1);
3538 if (VCHIQ_ENABLE_STATS) {
3539 len = scnprintf(buf, sizeof(buf),
3540 " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, error_count=%d",
3541 state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
3542 state->stats.error_count);
3543 err = vchiq_dump(dump_context, buf, len + 1);
3548 len = scnprintf(buf, sizeof(buf),
3549 " Slots: %d available (%d data), %d recyclable, %d stalls (%d data)",
3550 ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
3551 state->local_tx_pos) / VCHIQ_SLOT_SIZE,
3552 state->data_quota - state->data_use_count,
3553 state->local->slot_queue_recycle - state->slot_queue_available,
3554 state->stats.slot_stalls, state->stats.data_stalls);
3555 err = vchiq_dump(dump_context, buf, len + 1);
3559 err = vchiq_dump_platform_state(dump_context);
3563 err = vchiq_dump_shared_state(dump_context,
3569 err = vchiq_dump_shared_state(dump_context,
3576 err = vchiq_dump_platform_instances(dump_context);
3580 for (i = 0; i < state->unused_service; i++) {
3581 struct vchiq_service *service = find_service_by_port(state, i);
3584 err = vchiq_dump_service_state(dump_context, service);
3585 unlock_service(service);
3593 int vchiq_dump_service_state(void *dump_context, struct vchiq_service *service)
3598 unsigned int ref_count;
3600 /*Don't include the lock just taken*/
3601 ref_count = kref_read(&service->ref_count) - 1;
3602 len = scnprintf(buf, sizeof(buf), "Service %u: %s (ref %u)",
3603 service->localport, srvstate_names[service->srvstate],
3606 if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
3607 char remoteport[30];
3608 struct vchiq_service_quota *quota =
3609 &service->state->service_quotas[service->localport];
3610 int fourcc = service->base.fourcc;
3611 int tx_pending, rx_pending;
3613 if (service->remoteport != VCHIQ_PORT_FREE) {
3614 int len2 = scnprintf(remoteport, sizeof(remoteport),
3615 "%u", service->remoteport);
3617 if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
3618 scnprintf(remoteport + len2,
3619 sizeof(remoteport) - len2,
3620 " (client %x)", service->client_id);
3622 strcpy(remoteport, "n/a");
3624 len += scnprintf(buf + len, sizeof(buf) - len,
3625 " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
3626 VCHIQ_FOURCC_AS_4CHARS(fourcc),
3628 quota->message_use_count,
3629 quota->message_quota,
3630 quota->slot_use_count,
3633 err = vchiq_dump(dump_context, buf, len + 1);
3637 tx_pending = service->bulk_tx.local_insert -
3638 service->bulk_tx.remote_insert;
3640 rx_pending = service->bulk_rx.local_insert -
3641 service->bulk_rx.remote_insert;
3643 len = scnprintf(buf, sizeof(buf),
3644 " Bulk: tx_pending=%d (size %d), rx_pending=%d (size %d)",
3646 tx_pending ? service->bulk_tx.bulks[
3647 BULK_INDEX(service->bulk_tx.remove)].size : 0,
3649 rx_pending ? service->bulk_rx.bulks[
3650 BULK_INDEX(service->bulk_rx.remove)].size : 0);
3652 if (VCHIQ_ENABLE_STATS) {
3653 err = vchiq_dump(dump_context, buf, len + 1);
3657 len = scnprintf(buf, sizeof(buf),
3658 " Ctrl: tx_count=%d, tx_bytes=%llu, rx_count=%d, rx_bytes=%llu",
3659 service->stats.ctrl_tx_count,
3660 service->stats.ctrl_tx_bytes,
3661 service->stats.ctrl_rx_count,
3662 service->stats.ctrl_rx_bytes);
3663 err = vchiq_dump(dump_context, buf, len + 1);
3667 len = scnprintf(buf, sizeof(buf),
3668 " Bulk: tx_count=%d, tx_bytes=%llu, rx_count=%d, rx_bytes=%llu",
3669 service->stats.bulk_tx_count,
3670 service->stats.bulk_tx_bytes,
3671 service->stats.bulk_rx_count,
3672 service->stats.bulk_rx_bytes);
3673 err = vchiq_dump(dump_context, buf, len + 1);
3677 len = scnprintf(buf, sizeof(buf),
3678 " %d quota stalls, %d slot stalls, %d bulk stalls, %d aborted, %d errors",
3679 service->stats.quota_stalls,
3680 service->stats.slot_stalls,
3681 service->stats.bulk_stalls,
3682 service->stats.bulk_aborted_count,
3683 service->stats.error_count);
3687 err = vchiq_dump(dump_context, buf, len + 1);
3691 if (service->srvstate != VCHIQ_SRVSTATE_FREE)
3692 err = vchiq_dump_platform_service_state(dump_context, service);
3697 vchiq_loud_error_header(void)
3699 vchiq_log_error(vchiq_core_log_level,
3700 "============================================================================");
3701 vchiq_log_error(vchiq_core_log_level,
3702 "============================================================================");
3703 vchiq_log_error(vchiq_core_log_level, "=====");
3707 vchiq_loud_error_footer(void)
3709 vchiq_log_error(vchiq_core_log_level, "=====");
3710 vchiq_log_error(vchiq_core_log_level,
3711 "============================================================================");
3712 vchiq_log_error(vchiq_core_log_level,
3713 "============================================================================");
3716 enum vchiq_status vchiq_send_remote_use(struct vchiq_state *state)
3718 if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED)
3721 return queue_message(state, NULL,
3722 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
3726 enum vchiq_status vchiq_send_remote_use_active(struct vchiq_state *state)
3728 if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED)
3731 return queue_message(state, NULL,
3732 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
3736 void vchiq_log_dump_mem(const char *label, u32 addr, const void *void_mem,
3739 const u8 *mem = void_mem;
3744 while (num_bytes > 0) {
3747 for (offset = 0; offset < 16; offset++) {
3748 if (offset < num_bytes)
3749 s += scnprintf(s, 4, "%02x ", mem[offset]);
3751 s += scnprintf(s, 4, " ");
3754 for (offset = 0; offset < 16; offset++) {
3755 if (offset < num_bytes) {
3756 u8 ch = mem[offset];
3758 if ((ch < ' ') || (ch > '~'))
3765 if (label && (*label != '\0'))
3766 vchiq_log_trace(VCHIQ_LOG_TRACE,
3767 "%s: %08x: %s", label, addr, line_buf);
3769 vchiq_log_trace(VCHIQ_LOG_TRACE,
3770 "%08x: %s", addr, line_buf);