greybus: connection: rename an error label
[cascardo/linux.git] / drivers / staging / greybus / connection.c
index c3207c8..9c7133e 100644 (file)
 #include "greybus.h"
 
 
-static int gb_connection_bind_protocol(struct gb_connection *connection);
-static void gb_connection_unbind_protocol(struct gb_connection *connection);
+static void gb_connection_kref_release(struct kref *kref);
 
 
 static DEFINE_SPINLOCK(gb_connections_lock);
+static DEFINE_MUTEX(gb_connection_mutex);
 
-/* This is only used at initialization time; no locking is required. */
+
+/* Caller holds gb_connection_mutex. */
 static struct gb_connection *
 gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
 {
@@ -34,6 +35,19 @@ gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
        return NULL;
 }
 
+static void gb_connection_get(struct gb_connection *connection)
+{
+       kref_get(&connection->kref);
+}
+
+static void gb_connection_put(struct gb_connection *connection)
+{
+       kref_put(&connection->kref, gb_connection_kref_release);
+}
+
+/*
+ * Returns a reference-counted pointer to the connection if found.
+ */
 static struct gb_connection *
 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
 {
@@ -42,8 +56,10 @@ gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
 
        spin_lock_irqsave(&gb_connections_lock, flags);
        list_for_each_entry(connection, &hd->connections, hd_links)
-               if (connection->hd_cport_id == cport_id)
+               if (connection->hd_cport_id == cport_id) {
+                       gb_connection_get(connection);
                        goto found;
+               }
        connection = NULL;
 found:
        spin_unlock_irqrestore(&gb_connections_lock, flags);
@@ -67,19 +83,17 @@ void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
                return;
        }
        gb_connection_recv(connection, data, length);
+       gb_connection_put(connection);
 }
 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
 
-static DEFINE_MUTEX(connection_mutex);
-
 static void gb_connection_kref_release(struct kref *kref)
 {
        struct gb_connection *connection;
 
        connection = container_of(kref, struct gb_connection, kref);
-       destroy_workqueue(connection->wq);
+
        kfree(connection);
-       mutex_unlock(&connection_mutex);
 }
 
 static void gb_connection_init_name(struct gb_connection *connection)
@@ -98,13 +112,14 @@ static void gb_connection_init_name(struct gb_connection *connection)
 }
 
 /*
- * gb_connection_create() - create a Greybus connection
+ * _gb_connection_create() - create a Greybus connection
  * @hd:                        host device of the connection
  * @hd_cport_id:       host-device cport id, or -1 for dynamic allocation
  * @intf:              remote interface, or NULL for static connections
  * @bundle:            remote-interface bundle (may be NULL)
  * @cport_id:          remote-interface cport id, or 0 for static connections
- * @protocol_id:       protocol id
+ * @handler:           request handler (may be NULL)
+ * @flags:             connection flags
  *
  * Create a Greybus connection, representing the bidirectional link
  * between a CPort on a (local) Greybus host device and a CPort on
@@ -113,60 +128,52 @@ static void gb_connection_init_name(struct gb_connection *connection)
  * A connection also maintains the state of operations sent over the
  * connection.
  *
- * Return: A pointer to the new connection if successful, or NULL otherwise.
+ * Serialised against concurrent create and destroy using the
+ * gb_connection_mutex.
+ *
+ * Return: A pointer to the new connection if successful, or an ERR_PTR
+ * otherwise.
  */
 static struct gb_connection *
-gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
+_gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
                                struct gb_interface *intf,
                                struct gb_bundle *bundle, int cport_id,
