4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
13 #include "greybus_trace.h"
16 static void gb_connection_kref_release(struct kref *kref);
19 static DEFINE_SPINLOCK(gb_connections_lock);
20 static DEFINE_MUTEX(gb_connection_mutex);
23 /* Caller holds gb_connection_mutex. */
24 static bool gb_connection_cport_in_use(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);
42 trace_gb_connection_get(connection);
45 static void gb_connection_put(struct gb_connection *connection)
47 trace_gb_connection_put(connection);
49 kref_put(&connection->kref, gb_connection_kref_release);
53 * Returns a reference-counted pointer to the connection if found.
55 static struct gb_connection *
56 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
58 struct gb_connection *connection;
61 spin_lock_irqsave(&gb_connections_lock, flags);
62 list_for_each_entry(connection, &hd->connections, hd_links)
63 if (connection->hd_cport_id == cport_id) {
64 gb_connection_get(connection);
69 spin_unlock_irqrestore(&gb_connections_lock, flags);
75 * Callback from the host driver to let us know that data has been
76 * received on the bundle.
78 void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
79 u8 *data, size_t length)
81 struct gb_connection *connection;
85 connection = gb_connection_hd_find(hd, cport_id);
88 "nonexistent connection (%zu bytes dropped)\n", length);
91 gb_connection_recv(connection, data, length);
92 gb_connection_put(connection);
94 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
96 static void gb_connection_kref_release(struct kref *kref)
98 struct gb_connection *connection;
100 connection = container_of(kref, struct gb_connection, kref);
102 trace_gb_connection_release(connection);
107 static void gb_connection_init_name(struct gb_connection *connection)
109 u16 hd_cport_id = connection->hd_cport_id;
113 if (connection->intf) {
114 intf_id = connection->intf->interface_id;
115 cport_id = connection->intf_cport_id;
118 snprintf(connection->name, sizeof(connection->name),
119 "%u/%u:%u", hd_cport_id, intf_id, cport_id);
123 * _gb_connection_create() - create a Greybus connection
124 * @hd: host device of the connection
125 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
126 * @intf: remote interface, or NULL for static connections
127 * @bundle: remote-interface bundle (may be NULL)
128 * @cport_id: remote-interface cport id, or 0 for static connections
129 * @handler: request handler (may be NULL)
130 * @flags: connection flags
132 * Create a Greybus connection, representing the bidirectional link
133 * between a CPort on a (local) Greybus host device and a CPort on
134 * another Greybus interface.
136 * A connection also maintains the state of operations sent over the
139 * Serialised against concurrent create and destroy using the
140 * gb_connection_mutex.
142 * Return: A pointer to the new connection if successful, or an ERR_PTR
145 static struct gb_connection *
146 _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
147 struct gb_interface *intf,
148 struct gb_bundle *bundle, int cport_id,
149 gb_request_handler_t handler,
152 struct gb_connection *connection;
153 unsigned long irqflags;
156 mutex_lock(&gb_connection_mutex);
158 if (intf && gb_connection_cport_in_use(intf, cport_id)) {
159 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
164 ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
166 dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
171 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
174 goto err_hd_cport_release;
177 connection->hd_cport_id = hd_cport_id;
178 connection->intf_cport_id = cport_id;
180 connection->intf = intf;
181 connection->bundle = bundle;
182 connection->handler = handler;
183 connection->flags = flags;
184 if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
185 connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
186 connection->state = GB_CONNECTION_STATE_DISABLED;
188 atomic_set(&connection->op_cycle, 0);
189 mutex_init(&connection->mutex);
190 spin_lock_init(&connection->lock);
191 INIT_LIST_HEAD(&connection->operations);
193 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
194 dev_name(&hd->dev), hd_cport_id);
195 if (!connection->wq) {
197 goto err_free_connection;
200 kref_init(&connection->kref);
202 gb_connection_init_name(connection);
204 spin_lock_irqsave(&gb_connections_lock, irqflags);
205 list_add(&connection->hd_links, &hd->connections);
208 list_add(&connection->bundle_links, &bundle->connections);
210 INIT_LIST_HEAD(&connection->bundle_links);
212 spin_unlock_irqrestore(&gb_connections_lock, irqflags);
214 mutex_unlock(&gb_connection_mutex);
216 trace_gb_connection_create(connection);
222 err_hd_cport_release:
223 gb_hd_cport_release(hd, hd_cport_id);
225 mutex_unlock(&gb_connection_mutex);
230 struct gb_connection *
231 gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
232 gb_request_handler_t handler)
234 return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler,
235 GB_CONNECTION_FLAG_HIGH_PRIO);
238 struct gb_connection *
239 gb_connection_create_control(struct gb_interface *intf)
241 return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL,
242 GB_CONNECTION_FLAG_CONTROL |
243 GB_CONNECTION_FLAG_HIGH_PRIO);
246 struct gb_connection *
247 gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
248 gb_request_handler_t handler)
250 struct gb_interface *intf = bundle->intf;
252 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
255 EXPORT_SYMBOL_GPL(gb_connection_create);
257 struct gb_connection *
258 gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
259 gb_request_handler_t handler,
262 struct gb_interface *intf = bundle->intf;
264 if (WARN_ON_ONCE(flags & GB_CONNECTION_FLAG_CORE_MASK))
265 flags &= ~GB_CONNECTION_FLAG_CORE_MASK;
267 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
270 EXPORT_SYMBOL_GPL(gb_connection_create_flags);
272 struct gb_connection *
273 gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
276 flags |= GB_CONNECTION_FLAG_OFFLOADED;
278 return gb_connection_create_flags(bundle, cport_id, NULL, flags);
280 EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
282 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
284 struct gb_host_device *hd = connection->hd;
287 if (!hd->driver->cport_enable)
290 ret = hd->driver->cport_enable(hd, connection->hd_cport_id,
293 dev_err(&hd->dev, "%s: failed to enable host cport: %d\n",
294 connection->name, ret);
301 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
303 struct gb_host_device *hd = connection->hd;
306 if (!hd->driver->cport_disable)
309 ret = hd->driver->cport_disable(hd, connection->hd_cport_id);
311 dev_err(&hd->dev, "%s: failed to disable host cport: %d\n",
312 connection->name, ret);
316 static int gb_connection_hd_cport_flush(struct gb_connection *connection)
318 struct gb_host_device *hd = connection->hd;
321 if (!hd->driver->cport_flush)
324 ret = hd->driver->cport_flush(hd, connection->hd_cport_id);
326 dev_err(&hd->dev, "%s: failed to flush host cport: %d\n",
327 connection->name, ret);
335 gb_connection_hd_cport_features_enable(struct gb_connection *connection)
337 struct gb_host_device *hd = connection->hd;
340 if (!hd->driver->cport_features_enable)
343 ret = hd->driver->cport_features_enable(hd, connection->hd_cport_id);
345 dev_err(&hd->dev, "%s: failed to enable CPort features: %d\n",
346 connection->name, ret);
354 gb_connection_hd_cport_features_disable(struct gb_connection *connection)
356 struct gb_host_device *hd = connection->hd;
358 if (!hd->driver->cport_features_disable)
361 hd->driver->cport_features_disable(hd, connection->hd_cport_id);
365 * Request the SVC to create a connection from AP's cport to interface's
369 gb_connection_svc_connection_create(struct gb_connection *connection)
371 struct gb_host_device *hd = connection->hd;
372 struct gb_interface *intf;
376 if (gb_connection_is_static(connection))
379 intf = connection->intf;
382 * Enable either E2EFC or CSD, unless no flow control is requested.
384 cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
385 if (gb_connection_flow_control_disabled(connection)) {
386 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
387 } else if (gb_connection_e2efc_enabled(connection)) {
388 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
389 GB_SVC_CPORT_FLAG_E2EFC;
392 ret = gb_svc_connection_create(hd->svc,
394 connection->hd_cport_id,
396 connection->intf_cport_id,
399 dev_err(&connection->hd->dev,
400 "%s: failed to create svc connection: %d\n",
401 connection->name, ret);
409 gb_connection_svc_connection_destroy(struct gb_connection *connection)
411 if (gb_connection_is_static(connection))
414 gb_svc_connection_destroy(connection->hd->svc,
415 connection->hd->svc->ap_intf_id,
416 connection->hd_cport_id,
417 connection->intf->interface_id,
418 connection->intf_cport_id);
422 gb_connection_svc_connection_quiescing(struct gb_connection *connection)
424 struct gb_host_device *hd = connection->hd;
426 if (gb_connection_is_static(connection))
429 gb_svc_connection_quiescing(hd->svc,
431 connection->hd_cport_id,
432 connection->intf->interface_id,
433 connection->intf_cport_id);
436 /* Inform Interface about active CPorts */
437 static int gb_connection_control_connected(struct gb_connection *connection)
439 struct gb_control *control;
440 u16 cport_id = connection->intf_cport_id;
443 if (gb_connection_is_static(connection))
447 * HACK: Suppress connected request for the offloaded camera
448 * connection as it is currently not supported by firmware. Note that
449 * the corresponding non-fatal disconnected event is still sent.
451 if (gb_connection_is_offloaded(connection) &&
452 connection->flags & GB_CONNECTION_FLAG_CDSI1) {
456 if (gb_connection_is_control(connection))
459 control = connection->intf->control;
461 ret = gb_control_connected_operation(control, cport_id);
463 dev_err(&connection->bundle->dev,
464 "failed to connect cport: %d\n", ret);
472 gb_connection_control_disconnecting(struct gb_connection *connection)
474 struct gb_control *control;
475 u16 cport_id = connection->intf_cport_id;
478 if (gb_connection_is_static(connection))
481 control = connection->intf->control;
483 ret = gb_control_disconnecting_operation(control, cport_id);
485 dev_err(&connection->hd->dev,
486 "%s: failed to send disconnecting: %d\n",
487 connection->name, ret);
492 gb_connection_control_disconnected(struct gb_connection *connection)
494 struct gb_control *control;
495 u16 cport_id = connection->intf_cport_id;
498 if (gb_connection_is_static(connection))
501 control = connection->intf->control;
503 if (gb_connection_is_control(connection)) {
504 if (connection->mode_switch) {
505 ret = gb_control_mode_switch_operation(control);
508 * Allow mode switch to time out waiting for
518 ret = gb_control_disconnected_operation(control, cport_id);
520 dev_warn(&connection->bundle->dev,
521 "failed to disconnect cport: %d\n", ret);
525 static int gb_connection_ping_operation(struct gb_connection *connection)
527 struct gb_operation *operation;
530 operation = gb_operation_create_core(connection,
531 GB_REQUEST_TYPE_PING,
537 ret = gb_operation_request_send_sync(operation);
539 gb_operation_put(operation);
544 static int gb_connection_ping(struct gb_connection *connection)
546 struct gb_host_device *hd = connection->hd;
549 if (gb_connection_is_static(connection))
552 if (gb_connection_is_offloaded(connection)) {
553 if (!hd->driver->cport_ping)
556 ret = hd->driver->cport_ping(hd, connection->intf_cport_id);
558 ret = gb_connection_ping_operation(connection);
562 dev_err(&hd->dev, "%s: failed to send ping: %d\n",
563 connection->name, ret);
571 * Cancel all active operations on a connection.
573 * Locking: Called with connection lock held and state set to DISABLED or
576 static void gb_connection_cancel_operations(struct gb_connection *connection,
577 int errno, unsigned long *flags)
578 __must_hold(&connection->lock)
580 struct gb_operation *operation;
582 while (!list_empty(&connection->operations)) {
583 operation = list_last_entry(&connection->operations,
584 struct gb_operation, links);
585 gb_operation_get(operation);
586 spin_unlock_irqrestore(&connection->lock, *flags);
588 if (gb_operation_is_incoming(operation))
589 gb_operation_cancel_incoming(operation, errno);
591 gb_operation_cancel(operation, errno);
593 gb_operation_put(operation);
595 spin_lock_irqsave(&connection->lock, *flags);
600 * Cancel all active incoming operations on a connection.
602 * Locking: Called with connection lock held and state set to ENABLED_TX.
605 gb_connection_flush_incoming_operations(struct gb_connection *connection,
606 int errno, unsigned long *flags)
607 __must_hold(&connection->lock)
609 struct gb_operation *operation;
612 while (!list_empty(&connection->operations)) {
614 list_for_each_entry(operation, &connection->operations,
616 if (gb_operation_is_incoming(operation)) {
617 gb_operation_get(operation);
626 spin_unlock_irqrestore(&connection->lock, *flags);
628 /* FIXME: flush, not cancel? */
629 gb_operation_cancel_incoming(operation, errno);
630 gb_operation_put(operation);
632 spin_lock_irqsave(&connection->lock, *flags);
637 * _gb_connection_enable() - enable a connection
638 * @connection: connection to enable
639 * @rx: whether to enable incoming requests
641 * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
642 * ENABLED_TX->ENABLED state transitions.
644 * Locking: Caller holds connection->mutex.
646 static int _gb_connection_enable(struct gb_connection *connection, bool rx)
651 /* Handle ENABLED_TX -> ENABLED transitions. */
652 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
653 if (!(connection->handler && rx))
656 spin_lock_irqsave(&connection->lock, flags);
657 connection->state = GB_CONNECTION_STATE_ENABLED;
658 spin_unlock_irqrestore(&connection->lock, flags);
663 ret = gb_connection_hd_cport_enable(connection);
667 ret = gb_connection_svc_connection_create(connection);
669 goto err_hd_cport_disable;
671 ret = gb_connection_hd_cport_features_enable(connection);
673 goto err_svc_connection_destroy;
675 spin_lock_irqsave(&connection->lock, flags);
676 if (connection->handler && rx)
677 connection->state = GB_CONNECTION_STATE_ENABLED;
679 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
680 spin_unlock_irqrestore(&connection->lock, flags);
682 ret = gb_connection_control_connected(connection);
684 goto err_control_disconnecting;
688 err_control_disconnecting:
689 gb_connection_control_disconnecting(connection);
691 spin_lock_irqsave(&connection->lock, flags);
692 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
693 gb_connection_cancel_operations(connection, -ESHUTDOWN, &flags);
694 spin_unlock_irqrestore(&connection->lock, flags);
696 /* Transmit queue should already be empty. */
697 gb_connection_hd_cport_flush(connection);
699 gb_connection_ping(connection);
700 gb_connection_hd_cport_features_disable(connection);
701 gb_connection_svc_connection_quiescing(connection);
702 gb_connection_ping(connection);
703 gb_connection_control_disconnected(connection);
704 connection->state = GB_CONNECTION_STATE_DISABLED;
705 err_svc_connection_destroy:
706 gb_connection_svc_connection_destroy(connection);
707 err_hd_cport_disable:
708 gb_connection_hd_cport_disable(connection);
713 int gb_connection_enable(struct gb_connection *connection)
717 mutex_lock(&connection->mutex);
719 if (connection->state == GB_CONNECTION_STATE_ENABLED)
722 ret = _gb_connection_enable(connection, true);
724 trace_gb_connection_enable(connection);
727 mutex_unlock(&connection->mutex);
731 EXPORT_SYMBOL_GPL(gb_connection_enable);
733 int gb_connection_enable_tx(struct gb_connection *connection)
737 mutex_lock(&connection->mutex);
739 if (connection->state == GB_CONNECTION_STATE_ENABLED) {
744 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
747 ret = _gb_connection_enable(connection, false);
749 trace_gb_connection_enable(connection);
752 mutex_unlock(&connection->mutex);
756 EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
758 void gb_connection_disable_rx(struct gb_connection *connection)
762 mutex_lock(&connection->mutex);
764 spin_lock_irqsave(&connection->lock, flags);
765 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
766 spin_unlock_irqrestore(&connection->lock, flags);
769 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
770 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN, &flags);
771 spin_unlock_irqrestore(&connection->lock, flags);
773 trace_gb_connection_disable(connection);
776 mutex_unlock(&connection->mutex);
778 EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
780 void gb_connection_mode_switch_prepare(struct gb_connection *connection)
782 connection->mode_switch = true;
785 void gb_connection_mode_switch_complete(struct gb_connection *connection)
787 gb_connection_svc_connection_destroy(connection);
788 gb_connection_hd_cport_disable(connection);
789 connection->mode_switch = false;
792 void gb_connection_disable(struct gb_connection *connection)
796 mutex_lock(&connection->mutex);
798 if (connection->state == GB_CONNECTION_STATE_DISABLED)
801 trace_gb_connection_disable(connection);
803 gb_connection_control_disconnecting(connection);
805 spin_lock_irqsave(&connection->lock, flags);
806 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
807 gb_connection_cancel_operations(connection, -ESHUTDOWN, &flags);
808 spin_unlock_irqrestore(&connection->lock, flags);
810 gb_connection_hd_cport_flush(connection);
812 gb_connection_ping(connection);
813 gb_connection_hd_cport_features_disable(connection);
814 gb_connection_svc_connection_quiescing(connection);
815 gb_connection_ping(connection);
817 gb_connection_control_disconnected(connection);
819 connection->state = GB_CONNECTION_STATE_DISABLED;
821 /* control-connection tear down is deferred when mode switching */
822 if (!connection->mode_switch) {
823 gb_connection_svc_connection_destroy(connection);
824 gb_connection_hd_cport_disable(connection);
828 mutex_unlock(&connection->mutex);
830 EXPORT_SYMBOL_GPL(gb_connection_disable);
832 /* Disable a connection without communicating with the remote end. */
833 void gb_connection_disable_forced(struct gb_connection *connection)
837 mutex_lock(&connection->mutex);
839 if (connection->state == GB_CONNECTION_STATE_DISABLED)
842 trace_gb_connection_disable(connection);
844 spin_lock_irqsave(&connection->lock, flags);
845 connection->state = GB_CONNECTION_STATE_DISABLED;
846 gb_connection_cancel_operations(connection, -ESHUTDOWN, &flags);
847 spin_unlock_irqrestore(&connection->lock, flags);
849 gb_connection_hd_cport_flush(connection);
850 gb_connection_hd_cport_features_disable(connection);
851 gb_connection_svc_connection_destroy(connection);
852 gb_connection_hd_cport_disable(connection);
855 mutex_unlock(&connection->mutex);
857 EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
859 /* Caller must have disabled the connection before destroying it. */
860 void gb_connection_destroy(struct gb_connection *connection)
867 if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
868 gb_connection_disable(connection);
870 mutex_lock(&gb_connection_mutex);
872 spin_lock_irqsave(&gb_connections_lock, flags);
873 list_del(&connection->bundle_links);
874 list_del(&connection->hd_links);
875 spin_unlock_irqrestore(&gb_connections_lock, flags);
877 destroy_workqueue(connection->wq);
879 gb_hd_cport_release(connection->hd, connection->hd_cport_id);
880 connection->hd_cport_id = CPORT_ID_BAD;
882 mutex_unlock(&gb_connection_mutex);
884 gb_connection_put(connection);
886 EXPORT_SYMBOL_GPL(gb_connection_destroy);
888 void gb_connection_latency_tag_enable(struct gb_connection *connection)
890 struct gb_host_device *hd = connection->hd;
893 if (!hd->driver->latency_tag_enable)
896 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
898 dev_err(&connection->hd->dev,
899 "%s: failed to enable latency tag: %d\n",
900 connection->name, ret);
903 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
905 void gb_connection_latency_tag_disable(struct gb_connection *connection)
907 struct gb_host_device *hd = connection->hd;
910 if (!hd->driver->latency_tag_disable)
913 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
915 dev_err(&connection->hd->dev,
916 "%s: failed to disable latency tag: %d\n",
917 connection->name, ret);
920 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);