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 #define GB_CONNECTION_CPORT_QUIESCE_TIMEOUT 1000
19 static void gb_connection_kref_release(struct kref *kref);
22 static DEFINE_SPINLOCK(gb_connections_lock);
23 static DEFINE_MUTEX(gb_connection_mutex);
26 /* Caller holds gb_connection_mutex. */
27 static bool gb_connection_cport_in_use(struct gb_interface *intf, u16 cport_id)
29 struct gb_host_device *hd = intf->hd;
30 struct gb_connection *connection;
32 list_for_each_entry(connection, &hd->connections, hd_links) {
33 if (connection->intf == intf &&
34 connection->intf_cport_id == cport_id)
41 static void gb_connection_get(struct gb_connection *connection)
43 kref_get(&connection->kref);
45 trace_gb_connection_get(connection);
48 static void gb_connection_put(struct gb_connection *connection)
50 trace_gb_connection_put(connection);
52 kref_put(&connection->kref, gb_connection_kref_release);
56 * Returns a reference-counted pointer to the connection if found.
58 static struct gb_connection *
59 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
61 struct gb_connection *connection;
64 spin_lock_irqsave(&gb_connections_lock, flags);
65 list_for_each_entry(connection, &hd->connections, hd_links)
66 if (connection->hd_cport_id == cport_id) {
67 gb_connection_get(connection);
72 spin_unlock_irqrestore(&gb_connections_lock, flags);
78 * Callback from the host driver to let us know that data has been
79 * received on the bundle.
81 void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
82 u8 *data, size_t length)
84 struct gb_connection *connection;
88 connection = gb_connection_hd_find(hd, cport_id);
91 "nonexistent connection (%zu bytes dropped)\n", length);
94 gb_connection_recv(connection, data, length);
95 gb_connection_put(connection);
97 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
99 static void gb_connection_kref_release(struct kref *kref)
101 struct gb_connection *connection;
103 connection = container_of(kref, struct gb_connection, kref);
105 trace_gb_connection_release(connection);
110 static void gb_connection_init_name(struct gb_connection *connection)
112 u16 hd_cport_id = connection->hd_cport_id;
116 if (connection->intf) {
117 intf_id = connection->intf->interface_id;
118 cport_id = connection->intf_cport_id;
121 snprintf(connection->name, sizeof(connection->name),
122 "%u/%u:%u", hd_cport_id, intf_id, cport_id);
126 * _gb_connection_create() - create a Greybus connection
127 * @hd: host device of the connection
128 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
129 * @intf: remote interface, or NULL for static connections
130 * @bundle: remote-interface bundle (may be NULL)
131 * @cport_id: remote-interface cport id, or 0 for static connections
132 * @handler: request handler (may be NULL)
133 * @flags: connection flags
135 * Create a Greybus connection, representing the bidirectional link
136 * between a CPort on a (local) Greybus host device and a CPort on
137 * another Greybus interface.
139 * A connection also maintains the state of operations sent over the
142 * Serialised against concurrent create and destroy using the
143 * gb_connection_mutex.
145 * Return: A pointer to the new connection if successful, or an ERR_PTR
148 static struct gb_connection *
149 _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
150 struct gb_interface *intf,
151 struct gb_bundle *bundle, int cport_id,
152 gb_request_handler_t handler,
155 struct gb_connection *connection;
158 mutex_lock(&gb_connection_mutex);
160 if (intf && gb_connection_cport_in_use(intf, cport_id)) {
161 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
166 ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
168 dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
173 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
176 goto err_hd_cport_release;
179 connection->hd_cport_id = hd_cport_id;
180 connection->intf_cport_id = cport_id;
182 connection->intf = intf;
183 connection->bundle = bundle;
184 connection->handler = handler;
185 connection->flags = flags;
186 if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
187 connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
188 connection->state = GB_CONNECTION_STATE_DISABLED;
190 atomic_set(&connection->op_cycle, 0);
191 mutex_init(&connection->mutex);
192 spin_lock_init(&connection->lock);
193 INIT_LIST_HEAD(&connection->operations);
195 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
196 dev_name(&hd->dev), hd_cport_id);
197 if (!connection->wq) {
199 goto err_free_connection;
202 kref_init(&connection->kref);
204 gb_connection_init_name(connection);
206 spin_lock_irq(&gb_connections_lock);
207 list_add(&connection->hd_links, &hd->connections);
210 list_add(&connection->bundle_links, &bundle->connections);
212 INIT_LIST_HEAD(&connection->bundle_links);
214 spin_unlock_irq(&gb_connections_lock);
216 mutex_unlock(&gb_connection_mutex);
218 trace_gb_connection_create(connection);
224 err_hd_cport_release:
225 gb_hd_cport_release(hd, hd_cport_id);
227 mutex_unlock(&gb_connection_mutex);
232 struct gb_connection *
233 gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
234 gb_request_handler_t handler)
236 return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler,
237 GB_CONNECTION_FLAG_HIGH_PRIO);
240 struct gb_connection *
241 gb_connection_create_control(struct gb_interface *intf)
243 return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL,
244 GB_CONNECTION_FLAG_CONTROL |
245 GB_CONNECTION_FLAG_HIGH_PRIO);
248 struct gb_connection *
249 gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
250 gb_request_handler_t handler)
252 struct gb_interface *intf = bundle->intf;
254 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
257 EXPORT_SYMBOL_GPL(gb_connection_create);
259 struct gb_connection *
260 gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
261 gb_request_handler_t handler,
264 struct gb_interface *intf = bundle->intf;
266 if (WARN_ON_ONCE(flags & GB_CONNECTION_FLAG_CORE_MASK))
267 flags &= ~GB_CONNECTION_FLAG_CORE_MASK;
269 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
272 EXPORT_SYMBOL_GPL(gb_connection_create_flags);
274 struct gb_connection *
275 gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
278 flags |= GB_CONNECTION_FLAG_OFFLOADED;
280 return gb_connection_create_flags(bundle, cport_id, NULL, flags);
282 EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
284 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
286 struct gb_host_device *hd = connection->hd;
289 if (!hd->driver->cport_enable)
292 ret = hd->driver->cport_enable(hd, connection->hd_cport_id,
295 dev_err(&hd->dev, "%s: failed to enable host cport: %d\n",
296 connection->name, ret);
303 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
305 struct gb_host_device *hd = connection->hd;
308 if (!hd->driver->cport_disable)
311 ret = hd->driver->cport_disable(hd, connection->hd_cport_id);
313 dev_err(&hd->dev, "%s: failed to disable host cport: %d\n",
314 connection->name, ret);
318 static int gb_connection_hd_cport_connected(struct gb_connection *connection)
320 struct gb_host_device *hd = connection->hd;
323 if (!hd->driver->cport_connected)
326 ret = hd->driver->cport_connected(hd, connection->hd_cport_id);
328 dev_err(&hd->dev, "%s: failed to set connected state: %d\n",
329 connection->name, ret);
336 static int gb_connection_hd_cport_flush(struct gb_connection *connection)
338 struct gb_host_device *hd = connection->hd;
341 if (!hd->driver->cport_flush)
344 ret = hd->driver->cport_flush(hd, connection->hd_cport_id);
346 dev_err(&hd->dev, "%s: failed to flush host cport: %d\n",
347 connection->name, ret);
354 static int gb_connection_hd_cport_quiesce(struct gb_connection *connection)
356 struct gb_host_device *hd = connection->hd;
360 peer_space = sizeof(struct gb_operation_msg_hdr) +
361 sizeof(struct gb_cport_shutdown_request);
363 if (connection->mode_switch)
364 peer_space += sizeof(struct gb_operation_msg_hdr);
366 ret = hd->driver->cport_quiesce(hd, connection->hd_cport_id,
368 GB_CONNECTION_CPORT_QUIESCE_TIMEOUT);
370 dev_err(&hd->dev, "%s: failed to quiesce host cport: %d\n",
371 connection->name, ret);
378 static int gb_connection_hd_cport_clear(struct gb_connection *connection)
380 struct gb_host_device *hd = connection->hd;
383 ret = hd->driver->cport_clear(hd, connection->hd_cport_id);
385 dev_err(&hd->dev, "%s: failed to clear host cport: %d\n",
386 connection->name, ret);
394 * Request the SVC to create a connection from AP's cport to interface's
398 gb_connection_svc_connection_create(struct gb_connection *connection)
400 struct gb_host_device *hd = connection->hd;
401 struct gb_interface *intf;
405 if (gb_connection_is_static(connection))
408 intf = connection->intf;
411 * Enable either E2EFC or CSD, unless no flow control is requested.
413 cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
414 if (gb_connection_flow_control_disabled(connection)) {
415 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
416 } else if (gb_connection_e2efc_enabled(connection)) {
417 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
418 GB_SVC_CPORT_FLAG_E2EFC;
421 ret = gb_svc_connection_create(hd->svc,
423 connection->hd_cport_id,
425 connection->intf_cport_id,
428 dev_err(&connection->hd->dev,
429 "%s: failed to create svc connection: %d\n",
430 connection->name, ret);
438 gb_connection_svc_connection_destroy(struct gb_connection *connection)
440 if (gb_connection_is_static(connection))
443 gb_svc_connection_destroy(connection->hd->svc,
444 connection->hd->svc->ap_intf_id,
445 connection->hd_cport_id,
446 connection->intf->interface_id,
447 connection->intf_cport_id);
450 /* Inform Interface about active CPorts */
451 static int gb_connection_control_connected(struct gb_connection *connection)
453 struct gb_control *control;
454 u16 cport_id = connection->intf_cport_id;
457 if (gb_connection_is_static(connection))
460 if (gb_connection_is_control(connection))
463 control = connection->intf->control;
465 ret = gb_control_connected_operation(control, cport_id);
467 dev_err(&connection->bundle->dev,
468 "failed to connect cport: %d\n", ret);
476 gb_connection_control_disconnecting(struct gb_connection *connection)
478 struct gb_control *control;
479 u16 cport_id = connection->intf_cport_id;
482 if (gb_connection_is_static(connection))
485 control = connection->intf->control;
487 ret = gb_control_disconnecting_operation(control, cport_id);
489 dev_err(&connection->hd->dev,
490 "%s: failed to send disconnecting: %d\n",
491 connection->name, ret);
496 gb_connection_control_disconnected(struct gb_connection *connection)
498 struct gb_control *control;
499 u16 cport_id = connection->intf_cport_id;
502 if (gb_connection_is_static(connection))
505 control = connection->intf->control;
507 if (gb_connection_is_control(connection)) {
508 if (connection->mode_switch) {
509 ret = gb_control_mode_switch_operation(control);
512 * Allow mode switch to time out waiting for
522 ret = gb_control_disconnected_operation(control, cport_id);
524 dev_warn(&connection->bundle->dev,
525 "failed to disconnect cport: %d\n", ret);
529 static int gb_connection_shutdown_operation(struct gb_connection *connection,
532 struct gb_cport_shutdown_request *req;
533 struct gb_operation *operation;
536 operation = gb_operation_create_core(connection,
537 GB_REQUEST_TYPE_CPORT_SHUTDOWN,
543 req = operation->request->payload;
546 ret = gb_operation_request_send_sync(operation);
548 gb_operation_put(operation);
553 static int gb_connection_cport_shutdown(struct gb_connection *connection,
556 struct gb_host_device *hd = connection->hd;
557 const struct gb_hd_driver *drv = hd->driver;
560 if (gb_connection_is_static(connection))
563 if (gb_connection_is_offloaded(connection)) {
564 if (!drv->cport_shutdown)
567 ret = drv->cport_shutdown(hd, connection->hd_cport_id, phase,
568 GB_OPERATION_TIMEOUT_DEFAULT);
570 ret = gb_connection_shutdown_operation(connection, phase);
574 dev_err(&hd->dev, "%s: failed to send cport shutdown (phase %d): %d\n",
575 connection->name, phase, ret);
583 gb_connection_cport_shutdown_phase_1(struct gb_connection *connection)
585 return gb_connection_cport_shutdown(connection, 1);
589 gb_connection_cport_shutdown_phase_2(struct gb_connection *connection)
591 return gb_connection_cport_shutdown(connection, 2);
595 * Cancel all active operations on a connection.
597 * Locking: Called with connection lock held and state set to DISABLED or
600 static void gb_connection_cancel_operations(struct gb_connection *connection,
602 __must_hold(&connection->lock)
604 struct gb_operation *operation;
606 while (!list_empty(&connection->operations)) {
607 operation = list_last_entry(&connection->operations,
608 struct gb_operation, links);
609 gb_operation_get(operation);
610 spin_unlock_irq(&connection->lock);
612 if (gb_operation_is_incoming(operation))
613 gb_operation_cancel_incoming(operation, errno);
615 gb_operation_cancel(operation, errno);
617 gb_operation_put(operation);
619 spin_lock_irq(&connection->lock);
624 * Cancel all active incoming operations on a connection.
626 * Locking: Called with connection lock held and state set to ENABLED_TX.
629 gb_connection_flush_incoming_operations(struct gb_connection *connection,
631 __must_hold(&connection->lock)
633 struct gb_operation *operation;
636 while (!list_empty(&connection->operations)) {
638 list_for_each_entry(operation, &connection->operations,
640 if (gb_operation_is_incoming(operation)) {
641 gb_operation_get(operation);
650 spin_unlock_irq(&connection->lock);
652 /* FIXME: flush, not cancel? */
653 gb_operation_cancel_incoming(operation, errno);
654 gb_operation_put(operation);
656 spin_lock_irq(&connection->lock);
661 * _gb_connection_enable() - enable a connection
662 * @connection: connection to enable
663 * @rx: whether to enable incoming requests
665 * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
666 * ENABLED_TX->ENABLED state transitions.
668 * Locking: Caller holds connection->mutex.
670 static int _gb_connection_enable(struct gb_connection *connection, bool rx)
674 /* Handle ENABLED_TX -> ENABLED transitions. */
675 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
676 if (!(connection->handler && rx))
679 spin_lock_irq(&connection->lock);
680 connection->state = GB_CONNECTION_STATE_ENABLED;
681 spin_unlock_irq(&connection->lock);
686 ret = gb_connection_hd_cport_enable(connection);
690 ret = gb_connection_svc_connection_create(connection);
692 goto err_hd_cport_clear;
694 ret = gb_connection_hd_cport_connected(connection);
696 goto err_svc_connection_destroy;
698 spin_lock_irq(&connection->lock);
699 if (connection->handler && rx)
700 connection->state = GB_CONNECTION_STATE_ENABLED;
702 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
703 spin_unlock_irq(&connection->lock);
705 ret = gb_connection_control_connected(connection);
707 goto err_control_disconnecting;
711 err_control_disconnecting:
712 spin_lock_irq(&connection->lock);
713 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
714 gb_connection_cancel_operations(connection, -ESHUTDOWN);
715 spin_unlock_irq(&connection->lock);
717 /* Transmit queue should already be empty. */
718 gb_connection_hd_cport_flush(connection);
720 gb_connection_control_disconnecting(connection);
721 gb_connection_cport_shutdown_phase_1(connection);
722 gb_connection_hd_cport_quiesce(connection);
723 gb_connection_cport_shutdown_phase_2(connection);
724 gb_connection_control_disconnected(connection);
725 connection->state = GB_CONNECTION_STATE_DISABLED;
726 err_svc_connection_destroy:
727 gb_connection_svc_connection_destroy(connection);
729 gb_connection_hd_cport_clear(connection);
731 gb_connection_hd_cport_disable(connection);
736 int gb_connection_enable(struct gb_connection *connection)
740 mutex_lock(&connection->mutex);
742 if (connection->state == GB_CONNECTION_STATE_ENABLED)
745 ret = _gb_connection_enable(connection, true);
747 trace_gb_connection_enable(connection);
750 mutex_unlock(&connection->mutex);
754 EXPORT_SYMBOL_GPL(gb_connection_enable);
756 int gb_connection_enable_tx(struct gb_connection *connection)
760 mutex_lock(&connection->mutex);
762 if (connection->state == GB_CONNECTION_STATE_ENABLED) {
767 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
770 ret = _gb_connection_enable(connection, false);
772 trace_gb_connection_enable(connection);
775 mutex_unlock(&connection->mutex);
779 EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
781 void gb_connection_disable_rx(struct gb_connection *connection)
783 mutex_lock(&connection->mutex);
785 spin_lock_irq(&connection->lock);
786 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
787 spin_unlock_irq(&connection->lock);
790 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
791 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
792 spin_unlock_irq(&connection->lock);
794 trace_gb_connection_disable(connection);
797 mutex_unlock(&connection->mutex);
799 EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
801 void gb_connection_mode_switch_prepare(struct gb_connection *connection)
803 connection->mode_switch = true;
806 void gb_connection_mode_switch_complete(struct gb_connection *connection)
808 gb_connection_svc_connection_destroy(connection);
809 gb_connection_hd_cport_clear(connection);
811 gb_connection_hd_cport_disable(connection);
813 connection->mode_switch = false;
816 void gb_connection_disable(struct gb_connection *connection)
818 mutex_lock(&connection->mutex);
820 if (connection->state == GB_CONNECTION_STATE_DISABLED)
823 trace_gb_connection_disable(connection);
825 spin_lock_irq(&connection->lock);
826 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
827 gb_connection_cancel_operations(connection, -ESHUTDOWN);
828 spin_unlock_irq(&connection->lock);
830 gb_connection_hd_cport_flush(connection);
832 gb_connection_control_disconnecting(connection);
833 gb_connection_cport_shutdown_phase_1(connection);
834 gb_connection_hd_cport_quiesce(connection);
835 gb_connection_cport_shutdown_phase_2(connection);
836 gb_connection_control_disconnected(connection);
838 connection->state = GB_CONNECTION_STATE_DISABLED;
840 /* control-connection tear down is deferred when mode switching */
841 if (!connection->mode_switch) {
842 gb_connection_svc_connection_destroy(connection);
843 gb_connection_hd_cport_clear(connection);
845 gb_connection_hd_cport_disable(connection);
849 mutex_unlock(&connection->mutex);
851 EXPORT_SYMBOL_GPL(gb_connection_disable);
853 /* Disable a connection without communicating with the remote end. */
854 void gb_connection_disable_forced(struct gb_connection *connection)
856 mutex_lock(&connection->mutex);
858 if (connection->state == GB_CONNECTION_STATE_DISABLED)
861 trace_gb_connection_disable(connection);
863 spin_lock_irq(&connection->lock);
864 connection->state = GB_CONNECTION_STATE_DISABLED;
865 gb_connection_cancel_operations(connection, -ESHUTDOWN);
866 spin_unlock_irq(&connection->lock);
868 gb_connection_hd_cport_flush(connection);
870 gb_connection_svc_connection_destroy(connection);
871 gb_connection_hd_cport_clear(connection);
873 gb_connection_hd_cport_disable(connection);
875 mutex_unlock(&connection->mutex);
877 EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
879 /* Caller must have disabled the connection before destroying it. */
880 void gb_connection_destroy(struct gb_connection *connection)
885 if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
886 gb_connection_disable(connection);
888 mutex_lock(&gb_connection_mutex);
890 spin_lock_irq(&gb_connections_lock);
891 list_del(&connection->bundle_links);
892 list_del(&connection->hd_links);
893 spin_unlock_irq(&gb_connections_lock);
895 destroy_workqueue(connection->wq);
897 gb_hd_cport_release(connection->hd, connection->hd_cport_id);
898 connection->hd_cport_id = CPORT_ID_BAD;
900 mutex_unlock(&gb_connection_mutex);
902 gb_connection_put(connection);
904 EXPORT_SYMBOL_GPL(gb_connection_destroy);
906 void gb_connection_latency_tag_enable(struct gb_connection *connection)
908 struct gb_host_device *hd = connection->hd;
911 if (!hd->driver->latency_tag_enable)
914 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
916 dev_err(&connection->hd->dev,
917 "%s: failed to enable latency tag: %d\n",
918 connection->name, ret);
921 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
923 void gb_connection_latency_tag_disable(struct gb_connection *connection)
925 struct gb_host_device *hd = connection->hd;
928 if (!hd->driver->latency_tag_disable)
931 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
933 dev_err(&connection->hd->dev,
934 "%s: failed to disable latency tag: %d\n",
935 connection->name, ret);
938 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);