xref: /OK3568_Linux_fs/kernel/drivers/char/virtio_console.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
4*4882a593Smuzhiyun  * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
5*4882a593Smuzhiyun  * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/cdev.h>
8*4882a593Smuzhiyun #include <linux/debugfs.h>
9*4882a593Smuzhiyun #include <linux/completion.h>
10*4882a593Smuzhiyun #include <linux/device.h>
11*4882a593Smuzhiyun #include <linux/err.h>
12*4882a593Smuzhiyun #include <linux/freezer.h>
13*4882a593Smuzhiyun #include <linux/fs.h>
14*4882a593Smuzhiyun #include <linux/splice.h>
15*4882a593Smuzhiyun #include <linux/pagemap.h>
16*4882a593Smuzhiyun #include <linux/init.h>
17*4882a593Smuzhiyun #include <linux/list.h>
18*4882a593Smuzhiyun #include <linux/poll.h>
19*4882a593Smuzhiyun #include <linux/sched.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/spinlock.h>
22*4882a593Smuzhiyun #include <linux/virtio.h>
23*4882a593Smuzhiyun #include <linux/virtio_console.h>
24*4882a593Smuzhiyun #include <linux/wait.h>
25*4882a593Smuzhiyun #include <linux/workqueue.h>
26*4882a593Smuzhiyun #include <linux/module.h>
27*4882a593Smuzhiyun #include <linux/dma-mapping.h>
28*4882a593Smuzhiyun #include "../tty/hvc/hvc_console.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun  * This is a global struct for storing common data for all the devices
34*4882a593Smuzhiyun  * this driver handles.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * Mainly, it has a linked list for all the consoles in one place so
37*4882a593Smuzhiyun  * that callbacks from hvc for get_chars(), put_chars() work properly
38*4882a593Smuzhiyun  * across multiple devices and multiple ports per device.
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun struct ports_driver_data {
41*4882a593Smuzhiyun 	/* Used for registering chardevs */
42*4882a593Smuzhiyun 	struct class *class;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	/* Used for exporting per-port information to debugfs */
45*4882a593Smuzhiyun 	struct dentry *debugfs_dir;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	/* List of all the devices we're handling */
48*4882a593Smuzhiyun 	struct list_head portdevs;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	/*
51*4882a593Smuzhiyun 	 * This is used to keep track of the number of hvc consoles
52*4882a593Smuzhiyun 	 * spawned by this driver.  This number is given as the first
53*4882a593Smuzhiyun 	 * argument to hvc_alloc().  To correctly map an initial
54*4882a593Smuzhiyun 	 * console spawned via hvc_instantiate to the console being
55*4882a593Smuzhiyun 	 * hooked up via hvc_alloc, we need to pass the same vtermno.
56*4882a593Smuzhiyun 	 *
57*4882a593Smuzhiyun 	 * We also just assume the first console being initialised was
58*4882a593Smuzhiyun 	 * the first one that got used as the initial console.
59*4882a593Smuzhiyun 	 */
60*4882a593Smuzhiyun 	unsigned int next_vtermno;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	/* All the console devices handled by this driver */
63*4882a593Smuzhiyun 	struct list_head consoles;
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun static struct ports_driver_data pdrvdata = { .next_vtermno = 1};
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun static DEFINE_SPINLOCK(pdrvdata_lock);
68*4882a593Smuzhiyun static DECLARE_COMPLETION(early_console_added);
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun /* This struct holds information that's relevant only for console ports */
71*4882a593Smuzhiyun struct console {
72*4882a593Smuzhiyun 	/* We'll place all consoles in a list in the pdrvdata struct */
73*4882a593Smuzhiyun 	struct list_head list;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	/* The hvc device associated with this console port */
76*4882a593Smuzhiyun 	struct hvc_struct *hvc;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	/* The size of the console */
79*4882a593Smuzhiyun 	struct winsize ws;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/*
82*4882a593Smuzhiyun 	 * This number identifies the number that we used to register
83*4882a593Smuzhiyun 	 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
84*4882a593Smuzhiyun 	 * number passed on by the hvc callbacks to us to
85*4882a593Smuzhiyun 	 * differentiate between the other console ports handled by
86*4882a593Smuzhiyun 	 * this driver
87*4882a593Smuzhiyun 	 */
88*4882a593Smuzhiyun 	u32 vtermno;
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun struct port_buffer {
92*4882a593Smuzhiyun 	char *buf;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	/* size of the buffer in *buf above */
95*4882a593Smuzhiyun 	size_t size;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* used length of the buffer */
98*4882a593Smuzhiyun 	size_t len;
99*4882a593Smuzhiyun 	/* offset in the buf from which to consume data */
100*4882a593Smuzhiyun 	size_t offset;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	/* DMA address of buffer */
103*4882a593Smuzhiyun 	dma_addr_t dma;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/* Device we got DMA memory from */
106*4882a593Smuzhiyun 	struct device *dev;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* List of pending dma buffers to free */
109*4882a593Smuzhiyun 	struct list_head list;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	/* If sgpages == 0 then buf is used */
112*4882a593Smuzhiyun 	unsigned int sgpages;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	/* sg is used if spages > 0. sg must be the last in is struct */
115*4882a593Smuzhiyun 	struct scatterlist sg[];
116*4882a593Smuzhiyun };
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /*
119*4882a593Smuzhiyun  * This is a per-device struct that stores data common to all the
120*4882a593Smuzhiyun  * ports for that device (vdev->priv).
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun struct ports_device {
123*4882a593Smuzhiyun 	/* Next portdev in the list, head is in the pdrvdata struct */
124*4882a593Smuzhiyun 	struct list_head list;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	/*
127*4882a593Smuzhiyun 	 * Workqueue handlers where we process deferred work after
128*4882a593Smuzhiyun 	 * notification
129*4882a593Smuzhiyun 	 */
130*4882a593Smuzhiyun 	struct work_struct control_work;
131*4882a593Smuzhiyun 	struct work_struct config_work;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	struct list_head ports;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/* To protect the list of ports */
136*4882a593Smuzhiyun 	spinlock_t ports_lock;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* To protect the vq operations for the control channel */
139*4882a593Smuzhiyun 	spinlock_t c_ivq_lock;
140*4882a593Smuzhiyun 	spinlock_t c_ovq_lock;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	/* max. number of ports this device can hold */
143*4882a593Smuzhiyun 	u32 max_nr_ports;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	/* The virtio device we're associated with */
146*4882a593Smuzhiyun 	struct virtio_device *vdev;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	/*
149*4882a593Smuzhiyun 	 * A couple of virtqueues for the control channel: one for
150*4882a593Smuzhiyun 	 * guest->host transfers, one for host->guest transfers
151*4882a593Smuzhiyun 	 */
152*4882a593Smuzhiyun 	struct virtqueue *c_ivq, *c_ovq;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	/*
155*4882a593Smuzhiyun 	 * A control packet buffer for guest->host requests, protected
156*4882a593Smuzhiyun 	 * by c_ovq_lock.
157*4882a593Smuzhiyun 	 */
158*4882a593Smuzhiyun 	struct virtio_console_control cpkt;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	/* Array of per-port IO virtqueues */
161*4882a593Smuzhiyun 	struct virtqueue **in_vqs, **out_vqs;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/* Major number for this device.  Ports will be created as minors. */
164*4882a593Smuzhiyun 	int chr_major;
165*4882a593Smuzhiyun };
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun struct port_stats {
168*4882a593Smuzhiyun 	unsigned long bytes_sent, bytes_received, bytes_discarded;
169*4882a593Smuzhiyun };
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /* This struct holds the per-port data */
172*4882a593Smuzhiyun struct port {
173*4882a593Smuzhiyun 	/* Next port in the list, head is in the ports_device */
174*4882a593Smuzhiyun 	struct list_head list;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* Pointer to the parent virtio_console device */
177*4882a593Smuzhiyun 	struct ports_device *portdev;
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	/* The current buffer from which data has to be fed to readers */
180*4882a593Smuzhiyun 	struct port_buffer *inbuf;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/*
183*4882a593Smuzhiyun 	 * To protect the operations on the in_vq associated with this
184*4882a593Smuzhiyun 	 * port.  Has to be a spinlock because it can be called from
185*4882a593Smuzhiyun 	 * interrupt context (get_char()).
186*4882a593Smuzhiyun 	 */
187*4882a593Smuzhiyun 	spinlock_t inbuf_lock;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	/* Protect the operations on the out_vq. */
190*4882a593Smuzhiyun 	spinlock_t outvq_lock;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	/* The IO vqs for this port */
193*4882a593Smuzhiyun 	struct virtqueue *in_vq, *out_vq;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	/* File in the debugfs directory that exposes this port's information */
196*4882a593Smuzhiyun 	struct dentry *debugfs_file;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/*
199*4882a593Smuzhiyun 	 * Keep count of the bytes sent, received and discarded for
200*4882a593Smuzhiyun 	 * this port for accounting and debugging purposes.  These
201*4882a593Smuzhiyun 	 * counts are not reset across port open / close events.
202*4882a593Smuzhiyun 	 */
203*4882a593Smuzhiyun 	struct port_stats stats;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/*
206*4882a593Smuzhiyun 	 * The entries in this struct will be valid if this port is
207*4882a593Smuzhiyun 	 * hooked up to an hvc console
208*4882a593Smuzhiyun 	 */
209*4882a593Smuzhiyun 	struct console cons;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	/* Each port associates with a separate char device */
212*4882a593Smuzhiyun 	struct cdev *cdev;
213*4882a593Smuzhiyun 	struct device *dev;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/* Reference-counting to handle port hot-unplugs and file operations */
216*4882a593Smuzhiyun 	struct kref kref;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* A waitqueue for poll() or blocking read operations */
219*4882a593Smuzhiyun 	wait_queue_head_t waitqueue;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	/* The 'name' of the port that we expose via sysfs properties */
222*4882a593Smuzhiyun 	char *name;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	/* We can notify apps of host connect / disconnect events via SIGIO */
225*4882a593Smuzhiyun 	struct fasync_struct *async_queue;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	/* The 'id' to identify the port with the Host */
228*4882a593Smuzhiyun 	u32 id;
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	bool outvq_full;
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	/* Is the host device open */
233*4882a593Smuzhiyun 	bool host_connected;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/* We should allow only one process to open a port */
236*4882a593Smuzhiyun 	bool guest_connected;
237*4882a593Smuzhiyun };
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun /* This is the very early arch-specified put chars function. */
240*4882a593Smuzhiyun static int (*early_put_chars)(u32, const char *, int);
241*4882a593Smuzhiyun 
find_port_by_vtermno(u32 vtermno)242*4882a593Smuzhiyun static struct port *find_port_by_vtermno(u32 vtermno)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	struct port *port;
245*4882a593Smuzhiyun 	struct console *cons;
246*4882a593Smuzhiyun 	unsigned long flags;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	spin_lock_irqsave(&pdrvdata_lock, flags);
249*4882a593Smuzhiyun 	list_for_each_entry(cons, &pdrvdata.consoles, list) {
250*4882a593Smuzhiyun 		if (cons->vtermno == vtermno) {
251*4882a593Smuzhiyun 			port = container_of(cons, struct port, cons);
252*4882a593Smuzhiyun 			goto out;
253*4882a593Smuzhiyun 		}
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun 	port = NULL;
256*4882a593Smuzhiyun out:
257*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdrvdata_lock, flags);
258*4882a593Smuzhiyun 	return port;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun 
find_port_by_devt_in_portdev(struct ports_device * portdev,dev_t dev)261*4882a593Smuzhiyun static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
262*4882a593Smuzhiyun 						 dev_t dev)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct port *port;
265*4882a593Smuzhiyun 	unsigned long flags;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	spin_lock_irqsave(&portdev->ports_lock, flags);
268*4882a593Smuzhiyun 	list_for_each_entry(port, &portdev->ports, list) {
269*4882a593Smuzhiyun 		if (port->cdev->dev == dev) {
270*4882a593Smuzhiyun 			kref_get(&port->kref);
271*4882a593Smuzhiyun 			goto out;
272*4882a593Smuzhiyun 		}
273*4882a593Smuzhiyun 	}
274*4882a593Smuzhiyun 	port = NULL;
275*4882a593Smuzhiyun out:
276*4882a593Smuzhiyun 	spin_unlock_irqrestore(&portdev->ports_lock, flags);
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	return port;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
find_port_by_devt(dev_t dev)281*4882a593Smuzhiyun static struct port *find_port_by_devt(dev_t dev)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	struct ports_device *portdev;
284*4882a593Smuzhiyun 	struct port *port;
285*4882a593Smuzhiyun 	unsigned long flags;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	spin_lock_irqsave(&pdrvdata_lock, flags);
288*4882a593Smuzhiyun 	list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
289*4882a593Smuzhiyun 		port = find_port_by_devt_in_portdev(portdev, dev);
290*4882a593Smuzhiyun 		if (port)
291*4882a593Smuzhiyun 			goto out;
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun 	port = NULL;
294*4882a593Smuzhiyun out:
295*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pdrvdata_lock, flags);
296*4882a593Smuzhiyun 	return port;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun 
find_port_by_id(struct ports_device * portdev,u32 id)299*4882a593Smuzhiyun static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	struct port *port;
302*4882a593Smuzhiyun 	unsigned long flags;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	spin_lock_irqsave(&portdev->ports_lock, flags);
305*4882a593Smuzhiyun 	list_for_each_entry(port, &portdev->ports, list)
306*4882a593Smuzhiyun 		if (port->id == id)
307*4882a593Smuzhiyun 			goto out;
308*4882a593Smuzhiyun 	port = NULL;
309*4882a593Smuzhiyun out:
310*4882a593Smuzhiyun 	spin_unlock_irqrestore(&portdev->ports_lock, flags);
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	return port;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
find_port_by_vq(struct ports_device * portdev,struct virtqueue * vq)315*4882a593Smuzhiyun static struct port *find_port_by_vq(struct ports_device *portdev,
316*4882a593Smuzhiyun 				    struct virtqueue *vq)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	struct port *port;
319*4882a593Smuzhiyun 	unsigned long flags;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	spin_lock_irqsave(&portdev->ports_lock, flags);
322*4882a593Smuzhiyun 	list_for_each_entry(port, &portdev->ports, list)
323*4882a593Smuzhiyun 		if (port->in_vq == vq || port->out_vq == vq)
324*4882a593Smuzhiyun 			goto out;
325*4882a593Smuzhiyun 	port = NULL;
326*4882a593Smuzhiyun out:
327*4882a593Smuzhiyun 	spin_unlock_irqrestore(&portdev->ports_lock, flags);
328*4882a593Smuzhiyun 	return port;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
is_console_port(struct port * port)331*4882a593Smuzhiyun static bool is_console_port(struct port *port)
332*4882a593Smuzhiyun {
333*4882a593Smuzhiyun 	if (port->cons.hvc)
334*4882a593Smuzhiyun 		return true;
335*4882a593Smuzhiyun 	return false;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun 
is_rproc_serial(const struct virtio_device * vdev)338*4882a593Smuzhiyun static bool is_rproc_serial(const struct virtio_device *vdev)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun 	return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
use_multiport(struct ports_device * portdev)343*4882a593Smuzhiyun static inline bool use_multiport(struct ports_device *portdev)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun 	/*
346*4882a593Smuzhiyun 	 * This condition can be true when put_chars is called from
347*4882a593Smuzhiyun 	 * early_init
348*4882a593Smuzhiyun 	 */
349*4882a593Smuzhiyun 	if (!portdev->vdev)
350*4882a593Smuzhiyun 		return false;
351*4882a593Smuzhiyun 	return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun static DEFINE_SPINLOCK(dma_bufs_lock);
355*4882a593Smuzhiyun static LIST_HEAD(pending_free_dma_bufs);
356*4882a593Smuzhiyun 
free_buf(struct port_buffer * buf,bool can_sleep)357*4882a593Smuzhiyun static void free_buf(struct port_buffer *buf, bool can_sleep)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	unsigned int i;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	for (i = 0; i < buf->sgpages; i++) {
362*4882a593Smuzhiyun 		struct page *page = sg_page(&buf->sg[i]);
363*4882a593Smuzhiyun 		if (!page)
364*4882a593Smuzhiyun 			break;
365*4882a593Smuzhiyun 		put_page(page);
366*4882a593Smuzhiyun 	}
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (!buf->dev) {
369*4882a593Smuzhiyun 		kfree(buf->buf);
370*4882a593Smuzhiyun 	} else if (is_rproc_enabled) {
371*4882a593Smuzhiyun 		unsigned long flags;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 		/* dma_free_coherent requires interrupts to be enabled. */
374*4882a593Smuzhiyun 		if (!can_sleep) {
375*4882a593Smuzhiyun 			/* queue up dma-buffers to be freed later */
376*4882a593Smuzhiyun 			spin_lock_irqsave(&dma_bufs_lock, flags);
377*4882a593Smuzhiyun 			list_add_tail(&buf->list, &pending_free_dma_bufs);
378*4882a593Smuzhiyun 			spin_unlock_irqrestore(&dma_bufs_lock, flags);
379*4882a593Smuzhiyun 			return;
380*4882a593Smuzhiyun 		}
381*4882a593Smuzhiyun 		dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 		/* Release device refcnt and allow it to be freed */
384*4882a593Smuzhiyun 		put_device(buf->dev);
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	kfree(buf);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
reclaim_dma_bufs(void)390*4882a593Smuzhiyun static void reclaim_dma_bufs(void)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	unsigned long flags;
393*4882a593Smuzhiyun 	struct port_buffer *buf, *tmp;
394*4882a593Smuzhiyun 	LIST_HEAD(tmp_list);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	if (list_empty(&pending_free_dma_bufs))
397*4882a593Smuzhiyun 		return;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	/* Create a copy of the pending_free_dma_bufs while holding the lock */
400*4882a593Smuzhiyun 	spin_lock_irqsave(&dma_bufs_lock, flags);
401*4882a593Smuzhiyun 	list_cut_position(&tmp_list, &pending_free_dma_bufs,
402*4882a593Smuzhiyun 			  pending_free_dma_bufs.prev);
403*4882a593Smuzhiyun 	spin_unlock_irqrestore(&dma_bufs_lock, flags);
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	/* Release the dma buffers, without irqs enabled */
406*4882a593Smuzhiyun 	list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
407*4882a593Smuzhiyun 		list_del(&buf->list);
408*4882a593Smuzhiyun 		free_buf(buf, true);
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
alloc_buf(struct virtio_device * vdev,size_t buf_size,int pages)412*4882a593Smuzhiyun static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
413*4882a593Smuzhiyun 				     int pages)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	struct port_buffer *buf;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	reclaim_dma_bufs();
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	/*
420*4882a593Smuzhiyun 	 * Allocate buffer and the sg list. The sg list array is allocated
421*4882a593Smuzhiyun 	 * directly after the port_buffer struct.
422*4882a593Smuzhiyun 	 */
423*4882a593Smuzhiyun 	buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
424*4882a593Smuzhiyun 	if (!buf)
425*4882a593Smuzhiyun 		goto fail;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	buf->sgpages = pages;
428*4882a593Smuzhiyun 	if (pages > 0) {
429*4882a593Smuzhiyun 		buf->dev = NULL;
430*4882a593Smuzhiyun 		buf->buf = NULL;
431*4882a593Smuzhiyun 		return buf;
432*4882a593Smuzhiyun 	}
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	if (is_rproc_serial(vdev)) {
435*4882a593Smuzhiyun 		/*
436*4882a593Smuzhiyun 		 * Allocate DMA memory from ancestor. When a virtio
437*4882a593Smuzhiyun 		 * device is created by remoteproc, the DMA memory is
438*4882a593Smuzhiyun 		 * associated with the parent device:
439*4882a593Smuzhiyun 		 * virtioY => remoteprocX#vdevYbuffer.
440*4882a593Smuzhiyun 		 */
441*4882a593Smuzhiyun 		buf->dev = vdev->dev.parent;
442*4882a593Smuzhiyun 		if (!buf->dev)
443*4882a593Smuzhiyun 			goto free_buf;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 		/* Increase device refcnt to avoid freeing it */
446*4882a593Smuzhiyun 		get_device(buf->dev);
447*4882a593Smuzhiyun 		buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
448*4882a593Smuzhiyun 					      GFP_KERNEL);
449*4882a593Smuzhiyun 	} else {
450*4882a593Smuzhiyun 		buf->dev = NULL;
451*4882a593Smuzhiyun 		buf->buf = kmalloc(buf_size, GFP_KERNEL);
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	if (!buf->buf)
455*4882a593Smuzhiyun 		goto free_buf;
456*4882a593Smuzhiyun 	buf->len = 0;
457*4882a593Smuzhiyun 	buf->offset = 0;
458*4882a593Smuzhiyun 	buf->size = buf_size;
459*4882a593Smuzhiyun 	return buf;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun free_buf:
462*4882a593Smuzhiyun 	kfree(buf);
463*4882a593Smuzhiyun fail:
464*4882a593Smuzhiyun 	return NULL;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /* Callers should take appropriate locks */
get_inbuf(struct port * port)468*4882a593Smuzhiyun static struct port_buffer *get_inbuf(struct port *port)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	struct port_buffer *buf;
471*4882a593Smuzhiyun 	unsigned int len;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	if (port->inbuf)
474*4882a593Smuzhiyun 		return port->inbuf;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	buf = virtqueue_get_buf(port->in_vq, &len);
477*4882a593Smuzhiyun 	if (buf) {
478*4882a593Smuzhiyun 		buf->len = min_t(size_t, len, buf->size);
479*4882a593Smuzhiyun 		buf->offset = 0;
480*4882a593Smuzhiyun 		port->stats.bytes_received += len;
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun 	return buf;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun /*
486*4882a593Smuzhiyun  * Create a scatter-gather list representing our input buffer and put
487*4882a593Smuzhiyun  * it in the queue.
488*4882a593Smuzhiyun  *
489*4882a593Smuzhiyun  * Callers should take appropriate locks.
490*4882a593Smuzhiyun  */
add_inbuf(struct virtqueue * vq,struct port_buffer * buf)491*4882a593Smuzhiyun static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	struct scatterlist sg[1];
494*4882a593Smuzhiyun 	int ret;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	sg_init_one(sg, buf->buf, buf->size);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
499*4882a593Smuzhiyun 	virtqueue_kick(vq);
500*4882a593Smuzhiyun 	if (!ret)
501*4882a593Smuzhiyun 		ret = vq->num_free;
502*4882a593Smuzhiyun 	return ret;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun /* Discard any unread data this port has. Callers lockers. */
discard_port_data(struct port * port)506*4882a593Smuzhiyun static void discard_port_data(struct port *port)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun 	struct port_buffer *buf;
509*4882a593Smuzhiyun 	unsigned int err;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	if (!port->portdev) {
512*4882a593Smuzhiyun 		/* Device has been unplugged.  vqs are already gone. */
513*4882a593Smuzhiyun 		return;
514*4882a593Smuzhiyun 	}
515*4882a593Smuzhiyun 	buf = get_inbuf(port);
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	err = 0;
518*4882a593Smuzhiyun 	while (buf) {
519*4882a593Smuzhiyun 		port->stats.bytes_discarded += buf->len - buf->offset;
520*4882a593Smuzhiyun 		if (add_inbuf(port->in_vq, buf) < 0) {
521*4882a593Smuzhiyun 			err++;
522*4882a593Smuzhiyun 			free_buf(buf, false);
523*4882a593Smuzhiyun 		}
524*4882a593Smuzhiyun 		port->inbuf = NULL;
525*4882a593Smuzhiyun 		buf = get_inbuf(port);
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 	if (err)
528*4882a593Smuzhiyun 		dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
529*4882a593Smuzhiyun 			 err);
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun 
port_has_data(struct port * port)532*4882a593Smuzhiyun static bool port_has_data(struct port *port)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	unsigned long flags;
535*4882a593Smuzhiyun 	bool ret;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	ret = false;
538*4882a593Smuzhiyun 	spin_lock_irqsave(&port->inbuf_lock, flags);
539*4882a593Smuzhiyun 	port->inbuf = get_inbuf(port);
540*4882a593Smuzhiyun 	if (port->inbuf)
541*4882a593Smuzhiyun 		ret = true;
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	spin_unlock_irqrestore(&port->inbuf_lock, flags);
544*4882a593Smuzhiyun 	return ret;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun 
__send_control_msg(struct ports_device * portdev,u32 port_id,unsigned int event,unsigned int value)547*4882a593Smuzhiyun static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
548*4882a593Smuzhiyun 				  unsigned int event, unsigned int value)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun 	struct scatterlist sg[1];
551*4882a593Smuzhiyun 	struct virtqueue *vq;
552*4882a593Smuzhiyun 	unsigned int len;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 	if (!use_multiport(portdev))
555*4882a593Smuzhiyun 		return 0;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	vq = portdev->c_ovq;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	spin_lock(&portdev->c_ovq_lock);
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
562*4882a593Smuzhiyun 	portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
563*4882a593Smuzhiyun 	portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
568*4882a593Smuzhiyun 		virtqueue_kick(vq);
569*4882a593Smuzhiyun 		while (!virtqueue_get_buf(vq, &len)
570*4882a593Smuzhiyun 			&& !virtqueue_is_broken(vq))
571*4882a593Smuzhiyun 			cpu_relax();
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	spin_unlock(&portdev->c_ovq_lock);
575*4882a593Smuzhiyun 	return 0;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun 
send_control_msg(struct port * port,unsigned int event,unsigned int value)578*4882a593Smuzhiyun static ssize_t send_control_msg(struct port *port, unsigned int event,
579*4882a593Smuzhiyun 				unsigned int value)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun 	/* Did the port get unplugged before userspace closed it? */
582*4882a593Smuzhiyun 	if (port->portdev)
583*4882a593Smuzhiyun 		return __send_control_msg(port->portdev, port->id, event, value);
584*4882a593Smuzhiyun 	return 0;
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun /* Callers must take the port->outvq_lock */
reclaim_consumed_buffers(struct port * port)589*4882a593Smuzhiyun static void reclaim_consumed_buffers(struct port *port)
590*4882a593Smuzhiyun {
591*4882a593Smuzhiyun 	struct port_buffer *buf;
592*4882a593Smuzhiyun 	unsigned int len;
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	if (!port->portdev) {
595*4882a593Smuzhiyun 		/* Device has been unplugged.  vqs are already gone. */
596*4882a593Smuzhiyun 		return;
597*4882a593Smuzhiyun 	}
598*4882a593Smuzhiyun 	while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
599*4882a593Smuzhiyun 		free_buf(buf, false);
600*4882a593Smuzhiyun 		port->outvq_full = false;
601*4882a593Smuzhiyun 	}
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
__send_to_port(struct port * port,struct scatterlist * sg,int nents,size_t in_count,void * data,bool nonblock)604*4882a593Smuzhiyun static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
605*4882a593Smuzhiyun 			      int nents, size_t in_count,
606*4882a593Smuzhiyun 			      void *data, bool nonblock)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun 	struct virtqueue *out_vq;
609*4882a593Smuzhiyun 	int err;
610*4882a593Smuzhiyun 	unsigned long flags;
611*4882a593Smuzhiyun 	unsigned int len;
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	out_vq = port->out_vq;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	spin_lock_irqsave(&port->outvq_lock, flags);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	reclaim_consumed_buffers(port);
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* Tell Host to go! */
622*4882a593Smuzhiyun 	virtqueue_kick(out_vq);
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	if (err) {
625*4882a593Smuzhiyun 		in_count = 0;
626*4882a593Smuzhiyun 		goto done;
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	if (out_vq->num_free == 0)
630*4882a593Smuzhiyun 		port->outvq_full = true;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	if (nonblock)
633*4882a593Smuzhiyun 		goto done;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun 	/*
636*4882a593Smuzhiyun 	 * Wait till the host acknowledges it pushed out the data we
637*4882a593Smuzhiyun 	 * sent.  This is done for data from the hvc_console; the tty
638*4882a593Smuzhiyun 	 * operations are performed with spinlocks held so we can't
639*4882a593Smuzhiyun 	 * sleep here.  An alternative would be to copy the data to a
640*4882a593Smuzhiyun 	 * buffer and relax the spinning requirement.  The downside is
641*4882a593Smuzhiyun 	 * we need to kmalloc a GFP_ATOMIC buffer each time the
642*4882a593Smuzhiyun 	 * console driver writes something out.
643*4882a593Smuzhiyun 	 */
644*4882a593Smuzhiyun 	while (!virtqueue_get_buf(out_vq, &len)
645*4882a593Smuzhiyun 		&& !virtqueue_is_broken(out_vq))
646*4882a593Smuzhiyun 		cpu_relax();
647*4882a593Smuzhiyun done:
648*4882a593Smuzhiyun 	spin_unlock_irqrestore(&port->outvq_lock, flags);
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	port->stats.bytes_sent += in_count;
651*4882a593Smuzhiyun 	/*
652*4882a593Smuzhiyun 	 * We're expected to return the amount of data we wrote -- all
653*4882a593Smuzhiyun 	 * of it
654*4882a593Smuzhiyun 	 */
655*4882a593Smuzhiyun 	return in_count;
656*4882a593Smuzhiyun }
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun /*
659*4882a593Smuzhiyun  * Give out the data that's requested from the buffer that we have
660*4882a593Smuzhiyun  * queued up.
661*4882a593Smuzhiyun  */
fill_readbuf(struct port * port,char __user * out_buf,size_t out_count,bool to_user)662*4882a593Smuzhiyun static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
663*4882a593Smuzhiyun 			    size_t out_count, bool to_user)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	struct port_buffer *buf;
666*4882a593Smuzhiyun 	unsigned long flags;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	if (!out_count || !port_has_data(port))
669*4882a593Smuzhiyun 		return 0;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	buf = port->inbuf;
672*4882a593Smuzhiyun 	out_count = min(out_count, buf->len - buf->offset);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (to_user) {
675*4882a593Smuzhiyun 		ssize_t ret;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 		ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
678*4882a593Smuzhiyun 		if (ret)
679*4882a593Smuzhiyun 			return -EFAULT;
680*4882a593Smuzhiyun 	} else {
681*4882a593Smuzhiyun 		memcpy((__force char *)out_buf, buf->buf + buf->offset,
682*4882a593Smuzhiyun 		       out_count);
683*4882a593Smuzhiyun 	}
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	buf->offset += out_count;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	if (buf->offset == buf->len) {
688*4882a593Smuzhiyun 		/*
689*4882a593Smuzhiyun 		 * We're done using all the data in this buffer.
690*4882a593Smuzhiyun 		 * Re-queue so that the Host can send us more data.
691*4882a593Smuzhiyun 		 */
692*4882a593Smuzhiyun 		spin_lock_irqsave(&port->inbuf_lock, flags);
693*4882a593Smuzhiyun 		port->inbuf = NULL;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 		if (add_inbuf(port->in_vq, buf) < 0)
696*4882a593Smuzhiyun 			dev_warn(port->dev, "failed add_buf\n");
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 		spin_unlock_irqrestore(&port->inbuf_lock, flags);
699*4882a593Smuzhiyun 	}
700*4882a593Smuzhiyun 	/* Return the number of bytes actually copied */
701*4882a593Smuzhiyun 	return out_count;
702*4882a593Smuzhiyun }
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun /* The condition that must be true for polling to end */
will_read_block(struct port * port)705*4882a593Smuzhiyun static bool will_read_block(struct port *port)
706*4882a593Smuzhiyun {
707*4882a593Smuzhiyun 	if (!port->guest_connected) {
708*4882a593Smuzhiyun 		/* Port got hot-unplugged. Let's exit. */
709*4882a593Smuzhiyun 		return false;
710*4882a593Smuzhiyun 	}
711*4882a593Smuzhiyun 	return !port_has_data(port) && port->host_connected;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
will_write_block(struct port * port)714*4882a593Smuzhiyun static bool will_write_block(struct port *port)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	bool ret;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	if (!port->guest_connected) {
719*4882a593Smuzhiyun 		/* Port got hot-unplugged. Let's exit. */
720*4882a593Smuzhiyun 		return false;
721*4882a593Smuzhiyun 	}
722*4882a593Smuzhiyun 	if (!port->host_connected)
723*4882a593Smuzhiyun 		return true;
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	spin_lock_irq(&port->outvq_lock);
726*4882a593Smuzhiyun 	/*
727*4882a593Smuzhiyun 	 * Check if the Host has consumed any buffers since we last
728*4882a593Smuzhiyun 	 * sent data (this is only applicable for nonblocking ports).
729*4882a593Smuzhiyun 	 */
730*4882a593Smuzhiyun 	reclaim_consumed_buffers(port);
731*4882a593Smuzhiyun 	ret = port->outvq_full;
732*4882a593Smuzhiyun 	spin_unlock_irq(&port->outvq_lock);
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	return ret;
735*4882a593Smuzhiyun }
736*4882a593Smuzhiyun 
port_fops_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)737*4882a593Smuzhiyun static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
738*4882a593Smuzhiyun 			      size_t count, loff_t *offp)
739*4882a593Smuzhiyun {
740*4882a593Smuzhiyun 	struct port *port;
741*4882a593Smuzhiyun 	ssize_t ret;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	port = filp->private_data;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	/* Port is hot-unplugged. */
746*4882a593Smuzhiyun 	if (!port->guest_connected)
747*4882a593Smuzhiyun 		return -ENODEV;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	if (!port_has_data(port)) {
750*4882a593Smuzhiyun 		/*
751*4882a593Smuzhiyun 		 * If nothing's connected on the host just return 0 in
752*4882a593Smuzhiyun 		 * case of list_empty; this tells the userspace app
753*4882a593Smuzhiyun 		 * that there's no connection
754*4882a593Smuzhiyun 		 */
755*4882a593Smuzhiyun 		if (!port->host_connected)
756*4882a593Smuzhiyun 			return 0;
757*4882a593Smuzhiyun 		if (filp->f_flags & O_NONBLOCK)
758*4882a593Smuzhiyun 			return -EAGAIN;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 		ret = wait_event_freezable(port->waitqueue,
761*4882a593Smuzhiyun 					   !will_read_block(port));
762*4882a593Smuzhiyun 		if (ret < 0)
763*4882a593Smuzhiyun 			return ret;
764*4882a593Smuzhiyun 	}
765*4882a593Smuzhiyun 	/* Port got hot-unplugged while we were waiting above. */
766*4882a593Smuzhiyun 	if (!port->guest_connected)
767*4882a593Smuzhiyun 		return -ENODEV;
768*4882a593Smuzhiyun 	/*
769*4882a593Smuzhiyun 	 * We could've received a disconnection message while we were
770*4882a593Smuzhiyun 	 * waiting for more data.
771*4882a593Smuzhiyun 	 *
772*4882a593Smuzhiyun 	 * This check is not clubbed in the if() statement above as we
773*4882a593Smuzhiyun 	 * might receive some data as well as the host could get
774*4882a593Smuzhiyun 	 * disconnected after we got woken up from our wait.  So we
775*4882a593Smuzhiyun 	 * really want to give off whatever data we have and only then
776*4882a593Smuzhiyun 	 * check for host_connected.
777*4882a593Smuzhiyun 	 */
778*4882a593Smuzhiyun 	if (!port_has_data(port) && !port->host_connected)
779*4882a593Smuzhiyun 		return 0;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	return fill_readbuf(port, ubuf, count, true);
782*4882a593Smuzhiyun }
783*4882a593Smuzhiyun 
wait_port_writable(struct port * port,bool nonblock)784*4882a593Smuzhiyun static int wait_port_writable(struct port *port, bool nonblock)
785*4882a593Smuzhiyun {
786*4882a593Smuzhiyun 	int ret;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	if (will_write_block(port)) {
789*4882a593Smuzhiyun 		if (nonblock)
790*4882a593Smuzhiyun 			return -EAGAIN;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 		ret = wait_event_freezable(port->waitqueue,
793*4882a593Smuzhiyun 					   !will_write_block(port));
794*4882a593Smuzhiyun 		if (ret < 0)
795*4882a593Smuzhiyun 			return ret;
796*4882a593Smuzhiyun 	}
797*4882a593Smuzhiyun 	/* Port got hot-unplugged. */
798*4882a593Smuzhiyun 	if (!port->guest_connected)
799*4882a593Smuzhiyun 		return -ENODEV;
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	return 0;
802*4882a593Smuzhiyun }
803*4882a593Smuzhiyun 
port_fops_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * offp)804*4882a593Smuzhiyun static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
805*4882a593Smuzhiyun 			       size_t count, loff_t *offp)
806*4882a593Smuzhiyun {
807*4882a593Smuzhiyun 	struct port *port;
808*4882a593Smuzhiyun 	struct port_buffer *buf;
809*4882a593Smuzhiyun 	ssize_t ret;
810*4882a593Smuzhiyun 	bool nonblock;
811*4882a593Smuzhiyun 	struct scatterlist sg[1];
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	/* Userspace could be out to fool us */
814*4882a593Smuzhiyun 	if (!count)
815*4882a593Smuzhiyun 		return 0;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun 	port = filp->private_data;
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	nonblock = filp->f_flags & O_NONBLOCK;
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	ret = wait_port_writable(port, nonblock);
822*4882a593Smuzhiyun 	if (ret < 0)
823*4882a593Smuzhiyun 		return ret;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	count = min((size_t)(32 * 1024), count);
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	buf = alloc_buf(port->portdev->vdev, count, 0);
828*4882a593Smuzhiyun 	if (!buf)
829*4882a593Smuzhiyun 		return -ENOMEM;
830*4882a593Smuzhiyun 
831*4882a593Smuzhiyun 	ret = copy_from_user(buf->buf, ubuf, count);
832*4882a593Smuzhiyun 	if (ret) {
833*4882a593Smuzhiyun 		ret = -EFAULT;
834*4882a593Smuzhiyun 		goto free_buf;
835*4882a593Smuzhiyun 	}
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	/*
838*4882a593Smuzhiyun 	 * We now ask send_buf() to not spin for generic ports -- we
839*4882a593Smuzhiyun 	 * can re-use the same code path that non-blocking file
840*4882a593Smuzhiyun 	 * descriptors take for blocking file descriptors since the
841*4882a593Smuzhiyun 	 * wait is already done and we're certain the write will go
842*4882a593Smuzhiyun 	 * through to the host.
843*4882a593Smuzhiyun 	 */
844*4882a593Smuzhiyun 	nonblock = true;
845*4882a593Smuzhiyun 	sg_init_one(sg, buf->buf, count);
846*4882a593Smuzhiyun 	ret = __send_to_port(port, sg, 1, count, buf, nonblock);
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun 	if (nonblock && ret > 0)
849*4882a593Smuzhiyun 		goto out;
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun free_buf:
852*4882a593Smuzhiyun 	free_buf(buf, true);
853*4882a593Smuzhiyun out:
854*4882a593Smuzhiyun 	return ret;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun struct sg_list {
858*4882a593Smuzhiyun 	unsigned int n;
859*4882a593Smuzhiyun 	unsigned int size;
860*4882a593Smuzhiyun 	size_t len;
861*4882a593Smuzhiyun 	struct scatterlist *sg;
862*4882a593Smuzhiyun };
863*4882a593Smuzhiyun 
pipe_to_sg(struct pipe_inode_info * pipe,struct pipe_buffer * buf,struct splice_desc * sd)864*4882a593Smuzhiyun static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
865*4882a593Smuzhiyun 			struct splice_desc *sd)
866*4882a593Smuzhiyun {
867*4882a593Smuzhiyun 	struct sg_list *sgl = sd->u.data;
868*4882a593Smuzhiyun 	unsigned int offset, len;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	if (sgl->n == sgl->size)
871*4882a593Smuzhiyun 		return 0;
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	/* Try lock this page */
874*4882a593Smuzhiyun 	if (pipe_buf_try_steal(pipe, buf)) {
875*4882a593Smuzhiyun 		/* Get reference and unlock page for moving */
876*4882a593Smuzhiyun 		get_page(buf->page);
877*4882a593Smuzhiyun 		unlock_page(buf->page);
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 		len = min(buf->len, sd->len);
880*4882a593Smuzhiyun 		sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
881*4882a593Smuzhiyun 	} else {
882*4882a593Smuzhiyun 		/* Failback to copying a page */
883*4882a593Smuzhiyun 		struct page *page = alloc_page(GFP_KERNEL);
884*4882a593Smuzhiyun 		char *src;
885*4882a593Smuzhiyun 
886*4882a593Smuzhiyun 		if (!page)
887*4882a593Smuzhiyun 			return -ENOMEM;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 		offset = sd->pos & ~PAGE_MASK;
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 		len = sd->len;
892*4882a593Smuzhiyun 		if (len + offset > PAGE_SIZE)
893*4882a593Smuzhiyun 			len = PAGE_SIZE - offset;
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 		src = kmap_atomic(buf->page);
896*4882a593Smuzhiyun 		memcpy(page_address(page) + offset, src + buf->offset, len);
897*4882a593Smuzhiyun 		kunmap_atomic(src);
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 		sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 	sgl->n++;
902*4882a593Smuzhiyun 	sgl->len += len;
903*4882a593Smuzhiyun 
904*4882a593Smuzhiyun 	return len;
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun /* 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)908*4882a593Smuzhiyun static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
909*4882a593Smuzhiyun 				      struct file *filp, loff_t *ppos,
910*4882a593Smuzhiyun 				      size_t len, unsigned int flags)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	struct port *port = filp->private_data;
913*4882a593Smuzhiyun 	struct sg_list sgl;
914*4882a593Smuzhiyun 	ssize_t ret;
915*4882a593Smuzhiyun 	struct port_buffer *buf;
916*4882a593Smuzhiyun 	struct splice_desc sd = {
917*4882a593Smuzhiyun 		.total_len = len,
918*4882a593Smuzhiyun 		.flags = flags,
919*4882a593Smuzhiyun 		.pos = *ppos,
920*4882a593Smuzhiyun 		.u.data = &sgl,
921*4882a593Smuzhiyun 	};
922*4882a593Smuzhiyun 	unsigned int occupancy;
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	/*
925*4882a593Smuzhiyun 	 * Rproc_serial does not yet support splice. To support splice
926*4882a593Smuzhiyun 	 * pipe_to_sg() must allocate dma-buffers and copy content from
927*4882a593Smuzhiyun 	 * regular pages to dma pages. And alloc_buf and free_buf must
928*4882a593Smuzhiyun 	 * support allocating and freeing such a list of dma-buffers.
929*4882a593Smuzhiyun 	 */
930*4882a593Smuzhiyun 	if (is_rproc_serial(port->out_vq->vdev))
931*4882a593Smuzhiyun 		return -EINVAL;
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	pipe_lock(pipe);
934*4882a593Smuzhiyun 	ret = 0;
935*4882a593Smuzhiyun 	if (pipe_empty(pipe->head, pipe->tail))
936*4882a593Smuzhiyun 		goto error_out;
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
939*4882a593Smuzhiyun 	if (ret < 0)
940*4882a593Smuzhiyun 		goto error_out;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun 	occupancy = pipe_occupancy(pipe->head, pipe->tail);
943*4882a593Smuzhiyun 	buf = alloc_buf(port->portdev->vdev, 0, occupancy);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	if (!buf) {
946*4882a593Smuzhiyun 		ret = -ENOMEM;
947*4882a593Smuzhiyun 		goto error_out;
948*4882a593Smuzhiyun 	}
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	sgl.n = 0;
951*4882a593Smuzhiyun 	sgl.len = 0;
952*4882a593Smuzhiyun 	sgl.size = occupancy;
953*4882a593Smuzhiyun 	sgl.sg = buf->sg;
954*4882a593Smuzhiyun 	sg_init_table(sgl.sg, sgl.size);
955*4882a593Smuzhiyun 	ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
956*4882a593Smuzhiyun 	pipe_unlock(pipe);
957*4882a593Smuzhiyun 	if (likely(ret > 0))
958*4882a593Smuzhiyun 		ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	if (unlikely(ret <= 0))
961*4882a593Smuzhiyun 		free_buf(buf, true);
962*4882a593Smuzhiyun 	return ret;
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun error_out:
965*4882a593Smuzhiyun 	pipe_unlock(pipe);
966*4882a593Smuzhiyun 	return ret;
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun 
port_fops_poll(struct file * filp,poll_table * wait)969*4882a593Smuzhiyun static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun 	struct port *port;
972*4882a593Smuzhiyun 	__poll_t ret;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	port = filp->private_data;
975*4882a593Smuzhiyun 	poll_wait(filp, &port->waitqueue, wait);
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 	if (!port->guest_connected) {
978*4882a593Smuzhiyun 		/* Port got unplugged */
979*4882a593Smuzhiyun 		return EPOLLHUP;
980*4882a593Smuzhiyun 	}
981*4882a593Smuzhiyun 	ret = 0;
982*4882a593Smuzhiyun 	if (!will_read_block(port))
983*4882a593Smuzhiyun 		ret |= EPOLLIN | EPOLLRDNORM;
984*4882a593Smuzhiyun 	if (!will_write_block(port))
985*4882a593Smuzhiyun 		ret |= EPOLLOUT;
986*4882a593Smuzhiyun 	if (!port->host_connected)
987*4882a593Smuzhiyun 		ret |= EPOLLHUP;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	return ret;
990*4882a593Smuzhiyun }
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun static void remove_port(struct kref *kref);
993*4882a593Smuzhiyun 
port_fops_release(struct inode * inode,struct file * filp)994*4882a593Smuzhiyun static int port_fops_release(struct inode *inode, struct file *filp)
995*4882a593Smuzhiyun {
996*4882a593Smuzhiyun 	struct port *port;
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun 	port = filp->private_data;
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	/* Notify host of port being closed */
1001*4882a593Smuzhiyun 	send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	spin_lock_irq(&port->inbuf_lock);
1004*4882a593Smuzhiyun 	port->guest_connected = false;
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	discard_port_data(port);
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	spin_unlock_irq(&port->inbuf_lock);
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	spin_lock_irq(&port->outvq_lock);
1011*4882a593Smuzhiyun 	reclaim_consumed_buffers(port);
1012*4882a593Smuzhiyun 	spin_unlock_irq(&port->outvq_lock);
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	reclaim_dma_bufs();
1015*4882a593Smuzhiyun 	/*
1016*4882a593Smuzhiyun 	 * Locks aren't necessary here as a port can't be opened after
1017*4882a593Smuzhiyun 	 * unplug, and if a port isn't unplugged, a kref would already
1018*4882a593Smuzhiyun 	 * exist for the port.  Plus, taking ports_lock here would
1019*4882a593Smuzhiyun 	 * create a dependency on other locks taken by functions
1020*4882a593Smuzhiyun 	 * inside remove_port if we're the last holder of the port,
1021*4882a593Smuzhiyun 	 * creating many problems.
1022*4882a593Smuzhiyun 	 */
1023*4882a593Smuzhiyun 	kref_put(&port->kref, remove_port);
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun 	return 0;
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
port_fops_open(struct inode * inode,struct file * filp)1028*4882a593Smuzhiyun static int port_fops_open(struct inode *inode, struct file *filp)
1029*4882a593Smuzhiyun {
1030*4882a593Smuzhiyun 	struct cdev *cdev = inode->i_cdev;
1031*4882a593Smuzhiyun 	struct port *port;
1032*4882a593Smuzhiyun 	int ret;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	/* We get the port with a kref here */
1035*4882a593Smuzhiyun 	port = find_port_by_devt(cdev->dev);
1036*4882a593Smuzhiyun 	if (!port) {
1037*4882a593Smuzhiyun 		/* Port was unplugged before we could proceed */
1038*4882a593Smuzhiyun 		return -ENXIO;
1039*4882a593Smuzhiyun 	}
1040*4882a593Smuzhiyun 	filp->private_data = port;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	/*
1043*4882a593Smuzhiyun 	 * Don't allow opening of console port devices -- that's done
1044*4882a593Smuzhiyun 	 * via /dev/hvc
1045*4882a593Smuzhiyun 	 */
1046*4882a593Smuzhiyun 	if (is_console_port(port)) {
1047*4882a593Smuzhiyun 		ret = -ENXIO;
1048*4882a593Smuzhiyun 		goto out;
1049*4882a593Smuzhiyun 	}
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	/* Allow only one process to open a particular port at a time */
1052*4882a593Smuzhiyun 	spin_lock_irq(&port->inbuf_lock);
1053*4882a593Smuzhiyun 	if (port->guest_connected) {
1054*4882a593Smuzhiyun 		spin_unlock_irq(&port->inbuf_lock);
1055*4882a593Smuzhiyun 		ret = -EBUSY;
1056*4882a593Smuzhiyun 		goto out;
1057*4882a593Smuzhiyun 	}
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	port->guest_connected = true;
1060*4882a593Smuzhiyun 	spin_unlock_irq(&port->inbuf_lock);
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	spin_lock_irq(&port->outvq_lock);
1063*4882a593Smuzhiyun 	/*
1064*4882a593Smuzhiyun 	 * There might be a chance that we missed reclaiming a few
1065*4882a593Smuzhiyun 	 * buffers in the window of the port getting previously closed
1066*4882a593Smuzhiyun 	 * and opening now.
1067*4882a593Smuzhiyun 	 */
1068*4882a593Smuzhiyun 	reclaim_consumed_buffers(port);
1069*4882a593Smuzhiyun 	spin_unlock_irq(&port->outvq_lock);
1070*4882a593Smuzhiyun 
1071*4882a593Smuzhiyun 	nonseekable_open(inode, filp);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	/* Notify host of port being opened */
1074*4882a593Smuzhiyun 	send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	return 0;
1077*4882a593Smuzhiyun out:
1078*4882a593Smuzhiyun 	kref_put(&port->kref, remove_port);
1079*4882a593Smuzhiyun 	return ret;
1080*4882a593Smuzhiyun }
1081*4882a593Smuzhiyun 
port_fops_fasync(int fd,struct file * filp,int mode)1082*4882a593Smuzhiyun static int port_fops_fasync(int fd, struct file *filp, int mode)
1083*4882a593Smuzhiyun {
1084*4882a593Smuzhiyun 	struct port *port;
1085*4882a593Smuzhiyun 
1086*4882a593Smuzhiyun 	port = filp->private_data;
1087*4882a593Smuzhiyun 	return fasync_helper(fd, filp, mode, &port->async_queue);
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun /*
1091*4882a593Smuzhiyun  * The file operations that we support: programs in the guest can open
1092*4882a593Smuzhiyun  * a console device, read from it, write to it, poll for data and
1093*4882a593Smuzhiyun  * close it.  The devices are at
1094*4882a593Smuzhiyun  *   /dev/vport<device number>p<port number>
1095*4882a593Smuzhiyun  */
1096*4882a593Smuzhiyun static const struct file_operations port_fops = {
1097*4882a593Smuzhiyun 	.owner = THIS_MODULE,
1098*4882a593Smuzhiyun 	.open  = port_fops_open,
1099*4882a593Smuzhiyun 	.read  = port_fops_read,
1100*4882a593Smuzhiyun 	.write = port_fops_write,
1101*4882a593Smuzhiyun 	.splice_write = port_fops_splice_write,
1102*4882a593Smuzhiyun 	.poll  = port_fops_poll,
1103*4882a593Smuzhiyun 	.release = port_fops_release,
1104*4882a593Smuzhiyun 	.fasync = port_fops_fasync,
1105*4882a593Smuzhiyun 	.llseek = no_llseek,
1106*4882a593Smuzhiyun };
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun /*
1109*4882a593Smuzhiyun  * The put_chars() callback is pretty straightforward.
1110*4882a593Smuzhiyun  *
1111*4882a593Smuzhiyun  * We turn the characters into a scatter-gather list, add it to the
1112*4882a593Smuzhiyun  * output queue and then kick the Host.  Then we sit here waiting for
1113*4882a593Smuzhiyun  * it to finish: inefficient in theory, but in practice
1114*4882a593Smuzhiyun  * implementations will do it immediately.
1115*4882a593Smuzhiyun  */
put_chars(u32 vtermno,const char * buf,int count)1116*4882a593Smuzhiyun static int put_chars(u32 vtermno, const char *buf, int count)
1117*4882a593Smuzhiyun {
1118*4882a593Smuzhiyun 	struct port *port;
1119*4882a593Smuzhiyun 	struct scatterlist sg[1];
1120*4882a593Smuzhiyun 	void *data;
1121*4882a593Smuzhiyun 	int ret;
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	if (unlikely(early_put_chars))
1124*4882a593Smuzhiyun 		return early_put_chars(vtermno, buf, count);
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	port = find_port_by_vtermno(vtermno);
1127*4882a593Smuzhiyun 	if (!port)
1128*4882a593Smuzhiyun 		return -EPIPE;
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	data = kmemdup(buf, count, GFP_ATOMIC);
1131*4882a593Smuzhiyun 	if (!data)
1132*4882a593Smuzhiyun 		return -ENOMEM;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	sg_init_one(sg, data, count);
1135*4882a593Smuzhiyun 	ret = __send_to_port(port, sg, 1, count, data, false);
1136*4882a593Smuzhiyun 	kfree(data);
1137*4882a593Smuzhiyun 	return ret;
1138*4882a593Smuzhiyun }
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun /*
1141*4882a593Smuzhiyun  * get_chars() is the callback from the hvc_console infrastructure
1142*4882a593Smuzhiyun  * when an interrupt is received.
1143*4882a593Smuzhiyun  *
1144*4882a593Smuzhiyun  * We call out to fill_readbuf that gets us the required data from the
1145*4882a593Smuzhiyun  * buffers that are queued up.
1146*4882a593Smuzhiyun  */
get_chars(u32 vtermno,char * buf,int count)1147*4882a593Smuzhiyun static int get_chars(u32 vtermno, char *buf, int count)
1148*4882a593Smuzhiyun {
1149*4882a593Smuzhiyun 	struct port *port;
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	/* If we've not set up the port yet, we have no input to give. */
1152*4882a593Smuzhiyun 	if (unlikely(early_put_chars))
1153*4882a593Smuzhiyun 		return 0;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	port = find_port_by_vtermno(vtermno);
1156*4882a593Smuzhiyun 	if (!port)
1157*4882a593Smuzhiyun 		return -EPIPE;
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	/* If we don't have an input queue yet, we can't get input. */
1160*4882a593Smuzhiyun 	BUG_ON(!port->in_vq);
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	return fill_readbuf(port, (__force char __user *)buf, count, false);
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun 
resize_console(struct port * port)1165*4882a593Smuzhiyun static void resize_console(struct port *port)
1166*4882a593Smuzhiyun {
1167*4882a593Smuzhiyun 	struct virtio_device *vdev;
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	/* The port could have been hot-unplugged */
1170*4882a593Smuzhiyun 	if (!port || !is_console_port(port))
1171*4882a593Smuzhiyun 		return;
1172*4882a593Smuzhiyun 
1173*4882a593Smuzhiyun 	vdev = port->portdev->vdev;
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	/* Don't test F_SIZE at all if we're rproc: not a valid feature! */
1176*4882a593Smuzhiyun 	if (!is_rproc_serial(vdev) &&
1177*4882a593Smuzhiyun 	    virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1178*4882a593Smuzhiyun 		hvc_resize(port->cons.hvc, port->cons.ws);
1179*4882a593Smuzhiyun }
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun /* We set the configuration at this point, since we now have a tty */
notifier_add_vio(struct hvc_struct * hp,int data)1182*4882a593Smuzhiyun static int notifier_add_vio(struct hvc_struct *hp, int data)
1183*4882a593Smuzhiyun {
1184*4882a593Smuzhiyun 	struct port *port;
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	port = find_port_by_vtermno(hp->vtermno);
1187*4882a593Smuzhiyun 	if (!port)
1188*4882a593Smuzhiyun 		return -EINVAL;
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	hp->irq_requested = 1;
1191*4882a593Smuzhiyun 	resize_console(port);
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 	return 0;
1194*4882a593Smuzhiyun }
1195*4882a593Smuzhiyun 
notifier_del_vio(struct hvc_struct * hp,int data)1196*4882a593Smuzhiyun static void notifier_del_vio(struct hvc_struct *hp, int data)
1197*4882a593Smuzhiyun {
1198*4882a593Smuzhiyun 	hp->irq_requested = 0;
1199*4882a593Smuzhiyun }
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun /* The operations for console ports. */
1202*4882a593Smuzhiyun static const struct hv_ops hv_ops = {
1203*4882a593Smuzhiyun 	.get_chars = get_chars,
1204*4882a593Smuzhiyun 	.put_chars = put_chars,
1205*4882a593Smuzhiyun 	.notifier_add = notifier_add_vio,
1206*4882a593Smuzhiyun 	.notifier_del = notifier_del_vio,
1207*4882a593Smuzhiyun 	.notifier_hangup = notifier_del_vio,
1208*4882a593Smuzhiyun };
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun /*
1211*4882a593Smuzhiyun  * Console drivers are initialized very early so boot messages can go
1212*4882a593Smuzhiyun  * out, so we do things slightly differently from the generic virtio
1213*4882a593Smuzhiyun  * initialization of the net and block drivers.
1214*4882a593Smuzhiyun  *
1215*4882a593Smuzhiyun  * At this stage, the console is output-only.  It's too early to set
1216*4882a593Smuzhiyun  * up a virtqueue, so we let the drivers do some boutique early-output
1217*4882a593Smuzhiyun  * thing.
1218*4882a593Smuzhiyun  */
virtio_cons_early_init(int (* put_chars)(u32,const char *,int))1219*4882a593Smuzhiyun int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
1220*4882a593Smuzhiyun {
1221*4882a593Smuzhiyun 	early_put_chars = put_chars;
1222*4882a593Smuzhiyun 	return hvc_instantiate(0, 0, &hv_ops);
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun 
init_port_console(struct port * port)1225*4882a593Smuzhiyun static int init_port_console(struct port *port)
1226*4882a593Smuzhiyun {
1227*4882a593Smuzhiyun 	int ret;
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	/*
1230*4882a593Smuzhiyun 	 * The Host's telling us this port is a console port.  Hook it
1231*4882a593Smuzhiyun 	 * up with an hvc console.
1232*4882a593Smuzhiyun 	 *
1233*4882a593Smuzhiyun 	 * To set up and manage our virtual console, we call
1234*4882a593Smuzhiyun 	 * hvc_alloc().
1235*4882a593Smuzhiyun 	 *
1236*4882a593Smuzhiyun 	 * The first argument of hvc_alloc() is the virtual console
1237*4882a593Smuzhiyun 	 * number.  The second argument is the parameter for the
1238*4882a593Smuzhiyun 	 * notification mechanism (like irq number).  We currently
1239*4882a593Smuzhiyun 	 * leave this as zero, virtqueues have implicit notifications.
1240*4882a593Smuzhiyun 	 *
1241*4882a593Smuzhiyun 	 * The third argument is a "struct hv_ops" containing the
1242*4882a593Smuzhiyun 	 * put_chars() get_chars(), notifier_add() and notifier_del()
1243*4882a593Smuzhiyun 	 * pointers.  The final argument is the output buffer size: we
1244*4882a593Smuzhiyun 	 * can do any size, so we put PAGE_SIZE here.
1245*4882a593Smuzhiyun 	 */
1246*4882a593Smuzhiyun 	port->cons.vtermno = pdrvdata.next_vtermno;
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun 	port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1249*4882a593Smuzhiyun 	if (IS_ERR(port->cons.hvc)) {
1250*4882a593Smuzhiyun 		ret = PTR_ERR(port->cons.hvc);
1251*4882a593Smuzhiyun 		dev_err(port->dev,
1252*4882a593Smuzhiyun 			"error %d allocating hvc for port\n", ret);
1253*4882a593Smuzhiyun 		port->cons.hvc = NULL;
1254*4882a593Smuzhiyun 		return ret;
1255*4882a593Smuzhiyun 	}
1256*4882a593Smuzhiyun 	spin_lock_irq(&pdrvdata_lock);
1257*4882a593Smuzhiyun 	pdrvdata.next_vtermno++;
1258*4882a593Smuzhiyun 	list_add_tail(&port->cons.list, &pdrvdata.consoles);
1259*4882a593Smuzhiyun 	spin_unlock_irq(&pdrvdata_lock);
1260*4882a593Smuzhiyun 	port->guest_connected = true;
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	/*
1263*4882a593Smuzhiyun 	 * Start using the new console output if this is the first
1264*4882a593Smuzhiyun 	 * console to come up.
1265*4882a593Smuzhiyun 	 */
1266*4882a593Smuzhiyun 	if (early_put_chars)
1267*4882a593Smuzhiyun 		early_put_chars = NULL;
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	/* Notify host of port being opened */
1270*4882a593Smuzhiyun 	send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	return 0;
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun 
show_port_name(struct device * dev,struct device_attribute * attr,char * buffer)1275*4882a593Smuzhiyun static ssize_t show_port_name(struct device *dev,
1276*4882a593Smuzhiyun 			      struct device_attribute *attr, char *buffer)
1277*4882a593Smuzhiyun {
1278*4882a593Smuzhiyun 	struct port *port;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	port = dev_get_drvdata(dev);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 	return sprintf(buffer, "%s\n", port->name);
1283*4882a593Smuzhiyun }
1284*4882a593Smuzhiyun 
1285*4882a593Smuzhiyun static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun static struct attribute *port_sysfs_entries[] = {
1288*4882a593Smuzhiyun 	&dev_attr_name.attr,
1289*4882a593Smuzhiyun 	NULL
1290*4882a593Smuzhiyun };
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun static const struct attribute_group port_attribute_group = {
1293*4882a593Smuzhiyun 	.name = NULL,		/* put in device directory */
1294*4882a593Smuzhiyun 	.attrs = port_sysfs_entries,
1295*4882a593Smuzhiyun };
1296*4882a593Smuzhiyun 
port_debugfs_show(struct seq_file * s,void * data)1297*4882a593Smuzhiyun static int port_debugfs_show(struct seq_file *s, void *data)
1298*4882a593Smuzhiyun {
1299*4882a593Smuzhiyun 	struct port *port = s->private;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	seq_printf(s, "name: %s\n", port->name ? port->name : "");
1302*4882a593Smuzhiyun 	seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1303*4882a593Smuzhiyun 	seq_printf(s, "host_connected: %d\n", port->host_connected);
1304*4882a593Smuzhiyun 	seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1305*4882a593Smuzhiyun 	seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1306*4882a593Smuzhiyun 	seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1307*4882a593Smuzhiyun 	seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1308*4882a593Smuzhiyun 	seq_printf(s, "is_console: %s\n",
1309*4882a593Smuzhiyun 		   is_console_port(port) ? "yes" : "no");
1310*4882a593Smuzhiyun 	seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	return 0;
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun DEFINE_SHOW_ATTRIBUTE(port_debugfs);
1316*4882a593Smuzhiyun 
set_console_size(struct port * port,u16 rows,u16 cols)1317*4882a593Smuzhiyun static void set_console_size(struct port *port, u16 rows, u16 cols)
1318*4882a593Smuzhiyun {
1319*4882a593Smuzhiyun 	if (!port || !is_console_port(port))
1320*4882a593Smuzhiyun 		return;
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun 	port->cons.ws.ws_row = rows;
1323*4882a593Smuzhiyun 	port->cons.ws.ws_col = cols;
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun 
fill_queue(struct virtqueue * vq,spinlock_t * lock)1326*4882a593Smuzhiyun static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1327*4882a593Smuzhiyun {
1328*4882a593Smuzhiyun 	struct port_buffer *buf;
1329*4882a593Smuzhiyun 	int nr_added_bufs;
1330*4882a593Smuzhiyun 	int ret;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	nr_added_bufs = 0;
1333*4882a593Smuzhiyun 	do {
1334*4882a593Smuzhiyun 		buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
1335*4882a593Smuzhiyun 		if (!buf)
1336*4882a593Smuzhiyun 			return -ENOMEM;
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 		spin_lock_irq(lock);
1339*4882a593Smuzhiyun 		ret = add_inbuf(vq, buf);
1340*4882a593Smuzhiyun 		if (ret < 0) {
1341*4882a593Smuzhiyun 			spin_unlock_irq(lock);
1342*4882a593Smuzhiyun 			free_buf(buf, true);
1343*4882a593Smuzhiyun 			return ret;
1344*4882a593Smuzhiyun 		}
1345*4882a593Smuzhiyun 		nr_added_bufs++;
1346*4882a593Smuzhiyun 		spin_unlock_irq(lock);
1347*4882a593Smuzhiyun 	} while (ret > 0);
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 	return nr_added_bufs;
1350*4882a593Smuzhiyun }
1351*4882a593Smuzhiyun 
send_sigio_to_port(struct port * port)1352*4882a593Smuzhiyun static void send_sigio_to_port(struct port *port)
1353*4882a593Smuzhiyun {
1354*4882a593Smuzhiyun 	if (port->async_queue && port->guest_connected)
1355*4882a593Smuzhiyun 		kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1356*4882a593Smuzhiyun }
1357*4882a593Smuzhiyun 
add_port(struct ports_device * portdev,u32 id)1358*4882a593Smuzhiyun static int add_port(struct ports_device *portdev, u32 id)
1359*4882a593Smuzhiyun {
1360*4882a593Smuzhiyun 	char debugfs_name[16];
1361*4882a593Smuzhiyun 	struct port *port;
1362*4882a593Smuzhiyun 	dev_t devt;
1363*4882a593Smuzhiyun 	int err;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	port = kmalloc(sizeof(*port), GFP_KERNEL);
1366*4882a593Smuzhiyun 	if (!port) {
1367*4882a593Smuzhiyun 		err = -ENOMEM;
1368*4882a593Smuzhiyun 		goto fail;
1369*4882a593Smuzhiyun 	}
1370*4882a593Smuzhiyun 	kref_init(&port->kref);
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	port->portdev = portdev;
1373*4882a593Smuzhiyun 	port->id = id;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	port->name = NULL;
1376*4882a593Smuzhiyun 	port->inbuf = NULL;
1377*4882a593Smuzhiyun 	port->cons.hvc = NULL;
1378*4882a593Smuzhiyun 	port->async_queue = NULL;
1379*4882a593Smuzhiyun 
1380*4882a593Smuzhiyun 	port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1381*4882a593Smuzhiyun 	port->cons.vtermno = 0;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	port->host_connected = port->guest_connected = false;
1384*4882a593Smuzhiyun 	port->stats = (struct port_stats) { 0 };
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun 	port->outvq_full = false;
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	port->in_vq = portdev->in_vqs[port->id];
1389*4882a593Smuzhiyun 	port->out_vq = portdev->out_vqs[port->id];
1390*4882a593Smuzhiyun 
1391*4882a593Smuzhiyun 	port->cdev = cdev_alloc();
1392*4882a593Smuzhiyun 	if (!port->cdev) {
1393*4882a593Smuzhiyun 		dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1394*4882a593Smuzhiyun 		err = -ENOMEM;
1395*4882a593Smuzhiyun 		goto free_port;
1396*4882a593Smuzhiyun 	}
1397*4882a593Smuzhiyun 	port->cdev->ops = &port_fops;
1398*4882a593Smuzhiyun 
1399*4882a593Smuzhiyun 	devt = MKDEV(portdev->chr_major, id);
1400*4882a593Smuzhiyun 	err = cdev_add(port->cdev, devt, 1);
1401*4882a593Smuzhiyun 	if (err < 0) {
1402*4882a593Smuzhiyun 		dev_err(&port->portdev->vdev->dev,
1403*4882a593Smuzhiyun 			"Error %d adding cdev for port %u\n", err, id);
1404*4882a593Smuzhiyun 		goto free_cdev;
1405*4882a593Smuzhiyun 	}
1406*4882a593Smuzhiyun 	port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1407*4882a593Smuzhiyun 				  devt, port, "vport%up%u",
1408*4882a593Smuzhiyun 				  port->portdev->vdev->index, id);
1409*4882a593Smuzhiyun 	if (IS_ERR(port->dev)) {
1410*4882a593Smuzhiyun 		err = PTR_ERR(port->dev);
1411*4882a593Smuzhiyun 		dev_err(&port->portdev->vdev->dev,
1412*4882a593Smuzhiyun 			"Error %d creating device for port %u\n",
1413*4882a593Smuzhiyun 			err, id);
1414*4882a593Smuzhiyun 		goto free_cdev;
1415*4882a593Smuzhiyun 	}
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	spin_lock_init(&port->inbuf_lock);
1418*4882a593Smuzhiyun 	spin_lock_init(&port->outvq_lock);
1419*4882a593Smuzhiyun 	init_waitqueue_head(&port->waitqueue);
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	/* We can safely ignore ENOSPC because it means
1422*4882a593Smuzhiyun 	 * the queue already has buffers. Buffers are removed
1423*4882a593Smuzhiyun 	 * only by virtcons_remove(), not by unplug_port()
1424*4882a593Smuzhiyun 	 */
1425*4882a593Smuzhiyun 	err = fill_queue(port->in_vq, &port->inbuf_lock);
1426*4882a593Smuzhiyun 	if (err < 0 && err != -ENOSPC) {
1427*4882a593Smuzhiyun 		dev_err(port->dev, "Error allocating inbufs\n");
1428*4882a593Smuzhiyun 		goto free_device;
1429*4882a593Smuzhiyun 	}
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	if (is_rproc_serial(port->portdev->vdev))
1432*4882a593Smuzhiyun 		/*
1433*4882a593Smuzhiyun 		 * For rproc_serial assume remote processor is connected.
1434*4882a593Smuzhiyun 		 * rproc_serial does not want the console port, only
1435*4882a593Smuzhiyun 		 * the generic port implementation.
1436*4882a593Smuzhiyun 		 */
1437*4882a593Smuzhiyun 		port->host_connected = true;
1438*4882a593Smuzhiyun 	else if (!use_multiport(port->portdev)) {
1439*4882a593Smuzhiyun 		/*
1440*4882a593Smuzhiyun 		 * If we're not using multiport support,
1441*4882a593Smuzhiyun 		 * this has to be a console port.
1442*4882a593Smuzhiyun 		 */
1443*4882a593Smuzhiyun 		err = init_port_console(port);
1444*4882a593Smuzhiyun 		if (err)
1445*4882a593Smuzhiyun 			goto free_inbufs;
1446*4882a593Smuzhiyun 	}
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	spin_lock_irq(&portdev->ports_lock);
1449*4882a593Smuzhiyun 	list_add_tail(&port->list, &port->portdev->ports);
1450*4882a593Smuzhiyun 	spin_unlock_irq(&portdev->ports_lock);
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun 	/*
1453*4882a593Smuzhiyun 	 * Tell the Host we're set so that it can send us various
1454*4882a593Smuzhiyun 	 * configuration parameters for this port (eg, port name,
1455*4882a593Smuzhiyun 	 * caching, whether this is a console port, etc.)
1456*4882a593Smuzhiyun 	 */
1457*4882a593Smuzhiyun 	send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 	if (pdrvdata.debugfs_dir) {
1460*4882a593Smuzhiyun 		/*
1461*4882a593Smuzhiyun 		 * Finally, create the debugfs file that we can use to
1462*4882a593Smuzhiyun 		 * inspect a port's state at any time
1463*4882a593Smuzhiyun 		 */
1464*4882a593Smuzhiyun 		snprintf(debugfs_name, sizeof(debugfs_name), "vport%up%u",
1465*4882a593Smuzhiyun 			 port->portdev->vdev->index, id);
1466*4882a593Smuzhiyun 		port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1467*4882a593Smuzhiyun 							 pdrvdata.debugfs_dir,
1468*4882a593Smuzhiyun 							 port,
1469*4882a593Smuzhiyun 							 &port_debugfs_fops);
1470*4882a593Smuzhiyun 	}
1471*4882a593Smuzhiyun 	return 0;
1472*4882a593Smuzhiyun 
1473*4882a593Smuzhiyun free_inbufs:
1474*4882a593Smuzhiyun free_device:
1475*4882a593Smuzhiyun 	device_destroy(pdrvdata.class, port->dev->devt);
1476*4882a593Smuzhiyun free_cdev:
1477*4882a593Smuzhiyun 	cdev_del(port->cdev);
1478*4882a593Smuzhiyun free_port:
1479*4882a593Smuzhiyun 	kfree(port);
1480*4882a593Smuzhiyun fail:
1481*4882a593Smuzhiyun 	/* The host might want to notify management sw about port add failure */
1482*4882a593Smuzhiyun 	__send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1483*4882a593Smuzhiyun 	return err;
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun /* No users remain, remove all port-specific data. */
remove_port(struct kref * kref)1487*4882a593Smuzhiyun static void remove_port(struct kref *kref)
1488*4882a593Smuzhiyun {
1489*4882a593Smuzhiyun 	struct port *port;
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun 	port = container_of(kref, struct port, kref);
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	kfree(port);
1494*4882a593Smuzhiyun }
1495*4882a593Smuzhiyun 
remove_port_data(struct port * port)1496*4882a593Smuzhiyun static void remove_port_data(struct port *port)
1497*4882a593Smuzhiyun {
1498*4882a593Smuzhiyun 	spin_lock_irq(&port->inbuf_lock);
1499*4882a593Smuzhiyun 	/* Remove unused data this port might have received. */
1500*4882a593Smuzhiyun 	discard_port_data(port);
1501*4882a593Smuzhiyun 	spin_unlock_irq(&port->inbuf_lock);
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	spin_lock_irq(&port->outvq_lock);
1504*4882a593Smuzhiyun 	reclaim_consumed_buffers(port);
1505*4882a593Smuzhiyun 	spin_unlock_irq(&port->outvq_lock);
1506*4882a593Smuzhiyun }
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun /*
1509*4882a593Smuzhiyun  * Port got unplugged.  Remove port from portdev's list and drop the
1510*4882a593Smuzhiyun  * kref reference.  If no userspace has this port opened, it will
1511*4882a593Smuzhiyun  * result in immediate removal the port.
1512*4882a593Smuzhiyun  */
unplug_port(struct port * port)1513*4882a593Smuzhiyun static void unplug_port(struct port *port)
1514*4882a593Smuzhiyun {
1515*4882a593Smuzhiyun 	spin_lock_irq(&port->portdev->ports_lock);
1516*4882a593Smuzhiyun 	list_del(&port->list);
1517*4882a593Smuzhiyun 	spin_unlock_irq(&port->portdev->ports_lock);
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 	spin_lock_irq(&port->inbuf_lock);
1520*4882a593Smuzhiyun 	if (port->guest_connected) {
1521*4882a593Smuzhiyun 		/* Let the app know the port is going down. */
1522*4882a593Smuzhiyun 		send_sigio_to_port(port);
1523*4882a593Smuzhiyun 
1524*4882a593Smuzhiyun 		/* Do this after sigio is actually sent */
1525*4882a593Smuzhiyun 		port->guest_connected = false;
1526*4882a593Smuzhiyun 		port->host_connected = false;
1527*4882a593Smuzhiyun 
1528*4882a593Smuzhiyun 		wake_up_interruptible(&port->waitqueue);
1529*4882a593Smuzhiyun 	}
1530*4882a593Smuzhiyun 	spin_unlock_irq(&port->inbuf_lock);
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	if (is_console_port(port)) {
1533*4882a593Smuzhiyun 		spin_lock_irq(&pdrvdata_lock);
1534*4882a593Smuzhiyun 		list_del(&port->cons.list);
1535*4882a593Smuzhiyun 		spin_unlock_irq(&pdrvdata_lock);
1536*4882a593Smuzhiyun 		hvc_remove(port->cons.hvc);
1537*4882a593Smuzhiyun 	}
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	remove_port_data(port);
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	/*
1542*4882a593Smuzhiyun 	 * We should just assume the device itself has gone off --
1543*4882a593Smuzhiyun 	 * else a close on an open port later will try to send out a
1544*4882a593Smuzhiyun 	 * control message.
1545*4882a593Smuzhiyun 	 */
1546*4882a593Smuzhiyun 	port->portdev = NULL;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1549*4882a593Smuzhiyun 	device_destroy(pdrvdata.class, port->dev->devt);
1550*4882a593Smuzhiyun 	cdev_del(port->cdev);
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 	debugfs_remove(port->debugfs_file);
1553*4882a593Smuzhiyun 	kfree(port->name);
1554*4882a593Smuzhiyun 
1555*4882a593Smuzhiyun 	/*
1556*4882a593Smuzhiyun 	 * Locks around here are not necessary - a port can't be
1557*4882a593Smuzhiyun 	 * opened after we removed the port struct from ports_list
1558*4882a593Smuzhiyun 	 * above.
1559*4882a593Smuzhiyun 	 */
1560*4882a593Smuzhiyun 	kref_put(&port->kref, remove_port);
1561*4882a593Smuzhiyun }
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun /* 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)1564*4882a593Smuzhiyun static void handle_control_message(struct virtio_device *vdev,
1565*4882a593Smuzhiyun 				   struct ports_device *portdev,
1566*4882a593Smuzhiyun 				   struct port_buffer *buf)
1567*4882a593Smuzhiyun {
1568*4882a593Smuzhiyun 	struct virtio_console_control *cpkt;
1569*4882a593Smuzhiyun 	struct port *port;
1570*4882a593Smuzhiyun 	size_t name_size;
1571*4882a593Smuzhiyun 	int err;
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 	port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1576*4882a593Smuzhiyun 	if (!port &&
1577*4882a593Smuzhiyun 	    cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
1578*4882a593Smuzhiyun 		/* No valid header at start of buffer.  Drop it. */
1579*4882a593Smuzhiyun 		dev_dbg(&portdev->vdev->dev,
1580*4882a593Smuzhiyun 			"Invalid index %u in control packet\n", cpkt->id);
1581*4882a593Smuzhiyun 		return;
1582*4882a593Smuzhiyun 	}
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	switch (virtio16_to_cpu(vdev, cpkt->event)) {
1585*4882a593Smuzhiyun 	case VIRTIO_CONSOLE_PORT_ADD:
1586*4882a593Smuzhiyun 		if (port) {
1587*4882a593Smuzhiyun 			dev_dbg(&portdev->vdev->dev,
1588*4882a593Smuzhiyun 				"Port %u already added\n", port->id);
1589*4882a593Smuzhiyun 			send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1590*4882a593Smuzhiyun 			break;
1591*4882a593Smuzhiyun 		}
1592*4882a593Smuzhiyun 		if (virtio32_to_cpu(vdev, cpkt->id) >=
1593*4882a593Smuzhiyun 		    portdev->max_nr_ports) {
1594*4882a593Smuzhiyun 			dev_warn(&portdev->vdev->dev,
1595*4882a593Smuzhiyun 				"Request for adding port with "
1596*4882a593Smuzhiyun 				"out-of-bound id %u, max. supported id: %u\n",
1597*4882a593Smuzhiyun 				cpkt->id, portdev->max_nr_ports - 1);
1598*4882a593Smuzhiyun 			break;
1599*4882a593Smuzhiyun 		}
1600*4882a593Smuzhiyun 		add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
1601*4882a593Smuzhiyun 		break;
1602*4882a593Smuzhiyun 	case VIRTIO_CONSOLE_PORT_REMOVE:
1603*4882a593Smuzhiyun 		unplug_port(port);
1604*4882a593Smuzhiyun 		break;
1605*4882a593Smuzhiyun 	case VIRTIO_CONSOLE_CONSOLE_PORT:
1606*4882a593Smuzhiyun 		if (!cpkt->value)
1607*4882a593Smuzhiyun 			break;
1608*4882a593Smuzhiyun 		if (is_console_port(port))
1609*4882a593Smuzhiyun 			break;
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 		init_port_console(port);
1612*4882a593Smuzhiyun 		complete(&early_console_added);
1613*4882a593Smuzhiyun 		/*
1614*4882a593Smuzhiyun 		 * Could remove the port here in case init fails - but
1615*4882a593Smuzhiyun 		 * have to notify the host first.
1616*4882a593Smuzhiyun 		 */
1617*4882a593Smuzhiyun 		break;
1618*4882a593Smuzhiyun 	case VIRTIO_CONSOLE_RESIZE: {
1619*4882a593Smuzhiyun 		struct {
1620*4882a593Smuzhiyun 			__u16 rows;
1621*4882a593Smuzhiyun 			__u16 cols;
1622*4882a593Smuzhiyun 		} size;
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun 		if (!is_console_port(port))
1625*4882a593Smuzhiyun 			break;
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun 		memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1628*4882a593Smuzhiyun 		       sizeof(size));
1629*4882a593Smuzhiyun 		set_console_size(port, size.rows, size.cols);
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun 		port->cons.hvc->irq_requested = 1;
1632*4882a593Smuzhiyun 		resize_console(port);
1633*4882a593Smuzhiyun 		break;
1634*4882a593Smuzhiyun 	}
1635*4882a593Smuzhiyun 	case VIRTIO_CONSOLE_PORT_OPEN:
1636*4882a593Smuzhiyun 		port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
1637*4882a593Smuzhiyun 		wake_up_interruptible(&port->waitqueue);
1638*4882a593Smuzhiyun 		/*
1639*4882a593Smuzhiyun 		 * If the host port got closed and the host had any
1640*4882a593Smuzhiyun 		 * unconsumed buffers, we'll be able to reclaim them
1641*4882a593Smuzhiyun 		 * now.
1642*4882a593Smuzhiyun 		 */
1643*4882a593Smuzhiyun 		spin_lock_irq(&port->outvq_lock);
1644*4882a593Smuzhiyun 		reclaim_consumed_buffers(port);
1645*4882a593Smuzhiyun 		spin_unlock_irq(&port->outvq_lock);
1646*4882a593Smuzhiyun 
1647*4882a593Smuzhiyun 		/*
1648*4882a593Smuzhiyun 		 * If the guest is connected, it'll be interested in
1649*4882a593Smuzhiyun 		 * knowing the host connection state changed.
1650*4882a593Smuzhiyun 		 */
1651*4882a593Smuzhiyun 		spin_lock_irq(&port->inbuf_lock);
1652*4882a593Smuzhiyun 		send_sigio_to_port(port);
1653*4882a593Smuzhiyun 		spin_unlock_irq(&port->inbuf_lock);
1654*4882a593Smuzhiyun 		break;
1655*4882a593Smuzhiyun 	case VIRTIO_CONSOLE_PORT_NAME:
1656*4882a593Smuzhiyun 		/*
1657*4882a593Smuzhiyun 		 * If we woke up after hibernation, we can get this
1658*4882a593Smuzhiyun 		 * again.  Skip it in that case.
1659*4882a593Smuzhiyun 		 */
1660*4882a593Smuzhiyun 		if (port->name)
1661*4882a593Smuzhiyun 			break;
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun 		/*
1664*4882a593Smuzhiyun 		 * Skip the size of the header and the cpkt to get the size
1665*4882a593Smuzhiyun 		 * of the name that was sent
1666*4882a593Smuzhiyun 		 */
1667*4882a593Smuzhiyun 		name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 		port->name = kmalloc(name_size, GFP_KERNEL);
1670*4882a593Smuzhiyun 		if (!port->name) {
1671*4882a593Smuzhiyun 			dev_err(port->dev,
1672*4882a593Smuzhiyun 				"Not enough space to store port name\n");
1673*4882a593Smuzhiyun 			break;
1674*4882a593Smuzhiyun 		}
1675*4882a593Smuzhiyun 		strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1676*4882a593Smuzhiyun 			name_size - 1);
1677*4882a593Smuzhiyun 		port->name[name_size - 1] = 0;
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun 		/*
1680*4882a593Smuzhiyun 		 * Since we only have one sysfs attribute, 'name',
1681*4882a593Smuzhiyun 		 * create it only if we have a name for the port.
1682*4882a593Smuzhiyun 		 */
1683*4882a593Smuzhiyun 		err = sysfs_create_group(&port->dev->kobj,
1684*4882a593Smuzhiyun 					 &port_attribute_group);
1685*4882a593Smuzhiyun 		if (err) {
1686*4882a593Smuzhiyun 			dev_err(port->dev,
1687*4882a593Smuzhiyun 				"Error %d creating sysfs device attributes\n",
1688*4882a593Smuzhiyun 				err);
1689*4882a593Smuzhiyun 		} else {
1690*4882a593Smuzhiyun 			/*
1691*4882a593Smuzhiyun 			 * Generate a udev event so that appropriate
1692*4882a593Smuzhiyun 			 * symlinks can be created based on udev
1693*4882a593Smuzhiyun 			 * rules.
1694*4882a593Smuzhiyun 			 */
1695*4882a593Smuzhiyun 			kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1696*4882a593Smuzhiyun 		}
1697*4882a593Smuzhiyun 		break;
1698*4882a593Smuzhiyun 	}
1699*4882a593Smuzhiyun }
1700*4882a593Smuzhiyun 
control_work_handler(struct work_struct * work)1701*4882a593Smuzhiyun static void control_work_handler(struct work_struct *work)
1702*4882a593Smuzhiyun {
1703*4882a593Smuzhiyun 	struct ports_device *portdev;
1704*4882a593Smuzhiyun 	struct virtqueue *vq;
1705*4882a593Smuzhiyun 	struct port_buffer *buf;
1706*4882a593Smuzhiyun 	unsigned int len;
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun 	portdev = container_of(work, struct ports_device, control_work);
1709*4882a593Smuzhiyun 	vq = portdev->c_ivq;
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun 	spin_lock(&portdev->c_ivq_lock);
1712*4882a593Smuzhiyun 	while ((buf = virtqueue_get_buf(vq, &len))) {
1713*4882a593Smuzhiyun 		spin_unlock(&portdev->c_ivq_lock);
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 		buf->len = min_t(size_t, len, buf->size);
1716*4882a593Smuzhiyun 		buf->offset = 0;
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 		handle_control_message(vq->vdev, portdev, buf);
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun 		spin_lock(&portdev->c_ivq_lock);
1721*4882a593Smuzhiyun 		if (add_inbuf(portdev->c_ivq, buf) < 0) {
1722*4882a593Smuzhiyun 			dev_warn(&portdev->vdev->dev,
1723*4882a593Smuzhiyun 				 "Error adding buffer to queue\n");
1724*4882a593Smuzhiyun 			free_buf(buf, false);
1725*4882a593Smuzhiyun 		}
1726*4882a593Smuzhiyun 	}
1727*4882a593Smuzhiyun 	spin_unlock(&portdev->c_ivq_lock);
1728*4882a593Smuzhiyun }
1729*4882a593Smuzhiyun 
flush_bufs(struct virtqueue * vq,bool can_sleep)1730*4882a593Smuzhiyun static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1731*4882a593Smuzhiyun {
1732*4882a593Smuzhiyun 	struct port_buffer *buf;
1733*4882a593Smuzhiyun 	unsigned int len;
1734*4882a593Smuzhiyun 
1735*4882a593Smuzhiyun 	while ((buf = virtqueue_get_buf(vq, &len)))
1736*4882a593Smuzhiyun 		free_buf(buf, can_sleep);
1737*4882a593Smuzhiyun }
1738*4882a593Smuzhiyun 
out_intr(struct virtqueue * vq)1739*4882a593Smuzhiyun static void out_intr(struct virtqueue *vq)
1740*4882a593Smuzhiyun {
1741*4882a593Smuzhiyun 	struct port *port;
1742*4882a593Smuzhiyun 
1743*4882a593Smuzhiyun 	port = find_port_by_vq(vq->vdev->priv, vq);
1744*4882a593Smuzhiyun 	if (!port) {
1745*4882a593Smuzhiyun 		flush_bufs(vq, false);
1746*4882a593Smuzhiyun 		return;
1747*4882a593Smuzhiyun 	}
1748*4882a593Smuzhiyun 
1749*4882a593Smuzhiyun 	wake_up_interruptible(&port->waitqueue);
1750*4882a593Smuzhiyun }
1751*4882a593Smuzhiyun 
in_intr(struct virtqueue * vq)1752*4882a593Smuzhiyun static void in_intr(struct virtqueue *vq)
1753*4882a593Smuzhiyun {
1754*4882a593Smuzhiyun 	struct port *port;
1755*4882a593Smuzhiyun 	unsigned long flags;
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun 	port = find_port_by_vq(vq->vdev->priv, vq);
1758*4882a593Smuzhiyun 	if (!port) {
1759*4882a593Smuzhiyun 		flush_bufs(vq, false);
1760*4882a593Smuzhiyun 		return;
1761*4882a593Smuzhiyun 	}
1762*4882a593Smuzhiyun 
1763*4882a593Smuzhiyun 	spin_lock_irqsave(&port->inbuf_lock, flags);
1764*4882a593Smuzhiyun 	port->inbuf = get_inbuf(port);
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	/*
1767*4882a593Smuzhiyun 	 * Normally the port should not accept data when the port is
1768*4882a593Smuzhiyun 	 * closed. For generic serial ports, the host won't (shouldn't)
1769*4882a593Smuzhiyun 	 * send data till the guest is connected. But this condition
1770*4882a593Smuzhiyun 	 * can be reached when a console port is not yet connected (no
1771*4882a593Smuzhiyun 	 * tty is spawned) and the other side sends out data over the
1772*4882a593Smuzhiyun 	 * vring, or when a remote devices start sending data before
1773*4882a593Smuzhiyun 	 * the ports are opened.
1774*4882a593Smuzhiyun 	 *
1775*4882a593Smuzhiyun 	 * A generic serial port will discard data if not connected,
1776*4882a593Smuzhiyun 	 * while console ports and rproc-serial ports accepts data at
1777*4882a593Smuzhiyun 	 * any time. rproc-serial is initiated with guest_connected to
1778*4882a593Smuzhiyun 	 * false because port_fops_open expects this. Console ports are
1779*4882a593Smuzhiyun 	 * hooked up with an HVC console and is initialized with
1780*4882a593Smuzhiyun 	 * guest_connected to true.
1781*4882a593Smuzhiyun 	 */
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1784*4882a593Smuzhiyun 		discard_port_data(port);
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 	/* Send a SIGIO indicating new data in case the process asked for it */
1787*4882a593Smuzhiyun 	send_sigio_to_port(port);
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	spin_unlock_irqrestore(&port->inbuf_lock, flags);
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	wake_up_interruptible(&port->waitqueue);
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 	if (is_console_port(port) && hvc_poll(port->cons.hvc))
1794*4882a593Smuzhiyun 		hvc_kick();
1795*4882a593Smuzhiyun }
1796*4882a593Smuzhiyun 
control_intr(struct virtqueue * vq)1797*4882a593Smuzhiyun static void control_intr(struct virtqueue *vq)
1798*4882a593Smuzhiyun {
1799*4882a593Smuzhiyun 	struct ports_device *portdev;
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	portdev = vq->vdev->priv;
1802*4882a593Smuzhiyun 	schedule_work(&portdev->control_work);
1803*4882a593Smuzhiyun }
1804*4882a593Smuzhiyun 
config_intr(struct virtio_device * vdev)1805*4882a593Smuzhiyun static void config_intr(struct virtio_device *vdev)
1806*4882a593Smuzhiyun {
1807*4882a593Smuzhiyun 	struct ports_device *portdev;
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun 	portdev = vdev->priv;
1810*4882a593Smuzhiyun 
1811*4882a593Smuzhiyun 	if (!use_multiport(portdev))
1812*4882a593Smuzhiyun 		schedule_work(&portdev->config_work);
1813*4882a593Smuzhiyun }
1814*4882a593Smuzhiyun 
config_work_handler(struct work_struct * work)1815*4882a593Smuzhiyun static void config_work_handler(struct work_struct *work)
1816*4882a593Smuzhiyun {
1817*4882a593Smuzhiyun 	struct ports_device *portdev;
1818*4882a593Smuzhiyun 
1819*4882a593Smuzhiyun 	portdev = container_of(work, struct ports_device, config_work);
1820*4882a593Smuzhiyun 	if (!use_multiport(portdev)) {
1821*4882a593Smuzhiyun 		struct virtio_device *vdev;
1822*4882a593Smuzhiyun 		struct port *port;
1823*4882a593Smuzhiyun 		u16 rows, cols;
1824*4882a593Smuzhiyun 
1825*4882a593Smuzhiyun 		vdev = portdev->vdev;
1826*4882a593Smuzhiyun 		virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1827*4882a593Smuzhiyun 		virtio_cread(vdev, struct virtio_console_config, rows, &rows);
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun 		port = find_port_by_id(portdev, 0);
1830*4882a593Smuzhiyun 		set_console_size(port, rows, cols);
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 		/*
1833*4882a593Smuzhiyun 		 * We'll use this way of resizing only for legacy
1834*4882a593Smuzhiyun 		 * support.  For newer userspace
1835*4882a593Smuzhiyun 		 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1836*4882a593Smuzhiyun 		 * to indicate console size changes so that it can be
1837*4882a593Smuzhiyun 		 * done per-port.
1838*4882a593Smuzhiyun 		 */
1839*4882a593Smuzhiyun 		resize_console(port);
1840*4882a593Smuzhiyun 	}
1841*4882a593Smuzhiyun }
1842*4882a593Smuzhiyun 
init_vqs(struct ports_device * portdev)1843*4882a593Smuzhiyun static int init_vqs(struct ports_device *portdev)
1844*4882a593Smuzhiyun {
1845*4882a593Smuzhiyun 	vq_callback_t **io_callbacks;
1846*4882a593Smuzhiyun 	char **io_names;
1847*4882a593Smuzhiyun 	struct virtqueue **vqs;
1848*4882a593Smuzhiyun 	u32 i, j, nr_ports, nr_queues;
1849*4882a593Smuzhiyun 	int err;
1850*4882a593Smuzhiyun 
1851*4882a593Smuzhiyun 	nr_ports = portdev->max_nr_ports;
1852*4882a593Smuzhiyun 	nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1853*4882a593Smuzhiyun 
1854*4882a593Smuzhiyun 	vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
1855*4882a593Smuzhiyun 	io_callbacks = kmalloc_array(nr_queues, sizeof(vq_callback_t *),
1856*4882a593Smuzhiyun 				     GFP_KERNEL);
1857*4882a593Smuzhiyun 	io_names = kmalloc_array(nr_queues, sizeof(char *), GFP_KERNEL);
1858*4882a593Smuzhiyun 	portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1859*4882a593Smuzhiyun 					GFP_KERNEL);
1860*4882a593Smuzhiyun 	portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1861*4882a593Smuzhiyun 					 GFP_KERNEL);
1862*4882a593Smuzhiyun 	if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
1863*4882a593Smuzhiyun 	    !portdev->out_vqs) {
1864*4882a593Smuzhiyun 		err = -ENOMEM;
1865*4882a593Smuzhiyun 		goto free;
1866*4882a593Smuzhiyun 	}
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun 	/*
1869*4882a593Smuzhiyun 	 * For backward compat (newer host but older guest), the host
1870*4882a593Smuzhiyun 	 * spawns a console port first and also inits the vqs for port
1871*4882a593Smuzhiyun 	 * 0 before others.
1872*4882a593Smuzhiyun 	 */
1873*4882a593Smuzhiyun 	j = 0;
1874*4882a593Smuzhiyun 	io_callbacks[j] = in_intr;
1875*4882a593Smuzhiyun 	io_callbacks[j + 1] = out_intr;
1876*4882a593Smuzhiyun 	io_names[j] = "input";
1877*4882a593Smuzhiyun 	io_names[j + 1] = "output";
1878*4882a593Smuzhiyun 	j += 2;
1879*4882a593Smuzhiyun 
1880*4882a593Smuzhiyun 	if (use_multiport(portdev)) {
1881*4882a593Smuzhiyun 		io_callbacks[j] = control_intr;
1882*4882a593Smuzhiyun 		io_callbacks[j + 1] = NULL;
1883*4882a593Smuzhiyun 		io_names[j] = "control-i";
1884*4882a593Smuzhiyun 		io_names[j + 1] = "control-o";
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun 		for (i = 1; i < nr_ports; i++) {
1887*4882a593Smuzhiyun 			j += 2;
1888*4882a593Smuzhiyun 			io_callbacks[j] = in_intr;
1889*4882a593Smuzhiyun 			io_callbacks[j + 1] = out_intr;
1890*4882a593Smuzhiyun 			io_names[j] = "input";
1891*4882a593Smuzhiyun 			io_names[j + 1] = "output";
1892*4882a593Smuzhiyun 		}
1893*4882a593Smuzhiyun 	}
1894*4882a593Smuzhiyun 	/* Find the queues. */
1895*4882a593Smuzhiyun 	err = virtio_find_vqs(portdev->vdev, nr_queues, vqs,
1896*4882a593Smuzhiyun 			      io_callbacks,
1897*4882a593Smuzhiyun 			      (const char **)io_names, NULL);
1898*4882a593Smuzhiyun 	if (err)
1899*4882a593Smuzhiyun 		goto free;
1900*4882a593Smuzhiyun 
1901*4882a593Smuzhiyun 	j = 0;
1902*4882a593Smuzhiyun 	portdev->in_vqs[0] = vqs[0];
1903*4882a593Smuzhiyun 	portdev->out_vqs[0] = vqs[1];
1904*4882a593Smuzhiyun 	j += 2;
1905*4882a593Smuzhiyun 	if (use_multiport(portdev)) {
1906*4882a593Smuzhiyun 		portdev->c_ivq = vqs[j];
1907*4882a593Smuzhiyun 		portdev->c_ovq = vqs[j + 1];
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 		for (i = 1; i < nr_ports; i++) {
1910*4882a593Smuzhiyun 			j += 2;
1911*4882a593Smuzhiyun 			portdev->in_vqs[i] = vqs[j];
1912*4882a593Smuzhiyun 			portdev->out_vqs[i] = vqs[j + 1];
1913*4882a593Smuzhiyun 		}
1914*4882a593Smuzhiyun 	}
1915*4882a593Smuzhiyun 	kfree(io_names);
1916*4882a593Smuzhiyun 	kfree(io_callbacks);
1917*4882a593Smuzhiyun 	kfree(vqs);
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun 	return 0;
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun free:
1922*4882a593Smuzhiyun 	kfree(portdev->out_vqs);
1923*4882a593Smuzhiyun 	kfree(portdev->in_vqs);
1924*4882a593Smuzhiyun 	kfree(io_names);
1925*4882a593Smuzhiyun 	kfree(io_callbacks);
1926*4882a593Smuzhiyun 	kfree(vqs);
1927*4882a593Smuzhiyun 
1928*4882a593Smuzhiyun 	return err;
1929*4882a593Smuzhiyun }
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun static const struct file_operations portdev_fops = {
1932*4882a593Smuzhiyun 	.owner = THIS_MODULE,
1933*4882a593Smuzhiyun };
1934*4882a593Smuzhiyun 
remove_vqs(struct ports_device * portdev)1935*4882a593Smuzhiyun static void remove_vqs(struct ports_device *portdev)
1936*4882a593Smuzhiyun {
1937*4882a593Smuzhiyun 	struct virtqueue *vq;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	virtio_device_for_each_vq(portdev->vdev, vq) {
1940*4882a593Smuzhiyun 		struct port_buffer *buf;
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun 		flush_bufs(vq, true);
1943*4882a593Smuzhiyun 		while ((buf = virtqueue_detach_unused_buf(vq)))
1944*4882a593Smuzhiyun 			free_buf(buf, true);
1945*4882a593Smuzhiyun 	}
1946*4882a593Smuzhiyun 	portdev->vdev->config->del_vqs(portdev->vdev);
1947*4882a593Smuzhiyun 	kfree(portdev->in_vqs);
1948*4882a593Smuzhiyun 	kfree(portdev->out_vqs);
1949*4882a593Smuzhiyun }
1950*4882a593Smuzhiyun 
virtcons_remove(struct virtio_device * vdev)1951*4882a593Smuzhiyun static void virtcons_remove(struct virtio_device *vdev)
1952*4882a593Smuzhiyun {
1953*4882a593Smuzhiyun 	struct ports_device *portdev;
1954*4882a593Smuzhiyun 	struct port *port, *port2;
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun 	portdev = vdev->priv;
1957*4882a593Smuzhiyun 
1958*4882a593Smuzhiyun 	spin_lock_irq(&pdrvdata_lock);
1959*4882a593Smuzhiyun 	list_del(&portdev->list);
1960*4882a593Smuzhiyun 	spin_unlock_irq(&pdrvdata_lock);
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	/* Device is going away, exit any polling for buffers */
1963*4882a593Smuzhiyun 	virtio_break_device(vdev);
1964*4882a593Smuzhiyun 	if (use_multiport(portdev))
1965*4882a593Smuzhiyun 		flush_work(&portdev->control_work);
1966*4882a593Smuzhiyun 	else
1967*4882a593Smuzhiyun 		flush_work(&portdev->config_work);
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun 	/* Disable interrupts for vqs */
1970*4882a593Smuzhiyun 	vdev->config->reset(vdev);
1971*4882a593Smuzhiyun 	/* Finish up work that's lined up */
1972*4882a593Smuzhiyun 	if (use_multiport(portdev))
1973*4882a593Smuzhiyun 		cancel_work_sync(&portdev->control_work);
1974*4882a593Smuzhiyun 	else
1975*4882a593Smuzhiyun 		cancel_work_sync(&portdev->config_work);
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	list_for_each_entry_safe(port, port2, &portdev->ports, list)
1978*4882a593Smuzhiyun 		unplug_port(port);
1979*4882a593Smuzhiyun 
1980*4882a593Smuzhiyun 	unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun 	/*
1983*4882a593Smuzhiyun 	 * When yanking out a device, we immediately lose the
1984*4882a593Smuzhiyun 	 * (device-side) queues.  So there's no point in keeping the
1985*4882a593Smuzhiyun 	 * guest side around till we drop our final reference.  This
1986*4882a593Smuzhiyun 	 * also means that any ports which are in an open state will
1987*4882a593Smuzhiyun 	 * have to just stop using the port, as the vqs are going
1988*4882a593Smuzhiyun 	 * away.
1989*4882a593Smuzhiyun 	 */
1990*4882a593Smuzhiyun 	remove_vqs(portdev);
1991*4882a593Smuzhiyun 	kfree(portdev);
1992*4882a593Smuzhiyun }
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun /*
1995*4882a593Smuzhiyun  * Once we're further in boot, we get probed like any other virtio
1996*4882a593Smuzhiyun  * device.
1997*4882a593Smuzhiyun  *
1998*4882a593Smuzhiyun  * If the host also supports multiple console ports, we check the
1999*4882a593Smuzhiyun  * config space to see how many ports the host has spawned.  We
2000*4882a593Smuzhiyun  * initialize each port found.
2001*4882a593Smuzhiyun  */
virtcons_probe(struct virtio_device * vdev)2002*4882a593Smuzhiyun static int virtcons_probe(struct virtio_device *vdev)
2003*4882a593Smuzhiyun {
2004*4882a593Smuzhiyun 	struct ports_device *portdev;
2005*4882a593Smuzhiyun 	int err;
2006*4882a593Smuzhiyun 	bool multiport;
2007*4882a593Smuzhiyun 	bool early = early_put_chars != NULL;
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	/* We only need a config space if features are offered */
2010*4882a593Smuzhiyun 	if (!vdev->config->get &&
2011*4882a593Smuzhiyun 	    (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
2012*4882a593Smuzhiyun 	     || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
2013*4882a593Smuzhiyun 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
2014*4882a593Smuzhiyun 			__func__);
2015*4882a593Smuzhiyun 		return -EINVAL;
2016*4882a593Smuzhiyun 	}
2017*4882a593Smuzhiyun 
2018*4882a593Smuzhiyun 	/* Ensure to read early_put_chars now */
2019*4882a593Smuzhiyun 	barrier();
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
2022*4882a593Smuzhiyun 	if (!portdev) {
2023*4882a593Smuzhiyun 		err = -ENOMEM;
2024*4882a593Smuzhiyun 		goto fail;
2025*4882a593Smuzhiyun 	}
2026*4882a593Smuzhiyun 
2027*4882a593Smuzhiyun 	/* Attach this portdev to this virtio_device, and vice-versa. */
2028*4882a593Smuzhiyun 	portdev->vdev = vdev;
2029*4882a593Smuzhiyun 	vdev->priv = portdev;
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	portdev->chr_major = register_chrdev(0, "virtio-portsdev",
2032*4882a593Smuzhiyun 					     &portdev_fops);
2033*4882a593Smuzhiyun 	if (portdev->chr_major < 0) {
2034*4882a593Smuzhiyun 		dev_err(&vdev->dev,
2035*4882a593Smuzhiyun 			"Error %d registering chrdev for device %u\n",
2036*4882a593Smuzhiyun 			portdev->chr_major, vdev->index);
2037*4882a593Smuzhiyun 		err = portdev->chr_major;
2038*4882a593Smuzhiyun 		goto free;
2039*4882a593Smuzhiyun 	}
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun 	multiport = false;
2042*4882a593Smuzhiyun 	portdev->max_nr_ports = 1;
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun 	/* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
2045*4882a593Smuzhiyun 	if (!is_rproc_serial(vdev) &&
2046*4882a593Smuzhiyun 	    virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
2047*4882a593Smuzhiyun 				 struct virtio_console_config, max_nr_ports,
2048*4882a593Smuzhiyun 				 &portdev->max_nr_ports) == 0) {
2049*4882a593Smuzhiyun 		multiport = true;
2050*4882a593Smuzhiyun 	}
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	err = init_vqs(portdev);
2053*4882a593Smuzhiyun 	if (err < 0) {
2054*4882a593Smuzhiyun 		dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
2055*4882a593Smuzhiyun 		goto free_chrdev;
2056*4882a593Smuzhiyun 	}
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun 	spin_lock_init(&portdev->ports_lock);
2059*4882a593Smuzhiyun 	INIT_LIST_HEAD(&portdev->ports);
2060*4882a593Smuzhiyun 	INIT_LIST_HEAD(&portdev->list);
2061*4882a593Smuzhiyun 
2062*4882a593Smuzhiyun 	virtio_device_ready(portdev->vdev);
2063*4882a593Smuzhiyun 
2064*4882a593Smuzhiyun 	INIT_WORK(&portdev->config_work, &config_work_handler);
2065*4882a593Smuzhiyun 	INIT_WORK(&portdev->control_work, &control_work_handler);
2066*4882a593Smuzhiyun 
2067*4882a593Smuzhiyun 	if (multiport) {
2068*4882a593Smuzhiyun 		spin_lock_init(&portdev->c_ivq_lock);
2069*4882a593Smuzhiyun 		spin_lock_init(&portdev->c_ovq_lock);
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun 		err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2072*4882a593Smuzhiyun 		if (err < 0) {
2073*4882a593Smuzhiyun 			dev_err(&vdev->dev,
2074*4882a593Smuzhiyun 				"Error allocating buffers for control queue\n");
2075*4882a593Smuzhiyun 			/*
2076*4882a593Smuzhiyun 			 * The host might want to notify mgmt sw about device
2077*4882a593Smuzhiyun 			 * add failure.
2078*4882a593Smuzhiyun 			 */
2079*4882a593Smuzhiyun 			__send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2080*4882a593Smuzhiyun 					   VIRTIO_CONSOLE_DEVICE_READY, 0);
2081*4882a593Smuzhiyun 			/* Device was functional: we need full cleanup. */
2082*4882a593Smuzhiyun 			virtcons_remove(vdev);
2083*4882a593Smuzhiyun 			return err;
2084*4882a593Smuzhiyun 		}
2085*4882a593Smuzhiyun 	} else {
2086*4882a593Smuzhiyun 		/*
2087*4882a593Smuzhiyun 		 * For backward compatibility: Create a console port
2088*4882a593Smuzhiyun 		 * if we're running on older host.
2089*4882a593Smuzhiyun 		 */
2090*4882a593Smuzhiyun 		add_port(portdev, 0);
2091*4882a593Smuzhiyun 	}
2092*4882a593Smuzhiyun 
2093*4882a593Smuzhiyun 	spin_lock_irq(&pdrvdata_lock);
2094*4882a593Smuzhiyun 	list_add_tail(&portdev->list, &pdrvdata.portdevs);
2095*4882a593Smuzhiyun 	spin_unlock_irq(&pdrvdata_lock);
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	__send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2098*4882a593Smuzhiyun 			   VIRTIO_CONSOLE_DEVICE_READY, 1);
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 	/*
2101*4882a593Smuzhiyun 	 * If there was an early virtio console, assume that there are no
2102*4882a593Smuzhiyun 	 * other consoles. We need to wait until the hvc_alloc matches the
2103*4882a593Smuzhiyun 	 * hvc_instantiate, otherwise tty_open will complain, resulting in
2104*4882a593Smuzhiyun 	 * a "Warning: unable to open an initial console" boot failure.
2105*4882a593Smuzhiyun 	 * Without multiport this is done in add_port above. With multiport
2106*4882a593Smuzhiyun 	 * this might take some host<->guest communication - thus we have to
2107*4882a593Smuzhiyun 	 * wait.
2108*4882a593Smuzhiyun 	 */
2109*4882a593Smuzhiyun 	if (multiport && early)
2110*4882a593Smuzhiyun 		wait_for_completion(&early_console_added);
2111*4882a593Smuzhiyun 
2112*4882a593Smuzhiyun 	return 0;
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun free_chrdev:
2115*4882a593Smuzhiyun 	unregister_chrdev(portdev->chr_major, "virtio-portsdev");
2116*4882a593Smuzhiyun free:
2117*4882a593Smuzhiyun 	kfree(portdev);
2118*4882a593Smuzhiyun fail:
2119*4882a593Smuzhiyun 	return err;
2120*4882a593Smuzhiyun }
2121*4882a593Smuzhiyun 
2122*4882a593Smuzhiyun static const struct virtio_device_id id_table[] = {
2123*4882a593Smuzhiyun 	{ VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
2124*4882a593Smuzhiyun 	{ 0 },
2125*4882a593Smuzhiyun };
2126*4882a593Smuzhiyun MODULE_DEVICE_TABLE(virtio, id_table);
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun static const unsigned int features[] = {
2129*4882a593Smuzhiyun 	VIRTIO_CONSOLE_F_SIZE,
2130*4882a593Smuzhiyun 	VIRTIO_CONSOLE_F_MULTIPORT,
2131*4882a593Smuzhiyun };
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun static const struct virtio_device_id rproc_serial_id_table[] = {
2134*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_REMOTEPROC)
2135*4882a593Smuzhiyun 	{ VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
2136*4882a593Smuzhiyun #endif
2137*4882a593Smuzhiyun 	{ 0 },
2138*4882a593Smuzhiyun };
2139*4882a593Smuzhiyun MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun static const unsigned int rproc_serial_features[] = {
2142*4882a593Smuzhiyun };
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
virtcons_freeze(struct virtio_device * vdev)2145*4882a593Smuzhiyun static int virtcons_freeze(struct virtio_device *vdev)
2146*4882a593Smuzhiyun {
2147*4882a593Smuzhiyun 	struct ports_device *portdev;
2148*4882a593Smuzhiyun 	struct port *port;
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	portdev = vdev->priv;
2151*4882a593Smuzhiyun 
2152*4882a593Smuzhiyun 	vdev->config->reset(vdev);
2153*4882a593Smuzhiyun 
2154*4882a593Smuzhiyun 	if (use_multiport(portdev))
2155*4882a593Smuzhiyun 		virtqueue_disable_cb(portdev->c_ivq);
2156*4882a593Smuzhiyun 	cancel_work_sync(&portdev->control_work);
2157*4882a593Smuzhiyun 	cancel_work_sync(&portdev->config_work);
2158*4882a593Smuzhiyun 	/*
2159*4882a593Smuzhiyun 	 * Once more: if control_work_handler() was running, it would
2160*4882a593Smuzhiyun 	 * enable the cb as the last step.
2161*4882a593Smuzhiyun 	 */
2162*4882a593Smuzhiyun 	if (use_multiport(portdev))
2163*4882a593Smuzhiyun 		virtqueue_disable_cb(portdev->c_ivq);
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	list_for_each_entry(port, &portdev->ports, list) {
2166*4882a593Smuzhiyun 		virtqueue_disable_cb(port->in_vq);
2167*4882a593Smuzhiyun 		virtqueue_disable_cb(port->out_vq);
2168*4882a593Smuzhiyun 		/*
2169*4882a593Smuzhiyun 		 * We'll ask the host later if the new invocation has
2170*4882a593Smuzhiyun 		 * the port opened or closed.
2171*4882a593Smuzhiyun 		 */
2172*4882a593Smuzhiyun 		port->host_connected = false;
2173*4882a593Smuzhiyun 		remove_port_data(port);
2174*4882a593Smuzhiyun 	}
2175*4882a593Smuzhiyun 	remove_vqs(portdev);
2176*4882a593Smuzhiyun 
2177*4882a593Smuzhiyun 	return 0;
2178*4882a593Smuzhiyun }
2179*4882a593Smuzhiyun 
virtcons_restore(struct virtio_device * vdev)2180*4882a593Smuzhiyun static int virtcons_restore(struct virtio_device *vdev)
2181*4882a593Smuzhiyun {
2182*4882a593Smuzhiyun 	struct ports_device *portdev;
2183*4882a593Smuzhiyun 	struct port *port;
2184*4882a593Smuzhiyun 	int ret;
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 	portdev = vdev->priv;
2187*4882a593Smuzhiyun 
2188*4882a593Smuzhiyun 	ret = init_vqs(portdev);
2189*4882a593Smuzhiyun 	if (ret)
2190*4882a593Smuzhiyun 		return ret;
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun 	virtio_device_ready(portdev->vdev);
2193*4882a593Smuzhiyun 
2194*4882a593Smuzhiyun 	if (use_multiport(portdev))
2195*4882a593Smuzhiyun 		fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 	list_for_each_entry(port, &portdev->ports, list) {
2198*4882a593Smuzhiyun 		port->in_vq = portdev->in_vqs[port->id];
2199*4882a593Smuzhiyun 		port->out_vq = portdev->out_vqs[port->id];
2200*4882a593Smuzhiyun 
2201*4882a593Smuzhiyun 		fill_queue(port->in_vq, &port->inbuf_lock);
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun 		/* Get port open/close status on the host */
2204*4882a593Smuzhiyun 		send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun 		/*
2207*4882a593Smuzhiyun 		 * If a port was open at the time of suspending, we
2208*4882a593Smuzhiyun 		 * have to let the host know that it's still open.
2209*4882a593Smuzhiyun 		 */
2210*4882a593Smuzhiyun 		if (port->guest_connected)
2211*4882a593Smuzhiyun 			send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
2212*4882a593Smuzhiyun 	}
2213*4882a593Smuzhiyun 	return 0;
2214*4882a593Smuzhiyun }
2215*4882a593Smuzhiyun #endif
2216*4882a593Smuzhiyun 
2217*4882a593Smuzhiyun static struct virtio_driver virtio_console = {
2218*4882a593Smuzhiyun 	.feature_table = features,
2219*4882a593Smuzhiyun 	.feature_table_size = ARRAY_SIZE(features),
2220*4882a593Smuzhiyun 	.driver.name =	KBUILD_MODNAME,
2221*4882a593Smuzhiyun 	.driver.owner =	THIS_MODULE,
2222*4882a593Smuzhiyun 	.id_table =	id_table,
2223*4882a593Smuzhiyun 	.probe =	virtcons_probe,
2224*4882a593Smuzhiyun 	.remove =	virtcons_remove,
2225*4882a593Smuzhiyun 	.config_changed = config_intr,
2226*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
2227*4882a593Smuzhiyun 	.freeze =	virtcons_freeze,
2228*4882a593Smuzhiyun 	.restore =	virtcons_restore,
2229*4882a593Smuzhiyun #endif
2230*4882a593Smuzhiyun };
2231*4882a593Smuzhiyun 
2232*4882a593Smuzhiyun static struct virtio_driver virtio_rproc_serial = {
2233*4882a593Smuzhiyun 	.feature_table = rproc_serial_features,
2234*4882a593Smuzhiyun 	.feature_table_size = ARRAY_SIZE(rproc_serial_features),
2235*4882a593Smuzhiyun 	.driver.name =	"virtio_rproc_serial",
2236*4882a593Smuzhiyun 	.driver.owner =	THIS_MODULE,
2237*4882a593Smuzhiyun 	.id_table =	rproc_serial_id_table,
2238*4882a593Smuzhiyun 	.probe =	virtcons_probe,
2239*4882a593Smuzhiyun 	.remove =	virtcons_remove,
2240*4882a593Smuzhiyun };
2241*4882a593Smuzhiyun 
virtio_console_init(void)2242*4882a593Smuzhiyun static int __init virtio_console_init(void)
2243*4882a593Smuzhiyun {
2244*4882a593Smuzhiyun 	int err;
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
2247*4882a593Smuzhiyun 	if (IS_ERR(pdrvdata.class)) {
2248*4882a593Smuzhiyun 		err = PTR_ERR(pdrvdata.class);
2249*4882a593Smuzhiyun 		pr_err("Error %d creating virtio-ports class\n", err);
2250*4882a593Smuzhiyun 		return err;
2251*4882a593Smuzhiyun 	}
2252*4882a593Smuzhiyun 
2253*4882a593Smuzhiyun 	pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2254*4882a593Smuzhiyun 	if (!pdrvdata.debugfs_dir)
2255*4882a593Smuzhiyun 		pr_warn("Error creating debugfs dir for virtio-ports\n");
2256*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pdrvdata.consoles);
2257*4882a593Smuzhiyun 	INIT_LIST_HEAD(&pdrvdata.portdevs);
2258*4882a593Smuzhiyun 
2259*4882a593Smuzhiyun 	err = register_virtio_driver(&virtio_console);
2260*4882a593Smuzhiyun 	if (err < 0) {
2261*4882a593Smuzhiyun 		pr_err("Error %d registering virtio driver\n", err);
2262*4882a593Smuzhiyun 		goto free;
2263*4882a593Smuzhiyun 	}
2264*4882a593Smuzhiyun 	err = register_virtio_driver(&virtio_rproc_serial);
2265*4882a593Smuzhiyun 	if (err < 0) {
2266*4882a593Smuzhiyun 		pr_err("Error %d registering virtio rproc serial driver\n",
2267*4882a593Smuzhiyun 		       err);
2268*4882a593Smuzhiyun 		goto unregister;
2269*4882a593Smuzhiyun 	}
2270*4882a593Smuzhiyun 	return 0;
2271*4882a593Smuzhiyun unregister:
2272*4882a593Smuzhiyun 	unregister_virtio_driver(&virtio_console);
2273*4882a593Smuzhiyun free:
2274*4882a593Smuzhiyun 	debugfs_remove_recursive(pdrvdata.debugfs_dir);
2275*4882a593Smuzhiyun 	class_destroy(pdrvdata.class);
2276*4882a593Smuzhiyun 	return err;
2277*4882a593Smuzhiyun }
2278*4882a593Smuzhiyun 
virtio_console_fini(void)2279*4882a593Smuzhiyun static void __exit virtio_console_fini(void)
2280*4882a593Smuzhiyun {
2281*4882a593Smuzhiyun 	reclaim_dma_bufs();
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun 	unregister_virtio_driver(&virtio_console);
2284*4882a593Smuzhiyun 	unregister_virtio_driver(&virtio_rproc_serial);
2285*4882a593Smuzhiyun 
2286*4882a593Smuzhiyun 	class_destroy(pdrvdata.class);
2287*4882a593Smuzhiyun 	debugfs_remove_recursive(pdrvdata.debugfs_dir);
2288*4882a593Smuzhiyun }
2289*4882a593Smuzhiyun module_init(virtio_console_init);
2290*4882a593Smuzhiyun module_exit(virtio_console_fini);
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun MODULE_DESCRIPTION("Virtio console driver");
2293*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2294