greybus: es2: always set result value
[cascardo/linux.git] / drivers / staging / greybus / es2.c
index d6abfda..c31124d 100644 (file)
 #include <linux/usb.h>
 #include <linux/kfifo.h>
 #include <linux/debugfs.h>
+#include <linux/list.h>
 #include <asm/unaligned.h>
 
 #include "greybus.h"
+#include "greybus_trace.h"
 #include "kernel_ver.h"
 #include "connection.h"
-#include "greybus_trace.h"
-
 
 /* Fixed CPort numbers */
 #define ES2_CPORT_CDSI0                16
 /* Memory sizes for the buffers sent to/from the ES2 controller */
 #define ES2_GBUF_MSG_SIZE_MAX  2048
 
+/* Memory sizes for the ARPC buffers */
+#define ARPC_OUT_SIZE_MAX      U16_MAX
+#define ARPC_IN_SIZE_MAX       128
+
 static const struct usb_device_id id_table[] = {
        { USB_DEVICE(0x18d1, 0x1eaf) },
        { },
@@ -37,6 +41,12 @@ MODULE_DEVICE_TABLE(usb, id_table);
 /* Number of bulk in and bulk out couple */
 #define NUM_BULKS              7
 
+/* Expected number of bulk out endpoints */
+#define NUM_BULKS_OUT          NUM_BULKS
+
+/* Expected number of bulk in endpoints (including ARPC endpoint) */
+#define NUM_BULKS_IN           (NUM_BULKS + 1)
+
 /*
  * Number of CPort IN urbs in flight at any point in time.
  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
@@ -49,6 +59,11 @@ MODULE_DEVICE_TABLE(usb, id_table);
  */
 #define NUM_CPORT_OUT_URB      (8 * NUM_BULKS)
 
+/*
+ * Number of ARPC in urbs in flight at any point in time.
+ */
+#define NUM_ARPC_IN_URB                2
+
 /*
  * @endpoint: bulk in endpoint for CPort data
  * @urb: array of urbs for the CPort in messages
@@ -86,6 +101,12 @@ struct es2_cport_out {
  * @apb_log_dentry: file system entry for the log file interface
  * @apb_log_enable_dentry: file system entry for enabling logging
  * @apb_log_fifo: kernel FIFO to carry logged data
+ * @arpc_urb: array of urbs for the ARPC in messages
+ * @arpc_buffer: array of buffers for the @arpc_urb urbs
+ * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
+ * @arpc_id_cycle: gives an unique id to ARPC
+ * @arpc_lock: locks ARPC list
+ * @arpcs: list of in progress ARPCs
  */
 struct es2_ap_dev {
        struct usb_device *usb_dev;
@@ -107,6 +128,14 @@ struct es2_ap_dev {
        struct dentry *apb_log_dentry;
        struct dentry *apb_log_enable_dentry;
        DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
+
+       __u8 arpc_endpoint_in;
+       struct urb *arpc_urb[NUM_ARPC_IN_URB];
+       u8 *arpc_buffer[NUM_ARPC_IN_URB];
+
+       int arpc_id_cycle;
+       spinlock_t arpc_lock;
+       struct list_head arpcs;
 };
 
 /**
@@ -144,6 +173,14 @@ struct timesync_authoritative_request {
        __le64  frame_time[GB_TIMESYNC_MAX_STROBES];
 } __packed;
 
+struct arpc {
+       struct list_head list;
+       struct arpc_request_message *req;
+       struct arpc_response_message *resp;
+       struct completion response_received;
+       bool active;
+};
+
 static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
 {
        return (struct es2_ap_dev *)&hd->hd_priv;
@@ -152,6 +189,8 @@ static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
 static void cport_out_callback(struct urb *urb);
 static void usb_log_enable(struct es2_ap_dev *es2);
 static void usb_log_disable(struct es2_ap_dev *es2);
+static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
+                    size_t size, int *result, unsigned int timeout);
 
 /* Get the endpoints pair mapped to the cport */
 static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id)
@@ -345,6 +384,45 @@ static void es2_cport_in_disable(struct es2_ap_dev *es2,
        }
 }
 
