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 */
109bf5abc17SKees Cook struct scatterlist sg[] __counted_by(sgpages);
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
find_port_by_vtermno(u32 vtermno)23351df0accSAmit Shah static struct port *find_port_by_vtermno(u32 vtermno)
23451df0accSAmit Shah {
23551df0accSAmit Shah struct port *port;
23651df0accSAmit Shah struct console *cons;
23751df0accSAmit Shah unsigned long flags;
23851df0accSAmit Shah
23951df0accSAmit Shah spin_lock_irqsave(&pdrvdata_lock, flags);
24051df0accSAmit Shah list_for_each_entry(cons, &pdrvdata.consoles, list) {
24151df0accSAmit Shah if (cons->vtermno == vtermno) {
24251df0accSAmit Shah port = container_of(cons, struct port, cons);
24351df0accSAmit Shah goto out;
24451df0accSAmit Shah }
24551df0accSAmit Shah }
24651df0accSAmit Shah port = NULL;
24751df0accSAmit Shah out:
24851df0accSAmit Shah spin_unlock_irqrestore(&pdrvdata_lock, flags);
24951df0accSAmit Shah return port;
25051df0accSAmit Shah }
25151df0accSAmit Shah
find_port_by_devt_in_portdev(struct ports_device * portdev,dev_t dev)25251df0accSAmit Shah static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
25351df0accSAmit Shah dev_t dev)
25451df0accSAmit Shah {
25551df0accSAmit Shah struct port *port;
25651df0accSAmit Shah unsigned long flags;
25751df0accSAmit Shah
25851df0accSAmit Shah spin_lock_irqsave(&portdev->ports_lock, flags);
259057b82beSAmit Shah list_for_each_entry(port, &portdev->ports, list) {
260057b82beSAmit Shah if (port->cdev->dev == dev) {
261057b82beSAmit Shah kref_get(&port->kref);
26251df0accSAmit Shah goto out;
263057b82beSAmit Shah }
264057b82beSAmit Shah }
26551df0accSAmit Shah port = NULL;
26651df0accSAmit Shah out:
26751df0accSAmit Shah spin_unlock_irqrestore(&portdev->ports_lock, flags);
26851df0accSAmit Shah
26951df0accSAmit Shah return port;
27051df0accSAmit Shah }
27151df0accSAmit Shah
find_port_by_devt(dev_t dev)27251df0accSAmit Shah static struct port *find_port_by_devt(dev_t dev)
27351df0accSAmit Shah {
27451df0accSAmit Shah struct ports_device *portdev;
27551df0accSAmit Shah struct port *port;
27651df0accSAmit Shah unsigned long flags;
27751df0accSAmit Shah
27851df0accSAmit Shah spin_lock_irqsave(&pdrvdata_lock, flags);
27951df0accSAmit Shah list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
28051df0accSAmit Shah port = find_port_by_devt_in_portdev(portdev, dev);
28151df0accSAmit Shah if (port)
28251df0accSAmit Shah goto out;
28351df0accSAmit Shah }
28451df0accSAmit Shah port = NULL;
28551df0accSAmit Shah out:
28651df0accSAmit Shah spin_unlock_irqrestore(&pdrvdata_lock, flags);
28751df0accSAmit Shah return port;
28851df0accSAmit Shah }
28951df0accSAmit Shah
find_port_by_id(struct ports_device * portdev,u32 id)29051df0accSAmit Shah static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
29151df0accSAmit Shah {
29251df0accSAmit Shah struct port *port;
29351df0accSAmit Shah unsigned long flags;
29451df0accSAmit Shah
29551df0accSAmit Shah spin_lock_irqsave(&portdev->ports_lock, flags);
29651df0accSAmit Shah list_for_each_entry(port, &portdev->ports, list)
29751df0accSAmit Shah if (port->id == id)
29851df0accSAmit Shah goto out;
29951df0accSAmit Shah port = NULL;
30051df0accSAmit Shah out:
30151df0accSAmit Shah spin_unlock_irqrestore(&portdev->ports_lock, flags);
30251df0accSAmit Shah
30351df0accSAmit Shah return port;
30451df0accSAmit Shah }
30551df0accSAmit Shah
find_port_by_vq(struct ports_device * portdev,struct virtqueue * vq)30651df0accSAmit Shah static struct port *find_port_by_vq(struct ports_device *portdev,
30751df0accSAmit Shah struct virtqueue *vq)
30851df0accSAmit Shah {
30951df0accSAmit Shah struct port *port;
31051df0accSAmit Shah unsigned long flags;
31151df0accSAmit Shah
31251df0accSAmit Shah spin_lock_irqsave(&portdev->ports_lock, flags);
31351df0accSAmit Shah list_for_each_entry(port, &portdev->ports, list)
31451df0accSAmit Shah if (port->in_vq == vq || port->out_vq == vq)
31551df0accSAmit Shah goto out;
31651df0accSAmit Shah port = NULL;
31751df0accSAmit Shah out:
31851df0accSAmit Shah spin_unlock_irqrestore(&portdev->ports_lock, flags);
31951df0accSAmit Shah return port;
32051df0accSAmit Shah }
32151df0accSAmit Shah
is_console_port(struct port * port)32251df0accSAmit Shah static bool is_console_port(struct port *port)
32351df0accSAmit Shah {
32451df0accSAmit Shah if (port->cons.hvc)
32551df0accSAmit Shah return true;
32651df0accSAmit Shah return false;
32751df0accSAmit Shah }
32851df0accSAmit Shah
is_rproc_serial(const struct virtio_device * vdev)3291b637046SSjur Brændeland static bool is_rproc_serial(const struct virtio_device *vdev)
3301b637046SSjur Brændeland {
3311b637046SSjur Brændeland return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
3321b637046SSjur Brændeland }
3331b637046SSjur Brændeland
use_multiport(struct ports_device * portdev)33451df0accSAmit Shah static inline bool use_multiport(struct ports_device *portdev)
33551df0accSAmit Shah {
33651df0accSAmit Shah /*
33751df0accSAmit Shah * This condition can be true when put_chars is called from
33851df0accSAmit Shah * early_init
33951df0accSAmit Shah */
34051df0accSAmit Shah if (!portdev->vdev)
341f580d730SJoe Perches return false;
342e16e12beSMichael S. Tsirkin return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
34351df0accSAmit Shah }
34451df0accSAmit Shah
3451b637046SSjur Brændeland static DEFINE_SPINLOCK(dma_bufs_lock);
3461b637046SSjur Brændeland static LIST_HEAD(pending_free_dma_bufs);
3471b637046SSjur Brændeland
free_buf(struct port_buffer * buf,bool can_sleep)3481b637046SSjur Brændeland static void free_buf(struct port_buffer *buf, bool can_sleep)
34951df0accSAmit Shah {
350276a3e95SSjur Brændeland unsigned int i;
351276a3e95SSjur Brændeland
352276a3e95SSjur Brændeland for (i = 0; i < buf->sgpages; i++) {
353276a3e95SSjur Brændeland struct page *page = sg_page(&buf->sg[i]);
354276a3e95SSjur Brændeland if (!page)
355276a3e95SSjur Brændeland break;
356276a3e95SSjur Brændeland put_page(page);
357276a3e95SSjur Brændeland }
358276a3e95SSjur Brændeland
3591b637046SSjur Brændeland if (!buf->dev) {
36051df0accSAmit Shah kfree(buf->buf);
3611b637046SSjur Brændeland } else if (is_rproc_enabled) {
3621b637046SSjur Brændeland unsigned long flags;
3631b637046SSjur Brændeland
3641b637046SSjur Brændeland /* dma_free_coherent requires interrupts to be enabled. */
3651b637046SSjur Brændeland if (!can_sleep) {
3661b637046SSjur Brændeland /* queue up dma-buffers to be freed later */
3671b637046SSjur Brændeland spin_lock_irqsave(&dma_bufs_lock, flags);
3681b637046SSjur Brændeland list_add_tail(&buf->list, &pending_free_dma_bufs);
3691b637046SSjur Brændeland spin_unlock_irqrestore(&dma_bufs_lock, flags);
3701b637046SSjur Brændeland return;
3711b637046SSjur Brændeland }
3721b637046SSjur Brændeland dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
3731b637046SSjur Brændeland
3741b637046SSjur Brændeland /* Release device refcnt and allow it to be freed */
3751b637046SSjur Brændeland put_device(buf->dev);
3761b637046SSjur Brændeland }
3771b637046SSjur Brændeland
37851df0accSAmit Shah kfree(buf);
37951df0accSAmit Shah }
38051df0accSAmit Shah
reclaim_dma_bufs(void)3811b637046SSjur Brændeland static void reclaim_dma_bufs(void)
3821b637046SSjur Brændeland {
3831b637046SSjur Brændeland unsigned long flags;
3841b637046SSjur Brændeland struct port_buffer *buf, *tmp;
3851b637046SSjur Brændeland LIST_HEAD(tmp_list);
3861b637046SSjur Brændeland
3871b637046SSjur Brændeland if (list_empty(&pending_free_dma_bufs))
3881b637046SSjur Brændeland return;
3891b637046SSjur Brændeland
3901b637046SSjur Brændeland /* Create a copy of the pending_free_dma_bufs while holding the lock */
3911b637046SSjur Brændeland spin_lock_irqsave(&dma_bufs_lock, flags);
3921b637046SSjur Brændeland list_cut_position(&tmp_list, &pending_free_dma_bufs,
3931b637046SSjur Brændeland pending_free_dma_bufs.prev);
3941b637046SSjur Brændeland spin_unlock_irqrestore(&dma_bufs_lock, flags);
3951b637046SSjur Brændeland
3961b637046SSjur Brændeland /* Release the dma buffers, without irqs enabled */
3971b637046SSjur Brændeland list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
3981b637046SSjur Brændeland list_del(&buf->list);
3991b637046SSjur Brændeland free_buf(buf, true);
4001b637046SSjur Brændeland }
4011b637046SSjur Brændeland }
4021b637046SSjur Brændeland
alloc_buf(struct virtio_device * vdev,size_t buf_size,int pages)4032855b335SMichael S. Tsirkin static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
404276a3e95SSjur Brændeland int pages)
40551df0accSAmit Shah {
40651df0accSAmit Shah struct port_buffer *buf;
40751df0accSAmit Shah
4081b637046SSjur Brændeland reclaim_dma_bufs();
4091b637046SSjur Brændeland
410276a3e95SSjur Brændeland /*
411276a3e95SSjur Brændeland * Allocate buffer and the sg list. The sg list array is allocated
412276a3e95SSjur Brændeland * directly after the port_buffer struct.
413276a3e95SSjur Brændeland */
4145b572e25SMatthew Wilcox buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
41551df0accSAmit Shah if (!buf)
41651df0accSAmit Shah goto fail;
417276a3e95SSjur Brændeland
418276a3e95SSjur Brændeland buf->sgpages = pages;
419276a3e95SSjur Brændeland if (pages > 0) {
4201b637046SSjur Brændeland buf->dev = NULL;
421276a3e95SSjur Brændeland buf->buf = NULL;
422276a3e95SSjur Brændeland return buf;
423276a3e95SSjur Brændeland }
424276a3e95SSjur Brændeland
4252855b335SMichael S. Tsirkin if (is_rproc_serial(vdev)) {
4261b637046SSjur Brændeland /*
4271b637046SSjur Brændeland * Allocate DMA memory from ancestor. When a virtio
4281b637046SSjur Brændeland * device is created by remoteproc, the DMA memory is
4299d516aa8SAlexander Lobakin * associated with the parent device:
4309d516aa8SAlexander Lobakin * virtioY => remoteprocX#vdevYbuffer.
4311b637046SSjur Brændeland */
4329d516aa8SAlexander Lobakin buf->dev = vdev->dev.parent;
4339d516aa8SAlexander Lobakin if (!buf->dev)
4341b637046SSjur Brændeland goto free_buf;
4351b637046SSjur Brændeland
4361b637046SSjur Brændeland /* Increase device refcnt to avoid freeing it */
4371b637046SSjur Brændeland get_device(buf->dev);
4381b637046SSjur Brændeland buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
4391b637046SSjur Brændeland GFP_KERNEL);
4401b637046SSjur Brændeland } else {
4411b637046SSjur Brændeland buf->dev = NULL;
4420127f685SSjur Brændeland buf->buf = kmalloc(buf_size, GFP_KERNEL);
4431b637046SSjur Brændeland }
4441b637046SSjur Brændeland
44551df0accSAmit Shah if (!buf->buf)
44651df0accSAmit Shah goto free_buf;
44751df0accSAmit Shah buf->len = 0;
44851df0accSAmit Shah buf->offset = 0;
44951df0accSAmit Shah buf->size = buf_size;
45051df0accSAmit Shah return buf;
45151df0accSAmit Shah
45251df0accSAmit Shah free_buf:
45351df0accSAmit Shah kfree(buf);
45451df0accSAmit Shah fail:
45551df0accSAmit Shah return NULL;
45651df0accSAmit Shah }
45751df0accSAmit Shah
45851df0accSAmit Shah /* Callers should take appropriate locks */
get_inbuf(struct port * port)459defde669SAmit Shah static struct port_buffer *get_inbuf(struct port *port)
46051df0accSAmit Shah {
46151df0accSAmit Shah struct port_buffer *buf;
46251df0accSAmit Shah unsigned int len;
46351df0accSAmit Shah
464d25a9ddaSAmit Shah if (port->inbuf)
465d25a9ddaSAmit Shah return port->inbuf;
466d25a9ddaSAmit Shah
467d25a9ddaSAmit Shah buf = virtqueue_get_buf(port->in_vq, &len);
46851df0accSAmit Shah if (buf) {
469d00d8da5SXie Yongji buf->len = min_t(size_t, len, buf->size);
47051df0accSAmit Shah buf->offset = 0;
47117e5b4f2SAmit Shah port->stats.bytes_received += len;
47251df0accSAmit Shah }
47351df0accSAmit Shah return buf;
47451df0accSAmit Shah }
47551df0accSAmit Shah
47651df0accSAmit Shah /*
47751df0accSAmit Shah * Create a scatter-gather list representing our input buffer and put
47851df0accSAmit Shah * it in the queue.
47951df0accSAmit Shah *
48051df0accSAmit Shah * Callers should take appropriate locks.
48151df0accSAmit Shah */
add_inbuf(struct virtqueue * vq,struct port_buffer * buf)48251df0accSAmit Shah static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
48351df0accSAmit Shah {
48451df0accSAmit Shah struct scatterlist sg[1];
48551df0accSAmit Shah int ret;
48651df0accSAmit Shah
48751df0accSAmit Shah sg_init_one(sg, buf->buf, buf->size);
48851df0accSAmit Shah
4896797999dSRusty Russell ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
49051df0accSAmit Shah virtqueue_kick(vq);
49149e86f16SAmit Shah if (!ret)
49249e86f16SAmit Shah ret = vq->num_free;
49351df0accSAmit Shah return ret;
49451df0accSAmit Shah }
49551df0accSAmit Shah
49651df0accSAmit Shah /* Discard any unread data this port has. Callers lockers. */
discard_port_data(struct port * port)49751df0accSAmit Shah static void discard_port_data(struct port *port)
49851df0accSAmit Shah {
49951df0accSAmit Shah struct port_buffer *buf;
5002d24cdaaSAmit Shah unsigned int err;
50151df0accSAmit Shah
502d7a62cd0SAmit Shah if (!port->portdev) {
503d7a62cd0SAmit Shah /* Device has been unplugged. vqs are already gone. */
504d7a62cd0SAmit Shah return;
505d7a62cd0SAmit Shah }
5062d24cdaaSAmit Shah buf = get_inbuf(port);
50751df0accSAmit Shah
508ce072a0cSAmit Shah err = 0;
50951df0accSAmit Shah while (buf) {
51017e5b4f2SAmit Shah port->stats.bytes_discarded += buf->len - buf->offset;
5112d24cdaaSAmit Shah if (add_inbuf(port->in_vq, buf) < 0) {
512ce072a0cSAmit Shah err++;
5131b637046SSjur Brændeland free_buf(buf, false);
51451df0accSAmit Shah }
51551df0accSAmit Shah port->inbuf = NULL;
5162d24cdaaSAmit Shah buf = get_inbuf(port);
5172d24cdaaSAmit Shah }
518ce072a0cSAmit Shah if (err)
51951df0accSAmit Shah dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
520ce072a0cSAmit Shah err);
52151df0accSAmit Shah }
52251df0accSAmit Shah
port_has_data(struct port * port)52351df0accSAmit Shah static bool port_has_data(struct port *port)
52451df0accSAmit Shah {
52551df0accSAmit Shah unsigned long flags;
52651df0accSAmit Shah bool ret;
52751df0accSAmit Shah
52851df0accSAmit Shah ret = false;
529d25a9ddaSAmit Shah spin_lock_irqsave(&port->inbuf_lock, flags);
530d25a9ddaSAmit Shah port->inbuf = get_inbuf(port);
531d25a9ddaSAmit Shah if (port->inbuf)
532d25a9ddaSAmit Shah ret = true;
533d25a9ddaSAmit Shah
53451df0accSAmit Shah spin_unlock_irqrestore(&port->inbuf_lock, flags);
53551df0accSAmit Shah return ret;
53651df0accSAmit Shah }
53751df0accSAmit Shah
__send_control_msg(struct ports_device * portdev,u32 port_id,unsigned int event,unsigned int value)53851df0accSAmit Shah static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
53951df0accSAmit Shah unsigned int event, unsigned int value)
54051df0accSAmit Shah {
54151df0accSAmit Shah struct scatterlist sg[1];
54251df0accSAmit Shah struct virtqueue *vq;
54351df0accSAmit Shah unsigned int len;
54451df0accSAmit Shah
54551df0accSAmit Shah if (!use_multiport(portdev))
54651df0accSAmit Shah return 0;
54751df0accSAmit Shah
54851df0accSAmit Shah vq = portdev->c_ovq;
54951df0accSAmit Shah
5509ba5c80bSAmit Shah spin_lock(&portdev->c_ovq_lock);
5515e59d9a1SAndy Lutomirski
5525e59d9a1SAndy Lutomirski portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
5535e59d9a1SAndy Lutomirski portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
5545e59d9a1SAndy Lutomirski portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
5555e59d9a1SAndy Lutomirski
5565e59d9a1SAndy Lutomirski sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
5575e59d9a1SAndy Lutomirski
5585e59d9a1SAndy Lutomirski if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
55951df0accSAmit Shah virtqueue_kick(vq);
56040e4dc53SHeinz Graalfs while (!virtqueue_get_buf(vq, &len)
56140e4dc53SHeinz Graalfs && !virtqueue_is_broken(vq))
56251df0accSAmit Shah cpu_relax();
56351df0accSAmit Shah }
5645e59d9a1SAndy Lutomirski
5659ba5c80bSAmit Shah spin_unlock(&portdev->c_ovq_lock);
56651df0accSAmit Shah return 0;
56751df0accSAmit Shah }
56851df0accSAmit Shah
send_control_msg(struct port * port,unsigned int event,unsigned int value)56951df0accSAmit Shah static ssize_t send_control_msg(struct port *port, unsigned int event,
57051df0accSAmit Shah unsigned int value)
57151df0accSAmit Shah {
57251df0accSAmit Shah /* Did the port get unplugged before userspace closed it? */
57351df0accSAmit Shah if (port->portdev)
57451df0accSAmit Shah return __send_control_msg(port->portdev, port->id, event, value);
57551df0accSAmit Shah return 0;
57651df0accSAmit Shah }
57751df0accSAmit Shah
578eb5e89fcSMasami Hiramatsu
57951df0accSAmit Shah /* Callers must take the port->outvq_lock */
reclaim_consumed_buffers(struct port * port)58051df0accSAmit Shah static void reclaim_consumed_buffers(struct port *port)
58151df0accSAmit Shah {
582276a3e95SSjur Brændeland struct port_buffer *buf;
58351df0accSAmit Shah unsigned int len;
58451df0accSAmit Shah
585d7a62cd0SAmit Shah if (!port->portdev) {
586d7a62cd0SAmit Shah /* Device has been unplugged. vqs are already gone. */
587d7a62cd0SAmit Shah return;
588d7a62cd0SAmit Shah }
589276a3e95SSjur Brændeland while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
5901b637046SSjur Brændeland free_buf(buf, false);
59151df0accSAmit Shah port->outvq_full = false;
59251df0accSAmit Shah }
59351df0accSAmit Shah }
59451df0accSAmit Shah
__send_to_port(struct port * port,struct scatterlist * sg,int nents,size_t in_count,void * data,bool nonblock)595eb5e89fcSMasami Hiramatsu static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
596eb5e89fcSMasami Hiramatsu int nents, size_t in_count,
597276a3e95SSjur Brændeland void *data, bool nonblock)
59851df0accSAmit Shah {
59951df0accSAmit Shah struct virtqueue *out_vq;
600589575a2SRusty Russell int err;
60151df0accSAmit Shah unsigned long flags;
60251df0accSAmit Shah unsigned int len;
60351df0accSAmit Shah
60451df0accSAmit Shah out_vq = port->out_vq;
60551df0accSAmit Shah
60651df0accSAmit Shah spin_lock_irqsave(&port->outvq_lock, flags);
60751df0accSAmit Shah
60851df0accSAmit Shah reclaim_consumed_buffers(port);
60951df0accSAmit Shah
6106797999dSRusty Russell err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
61151df0accSAmit Shah
61251df0accSAmit Shah /* Tell Host to go! */
61351df0accSAmit Shah virtqueue_kick(out_vq);
61451df0accSAmit Shah
615589575a2SRusty Russell if (err) {
61651df0accSAmit Shah in_count = 0;
61751df0accSAmit Shah goto done;
61851df0accSAmit Shah }
61951df0accSAmit Shah
620589575a2SRusty Russell if (out_vq->num_free == 0)
62151df0accSAmit Shah port->outvq_full = true;
62251df0accSAmit Shah
62351df0accSAmit Shah if (nonblock)
62451df0accSAmit Shah goto done;
62551df0accSAmit Shah
62651df0accSAmit Shah /*
62751df0accSAmit Shah * Wait till the host acknowledges it pushed out the data we
62851df0accSAmit Shah * sent. This is done for data from the hvc_console; the tty
62951df0accSAmit Shah * operations are performed with spinlocks held so we can't
63051df0accSAmit Shah * sleep here. An alternative would be to copy the data to a
63151df0accSAmit Shah * buffer and relax the spinning requirement. The downside is
63251df0accSAmit Shah * we need to kmalloc a GFP_ATOMIC buffer each time the
63351df0accSAmit Shah * console driver writes something out.
63451df0accSAmit Shah */
63540e4dc53SHeinz Graalfs while (!virtqueue_get_buf(out_vq, &len)
63640e4dc53SHeinz Graalfs && !virtqueue_is_broken(out_vq))
63751df0accSAmit Shah cpu_relax();
63851df0accSAmit Shah done:
63951df0accSAmit Shah spin_unlock_irqrestore(&port->outvq_lock, flags);
64017e5b4f2SAmit Shah
64117e5b4f2SAmit Shah port->stats.bytes_sent += in_count;
64251df0accSAmit Shah /*
64351df0accSAmit Shah * We're expected to return the amount of data we wrote -- all
64451df0accSAmit Shah * of it
64551df0accSAmit Shah */
64651df0accSAmit Shah return in_count;
64751df0accSAmit Shah }
64851df0accSAmit Shah
64951df0accSAmit Shah /*
65051df0accSAmit Shah * Give out the data that's requested from the buffer that we have
65151df0accSAmit Shah * queued up.
65251df0accSAmit Shah */
fill_readbuf(struct port * port,u8 __user * out_buf,size_t out_count,bool to_user)653*f32fcbedSJiri Slaby (SUSE) static ssize_t fill_readbuf(struct port *port, u8 __user *out_buf,
65448b36066SMichael S. Tsirkin size_t out_count, bool to_user)
65551df0accSAmit Shah {
65651df0accSAmit Shah struct port_buffer *buf;
65751df0accSAmit Shah unsigned long flags;
65851df0accSAmit Shah
65951df0accSAmit Shah if (!out_count || !port_has_data(port))
66051df0accSAmit Shah return 0;
66151df0accSAmit Shah
66251df0accSAmit Shah buf = port->inbuf;
66351df0accSAmit Shah out_count = min(out_count, buf->len - buf->offset);
66451df0accSAmit Shah
66551df0accSAmit Shah if (to_user) {
66651df0accSAmit Shah ssize_t ret;
66751df0accSAmit Shah
66851df0accSAmit Shah ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
66951df0accSAmit Shah if (ret)
67051df0accSAmit Shah return -EFAULT;
67151df0accSAmit Shah } else {
672*f32fcbedSJiri Slaby (SUSE) memcpy((__force u8 *)out_buf, buf->buf + buf->offset,
67348b36066SMichael S. Tsirkin out_count);
67451df0accSAmit Shah }
67551df0accSAmit Shah
67651df0accSAmit Shah buf->offset += out_count;
67751df0accSAmit Shah
67851df0accSAmit Shah if (buf->offset == buf->len) {
67951df0accSAmit Shah /*
68051df0accSAmit Shah * We're done using all the data in this buffer.
68151df0accSAmit Shah * Re-queue so that the Host can send us more data.
68251df0accSAmit Shah */
68351df0accSAmit Shah spin_lock_irqsave(&port->inbuf_lock, flags);
68451df0accSAmit Shah port->inbuf = NULL;
68551df0accSAmit Shah
68651df0accSAmit Shah if (add_inbuf(port->in_vq, buf) < 0)
68751df0accSAmit Shah dev_warn(port->dev, "failed add_buf\n");
68851df0accSAmit Shah
68951df0accSAmit Shah spin_unlock_irqrestore(&port->inbuf_lock, flags);
69051df0accSAmit Shah }
69151df0accSAmit Shah /* Return the number of bytes actually copied */
69251df0accSAmit Shah return out_count;
69351df0accSAmit Shah }
69451df0accSAmit Shah
69551df0accSAmit Shah /* The condition that must be true for polling to end */
will_read_block(struct port * port)69651df0accSAmit Shah static bool will_read_block(struct port *port)
69751df0accSAmit Shah {
69851df0accSAmit Shah if (!port->guest_connected) {
69951df0accSAmit Shah /* Port got hot-unplugged. Let's exit. */
70051df0accSAmit Shah return false;
70151df0accSAmit Shah }
70251df0accSAmit Shah return !port_has_data(port) && port->host_connected;
70351df0accSAmit Shah }
70451df0accSAmit Shah
will_write_block(struct port * port)70551df0accSAmit Shah static bool will_write_block(struct port *port)
70651df0accSAmit Shah {
70751df0accSAmit Shah bool ret;
70851df0accSAmit Shah
70951df0accSAmit Shah if (!port->guest_connected) {
71051df0accSAmit Shah /* Port got hot-unplugged. Let's exit. */
71151df0accSAmit Shah return false;
71251df0accSAmit Shah }
71351df0accSAmit Shah if (!port->host_connected)
71451df0accSAmit Shah return true;
71551df0accSAmit Shah
71651df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
71751df0accSAmit Shah /*
71851df0accSAmit Shah * Check if the Host has consumed any buffers since we last
71951df0accSAmit Shah * sent data (this is only applicable for nonblocking ports).
72051df0accSAmit Shah */
72151df0accSAmit Shah reclaim_consumed_buffers(port);
72251df0accSAmit Shah ret = port->outvq_full;
72351df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
72451df0accSAmit Shah
72551df0accSAmit Shah return ret;
72651df0accSAmit Shah }
72751df0accSAmit Shah
port_fops_read(struct file * filp,char __user * ubuf,size_t count,loff_t * offp)72851df0accSAmit Shah static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
72951df0accSAmit Shah size_t count, loff_t *offp)
73051df0accSAmit Shah {
73151df0accSAmit Shah struct port *port;
73251df0accSAmit Shah ssize_t ret;
73351df0accSAmit Shah
73451df0accSAmit Shah port = filp->private_data;
73551df0accSAmit Shah
73696f97a83SAmit Shah /* Port is hot-unplugged. */
73796f97a83SAmit Shah if (!port->guest_connected)
73896f97a83SAmit Shah return -ENODEV;
73996f97a83SAmit Shah
74051df0accSAmit Shah if (!port_has_data(port)) {
74151df0accSAmit Shah /*
74251df0accSAmit Shah * If nothing's connected on the host just return 0 in
74351df0accSAmit Shah * case of list_empty; this tells the userspace app
74451df0accSAmit Shah * that there's no connection
74551df0accSAmit Shah */
74651df0accSAmit Shah if (!port->host_connected)
74751df0accSAmit Shah return 0;
74851df0accSAmit Shah if (filp->f_flags & O_NONBLOCK)
74951df0accSAmit Shah return -EAGAIN;
75051df0accSAmit Shah
751a08fa92dSAmit Shah ret = wait_event_freezable(port->waitqueue,
75251df0accSAmit Shah !will_read_block(port));
75351df0accSAmit Shah if (ret < 0)
75451df0accSAmit Shah return ret;
75551df0accSAmit Shah }
75696f97a83SAmit Shah /* Port got hot-unplugged while we were waiting above. */
75751df0accSAmit Shah if (!port->guest_connected)
75851df0accSAmit Shah return -ENODEV;
75951df0accSAmit Shah /*
76051df0accSAmit Shah * We could've received a disconnection message while we were
76151df0accSAmit Shah * waiting for more data.
76251df0accSAmit Shah *
76351df0accSAmit Shah * This check is not clubbed in the if() statement above as we
76451df0accSAmit Shah * might receive some data as well as the host could get
76551df0accSAmit Shah * disconnected after we got woken up from our wait. So we
76651df0accSAmit Shah * really want to give off whatever data we have and only then
76751df0accSAmit Shah * check for host_connected.
76851df0accSAmit Shah */
76951df0accSAmit Shah if (!port_has_data(port) && !port->host_connected)
77051df0accSAmit Shah return 0;
77151df0accSAmit Shah
77251df0accSAmit Shah return fill_readbuf(port, ubuf, count, true);
77351df0accSAmit Shah }
77451df0accSAmit Shah
wait_port_writable(struct port * port,bool nonblock)775efe75d24SMasami Hiramatsu static int wait_port_writable(struct port *port, bool nonblock)
776efe75d24SMasami Hiramatsu {
777efe75d24SMasami Hiramatsu int ret;
778efe75d24SMasami Hiramatsu
779efe75d24SMasami Hiramatsu if (will_write_block(port)) {
780efe75d24SMasami Hiramatsu if (nonblock)
781efe75d24SMasami Hiramatsu return -EAGAIN;
782efe75d24SMasami Hiramatsu
783efe75d24SMasami Hiramatsu ret = wait_event_freezable(port->waitqueue,
784efe75d24SMasami Hiramatsu !will_write_block(port));
785efe75d24SMasami Hiramatsu if (ret < 0)
786efe75d24SMasami Hiramatsu return ret;
787efe75d24SMasami Hiramatsu }
788efe75d24SMasami Hiramatsu /* Port got hot-unplugged. */
789efe75d24SMasami Hiramatsu if (!port->guest_connected)
790efe75d24SMasami Hiramatsu return -ENODEV;
791efe75d24SMasami Hiramatsu
792efe75d24SMasami Hiramatsu return 0;
793efe75d24SMasami Hiramatsu }
794efe75d24SMasami Hiramatsu
port_fops_write(struct file * filp,const char __user * ubuf,size_t count,loff_t * offp)79551df0accSAmit Shah static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
79651df0accSAmit Shah size_t count, loff_t *offp)
79751df0accSAmit Shah {
79851df0accSAmit Shah struct port *port;
799276a3e95SSjur Brændeland struct port_buffer *buf;
80051df0accSAmit Shah ssize_t ret;
80151df0accSAmit Shah bool nonblock;
802276a3e95SSjur Brændeland struct scatterlist sg[1];
80351df0accSAmit Shah
80451df0accSAmit Shah /* Userspace could be out to fool us */
80551df0accSAmit Shah if (!count)
80651df0accSAmit Shah return 0;
80751df0accSAmit Shah
80851df0accSAmit Shah port = filp->private_data;
80951df0accSAmit Shah
81051df0accSAmit Shah nonblock = filp->f_flags & O_NONBLOCK;
81151df0accSAmit Shah
812efe75d24SMasami Hiramatsu ret = wait_port_writable(port, nonblock);
81351df0accSAmit Shah if (ret < 0)
81451df0accSAmit Shah return ret;
81551df0accSAmit Shah
81651df0accSAmit Shah count = min((size_t)(32 * 1024), count);
81751df0accSAmit Shah
8182855b335SMichael S. Tsirkin buf = alloc_buf(port->portdev->vdev, count, 0);
81951df0accSAmit Shah if (!buf)
82051df0accSAmit Shah return -ENOMEM;
82151df0accSAmit Shah
822276a3e95SSjur Brændeland ret = copy_from_user(buf->buf, ubuf, count);
82351df0accSAmit Shah if (ret) {
82451df0accSAmit Shah ret = -EFAULT;
82551df0accSAmit Shah goto free_buf;
82651df0accSAmit Shah }
82751df0accSAmit Shah
82851df0accSAmit Shah /*
82951df0accSAmit Shah * We now ask send_buf() to not spin for generic ports -- we
83051df0accSAmit Shah * can re-use the same code path that non-blocking file
83151df0accSAmit Shah * descriptors take for blocking file descriptors since the
83251df0accSAmit Shah * wait is already done and we're certain the write will go
83351df0accSAmit Shah * through to the host.
83451df0accSAmit Shah */
83551df0accSAmit Shah nonblock = true;
836276a3e95SSjur Brændeland sg_init_one(sg, buf->buf, count);
837276a3e95SSjur Brændeland ret = __send_to_port(port, sg, 1, count, buf, nonblock);
83851df0accSAmit Shah
83951df0accSAmit Shah if (nonblock && ret > 0)
84051df0accSAmit Shah goto out;
84151df0accSAmit Shah
84251df0accSAmit Shah free_buf:
8431b637046SSjur Brændeland free_buf(buf, true);
84451df0accSAmit Shah out:
84551df0accSAmit Shah return ret;
84651df0accSAmit Shah }
84751df0accSAmit Shah
848eb5e89fcSMasami Hiramatsu struct sg_list {
849eb5e89fcSMasami Hiramatsu unsigned int n;
8508ca84a50SMasami Hiramatsu unsigned int size;
851eb5e89fcSMasami Hiramatsu size_t len;
852eb5e89fcSMasami Hiramatsu struct scatterlist *sg;
853eb5e89fcSMasami Hiramatsu };
854eb5e89fcSMasami Hiramatsu
pipe_to_sg(struct pipe_inode_info * pipe,struct pipe_buffer * buf,struct splice_desc * sd)855eb5e89fcSMasami Hiramatsu static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
856eb5e89fcSMasami Hiramatsu struct splice_desc *sd)
857eb5e89fcSMasami Hiramatsu {
858eb5e89fcSMasami Hiramatsu struct sg_list *sgl = sd->u.data;
859ec8fc870SMasami Hiramatsu unsigned int offset, len;
860eb5e89fcSMasami Hiramatsu
8618ca84a50SMasami Hiramatsu if (sgl->n == sgl->size)
862eb5e89fcSMasami Hiramatsu return 0;
863eb5e89fcSMasami Hiramatsu
864eb5e89fcSMasami Hiramatsu /* Try lock this page */
865c928f642SChristoph Hellwig if (pipe_buf_try_steal(pipe, buf)) {
866eb5e89fcSMasami Hiramatsu /* Get reference and unlock page for moving */
867eb5e89fcSMasami Hiramatsu get_page(buf->page);
868eb5e89fcSMasami Hiramatsu unlock_page(buf->page);
869eb5e89fcSMasami Hiramatsu
870eb5e89fcSMasami Hiramatsu len = min(buf->len, sd->len);
871eb5e89fcSMasami Hiramatsu sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
872ec8fc870SMasami Hiramatsu } else {
873ec8fc870SMasami Hiramatsu /* Failback to copying a page */
874ec8fc870SMasami Hiramatsu struct page *page = alloc_page(GFP_KERNEL);
875c9efe511SAl Viro char *src;
876ec8fc870SMasami Hiramatsu
877ec8fc870SMasami Hiramatsu if (!page)
878ec8fc870SMasami Hiramatsu return -ENOMEM;
879ec8fc870SMasami Hiramatsu
880ec8fc870SMasami Hiramatsu offset = sd->pos & ~PAGE_MASK;
881ec8fc870SMasami Hiramatsu
882ec8fc870SMasami Hiramatsu len = sd->len;
883ec8fc870SMasami Hiramatsu if (len + offset > PAGE_SIZE)
884ec8fc870SMasami Hiramatsu len = PAGE_SIZE - offset;
885ec8fc870SMasami Hiramatsu
886fbb32750SAl Viro src = kmap_atomic(buf->page);
887c9efe511SAl Viro memcpy(page_address(page) + offset, src + buf->offset, len);
888fbb32750SAl Viro kunmap_atomic(src);
889ec8fc870SMasami Hiramatsu
890ec8fc870SMasami Hiramatsu sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
891ec8fc870SMasami Hiramatsu }
892eb5e89fcSMasami Hiramatsu sgl->n++;
893eb5e89fcSMasami Hiramatsu sgl->len += len;
894eb5e89fcSMasami Hiramatsu
895eb5e89fcSMasami Hiramatsu return len;
896eb5e89fcSMasami Hiramatsu }
897eb5e89fcSMasami Hiramatsu
898eb5e89fcSMasami 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)899eb5e89fcSMasami Hiramatsu static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
900eb5e89fcSMasami Hiramatsu struct file *filp, loff_t *ppos,
901eb5e89fcSMasami Hiramatsu size_t len, unsigned int flags)
902eb5e89fcSMasami Hiramatsu {
903eb5e89fcSMasami Hiramatsu struct port *port = filp->private_data;
904eb5e89fcSMasami Hiramatsu struct sg_list sgl;
905eb5e89fcSMasami Hiramatsu ssize_t ret;
906276a3e95SSjur Brændeland struct port_buffer *buf;
907eb5e89fcSMasami Hiramatsu struct splice_desc sd = {
908eb5e89fcSMasami Hiramatsu .total_len = len,
909eb5e89fcSMasami Hiramatsu .flags = flags,
910eb5e89fcSMasami Hiramatsu .pos = *ppos,
911eb5e89fcSMasami Hiramatsu .u.data = &sgl,
912eb5e89fcSMasami Hiramatsu };
9138cefc107SDavid Howells unsigned int occupancy;
914eb5e89fcSMasami Hiramatsu
9151b637046SSjur Brændeland /*
9161b637046SSjur Brændeland * Rproc_serial does not yet support splice. To support splice
9171b637046SSjur Brændeland * pipe_to_sg() must allocate dma-buffers and copy content from
9181b637046SSjur Brændeland * regular pages to dma pages. And alloc_buf and free_buf must
9191b637046SSjur Brændeland * support allocating and freeing such a list of dma-buffers.
9201b637046SSjur Brændeland */
9211b637046SSjur Brændeland if (is_rproc_serial(port->out_vq->vdev))
9221b637046SSjur Brændeland return -EINVAL;
9231b637046SSjur Brændeland
9242b4fbf02SYoshihiro YUNOMAE pipe_lock(pipe);
9252b4fbf02SYoshihiro YUNOMAE ret = 0;
9268cefc107SDavid Howells if (pipe_empty(pipe->head, pipe->tail))
9272b4fbf02SYoshihiro YUNOMAE goto error_out;
92868c034feSYoshihiro YUNOMAE
929efe75d24SMasami Hiramatsu ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
930efe75d24SMasami Hiramatsu if (ret < 0)
9312b4fbf02SYoshihiro YUNOMAE goto error_out;
932efe75d24SMasami Hiramatsu
9338cefc107SDavid Howells occupancy = pipe_occupancy(pipe->head, pipe->tail);
9348cefc107SDavid Howells buf = alloc_buf(port->portdev->vdev, 0, occupancy);
9358cefc107SDavid Howells
9362b4fbf02SYoshihiro YUNOMAE if (!buf) {
9372b4fbf02SYoshihiro YUNOMAE ret = -ENOMEM;
9382b4fbf02SYoshihiro YUNOMAE goto error_out;
9392b4fbf02SYoshihiro YUNOMAE }
940276a3e95SSjur Brændeland
941eb5e89fcSMasami Hiramatsu sgl.n = 0;
942eb5e89fcSMasami Hiramatsu sgl.len = 0;
9438cefc107SDavid Howells sgl.size = occupancy;
944276a3e95SSjur Brændeland sgl.sg = buf->sg;
9458ca84a50SMasami Hiramatsu sg_init_table(sgl.sg, sgl.size);
946eb5e89fcSMasami Hiramatsu ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
9472b4fbf02SYoshihiro YUNOMAE pipe_unlock(pipe);
948eb5e89fcSMasami Hiramatsu if (likely(ret > 0))
949276a3e95SSjur Brændeland ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
950eb5e89fcSMasami Hiramatsu
951fe529537SSjur Brændeland if (unlikely(ret <= 0))
9521b637046SSjur Brændeland free_buf(buf, true);
953eb5e89fcSMasami Hiramatsu return ret;
9542b4fbf02SYoshihiro YUNOMAE
9552b4fbf02SYoshihiro YUNOMAE error_out:
9562b4fbf02SYoshihiro YUNOMAE pipe_unlock(pipe);
9572b4fbf02SYoshihiro YUNOMAE return ret;
958eb5e89fcSMasami Hiramatsu }
959eb5e89fcSMasami Hiramatsu
port_fops_poll(struct file * filp,poll_table * wait)960afc9a42bSAl Viro static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
96151df0accSAmit Shah {
96251df0accSAmit Shah struct port *port;
963afc9a42bSAl Viro __poll_t ret;
96451df0accSAmit Shah
96551df0accSAmit Shah port = filp->private_data;
96651df0accSAmit Shah poll_wait(filp, &port->waitqueue, wait);
96751df0accSAmit Shah
96851df0accSAmit Shah if (!port->guest_connected) {
96951df0accSAmit Shah /* Port got unplugged */
970a9a08845SLinus Torvalds return EPOLLHUP;
97151df0accSAmit Shah }
97251df0accSAmit Shah ret = 0;
97351df0accSAmit Shah if (!will_read_block(port))
974a9a08845SLinus Torvalds ret |= EPOLLIN | EPOLLRDNORM;
97551df0accSAmit Shah if (!will_write_block(port))
976a9a08845SLinus Torvalds ret |= EPOLLOUT;
97751df0accSAmit Shah if (!port->host_connected)
978a9a08845SLinus Torvalds ret |= EPOLLHUP;
97951df0accSAmit Shah
98051df0accSAmit Shah return ret;
98151df0accSAmit Shah }
98251df0accSAmit Shah
98351df0accSAmit Shah static void remove_port(struct kref *kref);
98451df0accSAmit Shah
port_fops_release(struct inode * inode,struct file * filp)98551df0accSAmit Shah static int port_fops_release(struct inode *inode, struct file *filp)
98651df0accSAmit Shah {
98751df0accSAmit Shah struct port *port;
98851df0accSAmit Shah
98951df0accSAmit Shah port = filp->private_data;
99051df0accSAmit Shah
99151df0accSAmit Shah /* Notify host of port being closed */
99251df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
99351df0accSAmit Shah
99451df0accSAmit Shah spin_lock_irq(&port->inbuf_lock);
99551df0accSAmit Shah port->guest_connected = false;
99651df0accSAmit Shah
99751df0accSAmit Shah discard_port_data(port);
99851df0accSAmit Shah
99951df0accSAmit Shah spin_unlock_irq(&port->inbuf_lock);
100051df0accSAmit Shah
100151df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
100251df0accSAmit Shah reclaim_consumed_buffers(port);
100351df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
100451df0accSAmit Shah
10051b637046SSjur Brændeland reclaim_dma_bufs();
100651df0accSAmit Shah /*
100751df0accSAmit Shah * Locks aren't necessary here as a port can't be opened after
100851df0accSAmit Shah * unplug, and if a port isn't unplugged, a kref would already
100951df0accSAmit Shah * exist for the port. Plus, taking ports_lock here would
101051df0accSAmit Shah * create a dependency on other locks taken by functions
101151df0accSAmit Shah * inside remove_port if we're the last holder of the port,
101251df0accSAmit Shah * creating many problems.
101351df0accSAmit Shah */
101451df0accSAmit Shah kref_put(&port->kref, remove_port);
101551df0accSAmit Shah
101651df0accSAmit Shah return 0;
101751df0accSAmit Shah }
101851df0accSAmit Shah
port_fops_open(struct inode * inode,struct file * filp)101951df0accSAmit Shah static int port_fops_open(struct inode *inode, struct file *filp)
102051df0accSAmit Shah {
102151df0accSAmit Shah struct cdev *cdev = inode->i_cdev;
102251df0accSAmit Shah struct port *port;
102351df0accSAmit Shah int ret;
102451df0accSAmit Shah
1025057b82beSAmit Shah /* We get the port with a kref here */
102651df0accSAmit Shah port = find_port_by_devt(cdev->dev);
1027671bdea2SAmit Shah if (!port) {
1028671bdea2SAmit Shah /* Port was unplugged before we could proceed */
1029671bdea2SAmit Shah return -ENXIO;
1030671bdea2SAmit Shah }
103151df0accSAmit Shah filp->private_data = port;
103251df0accSAmit Shah
103351df0accSAmit Shah /*
103451df0accSAmit Shah * Don't allow opening of console port devices -- that's done
103551df0accSAmit Shah * via /dev/hvc
103651df0accSAmit Shah */
103751df0accSAmit Shah if (is_console_port(port)) {
103851df0accSAmit Shah ret = -ENXIO;
103951df0accSAmit Shah goto out;
104051df0accSAmit Shah }
104151df0accSAmit Shah
104251df0accSAmit Shah /* Allow only one process to open a particular port at a time */
104351df0accSAmit Shah spin_lock_irq(&port->inbuf_lock);
104451df0accSAmit Shah if (port->guest_connected) {
104551df0accSAmit Shah spin_unlock_irq(&port->inbuf_lock);
104674ff582cSAmit Shah ret = -EBUSY;
104751df0accSAmit Shah goto out;
104851df0accSAmit Shah }
104951df0accSAmit Shah
105051df0accSAmit Shah port->guest_connected = true;
105151df0accSAmit Shah spin_unlock_irq(&port->inbuf_lock);
105251df0accSAmit Shah
105351df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
105451df0accSAmit Shah /*
105551df0accSAmit Shah * There might be a chance that we missed reclaiming a few
105651df0accSAmit Shah * buffers in the window of the port getting previously closed
105751df0accSAmit Shah * and opening now.
105851df0accSAmit Shah */
105951df0accSAmit Shah reclaim_consumed_buffers(port);
106051df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
106151df0accSAmit Shah
106251df0accSAmit Shah nonseekable_open(inode, filp);
106351df0accSAmit Shah
106451df0accSAmit Shah /* Notify host of port being opened */
106551df0accSAmit Shah send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
106651df0accSAmit Shah
106751df0accSAmit Shah return 0;
106851df0accSAmit Shah out:
106951df0accSAmit Shah kref_put(&port->kref, remove_port);
107051df0accSAmit Shah return ret;
107151df0accSAmit Shah }
107251df0accSAmit Shah
port_fops_fasync(int fd,struct file * filp,int mode)107351df0accSAmit Shah static int port_fops_fasync(int fd, struct file *filp, int mode)
107451df0accSAmit Shah {
107551df0accSAmit Shah struct port *port;
107651df0accSAmit Shah
107751df0accSAmit Shah port = filp->private_data;
107851df0accSAmit Shah return fasync_helper(fd, filp, mode, &port->async_queue);
107951df0accSAmit Shah }
108051df0accSAmit Shah
108151df0accSAmit Shah /*
108251df0accSAmit Shah * The file operations that we support: programs in the guest can open
108351df0accSAmit Shah * a console device, read from it, write to it, poll for data and
108451df0accSAmit Shah * close it. The devices are at
108551df0accSAmit Shah * /dev/vport<device number>p<port number>
108651df0accSAmit Shah */
108751df0accSAmit Shah static const struct file_operations port_fops = {
108851df0accSAmit Shah .owner = THIS_MODULE,
108951df0accSAmit Shah .open = port_fops_open,
109051df0accSAmit Shah .read = port_fops_read,
109151df0accSAmit Shah .write = port_fops_write,
1092eb5e89fcSMasami Hiramatsu .splice_write = port_fops_splice_write,
109351df0accSAmit Shah .poll = port_fops_poll,
109451df0accSAmit Shah .release = port_fops_release,
109551df0accSAmit Shah .fasync = port_fops_fasync,
109651df0accSAmit Shah .llseek = no_llseek,
109751df0accSAmit Shah };
109851df0accSAmit Shah
109951df0accSAmit Shah /*
110051df0accSAmit Shah * The put_chars() callback is pretty straightforward.
110151df0accSAmit Shah *
110251df0accSAmit Shah * We turn the characters into a scatter-gather list, add it to the
110351df0accSAmit Shah * output queue and then kick the Host. Then we sit here waiting for
110451df0accSAmit Shah * it to finish: inefficient in theory, but in practice
1105ecda85e7SJuergen Gross * implementations will do it immediately.
110651df0accSAmit Shah */
put_chars(u32 vtermno,const u8 * buf,size_t count)1107*f32fcbedSJiri Slaby (SUSE) static ssize_t put_chars(u32 vtermno, const u8 *buf, size_t count)
110851df0accSAmit Shah {
110951df0accSAmit Shah struct port *port;
1110276a3e95SSjur Brændeland struct scatterlist sg[1];
1111c4baad50SOmar Sandoval void *data;
1112c4baad50SOmar Sandoval int ret;
111351df0accSAmit Shah
111451df0accSAmit Shah port = find_port_by_vtermno(vtermno);
111551df0accSAmit Shah if (!port)
111651df0accSAmit Shah return -EPIPE;
111751df0accSAmit Shah
1118c4baad50SOmar Sandoval data = kmemdup(buf, count, GFP_ATOMIC);
1119c4baad50SOmar Sandoval if (!data)
1120c4baad50SOmar Sandoval return -ENOMEM;
1121c4baad50SOmar Sandoval
1122c4baad50SOmar Sandoval sg_init_one(sg, data, count);
1123c4baad50SOmar Sandoval ret = __send_to_port(port, sg, 1, count, data, false);
1124c4baad50SOmar Sandoval kfree(data);
1125c4baad50SOmar Sandoval return ret;
112651df0accSAmit Shah }
112751df0accSAmit Shah
112851df0accSAmit Shah /*
112951df0accSAmit Shah * get_chars() is the callback from the hvc_console infrastructure
113051df0accSAmit Shah * when an interrupt is received.
113151df0accSAmit Shah *
113251df0accSAmit Shah * We call out to fill_readbuf that gets us the required data from the
113351df0accSAmit Shah * buffers that are queued up.
113451df0accSAmit Shah */
get_chars(u32 vtermno,u8 * buf,size_t count)1135*f32fcbedSJiri Slaby (SUSE) static ssize_t get_chars(u32 vtermno, u8 *buf, size_t count)
113651df0accSAmit Shah {
113751df0accSAmit Shah struct port *port;
113851df0accSAmit Shah
113951df0accSAmit Shah port = find_port_by_vtermno(vtermno);
114051df0accSAmit Shah if (!port)
114151df0accSAmit Shah return -EPIPE;
114251df0accSAmit Shah
114351df0accSAmit Shah /* If we don't have an input queue yet, we can't get input. */
114451df0accSAmit Shah BUG_ON(!port->in_vq);
114551df0accSAmit Shah
1146*f32fcbedSJiri Slaby (SUSE) return fill_readbuf(port, (__force u8 __user *)buf, count, false);
114751df0accSAmit Shah }
114851df0accSAmit Shah
resize_console(struct port * port)114951df0accSAmit Shah static void resize_console(struct port *port)
115051df0accSAmit Shah {
115151df0accSAmit Shah struct virtio_device *vdev;
115251df0accSAmit Shah
115351df0accSAmit Shah /* The port could have been hot-unplugged */
115451df0accSAmit Shah if (!port || !is_console_port(port))
115551df0accSAmit Shah return;
115651df0accSAmit Shah
115751df0accSAmit Shah vdev = port->portdev->vdev;
11581b637046SSjur Brændeland
11591b637046SSjur Brændeland /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
11601b637046SSjur Brændeland if (!is_rproc_serial(vdev) &&
11611b637046SSjur Brændeland virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
116251df0accSAmit Shah hvc_resize(port->cons.hvc, port->cons.ws);
116351df0accSAmit Shah }
116451df0accSAmit Shah
116551df0accSAmit Shah /* We set the configuration at this point, since we now have a tty */
notifier_add_vio(struct hvc_struct * hp,int data)116651df0accSAmit Shah static int notifier_add_vio(struct hvc_struct *hp, int data)
116751df0accSAmit Shah {
116851df0accSAmit Shah struct port *port;
116951df0accSAmit Shah
117051df0accSAmit Shah port = find_port_by_vtermno(hp->vtermno);
117151df0accSAmit Shah if (!port)
117251df0accSAmit Shah return -EINVAL;
117351df0accSAmit Shah
117451df0accSAmit Shah hp->irq_requested = 1;
117551df0accSAmit Shah resize_console(port);
117651df0accSAmit Shah
117751df0accSAmit Shah return 0;
117851df0accSAmit Shah }
117951df0accSAmit Shah
notifier_del_vio(struct hvc_struct * hp,int data)118051df0accSAmit Shah static void notifier_del_vio(struct hvc_struct *hp, int data)
118151df0accSAmit Shah {
118251df0accSAmit Shah hp->irq_requested = 0;
118351df0accSAmit Shah }
118451df0accSAmit Shah
118551df0accSAmit Shah /* The operations for console ports. */
118651df0accSAmit Shah static const struct hv_ops hv_ops = {
118751df0accSAmit Shah .get_chars = get_chars,
118851df0accSAmit Shah .put_chars = put_chars,
118951df0accSAmit Shah .notifier_add = notifier_add_vio,
119051df0accSAmit Shah .notifier_del = notifier_del_vio,
119151df0accSAmit Shah .notifier_hangup = notifier_del_vio,
119251df0accSAmit Shah };
119351df0accSAmit Shah
init_port_console(struct port * port)11943826835aSWei Yongjun static int init_port_console(struct port *port)
119551df0accSAmit Shah {
119651df0accSAmit Shah int ret;
119751df0accSAmit Shah
119851df0accSAmit Shah /*
119951df0accSAmit Shah * The Host's telling us this port is a console port. Hook it
120051df0accSAmit Shah * up with an hvc console.
120151df0accSAmit Shah *
120251df0accSAmit Shah * To set up and manage our virtual console, we call
120351df0accSAmit Shah * hvc_alloc().
120451df0accSAmit Shah *
120551df0accSAmit Shah * The first argument of hvc_alloc() is the virtual console
120651df0accSAmit Shah * number. The second argument is the parameter for the
120751df0accSAmit Shah * notification mechanism (like irq number). We currently
120851df0accSAmit Shah * leave this as zero, virtqueues have implicit notifications.
120951df0accSAmit Shah *
121051df0accSAmit Shah * The third argument is a "struct hv_ops" containing the
121151df0accSAmit Shah * put_chars() get_chars(), notifier_add() and notifier_del()
121251df0accSAmit Shah * pointers. The final argument is the output buffer size: we
121351df0accSAmit Shah * can do any size, so we put PAGE_SIZE here.
121451df0accSAmit Shah */
1215e6278a54SCédric Le Goater ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL);
1216e6278a54SCédric Le Goater if (ret < 0)
1217e6278a54SCédric Le Goater return ret;
121851df0accSAmit Shah
1219e6278a54SCédric Le Goater port->cons.vtermno = ret;
122051df0accSAmit Shah port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
122151df0accSAmit Shah if (IS_ERR(port->cons.hvc)) {
122251df0accSAmit Shah ret = PTR_ERR(port->cons.hvc);
122351df0accSAmit Shah dev_err(port->dev,
122451df0accSAmit Shah "error %d allocating hvc for port\n", ret);
122551df0accSAmit Shah port->cons.hvc = NULL;
1226e6278a54SCédric Le Goater ida_free(&vtermno_ida, port->cons.vtermno);
122751df0accSAmit Shah return ret;
122851df0accSAmit Shah }
122951df0accSAmit Shah spin_lock_irq(&pdrvdata_lock);
123051df0accSAmit Shah list_add_tail(&port->cons.list, &pdrvdata.consoles);
123151df0accSAmit Shah spin_unlock_irq(&pdrvdata_lock);
123251df0accSAmit Shah port->guest_connected = true;
123351df0accSAmit Shah
123451df0accSAmit Shah /* Notify host of port being opened */
123551df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
123651df0accSAmit Shah
123751df0accSAmit Shah return 0;
123851df0accSAmit Shah }
123951df0accSAmit Shah
show_port_name(struct device * dev,struct device_attribute * attr,char * buffer)124051df0accSAmit Shah static ssize_t show_port_name(struct device *dev,
124151df0accSAmit Shah struct device_attribute *attr, char *buffer)
124251df0accSAmit Shah {
124351df0accSAmit Shah struct port *port;
124451df0accSAmit Shah
124551df0accSAmit Shah port = dev_get_drvdata(dev);
124651df0accSAmit Shah
124751df0accSAmit Shah return sprintf(buffer, "%s\n", port->name);
124851df0accSAmit Shah }
124951df0accSAmit Shah
125051df0accSAmit Shah static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
125151df0accSAmit Shah
125251df0accSAmit Shah static struct attribute *port_sysfs_entries[] = {
125351df0accSAmit Shah &dev_attr_name.attr,
125451df0accSAmit Shah NULL
125551df0accSAmit Shah };
125651df0accSAmit Shah
1257ac317e27SArvind Yadav static const struct attribute_group port_attribute_group = {
125851df0accSAmit Shah .name = NULL, /* put in device directory */
125951df0accSAmit Shah .attrs = port_sysfs_entries,
126051df0accSAmit Shah };
126151df0accSAmit Shah
port_debugfs_show(struct seq_file * s,void * data)1262ddfa728aSYangtao Li static int port_debugfs_show(struct seq_file *s, void *data)
126351df0accSAmit Shah {
12648d62fe94STuomas Tynkkynen struct port *port = s->private;
126551df0accSAmit Shah
12668d62fe94STuomas Tynkkynen seq_printf(s, "name: %s\n", port->name ? port->name : "");
12678d62fe94STuomas Tynkkynen seq_printf(s, "guest_connected: %d\n", port->guest_connected);
12688d62fe94STuomas Tynkkynen seq_printf(s, "host_connected: %d\n", port->host_connected);
12698d62fe94STuomas Tynkkynen seq_printf(s, "outvq_full: %d\n", port->outvq_full);
12708d62fe94STuomas Tynkkynen seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
12718d62fe94STuomas Tynkkynen seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
12728d62fe94STuomas Tynkkynen seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
12738d62fe94STuomas Tynkkynen seq_printf(s, "is_console: %s\n",
127451df0accSAmit Shah is_console_port(port) ? "yes" : "no");
12758d62fe94STuomas Tynkkynen seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
127651df0accSAmit Shah
12778d62fe94STuomas Tynkkynen return 0;
12788d62fe94STuomas Tynkkynen }
12798d62fe94STuomas Tynkkynen
1280ddfa728aSYangtao Li DEFINE_SHOW_ATTRIBUTE(port_debugfs);
128151df0accSAmit Shah
set_console_size(struct port * port,u16 rows,u16 cols)128251df0accSAmit Shah static void set_console_size(struct port *port, u16 rows, u16 cols)
128351df0accSAmit Shah {
128451df0accSAmit Shah if (!port || !is_console_port(port))
128551df0accSAmit Shah return;
128651df0accSAmit Shah
128751df0accSAmit Shah port->cons.ws.ws_row = rows;
128851df0accSAmit Shah port->cons.ws.ws_col = cols;
128951df0accSAmit Shah }
129051df0accSAmit Shah
fill_queue(struct virtqueue * vq,spinlock_t * lock)1291d791cfcbSLaurent Vivier static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
129251df0accSAmit Shah {
129351df0accSAmit Shah struct port_buffer *buf;
1294d791cfcbSLaurent Vivier int nr_added_bufs;
129551df0accSAmit Shah int ret;
129651df0accSAmit Shah
129751df0accSAmit Shah nr_added_bufs = 0;
129851df0accSAmit Shah do {
12992855b335SMichael S. Tsirkin buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
130051df0accSAmit Shah if (!buf)
1301d791cfcbSLaurent Vivier return -ENOMEM;
130251df0accSAmit Shah
130351df0accSAmit Shah spin_lock_irq(lock);
130451df0accSAmit Shah ret = add_inbuf(vq, buf);
130551df0accSAmit Shah if (ret < 0) {
130651df0accSAmit Shah spin_unlock_irq(lock);
13071b637046SSjur Brændeland free_buf(buf, true);
1308d791cfcbSLaurent Vivier return ret;
130951df0accSAmit Shah }
131051df0accSAmit Shah nr_added_bufs++;
131151df0accSAmit Shah spin_unlock_irq(lock);
131251df0accSAmit Shah } while (ret > 0);
131351df0accSAmit Shah
131451df0accSAmit Shah return nr_added_bufs;
131551df0accSAmit Shah }
131651df0accSAmit Shah
send_sigio_to_port(struct port * port)131751df0accSAmit Shah static void send_sigio_to_port(struct port *port)
131851df0accSAmit Shah {
131951df0accSAmit Shah if (port->async_queue && port->guest_connected)
132051df0accSAmit Shah kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
132151df0accSAmit Shah }
132251df0accSAmit Shah
add_port(struct ports_device * portdev,u32 id)132351df0accSAmit Shah static int add_port(struct ports_device *portdev, u32 id)
132451df0accSAmit Shah {
132551df0accSAmit Shah char debugfs_name[16];
132651df0accSAmit Shah struct port *port;
132751df0accSAmit Shah dev_t devt;
132851df0accSAmit Shah int err;
132951df0accSAmit Shah
133051df0accSAmit Shah port = kmalloc(sizeof(*port), GFP_KERNEL);
133151df0accSAmit Shah if (!port) {
133251df0accSAmit Shah err = -ENOMEM;
133351df0accSAmit Shah goto fail;
133451df0accSAmit Shah }
133551df0accSAmit Shah kref_init(&port->kref);
133651df0accSAmit Shah
133751df0accSAmit Shah port->portdev = portdev;
133851df0accSAmit Shah port->id = id;
133951df0accSAmit Shah
134051df0accSAmit Shah port->name = NULL;
134151df0accSAmit Shah port->inbuf = NULL;
134251df0accSAmit Shah port->cons.hvc = NULL;
134351df0accSAmit Shah port->async_queue = NULL;
134451df0accSAmit Shah
134551df0accSAmit Shah port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
13464b0a2c5fSPankaj Gupta port->cons.vtermno = 0;
134751df0accSAmit Shah
134851df0accSAmit Shah port->host_connected = port->guest_connected = false;
134917e5b4f2SAmit Shah port->stats = (struct port_stats) { 0 };
135051df0accSAmit Shah
135151df0accSAmit Shah port->outvq_full = false;
135251df0accSAmit Shah
135351df0accSAmit Shah port->in_vq = portdev->in_vqs[port->id];
135451df0accSAmit Shah port->out_vq = portdev->out_vqs[port->id];
135551df0accSAmit Shah
135651df0accSAmit Shah port->cdev = cdev_alloc();
135751df0accSAmit Shah if (!port->cdev) {
135851df0accSAmit Shah dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
135951df0accSAmit Shah err = -ENOMEM;
136051df0accSAmit Shah goto free_port;
136151df0accSAmit Shah }
136251df0accSAmit Shah port->cdev->ops = &port_fops;
136351df0accSAmit Shah
136451df0accSAmit Shah devt = MKDEV(portdev->chr_major, id);
136551df0accSAmit Shah err = cdev_add(port->cdev, devt, 1);
136651df0accSAmit Shah if (err < 0) {
136751df0accSAmit Shah dev_err(&port->portdev->vdev->dev,
136851df0accSAmit Shah "Error %d adding cdev for port %u\n", err, id);
136951df0accSAmit Shah goto free_cdev;
137051df0accSAmit Shah }
137111680fdfSIvan Orlov port->dev = device_create(&port_class, &port->portdev->vdev->dev,
137251df0accSAmit Shah devt, port, "vport%up%u",
1373dc18f080SSjur Brændeland port->portdev->vdev->index, id);
137451df0accSAmit Shah if (IS_ERR(port->dev)) {
137551df0accSAmit Shah err = PTR_ERR(port->dev);
137651df0accSAmit Shah dev_err(&port->portdev->vdev->dev,
137751df0accSAmit Shah "Error %d creating device for port %u\n",
137851df0accSAmit Shah err, id);
137951df0accSAmit Shah goto free_cdev;
138051df0accSAmit Shah }
138151df0accSAmit Shah
138251df0accSAmit Shah spin_lock_init(&port->inbuf_lock);
138351df0accSAmit Shah spin_lock_init(&port->outvq_lock);
138451df0accSAmit Shah init_waitqueue_head(&port->waitqueue);
138551df0accSAmit Shah
1386d791cfcbSLaurent Vivier /* We can safely ignore ENOSPC because it means
1387d791cfcbSLaurent Vivier * the queue already has buffers. Buffers are removed
1388d791cfcbSLaurent Vivier * only by virtcons_remove(), not by unplug_port()
1389d791cfcbSLaurent Vivier */
1390d791cfcbSLaurent Vivier err = fill_queue(port->in_vq, &port->inbuf_lock);
1391d791cfcbSLaurent Vivier if (err < 0 && err != -ENOSPC) {
139251df0accSAmit Shah dev_err(port->dev, "Error allocating inbufs\n");
139351df0accSAmit Shah goto free_device;
139451df0accSAmit Shah }
139551df0accSAmit Shah
13961b637046SSjur Brændeland if (is_rproc_serial(port->portdev->vdev))
139751df0accSAmit Shah /*
13981b637046SSjur Brændeland * For rproc_serial assume remote processor is connected.
13991b637046SSjur Brændeland * rproc_serial does not want the console port, only
14001b637046SSjur Brændeland * the generic port implementation.
140151df0accSAmit Shah */
1402aabd6a8fSSjur Brændeland port->host_connected = true;
14031b637046SSjur Brændeland else if (!use_multiport(port->portdev)) {
14041b637046SSjur Brændeland /*
14051b637046SSjur Brændeland * If we're not using multiport support,
14061b637046SSjur Brændeland * this has to be a console port.
14071b637046SSjur Brændeland */
140851df0accSAmit Shah err = init_port_console(port);
140951df0accSAmit Shah if (err)
141051df0accSAmit Shah goto free_inbufs;
141151df0accSAmit Shah }
141251df0accSAmit Shah
141351df0accSAmit Shah spin_lock_irq(&portdev->ports_lock);
141451df0accSAmit Shah list_add_tail(&port->list, &port->portdev->ports);
141551df0accSAmit Shah spin_unlock_irq(&portdev->ports_lock);
141651df0accSAmit Shah
141751df0accSAmit Shah /*
141851df0accSAmit Shah * Tell the Host we're set so that it can send us various
141951df0accSAmit Shah * configuration parameters for this port (eg, port name,
142051df0accSAmit Shah * caching, whether this is a console port, etc.)
142151df0accSAmit Shah */
142251df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
142351df0accSAmit Shah
142451df0accSAmit Shah /*
142551df0accSAmit Shah * Finally, create the debugfs file that we can use to
142651df0accSAmit Shah * inspect a port's state at any time
142751df0accSAmit Shah */
1428db170068SDan Carpenter snprintf(debugfs_name, sizeof(debugfs_name), "vport%up%u",
1429dc18f080SSjur Brændeland port->portdev->vdev->index, id);
143051df0accSAmit Shah port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
143151df0accSAmit Shah pdrvdata.debugfs_dir,
1432fb11de92SGreg Kroah-Hartman port, &port_debugfs_fops);
143351df0accSAmit Shah return 0;
143451df0accSAmit Shah
143551df0accSAmit Shah free_inbufs:
143651df0accSAmit Shah free_device:
143711680fdfSIvan Orlov device_destroy(&port_class, port->dev->devt);
143851df0accSAmit Shah free_cdev:
143951df0accSAmit Shah cdev_del(port->cdev);
144051df0accSAmit Shah free_port:
144151df0accSAmit Shah kfree(port);
144251df0accSAmit Shah fail:
144351df0accSAmit Shah /* The host might want to notify management sw about port add failure */
144451df0accSAmit Shah __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
144551df0accSAmit Shah return err;
144651df0accSAmit Shah }
144751df0accSAmit Shah
144851df0accSAmit Shah /* No users remain, remove all port-specific data. */
remove_port(struct kref * kref)144951df0accSAmit Shah static void remove_port(struct kref *kref)
145051df0accSAmit Shah {
145151df0accSAmit Shah struct port *port;
145251df0accSAmit Shah
145351df0accSAmit Shah port = container_of(kref, struct port, kref);
145451df0accSAmit Shah
145551df0accSAmit Shah kfree(port);
145651df0accSAmit Shah }
145751df0accSAmit Shah
remove_port_data(struct port * port)1458a0e2dbfcSAmit Shah static void remove_port_data(struct port *port)
1459a0e2dbfcSAmit Shah {
1460c6017e79SAmit Shah spin_lock_irq(&port->inbuf_lock);
1461a0e2dbfcSAmit Shah /* Remove unused data this port might have received. */
1462a0e2dbfcSAmit Shah discard_port_data(port);
146334563769SMatt Redfearn spin_unlock_irq(&port->inbuf_lock);
1464a0e2dbfcSAmit Shah
1465c6017e79SAmit Shah spin_lock_irq(&port->outvq_lock);
1466c6017e79SAmit Shah reclaim_consumed_buffers(port);
146734563769SMatt Redfearn spin_unlock_irq(&port->outvq_lock);
1468a0e2dbfcSAmit Shah }
1469a0e2dbfcSAmit Shah
147051df0accSAmit Shah /*
147151df0accSAmit Shah * Port got unplugged. Remove port from portdev's list and drop the
147251df0accSAmit Shah * kref reference. If no userspace has this port opened, it will
147351df0accSAmit Shah * result in immediate removal the port.
147451df0accSAmit Shah */
unplug_port(struct port * port)147551df0accSAmit Shah static void unplug_port(struct port *port)
147651df0accSAmit Shah {
147751df0accSAmit Shah spin_lock_irq(&port->portdev->ports_lock);
147851df0accSAmit Shah list_del(&port->list);
147951df0accSAmit Shah spin_unlock_irq(&port->portdev->ports_lock);
148051df0accSAmit Shah
14815549fb25SAmit Shah spin_lock_irq(&port->inbuf_lock);
148251df0accSAmit Shah if (port->guest_connected) {
148351df0accSAmit Shah /* Let the app know the port is going down. */
148451df0accSAmit Shah send_sigio_to_port(port);
148592d34538SAmit Shah
148692d34538SAmit Shah /* Do this after sigio is actually sent */
148792d34538SAmit Shah port->guest_connected = false;
148892d34538SAmit Shah port->host_connected = false;
148992d34538SAmit Shah
149092d34538SAmit Shah wake_up_interruptible(&port->waitqueue);
149151df0accSAmit Shah }
14925549fb25SAmit Shah spin_unlock_irq(&port->inbuf_lock);
149351df0accSAmit Shah
149451df0accSAmit Shah if (is_console_port(port)) {
149551df0accSAmit Shah spin_lock_irq(&pdrvdata_lock);
149651df0accSAmit Shah list_del(&port->cons.list);
149751df0accSAmit Shah spin_unlock_irq(&pdrvdata_lock);
149851df0accSAmit Shah hvc_remove(port->cons.hvc);
1499e6278a54SCédric Le Goater ida_free(&vtermno_ida, port->cons.vtermno);
150051df0accSAmit Shah }
150151df0accSAmit Shah
1502a0e2dbfcSAmit Shah remove_port_data(port);
150351df0accSAmit Shah
150451df0accSAmit Shah /*
150551df0accSAmit Shah * We should just assume the device itself has gone off --
150651df0accSAmit Shah * else a close on an open port later will try to send out a
150751df0accSAmit Shah * control message.
150851df0accSAmit Shah */
150951df0accSAmit Shah port->portdev = NULL;
151051df0accSAmit Shah
1511ea3768b4SAmit Shah sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
151211680fdfSIvan Orlov device_destroy(&port_class, port->dev->devt);
1513ea3768b4SAmit Shah cdev_del(port->cdev);
1514ea3768b4SAmit Shah
1515ea3768b4SAmit Shah debugfs_remove(port->debugfs_file);
15163b868a40SAmit Shah kfree(port->name);
1517ea3768b4SAmit Shah
151851df0accSAmit Shah /*
151951df0accSAmit Shah * Locks around here are not necessary - a port can't be
152051df0accSAmit Shah * opened after we removed the port struct from ports_list
152151df0accSAmit Shah * above.
152251df0accSAmit Shah */
152351df0accSAmit Shah kref_put(&port->kref, remove_port);
152451df0accSAmit Shah }
152551df0accSAmit Shah
152651df0accSAmit 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)15271f0f9106SMichael S. Tsirkin static void handle_control_message(struct virtio_device *vdev,
15281f0f9106SMichael S. Tsirkin struct ports_device *portdev,
152951df0accSAmit Shah struct port_buffer *buf)
153051df0accSAmit Shah {
153151df0accSAmit Shah struct virtio_console_control *cpkt;
153251df0accSAmit Shah struct port *port;
153351df0accSAmit Shah size_t name_size;
153451df0accSAmit Shah int err;
153551df0accSAmit Shah
153651df0accSAmit Shah cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
153751df0accSAmit Shah
15381f0f9106SMichael S. Tsirkin port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
15391f0f9106SMichael S. Tsirkin if (!port &&
15401f0f9106SMichael S. Tsirkin cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
154151df0accSAmit Shah /* No valid header at start of buffer. Drop it. */
154251df0accSAmit Shah dev_dbg(&portdev->vdev->dev,
154351df0accSAmit Shah "Invalid index %u in control packet\n", cpkt->id);
154451df0accSAmit Shah return;
154551df0accSAmit Shah }
154651df0accSAmit Shah
15471f0f9106SMichael S. Tsirkin switch (virtio16_to_cpu(vdev, cpkt->event)) {
154851df0accSAmit Shah case VIRTIO_CONSOLE_PORT_ADD:
154951df0accSAmit Shah if (port) {
155051df0accSAmit Shah dev_dbg(&portdev->vdev->dev,
155151df0accSAmit Shah "Port %u already added\n", port->id);
155251df0accSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
155351df0accSAmit Shah break;
155451df0accSAmit Shah }
15551f0f9106SMichael S. Tsirkin if (virtio32_to_cpu(vdev, cpkt->id) >=
15567328fa64SMichael S. Tsirkin portdev->max_nr_ports) {
155751df0accSAmit Shah dev_warn(&portdev->vdev->dev,
15581f0f9106SMichael S. Tsirkin "Request for adding port with "
15591f0f9106SMichael S. Tsirkin "out-of-bound id %u, max. supported id: %u\n",
15607328fa64SMichael S. Tsirkin cpkt->id, portdev->max_nr_ports - 1);
156151df0accSAmit Shah break;
156251df0accSAmit Shah }
15631f0f9106SMichael S. Tsirkin add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
156451df0accSAmit Shah break;
156551df0accSAmit Shah case VIRTIO_CONSOLE_PORT_REMOVE:
156651df0accSAmit Shah unplug_port(port);
156751df0accSAmit Shah break;
156851df0accSAmit Shah case VIRTIO_CONSOLE_CONSOLE_PORT:
156951df0accSAmit Shah if (!cpkt->value)
157051df0accSAmit Shah break;
157151df0accSAmit Shah if (is_console_port(port))
157251df0accSAmit Shah break;
157351df0accSAmit Shah
157451df0accSAmit Shah init_port_console(port);
15755e38483bSChristian Borntraeger complete(&early_console_added);
157651df0accSAmit Shah /*
157751df0accSAmit Shah * Could remove the port here in case init fails - but
157851df0accSAmit Shah * have to notify the host first.
157951df0accSAmit Shah */
158051df0accSAmit Shah break;
158151df0accSAmit Shah case VIRTIO_CONSOLE_RESIZE: {
158251df0accSAmit Shah struct {
158351df0accSAmit Shah __u16 rows;
158451df0accSAmit Shah __u16 cols;
158551df0accSAmit Shah } size;
158651df0accSAmit Shah
158751df0accSAmit Shah if (!is_console_port(port))
158851df0accSAmit Shah break;
158951df0accSAmit Shah
159051df0accSAmit Shah memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
159151df0accSAmit Shah sizeof(size));
159251df0accSAmit Shah set_console_size(port, size.rows, size.cols);
159351df0accSAmit Shah
159451df0accSAmit Shah port->cons.hvc->irq_requested = 1;
159551df0accSAmit Shah resize_console(port);
159651df0accSAmit Shah break;
159751df0accSAmit Shah }
159851df0accSAmit Shah case VIRTIO_CONSOLE_PORT_OPEN:
15991f0f9106SMichael S. Tsirkin port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
160051df0accSAmit Shah wake_up_interruptible(&port->waitqueue);
160151df0accSAmit Shah /*
160251df0accSAmit Shah * If the host port got closed and the host had any
160351df0accSAmit Shah * unconsumed buffers, we'll be able to reclaim them
160451df0accSAmit Shah * now.
160551df0accSAmit Shah */
160651df0accSAmit Shah spin_lock_irq(&port->outvq_lock);
160751df0accSAmit Shah reclaim_consumed_buffers(port);
160851df0accSAmit Shah spin_unlock_irq(&port->outvq_lock);
160951df0accSAmit Shah
161051df0accSAmit Shah /*
161151df0accSAmit Shah * If the guest is connected, it'll be interested in
161251df0accSAmit Shah * knowing the host connection state changed.
161351df0accSAmit Shah */
1614314081f1SAmit Shah spin_lock_irq(&port->inbuf_lock);
161551df0accSAmit Shah send_sigio_to_port(port);
1616314081f1SAmit Shah spin_unlock_irq(&port->inbuf_lock);
161751df0accSAmit Shah break;
161851df0accSAmit Shah case VIRTIO_CONSOLE_PORT_NAME:
161951df0accSAmit Shah /*
1620291024efSAmit Shah * If we woke up after hibernation, we can get this
1621291024efSAmit Shah * again. Skip it in that case.
1622291024efSAmit Shah */
1623291024efSAmit Shah if (port->name)
1624291024efSAmit Shah break;
1625291024efSAmit Shah
1626291024efSAmit Shah /*
162751df0accSAmit Shah * Skip the size of the header and the cpkt to get the size
162851df0accSAmit Shah * of the name that was sent
162951df0accSAmit Shah */
163051df0accSAmit Shah name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
163151df0accSAmit Shah
163251df0accSAmit Shah port->name = kmalloc(name_size, GFP_KERNEL);
163351df0accSAmit Shah if (!port->name) {
163451df0accSAmit Shah dev_err(port->dev,
163551df0accSAmit Shah "Not enough space to store port name\n");
163651df0accSAmit Shah break;
163751df0accSAmit Shah }
1638c7109c72SBo Liu strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1639c7109c72SBo Liu name_size);
164051df0accSAmit Shah
164151df0accSAmit Shah /*
164251df0accSAmit Shah * Since we only have one sysfs attribute, 'name',
164351df0accSAmit Shah * create it only if we have a name for the port.
164451df0accSAmit Shah */
164551df0accSAmit Shah err = sysfs_create_group(&port->dev->kobj,
164651df0accSAmit Shah &port_attribute_group);
164751df0accSAmit Shah if (err) {
164851df0accSAmit Shah dev_err(port->dev,
164951df0accSAmit Shah "Error %d creating sysfs device attributes\n",
165051df0accSAmit Shah err);
165151df0accSAmit Shah } else {
165251df0accSAmit Shah /*
165351df0accSAmit Shah * Generate a udev event so that appropriate
165451df0accSAmit Shah * symlinks can be created based on udev
165551df0accSAmit Shah * rules.
165651df0accSAmit Shah */
165751df0accSAmit Shah kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
165851df0accSAmit Shah }
165951df0accSAmit Shah break;
166051df0accSAmit Shah }
166151df0accSAmit Shah }
166251df0accSAmit Shah
control_work_handler(struct work_struct * work)166351df0accSAmit Shah static void control_work_handler(struct work_struct *work)
166451df0accSAmit Shah {
166551df0accSAmit Shah struct ports_device *portdev;
166651df0accSAmit Shah struct virtqueue *vq;
166751df0accSAmit Shah struct port_buffer *buf;
166851df0accSAmit Shah unsigned int len;
166951df0accSAmit Shah
167051df0accSAmit Shah portdev = container_of(work, struct ports_device, control_work);
167151df0accSAmit Shah vq = portdev->c_ivq;
167251df0accSAmit Shah
1673165b1b8bSAmit Shah spin_lock(&portdev->c_ivq_lock);
167451df0accSAmit Shah while ((buf = virtqueue_get_buf(vq, &len))) {
1675165b1b8bSAmit Shah spin_unlock(&portdev->c_ivq_lock);
167651df0accSAmit Shah
1677d00d8da5SXie Yongji buf->len = min_t(size_t, len, buf->size);
167851df0accSAmit Shah buf->offset = 0;
167951df0accSAmit Shah
16801f0f9106SMichael S. Tsirkin handle_control_message(vq->vdev, portdev, buf);
168151df0accSAmit Shah
1682165b1b8bSAmit Shah spin_lock(&portdev->c_ivq_lock);
168351df0accSAmit Shah if (add_inbuf(portdev->c_ivq, buf) < 0) {
168451df0accSAmit Shah dev_warn(&portdev->vdev->dev,
168551df0accSAmit Shah "Error adding buffer to queue\n");
16861b637046SSjur Brændeland free_buf(buf, false);
168751df0accSAmit Shah }
168851df0accSAmit Shah }
1689165b1b8bSAmit Shah spin_unlock(&portdev->c_ivq_lock);
169051df0accSAmit Shah }
169151df0accSAmit Shah
flush_bufs(struct virtqueue * vq,bool can_sleep)1692a7a69ec0SMichael S. Tsirkin static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1693a7a69ec0SMichael S. Tsirkin {
1694a7a69ec0SMichael S. Tsirkin struct port_buffer *buf;
1695a7a69ec0SMichael S. Tsirkin unsigned int len;
1696a7a69ec0SMichael S. Tsirkin
1697a7a69ec0SMichael S. Tsirkin while ((buf = virtqueue_get_buf(vq, &len)))
1698a7a69ec0SMichael S. Tsirkin free_buf(buf, can_sleep);
1699a7a69ec0SMichael S. Tsirkin }
1700a7a69ec0SMichael S. Tsirkin
out_intr(struct virtqueue * vq)1701ce86d35dSLinus Torvalds static void out_intr(struct virtqueue *vq)
1702ce86d35dSLinus Torvalds {
1703ce86d35dSLinus Torvalds struct port *port;
1704ce86d35dSLinus Torvalds
1705ce86d35dSLinus Torvalds port = find_port_by_vq(vq->vdev->priv, vq);
1706a7a69ec0SMichael S. Tsirkin if (!port) {
1707a7a69ec0SMichael S. Tsirkin flush_bufs(vq, false);
1708ce86d35dSLinus Torvalds return;
1709a7a69ec0SMichael S. Tsirkin }
1710ce86d35dSLinus Torvalds
1711ce86d35dSLinus Torvalds wake_up_interruptible(&port->waitqueue);
1712ce86d35dSLinus Torvalds }
1713ce86d35dSLinus Torvalds
in_intr(struct virtqueue * vq)171451df0accSAmit Shah static void in_intr(struct virtqueue *vq)
171551df0accSAmit Shah {
171651df0accSAmit Shah struct port *port;
171751df0accSAmit Shah unsigned long flags;
171851df0accSAmit Shah
171951df0accSAmit Shah port = find_port_by_vq(vq->vdev->priv, vq);
1720a7a69ec0SMichael S. Tsirkin if (!port) {
1721a7a69ec0SMichael S. Tsirkin flush_bufs(vq, false);
172251df0accSAmit Shah return;
1723a7a69ec0SMichael S. Tsirkin }
172451df0accSAmit Shah
172551df0accSAmit Shah spin_lock_irqsave(&port->inbuf_lock, flags);
172651df0accSAmit Shah port->inbuf = get_inbuf(port);
172751df0accSAmit Shah
172851df0accSAmit Shah /*
1729aabd6a8fSSjur Brændeland * Normally the port should not accept data when the port is
1730aabd6a8fSSjur Brændeland * closed. For generic serial ports, the host won't (shouldn't)
1731aabd6a8fSSjur Brændeland * send data till the guest is connected. But this condition
173251df0accSAmit Shah * can be reached when a console port is not yet connected (no
1733aabd6a8fSSjur Brændeland * tty is spawned) and the other side sends out data over the
1734aabd6a8fSSjur Brændeland * vring, or when a remote devices start sending data before
1735aabd6a8fSSjur Brændeland * the ports are opened.
1736aabd6a8fSSjur Brændeland *
1737aabd6a8fSSjur Brændeland * A generic serial port will discard data if not connected,
1738aabd6a8fSSjur Brændeland * while console ports and rproc-serial ports accepts data at
1739aabd6a8fSSjur Brændeland * any time. rproc-serial is initiated with guest_connected to
1740aabd6a8fSSjur Brændeland * false because port_fops_open expects this. Console ports are
1741aabd6a8fSSjur Brændeland * hooked up with an HVC console and is initialized with
1742aabd6a8fSSjur Brændeland * guest_connected to true.
174351df0accSAmit Shah */
1744aabd6a8fSSjur Brændeland
1745aabd6a8fSSjur Brændeland if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
174651df0accSAmit Shah discard_port_data(port);
174751df0accSAmit Shah
1748314081f1SAmit Shah /* Send a SIGIO indicating new data in case the process asked for it */
1749314081f1SAmit Shah send_sigio_to_port(port);
1750314081f1SAmit Shah
175151df0accSAmit Shah spin_unlock_irqrestore(&port->inbuf_lock, flags);
175251df0accSAmit Shah
175351df0accSAmit Shah wake_up_interruptible(&port->waitqueue);
175451df0accSAmit Shah
175551df0accSAmit Shah if (is_console_port(port) && hvc_poll(port->cons.hvc))
175651df0accSAmit Shah hvc_kick();
175751df0accSAmit Shah }
175851df0accSAmit Shah
control_intr(struct virtqueue * vq)175951df0accSAmit Shah static void control_intr(struct virtqueue *vq)
176051df0accSAmit Shah {
176151df0accSAmit Shah struct ports_device *portdev;
176251df0accSAmit Shah
176351df0accSAmit Shah portdev = vq->vdev->priv;
176451df0accSAmit Shah schedule_work(&portdev->control_work);
176551df0accSAmit Shah }
176651df0accSAmit Shah
config_intr(struct virtio_device * vdev)176751df0accSAmit Shah static void config_intr(struct virtio_device *vdev)
176851df0accSAmit Shah {
176951df0accSAmit Shah struct ports_device *portdev;
177051df0accSAmit Shah
177151df0accSAmit Shah portdev = vdev->priv;
177251df0accSAmit Shah
1773eeb8a7e8SMichael S. Tsirkin if (!use_multiport(portdev))
1774eeb8a7e8SMichael S. Tsirkin schedule_work(&portdev->config_work);
1775eeb8a7e8SMichael S. Tsirkin }
1776eeb8a7e8SMichael S. Tsirkin
config_work_handler(struct work_struct * work)1777eeb8a7e8SMichael S. Tsirkin static void config_work_handler(struct work_struct *work)
1778eeb8a7e8SMichael S. Tsirkin {
1779eeb8a7e8SMichael S. Tsirkin struct ports_device *portdev;
1780eeb8a7e8SMichael S. Tsirkin
17818379cadfSG. Campana portdev = container_of(work, struct ports_device, config_work);
178251df0accSAmit Shah if (!use_multiport(portdev)) {
1783eeb8a7e8SMichael S. Tsirkin struct virtio_device *vdev;
178451df0accSAmit Shah struct port *port;
178551df0accSAmit Shah u16 rows, cols;
178651df0accSAmit Shah
1787eeb8a7e8SMichael S. Tsirkin vdev = portdev->vdev;
1788855e0c52SRusty Russell virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1789855e0c52SRusty Russell virtio_cread(vdev, struct virtio_console_config, rows, &rows);
179051df0accSAmit Shah
179151df0accSAmit Shah port = find_port_by_id(portdev, 0);
179251df0accSAmit Shah set_console_size(port, rows, cols);
179351df0accSAmit Shah
179451df0accSAmit Shah /*
179551df0accSAmit Shah * We'll use this way of resizing only for legacy
179651df0accSAmit Shah * support. For newer userspace
179751df0accSAmit Shah * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
179851df0accSAmit Shah * to indicate console size changes so that it can be
179951df0accSAmit Shah * done per-port.
180051df0accSAmit Shah */
180151df0accSAmit Shah resize_console(port);
180251df0accSAmit Shah }
180351df0accSAmit Shah }
180451df0accSAmit Shah
init_vqs(struct ports_device * portdev)180551df0accSAmit Shah static int init_vqs(struct ports_device *portdev)
180651df0accSAmit Shah {
180751df0accSAmit Shah vq_callback_t **io_callbacks;
180851df0accSAmit Shah char **io_names;
180951df0accSAmit Shah struct virtqueue **vqs;
181051df0accSAmit Shah u32 i, j, nr_ports, nr_queues;
181151df0accSAmit Shah int err;
181251df0accSAmit Shah
18137328fa64SMichael S. Tsirkin nr_ports = portdev->max_nr_ports;
181451df0accSAmit Shah nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
181551df0accSAmit Shah
18166da2ec56SKees Cook vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
18176da2ec56SKees Cook io_callbacks = kmalloc_array(nr_queues, sizeof(vq_callback_t *),
181851df0accSAmit Shah GFP_KERNEL);
18196da2ec56SKees Cook io_names = kmalloc_array(nr_queues, sizeof(char *), GFP_KERNEL);
18206da2ec56SKees Cook portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
18216da2ec56SKees Cook GFP_KERNEL);
18226da2ec56SKees Cook portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
182351df0accSAmit Shah GFP_KERNEL);
182451df0accSAmit Shah if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
182551df0accSAmit Shah !portdev->out_vqs) {
182651df0accSAmit Shah err = -ENOMEM;
182751df0accSAmit Shah goto free;
182851df0accSAmit Shah }
182951df0accSAmit Shah
183051df0accSAmit Shah /*
183151df0accSAmit Shah * For backward compat (newer host but older guest), the host
183251df0accSAmit Shah * spawns a console port first and also inits the vqs for port
183351df0accSAmit Shah * 0 before others.
183451df0accSAmit Shah */
183551df0accSAmit Shah j = 0;
183651df0accSAmit Shah io_callbacks[j] = in_intr;
1837ce86d35dSLinus Torvalds io_callbacks[j + 1] = out_intr;
183851df0accSAmit Shah io_names[j] = "input";
183951df0accSAmit Shah io_names[j + 1] = "output";
184051df0accSAmit Shah j += 2;
184151df0accSAmit Shah
184251df0accSAmit Shah if (use_multiport(portdev)) {
184351df0accSAmit Shah io_callbacks[j] = control_intr;
184451df0accSAmit Shah io_callbacks[j + 1] = NULL;
184551df0accSAmit Shah io_names[j] = "control-i";
184651df0accSAmit Shah io_names[j + 1] = "control-o";
184751df0accSAmit Shah
184851df0accSAmit Shah for (i = 1; i < nr_ports; i++) {
184951df0accSAmit Shah j += 2;
185051df0accSAmit Shah io_callbacks[j] = in_intr;
1851ce86d35dSLinus Torvalds io_callbacks[j + 1] = out_intr;
185251df0accSAmit Shah io_names[j] = "input";
185351df0accSAmit Shah io_names[j + 1] = "output";
185451df0accSAmit Shah }
185551df0accSAmit Shah }
185651df0accSAmit Shah /* Find the queues. */
18579b2bbdb2SMichael S. Tsirkin err = virtio_find_vqs(portdev->vdev, nr_queues, vqs,
185851df0accSAmit Shah io_callbacks,
1859fb5e31d9SChristoph Hellwig (const char **)io_names, NULL);
186051df0accSAmit Shah if (err)
186151df0accSAmit Shah goto free;
186251df0accSAmit Shah
186351df0accSAmit Shah j = 0;
186451df0accSAmit Shah portdev->in_vqs[0] = vqs[0];
186551df0accSAmit Shah portdev->out_vqs[0] = vqs[1];
186651df0accSAmit Shah j += 2;
186751df0accSAmit Shah if (use_multiport(portdev)) {
186851df0accSAmit Shah portdev->c_ivq = vqs[j];
186951df0accSAmit Shah portdev->c_ovq = vqs[j + 1];
187051df0accSAmit Shah
187151df0accSAmit Shah for (i = 1; i < nr_ports; i++) {
187251df0accSAmit Shah j += 2;
187351df0accSAmit Shah portdev->in_vqs[i] = vqs[j];
187451df0accSAmit Shah portdev->out_vqs[i] = vqs[j + 1];
187551df0accSAmit Shah }
187651df0accSAmit Shah }
187751df0accSAmit Shah kfree(io_names);
187851df0accSAmit Shah kfree(io_callbacks);
187951df0accSAmit Shah kfree(vqs);
188051df0accSAmit Shah
188151df0accSAmit Shah return 0;
188251df0accSAmit Shah
188351df0accSAmit Shah free:
188451df0accSAmit Shah kfree(portdev->out_vqs);
188551df0accSAmit Shah kfree(portdev->in_vqs);
188651df0accSAmit Shah kfree(io_names);
188751df0accSAmit Shah kfree(io_callbacks);
188851df0accSAmit Shah kfree(vqs);
188951df0accSAmit Shah
189051df0accSAmit Shah return err;
189151df0accSAmit Shah }
189251df0accSAmit Shah
189351df0accSAmit Shah static const struct file_operations portdev_fops = {
189451df0accSAmit Shah .owner = THIS_MODULE,
189551df0accSAmit Shah };
189651df0accSAmit Shah
remove_vqs(struct ports_device * portdev)1897a0e2dbfcSAmit Shah static void remove_vqs(struct ports_device *portdev)
1898a0e2dbfcSAmit Shah {
1899a7a69ec0SMichael S. Tsirkin struct virtqueue *vq;
1900a7a69ec0SMichael S. Tsirkin
1901a7a69ec0SMichael S. Tsirkin virtio_device_for_each_vq(portdev->vdev, vq) {
1902a7a69ec0SMichael S. Tsirkin struct port_buffer *buf;
1903a7a69ec0SMichael S. Tsirkin
1904a7a69ec0SMichael S. Tsirkin flush_bufs(vq, true);
1905a7a69ec0SMichael S. Tsirkin while ((buf = virtqueue_detach_unused_buf(vq)))
1906a7a69ec0SMichael S. Tsirkin free_buf(buf, true);
190756b5e65eSXianting Tian cond_resched();
1908a7a69ec0SMichael S. Tsirkin }
1909a0e2dbfcSAmit Shah portdev->vdev->config->del_vqs(portdev->vdev);
1910a0e2dbfcSAmit Shah kfree(portdev->in_vqs);
1911a0e2dbfcSAmit Shah kfree(portdev->out_vqs);
1912a0e2dbfcSAmit Shah }
1913a0e2dbfcSAmit Shah
virtcons_remove(struct virtio_device * vdev)1914aa44ec86SMichael S. Tsirkin static void virtcons_remove(struct virtio_device *vdev)
1915aa44ec86SMichael S. Tsirkin {
1916aa44ec86SMichael S. Tsirkin struct ports_device *portdev;
1917aa44ec86SMichael S. Tsirkin struct port *port, *port2;
1918aa44ec86SMichael S. Tsirkin
1919aa44ec86SMichael S. Tsirkin portdev = vdev->priv;
1920aa44ec86SMichael S. Tsirkin
1921aa44ec86SMichael S. Tsirkin spin_lock_irq(&pdrvdata_lock);
1922aa44ec86SMichael S. Tsirkin list_del(&portdev->list);
1923aa44ec86SMichael S. Tsirkin spin_unlock_irq(&pdrvdata_lock);
1924aa44ec86SMichael S. Tsirkin
19250e7174b9SMichael S. Tsirkin /* Device is going away, exit any polling for buffers */
19260e7174b9SMichael S. Tsirkin virtio_break_device(vdev);
19270e7174b9SMichael S. Tsirkin if (use_multiport(portdev))
19280e7174b9SMichael S. Tsirkin flush_work(&portdev->control_work);
19290e7174b9SMichael S. Tsirkin else
19300e7174b9SMichael S. Tsirkin flush_work(&portdev->config_work);
19310e7174b9SMichael S. Tsirkin
1932aa44ec86SMichael S. Tsirkin /* Disable interrupts for vqs */
1933d9679d00SMichael S. Tsirkin virtio_reset_device(vdev);
1934aa44ec86SMichael S. Tsirkin /* Finish up work that's lined up */
1935aa44ec86SMichael S. Tsirkin if (use_multiport(portdev))
1936aa44ec86SMichael S. Tsirkin cancel_work_sync(&portdev->control_work);
1937aa44ec86SMichael S. Tsirkin else
1938aa44ec86SMichael S. Tsirkin cancel_work_sync(&portdev->config_work);
1939aa44ec86SMichael S. Tsirkin
1940aa44ec86SMichael S. Tsirkin list_for_each_entry_safe(port, port2, &portdev->ports, list)
1941aa44ec86SMichael S. Tsirkin unplug_port(port);
1942aa44ec86SMichael S. Tsirkin
1943aa44ec86SMichael S. Tsirkin unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1944aa44ec86SMichael S. Tsirkin
1945aa44ec86SMichael S. Tsirkin /*
1946aa44ec86SMichael S. Tsirkin * When yanking out a device, we immediately lose the
1947aa44ec86SMichael S. Tsirkin * (device-side) queues. So there's no point in keeping the
1948aa44ec86SMichael S. Tsirkin * guest side around till we drop our final reference. This
1949aa44ec86SMichael S. Tsirkin * also means that any ports which are in an open state will
1950aa44ec86SMichael S. Tsirkin * have to just stop using the port, as the vqs are going
1951aa44ec86SMichael S. Tsirkin * away.
1952aa44ec86SMichael S. Tsirkin */
1953aa44ec86SMichael S. Tsirkin remove_vqs(portdev);
1954aa44ec86SMichael S. Tsirkin kfree(portdev);
1955aa44ec86SMichael S. Tsirkin }
1956aa44ec86SMichael S. Tsirkin
195751df0accSAmit Shah /*
195851df0accSAmit Shah * Once we're further in boot, we get probed like any other virtio
195951df0accSAmit Shah * device.
196051df0accSAmit Shah *
196151df0accSAmit Shah * If the host also supports multiple console ports, we check the
196251df0accSAmit Shah * config space to see how many ports the host has spawned. We
196351df0accSAmit Shah * initialize each port found.
196451df0accSAmit Shah */
virtcons_probe(struct virtio_device * vdev)19652223cbecSBill Pemberton static int virtcons_probe(struct virtio_device *vdev)
196651df0accSAmit Shah {
196751df0accSAmit Shah struct ports_device *portdev;
196851df0accSAmit Shah int err;
196951df0accSAmit Shah bool multiport;
19705e38483bSChristian Borntraeger
1971be8ff595SRusty Russell /* We only need a config space if features are offered */
1972be8ff595SRusty Russell if (!vdev->config->get &&
1973be8ff595SRusty Russell (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
1974be8ff595SRusty Russell || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
1975011f0e7aSMichael S. Tsirkin dev_err(&vdev->dev, "%s failure: config access disabled\n",
1976011f0e7aSMichael S. Tsirkin __func__);
1977011f0e7aSMichael S. Tsirkin return -EINVAL;
1978011f0e7aSMichael S. Tsirkin }
1979011f0e7aSMichael S. Tsirkin
198051df0accSAmit Shah portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
198151df0accSAmit Shah if (!portdev) {
198251df0accSAmit Shah err = -ENOMEM;
198351df0accSAmit Shah goto fail;
198451df0accSAmit Shah }
198551df0accSAmit Shah
198651df0accSAmit Shah /* Attach this portdev to this virtio_device, and vice-versa. */
198751df0accSAmit Shah portdev->vdev = vdev;
198851df0accSAmit Shah vdev->priv = portdev;
198951df0accSAmit Shah
199051df0accSAmit Shah portdev->chr_major = register_chrdev(0, "virtio-portsdev",
199151df0accSAmit Shah &portdev_fops);
199251df0accSAmit Shah if (portdev->chr_major < 0) {
199351df0accSAmit Shah dev_err(&vdev->dev,
199451df0accSAmit Shah "Error %d registering chrdev for device %u\n",
1995dc18f080SSjur Brændeland portdev->chr_major, vdev->index);
199651df0accSAmit Shah err = portdev->chr_major;
199751df0accSAmit Shah goto free;
199851df0accSAmit Shah }
199951df0accSAmit Shah
200051df0accSAmit Shah multiport = false;
20017328fa64SMichael S. Tsirkin portdev->max_nr_ports = 1;
20021b637046SSjur Brændeland
20031b637046SSjur Brændeland /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
20041b637046SSjur Brændeland if (!is_rproc_serial(vdev) &&
2005855e0c52SRusty Russell virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
2006855e0c52SRusty Russell struct virtio_console_config, max_nr_ports,
20077328fa64SMichael S. Tsirkin &portdev->max_nr_ports) == 0) {
200828962ec5SJason Wang if (portdev->max_nr_ports == 0 ||
200928962ec5SJason Wang portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
201028962ec5SJason Wang dev_err(&vdev->dev,
201128962ec5SJason Wang "Invalidate max_nr_ports %d",
201228962ec5SJason Wang portdev->max_nr_ports);
201328962ec5SJason Wang err = -EINVAL;
201428962ec5SJason Wang goto free;
201528962ec5SJason Wang }
201651c6d61aSSasha Levin multiport = true;
20171b637046SSjur Brændeland }
201851df0accSAmit Shah
201951df0accSAmit Shah err = init_vqs(portdev);
202051df0accSAmit Shah if (err < 0) {
202151df0accSAmit Shah dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
202251df0accSAmit Shah goto free_chrdev;
202351df0accSAmit Shah }
202451df0accSAmit Shah
202551df0accSAmit Shah spin_lock_init(&portdev->ports_lock);
202651df0accSAmit Shah INIT_LIST_HEAD(&portdev->ports);
20275c60300dSMichael S. Tsirkin INIT_LIST_HEAD(&portdev->list);
202851df0accSAmit Shah
202965eca3a2SCornelia Huck virtio_device_ready(portdev->vdev);
203065eca3a2SCornelia Huck
2031eeb8a7e8SMichael S. Tsirkin INIT_WORK(&portdev->config_work, &config_work_handler);
20324f6e24edSMichael S. Tsirkin INIT_WORK(&portdev->control_work, &control_work_handler);
20334f6e24edSMichael S. Tsirkin
203451df0accSAmit Shah if (multiport) {
2035165b1b8bSAmit Shah spin_lock_init(&portdev->c_ivq_lock);
20369ba5c80bSAmit Shah spin_lock_init(&portdev->c_ovq_lock);
203751df0accSAmit Shah
2038d791cfcbSLaurent Vivier err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2039d791cfcbSLaurent Vivier if (err < 0) {
204051df0accSAmit Shah dev_err(&vdev->dev,
204151df0accSAmit Shah "Error allocating buffers for control queue\n");
20425c60300dSMichael S. Tsirkin /*
20435c60300dSMichael S. Tsirkin * The host might want to notify mgmt sw about device
20445c60300dSMichael S. Tsirkin * add failure.
20455c60300dSMichael S. Tsirkin */
20465c60300dSMichael S. Tsirkin __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
20475c60300dSMichael S. Tsirkin VIRTIO_CONSOLE_DEVICE_READY, 0);
20485c60300dSMichael S. Tsirkin /* Device was functional: we need full cleanup. */
20495c60300dSMichael S. Tsirkin virtcons_remove(vdev);
2050d791cfcbSLaurent Vivier return err;
205151df0accSAmit Shah }
205251df0accSAmit Shah } else {
205351df0accSAmit Shah /*
205451df0accSAmit Shah * For backward compatibility: Create a console port
205551df0accSAmit Shah * if we're running on older host.
205651df0accSAmit Shah */
205751df0accSAmit Shah add_port(portdev, 0);
205851df0accSAmit Shah }
205951df0accSAmit Shah
206051df0accSAmit Shah spin_lock_irq(&pdrvdata_lock);
206151df0accSAmit Shah list_add_tail(&portdev->list, &pdrvdata.portdevs);
206251df0accSAmit Shah spin_unlock_irq(&pdrvdata_lock);
206351df0accSAmit Shah
206451df0accSAmit Shah __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
206551df0accSAmit Shah VIRTIO_CONSOLE_DEVICE_READY, 1);
20665e38483bSChristian Borntraeger
206751df0accSAmit Shah return 0;
206851df0accSAmit Shah
206951df0accSAmit Shah free_chrdev:
207051df0accSAmit Shah unregister_chrdev(portdev->chr_major, "virtio-portsdev");
207151df0accSAmit Shah free:
207251df0accSAmit Shah kfree(portdev);
207351df0accSAmit Shah fail:
207451df0accSAmit Shah return err;
207551df0accSAmit Shah }
207651df0accSAmit Shah
20778b66c917SRikard Falkeborn static const struct virtio_device_id id_table[] = {
207851df0accSAmit Shah { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
207951df0accSAmit Shah { 0 },
208051df0accSAmit Shah };
2081897c44f0SAlexander Lobakin MODULE_DEVICE_TABLE(virtio, id_table);
208251df0accSAmit Shah
20838b66c917SRikard Falkeborn static const unsigned int features[] = {
208451df0accSAmit Shah VIRTIO_CONSOLE_F_SIZE,
208551df0accSAmit Shah VIRTIO_CONSOLE_F_MULTIPORT,
208651df0accSAmit Shah };
208751df0accSAmit Shah
20888b66c917SRikard Falkeborn static const struct virtio_device_id rproc_serial_id_table[] = {
20891b637046SSjur Brændeland #if IS_ENABLED(CONFIG_REMOTEPROC)
20901b637046SSjur Brændeland { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
20911b637046SSjur Brændeland #endif
20921b637046SSjur Brændeland { 0 },
20931b637046SSjur Brændeland };
2094897c44f0SAlexander Lobakin MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
20951b637046SSjur Brændeland
20968b66c917SRikard Falkeborn static const unsigned int rproc_serial_features[] = {
20971b637046SSjur Brændeland };
20981b637046SSjur Brændeland
209989107000SAaron Lu #ifdef CONFIG_PM_SLEEP
virtcons_freeze(struct virtio_device * vdev)21002b8f41d8SAmit Shah static int virtcons_freeze(struct virtio_device *vdev)
21012b8f41d8SAmit Shah {
21022b8f41d8SAmit Shah struct ports_device *portdev;
21032b8f41d8SAmit Shah struct port *port;
21042b8f41d8SAmit Shah
21052b8f41d8SAmit Shah portdev = vdev->priv;
21062b8f41d8SAmit Shah
2107d9679d00SMichael S. Tsirkin virtio_reset_device(vdev);
21082b8f41d8SAmit Shah
21092055997fSMichael S. Tsirkin if (use_multiport(portdev))
2110c743d09dSAmit Shah virtqueue_disable_cb(portdev->c_ivq);
21112b8f41d8SAmit Shah cancel_work_sync(&portdev->control_work);
2112eeb8a7e8SMichael S. Tsirkin cancel_work_sync(&portdev->config_work);
2113c743d09dSAmit Shah /*
2114c743d09dSAmit Shah * Once more: if control_work_handler() was running, it would
2115c743d09dSAmit Shah * enable the cb as the last step.
2116c743d09dSAmit Shah */
21172055997fSMichael S. Tsirkin if (use_multiport(portdev))
2118c743d09dSAmit Shah virtqueue_disable_cb(portdev->c_ivq);
21192b8f41d8SAmit Shah
21202b8f41d8SAmit Shah list_for_each_entry(port, &portdev->ports, list) {
2121c743d09dSAmit Shah virtqueue_disable_cb(port->in_vq);
2122c743d09dSAmit Shah virtqueue_disable_cb(port->out_vq);
21232b8f41d8SAmit Shah /*
21242b8f41d8SAmit Shah * We'll ask the host later if the new invocation has
21252b8f41d8SAmit Shah * the port opened or closed.
21262b8f41d8SAmit Shah */
21272b8f41d8SAmit Shah port->host_connected = false;
21282b8f41d8SAmit Shah remove_port_data(port);
21292b8f41d8SAmit Shah }
21302b8f41d8SAmit Shah remove_vqs(portdev);
21312b8f41d8SAmit Shah
21322b8f41d8SAmit Shah return 0;
21332b8f41d8SAmit Shah }
21342b8f41d8SAmit Shah
virtcons_restore(struct virtio_device * vdev)21352b8f41d8SAmit Shah static int virtcons_restore(struct virtio_device *vdev)
21362b8f41d8SAmit Shah {
21372b8f41d8SAmit Shah struct ports_device *portdev;
21382b8f41d8SAmit Shah struct port *port;
21392b8f41d8SAmit Shah int ret;
21402b8f41d8SAmit Shah
21412b8f41d8SAmit Shah portdev = vdev->priv;
21422b8f41d8SAmit Shah
21432b8f41d8SAmit Shah ret = init_vqs(portdev);
21442b8f41d8SAmit Shah if (ret)
21452b8f41d8SAmit Shah return ret;
21462b8f41d8SAmit Shah
2147401bbdc9SMichael S. Tsirkin virtio_device_ready(portdev->vdev);
2148401bbdc9SMichael S. Tsirkin
21492b8f41d8SAmit Shah if (use_multiport(portdev))
2150165b1b8bSAmit Shah fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
21512b8f41d8SAmit Shah
21522b8f41d8SAmit Shah list_for_each_entry(port, &portdev->ports, list) {
21532b8f41d8SAmit Shah port->in_vq = portdev->in_vqs[port->id];
21542b8f41d8SAmit Shah port->out_vq = portdev->out_vqs[port->id];
21552b8f41d8SAmit Shah
21562b8f41d8SAmit Shah fill_queue(port->in_vq, &port->inbuf_lock);
21572b8f41d8SAmit Shah
21582b8f41d8SAmit Shah /* Get port open/close status on the host */
21592b8f41d8SAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2160fa8b66ccSAmit Shah
2161fa8b66ccSAmit Shah /*
2162fa8b66ccSAmit Shah * If a port was open at the time of suspending, we
2163fa8b66ccSAmit Shah * have to let the host know that it's still open.
2164fa8b66ccSAmit Shah */
2165fa8b66ccSAmit Shah if (port->guest_connected)
2166fa8b66ccSAmit Shah send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
21672b8f41d8SAmit Shah }
21682b8f41d8SAmit Shah return 0;
21692b8f41d8SAmit Shah }
21702b8f41d8SAmit Shah #endif
21712b8f41d8SAmit Shah
217251df0accSAmit Shah static struct virtio_driver virtio_console = {
217351df0accSAmit Shah .feature_table = features,
217451df0accSAmit Shah .feature_table_size = ARRAY_SIZE(features),
217551df0accSAmit Shah .driver.name = KBUILD_MODNAME,
217651df0accSAmit Shah .id_table = id_table,
217751df0accSAmit Shah .probe = virtcons_probe,
217851df0accSAmit Shah .remove = virtcons_remove,
217951df0accSAmit Shah .config_changed = config_intr,
218089107000SAaron Lu #ifdef CONFIG_PM_SLEEP
21812b8f41d8SAmit Shah .freeze = virtcons_freeze,
21822b8f41d8SAmit Shah .restore = virtcons_restore,
21832b8f41d8SAmit Shah #endif
218451df0accSAmit Shah };
218551df0accSAmit Shah
2186bcd2982aSGreg Kroah-Hartman static struct virtio_driver virtio_rproc_serial = {
21871b637046SSjur Brændeland .feature_table = rproc_serial_features,
21881b637046SSjur Brændeland .feature_table_size = ARRAY_SIZE(rproc_serial_features),
21891b637046SSjur Brændeland .driver.name = "virtio_rproc_serial",
21901b637046SSjur Brændeland .id_table = rproc_serial_id_table,
21911b637046SSjur Brændeland .probe = virtcons_probe,
21921b637046SSjur Brændeland .remove = virtcons_remove,
21931b637046SSjur Brændeland };
21941b637046SSjur Brændeland
virtio_console_init(void)2195fefb8a2aSRandy Dunlap static int __init virtio_console_init(void)
219651df0accSAmit Shah {
219751df0accSAmit Shah int err;
219851df0accSAmit Shah
219911680fdfSIvan Orlov err = class_register(&port_class);
220011680fdfSIvan Orlov if (err)
220151df0accSAmit Shah return err;
220251df0accSAmit Shah
220351df0accSAmit Shah pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
220451df0accSAmit Shah INIT_LIST_HEAD(&pdrvdata.consoles);
220551df0accSAmit Shah INIT_LIST_HEAD(&pdrvdata.portdevs);
220651df0accSAmit Shah
220733e1afc3SAlexey Khoroshilov err = register_virtio_driver(&virtio_console);
220833e1afc3SAlexey Khoroshilov if (err < 0) {
220933e1afc3SAlexey Khoroshilov pr_err("Error %d registering virtio driver\n", err);
221033e1afc3SAlexey Khoroshilov goto free;
221133e1afc3SAlexey Khoroshilov }
22121b637046SSjur Brændeland err = register_virtio_driver(&virtio_rproc_serial);
22131b637046SSjur Brændeland if (err < 0) {
22141b637046SSjur Brændeland pr_err("Error %d registering virtio rproc serial driver\n",
22151b637046SSjur Brændeland err);
22161b637046SSjur Brændeland goto unregister;
22171b637046SSjur Brændeland }
221833e1afc3SAlexey Khoroshilov return 0;
22191b637046SSjur Brændeland unregister:
22201b637046SSjur Brændeland unregister_virtio_driver(&virtio_console);
222133e1afc3SAlexey Khoroshilov free:
222233e1afc3SAlexey Khoroshilov debugfs_remove_recursive(pdrvdata.debugfs_dir);
222311680fdfSIvan Orlov class_unregister(&port_class);
222433e1afc3SAlexey Khoroshilov return err;
222551df0accSAmit Shah }
222651df0accSAmit Shah
virtio_console_fini(void)2227fefb8a2aSRandy Dunlap static void __exit virtio_console_fini(void)
222851df0accSAmit Shah {
22291b637046SSjur Brændeland reclaim_dma_bufs();
22301b637046SSjur Brændeland
223151df0accSAmit Shah unregister_virtio_driver(&virtio_console);
22321b637046SSjur Brændeland unregister_virtio_driver(&virtio_rproc_serial);
223351df0accSAmit Shah
223411680fdfSIvan Orlov class_unregister(&port_class);
223551df0accSAmit Shah debugfs_remove_recursive(pdrvdata.debugfs_dir);
223651df0accSAmit Shah }
2237fefb8a2aSRandy Dunlap module_init(virtio_console_init);
2238fefb8a2aSRandy Dunlap module_exit(virtio_console_fini);
223951df0accSAmit Shah
224051df0accSAmit Shah MODULE_DESCRIPTION("Virtio console driver");
224151df0accSAmit Shah MODULE_LICENSE("GPL");
2242