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 <linux/string_choices.h>
30 #include "../tty/hvc/hvc_console.h"
32 #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
33 #define VIRTCONS_MAX_PORTS 0x8000
36 * This is a global struct for storing common data for all the devices
37 * this driver handles.
39 * Mainly, it has a linked list for all the consoles in one place so
40 * that callbacks from hvc for get_chars(), put_chars() work properly
41 * across multiple devices and multiple ports per device.
43 struct ports_driver_data {
44 /* Used for exporting per-port information to debugfs */
45 struct dentry *debugfs_dir;
47 /* List of all the devices we're handling */
48 struct list_head portdevs;
50 /* All the console devices handled by this driver */
51 struct list_head consoles;
54 static struct ports_driver_data pdrvdata;
56 static const struct class port_class = {
57 .name = "virtio-ports",
60 static DEFINE_SPINLOCK(pdrvdata_lock);
61 static DECLARE_COMPLETION(early_console_added);
63 /* This struct holds information that's relevant only for console ports */
65 /* We'll place all consoles in a list in the pdrvdata struct */
66 struct list_head list;
68 /* The hvc device associated with this console port */
69 struct hvc_struct *hvc;
71 /* The size of the console */
75 * This number identifies the number that we used to register
76 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
77 * number passed on by the hvc callbacks to us to
78 * differentiate between the other console ports handled by
84 static DEFINE_IDA(vtermno_ida);
89 /* size of the buffer in *buf above */
92 /* used length of the buffer */
94 /* offset in the buf from which to consume data */
97 /* DMA address of buffer */
100 /* Device we got DMA memory from */
103 /* List of pending dma buffers to free */
104 struct list_head list;
106 /* If sgpages == 0 then buf is used */
107 unsigned int sgpages;
109 /* sg is used if spages > 0. sg must be the last in is struct */
110 struct scatterlist sg[] __counted_by(sgpages);
114 * This is a per-device struct that stores data common to all the
115 * ports for that device (vdev->priv).
117 struct ports_device {
118 /* Next portdev in the list, head is in the pdrvdata struct */
119 struct list_head list;
122 * Workqueue handlers where we process deferred work after
125 struct work_struct control_work;
126 struct work_struct config_work;
128 struct list_head ports;
130 /* To protect the list of ports */
131 spinlock_t ports_lock;
133 /* To protect the vq operations for the control channel */
134 spinlock_t c_ivq_lock;
135 spinlock_t c_ovq_lock;
137 /* max. number of ports this device can hold */
140 /* The virtio device we're associated with */
141 struct virtio_device *vdev;
144 * A couple of virtqueues for the control channel: one for
145 * guest->host transfers, one for host->guest transfers
147 struct virtqueue *c_ivq, *c_ovq;
150 * A control packet buffer for guest->host requests, protected
153 struct virtio_console_control cpkt;
155 /* Array of per-port IO virtqueues */
156 struct virtqueue **in_vqs, **out_vqs;
158 /* Major number for this device. Ports will be created as minors. */
163 unsigned long bytes_sent, bytes_received, bytes_discarded;
166 /* This struct holds the per-port data */
168 /* Next port in the list, head is in the ports_device */
169 struct list_head list;
171 /* Pointer to the parent virtio_console device */
172 struct ports_device *portdev;
174 /* The current buffer from which data has to be fed to readers */
175 struct port_buffer *inbuf;
178 * To protect the operations on the in_vq associated with this
179 * port. Has to be a spinlock because it can be called from
180 * interrupt context (get_char()).
182 spinlock_t inbuf_lock;
184 /* Protect the operations on the out_vq. */
185 spinlock_t outvq_lock;
187 /* The IO vqs for this port */
188 struct virtqueue *in_vq, *out_vq;
190 /* File in the debugfs directory that exposes this port's information */
191 struct dentry *debugfs_file;
194 * Keep count of the bytes sent, received and discarded for
195 * this port for accounting and debugging purposes. These
196 * counts are not reset across port open / close events.
198 struct port_stats stats;
201 * The entries in this struct will be valid if this port is
202 * hooked up to an hvc console
206 /* Each port associates with a separate char device */
210 /* Reference-counting to handle port hot-unplugs and file operations */
213 /* A waitqueue for poll() or blocking read operations */
214 wait_queue_head_t waitqueue;
216 /* The 'name' of the port that we expose via sysfs properties */
219 /* We can notify apps of host connect / disconnect events via SIGIO */
220 struct fasync_struct *async_queue;
222 /* The 'id' to identify the port with the Host */
227 /* Is the host device open */
230 /* We should allow only one process to open a port */
231 bool guest_connected;
234 static struct port *find_port_by_vtermno(u32 vtermno)
237 struct console *cons;
240 spin_lock_irqsave(&pdrvdata_lock, flags);
241 list_for_each_entry(cons, &pdrvdata.consoles, list) {
242 if (cons->vtermno == vtermno) {
243 port = container_of(cons, struct port, cons);
249 spin_unlock_irqrestore(&pdrvdata_lock, flags);
253 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
259 spin_lock_irqsave(&portdev->ports_lock, flags);
260 list_for_each_entry(port, &portdev->ports, list) {
261 if (port->cdev->dev == dev) {
262 kref_get(&port->kref);
268 spin_unlock_irqrestore(&portdev->ports_lock, flags);
273 static struct port *find_port_by_devt(dev_t dev)
275 struct ports_device *portdev;
279 spin_lock_irqsave(&pdrvdata_lock, flags);
280 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
281 port = find_port_by_devt_in_portdev(portdev, dev);
287 spin_unlock_irqrestore(&pdrvdata_lock, flags);
291 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
296 spin_lock_irqsave(&portdev->ports_lock, flags);
297 list_for_each_entry(port, &portdev->ports, list)
302 spin_unlock_irqrestore(&portdev->ports_lock, flags);
307 static struct port *find_port_by_vq(struct ports_device *portdev,
308 struct virtqueue *vq)
313 spin_lock_irqsave(&portdev->ports_lock, flags);
314 list_for_each_entry(port, &portdev->ports, list)
315 if (port->in_vq == vq || port->out_vq == vq)
319 spin_unlock_irqrestore(&portdev->ports_lock, flags);
323 static bool is_console_port(struct port *port)
330 static bool is_rproc_serial(const struct virtio_device *vdev)
332 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
335 static inline bool use_multiport(struct ports_device *portdev)
338 * This condition can be true when put_chars is called from
343 return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
346 static DEFINE_SPINLOCK(dma_bufs_lock);
347 static LIST_HEAD(pending_free_dma_bufs);
349 static void free_buf(struct port_buffer *buf, bool can_sleep)
353 for (i = 0; i < buf->sgpages; i++) {
354 struct page *page = sg_page(&buf->sg[i]);
362 } else if (is_rproc_enabled) {
365 /* dma_free_coherent requires interrupts to be enabled. */
367 /* queue up dma-buffers to be freed later */
368 spin_lock_irqsave(&dma_bufs_lock, flags);
369 list_add_tail(&buf->list, &pending_free_dma_bufs);
370 spin_unlock_irqrestore(&dma_bufs_lock, flags);
373 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
375 /* Release device refcnt and allow it to be freed */
376 put_device(buf->dev);
382 static void reclaim_dma_bufs(void)
385 struct port_buffer *buf, *tmp;
388 if (list_empty(&pending_free_dma_bufs))
391 /* Create a copy of the pending_free_dma_bufs while holding the lock */
392 spin_lock_irqsave(&dma_bufs_lock, flags);
393 list_cut_position(&tmp_list, &pending_free_dma_bufs,
394 pending_free_dma_bufs.prev);
395 spin_unlock_irqrestore(&dma_bufs_lock, flags);
397 /* Release the dma buffers, without irqs enabled */
398 list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
399 list_del(&buf->list);
404 static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
407 struct port_buffer *buf;
412 * Allocate buffer and the sg list. The sg list array is allocated
413 * directly after the port_buffer struct.
415 buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
419 buf->sgpages = pages;
426 if (is_rproc_serial(vdev)) {
428 * Allocate DMA memory from ancestor. When a virtio
429 * device is created by remoteproc, the DMA memory is
430 * associated with the parent device:
431 * virtioY => remoteprocX#vdevYbuffer.
433 buf->dev = vdev->dev.parent;
437 /* Increase device refcnt to avoid freeing it */
438 get_device(buf->dev);
439 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
443 buf->buf = kmalloc(buf_size, GFP_KERNEL);
450 buf->size = buf_size;
459 /* Callers should take appropriate locks */
460 static struct port_buffer *get_inbuf(struct port *port)
462 struct port_buffer *buf;
468 buf = virtqueue_get_buf(port->in_vq, &len);
470 buf->len = min_t(size_t, len, buf->size);
472 port->stats.bytes_received += len;
478 * Create a scatter-gather list representing our input buffer and put
481 * Callers should take appropriate locks.
483 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
485 struct scatterlist sg[1];
488 sg_init_one(sg, buf->buf, buf->size);
490 ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
497 /* Discard any unread data this port has. Callers lockers. */
498 static void discard_port_data(struct port *port)
500 struct port_buffer *buf;
503 if (!port->portdev) {
504 /* Device has been unplugged. vqs are already gone. */
507 buf = get_inbuf(port);
511 port->stats.bytes_discarded += buf->len - buf->offset;
512 if (add_inbuf(port->in_vq, buf) < 0) {
514 free_buf(buf, false);
517 buf = get_inbuf(port);
520 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
524 static bool port_has_data(struct port *port)
530 spin_lock_irqsave(&port->inbuf_lock, flags);
531 port->inbuf = get_inbuf(port);
535 spin_unlock_irqrestore(&port->inbuf_lock, flags);
539 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
540 unsigned int event, unsigned int value)
542 struct scatterlist sg[1];
543 struct virtqueue *vq;
546 if (!use_multiport(portdev))
551 spin_lock(&portdev->c_ovq_lock);
553 portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
554 portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
555 portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
557 sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
559 if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
561 while (!virtqueue_get_buf(vq, &len)
562 && !virtqueue_is_broken(vq))
566 spin_unlock(&portdev->c_ovq_lock);
570 static ssize_t send_control_msg(struct port *port, unsigned int event,
573 /* Did the port get unplugged before userspace closed it? */
575 return __send_control_msg(port->portdev, port->id, event, value);
580 /* Callers must take the port->outvq_lock */
581 static void reclaim_consumed_buffers(struct port *port)
583 struct port_buffer *buf;
586 if (!port->portdev) {
587 /* Device has been unplugged. vqs are already gone. */
590 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
591 free_buf(buf, false);
592 port->outvq_full = false;
596 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
597 int nents, size_t in_count,
598 void *data, bool nonblock)
600 struct virtqueue *out_vq;
605 out_vq = port->out_vq;
607 spin_lock_irqsave(&port->outvq_lock, flags);
609 reclaim_consumed_buffers(port);
611 err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
613 /* Tell Host to go! */
614 virtqueue_kick(out_vq);
621 if (out_vq->num_free == 0)
622 port->outvq_full = true;
628 * Wait till the host acknowledges it pushed out the data we
629 * sent. This is done for data from the hvc_console; the tty
630 * operations are performed with spinlocks held so we can't
631 * sleep here. An alternative would be to copy the data to a
632 * buffer and relax the spinning requirement. The downside is
633 * we need to kmalloc a GFP_ATOMIC buffer each time the
634 * console driver writes something out.
636 while (!virtqueue_get_buf(out_vq, &len)
637 && !virtqueue_is_broken(out_vq))
640 spin_unlock_irqrestore(&port->outvq_lock, flags);
642 port->stats.bytes_sent += in_count;
644 * We're expected to return the amount of data we wrote -- all
651 * Give out the data that's requested from the buffer that we have
654 static ssize_t fill_readbuf(struct port *port, u8 __user *out_buf,
655 size_t out_count, bool to_user)
657 struct port_buffer *buf;
660 if (!out_count || !port_has_data(port))
664 out_count = min(out_count, buf->len - buf->offset);
669 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
673 memcpy((__force u8 *)out_buf, buf->buf + buf->offset,
677 buf->offset += out_count;
679 if (buf->offset == buf->len) {
681 * We're done using all the data in this buffer.
682 * Re-queue so that the Host can send us more data.
684 spin_lock_irqsave(&port->inbuf_lock, flags);
687 if (add_inbuf(port->in_vq, buf) < 0)
688 dev_warn(port->dev, "failed add_buf\n");
690 spin_unlock_irqrestore(&port->inbuf_lock, flags);
692 /* Return the number of bytes actually copied */
696 /* The condition that must be true for polling to end */
697 static bool will_read_block(struct port *port)
699 if (!port->guest_connected) {
700 /* Port got hot-unplugged. Let's exit. */
703 return !port_has_data(port) && port->host_connected;
706 static bool will_write_block(struct port *port)
710 if (!port->guest_connected) {
711 /* Port got hot-unplugged. Let's exit. */
714 if (!port->host_connected)
717 spin_lock_irq(&port->outvq_lock);
719 * Check if the Host has consumed any buffers since we last
720 * sent data (this is only applicable for nonblocking ports).
722 reclaim_consumed_buffers(port);
723 ret = port->outvq_full;
724 spin_unlock_irq(&port->outvq_lock);
729 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
730 size_t count, loff_t *offp)
735 port = filp->private_data;
737 /* Port is hot-unplugged. */
738 if (!port->guest_connected)
741 if (!port_has_data(port)) {
743 * If nothing's connected on the host just return 0 in
744 * case of list_empty; this tells the userspace app
745 * that there's no connection
747 if (!port->host_connected)
749 if (filp->f_flags & O_NONBLOCK)
752 ret = wait_event_freezable(port->waitqueue,
753 !will_read_block(port));
757 /* Port got hot-unplugged while we were waiting above. */
758 if (!port->guest_connected)
761 * We could've received a disconnection message while we were
762 * waiting for more data.
764 * This check is not clubbed in the if() statement above as we
765 * might receive some data as well as the host could get
766 * disconnected after we got woken up from our wait. So we
767 * really want to give off whatever data we have and only then
768 * check for host_connected.
770 if (!port_has_data(port) && !port->host_connected)
773 return fill_readbuf(port, ubuf, count, true);
776 static int wait_port_writable(struct port *port, bool nonblock)
780 if (will_write_block(port)) {
784 ret = wait_event_freezable(port->waitqueue,
785 !will_write_block(port));
789 /* Port got hot-unplugged. */
790 if (!port->guest_connected)
796 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
797 size_t count, loff_t *offp)
800 struct port_buffer *buf;
803 struct scatterlist sg[1];
805 /* Userspace could be out to fool us */
809 port = filp->private_data;
811 nonblock = filp->f_flags & O_NONBLOCK;
813 ret = wait_port_writable(port, nonblock);
817 count = min((size_t)(32 * 1024), count);
819 buf = alloc_buf(port->portdev->vdev, count, 0);
823 ret = copy_from_user(buf->buf, ubuf, count);
830 * We now ask send_buf() to not spin for generic ports -- we
831 * can re-use the same code path that non-blocking file
832 * descriptors take for blocking file descriptors since the
833 * wait is already done and we're certain the write will go
834 * through to the host.
837 sg_init_one(sg, buf->buf, count);
838 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
840 if (nonblock && ret > 0)
853 struct scatterlist *sg;
856 static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
857 struct splice_desc *sd)
859 struct sg_list *sgl = sd->u.data;
860 unsigned int offset, len;
862 if (sgl->n == sgl->size)
865 /* Try lock this page */
866 if (pipe_buf_try_steal(pipe, buf)) {
867 /* Get reference and unlock page for moving */
869 unlock_page(buf->page);
871 len = min(buf->len, sd->len);
872 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
874 /* Failback to copying a page */
875 struct page *page = alloc_page(GFP_KERNEL);
881 offset = sd->pos & ~PAGE_MASK;
884 if (len + offset > PAGE_SIZE)
885 len = PAGE_SIZE - offset;
887 src = kmap_local_page(buf->page);
888 memcpy(page_address(page) + offset, src + buf->offset, len);
891 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
899 /* Faster zero-copy write by splicing */
900 static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
901 struct file *filp, loff_t *ppos,
902 size_t len, unsigned int flags)
904 struct port *port = filp->private_data;
907 struct port_buffer *buf;
908 struct splice_desc sd = {
914 unsigned int occupancy;
917 * Rproc_serial does not yet support splice. To support splice
918 * pipe_to_sg() must allocate dma-buffers and copy content from
919 * regular pages to dma pages. And alloc_buf and free_buf must
920 * support allocating and freeing such a list of dma-buffers.
922 if (is_rproc_serial(port->out_vq->vdev))
927 if (pipe_is_empty(pipe))
930 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
934 occupancy = pipe_buf_usage(pipe);
935 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
944 sgl.size = occupancy;
946 sg_init_table(sgl.sg, sgl.size);
947 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
950 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
952 if (unlikely(ret <= 0))
961 static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
966 port = filp->private_data;
967 poll_wait(filp, &port->waitqueue, wait);
969 if (!port->guest_connected) {
970 /* Port got unplugged */
974 if (!will_read_block(port))
975 ret |= EPOLLIN | EPOLLRDNORM;
976 if (!will_write_block(port))
978 if (!port->host_connected)
984 static void remove_port(struct kref *kref);
986 static int port_fops_release(struct inode *inode, struct file *filp)
990 port = filp->private_data;
992 /* Notify host of port being closed */
993 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
995 spin_lock_irq(&port->inbuf_lock);
996 port->guest_connected = false;
998 discard_port_data(port);
1000 spin_unlock_irq(&port->inbuf_lock);
1002 spin_lock_irq(&port->outvq_lock);
1003 reclaim_consumed_buffers(port);
1004 spin_unlock_irq(&port->outvq_lock);
1008 * Locks aren't necessary here as a port can't be opened after
1009 * unplug, and if a port isn't unplugged, a kref would already
1010 * exist for the port. Plus, taking ports_lock here would
1011 * create a dependency on other locks taken by functions
1012 * inside remove_port if we're the last holder of the port,
1013 * creating many problems.
1015 kref_put(&port->kref, remove_port);
1020 static int port_fops_open(struct inode *inode, struct file *filp)
1022 struct cdev *cdev = inode->i_cdev;
1026 /* We get the port with a kref here */
1027 port = find_port_by_devt(cdev->dev);
1029 /* Port was unplugged before we could proceed */
1032 filp->private_data = port;
1035 * Don't allow opening of console port devices -- that's done
1038 if (is_console_port(port)) {
1043 /* Allow only one process to open a particular port at a time */
1044 spin_lock_irq(&port->inbuf_lock);
1045 if (port->guest_connected) {
1046 spin_unlock_irq(&port->inbuf_lock);
1051 port->guest_connected = true;
1052 spin_unlock_irq(&port->inbuf_lock);
1054 spin_lock_irq(&port->outvq_lock);
1056 * There might be a chance that we missed reclaiming a few
1057 * buffers in the window of the port getting previously closed
1060 reclaim_consumed_buffers(port);
1061 spin_unlock_irq(&port->outvq_lock);
1063 nonseekable_open(inode, filp);
1065 /* Notify host of port being opened */
1066 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
1070 kref_put(&port->kref, remove_port);
1074 static int port_fops_fasync(int fd, struct file *filp, int mode)
1078 port = filp->private_data;
1079 return fasync_helper(fd, filp, mode, &port->async_queue);
1083 * The file operations that we support: programs in the guest can open
1084 * a console device, read from it, write to it, poll for data and
1085 * close it. The devices are at
1086 * /dev/vport<device number>p<port number>
1088 static const struct file_operations port_fops = {
1089 .owner = THIS_MODULE,
1090 .open = port_fops_open,
1091 .read = port_fops_read,
1092 .write = port_fops_write,
1093 .splice_write = port_fops_splice_write,
1094 .poll = port_fops_poll,
1095 .release = port_fops_release,
1096 .fasync = port_fops_fasync,
1100 * The put_chars() callback is pretty straightforward.
1102 * We turn the characters into a scatter-gather list, add it to the
1103 * output queue and then kick the Host. Then we sit here waiting for
1104 * it to finish: inefficient in theory, but in practice
1105 * implementations will do it immediately.
1107 static ssize_t put_chars(u32 vtermno, const u8 *buf, size_t count)
1110 struct scatterlist sg[1];
1114 port = find_port_by_vtermno(vtermno);
1118 data = kmemdup(buf, count, GFP_ATOMIC);
1122 sg_init_one(sg, data, count);
1123 ret = __send_to_port(port, sg, 1, count, data, false);
1129 * get_chars() is the callback from the hvc_console infrastructure
1130 * when an interrupt is received.
1132 * We call out to fill_readbuf that gets us the required data from the
1133 * buffers that are queued up.
1135 static ssize_t get_chars(u32 vtermno, u8 *buf, size_t count)
1139 port = find_port_by_vtermno(vtermno);
1143 /* If we don't have an input queue yet, we can't get input. */
1144 BUG_ON(!port->in_vq);
1146 return fill_readbuf(port, (__force u8 __user *)buf, count, false);
1149 static void resize_console(struct port *port)
1151 struct virtio_device *vdev;
1153 /* The port could have been hot-unplugged */
1154 if (!port || !is_console_port(port))
1157 vdev = port->portdev->vdev;
1159 /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
1160 if (!is_rproc_serial(vdev) &&
1161 virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1162 hvc_resize(port->cons.hvc, port->cons.ws);
1165 /* We set the configuration at this point, since we now have a tty */
1166 static int notifier_add_vio(struct hvc_struct *hp, int data)
1170 port = find_port_by_vtermno(hp->vtermno);
1174 hp->irq_requested = 1;
1175 resize_console(port);
1180 static void notifier_del_vio(struct hvc_struct *hp, int data)
1182 hp->irq_requested = 0;
1185 /* The operations for console ports. */
1186 static const struct hv_ops hv_ops = {
1187 .get_chars = get_chars,
1188 .put_chars = put_chars,
1189 .notifier_add = notifier_add_vio,
1190 .notifier_del = notifier_del_vio,
1191 .notifier_hangup = notifier_del_vio,
1194 static int init_port_console(struct port *port)
1199 * The Host's telling us this port is a console port. Hook it
1200 * up with an hvc console.
1202 * To set up and manage our virtual console, we call
1205 * The first argument of hvc_alloc() is the virtual console
1206 * number. The second argument is the parameter for the
1207 * notification mechanism (like irq number). We currently
1208 * leave this as zero, virtqueues have implicit notifications.
1210 * The third argument is a "struct hv_ops" containing the
1211 * put_chars() get_chars(), notifier_add() and notifier_del()
1212 * pointers. The final argument is the output buffer size: we
1213 * can do any size, so we put PAGE_SIZE here.
1215 ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL);
1219 port->cons.vtermno = ret;
1220 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1221 if (IS_ERR(port->cons.hvc)) {
1222 ret = PTR_ERR(port->cons.hvc);
1224 "error %d allocating hvc for port\n", ret);
1225 port->cons.hvc = NULL;
1226 ida_free(&vtermno_ida, port->cons.vtermno);
1229 spin_lock_irq(&pdrvdata_lock);
1230 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1231 spin_unlock_irq(&pdrvdata_lock);
1232 port->guest_connected = true;
1234 /* Notify host of port being opened */
1235 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1240 static ssize_t show_port_name(struct device *dev,
1241 struct device_attribute *attr, char *buffer)
1245 port = dev_get_drvdata(dev);
1247 return sprintf(buffer, "%s\n", port->name);
1250 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1252 static struct attribute *port_sysfs_entries[] = {
1253 &dev_attr_name.attr,
1257 static const struct attribute_group port_attribute_group = {
1258 .name = NULL, /* put in device directory */
1259 .attrs = port_sysfs_entries,
1262 static int port_debugfs_show(struct seq_file *s, void *data)
1264 struct port *port = s->private;
1266 seq_printf(s, "name: %s\n", port->name ? port->name : "");
1267 seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1268 seq_printf(s, "host_connected: %d\n", port->host_connected);
1269 seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1270 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1271 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1272 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1273 seq_printf(s, "is_console: %s\n", str_yes_no(is_console_port(port)));
1274 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
1279 DEFINE_SHOW_ATTRIBUTE(port_debugfs);
1281 static void set_console_size(struct port *port, u16 rows, u16 cols)
1283 if (!port || !is_console_port(port))
1286 port->cons.ws.ws_row = rows;
1287 port->cons.ws.ws_col = cols;
1290 static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1292 struct port_buffer *buf;
1298 buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
1302 spin_lock_irq(lock);
1303 ret = add_inbuf(vq, buf);
1305 spin_unlock_irq(lock);
1306 free_buf(buf, true);
1310 spin_unlock_irq(lock);
1313 return nr_added_bufs;
1316 static void send_sigio_to_port(struct port *port)
1318 if (port->async_queue && port->guest_connected)
1319 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1322 static int add_port(struct ports_device *portdev, u32 id)
1328 port = kmalloc(sizeof(*port), GFP_KERNEL);
1333 kref_init(&port->kref);
1335 port->portdev = portdev;
1340 port->cons.hvc = NULL;
1341 port->async_queue = NULL;
1343 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1344 port->cons.vtermno = 0;
1346 port->host_connected = port->guest_connected = false;
1347 port->stats = (struct port_stats) { 0 };
1349 port->outvq_full = false;
1351 port->in_vq = portdev->in_vqs[port->id];
1352 port->out_vq = portdev->out_vqs[port->id];
1354 port->cdev = cdev_alloc();
1356 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1360 port->cdev->ops = &port_fops;
1362 devt = MKDEV(portdev->chr_major, id);
1363 err = cdev_add(port->cdev, devt, 1);
1365 dev_err(&port->portdev->vdev->dev,
1366 "Error %d adding cdev for port %u\n", err, id);
1369 port->dev = device_create(&port_class, &port->portdev->vdev->dev,
1370 devt, port, "vport%up%u",
1371 port->portdev->vdev->index, id);
1372 if (IS_ERR(port->dev)) {
1373 err = PTR_ERR(port->dev);
1374 dev_err(&port->portdev->vdev->dev,
1375 "Error %d creating device for port %u\n",
1380 spin_lock_init(&port->inbuf_lock);
1381 spin_lock_init(&port->outvq_lock);
1382 init_waitqueue_head(&port->waitqueue);
1384 /* We can safely ignore ENOSPC because it means
1385 * the queue already has buffers. Buffers are removed
1386 * only by virtcons_remove(), not by unplug_port()
1388 err = fill_queue(port->in_vq, &port->inbuf_lock);
1389 if (err < 0 && err != -ENOSPC) {
1390 dev_err(port->dev, "Error allocating inbufs\n");
1394 if (is_rproc_serial(port->portdev->vdev))
1396 * For rproc_serial assume remote processor is connected.
1397 * rproc_serial does not want the console port, only
1398 * the generic port implementation.
1400 port->host_connected = true;
1401 else if (!use_multiport(port->portdev)) {
1403 * If we're not using multiport support,
1404 * this has to be a console port.
1406 err = init_port_console(port);
1411 spin_lock_irq(&portdev->ports_lock);
1412 list_add_tail(&port->list, &port->portdev->ports);
1413 spin_unlock_irq(&portdev->ports_lock);
1416 * Tell the Host we're set so that it can send us various
1417 * configuration parameters for this port (eg, port name,
1418 * caching, whether this is a console port, etc.)
1420 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1423 * Finally, create the debugfs file that we can use to
1424 * inspect a port's state at any time
1426 port->debugfs_file = debugfs_create_file(dev_name(port->dev), 0444,
1427 pdrvdata.debugfs_dir,
1428 port, &port_debugfs_fops);
1433 device_destroy(&port_class, port->dev->devt);
1435 cdev_del(port->cdev);
1439 /* The host might want to notify management sw about port add failure */
1440 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1444 /* No users remain, remove all port-specific data. */
1445 static void remove_port(struct kref *kref)
1449 port = container_of(kref, struct port, kref);
1454 static void remove_port_data(struct port *port)
1456 spin_lock_irq(&port->inbuf_lock);
1457 /* Remove unused data this port might have received. */
1458 discard_port_data(port);
1459 spin_unlock_irq(&port->inbuf_lock);
1461 spin_lock_irq(&port->outvq_lock);
1462 reclaim_consumed_buffers(port);
1463 spin_unlock_irq(&port->outvq_lock);
1467 * Port got unplugged. Remove port from portdev's list and drop the
1468 * kref reference. If no userspace has this port opened, it will
1469 * result in immediate removal the port.
1471 static void unplug_port(struct port *port)
1473 spin_lock_irq(&port->portdev->ports_lock);
1474 list_del(&port->list);
1475 spin_unlock_irq(&port->portdev->ports_lock);
1477 spin_lock_irq(&port->inbuf_lock);
1478 if (port->guest_connected) {
1479 /* Let the app know the port is going down. */
1480 send_sigio_to_port(port);
1482 /* Do this after sigio is actually sent */
1483 port->guest_connected = false;
1484 port->host_connected = false;
1486 wake_up_interruptible(&port->waitqueue);
1488 spin_unlock_irq(&port->inbuf_lock);
1490 if (is_console_port(port)) {
1491 spin_lock_irq(&pdrvdata_lock);
1492 list_del(&port->cons.list);
1493 spin_unlock_irq(&pdrvdata_lock);
1494 hvc_remove(port->cons.hvc);
1495 ida_free(&vtermno_ida, port->cons.vtermno);
1498 remove_port_data(port);
1501 * We should just assume the device itself has gone off --
1502 * else a close on an open port later will try to send out a
1505 port->portdev = NULL;
1507 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1508 device_destroy(&port_class, port->dev->devt);
1509 cdev_del(port->cdev);
1511 debugfs_remove(port->debugfs_file);
1515 * Locks around here are not necessary - a port can't be
1516 * opened after we removed the port struct from ports_list
1519 kref_put(&port->kref, remove_port);
1522 /* Any private messages that the Host and Guest want to share */
1523 static void handle_control_message(struct virtio_device *vdev,
1524 struct ports_device *portdev,
1525 struct port_buffer *buf)
1527 struct virtio_console_control *cpkt;
1532 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1534 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1536 cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
1537 /* No valid header at start of buffer. Drop it. */
1538 dev_dbg(&portdev->vdev->dev,
1539 "Invalid index %u in control packet\n", cpkt->id);
1543 switch (virtio16_to_cpu(vdev, cpkt->event)) {
1544 case VIRTIO_CONSOLE_PORT_ADD:
1546 dev_dbg(&portdev->vdev->dev,
1547 "Port %u already added\n", port->id);
1548 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1551 if (virtio32_to_cpu(vdev, cpkt->id) >=
1552 portdev->max_nr_ports) {
1553 dev_warn(&portdev->vdev->dev,
1554 "Request for adding port with "
1555 "out-of-bound id %u, max. supported id: %u\n",
1556 cpkt->id, portdev->max_nr_ports - 1);
1559 add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
1561 case VIRTIO_CONSOLE_PORT_REMOVE:
1564 case VIRTIO_CONSOLE_CONSOLE_PORT:
1567 if (is_console_port(port))
1570 init_port_console(port);
1571 complete(&early_console_added);
1573 * Could remove the port here in case init fails - but
1574 * have to notify the host first.
1577 case VIRTIO_CONSOLE_RESIZE: {
1583 if (!is_console_port(port))
1586 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1588 set_console_size(port, virtio16_to_cpu(vdev, size.rows),
1589 virtio16_to_cpu(vdev, size.cols));
1591 port->cons.hvc->irq_requested = 1;
1592 resize_console(port);
1595 case VIRTIO_CONSOLE_PORT_OPEN:
1596 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
1597 wake_up_interruptible(&port->waitqueue);
1599 * If the host port got closed and the host had any
1600 * unconsumed buffers, we'll be able to reclaim them
1603 spin_lock_irq(&port->outvq_lock);
1604 reclaim_consumed_buffers(port);
1605 spin_unlock_irq(&port->outvq_lock);
1608 * If the guest is connected, it'll be interested in
1609 * knowing the host connection state changed.
1611 spin_lock_irq(&port->inbuf_lock);
1612 send_sigio_to_port(port);
1613 spin_unlock_irq(&port->inbuf_lock);
1615 case VIRTIO_CONSOLE_PORT_NAME:
1617 * If we woke up after hibernation, we can get this
1618 * again. Skip it in that case.
1624 * Skip the size of the header and the cpkt to get the size
1625 * of the name that was sent
1627 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1629 port->name = kmalloc(name_size, GFP_KERNEL);
1632 "Not enough space to store port name\n");
1635 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1639 * Since we only have one sysfs attribute, 'name',
1640 * create it only if we have a name for the port.
1642 err = sysfs_create_group(&port->dev->kobj,
1643 &port_attribute_group);
1646 "Error %d creating sysfs device attributes\n",
1650 * Generate a udev event so that appropriate
1651 * symlinks can be created based on udev
1654 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1660 static void control_work_handler(struct work_struct *work)
1662 struct ports_device *portdev;
1663 struct virtqueue *vq;
1664 struct port_buffer *buf;
1667 portdev = container_of(work, struct ports_device, control_work);
1668 vq = portdev->c_ivq;
1670 spin_lock(&portdev->c_ivq_lock);
1671 while ((buf = virtqueue_get_buf(vq, &len))) {
1672 spin_unlock(&portdev->c_ivq_lock);
1674 buf->len = min_t(size_t, len, buf->size);
1677 handle_control_message(vq->vdev, portdev, buf);
1679 spin_lock(&portdev->c_ivq_lock);
1680 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1681 dev_warn(&portdev->vdev->dev,
1682 "Error adding buffer to queue\n");
1683 free_buf(buf, false);
1686 spin_unlock(&portdev->c_ivq_lock);
1689 static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1691 struct port_buffer *buf;
1694 while ((buf = virtqueue_get_buf(vq, &len)))
1695 free_buf(buf, can_sleep);
1698 static void out_intr(struct virtqueue *vq)
1702 port = find_port_by_vq(vq->vdev->priv, vq);
1704 flush_bufs(vq, false);
1708 wake_up_interruptible(&port->waitqueue);
1711 static void in_intr(struct virtqueue *vq)
1714 unsigned long flags;
1716 port = find_port_by_vq(vq->vdev->priv, vq);
1718 flush_bufs(vq, false);
1722 spin_lock_irqsave(&port->inbuf_lock, flags);
1723 port->inbuf = get_inbuf(port);
1726 * Normally the port should not accept data when the port is
1727 * closed. For generic serial ports, the host won't (shouldn't)
1728 * send data till the guest is connected. But this condition
1729 * can be reached when a console port is not yet connected (no
1730 * tty is spawned) and the other side sends out data over the
1731 * vring, or when a remote devices start sending data before
1732 * the ports are opened.
1734 * A generic serial port will discard data if not connected,
1735 * while console ports and rproc-serial ports accepts data at
1736 * any time. rproc-serial is initiated with guest_connected to
1737 * false because port_fops_open expects this. Console ports are
1738 * hooked up with an HVC console and is initialized with
1739 * guest_connected to true.
1742 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1743 discard_port_data(port);
1745 /* Send a SIGIO indicating new data in case the process asked for it */
1746 send_sigio_to_port(port);
1748 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1750 wake_up_interruptible(&port->waitqueue);
1752 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1756 static void control_intr(struct virtqueue *vq)
1758 struct ports_device *portdev;
1760 portdev = vq->vdev->priv;
1761 schedule_work(&portdev->control_work);
1764 static void config_intr(struct virtio_device *vdev)
1766 struct ports_device *portdev;
1768 portdev = vdev->priv;
1770 if (!use_multiport(portdev))
1771 schedule_work(&portdev->config_work);
1774 static void config_work_handler(struct work_struct *work)
1776 struct ports_device *portdev;
1778 portdev = container_of(work, struct ports_device, config_work);
1779 if (!use_multiport(portdev)) {
1780 struct virtio_device *vdev;
1784 vdev = portdev->vdev;
1785 virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1786 virtio_cread(vdev, struct virtio_console_config, rows, &rows);
1788 port = find_port_by_id(portdev, 0);
1789 set_console_size(port, rows, cols);
1792 * We'll use this way of resizing only for legacy
1793 * support. For newer userspace
1794 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1795 * to indicate console size changes so that it can be
1798 resize_console(port);
1802 static int init_vqs(struct ports_device *portdev)
1804 struct virtqueue_info *vqs_info;
1805 struct virtqueue **vqs;
1806 u32 i, j, nr_ports, nr_queues;
1809 nr_ports = portdev->max_nr_ports;
1810 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1812 vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
1813 vqs_info = kcalloc(nr_queues, sizeof(*vqs_info), GFP_KERNEL);
1814 portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1816 portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1818 if (!vqs || !vqs_info || !portdev->in_vqs || !portdev->out_vqs) {
1824 * For backward compat (newer host but older guest), the host
1825 * spawns a console port first and also inits the vqs for port
1829 vqs_info[j].callback = in_intr;
1830 vqs_info[j + 1].callback = out_intr;
1831 vqs_info[j].name = "input";
1832 vqs_info[j + 1].name = "output";
1835 if (use_multiport(portdev)) {
1836 vqs_info[j].callback = control_intr;
1837 vqs_info[j].name = "control-i";
1838 vqs_info[j + 1].name = "control-o";
1840 for (i = 1; i < nr_ports; i++) {
1842 vqs_info[j].callback = in_intr;
1843 vqs_info[j + 1].callback = out_intr;
1844 vqs_info[j].name = "input";
1845 vqs_info[j + 1].name = "output";
1848 /* Find the queues. */
1849 err = virtio_find_vqs(portdev->vdev, nr_queues, vqs, vqs_info, NULL);
1854 portdev->in_vqs[0] = vqs[0];
1855 portdev->out_vqs[0] = vqs[1];
1857 if (use_multiport(portdev)) {
1858 portdev->c_ivq = vqs[j];
1859 portdev->c_ovq = vqs[j + 1];
1861 for (i = 1; i < nr_ports; i++) {
1863 portdev->in_vqs[i] = vqs[j];
1864 portdev->out_vqs[i] = vqs[j + 1];
1873 kfree(portdev->out_vqs);
1874 kfree(portdev->in_vqs);
1881 static const struct file_operations portdev_fops = {
1882 .owner = THIS_MODULE,
1885 static void remove_vqs(struct ports_device *portdev)
1887 struct virtqueue *vq;
1889 virtio_device_for_each_vq(portdev->vdev, vq) {
1890 struct port_buffer *buf;
1892 flush_bufs(vq, true);
1893 while ((buf = virtqueue_detach_unused_buf(vq)))
1894 free_buf(buf, true);
1897 portdev->vdev->config->del_vqs(portdev->vdev);
1898 kfree(portdev->in_vqs);
1899 kfree(portdev->out_vqs);
1902 static void virtcons_remove(struct virtio_device *vdev)
1904 struct ports_device *portdev;
1905 struct port *port, *port2;
1907 portdev = vdev->priv;
1909 spin_lock_irq(&pdrvdata_lock);
1910 list_del(&portdev->list);
1911 spin_unlock_irq(&pdrvdata_lock);
1913 /* Device is going away, exit any polling for buffers */
1914 virtio_break_device(vdev);
1915 if (use_multiport(portdev))
1916 flush_work(&portdev->control_work);
1918 flush_work(&portdev->config_work);
1920 /* Disable interrupts for vqs */
1921 virtio_reset_device(vdev);
1922 /* Finish up work that's lined up */
1923 if (use_multiport(portdev))
1924 cancel_work_sync(&portdev->control_work);
1926 cancel_work_sync(&portdev->config_work);
1928 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1931 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1934 * When yanking out a device, we immediately lose the
1935 * (device-side) queues. So there's no point in keeping the
1936 * guest side around till we drop our final reference. This
1937 * also means that any ports which are in an open state will
1938 * have to just stop using the port, as the vqs are going
1941 remove_vqs(portdev);
1946 * Once we're further in boot, we get probed like any other virtio
1949 * If the host also supports multiple console ports, we check the
1950 * config space to see how many ports the host has spawned. We
1951 * initialize each port found.
1953 static int virtcons_probe(struct virtio_device *vdev)
1955 struct ports_device *portdev;
1959 /* We only need a config space if features are offered */
1960 if (!vdev->config->get &&
1961 (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
1962 || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
1963 dev_err(&vdev->dev, "%s failure: config access disabled\n",
1968 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1974 /* Attach this portdev to this virtio_device, and vice-versa. */
1975 portdev->vdev = vdev;
1976 vdev->priv = portdev;
1978 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1980 if (portdev->chr_major < 0) {
1982 "Error %d registering chrdev for device %u\n",
1983 portdev->chr_major, vdev->index);
1984 err = portdev->chr_major;
1989 portdev->max_nr_ports = 1;
1991 /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
1992 if (!is_rproc_serial(vdev) &&
1993 virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1994 struct virtio_console_config, max_nr_ports,
1995 &portdev->max_nr_ports) == 0) {
1996 if (portdev->max_nr_ports == 0 ||
1997 portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
1999 "Invalidate max_nr_ports %d",
2000 portdev->max_nr_ports);
2007 spin_lock_init(&portdev->ports_lock);
2008 INIT_LIST_HEAD(&portdev->ports);
2009 INIT_LIST_HEAD(&portdev->list);
2011 INIT_WORK(&portdev->config_work, &config_work_handler);
2012 INIT_WORK(&portdev->control_work, &control_work_handler);
2015 spin_lock_init(&portdev->c_ivq_lock);
2016 spin_lock_init(&portdev->c_ovq_lock);
2019 err = init_vqs(portdev);
2021 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
2025 virtio_device_ready(portdev->vdev);
2028 err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2031 "Error allocating buffers for control queue\n");
2033 * The host might want to notify mgmt sw about device
2036 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2037 VIRTIO_CONSOLE_DEVICE_READY, 0);
2038 /* Device was functional: we need full cleanup. */
2039 virtcons_remove(vdev);
2044 * For backward compatibility: Create a console port
2045 * if we're running on older host.
2047 add_port(portdev, 0);
2050 spin_lock_irq(&pdrvdata_lock);
2051 list_add_tail(&portdev->list, &pdrvdata.portdevs);
2052 spin_unlock_irq(&pdrvdata_lock);
2054 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2055 VIRTIO_CONSOLE_DEVICE_READY, 1);
2060 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
2067 static const struct virtio_device_id id_table[] = {
2068 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
2071 MODULE_DEVICE_TABLE(virtio, id_table);
2073 static const unsigned int features[] = {
2074 VIRTIO_CONSOLE_F_SIZE,
2075 VIRTIO_CONSOLE_F_MULTIPORT,
2078 static const struct virtio_device_id rproc_serial_id_table[] = {
2079 #if IS_ENABLED(CONFIG_REMOTEPROC)
2080 { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
2084 MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
2086 static const unsigned int rproc_serial_features[] = {
2089 #ifdef CONFIG_PM_SLEEP
2090 static int virtcons_freeze(struct virtio_device *vdev)
2092 struct ports_device *portdev;
2095 portdev = vdev->priv;
2097 virtio_reset_device(vdev);
2099 if (use_multiport(portdev))
2100 virtqueue_disable_cb(portdev->c_ivq);
2101 cancel_work_sync(&portdev->control_work);
2102 cancel_work_sync(&portdev->config_work);
2104 * Once more: if control_work_handler() was running, it would
2105 * enable the cb as the last step.
2107 if (use_multiport(portdev))
2108 virtqueue_disable_cb(portdev->c_ivq);
2110 list_for_each_entry(port, &portdev->ports, list) {
2111 virtqueue_disable_cb(port->in_vq);
2112 virtqueue_disable_cb(port->out_vq);
2114 * We'll ask the host later if the new invocation has
2115 * the port opened or closed.
2117 port->host_connected = false;
2118 remove_port_data(port);
2120 remove_vqs(portdev);
2125 static int virtcons_restore(struct virtio_device *vdev)
2127 struct ports_device *portdev;
2131 portdev = vdev->priv;
2133 ret = init_vqs(portdev);
2137 virtio_device_ready(portdev->vdev);
2139 if (use_multiport(portdev))
2140 fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2142 list_for_each_entry(port, &portdev->ports, list) {
2143 port->in_vq = portdev->in_vqs[port->id];
2144 port->out_vq = portdev->out_vqs[port->id];
2146 fill_queue(port->in_vq, &port->inbuf_lock);
2148 /* Get port open/close status on the host */
2149 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2152 * If a port was open at the time of suspending, we
2153 * have to let the host know that it's still open.
2155 if (port->guest_connected)
2156 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
2162 static struct virtio_driver virtio_console = {
2163 .feature_table = features,
2164 .feature_table_size = ARRAY_SIZE(features),
2165 .driver.name = KBUILD_MODNAME,
2166 .id_table = id_table,
2167 .probe = virtcons_probe,
2168 .remove = virtcons_remove,
2169 .config_changed = config_intr,
2170 #ifdef CONFIG_PM_SLEEP
2171 .freeze = virtcons_freeze,
2172 .restore = virtcons_restore,
2176 static struct virtio_driver virtio_rproc_serial = {
2177 .feature_table = rproc_serial_features,
2178 .feature_table_size = ARRAY_SIZE(rproc_serial_features),
2179 .driver.name = "virtio_rproc_serial",
2180 .id_table = rproc_serial_id_table,
2181 .probe = virtcons_probe,
2182 .remove = virtcons_remove,
2185 static int __init virtio_console_init(void)
2189 err = class_register(&port_class);
2193 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2194 INIT_LIST_HEAD(&pdrvdata.consoles);
2195 INIT_LIST_HEAD(&pdrvdata.portdevs);
2197 err = register_virtio_driver(&virtio_console);
2199 pr_err("Error %d registering virtio driver\n", err);
2202 err = register_virtio_driver(&virtio_rproc_serial);
2204 pr_err("Error %d registering virtio rproc serial driver\n",
2210 unregister_virtio_driver(&virtio_console);
2212 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2213 class_unregister(&port_class);
2217 static void __exit virtio_console_fini(void)
2221 unregister_virtio_driver(&virtio_console);
2222 unregister_virtio_driver(&virtio_rproc_serial);
2224 class_unregister(&port_class);
2225 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2227 module_init(virtio_console_init);
2228 module_exit(virtio_console_fini);
2230 MODULE_DESCRIPTION("Virtio console driver");
2231 MODULE_LICENSE("GPL");