virtio_console.c 56.3 KB
Newer Older
1 2
/*
 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
Amit Shah's avatar
Amit Shah committed
3 4
 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
Rusty Russell's avatar
Rusty Russell committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
20
#include <linux/cdev.h>
21
#include <linux/debugfs.h>
22
#include <linux/completion.h>
23
#include <linux/device.h>
Rusty Russell's avatar
Rusty Russell committed
24
#include <linux/err.h>
25
#include <linux/freezer.h>
26
#include <linux/fs.h>
27 28
#include <linux/splice.h>
#include <linux/pagemap.h>
Rusty Russell's avatar
Rusty Russell committed
29
#include <linux/init.h>
30
#include <linux/list.h>
31 32
#include <linux/poll.h>
#include <linux/sched.h>
33
#include <linux/slab.h>
34
#include <linux/spinlock.h>
Rusty Russell's avatar
Rusty Russell committed
35 36
#include <linux/virtio.h>
#include <linux/virtio_console.h>
37
#include <linux/wait.h>
38
#include <linux/workqueue.h>
39
#include <linux/module.h>
40
#include <linux/dma-mapping.h>
41
#include "../tty/hvc/hvc_console.h"
Rusty Russell's avatar
Rusty Russell committed
42

43 44
#define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)

45 46 47 48 49 50 51 52 53
/*
 * This is a global struct for storing common data for all the devices
 * this driver handles.
 *
 * Mainly, it has a linked list for all the consoles in one place so
 * that callbacks from hvc for get_chars(), put_chars() work properly
 * across multiple devices and multiple ports per device.
 */
struct ports_driver_data {
54 55 56
	/* Used for registering chardevs */
	struct class *class;

57 58 59
	/* Used for exporting per-port information to debugfs */
	struct dentry *debugfs_dir;

60 61 62
	/* List of all the devices we're handling */
	struct list_head portdevs;

63 64 65 66 67 68 69 70 71 72 73 74
	/*
	 * This is used to keep track of the number of hvc consoles
	 * spawned by this driver.  This number is given as the first
	 * argument to hvc_alloc().  To correctly map an initial
	 * console spawned via hvc_instantiate to the console being
	 * hooked up via hvc_alloc, we need to pass the same vtermno.
	 *
	 * We also just assume the first console being initialised was
	 * the first one that got used as the initial console.
	 */
	unsigned int next_vtermno;

75 76 77 78 79
	/* All the console devices handled by this driver */
	struct list_head consoles;
};
static struct ports_driver_data pdrvdata;

80 81
static DEFINE_SPINLOCK(pdrvdata_lock);
static DECLARE_COMPLETION(early_console_added);
82

83 84 85 86 87 88 89 90
/* This struct holds information that's relevant only for console ports */
struct console {
	/* We'll place all consoles in a list in the pdrvdata struct */
	struct list_head list;

	/* The hvc device associated with this console port */
	struct hvc_struct *hvc;

91 92 93
	/* The size of the console */
	struct winsize ws;

94 95 96 97 98 99 100 101 102 103
	/*
	 * This number identifies the number that we used to register
	 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
	 * number passed on by the hvc callbacks to us to
	 * differentiate between the other console ports handled by
	 * this driver
	 */
	u32 vtermno;
};

104 105 106 107 108 109 110 111 112 113
struct port_buffer {
	char *buf;

	/* size of the buffer in *buf above */
	size_t size;

	/* used length of the buffer */
	size_t len;
	/* offset in the buf from which to consume data */
	size_t offset;
114

115 116 117 118 119 120 121 122 123
	/* DMA address of buffer */
	dma_addr_t dma;

	/* Device we got DMA memory from */
	struct device *dev;

	/* List of pending dma buffers to free */
	struct list_head list;

124 125 126 127 128
	/* If sgpages == 0 then buf is used */
	unsigned int sgpages;

	/* sg is used if spages > 0. sg must be the last in is struct */
	struct scatterlist sg[0];
129 130
};

131 132 133 134 135
/*
 * This is a per-device struct that stores data common to all the
 * ports for that device (vdev->priv).
 */
struct ports_device {
136 137 138
	/* Next portdev in the list, head is in the pdrvdata struct */
	struct list_head list;

139 140 141 142 143
	/*
	 * Workqueue handlers where we process deferred work after
	 * notification
	 */
	struct work_struct control_work;
144
	struct work_struct config_work;
145 146 147 148 149 150 151

	struct list_head ports;

	/* To protect the list of ports */
	spinlock_t ports_lock;

	/* To protect the vq operations for the control channel */
152
	spinlock_t c_ivq_lock;
153
	spinlock_t c_ovq_lock;
154

155 156
	/* max. number of ports this device can hold */
	u32 max_nr_ports;
157 158 159 160 161 162 163 164 165 166

	/* The virtio device we're associated with */
	struct virtio_device *vdev;

	/*
	 * A couple of virtqueues for the control channel: one for
	 * guest->host transfers, one for host->guest transfers
	 */
	struct virtqueue *c_ivq, *c_ovq;

167 168 169 170 171 172
	/*
	 * A control packet buffer for guest->host requests, protected
	 * by c_ovq_lock.
	 */
	struct virtio_console_control cpkt;

173 174
	/* Array of per-port IO virtqueues */
	struct virtqueue **in_vqs, **out_vqs;
175 176 177

	/* Major number for this device.  Ports will be created as minors. */
	int chr_major;
178 179
};

180 181 182 183
struct port_stats {
	unsigned long bytes_sent, bytes_received, bytes_discarded;
};

184
/* This struct holds the per-port data */
185
struct port {
186 187 188
	/* Next port in the list, head is in the ports_device */
	struct list_head list;

189 190
	/* Pointer to the parent virtio_console device */
	struct ports_device *portdev;
191 192 193

	/* The current buffer from which data has to be fed to readers */
	struct port_buffer *inbuf;
194

195 196 197 198 199 200 201
	/*
	 * To protect the operations on the in_vq associated with this
	 * port.  Has to be a spinlock because it can be called from
	 * interrupt context (get_char()).
	 */
	spinlock_t inbuf_lock;

202 203 204
	/* Protect the operations on the out_vq. */
	spinlock_t outvq_lock;

205 206 207
	/* The IO vqs for this port */
	struct virtqueue *in_vq, *out_vq;

208 209 210
	/* File in the debugfs directory that exposes this port's information */
	struct dentry *debugfs_file;

211 212 213 214 215 216 217
	/*
	 * Keep count of the bytes sent, received and discarded for
	 * this port for accounting and debugging purposes.  These
	 * counts are not reset across port open / close events.
	 */
	struct port_stats stats;

218 219 220 221 222
	/*
	 * The entries in this struct will be valid if this port is
	 * hooked up to an hvc console
	 */
	struct console cons;
223

224
	/* Each port associates with a separate char device */
225
	struct cdev *cdev;
226 227
	struct device *dev;

228 229 230
	/* Reference-counting to handle port hot-unplugs and file operations */
	struct kref kref;

231 232 233
	/* A waitqueue for poll() or blocking read operations */
	wait_queue_head_t waitqueue;

234 235 236
	/* The 'name' of the port that we expose via sysfs properties */
	char *name;

237 238 239
	/* We can notify apps of host connect / disconnect events via SIGIO */
	struct fasync_struct *async_queue;

240 241
	/* The 'id' to identify the port with the Host */
	u32 id;
242

243 244
	bool outvq_full;

245 246
	/* Is the host device open */
	bool host_connected;
247 248 249

	/* We should allow only one process to open a port */
	bool guest_connected;
250
};
Rusty Russell's avatar
Rusty Russell committed
251

252 253 254
/* This is the very early arch-specified put chars function. */
static int (*early_put_chars)(u32, const char *, int);

255 256 257
static struct port *find_port_by_vtermno(u32 vtermno)
{
	struct port *port;
258
	struct console *cons;
259 260 261
	unsigned long flags;

	spin_lock_irqsave(&pdrvdata_lock, flags);
262 263 264
	list_for_each_entry(cons, &pdrvdata.consoles, list) {
		if (cons->vtermno == vtermno) {
			port = container_of(cons, struct port, cons);
265
			goto out;
266
		}
267 268 269 270 271 272 273
	}
	port = NULL;
out:
	spin_unlock_irqrestore(&pdrvdata_lock, flags);
	return port;
}