+static int es2_arpc_in_enable(struct es2_ap_dev *es2)
+{
+       struct urb *urb;
+       int ret;
+       int i;
+
+       for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
+               urb = es2->arpc_urb[i];
+
+               ret = usb_submit_urb(urb, GFP_KERNEL);
+               if (ret) {
+                       dev_err(&es2->usb_dev->dev,
+                               "failed to submit arpc in-urb: %d\n", ret);
+                       goto err_kill_urbs;
+               }
+       }
+
+       return 0;
+
+err_kill_urbs:
+       for (--i; i >= 0; --i) {
+               urb = es2->arpc_urb[i];
+               usb_kill_urb(urb);
+       }
+
+       return ret;
+}
+
+static void es2_arpc_in_disable(struct es2_ap_dev *es2)
+{
+       struct urb *urb;
+       int i;
+
+       for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
+               urb = es2->arpc_urb[i];
+               usb_kill_urb(urb);
+       }
+}
+
 static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
 {
        struct urb *urb = NULL;
@@ -469,7 +547,9 @@ static int message_send(struct gb_host_device *hd, u16 cport_id,
                          message->buffer, buffer_size,
                          cport_out_callback, message);
        urb->transfer_flags |= URB_ZERO_PACKET;
-       trace_gb_host_device_send(hd, cport_id, buffer_size);
+
+       trace_gb_message_submit(message);
+
        retval = usb_submit_urb(urb, gfp_mask);
        if (retval) {
                dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
@@ -529,7 +609,9 @@ static int cport_reset(struct gb_host_device *hd, u16 cport_id)
 {
        struct es2_ap_dev *es2 = hd_to_es2(hd);
        struct usb_device *udev = es2->usb_dev;
+       struct arpc_cport_reset_req req;
        int retval;
+       int result;
 
        switch (cport_id) {
        case GB_SVC_CPORT_ID:
@@ -538,18 +620,15 @@ static int cport_reset(struct gb_host_device *hd, u16 cport_id)
                return 0;
        }
 
-       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
-                                GB_APB_REQUEST_RESET_CPORT,
-                                USB_DIR_OUT | USB_TYPE_VENDOR |
-                                USB_RECIP_INTERFACE, cport_id, 0,
-                                NULL, 0, ES2_TIMEOUT);
-       if (retval < 0) {
+       req.cport_id = cpu_to_le16(cport_id);
+       retval = arpc_sync(es2, ARPC_TYPE_CPORT_RESET, &req, sizeof(req),
+                          &result, ES2_TIMEOUT);
+       if (retval == -EREMOTEIO) {
                dev_err(&udev->dev, "failed to reset cport %u: %d\n", cport_id,
-                       retval);
-               return retval;
+                       result);
        }
 
-       return 0;
+       return retval;
 }
 
 static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
@@ -605,7 +684,52 @@ static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
        ida_simple_remove(&hd->cport_id_map, cport_id);
 }
 