-                               u8 protocol_id)
+                               gb_request_handler_t handler,
+                               unsigned long flags)
 {
        struct gb_connection *connection;
-       struct ida *id_map = &hd->cport_id_map;
-       int ida_start, ida_end;
-       u8 major = 0;
-       u8 minor = 1;
+       int ret;
 
-       /*
-        * If a manifest tries to reuse a cport, reject it.  We
-        * initialize connections serially so we don't need to worry
-        * about holding the connection lock.
-        */
-       if (bundle && gb_connection_intf_find(bundle->intf, cport_id)) {
-               dev_err(&bundle->dev, "cport %u already connected\n",
-                               cport_id);
-               return NULL;
-       }
+       mutex_lock(&gb_connection_mutex);
 
-       if (hd_cport_id < 0) {
-               ida_start = 0;
-               ida_end = hd->num_cports;
-       } else if (hd_cport_id < hd->num_cports) {
-               ida_start = hd_cport_id;
-               ida_end = hd_cport_id + 1;
-       } else {
-               dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
-               return NULL;
+       if (intf && gb_connection_intf_find(intf, cport_id)) {
+               dev_err(&intf->dev, "cport %u already in use\n", cport_id);
+               ret = -EBUSY;
+               goto err_unlock;
        }
 
-       hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
-       if (hd_cport_id < 0)
-               return NULL;
+       ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
+       if (ret < 0) {
+               dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
+               goto err_unlock;
+       }
+       hd_cport_id = ret;
 
        connection = kzalloc(sizeof(*connection), GFP_KERNEL);
-       if (!connection)
-               goto err_remove_ida;
+       if (!connection) {
+               ret = -ENOMEM;
+               goto err_hd_cport_release;
+       }
 
        connection->hd_cport_id = hd_cport_id;
        connection->intf_cport_id = cport_id;
        connection->hd = hd;
        connection->intf = intf;
-
-       connection->protocol_id = protocol_id;
-       connection->major = major;
-       connection->minor = minor;
-
        connection->bundle = bundle;
+       connection->handler = handler;
+       connection->flags = flags;
+       if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
+               connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
        connection->state = GB_CONNECTION_STATE_DISABLED;
 
        atomic_set(&connection->op_cycle, 0);
@@ -176,8 +183,10 @@ gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
 
        connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
                                         dev_name(&hd->dev), hd_cport_id);
-       if (!connection->wq)
+       if (!connection->wq) {
+               ret = -ENOMEM;
                goto err_free_connection;
+       }
 
        kref_init(&connection->kref);
 
@@ -193,32 +202,70 @@ gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
 
        spin_unlock_irq(&gb_connections_lock);
 
+       mutex_unlock(&gb_connection_mutex);
+
        return connection;
 
 err_free_connection:
        kfree(connection);
-err_remove_ida:
-       ida_simple_remove(id_map, hd_cport_id);
+err_hd_cport_release:
+       gb_hd_cport_release(hd, hd_cport_id);
+err_unlock:
+       mutex_unlock(&gb_connection_mutex);
 
-       return NULL;
+       return ERR_PTR(ret);
+}
+
+struct gb_connection *
+gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
+                                       gb_request_handler_t handler)
+{
+       return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler,
+                                       0);
 }
 
 struct gb_connection *
-gb_connection_create_static(struct gb_host_device *hd,
-                                       u16 hd_cport_id, u8 protocol_id)
+gb_connection_create_control(struct gb_interface *intf)
 {
-       return gb_connection_create(hd, hd_cport_id, NULL, NULL, 0,
-                                                               protocol_id);
+       return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL,
+                                       GB_CONNECTION_FLAG_CONTROL);
 }
 
 struct gb_connection *
-gb_connection_create_dynamic(struct gb_interface *intf,
-                                       struct gb_bundle *bundle,
-                                       u16 cport_id, u8 protocol_id)
+gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
+                                       gb_request_handler_t handler)
 {
-       return gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
-                                                               protocol_id);
+       struct gb_interface *intf = bundle->intf;
+
+       return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
+                                       handler, 0);
+}
+EXPORT_SYMBOL_GPL(gb_connection_create);
+
+struct gb_connection *
+gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
+                                       gb_request_handler_t handler,
+                                       unsigned long flags)
+{
+       struct gb_interface *intf = bundle->intf;
+
+       return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
+                                       handler, flags);
 }