274 275 276 277 278 279 280
static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
						 dev_t dev)
{
	struct port *port;
	unsigned long flags;

	spin_lock_irqsave(&portdev->ports_lock, flags);
281 282 283
	list_for_each_entry(port, &portdev->ports, list) {
		if (port->cdev->dev == dev) {
			kref_get(&port->kref);
284
			goto out;
285 286
		}
	}
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
	port = NULL;
out:
	spin_unlock_irqrestore(&portdev->ports_lock, flags);

	return port;
}

static struct port *find_port_by_devt(dev_t dev)
{
	struct ports_device *portdev;
	struct port *port;
	unsigned long flags;

	spin_lock_irqsave(&pdrvdata_lock, flags);
	list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
		port = find_port_by_devt_in_portdev(portdev, dev);
		if (port)
			goto out;
	}
	port = NULL;
out:
	spin_unlock_irqrestore(&pdrvdata_lock, flags);
	return port;
}

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
{
	struct port *port;
	unsigned long flags;

	spin_lock_irqsave(&portdev->ports_lock, flags);
	list_for_each_entry(port, &portdev->ports, list)
		if (port->id == id)
			goto out;
	port = NULL;
out:
	spin_unlock_irqrestore(&portdev->ports_lock, flags);

	return port;
}

328 329 330 331 332 333
static struct port *find_port_by_vq(struct ports_device *portdev,
				    struct virtqueue *vq)
{
	struct port *port;
	unsigned long flags;

334 335
	spin_lock_irqsave(&portdev->ports_lock, flags);
	list_for_each_entry(port, &portdev->ports, list)
336 337 338 339
		if (port->in_vq == vq || port->out_vq == vq)
			goto out;
	port = NULL;
out:
340
	spin_unlock_irqrestore(&portdev->ports_lock, flags);
341 342 343
	return port;
}

344 345 346 347 348 349 350
static bool is_console_port(struct port *port)
{
	if (port->cons.hvc)
		return true;
	return false;
}

351 352 353 354 355
static bool is_rproc_serial(const struct virtio_device *vdev)
{
	return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
}

356 357 358 359 360 361 362
static inline bool use_multiport(struct ports_device *portdev)
{
	/*
	 * This condition can be true when put_chars is called from
	 * early_init
	 */
	if (!portdev->vdev)
363
		return false;
364
	return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
365 366
}

367 368 369 370
static DEFINE_SPINLOCK(dma_bufs_lock);
static LIST_HEAD(pending_free_dma_bufs);

static void free_buf(struct port_buffer *buf, bool can_sleep)
371
{
372 373 374 375 376 377 378 379 380
	unsigned int i;

	for (i = 0; i < buf->sgpages; i++) {
		struct page *page = sg_page(&buf->sg[i]);
		if (!page)
			break;
		put_page(page);
	}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	if (!buf->dev) {
		kfree(buf->buf);
	} else if (is_rproc_enabled) {
		unsigned long flags;

		/* dma_free_coherent requires interrupts to be enabled. */
		if (!can_sleep) {
			/* queue up dma-buffers to be freed later */
			spin_lock_irqsave(&dma_bufs_lock, flags);
			list_add_tail(&buf->list, &pending_free_dma_bufs);
			spin_unlock_irqrestore(&dma_bufs_lock, flags);
			return;
		}
		dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);

		/* Release device refcnt and allow it to be freed */
		put_device(buf->dev);
	}

400 401 402
	kfree(buf);
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
static void reclaim_dma_bufs(void)
{
	unsigned long flags;
	struct port_buffer *buf, *tmp;
	LIST_HEAD(tmp_list);

	if (list_empty(&pending_free_dma_bufs))
		return;

	/* Create a copy of the pending_free_dma_bufs while holding the lock */
	spin_lock_irqsave(&dma_bufs_lock, flags);
	list_cut_position(&tmp_list, &pending_free_dma_bufs,
			  pending_free_dma_bufs.prev);
	spin_unlock_irqrestore(&dma_bufs_lock, flags);

	/* Release the dma buffers, without irqs enabled */
	list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
		list_del(&buf->list);
		free_buf(buf, true);
	}
}

425
static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
426
				     int pages)