-static int cport_enable(struct gb_host_device *hd, u16 cport_id)
+static int cport_enable(struct gb_host_device *hd, u16 cport_id,
+                       unsigned long flags)
+{
+       struct es2_ap_dev *es2 = hd_to_es2(hd);
+       struct usb_device *udev = es2->usb_dev;
+       struct gb_apb_request_cport_flags *req;
+       u32 connection_flags;
+       int ret;
+
+       req = kzalloc(sizeof(*req), GFP_KERNEL);
+       if (!req)
+               return -ENOMEM;
+
+       connection_flags = 0;
+       if (flags & GB_CONNECTION_FLAG_CONTROL)
+               connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
+       if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
+               connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
+
+       req->flags = cpu_to_le32(connection_flags);
+
+       dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
+                       cport_id, connection_flags);
+
+       ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                               GB_APB_REQUEST_CPORT_FLAGS,
+                               USB_DIR_OUT | USB_TYPE_VENDOR |
+                               USB_RECIP_INTERFACE, cport_id, 0,
+                               req, sizeof(*req), ES2_TIMEOUT);
+       if (ret != sizeof(*req)) {
+               dev_err(&udev->dev, "failed to set cport flags for port %d\n",
+                               cport_id);
+               if (ret >= 0)
+                       ret = -EIO;
+
+               goto out;
+       }
+
+       ret = 0;
+out:
+       kfree(req);
+
+       return ret;
+}
+
+static int cport_disable(struct gb_host_device *hd, u16 cport_id)
 {
        int retval;
 
@@ -622,11 +746,6 @@ static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
        struct es2_ap_dev *es2 = hd_to_es2(hd);
        struct usb_device *udev = es2->usb_dev;
 
-       if (!cport_id_valid(hd, cport_id)) {
-               dev_err(&udev->dev, "invalid cport %u\n", cport_id);
-               return -EINVAL;
-       }
-
        retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
                                 GB_APB_REQUEST_LATENCY_TAG_EN,
                                 USB_DIR_OUT | USB_TYPE_VENDOR |
@@ -645,11 +764,6 @@ static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
        struct es2_ap_dev *es2 = hd_to_es2(hd);
        struct usb_device *udev = es2->usb_dev;
 
-       if (!cport_id_valid(hd, cport_id)) {
-               dev_err(&udev->dev, "invalid cport %u\n", cport_id);
-               return -EINVAL;
-       }
-
        retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
                                 GB_APB_REQUEST_LATENCY_TAG_DIS,
                                 USB_DIR_OUT | USB_TYPE_VENDOR |
@@ -773,7 +887,7 @@ static int timesync_get_last_event(struct gb_host_device *hd, u64 *frame_time)
        int retval;
        struct es2_ap_dev *es2 = hd_to_es2(hd);
        struct usb_device *udev = es2->usb_dev;
-       u64 *response_frame_time;
+       __le64 *response_frame_time;
 
        response_frame_time = kzalloc(sizeof(*response_frame_time), GFP_KERNEL);
        if (!response_frame_time)
@@ -808,6 +922,7 @@ static struct gb_hd_driver es2_driver = {
        .cport_allocate                 = es2_cport_allocate,
        .cport_release                  = es2_cport_release,
        .cport_enable                   = cport_enable,
+       .cport_disable                  = cport_disable,
        .latency_tag_enable             = latency_tag_enable,
        .latency_tag_disable            = latency_tag_disable,
        .output                         = output,
@@ -866,6 +981,16 @@ static void es2_destroy(struct es2_ap_dev *es2)
                es2->cport_out_urb_busy[i] = false;     /* just to be anal */
        }
 
+       for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
+               struct urb *urb = es2->arpc_urb[i];
+
+               if (!urb)
+                       break;
+               usb_free_urb(urb);
+               kfree(es2->arpc_buffer[i]);
+               es2->arpc_buffer[i] = NULL;
+       }
+
        for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) {
                struct es2_cport_in *cport_in = &es2->cport_in[bulk_in];
 
@@ -882,6 +1007,10 @@ static void es2_destroy(struct es2_ap_dev *es2)
 
        kfree(es2->cport_to_ep);
 
+       /* release reserved CDSI0 and CDSI1 cports */
+       gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
+       gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
+
        udev = es2->usb_dev;
        gb_hd_put(es2->hd);
 
@@ -919,7 +1048,6 @@ static void cport_in_callback(struct urb *urb)
        cport_id = gb_message_cport_unpack(header);
 
        if (cport_id_valid(hd, cport_id)) {
-               trace_gb_host_device_recv(hd, cport_id, urb->actual_length);
                greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
                                                        urb->actual_length);
        } else {
@@ -955,6 +1083,200 @@ static void cport_out_callback(struct urb *urb)
        free_urb(es2, urb);
 }
 
