greybus: replace Ara references
[cascardo/linux.git] / drivers / staging / greybus / bootrom.c
index 3cbe9fe..5f90721 100644 (file)
@@ -8,24 +8,95 @@
  */
 
 #include <linux/firmware.h>
+#include <linux/jiffies.h>
+#include <linux/mutex.h>
+#include <linux/workqueue.h>
 
-#include "bootrom.h"
 #include "greybus.h"
+#include "firmware.h"
 
+/* Timeout, in jiffies, within which the next request must be received */
+#define NEXT_REQ_TIMEOUT_MS    1000
+
+/*
+ * FIXME: Reduce this timeout once svc core handles parallel processing of
+ * events from the SVC, which are handled sequentially today.
+ */
+#define MODE_SWITCH_TIMEOUT_MS 10000
+
+enum next_request_type {
+       NEXT_REQ_FIRMWARE_SIZE,
+       NEXT_REQ_GET_FIRMWARE,
+       NEXT_REQ_READY_TO_BOOT,
+       NEXT_REQ_MODE_SWITCH,
+};
 
 struct gb_bootrom {
        struct gb_connection    *connection;
        const struct firmware   *fw;
        u8                      protocol_major;
        u8                      protocol_minor;
+       enum next_request_type  next_request;
+       struct delayed_work     dwork;
+       struct mutex            mutex; /* Protects bootrom->fw */
 };
 
 static void free_firmware(struct gb_bootrom *bootrom)
 {
+       if (!bootrom->fw)
+               return;
+
        release_firmware(bootrom->fw);
        bootrom->fw = NULL;
 }
 
+static void gb_bootrom_timedout(struct work_struct *work)
+{
+       struct delayed_work *dwork = to_delayed_work(work);
+       struct gb_bootrom *bootrom = container_of(dwork, struct gb_bootrom, dwork);
+       struct device *dev = &bootrom->connection->bundle->dev;
+       const char *reason;
+
+       switch (bootrom->next_request) {
+       case NEXT_REQ_FIRMWARE_SIZE:
+               reason = "Firmware Size Request";
+               break;
+       case NEXT_REQ_GET_FIRMWARE:
+               reason = "Get Firmware Request";
+               break;
+       case NEXT_REQ_READY_TO_BOOT:
+               reason = "Ready to Boot Request";
+               break;
+       case NEXT_REQ_MODE_SWITCH:
+               reason = "Interface Mode Switch";
+               break;
+       default:
+               reason = NULL;
+               dev_err(dev, "Invalid next-request: %u", bootrom->next_request);
+               break;
+       }
+
+       dev_err(dev, "Timed out waiting for %s from the Module\n", reason);
+
+       mutex_lock(&bootrom->mutex);
+       free_firmware(bootrom);
+       mutex_unlock(&bootrom->mutex);
+
+       /* TODO: Power-off Module ? */
+}
+
+static void gb_bootrom_set_timeout(struct gb_bootrom *bootrom,
+                       enum next_request_type next, unsigned long timeout)
+{
+       bootrom->next_request = next;
+       schedule_delayed_work(&bootrom->dwork, msecs_to_jiffies(timeout));
+}
+
+static void gb_bootrom_cancel_timeout(struct gb_bootrom *bootrom)
+{
+       cancel_delayed_work_sync(&bootrom->dwork);
+}
+
 /*
  * The es2 chip doesn't have VID/PID programmed into the hardware and we need to
  * hack that up to distinguish different modules and their firmware blobs.
@@ -43,7 +114,7 @@ static void bootrom_es2_fixup_vid_pid(struct gb_bootrom *bootrom)
        struct gb_interface *intf = connection->bundle->intf;
        int ret;
 
-       if (!(intf->quirks & GB_INTERFACE_QUIRK_NO_ARA_IDS))
+       if (!(intf->quirks & GB_INTERFACE_QUIRK_NO_GMP_IDS))
                return;
 
        ret = gb_operation_sync(connection, GB_BOOTROM_TYPE_GET_VID_PID,
@@ -69,16 +140,22 @@ static void bootrom_es2_fixup_vid_pid(struct gb_bootrom *bootrom)
 }
 
 /* This returns path of the firmware blob on the disk */
-static int download_firmware(struct gb_bootrom *bootrom, u8 stage)
+static int find_firmware(struct gb_bootrom *bootrom, u8 stage)
 {
        struct gb_connection *connection = bootrom->connection;
        struct gb_interface *intf = connection->bundle->intf;
-       char firmware_name[48];
+       char firmware_name[49];
        int rc;
 
        /* Already have a firmware, free it */
-       if (bootrom->fw)
-               free_firmware(bootrom);
+       free_firmware(bootrom);
+
+       /* Bootrom protocol is only supported for loading Stage 2 firmware */
+       if (stage != 2) {
+               dev_err(&connection->bundle->dev, "Invalid boot stage: %u\n",
+                       stage);
+               return -EINVAL;
+       }
 
        /*
         * Create firmware name
@@ -86,9 +163,9 @@ static int download_firmware(struct gb_bootrom *bootrom, u8 stage)
         * XXX Name it properly..
         */
        snprintf(firmware_name, sizeof(firmware_name),
-                "ara_%08x_%08x_%08x_%08x_%02x.tftf",
+                FW_NAME_PREFIX "%08x_%08x_%08x_%08x_s2l.tftf",
                 intf->ddbl1_manufacturer_id, intf->ddbl1_product_id,
-                intf->vendor_id, intf->product_id, stage);
+                intf->vendor_id, intf->product_id);
 
        // FIXME:
        // Turn to dev_dbg later after everyone has valid bootloaders with good
