greybus: connection: protocol can be NULL in gb_connection_exit()
[cascardo/linux.git] / drivers / staging / greybus / connection.c
index 703c286..466ea36 100644 (file)
  * Greybus connections
  *
  * Copyright 2014 Google Inc.
+ * Copyright 2014 Linaro Ltd.
  *
  * Released under the GPLv2 only.
  */
 
-#include <linux/atomic.h>
+#include <linux/workqueue.h>
 
-#include "kernel_ver.h"
 #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 DEFINE_SPINLOCK(gb_connections_lock);
 
-static void _gb_hd_connection_insert(struct greybus_host_device *hd,
-                                       struct gb_connection *connection)
+/* 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 rb_root *root = &hd->connections;
-       struct rb_node *node = &connection->hd_node;
-       struct rb_node **link = &root->rb_node;
-       struct rb_node *above = NULL;
-       u16 cport_id = connection->hd_cport_id;
-
-       while (*link) {
-               struct gb_connection *connection;
-
-               above = *link;
-               connection = rb_entry(above, struct gb_connection, hd_node);
-               if (connection->hd_cport_id > cport_id)
-                       link = &above->rb_left;
-               else if (connection->hd_cport_id < cport_id)
-                       link = &above->rb_right;
-       }
-       rb_link_node(node, above, link);
-       rb_insert_color(node, root);
-}
+       struct greybus_host_device *hd = intf->hd;
+       struct gb_connection *connection;
 
-static void _gb_hd_connection_remove(struct gb_connection *connection)
-{
-       rb_erase(&connection->hd_node, &connection->hd->connections);
+       list_for_each_entry(connection, &hd->connections, hd_links)
+               if (connection->bundle->intf == intf &&
+                               connection->intf_cport_id == cport_id)
+                       return connection;
+       return NULL;
 }
 
-struct gb_connection *gb_hd_connection_find(struct greybus_host_device *hd,
-                                               u16 cport_id)
+static struct gb_connection *
+gb_connection_hd_find(struct greybus_host_device *hd, u16 cport_id)
 {
-       struct gb_connection *connection = NULL;
-       struct rb_node *node;
+       struct gb_connection *connection;
+       unsigned long flags;
 
-       spin_lock_irq(&gb_connections_lock);
-       node = hd->connections.rb_node;
-       while (node) {
-               connection = rb_entry(node, struct gb_connection, hd_node);
-               if (connection->hd_cport_id > cport_id)
-                       node = node->rb_left;
-               else if (connection->hd_cport_id < cport_id)
-                       node = node->rb_right;
-               else
+       spin_lock_irqsave(&gb_connections_lock, flags);
+       list_for_each_entry(connection, &hd->connections, hd_links)
+               if (connection->hd_cport_id == cport_id)
                        goto found;
-       }
        connection = NULL;
- found:
-       spin_unlock_irq(&gb_connections_lock);
+found:
+       spin_unlock_irqrestore(&gb_connections_lock, flags);
 
        return connection;
 }
 
 /*
- * Allocate an available CPort Id for use for the host side of the
- * given connection.  The lowest-available id is returned, so the
- * first call is guaranteed to allocate CPort Id 0.
- *
- * Assigns the connection's hd_cport_id and returns true if successful.
- * Returns false otherwise.
+ * Callback from the host driver to let us know that data has been
+ * received on the bundle.
  */
-static bool gb_connection_hd_cport_id_alloc(struct gb_connection *connection)
+void greybus_data_rcvd(struct greybus_host_device *hd, u16 cport_id,
+                       u8 *data, size_t length)
 {
-       struct ida *ida = &connection->hd->cport_id_map;
-       int id;
+       struct gb_connection *connection;
 
-       spin_lock(&connection->hd->cport_id_map_lock);
-       id = ida_simple_get(ida, 0, HOST_DEV_CPORT_ID_MAX, GFP_KERNEL);
-       spin_unlock(&connection->hd->cport_id_map_lock);
-       if (id < 0)
-               return false;
+       connection = gb_connection_hd_find(hd, cport_id);
+       if (!connection) {
+               dev_err(hd->parent,
+                       "nonexistent connection (%zu bytes dropped)\n", length);
+               return;
+       }
+       gb_connection_recv(connection, data, length);
+}
+EXPORT_SYMBOL_GPL(greybus_data_rcvd);
 