427 428 429
{
	struct port_buffer *buf;

430 431
	reclaim_dma_bufs();

432 433 434 435 436 437
	/*
	 * Allocate buffer and the sg list. The sg list array is allocated
	 * directly after the port_buffer struct.
	 */
	buf = kmalloc(sizeof(*buf) + sizeof(struct scatterlist) * pages,
		      GFP_KERNEL);
438 439
	if (!buf)
		goto fail;
440 441 442

	buf->sgpages = pages;
	if (pages > 0) {
443
		buf->dev = NULL;
444 445 446 447
		buf->buf = NULL;
		return buf;
	}

448
	if (is_rproc_serial(vdev)) {
449 450 451 452 453 454
		/*
		 * Allocate DMA memory from ancestor. When a virtio
		 * device is created by remoteproc, the DMA memory is
		 * associated with the grandparent device:
		 * vdev => rproc => platform-dev.
		 */
455
		if (!vdev->dev.parent || !vdev->dev.parent->parent)
456
			goto free_buf;
457
		buf->dev = vdev->dev.parent->parent;
458 459 460 461 462 463 464 465 466 467

		/* Increase device refcnt to avoid freeing it */
		get_device(buf->dev);
		buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
					      GFP_KERNEL);
	} else {
		buf->dev = NULL;
		buf->buf = kmalloc(buf_size, GFP_KERNEL);
	}

468 469 470 471 472 473 474 475 476 477 478 479 480
	if (!buf->buf)
		goto free_buf;
	buf->len = 0;
	buf->offset = 0;
	buf->size = buf_size;
	return buf;

free_buf:
	kfree(buf);
fail:
	return NULL;
}

481
/* Callers should take appropriate locks */
482
static struct port_buffer *get_inbuf(struct port *port)
483 484 485 486
{
	struct port_buffer *buf;
	unsigned int len;

487 488 489 490
	if (port->inbuf)
		return port->inbuf;

	buf = virtqueue_get_buf(port->in_vq, &len);
491 492 493
	if (buf) {
		buf->len = len;
		buf->offset = 0;
494
		port->stats.bytes_received += len;
495 496 497 498
	}
	return buf;
}

499 500 501 502 503 504
/*
 * Create a scatter-gather list representing our input buffer and put
 * it in the queue.
 *
 * Callers should take appropriate locks.
 */
505
static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
506 507
{
	struct scatterlist sg[1];
508
	int ret;
509

510 511
	sg_init_one(sg, buf->buf, buf->size);

512
	ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
513
	virtqueue_kick(vq);
514 515
	if (!ret)
		ret = vq->num_free;
516 517 518
	return ret;
}

519 520 521 522
/* Discard any unread data this port has. Callers lockers. */
static void discard_port_data(struct port *port)
{
	struct port_buffer *buf;
523
	unsigned int err;
524

525 526 527 528
	if (!port->portdev) {
		/* Device has been unplugged.  vqs are already gone. */
		return;
	}
529
	buf = get_inbuf(port);
530

Amit Shah's avatar
Amit Shah committed
531
	err = 0;
532
	while (buf) {
533
		port->stats.bytes_discarded += buf->len - buf->offset;
534
		if (add_inbuf(port->in_vq, buf) < 0) {
Amit Shah's avatar
Amit Shah committed
535
			err++;
536
			free_buf(buf, false);
537
		}
538 539
		port->inbuf = NULL;
		buf = get_inbuf(port);
540
	}
Amit Shah's avatar
Amit Shah committed
541
	if (err)
542
		dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
Amit Shah's avatar
Amit Shah committed
543
			 err);
544 545
}

546 547 548 549 550
static bool port_has_data(struct port *port)
{
	unsigned long flags;
	bool ret;

551
	ret = false;
552
	spin_lock_irqsave(&port->inbuf_lock, flags);
553
	port->inbuf = get_inbuf(port);
554
	if (port->inbuf)
555
		ret = true;
556

557 558 559 560
	spin_unlock_irqrestore(&port->inbuf_lock, flags);
	return ret;
}

561 562
static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
				  unsigned int event, unsigned int value)
