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