xref: /openbmc/linux/drivers/char/virtio_console.c (revision 56b5e65e)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
251df0accSAmit Shah /*
351df0accSAmit Shah  * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
4ce86d35dSLinus Torvalds  * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
5ce86d35dSLinus Torvalds  * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
651df0accSAmit Shah  */
751df0accSAmit Shah #include <linux/cdev.h>
851df0accSAmit Shah #include <linux/debugfs.h>
95e38483bSChristian Borntraeger #include <linux/completion.h>
1051df0accSAmit Shah #include <linux/device.h>
1151df0accSAmit Shah #include <linux/err.h>
12a08fa92dSAmit Shah #include <linux/freezer.h>
1351df0accSAmit Shah #include <linux/fs.h>
14eb5e89fcSMasami Hiramatsu #include <linux/splice.h>
15eb5e89fcSMasami Hiramatsu #include <linux/pagemap.h>
16e6278a54SCédric Le Goater #include <linux/idr.h>
1751df0accSAmit Shah #include <linux/init.h>
1851df0accSAmit Shah #include <linux/list.h>
1951df0accSAmit Shah #include <linux/poll.h>
2051df0accSAmit Shah #include <linux/sched.h>
2151df0accSAmit Shah #include <linux/slab.h>
2251df0accSAmit Shah #include <linux/spinlock.h>
2351df0accSAmit Shah #include <linux/virtio.h>
2451df0accSAmit Shah #include <linux/virtio_console.h>
2551df0accSAmit Shah #include <linux/wait.h>
2651df0accSAmit Shah #include <linux/workqueue.h>
27c22405c9SPaul Gortmaker #include <linux/module.h>
281b637046SSjur Brændeland #include <linux/dma-mapping.h>
2951df0accSAmit Shah #include "../tty/hvc/hvc_console.h"
3051df0accSAmit Shah 
311b637046SSjur Brændeland #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
3228962ec5SJason Wang #define VIRTCONS_MAX_PORTS 0x8000
331b637046SSjur Brændeland 
3451df0accSAmit Shah /*
3551df0accSAmit Shah  * This is a global struct for storing common data for all the devices
3651df0accSAmit Shah  * this driver handles.
3751df0accSAmit Shah  *
3851df0accSAmit Shah  * Mainly, it has a linked list for all the consoles in one place so
3951df0accSAmit Shah  * that callbacks from hvc for get_chars(), put_chars() work properly
4051df0accSAmit Shah  * across multiple devices and multiple ports per device.
4151df0accSAmit Shah  */
4251df0accSAmit Shah struct ports_driver_data {
4351df0accSAmit Shah 	/* Used for exporting per-port information to debugfs */
4451df0accSAmit Shah 	struct dentry *debugfs_dir;
4551df0accSAmit Shah 
4651df0accSAmit Shah 	/* List of all the devices we're handling */
4751df0accSAmit Shah 	struct list_head portdevs;
4851df0accSAmit Shah 
4951df0accSAmit Shah 	/* All the console devices handled by this driver */
5051df0accSAmit Shah 	struct list_head consoles;
5151df0accSAmit Shah };
5251df0accSAmit Shah 
5351df0accSAmit Shah static struct ports_driver_data pdrvdata;
5451df0accSAmit Shah 
55e6278a54SCédric Le Goater static const struct class port_class = {
56e6278a54SCédric Le Goater 	.name = "virtio-ports",
5751df0accSAmit Shah };
583826835aSWei Yongjun 
593826835aSWei Yongjun static DEFINE_SPINLOCK(pdrvdata_lock);
6051df0accSAmit Shah static DECLARE_COMPLETION(early_console_added);
6151df0accSAmit Shah 
6251df0accSAmit Shah /* This struct holds information that's relevant only for console ports */
6351df0accSAmit Shah struct console {
6451df0accSAmit Shah 	/* We'll place all consoles in a list in the pdrvdata struct */
6551df0accSAmit Shah 	struct list_head list;
6651df0accSAmit Shah 
6751df0accSAmit Shah 	/* The hvc device associated with this console port */
6851df0accSAmit Shah 	struct hvc_struct *hvc;
6951df0accSAmit Shah 
7051df0accSAmit Shah 	/* The size of the console */
7151df0accSAmit Shah 	struct winsize ws;
7251df0accSAmit Shah 
7351df0accSAmit Shah 	/*
7451df0accSAmit Shah 	 * This number identifies the number that we used to register
7551df0accSAmit Shah 	 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
7651df0accSAmit Shah 	 * number passed on by the hvc callbacks to us to
7751df0accSAmit Shah 	 * differentiate between the other console ports handled by
7851df0accSAmit Shah 	 * this driver
7951df0accSAmit Shah 	 */
8051df0accSAmit Shah 	u32 vtermno;
8151df0accSAmit Shah };
82e6278a54SCédric Le Goater 
83e6278a54SCédric Le Goater static DEFINE_IDA(vtermno_ida);
8451df0accSAmit Shah 
8551df0accSAmit Shah struct port_buffer {
8651df0accSAmit Shah 	char *buf;
8751df0accSAmit Shah 
8851df0accSAmit Shah 	/* size of the buffer in *buf above */
8951df0accSAmit Shah 	size_t size;
9051df0accSAmit Shah 
9151df0accSAmit Shah 	/* used length of the buffer */
9251df0accSAmit Shah 	size_t len;
9351df0accSAmit Shah 	/* offset in the buf from which to consume data */
94276a3e95SSjur Brændeland 	size_t offset;
951b637046SSjur Brændeland 
961b637046SSjur Brændeland 	/* DMA address of buffer */
971b637046SSjur Brændeland 	dma_addr_t dma;
981b637046SSjur Brændeland 
991b637046SSjur Brændeland 	/* Device we got DMA memory from */
1001b637046SSjur Brændeland 	struct device *dev;
1011b637046SSjur Brændeland 
1021b637046SSjur Brændeland 	/* List of pending dma buffers to free */
1031b637046SSjur Brændeland 	struct list_head list;
104276a3e95SSjur Brændeland 
105276a3e95SSjur Brændeland 	/* If sgpages == 0 then buf is used */
106276a3e95SSjur Brændeland 	unsigned int sgpages;
107276a3e95SSjur Brændeland 
1081031eb90SGustavo A. R. Silva 	/* sg is used if spages > 0. sg must be the last in is struct */
10951df0accSAmit Shah 	struct scatterlist sg[];
11051df0accSAmit Shah };
11151df0accSAmit Shah 
11251df0accSAmit Shah /*
11351df0accSAmit Shah  * This is a per-device struct that stores data common to all the
11451df0accSAmit Shah  * ports for that device (vdev->priv).
11551df0accSAmit Shah  */
11651df0accSAmit Shah struct ports_device {
11751df0accSAmit Shah 	/* Next portdev in the list, head is in the pdrvdata struct */
11851df0accSAmit Shah 	struct list_head list;
11951df0accSAmit Shah 
12051df0accSAmit Shah 	/*
12151df0accSAmit Shah 	 * Workqueue handlers where we process deferred work after
12251df0accSAmit Shah 	 * notification
12351df0accSAmit Shah 	 */
124eeb8a7e8SMichael S. Tsirkin 	struct work_struct control_work;
12551df0accSAmit Shah 	struct work_struct config_work;
12651df0accSAmit Shah 
12751df0accSAmit Shah 	struct list_head ports;
12851df0accSAmit Shah 
12951df0accSAmit Shah 	/* To protect the list of ports */
13051df0accSAmit Shah 	spinlock_t ports_lock;
13151df0accSAmit Shah 
132165b1b8bSAmit Shah 	/* To protect the vq operations for the control channel */
1339ba5c80bSAmit Shah 	spinlock_t c_ivq_lock;
13451df0accSAmit Shah 	spinlock_t c_ovq_lock;
1357328fa64SMichael S. Tsirkin 
1367328fa64SMichael S. Tsirkin 	/* max. number of ports this device can hold */
13751df0accSAmit Shah 	u32 max_nr_ports;
13851df0accSAmit Shah 
13951df0accSAmit Shah 	/* The virtio device we're associated with */
14051df0accSAmit Shah 	struct virtio_device *vdev;
14151df0accSAmit Shah 
14251df0accSAmit Shah 	/*
14351df0accSAmit Shah 	 * A couple of virtqueues for the control channel: one for
14451df0accSAmit Shah 	 * guest->host transfers, one for host->guest transfers
14551df0accSAmit Shah 	 */
14651df0accSAmit Shah 	struct virtqueue *c_ivq, *c_ovq;
1475e59d9a1SAndy Lutomirski 
1485e59d9a1SAndy Lutomirski 	/*
1495e59d9a1SAndy Lutomirski 	 * A control packet buffer for guest->host requests, protected
1505e59d9a1SAndy Lutomirski 	 * by c_ovq_lock.
1515e59d9a1SAndy Lutomirski 	 */
1525e59d9a1SAndy Lutomirski 	struct virtio_console_control cpkt;
15351df0accSAmit Shah 
15451df0accSAmit Shah 	/* Array of per-port IO virtqueues */
15551df0accSAmit Shah 	struct virtqueue **in_vqs, **out_vqs;
15651df0accSAmit Shah 
15751df0accSAmit Shah 	/* Major number for this device.  Ports will be created as minors. */
15851df0accSAmit Shah 	int chr_major;
15951df0accSAmit Shah };
16017e5b4f2SAmit Shah 
16117e5b4f2SAmit Shah struct port_stats {
16217e5b4f2SAmit Shah 	unsigned long bytes_sent, bytes_received, bytes_discarded;
16317e5b4f2SAmit Shah };
16451df0accSAmit Shah 
16551df0accSAmit Shah /* This struct holds the per-port data */
16651df0accSAmit Shah struct port {
16751df0accSAmit Shah 	/* Next port in the list, head is in the ports_device */
16851df0accSAmit Shah 	struct list_head list;
16951df0accSAmit Shah 
17051df0accSAmit Shah 	/* Pointer to the parent virtio_console device */
17151df0accSAmit Shah 	struct ports_device *portdev;
17251df0accSAmit Shah 
17351df0accSAmit Shah 	/* The current buffer from which data has to be fed to readers */
17451df0accSAmit Shah 	struct port_buffer *inbuf;
17551df0accSAmit Shah 
17651df0accSAmit Shah 	/*
17751df0accSAmit Shah 	 * To protect the operations on the in_vq associated with this
17851df0accSAmit Shah 	 * port.  Has to be a spinlock because it can be called from
17951df0accSAmit Shah 	 * interrupt context (get_char()).
18051df0accSAmit Shah 	 */
18151df0accSAmit Shah 	spinlock_t inbuf_lock;
18251df0accSAmit Shah 
18351df0accSAmit Shah 	/* Protect the operations on the out_vq. */
18451df0accSAmit Shah 	spinlock_t outvq_lock;
18551df0accSAmit Shah 
18651df0accSAmit Shah 	/* The IO vqs for this port */
18751df0accSAmit Shah 	struct virtqueue *in_vq, *out_vq;
18851df0accSAmit Shah 
18951df0accSAmit Shah 	/* File in the debugfs directory that exposes this port's information */
19051df0accSAmit Shah 	struct dentry *debugfs_file;
19151df0accSAmit Shah 
19217e5b4f2SAmit Shah 	/*
19317e5b4f2SAmit Shah 	 * Keep count of the bytes sent, received and discarded for
19417e5b4f2SAmit Shah 	 * this port for accounting and debugging purposes.  These
19517e5b4f2SAmit Shah 	 * counts are not reset across port open / close events.
19617e5b4f2SAmit Shah 	 */
19717e5b4f2SAmit Shah 	struct port_stats stats;
19817e5b4f2SAmit Shah 
19951df0accSAmit Shah 	/*
20051df0accSAmit Shah 	 * The entries in this struct will be valid if this port is
20151df0accSAmit Shah 	 * hooked up to an hvc console
20251df0accSAmit Shah 	 */
20351df0accSAmit Shah 	struct console cons;
20451df0accSAmit Shah 
20551df0accSAmit Shah 	/* Each port associates with a separate char device */
20651df0accSAmit Shah 	struct cdev *cdev;
20751df0accSAmit Shah 	struct device *dev;
20851df0accSAmit Shah 
20951df0accSAmit Shah 	/* Reference-counting to handle port hot-unplugs and file operations */
21051df0accSAmit Shah 	struct kref kref;
21151df0accSAmit Shah 
21251df0accSAmit Shah 	/* A waitqueue for poll() or blocking read operations */
21351df0accSAmit Shah 	wait_queue_head_t waitqueue;
21451df0accSAmit Shah 
21551df0accSAmit Shah 	/* The 'name' of the port that we expose via sysfs properties */
21651df0accSAmit Shah 	char *name;
21751df0accSAmit Shah 
21851df0accSAmit Shah 	/* We can notify apps of host connect / disconnect events via SIGIO */
21951df0accSAmit Shah 	struct fasync_struct *async_queue;
22051df0accSAmit Shah 
22151df0accSAmit Shah 	/* The 'id' to identify the port with the Host */
22251df0accSAmit Shah 	u32 id;
22351df0accSAmit Shah 
22451df0accSAmit Shah 	bool outvq_full;
22551df0accSAmit Shah 
22651df0accSAmit Shah 	/* Is the host device open */
22751df0accSAmit Shah 	bool host_connected;
22851df0accSAmit Shah 
22951df0accSAmit Shah 	/* We should allow only one process to open a port */
23051df0accSAmit Shah 	bool guest_connected;
23151df0accSAmit Shah };
23251df0accSAmit Shah 
23351df0accSAmit Shah /* This is the very early arch-specified put chars function. */
23451df0accSAmit Shah static int (*early_put_chars)(u32, const char *, int);
23551df0accSAmit Shah 
find_port_by_vtermno(u32 vtermno)23651df0accSAmit Shah static struct port *find_port_by_vtermno(u32 vtermno)
23751df0accSAmit Shah {
23851df0accSAmit Shah 	struct port *port;
23951df0accSAmit Shah 	struct console *cons;
24051df0accSAmit Shah 	unsigned long flags;
24151df0accSAmit Shah 
24251df0accSAmit Shah 	spin_lock_irqsave(&pdrvdata_lock, flags);
24351df0accSAmit Shah 	list_for_each_entry(cons, &pdrvdata.consoles, list) {
24451df0accSAmit Shah 		if (cons->vtermno == vtermno) {
24551df0accSAmit Shah 			port = container_of(cons, struct port, cons);
24651df0accSAmit Shah 			goto out;
24751df0accSAmit Shah 		}
24851df0accSAmit Shah 	}
24951df0accSAmit Shah 	port = NULL;
25051df0accSAmit Shah out:
25151df0accSAmit Shah 	spin_unlock_irqrestore(&pdrvdata_lock, flags);
25251df0accSAmit Shah 	return port;
25351df0accSAmit Shah }
25451df0accSAmit Shah 
find_port_by_devt_in_portdev(struct ports_device * portdev,dev_t dev)25551df0accSAmit Shah static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
25651df0accSAmit Shah 						 dev_t dev)
25751df0accSAmit Shah {
25851df0accSAmit Shah 	struct port *port;
25951df0accSAmit Shah 	unsigned long flags;
26051df0accSAmit Shah 
261057b82beSAmit Shah 	spin_lock_irqsave(&portdev->ports_lock, flags);
262057b82beSAmit Shah 	list_for_each_entry(port, &portdev->ports, list) {
263057b82beSAmit Shah 		if (port->cdev->dev == dev) {
26451df0accSAmit Shah 			kref_get(&port->kref);
265057b82beSAmit Shah 			goto out;
266057b82beSAmit Shah 		}
26751df0accSAmit Shah 	}
26851df0accSAmit Shah 	port = NULL;
26951df0accSAmit Shah out:
27051df0accSAmit Shah 	spin_unlock_irqrestore(&portdev->ports_lock, flags);
27151df0accSAmit Shah 
27251df0accSAmit Shah 	return port;
27351df0accSAmit Shah }
27451df0accSAmit Shah 
find_port_by_devt(dev_t dev)27551df0accSAmit Shah static struct port *find_port_by_devt(dev_t dev)
27651df0accSAmit Shah {
27751df0accSAmit Shah 	struct ports_device *portdev;
27851df0accSAmit Shah 	struct port *port;
27951df0accSAmit Shah 	unsigned long flags;
28051df0accSAmit Shah 
28151df0accSAmit Shah 	spin_lock_irqsave(&pdrvdata_lock, flags);
28251df0accSAmit Shah 	list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
28351df0accSAmit Shah 		port = find_port_by_devt_in_portdev(portdev, dev);
28451df0accSAmit Shah 		if (port)
28551df0accSAmit Shah 			goto out;
28651df0accSAmit Shah 	}
28751df0accSAmit Shah 	port = NULL;
28851df0accSAmit Shah out:
28951df0accSAmit Shah 	spin_unlock_irqrestore(&pdrvdata_lock, flags);
29051df0accSAmit Shah 	return port;
29151df0accSAmit Shah }
29251df0accSAmit Shah 
find_port_by_id(struct ports_device * portdev,u32 id)29351df0accSAmit Shah static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
29451df0accSAmit Shah {
29551df0accSAmit Shah 	struct port *port;
29651df0accSAmit Shah 	unsigned long flags;
29751df0accSAmit Shah 
29851df0accSAmit Shah 	spin_lock_irqsave(&portdev->ports_lock, flags);
29951df0accSAmit Shah 	list_for_each_entry(port, &portdev->ports, list)
30051df0accSAmit Shah 		if (port->id == id)
30151df0accSAmit Shah 			goto out;
30251df0accSAmit Shah 	port = NULL;
30351df0accSAmit Shah out:
30451df0accSAmit Shah 	spin_unlock_irqrestore(&portdev->ports_lock, flags);
30551df0accSAmit Shah 
30651df0accSAmit Shah 	return port;
30751df0accSAmit Shah }
30851df0accSAmit Shah 
find_port_by_vq(struct ports_device * portdev,struct virtqueue * vq)30951df0accSAmit Shah static struct port *find_port_by_vq(struct ports_device *portdev,
31051df0accSAmit Shah 				    struct virtqueue *vq)
31151df0accSAmit Shah {
31251df0accSAmit Shah 	struct port *port;
31351df0accSAmit Shah 	unsigned long flags;
31451df0accSAmit Shah 
31551df0accSAmit Shah 	spin_lock_irqsave(&portdev->ports_lock, flags);
31651df0accSAmit Shah 	list_for_each_entry(port, &portdev->ports, list)
31751df0accSAmit Shah 		if (port->in_vq == vq || port->out_vq == vq)
31851df0accSAmit Shah 			goto out;
31951df0accSAmit Shah 	port = NULL;
32051df0accSAmit Shah out:
32151df0accSAmit Shah 	spin_unlock_irqrestore(&portdev->ports_lock, flags);
32251df0accSAmit Shah 	return port;
32351df0accSAmit Shah }
32451df0accSAmit Shah 
is_console_port(struct port * port)32551df0accSAmit Shah static bool is_console_port(struct port *port)
32651df0accSAmit Shah {
32751df0accSAmit Shah 	if (port->cons.hvc)
32851df0accSAmit Shah 		return true;
32951df0accSAmit Shah 	return false;
33051df0accSAmit Shah }
3311b637046SSjur Brændeland 
is_rproc_serial(const struct virtio_device * vdev)3321b637046SSjur Brændeland static bool is_rproc_serial(const struct virtio_device *vdev)
3331b637046SSjur Brændeland {
3341b637046SSjur Brændeland 	return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
3351b637046SSjur Brændeland }
33651df0accSAmit Shah 
use_multiport(struct ports_device * portdev)33751df0accSAmit Shah static inline bool use_multiport(struct ports_device *portdev)
33851df0accSAmit Shah {
33951df0accSAmit Shah 	/*
34051df0accSAmit Shah 	 * This condition can be true when put_chars is called from
34151df0accSAmit Shah 	 * early_init
34251df0accSAmit Shah 	 */
343f580d730SJoe Perches 	if (!portdev->vdev)
344e16e12beSMichael S. Tsirkin 		return false;
34551df0accSAmit Shah 	return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
34651df0accSAmit Shah }
3471b637046SSjur Brændeland 
3481b637046SSjur Brændeland static DEFINE_SPINLOCK(dma_bufs_lock);
3491b637046SSjur Brændeland static LIST_HEAD(pending_free_dma_bufs);
3501b637046SSjur Brændeland 
free_buf(struct port_buffer * buf,bool can_sleep)35151df0accSAmit Shah static void free_buf(struct port_buffer *buf, bool can_sleep)
352276a3e95SSjur Brændeland {
353276a3e95SSjur Brændeland 	unsigned int i;
354276a3e95SSjur Brændeland 
355276a3e95SSjur Brændeland 	for (i = 0; i < buf->sgpages; i++) {
356276a3e95SSjur Brændeland 		struct page *page = sg_page(&buf->sg[i]);
357276a3e95SSjur Brændeland 		if (!page)
358276a3e95SSjur Brændeland 			break;
359276a3e95SSjur Brændeland 		put_page(page);
360276a3e95SSjur Brændeland 	}
3611b637046SSjur Brændeland 
36251df0accSAmit Shah 	if (!buf->dev) {
3631b637046SSjur Brændeland 		kfree(buf->buf);
3641b637046SSjur Brændeland 	} else if (is_rproc_enabled) {
3651b637046SSjur Brændeland 		unsigned long flags;
3661b637046SSjur Brændeland 
3671b637046SSjur Brændeland 		/* dma_free_coherent requires interrupts to be enabled. */
3681b637046SSjur Brændeland 		if (!can_sleep) {
3691b637046SSjur Brændeland 			/* queue up dma-buffers to be freed later */
3701b637046SSjur Brændeland 			spin_lock_irqsave(&dma_bufs_lock, flags);
3711b637046SSjur Brændeland 			list_add_tail(&buf->list, &pending_free_dma_bufs);
3721b637046SSjur Brændeland 			spin_unlock_irqrestore(&dma_bufs_lock, flags);
3731b637046SSjur Brændeland 			return;
3741b637046SSjur Brændeland 		}
3751b637046SSjur Brændeland 		dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
3761b637046SSjur Brændeland 
3771b637046SSjur Brændeland 		/* Release device refcnt and allow it to be freed */
3781b637046SSjur Brændeland 		put_device(buf->dev);
3791b637046SSjur Brændeland 	}
38051df0accSAmit Shah 
38151df0accSAmit Shah 	kfree(buf);
38251df0accSAmit Shah }
3831b637046SSjur Brændeland 
reclaim_dma_bufs(void)3841b637046SSjur Brændeland static void reclaim_dma_bufs(void)
3851b637046SSjur Brændeland {
3861b637046SSjur Brændeland 	unsigned long flags;
3871b637046SSjur Brændeland 	struct port_buffer *buf, *tmp;
3881b637046SSjur Brændeland 	LIST_HEAD(tmp_list);
3891b637046SSjur Brændeland 
3901b637046SSjur Brændeland 	if (list_empty(&pending_free_dma_bufs))
3911b637046SSjur Brændeland 		return;
3921b637046SSjur Brændeland 
3931b637046SSjur Brændeland 	/* Create a copy of the pending_free_dma_bufs while holding the lock */
3941b637046SSjur Brændeland 	spin_lock_irqsave(&dma_bufs_lock, flags);
3951b637046SSjur Brændeland 	list_cut_position(&tmp_list, &pending_free_dma_bufs,
3961b637046SSjur Brændeland 			  pending_free_dma_bufs.prev);
3971b637046SSjur Brændeland 	spin_unlock_irqrestore(&dma_bufs_lock, flags);
3981b637046SSjur Brændeland 
3991b637046SSjur Brændeland 	/* Release the dma buffers, without irqs enabled */
4001b637046SSjur Brændeland 	list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
4011b637046SSjur Brændeland 		list_del(&buf->list);
4021b637046SSjur Brændeland 		free_buf(buf, true);
4031b637046SSjur Brændeland 	}
4041b637046SSjur Brændeland }
4052855b335SMichael S. Tsirkin 
alloc_buf(struct virtio_device * vdev,size_t buf_size,int pages)406276a3e95SSjur Brændeland static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
40751df0accSAmit Shah 				     int pages)
40851df0accSAmit Shah {
40951df0accSAmit Shah 	struct port_buffer *buf;
4101b637046SSjur Brændeland 
4111b637046SSjur Brændeland 	reclaim_dma_bufs();
412276a3e95SSjur Brændeland 
413276a3e95SSjur Brændeland 	/*
414276a3e95SSjur Brændeland 	 * Allocate buffer and the sg list. The sg list array is allocated
415276a3e95SSjur Brændeland 	 * directly after the port_buffer struct.
4165b572e25SMatthew Wilcox 	 */
41751df0accSAmit Shah 	buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
41851df0accSAmit Shah 	if (!buf)
419276a3e95SSjur Brændeland 		goto fail;
420276a3e95SSjur Brændeland 
421276a3e95SSjur Brændeland 	buf->sgpages = pages;
4221b637046SSjur Brændeland 	if (pages > 0) {
423276a3e95SSjur Brændeland 		buf->dev = NULL;
424276a3e95SSjur Brændeland 		buf->buf = NULL;
425276a3e95SSjur Brændeland 		return buf;
426276a3e95SSjur Brændeland 	}
4272855b335SMichael S. Tsirkin 
4281b637046SSjur Brændeland 	if (is_rproc_serial(vdev)) {
4291b637046SSjur Brændeland 		/*
4301b637046SSjur Brændeland 		 * Allocate DMA memory from ancestor. When a virtio
4319d516aa8SAlexander Lobakin 		 * device is created by remoteproc, the DMA memory is
4329d516aa8SAlexander Lobakin 		 * associated with the parent device:
4331b637046SSjur Brændeland 		 * virtioY => remoteprocX#vdevYbuffer.
4349d516aa8SAlexander Lobakin 		 */
4359d516aa8SAlexander Lobakin 		buf->dev = vdev->dev.parent;
4361b637046SSjur Brændeland 		if (!buf->dev)
4371b637046SSjur Brændeland 			goto free_buf;
4381b637046SSjur Brændeland 
4391b637046SSjur Brændeland 		/* Increase device refcnt to avoid freeing it */
4401b637046SSjur Brændeland 		get_device(buf->dev);
4411b637046SSjur Brændeland 		buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
4421b637046SSjur Brændeland 					      GFP_KERNEL);
4431b637046SSjur Brændeland 	} else {
4440127f685SSjur Brændeland 		buf->dev = NULL;
4451b637046SSjur Brændeland 		buf->buf = kmalloc(buf_size, GFP_KERNEL);
4461b637046SSjur Brændeland 	}
44751df0accSAmit Shah 
44851df0accSAmit Shah 	if (!buf->buf)
44951df0accSAmit Shah 		goto free_buf;
45051df0accSAmit Shah 	buf->len = 0;
45151df0accSAmit Shah 	buf->offset = 0;
45251df0accSAmit Shah 	buf->size = buf_size;
45351df0accSAmit Shah 	return buf;
45451df0accSAmit Shah 
45551df0accSAmit Shah free_buf:
45651df0accSAmit Shah 	kfree(buf);
45751df0accSAmit Shah fail:
45851df0accSAmit Shah 	return NULL;
45951df0accSAmit Shah }
46051df0accSAmit Shah 
461defde669SAmit Shah /* Callers should take appropriate locks */
get_inbuf(struct port * port)46251df0accSAmit Shah static struct port_buffer *get_inbuf(struct port *port)
46351df0accSAmit Shah {
46451df0accSAmit Shah 	struct port_buffer *buf;
46551df0accSAmit Shah 	unsigned int len;
466d25a9ddaSAmit Shah 
467d25a9ddaSAmit Shah 	if (port->inbuf)
468d25a9ddaSAmit Shah 		return port->inbuf;
469d25a9ddaSAmit Shah 
47051df0accSAmit Shah 	buf = virtqueue_get_buf(port->in_vq, &len);
471d00d8da5SXie Yongji 	if (buf) {
47251df0accSAmit Shah 		buf->len = min_t(size_t, len, buf->size);
47317e5b4f2SAmit Shah 		buf->offset = 0;
47451df0accSAmit Shah 		port->stats.bytes_received += len;
47551df0accSAmit Shah 	}
47651df0accSAmit Shah 	return buf;
47751df0accSAmit Shah }
47851df0accSAmit Shah 
47951df0accSAmit Shah /*
48051df0accSAmit Shah  * Create a scatter-gather list representing our input buffer and put
48151df0accSAmit Shah  * it in the queue.
48251df0accSAmit Shah  *
48351df0accSAmit Shah  * Callers should take appropriate locks.
48451df0accSAmit Shah  */
add_inbuf(struct virtqueue * vq,struct port_buffer * buf)48551df0accSAmit Shah static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
48651df0accSAmit Shah {
48751df0accSAmit Shah 	struct scatterlist sg[1];
48851df0accSAmit Shah 	int ret;
48951df0accSAmit Shah 
49051df0accSAmit Shah 	sg_init_one(sg, buf->buf, buf->size);
4916797999dSRusty Russell 
49251df0accSAmit Shah 	ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
49349e86f16SAmit Shah 	virtqueue_kick(vq);
49449e86f16SAmit Shah 	if (!ret)
49551df0accSAmit Shah 		ret = vq->num_free;
49651df0accSAmit Shah 	return ret;
49751df0accSAmit Shah }
49851df0accSAmit Shah 
49951df0accSAmit Shah /* Discard any unread data this port has. Callers lockers. */
discard_port_data(struct port * port)50051df0accSAmit Shah static void discard_port_data(struct port *port)
50151df0accSAmit Shah {
5022d24cdaaSAmit Shah 	struct port_buffer *buf;
50351df0accSAmit Shah 	unsigned int err;
504d7a62cd0SAmit Shah 
505d7a62cd0SAmit Shah 	if (!port->portdev) {
506d7a62cd0SAmit Shah 		/* Device has been unplugged.  vqs are already gone. */
507d7a62cd0SAmit Shah 		return;
5082d24cdaaSAmit Shah 	}
50951df0accSAmit Shah 	buf = get_inbuf(port);
510ce072a0cSAmit Shah 
51151df0accSAmit Shah 	err = 0;
51217e5b4f2SAmit Shah 	while (buf) {
5132d24cdaaSAmit Shah 		port->stats.bytes_discarded += buf->len - buf->offset;
514ce072a0cSAmit Shah 		if (add_inbuf(port->in_vq, buf) < 0) {
5151b637046SSjur Brændeland 			err++;
51651df0accSAmit Shah 			free_buf(buf, false);
51751df0accSAmit Shah 		}
5182d24cdaaSAmit Shah 		port->inbuf = NULL;
5192d24cdaaSAmit Shah 		buf = get_inbuf(port);
520ce072a0cSAmit Shah 	}
52151df0accSAmit Shah 	if (err)
522ce072a0cSAmit Shah 		dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
52351df0accSAmit Shah 			 err);
52451df0accSAmit Shah }
52551df0accSAmit Shah 
port_has_data(struct port * port)52651df0accSAmit Shah static bool port_has_data(struct port *port)
52751df0accSAmit Shah {
52851df0accSAmit Shah 	unsigned long flags;
52951df0accSAmit Shah 	bool ret;
53051df0accSAmit Shah 
531d25a9ddaSAmit Shah 	ret = false;
532d25a9ddaSAmit Shah 	spin_lock_irqsave(&port->inbuf_lock, flags);
533d25a9ddaSAmit Shah 	port->inbuf = get_inbuf(port);
534d25a9ddaSAmit Shah 	if (port->inbuf)
535d25a9ddaSAmit Shah 		ret = true;
53651df0accSAmit Shah 
53751df0accSAmit Shah 	spin_unlock_irqrestore(&port->inbuf_lock, flags);
53851df0accSAmit Shah 	return ret;
53951df0accSAmit Shah }
54051df0accSAmit Shah 
__send_control_msg(struct ports_device * portdev,u32 port_id,unsigned int event,unsigned int value)54151df0accSAmit Shah static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
54251df0accSAmit Shah 				  unsigned int event, unsigned int value)
54351df0accSAmit Shah {
54451df0accSAmit Shah 	struct scatterlist sg[1];
54551df0accSAmit Shah 	struct virtqueue *vq;
54651df0accSAmit Shah 	unsigned int len;
54751df0accSAmit Shah 
54851df0accSAmit Shah 	if (!use_multiport(portdev))
54951df0accSAmit Shah 		return 0;
55051df0accSAmit Shah 
55151df0accSAmit Shah 	vq = portdev->c_ovq;
5529ba5c80bSAmit Shah 
5535e59d9a1SAndy Lutomirski 	spin_lock(&portdev->c_ovq_lock);
5545e59d9a1SAndy Lutomirski 
5555e59d9a1SAndy Lutomirski 	portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
5565e59d9a1SAndy Lutomirski 	portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
5575e59d9a1SAndy Lutomirski 	portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
5585e59d9a1SAndy Lutomirski 
5595e59d9a1SAndy Lutomirski 	sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
5605e59d9a1SAndy Lutomirski 
56151df0accSAmit Shah 	if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
56240e4dc53SHeinz Graalfs 		virtqueue_kick(vq);
56340e4dc53SHeinz Graalfs 		while (!virtqueue_get_buf(vq, &len)
56451df0accSAmit Shah 			&& !virtqueue_is_broken(vq))
56551df0accSAmit Shah 			cpu_relax();
5665e59d9a1SAndy Lutomirski 	}
5679ba5c80bSAmit Shah 
56851df0accSAmit Shah 	spin_unlock(&portdev->c_ovq_lock);
56951df0accSAmit Shah 	return 0;
57051df0accSAmit Shah }
57151df0accSAmit Shah 
send_control_msg(struct port * port,unsigned int event,unsigned int value)57251df0accSAmit Shah static ssize_t send_control_msg(struct port *port, unsigned int event,
57351df0accSAmit Shah 				unsigned int value)
57451df0accSAmit Shah {
57551df0accSAmit Shah 	/* Did the port get unplugged before userspace closed it? */
57651df0accSAmit Shah 	if (port->portdev)
57751df0accSAmit Shah 		return __send_control_msg(port->portdev, port->id, event, value);
57851df0accSAmit Shah 	return 0;
57951df0accSAmit Shah }
580eb5e89fcSMasami Hiramatsu 
58151df0accSAmit Shah 
58251df0accSAmit Shah /* Callers must take the port->outvq_lock */
reclaim_consumed_buffers(struct port * port)58351df0accSAmit Shah static void reclaim_consumed_buffers(struct port *port)
584276a3e95SSjur Brændeland {
58551df0accSAmit Shah 	struct port_buffer *buf;
58651df0accSAmit Shah 	unsigned int len;
587d7a62cd0SAmit Shah 
588d7a62cd0SAmit Shah 	if (!port->portdev) {
589d7a62cd0SAmit Shah 		/* Device has been unplugged.  vqs are already gone. */
590d7a62cd0SAmit Shah 		return;
591276a3e95SSjur Brændeland 	}
5921b637046SSjur Brændeland 	while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
59351df0accSAmit Shah 		free_buf(buf, false);
59451df0accSAmit Shah 		port->outvq_full = false;
59551df0accSAmit Shah 	}
59651df0accSAmit Shah }
597eb5e89fcSMasami Hiramatsu 
__send_to_port(struct port * port,struct scatterlist * sg,int nents,size_t in_count,void * data,bool nonblock)598eb5e89fcSMasami Hiramatsu static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
599276a3e95SSjur Brændeland 			      int nents, size_t in_count,
60051df0accSAmit Shah 			      void *data, bool nonblock)
60151df0accSAmit Shah {
602589575a2SRusty Russell 	struct virtqueue *out_vq;
60351df0accSAmit Shah 	int err;
60451df0accSAmit Shah 	unsigned long flags;
60551df0accSAmit Shah 	unsigned int len;
60651df0accSAmit Shah 
60751df0accSAmit Shah 	out_vq = port->out_vq;
60851df0accSAmit Shah 
60951df0accSAmit Shah 	spin_lock_irqsave(&port->outvq_lock, flags);
61051df0accSAmit Shah 
61151df0accSAmit Shah 	reclaim_consumed_buffers(port);
6126797999dSRusty Russell 
61351df0accSAmit Shah 	err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
61451df0accSAmit Shah 
61551df0accSAmit Shah 	/* Tell Host to go! */
61651df0accSAmit Shah 	virtqueue_kick(out_vq);
617589575a2SRusty Russell 
61851df0accSAmit Shah 	if (err) {
61951df0accSAmit Shah 		in_count = 0;
62051df0accSAmit Shah 		goto done;
62151df0accSAmit Shah 	}
622589575a2SRusty Russell 
62351df0accSAmit Shah 	if (out_vq->num_free == 0)
62451df0accSAmit Shah 		port->outvq_full = true;
62551df0accSAmit Shah 
62651df0accSAmit Shah 	if (nonblock)
62751df0accSAmit Shah 		goto done;
62851df0accSAmit Shah 
62951df0accSAmit Shah 	/*
63051df0accSAmit Shah 	 * Wait till the host acknowledges it pushed out the data we
63151df0accSAmit Shah 	 * sent.  This is done for data from the hvc_console; the tty
63251df0accSAmit Shah 	 * operations are performed with spinlocks held so we can't
63351df0accSAmit Shah 	 * sleep here.  An alternative would be to copy the data to a
63451df0accSAmit Shah 	 * buffer and relax the spinning requirement.  The downside is
63551df0accSAmit Shah 	 * we need to kmalloc a GFP_ATOMIC buffer each time the
63651df0accSAmit Shah 	 * console driver writes something out.
63740e4dc53SHeinz Graalfs 	 */
63840e4dc53SHeinz Graalfs 	while (!virtqueue_get_buf(out_vq, &len)
63951df0accSAmit Shah 		&& !virtqueue_is_broken(out_vq))
64051df0accSAmit Shah 		cpu_relax();
64151df0accSAmit Shah done:
64217e5b4f2SAmit Shah 	spin_unlock_irqrestore(&port->outvq_lock, flags);
64317e5b4f2SAmit Shah 
64451df0accSAmit Shah 	port->stats.bytes_sent += in_count;
64551df0accSAmit Shah 	/*
64651df0accSAmit Shah 	 * We're expected to return the amount of data we wrote -- all
64751df0accSAmit Shah 	 * of it
64851df0accSAmit Shah 	 */
64951df0accSAmit Shah 	return in_count;
65051df0accSAmit Shah }
65151df0accSAmit Shah 
65251df0accSAmit Shah /*
65351df0accSAmit Shah  * Give out the data that's requested from the buffer that we have
65451df0accSAmit Shah  * queued up.
65548b36066SMichael S. Tsirkin  */
fill_readbuf(struct port * port,char __user * out_buf,size_t out_count,bool to_user)65648b36066SMichael S. Tsirkin static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
65751df0accSAmit Shah 			    size_t out_count, bool to_user)
65851df0accSAmit Shah {
65951df0accSAmit Shah 	struct port_buffer *buf;
66051df0accSAmit Shah 	unsigned long flags;
66151df0accSAmit Shah 
66251df0accSAmit Shah 	if (!out_count || !port_has_data(port))
66351df0accSAmit Shah 		return 0;
66451df0accSAmit Shah 
66551df0accSAmit Shah 	buf = port->inbuf;
66651df0accSAmit Shah 	out_count = min(out_count, buf->len - buf->offset);
66751df0accSAmit Shah 
66851df0accSAmit Shah 	if (to_user) {
66951df0accSAmit Shah 		ssize_t ret;
67051df0accSAmit Shah 
67151df0accSAmit Shah 		ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
67251df0accSAmit Shah 		if (ret)
67351df0accSAmit Shah 			return -EFAULT;
67448b36066SMichael S. Tsirkin 	} else {
67548b36066SMichael S. Tsirkin 		memcpy((__force char *)out_buf, buf->buf + buf->offset,
67651df0accSAmit Shah 		       out_count);
67751df0accSAmit Shah 	}
67851df0accSAmit Shah 
67951df0accSAmit Shah 	buf->offset += out_count;
68051df0accSAmit Shah 
68151df0accSAmit Shah 	if (buf->offset == buf->len) {
68251df0accSAmit Shah 		/*
68351df0accSAmit Shah 		 * We're done using all the data in this buffer.
68451df0accSAmit Shah 		 * Re-queue so that the Host can send us more data.
68551df0accSAmit Shah 		 */
68651df0accSAmit Shah 		spin_lock_irqsave(&port->inbuf_lock, flags);
68751df0accSAmit Shah 		port->inbuf = NULL;
68851df0accSAmit Shah 
68951df0accSAmit Shah 		if (add_inbuf(port->in_vq, buf) < 0)
69051df0accSAmit Shah 			dev_warn(port->dev, "failed add_buf\n");
69151df0accSAmit Shah 
69251df0accSAmit Shah 		spin_unlock_irqrestore(&port->inbuf_lock, flags);
69351df0accSAmit Shah 	}
69451df0accSAmit Shah 	/* Return the number of bytes actually copied */
69551df0accSAmit Shah 	return out_count;
69651df0accSAmit Shah }
69751df0accSAmit Shah 
69851df0accSAmit Shah /* The condition that must be true for polling to end */
will_read_block(struct port * port)69951df0accSAmit Shah static bool will_read_block(struct port *port)
70051df0accSAmit Shah {
70151df0accSAmit Shah 	if (!port->guest_connected) {
70251df0accSAmit Shah 		/* Port got hot-unplugged. Let's exit. */
70351df0accSAmit Shah 		return false;
70451df0accSAmit Shah 	}
70551df0accSAmit Shah 	return !port_has_data(port) && port->host_connected;
70651df0accSAmit Shah }
70751df0accSAmit Shah 
will_write_block(struct port * port)70851df0accSAmit Shah static bool will_write_block(struct port *port)
70951df0accSAmit Shah {
71051df0accSAmit Shah 	bool ret;
71151df0accSAmit Shah 
71251df0accSAmit Shah 	if (!port->guest_connected) {
71351df0accSAmit Shah 		/* Port got hot-unplugged. Let's exit. */
71451df0accSAmit Shah 		return false;
71551df0accSAmit Shah 	}
71651df0accSAmit Shah 	if (!port->host_connected)
71751df0accSAmit Shah 		return true;
71851df0accSAmit Shah 
71951df0accSAmit Shah 	spin_lock_irq(&port->outvq_lock);
72051df0accSAmit Shah 	/*
72151df0accSAmit Shah 	 * Check if the Host has consumed any buffers since we last
72251df0accSAmit Shah 	 * sent data (this is only applicable for nonblocking ports).
72351df0accSAmit Shah 	 */
72451df0accSAmit Shah 	reclaim_consumed_buffers(port);
72551df0accSAmit Shah 	ret = port->outvq_full;
72651df0accSAmit Shah 	spin_unlock_irq(&port->outvq_lock);
72751df0accSAmit Shah 
72851df0accSAmit Shah 	return ret;
72951df0accSAmit Shah }
73051df0accSAmit Shah 
port_fops_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)73151df0accSAmit Shah static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
73251df0accSAmit Shah 			      size_t count, loff_t *offp)
73351df0accSAmit Shah {
73451df0accSAmit Shah 	struct port *port;
73551df0accSAmit Shah 	ssize_t ret;
73651df0accSAmit Shah 
73751df0accSAmit Shah 	port = filp->private_data;
73896f97a83SAmit Shah 
73996f97a83SAmit Shah 	/* Port is hot-unplugged. */
74096f97a83SAmit Shah 	if (!port->guest_connected)
74196f97a83SAmit Shah 		return -ENODEV;
74251df0accSAmit Shah 
74351df0accSAmit Shah 	if (!port_has_data(port)) {
74451df0accSAmit Shah 		/*
74551df0accSAmit Shah 		 * If nothing's connected on the host just return 0 in
74651df0accSAmit Shah 		 * case of list_empty; this tells the userspace app
74751df0accSAmit Shah 		 * that there's no connection
74851df0accSAmit Shah 		 */
74951df0accSAmit Shah 		if (!port->host_connected)
75051df0accSAmit Shah 			return 0;
75151df0accSAmit Shah 		if (filp->f_flags & O_NONBLOCK)
75251df0accSAmit Shah 			return -EAGAIN;
753a08fa92dSAmit Shah 
75451df0accSAmit Shah 		ret = wait_event_freezable(port->waitqueue,
75551df0accSAmit Shah 					   !will_read_block(port));
75651df0accSAmit Shah 		if (ret < 0)
75751df0accSAmit Shah 			return ret;
75896f97a83SAmit Shah 	}
75951df0accSAmit Shah 	/* Port got hot-unplugged while we were waiting above. */
76051df0accSAmit Shah 	if (!port->guest_connected)
76151df0accSAmit Shah 		return -ENODEV;
76251df0accSAmit Shah 	/*
76351df0accSAmit Shah 	 * We could've received a disconnection message while we were
76451df0accSAmit Shah 	 * waiting for more data.
76551df0accSAmit Shah 	 *
76651df0accSAmit Shah 	 * This check is not clubbed in the if() statement above as we
76751df0accSAmit Shah 	 * might receive some data as well as the host could get
76851df0accSAmit Shah 	 * disconnected after we got woken up from our wait.  So we
76951df0accSAmit Shah 	 * really want to give off whatever data we have and only then
77051df0accSAmit Shah 	 * check for host_connected.
77151df0accSAmit Shah 	 */
77251df0accSAmit Shah 	if (!port_has_data(port) && !port->host_connected)
77351df0accSAmit Shah 		return 0;
77451df0accSAmit Shah 
77551df0accSAmit Shah 	return fill_readbuf(port, ubuf, count, true);
77651df0accSAmit Shah }
777efe75d24SMasami Hiramatsu 
wait_port_writable(struct port * port,bool nonblock)778efe75d24SMasami Hiramatsu static int wait_port_writable(struct port *port, bool nonblock)
779efe75d24SMasami Hiramatsu {
780efe75d24SMasami Hiramatsu 	int ret;
781efe75d24SMasami Hiramatsu 
782efe75d24SMasami Hiramatsu 	if (will_write_block(port)) {
783efe75d24SMasami Hiramatsu 		if (nonblock)
784efe75d24SMasami Hiramatsu 			return -EAGAIN;
785efe75d24SMasami Hiramatsu 
786efe75d24SMasami Hiramatsu 		ret = wait_event_freezable(port->waitqueue,
787efe75d24SMasami Hiramatsu 					   !will_write_block(port));
788efe75d24SMasami Hiramatsu 		if (ret < 0)
789efe75d24SMasami Hiramatsu 			return ret;
790efe75d24SMasami Hiramatsu 	}
791efe75d24SMasami Hiramatsu 	/* Port got hot-unplugged. */
792efe75d24SMasami Hiramatsu 	if (!port->guest_connected)
793efe75d24SMasami Hiramatsu 		return -ENODEV;
794efe75d24SMasami Hiramatsu 
795efe75d24SMasami Hiramatsu 	return 0;
796efe75d24SMasami Hiramatsu }
79751df0accSAmit Shah 
port_fops_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * offp)79851df0accSAmit Shah static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
79951df0accSAmit Shah 			       size_t count, loff_t *offp)
80051df0accSAmit Shah {
801276a3e95SSjur Brændeland 	struct port *port;
80251df0accSAmit Shah 	struct port_buffer *buf;
80351df0accSAmit Shah 	ssize_t ret;
804276a3e95SSjur Brændeland 	bool nonblock;
80551df0accSAmit Shah 	struct scatterlist sg[1];
80651df0accSAmit Shah 
80751df0accSAmit Shah 	/* Userspace could be out to fool us */
80851df0accSAmit Shah 	if (!count)
80951df0accSAmit Shah 		return 0;
81051df0accSAmit Shah 
81151df0accSAmit Shah 	port = filp->private_data;
81251df0accSAmit Shah 
81351df0accSAmit Shah 	nonblock = filp->f_flags & O_NONBLOCK;
814efe75d24SMasami Hiramatsu 
81551df0accSAmit Shah 	ret = wait_port_writable(port, nonblock);
81651df0accSAmit Shah 	if (ret < 0)
81751df0accSAmit Shah 		return ret;
81851df0accSAmit Shah 
81951df0accSAmit Shah 	count = min((size_t)(32 * 1024), count);
8202855b335SMichael S. Tsirkin 
82151df0accSAmit Shah 	buf = alloc_buf(port->portdev->vdev, count, 0);
82251df0accSAmit Shah 	if (!buf)
82351df0accSAmit Shah 		return -ENOMEM;
824276a3e95SSjur Brændeland 
82551df0accSAmit Shah 	ret = copy_from_user(buf->buf, ubuf, count);
82651df0accSAmit Shah 	if (ret) {
82751df0accSAmit Shah 		ret = -EFAULT;
82851df0accSAmit Shah 		goto free_buf;
82951df0accSAmit Shah 	}
83051df0accSAmit Shah 
83151df0accSAmit Shah 	/*
83251df0accSAmit Shah 	 * We now ask send_buf() to not spin for generic ports -- we
83351df0accSAmit Shah 	 * can re-use the same code path that non-blocking file
83451df0accSAmit Shah 	 * descriptors take for blocking file descriptors since the
83551df0accSAmit Shah 	 * wait is already done and we're certain the write will go
83651df0accSAmit Shah 	 * through to the host.
83751df0accSAmit Shah 	 */
838276a3e95SSjur Brændeland 	nonblock = true;
839276a3e95SSjur Brændeland 	sg_init_one(sg, buf->buf, count);
84051df0accSAmit Shah 	ret = __send_to_port(port, sg, 1, count, buf, nonblock);
84151df0accSAmit Shah 
84251df0accSAmit Shah 	if (nonblock && ret > 0)
84351df0accSAmit Shah 		goto out;
84451df0accSAmit Shah 
8451b637046SSjur Brændeland free_buf:
84651df0accSAmit Shah 	free_buf(buf, true);
84751df0accSAmit Shah out:
84851df0accSAmit Shah 	return ret;
84951df0accSAmit Shah }
850eb5e89fcSMasami Hiramatsu 
851eb5e89fcSMasami Hiramatsu struct sg_list {
8528ca84a50SMasami Hiramatsu 	unsigned int n;
853eb5e89fcSMasami Hiramatsu 	unsigned int size;
854eb5e89fcSMasami Hiramatsu 	size_t len;
855eb5e89fcSMasami Hiramatsu 	struct scatterlist *sg;
856eb5e89fcSMasami Hiramatsu };
857eb5e89fcSMasami Hiramatsu 
pipe_to_sg(struct pipe_inode_info * pipe,struct pipe_buffer * buf,struct splice_desc * sd)858eb5e89fcSMasami Hiramatsu static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
859eb5e89fcSMasami Hiramatsu 			struct splice_desc *sd)
860eb5e89fcSMasami Hiramatsu {
861ec8fc870SMasami Hiramatsu 	struct sg_list *sgl = sd->u.data;
862eb5e89fcSMasami Hiramatsu 	unsigned int offset, len;
8638ca84a50SMasami Hiramatsu 
864eb5e89fcSMasami Hiramatsu 	if (sgl->n == sgl->size)
865eb5e89fcSMasami Hiramatsu 		return 0;
866eb5e89fcSMasami Hiramatsu 
867c928f642SChristoph Hellwig 	/* Try lock this page */
868eb5e89fcSMasami Hiramatsu 	if (pipe_buf_try_steal(pipe, buf)) {
869eb5e89fcSMasami Hiramatsu 		/* Get reference and unlock page for moving */
870eb5e89fcSMasami Hiramatsu 		get_page(buf->page);
871eb5e89fcSMasami Hiramatsu 		unlock_page(buf->page);
872eb5e89fcSMasami Hiramatsu 
873eb5e89fcSMasami Hiramatsu 		len = min(buf->len, sd->len);
874ec8fc870SMasami Hiramatsu 		sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
875ec8fc870SMasami Hiramatsu 	} else {
876ec8fc870SMasami Hiramatsu 		/* Failback to copying a page */
877c9efe511SAl Viro 		struct page *page = alloc_page(GFP_KERNEL);
878ec8fc870SMasami Hiramatsu 		char *src;
879ec8fc870SMasami Hiramatsu 
880ec8fc870SMasami Hiramatsu 		if (!page)
881ec8fc870SMasami Hiramatsu 			return -ENOMEM;
882ec8fc870SMasami Hiramatsu 
883ec8fc870SMasami Hiramatsu 		offset = sd->pos & ~PAGE_MASK;
884ec8fc870SMasami Hiramatsu 
885ec8fc870SMasami Hiramatsu 		len = sd->len;
886ec8fc870SMasami Hiramatsu 		if (len + offset > PAGE_SIZE)
887ec8fc870SMasami Hiramatsu 			len = PAGE_SIZE - offset;
888fbb32750SAl Viro 
889c9efe511SAl Viro 		src = kmap_atomic(buf->page);
890fbb32750SAl Viro 		memcpy(page_address(page) + offset, src + buf->offset, len);
891ec8fc870SMasami Hiramatsu 		kunmap_atomic(src);
892ec8fc870SMasami Hiramatsu 
893ec8fc870SMasami Hiramatsu 		sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
894eb5e89fcSMasami Hiramatsu 	}
895eb5e89fcSMasami Hiramatsu 	sgl->n++;
896eb5e89fcSMasami Hiramatsu 	sgl->len += len;
897eb5e89fcSMasami Hiramatsu 
898eb5e89fcSMasami Hiramatsu 	return len;
899eb5e89fcSMasami Hiramatsu }
900eb5e89fcSMasami Hiramatsu 
901eb5e89fcSMasami Hiramatsu /* Faster zero-copy write by splicing */
port_fops_splice_write(struct pipe_inode_info * pipe,struct file * filp,loff_t * ppos,size_t len,unsigned int flags)902eb5e89fcSMasami Hiramatsu static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
903eb5e89fcSMasami Hiramatsu 				      struct file *filp, loff_t *ppos,
904eb5e89fcSMasami Hiramatsu 				      size_t len, unsigned int flags)
905eb5e89fcSMasami Hiramatsu {
906eb5e89fcSMasami Hiramatsu 	struct port *port = filp->private_data;
907eb5e89fcSMasami Hiramatsu 	struct sg_list sgl;
908276a3e95SSjur Brændeland 	ssize_t ret;
909eb5e89fcSMasami Hiramatsu 	struct port_buffer *buf;
910eb5e89fcSMasami Hiramatsu 	struct splice_desc sd = {
911eb5e89fcSMasami Hiramatsu 		.total_len = len,
912eb5e89fcSMasami Hiramatsu 		.flags = flags,
913eb5e89fcSMasami Hiramatsu 		.pos = *ppos,
914eb5e89fcSMasami Hiramatsu 		.u.data = &sgl,
9158cefc107SDavid Howells 	};
916eb5e89fcSMasami Hiramatsu 	unsigned int occupancy;
9171b637046SSjur Brændeland 
9181b637046SSjur Brændeland 	/*
9191b637046SSjur Brændeland 	 * Rproc_serial does not yet support splice. To support splice
9201b637046SSjur Brændeland 	 * pipe_to_sg() must allocate dma-buffers and copy content from
9211b637046SSjur Brændeland 	 * regular pages to dma pages. And alloc_buf and free_buf must
9221b637046SSjur Brændeland 	 * support allocating and freeing such a list of dma-buffers.
9231b637046SSjur Brændeland 	 */
9241b637046SSjur Brændeland 	if (is_rproc_serial(port->out_vq->vdev))
9251b637046SSjur Brændeland 		return -EINVAL;
9262b4fbf02SYoshihiro YUNOMAE 
9272b4fbf02SYoshihiro YUNOMAE 	pipe_lock(pipe);
9288cefc107SDavid Howells 	ret = 0;
9292b4fbf02SYoshihiro YUNOMAE 	if (pipe_empty(pipe->head, pipe->tail))
93068c034feSYoshihiro YUNOMAE 		goto error_out;
931efe75d24SMasami Hiramatsu 
932efe75d24SMasami Hiramatsu 	ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
9332b4fbf02SYoshihiro YUNOMAE 	if (ret < 0)
934efe75d24SMasami Hiramatsu 		goto error_out;
9358cefc107SDavid Howells 
9368cefc107SDavid Howells 	occupancy = pipe_occupancy(pipe->head, pipe->tail);
9378cefc107SDavid Howells 	buf = alloc_buf(port->portdev->vdev, 0, occupancy);
9382b4fbf02SYoshihiro YUNOMAE 
9392b4fbf02SYoshihiro YUNOMAE 	if (!buf) {
9402b4fbf02SYoshihiro YUNOMAE 		ret = -ENOMEM;
9412b4fbf02SYoshihiro YUNOMAE 		goto error_out;
942276a3e95SSjur Brændeland 	}
943eb5e89fcSMasami Hiramatsu 
944eb5e89fcSMasami Hiramatsu 	sgl.n = 0;
9458cefc107SDavid Howells 	sgl.len = 0;
946276a3e95SSjur Brændeland 	sgl.size = occupancy;
9478ca84a50SMasami Hiramatsu 	sgl.sg = buf->sg;
948eb5e89fcSMasami Hiramatsu 	sg_init_table(sgl.sg, sgl.size);
9492b4fbf02SYoshihiro YUNOMAE 	ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
950eb5e89fcSMasami Hiramatsu 	pipe_unlock(pipe);
951276a3e95SSjur Brændeland 	if (likely(ret > 0))
952eb5e89fcSMasami Hiramatsu 		ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
953fe529537SSjur Brændeland 
9541b637046SSjur Brændeland 	if (unlikely(ret <= 0))
955eb5e89fcSMasami Hiramatsu 		free_buf(buf, true);
9562b4fbf02SYoshihiro YUNOMAE 	return ret;
9572b4fbf02SYoshihiro YUNOMAE 
9582b4fbf02SYoshihiro YUNOMAE error_out:
9592b4fbf02SYoshihiro YUNOMAE 	pipe_unlock(pipe);
960eb5e89fcSMasami Hiramatsu 	return ret;
961eb5e89fcSMasami Hiramatsu }
962afc9a42bSAl Viro 
port_fops_poll(struct file * filp,poll_table * wait)96351df0accSAmit Shah static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
96451df0accSAmit Shah {
965afc9a42bSAl Viro 	struct port *port;
96651df0accSAmit Shah 	__poll_t ret;
96751df0accSAmit Shah 
96851df0accSAmit Shah 	port = filp->private_data;
96951df0accSAmit Shah 	poll_wait(filp, &port->waitqueue, wait);
97051df0accSAmit Shah 
97151df0accSAmit Shah 	if (!port->guest_connected) {
972a9a08845SLinus Torvalds 		/* Port got unplugged */
97351df0accSAmit Shah 		return EPOLLHUP;
97451df0accSAmit Shah 	}
97551df0accSAmit Shah 	ret = 0;
976a9a08845SLinus Torvalds 	if (!will_read_block(port))
97751df0accSAmit Shah 		ret |= EPOLLIN | EPOLLRDNORM;
978a9a08845SLinus Torvalds 	if (!will_write_block(port))
97951df0accSAmit Shah 		ret |= EPOLLOUT;
980a9a08845SLinus Torvalds 	if (!port->host_connected)
98151df0accSAmit Shah 		ret |= EPOLLHUP;
98251df0accSAmit Shah 
98351df0accSAmit Shah 	return ret;
98451df0accSAmit Shah }
98551df0accSAmit Shah 
98651df0accSAmit Shah static void remove_port(struct kref *kref);
98751df0accSAmit Shah 
port_fops_release(struct inode * inode,struct file * filp)98851df0accSAmit Shah static int port_fops_release(struct inode *inode, struct file *filp)
98951df0accSAmit Shah {
99051df0accSAmit Shah 	struct port *port;
99151df0accSAmit Shah 
99251df0accSAmit Shah 	port = filp->private_data;
99351df0accSAmit Shah 
99451df0accSAmit Shah 	/* Notify host of port being closed */
99551df0accSAmit Shah 	send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
99651df0accSAmit Shah 
99751df0accSAmit Shah 	spin_lock_irq(&port->inbuf_lock);
99851df0accSAmit Shah 	port->guest_connected = false;
99951df0accSAmit Shah 
100051df0accSAmit Shah 	discard_port_data(port);
100151df0accSAmit Shah 
100251df0accSAmit Shah 	spin_unlock_irq(&port->inbuf_lock);
100351df0accSAmit Shah 
100451df0accSAmit Shah 	spin_lock_irq(&port->outvq_lock);
100551df0accSAmit Shah 	reclaim_consumed_buffers(port);
100651df0accSAmit Shah 	spin_unlock_irq(&port->outvq_lock);
10071b637046SSjur Brændeland 
100851df0accSAmit Shah 	reclaim_dma_bufs();
100951df0accSAmit Shah 	/*
101051df0accSAmit Shah 	 * Locks aren't necessary here as a port can't be opened after
101151df0accSAmit Shah 	 * unplug, and if a port isn't unplugged, a kref would already
101251df0accSAmit Shah 	 * exist for the port.  Plus, taking ports_lock here would
101351df0accSAmit Shah 	 * create a dependency on other locks taken by functions
101451df0accSAmit Shah 	 * inside remove_port if we're the last holder of the port,
101551df0accSAmit Shah 	 * creating many problems.
101651df0accSAmit Shah 	 */
101751df0accSAmit Shah 	kref_put(&port->kref, remove_port);
101851df0accSAmit Shah 
101951df0accSAmit Shah 	return 0;
102051df0accSAmit Shah }
102151df0accSAmit Shah 
port_fops_open(struct inode * inode,struct file * filp)102251df0accSAmit Shah static int port_fops_open(struct inode *inode, struct file *filp)
102351df0accSAmit Shah {
102451df0accSAmit Shah 	struct cdev *cdev = inode->i_cdev;
102551df0accSAmit Shah 	struct port *port;
102651df0accSAmit Shah 	int ret;
1027057b82beSAmit Shah 
102851df0accSAmit Shah 	/* We get the port with a kref here */
1029671bdea2SAmit Shah 	port = find_port_by_devt(cdev->dev);
1030671bdea2SAmit Shah 	if (!port) {
1031671bdea2SAmit Shah 		/* Port was unplugged before we could proceed */
1032671bdea2SAmit Shah 		return -ENXIO;
103351df0accSAmit Shah 	}
103451df0accSAmit Shah 	filp->private_data = port;
103551df0accSAmit Shah 
103651df0accSAmit Shah 	/*
103751df0accSAmit Shah 	 * Don't allow opening of console port devices -- that's done
103851df0accSAmit Shah 	 * via /dev/hvc
103951df0accSAmit Shah 	 */
104051df0accSAmit Shah 	if (is_console_port(port)) {
104151df0accSAmit Shah 		ret = -ENXIO;
104251df0accSAmit Shah 		goto out;
104351df0accSAmit Shah 	}
104451df0accSAmit Shah 
104551df0accSAmit Shah 	/* Allow only one process to open a particular port at a time */
104651df0accSAmit Shah 	spin_lock_irq(&port->inbuf_lock);
104751df0accSAmit Shah 	if (port->guest_connected) {
104874ff582cSAmit Shah 		spin_unlock_irq(&port->inbuf_lock);
104951df0accSAmit Shah 		ret = -EBUSY;
105051df0accSAmit Shah 		goto out;
105151df0accSAmit Shah 	}
105251df0accSAmit Shah 
105351df0accSAmit Shah 	port->guest_connected = true;
105451df0accSAmit Shah 	spin_unlock_irq(&port->inbuf_lock);
105551df0accSAmit Shah 
105651df0accSAmit Shah 	spin_lock_irq(&port->outvq_lock);
105751df0accSAmit Shah 	/*
105851df0accSAmit Shah 	 * There might be a chance that we missed reclaiming a few
105951df0accSAmit Shah 	 * buffers in the window of the port getting previously closed
106051df0accSAmit Shah 	 * and opening now.
106151df0accSAmit Shah 	 */
106251df0accSAmit Shah 	reclaim_consumed_buffers(port);
106351df0accSAmit Shah 	spin_unlock_irq(&port->outvq_lock);
106451df0accSAmit Shah 
106551df0accSAmit Shah 	nonseekable_open(inode, filp);
106651df0accSAmit Shah 
106751df0accSAmit Shah 	/* Notify host of port being opened */
106851df0accSAmit Shah 	send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
106951df0accSAmit Shah 
107051df0accSAmit Shah 	return 0;
107151df0accSAmit Shah out:
107251df0accSAmit Shah 	kref_put(&port->kref, remove_port);
107351df0accSAmit Shah 	return ret;
107451df0accSAmit Shah }
107551df0accSAmit Shah 
port_fops_fasync(int fd,struct file * filp,int mode)107651df0accSAmit Shah static int port_fops_fasync(int fd, struct file *filp, int mode)
107751df0accSAmit Shah {
107851df0accSAmit Shah 	struct port *port;
107951df0accSAmit Shah 
108051df0accSAmit Shah 	port = filp->private_data;
108151df0accSAmit Shah 	return fasync_helper(fd, filp, mode, &port->async_queue);
108251df0accSAmit Shah }
108351df0accSAmit Shah 
108451df0accSAmit Shah /*
108551df0accSAmit Shah  * The file operations that we support: programs in the guest can open
108651df0accSAmit Shah  * a console device, read from it, write to it, poll for data and
108751df0accSAmit Shah  * close it.  The devices are at
108851df0accSAmit Shah  *   /dev/vport<device number>p<port number>
108951df0accSAmit Shah  */
109051df0accSAmit Shah static const struct file_operations port_fops = {
109151df0accSAmit Shah 	.owner = THIS_MODULE,
109251df0accSAmit Shah 	.open  = port_fops_open,
109351df0accSAmit Shah 	.read  = port_fops_read,
1094eb5e89fcSMasami Hiramatsu 	.write = port_fops_write,
109551df0accSAmit Shah 	.splice_write = port_fops_splice_write,
109651df0accSAmit Shah 	.poll  = port_fops_poll,
109751df0accSAmit Shah 	.release = port_fops_release,
109851df0accSAmit Shah 	.fasync = port_fops_fasync,
109951df0accSAmit Shah 	.llseek = no_llseek,
110051df0accSAmit Shah };
110151df0accSAmit Shah 
110251df0accSAmit Shah /*
110351df0accSAmit Shah  * The put_chars() callback is pretty straightforward.
110451df0accSAmit Shah  *
110551df0accSAmit Shah  * We turn the characters into a scatter-gather list, add it to the
110651df0accSAmit Shah  * output queue and then kick the Host.  Then we sit here waiting for
1107ecda85e7SJuergen Gross  * it to finish: inefficient in theory, but in practice
110851df0accSAmit Shah  * implementations will do it immediately.
110951df0accSAmit Shah  */
put_chars(u32 vtermno,const char * buf,int count)111051df0accSAmit Shah static int put_chars(u32 vtermno, const char *buf, int count)
111151df0accSAmit Shah {
1112276a3e95SSjur Brændeland 	struct port *port;
1113c4baad50SOmar Sandoval 	struct scatterlist sg[1];
1114c4baad50SOmar Sandoval 	void *data;
111551df0accSAmit Shah 	int ret;
111651df0accSAmit Shah 
111751df0accSAmit Shah 	if (unlikely(early_put_chars))
111851df0accSAmit Shah 		return early_put_chars(vtermno, buf, count);
111951df0accSAmit Shah 
112051df0accSAmit Shah 	port = find_port_by_vtermno(vtermno);
112151df0accSAmit Shah 	if (!port)
112251df0accSAmit Shah 		return -EPIPE;
1123c4baad50SOmar Sandoval 
1124c4baad50SOmar Sandoval 	data = kmemdup(buf, count, GFP_ATOMIC);
1125c4baad50SOmar Sandoval 	if (!data)
1126c4baad50SOmar Sandoval 		return -ENOMEM;
1127c4baad50SOmar Sandoval 
1128c4baad50SOmar Sandoval 	sg_init_one(sg, data, count);
1129c4baad50SOmar Sandoval 	ret = __send_to_port(port, sg, 1, count, data, false);
1130c4baad50SOmar Sandoval 	kfree(data);
113151df0accSAmit Shah 	return ret;
113251df0accSAmit Shah }
113351df0accSAmit Shah 
113451df0accSAmit Shah /*
113551df0accSAmit Shah  * get_chars() is the callback from the hvc_console infrastructure
113651df0accSAmit Shah  * when an interrupt is received.
113751df0accSAmit Shah  *
113851df0accSAmit Shah  * We call out to fill_readbuf that gets us the required data from the
113951df0accSAmit Shah  * buffers that are queued up.
114051df0accSAmit Shah  */
get_chars(u32 vtermno,char * buf,int count)114151df0accSAmit Shah static int get_chars(u32 vtermno, char *buf, int count)
114251df0accSAmit Shah {
114351df0accSAmit Shah 	struct port *port;
114451df0accSAmit Shah 
114551df0accSAmit Shah 	/* If we've not set up the port yet, we have no input to give. */
114651df0accSAmit Shah 	if (unlikely(early_put_chars))
114751df0accSAmit Shah 		return 0;
114851df0accSAmit Shah 
114951df0accSAmit Shah 	port = find_port_by_vtermno(vtermno);
115051df0accSAmit Shah 	if (!port)
115151df0accSAmit Shah 		return -EPIPE;
115251df0accSAmit Shah 
115351df0accSAmit Shah 	/* If we don't have an input queue yet, we can't get input. */
115451df0accSAmit Shah 	BUG_ON(!port->in_vq);
115548b36066SMichael S. Tsirkin 
115651df0accSAmit Shah 	return fill_readbuf(port, (__force char __user *)buf, count, false);
115751df0accSAmit Shah }
115851df0accSAmit Shah 
resize_console(struct port * port)115951df0accSAmit Shah static void resize_console(struct port *port)
116051df0accSAmit Shah {
116151df0accSAmit Shah 	struct virtio_device *vdev;
116251df0accSAmit Shah 
116351df0accSAmit Shah 	/* The port could have been hot-unplugged */
116451df0accSAmit Shah 	if (!port || !is_console_port(port))
116551df0accSAmit Shah 		return;
116651df0accSAmit Shah 
11671b637046SSjur Brændeland 	vdev = port->portdev->vdev;
11681b637046SSjur Brændeland 
11691b637046SSjur Brændeland 	/* Don't test F_SIZE at all if we're rproc: not a valid feature! */
11701b637046SSjur Brændeland 	if (!is_rproc_serial(vdev) &&
117151df0accSAmit Shah 	    virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
117251df0accSAmit Shah 		hvc_resize(port->cons.hvc, port->cons.ws);
117351df0accSAmit Shah }
117451df0accSAmit Shah 
117551df0accSAmit Shah /* We set the configuration at this point, since we now have a tty */
notifier_add_vio(struct hvc_struct * hp,int data)117651df0accSAmit Shah static int notifier_add_vio(struct hvc_struct *hp, int data)
117751df0accSAmit Shah {
117851df0accSAmit Shah 	struct port *port;
117951df0accSAmit Shah 
118051df0accSAmit Shah 	port = find_port_by_vtermno(hp->vtermno);
118151df0accSAmit Shah 	if (!port)
118251df0accSAmit Shah 		return -EINVAL;
118351df0accSAmit Shah 
118451df0accSAmit Shah 	hp->irq_requested = 1;
118551df0accSAmit Shah 	resize_console(port);
118651df0accSAmit Shah 
118751df0accSAmit Shah 	return 0;
118851df0accSAmit Shah }
118951df0accSAmit Shah 
notifier_del_vio(struct hvc_struct * hp,int data)119051df0accSAmit Shah static void notifier_del_vio(struct hvc_struct *hp, int data)
119151df0accSAmit Shah {
119251df0accSAmit Shah 	hp->irq_requested = 0;
119351df0accSAmit Shah }
119451df0accSAmit Shah 
119551df0accSAmit Shah /* The operations for console ports. */
119651df0accSAmit Shah static const struct hv_ops hv_ops = {
119751df0accSAmit Shah 	.get_chars = get_chars,
119851df0accSAmit Shah 	.put_chars = put_chars,
119951df0accSAmit Shah 	.notifier_add = notifier_add_vio,
120051df0accSAmit Shah 	.notifier_del = notifier_del_vio,
120151df0accSAmit Shah 	.notifier_hangup = notifier_del_vio,
120251df0accSAmit Shah };
120351df0accSAmit Shah 
120451df0accSAmit Shah /*
120551df0accSAmit Shah  * Console drivers are initialized very early so boot messages can go
120651df0accSAmit Shah  * out, so we do things slightly differently from the generic virtio
120751df0accSAmit Shah  * initialization of the net and block drivers.
120851df0accSAmit Shah  *
120951df0accSAmit Shah  * At this stage, the console is output-only.  It's too early to set
121051df0accSAmit Shah  * up a virtqueue, so we let the drivers do some boutique early-output
121151df0accSAmit Shah  * thing.
121251df0accSAmit Shah  */
virtio_cons_early_init(int (* put_chars)(u32,const char *,int))121351df0accSAmit Shah int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
121451df0accSAmit Shah {
121551df0accSAmit Shah 	early_put_chars = put_chars;
121651df0accSAmit Shah 	return hvc_instantiate(0, 0, &hv_ops);
121751df0accSAmit Shah }
12183826835aSWei Yongjun 
init_port_console(struct port * port)121951df0accSAmit Shah static int init_port_console(struct port *port)
122051df0accSAmit Shah {
122151df0accSAmit Shah 	int ret;
122251df0accSAmit Shah 
122351df0accSAmit Shah 	/*
122451df0accSAmit Shah 	 * The Host's telling us this port is a console port.  Hook it
122551df0accSAmit Shah 	 * up with an hvc console.
122651df0accSAmit Shah 	 *
122751df0accSAmit Shah 	 * To set up and manage our virtual console, we call
122851df0accSAmit Shah 	 * hvc_alloc().
122951df0accSAmit Shah 	 *
123051df0accSAmit Shah 	 * The first argument of hvc_alloc() is the virtual console
123151df0accSAmit Shah 	 * number.  The second argument is the parameter for the
123251df0accSAmit Shah 	 * notification mechanism (like irq number).  We currently
123351df0accSAmit Shah 	 * leave this as zero, virtqueues have implicit notifications.
123451df0accSAmit Shah 	 *
123551df0accSAmit Shah 	 * The third argument is a "struct hv_ops" containing the
123651df0accSAmit Shah 	 * put_chars() get_chars(), notifier_add() and notifier_del()
123751df0accSAmit Shah 	 * pointers.  The final argument is the output buffer size: we
123851df0accSAmit Shah 	 * can do any size, so we put PAGE_SIZE here.
1239e6278a54SCédric Le Goater 	 */
1240e6278a54SCédric Le Goater 	ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL);
1241e6278a54SCédric Le Goater 	if (ret < 0)
124251df0accSAmit Shah 		return ret;
1243e6278a54SCédric Le Goater 
124451df0accSAmit Shah 	port->cons.vtermno = ret;
124551df0accSAmit Shah 	port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
124651df0accSAmit Shah 	if (IS_ERR(port->cons.hvc)) {
124751df0accSAmit Shah 		ret = PTR_ERR(port->cons.hvc);
124851df0accSAmit Shah 		dev_err(port->dev,
124951df0accSAmit Shah 			"error %d allocating hvc for port\n", ret);
1250e6278a54SCédric Le Goater 		port->cons.hvc = NULL;
125151df0accSAmit Shah 		ida_free(&vtermno_ida, port->cons.vtermno);
125251df0accSAmit Shah 		return ret;
125351df0accSAmit Shah 	}
125451df0accSAmit Shah 	spin_lock_irq(&pdrvdata_lock);
125551df0accSAmit Shah 	list_add_tail(&port->cons.list, &pdrvdata.consoles);
125651df0accSAmit Shah 	spin_unlock_irq(&pdrvdata_lock);
125751df0accSAmit Shah 	port->guest_connected = true;
125851df0accSAmit Shah 
125951df0accSAmit Shah 	/*
126051df0accSAmit Shah 	 * Start using the new console output if this is the first
126151df0accSAmit Shah 	 * console to come up.
126251df0accSAmit Shah 	 */
126351df0accSAmit Shah 	if (early_put_chars)
126451df0accSAmit Shah 		early_put_chars = NULL;
126551df0accSAmit Shah 
126651df0accSAmit Shah 	/* Notify host of port being opened */
126751df0accSAmit Shah 	send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
126851df0accSAmit Shah 
126951df0accSAmit Shah 	return 0;
127051df0accSAmit Shah }
127151df0accSAmit Shah 
show_port_name(struct device * dev,struct device_attribute * attr,char * buffer)127251df0accSAmit Shah static ssize_t show_port_name(struct device *dev,
127351df0accSAmit Shah 			      struct device_attribute *attr, char *buffer)
127451df0accSAmit Shah {
127551df0accSAmit Shah 	struct port *port;
127651df0accSAmit Shah 
127751df0accSAmit Shah 	port = dev_get_drvdata(dev);
127851df0accSAmit Shah 
127951df0accSAmit Shah 	return sprintf(buffer, "%s\n", port->name);
128051df0accSAmit Shah }
128151df0accSAmit Shah 
128251df0accSAmit Shah static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
128351df0accSAmit Shah 
128451df0accSAmit Shah static struct attribute *port_sysfs_entries[] = {
128551df0accSAmit Shah 	&dev_attr_name.attr,
128651df0accSAmit Shah 	NULL
128751df0accSAmit Shah };
1288ac317e27SArvind Yadav 
128951df0accSAmit Shah static const struct attribute_group port_attribute_group = {
129051df0accSAmit Shah 	.name = NULL,		/* put in device directory */
129151df0accSAmit Shah 	.attrs = port_sysfs_entries,
129251df0accSAmit Shah };
1293ddfa728aSYangtao Li 
port_debugfs_show(struct seq_file * s,void * data)129451df0accSAmit Shah static int port_debugfs_show(struct seq_file *s, void *data)
12958d62fe94STuomas Tynkkynen {
129651df0accSAmit Shah 	struct port *port = s->private;
12978d62fe94STuomas Tynkkynen 
12988d62fe94STuomas Tynkkynen 	seq_printf(s, "name: %s\n", port->name ? port->name : "");
12998d62fe94STuomas Tynkkynen 	seq_printf(s, "guest_connected: %d\n", port->guest_connected);
13008d62fe94STuomas Tynkkynen 	seq_printf(s, "host_connected: %d\n", port->host_connected);
13018d62fe94STuomas Tynkkynen 	seq_printf(s, "outvq_full: %d\n", port->outvq_full);
13028d62fe94STuomas Tynkkynen 	seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
13038d62fe94STuomas Tynkkynen 	seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
13048d62fe94STuomas Tynkkynen 	seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
130551df0accSAmit Shah 	seq_printf(s, "is_console: %s\n",
13068d62fe94STuomas Tynkkynen 		   is_console_port(port) ? "yes" : "no");
130751df0accSAmit Shah 	seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
13088d62fe94STuomas Tynkkynen 
13098d62fe94STuomas Tynkkynen 	return 0;
13108d62fe94STuomas Tynkkynen }
1311ddfa728aSYangtao Li 
131251df0accSAmit Shah DEFINE_SHOW_ATTRIBUTE(port_debugfs);
131351df0accSAmit Shah 
set_console_size(struct port * port,u16 rows,u16 cols)131451df0accSAmit Shah static void set_console_size(struct port *port, u16 rows, u16 cols)
131551df0accSAmit Shah {
131651df0accSAmit Shah 	if (!port || !is_console_port(port))
131751df0accSAmit Shah 		return;
131851df0accSAmit Shah 
131951df0accSAmit Shah 	port->cons.ws.ws_row = rows;
132051df0accSAmit Shah 	port->cons.ws.ws_col = cols;
132151df0accSAmit Shah }
1322d791cfcbSLaurent Vivier 
fill_queue(struct virtqueue * vq,spinlock_t * lock)132351df0accSAmit Shah static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
132451df0accSAmit Shah {
1325d791cfcbSLaurent Vivier 	struct port_buffer *buf;
132651df0accSAmit Shah 	int nr_added_bufs;
132751df0accSAmit Shah 	int ret;
132851df0accSAmit Shah 
132951df0accSAmit Shah 	nr_added_bufs = 0;
13302855b335SMichael S. Tsirkin 	do {
133151df0accSAmit Shah 		buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
1332d791cfcbSLaurent Vivier 		if (!buf)
133351df0accSAmit Shah 			return -ENOMEM;
133451df0accSAmit Shah 
133551df0accSAmit Shah 		spin_lock_irq(lock);
133651df0accSAmit Shah 		ret = add_inbuf(vq, buf);
133751df0accSAmit Shah 		if (ret < 0) {
13381b637046SSjur Brændeland 			spin_unlock_irq(lock);
1339d791cfcbSLaurent Vivier 			free_buf(buf, true);
134051df0accSAmit Shah 			return ret;
134151df0accSAmit Shah 		}
134251df0accSAmit Shah 		nr_added_bufs++;
134351df0accSAmit Shah 		spin_unlock_irq(lock);
134451df0accSAmit Shah 	} while (ret > 0);
134551df0accSAmit Shah 
134651df0accSAmit Shah 	return nr_added_bufs;
134751df0accSAmit Shah }
134851df0accSAmit Shah 
send_sigio_to_port(struct port * port)134951df0accSAmit Shah static void send_sigio_to_port(struct port *port)
135051df0accSAmit Shah {
135151df0accSAmit Shah 	if (port->async_queue && port->guest_connected)
135251df0accSAmit Shah 		kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
135351df0accSAmit Shah }
135451df0accSAmit Shah 
add_port(struct ports_device * portdev,u32 id)135551df0accSAmit Shah static int add_port(struct ports_device *portdev, u32 id)
135651df0accSAmit Shah {
135751df0accSAmit Shah 	char debugfs_name[16];
135851df0accSAmit Shah 	struct port *port;
135951df0accSAmit Shah 	dev_t devt;
136051df0accSAmit Shah 	int err;
136151df0accSAmit Shah 
136251df0accSAmit Shah 	port = kmalloc(sizeof(*port), GFP_KERNEL);
136351df0accSAmit Shah 	if (!port) {
136451df0accSAmit Shah 		err = -ENOMEM;
136551df0accSAmit Shah 		goto fail;
136651df0accSAmit Shah 	}
136751df0accSAmit Shah 	kref_init(&port->kref);
136851df0accSAmit Shah 
136951df0accSAmit Shah 	port->portdev = portdev;
137051df0accSAmit Shah 	port->id = id;
137151df0accSAmit Shah 
137251df0accSAmit Shah 	port->name = NULL;
137351df0accSAmit Shah 	port->inbuf = NULL;
137451df0accSAmit Shah 	port->cons.hvc = NULL;
137551df0accSAmit Shah 	port->async_queue = NULL;
137651df0accSAmit Shah 
13774b0a2c5fSPankaj Gupta 	port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
137851df0accSAmit Shah 	port->cons.vtermno = 0;
137951df0accSAmit Shah 
138017e5b4f2SAmit Shah 	port->host_connected = port->guest_connected = false;
138151df0accSAmit Shah 	port->stats = (struct port_stats) { 0 };
138251df0accSAmit Shah 
138351df0accSAmit Shah 	port->outvq_full = false;
138451df0accSAmit Shah 
138551df0accSAmit Shah 	port->in_vq = portdev->in_vqs[port->id];
138651df0accSAmit Shah 	port->out_vq = portdev->out_vqs[port->id];
138751df0accSAmit Shah 
138851df0accSAmit Shah 	port->cdev = cdev_alloc();
138951df0accSAmit Shah 	if (!port->cdev) {
139051df0accSAmit Shah 		dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
139151df0accSAmit Shah 		err = -ENOMEM;
139251df0accSAmit Shah 		goto free_port;
139351df0accSAmit Shah 	}
139451df0accSAmit Shah 	port->cdev->ops = &port_fops;
139551df0accSAmit Shah 
139651df0accSAmit Shah 	devt = MKDEV(portdev->chr_major, id);
139751df0accSAmit Shah 	err = cdev_add(port->cdev, devt, 1);
139851df0accSAmit Shah 	if (err < 0) {
139951df0accSAmit Shah 		dev_err(&port->portdev->vdev->dev,
140051df0accSAmit Shah 			"Error %d adding cdev for port %u\n", err, id);
140151df0accSAmit Shah 		goto free_cdev;
140251df0accSAmit Shah 	}
140351df0accSAmit Shah 	port->dev = device_create(&port_class, &port->portdev->vdev->dev,
1404dc18f080SSjur Brændeland 				  devt, port, "vport%up%u",
140551df0accSAmit Shah 				  port->portdev->vdev->index, id);
140651df0accSAmit Shah 	if (IS_ERR(port->dev)) {
140751df0accSAmit Shah 		err = PTR_ERR(port->dev);
140851df0accSAmit Shah 		dev_err(&port->portdev->vdev->dev,
140951df0accSAmit Shah 			"Error %d creating device for port %u\n",
141051df0accSAmit Shah 			err, id);
141151df0accSAmit Shah 		goto free_cdev;
141251df0accSAmit Shah 	}
141351df0accSAmit Shah 
141451df0accSAmit Shah 	spin_lock_init(&port->inbuf_lock);
141551df0accSAmit Shah 	spin_lock_init(&port->outvq_lock);
141651df0accSAmit Shah 	init_waitqueue_head(&port->waitqueue);
1417d791cfcbSLaurent Vivier 
1418d791cfcbSLaurent Vivier 	/* We can safely ignore ENOSPC because it means
1419d791cfcbSLaurent Vivier 	 * the queue already has buffers. Buffers are removed
1420d791cfcbSLaurent Vivier 	 * only by virtcons_remove(), not by unplug_port()
1421d791cfcbSLaurent Vivier 	 */
1422d791cfcbSLaurent Vivier 	err = fill_queue(port->in_vq, &port->inbuf_lock);
142351df0accSAmit Shah 	if (err < 0 && err != -ENOSPC) {
142451df0accSAmit Shah 		dev_err(port->dev, "Error allocating inbufs\n");
142551df0accSAmit Shah 		goto free_device;
142651df0accSAmit Shah 	}
14271b637046SSjur Brændeland 
142851df0accSAmit Shah 	if (is_rproc_serial(port->portdev->vdev))
14291b637046SSjur Brændeland 		/*
14301b637046SSjur Brændeland 		 * For rproc_serial assume remote processor is connected.
14311b637046SSjur Brændeland 		 * rproc_serial does not want the console port, only
143251df0accSAmit Shah 		 * the generic port implementation.
1433aabd6a8fSSjur Brændeland 		 */
14341b637046SSjur Brændeland 		port->host_connected = true;
14351b637046SSjur Brændeland 	else if (!use_multiport(port->portdev)) {
14361b637046SSjur Brændeland 		/*
14371b637046SSjur Brændeland 		 * If we're not using multiport support,
14381b637046SSjur Brændeland 		 * this has to be a console port.
143951df0accSAmit Shah 		 */
144051df0accSAmit Shah 		err = init_port_console(port);
144151df0accSAmit Shah 		if (err)
144251df0accSAmit Shah 			goto free_inbufs;
144351df0accSAmit Shah 	}
144451df0accSAmit Shah 
144551df0accSAmit Shah 	spin_lock_irq(&portdev->ports_lock);
144651df0accSAmit Shah 	list_add_tail(&port->list, &port->portdev->ports);
144751df0accSAmit Shah 	spin_unlock_irq(&portdev->ports_lock);
144851df0accSAmit Shah 
144951df0accSAmit Shah 	/*
145051df0accSAmit Shah 	 * Tell the Host we're set so that it can send us various
145151df0accSAmit Shah 	 * configuration parameters for this port (eg, port name,
145251df0accSAmit Shah 	 * caching, whether this is a console port, etc.)
145351df0accSAmit Shah 	 */
145451df0accSAmit Shah 	send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
145551df0accSAmit Shah 
145651df0accSAmit Shah 	/*
145751df0accSAmit Shah 	 * Finally, create the debugfs file that we can use to
145851df0accSAmit Shah 	 * inspect a port's state at any time
1459db170068SDan Carpenter 	 */
1460dc18f080SSjur Brændeland 	snprintf(debugfs_name, sizeof(debugfs_name), "vport%up%u",
146151df0accSAmit Shah 		 port->portdev->vdev->index, id);
146251df0accSAmit Shah 	port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1463fb11de92SGreg Kroah-Hartman 						 pdrvdata.debugfs_dir,
146451df0accSAmit Shah 						 port, &port_debugfs_fops);
146551df0accSAmit Shah 	return 0;
146651df0accSAmit Shah 
146751df0accSAmit Shah free_inbufs:
146851df0accSAmit Shah free_device:
146951df0accSAmit Shah 	device_destroy(&port_class, port->dev->devt);
147051df0accSAmit Shah free_cdev:
147151df0accSAmit Shah 	cdev_del(port->cdev);
147251df0accSAmit Shah free_port:
147351df0accSAmit Shah 	kfree(port);
147451df0accSAmit Shah fail:
147551df0accSAmit Shah 	/* The host might want to notify management sw about port add failure */
147651df0accSAmit Shah 	__send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
147751df0accSAmit Shah 	return err;
147851df0accSAmit Shah }
147951df0accSAmit Shah 
148051df0accSAmit Shah /* No users remain, remove all port-specific data. */
remove_port(struct kref * kref)148151df0accSAmit Shah static void remove_port(struct kref *kref)
148251df0accSAmit Shah {
148351df0accSAmit Shah 	struct port *port;
148451df0accSAmit Shah 
148551df0accSAmit Shah 	port = container_of(kref, struct port, kref);
148651df0accSAmit Shah 
148751df0accSAmit Shah 	kfree(port);
148851df0accSAmit Shah }
1489a0e2dbfcSAmit Shah 
remove_port_data(struct port * port)1490a0e2dbfcSAmit Shah static void remove_port_data(struct port *port)
1491c6017e79SAmit Shah {
1492a0e2dbfcSAmit Shah 	spin_lock_irq(&port->inbuf_lock);
1493a0e2dbfcSAmit Shah 	/* Remove unused data this port might have received. */
149434563769SMatt Redfearn 	discard_port_data(port);
1495a0e2dbfcSAmit Shah 	spin_unlock_irq(&port->inbuf_lock);
1496c6017e79SAmit Shah 
1497c6017e79SAmit Shah 	spin_lock_irq(&port->outvq_lock);
149834563769SMatt Redfearn 	reclaim_consumed_buffers(port);
1499a0e2dbfcSAmit Shah 	spin_unlock_irq(&port->outvq_lock);
1500a0e2dbfcSAmit Shah }
150151df0accSAmit Shah 
150251df0accSAmit Shah /*
150351df0accSAmit Shah  * Port got unplugged.  Remove port from portdev's list and drop the
150451df0accSAmit Shah  * kref reference.  If no userspace has this port opened, it will
150551df0accSAmit Shah  * result in immediate removal the port.
150651df0accSAmit Shah  */
unplug_port(struct port * port)150751df0accSAmit Shah static void unplug_port(struct port *port)
150851df0accSAmit Shah {
150951df0accSAmit Shah 	spin_lock_irq(&port->portdev->ports_lock);
151051df0accSAmit Shah 	list_del(&port->list);
151151df0accSAmit Shah 	spin_unlock_irq(&port->portdev->ports_lock);
15125549fb25SAmit Shah 
151351df0accSAmit Shah 	spin_lock_irq(&port->inbuf_lock);
151451df0accSAmit Shah 	if (port->guest_connected) {
151551df0accSAmit Shah 		/* Let the app know the port is going down. */
151692d34538SAmit Shah 		send_sigio_to_port(port);
151792d34538SAmit Shah 
151892d34538SAmit Shah 		/* Do this after sigio is actually sent */
151992d34538SAmit Shah 		port->guest_connected = false;
152092d34538SAmit Shah 		port->host_connected = false;
152192d34538SAmit Shah 
152251df0accSAmit Shah 		wake_up_interruptible(&port->waitqueue);
15235549fb25SAmit Shah 	}
152451df0accSAmit Shah 	spin_unlock_irq(&port->inbuf_lock);
152551df0accSAmit Shah 
152651df0accSAmit Shah 	if (is_console_port(port)) {
152751df0accSAmit Shah 		spin_lock_irq(&pdrvdata_lock);
152851df0accSAmit Shah 		list_del(&port->cons.list);
152951df0accSAmit Shah 		spin_unlock_irq(&pdrvdata_lock);
1530e6278a54SCédric Le Goater 		hvc_remove(port->cons.hvc);
153151df0accSAmit Shah 		ida_free(&vtermno_ida, port->cons.vtermno);
153251df0accSAmit Shah 	}
1533a0e2dbfcSAmit Shah 
153451df0accSAmit Shah 	remove_port_data(port);
153551df0accSAmit Shah 
153651df0accSAmit Shah 	/*
153751df0accSAmit Shah 	 * We should just assume the device itself has gone off --
153851df0accSAmit Shah 	 * else a close on an open port later will try to send out a
153951df0accSAmit Shah 	 * control message.
154051df0accSAmit Shah 	 */
154151df0accSAmit Shah 	port->portdev = NULL;
1542ea3768b4SAmit Shah 
1543ea3768b4SAmit Shah 	sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1544ea3768b4SAmit Shah 	device_destroy(&port_class, port->dev->devt);
1545ea3768b4SAmit Shah 	cdev_del(port->cdev);
1546ea3768b4SAmit Shah 
15473b868a40SAmit Shah 	debugfs_remove(port->debugfs_file);
1548ea3768b4SAmit Shah 	kfree(port->name);
154951df0accSAmit Shah 
155051df0accSAmit Shah 	/*
155151df0accSAmit Shah 	 * Locks around here are not necessary - a port can't be
155251df0accSAmit Shah 	 * opened after we removed the port struct from ports_list
155351df0accSAmit Shah 	 * above.
155451df0accSAmit Shah 	 */
155551df0accSAmit Shah 	kref_put(&port->kref, remove_port);
155651df0accSAmit Shah }
155751df0accSAmit Shah 
15581f0f9106SMichael S. Tsirkin /* Any private messages that the Host and Guest want to share */
handle_control_message(struct virtio_device * vdev,struct ports_device * portdev,struct port_buffer * buf)15591f0f9106SMichael S. Tsirkin static void handle_control_message(struct virtio_device *vdev,
156051df0accSAmit Shah 				   struct ports_device *portdev,
156151df0accSAmit Shah 				   struct port_buffer *buf)
156251df0accSAmit Shah {
156351df0accSAmit Shah 	struct virtio_console_control *cpkt;
156451df0accSAmit Shah 	struct port *port;
156551df0accSAmit Shah 	size_t name_size;
156651df0accSAmit Shah 	int err;
156751df0accSAmit Shah 
156851df0accSAmit Shah 	cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
15691f0f9106SMichael S. Tsirkin 
15701f0f9106SMichael S. Tsirkin 	port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
15711f0f9106SMichael S. Tsirkin 	if (!port &&
157251df0accSAmit Shah 	    cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
157351df0accSAmit Shah 		/* No valid header at start of buffer.  Drop it. */
157451df0accSAmit Shah 		dev_dbg(&portdev->vdev->dev,
157551df0accSAmit Shah 			"Invalid index %u in control packet\n", cpkt->id);
157651df0accSAmit Shah 		return;
157751df0accSAmit Shah 	}
15781f0f9106SMichael S. Tsirkin 
157951df0accSAmit Shah 	switch (virtio16_to_cpu(vdev, cpkt->event)) {
158051df0accSAmit Shah 	case VIRTIO_CONSOLE_PORT_ADD:
158151df0accSAmit Shah 		if (port) {
158251df0accSAmit Shah 			dev_dbg(&portdev->vdev->dev,
158351df0accSAmit Shah 				"Port %u already added\n", port->id);
158451df0accSAmit Shah 			send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
158551df0accSAmit Shah 			break;
15861f0f9106SMichael S. Tsirkin 		}
15877328fa64SMichael S. Tsirkin 		if (virtio32_to_cpu(vdev, cpkt->id) >=
158851df0accSAmit Shah 		    portdev->max_nr_ports) {
15891f0f9106SMichael S. Tsirkin 			dev_warn(&portdev->vdev->dev,
15901f0f9106SMichael S. Tsirkin 				"Request for adding port with "
15917328fa64SMichael S. Tsirkin 				"out-of-bound id %u, max. supported id: %u\n",
159251df0accSAmit Shah 				cpkt->id, portdev->max_nr_ports - 1);
159351df0accSAmit Shah 			break;
15941f0f9106SMichael S. Tsirkin 		}
159551df0accSAmit Shah 		add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
159651df0accSAmit Shah 		break;
159751df0accSAmit Shah 	case VIRTIO_CONSOLE_PORT_REMOVE:
159851df0accSAmit Shah 		unplug_port(port);
159951df0accSAmit Shah 		break;
160051df0accSAmit Shah 	case VIRTIO_CONSOLE_CONSOLE_PORT:
160151df0accSAmit Shah 		if (!cpkt->value)
160251df0accSAmit Shah 			break;
160351df0accSAmit Shah 		if (is_console_port(port))
160451df0accSAmit Shah 			break;
160551df0accSAmit Shah 
16065e38483bSChristian Borntraeger 		init_port_console(port);
160751df0accSAmit Shah 		complete(&early_console_added);
160851df0accSAmit Shah 		/*
160951df0accSAmit Shah 		 * Could remove the port here in case init fails - but
161051df0accSAmit Shah 		 * have to notify the host first.
161151df0accSAmit Shah 		 */
161251df0accSAmit Shah 		break;
161351df0accSAmit Shah 	case VIRTIO_CONSOLE_RESIZE: {
161451df0accSAmit Shah 		struct {
161551df0accSAmit Shah 			__u16 rows;
161651df0accSAmit Shah 			__u16 cols;
161751df0accSAmit Shah 		} size;
161851df0accSAmit Shah 
161951df0accSAmit Shah 		if (!is_console_port(port))
162051df0accSAmit Shah 			break;
162151df0accSAmit Shah 
162251df0accSAmit Shah 		memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
162351df0accSAmit Shah 		       sizeof(size));
162451df0accSAmit Shah 		set_console_size(port, size.rows, size.cols);
162551df0accSAmit Shah 
162651df0accSAmit Shah 		port->cons.hvc->irq_requested = 1;
162751df0accSAmit Shah 		resize_console(port);
162851df0accSAmit Shah 		break;
162951df0accSAmit Shah 	}
16301f0f9106SMichael S. Tsirkin 	case VIRTIO_CONSOLE_PORT_OPEN:
163151df0accSAmit Shah 		port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
163251df0accSAmit Shah 		wake_up_interruptible(&port->waitqueue);
163351df0accSAmit Shah 		/*
163451df0accSAmit Shah 		 * If the host port got closed and the host had any
163551df0accSAmit Shah 		 * unconsumed buffers, we'll be able to reclaim them
163651df0accSAmit Shah 		 * now.
163751df0accSAmit Shah 		 */
163851df0accSAmit Shah 		spin_lock_irq(&port->outvq_lock);
163951df0accSAmit Shah 		reclaim_consumed_buffers(port);
164051df0accSAmit Shah 		spin_unlock_irq(&port->outvq_lock);
164151df0accSAmit Shah 
164251df0accSAmit Shah 		/*
164351df0accSAmit Shah 		 * If the guest is connected, it'll be interested in
164451df0accSAmit Shah 		 * knowing the host connection state changed.
1645314081f1SAmit Shah 		 */
164651df0accSAmit Shah 		spin_lock_irq(&port->inbuf_lock);
1647314081f1SAmit Shah 		send_sigio_to_port(port);
164851df0accSAmit Shah 		spin_unlock_irq(&port->inbuf_lock);
164951df0accSAmit Shah 		break;
165051df0accSAmit Shah 	case VIRTIO_CONSOLE_PORT_NAME:
1651291024efSAmit Shah 		/*
1652291024efSAmit Shah 		 * If we woke up after hibernation, we can get this
1653291024efSAmit Shah 		 * again.  Skip it in that case.
1654291024efSAmit Shah 		 */
1655291024efSAmit Shah 		if (port->name)
1656291024efSAmit Shah 			break;
1657291024efSAmit Shah 
165851df0accSAmit Shah 		/*
165951df0accSAmit Shah 		 * Skip the size of the header and the cpkt to get the size
166051df0accSAmit Shah 		 * of the name that was sent
166151df0accSAmit Shah 		 */
166251df0accSAmit Shah 		name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
166351df0accSAmit Shah 
166451df0accSAmit Shah 		port->name = kmalloc(name_size, GFP_KERNEL);
166551df0accSAmit Shah 		if (!port->name) {
166651df0accSAmit Shah 			dev_err(port->dev,
166751df0accSAmit Shah 				"Not enough space to store port name\n");
166851df0accSAmit Shah 			break;
1669c7109c72SBo Liu 		}
1670c7109c72SBo Liu 		strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
167151df0accSAmit Shah 			name_size);
167251df0accSAmit Shah 
167351df0accSAmit Shah 		/*
167451df0accSAmit Shah 		 * Since we only have one sysfs attribute, 'name',
167551df0accSAmit Shah 		 * create it only if we have a name for the port.
167651df0accSAmit Shah 		 */
167751df0accSAmit Shah 		err = sysfs_create_group(&port->dev->kobj,
167851df0accSAmit Shah 					 &port_attribute_group);
167951df0accSAmit Shah 		if (err) {
168051df0accSAmit Shah 			dev_err(port->dev,
168151df0accSAmit Shah 				"Error %d creating sysfs device attributes\n",
168251df0accSAmit Shah 				err);
168351df0accSAmit Shah 		} else {
168451df0accSAmit Shah 			/*
168551df0accSAmit Shah 			 * Generate a udev event so that appropriate
168651df0accSAmit Shah 			 * symlinks can be created based on udev
168751df0accSAmit Shah 			 * rules.
168851df0accSAmit Shah 			 */
168951df0accSAmit Shah 			kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
169051df0accSAmit Shah 		}
169151df0accSAmit Shah 		break;
169251df0accSAmit Shah 	}
169351df0accSAmit Shah }
169451df0accSAmit Shah 
control_work_handler(struct work_struct * work)169551df0accSAmit Shah static void control_work_handler(struct work_struct *work)
169651df0accSAmit Shah {
169751df0accSAmit Shah 	struct ports_device *portdev;
169851df0accSAmit Shah 	struct virtqueue *vq;
169951df0accSAmit Shah 	struct port_buffer *buf;
170051df0accSAmit Shah 	unsigned int len;
170151df0accSAmit Shah 
170251df0accSAmit Shah 	portdev = container_of(work, struct ports_device, control_work);
170351df0accSAmit Shah 	vq = portdev->c_ivq;
1704165b1b8bSAmit Shah 
170551df0accSAmit Shah 	spin_lock(&portdev->c_ivq_lock);
1706165b1b8bSAmit Shah 	while ((buf = virtqueue_get_buf(vq, &len))) {
170751df0accSAmit Shah 		spin_unlock(&portdev->c_ivq_lock);
1708d00d8da5SXie Yongji 
170951df0accSAmit Shah 		buf->len = min_t(size_t, len, buf->size);
171051df0accSAmit Shah 		buf->offset = 0;
17111f0f9106SMichael S. Tsirkin 
171251df0accSAmit Shah 		handle_control_message(vq->vdev, portdev, buf);
1713165b1b8bSAmit Shah 
171451df0accSAmit Shah 		spin_lock(&portdev->c_ivq_lock);
171551df0accSAmit Shah 		if (add_inbuf(portdev->c_ivq, buf) < 0) {
171651df0accSAmit Shah 			dev_warn(&portdev->vdev->dev,
17171b637046SSjur Brændeland 				 "Error adding buffer to queue\n");
171851df0accSAmit Shah 			free_buf(buf, false);
171951df0accSAmit Shah 		}
1720165b1b8bSAmit Shah 	}
172151df0accSAmit Shah 	spin_unlock(&portdev->c_ivq_lock);
172251df0accSAmit Shah }
1723a7a69ec0SMichael S. Tsirkin 
flush_bufs(struct virtqueue * vq,bool can_sleep)1724a7a69ec0SMichael S. Tsirkin static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1725a7a69ec0SMichael S. Tsirkin {
1726a7a69ec0SMichael S. Tsirkin 	struct port_buffer *buf;
1727a7a69ec0SMichael S. Tsirkin 	unsigned int len;
1728a7a69ec0SMichael S. Tsirkin 
1729a7a69ec0SMichael S. Tsirkin 	while ((buf = virtqueue_get_buf(vq, &len)))
1730a7a69ec0SMichael S. Tsirkin 		free_buf(buf, can_sleep);
1731a7a69ec0SMichael S. Tsirkin }
1732ce86d35dSLinus Torvalds 
out_intr(struct virtqueue * vq)1733ce86d35dSLinus Torvalds static void out_intr(struct virtqueue *vq)
1734ce86d35dSLinus Torvalds {
1735ce86d35dSLinus Torvalds 	struct port *port;
1736ce86d35dSLinus Torvalds 
1737a7a69ec0SMichael S. Tsirkin 	port = find_port_by_vq(vq->vdev->priv, vq);
1738a7a69ec0SMichael S. Tsirkin 	if (!port) {
1739ce86d35dSLinus Torvalds 		flush_bufs(vq, false);
1740a7a69ec0SMichael S. Tsirkin 		return;
1741ce86d35dSLinus Torvalds 	}
1742ce86d35dSLinus Torvalds 
1743ce86d35dSLinus Torvalds 	wake_up_interruptible(&port->waitqueue);
1744ce86d35dSLinus Torvalds }
174551df0accSAmit Shah 
in_intr(struct virtqueue * vq)174651df0accSAmit Shah static void in_intr(struct virtqueue *vq)
174751df0accSAmit Shah {
174851df0accSAmit Shah 	struct port *port;
174951df0accSAmit Shah 	unsigned long flags;
175051df0accSAmit Shah 
1751a7a69ec0SMichael S. Tsirkin 	port = find_port_by_vq(vq->vdev->priv, vq);
1752a7a69ec0SMichael S. Tsirkin 	if (!port) {
175351df0accSAmit Shah 		flush_bufs(vq, false);
1754a7a69ec0SMichael S. Tsirkin 		return;
175551df0accSAmit Shah 	}
175651df0accSAmit Shah 
175751df0accSAmit Shah 	spin_lock_irqsave(&port->inbuf_lock, flags);
175851df0accSAmit Shah 	port->inbuf = get_inbuf(port);
175951df0accSAmit Shah 
1760aabd6a8fSSjur Brændeland 	/*
1761aabd6a8fSSjur Brændeland 	 * Normally the port should not accept data when the port is
1762aabd6a8fSSjur Brændeland 	 * closed. For generic serial ports, the host won't (shouldn't)
176351df0accSAmit Shah 	 * send data till the guest is connected. But this condition
1764aabd6a8fSSjur Brændeland 	 * can be reached when a console port is not yet connected (no
1765aabd6a8fSSjur Brændeland 	 * tty is spawned) and the other side sends out data over the
1766aabd6a8fSSjur Brændeland 	 * vring, or when a remote devices start sending data before
1767aabd6a8fSSjur Brændeland 	 * the ports are opened.
1768aabd6a8fSSjur Brændeland 	 *
1769aabd6a8fSSjur Brændeland 	 * A generic serial port will discard data if not connected,
1770aabd6a8fSSjur Brændeland 	 * while console ports and rproc-serial ports accepts data at
1771aabd6a8fSSjur Brændeland 	 * any time. rproc-serial is initiated with guest_connected to
1772aabd6a8fSSjur Brændeland 	 * false because port_fops_open expects this. Console ports are
1773aabd6a8fSSjur Brændeland 	 * hooked up with an HVC console and is initialized with
177451df0accSAmit Shah 	 * guest_connected to true.
1775aabd6a8fSSjur Brændeland 	 */
1776aabd6a8fSSjur Brændeland 
177751df0accSAmit Shah 	if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
177851df0accSAmit Shah 		discard_port_data(port);
1779314081f1SAmit Shah 
1780314081f1SAmit Shah 	/* Send a SIGIO indicating new data in case the process asked for it */
1781314081f1SAmit Shah 	send_sigio_to_port(port);
178251df0accSAmit Shah 
178351df0accSAmit Shah 	spin_unlock_irqrestore(&port->inbuf_lock, flags);
178451df0accSAmit Shah 
178551df0accSAmit Shah 	wake_up_interruptible(&port->waitqueue);
178651df0accSAmit Shah 
178751df0accSAmit Shah 	if (is_console_port(port) && hvc_poll(port->cons.hvc))
178851df0accSAmit Shah 		hvc_kick();
178951df0accSAmit Shah }
179051df0accSAmit Shah 
control_intr(struct virtqueue * vq)179151df0accSAmit Shah static void control_intr(struct virtqueue *vq)
179251df0accSAmit Shah {
179351df0accSAmit Shah 	struct ports_device *portdev;
179451df0accSAmit Shah 
179551df0accSAmit Shah 	portdev = vq->vdev->priv;
179651df0accSAmit Shah 	schedule_work(&portdev->control_work);
179751df0accSAmit Shah }
179851df0accSAmit Shah 
config_intr(struct virtio_device * vdev)179951df0accSAmit Shah static void config_intr(struct virtio_device *vdev)
180051df0accSAmit Shah {
180151df0accSAmit Shah 	struct ports_device *portdev;
180251df0accSAmit Shah 
180351df0accSAmit Shah 	portdev = vdev->priv;
1804eeb8a7e8SMichael S. Tsirkin 
1805eeb8a7e8SMichael S. Tsirkin 	if (!use_multiport(portdev))
1806eeb8a7e8SMichael S. Tsirkin 		schedule_work(&portdev->config_work);
1807eeb8a7e8SMichael S. Tsirkin }
1808eeb8a7e8SMichael S. Tsirkin 
config_work_handler(struct work_struct * work)1809eeb8a7e8SMichael S. Tsirkin static void config_work_handler(struct work_struct *work)
1810eeb8a7e8SMichael S. Tsirkin {
1811eeb8a7e8SMichael S. Tsirkin 	struct ports_device *portdev;
18128379cadfSG. Campana 
181351df0accSAmit Shah 	portdev = container_of(work, struct ports_device, config_work);
1814eeb8a7e8SMichael S. Tsirkin 	if (!use_multiport(portdev)) {
181551df0accSAmit Shah 		struct virtio_device *vdev;
181651df0accSAmit Shah 		struct port *port;
181751df0accSAmit Shah 		u16 rows, cols;
1818eeb8a7e8SMichael S. Tsirkin 
1819855e0c52SRusty Russell 		vdev = portdev->vdev;
1820855e0c52SRusty Russell 		virtio_cread(vdev, struct virtio_console_config, cols, &cols);
182151df0accSAmit Shah 		virtio_cread(vdev, struct virtio_console_config, rows, &rows);
182251df0accSAmit Shah 
182351df0accSAmit Shah 		port = find_port_by_id(portdev, 0);
182451df0accSAmit Shah 		set_console_size(port, rows, cols);
182551df0accSAmit Shah 
182651df0accSAmit Shah 		/*
182751df0accSAmit Shah 		 * We'll use this way of resizing only for legacy
182851df0accSAmit Shah 		 * support.  For newer userspace
182951df0accSAmit Shah 		 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
183051df0accSAmit Shah 		 * to indicate console size changes so that it can be
183151df0accSAmit Shah 		 * done per-port.
183251df0accSAmit Shah 		 */
183351df0accSAmit Shah 		resize_console(port);
183451df0accSAmit Shah 	}
183551df0accSAmit Shah }
183651df0accSAmit Shah 
init_vqs(struct ports_device * portdev)183751df0accSAmit Shah static int init_vqs(struct ports_device *portdev)
183851df0accSAmit Shah {
183951df0accSAmit Shah 	vq_callback_t **io_callbacks;
184051df0accSAmit Shah 	char **io_names;
184151df0accSAmit Shah 	struct virtqueue **vqs;
184251df0accSAmit Shah 	u32 i, j, nr_ports, nr_queues;
184351df0accSAmit Shah 	int err;
18447328fa64SMichael S. Tsirkin 
184551df0accSAmit Shah 	nr_ports = portdev->max_nr_ports;
184651df0accSAmit Shah 	nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
18476da2ec56SKees Cook 
18486da2ec56SKees Cook 	vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
184951df0accSAmit Shah 	io_callbacks = kmalloc_array(nr_queues, sizeof(vq_callback_t *),
18506da2ec56SKees Cook 				     GFP_KERNEL);
18516da2ec56SKees Cook 	io_names = kmalloc_array(nr_queues, sizeof(char *), GFP_KERNEL);
18526da2ec56SKees Cook 	portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
18536da2ec56SKees Cook 					GFP_KERNEL);
185451df0accSAmit Shah 	portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
185551df0accSAmit Shah 					 GFP_KERNEL);
185651df0accSAmit Shah 	if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
185751df0accSAmit Shah 	    !portdev->out_vqs) {
185851df0accSAmit Shah 		err = -ENOMEM;
185951df0accSAmit Shah 		goto free;
186051df0accSAmit Shah 	}
186151df0accSAmit Shah 
186251df0accSAmit Shah 	/*
186351df0accSAmit Shah 	 * For backward compat (newer host but older guest), the host
186451df0accSAmit Shah 	 * spawns a console port first and also inits the vqs for port
186551df0accSAmit Shah 	 * 0 before others.
186651df0accSAmit Shah 	 */
186751df0accSAmit Shah 	j = 0;
1868ce86d35dSLinus Torvalds 	io_callbacks[j] = in_intr;
186951df0accSAmit Shah 	io_callbacks[j + 1] = out_intr;
187051df0accSAmit Shah 	io_names[j] = "input";
187151df0accSAmit Shah 	io_names[j + 1] = "output";
187251df0accSAmit Shah 	j += 2;
187351df0accSAmit Shah 
187451df0accSAmit Shah 	if (use_multiport(portdev)) {
187551df0accSAmit Shah 		io_callbacks[j] = control_intr;
187651df0accSAmit Shah 		io_callbacks[j + 1] = NULL;
187751df0accSAmit Shah 		io_names[j] = "control-i";
187851df0accSAmit Shah 		io_names[j + 1] = "control-o";
187951df0accSAmit Shah 
188051df0accSAmit Shah 		for (i = 1; i < nr_ports; i++) {
188151df0accSAmit Shah 			j += 2;
1882ce86d35dSLinus Torvalds 			io_callbacks[j] = in_intr;
188351df0accSAmit Shah 			io_callbacks[j + 1] = out_intr;
188451df0accSAmit Shah 			io_names[j] = "input";
188551df0accSAmit Shah 			io_names[j + 1] = "output";
188651df0accSAmit Shah 		}
188751df0accSAmit Shah 	}
18889b2bbdb2SMichael S. Tsirkin 	/* Find the queues. */
188951df0accSAmit Shah 	err = virtio_find_vqs(portdev->vdev, nr_queues, vqs,
1890fb5e31d9SChristoph Hellwig 			      io_callbacks,
189151df0accSAmit Shah 			      (const char **)io_names, NULL);
189251df0accSAmit Shah 	if (err)
189351df0accSAmit Shah 		goto free;
189451df0accSAmit Shah 
189551df0accSAmit Shah 	j = 0;
189651df0accSAmit Shah 	portdev->in_vqs[0] = vqs[0];
189751df0accSAmit Shah 	portdev->out_vqs[0] = vqs[1];
189851df0accSAmit Shah 	j += 2;
189951df0accSAmit Shah 	if (use_multiport(portdev)) {
190051df0accSAmit Shah 		portdev->c_ivq = vqs[j];
190151df0accSAmit Shah 		portdev->c_ovq = vqs[j + 1];
190251df0accSAmit Shah 
190351df0accSAmit Shah 		for (i = 1; i < nr_ports; i++) {
190451df0accSAmit Shah 			j += 2;
190551df0accSAmit Shah 			portdev->in_vqs[i] = vqs[j];
190651df0accSAmit Shah 			portdev->out_vqs[i] = vqs[j + 1];
190751df0accSAmit Shah 		}
190851df0accSAmit Shah 	}
190951df0accSAmit Shah 	kfree(io_names);
191051df0accSAmit Shah 	kfree(io_callbacks);
191151df0accSAmit Shah 	kfree(vqs);
191251df0accSAmit Shah 
191351df0accSAmit Shah 	return 0;
191451df0accSAmit Shah 
191551df0accSAmit Shah free:
191651df0accSAmit Shah 	kfree(portdev->out_vqs);
191751df0accSAmit Shah 	kfree(portdev->in_vqs);
191851df0accSAmit Shah 	kfree(io_names);
191951df0accSAmit Shah 	kfree(io_callbacks);
192051df0accSAmit Shah 	kfree(vqs);
192151df0accSAmit Shah 
192251df0accSAmit Shah 	return err;
192351df0accSAmit Shah }
192451df0accSAmit Shah 
192551df0accSAmit Shah static const struct file_operations portdev_fops = {
192651df0accSAmit Shah 	.owner = THIS_MODULE,
192751df0accSAmit Shah };
1928a0e2dbfcSAmit Shah 
remove_vqs(struct ports_device * portdev)1929a0e2dbfcSAmit Shah static void remove_vqs(struct ports_device *portdev)
1930a7a69ec0SMichael S. Tsirkin {
1931a7a69ec0SMichael S. Tsirkin 	struct virtqueue *vq;
1932a7a69ec0SMichael S. Tsirkin 
1933a7a69ec0SMichael S. Tsirkin 	virtio_device_for_each_vq(portdev->vdev, vq) {
1934a7a69ec0SMichael S. Tsirkin 		struct port_buffer *buf;
1935a7a69ec0SMichael S. Tsirkin 
1936a7a69ec0SMichael S. Tsirkin 		flush_bufs(vq, true);
1937a7a69ec0SMichael S. Tsirkin 		while ((buf = virtqueue_detach_unused_buf(vq)))
1938*56b5e65eSXianting Tian 			free_buf(buf, true);
1939a7a69ec0SMichael S. Tsirkin 		cond_resched();
1940a0e2dbfcSAmit Shah 	}
1941a0e2dbfcSAmit Shah 	portdev->vdev->config->del_vqs(portdev->vdev);
1942a0e2dbfcSAmit Shah 	kfree(portdev->in_vqs);
1943a0e2dbfcSAmit Shah 	kfree(portdev->out_vqs);
1944a0e2dbfcSAmit Shah }
1945aa44ec86SMichael S. Tsirkin 
virtcons_remove(struct virtio_device * vdev)1946aa44ec86SMichael S. Tsirkin static void virtcons_remove(struct virtio_device *vdev)
1947aa44ec86SMichael S. Tsirkin {
1948aa44ec86SMichael S. Tsirkin 	struct ports_device *portdev;
1949aa44ec86SMichael S. Tsirkin 	struct port *port, *port2;
1950aa44ec86SMichael S. Tsirkin 
1951aa44ec86SMichael S. Tsirkin 	portdev = vdev->priv;
1952aa44ec86SMichael S. Tsirkin 
1953aa44ec86SMichael S. Tsirkin 	spin_lock_irq(&pdrvdata_lock);
1954aa44ec86SMichael S. Tsirkin 	list_del(&portdev->list);
1955aa44ec86SMichael S. Tsirkin 	spin_unlock_irq(&pdrvdata_lock);
19560e7174b9SMichael S. Tsirkin 
19570e7174b9SMichael S. Tsirkin 	/* Device is going away, exit any polling for buffers */
19580e7174b9SMichael S. Tsirkin 	virtio_break_device(vdev);
19590e7174b9SMichael S. Tsirkin 	if (use_multiport(portdev))
19600e7174b9SMichael S. Tsirkin 		flush_work(&portdev->control_work);
19610e7174b9SMichael S. Tsirkin 	else
19620e7174b9SMichael S. Tsirkin 		flush_work(&portdev->config_work);
1963aa44ec86SMichael S. Tsirkin 
1964d9679d00SMichael S. Tsirkin 	/* Disable interrupts for vqs */
1965aa44ec86SMichael S. Tsirkin 	virtio_reset_device(vdev);
1966aa44ec86SMichael S. Tsirkin 	/* Finish up work that's lined up */
1967aa44ec86SMichael S. Tsirkin 	if (use_multiport(portdev))
1968aa44ec86SMichael S. Tsirkin 		cancel_work_sync(&portdev->control_work);
1969aa44ec86SMichael S. Tsirkin 	else
1970aa44ec86SMichael S. Tsirkin 		cancel_work_sync(&portdev->config_work);
1971aa44ec86SMichael S. Tsirkin 
1972aa44ec86SMichael S. Tsirkin 	list_for_each_entry_safe(port, port2, &portdev->ports, list)
1973aa44ec86SMichael S. Tsirkin 		unplug_port(port);
1974aa44ec86SMichael S. Tsirkin 
1975aa44ec86SMichael S. Tsirkin 	unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1976aa44ec86SMichael S. Tsirkin 
1977aa44ec86SMichael S. Tsirkin 	/*
1978aa44ec86SMichael S. Tsirkin 	 * When yanking out a device, we immediately lose the
1979aa44ec86SMichael S. Tsirkin 	 * (device-side) queues.  So there's no point in keeping the
1980aa44ec86SMichael S. Tsirkin 	 * guest side around till we drop our final reference.  This
1981aa44ec86SMichael S. Tsirkin 	 * also means that any ports which are in an open state will
1982aa44ec86SMichael S. Tsirkin 	 * have to just stop using the port, as the vqs are going
1983aa44ec86SMichael S. Tsirkin 	 * away.
1984aa44ec86SMichael S. Tsirkin 	 */
1985aa44ec86SMichael S. Tsirkin 	remove_vqs(portdev);
1986aa44ec86SMichael S. Tsirkin 	kfree(portdev);
1987aa44ec86SMichael S. Tsirkin }
198851df0accSAmit Shah 
198951df0accSAmit Shah /*
199051df0accSAmit Shah  * Once we're further in boot, we get probed like any other virtio
199151df0accSAmit Shah  * device.
199251df0accSAmit Shah  *
199351df0accSAmit Shah  * If the host also supports multiple console ports, we check the
199451df0accSAmit Shah  * config space to see how many ports the host has spawned.  We
199551df0accSAmit Shah  * initialize each port found.
19962223cbecSBill Pemberton  */
virtcons_probe(struct virtio_device * vdev)199751df0accSAmit Shah static int virtcons_probe(struct virtio_device *vdev)
199851df0accSAmit Shah {
199951df0accSAmit Shah 	struct ports_device *portdev;
200051df0accSAmit Shah 	int err;
20015e38483bSChristian Borntraeger 	bool multiport;
20025e38483bSChristian Borntraeger 	bool early = early_put_chars != NULL;
2003be8ff595SRusty Russell 
2004be8ff595SRusty Russell 	/* We only need a config space if features are offered */
2005be8ff595SRusty Russell 	if (!vdev->config->get &&
2006be8ff595SRusty Russell 	    (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
2007011f0e7aSMichael S. Tsirkin 	     || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
2008011f0e7aSMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
2009011f0e7aSMichael S. Tsirkin 			__func__);
2010011f0e7aSMichael S. Tsirkin 		return -EINVAL;
2011011f0e7aSMichael S. Tsirkin 	}
20125e38483bSChristian Borntraeger 
20135e38483bSChristian Borntraeger 	/* Ensure to read early_put_chars now */
201451df0accSAmit Shah 	barrier();
201551df0accSAmit Shah 
201651df0accSAmit Shah 	portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
201751df0accSAmit Shah 	if (!portdev) {
201851df0accSAmit Shah 		err = -ENOMEM;
201951df0accSAmit Shah 		goto fail;
202051df0accSAmit Shah 	}
202151df0accSAmit Shah 
202251df0accSAmit Shah 	/* Attach this portdev to this virtio_device, and vice-versa. */
202351df0accSAmit Shah 	portdev->vdev = vdev;
202451df0accSAmit Shah 	vdev->priv = portdev;
202551df0accSAmit Shah 
202651df0accSAmit Shah 	portdev->chr_major = register_chrdev(0, "virtio-portsdev",
202751df0accSAmit Shah 					     &portdev_fops);
202851df0accSAmit Shah 	if (portdev->chr_major < 0) {
202951df0accSAmit Shah 		dev_err(&vdev->dev,
2030dc18f080SSjur Brændeland 			"Error %d registering chrdev for device %u\n",
203151df0accSAmit Shah 			portdev->chr_major, vdev->index);
203251df0accSAmit Shah 		err = portdev->chr_major;
203351df0accSAmit Shah 		goto free;
203451df0accSAmit Shah 	}
203551df0accSAmit Shah 
20367328fa64SMichael S. Tsirkin 	multiport = false;
20371b637046SSjur Brændeland 	portdev->max_nr_ports = 1;
20381b637046SSjur Brændeland 
20391b637046SSjur Brændeland 	/* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
2040855e0c52SRusty Russell 	if (!is_rproc_serial(vdev) &&
2041855e0c52SRusty Russell 	    virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
20427328fa64SMichael S. Tsirkin 				 struct virtio_console_config, max_nr_ports,
204328962ec5SJason Wang 				 &portdev->max_nr_ports) == 0) {
204428962ec5SJason Wang 		if (portdev->max_nr_ports == 0 ||
204528962ec5SJason Wang 		    portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
204628962ec5SJason Wang 			dev_err(&vdev->dev,
204728962ec5SJason Wang 				"Invalidate max_nr_ports %d",
204828962ec5SJason Wang 				portdev->max_nr_ports);
204928962ec5SJason Wang 			err = -EINVAL;
205028962ec5SJason Wang 			goto free;
205151c6d61aSSasha Levin 		}
20521b637046SSjur Brændeland 		multiport = true;
205351df0accSAmit Shah 	}
205451df0accSAmit Shah 
205551df0accSAmit Shah 	err = init_vqs(portdev);
205651df0accSAmit Shah 	if (err < 0) {
205751df0accSAmit Shah 		dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
205851df0accSAmit Shah 		goto free_chrdev;
205951df0accSAmit Shah 	}
206051df0accSAmit Shah 
206151df0accSAmit Shah 	spin_lock_init(&portdev->ports_lock);
20625c60300dSMichael S. Tsirkin 	INIT_LIST_HEAD(&portdev->ports);
206351df0accSAmit Shah 	INIT_LIST_HEAD(&portdev->list);
206465eca3a2SCornelia Huck 
206565eca3a2SCornelia Huck 	virtio_device_ready(portdev->vdev);
2066eeb8a7e8SMichael S. Tsirkin 
20674f6e24edSMichael S. Tsirkin 	INIT_WORK(&portdev->config_work, &config_work_handler);
20684f6e24edSMichael S. Tsirkin 	INIT_WORK(&portdev->control_work, &control_work_handler);
206951df0accSAmit Shah 
2070165b1b8bSAmit Shah 	if (multiport) {
20719ba5c80bSAmit Shah 		spin_lock_init(&portdev->c_ivq_lock);
207251df0accSAmit Shah 		spin_lock_init(&portdev->c_ovq_lock);
2073d791cfcbSLaurent Vivier 
2074d791cfcbSLaurent Vivier 		err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
207551df0accSAmit Shah 		if (err < 0) {
207651df0accSAmit Shah 			dev_err(&vdev->dev,
20775c60300dSMichael S. Tsirkin 				"Error allocating buffers for control queue\n");
20785c60300dSMichael S. Tsirkin 			/*
20795c60300dSMichael S. Tsirkin 			 * The host might want to notify mgmt sw about device
20805c60300dSMichael S. Tsirkin 			 * add failure.
20815c60300dSMichael S. Tsirkin 			 */
20825c60300dSMichael S. Tsirkin 			__send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
20835c60300dSMichael S. Tsirkin 					   VIRTIO_CONSOLE_DEVICE_READY, 0);
20845c60300dSMichael S. Tsirkin 			/* Device was functional: we need full cleanup. */
2085d791cfcbSLaurent Vivier 			virtcons_remove(vdev);
208651df0accSAmit Shah 			return err;
208751df0accSAmit Shah 		}
208851df0accSAmit Shah 	} else {
208951df0accSAmit Shah 		/*
209051df0accSAmit Shah 		 * For backward compatibility: Create a console port
209151df0accSAmit Shah 		 * if we're running on older host.
209251df0accSAmit Shah 		 */
209351df0accSAmit Shah 		add_port(portdev, 0);
209451df0accSAmit Shah 	}
209551df0accSAmit Shah 
209651df0accSAmit Shah 	spin_lock_irq(&pdrvdata_lock);
209751df0accSAmit Shah 	list_add_tail(&portdev->list, &pdrvdata.portdevs);
209851df0accSAmit Shah 	spin_unlock_irq(&pdrvdata_lock);
209951df0accSAmit Shah 
210051df0accSAmit Shah 	__send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
21015e38483bSChristian Borntraeger 			   VIRTIO_CONSOLE_DEVICE_READY, 1);
21025e38483bSChristian Borntraeger 
21035e38483bSChristian Borntraeger 	/*
21045e38483bSChristian Borntraeger 	 * If there was an early virtio console, assume that there are no
21055e38483bSChristian Borntraeger 	 * other consoles. We need to wait until the hvc_alloc matches the
21065e38483bSChristian Borntraeger 	 * hvc_instantiate, otherwise tty_open will complain, resulting in
21075e38483bSChristian Borntraeger 	 * a "Warning: unable to open an initial console" boot failure.
21085e38483bSChristian Borntraeger 	 * Without multiport this is done in add_port above. With multiport
21095e38483bSChristian Borntraeger 	 * this might take some host<->guest communication - thus we have to
21105e38483bSChristian Borntraeger 	 * wait.
21115e38483bSChristian Borntraeger 	 */
21125e38483bSChristian Borntraeger 	if (multiport && early)
21135e38483bSChristian Borntraeger 		wait_for_completion(&early_console_added);
211451df0accSAmit Shah 
211551df0accSAmit Shah 	return 0;
211651df0accSAmit Shah 
211751df0accSAmit Shah free_chrdev:
211851df0accSAmit Shah 	unregister_chrdev(portdev->chr_major, "virtio-portsdev");
211951df0accSAmit Shah free:
212051df0accSAmit Shah 	kfree(portdev);
212151df0accSAmit Shah fail:
212251df0accSAmit Shah 	return err;
212351df0accSAmit Shah }
21248b66c917SRikard Falkeborn 
212551df0accSAmit Shah static const struct virtio_device_id id_table[] = {
212651df0accSAmit Shah 	{ VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
212751df0accSAmit Shah 	{ 0 },
2128897c44f0SAlexander Lobakin };
212951df0accSAmit Shah MODULE_DEVICE_TABLE(virtio, id_table);
21308b66c917SRikard Falkeborn 
213151df0accSAmit Shah static const unsigned int features[] = {
213251df0accSAmit Shah 	VIRTIO_CONSOLE_F_SIZE,
213351df0accSAmit Shah 	VIRTIO_CONSOLE_F_MULTIPORT,
213451df0accSAmit Shah };
21358b66c917SRikard Falkeborn 
21361b637046SSjur Brændeland static const struct virtio_device_id rproc_serial_id_table[] = {
21371b637046SSjur Brændeland #if IS_ENABLED(CONFIG_REMOTEPROC)
21381b637046SSjur Brændeland 	{ VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
21391b637046SSjur Brændeland #endif
21401b637046SSjur Brændeland 	{ 0 },
2141897c44f0SAlexander Lobakin };
21421b637046SSjur Brændeland MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
21438b66c917SRikard Falkeborn 
21441b637046SSjur Brændeland static const unsigned int rproc_serial_features[] = {
21451b637046SSjur Brændeland };
214689107000SAaron Lu 
21472b8f41d8SAmit Shah #ifdef CONFIG_PM_SLEEP
virtcons_freeze(struct virtio_device * vdev)21482b8f41d8SAmit Shah static int virtcons_freeze(struct virtio_device *vdev)
21492b8f41d8SAmit Shah {
21502b8f41d8SAmit Shah 	struct ports_device *portdev;
21512b8f41d8SAmit Shah 	struct port *port;
21522b8f41d8SAmit Shah 
21532b8f41d8SAmit Shah 	portdev = vdev->priv;
2154d9679d00SMichael S. Tsirkin 
21552b8f41d8SAmit Shah 	virtio_reset_device(vdev);
21562055997fSMichael S. Tsirkin 
2157c743d09dSAmit Shah 	if (use_multiport(portdev))
21582b8f41d8SAmit Shah 		virtqueue_disable_cb(portdev->c_ivq);
2159eeb8a7e8SMichael S. Tsirkin 	cancel_work_sync(&portdev->control_work);
2160c743d09dSAmit Shah 	cancel_work_sync(&portdev->config_work);
2161c743d09dSAmit Shah 	/*
2162c743d09dSAmit Shah 	 * Once more: if control_work_handler() was running, it would
2163c743d09dSAmit Shah 	 * enable the cb as the last step.
21642055997fSMichael S. Tsirkin 	 */
2165c743d09dSAmit Shah 	if (use_multiport(portdev))
21662b8f41d8SAmit Shah 		virtqueue_disable_cb(portdev->c_ivq);
21672b8f41d8SAmit Shah 
2168c743d09dSAmit Shah 	list_for_each_entry(port, &portdev->ports, list) {
2169c743d09dSAmit Shah 		virtqueue_disable_cb(port->in_vq);
21702b8f41d8SAmit Shah 		virtqueue_disable_cb(port->out_vq);
21712b8f41d8SAmit Shah 		/*
21722b8f41d8SAmit Shah 		 * We'll ask the host later if the new invocation has
21732b8f41d8SAmit Shah 		 * the port opened or closed.
21742b8f41d8SAmit Shah 		 */
21752b8f41d8SAmit Shah 		port->host_connected = false;
21762b8f41d8SAmit Shah 		remove_port_data(port);
21772b8f41d8SAmit Shah 	}
21782b8f41d8SAmit Shah 	remove_vqs(portdev);
21792b8f41d8SAmit Shah 
21802b8f41d8SAmit Shah 	return 0;
21812b8f41d8SAmit Shah }
21822b8f41d8SAmit Shah 
virtcons_restore(struct virtio_device * vdev)21832b8f41d8SAmit Shah static int virtcons_restore(struct virtio_device *vdev)
21842b8f41d8SAmit Shah {
21852b8f41d8SAmit Shah 	struct ports_device *portdev;
21862b8f41d8SAmit Shah 	struct port *port;
21872b8f41d8SAmit Shah 	int ret;
21882b8f41d8SAmit Shah 
21892b8f41d8SAmit Shah 	portdev = vdev->priv;
21902b8f41d8SAmit Shah 
21912b8f41d8SAmit Shah 	ret = init_vqs(portdev);
21922b8f41d8SAmit Shah 	if (ret)
21932b8f41d8SAmit Shah 		return ret;
2194401bbdc9SMichael S. Tsirkin 
2195401bbdc9SMichael S. Tsirkin 	virtio_device_ready(portdev->vdev);
21962b8f41d8SAmit Shah 
2197165b1b8bSAmit Shah 	if (use_multiport(portdev))
21982b8f41d8SAmit Shah 		fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
21992b8f41d8SAmit Shah 
22002b8f41d8SAmit Shah 	list_for_each_entry(port, &portdev->ports, list) {
22012b8f41d8SAmit Shah 		port->in_vq = portdev->in_vqs[port->id];
22022b8f41d8SAmit Shah 		port->out_vq = portdev->out_vqs[port->id];
22032b8f41d8SAmit Shah 
22042b8f41d8SAmit Shah 		fill_queue(port->in_vq, &port->inbuf_lock);
22052b8f41d8SAmit Shah 
22062b8f41d8SAmit Shah 		/* Get port open/close status on the host */
2207fa8b66ccSAmit Shah 		send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2208fa8b66ccSAmit Shah 
2209fa8b66ccSAmit Shah 		/*
2210fa8b66ccSAmit Shah 		 * If a port was open at the time of suspending, we
2211fa8b66ccSAmit Shah 		 * have to let the host know that it's still open.
2212fa8b66ccSAmit Shah 		 */
2213fa8b66ccSAmit Shah 		if (port->guest_connected)
22142b8f41d8SAmit Shah 			send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
22152b8f41d8SAmit Shah 	}
22162b8f41d8SAmit Shah 	return 0;
22172b8f41d8SAmit Shah }
22182b8f41d8SAmit Shah #endif
221951df0accSAmit Shah 
222051df0accSAmit Shah static struct virtio_driver virtio_console = {
222151df0accSAmit Shah 	.feature_table = features,
222251df0accSAmit Shah 	.feature_table_size = ARRAY_SIZE(features),
222351df0accSAmit Shah 	.driver.name =	KBUILD_MODNAME,
222451df0accSAmit Shah 	.driver.owner =	THIS_MODULE,
222551df0accSAmit Shah 	.id_table =	id_table,
222651df0accSAmit Shah 	.probe =	virtcons_probe,
222751df0accSAmit Shah 	.remove =	virtcons_remove,
222889107000SAaron Lu 	.config_changed = config_intr,
22292b8f41d8SAmit Shah #ifdef CONFIG_PM_SLEEP
22302b8f41d8SAmit Shah 	.freeze =	virtcons_freeze,
22312b8f41d8SAmit Shah 	.restore =	virtcons_restore,
223251df0accSAmit Shah #endif
223351df0accSAmit Shah };
2234bcd2982aSGreg Kroah-Hartman 
22351b637046SSjur Brændeland static struct virtio_driver virtio_rproc_serial = {
22361b637046SSjur Brændeland 	.feature_table = rproc_serial_features,
22371b637046SSjur Brændeland 	.feature_table_size = ARRAY_SIZE(rproc_serial_features),
22381b637046SSjur Brændeland 	.driver.name =	"virtio_rproc_serial",
22391b637046SSjur Brændeland 	.driver.owner =	THIS_MODULE,
22401b637046SSjur Brændeland 	.id_table =	rproc_serial_id_table,
22411b637046SSjur Brændeland 	.probe =	virtcons_probe,
22421b637046SSjur Brændeland 	.remove =	virtcons_remove,
22431b637046SSjur Brændeland };
2244fefb8a2aSRandy Dunlap 
virtio_console_init(void)224551df0accSAmit Shah static int __init virtio_console_init(void)
224651df0accSAmit Shah {
224751df0accSAmit Shah 	int err;
22481aaba11dSGreg Kroah-Hartman 
224951df0accSAmit Shah 	err = class_register(&port_class);
225051df0accSAmit Shah 	if (err)
225151df0accSAmit Shah 		return err;
225251df0accSAmit Shah 
225351df0accSAmit Shah 	pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
225451df0accSAmit Shah 	INIT_LIST_HEAD(&pdrvdata.consoles);
225551df0accSAmit Shah 	INIT_LIST_HEAD(&pdrvdata.portdevs);
225651df0accSAmit Shah 
225751df0accSAmit Shah 	err = register_virtio_driver(&virtio_console);
225851df0accSAmit Shah 	if (err < 0) {
225933e1afc3SAlexey Khoroshilov 		pr_err("Error %d registering virtio driver\n", err);
226033e1afc3SAlexey Khoroshilov 		goto free;
226133e1afc3SAlexey Khoroshilov 	}
226233e1afc3SAlexey Khoroshilov 	err = register_virtio_driver(&virtio_rproc_serial);
226333e1afc3SAlexey Khoroshilov 	if (err < 0) {
22641b637046SSjur Brændeland 		pr_err("Error %d registering virtio rproc serial driver\n",
22651b637046SSjur Brændeland 		       err);
22661b637046SSjur Brændeland 		goto unregister;
22671b637046SSjur Brændeland 	}
22681b637046SSjur Brændeland 	return 0;
22691b637046SSjur Brændeland unregister:
227033e1afc3SAlexey Khoroshilov 	unregister_virtio_driver(&virtio_console);
22711b637046SSjur Brændeland free:
22721b637046SSjur Brændeland 	debugfs_remove_recursive(pdrvdata.debugfs_dir);
227333e1afc3SAlexey Khoroshilov 	class_unregister(&port_class);
227433e1afc3SAlexey Khoroshilov 	return err;
227533e1afc3SAlexey Khoroshilov }
227633e1afc3SAlexey Khoroshilov 
virtio_console_fini(void)227751df0accSAmit Shah static void __exit virtio_console_fini(void)
227851df0accSAmit Shah {
2279fefb8a2aSRandy Dunlap 	reclaim_dma_bufs();
228051df0accSAmit Shah 
22811b637046SSjur Brændeland 	unregister_virtio_driver(&virtio_console);
22821b637046SSjur Brændeland 	unregister_virtio_driver(&virtio_rproc_serial);
228351df0accSAmit Shah 
22841b637046SSjur Brændeland 	class_unregister(&port_class);
228551df0accSAmit Shah 	debugfs_remove_recursive(pdrvdata.debugfs_dir);
228651df0accSAmit Shah }
228751df0accSAmit Shah module_init(virtio_console_init);
228851df0accSAmit Shah module_exit(virtio_console_fini);
2289fefb8a2aSRandy Dunlap 
2290fefb8a2aSRandy Dunlap MODULE_DESCRIPTION("Virtio console driver");
229151df0accSAmit Shah MODULE_LICENSE("GPL");
229251df0accSAmit Shah