563 564 565
{
	struct scatterlist sg[1];
	struct virtqueue *vq;
566
	unsigned int len;
567

568
	if (!use_multiport(portdev))
569 570
		return 0;

571
	vq = portdev->c_ovq;
572

573
	spin_lock(&portdev->c_ovq_lock);
574 575 576 577 578 579 580 581

	portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
	portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
	portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);

	sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));

	if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
582
		virtqueue_kick(vq);
583 584
		while (!virtqueue_get_buf(vq, &len)
			&& !virtqueue_is_broken(vq))
585 586
			cpu_relax();
	}
587

588
	spin_unlock(&portdev->c_ovq_lock);
589 590 591
	return 0;
}

592 593 594
static ssize_t send_control_msg(struct port *port, unsigned int event,
				unsigned int value)
{
595 596 597 598
	/* Did the port get unplugged before userspace closed it? */
	if (port->portdev)
		return __send_control_msg(port->portdev, port->id, event, value);
	return 0;
599 600
}

601

602 603 604
/* Callers must take the port->outvq_lock */
static void reclaim_consumed_buffers(struct port *port)
{
605
	struct port_buffer *buf;
606 607
	unsigned int len;

608 609 610 611
	if (!port->portdev) {
		/* Device has been unplugged.  vqs are already gone. */
		return;
	}
612
	while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
613
		free_buf(buf, false);
614 615 616 617
		port->outvq_full = false;
	}
}

618 619
static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
			      int nents, size_t in_count,
620
			      void *data, bool nonblock)
621 622
{
	struct virtqueue *out_vq;
623
	int err;
624
	unsigned long flags;
625 626 627 628
	unsigned int len;

	out_vq = port->out_vq;

629 630 631 632
	spin_lock_irqsave(&port->outvq_lock, flags);

	reclaim_consumed_buffers(port);

633
	err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
634 635

	/* Tell Host to go! */
636
	virtqueue_kick(out_vq);
637

638
	if (err) {
639
		in_count = 0;
640
		goto done;
641 642
	}

643
	if (out_vq->num_free == 0)
644 645 646 647 648 649 650
		port->outvq_full = true;

	if (nonblock)
		goto done;

	/*
	 * Wait till the host acknowledges it pushed out the data we
651 652 653 654 655 656
	 * sent.  This is done for data from the hvc_console; the tty
	 * operations are performed with spinlocks held so we can't
	 * sleep here.  An alternative would be to copy the data to a
	 * buffer and relax the spinning requirement.  The downside is
	 * we need to kmalloc a GFP_ATOMIC buffer each time the
	 * console driver writes something out.
657
	 */
658 659
	while (!virtqueue_get_buf(out_vq, &len)
		&& !virtqueue_is_broken(out_vq))
660
		cpu_relax();
661 662
done:
	spin_unlock_irqrestore(&port->outvq_lock, flags);
663 664

	port->stats.bytes_sent += in_count;
665 666 667 668
	/*
	 * We're expected to return the amount of data we wrote -- all
	 * of it
	 */
669
	return in_count;
670 671
}

672 673 674 675
/*
 * Give out the data that's requested from the buffer that we have
 * queued up.
 */
676 677
static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
			    size_t out_count, bool to_user)
678 679 680 681 682 683 684 685
{
	struct port_buffer *buf;
	unsigned long flags;

	if (!out_count || !port_has_data(port))
		return 0;

	buf = port->inbuf;
686
	out_count = min(out_count, buf->len - buf->offset);
687

688 689 690 691 692 693 694
	if (to_user) {
		ssize_t ret;

		ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
		if (ret)
			return -EFAULT;
	} else {
695 696
		memcpy((__force char *)out_buf, buf->buf + buf->offset,
		       out_count);
697
	}
698 699 700 701 702 703 704 705 706 707 708 709

	buf->offset += out_count;

	if (buf->offset == buf->len) {
		/*
		 * We're done using all the data in this buffer.
		 * Re-queue so that the Host can send us more data.
		 */
		spin_lock_irqsave(&port->inbuf_lock, flags);
		port->inbuf = NULL;

		if (add_inbuf(port->in_vq, buf) < 0)
710
			dev_warn(port->dev, "failed add_buf\n");
711 712 713

		spin_unlock_irqrestore(&port->inbuf_lock, flags);
	}
714
	/* Return the number of bytes actually copied */
715
	return out_count;
716 717
}

