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,
231 GB_CONNECTION_FLAG_CONTROL);
234 struct gb_connection *
235 gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
236 gb_request_handler_t handler)
238 struct gb_interface *intf = bundle->intf;
240 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
243 EXPORT_SYMBOL_GPL(gb_connection_create);
245 struct gb_connection *
246 gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
247 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_flags);
257 struct gb_connection *
258 gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
261 struct gb_interface *intf = bundle->intf;
263 flags |= GB_CONNECTION_FLAG_OFFLOADED;
265 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
268 EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
270 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
272 struct gb_host_device *hd = connection->hd;
275 if (!hd->driver->cport_enable)
278 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
281 "%s: failed to enable host cport: %d\n",
282 connection->name, ret);
289 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
291 struct gb_host_device *hd = connection->hd;
294 if (!hd->driver->cport_disable)
297 ret = hd->driver->cport_disable(hd, connection->hd_cport_id);
300 "%s: failed to disable host cport: %d\n",
301 connection->name, ret);
305 static int gb_connection_hd_cport_flush(struct gb_connection *connection)
307 struct gb_host_device *hd = connection->hd;
310 if (!hd->driver->cport_flush)
313 ret = hd->driver->cport_flush(hd, connection->hd_cport_id);
315 dev_err(&hd->dev, "%s: failed to flush host cport: %d\n",
316 connection->name, ret);
324 gb_connection_hd_cport_features_enable(struct gb_connection *connection)
326 struct gb_host_device *hd = connection->hd;
329 if (!hd->driver->cport_features_enable)
332 ret = hd->driver->cport_features_enable(hd, connection->hd_cport_id);
334 dev_err(&hd->dev, "%s: failed to enable CPort features: %d\n",
335 connection->name, ret);
343 gb_connection_hd_cport_features_disable(struct gb_connection *connection)
345 struct gb_host_device *hd = connection->hd;
347 if (!hd->driver->cport_features_disable)
350 hd->driver->cport_features_disable(hd, connection->hd_cport_id);
354 * Request the SVC to create a connection from AP's cport to interface's
358 gb_connection_svc_connection_create(struct gb_connection *connection)
360 struct gb_host_device *hd = connection->hd;
361 struct gb_interface *intf;
365 if (gb_connection_is_static(connection))
368 intf = connection->intf;
371 * Enable either E2EFC or CSD, unless no flow control is requested.
373 cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
374 if (gb_connection_flow_control_disabled(connection)) {
375 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
376 } else if (gb_connection_e2efc_enabled(connection)) {
377 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
378 GB_SVC_CPORT_FLAG_E2EFC;
381 ret = gb_svc_connection_create(hd->svc,
383 connection->hd_cport_id,
385 connection->intf_cport_id,
388 dev_err(&connection->hd->dev,
389 "%s: failed to create svc connection: %d\n",
390 connection->name, ret);
398 gb_connection_svc_connection_destroy(struct gb_connection *connection)
400 if (gb_connection_is_static(connection))
403 gb_svc_connection_destroy(connection->hd->svc,
404 connection->hd->svc->ap_intf_id,
405 connection->hd_cport_id,
406 connection->intf->interface_id,
407 connection->intf_cport_id);
411 gb_connection_svc_connection_quiescing(struct gb_connection *connection)
413 struct gb_host_device *hd = connection->hd;
415 if (gb_connection_is_static(connection))
418 gb_svc_connection_quiescing(hd->svc,
420 connection->hd_cport_id,
421 connection->intf->interface_id,
422 connection->intf_cport_id);
425 /* Inform Interface about active CPorts */
426 static int gb_connection_control_connected(struct gb_connection *connection)
428 struct gb_control *control;
429 u16 cport_id = connection->intf_cport_id;
432 if (gb_connection_is_static(connection))
436 * HACK: Suppress connected request for the offloaded camera
437 * connection as it is currently not supported by firmware. Note that
438 * the corresponding non-fatal disconnected event is still sent.
440 if (gb_connection_is_offloaded(connection) &&
441 connection->flags & GB_CONNECTION_FLAG_CDSI1) {
445 if (gb_connection_is_control(connection))
448 control = connection->intf->control;
450 ret = gb_control_connected_operation(control, cport_id);
452 dev_err(&connection->bundle->dev,
453 "failed to connect cport: %d\n", ret);
461 gb_connection_control_disconnecting(struct gb_connection *connection)
463 struct gb_control *control;
464 u16 cport_id = connection->intf_cport_id;
467 if (gb_connection_is_static(connection))
470 control = connection->intf->control;
472 ret = gb_control_disconnecting_operation(control, cport_id);
474 dev_err(&connection->hd->dev,
475 "%s: failed to send disconnecting: %d\n",
476 connection->name, ret);
481 gb_connection_control_disconnected(struct gb_connection *connection)
483 struct gb_control *control;
484 u16 cport_id = connection->intf_cport_id;
487 if (gb_connection_is_static(connection))
490 if (gb_connection_is_control(connection))
493 control = connection->intf->control;
495 ret = gb_control_disconnected_operation(control, cport_id);
497 dev_warn(&connection->bundle->dev,
498 "failed to disconnect cport: %d\n", ret);
502 static int gb_connection_ping_operation(struct gb_connection *connection)
504 struct gb_operation *operation;
507 operation = gb_operation_create_core(connection,
508 GB_REQUEST_TYPE_PING,
514 ret = gb_operation_request_send_sync(operation);
516 gb_operation_put(operation);
521 static int gb_connection_ping(struct gb_connection *connection)
523 struct gb_host_device *hd = connection->hd;
526 if (gb_connection_is_static(connection))
529 if (gb_connection_is_offloaded(connection)) {
530 if (!hd->driver->cport_ping)
533 ret = hd->driver->cport_ping(hd, connection->intf_cport_id);
535 ret = gb_connection_ping_operation(connection);
539 dev_err(&hd->dev, "%s: failed to send ping: %d\n",
540 connection->name, ret);
548 * Cancel all active operations on a connection.
550 * Locking: Called with connection lock held and state set to DISABLED or
553 static void gb_connection_cancel_operations(struct gb_connection *connection,
555 __must_hold(&connection->lock)
557 struct gb_operation *operation;
559 while (!list_empty(&connection->operations)) {
560 operation = list_last_entry(&connection->operations,
561 struct gb_operation, links);
562 gb_operation_get(operation);
563 spin_unlock_irq(&connection->lock);
565 if (gb_operation_is_incoming(operation))
566 gb_operation_cancel_incoming(operation, errno);
568 gb_operation_cancel(operation, errno);
570 gb_operation_put(operation);
572 spin_lock_irq(&connection->lock);
577 * Cancel all active incoming operations on a connection.
579 * Locking: Called with connection lock held and state set to ENABLED_TX.
582 gb_connection_flush_incoming_operations(struct gb_connection *connection,
584 __must_hold(&connection->lock)
586 struct gb_operation *operation;
589 while (!list_empty(&connection->operations)) {
591 list_for_each_entry(operation, &connection->operations,
593 if (gb_operation_is_incoming(operation)) {
594 gb_operation_get(operation);
603 spin_unlock_irq(&connection->lock);
605 /* FIXME: flush, not cancel? */
606 gb_operation_cancel_incoming(operation, errno);
607 gb_operation_put(operation);
609 spin_lock_irq(&connection->lock);
614 * _gb_connection_enable() - enable a connection
615 * @connection: connection to enable
616 * @rx: whether to enable incoming requests
618 * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
619 * ENABLED_TX->ENABLED state transitions.
621 * Locking: Caller holds connection->mutex.
623 static int _gb_connection_enable(struct gb_connection *connection, bool rx)
627 /* Handle ENABLED_TX -> ENABLED transitions. */
628 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
629 if (!(connection->handler && rx))
632 spin_lock_irq(&connection->lock);
633 connection->state = GB_CONNECTION_STATE_ENABLED;
634 spin_unlock_irq(&connection->lock);
639 ret = gb_connection_hd_cport_enable(connection);
643 ret = gb_connection_svc_connection_create(connection);
645 goto err_hd_cport_disable;
647 ret = gb_connection_hd_cport_features_enable(connection);
649 goto err_svc_connection_destroy;
651 spin_lock_irq(&connection->lock);
652 if (connection->handler && rx)
653 connection->state = GB_CONNECTION_STATE_ENABLED;
655 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
656 spin_unlock_irq(&connection->lock);
658 ret = gb_connection_control_connected(connection);
660 goto err_control_disconnecting;
664 err_control_disconnecting:
665 gb_connection_control_disconnecting(connection);
667 spin_lock_irq(&connection->lock);
668 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
669 gb_connection_cancel_operations(connection, -ESHUTDOWN);
670 spin_unlock_irq(&connection->lock);
672 /* Transmit queue should already be empty. */
673 gb_connection_hd_cport_flush(connection);
675 gb_connection_ping(connection);
676 gb_connection_hd_cport_features_disable(connection);
677 gb_connection_svc_connection_quiescing(connection);
678 gb_connection_ping(connection);
679 gb_connection_control_disconnected(connection);
680 connection->state = GB_CONNECTION_STATE_DISABLED;
681 err_svc_connection_destroy:
682 gb_connection_svc_connection_destroy(connection);
683 err_hd_cport_disable:
684 gb_connection_hd_cport_disable(connection);
689 int gb_connection_enable(struct gb_connection *connection)
693 mutex_lock(&connection->mutex);
695 if (connection->state == GB_CONNECTION_STATE_ENABLED)
698 ret = _gb_connection_enable(connection, true);
700 mutex_unlock(&connection->mutex);
704 EXPORT_SYMBOL_GPL(gb_connection_enable);
706 int gb_connection_enable_tx(struct gb_connection *connection)
710 mutex_lock(&connection->mutex);
712 if (connection->state == GB_CONNECTION_STATE_ENABLED) {
717 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
720 ret = _gb_connection_enable(connection, false);
722 mutex_unlock(&connection->mutex);
726 EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
728 void gb_connection_disable_rx(struct gb_connection *connection)
730 mutex_lock(&connection->mutex);
732 spin_lock_irq(&connection->lock);
733 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
734 spin_unlock_irq(&connection->lock);
737 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
738 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
739 spin_unlock_irq(&connection->lock);
742 mutex_unlock(&connection->mutex);
744 EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
746 void gb_connection_disable(struct gb_connection *connection)
748 mutex_lock(&connection->mutex);
750 if (connection->state == GB_CONNECTION_STATE_DISABLED)
753 gb_connection_control_disconnecting(connection);
755 spin_lock_irq(&connection->lock);
756 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
757 gb_connection_cancel_operations(connection, -ESHUTDOWN);
758 spin_unlock_irq(&connection->lock);
760 gb_connection_hd_cport_flush(connection);
762 gb_connection_ping(connection);
763 gb_connection_hd_cport_features_disable(connection);
764 gb_connection_svc_connection_quiescing(connection);
765 gb_connection_ping(connection);
767 gb_connection_control_disconnected(connection);
769 connection->state = GB_CONNECTION_STATE_DISABLED;
771 gb_connection_svc_connection_destroy(connection);
772 gb_connection_hd_cport_disable(connection);
775 mutex_unlock(&connection->mutex);
777 EXPORT_SYMBOL_GPL(gb_connection_disable);
779 /* Disable a connection without communicating with the remote end. */
780 void gb_connection_disable_forced(struct gb_connection *connection)
782 mutex_lock(&connection->mutex);
784 if (connection->state == GB_CONNECTION_STATE_DISABLED)
787 spin_lock_irq(&connection->lock);
788 connection->state = GB_CONNECTION_STATE_DISABLED;
789 gb_connection_cancel_operations(connection, -ESHUTDOWN);
790 spin_unlock_irq(&connection->lock);
792 gb_connection_hd_cport_flush(connection);
793 gb_connection_hd_cport_features_disable(connection);
794 gb_connection_svc_connection_destroy(connection);
795 gb_connection_hd_cport_disable(connection);
798 mutex_unlock(&connection->mutex);
800 EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
802 /* Caller must have disabled the connection before destroying it. */
803 void gb_connection_destroy(struct gb_connection *connection)
808 if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
809 gb_connection_disable(connection);
811 mutex_lock(&gb_connection_mutex);
813 spin_lock_irq(&gb_connections_lock);
814 list_del(&connection->bundle_links);
815 list_del(&connection->hd_links);
816 spin_unlock_irq(&gb_connections_lock);
818 destroy_workqueue(connection->wq);
820 gb_hd_cport_release(connection->hd, connection->hd_cport_id);
821 connection->hd_cport_id = CPORT_ID_BAD;
823 mutex_unlock(&gb_connection_mutex);
825 gb_connection_put(connection);
827 EXPORT_SYMBOL_GPL(gb_connection_destroy);
829 void gb_connection_latency_tag_enable(struct gb_connection *connection)
831 struct gb_host_device *hd = connection->hd;
834 if (!hd->driver->latency_tag_enable)
837 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
839 dev_err(&connection->hd->dev,
840 "%s: failed to enable latency tag: %d\n",
841 connection->name, ret);
844 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
846 void gb_connection_latency_tag_disable(struct gb_connection *connection)
848 struct gb_host_device *hd = connection->hd;
851 if (!hd->driver->latency_tag_disable)
854 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
856 dev_err(&connection->hd->dev,
857 "%s: failed to disable latency tag: %d\n",
858 connection->name, ret);
861 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);