0001
0002
0003
0004
0005
0006
0007 #include <linux/cdev.h>
0008 #include <linux/debugfs.h>
0009 #include <linux/completion.h>
0010 #include <linux/device.h>
0011 #include <linux/err.h>
0012 #include <linux/freezer.h>
0013 #include <linux/fs.h>
0014 #include <linux/splice.h>
0015 #include <linux/pagemap.h>
0016 #include <linux/init.h>
0017 #include <linux/list.h>
0018 #include <linux/poll.h>
0019 #include <linux/sched.h>
0020 #include <linux/slab.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/virtio.h>
0023 #include <linux/virtio_console.h>
0024 #include <linux/wait.h>
0025 #include <linux/workqueue.h>
0026 #include <linux/module.h>
0027 #include <linux/dma-mapping.h>
0028 #include "../tty/hvc/hvc_console.h"
0029
0030 #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
0031 #define VIRTCONS_MAX_PORTS 0x8000
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 struct ports_driver_data {
0042
0043 struct class *class;
0044
0045
0046 struct dentry *debugfs_dir;
0047
0048
0049 struct list_head portdevs;
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 unsigned int next_vtermno;
0062
0063
0064 struct list_head consoles;
0065 };
0066 static struct ports_driver_data pdrvdata = { .next_vtermno = 1};
0067
0068 static DEFINE_SPINLOCK(pdrvdata_lock);
0069 static DECLARE_COMPLETION(early_console_added);
0070
0071
0072 struct console {
0073
0074 struct list_head list;
0075
0076
0077 struct hvc_struct *hvc;
0078
0079
0080 struct winsize ws;
0081
0082
0083
0084
0085
0086
0087
0088
0089 u32 vtermno;
0090 };
0091
0092 struct port_buffer {
0093 char *buf;
0094
0095
0096 size_t size;
0097
0098
0099 size_t len;
0100
0101 size_t offset;
0102
0103
0104 dma_addr_t dma;
0105
0106
0107 struct device *dev;
0108
0109
0110 struct list_head list;
0111
0112
0113 unsigned int sgpages;
0114
0115
0116 struct scatterlist sg[];
0117 };
0118
0119
0120
0121
0122
0123 struct ports_device {
0124
0125 struct list_head list;
0126
0127
0128
0129
0130
0131 struct work_struct control_work;
0132 struct work_struct config_work;
0133
0134 struct list_head ports;
0135
0136
0137 spinlock_t ports_lock;
0138
0139
0140 spinlock_t c_ivq_lock;
0141 spinlock_t c_ovq_lock;
0142
0143
0144 u32 max_nr_ports;
0145
0146
0147 struct virtio_device *vdev;
0148
0149
0150
0151
0152
0153 struct virtqueue *c_ivq, *c_ovq;
0154
0155
0156
0157
0158
0159 struct virtio_console_control cpkt;
0160
0161
0162 struct virtqueue **in_vqs, **out_vqs;
0163
0164
0165 int chr_major;
0166 };
0167
0168 struct port_stats {
0169 unsigned long bytes_sent, bytes_received, bytes_discarded;
0170 };
0171
0172
0173 struct port {
0174
0175 struct list_head list;
0176
0177
0178 struct ports_device *portdev;
0179
0180
0181 struct port_buffer *inbuf;
0182
0183
0184
0185
0186
0187
0188 spinlock_t inbuf_lock;
0189
0190
0191 spinlock_t outvq_lock;
0192
0193
0194 struct virtqueue *in_vq, *out_vq;
0195
0196
0197 struct dentry *debugfs_file;
0198
0199
0200
0201
0202
0203
0204 struct port_stats stats;
0205
0206
0207
0208
0209
0210 struct console cons;
0211
0212
0213 struct cdev *cdev;
0214 struct device *dev;
0215
0216
0217 struct kref kref;
0218
0219
0220 wait_queue_head_t waitqueue;
0221
0222
0223 char *name;
0224
0225
0226 struct fasync_struct *async_queue;
0227
0228
0229 u32 id;
0230
0231 bool outvq_full;
0232
0233
0234 bool host_connected;
0235
0236
0237 bool guest_connected;
0238 };
0239
0240
0241 static int (*early_put_chars)(u32, const char *, int);
0242
0243 static struct port *find_port_by_vtermno(u32 vtermno)
0244 {
0245 struct port *port;
0246 struct console *cons;
0247 unsigned long flags;
0248
0249 spin_lock_irqsave(&pdrvdata_lock, flags);
0250 list_for_each_entry(cons, &pdrvdata.consoles, list) {
0251 if (cons->vtermno == vtermno) {
0252 port = container_of(cons, struct port, cons);
0253 goto out;
0254 }
0255 }
0256 port = NULL;
0257 out:
0258 spin_unlock_irqrestore(&pdrvdata_lock, flags);
0259 return port;
0260 }
0261
0262 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
0263 dev_t dev)
0264 {
0265 struct port *port;
0266 unsigned long flags;
0267
0268 spin_lock_irqsave(&portdev->ports_lock, flags);
0269 list_for_each_entry(port, &portdev->ports, list) {
0270 if (port->cdev->dev == dev) {
0271 kref_get(&port->kref);
0272 goto out;
0273 }
0274 }
0275 port = NULL;
0276 out:
0277 spin_unlock_irqrestore(&portdev->ports_lock, flags);
0278
0279 return port;
0280 }
0281
0282 static struct port *find_port_by_devt(dev_t dev)
0283 {
0284 struct ports_device *portdev;
0285 struct port *port;
0286 unsigned long flags;
0287
0288 spin_lock_irqsave(&pdrvdata_lock, flags);
0289 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
0290 port = find_port_by_devt_in_portdev(portdev, dev);
0291 if (port)
0292 goto out;
0293 }
0294 port = NULL;
0295 out:
0296 spin_unlock_irqrestore(&pdrvdata_lock, flags);
0297 return port;
0298 }
0299
0300 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
0301 {
0302 struct port *port;
0303 unsigned long flags;
0304
0305 spin_lock_irqsave(&portdev->ports_lock, flags);
0306 list_for_each_entry(port, &portdev->ports, list)
0307 if (port->id == id)
0308 goto out;
0309 port = NULL;
0310 out:
0311 spin_unlock_irqrestore(&portdev->ports_lock, flags);
0312
0313 return port;
0314 }
0315
0316 static struct port *find_port_by_vq(struct ports_device *portdev,
0317 struct virtqueue *vq)
0318 {
0319 struct port *port;
0320 unsigned long flags;
0321
0322 spin_lock_irqsave(&portdev->ports_lock, flags);
0323 list_for_each_entry(port, &portdev->ports, list)
0324 if (port->in_vq == vq || port->out_vq == vq)
0325 goto out;
0326 port = NULL;
0327 out:
0328 spin_unlock_irqrestore(&portdev->ports_lock, flags);
0329 return port;
0330 }
0331
0332 static bool is_console_port(struct port *port)
0333 {
0334 if (port->cons.hvc)
0335 return true;
0336 return false;
0337 }
0338
0339 static bool is_rproc_serial(const struct virtio_device *vdev)
0340 {
0341 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
0342 }
0343
0344 static inline bool use_multiport(struct ports_device *portdev)
0345 {
0346
0347
0348
0349
0350 if (!portdev->vdev)
0351 return false;
0352 return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
0353 }
0354
0355 static DEFINE_SPINLOCK(dma_bufs_lock);
0356 static LIST_HEAD(pending_free_dma_bufs);
0357
0358 static void free_buf(struct port_buffer *buf, bool can_sleep)
0359 {
0360 unsigned int i;
0361
0362 for (i = 0; i < buf->sgpages; i++) {
0363 struct page *page = sg_page(&buf->sg[i]);
0364 if (!page)
0365 break;
0366 put_page(page);
0367 }
0368
0369 if (!buf->dev) {
0370 kfree(buf->buf);
0371 } else if (is_rproc_enabled) {
0372 unsigned long flags;
0373
0374
0375 if (!can_sleep) {
0376
0377 spin_lock_irqsave(&dma_bufs_lock, flags);
0378 list_add_tail(&buf->list, &pending_free_dma_bufs);
0379 spin_unlock_irqrestore(&dma_bufs_lock, flags);
0380 return;
0381 }
0382 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
0383
0384
0385 put_device(buf->dev);
0386 }
0387
0388 kfree(buf);
0389 }
0390
0391 static void reclaim_dma_bufs(void)
0392 {
0393 unsigned long flags;
0394 struct port_buffer *buf, *tmp;
0395 LIST_HEAD(tmp_list);
0396
0397 if (list_empty(&pending_free_dma_bufs))
0398 return;
0399
0400
0401 spin_lock_irqsave(&dma_bufs_lock, flags);
0402 list_cut_position(&tmp_list, &pending_free_dma_bufs,
0403 pending_free_dma_bufs.prev);
0404 spin_unlock_irqrestore(&dma_bufs_lock, flags);
0405
0406
0407 list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
0408 list_del(&buf->list);
0409 free_buf(buf, true);
0410 }
0411 }
0412
0413 static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
0414 int pages)
0415 {
0416 struct port_buffer *buf;
0417
0418 reclaim_dma_bufs();
0419
0420
0421
0422
0423
0424 buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
0425 if (!buf)
0426 goto fail;
0427
0428 buf->sgpages = pages;
0429 if (pages > 0) {
0430 buf->dev = NULL;
0431 buf->buf = NULL;
0432 return buf;
0433 }
0434
0435 if (is_rproc_serial(vdev)) {
0436
0437
0438
0439
0440
0441
0442 buf->dev = vdev->dev.parent;
0443 if (!buf->dev)
0444 goto free_buf;
0445
0446
0447 get_device(buf->dev);
0448 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
0449 GFP_KERNEL);
0450 } else {
0451 buf->dev = NULL;
0452 buf->buf = kmalloc(buf_size, GFP_KERNEL);
0453 }
0454
0455 if (!buf->buf)
0456 goto free_buf;
0457 buf->len = 0;
0458 buf->offset = 0;
0459 buf->size = buf_size;
0460 return buf;
0461
0462 free_buf:
0463 kfree(buf);
0464 fail:
0465 return NULL;
0466 }
0467
0468
0469 static struct port_buffer *get_inbuf(struct port *port)
0470 {
0471 struct port_buffer *buf;
0472 unsigned int len;
0473
0474 if (port->inbuf)
0475 return port->inbuf;
0476
0477 buf = virtqueue_get_buf(port->in_vq, &len);
0478 if (buf) {
0479 buf->len = min_t(size_t, len, buf->size);
0480 buf->offset = 0;
0481 port->stats.bytes_received += len;
0482 }
0483 return buf;
0484 }
0485
0486
0487
0488
0489
0490
0491
0492 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
0493 {
0494 struct scatterlist sg[1];
0495 int ret;
0496
0497 sg_init_one(sg, buf->buf, buf->size);
0498
0499 ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
0500 virtqueue_kick(vq);
0501 if (!ret)
0502 ret = vq->num_free;
0503 return ret;
0504 }
0505
0506
0507 static void discard_port_data(struct port *port)
0508 {
0509 struct port_buffer *buf;
0510 unsigned int err;
0511
0512 if (!port->portdev) {
0513
0514 return;
0515 }
0516 buf = get_inbuf(port);
0517
0518 err = 0;
0519 while (buf) {
0520 port->stats.bytes_discarded += buf->len - buf->offset;
0521 if (add_inbuf(port->in_vq, buf) < 0) {
0522 err++;
0523 free_buf(buf, false);
0524 }
0525 port->inbuf = NULL;
0526 buf = get_inbuf(port);
0527 }
0528 if (err)
0529 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
0530 err);
0531 }
0532
0533 static bool port_has_data(struct port *port)
0534 {
0535 unsigned long flags;
0536 bool ret;
0537
0538 ret = false;
0539 spin_lock_irqsave(&port->inbuf_lock, flags);
0540 port->inbuf = get_inbuf(port);
0541 if (port->inbuf)
0542 ret = true;
0543
0544 spin_unlock_irqrestore(&port->inbuf_lock, flags);
0545 return ret;
0546 }
0547
0548 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
0549 unsigned int event, unsigned int value)
0550 {
0551 struct scatterlist sg[1];
0552 struct virtqueue *vq;
0553 unsigned int len;
0554
0555 if (!use_multiport(portdev))
0556 return 0;
0557
0558 vq = portdev->c_ovq;
0559
0560 spin_lock(&portdev->c_ovq_lock);
0561
0562 portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
0563 portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
0564 portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
0565
0566 sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
0567
0568 if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
0569 virtqueue_kick(vq);
0570 while (!virtqueue_get_buf(vq, &len)
0571 && !virtqueue_is_broken(vq))
0572 cpu_relax();
0573 }
0574
0575 spin_unlock(&portdev->c_ovq_lock);
0576 return 0;
0577 }
0578
0579 static ssize_t send_control_msg(struct port *port, unsigned int event,
0580 unsigned int value)
0581 {
0582
0583 if (port->portdev)
0584 return __send_control_msg(port->portdev, port->id, event, value);
0585 return 0;
0586 }
0587
0588
0589
0590 static void reclaim_consumed_buffers(struct port *port)
0591 {
0592 struct port_buffer *buf;
0593 unsigned int len;
0594
0595 if (!port->portdev) {
0596
0597 return;
0598 }
0599 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
0600 free_buf(buf, false);
0601 port->outvq_full = false;
0602 }
0603 }
0604
0605 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
0606 int nents, size_t in_count,
0607 void *data, bool nonblock)
0608 {
0609 struct virtqueue *out_vq;
0610 int err;
0611 unsigned long flags;
0612 unsigned int len;
0613
0614 out_vq = port->out_vq;
0615
0616 spin_lock_irqsave(&port->outvq_lock, flags);
0617
0618 reclaim_consumed_buffers(port);
0619
0620 err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
0621
0622
0623 virtqueue_kick(out_vq);
0624
0625 if (err) {
0626 in_count = 0;
0627 goto done;
0628 }
0629
0630 if (out_vq->num_free == 0)
0631 port->outvq_full = true;
0632
0633 if (nonblock)
0634 goto done;
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645 while (!virtqueue_get_buf(out_vq, &len)
0646 && !virtqueue_is_broken(out_vq))
0647 cpu_relax();
0648 done:
0649 spin_unlock_irqrestore(&port->outvq_lock, flags);
0650
0651 port->stats.bytes_sent += in_count;
0652
0653
0654
0655
0656 return in_count;
0657 }
0658
0659
0660
0661
0662
0663 static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
0664 size_t out_count, bool to_user)
0665 {
0666 struct port_buffer *buf;
0667 unsigned long flags;
0668
0669 if (!out_count || !port_has_data(port))
0670 return 0;
0671
0672 buf = port->inbuf;
0673 out_count = min(out_count, buf->len - buf->offset);
0674
0675 if (to_user) {
0676 ssize_t ret;
0677
0678 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
0679 if (ret)
0680 return -EFAULT;
0681 } else {
0682 memcpy((__force char *)out_buf, buf->buf + buf->offset,
0683 out_count);
0684 }
0685
0686 buf->offset += out_count;
0687
0688 if (buf->offset == buf->len) {
0689
0690
0691
0692
0693 spin_lock_irqsave(&port->inbuf_lock, flags);
0694 port->inbuf = NULL;
0695
0696 if (add_inbuf(port->in_vq, buf) < 0)
0697 dev_warn(port->dev, "failed add_buf\n");
0698
0699 spin_unlock_irqrestore(&port->inbuf_lock, flags);
0700 }
0701
0702 return out_count;
0703 }
0704
0705
0706 static bool will_read_block(struct port *port)
0707 {
0708 if (!port->guest_connected) {
0709
0710 return false;
0711 }
0712 return !port_has_data(port) && port->host_connected;
0713 }
0714
0715 static bool will_write_block(struct port *port)
0716 {
0717 bool ret;
0718
0719 if (!port->guest_connected) {
0720
0721 return false;
0722 }
0723 if (!port->host_connected)
0724 return true;
0725
0726 spin_lock_irq(&port->outvq_lock);
0727
0728
0729
0730
0731 reclaim_consumed_buffers(port);
0732 ret = port->outvq_full;
0733 spin_unlock_irq(&port->outvq_lock);
0734
0735 return ret;
0736 }
0737
0738 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
0739 size_t count, loff_t *offp)
0740 {
0741 struct port *port;
0742 ssize_t ret;
0743
0744 port = filp->private_data;
0745
0746
0747 if (!port->guest_connected)
0748 return -ENODEV;
0749
0750 if (!port_has_data(port)) {
0751
0752
0753
0754
0755
0756 if (!port->host_connected)
0757 return 0;
0758 if (filp->f_flags & O_NONBLOCK)
0759 return -EAGAIN;
0760
0761 ret = wait_event_freezable(port->waitqueue,
0762 !will_read_block(port));
0763 if (ret < 0)
0764 return ret;
0765 }
0766
0767 if (!port->guest_connected)
0768 return -ENODEV;
0769
0770
0771
0772
0773
0774
0775
0776
0777
0778
0779 if (!port_has_data(port) && !port->host_connected)
0780 return 0;
0781
0782 return fill_readbuf(port, ubuf, count, true);
0783 }
0784
0785 static int wait_port_writable(struct port *port, bool nonblock)
0786 {
0787 int ret;
0788
0789 if (will_write_block(port)) {
0790 if (nonblock)
0791 return -EAGAIN;
0792
0793 ret = wait_event_freezable(port->waitqueue,
0794 !will_write_block(port));
0795 if (ret < 0)
0796 return ret;
0797 }
0798
0799 if (!port->guest_connected)
0800 return -ENODEV;
0801
0802 return 0;
0803 }
0804
0805 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
0806 size_t count, loff_t *offp)
0807 {
0808 struct port *port;
0809 struct port_buffer *buf;
0810 ssize_t ret;
0811 bool nonblock;
0812 struct scatterlist sg[1];
0813
0814
0815 if (!count)
0816 return 0;
0817
0818 port = filp->private_data;
0819
0820 nonblock = filp->f_flags & O_NONBLOCK;
0821
0822 ret = wait_port_writable(port, nonblock);
0823 if (ret < 0)
0824 return ret;
0825
0826 count = min((size_t)(32 * 1024), count);
0827
0828 buf = alloc_buf(port->portdev->vdev, count, 0);
0829 if (!buf)
0830 return -ENOMEM;
0831
0832 ret = copy_from_user(buf->buf, ubuf, count);
0833 if (ret) {
0834 ret = -EFAULT;
0835 goto free_buf;
0836 }
0837
0838
0839
0840
0841
0842
0843
0844
0845 nonblock = true;
0846 sg_init_one(sg, buf->buf, count);
0847 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
0848
0849 if (nonblock && ret > 0)
0850 goto out;
0851
0852 free_buf:
0853 free_buf(buf, true);
0854 out:
0855 return ret;
0856 }
0857
0858 struct sg_list {
0859 unsigned int n;
0860 unsigned int size;
0861 size_t len;
0862 struct scatterlist *sg;
0863 };
0864
0865 static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
0866 struct splice_desc *sd)
0867 {
0868 struct sg_list *sgl = sd->u.data;
0869 unsigned int offset, len;
0870
0871 if (sgl->n == sgl->size)
0872 return 0;
0873
0874
0875 if (pipe_buf_try_steal(pipe, buf)) {
0876
0877 get_page(buf->page);
0878 unlock_page(buf->page);
0879
0880 len = min(buf->len, sd->len);
0881 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
0882 } else {
0883
0884 struct page *page = alloc_page(GFP_KERNEL);
0885 char *src;
0886
0887 if (!page)
0888 return -ENOMEM;
0889
0890 offset = sd->pos & ~PAGE_MASK;
0891
0892 len = sd->len;
0893 if (len + offset > PAGE_SIZE)
0894 len = PAGE_SIZE - offset;
0895
0896 src = kmap_atomic(buf->page);
0897 memcpy(page_address(page) + offset, src + buf->offset, len);
0898 kunmap_atomic(src);
0899
0900 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
0901 }
0902 sgl->n++;
0903 sgl->len += len;
0904
0905 return len;
0906 }
0907
0908
0909 static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
0910 struct file *filp, loff_t *ppos,
0911 size_t len, unsigned int flags)
0912 {
0913 struct port *port = filp->private_data;
0914 struct sg_list sgl;
0915 ssize_t ret;
0916 struct port_buffer *buf;
0917 struct splice_desc sd = {
0918 .total_len = len,
0919 .flags = flags,
0920 .pos = *ppos,
0921 .u.data = &sgl,
0922 };
0923 unsigned int occupancy;
0924
0925
0926
0927
0928
0929
0930
0931 if (is_rproc_serial(port->out_vq->vdev))
0932 return -EINVAL;
0933
0934 pipe_lock(pipe);
0935 ret = 0;
0936 if (pipe_empty(pipe->head, pipe->tail))
0937 goto error_out;
0938
0939 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
0940 if (ret < 0)
0941 goto error_out;
0942
0943 occupancy = pipe_occupancy(pipe->head, pipe->tail);
0944 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
0945
0946 if (!buf) {
0947 ret = -ENOMEM;
0948 goto error_out;
0949 }
0950
0951 sgl.n = 0;
0952 sgl.len = 0;
0953 sgl.size = occupancy;
0954 sgl.sg = buf->sg;
0955 sg_init_table(sgl.sg, sgl.size);
0956 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
0957 pipe_unlock(pipe);
0958 if (likely(ret > 0))
0959 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
0960
0961 if (unlikely(ret <= 0))
0962 free_buf(buf, true);
0963 return ret;
0964
0965 error_out:
0966 pipe_unlock(pipe);
0967 return ret;
0968 }
0969
0970 static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
0971 {
0972 struct port *port;
0973 __poll_t ret;
0974
0975 port = filp->private_data;
0976 poll_wait(filp, &port->waitqueue, wait);
0977
0978 if (!port->guest_connected) {
0979
0980 return EPOLLHUP;
0981 }
0982 ret = 0;
0983 if (!will_read_block(port))
0984 ret |= EPOLLIN | EPOLLRDNORM;
0985 if (!will_write_block(port))
0986 ret |= EPOLLOUT;
0987 if (!port->host_connected)
0988 ret |= EPOLLHUP;
0989
0990 return ret;
0991 }
0992
0993 static void remove_port(struct kref *kref);
0994
0995 static int port_fops_release(struct inode *inode, struct file *filp)
0996 {
0997 struct port *port;
0998
0999 port = filp->private_data;
1000
1001
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
1018
1019
1020
1021
1022
1023
1024 kref_put(&port->kref, remove_port);
1025
1026 return 0;
1027 }
1028
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
1036 port = find_port_by_devt(cdev->dev);
1037 if (!port) {
1038
1039 return -ENXIO;
1040 }
1041 filp->private_data = port;
1042
1043
1044
1045
1046
1047 if (is_console_port(port)) {
1048 ret = -ENXIO;
1049 goto out;
1050 }
1051
1052
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
1066
1067
1068
1069 reclaim_consumed_buffers(port);
1070 spin_unlock_irq(&port->outvq_lock);
1071
1072 nonseekable_open(inode, filp);
1073
1074
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
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
1093
1094
1095
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
1111
1112
1113
1114
1115
1116
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
1143
1144
1145
1146
1147
1148 static int get_chars(u32 vtermno, char *buf, int count)
1149 {
1150 struct port *port;
1151
1152
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
1161 BUG_ON(!port->in_vq);
1162
1163 return fill_readbuf(port, (__force char __user *)buf, count, false);
1164 }
1165
1166 static void resize_console(struct port *port)
1167 {
1168 struct virtio_device *vdev;
1169
1170
1171 if (!port || !is_console_port(port))
1172 return;
1173
1174 vdev = port->portdev->vdev;
1175
1176
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
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
1197 static void notifier_del_vio(struct hvc_struct *hp, int data)
1198 {
1199 hp->irq_requested = 0;
1200 }
1201
1202
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
1213
1214
1215
1216
1217
1218
1219
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
1226 static int init_port_console(struct port *port)
1227 {
1228 int ret;
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
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
1265
1266
1267 if (early_put_chars)
1268 early_put_chars = NULL;
1269
1270
1271 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1272
1273 return 0;
1274 }
1275
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,
1295 .attrs = port_sysfs_entries,
1296 };
1297
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
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
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
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
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
1423
1424
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
1435
1436
1437
1438 port->host_connected = true;
1439 else if (!use_multiport(port->portdev)) {
1440
1441
1442
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
1455
1456
1457
1458 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1459
1460
1461
1462
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
1480 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1481 return err;
1482 }
1483
1484
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
1494 static void remove_port_data(struct port *port)
1495 {
1496 spin_lock_irq(&port->inbuf_lock);
1497
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
1508
1509
1510
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
1520 send_sigio_to_port(port);
1521
1522
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
1541
1542
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
1555
1556
1557
1558 kref_put(&port->kref, remove_port);
1559 }
1560
1561
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
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
1613
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
1638
1639
1640
1641 spin_lock_irq(&port->outvq_lock);
1642 reclaim_consumed_buffers(port);
1643 spin_unlock_irq(&port->outvq_lock);
1644
1645
1646
1647
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
1656
1657
1658 if (port->name)
1659 break;
1660
1661
1662
1663
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
1679
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
1690
1691
1692
1693 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1694 }
1695 break;
1696 }
1697 }
1698
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
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
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
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
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1782 discard_port_data(port);
1783
1784
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
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
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
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
1832
1833
1834
1835
1836
1837 resize_console(port);
1838 }
1839 }
1840
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
1868
1869
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
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
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
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
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
1968 virtio_reset_device(vdev);
1969
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
1982
1983
1984
1985
1986
1987
1988 remove_vqs(portdev);
1989 kfree(portdev);
1990 }
1991
1992
1993
1994
1995
1996
1997
1998
1999
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
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
2017 barrier();
2018
2019 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
2020 if (!portdev) {
2021 err = -ENOMEM;
2022 goto fail;
2023 }
2024
2025
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
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
2083
2084
2085 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2086 VIRTIO_CONSOLE_DEVICE_READY, 0);
2087
2088 virtcons_remove(vdev);
2089 return err;
2090 }
2091 } else {
2092
2093
2094
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
2108
2109
2110
2111
2112
2113
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
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
2166
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
2176
2177
2178 port->host_connected = false;
2179 remove_port_data(port);
2180 }
2181 remove_vqs(portdev);
2182
2183 return 0;
2184 }
2185
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
2210 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2211
2212
2213
2214
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
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
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");