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