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_devt_in_portdev(struct ports_device *portdev,
236 spin_lock_irqsave(&portdev->ports_lock, flags);
237 list_for_each_entry(port, &portdev->ports, list)
238 if (port->cdev.dev == dev)
242 spin_unlock_irqrestore(&portdev->ports_lock, flags);
247 static struct port *find_port_by_devt(dev_t dev)
249 struct ports_device *portdev;
253 spin_lock_irqsave(&pdrvdata_lock, flags);
254 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
255 port = find_port_by_devt_in_portdev(portdev, dev);
261 spin_unlock_irqrestore(&pdrvdata_lock, flags);
265 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
270 spin_lock_irqsave(&portdev->ports_lock, flags);
271 list_for_each_entry(port, &portdev->ports, list)
276 spin_unlock_irqrestore(&portdev->ports_lock, flags);
281 static struct port *find_port_by_vq(struct ports_device *portdev,
282 struct virtqueue *vq)
287 spin_lock_irqsave(&portdev->ports_lock, flags);
288 list_for_each_entry(port, &portdev->ports, list)
289 if (port->in_vq == vq || port->out_vq == vq)
293 spin_unlock_irqrestore(&portdev->ports_lock, flags);
297 static bool is_console_port(struct port *port)
304 static inline bool use_multiport(struct ports_device *portdev)
307 * This condition can be true when put_chars is called from
312 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
315 static void free_buf(struct port_buffer *buf)
321 static struct port_buffer *alloc_buf(size_t buf_size)
323 struct port_buffer *buf;
325 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
328 buf->buf = kzalloc(buf_size, GFP_KERNEL);
333 buf->size = buf_size;
342 /* Callers should take appropriate locks */
343 static void *get_inbuf(struct port *port)
345 struct port_buffer *buf;
346 struct virtqueue *vq;
350 buf = virtqueue_get_buf(vq, &len);
359 * Create a scatter-gather list representing our input buffer and put
362 * Callers should take appropriate locks.
364 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
366 struct scatterlist sg[1];
369 sg_init_one(sg, buf->buf, buf->size);
371 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
376 /* Discard any unread data this port has. Callers lockers. */
377 static void discard_port_data(struct port *port)
379 struct port_buffer *buf;
380 struct virtqueue *vq;
388 buf = virtqueue_get_buf(vq, &len);
392 if (add_inbuf(vq, buf) < 0) {
396 buf = virtqueue_get_buf(vq, &len);
400 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
404 static bool port_has_data(struct port *port)
409 spin_lock_irqsave(&port->inbuf_lock, flags);
414 port->inbuf = get_inbuf(port);
421 spin_unlock_irqrestore(&port->inbuf_lock, flags);
425 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
426 unsigned int event, unsigned int value)
428 struct scatterlist sg[1];
429 struct virtio_console_control cpkt;
430 struct virtqueue *vq;
433 if (!use_multiport(portdev))
442 sg_init_one(sg, &cpkt, sizeof(cpkt));
443 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
445 while (!virtqueue_get_buf(vq, &len))
451 static ssize_t send_control_msg(struct port *port, unsigned int event,
454 /* Did the port get unplugged before userspace closed it? */
456 return __send_control_msg(port->portdev, port->id, event, value);
460 /* Callers must take the port->outvq_lock */
461 static void reclaim_consumed_buffers(struct port *port)
466 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
468 port->outvq_full = false;
472 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
475 struct scatterlist sg[1];
476 struct virtqueue *out_vq;
481 out_vq = port->out_vq;
483 spin_lock_irqsave(&port->outvq_lock, flags);
485 reclaim_consumed_buffers(port);
487 sg_init_one(sg, in_buf, in_count);
488 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
490 /* Tell Host to go! */
491 virtqueue_kick(out_vq);
499 port->outvq_full = true;
505 * Wait till the host acknowledges it pushed out the data we
506 * sent. This is done for data from the hvc_console; the tty
507 * operations are performed with spinlocks held so we can't
508 * sleep here. An alternative would be to copy the data to a
509 * buffer and relax the spinning requirement. The downside is
510 * we need to kmalloc a GFP_ATOMIC buffer each time the
511 * console driver writes something out.
513 while (!virtqueue_get_buf(out_vq, &len))
516 spin_unlock_irqrestore(&port->outvq_lock, flags);
518 * We're expected to return the amount of data we wrote -- all
525 * Give out the data that's requested from the buffer that we have
528 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
531 struct port_buffer *buf;
534 if (!out_count || !port_has_data(port))
538 out_count = min(out_count, buf->len - buf->offset);
543 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
547 memcpy(out_buf, buf->buf + buf->offset, out_count);
550 buf->offset += out_count;
552 if (buf->offset == buf->len) {
554 * We're done using all the data in this buffer.
555 * Re-queue so that the Host can send us more data.
557 spin_lock_irqsave(&port->inbuf_lock, flags);
560 if (add_inbuf(port->in_vq, buf) < 0)
561 dev_warn(port->dev, "failed add_buf\n");
563 spin_unlock_irqrestore(&port->inbuf_lock, flags);
565 /* Return the number of bytes actually copied */
569 /* The condition that must be true for polling to end */
570 static bool will_read_block(struct port *port)
572 if (!port->guest_connected) {
573 /* Port got hot-unplugged. Let's exit. */
576 return !port_has_data(port) && port->host_connected;
579 static bool will_write_block(struct port *port)
583 if (!port->guest_connected) {
584 /* Port got hot-unplugged. Let's exit. */
587 if (!port->host_connected)
590 spin_lock_irq(&port->outvq_lock);
592 * Check if the Host has consumed any buffers since we last
593 * sent data (this is only applicable for nonblocking ports).
595 reclaim_consumed_buffers(port);
596 ret = port->outvq_full;
597 spin_unlock_irq(&port->outvq_lock);
602 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
603 size_t count, loff_t *offp)
608 port = filp->private_data;
610 if (!port_has_data(port)) {
612 * If nothing's connected on the host just return 0 in
613 * case of list_empty; this tells the userspace app
614 * that there's no connection
616 if (!port->host_connected)
618 if (filp->f_flags & O_NONBLOCK)
621 ret = wait_event_interruptible(port->waitqueue,
622 !will_read_block(port));
626 /* Port got hot-unplugged. */
627 if (!port->guest_connected)
630 * We could've received a disconnection message while we were
631 * waiting for more data.
633 * This check is not clubbed in the if() statement above as we
634 * might receive some data as well as the host could get
635 * disconnected after we got woken up from our wait. So we
636 * really want to give off whatever data we have and only then
637 * check for host_connected.
639 if (!port_has_data(port) && !port->host_connected)
642 return fill_readbuf(port, ubuf, count, true);
645 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
646 size_t count, loff_t *offp)
653 /* Userspace could be out to fool us */
657 port = filp->private_data;
659 nonblock = filp->f_flags & O_NONBLOCK;
661 if (will_write_block(port)) {
665 ret = wait_event_interruptible(port->waitqueue,
666 !will_write_block(port));
670 /* Port got hot-unplugged. */
671 if (!port->guest_connected)
674 count = min((size_t)(32 * 1024), count);
676 buf = kmalloc(count, GFP_KERNEL);
680 ret = copy_from_user(buf, ubuf, count);
687 * We now ask send_buf() to not spin for generic ports -- we
688 * can re-use the same code path that non-blocking file
689 * descriptors take for blocking file descriptors since the
690 * wait is already done and we're certain the write will go
691 * through to the host.
694 ret = send_buf(port, buf, count, nonblock);
696 if (nonblock && ret > 0)
705 static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
710 port = filp->private_data;
711 poll_wait(filp, &port->waitqueue, wait);
713 if (!port->guest_connected) {
714 /* Port got unplugged */
718 if (!will_read_block(port))
719 ret |= POLLIN | POLLRDNORM;
720 if (!will_write_block(port))
722 if (!port->host_connected)
728 static int port_fops_release(struct inode *inode, struct file *filp)
732 port = filp->private_data;
734 /* Notify host of port being closed */
735 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
737 spin_lock_irq(&port->inbuf_lock);
738 port->guest_connected = false;
740 discard_port_data(port);
742 spin_unlock_irq(&port->inbuf_lock);
744 spin_lock_irq(&port->outvq_lock);
745 reclaim_consumed_buffers(port);
746 spin_unlock_irq(&port->outvq_lock);
751 static int port_fops_open(struct inode *inode, struct file *filp)
753 struct cdev *cdev = inode->i_cdev;
757 port = find_port_by_devt(cdev->dev);
758 filp->private_data = port;
761 * Don't allow opening of console port devices -- that's done
764 if (is_console_port(port)) {
769 /* Allow only one process to open a particular port at a time */
770 spin_lock_irq(&port->inbuf_lock);
771 if (port->guest_connected) {
772 spin_unlock_irq(&port->inbuf_lock);
777 port->guest_connected = true;
778 spin_unlock_irq(&port->inbuf_lock);
780 spin_lock_irq(&port->outvq_lock);
782 * There might be a chance that we missed reclaiming a few
783 * buffers in the window of the port getting previously closed
786 reclaim_consumed_buffers(port);
787 spin_unlock_irq(&port->outvq_lock);
789 /* Notify host of port being opened */
790 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
798 * The file operations that we support: programs in the guest can open
799 * a console device, read from it, write to it, poll for data and
800 * close it. The devices are at
801 * /dev/vport<device number>p<port number>
803 static const struct file_operations port_fops = {
804 .owner = THIS_MODULE,
805 .open = port_fops_open,
806 .read = port_fops_read,
807 .write = port_fops_write,
808 .poll = port_fops_poll,
809 .release = port_fops_release,
813 * The put_chars() callback is pretty straightforward.
815 * We turn the characters into a scatter-gather list, add it to the
816 * output queue and then kick the Host. Then we sit here waiting for
817 * it to finish: inefficient in theory, but in practice
818 * implementations will do it immediately (lguest's Launcher does).
820 static int put_chars(u32 vtermno, const char *buf, int count)
824 if (unlikely(early_put_chars))
825 return early_put_chars(vtermno, buf, count);
827 port = find_port_by_vtermno(vtermno);
831 return send_buf(port, (void *)buf, count, false);
835 * get_chars() is the callback from the hvc_console infrastructure
836 * when an interrupt is received.
838 * We call out to fill_readbuf that gets us the required data from the
839 * buffers that are queued up.
841 static int get_chars(u32 vtermno, char *buf, int count)
845 /* If we've not set up the port yet, we have no input to give. */
846 if (unlikely(early_put_chars))
849 port = find_port_by_vtermno(vtermno);
853 /* If we don't have an input queue yet, we can't get input. */
854 BUG_ON(!port->in_vq);
856 return fill_readbuf(port, buf, count, false);
859 static void resize_console(struct port *port)
861 struct virtio_device *vdev;
863 /* The port could have been hot-unplugged */
864 if (!port || !is_console_port(port))
867 vdev = port->portdev->vdev;
868 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
869 hvc_resize(port->cons.hvc, port->cons.ws);
872 /* We set the configuration at this point, since we now have a tty */
873 static int notifier_add_vio(struct hvc_struct *hp, int data)
877 port = find_port_by_vtermno(hp->vtermno);
881 hp->irq_requested = 1;
882 resize_console(port);
887 static void notifier_del_vio(struct hvc_struct *hp, int data)
889 hp->irq_requested = 0;
892 /* The operations for console ports. */
893 static const struct hv_ops hv_ops = {
894 .get_chars = get_chars,
895 .put_chars = put_chars,
896 .notifier_add = notifier_add_vio,
897 .notifier_del = notifier_del_vio,
898 .notifier_hangup = notifier_del_vio,
902 * Console drivers are initialized very early so boot messages can go
903 * out, so we do things slightly differently from the generic virtio
904 * initialization of the net and block drivers.
906 * At this stage, the console is output-only. It's too early to set
907 * up a virtqueue, so we let the drivers do some boutique early-output
910 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
912 early_put_chars = put_chars;
913 return hvc_instantiate(0, 0, &hv_ops);
916 int init_port_console(struct port *port)
921 * The Host's telling us this port is a console port. Hook it
922 * up with an hvc console.
924 * To set up and manage our virtual console, we call
927 * The first argument of hvc_alloc() is the virtual console
928 * number. The second argument is the parameter for the
929 * notification mechanism (like irq number). We currently
930 * leave this as zero, virtqueues have implicit notifications.
932 * The third argument is a "struct hv_ops" containing the
933 * put_chars() get_chars(), notifier_add() and notifier_del()
934 * pointers. The final argument is the output buffer size: we
935 * can do any size, so we put PAGE_SIZE here.
937 port->cons.vtermno = pdrvdata.next_vtermno;
939 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
940 if (IS_ERR(port->cons.hvc)) {
941 ret = PTR_ERR(port->cons.hvc);
943 "error %d allocating hvc for port\n", ret);
944 port->cons.hvc = NULL;
947 spin_lock_irq(&pdrvdata_lock);
948 pdrvdata.next_vtermno++;
949 list_add_tail(&port->cons.list, &pdrvdata.consoles);
950 spin_unlock_irq(&pdrvdata_lock);
951 port->guest_connected = true;
954 * Start using the new console output if this is the first
955 * console to come up.
958 early_put_chars = NULL;
960 /* Notify host of port being opened */
961 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
966 static ssize_t show_port_name(struct device *dev,
967 struct device_attribute *attr, char *buffer)
971 port = dev_get_drvdata(dev);
973 return sprintf(buffer, "%s\n", port->name);
976 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
978 static struct attribute *port_sysfs_entries[] = {
983 static struct attribute_group port_attribute_group = {
984 .name = NULL, /* put in device directory */
985 .attrs = port_sysfs_entries,
988 static int debugfs_open(struct inode *inode, struct file *filp)
990 filp->private_data = inode->i_private;
994 static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
995 size_t count, loff_t *offp)
999 ssize_t ret, out_offset, out_count;
1002 buf = kmalloc(out_count, GFP_KERNEL);
1006 port = filp->private_data;
1008 out_offset += snprintf(buf + out_offset, out_count,
1009 "name: %s\n", port->name ? port->name : "");
1010 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1011 "guest_connected: %d\n", port->guest_connected);
1012 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1013 "host_connected: %d\n", port->host_connected);
1014 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1015 "outvq_full: %d\n", port->outvq_full);
1016 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1018 is_console_port(port) ? "yes" : "no");
1019 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1020 "console_vtermno: %u\n", port->cons.vtermno);
1022 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1027 static const struct file_operations port_debugfs_ops = {
1028 .owner = THIS_MODULE,
1029 .open = debugfs_open,
1030 .read = debugfs_read,
1033 static void set_console_size(struct port *port, u16 rows, u16 cols)
1035 if (!port || !is_console_port(port))
1038 port->cons.ws.ws_row = rows;
1039 port->cons.ws.ws_col = cols;
1042 static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1044 struct port_buffer *buf;
1045 unsigned int nr_added_bufs;
1050 buf = alloc_buf(PAGE_SIZE);
1054 spin_lock_irq(lock);
1055 ret = add_inbuf(vq, buf);
1057 spin_unlock_irq(lock);
1062 spin_unlock_irq(lock);
1065 return nr_added_bufs;
1068 static int add_port(struct ports_device *portdev, u32 id)
1070 char debugfs_name[16];
1072 struct port_buffer *buf;
1074 unsigned int nr_added_bufs;
1077 port = kmalloc(sizeof(*port), GFP_KERNEL);
1083 port->portdev = portdev;
1088 port->cons.hvc = NULL;
1090 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1092 port->host_connected = port->guest_connected = false;
1094 port->outvq_full = false;
1096 port->in_vq = portdev->in_vqs[port->id];
1097 port->out_vq = portdev->out_vqs[port->id];
1099 cdev_init(&port->cdev, &port_fops);
1101 devt = MKDEV(portdev->chr_major, id);
1102 err = cdev_add(&port->cdev, devt, 1);
1104 dev_err(&port->portdev->vdev->dev,
1105 "Error %d adding cdev for port %u\n", err, id);
1108 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1109 devt, port, "vport%up%u",
1110 port->portdev->drv_index, id);
1111 if (IS_ERR(port->dev)) {
1112 err = PTR_ERR(port->dev);
1113 dev_err(&port->portdev->vdev->dev,
1114 "Error %d creating device for port %u\n",
1119 spin_lock_init(&port->inbuf_lock);
1120 spin_lock_init(&port->outvq_lock);
1121 init_waitqueue_head(&port->waitqueue);
1123 /* Fill the in_vq with buffers so the host can send us data. */
1124 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1125 if (!nr_added_bufs) {
1126 dev_err(port->dev, "Error allocating inbufs\n");
1132 * If we're not using multiport support, this has to be a console port
1134 if (!use_multiport(port->portdev)) {
1135 err = init_port_console(port);
1140 spin_lock_irq(&portdev->ports_lock);
1141 list_add_tail(&port->list, &port->portdev->ports);
1142 spin_unlock_irq(&portdev->ports_lock);
1145 * Tell the Host we're set so that it can send us various
1146 * configuration parameters for this port (eg, port name,
1147 * caching, whether this is a console port, etc.)
1149 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1151 if (pdrvdata.debugfs_dir) {
1153 * Finally, create the debugfs file that we can use to
1154 * inspect a port's state at any time
1156 sprintf(debugfs_name, "vport%up%u",
1157 port->portdev->drv_index, id);
1158 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1159 pdrvdata.debugfs_dir,
1166 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1169 device_destroy(pdrvdata.class, port->dev->devt);
1171 cdev_del(&port->cdev);
1175 /* The host might want to notify management sw about port add failure */
1176 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1180 /* Remove all port-specific data. */
1181 static void remove_port(struct port *port)
1183 struct port_buffer *buf;
1185 if (port->guest_connected) {
1186 port->guest_connected = false;
1187 port->host_connected = false;
1188 wake_up_interruptible(&port->waitqueue);
1189 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1192 spin_lock_irq(&port->portdev->ports_lock);
1193 list_del(&port->list);
1194 spin_unlock_irq(&port->portdev->ports_lock);
1196 if (is_console_port(port)) {
1197 spin_lock_irq(&pdrvdata_lock);
1198 list_del(&port->cons.list);
1199 spin_unlock_irq(&pdrvdata_lock);
1202 * hvc_remove() not called as removing one hvc port
1203 * results in other hvc ports getting frozen.
1205 * Once this is resolved in hvc, this functionality
1206 * will be enabled. Till that is done, the -EPIPE
1207 * return from get_chars() above will help
1208 * hvc_console.c to clean up on ports we remove here.
1210 hvc_remove(port->cons.hvc);
1213 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1214 device_destroy(pdrvdata.class, port->dev->devt);
1215 cdev_del(&port->cdev);
1217 /* Remove unused data this port might have received. */
1218 discard_port_data(port);
1220 reclaim_consumed_buffers(port);
1222 /* Remove buffers we queued up for the Host to send us data in. */
1223 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1228 debugfs_remove(port->debugfs_file);
1233 /* Any private messages that the Host and Guest want to share */
1234 static void handle_control_message(struct ports_device *portdev,
1235 struct port_buffer *buf)
1237 struct virtio_console_control *cpkt;
1242 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1244 port = find_port_by_id(portdev, cpkt->id);
1245 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
1246 /* No valid header at start of buffer. Drop it. */
1247 dev_dbg(&portdev->vdev->dev,
1248 "Invalid index %u in control packet\n", cpkt->id);
1252 switch (cpkt->event) {
1253 case VIRTIO_CONSOLE_PORT_ADD:
1255 dev_dbg(&portdev->vdev->dev,
1256 "Port %u already added\n", port->id);
1257 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1260 if (cpkt->id >= portdev->config.max_nr_ports) {
1261 dev_warn(&portdev->vdev->dev,
1262 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1263 cpkt->id, portdev->config.max_nr_ports - 1);
1266 add_port(portdev, cpkt->id);
1268 case VIRTIO_CONSOLE_PORT_REMOVE:
1271 case VIRTIO_CONSOLE_CONSOLE_PORT:
1274 if (is_console_port(port))
1277 init_port_console(port);
1279 * Could remove the port here in case init fails - but
1280 * have to notify the host first.
1283 case VIRTIO_CONSOLE_RESIZE: {
1289 if (!is_console_port(port))
1292 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1294 set_console_size(port, size.rows, size.cols);
1296 port->cons.hvc->irq_requested = 1;
1297 resize_console(port);
1300 case VIRTIO_CONSOLE_PORT_OPEN:
1301 port->host_connected = cpkt->value;
1302 wake_up_interruptible(&port->waitqueue);
1304 * If the host port got closed and the host had any
1305 * unconsumed buffers, we'll be able to reclaim them
1308 spin_lock_irq(&port->outvq_lock);
1309 reclaim_consumed_buffers(port);
1310 spin_unlock_irq(&port->outvq_lock);
1312 case VIRTIO_CONSOLE_PORT_NAME:
1314 * Skip the size of the header and the cpkt to get the size
1315 * of the name that was sent
1317 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1319 port->name = kmalloc(name_size, GFP_KERNEL);
1322 "Not enough space to store port name\n");
1325 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1327 port->name[name_size - 1] = 0;
1330 * Since we only have one sysfs attribute, 'name',
1331 * create it only if we have a name for the port.
1333 err = sysfs_create_group(&port->dev->kobj,
1334 &port_attribute_group);
1337 "Error %d creating sysfs device attributes\n",
1341 * Generate a udev event so that appropriate
1342 * symlinks can be created based on udev
1345 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1351 static void control_work_handler(struct work_struct *work)
1353 struct ports_device *portdev;
1354 struct virtqueue *vq;
1355 struct port_buffer *buf;
1358 portdev = container_of(work, struct ports_device, control_work);
1359 vq = portdev->c_ivq;
1361 spin_lock(&portdev->cvq_lock);
1362 while ((buf = virtqueue_get_buf(vq, &len))) {
1363 spin_unlock(&portdev->cvq_lock);
1368 handle_control_message(portdev, buf);
1370 spin_lock(&portdev->cvq_lock);
1371 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1372 dev_warn(&portdev->vdev->dev,
1373 "Error adding buffer to queue\n");
1377 spin_unlock(&portdev->cvq_lock);
1380 static void in_intr(struct virtqueue *vq)
1383 unsigned long flags;
1385 port = find_port_by_vq(vq->vdev->priv, vq);
1389 spin_lock_irqsave(&port->inbuf_lock, flags);
1391 port->inbuf = get_inbuf(port);
1394 * Don't queue up data when port is closed. This condition
1395 * can be reached when a console port is not yet connected (no
1396 * tty is spawned) and the host sends out data to console
1397 * ports. For generic serial ports, the host won't
1398 * (shouldn't) send data till the guest is connected.
1400 if (!port->guest_connected)
1401 discard_port_data(port);
1403 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1405 wake_up_interruptible(&port->waitqueue);
1407 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1411 static void control_intr(struct virtqueue *vq)
1413 struct ports_device *portdev;
1415 portdev = vq->vdev->priv;
1416 schedule_work(&portdev->control_work);
1419 static void config_intr(struct virtio_device *vdev)
1421 struct ports_device *portdev;
1423 portdev = vdev->priv;
1425 if (!use_multiport(portdev)) {
1429 vdev->config->get(vdev,
1430 offsetof(struct virtio_console_config, cols),
1431 &cols, sizeof(u16));
1432 vdev->config->get(vdev,
1433 offsetof(struct virtio_console_config, rows),
1434 &rows, sizeof(u16));
1436 port = find_port_by_id(portdev, 0);
1437 set_console_size(port, rows, cols);
1440 * We'll use this way of resizing only for legacy
1441 * support. For newer userspace
1442 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1443 * to indicate console size changes so that it can be
1446 resize_console(port);
1450 static int init_vqs(struct ports_device *portdev)
1452 vq_callback_t **io_callbacks;
1454 struct virtqueue **vqs;
1455 u32 i, j, nr_ports, nr_queues;
1458 nr_ports = portdev->config.max_nr_ports;
1459 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1461 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1466 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1467 if (!io_callbacks) {
1471 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1474 goto free_callbacks;
1476 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1478 if (!portdev->in_vqs) {
1482 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1484 if (!portdev->out_vqs) {
1490 * For backward compat (newer host but older guest), the host
1491 * spawns a console port first and also inits the vqs for port
1495 io_callbacks[j] = in_intr;
1496 io_callbacks[j + 1] = NULL;
1497 io_names[j] = "input";
1498 io_names[j + 1] = "output";
1501 if (use_multiport(portdev)) {
1502 io_callbacks[j] = control_intr;
1503 io_callbacks[j + 1] = NULL;
1504 io_names[j] = "control-i";
1505 io_names[j + 1] = "control-o";
1507 for (i = 1; i < nr_ports; i++) {
1509 io_callbacks[j] = in_intr;
1510 io_callbacks[j + 1] = NULL;
1511 io_names[j] = "input";
1512 io_names[j + 1] = "output";
1515 /* Find the queues. */
1516 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1518 (const char **)io_names);
1523 portdev->in_vqs[0] = vqs[0];
1524 portdev->out_vqs[0] = vqs[1];
1526 if (use_multiport(portdev)) {
1527 portdev->c_ivq = vqs[j];
1528 portdev->c_ovq = vqs[j + 1];
1530 for (i = 1; i < nr_ports; i++) {
1532 portdev->in_vqs[i] = vqs[j];
1533 portdev->out_vqs[i] = vqs[j + 1];
1536 kfree(io_callbacks);
1545 kfree(io_callbacks);
1547 kfree(portdev->out_vqs);
1549 kfree(portdev->in_vqs);
1556 static const struct file_operations portdev_fops = {
1557 .owner = THIS_MODULE,
1561 * Once we're further in boot, we get probed like any other virtio
1564 * If the host also supports multiple console ports, we check the
1565 * config space to see how many ports the host has spawned. We
1566 * initialize each port found.
1568 static int __devinit virtcons_probe(struct virtio_device *vdev)
1570 struct ports_device *portdev;
1574 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1580 /* Attach this portdev to this virtio_device, and vice-versa. */
1581 portdev->vdev = vdev;
1582 vdev->priv = portdev;
1584 spin_lock_irq(&pdrvdata_lock);
1585 portdev->drv_index = pdrvdata.index++;
1586 spin_unlock_irq(&pdrvdata_lock);
1588 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1590 if (portdev->chr_major < 0) {
1592 "Error %d registering chrdev for device %u\n",
1593 portdev->chr_major, portdev->drv_index);
1594 err = portdev->chr_major;
1599 portdev->config.max_nr_ports = 1;
1600 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1602 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1604 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1606 &portdev->config.max_nr_ports,
1607 sizeof(portdev->config.max_nr_ports));
1610 /* Let the Host know we support multiple ports.*/
1611 vdev->config->finalize_features(vdev);
1613 err = init_vqs(portdev);
1615 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
1619 spin_lock_init(&portdev->ports_lock);
1620 INIT_LIST_HEAD(&portdev->ports);
1623 unsigned int nr_added_bufs;
1625 spin_lock_init(&portdev->cvq_lock);
1626 INIT_WORK(&portdev->control_work, &control_work_handler);
1628 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1629 if (!nr_added_bufs) {
1631 "Error allocating buffers for control queue\n");
1637 * For backward compatibility: Create a console port
1638 * if we're running on older host.
1640 add_port(portdev, 0);
1643 spin_lock_irq(&pdrvdata_lock);
1644 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1645 spin_unlock_irq(&pdrvdata_lock);
1647 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1648 VIRTIO_CONSOLE_DEVICE_READY, 1);
1652 /* The host might want to notify mgmt sw about device add failure */
1653 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1654 VIRTIO_CONSOLE_DEVICE_READY, 0);
1655 vdev->config->del_vqs(vdev);
1656 kfree(portdev->in_vqs);
1657 kfree(portdev->out_vqs);
1659 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1666 static void virtcons_remove(struct virtio_device *vdev)
1668 struct ports_device *portdev;
1669 struct port *port, *port2;
1671 portdev = vdev->priv;
1673 spin_lock_irq(&pdrvdata_lock);
1674 list_del(&portdev->list);
1675 spin_unlock_irq(&pdrvdata_lock);
1677 /* Disable interrupts for vqs */
1678 vdev->config->reset(vdev);
1679 /* Finish up work that's lined up */
1680 cancel_work_sync(&portdev->control_work);
1682 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1685 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1687 if (use_multiport(portdev)) {
1688 struct port_buffer *buf;
1691 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1694 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1698 vdev->config->del_vqs(vdev);
1699 kfree(portdev->in_vqs);
1700 kfree(portdev->out_vqs);
1705 static struct virtio_device_id id_table[] = {
1706 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1710 static unsigned int features[] = {
1711 VIRTIO_CONSOLE_F_SIZE,
1712 VIRTIO_CONSOLE_F_MULTIPORT,
1715 static struct virtio_driver virtio_console = {
1716 .feature_table = features,
1717 .feature_table_size = ARRAY_SIZE(features),
1718 .driver.name = KBUILD_MODNAME,
1719 .driver.owner = THIS_MODULE,
1720 .id_table = id_table,
1721 .probe = virtcons_probe,
1722 .remove = virtcons_remove,
1723 .config_changed = config_intr,
1726 static int __init init(void)
1730 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1731 if (IS_ERR(pdrvdata.class)) {
1732 err = PTR_ERR(pdrvdata.class);
1733 pr_err("Error %d creating virtio-ports class\n", err);
1737 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1738 if (!pdrvdata.debugfs_dir) {
1739 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1740 PTR_ERR(pdrvdata.debugfs_dir));
1742 INIT_LIST_HEAD(&pdrvdata.consoles);
1743 INIT_LIST_HEAD(&pdrvdata.portdevs);
1745 return register_virtio_driver(&virtio_console);
1748 static void __exit fini(void)
1750 unregister_virtio_driver(&virtio_console);
1752 class_destroy(pdrvdata.class);
1753 if (pdrvdata.debugfs_dir)
1754 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1759 MODULE_DEVICE_TABLE(virtio, id_table);
1760 MODULE_DESCRIPTION("Virtio console driver");
1761 MODULE_LICENSE("GPL");