11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
251df0accSAmit Shah /*
351df0accSAmit Shah * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
4ce86d35dSLinus Torvalds * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
5ce86d35dSLinus Torvalds * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
651df0accSAmit Shah */
751df0accSAmit Shah #include <linux/cdev.h>
851df0accSAmit Shah #include <linux/debugfs.h>
95e38483bSChristian Borntraeger #include <linux/completion.h>
1051df0accSAmit Shah #include <linux/device.h>
1151df0accSAmit Shah #include <linux/err.h>
12a08fa92dSAmit Shah #include <linux/freezer.h>
1351df0accSAmit Shah #include <linux/fs.h>
14eb5e89fcSMasami Hiramatsu #include <linux/splice.h>
15eb5e89fcSMasami Hiramatsu #include <linux/pagemap.h>
16e6278a54SCédric Le Goater #include <linux/idr.h>
1751df0accSAmit Shah #include <linux/init.h>
1851df0accSAmit Shah #include <linux/list.h>
1951df0accSAmit Shah #include <linux/poll.h>
2051df0accSAmit Shah #include <linux/sched.h>
2151df0accSAmit Shah #include <linux/slab.h>
2251df0accSAmit Shah #include <linux/spinlock.h>
2351df0accSAmit Shah #include <linux/virtio.h>
2451df0accSAmit Shah #include <linux/virtio_console.h>
2551df0accSAmit Shah #include <linux/wait.h>
2651df0accSAmit Shah #include <linux/workqueue.h>
27c22405c9SPaul Gortmaker #include <linux/module.h>
281b637046SSjur Brændeland #include <linux/dma-mapping.h>
2951df0accSAmit Shah #include "../tty/hvc/hvc_console.h"
3051df0accSAmit Shah
311b637046SSjur Brændeland #define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
3228962ec5SJason Wang #define VIRTCONS_MAX_PORTS 0x8000
331b637046SSjur Brændeland
3451df0accSAmit Shah /*
3551df0accSAmit Shah * This is a global struct for storing common data for all the devices
3651df0accSAmit Shah * this driver handles.
3751df0accSAmit Shah *
3851df0accSAmit Shah * Mainly, it has a linked list for all the consoles in one place so
3951df0accSAmit Shah * that callbacks from hvc for get_chars(), put_chars() work properly
4051df0accSAmit Shah * across multiple devices and multiple ports per device.
4151df0accSAmit Shah */
4251df0accSAmit Shah struct ports_driver_data {
4351df0accSAmit Shah /* Used for exporting per-port information to debugfs */
4451df0accSAmit Shah struct dentry *debugfs_dir;
4551df0accSAmit Shah
4651df0accSAmit Shah /* List of all the devices we're handling */
4751df0accSAmit Shah struct list_head portdevs;
4851df0accSAmit Shah
4951df0accSAmit Shah /* All the console devices handled by this driver */
5051df0accSAmit Shah struct list_head consoles;
5151df0accSAmit Shah };
52e6278a54SCédric Le Goater
53e6278a54SCédric Le Goater static struct ports_driver_data pdrvdata;
5451df0accSAmit Shah
5511680fdfSIvan Orlov static const struct class port_class = {
5611680fdfSIvan Orlov .name = "virtio-ports",
5711680fdfSIvan Orlov };
5811680fdfSIvan Orlov
593826835aSWei Yongjun static DEFINE_SPINLOCK(pdrvdata_lock);
603826835aSWei Yongjun static DECLARE_COMPLETION(early_console_added);
6151df0accSAmit Shah
6251df0accSAmit Shah /* This struct holds information that's relevant only for console ports */
6351df0accSAmit Shah struct console {
6451df0accSAmit Shah /* We'll place all consoles in a list in the pdrvdata struct */
6551df0accSAmit Shah struct list_head list;
6651df0accSAmit Shah
6751df0accSAmit Shah /* The hvc device associated with this console port */
6851df0accSAmit Shah struct hvc_struct *hvc;
6951df0accSAmit Shah
7051df0accSAmit Shah /* The size of the console */
7151df0accSAmit Shah struct winsize ws;
7251df0accSAmit Shah
7351df0accSAmit Shah /*
7451df0accSAmit Shah * This number identifies the number that we used to register
7551df0accSAmit Shah * with hvc in hvc_instantiate() and hvc_alloc(); this is the
7651df0accSAmit Shah * number passed on by the hvc callbacks to us to
7751df0accSAmit Shah * differentiate between the other console ports handled by
7851df0accSAmit Shah * this driver
7951df0accSAmit Shah */
8051df0accSAmit Shah u32 vtermno;
8151df0accSAmit Shah };
8251df0accSAmit Shah
83e6278a54SCédric Le Goater static DEFINE_IDA(vtermno_ida);
84e6278a54SCédric Le Goater
8551df0accSAmit Shah struct port_buffer {
8651df0accSAmit Shah char *buf;
8751df0accSAmit Shah
8851df0accSAmit Shah /* size of the buffer in *buf above */
8951df0accSAmit Shah size_t size;
9051df0accSAmit Shah
9151df0accSAmit Shah /* used length of the buffer */
9251df0accSAmit Shah size_t len;
9351df0accSAmit Shah /* offset in the buf from which to consume data */
9451df0accSAmit Shah size_t offset;
95276a3e95SSjur Brændeland
961b637046SSjur Brændeland /* DMA address of buffer */
971b637046SSjur Brændeland dma_addr_t dma;
981b637046SSjur Brændeland
991b637046SSjur Brændeland /* Device we got DMA memory from */
1001b637046SSjur Brændeland struct device *dev;
1011b637046SSjur Brændeland
1021b637046SSjur Brændeland /* List of pending dma buffers to free */
1031b637046SSjur Brændeland struct list_head list;
1041b637046SSjur Brændeland
105276a3e95SSjur Brændeland /* If sgpages == 0 then buf is used */
106276a3e95SSjur Brændeland unsigned int sgpages;
107276a3e95SSjur Brændeland
108276a3e95SSjur Brændeland /* sg is used if spages > 0. sg must be the last in is struct */
1091031eb90SGustavo A. R. Silva struct scatterlist sg[];
11051df0accSAmit Shah };
11151df0accSAmit Shah
11251df0accSAmit Shah /*
11351df0accSAmit Shah * This is a per-device struct that stores data common to all the
11451df0accSAmit Shah * ports for that device (vdev->priv).
11551df0accSAmit Shah */
11651df0accSAmit Shah struct ports_device {
11751df0accSAmit Shah /* Next portdev in the list, head is in the pdrvdata struct */
11851df0accSAmit Shah struct list_head list;
11951df0accSAmit Shah
12051df0accSAmit Shah /*
12151df0accSAmit Shah * Workqueue handlers where we process deferred work after
12251df0accSAmit Shah * notification
12351df0accSAmit Shah */
12451df0accSAmit Shah struct work_struct control_work;
125eeb8a7e8SMichael S. Tsirkin struct work_struct config_work;
12651df0accSAmit Shah
12751df0accSAmit Shah struct list_head ports;
12851df0accSAmit Shah
12951df0accSAmit Shah /* To protect the list of ports */
13051df0accSAmit Shah spinlock_t ports_lock;
13151df0accSAmit Shah
13251df0accSAmit Shah /* To protect the vq operations for the control channel */
133165b1b8bSAmit Shah spinlock_t c_ivq_lock;
1349ba5c80bSAmit Shah spinlock_t c_ovq_lock;
13551df0accSAmit Shah
1367328fa64SMichael S. Tsirkin /* max. number of ports this device can hold */
1377328fa64SMichael S. Tsirkin u32 max_nr_ports;
13851df0accSAmit Shah
13951df0accSAmit Shah /* The virtio device we're associated with */
14051df0accSAmit Shah struct virtio_device *vdev;
14151df0accSAmit Shah
14251df0accSAmit Shah /*
14351df0accSAmit Shah * A couple of virtqueues for the control channel: one for
14451df0accSAmit Shah * guest->host transfers, one for host->guest transfers
14551df0accSAmit Shah */
14651df0accSAmit Shah struct virtqueue *c_ivq, *c_ovq;
14751df0accSAmit Shah
1485e59d9a1SAndy Lutomirski /*
1495e59d9a1SAndy Lutomirski * A control packet buffer for guest->host requests, protected
1505e59d9a1SAndy Lutomirski * by c_ovq_lock.
1515e59d9a1SAndy Lutomirski */
1525e59d9a1SAndy Lutomirski struct virtio_console_control cpkt;
1535e59d9a1SAndy Lutomirski
15451df0accSAmit Shah /* Array of per-port IO virtqueues */
15551df0accSAmit Shah struct virtqueue **in_vqs, **out_vqs;
15651df0accSAmit Shah
15751df0accSAmit Shah /* Major number for this device. Ports will be created as minors. */
15851df0accSAmit Shah int chr_major;
15951df0accSAmit Shah };
16051df0accSAmit Shah
16117e5b4f2SAmit Shah struct port_stats {
16217e5b4f2SAmit Shah unsigned long bytes_sent, bytes_received, bytes_discarded;
16317e5b4f2SAmit Shah };
16417e5b4f2SAmit Shah
16551df0accSAmit Shah /* This struct holds the per-port data */
16651df0accSAmit Shah struct port {
16751df0accSAmit Shah /* Next port in the list, head is in the ports_device */
16851df0accSAmit Shah struct list_head list;
16951df0accSAmit Shah
17051df0accSAmit Shah /* Pointer to the parent virtio_console device */
17151df0accSAmit Shah struct ports_device *portdev;
17251df0accSAmit Shah
17351df0accSAmit Shah /* The current buffer from which data has to be fed to readers */
17451df0accSAmit Shah struct port_buffer *inbuf;
17551df0accSAmit Shah
17651df0accSAmit Shah /*
17751df0accSAmit Shah * To protect the operations on the in_vq associated with this
17851df0accSAmit Shah * port. Has to be a spinlock because it can be called from
17951df0accSAmit Shah * interrupt context (get_char()).
18051df0accSAmit Shah */
18151df0accSAmit Shah spinlock_t inbuf_lock;
18251df0accSAmit Shah
18351df0accSAmit Shah /* Protect the operations on the out_vq. */
18451df0accSAmit Shah spinlock_t outvq_lock;
18551df0accSAmit Shah
18651df0accSAmit Shah /* The IO vqs for this port */
18751df0accSAmit Shah struct virtqueue *in_vq, *out_vq;
18851df0accSAmit Shah
18951df0accSAmit Shah /* File in the debugfs directory that exposes this port's information */
19051df0accSAmit Shah struct dentry *debugfs_file;
19151df0accSAmit Shah
19251df0accSAmit Shah /*
19317e5b4f2SAmit Shah * Keep count of the bytes sent, received and discarded for
19417e5b4f2SAmit Shah * this port for accounting and debugging purposes. These
19517e5b4f2SAmit Shah * counts are not reset across port open / close events.
19617e5b4f2SAmit Shah */
19717e5b4f2SAmit Shah struct port_stats stats;
19817e5b4f2SAmit Shah
19917e5b4f2SAmit Shah /*
20051df0accSAmit Shah * The entries in this struct will be valid if this port is
20151df0accSAmit Shah * hooked up to an hvc console
20251df0accSAmit Shah */
20351df0accSAmit Shah struct console cons;
20451df0accSAmit Shah
20551df0accSAmit Shah /* Each port associates with a separate char device */
20651df0accSAmit Shah struct cdev *cdev;
20751df0accSAmit Shah struct device *dev;
20851df0accSAmit Shah
20951df0accSAmit Shah /* Reference-counting to handle port hot-unplugs and file operations */
21051df0accSAmit Shah struct kref kref;
21151df0accSAmit Shah
21251df0accSAmit Shah /* A waitqueue for poll() or blocking read operations */
21351df0accSAmit Shah wait_queue_head_t waitqueue;
21451df0accSAmit Shah
21551df0accSAmit Shah /* The 'name' of the port that we expose via sysfs properties */
21651df0accSAmit Shah char *name;
21751df0accSAmit Shah
21851df0accSAmit Shah /* We can notify apps of host connect / disconnect events via SIGIO */
21951df0accSAmit Shah struct fasync_struct *async_queue;
22051df0accSAmit Shah
22151df0accSAmit Shah /* The 'id' to identify the port with the Host */
22251df0accSAmit Shah u32 id;
22351df0accSAmit Shah
22451df0accSAmit Shah bool outvq_full;
22551df0accSAmit Shah
22651df0accSAmit Shah /* Is the host device open */
22751df0accSAmit Shah bool host_connected;
22851df0accSAmit Shah
22951df0accSAmit Shah /* We should allow only one process to open a port */
23051df0accSAmit Shah bool guest_connected;
23151df0accSAmit Shah };
23251df0accSAmit Shah
23351df0accSAmit Shah /* This is the very early arch-specified put chars function. */
23451df0accSAmit Shah static int (*early_put_chars)(u32, const char *, int);
23551df0accSAmit Shah
find_port_by_vtermno(u32 vtermno)23651df0accSAmit Shah static struct port *find_port_by_vtermno(u32 vtermno)
23751df0accSAmit Shah {
23851df0accSAmit Shah struct port *port;
23951df0accSAmit Shah struct console *cons;
24051df0accSAmit Shah unsigned long flags;
24151df0accSAmit Shah
24251df0accSAmit Shah spin_lock_irqsave(&pdrvdata_lock, flags);
24351df0accSAmit Shah list_for_each_entry(cons, &pdrvdata.consoles, list) {
24451df0accSAmit Shah if (cons->vtermno == vtermno) {
24551df0accSAmit Shah port = container_of(cons, struct port, cons);
24651df0accSAmit Shah goto out;
24751df0accSAmit Shah }
24851df0accSAmit Shah }
24951df0accSAmit Shah port = NULL;
25051df0accSAmit Shah out:
25151df0accSAmit Shah spin_unlock_irqrestore(&pdrvdata_lock, flags);
25251df0accSAmit Shah return port;
25351df0accSAmit Shah }
25451df0accSAmit Shah
find_port_by_devt_in_portdev(struct ports_device * portdev,dev_t dev)25551df0accSAmit Shah static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
25651df0accSAmit Shah dev_t dev)
25751df0accSAmit Shah {
25851df0accSAmit Shah struct port *port;
25951df0accSAmit Shah unsigned long flags;
26051df0accSAmit Shah
26151df0accSAmit Shah spin_lock_irqsave(&portdev->ports_lock, flags);
262057b82beSAmit Shah list_for_each_entry(port, &portdev->ports, list) {
263057b82beSAmit Shah if (port->cdev->dev == dev) {
264057b82beSAmit Shah kref_get(&port->kref);
26551df0accSAmit Shah goto out;
266057b82beSAmit Shah }
267057b82beSAmit Shah }
26851df0accSAmit Shah port = NULL;
26951df0accSAmit Shah out:
27051df0accSAmit Shah spin_unlock_irqrestore(&portdev->ports_lock, flags);
27151df0accSAmit Shah
27251df0accSAmit Shah return port;
27351df0accSAmit Shah }
27451df0accSAmit Shah
find_port_by_devt(dev_t dev)27551df0accSAmit Shah static struct port *find_port_by_devt(dev_t dev)
27651df0accSAmit Shah {
27751df0accSAmit Shah struct ports_device *portdev;
27851df0accSAmit Shah struct port *port;
27951df0accSAmit Shah unsigned long flags;
28051df0accSAmit Shah
28151df0accSAmit Shah spin_lock_irqsave(&pdrvdata_lock, flags);
28251df0accSAmit Shah list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
28351df0accSAmit Shah port = find_port_by_devt_in_portdev(portdev, dev);
28451df0accSAmit Shah if (port)
28551df0accSAmit Shah goto out;
28651df0accSAmit Shah }
28751df0accSAmit Shah port = NULL;
28851df0accSAmit Shah out:
28951df0accSAmit Shah spin_unlock_irqrestore(&pdrvdata_lock, flags);
29051df0accSAmit Shah return port;
29151df0accSAmit Shah }
29251df0accSAmit Shah
find_port_by_id(struct ports_device * portdev,u32 id)29351df0accSAmit Shah static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
29451df0accSAmit Shah {
29551df0accSAmit Shah struct port *port;
29651df0accSAmit Shah unsigned long flags;
29751df0accSAmit Shah
29851df0accSAmit Shah spin_lock_irqsave(&portdev->ports_lock, flags);
29951df0accSAmit Shah list_for_each_entry(port, &portdev->ports, list)
30051df0accSAmit Shah if (port->id == id)
30151df0accSAmit Shah goto out;
30251df0accSAmit Shah port = NULL;
30351df0accSAmit Shah out:
30451df0accSAmit Shah spin_unlock_irqrestore(&portdev->ports_lock, flags);
30551df0accSAmit Shah
30651df0accSAmit Shah return port;
30751df0accSAmit Shah }
30851df0accSAmit Shah
find_port_by_vq(struct ports_device * portdev,struct virtqueue * vq)30951df0accSAmit Shah static struct port *find_port_by_vq(struct ports_device *portdev,
31051df0accSAmit Shah struct virtqueue *vq)
31151df0accSAmit Shah {
31251df0accSAmit Shah struct port *port;
31351df0accSAmit Shah unsigned long flags;
31451df0accSAmit Shah
31551df0accSAmit Shah spin_lock_irqsave(&portdev->ports_lock, flags);
31651df0accSAmit Shah list_for_each_entry(port, &portdev->ports, list)
31751df0accSAmit Shah if (port->in_vq == vq || port->out_vq == vq)
31851df0accSAmit Shah goto out;
31951df0accSAmit Shah port = NULL;
32051df0accSAmit Shah out:
32151df0accSAmit Shah spin_unlock_irqrestore(&portdev->ports_lock, flags);
32251df0accSAmit Shah return port;
32351df0accSAmit Shah }
32451df0accSAmit Shah
is_console_port(struct port * port)32551df0accSAmit Shah static bool is_console_port(struct port *port)
32651df0accSAmit Shah {
32751df0accSAmit Shah if (port->cons.hvc)
32851df0accSAmit Shah return true;
32951df0accSAmit Shah return false;
33051df0accSAmit Shah }
33151df0accSAmit Shah
is_rproc_serial(const struct virtio_device * vdev)3321b637046SSjur Brændeland static bool is_rproc_serial(const struct virtio_device *vdev)
3331b637046SSjur Brændeland {
3341b637046SSjur Brændeland return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
3351b637046SSjur Brændeland }
3361b637046SSjur Brændeland
use_multiport(struct ports_device * portdev)33751df0accSAmit Shah static inline bool use_multiport(struct ports_device *portdev)
33851df0accSAmit Shah {
33951df0accSAmit Shah /*
34051df0accSAmit Shah * This condition can be true when put_chars is called from
34151df0accSAmit Shah * early_init
34251df0accSAmit Shah */
34351df0accSAmit Shah if (!portdev->vdev)
344f580d730SJoe Perches return false;
345e16e12beSMichael S. Tsirkin return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
34651df0accSAmit Shah }
34751df0accSAmit Shah
3481b637046SSjur Brændeland static DEFINE_SPINLOCK(dma_bufs_lock);
3491b637046SSjur Brændeland static LIST_HEAD(pending_free_dma_bufs);
3501b637046SSjur Brændeland
free_buf(struct port_buffer * buf,bool can_sleep)3511b637046SSjur Brændeland static void free_buf(struct port_buffer *buf, bool can_sleep)
35251df0accSAmit Shah {
353276a3e95SSjur Brændeland unsigned int i;
354276a3e95SSjur Brændeland
355276a3e95SSjur Brændeland for (i = 0; i < buf->sgpages; i++) {
356276a3e95SSjur Brændeland struct page *page = sg_page(&buf->sg[i]);
357276a3e95SSjur Brændeland if (!page)
358276a3e95SSjur Brændeland break;
359276a3e95SSjur Brændeland put_page(page);
360276a3e95SSjur Brændeland }
361276a3e95SSjur Brændeland
3621b637046SSjur Brændeland if (!buf->dev) {
36351df0accSAmit Shah kfree(buf->buf);
3641b637046SSjur Brændeland } else if (is_rproc_enabled) {
3651b637046SSjur Brændeland unsigned long flags;
3661b637046SSjur Brændeland
3671b637046SSjur Brændeland /* dma_free_coherent requires interrupts to be enabled. */
3681b637046SSjur Brændeland if (!can_sleep) {
3691b637046SSjur Brændeland /* queue up dma-buffers to be freed later */
3701b637046SSjur Brændeland spin_lock_irqsave(&dma_bufs_lock, flags);
3711b637046SSjur Brændeland list_add_tail(&buf->list, &pending_free_dma_bufs);
3721b637046SSjur Brændeland spin_unlock_irqrestore(&dma_bufs_lock, flags);
3731b637046SSjur Brændeland return;
3741b637046SSjur Brændeland }
3751b637046SSjur Brændeland dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
3761b637046SSjur Brændeland
3771b637046SSjur Brændeland /* Release device refcnt and allow it to be freed */
3781b637046SSjur Brændeland put_device(buf->dev);
3791b637046SSjur Brændeland }
3801b637046SSjur Brændeland
38151df0accSAmit Shah kfree(buf);
38251df0accSAmit Shah }
38351df0accSAmit Shah
reclaim_dma_bufs(void)3841b637046SSjur Brændeland static void reclaim_dma_bufs(void)
3851b637046SSjur Brændeland {
3861b637046SSjur Brændeland unsigned long flags;
3871b637046SSjur Brændeland struct port_buffer *buf, *tmp;
3881b637046SSjur Brændeland LIST_HEAD(tmp_list);
3891b637046SSjur Brændeland
3901b637046SSjur Brændeland if (list_empty(&pending_free_dma_bufs))
3911b637046SSjur Brændeland return;
3921b637046SSjur Brændeland
3931b637046SSjur Brændeland /* Create a copy of the pending_free_dma_bufs while holding the lock */
3941b637046SSjur Brændeland spin_lock_irqsave(&dma_bufs_lock, flags);
3951b637046SSjur Brændeland list_cut_position(&tmp_list, &pending_free_dma_bufs,
3961b637046SSjur Brændeland pending_free_dma_bufs.prev);
3971b637046SSjur Brændeland spin_unlock_irqrestore(&dma_bufs_lock, flags);
3981b637046SSjur Brændeland
3991b637046SSjur Brændeland /* Release the dma buffers, without irqs enabled */
4001b637046SSjur Brændeland list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
4011b637046SSjur Brændeland list_del(&buf->list);
4021b637046SSjur Brændeland free_buf(buf, true);
4031b637046SSjur Brændeland }
4041b637046SSjur Brændeland }
4051b637046SSjur Brændeland
alloc_buf(struct virtio_device * vdev,size_t buf_size,int pages)4062855b335SMichael S. Tsirkin static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
407276a3e95SSjur Brændeland int pages)
40851df0accSAmit Shah {
40951df0accSAmit Shah struct port_buffer *buf;
41051df0accSAmit Shah
4111b637046SSjur Brændeland reclaim_dma_bufs();
4121b637046SSjur Brændeland
413276a3e95SSjur Brændeland /*
414276a3e95SSjur Brændeland * Allocate buffer and the sg list. The sg list array is allocated
415276a3e95SSjur Brændeland * directly after the port_buffer struct.
416276a3e95SSjur Brændeland */
4175b572e25SMatthew Wilcox buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
41851df0accSAmit Shah if (!buf)
41951df0accSAmit Shah goto fail;
420276a3e95SSjur Brændeland
421276a3e95SSjur Brændeland buf->sgpages = pages;
422276a3e95SSjur Brændeland if (pages > 0) {
4231b637046SSjur Brændeland buf->dev = NULL;
424276a3e95SSjur Brændeland buf->buf = NULL;
425276a3e95SSjur Brændeland return buf;
426276a3e95SSjur Brændeland }
427276a3e95SSjur Brændeland
4282855b335SMichael S. Tsirkin if (is_rproc_serial(vdev)) {
4291b637046SSjur Brændeland /*
4301b637046SSjur Brændeland * Allocate DMA memory from ancestor. When a virtio
4311b637046SSjur Brændeland * device is created by remoteproc, the DMA memory is
4329d516aa8SAlexander Lobakin * associated with the parent device:
4339d516aa8SAlexander Lobakin * virtioY => remoteprocX#vdevYbuffer.
4341b637046SSjur Brændeland */
4359d516aa8SAlexander Lobakin buf->dev = vdev->dev.parent;
4369d516aa8SAlexander Lobakin if (!buf->dev)
4371b637046SSjur Brændeland goto free_buf;
4381b637046SSjur Brændeland
4391b637046SSjur Brændeland /* Increase device refcnt to avoid freeing it */
4401b637046SSjur Brændeland get_device(buf->dev);
4411b637046SSjur Brændeland buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
4421b637046SSjur Brændeland GFP_KERNEL);
4431b637046SSjur Brændeland } else {
4441b637046SSjur Brændeland buf->dev = NULL;
4450127f685SSjur Brændeland buf->buf = kmalloc(buf_size, GFP_KERNEL);
4461b637046SSjur Brændeland }
4471b637046SSjur Brændeland
44851df0accSAmit Shah if (!buf->buf)
44951df0accSAmit Shah goto free_buf;
45051df0accSAmit Shah buf->len = 0;
45151df0accSAmit Shah buf->offset = 0;
45251df0accSAmit Shah buf->size = buf_size;
45351df0accSAmit Shah return buf;
45451df0accSAmit Shah
45551df0accSAmit Shah free_buf:
45651df0accSAmit Shah kfree(buf);
45751df0accSAmit Shah fail:
45851df0accSAmit Shah return NULL;
45951df0accSAmit Shah }
46051df0accSAmit Shah
46151df0accSAmit Shah /* Callers should take appropriate locks */
get_inbuf(struct port * port)462defde669SAmit Shah static struct port_buffer *get_inbuf(struct port *port)
46351df0accSAmit Shah {
46451df0accSAmit Shah struct port_buffer *buf;
46551df0accSAmit Shah unsigned int len;
46651df0accSAmit Shah
467d25a9ddaSAmit Shah if (port->inbuf)
468d25a9ddaSAmit Shah return port->inbuf;
469d25a9ddaSAmit Shah
470d25a9ddaSAmit Shah buf = virtqueue_get_buf(port->in_vq, &len);
47151df0accSAmit Shah if (buf) {
472d00d8da5SXie Yongji buf->len = min_t(size_t, len, buf->size);
47351df0accSAmit Shah buf->offset = 0;
47417e5b4f2SAmit Shah port->stats.bytes_received += len;
47551df0accSAmit Shah }
47651df0accSAmit Shah return buf;
47751df0accSAmit Shah }
47851df0accSAmit Shah
47951df0accSAmit Shah /*
48051df0accSAmit Shah * Create a scatter-gather list representing our input buffer and put
48151df0accSAmit Shah * it in the queue.
48251df0accSAmit Shah *
48351df0accSAmit Shah * Callers should take appropriate locks.
48451df0accSAmit Shah */
add_inbuf(struct virtqueue * vq,struct port_buffer * buf)48551df0accSAmit Shah static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
48651df0accSAmit Shah {
48751df0accSAmit Shah struct scatterlist sg[1];
48851df0accSAmit Shah int ret;
48951df0accSAmit Shah
49051df0accSAmit Shah sg_init_one(sg, buf->buf, buf->size);
49151df0accSAmit Shah
4926797999dSRusty Russell ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
49351df0accSAmit Shah virtqueue_kick(vq);
49449e86f16SAmit Shah if (!ret)
49549e86f16SAmit Shah ret = vq->num_free;
49651df0accSAmit Shah return ret;
49751df0accSAmit Shah }
49851df0accSAmit Shah
49951df0accSAmit Shah /* Discard any unread data this port has. Callers lockers. */
discard_port_data(struct port * port)50051df0accSAmit Shah static void discard_port_data(struct port *port)
50151df0accSAmit Shah {
50251df0accSAmit Shah struct port_buffer *buf;
5032d24cdaaSAmit Shah unsigned int err;
50451df0accSAmit Shah
505d7a62cd0SAmit Shah if (!port->portdev) {
506d7a62cd0SAmit Shah /* Device has been unplugged. vqs are already gone. */
507d7a62cd0SAmit Shah return;
508d7a62cd0SAmit Shah }
5092d24cdaaSAmit Shah buf = get_inbuf(port);
51051df0accSAmit Shah
511ce072a0cSAmit Shah err = 0;
51251df0accSAmit Shah while (buf) {
51317e5b4f2SAmit Shah port->stats.bytes_discarded += buf->len - buf->offset;
5142d24cdaaSAmit Shah if (add_inbuf(port->in_vq, buf) < 0) {
515ce072a0cSAmit Shah err++;
5161b637046SSjur Brændeland free_buf(buf, false);
51751df0accSAmit Shah }
51851df0accSAmit Shah port->inbuf = NULL;
5192d24cdaaSAmit Shah buf = get_inbuf(port);
5202d24cdaaSAmit Shah }
521ce072a0cSAmit Shah if (err)
52251df0accSAmit Shah dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
523ce072a0cSAmit Shah err);
52451df0accSAmit Shah }
52551df0accSAmit Shah
port_has_data(struct port * port)52651df0accSAmit Shah static bool port_has_data(struct port *port)
52751df0accSAmit Shah {
52851df0accSAmit Shah unsigned long flags;
52951df0accSAmit Shah bool ret;
53051df0accSAmit Shah
53151df0accSAmit Shah ret = false;
532d25a9ddaSAmit Shah spin_lock_irqsave(&port->inbuf_lock, flags);
533d25a9ddaSAmit Shah port->inbuf = get_inbuf(port);
534d25a9ddaSAmit Shah if (port->inbuf)
535d25a9ddaSAmit Shah ret = true;
536d25a9ddaSAmit Shah
53751df0accSAmit Shah spin_unlock_irqrestore(&port->inbuf_lock, flags);
53851df0accSAmit Shah return ret;
53951df0accSAmit Shah }
54051df0accSAmit Shah
__send_control_msg(struct ports_device * portdev,u32 port_id,unsigned int event,unsigned int value)54151df0accSAmit Shah static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
54251df0accSAmit Shah unsigned int event, unsigned int value)
54351df0accSAmit Shah {
54451df0accSAmit Shah struct scatterlist sg[1];
54551df0accSAmit Shah struct virtqueue *vq;
54651df0accSAmit Shah unsigned int len;
54751df0accSAmit Shah
54851df0accSAmit Shah if (!use_multiport(portdev))
54951df0accSAmit Shah return 0;
55051df0accSAmit Shah
55151df0accSAmit Shah vq = portdev->c_ovq;
55251df0accSAmit Shah
5539ba5c80bSAmit Shah spin_lock(&portdev->c_ovq_lock);
5545e59d9a1SAndy Lutomirski
5555e59d9a1SAndy Lutomirski portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
5565e59d9a1SAndy Lutomirski portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
5575e59d9a1SAndy Lutomirski portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
5585e59d9a1SAndy Lutomirski
5595e59d9a1SAndy Lutomirski sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
5605e59d9a1SAndy Lutomirski
5615e59d9a1SAndy Lutomirski if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
56251df0accSAmit Shah virtqueue_kick(vq);
56340e4dc53SHeinz Graalfs while (!virtqueue_get_buf(vq, &len)
56440e4dc53SHeinz Graalfs && !virtqueue_is_broken(vq))
56551df0accSAmit Shah cpu_relax();
56651df0accSAmit Shah }
5675e59d9a1SAndy Lutomirski
5689ba5c80bSAmit Shah spin_unlock(&portdev->c_ovq_lock);
56951df0accSAmit Shah return 0;
57051df0accSAmit Shah }
57151df0accSAmit Shah
send_control_msg(struct port * port,unsigned int event,unsigned int value)57251df0accSAmit Shah static ssize_t send_control_msg(struct port *port, unsigned int event,
57351df0accSAmit Shah unsigned int value)
57451df0accSAmit Shah {
57551df0accSAmit Shah /* Did the port get unplugged before userspace closed it? */
57651df0accSAmit Shah if (port->portdev)
57751df0accSAmit Shah return __send_control_msg(port->portdev, port->id, event, value);
57851df0accSAmit Shah return 0;
57951df0accSAmit Shah }
58051df0accSAmit Shah
581eb5e89fcSMasami Hiramatsu
58251df0accSAmit Shah /* Callers must take the port->outvq_lock */
reclaim_consumed_buffers(struct port * port)58351df0accSAmit Shah static void reclaim_consumed_buffers(struct port *port)
58451df0accSAmit Shah {
585276a3e95SSjur Brændeland struct port_buffer *buf;
58651df0accSAmit Shah unsigned int len;
58751df0accSAmit Shah
588d7a62cd0SAmit Shah if (!port->portdev) {
589d7a62cd0SAmit Shah /* Device has been unplugged. vqs are already gone. */
590d7a62cd0SAmit Shah return;
591d7a62cd0SAmit Shah }
592276a3e95SSjur Brændeland while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
5931b637046SSjur Brændeland free_buf(buf, false);
59451df0accSAmit Shah port->outvq_full = false;
59551df0accSAmit Shah }
59651df0accSAmit Shah }
59751df0accSAmit Shah
__send_to_port(struct port * port,struct scatterlist * sg,int nents,size_t in_count,void * data,bool nonblock)598eb5e89fcSMasami Hiramatsu static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
599eb5e89fcSMasami Hiramatsu int nents, size_t in_count,
600276a3e95SSjur Brændeland void *data, bool nonblock)
60151df0accSAmit Shah {
60251df0accSAmit Shah struct virtqueue *out_vq;
603589575a2SRusty Russell int err;
60451df0accSAmit Shah unsigned long flags;
60551df0accSAmit Shah unsigned int len;
60651df0accSAmit Shah
60751df0accSAmit Shah out_vq = port->out_vq;
60851df0accSAmit Shah
60951df0accSAmit Shah spin_lock_irqsave(&port->outvq_lock, flags);
61051df0accSAmit Shah
61151df0accSAmit Shah reclaim_consumed_buffers(port);
61251df0accSAmit Shah
6136797999dSRusty Russell err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
61451df0accSAmit Shah
61551df0accSAmit Shah /* Tell Host to go! */
61651df0accSAmit Shah virtqueue_kick(out_vq);
61751df0accSAmit Shah
618589575a2SRusty Russell if (err) {
61951df0accSAmit Shah in_count = 0;
62051df0accSAmit Shah goto done;
62151df0accSAmit Shah }
62251df0accSAmit Shah
623589575a2SRusty Russell if (out_vq->num_free == 0)
62451df0accSAmit Shah port->outvq_full = true;
62551df0accSAmit Shah
62651df0accSAmit Shah if (nonblock)
62751df0accSAmit Shah goto done;
62851df0accSAmit Shah
62951df0accSAmit Shah /*
63051df0accSAmit Shah * Wait till the host acknowledges it pushed out the data we
63151df0accSAmit Shah * sent. This is done for data from the hvc_console; the tty
63251df0accSAmit Shah * operations are performed with spinlocks held so we can't
63351df0accSAmit Shah * sleep here. An alternative would be to copy the data to a
63451df0accSAmit Shah * buffer and relax the spinning requirement. The downside is
63551df0accSAmit Shah * we need to kmalloc a GFP_ATOMIC buffer each time the
63651df0accSAmit Shah * console driver writes something out.
63751df0accSAmit Shah */
63840e4dc53SHeinz Graalfs while (!virtqueue_get_buf(out_vq, &len)
63940e4dc53SHeinz Graalfs && !virtqueue_is_broken(out_vq))
64051df0accSAmit Shah cpu_relax();
64151df0accSAmit Shah done:
64251df0accSAmit Shah spin_unlock_irqrestore(&port->outvq_lock, flags);
64317e5b4f2SAmit Shah
64417e5b4f2SAmit Shah port->stats.bytes_sent += in_count;
64551df0accSAmit Shah /*
64651df0accSAmit Shah * We're expected to return the amount of data we wrote -- all
64751df0accSAmit Shah * of it
64851df0accSAmit Shah */
64951df0accSAmit Shah return in_count;
65051df0accSAmit Shah }
65151df0accSAmit Shah
65251df0accSAmit Shah /*
65351df0accSAmit Shah * Give out the data that's requested from the buffer that we have
65451df0accSAmit Shah * queued up.
65551df0accSAmit Shah */
fill_readbuf(struct port * port,char __user * out_buf,size_t out_count,bool to_user)65648b36066SMichael S. Tsirkin static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
65748b36066SMichael S. Tsirkin size_t out_count, bool to_user)
65851df0accSAmit Shah {
65951df0accSAmit Shah struct port_buffer *buf;
66051df0accSAmit Shah unsigned long flags;
66151df0accSAmit Shah
66251df0accSAmit Shah if (!out_count || !port_has_data(port))
66351df0accSAmit Shah return 0;
66451df0accSAmit Shah
66551df0accSAmit Shah buf = port->inbuf;
66651df0accSAmit Shah out_count = min(out_count, buf->len - buf->offset);
66751df0accSAmit Shah
66851df0accSAmit Shah if (to_user) {
66951df0accSAmit Shah ssize_t ret;
67051df0accSAmit Shah
67151df0accSAmit Shah ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
67251df0accSAmit Shah if (ret)
67351df0accSAmit Shah return -EFAULT;
67451df0accSAmit Shah } else {
67548b36066SMichael S. Tsirkin memcpy((__force char *)out_buf, buf->buf + buf->offset,
67648b36066SMichael S. Tsirkin out_count);
67751df0accSAmit Shah }
67851df0accSAmit Shah
67951df0accSAmit Shah buf->offset += out_count;
68051df0accSAmit Shah
68151df0accSAmit Shah if (buf->offset == buf->len) {
68251df0accSAmit Shah /*
68351df0accSAmit Shah * We're done using all the data in this buffer.
68451df0accSAmit Shah * Re-queue so that the Host can send us more data.
68551df0accSAmit Shah */
68651df0accSAmit Shah spin_lock_irqsave(&port->inbuf_lock, flags);
68751df0accSAmit Shah port->inbuf = NULL;
68851df0accSAmit Shah
68951df0accSAmit Shah if (add_inbuf(port->in_vq, buf) < 0)
69051df0accSAmit Shah dev_warn(port->dev, "failed add_buf\n");
69151df0accSAmit Shah
69251df0accSAmit Shah spin_unlock_irqrestore(&port->inbuf_lock, flags);
69351df0accSAmit Shah }
69451df0accSAmit Shah /* Return the number of bytes actually copied */
69551df0accSAmit Shah return out_count;
69651df0accSAmit Shah }
69751df0accSAmit Shah
69851df0accSAmit Shah /* The condition that must be true for polling to end */
will_read_block(struct port * port)69951df0accSAmit Shah static bool will_read_block(struct port *port)
70051df0accSAmit Shah {
70151df0accSAmit Shah if (!port->guest_connected) {
70251df0accSAmit Shah /* Port got hot-unplugged. Let's exit. */
70351df0accSAmit Shah return false;
70451df0accSAmit Shah }
70551df0accSAmit Shah return !port_has_data(port) && port->host_connected;
70651df0accSAmit Shah }
70751df0accSAmit Shah
will_write_block(struct port * port)70851df0accSAmit Shah static bool will_write_block(struct port *port)
70951df0accSAmit Shah {
71051df0accSAmit Shah bool ret;
71151df0accSAmit Shah
71251df0accSAmit Shah if (!port->guest_connected) {
71351df0accSAmit Shah /* Port got hot-unplugged. Let's exit. */
71451df0accSAmit Shah return false;
71551df0accSAmit Shah }
71651df0accSAmit Shah if (!port->host_connected)
71751df0accSAmit Shah return true;
71851df0accSAmit Shah
71951df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
72051df0accSAmit Shah /*
72151df0accSAmit Shah * Check if the Host has consumed any buffers since we last
72251df0accSAmit Shah * sent data (this is only applicable for nonblocking ports).
72351df0accSAmit Shah */
72451df0accSAmit Shah reclaim_consumed_buffers(port);
72551df0accSAmit Shah ret = port->outvq_full;
72651df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
72751df0accSAmit Shah
72851df0accSAmit Shah return ret;
72951df0accSAmit Shah }
73051df0accSAmit Shah
port_fops_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)73151df0accSAmit Shah static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
73251df0accSAmit Shah size_t count, loff_t *offp)
73351df0accSAmit Shah {
73451df0accSAmit Shah struct port *port;
73551df0accSAmit Shah ssize_t ret;
73651df0accSAmit Shah
73751df0accSAmit Shah port = filp->private_data;
73851df0accSAmit Shah
73996f97a83SAmit Shah /* Port is hot-unplugged. */
74096f97a83SAmit Shah if (!port->guest_connected)
74196f97a83SAmit Shah return -ENODEV;
74296f97a83SAmit Shah
74351df0accSAmit Shah if (!port_has_data(port)) {
74451df0accSAmit Shah /*
74551df0accSAmit Shah * If nothing's connected on the host just return 0 in
74651df0accSAmit Shah * case of list_empty; this tells the userspace app
74751df0accSAmit Shah * that there's no connection
74851df0accSAmit Shah */
74951df0accSAmit Shah if (!port->host_connected)
75051df0accSAmit Shah return 0;
75151df0accSAmit Shah if (filp->f_flags & O_NONBLOCK)
75251df0accSAmit Shah return -EAGAIN;
75351df0accSAmit Shah
754a08fa92dSAmit Shah ret = wait_event_freezable(port->waitqueue,
75551df0accSAmit Shah !will_read_block(port));
75651df0accSAmit Shah if (ret < 0)
75751df0accSAmit Shah return ret;
75851df0accSAmit Shah }
75996f97a83SAmit Shah /* Port got hot-unplugged while we were waiting above. */
76051df0accSAmit Shah if (!port->guest_connected)
76151df0accSAmit Shah return -ENODEV;
76251df0accSAmit Shah /*
76351df0accSAmit Shah * We could've received a disconnection message while we were
76451df0accSAmit Shah * waiting for more data.
76551df0accSAmit Shah *
76651df0accSAmit Shah * This check is not clubbed in the if() statement above as we
76751df0accSAmit Shah * might receive some data as well as the host could get
76851df0accSAmit Shah * disconnected after we got woken up from our wait. So we
76951df0accSAmit Shah * really want to give off whatever data we have and only then
77051df0accSAmit Shah * check for host_connected.
77151df0accSAmit Shah */
77251df0accSAmit Shah if (!port_has_data(port) && !port->host_connected)
77351df0accSAmit Shah return 0;
77451df0accSAmit Shah
77551df0accSAmit Shah return fill_readbuf(port, ubuf, count, true);
77651df0accSAmit Shah }
77751df0accSAmit Shah
wait_port_writable(struct port * port,bool nonblock)778efe75d24SMasami Hiramatsu static int wait_port_writable(struct port *port, bool nonblock)
779efe75d24SMasami Hiramatsu {
780efe75d24SMasami Hiramatsu int ret;
781efe75d24SMasami Hiramatsu
782efe75d24SMasami Hiramatsu if (will_write_block(port)) {
783efe75d24SMasami Hiramatsu if (nonblock)
784efe75d24SMasami Hiramatsu return -EAGAIN;
785efe75d24SMasami Hiramatsu
786efe75d24SMasami Hiramatsu ret = wait_event_freezable(port->waitqueue,
787efe75d24SMasami Hiramatsu !will_write_block(port));
788efe75d24SMasami Hiramatsu if (ret < 0)
789efe75d24SMasami Hiramatsu return ret;
790efe75d24SMasami Hiramatsu }
791efe75d24SMasami Hiramatsu /* Port got hot-unplugged. */
792efe75d24SMasami Hiramatsu if (!port->guest_connected)
793efe75d24SMasami Hiramatsu return -ENODEV;
794efe75d24SMasami Hiramatsu
795efe75d24SMasami Hiramatsu return 0;
796efe75d24SMasami Hiramatsu }
797efe75d24SMasami Hiramatsu
port_fops_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * offp)79851df0accSAmit Shah static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
79951df0accSAmit Shah size_t count, loff_t *offp)
80051df0accSAmit Shah {
80151df0accSAmit Shah struct port *port;
802276a3e95SSjur Brændeland struct port_buffer *buf;
80351df0accSAmit Shah ssize_t ret;
80451df0accSAmit Shah bool nonblock;
805276a3e95SSjur Brændeland struct scatterlist sg[1];
80651df0accSAmit Shah
80751df0accSAmit Shah /* Userspace could be out to fool us */
80851df0accSAmit Shah if (!count)
80951df0accSAmit Shah return 0;
81051df0accSAmit Shah
81151df0accSAmit Shah port = filp->private_data;
81251df0accSAmit Shah
81351df0accSAmit Shah nonblock = filp->f_flags & O_NONBLOCK;
81451df0accSAmit Shah
815efe75d24SMasami Hiramatsu ret = wait_port_writable(port, nonblock);
81651df0accSAmit Shah if (ret < 0)
81751df0accSAmit Shah return ret;
81851df0accSAmit Shah
81951df0accSAmit Shah count = min((size_t)(32 * 1024), count);
82051df0accSAmit Shah
8212855b335SMichael S. Tsirkin buf = alloc_buf(port->portdev->vdev, count, 0);
82251df0accSAmit Shah if (!buf)
82351df0accSAmit Shah return -ENOMEM;
82451df0accSAmit Shah
825276a3e95SSjur Brændeland ret = copy_from_user(buf->buf, ubuf, count);
82651df0accSAmit Shah if (ret) {
82751df0accSAmit Shah ret = -EFAULT;
82851df0accSAmit Shah goto free_buf;
82951df0accSAmit Shah }
83051df0accSAmit Shah
83151df0accSAmit Shah /*
83251df0accSAmit Shah * We now ask send_buf() to not spin for generic ports -- we
83351df0accSAmit Shah * can re-use the same code path that non-blocking file
83451df0accSAmit Shah * descriptors take for blocking file descriptors since the
83551df0accSAmit Shah * wait is already done and we're certain the write will go
83651df0accSAmit Shah * through to the host.
83751df0accSAmit Shah */
83851df0accSAmit Shah nonblock = true;
839276a3e95SSjur Brændeland sg_init_one(sg, buf->buf, count);
840276a3e95SSjur Brændeland ret = __send_to_port(port, sg, 1, count, buf, nonblock);
84151df0accSAmit Shah
84251df0accSAmit Shah if (nonblock && ret > 0)
84351df0accSAmit Shah goto out;
84451df0accSAmit Shah
84551df0accSAmit Shah free_buf:
8461b637046SSjur Brændeland free_buf(buf, true);
84751df0accSAmit Shah out:
84851df0accSAmit Shah return ret;
84951df0accSAmit Shah }
85051df0accSAmit Shah
851eb5e89fcSMasami Hiramatsu struct sg_list {
852eb5e89fcSMasami Hiramatsu unsigned int n;
8538ca84a50SMasami Hiramatsu unsigned int size;
854eb5e89fcSMasami Hiramatsu size_t len;
855eb5e89fcSMasami Hiramatsu struct scatterlist *sg;
856eb5e89fcSMasami Hiramatsu };
857eb5e89fcSMasami Hiramatsu
pipe_to_sg(struct pipe_inode_info * pipe,struct pipe_buffer * buf,struct splice_desc * sd)858eb5e89fcSMasami Hiramatsu static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
859eb5e89fcSMasami Hiramatsu struct splice_desc *sd)
860eb5e89fcSMasami Hiramatsu {
861eb5e89fcSMasami Hiramatsu struct sg_list *sgl = sd->u.data;
862ec8fc870SMasami Hiramatsu unsigned int offset, len;
863eb5e89fcSMasami Hiramatsu
8648ca84a50SMasami Hiramatsu if (sgl->n == sgl->size)
865eb5e89fcSMasami Hiramatsu return 0;
866eb5e89fcSMasami Hiramatsu
867eb5e89fcSMasami Hiramatsu /* Try lock this page */
868c928f642SChristoph Hellwig if (pipe_buf_try_steal(pipe, buf)) {
869eb5e89fcSMasami Hiramatsu /* Get reference and unlock page for moving */
870eb5e89fcSMasami Hiramatsu get_page(buf->page);
871eb5e89fcSMasami Hiramatsu unlock_page(buf->page);
872eb5e89fcSMasami Hiramatsu
873eb5e89fcSMasami Hiramatsu len = min(buf->len, sd->len);
874eb5e89fcSMasami Hiramatsu sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
875ec8fc870SMasami Hiramatsu } else {
876ec8fc870SMasami Hiramatsu /* Failback to copying a page */
877ec8fc870SMasami Hiramatsu struct page *page = alloc_page(GFP_KERNEL);
878c9efe511SAl Viro char *src;
879ec8fc870SMasami Hiramatsu
880ec8fc870SMasami Hiramatsu if (!page)
881ec8fc870SMasami Hiramatsu return -ENOMEM;
882ec8fc870SMasami Hiramatsu
883ec8fc870SMasami Hiramatsu offset = sd->pos & ~PAGE_MASK;
884ec8fc870SMasami Hiramatsu
885ec8fc870SMasami Hiramatsu len = sd->len;
886ec8fc870SMasami Hiramatsu if (len + offset > PAGE_SIZE)
887ec8fc870SMasami Hiramatsu len = PAGE_SIZE - offset;
888ec8fc870SMasami Hiramatsu
889fbb32750SAl Viro src = kmap_atomic(buf->page);
890c9efe511SAl Viro memcpy(page_address(page) + offset, src + buf->offset, len);
891fbb32750SAl Viro kunmap_atomic(src);
892ec8fc870SMasami Hiramatsu
893ec8fc870SMasami Hiramatsu sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
894ec8fc870SMasami Hiramatsu }
895eb5e89fcSMasami Hiramatsu sgl->n++;
896eb5e89fcSMasami Hiramatsu sgl->len += len;
897eb5e89fcSMasami Hiramatsu
898eb5e89fcSMasami Hiramatsu return len;
899eb5e89fcSMasami Hiramatsu }
900eb5e89fcSMasami Hiramatsu
901eb5e89fcSMasami Hiramatsu /* Faster zero-copy write by splicing */
port_fops_splice_write(struct pipe_inode_info * pipe,struct file * filp,loff_t * ppos,size_t len,unsigned int flags)902eb5e89fcSMasami Hiramatsu static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
903eb5e89fcSMasami Hiramatsu struct file *filp, loff_t *ppos,
904eb5e89fcSMasami Hiramatsu size_t len, unsigned int flags)
905eb5e89fcSMasami Hiramatsu {
906eb5e89fcSMasami Hiramatsu struct port *port = filp->private_data;
907eb5e89fcSMasami Hiramatsu struct sg_list sgl;
908eb5e89fcSMasami Hiramatsu ssize_t ret;
909276a3e95SSjur Brændeland struct port_buffer *buf;
910eb5e89fcSMasami Hiramatsu struct splice_desc sd = {
911eb5e89fcSMasami Hiramatsu .total_len = len,
912eb5e89fcSMasami Hiramatsu .flags = flags,
913eb5e89fcSMasami Hiramatsu .pos = *ppos,
914eb5e89fcSMasami Hiramatsu .u.data = &sgl,
915eb5e89fcSMasami Hiramatsu };
9168cefc107SDavid Howells unsigned int occupancy;
917eb5e89fcSMasami Hiramatsu
9181b637046SSjur Brændeland /*
9191b637046SSjur Brændeland * Rproc_serial does not yet support splice. To support splice
9201b637046SSjur Brændeland * pipe_to_sg() must allocate dma-buffers and copy content from
9211b637046SSjur Brændeland * regular pages to dma pages. And alloc_buf and free_buf must
9221b637046SSjur Brændeland * support allocating and freeing such a list of dma-buffers.
9231b637046SSjur Brændeland */
9241b637046SSjur Brændeland if (is_rproc_serial(port->out_vq->vdev))
9251b637046SSjur Brændeland return -EINVAL;
9261b637046SSjur Brændeland
9272b4fbf02SYoshihiro YUNOMAE pipe_lock(pipe);
9282b4fbf02SYoshihiro YUNOMAE ret = 0;
9298cefc107SDavid Howells if (pipe_empty(pipe->head, pipe->tail))
9302b4fbf02SYoshihiro YUNOMAE goto error_out;
93168c034feSYoshihiro YUNOMAE
932efe75d24SMasami Hiramatsu ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
933efe75d24SMasami Hiramatsu if (ret < 0)
9342b4fbf02SYoshihiro YUNOMAE goto error_out;
935efe75d24SMasami Hiramatsu
9368cefc107SDavid Howells occupancy = pipe_occupancy(pipe->head, pipe->tail);
9378cefc107SDavid Howells buf = alloc_buf(port->portdev->vdev, 0, occupancy);
9388cefc107SDavid Howells
9392b4fbf02SYoshihiro YUNOMAE if (!buf) {
9402b4fbf02SYoshihiro YUNOMAE ret = -ENOMEM;
9412b4fbf02SYoshihiro YUNOMAE goto error_out;
9422b4fbf02SYoshihiro YUNOMAE }
943276a3e95SSjur Brændeland
944eb5e89fcSMasami Hiramatsu sgl.n = 0;
945eb5e89fcSMasami Hiramatsu sgl.len = 0;
9468cefc107SDavid Howells sgl.size = occupancy;
947276a3e95SSjur Brændeland sgl.sg = buf->sg;
9488ca84a50SMasami Hiramatsu sg_init_table(sgl.sg, sgl.size);
949eb5e89fcSMasami Hiramatsu ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
9502b4fbf02SYoshihiro YUNOMAE pipe_unlock(pipe);
951eb5e89fcSMasami Hiramatsu if (likely(ret > 0))
952276a3e95SSjur Brændeland ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
953eb5e89fcSMasami Hiramatsu
954fe529537SSjur Brændeland if (unlikely(ret <= 0))
9551b637046SSjur Brændeland free_buf(buf, true);
956eb5e89fcSMasami Hiramatsu return ret;
9572b4fbf02SYoshihiro YUNOMAE
9582b4fbf02SYoshihiro YUNOMAE error_out:
9592b4fbf02SYoshihiro YUNOMAE pipe_unlock(pipe);
9602b4fbf02SYoshihiro YUNOMAE return ret;
961eb5e89fcSMasami Hiramatsu }
962eb5e89fcSMasami Hiramatsu
port_fops_poll(struct file * filp,poll_table * wait)963afc9a42bSAl Viro static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
96451df0accSAmit Shah {
96551df0accSAmit Shah struct port *port;
966afc9a42bSAl Viro __poll_t ret;
96751df0accSAmit Shah
96851df0accSAmit Shah port = filp->private_data;
96951df0accSAmit Shah poll_wait(filp, &port->waitqueue, wait);
97051df0accSAmit Shah
97151df0accSAmit Shah if (!port->guest_connected) {
97251df0accSAmit Shah /* Port got unplugged */
973a9a08845SLinus Torvalds return EPOLLHUP;
97451df0accSAmit Shah }
97551df0accSAmit Shah ret = 0;
97651df0accSAmit Shah if (!will_read_block(port))
977a9a08845SLinus Torvalds ret |= EPOLLIN | EPOLLRDNORM;
97851df0accSAmit Shah if (!will_write_block(port))
979a9a08845SLinus Torvalds ret |= EPOLLOUT;
98051df0accSAmit Shah if (!port->host_connected)
981a9a08845SLinus Torvalds ret |= EPOLLHUP;
98251df0accSAmit Shah
98351df0accSAmit Shah return ret;
98451df0accSAmit Shah }
98551df0accSAmit Shah
98651df0accSAmit Shah static void remove_port(struct kref *kref);
98751df0accSAmit Shah
port_fops_release(struct inode * inode,struct file * filp)98851df0accSAmit Shah static int port_fops_release(struct inode *inode, struct file *filp)
98951df0accSAmit Shah {
99051df0accSAmit Shah struct port *port;
99151df0accSAmit Shah
99251df0accSAmit Shah port = filp->private_data;
99351df0accSAmit Shah
99451df0accSAmit Shah /* Notify host of port being closed */
99551df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
99651df0accSAmit Shah
99751df0accSAmit Shah spin_lock_irq(&port->inbuf_lock);
99851df0accSAmit Shah port->guest_connected = false;
99951df0accSAmit Shah
100051df0accSAmit Shah discard_port_data(port);
100151df0accSAmit Shah
100251df0accSAmit Shah spin_unlock_irq(&port->inbuf_lock);
100351df0accSAmit Shah
100451df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
100551df0accSAmit Shah reclaim_consumed_buffers(port);
100651df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
100751df0accSAmit Shah
10081b637046SSjur Brændeland reclaim_dma_bufs();
100951df0accSAmit Shah /*
101051df0accSAmit Shah * Locks aren't necessary here as a port can't be opened after
101151df0accSAmit Shah * unplug, and if a port isn't unplugged, a kref would already
101251df0accSAmit Shah * exist for the port. Plus, taking ports_lock here would
101351df0accSAmit Shah * create a dependency on other locks taken by functions
101451df0accSAmit Shah * inside remove_port if we're the last holder of the port,
101551df0accSAmit Shah * creating many problems.
101651df0accSAmit Shah */
101751df0accSAmit Shah kref_put(&port->kref, remove_port);
101851df0accSAmit Shah
101951df0accSAmit Shah return 0;
102051df0accSAmit Shah }
102151df0accSAmit Shah
port_fops_open(struct inode * inode,struct file * filp)102251df0accSAmit Shah static int port_fops_open(struct inode *inode, struct file *filp)
102351df0accSAmit Shah {
102451df0accSAmit Shah struct cdev *cdev = inode->i_cdev;
102551df0accSAmit Shah struct port *port;
102651df0accSAmit Shah int ret;
102751df0accSAmit Shah
1028057b82beSAmit Shah /* We get the port with a kref here */
102951df0accSAmit Shah port = find_port_by_devt(cdev->dev);
1030671bdea2SAmit Shah if (!port) {
1031671bdea2SAmit Shah /* Port was unplugged before we could proceed */
1032671bdea2SAmit Shah return -ENXIO;
1033671bdea2SAmit Shah }
103451df0accSAmit Shah filp->private_data = port;
103551df0accSAmit Shah
103651df0accSAmit Shah /*
103751df0accSAmit Shah * Don't allow opening of console port devices -- that's done
103851df0accSAmit Shah * via /dev/hvc
103951df0accSAmit Shah */
104051df0accSAmit Shah if (is_console_port(port)) {
104151df0accSAmit Shah ret = -ENXIO;
104251df0accSAmit Shah goto out;
104351df0accSAmit Shah }
104451df0accSAmit Shah
104551df0accSAmit Shah /* Allow only one process to open a particular port at a time */
104651df0accSAmit Shah spin_lock_irq(&port->inbuf_lock);
104751df0accSAmit Shah if (port->guest_connected) {
104851df0accSAmit Shah spin_unlock_irq(&port->inbuf_lock);
104974ff582cSAmit Shah ret = -EBUSY;
105051df0accSAmit Shah goto out;
105151df0accSAmit Shah }
105251df0accSAmit Shah
105351df0accSAmit Shah port->guest_connected = true;
105451df0accSAmit Shah spin_unlock_irq(&port->inbuf_lock);
105551df0accSAmit Shah
105651df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
105751df0accSAmit Shah /*
105851df0accSAmit Shah * There might be a chance that we missed reclaiming a few
105951df0accSAmit Shah * buffers in the window of the port getting previously closed
106051df0accSAmit Shah * and opening now.
106151df0accSAmit Shah */
106251df0accSAmit Shah reclaim_consumed_buffers(port);
106351df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
106451df0accSAmit Shah
106551df0accSAmit Shah nonseekable_open(inode, filp);
106651df0accSAmit Shah
106751df0accSAmit Shah /* Notify host of port being opened */
106851df0accSAmit Shah send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
106951df0accSAmit Shah
107051df0accSAmit Shah return 0;
107151df0accSAmit Shah out:
107251df0accSAmit Shah kref_put(&port->kref, remove_port);
107351df0accSAmit Shah return ret;
107451df0accSAmit Shah }
107551df0accSAmit Shah
port_fops_fasync(int fd,struct file * filp,int mode)107651df0accSAmit Shah static int port_fops_fasync(int fd, struct file *filp, int mode)
107751df0accSAmit Shah {
107851df0accSAmit Shah struct port *port;
107951df0accSAmit Shah
108051df0accSAmit Shah port = filp->private_data;
108151df0accSAmit Shah return fasync_helper(fd, filp, mode, &port->async_queue);
108251df0accSAmit Shah }
108351df0accSAmit Shah
108451df0accSAmit Shah /*
108551df0accSAmit Shah * The file operations that we support: programs in the guest can open
108651df0accSAmit Shah * a console device, read from it, write to it, poll for data and
108751df0accSAmit Shah * close it. The devices are at
108851df0accSAmit Shah * /dev/vport<device number>p<port number>
108951df0accSAmit Shah */
109051df0accSAmit Shah static const struct file_operations port_fops = {
109151df0accSAmit Shah .owner = THIS_MODULE,
109251df0accSAmit Shah .open = port_fops_open,
109351df0accSAmit Shah .read = port_fops_read,
109451df0accSAmit Shah .write = port_fops_write,
1095eb5e89fcSMasami Hiramatsu .splice_write = port_fops_splice_write,
109651df0accSAmit Shah .poll = port_fops_poll,
109751df0accSAmit Shah .release = port_fops_release,
109851df0accSAmit Shah .fasync = port_fops_fasync,
109951df0accSAmit Shah .llseek = no_llseek,
110051df0accSAmit Shah };
110151df0accSAmit Shah
110251df0accSAmit Shah /*
110351df0accSAmit Shah * The put_chars() callback is pretty straightforward.
110451df0accSAmit Shah *
110551df0accSAmit Shah * We turn the characters into a scatter-gather list, add it to the
110651df0accSAmit Shah * output queue and then kick the Host. Then we sit here waiting for
110751df0accSAmit Shah * it to finish: inefficient in theory, but in practice
1108ecda85e7SJuergen Gross * implementations will do it immediately.
110951df0accSAmit Shah */
put_chars(u32 vtermno,const char * buf,int count)111051df0accSAmit Shah static int put_chars(u32 vtermno, const char *buf, int count)
111151df0accSAmit Shah {
111251df0accSAmit Shah struct port *port;
1113276a3e95SSjur Brændeland struct scatterlist sg[1];
1114c4baad50SOmar Sandoval void *data;
1115c4baad50SOmar Sandoval int ret;
111651df0accSAmit Shah
111751df0accSAmit Shah if (unlikely(early_put_chars))
111851df0accSAmit Shah return early_put_chars(vtermno, buf, count);
111951df0accSAmit Shah
112051df0accSAmit Shah port = find_port_by_vtermno(vtermno);
112151df0accSAmit Shah if (!port)
112251df0accSAmit Shah return -EPIPE;
112351df0accSAmit Shah
1124c4baad50SOmar Sandoval data = kmemdup(buf, count, GFP_ATOMIC);
1125c4baad50SOmar Sandoval if (!data)
1126c4baad50SOmar Sandoval return -ENOMEM;
1127c4baad50SOmar Sandoval
1128c4baad50SOmar Sandoval sg_init_one(sg, data, count);
1129c4baad50SOmar Sandoval ret = __send_to_port(port, sg, 1, count, data, false);
1130c4baad50SOmar Sandoval kfree(data);
1131c4baad50SOmar Sandoval return ret;
113251df0accSAmit Shah }
113351df0accSAmit Shah
113451df0accSAmit Shah /*
113551df0accSAmit Shah * get_chars() is the callback from the hvc_console infrastructure
113651df0accSAmit Shah * when an interrupt is received.
113751df0accSAmit Shah *
113851df0accSAmit Shah * We call out to fill_readbuf that gets us the required data from the
113951df0accSAmit Shah * buffers that are queued up.
114051df0accSAmit Shah */
get_chars(u32 vtermno,char * buf,int count)114151df0accSAmit Shah static int get_chars(u32 vtermno, char *buf, int count)
114251df0accSAmit Shah {
114351df0accSAmit Shah struct port *port;
114451df0accSAmit Shah
114551df0accSAmit Shah /* If we've not set up the port yet, we have no input to give. */
114651df0accSAmit Shah if (unlikely(early_put_chars))
114751df0accSAmit Shah return 0;
114851df0accSAmit Shah
114951df0accSAmit Shah port = find_port_by_vtermno(vtermno);
115051df0accSAmit Shah if (!port)
115151df0accSAmit Shah return -EPIPE;
115251df0accSAmit Shah
115351df0accSAmit Shah /* If we don't have an input queue yet, we can't get input. */
115451df0accSAmit Shah BUG_ON(!port->in_vq);
115551df0accSAmit Shah
115648b36066SMichael S. Tsirkin return fill_readbuf(port, (__force char __user *)buf, count, false);
115751df0accSAmit Shah }
115851df0accSAmit Shah
resize_console(struct port * port)115951df0accSAmit Shah static void resize_console(struct port *port)
116051df0accSAmit Shah {
116151df0accSAmit Shah struct virtio_device *vdev;
116251df0accSAmit Shah
116351df0accSAmit Shah /* The port could have been hot-unplugged */
116451df0accSAmit Shah if (!port || !is_console_port(port))
116551df0accSAmit Shah return;
116651df0accSAmit Shah
116751df0accSAmit Shah vdev = port->portdev->vdev;
11681b637046SSjur Brændeland
11691b637046SSjur Brændeland /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
11701b637046SSjur Brændeland if (!is_rproc_serial(vdev) &&
11711b637046SSjur Brændeland virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
117251df0accSAmit Shah hvc_resize(port->cons.hvc, port->cons.ws);
117351df0accSAmit Shah }
117451df0accSAmit Shah
117551df0accSAmit Shah /* We set the configuration at this point, since we now have a tty */
notifier_add_vio(struct hvc_struct * hp,int data)117651df0accSAmit Shah static int notifier_add_vio(struct hvc_struct *hp, int data)
117751df0accSAmit Shah {
117851df0accSAmit Shah struct port *port;
117951df0accSAmit Shah
118051df0accSAmit Shah port = find_port_by_vtermno(hp->vtermno);
118151df0accSAmit Shah if (!port)
118251df0accSAmit Shah return -EINVAL;
118351df0accSAmit Shah
118451df0accSAmit Shah hp->irq_requested = 1;
118551df0accSAmit Shah resize_console(port);
118651df0accSAmit Shah
118751df0accSAmit Shah return 0;
118851df0accSAmit Shah }
118951df0accSAmit Shah
notifier_del_vio(struct hvc_struct * hp,int data)119051df0accSAmit Shah static void notifier_del_vio(struct hvc_struct *hp, int data)
119151df0accSAmit Shah {
119251df0accSAmit Shah hp->irq_requested = 0;
119351df0accSAmit Shah }
119451df0accSAmit Shah
119551df0accSAmit Shah /* The operations for console ports. */
119651df0accSAmit Shah static const struct hv_ops hv_ops = {
119751df0accSAmit Shah .get_chars = get_chars,
119851df0accSAmit Shah .put_chars = put_chars,
119951df0accSAmit Shah .notifier_add = notifier_add_vio,
120051df0accSAmit Shah .notifier_del = notifier_del_vio,
120151df0accSAmit Shah .notifier_hangup = notifier_del_vio,
120251df0accSAmit Shah };
120351df0accSAmit Shah
120451df0accSAmit Shah /*
120551df0accSAmit Shah * Console drivers are initialized very early so boot messages can go
120651df0accSAmit Shah * out, so we do things slightly differently from the generic virtio
120751df0accSAmit Shah * initialization of the net and block drivers.
120851df0accSAmit Shah *
120951df0accSAmit Shah * At this stage, the console is output-only. It's too early to set
121051df0accSAmit Shah * up a virtqueue, so we let the drivers do some boutique early-output
121151df0accSAmit Shah * thing.
121251df0accSAmit Shah */
virtio_cons_early_init(int (* put_chars)(u32,const char *,int))121351df0accSAmit Shah int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
121451df0accSAmit Shah {
121551df0accSAmit Shah early_put_chars = put_chars;
121651df0accSAmit Shah return hvc_instantiate(0, 0, &hv_ops);
121751df0accSAmit Shah }
121851df0accSAmit Shah
init_port_console(struct port * port)12193826835aSWei Yongjun static int init_port_console(struct port *port)
122051df0accSAmit Shah {
122151df0accSAmit Shah int ret;
122251df0accSAmit Shah
122351df0accSAmit Shah /*
122451df0accSAmit Shah * The Host's telling us this port is a console port. Hook it
122551df0accSAmit Shah * up with an hvc console.
122651df0accSAmit Shah *
122751df0accSAmit Shah * To set up and manage our virtual console, we call
122851df0accSAmit Shah * hvc_alloc().
122951df0accSAmit Shah *
123051df0accSAmit Shah * The first argument of hvc_alloc() is the virtual console
123151df0accSAmit Shah * number. The second argument is the parameter for the
123251df0accSAmit Shah * notification mechanism (like irq number). We currently
123351df0accSAmit Shah * leave this as zero, virtqueues have implicit notifications.
123451df0accSAmit Shah *
123551df0accSAmit Shah * The third argument is a "struct hv_ops" containing the
123651df0accSAmit Shah * put_chars() get_chars(), notifier_add() and notifier_del()
123751df0accSAmit Shah * pointers. The final argument is the output buffer size: we
123851df0accSAmit Shah * can do any size, so we put PAGE_SIZE here.
123951df0accSAmit Shah */
1240e6278a54SCédric Le Goater ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL);
1241e6278a54SCédric Le Goater if (ret < 0)
1242e6278a54SCédric Le Goater return ret;
124351df0accSAmit Shah
1244e6278a54SCédric Le Goater port->cons.vtermno = ret;
124551df0accSAmit Shah port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
124651df0accSAmit Shah if (IS_ERR(port->cons.hvc)) {
124751df0accSAmit Shah ret = PTR_ERR(port->cons.hvc);
124851df0accSAmit Shah dev_err(port->dev,
124951df0accSAmit Shah "error %d allocating hvc for port\n", ret);
125051df0accSAmit Shah port->cons.hvc = NULL;
1251e6278a54SCédric Le Goater ida_free(&vtermno_ida, port->cons.vtermno);
125251df0accSAmit Shah return ret;
125351df0accSAmit Shah }
125451df0accSAmit Shah spin_lock_irq(&pdrvdata_lock);
125551df0accSAmit Shah list_add_tail(&port->cons.list, &pdrvdata.consoles);
125651df0accSAmit Shah spin_unlock_irq(&pdrvdata_lock);
125751df0accSAmit Shah port->guest_connected = true;
125851df0accSAmit Shah
125951df0accSAmit Shah /*
126051df0accSAmit Shah * Start using the new console output if this is the first
126151df0accSAmit Shah * console to come up.
126251df0accSAmit Shah */
126351df0accSAmit Shah if (early_put_chars)
126451df0accSAmit Shah early_put_chars = NULL;
126551df0accSAmit Shah
126651df0accSAmit Shah /* Notify host of port being opened */
126751df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
126851df0accSAmit Shah
126951df0accSAmit Shah return 0;
127051df0accSAmit Shah }
127151df0accSAmit Shah
show_port_name(struct device * dev,struct device_attribute * attr,char * buffer)127251df0accSAmit Shah static ssize_t show_port_name(struct device *dev,
127351df0accSAmit Shah struct device_attribute *attr, char *buffer)
127451df0accSAmit Shah {
127551df0accSAmit Shah struct port *port;
127651df0accSAmit Shah
127751df0accSAmit Shah port = dev_get_drvdata(dev);
127851df0accSAmit Shah
127951df0accSAmit Shah return sprintf(buffer, "%s\n", port->name);
128051df0accSAmit Shah }
128151df0accSAmit Shah
128251df0accSAmit Shah static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
128351df0accSAmit Shah
128451df0accSAmit Shah static struct attribute *port_sysfs_entries[] = {
128551df0accSAmit Shah &dev_attr_name.attr,
128651df0accSAmit Shah NULL
128751df0accSAmit Shah };
128851df0accSAmit Shah
1289ac317e27SArvind Yadav static const struct attribute_group port_attribute_group = {
129051df0accSAmit Shah .name = NULL, /* put in device directory */
129151df0accSAmit Shah .attrs = port_sysfs_entries,
129251df0accSAmit Shah };
129351df0accSAmit Shah
port_debugfs_show(struct seq_file * s,void * data)1294ddfa728aSYangtao Li static int port_debugfs_show(struct seq_file *s, void *data)
129551df0accSAmit Shah {
12968d62fe94STuomas Tynkkynen struct port *port = s->private;
129751df0accSAmit Shah
12988d62fe94STuomas Tynkkynen seq_printf(s, "name: %s\n", port->name ? port->name : "");
12998d62fe94STuomas Tynkkynen seq_printf(s, "guest_connected: %d\n", port->guest_connected);
13008d62fe94STuomas Tynkkynen seq_printf(s, "host_connected: %d\n", port->host_connected);
13018d62fe94STuomas Tynkkynen seq_printf(s, "outvq_full: %d\n", port->outvq_full);
13028d62fe94STuomas Tynkkynen seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
13038d62fe94STuomas Tynkkynen seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
13048d62fe94STuomas Tynkkynen seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
13058d62fe94STuomas Tynkkynen seq_printf(s, "is_console: %s\n",
130651df0accSAmit Shah is_console_port(port) ? "yes" : "no");
13078d62fe94STuomas Tynkkynen seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
130851df0accSAmit Shah
13098d62fe94STuomas Tynkkynen return 0;
13108d62fe94STuomas Tynkkynen }
13118d62fe94STuomas Tynkkynen
1312ddfa728aSYangtao Li DEFINE_SHOW_ATTRIBUTE(port_debugfs);
131351df0accSAmit Shah
set_console_size(struct port * port,u16 rows,u16 cols)131451df0accSAmit Shah static void set_console_size(struct port *port, u16 rows, u16 cols)
131551df0accSAmit Shah {
131651df0accSAmit Shah if (!port || !is_console_port(port))
131751df0accSAmit Shah return;
131851df0accSAmit Shah
131951df0accSAmit Shah port->cons.ws.ws_row = rows;
132051df0accSAmit Shah port->cons.ws.ws_col = cols;
132151df0accSAmit Shah }
132251df0accSAmit Shah
fill_queue(struct virtqueue * vq,spinlock_t * lock)1323d791cfcbSLaurent Vivier static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
132451df0accSAmit Shah {
132551df0accSAmit Shah struct port_buffer *buf;
1326d791cfcbSLaurent Vivier int nr_added_bufs;
132751df0accSAmit Shah int ret;
132851df0accSAmit Shah
132951df0accSAmit Shah nr_added_bufs = 0;
133051df0accSAmit Shah do {
13312855b335SMichael S. Tsirkin buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
133251df0accSAmit Shah if (!buf)
1333d791cfcbSLaurent Vivier return -ENOMEM;
133451df0accSAmit Shah
133551df0accSAmit Shah spin_lock_irq(lock);
133651df0accSAmit Shah ret = add_inbuf(vq, buf);
133751df0accSAmit Shah if (ret < 0) {
133851df0accSAmit Shah spin_unlock_irq(lock);
13391b637046SSjur Brændeland free_buf(buf, true);
1340d791cfcbSLaurent Vivier return ret;
134151df0accSAmit Shah }
134251df0accSAmit Shah nr_added_bufs++;
134351df0accSAmit Shah spin_unlock_irq(lock);
134451df0accSAmit Shah } while (ret > 0);
134551df0accSAmit Shah
134651df0accSAmit Shah return nr_added_bufs;
134751df0accSAmit Shah }
134851df0accSAmit Shah
send_sigio_to_port(struct port * port)134951df0accSAmit Shah static void send_sigio_to_port(struct port *port)
135051df0accSAmit Shah {
135151df0accSAmit Shah if (port->async_queue && port->guest_connected)
135251df0accSAmit Shah kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
135351df0accSAmit Shah }
135451df0accSAmit Shah
add_port(struct ports_device * portdev,u32 id)135551df0accSAmit Shah static int add_port(struct ports_device *portdev, u32 id)
135651df0accSAmit Shah {
135751df0accSAmit Shah char debugfs_name[16];
135851df0accSAmit Shah struct port *port;
135951df0accSAmit Shah dev_t devt;
136051df0accSAmit Shah int err;
136151df0accSAmit Shah
136251df0accSAmit Shah port = kmalloc(sizeof(*port), GFP_KERNEL);
136351df0accSAmit Shah if (!port) {
136451df0accSAmit Shah err = -ENOMEM;
136551df0accSAmit Shah goto fail;
136651df0accSAmit Shah }
136751df0accSAmit Shah kref_init(&port->kref);
136851df0accSAmit Shah
136951df0accSAmit Shah port->portdev = portdev;
137051df0accSAmit Shah port->id = id;
137151df0accSAmit Shah
137251df0accSAmit Shah port->name = NULL;
137351df0accSAmit Shah port->inbuf = NULL;
137451df0accSAmit Shah port->cons.hvc = NULL;
137551df0accSAmit Shah port->async_queue = NULL;
137651df0accSAmit Shah
137751df0accSAmit Shah port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
13784b0a2c5fSPankaj Gupta port->cons.vtermno = 0;
137951df0accSAmit Shah
138051df0accSAmit Shah port->host_connected = port->guest_connected = false;
138117e5b4f2SAmit Shah port->stats = (struct port_stats) { 0 };
138251df0accSAmit Shah
138351df0accSAmit Shah port->outvq_full = false;
138451df0accSAmit Shah
138551df0accSAmit Shah port->in_vq = portdev->in_vqs[port->id];
138651df0accSAmit Shah port->out_vq = portdev->out_vqs[port->id];
138751df0accSAmit Shah
138851df0accSAmit Shah port->cdev = cdev_alloc();
138951df0accSAmit Shah if (!port->cdev) {
139051df0accSAmit Shah dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
139151df0accSAmit Shah err = -ENOMEM;
139251df0accSAmit Shah goto free_port;
139351df0accSAmit Shah }
139451df0accSAmit Shah port->cdev->ops = &port_fops;
139551df0accSAmit Shah
139651df0accSAmit Shah devt = MKDEV(portdev->chr_major, id);
139751df0accSAmit Shah err = cdev_add(port->cdev, devt, 1);
139851df0accSAmit Shah if (err < 0) {
139951df0accSAmit Shah dev_err(&port->portdev->vdev->dev,
140051df0accSAmit Shah "Error %d adding cdev for port %u\n", err, id);
140151df0accSAmit Shah goto free_cdev;
140251df0accSAmit Shah }
140311680fdfSIvan Orlov port->dev = device_create(&port_class, &port->portdev->vdev->dev,
140451df0accSAmit Shah devt, port, "vport%up%u",
1405dc18f080SSjur Brændeland port->portdev->vdev->index, id);
140651df0accSAmit Shah if (IS_ERR(port->dev)) {
140751df0accSAmit Shah err = PTR_ERR(port->dev);
140851df0accSAmit Shah dev_err(&port->portdev->vdev->dev,
140951df0accSAmit Shah "Error %d creating device for port %u\n",
141051df0accSAmit Shah err, id);
141151df0accSAmit Shah goto free_cdev;
141251df0accSAmit Shah }
141351df0accSAmit Shah
141451df0accSAmit Shah spin_lock_init(&port->inbuf_lock);
141551df0accSAmit Shah spin_lock_init(&port->outvq_lock);
141651df0accSAmit Shah init_waitqueue_head(&port->waitqueue);
141751df0accSAmit Shah
1418d791cfcbSLaurent Vivier /* We can safely ignore ENOSPC because it means
1419d791cfcbSLaurent Vivier * the queue already has buffers. Buffers are removed
1420d791cfcbSLaurent Vivier * only by virtcons_remove(), not by unplug_port()
1421d791cfcbSLaurent Vivier */
1422d791cfcbSLaurent Vivier err = fill_queue(port->in_vq, &port->inbuf_lock);
1423d791cfcbSLaurent Vivier if (err < 0 && err != -ENOSPC) {
142451df0accSAmit Shah dev_err(port->dev, "Error allocating inbufs\n");
142551df0accSAmit Shah goto free_device;
142651df0accSAmit Shah }
142751df0accSAmit Shah
14281b637046SSjur Brændeland if (is_rproc_serial(port->portdev->vdev))
142951df0accSAmit Shah /*
14301b637046SSjur Brændeland * For rproc_serial assume remote processor is connected.
14311b637046SSjur Brændeland * rproc_serial does not want the console port, only
14321b637046SSjur Brændeland * the generic port implementation.
143351df0accSAmit Shah */
1434aabd6a8fSSjur Brændeland port->host_connected = true;
14351b637046SSjur Brændeland else if (!use_multiport(port->portdev)) {
14361b637046SSjur Brændeland /*
14371b637046SSjur Brændeland * If we're not using multiport support,
14381b637046SSjur Brændeland * this has to be a console port.
14391b637046SSjur Brændeland */
144051df0accSAmit Shah err = init_port_console(port);
144151df0accSAmit Shah if (err)
144251df0accSAmit Shah goto free_inbufs;
144351df0accSAmit Shah }
144451df0accSAmit Shah
144551df0accSAmit Shah spin_lock_irq(&portdev->ports_lock);
144651df0accSAmit Shah list_add_tail(&port->list, &port->portdev->ports);
144751df0accSAmit Shah spin_unlock_irq(&portdev->ports_lock);
144851df0accSAmit Shah
144951df0accSAmit Shah /*
145051df0accSAmit Shah * Tell the Host we're set so that it can send us various
145151df0accSAmit Shah * configuration parameters for this port (eg, port name,
145251df0accSAmit Shah * caching, whether this is a console port, etc.)
145351df0accSAmit Shah */
145451df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
145551df0accSAmit Shah
145651df0accSAmit Shah /*
145751df0accSAmit Shah * Finally, create the debugfs file that we can use to
145851df0accSAmit Shah * inspect a port's state at any time
145951df0accSAmit Shah */
1460db170068SDan Carpenter snprintf(debugfs_name, sizeof(debugfs_name), "vport%up%u",
1461dc18f080SSjur Brændeland port->portdev->vdev->index, id);
146251df0accSAmit Shah port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
146351df0accSAmit Shah pdrvdata.debugfs_dir,
1464fb11de92SGreg Kroah-Hartman port, &port_debugfs_fops);
146551df0accSAmit Shah return 0;
146651df0accSAmit Shah
146751df0accSAmit Shah free_inbufs:
146851df0accSAmit Shah free_device:
146911680fdfSIvan Orlov device_destroy(&port_class, port->dev->devt);
147051df0accSAmit Shah free_cdev:
147151df0accSAmit Shah cdev_del(port->cdev);
147251df0accSAmit Shah free_port:
147351df0accSAmit Shah kfree(port);
147451df0accSAmit Shah fail:
147551df0accSAmit Shah /* The host might want to notify management sw about port add failure */
147651df0accSAmit Shah __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
147751df0accSAmit Shah return err;
147851df0accSAmit Shah }
147951df0accSAmit Shah
148051df0accSAmit Shah /* No users remain, remove all port-specific data. */
remove_port(struct kref * kref)148151df0accSAmit Shah static void remove_port(struct kref *kref)
148251df0accSAmit Shah {
148351df0accSAmit Shah struct port *port;
148451df0accSAmit Shah
148551df0accSAmit Shah port = container_of(kref, struct port, kref);
148651df0accSAmit Shah
148751df0accSAmit Shah kfree(port);
148851df0accSAmit Shah }
148951df0accSAmit Shah
remove_port_data(struct port * port)1490a0e2dbfcSAmit Shah static void remove_port_data(struct port *port)
1491a0e2dbfcSAmit Shah {
1492c6017e79SAmit Shah spin_lock_irq(&port->inbuf_lock);
1493a0e2dbfcSAmit Shah /* Remove unused data this port might have received. */
1494a0e2dbfcSAmit Shah discard_port_data(port);
149534563769SMatt Redfearn spin_unlock_irq(&port->inbuf_lock);
1496a0e2dbfcSAmit Shah
1497c6017e79SAmit Shah spin_lock_irq(&port->outvq_lock);
1498c6017e79SAmit Shah reclaim_consumed_buffers(port);
149934563769SMatt Redfearn spin_unlock_irq(&port->outvq_lock);
1500a0e2dbfcSAmit Shah }
1501a0e2dbfcSAmit Shah
150251df0accSAmit Shah /*
150351df0accSAmit Shah * Port got unplugged. Remove port from portdev's list and drop the
150451df0accSAmit Shah * kref reference. If no userspace has this port opened, it will
150551df0accSAmit Shah * result in immediate removal the port.
150651df0accSAmit Shah */
unplug_port(struct port * port)150751df0accSAmit Shah static void unplug_port(struct port *port)
150851df0accSAmit Shah {
150951df0accSAmit Shah spin_lock_irq(&port->portdev->ports_lock);
151051df0accSAmit Shah list_del(&port->list);
151151df0accSAmit Shah spin_unlock_irq(&port->portdev->ports_lock);
151251df0accSAmit Shah
15135549fb25SAmit Shah spin_lock_irq(&port->inbuf_lock);
151451df0accSAmit Shah if (port->guest_connected) {
151551df0accSAmit Shah /* Let the app know the port is going down. */
151651df0accSAmit Shah send_sigio_to_port(port);
151792d34538SAmit Shah
151892d34538SAmit Shah /* Do this after sigio is actually sent */
151992d34538SAmit Shah port->guest_connected = false;
152092d34538SAmit Shah port->host_connected = false;
152192d34538SAmit Shah
152292d34538SAmit Shah wake_up_interruptible(&port->waitqueue);
152351df0accSAmit Shah }
15245549fb25SAmit Shah spin_unlock_irq(&port->inbuf_lock);
152551df0accSAmit Shah
152651df0accSAmit Shah if (is_console_port(port)) {
152751df0accSAmit Shah spin_lock_irq(&pdrvdata_lock);
152851df0accSAmit Shah list_del(&port->cons.list);
152951df0accSAmit Shah spin_unlock_irq(&pdrvdata_lock);
153051df0accSAmit Shah hvc_remove(port->cons.hvc);
1531e6278a54SCédric Le Goater ida_free(&vtermno_ida, port->cons.vtermno);
153251df0accSAmit Shah }
153351df0accSAmit Shah
1534a0e2dbfcSAmit Shah remove_port_data(port);
153551df0accSAmit Shah
153651df0accSAmit Shah /*
153751df0accSAmit Shah * We should just assume the device itself has gone off --
153851df0accSAmit Shah * else a close on an open port later will try to send out a
153951df0accSAmit Shah * control message.
154051df0accSAmit Shah */
154151df0accSAmit Shah port->portdev = NULL;
154251df0accSAmit Shah
1543ea3768b4SAmit Shah sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
154411680fdfSIvan Orlov device_destroy(&port_class, port->dev->devt);
1545ea3768b4SAmit Shah cdev_del(port->cdev);
1546ea3768b4SAmit Shah
1547ea3768b4SAmit Shah debugfs_remove(port->debugfs_file);
15483b868a40SAmit Shah kfree(port->name);
1549ea3768b4SAmit Shah
155051df0accSAmit Shah /*
155151df0accSAmit Shah * Locks around here are not necessary - a port can't be
155251df0accSAmit Shah * opened after we removed the port struct from ports_list
155351df0accSAmit Shah * above.
155451df0accSAmit Shah */
155551df0accSAmit Shah kref_put(&port->kref, remove_port);
155651df0accSAmit Shah }
155751df0accSAmit Shah
155851df0accSAmit Shah /* Any private messages that the Host and Guest want to share */
handle_control_message(struct virtio_device * vdev,struct ports_device * portdev,struct port_buffer * buf)15591f0f9106SMichael S. Tsirkin static void handle_control_message(struct virtio_device *vdev,
15601f0f9106SMichael S. Tsirkin struct ports_device *portdev,
156151df0accSAmit Shah struct port_buffer *buf)
156251df0accSAmit Shah {
156351df0accSAmit Shah struct virtio_console_control *cpkt;
156451df0accSAmit Shah struct port *port;
156551df0accSAmit Shah size_t name_size;
156651df0accSAmit Shah int err;
156751df0accSAmit Shah
156851df0accSAmit Shah cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
156951df0accSAmit Shah
15701f0f9106SMichael S. Tsirkin port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
15711f0f9106SMichael S. Tsirkin if (!port &&
15721f0f9106SMichael S. Tsirkin cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
157351df0accSAmit Shah /* No valid header at start of buffer. Drop it. */
157451df0accSAmit Shah dev_dbg(&portdev->vdev->dev,
157551df0accSAmit Shah "Invalid index %u in control packet\n", cpkt->id);
157651df0accSAmit Shah return;
157751df0accSAmit Shah }
157851df0accSAmit Shah
15791f0f9106SMichael S. Tsirkin switch (virtio16_to_cpu(vdev, cpkt->event)) {
158051df0accSAmit Shah case VIRTIO_CONSOLE_PORT_ADD:
158151df0accSAmit Shah if (port) {
158251df0accSAmit Shah dev_dbg(&portdev->vdev->dev,
158351df0accSAmit Shah "Port %u already added\n", port->id);
158451df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
158551df0accSAmit Shah break;
158651df0accSAmit Shah }
15871f0f9106SMichael S. Tsirkin if (virtio32_to_cpu(vdev, cpkt->id) >=
15887328fa64SMichael S. Tsirkin portdev->max_nr_ports) {
158951df0accSAmit Shah dev_warn(&portdev->vdev->dev,
15901f0f9106SMichael S. Tsirkin "Request for adding port with "
15911f0f9106SMichael S. Tsirkin "out-of-bound id %u, max. supported id: %u\n",
15927328fa64SMichael S. Tsirkin cpkt->id, portdev->max_nr_ports - 1);
159351df0accSAmit Shah break;
159451df0accSAmit Shah }
15951f0f9106SMichael S. Tsirkin add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
159651df0accSAmit Shah break;
159751df0accSAmit Shah case VIRTIO_CONSOLE_PORT_REMOVE:
159851df0accSAmit Shah unplug_port(port);
159951df0accSAmit Shah break;
160051df0accSAmit Shah case VIRTIO_CONSOLE_CONSOLE_PORT:
160151df0accSAmit Shah if (!cpkt->value)
160251df0accSAmit Shah break;
160351df0accSAmit Shah if (is_console_port(port))
160451df0accSAmit Shah break;
160551df0accSAmit Shah
160651df0accSAmit Shah init_port_console(port);
16075e38483bSChristian Borntraeger complete(&early_console_added);
160851df0accSAmit Shah /*
160951df0accSAmit Shah * Could remove the port here in case init fails - but
161051df0accSAmit Shah * have to notify the host first.
161151df0accSAmit Shah */
161251df0accSAmit Shah break;
161351df0accSAmit Shah case VIRTIO_CONSOLE_RESIZE: {
161451df0accSAmit Shah struct {
161551df0accSAmit Shah __u16 rows;
161651df0accSAmit Shah __u16 cols;
161751df0accSAmit Shah } size;
161851df0accSAmit Shah
161951df0accSAmit Shah if (!is_console_port(port))
162051df0accSAmit Shah break;
162151df0accSAmit Shah
162251df0accSAmit Shah memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
162351df0accSAmit Shah sizeof(size));
162451df0accSAmit Shah set_console_size(port, size.rows, size.cols);
162551df0accSAmit Shah
162651df0accSAmit Shah port->cons.hvc->irq_requested = 1;
162751df0accSAmit Shah resize_console(port);
162851df0accSAmit Shah break;
162951df0accSAmit Shah }
163051df0accSAmit Shah case VIRTIO_CONSOLE_PORT_OPEN:
16311f0f9106SMichael S. Tsirkin port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
163251df0accSAmit Shah wake_up_interruptible(&port->waitqueue);
163351df0accSAmit Shah /*
163451df0accSAmit Shah * If the host port got closed and the host had any
163551df0accSAmit Shah * unconsumed buffers, we'll be able to reclaim them
163651df0accSAmit Shah * now.
163751df0accSAmit Shah */
163851df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
163951df0accSAmit Shah reclaim_consumed_buffers(port);
164051df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
164151df0accSAmit Shah
164251df0accSAmit Shah /*
164351df0accSAmit Shah * If the guest is connected, it'll be interested in
164451df0accSAmit Shah * knowing the host connection state changed.
164551df0accSAmit Shah */
1646314081f1SAmit Shah spin_lock_irq(&port->inbuf_lock);
164751df0accSAmit Shah send_sigio_to_port(port);
1648314081f1SAmit Shah spin_unlock_irq(&port->inbuf_lock);
164951df0accSAmit Shah break;
165051df0accSAmit Shah case VIRTIO_CONSOLE_PORT_NAME:
165151df0accSAmit Shah /*
1652291024efSAmit Shah * If we woke up after hibernation, we can get this
1653291024efSAmit Shah * again. Skip it in that case.
1654291024efSAmit Shah */
1655291024efSAmit Shah if (port->name)
1656291024efSAmit Shah break;
1657291024efSAmit Shah
1658291024efSAmit Shah /*
165951df0accSAmit Shah * Skip the size of the header and the cpkt to get the size
166051df0accSAmit Shah * of the name that was sent
166151df0accSAmit Shah */
166251df0accSAmit Shah name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
166351df0accSAmit Shah
166451df0accSAmit Shah port->name = kmalloc(name_size, GFP_KERNEL);
166551df0accSAmit Shah if (!port->name) {
166651df0accSAmit Shah dev_err(port->dev,
166751df0accSAmit Shah "Not enough space to store port name\n");
166851df0accSAmit Shah break;
166951df0accSAmit Shah }
1670c7109c72SBo Liu strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1671c7109c72SBo Liu name_size);
167251df0accSAmit Shah
167351df0accSAmit Shah /*
167451df0accSAmit Shah * Since we only have one sysfs attribute, 'name',
167551df0accSAmit Shah * create it only if we have a name for the port.
167651df0accSAmit Shah */
167751df0accSAmit Shah err = sysfs_create_group(&port->dev->kobj,
167851df0accSAmit Shah &port_attribute_group);
167951df0accSAmit Shah if (err) {
168051df0accSAmit Shah dev_err(port->dev,
168151df0accSAmit Shah "Error %d creating sysfs device attributes\n",
168251df0accSAmit Shah err);
168351df0accSAmit Shah } else {
168451df0accSAmit Shah /*
168551df0accSAmit Shah * Generate a udev event so that appropriate
168651df0accSAmit Shah * symlinks can be created based on udev
168751df0accSAmit Shah * rules.
168851df0accSAmit Shah */
168951df0accSAmit Shah kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
169051df0accSAmit Shah }
169151df0accSAmit Shah break;
169251df0accSAmit Shah }
169351df0accSAmit Shah }
169451df0accSAmit Shah
control_work_handler(struct work_struct * work)169551df0accSAmit Shah static void control_work_handler(struct work_struct *work)
169651df0accSAmit Shah {
169751df0accSAmit Shah struct ports_device *portdev;
169851df0accSAmit Shah struct virtqueue *vq;
169951df0accSAmit Shah struct port_buffer *buf;
170051df0accSAmit Shah unsigned int len;
170151df0accSAmit Shah
170251df0accSAmit Shah portdev = container_of(work, struct ports_device, control_work);
170351df0accSAmit Shah vq = portdev->c_ivq;
170451df0accSAmit Shah
1705165b1b8bSAmit Shah spin_lock(&portdev->c_ivq_lock);
170651df0accSAmit Shah while ((buf = virtqueue_get_buf(vq, &len))) {
1707165b1b8bSAmit Shah spin_unlock(&portdev->c_ivq_lock);
170851df0accSAmit Shah
1709d00d8da5SXie Yongji buf->len = min_t(size_t, len, buf->size);
171051df0accSAmit Shah buf->offset = 0;
171151df0accSAmit Shah
17121f0f9106SMichael S. Tsirkin handle_control_message(vq->vdev, portdev, buf);
171351df0accSAmit Shah
1714165b1b8bSAmit Shah spin_lock(&portdev->c_ivq_lock);
171551df0accSAmit Shah if (add_inbuf(portdev->c_ivq, buf) < 0) {
171651df0accSAmit Shah dev_warn(&portdev->vdev->dev,
171751df0accSAmit Shah "Error adding buffer to queue\n");
17181b637046SSjur Brændeland free_buf(buf, false);
171951df0accSAmit Shah }
172051df0accSAmit Shah }
1721165b1b8bSAmit Shah spin_unlock(&portdev->c_ivq_lock);
172251df0accSAmit Shah }
172351df0accSAmit Shah
flush_bufs(struct virtqueue * vq,bool can_sleep)1724a7a69ec0SMichael S. Tsirkin static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1725a7a69ec0SMichael S. Tsirkin {
1726a7a69ec0SMichael S. Tsirkin struct port_buffer *buf;
1727a7a69ec0SMichael S. Tsirkin unsigned int len;
1728a7a69ec0SMichael S. Tsirkin
1729a7a69ec0SMichael S. Tsirkin while ((buf = virtqueue_get_buf(vq, &len)))
1730a7a69ec0SMichael S. Tsirkin free_buf(buf, can_sleep);
1731a7a69ec0SMichael S. Tsirkin }
1732a7a69ec0SMichael S. Tsirkin
out_intr(struct virtqueue * vq)1733ce86d35dSLinus Torvalds static void out_intr(struct virtqueue *vq)
1734ce86d35dSLinus Torvalds {
1735ce86d35dSLinus Torvalds struct port *port;
1736ce86d35dSLinus Torvalds
1737ce86d35dSLinus Torvalds port = find_port_by_vq(vq->vdev->priv, vq);
1738a7a69ec0SMichael S. Tsirkin if (!port) {
1739a7a69ec0SMichael S. Tsirkin flush_bufs(vq, false);
1740ce86d35dSLinus Torvalds return;
1741a7a69ec0SMichael S. Tsirkin }
1742ce86d35dSLinus Torvalds
1743ce86d35dSLinus Torvalds wake_up_interruptible(&port->waitqueue);
1744ce86d35dSLinus Torvalds }
1745ce86d35dSLinus Torvalds
in_intr(struct virtqueue * vq)174651df0accSAmit Shah static void in_intr(struct virtqueue *vq)
174751df0accSAmit Shah {
174851df0accSAmit Shah struct port *port;
174951df0accSAmit Shah unsigned long flags;
175051df0accSAmit Shah
175151df0accSAmit Shah port = find_port_by_vq(vq->vdev->priv, vq);
1752a7a69ec0SMichael S. Tsirkin if (!port) {
1753a7a69ec0SMichael S. Tsirkin flush_bufs(vq, false);
175451df0accSAmit Shah return;
1755a7a69ec0SMichael S. Tsirkin }
175651df0accSAmit Shah
175751df0accSAmit Shah spin_lock_irqsave(&port->inbuf_lock, flags);
175851df0accSAmit Shah port->inbuf = get_inbuf(port);
175951df0accSAmit Shah
176051df0accSAmit Shah /*
1761aabd6a8fSSjur Brændeland * Normally the port should not accept data when the port is
1762aabd6a8fSSjur Brændeland * closed. For generic serial ports, the host won't (shouldn't)
1763aabd6a8fSSjur Brændeland * send data till the guest is connected. But this condition
176451df0accSAmit Shah * can be reached when a console port is not yet connected (no
1765aabd6a8fSSjur Brændeland * tty is spawned) and the other side sends out data over the
1766aabd6a8fSSjur Brændeland * vring, or when a remote devices start sending data before
1767aabd6a8fSSjur Brændeland * the ports are opened.
1768aabd6a8fSSjur Brændeland *
1769aabd6a8fSSjur Brændeland * A generic serial port will discard data if not connected,
1770aabd6a8fSSjur Brændeland * while console ports and rproc-serial ports accepts data at
1771aabd6a8fSSjur Brændeland * any time. rproc-serial is initiated with guest_connected to
1772aabd6a8fSSjur Brændeland * false because port_fops_open expects this. Console ports are
1773aabd6a8fSSjur Brændeland * hooked up with an HVC console and is initialized with
1774aabd6a8fSSjur Brændeland * guest_connected to true.
177551df0accSAmit Shah */
1776aabd6a8fSSjur Brændeland
1777aabd6a8fSSjur Brændeland if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
177851df0accSAmit Shah discard_port_data(port);
177951df0accSAmit Shah
1780314081f1SAmit Shah /* Send a SIGIO indicating new data in case the process asked for it */
1781314081f1SAmit Shah send_sigio_to_port(port);
1782314081f1SAmit Shah
178351df0accSAmit Shah spin_unlock_irqrestore(&port->inbuf_lock, flags);
178451df0accSAmit Shah
178551df0accSAmit Shah wake_up_interruptible(&port->waitqueue);
178651df0accSAmit Shah
178751df0accSAmit Shah if (is_console_port(port) && hvc_poll(port->cons.hvc))
178851df0accSAmit Shah hvc_kick();
178951df0accSAmit Shah }
179051df0accSAmit Shah
control_intr(struct virtqueue * vq)179151df0accSAmit Shah static void control_intr(struct virtqueue *vq)
179251df0accSAmit Shah {
179351df0accSAmit Shah struct ports_device *portdev;
179451df0accSAmit Shah
179551df0accSAmit Shah portdev = vq->vdev->priv;
179651df0accSAmit Shah schedule_work(&portdev->control_work);
179751df0accSAmit Shah }
179851df0accSAmit Shah
config_intr(struct virtio_device * vdev)179951df0accSAmit Shah static void config_intr(struct virtio_device *vdev)
180051df0accSAmit Shah {
180151df0accSAmit Shah struct ports_device *portdev;
180251df0accSAmit Shah
180351df0accSAmit Shah portdev = vdev->priv;
180451df0accSAmit Shah
1805eeb8a7e8SMichael S. Tsirkin if (!use_multiport(portdev))
1806eeb8a7e8SMichael S. Tsirkin schedule_work(&portdev->config_work);
1807eeb8a7e8SMichael S. Tsirkin }
1808eeb8a7e8SMichael S. Tsirkin
config_work_handler(struct work_struct * work)1809eeb8a7e8SMichael S. Tsirkin static void config_work_handler(struct work_struct *work)
1810eeb8a7e8SMichael S. Tsirkin {
1811eeb8a7e8SMichael S. Tsirkin struct ports_device *portdev;
1812eeb8a7e8SMichael S. Tsirkin
18138379cadfSG. Campana portdev = container_of(work, struct ports_device, config_work);
181451df0accSAmit Shah if (!use_multiport(portdev)) {
1815eeb8a7e8SMichael S. Tsirkin struct virtio_device *vdev;
181651df0accSAmit Shah struct port *port;
181751df0accSAmit Shah u16 rows, cols;
181851df0accSAmit Shah
1819eeb8a7e8SMichael S. Tsirkin vdev = portdev->vdev;
1820855e0c52SRusty Russell virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1821855e0c52SRusty Russell virtio_cread(vdev, struct virtio_console_config, rows, &rows);
182251df0accSAmit Shah
182351df0accSAmit Shah port = find_port_by_id(portdev, 0);
182451df0accSAmit Shah set_console_size(port, rows, cols);
182551df0accSAmit Shah
182651df0accSAmit Shah /*
182751df0accSAmit Shah * We'll use this way of resizing only for legacy
182851df0accSAmit Shah * support. For newer userspace
182951df0accSAmit Shah * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
183051df0accSAmit Shah * to indicate console size changes so that it can be
183151df0accSAmit Shah * done per-port.
183251df0accSAmit Shah */
183351df0accSAmit Shah resize_console(port);
183451df0accSAmit Shah }
183551df0accSAmit Shah }
183651df0accSAmit Shah
init_vqs(struct ports_device * portdev)183751df0accSAmit Shah static int init_vqs(struct ports_device *portdev)
183851df0accSAmit Shah {
183951df0accSAmit Shah vq_callback_t **io_callbacks;
184051df0accSAmit Shah char **io_names;
184151df0accSAmit Shah struct virtqueue **vqs;
184251df0accSAmit Shah u32 i, j, nr_ports, nr_queues;
184351df0accSAmit Shah int err;
184451df0accSAmit Shah
18457328fa64SMichael S. Tsirkin nr_ports = portdev->max_nr_ports;
184651df0accSAmit Shah nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
184751df0accSAmit Shah
18486da2ec56SKees Cook vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
18496da2ec56SKees Cook io_callbacks = kmalloc_array(nr_queues, sizeof(vq_callback_t *),
185051df0accSAmit Shah GFP_KERNEL);
18516da2ec56SKees Cook io_names = kmalloc_array(nr_queues, sizeof(char *), GFP_KERNEL);
18526da2ec56SKees Cook portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
18536da2ec56SKees Cook GFP_KERNEL);
18546da2ec56SKees Cook portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
185551df0accSAmit Shah GFP_KERNEL);
185651df0accSAmit Shah if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
185751df0accSAmit Shah !portdev->out_vqs) {
185851df0accSAmit Shah err = -ENOMEM;
185951df0accSAmit Shah goto free;
186051df0accSAmit Shah }
186151df0accSAmit Shah
186251df0accSAmit Shah /*
186351df0accSAmit Shah * For backward compat (newer host but older guest), the host
186451df0accSAmit Shah * spawns a console port first and also inits the vqs for port
186551df0accSAmit Shah * 0 before others.
186651df0accSAmit Shah */
186751df0accSAmit Shah j = 0;
186851df0accSAmit Shah io_callbacks[j] = in_intr;
1869ce86d35dSLinus Torvalds io_callbacks[j + 1] = out_intr;
187051df0accSAmit Shah io_names[j] = "input";
187151df0accSAmit Shah io_names[j + 1] = "output";
187251df0accSAmit Shah j += 2;
187351df0accSAmit Shah
187451df0accSAmit Shah if (use_multiport(portdev)) {
187551df0accSAmit Shah io_callbacks[j] = control_intr;
187651df0accSAmit Shah io_callbacks[j + 1] = NULL;
187751df0accSAmit Shah io_names[j] = "control-i";
187851df0accSAmit Shah io_names[j + 1] = "control-o";
187951df0accSAmit Shah
188051df0accSAmit Shah for (i = 1; i < nr_ports; i++) {
188151df0accSAmit Shah j += 2;
188251df0accSAmit Shah io_callbacks[j] = in_intr;
1883ce86d35dSLinus Torvalds io_callbacks[j + 1] = out_intr;
188451df0accSAmit Shah io_names[j] = "input";
188551df0accSAmit Shah io_names[j + 1] = "output";
188651df0accSAmit Shah }
188751df0accSAmit Shah }
188851df0accSAmit Shah /* Find the queues. */
18899b2bbdb2SMichael S. Tsirkin err = virtio_find_vqs(portdev->vdev, nr_queues, vqs,
189051df0accSAmit Shah io_callbacks,
1891fb5e31d9SChristoph Hellwig (const char **)io_names, NULL);
189251df0accSAmit Shah if (err)
189351df0accSAmit Shah goto free;
189451df0accSAmit Shah
189551df0accSAmit Shah j = 0;
189651df0accSAmit Shah portdev->in_vqs[0] = vqs[0];
189751df0accSAmit Shah portdev->out_vqs[0] = vqs[1];
189851df0accSAmit Shah j += 2;
189951df0accSAmit Shah if (use_multiport(portdev)) {
190051df0accSAmit Shah portdev->c_ivq = vqs[j];
190151df0accSAmit Shah portdev->c_ovq = vqs[j + 1];
190251df0accSAmit Shah
190351df0accSAmit Shah for (i = 1; i < nr_ports; i++) {
190451df0accSAmit Shah j += 2;
190551df0accSAmit Shah portdev->in_vqs[i] = vqs[j];
190651df0accSAmit Shah portdev->out_vqs[i] = vqs[j + 1];
190751df0accSAmit Shah }
190851df0accSAmit Shah }
190951df0accSAmit Shah kfree(io_names);
191051df0accSAmit Shah kfree(io_callbacks);
191151df0accSAmit Shah kfree(vqs);
191251df0accSAmit Shah
191351df0accSAmit Shah return 0;
191451df0accSAmit Shah
191551df0accSAmit Shah free:
191651df0accSAmit Shah kfree(portdev->out_vqs);
191751df0accSAmit Shah kfree(portdev->in_vqs);
191851df0accSAmit Shah kfree(io_names);
191951df0accSAmit Shah kfree(io_callbacks);
192051df0accSAmit Shah kfree(vqs);
192151df0accSAmit Shah
192251df0accSAmit Shah return err;
192351df0accSAmit Shah }
192451df0accSAmit Shah
192551df0accSAmit Shah static const struct file_operations portdev_fops = {
192651df0accSAmit Shah .owner = THIS_MODULE,
192751df0accSAmit Shah };
192851df0accSAmit Shah
remove_vqs(struct ports_device * portdev)1929a0e2dbfcSAmit Shah static void remove_vqs(struct ports_device *portdev)
1930a0e2dbfcSAmit Shah {
1931a7a69ec0SMichael S. Tsirkin struct virtqueue *vq;
1932a7a69ec0SMichael S. Tsirkin
1933a7a69ec0SMichael S. Tsirkin virtio_device_for_each_vq(portdev->vdev, vq) {
1934a7a69ec0SMichael S. Tsirkin struct port_buffer *buf;
1935a7a69ec0SMichael S. Tsirkin
1936a7a69ec0SMichael S. Tsirkin flush_bufs(vq, true);
1937a7a69ec0SMichael S. Tsirkin while ((buf = virtqueue_detach_unused_buf(vq)))
1938a7a69ec0SMichael S. Tsirkin free_buf(buf, true);
193956b5e65eSXianting Tian cond_resched();
1940a7a69ec0SMichael S. Tsirkin }
1941a0e2dbfcSAmit Shah portdev->vdev->config->del_vqs(portdev->vdev);
1942a0e2dbfcSAmit Shah kfree(portdev->in_vqs);
1943a0e2dbfcSAmit Shah kfree(portdev->out_vqs);
1944a0e2dbfcSAmit Shah }
1945a0e2dbfcSAmit Shah
virtcons_remove(struct virtio_device * vdev)1946aa44ec86SMichael S. Tsirkin static void virtcons_remove(struct virtio_device *vdev)
1947aa44ec86SMichael S. Tsirkin {
1948aa44ec86SMichael S. Tsirkin struct ports_device *portdev;
1949aa44ec86SMichael S. Tsirkin struct port *port, *port2;
1950aa44ec86SMichael S. Tsirkin
1951aa44ec86SMichael S. Tsirkin portdev = vdev->priv;
1952aa44ec86SMichael S. Tsirkin
1953aa44ec86SMichael S. Tsirkin spin_lock_irq(&pdrvdata_lock);
1954aa44ec86SMichael S. Tsirkin list_del(&portdev->list);
1955aa44ec86SMichael S. Tsirkin spin_unlock_irq(&pdrvdata_lock);
1956aa44ec86SMichael S. Tsirkin
19570e7174b9SMichael S. Tsirkin /* Device is going away, exit any polling for buffers */
19580e7174b9SMichael S. Tsirkin virtio_break_device(vdev);
19590e7174b9SMichael S. Tsirkin if (use_multiport(portdev))
19600e7174b9SMichael S. Tsirkin flush_work(&portdev->control_work);
19610e7174b9SMichael S. Tsirkin else
19620e7174b9SMichael S. Tsirkin flush_work(&portdev->config_work);
19630e7174b9SMichael S. Tsirkin
1964aa44ec86SMichael S. Tsirkin /* Disable interrupts for vqs */
1965d9679d00SMichael S. Tsirkin virtio_reset_device(vdev);
1966aa44ec86SMichael S. Tsirkin /* Finish up work that's lined up */
1967aa44ec86SMichael S. Tsirkin if (use_multiport(portdev))
1968aa44ec86SMichael S. Tsirkin cancel_work_sync(&portdev->control_work);
1969aa44ec86SMichael S. Tsirkin else
1970aa44ec86SMichael S. Tsirkin cancel_work_sync(&portdev->config_work);
1971aa44ec86SMichael S. Tsirkin
1972aa44ec86SMichael S. Tsirkin list_for_each_entry_safe(port, port2, &portdev->ports, list)
1973aa44ec86SMichael S. Tsirkin unplug_port(port);
1974aa44ec86SMichael S. Tsirkin
1975aa44ec86SMichael S. Tsirkin unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1976aa44ec86SMichael S. Tsirkin
1977aa44ec86SMichael S. Tsirkin /*
1978aa44ec86SMichael S. Tsirkin * When yanking out a device, we immediately lose the
1979aa44ec86SMichael S. Tsirkin * (device-side) queues. So there's no point in keeping the
1980aa44ec86SMichael S. Tsirkin * guest side around till we drop our final reference. This
1981aa44ec86SMichael S. Tsirkin * also means that any ports which are in an open state will
1982aa44ec86SMichael S. Tsirkin * have to just stop using the port, as the vqs are going
1983aa44ec86SMichael S. Tsirkin * away.
1984aa44ec86SMichael S. Tsirkin */
1985aa44ec86SMichael S. Tsirkin remove_vqs(portdev);
1986aa44ec86SMichael S. Tsirkin kfree(portdev);
1987aa44ec86SMichael S. Tsirkin }
1988aa44ec86SMichael S. Tsirkin
198951df0accSAmit Shah /*
199051df0accSAmit Shah * Once we're further in boot, we get probed like any other virtio
199151df0accSAmit Shah * device.
199251df0accSAmit Shah *
199351df0accSAmit Shah * If the host also supports multiple console ports, we check the
199451df0accSAmit Shah * config space to see how many ports the host has spawned. We
199551df0accSAmit Shah * initialize each port found.
199651df0accSAmit Shah */
virtcons_probe(struct virtio_device * vdev)19972223cbecSBill Pemberton static int virtcons_probe(struct virtio_device *vdev)
199851df0accSAmit Shah {
199951df0accSAmit Shah struct ports_device *portdev;
200051df0accSAmit Shah int err;
200151df0accSAmit Shah bool multiport;
20025e38483bSChristian Borntraeger bool early = early_put_chars != NULL;
20035e38483bSChristian Borntraeger
2004be8ff595SRusty Russell /* We only need a config space if features are offered */
2005be8ff595SRusty Russell if (!vdev->config->get &&
2006be8ff595SRusty Russell (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
2007be8ff595SRusty Russell || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
2008011f0e7aSMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n",
2009011f0e7aSMichael S. Tsirkin __func__);
2010011f0e7aSMichael S. Tsirkin return -EINVAL;
2011011f0e7aSMichael S. Tsirkin }
2012011f0e7aSMichael S. Tsirkin
20135e38483bSChristian Borntraeger /* Ensure to read early_put_chars now */
20145e38483bSChristian Borntraeger barrier();
201551df0accSAmit Shah
201651df0accSAmit Shah portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
201751df0accSAmit Shah if (!portdev) {
201851df0accSAmit Shah err = -ENOMEM;
201951df0accSAmit Shah goto fail;
202051df0accSAmit Shah }
202151df0accSAmit Shah
202251df0accSAmit Shah /* Attach this portdev to this virtio_device, and vice-versa. */
202351df0accSAmit Shah portdev->vdev = vdev;
202451df0accSAmit Shah vdev->priv = portdev;
202551df0accSAmit Shah
202651df0accSAmit Shah portdev->chr_major = register_chrdev(0, "virtio-portsdev",
202751df0accSAmit Shah &portdev_fops);
202851df0accSAmit Shah if (portdev->chr_major < 0) {
202951df0accSAmit Shah dev_err(&vdev->dev,
203051df0accSAmit Shah "Error %d registering chrdev for device %u\n",
2031dc18f080SSjur Brændeland portdev->chr_major, vdev->index);
203251df0accSAmit Shah err = portdev->chr_major;
203351df0accSAmit Shah goto free;
203451df0accSAmit Shah }
203551df0accSAmit Shah
203651df0accSAmit Shah multiport = false;
20377328fa64SMichael S. Tsirkin portdev->max_nr_ports = 1;
20381b637046SSjur Brændeland
20391b637046SSjur Brændeland /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
20401b637046SSjur Brændeland if (!is_rproc_serial(vdev) &&
2041855e0c52SRusty Russell virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
2042855e0c52SRusty Russell struct virtio_console_config, max_nr_ports,
20437328fa64SMichael S. Tsirkin &portdev->max_nr_ports) == 0) {
204428962ec5SJason Wang if (portdev->max_nr_ports == 0 ||
204528962ec5SJason Wang portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
204628962ec5SJason Wang dev_err(&vdev->dev,
204728962ec5SJason Wang "Invalidate max_nr_ports %d",
204828962ec5SJason Wang portdev->max_nr_ports);
204928962ec5SJason Wang err = -EINVAL;
205028962ec5SJason Wang goto free;
205128962ec5SJason Wang }
205251c6d61aSSasha Levin multiport = true;
20531b637046SSjur Brændeland }
205451df0accSAmit Shah
205551df0accSAmit Shah spin_lock_init(&portdev->ports_lock);
205651df0accSAmit Shah INIT_LIST_HEAD(&portdev->ports);
20575c60300dSMichael S. Tsirkin INIT_LIST_HEAD(&portdev->list);
205851df0accSAmit Shah
2059eeb8a7e8SMichael S. Tsirkin INIT_WORK(&portdev->config_work, &config_work_handler);
20604f6e24edSMichael S. Tsirkin INIT_WORK(&portdev->control_work, &control_work_handler);
20614f6e24edSMichael S. Tsirkin
206251df0accSAmit Shah if (multiport) {
2063165b1b8bSAmit Shah spin_lock_init(&portdev->c_ivq_lock);
20649ba5c80bSAmit Shah spin_lock_init(&portdev->c_ovq_lock);
2065*fd949863SMichael S. Tsirkin }
206651df0accSAmit Shah
2067*fd949863SMichael S. Tsirkin err = init_vqs(portdev);
2068*fd949863SMichael S. Tsirkin if (err < 0) {
2069*fd949863SMichael S. Tsirkin dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
2070*fd949863SMichael S. Tsirkin goto free_chrdev;
2071*fd949863SMichael S. Tsirkin }
2072*fd949863SMichael S. Tsirkin
2073*fd949863SMichael S. Tsirkin virtio_device_ready(portdev->vdev);
2074*fd949863SMichael S. Tsirkin
2075*fd949863SMichael S. Tsirkin if (multiport) {
2076d791cfcbSLaurent Vivier err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2077d791cfcbSLaurent Vivier if (err < 0) {
207851df0accSAmit Shah dev_err(&vdev->dev,
207951df0accSAmit Shah "Error allocating buffers for control queue\n");
20805c60300dSMichael S. Tsirkin /*
20815c60300dSMichael S. Tsirkin * The host might want to notify mgmt sw about device
20825c60300dSMichael S. Tsirkin * add failure.
20835c60300dSMichael S. Tsirkin */
20845c60300dSMichael S. Tsirkin __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
20855c60300dSMichael S. Tsirkin VIRTIO_CONSOLE_DEVICE_READY, 0);
20865c60300dSMichael S. Tsirkin /* Device was functional: we need full cleanup. */
20875c60300dSMichael S. Tsirkin virtcons_remove(vdev);
2088d791cfcbSLaurent Vivier return err;
208951df0accSAmit Shah }
209051df0accSAmit Shah } else {
209151df0accSAmit Shah /*
209251df0accSAmit Shah * For backward compatibility: Create a console port
209351df0accSAmit Shah * if we're running on older host.
209451df0accSAmit Shah */
209551df0accSAmit Shah add_port(portdev, 0);
209651df0accSAmit Shah }
209751df0accSAmit Shah
209851df0accSAmit Shah spin_lock_irq(&pdrvdata_lock);
209951df0accSAmit Shah list_add_tail(&portdev->list, &pdrvdata.portdevs);
210051df0accSAmit Shah spin_unlock_irq(&pdrvdata_lock);
210151df0accSAmit Shah
210251df0accSAmit Shah __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
210351df0accSAmit Shah VIRTIO_CONSOLE_DEVICE_READY, 1);
21045e38483bSChristian Borntraeger
21055e38483bSChristian Borntraeger /*
21065e38483bSChristian Borntraeger * If there was an early virtio console, assume that there are no
21075e38483bSChristian Borntraeger * other consoles. We need to wait until the hvc_alloc matches the
21085e38483bSChristian Borntraeger * hvc_instantiate, otherwise tty_open will complain, resulting in
21095e38483bSChristian Borntraeger * a "Warning: unable to open an initial console" boot failure.
21105e38483bSChristian Borntraeger * Without multiport this is done in add_port above. With multiport
21115e38483bSChristian Borntraeger * this might take some host<->guest communication - thus we have to
21125e38483bSChristian Borntraeger * wait.
21135e38483bSChristian Borntraeger */
21145e38483bSChristian Borntraeger if (multiport && early)
21155e38483bSChristian Borntraeger wait_for_completion(&early_console_added);
21165e38483bSChristian Borntraeger
211751df0accSAmit Shah return 0;
211851df0accSAmit Shah
211951df0accSAmit Shah free_chrdev:
212051df0accSAmit Shah unregister_chrdev(portdev->chr_major, "virtio-portsdev");
212151df0accSAmit Shah free:
212251df0accSAmit Shah kfree(portdev);
212351df0accSAmit Shah fail:
212451df0accSAmit Shah return err;
212551df0accSAmit Shah }
212651df0accSAmit Shah
21278b66c917SRikard Falkeborn static const struct virtio_device_id id_table[] = {
212851df0accSAmit Shah { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
212951df0accSAmit Shah { 0 },
213051df0accSAmit Shah };
2131897c44f0SAlexander Lobakin MODULE_DEVICE_TABLE(virtio, id_table);
213251df0accSAmit Shah
21338b66c917SRikard Falkeborn static const unsigned int features[] = {
213451df0accSAmit Shah VIRTIO_CONSOLE_F_SIZE,
213551df0accSAmit Shah VIRTIO_CONSOLE_F_MULTIPORT,
213651df0accSAmit Shah };
213751df0accSAmit Shah
21388b66c917SRikard Falkeborn static const struct virtio_device_id rproc_serial_id_table[] = {
21391b637046SSjur Brændeland #if IS_ENABLED(CONFIG_REMOTEPROC)
21401b637046SSjur Brændeland { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
21411b637046SSjur Brændeland #endif
21421b637046SSjur Brændeland { 0 },
21431b637046SSjur Brændeland };
2144897c44f0SAlexander Lobakin MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
21451b637046SSjur Brændeland
21468b66c917SRikard Falkeborn static const unsigned int rproc_serial_features[] = {
21471b637046SSjur Brændeland };
21481b637046SSjur Brændeland
214989107000SAaron Lu #ifdef CONFIG_PM_SLEEP
virtcons_freeze(struct virtio_device * vdev)21502b8f41d8SAmit Shah static int virtcons_freeze(struct virtio_device *vdev)
21512b8f41d8SAmit Shah {
21522b8f41d8SAmit Shah struct ports_device *portdev;
21532b8f41d8SAmit Shah struct port *port;
21542b8f41d8SAmit Shah
21552b8f41d8SAmit Shah portdev = vdev->priv;
21562b8f41d8SAmit Shah
2157d9679d00SMichael S. Tsirkin virtio_reset_device(vdev);
21582b8f41d8SAmit Shah
21592055997fSMichael S. Tsirkin if (use_multiport(portdev))
2160c743d09dSAmit Shah virtqueue_disable_cb(portdev->c_ivq);
21612b8f41d8SAmit Shah cancel_work_sync(&portdev->control_work);
2162eeb8a7e8SMichael S. Tsirkin cancel_work_sync(&portdev->config_work);
2163c743d09dSAmit Shah /*
2164c743d09dSAmit Shah * Once more: if control_work_handler() was running, it would
2165c743d09dSAmit Shah * enable the cb as the last step.
2166c743d09dSAmit Shah */
21672055997fSMichael S. Tsirkin if (use_multiport(portdev))
2168c743d09dSAmit Shah virtqueue_disable_cb(portdev->c_ivq);
21692b8f41d8SAmit Shah
21702b8f41d8SAmit Shah list_for_each_entry(port, &portdev->ports, list) {
2171c743d09dSAmit Shah virtqueue_disable_cb(port->in_vq);
2172c743d09dSAmit Shah virtqueue_disable_cb(port->out_vq);
21732b8f41d8SAmit Shah /*
21742b8f41d8SAmit Shah * We'll ask the host later if the new invocation has
21752b8f41d8SAmit Shah * the port opened or closed.
21762b8f41d8SAmit Shah */
21772b8f41d8SAmit Shah port->host_connected = false;
21782b8f41d8SAmit Shah remove_port_data(port);
21792b8f41d8SAmit Shah }
21802b8f41d8SAmit Shah remove_vqs(portdev);
21812b8f41d8SAmit Shah
21822b8f41d8SAmit Shah return 0;
21832b8f41d8SAmit Shah }
21842b8f41d8SAmit Shah
virtcons_restore(struct virtio_device * vdev)21852b8f41d8SAmit Shah static int virtcons_restore(struct virtio_device *vdev)
21862b8f41d8SAmit Shah {
21872b8f41d8SAmit Shah struct ports_device *portdev;
21882b8f41d8SAmit Shah struct port *port;
21892b8f41d8SAmit Shah int ret;
21902b8f41d8SAmit Shah
21912b8f41d8SAmit Shah portdev = vdev->priv;
21922b8f41d8SAmit Shah
21932b8f41d8SAmit Shah ret = init_vqs(portdev);
21942b8f41d8SAmit Shah if (ret)
21952b8f41d8SAmit Shah return ret;
21962b8f41d8SAmit Shah
2197401bbdc9SMichael S. Tsirkin virtio_device_ready(portdev->vdev);
2198401bbdc9SMichael S. Tsirkin
21992b8f41d8SAmit Shah if (use_multiport(portdev))
2200165b1b8bSAmit Shah fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
22012b8f41d8SAmit Shah
22022b8f41d8SAmit Shah list_for_each_entry(port, &portdev->ports, list) {
22032b8f41d8SAmit Shah port->in_vq = portdev->in_vqs[port->id];
22042b8f41d8SAmit Shah port->out_vq = portdev->out_vqs[port->id];
22052b8f41d8SAmit Shah
22062b8f41d8SAmit Shah fill_queue(port->in_vq, &port->inbuf_lock);
22072b8f41d8SAmit Shah
22082b8f41d8SAmit Shah /* Get port open/close status on the host */
22092b8f41d8SAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2210fa8b66ccSAmit Shah
2211fa8b66ccSAmit Shah /*
2212fa8b66ccSAmit Shah * If a port was open at the time of suspending, we
2213fa8b66ccSAmit Shah * have to let the host know that it's still open.
2214fa8b66ccSAmit Shah */
2215fa8b66ccSAmit Shah if (port->guest_connected)
2216fa8b66ccSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
22172b8f41d8SAmit Shah }
22182b8f41d8SAmit Shah return 0;
22192b8f41d8SAmit Shah }
22202b8f41d8SAmit Shah #endif
22212b8f41d8SAmit Shah
222251df0accSAmit Shah static struct virtio_driver virtio_console = {
222351df0accSAmit Shah .feature_table = features,
222451df0accSAmit Shah .feature_table_size = ARRAY_SIZE(features),
222551df0accSAmit Shah .driver.name = KBUILD_MODNAME,
222651df0accSAmit Shah .driver.owner = THIS_MODULE,
222751df0accSAmit Shah .id_table = id_table,
222851df0accSAmit Shah .probe = virtcons_probe,
222951df0accSAmit Shah .remove = virtcons_remove,
223051df0accSAmit Shah .config_changed = config_intr,
223189107000SAaron Lu #ifdef CONFIG_PM_SLEEP
22322b8f41d8SAmit Shah .freeze = virtcons_freeze,
22332b8f41d8SAmit Shah .restore = virtcons_restore,
22342b8f41d8SAmit Shah #endif
223551df0accSAmit Shah };
223651df0accSAmit Shah
2237bcd2982aSGreg Kroah-Hartman static struct virtio_driver virtio_rproc_serial = {
22381b637046SSjur Brændeland .feature_table = rproc_serial_features,
22391b637046SSjur Brændeland .feature_table_size = ARRAY_SIZE(rproc_serial_features),
22401b637046SSjur Brændeland .driver.name = "virtio_rproc_serial",
22411b637046SSjur Brændeland .driver.owner = THIS_MODULE,
22421b637046SSjur Brændeland .id_table = rproc_serial_id_table,
22431b637046SSjur Brændeland .probe = virtcons_probe,
22441b637046SSjur Brændeland .remove = virtcons_remove,
22451b637046SSjur Brændeland };
22461b637046SSjur Brændeland
virtio_console_init(void)2247fefb8a2aSRandy Dunlap static int __init virtio_console_init(void)
224851df0accSAmit Shah {
224951df0accSAmit Shah int err;
225051df0accSAmit Shah
225111680fdfSIvan Orlov err = class_register(&port_class);
225211680fdfSIvan Orlov if (err)
225351df0accSAmit Shah return err;
225451df0accSAmit Shah
225551df0accSAmit Shah pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
225651df0accSAmit Shah INIT_LIST_HEAD(&pdrvdata.consoles);
225751df0accSAmit Shah INIT_LIST_HEAD(&pdrvdata.portdevs);
225851df0accSAmit Shah
225933e1afc3SAlexey Khoroshilov err = register_virtio_driver(&virtio_console);
226033e1afc3SAlexey Khoroshilov if (err < 0) {
226133e1afc3SAlexey Khoroshilov pr_err("Error %d registering virtio driver\n", err);
226233e1afc3SAlexey Khoroshilov goto free;
226333e1afc3SAlexey Khoroshilov }
22641b637046SSjur Brændeland err = register_virtio_driver(&virtio_rproc_serial);
22651b637046SSjur Brændeland if (err < 0) {
22661b637046SSjur Brændeland pr_err("Error %d registering virtio rproc serial driver\n",
22671b637046SSjur Brændeland err);
22681b637046SSjur Brændeland goto unregister;
22691b637046SSjur Brændeland }
227033e1afc3SAlexey Khoroshilov return 0;
22711b637046SSjur Brændeland unregister:
22721b637046SSjur Brændeland unregister_virtio_driver(&virtio_console);
227333e1afc3SAlexey Khoroshilov free:
227433e1afc3SAlexey Khoroshilov debugfs_remove_recursive(pdrvdata.debugfs_dir);
227511680fdfSIvan Orlov class_unregister(&port_class);
227633e1afc3SAlexey Khoroshilov return err;
227751df0accSAmit Shah }
227851df0accSAmit Shah
virtio_console_fini(void)2279fefb8a2aSRandy Dunlap static void __exit virtio_console_fini(void)
228051df0accSAmit Shah {
22811b637046SSjur Brændeland reclaim_dma_bufs();
22821b637046SSjur Brændeland
228351df0accSAmit Shah unregister_virtio_driver(&virtio_console);
22841b637046SSjur Brændeland unregister_virtio_driver(&virtio_rproc_serial);
228551df0accSAmit Shah
228611680fdfSIvan Orlov class_unregister(&port_class);
228751df0accSAmit Shah debugfs_remove_recursive(pdrvdata.debugfs_dir);
228851df0accSAmit Shah }
2289fefb8a2aSRandy Dunlap module_init(virtio_console_init);
2290fefb8a2aSRandy Dunlap module_exit(virtio_console_fini);
229151df0accSAmit Shah
229251df0accSAmit Shah MODULE_DESCRIPTION("Virtio console driver");
229351df0accSAmit Shah MODULE_LICENSE("GPL");
2294