-       connection->hd_cport_id = (u16)id;
+void gb_connection_push_timestamp(struct gb_connection *connection)
+{
+       struct timeval tv;
 
-       return true;
+       do_gettimeofday(&tv);
+       kfifo_in_locked(&connection->ts_kfifo, (void *)&tv,
+                       sizeof(struct timeval), &connection->lock);
 }
+EXPORT_SYMBOL_GPL(gb_connection_push_timestamp);
 
-/*
- * Free a previously-allocated CPort Id on the given host device.
- */
-static void gb_connection_hd_cport_id_free(struct gb_connection *connection)
+int gb_connection_pop_timestamp(struct gb_connection *connection,
+                               struct timeval *tv)
 {
-       struct ida *ida = &connection->hd->cport_id_map;
+       int retval;
 
-       spin_lock(&connection->hd->cport_id_map_lock);
-       ida_simple_remove(ida, connection->hd_cport_id);
-       spin_unlock(&connection->hd->cport_id_map_lock);
-       connection->hd_cport_id = CPORT_ID_BAD;
+       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;
 
-       return sprintf(buf, "%d", connection->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);
 
@@ -116,7 +109,10 @@ protocol_id_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
        struct gb_connection *connection = to_gb_connection(dev);
 
-       return sprintf(buf, "%d", connection->protocol->id);
+       if (connection->protocol)
+               return sprintf(buf, "%d\n", connection->protocol->id);
+       else
+               return -EINVAL;
 }
 static DEVICE_ATTR_RO(protocol_id);
 
@@ -132,14 +128,41 @@ static void gb_connection_release(struct device *dev)
 {
        struct gb_connection *connection = to_gb_connection(dev);
 
+       destroy_workqueue(connection->wq);
+       kfifo_free(&connection->ts_kfifo);
        kfree(connection);
 }
 
-static struct device_type greybus_connection_type = {
+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)
+{
+       struct gb_bundle *bundle;
+
+       bundle = gb_bundle_create(intf, GB_SVC_BUNDLE_ID, GREYBUS_CLASS_SVC);
+       if (!bundle)
+               return -EINVAL;
+
+       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);
+
+       return 0;
+}
+
 /*
  * Set up a Greybus connection, representing the bidirectional link
  * between a CPort on a (local) Greybus host device and a CPort on
@@ -151,174 +174,311 @@ static struct device_type greybus_connection_type = {
  * Returns a pointer to the new connection if successful, or a null
  * pointer otherwise.
  */
-struct gb_connection *gb_connection_create(struct gb_interface *interface,
-                               u16 cport_id, u8 protocol_id)
+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)
 {
        struct gb_connection *connection;
-       struct greybus_host_device *hd;
+       struct ida *id_map = &hd->cport_id_map;
+       int hd_cport_id;
        int retval;
        u8 major = 0;
        u8 minor = 1;
 
-       connection = kzalloc(sizeof(*connection), GFP_KERNEL);
-       if (!connection)
+       /*
+        * 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;
+       }
 
-       INIT_LIST_HEAD(&connection->protocol_links);
-       /* XXX Will have to establish connections to get version */
-       if (!gb_protocol_get(connection, protocol_id, major, minor)) {
-               pr_err("protocol 0x%02hhx not found\n", protocol_id);
-               kfree(connection);
+       hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
+       if (hd_cport_id < 0)
                return NULL;
-       }
 
-       hd = interface->gmod->hd;
+       connection = kzalloc(sizeof(*connection), GFP_KERNEL);
+       if (!connection)
+               goto err_remove_ida;
+
+       connection->hd_cport_id = hd_cport_id;
+       connection->intf_cport_id = cport_id;
        connection->hd = hd;
-       if (!gb_connection_hd_cport_id_alloc(connection)) {
-               gb_protocol_put(connection);
-               kfree(connection);
-               return NULL;
-       }
 
-       connection->interface = interface;
-       connection->interface_cport_id = cport_id;
+       connection->protocol_id = protocol_id;
+       connection->major = major;
+       connection->minor = minor;
+
+       connection->bundle = bundle;
        connection->state = GB_CONNECTION_STATE_DISABLED;
 
-       connection->dev.parent = &interface->dev;
-       connection->dev.driver = NULL;
+       atomic_set(&connection->op_cycle, 0);
+       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)
+               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(&interface->dev), cport_id);
+                    dev_name(parent), cport_id);
 
        retval = device_add(&connection->dev);
        if (retval) {
+               connection->hd_cport_id = CPORT_ID_BAD;
+               put_device(&connection->dev);
+
                pr_err("failed to add connection device for cport 0x%04hx\n",
                        cport_id);
-               gb_connection_hd_cport_id_free(connection);
-               gb_protocol_put(connection);
-               put_device(&connection->dev);
-               return NULL;
+
+               goto err_remove_ida;
        }
 
        spin_lock_irq(&gb_connections_lock);
