1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
4 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
5 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
7 #include <linux/cdev.h>
8 #include <linux/debugfs.h>
9 #include <linux/completion.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/freezer.h>
14 #include <linux/splice.h>
15 #include <linux/pagemap.h>
16 #include <linux/idr.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/virtio.h>
24 #include <linux/virtio_console.h>
25 #include <linux/wait.h>
26 #include <linux/workqueue.h>
27 #include <linux/module.h>
28 #include <linux/dma-mapping.h>
29 #include "../tty/hvc/hvc_console.h"
31 #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
32 #define VIRTCONS_MAX_PORTS 0x8000
35 * This is a global struct for storing common data for all the devices
36 * this driver handles.
38 * Mainly, it has a linked list for all the consoles in one place so
39 * that callbacks from hvc for get_chars(), put_chars() work properly
40 * across multiple devices and multiple ports per device.
42 struct ports_driver_data {
43 /* Used for exporting per-port information to debugfs */
44 struct dentry *debugfs_dir;
46 /* List of all the devices we're handling */
47 struct list_head portdevs;
49 /* All the console devices handled by this driver */
50 struct list_head consoles;
53 static struct ports_driver_data pdrvdata;
55 static const struct class port_class = {
56 .name = "virtio-ports",
59 static DEFINE_SPINLOCK(pdrvdata_lock);
60 static DECLARE_COMPLETION(early_console_added);
62 /* This struct holds information that's relevant only for console ports */
64 /* We'll place all consoles in a list in the pdrvdata struct */
65 struct list_head list;
67 /* The hvc device associated with this console port */
68 struct hvc_struct *hvc;
70 /* The size of the console */
74 * This number identifies the number that we used to register
75 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
76 * number passed on by the hvc callbacks to us to
77 * differentiate between the other console ports handled by
83 static DEFINE_IDA(vtermno_ida);
88 /* size of the buffer in *buf above */
91 /* used length of the buffer */
93 /* offset in the buf from which to consume data */
96 /* DMA address of buffer */
99 /* Device we got DMA memory from */
102 /* List of pending dma buffers to free */
103 struct list_head list;
105 /* If sgpages == 0 then buf is used */
106 unsigned int sgpages;
108 /* sg is used if spages > 0. sg must be the last in is struct */
109 struct scatterlist sg[];
113 * This is a per-device struct that stores data common to all the
114 * ports for that device (vdev->priv).
116 struct ports_device {
117 /* Next portdev in the list, head is in the pdrvdata struct */
118 struct list_head list;
121 * Workqueue handlers where we process deferred work after
124 struct work_struct control_work;
125 struct work_struct config_work;
127 struct list_head ports;
129 /* To protect the list of ports */
130 spinlock_t ports_lock;
132 /* To protect the vq operations for the control channel */
133 spinlock_t c_ivq_lock;
134 spinlock_t c_ovq_lock;
136 /* max. number of ports this device can hold */
139 /* The virtio device we're associated with */
140 struct virtio_device *vdev;
143 * A couple of virtqueues for the control channel: one for
144 * guest->host transfers, one for host->guest transfers
146 struct virtqueue *c_ivq, *c_ovq;
149 * A control packet buffer for guest->host requests, protected
152 struct virtio_console_control cpkt;
154 /* Array of per-port IO virtqueues */
155 struct virtqueue **in_vqs, **out_vqs;
157 /* Major number for this device. Ports will be created as minors. */
162 unsigned long bytes_sent, bytes_received, bytes_discarded;
165 /* This struct holds the per-port data */
167 /* Next port in the list, head is in the ports_device */
168 struct list_head list;
170 /* Pointer to the parent virtio_console device */
171 struct ports_device *portdev;
173 /* The current buffer from which data has to be fed to readers */
174 struct port_buffer *inbuf;
177 * To protect the operations on the in_vq associated with this
178 * port. Has to be a spinlock because it can be called from
179 * interrupt context (get_char()).
181 spinlock_t inbuf_lock;
183 /* Protect the operations on the out_vq. */
184 spinlock_t outvq_lock;
186 /* The IO vqs for this port */
187 struct virtqueue *in_vq, *out_vq;
189 /* File in the debugfs directory that exposes this port's information */
190 struct dentry *debugfs_file;
193 * Keep count of the bytes sent, received and discarded for
194 * this port for accounting and debugging purposes. These
195 * counts are not reset across port open / close events.
197 struct port_stats stats;
200 * The entries in this struct will be valid if this port is
201 * hooked up to an hvc console
205 /* Each port associates with a separate char device */
209 /* Reference-counting to handle port hot-unplugs and file operations */
212 /* A waitqueue for poll() or blocking read operations */
213 wait_queue_head_t waitqueue;
215 /* The 'name' of the port that we expose via sysfs properties */
218 /* We can notify apps of host connect / disconnect events via SIGIO */
219 struct fasync_struct *async_queue;
221 /* The 'id' to identify the port with the Host */
226 /* Is the host device open */
229 /* We should allow only one process to open a port */
230 bool guest_connected;
233 /* This is the very early arch-specified put chars function. */
234 static int (*early_put_chars)(u32, const char *, int);
236 static struct port *find_port_by_vtermno(u32 vtermno)
239 struct console *cons;
242 spin_lock_irqsave(&pdrvdata_lock, flags);
243 list_for_each_entry(cons, &pdrvdata.consoles, list) {
244 if (cons->vtermno == vtermno) {
245 port = container_of(cons, struct port, cons);
251 spin_unlock_irqrestore(&pdrvdata_lock, flags);
255 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
261 spin_lock_irqsave(&portdev->ports_lock, flags);
262 list_for_each_entry(port, &portdev->ports, list) {
263 if (port->cdev->dev == dev) {
264 kref_get(&port->kref);
270 spin_unlock_irqrestore(&portdev->ports_lock, flags);
275 static struct port *find_port_by_devt(dev_t dev)
277 struct ports_device *portdev;
281 spin_lock_irqsave(&pdrvdata_lock, flags);
282 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
283 port = find_port_by_devt_in_portdev(portdev, dev);
289 spin_unlock_irqrestore(&pdrvdata_lock, flags);
293 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
298 spin_lock_irqsave(&portdev->ports_lock, flags);
299 list_for_each_entry(port, &portdev->ports, list)
304 spin_unlock_irqrestore(&portdev->ports_lock, flags);
309 static struct port *find_port_by_vq(struct ports_device *portdev,
310 struct virtqueue *vq)
315 spin_lock_irqsave(&portdev->ports_lock, flags);
316 list_for_each_entry(port, &portdev->ports, list)
317 if (port->in_vq == vq || port->out_vq == vq)
321 spin_unlock_irqrestore(&portdev->ports_lock, flags);
325 static bool is_console_port(struct port *port)
332 static bool is_rproc_serial(const struct virtio_device *vdev)
334 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
337 static inline bool use_multiport(struct ports_device *portdev)
340 * This condition can be true when put_chars is called from
345 return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
348 static DEFINE_SPINLOCK(dma_bufs_lock);
349 static LIST_HEAD(pending_free_dma_bufs);
351 static void free_buf(struct port_buffer *buf, bool can_sleep)
355 for (i = 0; i < buf->sgpages; i++) {
356 struct page *page = sg_page(&buf->sg[i]);
364 } else if (is_rproc_enabled) {
367 /* dma_free_coherent requires interrupts to be enabled. */
369 /* queue up dma-buffers to be freed later */
370 spin_lock_irqsave(&dma_bufs_lock, flags);
371 list_add_tail(&buf->list, &pending_free_dma_bufs);
372 spin_unlock_irqrestore(&dma_bufs_lock, flags);
375 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
377 /* Release device refcnt and allow it to be freed */
378 put_device(buf->dev);
384 static void reclaim_dma_bufs(void)
387 struct port_buffer *buf, *tmp;
390 if (list_empty(&pending_free_dma_bufs))
393 /* Create a copy of the pending_free_dma_bufs while holding the lock */
394 spin_lock_irqsave(&dma_bufs_lock, flags);
395 list_cut_position(&tmp_list, &pending_free_dma_bufs,
396 pending_free_dma_bufs.prev);
397 spin_unlock_irqrestore(&dma_bufs_lock, flags);
399 /* Release the dma buffers, without irqs enabled */
400 list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
401 list_del(&buf->list);
406 static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
409 struct port_buffer *buf;
414 * Allocate buffer and the sg list. The sg list array is allocated
415 * directly after the port_buffer struct.
417 buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
421 buf->sgpages = pages;
428 if (is_rproc_serial(vdev)) {
430 * Allocate DMA memory from ancestor. When a virtio
431 * device is created by remoteproc, the DMA memory is
432 * associated with the parent device:
433 * virtioY => remoteprocX#vdevYbuffer.
435 buf->dev = vdev->dev.parent;
439 /* Increase device refcnt to avoid freeing it */
440 get_device(buf->dev);
441 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
445 buf->buf = kmalloc(buf_size, GFP_KERNEL);
452 buf->size = buf_size;
461 /* Callers should take appropriate locks */
462 static struct port_buffer *get_inbuf(struct port *port)
464 struct port_buffer *buf;
470 buf = virtqueue_get_buf(port->in_vq, &len);
472 buf->len = min_t(size_t, len, buf->size);
474 port->stats.bytes_received += len;
480 * Create a scatter-gather list representing our input buffer and put
483 * Callers should take appropriate locks.
485 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
487 struct scatterlist sg[1];
490 sg_init_one(sg, buf->buf, buf->size);
492 ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
499 /* Discard any unread data this port has. Callers lockers. */
500 static void discard_port_data(struct port *port)
502 struct port_buffer *buf;
505 if (!port->portdev) {
506 /* Device has been unplugged. vqs are already gone. */
509 buf = get_inbuf(port);
513 port->stats.bytes_discarded += buf->len - buf->offset;
514 if (add_inbuf(port->in_vq, buf) < 0) {
516 free_buf(buf, false);
519 buf = get_inbuf(port);
522 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
526 static bool port_has_data(struct port *port)
532 spin_lock_irqsave(&port->inbuf_lock, flags);
533 port->inbuf = get_inbuf(port);
537 spin_unlock_irqrestore(&port->inbuf_lock, flags);
541 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
542 unsigned int event, unsigned int value)
544 struct scatterlist sg[1];
545 struct virtqueue *vq;
548 if (!use_multiport(portdev))
553 spin_lock(&portdev->c_ovq_lock);
555 portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
556 portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
557 portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
559 sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
561 if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
563 while (!virtqueue_get_buf(vq, &len)
564 && !virtqueue_is_broken(vq))
568 spin_unlock(&portdev->c_ovq_lock);
572 static ssize_t send_control_msg(struct port *port, unsigned int event,
575 /* Did the port get unplugged before userspace closed it? */
577 return __send_control_msg(port->portdev, port->id, event, value);
582 /* Callers must take the port->outvq_lock */
583 static void reclaim_consumed_buffers(struct port *port)
585 struct port_buffer *buf;
588 if (!port->portdev) {
589 /* Device has been unplugged. vqs are already gone. */
592 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
593 free_buf(buf, false);
594 port->outvq_full = false;
598 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
599 int nents, size_t in_count,
600 void *data, bool nonblock)
602 struct virtqueue *out_vq;
607 out_vq = port->out_vq;
609 spin_lock_irqsave(&port->outvq_lock, flags);
611 reclaim_consumed_buffers(port);
613 err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
615 /* Tell Host to go! */
616 virtqueue_kick(out_vq);
623 if (out_vq->num_free == 0)
624 port->outvq_full = true;
630 * Wait till the host acknowledges it pushed out the data we
631 * sent. This is done for data from the hvc_console; the tty
632 * operations are performed with spinlocks held so we can't
633 * sleep here. An alternative would be to copy the data to a
634 * buffer and relax the spinning requirement. The downside is
635 * we need to kmalloc a GFP_ATOMIC buffer each time the
636 * console driver writes something out.
638 while (!virtqueue_get_buf(out_vq, &len)
639 && !virtqueue_is_broken(out_vq))
642 spin_unlock_irqrestore(&port->outvq_lock, flags);
644 port->stats.bytes_sent += in_count;
646 * We're expected to return the amount of data we wrote -- all
653 * Give out the data that's requested from the buffer that we have
656 static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
657 size_t out_count, bool to_user)
659 struct port_buffer *buf;
662 if (!out_count || !port_has_data(port))
666 out_count = min(out_count, buf->len - buf->offset);
671 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
675 memcpy((__force char *)out_buf, buf->buf + buf->offset,
679 buf->offset += out_count;
681 if (buf->offset == buf->len) {
683 * We're done using all the data in this buffer.
684 * Re-queue so that the Host can send us more data.
686 spin_lock_irqsave(&port->inbuf_lock, flags);
689 if (add_inbuf(port->in_vq, buf) < 0)
690 dev_warn(port->dev, "failed add_buf\n");
692 spin_unlock_irqrestore(&port->inbuf_lock, flags);
694 /* Return the number of bytes actually copied */
698 /* The condition that must be true for polling to end */
699 static bool will_read_block(struct port *port)
701 if (!port->guest_connected) {
702 /* Port got hot-unplugged. Let's exit. */
705 return !port_has_data(port) && port->host_connected;
708 static bool will_write_block(struct port *port)
712 if (!port->guest_connected) {
713 /* Port got hot-unplugged. Let's exit. */
716 if (!port->host_connected)
719 spin_lock_irq(&port->outvq_lock);
721 * Check if the Host has consumed any buffers since we last
722 * sent data (this is only applicable for nonblocking ports).
724 reclaim_consumed_buffers(port);
725 ret = port->outvq_full;
726 spin_unlock_irq(&port->outvq_lock);
731 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
732 size_t count, loff_t *offp)
737 port = filp->private_data;
739 /* Port is hot-unplugged. */
740 if (!port->guest_connected)
743 if (!port_has_data(port)) {
745 * If nothing's connected on the host just return 0 in
746 * case of list_empty; this tells the userspace app
747 * that there's no connection
749 if (!port->host_connected)
751 if (filp->f_flags & O_NONBLOCK)
754 ret = wait_event_freezable(port->waitqueue,
755 !will_read_block(port));
759 /* Port got hot-unplugged while we were waiting above. */
760 if (!port->guest_connected)
763 * We could've received a disconnection message while we were
764 * waiting for more data.
766 * This check is not clubbed in the if() statement above as we
767 * might receive some data as well as the host could get
768 * disconnected after we got woken up from our wait. So we
769 * really want to give off whatever data we have and only then
770 * check for host_connected.
772 if (!port_has_data(port) && !port->host_connected)
775 return fill_readbuf(port, ubuf, count, true);
778 static int wait_port_writable(struct port *port, bool nonblock)
782 if (will_write_block(port)) {
786 ret = wait_event_freezable(port->waitqueue,
787 !will_write_block(port));
791 /* Port got hot-unplugged. */
792 if (!port->guest_connected)
798 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
799 size_t count, loff_t *offp)
802 struct port_buffer *buf;
805 struct scatterlist sg[1];
807 /* Userspace could be out to fool us */
811 port = filp->private_data;
813 nonblock = filp->f_flags & O_NONBLOCK;
815 ret = wait_port_writable(port, nonblock);
819 count = min((size_t)(32 * 1024), count);
821 buf = alloc_buf(port->portdev->vdev, count, 0);
825 ret = copy_from_user(buf->buf, ubuf, count);
832 * We now ask send_buf() to not spin for generic ports -- we
833 * can re-use the same code path that non-blocking file
834 * descriptors take for blocking file descriptors since the
835 * wait is already done and we're certain the write will go
836 * through to the host.
839 sg_init_one(sg, buf->buf, count);
840 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
842 if (nonblock && ret > 0)
855 struct scatterlist *sg;
858 static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
859 struct splice_desc *sd)
861 struct sg_list *sgl = sd->u.data;
862 unsigned int offset, len;
864 if (sgl->n == sgl->size)
867 /* Try lock this page */
868 if (pipe_buf_try_steal(pipe, buf)) {
869 /* Get reference and unlock page for moving */
871 unlock_page(buf->page);
873 len = min(buf->len, sd->len);
874 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
876 /* Failback to copying a page */
877 struct page *page = alloc_page(GFP_KERNEL);
883 offset = sd->pos & ~PAGE_MASK;
886 if (len + offset > PAGE_SIZE)
887 len = PAGE_SIZE - offset;
889 src = kmap_atomic(buf->page);
890 memcpy(page_address(page) + offset, src + buf->offset, len);
893 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
901 /* Faster zero-copy write by splicing */
902 static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
903 struct file *filp, loff_t *ppos,
904 size_t len, unsigned int flags)
906 struct port *port = filp->private_data;
909 struct port_buffer *buf;
910 struct splice_desc sd = {
916 unsigned int occupancy;
919 * Rproc_serial does not yet support splice. To support splice
920 * pipe_to_sg() must allocate dma-buffers and copy content from
921 * regular pages to dma pages. And alloc_buf and free_buf must
922 * support allocating and freeing such a list of dma-buffers.
924 if (is_rproc_serial(port->out_vq->vdev))
929 if (pipe_empty(pipe->head, pipe->tail))
932 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
936 occupancy = pipe_occupancy(pipe->head, pipe->tail);
937 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
946 sgl.size = occupancy;
948 sg_init_table(sgl.sg, sgl.size);
949 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
952 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
954 if (unlikely(ret <= 0))
963 static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
968 port = filp->private_data;
969 poll_wait(filp, &port->waitqueue, wait);
971 if (!port->guest_connected) {
972 /* Port got unplugged */
976 if (!will_read_block(port))
977 ret |= EPOLLIN | EPOLLRDNORM;
978 if (!will_write_block(port))
980 if (!port->host_connected)
986 static void remove_port(struct kref *kref);
988 static int port_fops_release(struct inode *inode, struct file *filp)
992 port = filp->private_data;
994 /* Notify host of port being closed */
995 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
997 spin_lock_irq(&port->inbuf_lock);
998 port->guest_connected = false;
1000 discard_port_data(port);
1002 spin_unlock_irq(&port->inbuf_lock);
1004 spin_lock_irq(&port->outvq_lock);
1005 reclaim_consumed_buffers(port);
1006 spin_unlock_irq(&port->outvq_lock);
1010 * Locks aren't necessary here as a port can't be opened after
1011 * unplug, and if a port isn't unplugged, a kref would already
1012 * exist for the port. Plus, taking ports_lock here would
1013 * create a dependency on other locks taken by functions
1014 * inside remove_port if we're the last holder of the port,
1015 * creating many problems.
1017 kref_put(&port->kref, remove_port);
1022 static int port_fops_open(struct inode *inode, struct file *filp)
1024 struct cdev *cdev = inode->i_cdev;
1028 /* We get the port with a kref here */
1029 port = find_port_by_devt(cdev->dev);
1031 /* Port was unplugged before we could proceed */
1034 filp->private_data = port;
1037 * Don't allow opening of console port devices -- that's done
1040 if (is_console_port(port)) {
1045 /* Allow only one process to open a particular port at a time */
1046 spin_lock_irq(&port->inbuf_lock);
1047 if (port->guest_connected) {
1048 spin_unlock_irq(&port->inbuf_lock);
1053 port->guest_connected = true;
1054 spin_unlock_irq(&port->inbuf_lock);
1056 spin_lock_irq(&port->outvq_lock);
1058 * There might be a chance that we missed reclaiming a few
1059 * buffers in the window of the port getting previously closed
1062 reclaim_consumed_buffers(port);
1063 spin_unlock_irq(&port->outvq_lock);
1065 nonseekable_open(inode, filp);
1067 /* Notify host of port being opened */
1068 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
1072 kref_put(&port->kref, remove_port);
1076 static int port_fops_fasync(int fd, struct file *filp, int mode)
1080 port = filp->private_data;
1081 return fasync_helper(fd, filp, mode, &port->async_queue);
1085 * The file operations that we support: programs in the guest can open
1086 * a console device, read from it, write to it, poll for data and
1087 * close it. The devices are at
1088 * /dev/vport<device number>p<port number>
1090 static const struct file_operations port_fops = {
1091 .owner = THIS_MODULE,
1092 .open = port_fops_open,
1093 .read = port_fops_read,
1094 .write = port_fops_write,
1095 .splice_write = port_fops_splice_write,
1096 .poll = port_fops_poll,
1097 .release = port_fops_release,
1098 .fasync = port_fops_fasync,
1099 .llseek = no_llseek,
1103 * The put_chars() callback is pretty straightforward.
1105 * We turn the characters into a scatter-gather list, add it to the
1106 * output queue and then kick the Host. Then we sit here waiting for
1107 * it to finish: inefficient in theory, but in practice
1108 * implementations will do it immediately.
1110 static int put_chars(u32 vtermno, const char *buf, int count)
1113 struct scatterlist sg[1];
1117 if (unlikely(early_put_chars))
1118 return early_put_chars(vtermno, buf, count);
1120 port = find_port_by_vtermno(vtermno);
1124 data = kmemdup(buf, count, GFP_ATOMIC);
1128 sg_init_one(sg, data, count);
1129 ret = __send_to_port(port, sg, 1, count, data, false);
1135 * get_chars() is the callback from the hvc_console infrastructure
1136 * when an interrupt is received.
1138 * We call out to fill_readbuf that gets us the required data from the
1139 * buffers that are queued up.
1141 static int get_chars(u32 vtermno, char *buf, int count)
1145 /* If we've not set up the port yet, we have no input to give. */
1146 if (unlikely(early_put_chars))
1149 port = find_port_by_vtermno(vtermno);
1153 /* If we don't have an input queue yet, we can't get input. */
1154 BUG_ON(!port->in_vq);
1156 return fill_readbuf(port, (__force char __user *)buf, count, false);
1159 static void resize_console(struct port *port)
1161 struct virtio_device *vdev;
1163 /* The port could have been hot-unplugged */
1164 if (!port || !is_console_port(port))
1167 vdev = port->portdev->vdev;
1169 /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
1170 if (!is_rproc_serial(vdev) &&
1171 virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1172 hvc_resize(port->cons.hvc, port->cons.ws);
1175 /* We set the configuration at this point, since we now have a tty */
1176 static int notifier_add_vio(struct hvc_struct *hp, int data)
1180 port = find_port_by_vtermno(hp->vtermno);
1184 hp->irq_requested = 1;
1185 resize_console(port);
1190 static void notifier_del_vio(struct hvc_struct *hp, int data)
1192 hp->irq_requested = 0;
1195 /* The operations for console ports. */
1196 static const struct hv_ops hv_ops = {
1197 .get_chars = get_chars,
1198 .put_chars = put_chars,
1199 .notifier_add = notifier_add_vio,
1200 .notifier_del = notifier_del_vio,
1201 .notifier_hangup = notifier_del_vio,
1205 * Console drivers are initialized very early so boot messages can go
1206 * out, so we do things slightly differently from the generic virtio
1207 * initialization of the net and block drivers.
1209 * At this stage, the console is output-only. It's too early to set
1210 * up a virtqueue, so we let the drivers do some boutique early-output
1213 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
1215 early_put_chars = put_chars;
1216 return hvc_instantiate(0, 0, &hv_ops);
1219 static int init_port_console(struct port *port)
1224 * The Host's telling us this port is a console port. Hook it
1225 * up with an hvc console.
1227 * To set up and manage our virtual console, we call
1230 * The first argument of hvc_alloc() is the virtual console
1231 * number. The second argument is the parameter for the
1232 * notification mechanism (like irq number). We currently
1233 * leave this as zero, virtqueues have implicit notifications.
1235 * The third argument is a "struct hv_ops" containing the
1236 * put_chars() get_chars(), notifier_add() and notifier_del()
1237 * pointers. The final argument is the output buffer size: we
1238 * can do any size, so we put PAGE_SIZE here.
1240 ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL);
1244 port->cons.vtermno = ret;
1245 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1246 if (IS_ERR(port->cons.hvc)) {
1247 ret = PTR_ERR(port->cons.hvc);
1249 "error %d allocating hvc for port\n", ret);
1250 port->cons.hvc = NULL;
1251 ida_free(&vtermno_ida, port->cons.vtermno);
1254 spin_lock_irq(&pdrvdata_lock);
1255 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1256 spin_unlock_irq(&pdrvdata_lock);
1257 port->guest_connected = true;
1260 * Start using the new console output if this is the first
1261 * console to come up.
1263 if (early_put_chars)
1264 early_put_chars = NULL;
1266 /* Notify host of port being opened */
1267 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1272 static ssize_t show_port_name(struct device *dev,
1273 struct device_attribute *attr, char *buffer)
1277 port = dev_get_drvdata(dev);
1279 return sprintf(buffer, "%s\n", port->name);
1282 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1284 static struct attribute *port_sysfs_entries[] = {
1285 &dev_attr_name.attr,
1289 static const struct attribute_group port_attribute_group = {
1290 .name = NULL, /* put in device directory */
1291 .attrs = port_sysfs_entries,
1294 static int port_debugfs_show(struct seq_file *s, void *data)
1296 struct port *port = s->private;
1298 seq_printf(s, "name: %s\n", port->name ? port->name : "");
1299 seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1300 seq_printf(s, "host_connected: %d\n", port->host_connected);
1301 seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1302 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1303 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1304 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1305 seq_printf(s, "is_console: %s\n",
1306 is_console_port(port) ? "yes" : "no");
1307 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
1312 DEFINE_SHOW_ATTRIBUTE(port_debugfs);
1314 static void set_console_size(struct port *port, u16 rows, u16 cols)
1316 if (!port || !is_console_port(port))
1319 port->cons.ws.ws_row = rows;
1320 port->cons.ws.ws_col = cols;
1323 static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1325 struct port_buffer *buf;
1331 buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
1335 spin_lock_irq(lock);
1336 ret = add_inbuf(vq, buf);
1338 spin_unlock_irq(lock);
1339 free_buf(buf, true);
1343 spin_unlock_irq(lock);
1346 return nr_added_bufs;
1349 static void send_sigio_to_port(struct port *port)
1351 if (port->async_queue && port->guest_connected)
1352 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1355 static int add_port(struct ports_device *portdev, u32 id)
1357 char debugfs_name[16];
1362 port = kmalloc(sizeof(*port), GFP_KERNEL);
1367 kref_init(&port->kref);
1369 port->portdev = portdev;
1374 port->cons.hvc = NULL;
1375 port->async_queue = NULL;
1377 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1378 port->cons.vtermno = 0;
1380 port->host_connected = port->guest_connected = false;
1381 port->stats = (struct port_stats) { 0 };
1383 port->outvq_full = false;
1385 port->in_vq = portdev->in_vqs[port->id];
1386 port->out_vq = portdev->out_vqs[port->id];
1388 port->cdev = cdev_alloc();
1390 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1394 port->cdev->ops = &port_fops;
1396 devt = MKDEV(portdev->chr_major, id);
1397 err = cdev_add(port->cdev, devt, 1);
1399 dev_err(&port->portdev->vdev->dev,
1400 "Error %d adding cdev for port %u\n", err, id);
1403 port->dev = device_create(&port_class, &port->portdev->vdev->dev,
1404 devt, port, "vport%up%u",
1405 port->portdev->vdev->index, id);
1406 if (IS_ERR(port->dev)) {
1407 err = PTR_ERR(port->dev);
1408 dev_err(&port->portdev->vdev->dev,
1409 "Error %d creating device for port %u\n",
1414 spin_lock_init(&port->inbuf_lock);
1415 spin_lock_init(&port->outvq_lock);
1416 init_waitqueue_head(&port->waitqueue);
1418 /* We can safely ignore ENOSPC because it means
1419 * the queue already has buffers. Buffers are removed
1420 * only by virtcons_remove(), not by unplug_port()
1422 err = fill_queue(port->in_vq, &port->inbuf_lock);
1423 if (err < 0 && err != -ENOSPC) {
1424 dev_err(port->dev, "Error allocating inbufs\n");
1428 if (is_rproc_serial(port->portdev->vdev))
1430 * For rproc_serial assume remote processor is connected.
1431 * rproc_serial does not want the console port, only
1432 * the generic port implementation.
1434 port->host_connected = true;
1435 else if (!use_multiport(port->portdev)) {
1437 * If we're not using multiport support,
1438 * this has to be a console port.
1440 err = init_port_console(port);
1445 spin_lock_irq(&portdev->ports_lock);
1446 list_add_tail(&port->list, &port->portdev->ports);
1447 spin_unlock_irq(&portdev->ports_lock);
1450 * Tell the Host we're set so that it can send us various
1451 * configuration parameters for this port (eg, port name,
1452 * caching, whether this is a console port, etc.)
1454 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1457 * Finally, create the debugfs file that we can use to
1458 * inspect a port's state at any time
1460 snprintf(debugfs_name, sizeof(debugfs_name), "vport%up%u",
1461 port->portdev->vdev->index, id);
1462 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1463 pdrvdata.debugfs_dir,
1464 port, &port_debugfs_fops);
1469 device_destroy(&port_class, port->dev->devt);
1471 cdev_del(port->cdev);
1475 /* The host might want to notify management sw about port add failure */
1476 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1480 /* No users remain, remove all port-specific data. */
1481 static void remove_port(struct kref *kref)
1485 port = container_of(kref, struct port, kref);
1490 static void remove_port_data(struct port *port)
1492 spin_lock_irq(&port->inbuf_lock);
1493 /* Remove unused data this port might have received. */
1494 discard_port_data(port);
1495 spin_unlock_irq(&port->inbuf_lock);
1497 spin_lock_irq(&port->outvq_lock);
1498 reclaim_consumed_buffers(port);
1499 spin_unlock_irq(&port->outvq_lock);
1503 * Port got unplugged. Remove port from portdev's list and drop the
1504 * kref reference. If no userspace has this port opened, it will
1505 * result in immediate removal the port.
1507 static void unplug_port(struct port *port)
1509 spin_lock_irq(&port->portdev->ports_lock);
1510 list_del(&port->list);
1511 spin_unlock_irq(&port->portdev->ports_lock);
1513 spin_lock_irq(&port->inbuf_lock);
1514 if (port->guest_connected) {
1515 /* Let the app know the port is going down. */
1516 send_sigio_to_port(port);
1518 /* Do this after sigio is actually sent */
1519 port->guest_connected = false;
1520 port->host_connected = false;
1522 wake_up_interruptible(&port->waitqueue);
1524 spin_unlock_irq(&port->inbuf_lock);
1526 if (is_console_port(port)) {
1527 spin_lock_irq(&pdrvdata_lock);
1528 list_del(&port->cons.list);
1529 spin_unlock_irq(&pdrvdata_lock);
1530 hvc_remove(port->cons.hvc);
1531 ida_free(&vtermno_ida, port->cons.vtermno);
1534 remove_port_data(port);
1537 * We should just assume the device itself has gone off --
1538 * else a close on an open port later will try to send out a
1541 port->portdev = NULL;
1543 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1544 device_destroy(&port_class, port->dev->devt);
1545 cdev_del(port->cdev);
1547 debugfs_remove(port->debugfs_file);
1551 * Locks around here are not necessary - a port can't be
1552 * opened after we removed the port struct from ports_list
1555 kref_put(&port->kref, remove_port);
1558 /* Any private messages that the Host and Guest want to share */
1559 static void handle_control_message(struct virtio_device *vdev,
1560 struct ports_device *portdev,
1561 struct port_buffer *buf)
1563 struct virtio_console_control *cpkt;
1568 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1570 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1572 cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
1573 /* No valid header at start of buffer. Drop it. */
1574 dev_dbg(&portdev->vdev->dev,
1575 "Invalid index %u in control packet\n", cpkt->id);
1579 switch (virtio16_to_cpu(vdev, cpkt->event)) {
1580 case VIRTIO_CONSOLE_PORT_ADD:
1582 dev_dbg(&portdev->vdev->dev,
1583 "Port %u already added\n", port->id);
1584 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1587 if (virtio32_to_cpu(vdev, cpkt->id) >=
1588 portdev->max_nr_ports) {
1589 dev_warn(&portdev->vdev->dev,
1590 "Request for adding port with "
1591 "out-of-bound id %u, max. supported id: %u\n",
1592 cpkt->id, portdev->max_nr_ports - 1);
1595 add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
1597 case VIRTIO_CONSOLE_PORT_REMOVE:
1600 case VIRTIO_CONSOLE_CONSOLE_PORT:
1603 if (is_console_port(port))
1606 init_port_console(port);
1607 complete(&early_console_added);
1609 * Could remove the port here in case init fails - but
1610 * have to notify the host first.
1613 case VIRTIO_CONSOLE_RESIZE: {
1619 if (!is_console_port(port))
1622 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1624 set_console_size(port, size.rows, size.cols);
1626 port->cons.hvc->irq_requested = 1;
1627 resize_console(port);
1630 case VIRTIO_CONSOLE_PORT_OPEN:
1631 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
1632 wake_up_interruptible(&port->waitqueue);
1634 * If the host port got closed and the host had any
1635 * unconsumed buffers, we'll be able to reclaim them
1638 spin_lock_irq(&port->outvq_lock);
1639 reclaim_consumed_buffers(port);
1640 spin_unlock_irq(&port->outvq_lock);
1643 * If the guest is connected, it'll be interested in
1644 * knowing the host connection state changed.
1646 spin_lock_irq(&port->inbuf_lock);
1647 send_sigio_to_port(port);
1648 spin_unlock_irq(&port->inbuf_lock);
1650 case VIRTIO_CONSOLE_PORT_NAME:
1652 * If we woke up after hibernation, we can get this
1653 * again. Skip it in that case.
1659 * Skip the size of the header and the cpkt to get the size
1660 * of the name that was sent
1662 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1664 port->name = kmalloc(name_size, GFP_KERNEL);
1667 "Not enough space to store port name\n");
1670 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1674 * Since we only have one sysfs attribute, 'name',
1675 * create it only if we have a name for the port.
1677 err = sysfs_create_group(&port->dev->kobj,
1678 &port_attribute_group);
1681 "Error %d creating sysfs device attributes\n",
1685 * Generate a udev event so that appropriate
1686 * symlinks can be created based on udev
1689 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1695 static void control_work_handler(struct work_struct *work)
1697 struct ports_device *portdev;
1698 struct virtqueue *vq;
1699 struct port_buffer *buf;
1702 portdev = container_of(work, struct ports_device, control_work);
1703 vq = portdev->c_ivq;
1705 spin_lock(&portdev->c_ivq_lock);
1706 while ((buf = virtqueue_get_buf(vq, &len))) {
1707 spin_unlock(&portdev->c_ivq_lock);
1709 buf->len = min_t(size_t, len, buf->size);
1712 handle_control_message(vq->vdev, portdev, buf);
1714 spin_lock(&portdev->c_ivq_lock);
1715 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1716 dev_warn(&portdev->vdev->dev,
1717 "Error adding buffer to queue\n");
1718 free_buf(buf, false);
1721 spin_unlock(&portdev->c_ivq_lock);
1724 static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1726 struct port_buffer *buf;
1729 while ((buf = virtqueue_get_buf(vq, &len)))
1730 free_buf(buf, can_sleep);
1733 static void out_intr(struct virtqueue *vq)
1737 port = find_port_by_vq(vq->vdev->priv, vq);
1739 flush_bufs(vq, false);
1743 wake_up_interruptible(&port->waitqueue);
1746 static void in_intr(struct virtqueue *vq)
1749 unsigned long flags;
1751 port = find_port_by_vq(vq->vdev->priv, vq);
1753 flush_bufs(vq, false);
1757 spin_lock_irqsave(&port->inbuf_lock, flags);
1758 port->inbuf = get_inbuf(port);
1761 * Normally the port should not accept data when the port is
1762 * closed. For generic serial ports, the host won't (shouldn't)
1763 * send data till the guest is connected. But this condition
1764 * can be reached when a console port is not yet connected (no
1765 * tty is spawned) and the other side sends out data over the
1766 * vring, or when a remote devices start sending data before
1767 * the ports are opened.
1769 * A generic serial port will discard data if not connected,
1770 * while console ports and rproc-serial ports accepts data at
1771 * any time. rproc-serial is initiated with guest_connected to
1772 * false because port_fops_open expects this. Console ports are
1773 * hooked up with an HVC console and is initialized with
1774 * guest_connected to true.
1777 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1778 discard_port_data(port);
1780 /* Send a SIGIO indicating new data in case the process asked for it */
1781 send_sigio_to_port(port);
1783 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1785 wake_up_interruptible(&port->waitqueue);
1787 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1791 static void control_intr(struct virtqueue *vq)
1793 struct ports_device *portdev;
1795 portdev = vq->vdev->priv;
1796 schedule_work(&portdev->control_work);
1799 static void config_intr(struct virtio_device *vdev)
1801 struct ports_device *portdev;
1803 portdev = vdev->priv;
1805 if (!use_multiport(portdev))
1806 schedule_work(&portdev->config_work);
1809 static void config_work_handler(struct work_struct *work)
1811 struct ports_device *portdev;
1813 portdev = container_of(work, struct ports_device, config_work);
1814 if (!use_multiport(portdev)) {
1815 struct virtio_device *vdev;
1819 vdev = portdev->vdev;
1820 virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1821 virtio_cread(vdev, struct virtio_console_config, rows, &rows);
1823 port = find_port_by_id(portdev, 0);
1824 set_console_size(port, rows, cols);
1827 * We'll use this way of resizing only for legacy
1828 * support. For newer userspace
1829 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1830 * to indicate console size changes so that it can be
1833 resize_console(port);
1837 static int init_vqs(struct ports_device *portdev)
1839 vq_callback_t **io_callbacks;
1841 struct virtqueue **vqs;
1842 u32 i, j, nr_ports, nr_queues;
1845 nr_ports = portdev->max_nr_ports;
1846 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1848 vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
1849 io_callbacks = kmalloc_array(nr_queues, sizeof(vq_callback_t *),
1851 io_names = kmalloc_array(nr_queues, sizeof(char *), GFP_KERNEL);
1852 portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1854 portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1856 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
1857 !portdev->out_vqs) {
1863 * For backward compat (newer host but older guest), the host
1864 * spawns a console port first and also inits the vqs for port
1868 io_callbacks[j] = in_intr;
1869 io_callbacks[j + 1] = out_intr;
1870 io_names[j] = "input";
1871 io_names[j + 1] = "output";
1874 if (use_multiport(portdev)) {
1875 io_callbacks[j] = control_intr;
1876 io_callbacks[j + 1] = NULL;
1877 io_names[j] = "control-i";
1878 io_names[j + 1] = "control-o";
1880 for (i = 1; i < nr_ports; i++) {
1882 io_callbacks[j] = in_intr;
1883 io_callbacks[j + 1] = out_intr;
1884 io_names[j] = "input";
1885 io_names[j + 1] = "output";
1888 /* Find the queues. */
1889 err = virtio_find_vqs(portdev->vdev, nr_queues, vqs,
1891 (const char **)io_names, NULL);
1896 portdev->in_vqs[0] = vqs[0];
1897 portdev->out_vqs[0] = vqs[1];
1899 if (use_multiport(portdev)) {
1900 portdev->c_ivq = vqs[j];
1901 portdev->c_ovq = vqs[j + 1];
1903 for (i = 1; i < nr_ports; i++) {
1905 portdev->in_vqs[i] = vqs[j];
1906 portdev->out_vqs[i] = vqs[j + 1];
1910 kfree(io_callbacks);
1916 kfree(portdev->out_vqs);
1917 kfree(portdev->in_vqs);
1919 kfree(io_callbacks);
1925 static const struct file_operations portdev_fops = {
1926 .owner = THIS_MODULE,
1929 static void remove_vqs(struct ports_device *portdev)
1931 struct virtqueue *vq;
1933 virtio_device_for_each_vq(portdev->vdev, vq) {
1934 struct port_buffer *buf;
1936 flush_bufs(vq, true);
1937 while ((buf = virtqueue_detach_unused_buf(vq)))
1938 free_buf(buf, true);
1941 portdev->vdev->config->del_vqs(portdev->vdev);
1942 kfree(portdev->in_vqs);
1943 kfree(portdev->out_vqs);
1946 static void virtcons_remove(struct virtio_device *vdev)
1948 struct ports_device *portdev;
1949 struct port *port, *port2;
1951 portdev = vdev->priv;
1953 spin_lock_irq(&pdrvdata_lock);
1954 list_del(&portdev->list);
1955 spin_unlock_irq(&pdrvdata_lock);
1957 /* Device is going away, exit any polling for buffers */
1958 virtio_break_device(vdev);
1959 if (use_multiport(portdev))
1960 flush_work(&portdev->control_work);
1962 flush_work(&portdev->config_work);
1964 /* Disable interrupts for vqs */
1965 virtio_reset_device(vdev);
1966 /* Finish up work that's lined up */
1967 if (use_multiport(portdev))
1968 cancel_work_sync(&portdev->control_work);
1970 cancel_work_sync(&portdev->config_work);
1972 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1975 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1978 * When yanking out a device, we immediately lose the
1979 * (device-side) queues. So there's no point in keeping the
1980 * guest side around till we drop our final reference. This
1981 * also means that any ports which are in an open state will
1982 * have to just stop using the port, as the vqs are going
1985 remove_vqs(portdev);
1990 * Once we're further in boot, we get probed like any other virtio
1993 * If the host also supports multiple console ports, we check the
1994 * config space to see how many ports the host has spawned. We
1995 * initialize each port found.
1997 static int virtcons_probe(struct virtio_device *vdev)
1999 struct ports_device *portdev;
2002 bool early = early_put_chars != NULL;
2004 /* We only need a config space if features are offered */
2005 if (!vdev->config->get &&
2006 (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
2007 || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
2008 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2013 /* Ensure to read early_put_chars now */
2016 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
2022 /* Attach this portdev to this virtio_device, and vice-versa. */
2023 portdev->vdev = vdev;
2024 vdev->priv = portdev;
2026 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
2028 if (portdev->chr_major < 0) {
2030 "Error %d registering chrdev for device %u\n",
2031 portdev->chr_major, vdev->index);
2032 err = portdev->chr_major;
2037 portdev->max_nr_ports = 1;
2039 /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
2040 if (!is_rproc_serial(vdev) &&
2041 virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
2042 struct virtio_console_config, max_nr_ports,
2043 &portdev->max_nr_ports) == 0) {
2044 if (portdev->max_nr_ports == 0 ||
2045 portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
2047 "Invalidate max_nr_ports %d",
2048 portdev->max_nr_ports);
2055 err = init_vqs(portdev);
2057 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
2061 spin_lock_init(&portdev->ports_lock);
2062 INIT_LIST_HEAD(&portdev->ports);
2063 INIT_LIST_HEAD(&portdev->list);
2065 virtio_device_ready(portdev->vdev);
2067 INIT_WORK(&portdev->config_work, &config_work_handler);
2068 INIT_WORK(&portdev->control_work, &control_work_handler);
2071 spin_lock_init(&portdev->c_ivq_lock);
2072 spin_lock_init(&portdev->c_ovq_lock);
2074 err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2077 "Error allocating buffers for control queue\n");
2079 * The host might want to notify mgmt sw about device
2082 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2083 VIRTIO_CONSOLE_DEVICE_READY, 0);
2084 /* Device was functional: we need full cleanup. */
2085 virtcons_remove(vdev);
2090 * For backward compatibility: Create a console port
2091 * if we're running on older host.
2093 add_port(portdev, 0);
2096 spin_lock_irq(&pdrvdata_lock);
2097 list_add_tail(&portdev->list, &pdrvdata.portdevs);
2098 spin_unlock_irq(&pdrvdata_lock);
2100 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2101 VIRTIO_CONSOLE_DEVICE_READY, 1);
2104 * If there was an early virtio console, assume that there are no
2105 * other consoles. We need to wait until the hvc_alloc matches the
2106 * hvc_instantiate, otherwise tty_open will complain, resulting in
2107 * a "Warning: unable to open an initial console" boot failure.
2108 * Without multiport this is done in add_port above. With multiport
2109 * this might take some host<->guest communication - thus we have to
2112 if (multiport && early)
2113 wait_for_completion(&early_console_added);
2118 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
2125 static const struct virtio_device_id id_table[] = {
2126 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
2129 MODULE_DEVICE_TABLE(virtio, id_table);
2131 static const unsigned int features[] = {
2132 VIRTIO_CONSOLE_F_SIZE,
2133 VIRTIO_CONSOLE_F_MULTIPORT,
2136 static const struct virtio_device_id rproc_serial_id_table[] = {
2137 #if IS_ENABLED(CONFIG_REMOTEPROC)
2138 { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
2142 MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
2144 static const unsigned int rproc_serial_features[] = {
2147 #ifdef CONFIG_PM_SLEEP
2148 static int virtcons_freeze(struct virtio_device *vdev)
2150 struct ports_device *portdev;
2153 portdev = vdev->priv;
2155 virtio_reset_device(vdev);
2157 if (use_multiport(portdev))
2158 virtqueue_disable_cb(portdev->c_ivq);
2159 cancel_work_sync(&portdev->control_work);
2160 cancel_work_sync(&portdev->config_work);
2162 * Once more: if control_work_handler() was running, it would
2163 * enable the cb as the last step.
2165 if (use_multiport(portdev))
2166 virtqueue_disable_cb(portdev->c_ivq);
2168 list_for_each_entry(port, &portdev->ports, list) {
2169 virtqueue_disable_cb(port->in_vq);
2170 virtqueue_disable_cb(port->out_vq);
2172 * We'll ask the host later if the new invocation has
2173 * the port opened or closed.
2175 port->host_connected = false;
2176 remove_port_data(port);
2178 remove_vqs(portdev);
2183 static int virtcons_restore(struct virtio_device *vdev)
2185 struct ports_device *portdev;
2189 portdev = vdev->priv;
2191 ret = init_vqs(portdev);
2195 virtio_device_ready(portdev->vdev);
2197 if (use_multiport(portdev))
2198 fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2200 list_for_each_entry(port, &portdev->ports, list) {
2201 port->in_vq = portdev->in_vqs[port->id];
2202 port->out_vq = portdev->out_vqs[port->id];
2204 fill_queue(port->in_vq, &port->inbuf_lock);
2206 /* Get port open/close status on the host */
2207 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2210 * If a port was open at the time of suspending, we
2211 * have to let the host know that it's still open.
2213 if (port->guest_connected)
2214 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
2220 static struct virtio_driver virtio_console = {
2221 .feature_table = features,
2222 .feature_table_size = ARRAY_SIZE(features),
2223 .driver.name = KBUILD_MODNAME,
2224 .driver.owner = THIS_MODULE,
2225 .id_table = id_table,
2226 .probe = virtcons_probe,
2227 .remove = virtcons_remove,
2228 .config_changed = config_intr,
2229 #ifdef CONFIG_PM_SLEEP
2230 .freeze = virtcons_freeze,
2231 .restore = virtcons_restore,
2235 static struct virtio_driver virtio_rproc_serial = {
2236 .feature_table = rproc_serial_features,
2237 .feature_table_size = ARRAY_SIZE(rproc_serial_features),
2238 .driver.name = "virtio_rproc_serial",
2239 .driver.owner = THIS_MODULE,
2240 .id_table = rproc_serial_id_table,
2241 .probe = virtcons_probe,
2242 .remove = virtcons_remove,
2245 static int __init virtio_console_init(void)
2249 err = class_register(&port_class);
2253 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2254 INIT_LIST_HEAD(&pdrvdata.consoles);
2255 INIT_LIST_HEAD(&pdrvdata.portdevs);
2257 err = register_virtio_driver(&virtio_console);
2259 pr_err("Error %d registering virtio driver\n", err);
2262 err = register_virtio_driver(&virtio_rproc_serial);
2264 pr_err("Error %d registering virtio rproc serial driver\n",
2270 unregister_virtio_driver(&virtio_console);
2272 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2273 class_unregister(&port_class);
2277 static void __exit virtio_console_fini(void)
2281 unregister_virtio_driver(&virtio_console);
2282 unregister_virtio_driver(&virtio_rproc_serial);
2284 class_unregister(&port_class);
2285 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2287 module_init(virtio_console_init);
2288 module_exit(virtio_console_fini);
2290 MODULE_DESCRIPTION("Virtio console driver");
2291 MODULE_LICENSE("GPL");