Merge branch 'modsplit-Oct31_2011' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / char / virtio_console.c
index 9704431..8e3c46d 100644 (file)
  */
 #include <linux/cdev.h>
 #include <linux/debugfs.h>
+#include <linux/completion.h>
 #include <linux/device.h>
 #include <linux/err.h>
+#include <linux/freezer.h>
 #include <linux/fs.h>
 #include <linux/init.h>
 #include <linux/list.h>
@@ -74,6 +76,7 @@ struct ports_driver_data {
 static struct ports_driver_data pdrvdata;
 
 DEFINE_SPINLOCK(pdrvdata_lock);
+DECLARE_COMPLETION(early_console_added);
 
 /* This struct holds information that's relevant only for console ports */
 struct console {
@@ -152,6 +155,10 @@ struct ports_device {
        int chr_major;
 };
 
+struct port_stats {
+       unsigned long bytes_sent, bytes_received, bytes_discarded;
+};
+
 /* This struct holds the per-port data */
 struct port {
        /* Next port in the list, head is in the ports_device */
@@ -179,6 +186,13 @@ struct port {
        /* File in the debugfs directory that exposes this port's information */
        struct dentry *debugfs_file;
 
+       /*
+        * Keep count of the bytes sent, received and discarded for
+        * this port for accounting and debugging purposes.  These
+        * counts are not reset across port open / close events.
+        */
+       struct port_stats stats;
+
        /*
         * The entries in this struct will be valid if this port is
         * hooked up to an hvc console
@@ -348,17 +362,19 @@ fail:
 }
 
 /* Callers should take appropriate locks */
-static void *get_inbuf(struct port *port)
+static struct port_buffer *get_inbuf(struct port *port)
 {
        struct port_buffer *buf;
-       struct virtqueue *vq;
        unsigned int len;
 
-       vq = port->in_vq;
-       buf = virtqueue_get_buf(vq, &len);
+       if (port->inbuf)
+               return port->inbuf;
+
+       buf = virtqueue_get_buf(port->in_vq, &len);
        if (buf) {
                buf->len = len;
                buf->offset = 0;
+               port->stats.bytes_received += len;
        }
        return buf;
 }
@@ -385,32 +401,27 @@ static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
 static void discard_port_data(struct port *port)
 {
        struct port_buffer *buf;
-       struct virtqueue *vq;
-       unsigned int len;
-       int ret;
+       unsigned int err;
 
        if (!port->portdev) {
                /* Device has been unplugged.  vqs are already gone. */
                return;
        }
-       vq = port->in_vq;
-       if (port->inbuf)
-               buf = port->inbuf;
-       else
-               buf = virtqueue_get_buf(vq, &len);
+       buf = get_inbuf(port);
 
-       ret = 0;
+       err = 0;
        while (buf) {
-               if (add_inbuf(vq, buf) < 0) {
-                       ret++;
+               port->stats.bytes_discarded += buf->len - buf->offset;
+               if (add_inbuf(port->in_vq, buf) < 0) {
+                       err++;
                        free_buf(buf);
                }
-               buf = virtqueue_get_buf(vq, &len);
+               port->inbuf = NULL;
+               buf = get_inbuf(port);
        }
-       port->inbuf = NULL;
-       if (ret)
+       if (err)
                dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
-                        ret);
+                        err);
 }
 
 static bool port_has_data(struct port *port)
@@ -418,18 +429,12 @@ static bool port_has_data(struct port *port)
        unsigned long flags;
        bool ret;
 
+       ret = false;
        spin_lock_irqsave(&port->inbuf_lock, flags);
-       if (port->inbuf) {
-               ret = true;
-               goto out;
-       }
        port->inbuf = get_inbuf(port);
-       if (port->inbuf) {
+       if (port->inbuf)
                ret = true;
-               goto out;
-       }
-       ret = false;
-out:
+
        spin_unlock_irqrestore(&port->inbuf_lock, flags);
        return ret;
 }
@@ -530,6 +535,8 @@ static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
                cpu_relax();
 done:
        spin_unlock_irqrestore(&port->outvq_lock, flags);
+
+       port->stats.bytes_sent += in_count;
        /*
         * We're expected to return the amount of data we wrote -- all
         * of it
@@ -634,8 +641,8 @@ static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
                if (filp->f_flags & O_NONBLOCK)
                        return -EAGAIN;
 
-               ret = wait_event_interruptible(port->waitqueue,
-                                              !will_read_block(port));
+               ret = wait_event_freezable(port->waitqueue,
+                                          !will_read_block(port));
                if (ret < 0)
                        return ret;
        }
@@ -678,8 +685,8 @@ static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
                if (nonblock)
                        return -EAGAIN;
 
-               ret = wait_event_interruptible(port->waitqueue,
-                                              !will_write_block(port));
+               ret = wait_event_freezable(port->waitqueue,
+                                          !will_write_block(port));
                if (ret < 0)
                        return ret;
        }
@@ -1059,6 +1066,14 @@ static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
                               "host_connected: %d\n", port->host_connected);
        out_offset += snprintf(buf + out_offset, out_count - out_offset,
                               "outvq_full: %d\n", port->outvq_full);
+       out_offset += snprintf(buf + out_offset, out_count - out_offset,
+                              "bytes_sent: %lu\n", port->stats.bytes_sent);
+       out_offset += snprintf(buf + out_offset, out_count - out_offset,
+                              "bytes_received: %lu\n",
+                              port->stats.bytes_received);
+       out_offset += snprintf(buf + out_offset, out_count - out_offset,
+                              "bytes_discarded: %lu\n",
+                              port->stats.bytes_discarded);
        out_offset += snprintf(buf + out_offset, out_count - out_offset,
                               "is_console: %s\n",
                               is_console_port(port) ? "yes" : "no");
@@ -1144,6 +1159,7 @@ static int add_port(struct ports_device *portdev, u32 id)
        port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
 
        port->host_connected = port->guest_connected = false;
+       port->stats = (struct port_stats) { 0 };
 
        port->outvq_full = false;
 
@@ -1353,6 +1369,7 @@ static void handle_control_message(struct ports_device *portdev,
                        break;
 
                init_port_console(port);
+               complete(&early_console_added);
                /*
                 * Could remove the port here in case init fails - but
                 * have to notify the host first.
@@ -1394,6 +1411,13 @@ static void handle_control_message(struct ports_device *portdev,
                send_sigio_to_port(port);
                break;
        case VIRTIO_CONSOLE_PORT_NAME:
+               /*
+                * If we woke up after hibernation, we can get this
+                * again.  Skip it in that case.
+                */
+               if (port->name)
+                       break;
+
                /*
                 * Skip the size of the header and the cpkt to get the size
                 * of the name that was sent
@@ -1482,8 +1506,7 @@ static void in_intr(struct virtqueue *vq)
                return;
 
        spin_lock_irqsave(&port->inbuf_lock, flags);
-       if (!port->inbuf)
-               port->inbuf = get_inbuf(port);
+       port->inbuf = get_inbuf(port);
 
        /*
         * Don't queue up data when port is closed.  This condition
@@ -1564,7 +1587,7 @@ static int init_vqs(struct ports_device *portdev)
        portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
                                   GFP_KERNEL);
        if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
-                       !portdev->out_vqs) {
+           !portdev->out_vqs) {
                err = -ENOMEM;
                goto free;
        }
@@ -1649,6 +1672,10 @@ static int __devinit virtcons_probe(struct virtio_device *vdev)
        struct ports_device *portdev;
        int err;
        bool multiport;
+       bool early = early_put_chars != NULL;
+
+       /* Ensure to read early_put_chars now */
+       barrier();
 
        portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
        if (!portdev) {
@@ -1676,13 +1703,11 @@ static int __devinit virtcons_probe(struct virtio_device *vdev)
 
        multiport = false;
        portdev->config.max_nr_ports = 1;
-       if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
+       if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
+                             offsetof(struct virtio_console_config,
+                                      max_nr_ports),
+                             &portdev->config.max_nr_ports) == 0)
                multiport = true;
-               vdev->config->get(vdev, offsetof(struct virtio_console_config,
-                                                max_nr_ports),
-                                 &portdev->config.max_nr_ports,
-                                 sizeof(portdev->config.max_nr_ports));
-       }
 
        err = init_vqs(portdev);
        if (err < 0) {
@@ -1720,6 +1745,19 @@ static int __devinit virtcons_probe(struct virtio_device *vdev)
 
        __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
                           VIRTIO_CONSOLE_DEVICE_READY, 1);
+
+       /*
+        * If there was an early virtio console, assume that there are no
+        * other consoles. We need to wait until the hvc_alloc matches the
+        * hvc_instantiate, otherwise tty_open will complain, resulting in
+        * a "Warning: unable to open an initial console" boot failure.
+        * Without multiport this is done in add_port above. With multiport
+        * this might take some host<->guest communication - thus we have to
+        * wait.
+        */
+       if (multiport && early)
+               wait_for_completion(&early_console_added);
+
        return 0;
 
 free_vqs: