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 /* A waitqueue for poll() or blocking read operations */
191 wait_queue_head_t waitqueue;
193 /* The 'name' of the port that we expose via sysfs properties */
196 /* The 'id' to identify the port with the Host */
201 /* Is the host device open */
204 /* We should allow only one process to open a port */
205 bool guest_connected;
208 /* This is the very early arch-specified put chars function. */
209 static int (*early_put_chars)(u32, const char *, int);
211 static struct port *find_port_by_vtermno(u32 vtermno)
214 struct console *cons;
217 spin_lock_irqsave(&pdrvdata_lock, flags);
218 list_for_each_entry(cons, &pdrvdata.consoles, list) {
219 if (cons->vtermno == vtermno) {
220 port = container_of(cons, struct port, cons);
226 spin_unlock_irqrestore(&pdrvdata_lock, flags);
230 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
235 spin_lock_irqsave(&portdev->ports_lock, flags);
236 list_for_each_entry(port, &portdev->ports, list)
241 spin_unlock_irqrestore(&portdev->ports_lock, flags);
246 static struct port *find_port_by_vq(struct ports_device *portdev,
247 struct virtqueue *vq)
252 spin_lock_irqsave(&portdev->ports_lock, flags);
253 list_for_each_entry(port, &portdev->ports, list)
254 if (port->in_vq == vq || port->out_vq == vq)
258 spin_unlock_irqrestore(&portdev->ports_lock, flags);
262 static bool is_console_port(struct port *port)
269 static inline bool use_multiport(struct ports_device *portdev)
272 * This condition can be true when put_chars is called from
277 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
280 static void free_buf(struct port_buffer *buf)
286 static struct port_buffer *alloc_buf(size_t buf_size)
288 struct port_buffer *buf;
290 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
293 buf->buf = kzalloc(buf_size, GFP_KERNEL);
298 buf->size = buf_size;
307 /* Callers should take appropriate locks */
308 static void *get_inbuf(struct port *port)
310 struct port_buffer *buf;
311 struct virtqueue *vq;
315 buf = virtqueue_get_buf(vq, &len);
324 * Create a scatter-gather list representing our input buffer and put
327 * Callers should take appropriate locks.
329 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
331 struct scatterlist sg[1];
334 sg_init_one(sg, buf->buf, buf->size);
336 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
341 /* Discard any unread data this port has. Callers lockers. */
342 static void discard_port_data(struct port *port)
344 struct port_buffer *buf;
345 struct virtqueue *vq;
353 buf = virtqueue_get_buf(vq, &len);
357 if (add_inbuf(vq, buf) < 0) {
361 buf = virtqueue_get_buf(vq, &len);
365 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
369 static bool port_has_data(struct port *port)
374 spin_lock_irqsave(&port->inbuf_lock, flags);
379 port->inbuf = get_inbuf(port);
386 spin_unlock_irqrestore(&port->inbuf_lock, flags);
390 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
391 unsigned int event, unsigned int value)
393 struct scatterlist sg[1];
394 struct virtio_console_control cpkt;
395 struct virtqueue *vq;
398 if (!use_multiport(portdev))
407 sg_init_one(sg, &cpkt, sizeof(cpkt));
408 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
410 while (!virtqueue_get_buf(vq, &len))
416 static ssize_t send_control_msg(struct port *port, unsigned int event,
419 /* Did the port get unplugged before userspace closed it? */
421 return __send_control_msg(port->portdev, port->id, event, value);
425 /* Callers must take the port->outvq_lock */
426 static void reclaim_consumed_buffers(struct port *port)
431 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
433 port->outvq_full = false;
437 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
440 struct scatterlist sg[1];
441 struct virtqueue *out_vq;
446 out_vq = port->out_vq;
448 spin_lock_irqsave(&port->outvq_lock, flags);
450 reclaim_consumed_buffers(port);
452 sg_init_one(sg, in_buf, in_count);
453 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
455 /* Tell Host to go! */
456 virtqueue_kick(out_vq);
464 port->outvq_full = true;
470 * Wait till the host acknowledges it pushed out the data we
471 * sent. This is done for data from the hvc_console; the tty
472 * operations are performed with spinlocks held so we can't
473 * sleep here. An alternative would be to copy the data to a
474 * buffer and relax the spinning requirement. The downside is
475 * we need to kmalloc a GFP_ATOMIC buffer each time the
476 * console driver writes something out.
478 while (!virtqueue_get_buf(out_vq, &len))
481 spin_unlock_irqrestore(&port->outvq_lock, flags);
483 * We're expected to return the amount of data we wrote -- all
490 * Give out the data that's requested from the buffer that we have
493 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
496 struct port_buffer *buf;
499 if (!out_count || !port_has_data(port))
503 out_count = min(out_count, buf->len - buf->offset);
508 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
512 memcpy(out_buf, buf->buf + buf->offset, out_count);
515 buf->offset += out_count;
517 if (buf->offset == buf->len) {
519 * We're done using all the data in this buffer.
520 * Re-queue so that the Host can send us more data.
522 spin_lock_irqsave(&port->inbuf_lock, flags);
525 if (add_inbuf(port->in_vq, buf) < 0)
526 dev_warn(port->dev, "failed add_buf\n");
528 spin_unlock_irqrestore(&port->inbuf_lock, flags);
530 /* Return the number of bytes actually copied */
534 /* The condition that must be true for polling to end */
535 static bool will_read_block(struct port *port)
537 if (!port->guest_connected) {
538 /* Port got hot-unplugged. Let's exit. */
541 return !port_has_data(port) && port->host_connected;
544 static bool will_write_block(struct port *port)
548 if (!port->guest_connected) {
549 /* Port got hot-unplugged. Let's exit. */
552 if (!port->host_connected)
555 spin_lock_irq(&port->outvq_lock);
557 * Check if the Host has consumed any buffers since we last
558 * sent data (this is only applicable for nonblocking ports).
560 reclaim_consumed_buffers(port);
561 ret = port->outvq_full;
562 spin_unlock_irq(&port->outvq_lock);
567 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
568 size_t count, loff_t *offp)
573 port = filp->private_data;
575 if (!port_has_data(port)) {
577 * If nothing's connected on the host just return 0 in
578 * case of list_empty; this tells the userspace app
579 * that there's no connection
581 if (!port->host_connected)
583 if (filp->f_flags & O_NONBLOCK)
586 ret = wait_event_interruptible(port->waitqueue,
587 !will_read_block(port));
591 /* Port got hot-unplugged. */
592 if (!port->guest_connected)
595 * We could've received a disconnection message while we were
596 * waiting for more data.
598 * This check is not clubbed in the if() statement above as we
599 * might receive some data as well as the host could get
600 * disconnected after we got woken up from our wait. So we
601 * really want to give off whatever data we have and only then
602 * check for host_connected.
604 if (!port_has_data(port) && !port->host_connected)
607 return fill_readbuf(port, ubuf, count, true);
610 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
611 size_t count, loff_t *offp)
618 /* Userspace could be out to fool us */
622 port = filp->private_data;
624 nonblock = filp->f_flags & O_NONBLOCK;
626 if (will_write_block(port)) {
630 ret = wait_event_interruptible(port->waitqueue,
631 !will_write_block(port));
635 /* Port got hot-unplugged. */
636 if (!port->guest_connected)
639 count = min((size_t)(32 * 1024), count);
641 buf = kmalloc(count, GFP_KERNEL);
645 ret = copy_from_user(buf, ubuf, count);
652 * We now ask send_buf() to not spin for generic ports -- we
653 * can re-use the same code path that non-blocking file
654 * descriptors take for blocking file descriptors since the
655 * wait is already done and we're certain the write will go
656 * through to the host.
659 ret = send_buf(port, buf, count, nonblock);
661 if (nonblock && ret > 0)
670 static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
675 port = filp->private_data;
676 poll_wait(filp, &port->waitqueue, wait);
678 if (!port->guest_connected) {
679 /* Port got unplugged */
683 if (!will_read_block(port))
684 ret |= POLLIN | POLLRDNORM;
685 if (!will_write_block(port))
687 if (!port->host_connected)
693 static int port_fops_release(struct inode *inode, struct file *filp)
697 port = filp->private_data;
699 /* Notify host of port being closed */
700 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
702 spin_lock_irq(&port->inbuf_lock);
703 port->guest_connected = false;
705 discard_port_data(port);
707 spin_unlock_irq(&port->inbuf_lock);
709 spin_lock_irq(&port->outvq_lock);
710 reclaim_consumed_buffers(port);
711 spin_unlock_irq(&port->outvq_lock);
716 static int port_fops_open(struct inode *inode, struct file *filp)
718 struct cdev *cdev = inode->i_cdev;
722 port = container_of(cdev, struct port, cdev);
723 filp->private_data = port;
726 * Don't allow opening of console port devices -- that's done
729 if (is_console_port(port)) {
734 /* Allow only one process to open a particular port at a time */
735 spin_lock_irq(&port->inbuf_lock);
736 if (port->guest_connected) {
737 spin_unlock_irq(&port->inbuf_lock);
742 port->guest_connected = true;
743 spin_unlock_irq(&port->inbuf_lock);
745 spin_lock_irq(&port->outvq_lock);
747 * There might be a chance that we missed reclaiming a few
748 * buffers in the window of the port getting previously closed
751 reclaim_consumed_buffers(port);
752 spin_unlock_irq(&port->outvq_lock);
754 /* Notify host of port being opened */
755 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
763 * The file operations that we support: programs in the guest can open
764 * a console device, read from it, write to it, poll for data and
765 * close it. The devices are at
766 * /dev/vport<device number>p<port number>
768 static const struct file_operations port_fops = {
769 .owner = THIS_MODULE,
770 .open = port_fops_open,
771 .read = port_fops_read,
772 .write = port_fops_write,
773 .poll = port_fops_poll,
774 .release = port_fops_release,
778 * The put_chars() callback is pretty straightforward.
780 * We turn the characters into a scatter-gather list, add it to the
781 * output queue and then kick the Host. Then we sit here waiting for
782 * it to finish: inefficient in theory, but in practice
783 * implementations will do it immediately (lguest's Launcher does).
785 static int put_chars(u32 vtermno, const char *buf, int count)
789 if (unlikely(early_put_chars))
790 return early_put_chars(vtermno, buf, count);
792 port = find_port_by_vtermno(vtermno);
796 return send_buf(port, (void *)buf, count, false);
800 * get_chars() is the callback from the hvc_console infrastructure
801 * when an interrupt is received.
803 * We call out to fill_readbuf that gets us the required data from the
804 * buffers that are queued up.
806 static int get_chars(u32 vtermno, char *buf, int count)
810 /* If we've not set up the port yet, we have no input to give. */
811 if (unlikely(early_put_chars))
814 port = find_port_by_vtermno(vtermno);
818 /* If we don't have an input queue yet, we can't get input. */
819 BUG_ON(!port->in_vq);
821 return fill_readbuf(port, buf, count, false);
824 static void resize_console(struct port *port)
826 struct virtio_device *vdev;
828 /* The port could have been hot-unplugged */
829 if (!port || !is_console_port(port))
832 vdev = port->portdev->vdev;
833 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
834 hvc_resize(port->cons.hvc, port->cons.ws);
837 /* We set the configuration at this point, since we now have a tty */
838 static int notifier_add_vio(struct hvc_struct *hp, int data)
842 port = find_port_by_vtermno(hp->vtermno);
846 hp->irq_requested = 1;
847 resize_console(port);
852 static void notifier_del_vio(struct hvc_struct *hp, int data)
854 hp->irq_requested = 0;
857 /* The operations for console ports. */
858 static const struct hv_ops hv_ops = {
859 .get_chars = get_chars,
860 .put_chars = put_chars,
861 .notifier_add = notifier_add_vio,
862 .notifier_del = notifier_del_vio,
863 .notifier_hangup = notifier_del_vio,
867 * Console drivers are initialized very early so boot messages can go
868 * out, so we do things slightly differently from the generic virtio
869 * initialization of the net and block drivers.
871 * At this stage, the console is output-only. It's too early to set
872 * up a virtqueue, so we let the drivers do some boutique early-output
875 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
877 early_put_chars = put_chars;
878 return hvc_instantiate(0, 0, &hv_ops);
881 int init_port_console(struct port *port)
886 * The Host's telling us this port is a console port. Hook it
887 * up with an hvc console.
889 * To set up and manage our virtual console, we call
892 * The first argument of hvc_alloc() is the virtual console
893 * number. The second argument is the parameter for the
894 * notification mechanism (like irq number). We currently
895 * leave this as zero, virtqueues have implicit notifications.
897 * The third argument is a "struct hv_ops" containing the
898 * put_chars() get_chars(), notifier_add() and notifier_del()
899 * pointers. The final argument is the output buffer size: we
900 * can do any size, so we put PAGE_SIZE here.
902 port->cons.vtermno = pdrvdata.next_vtermno;
904 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
905 if (IS_ERR(port->cons.hvc)) {
906 ret = PTR_ERR(port->cons.hvc);
908 "error %d allocating hvc for port\n", ret);
909 port->cons.hvc = NULL;
912 spin_lock_irq(&pdrvdata_lock);
913 pdrvdata.next_vtermno++;
914 list_add_tail(&port->cons.list, &pdrvdata.consoles);
915 spin_unlock_irq(&pdrvdata_lock);
916 port->guest_connected = true;
919 * Start using the new console output if this is the first
920 * console to come up.
923 early_put_chars = NULL;
925 /* Notify host of port being opened */
926 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
931 static ssize_t show_port_name(struct device *dev,
932 struct device_attribute *attr, char *buffer)
936 port = dev_get_drvdata(dev);
938 return sprintf(buffer, "%s\n", port->name);
941 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
943 static struct attribute *port_sysfs_entries[] = {
948 static struct attribute_group port_attribute_group = {
949 .name = NULL, /* put in device directory */
950 .attrs = port_sysfs_entries,
953 static int debugfs_open(struct inode *inode, struct file *filp)
955 filp->private_data = inode->i_private;
959 static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
960 size_t count, loff_t *offp)
964 ssize_t ret, out_offset, out_count;
967 buf = kmalloc(out_count, GFP_KERNEL);
971 port = filp->private_data;
973 out_offset += snprintf(buf + out_offset, out_count,
974 "name: %s\n", port->name ? port->name : "");
975 out_offset += snprintf(buf + out_offset, out_count - out_offset,
976 "guest_connected: %d\n", port->guest_connected);
977 out_offset += snprintf(buf + out_offset, out_count - out_offset,
978 "host_connected: %d\n", port->host_connected);
979 out_offset += snprintf(buf + out_offset, out_count - out_offset,
980 "outvq_full: %d\n", port->outvq_full);
981 out_offset += snprintf(buf + out_offset, out_count - out_offset,
983 is_console_port(port) ? "yes" : "no");
984 out_offset += snprintf(buf + out_offset, out_count - out_offset,
985 "console_vtermno: %u\n", port->cons.vtermno);
987 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
992 static const struct file_operations port_debugfs_ops = {
993 .owner = THIS_MODULE,
994 .open = debugfs_open,
995 .read = debugfs_read,
998 static void set_console_size(struct port *port, u16 rows, u16 cols)
1000 if (!port || !is_console_port(port))
1003 port->cons.ws.ws_row = rows;
1004 port->cons.ws.ws_col = cols;
1007 static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1009 struct port_buffer *buf;
1010 unsigned int nr_added_bufs;
1015 buf = alloc_buf(PAGE_SIZE);
1019 spin_lock_irq(lock);
1020 ret = add_inbuf(vq, buf);
1022 spin_unlock_irq(lock);
1027 spin_unlock_irq(lock);
1030 return nr_added_bufs;
1033 static int add_port(struct ports_device *portdev, u32 id)
1035 char debugfs_name[16];
1037 struct port_buffer *buf;
1039 unsigned int nr_added_bufs;
1042 port = kmalloc(sizeof(*port), GFP_KERNEL);
1048 port->portdev = portdev;
1053 port->cons.hvc = NULL;
1055 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1057 port->host_connected = port->guest_connected = false;
1059 port->outvq_full = false;
1061 port->in_vq = portdev->in_vqs[port->id];
1062 port->out_vq = portdev->out_vqs[port->id];
1064 cdev_init(&port->cdev, &port_fops);
1066 devt = MKDEV(portdev->chr_major, id);
1067 err = cdev_add(&port->cdev, devt, 1);
1069 dev_err(&port->portdev->vdev->dev,
1070 "Error %d adding cdev for port %u\n", err, id);
1073 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1074 devt, port, "vport%up%u",
1075 port->portdev->drv_index, id);
1076 if (IS_ERR(port->dev)) {
1077 err = PTR_ERR(port->dev);
1078 dev_err(&port->portdev->vdev->dev,
1079 "Error %d creating device for port %u\n",
1084 spin_lock_init(&port->inbuf_lock);
1085 spin_lock_init(&port->outvq_lock);
1086 init_waitqueue_head(&port->waitqueue);
1088 /* Fill the in_vq with buffers so the host can send us data. */
1089 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1090 if (!nr_added_bufs) {
1091 dev_err(port->dev, "Error allocating inbufs\n");
1097 * If we're not using multiport support, this has to be a console port
1099 if (!use_multiport(port->portdev)) {
1100 err = init_port_console(port);
1105 spin_lock_irq(&portdev->ports_lock);
1106 list_add_tail(&port->list, &port->portdev->ports);
1107 spin_unlock_irq(&portdev->ports_lock);
1110 * Tell the Host we're set so that it can send us various
1111 * configuration parameters for this port (eg, port name,
1112 * caching, whether this is a console port, etc.)
1114 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1116 if (pdrvdata.debugfs_dir) {
1118 * Finally, create the debugfs file that we can use to
1119 * inspect a port's state at any time
1121 sprintf(debugfs_name, "vport%up%u",
1122 port->portdev->drv_index, id);
1123 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1124 pdrvdata.debugfs_dir,
1131 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1134 device_destroy(pdrvdata.class, port->dev->devt);
1136 cdev_del(&port->cdev);
1140 /* The host might want to notify management sw about port add failure */
1141 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1145 /* Remove all port-specific data. */
1146 static void remove_port(struct port *port)
1148 struct port_buffer *buf;
1150 if (port->guest_connected) {
1151 port->guest_connected = false;
1152 port->host_connected = false;
1153 wake_up_interruptible(&port->waitqueue);
1154 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1157 spin_lock_irq(&port->portdev->ports_lock);
1158 list_del(&port->list);
1159 spin_unlock_irq(&port->portdev->ports_lock);
1161 if (is_console_port(port)) {
1162 spin_lock_irq(&pdrvdata_lock);
1163 list_del(&port->cons.list);
1164 spin_unlock_irq(&pdrvdata_lock);
1167 * hvc_remove() not called as removing one hvc port
1168 * results in other hvc ports getting frozen.
1170 * Once this is resolved in hvc, this functionality
1171 * will be enabled. Till that is done, the -EPIPE
1172 * return from get_chars() above will help
1173 * hvc_console.c to clean up on ports we remove here.
1175 hvc_remove(port->cons.hvc);
1178 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1179 device_destroy(pdrvdata.class, port->dev->devt);
1180 cdev_del(&port->cdev);
1182 /* Remove unused data this port might have received. */
1183 discard_port_data(port);
1185 reclaim_consumed_buffers(port);
1187 /* Remove buffers we queued up for the Host to send us data in. */
1188 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1193 debugfs_remove(port->debugfs_file);
1198 /* Any private messages that the Host and Guest want to share */
1199 static void handle_control_message(struct ports_device *portdev,
1200 struct port_buffer *buf)
1202 struct virtio_console_control *cpkt;
1207 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1209 port = find_port_by_id(portdev, cpkt->id);
1210 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
1211 /* No valid header at start of buffer. Drop it. */
1212 dev_dbg(&portdev->vdev->dev,
1213 "Invalid index %u in control packet\n", cpkt->id);
1217 switch (cpkt->event) {
1218 case VIRTIO_CONSOLE_PORT_ADD:
1220 dev_dbg(&portdev->vdev->dev,
1221 "Port %u already added\n", port->id);
1222 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1225 if (cpkt->id >= portdev->config.max_nr_ports) {
1226 dev_warn(&portdev->vdev->dev,
1227 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1228 cpkt->id, portdev->config.max_nr_ports - 1);
1231 add_port(portdev, cpkt->id);
1233 case VIRTIO_CONSOLE_PORT_REMOVE:
1236 case VIRTIO_CONSOLE_CONSOLE_PORT:
1239 if (is_console_port(port))
1242 init_port_console(port);
1244 * Could remove the port here in case init fails - but
1245 * have to notify the host first.
1248 case VIRTIO_CONSOLE_RESIZE: {
1254 if (!is_console_port(port))
1257 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1259 set_console_size(port, size.rows, size.cols);
1261 port->cons.hvc->irq_requested = 1;
1262 resize_console(port);
1265 case VIRTIO_CONSOLE_PORT_OPEN:
1266 port->host_connected = cpkt->value;
1267 wake_up_interruptible(&port->waitqueue);
1269 * If the host port got closed and the host had any
1270 * unconsumed buffers, we'll be able to reclaim them
1273 spin_lock_irq(&port->outvq_lock);
1274 reclaim_consumed_buffers(port);
1275 spin_unlock_irq(&port->outvq_lock);
1277 case VIRTIO_CONSOLE_PORT_NAME:
1279 * Skip the size of the header and the cpkt to get the size
1280 * of the name that was sent
1282 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1284 port->name = kmalloc(name_size, GFP_KERNEL);
1287 "Not enough space to store port name\n");
1290 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1292 port->name[name_size - 1] = 0;
1295 * Since we only have one sysfs attribute, 'name',
1296 * create it only if we have a name for the port.
1298 err = sysfs_create_group(&port->dev->kobj,
1299 &port_attribute_group);
1302 "Error %d creating sysfs device attributes\n",
1306 * Generate a udev event so that appropriate
1307 * symlinks can be created based on udev
1310 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1316 static void control_work_handler(struct work_struct *work)
1318 struct ports_device *portdev;
1319 struct virtqueue *vq;
1320 struct port_buffer *buf;
1323 portdev = container_of(work, struct ports_device, control_work);
1324 vq = portdev->c_ivq;
1326 spin_lock(&portdev->cvq_lock);
1327 while ((buf = virtqueue_get_buf(vq, &len))) {
1328 spin_unlock(&portdev->cvq_lock);
1333 handle_control_message(portdev, buf);
1335 spin_lock(&portdev->cvq_lock);
1336 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1337 dev_warn(&portdev->vdev->dev,
1338 "Error adding buffer to queue\n");
1342 spin_unlock(&portdev->cvq_lock);
1345 static void in_intr(struct virtqueue *vq)
1348 unsigned long flags;
1350 port = find_port_by_vq(vq->vdev->priv, vq);
1354 spin_lock_irqsave(&port->inbuf_lock, flags);
1356 port->inbuf = get_inbuf(port);
1359 * Don't queue up data when port is closed. This condition
1360 * can be reached when a console port is not yet connected (no
1361 * tty is spawned) and the host sends out data to console
1362 * ports. For generic serial ports, the host won't
1363 * (shouldn't) send data till the guest is connected.
1365 if (!port->guest_connected)
1366 discard_port_data(port);
1368 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1370 wake_up_interruptible(&port->waitqueue);
1372 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1376 static void control_intr(struct virtqueue *vq)
1378 struct ports_device *portdev;
1380 portdev = vq->vdev->priv;
1381 schedule_work(&portdev->control_work);
1384 static void config_intr(struct virtio_device *vdev)
1386 struct ports_device *portdev;
1388 portdev = vdev->priv;
1390 if (!use_multiport(portdev)) {
1394 vdev->config->get(vdev,
1395 offsetof(struct virtio_console_config, cols),
1396 &cols, sizeof(u16));
1397 vdev->config->get(vdev,
1398 offsetof(struct virtio_console_config, rows),
1399 &rows, sizeof(u16));
1401 port = find_port_by_id(portdev, 0);
1402 set_console_size(port, rows, cols);
1405 * We'll use this way of resizing only for legacy
1406 * support. For newer userspace
1407 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1408 * to indicate console size changes so that it can be
1411 resize_console(port);
1415 static int init_vqs(struct ports_device *portdev)
1417 vq_callback_t **io_callbacks;
1419 struct virtqueue **vqs;
1420 u32 i, j, nr_ports, nr_queues;
1423 nr_ports = portdev->config.max_nr_ports;
1424 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1426 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1431 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1432 if (!io_callbacks) {
1436 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1439 goto free_callbacks;
1441 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1443 if (!portdev->in_vqs) {
1447 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1449 if (!portdev->out_vqs) {
1455 * For backward compat (newer host but older guest), the host
1456 * spawns a console port first and also inits the vqs for port
1460 io_callbacks[j] = in_intr;
1461 io_callbacks[j + 1] = NULL;
1462 io_names[j] = "input";
1463 io_names[j + 1] = "output";
1466 if (use_multiport(portdev)) {
1467 io_callbacks[j] = control_intr;
1468 io_callbacks[j + 1] = NULL;
1469 io_names[j] = "control-i";
1470 io_names[j + 1] = "control-o";
1472 for (i = 1; i < nr_ports; i++) {
1474 io_callbacks[j] = in_intr;
1475 io_callbacks[j + 1] = NULL;
1476 io_names[j] = "input";
1477 io_names[j + 1] = "output";
1480 /* Find the queues. */
1481 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1483 (const char **)io_names);
1488 portdev->in_vqs[0] = vqs[0];
1489 portdev->out_vqs[0] = vqs[1];
1491 if (use_multiport(portdev)) {
1492 portdev->c_ivq = vqs[j];
1493 portdev->c_ovq = vqs[j + 1];
1495 for (i = 1; i < nr_ports; i++) {
1497 portdev->in_vqs[i] = vqs[j];
1498 portdev->out_vqs[i] = vqs[j + 1];
1501 kfree(io_callbacks);
1510 kfree(io_callbacks);
1512 kfree(portdev->out_vqs);
1514 kfree(portdev->in_vqs);
1521 static const struct file_operations portdev_fops = {
1522 .owner = THIS_MODULE,
1526 * Once we're further in boot, we get probed like any other virtio
1529 * If the host also supports multiple console ports, we check the
1530 * config space to see how many ports the host has spawned. We
1531 * initialize each port found.
1533 static int __devinit virtcons_probe(struct virtio_device *vdev)
1535 struct ports_device *portdev;
1539 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1545 /* Attach this portdev to this virtio_device, and vice-versa. */
1546 portdev->vdev = vdev;
1547 vdev->priv = portdev;
1549 spin_lock_irq(&pdrvdata_lock);
1550 portdev->drv_index = pdrvdata.index++;
1551 spin_unlock_irq(&pdrvdata_lock);
1553 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1555 if (portdev->chr_major < 0) {
1557 "Error %d registering chrdev for device %u\n",
1558 portdev->chr_major, portdev->drv_index);
1559 err = portdev->chr_major;
1564 portdev->config.max_nr_ports = 1;
1565 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1567 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1569 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1571 &portdev->config.max_nr_ports,
1572 sizeof(portdev->config.max_nr_ports));
1575 /* Let the Host know we support multiple ports.*/
1576 vdev->config->finalize_features(vdev);
1578 err = init_vqs(portdev);
1580 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
1584 spin_lock_init(&portdev->ports_lock);
1585 INIT_LIST_HEAD(&portdev->ports);
1588 unsigned int nr_added_bufs;
1590 spin_lock_init(&portdev->cvq_lock);
1591 INIT_WORK(&portdev->control_work, &control_work_handler);
1593 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1594 if (!nr_added_bufs) {
1596 "Error allocating buffers for control queue\n");
1602 * For backward compatibility: Create a console port
1603 * if we're running on older host.
1605 add_port(portdev, 0);
1608 spin_lock_irq(&pdrvdata_lock);
1609 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1610 spin_unlock_irq(&pdrvdata_lock);
1612 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1613 VIRTIO_CONSOLE_DEVICE_READY, 1);
1617 /* The host might want to notify mgmt sw about device add failure */
1618 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1619 VIRTIO_CONSOLE_DEVICE_READY, 0);
1620 vdev->config->del_vqs(vdev);
1621 kfree(portdev->in_vqs);
1622 kfree(portdev->out_vqs);
1624 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1631 static void virtcons_remove(struct virtio_device *vdev)
1633 struct ports_device *portdev;
1634 struct port *port, *port2;
1636 portdev = vdev->priv;
1638 spin_lock_irq(&pdrvdata_lock);
1639 list_del(&portdev->list);
1640 spin_unlock_irq(&pdrvdata_lock);
1642 /* Disable interrupts for vqs */
1643 vdev->config->reset(vdev);
1644 /* Finish up work that's lined up */
1645 cancel_work_sync(&portdev->control_work);
1647 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1650 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1652 if (use_multiport(portdev)) {
1653 struct port_buffer *buf;
1656 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1659 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1663 vdev->config->del_vqs(vdev);
1664 kfree(portdev->in_vqs);
1665 kfree(portdev->out_vqs);
1670 static struct virtio_device_id id_table[] = {
1671 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1675 static unsigned int features[] = {
1676 VIRTIO_CONSOLE_F_SIZE,
1677 VIRTIO_CONSOLE_F_MULTIPORT,
1680 static struct virtio_driver virtio_console = {
1681 .feature_table = features,
1682 .feature_table_size = ARRAY_SIZE(features),
1683 .driver.name = KBUILD_MODNAME,
1684 .driver.owner = THIS_MODULE,
1685 .id_table = id_table,
1686 .probe = virtcons_probe,
1687 .remove = virtcons_remove,
1688 .config_changed = config_intr,
1691 static int __init init(void)
1695 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1696 if (IS_ERR(pdrvdata.class)) {
1697 err = PTR_ERR(pdrvdata.class);
1698 pr_err("Error %d creating virtio-ports class\n", err);
1702 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1703 if (!pdrvdata.debugfs_dir) {
1704 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1705 PTR_ERR(pdrvdata.debugfs_dir));
1707 INIT_LIST_HEAD(&pdrvdata.consoles);
1708 INIT_LIST_HEAD(&pdrvdata.portdevs);
1710 return register_virtio_driver(&virtio_console);
1713 static void __exit fini(void)
1715 unregister_virtio_driver(&virtio_console);
1717 class_destroy(pdrvdata.class);
1718 if (pdrvdata.debugfs_dir)
1719 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1724 MODULE_DEVICE_TABLE(virtio, id_table);
1725 MODULE_DESCRIPTION("Virtio console driver");
1726 MODULE_LICENSE("GPL");