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 {
70 QMFLAGS_IS_BLOCKING = (1 << 0),
71 QMFLAGS_NO_MUTEX_LOCK = (1 << 1),
72 QMFLAGS_NO_MUTEX_UNLOCK = (1 << 2)
75 /* we require this for consistency between endpoints */
76 vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
77 vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
78 vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
79 vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
80 vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
81 vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
83 /* Run time control of log level, based on KERN_XXX level. */
84 int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
85 int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
86 int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
88 static atomic_t pause_bulks_count = ATOMIC_INIT(0);
90 static DEFINE_SPINLOCK(service_spinlock);
91 DEFINE_SPINLOCK(bulk_waiter_spinlock);
92 static DEFINE_SPINLOCK(quota_spinlock);
94 VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
95 static unsigned int handle_seq;
97 static const char *const srvstate_names[] = {
110 static const char *const reason_names[] = {
114 "BULK_TRANSMIT_DONE",
116 "BULK_TRANSMIT_ABORTED",
117 "BULK_RECEIVE_ABORTED"
120 static const char *const conn_state_names[] = {
133 release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
135 static const char *msg_type_str(unsigned int msg_type)
138 case VCHIQ_MSG_PADDING: return "PADDING";
139 case VCHIQ_MSG_CONNECT: return "CONNECT";
140 case VCHIQ_MSG_OPEN: return "OPEN";
141 case VCHIQ_MSG_OPENACK: return "OPENACK";
142 case VCHIQ_MSG_CLOSE: return "CLOSE";
143 case VCHIQ_MSG_DATA: return "DATA";
144 case VCHIQ_MSG_BULK_RX: return "BULK_RX";
145 case VCHIQ_MSG_BULK_TX: return "BULK_TX";
146 case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
147 case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
148 case VCHIQ_MSG_PAUSE: return "PAUSE";
149 case VCHIQ_MSG_RESUME: return "RESUME";
150 case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
151 case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
152 case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
158 vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
160 vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
161 service->state->id, service->localport,
162 srvstate_names[service->srvstate],
163 srvstate_names[newstate]);
164 service->srvstate = newstate;
168 find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
170 VCHIQ_SERVICE_T *service;
172 spin_lock(&service_spinlock);
173 service = handle_to_service(handle);
174 if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
175 (service->handle == handle)) {
176 WARN_ON(service->ref_count == 0);
177 service->ref_count++;
180 spin_unlock(&service_spinlock);
183 vchiq_log_info(vchiq_core_log_level,
184 "Invalid service handle 0x%x", handle);
190 find_service_by_port(VCHIQ_STATE_T *state, int localport)
192 VCHIQ_SERVICE_T *service = NULL;
194 if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
195 spin_lock(&service_spinlock);
196 service = state->services[localport];
197 if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
198 WARN_ON(service->ref_count == 0);
199 service->ref_count++;
202 spin_unlock(&service_spinlock);
206 vchiq_log_info(vchiq_core_log_level,
207 "Invalid port %d", localport);
213 find_service_for_instance(VCHIQ_INSTANCE_T instance,
214 VCHIQ_SERVICE_HANDLE_T handle)
216 VCHIQ_SERVICE_T *service;
218 spin_lock(&service_spinlock);
219 service = handle_to_service(handle);
220 if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
221 (service->handle == handle) &&
222 (service->instance == instance)) {
223 WARN_ON(service->ref_count == 0);
224 service->ref_count++;
227 spin_unlock(&service_spinlock);
230 vchiq_log_info(vchiq_core_log_level,
231 "Invalid service handle 0x%x", handle);
237 find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
238 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, BITSET_T *service_found, size_t length)
625 VCHIQ_SHARED_STATE_T *local = state->local;
626 int slot_queue_available;
628 /* Find slots which have been freed by the other side, and return them
629 ** to the available queue. */
630 slot_queue_available = state->slot_queue_available;
633 * Use a memory barrier to ensure that any state that may have been
634 * modified by another thread is not masked by stale prefetched
639 while (slot_queue_available != local->slot_queue_recycle) {
641 int slot_index = local->slot_queue[slot_queue_available++ &
642 VCHIQ_SLOT_QUEUE_MASK];
643 char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
647 * Beware of the address dependency - data is calculated
648 * using an index written by the other side.
652 vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%pK %x %x",
653 state->id, slot_index, data,
654 local->slot_queue_recycle, slot_queue_available);
656 /* Initialise the bitmask for services which have used this
658 memset(service_found, 0, length);
662 while (pos < VCHIQ_SLOT_SIZE) {
663 VCHIQ_HEADER_T *header =
664 (VCHIQ_HEADER_T *)(data + pos);
665 int msgid = header->msgid;
667 if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
668 int port = VCHIQ_MSG_SRCPORT(msgid);
669 VCHIQ_SERVICE_QUOTA_T *service_quota =
670 &state->service_quotas[port];
673 spin_lock("a_spinlock);
674 count = service_quota->message_use_count;
676 service_quota->message_use_count =
678 spin_unlock("a_spinlock);
680 if (count == service_quota->message_quota)
681 /* Signal the service that it
682 ** has dropped below its quota
684 up(&service_quota->quota_event);
685 else if (count == 0) {
686 vchiq_log_error(vchiq_core_log_level,
687 "service %d message_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
689 service_quota->message_use_count,
690 header, msgid, header->msgid,
692 WARN(1, "invalid message use count\n");
694 if (!BITSET_IS_SET(service_found, port)) {
695 /* Set the found bit for this service */
696 BITSET_SET(service_found, port);
698 spin_lock("a_spinlock);
699 count = service_quota->slot_use_count;
701 service_quota->slot_use_count =
703 spin_unlock("a_spinlock);
706 /* Signal the service in case
707 ** it has dropped below its
709 up(&service_quota->quota_event);
711 vchiq_core_log_level,
712 "%d: pfq:%d %x@%pK - slot_use->%d",
714 header->size, header,
718 vchiq_core_log_level,
719 "service %d slot_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
721 msgid, header->msgid,
723 WARN(1, "bad slot use count\n");
730 pos += calc_stride(header->size);
731 if (pos > VCHIQ_SLOT_SIZE) {
732 vchiq_log_error(vchiq_core_log_level,
733 "pfq - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
734 pos, header, msgid, header->msgid,
736 WARN(1, "invalid slot position\n");
743 spin_lock("a_spinlock);
744 count = state->data_use_count;
746 state->data_use_count =
748 spin_unlock("a_spinlock);
749 if (count == state->data_quota)
750 up(&state->data_quota_event);
754 * Don't allow the slot to be reused until we are no
755 * longer interested in it.
759 state->slot_queue_available = slot_queue_available;
760 up(&state->slot_available_event);
765 memcpy_copy_callback(
766 void *context, void *dest,
767 size_t offset, size_t maxsize)
769 memcpy(dest + offset, context + offset, maxsize);
775 ssize_t (*copy_callback)(void *context, void *dest,
776 size_t offset, size_t maxsize),
784 ssize_t callback_result;
785 size_t max_bytes = size - pos;
788 copy_callback(context, dest + pos,
791 if (callback_result < 0)
792 return callback_result;
794 if (!callback_result)
797 if (callback_result > max_bytes)
800 pos += callback_result;
806 /* Called by the slot handler and application threads */
807 static VCHIQ_STATUS_T
808 queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
810 ssize_t (*copy_callback)(void *context, void *dest,
811 size_t offset, size_t maxsize),
816 VCHIQ_SHARED_STATE_T *local;
817 VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
818 VCHIQ_HEADER_T *header;
819 int type = VCHIQ_MSG_TYPE(msgid);
823 local = state->local;
825 stride = calc_stride(size);
827 WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
829 if (!(flags & QMFLAGS_NO_MUTEX_LOCK) &&
830 (mutex_lock_killable(&state->slot_mutex) != 0))
833 if (type == VCHIQ_MSG_DATA) {
837 WARN(1, "%s: service is NULL\n", __func__);
838 mutex_unlock(&state->slot_mutex);
842 WARN_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
843 QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
845 if (service->closing) {
846 /* The service has been closed */
847 mutex_unlock(&state->slot_mutex);
851 service_quota = &state->service_quotas[service->localport];
853 spin_lock("a_spinlock);
855 /* Ensure this service doesn't use more than its quota of
856 ** messages or slots */
857 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
858 state->local_tx_pos + stride - 1);
860 /* Ensure data messages don't use more than their quota of
862 while ((tx_end_index != state->previous_data_index) &&
863 (state->data_use_count == state->data_quota)) {
864 VCHIQ_STATS_INC(state, data_stalls);
865 spin_unlock("a_spinlock);
866 mutex_unlock(&state->slot_mutex);
868 if (down_interruptible(&state->data_quota_event)
872 mutex_lock(&state->slot_mutex);
873 spin_lock("a_spinlock);
874 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
875 state->local_tx_pos + stride - 1);
876 if ((tx_end_index == state->previous_data_index) ||
877 (state->data_use_count < state->data_quota)) {
878 /* Pass the signal on to other waiters */
879 up(&state->data_quota_event);
884 while ((service_quota->message_use_count ==
885 service_quota->message_quota) ||
886 ((tx_end_index != service_quota->previous_tx_index) &&
887 (service_quota->slot_use_count ==
888 service_quota->slot_quota))) {
889 spin_unlock("a_spinlock);
890 vchiq_log_trace(vchiq_core_log_level,
891 "%d: qm:%d %s,%zx - quota stall "
893 state->id, service->localport,
894 msg_type_str(type), size,
895 service_quota->message_use_count,
896 service_quota->slot_use_count);
897 VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
898 mutex_unlock(&state->slot_mutex);
899 if (down_interruptible(&service_quota->quota_event)
902 if (service->closing)
904 if (mutex_lock_killable(&state->slot_mutex) != 0)
906 if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
907 /* The service has been closed */
908 mutex_unlock(&state->slot_mutex);
911 spin_lock("a_spinlock);
912 tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
913 state->local_tx_pos + stride - 1);
916 spin_unlock("a_spinlock);
919 header = reserve_space(state, stride, flags & QMFLAGS_IS_BLOCKING);
923 VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
924 /* In the event of a failure, return the mutex to the
926 if (!(flags & QMFLAGS_NO_MUTEX_LOCK))
927 mutex_unlock(&state->slot_mutex);
931 if (type == VCHIQ_MSG_DATA) {
932 ssize_t callback_result;
936 vchiq_log_info(vchiq_core_log_level,
937 "%d: qm %s@%pK,%zx (%d->%d)",
938 state->id, msg_type_str(VCHIQ_MSG_TYPE(msgid)),
939 header, size, VCHIQ_MSG_SRCPORT(msgid),
940 VCHIQ_MSG_DSTPORT(msgid));
942 WARN_ON((flags & (QMFLAGS_NO_MUTEX_LOCK |
943 QMFLAGS_NO_MUTEX_UNLOCK)) != 0);
946 copy_message_data(copy_callback, context,
949 if (callback_result < 0) {
950 mutex_unlock(&state->slot_mutex);
951 VCHIQ_SERVICE_STATS_INC(service,
956 if (SRVTRACE_ENABLED(service,
958 vchiq_log_dump_mem("Sent", 0,
961 (size_t)callback_result));
963 spin_lock("a_spinlock);
964 service_quota->message_use_count++;
967 SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
969 /* If this transmission can't fit in the last slot used by any
970 ** service, the data_use_count must be increased. */
971 if (tx_end_index != state->previous_data_index) {
972 state->previous_data_index = tx_end_index;
973 state->data_use_count++;
976 /* If this isn't the same slot last used by this service,
977 ** the service's slot_use_count must be increased. */
978 if (tx_end_index != service_quota->previous_tx_index) {
979 service_quota->previous_tx_index = tx_end_index;
980 slot_use_count = ++service_quota->slot_use_count;
985 spin_unlock("a_spinlock);
988 vchiq_log_trace(vchiq_core_log_level,
989 "%d: qm:%d %s,%zx - slot_use->%d (hdr %p)",
990 state->id, service->localport,
991 msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
992 slot_use_count, header);
994 VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
995 VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
997 vchiq_log_info(vchiq_core_log_level,
998 "%d: qm %s@%pK,%zx (%d->%d)", state->id,
999 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1000 header, size, VCHIQ_MSG_SRCPORT(msgid),
1001 VCHIQ_MSG_DSTPORT(msgid));
1003 /* It is assumed for now that this code path
1004 * only happens from calls inside this file.
1006 * External callers are through the vchiq_queue_message
1007 * path which always sets the type to be VCHIQ_MSG_DATA
1009 * At first glance this appears to be correct but
1010 * more review is needed.
1012 copy_message_data(copy_callback, context,
1013 header->data, size);
1015 VCHIQ_STATS_INC(state, ctrl_tx_count);
1018 header->msgid = msgid;
1019 header->size = size;
1024 svc_fourcc = service
1025 ? service->base.fourcc
1026 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1028 vchiq_log_info(SRVTRACE_LEVEL(service),
1029 "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%zu",
1030 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1031 VCHIQ_MSG_TYPE(msgid),
1032 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1033 VCHIQ_MSG_SRCPORT(msgid),
1034 VCHIQ_MSG_DSTPORT(msgid),
1038 /* Make sure the new header is visible to the peer. */
1041 /* Make the new tx_pos visible to the peer. */
1042 local->tx_pos = state->local_tx_pos;
1045 if (service && (type == VCHIQ_MSG_CLOSE))
1046 vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
1048 if (!(flags & QMFLAGS_NO_MUTEX_UNLOCK))
1049 mutex_unlock(&state->slot_mutex);
1051 remote_event_signal(&state->remote->trigger);
1053 return VCHIQ_SUCCESS;
1056 /* Called by the slot handler and application threads */
1057 static VCHIQ_STATUS_T
1058 queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
1060 ssize_t (*copy_callback)(void *context, void *dest,
1061 size_t offset, size_t maxsize),
1066 VCHIQ_SHARED_STATE_T *local;
1067 VCHIQ_HEADER_T *header;
1068 ssize_t callback_result;
1070 local = state->local;
1072 if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
1073 (mutex_lock_killable(&state->sync_mutex) != 0))
1076 remote_event_wait(state, &local->sync_release);
1080 header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
1084 int oldmsgid = header->msgid;
1086 if (oldmsgid != VCHIQ_MSGID_PADDING)
1087 vchiq_log_error(vchiq_core_log_level,
1088 "%d: qms - msgid %x, not PADDING",
1089 state->id, oldmsgid);
1092 vchiq_log_info(vchiq_sync_log_level,
1093 "%d: qms %s@%pK,%x (%d->%d)", state->id,
1094 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1095 header, size, VCHIQ_MSG_SRCPORT(msgid),
1096 VCHIQ_MSG_DSTPORT(msgid));
1099 copy_message_data(copy_callback, context,
1100 header->data, size);
1102 if (callback_result < 0) {
1103 mutex_unlock(&state->slot_mutex);
1104 VCHIQ_SERVICE_STATS_INC(service,
1110 if (SRVTRACE_ENABLED(service,
1112 vchiq_log_dump_mem("Sent", 0,
1115 (size_t)callback_result));
1117 VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
1118 VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
1120 VCHIQ_STATS_INC(state, ctrl_tx_count);
1123 header->size = size;
1124 header->msgid = msgid;
1126 if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
1129 svc_fourcc = service
1130 ? service->base.fourcc
1131 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1133 vchiq_log_trace(vchiq_sync_log_level,
1134 "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
1135 msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1136 VCHIQ_MSG_TYPE(msgid),
1137 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1138 VCHIQ_MSG_SRCPORT(msgid),
1139 VCHIQ_MSG_DSTPORT(msgid),
1143 /* Make sure the new header is visible to the peer. */
1146 remote_event_signal(&state->remote->sync_trigger);
1148 if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
1149 mutex_unlock(&state->sync_mutex);
1151 return VCHIQ_SUCCESS;
1155 claim_slot(VCHIQ_SLOT_INFO_T *slot)
1161 release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
1162 VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
1166 mutex_lock(&state->recycle_mutex);
1169 int msgid = header->msgid;
1171 if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
1172 (service && service->closing)) {
1173 mutex_unlock(&state->recycle_mutex);
1177 /* Rewrite the message header to prevent a double
1179 header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
1182 release_count = slot_info->release_count;
1183 slot_info->release_count = ++release_count;
1185 if (release_count == slot_info->use_count) {
1186 int slot_queue_recycle;
1187 /* Add to the freed queue */
1189 /* A read barrier is necessary here to prevent speculative
1190 ** fetches of remote->slot_queue_recycle from overtaking the
1194 slot_queue_recycle = state->remote->slot_queue_recycle;
1195 state->remote->slot_queue[slot_queue_recycle &
1196 VCHIQ_SLOT_QUEUE_MASK] =
1197 SLOT_INDEX_FROM_INFO(state, slot_info);
1198 state->remote->slot_queue_recycle = slot_queue_recycle + 1;
1199 vchiq_log_info(vchiq_core_log_level,
1200 "%d: %s %d - recycle->%x", state->id, __func__,
1201 SLOT_INDEX_FROM_INFO(state, slot_info),
1202 state->remote->slot_queue_recycle);
1204 /* A write barrier is necessary, but remote_event_signal
1206 remote_event_signal(&state->remote->recycle);
1209 mutex_unlock(&state->recycle_mutex);
1212 /* Called by the slot handler - don't hold the bulk mutex */
1213 static VCHIQ_STATUS_T
1214 notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
1217 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
1219 vchiq_log_trace(vchiq_core_log_level,
1220 "%d: nb:%d %cx - p=%x rn=%x r=%x",
1221 service->state->id, service->localport,
1222 (queue == &service->bulk_tx) ? 't' : 'r',
1223 queue->process, queue->remote_notify, queue->remove);
1225 if (service->state->is_master) {
1226 while (queue->remote_notify != queue->process) {
1227 VCHIQ_BULK_T *bulk =
1228 &queue->bulks[BULK_INDEX(queue->remote_notify)];
1229 int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
1230 VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
1231 int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
1232 service->remoteport);
1233 /* Only reply to non-dummy bulk requests */
1234 if (bulk->remote_data) {
1235 status = queue_message(
1239 memcpy_copy_callback,
1243 if (status != VCHIQ_SUCCESS)
1246 queue->remote_notify++;
1249 queue->remote_notify = queue->process;
1252 if (status == VCHIQ_SUCCESS) {
1253 while (queue->remove != queue->remote_notify) {
1254 VCHIQ_BULK_T *bulk =
1255 &queue->bulks[BULK_INDEX(queue->remove)];
1257 /* Only generate callbacks for non-dummy bulk
1258 ** requests, and non-terminated services */
1259 if (bulk->data && service->instance) {
1260 if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
1261 if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
1262 VCHIQ_SERVICE_STATS_INC(service,
1264 VCHIQ_SERVICE_STATS_ADD(service,
1268 VCHIQ_SERVICE_STATS_INC(service,
1270 VCHIQ_SERVICE_STATS_ADD(service,
1275 VCHIQ_SERVICE_STATS_INC(service,
1276 bulk_aborted_count);
1278 if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
1279 struct bulk_waiter *waiter;
1281 spin_lock(&bulk_waiter_spinlock);
1282 waiter = bulk->userdata;
1284 waiter->actual = bulk->actual;
1287 spin_unlock(&bulk_waiter_spinlock);
1288 } else if (bulk->mode ==
1289 VCHIQ_BULK_MODE_CALLBACK) {
1290 VCHIQ_REASON_T reason = (bulk->dir ==
1291 VCHIQ_BULK_TRANSMIT) ?
1293 VCHIQ_BULK_ACTUAL_ABORTED) ?
1294 VCHIQ_BULK_TRANSMIT_ABORTED :
1295 VCHIQ_BULK_TRANSMIT_DONE) :
1297 VCHIQ_BULK_ACTUAL_ABORTED) ?
1298 VCHIQ_BULK_RECEIVE_ABORTED :
1299 VCHIQ_BULK_RECEIVE_DONE);
1300 status = make_service_callback(service,
1301 reason, NULL, bulk->userdata);
1302 if (status == VCHIQ_RETRY)
1308 up(&service->bulk_remove_event);
1311 status = VCHIQ_SUCCESS;
1314 if (status == VCHIQ_RETRY)
1315 request_poll(service->state, service,
1316 (queue == &service->bulk_tx) ?
1317 VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
1322 /* Called by the slot handler thread */
1324 poll_services(VCHIQ_STATE_T *state)
1328 for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
1331 flags = atomic_xchg(&state->poll_services[group], 0);
1332 for (i = 0; flags; i++) {
1333 if (flags & (1 << i)) {
1334 VCHIQ_SERVICE_T *service =
1335 find_service_by_port(state,
1343 atomic_xchg(&service->poll_flags, 0);
1345 (1 << VCHIQ_POLL_REMOVE)) {
1346 vchiq_log_info(vchiq_core_log_level,
1347 "%d: ps - remove %d<->%d",
1348 state->id, service->localport,
1349 service->remoteport);
1351 /* Make it look like a client, because
1352 it must be removed and not left in
1353 the LISTENING state. */
1354 service->public_fourcc =
1355 VCHIQ_FOURCC_INVALID;
1357 if (vchiq_close_service_internal(
1358 service, 0/*!close_recvd*/) !=
1360 request_poll(state, service,
1362 } else if (service_flags &
1363 (1 << VCHIQ_POLL_TERMINATE)) {
1364 vchiq_log_info(vchiq_core_log_level,
1365 "%d: ps - terminate %d<->%d",
1366 state->id, service->localport,
1367 service->remoteport);
1368 if (vchiq_close_service_internal(
1369 service, 0/*!close_recvd*/) !=
1371 request_poll(state, service,
1372 VCHIQ_POLL_TERMINATE);
1374 if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
1375 notify_bulks(service,
1378 if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
1379 notify_bulks(service,
1382 unlock_service(service);
1388 /* Called by the slot handler or application threads, holding the bulk mutex. */
1390 resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
1392 VCHIQ_STATE_T *state = service->state;
1395 while ((queue->process != queue->local_insert) &&
1396 (queue->process != queue->remote_insert)) {
1397 VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
1399 vchiq_log_trace(vchiq_core_log_level,
1400 "%d: rb:%d %cx - li=%x ri=%x p=%x",
1401 state->id, service->localport,
1402 (queue == &service->bulk_tx) ? 't' : 'r',
1403 queue->local_insert, queue->remote_insert,
1406 WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
1407 WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
1409 if (mutex_lock_killable(&state->bulk_transfer_mutex))
1412 vchiq_transfer_bulk(bulk);
1413 mutex_unlock(&state->bulk_transfer_mutex);
1415 if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
1416 const char *header = (queue == &service->bulk_tx) ?
1417 "Send Bulk to" : "Recv Bulk from";
1418 if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
1419 vchiq_log_info(SRVTRACE_LEVEL(service),
1420 "%s %c%c%c%c d:%d len:%d %pK<->%pK",
1422 VCHIQ_FOURCC_AS_4CHARS(
1423 service->base.fourcc),
1424 service->remoteport, bulk->size,
1425 bulk->data, bulk->remote_data);
1427 vchiq_log_info(SRVTRACE_LEVEL(service),
1428 "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
1429 " rx len:%d %pK<->%pK",
1431 VCHIQ_FOURCC_AS_4CHARS(
1432 service->base.fourcc),
1433 service->remoteport,
1434 bulk->size, bulk->remote_size,
1435 bulk->data, bulk->remote_data);
1438 vchiq_complete_bulk(bulk);
1445 /* Called with the bulk_mutex held */
1447 abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
1449 int is_tx = (queue == &service->bulk_tx);
1451 vchiq_log_trace(vchiq_core_log_level,
1452 "%d: aob:%d %cx - li=%x ri=%x p=%x",
1453 service->state->id, service->localport, is_tx ? 't' : 'r',
1454 queue->local_insert, queue->remote_insert, queue->process);
1456 WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
1457 WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
1459 while ((queue->process != queue->local_insert) ||
1460 (queue->process != queue->remote_insert)) {
1461 VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
1463 if (queue->process == queue->remote_insert) {
1464 /* fabricate a matching dummy bulk */
1465 bulk->remote_data = NULL;
1466 bulk->remote_size = 0;
1467 queue->remote_insert++;
1470 if (queue->process != queue->local_insert) {
1471 vchiq_complete_bulk(bulk);
1473 vchiq_log_info(SRVTRACE_LEVEL(service),
1474 "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
1476 is_tx ? "Send Bulk to" : "Recv Bulk from",
1477 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1478 service->remoteport,
1482 /* fabricate a matching dummy bulk */
1485 bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
1486 bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
1488 queue->local_insert++;
1495 /* Called from the slot handler thread */
1497 pause_bulks(VCHIQ_STATE_T *state)
1499 if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
1501 atomic_set(&pause_bulks_count, 1);
1505 /* Block bulk transfers from all services */
1506 mutex_lock(&state->bulk_transfer_mutex);
1509 /* Called from the slot handler thread */
1511 resume_bulks(VCHIQ_STATE_T *state)
1515 if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
1517 atomic_set(&pause_bulks_count, 0);
1521 /* Allow bulk transfers from all services */
1522 mutex_unlock(&state->bulk_transfer_mutex);
1524 if (state->deferred_bulks == 0)
1527 /* Deal with any bulks which had to be deferred due to being in
1528 * paused state. Don't try to match up to number of deferred bulks
1529 * in case we've had something come and close the service in the
1530 * interim - just process all bulk queues for all services */
1531 vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
1532 __func__, state->deferred_bulks);
1534 for (i = 0; i < state->unused_service; i++) {
1535 VCHIQ_SERVICE_T *service = state->services[i];
1536 int resolved_rx = 0;
1537 int resolved_tx = 0;
1539 if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
1542 mutex_lock(&service->bulk_mutex);
1543 resolved_rx = resolve_bulks(service, &service->bulk_rx);
1544 resolved_tx = resolve_bulks(service, &service->bulk_tx);
1545 mutex_unlock(&service->bulk_mutex);
1547 notify_bulks(service, &service->bulk_rx, 1);
1549 notify_bulks(service, &service->bulk_tx, 1);
1551 state->deferred_bulks = 0;
1555 parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
1557 VCHIQ_SERVICE_T *service = NULL;
1559 unsigned int localport, remoteport;
1561 msgid = header->msgid;
1562 size = header->size;
1563 localport = VCHIQ_MSG_DSTPORT(msgid);
1564 remoteport = VCHIQ_MSG_SRCPORT(msgid);
1565 if (size >= sizeof(struct vchiq_open_payload)) {
1566 const struct vchiq_open_payload *payload =
1567 (struct vchiq_open_payload *)header->data;
1568 unsigned int fourcc;
1570 fourcc = payload->fourcc;
1571 vchiq_log_info(vchiq_core_log_level,
1572 "%d: prs OPEN@%pK (%d->'%c%c%c%c')",
1573 state->id, header, localport,
1574 VCHIQ_FOURCC_AS_4CHARS(fourcc));
1576 service = get_listening_service(state, fourcc);
1579 /* A matching service exists */
1580 short version = payload->version;
1581 short version_min = payload->version_min;
1583 if ((service->version < version_min) ||
1584 (version < service->version_min)) {
1585 /* Version mismatch */
1586 vchiq_loud_error_header();
1587 vchiq_loud_error("%d: service %d (%c%c%c%c) "
1588 "version mismatch - local (%d, min %d)"
1589 " vs. remote (%d, min %d)",
1590 state->id, service->localport,
1591 VCHIQ_FOURCC_AS_4CHARS(fourcc),
1592 service->version, service->version_min,
1593 version, version_min);
1594 vchiq_loud_error_footer();
1595 unlock_service(service);
1599 service->peer_version = version;
1601 if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
1602 struct vchiq_openack_payload ack_payload = {
1606 if (state->version_common <
1607 VCHIQ_VERSION_SYNCHRONOUS_MODE)
1610 /* Acknowledge the OPEN */
1611 if (service->sync &&
1612 (state->version_common >=
1613 VCHIQ_VERSION_SYNCHRONOUS_MODE)) {
1614 if (queue_message_sync(
1621 memcpy_copy_callback,
1623 sizeof(ack_payload),
1625 goto bail_not_ready;
1627 if (queue_message(state,
1633 memcpy_copy_callback,
1635 sizeof(ack_payload),
1637 goto bail_not_ready;
1640 /* The service is now open */
1641 vchiq_set_service_state(service,
1642 service->sync ? VCHIQ_SRVSTATE_OPENSYNC
1643 : VCHIQ_SRVSTATE_OPEN);
1646 service->remoteport = remoteport;
1647 service->client_id = ((int *)header->data)[1];
1648 if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
1649 NULL, NULL) == VCHIQ_RETRY) {
1650 /* Bail out if not ready */
1651 service->remoteport = VCHIQ_PORT_FREE;
1652 goto bail_not_ready;
1655 /* Success - the message has been dealt with */
1656 unlock_service(service);
1662 /* No available service, or an invalid request - send a CLOSE */
1663 if (queue_message(state, NULL,
1664 VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
1665 NULL, NULL, 0, 0) == VCHIQ_RETRY)
1666 goto bail_not_ready;
1672 unlock_service(service);
1677 /* Called by the slot handler thread */
1679 parse_rx_slots(VCHIQ_STATE_T *state)
1681 VCHIQ_SHARED_STATE_T *remote = state->remote;
1682 VCHIQ_SERVICE_T *service = NULL;
1685 DEBUG_INITIALISE(state->local)
1687 tx_pos = remote->tx_pos;
1689 while (state->rx_pos != tx_pos) {
1690 VCHIQ_HEADER_T *header;
1693 unsigned int localport, remoteport;
1695 DEBUG_TRACE(PARSE_LINE);
1696 if (!state->rx_data) {
1699 WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
1700 rx_index = remote->slot_queue[
1701 SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
1702 VCHIQ_SLOT_QUEUE_MASK];
1703 state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
1705 state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
1707 /* Initialise use_count to one, and increment
1708 ** release_count at the end of the slot to avoid
1709 ** releasing the slot prematurely. */
1710 state->rx_info->use_count = 1;
1711 state->rx_info->release_count = 0;
1714 header = (VCHIQ_HEADER_T *)(state->rx_data +
1715 (state->rx_pos & VCHIQ_SLOT_MASK));
1716 DEBUG_VALUE(PARSE_HEADER, (int)(long)header);
1717 msgid = header->msgid;
1718 DEBUG_VALUE(PARSE_MSGID, msgid);
1719 size = header->size;
1720 type = VCHIQ_MSG_TYPE(msgid);
1721 localport = VCHIQ_MSG_DSTPORT(msgid);
1722 remoteport = VCHIQ_MSG_SRCPORT(msgid);
1724 if (type != VCHIQ_MSG_DATA)
1725 VCHIQ_STATS_INC(state, ctrl_rx_count);
1728 case VCHIQ_MSG_OPENACK:
1729 case VCHIQ_MSG_CLOSE:
1730 case VCHIQ_MSG_DATA:
1731 case VCHIQ_MSG_BULK_RX:
1732 case VCHIQ_MSG_BULK_TX:
1733 case VCHIQ_MSG_BULK_RX_DONE:
1734 case VCHIQ_MSG_BULK_TX_DONE:
1735 service = find_service_by_port(state, localport);
1737 ((service->remoteport != remoteport) &&
1738 (service->remoteport != VCHIQ_PORT_FREE))) &&
1740 (type == VCHIQ_MSG_CLOSE)) {
1741 /* This could be a CLOSE from a client which
1742 hadn't yet received the OPENACK - look for
1743 the connected service */
1745 unlock_service(service);
1746 service = get_connected_service(state,
1749 vchiq_log_warning(vchiq_core_log_level,
1750 "%d: prs %s@%pK (%d->%d) - found connected service %d",
1751 state->id, msg_type_str(type),
1752 header, remoteport, localport,
1753 service->localport);
1757 vchiq_log_error(vchiq_core_log_level,
1758 "%d: prs %s@%pK (%d->%d) - invalid/closed service %d",
1759 state->id, msg_type_str(type),
1760 header, remoteport, localport,
1769 if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
1772 svc_fourcc = service
1773 ? service->base.fourcc
1774 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1775 vchiq_log_info(SRVTRACE_LEVEL(service),
1776 "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
1778 msg_type_str(type), type,
1779 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1780 remoteport, localport, size);
1782 vchiq_log_dump_mem("Rcvd", 0, header->data,
1786 if (((unsigned long)header & VCHIQ_SLOT_MASK) +
1787 calc_stride(size) > VCHIQ_SLOT_SIZE) {
1788 vchiq_log_error(vchiq_core_log_level,
1789 "header %pK (msgid %x) - size %x too big for slot",
1790 header, (unsigned int)msgid,
1791 (unsigned int)size);
1792 WARN(1, "oversized for slot\n");
1796 case VCHIQ_MSG_OPEN:
1797 WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
1798 if (!parse_open(state, header))
1799 goto bail_not_ready;
1801 case VCHIQ_MSG_OPENACK:
1802 if (size >= sizeof(struct vchiq_openack_payload)) {
1803 const struct vchiq_openack_payload *payload =
1804 (struct vchiq_openack_payload *)
1806 service->peer_version = payload->version;
1808 vchiq_log_info(vchiq_core_log_level,
1809 "%d: prs OPENACK@%pK,%x (%d->%d) v:%d",
1810 state->id, header, size, remoteport, localport,
1811 service->peer_version);
1812 if (service->srvstate ==
1813 VCHIQ_SRVSTATE_OPENING) {
1814 service->remoteport = remoteport;
1815 vchiq_set_service_state(service,
1816 VCHIQ_SRVSTATE_OPEN);
1817 up(&service->remove_event);
1819 vchiq_log_error(vchiq_core_log_level,
1820 "OPENACK received in state %s",
1821 srvstate_names[service->srvstate]);
1823 case VCHIQ_MSG_CLOSE:
1824 WARN_ON(size != 0); /* There should be no data */
1826 vchiq_log_info(vchiq_core_log_level,
1827 "%d: prs CLOSE@%pK (%d->%d)",
1828 state->id, header, remoteport, localport);
1830 mark_service_closing_internal(service, 1);
1832 if (vchiq_close_service_internal(service,
1833 1/*close_recvd*/) == VCHIQ_RETRY)
1834 goto bail_not_ready;
1836 vchiq_log_info(vchiq_core_log_level,
1837 "Close Service %c%c%c%c s:%u d:%d",
1838 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1840 service->remoteport);
1842 case VCHIQ_MSG_DATA:
1843 vchiq_log_info(vchiq_core_log_level,
1844 "%d: prs DATA@%pK,%x (%d->%d)",
1845 state->id, header, size, remoteport, localport);
1847 if ((service->remoteport == remoteport)
1848 && (service->srvstate ==
1849 VCHIQ_SRVSTATE_OPEN)) {
1850 header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
1851 claim_slot(state->rx_info);
1852 DEBUG_TRACE(PARSE_LINE);
1853 if (make_service_callback(service,
1854 VCHIQ_MESSAGE_AVAILABLE, header,
1855 NULL) == VCHIQ_RETRY) {
1856 DEBUG_TRACE(PARSE_LINE);
1857 goto bail_not_ready;
1859 VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
1860 VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
1863 VCHIQ_STATS_INC(state, error_count);
1866 case VCHIQ_MSG_CONNECT:
1867 vchiq_log_info(vchiq_core_log_level,
1868 "%d: prs CONNECT@%pK", state->id, header);
1869 state->version_common = ((VCHIQ_SLOT_ZERO_T *)
1870 state->slot_data)->version;
1871 up(&state->connect);
1873 case VCHIQ_MSG_BULK_RX:
1874 case VCHIQ_MSG_BULK_TX: {
1875 VCHIQ_BULK_QUEUE_T *queue;
1877 WARN_ON(!state->is_master);
1878 queue = (type == VCHIQ_MSG_BULK_RX) ?
1879 &service->bulk_tx : &service->bulk_rx;
1880 if ((service->remoteport == remoteport)
1881 && (service->srvstate ==
1882 VCHIQ_SRVSTATE_OPEN)) {
1886 DEBUG_TRACE(PARSE_LINE);
1887 if (mutex_lock_killable(
1888 &service->bulk_mutex) != 0) {
1889 DEBUG_TRACE(PARSE_LINE);
1890 goto bail_not_ready;
1893 WARN_ON(!(queue->remote_insert < queue->remove +
1894 VCHIQ_NUM_SERVICE_BULKS));
1895 bulk = &queue->bulks[
1896 BULK_INDEX(queue->remote_insert)];
1898 (void *)(long)((int *)header->data)[0];
1899 bulk->remote_size = ((int *)header->data)[1];
1902 vchiq_log_info(vchiq_core_log_level,
1903 "%d: prs %s@%pK (%d->%d) %x@%pK",
1904 state->id, msg_type_str(type),
1905 header, remoteport, localport,
1906 bulk->remote_size, bulk->remote_data);
1908 queue->remote_insert++;
1910 if (atomic_read(&pause_bulks_count)) {
1911 state->deferred_bulks++;
1912 vchiq_log_info(vchiq_core_log_level,
1913 "%s: deferring bulk (%d)",
1915 state->deferred_bulks);
1916 if (state->conn_state !=
1917 VCHIQ_CONNSTATE_PAUSE_SENT)
1919 vchiq_core_log_level,
1920 "%s: bulks paused in "
1921 "unexpected state %s",
1924 state->conn_state]);
1925 } else if (state->conn_state ==
1926 VCHIQ_CONNSTATE_CONNECTED) {
1927 DEBUG_TRACE(PARSE_LINE);
1928 resolved = resolve_bulks(service,
1932 mutex_unlock(&service->bulk_mutex);
1934 notify_bulks(service, queue,
1938 case VCHIQ_MSG_BULK_RX_DONE:
1939 case VCHIQ_MSG_BULK_TX_DONE:
1940 WARN_ON(state->is_master);
1941 if ((service->remoteport == remoteport)
1942 && (service->srvstate !=
1943 VCHIQ_SRVSTATE_FREE)) {
1944 VCHIQ_BULK_QUEUE_T *queue;
1947 queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
1948 &service->bulk_rx : &service->bulk_tx;
1950 DEBUG_TRACE(PARSE_LINE);
1951 if (mutex_lock_killable(
1952 &service->bulk_mutex) != 0) {
1953 DEBUG_TRACE(PARSE_LINE);
1954 goto bail_not_ready;
1956 if ((int)(queue->remote_insert -
1957 queue->local_insert) >= 0) {
1958 vchiq_log_error(vchiq_core_log_level,
1959 "%d: prs %s@%pK (%d->%d) "
1960 "unexpected (ri=%d,li=%d)",
1961 state->id, msg_type_str(type),
1962 header, remoteport, localport,
1963 queue->remote_insert,
1964 queue->local_insert);
1965 mutex_unlock(&service->bulk_mutex);
1968 if (queue->process != queue->remote_insert) {
1969 pr_err("%s: p %x != ri %x\n",
1972 queue->remote_insert);
1973 mutex_unlock(&service->bulk_mutex);
1974 goto bail_not_ready;
1977 bulk = &queue->bulks[
1978 BULK_INDEX(queue->remote_insert)];
1979 bulk->actual = *(int *)header->data;
1980 queue->remote_insert++;
1982 vchiq_log_info(vchiq_core_log_level,
1983 "%d: prs %s@%pK (%d->%d) %x@%pK",
1984 state->id, msg_type_str(type),
1985 header, remoteport, localport,
1986 bulk->actual, bulk->data);
1988 vchiq_log_trace(vchiq_core_log_level,
1989 "%d: prs:%d %cx li=%x ri=%x p=%x",
1990 state->id, localport,
1991 (type == VCHIQ_MSG_BULK_RX_DONE) ?
1993 queue->local_insert,
1994 queue->remote_insert, queue->process);
1996 DEBUG_TRACE(PARSE_LINE);
1997 WARN_ON(queue->process == queue->local_insert);
1998 vchiq_complete_bulk(bulk);
2000 mutex_unlock(&service->bulk_mutex);
2001 DEBUG_TRACE(PARSE_LINE);
2002 notify_bulks(service, queue, 1/*retry_poll*/);
2003 DEBUG_TRACE(PARSE_LINE);
2006 case VCHIQ_MSG_PADDING:
2007 vchiq_log_trace(vchiq_core_log_level,
2008 "%d: prs PADDING@%pK,%x",
2009 state->id, header, size);
2011 case VCHIQ_MSG_PAUSE:
2012 /* If initiated, signal the application thread */
2013 vchiq_log_trace(vchiq_core_log_level,
2014 "%d: prs PAUSE@%pK,%x",
2015 state->id, header, size);
2016 if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
2017 vchiq_log_error(vchiq_core_log_level,
2018 "%d: PAUSE received in state PAUSED",
2022 if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
2023 /* Send a PAUSE in response */
2024 if (queue_message(state, NULL,
2025 VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
2026 NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK)
2028 goto bail_not_ready;
2029 if (state->is_master)
2032 /* At this point slot_mutex is held */
2033 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
2034 vchiq_platform_paused(state);
2036 case VCHIQ_MSG_RESUME:
2037 vchiq_log_trace(vchiq_core_log_level,
2038 "%d: prs RESUME@%pK,%x",
2039 state->id, header, size);
2040 /* Release the slot mutex */
2041 mutex_unlock(&state->slot_mutex);
2042 if (state->is_master)
2043 resume_bulks(state);
2044 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
2045 vchiq_platform_resumed(state);
2048 case VCHIQ_MSG_REMOTE_USE:
2049 vchiq_on_remote_use(state);
2051 case VCHIQ_MSG_REMOTE_RELEASE:
2052 vchiq_on_remote_release(state);
2054 case VCHIQ_MSG_REMOTE_USE_ACTIVE:
2055 vchiq_on_remote_use_active(state);
2059 vchiq_log_error(vchiq_core_log_level,
2060 "%d: prs invalid msgid %x@%pK,%x",
2061 state->id, msgid, header, size);
2062 WARN(1, "invalid message\n");
2068 unlock_service(service);
2072 state->rx_pos += calc_stride(size);
2074 DEBUG_TRACE(PARSE_LINE);
2075 /* Perform some housekeeping when the end of the slot is
2077 if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
2078 /* Remove the extra reference count. */
2079 release_slot(state, state->rx_info, NULL, NULL);
2080 state->rx_data = NULL;
2086 unlock_service(service);
2089 /* Called by the slot handler thread */
2091 slot_handler_func(void *v)
2093 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
2094 VCHIQ_SHARED_STATE_T *local = state->local;
2096 DEBUG_INITIALISE(local)
2099 DEBUG_COUNT(SLOT_HANDLER_COUNT);
2100 DEBUG_TRACE(SLOT_HANDLER_LINE);
2101 remote_event_wait(state, &local->trigger);
2105 DEBUG_TRACE(SLOT_HANDLER_LINE);
2106 if (state->poll_needed) {
2107 /* Check if we need to suspend - may change our
2109 vchiq_platform_check_suspend(state);
2111 state->poll_needed = 0;
2113 /* Handle service polling and other rare conditions here
2114 ** out of the mainline code */
2115 switch (state->conn_state) {
2116 case VCHIQ_CONNSTATE_CONNECTED:
2117 /* Poll the services as requested */
2118 poll_services(state);
2121 case VCHIQ_CONNSTATE_PAUSING:
2122 if (state->is_master)
2124 if (queue_message(state, NULL,
2125 VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
2127 QMFLAGS_NO_MUTEX_UNLOCK)
2129 vchiq_set_conn_state(state,
2130 VCHIQ_CONNSTATE_PAUSE_SENT);
2132 if (state->is_master)
2133 resume_bulks(state);
2135 state->poll_needed = 1;
2139 case VCHIQ_CONNSTATE_PAUSED:
2140 vchiq_platform_resume(state);
2143 case VCHIQ_CONNSTATE_RESUMING:
2144 if (queue_message(state, NULL,
2145 VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
2146 NULL, NULL, 0, QMFLAGS_NO_MUTEX_LOCK)
2148 if (state->is_master)
2149 resume_bulks(state);
2150 vchiq_set_conn_state(state,
2151 VCHIQ_CONNSTATE_CONNECTED);
2152 vchiq_platform_resumed(state);
2154 /* This should really be impossible,
2155 ** since the PAUSE should have flushed
2156 ** through outstanding messages. */
2157 vchiq_log_error(vchiq_core_log_level,
2158 "Failed to send RESUME "
2163 case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
2164 case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
2165 vchiq_platform_handle_timeout(state);
2173 DEBUG_TRACE(SLOT_HANDLER_LINE);
2174 parse_rx_slots(state);
2179 /* Called by the recycle thread */
2181 recycle_func(void *v)
2183 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
2184 VCHIQ_SHARED_STATE_T *local = state->local;
2188 length = sizeof(*found) * BITSET_SIZE(VCHIQ_MAX_SERVICES);
2190 found = kmalloc_array(BITSET_SIZE(VCHIQ_MAX_SERVICES), sizeof(*found),
2196 remote_event_wait(state, &local->recycle);
2198 process_free_queue(state, found, length);
2203 /* Called by the sync thread */
2207 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
2208 VCHIQ_SHARED_STATE_T *local = state->local;
2209 VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
2210 state->remote->slot_sync);
2213 VCHIQ_SERVICE_T *service;
2216 unsigned int localport, remoteport;
2218 remote_event_wait(state, &local->sync_trigger);
2222 msgid = header->msgid;
2223 size = header->size;
2224 type = VCHIQ_MSG_TYPE(msgid);
2225 localport = VCHIQ_MSG_DSTPORT(msgid);
2226 remoteport = VCHIQ_MSG_SRCPORT(msgid);
2228 service = find_service_by_port(state, localport);
2231 vchiq_log_error(vchiq_sync_log_level,
2232 "%d: sf %s@%pK (%d->%d) - invalid/closed service %d",
2233 state->id, msg_type_str(type),
2234 header, remoteport, localport, localport);
2235 release_message_sync(state, header);
2239 if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
2242 svc_fourcc = service
2243 ? service->base.fourcc
2244 : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
2245 vchiq_log_trace(vchiq_sync_log_level,
2246 "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
2248 VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
2249 remoteport, localport, size);
2251 vchiq_log_dump_mem("Rcvd", 0, header->data,
2256 case VCHIQ_MSG_OPENACK:
2257 if (size >= sizeof(struct vchiq_openack_payload)) {
2258 const struct vchiq_openack_payload *payload =
2259 (struct vchiq_openack_payload *)
2261 service->peer_version = payload->version;
2263 vchiq_log_info(vchiq_sync_log_level,
2264 "%d: sf OPENACK@%pK,%x (%d->%d) v:%d",
2265 state->id, header, size, remoteport, localport,
2266 service->peer_version);
2267 if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
2268 service->remoteport = remoteport;
2269 vchiq_set_service_state(service,
2270 VCHIQ_SRVSTATE_OPENSYNC);
2272 up(&service->remove_event);
2274 release_message_sync(state, header);
2277 case VCHIQ_MSG_DATA:
2278 vchiq_log_trace(vchiq_sync_log_level,
2279 "%d: sf DATA@%pK,%x (%d->%d)",
2280 state->id, header, size, remoteport, localport);
2282 if ((service->remoteport == remoteport) &&
2283 (service->srvstate ==
2284 VCHIQ_SRVSTATE_OPENSYNC)) {
2285 if (make_service_callback(service,
2286 VCHIQ_MESSAGE_AVAILABLE, header,
2287 NULL) == VCHIQ_RETRY)
2288 vchiq_log_error(vchiq_sync_log_level,
2289 "synchronous callback to "
2290 "service %d returns "
2297 vchiq_log_error(vchiq_sync_log_level,
2298 "%d: sf unexpected msgid %x@%pK,%x",
2299 state->id, msgid, header, size);
2300 release_message_sync(state, header);
2304 unlock_service(service);
2311 init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
2313 queue->local_insert = 0;
2314 queue->remote_insert = 0;
2316 queue->remote_notify = 0;
2321 get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
2323 return conn_state_names[conn_state];
2327 vchiq_init_slots(void *mem_base, int mem_size)
2330 (int)((VCHIQ_SLOT_SIZE - (long)mem_base) & VCHIQ_SLOT_MASK);
2331 VCHIQ_SLOT_ZERO_T *slot_zero =
2332 (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
2333 int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
2334 int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
2336 /* Ensure there is enough memory to run an absolutely minimum system */
2337 num_slots -= first_data_slot;
2339 if (num_slots < 4) {
2340 vchiq_log_error(vchiq_core_log_level,
2341 "%s - insufficient memory %x bytes",
2342 __func__, mem_size);
2346 memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
2348 slot_zero->magic = VCHIQ_MAGIC;
2349 slot_zero->version = VCHIQ_VERSION;
2350 slot_zero->version_min = VCHIQ_VERSION_MIN;
2351 slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
2352 slot_zero->slot_size = VCHIQ_SLOT_SIZE;
2353 slot_zero->max_slots = VCHIQ_MAX_SLOTS;
2354 slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
2356 slot_zero->master.slot_sync = first_data_slot;
2357 slot_zero->master.slot_first = first_data_slot + 1;
2358 slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
2359 slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
2360 slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
2361 slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
2367 vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
2370 VCHIQ_SHARED_STATE_T *local;
2371 VCHIQ_SHARED_STATE_T *remote;
2372 VCHIQ_STATUS_T status;
2373 char threadname[16];
2376 vchiq_log_warning(vchiq_core_log_level,
2377 "%s: slot_zero = %pK, is_master = %d",
2378 __func__, slot_zero, is_master);
2380 if (vchiq_states[0]) {
2381 pr_err("%s: VCHIQ state already initialized\n", __func__);
2385 /* Check the input configuration */
2387 if (slot_zero->magic != VCHIQ_MAGIC) {
2388 vchiq_loud_error_header();
2389 vchiq_loud_error("Invalid VCHIQ magic value found.");
2390 vchiq_loud_error("slot_zero=%pK: magic=%x (expected %x)",
2391 slot_zero, slot_zero->magic, VCHIQ_MAGIC);
2392 vchiq_loud_error_footer();
2396 if (slot_zero->version < VCHIQ_VERSION_MIN) {
2397 vchiq_loud_error_header();
2398 vchiq_loud_error("Incompatible VCHIQ versions found.");
2399 vchiq_loud_error("slot_zero=%pK: VideoCore version=%d (minimum %d)",
2400 slot_zero, slot_zero->version, VCHIQ_VERSION_MIN);
2401 vchiq_loud_error("Restart with a newer VideoCore image.");
2402 vchiq_loud_error_footer();
2406 if (VCHIQ_VERSION < slot_zero->version_min) {
2407 vchiq_loud_error_header();
2408 vchiq_loud_error("Incompatible VCHIQ versions found.");
2409 vchiq_loud_error("slot_zero=%pK: version=%d (VideoCore minimum %d)",
2410 slot_zero, VCHIQ_VERSION, slot_zero->version_min);
2411 vchiq_loud_error("Restart with a newer kernel.");
2412 vchiq_loud_error_footer();
2416 if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
2417 (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
2418 (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
2419 (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
2420 vchiq_loud_error_header();
2421 if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
2422 vchiq_loud_error("slot_zero=%pK: slot_zero_size=%d (expected %d)",
2423 slot_zero, slot_zero->slot_zero_size,
2424 (int)sizeof(VCHIQ_SLOT_ZERO_T));
2425 if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
2426 vchiq_loud_error("slot_zero=%pK: slot_size=%d (expected %d)",
2427 slot_zero, slot_zero->slot_size,
2429 if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
2430 vchiq_loud_error("slot_zero=%pK: max_slots=%d (expected %d)",
2431 slot_zero, slot_zero->max_slots,
2433 if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
2434 vchiq_loud_error("slot_zero=%pK: max_slots_per_side=%d (expected %d)",
2435 slot_zero, slot_zero->max_slots_per_side,
2436 VCHIQ_MAX_SLOTS_PER_SIDE);
2437 vchiq_loud_error_footer();
2441 if (VCHIQ_VERSION < slot_zero->version)
2442 slot_zero->version = VCHIQ_VERSION;
2445 local = &slot_zero->master;
2446 remote = &slot_zero->slave;
2448 local = &slot_zero->slave;
2449 remote = &slot_zero->master;
2452 if (local->initialised) {
2453 vchiq_loud_error_header();
2454 if (remote->initialised)
2455 vchiq_loud_error("local state has already been "
2458 vchiq_loud_error("master/slave mismatch - two %ss",
2459 is_master ? "master" : "slave");
2460 vchiq_loud_error_footer();
2464 memset(state, 0, sizeof(VCHIQ_STATE_T));
2466 state->is_master = is_master;
2469 initialize shared state pointers
2472 state->local = local;
2473 state->remote = remote;
2474 state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
2477 initialize events and mutexes
2480 sema_init(&state->connect, 0);
2481 mutex_init(&state->mutex);
2482 sema_init(&state->trigger_event, 0);
2483 sema_init(&state->recycle_event, 0);
2484 sema_init(&state->sync_trigger_event, 0);
2485 sema_init(&state->sync_release_event, 0);
2487 mutex_init(&state->slot_mutex);
2488 mutex_init(&state->recycle_mutex);
2489 mutex_init(&state->sync_mutex);
2490 mutex_init(&state->bulk_transfer_mutex);
2492 sema_init(&state->slot_available_event, 0);
2493 sema_init(&state->slot_remove_event, 0);
2494 sema_init(&state->data_quota_event, 0);
2496 state->slot_queue_available = 0;
2498 for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
2499 VCHIQ_SERVICE_QUOTA_T *service_quota =
2500 &state->service_quotas[i];
2501 sema_init(&service_quota->quota_event, 0);
2504 for (i = local->slot_first; i <= local->slot_last; i++) {
2505 local->slot_queue[state->slot_queue_available++] = i;
2506 up(&state->slot_available_event);
2509 state->default_slot_quota = state->slot_queue_available/2;
2510 state->default_message_quota =
2511 min((unsigned short)(state->default_slot_quota * 256),
2512 (unsigned short)~0);
2514 state->previous_data_index = -1;
2515 state->data_use_count = 0;
2516 state->data_quota = state->slot_queue_available - 1;
2518 local->trigger.event = offsetof(VCHIQ_STATE_T, trigger_event);
2519 remote_event_create(state, &local->trigger);
2522 local->recycle.event = offsetof(VCHIQ_STATE_T, recycle_event);
2523 remote_event_create(state, &local->recycle);
2524 local->slot_queue_recycle = state->slot_queue_available;
2526 local->sync_trigger.event = offsetof(VCHIQ_STATE_T, sync_trigger_event);
2527 remote_event_create(state, &local->sync_trigger);
2529 local->sync_release.event = offsetof(VCHIQ_STATE_T, sync_release_event);
2530 remote_event_create(state, &local->sync_release);
2532 /* At start-of-day, the slot is empty and available */
2533 ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
2534 = VCHIQ_MSGID_PADDING;
2535 remote_event_signal_local(state, &local->sync_release);
2537 local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
2539 status = vchiq_platform_init_state(state);
2542 bring up slot handler thread
2544 snprintf(threadname, sizeof(threadname), "vchiq-slot/%d", state->id);
2545 state->slot_handler_thread = kthread_create(&slot_handler_func,
2549 if (IS_ERR(state->slot_handler_thread)) {
2550 vchiq_loud_error_header();
2551 vchiq_loud_error("couldn't create thread %s", threadname);
2552 vchiq_loud_error_footer();
2555 set_user_nice(state->slot_handler_thread, -19);
2557 snprintf(threadname, sizeof(threadname), "vchiq-recy/%d", state->id);
2558 state->recycle_thread = kthread_create(&recycle_func,
2561 if (IS_ERR(state->recycle_thread)) {
2562 vchiq_loud_error_header();
2563 vchiq_loud_error("couldn't create thread %s", threadname);
2564 vchiq_loud_error_footer();
2565 goto fail_free_handler_thread;
2567 set_user_nice(state->recycle_thread, -19);
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();
2577 goto fail_free_recycle_thread;
2579 set_user_nice(state->sync_thread, -20);
2581 wake_up_process(state->slot_handler_thread);
2582 wake_up_process(state->recycle_thread);
2583 wake_up_process(state->sync_thread);
2585 vchiq_states[0] = state;
2587 /* Indicate readiness to the other side */
2588 local->initialised = 1;
2592 fail_free_recycle_thread:
2593 kthread_stop(state->recycle_thread);
2594 fail_free_handler_thread:
2595 kthread_stop(state->slot_handler_thread);
2600 /* Called from application thread when a client or server service is created. */
2602 vchiq_add_service_internal(VCHIQ_STATE_T *state,
2603 const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
2604 VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
2606 VCHIQ_SERVICE_T *service;
2607 VCHIQ_SERVICE_T **pservice = NULL;
2608 VCHIQ_SERVICE_QUOTA_T *service_quota;
2611 service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
2615 service->base.fourcc = params->fourcc;
2616 service->base.callback = params->callback;
2617 service->base.userdata = params->userdata;
2618 service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
2619 service->ref_count = 1;
2620 service->srvstate = VCHIQ_SRVSTATE_FREE;
2621 service->userdata_term = userdata_term;
2622 service->localport = VCHIQ_PORT_FREE;
2623 service->remoteport = VCHIQ_PORT_FREE;
2625 service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
2626 VCHIQ_FOURCC_INVALID : params->fourcc;
2627 service->client_id = 0;
2628 service->auto_close = 1;
2630 service->closing = 0;
2632 atomic_set(&service->poll_flags, 0);
2633 service->version = params->version;
2634 service->version_min = params->version_min;
2635 service->state = state;
2636 service->instance = instance;
2637 service->service_use_count = 0;
2638 init_bulk_queue(&service->bulk_tx);
2639 init_bulk_queue(&service->bulk_rx);
2640 sema_init(&service->remove_event, 0);
2641 sema_init(&service->bulk_remove_event, 0);
2642 mutex_init(&service->bulk_mutex);
2643 memset(&service->stats, 0, sizeof(service->stats));
2645 /* Although it is perfectly possible to use service_spinlock
2646 ** to protect the creation of services, it is overkill as it
2647 ** disables interrupts while the array is searched.
2648 ** The only danger is of another thread trying to create a
2649 ** service - service deletion is safe.
2650 ** Therefore it is preferable to use state->mutex which,
2651 ** although slower to claim, doesn't block interrupts while
2655 mutex_lock(&state->mutex);
2657 /* Prepare to use a previously unused service */
2658 if (state->unused_service < VCHIQ_MAX_SERVICES)
2659 pservice = &state->services[state->unused_service];
2661 if (srvstate == VCHIQ_SRVSTATE_OPENING) {
2662 for (i = 0; i < state->unused_service; i++) {
2663 VCHIQ_SERVICE_T *srv = state->services[i];
2666 pservice = &state->services[i];
2671 for (i = (state->unused_service - 1); i >= 0; i--) {
2672 VCHIQ_SERVICE_T *srv = state->services[i];
2675 pservice = &state->services[i];
2676 else if ((srv->public_fourcc == params->fourcc)
2677 && ((srv->instance != instance) ||
2678 (srv->base.callback !=
2679 params->callback))) {
2680 /* There is another server using this
2681 ** fourcc which doesn't match. */
2689 service->localport = (pservice - state->services);
2691 handle_seq = VCHIQ_MAX_STATES *
2693 service->handle = handle_seq |
2694 (state->id * VCHIQ_MAX_SERVICES) |
2696 handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
2697 *pservice = service;
2698 if (pservice == &state->services[state->unused_service])
2699 state->unused_service++;
2702 mutex_unlock(&state->mutex);
2709 service_quota = &state->service_quotas[service->localport];
2710 service_quota->slot_quota = state->default_slot_quota;
2711 service_quota->message_quota = state->default_message_quota;
2712 if (service_quota->slot_use_count == 0)
2713 service_quota->previous_tx_index =
2714 SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
2717 /* Bring this service online */
2718 vchiq_set_service_state(service, srvstate);
2720 vchiq_log_info(vchiq_core_msg_log_level,
2721 "%s Service %c%c%c%c SrcPort:%d",
2722 (srvstate == VCHIQ_SRVSTATE_OPENING)
2724 VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
2725 service->localport);
2727 /* Don't unlock the service - leave it with a ref_count of 1. */
2733 vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
2735 struct vchiq_open_payload payload = {
2736 service->base.fourcc,
2739 service->version_min
2741 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2743 service->client_id = client_id;
2744 vchiq_use_service_internal(service);
2745 status = queue_message(service->state,
2747 VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN,
2750 memcpy_copy_callback,
2753 QMFLAGS_IS_BLOCKING);
2754 if (status == VCHIQ_SUCCESS) {
2755 /* Wait for the ACK/NAK */
2756 if (down_interruptible(&service->remove_event) != 0) {
2757 status = VCHIQ_RETRY;
2758 vchiq_release_service_internal(service);
2759 } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
2760 (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
2761 if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
2762 vchiq_log_error(vchiq_core_log_level,
2763 "%d: osi - srvstate = %s (ref %d)",
2765 srvstate_names[service->srvstate],
2766 service->ref_count);
2767 status = VCHIQ_ERROR;
2768 VCHIQ_SERVICE_STATS_INC(service, error_count);
2769 vchiq_release_service_internal(service);
2776 release_service_messages(VCHIQ_SERVICE_T *service)
2778 VCHIQ_STATE_T *state = service->state;
2779 int slot_last = state->remote->slot_last;
2782 /* Release any claimed messages aimed at this service */
2784 if (service->sync) {
2785 VCHIQ_HEADER_T *header =
2786 (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
2787 state->remote->slot_sync);
2788 if (VCHIQ_MSG_DSTPORT(header->msgid) == service->localport)
2789 release_message_sync(state, header);
2794 for (i = state->remote->slot_first; i <= slot_last; i++) {
2795 VCHIQ_SLOT_INFO_T *slot_info =
2796 SLOT_INFO_FROM_INDEX(state, i);
2797 if (slot_info->release_count != slot_info->use_count) {
2799 (char *)SLOT_DATA_FROM_INDEX(state, i);
2800 unsigned int pos, end;
2802 end = VCHIQ_SLOT_SIZE;
2803 if (data == state->rx_data)
2804 /* This buffer is still being read from - stop
2805 ** at the current read position */
2806 end = state->rx_pos & VCHIQ_SLOT_MASK;
2811 VCHIQ_HEADER_T *header =
2812 (VCHIQ_HEADER_T *)(data + pos);
2813 int msgid = header->msgid;
2814 int port = VCHIQ_MSG_DSTPORT(msgid);
2816 if ((port == service->localport) &&
2817 (msgid & VCHIQ_MSGID_CLAIMED)) {
2818 vchiq_log_info(vchiq_core_log_level,
2819 " fsi - hdr %pK", header);
2820 release_slot(state, slot_info, header,
2823 pos += calc_stride(header->size);
2824 if (pos > VCHIQ_SLOT_SIZE) {
2825 vchiq_log_error(vchiq_core_log_level,
2826 "fsi - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
2828 header->msgid, header->size);
2829 WARN(1, "invalid slot position\n");
2837 do_abort_bulks(VCHIQ_SERVICE_T *service)
2839 VCHIQ_STATUS_T status;
2841 /* Abort any outstanding bulk transfers */
2842 if (mutex_lock_killable(&service->bulk_mutex) != 0)
2844 abort_outstanding_bulks(service, &service->bulk_tx);
2845 abort_outstanding_bulks(service, &service->bulk_rx);
2846 mutex_unlock(&service->bulk_mutex);
2848 status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
2849 if (status == VCHIQ_SUCCESS)
2850 status = notify_bulks(service, &service->bulk_rx,
2852 return (status == VCHIQ_SUCCESS);
2855 static VCHIQ_STATUS_T
2856 close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
2858 VCHIQ_STATUS_T status;
2859 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2862 switch (service->srvstate) {
2863 case VCHIQ_SRVSTATE_OPEN:
2864 case VCHIQ_SRVSTATE_CLOSESENT:
2865 case VCHIQ_SRVSTATE_CLOSERECVD:
2867 if (service->auto_close) {
2868 service->client_id = 0;
2869 service->remoteport = VCHIQ_PORT_FREE;
2870 newstate = VCHIQ_SRVSTATE_LISTENING;
2872 newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
2874 newstate = VCHIQ_SRVSTATE_CLOSED;
2875 vchiq_set_service_state(service, newstate);
2877 case VCHIQ_SRVSTATE_LISTENING:
2880 vchiq_log_error(vchiq_core_log_level,
2881 "%s(%x) called in state %s", __func__,
2882 service->handle, srvstate_names[service->srvstate]);
2883 WARN(1, "%s in unexpected state\n", __func__);
2887 status = make_service_callback(service,
2888 VCHIQ_SERVICE_CLOSED, NULL, NULL);
2890 if (status != VCHIQ_RETRY) {
2891 int uc = service->service_use_count;
2893 /* Complete the close process */
2894 for (i = 0; i < uc; i++)
2895 /* cater for cases where close is forced and the
2896 ** client may not close all it's handles */
2897 vchiq_release_service_internal(service);
2899 service->client_id = 0;
2900 service->remoteport = VCHIQ_PORT_FREE;
2902 if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
2903 vchiq_free_service_internal(service);
2904 else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
2906 service->closing = 0;
2908 up(&service->remove_event);
2911 vchiq_set_service_state(service, failstate);
2916 /* Called by the slot handler */
2918 vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
2920 VCHIQ_STATE_T *state = service->state;
2921 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2922 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2924 vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
2925 service->state->id, service->localport, close_recvd,
2926 srvstate_names[service->srvstate]);
2928 switch (service->srvstate) {
2929 case VCHIQ_SRVSTATE_CLOSED:
2930 case VCHIQ_SRVSTATE_HIDDEN:
2931 case VCHIQ_SRVSTATE_LISTENING:
2932 case VCHIQ_SRVSTATE_CLOSEWAIT:
2934 vchiq_log_error(vchiq_core_log_level,
2937 __func__, srvstate_names[service->srvstate]);
2938 else if (is_server) {
2939 if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
2940 status = VCHIQ_ERROR;
2942 service->client_id = 0;
2943 service->remoteport = VCHIQ_PORT_FREE;
2944 if (service->srvstate ==
2945 VCHIQ_SRVSTATE_CLOSEWAIT)
2946 vchiq_set_service_state(service,
2947 VCHIQ_SRVSTATE_LISTENING);
2949 up(&service->remove_event);
2951 vchiq_free_service_internal(service);
2953 case VCHIQ_SRVSTATE_OPENING:
2955 /* The open was rejected - tell the user */
2956 vchiq_set_service_state(service,
2957 VCHIQ_SRVSTATE_CLOSEWAIT);
2958 up(&service->remove_event);
2960 /* Shutdown mid-open - let the other side know */
2961 status = queue_message(state, service,
2965 VCHIQ_MSG_DSTPORT(service->remoteport)),
2970 case VCHIQ_SRVSTATE_OPENSYNC:
2971 mutex_lock(&state->sync_mutex);
2973 case VCHIQ_SRVSTATE_OPEN:
2974 if (state->is_master || close_recvd) {
2975 if (!do_abort_bulks(service))
2976 status = VCHIQ_RETRY;
2979 release_service_messages(service);
2981 if (status == VCHIQ_SUCCESS)
2982 status = queue_message(state, service,
2986 VCHIQ_MSG_DSTPORT(service->remoteport)),
2987 NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK);
2989 if (status == VCHIQ_SUCCESS) {
2991 /* Change the state while the mutex is
2993 vchiq_set_service_state(service,
2994 VCHIQ_SRVSTATE_CLOSESENT);
2995 mutex_unlock(&state->slot_mutex);
2997 mutex_unlock(&state->sync_mutex);
3000 } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
3001 mutex_unlock(&state->sync_mutex);
3006 /* Change the state while the mutex is still held */
3007 vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSERECVD);
3008 mutex_unlock(&state->slot_mutex);
3010 mutex_unlock(&state->sync_mutex);
3012 status = close_service_complete(service,
3013 VCHIQ_SRVSTATE_CLOSERECVD);
3016 case VCHIQ_SRVSTATE_CLOSESENT:
3018 /* This happens when a process is killed mid-close */
3021 if (!state->is_master) {
3022 if (!do_abort_bulks(service)) {
3023 status = VCHIQ_RETRY;
3028 if (status == VCHIQ_SUCCESS)
3029 status = close_service_complete(service,
3030 VCHIQ_SRVSTATE_CLOSERECVD);
3033 case VCHIQ_SRVSTATE_CLOSERECVD:
3034 if (!close_recvd && is_server)
3035 /* Force into LISTENING mode */
3036 vchiq_set_service_state(service,
3037 VCHIQ_SRVSTATE_LISTENING);
3038 status = close_service_complete(service,
3039 VCHIQ_SRVSTATE_CLOSERECVD);
3043 vchiq_log_error(vchiq_core_log_level,
3044 "%s(%d) called in state %s", __func__,
3045 close_recvd, srvstate_names[service->srvstate]);
3052 /* Called from the application process upon process death */
3054 vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
3056 VCHIQ_STATE_T *state = service->state;
3058 vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
3059 state->id, service->localport, service->remoteport);
3061 mark_service_closing(service);
3063 /* Mark the service for removal by the slot handler */
3064 request_poll(state, service, VCHIQ_POLL_REMOVE);
3067 /* Called from the slot handler */
3069 vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
3071 VCHIQ_STATE_T *state = service->state;
3073 vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
3074 state->id, service->localport);
3076 switch (service->srvstate) {
3077 case VCHIQ_SRVSTATE_OPENING:
3078 case VCHIQ_SRVSTATE_CLOSED:
3079 case VCHIQ_SRVSTATE_HIDDEN:
3080 case VCHIQ_SRVSTATE_LISTENING:
3081 case VCHIQ_SRVSTATE_CLOSEWAIT:
3084 vchiq_log_error(vchiq_core_log_level,
3085 "%d: fsi - (%d) in state %s",
3086 state->id, service->localport,
3087 srvstate_names[service->srvstate]);
3091 vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
3093 up(&service->remove_event);
3095 /* Release the initial lock */
3096 unlock_service(service);
3100 vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
3102 VCHIQ_SERVICE_T *service;
3105 /* Find all services registered to this client and enable them. */
3107 while ((service = next_service_by_instance(state, instance,
3109 if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
3110 vchiq_set_service_state(service,
3111 VCHIQ_SRVSTATE_LISTENING);
3112 unlock_service(service);
3115 if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
3116 if (queue_message(state, NULL,
3117 VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, NULL,
3118 0, QMFLAGS_IS_BLOCKING) == VCHIQ_RETRY)
3121 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
3124 if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
3125 if (down_interruptible(&state->connect) != 0)
3128 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
3129 up(&state->connect);
3132 return VCHIQ_SUCCESS;
3136 vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
3138 VCHIQ_SERVICE_T *service;
3141 /* Find all services registered to this client and enable them. */
3143 while ((service = next_service_by_instance(state, instance,
3145 (void)vchiq_remove_service(service->handle);
3146 unlock_service(service);
3149 return VCHIQ_SUCCESS;
3153 vchiq_pause_internal(VCHIQ_STATE_T *state)
3155 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3157 switch (state->conn_state) {
3158 case VCHIQ_CONNSTATE_CONNECTED:
3159 /* Request a pause */
3160 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
3161 request_poll(state, NULL, 0);
3164 vchiq_log_error(vchiq_core_log_level,
3166 __func__, conn_state_names[state->conn_state]);
3167 status = VCHIQ_ERROR;
3168 VCHIQ_STATS_INC(state, error_count);
3176 vchiq_resume_internal(VCHIQ_STATE_T *state)
3178 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3180 if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
3181 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
3182 request_poll(state, NULL, 0);
3184 status = VCHIQ_ERROR;
3185 VCHIQ_STATS_INC(state, error_count);
3192 vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
3194 /* Unregister the service */
3195 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3196 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3201 vchiq_log_info(vchiq_core_log_level,
3202 "%d: close_service:%d",
3203 service->state->id, service->localport);
3205 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3206 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
3207 (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
3208 unlock_service(service);
3212 mark_service_closing(service);
3214 if (current == service->state->slot_handler_thread) {
3215 status = vchiq_close_service_internal(service,
3217 WARN_ON(status == VCHIQ_RETRY);
3219 /* Mark the service for termination by the slot handler */
3220 request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
3224 if (down_interruptible(&service->remove_event) != 0) {
3225 status = VCHIQ_RETRY;
3229 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3230 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
3231 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
3234 vchiq_log_warning(vchiq_core_log_level,
3235 "%d: close_service:%d - waiting in state %s",
3236 service->state->id, service->localport,
3237 srvstate_names[service->srvstate]);
3240 if ((status == VCHIQ_SUCCESS) &&
3241 (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
3242 (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
3243 status = VCHIQ_ERROR;
3245 unlock_service(service);
3251 vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
3253 /* Unregister the service */
3254 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3255 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3260 vchiq_log_info(vchiq_core_log_level,
3261 "%d: remove_service:%d",
3262 service->state->id, service->localport);
3264 if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
3265 unlock_service(service);
3269 mark_service_closing(service);
3271 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3272 (current == service->state->slot_handler_thread)) {
3273 /* Make it look like a client, because it must be removed and
3274 not left in the LISTENING state. */
3275 service->public_fourcc = VCHIQ_FOURCC_INVALID;
3277 status = vchiq_close_service_internal(service,
3279 WARN_ON(status == VCHIQ_RETRY);
3281 /* Mark the service for removal by the slot handler */
3282 request_poll(service->state, service, VCHIQ_POLL_REMOVE);
3285 if (down_interruptible(&service->remove_event) != 0) {
3286 status = VCHIQ_RETRY;
3290 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3291 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
3294 vchiq_log_warning(vchiq_core_log_level,
3295 "%d: remove_service:%d - waiting in state %s",
3296 service->state->id, service->localport,
3297 srvstate_names[service->srvstate]);
3300 if ((status == VCHIQ_SUCCESS) &&
3301 (service->srvstate != VCHIQ_SRVSTATE_FREE))
3302 status = VCHIQ_ERROR;
3304 unlock_service(service);
3309 /* This function may be called by kernel threads or user threads.
3310 * User threads may receive VCHIQ_RETRY to indicate that a signal has been
3311 * received and the call should be retried after being returned to user
3313 * When called in blocking mode, the userdata field points to a bulk_waiter
3317 vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
3318 VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
3319 VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
3321 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3322 VCHIQ_BULK_QUEUE_T *queue;
3324 VCHIQ_STATE_T *state;
3325 struct bulk_waiter *bulk_waiter = NULL;
3326 const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
3327 const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
3328 VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
3329 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3332 (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
3333 ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
3334 (vchiq_check_service(service) != VCHIQ_SUCCESS))
3338 case VCHIQ_BULK_MODE_NOCALLBACK:
3339 case VCHIQ_BULK_MODE_CALLBACK:
3341 case VCHIQ_BULK_MODE_BLOCKING:
3342 bulk_waiter = (struct bulk_waiter *)userdata;
3343 sema_init(&bulk_waiter->event, 0);
3344 bulk_waiter->actual = 0;
3345 bulk_waiter->bulk = NULL;
3347 case VCHIQ_BULK_MODE_WAITING:
3348 bulk_waiter = (struct bulk_waiter *)userdata;
3349 bulk = bulk_waiter->bulk;
3355 state = service->state;
3357 queue = (dir == VCHIQ_BULK_TRANSMIT) ?
3358 &service->bulk_tx : &service->bulk_rx;
3360 if (mutex_lock_killable(&service->bulk_mutex) != 0) {
3361 status = VCHIQ_RETRY;
3365 if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
3366 VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
3368 mutex_unlock(&service->bulk_mutex);
3369 if (down_interruptible(&service->bulk_remove_event)
3371 status = VCHIQ_RETRY;
3374 if (mutex_lock_killable(&service->bulk_mutex)
3376 status = VCHIQ_RETRY;
3379 } while (queue->local_insert == queue->remove +
3380 VCHIQ_NUM_SERVICE_BULKS);
3383 bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
3387 bulk->userdata = userdata;
3389 bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
3391 if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
3393 goto unlock_error_exit;
3397 vchiq_log_info(vchiq_core_log_level,
3398 "%d: bt (%d->%d) %cx %x@%pK %pK",
3399 state->id, service->localport, service->remoteport, dir_char,
3400 size, bulk->data, userdata);
3402 /* The slot mutex must be held when the service is being closed, so
3403 claim it here to ensure that isn't happening */
3404 if (mutex_lock_killable(&state->slot_mutex) != 0) {
3405 status = VCHIQ_RETRY;
3406 goto cancel_bulk_error_exit;
3409 if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
3410 goto unlock_both_error_exit;
3412 if (state->is_master) {
3413 queue->local_insert++;
3414 if (resolve_bulks(service, queue))
3415 request_poll(state, service,
3416 (dir == VCHIQ_BULK_TRANSMIT) ?
3417 VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
3419 int payload[2] = { (int)(long)bulk->data, bulk->size };
3421 status = queue_message(state,
3423 VCHIQ_MAKE_MSG(dir_msgtype,
3425 service->remoteport),
3426 memcpy_copy_callback,
3429 QMFLAGS_IS_BLOCKING |
3430 QMFLAGS_NO_MUTEX_LOCK |
3431 QMFLAGS_NO_MUTEX_UNLOCK);
3432 if (status != VCHIQ_SUCCESS) {
3433 goto unlock_both_error_exit;
3435 queue->local_insert++;
3438 mutex_unlock(&state->slot_mutex);
3439 mutex_unlock(&service->bulk_mutex);
3441 vchiq_log_trace(vchiq_core_log_level,
3442 "%d: bt:%d %cx li=%x ri=%x p=%x",
3444 service->localport, dir_char,
3445 queue->local_insert, queue->remote_insert, queue->process);
3448 unlock_service(service);
3450 status = VCHIQ_SUCCESS;
3453 bulk_waiter->bulk = bulk;
3454 if (down_interruptible(&bulk_waiter->event) != 0)
3455 status = VCHIQ_RETRY;
3456 else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
3457 status = VCHIQ_ERROR;
3462 unlock_both_error_exit:
3463 mutex_unlock(&state->slot_mutex);
3464 cancel_bulk_error_exit:
3465 vchiq_complete_bulk(bulk);
3467 mutex_unlock(&service->bulk_mutex);
3471 unlock_service(service);
3476 vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
3477 ssize_t (*copy_callback)(void *context, void *dest,
3478 size_t offset, size_t maxsize),
3482 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3483 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3486 (vchiq_check_service(service) != VCHIQ_SUCCESS))
3490 VCHIQ_SERVICE_STATS_INC(service, error_count);
3495 if (size > VCHIQ_MAX_MSG_SIZE) {
3496 VCHIQ_SERVICE_STATS_INC(service, error_count);
3500 switch (service->srvstate) {
3501 case VCHIQ_SRVSTATE_OPEN:
3502 status = queue_message(service->state, service,
3503 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3505 service->remoteport),
3506 copy_callback, context, size, 1);
3508 case VCHIQ_SRVSTATE_OPENSYNC:
3509 status = queue_message_sync(service->state, service,
3510 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3512 service->remoteport),
3513 copy_callback, context, size, 1);
3516 status = VCHIQ_ERROR;
3522 unlock_service(service);
3528 vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
3530 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3531 VCHIQ_SHARED_STATE_T *remote;
3532 VCHIQ_STATE_T *state;
3538 state = service->state;
3539 remote = state->remote;
3541 slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
3543 if ((slot_index >= remote->slot_first) &&
3544 (slot_index <= remote->slot_last)) {
3545 int msgid = header->msgid;
3547 if (msgid & VCHIQ_MSGID_CLAIMED) {
3548 VCHIQ_SLOT_INFO_T *slot_info =
3549 SLOT_INFO_FROM_INDEX(state, slot_index);
3551 release_slot(state, slot_info, header, service);
3553 } else if (slot_index == remote->slot_sync)
3554 release_message_sync(state, header);
3556 unlock_service(service);
3560 release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
3562 header->msgid = VCHIQ_MSGID_PADDING;
3564 remote_event_signal(&state->remote->sync_release);
3568 vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
3570 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3571 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3574 (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
3577 *peer_version = service->peer_version;
3578 status = VCHIQ_SUCCESS;
3582 unlock_service(service);
3587 vchiq_get_config(VCHIQ_INSTANCE_T instance,
3588 int config_size, VCHIQ_CONFIG_T *pconfig)
3590 VCHIQ_CONFIG_T config;
3594 config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
3595 config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
3596 config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
3597 config.max_services = VCHIQ_MAX_SERVICES;
3598 config.version = VCHIQ_VERSION;
3599 config.version_min = VCHIQ_VERSION_MIN;
3601 if (config_size > sizeof(VCHIQ_CONFIG_T))
3604 memcpy(pconfig, &config,
3605 min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
3607 return VCHIQ_SUCCESS;
3611 vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
3612 VCHIQ_SERVICE_OPTION_T option, int value)
3614 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3615 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3619 case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
3620 service->auto_close = value;
3621 status = VCHIQ_SUCCESS;
3624 case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
3625 VCHIQ_SERVICE_QUOTA_T *service_quota =
3626 &service->state->service_quotas[
3627 service->localport];
3629 value = service->state->default_slot_quota;
3630 if ((value >= service_quota->slot_use_count) &&
3631 (value < (unsigned short)~0)) {
3632 service_quota->slot_quota = value;
3633 if ((value >= service_quota->slot_use_count) &&
3634 (service_quota->message_quota >=
3635 service_quota->message_use_count)) {
3636 /* Signal the service that it may have
3637 ** dropped below its quota */
3638 up(&service_quota->quota_event);
3640 status = VCHIQ_SUCCESS;
3644 case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
3645 VCHIQ_SERVICE_QUOTA_T *service_quota =
3646 &service->state->service_quotas[
3647 service->localport];
3649 value = service->state->default_message_quota;
3650 if ((value >= service_quota->message_use_count) &&
3651 (value < (unsigned short)~0)) {
3652 service_quota->message_quota = value;
3654 service_quota->message_use_count) &&
3655 (service_quota->slot_quota >=
3656 service_quota->slot_use_count))
3657 /* Signal the service that it may have
3658 ** dropped below its quota */
3659 up(&service_quota->quota_event);
3660 status = VCHIQ_SUCCESS;
3664 case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
3665 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3666 (service->srvstate ==
3667 VCHIQ_SRVSTATE_LISTENING)) {
3668 service->sync = value;
3669 status = VCHIQ_SUCCESS;
3673 case VCHIQ_SERVICE_OPTION_TRACE:
3674 service->trace = value;
3675 status = VCHIQ_SUCCESS;
3681 unlock_service(service);
3688 vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
3689 VCHIQ_SHARED_STATE_T *shared, const char *label)
3691 static const char *const debug_names[] = {
3693 "SLOT_HANDLER_COUNT",
3694 "SLOT_HANDLER_LINE",
3698 "AWAIT_COMPLETION_LINE",
3699 "DEQUEUE_MESSAGE_LINE",
3700 "SERVICE_CALLBACK_LINE",
3701 "MSG_QUEUE_FULL_COUNT",
3702 "COMPLETION_QUEUE_FULL_COUNT"
3708 len = snprintf(buf, sizeof(buf),
3709 " %s: slots %d-%d tx_pos=%x recycle=%x",
3710 label, shared->slot_first, shared->slot_last,
3711 shared->tx_pos, shared->slot_queue_recycle);
3712 vchiq_dump(dump_context, buf, len + 1);
3714 len = snprintf(buf, sizeof(buf),
3716 vchiq_dump(dump_context, buf, len + 1);
3718 for (i = shared->slot_first; i <= shared->slot_last; i++) {
3719 VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
3720 if (slot_info.use_count != slot_info.release_count) {
3721 len = snprintf(buf, sizeof(buf),
3722 " %d: %d/%d", i, slot_info.use_count,
3723 slot_info.release_count);
3724 vchiq_dump(dump_context, buf, len + 1);
3728 for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
3729 len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
3730 debug_names[i], shared->debug[i], shared->debug[i]);
3731 vchiq_dump(dump_context, buf, len + 1);
3736 vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
3742 len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
3743 conn_state_names[state->conn_state]);
3744 vchiq_dump(dump_context, buf, len + 1);
3746 len = snprintf(buf, sizeof(buf),
3747 " tx_pos=%x(@%pK), rx_pos=%x(@%pK)",
3748 state->local->tx_pos,
3749 state->tx_data + (state->local_tx_pos & VCHIQ_SLOT_MASK),
3751 state->rx_data + (state->rx_pos & VCHIQ_SLOT_MASK));
3752 vchiq_dump(dump_context, buf, len + 1);
3754 len = snprintf(buf, sizeof(buf),
3755 " Version: %d (min %d)",
3756 VCHIQ_VERSION, VCHIQ_VERSION_MIN);
3757 vchiq_dump(dump_context, buf, len + 1);
3759 if (VCHIQ_ENABLE_STATS) {
3760 len = snprintf(buf, sizeof(buf),
3761 " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
3763 state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
3764 state->stats.error_count);
3765 vchiq_dump(dump_context, buf, len + 1);
3768 len = snprintf(buf, sizeof(buf),
3769 " Slots: %d available (%d data), %d recyclable, %d stalls "
3771 ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
3772 state->local_tx_pos) / VCHIQ_SLOT_SIZE,
3773 state->data_quota - state->data_use_count,
3774 state->local->slot_queue_recycle - state->slot_queue_available,
3775 state->stats.slot_stalls, state->stats.data_stalls);
3776 vchiq_dump(dump_context, buf, len + 1);
3778 vchiq_dump_platform_state(dump_context);
3780 vchiq_dump_shared_state(dump_context, state, state->local, "Local");
3781 vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
3783 vchiq_dump_platform_instances(dump_context);
3785 for (i = 0; i < state->unused_service; i++) {
3786 VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
3789 vchiq_dump_service_state(dump_context, service);
3790 unlock_service(service);
3796 vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
3801 len = snprintf(buf, sizeof(buf), "Service %u: %s (ref %u)",
3802 service->localport, srvstate_names[service->srvstate],
3803 service->ref_count - 1); /*Don't include the lock just taken*/
3805 if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
3806 char remoteport[30];
3807 VCHIQ_SERVICE_QUOTA_T *service_quota =
3808 &service->state->service_quotas[service->localport];
3809 int fourcc = service->base.fourcc;
3810 int tx_pending, rx_pending;
3812 if (service->remoteport != VCHIQ_PORT_FREE) {
3813 int len2 = snprintf(remoteport, sizeof(remoteport),
3814 "%u", service->remoteport);
3816 if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
3817 snprintf(remoteport + len2,
3818 sizeof(remoteport) - len2,
3819 " (client %x)", service->client_id);
3821 strcpy(remoteport, "n/a");
3823 len += snprintf(buf + len, sizeof(buf) - len,
3824 " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
3825 VCHIQ_FOURCC_AS_4CHARS(fourcc),
3827 service_quota->message_use_count,
3828 service_quota->message_quota,
3829 service_quota->slot_use_count,
3830 service_quota->slot_quota);
3832 vchiq_dump(dump_context, buf, len + 1);
3834 tx_pending = service->bulk_tx.local_insert -
3835 service->bulk_tx.remote_insert;
3837 rx_pending = service->bulk_rx.local_insert -
3838 service->bulk_rx.remote_insert;
3840 len = snprintf(buf, sizeof(buf),
3841 " Bulk: tx_pending=%d (size %d),"
3842 " rx_pending=%d (size %d)",
3844 tx_pending ? service->bulk_tx.bulks[
3845 BULK_INDEX(service->bulk_tx.remove)].size : 0,
3847 rx_pending ? service->bulk_rx.bulks[
3848 BULK_INDEX(service->bulk_rx.remove)].size : 0);
3850 if (VCHIQ_ENABLE_STATS) {
3851 vchiq_dump(dump_context, buf, len + 1);
3853 len = snprintf(buf, sizeof(buf),
3854 " Ctrl: tx_count=%d, tx_bytes=%llu, "
3855 "rx_count=%d, rx_bytes=%llu",
3856 service->stats.ctrl_tx_count,
3857 service->stats.ctrl_tx_bytes,
3858 service->stats.ctrl_rx_count,
3859 service->stats.ctrl_rx_bytes);
3860 vchiq_dump(dump_context, buf, len + 1);
3862 len = snprintf(buf, sizeof(buf),
3863 " Bulk: tx_count=%d, tx_bytes=%llu, "
3864 "rx_count=%d, rx_bytes=%llu",
3865 service->stats.bulk_tx_count,
3866 service->stats.bulk_tx_bytes,
3867 service->stats.bulk_rx_count,
3868 service->stats.bulk_rx_bytes);
3869 vchiq_dump(dump_context, buf, len + 1);
3871 len = snprintf(buf, sizeof(buf),
3872 " %d quota stalls, %d slot stalls, "
3873 "%d bulk stalls, %d aborted, %d errors",
3874 service->stats.quota_stalls,
3875 service->stats.slot_stalls,
3876 service->stats.bulk_stalls,
3877 service->stats.bulk_aborted_count,
3878 service->stats.error_count);
3882 vchiq_dump(dump_context, buf, len + 1);
3884 if (service->srvstate != VCHIQ_SRVSTATE_FREE)
3885 vchiq_dump_platform_service_state(dump_context, service);
3889 vchiq_loud_error_header(void)
3891 vchiq_log_error(vchiq_core_log_level,
3892 "============================================================"
3893 "================");
3894 vchiq_log_error(vchiq_core_log_level,
3895 "============================================================"
3896 "================");
3897 vchiq_log_error(vchiq_core_log_level, "=====");
3901 vchiq_loud_error_footer(void)
3903 vchiq_log_error(vchiq_core_log_level, "=====");
3904 vchiq_log_error(vchiq_core_log_level,
3905 "============================================================"
3906 "================");
3907 vchiq_log_error(vchiq_core_log_level,
3908 "============================================================"
3909 "================");
3912 VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
3914 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3916 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3917 status = queue_message(state, NULL,
3918 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
3923 VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
3925 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3927 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3928 status = queue_message(state, NULL,
3929 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
3934 VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
3936 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3938 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3939 status = queue_message(state, NULL,
3940 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
3945 void vchiq_log_dump_mem(const char *label, u32 addr, const void *void_mem,
3948 const u8 *mem = (const u8 *)void_mem;
3953 while (num_bytes > 0) {
3956 for (offset = 0; offset < 16; offset++) {
3957 if (offset < num_bytes)
3958 s += snprintf(s, 4, "%02x ", mem[offset]);
3960 s += snprintf(s, 4, " ");
3963 for (offset = 0; offset < 16; offset++) {
3964 if (offset < num_bytes) {
3965 u8 ch = mem[offset];
3967 if ((ch < ' ') || (ch > '~'))
3974 if ((label != NULL) && (*label != '\0'))
3975 vchiq_log_trace(VCHIQ_LOG_TRACE,
3976 "%s: %08x: %s", label, addr, line_buf);
3978 vchiq_log_trace(VCHIQ_LOG_TRACE,
3979 "%08x: %s", addr, line_buf);