4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
15 static void gb_connection_kref_release(struct kref *kref);
18 static DEFINE_SPINLOCK(gb_connections_lock);
19 static DEFINE_MUTEX(gb_connection_mutex);
22 /* Caller holds gb_connection_mutex. */
23 static struct gb_connection *
24 gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
26 struct gb_host_device *hd = intf->hd;
27 struct gb_connection *connection;
29 list_for_each_entry(connection, &hd->connections, hd_links) {
30 if (connection->intf == intf &&
31 connection->intf_cport_id == cport_id)
38 static void gb_connection_get(struct gb_connection *connection)
40 kref_get(&connection->kref);
43 static void gb_connection_put(struct gb_connection *connection)
45 kref_put(&connection->kref, gb_connection_kref_release);
49 * Returns a reference-counted pointer to the connection if found.
51 static struct gb_connection *
52 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
54 struct gb_connection *connection;
57 spin_lock_irqsave(&gb_connections_lock, flags);
58 list_for_each_entry(connection, &hd->connections, hd_links)
59 if (connection->hd_cport_id == cport_id) {
60 gb_connection_get(connection);
65 spin_unlock_irqrestore(&gb_connections_lock, flags);
71 * Callback from the host driver to let us know that data has been
72 * received on the bundle.
74 void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
75 u8 *data, size_t length)
77 struct gb_connection *connection;
79 connection = gb_connection_hd_find(hd, cport_id);
82 "nonexistent connection (%zu bytes dropped)\n", length);
85 gb_connection_recv(connection, data, length);
86 gb_connection_put(connection);
88 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
90 static void gb_connection_kref_release(struct kref *kref)
92 struct gb_connection *connection;
94 connection = container_of(kref, struct gb_connection, kref);
99 static void gb_connection_init_name(struct gb_connection *connection)
101 u16 hd_cport_id = connection->hd_cport_id;
105 if (connection->intf) {
106 intf_id = connection->intf->interface_id;
107 cport_id = connection->intf_cport_id;
110 snprintf(connection->name, sizeof(connection->name),
111 "%u/%u:%u", hd_cport_id, intf_id, cport_id);
115 * _gb_connection_create() - create a Greybus connection
116 * @hd: host device of the connection
117 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
118 * @intf: remote interface, or NULL for static connections
119 * @bundle: remote-interface bundle (may be NULL)
120 * @cport_id: remote-interface cport id, or 0 for static connections
121 * @handler: request handler (may be NULL)
122 * @flags: connection flags
124 * Create a Greybus connection, representing the bidirectional link
125 * between a CPort on a (local) Greybus host device and a CPort on
126 * another Greybus interface.
128 * A connection also maintains the state of operations sent over the
131 * Serialised against concurrent create and destroy using the
132 * gb_connection_mutex.
134 * Return: A pointer to the new connection if successful, or an ERR_PTR
137 static struct gb_connection *
138 _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
139 struct gb_interface *intf,
140 struct gb_bundle *bundle, int cport_id,
141 gb_request_handler_t handler,
144 struct gb_connection *connection;
147 mutex_lock(&gb_connection_mutex);
149 if (intf && gb_connection_intf_find(intf, cport_id)) {
150 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
155 ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
157 dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
162 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
165 goto err_hd_cport_release;
168 connection->hd_cport_id = hd_cport_id;
169 connection->intf_cport_id = cport_id;
171 connection->intf = intf;
172 connection->bundle = bundle;
173 connection->handler = handler;
174 connection->flags = flags;
175 if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
176 connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
177 connection->state = GB_CONNECTION_STATE_DISABLED;
179 atomic_set(&connection->op_cycle, 0);
180 mutex_init(&connection->mutex);
181 spin_lock_init(&connection->lock);
182 INIT_LIST_HEAD(&connection->operations);
184 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
185 dev_name(&hd->dev), hd_cport_id);
186 if (!connection->wq) {
188 goto err_free_connection;
191 kref_init(&connection->kref);
193 gb_connection_init_name(connection);
195 spin_lock_irq(&gb_connections_lock);
196 list_add(&connection->hd_links, &hd->connections);
199 list_add(&connection->bundle_links, &bundle->connections);
201 INIT_LIST_HEAD(&connection->bundle_links);
203 spin_unlock_irq(&gb_connections_lock);
205 mutex_unlock(&gb_connection_mutex);
211 err_hd_cport_release:
212 gb_hd_cport_release(hd, hd_cport_id);
214 mutex_unlock(&gb_connection_mutex);
219 struct gb_connection *
220 gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
221 gb_request_handler_t handler)
223 return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler,
227 struct gb_connection *
228 gb_connection_create_control(struct gb_interface *intf)
230 return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL, 0);
233 struct gb_connection *
234 gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
235 gb_request_handler_t handler)
237 struct gb_interface *intf = bundle->intf;
239 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
242 EXPORT_SYMBOL_GPL(gb_connection_create);
244 struct gb_connection *
245 gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
246 gb_request_handler_t handler,
249 struct gb_interface *intf = bundle->intf;
251 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
254 EXPORT_SYMBOL_GPL(gb_connection_create_flags);
256 struct gb_connection *
257 gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
260 struct gb_interface *intf = bundle->intf;
262 flags |= GB_CONNECTION_FLAG_OFFLOADED;
264 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
267 EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
269 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
271 struct gb_host_device *hd = connection->hd;
274 if (!hd->driver->cport_enable)
277 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
280 "failed to enable host cport: %d\n", ret);
287 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
289 struct gb_host_device *hd = connection->hd;
291 if (!hd->driver->cport_disable)
294 hd->driver->cport_disable(hd, connection->hd_cport_id);
298 gb_connection_hd_cport_features_enable(struct gb_connection *connection)
300 struct gb_host_device *hd = connection->hd;
303 if (!hd->driver->cport_features_enable)
306 ret = hd->driver->cport_features_enable(hd, connection->hd_cport_id);
308 dev_err(&hd->dev, "%s: failed to enable CPort features: %d\n",
309 connection->name, ret);
317 gb_connection_hd_cport_features_disable(struct gb_connection *connection)
319 struct gb_host_device *hd = connection->hd;
321 if (!hd->driver->cport_features_disable)
324 hd->driver->cport_features_disable(hd, connection->hd_cport_id);
328 * Request the SVC to create a connection from AP's cport to interface's
332 gb_connection_svc_connection_create(struct gb_connection *connection)
334 struct gb_host_device *hd = connection->hd;
335 struct gb_interface *intf;
339 if (gb_connection_is_static(connection))
340 return gb_connection_hd_cport_features_enable(connection);
342 intf = connection->intf;
345 * Enable either E2EFC or CSD, unless no flow control is requested.
347 cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
348 if (gb_connection_flow_control_disabled(connection)) {
349 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
350 } else if (gb_connection_e2efc_enabled(connection)) {
351 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
352 GB_SVC_CPORT_FLAG_E2EFC;
355 ret = gb_svc_connection_create(hd->svc,
357 connection->hd_cport_id,
359 connection->intf_cport_id,
362 dev_err(&connection->hd->dev,
363 "%s: failed to create svc connection: %d\n",
364 connection->name, ret);
368 ret = gb_connection_hd_cport_features_enable(connection);
370 gb_svc_connection_destroy(hd->svc, hd->svc->ap_intf_id,
371 connection->hd_cport_id,
373 connection->intf_cport_id);
381 gb_connection_svc_connection_destroy(struct gb_connection *connection)
383 gb_connection_hd_cport_features_disable(connection);
385 if (gb_connection_is_static(connection))
388 gb_svc_connection_destroy(connection->hd->svc,
389 connection->hd->svc->ap_intf_id,
390 connection->hd_cport_id,
391 connection->intf->interface_id,
392 connection->intf_cport_id);
395 /* Inform Interface about active CPorts */
396 static int gb_connection_control_connected(struct gb_connection *connection)
398 struct gb_control *control;
399 u16 cport_id = connection->intf_cport_id;
402 if (gb_connection_is_static(connection))
406 * HACK: Suppress connected request for the offloaded camera
407 * connection as it is currently not supported by firmware. Note that
408 * the corresponding non-fatal disconnected event is still sent.
410 if (gb_connection_is_offloaded(connection) &&
411 connection->flags & GB_CONNECTION_FLAG_CDSI1) {
415 control = connection->intf->control;
417 if (connection == control->connection)
420 ret = gb_control_connected_operation(control, cport_id);
422 dev_err(&connection->bundle->dev,
423 "failed to connect cport: %d\n", ret);
430 /* Inform Interface about inactive CPorts */
432 gb_connection_control_disconnected(struct gb_connection *connection)
434 struct gb_control *control;
435 u16 cport_id = connection->intf_cport_id;
438 if (gb_connection_is_static(connection))
441 control = connection->intf->control;
443 if (connection == control->connection)
446 ret = gb_control_disconnected_operation(control, cport_id);
448 dev_warn(&connection->bundle->dev,
449 "failed to disconnect cport: %d\n", ret);
454 * Cancel all active operations on a connection.
456 * Locking: Called with connection lock held and state set to DISABLED.
458 static void gb_connection_cancel_operations(struct gb_connection *connection,
460 __must_hold(&connection->lock)
462 struct gb_operation *operation;
464 while (!list_empty(&connection->operations)) {
465 operation = list_last_entry(&connection->operations,
466 struct gb_operation, links);
467 gb_operation_get(operation);
468 spin_unlock_irq(&connection->lock);
470 if (gb_operation_is_incoming(operation))
471 gb_operation_cancel_incoming(operation, errno);
473 gb_operation_cancel(operation, errno);
475 gb_operation_put(operation);
477 spin_lock_irq(&connection->lock);
482 * Cancel all active incoming operations on a connection.
484 * Locking: Called with connection lock held and state set to ENABLED_TX.
487 gb_connection_flush_incoming_operations(struct gb_connection *connection,
489 __must_hold(&connection->lock)
491 struct gb_operation *operation;
494 while (!list_empty(&connection->operations)) {
496 list_for_each_entry(operation, &connection->operations,
498 if (gb_operation_is_incoming(operation)) {
499 gb_operation_get(operation);
508 spin_unlock_irq(&connection->lock);
510 /* FIXME: flush, not cancel? */
511 gb_operation_cancel_incoming(operation, errno);
512 gb_operation_put(operation);
514 spin_lock_irq(&connection->lock);
519 * _gb_connection_enable() - enable a connection
520 * @connection: connection to enable
521 * @rx: whether to enable incoming requests
523 * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
524 * ENABLED_TX->ENABLED state transitions.
526 * Locking: Caller holds connection->mutex.
528 static int _gb_connection_enable(struct gb_connection *connection, bool rx)
532 /* Handle ENABLED_TX -> ENABLED transitions. */
533 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
534 if (!(connection->handler && rx))
537 spin_lock_irq(&connection->lock);
538 connection->state = GB_CONNECTION_STATE_ENABLED;
539 spin_unlock_irq(&connection->lock);
544 ret = gb_connection_hd_cport_enable(connection);
548 ret = gb_connection_svc_connection_create(connection);
550 goto err_hd_cport_disable;
552 spin_lock_irq(&connection->lock);
553 if (connection->handler && rx)
554 connection->state = GB_CONNECTION_STATE_ENABLED;
556 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
557 spin_unlock_irq(&connection->lock);
559 ret = gb_connection_control_connected(connection);
561 goto err_svc_destroy;
566 spin_lock_irq(&connection->lock);
567 connection->state = GB_CONNECTION_STATE_DISABLED;
568 gb_connection_cancel_operations(connection, -ESHUTDOWN);
569 spin_unlock_irq(&connection->lock);
571 gb_connection_svc_connection_destroy(connection);
572 err_hd_cport_disable:
573 gb_connection_hd_cport_disable(connection);
578 int gb_connection_enable(struct gb_connection *connection)
582 mutex_lock(&connection->mutex);
584 if (connection->state == GB_CONNECTION_STATE_ENABLED)
587 ret = _gb_connection_enable(connection, true);
589 mutex_unlock(&connection->mutex);
593 EXPORT_SYMBOL_GPL(gb_connection_enable);
595 int gb_connection_enable_tx(struct gb_connection *connection)
599 mutex_lock(&connection->mutex);
601 if (connection->state == GB_CONNECTION_STATE_ENABLED) {
606 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
609 ret = _gb_connection_enable(connection, false);
611 mutex_unlock(&connection->mutex);
615 EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
617 void gb_connection_disable_rx(struct gb_connection *connection)
619 mutex_lock(&connection->mutex);
621 spin_lock_irq(&connection->lock);
622 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
623 spin_unlock_irq(&connection->lock);
626 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
627 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
628 spin_unlock_irq(&connection->lock);
631 mutex_unlock(&connection->mutex);
633 EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
635 void gb_connection_disable(struct gb_connection *connection)
637 mutex_lock(&connection->mutex);
639 if (connection->state == GB_CONNECTION_STATE_DISABLED)
642 gb_connection_control_disconnected(connection);
644 spin_lock_irq(&connection->lock);
645 connection->state = GB_CONNECTION_STATE_DISABLED;
646 gb_connection_cancel_operations(connection, -ESHUTDOWN);
647 spin_unlock_irq(&connection->lock);
649 gb_connection_svc_connection_destroy(connection);
650 gb_connection_hd_cport_disable(connection);
653 mutex_unlock(&connection->mutex);
655 EXPORT_SYMBOL_GPL(gb_connection_disable);
657 /* Caller must have disabled the connection before destroying it. */
658 void gb_connection_destroy(struct gb_connection *connection)
663 mutex_lock(&gb_connection_mutex);
665 spin_lock_irq(&gb_connections_lock);
666 list_del(&connection->bundle_links);
667 list_del(&connection->hd_links);
668 spin_unlock_irq(&gb_connections_lock);
670 destroy_workqueue(connection->wq);
672 gb_hd_cport_release(connection->hd, connection->hd_cport_id);
673 connection->hd_cport_id = CPORT_ID_BAD;
675 mutex_unlock(&gb_connection_mutex);
677 gb_connection_put(connection);
679 EXPORT_SYMBOL_GPL(gb_connection_destroy);
681 void gb_connection_latency_tag_enable(struct gb_connection *connection)
683 struct gb_host_device *hd = connection->hd;
686 if (!hd->driver->latency_tag_enable)
689 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
691 dev_err(&connection->hd->dev,
692 "%s: failed to enable latency tag: %d\n",
693 connection->name, ret);
696 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
698 void gb_connection_latency_tag_disable(struct gb_connection *connection)
700 struct gb_host_device *hd = connection->hd;
703 if (!hd->driver->latency_tag_disable)
706 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
708 dev_err(&connection->hd->dev,
709 "%s: failed to disable latency tag: %d\n",
710 connection->name, ret);
713 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);