+static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
+{
+       struct arpc *rpc;
+
+       if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
+               return NULL;
+
+       rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
+       if (!rpc)
+               return NULL;
+
+       INIT_LIST_HEAD(&rpc->list);
+       rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
+       if (!rpc->req)
+               goto err_free_rpc;
+
+       rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
+       if (!rpc->resp)
+               goto err_free_req;
+
+       rpc->req->type = type;
+       rpc->req->size = cpu_to_le16(sizeof(rpc->req) + size);
+       memcpy(rpc->req->data, payload, size);
+
+       init_completion(&rpc->response_received);
+
+       return rpc;
+
+err_free_req:
+       kfree(rpc->req);
+err_free_rpc:
+       kfree(rpc);
+
+       return NULL;
+}
+
+static void arpc_free(struct arpc *rpc)
+{
+       kfree(rpc->req);
+       kfree(rpc->resp);
+       kfree(rpc);
+}
+
+static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
+{
+       struct arpc *rpc;
+
+       list_for_each_entry(rpc, &es2->arpcs, list) {
+               if (rpc->req->id == id)
+                       return rpc;
+       }
+
+       return NULL;
+}
+
+static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
+{
+       rpc->active = true;
+       rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
+       list_add_tail(&rpc->list, &es2->arpcs);
+}
+
+static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
+{
+       if (rpc->active) {
+               rpc->active = false;
+               list_del(&rpc->list);
+       }
+}
+
+static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
+{
+       struct usb_device *udev = es2->usb_dev;
+       int retval;
+
+       retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
+                                APBA_REQUEST_ARPC_RUN,
+                                USB_DIR_OUT | USB_TYPE_VENDOR |
+                                USB_RECIP_INTERFACE,
+                                0, 0,
+                                rpc->req, le16_to_cpu(rpc->req->size),
+                                ES2_TIMEOUT);
+       if (retval != le16_to_cpu(rpc->req->size)) {
+               dev_err(&udev->dev,
+                       "failed to send ARPC request %d: %d\n",
+                       rpc->req->type, retval);
+               if (retval > 0)
+                       retval = -EIO;
+               return retval;
+       }
+
+       return 0;
+}
+
+static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
+                    size_t size, int *result, unsigned int timeout)
+{
+       struct arpc *rpc;
+       unsigned long flags;
+       int retval;
+
+       *result = 0;
+
+       rpc = arpc_alloc(payload, size, type);
+       if (!rpc)
+               return -ENOMEM;
+
+       spin_lock_irqsave(&es2->arpc_lock, flags);
+       arpc_add(es2, rpc);
+       spin_unlock_irqrestore(&es2->arpc_lock, flags);
+
+       retval = arpc_send(es2, rpc, timeout);
+       if (retval)
+               goto out_arpc_del;
+
+       retval = wait_for_completion_interruptible_timeout(
+                                               &rpc->response_received,
+                                               msecs_to_jiffies(timeout));
+       if (retval <= 0) {
+               if (!retval)
+                       retval = -ETIMEDOUT;
+               goto out_arpc_del;
+       }
+
+       if (rpc->resp->result) {
+               retval = -EREMOTEIO;
+               *result = rpc->resp->result;
+       } else {
+               retval = 0;
+       }
+
+out_arpc_del:
+       spin_lock_irqsave(&es2->arpc_lock, flags);
+       arpc_del(es2, rpc);
+       spin_unlock_irqrestore(&es2->arpc_lock, flags);
+       arpc_free(rpc);
+
+       if (retval < 0 && retval != -EREMOTEIO) {
+               dev_err(&es2->usb_dev->dev,
+                       "failed to execute ARPC: %d\n", retval);
+       }
+
+       return retval;
+}
+
+static void arpc_in_callback(struct urb *urb)
+{
+       struct es2_ap_dev *es2 = urb->context;
+       struct device *dev = &urb->dev->dev;
+       int status = check_urb_status(urb);
+       struct arpc *rpc;
+       struct arpc_response_message *resp;
+       unsigned long flags;
+       int retval;
+
+       if (status) {
+               if ((status == -EAGAIN) || (status == -EPROTO))
+                       goto exit;
+
+               /* The urb is being unlinked */
+               if (status == -ENOENT || status == -ESHUTDOWN)
+                       return;
+
+               dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
+               return;
+       }
+
+       if (urb->actual_length < sizeof(*resp)) {
+               dev_err(dev, "short aprc response received\n");
+               goto exit;
+       }
+
+       resp = urb->transfer_buffer;
+       spin_lock_irqsave(&es2->arpc_lock, flags);
+       rpc = arpc_find(es2, resp->id);
+       if (!rpc) {
+               dev_err(dev, "invalid arpc response id received: %u\n",
+                       le16_to_cpu(resp->id));
+               spin_unlock_irqrestore(&es2->arpc_lock, flags);
+               goto exit;
+       }
+
+       arpc_del(es2, rpc);
+       memcpy(rpc->resp, resp, sizeof(*resp));
+       complete(&rpc->response_received);
+       spin_unlock_irqrestore(&es2->arpc_lock, flags);
+
+exit:
+       /* put our urb back in the request pool */
+       retval = usb_submit_urb(urb, GFP_ATOMIC);
+       if (retval)
+               dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
+}
+
 #define APB1_LOG_MSG_SIZE      64
 static void apb_log_get(struct es2_ap_dev *es2, char *buf)
 {
@@ -1189,8 +1511,13 @@ static int ap_probe(struct usb_interface *interface,
                endpoint = &iface_desc->endpoint[i].desc;
 
                if (usb_endpoint_is_bulk_in(endpoint)) {
-                       es2->cport_in[bulk_in++].endpoint =
-                               endpoint->bEndpointAddress;
+                       if (bulk_in < NUM_BULKS)
+                               es2->cport_in[bulk_in].endpoint =
+                                       endpoint->bEndpointAddress;
+                       else
+                               es2->arpc_endpoint_in =
+                                       endpoint->bEndpointAddress;
+                       bulk_in++;
                } else if (usb_endpoint_is_bulk_out(endpoint)) {
                        es2->cport_out[bulk_out++].endpoint =
                                endpoint->bEndpointAddress;
@@ -1200,7 +1527,7 @@ static int ap_probe(struct usb_interface *interface,
                                endpoint->bEndpointAddress);
                }
        }
-       if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) {
+       if (bulk_in != NUM_BULKS_IN || bulk_out != NUM_BULKS_OUT) {
                dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
                retval = -ENODEV;
                goto error;
@@ -1235,6 +1562,32 @@ static int ap_probe(struct usb_interface *interface,
                }
        }
 
+       /* Allocate buffers for ARPC in messages */
+       for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
+               struct urb *urb;
+               u8 *buffer;
+
+               urb = usb_alloc_urb(0, GFP_KERNEL);
+               if (!urb) {
+                       retval = -ENOMEM;
+                       goto error;
+               }
+               buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
+               if (!buffer) {
+                       retval = -ENOMEM;
+                       goto error;
+               }
+
+               usb_fill_bulk_urb(urb, udev,
+                                 usb_rcvbulkpipe(udev,
+                                                 es2->arpc_endpoint_in),
+                                 buffer, ARPC_IN_SIZE_MAX,
+                                 arpc_in_callback, es2);
+
+               es2->arpc_urb[i] = urb;
+               es2->arpc_buffer[i] = buffer;
+       }
+
        /* Allocate urbs for our CPort OUT messages */
        for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
                struct urb *urb;
