Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 : : /* Copyright (c) 2010-2012 Broadcom. All rights reserved. */
3 : :
4 : : #include "vchiq_core.h"
5 : : #include "vchiq_killable.h"
6 : :
7 : : #define VCHIQ_SLOT_HANDLER_STACK 8192
8 : :
9 : : #define HANDLE_STATE_SHIFT 12
10 : :
11 : : #define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
12 : : #define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
13 : : #define SLOT_INDEX_FROM_DATA(state, data) \
14 : : (((unsigned int)((char *)data - (char *)state->slot_data)) / \
15 : : VCHIQ_SLOT_SIZE)
16 : : #define SLOT_INDEX_FROM_INFO(state, info) \
17 : : ((unsigned int)(info - state->slot_info))
18 : : #define SLOT_QUEUE_INDEX_FROM_POS(pos) \
19 : : ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
20 : :
21 : : #define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
22 : :
23 : : #define SRVTRACE_LEVEL(srv) \
24 : : (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
25 : : #define SRVTRACE_ENABLED(srv, lev) \
26 : : (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
27 : :
28 : : struct vchiq_open_payload {
29 : : int fourcc;
30 : : int client_id;
31 : : short version;
32 : : short version_min;
33 : : };
34 : :
35 : : struct vchiq_openack_payload {
36 : : short version;
37 : : };
38 : :
39 : : enum {
40 : : QMFLAGS_IS_BLOCKING = (1 << 0),
41 : : QMFLAGS_NO_MUTEX_LOCK = (1 << 1),
42 : : QMFLAGS_NO_MUTEX_UNLOCK = (1 << 2)
43 : : };
44 : :
45 : : /* we require this for consistency between endpoints */
46 : : vchiq_static_assert(sizeof(struct vchiq_header) == 8);
47 : : vchiq_static_assert(IS_POW2(sizeof(struct vchiq_header)));
48 : : vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
49 : : vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
50 : : vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
51 : : vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
52 : :
53 : : /* Run time control of log level, based on KERN_XXX level. */
54 : : int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
55 : : int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
56 : : int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
57 : :
58 : : static DEFINE_SPINLOCK(service_spinlock);
59 : : DEFINE_SPINLOCK(bulk_waiter_spinlock);
60 : : static DEFINE_SPINLOCK(quota_spinlock);
61 : :
62 : : struct vchiq_state *vchiq_states[VCHIQ_MAX_STATES];
63 : : static unsigned int handle_seq;
64 : :
65 : : static const char *const srvstate_names[] = {
66 : : "FREE",
67 : : "HIDDEN",
68 : : "LISTENING",
69 : : "OPENING",
70 : : "OPEN",
71 : : "OPENSYNC",
72 : : "CLOSESENT",
73 : : "CLOSERECVD",
74 : : "CLOSEWAIT",
75 : : "CLOSED"
76 : : };
77 : :
78 : : static const char *const reason_names[] = {
79 : : "SERVICE_OPENED",
80 : : "SERVICE_CLOSED",
81 : : "MESSAGE_AVAILABLE",
82 : : "BULK_TRANSMIT_DONE",
83 : : "BULK_RECEIVE_DONE",
84 : : "BULK_TRANSMIT_ABORTED",
85 : : "BULK_RECEIVE_ABORTED"
86 : : };
87 : :
88 : : static const char *const conn_state_names[] = {
89 : : "DISCONNECTED",
90 : : "CONNECTING",
91 : : "CONNECTED",
92 : : "PAUSING",
93 : : "PAUSE_SENT",
94 : : "PAUSED",
95 : : "RESUMING",
96 : : "PAUSE_TIMEOUT",
97 : : "RESUME_TIMEOUT"
98 : : };
99 : :
100 : : static void
101 : : release_message_sync(struct vchiq_state *state, struct vchiq_header *header);
102 : :
103 : : static const char *msg_type_str(unsigned int msg_type)
104 : : {
105 : : switch (msg_type) {
106 : : case VCHIQ_MSG_PADDING: return "PADDING";
107 : : case VCHIQ_MSG_CONNECT: return "CONNECT";
108 : : case VCHIQ_MSG_OPEN: return "OPEN";
109 : : case VCHIQ_MSG_OPENACK: return "OPENACK";
110 : : case VCHIQ_MSG_CLOSE: return "CLOSE";
111 : : case VCHIQ_MSG_DATA: return "DATA";
112 : : case VCHIQ_MSG_BULK_RX: return "BULK_RX";
113 : : case VCHIQ_MSG_BULK_TX: return "BULK_TX";
114 : : case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
115 : : case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
116 : : case VCHIQ_MSG_PAUSE: return "PAUSE";
117 : : case VCHIQ_MSG_RESUME: return "RESUME";
118 : : case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
119 : : case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
120 : : case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
121 : : }
122 : : return "???";
123 : : }
124 : :
125 : : static inline void
126 : 3933 : vchiq_set_service_state(struct vchiq_service *service, int newstate)
127 : : {
128 [ - + ]: 3933 : vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
129 : : service->state->id, service->localport,
130 : : srvstate_names[service->srvstate],
131 : : srvstate_names[newstate]);
132 : 3933 : service->srvstate = newstate;
133 : 3933 : }
134 : :
135 : : struct vchiq_service *
136 : 34769 : find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
137 : : {
138 : : struct vchiq_service *service;
139 : :
140 : : spin_lock(&service_spinlock);
141 : : service = handle_to_service(handle);
142 [ + - + - : 69552 : if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
+ - ]
143 : 34776 : (service->handle == handle)) {
144 [ - + ]: 34776 : WARN_ON(service->ref_count == 0);
145 : 34776 : service->ref_count++;
146 : : } else
147 : : service = NULL;
148 : : spin_unlock(&service_spinlock);
149 : :
150 [ - + ]: 34776 : if (!service)
151 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
152 : : "Invalid service handle 0x%x", handle);
153 : :
154 : 34776 : return service;
155 : : }
156 : :
157 : : struct vchiq_service *
158 : 9936 : find_service_by_port(struct vchiq_state *state, int localport)
159 : : {
160 : : struct vchiq_service *service = NULL;
161 : :
162 [ + - ]: 9936 : if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
163 : : spin_lock(&service_spinlock);
164 : 9936 : service = state->services[localport];
165 [ + - + - ]: 9936 : if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
166 [ - + ]: 9936 : WARN_ON(service->ref_count == 0);
167 : 9936 : service->ref_count++;
168 : : } else
169 : : service = NULL;
170 : : spin_unlock(&service_spinlock);
171 : : }
172 : :
173 [ - + ]: 9936 : if (!service)
174 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
175 : : "Invalid port %d", localport);
176 : :
177 : 9936 : return service;
178 : : }
179 : :
180 : : struct vchiq_service *
181 : 0 : find_service_for_instance(VCHIQ_INSTANCE_T instance,
182 : : VCHIQ_SERVICE_HANDLE_T handle)
183 : : {
184 : : struct vchiq_service *service;
185 : :
186 : : spin_lock(&service_spinlock);
187 : : service = handle_to_service(handle);
188 [ # # # # : 0 : if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
# # ]
189 [ # # ]: 0 : (service->handle == handle) &&
190 : 0 : (service->instance == instance)) {
191 [ # # ]: 0 : WARN_ON(service->ref_count == 0);
192 : 0 : service->ref_count++;
193 : : } else
194 : : service = NULL;
195 : : spin_unlock(&service_spinlock);
196 : :
197 [ # # ]: 0 : if (!service)
198 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
199 : : "Invalid service handle 0x%x", handle);
200 : :
201 : 0 : return service;
202 : : }
203 : :
204 : : struct vchiq_service *
205 : 0 : find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
206 : : VCHIQ_SERVICE_HANDLE_T handle)
207 : : {
208 : : struct vchiq_service *service;
209 : :
210 : : spin_lock(&service_spinlock);
211 : : service = handle_to_service(handle);
212 [ # # # # ]: 0 : if (service &&
213 : 0 : ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
214 [ # # ]: 0 : (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
215 [ # # ]: 0 : (service->handle == handle) &&
216 : 0 : (service->instance == instance)) {
217 [ # # ]: 0 : WARN_ON(service->ref_count == 0);
218 : 0 : service->ref_count++;
219 : : } else
220 : : service = NULL;
221 : : spin_unlock(&service_spinlock);
222 : :
223 [ # # ]: 0 : if (!service)
224 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
225 : : "Invalid service handle 0x%x", handle);
226 : :
227 : 0 : return service;
228 : : }
229 : :
230 : : struct vchiq_service *
231 : 1863 : next_service_by_instance(struct vchiq_state *state, VCHIQ_INSTANCE_T instance,
232 : : int *pidx)
233 : : {
234 : : struct vchiq_service *service = NULL;
235 : 1863 : int idx = *pidx;
236 : :
237 : : spin_lock(&service_spinlock);
238 [ + + ]: 7849 : while (idx < state->unused_service) {
239 : 5986 : struct vchiq_service *srv = state->services[idx++];
240 : :
241 [ + + + + : 11804 : if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
- + ]
242 : 5818 : (srv->instance == instance)) {
243 : 0 : service = srv;
244 [ # # ]: 0 : WARN_ON(service->ref_count == 0);
245 : 0 : service->ref_count++;
246 : 0 : break;
247 : : }
248 : : }
249 : : spin_unlock(&service_spinlock);
250 : :
251 : 1863 : *pidx = idx;
252 : :
253 : 1863 : return service;
254 : : }
255 : :
256 : : void
257 : 0 : lock_service(struct vchiq_service *service)
258 : : {
259 : : spin_lock(&service_spinlock);
260 [ # # ]: 0 : WARN_ON(!service);
261 [ # # ]: 0 : if (service) {
262 [ # # ]: 0 : WARN_ON(service->ref_count == 0);
263 : 0 : service->ref_count++;
264 : : }
265 : : spin_unlock(&service_spinlock);
266 : 0 : }
267 : :
268 : : void
269 : 44914 : unlock_service(struct vchiq_service *service)
270 : : {
271 : : spin_lock(&service_spinlock);
272 [ - + ]: 44919 : if (!service) {
273 : 0 : WARN(1, "%s: service is NULL\n", __func__);
274 : 0 : goto unlock;
275 : : }
276 [ - + ]: 44919 : if (!service->ref_count) {
277 : 0 : WARN(1, "%s: ref_count is zero\n", __func__);
278 : 0 : goto unlock;
279 : : }
280 : 44919 : service->ref_count--;
281 [ + + ]: 44919 : if (!service->ref_count) {
282 : 207 : struct vchiq_state *state = service->state;
283 : :
284 [ - + ]: 207 : WARN_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
285 : 207 : state->services[service->localport] = NULL;
286 : : } else {
287 : : service = NULL;
288 : : }
289 : : unlock:
290 : : spin_unlock(&service_spinlock);
291 : :
292 [ + + - + ]: 44919 : if (service && service->userdata_term)
293 : 0 : service->userdata_term(service->base.userdata);
294 : :
295 : 44919 : kfree(service);
296 : 44919 : }
297 : :
298 : : int
299 : 0 : vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
300 : : {
301 : 0 : struct vchiq_service *service = find_service_by_handle(handle);
302 : : int id;
303 : :
304 [ # # ]: 0 : id = service ? service->client_id : 0;
305 [ # # ]: 0 : if (service)
306 : 0 : unlock_service(service);
307 : :
308 : 0 : return id;
309 : : }
310 : :
311 : : void *
312 : 8280 : vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
313 : : {
314 : : struct vchiq_service *service = handle_to_service(handle);
315 : :
316 [ + - ]: 8280 : return service ? service->base.userdata : NULL;
317 : : }
318 : :
319 : : int
320 : 0 : vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
321 : : {
322 : : struct vchiq_service *service = handle_to_service(handle);
323 : :
324 [ # # ]: 0 : return service ? service->base.fourcc : 0;
325 : : }
326 : :
327 : : static void
328 : 414 : mark_service_closing_internal(struct vchiq_service *service, int sh_thread)
329 : : {
330 : 414 : struct vchiq_state *state = service->state;
331 : : struct vchiq_service_quota *service_quota;
332 : :
333 : 414 : service->closing = 1;
334 : :
335 : : /* Synchronise with other threads. */
336 : 414 : mutex_lock(&state->recycle_mutex);
337 : 414 : mutex_unlock(&state->recycle_mutex);
338 [ + + + - ]: 414 : if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
339 : : /* If we're pausing then the slot_mutex is held until resume
340 : : * by the slot handler. Therefore don't try to acquire this
341 : : * mutex if we're the slot handler and in the pause sent state.
342 : : * We don't need to in this case anyway. */
343 : 414 : mutex_lock(&state->slot_mutex);
344 : 414 : mutex_unlock(&state->slot_mutex);
345 : : }
346 : :
347 : : /* Unblock any sending thread. */
348 : 414 : service_quota = &state->service_quotas[service->localport];
349 : 414 : complete(&service_quota->quota_event);
350 : 414 : }
351 : :
352 : : static void
353 : : mark_service_closing(struct vchiq_service *service)
354 : : {
355 : 207 : mark_service_closing_internal(service, 0);
356 : : }
357 : :
358 : : static inline VCHIQ_STATUS_T
359 : 8280 : make_service_callback(struct vchiq_service *service, VCHIQ_REASON_T reason,
360 : : struct vchiq_header *header, void *bulk_userdata)
361 : : {
362 : : VCHIQ_STATUS_T status;
363 : :
364 [ - + ]: 8280 : vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %pK, %pK)",
365 : : service->state->id, service->localport, reason_names[reason],
366 : : header, bulk_userdata);
367 : 8280 : status = service->base.callback(reason, header, service->handle,
368 : : bulk_userdata);
369 [ - + ]: 8280 : if (status == VCHIQ_ERROR) {
370 [ # # ]: 0 : vchiq_log_warning(vchiq_core_log_level,
371 : : "%d: ignoring ERROR from callback to service %x",
372 : : service->state->id, service->handle);
373 : : status = VCHIQ_SUCCESS;
374 : : }
375 : 8280 : return status;
376 : : }
377 : :
378 : : inline void
379 : 414 : vchiq_set_conn_state(struct vchiq_state *state, VCHIQ_CONNSTATE_T newstate)
380 : : {
381 : 414 : VCHIQ_CONNSTATE_T oldstate = state->conn_state;
382 : :
383 [ - + ]: 414 : vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
384 : : conn_state_names[oldstate],
385 : : conn_state_names[newstate]);
386 : 414 : state->conn_state = newstate;
387 : 414 : vchiq_platform_conn_state_changed(state, oldstate, newstate);
388 : 414 : }
389 : :
390 : : static inline void
391 : : remote_event_create(wait_queue_head_t *wq, struct remote_event *event)
392 : : {
393 : 828 : event->armed = 0;
394 : : /* Don't clear the 'fired' flag because it may already have been set
395 : : ** by the other side. */
396 : 828 : init_waitqueue_head(wq);
397 : : }
398 : :
399 : : /*
400 : : * All the event waiting routines in VCHIQ used a custom semaphore
401 : : * implementation that filtered most signals. This achieved a behaviour similar
402 : : * to the "killable" family of functions. While cleaning up this code all the
403 : : * routines where switched to the "interruptible" family of functions, as the
404 : : * former was deemed unjustified and the use "killable" set all VCHIQ's
405 : : * threads in D state.
406 : : */
407 : : static inline int
408 : 10524 : remote_event_wait(wait_queue_head_t *wq, struct remote_event *event)
409 : : {
410 [ + + ]: 10524 : if (!event->fired) {
411 : 10450 : event->armed = 1;
412 : 10450 : dsb(sy);
413 [ + + + + : 10448 : if (wait_event_interruptible(*wq, event->fired)) {
- + - + ]
414 : 0 : event->armed = 0;
415 : 0 : return 0;
416 : : }
417 : 9829 : event->armed = 0;
418 : 9829 : wmb();
419 : : }
420 : :
421 : 9910 : event->fired = 0;
422 : 9910 : return 1;
423 : : }
424 : :
425 : : static inline void
426 : : remote_event_signal_local(wait_queue_head_t *wq, struct remote_event *event)
427 : : {
428 : 10030 : event->fired = 1;
429 : 10030 : event->armed = 0;
430 : 10030 : wake_up_all(wq);
431 : : }
432 : :
433 : : static inline void
434 : 38536 : remote_event_poll(wait_queue_head_t *wq, struct remote_event *event)
435 : : {
436 [ + + + + ]: 38536 : if (event->fired && event->armed)
437 : : remote_event_signal_local(wq, event);
438 : 38536 : }
439 : :
440 : : void
441 : 9634 : remote_event_pollall(struct vchiq_state *state)
442 : : {
443 : 9634 : remote_event_poll(&state->sync_trigger_event, &state->local->sync_trigger);
444 : 9634 : remote_event_poll(&state->sync_release_event, &state->local->sync_release);
445 : 9634 : remote_event_poll(&state->trigger_event, &state->local->trigger);
446 : 9634 : remote_event_poll(&state->recycle_event, &state->local->recycle);
447 : 9634 : }
448 : :
449 : : /* Round up message sizes so that any space at the end of a slot is always big
450 : : ** enough for a header. This relies on header size being a power of two, which
451 : : ** has been verified earlier by a static assertion. */
452 : :
453 : : static inline size_t
454 : : calc_stride(size_t size)
455 : : {
456 : : /* Allow room for the header */
457 : : size += sizeof(struct vchiq_header);
458 : :
459 : : /* Round up */
460 : 33470 : return (size + sizeof(struct vchiq_header) - 1) &
461 : : ~(sizeof(struct vchiq_header) - 1);
462 : : }
463 : :
464 : : /* Called by the slot handler thread */
465 : : static struct vchiq_service *
466 : 0 : get_listening_service(struct vchiq_state *state, int fourcc)
467 : : {
468 : : int i;
469 : :
470 [ # # ]: 0 : WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
471 : :
472 [ # # ]: 0 : for (i = 0; i < state->unused_service; i++) {
473 : 0 : struct vchiq_service *service = state->services[i];
474 : :
475 [ # # # # ]: 0 : if (service &&
476 [ # # ]: 0 : (service->public_fourcc == fourcc) &&
477 [ # # ]: 0 : ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
478 [ # # ]: 0 : ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
479 : 0 : (service->remoteport == VCHIQ_PORT_FREE)))) {
480 : 0 : lock_service(service);
481 : 0 : return service;
482 : : }
483 : : }
484 : :
485 : : return NULL;
486 : : }
487 : :
488 : : /* Called by the slot handler thread */
489 : : static struct vchiq_service *
490 : 0 : get_connected_service(struct vchiq_state *state, unsigned int port)
491 : : {
492 : : int i;
493 : :
494 [ # # ]: 0 : for (i = 0; i < state->unused_service; i++) {
495 : 0 : struct vchiq_service *service = state->services[i];
496 : :
497 [ # # # # ]: 0 : if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
498 [ # # ]: 0 : && (service->remoteport == port)) {
499 : 0 : lock_service(service);
500 : 0 : return service;
501 : : }
502 : : }
503 : : return NULL;
504 : : }
505 : :
506 : : inline void
507 : 207 : request_poll(struct vchiq_state *state, struct vchiq_service *service,
508 : : int poll_type)
509 : : {
510 : : u32 value;
511 : :
512 [ + - ]: 207 : if (service) {
513 : : do {
514 : 207 : value = atomic_read(&service->poll_flags);
515 : 414 : } while (atomic_cmpxchg(&service->poll_flags, value,
516 [ - + ]: 414 : value | (1 << poll_type)) != value);
517 : :
518 : : do {
519 : 414 : value = atomic_read(&state->poll_services[
520 : : service->localport>>5]);
521 : 414 : } while (atomic_cmpxchg(
522 : : &state->poll_services[service->localport>>5],
523 : 207 : value, value | (1 << (service->localport & 0x1f)))
524 [ - + ]: 207 : != value);
525 : : }
526 : :
527 : 207 : state->poll_needed = 1;
528 : 207 : wmb();
529 : :
530 : : /* ... and ensure the slot handler runs. */
531 : 207 : remote_event_signal_local(&state->trigger_event, &state->local->trigger);
532 : 207 : }
533 : :
534 : : /* Called from queue_message, by the slot handler and application threads,
535 : : ** with slot_mutex held */
536 : : static struct vchiq_header *
537 : 9936 : reserve_space(struct vchiq_state *state, size_t space, int is_blocking)
538 : : {
539 : 9936 : struct vchiq_shared_state *local = state->local;
540 : 9936 : int tx_pos = state->local_tx_pos;
541 : 9936 : int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
542 : :
543 [ - + ]: 9936 : if (space > slot_space) {
544 : : struct vchiq_header *header;
545 : : /* Fill the remaining space with padding */
546 [ # # ]: 0 : WARN_ON(state->tx_data == NULL);
547 : 0 : header = (struct vchiq_header *)
548 : 0 : (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
549 : 0 : header->msgid = VCHIQ_MSGID_PADDING;
550 : 0 : header->size = slot_space - sizeof(struct vchiq_header);
551 : :
552 : 0 : tx_pos += slot_space;
553 : : }
554 : :
555 : : /* If necessary, get the next slot. */
556 [ + + ]: 9936 : if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
557 : : int slot_index;
558 : :
559 : : /* If there is no free slot... */
560 : :
561 [ - + ]: 207 : if (!try_wait_for_completion(&state->slot_available_event)) {
562 : : /* ...wait for one. */
563 : :
564 : 0 : VCHIQ_STATS_INC(state, slot_stalls);
565 : :
566 : : /* But first, flush through the last slot. */
567 : 0 : state->local_tx_pos = tx_pos;
568 : 0 : local->tx_pos = tx_pos;
569 : 0 : remote_event_signal(&state->remote->trigger);
570 : :
571 [ # # # # ]: 0 : if (!is_blocking ||
572 : 0 : (wait_for_completion_interruptible(
573 : : &state->slot_available_event)))
574 : : return NULL; /* No space available */
575 : : }
576 : :
577 [ - + ]: 207 : if (tx_pos == (state->slot_queue_available * VCHIQ_SLOT_SIZE)) {
578 : 0 : complete(&state->slot_available_event);
579 : 0 : pr_warn("%s: invalid tx_pos: %d\n", __func__, tx_pos);
580 : 0 : return NULL;
581 : : }
582 : :
583 : 207 : slot_index = local->slot_queue[
584 : 207 : SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
585 : : VCHIQ_SLOT_QUEUE_MASK];
586 : 207 : state->tx_data =
587 : 207 : (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
588 : : }
589 : :
590 : 9936 : state->local_tx_pos = tx_pos + space;
591 : :
592 : 9936 : return (struct vchiq_header *)(state->tx_data +
593 : : (tx_pos & VCHIQ_SLOT_MASK));
594 : : }
595 : :
596 : : /* Called by the recycle thread. */
597 : : static void
598 : 0 : process_free_queue(struct vchiq_state *state, BITSET_T *service_found,
599 : : size_t length)
600 : : {
601 : 0 : struct vchiq_shared_state *local = state->local;
602 : : int slot_queue_available;
603 : :
604 : : /* Find slots which have been freed by the other side, and return them
605 : : ** to the available queue. */
606 : 0 : slot_queue_available = state->slot_queue_available;
607 : :
608 : : /*
609 : : * Use a memory barrier to ensure that any state that may have been
610 : : * modified by another thread is not masked by stale prefetched
611 : : * values.
612 : : */
613 : 0 : mb();
614 : :
615 [ # # ]: 0 : while (slot_queue_available != local->slot_queue_recycle) {
616 : : unsigned int pos;
617 : 0 : int slot_index = local->slot_queue[slot_queue_available++ &
618 : : VCHIQ_SLOT_QUEUE_MASK];
619 : 0 : char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
620 : : int data_found = 0;
621 : :
622 : : /*
623 : : * Beware of the address dependency - data is calculated
624 : : * using an index written by the other side.
625 : : */
626 : 0 : rmb();
627 : :
628 [ # # ]: 0 : vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%pK %x %x",
629 : : state->id, slot_index, data,
630 : : local->slot_queue_recycle, slot_queue_available);
631 : :
632 : : /* Initialise the bitmask for services which have used this
633 : : ** slot */
634 : 0 : memset(service_found, 0, length);
635 : :
636 : : pos = 0;
637 : :
638 [ # # ]: 0 : while (pos < VCHIQ_SLOT_SIZE) {
639 : 0 : struct vchiq_header *header =
640 : : (struct vchiq_header *)(data + pos);
641 : 0 : int msgid = header->msgid;
642 : :
643 [ # # ]: 0 : if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
644 : 0 : int port = VCHIQ_MSG_SRCPORT(msgid);
645 : : struct vchiq_service_quota *service_quota =
646 : : &state->service_quotas[port];
647 : : int count;
648 : :
649 : : spin_lock("a_spinlock);
650 : 0 : count = service_quota->message_use_count;
651 [ # # ]: 0 : if (count > 0)
652 : 0 : service_quota->message_use_count =
653 : : count - 1;
654 : : spin_unlock("a_spinlock);
655 : :
656 [ # # ]: 0 : if (count == service_quota->message_quota)
657 : : /* Signal the service that it
658 : : ** has dropped below its quota
659 : : */
660 : 0 : complete(&service_quota->quota_event);
661 [ # # ]: 0 : else if (count == 0) {
662 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
663 : : "service %d message_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
664 : : port,
665 : : service_quota->message_use_count,
666 : : header, msgid, header->msgid,
667 : : header->size);
668 : 0 : WARN(1, "invalid message use count\n");
669 : : }
670 [ # # ]: 0 : if (!BITSET_IS_SET(service_found, port)) {
671 : : /* Set the found bit for this service */
672 : 0 : BITSET_SET(service_found, port);
673 : :
674 : : spin_lock("a_spinlock);
675 : 0 : count = service_quota->slot_use_count;
676 [ # # ]: 0 : if (count > 0)
677 : 0 : service_quota->slot_use_count =
678 : : count - 1;
679 : : spin_unlock("a_spinlock);
680 : :
681 [ # # ]: 0 : if (count > 0) {
682 : : /* Signal the service in case
683 : : ** it has dropped below its
684 : : ** quota */
685 : 0 : complete(&service_quota->quota_event);
686 [ # # ]: 0 : vchiq_log_trace(
687 : : vchiq_core_log_level,
688 : : "%d: pfq:%d %x@%pK - slot_use->%d",
689 : : state->id, port,
690 : : header->size, header,
691 : : count - 1);
692 : : } else {
693 [ # # ]: 0 : vchiq_log_error(
694 : : vchiq_core_log_level,
695 : : "service %d slot_use_count=%d (header %pK, msgid %x, header->msgid %x, header->size %x)",
696 : : port, count, header,
697 : : msgid, header->msgid,
698 : : header->size);
699 : 0 : WARN(1, "bad slot use count\n");
700 : : }
701 : : }
702 : :
703 : : data_found = 1;
704 : : }
705 : :
706 : 0 : pos += calc_stride(header->size);
707 [ # # ]: 0 : if (pos > VCHIQ_SLOT_SIZE) {
708 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
709 : : "pfq - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
710 : : pos, header, msgid, header->msgid,
711 : : header->size);
712 : 0 : WARN(1, "invalid slot position\n");
713 : : }
714 : : }
715 : :
716 [ # # ]: 0 : if (data_found) {
717 : : int count;
718 : :
719 : : spin_lock("a_spinlock);
720 : 0 : count = state->data_use_count;
721 [ # # ]: 0 : if (count > 0)
722 : 0 : state->data_use_count =
723 : : count - 1;
724 : : spin_unlock("a_spinlock);
725 [ # # ]: 0 : if (count == state->data_quota)
726 : 0 : complete(&state->data_quota_event);
727 : : }
728 : :
729 : : /*
730 : : * Don't allow the slot to be reused until we are no
731 : : * longer interested in it.
732 : : */
733 : 0 : mb();
734 : :
735 : 0 : state->slot_queue_available = slot_queue_available;
736 : 0 : complete(&state->slot_available_event);
737 : : }
738 : 0 : }
739 : :
740 : : static ssize_t
741 : 1449 : memcpy_copy_callback(
742 : : void *context, void *dest,
743 : : size_t offset, size_t maxsize)
744 : : {
745 : 1449 : memcpy(dest + offset, context + offset, maxsize);
746 : 1449 : return maxsize;
747 : : }
748 : :
749 : : static ssize_t
750 : 9522 : copy_message_data(
751 : : ssize_t (*copy_callback)(void *context, void *dest,
752 : : size_t offset, size_t maxsize),
753 : : void *context,
754 : : void *dest,
755 : : size_t size)
756 : : {
757 : : size_t pos = 0;
758 : :
759 [ + + ]: 28566 : while (pos < size) {
760 : : ssize_t callback_result;
761 : 9522 : size_t max_bytes = size - pos;
762 : :
763 : 9522 : callback_result =
764 : 9522 : copy_callback(context, dest + pos,
765 : : pos, max_bytes);
766 : :
767 [ - + ]: 9522 : if (callback_result < 0)
768 : 0 : return callback_result;
769 : :
770 [ + - ]: 9522 : if (!callback_result)
771 : : return -EIO;
772 : :
773 [ + - ]: 9522 : if (callback_result > max_bytes)
774 : : return -EIO;
775 : :
776 : 9522 : pos += callback_result;
777 : : }
778 : :
779 : 9522 : return size;
780 : : }
781 : :
782 : : /* Called by the slot handler and application threads */
783 : : static VCHIQ_STATUS_T
784 : 9936 : queue_message(struct vchiq_state *state, struct vchiq_service *service,
785 : : int msgid,
786 : : ssize_t (*copy_callback)(void *context, void *dest,
787 : : size_t offset, size_t maxsize),
788 : : void *context, size_t size, int flags)
789 : : {
790 : : struct vchiq_shared_state *local;
791 : : struct vchiq_service_quota *service_quota = NULL;
792 : : struct vchiq_header *header;
793 : 9936 : int type = VCHIQ_MSG_TYPE(msgid);
794 : :
795 : : size_t stride;
796 : :
797 : 9936 : local = state->local;
798 : :
799 : : stride = calc_stride(size);
800 : :
801 [ - + ]: 9936 : WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
802 : :
803 [ + - + - ]: 19870 : if (!(flags & QMFLAGS_NO_MUTEX_LOCK) &&
804 : 9934 : mutex_lock_killable(&state->slot_mutex))
805 : : return VCHIQ_RETRY;
806 : :
807 [ + + ]: 9936 : if (type == VCHIQ_MSG_DATA) {
808 : : int tx_end_index;
809 : :
810 [ - + ]: 8073 : if (!service) {
811 : 0 : WARN(1, "%s: service is NULL\n", __func__);
812 : 0 : mutex_unlock(&state->slot_mutex);
813 : 0 : return VCHIQ_ERROR;
814 : : }
815 : :
816 [ - + ]: 8073 : WARN_ON(flags & (QMFLAGS_NO_MUTEX_LOCK |
817 : : QMFLAGS_NO_MUTEX_UNLOCK));
818 : :
819 [ - + ]: 8073 : if (service->closing) {
820 : : /* The service has been closed */
821 : 0 : mutex_unlock(&state->slot_mutex);
822 : 0 : return VCHIQ_ERROR;
823 : : }
824 : :
825 : 8073 : service_quota = &state->service_quotas[service->localport];
826 : :
827 : : spin_lock("a_spinlock);
828 : :
829 : : /* Ensure this service doesn't use more than its quota of
830 : : ** messages or slots */
831 : 8073 : tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
832 : : state->local_tx_pos + stride - 1);
833 : :
834 : : /* Ensure data messages don't use more than their quota of
835 : : ** slots */
836 [ + + - + ]: 16353 : while ((tx_end_index != state->previous_data_index) &&
837 : 207 : (state->data_use_count == state->data_quota)) {
838 : 0 : VCHIQ_STATS_INC(state, data_stalls);
839 : : spin_unlock("a_spinlock);
840 : 0 : mutex_unlock(&state->slot_mutex);
841 : :
842 [ # # ]: 0 : if (wait_for_completion_interruptible(
843 : : &state->data_quota_event))
844 : : return VCHIQ_RETRY;
845 : :
846 : 0 : mutex_lock(&state->slot_mutex);
847 : : spin_lock("a_spinlock);
848 : 0 : tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
849 : : state->local_tx_pos + stride - 1);
850 [ # # # # ]: 0 : if ((tx_end_index == state->previous_data_index) ||
851 : 0 : (state->data_use_count < state->data_quota)) {
852 : : /* Pass the signal on to other waiters */
853 : 0 : complete(&state->data_quota_event);
854 : 0 : break;
855 : : }
856 : : }
857 : :
858 [ - + ]: 16146 : while ((service_quota->message_use_count ==
859 [ + + ]: 16146 : service_quota->message_quota) ||
860 [ - + ]: 9129 : ((tx_end_index != service_quota->previous_tx_index) &&
861 : 1056 : (service_quota->slot_use_count ==
862 : 1056 : service_quota->slot_quota))) {
863 : : spin_unlock("a_spinlock);
864 [ # # # # ]: 0 : vchiq_log_trace(vchiq_core_log_level,
865 : : "%d: qm:%d %s,%zx - quota stall "
866 : : "(msg %d, slot %d)",
867 : : state->id, service->localport,
868 : : msg_type_str(type), size,
869 : : service_quota->message_use_count,
870 : : service_quota->slot_use_count);
871 : 0 : VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
872 : 0 : mutex_unlock(&state->slot_mutex);
873 [ # # ]: 0 : if (wait_for_completion_interruptible(
874 : : &service_quota->quota_event))
875 : : return VCHIQ_RETRY;
876 [ # # ]: 0 : if (service->closing)
877 : : return VCHIQ_ERROR;
878 [ # # ]: 0 : if (mutex_lock_killable(&state->slot_mutex))
879 : : return VCHIQ_RETRY;
880 [ # # ]: 0 : if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
881 : : /* The service has been closed */
882 : 0 : mutex_unlock(&state->slot_mutex);
883 : 0 : return VCHIQ_ERROR;
884 : : }
885 : : spin_lock("a_spinlock);
886 : 0 : tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
887 : : state->local_tx_pos + stride - 1);
888 : : }
889 : :
890 : : spin_unlock("a_spinlock);
891 : : }
892 : :
893 : 9936 : header = reserve_space(state, stride, flags & QMFLAGS_IS_BLOCKING);
894 : :
895 [ - + ]: 9936 : if (!header) {
896 [ # # ]: 0 : if (service)
897 : 0 : VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
898 : : /* In the event of a failure, return the mutex to the
899 : : state it was in */
900 [ # # ]: 0 : if (!(flags & QMFLAGS_NO_MUTEX_LOCK))
901 : 0 : mutex_unlock(&state->slot_mutex);
902 : : return VCHIQ_RETRY;
903 : : }
904 : :
905 [ + + ]: 9936 : if (type == VCHIQ_MSG_DATA) {
906 : : ssize_t callback_result;
907 : : int tx_end_index;
908 : : int slot_use_count;
909 : :
910 [ - + # # ]: 8073 : vchiq_log_info(vchiq_core_log_level,
911 : : "%d: qm %s@%pK,%zx (%d->%d)",
912 : : state->id, msg_type_str(VCHIQ_MSG_TYPE(msgid)),
913 : : header, size, VCHIQ_MSG_SRCPORT(msgid),
914 : : VCHIQ_MSG_DSTPORT(msgid));
915 : :
916 [ - + ]: 8073 : WARN_ON(flags & (QMFLAGS_NO_MUTEX_LOCK |
917 : : QMFLAGS_NO_MUTEX_UNLOCK));
918 : :
919 : 8073 : callback_result =
920 : : copy_message_data(copy_callback, context,
921 : 8073 : header->data, size);
922 : :
923 [ - + ]: 8073 : if (callback_result < 0) {
924 : 0 : mutex_unlock(&state->slot_mutex);
925 : 0 : VCHIQ_SERVICE_STATS_INC(service,
926 : : error_count);
927 : 0 : return VCHIQ_ERROR;
928 : : }
929 : :
930 [ + - + - : 8073 : if (SRVTRACE_ENABLED(service,
- + ]
931 : : VCHIQ_LOG_INFO))
932 : 0 : vchiq_log_dump_mem("Sent", 0,
933 : : header->data,
934 : 0 : min((size_t)16,
935 : : (size_t)callback_result));
936 : :
937 : : spin_lock("a_spinlock);
938 : 8073 : service_quota->message_use_count++;
939 : :
940 : 8073 : tx_end_index =
941 : 8073 : SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
942 : :
943 : : /* If this transmission can't fit in the last slot used by any
944 : : ** service, the data_use_count must be increased. */
945 [ + + ]: 8073 : if (tx_end_index != state->previous_data_index) {
946 : 207 : state->previous_data_index = tx_end_index;
947 : 207 : state->data_use_count++;
948 : : }
949 : :
950 : : /* If this isn't the same slot last used by this service,
951 : : ** the service's slot_use_count must be increased. */
952 [ + + ]: 8073 : if (tx_end_index != service_quota->previous_tx_index) {
953 : 1056 : service_quota->previous_tx_index = tx_end_index;
954 : 1056 : slot_use_count = ++service_quota->slot_use_count;
955 : : } else {
956 : : slot_use_count = 0;
957 : : }
958 : :
959 : : spin_unlock("a_spinlock);
960 : :
961 [ + + ]: 8073 : if (slot_use_count)
962 [ - + # # ]: 1056 : vchiq_log_trace(vchiq_core_log_level,
963 : : "%d: qm:%d %s,%zx - slot_use->%d (hdr %p)",
964 : : state->id, service->localport,
965 : : msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
966 : : slot_use_count, header);
967 : :
968 : 8073 : VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
969 : 8073 : VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
970 : : } else {
971 [ - + # # ]: 1863 : vchiq_log_info(vchiq_core_log_level,
972 : : "%d: qm %s@%pK,%zx (%d->%d)", state->id,
973 : : msg_type_str(VCHIQ_MSG_TYPE(msgid)),
974 : : header, size, VCHIQ_MSG_SRCPORT(msgid),
975 : : VCHIQ_MSG_DSTPORT(msgid));
976 [ + + ]: 1863 : if (size != 0) {
977 : : /* It is assumed for now that this code path
978 : : * only happens from calls inside this file.
979 : : *
980 : : * External callers are through the vchiq_queue_message
981 : : * path which always sets the type to be VCHIQ_MSG_DATA
982 : : *
983 : : * At first glance this appears to be correct but
984 : : * more review is needed.
985 : : */
986 : 1449 : copy_message_data(copy_callback, context,
987 : 1449 : header->data, size);
988 : : }
989 : 1863 : VCHIQ_STATS_INC(state, ctrl_tx_count);
990 : : }
991 : :
992 : 9936 : header->msgid = msgid;
993 : 9936 : header->size = size;
994 : :
995 : : {
996 : : int svc_fourcc;
997 : :
998 : : svc_fourcc = service
999 : : ? service->base.fourcc
1000 [ + + ]: 9936 : : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1001 : :
1002 [ + + + - : 9936 : vchiq_log_info(SRVTRACE_LEVEL(service),
- + # # ]
1003 : : "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%zu",
1004 : : msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1005 : : VCHIQ_MSG_TYPE(msgid),
1006 : : VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1007 : : VCHIQ_MSG_SRCPORT(msgid),
1008 : : VCHIQ_MSG_DSTPORT(msgid),
1009 : : size);
1010 : : }
1011 : :
1012 : : /* Make sure the new header is visible to the peer. */
1013 : 9936 : wmb();
1014 : :
1015 : : /* Make the new tx_pos visible to the peer. */
1016 : 9936 : local->tx_pos = state->local_tx_pos;
1017 : 9936 : wmb();
1018 : :
1019 [ + + ]: 9936 : if (service && (type == VCHIQ_MSG_CLOSE))
1020 : 207 : vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
1021 : :
1022 [ + + ]: 9936 : if (!(flags & QMFLAGS_NO_MUTEX_UNLOCK))
1023 : 9729 : mutex_unlock(&state->slot_mutex);
1024 : :
1025 : 9936 : remote_event_signal(&state->remote->trigger);
1026 : :
1027 : 9936 : return VCHIQ_SUCCESS;
1028 : : }
1029 : :
1030 : : /* Called by the slot handler and application threads */
1031 : : static VCHIQ_STATUS_T
1032 : 0 : queue_message_sync(struct vchiq_state *state, struct vchiq_service *service,
1033 : : int msgid,
1034 : : ssize_t (*copy_callback)(void *context, void *dest,
1035 : : size_t offset, size_t maxsize),
1036 : : void *context, int size, int is_blocking)
1037 : : {
1038 : : struct vchiq_shared_state *local;
1039 : : struct vchiq_header *header;
1040 : : ssize_t callback_result;
1041 : :
1042 : 0 : local = state->local;
1043 : :
1044 [ # # # # ]: 0 : if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME &&
1045 : 0 : mutex_lock_killable(&state->sync_mutex))
1046 : : return VCHIQ_RETRY;
1047 : :
1048 : 0 : remote_event_wait(&state->sync_release_event, &local->sync_release);
1049 : :
1050 : 0 : rmb();
1051 : :
1052 : 0 : header = (struct vchiq_header *)SLOT_DATA_FROM_INDEX(state,
1053 : : local->slot_sync);
1054 : :
1055 : : {
1056 : 0 : int oldmsgid = header->msgid;
1057 : :
1058 [ # # ]: 0 : if (oldmsgid != VCHIQ_MSGID_PADDING)
1059 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1060 : : "%d: qms - msgid %x, not PADDING",
1061 : : state->id, oldmsgid);
1062 : : }
1063 : :
1064 [ # # # # ]: 0 : vchiq_log_info(vchiq_sync_log_level,
1065 : : "%d: qms %s@%pK,%x (%d->%d)", state->id,
1066 : : msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1067 : : header, size, VCHIQ_MSG_SRCPORT(msgid),
1068 : : VCHIQ_MSG_DSTPORT(msgid));
1069 : :
1070 : 0 : callback_result =
1071 : 0 : copy_message_data(copy_callback, context,
1072 : 0 : header->data, size);
1073 : :
1074 [ # # ]: 0 : if (callback_result < 0) {
1075 : 0 : mutex_unlock(&state->slot_mutex);
1076 : 0 : VCHIQ_SERVICE_STATS_INC(service,
1077 : : error_count);
1078 : 0 : return VCHIQ_ERROR;
1079 : : }
1080 : :
1081 [ # # ]: 0 : if (service) {
1082 [ # # # # : 0 : if (SRVTRACE_ENABLED(service,
# # ]
1083 : : VCHIQ_LOG_INFO))
1084 : 0 : vchiq_log_dump_mem("Sent", 0,
1085 : : header->data,
1086 : 0 : min((size_t)16,
1087 : : (size_t)callback_result));
1088 : :
1089 : 0 : VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
1090 : 0 : VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
1091 : : } else {
1092 : 0 : VCHIQ_STATS_INC(state, ctrl_tx_count);
1093 : : }
1094 : :
1095 : 0 : header->size = size;
1096 : 0 : header->msgid = msgid;
1097 : :
1098 [ # # ]: 0 : if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
1099 : : int svc_fourcc;
1100 : :
1101 : : svc_fourcc = service
1102 : : ? service->base.fourcc
1103 [ # # ]: 0 : : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1104 : :
1105 [ # # # # ]: 0 : vchiq_log_trace(vchiq_sync_log_level,
1106 : : "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
1107 : : msg_type_str(VCHIQ_MSG_TYPE(msgid)),
1108 : : VCHIQ_MSG_TYPE(msgid),
1109 : : VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1110 : : VCHIQ_MSG_SRCPORT(msgid),
1111 : : VCHIQ_MSG_DSTPORT(msgid),
1112 : : size);
1113 : : }
1114 : :
1115 : 0 : remote_event_signal(&state->remote->sync_trigger);
1116 : :
1117 [ # # ]: 0 : if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
1118 : 0 : mutex_unlock(&state->sync_mutex);
1119 : :
1120 : : return VCHIQ_SUCCESS;
1121 : : }
1122 : :
1123 : : static inline void
1124 : : claim_slot(struct vchiq_slot_info *slot)
1125 : : {
1126 : 8073 : slot->use_count++;
1127 : : }
1128 : :
1129 : : static void
1130 : 8277 : release_slot(struct vchiq_state *state, struct vchiq_slot_info *slot_info,
1131 : : struct vchiq_header *header, struct vchiq_service *service)
1132 : : {
1133 : : int release_count;
1134 : :
1135 : 8277 : mutex_lock(&state->recycle_mutex);
1136 : :
1137 [ + + ]: 8280 : if (header) {
1138 : 8073 : int msgid = header->msgid;
1139 : :
1140 [ + - + - ]: 8073 : if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
1141 [ - + ]: 8073 : (service && service->closing)) {
1142 : 0 : mutex_unlock(&state->recycle_mutex);
1143 : 8280 : return;
1144 : : }
1145 : :
1146 : : /* Rewrite the message header to prevent a double
1147 : : ** release */
1148 : 8073 : header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
1149 : : }
1150 : :
1151 : 8280 : release_count = slot_info->release_count;
1152 : 8280 : slot_info->release_count = ++release_count;
1153 : :
1154 [ + + ]: 8280 : if (release_count == slot_info->use_count) {
1155 : : int slot_queue_recycle;
1156 : : /* Add to the freed queue */
1157 : :
1158 : : /* A read barrier is necessary here to prevent speculative
1159 : : ** fetches of remote->slot_queue_recycle from overtaking the
1160 : : ** mutex. */
1161 : 207 : rmb();
1162 : :
1163 : 207 : slot_queue_recycle = state->remote->slot_queue_recycle;
1164 : 207 : state->remote->slot_queue[slot_queue_recycle &
1165 : 207 : VCHIQ_SLOT_QUEUE_MASK] =
1166 : 207 : SLOT_INDEX_FROM_INFO(state, slot_info);
1167 : 207 : state->remote->slot_queue_recycle = slot_queue_recycle + 1;
1168 [ - + ]: 207 : vchiq_log_info(vchiq_core_log_level,
1169 : : "%d: %s %d - recycle->%x", state->id, __func__,
1170 : : SLOT_INDEX_FROM_INFO(state, slot_info),
1171 : : state->remote->slot_queue_recycle);
1172 : :
1173 : : /* A write barrier is necessary, but remote_event_signal
1174 : : ** contains one. */
1175 : 207 : remote_event_signal(&state->remote->recycle);
1176 : : }
1177 : :
1178 : 8280 : mutex_unlock(&state->recycle_mutex);
1179 : : }
1180 : :
1181 : : /* Called by the slot handler - don't hold the bulk mutex */
1182 : : static VCHIQ_STATUS_T
1183 : 414 : notify_bulks(struct vchiq_service *service, struct vchiq_bulk_queue *queue,
1184 : : int retry_poll)
1185 : : {
1186 : : VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
1187 : :
1188 [ - + # # ]: 414 : vchiq_log_trace(vchiq_core_log_level,
1189 : : "%d: nb:%d %cx - p=%x rn=%x r=%x",
1190 : : service->state->id, service->localport,
1191 : : (queue == &service->bulk_tx) ? 't' : 'r',
1192 : : queue->process, queue->remote_notify, queue->remove);
1193 : :
1194 : 414 : queue->remote_notify = queue->process;
1195 : :
1196 : : if (status == VCHIQ_SUCCESS) {
1197 [ - + ]: 414 : while (queue->remove != queue->remote_notify) {
1198 : : struct vchiq_bulk *bulk =
1199 : 0 : &queue->bulks[BULK_INDEX(queue->remove)];
1200 : :
1201 : : /* Only generate callbacks for non-dummy bulk
1202 : : ** requests, and non-terminated services */
1203 [ # # # # ]: 0 : if (bulk->data && service->instance) {
1204 [ # # ]: 0 : if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
1205 [ # # ]: 0 : if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
1206 : 0 : VCHIQ_SERVICE_STATS_INC(service,
1207 : : bulk_tx_count);
1208 : 0 : VCHIQ_SERVICE_STATS_ADD(service,
1209 : : bulk_tx_bytes,
1210 : : bulk->actual);
1211 : : } else {
1212 : 0 : VCHIQ_SERVICE_STATS_INC(service,
1213 : : bulk_rx_count);
1214 : 0 : VCHIQ_SERVICE_STATS_ADD(service,
1215 : : bulk_rx_bytes,
1216 : : bulk->actual);
1217 : : }
1218 : : } else {
1219 : 0 : VCHIQ_SERVICE_STATS_INC(service,
1220 : : bulk_aborted_count);
1221 : : }
1222 [ # # ]: 0 : if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
1223 : : struct bulk_waiter *waiter;
1224 : :
1225 : : spin_lock(&bulk_waiter_spinlock);
1226 : 0 : waiter = bulk->userdata;
1227 [ # # ]: 0 : if (waiter) {
1228 : 0 : waiter->actual = bulk->actual;
1229 : 0 : complete(&waiter->event);
1230 : : }
1231 : : spin_unlock(&bulk_waiter_spinlock);
1232 [ # # ]: 0 : } else if (bulk->mode ==
1233 : : VCHIQ_BULK_MODE_CALLBACK) {
1234 [ # # # # : 0 : VCHIQ_REASON_T reason = (bulk->dir ==
# # ]
1235 : : VCHIQ_BULK_TRANSMIT) ?
1236 : 0 : ((bulk->actual ==
1237 : : VCHIQ_BULK_ACTUAL_ABORTED) ?
1238 : : VCHIQ_BULK_TRANSMIT_ABORTED :
1239 : : VCHIQ_BULK_TRANSMIT_DONE) :
1240 : 0 : ((bulk->actual ==
1241 : : VCHIQ_BULK_ACTUAL_ABORTED) ?
1242 : : VCHIQ_BULK_RECEIVE_ABORTED :
1243 : : VCHIQ_BULK_RECEIVE_DONE);
1244 : 0 : status = make_service_callback(service,
1245 : : reason, NULL, bulk->userdata);
1246 [ # # ]: 0 : if (status == VCHIQ_RETRY)
1247 : : break;
1248 : : }
1249 : : }
1250 : :
1251 : 0 : queue->remove++;
1252 : 0 : complete(&service->bulk_remove_event);
1253 : : }
1254 [ + - ]: 414 : if (!retry_poll)
1255 : : status = VCHIQ_SUCCESS;
1256 : : }
1257 : :
1258 [ - + ]: 414 : if (status == VCHIQ_RETRY)
1259 [ # # ]: 0 : request_poll(service->state, service,
1260 : 0 : (queue == &service->bulk_tx) ?
1261 : : VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
1262 : :
1263 : 414 : return status;
1264 : : }
1265 : :
1266 : : /* Called by the slot handler thread */
1267 : : static void
1268 : 207 : poll_services(struct vchiq_state *state)
1269 : : {
1270 : : int group, i;
1271 : :
1272 [ + + ]: 414 : for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
1273 : : u32 flags;
1274 : :
1275 : 207 : flags = atomic_xchg(&state->poll_services[group], 0);
1276 [ + + ]: 1267 : for (i = 0; flags; i++) {
1277 [ + + ]: 1060 : if (flags & (1 << i)) {
1278 : 207 : struct vchiq_service *service =
1279 : 207 : find_service_by_port(state,
1280 : 207 : (group<<5) + i);
1281 : : u32 service_flags;
1282 : :
1283 : 207 : flags &= ~(1 << i);
1284 [ - + ]: 207 : if (!service)
1285 : 0 : continue;
1286 : : service_flags =
1287 : 207 : atomic_xchg(&service->poll_flags, 0);
1288 [ - + ]: 207 : if (service_flags &
1289 : : (1 << VCHIQ_POLL_REMOVE)) {
1290 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
1291 : : "%d: ps - remove %d<->%d",
1292 : : state->id, service->localport,
1293 : : service->remoteport);
1294 : :
1295 : : /* Make it look like a client, because
1296 : : it must be removed and not left in
1297 : : the LISTENING state. */
1298 : 0 : service->public_fourcc =
1299 : : VCHIQ_FOURCC_INVALID;
1300 : :
1301 [ # # ]: 0 : if (vchiq_close_service_internal(
1302 : : service, 0/*!close_recvd*/) !=
1303 : : VCHIQ_SUCCESS)
1304 : 0 : request_poll(state, service,
1305 : : VCHIQ_POLL_REMOVE);
1306 [ + - ]: 207 : } else if (service_flags &
1307 : : (1 << VCHIQ_POLL_TERMINATE)) {
1308 [ - + ]: 207 : vchiq_log_info(vchiq_core_log_level,
1309 : : "%d: ps - terminate %d<->%d",
1310 : : state->id, service->localport,
1311 : : service->remoteport);
1312 [ - + ]: 207 : if (vchiq_close_service_internal(
1313 : : service, 0/*!close_recvd*/) !=
1314 : : VCHIQ_SUCCESS)
1315 : 0 : request_poll(state, service,
1316 : : VCHIQ_POLL_TERMINATE);
1317 : : }
1318 [ - + ]: 207 : if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
1319 : 0 : notify_bulks(service,
1320 : : &service->bulk_tx,
1321 : : 1/*retry_poll*/);
1322 [ - + ]: 207 : if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
1323 : 0 : notify_bulks(service,
1324 : : &service->bulk_rx,
1325 : : 1/*retry_poll*/);
1326 : 207 : unlock_service(service);
1327 : : }
1328 : : }
1329 : : }
1330 : 207 : }
1331 : :
1332 : : /* Called with the bulk_mutex held */
1333 : : static void
1334 : 414 : abort_outstanding_bulks(struct vchiq_service *service,
1335 : : struct vchiq_bulk_queue *queue)
1336 : : {
1337 : 414 : int is_tx = (queue == &service->bulk_tx);
1338 : :
1339 [ - + # # ]: 414 : vchiq_log_trace(vchiq_core_log_level,
1340 : : "%d: aob:%d %cx - li=%x ri=%x p=%x",
1341 : : service->state->id, service->localport, is_tx ? 't' : 'r',
1342 : : queue->local_insert, queue->remote_insert, queue->process);
1343 : :
1344 [ - + ]: 414 : WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
1345 [ - + ]: 414 : WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
1346 : :
1347 [ - + - + ]: 828 : while ((queue->process != queue->local_insert) ||
1348 : 414 : (queue->process != queue->remote_insert)) {
1349 : 0 : struct vchiq_bulk *bulk =
1350 : 0 : &queue->bulks[BULK_INDEX(queue->process)];
1351 : :
1352 [ # # ]: 0 : if (queue->process == queue->remote_insert) {
1353 : : /* fabricate a matching dummy bulk */
1354 : 0 : bulk->remote_data = NULL;
1355 : 0 : bulk->remote_size = 0;
1356 : 0 : queue->remote_insert++;
1357 : : }
1358 : :
1359 [ # # ]: 0 : if (queue->process != queue->local_insert) {
1360 : 0 : vchiq_complete_bulk(bulk);
1361 : :
1362 [ # # # # : 0 : vchiq_log_info(SRVTRACE_LEVEL(service),
# # # # ]
1363 : : "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
1364 : : "rx len:%d",
1365 : : is_tx ? "Send Bulk to" : "Recv Bulk from",
1366 : : VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1367 : : service->remoteport,
1368 : : bulk->size,
1369 : : bulk->remote_size);
1370 : : } else {
1371 : : /* fabricate a matching dummy bulk */
1372 : 0 : bulk->data = NULL;
1373 : 0 : bulk->size = 0;
1374 : 0 : bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
1375 : 0 : bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
1376 : : VCHIQ_BULK_RECEIVE;
1377 : 0 : queue->local_insert++;
1378 : : }
1379 : :
1380 : 0 : queue->process++;
1381 : : }
1382 : 414 : }
1383 : :
1384 : : static int
1385 : 0 : parse_open(struct vchiq_state *state, struct vchiq_header *header)
1386 : : {
1387 : : struct vchiq_service *service = NULL;
1388 : : int msgid, size;
1389 : : unsigned int localport, remoteport;
1390 : :
1391 : 0 : msgid = header->msgid;
1392 : 0 : size = header->size;
1393 : 0 : localport = VCHIQ_MSG_DSTPORT(msgid);
1394 : 0 : remoteport = VCHIQ_MSG_SRCPORT(msgid);
1395 [ # # ]: 0 : if (size >= sizeof(struct vchiq_open_payload)) {
1396 : : const struct vchiq_open_payload *payload =
1397 : : (struct vchiq_open_payload *)header->data;
1398 : : unsigned int fourcc;
1399 : :
1400 : 0 : fourcc = payload->fourcc;
1401 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
1402 : : "%d: prs OPEN@%pK (%d->'%c%c%c%c')",
1403 : : state->id, header, localport,
1404 : : VCHIQ_FOURCC_AS_4CHARS(fourcc));
1405 : :
1406 : 0 : service = get_listening_service(state, fourcc);
1407 : :
1408 [ # # ]: 0 : if (service) {
1409 : : /* A matching service exists */
1410 : 0 : short version = payload->version;
1411 : 0 : short version_min = payload->version_min;
1412 : :
1413 [ # # # # ]: 0 : if ((service->version < version_min) ||
1414 : 0 : (version < service->version_min)) {
1415 : : /* Version mismatch */
1416 : 0 : vchiq_loud_error_header();
1417 [ # # ]: 0 : vchiq_loud_error("%d: service %d (%c%c%c%c) "
1418 : : "version mismatch - local (%d, min %d)"
1419 : : " vs. remote (%d, min %d)",
1420 : : state->id, service->localport,
1421 : : VCHIQ_FOURCC_AS_4CHARS(fourcc),
1422 : : service->version, service->version_min,
1423 : : version, version_min);
1424 : 0 : vchiq_loud_error_footer();
1425 : 0 : unlock_service(service);
1426 : : service = NULL;
1427 : 0 : goto fail_open;
1428 : : }
1429 : 0 : service->peer_version = version;
1430 : :
1431 [ # # ]: 0 : if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
1432 : 0 : struct vchiq_openack_payload ack_payload = {
1433 : : service->version
1434 : : };
1435 : :
1436 [ # # ]: 0 : if (state->version_common <
1437 : : VCHIQ_VERSION_SYNCHRONOUS_MODE)
1438 : 0 : service->sync = 0;
1439 : :
1440 : : /* Acknowledge the OPEN */
1441 [ # # ]: 0 : if (service->sync) {
1442 [ # # ]: 0 : if (queue_message_sync(
1443 : : state,
1444 : : NULL,
1445 : 0 : VCHIQ_MAKE_MSG(
1446 : : VCHIQ_MSG_OPENACK,
1447 : : service->localport,
1448 : : remoteport),
1449 : : memcpy_copy_callback,
1450 : : &ack_payload,
1451 : : sizeof(ack_payload),
1452 : : 0) == VCHIQ_RETRY)
1453 : : goto bail_not_ready;
1454 : : } else {
1455 [ # # ]: 0 : if (queue_message(state,
1456 : : NULL,
1457 : 0 : VCHIQ_MAKE_MSG(
1458 : : VCHIQ_MSG_OPENACK,
1459 : : service->localport,
1460 : : remoteport),
1461 : : memcpy_copy_callback,
1462 : : &ack_payload,
1463 : : sizeof(ack_payload),
1464 : : 0) == VCHIQ_RETRY)
1465 : : goto bail_not_ready;
1466 : : }
1467 : :
1468 : : /* The service is now open */
1469 [ # # ]: 0 : vchiq_set_service_state(service,
1470 : 0 : service->sync ? VCHIQ_SRVSTATE_OPENSYNC
1471 : : : VCHIQ_SRVSTATE_OPEN);
1472 : : }
1473 : :
1474 : 0 : service->remoteport = remoteport;
1475 : 0 : service->client_id = ((int *)header->data)[1];
1476 [ # # ]: 0 : if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
1477 : : NULL, NULL) == VCHIQ_RETRY) {
1478 : : /* Bail out if not ready */
1479 : 0 : service->remoteport = VCHIQ_PORT_FREE;
1480 : 0 : goto bail_not_ready;
1481 : : }
1482 : :
1483 : : /* Success - the message has been dealt with */
1484 : 0 : unlock_service(service);
1485 : 0 : return 1;
1486 : : }
1487 : : }
1488 : :
1489 : : fail_open:
1490 : : /* No available service, or an invalid request - send a CLOSE */
1491 [ # # ]: 0 : if (queue_message(state, NULL,
1492 : 0 : VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
1493 : : NULL, NULL, 0, 0) == VCHIQ_RETRY)
1494 : : goto bail_not_ready;
1495 : :
1496 : : return 1;
1497 : :
1498 : : bail_not_ready:
1499 [ # # ]: 0 : if (service)
1500 : 0 : unlock_service(service);
1501 : :
1502 : : return 0;
1503 : : }
1504 : :
1505 : : /* Called by the slot handler thread */
1506 : : static void
1507 : 9910 : parse_rx_slots(struct vchiq_state *state)
1508 : : {
1509 : 9910 : struct vchiq_shared_state *remote = state->remote;
1510 : : struct vchiq_service *service = NULL;
1511 : : int tx_pos;
1512 : :
1513 : 9910 : DEBUG_INITIALISE(state->local)
1514 : :
1515 : 9910 : tx_pos = remote->tx_pos;
1516 : :
1517 [ + + ]: 29958 : while (state->rx_pos != tx_pos) {
1518 : : struct vchiq_header *header;
1519 : : int msgid, size;
1520 : : int type;
1521 : : unsigned int localport, remoteport;
1522 : :
1523 : 10138 : DEBUG_TRACE(PARSE_LINE);
1524 [ + + ]: 10138 : if (!state->rx_data) {
1525 : : int rx_index;
1526 : :
1527 [ - + ]: 414 : WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
1528 : 414 : rx_index = remote->slot_queue[
1529 : 414 : SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
1530 : : VCHIQ_SLOT_QUEUE_MASK];
1531 : 414 : state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
1532 : : rx_index);
1533 : 414 : state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
1534 : :
1535 : : /* Initialise use_count to one, and increment
1536 : : ** release_count at the end of the slot to avoid
1537 : : ** releasing the slot prematurely. */
1538 : 414 : state->rx_info->use_count = 1;
1539 : 414 : state->rx_info->release_count = 0;
1540 : : }
1541 : :
1542 : 20276 : header = (struct vchiq_header *)(state->rx_data +
1543 : 10138 : (state->rx_pos & VCHIQ_SLOT_MASK));
1544 : 10138 : DEBUG_VALUE(PARSE_HEADER, (int)(long)header);
1545 : 10138 : msgid = header->msgid;
1546 : 10138 : DEBUG_VALUE(PARSE_MSGID, msgid);
1547 : 10138 : size = header->size;
1548 : 10138 : type = VCHIQ_MSG_TYPE(msgid);
1549 : 10138 : localport = VCHIQ_MSG_DSTPORT(msgid);
1550 : 10138 : remoteport = VCHIQ_MSG_SRCPORT(msgid);
1551 : :
1552 [ + + ]: 10138 : if (type != VCHIQ_MSG_DATA)
1553 : 2065 : VCHIQ_STATS_INC(state, ctrl_rx_count);
1554 : :
1555 [ + + ]: 10138 : switch (type) {
1556 : : case VCHIQ_MSG_OPENACK:
1557 : : case VCHIQ_MSG_CLOSE:
1558 : : case VCHIQ_MSG_DATA:
1559 : : case VCHIQ_MSG_BULK_RX:
1560 : : case VCHIQ_MSG_BULK_TX:
1561 : : case VCHIQ_MSG_BULK_RX_DONE:
1562 : : case VCHIQ_MSG_BULK_TX_DONE:
1563 : 9729 : service = find_service_by_port(state, localport);
1564 [ + - + + ]: 19458 : if ((!service ||
1565 [ - + ]: 11178 : ((service->remoteport != remoteport) &&
1566 : : (service->remoteport != VCHIQ_PORT_FREE))) &&
1567 [ # # ]: 0 : (localport == 0) &&
1568 : 0 : (type == VCHIQ_MSG_CLOSE)) {
1569 : : /* This could be a CLOSE from a client which
1570 : : hadn't yet received the OPENACK - look for
1571 : : the connected service */
1572 [ # # ]: 0 : if (service)
1573 : 0 : unlock_service(service);
1574 : 0 : service = get_connected_service(state,
1575 : : remoteport);
1576 [ # # ]: 0 : if (service)
1577 [ # # # # ]: 0 : vchiq_log_warning(vchiq_core_log_level,
1578 : : "%d: prs %s@%pK (%d->%d) - found connected service %d",
1579 : : state->id, msg_type_str(type),
1580 : : header, remoteport, localport,
1581 : : service->localport);
1582 : : }
1583 : :
1584 [ - + ]: 9729 : if (!service) {
1585 [ # # # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1586 : : "%d: prs %s@%pK (%d->%d) - invalid/closed service %d",
1587 : : state->id, msg_type_str(type),
1588 : : header, remoteport, localport,
1589 : : localport);
1590 : : goto skip_message;
1591 : : }
1592 : : break;
1593 : : default:
1594 : : break;
1595 : : }
1596 : :
1597 [ + + + - : 10138 : if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
- + ]
1598 : : int svc_fourcc;
1599 : :
1600 : : svc_fourcc = service
1601 : : ? service->base.fourcc
1602 [ # # ]: 0 : : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
1603 [ # # # # : 0 : vchiq_log_info(SRVTRACE_LEVEL(service),
# # # # ]
1604 : : "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
1605 : : "len:%d",
1606 : : msg_type_str(type), type,
1607 : : VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
1608 : : remoteport, localport, size);
1609 [ # # ]: 0 : if (size > 0)
1610 : 0 : vchiq_log_dump_mem("Rcvd", 0, header->data,
1611 : 0 : min(16, size));
1612 : : }
1613 : :
1614 [ - + ]: 20276 : if (((unsigned long)header & VCHIQ_SLOT_MASK) +
1615 : : calc_stride(size) > VCHIQ_SLOT_SIZE) {
1616 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1617 : : "header %pK (msgid %x) - size %x too big for slot",
1618 : : header, (unsigned int)msgid,
1619 : : (unsigned int)size);
1620 : 0 : WARN(1, "oversized for slot\n");
1621 : : }
1622 : :
1623 [ - + + + : 10138 : switch (type) {
+ - - + -
- - - -
- ]
1624 : : case VCHIQ_MSG_OPEN:
1625 [ # # ]: 0 : WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
1626 [ # # ]: 0 : if (!parse_open(state, header))
1627 : : goto bail_not_ready;
1628 : : break;
1629 : : case VCHIQ_MSG_OPENACK:
1630 [ + - ]: 1449 : if (size >= sizeof(struct vchiq_openack_payload)) {
1631 : : const struct vchiq_openack_payload *payload =
1632 : : (struct vchiq_openack_payload *)
1633 : : header->data;
1634 : 1449 : service->peer_version = payload->version;
1635 : : }
1636 [ - + ]: 1449 : vchiq_log_info(vchiq_core_log_level,
1637 : : "%d: prs OPENACK@%pK,%x (%d->%d) v:%d",
1638 : : state->id, header, size, remoteport, localport,
1639 : : service->peer_version);
1640 [ + - ]: 1449 : if (service->srvstate ==
1641 : : VCHIQ_SRVSTATE_OPENING) {
1642 : 1449 : service->remoteport = remoteport;
1643 : 1449 : vchiq_set_service_state(service,
1644 : : VCHIQ_SRVSTATE_OPEN);
1645 : 1449 : complete(&service->remove_event);
1646 : : } else
1647 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1648 : : "OPENACK received in state %s",
1649 : : srvstate_names[service->srvstate]);
1650 : : break;
1651 : : case VCHIQ_MSG_CLOSE:
1652 [ - + ]: 207 : WARN_ON(size != 0); /* There should be no data */
1653 : :
1654 [ - + ]: 207 : vchiq_log_info(vchiq_core_log_level,
1655 : : "%d: prs CLOSE@%pK (%d->%d)",
1656 : : state->id, header, remoteport, localport);
1657 : :
1658 : 207 : mark_service_closing_internal(service, 1);
1659 : :
1660 [ + - ]: 207 : if (vchiq_close_service_internal(service,
1661 : : 1/*close_recvd*/) == VCHIQ_RETRY)
1662 : : goto bail_not_ready;
1663 : :
1664 [ - + ]: 207 : vchiq_log_info(vchiq_core_log_level,
1665 : : "Close Service %c%c%c%c s:%u d:%d",
1666 : : VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1667 : : service->localport,
1668 : : service->remoteport);
1669 : : break;
1670 : : case VCHIQ_MSG_DATA:
1671 [ - + ]: 8073 : vchiq_log_info(vchiq_core_log_level,
1672 : : "%d: prs DATA@%pK,%x (%d->%d)",
1673 : : state->id, header, size, remoteport, localport);
1674 : :
1675 [ + - ]: 8073 : if ((service->remoteport == remoteport)
1676 [ + - ]: 8073 : && (service->srvstate ==
1677 : : VCHIQ_SRVSTATE_OPEN)) {
1678 : 8073 : header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
1679 : 8073 : claim_slot(state->rx_info);
1680 : 8073 : DEBUG_TRACE(PARSE_LINE);
1681 [ - + ]: 8073 : if (make_service_callback(service,
1682 : : VCHIQ_MESSAGE_AVAILABLE, header,
1683 : : NULL) == VCHIQ_RETRY) {
1684 : 0 : DEBUG_TRACE(PARSE_LINE);
1685 : 0 : goto bail_not_ready;
1686 : : }
1687 : 8073 : VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
1688 : 8073 : VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
1689 : : size);
1690 : : } else {
1691 : 0 : VCHIQ_STATS_INC(state, error_count);
1692 : : }
1693 : : break;
1694 : : case VCHIQ_MSG_CONNECT:
1695 [ - + ]: 207 : vchiq_log_info(vchiq_core_log_level,
1696 : : "%d: prs CONNECT@%pK", state->id, header);
1697 : 207 : state->version_common = ((struct vchiq_slot_zero *)
1698 : 207 : state->slot_data)->version;
1699 : 207 : complete(&state->connect);
1700 : 207 : break;
1701 : : case VCHIQ_MSG_BULK_RX:
1702 : : case VCHIQ_MSG_BULK_TX:
1703 : : /*
1704 : : * We should never receive a bulk request from the
1705 : : * other side since we're not setup to perform as the
1706 : : * master.
1707 : : */
1708 : 0 : WARN_ON(1);
1709 : 0 : break;
1710 : : case VCHIQ_MSG_BULK_RX_DONE:
1711 : : case VCHIQ_MSG_BULK_TX_DONE:
1712 [ # # ]: 0 : if ((service->remoteport == remoteport)
1713 [ # # ]: 0 : && (service->srvstate !=
1714 : : VCHIQ_SRVSTATE_FREE)) {
1715 : : struct vchiq_bulk_queue *queue;
1716 : : struct vchiq_bulk *bulk;
1717 : :
1718 : : queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
1719 [ # # ]: 0 : &service->bulk_rx : &service->bulk_tx;
1720 : :
1721 : 0 : DEBUG_TRACE(PARSE_LINE);
1722 [ # # ]: 0 : if (mutex_lock_killable(&service->bulk_mutex)) {
1723 : 0 : DEBUG_TRACE(PARSE_LINE);
1724 : 0 : goto bail_not_ready;
1725 : : }
1726 [ # # ]: 0 : if ((int)(queue->remote_insert -
1727 : 0 : queue->local_insert) >= 0) {
1728 [ # # # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1729 : : "%d: prs %s@%pK (%d->%d) "
1730 : : "unexpected (ri=%d,li=%d)",
1731 : : state->id, msg_type_str(type),
1732 : : header, remoteport, localport,
1733 : : queue->remote_insert,
1734 : : queue->local_insert);
1735 : 0 : mutex_unlock(&service->bulk_mutex);
1736 : 0 : break;
1737 : : }
1738 [ # # ]: 0 : if (queue->process != queue->remote_insert) {
1739 : 0 : pr_err("%s: p %x != ri %x\n",
1740 : : __func__,
1741 : : queue->process,
1742 : : queue->remote_insert);
1743 : 0 : mutex_unlock(&service->bulk_mutex);
1744 : 0 : goto bail_not_ready;
1745 : : }
1746 : :
1747 : 0 : bulk = &queue->bulks[
1748 : 0 : BULK_INDEX(queue->remote_insert)];
1749 : 0 : bulk->actual = *(int *)header->data;
1750 : 0 : queue->remote_insert++;
1751 : :
1752 [ # # # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
1753 : : "%d: prs %s@%pK (%d->%d) %x@%pK",
1754 : : state->id, msg_type_str(type),
1755 : : header, remoteport, localport,
1756 : : bulk->actual, bulk->data);
1757 : :
1758 [ # # # # ]: 0 : vchiq_log_trace(vchiq_core_log_level,
1759 : : "%d: prs:%d %cx li=%x ri=%x p=%x",
1760 : : state->id, localport,
1761 : : (type == VCHIQ_MSG_BULK_RX_DONE) ?
1762 : : 'r' : 't',
1763 : : queue->local_insert,
1764 : : queue->remote_insert, queue->process);
1765 : :
1766 : 0 : DEBUG_TRACE(PARSE_LINE);
1767 [ # # ]: 0 : WARN_ON(queue->process == queue->local_insert);
1768 : 0 : vchiq_complete_bulk(bulk);
1769 : 0 : queue->process++;
1770 : 0 : mutex_unlock(&service->bulk_mutex);
1771 : 0 : DEBUG_TRACE(PARSE_LINE);
1772 : 0 : notify_bulks(service, queue, 1/*retry_poll*/);
1773 : 0 : DEBUG_TRACE(PARSE_LINE);
1774 : : }
1775 : : break;
1776 : : case VCHIQ_MSG_PADDING:
1777 [ - + ]: 202 : vchiq_log_trace(vchiq_core_log_level,
1778 : : "%d: prs PADDING@%pK,%x",
1779 : : state->id, header, size);
1780 : : break;
1781 : : case VCHIQ_MSG_PAUSE:
1782 : : /* If initiated, signal the application thread */
1783 [ # # ]: 0 : vchiq_log_trace(vchiq_core_log_level,
1784 : : "%d: prs PAUSE@%pK,%x",
1785 : : state->id, header, size);
1786 [ # # ]: 0 : if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
1787 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1788 : : "%d: PAUSE received in state PAUSED",
1789 : : state->id);
1790 : : break;
1791 : : }
1792 [ # # ]: 0 : if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
1793 : : /* Send a PAUSE in response */
1794 [ # # ]: 0 : if (queue_message(state, NULL,
1795 : : VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
1796 : : NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK)
1797 : : == VCHIQ_RETRY)
1798 : : goto bail_not_ready;
1799 : : }
1800 : : /* At this point slot_mutex is held */
1801 : 0 : vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
1802 : 0 : vchiq_platform_paused(state);
1803 : 0 : break;
1804 : : case VCHIQ_MSG_RESUME:
1805 [ # # ]: 0 : vchiq_log_trace(vchiq_core_log_level,
1806 : : "%d: prs RESUME@%pK,%x",
1807 : : state->id, header, size);
1808 : : /* Release the slot mutex */
1809 : 0 : mutex_unlock(&state->slot_mutex);
1810 : 0 : vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
1811 : 0 : vchiq_platform_resumed(state);
1812 : 0 : break;
1813 : :
1814 : : case VCHIQ_MSG_REMOTE_USE:
1815 : 0 : vchiq_on_remote_use(state);
1816 : 0 : break;
1817 : : case VCHIQ_MSG_REMOTE_RELEASE:
1818 : 0 : vchiq_on_remote_release(state);
1819 : 0 : break;
1820 : : case VCHIQ_MSG_REMOTE_USE_ACTIVE:
1821 : 0 : vchiq_on_remote_use_active(state);
1822 : 0 : break;
1823 : :
1824 : : default:
1825 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1826 : : "%d: prs invalid msgid %x@%pK,%x",
1827 : : state->id, msgid, header, size);
1828 : 0 : WARN(1, "invalid message\n");
1829 : 0 : break;
1830 : : }
1831 : :
1832 : : skip_message:
1833 [ + + ]: 10138 : if (service) {
1834 : 9729 : unlock_service(service);
1835 : : service = NULL;
1836 : : }
1837 : :
1838 : 10138 : state->rx_pos += calc_stride(size);
1839 : :
1840 : 10138 : DEBUG_TRACE(PARSE_LINE);
1841 : : /* Perform some housekeeping when the end of the slot is
1842 : : ** reached. */
1843 [ + + ]: 10138 : if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
1844 : : /* Remove the extra reference count. */
1845 : 207 : release_slot(state, state->rx_info, NULL, NULL);
1846 : 207 : state->rx_data = NULL;
1847 : : }
1848 : : }
1849 : :
1850 : : bail_not_ready:
1851 [ - + ]: 9910 : if (service)
1852 : 0 : unlock_service(service);
1853 : 9910 : }
1854 : :
1855 : : /* Called by the slot handler thread */
1856 : : static int
1857 : 207 : slot_handler_func(void *v)
1858 : : {
1859 : : struct vchiq_state *state = v;
1860 : 207 : struct vchiq_shared_state *local = state->local;
1861 : :
1862 : : DEBUG_INITIALISE(local)
1863 : :
1864 : : while (1) {
1865 : 10117 : DEBUG_COUNT(SLOT_HANDLER_COUNT);
1866 : 10117 : DEBUG_TRACE(SLOT_HANDLER_LINE);
1867 : 10117 : remote_event_wait(&state->trigger_event, &local->trigger);
1868 : :
1869 : 9910 : rmb();
1870 : :
1871 : 9910 : DEBUG_TRACE(SLOT_HANDLER_LINE);
1872 [ + + ]: 9910 : if (state->poll_needed) {
1873 : : /* Check if we need to suspend - may change our
1874 : : * conn_state */
1875 : 207 : vchiq_platform_check_suspend(state);
1876 : :
1877 : 207 : state->poll_needed = 0;
1878 : :
1879 : : /* Handle service polling and other rare conditions here
1880 : : ** out of the mainline code */
1881 [ + - - - : 207 : switch (state->conn_state) {
- - ]
1882 : : case VCHIQ_CONNSTATE_CONNECTED:
1883 : : /* Poll the services as requested */
1884 : 207 : poll_services(state);
1885 : 207 : break;
1886 : :
1887 : : case VCHIQ_CONNSTATE_PAUSING:
1888 [ # # ]: 0 : if (queue_message(state, NULL,
1889 : : VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
1890 : : NULL, NULL, 0,
1891 : : QMFLAGS_NO_MUTEX_UNLOCK)
1892 : : != VCHIQ_RETRY) {
1893 : 0 : vchiq_set_conn_state(state,
1894 : : VCHIQ_CONNSTATE_PAUSE_SENT);
1895 : : } else {
1896 : : /* Retry later */
1897 : 0 : state->poll_needed = 1;
1898 : : }
1899 : : break;
1900 : :
1901 : : case VCHIQ_CONNSTATE_PAUSED:
1902 : 0 : vchiq_platform_resume(state);
1903 : 0 : break;
1904 : :
1905 : : case VCHIQ_CONNSTATE_RESUMING:
1906 [ # # ]: 0 : if (queue_message(state, NULL,
1907 : : VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
1908 : : NULL, NULL, 0, QMFLAGS_NO_MUTEX_LOCK)
1909 : : != VCHIQ_RETRY) {
1910 : 0 : vchiq_set_conn_state(state,
1911 : : VCHIQ_CONNSTATE_CONNECTED);
1912 : 0 : vchiq_platform_resumed(state);
1913 : : } else {
1914 : : /* This should really be impossible,
1915 : : ** since the PAUSE should have flushed
1916 : : ** through outstanding messages. */
1917 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
1918 : : "Failed to send RESUME "
1919 : : "message");
1920 : : }
1921 : : break;
1922 : :
1923 : : case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
1924 : : case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
1925 : 0 : vchiq_platform_handle_timeout(state);
1926 : 0 : break;
1927 : : default:
1928 : : break;
1929 : : }
1930 : :
1931 : : }
1932 : :
1933 : 9910 : DEBUG_TRACE(SLOT_HANDLER_LINE);
1934 : 9910 : parse_rx_slots(state);
1935 : 9910 : }
1936 : : return 0;
1937 : : }
1938 : :
1939 : : /* Called by the recycle thread */
1940 : : static int
1941 : 207 : recycle_func(void *v)
1942 : : {
1943 : : struct vchiq_state *state = v;
1944 : 207 : struct vchiq_shared_state *local = state->local;
1945 : : BITSET_T *found;
1946 : : size_t length;
1947 : :
1948 : : length = sizeof(*found) * BITSET_SIZE(VCHIQ_MAX_SERVICES);
1949 : :
1950 : 207 : found = kmalloc_array(BITSET_SIZE(VCHIQ_MAX_SERVICES), sizeof(*found),
1951 : : GFP_KERNEL);
1952 [ + - ]: 207 : if (!found)
1953 : : return -ENOMEM;
1954 : :
1955 : : while (1) {
1956 : 207 : remote_event_wait(&state->recycle_event, &local->recycle);
1957 : :
1958 : 0 : process_free_queue(state, found, length);
1959 : 0 : }
1960 : : return 0;
1961 : : }
1962 : :
1963 : : /* Called by the sync thread */
1964 : : static int
1965 : 207 : sync_func(void *v)
1966 : : {
1967 : : struct vchiq_state *state = v;
1968 : 207 : struct vchiq_shared_state *local = state->local;
1969 : 207 : struct vchiq_header *header =
1970 : 207 : (struct vchiq_header *)SLOT_DATA_FROM_INDEX(state,
1971 : : state->remote->slot_sync);
1972 : :
1973 : : while (1) {
1974 : : struct vchiq_service *service;
1975 : : int msgid, size;
1976 : : int type;
1977 : : unsigned int localport, remoteport;
1978 : :
1979 : 207 : remote_event_wait(&state->sync_trigger_event, &local->sync_trigger);
1980 : :
1981 : 0 : rmb();
1982 : :
1983 : 0 : msgid = header->msgid;
1984 : 0 : size = header->size;
1985 : 0 : type = VCHIQ_MSG_TYPE(msgid);
1986 : 0 : localport = VCHIQ_MSG_DSTPORT(msgid);
1987 : 0 : remoteport = VCHIQ_MSG_SRCPORT(msgid);
1988 : :
1989 : 0 : service = find_service_by_port(state, localport);
1990 : :
1991 [ # # ]: 0 : if (!service) {
1992 [ # # # # ]: 0 : vchiq_log_error(vchiq_sync_log_level,
1993 : : "%d: sf %s@%pK (%d->%d) - invalid/closed service %d",
1994 : : state->id, msg_type_str(type),
1995 : : header, remoteport, localport, localport);
1996 : : release_message_sync(state, header);
1997 : 0 : continue;
1998 : : }
1999 : :
2000 [ # # ]: 0 : if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
2001 : : int svc_fourcc;
2002 : :
2003 : : svc_fourcc = service
2004 : : ? service->base.fourcc
2005 [ # # ]: 0 : : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
2006 [ # # # # ]: 0 : vchiq_log_trace(vchiq_sync_log_level,
2007 : : "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
2008 : : msg_type_str(type),
2009 : : VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
2010 : : remoteport, localport, size);
2011 [ # # ]: 0 : if (size > 0)
2012 : 0 : vchiq_log_dump_mem("Rcvd", 0, header->data,
2013 : 0 : min(16, size));
2014 : : }
2015 : :
2016 [ # # # ]: 0 : switch (type) {
2017 : : case VCHIQ_MSG_OPENACK:
2018 [ # # ]: 0 : if (size >= sizeof(struct vchiq_openack_payload)) {
2019 : : const struct vchiq_openack_payload *payload =
2020 : : (struct vchiq_openack_payload *)
2021 : : header->data;
2022 : 0 : service->peer_version = payload->version;
2023 : : }
2024 [ # # ]: 0 : vchiq_log_info(vchiq_sync_log_level,
2025 : : "%d: sf OPENACK@%pK,%x (%d->%d) v:%d",
2026 : : state->id, header, size, remoteport, localport,
2027 : : service->peer_version);
2028 [ # # ]: 0 : if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
2029 : 0 : service->remoteport = remoteport;
2030 : 0 : vchiq_set_service_state(service,
2031 : : VCHIQ_SRVSTATE_OPENSYNC);
2032 : 0 : service->sync = 1;
2033 : 0 : complete(&service->remove_event);
2034 : : }
2035 : : release_message_sync(state, header);
2036 : : break;
2037 : :
2038 : : case VCHIQ_MSG_DATA:
2039 [ # # ]: 0 : vchiq_log_trace(vchiq_sync_log_level,
2040 : : "%d: sf DATA@%pK,%x (%d->%d)",
2041 : : state->id, header, size, remoteport, localport);
2042 : :
2043 [ # # # # ]: 0 : if ((service->remoteport == remoteport) &&
2044 : 0 : (service->srvstate ==
2045 : : VCHIQ_SRVSTATE_OPENSYNC)) {
2046 [ # # ]: 0 : if (make_service_callback(service,
2047 : : VCHIQ_MESSAGE_AVAILABLE, header,
2048 : : NULL) == VCHIQ_RETRY)
2049 [ # # ]: 0 : vchiq_log_error(vchiq_sync_log_level,
2050 : : "synchronous callback to "
2051 : : "service %d returns "
2052 : : "VCHIQ_RETRY",
2053 : : localport);
2054 : : }
2055 : : break;
2056 : :
2057 : : default:
2058 [ # # ]: 0 : vchiq_log_error(vchiq_sync_log_level,
2059 : : "%d: sf unexpected msgid %x@%pK,%x",
2060 : : state->id, msgid, header, size);
2061 : : release_message_sync(state, header);
2062 : : break;
2063 : : }
2064 : :
2065 : 0 : unlock_service(service);
2066 : : }
2067 : :
2068 : : return 0;
2069 : : }
2070 : :
2071 : : static void
2072 : : init_bulk_queue(struct vchiq_bulk_queue *queue)
2073 : : {
2074 : 3310 : queue->local_insert = 0;
2075 : 3310 : queue->remote_insert = 0;
2076 : 3310 : queue->process = 0;
2077 : 3310 : queue->remote_notify = 0;
2078 : 3310 : queue->remove = 0;
2079 : : }
2080 : :
2081 : : inline const char *
2082 : 0 : get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
2083 : : {
2084 : 0 : return conn_state_names[conn_state];
2085 : : }
2086 : :
2087 : : struct vchiq_slot_zero *
2088 : 207 : vchiq_init_slots(void *mem_base, int mem_size)
2089 : : {
2090 : 207 : int mem_align =
2091 : 207 : (int)((VCHIQ_SLOT_SIZE - (long)mem_base) & VCHIQ_SLOT_MASK);
2092 : 207 : struct vchiq_slot_zero *slot_zero =
2093 : 207 : (struct vchiq_slot_zero *)(mem_base + mem_align);
2094 : 207 : int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
2095 : : int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
2096 : :
2097 : : /* Ensure there is enough memory to run an absolutely minimum system */
2098 : 207 : num_slots -= first_data_slot;
2099 : :
2100 [ - + ]: 207 : if (num_slots < 4) {
2101 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
2102 : : "%s - insufficient memory %x bytes",
2103 : : __func__, mem_size);
2104 : : return NULL;
2105 : : }
2106 : :
2107 : 207 : memset(slot_zero, 0, sizeof(struct vchiq_slot_zero));
2108 : :
2109 : 207 : slot_zero->magic = VCHIQ_MAGIC;
2110 : 207 : slot_zero->version = VCHIQ_VERSION;
2111 : 207 : slot_zero->version_min = VCHIQ_VERSION_MIN;
2112 : 207 : slot_zero->slot_zero_size = sizeof(struct vchiq_slot_zero);
2113 : 207 : slot_zero->slot_size = VCHIQ_SLOT_SIZE;
2114 : 207 : slot_zero->max_slots = VCHIQ_MAX_SLOTS;
2115 : 207 : slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
2116 : :
2117 : 207 : slot_zero->master.slot_sync = first_data_slot;
2118 : 207 : slot_zero->master.slot_first = first_data_slot + 1;
2119 : 207 : slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
2120 : 207 : slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
2121 : 207 : slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
2122 : 207 : slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
2123 : :
2124 : 207 : return slot_zero;
2125 : : }
2126 : :
2127 : : VCHIQ_STATUS_T
2128 : 207 : vchiq_init_state(struct vchiq_state *state, struct vchiq_slot_zero *slot_zero)
2129 : : {
2130 : : struct vchiq_shared_state *local;
2131 : : struct vchiq_shared_state *remote;
2132 : : VCHIQ_STATUS_T status;
2133 : : char threadname[16];
2134 : : int i;
2135 : :
2136 [ + - ]: 207 : vchiq_log_warning(vchiq_core_log_level,
2137 : : "%s: slot_zero = %pK", __func__, slot_zero);
2138 : :
2139 [ - + ]: 207 : if (vchiq_states[0]) {
2140 : 0 : pr_err("%s: VCHIQ state already initialized\n", __func__);
2141 : 0 : return VCHIQ_ERROR;
2142 : : }
2143 : :
2144 : 207 : local = &slot_zero->slave;
2145 : 207 : remote = &slot_zero->master;
2146 : :
2147 [ - + ]: 207 : if (local->initialised) {
2148 : 0 : vchiq_loud_error_header();
2149 [ # # ]: 0 : if (remote->initialised)
2150 [ # # ]: 0 : vchiq_loud_error("local state has already been "
2151 : : "initialised");
2152 : : else
2153 [ # # ]: 0 : vchiq_loud_error("master/slave mismatch two slaves");
2154 : 0 : vchiq_loud_error_footer();
2155 : 0 : return VCHIQ_ERROR;
2156 : : }
2157 : :
2158 : 207 : memset(state, 0, sizeof(struct vchiq_state));
2159 : :
2160 : : /*
2161 : : initialize shared state pointers
2162 : : */
2163 : :
2164 : 207 : state->local = local;
2165 : 207 : state->remote = remote;
2166 : 207 : state->slot_data = (struct vchiq_slot *)slot_zero;
2167 : :
2168 : : /*
2169 : : initialize events and mutexes
2170 : : */
2171 : :
2172 : : init_completion(&state->connect);
2173 : 207 : mutex_init(&state->mutex);
2174 : 207 : mutex_init(&state->slot_mutex);
2175 : 207 : mutex_init(&state->recycle_mutex);
2176 : 207 : mutex_init(&state->sync_mutex);
2177 : 207 : mutex_init(&state->bulk_transfer_mutex);
2178 : :
2179 : : init_completion(&state->slot_available_event);
2180 : : init_completion(&state->slot_remove_event);
2181 : : init_completion(&state->data_quota_event);
2182 : :
2183 : 207 : state->slot_queue_available = 0;
2184 : :
2185 [ + + ]: 848079 : for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
2186 : : struct vchiq_service_quota *service_quota =
2187 : : &state->service_quotas[i];
2188 : : init_completion(&service_quota->quota_event);
2189 : : }
2190 : :
2191 [ + + ]: 6624 : for (i = local->slot_first; i <= local->slot_last; i++) {
2192 : 6417 : local->slot_queue[state->slot_queue_available++] = i;
2193 : 6417 : complete(&state->slot_available_event);
2194 : : }
2195 : :
2196 : 207 : state->default_slot_quota = state->slot_queue_available/2;
2197 : 207 : state->default_message_quota =
2198 : 207 : min((unsigned short)(state->default_slot_quota * 256),
2199 : : (unsigned short)~0);
2200 : :
2201 : 207 : state->previous_data_index = -1;
2202 : 207 : state->data_use_count = 0;
2203 : 207 : state->data_quota = state->slot_queue_available - 1;
2204 : :
2205 : 207 : remote_event_create(&state->trigger_event, &local->trigger);
2206 : 207 : local->tx_pos = 0;
2207 : 207 : remote_event_create(&state->recycle_event, &local->recycle);
2208 : 207 : local->slot_queue_recycle = state->slot_queue_available;
2209 : 207 : remote_event_create(&state->sync_trigger_event, &local->sync_trigger);
2210 : 207 : remote_event_create(&state->sync_release_event, &local->sync_release);
2211 : :
2212 : : /* At start-of-day, the slot is empty and available */
2213 : 414 : ((struct vchiq_header *)
2214 : 621 : SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid =
2215 : : VCHIQ_MSGID_PADDING;
2216 : : remote_event_signal_local(&state->sync_release_event, &local->sync_release);
2217 : :
2218 : 207 : local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
2219 : :
2220 : 207 : status = vchiq_platform_init_state(state);
2221 [ + - ]: 207 : if (status != VCHIQ_SUCCESS)
2222 : : return VCHIQ_ERROR;
2223 : :
2224 : : /*
2225 : : bring up slot handler thread
2226 : : */
2227 : 207 : snprintf(threadname, sizeof(threadname), "vchiq-slot/%d", state->id);
2228 : 207 : state->slot_handler_thread = kthread_create(&slot_handler_func,
2229 : : (void *)state,
2230 : : threadname);
2231 : :
2232 [ - + ]: 207 : if (IS_ERR(state->slot_handler_thread)) {
2233 : 0 : vchiq_loud_error_header();
2234 [ # # ]: 0 : vchiq_loud_error("couldn't create thread %s", threadname);
2235 : 0 : vchiq_loud_error_footer();
2236 : 0 : return VCHIQ_ERROR;
2237 : : }
2238 : 207 : set_user_nice(state->slot_handler_thread, -19);
2239 : :
2240 : 207 : snprintf(threadname, sizeof(threadname), "vchiq-recy/%d", state->id);
2241 : 207 : state->recycle_thread = kthread_create(&recycle_func,
2242 : : (void *)state,
2243 : : threadname);
2244 [ - + ]: 207 : if (IS_ERR(state->recycle_thread)) {
2245 : 0 : vchiq_loud_error_header();
2246 [ # # ]: 0 : vchiq_loud_error("couldn't create thread %s", threadname);
2247 : 0 : vchiq_loud_error_footer();
2248 : 0 : goto fail_free_handler_thread;
2249 : : }
2250 : 207 : set_user_nice(state->recycle_thread, -19);
2251 : :
2252 : 207 : snprintf(threadname, sizeof(threadname), "vchiq-sync/%d", state->id);
2253 : 207 : state->sync_thread = kthread_create(&sync_func,
2254 : : (void *)state,
2255 : : threadname);
2256 [ - + ]: 207 : if (IS_ERR(state->sync_thread)) {
2257 : 0 : vchiq_loud_error_header();
2258 [ # # ]: 0 : vchiq_loud_error("couldn't create thread %s", threadname);
2259 : 0 : vchiq_loud_error_footer();
2260 : : goto fail_free_recycle_thread;
2261 : : }
2262 : 207 : set_user_nice(state->sync_thread, -20);
2263 : :
2264 : 207 : wake_up_process(state->slot_handler_thread);
2265 : 207 : wake_up_process(state->recycle_thread);
2266 : 207 : wake_up_process(state->sync_thread);
2267 : :
2268 : 207 : vchiq_states[0] = state;
2269 : :
2270 : : /* Indicate readiness to the other side */
2271 : 207 : local->initialised = 1;
2272 : :
2273 : 207 : return status;
2274 : :
2275 : : fail_free_recycle_thread:
2276 : 0 : kthread_stop(state->recycle_thread);
2277 : : fail_free_handler_thread:
2278 : 0 : kthread_stop(state->slot_handler_thread);
2279 : :
2280 : 0 : return VCHIQ_ERROR;
2281 : : }
2282 : :
2283 : : /* Called from application thread when a client or server service is created. */
2284 : : struct vchiq_service *
2285 : 1656 : vchiq_add_service_internal(struct vchiq_state *state,
2286 : : const struct vchiq_service_params *params,
2287 : : int srvstate, VCHIQ_INSTANCE_T instance,
2288 : : VCHIQ_USERDATA_TERM_T userdata_term)
2289 : : {
2290 : : struct vchiq_service *service;
2291 : : struct vchiq_service **pservice = NULL;
2292 : : struct vchiq_service_quota *service_quota;
2293 : : int i;
2294 : :
2295 : : service = kmalloc(sizeof(*service), GFP_KERNEL);
2296 [ + + ]: 1656 : if (!service)
2297 : : return service;
2298 : :
2299 : 1655 : service->base.fourcc = params->fourcc;
2300 : 1655 : service->base.callback = params->callback;
2301 : 1655 : service->base.userdata = params->userdata;
2302 : 1655 : service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
2303 : 1655 : service->ref_count = 1;
2304 : 1655 : service->srvstate = VCHIQ_SRVSTATE_FREE;
2305 : 1655 : service->userdata_term = userdata_term;
2306 : 1655 : service->localport = VCHIQ_PORT_FREE;
2307 : 1655 : service->remoteport = VCHIQ_PORT_FREE;
2308 : :
2309 : 1655 : service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
2310 [ + + ]: 1655 : VCHIQ_FOURCC_INVALID : params->fourcc;
2311 : 1655 : service->client_id = 0;
2312 : 1655 : service->auto_close = 1;
2313 : 1655 : service->sync = 0;
2314 : 1655 : service->closing = 0;
2315 : 1655 : service->trace = 0;
2316 : : atomic_set(&service->poll_flags, 0);
2317 : 1655 : service->version = params->version;
2318 : 1655 : service->version_min = params->version_min;
2319 : 1655 : service->state = state;
2320 : 1655 : service->instance = instance;
2321 : 1655 : service->service_use_count = 0;
2322 : : init_bulk_queue(&service->bulk_tx);
2323 : : init_bulk_queue(&service->bulk_rx);
2324 : : init_completion(&service->remove_event);
2325 : : init_completion(&service->bulk_remove_event);
2326 : 1656 : mutex_init(&service->bulk_mutex);
2327 : 1653 : memset(&service->stats, 0, sizeof(service->stats));
2328 : :
2329 : : /* Although it is perfectly possible to use service_spinlock
2330 : : ** to protect the creation of services, it is overkill as it
2331 : : ** disables interrupts while the array is searched.
2332 : : ** The only danger is of another thread trying to create a
2333 : : ** service - service deletion is safe.
2334 : : ** Therefore it is preferable to use state->mutex which,
2335 : : ** although slower to claim, doesn't block interrupts while
2336 : : ** it is held.
2337 : : */
2338 : :
2339 : 1653 : mutex_lock(&state->mutex);
2340 : :
2341 : : /* Prepare to use a previously unused service */
2342 [ + - ]: 1656 : if (state->unused_service < VCHIQ_MAX_SERVICES)
2343 : 1656 : pservice = &state->services[state->unused_service];
2344 : :
2345 [ + + ]: 1656 : if (srvstate == VCHIQ_SRVSTATE_OPENING) {
2346 [ + + ]: 5021 : for (i = 0; i < state->unused_service; i++) {
2347 : 5207 : struct vchiq_service *srv = state->services[i];
2348 : :
2349 [ + + ]: 5207 : if (!srv) {
2350 : 186 : pservice = &state->services[i];
2351 : 186 : break;
2352 : : }
2353 : : }
2354 : : } else {
2355 [ + + ]: 407 : for (i = (state->unused_service - 1); i >= 0; i--) {
2356 : 200 : struct vchiq_service *srv = state->services[i];
2357 : :
2358 [ - + ]: 200 : if (!srv)
2359 : 0 : pservice = &state->services[i];
2360 [ - + ]: 200 : else if ((srv->public_fourcc == params->fourcc)
2361 [ # # # # ]: 0 : && ((srv->instance != instance) ||
2362 : 0 : (srv->base.callback !=
2363 : 0 : params->callback))) {
2364 : : /* There is another server using this
2365 : : ** fourcc which doesn't match. */
2366 : : pservice = NULL;
2367 : : break;
2368 : : }
2369 : : }
2370 : : }
2371 : :
2372 [ + - ]: 1656 : if (pservice) {
2373 : 1656 : service->localport = (pservice - state->services);
2374 [ + + ]: 1656 : if (!handle_seq)
2375 : 207 : handle_seq = VCHIQ_MAX_STATES *
2376 : : VCHIQ_MAX_SERVICES;
2377 : 3312 : service->handle = handle_seq |
2378 : 3312 : (state->id * VCHIQ_MAX_SERVICES) |
2379 : : service->localport;
2380 : 1656 : handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
2381 : 1656 : *pservice = service;
2382 [ + + ]: 1656 : if (pservice == &state->services[state->unused_service])
2383 : 1470 : state->unused_service++;
2384 : : }
2385 : :
2386 : 1656 : mutex_unlock(&state->mutex);
2387 : :
2388 [ - + ]: 1656 : if (!pservice) {
2389 : 0 : kfree(service);
2390 : 0 : return NULL;
2391 : : }
2392 : :
2393 : 1656 : service_quota = &state->service_quotas[service->localport];
2394 : 1656 : service_quota->slot_quota = state->default_slot_quota;
2395 : 1656 : service_quota->message_quota = state->default_message_quota;
2396 [ + + ]: 1656 : if (service_quota->slot_use_count == 0)
2397 : 1470 : service_quota->previous_tx_index =
2398 : 1470 : SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
2399 : 1470 : - 1;
2400 : :
2401 : : /* Bring this service online */
2402 : 1656 : vchiq_set_service_state(service, srvstate);
2403 : :
2404 [ - + # # ]: 1656 : vchiq_log_info(vchiq_core_msg_log_level,
2405 : : "%s Service %c%c%c%c SrcPort:%d",
2406 : : (srvstate == VCHIQ_SRVSTATE_OPENING)
2407 : : ? "Open" : "Add",
2408 : : VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
2409 : : service->localport);
2410 : :
2411 : : /* Don't unlock the service - leave it with a ref_count of 1. */
2412 : :
2413 : 1656 : return service;
2414 : : }
2415 : :
2416 : : VCHIQ_STATUS_T
2417 : 1449 : vchiq_open_service_internal(struct vchiq_service *service, int client_id)
2418 : : {
2419 : 4347 : struct vchiq_open_payload payload = {
2420 : 1449 : service->base.fourcc,
2421 : : client_id,
2422 : 1449 : service->version,
2423 : 1449 : service->version_min
2424 : : };
2425 : : VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2426 : :
2427 : 1449 : service->client_id = client_id;
2428 : 1449 : vchiq_use_service_internal(service);
2429 : 2894 : status = queue_message(service->state,
2430 : : NULL,
2431 : 1447 : VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN,
2432 : : service->localport,
2433 : : 0),
2434 : : memcpy_copy_callback,
2435 : : &payload,
2436 : : sizeof(payload),
2437 : : QMFLAGS_IS_BLOCKING);
2438 [ + - ]: 1449 : if (status == VCHIQ_SUCCESS) {
2439 : : /* Wait for the ACK/NAK */
2440 [ - + ]: 1449 : if (wait_for_completion_interruptible(&service->remove_event)) {
2441 : : status = VCHIQ_RETRY;
2442 : 0 : vchiq_release_service_internal(service);
2443 [ - + ]: 1449 : } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
2444 : : (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
2445 [ # # ]: 0 : if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
2446 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
2447 : : "%d: osi - srvstate = %s (ref %d)",
2448 : : service->state->id,
2449 : : srvstate_names[service->srvstate],
2450 : : service->ref_count);
2451 : : status = VCHIQ_ERROR;
2452 : 0 : VCHIQ_SERVICE_STATS_INC(service, error_count);
2453 : 0 : vchiq_release_service_internal(service);
2454 : : }
2455 : : }
2456 : 1449 : return status;
2457 : : }
2458 : :
2459 : : static void
2460 : 207 : release_service_messages(struct vchiq_service *service)
2461 : : {
2462 : 207 : struct vchiq_state *state = service->state;
2463 : 207 : int slot_last = state->remote->slot_last;
2464 : : int i;
2465 : :
2466 : : /* Release any claimed messages aimed at this service */
2467 : :
2468 [ - + ]: 207 : if (service->sync) {
2469 : 0 : struct vchiq_header *header =
2470 : 0 : (struct vchiq_header *)SLOT_DATA_FROM_INDEX(state,
2471 : : state->remote->slot_sync);
2472 [ # # ]: 0 : if (VCHIQ_MSG_DSTPORT(header->msgid) == service->localport)
2473 : : release_message_sync(state, header);
2474 : :
2475 : 207 : return;
2476 : : }
2477 : :
2478 [ + + ]: 6624 : for (i = state->remote->slot_first; i <= slot_last; i++) {
2479 : 6417 : struct vchiq_slot_info *slot_info =
2480 : 6417 : SLOT_INFO_FROM_INDEX(state, i);
2481 [ + + ]: 6417 : if (slot_info->release_count != slot_info->use_count) {
2482 : 208 : char *data =
2483 : 208 : (char *)SLOT_DATA_FROM_INDEX(state, i);
2484 : : unsigned int pos, end;
2485 : :
2486 : : end = VCHIQ_SLOT_SIZE;
2487 [ + + ]: 208 : if (data == state->rx_data)
2488 : : /* This buffer is still being read from - stop
2489 : : ** at the current read position */
2490 : 207 : end = state->rx_pos & VCHIQ_SLOT_MASK;
2491 : :
2492 : : pos = 0;
2493 : :
2494 [ + + ]: 3674 : while (pos < end) {
2495 : 3258 : struct vchiq_header *header =
2496 : : (struct vchiq_header *)(data + pos);
2497 : 3258 : int msgid = header->msgid;
2498 : 3258 : int port = VCHIQ_MSG_DSTPORT(msgid);
2499 : :
2500 [ + + - + ]: 4217 : if ((port == service->localport) &&
2501 : 959 : (msgid & VCHIQ_MSGID_CLAIMED)) {
2502 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
2503 : : " fsi - hdr %pK", header);
2504 : 0 : release_slot(state, slot_info, header,
2505 : : NULL);
2506 : : }
2507 : 6516 : pos += calc_stride(header->size);
2508 [ - + ]: 3258 : if (pos > VCHIQ_SLOT_SIZE) {
2509 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
2510 : : "fsi - pos %x: header %pK, msgid %x, header->msgid %x, header->size %x",
2511 : : pos, header, msgid,
2512 : : header->msgid, header->size);
2513 : 0 : WARN(1, "invalid slot position\n");
2514 : : }
2515 : : }
2516 : : }
2517 : : }
2518 : : }
2519 : :
2520 : : static int
2521 : 207 : do_abort_bulks(struct vchiq_service *service)
2522 : : {
2523 : : VCHIQ_STATUS_T status;
2524 : :
2525 : : /* Abort any outstanding bulk transfers */
2526 [ + - ]: 207 : if (mutex_lock_killable(&service->bulk_mutex))
2527 : : return 0;
2528 : 207 : abort_outstanding_bulks(service, &service->bulk_tx);
2529 : 207 : abort_outstanding_bulks(service, &service->bulk_rx);
2530 : 207 : mutex_unlock(&service->bulk_mutex);
2531 : :
2532 : 207 : status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
2533 [ + - ]: 207 : if (status == VCHIQ_SUCCESS)
2534 : 207 : status = notify_bulks(service, &service->bulk_rx,
2535 : : 0/*!retry_poll*/);
2536 : 207 : return (status == VCHIQ_SUCCESS);
2537 : : }
2538 : :
2539 : : static VCHIQ_STATUS_T
2540 : 207 : close_service_complete(struct vchiq_service *service, int failstate)
2541 : : {
2542 : : VCHIQ_STATUS_T status;
2543 : 207 : int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2544 : : int newstate;
2545 : :
2546 [ + - - ]: 207 : switch (service->srvstate) {
2547 : : case VCHIQ_SRVSTATE_OPEN:
2548 : : case VCHIQ_SRVSTATE_CLOSESENT:
2549 : : case VCHIQ_SRVSTATE_CLOSERECVD:
2550 [ - + ]: 207 : if (is_server) {
2551 [ # # ]: 0 : if (service->auto_close) {
2552 : 0 : service->client_id = 0;
2553 : 0 : service->remoteport = VCHIQ_PORT_FREE;
2554 : : newstate = VCHIQ_SRVSTATE_LISTENING;
2555 : : } else
2556 : : newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
2557 : : } else
2558 : : newstate = VCHIQ_SRVSTATE_CLOSED;
2559 : 207 : vchiq_set_service_state(service, newstate);
2560 : 207 : break;
2561 : : case VCHIQ_SRVSTATE_LISTENING:
2562 : : break;
2563 : : default:
2564 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
2565 : : "%s(%x) called in state %s", __func__,
2566 : : service->handle, srvstate_names[service->srvstate]);
2567 : 0 : WARN(1, "%s in unexpected state\n", __func__);
2568 : 0 : return VCHIQ_ERROR;
2569 : : }
2570 : :
2571 : 207 : status = make_service_callback(service,
2572 : : VCHIQ_SERVICE_CLOSED, NULL, NULL);
2573 : :
2574 [ + - ]: 207 : if (status != VCHIQ_RETRY) {
2575 : 207 : int uc = service->service_use_count;
2576 : : int i;
2577 : : /* Complete the close process */
2578 [ + + ]: 414 : for (i = 0; i < uc; i++)
2579 : : /* cater for cases where close is forced and the
2580 : : ** client may not close all it's handles */
2581 : 207 : vchiq_release_service_internal(service);
2582 : :
2583 : 207 : service->client_id = 0;
2584 : 207 : service->remoteport = VCHIQ_PORT_FREE;
2585 : :
2586 [ + - ]: 207 : if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
2587 : 207 : vchiq_free_service_internal(service);
2588 [ # # ]: 0 : else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
2589 [ # # ]: 0 : if (is_server)
2590 : 0 : service->closing = 0;
2591 : :
2592 : 0 : complete(&service->remove_event);
2593 : : }
2594 : : } else
2595 : 0 : vchiq_set_service_state(service, failstate);
2596 : :
2597 : 207 : return status;
2598 : : }
2599 : :
2600 : : /* Called by the slot handler */
2601 : : VCHIQ_STATUS_T
2602 : 414 : vchiq_close_service_internal(struct vchiq_service *service, int close_recvd)
2603 : : {
2604 : 414 : struct vchiq_state *state = service->state;
2605 : : VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2606 : 414 : int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
2607 : :
2608 [ - + ]: 414 : vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
2609 : : service->state->id, service->localport, close_recvd,
2610 : : srvstate_names[service->srvstate]);
2611 : :
2612 [ - - - + : 414 : switch (service->srvstate) {
+ - - ]
2613 : : case VCHIQ_SRVSTATE_CLOSED:
2614 : : case VCHIQ_SRVSTATE_HIDDEN:
2615 : : case VCHIQ_SRVSTATE_LISTENING:
2616 : : case VCHIQ_SRVSTATE_CLOSEWAIT:
2617 [ # # ]: 0 : if (close_recvd)
2618 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
2619 : : "%s(1) called "
2620 : : "in state %s",
2621 : : __func__, srvstate_names[service->srvstate]);
2622 [ # # ]: 0 : else if (is_server) {
2623 [ # # ]: 0 : if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
2624 : : status = VCHIQ_ERROR;
2625 : : } else {
2626 : 0 : service->client_id = 0;
2627 : 0 : service->remoteport = VCHIQ_PORT_FREE;
2628 [ # # ]: 0 : if (service->srvstate ==
2629 : : VCHIQ_SRVSTATE_CLOSEWAIT)
2630 : 0 : vchiq_set_service_state(service,
2631 : : VCHIQ_SRVSTATE_LISTENING);
2632 : : }
2633 : 0 : complete(&service->remove_event);
2634 : : } else
2635 : 0 : vchiq_free_service_internal(service);
2636 : : break;
2637 : : case VCHIQ_SRVSTATE_OPENING:
2638 [ # # ]: 0 : if (close_recvd) {
2639 : : /* The open was rejected - tell the user */
2640 : 0 : vchiq_set_service_state(service,
2641 : : VCHIQ_SRVSTATE_CLOSEWAIT);
2642 : 0 : complete(&service->remove_event);
2643 : : } else {
2644 : : /* Shutdown mid-open - let the other side know */
2645 : 0 : status = queue_message(state, service,
2646 : 0 : VCHIQ_MAKE_MSG
2647 : : (VCHIQ_MSG_CLOSE,
2648 : : service->localport,
2649 : : VCHIQ_MSG_DSTPORT(service->remoteport)),
2650 : : NULL, NULL, 0, 0);
2651 : : }
2652 : : break;
2653 : :
2654 : : case VCHIQ_SRVSTATE_OPENSYNC:
2655 : 0 : mutex_lock(&state->sync_mutex);
2656 : : /* fall through */
2657 : : case VCHIQ_SRVSTATE_OPEN:
2658 [ - + ]: 207 : if (close_recvd) {
2659 [ # # ]: 0 : if (!do_abort_bulks(service))
2660 : : status = VCHIQ_RETRY;
2661 : : }
2662 : :
2663 : 207 : release_service_messages(service);
2664 : :
2665 [ + - ]: 207 : if (status == VCHIQ_SUCCESS)
2666 : 207 : status = queue_message(state, service,
2667 : 207 : VCHIQ_MAKE_MSG
2668 : : (VCHIQ_MSG_CLOSE,
2669 : : service->localport,
2670 : : VCHIQ_MSG_DSTPORT(service->remoteport)),
2671 : : NULL, NULL, 0, QMFLAGS_NO_MUTEX_UNLOCK);
2672 : :
2673 [ + - ]: 207 : if (status == VCHIQ_SUCCESS) {
2674 [ + - ]: 207 : if (!close_recvd) {
2675 : : /* Change the state while the mutex is
2676 : : still held */
2677 : 207 : vchiq_set_service_state(service,
2678 : : VCHIQ_SRVSTATE_CLOSESENT);
2679 : 207 : mutex_unlock(&state->slot_mutex);
2680 [ - + ]: 207 : if (service->sync)
2681 : 0 : mutex_unlock(&state->sync_mutex);
2682 : : break;
2683 : : }
2684 [ # # ]: 0 : } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
2685 : 0 : mutex_unlock(&state->sync_mutex);
2686 : 0 : break;
2687 : : } else
2688 : : break;
2689 : :
2690 : : /* Change the state while the mutex is still held */
2691 : 0 : vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSERECVD);
2692 : 0 : mutex_unlock(&state->slot_mutex);
2693 [ # # ]: 0 : if (service->sync)
2694 : 0 : mutex_unlock(&state->sync_mutex);
2695 : :
2696 : 0 : status = close_service_complete(service,
2697 : : VCHIQ_SRVSTATE_CLOSERECVD);
2698 : 0 : break;
2699 : :
2700 : : case VCHIQ_SRVSTATE_CLOSESENT:
2701 [ + - ]: 207 : if (!close_recvd)
2702 : : /* This happens when a process is killed mid-close */
2703 : : break;
2704 : :
2705 [ + - ]: 207 : if (!do_abort_bulks(service)) {
2706 : : status = VCHIQ_RETRY;
2707 : : break;
2708 : : }
2709 : :
2710 : : if (status == VCHIQ_SUCCESS)
2711 : 207 : status = close_service_complete(service,
2712 : : VCHIQ_SRVSTATE_CLOSERECVD);
2713 : : break;
2714 : :
2715 : : case VCHIQ_SRVSTATE_CLOSERECVD:
2716 [ # # ]: 0 : if (!close_recvd && is_server)
2717 : : /* Force into LISTENING mode */
2718 : 0 : vchiq_set_service_state(service,
2719 : : VCHIQ_SRVSTATE_LISTENING);
2720 : 0 : status = close_service_complete(service,
2721 : : VCHIQ_SRVSTATE_CLOSERECVD);
2722 : 0 : break;
2723 : :
2724 : : default:
2725 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
2726 : : "%s(%d) called in state %s", __func__,
2727 : : close_recvd, srvstate_names[service->srvstate]);
2728 : : break;
2729 : : }
2730 : :
2731 : 414 : return status;
2732 : : }
2733 : :
2734 : : /* Called from the application process upon process death */
2735 : : void
2736 : 0 : vchiq_terminate_service_internal(struct vchiq_service *service)
2737 : : {
2738 : 0 : struct vchiq_state *state = service->state;
2739 : :
2740 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
2741 : : state->id, service->localport, service->remoteport);
2742 : :
2743 : : mark_service_closing(service);
2744 : :
2745 : : /* Mark the service for removal by the slot handler */
2746 : 0 : request_poll(state, service, VCHIQ_POLL_REMOVE);
2747 : 0 : }
2748 : :
2749 : : /* Called from the slot handler */
2750 : : void
2751 : 207 : vchiq_free_service_internal(struct vchiq_service *service)
2752 : : {
2753 : 207 : struct vchiq_state *state = service->state;
2754 : :
2755 [ - + ]: 207 : vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
2756 : : state->id, service->localport);
2757 : :
2758 [ - + ]: 207 : switch (service->srvstate) {
2759 : : case VCHIQ_SRVSTATE_OPENING:
2760 : : case VCHIQ_SRVSTATE_CLOSED:
2761 : : case VCHIQ_SRVSTATE_HIDDEN:
2762 : : case VCHIQ_SRVSTATE_LISTENING:
2763 : : case VCHIQ_SRVSTATE_CLOSEWAIT:
2764 : : break;
2765 : : default:
2766 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
2767 : : "%d: fsi - (%d) in state %s",
2768 : : state->id, service->localport,
2769 : : srvstate_names[service->srvstate]);
2770 : 207 : return;
2771 : : }
2772 : :
2773 : 207 : vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
2774 : :
2775 : 207 : complete(&service->remove_event);
2776 : :
2777 : : /* Release the initial lock */
2778 : 207 : unlock_service(service);
2779 : : }
2780 : :
2781 : : VCHIQ_STATUS_T
2782 : 1863 : vchiq_connect_internal(struct vchiq_state *state, VCHIQ_INSTANCE_T instance)
2783 : : {
2784 : : struct vchiq_service *service;
2785 : : int i;
2786 : :
2787 : : /* Find all services registered to this client and enable them. */
2788 : 1863 : i = 0;
2789 [ - + ]: 3726 : while ((service = next_service_by_instance(state, instance,
2790 : : &i)) != NULL) {
2791 [ # # ]: 0 : if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
2792 : 0 : vchiq_set_service_state(service,
2793 : : VCHIQ_SRVSTATE_LISTENING);
2794 : 0 : unlock_service(service);
2795 : : }
2796 : :
2797 [ + + ]: 1863 : if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
2798 [ + - ]: 207 : if (queue_message(state, NULL,
2799 : : VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, NULL,
2800 : : 0, QMFLAGS_IS_BLOCKING) == VCHIQ_RETRY)
2801 : : return VCHIQ_RETRY;
2802 : :
2803 : 207 : vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
2804 : : }
2805 : :
2806 [ + + ]: 1863 : if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
2807 [ + - ]: 207 : if (wait_for_completion_interruptible(&state->connect))
2808 : : return VCHIQ_RETRY;
2809 : :
2810 : 207 : vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
2811 : 207 : complete(&state->connect);
2812 : : }
2813 : :
2814 : : return VCHIQ_SUCCESS;
2815 : : }
2816 : :
2817 : : VCHIQ_STATUS_T
2818 : 0 : vchiq_shutdown_internal(struct vchiq_state *state, VCHIQ_INSTANCE_T instance)
2819 : : {
2820 : : struct vchiq_service *service;
2821 : : int i;
2822 : :
2823 : : /* Find all services registered to this client and enable them. */
2824 : 0 : i = 0;
2825 [ # # ]: 0 : while ((service = next_service_by_instance(state, instance,
2826 : : &i)) != NULL) {
2827 : 0 : (void)vchiq_remove_service(service->handle);
2828 : 0 : unlock_service(service);
2829 : : }
2830 : :
2831 : 0 : return VCHIQ_SUCCESS;
2832 : : }
2833 : :
2834 : : VCHIQ_STATUS_T
2835 : 207 : vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
2836 : : {
2837 : : /* Unregister the service */
2838 : 207 : struct vchiq_service *service = find_service_by_handle(handle);
2839 : : VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2840 : :
2841 [ + - ]: 207 : if (!service)
2842 : : return VCHIQ_ERROR;
2843 : :
2844 [ - + ]: 207 : vchiq_log_info(vchiq_core_log_level,
2845 : : "%d: close_service:%d",
2846 : : service->state->id, service->localport);
2847 : :
2848 [ + - ]: 207 : if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
2849 [ - + ]: 207 : (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
2850 : : (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
2851 : 0 : unlock_service(service);
2852 : 0 : return VCHIQ_ERROR;
2853 : : }
2854 : :
2855 : : mark_service_closing(service);
2856 : :
2857 [ - + ]: 207 : if (current == service->state->slot_handler_thread) {
2858 : 0 : status = vchiq_close_service_internal(service,
2859 : : 0/*!close_recvd*/);
2860 [ # # ]: 0 : WARN_ON(status == VCHIQ_RETRY);
2861 : : } else {
2862 : : /* Mark the service for termination by the slot handler */
2863 : 207 : request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
2864 : : }
2865 : :
2866 : : while (1) {
2867 [ + - ]: 207 : if (wait_for_completion_interruptible(&service->remove_event)) {
2868 : : status = VCHIQ_RETRY;
2869 : : break;
2870 : : }
2871 : :
2872 [ - + ]: 207 : if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
2873 [ # # ]: 0 : (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
2874 : : (service->srvstate == VCHIQ_SRVSTATE_OPEN))
2875 : : break;
2876 : :
2877 [ # # ]: 0 : vchiq_log_warning(vchiq_core_log_level,
2878 : : "%d: close_service:%d - waiting in state %s",
2879 : : service->state->id, service->localport,
2880 : : srvstate_names[service->srvstate]);
2881 : : }
2882 : :
2883 [ + - - + ]: 414 : if ((status == VCHIQ_SUCCESS) &&
2884 [ # # ]: 207 : (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
2885 : : (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
2886 : : status = VCHIQ_ERROR;
2887 : :
2888 : 207 : unlock_service(service);
2889 : :
2890 : 207 : return status;
2891 : : }
2892 : :
2893 : : VCHIQ_STATUS_T
2894 : 0 : vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
2895 : : {
2896 : : /* Unregister the service */
2897 : 0 : struct vchiq_service *service = find_service_by_handle(handle);
2898 : : VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2899 : :
2900 [ # # ]: 0 : if (!service)
2901 : : return VCHIQ_ERROR;
2902 : :
2903 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
2904 : : "%d: remove_service:%d",
2905 : : service->state->id, service->localport);
2906 : :
2907 [ # # ]: 0 : if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
2908 : 0 : unlock_service(service);
2909 : 0 : return VCHIQ_ERROR;
2910 : : }
2911 : :
2912 : : mark_service_closing(service);
2913 : :
2914 [ # # # # ]: 0 : if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
2915 : 0 : (current == service->state->slot_handler_thread)) {
2916 : : /* Make it look like a client, because it must be removed and
2917 : : not left in the LISTENING state. */
2918 : 0 : service->public_fourcc = VCHIQ_FOURCC_INVALID;
2919 : :
2920 : 0 : status = vchiq_close_service_internal(service,
2921 : : 0/*!close_recvd*/);
2922 [ # # ]: 0 : WARN_ON(status == VCHIQ_RETRY);
2923 : : } else {
2924 : : /* Mark the service for removal by the slot handler */
2925 : 0 : request_poll(service->state, service, VCHIQ_POLL_REMOVE);
2926 : : }
2927 : : while (1) {
2928 [ # # ]: 0 : if (wait_for_completion_interruptible(&service->remove_event)) {
2929 : : status = VCHIQ_RETRY;
2930 : : break;
2931 : : }
2932 : :
2933 [ # # ]: 0 : if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
2934 : : (service->srvstate == VCHIQ_SRVSTATE_OPEN))
2935 : : break;
2936 : :
2937 [ # # ]: 0 : vchiq_log_warning(vchiq_core_log_level,
2938 : : "%d: remove_service:%d - waiting in state %s",
2939 : : service->state->id, service->localport,
2940 : : srvstate_names[service->srvstate]);
2941 : : }
2942 : :
2943 [ # # # # ]: 0 : if ((status == VCHIQ_SUCCESS) &&
2944 : 0 : (service->srvstate != VCHIQ_SRVSTATE_FREE))
2945 : : status = VCHIQ_ERROR;
2946 : :
2947 : 0 : unlock_service(service);
2948 : :
2949 : 0 : return status;
2950 : : }
2951 : :
2952 : : /* This function may be called by kernel threads or user threads.
2953 : : * User threads may receive VCHIQ_RETRY to indicate that a signal has been
2954 : : * received and the call should be retried after being returned to user
2955 : : * context.
2956 : : * When called in blocking mode, the userdata field points to a bulk_waiter
2957 : : * structure.
2958 : : */
2959 : 0 : VCHIQ_STATUS_T vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
2960 : : void *offset, int size, void *userdata,
2961 : : VCHIQ_BULK_MODE_T mode,
2962 : : VCHIQ_BULK_DIR_T dir)
2963 : : {
2964 : 0 : struct vchiq_service *service = find_service_by_handle(handle);
2965 : : struct vchiq_bulk_queue *queue;
2966 : : struct vchiq_bulk *bulk;
2967 : : struct vchiq_state *state;
2968 : : struct bulk_waiter *bulk_waiter = NULL;
2969 [ # # ]: 0 : const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
2970 : : const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
2971 [ # # ]: 0 : VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
2972 : : VCHIQ_STATUS_T status = VCHIQ_ERROR;
2973 : : int payload[2];
2974 : :
2975 [ # # # # : 0 : if (!service || service->srvstate != VCHIQ_SRVSTATE_OPEN ||
# # ]
2976 [ # # ]: 0 : !offset || vchiq_check_service(service) != VCHIQ_SUCCESS)
2977 : : goto error_exit;
2978 : :
2979 [ # # # # ]: 0 : switch (mode) {
2980 : : case VCHIQ_BULK_MODE_NOCALLBACK:
2981 : : case VCHIQ_BULK_MODE_CALLBACK:
2982 : : break;
2983 : : case VCHIQ_BULK_MODE_BLOCKING:
2984 : : bulk_waiter = userdata;
2985 : : init_completion(&bulk_waiter->event);
2986 : 0 : bulk_waiter->actual = 0;
2987 : 0 : bulk_waiter->bulk = NULL;
2988 : 0 : break;
2989 : : case VCHIQ_BULK_MODE_WAITING:
2990 : : bulk_waiter = userdata;
2991 : 0 : bulk = bulk_waiter->bulk;
2992 : 0 : goto waiting;
2993 : : default:
2994 : : goto error_exit;
2995 : : }
2996 : :
2997 : 0 : state = service->state;
2998 : :
2999 : : queue = (dir == VCHIQ_BULK_TRANSMIT) ?
3000 [ # # ]: 0 : &service->bulk_tx : &service->bulk_rx;
3001 : :
3002 [ # # ]: 0 : if (mutex_lock_killable(&service->bulk_mutex)) {
3003 : : status = VCHIQ_RETRY;
3004 : : goto error_exit;
3005 : : }
3006 : :
3007 [ # # ]: 0 : if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
3008 : 0 : VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
3009 : : do {
3010 : 0 : mutex_unlock(&service->bulk_mutex);
3011 [ # # ]: 0 : if (wait_for_completion_interruptible(
3012 : : &service->bulk_remove_event)) {
3013 : : status = VCHIQ_RETRY;
3014 : : goto error_exit;
3015 : : }
3016 [ # # ]: 0 : if (mutex_lock_killable(&service->bulk_mutex)) {
3017 : : status = VCHIQ_RETRY;
3018 : : goto error_exit;
3019 : : }
3020 : 0 : } while (queue->local_insert == queue->remove +
3021 [ # # ]: 0 : VCHIQ_NUM_SERVICE_BULKS);
3022 : : }
3023 : :
3024 : 0 : bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
3025 : :
3026 : 0 : bulk->mode = mode;
3027 : 0 : bulk->dir = dir;
3028 : 0 : bulk->userdata = userdata;
3029 : 0 : bulk->size = size;
3030 : 0 : bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
3031 : :
3032 [ # # ]: 0 : if (vchiq_prepare_bulk_data(bulk, offset, size, dir) != VCHIQ_SUCCESS)
3033 : : goto unlock_error_exit;
3034 : :
3035 : 0 : wmb();
3036 : :
3037 [ # # ]: 0 : vchiq_log_info(vchiq_core_log_level,
3038 : : "%d: bt (%d->%d) %cx %x@%pK %pK",
3039 : : state->id, service->localport, service->remoteport, dir_char,
3040 : : size, bulk->data, userdata);
3041 : :
3042 : : /* The slot mutex must be held when the service is being closed, so
3043 : : claim it here to ensure that isn't happening */
3044 [ # # ]: 0 : if (mutex_lock_killable(&state->slot_mutex)) {
3045 : : status = VCHIQ_RETRY;
3046 : : goto cancel_bulk_error_exit;
3047 : : }
3048 : :
3049 [ # # ]: 0 : if (service->srvstate != VCHIQ_SRVSTATE_OPEN)
3050 : : goto unlock_both_error_exit;
3051 : :
3052 : 0 : payload[0] = (int)(long)bulk->data;
3053 : 0 : payload[1] = bulk->size;
3054 : 0 : status = queue_message(state,
3055 : : NULL,
3056 : 0 : VCHIQ_MAKE_MSG(dir_msgtype,
3057 : : service->localport,
3058 : : service->remoteport),
3059 : : memcpy_copy_callback,
3060 : : &payload,
3061 : : sizeof(payload),
3062 : : QMFLAGS_IS_BLOCKING |
3063 : : QMFLAGS_NO_MUTEX_LOCK |
3064 : : QMFLAGS_NO_MUTEX_UNLOCK);
3065 [ # # ]: 0 : if (status != VCHIQ_SUCCESS)
3066 : : goto unlock_both_error_exit;
3067 : :
3068 : 0 : queue->local_insert++;
3069 : :
3070 : 0 : mutex_unlock(&state->slot_mutex);
3071 : 0 : mutex_unlock(&service->bulk_mutex);
3072 : :
3073 [ # # ]: 0 : vchiq_log_trace(vchiq_core_log_level,
3074 : : "%d: bt:%d %cx li=%x ri=%x p=%x",
3075 : : state->id,
3076 : : service->localport, dir_char,
3077 : : queue->local_insert, queue->remote_insert, queue->process);
3078 : :
3079 : : waiting:
3080 : 0 : unlock_service(service);
3081 : :
3082 : : status = VCHIQ_SUCCESS;
3083 : :
3084 [ # # ]: 0 : if (bulk_waiter) {
3085 : 0 : bulk_waiter->bulk = bulk;
3086 [ # # ]: 0 : if (wait_for_completion_interruptible(&bulk_waiter->event))
3087 : : status = VCHIQ_RETRY;
3088 [ # # ]: 0 : else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
3089 : : status = VCHIQ_ERROR;
3090 : : }
3091 : :
3092 : 0 : return status;
3093 : :
3094 : : unlock_both_error_exit:
3095 : 0 : mutex_unlock(&state->slot_mutex);
3096 : : cancel_bulk_error_exit:
3097 : 0 : vchiq_complete_bulk(bulk);
3098 : : unlock_error_exit:
3099 : 0 : mutex_unlock(&service->bulk_mutex);
3100 : :
3101 : : error_exit:
3102 [ # # ]: 0 : if (service)
3103 : 0 : unlock_service(service);
3104 : 0 : return status;
3105 : : }
3106 : :
3107 : : VCHIQ_STATUS_T
3108 : 8070 : vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
3109 : : ssize_t (*copy_callback)(void *context, void *dest,
3110 : : size_t offset, size_t maxsize),
3111 : : void *context,
3112 : : size_t size)
3113 : : {
3114 : 8070 : struct vchiq_service *service = find_service_by_handle(handle);
3115 : : VCHIQ_STATUS_T status = VCHIQ_ERROR;
3116 : :
3117 [ + - + - ]: 16144 : if (!service ||
3118 : 8073 : (vchiq_check_service(service) != VCHIQ_SUCCESS))
3119 : : goto error_exit;
3120 : :
3121 [ - + ]: 8073 : if (!size) {
3122 : 0 : VCHIQ_SERVICE_STATS_INC(service, error_count);
3123 : 0 : goto error_exit;
3124 : :
3125 : : }
3126 : :
3127 [ - + ]: 8073 : if (size > VCHIQ_MAX_MSG_SIZE) {
3128 : 0 : VCHIQ_SERVICE_STATS_INC(service, error_count);
3129 : 0 : goto error_exit;
3130 : : }
3131 : :
3132 [ + - + ]: 8073 : switch (service->srvstate) {
3133 : : case VCHIQ_SRVSTATE_OPEN:
3134 : 16130 : status = queue_message(service->state, service,
3135 : 8065 : VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3136 : : service->localport,
3137 : : service->remoteport),
3138 : : copy_callback, context, size, 1);
3139 : 8073 : break;
3140 : : case VCHIQ_SRVSTATE_OPENSYNC:
3141 : 0 : status = queue_message_sync(service->state, service,
3142 : 0 : VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
3143 : : service->localport,
3144 : : service->remoteport),
3145 : : copy_callback, context, size, 1);
3146 : 0 : break;
3147 : : default:
3148 : : status = VCHIQ_ERROR;
3149 : : break;
3150 : : }
3151 : :
3152 : : error_exit:
3153 [ + + ]: 8079 : if (service)
3154 : 8073 : unlock_service(service);
3155 : :
3156 : 8079 : return status;
3157 : : }
3158 : :
3159 : : void
3160 : 8280 : vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle,
3161 : : struct vchiq_header *header)
3162 : : {
3163 : 8280 : struct vchiq_service *service = find_service_by_handle(handle);
3164 : : struct vchiq_shared_state *remote;
3165 : : struct vchiq_state *state;
3166 : : int slot_index;
3167 : :
3168 [ + - ]: 8280 : if (!service)
3169 : 8280 : return;
3170 : :
3171 : 8280 : state = service->state;
3172 : 8280 : remote = state->remote;
3173 : :
3174 : 8280 : slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
3175 : :
3176 [ + - + + ]: 16560 : if ((slot_index >= remote->slot_first) &&
3177 : 8280 : (slot_index <= remote->slot_last)) {
3178 : 8073 : int msgid = header->msgid;
3179 : :
3180 [ + - ]: 8073 : if (msgid & VCHIQ_MSGID_CLAIMED) {
3181 : 8073 : struct vchiq_slot_info *slot_info =
3182 : : SLOT_INFO_FROM_INDEX(state, slot_index);
3183 : :
3184 : 8073 : release_slot(state, slot_info, header, service);
3185 : : }
3186 [ - + ]: 207 : } else if (slot_index == remote->slot_sync)
3187 : : release_message_sync(state, header);
3188 : :
3189 : 8280 : unlock_service(service);
3190 : : }
3191 : :
3192 : : static void
3193 : : release_message_sync(struct vchiq_state *state, struct vchiq_header *header)
3194 : : {
3195 : 0 : header->msgid = VCHIQ_MSGID_PADDING;
3196 : 0 : remote_event_signal(&state->remote->sync_release);
3197 : : }
3198 : :
3199 : : VCHIQ_STATUS_T
3200 : 0 : vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
3201 : : {
3202 : : VCHIQ_STATUS_T status = VCHIQ_ERROR;
3203 : 0 : struct vchiq_service *service = find_service_by_handle(handle);
3204 : :
3205 [ # # # # ]: 0 : if (!service ||
3206 [ # # ]: 0 : (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
3207 : : !peer_version)
3208 : : goto exit;
3209 : 0 : *peer_version = service->peer_version;
3210 : : status = VCHIQ_SUCCESS;
3211 : :
3212 : : exit:
3213 [ # # ]: 0 : if (service)
3214 : 0 : unlock_service(service);
3215 : 0 : return status;
3216 : : }
3217 : :
3218 : 0 : void vchiq_get_config(struct vchiq_config *config)
3219 : : {
3220 : 0 : config->max_msg_size = VCHIQ_MAX_MSG_SIZE;
3221 : 0 : config->bulk_threshold = VCHIQ_MAX_MSG_SIZE;
3222 : 0 : config->max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
3223 : 0 : config->max_services = VCHIQ_MAX_SERVICES;
3224 : 0 : config->version = VCHIQ_VERSION;
3225 : 0 : config->version_min = VCHIQ_VERSION_MIN;
3226 : 0 : }
3227 : :
3228 : : VCHIQ_STATUS_T
3229 : 0 : vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
3230 : : VCHIQ_SERVICE_OPTION_T option, int value)
3231 : : {
3232 : 0 : struct vchiq_service *service = find_service_by_handle(handle);
3233 : : VCHIQ_STATUS_T status = VCHIQ_ERROR;
3234 : :
3235 [ # # ]: 0 : if (service) {
3236 [ # # # # : 0 : switch (option) {
# # ]
3237 : : case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
3238 : 0 : service->auto_close = value;
3239 : : status = VCHIQ_SUCCESS;
3240 : 0 : break;
3241 : :
3242 : : case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
3243 : : struct vchiq_service_quota *service_quota =
3244 : 0 : &service->state->service_quotas[
3245 : 0 : service->localport];
3246 [ # # ]: 0 : if (value == 0)
3247 : 0 : value = service->state->default_slot_quota;
3248 [ # # # # ]: 0 : if ((value >= service_quota->slot_use_count) &&
3249 : : (value < (unsigned short)~0)) {
3250 : 0 : service_quota->slot_quota = value;
3251 [ # # # # ]: 0 : if ((value >= service_quota->slot_use_count) &&
3252 : 0 : (service_quota->message_quota >=
3253 : 0 : service_quota->message_use_count)) {
3254 : : /* Signal the service that it may have
3255 : : ** dropped below its quota */
3256 : 0 : complete(&service_quota->quota_event);
3257 : : }
3258 : : status = VCHIQ_SUCCESS;
3259 : : }
3260 : : } break;
3261 : :
3262 : : case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
3263 : : struct vchiq_service_quota *service_quota =
3264 : 0 : &service->state->service_quotas[
3265 : 0 : service->localport];
3266 [ # # ]: 0 : if (value == 0)
3267 : 0 : value = service->state->default_message_quota;
3268 [ # # # # ]: 0 : if ((value >= service_quota->message_use_count) &&
3269 : : (value < (unsigned short)~0)) {
3270 : 0 : service_quota->message_quota = value;
3271 [ # # ]: 0 : if ((value >=
3272 [ # # ]: 0 : service_quota->message_use_count) &&
3273 : 0 : (service_quota->slot_quota >=
3274 : 0 : service_quota->slot_use_count))
3275 : : /* Signal the service that it may have
3276 : : ** dropped below its quota */
3277 : 0 : complete(&service_quota->quota_event);
3278 : : status = VCHIQ_SUCCESS;
3279 : : }
3280 : : } break;
3281 : :
3282 : : case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
3283 [ # # ]: 0 : if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
3284 : : (service->srvstate ==
3285 : : VCHIQ_SRVSTATE_LISTENING)) {
3286 : 0 : service->sync = value;
3287 : : status = VCHIQ_SUCCESS;
3288 : : }
3289 : : break;
3290 : :
3291 : : case VCHIQ_SERVICE_OPTION_TRACE:
3292 : 0 : service->trace = value;
3293 : : status = VCHIQ_SUCCESS;
3294 : 0 : break;
3295 : :
3296 : : default:
3297 : : break;
3298 : : }
3299 : 0 : unlock_service(service);
3300 : : }
3301 : :
3302 : 0 : return status;
3303 : : }
3304 : :
3305 : : static void
3306 : 0 : vchiq_dump_shared_state(void *dump_context, struct vchiq_state *state,
3307 : : struct vchiq_shared_state *shared, const char *label)
3308 : : {
3309 : : static const char *const debug_names[] = {
3310 : : "<entries>",
3311 : : "SLOT_HANDLER_COUNT",
3312 : : "SLOT_HANDLER_LINE",
3313 : : "PARSE_LINE",
3314 : : "PARSE_HEADER",
3315 : : "PARSE_MSGID",
3316 : : "AWAIT_COMPLETION_LINE",
3317 : : "DEQUEUE_MESSAGE_LINE",
3318 : : "SERVICE_CALLBACK_LINE",
3319 : : "MSG_QUEUE_FULL_COUNT",
3320 : : "COMPLETION_QUEUE_FULL_COUNT"
3321 : : };
3322 : : int i;
3323 : : char buf[80];
3324 : : int len;
3325 : :
3326 : 0 : len = scnprintf(buf, sizeof(buf),
3327 : : " %s: slots %d-%d tx_pos=%x recycle=%x",
3328 : : label, shared->slot_first, shared->slot_last,
3329 : : shared->tx_pos, shared->slot_queue_recycle);
3330 : 0 : vchiq_dump(dump_context, buf, len + 1);
3331 : :
3332 : 0 : len = scnprintf(buf, sizeof(buf),
3333 : : " Slots claimed:");
3334 : 0 : vchiq_dump(dump_context, buf, len + 1);
3335 : :
3336 [ # # ]: 0 : for (i = shared->slot_first; i <= shared->slot_last; i++) {
3337 : 0 : struct vchiq_slot_info slot_info =
3338 : 0 : *SLOT_INFO_FROM_INDEX(state, i);
3339 [ # # ]: 0 : if (slot_info.use_count != slot_info.release_count) {
3340 : 0 : len = scnprintf(buf, sizeof(buf),
3341 : : " %d: %d/%d", i, slot_info.use_count,
3342 : : slot_info.release_count);
3343 : 0 : vchiq_dump(dump_context, buf, len + 1);
3344 : : }
3345 : : }
3346 : :
3347 [ # # ]: 0 : for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
3348 : 0 : len = scnprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
3349 : : debug_names[i], shared->debug[i], shared->debug[i]);
3350 : 0 : vchiq_dump(dump_context, buf, len + 1);
3351 : : }
3352 : 0 : }
3353 : :
3354 : : void
3355 : 0 : vchiq_dump_state(void *dump_context, struct vchiq_state *state)
3356 : : {
3357 : : char buf[80];
3358 : : int len;
3359 : : int i;
3360 : :
3361 : 0 : len = scnprintf(buf, sizeof(buf), "State %d: %s", state->id,
3362 : 0 : conn_state_names[state->conn_state]);
3363 : 0 : vchiq_dump(dump_context, buf, len + 1);
3364 : :
3365 : 0 : len = scnprintf(buf, sizeof(buf),
3366 : : " tx_pos=%x(@%pK), rx_pos=%x(@%pK)",
3367 : 0 : state->local->tx_pos,
3368 : 0 : state->tx_data + (state->local_tx_pos & VCHIQ_SLOT_MASK),
3369 : : state->rx_pos,
3370 : 0 : state->rx_data + (state->rx_pos & VCHIQ_SLOT_MASK));
3371 : 0 : vchiq_dump(dump_context, buf, len + 1);
3372 : :
3373 : 0 : len = scnprintf(buf, sizeof(buf),
3374 : : " Version: %d (min %d)",
3375 : : VCHIQ_VERSION, VCHIQ_VERSION_MIN);
3376 : 0 : vchiq_dump(dump_context, buf, len + 1);
3377 : :
3378 : : if (VCHIQ_ENABLE_STATS) {
3379 : 0 : len = scnprintf(buf, sizeof(buf),
3380 : : " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
3381 : : "error_count=%d",
3382 : : state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
3383 : : state->stats.error_count);
3384 : 0 : vchiq_dump(dump_context, buf, len + 1);
3385 : : }
3386 : :
3387 : 0 : len = scnprintf(buf, sizeof(buf),
3388 : : " Slots: %d available (%d data), %d recyclable, %d stalls "
3389 : : "(%d data)",
3390 : 0 : ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
3391 : 0 : state->local_tx_pos) / VCHIQ_SLOT_SIZE,
3392 : 0 : state->data_quota - state->data_use_count,
3393 : 0 : state->local->slot_queue_recycle - state->slot_queue_available,
3394 : : state->stats.slot_stalls, state->stats.data_stalls);
3395 : 0 : vchiq_dump(dump_context, buf, len + 1);
3396 : :
3397 : 0 : vchiq_dump_platform_state(dump_context);
3398 : :
3399 : 0 : vchiq_dump_shared_state(dump_context, state, state->local, "Local");
3400 : 0 : vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
3401 : :
3402 : 0 : vchiq_dump_platform_instances(dump_context);
3403 : :
3404 [ # # ]: 0 : for (i = 0; i < state->unused_service; i++) {
3405 : 0 : struct vchiq_service *service = find_service_by_port(state, i);
3406 : :
3407 [ # # ]: 0 : if (service) {
3408 : 0 : vchiq_dump_service_state(dump_context, service);
3409 : 0 : unlock_service(service);
3410 : : }
3411 : : }
3412 : 0 : }
3413 : :
3414 : : void
3415 : 0 : vchiq_dump_service_state(void *dump_context, struct vchiq_service *service)
3416 : : {
3417 : : char buf[80];
3418 : : int len;
3419 : :
3420 : 0 : len = scnprintf(buf, sizeof(buf), "Service %u: %s (ref %u)",
3421 : 0 : service->localport, srvstate_names[service->srvstate],
3422 : 0 : service->ref_count - 1); /*Don't include the lock just taken*/
3423 : :
3424 [ # # ]: 0 : if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
3425 : : char remoteport[30];
3426 : : struct vchiq_service_quota *service_quota =
3427 : 0 : &service->state->service_quotas[service->localport];
3428 : 0 : int fourcc = service->base.fourcc;
3429 : : int tx_pending, rx_pending;
3430 : :
3431 [ # # ]: 0 : if (service->remoteport != VCHIQ_PORT_FREE) {
3432 : 0 : int len2 = scnprintf(remoteport, sizeof(remoteport),
3433 : : "%u", service->remoteport);
3434 : :
3435 [ # # ]: 0 : if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
3436 : 0 : scnprintf(remoteport + len2,
3437 : : sizeof(remoteport) - len2,
3438 : : " (client %x)", service->client_id);
3439 : : } else
3440 : 0 : strcpy(remoteport, "n/a");
3441 : :
3442 : 0 : len += scnprintf(buf + len, sizeof(buf) - len,
3443 : : " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
3444 : 0 : VCHIQ_FOURCC_AS_4CHARS(fourcc),
3445 : : remoteport,
3446 : 0 : service_quota->message_use_count,
3447 : 0 : service_quota->message_quota,
3448 : 0 : service_quota->slot_use_count,
3449 : 0 : service_quota->slot_quota);
3450 : :
3451 : 0 : vchiq_dump(dump_context, buf, len + 1);
3452 : :
3453 : 0 : tx_pending = service->bulk_tx.local_insert -
3454 : 0 : service->bulk_tx.remote_insert;
3455 : :
3456 : 0 : rx_pending = service->bulk_rx.local_insert -
3457 : 0 : service->bulk_rx.remote_insert;
3458 : :
3459 [ # # # # ]: 0 : len = scnprintf(buf, sizeof(buf),
3460 : : " Bulk: tx_pending=%d (size %d),"
3461 : : " rx_pending=%d (size %d)",
3462 : : tx_pending,
3463 : : tx_pending ? service->bulk_tx.bulks[
3464 : 0 : BULK_INDEX(service->bulk_tx.remove)].size : 0,
3465 : : rx_pending,
3466 : : rx_pending ? service->bulk_rx.bulks[
3467 : 0 : BULK_INDEX(service->bulk_rx.remove)].size : 0);
3468 : :
3469 : : if (VCHIQ_ENABLE_STATS) {
3470 : 0 : vchiq_dump(dump_context, buf, len + 1);
3471 : :
3472 : 0 : len = scnprintf(buf, sizeof(buf),
3473 : : " Ctrl: tx_count=%d, tx_bytes=%llu, "
3474 : : "rx_count=%d, rx_bytes=%llu",
3475 : : service->stats.ctrl_tx_count,
3476 : : service->stats.ctrl_tx_bytes,
3477 : : service->stats.ctrl_rx_count,
3478 : : service->stats.ctrl_rx_bytes);
3479 : 0 : vchiq_dump(dump_context, buf, len + 1);
3480 : :
3481 : 0 : len = scnprintf(buf, sizeof(buf),
3482 : : " Bulk: tx_count=%d, tx_bytes=%llu, "
3483 : : "rx_count=%d, rx_bytes=%llu",
3484 : : service->stats.bulk_tx_count,
3485 : : service->stats.bulk_tx_bytes,
3486 : : service->stats.bulk_rx_count,
3487 : : service->stats.bulk_rx_bytes);
3488 : 0 : vchiq_dump(dump_context, buf, len + 1);
3489 : :
3490 : 0 : len = scnprintf(buf, sizeof(buf),
3491 : : " %d quota stalls, %d slot stalls, "
3492 : : "%d bulk stalls, %d aborted, %d errors",
3493 : : service->stats.quota_stalls,
3494 : : service->stats.slot_stalls,
3495 : : service->stats.bulk_stalls,
3496 : : service->stats.bulk_aborted_count,
3497 : : service->stats.error_count);
3498 : : }
3499 : : }
3500 : :
3501 : 0 : vchiq_dump(dump_context, buf, len + 1);
3502 : :
3503 [ # # ]: 0 : if (service->srvstate != VCHIQ_SRVSTATE_FREE)
3504 : 0 : vchiq_dump_platform_service_state(dump_context, service);
3505 : 0 : }
3506 : :
3507 : : void
3508 : 0 : vchiq_loud_error_header(void)
3509 : : {
3510 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
3511 : : "============================================================"
3512 : : "================");
3513 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
3514 : : "============================================================"
3515 : : "================");
3516 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level, "=====");
3517 : 0 : }
3518 : :
3519 : : void
3520 : 0 : vchiq_loud_error_footer(void)
3521 : : {
3522 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level, "=====");
3523 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
3524 : : "============================================================"
3525 : : "================");
3526 [ # # ]: 0 : vchiq_log_error(vchiq_core_log_level,
3527 : : "============================================================"
3528 : : "================");
3529 : 0 : }
3530 : :
3531 : 0 : VCHIQ_STATUS_T vchiq_send_remote_use(struct vchiq_state *state)
3532 : : {
3533 : : VCHIQ_STATUS_T status = VCHIQ_RETRY;
3534 : :
3535 [ # # ]: 0 : if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3536 : 0 : status = queue_message(state, NULL,
3537 : : VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
3538 : : NULL, NULL, 0, 0);
3539 : 0 : return status;
3540 : : }
3541 : :
3542 : 0 : VCHIQ_STATUS_T vchiq_send_remote_use_active(struct vchiq_state *state)
3543 : : {
3544 : : VCHIQ_STATUS_T status = VCHIQ_RETRY;
3545 : :
3546 [ # # ]: 0 : if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
3547 : 0 : status = queue_message(state, NULL,
3548 : : VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
3549 : : NULL, NULL, 0, 0);
3550 : 0 : return status;
3551 : : }
3552 : :
3553 : 0 : void vchiq_log_dump_mem(const char *label, u32 addr, const void *void_mem,
3554 : : size_t num_bytes)
3555 : : {
3556 : : const u8 *mem = void_mem;
3557 : : size_t offset;
3558 : : char line_buf[100];
3559 : : char *s;
3560 : :
3561 [ # # ]: 0 : while (num_bytes > 0) {
3562 : : s = line_buf;
3563 : :
3564 [ # # ]: 0 : for (offset = 0; offset < 16; offset++) {
3565 [ # # ]: 0 : if (offset < num_bytes)
3566 : 0 : s += scnprintf(s, 4, "%02x ", mem[offset]);
3567 : : else
3568 : 0 : s += scnprintf(s, 4, " ");
3569 : : }
3570 : :
3571 [ # # ]: 0 : for (offset = 0; offset < 16; offset++) {
3572 [ # # ]: 0 : if (offset < num_bytes) {
3573 : 0 : u8 ch = mem[offset];
3574 : :
3575 [ # # ]: 0 : if ((ch < ' ') || (ch > '~'))
3576 : : ch = '.';
3577 : 0 : *s++ = (char)ch;
3578 : : }
3579 : : }
3580 : 0 : *s++ = '\0';
3581 : :
3582 [ # # # # ]: 0 : if ((label != NULL) && (*label != '\0'))
3583 : 0 : vchiq_log_trace(VCHIQ_LOG_TRACE,
3584 : : "%s: %08x: %s", label, addr, line_buf);
3585 : : else
3586 : 0 : vchiq_log_trace(VCHIQ_LOG_TRACE,
3587 : : "%08x: %s", addr, line_buf);
3588 : :
3589 : 0 : addr += 16;
3590 : 0 : mem += 16;
3591 [ # # ]: 0 : if (num_bytes > 16)
3592 : 0 : num_bytes -= 16;
3593 : : else
3594 : : num_bytes = 0;
3595 : : }
3596 : 0 : }
|