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 /* Number of devices this driver is handling */
55 * This is used to keep track of the number of hvc consoles
56 * spawned by this driver. This number is given as the first
57 * argument to hvc_alloc(). To correctly map an initial
58 * console spawned via hvc_instantiate to the console being
59 * hooked up via hvc_alloc, we need to pass the same vtermno.
61 * We also just assume the first console being initialised was
62 * the first one that got used as the initial console.
64 unsigned int next_vtermno;
66 /* All the console devices handled by this driver */
67 struct list_head consoles;
69 static struct ports_driver_data pdrvdata;
71 DEFINE_SPINLOCK(pdrvdata_lock);
73 /* This struct holds information that's relevant only for console ports */
75 /* We'll place all consoles in a list in the pdrvdata struct */
76 struct list_head list;
78 /* The hvc device associated with this console port */
79 struct hvc_struct *hvc;
82 * This number identifies the number that we used to register
83 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
84 * number passed on by the hvc callbacks to us to
85 * differentiate between the other console ports handled by
94 /* size of the buffer in *buf above */
97 /* used length of the buffer */
99 /* offset in the buf from which to consume data */
104 * This is a per-device struct that stores data common to all the
105 * ports for that device (vdev->priv).
107 struct ports_device {
109 * Workqueue handlers where we process deferred work after
112 struct work_struct control_work;
114 struct list_head ports;
116 /* To protect the list of ports */
117 spinlock_t ports_lock;
119 /* To protect the vq operations for the control channel */
122 /* The current config space is stored here */
123 struct virtio_console_config config;
125 /* The virtio device we're associated with */
126 struct virtio_device *vdev;
129 * A couple of virtqueues for the control channel: one for
130 * guest->host transfers, one for host->guest transfers
132 struct virtqueue *c_ivq, *c_ovq;
134 /* Array of per-port IO virtqueues */
135 struct virtqueue **in_vqs, **out_vqs;
137 /* Used for numbering devices for sysfs and debugfs */
138 unsigned int drv_index;
140 /* Major number for this device. Ports will be created as minors. */
144 /* This struct holds the per-port data */
146 /* Next port in the list, head is in the ports_device */
147 struct list_head list;
149 /* Pointer to the parent virtio_console device */
150 struct ports_device *portdev;
152 /* The current buffer from which data has to be fed to readers */
153 struct port_buffer *inbuf;
156 * To protect the operations on the in_vq associated with this
157 * port. Has to be a spinlock because it can be called from
158 * interrupt context (get_char()).
160 spinlock_t inbuf_lock;
162 /* Protect the operations on the out_vq. */
163 spinlock_t outvq_lock;
165 /* The IO vqs for this port */
166 struct virtqueue *in_vq, *out_vq;
168 /* File in the debugfs directory that exposes this port's information */
169 struct dentry *debugfs_file;
172 * The entries in this struct will be valid if this port is
173 * hooked up to an hvc console
177 /* Each port associates with a separate char device */
181 /* A waitqueue for poll() or blocking read operations */
182 wait_queue_head_t waitqueue;
184 /* The 'name' of the port that we expose via sysfs properties */
187 /* The 'id' to identify the port with the Host */
192 /* Is the host device open */
195 /* We should allow only one process to open a port */
196 bool guest_connected;
199 /* This is the very early arch-specified put chars function. */
200 static int (*early_put_chars)(u32, const char *, int);
202 static struct port *find_port_by_vtermno(u32 vtermno)
205 struct console *cons;
208 spin_lock_irqsave(&pdrvdata_lock, flags);
209 list_for_each_entry(cons, &pdrvdata.consoles, list) {
210 if (cons->vtermno == vtermno) {
211 port = container_of(cons, struct port, cons);
217 spin_unlock_irqrestore(&pdrvdata_lock, flags);
221 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
226 spin_lock_irqsave(&portdev->ports_lock, flags);
227 list_for_each_entry(port, &portdev->ports, list)
232 spin_unlock_irqrestore(&portdev->ports_lock, flags);
237 static struct port *find_port_by_vq(struct ports_device *portdev,
238 struct virtqueue *vq)
243 spin_lock_irqsave(&portdev->ports_lock, flags);
244 list_for_each_entry(port, &portdev->ports, list)
245 if (port->in_vq == vq || port->out_vq == vq)
249 spin_unlock_irqrestore(&portdev->ports_lock, flags);
253 static bool is_console_port(struct port *port)
260 static inline bool use_multiport(struct ports_device *portdev)
263 * This condition can be true when put_chars is called from
268 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
271 static void free_buf(struct port_buffer *buf)
277 static struct port_buffer *alloc_buf(size_t buf_size)
279 struct port_buffer *buf;
281 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
284 buf->buf = kzalloc(buf_size, GFP_KERNEL);
289 buf->size = buf_size;
298 /* Callers should take appropriate locks */
299 static void *get_inbuf(struct port *port)
301 struct port_buffer *buf;
302 struct virtqueue *vq;
306 buf = virtqueue_get_buf(vq, &len);
315 * Create a scatter-gather list representing our input buffer and put
318 * Callers should take appropriate locks.
320 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
322 struct scatterlist sg[1];
325 sg_init_one(sg, buf->buf, buf->size);
327 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
332 /* Discard any unread data this port has. Callers lockers. */
333 static void discard_port_data(struct port *port)
335 struct port_buffer *buf;
336 struct virtqueue *vq;
344 buf = virtqueue_get_buf(vq, &len);
348 if (add_inbuf(vq, buf) < 0) {
352 buf = virtqueue_get_buf(vq, &len);
356 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
360 static bool port_has_data(struct port *port)
365 spin_lock_irqsave(&port->inbuf_lock, flags);
370 port->inbuf = get_inbuf(port);
377 spin_unlock_irqrestore(&port->inbuf_lock, flags);
381 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
382 unsigned int event, unsigned int value)
384 struct scatterlist sg[1];
385 struct virtio_console_control cpkt;
386 struct virtqueue *vq;
389 if (!use_multiport(portdev))
398 sg_init_one(sg, &cpkt, sizeof(cpkt));
399 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
401 while (!virtqueue_get_buf(vq, &len))
407 static ssize_t send_control_msg(struct port *port, unsigned int event,
410 return __send_control_msg(port->portdev, port->id, event, value);
413 /* Callers must take the port->outvq_lock */
414 static void reclaim_consumed_buffers(struct port *port)
419 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
421 port->outvq_full = false;
425 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
428 struct scatterlist sg[1];
429 struct virtqueue *out_vq;
434 out_vq = port->out_vq;
436 spin_lock_irqsave(&port->outvq_lock, flags);
438 reclaim_consumed_buffers(port);
440 sg_init_one(sg, in_buf, in_count);
441 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
443 /* Tell Host to go! */
444 virtqueue_kick(out_vq);
452 port->outvq_full = true;
458 * Wait till the host acknowledges it pushed out the data we
459 * sent. This is done for ports in blocking mode or for data
460 * from the hvc_console; the tty operations are performed with
461 * spinlocks held so we can't sleep here.
463 while (!virtqueue_get_buf(out_vq, &len))
466 spin_unlock_irqrestore(&port->outvq_lock, flags);
468 * We're expected to return the amount of data we wrote -- all
475 * Give out the data that's requested from the buffer that we have
478 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
481 struct port_buffer *buf;
484 if (!out_count || !port_has_data(port))
488 out_count = min(out_count, buf->len - buf->offset);
493 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
497 memcpy(out_buf, buf->buf + buf->offset, out_count);
500 buf->offset += out_count;
502 if (buf->offset == buf->len) {
504 * We're done using all the data in this buffer.
505 * Re-queue so that the Host can send us more data.
507 spin_lock_irqsave(&port->inbuf_lock, flags);
510 if (add_inbuf(port->in_vq, buf) < 0)
511 dev_warn(port->dev, "failed add_buf\n");
513 spin_unlock_irqrestore(&port->inbuf_lock, flags);
515 /* Return the number of bytes actually copied */
519 /* The condition that must be true for polling to end */
520 static bool will_read_block(struct port *port)
522 return !port_has_data(port) && port->host_connected;
525 static bool will_write_block(struct port *port)
529 if (!port->host_connected)
532 spin_lock_irq(&port->outvq_lock);
534 * Check if the Host has consumed any buffers since we last
535 * sent data (this is only applicable for nonblocking ports).
537 reclaim_consumed_buffers(port);
538 ret = port->outvq_full;
539 spin_unlock_irq(&port->outvq_lock);
544 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
545 size_t count, loff_t *offp)
550 port = filp->private_data;
552 if (!port_has_data(port)) {
554 * If nothing's connected on the host just return 0 in
555 * case of list_empty; this tells the userspace app
556 * that there's no connection
558 if (!port->host_connected)
560 if (filp->f_flags & O_NONBLOCK)
563 ret = wait_event_interruptible(port->waitqueue,
564 !will_read_block(port));
569 * We could've received a disconnection message while we were
570 * waiting for more data.
572 * This check is not clubbed in the if() statement above as we
573 * might receive some data as well as the host could get
574 * disconnected after we got woken up from our wait. So we
575 * really want to give off whatever data we have and only then
576 * check for host_connected.
578 if (!port_has_data(port) && !port->host_connected)
581 return fill_readbuf(port, ubuf, count, true);
584 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
585 size_t count, loff_t *offp)
592 port = filp->private_data;
594 nonblock = filp->f_flags & O_NONBLOCK;
596 if (will_write_block(port)) {
600 ret = wait_event_interruptible(port->waitqueue,
601 !will_write_block(port));
606 count = min((size_t)(32 * 1024), count);
608 buf = kmalloc(count, GFP_KERNEL);
612 ret = copy_from_user(buf, ubuf, count);
618 ret = send_buf(port, buf, count, nonblock);
620 if (nonblock && ret > 0)
629 static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
634 port = filp->private_data;
635 poll_wait(filp, &port->waitqueue, wait);
639 ret |= POLLIN | POLLRDNORM;
640 if (!will_write_block(port))
642 if (!port->host_connected)
648 static int port_fops_release(struct inode *inode, struct file *filp)
652 port = filp->private_data;
654 /* Notify host of port being closed */
655 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
657 spin_lock_irq(&port->inbuf_lock);
658 port->guest_connected = false;
660 discard_port_data(port);
662 spin_unlock_irq(&port->inbuf_lock);
664 spin_lock_irq(&port->outvq_lock);
665 reclaim_consumed_buffers(port);
666 spin_unlock_irq(&port->outvq_lock);
671 static int port_fops_open(struct inode *inode, struct file *filp)
673 struct cdev *cdev = inode->i_cdev;
676 port = container_of(cdev, struct port, cdev);
677 filp->private_data = port;
680 * Don't allow opening of console port devices -- that's done
683 if (is_console_port(port))
686 /* Allow only one process to open a particular port at a time */
687 spin_lock_irq(&port->inbuf_lock);
688 if (port->guest_connected) {
689 spin_unlock_irq(&port->inbuf_lock);
693 port->guest_connected = true;
694 spin_unlock_irq(&port->inbuf_lock);
696 spin_lock_irq(&port->outvq_lock);
698 * There might be a chance that we missed reclaiming a few
699 * buffers in the window of the port getting previously closed
702 reclaim_consumed_buffers(port);
703 spin_unlock_irq(&port->outvq_lock);
705 /* Notify host of port being opened */
706 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
712 * The file operations that we support: programs in the guest can open
713 * a console device, read from it, write to it, poll for data and
714 * close it. The devices are at
715 * /dev/vport<device number>p<port number>
717 static const struct file_operations port_fops = {
718 .owner = THIS_MODULE,
719 .open = port_fops_open,
720 .read = port_fops_read,
721 .write = port_fops_write,
722 .poll = port_fops_poll,
723 .release = port_fops_release,
727 * The put_chars() callback is pretty straightforward.
729 * We turn the characters into a scatter-gather list, add it to the
730 * output queue and then kick the Host. Then we sit here waiting for
731 * it to finish: inefficient in theory, but in practice
732 * implementations will do it immediately (lguest's Launcher does).
734 static int put_chars(u32 vtermno, const char *buf, int count)
738 if (unlikely(early_put_chars))
739 return early_put_chars(vtermno, buf, count);
741 port = find_port_by_vtermno(vtermno);
745 return send_buf(port, (void *)buf, count, false);
749 * get_chars() is the callback from the hvc_console infrastructure
750 * when an interrupt is received.
752 * We call out to fill_readbuf that gets us the required data from the
753 * buffers that are queued up.
755 static int get_chars(u32 vtermno, char *buf, int count)
759 /* If we've not set up the port yet, we have no input to give. */
760 if (unlikely(early_put_chars))
763 port = find_port_by_vtermno(vtermno);
767 /* If we don't have an input queue yet, we can't get input. */
768 BUG_ON(!port->in_vq);
770 return fill_readbuf(port, buf, count, false);
773 static void resize_console(struct port *port)
775 struct virtio_device *vdev;
778 /* The port could have been hot-unplugged */
782 vdev = port->portdev->vdev;
783 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)) {
784 vdev->config->get(vdev,
785 offsetof(struct virtio_console_config, cols),
786 &ws.ws_col, sizeof(u16));
787 vdev->config->get(vdev,
788 offsetof(struct virtio_console_config, rows),
789 &ws.ws_row, sizeof(u16));
790 hvc_resize(port->cons.hvc, ws);
794 /* We set the configuration at this point, since we now have a tty */
795 static int notifier_add_vio(struct hvc_struct *hp, int data)
799 port = find_port_by_vtermno(hp->vtermno);
803 hp->irq_requested = 1;
804 resize_console(port);
809 static void notifier_del_vio(struct hvc_struct *hp, int data)
811 hp->irq_requested = 0;
814 /* The operations for console ports. */
815 static const struct hv_ops hv_ops = {
816 .get_chars = get_chars,
817 .put_chars = put_chars,
818 .notifier_add = notifier_add_vio,
819 .notifier_del = notifier_del_vio,
820 .notifier_hangup = notifier_del_vio,
824 * Console drivers are initialized very early so boot messages can go
825 * out, so we do things slightly differently from the generic virtio
826 * initialization of the net and block drivers.
828 * At this stage, the console is output-only. It's too early to set
829 * up a virtqueue, so we let the drivers do some boutique early-output
832 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
834 early_put_chars = put_chars;
835 return hvc_instantiate(0, 0, &hv_ops);
838 int init_port_console(struct port *port)
843 * The Host's telling us this port is a console port. Hook it
844 * up with an hvc console.
846 * To set up and manage our virtual console, we call
849 * The first argument of hvc_alloc() is the virtual console
850 * number. The second argument is the parameter for the
851 * notification mechanism (like irq number). We currently
852 * leave this as zero, virtqueues have implicit notifications.
854 * The third argument is a "struct hv_ops" containing the
855 * put_chars() get_chars(), notifier_add() and notifier_del()
856 * pointers. The final argument is the output buffer size: we
857 * can do any size, so we put PAGE_SIZE here.
859 port->cons.vtermno = pdrvdata.next_vtermno;
861 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
862 if (IS_ERR(port->cons.hvc)) {
863 ret = PTR_ERR(port->cons.hvc);
865 "error %d allocating hvc for port\n", ret);
866 port->cons.hvc = NULL;
869 spin_lock_irq(&pdrvdata_lock);
870 pdrvdata.next_vtermno++;
871 list_add_tail(&port->cons.list, &pdrvdata.consoles);
872 spin_unlock_irq(&pdrvdata_lock);
873 port->guest_connected = true;
876 * Start using the new console output if this is the first
877 * console to come up.
880 early_put_chars = NULL;
882 /* Notify host of port being opened */
883 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
888 static ssize_t show_port_name(struct device *dev,
889 struct device_attribute *attr, char *buffer)
893 port = dev_get_drvdata(dev);
895 return sprintf(buffer, "%s\n", port->name);
898 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
900 static struct attribute *port_sysfs_entries[] = {
905 static struct attribute_group port_attribute_group = {
906 .name = NULL, /* put in device directory */
907 .attrs = port_sysfs_entries,
910 static int debugfs_open(struct inode *inode, struct file *filp)
912 filp->private_data = inode->i_private;
916 static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
917 size_t count, loff_t *offp)
921 ssize_t ret, out_offset, out_count;
924 buf = kmalloc(out_count, GFP_KERNEL);
928 port = filp->private_data;
930 out_offset += snprintf(buf + out_offset, out_count,
931 "name: %s\n", port->name ? port->name : "");
932 out_offset += snprintf(buf + out_offset, out_count - out_offset,
933 "guest_connected: %d\n", port->guest_connected);
934 out_offset += snprintf(buf + out_offset, out_count - out_offset,
935 "host_connected: %d\n", port->host_connected);
936 out_offset += snprintf(buf + out_offset, out_count - out_offset,
937 "outvq_full: %d\n", port->outvq_full);
938 out_offset += snprintf(buf + out_offset, out_count - out_offset,
940 is_console_port(port) ? "yes" : "no");
941 out_offset += snprintf(buf + out_offset, out_count - out_offset,
942 "console_vtermno: %u\n", port->cons.vtermno);
944 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
949 static const struct file_operations port_debugfs_ops = {
950 .owner = THIS_MODULE,
951 .open = debugfs_open,
952 .read = debugfs_read,
955 static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
957 struct port_buffer *buf;
958 unsigned int nr_added_bufs;
963 buf = alloc_buf(PAGE_SIZE);
968 ret = add_inbuf(vq, buf);
970 spin_unlock_irq(lock);
975 spin_unlock_irq(lock);
978 return nr_added_bufs;
981 static int add_port(struct ports_device *portdev, u32 id)
983 char debugfs_name[16];
985 struct port_buffer *buf;
987 unsigned int nr_added_bufs;
990 port = kmalloc(sizeof(*port), GFP_KERNEL);
996 port->portdev = portdev;
1001 port->cons.hvc = NULL;
1003 port->host_connected = port->guest_connected = false;
1005 port->outvq_full = false;
1007 port->in_vq = portdev->in_vqs[port->id];
1008 port->out_vq = portdev->out_vqs[port->id];
1010 cdev_init(&port->cdev, &port_fops);
1012 devt = MKDEV(portdev->chr_major, id);
1013 err = cdev_add(&port->cdev, devt, 1);
1015 dev_err(&port->portdev->vdev->dev,
1016 "Error %d adding cdev for port %u\n", err, id);
1019 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1020 devt, port, "vport%up%u",
1021 port->portdev->drv_index, id);
1022 if (IS_ERR(port->dev)) {
1023 err = PTR_ERR(port->dev);
1024 dev_err(&port->portdev->vdev->dev,
1025 "Error %d creating device for port %u\n",
1030 spin_lock_init(&port->inbuf_lock);
1031 spin_lock_init(&port->outvq_lock);
1032 init_waitqueue_head(&port->waitqueue);
1034 /* Fill the in_vq with buffers so the host can send us data. */
1035 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1036 if (!nr_added_bufs) {
1037 dev_err(port->dev, "Error allocating inbufs\n");
1043 * If we're not using multiport support, this has to be a console port
1045 if (!use_multiport(port->portdev)) {
1046 err = init_port_console(port);
1051 spin_lock_irq(&portdev->ports_lock);
1052 list_add_tail(&port->list, &port->portdev->ports);
1053 spin_unlock_irq(&portdev->ports_lock);
1056 * Tell the Host we're set so that it can send us various
1057 * configuration parameters for this port (eg, port name,
1058 * caching, whether this is a console port, etc.)
1060 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1062 if (pdrvdata.debugfs_dir) {
1064 * Finally, create the debugfs file that we can use to
1065 * inspect a port's state at any time
1067 sprintf(debugfs_name, "vport%up%u",
1068 port->portdev->drv_index, id);
1069 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1070 pdrvdata.debugfs_dir,
1077 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1080 device_destroy(pdrvdata.class, port->dev->devt);
1082 cdev_del(&port->cdev);
1086 /* The host might want to notify management sw about port add failure */
1087 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 0);
1091 /* Remove all port-specific data. */
1092 static int remove_port(struct port *port)
1094 struct port_buffer *buf;
1096 spin_lock_irq(&port->portdev->ports_lock);
1097 list_del(&port->list);
1098 spin_unlock_irq(&port->portdev->ports_lock);
1100 if (is_console_port(port)) {
1101 spin_lock_irq(&pdrvdata_lock);
1102 list_del(&port->cons.list);
1103 spin_unlock_irq(&pdrvdata_lock);
1106 * hvc_remove() not called as removing one hvc port
1107 * results in other hvc ports getting frozen.
1109 * Once this is resolved in hvc, this functionality
1110 * will be enabled. Till that is done, the -EPIPE
1111 * return from get_chars() above will help
1112 * hvc_console.c to clean up on ports we remove here.
1114 hvc_remove(port->cons.hvc);
1117 if (port->guest_connected)
1118 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1120 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1121 device_destroy(pdrvdata.class, port->dev->devt);
1122 cdev_del(&port->cdev);
1124 /* Remove unused data this port might have received. */
1125 discard_port_data(port);
1127 reclaim_consumed_buffers(port);
1129 /* Remove buffers we queued up for the Host to send us data in. */
1130 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1135 debugfs_remove(port->debugfs_file);
1141 /* Any private messages that the Host and Guest want to share */
1142 static void handle_control_message(struct ports_device *portdev,
1143 struct port_buffer *buf)
1145 struct virtio_console_control *cpkt;
1150 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1152 port = find_port_by_id(portdev, cpkt->id);
1153 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
1154 /* No valid header at start of buffer. Drop it. */
1155 dev_dbg(&portdev->vdev->dev,
1156 "Invalid index %u in control packet\n", cpkt->id);
1160 switch (cpkt->event) {
1161 case VIRTIO_CONSOLE_PORT_ADD:
1163 dev_dbg(&portdev->vdev->dev,
1164 "Port %u already added\n", port->id);
1165 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1168 if (cpkt->id >= portdev->config.max_nr_ports) {
1169 dev_warn(&portdev->vdev->dev,
1170 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1171 cpkt->id, portdev->config.max_nr_ports - 1);
1174 add_port(portdev, cpkt->id);
1176 case VIRTIO_CONSOLE_PORT_REMOVE:
1179 case VIRTIO_CONSOLE_CONSOLE_PORT:
1182 if (is_console_port(port))
1185 init_port_console(port);
1187 * Could remove the port here in case init fails - but
1188 * have to notify the host first.
1191 case VIRTIO_CONSOLE_RESIZE:
1192 if (!is_console_port(port))
1194 port->cons.hvc->irq_requested = 1;
1195 resize_console(port);
1197 case VIRTIO_CONSOLE_PORT_OPEN:
1198 port->host_connected = cpkt->value;
1199 wake_up_interruptible(&port->waitqueue);
1201 * If the host port got closed and the host had any
1202 * unconsumed buffers, we'll be able to reclaim them
1205 spin_lock_irq(&port->outvq_lock);
1206 reclaim_consumed_buffers(port);
1207 spin_unlock_irq(&port->outvq_lock);
1209 case VIRTIO_CONSOLE_PORT_NAME:
1211 * Skip the size of the header and the cpkt to get the size
1212 * of the name that was sent
1214 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1216 port->name = kmalloc(name_size, GFP_KERNEL);
1219 "Not enough space to store port name\n");
1222 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1224 port->name[name_size - 1] = 0;
1227 * Since we only have one sysfs attribute, 'name',
1228 * create it only if we have a name for the port.
1230 err = sysfs_create_group(&port->dev->kobj,
1231 &port_attribute_group);
1234 "Error %d creating sysfs device attributes\n",
1238 * Generate a udev event so that appropriate
1239 * symlinks can be created based on udev
1242 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1248 static void control_work_handler(struct work_struct *work)
1250 struct ports_device *portdev;
1251 struct virtqueue *vq;
1252 struct port_buffer *buf;
1255 portdev = container_of(work, struct ports_device, control_work);
1256 vq = portdev->c_ivq;
1258 spin_lock(&portdev->cvq_lock);
1259 while ((buf = virtqueue_get_buf(vq, &len))) {
1260 spin_unlock(&portdev->cvq_lock);
1265 handle_control_message(portdev, buf);
1267 spin_lock(&portdev->cvq_lock);
1268 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1269 dev_warn(&portdev->vdev->dev,
1270 "Error adding buffer to queue\n");
1274 spin_unlock(&portdev->cvq_lock);
1277 static void in_intr(struct virtqueue *vq)
1280 unsigned long flags;
1282 port = find_port_by_vq(vq->vdev->priv, vq);
1286 spin_lock_irqsave(&port->inbuf_lock, flags);
1288 port->inbuf = get_inbuf(port);
1291 * Don't queue up data when port is closed. This condition
1292 * can be reached when a console port is not yet connected (no
1293 * tty is spawned) and the host sends out data to console
1294 * ports. For generic serial ports, the host won't
1295 * (shouldn't) send data till the guest is connected.
1297 if (!port->guest_connected)
1298 discard_port_data(port);
1300 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1302 wake_up_interruptible(&port->waitqueue);
1304 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1308 static void control_intr(struct virtqueue *vq)
1310 struct ports_device *portdev;
1312 portdev = vq->vdev->priv;
1313 schedule_work(&portdev->control_work);
1316 static void config_intr(struct virtio_device *vdev)
1318 struct ports_device *portdev;
1320 portdev = vdev->priv;
1323 * We'll use this way of resizing only for legacy support.
1324 * For newer userspace (VIRTIO_CONSOLE_F_MULTPORT+), use
1325 * control messages to indicate console size changes so that
1326 * it can be done per-port
1328 resize_console(find_port_by_id(portdev, 0));
1331 static int init_vqs(struct ports_device *portdev)
1333 vq_callback_t **io_callbacks;
1335 struct virtqueue **vqs;
1336 u32 i, j, nr_ports, nr_queues;
1339 nr_ports = portdev->config.max_nr_ports;
1340 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1342 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1347 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1348 if (!io_callbacks) {
1352 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1355 goto free_callbacks;
1357 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1359 if (!portdev->in_vqs) {
1363 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1365 if (!portdev->out_vqs) {
1371 * For backward compat (newer host but older guest), the host
1372 * spawns a console port first and also inits the vqs for port
1376 io_callbacks[j] = in_intr;
1377 io_callbacks[j + 1] = NULL;
1378 io_names[j] = "input";
1379 io_names[j + 1] = "output";
1382 if (use_multiport(portdev)) {
1383 io_callbacks[j] = control_intr;
1384 io_callbacks[j + 1] = NULL;
1385 io_names[j] = "control-i";
1386 io_names[j + 1] = "control-o";
1388 for (i = 1; i < nr_ports; i++) {
1390 io_callbacks[j] = in_intr;
1391 io_callbacks[j + 1] = NULL;
1392 io_names[j] = "input";
1393 io_names[j + 1] = "output";
1396 /* Find the queues. */
1397 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1399 (const char **)io_names);
1404 portdev->in_vqs[0] = vqs[0];
1405 portdev->out_vqs[0] = vqs[1];
1407 if (use_multiport(portdev)) {
1408 portdev->c_ivq = vqs[j];
1409 portdev->c_ovq = vqs[j + 1];
1411 for (i = 1; i < nr_ports; i++) {
1413 portdev->in_vqs[i] = vqs[j];
1414 portdev->out_vqs[i] = vqs[j + 1];
1417 kfree(io_callbacks);
1426 kfree(io_callbacks);
1428 kfree(portdev->out_vqs);
1430 kfree(portdev->in_vqs);
1437 static const struct file_operations portdev_fops = {
1438 .owner = THIS_MODULE,
1442 * Once we're further in boot, we get probed like any other virtio
1445 * If the host also supports multiple console ports, we check the
1446 * config space to see how many ports the host has spawned. We
1447 * initialize each port found.
1449 static int __devinit virtcons_probe(struct virtio_device *vdev)
1451 struct ports_device *portdev;
1455 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1461 /* Attach this portdev to this virtio_device, and vice-versa. */
1462 portdev->vdev = vdev;
1463 vdev->priv = portdev;
1465 spin_lock_irq(&pdrvdata_lock);
1466 portdev->drv_index = pdrvdata.index++;
1467 spin_unlock_irq(&pdrvdata_lock);
1469 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1471 if (portdev->chr_major < 0) {
1473 "Error %d registering chrdev for device %u\n",
1474 portdev->chr_major, portdev->drv_index);
1475 err = portdev->chr_major;
1480 portdev->config.max_nr_ports = 1;
1481 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1483 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1485 vdev->config->get(vdev, offsetof(struct virtio_console_config,
1487 &portdev->config.max_nr_ports,
1488 sizeof(portdev->config.max_nr_ports));
1491 /* Let the Host know we support multiple ports.*/
1492 vdev->config->finalize_features(vdev);
1494 err = init_vqs(portdev);
1496 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
1500 spin_lock_init(&portdev->ports_lock);
1501 INIT_LIST_HEAD(&portdev->ports);
1504 unsigned int nr_added_bufs;
1506 spin_lock_init(&portdev->cvq_lock);
1507 INIT_WORK(&portdev->control_work, &control_work_handler);
1509 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1510 if (!nr_added_bufs) {
1512 "Error allocating buffers for control queue\n");
1518 * For backward compatibility: Create a console port
1519 * if we're running on older host.
1521 add_port(portdev, 0);
1524 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1525 VIRTIO_CONSOLE_DEVICE_READY, 1);
1529 vdev->config->del_vqs(vdev);
1530 kfree(portdev->in_vqs);
1531 kfree(portdev->out_vqs);
1533 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1537 /* The host might want to notify mgmt sw about device add failure */
1538 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1539 VIRTIO_CONSOLE_DEVICE_READY, 0);
1543 static void virtcons_remove(struct virtio_device *vdev)
1545 struct ports_device *portdev;
1546 struct port *port, *port2;
1547 struct port_buffer *buf;
1550 portdev = vdev->priv;
1552 cancel_work_sync(&portdev->control_work);
1554 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1557 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1559 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1562 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1565 vdev->config->del_vqs(vdev);
1566 kfree(portdev->in_vqs);
1567 kfree(portdev->out_vqs);
1572 static struct virtio_device_id id_table[] = {
1573 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1577 static unsigned int features[] = {
1578 VIRTIO_CONSOLE_F_SIZE,
1579 VIRTIO_CONSOLE_F_MULTIPORT,
1582 static struct virtio_driver virtio_console = {
1583 .feature_table = features,
1584 .feature_table_size = ARRAY_SIZE(features),
1585 .driver.name = KBUILD_MODNAME,
1586 .driver.owner = THIS_MODULE,
1587 .id_table = id_table,
1588 .probe = virtcons_probe,
1589 .remove = virtcons_remove,
1590 .config_changed = config_intr,
1593 static int __init init(void)
1597 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1598 if (IS_ERR(pdrvdata.class)) {
1599 err = PTR_ERR(pdrvdata.class);
1600 pr_err("Error %d creating virtio-ports class\n", err);
1604 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1605 if (!pdrvdata.debugfs_dir) {
1606 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1607 PTR_ERR(pdrvdata.debugfs_dir));
1609 INIT_LIST_HEAD(&pdrvdata.consoles);
1611 return register_virtio_driver(&virtio_console);
1614 static void __exit fini(void)
1616 unregister_virtio_driver(&virtio_console);
1618 class_destroy(pdrvdata.class);
1619 if (pdrvdata.debugfs_dir)
1620 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1625 MODULE_DEVICE_TABLE(virtio, id_table);
1626 MODULE_DESCRIPTION("Virtio console driver");
1627 MODULE_LICENSE("GPL");