2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
3 * Copyright (C) 2009, 2010 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/cdev.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/err.h>
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/poll.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/virtio.h>
31 #include <linux/virtio_console.h>
32 #include <linux/wait.h>
33 #include <linux/workqueue.h>
34 #include "hvc_console.h"
37 * This is a global struct for storing common data for all the devices
38 * this driver handles.
40 * Mainly, it has a linked list for all the consoles in one place so
41 * that callbacks from hvc for get_chars(), put_chars() work properly
42 * across multiple devices and multiple ports per device.
44 struct ports_driver_data {
45 /* Used for registering chardevs */
48 /* Used for exporting per-port information to debugfs */
49 struct dentry *debugfs_dir;
51 /* List of all the devices we're handling */
52 struct list_head portdevs;
54 /* Number of devices this driver is handling */
58 * This is used to keep track of the number of hvc consoles
59 * spawned by this driver. This number is given as the first
60 * argument to hvc_alloc(). To correctly map an initial
61 * console spawned via hvc_instantiate to the console being
62 * hooked up via hvc_alloc, we need to pass the same vtermno.
64 * We also just assume the first console being initialised was
65 * the first one that got used as the initial console.
67 unsigned int next_vtermno;
69 /* All the console devices handled by this driver */
70 struct list_head consoles;
72 static struct ports_driver_data pdrvdata;
74 DEFINE_SPINLOCK(pdrvdata_lock);
76 /* This struct holds information that's relevant only for console ports */
78 /* We'll place all consoles in a list in the pdrvdata struct */
79 struct list_head list;
81 /* The hvc device associated with this console port */
82 struct hvc_struct *hvc;
84 /* The size of the console */
88 * This number identifies the number that we used to register
89 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
90 * number passed on by the hvc callbacks to us to
91 * differentiate between the other console ports handled by
100 /* size of the buffer in *buf above */
103 /* used length of the buffer */
105 /* offset in the buf from which to consume data */
110 * This is a per-device struct that stores data common to all the
111 * ports for that device (vdev->priv).
113 struct ports_device {
114 /* Next portdev in the list, head is in the pdrvdata struct */
115 struct list_head list;
118 * Workqueue handlers where we process deferred work after
121 struct work_struct control_work;
123 struct list_head ports;
125 /* To protect the list of ports */
126 spinlock_t ports_lock;
128 /* To protect the vq operations for the control channel */
131 /* The current config space is stored here */
132 struct virtio_console_config config;
134 /* The virtio device we're associated with */
135 struct virtio_device *vdev;
138 * A couple of virtqueues for the control channel: one for
139 * guest->host transfers, one for host->guest transfers
141 struct virtqueue *c_ivq, *c_ovq;
143 /* Array of per-port IO virtqueues */
144 struct virtqueue **in_vqs, **out_vqs;
146 /* Used for numbering devices for sysfs and debugfs */
147 unsigned int drv_index;
149 /* Major number for this device. Ports will be created as minors. */
153 /* This struct holds the per-port data */
155 /* Next port in the list, head is in the ports_device */
156 struct list_head list;
158 /* Pointer to the parent virtio_console device */
159 struct ports_device *portdev;
161 /* The current buffer from which data has to be fed to readers */
162 struct port_buffer *inbuf;
165 * To protect the operations on the in_vq associated with this
166 * port. Has to be a spinlock because it can be called from
167 * interrupt context (get_char()).
169 spinlock_t inbuf_lock;
171 /* Protect the operations on the out_vq. */
172 spinlock_t outvq_lock;
174 /* The IO vqs for this port */
175 struct virtqueue *in_vq, *out_vq;
177 /* File in the debugfs directory that exposes this port's information */
178 struct dentry *debugfs_file;
181 * The entries in this struct will be valid if this port is
182 * hooked up to an hvc console
186 /* Each port associates with a separate char device */
190 /* Reference-counting to handle port hot-unplugs and file operations */
193 /* A waitqueue for poll() or blocking read operations */
194 wait_queue_head_t waitqueue;
196 /* The 'name' of the port that we expose via sysfs properties */
199 /* The 'id' to identify the port with the Host */
204 /* Is the host device open */
207 /* We should allow only one process to open a port */
208 bool guest_connected;
211 /* This is the very early arch-specified put chars function. */
212 static int (*early_put_chars)(u32, const char *, int);
214 static struct port *find_port_by_vtermno(u32 vtermno)
217 struct console *cons;
220 spin_lock_irqsave(&pdrvdata_lock, flags);
221 list_for_each_entry(cons, &pdrvdata.consoles, list) {
222 if (cons->vtermno == vtermno) {
223 port = container_of(cons, struct port, cons);
229 spin_unlock_irqrestore(&pdrvdata_lock, flags);
233 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
239 spin_lock_irqsave(&portdev->ports_lock, flags);
240 list_for_each_entry(port, &portdev->ports, list)
241 if (port->cdev->dev == dev)
245 spin_unlock_irqrestore(&portdev->ports_lock, flags);
250 static struct port *find_port_by_devt(dev_t dev)
252 struct ports_device *portdev;
256 spin_lock_irqsave(&pdrvdata_lock, flags);
257 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
258 port = find_port_by_devt_in_portdev(portdev, dev);
264 spin_unlock_irqrestore(&pdrvdata_lock, flags);
268 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
273 spin_lock_irqsave(&portdev->ports_lock, flags);
274 list_for_each_entry(port, &portdev->ports, list)
279 spin_unlock_irqrestore(&portdev->ports_lock, flags);
284 static struct port *find_port_by_vq(struct ports_device *portdev,
285 struct virtqueue *vq)
290 spin_lock_irqsave(&portdev->ports_lock, flags);
291 list_for_each_entry(port, &portdev->ports, list)
292 if (port->in_vq == vq || port->out_vq == vq)
296 spin_unlock_irqrestore(&portdev->ports_lock, flags);
300 static bool is_console_port(struct port *port)
307 static inline bool use_multiport(struct ports_device *portdev)
310 * This condition can be true when put_chars is called from
315 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
318 static void free_buf(struct port_buffer *buf)
324 static struct port_buffer *alloc_buf(size_t buf_size)
326 struct port_buffer *buf;
328 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
331 buf->buf = kzalloc(buf_size, GFP_KERNEL);
336 buf->size = buf_size;
345 /* Callers should take appropriate locks */
346 static void *get_inbuf(struct port *port)
348 struct port_buffer *buf;
349 struct virtqueue *vq;
353 buf = virtqueue_get_buf(vq, &len);
362 * Create a scatter-gather list representing our input buffer and put
365 * Callers should take appropriate locks.
367 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
369 struct scatterlist sg[1];
372 sg_init_one(sg, buf->buf, buf->size);
374 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
379 /* Discard any unread data this port has. Callers lockers. */
380 static void discard_port_data(struct port *port)
382 struct port_buffer *buf;
383 struct virtqueue *vq;
391 buf = virtqueue_get_buf(vq, &len);
395 if (add_inbuf(vq, buf) < 0) {
399 buf = virtqueue_get_buf(vq, &len);
403 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
407 static bool port_has_data(struct port *port)
412 spin_lock_irqsave(&port->inbuf_lock, flags);
417 port->inbuf = get_inbuf(port);
424 spin_unlock_irqrestore(&port->inbuf_lock, flags);
428 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
429 unsigned int event, unsigned int value)
431 struct scatterlist sg[1];
432 struct virtio_console_control cpkt;
433 struct virtqueue *vq;
436 if (!use_multiport(portdev))
445 sg_init_one(sg, &cpkt, sizeof(cpkt));
446 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
448 while (!virtqueue_get_buf(vq, &len))
454 static ssize_t send_control_msg(struct port *port, unsigned int event,
457 /* Did the port get unplugged before userspace closed it? */
459 return __send_control_msg(port->portdev, port->id, event, value);
463 /* Callers must take the port->outvq_lock */
464 static void reclaim_consumed_buffers(struct port *port)
469 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
471 port->outvq_full = false;
475 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
478 struct scatterlist sg[1];
479 struct virtqueue *out_vq;
484 out_vq = port->out_vq;
486 spin_lock_irqsave(&port->outvq_lock, flags);
488 reclaim_consumed_buffers(port);
490 sg_init_one(sg, in_buf, in_count);
491 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
493 /* Tell Host to go! */
494 virtqueue_kick(out_vq);
502 port->outvq_full = true;
508 * Wait till the host acknowledges it pushed out the data we
509 * sent. This is done for data from the hvc_console; the tty
510 * operations are performed with spinlocks held so we can't
511 * sleep here. An alternative would be to copy the data to a
512 * buffer and relax the spinning requirement. The downside is
513 * we need to kmalloc a GFP_ATOMIC buffer each time the
514 * console driver writes something out.
516 while (!virtqueue_get_buf(out_vq, &len))
519 spin_unlock_irqrestore(&port->outvq_lock, flags);
521 * We're expected to return the amount of data we wrote -- all
528 * Give out the data that's requested from the buffer that we have
531 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
534 struct port_buffer *buf;
537 if (!out_count || !port_has_data(port))
541 out_count = min(out_count, buf->len - buf->offset);
546 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
550 memcpy(out_buf, buf->buf + buf->offset, out_count);
553 buf->offset += out_count;
555 if (buf->offset == buf->len) {
557 * We're done using all the data in this buffer.
558 * Re-queue so that the Host can send us more data.
560 spin_lock_irqsave(&port->inbuf_lock, flags);
563 if (add_inbuf(port->in_vq, buf) < 0)
564 dev_warn(port->dev, "failed add_buf\n");
566 spin_unlock_irqrestore(&port->inbuf_lock, flags);
568 /* Return the number of bytes actually copied */
572 /* The condition that must be true for polling to end */
573 static bool will_read_block(struct port *port)
575 if (!port->guest_connected) {
576 /* Port got hot-unplugged. Let's exit. */
579 return !port_has_data(port) && port->host_connected;
582 static bool will_write_block(struct port *port)
586 if (!port->guest_connected) {
587 /* Port got hot-unplugged. Let's exit. */
590 if (!port->host_connected)
593 spin_lock_irq(&port->outvq_lock);
595 * Check if the Host has consumed any buffers since we last
596 * sent data (this is only applicable for nonblocking ports).
598 reclaim_consumed_buffers(port);
599 ret = port->outvq_full;
600 spin_unlock_irq(&port->outvq_lock);
605 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
606 size_t count, loff_t *offp)
611 port = filp->private_data;
613 if (!port_has_data(port)) {
615 * If nothing's connected on the host just return 0 in
616 * case of list_empty; this tells the userspace app
617 * that there's no connection
619 if (!port->host_connected)
621 if (filp->f_flags & O_NONBLOCK)
624 ret = wait_event_interruptible(port->waitqueue,
625 !will_read_block(port));
629 /* Port got hot-unplugged. */
630 if (!port->guest_connected)
633 * We could've received a disconnection message while we were
634 * waiting for more data.
636 * This check is not clubbed in the if() statement above as we
637 * might receive some data as well as the host could get
638 * disconnected after we got woken up from our wait. So we
639 * really want to give off whatever data we have and only then
640 * check for host_connected.
642 if (!port_has_data(port) && !port->host_connected)
645 return fill_readbuf(port, ubuf, count, true);
648 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
649 size_t count, loff_t *offp)
656 /* Userspace could be out to fool us */
660 port = filp->private_data;
662 nonblock = filp->f_flags & O_NONBLOCK;
664 if (will_write_block(port)) {
668 ret = wait_event_interruptible(port->waitqueue,
669 !will_write_block(port));
673 /* Port got hot-unplugged. */
674 if (!port->guest_connected)
677 count = min((size_t)(32 * 1024), count);
679 buf = kmalloc(count, GFP_KERNEL);
683 ret = copy_from_user(buf, ubuf, count);
690 * We now ask send_buf() to not spin for generic ports -- we
691 * can re-use the same code path that non-blocking file
692 * descriptors take for blocking file descriptors since the
693 * wait is already done and we're certain the write will go
694 * through to the host.
697 ret = send_buf(port, buf, count, nonblock);
699 if (nonblock && ret > 0)
708 static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
713 port = filp->private_data;
714 poll_wait(filp, &port->waitqueue, wait);
716 if (!port->guest_connected) {
717 /* Port got unplugged */
721 if (!will_read_block(port))
722 ret |= POLLIN | POLLRDNORM;
723 if (!will_write_block(port))
725 if (!port->host_connected)
731 static void remove_port(struct kref *kref);
733 static int port_fops_release(struct inode *inode, struct file *filp)
737 port = filp->private_data;
739 /* Notify host of port being closed */
740 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
742 spin_lock_irq(&port->inbuf_lock);
743 port->guest_connected = false;
745 discard_port_data(port);
747 spin_unlock_irq(&port->inbuf_lock);
749 spin_lock_irq(&port->outvq_lock);
750 reclaim_consumed_buffers(port);
751 spin_unlock_irq(&port->outvq_lock);
754 * Locks aren't necessary here as a port can't be opened after
755 * unplug, and if a port isn't unplugged, a kref would already
756 * exist for the port. Plus, taking ports_lock here would
757 * create a dependency on other locks taken by functions
758 * inside remove_port if we're the last holder of the port,
759 * creating many problems.
761 kref_put(&port->kref, remove_port);
766 static int port_fops_open(struct inode *inode, struct file *filp)
768 struct cdev *cdev = inode->i_cdev;
772 port = find_port_by_devt(cdev->dev);
773 filp->private_data = port;
775 /* Prevent against a port getting hot-unplugged at the same time */
776 spin_lock_irq(&port->portdev->ports_lock);
777 kref_get(&port->kref);
778 spin_unlock_irq(&port->portdev->ports_lock);
781 * Don't allow opening of console port devices -- that's done
784 if (is_console_port(port)) {
789 /* Allow only one process to open a particular port at a time */
790 spin_lock_irq(&port->inbuf_lock);
791 if (port->guest_connected) {
792 spin_unlock_irq(&port->inbuf_lock);
797 port->guest_connected = true;
798 spin_unlock_irq(&port->inbuf_lock);
800 spin_lock_irq(&port->outvq_lock);
802 * There might be a chance that we missed reclaiming a few
803 * buffers in the window of the port getting previously closed
806 reclaim_consumed_buffers(port);
807 spin_unlock_irq(&port->outvq_lock);
809 /* Notify host of port being opened */
810 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
814 kref_put(&port->kref, remove_port);
819 * The file operations that we support: programs in the guest can open
820 * a console device, read from it, write to it, poll for data and
821 * close it. The devices are at
822 * /dev/vport<device number>p<port number>
824 static const struct file_operations port_fops = {
825 .owner = THIS_MODULE,
826 .open = port_fops_open,
827 .read = port_fops_read,
828 .write = port_fops_write,
829 .poll = port_fops_poll,
830 .release = port_fops_release,
834 * The put_chars() callback is pretty straightforward.
836 * We turn the characters into a scatter-gather list, add it to the
837 * output queue and then kick the Host. Then we sit here waiting for
838 * it to finish: inefficient in theory, but in practice
839 * implementations will do it immediately (lguest's Launcher does).
841 static int put_chars(u32 vtermno, const char *buf, int count)
845 if (unlikely(early_put_chars))
846 return early_put_chars(vtermno, buf, count);
848 port = find_port_by_vtermno(vtermno);
852 return send_buf(port, (void *)buf, count, false);
856 * get_chars() is the callback from the hvc_console infrastructure
857 * when an interrupt is received.
859 * We call out to fill_readbuf that gets us the required data from the
860 * buffers that are queued up.
862 static int get_chars(u32 vtermno, char *buf, int count)
866 /* If we've not set up the port yet, we have no input to give. */
867 if (unlikely(early_put_chars))
870 port = find_port_by_vtermno(vtermno);
874 /* If we don't have an input queue yet, we can't get input. */
875 BUG_ON(!port->in_vq);
877 return fill_readbuf(port, buf, count, false);
880 static void resize_console(struct port *port)
882 struct virtio_device *vdev;
884 /* The port could have been hot-unplugged */
885 if (!port || !is_console_port(port))
888 vdev = port->portdev->vdev;
889 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
890 hvc_resize(port->cons.hvc, port->cons.ws);
893 /* We set the configuration at this point, since we now have a tty */
894 static int notifier_add_vio(struct hvc_struct *hp, int data)
898 port = find_port_by_vtermno(hp->vtermno);
902 hp->irq_requested = 1;
903 resize_console(port);
908 static void notifier_del_vio(struct hvc_struct *hp, int data)
910 hp->irq_requested = 0;
913 /* The operations for console ports. */
914 static const struct hv_ops hv_ops = {
915 .get_chars = get_chars,
916 .put_chars = put_chars,
917 .notifier_add = notifier_add_vio,
918 .notifier_del = notifier_del_vio,
919 .notifier_hangup = notifier_del_vio,
923 * Console drivers are initialized very early so boot messages can go
924 * out, so we do things slightly differently from the generic virtio
925 * initialization of the net and block drivers.
927 * At this stage, the console is output-only. It's too early to set
928 * up a virtqueue, so we let the drivers do some boutique early-output
931 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
933 early_put_chars = put_chars;
934 return hvc_instantiate(0, 0, &hv_ops);
937 int init_port_console(struct port *port)
942 * The Host's telling us this port is a console port. Hook it
943 * up with an hvc console.
945 * To set up and manage our virtual console, we call
948 * The first argument of hvc_alloc() is the virtual console
949 * number. The second argument is the parameter for the
950 * notification mechanism (like irq number). We currently
951 * leave this as zero, virtqueues have implicit notifications.
953 * The third argument is a "struct hv_ops" containing the
954 * put_chars() get_chars(), notifier_add() and notifier_del()
955 * pointers. The final argument is the output buffer size: we
956 * can do any size, so we put PAGE_SIZE here.
958 port->cons.vtermno = pdrvdata.next_vtermno;
960 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
961 if (IS_ERR(port->cons.hvc)) {
962 ret = PTR_ERR(port->cons.hvc);
964 "error %d allocating hvc for port\n", ret);
965 port->cons.hvc = NULL;
968 spin_lock_irq(&pdrvdata_lock);
969 pdrvdata.next_vtermno++;
970 list_add_tail(&port->cons.list, &pdrvdata.consoles);
971 spin_unlock_irq(&pdrvdata_lock);
972 port->guest_connected = true;
975 * Start using the new console output if this is the first
976 * console to come up.
979 early_put_chars = NULL;
981 /* Notify host of port being opened */
982 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
987 static ssize_t show_port_name(struct device *dev,
988 struct device_attribute *attr, char *buffer)
992 port = dev_get_drvdata(dev);
994 return sprintf(buffer, "%s\n", port->name);
997 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
999 static struct attribute *port_sysfs_entries[] = {
1000 &dev_attr_name.attr,
1004 static struct attribute_group port_attribute_group = {
1005 .name = NULL, /* put in device directory */
1006 .attrs = port_sysfs_entries,
1009 static int debugfs_open(struct inode *inode, struct file *filp)
1011 filp->private_data = inode->i_private;
1015 static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1016 size_t count, loff_t *offp)
1020 ssize_t ret, out_offset, out_count;
1023 buf = kmalloc(out_count, GFP_KERNEL);
1027 port = filp->private_data;
1029 out_offset += snprintf(buf + out_offset, out_count,
1030 "name: %s\n", port->name ? port->name : "");
1031 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1032 "guest_connected: %d\n", port->guest_connected);
1033 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1034 "host_connected: %d\n", port->host_connected);
1035 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1036 "outvq_full: %d\n", port->outvq_full);
1037 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1039 is_console_port(port) ? "yes" : "no");
1040 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1041 "console_vtermno: %u\n", port->cons.vtermno);
1043 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1048 static const struct file_operations port_debugfs_ops = {
1049 .owner = THIS_MODULE,
1050 .open = debugfs_open,
1051 .read = debugfs_read,
1054 static void set_console_size(struct port *port, u16 rows, u16 cols)
1056 if (!port || !is_console_port(port))
1059 port->cons.ws.ws_row = rows;
1060 port->cons.ws.ws_col = cols;
1063 static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1065 struct port_buffer *buf;
1066 unsigned int nr_added_bufs;
1071 buf = alloc_buf(PAGE_SIZE);
1075 spin_lock_irq(lock);
1076 ret = add_inbuf(vq, buf);
1078 spin_unlock_irq(lock);
1083 spin_unlock_irq(lock);
1086 return nr_added_bufs;
1089 static int add_port(struct ports_device *portdev, u32 id)
1091 char debugfs_name[16];
1093 struct port_buffer *buf;
1095 unsigned int nr_added_bufs;
1098 port = kmalloc(sizeof(*port), GFP_KERNEL);
1103 kref_init(&port->kref);
1105 port->portdev = portdev;
1110 port->cons.hvc = NULL;
1112 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1114 port->host_connected = port->guest_connected = false;
1116 port->outvq_full = false;
1118 port->in_vq = portdev->in_vqs[port->id];
1119 port->out_vq = portdev->out_vqs[port->id];
1121 port->cdev = cdev_alloc();
1123 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1127 port->cdev->ops = &port_fops;
1129 devt = MKDEV(portdev->chr_major, id);
1130 err = cdev_add(port->cdev, devt, 1);
1132 dev_err(&port->portdev->vdev->dev,
1133 "Error %d adding cdev for port %u\n", err, id);
1136 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1137 devt, port, "vport%up%u",
1138 port->portdev->drv_index, id);
1139 if (IS_ERR(port->dev)) {
1140 err = PTR_ERR(port->dev);
1141 dev_err(&port->portdev->vdev->dev,
1142 "Error %d creating device for port %u\n",
1147 spin_lock_init(&port->inbuf_lock);
1148 spin_lock_init(&port->outvq_lock);
1149 init_waitqueue_head(&port->waitqueue);
1151 /* Fill the in_vq with buffers so the host can send us data. */
1152 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1153 if (!nr_added_bufs) {
1154 dev_err(port->dev, "Error allocating inbufs\n");
1160 * If we're not using multiport support, this has to be a console port
1162 if (!use_multiport(port->portdev)) {
1163 err = init_port_console(port);
1168 spin_lock_irq(&portdev->ports_lock);
1169 list_add_tail(&port->list, &port->portdev->ports);
1170 spin_unlock_irq(&portdev->ports_lock);
1173 * Tell the Host we're set so that it can send us various
1174 * configuration parameters for this port (eg, port name,
1175 * caching, whether this is a console port, etc.)
1177 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1179 if (pdrvdata.debugfs_dir) {
1181 * Finally, create the debugfs file that we can use to
1182 * inspect a port's state at any time
1184 sprintf(debugfs_name, "vport%up%u",
1185 port->portdev->drv_index, id);
1186 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1187 pdrvdata.debugfs_dir,
1194 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1197 device_destroy(pdrvdata.class, port->dev->devt);
1199 cdev_del(port->cdev);
1203 /* The host might want to notify management sw about port add failure */
1204 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1208 /* No users remain, remove all port-specific data. */
1209 static void remove_port(struct kref *kref)
1213 port = container_of(kref, struct port, kref);
1215 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1216 device_destroy(pdrvdata.class, port->dev->devt);
1217 cdev_del(port->cdev);
1221 debugfs_remove(port->debugfs_file);
1227 * Port got unplugged. Remove port from portdev's list and drop the
1228 * kref reference. If no userspace has this port opened, it will
1229 * result in immediate removal the port.
1231 static void unplug_port(struct port *port)
1233 struct port_buffer *buf;
1235 spin_lock_irq(&port->portdev->ports_lock);
1236 list_del(&port->list);
1237 spin_unlock_irq(&port->portdev->ports_lock);
1239 if (port->guest_connected) {
1240 port->guest_connected = false;
1241 port->host_connected = false;
1242 wake_up_interruptible(&port->waitqueue);
1245 if (is_console_port(port)) {
1246 spin_lock_irq(&pdrvdata_lock);
1247 list_del(&port->cons.list);
1248 spin_unlock_irq(&pdrvdata_lock);
1251 * hvc_remove() not called as removing one hvc port
1252 * results in other hvc ports getting frozen.
1254 * Once this is resolved in hvc, this functionality
1255 * will be enabled. Till that is done, the -EPIPE
1256 * return from get_chars() above will help
1257 * hvc_console.c to clean up on ports we remove here.
1259 hvc_remove(port->cons.hvc);
1263 /* Remove unused data this port might have received. */
1264 discard_port_data(port);
1266 reclaim_consumed_buffers(port);
1268 /* Remove buffers we queued up for the Host to send us data in. */
1269 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1273 * We should just assume the device itself has gone off --
1274 * else a close on an open port later will try to send out a
1277 port->portdev = NULL;
1280 * Locks around here are not necessary - a port can't be
1281 * opened after we removed the port struct from ports_list
1284 kref_put(&port->kref, remove_port);
1287 /* Any private messages that the Host and Guest want to share */
1288 static void handle_control_message(struct ports_device *portdev,
1289 struct port_buffer *buf)
1291 struct virtio_console_control *cpkt;
1296 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1298 port = find_port_by_id(portdev, cpkt->id);
1299 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
1300 /* No valid header at start of buffer. Drop it. */
1301 dev_dbg(&portdev->vdev->dev,
1302 "Invalid index %u in control packet\n", cpkt->id);
1306 switch (cpkt->event) {
1307 case VIRTIO_CONSOLE_PORT_ADD:
1309 dev_dbg(&portdev->vdev->dev,
1310 "Port %u already added\n", port->id);
1311 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1314 if (cpkt->id >= portdev->config.max_nr_ports) {
1315 dev_warn(&portdev->vdev->dev,
1316 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1317 cpkt->id, portdev->config.max_nr_ports - 1);
1320 add_port(portdev, cpkt->id);
1322 case VIRTIO_CONSOLE_PORT_REMOVE:
1325 case VIRTIO_CONSOLE_CONSOLE_PORT:
1328 if (is_console_port(port))
1331 init_port_console(port);
1333 * Could remove the port here in case init fails - but
1334 * have to notify the host first.
1337 case VIRTIO_CONSOLE_RESIZE: {
1343 if (!is_console_port(port))
1346 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1348 set_console_size(port, size.rows, size.cols);
1350 port->cons.hvc->irq_requested = 1;
1351 resize_console(port);
1354 case VIRTIO_CONSOLE_PORT_OPEN:
1355 port->host_connected = cpkt->value;
1356 wake_up_interruptible(&port->waitqueue);
1358 * If the host port got closed and the host had any
1359 * unconsumed buffers, we'll be able to reclaim them
1362 spin_lock_irq(&port->outvq_lock);
1363 reclaim_consumed_buffers(port);
1364 spin_unlock_irq(&port->outvq_lock);
1366 case VIRTIO_CONSOLE_PORT_NAME:
1368 * Skip the size of the header and the cpkt to get the size
1369 * of the name that was sent
1371 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1373 port->name = kmalloc(name_size, GFP_KERNEL);
1376 "Not enough space to store port name\n");
1379 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1381 port->name[name_size - 1] = 0;
1384 * Since we only have one sysfs attribute, 'name',
1385 * create it only if we have a name for the port.
1387 err = sysfs_create_group(&port->dev->kobj,
1388 &port_attribute_group);
1391 "Error %d creating sysfs device attributes\n",
1395 * Generate a udev event so that appropriate
1396 * symlinks can be created based on udev
1399 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1405 static void control_work_handler(struct work_struct *work)
1407 struct ports_device *portdev;
1408 struct virtqueue *vq;
1409 struct port_buffer *buf;
1412 portdev = container_of(work, struct ports_device, control_work);
1413 vq = portdev->c_ivq;
1415 spin_lock(&portdev->cvq_lock);
1416 while ((buf = virtqueue_get_buf(vq, &len))) {
1417 spin_unlock(&portdev->cvq_lock);
1422 handle_control_message(portdev, buf);
1424 spin_lock(&portdev->cvq_lock);
1425 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1426 dev_warn(&portdev->vdev->dev,
1427 "Error adding buffer to queue\n");
1431 spin_unlock(&portdev->cvq_lock);
1434 static void in_intr(struct virtqueue *vq)
1437 unsigned long flags;
1439 port = find_port_by_vq(vq->vdev->priv, vq);
1443 spin_lock_irqsave(&port->inbuf_lock, flags);
1445 port->inbuf = get_inbuf(port);
1448 * Don't queue up data when port is closed. This condition
1449 * can be reached when a console port is not yet connected (no
1450 * tty is spawned) and the host sends out data to console
1451 * ports. For generic serial ports, the host won't
1452 * (shouldn't) send data till the guest is connected.
1454 if (!port->guest_connected)
1455 discard_port_data(port);
1457 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1459 wake_up_interruptible(&port->waitqueue);
1461 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1465 static void control_intr(struct virtqueue *vq)
1467 struct ports_device *portdev;
1469 portdev = vq->vdev->priv;
1470 schedule_work(&portdev->control_work);
1473 static void config_intr(struct virtio_device *vdev)
1475 struct ports_device *portdev;
1477 portdev = vdev->priv;
1479 if (!use_multiport(portdev)) {
1483 vdev->config->get(vdev,
1484 offsetof(struct virtio_console_config, cols),
1485 &cols, sizeof(u16));
1486 vdev->config->get(vdev,
1487 offsetof(struct virtio_console_config, rows),
1488 &rows, sizeof(u16));
1490 port = find_port_by_id(portdev, 0);
1491 set_console_size(port, rows, cols);
1494 * We'll use this way of resizing only for legacy
1495 * support. For newer userspace
1496 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1497 * to indicate console size changes so that it can be
1500 resize_console(port);
1504 static int init_vqs(struct ports_device *portdev)
1506 vq_callback_t **io_callbacks;
1508 struct virtqueue **vqs;
1509 u32 i, j, nr_ports, nr_queues;
1512 nr_ports = portdev->config.max_nr_ports;
1513 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1515 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1520 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1521 if (!io_callbacks) {
1525 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1528 goto free_callbacks;
1530 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1532 if (!portdev->in_vqs) {
1536 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1538 if (!portdev->out_vqs) {
1544 * For backward compat (newer host but older guest), the host
1545 * spawns a console port first and also inits the vqs for port
1549 io_callbacks[j] = in_intr;
1550 io_callbacks[j + 1] = NULL;
1551 io_names[j] = "input";
1552 io_names[j + 1] = "output";
1555 if (use_multiport(portdev)) {
1556 io_callbacks[j] = control_intr;
1557 io_callbacks[j + 1] = NULL;
1558 io_names[j] = "control-i";
1559 io_names[j + 1] = "control-o";
1561 for (i = 1; i < nr_ports; i++) {
1563 io_callbacks[j] = in_intr;
1564 io_callbacks[j + 1] = NULL;
1565 io_names[j] = "input";
1566 io_names[j + 1] = "output";
1569 /* Find the queues. */
1570 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1572 (const char **)io_names);
1577 portdev->in_vqs[0] = vqs[0];
1578 portdev->out_vqs[0] = vqs[1];
1580 if (use_multiport(portdev)) {
1581 portdev->c_ivq = vqs[j];
1582 portdev->c_ovq = vqs[j + 1];
1584 for (i = 1; i < nr_ports; i++) {
1586 portdev->in_vqs[i] = vqs[j];
1587 portdev->out_vqs[i] = vqs[j + 1];
1590 kfree(io_callbacks);
1599 kfree(io_callbacks);
1601 kfree(portdev->out_vqs);
1603 kfree(portdev->in_vqs);
1610 static const struct file_operations portdev_fops = {
1611 .owner = THIS_MODULE,
1615 * Once we're further in boot, we get probed like any other virtio
1618 * If the host also supports multiple console ports, we check the
1619 * config space to see how many ports the host has spawned. We
1620 * initialize each port found.
1622 static int __devinit virtcons_probe(struct virtio_device *vdev)
1624 struct ports_device *portdev;
1628 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1634 /* Attach this portdev to this virtio_device, and vice-versa. */
1635 portdev->vdev = vdev;
1636 vdev->priv = portdev;
1638 spin_lock_irq(&pdrvdata_lock);
1639 portdev->drv_index = pdrvdata.index++;
1640 spin_unlock_irq(&pdrvdata_lock);
1642 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1644 if (portdev->chr_major < 0) {
1646 "Error %d registering chrdev for device %u\n",
1647 portdev->chr_major, portdev->drv_index);
1648 err = portdev->chr_major;
1653 portdev->config.max_nr_ports = 1;
1654 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1656 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1658 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1660 &portdev->config.max_nr_ports,
1661 sizeof(portdev->config.max_nr_ports));
1664 /* Let the Host know we support multiple ports.*/
1665 vdev->config->finalize_features(vdev);
1667 err = init_vqs(portdev);
1669 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
1673 spin_lock_init(&portdev->ports_lock);
1674 INIT_LIST_HEAD(&portdev->ports);
1677 unsigned int nr_added_bufs;
1679 spin_lock_init(&portdev->cvq_lock);
1680 INIT_WORK(&portdev->control_work, &control_work_handler);
1682 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1683 if (!nr_added_bufs) {
1685 "Error allocating buffers for control queue\n");
1691 * For backward compatibility: Create a console port
1692 * if we're running on older host.
1694 add_port(portdev, 0);
1697 spin_lock_irq(&pdrvdata_lock);
1698 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1699 spin_unlock_irq(&pdrvdata_lock);
1701 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1702 VIRTIO_CONSOLE_DEVICE_READY, 1);
1706 /* The host might want to notify mgmt sw about device add failure */
1707 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1708 VIRTIO_CONSOLE_DEVICE_READY, 0);
1709 vdev->config->del_vqs(vdev);
1710 kfree(portdev->in_vqs);
1711 kfree(portdev->out_vqs);
1713 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1720 static void virtcons_remove(struct virtio_device *vdev)
1722 struct ports_device *portdev;
1723 struct port *port, *port2;
1725 portdev = vdev->priv;
1727 spin_lock_irq(&pdrvdata_lock);
1728 list_del(&portdev->list);
1729 spin_unlock_irq(&pdrvdata_lock);
1731 /* Disable interrupts for vqs */
1732 vdev->config->reset(vdev);
1733 /* Finish up work that's lined up */
1734 cancel_work_sync(&portdev->control_work);
1736 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1739 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1741 if (use_multiport(portdev)) {
1742 struct port_buffer *buf;
1745 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1748 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1752 vdev->config->del_vqs(vdev);
1753 kfree(portdev->in_vqs);
1754 kfree(portdev->out_vqs);
1759 static struct virtio_device_id id_table[] = {
1760 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1764 static unsigned int features[] = {
1765 VIRTIO_CONSOLE_F_SIZE,
1766 VIRTIO_CONSOLE_F_MULTIPORT,
1769 static struct virtio_driver virtio_console = {
1770 .feature_table = features,
1771 .feature_table_size = ARRAY_SIZE(features),
1772 .driver.name = KBUILD_MODNAME,
1773 .driver.owner = THIS_MODULE,
1774 .id_table = id_table,
1775 .probe = virtcons_probe,
1776 .remove = virtcons_remove,
1777 .config_changed = config_intr,
1780 static int __init init(void)
1784 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1785 if (IS_ERR(pdrvdata.class)) {
1786 err = PTR_ERR(pdrvdata.class);
1787 pr_err("Error %d creating virtio-ports class\n", err);
1791 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1792 if (!pdrvdata.debugfs_dir) {
1793 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1794 PTR_ERR(pdrvdata.debugfs_dir));
1796 INIT_LIST_HEAD(&pdrvdata.consoles);
1797 INIT_LIST_HEAD(&pdrvdata.portdevs);
1799 return register_virtio_driver(&virtio_console);
1802 static void __exit fini(void)
1804 unregister_virtio_driver(&virtio_console);
1806 class_destroy(pdrvdata.class);
1807 if (pdrvdata.debugfs_dir)
1808 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1813 MODULE_DEVICE_TABLE(virtio, id_table);
1814 MODULE_DESCRIPTION("Virtio console driver");
1815 MODULE_LICENSE("GPL");