-       _gb_hd_connection_insert(hd, connection);
-       list_add_tail(&connection->interface_links, &interface->connections);
+       list_add(&connection->hd_links, &hd->connections);
+
+       if (bundle)
+               list_add(&connection->bundle_links, &bundle->connections);
+       else
+               INIT_LIST_HEAD(&connection->bundle_links);
+
        spin_unlock_irq(&gb_connections_lock);
 
-       INIT_LIST_HEAD(&connection->operations);
-       connection->pending = RB_ROOT;
-       atomic_set(&connection->op_cycle, 0);
+       if (hd_cport_id != GB_SVC_CPORT_ID) {
+               gb_svc_connection_create(hd->svc,
+                                        hd->endo->ap_intf_id, hd_cport_id,
+                                        bundle->intf->interface_id, cport_id);
+       }
+
+       gb_connection_bind_protocol(connection);
+       if (!connection->protocol)
+               dev_warn(&connection->dev,
+                        "protocol 0x%02hhx handler not found\n", protocol_id);
 
        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);
+
+       return NULL;
+}
+
+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);
 }
 
 /*
- * Tear down a previously set up connection.
+ * Cancel all active operations on a connection.
+ *
+ * Should only be called during connection tear down.
  */
-void gb_connection_destroy(struct gb_connection *connection)
+static void gb_connection_cancel_operations(struct gb_connection *connection,
+                                               int errno)
 {
        struct gb_operation *operation;
-       struct gb_operation *next;
-
-       if (WARN_ON(!connection))
-               return;
 
-       /* XXX Need to wait for any outstanding requests to complete */
-       WARN_ON(!list_empty(&connection->operations));
+       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);
 
-       list_for_each_entry_safe(operation, next, &connection->operations,
-                                       links) {
-               gb_operation_cancel(operation);
-       }
-       spin_lock_irq(&gb_connections_lock);
-       list_del(&connection->interface_links);
-       _gb_hd_connection_remove(connection);
-       spin_unlock_irq(&gb_connections_lock);
+               if (gb_operation_is_incoming(operation))
+                       gb_operation_cancel_incoming(operation, errno);
+               else
+                       gb_operation_cancel(operation, errno);
 
-       gb_connection_hd_cport_id_free(connection);
-       /* kref_put(connection->hd); */
-       gb_protocol_put(connection);
+               gb_operation_put(operation);
 
-       device_del(&connection->dev);
-}
-
-u16 gb_connection_operation_id(struct gb_connection *connection)
-{
-       return (u16)(atomic_inc_return(&connection->op_cycle) & (int)U16_MAX);
+               spin_lock_irq(&connection->lock);
+       }
+       spin_unlock_irq(&connection->lock);
 }
 