718
/* The condition that must be true for polling to end */
719
static bool will_read_block(struct port *port)
720
{
721 722 723 724
	if (!port->guest_connected) {
		/* Port got hot-unplugged. Let's exit. */
		return false;
	}
725
	return !port_has_data(port) && port->host_connected;
726 727
}

728 729 730 731
static bool will_write_block(struct port *port)
{
	bool ret;

732 733 734 735
	if (!port->guest_connected) {
		/* Port got hot-unplugged. Let's exit. */
		return false;
	}
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	if (!port->host_connected)
		return true;

	spin_lock_irq(&port->outvq_lock);
	/*
	 * Check if the Host has consumed any buffers since we last
	 * sent data (this is only applicable for nonblocking ports).
	 */
	reclaim_consumed_buffers(port);
	ret = port->outvq_full;
	spin_unlock_irq(&port->outvq_lock);

	return ret;
}

751 752 753 754 755 756 757 758
static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
			      size_t count, loff_t *offp)
{
	struct port *port;
	ssize_t ret;

	port = filp->private_data;

759 760 761 762
	/* Port is hot-unplugged. */
	if (!port->guest_connected)
		return -ENODEV;

763 764 765 766 767 768 769 770 771 772 773
	if (!port_has_data(port)) {
		/*
		 * If nothing's connected on the host just return 0 in
		 * case of list_empty; this tells the userspace app
		 * that there's no connection
		 */
		if (!port->host_connected)
			return 0;
		if (filp->f_flags & O_NONBLOCK)
			return -EAGAIN;

774 775
		ret = wait_event_freezable(port->waitqueue,
					   !will_read_block(port));
776 777 778
		if (ret < 0)
			return ret;
	}
779
	/* Port got hot-unplugged while we were waiting above. */
780 781
	if (!port->guest_connected)
		return -ENODEV;
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
	/*
	 * We could've received a disconnection message while we were
	 * waiting for more data.
	 *
	 * This check is not clubbed in the if() statement above as we
	 * might receive some data as well as the host could get
	 * disconnected after we got woken up from our wait.  So we
	 * really want to give off whatever data we have and only then
	 * check for host_connected.
	 */
	if (!port_has_data(port) && !port->host_connected)
		return 0;

	return fill_readbuf(port, ubuf, count, true);
}

798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
static int wait_port_writable(struct port *port, bool nonblock)
{
	int ret;

	if (will_write_block(port)) {
		if (nonblock)
			return -EAGAIN;

		ret = wait_event_freezable(port->waitqueue,
					   !will_write_block(port));
		if (ret < 0)
			return ret;
	}
	/* Port got hot-unplugged. */
	if (!port->guest_connected)
		return -ENODEV;

	return 0;
}

818 819 820 821
static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
			       size_t count, loff_t *offp)
{
	struct port *port;
822
	struct port_buffer *buf;
823
	ssize_t ret;
824
	bool nonblock;
825
	struct scatterlist sg[1];
826

827 828 829 830
	/* Userspace could be out to fool us */
	if (!count)
		return 0;

831 832
	port = filp->private_data;

833 834
	nonblock = filp->f_flags & O_NONBLOCK;

835 836 837
	ret = wait_port_writable(port, nonblock);
	if (ret < 0)
		return ret;
838

839 840
	count = min((size_t)(32 * 1024), count);

841
	buf = alloc_buf(port->portdev->vdev, count, 0);
842 843 844
	if (!buf)
		return -ENOMEM;

845
	ret = copy_from_user(buf->buf, ubuf, count);
846 847 848 849 850
	if (ret) {
		ret = -EFAULT;
		goto free_buf;
	}

851 852 853 854 855 856 857 858
	/*
	 * We now ask send_buf() to not spin for generic ports -- we
	 * can re-use the same code path that non-blocking file
	 * descriptors take for blocking file descriptors since the
	 * wait is already done and we're certain the write will go
	 * through to the host.
	 */
	nonblock = true;
859 860
	sg_init_one(sg, buf->buf, count);
	ret = __send_to_port(port, sg, 1, count, buf, nonblock);
861 862 863 864

	if (nonblock && ret > 0)
		goto out;

865
free_buf:
866
	free_buf(buf, true);
867
out:
868 869 870
	return ret;
}

