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);
2540 if (status != VCHIQ_SUCCESS)
2544 bring up slot handler thread
2546 snprintf(threadname, sizeof(threadname), "vchiq-slot/%d", state->id);
2547 state->slot_handler_thread = kthread_create(&slot_handler_func,
2551 if (IS_ERR(state->slot_handler_thread)) {
2552 vchiq_loud_error_header();
2553 vchiq_loud_error("couldn't create thread %s", threadname);
2554 vchiq_loud_error_footer();
2557 set_user_nice(state->slot_handler_thread, -19);
2559 snprintf(threadname, sizeof(threadname), "vchiq-recy/%d", state->id);
2560 state->recycle_thread = kthread_create(&recycle_func,
2563 if (IS_ERR(state->recycle_thread)) {
2564 vchiq_loud_error_header();
2565 vchiq_loud_error("couldn't create thread %s", threadname);
2566 vchiq_loud_error_footer();
2567 goto fail_free_handler_thread;
2569 set_user_nice(state->recycle_thread, -19);
2571 snprintf(threadname, sizeof(threadname), "vchiq-sync/%d", state->id);
2572 state->sync_thread = kthread_create(&sync_func,
2575 if (IS_ERR(state->sync_thread)) {
2576 vchiq_loud_error_header();
2577 vchiq_loud_error("couldn't create thread %s", threadname);
2578 vchiq_loud_error_footer();
2579 goto fail_free_recycle_thread;
2581 set_user_nice(state->sync_thread, -20);
2583 wake_up_process(state->slot_handler_thread);
2584 wake_up_process(state->recycle_thread);
2585 wake_up_process(state->sync_thread);
2587 vchiq_states[0] = state;
2589 /* Indicate readiness to the other side */
2590 local->initialised = 1;
2594 fail_free_recycle_thread:
2595 kthread_stop(state->recycle_thread);
2596 fail_free_handler_thread:
2597 kthread_stop(state->slot_handler_thread);
2602 /* Called from application thread when a client or server service is created. */
2604 vchiq_add_service_internal(VCHIQ_STATE_T *state,
2605 const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
2606 VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
2608 VCHIQ_SERVICE_T *service;
2609 VCHIQ_SERVICE_T **pservice = NULL;
2610 VCHIQ_SERVICE_QUOTA_T *service_quota;
2613 service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
2617 service->base.fourcc = params->fourcc;
2618 service->base.callback = params->callback;
2619 service->base.userdata = params->userdata;
2620 service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
2621 service->ref_count = 1;
2622 service->srvstate = VCHIQ_SRVSTATE_FREE;
2623 service->userdata_term = userdata_term;
2624 service->localport = VCHIQ_PORT_FREE;
2625 service->remoteport = VCHIQ_PORT_FREE;
2627 service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
2628 VCHIQ_FOURCC_INVALID : params->fourcc;
2629 service->client_id = 0;
2630 service->auto_close = 1;
2632 service->closing = 0;
2634 atomic_set(&service->poll_flags, 0);
2635 service->version = params->version;
2636 service->version_min = params->version_min;
2637 service->state = state;
2638 service->instance = instance;
2639 service->service_use_count = 0;
2640 init_bulk_queue(&service->bulk_tx);
2641 init_bulk_queue(&service->bulk_rx);
2642 sema_init(&service->remove_event, 0);
2643 sema_init(&service->bulk_remove_event, 0);
2644 mutex_init(&service->bulk_mutex);
2645 memset(&service->stats, 0, sizeof(service->stats));
2647 /* Although it is perfectly possible to use service_spinlock
2648 ** to protect the creation of services, it is overkill as it
2649 ** disables interrupts while the array is searched.
2650 ** The only danger is of another thread trying to create a
2651 ** service - service deletion is safe.
2652 ** Therefore it is preferable to use state->mutex which,
2653 ** although slower to claim, doesn't block interrupts while
2657 mutex_lock(&state->mutex);
2659 /* Prepare to use a previously unused service */
2660 if (state->unused_service < VCHIQ_MAX_SERVICES)
2661 pservice = &state->services[state->unused_service];
2663 if (srvstate == VCHIQ_SRVSTATE_OPENING) {
2664 for (i = 0; i < state->unused_service; i++) {
2665 VCHIQ_SERVICE_T *srv = state->services[i];
2668 pservice = &state->services[i];
2673 for (i = (state->unused_service - 1); i >= 0; i--) {
2674 VCHIQ_SERVICE_T *srv = state->services[i];
2677 pservice = &state->services[i];
2678 else if ((srv->public_fourcc == params->fourcc)
2679 && ((srv->instance != instance) ||
2680 (srv->base.callback !=
2681 params->callback))) {
2682 /* There is another server using this
2683 ** fourcc which doesn't match. */
2691 service->localport = (pservice - state->services);
2693 handle_seq = VCHIQ_MAX_STATES *
2695 service->handle = handle_seq |
2696 (state->id * VCHIQ_MAX_SERVICES) |
2698 handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
2699 *pservice = service;
2700 if (pservice == &state->services[state->unused_service])
2701 state->unused_service++;
2704 mutex_unlock(&state->mutex);
2711 service_quota = &state->service_quotas[service->localport];
2712 service_quota->slot_quota = state->default_slot_quota;
2713 service_quota->message_quota = state->default_message_quota;
2714 if (service_quota->slot_use_count == 0)
2715 service_quota->previous_tx_index =
2716 SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
2719 /* Bring this service online */
2720 vchiq_set_service_state(service, srvstate);
2722 vchiq_log_info(vchiq_core_msg_log_level,
2723 "%s Service %c%c%c%c SrcPort:%d",
2724 (srvstate == VCHIQ_SRVSTATE_OPENING)
2726 VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
2727 service->localport);
2729 /* Don't unlock the service - leave it with a ref_count of 1. */
2735 vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
2737 struct vchiq_open_payload payload = {
2738 service->base.fourcc,
2741 service->version_min
2743 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2745 service->client_id = client_id;
2746 vchiq_use_service_internal(service);
2747 status = queue_message(service->state,
2749 VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN,
2752 memcpy_copy_callback,
2755 QMFLAGS_IS_BLOCKING);
2756 if (status == VCHIQ_SUCCESS) {
2757 /* Wait for the ACK/NAK */
2758 if (down_interruptible(&service->remove_event) != 0) {
2759 status = VCHIQ_RETRY;
2760 vchiq_release_service_internal(service);
2761 } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
2762 (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
2763 if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
2764 vchiq_log_error(vchiq_core_log_level,
2765 "%d: osi - srvstate = %s (ref %d)",
2767 srvstate_names[service->srvstate],
2768 service->ref_count);
2769 status = VCHIQ_ERROR;
2770 VCHIQ_SERVICE_STATS_INC(service, error_count);
2771 vchiq_release_service_internal(service);
2778 release_service_messages(VCHIQ_SERVICE_T *service)
2780 VCHIQ_STATE_T *state = service->state;
2781 int slot_last = state->remote->slot_last;
2784 /* Release any claimed messages aimed at this service */
2786 if (service->sync) {
2787 VCHIQ_HEADER_T *header =
2788 (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
2789 state->remote->slot_sync);
2790 if (VCHIQ_MSG_DSTPORT(header->msgid) == service->localport)
2791 release_message_sync(state, header);
2796 for (i = state->remote->slot_first; i <= slot_last; i++) {
2797 VCHIQ_SLOT_INFO_T *slot_info =
2798 SLOT_INFO_FROM_INDEX(state, i);
2799 if (slot_info->release_count != slot_info->use_count) {
2801 (char *)SLOT_DATA_FROM_INDEX(state, i);
2802 unsigned int pos, end;
2804 end = VCHIQ_SLOT_SIZE;
2805 if (data == state->rx_data)
2806 /* This buffer is still being read from - stop
2807 ** at the current read position */
2808 end = state->rx_pos & VCHIQ_SLOT_MASK;
2813 VCHIQ_HEADER_T *header =
2814 (VCHIQ_HEADER_T *)(data + pos);
2815 int msgid = header->msgid;
2816 int port = VCHIQ_MSG_DSTPORT(msgid);
2818 if ((port == service->localport) &&
2819 (msgid & VCHIQ_MSGID_CLAIMED)) {
2820 vchiq_log_info(vchiq_core_log_level,
2821 " fsi - hdr %pK", header);
2822 release_slot(state, slot_info, header,
2825 pos += calc_stride(header->size);
2826 if (pos > VCHIQ_SLOT_SIZE) {
2827 vchiq_log_error(vchiq_core_log_level,
2828 "fsi - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
2830 header->msgid, header->size);
2831 WARN(1, "invalid slot position\n");
2839 do_abort_bulks(VCHIQ_SERVICE_T *service)
2841 VCHIQ_STATUS_T status;
2843 /* Abort any outstanding bulk transfers */
2844 if (mutex_lock_killable(&service->bulk_mutex) != 0)
2846 abort_outstanding_bulks(service, &service->bulk_tx);
2847 abort_outstanding_bulks(service, &service->bulk_rx);
2848 mutex_unlock(&service->bulk_mutex);
2850 status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
2851 if (status == VCHIQ_SUCCESS)
2852 status = notify_bulks(service, &service->bulk_rx,
2854 return (status == VCHIQ_SUCCESS);
2857 static VCHIQ_STATUS_T
2858 close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
2860 VCHIQ_STATUS_T status;
2861 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2864 switch (service->srvstate) {
2865 case VCHIQ_SRVSTATE_OPEN:
2866 case VCHIQ_SRVSTATE_CLOSESENT:
2867 case VCHIQ_SRVSTATE_CLOSERECVD:
2869 if (service->auto_close) {
2870 service->client_id = 0;
2871 service->remoteport = VCHIQ_PORT_FREE;
2872 newstate = VCHIQ_SRVSTATE_LISTENING;
2874 newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
2876 newstate = VCHIQ_SRVSTATE_CLOSED;
2877 vchiq_set_service_state(service, newstate);
2879 case VCHIQ_SRVSTATE_LISTENING:
2882 vchiq_log_error(vchiq_core_log_level,
2883 "%s(%x) called in state %s", __func__,
2884 service->handle, srvstate_names[service->srvstate]);
2885 WARN(1, "%s in unexpected state\n", __func__);
2889 status = make_service_callback(service,
2890 VCHIQ_SERVICE_CLOSED, NULL, NULL);
2892 if (status != VCHIQ_RETRY) {
2893 int uc = service->service_use_count;
2895 /* Complete the close process */
2896 for (i = 0; i < uc; i++)
2897 /* cater for cases where close is forced and the
2898 ** client may not close all it's handles */
2899 vchiq_release_service_internal(service);
2901 service->client_id = 0;
2902 service->remoteport = VCHIQ_PORT_FREE;
2904 if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
2905 vchiq_free_service_internal(service);
2906 else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
2908 service->closing = 0;
2910 up(&service->remove_event);
2913 vchiq_set_service_state(service, failstate);
2918 /* Called by the slot handler */
2920 vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
2922 VCHIQ_STATE_T *state = service->state;
2923 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2924 int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2926 vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
2927 service->state->id, service->localport, close_recvd,
2928 srvstate_names[service->srvstate]);
2930 switch (service->srvstate) {
2931 case VCHIQ_SRVSTATE_CLOSED:
2932 case VCHIQ_SRVSTATE_HIDDEN:
2933 case VCHIQ_SRVSTATE_LISTENING:
2934 case VCHIQ_SRVSTATE_CLOSEWAIT:
2936 vchiq_log_error(vchiq_core_log_level,
2939 __func__, srvstate_names[service->srvstate]);
2940 else if (is_server) {
2941 if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
2942 status = VCHIQ_ERROR;
2944 service->client_id = 0;
2945 service->remoteport = VCHIQ_PORT_FREE;
2946 if (service->srvstate ==
2947 VCHIQ_SRVSTATE_CLOSEWAIT)
2948 vchiq_set_service_state(service,
2949 VCHIQ_SRVSTATE_LISTENING);
2951 up(&service->remove_event);
2953 vchiq_free_service_internal(service);
2955 case VCHIQ_SRVSTATE_OPENING:
2957 /* The open was rejected - tell the user */
2958 vchiq_set_service_state(service,
2959 VCHIQ_SRVSTATE_CLOSEWAIT);
2960 up(&service->remove_event);
2962 /* Shutdown mid-open - let the other side know */
2963 status = queue_message(state, service,
2967 VCHIQ_MSG_DSTPORT(service->remoteport)),
2972 case VCHIQ_SRVSTATE_OPENSYNC:
2973 mutex_lock(&state->sync_mutex);
2975 case VCHIQ_SRVSTATE_OPEN:
2976 if (state->is_master || close_recvd) {
2977 if (!do_abort_bulks(service))
2978 status = VCHIQ_RETRY;
2981 release_service_messages(service);
2983 if (status == VCHIQ_SUCCESS)
2984 status = queue_message(state, service,
2988 VCHIQ_MSG_DSTPORT(service->remoteport)),
2989 NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK);
2991 if (status == VCHIQ_SUCCESS) {
2993 /* Change the state while the mutex is
2995 vchiq_set_service_state(service,
2996 VCHIQ_SRVSTATE_CLOSESENT);
2997 mutex_unlock(&state->slot_mutex);
2999 mutex_unlock(&state->sync_mutex);
3002 } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
3003 mutex_unlock(&state->sync_mutex);
3008 /* Change the state while the mutex is still held */
3009 vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSERECVD);
3010 mutex_unlock(&state->slot_mutex);
3012 mutex_unlock(&state->sync_mutex);
3014 status = close_service_complete(service,
3015 VCHIQ_SRVSTATE_CLOSERECVD);
3018 case VCHIQ_SRVSTATE_CLOSESENT:
3020 /* This happens when a process is killed mid-close */
3023 if (!state->is_master) {
3024 if (!do_abort_bulks(service)) {
3025 status = VCHIQ_RETRY;
3030 if (status == VCHIQ_SUCCESS)
3031 status = close_service_complete(service,
3032 VCHIQ_SRVSTATE_CLOSERECVD);
3035 case VCHIQ_SRVSTATE_CLOSERECVD:
3036 if (!close_recvd && is_server)
3037 /* Force into LISTENING mode */
3038 vchiq_set_service_state(service,
3039 VCHIQ_SRVSTATE_LISTENING);
3040 status = close_service_complete(service,
3041 VCHIQ_SRVSTATE_CLOSERECVD);
3045 vchiq_log_error(vchiq_core_log_level,
3046 "%s(%d) called in state %s", __func__,
3047 close_recvd, srvstate_names[service->srvstate]);
3054 /* Called from the application process upon process death */
3056 vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
3058 VCHIQ_STATE_T *state = service->state;
3060 vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
3061 state->id, service->localport, service->remoteport);
3063 mark_service_closing(service);
3065 /* Mark the service for removal by the slot handler */
3066 request_poll(state, service, VCHIQ_POLL_REMOVE);
3069 /* Called from the slot handler */
3071 vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
3073 VCHIQ_STATE_T *state = service->state;
3075 vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
3076 state->id, service->localport);
3078 switch (service->srvstate) {
3079 case VCHIQ_SRVSTATE_OPENING:
3080 case VCHIQ_SRVSTATE_CLOSED:
3081 case VCHIQ_SRVSTATE_HIDDEN:
3082 case VCHIQ_SRVSTATE_LISTENING:
3083 case VCHIQ_SRVSTATE_CLOSEWAIT:
3086 vchiq_log_error(vchiq_core_log_level,
3087 "%d: fsi - (%d) in state %s",
3088 state->id, service->localport,
3089 srvstate_names[service->srvstate]);
3093 vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
3095 up(&service->remove_event);
3097 /* Release the initial lock */
3098 unlock_service(service);
3102 vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
3104 VCHIQ_SERVICE_T *service;
3107 /* Find all services registered to this client and enable them. */
3109 while ((service = next_service_by_instance(state, instance,
3111 if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
3112 vchiq_set_service_state(service,
3113 VCHIQ_SRVSTATE_LISTENING);
3114 unlock_service(service);
3117 if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
3118 if (queue_message(state, NULL,
3119 VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, NULL,
3120 0, QMFLAGS_IS_BLOCKING) == VCHIQ_RETRY)
3123 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
3126 if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
3127 if (down_interruptible(&state->connect) != 0)
3130 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
3131 up(&state->connect);
3134 return VCHIQ_SUCCESS;
3138 vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
3140 VCHIQ_SERVICE_T *service;
3143 /* Find all services registered to this client and enable them. */
3145 while ((service = next_service_by_instance(state, instance,
3147 (void)vchiq_remove_service(service->handle);
3148 unlock_service(service);
3151 return VCHIQ_SUCCESS;
3155 vchiq_pause_internal(VCHIQ_STATE_T *state)
3157 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3159 switch (state->conn_state) {
3160 case VCHIQ_CONNSTATE_CONNECTED:
3161 /* Request a pause */
3162 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
3163 request_poll(state, NULL, 0);
3166 vchiq_log_error(vchiq_core_log_level,
3168 __func__, conn_state_names[state->conn_state]);
3169 status = VCHIQ_ERROR;
3170 VCHIQ_STATS_INC(state, error_count);
3178 vchiq_resume_internal(VCHIQ_STATE_T *state)
3180 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3182 if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
3183 vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
3184 request_poll(state, NULL, 0);
3186 status = VCHIQ_ERROR;
3187 VCHIQ_STATS_INC(state, error_count);
3194 vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
3196 /* Unregister the service */
3197 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3198 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3203 vchiq_log_info(vchiq_core_log_level,
3204 "%d: close_service:%d",
3205 service->state->id, service->localport);
3207 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3208 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
3209 (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
3210 unlock_service(service);
3214 mark_service_closing(service);
3216 if (current == service->state->slot_handler_thread) {
3217 status = vchiq_close_service_internal(service,
3219 WARN_ON(status == VCHIQ_RETRY);
3221 /* Mark the service for termination by the slot handler */
3222 request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
3226 if (down_interruptible(&service->remove_event) != 0) {
3227 status = VCHIQ_RETRY;
3231 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3232 (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
3233 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
3236 vchiq_log_warning(vchiq_core_log_level,
3237 "%d: close_service:%d - waiting in state %s",
3238 service->state->id, service->localport,
3239 srvstate_names[service->srvstate]);
3242 if ((status == VCHIQ_SUCCESS) &&
3243 (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
3244 (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
3245 status = VCHIQ_ERROR;
3247 unlock_service(service);
3253 vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
3255 /* Unregister the service */
3256 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3257 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3262 vchiq_log_info(vchiq_core_log_level,
3263 "%d: remove_service:%d",
3264 service->state->id, service->localport);
3266 if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
3267 unlock_service(service);
3271 mark_service_closing(service);
3273 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3274 (current == service->state->slot_handler_thread)) {
3275 /* Make it look like a client, because it must be removed and
3276 not left in the LISTENING state. */
3277 service->public_fourcc = VCHIQ_FOURCC_INVALID;
3279 status = vchiq_close_service_internal(service,
3281 WARN_ON(status == VCHIQ_RETRY);
3283 /* Mark the service for removal by the slot handler */
3284 request_poll(service->state, service, VCHIQ_POLL_REMOVE);
3287 if (down_interruptible(&service->remove_event) != 0) {
3288 status = VCHIQ_RETRY;
3292 if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
3293 (service->srvstate == VCHIQ_SRVSTATE_OPEN))
3296 vchiq_log_warning(vchiq_core_log_level,
3297 "%d: remove_service:%d - waiting in state %s",
3298 service->state->id, service->localport,
3299 srvstate_names[service->srvstate]);
3302 if ((status == VCHIQ_SUCCESS) &&
3303 (service->srvstate != VCHIQ_SRVSTATE_FREE))
3304 status = VCHIQ_ERROR;
3306 unlock_service(service);
3311 /* This function may be called by kernel threads or user threads.
3312 * User threads may receive VCHIQ_RETRY to indicate that a signal has been
3313 * received and the call should be retried after being returned to user
3315 * When called in blocking mode, the userdata field points to a bulk_waiter
3319 vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
3320 VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
3321 VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
3323 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3324 VCHIQ_BULK_QUEUE_T *queue;
3326 VCHIQ_STATE_T *state;
3327 struct bulk_waiter *bulk_waiter = NULL;
3328 const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
3329 const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
3330 VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
3331 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3334 (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
3335 ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
3336 (vchiq_check_service(service) != VCHIQ_SUCCESS))
3340 case VCHIQ_BULK_MODE_NOCALLBACK:
3341 case VCHIQ_BULK_MODE_CALLBACK:
3343 case VCHIQ_BULK_MODE_BLOCKING:
3344 bulk_waiter = (struct bulk_waiter *)userdata;
3345 sema_init(&bulk_waiter->event, 0);
3346 bulk_waiter->actual = 0;
3347 bulk_waiter->bulk = NULL;
3349 case VCHIQ_BULK_MODE_WAITING:
3350 bulk_waiter = (struct bulk_waiter *)userdata;
3351 bulk = bulk_waiter->bulk;
3357 state = service->state;
3359 queue = (dir == VCHIQ_BULK_TRANSMIT) ?
3360 &service->bulk_tx : &service->bulk_rx;
3362 if (mutex_lock_killable(&service->bulk_mutex) != 0) {
3363 status = VCHIQ_RETRY;
3367 if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
3368 VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
3370 mutex_unlock(&service->bulk_mutex);
3371 if (down_interruptible(&service->bulk_remove_event)
3373 status = VCHIQ_RETRY;
3376 if (mutex_lock_killable(&service->bulk_mutex)
3378 status = VCHIQ_RETRY;
3381 } while (queue->local_insert == queue->remove +
3382 VCHIQ_NUM_SERVICE_BULKS);
3385 bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
3389 bulk->userdata = userdata;
3391 bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
3393 if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
3395 goto unlock_error_exit;
3399 vchiq_log_info(vchiq_core_log_level,
3400 "%d: bt (%d->%d) %cx %x@%pK %pK",
3401 state->id, service->localport, service->remoteport, dir_char,
3402 size, bulk->data, userdata);
3404 /* The slot mutex must be held when the service is being closed, so
3405 claim it here to ensure that isn't happening */
3406 if (mutex_lock_killable(&state->slot_mutex) != 0) {
3407 status = VCHIQ_RETRY;
3408 goto cancel_bulk_error_exit;
3411 if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
3412 goto unlock_both_error_exit;
3414 if (state->is_master) {
3415 queue->local_insert++;
3416 if (resolve_bulks(service, queue))
3417 request_poll(state, service,
3418 (dir == VCHIQ_BULK_TRANSMIT) ?
3419 VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
3421 int payload[2] = { (int)(long)bulk->data, bulk->size };
3423 status = queue_message(state,
3425 VCHIQ_MAKE_MSG(dir_msgtype,
3427 service->remoteport),
3428 memcpy_copy_callback,
3431 QMFLAGS_IS_BLOCKING |
3432 QMFLAGS_NO_MUTEX_LOCK |
3433 QMFLAGS_NO_MUTEX_UNLOCK);
3434 if (status != VCHIQ_SUCCESS) {
3435 goto unlock_both_error_exit;
3437 queue->local_insert++;
3440 mutex_unlock(&state->slot_mutex);
3441 mutex_unlock(&service->bulk_mutex);
3443 vchiq_log_trace(vchiq_core_log_level,
3444 "%d: bt:%d %cx li=%x ri=%x p=%x",
3446 service->localport, dir_char,
3447 queue->local_insert, queue->remote_insert, queue->process);
3450 unlock_service(service);
3452 status = VCHIQ_SUCCESS;
3455 bulk_waiter->bulk = bulk;
3456 if (down_interruptible(&bulk_waiter->event) != 0)
3457 status = VCHIQ_RETRY;
3458 else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
3459 status = VCHIQ_ERROR;
3464 unlock_both_error_exit:
3465 mutex_unlock(&state->slot_mutex);
3466 cancel_bulk_error_exit:
3467 vchiq_complete_bulk(bulk);
3469 mutex_unlock(&service->bulk_mutex);
3473 unlock_service(service);
3478 vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
3479 ssize_t (*copy_callback)(void *context, void *dest,
3480 size_t offset, size_t maxsize),
3484 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3485 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3488 (vchiq_check_service(service) != VCHIQ_SUCCESS))
3492 VCHIQ_SERVICE_STATS_INC(service, error_count);
3497 if (size > VCHIQ_MAX_MSG_SIZE) {
3498 VCHIQ_SERVICE_STATS_INC(service, error_count);
3502 switch (service->srvstate) {
3503 case VCHIQ_SRVSTATE_OPEN:
3504 status = queue_message(service->state, service,
3505 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3507 service->remoteport),
3508 copy_callback, context, size, 1);
3510 case VCHIQ_SRVSTATE_OPENSYNC:
3511 status = queue_message_sync(service->state, service,
3512 VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3514 service->remoteport),
3515 copy_callback, context, size, 1);
3518 status = VCHIQ_ERROR;
3524 unlock_service(service);
3530 vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
3532 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3533 VCHIQ_SHARED_STATE_T *remote;
3534 VCHIQ_STATE_T *state;
3540 state = service->state;
3541 remote = state->remote;
3543 slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
3545 if ((slot_index >= remote->slot_first) &&
3546 (slot_index <= remote->slot_last)) {
3547 int msgid = header->msgid;
3549 if (msgid & VCHIQ_MSGID_CLAIMED) {
3550 VCHIQ_SLOT_INFO_T *slot_info =
3551 SLOT_INFO_FROM_INDEX(state, slot_index);
3553 release_slot(state, slot_info, header, service);
3555 } else if (slot_index == remote->slot_sync)
3556 release_message_sync(state, header);
3558 unlock_service(service);
3562 release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
3564 header->msgid = VCHIQ_MSGID_PADDING;
3566 remote_event_signal(&state->remote->sync_release);
3570 vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
3572 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3573 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3576 (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
3579 *peer_version = service->peer_version;
3580 status = VCHIQ_SUCCESS;
3584 unlock_service(service);
3589 vchiq_get_config(VCHIQ_INSTANCE_T instance,
3590 int config_size, VCHIQ_CONFIG_T *pconfig)
3592 VCHIQ_CONFIG_T config;
3596 config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
3597 config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
3598 config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
3599 config.max_services = VCHIQ_MAX_SERVICES;
3600 config.version = VCHIQ_VERSION;
3601 config.version_min = VCHIQ_VERSION_MIN;
3603 if (config_size > sizeof(VCHIQ_CONFIG_T))
3606 memcpy(pconfig, &config,
3607 min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
3609 return VCHIQ_SUCCESS;
3613 vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
3614 VCHIQ_SERVICE_OPTION_T option, int value)
3616 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3617 VCHIQ_STATUS_T status = VCHIQ_ERROR;
3621 case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
3622 service->auto_close = value;
3623 status = VCHIQ_SUCCESS;
3626 case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
3627 VCHIQ_SERVICE_QUOTA_T *service_quota =
3628 &service->state->service_quotas[
3629 service->localport];
3631 value = service->state->default_slot_quota;
3632 if ((value >= service_quota->slot_use_count) &&
3633 (value < (unsigned short)~0)) {
3634 service_quota->slot_quota = value;
3635 if ((value >= service_quota->slot_use_count) &&
3636 (service_quota->message_quota >=
3637 service_quota->message_use_count)) {
3638 /* Signal the service that it may have
3639 ** dropped below its quota */
3640 up(&service_quota->quota_event);
3642 status = VCHIQ_SUCCESS;
3646 case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
3647 VCHIQ_SERVICE_QUOTA_T *service_quota =
3648 &service->state->service_quotas[
3649 service->localport];
3651 value = service->state->default_message_quota;
3652 if ((value >= service_quota->message_use_count) &&
3653 (value < (unsigned short)~0)) {
3654 service_quota->message_quota = value;
3656 service_quota->message_use_count) &&
3657 (service_quota->slot_quota >=
3658 service_quota->slot_use_count))
3659 /* Signal the service that it may have
3660 ** dropped below its quota */
3661 up(&service_quota->quota_event);
3662 status = VCHIQ_SUCCESS;
3666 case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
3667 if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3668 (service->srvstate ==
3669 VCHIQ_SRVSTATE_LISTENING)) {
3670 service->sync = value;
3671 status = VCHIQ_SUCCESS;
3675 case VCHIQ_SERVICE_OPTION_TRACE:
3676 service->trace = value;
3677 status = VCHIQ_SUCCESS;
3683 unlock_service(service);
3690 vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
3691 VCHIQ_SHARED_STATE_T *shared, const char *label)
3693 static const char *const debug_names[] = {
3695 "SLOT_HANDLER_COUNT",
3696 "SLOT_HANDLER_LINE",
3700 "AWAIT_COMPLETION_LINE",
3701 "DEQUEUE_MESSAGE_LINE",
3702 "SERVICE_CALLBACK_LINE",
3703 "MSG_QUEUE_FULL_COUNT",
3704 "COMPLETION_QUEUE_FULL_COUNT"
3710 len = snprintf(buf, sizeof(buf),
3711 " %s: slots %d-%d tx_pos=%x recycle=%x",
3712 label, shared->slot_first, shared->slot_last,
3713 shared->tx_pos, shared->slot_queue_recycle);
3714 vchiq_dump(dump_context, buf, len + 1);
3716 len = snprintf(buf, sizeof(buf),
3718 vchiq_dump(dump_context, buf, len + 1);
3720 for (i = shared->slot_first; i <= shared->slot_last; i++) {
3721 VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
3722 if (slot_info.use_count != slot_info.release_count) {
3723 len = snprintf(buf, sizeof(buf),
3724 " %d: %d/%d", i, slot_info.use_count,
3725 slot_info.release_count);
3726 vchiq_dump(dump_context, buf, len + 1);
3730 for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
3731 len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
3732 debug_names[i], shared->debug[i], shared->debug[i]);
3733 vchiq_dump(dump_context, buf, len + 1);
3738 vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
3744 len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
3745 conn_state_names[state->conn_state]);
3746 vchiq_dump(dump_context, buf, len + 1);
3748 len = snprintf(buf, sizeof(buf),
3749 " tx_pos=%x(@%pK), rx_pos=%x(@%pK)",
3750 state->local->tx_pos,
3751 state->tx_data + (state->local_tx_pos & VCHIQ_SLOT_MASK),
3753 state->rx_data + (state->rx_pos & VCHIQ_SLOT_MASK));
3754 vchiq_dump(dump_context, buf, len + 1);
3756 len = snprintf(buf, sizeof(buf),
3757 " Version: %d (min %d)",
3758 VCHIQ_VERSION, VCHIQ_VERSION_MIN);
3759 vchiq_dump(dump_context, buf, len + 1);
3761 if (VCHIQ_ENABLE_STATS) {
3762 len = snprintf(buf, sizeof(buf),
3763 " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
3765 state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
3766 state->stats.error_count);
3767 vchiq_dump(dump_context, buf, len + 1);
3770 len = snprintf(buf, sizeof(buf),
3771 " Slots: %d available (%d data), %d recyclable, %d stalls "
3773 ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
3774 state->local_tx_pos) / VCHIQ_SLOT_SIZE,
3775 state->data_quota - state->data_use_count,
3776 state->local->slot_queue_recycle - state->slot_queue_available,
3777 state->stats.slot_stalls, state->stats.data_stalls);
3778 vchiq_dump(dump_context, buf, len + 1);
3780 vchiq_dump_platform_state(dump_context);
3782 vchiq_dump_shared_state(dump_context, state, state->local, "Local");
3783 vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
3785 vchiq_dump_platform_instances(dump_context);
3787 for (i = 0; i < state->unused_service; i++) {
3788 VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
3791 vchiq_dump_service_state(dump_context, service);
3792 unlock_service(service);
3798 vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
3803 len = snprintf(buf, sizeof(buf), "Service %u: %s (ref %u)",
3804 service->localport, srvstate_names[service->srvstate],
3805 service->ref_count - 1); /*Don't include the lock just taken*/
3807 if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
3808 char remoteport[30];
3809 VCHIQ_SERVICE_QUOTA_T *service_quota =
3810 &service->state->service_quotas[service->localport];
3811 int fourcc = service->base.fourcc;
3812 int tx_pending, rx_pending;
3814 if (service->remoteport != VCHIQ_PORT_FREE) {
3815 int len2 = snprintf(remoteport, sizeof(remoteport),
3816 "%u", service->remoteport);
3818 if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
3819 snprintf(remoteport + len2,
3820 sizeof(remoteport) - len2,
3821 " (client %x)", service->client_id);
3823 strcpy(remoteport, "n/a");
3825 len += snprintf(buf + len, sizeof(buf) - len,
3826 " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
3827 VCHIQ_FOURCC_AS_4CHARS(fourcc),
3829 service_quota->message_use_count,
3830 service_quota->message_quota,
3831 service_quota->slot_use_count,
3832 service_quota->slot_quota);
3834 vchiq_dump(dump_context, buf, len + 1);
3836 tx_pending = service->bulk_tx.local_insert -
3837 service->bulk_tx.remote_insert;
3839 rx_pending = service->bulk_rx.local_insert -
3840 service->bulk_rx.remote_insert;
3842 len = snprintf(buf, sizeof(buf),
3843 " Bulk: tx_pending=%d (size %d),"
3844 " rx_pending=%d (size %d)",
3846 tx_pending ? service->bulk_tx.bulks[
3847 BULK_INDEX(service->bulk_tx.remove)].size : 0,
3849 rx_pending ? service->bulk_rx.bulks[
3850 BULK_INDEX(service->bulk_rx.remove)].size : 0);
3852 if (VCHIQ_ENABLE_STATS) {
3853 vchiq_dump(dump_context, buf, len + 1);
3855 len = snprintf(buf, sizeof(buf),
3856 " Ctrl: tx_count=%d, tx_bytes=%llu, "
3857 "rx_count=%d, rx_bytes=%llu",
3858 service->stats.ctrl_tx_count,
3859 service->stats.ctrl_tx_bytes,
3860 service->stats.ctrl_rx_count,
3861 service->stats.ctrl_rx_bytes);
3862 vchiq_dump(dump_context, buf, len + 1);
3864 len = snprintf(buf, sizeof(buf),
3865 " Bulk: tx_count=%d, tx_bytes=%llu, "
3866 "rx_count=%d, rx_bytes=%llu",
3867 service->stats.bulk_tx_count,
3868 service->stats.bulk_tx_bytes,
3869 service->stats.bulk_rx_count,
3870 service->stats.bulk_rx_bytes);
3871 vchiq_dump(dump_context, buf, len + 1);
3873 len = snprintf(buf, sizeof(buf),
3874 " %d quota stalls, %d slot stalls, "
3875 "%d bulk stalls, %d aborted, %d errors",
3876 service->stats.quota_stalls,
3877 service->stats.slot_stalls,
3878 service->stats.bulk_stalls,
3879 service->stats.bulk_aborted_count,
3880 service->stats.error_count);
3884 vchiq_dump(dump_context, buf, len + 1);
3886 if (service->srvstate != VCHIQ_SRVSTATE_FREE)
3887 vchiq_dump_platform_service_state(dump_context, service);
3891 vchiq_loud_error_header(void)
3893 vchiq_log_error(vchiq_core_log_level,
3894 "============================================================"
3895 "================");
3896 vchiq_log_error(vchiq_core_log_level,
3897 "============================================================"
3898 "================");
3899 vchiq_log_error(vchiq_core_log_level, "=====");
3903 vchiq_loud_error_footer(void)
3905 vchiq_log_error(vchiq_core_log_level, "=====");
3906 vchiq_log_error(vchiq_core_log_level,
3907 "============================================================"
3908 "================");
3909 vchiq_log_error(vchiq_core_log_level,
3910 "============================================================"
3911 "================");
3914 VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
3916 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3918 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3919 status = queue_message(state, NULL,
3920 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
3925 VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
3927 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3929 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3930 status = queue_message(state, NULL,
3931 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
3936 VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
3938 VCHIQ_STATUS_T status = VCHIQ_RETRY;
3940 if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3941 status = queue_message(state, NULL,
3942 VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
3947 void vchiq_log_dump_mem(const char *label, u32 addr, const void *void_mem,
3950 const u8 *mem = (const u8 *)void_mem;
3955 while (num_bytes > 0) {
3958 for (offset = 0; offset < 16; offset++) {
3959 if (offset < num_bytes)
3960 s += snprintf(s, 4, "%02x ", mem[offset]);
3962 s += snprintf(s, 4, " ");
3965 for (offset = 0; offset < 16; offset++) {
3966 if (offset < num_bytes) {
3967 u8 ch = mem[offset];
3969 if ((ch < ' ') || (ch > '~'))
3976 if ((label != NULL) && (*label != '\0'))
3977 vchiq_log_trace(VCHIQ_LOG_TRACE,
3978 "%s: %08x: %s", label, addr, line_buf);
3980 vchiq_log_trace(VCHIQ_LOG_TRACE,
3981 "%08x: %s", addr, line_buf);