greybus: hd: generalise cport allocation
[cascardo/linux.git] / drivers / staging / greybus / connection.c
index e1a7705..395a9df 100644 (file)
 
 #include "greybus.h"
 
-#define GB_CONNECTION_TS_KFIFO_ELEMENTS        2
-#define GB_CONNECTION_TS_KFIFO_LEN \
-       (GB_CONNECTION_TS_KFIFO_ELEMENTS * sizeof(struct timeval))
+
+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)
 {
-       struct greybus_host_device *hd = intf->hd;
+       struct gb_host_device *hd = intf->hd;
        struct gb_connection *connection;
 
-       list_for_each_entry(connection, &hd->connections, hd_links)
-               if (connection->bundle->intf == intf &&
+       list_for_each_entry(connection, &hd->connections, hd_links) {
+               if (connection->intf == intf &&
                                connection->intf_cport_id == cport_id)
                        return connection;
+       }
+
        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 greybus_host_device *hd, u16 cport_id)
+gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
 {
        struct gb_connection *connection;
        unsigned long flags;
 
        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);
@@ -52,201 +71,126 @@ found:
  * Callback from the host driver to let us know that data has been
  * received on the bundle.
  */
-void greybus_data_rcvd(struct greybus_host_device *hd, u16 cport_id,
+void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
                        u8 *data, size_t length)
 {
        struct gb_connection *connection;
 
        connection = gb_connection_hd_find(hd, cport_id);
        if (!connection) {
-               dev_err(hd->parent,
+               dev_err(&hd->dev,
                        "nonexistent connection (%zu bytes dropped)\n", length);
                return;
        }
        gb_connection_recv(connection, data, length);
+       gb_connection_put(connection);
 }
 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
 
-void gb_connection_push_timestamp(struct gb_connection *connection)
-{
-       struct timeval tv;
-
-       do_gettimeofday(&tv);
-       kfifo_in_locked(&connection->ts_kfifo, (void *)&tv,
-                       sizeof(struct timeval), &connection->lock);
-}
-EXPORT_SYMBOL_GPL(gb_connection_push_timestamp);
-
-int gb_connection_pop_timestamp(struct gb_connection *connection,
-                               struct timeval *tv)
-{
-       int retval;
-
-       if (!kfifo_len(&connection->ts_kfifo))
-               return -ENOMEM;
-       retval = kfifo_out_locked(&connection->ts_kfifo, (void *)tv,
-                                 sizeof(*tv), &connection->lock);
-       return retval;
-}
-EXPORT_SYMBOL_GPL(gb_connection_pop_timestamp);
-
-static ssize_t state_show(struct device *dev, struct device_attribute *attr,
-                         char *buf)
-{
-       struct gb_connection *connection = to_gb_connection(dev);
-       enum gb_connection_state state;
-
-       spin_lock_irq(&connection->lock);
-       state = connection->state;
-       spin_unlock_irq(&connection->lock);
-
-       return sprintf(buf, "%d\n", state);
-}
-static DEVICE_ATTR_RO(state);
-
-static ssize_t
-protocol_id_show(struct device *dev, struct device_attribute *attr, char *buf)
+static void gb_connection_kref_release(struct kref *kref)
 {
-       struct gb_connection *connection = to_gb_connection(dev);
-
-       if (connection->protocol)
-               return sprintf(buf, "%d\n", connection->protocol->id);
-       else
-               return -EINVAL;
-}
-static DEVICE_ATTR_RO(protocol_id);
-
-static struct attribute *connection_attrs[] = {
-       &dev_attr_state.attr,
-       &dev_attr_protocol_id.attr,
-       NULL,
-};
-
-ATTRIBUTE_GROUPS(connection);
+       struct gb_connection *connection;
 
-static void gb_connection_release(struct device *dev)
-{
-       struct gb_connection *connection = to_gb_connection(dev);
+       connection = container_of(kref, struct gb_connection, kref);
 
-       destroy_workqueue(connection->wq);
-       kfifo_free(&connection->ts_kfifo);
        kfree(connection);
 }
 
-struct device_type greybus_connection_type = {
-       .name =         "greybus_connection",
-       .release =      gb_connection_release,
-};
-
-
-int svc_update_connection(struct gb_interface *intf,
-                         struct gb_connection *connection)
+static void gb_connection_init_name(struct gb_connection *connection)
 {
-       struct gb_bundle *bundle;
-
-       bundle = gb_bundle_create(intf, GB_SVC_BUNDLE_ID, GREYBUS_CLASS_SVC);
-       if (!bundle)
-               return -EINVAL;
+       u16 hd_cport_id = connection->hd_cport_id;
+       u16 cport_id = 0;
+       u8 intf_id = 0;
 
-       device_del(&connection->dev);
-       connection->bundle = bundle;
-       connection->dev.parent = &bundle->dev;
-       dev_set_name(&connection->dev, "%s:%d", dev_name(&bundle->dev),
-                    GB_SVC_CPORT_ID);
-
-       WARN_ON(device_add(&connection->dev));
-
-       spin_lock_irq(&gb_connections_lock);
-       list_add(&connection->bundle_links, &bundle->connections);
-       spin_unlock_irq(&gb_connections_lock);
+       if (connection->intf) {
+               intf_id = connection->intf->interface_id;
+               cport_id = connection->intf_cport_id;
+       }
 
-       return 0;
+       snprintf(connection->name, sizeof(connection->name),
+                       "%u/%u:%u", hd_cport_id, intf_id, cport_id);
 }
 
 /*
- * Set up a Greybus connection, representing the bidirectional link
+ * _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
+ * @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
- * another Greybus module.
+ * another Greybus interface.
  *
  * A connection also maintains the state of operations sent over the
  * connection.
  *
- * Returns a pointer to the new connection if successful, or a null
- * pointer 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.
  */
-struct gb_connection *
-gb_connection_create_range(struct greybus_host_device *hd,
-                          struct gb_bundle *bundle, struct device *parent,
-                          u16 cport_id, u8 protocol_id, u32 ida_start,
-                          u32 ida_end)
+static struct gb_connection *
+_gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
+                               struct gb_interface *intf,
+                               struct gb_bundle *bundle, int cport_id,
+                               gb_request_handler_t handler,
+                               unsigned long flags)
 {
        struct gb_connection *connection;
-       struct ida *id_map = &hd->cport_id_map;
-       int hd_cport_id;
-       int retval;
-       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)) {
-               pr_err("duplicate interface cport id 0x%04hx\n", cport_id);
-               return NULL;
+       mutex_lock(&gb_connection_mutex);
+
+       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->protocol_id = protocol_id;
-       connection->major = major;
-       connection->minor = minor;
-
+       connection->intf = intf;
        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);