871 872
struct sg_list {
	unsigned int n;
873
	unsigned int size;
874 875 876 877 878 879 880 881
	size_t len;
	struct scatterlist *sg;
};

static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
			struct splice_desc *sd)
{
	struct sg_list *sgl = sd->u.data;
882
	unsigned int offset, len;
883

884
	if (sgl->n == sgl->size)
885 886 887
		return 0;

	/* Try lock this page */
888
	if (pipe_buf_steal(pipe, buf) == 0) {
889 890 891 892 893 894
		/* Get reference and unlock page for moving */
		get_page(buf->page);
		unlock_page(buf->page);

		len = min(buf->len, sd->len);
		sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
895 896 897
	} else {
		/* Failback to copying a page */
		struct page *page = alloc_page(GFP_KERNEL);
Al Viro's avatar
Al Viro committed
898
		char *src;
899 900 901 902 903 904 905 906 907 908

		if (!page)
			return -ENOMEM;

		offset = sd->pos & ~PAGE_MASK;

		len = sd->len;
		if (len + offset > PAGE_SIZE)
			len = PAGE_SIZE - offset;

Al Viro's avatar
Al Viro committed
909
		src = kmap_atomic(buf->page);
Al Viro's avatar
Al Viro committed
910
		memcpy(page_address(page) + offset, src + buf->offset, len);
Al Viro's avatar
Al Viro committed
911
		kunmap_atomic(src);
912 913

		sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
914
	}
915 916
	sgl->n++;
	sgl->len += len;
917 918 919 920 921 922 923 924 925 926 927 928

	return len;
}

/* Faster zero-copy write by splicing */
static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
				      struct file *filp, loff_t *ppos,
				      size_t len, unsigned int flags)
{
	struct port *port = filp->private_data;
	struct sg_list sgl;
	ssize_t ret;
929
	struct port_buffer *buf;
930 931 932 933 934 935 936
	struct splice_desc sd = {
		.total_len = len,
		.flags = flags,
		.pos = *ppos,
		.u.data = &sgl,
	};

937 938 939 940 941 942 943 944 945
	/*
	 * Rproc_serial does not yet support splice. To support splice
	 * pipe_to_sg() must allocate dma-buffers and copy content from
	 * regular pages to dma pages. And alloc_buf and free_buf must
	 * support allocating and freeing such a list of dma-buffers.
	 */
	if (is_rproc_serial(port->out_vq->vdev))
		return -EINVAL;

946 947 948 949
	/*
	 * pipe->nrbufs == 0 means there are no data to transfer,
	 * so this returns just 0 for no data.
	 */
950 951 952 953 954
	pipe_lock(pipe);
	if (!pipe->nrbufs) {
		ret = 0;
		goto error_out;
	}
955

956 957
	ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
	if (ret < 0)
958
		goto error_out;
959

960
	buf = alloc_buf(port->portdev->vdev, 0, pipe->nrbufs);
961 962 963 964
	if (!buf) {
		ret = -ENOMEM;
		goto error_out;
	}
965

966 967
	sgl.n = 0;
	sgl.len = 0;
968
	sgl.size = pipe->nrbufs;
969
	sgl.sg = buf->sg;
970
	sg_init_table(sgl.sg, sgl.size);
971
	ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
972
	pipe_unlock(pipe);
973
	if (likely(ret > 0))
974
		ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
975

976
	if (unlikely(ret <= 0))
977
		free_buf(buf, true);
978
	return ret;
979 980 981 982

error_out:
	pipe_unlock(pipe);
	return ret;
983 984
}

985 986 987 988 989 990 991 992
static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
{
	struct port *port;
	unsigned int ret;

	port = filp->private_data;
	poll_wait(filp, &port->waitqueue, wait);

993 994 995 996
	if (!port->guest_connected) {
		/* Port got unplugged */
		return POLLHUP;
	}
997
	ret = 0;
998
	if (!will_read_block(port))
999
		ret |= POLLIN | POLLRDNORM;
1000
	if (!will_write_block(port))
1001 1002 1003 1004 1005 1006 1007
		ret |= POLLOUT;
	if (!port->host_connected)
		ret |= POLLHUP;

	return ret;
}

