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