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