+       mutex_init(&connection->mutex);
        spin_lock_init(&connection->lock);
        INIT_LIST_HEAD(&connection->operations);
 
        connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
-                                        dev_name(parent), cport_id);
-       if (!connection->wq)
+                                        dev_name(&hd->dev), hd_cport_id);
+       if (!connection->wq) {
+               ret = -ENOMEM;
                goto err_free_connection;
+       }
 
-       if (kfifo_alloc(&connection->ts_kfifo, GB_CONNECTION_TS_KFIFO_LEN,
-                       GFP_KERNEL))
-               goto err_destroy_wq;
-
-       connection->dev.parent = parent;
-       connection->dev.bus = &greybus_bus_type;
-       connection->dev.type = &greybus_connection_type;
-       connection->dev.groups = connection_groups;
-       device_initialize(&connection->dev);
-       dev_set_name(&connection->dev, "%s:%d",
-                    dev_name(parent), cport_id);
-
-       retval = device_add(&connection->dev);
-       if (retval) {
-               connection->hd_cport_id = CPORT_ID_BAD;
-               put_device(&connection->dev);
+       kref_init(&connection->kref);
 
-               pr_err("failed to add connection device for cport 0x%04hx\n",
-                       cport_id);
-
-               goto err_remove_ida;
-       }
+       gb_connection_init_name(connection);
 
        spin_lock_irq(&gb_connections_lock);
        list_add(&connection->hd_links, &hd->connections);
@@ -258,42 +202,255 @@ gb_connection_create_range(struct greybus_host_device *hd,
 
        spin_unlock_irq(&gb_connections_lock);
 
-       gb_connection_bind_protocol(connection);
-       if (!connection->protocol)
-               dev_warn(&connection->dev,
-                        "protocol 0x%02hhx handler not found\n", protocol_id);
+       mutex_unlock(&gb_connection_mutex);
 
        return connection;
 
-err_destroy_wq:
-       destroy_workqueue(connection->wq);
 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_control(struct gb_interface *intf)
+{
+       return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL, 0);
+}
+
+struct gb_connection *
+gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
+                                       gb_request_handler_t handler)
+{
+       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)
+{
+       struct gb_host_device *hd = connection->hd;
+       int ret;
+
+       if (!hd->driver->cport_enable)
+               return 0;
+
+       ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
+       if (ret) {
+               dev_err(&hd->dev,
+                       "failed to enable host cport: %d\n", ret);
+               return ret;
+       }
+
+       return 0;
+}
+
+static void gb_connection_hd_cport_disable(struct gb_connection *connection)
+{
+       struct gb_host_device *hd = connection->hd;
+
+       if (!hd->driver->cport_disable)
+               return;
+
+       hd->driver->cport_disable(hd, connection->hd_cport_id);
+}
+
+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;
 }
 
-struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
-                               u16 cport_id, u8 protocol_id)
+static void
+gb_connection_hd_cport_features_disable(struct gb_connection *connection)
 {
-       return gb_connection_create_range(bundle->intf->hd, bundle,
-                                         &bundle->dev, cport_id, protocol_id,
-                                         0, bundle->intf->hd->num_cports - 1);
+       struct gb_host_device *hd = connection->hd;
+
+       if (!hd->driver->cport_features_disable)
+               return;
+
+       hd->driver->cport_features_disable(hd, connection->hd_cport_id);
+}
+
+/*
+ * Request the SVC to create a connection from AP's cport to interface's
+ * cport.
+ */
+static int
+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 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,
+                       cport_flags);
+       if (ret) {
+               dev_err(&connection->hd->dev,
+                       "%s: failed to create svc connection: %d\n",
+                       connection->name, ret);
+               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;
+
+       gb_svc_connection_destroy(connection->hd->svc,
+                                 connection->hd->svc->ap_intf_id,
+                                 connection->hd_cport_id,
+                                 connection->intf->interface_id,
+                                 connection->intf_cport_id);
+}
+
+/* Inform Interface about active CPorts */
+static int gb_connection_control_connected(struct gb_connection *connection)
+{
+       struct gb_control *control;
+       u16 cport_id = connection->intf_cport_id;
+       int ret;
+
+       if (gb_connection_is_static(connection))
+               return 0;
+
+       control = connection->intf->control;
+
+       if (connection == control->connection)
+               return 0;
+
+       ret = gb_control_connected_operation(control, cport_id);
+       if (ret) {
+               dev_err(&connection->bundle->dev,
+                       "failed to connect cport: %d\n", ret);
+               return ret;
+       }
+
+       return 0;
+}
+
+/* Inform Interface about inactive CPorts */
+static void
+gb_connection_control_disconnected(struct gb_connection *connection)
+{
+       struct gb_control *control;
+       u16 cport_id = connection->intf_cport_id;
+       int ret;
+
+       if (gb_connection_is_static(connection))
+               return;
+
+       control = connection->intf->control;
+
+       if (connection == control->connection)
+               return;
+
+       ret = gb_control_disconnected_operation(control, cport_id);
+       if (ret) {
+               dev_warn(&connection->bundle->dev,
+                        "failed to disconnect cport: %d\n", ret);
+       }
 }
 
 /*
  * Cancel all active operations on a connection.
  *
- * Should only be called during connection tear down.
+ * Locking: Called with connection lock held and state set to DISABLED.
  */
 static void gb_connection_cancel_operations(struct gb_connection *connection,
                                                int errno)