@@ -99,10 +176,11 @@ static int download_firmware(struct gb_bootrom *bootrom, u8 stage)
 
        rc = request_firmware(&bootrom->fw, firmware_name,
                &connection->bundle->dev);
-       if (rc)
+       if (rc) {
                dev_err(&connection->bundle->dev,
-                       "Firware request for %s has failed : %d",
-                       firmware_name, rc);
+                       "failed to find %s firmware (%d)\n", firmware_name, rc);
+       }
+
        return rc;
 }
 
@@ -114,25 +192,29 @@ static int gb_bootrom_firmware_size_request(struct gb_operation *op)
        struct device *dev = &op->connection->bundle->dev;
        int ret;
 
+       /* Disable timeouts */
+       gb_bootrom_cancel_timeout(bootrom);
+
        if (op->request->payload_size != sizeof(*size_request)) {
                dev_err(dev, "%s: illegal size of firmware size request (%zu != %zu)\n",
                        __func__, op->request->payload_size,
                        sizeof(*size_request));
-               return -EINVAL;
+               ret = -EINVAL;
+               goto queue_work;
        }
 
-       ret = download_firmware(bootrom, size_request->stage);
-       if (ret) {
-               dev_err(dev, "%s: failed to download firmware (%d)\n", __func__,
-                       ret);
-               return ret;
-       }
+       mutex_lock(&bootrom->mutex);
+
+       ret = find_firmware(bootrom, size_request->stage);
+       if (ret)
+               goto unlock;
 
        if (!gb_operation_response_alloc(op, sizeof(*size_response),
                                         GFP_KERNEL)) {
                dev_err(dev, "%s: error allocating response\n", __func__);
                free_firmware(bootrom);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto unlock;
        }
 
        size_response = op->response->payload;
@@ -140,28 +222,48 @@ static int gb_bootrom_firmware_size_request(struct gb_operation *op)
 
        dev_dbg(dev, "%s: firmware size %d bytes\n", __func__, size_response->size);
 