-void gb_connection_err(struct gb_connection *connection, const char *fmt, ...)
+static void gb_connection_disconnected(struct gb_connection *connection)
 {
-       struct va_format vaf;
-       va_list args;
-
-       va_start(args, fmt);
+       struct gb_control *control;
+       int cport_id = connection->intf_cport_id;
+       int ret;
 
-       vaf.fmt = fmt;
-       vaf.va = &args;
+       /*
+        * 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;
 
-       pr_err("greybus: [%hhu:%hhu:%hu]: %pV\n",
-               connection->interface->gmod->module_id,
-               connection->interface->id,
-               connection->interface_cport_id, &vaf);
+       control = connection->bundle->intf->control;
 
-       va_end(args);
+       ret = gb_control_disconnected_operation(control, cport_id);
+       if (ret)
+               dev_warn(&connection->dev,
+                       "Failed to disconnect CPort-%d (%d)\n", cport_id, ret);
 }
 
-/*
- * XXX Protocols should have a set of function pointers:
- *     ->init (called here, to initialize the device)
- *     ->input_handler
- *     ->exit (reverse of init)
- */
-int gb_connection_init(struct gb_connection *connection)
+static int gb_connection_init(struct gb_connection *connection)
 {
+       int cport_id = connection->intf_cport_id;
        int ret;
 
+       /*
+        * 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);
+                       return ret;
+               }
+       }
+
        /* Need to enable the connection to initialize it */
+       spin_lock_irq(&connection->lock);
        connection->state = GB_CONNECTION_STATE_ENABLED;
-       switch (connection->protocol->id) {
-       case GREYBUS_PROTOCOL_I2C:
-               connection->handler = &gb_i2c_connection_handler;
-               break;
-       case GREYBUS_PROTOCOL_GPIO:
-               connection->handler = &gb_gpio_connection_handler;
-               break;
-       case GREYBUS_PROTOCOL_BATTERY:
-               connection->handler = &gb_battery_connection_handler;
-               break;
-       case GREYBUS_PROTOCOL_UART:
-               connection->handler = &gb_uart_connection_handler;
-               break;
-       case GREYBUS_PROTOCOL_CONTROL:
-       case GREYBUS_PROTOCOL_AP:
-       case GREYBUS_PROTOCOL_HID:
-       case GREYBUS_PROTOCOL_LED:
-       case GREYBUS_PROTOCOL_VENDOR:
-       default:
-               gb_connection_err(connection, "unimplemented protocol %hhu",
-                       connection->protocol->id);
-               ret = -ENXIO;
-               break;
+       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 = connection->handler->connection_init(connection);
+       ret = connection->protocol->connection_init(connection);
+       if (!ret)
+               return 0;
 
-       if (ret)
-               connection->state = GB_CONNECTION_STATE_ERROR;
+disconnect:
+       spin_lock_irq(&connection->lock);
+       connection->state = GB_CONNECTION_STATE_ERROR;
+       spin_unlock_irq(&connection->lock);
 
+       gb_connection_disconnected(connection);
        return ret;
 }
 
-void gb_connection_exit(struct gb_connection *connection)
+static void gb_connection_exit(struct gb_connection *connection)
 {
-       if (!connection->handler) {
-               gb_connection_err(connection, "uninitialized connection");
+       if (!connection->protocol)
+               return;
+
+       spin_lock_irq(&connection->lock);
+       if (connection->state != GB_CONNECTION_STATE_ENABLED) {
+               spin_unlock_irq(&connection->lock);
                return;
        }
        connection->state = GB_CONNECTION_STATE_DESTROYING;
-       connection->handler->connection_exit(connection);
+       spin_unlock_irq(&connection->lock);
+
+       gb_connection_cancel_operations(connection, -ESHUTDOWN);
+
+       connection->protocol->connection_exit(connection);
+       gb_connection_disconnected(connection);
+}
+
+/*
+ * Tear down a previously set up connection.
+ */
+void gb_connection_destroy(struct gb_connection *connection)
+{
+       struct ida *id_map;
+
+       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;
+
+       device_unregister(&connection->dev);
+}
+
+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)
+{
+       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)
+               return 0;
+       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 &&
+            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;
+               }
+       }
+
+       return 0;
 }