+       __must_hold(&connection->lock)
 {
        struct gb_operation *operation;
 
-       spin_lock_irq(&connection->lock);
        while (!list_empty(&connection->operations)) {
                operation = list_last_entry(&connection->operations,
                                                struct gb_operation, links);
@@ -309,211 +466,238 @@ static void gb_connection_cancel_operations(struct gb_connection *connection,
 
                spin_lock_irq(&connection->lock);
        }
-       spin_unlock_irq(&connection->lock);
 }
 
+/*
+ * Cancel all active incoming operations on a connection.
+ *
+ * Locking: Called with connection lock held and state set to ENABLED_TX.
+ */
 static void
-gb_connection_svc_connection_destroy(struct gb_connection *connection)
+gb_connection_flush_incoming_operations(struct gb_connection *connection,
+                                               int errno)
+       __must_hold(&connection->lock)
 {
-       if (connection->hd_cport_id == GB_SVC_CPORT_ID)
-               return;
-
-       if (connection->hd->driver->connection_destroy)
-               connection->hd->driver->connection_destroy(connection);
+       struct gb_operation *operation;
+       bool incoming;
 
-       gb_svc_connection_destroy(connection->hd->svc,
-                                 connection->hd->endo->ap_intf_id,
-                                 connection->hd_cport_id,
-                                 connection->bundle->intf->interface_id,
-                                 connection->intf_cport_id);
-}
+       while (!list_empty(&connection->operations)) {
+               incoming = false;
+               list_for_each_entry(operation, &connection->operations,
+                                                               links) {
+                       if (gb_operation_is_incoming(operation)) {
+                               gb_operation_get(operation);
+                               incoming = true;
+                               break;
+                       }
+               }
 
-static void gb_connection_disconnected(struct gb_connection *connection)
-{
-       struct gb_control *control;
-       int cport_id = connection->intf_cport_id;
-       int ret;
+               if (!incoming)
+                       break;
 
-       /*
-        * Inform Interface about In-active CPorts. We don't need to do this
-        * operation for control cport.
-        */
-       if ((cport_id == GB_CONTROL_CPORT_ID) ||
-           (connection->hd_cport_id == GB_SVC_CPORT_ID))
-               return;
+               spin_unlock_irq(&connection->lock);
 
-       control = connection->bundle->intf->control;
+               /* FIXME: flush, not cancel? */
+               gb_operation_cancel_incoming(operation, errno);
+               gb_operation_put(operation);
 
-       ret = gb_control_disconnected_operation(control, cport_id);
-       if (ret)
-               dev_warn(&connection->dev,
-                       "Failed to disconnect CPort-%d (%d)\n", cport_id, ret);
+               spin_lock_irq(&connection->lock);
+       }
 }
 
-static int gb_connection_init(struct gb_connection *connection)
+/*
+ * _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 cport_id = connection->intf_cport_id;
-       struct greybus_host_device *hd = connection->hd;
        int ret;
 
-       /*
-        * Request the SVC to create a connection from AP's cport to interface's
-        * cport.
-        */
-       if (connection->hd_cport_id != GB_SVC_CPORT_ID) {
-               ret = gb_svc_connection_create(hd->svc,
-                               hd->endo->ap_intf_id, connection->hd_cport_id,
-                               connection->bundle->intf->interface_id,
-                               cport_id);
-               if (ret) {
-                       dev_err(&connection->dev,
-                               "%s: Failed to create svc connection (%d)\n",
-                               __func__, ret);
-                       return ret;
-               }
+       /* Handle ENABLED_TX -> ENABLED transitions. */
+       if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
+               if (!(connection->handler && rx))
+                       return 0;
 
-               if (hd->driver->connection_create)
-                       hd->driver->connection_create(connection);
-       }
+               spin_lock_irq(&connection->lock);
+               connection->state = GB_CONNECTION_STATE_ENABLED;
+               spin_unlock_irq(&connection->lock);
 
-       /*
-        * Inform Interface about Active CPorts. We don't need to do this
-        * operation for control cport.
-        */
-       if (cport_id != GB_CONTROL_CPORT_ID &&
-           connection->hd_cport_id != GB_SVC_CPORT_ID) {
-               struct gb_control *control = connection->bundle->intf->control;
-
-               ret = gb_control_connected_operation(control, cport_id);
-               if (ret) {
-                       dev_err(&connection->dev,
-                               "Failed to connect CPort-%d (%d)\n",
-                               cport_id, ret);
-                       goto svc_destroy;
-               }
+               return 0;
        }
 
-       /* Need to enable the connection to initialize it */
+       ret = gb_connection_hd_cport_enable(connection);
+       if (ret)
+               return ret;
+
+       ret = gb_connection_svc_connection_create(connection);
+       if (ret)
+               goto err_hd_cport_disable;
+
        spin_lock_irq(&connection->lock);
-       connection->state = GB_CONNECTION_STATE_ENABLED;
+       if (connection->handler && rx)
+               connection->state = GB_CONNECTION_STATE_ENABLED;
+       else
+               connection->state = GB_CONNECTION_STATE_ENABLED_TX;
        spin_unlock_irq(&connection->lock);
 
-       /*
-        * Request protocol version supported by the module. We don't need to do
-        * this for SVC as that is initiated by the SVC.
-        */
-       if (connection->hd_cport_id != GB_SVC_CPORT_ID) {
-               ret = gb_protocol_get_version(connection);
-               if (ret) {
-                       dev_err(&connection->dev,
-                               "Failed to get version CPort-%d (%d)\n",
-                               cport_id, ret);
-                       goto disconnect;
-               }
-       }
+       ret = gb_connection_control_connected(connection);
+       if (ret)
+               goto err_svc_destroy;
 
-       ret = connection->protocol->connection_init(connection);
-       if (!ret)
-               return 0;
+       return 0;
 
-disconnect:
+err_svc_destroy:
        spin_lock_irq(&connection->lock);
-       connection->state = GB_CONNECTION_STATE_ERROR;
+       connection->state = GB_CONNECTION_STATE_DISABLED;
+       gb_connection_cancel_operations(connection, -ESHUTDOWN);
        spin_unlock_irq(&connection->lock);
 
-       gb_connection_disconnected(connection);
-svc_destroy:
        gb_connection_svc_connection_destroy(connection);
+err_hd_cport_disable:
+       gb_connection_hd_cport_disable(connection);
 
        return ret;
 }
 