-       return 0;
+unlock:
+       mutex_unlock(&bootrom->mutex);
+
+queue_work:
+       if (!ret) {
+               /* Refresh timeout */
+               gb_bootrom_set_timeout(bootrom, NEXT_REQ_GET_FIRMWARE,
+                                      NEXT_REQ_TIMEOUT_MS);
+       }
+
+       return ret;
 }
 
 static int gb_bootrom_get_firmware(struct gb_operation *op)
 {
        struct gb_bootrom *bootrom = gb_connection_get_data(op->connection);
-       const struct firmware *fw = bootrom->fw;
+       const struct firmware *fw;
        struct gb_bootrom_get_firmware_request *firmware_request;
        struct gb_bootrom_get_firmware_response *firmware_response;
        struct device *dev = &op->connection->bundle->dev;
        unsigned int offset, size;
+       enum next_request_type next_request;
+       int ret = 0;
+
+       /* Disable timeouts */
+       gb_bootrom_cancel_timeout(bootrom);
 
        if (op->request->payload_size != sizeof(*firmware_request)) {
                dev_err(dev, "%s: Illegal size of get firmware request (%zu %zu)\n",
                        __func__, op->request->payload_size,
                        sizeof(*firmware_request));
-               return -EINVAL;
+               ret = -EINVAL;
+               goto queue_work;
        }
 
+       mutex_lock(&bootrom->mutex);
+
+       fw = bootrom->fw;
        if (!fw) {
                dev_err(dev, "%s: firmware not available\n", __func__);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto unlock;
        }
 
        firmware_request = op->request->payload;
@@ -171,13 +273,15 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
        if (offset >= fw->size || size > fw->size - offset) {
                dev_warn(dev, "bad firmware request (offs = %u, size = %u)\n",
                                offset, size);
-               return -EINVAL;
+               ret = -EINVAL;
+               goto unlock;
        }
 
        if (!gb_operation_response_alloc(op, sizeof(*firmware_response) + size,
                                         GFP_KERNEL)) {
                dev_err(dev, "%s: error allocating response\n", __func__);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto unlock;
        }
 
        firmware_response = op->response->payload;
@@ -186,36 +290,65 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
        dev_dbg(dev, "responding with firmware (offs = %u, size = %u)\n", offset,
                size);
 
-       return 0;
+unlock:
+       mutex_unlock(&bootrom->mutex);
+
+queue_work:
+       /* Refresh timeout */
+       if (!ret && (offset + size == fw->size))
+               next_request = NEXT_REQ_READY_TO_BOOT;
+       else
+               next_request = NEXT_REQ_GET_FIRMWARE;
+
+       gb_bootrom_set_timeout(bootrom, next_request, NEXT_REQ_TIMEOUT_MS);
+
+       return ret;
 }
 
 static int gb_bootrom_ready_to_boot(struct gb_operation *op)
 {
        struct gb_connection *connection = op->connection;
+       struct gb_bootrom *bootrom = gb_connection_get_data(connection);
        struct gb_bootrom_ready_to_boot_request *rtb_request;
        struct device *dev = &connection->bundle->dev;
        u8 status;
+       int ret = 0;
+
+       /* Disable timeouts */
+       gb_bootrom_cancel_timeout(bootrom);
 
        if (op->request->payload_size != sizeof(*rtb_request)) {
                dev_err(dev, "%s: Illegal size of ready to boot request (%zu %zu)\n",
                        __func__, op->request->payload_size,
                        sizeof(*rtb_request));
-               return -EINVAL;
+               ret = -EINVAL;
+               goto queue_work;
        }
 
        rtb_request = op->request->payload;
        status = rtb_request->status;
 
        /* Return error if the blob was invalid */
-       if (status == GB_BOOTROM_BOOT_STATUS_INVALID)
-               return -EINVAL;
+       if (status == GB_BOOTROM_BOOT_STATUS_INVALID) {
+               ret = -EINVAL;
+               goto queue_work;
+       }
 
        /*
         * XXX Should we return error for insecure firmware?
         */
        dev_dbg(dev, "ready to boot: 0x%x, 0\n", status);
 
-       return 0;
+queue_work:
+       /*
+        * Refresh timeout, the Interface shall load the new personality and
+        * send a new hotplug request, which shall get rid of the bootrom
+        * connection. As that can take some time, increase the timeout a bit.
+        */
+       gb_bootrom_set_timeout(bootrom, NEXT_REQ_MODE_SWITCH,
+                              MODE_SWITCH_TIMEOUT_MS);
+
+       return ret;
 }
 
 static int gb_bootrom_request_handler(struct gb_operation *op)
@@ -304,6 +437,8 @@ static int gb_bootrom_probe(struct gb_bundle *bundle,
 
        bootrom->connection = connection;
 
+       mutex_init(&bootrom->mutex);
+       INIT_DELAYED_WORK(&bootrom->dwork, gb_bootrom_timedout);
        greybus_set_drvdata(bundle, bootrom);
 
        ret = gb_connection_enable_tx(connection);
@@ -320,19 +455,25 @@ static int gb_bootrom_probe(struct gb_bundle *bundle,
        if (ret)
                goto err_connection_disable;
 
+       /* Refresh timeout */
+       gb_bootrom_set_timeout(bootrom, NEXT_REQ_FIRMWARE_SIZE,
+                              NEXT_REQ_TIMEOUT_MS);
+
        /* Tell bootrom we're ready. */
        ret = gb_operation_sync(connection, GB_BOOTROM_TYPE_AP_READY, NULL, 0,
                                NULL, 0);
        if (ret) {
                dev_err(&connection->bundle->dev,
                                "failed to send AP READY: %d\n", ret);
-               goto err_connection_disable;
+               goto err_cancel_timeout;
        }
 
        dev_dbg(&bundle->dev, "AP_READY sent\n");
 
        return 0;
 
+err_cancel_timeout:
+       gb_bootrom_cancel_timeout(bootrom);
 err_connection_disable:
        gb_connection_disable(connection);
 err_connection_destroy:
@@ -351,9 +492,16 @@ static void gb_bootrom_disconnect(struct gb_bundle *bundle)
 
        gb_connection_disable(bootrom->connection);
 
-       /* Release firmware */
-       if (bootrom->fw)
-               free_firmware(bootrom);
+       /* Disable timeouts */
+       gb_bootrom_cancel_timeout(bootrom);
+
+       /*
+        * Release firmware:
+        *
+        * As the connection and the delayed work are already disabled, we don't
+        * need to lock access to bootrom->fw here.
+        */
+       free_firmware(bootrom);
 
        gb_connection_destroy(bootrom->connection);
        kfree(bootrom);
@@ -371,12 +519,6 @@ static struct greybus_driver gb_bootrom_driver = {
        .id_table       = gb_bootrom_id_table,
 };
 
-int gb_bootrom_init(void)
-{
-       return greybus_register(&gb_bootrom_driver);
-}
+module_greybus_driver(gb_bootrom_driver);
 
-void gb_bootrom_exit(void)
-{
-       greybus_deregister(&gb_bootrom_driver);
-}
+MODULE_LICENSE("GPL v2");