1008 1009
static void remove_port(struct kref *kref);

1010 1011 1012 1013 1014 1015 1016 1017 1018
static int port_fops_release(struct inode *inode, struct file *filp)
{
	struct port *port;

	port = filp->private_data;

	/* Notify host of port being closed */
	send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);

1019
	spin_lock_irq(&port->inbuf_lock);
1020 1021
	port->guest_connected = false;

1022 1023 1024 1025
	discard_port_data(port);

	spin_unlock_irq(&port->inbuf_lock);

1026 1027 1028 1029
	spin_lock_irq(&port->outvq_lock);
	reclaim_consumed_buffers(port);
	spin_unlock_irq(&port->outvq_lock);

1030
	reclaim_dma_bufs();
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
	/*
	 * Locks aren't necessary here as a port can't be opened after
	 * unplug, and if a port isn't unplugged, a kref would already
	 * exist for the port.  Plus, taking ports_lock here would
	 * create a dependency on other locks taken by functions
	 * inside remove_port if we're the last holder of the port,
	 * creating many problems.
	 */
	kref_put(&port->kref, remove_port);

1041 1042 1043 1044 1045 1046 1047
	return 0;
}

static int port_fops_open(struct inode *inode, struct file *filp)
{
	struct cdev *cdev = inode->i_cdev;
	struct port *port;
1048
	int ret;
1049

1050
	/* We get the port with a kref here */
1051
	port = find_port_by_devt(cdev->dev);
1052 1053 1054 1055
	if (!port) {
		/* Port was unplugged before we could proceed */
		return -ENXIO;
	}
1056 1057 1058 1059 1060 1061
	filp->private_data = port;

	/*
	 * Don't allow opening of console port devices -- that's done
	 * via /dev/hvc
	 */
1062 1063 1064 1065
	if (is_console_port(port)) {
		ret = -ENXIO;
		goto out;
	}
1066

1067 1068 1069 1070
	/* Allow only one process to open a particular port at a time */
	spin_lock_irq(&port->inbuf_lock);
	if (port->guest_connected) {
		spin_unlock_irq(&port->inbuf_lock);
1071
		ret = -EBUSY;
1072
		goto out;
1073 1074 1075 1076 1077
	}

	port->guest_connected = true;
	spin_unlock_irq(&port->inbuf_lock);

1078 1079 1080 1081 1082 1083 1084 1085 1086
	spin_lock_irq(&port->outvq_lock);
	/*
	 * There might be a chance that we missed reclaiming a few
	 * buffers in the window of the port getting previously closed
	 * and opening now.
	 */
	reclaim_consumed_buffers(port);
	spin_unlock_irq(&port->outvq_lock);

1087 1088
	nonseekable_open(inode, filp);

1089 1090 1091 1092
	/* Notify host of port being opened */
	send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);

	return 0;
1093
out:
1094
	kref_put(&port->kref, remove_port);
1095
	return ret;
1096 1097
}

1098 1099 1100 1101 1102 1103 1104 1105
static int port_fops_fasync(int fd, struct file *filp, int mode)
{
	struct port *port;

	port = filp->private_data;
	return fasync_helper(fd, filp, mode, &port->async_queue);
}

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
/*
 * The file operations that we support: programs in the guest can open
 * a console device, read from it, write to it, poll for data and
 * close it.  The devices are at
 *   /dev/vport<device number>p<port number>
 */
static const struct file_operations port_fops = {
	.owner = THIS_MODULE,
	.open  = port_fops_open,
	.read  = port_fops_read,
	.write = port_fops_write,
1117
	.splice_write = port_fops_splice_write,
1118 1119
	.poll  = port_fops_poll,
	.release = port_fops_release,
1120
	.fasync = port_fops_fasync,
1121
	.llseek = no_llseek,
1122 1123
};

1124 1125
/*
 * The put_chars() callback is pretty straightforward.
Rusty Russell's avatar
Rusty Russell committed
1126
 *
1127 1128 1129
 * We turn the characters into a scatter-gather list, add it to the
 * output queue and then kick the Host.  Then we sit here waiting for
 * it to finish: inefficient in theory, but in practice
1130
 * implementations will do it immediately.
1131
 */