/* * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation * Copyright (C) 2009, 2010, 2011 Red Hat, Inc. * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#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>#include <linux/poll.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/spinlock.h>#include <linux/virtio.h>#include <linux/virtio_console.h>#include <linux/wait.h>#include <linux/workqueue.h>#include <linux/module.h>#include "../tty/hvc/hvc_console.h" /* * This is a global struct for storing common data for all the devices * this driver handles. * * Mainly, it has a linked list for all the consoles in one place so * that callbacks from hvc for get_chars(), put_chars() work properly * across multiple devices and multiple ports per device. */struct ports_driver_data { /* Used for registering chardevs */ struct class *class; /* Used for exporting per-port information to debugfs */ struct dentry *debugfs_dir; /* List of all the devices we're handling */ struct list_head portdevs; /* Number of devices this driver is handling */ unsigned int index; /* * This is used to keep track of the number of hvc consoles * spawned by this driver. This number is given as the first * argument to hvc_alloc(). To correctly map an initial * console spawned via hvc_instantiate to the console being * hooked up via hvc_alloc, we need to pass the same vtermno. * * We also just assume the first console being initialised was * the first one that got used as the initial console. */ unsigned int next_vtermno; /* All the console devices handled by this driver */ struct list_head consoles;};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 { /* We'll place all consoles in a list in the pdrvdata struct */ struct list_head list; /* The hvc device associated with this console port */ struct hvc_struct *hvc; /* The size of the console */ struct winsize ws; /* * This number identifies the number that we used to register * with hvc in hvc_instantiate() and hvc_alloc(); this is the * number passed on by the hvc callbacks to us to * differentiate between the other console ports handled by * this driver */ u32 vtermno;}; struct port_buffer { char *buf; /* size of the buffer in *buf above */ size_t size; /* used length of the buffer */ size_t len; /* offset in the buf from which to consume data */ size_t offset;}; /* * This is a per-device struct that stores data common to all the * ports for that device (vdev->priv). */struct ports_device { /* Next portdev in the list, head is in the pdrvdata struct */ struct list_head list; /* * Workqueue handlers where we process deferred work after * notification */ struct work_struct control_work; struct list_head ports; /* To protect the list of ports */ spinlock_t ports_lock; /* To protect the vq operations for the control channel */ spinlock_t cvq_lock; /* The current config space is stored here */ struct virtio_console_config config; /* The virtio device we're associated with */ struct virtio_device *vdev; /* * A couple of virtqueues for the control channel: one for * guest->host transfers, one for host->guest transfers */ struct virtqueue *c_ivq, *c_ovq; /* Array of per-port IO virtqueues */ struct virtqueue **in_vqs, **out_vqs; /* Used for numbering devices for sysfs and debugfs */ unsigned int drv_index; /* Major number for this device. Ports will be created as minors. */ 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 */ struct list_head list; /* Pointer to the parent virtio_console device */ struct ports_device *portdev; /* The current buffer from which data has to be fed to readers */ struct port_buffer *inbuf; /* * To protect the operations on the in_vq associated with this * port. Has to be a spinlock because it can be called from * interrupt context (get_char()). */ spinlock_t inbuf_lock; /* Protect the operations on the out_vq. */ spinlock_t outvq_lock; /* The IO vqs for this port */ struct virtqueue *in_vq, *out_vq; /* 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 */ struct console cons; /* Each port associates with a separate char device */ struct cdev *cdev; struct device *dev; /* Reference-counting to handle port hot-unplugs and file operations */ struct kref kref; /* A waitqueue for poll() or blocking read operations */ wait_queue_head_t waitqueue; /* The 'name' of the port that we expose via sysfs properties */ char *name; /* We can notify apps of host connect / disconnect events via SIGIO */ struct fasync_struct *async_queue; /* The 'id' to identify the port with the Host */ u32 id; bool outvq_full; /* Is the host device open */ bool host_connected; /* We should allow only one process to open a port */ bool guest_connected;}; /* This is the very early arch-specified put chars function. */static int (*early_put_chars)(u32, const char *, int); static struct port *find_port_by_vtermno(u32 vtermno){ struct port *port; struct console *cons; unsigned long flags; spin_lock_irqsave(&pdrvdata_lock, flags); list_for_each_entry(cons, &pdrvdata.consoles, list) { if (cons->vtermno == vtermno) { port = container_of(cons, struct port, cons); goto out; } } port = NULL;out: spin_unlock_irqrestore(&pdrvdata_lock, flags); return port;} static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev, dev_t dev){ struct port *port; unsigned long flags; spin_lock_irqsave(&portdev->ports_lock, flags); list_for_each_entry(port, &portdev->ports, list) if (port->cdev->dev == dev) goto out; port = NULL;out: spin_unlock_irqrestore(&portdev->ports_lock, flags); return port;} static struct port *find_port_by_devt(dev_t dev){ struct ports_device *portdev; struct port *port; unsigned long flags; spin_lock_irqsave(&pdrvdata_lock, flags); list_for_each_entry(portdev, &pdrvdata.portdevs, list) { port = find_port_by_devt_in_portdev(portdev, dev); if (port) goto out; } port = NULL;out: spin_unlock_irqrestore(&pdrvdata_lock, flags); return port;} static struct port *find_port_by_id(struct ports_device *portdev, u32 id){ struct port *port; unsigned long flags; spin_lock_irqsave(&portdev->ports_lock, flags); list_for_each_entry(port, &portdev->ports, list) if (port->id == id) goto out; port = NULL;out: spin_unlock_irqrestore(&portdev->ports_lock, flags); return port;} static struct port *find_port_by_vq(struct ports_device *portdev, struct virtqueue *vq){ struct port *port; unsigned long flags; spin_lock_irqsave(&portdev->ports_lock, flags); list_for_each_entry(port, &portdev->ports, list) if (port->in_vq == vq || port->out_vq == vq) goto out; port = NULL;out: spin_unlock_irqrestore(&portdev->ports_lock, flags); return port;} static bool is_console_port(struct port *port){ if (port->cons.hvc) return true; return false;} static inline bool use_multiport(struct ports_device *portdev){ /* * This condition can be true when put_chars is called from * early_init */ if (!portdev->vdev) return 0; return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);} static void free_buf(struct port_buffer *buf){ kfree(buf->buf); kfree(buf);} static struct port_buffer *alloc_buf(size_t buf_size){ struct port_buffer *buf; buf = kmalloc(sizeof(*buf), GFP_KERNEL); if (!buf) goto fail; buf->buf = kzalloc(buf_size, GFP_KERNEL); if (!buf->buf) goto free_buf; buf->len = 0; buf->offset = 0; buf->size = buf_size; return buf; free_buf: kfree(buf);fail: return NULL;} /* Callers should take appropriate locks */static struct port_buffer *get_inbuf(struct port *port){ struct port_buffer *buf; unsigned int 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;} /* * Create a scatter-gather list representing our input buffer and put * it in the queue. * * Callers should take appropriate locks. */static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf){ struct scatterlist sg[1]; int ret; sg_init_one(sg, buf->buf, buf->size); ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC); virtqueue_kick(vq); return ret;} /* Discard any unread data this port has. Callers lockers. */static void discard_port_data(struct port *port){ struct port_buffer *buf; unsigned int err; if (!port->portdev) { /* Device has been unplugged. vqs are already gone. */ return; } buf = get_inbuf(port); err = 0; while (buf) { port->stats.bytes_discarded += buf->len - buf->offset; if (add_inbuf(port->in_vq, buf) < 0) { err++; free_buf(buf); } port->inbuf = NULL; buf = get_inbuf(port); } if (err) dev_warn(port->dev, "Errors adding %d buffers back to vq\n", err);} static bool port_has_data(struct port *port){ unsigned long flags; bool ret; ret = false; spin_lock_irqsave(&port->inbuf_lock, flags); port->inbuf = get_inbuf(port); if (port->inbuf) ret = true; spin_unlock_irqrestore(&port->inbuf_lock, flags); return ret;} static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id, unsigned int event, unsigned int value){ struct scatterlist sg[1]; struct virtio_console_control cpkt; struct virtqueue *vq; unsigned int len; if (!use_multiport(portdev)) return 0; cpkt.id = port_id; cpkt.event = event; cpkt.value = value; vq = portdev->c_ovq; sg_init_one(sg, &cpkt, sizeof(cpkt)); if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) { virtqueue_kick(vq); while (!virtqueue_get_buf(vq, &len)) cpu_relax(); } return 0;} static ssize_t send_control_msg(struct port *port, unsigned int event, unsigned int value){ /* Did the port get unplugged before userspace closed it? */ if (port->portdev) return __send_control_msg(port->portdev, port->id, event, value); return 0;} /* Callers must take the port->outvq_lock */static void reclaim_consumed_buffers(struct port *port){ void *buf; unsigned int len; if (!port->portdev) { /* Device has been unplugged. vqs are already gone. */ return; } while ((buf = virtqueue_get_buf(port->out_vq, &len))) { kfree(buf); port->outvq_full = false; }} static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count, bool nonblock){ struct scatterlist sg[1]; struct virtqueue *out_vq; ssize_t ret; unsigned long flags; unsigned int len;Lines 1–500 of 1960. Load more
/* * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation * Copyright (C) 2009, 2010, 2011 Red Hat, Inc. * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #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> #include <linux/poll.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/virtio.h> #include <linux/virtio_console.h> #include <linux/wait.h> #include <linux/workqueue.h> #include <linux/module.h> #include "../tty/hvc/hvc_console.h" /* * This is a global struct for storing common data for all the devices * this driver handles. * * Mainly, it has a linked list for all the consoles in one place so * that callbacks from hvc for get_chars(), put_chars() work properly * across multiple devices and multiple ports per device. */ struct ports_driver_data { /* Used for registering chardevs */ struct class *class; /* Used for exporting per-port information to debugfs */ struct dentry *debugfs_dir; /* List of all the devices we're handling */ struct list_head portdevs; /* Number of devices this driver is handling */ unsigned int index; /* * This is used to keep track of the number of hvc consoles * spawned by this driver. This number is given as the first * argument to hvc_alloc(). To correctly map an initial * console spawned via hvc_instantiate to the console being * hooked up via hvc_alloc, we need to pass the same vtermno. * * We also just assume the first console being initialised was * the first one that got used as the initial console. */ unsigned int next_vtermno; /* All the console devices handled by this driver */ struct list_head consoles; }; 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 { /* We'll place all consoles in a list in the pdrvdata struct */ struct list_head list; /* The hvc device associated with this console port */ struct hvc_struct *hvc; /* The size of the console */ struct winsize ws; /* * This number identifies the number that we used to register * with hvc in hvc_instantiate() and hvc_alloc(); this is the * number passed on by the hvc callbacks to us to * differentiate between the other console ports handled by * this driver */ u32 vtermno; }; struct port_buffer { char *buf; /* size of the buffer in *buf above */ size_t size; /* used length of the buffer */ size_t len; /* offset in the buf from which to consume data */ size_t offset; }; /* * This is a per-device struct that stores data common to all the * ports for that device (vdev->priv). */ struct ports_device { /* Next portdev in the list, head is in the pdrvdata struct */ struct list_head list; /* * Workqueue handlers where we process deferred work after * notification */ struct work_struct control_work; struct list_head ports; /* To protect the list of ports */ spinlock_t ports_lock; /* To protect the vq operations for the control channel */ spinlock_t cvq_lock; /* The current config space is stored here */ struct virtio_console_config config; /* The virtio device we're associated with */ struct virtio_device *vdev; /* * A couple of virtqueues for the control channel: one for * guest->host transfers, one for host->guest transfers */ struct virtqueue *c_ivq, *c_ovq; /* Array of per-port IO virtqueues */ struct virtqueue **in_vqs, **out_vqs; /* Used for numbering devices for sysfs and debugfs */ unsigned int drv_index; /* Major number for this device. Ports will be created as minors. */ 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 */ struct list_head list; /* Pointer to the parent virtio_console device */ struct ports_device *portdev; /* The current buffer from which data has to be fed to readers */ struct port_buffer *inbuf; /* * To protect the operations on the in_vq associated with this * port. Has to be a spinlock because it can be called from * interrupt context (get_char()). */ spinlock_t inbuf_lock; /* Protect the operations on the out_vq. */ spinlock_t outvq_lock; /* The IO vqs for this port */ struct virtqueue *in_vq, *out_vq; /* 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 */ struct console cons; /* Each port associates with a separate char device */ struct cdev *cdev; struct device *dev; /* Reference-counting to handle port hot-unplugs and file operations */ struct kref kref; /* A waitqueue for poll() or blocking read operations */ wait_queue_head_t waitqueue; /* The 'name' of the port that we expose via sysfs properties */ char *name; /* We can notify apps of host connect / disconnect events via SIGIO */ struct fasync_struct *async_queue; /* The 'id' to identify the port with the Host */ u32 id; bool outvq_full; /* Is the host device open */ bool host_connected; /* We should allow only one process to open a port */ bool guest_connected; }; /* This is the very early arch-specified put chars function. */ static int (*early_put_chars)(u32, const char *, int); static struct port *find_port_by_vtermno(u32 vtermno) { struct port *port; struct console *cons; unsigned long flags; spin_lock_irqsave(&pdrvdata_lock, flags); list_for_each_entry(cons, &pdrvdata.consoles, list) { if (cons->vtermno == vtermno) { port = container_of(cons, struct port, cons); goto out; } } port = NULL; out: spin_unlock_irqrestore(&pdrvdata_lock, flags); return port; } static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev, dev_t dev) { struct port *port; unsigned long flags; spin_lock_irqsave(&portdev->ports_lock, flags); list_for_each_entry(port, &portdev->ports, list) if (port->cdev->dev == dev) goto out; port = NULL; out: spin_unlock_irqrestore(&portdev->ports_lock, flags); return port; } static struct port *find_port_by_devt(dev_t dev) { struct ports_device *portdev; struct port *port; unsigned long flags; spin_lock_irqsave(&pdrvdata_lock, flags); list_for_each_entry(portdev, &pdrvdata.portdevs, list) { port = find_port_by_devt_in_portdev(portdev, dev); if (port) goto out; } port = NULL; out: spin_unlock_irqrestore(&pdrvdata_lock, flags); return port; } static struct port *find_port_by_id(struct ports_device *portdev, u32 id) { struct port *port; unsigned long flags; spin_lock_irqsave(&portdev->ports_lock, flags); list_for_each_entry(port, &portdev->ports, list) if (port->id == id) goto out; port = NULL; out: spin_unlock_irqrestore(&portdev->ports_lock, flags); return port; } static struct port *find_port_by_vq(struct ports_device *portdev, struct virtqueue *vq) { struct port *port; unsigned long flags; spin_lock_irqsave(&portdev->ports_lock, flags); list_for_each_entry(port, &portdev->ports, list) if (port->in_vq == vq || port->out_vq == vq) goto out; port = NULL; out: spin_unlock_irqrestore(&portdev->ports_lock, flags); return port; } static bool is_console_port(struct port *port) { if (port->cons.hvc) return true; return false; } static inline bool use_multiport(struct ports_device *portdev) { /* * This condition can be true when put_chars is called from * early_init */ if (!portdev->vdev) return 0; return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT); } static void free_buf(struct port_buffer *buf) { kfree(buf->buf); kfree(buf); } static struct port_buffer *alloc_buf(size_t buf_size) { struct port_buffer *buf; buf = kmalloc(sizeof(*buf), GFP_KERNEL); if (!buf) goto fail; buf->buf = kzalloc(buf_size, GFP_KERNEL); if (!buf->buf) goto free_buf; buf->len = 0; buf->offset = 0; buf->size = buf_size; return buf; free_buf: kfree(buf); fail: return NULL; } /* Callers should take appropriate locks */ static struct port_buffer *get_inbuf(struct port *port) { struct port_buffer *buf; unsigned int 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; } /* * Create a scatter-gather list representing our input buffer and put * it in the queue. * * Callers should take appropriate locks. */ static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf) { struct scatterlist sg[1]; int ret; sg_init_one(sg, buf->buf, buf->size); ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC); virtqueue_kick(vq); return ret; } /* Discard any unread data this port has. Callers lockers. */ static void discard_port_data(struct port *port) { struct port_buffer *buf; unsigned int err; if (!port->portdev) { /* Device has been unplugged. vqs are already gone. */ return; } buf = get_inbuf(port); err = 0; while (buf) { port->stats.bytes_discarded += buf->len - buf->offset; if (add_inbuf(port->in_vq, buf) < 0) { err++; free_buf(buf); } port->inbuf = NULL; buf = get_inbuf(port); } if (err) dev_warn(port->dev, "Errors adding %d buffers back to vq\n", err); } static bool port_has_data(struct port *port) { unsigned long flags; bool ret; ret = false; spin_lock_irqsave(&port->inbuf_lock, flags); port->inbuf = get_inbuf(port); if (port->inbuf) ret = true; spin_unlock_irqrestore(&port->inbuf_lock, flags); return ret; } static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id, unsigned int event, unsigned int value) { struct scatterlist sg[1]; struct virtio_console_control cpkt; struct virtqueue *vq; unsigned int len; if (!use_multiport(portdev)) return 0; cpkt.id = port_id; cpkt.event = event; cpkt.value = value; vq = portdev->c_ovq; sg_init_one(sg, &cpkt, sizeof(cpkt)); if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) { virtqueue_kick(vq); while (!virtqueue_get_buf(vq, &len)) cpu_relax(); } return 0; } static ssize_t send_control_msg(struct port *port, unsigned int event, unsigned int value) { /* Did the port get unplugged before userspace closed it? */ if (port->portdev) return __send_control_msg(port->portdev, port->id, event, value); return 0; } /* Callers must take the port->outvq_lock */ static void reclaim_consumed_buffers(struct port *port) { void *buf; unsigned int len; if (!port->portdev) { /* Device has been unplugged. vqs are already gone. */ return; } while ((buf = virtqueue_get_buf(port->out_vq, &len))) { kfree(buf); port->outvq_full = false; } } static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count, bool nonblock) { struct scatterlist sg[1]; struct virtqueue *out_vq; ssize_t ret; unsigned long flags; unsigned int len;