greybus: connection: make connection enable/disable thread safe
[cascardo/linux.git] / drivers / staging / greybus / connection.c
index 294e72e..56588f3 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 DEFINE_SPINLOCK(gb_connections_lock);
 
 /* This is only used at initialization time; no locking is required. */
 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 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;
@@ -48,14 +55,14 @@ 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;
        }
@@ -75,45 +82,48 @@ static void gb_connection_kref_release(struct kref *kref)
        mutex_unlock(&connection_mutex);
 }
 
-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;
+       u16 hd_cport_id = connection->hd_cport_id;
+       u16 cport_id = 0;
+       u8 intf_id = 0;
 
-       bundle = gb_bundle_create(intf, GB_SVC_BUNDLE_ID, GREYBUS_CLASS_SVC);
-       if (!bundle)
-               return -EINVAL;
-
-       connection->bundle = bundle;
-
-       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
+ * @protocol_id:       protocol id
+ *
+ * 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.
+ * Return: A pointer to the new connection if successful, or NULL 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,
+                               u8 protocol_id)
 {
        struct gb_connection *connection;
        struct ida *id_map = &hd->cport_id_map;
-       int hd_cport_id;
-       int retval;
+       int ida_start, ida_end;
        u8 major = 0;
        u8 minor = 1;
 
@@ -123,11 +133,22 @@ gb_connection_create_range(struct greybus_host_device *hd,
         * about holding the connection lock.
         */
        if (bundle && gb_connection_intf_find(bundle->intf, cport_id)) {
-               dev_err(&bundle->dev, "cport 0x%04hx already connected\n",
+               dev_err(&bundle->dev, "cport %u already connected\n",
                                cport_id);
                return NULL;
        }
 
+       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;
+       }
+
        hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
        if (hd_cport_id < 0)
                return NULL;
@@ -139,6 +160,7 @@ gb_connection_create_range(struct greybus_host_device *hd,
        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;
@@ -148,16 +170,19 @@ gb_connection_create_range(struct greybus_host_device *hd,
        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);
+                                        dev_name(&hd->dev), hd_cport_id);
        if (!connection->wq)
                goto err_free_connection;
 
        kref_init(&connection->kref);
 
+       gb_connection_init_name(connection);
+
        spin_lock_irq(&gb_connections_lock);
        list_add(&connection->hd_links, &hd->connections);
 
@@ -168,14 +193,6 @@ gb_connection_create_range(struct greybus_host_device *hd,
 
        spin_unlock_irq(&gb_connections_lock);
 
-       retval = gb_connection_bind_protocol(connection);
-       if (retval) {
-               dev_err(parent, "%d: failed to bind protocol: %d\n",
-                       cport_id, retval);
-               gb_connection_destroy(connection);
-               return NULL;
-       }
-
        return connection;
 
 err_free_connection:
@@ -186,9 +203,26 @@ err_remove_ida:
        return NULL;
 }
 
+struct gb_connection *
+gb_connection_create_static(struct gb_host_device *hd,
+                                       u16 hd_cport_id, u8 protocol_id)
+{
+       return gb_connection_create(hd, hd_cport_id, NULL, NULL, 0,
+                                                               protocol_id);
+}
+
+struct gb_connection *
+gb_connection_create_dynamic(struct gb_interface *intf,
+                                       struct gb_bundle *bundle,
+                                       u16 cport_id, u8 protocol_id)
+{
+       return gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
+                                                               protocol_id);
+}
+
 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
 {
-       struct greybus_host_device *hd = connection->hd;
+       struct gb_host_device *hd = connection->hd;
        int ret;
 
        if (!hd->driver->cport_enable)
@@ -196,7 +230,7 @@ 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->parent,
+               dev_err(&hd->dev,
                        "failed to enable host cport: %d\n", ret);
                return ret;
        }
@@ -206,7 +240,7 @@ static int gb_connection_hd_cport_enable(struct gb_connection *connection)
 
 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
 {
-       struct greybus_host_device *hd = connection->hd;
+       struct gb_host_device *hd = connection->hd;
 
        if (!hd->driver->cport_disable)
                return;
@@ -214,43 +248,6 @@ static void gb_connection_hd_cport_disable(struct gb_connection *connection)
        hd->driver->cport_disable(hd, connection->hd_cport_id);
 }
 
-struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
-                               u16 cport_id, u8 protocol_id)
-{
-       return gb_connection_create_range(bundle->intf->hd, bundle,
-                                         &bundle->dev, cport_id, protocol_id,
-                                         0, bundle->intf->hd->num_cports - 1);
-}
-
-/*
- * Cancel all active operations on a connection.
- *
- * Should only be called during connection tear down.
- */
-static void gb_connection_cancel_operations(struct gb_connection *connection,
-                                               int errno)
-{
-       struct gb_operation *operation;
-
-       spin_lock_irq(&connection->lock);
-       while (!list_empty(&connection->operations)) {
-               operation = list_last_entry(&connection->operations,
-                                               struct gb_operation, links);
-               gb_operation_get(operation);
-               spin_unlock_irq(&connection->lock);
-
-               if (gb_operation_is_incoming(operation))
-                       gb_operation_cancel_incoming(operation, errno);
-               else
-                       gb_operation_cancel(operation, errno);
-
-               gb_operation_put(operation);
-
-               spin_lock_irq(&connection->lock);
-       }
-       spin_unlock_irq(&connection->lock);
-}
-
 /*
  * Request the SVC to create a connection from AP's cport to interface's
  * cport.
@@ -258,24 +255,24 @@ static void gb_connection_cancel_operations(struct gb_connection *connection,
 static int
 gb_connection_svc_connection_create(struct gb_connection *connection)
 {
-       struct greybus_host_device *hd = connection->hd;
-       struct gb_protocol *protocol = connection->protocol;
+       struct gb_host_device *hd = connection->hd;
        struct gb_interface *intf;
        int ret;
 
-       if (protocol->flags & GB_PROTOCOL_SKIP_SVC_CONNECTION)
+       if (gb_connection_is_static(connection))
                return 0;
 
-       intf = connection->bundle->intf;
+       intf = connection->intf;
        ret = gb_svc_connection_create(hd->svc,
-                       hd->endo->ap_intf_id,
+                       hd->svc->ap_intf_id,
                        connection->hd_cport_id,
                        intf->interface_id,
                        connection->intf_cport_id,
                        intf->boot_over_unipro);
        if (ret) {
-               dev_err(&connection->bundle->dev,
-                       "failed to create svc connection: %d\n", ret);
+               dev_err(&connection->hd->dev,
+                       "%s: failed to create svc connection: %d\n",
+                       connection->name, ret);
                return ret;
        }
 
@@ -285,13 +282,13 @@ gb_connection_svc_connection_create(struct gb_connection *connection)
 static void
 gb_connection_svc_connection_destroy(struct gb_connection *connection)
 {
-       if (connection->protocol->flags & GB_PROTOCOL_SKIP_SVC_CONNECTION)
+       if (gb_connection_is_static(connection))
                return;
 
        gb_svc_connection_destroy(connection->hd->svc,
-                                 connection->hd->endo->ap_intf_id,
+                                 connection->hd->svc->ap_intf_id,
                                  connection->hd_cport_id,
-                                 connection->bundle->intf->interface_id,
+                                 connection->intf->interface_id,
                                  connection->intf_cport_id);
 }
 
@@ -353,80 +350,168 @@ static int gb_connection_protocol_get_version(struct gb_connection *connection)
 
        ret = gb_protocol_get_version(connection);
        if (ret) {
-               dev_err(&connection->bundle->dev,
-                       "failed to get protocol version: %d\n", ret);
+               dev_err(&connection->hd->dev,
+                       "%s: failed to get protocol version: %d\n",
+                       connection->name, ret);
                return ret;
        }
 
        return 0;
 }
 
-static int gb_connection_init(struct gb_connection *connection)
+/*
+ * Cancel all active operations on a connection.
+ *
+ * Locking: Called with connection lock held and state set to DISABLED.
+ */
+static void gb_connection_cancel_operations(struct gb_connection *connection,
+                                               int errno)
+{
+       struct gb_operation *operation;
+
+       while (!list_empty(&connection->operations)) {
+               operation = list_last_entry(&connection->operations,
+                                               struct gb_operation, links);
+               gb_operation_get(operation);
+               spin_unlock_irq(&connection->lock);
+
+               if (gb_operation_is_incoming(operation))
+                       gb_operation_cancel_incoming(operation, errno);
+               else
+                       gb_operation_cancel(operation, errno);
+
+               gb_operation_put(operation);
+
+               spin_lock_irq(&connection->lock);
+       }
+}
+
+int gb_connection_enable(struct gb_connection *connection,
+                               gb_request_handler_t handler)
 {
-       struct gb_protocol *protocol = connection->protocol;
        int ret;
 
+       mutex_lock(&connection->mutex);
+
+       if (connection->state == GB_CONNECTION_STATE_ENABLED)
+               goto out_unlock;
+
        ret = gb_connection_hd_cport_enable(connection);
        if (ret)
-               return ret;
+               goto err_unlock;
 
        ret = gb_connection_svc_connection_create(connection);
        if (ret)
                goto err_hd_cport_disable;
 
-       ret = gb_connection_control_connected(connection);
-       if (ret)
-               goto err_svc_destroy;
-
-       /* Need to enable the connection to initialize it */
        spin_lock_irq(&connection->lock);
+       connection->handler = handler;
        connection->state = GB_CONNECTION_STATE_ENABLED;
        spin_unlock_irq(&connection->lock);
 
-       ret = gb_connection_protocol_get_version(connection);
+       ret = gb_connection_control_connected(connection);
        if (ret)
-               goto err_disconnect;
+               goto err_svc_destroy;
 
-       ret = protocol->connection_init(connection);
-       if (ret)
-               goto err_disconnect;
+out_unlock:
+       mutex_unlock(&connection->mutex);
 
        return 0;
 
-err_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_control_disconnected(connection);
-err_svc_destroy:
        gb_connection_svc_connection_destroy(connection);
 err_hd_cport_disable:
        gb_connection_hd_cport_disable(connection);
+err_unlock:
+       mutex_unlock(&connection->mutex);
 
        return ret;
 }
+EXPORT_SYMBOL_GPL(gb_connection_enable);
 
-static void gb_connection_exit(struct gb_connection *connection)
+void gb_connection_disable(struct gb_connection *connection)
 {
-       if (!connection->protocol)
-               return;
+       mutex_lock(&connection->mutex);
 
-       spin_lock_irq(&connection->lock);
-       if (connection->state != GB_CONNECTION_STATE_ENABLED) {
-               spin_unlock_irq(&connection->lock);
-               return;
-       }
-       connection->state = GB_CONNECTION_STATE_DESTROYING;
-       spin_unlock_irq(&connection->lock);
+       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_control_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);
+
+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)
+{
+       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);
+
+void gb_connection_legacy_exit(struct gb_connection *connection)
+{
+       if (connection->state == GB_CONNECTION_STATE_DISABLED)
+               return;
+
+       gb_connection_disable(connection);
+
+       connection->protocol->connection_exit(connection);
+
+       gb_connection_unbind_protocol(connection);
+}
+EXPORT_SYMBOL_GPL(gb_connection_legacy_exit);
 
 /*
  * Tear down a previously set up connection.
@@ -438,17 +523,11 @@ void gb_connection_destroy(struct gb_connection *connection)
        if (WARN_ON(!connection))
                return;
 
-       gb_connection_exit(connection);
-
        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;
-
        id_map = &connection->hd->cport_id_map;
        ida_simple_remove(id_map, connection->hd_cport_id);
        connection->hd_cport_id = CPORT_ID_BAD;
@@ -459,7 +538,7 @@ void gb_connection_destroy(struct gb_connection *connection)
 
 void gb_connection_latency_tag_enable(struct gb_connection *connection)
 {
-       struct greybus_host_device *hd = connection->hd;
+       struct gb_host_device *hd = connection->hd;
        int ret;
 
        if (!hd->driver->latency_tag_enable)
@@ -467,15 +546,16 @@ void gb_connection_latency_tag_enable(struct gb_connection *connection)
 
        ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
        if (ret) {
-               dev_err(&connection->bundle->dev,
-                       "failed to enable latency tag: %d\n", 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);
 
 void gb_connection_latency_tag_disable(struct gb_connection *connection)
 {
-       struct greybus_host_device *hd = connection->hd;
+       struct gb_host_device *hd = connection->hd;
        int ret;
 
        if (!hd->driver->latency_tag_disable)
@@ -483,55 +563,37 @@ void gb_connection_latency_tag_disable(struct gb_connection *connection)
 
        ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
        if (ret) {
-               dev_err(&connection->bundle->dev,
-                       "failed to disable latency tag: %d\n", ret);
+               dev_err(&connection->hd->dev,
+                       "%s: failed to disable latency tag: %d\n",
+                       connection->name, ret);
        }
 }
 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);
 
-void gb_hd_connections_exit(struct greybus_host_device *hd)
-{
-       struct gb_connection *connection;
-
-       list_for_each_entry(connection, &hd->connections, hd_links)
-               gb_connection_destroy(connection);
-}
-
-int gb_connection_bind_protocol(struct gb_connection *connection)
+static int gb_connection_bind_protocol(struct gb_connection *connection)
 {
        struct gb_protocol *protocol;
-       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) {
-               dev_warn(connection->hd->parent,
-                               "protocol 0x%02hhx version %hhu.%hhu not found\n",
+               dev_err(&connection->hd->dev,
+                               "protocol 0x%02x version %u.%u not found\n",
                                connection->protocol_id,
                                connection->major, connection->minor);
-               return 0;
+               return -EPROTONOSUPPORT;
        }
        connection->protocol = protocol;
 
-       /*
-        * 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 &&
-            protocol->flags & GB_PROTOCOL_NO_BUNDLE) ||
-           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;
-               }
-       }
-
        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;
+}