-static void gb_connection_exit(struct gb_connection *connection)
+int gb_connection_enable(struct gb_connection *connection)
 {
-       if (!connection->protocol)
-               return;
+       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);
 
        spin_lock_irq(&connection->lock);
        if (connection->state != GB_CONNECTION_STATE_ENABLED) {
                spin_unlock_irq(&connection->lock);
-               return;
+               goto out_unlock;
        }
-       connection->state = GB_CONNECTION_STATE_DESTROYING;
+       connection->state = GB_CONNECTION_STATE_ENABLED_TX;
+       gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
        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)
+{
+       mutex_lock(&connection->mutex);
+
+       if (connection->state == GB_CONNECTION_STATE_DISABLED)
+               goto out_unlock;
+
+       gb_connection_control_disconnected(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_disconnected(connection);
        gb_connection_svc_connection_destroy(connection);
+       gb_connection_hd_cport_disable(connection);
+
+out_unlock:
+       mutex_unlock(&connection->mutex);
 }
+EXPORT_SYMBOL_GPL(gb_connection_disable);
 
-/*
- * 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;
 
-       gb_connection_exit(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);
 
-       if (connection->protocol)
-               gb_protocol_put(connection->protocol);
-       connection->protocol = NULL;
+       destroy_workqueue(connection->wq);
 
-       id_map = &connection->hd->cport_id_map;
-       ida_simple_remove(id_map, connection->hd_cport_id);
+       gb_hd_cport_release(connection->hd, connection->hd_cport_id);
        connection->hd_cport_id = CPORT_ID_BAD;
 
-       device_unregister(&connection->dev);
+       mutex_unlock(&gb_connection_mutex);
+
+       gb_connection_put(connection);
 }
+EXPORT_SYMBOL_GPL(gb_connection_destroy);
 
-void gb_hd_connections_exit(struct greybus_host_device *hd)
+void gb_connection_latency_tag_enable(struct gb_connection *connection)
 {
-       struct gb_connection *connection;
+       struct gb_host_device *hd = connection->hd;
+       int ret;
 
-       list_for_each_entry(connection, &hd->connections, hd_links)
-               gb_connection_destroy(connection);
+       if (!hd->driver->latency_tag_enable)
+               return;
+
+       ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
+       if (ret) {
+               dev_err(&connection->hd->dev,
+                       "%s: failed to enable latency tag: %d\n",
+                       connection->name, ret);
+       }
 }
+EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
 
-int gb_connection_bind_protocol(struct gb_connection *connection)
+void gb_connection_latency_tag_disable(struct gb_connection *connection)
 {
-       struct gb_protocol *protocol;
+       struct gb_host_device *hd = connection->hd;
        int ret;
 
-       /* If we already have a protocol bound here, just return */
-       if (connection->protocol)
-               return 0;
-
-       protocol = gb_protocol_get(connection->protocol_id,
-                                  connection->major,
-                                  connection->minor);
-       if (!protocol)
-               return 0;
-       connection->protocol = protocol;
+       if (!hd->driver->latency_tag_disable)
+               return;
 
-       /*
-        * If we have a valid device_id for the interface block, then we have an
-        * active device, so bring up the connection at the same time.
-        */
-       if ((!connection->bundle &&
-            connection->hd_cport_id == GB_SVC_CPORT_ID) ||
-           connection->bundle->intf->device_id != GB_DEVICE_ID_BAD) {
-               ret = gb_connection_init(connection);
-               if (ret) {
-                       gb_protocol_put(protocol);
-                       connection->protocol = NULL;
-                       return ret;
-               }
+       ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
+       if (ret) {
+               dev_err(&connection->hd->dev,
+                       "%s: failed to disable latency tag: %d\n",
+                       connection->name, ret);
        }
-
-       return 0;
 }
+EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);