+EXPORT_SYMBOL_GPL(gb_connection_create_flags);
+
+struct gb_connection *
+gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
+                                       unsigned long flags)
+{
+       struct gb_interface *intf = bundle->intf;
+
+       flags |= GB_CONNECTION_FLAG_OFFLOADED;
+
+       return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
+                                       NULL, flags);
+}
+EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
 
 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
 {
@@ -231,7 +278,8 @@ static int gb_connection_hd_cport_enable(struct gb_connection *connection)
        ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
        if (ret) {
                dev_err(&hd->dev,
-                       "failed to enable host cport: %d\n", ret);
+                               "%s: failed to enable host cport: %d\n",
+                               connection->name, ret);
                return ret;
        }
 
@@ -241,11 +289,47 @@ static int gb_connection_hd_cport_enable(struct gb_connection *connection)
 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
 {
        struct gb_host_device *hd = connection->hd;
+       int ret;
 
        if (!hd->driver->cport_disable)
                return;
 
-       hd->driver->cport_disable(hd, connection->hd_cport_id);
+       ret = hd->driver->cport_disable(hd, connection->hd_cport_id);
+       if (ret) {
+               dev_err(&hd->dev,
+                               "%s: failed to disable host cport: %d\n",
+                               connection->name, ret);
+       }
+}
+
+static int
+gb_connection_hd_cport_features_enable(struct gb_connection *connection)
+{
+       struct gb_host_device *hd = connection->hd;
+       int ret;
+
+       if (!hd->driver->cport_features_enable)
+               return 0;
+
+       ret = hd->driver->cport_features_enable(hd, connection->hd_cport_id);
+       if (ret) {
+               dev_err(&hd->dev, "%s: failed to enable CPort features: %d\n",
+                       connection->name, ret);
+               return ret;
+       }
+
+       return 0;
+}
+
+static void
+gb_connection_hd_cport_features_disable(struct gb_connection *connection)
+{
+       struct gb_host_device *hd = connection->hd;
+
+       if (!hd->driver->cport_features_disable)
+               return;
+
+       hd->driver->cport_features_disable(hd, connection->hd_cport_id);
 }
 
 /*
@@ -257,18 +341,31 @@ gb_connection_svc_connection_create(struct gb_connection *connection)
 {
        struct gb_host_device *hd = connection->hd;
        struct gb_interface *intf;
+       u8 cport_flags;
        int ret;
 
        if (gb_connection_is_static(connection))
-               return 0;
+               return gb_connection_hd_cport_features_enable(connection);
 
        intf = connection->intf;
+
+       /*
+        * Enable either E2EFC or CSD, unless no flow control is requested.
+        */
+       cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
+       if (gb_connection_flow_control_disabled(connection)) {
+               cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
+       } else if (gb_connection_e2efc_enabled(connection)) {
+               cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
+                               GB_SVC_CPORT_FLAG_E2EFC;
+       }
+
        ret = gb_svc_connection_create(hd->svc,
                        hd->svc->ap_intf_id,
                        connection->hd_cport_id,
                        intf->interface_id,
                        connection->intf_cport_id,
-                       intf->boot_over_unipro);
+                       cport_flags);
        if (ret) {
                dev_err(&connection->hd->dev,
                        "%s: failed to create svc connection: %d\n",
@@ -276,12 +373,23 @@ gb_connection_svc_connection_create(struct gb_connection *connection)
                return ret;
        }
 
+       ret = gb_connection_hd_cport_features_enable(connection);
+       if (ret) {
+               gb_svc_connection_destroy(hd->svc, hd->svc->ap_intf_id,
+                                         connection->hd_cport_id,
+                                         intf->interface_id,
+                                         connection->intf_cport_id);
+               return ret;
+       }
+
        return 0;
 }
 
 static void
 gb_connection_svc_connection_destroy(struct gb_connection *connection)
 {
+       gb_connection_hd_cport_features_disable(connection);
+
        if (gb_connection_is_static(connection))
                return;
 
@@ -295,15 +403,27 @@ gb_connection_svc_connection_destroy(struct gb_connection *connection)
 /* Inform Interface about active CPorts */
 static int gb_connection_control_connected(struct gb_connection *connection)
 {
-       struct gb_protocol *protocol = connection->protocol;
        struct gb_control *control;
        u16 cport_id = connection->intf_cport_id;
        int ret;
 
-       if (protocol->flags & GB_PROTOCOL_SKIP_CONTROL_CONNECTED)
+       if (gb_connection_is_static(connection))
                return 0;
 
-       control = connection->bundle->intf->control;
+       /*
+        * HACK: Suppress connected request for the offloaded camera
+        * connection as it is currently not supported by firmware. Note that
+        * the corresponding non-fatal disconnected event is still sent.
+        */
+       if (gb_connection_is_offloaded(connection) &&
+                       connection->flags & GB_CONNECTION_FLAG_CDSI1) {
+               return 0;
+       }
+
+       if (gb_connection_is_control(connection))
+               return 0;
+
+       control = connection->intf->control;
 
        ret = gb_control_connected_operation(control, cport_id);
        if (ret) {
@@ -319,15 +439,17 @@ static int gb_connection_control_connected(struct gb_connection *connection)
 static void
 gb_connection_control_disconnected(struct gb_connection *connection)
 {
-       struct gb_protocol *protocol = connection->protocol;
        struct gb_control *control;
        u16 cport_id = connection->intf_cport_id;
        int ret;
 
-       if (protocol->flags & GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED)
+       if (gb_connection_is_static(connection))
+               return;
+
+       if (gb_connection_is_control(connection))
                return;
 
-       control = connection->bundle->intf->control;
+       control = connection->intf->control;
 
        ret = gb_control_disconnected_operation(control, cport_id);
        if (ret) {
@@ -336,29 +458,6 @@ gb_connection_control_disconnected(struct gb_connection *connection)
        }
 }
 
-/*
- * Request protocol version supported by the module. We don't need to do
- * this for SVC as that is initiated by the SVC.
- */
-static int gb_connection_protocol_get_version(struct gb_connection *connection)
-{
-       struct gb_protocol *protocol = connection->protocol;
-       int ret;
-
-       if (protocol->flags & GB_PROTOCOL_SKIP_VERSION)
-               return 0;
-
-       ret = gb_protocol_get_version(connection);
-       if (ret) {
-               dev_err(&connection->hd->dev,
-                       "%s: failed to get protocol version: %d\n",
-                       connection->name, ret);
-               return ret;
-       }
-
-       return 0;
-}
-
 /*
  * Cancel all active operations on a connection.
  *
@@ -366,6 +465,7 @@ static int gb_connection_protocol_get_version(struct gb_connection *connection)
  */
 static void gb_connection_cancel_operations(struct gb_connection *connection,
                                                int errno)
+       __must_hold(&connection->lock)
 {
        struct gb_operation *operation;
 
@@ -394,6 +494,7 @@ static void gb_connection_cancel_operations(struct gb_connection *connection,
 static void
 gb_connection_flush_incoming_operations(struct gb_connection *connection,
                                                int errno)
+       __must_hold(&connection->lock)
 {
        struct gb_operation *operation;
        bool incoming;
@@ -422,39 +523,42 @@ gb_connection_flush_incoming_operations(struct gb_connection *connection,
        }
 }
 
-int gb_connection_enable(struct gb_connection *connection,
-                               gb_request_handler_t handler)
+/*
+ * _gb_connection_enable() - enable a connection
+ * @connection:                connection to enable
+ * @rx:                        whether to enable incoming requests
+ *
+ * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
+ * ENABLED_TX->ENABLED state transitions.
+ *
+ * Locking: Caller holds connection->mutex.
+ */
+static int _gb_connection_enable(struct gb_connection *connection, bool rx)
 {
        int ret;
 
-       mutex_lock(&connection->mutex);
-
-       if (connection->state == GB_CONNECTION_STATE_ENABLED)
-               goto out_unlock;
-
+       /* Handle ENABLED_TX -> ENABLED transitions. */
        if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
-               if (!handler)
-                       goto out_unlock;
+               if (!(connection->handler && rx))
+                       return 0;
 
                spin_lock_irq(&connection->lock);
-               connection->handler = handler;
                connection->state = GB_CONNECTION_STATE_ENABLED;
                spin_unlock_irq(&connection->lock);
 
-               goto out_unlock;
+               return 0;
        }
 
        ret = gb_connection_hd_cport_enable(connection);
        if (ret)
-               goto err_unlock;
+               return ret;
 
        ret = gb_connection_svc_connection_create(connection);
        if (ret)
                goto err_hd_cport_disable;
 
        spin_lock_irq(&connection->lock);
-       connection->handler = handler;
-       if (handler)
+       if (connection->handler && rx)
                connection->state = GB_CONNECTION_STATE_ENABLED;
        else
                connection->state = GB_CONNECTION_STATE_ENABLED_TX;
@@ -462,30 +566,62 @@ int gb_connection_enable(struct gb_connection *connection,
 
        ret = gb_connection_control_connected(connection);
        if (ret)
-               goto err_svc_destroy;
-
-out_unlock:
-       mutex_unlock(&connection->mutex);
+               goto err_flush_operations;
 
        return 0;
 
-err_svc_destroy:
+err_flush_operations:
        spin_lock_irq(&connection->lock);
        connection->state = GB_CONNECTION_STATE_DISABLED;
        gb_connection_cancel_operations(connection, -ESHUTDOWN);
-       connection->handler = NULL;
        spin_unlock_irq(&connection->lock);
 
        gb_connection_svc_connection_destroy(connection);
 err_hd_cport_disable:
        gb_connection_hd_cport_disable(connection);
-err_unlock:
+
+       return ret;
+}
+
+int gb_connection_enable(struct gb_connection *connection)
+{
+       int ret = 0;
+
+       mutex_lock(&connection->mutex);
+
+       if (connection->state == GB_CONNECTION_STATE_ENABLED)
+               goto out_unlock;
+
+       ret = _gb_connection_enable(connection, true);
+out_unlock:
        mutex_unlock(&connection->mutex);
 
        return ret;
 }
 EXPORT_SYMBOL_GPL(gb_connection_enable);
 
+int gb_connection_enable_tx(struct gb_connection *connection)
+{
+       int ret = 0;
+
+       mutex_lock(&connection->mutex);
+
+       if (connection->state == GB_CONNECTION_STATE_ENABLED) {
+               ret = -EINVAL;
+               goto out_unlock;
+       }
+
+       if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
+               goto out_unlock;
+
+       ret = _gb_connection_enable(connection, false);
+out_unlock:
+       mutex_unlock(&connection->mutex);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
+
 void gb_connection_disable_rx(struct gb_connection *connection)
 {
        mutex_lock(&connection->mutex);
@@ -497,12 +633,12 @@ void gb_connection_disable_rx(struct gb_connection *connection)
        }
        connection->state = GB_CONNECTION_STATE_ENABLED_TX;
        gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
-       connection->handler = NULL;
        spin_unlock_irq(&connection->lock);
 
 out_unlock:
        mutex_unlock(&connection->mutex);
 }
+EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
 
 void gb_connection_disable(struct gb_connection *connection)
 {
@@ -516,7 +652,6 @@ void gb_connection_disable(struct gb_connection *connection)
        spin_lock_irq(&connection->lock);
        connection->state = GB_CONNECTION_STATE_DISABLED;
        gb_connection_cancel_operations(connection, -ESHUTDOWN);
-       connection->handler = NULL;
        spin_unlock_irq(&connection->lock);
 
        gb_connection_svc_connection_destroy(connection);
@@ -527,85 +662,53 @@ out_unlock:
 }
 EXPORT_SYMBOL_GPL(gb_connection_disable);
 
-static int gb_legacy_request_handler(struct gb_operation *operation)
-{
-       struct gb_protocol *protocol = operation->connection->protocol;
-
-       return protocol->request_recv(operation->type, operation);
-}
-
-int gb_connection_legacy_init(struct gb_connection *connection)
+/* Disable a connection without communicating with the remote end. */
+void gb_connection_disable_forced(struct gb_connection *connection)
 {
-       gb_request_handler_t handler;
-       int ret;
-
-       ret = gb_connection_bind_protocol(connection);
-       if (ret)
-               return ret;
-
-       if (connection->protocol->request_recv)
-               handler = gb_legacy_request_handler;
-       else
-               handler = NULL;
-
-       ret = gb_connection_enable(connection, handler);
-       if (ret)
-               goto err_unbind_protocol;
-
-       ret = gb_connection_protocol_get_version(connection);
-       if (ret)
-               goto err_disable;
-
-       ret = connection->protocol->connection_init(connection);
-       if (ret)
-               goto err_disable;
-
-       return 0;
-
-err_disable:
-       gb_connection_disable(connection);
-err_unbind_protocol:
-       gb_connection_unbind_protocol(connection);
-
-       return ret;
-}
-EXPORT_SYMBOL_GPL(gb_connection_legacy_init);
+       mutex_lock(&connection->mutex);
 
-void gb_connection_legacy_exit(struct gb_connection *connection)
-{
        if (connection->state == GB_CONNECTION_STATE_DISABLED)
-               return;
+               goto out_unlock;
 
-       gb_connection_disable(connection);
+       spin_lock_irq(&connection->lock);
+       connection->state = GB_CONNECTION_STATE_DISABLED;
+       gb_connection_cancel_operations(connection, -ESHUTDOWN);
+       spin_unlock_irq(&connection->lock);
 
-       connection->protocol->connection_exit(connection);
+       gb_connection_svc_connection_destroy(connection);
+       gb_connection_hd_cport_disable(connection);
 
-       gb_connection_unbind_protocol(connection);
+out_unlock:
+       mutex_unlock(&connection->mutex);
 }
-EXPORT_SYMBOL_GPL(gb_connection_legacy_exit);
+EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
 
-/*
- * Tear down a previously set up connection.
- */
+/* Caller must have disabled the connection before destroying it. */
 void gb_connection_destroy(struct gb_connection *connection)
 {
-       struct ida *id_map;
-
-       if (WARN_ON(!connection))
+       if (!connection)
                return;
 
+       if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
+               gb_connection_disable(connection);
+
+       mutex_lock(&gb_connection_mutex);
+
        spin_lock_irq(&gb_connections_lock);
        list_del(&connection->bundle_links);
        list_del(&connection->hd_links);
        spin_unlock_irq(&gb_connections_lock);
 
-       id_map = &connection->hd->cport_id_map;
-       ida_simple_remove(id_map, connection->hd_cport_id);
+       destroy_workqueue(connection->wq);
+
+       gb_hd_cport_release(connection->hd, connection->hd_cport_id);
        connection->hd_cport_id = CPORT_ID_BAD;
 
-       kref_put_mutex(&connection->kref, gb_connection_kref_release,
-                      &connection_mutex);
+       mutex_unlock(&gb_connection_mutex);
+
+       gb_connection_put(connection);
 }
+EXPORT_SYMBOL_GPL(gb_connection_destroy);
 
 void gb_connection_latency_tag_enable(struct gb_connection *connection)
 {
@@ -640,31 +743,3 @@ void gb_connection_latency_tag_disable(struct gb_connection *connection)
        }
 }
 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);
-
-static int gb_connection_bind_protocol(struct gb_connection *connection)
-{
-       struct gb_protocol *protocol;
-
-       protocol = gb_protocol_get(connection->protocol_id,
-                                  connection->major,
-                                  connection->minor);
-       if (!protocol) {
-               dev_err(&connection->hd->dev,
-                               "protocol 0x%02x version %u.%u not found\n",
-                               connection->protocol_id,
-                               connection->major, connection->minor);
-               return -EPROTONOSUPPORT;
-       }
-       connection->protocol = protocol;
-
-       return 0;
-}
-
-static void gb_connection_unbind_protocol(struct gb_connection *connection)
-{
-       struct gb_protocol *protocol = connection->protocol;
-
-       gb_protocol_put(protocol);
-
-       connection->protocol = NULL;
-}