@@ -1255,9 +1608,15 @@ static int ap_probe(struct usb_interface *interface,
                                                        gb_debugfs_get(), es2,
                                                        &apb_log_enable_fops);
 
+       INIT_LIST_HEAD(&es2->arpcs);
+       spin_lock_init(&es2->arpc_lock);
+
+       if (es2_arpc_in_enable(es2))
+               goto error;
+
        retval = gb_hd_add(hd);
        if (retval)
-               goto error;
+               goto err_disable_arpc_in;
 
        for (i = 0; i < NUM_BULKS; ++i) {
                retval = es2_cport_in_enable(es2, &es2->cport_in[i]);
@@ -1271,6 +1630,8 @@ err_disable_cport_in:
        for (--i; i >= 0; --i)
                es2_cport_in_disable(es2, &es2->cport_in[i]);
        gb_hd_del(hd);
+err_disable_arpc_in:
+       es2_arpc_in_disable(es2);
 error:
        es2_destroy(es2);
 
@@ -1286,6 +1647,7 @@ static void ap_disconnect(struct usb_interface *interface)
 
        for (i = 0; i < NUM_BULKS; ++i)
                es2_cport_in_disable(es2, &es2->cport_in[i]);
+       es2_arpc_in_disable(es2);
 
        es2_destroy(es2);
 }