2 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions, and the following disclaimer,
9 * without modification.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the above-listed copyright holders may not be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
17 * ALTERNATIVELY, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2, as published by the Free
19 * Software Foundation.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "vchiq_core.h"
35 #include "vchiq_killable.h"
37 #define VCHIQ_SLOT_HANDLER_STACK 8192
39 #define HANDLE_STATE_SHIFT 12
41 #define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
42 #define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
43 #define SLOT_INDEX_FROM_DATA(state, data) \
44 (((unsigned int)((char *)data - (char *)state->slot_data)) / \
46 #define SLOT_INDEX_FROM_INFO(state, info) \
47 ((unsigned int)(info - state->slot_info))
48 #define SLOT_QUEUE_INDEX_FROM_POS(pos) \
49 ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
51 #define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
53 #define SRVTRACE_LEVEL(srv) \
54 (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
55 #define SRVTRACE_ENABLED(srv, lev) \
56 (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
58 struct vchiq_open_payload {
65 struct vchiq_openack_payload {
71 QMFLAGS_IS_BLOCKING = (1 << 0),
72 QMFLAGS_NO_MUTEX_LOCK = (1 << 1),
73 QMFLAGS_NO_MUTEX_UNLOCK = (1 << 2)
76 /* we require this for consistency between endpoints */
77 vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
78 vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
79 vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
80 vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
81 vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
82 vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
84 /* Run time control of log level, based on KERN_XXX level. */
85 int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
86 int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
87 int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
89 static atomic_t pause_bulks_count = ATOMIC_INIT(0);
91 static DEFINE_SPINLOCK(service_spinlock);
92 DEFINE_SPINLOCK(bulk_waiter_spinlock);
93 static DEFINE_SPINLOCK(quota_spinlock);
95 VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
96 static unsigned int handle_seq;
98 static const char *const srvstate_names[] = {
111 static const char *const reason_names[] = {
115 "BULK_TRANSMIT_DONE",
117 "BULK_TRANSMIT_ABORTED",
118 "BULK_RECEIVE_ABORTED"
121 static const char *const conn_state_names[] = {
135 release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
137 static const char *msg_type_str(unsigned int msg_type)
140 case VCHIQ_MSG_PADDING: return "PADDING";
141 case VCHIQ_MSG_CONNECT: return "CONNECT";
142 case VCHIQ_MSG_OPEN: return "OPEN";
143 case VCHIQ_MSG_OPENACK: return "OPENACK";
144 case VCHIQ_MSG_CLOSE: return "CLOSE";
145 case VCHIQ_MSG_DATA: return "DATA";
146 case VCHIQ_MSG_BULK_RX: return "BULK_RX";
147 case VCHIQ_MSG_BULK_TX: return "BULK_TX";
148 case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
149 case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
150 case VCHIQ_MSG_PAUSE: return "PAUSE";
151 case VCHIQ_MSG_RESUME: return "RESUME";
152 case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
153 case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
154 case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
160 vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
162 vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
163 service->state->id, service->localport,
164 srvstate_names[service->srvstate],
165 srvstate_names[newstate]);
166 service->srvstate = newstate;
170 find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
172 VCHIQ_SERVICE_T *service;
174 spin_lock(&service_spinlock);
175 service = handle_to_service(handle);
176 if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
177 (service->handle == handle)) {
178 WARN_ON(service->ref_count == 0);
179 service->ref_count++;
182 spin_unlock(&service_spinlock);
185 vchiq_log_info(vchiq_core_log_level,
186 "Invalid service handle 0x%x", handle);
192 find_service_by_port(VCHIQ_STATE_T *state, int localport)
194 VCHIQ_SERVICE_T *service = NULL;
196 if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
197 spin_lock(&service_spinlock);
198 service = state->services[localport];
199 if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
200 WARN_ON(service->ref_count == 0);
201 service->ref_count++;
204 spin_unlock(&service_spinlock);
208 vchiq_log_info(vchiq_core_log_level,
209 "Invalid port %d", localport);
215 find_service_for_instance(VCHIQ_INSTANCE_T instance,
216 VCHIQ_SERVICE_HANDLE_T handle) {
217 VCHIQ_SERVICE_T *service;
219 spin_lock(&service_spinlock);
220 service = handle_to_service(handle);
221 if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
222 (service->handle == handle) &&
223 (service->instance == instance)) {
224 WARN_ON(service->ref_count == 0);
225 service->ref_count++;
228 spin_unlock(&service_spinlock);
231 vchiq_log_info(vchiq_core_log_level,
232 "Invalid service handle 0x%x", handle);
238 find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
239 VCHIQ_SERVICE_HANDLE_T handle) {
240 VCHIQ_SERVICE_T *service;
242 spin_lock(&service_spinlock);
243 service = handle_to_service(handle);
245 ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
246 (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
247 (service->handle == handle) &&
248 (service->instance == instance)) {
249 WARN_ON(service->ref_count == 0);
250 service->ref_count++;
253 spin_unlock(&service_spinlock);
256 vchiq_log_info(vchiq_core_log_level,
257 "Invalid service handle 0x%x", handle);
263 next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
266 VCHIQ_SERVICE_T *service = NULL;
269 spin_lock(&service_spinlock);
270 while (idx < state->unused_service) {
271 VCHIQ_SERVICE_T *srv = state->services[idx++];
273 if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
274 (srv->instance == instance)) {
276 WARN_ON(service->ref_count == 0);
277 service->ref_count++;
281 spin_unlock(&service_spinlock);
289 lock_service(VCHIQ_SERVICE_T *service)
291 spin_lock(&service_spinlock);
294 WARN_ON(service->ref_count == 0);
295 service->ref_count++;
297 spin_unlock(&service_spinlock);
301 unlock_service(VCHIQ_SERVICE_T *service)
303 spin_lock(&service_spinlock);
305 WARN(1, "%s: service is NULL\n", __func__);
308 if (!service->ref_count) {
309 WARN(1, "%s: ref_count is zero\n", __func__);
312 service->ref_count--;
313 if (!service->ref_count) {
314 VCHIQ_STATE_T *state = service->state;
316 WARN_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
317 state->services[service->localport] = NULL;
322 spin_unlock(&service_spinlock);
324 if (service && service->userdata_term)
325 service->userdata_term(service->base.userdata);
331 vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
333 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
336 id = service ? service->client_id : 0;
338 unlock_service(service);
344 vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
346 VCHIQ_SERVICE_T *service = handle_to_service(handle);
348 return service ? service->base.userdata : NULL;
352 vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
354 VCHIQ_SERVICE_T *service = handle_to_service(handle);
356 return service ? service->base.fourcc : 0;
360 mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
362 VCHIQ_STATE_T *state = service->state;
363 VCHIQ_SERVICE_QUOTA_T *service_quota;
365 service->closing = 1;
367 /* Synchronise with other threads. */
368 mutex_lock(&state->recycle_mutex);
369 mutex_unlock(&state->recycle_mutex);
370 if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
371 /* If we're pausing then the slot_mutex is held until resume
372 * by the slot handler. Therefore don't try to acquire this
373 * mutex if we're the slot handler and in the pause sent state.
374 * We don't need to in this case anyway. */
375 mutex_lock(&state->slot_mutex);
376 mutex_unlock(&state->slot_mutex);
379 /* Unblock any sending thread. */
380 service_quota = &state->service_quotas[service->localport];
381 up(&service_quota->quota_event);
385 mark_service_closing(VCHIQ_SERVICE_T *service)
387 mark_service_closing_internal(service, 0);
390 static inline VCHIQ_STATUS_T
391 make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
392 VCHIQ_HEADER_T *header, void *bulk_userdata)
394 VCHIQ_STATUS_T status;
396 vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %pK, %pK)",
397 service->state->id, service->localport, reason_names[reason],
398 header, bulk_userdata);
399 status = service->base.callback(reason, header, service->handle,
401 if (status == VCHIQ_ERROR) {
402 vchiq_log_warning(vchiq_core_log_level,
403 "%d: ignoring ERROR from callback to service %x",
404 service->state->id, service->handle);
405 status = VCHIQ_SUCCESS;
411 vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
413 VCHIQ_CONNSTATE_T oldstate = state->conn_state;
415 vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
416 conn_state_names[oldstate],
417 conn_state_names[newstate]);
418 state->conn_state = newstate;
419 vchiq_platform_conn_state_changed(state, oldstate, newstate);
423 remote_event_create(VCHIQ_STATE_T *state, REMOTE_EVENT_T *event)
426 /* Don't clear the 'fired' flag because it may already have been set
427 ** by the other side. */
428 sema_init((struct semaphore *)((char *)state + event->event), 0);
432 remote_event_wait(VCHIQ_STATE_T *state, REMOTE_EVENT_T *event)
438 if (down_interruptible(
440 ((char *)state + event->event)) != 0) {
454 remote_event_signal_local(VCHIQ_STATE_T *state, REMOTE_EVENT_T *event)
457 up((struct semaphore *)((char *)state + event->event));
461 remote_event_poll(VCHIQ_STATE_T *state, REMOTE_EVENT_T *event)
463 if (event->fired && event->armed)
464 remote_event_signal_local(state, event);
468 remote_event_pollall(VCHIQ_STATE_T *state)
470 remote_event_poll(state, &state->local->sync_trigger);
471 remote_event_poll(state, &state->local->sync_release);
472 remote_event_poll(state, &state->local->trigger);
473 remote_event_poll(state, &state->local->recycle);
476 /* Round up message sizes so that any space at the end of a slot is always big
477 ** enough for a header. This relies on header size being a power of two, which
478 ** has been verified earlier by a static assertion. */
481 calc_stride(size_t size)
483 /* Allow room for the header */
484 size += sizeof(VCHIQ_HEADER_T);
487 return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
491 /* Called by the slot handler thread */
492 static VCHIQ_SERVICE_T *
493 get_listening_service(VCHIQ_STATE_T *state, int fourcc)
497 WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
499 for (i = 0; i < state->unused_service; i++) {
500 VCHIQ_SERVICE_T *service = state->services[i];
503 (service->public_fourcc == fourcc) &&
504 ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
505 ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
506 (service->remoteport == VCHIQ_PORT_FREE)))) {
507 lock_service(service);
515 /* Called by the slot handler thread */
516 static VCHIQ_SERVICE_T *
517 get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
521 for (i = 0; i < state->unused_service; i++) {
522 VCHIQ_SERVICE_T *service = state->services[i];
524 if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
525 && (service->remoteport == port)) {
526 lock_service(service);
534 request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
540 value = atomic_read(&service->poll_flags);
541 } while (atomic_cmpxchg(&service->poll_flags, value,
542 value | (1 << poll_type)) != value);
545 value = atomic_read(&state->poll_services[
546 service->localport>>5]);
547 } while (atomic_cmpxchg(
548 &state->poll_services[service->localport>>5],
549 value, value | (1 << (service->localport & 0x1f)))
553 state->poll_needed = 1;
556 /* ... and ensure the slot handler runs. */
557 remote_event_signal_local(state, &state->local->trigger);
560 /* Called from queue_message, by the slot handler and application threads,
561 ** with slot_mutex held */
562 static VCHIQ_HEADER_T *
563 reserve_space(VCHIQ_STATE_T *state, size_t space, int is_blocking)
565 VCHIQ_SHARED_STATE_T *local = state->local;
566 int tx_pos = state->local_tx_pos;
567 int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
569 if (space > slot_space) {
570 VCHIQ_HEADER_T *header;
571 /* Fill the remaining space with padding */
572 WARN_ON(state->tx_data == NULL);
573 header = (VCHIQ_HEADER_T *)
574 (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
575 header->msgid = VCHIQ_MSGID_PADDING;
576 header->size = slot_space - sizeof(VCHIQ_HEADER_T);
578 tx_pos += slot_space;
581 /* If necessary, get the next slot. */
582 if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
585 /* If there is no free slot... */
587 if (down_trylock(&state->slot_available_event) != 0) {
588 /* ...wait for one. */
590 VCHIQ_STATS_INC(state, slot_stalls);
592 /* But first, flush through the last slot. */
593 state->local_tx_pos = tx_pos;
594 local->tx_pos = tx_pos;
595 remote_event_signal(&state->remote->trigger);
599 &state->slot_available_event) != 0))
600 return NULL; /* No space available */
603 if (tx_pos == (state->slot_queue_available * VCHIQ_SLOT_SIZE)) {
604 up(&state->slot_available_event);
605 pr_warn("%s: invalid tx_pos: %d\n", __func__, tx_pos);
609 slot_index = local->slot_queue[
610 SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
611 VCHIQ_SLOT_QUEUE_MASK];
613 (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
616 state->local_tx_pos = tx_pos + space;
618 return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
621 /* Called by the recycle thread. */
623 process_free_queue(VCHIQ_STATE_T *state)
625 VCHIQ_SHARED_STATE_T *local = state->local;
626 BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
627 int slot_queue_available;
629 /* Find slots which have been freed by the other side, and return them
630 ** to the available queue. */
631 slot_queue_available = state->slot_queue_available;
634 * Use a memory barrier to ensure that any state that may have been
635 * modified by another thread is not masked by stale prefetched
640 while (slot_queue_available != local->slot_queue_recycle) {
642 int slot_index = local->slot_queue[slot_queue_available++ &
643 VCHIQ_SLOT_QUEUE_MASK];
644 char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
648 * Beware of the address dependency - data is calculated
649 * using an index written by the other side.
653 vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%pK %x %x",
654 state->id, slot_index, data,
655 local->slot_queue_recycle, slot_queue_available);
657 /* Initialise the bitmask for services which have used this
659 BITSET_ZERO(service_found);
663 while (pos < VCHIQ_SLOT_SIZE) {
664 VCHIQ_HEADER_T *header =
665 (VCHIQ_HEADER_T *)(data + pos);
666 int msgid = header->msgid;
668 if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
669 int port = VCHIQ_MSG_SRCPORT(msgid);
670 VCHIQ_SERVICE_QUOTA_T *service_quota =
671 &state->service_quotas[port];
674 spin_lock("a_spinlock);
675 count = service_quota->message_use_count;
677 service_quota->message_use_count =
679 spin_unlock("a_spinlock);
681 if (count == service_quota->message_quota)
682 /* Signal the service that it
683 ** has dropped below its quota
685 up(&service_quota->quota_event);
686 else if (count == 0) {
687 vchiq_log_error(vchiq_core_log_level,
688 "service %d message_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
690 service_quota->message_use_count,
691 header, msgid, header->msgid,
693 WARN(1, "invalid message use count\n");
695 if (!BITSET_IS_SET(service_found, port)) {
696 /* Set the found bit for this service */
697 BITSET_SET(service_found, port);
699 spin_lock("a_spinlock);
700 count = service_quota->slot_use_count;
702 service_quota->slot_use_count =
704 spin_unlock("a_spinlock);
707 /* Signal the service in case
708 ** it has dropped below its
710 up(&service_quota->quota_event);
712 vchiq_core_log_level,
713 "%d: pfq:%d %x@%pK - slot_use->%d",
715 header->size, header,
719 vchiq_core_log_level,
720 "service %d slot_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
722 msgid, header->msgid,
724 WARN(1, "bad slot use count\n");
731 pos += calc_stride(header->size);
732 if (pos > VCHIQ_SLOT_SIZE) {
733 vchiq_log_error(vchiq_core_log_level,
734 "pfq - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
735 pos, header, msgid, header->msgid,
737 WARN(1, "invalid slot position\n");
744 spin_lock("a_spinlock);
745 count = state->data_use_count;
747 state->data_use_count =
749 spin_unlock("a_spinlock);
750 if (count == state->data_quota)
751 up(&state->data_quota_event);
755 * Don't allow the slot to be reused until we are no
756 * longer interested in it.
760 state->slot_queue_available = slot_queue_available;
761 up(&state->slot_available_event);
766 memcpy_copy_callback(
767 void *context, void *dest,
768 size_t offset, size_t maxsize)
770 memcpy(dest + offset, context + offset, maxsize);
776 ssize_t (*copy_callback)(void *context, void *dest,
777 size_t offset, size_t maxsize),
785 ssize_t callback_result;
786 size_t max_bytes = size - pos;
789 copy_callback(context, dest + pos,
792 if (callback_result < 0)
793 return callback_result;
795 if (!callback_result)
798 if (callback_result > max_bytes)
801 pos += callback_result;
807 /* Called by the slot handler and application threads */
808 static VCHIQ_STATUS_T
809 queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
811 ssize_t (*copy_callback)(void *context, void *dest,
812 size_t offset, size_t maxsize),
817 VCHIQ_SHARED_STATE_T *local;
818 VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
819 VCHIQ_HEADER_T *header;
820 int type = VCHIQ_MSG_TYPE(msgid);
824 local = state->local;
826 stride = calc_stride(size);
828 WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
830 if (!(flags & QMFLAGS_NO_MUTEX_LOCK) &&
831 (mutex_lock_killable(&state->slot_mutex) != 0))
834 if (type == VCHIQ_MSG_DATA) {
838 WARN(1, "%s: service is NULL\n", __func__);
839 mutex_unlock(&state->slot_mutex);
843 WARN_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
844 QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
846 if (service->closing) {
847 /* The service has been closed */
848 mutex_unlock(&state->slot_mutex);
852 service_quota = &state->service_quotas[service->localport];
854 spin_lock("a_spinlock);
856 /* Ensure this service doesn't use more than its quota of
857 ** messages or slots */
858 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
859 state->local_tx_pos + stride - 1);
861 /* Ensure data messages don't use more than their quota of
863 while ((tx_end_index != state->previous_data_index) &&
864 (state->data_use_count == state->data_quota)) {
865 VCHIQ_STATS_INC(state, data_stalls);
866 spin_unlock("a_spinlock);
867 mutex_unlock(&state->slot_mutex);
869 if (down_interruptible(&state->data_quota_event)
873 mutex_lock(&state->slot_mutex);
874 spin_lock("a_spinlock);
875 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
876 state->local_tx_pos + stride - 1);
877 if ((tx_end_index == state->previous_data_index) ||
878 (state->data_use_count < state->data_quota)) {
879 /* Pass the signal on to other waiters */
880 up(&state->data_quota_event);
885 while ((service_quota->message_use_count ==
886 service_quota->message_quota) ||
887 ((tx_end_index != service_quota->previous_tx_index) &&
888 (service_quota->slot_use_count ==
889 service_quota->slot_quota))) {
890 spin_unlock("a_spinlock);
891 vchiq_log_trace(vchiq_core_log_level,
892 "%d: qm:%d %s,%zx - quota stall "
894 state->id, service->localport,
895 msg_type_str(type), size,
896 service_quota->message_use_count,
897 service_quota->slot_use_count);
898 VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
899 mutex_unlock(&state->slot_mutex);
900 if (down_interruptible(&service_quota->quota_event)
903 if (service->closing)
905 if (mutex_lock_killable(&state->slot_mutex) != 0)
907 if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
908 /* The service has been closed */
909 mutex_unlock(&state->slot_mutex);
912 spin_lock("a_spinlock);
913 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
914 state->local_tx_pos + stride - 1);
917 spin_unlock("a_spinlock);
920 header = reserve_space(state, stride, flags & QMFLAGS_IS_BLOCKING);
924 VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
925 /* In the event of a failure, return the mutex to the
927 if (!(flags & QMFLAGS_NO_MUTEX_LOCK))
928 mutex_unlock(&state->slot_mutex);
932 if (type == VCHIQ_MSG_DATA) {
933 ssize_t callback_result;
937 vchiq_log_info(vchiq_core_log_level,
938 "%d: qm %s@%pK,%zx (%d->%d)",
939 state->id, msg_type_str(VCHIQ_MSG_TYPE(msgid)),
940 header, size, VCHIQ_MSG_SRCPORT(msgid),
941 VCHIQ_MSG_DSTPORT(msgid));
943 WARN_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
944 QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
947 copy_message_data(copy_callback, context,
950 if (callback_result < 0) {
951 mutex_unlock(&state->slot_mutex);
952 VCHIQ_SERVICE_STATS_INC(service,
957 if (SRVTRACE_ENABLED(service,
959 vchiq_log_dump_mem("Sent", 0,
962 (size_t)callback_result));
964 spin_lock("a_spinlock);
965 service_quota->message_use_count++;
968 SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
970 /* If this transmission can't fit in the last slot used by any
971 ** service, the data_use_count must be increased. */
972 if (tx_end_index != state->previous_data_index) {
973 state->previous_data_index = tx_end_index;
974 state->data_use_count++;
977 /* If this isn't the same slot last used by this service,
978 ** the service's slot_use_count must be increased. */
979 if (tx_end_index != service_quota->previous_tx_index) {
980 service_quota->previous_tx_index = tx_end_index;
981 slot_use_count = ++service_quota->slot_use_count;
986 spin_unlock("a_spinlock);
989 vchiq_log_trace(vchiq_core_log_level,
990 "%d: qm:%d %s,%zx - slot_use->%d (hdr %p)",
991 state->id, service->localport,
992 msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
993 slot_use_count, header);
995 VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
996 VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
998 vchiq_log_info(vchiq_core_log_level,
999 "%d: qm %s@%pK,%zx (%d->%d)", state->id,
1000 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1001 header, size, VCHIQ_MSG_SRCPORT(msgid),
1002 VCHIQ_MSG_DSTPORT(msgid));
1004 /* It is assumed for now that this code path
1005 * only happens from calls inside this file.
1007 * External callers are through the vchiq_queue_message
1008 * path which always sets the type to be VCHIQ_MSG_DATA
1010 * At first glance this appears to be correct but
1011 * more review is needed.
1013 copy_message_data(copy_callback, context,
1014 header->data, size);
1016 VCHIQ_STATS_INC(state, ctrl_tx_count);
1019 header->msgid = msgid;
1020 header->size = size;
1025 svc_fourcc = service
1026 ? service->base.fourcc
1027 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1029 vchiq_log_info(SRVTRACE_LEVEL(service),
1030 "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%zu",
1031 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1032 VCHIQ_MSG_TYPE(msgid),
1033 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1034 VCHIQ_MSG_SRCPORT(msgid),
1035 VCHIQ_MSG_DSTPORT(msgid),
1039 /* Make sure the new header is visible to the peer. */
1042 /* Make the new tx_pos visible to the peer. */
1043 local->tx_pos = state->local_tx_pos;
1046 if (service && (type == VCHIQ_MSG_CLOSE))
1047 vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
1049 if (!(flags & QMFLAGS_NO_MUTEX_UNLOCK))
1050 mutex_unlock(&state->slot_mutex);
1052 remote_event_signal(&state->remote->trigger);
1054 return VCHIQ_SUCCESS;
1057 /* Called by the slot handler and application threads */
1058 static VCHIQ_STATUS_T
1059 queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
1061 ssize_t (*copy_callback)(void *context, void *dest,
1062 size_t offset, size_t maxsize),
1067 VCHIQ_SHARED_STATE_T *local;
1068 VCHIQ_HEADER_T *header;
1069 ssize_t callback_result;
1071 local = state->local;
1073 if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
1074 (mutex_lock_killable(&state->sync_mutex) != 0))
1077 remote_event_wait(state, &local->sync_release);
1081 header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
1085 int oldmsgid = header->msgid;
1087 if (oldmsgid != VCHIQ_MSGID_PADDING)
1088 vchiq_log_error(vchiq_core_log_level,
1089 "%d: qms - msgid %x, not PADDING",
1090 state->id, oldmsgid);
1093 vchiq_log_info(vchiq_sync_log_level,
1094 "%d: qms %s@%pK,%x (%d->%d)", state->id,
1095 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1096 header, size, VCHIQ_MSG_SRCPORT(msgid),
1097 VCHIQ_MSG_DSTPORT(msgid));
1100 copy_message_data(copy_callback, context,
1101 header->data, size);
1103 if (callback_result < 0) {
1104 mutex_unlock(&state->slot_mutex);
1105 VCHIQ_SERVICE_STATS_INC(service,
1111 if (SRVTRACE_ENABLED(service,
1113 vchiq_log_dump_mem("Sent", 0,
1116 (size_t)callback_result));
1118 VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
1119 VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
1121 VCHIQ_STATS_INC(state, ctrl_tx_count);
1124 header->size = size;
1125 header->msgid = msgid;
1127 if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
1130 svc_fourcc = service
1131 ? service->base.fourcc
1132 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1134 vchiq_log_trace(vchiq_sync_log_level,
1135 "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
1136 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1137 VCHIQ_MSG_TYPE(msgid),
1138 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1139 VCHIQ_MSG_SRCPORT(msgid),
1140 VCHIQ_MSG_DSTPORT(msgid),
1144 /* Make sure the new header is visible to the peer. */
1147 remote_event_signal(&state->remote->sync_trigger);
1149 if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
1150 mutex_unlock(&state->sync_mutex);
1152 return VCHIQ_SUCCESS;
1156 claim_slot(VCHIQ_SLOT_INFO_T *slot)
1162 release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
1163 VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
1167 mutex_lock(&state->recycle_mutex);
1170 int msgid = header->msgid;
1172 if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
1173 (service && service->closing)) {
1174 mutex_unlock(&state->recycle_mutex);
1178 /* Rewrite the message header to prevent a double
1180 header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
1183 release_count = slot_info->release_count;
1184 slot_info->release_count = ++release_count;
1186 if (release_count == slot_info->use_count) {
1187 int slot_queue_recycle;
1188 /* Add to the freed queue */
1190 /* A read barrier is necessary here to prevent speculative
1191 ** fetches of remote->slot_queue_recycle from overtaking the
1195 slot_queue_recycle = state->remote->slot_queue_recycle;
1196 state->remote->slot_queue[slot_queue_recycle &
1197 VCHIQ_SLOT_QUEUE_MASK] =
1198 SLOT_INDEX_FROM_INFO(state, slot_info);
1199 state->remote->slot_queue_recycle = slot_queue_recycle + 1;
1200 vchiq_log_info(vchiq_core_log_level,
1201 "%d: release_slot %d - recycle->%x",
1202 state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
1203 state->remote->slot_queue_recycle);
1205 /* A write barrier is necessary, but remote_event_signal
1207 remote_event_signal(&state->remote->recycle);
1210 mutex_unlock(&state->recycle_mutex);
1213 /* Called by the slot handler - don't hold the bulk mutex */
1214 static VCHIQ_STATUS_T
1215 notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
1218 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
1220 vchiq_log_trace(vchiq_core_log_level,
1221 "%d: nb:%d %cx - p=%x rn=%x r=%x",
1222 service->state->id, service->localport,
1223 (queue == &service->bulk_tx) ? 't' : 'r',
1224 queue->process, queue->remote_notify, queue->remove);
1226 if (service->state->is_master) {
1227 while (queue->remote_notify != queue->process) {
1228 VCHIQ_BULK_T *bulk =
1229 &queue->bulks[BULK_INDEX(queue->remote_notify)];
1230 int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
1231 VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
1232 int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
1233 service->remoteport);
1234 /* Only reply to non-dummy bulk requests */
1235 if (bulk->remote_data) {
1236 status = queue_message(
1240 memcpy_copy_callback,
1244 if (status != VCHIQ_SUCCESS)
1247 queue->remote_notify++;
1250 queue->remote_notify = queue->process;
1253 if (status == VCHIQ_SUCCESS) {
1254 while (queue->remove != queue->remote_notify) {
1255 VCHIQ_BULK_T *bulk =
1256 &queue->bulks[BULK_INDEX(queue->remove)];
1258 /* Only generate callbacks for non-dummy bulk
1259 ** requests, and non-terminated services */
1260 if (bulk->data && service->instance) {
1261 if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
1262 if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
1263 VCHIQ_SERVICE_STATS_INC(service,
1265 VCHIQ_SERVICE_STATS_ADD(service,
1269 VCHIQ_SERVICE_STATS_INC(service,
1271 VCHIQ_SERVICE_STATS_ADD(service,
1276 VCHIQ_SERVICE_STATS_INC(service,
1277 bulk_aborted_count);
1279 if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
1280 struct bulk_waiter *waiter;
1282 spin_lock(&bulk_waiter_spinlock);
1283 waiter = bulk->userdata;
1285 waiter->actual = bulk->actual;
1288 spin_unlock(&bulk_waiter_spinlock);
1289 } else if (bulk->mode ==
1290 VCHIQ_BULK_MODE_CALLBACK) {
1291 VCHIQ_REASON_T reason = (bulk->dir ==
1292 VCHIQ_BULK_TRANSMIT) ?
1294 VCHIQ_BULK_ACTUAL_ABORTED) ?
1295 VCHIQ_BULK_TRANSMIT_ABORTED :
1296 VCHIQ_BULK_TRANSMIT_DONE) :
1298 VCHIQ_BULK_ACTUAL_ABORTED) ?
1299 VCHIQ_BULK_RECEIVE_ABORTED :
1300 VCHIQ_BULK_RECEIVE_DONE);
1301 status = make_service_callback(service,
1302 reason, NULL, bulk->userdata);
1303 if (status == VCHIQ_RETRY)
1309 up(&service->bulk_remove_event);
1312 status = VCHIQ_SUCCESS;
1315 if (status == VCHIQ_RETRY)
1316 request_poll(service->state, service,
1317 (queue == &service->bulk_tx) ?
1318 VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
1323 /* Called by the slot handler thread */
1325 poll_services(VCHIQ_STATE_T *state)
1329 for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
1332 flags = atomic_xchg(&state->poll_services[group], 0);
1333 for (i = 0; flags; i++) {
1334 if (flags & (1 << i)) {
1335 VCHIQ_SERVICE_T *service =
1336 find_service_by_port(state,
1344 atomic_xchg(&service->poll_flags, 0);
1346 (1 << VCHIQ_POLL_REMOVE)) {
1347 vchiq_log_info(vchiq_core_log_level,
1348 "%d: ps - remove %d<->%d",
1349 state->id, service->localport,
1350 service->remoteport);
1352 /* Make it look like a client, because
1353 it must be removed and not left in
1354 the LISTENING state. */
1355 service->public_fourcc =
1356 VCHIQ_FOURCC_INVALID;
1358 if (vchiq_close_service_internal(
1359 service, 0/*!close_recvd*/) !=
1361 request_poll(state, service,
1363 } else if (service_flags &
1364 (1 << VCHIQ_POLL_TERMINATE)) {
1365 vchiq_log_info(vchiq_core_log_level,
1366 "%d: ps - terminate %d<->%d",
1367 state->id, service->localport,
1368 service->remoteport);
1369 if (vchiq_close_service_internal(
1370 service, 0/*!close_recvd*/) !=
1372 request_poll(state, service,
1373 VCHIQ_POLL_TERMINATE);
1375 if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
1376 notify_bulks(service,
1379 if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
1380 notify_bulks(service,
1383 unlock_service(service);
1389 /* Called by the slot handler or application threads, holding the bulk mutex. */
1391 resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
1393 VCHIQ_STATE_T *state = service->state;
1396 while ((queue->process != queue->local_insert) &&
1397 (queue->process != queue->remote_insert)) {
1398 VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
1400 vchiq_log_trace(vchiq_core_log_level,
1401 "%d: rb:%d %cx - li=%x ri=%x p=%x",
1402 state->id, service->localport,
1403 (queue == &service->bulk_tx) ? 't' : 'r',
1404 queue->local_insert, queue->remote_insert,
1407 WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
1408 WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
1410 if (mutex_lock_killable(&state->bulk_transfer_mutex))
1413 vchiq_transfer_bulk(bulk);
1414 mutex_unlock(&state->bulk_transfer_mutex);
1416 if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
1417 const char *header = (queue == &service->bulk_tx) ?
1418 "Send Bulk to" : "Recv Bulk from";
1419 if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
1420 vchiq_log_info(SRVTRACE_LEVEL(service),
1421 "%s %c%c%c%c d:%d len:%d %pK<->%pK",
1423 VCHIQ_FOURCC_AS_4CHARS(
1424 service->base.fourcc),
1425 service->remoteport, bulk->size,
1426 bulk->data, bulk->remote_data);
1428 vchiq_log_info(SRVTRACE_LEVEL(service),
1429 "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
1430 " rx len:%d %pK<->%pK",
1432 VCHIQ_FOURCC_AS_4CHARS(
1433 service->base.fourcc),
1434 service->remoteport,
1435 bulk->size, bulk->remote_size,
1436 bulk->data, bulk->remote_data);
1439 vchiq_complete_bulk(bulk);
1446 /* Called with the bulk_mutex held */
1448 abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
1450 int is_tx = (queue == &service->bulk_tx);
1452 vchiq_log_trace(vchiq_core_log_level,
1453 "%d: aob:%d %cx - li=%x ri=%x p=%x",
1454 service->state->id, service->localport, is_tx ? 't' : 'r',
1455 queue->local_insert, queue->remote_insert, queue->process);
1457 WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
1458 WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
1460 while ((queue->process != queue->local_insert) ||
1461 (queue->process != queue->remote_insert)) {
1462 VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
1464 if (queue->process == queue->remote_insert) {
1465 /* fabricate a matching dummy bulk */
1466 bulk->remote_data = NULL;
1467 bulk->remote_size = 0;
1468 queue->remote_insert++;
1471 if (queue->process != queue->local_insert) {
1472 vchiq_complete_bulk(bulk);
1474 vchiq_log_info(SRVTRACE_LEVEL(service),
1475 "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
1477 is_tx ? "Send Bulk to" : "Recv Bulk from",
1478 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1479 service->remoteport,
1483 /* fabricate a matching dummy bulk */
1486 bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
1487 bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
1489 queue->local_insert++;
1496 /* Called from the slot handler thread */
1498 pause_bulks(VCHIQ_STATE_T *state)
1500 if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
1502 atomic_set(&pause_bulks_count, 1);
1506 /* Block bulk transfers from all services */
1507 mutex_lock(&state->bulk_transfer_mutex);
1510 /* Called from the slot handler thread */
1512 resume_bulks(VCHIQ_STATE_T *state)
1516 if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
1518 atomic_set(&pause_bulks_count, 0);
1522 /* Allow bulk transfers from all services */
1523 mutex_unlock(&state->bulk_transfer_mutex);
1525 if (state->deferred_bulks == 0)
1528 /* Deal with any bulks which had to be deferred due to being in
1529 * paused state. Don't try to match up to number of deferred bulks
1530 * in case we've had something come and close the service in the
1531 * interim - just process all bulk queues for all services */
1532 vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
1533 __func__, state->deferred_bulks);
1535 for (i = 0; i < state->unused_service; i++) {
1536 VCHIQ_SERVICE_T *service = state->services[i];
1537 int resolved_rx = 0;
1538 int resolved_tx = 0;
1540 if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
1543 mutex_lock(&service->bulk_mutex);
1544 resolved_rx = resolve_bulks(service, &service->bulk_rx);
1545 resolved_tx = resolve_bulks(service, &service->bulk_tx);
1546 mutex_unlock(&service->bulk_mutex);
1548 notify_bulks(service, &service->bulk_rx, 1);
1550 notify_bulks(service, &service->bulk_tx, 1);
1552 state->deferred_bulks = 0;
1556 parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
1558 VCHIQ_SERVICE_T *service = NULL;
1560 unsigned int localport, remoteport;
1562 msgid = header->msgid;
1563 size = header->size;
1564 localport = VCHIQ_MSG_DSTPORT(msgid);
1565 remoteport = VCHIQ_MSG_SRCPORT(msgid);
1566 if (size >= sizeof(struct vchiq_open_payload)) {
1567 const struct vchiq_open_payload *payload =
1568 (struct vchiq_open_payload *)header->data;
1569 unsigned int fourcc;
1571 fourcc = payload->fourcc;
1572 vchiq_log_info(vchiq_core_log_level,
1573 "%d: prs OPEN@%pK (%d->'%c%c%c%c')",
1574 state->id, header, localport,
1575 VCHIQ_FOURCC_AS_4CHARS(fourcc));
1577 service = get_listening_service(state, fourcc);
1580 /* A matching service exists */
1581 short version = payload->version;
1582 short version_min = payload->version_min;
1584 if ((service->version < version_min) ||
1585 (version < service->version_min)) {
1586 /* Version mismatch */
1587 vchiq_loud_error_header();
1588 vchiq_loud_error("%d: service %d (%c%c%c%c) "
1589 "version mismatch - local (%d, min %d)"
1590 " vs. remote (%d, min %d)",
1591 state->id, service->localport,
1592 VCHIQ_FOURCC_AS_4CHARS(fourcc),
1593 service->version, service->version_min,
1594 version, version_min);
1595 vchiq_loud_error_footer();
1596 unlock_service(service);
1600 service->peer_version = version;
1602 if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
1603 struct vchiq_openack_payload ack_payload = {
1607 if (state->version_common <
1608 VCHIQ_VERSION_SYNCHRONOUS_MODE)
1611 /* Acknowledge the OPEN */
1612 if (service->sync &&
1613 (state->version_common >=
1614 VCHIQ_VERSION_SYNCHRONOUS_MODE)) {
1615 if (queue_message_sync(
1622 memcpy_copy_callback,
1624 sizeof(ack_payload),
1626 goto bail_not_ready;
1628 if (queue_message(state,
1634 memcpy_copy_callback,
1636 sizeof(ack_payload),
1638 goto bail_not_ready;
1641 /* The service is now open */
1642 vchiq_set_service_state(service,
1643 service->sync ? VCHIQ_SRVSTATE_OPENSYNC
1644 : VCHIQ_SRVSTATE_OPEN);
1647 service->remoteport = remoteport;
1648 service->client_id = ((int *)header->data)[1];
1649 if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
1650 NULL, NULL) == VCHIQ_RETRY) {
1651 /* Bail out if not ready */
1652 service->remoteport = VCHIQ_PORT_FREE;
1653 goto bail_not_ready;
1656 /* Success - the message has been dealt with */
1657 unlock_service(service);
1663 /* No available service, or an invalid request - send a CLOSE */
1664 if (queue_message(state, NULL,
1665 VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
1666 NULL, NULL, 0, 0) == VCHIQ_RETRY)
1667 goto bail_not_ready;
1673 unlock_service(service);
1678 /* Called by the slot handler thread */
1680 parse_rx_slots(VCHIQ_STATE_T *state)
1682 VCHIQ_SHARED_STATE_T *remote = state->remote;
1683 VCHIQ_SERVICE_T *service = NULL;
1686 DEBUG_INITIALISE(state->local)
1688 tx_pos = remote->tx_pos;
1690 while (state->rx_pos != tx_pos) {
1691 VCHIQ_HEADER_T *header;
1694 unsigned int localport, remoteport;
1696 DEBUG_TRACE(PARSE_LINE);
1697 if (!state->rx_data) {
1700 WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
1701 rx_index = remote->slot_queue[
1702 SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
1703 VCHIQ_SLOT_QUEUE_MASK];
1704 state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
1706 state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
1708 /* Initialise use_count to one, and increment
1709 ** release_count at the end of the slot to avoid
1710 ** releasing the slot prematurely. */
1711 state->rx_info->use_count = 1;
1712 state->rx_info->release_count = 0;
1715 header = (VCHIQ_HEADER_T *)(state->rx_data +
1716 (state->rx_pos & VCHIQ_SLOT_MASK));
1717 DEBUG_VALUE(PARSE_HEADER, (int)(long)header);
1718 msgid = header->msgid;
1719 DEBUG_VALUE(PARSE_MSGID, msgid);
1720 size = header->size;
1721 type = VCHIQ_MSG_TYPE(msgid);
1722 localport = VCHIQ_MSG_DSTPORT(msgid);
1723 remoteport = VCHIQ_MSG_SRCPORT(msgid);
1725 if (type != VCHIQ_MSG_DATA)
1726 VCHIQ_STATS_INC(state, ctrl_rx_count);
1729 case VCHIQ_MSG_OPENACK:
1730 case VCHIQ_MSG_CLOSE:
1731 case VCHIQ_MSG_DATA:
1732 case VCHIQ_MSG_BULK_RX:
1733 case VCHIQ_MSG_BULK_TX:
1734 case VCHIQ_MSG_BULK_RX_DONE:
1735 case VCHIQ_MSG_BULK_TX_DONE:
1736 service = find_service_by_port(state, localport);
1738 ((service->remoteport != remoteport) &&
1739 (service->remoteport != VCHIQ_PORT_FREE))) &&
1741 (type == VCHIQ_MSG_CLOSE)) {
1742 /* This could be a CLOSE from a client which
1743 hadn't yet received the OPENACK - look for
1744 the connected service */
1746 unlock_service(service);
1747 service = get_connected_service(state,
1750 vchiq_log_warning(vchiq_core_log_level,
1751 "%d: prs %s@%pK (%d->%d) - found connected service %d",
1752 state->id, msg_type_str(type),
1753 header, remoteport, localport,
1754 service->localport);
1758 vchiq_log_error(vchiq_core_log_level,
1759 "%d: prs %s@%pK (%d->%d) - invalid/closed service %d",
1760 state->id, msg_type_str(type),
1761 header, remoteport, localport,
1770 if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
1773 svc_fourcc = service
1774 ? service->base.fourcc
1775 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1776 vchiq_log_info(SRVTRACE_LEVEL(service),
1777 "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
1779 msg_type_str(type), type,
1780 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1781 remoteport, localport, size);
1783 vchiq_log_dump_mem("Rcvd", 0, header->data,
1787 if (((unsigned long)header & VCHIQ_SLOT_MASK) +
1788 calc_stride(size) > VCHIQ_SLOT_SIZE) {
1789 vchiq_log_error(vchiq_core_log_level,
1790 "header %pK (msgid %x) - size %x too big for slot",
1791 header, (unsigned int)msgid,
1792 (unsigned int)size);
1793 WARN(1, "oversized for slot\n");
1797 case VCHIQ_MSG_OPEN:
1798 WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
1799 if (!parse_open(state, header))
1800 goto bail_not_ready;
1802 case VCHIQ_MSG_OPENACK:
1803 if (size >= sizeof(struct vchiq_openack_payload)) {
1804 const struct vchiq_openack_payload *payload =
1805 (struct vchiq_openack_payload *)
1807 service->peer_version = payload->version;
1809 vchiq_log_info(vchiq_core_log_level,
1810 "%d: prs OPENACK@%pK,%x (%d->%d) v:%d",
1811 state->id, header, size, remoteport, localport,
1812 service->peer_version);
1813 if (service->srvstate ==
1814 VCHIQ_SRVSTATE_OPENING) {
1815 service->remoteport = remoteport;
1816 vchiq_set_service_state(service,
1817 VCHIQ_SRVSTATE_OPEN);
1818 up(&service->remove_event);
1820 vchiq_log_error(vchiq_core_log_level,
1821 "OPENACK received in state %s",
1822 srvstate_names[service->srvstate]);
1824 case VCHIQ_MSG_CLOSE:
1825 WARN_ON(size != 0); /* There should be no data */
1827 vchiq_log_info(vchiq_core_log_level,
1828 "%d: prs CLOSE@%pK (%d->%d)",
1829 state->id, header, remoteport, localport);
1831 mark_service_closing_internal(service, 1);
1833 if (vchiq_close_service_internal(service,
1834 1/*close_recvd*/) == VCHIQ_RETRY)
1835 goto bail_not_ready;
1837 vchiq_log_info(vchiq_core_log_level,
1838 "Close Service %c%c%c%c s:%u d:%d",
1839 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1841 service->remoteport);
1843 case VCHIQ_MSG_DATA:
1844 vchiq_log_info(vchiq_core_log_level,
1845 "%d: prs DATA@%pK,%x (%d->%d)",
1846 state->id, header, size, remoteport, localport);
1848 if ((service->remoteport == remoteport)
1849 && (service->srvstate ==
1850 VCHIQ_SRVSTATE_OPEN)) {
1851 header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
1852 claim_slot(state->rx_info);
1853 DEBUG_TRACE(PARSE_LINE);
1854 if (make_service_callback(service,
1855 VCHIQ_MESSAGE_AVAILABLE, header,
1856 NULL) == VCHIQ_RETRY) {
1857 DEBUG_TRACE(PARSE_LINE);
1858 goto bail_not_ready;
1860 VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
1861 VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
1864 VCHIQ_STATS_INC(state, error_count);
1867 case VCHIQ_MSG_CONNECT:
1868 vchiq_log_info(vchiq_core_log_level,
1869 "%d: prs CONNECT@%pK", state->id, header);
1870 state->version_common = ((VCHIQ_SLOT_ZERO_T *)
1871 state->slot_data)->version;
1872 up(&state->connect);
1874 case VCHIQ_MSG_BULK_RX:
1875 case VCHIQ_MSG_BULK_TX: {
1876 VCHIQ_BULK_QUEUE_T *queue;
1878 WARN_ON(!state->is_master);
1879 queue = (type == VCHIQ_MSG_BULK_RX) ?
1880 &service->bulk_tx : &service->bulk_rx;
1881 if ((service->remoteport == remoteport)
1882 && (service->srvstate ==
1883 VCHIQ_SRVSTATE_OPEN)) {
1887 DEBUG_TRACE(PARSE_LINE);
1888 if (mutex_lock_killable(
1889 &service->bulk_mutex) != 0) {
1890 DEBUG_TRACE(PARSE_LINE);
1891 goto bail_not_ready;
1894 WARN_ON(!(queue->remote_insert < queue->remove +
1895 VCHIQ_NUM_SERVICE_BULKS));
1896 bulk = &queue->bulks[
1897 BULK_INDEX(queue->remote_insert)];
1899 (void *)(long)((int *)header->data)[0];
1900 bulk->remote_size = ((int *)header->data)[1];
1903 vchiq_log_info(vchiq_core_log_level,
1904 "%d: prs %s@%pK (%d->%d) %x@%pK",
1905 state->id, msg_type_str(type),
1906 header, remoteport, localport,
1907 bulk->remote_size, bulk->remote_data);
1909 queue->remote_insert++;
1911 if (atomic_read(&pause_bulks_count)) {
1912 state->deferred_bulks++;
1913 vchiq_log_info(vchiq_core_log_level,
1914 "%s: deferring bulk (%d)",
1916 state->deferred_bulks);
1917 if (state->conn_state !=
1918 VCHIQ_CONNSTATE_PAUSE_SENT)
1920 vchiq_core_log_level,
1921 "%s: bulks paused in "
1922 "unexpected state %s",
1925 state->conn_state]);
1926 } else if (state->conn_state ==
1927 VCHIQ_CONNSTATE_CONNECTED) {
1928 DEBUG_TRACE(PARSE_LINE);
1929 resolved = resolve_bulks(service,
1933 mutex_unlock(&service->bulk_mutex);
1935 notify_bulks(service, queue,
1939 case VCHIQ_MSG_BULK_RX_DONE:
1940 case VCHIQ_MSG_BULK_TX_DONE:
1941 WARN_ON(state->is_master);
1942 if ((service->remoteport == remoteport)
1943 && (service->srvstate !=
1944 VCHIQ_SRVSTATE_FREE)) {
1945 VCHIQ_BULK_QUEUE_T *queue;
1948 queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
1949 &service->bulk_rx : &service->bulk_tx;
1951 DEBUG_TRACE(PARSE_LINE);
1952 if (mutex_lock_killable(
1953 &service->bulk_mutex) != 0) {
1954 DEBUG_TRACE(PARSE_LINE);
1955 goto bail_not_ready;
1957 if ((int)(queue->remote_insert -
1958 queue->local_insert) >= 0) {
1959 vchiq_log_error(vchiq_core_log_level,
1960 "%d: prs %s@%pK (%d->%d) "
1961 "unexpected (ri=%d,li=%d)",
1962 state->id, msg_type_str(type),
1963 header, remoteport, localport,
1964 queue->remote_insert,
1965 queue->local_insert);
1966 mutex_unlock(&service->bulk_mutex);
1969 if (queue->process != queue->remote_insert) {
1970 pr_err("%s: p %x != ri %x\n",
1973 queue->remote_insert);
1974 mutex_unlock(&service->bulk_mutex);
1975 goto bail_not_ready;
1978 bulk = &queue->bulks[
1979 BULK_INDEX(queue->remote_insert)];
1980 bulk->actual = *(int *)header->data;
1981 queue->remote_insert++;
1983 vchiq_log_info(vchiq_core_log_level,
1984 "%d: prs %s@%pK (%d->%d) %x@%pK",
1985 state->id, msg_type_str(type),
1986 header, remoteport, localport,
1987 bulk->actual, bulk->data);
1989 vchiq_log_trace(vchiq_core_log_level,
1990 "%d: prs:%d %cx li=%x ri=%x p=%x",
1991 state->id, localport,
1992 (type == VCHIQ_MSG_BULK_RX_DONE) ?
1994 queue->local_insert,
1995 queue->remote_insert, queue->process);
1997 DEBUG_TRACE(PARSE_LINE);
1998 WARN_ON(queue->process == queue->local_insert);
1999 vchiq_complete_bulk(bulk);
2001 mutex_unlock(&service->bulk_mutex);
2002 DEBUG_TRACE(PARSE_LINE);
2003 notify_bulks(service, queue, 1/*retry_poll*/);
2004 DEBUG_TRACE(PARSE_LINE);
2007 case VCHIQ_MSG_PADDING:
2008 vchiq_log_trace(vchiq_core_log_level,
2009 "%d: prs PADDING@%pK,%x",
2010 state->id, header, size);
2012 case VCHIQ_MSG_PAUSE:
2013 /* If initiated, signal the application thread */
2014 vchiq_log_trace(vchiq_core_log_level,
2015 "%d: prs PAUSE@%pK,%x",
2016 state->id, header, size);
2017 if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
2018 vchiq_log_error(vchiq_core_log_level,
2019 "%d: PAUSE received in state PAUSED",
2023 if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
2024 /* Send a PAUSE in response */
2025 if (queue_message(state, NULL,
2026 VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
2027 NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK)
2029 goto bail_not_ready;
2030 if (state->is_master)
2033 /* At this point slot_mutex is held */
2034 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
2035 vchiq_platform_paused(state);
2037 case VCHIQ_MSG_RESUME:
2038 vchiq_log_trace(vchiq_core_log_level,
2039 "%d: prs RESUME@%pK,%x",
2040 state->id, header, size);
2041 /* Release the slot mutex */
2042 mutex_unlock(&state->slot_mutex);
2043 if (state->is_master)
2044 resume_bulks(state);
2045 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
2046 vchiq_platform_resumed(state);
2049 case VCHIQ_MSG_REMOTE_USE:
2050 vchiq_on_remote_use(state);
2052 case VCHIQ_MSG_REMOTE_RELEASE:
2053 vchiq_on_remote_release(state);
2055 case VCHIQ_MSG_REMOTE_USE_ACTIVE:
2056 vchiq_on_remote_use_active(state);
2060 vchiq_log_error(vchiq_core_log_level,
2061 "%d: prs invalid msgid %x@%pK,%x",
2062 state->id, msgid, header, size);
2063 WARN(1, "invalid message\n");
2069 unlock_service(service);
2073 state->rx_pos += calc_stride(size);
2075 DEBUG_TRACE(PARSE_LINE);
2076 /* Perform some housekeeping when the end of the slot is
2078 if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
2079 /* Remove the extra reference count. */
2080 release_slot(state, state->rx_info, NULL, NULL);
2081 state->rx_data = NULL;
2087 unlock_service(service);
2090 /* Called by the slot handler thread */
2092 slot_handler_func(void *v)
2094 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
2095 VCHIQ_SHARED_STATE_T *local = state->local;
2097 DEBUG_INITIALISE(local)
2100 DEBUG_COUNT(SLOT_HANDLER_COUNT);
2101 DEBUG_TRACE(SLOT_HANDLER_LINE);
2102 remote_event_wait(state, &local->trigger);
2106 DEBUG_TRACE(SLOT_HANDLER_LINE);
2107 if (state->poll_needed) {
2108 /* Check if we need to suspend - may change our
2110 vchiq_platform_check_suspend(state);
2112 state->poll_needed = 0;
2114 /* Handle service polling and other rare conditions here
2115 ** out of the mainline code */
2116 switch (state->conn_state) {
2117 case VCHIQ_CONNSTATE_CONNECTED:
2118 /* Poll the services as requested */
2119 poll_services(state);
2122 case VCHIQ_CONNSTATE_PAUSING:
2123 if (state->is_master)
2125 if (queue_message(state, NULL,
2126 VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
2128 QMFLAGS_NO_MUTEX_UNLOCK)
2130 vchiq_set_conn_state(state,
2131 VCHIQ_CONNSTATE_PAUSE_SENT);
2133 if (state->is_master)
2134 resume_bulks(state);
2136 state->poll_needed = 1;
2140 case VCHIQ_CONNSTATE_PAUSED:
2141 vchiq_platform_resume(state);
2144 case VCHIQ_CONNSTATE_RESUMING:
2145 if (queue_message(state, NULL,
2146 VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
2147 NULL, NULL, 0, QMFLAGS_NO_MUTEX_LOCK)
2149 if (state->is_master)
2150 resume_bulks(state);
2151 vchiq_set_conn_state(state,
2152 VCHIQ_CONNSTATE_CONNECTED);
2153 vchiq_platform_resumed(state);
2155 /* This should really be impossible,
2156 ** since the PAUSE should have flushed
2157 ** through outstanding messages. */
2158 vchiq_log_error(vchiq_core_log_level,
2159 "Failed to send RESUME "
2164 case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
2165 case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
2166 vchiq_platform_handle_timeout(state);
2175 DEBUG_TRACE(SLOT_HANDLER_LINE);
2176 parse_rx_slots(state);
2182 /* Called by the recycle thread */
2184 recycle_func(void *v)
2186 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
2187 VCHIQ_SHARED_STATE_T *local = state->local;
2190 remote_event_wait(state, &local->recycle);
2192 process_free_queue(state);
2198 /* Called by the sync thread */
2202 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
2203 VCHIQ_SHARED_STATE_T *local = state->local;
2204 VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
2205 state->remote->slot_sync);
2208 VCHIQ_SERVICE_T *service;
2211 unsigned int localport, remoteport;
2213 remote_event_wait(state, &local->sync_trigger);
2217 msgid = header->msgid;
2218 size = header->size;
2219 type = VCHIQ_MSG_TYPE(msgid);
2220 localport = VCHIQ_MSG_DSTPORT(msgid);
2221 remoteport = VCHIQ_MSG_SRCPORT(msgid);
2223 service = find_service_by_port(state, localport);
2226 vchiq_log_error(vchiq_sync_log_level,
2227 "%d: sf %s@%pK (%d->%d) - invalid/closed service %d",
2228 state->id, msg_type_str(type),
2229 header, remoteport, localport, localport);
2230 release_message_sync(state, header);
2234 if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
2237 svc_fourcc = service
2238 ? service->base.fourcc
2239 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
2240 vchiq_log_trace(vchiq_sync_log_level,
2241 "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
2243 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
2244 remoteport, localport, size);
2246 vchiq_log_dump_mem("Rcvd", 0, header->data,
2251 case VCHIQ_MSG_OPENACK:
2252 if (size >= sizeof(struct vchiq_openack_payload)) {
2253 const struct vchiq_openack_payload *payload =
2254 (struct vchiq_openack_payload *)
2256 service->peer_version = payload->version;
2258 vchiq_log_info(vchiq_sync_log_level,
2259 "%d: sf OPENACK@%pK,%x (%d->%d) v:%d",
2260 state->id, header, size, remoteport, localport,
2261 service->peer_version);
2262 if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
2263 service->remoteport = remoteport;
2264 vchiq_set_service_state(service,
2265 VCHIQ_SRVSTATE_OPENSYNC);
2267 up(&service->remove_event);
2269 release_message_sync(state, header);
2272 case VCHIQ_MSG_DATA:
2273 vchiq_log_trace(vchiq_sync_log_level,
2274 "%d: sf DATA@%pK,%x (%d->%d)",
2275 state->id, header, size, remoteport, localport);
2277 if ((service->remoteport == remoteport) &&
2278 (service->srvstate ==
2279 VCHIQ_SRVSTATE_OPENSYNC)) {
2280 if (make_service_callback(service,
2281 VCHIQ_MESSAGE_AVAILABLE, header,
2282 NULL) == VCHIQ_RETRY)
2283 vchiq_log_error(vchiq_sync_log_level,
2284 "synchronous callback to "
2285 "service %d returns "
2292 vchiq_log_error(vchiq_sync_log_level,
2293 "%d: sf unexpected msgid %x@%pK,%x",
2294 state->id, msgid, header, size);
2295 release_message_sync(state, header);
2299 unlock_service(service);
2307 init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
2309 queue->local_insert = 0;
2310 queue->remote_insert = 0;
2312 queue->remote_notify = 0;
2318 get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
2320 return conn_state_names[conn_state];
2325 vchiq_init_slots(void *mem_base, int mem_size)
2328 (int)((VCHIQ_SLOT_SIZE - (long)mem_base) & VCHIQ_SLOT_MASK);
2329 VCHIQ_SLOT_ZERO_T *slot_zero =
2330 (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
2331 int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
2332 int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
2334 /* Ensure there is enough memory to run an absolutely minimum system */
2335 num_slots -= first_data_slot;
2337 if (num_slots < 4) {
2338 vchiq_log_error(vchiq_core_log_level,
2339 "vchiq_init_slots - insufficient memory %x bytes",
2344 memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
2346 slot_zero->magic = VCHIQ_MAGIC;
2347 slot_zero->version = VCHIQ_VERSION;
2348 slot_zero->version_min = VCHIQ_VERSION_MIN;
2349 slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
2350 slot_zero->slot_size = VCHIQ_SLOT_SIZE;
2351 slot_zero->max_slots = VCHIQ_MAX_SLOTS;
2352 slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
2354 slot_zero->master.slot_sync = first_data_slot;
2355 slot_zero->master.slot_first = first_data_slot + 1;
2356 slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
2357 slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
2358 slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
2359 slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
2365 vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
2368 VCHIQ_SHARED_STATE_T *local;
2369 VCHIQ_SHARED_STATE_T *remote;
2370 VCHIQ_STATUS_T status;
2371 char threadname[16];
2374 vchiq_log_warning(vchiq_core_log_level,
2375 "%s: slot_zero = %pK, is_master = %d",
2376 __func__, slot_zero, is_master);
2378 if (vchiq_states[0]) {
2379 pr_err("%s: VCHIQ state already initialized\n", __func__);
2383 /* Check the input configuration */
2385 if (slot_zero->magic != VCHIQ_MAGIC) {
2386 vchiq_loud_error_header();
2387 vchiq_loud_error("Invalid VCHIQ magic value found.");
2388 vchiq_loud_error("slot_zero=%pK: magic=%x (expected %x)",
2389 slot_zero, slot_zero->magic, VCHIQ_MAGIC);
2390 vchiq_loud_error_footer();
2394 if (slot_zero->version < VCHIQ_VERSION_MIN) {
2395 vchiq_loud_error_header();
2396 vchiq_loud_error("Incompatible VCHIQ versions found.");
2397 vchiq_loud_error("slot_zero=%pK: VideoCore version=%d (minimum %d)",
2398 slot_zero, slot_zero->version, VCHIQ_VERSION_MIN);
2399 vchiq_loud_error("Restart with a newer VideoCore image.");
2400 vchiq_loud_error_footer();
2404 if (VCHIQ_VERSION < slot_zero->version_min) {
2405 vchiq_loud_error_header();
2406 vchiq_loud_error("Incompatible VCHIQ versions found.");
2407 vchiq_loud_error("slot_zero=%pK: version=%d (VideoCore minimum %d)",
2408 slot_zero, VCHIQ_VERSION, slot_zero->version_min);
2409 vchiq_loud_error("Restart with a newer kernel.");
2410 vchiq_loud_error_footer();
2414 if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
2415 (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
2416 (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
2417 (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
2418 vchiq_loud_error_header();
2419 if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
2420 vchiq_loud_error("slot_zero=%pK: slot_zero_size=%d (expected %d)",
2421 slot_zero, slot_zero->slot_zero_size,
2422 (int)sizeof(VCHIQ_SLOT_ZERO_T));
2423 if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
2424 vchiq_loud_error("slot_zero=%pK: slot_size=%d (expected %d)",
2425 slot_zero, slot_zero->slot_size,
2427 if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
2428 vchiq_loud_error("slot_zero=%pK: max_slots=%d (expected %d)",
2429 slot_zero, slot_zero->max_slots,
2431 if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
2432 vchiq_loud_error("slot_zero=%pK: max_slots_per_side=%d (expected %d)",
2433 slot_zero, slot_zero->max_slots_per_side,
2434 VCHIQ_MAX_SLOTS_PER_SIDE);
2435 vchiq_loud_error_footer();
2439 if (VCHIQ_VERSION < slot_zero->version)
2440 slot_zero->version = VCHIQ_VERSION;
2443 local = &slot_zero->master;
2444 remote = &slot_zero->slave;
2446 local = &slot_zero->slave;
2447 remote = &slot_zero->master;
2450 if (local->initialised) {
2451 vchiq_loud_error_header();
2452 if (remote->initialised)
2453 vchiq_loud_error("local state has already been "
2456 vchiq_loud_error("master/slave mismatch - two %ss",
2457 is_master ? "master" : "slave");
2458 vchiq_loud_error_footer();
2462 memset(state, 0, sizeof(VCHIQ_STATE_T));
2464 state->is_master = is_master;
2467 initialize shared state pointers
2470 state->local = local;
2471 state->remote = remote;
2472 state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
2475 initialize events and mutexes
2478 sema_init(&state->connect, 0);
2479 mutex_init(&state->mutex);
2480 sema_init(&state->trigger_event, 0);
2481 sema_init(&state->recycle_event, 0);
2482 sema_init(&state->sync_trigger_event, 0);
2483 sema_init(&state->sync_release_event, 0);
2485 mutex_init(&state->slot_mutex);
2486 mutex_init(&state->recycle_mutex);
2487 mutex_init(&state->sync_mutex);
2488 mutex_init(&state->bulk_transfer_mutex);
2490 sema_init(&state->slot_available_event, 0);
2491 sema_init(&state->slot_remove_event, 0);
2492 sema_init(&state->data_quota_event, 0);
2494 state->slot_queue_available = 0;
2496 for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
2497 VCHIQ_SERVICE_QUOTA_T *service_quota =
2498 &state->service_quotas[i];
2499 sema_init(&service_quota->quota_event, 0);
2502 for (i = local->slot_first; i <= local->slot_last; i++) {
2503 local->slot_queue[state->slot_queue_available++] = i;
2504 up(&state->slot_available_event);
2507 state->default_slot_quota = state->slot_queue_available/2;
2508 state->default_message_quota =
2509 min((unsigned short)(state->default_slot_quota * 256),
2510 (unsigned short)~0);
2512 state->previous_data_index = -1;
2513 state->data_use_count = 0;
2514 state->data_quota = state->slot_queue_available - 1;
2516 local->trigger.event = offsetof(VCHIQ_STATE_T, trigger_event);
2517 remote_event_create(state, &local->trigger);
2520 local->recycle.event = offsetof(VCHIQ_STATE_T, recycle_event);
2521 remote_event_create(state, &local->recycle);
2522 local->slot_queue_recycle = state->slot_queue_available;
2524 local->sync_trigger.event = offsetof(VCHIQ_STATE_T, sync_trigger_event);
2525 remote_event_create(state, &local->sync_trigger);
2527 local->sync_release.event = offsetof(VCHIQ_STATE_T, sync_release_event);
2528 remote_event_create(state, &local->sync_release);
2530 /* At start-of-day, the slot is empty and available */
2531 ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
2532 = VCHIQ_MSGID_PADDING;
2533 remote_event_signal_local(state, &local->sync_release);
2535 local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
2537 status = vchiq_platform_init_state(state);
2540 bring up slot handler thread
2542 snprintf(threadname, sizeof(threadname), "vchiq-slot/%d", state->id);
2543 state->slot_handler_thread = kthread_create(&slot_handler_func,
2547 if (IS_ERR(state->slot_handler_thread)) {
2548 vchiq_loud_error_header();
2549 vchiq_loud_error("couldn't create thread %s", threadname);
2550 vchiq_loud_error_footer();
2553 set_user_nice(state->slot_handler_thread, -19);
2554 wake_up_process(state->slot_handler_thread);
2556 snprintf(threadname, sizeof(threadname), "vchiq-recy/%d", state->id);
2557 state->recycle_thread = kthread_create(&recycle_func,
2560 if (IS_ERR(state->recycle_thread)) {
2561 vchiq_loud_error_header();
2562 vchiq_loud_error("couldn't create thread %s", threadname);
2563 vchiq_loud_error_footer();
2566 set_user_nice(state->recycle_thread, -19);
2567 wake_up_process(state->recycle_thread);
2569 snprintf(threadname, sizeof(threadname), "vchiq-sync/%d", state->id);
2570 state->sync_thread = kthread_create(&sync_func,
2573 if (IS_ERR(state->sync_thread)) {
2574 vchiq_loud_error_header();
2575 vchiq_loud_error("couldn't create thread %s", threadname);
2576 vchiq_loud_error_footer();
2579 set_user_nice(state->sync_thread, -20);
2580 wake_up_process(state->sync_thread);
2582 vchiq_states[0] = state;
2584 /* Indicate readiness to the other side */
2585 local->initialised = 1;
2590 /* Called from application thread when a client or server service is created. */
2592 vchiq_add_service_internal(VCHIQ_STATE_T *state,
2593 const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
2594 VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
2596 VCHIQ_SERVICE_T *service;
2597 VCHIQ_SERVICE_T **pservice = NULL;
2598 VCHIQ_SERVICE_QUOTA_T *service_quota;
2601 service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
2605 service->base.fourcc = params->fourcc;
2606 service->base.callback = params->callback;
2607 service->base.userdata = params->userdata;
2608 service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
2609 service->ref_count = 1;
2610 service->srvstate = VCHIQ_SRVSTATE_FREE;
2611 service->userdata_term = userdata_term;
2612 service->localport = VCHIQ_PORT_FREE;
2613 service->remoteport = VCHIQ_PORT_FREE;
2615 service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
2616 VCHIQ_FOURCC_INVALID : params->fourcc;
2617 service->client_id = 0;
2618 service->auto_close = 1;
2620 service->closing = 0;
2622 atomic_set(&service->poll_flags, 0);
2623 service->version = params->version;
2624 service->version_min = params->version_min;
2625 service->state = state;
2626 service->instance = instance;
2627 service->service_use_count = 0;
2628 init_bulk_queue(&service->bulk_tx);
2629 init_bulk_queue(&service->bulk_rx);
2630 sema_init(&service->remove_event, 0);
2631 sema_init(&service->bulk_remove_event, 0);
2632 mutex_init(&service->bulk_mutex);
2633 memset(&service->stats, 0, sizeof(service->stats));
2635 /* Although it is perfectly possible to use service_spinlock
2636 ** to protect the creation of services, it is overkill as it
2637 ** disables interrupts while the array is searched.
2638 ** The only danger is of another thread trying to create a
2639 ** service - service deletion is safe.
2640 ** Therefore it is preferable to use state->mutex which,
2641 ** although slower to claim, doesn't block interrupts while
2645 mutex_lock(&state->mutex);
2647 /* Prepare to use a previously unused service */
2648 if (state->unused_service < VCHIQ_MAX_SERVICES)
2649 pservice = &state->services[state->unused_service];
2651 if (srvstate == VCHIQ_SRVSTATE_OPENING) {
2652 for (i = 0; i < state->unused_service; i++) {
2653 VCHIQ_SERVICE_T *srv = state->services[i];
2656 pservice = &state->services[i];
2661 for (i = (state->unused_service - 1); i >= 0; i--) {
2662 VCHIQ_SERVICE_T *srv = state->services[i];
2665 pservice = &state->services[i];
2666 else if ((srv->public_fourcc == params->fourcc)
2667 && ((srv->instance != instance) ||
2668 (srv->base.callback !=
2669 params->callback))) {
2670 /* There is another server using this
2671 ** fourcc which doesn't match. */
2679 service->localport = (pservice - state->services);
2681 handle_seq = VCHIQ_MAX_STATES *
2683 service->handle = handle_seq |
2684 (state->id * VCHIQ_MAX_SERVICES) |
2686 handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
2687 *pservice = service;
2688 if (pservice == &state->services[state->unused_service])
2689 state->unused_service++;
2692 mutex_unlock(&state->mutex);
2699 service_quota = &state->service_quotas[service->localport];
2700 service_quota->slot_quota = state->default_slot_quota;
2701 service_quota->message_quota = state->default_message_quota;
2702 if (service_quota->slot_use_count == 0)
2703 service_quota->previous_tx_index =
2704 SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
2707 /* Bring this service online */
2708 vchiq_set_service_state(service, srvstate);
2710 vchiq_log_info(vchiq_core_msg_log_level,
2711 "%s Service %c%c%c%c SrcPort:%d",
2712 (srvstate == VCHIQ_SRVSTATE_OPENING)
2714 VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
2715 service->localport);
2717 /* Don't unlock the service - leave it with a ref_count of 1. */
2723 vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
2725 struct vchiq_open_payload payload = {
2726 service->base.fourcc,
2729 service->version_min
2731 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2733 service->client_id = client_id;
2734 vchiq_use_service_internal(service);
2735 status = queue_message(service->state,
2737 VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN,
2740 memcpy_copy_callback,
2743 QMFLAGS_IS_BLOCKING);
2744 if (status == VCHIQ_SUCCESS) {
2745 /* Wait for the ACK/NAK */
2746 if (down_interruptible(&service->remove_event) != 0) {
2747 status = VCHIQ_RETRY;
2748 vchiq_release_service_internal(service);
2749 } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
2750 (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
2751 if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
2752 vchiq_log_error(vchiq_core_log_level,
2753 "%d: osi - srvstate = %s (ref %d)",
2755 srvstate_names[service->srvstate],
2756 service->ref_count);
2757 status = VCHIQ_ERROR;
2758 VCHIQ_SERVICE_STATS_INC(service, error_count);
2759 vchiq_release_service_internal(service);
2766 release_service_messages(VCHIQ_SERVICE_T *service)
2768 VCHIQ_STATE_T *state = service->state;
2769 int slot_last = state->remote->slot_last;
2772 /* Release any claimed messages aimed at this service */
2774 if (service->sync) {
2775 VCHIQ_HEADER_T *header =
2776 (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
2777 state->remote->slot_sync);
2778 if (VCHIQ_MSG_DSTPORT(header->msgid) == service->localport)
2779 release_message_sync(state, header);
2784 for (i = state->remote->slot_first; i <= slot_last; i++) {
2785 VCHIQ_SLOT_INFO_T *slot_info =
2786 SLOT_INFO_FROM_INDEX(state, i);
2787 if (slot_info->release_count != slot_info->use_count) {
2789 (char *)SLOT_DATA_FROM_INDEX(state, i);
2790 unsigned int pos, end;
2792 end = VCHIQ_SLOT_SIZE;
2793 if (data == state->rx_data)
2794 /* This buffer is still being read from - stop
2795 ** at the current read position */
2796 end = state->rx_pos & VCHIQ_SLOT_MASK;
2801 VCHIQ_HEADER_T *header =
2802 (VCHIQ_HEADER_T *)(data + pos);
2803 int msgid = header->msgid;
2804 int port = VCHIQ_MSG_DSTPORT(msgid);
2806 if ((port == service->localport) &&
2807 (msgid & VCHIQ_MSGID_CLAIMED)) {
2808 vchiq_log_info(vchiq_core_log_level,
2809 " fsi - hdr %pK", header);
2810 release_slot(state, slot_info, header,
2813 pos += calc_stride(header->size);
2814 if (pos > VCHIQ_SLOT_SIZE) {
2815 vchiq_log_error(vchiq_core_log_level,
2816 "fsi - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
2818 header->msgid, header->size);
2819 WARN(1, "invalid slot position\n");
2827 do_abort_bulks(VCHIQ_SERVICE_T *service)
2829 VCHIQ_STATUS_T status;
2831 /* Abort any outstanding bulk transfers */
2832 if (mutex_lock_killable(&service->bulk_mutex) != 0)
2834 abort_outstanding_bulks(service, &service->bulk_tx);
2835 abort_outstanding_bulks(service, &service->bulk_rx);
2836 mutex_unlock(&service->bulk_mutex);
2838 status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
2839 if (status == VCHIQ_SUCCESS)
2840 status = notify_bulks(service, &service->bulk_rx,
2842 return (status == VCHIQ_SUCCESS);
2845 static VCHIQ_STATUS_T
2846 close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
2848 VCHIQ_STATUS_T status;
2849 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2852 switch (service->srvstate) {
2853 case VCHIQ_SRVSTATE_OPEN:
2854 case VCHIQ_SRVSTATE_CLOSESENT:
2855 case VCHIQ_SRVSTATE_CLOSERECVD:
2857 if (service->auto_close) {
2858 service->client_id = 0;
2859 service->remoteport = VCHIQ_PORT_FREE;
2860 newstate = VCHIQ_SRVSTATE_LISTENING;
2862 newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
2864 newstate = VCHIQ_SRVSTATE_CLOSED;
2865 vchiq_set_service_state(service, newstate);
2867 case VCHIQ_SRVSTATE_LISTENING:
2870 vchiq_log_error(vchiq_core_log_level,
2871 "close_service_complete(%x) called in state %s",
2872 service->handle, srvstate_names[service->srvstate]);
2873 WARN(1, "close_service_complete in unexpected state\n");
2877 status = make_service_callback(service,
2878 VCHIQ_SERVICE_CLOSED, NULL, NULL);
2880 if (status != VCHIQ_RETRY) {
2881 int uc = service->service_use_count;
2883 /* Complete the close process */
2884 for (i = 0; i < uc; i++)
2885 /* cater for cases where close is forced and the
2886 ** client may not close all it's handles */
2887 vchiq_release_service_internal(service);
2889 service->client_id = 0;
2890 service->remoteport = VCHIQ_PORT_FREE;
2892 if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
2893 vchiq_free_service_internal(service);
2894 else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
2896 service->closing = 0;
2898 up(&service->remove_event);
2901 vchiq_set_service_state(service, failstate);
2906 /* Called by the slot handler */
2908 vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
2910 VCHIQ_STATE_T *state = service->state;
2911 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2912 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2914 vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
2915 service->state->id, service->localport, close_recvd,
2916 srvstate_names[service->srvstate]);
2918 switch (service->srvstate) {
2919 case VCHIQ_SRVSTATE_CLOSED:
2920 case VCHIQ_SRVSTATE_HIDDEN:
2921 case VCHIQ_SRVSTATE_LISTENING:
2922 case VCHIQ_SRVSTATE_CLOSEWAIT:
2924 vchiq_log_error(vchiq_core_log_level,
2925 "vchiq_close_service_internal(1) called "
2927 srvstate_names[service->srvstate]);
2928 else if (is_server) {
2929 if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
2930 status = VCHIQ_ERROR;
2932 service->client_id = 0;
2933 service->remoteport = VCHIQ_PORT_FREE;
2934 if (service->srvstate ==
2935 VCHIQ_SRVSTATE_CLOSEWAIT)
2936 vchiq_set_service_state(service,
2937 VCHIQ_SRVSTATE_LISTENING);
2939 up(&service->remove_event);
2941 vchiq_free_service_internal(service);
2943 case VCHIQ_SRVSTATE_OPENING:
2945 /* The open was rejected - tell the user */
2946 vchiq_set_service_state(service,
2947 VCHIQ_SRVSTATE_CLOSEWAIT);
2948 up(&service->remove_event);
2950 /* Shutdown mid-open - let the other side know */
2951 status = queue_message(state, service,
2955 VCHIQ_MSG_DSTPORT(service->remoteport)),
2960 case VCHIQ_SRVSTATE_OPENSYNC:
2961 mutex_lock(&state->sync_mutex);
2964 case VCHIQ_SRVSTATE_OPEN:
2965 if (state->is_master || close_recvd) {
2966 if (!do_abort_bulks(service))
2967 status = VCHIQ_RETRY;
2970 release_service_messages(service);
2972 if (status == VCHIQ_SUCCESS)
2973 status = queue_message(state, service,
2977 VCHIQ_MSG_DSTPORT(service->remoteport)),
2978 NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK);
2980 if (status == VCHIQ_SUCCESS) {
2982 /* Change the state while the mutex is
2984 vchiq_set_service_state(service,
2985 VCHIQ_SRVSTATE_CLOSESENT);
2986 mutex_unlock(&state->slot_mutex);
2988 mutex_unlock(&state->sync_mutex);
2991 } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
2992 mutex_unlock(&state->sync_mutex);
2997 /* Change the state while the mutex is still held */
2998 vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSERECVD);
2999 mutex_unlock(&state->slot_mutex);
3001 mutex_unlock(&state->sync_mutex);
3003 status = close_service_complete(service,
3004 VCHIQ_SRVSTATE_CLOSERECVD);
3007 case VCHIQ_SRVSTATE_CLOSESENT:
3009 /* This happens when a process is killed mid-close */
3012 if (!state->is_master) {
3013 if (!do_abort_bulks(service)) {
3014 status = VCHIQ_RETRY;
3019 if (status == VCHIQ_SUCCESS)
3020 status = close_service_complete(service,
3021 VCHIQ_SRVSTATE_CLOSERECVD);
3024 case VCHIQ_SRVSTATE_CLOSERECVD:
3025 if (!close_recvd && is_server)
3026 /* Force into LISTENING mode */
3027 vchiq_set_service_state(service,
3028 VCHIQ_SRVSTATE_LISTENING);
3029 status = close_service_complete(service,
3030 VCHIQ_SRVSTATE_CLOSERECVD);
3034 vchiq_log_error(vchiq_core_log_level,
3035 "vchiq_close_service_internal(%d) called in state %s",
3036 close_recvd, srvstate_names[service->srvstate]);
3043 /* Called from the application process upon process death */
3045 vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
3047 VCHIQ_STATE_T *state = service->state;
3049 vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
3050 state->id, service->localport, service->remoteport);
3052 mark_service_closing(service);
3054 /* Mark the service for removal by the slot handler */
3055 request_poll(state, service, VCHIQ_POLL_REMOVE);
3058 /* Called from the slot handler */
3060 vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
3062 VCHIQ_STATE_T *state = service->state;
3064 vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
3065 state->id, service->localport);
3067 switch (service->srvstate) {
3068 case VCHIQ_SRVSTATE_OPENING:
3069 case VCHIQ_SRVSTATE_CLOSED:
3070 case VCHIQ_SRVSTATE_HIDDEN:
3071 case VCHIQ_SRVSTATE_LISTENING:
3072 case VCHIQ_SRVSTATE_CLOSEWAIT:
3075 vchiq_log_error(vchiq_core_log_level,
3076 "%d: fsi - (%d) in state %s",
3077 state->id, service->localport,
3078 srvstate_names[service->srvstate]);
3082 vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
3084 up(&service->remove_event);
3086 /* Release the initial lock */
3087 unlock_service(service);
3091 vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
3093 VCHIQ_SERVICE_T *service;
3096 /* Find all services registered to this client and enable them. */
3098 while ((service = next_service_by_instance(state, instance,
3100 if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
3101 vchiq_set_service_state(service,
3102 VCHIQ_SRVSTATE_LISTENING);
3103 unlock_service(service);
3106 if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
3107 if (queue_message(state, NULL,
3108 VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, NULL,
3109 0, QMFLAGS_IS_BLOCKING) == VCHIQ_RETRY)
3112 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
3115 if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
3116 if (down_interruptible(&state->connect) != 0)
3119 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
3120 up(&state->connect);
3123 return VCHIQ_SUCCESS;
3127 vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
3129 VCHIQ_SERVICE_T *service;
3132 /* Find all services registered to this client and enable them. */
3134 while ((service = next_service_by_instance(state, instance,
3136 (void)vchiq_remove_service(service->handle);
3137 unlock_service(service);
3140 return VCHIQ_SUCCESS;
3144 vchiq_pause_internal(VCHIQ_STATE_T *state)
3146 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3148 switch (state->conn_state) {
3149 case VCHIQ_CONNSTATE_CONNECTED:
3150 /* Request a pause */
3151 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
3152 request_poll(state, NULL, 0);
3155 vchiq_log_error(vchiq_core_log_level,
3156 "vchiq_pause_internal in state %s\n",
3157 conn_state_names[state->conn_state]);
3158 status = VCHIQ_ERROR;
3159 VCHIQ_STATS_INC(state, error_count);
3167 vchiq_resume_internal(VCHIQ_STATE_T *state)
3169 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3171 if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
3172 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
3173 request_poll(state, NULL, 0);
3175 status = VCHIQ_ERROR;
3176 VCHIQ_STATS_INC(state, error_count);
3183 vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
3185 /* Unregister the service */
3186 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3187 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3192 vchiq_log_info(vchiq_core_log_level,
3193 "%d: close_service:%d",
3194 service->state->id, service->localport);
3196 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3197 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
3198 (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
3199 unlock_service(service);
3203 mark_service_closing(service);
3205 if (current == service->state->slot_handler_thread) {
3206 status = vchiq_close_service_internal(service,
3208 WARN_ON(status == VCHIQ_RETRY);
3210 /* Mark the service for termination by the slot handler */
3211 request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
3215 if (down_interruptible(&service->remove_event) != 0) {
3216 status = VCHIQ_RETRY;
3220 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3221 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
3222 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
3225 vchiq_log_warning(vchiq_core_log_level,
3226 "%d: close_service:%d - waiting in state %s",
3227 service->state->id, service->localport,
3228 srvstate_names[service->srvstate]);
3231 if ((status == VCHIQ_SUCCESS) &&
3232 (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
3233 (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
3234 status = VCHIQ_ERROR;
3236 unlock_service(service);
3242 vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
3244 /* Unregister the service */
3245 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3246 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3251 vchiq_log_info(vchiq_core_log_level,
3252 "%d: remove_service:%d",
3253 service->state->id, service->localport);
3255 if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
3256 unlock_service(service);
3260 mark_service_closing(service);
3262 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3263 (current == service->state->slot_handler_thread)) {
3264 /* Make it look like a client, because it must be removed and
3265 not left in the LISTENING state. */
3266 service->public_fourcc = VCHIQ_FOURCC_INVALID;
3268 status = vchiq_close_service_internal(service,
3270 WARN_ON(status == VCHIQ_RETRY);
3272 /* Mark the service for removal by the slot handler */
3273 request_poll(service->state, service, VCHIQ_POLL_REMOVE);
3276 if (down_interruptible(&service->remove_event) != 0) {
3277 status = VCHIQ_RETRY;
3281 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3282 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
3285 vchiq_log_warning(vchiq_core_log_level,
3286 "%d: remove_service:%d - waiting in state %s",
3287 service->state->id, service->localport,
3288 srvstate_names[service->srvstate]);
3291 if ((status == VCHIQ_SUCCESS) &&
3292 (service->srvstate != VCHIQ_SRVSTATE_FREE))
3293 status = VCHIQ_ERROR;
3295 unlock_service(service);
3301 /* This function may be called by kernel threads or user threads.
3302 * User threads may receive VCHIQ_RETRY to indicate that a signal has been
3303 * received and the call should be retried after being returned to user
3305 * When called in blocking mode, the userdata field points to a bulk_waiter
3309 vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
3310 VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
3311 VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
3313 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3314 VCHIQ_BULK_QUEUE_T *queue;
3316 VCHIQ_STATE_T *state;
3317 struct bulk_waiter *bulk_waiter = NULL;
3318 const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
3319 const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
3320 VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
3321 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3324 (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
3325 ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
3326 (vchiq_check_service(service) != VCHIQ_SUCCESS))
3330 case VCHIQ_BULK_MODE_NOCALLBACK:
3331 case VCHIQ_BULK_MODE_CALLBACK:
3333 case VCHIQ_BULK_MODE_BLOCKING:
3334 bulk_waiter = (struct bulk_waiter *)userdata;
3335 sema_init(&bulk_waiter->event, 0);
3336 bulk_waiter->actual = 0;
3337 bulk_waiter->bulk = NULL;
3339 case VCHIQ_BULK_MODE_WAITING:
3340 bulk_waiter = (struct bulk_waiter *)userdata;
3341 bulk = bulk_waiter->bulk;
3347 state = service->state;
3349 queue = (dir == VCHIQ_BULK_TRANSMIT) ?
3350 &service->bulk_tx : &service->bulk_rx;
3352 if (mutex_lock_killable(&service->bulk_mutex) != 0) {
3353 status = VCHIQ_RETRY;
3357 if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
3358 VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
3360 mutex_unlock(&service->bulk_mutex);
3361 if (down_interruptible(&service->bulk_remove_event)
3363 status = VCHIQ_RETRY;
3366 if (mutex_lock_killable(&service->bulk_mutex)
3368 status = VCHIQ_RETRY;
3371 } while (queue->local_insert == queue->remove +
3372 VCHIQ_NUM_SERVICE_BULKS);
3375 bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
3379 bulk->userdata = userdata;
3381 bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
3383 if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
3385 goto unlock_error_exit;
3389 vchiq_log_info(vchiq_core_log_level,
3390 "%d: bt (%d->%d) %cx %x@%pK %pK",
3391 state->id, service->localport, service->remoteport, dir_char,
3392 size, bulk->data, userdata);
3394 /* The slot mutex must be held when the service is being closed, so
3395 claim it here to ensure that isn't happening */
3396 if (mutex_lock_killable(&state->slot_mutex) != 0) {
3397 status = VCHIQ_RETRY;
3398 goto cancel_bulk_error_exit;
3401 if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
3402 goto unlock_both_error_exit;
3404 if (state->is_master) {
3405 queue->local_insert++;
3406 if (resolve_bulks(service, queue))
3407 request_poll(state, service,
3408 (dir == VCHIQ_BULK_TRANSMIT) ?
3409 VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
3411 int payload[2] = { (int)(long)bulk->data, bulk->size };
3413 status = queue_message(state,
3415 VCHIQ_MAKE_MSG(dir_msgtype,
3417 service->remoteport),
3418 memcpy_copy_callback,
3421 QMFLAGS_IS_BLOCKING |
3422 QMFLAGS_NO_MUTEX_LOCK |
3423 QMFLAGS_NO_MUTEX_UNLOCK);
3424 if (status != VCHIQ_SUCCESS) {
3425 goto unlock_both_error_exit;
3427 queue->local_insert++;
3430 mutex_unlock(&state->slot_mutex);
3431 mutex_unlock(&service->bulk_mutex);
3433 vchiq_log_trace(vchiq_core_log_level,
3434 "%d: bt:%d %cx li=%x ri=%x p=%x",
3436 service->localport, dir_char,
3437 queue->local_insert, queue->remote_insert, queue->process);
3440 unlock_service(service);
3442 status = VCHIQ_SUCCESS;
3445 bulk_waiter->bulk = bulk;
3446 if (down_interruptible(&bulk_waiter->event) != 0)
3447 status = VCHIQ_RETRY;
3448 else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
3449 status = VCHIQ_ERROR;
3454 unlock_both_error_exit:
3455 mutex_unlock(&state->slot_mutex);
3456 cancel_bulk_error_exit:
3457 vchiq_complete_bulk(bulk);
3459 mutex_unlock(&service->bulk_mutex);
3463 unlock_service(service);
3468 vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
3469 ssize_t (*copy_callback)(void *context, void *dest,
3470 size_t offset, size_t maxsize),
3474 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3475 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3478 (vchiq_check_service(service) != VCHIQ_SUCCESS))
3482 VCHIQ_SERVICE_STATS_INC(service, error_count);
3487 if (size > VCHIQ_MAX_MSG_SIZE) {
3488 VCHIQ_SERVICE_STATS_INC(service, error_count);
3492 switch (service->srvstate) {
3493 case VCHIQ_SRVSTATE_OPEN:
3494 status = queue_message(service->state, service,
3495 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3497 service->remoteport),
3498 copy_callback, context, size, 1);
3500 case VCHIQ_SRVSTATE_OPENSYNC:
3501 status = queue_message_sync(service->state, service,
3502 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3504 service->remoteport),
3505 copy_callback, context, size, 1);
3508 status = VCHIQ_ERROR;
3514 unlock_service(service);
3520 vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
3522 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3523 VCHIQ_SHARED_STATE_T *remote;
3524 VCHIQ_STATE_T *state;
3530 state = service->state;
3531 remote = state->remote;
3533 slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
3535 if ((slot_index >= remote->slot_first) &&
3536 (slot_index <= remote->slot_last)) {
3537 int msgid = header->msgid;
3539 if (msgid & VCHIQ_MSGID_CLAIMED) {
3540 VCHIQ_SLOT_INFO_T *slot_info =
3541 SLOT_INFO_FROM_INDEX(state, slot_index);
3543 release_slot(state, slot_info, header, service);
3545 } else if (slot_index == remote->slot_sync)
3546 release_message_sync(state, header);
3548 unlock_service(service);
3552 release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
3554 header->msgid = VCHIQ_MSGID_PADDING;
3556 remote_event_signal(&state->remote->sync_release);
3560 vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
3562 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3563 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3566 (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
3569 *peer_version = service->peer_version;
3570 status = VCHIQ_SUCCESS;
3574 unlock_service(service);
3579 vchiq_get_config(VCHIQ_INSTANCE_T instance,
3580 int config_size, VCHIQ_CONFIG_T *pconfig)
3582 VCHIQ_CONFIG_T config;
3586 config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
3587 config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
3588 config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
3589 config.max_services = VCHIQ_MAX_SERVICES;
3590 config.version = VCHIQ_VERSION;
3591 config.version_min = VCHIQ_VERSION_MIN;
3593 if (config_size > sizeof(VCHIQ_CONFIG_T))
3596 memcpy(pconfig, &config,
3597 min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
3599 return VCHIQ_SUCCESS;
3603 vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
3604 VCHIQ_SERVICE_OPTION_T option, int value)
3606 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3607 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3611 case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
3612 service->auto_close = value;
3613 status = VCHIQ_SUCCESS;
3616 case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
3617 VCHIQ_SERVICE_QUOTA_T *service_quota =
3618 &service->state->service_quotas[
3619 service->localport];
3621 value = service->state->default_slot_quota;
3622 if ((value >= service_quota->slot_use_count) &&
3623 (value < (unsigned short)~0)) {
3624 service_quota->slot_quota = value;
3625 if ((value >= service_quota->slot_use_count) &&
3626 (service_quota->message_quota >=
3627 service_quota->message_use_count)) {
3628 /* Signal the service that it may have
3629 ** dropped below its quota */
3630 up(&service_quota->quota_event);
3632 status = VCHIQ_SUCCESS;
3636 case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
3637 VCHIQ_SERVICE_QUOTA_T *service_quota =
3638 &service->state->service_quotas[
3639 service->localport];
3641 value = service->state->default_message_quota;
3642 if ((value >= service_quota->message_use_count) &&
3643 (value < (unsigned short)~0)) {
3644 service_quota->message_quota = value;
3646 service_quota->message_use_count) &&
3647 (service_quota->slot_quota >=
3648 service_quota->slot_use_count))
3649 /* Signal the service that it may have
3650 ** dropped below its quota */
3651 up(&service_quota->quota_event);
3652 status = VCHIQ_SUCCESS;
3656 case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
3657 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3658 (service->srvstate ==
3659 VCHIQ_SRVSTATE_LISTENING)) {
3660 service->sync = value;
3661 status = VCHIQ_SUCCESS;
3665 case VCHIQ_SERVICE_OPTION_TRACE:
3666 service->trace = value;
3667 status = VCHIQ_SUCCESS;
3673 unlock_service(service);
3680 vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
3681 VCHIQ_SHARED_STATE_T *shared, const char *label)
3683 static const char *const debug_names[] = {
3685 "SLOT_HANDLER_COUNT",
3686 "SLOT_HANDLER_LINE",
3690 "AWAIT_COMPLETION_LINE",
3691 "DEQUEUE_MESSAGE_LINE",
3692 "SERVICE_CALLBACK_LINE",
3693 "MSG_QUEUE_FULL_COUNT",
3694 "COMPLETION_QUEUE_FULL_COUNT"
3700 len = snprintf(buf, sizeof(buf),
3701 " %s: slots %d-%d tx_pos=%x recycle=%x",
3702 label, shared->slot_first, shared->slot_last,
3703 shared->tx_pos, shared->slot_queue_recycle);
3704 vchiq_dump(dump_context, buf, len + 1);
3706 len = snprintf(buf, sizeof(buf),
3708 vchiq_dump(dump_context, buf, len + 1);
3710 for (i = shared->slot_first; i <= shared->slot_last; i++) {
3711 VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
3712 if (slot_info.use_count != slot_info.release_count) {
3713 len = snprintf(buf, sizeof(buf),
3714 " %d: %d/%d", i, slot_info.use_count,
3715 slot_info.release_count);
3716 vchiq_dump(dump_context, buf, len + 1);
3720 for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
3721 len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
3722 debug_names[i], shared->debug[i], shared->debug[i]);
3723 vchiq_dump(dump_context, buf, len + 1);
3728 vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
3734 len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
3735 conn_state_names[state->conn_state]);
3736 vchiq_dump(dump_context, buf, len + 1);
3738 len = snprintf(buf, sizeof(buf),
3739 " tx_pos=%x(@%pK), rx_pos=%x(@%pK)",
3740 state->local->tx_pos,
3741 state->tx_data + (state->local_tx_pos & VCHIQ_SLOT_MASK),
3743 state->rx_data + (state->rx_pos & VCHIQ_SLOT_MASK));
3744 vchiq_dump(dump_context, buf, len + 1);
3746 len = snprintf(buf, sizeof(buf),
3747 " Version: %d (min %d)",
3748 VCHIQ_VERSION, VCHIQ_VERSION_MIN);
3749 vchiq_dump(dump_context, buf, len + 1);
3751 if (VCHIQ_ENABLE_STATS) {
3752 len = snprintf(buf, sizeof(buf),
3753 " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
3755 state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
3756 state->stats.error_count);
3757 vchiq_dump(dump_context, buf, len + 1);
3760 len = snprintf(buf, sizeof(buf),
3761 " Slots: %d available (%d data), %d recyclable, %d stalls "
3763 ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
3764 state->local_tx_pos) / VCHIQ_SLOT_SIZE,
3765 state->data_quota - state->data_use_count,
3766 state->local->slot_queue_recycle - state->slot_queue_available,
3767 state->stats.slot_stalls, state->stats.data_stalls);
3768 vchiq_dump(dump_context, buf, len + 1);
3770 vchiq_dump_platform_state(dump_context);
3772 vchiq_dump_shared_state(dump_context, state, state->local, "Local");
3773 vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
3775 vchiq_dump_platform_instances(dump_context);
3777 for (i = 0; i < state->unused_service; i++) {
3778 VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
3781 vchiq_dump_service_state(dump_context, service);
3782 unlock_service(service);
3788 vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
3793 len = snprintf(buf, sizeof(buf), "Service %u: %s (ref %u)",
3794 service->localport, srvstate_names[service->srvstate],
3795 service->ref_count - 1); /*Don't include the lock just taken*/
3797 if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
3798 char remoteport[30];
3799 VCHIQ_SERVICE_QUOTA_T *service_quota =
3800 &service->state->service_quotas[service->localport];
3801 int fourcc = service->base.fourcc;
3802 int tx_pending, rx_pending;
3804 if (service->remoteport != VCHIQ_PORT_FREE) {
3805 int len2 = snprintf(remoteport, sizeof(remoteport),
3806 "%u", service->remoteport);
3808 if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
3809 snprintf(remoteport + len2,
3810 sizeof(remoteport) - len2,
3811 " (client %x)", service->client_id);
3813 strcpy(remoteport, "n/a");
3815 len += snprintf(buf + len, sizeof(buf) - len,
3816 " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
3817 VCHIQ_FOURCC_AS_4CHARS(fourcc),
3819 service_quota->message_use_count,
3820 service_quota->message_quota,
3821 service_quota->slot_use_count,
3822 service_quota->slot_quota);
3824 vchiq_dump(dump_context, buf, len + 1);
3826 tx_pending = service->bulk_tx.local_insert -
3827 service->bulk_tx.remote_insert;
3829 rx_pending = service->bulk_rx.local_insert -
3830 service->bulk_rx.remote_insert;
3832 len = snprintf(buf, sizeof(buf),
3833 " Bulk: tx_pending=%d (size %d),"
3834 " rx_pending=%d (size %d)",
3836 tx_pending ? service->bulk_tx.bulks[
3837 BULK_INDEX(service->bulk_tx.remove)].size : 0,
3839 rx_pending ? service->bulk_rx.bulks[
3840 BULK_INDEX(service->bulk_rx.remove)].size : 0);
3842 if (VCHIQ_ENABLE_STATS) {
3843 vchiq_dump(dump_context, buf, len + 1);
3845 len = snprintf(buf, sizeof(buf),
3846 " Ctrl: tx_count=%d, tx_bytes=%llu, "
3847 "rx_count=%d, rx_bytes=%llu",
3848 service->stats.ctrl_tx_count,
3849 service->stats.ctrl_tx_bytes,
3850 service->stats.ctrl_rx_count,
3851 service->stats.ctrl_rx_bytes);
3852 vchiq_dump(dump_context, buf, len + 1);
3854 len = snprintf(buf, sizeof(buf),
3855 " Bulk: tx_count=%d, tx_bytes=%llu, "
3856 "rx_count=%d, rx_bytes=%llu",
3857 service->stats.bulk_tx_count,
3858 service->stats.bulk_tx_bytes,
3859 service->stats.bulk_rx_count,
3860 service->stats.bulk_rx_bytes);
3861 vchiq_dump(dump_context, buf, len + 1);
3863 len = snprintf(buf, sizeof(buf),
3864 " %d quota stalls, %d slot stalls, "
3865 "%d bulk stalls, %d aborted, %d errors",
3866 service->stats.quota_stalls,
3867 service->stats.slot_stalls,
3868 service->stats.bulk_stalls,
3869 service->stats.bulk_aborted_count,
3870 service->stats.error_count);
3874 vchiq_dump(dump_context, buf, len + 1);
3876 if (service->srvstate != VCHIQ_SRVSTATE_FREE)
3877 vchiq_dump_platform_service_state(dump_context, service);
3882 vchiq_loud_error_header(void)
3884 vchiq_log_error(vchiq_core_log_level,
3885 "============================================================"
3886 "================");
3887 vchiq_log_error(vchiq_core_log_level,
3888 "============================================================"
3889 "================");
3890 vchiq_log_error(vchiq_core_log_level, "=====");
3894 vchiq_loud_error_footer(void)
3896 vchiq_log_error(vchiq_core_log_level, "=====");
3897 vchiq_log_error(vchiq_core_log_level,
3898 "============================================================"
3899 "================");
3900 vchiq_log_error(vchiq_core_log_level,
3901 "============================================================"
3902 "================");
3906 VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
3908 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3910 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3911 status = queue_message(state, NULL,
3912 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
3917 VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
3919 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3921 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3922 status = queue_message(state, NULL,
3923 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
3928 VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
3930 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3932 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3933 status = queue_message(state, NULL,
3934 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
3939 void vchiq_log_dump_mem(const char *label, u32 addr, const void *void_mem,
3942 const u8 *mem = (const u8 *)void_mem;
3947 while (num_bytes > 0) {
3950 for (offset = 0; offset < 16; offset++) {
3951 if (offset < num_bytes)
3952 s += snprintf(s, 4, "%02x ", mem[offset]);
3954 s += snprintf(s, 4, " ");
3957 for (offset = 0; offset < 16; offset++) {
3958 if (offset < num_bytes) {
3959 u8 ch = mem[offset];
3961 if ((ch < ' ') || (ch > '~'))
3968 if ((label != NULL) && (*label != '\0'))
3969 vchiq_log_trace(VCHIQ_LOG_TRACE,
3970 "%s: %08x: %s", label, addr, line_buf);
3972 vchiq_log_trace(VCHIQ_LOG_TRACE,
3973 "%08x: %s", addr, line_buf);