2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
3 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
4 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/cdev.h>
21 #include <linux/debugfs.h>
22 #include <linux/device.h>
23 #include <linux/err.h>
25 #include <linux/init.h>
26 #include <linux/list.h>
27 #include <linux/poll.h>
28 #include <linux/sched.h>
29 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/virtio.h>
32 #include <linux/virtio_console.h>
33 #include <linux/wait.h>
34 #include <linux/workqueue.h>
35 #include "../tty/hvc/hvc_console.h"
38 * This is a global struct for storing common data for all the devices
39 * this driver handles.
41 * Mainly, it has a linked list for all the consoles in one place so
42 * that callbacks from hvc for get_chars(), put_chars() work properly
43 * across multiple devices and multiple ports per device.
45 struct ports_driver_data
{
46 /* Used for registering chardevs */
49 /* Used for exporting per-port information to debugfs */
50 struct dentry
*debugfs_dir
;
52 /* List of all the devices we're handling */
53 struct list_head portdevs
;
55 /* Number of devices this driver is handling */
59 * This is used to keep track of the number of hvc consoles
60 * spawned by this driver. This number is given as the first
61 * argument to hvc_alloc(). To correctly map an initial
62 * console spawned via hvc_instantiate to the console being
63 * hooked up via hvc_alloc, we need to pass the same vtermno.
65 * We also just assume the first console being initialised was
66 * the first one that got used as the initial console.
68 unsigned int next_vtermno
;
70 /* All the console devices handled by this driver */
71 struct list_head consoles
;
73 static struct ports_driver_data pdrvdata
;
75 DEFINE_SPINLOCK(pdrvdata_lock
);
77 /* This struct holds information that's relevant only for console ports */
79 /* We'll place all consoles in a list in the pdrvdata struct */
80 struct list_head list
;
82 /* The hvc device associated with this console port */
83 struct hvc_struct
*hvc
;
85 /* The size of the console */
89 * This number identifies the number that we used to register
90 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
91 * number passed on by the hvc callbacks to us to
92 * differentiate between the other console ports handled by
101 /* size of the buffer in *buf above */
104 /* used length of the buffer */
106 /* offset in the buf from which to consume data */
111 * This is a per-device struct that stores data common to all the
112 * ports for that device (vdev->priv).
114 struct ports_device
{
115 /* Next portdev in the list, head is in the pdrvdata struct */
116 struct list_head list
;
119 * Workqueue handlers where we process deferred work after
122 struct work_struct control_work
;
124 struct list_head ports
;
126 /* To protect the list of ports */
127 spinlock_t ports_lock
;
129 /* To protect the vq operations for the control channel */
132 /* The current config space is stored here */
133 struct virtio_console_config config
;
135 /* The virtio device we're associated with */
136 struct virtio_device
*vdev
;
139 * A couple of virtqueues for the control channel: one for
140 * guest->host transfers, one for host->guest transfers
142 struct virtqueue
*c_ivq
, *c_ovq
;
144 /* Array of per-port IO virtqueues */
145 struct virtqueue
**in_vqs
, **out_vqs
;
147 /* Used for numbering devices for sysfs and debugfs */
148 unsigned int drv_index
;
150 /* Major number for this device. Ports will be created as minors. */
154 /* This struct holds the per-port data */
156 /* Next port in the list, head is in the ports_device */
157 struct list_head list
;
159 /* Pointer to the parent virtio_console device */
160 struct ports_device
*portdev
;
162 /* The current buffer from which data has to be fed to readers */
163 struct port_buffer
*inbuf
;
166 * To protect the operations on the in_vq associated with this
167 * port. Has to be a spinlock because it can be called from
168 * interrupt context (get_char()).
170 spinlock_t inbuf_lock
;
172 /* Protect the operations on the out_vq. */
173 spinlock_t outvq_lock
;
175 /* The IO vqs for this port */
176 struct virtqueue
*in_vq
, *out_vq
;
178 /* File in the debugfs directory that exposes this port's information */
179 struct dentry
*debugfs_file
;
182 * The entries in this struct will be valid if this port is
183 * hooked up to an hvc console
187 /* Each port associates with a separate char device */
191 /* Reference-counting to handle port hot-unplugs and file operations */
194 /* A waitqueue for poll() or blocking read operations */
195 wait_queue_head_t waitqueue
;
197 /* The 'name' of the port that we expose via sysfs properties */
200 /* We can notify apps of host connect / disconnect events via SIGIO */
201 struct fasync_struct
*async_queue
;
203 /* The 'id' to identify the port with the Host */
208 /* Is the host device open */
211 /* We should allow only one process to open a port */
212 bool guest_connected
;
215 /* This is the very early arch-specified put chars function. */
216 static int (*early_put_chars
)(u32
, const char *, int);
218 static struct port
*find_port_by_vtermno(u32 vtermno
)
221 struct console
*cons
;
224 spin_lock_irqsave(&pdrvdata_lock
, flags
);
225 list_for_each_entry(cons
, &pdrvdata
.consoles
, list
) {
226 if (cons
->vtermno
== vtermno
) {
227 port
= container_of(cons
, struct port
, cons
);
233 spin_unlock_irqrestore(&pdrvdata_lock
, flags
);
237 static struct port
*find_port_by_devt_in_portdev(struct ports_device
*portdev
,
243 spin_lock_irqsave(&portdev
->ports_lock
, flags
);
244 list_for_each_entry(port
, &portdev
->ports
, list
)
245 if (port
->cdev
->dev
== dev
)
249 spin_unlock_irqrestore(&portdev
->ports_lock
, flags
);
254 static struct port
*find_port_by_devt(dev_t dev
)
256 struct ports_device
*portdev
;
260 spin_lock_irqsave(&pdrvdata_lock
, flags
);
261 list_for_each_entry(portdev
, &pdrvdata
.portdevs
, list
) {
262 port
= find_port_by_devt_in_portdev(portdev
, dev
);
268 spin_unlock_irqrestore(&pdrvdata_lock
, flags
);
272 static struct port
*find_port_by_id(struct ports_device
*portdev
, u32 id
)
277 spin_lock_irqsave(&portdev
->ports_lock
, flags
);
278 list_for_each_entry(port
, &portdev
->ports
, list
)
283 spin_unlock_irqrestore(&portdev
->ports_lock
, flags
);
288 static struct port
*find_port_by_vq(struct ports_device
*portdev
,
289 struct virtqueue
*vq
)
294 spin_lock_irqsave(&portdev
->ports_lock
, flags
);
295 list_for_each_entry(port
, &portdev
->ports
, list
)
296 if (port
->in_vq
== vq
|| port
->out_vq
== vq
)
300 spin_unlock_irqrestore(&portdev
->ports_lock
, flags
);
304 static bool is_console_port(struct port
*port
)
311 static inline bool use_multiport(struct ports_device
*portdev
)
314 * This condition can be true when put_chars is called from
319 return portdev
->vdev
->features
[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT
);
322 static void free_buf(struct port_buffer
*buf
)
328 static struct port_buffer
*alloc_buf(size_t buf_size
)
330 struct port_buffer
*buf
;
332 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
335 buf
->buf
= kzalloc(buf_size
, GFP_KERNEL
);
340 buf
->size
= buf_size
;
349 /* Callers should take appropriate locks */
350 static void *get_inbuf(struct port
*port
)
352 struct port_buffer
*buf
;
353 struct virtqueue
*vq
;
357 buf
= virtqueue_get_buf(vq
, &len
);
366 * Create a scatter-gather list representing our input buffer and put
369 * Callers should take appropriate locks.
371 static int add_inbuf(struct virtqueue
*vq
, struct port_buffer
*buf
)
373 struct scatterlist sg
[1];
376 sg_init_one(sg
, buf
->buf
, buf
->size
);
378 ret
= virtqueue_add_buf(vq
, sg
, 0, 1, buf
);
383 /* Discard any unread data this port has. Callers lockers. */
384 static void discard_port_data(struct port
*port
)
386 struct port_buffer
*buf
;
387 struct virtqueue
*vq
;
395 buf
= virtqueue_get_buf(vq
, &len
);
399 if (add_inbuf(vq
, buf
) < 0) {
403 buf
= virtqueue_get_buf(vq
, &len
);
407 dev_warn(port
->dev
, "Errors adding %d buffers back to vq\n",
411 static bool port_has_data(struct port
*port
)
416 spin_lock_irqsave(&port
->inbuf_lock
, flags
);
421 port
->inbuf
= get_inbuf(port
);
428 spin_unlock_irqrestore(&port
->inbuf_lock
, flags
);
432 static ssize_t
__send_control_msg(struct ports_device
*portdev
, u32 port_id
,
433 unsigned int event
, unsigned int value
)
435 struct scatterlist sg
[1];
436 struct virtio_console_control cpkt
;
437 struct virtqueue
*vq
;
440 if (!use_multiport(portdev
))
449 sg_init_one(sg
, &cpkt
, sizeof(cpkt
));
450 if (virtqueue_add_buf(vq
, sg
, 1, 0, &cpkt
) >= 0) {
452 while (!virtqueue_get_buf(vq
, &len
))
458 static ssize_t
send_control_msg(struct port
*port
, unsigned int event
,
461 /* Did the port get unplugged before userspace closed it? */
463 return __send_control_msg(port
->portdev
, port
->id
, event
, value
);
467 /* Callers must take the port->outvq_lock */
468 static void reclaim_consumed_buffers(struct port
*port
)
473 while ((buf
= virtqueue_get_buf(port
->out_vq
, &len
))) {
475 port
->outvq_full
= false;
479 static ssize_t
send_buf(struct port
*port
, void *in_buf
, size_t in_count
,
482 struct scatterlist sg
[1];
483 struct virtqueue
*out_vq
;
488 out_vq
= port
->out_vq
;
490 spin_lock_irqsave(&port
->outvq_lock
, flags
);
492 reclaim_consumed_buffers(port
);
494 sg_init_one(sg
, in_buf
, in_count
);
495 ret
= virtqueue_add_buf(out_vq
, sg
, 1, 0, in_buf
);
497 /* Tell Host to go! */
498 virtqueue_kick(out_vq
);
506 port
->outvq_full
= true;
512 * Wait till the host acknowledges it pushed out the data we
513 * sent. This is done for data from the hvc_console; the tty
514 * operations are performed with spinlocks held so we can't
515 * sleep here. An alternative would be to copy the data to a
516 * buffer and relax the spinning requirement. The downside is
517 * we need to kmalloc a GFP_ATOMIC buffer each time the
518 * console driver writes something out.
520 while (!virtqueue_get_buf(out_vq
, &len
))
523 spin_unlock_irqrestore(&port
->outvq_lock
, flags
);
525 * We're expected to return the amount of data we wrote -- all
532 * Give out the data that's requested from the buffer that we have
535 static ssize_t
fill_readbuf(struct port
*port
, char *out_buf
, size_t out_count
,
538 struct port_buffer
*buf
;
541 if (!out_count
|| !port_has_data(port
))
545 out_count
= min(out_count
, buf
->len
- buf
->offset
);
550 ret
= copy_to_user(out_buf
, buf
->buf
+ buf
->offset
, out_count
);
554 memcpy(out_buf
, buf
->buf
+ buf
->offset
, out_count
);
557 buf
->offset
+= out_count
;
559 if (buf
->offset
== buf
->len
) {
561 * We're done using all the data in this buffer.
562 * Re-queue so that the Host can send us more data.
564 spin_lock_irqsave(&port
->inbuf_lock
, flags
);
567 if (add_inbuf(port
->in_vq
, buf
) < 0)
568 dev_warn(port
->dev
, "failed add_buf\n");
570 spin_unlock_irqrestore(&port
->inbuf_lock
, flags
);
572 /* Return the number of bytes actually copied */
576 /* The condition that must be true for polling to end */
577 static bool will_read_block(struct port
*port
)
579 if (!port
->guest_connected
) {
580 /* Port got hot-unplugged. Let's exit. */
583 return !port_has_data(port
) && port
->host_connected
;
586 static bool will_write_block(struct port
*port
)
590 if (!port
->guest_connected
) {
591 /* Port got hot-unplugged. Let's exit. */
594 if (!port
->host_connected
)
597 spin_lock_irq(&port
->outvq_lock
);
599 * Check if the Host has consumed any buffers since we last
600 * sent data (this is only applicable for nonblocking ports).
602 reclaim_consumed_buffers(port
);
603 ret
= port
->outvq_full
;
604 spin_unlock_irq(&port
->outvq_lock
);
609 static ssize_t
port_fops_read(struct file
*filp
, char __user
*ubuf
,
610 size_t count
, loff_t
*offp
)
615 port
= filp
->private_data
;
617 if (!port_has_data(port
)) {
619 * If nothing's connected on the host just return 0 in
620 * case of list_empty; this tells the userspace app
621 * that there's no connection
623 if (!port
->host_connected
)
625 if (filp
->f_flags
& O_NONBLOCK
)
628 ret
= wait_event_interruptible(port
->waitqueue
,
629 !will_read_block(port
));
633 /* Port got hot-unplugged. */
634 if (!port
->guest_connected
)
637 * We could've received a disconnection message while we were
638 * waiting for more data.
640 * This check is not clubbed in the if() statement above as we
641 * might receive some data as well as the host could get
642 * disconnected after we got woken up from our wait. So we
643 * really want to give off whatever data we have and only then
644 * check for host_connected.
646 if (!port_has_data(port
) && !port
->host_connected
)
649 return fill_readbuf(port
, ubuf
, count
, true);
652 static ssize_t
port_fops_write(struct file
*filp
, const char __user
*ubuf
,
653 size_t count
, loff_t
*offp
)
660 /* Userspace could be out to fool us */
664 port
= filp
->private_data
;
666 nonblock
= filp
->f_flags
& O_NONBLOCK
;
668 if (will_write_block(port
)) {
672 ret
= wait_event_interruptible(port
->waitqueue
,
673 !will_write_block(port
));
677 /* Port got hot-unplugged. */
678 if (!port
->guest_connected
)
681 count
= min((size_t)(32 * 1024), count
);
683 buf
= kmalloc(count
, GFP_KERNEL
);
687 ret
= copy_from_user(buf
, ubuf
, count
);
694 * We now ask send_buf() to not spin for generic ports -- we
695 * can re-use the same code path that non-blocking file
696 * descriptors take for blocking file descriptors since the
697 * wait is already done and we're certain the write will go
698 * through to the host.
701 ret
= send_buf(port
, buf
, count
, nonblock
);
703 if (nonblock
&& ret
> 0)
712 static unsigned int port_fops_poll(struct file
*filp
, poll_table
*wait
)
717 port
= filp
->private_data
;
718 poll_wait(filp
, &port
->waitqueue
, wait
);
720 if (!port
->guest_connected
) {
721 /* Port got unplugged */
725 if (!will_read_block(port
))
726 ret
|= POLLIN
| POLLRDNORM
;
727 if (!will_write_block(port
))
729 if (!port
->host_connected
)
735 static void remove_port(struct kref
*kref
);
737 static int port_fops_release(struct inode
*inode
, struct file
*filp
)
741 port
= filp
->private_data
;
743 /* Notify host of port being closed */
744 send_control_msg(port
, VIRTIO_CONSOLE_PORT_OPEN
, 0);
746 spin_lock_irq(&port
->inbuf_lock
);
747 port
->guest_connected
= false;
749 discard_port_data(port
);
751 spin_unlock_irq(&port
->inbuf_lock
);
753 spin_lock_irq(&port
->outvq_lock
);
754 reclaim_consumed_buffers(port
);
755 spin_unlock_irq(&port
->outvq_lock
);
758 * Locks aren't necessary here as a port can't be opened after
759 * unplug, and if a port isn't unplugged, a kref would already
760 * exist for the port. Plus, taking ports_lock here would
761 * create a dependency on other locks taken by functions
762 * inside remove_port if we're the last holder of the port,
763 * creating many problems.
765 kref_put(&port
->kref
, remove_port
);
770 static int port_fops_open(struct inode
*inode
, struct file
*filp
)
772 struct cdev
*cdev
= inode
->i_cdev
;
776 port
= find_port_by_devt(cdev
->dev
);
777 filp
->private_data
= port
;
779 /* Prevent against a port getting hot-unplugged at the same time */
780 spin_lock_irq(&port
->portdev
->ports_lock
);
781 kref_get(&port
->kref
);
782 spin_unlock_irq(&port
->portdev
->ports_lock
);
785 * Don't allow opening of console port devices -- that's done
788 if (is_console_port(port
)) {
793 /* Allow only one process to open a particular port at a time */
794 spin_lock_irq(&port
->inbuf_lock
);
795 if (port
->guest_connected
) {
796 spin_unlock_irq(&port
->inbuf_lock
);
801 port
->guest_connected
= true;
802 spin_unlock_irq(&port
->inbuf_lock
);
804 spin_lock_irq(&port
->outvq_lock
);
806 * There might be a chance that we missed reclaiming a few
807 * buffers in the window of the port getting previously closed
810 reclaim_consumed_buffers(port
);
811 spin_unlock_irq(&port
->outvq_lock
);
813 nonseekable_open(inode
, filp
);
815 /* Notify host of port being opened */
816 send_control_msg(filp
->private_data
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
820 kref_put(&port
->kref
, remove_port
);
824 static int port_fops_fasync(int fd
, struct file
*filp
, int mode
)
828 port
= filp
->private_data
;
829 return fasync_helper(fd
, filp
, mode
, &port
->async_queue
);
833 * The file operations that we support: programs in the guest can open
834 * a console device, read from it, write to it, poll for data and
835 * close it. The devices are at
836 * /dev/vport<device number>p<port number>
838 static const struct file_operations port_fops
= {
839 .owner
= THIS_MODULE
,
840 .open
= port_fops_open
,
841 .read
= port_fops_read
,
842 .write
= port_fops_write
,
843 .poll
= port_fops_poll
,
844 .release
= port_fops_release
,
845 .fasync
= port_fops_fasync
,
850 * The put_chars() callback is pretty straightforward.
852 * We turn the characters into a scatter-gather list, add it to the
853 * output queue and then kick the Host. Then we sit here waiting for
854 * it to finish: inefficient in theory, but in practice
855 * implementations will do it immediately (lguest's Launcher does).
857 static int put_chars(u32 vtermno
, const char *buf
, int count
)
861 if (unlikely(early_put_chars
))
862 return early_put_chars(vtermno
, buf
, count
);
864 port
= find_port_by_vtermno(vtermno
);
868 return send_buf(port
, (void *)buf
, count
, false);
872 * get_chars() is the callback from the hvc_console infrastructure
873 * when an interrupt is received.
875 * We call out to fill_readbuf that gets us the required data from the
876 * buffers that are queued up.
878 static int get_chars(u32 vtermno
, char *buf
, int count
)
882 /* If we've not set up the port yet, we have no input to give. */
883 if (unlikely(early_put_chars
))
886 port
= find_port_by_vtermno(vtermno
);
890 /* If we don't have an input queue yet, we can't get input. */
891 BUG_ON(!port
->in_vq
);
893 return fill_readbuf(port
, buf
, count
, false);
896 static void resize_console(struct port
*port
)
898 struct virtio_device
*vdev
;
900 /* The port could have been hot-unplugged */
901 if (!port
|| !is_console_port(port
))
904 vdev
= port
->portdev
->vdev
;
905 if (virtio_has_feature(vdev
, VIRTIO_CONSOLE_F_SIZE
))
906 hvc_resize(port
->cons
.hvc
, port
->cons
.ws
);
909 /* We set the configuration at this point, since we now have a tty */
910 static int notifier_add_vio(struct hvc_struct
*hp
, int data
)
914 port
= find_port_by_vtermno(hp
->vtermno
);
918 hp
->irq_requested
= 1;
919 resize_console(port
);
924 static void notifier_del_vio(struct hvc_struct
*hp
, int data
)
926 hp
->irq_requested
= 0;
929 /* The operations for console ports. */
930 static const struct hv_ops hv_ops
= {
931 .get_chars
= get_chars
,
932 .put_chars
= put_chars
,
933 .notifier_add
= notifier_add_vio
,
934 .notifier_del
= notifier_del_vio
,
935 .notifier_hangup
= notifier_del_vio
,
939 * Console drivers are initialized very early so boot messages can go
940 * out, so we do things slightly differently from the generic virtio
941 * initialization of the net and block drivers.
943 * At this stage, the console is output-only. It's too early to set
944 * up a virtqueue, so we let the drivers do some boutique early-output
947 int __init
virtio_cons_early_init(int (*put_chars
)(u32
, const char *, int))
949 early_put_chars
= put_chars
;
950 return hvc_instantiate(0, 0, &hv_ops
);
953 int init_port_console(struct port
*port
)
958 * The Host's telling us this port is a console port. Hook it
959 * up with an hvc console.
961 * To set up and manage our virtual console, we call
964 * The first argument of hvc_alloc() is the virtual console
965 * number. The second argument is the parameter for the
966 * notification mechanism (like irq number). We currently
967 * leave this as zero, virtqueues have implicit notifications.
969 * The third argument is a "struct hv_ops" containing the
970 * put_chars() get_chars(), notifier_add() and notifier_del()
971 * pointers. The final argument is the output buffer size: we
972 * can do any size, so we put PAGE_SIZE here.
974 port
->cons
.vtermno
= pdrvdata
.next_vtermno
;
976 port
->cons
.hvc
= hvc_alloc(port
->cons
.vtermno
, 0, &hv_ops
, PAGE_SIZE
);
977 if (IS_ERR(port
->cons
.hvc
)) {
978 ret
= PTR_ERR(port
->cons
.hvc
);
980 "error %d allocating hvc for port\n", ret
);
981 port
->cons
.hvc
= NULL
;
984 spin_lock_irq(&pdrvdata_lock
);
985 pdrvdata
.next_vtermno
++;
986 list_add_tail(&port
->cons
.list
, &pdrvdata
.consoles
);
987 spin_unlock_irq(&pdrvdata_lock
);
988 port
->guest_connected
= true;
991 * Start using the new console output if this is the first
992 * console to come up.
995 early_put_chars
= NULL
;
997 /* Notify host of port being opened */
998 send_control_msg(port
, VIRTIO_CONSOLE_PORT_OPEN
, 1);
1003 static ssize_t
show_port_name(struct device
*dev
,
1004 struct device_attribute
*attr
, char *buffer
)
1008 port
= dev_get_drvdata(dev
);
1010 return sprintf(buffer
, "%s\n", port
->name
);
1013 static DEVICE_ATTR(name
, S_IRUGO
, show_port_name
, NULL
);
1015 static struct attribute
*port_sysfs_entries
[] = {
1016 &dev_attr_name
.attr
,
1020 static struct attribute_group port_attribute_group
= {
1021 .name
= NULL
, /* put in device directory */
1022 .attrs
= port_sysfs_entries
,
1025 static int debugfs_open(struct inode
*inode
, struct file
*filp
)
1027 filp
->private_data
= inode
->i_private
;
1031 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
,
1032 size_t count
, loff_t
*offp
)
1036 ssize_t ret
, out_offset
, out_count
;
1039 buf
= kmalloc(out_count
, GFP_KERNEL
);
1043 port
= filp
->private_data
;
1045 out_offset
+= snprintf(buf
+ out_offset
, out_count
,
1046 "name: %s\n", port
->name
? port
->name
: "");
1047 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
1048 "guest_connected: %d\n", port
->guest_connected
);
1049 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
1050 "host_connected: %d\n", port
->host_connected
);
1051 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
1052 "outvq_full: %d\n", port
->outvq_full
);
1053 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
1055 is_console_port(port
) ? "yes" : "no");
1056 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
1057 "console_vtermno: %u\n", port
->cons
.vtermno
);
1059 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
1064 static const struct file_operations port_debugfs_ops
= {
1065 .owner
= THIS_MODULE
,
1066 .open
= debugfs_open
,
1067 .read
= debugfs_read
,
1070 static void set_console_size(struct port
*port
, u16 rows
, u16 cols
)
1072 if (!port
|| !is_console_port(port
))
1075 port
->cons
.ws
.ws_row
= rows
;
1076 port
->cons
.ws
.ws_col
= cols
;
1079 static unsigned int fill_queue(struct virtqueue
*vq
, spinlock_t
*lock
)
1081 struct port_buffer
*buf
;
1082 unsigned int nr_added_bufs
;
1087 buf
= alloc_buf(PAGE_SIZE
);
1091 spin_lock_irq(lock
);
1092 ret
= add_inbuf(vq
, buf
);
1094 spin_unlock_irq(lock
);
1099 spin_unlock_irq(lock
);
1102 return nr_added_bufs
;
1105 static void send_sigio_to_port(struct port
*port
)
1107 if (port
->async_queue
&& port
->guest_connected
)
1108 kill_fasync(&port
->async_queue
, SIGIO
, POLL_OUT
);
1111 static int add_port(struct ports_device
*portdev
, u32 id
)
1113 char debugfs_name
[16];
1115 struct port_buffer
*buf
;
1117 unsigned int nr_added_bufs
;
1120 port
= kmalloc(sizeof(*port
), GFP_KERNEL
);
1125 kref_init(&port
->kref
);
1127 port
->portdev
= portdev
;
1132 port
->cons
.hvc
= NULL
;
1133 port
->async_queue
= NULL
;
1135 port
->cons
.ws
.ws_row
= port
->cons
.ws
.ws_col
= 0;
1137 port
->host_connected
= port
->guest_connected
= false;
1139 port
->outvq_full
= false;
1141 port
->in_vq
= portdev
->in_vqs
[port
->id
];
1142 port
->out_vq
= portdev
->out_vqs
[port
->id
];
1144 port
->cdev
= cdev_alloc();
1146 dev_err(&port
->portdev
->vdev
->dev
, "Error allocating cdev\n");
1150 port
->cdev
->ops
= &port_fops
;
1152 devt
= MKDEV(portdev
->chr_major
, id
);
1153 err
= cdev_add(port
->cdev
, devt
, 1);
1155 dev_err(&port
->portdev
->vdev
->dev
,
1156 "Error %d adding cdev for port %u\n", err
, id
);
1159 port
->dev
= device_create(pdrvdata
.class, &port
->portdev
->vdev
->dev
,
1160 devt
, port
, "vport%up%u",
1161 port
->portdev
->drv_index
, id
);
1162 if (IS_ERR(port
->dev
)) {
1163 err
= PTR_ERR(port
->dev
);
1164 dev_err(&port
->portdev
->vdev
->dev
,
1165 "Error %d creating device for port %u\n",
1170 spin_lock_init(&port
->inbuf_lock
);
1171 spin_lock_init(&port
->outvq_lock
);
1172 init_waitqueue_head(&port
->waitqueue
);
1174 /* Fill the in_vq with buffers so the host can send us data. */
1175 nr_added_bufs
= fill_queue(port
->in_vq
, &port
->inbuf_lock
);
1176 if (!nr_added_bufs
) {
1177 dev_err(port
->dev
, "Error allocating inbufs\n");
1183 * If we're not using multiport support, this has to be a console port
1185 if (!use_multiport(port
->portdev
)) {
1186 err
= init_port_console(port
);
1191 spin_lock_irq(&portdev
->ports_lock
);
1192 list_add_tail(&port
->list
, &port
->portdev
->ports
);
1193 spin_unlock_irq(&portdev
->ports_lock
);
1196 * Tell the Host we're set so that it can send us various
1197 * configuration parameters for this port (eg, port name,
1198 * caching, whether this is a console port, etc.)
1200 send_control_msg(port
, VIRTIO_CONSOLE_PORT_READY
, 1);
1202 if (pdrvdata
.debugfs_dir
) {
1204 * Finally, create the debugfs file that we can use to
1205 * inspect a port's state at any time
1207 sprintf(debugfs_name
, "vport%up%u",
1208 port
->portdev
->drv_index
, id
);
1209 port
->debugfs_file
= debugfs_create_file(debugfs_name
, 0444,
1210 pdrvdata
.debugfs_dir
,
1217 while ((buf
= virtqueue_detach_unused_buf(port
->in_vq
)))
1220 device_destroy(pdrvdata
.class, port
->dev
->devt
);
1222 cdev_del(port
->cdev
);
1226 /* The host might want to notify management sw about port add failure */
1227 __send_control_msg(portdev
, id
, VIRTIO_CONSOLE_PORT_READY
, 0);
1231 /* No users remain, remove all port-specific data. */
1232 static void remove_port(struct kref
*kref
)
1236 port
= container_of(kref
, struct port
, kref
);
1238 sysfs_remove_group(&port
->dev
->kobj
, &port_attribute_group
);
1239 device_destroy(pdrvdata
.class, port
->dev
->devt
);
1240 cdev_del(port
->cdev
);
1244 debugfs_remove(port
->debugfs_file
);
1250 * Port got unplugged. Remove port from portdev's list and drop the
1251 * kref reference. If no userspace has this port opened, it will
1252 * result in immediate removal the port.
1254 static void unplug_port(struct port
*port
)
1256 struct port_buffer
*buf
;
1258 spin_lock_irq(&port
->portdev
->ports_lock
);
1259 list_del(&port
->list
);
1260 spin_unlock_irq(&port
->portdev
->ports_lock
);
1262 if (port
->guest_connected
) {
1263 port
->guest_connected
= false;
1264 port
->host_connected
= false;
1265 wake_up_interruptible(&port
->waitqueue
);
1267 /* Let the app know the port is going down. */
1268 send_sigio_to_port(port
);
1271 if (is_console_port(port
)) {
1272 spin_lock_irq(&pdrvdata_lock
);
1273 list_del(&port
->cons
.list
);
1274 spin_unlock_irq(&pdrvdata_lock
);
1277 * hvc_remove() not called as removing one hvc port
1278 * results in other hvc ports getting frozen.
1280 * Once this is resolved in hvc, this functionality
1281 * will be enabled. Till that is done, the -EPIPE
1282 * return from get_chars() above will help
1283 * hvc_console.c to clean up on ports we remove here.
1285 hvc_remove(port
->cons
.hvc
);
1289 /* Remove unused data this port might have received. */
1290 discard_port_data(port
);
1292 reclaim_consumed_buffers(port
);
1294 /* Remove buffers we queued up for the Host to send us data in. */
1295 while ((buf
= virtqueue_detach_unused_buf(port
->in_vq
)))
1299 * We should just assume the device itself has gone off --
1300 * else a close on an open port later will try to send out a
1303 port
->portdev
= NULL
;
1306 * Locks around here are not necessary - a port can't be
1307 * opened after we removed the port struct from ports_list
1310 kref_put(&port
->kref
, remove_port
);
1313 /* Any private messages that the Host and Guest want to share */
1314 static void handle_control_message(struct ports_device
*portdev
,
1315 struct port_buffer
*buf
)
1317 struct virtio_console_control
*cpkt
;
1322 cpkt
= (struct virtio_console_control
*)(buf
->buf
+ buf
->offset
);
1324 port
= find_port_by_id(portdev
, cpkt
->id
);
1325 if (!port
&& cpkt
->event
!= VIRTIO_CONSOLE_PORT_ADD
) {
1326 /* No valid header at start of buffer. Drop it. */
1327 dev_dbg(&portdev
->vdev
->dev
,
1328 "Invalid index %u in control packet\n", cpkt
->id
);
1332 switch (cpkt
->event
) {
1333 case VIRTIO_CONSOLE_PORT_ADD
:
1335 dev_dbg(&portdev
->vdev
->dev
,
1336 "Port %u already added\n", port
->id
);
1337 send_control_msg(port
, VIRTIO_CONSOLE_PORT_READY
, 1);
1340 if (cpkt
->id
>= portdev
->config
.max_nr_ports
) {
1341 dev_warn(&portdev
->vdev
->dev
,
1342 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1343 cpkt
->id
, portdev
->config
.max_nr_ports
- 1);
1346 add_port(portdev
, cpkt
->id
);
1348 case VIRTIO_CONSOLE_PORT_REMOVE
:
1351 case VIRTIO_CONSOLE_CONSOLE_PORT
:
1354 if (is_console_port(port
))
1357 init_port_console(port
);
1359 * Could remove the port here in case init fails - but
1360 * have to notify the host first.
1363 case VIRTIO_CONSOLE_RESIZE
: {
1369 if (!is_console_port(port
))
1372 memcpy(&size
, buf
->buf
+ buf
->offset
+ sizeof(*cpkt
),
1374 set_console_size(port
, size
.rows
, size
.cols
);
1376 port
->cons
.hvc
->irq_requested
= 1;
1377 resize_console(port
);
1380 case VIRTIO_CONSOLE_PORT_OPEN
:
1381 port
->host_connected
= cpkt
->value
;
1382 wake_up_interruptible(&port
->waitqueue
);
1384 * If the host port got closed and the host had any
1385 * unconsumed buffers, we'll be able to reclaim them
1388 spin_lock_irq(&port
->outvq_lock
);
1389 reclaim_consumed_buffers(port
);
1390 spin_unlock_irq(&port
->outvq_lock
);
1393 * If the guest is connected, it'll be interested in
1394 * knowing the host connection state changed.
1396 send_sigio_to_port(port
);
1398 case VIRTIO_CONSOLE_PORT_NAME
:
1400 * Skip the size of the header and the cpkt to get the size
1401 * of the name that was sent
1403 name_size
= buf
->len
- buf
->offset
- sizeof(*cpkt
) + 1;
1405 port
->name
= kmalloc(name_size
, GFP_KERNEL
);
1408 "Not enough space to store port name\n");
1411 strncpy(port
->name
, buf
->buf
+ buf
->offset
+ sizeof(*cpkt
),
1413 port
->name
[name_size
- 1] = 0;
1416 * Since we only have one sysfs attribute, 'name',
1417 * create it only if we have a name for the port.
1419 err
= sysfs_create_group(&port
->dev
->kobj
,
1420 &port_attribute_group
);
1423 "Error %d creating sysfs device attributes\n",
1427 * Generate a udev event so that appropriate
1428 * symlinks can be created based on udev
1431 kobject_uevent(&port
->dev
->kobj
, KOBJ_CHANGE
);
1437 static void control_work_handler(struct work_struct
*work
)
1439 struct ports_device
*portdev
;
1440 struct virtqueue
*vq
;
1441 struct port_buffer
*buf
;
1444 portdev
= container_of(work
, struct ports_device
, control_work
);
1445 vq
= portdev
->c_ivq
;
1447 spin_lock(&portdev
->cvq_lock
);
1448 while ((buf
= virtqueue_get_buf(vq
, &len
))) {
1449 spin_unlock(&portdev
->cvq_lock
);
1454 handle_control_message(portdev
, buf
);
1456 spin_lock(&portdev
->cvq_lock
);
1457 if (add_inbuf(portdev
->c_ivq
, buf
) < 0) {
1458 dev_warn(&portdev
->vdev
->dev
,
1459 "Error adding buffer to queue\n");
1463 spin_unlock(&portdev
->cvq_lock
);
1466 static void out_intr(struct virtqueue
*vq
)
1470 port
= find_port_by_vq(vq
->vdev
->priv
, vq
);
1474 wake_up_interruptible(&port
->waitqueue
);
1477 static void in_intr(struct virtqueue
*vq
)
1480 unsigned long flags
;
1482 port
= find_port_by_vq(vq
->vdev
->priv
, vq
);
1486 spin_lock_irqsave(&port
->inbuf_lock
, flags
);
1488 port
->inbuf
= get_inbuf(port
);
1491 * Don't queue up data when port is closed. This condition
1492 * can be reached when a console port is not yet connected (no
1493 * tty is spawned) and the host sends out data to console
1494 * ports. For generic serial ports, the host won't
1495 * (shouldn't) send data till the guest is connected.
1497 if (!port
->guest_connected
)
1498 discard_port_data(port
);
1500 spin_unlock_irqrestore(&port
->inbuf_lock
, flags
);
1502 wake_up_interruptible(&port
->waitqueue
);
1504 /* Send a SIGIO indicating new data in case the process asked for it */
1505 send_sigio_to_port(port
);
1507 if (is_console_port(port
) && hvc_poll(port
->cons
.hvc
))
1511 static void control_intr(struct virtqueue
*vq
)
1513 struct ports_device
*portdev
;
1515 portdev
= vq
->vdev
->priv
;
1516 schedule_work(&portdev
->control_work
);
1519 static void config_intr(struct virtio_device
*vdev
)
1521 struct ports_device
*portdev
;
1523 portdev
= vdev
->priv
;
1525 if (!use_multiport(portdev
)) {
1529 vdev
->config
->get(vdev
,
1530 offsetof(struct virtio_console_config
, cols
),
1531 &cols
, sizeof(u16
));
1532 vdev
->config
->get(vdev
,
1533 offsetof(struct virtio_console_config
, rows
),
1534 &rows
, sizeof(u16
));
1536 port
= find_port_by_id(portdev
, 0);
1537 set_console_size(port
, rows
, cols
);
1540 * We'll use this way of resizing only for legacy
1541 * support. For newer userspace
1542 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1543 * to indicate console size changes so that it can be
1546 resize_console(port
);
1550 static int init_vqs(struct ports_device
*portdev
)
1552 vq_callback_t
**io_callbacks
;
1554 struct virtqueue
**vqs
;
1555 u32 i
, j
, nr_ports
, nr_queues
;
1558 nr_ports
= portdev
->config
.max_nr_ports
;
1559 nr_queues
= use_multiport(portdev
) ? (nr_ports
+ 1) * 2 : 2;
1561 vqs
= kmalloc(nr_queues
* sizeof(struct virtqueue
*), GFP_KERNEL
);
1562 io_callbacks
= kmalloc(nr_queues
* sizeof(vq_callback_t
*), GFP_KERNEL
);
1563 io_names
= kmalloc(nr_queues
* sizeof(char *), GFP_KERNEL
);
1564 portdev
->in_vqs
= kmalloc(nr_ports
* sizeof(struct virtqueue
*),
1566 portdev
->out_vqs
= kmalloc(nr_ports
* sizeof(struct virtqueue
*),
1568 if (!vqs
|| !io_callbacks
|| !io_names
|| !portdev
->in_vqs
||
1569 !portdev
->out_vqs
) {
1575 * For backward compat (newer host but older guest), the host
1576 * spawns a console port first and also inits the vqs for port
1580 io_callbacks
[j
] = in_intr
;
1581 io_callbacks
[j
+ 1] = out_intr
;
1582 io_names
[j
] = "input";
1583 io_names
[j
+ 1] = "output";
1586 if (use_multiport(portdev
)) {
1587 io_callbacks
[j
] = control_intr
;
1588 io_callbacks
[j
+ 1] = NULL
;
1589 io_names
[j
] = "control-i";
1590 io_names
[j
+ 1] = "control-o";
1592 for (i
= 1; i
< nr_ports
; i
++) {
1594 io_callbacks
[j
] = in_intr
;
1595 io_callbacks
[j
+ 1] = out_intr
;
1596 io_names
[j
] = "input";
1597 io_names
[j
+ 1] = "output";
1600 /* Find the queues. */
1601 err
= portdev
->vdev
->config
->find_vqs(portdev
->vdev
, nr_queues
, vqs
,
1603 (const char **)io_names
);
1608 portdev
->in_vqs
[0] = vqs
[0];
1609 portdev
->out_vqs
[0] = vqs
[1];
1611 if (use_multiport(portdev
)) {
1612 portdev
->c_ivq
= vqs
[j
];
1613 portdev
->c_ovq
= vqs
[j
+ 1];
1615 for (i
= 1; i
< nr_ports
; i
++) {
1617 portdev
->in_vqs
[i
] = vqs
[j
];
1618 portdev
->out_vqs
[i
] = vqs
[j
+ 1];
1622 kfree(io_callbacks
);
1628 kfree(portdev
->out_vqs
);
1629 kfree(portdev
->in_vqs
);
1631 kfree(io_callbacks
);
1637 static const struct file_operations portdev_fops
= {
1638 .owner
= THIS_MODULE
,
1642 * Once we're further in boot, we get probed like any other virtio
1645 * If the host also supports multiple console ports, we check the
1646 * config space to see how many ports the host has spawned. We
1647 * initialize each port found.
1649 static int __devinit
virtcons_probe(struct virtio_device
*vdev
)
1651 struct ports_device
*portdev
;
1655 portdev
= kmalloc(sizeof(*portdev
), GFP_KERNEL
);
1661 /* Attach this portdev to this virtio_device, and vice-versa. */
1662 portdev
->vdev
= vdev
;
1663 vdev
->priv
= portdev
;
1665 spin_lock_irq(&pdrvdata_lock
);
1666 portdev
->drv_index
= pdrvdata
.index
++;
1667 spin_unlock_irq(&pdrvdata_lock
);
1669 portdev
->chr_major
= register_chrdev(0, "virtio-portsdev",
1671 if (portdev
->chr_major
< 0) {
1673 "Error %d registering chrdev for device %u\n",
1674 portdev
->chr_major
, portdev
->drv_index
);
1675 err
= portdev
->chr_major
;
1680 portdev
->config
.max_nr_ports
= 1;
1681 if (virtio_has_feature(vdev
, VIRTIO_CONSOLE_F_MULTIPORT
)) {
1683 vdev
->features
[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT
;
1685 vdev
->config
->get(vdev
, offsetof(struct virtio_console_config
,
1687 &portdev
->config
.max_nr_ports
,
1688 sizeof(portdev
->config
.max_nr_ports
));
1691 /* Let the Host know we support multiple ports.*/
1692 vdev
->config
->finalize_features(vdev
);
1694 err
= init_vqs(portdev
);
1696 dev_err(&vdev
->dev
, "Error %d initializing vqs\n", err
);
1700 spin_lock_init(&portdev
->ports_lock
);
1701 INIT_LIST_HEAD(&portdev
->ports
);
1704 unsigned int nr_added_bufs
;
1706 spin_lock_init(&portdev
->cvq_lock
);
1707 INIT_WORK(&portdev
->control_work
, &control_work_handler
);
1709 nr_added_bufs
= fill_queue(portdev
->c_ivq
, &portdev
->cvq_lock
);
1710 if (!nr_added_bufs
) {
1712 "Error allocating buffers for control queue\n");
1718 * For backward compatibility: Create a console port
1719 * if we're running on older host.
1721 add_port(portdev
, 0);
1724 spin_lock_irq(&pdrvdata_lock
);
1725 list_add_tail(&portdev
->list
, &pdrvdata
.portdevs
);
1726 spin_unlock_irq(&pdrvdata_lock
);
1728 __send_control_msg(portdev
, VIRTIO_CONSOLE_BAD_ID
,
1729 VIRTIO_CONSOLE_DEVICE_READY
, 1);
1733 /* The host might want to notify mgmt sw about device add failure */
1734 __send_control_msg(portdev
, VIRTIO_CONSOLE_BAD_ID
,
1735 VIRTIO_CONSOLE_DEVICE_READY
, 0);
1736 vdev
->config
->del_vqs(vdev
);
1737 kfree(portdev
->in_vqs
);
1738 kfree(portdev
->out_vqs
);
1740 unregister_chrdev(portdev
->chr_major
, "virtio-portsdev");
1747 static void virtcons_remove(struct virtio_device
*vdev
)
1749 struct ports_device
*portdev
;
1750 struct port
*port
, *port2
;
1752 portdev
= vdev
->priv
;
1754 spin_lock_irq(&pdrvdata_lock
);
1755 list_del(&portdev
->list
);
1756 spin_unlock_irq(&pdrvdata_lock
);
1758 /* Disable interrupts for vqs */
1759 vdev
->config
->reset(vdev
);
1760 /* Finish up work that's lined up */
1761 cancel_work_sync(&portdev
->control_work
);
1763 list_for_each_entry_safe(port
, port2
, &portdev
->ports
, list
)
1766 unregister_chrdev(portdev
->chr_major
, "virtio-portsdev");
1769 * When yanking out a device, we immediately lose the
1770 * (device-side) queues. So there's no point in keeping the
1771 * guest side around till we drop our final reference. This
1772 * also means that any ports which are in an open state will
1773 * have to just stop using the port, as the vqs are going
1776 if (use_multiport(portdev
)) {
1777 struct port_buffer
*buf
;
1780 while ((buf
= virtqueue_get_buf(portdev
->c_ivq
, &len
)))
1783 while ((buf
= virtqueue_detach_unused_buf(portdev
->c_ivq
)))
1787 vdev
->config
->del_vqs(vdev
);
1788 kfree(portdev
->in_vqs
);
1789 kfree(portdev
->out_vqs
);
1794 static struct virtio_device_id id_table
[] = {
1795 { VIRTIO_ID_CONSOLE
, VIRTIO_DEV_ANY_ID
},
1799 static unsigned int features
[] = {
1800 VIRTIO_CONSOLE_F_SIZE
,
1801 VIRTIO_CONSOLE_F_MULTIPORT
,
1804 static struct virtio_driver virtio_console
= {
1805 .feature_table
= features
,
1806 .feature_table_size
= ARRAY_SIZE(features
),
1807 .driver
.name
= KBUILD_MODNAME
,
1808 .driver
.owner
= THIS_MODULE
,
1809 .id_table
= id_table
,
1810 .probe
= virtcons_probe
,
1811 .remove
= virtcons_remove
,
1812 .config_changed
= config_intr
,
1815 static int __init
init(void)
1819 pdrvdata
.class = class_create(THIS_MODULE
, "virtio-ports");
1820 if (IS_ERR(pdrvdata
.class)) {
1821 err
= PTR_ERR(pdrvdata
.class);
1822 pr_err("Error %d creating virtio-ports class\n", err
);
1826 pdrvdata
.debugfs_dir
= debugfs_create_dir("virtio-ports", NULL
);
1827 if (!pdrvdata
.debugfs_dir
) {
1828 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1829 PTR_ERR(pdrvdata
.debugfs_dir
));
1831 INIT_LIST_HEAD(&pdrvdata
.consoles
);
1832 INIT_LIST_HEAD(&pdrvdata
.portdevs
);
1834 return register_virtio_driver(&virtio_console
);
1837 static void __exit
fini(void)
1839 unregister_virtio_driver(&virtio_console
);
1841 class_destroy(pdrvdata
.class);
1842 if (pdrvdata
.debugfs_dir
)
1843 debugfs_remove_recursive(pdrvdata
.debugfs_dir
);
1848 MODULE_DEVICE_TABLE(virtio
, id_table
);
1849 MODULE_DESCRIPTION("Virtio console driver");
1850 MODULE_LICENSE("GPL");